Skip to content

Commit

Permalink
isCgoImport accounts for back-quoted "C" import
Browse files Browse the repository at this point in the history
  • Loading branch information
nishanths authored Nov 14, 2021
1 parent 9d037d4 commit 7ca7e6c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,13 +833,21 @@ func identEqual(expr ast.Expr, name string) bool {
//
// import "C"
//
// or the equivalent:
//
// import `C`
//
// Note that parentheses do not affect the result.
func isCgoImport(decl *ast.GenDecl) bool {
if decl.Tok != token.IMPORT || len(decl.Specs) != 1 {
return false
}
spec := decl.Specs[0].(*ast.ImportSpec)
return spec.Path.Value == `"C"`
v, err := strconv.Unquote(spec.Path.Value)
if err != nil {
panic(err) // should never error
}
return v == "C"
}

// joinStdImports ensures that all standard library imports are together and at
Expand Down
18 changes: 18 additions & 0 deletions testdata/scripts/cgo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,36 @@ package p
import "C"
import "os"

import `C`
import "os"

import "C"
import (
"io"
"utf8"
)

import `C`
import (
"io"
"utf8"
)

-- foo.go.golden --
package p

import "C"
import "os"

import "C"
import "os"

import "C"
import (
"io"
"utf8"
)

import "C"
import (
"io"
Expand Down

0 comments on commit 7ca7e6c

Please sign in to comment.