Skip to content

Commit 302ee43

Browse files
committed
cmd/godefs: support comment
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
1 parent 65491f5 commit 302ee43

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

cmd/godefs/ast.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (f *File) ParseGo(abspath string, src []byte) {
5454
// In cgo mode, we ignore ast2 and just apply edits directly
5555
// the text behind ast1. In godefs mode we modify and print ast2.
5656
ast1 := parse(abspath, src, parser.SkipObjectResolution|parser.ParseComments)
57-
ast2 := parse(abspath, src, parser.SkipObjectResolution)
57+
ast2 := parse(abspath, src, parser.SkipObjectResolution|parser.ParseComments)
5858

5959
f.Package = ast1.Name.Name
6060
f.Name = make(map[string]*Name)
@@ -115,6 +115,7 @@ func (f *File) ParseGo(abspath string, src []byte) {
115115
error_(ast1.Package, `cannot find import "C"`)
116116
}
117117

118+
comments := []*ast.CommentGroup{}
118119
// In ast2, strip the import "C" line.
119120
if *godefs {
120121
w := 0
@@ -141,6 +142,7 @@ func (f *File) ParseGo(abspath string, src []byte) {
141142
w++
142143
}
143144
ast2.Decls = ast2.Decls[0:w]
145+
comments = ast2.Comments[w:]
144146
} else {
145147
for _, decl := range ast2.Decls {
146148
d, ok := decl.(*ast.GenDecl)
@@ -157,6 +159,7 @@ func (f *File) ParseGo(abspath string, src []byte) {
157159
}
158160
}
159161
}
162+
ast2.Comments = comments
160163

161164
// Accumulate pointers to uses of C.x.
162165
if f.Ref == nil {

cmd/godefs/ast_go1.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build !go1.18
6+
57
package main
68

79
import (

cmd/godefs/ast_go118.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.18
6+
57
package main
68

79
import (

0 commit comments

Comments
 (0)