Skip to content

Commit

Permalink
go/ast, go/printer: recognize export and extern line directives
Browse files Browse the repository at this point in the history
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 golang#51082.

Change-Id: Id0970331fa0b52ab5fa621631b5fa460767068bb
Reviewed-on: https://go-review.googlesource.com/c/go/+/399734
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Russ Cox <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
rsc authored and gopherbot committed Apr 11, 2022
1 parent 1930977 commit 0605bf6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/go/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions src/go/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/go/printer/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions src/go/printer/testdata/comments.golden
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,12 @@ func _() {
}
}

//extern foo
func foo() {}

//export bar
func bar() {}

// Print line directives correctly.

// The following is a legal line directive.
Expand Down
6 changes: 6 additions & 0 deletions src/go/printer/testdata/comments.input
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,12 @@ func _() {
}
}

//extern foo
func foo() {}

//export bar
func bar() {}

// Print line directives correctly.

// The following is a legal line directive.
Expand Down

0 comments on commit 0605bf6

Please sign in to comment.