]> Cypherpunks repositories - gostls13.git/commitdiff
go/ast, go/printer: recognize export and extern line directives
authorRuss Cox <rsc@golang.org>
Mon, 11 Apr 2022 21:19:09 +0000 (17:19 -0400)
committerGopher Robot <gobot@golang.org>
Mon, 11 Apr 2022 22:03:44 +0000 (22:03 +0000)
Now that gofmt is reformatting these, we can't get away with
not knowing about directives such as //export and //extern (for gccgo).
Otherwise "//export foo" and "//extern foo" turn into "// export foo",
and "// extern foo", which are completely different meanings.

For #51082.

Change-Id: Id0970331fa0b52ab5fa621631b5fa460767068bb
Reviewed-on: https://go-review.googlesource.com/c/go/+/399734
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/go/ast/ast.go
src/go/ast/ast_test.go
src/go/printer/comment.go
src/go/printer/testdata/comments.golden
src/go/printer/testdata/comments.input

index 3ae5a60a1033bd4c918d9441171fcaba0c7a4dfd..1e089b9e70f03fd050f5049697e54388aac890a6 100644 (file)
@@ -161,8 +161,10 @@ func (g *CommentGroup) Text() string {
 // This code is also in go/printer.
 func isDirective(c string) bool {
        // "//line " is a line directive.
+       // "//extern " is for gccgo.
+       // "//export " is for cgo.
        // (The // has been removed.)
-       if strings.HasPrefix(c, "line ") {
+       if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") {
                return true
        }
 
index 71b2d6ca4b80eb52322f127f9e356ded92697eb1..66ae88486780e7c15bc16a8656ed2c01865fa239 100644 (file)
@@ -68,6 +68,9 @@ var isDirectiveTests = []struct {
        {"go:", false},
        {"go:*", false},
        {"go:x*", true},
+       {"export foo", true},
+       {"extern foo", true},
+       {"expert foo", false},
 }
 
 func TestIsDirective(t *testing.T) {
index 9749146739730fc53afe92fa7c007318faf3386d..76dd31efc73a8d59b60547400e4a36b1a02d1e04 100644 (file)
@@ -111,8 +111,10 @@ func formatDocComment(list []*ast.Comment) []*ast.Comment {
 // This code is also in go/ast.
 func isDirective(c string) bool {
        // "//line " is a line directive.
+       // "//extern " is for gccgo.
+       // "//export " is for cgo.
        // (The // has been removed.)
-       if strings.HasPrefix(c, "line ") {
+       if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") {
                return true
        }
 
index d03da3b65afd029825be36fc2adcff8e674b3229..62f37ea091069fbecb7c818de90df90eaf2ff665 100644 (file)
@@ -692,6 +692,12 @@ func _() {
        }
 }
 
+//extern foo
+func foo()     {}
+
+//export bar
+func bar()     {}
+
 // Print line directives correctly.
 
 // The following is a legal line directive.
index 2a15fa44a5dd93f57d04f38844301ec7425e8003..4bdafc3781de31793c0295b68ad95a9ea3dcda83 100644 (file)
@@ -691,6 +691,12 @@ func _() {
        }
 }
 
+//extern foo
+func foo() {}
+
+//export bar
+func bar() {}
+
 // Print line directives correctly.
 
 // The following is a legal line directive.