Skip to content

Commit 7ca7e6c

Browse files
authored
isCgoImport accounts for back-quoted "C" import
1 parent 9d037d4 commit 7ca7e6c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

format/format.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,13 +833,21 @@ func identEqual(expr ast.Expr, name string) bool {
833833
//
834834
// import "C"
835835
//
836+
// or the equivalent:
837+
//
838+
// import `C`
839+
//
836840
// Note that parentheses do not affect the result.
837841
func isCgoImport(decl *ast.GenDecl) bool {
838842
if decl.Tok != token.IMPORT || len(decl.Specs) != 1 {
839843
return false
840844
}
841845
spec := decl.Specs[0].(*ast.ImportSpec)
842-
return spec.Path.Value == `"C"`
846+
v, err := strconv.Unquote(spec.Path.Value)
847+
if err != nil {
848+
panic(err) // should never error
849+
}
850+
return v == "C"
843851
}
844852

845853
// joinStdImports ensures that all standard library imports are together and at

testdata/scripts/cgo.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,36 @@ package p
1010
import "C"
1111
import "os"
1212

13+
import `C`
14+
import "os"
15+
1316
import "C"
1417
import (
1518
"io"
1619
"utf8"
1720
)
1821

22+
import `C`
23+
import (
24+
"io"
25+
"utf8"
26+
)
27+
1928
-- foo.go.golden --
2029
package p
2130

2231
import "C"
2332
import "os"
2433

34+
import "C"
35+
import "os"
36+
37+
import "C"
38+
import (
39+
"io"
40+
"utf8"
41+
)
42+
2543
import "C"
2644
import (
2745
"io"

0 commit comments

Comments
 (0)