summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2022-04-11 17:19:09 -0400
committerGopher Robot <gobot@golang.org>2022-04-11 22:03:44 +0000
commit0605bf6052807e71e52fc3864b18b221ce61b047 (patch)
treed1929a1f972a68181ef66d881442639bc71738d7
parent19309779ac5e2f5a2fd3cbb34421dafb2855ac21 (diff)
downloadgo-git-0605bf6052807e71e52fc3864b18b221ce61b047.tar.gz
go/ast, go/printer: recognize export and extern line directives
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>
-rw-r--r--src/go/ast/ast.go4
-rw-r--r--src/go/ast/ast_test.go3
-rw-r--r--src/go/printer/comment.go4
-rw-r--r--src/go/printer/testdata/comments.golden6
-rw-r--r--src/go/printer/testdata/comments.input6
5 files changed, 21 insertions, 2 deletions
diff --git a/src/go/ast/ast.go b/src/go/ast/ast.go
index 3ae5a60a10..1e089b9e70 100644
--- a/src/go/ast/ast.go
+++ b/src/go/ast/ast.go
@@ -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
}
diff --git a/src/go/ast/ast_test.go b/src/go/ast/ast_test.go
index 71b2d6ca4b..66ae884867 100644
--- a/src/go/ast/ast_test.go
+++ b/src/go/ast/ast_test.go
@@ -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) {
diff --git a/src/go/printer/comment.go b/src/go/printer/comment.go
index 9749146739..76dd31efc7 100644
--- a/src/go/printer/comment.go
+++ b/src/go/printer/comment.go
@@ -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
}
diff --git a/src/go/printer/testdata/comments.golden b/src/go/printer/testdata/comments.golden
index d03da3b65a..62f37ea091 100644
--- a/src/go/printer/testdata/comments.golden
+++ b/src/go/printer/testdata/comments.golden
@@ -692,6 +692,12 @@ func _() {
}
}
+//extern foo
+func foo() {}
+
+//export bar
+func bar() {}
+
// Print line directives correctly.
// The following is a legal line directive.
diff --git a/src/go/printer/testdata/comments.input b/src/go/printer/testdata/comments.input
index 2a15fa44a5..4bdafc3781 100644
--- a/src/go/printer/testdata/comments.input
+++ b/src/go/printer/testdata/comments.input
@@ -691,6 +691,12 @@ func _() {
}
}
+//extern foo
+func foo() {}
+
+//export bar
+func bar() {}
+
// Print line directives correctly.
// The following is a legal line directive.