Repository: goplus/gop
Pull request #1768 (2024-02-22 02:31:22)

cl: fix ast.GenDecl pos for commentStmtEx

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/goplus/gop/pull/1768?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`4d49c6f`)](https://app.codecov.io/gh/goplus/gop/commit/4d49c6f78c2a8a2cafee1b0fb0785d153bf5e3ac?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) 92.19% compared to head [(`1455d87`)](https://app.codecov.io/gh/goplus/gop/pull/1768?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) 92.85%.
> Report is 3 commits behind head on main.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1768      +/-   ##
==========================================
+ Coverage   92.19%   92.85%   +0.65%     
==========================================
  Files          40       39       -1     
  Lines       10985     9038    -1947     
==========================================
- Hits        10128     8392    -1736     
+ Misses        694      483     -211     
  Partials      163      163              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/goplus/gop/pull/1768?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).


Patch:
diff --git a/cl/stmt.go b/cl/stmt.go
index 498f06a5..4714925c 100644
--- a/cl/stmt.go
+++ b/cl/stmt.go
@@ -53,6 +53,9 @@ func commentStmt(ctx *blockCtx, stmt ast.Stmt) {
 
 func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt) {
 	start := stmt.Pos()
+	if doc := checkStmtDoc(stmt); doc != nil {
+		start = doc.Pos()
+	}
 	pos := ctx.fset.Position(start)
 	if ctx.relBaseDir != "" {
 		pos.Filename = fileLineFile(ctx.relBaseDir, pos.Filename)
@@ -64,6 +67,15 @@ func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt) {
 	cb.SetComments(comments, false)
 }
 
+func checkStmtDoc(stmt ast.Stmt) *ast.CommentGroup {
+	if decl, ok := stmt.(*ast.DeclStmt); ok {
+		if d, ok := decl.Decl.(*ast.GenDecl); ok {
+			return d.Doc
+		}
+	}
+	return nil
+}
+
 func commentFunc(ctx *blockCtx, fn *gox.Func, decl *ast.FuncDecl) {
 	start := decl.Name.Pos()
 	if ctx.fileLine && start != token.NoPos {


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 60c4c5de..decca2b5 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -4269,3 +4269,72 @@ func main() {
 }
 `)
 }
+
+func TestCommentVar(t *testing.T) {
+	gopClTestEx(t, gblConfLine, "main", `
+// doc a line2
+var a int
+println a
+
+// doc b line6
+var b int
+println b
+
+var c int
+println c
+`, `package main
+
+import "fmt"
+// doc a line2
+var a int
+//line /foo/bar.gop:4
+func main() {
+//line /foo/bar.gop:4:1
+	fmt.Println(a)
+//line /foo/bar.gop:6:1
+	// doc b line6
+	var b int
+//line /foo/bar.gop:8:1
+	fmt.Println(b)
+//line /foo/bar.gop:10:1
+	var c int
+//line /foo/bar.gop:11:1
+	fmt.Println(c)
+}
+`)
+
+	gopClTestEx(t, gblConfLine, "main", `
+func demo() {
+	// doc a line3
+	var a int
+	println a
+	
+	// doc b line7
+	var b int
+	println b
+	
+	var c int
+	println c
+}
+`, `package main
+
+import "fmt"
+//line /foo/bar.gop:2:1
+func demo() {
+//line /foo/bar.gop:3:1
+	// doc a line3
+	var a int
+//line /foo/bar.gop:5:1
+	fmt.Println(a)
+//line /foo/bar.gop:7:1
+	// doc b line7
+	var b int
+//line /foo/bar.gop:9:1
+	fmt.Println(b)
+//line /foo/bar.gop:11:1
+	var c int
+//line /foo/bar.gop:12:1
+	fmt.Println(c)
+}
+`)
+}