Skip to content

Commit

Permalink
Merge pull request #37 from ldez/fix/cgo
Browse files Browse the repository at this point in the history
fix: cgo support
  • Loading branch information
tetafro authored Dec 23, 2024
2 parents 65c6c1e + 440e395 commit 9319d76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 59 deletions.
50 changes: 18 additions & 32 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,22 @@ func newParsedFile(file *ast.File, fset *token.FileSet) (*parsedFile, error) {
file: file,
}

var err error

// Read original file. This is necessary for making a replacements for
// inline comments. I couldn't find a better way to get original line
// with code and comment without reading the file. Function `Format`
// from "go/format" won't help here if the original file is not gofmt-ed.
pf.lines, err = readFile(file, fset)
if err != nil {
return nil, fmt.Errorf("read file: %w", 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
filename := getFilename(fset, file)

if !strings.HasSuffix(filename, ".go") {
return nil, errEmptyInput
}

// Check consistency to avoid checking slice indexes in each function.
// Note that `PositionFor` is used with `adjusted=false` to skip `//line`
// directives that can set references to other files (e.g. templates)
// instead of the real ones, and break consistency here.
// Issue: https://github.com/tetafro/godot/issues/32
lastComment := pf.file.Comments[len(pf.file.Comments)-1]
if p := pf.fset.PositionFor(lastComment.End(), false); len(pf.lines) < p.Line {
return nil, fmt.Errorf("inconsistency between file and AST: %s", p.Filename)
var err error

pf.lines, err = readFile(filename)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}

return &pf, nil
Expand Down Expand Up @@ -244,12 +230,12 @@ func getText(comment *ast.CommentGroup, exclude []*regexp.Regexp) (s string) {
}

// readFile reads file and returns its lines as strings.
func readFile(file *ast.File, fset *token.FileSet) ([]string, error) {
fname := fset.File(file.Package)
f, err := os.ReadFile(fname.Name())
func readFile(filename string) ([]string, error) {
f, err := os.ReadFile(filename)
if err != nil {
return nil, err //nolint:wrapcheck
}

return strings.Split(string(f), "\n"), nil
}

Expand All @@ -275,11 +261,11 @@ 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
}
func getFilename(fset *token.FileSet, file *ast.File) string {
filename := fset.PositionFor(file.Pos(), true).Filename
if !strings.HasSuffix(filename, ".go") {
return fset.PositionFor(file.Pos(), false).Filename
}
return false

return filename
}
21 changes: 0 additions & 21 deletions getters_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package godot

import (
"errors"
"go/ast"
"go/parser"
"go/token"
Expand Down Expand Up @@ -70,26 +69,6 @@ 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 !errors.Is(err, errUnsuitableInput) {
t.Fatalf(
"Unexpected error:\n expected: %v\n got: %v",
errUnsuitableInput, err,
)
}
})
}

func TestGetText(t *testing.T) {
Expand Down
6 changes: 0 additions & 6 deletions testdata/get/cgo.go

This file was deleted.

0 comments on commit 9319d76

Please sign in to comment.