Skip to content

Commit

Permalink
Fix false positive for exported cgo code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed May 4, 2020
1 parent ac94f1e commit 8ff8ae1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ Build from source
go get -u github.com/tetafro/godot/cmd/godot
```

Or download binary from [releases page](https://github.com/tetafro/godot/releases)
like this
```sh
version=0.3.6
platform=linux_amd64
curl -L https://github.com/tetafro/godot/releases/download/v${version}/godot_${version}_${platform}.tar.gz | tar xzf - -C $GOPATH/bin
```
or download binary from [releases page](https://github.com/tetafro/godot/releases).

Run
```sh
Expand Down
4 changes: 4 additions & 0 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func checkLastChar(s string) bool {
if strings.HasPrefix(s, " ") || strings.HasPrefix(s, " \t") || strings.HasPrefix(s, "\t") {
return true
}
// Skip cgo export tags: https://golang.org/cmd/cgo/#hdr-C_references_to_Go
if strings.HasPrefix(s, "export") {
return true
}
s = strings.TrimSpace(s)
if tags.MatchString(s) ||
hashtags.MatchString(s) ||
Expand Down
10 changes: 8 additions & 2 deletions godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func TestCheckComment(t *testing.T) {
ok: true,
line: 0,
},
{
name: "singleline comment: cgo exported function",
comment: "//export FuncName",
ok: true,
line: 0,
},
{
name: "singleline comment: url at the end of line",
comment: "// Read more: http://example.com/",
Expand Down Expand Up @@ -227,7 +233,7 @@ func TestCheckComment(t *testing.T) {

func TestIntegration(t *testing.T) {
t.Run("default check", func(t *testing.T) {
var testFile = filepath.Join("testdata", "example_default.go")
var testFile = filepath.Join("testdata", "default", "example.go")
expected, err := readTestFile(testFile)
if err != nil {
t.Fatalf("Failed to read test file %s: %v", testFile, err)
Expand All @@ -254,7 +260,7 @@ func TestIntegration(t *testing.T) {
})

t.Run("check all", func(t *testing.T) {
var testFile = filepath.Join("testdata", "example_checkall.go")
var testFile = filepath.Join("testdata", "checkall", "example.go")
expected, err := readTestFile(testFile)
if err != nil {
t.Fatalf("Failed to read test file %s: %v", testFile, err)
Expand Down
23 changes: 15 additions & 8 deletions testdata/example_checkall.go → testdata/checkall/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package example
void myprint(char* s) {
printf("%d\n", s);
}
# PASS
*/
import "C"
import "unsafe"
Expand All @@ -34,34 +36,34 @@ Multiline comment with a period PASS.
// Single-line comment with a period PASS.

// Declaration comment without a period FAIL
type ObjectX struct {
type SimpleObject struct {
// Exported field comment - always PASS
Type string
// Unexported field comment - always PASS
secret int
}

// Declaration comment without a period, with an indented code example:
// co := ComplexObjectX{}
// co := ComplexObject{}
// fmt.Println(co) // PASS
type ComplexObjectX struct {
type ComplexObject struct {
// Exported field comment - always PASS
Type string
// Unexported field comment - always PASS
secret int
}

// Declaration comment without a period, with a mixed indented code example:
// co := MessageX{}
// co := Message{}
// fmt.Println(co) // PASS
type MessageX struct {
type Message struct {
Type string
}

// Declaration multiline comment
// second line
// third line with a period PASS.
func SumX(a, b int) int {
func Sum(a, b int) int {
// Inner comment - always PASS
a++
b++
Expand All @@ -72,11 +74,16 @@ func SumX(a, b int) int {
// Declaration multiline comment
// second line
// third line without a period FAIL
func MultX(a, b int) int {
func Mult(a, b int) int {
return a * b
}

func noCommentX() {
//export CgoExportedFunction PASS
func CgoExportedFunction(a, b int) int {
return a + b
}

func noComment() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
Expand Down
7 changes: 7 additions & 0 deletions testdata/example_default.go → testdata/default/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package example
void myprint(char* s) {
printf("%d\n", s);
}
# PASS
*/
import "C"
import "unsafe"
Expand Down Expand Up @@ -76,6 +78,11 @@ func Mult(a, b int) int {
return a * b
}

//export CgoExportedFunction PASS
func CgoExportedFunction(a, b int) int {
return a + b
}

func noComment() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
Expand Down

0 comments on commit 8ff8ae1

Please sign in to comment.