Skip to content

Commit d54d6d3

Browse files
committed
cl: fix ast.GenDecl pos for commentStmtEx
1 parent 1f534da commit d54d6d3

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

cl/compile_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -4269,3 +4269,72 @@ func main() {
42694269
}
42704270
`)
42714271
}
4272+
4273+
func TestCommentVar(t *testing.T) {
4274+
gopClTestEx(t, gblConfLine, "main", `
4275+
// doc a line2
4276+
var a int
4277+
println a
4278+
4279+
// doc b line6
4280+
var b int
4281+
println b
4282+
4283+
var c int
4284+
println c
4285+
`, `package main
4286+
4287+
import "fmt"
4288+
// doc a line2
4289+
var a int
4290+
//line /foo/bar.gop:4
4291+
func main() {
4292+
//line /foo/bar.gop:4:1
4293+
fmt.Println(a)
4294+
//line /foo/bar.gop:6:1
4295+
// doc b line6
4296+
var b int
4297+
//line /foo/bar.gop:8:1
4298+
fmt.Println(b)
4299+
//line /foo/bar.gop:10:1
4300+
var c int
4301+
//line /foo/bar.gop:11:1
4302+
fmt.Println(c)
4303+
}
4304+
`)
4305+
4306+
gopClTestEx(t, gblConfLine, "main", `
4307+
func demo() {
4308+
// doc a line3
4309+
var a int
4310+
println a
4311+
4312+
// doc b line7
4313+
var b int
4314+
println b
4315+
4316+
var c int
4317+
println c
4318+
}
4319+
`, `package main
4320+
4321+
import "fmt"
4322+
//line /foo/bar.gop:2:1
4323+
func demo() {
4324+
//line /foo/bar.gop:3:1
4325+
// doc a line3
4326+
var a int
4327+
//line /foo/bar.gop:5:1
4328+
fmt.Println(a)
4329+
//line /foo/bar.gop:7:1
4330+
// doc b line7
4331+
var b int
4332+
//line /foo/bar.gop:9:1
4333+
fmt.Println(b)
4334+
//line /foo/bar.gop:11:1
4335+
var c int
4336+
//line /foo/bar.gop:12:1
4337+
fmt.Println(c)
4338+
}
4339+
`)
4340+
}

cl/stmt.go

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func commentStmt(ctx *blockCtx, stmt ast.Stmt) {
5353

5454
func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt) {
5555
start := stmt.Pos()
56+
if doc := checkStmtDoc(stmt); doc != nil {
57+
start = doc.Pos()
58+
}
5659
pos := ctx.fset.Position(start)
5760
if ctx.relBaseDir != "" {
5861
pos.Filename = fileLineFile(ctx.relBaseDir, pos.Filename)
@@ -64,6 +67,15 @@ func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt) {
6467
cb.SetComments(comments, false)
6568
}
6669

70+
func checkStmtDoc(stmt ast.Stmt) *ast.CommentGroup {
71+
if decl, ok := stmt.(*ast.DeclStmt); ok {
72+
if d, ok := decl.Decl.(*ast.GenDecl); ok {
73+
return d.Doc
74+
}
75+
}
76+
return nil
77+
}
78+
6779
func commentFunc(ctx *blockCtx, fn *gox.Func, decl *ast.FuncDecl) {
6880
start := decl.Name.Pos()
6981
if ctx.fileLine && start != token.NoPos {

0 commit comments

Comments
 (0)