Skip to content

Commit

Permalink
Fix working with cgo generated files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed May 23, 2021
1 parent 17fccac commit 8a1c19a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
25 changes: 24 additions & 1 deletion getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"strings"
)

var errEmptyInput = errors.New("empty input")
var (
errEmptyInput = errors.New("empty input")
errUnsuitableInput = errors.New("unsuitable input")
)

// specialReplacer is a replacer for some types of special lines in comments,
// which shouldn't be checked. For example, if comment ends with a block of
Expand Down Expand Up @@ -44,6 +47,17 @@ func newParsedFile(file *ast.File, fset *token.FileSet) (*parsedFile, error) {
return nil, fmt.Errorf("read file: %v", err)
}

// Dirty hack. For some cases Go generates temporary files during
// compilation process if there is a cgo block in the source file. Some of
// these temporary files are just copies of original source files but with
// new generated comments at the top. Because of them the content differs
// from AST. For some reason it differs only in golangci-lint. I failed to
// find out the exact description of the process, so let's just skip files
// generated by cgo.
if isCgoGenerated(pf.lines) {
return nil, errUnsuitableInput
}

// Check consistency to avoid checking slice indexes in each function
lastComment := pf.file.Comments[len(pf.file.Comments)-1]
if p := pf.fset.Position(lastComment.End()); len(pf.lines) < p.Line {
Expand Down Expand Up @@ -258,3 +272,12 @@ func matchAny(s string, rr []*regexp.Regexp) bool {
}
return false
}

func isCgoGenerated(lines []string) bool {
for i := range lines {
if strings.Contains(lines[i], "Code generated by cmd/cgo") {
return true
}
}
return false
}
20 changes: 20 additions & 0 deletions getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ func TestGetComments(t *testing.T) {
}
})
}

t.Run("try to get comments from cgo generated file", func(t *testing.T) {
testFile := filepath.Join("testdata", "get", "cgo.go")
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
if err != nil {
t.Fatalf("Failed to parse input file: %v", err)
}

pf, err := newParsedFile(file, fset)
if pf != nil {
t.Fatalf("Unexpected file content")
}
if err != errUnsuitableInput {
t.Fatalf(
"Unexpected error:\n expected: %v\n got: %v",
errUnsuitableInput, err,
)
}
})
}

func TestGetText(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type comment struct {
// Run runs this linter on the provided code.
func Run(file *ast.File, fset *token.FileSet, settings Settings) ([]Issue, error) {
pf, err := newParsedFile(file, fset)
if err == errEmptyInput {
if err == errEmptyInput || err == errUnsuitableInput {
return nil, nil
}
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions testdata/get/cgo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8a1c19a

Please sign in to comment.