Skip to content

Commit

Permalink
Merge pull request #1510 from visualfc/line
Browse files Browse the repository at this point in the history
cl: commentFunc add line
  • Loading branch information
xushiwei committed Nov 3, 2023
2 parents a725ba0 + 63306e6 commit fb14bb9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
4 changes: 1 addition & 3 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,7 @@ func loadFunc(ctx *blockCtx, recv *types.Var, d *ast.FuncDecl, genBody bool) {
ctx.handleErr(err)
return
}
if d.Doc != nil {
fn.SetComments(pkg, d.Doc)
}
commentFunc(ctx, fn, d)
if rec := ctx.recorder(); rec != nil {
rec.Def(d.Name, fn.Func)
if recv == nil {
Expand Down
68 changes: 66 additions & 2 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ const (
)

var (
gblFset *token.FileSet
gblConf *cl.Config
gblFset *token.FileSet
gblConf *cl.Config
gblConfLine *cl.Config
)

func init() {
Expand All @@ -57,6 +58,18 @@ func init() {
NoFileLine: true,
NoAutoGenMain: true,
}
gblConfLine = &cl.Config{
Fset: gblFset,
Importer: imp,
Recorder: gopRecorder{},
LookupClass: lookupClass,
LookupPub: lookupPub,
C2goBase: "github.com/goplus/gop/cl/internal",
NoFileLine: false,
NoAutoGenMain: true,
RelativePath: true,
TargetDir: ".",
}
}

func gopClNamedTest(t *testing.T, name string, gopcode, expected string) {
Expand Down Expand Up @@ -4588,3 +4601,54 @@ func (this *Rect) Test() {
}
`, "Rect.gox")
}

func TestCommentLine(t *testing.T) {
gopClTestEx(t, gblConfLine, "main", `
type Point struct {
x int
y int
}
func (pt *Point) Test() {
println(pt.x, pt.y)
}
// testPoint is test point
func testPoint() {
var pt Point
pt.Test()
}
println "hello"
testPoint()
`, `package main
import "fmt"
type Point struct {
x int
y int
}
//line /foo/bar.gop:7:1
func (pt *Point) Test() {
//line /foo/bar.gop:8:1
fmt.Println(pt.x, pt.y)
}
// testPoint is test point
//
//line /foo/bar.gop:12:1
func testPoint() {
//line /foo/bar.gop:13:1
var pt Point
//line /foo/bar.gop:14:1
pt.Test()
}
//line /foo/bar.gop:17
func main() {
//line /foo/bar.gop:17:1
fmt.Println("hello")
//line /foo/bar.gop:18:1
testPoint()
}
`)
}
27 changes: 26 additions & 1 deletion cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,39 @@ func commentStmt(ctx *blockCtx, stmt ast.Stmt) {
if ctx.relativePath {
pos.Filename = relFile(ctx.targetDir, pos.Filename)
}
line := fmt.Sprintf("\n//line %s:%d", pos.Filename, pos.Line)
line := fmt.Sprintf("\n//line %s:%d:1", pos.Filename, pos.Line)
comments := &goast.CommentGroup{
List: []*goast.Comment{{Text: line}},
}
ctx.cb.SetComments(comments, false)
}
}

func commentFunc(ctx *blockCtx, fn *gox.Func, decl *ast.FuncDecl) {
if ctx.fileLine {
start := decl.Name.Pos()
pos := ctx.fset.Position(start)
if ctx.relativePath {
pos.Filename = relFile(ctx.targetDir, pos.Filename)
}
var line string
if decl.Shadow {
line = fmt.Sprintf("//line %s:%d", pos.Filename, pos.Line)
} else {
line = fmt.Sprintf("//line %s:%d:1", pos.Filename, pos.Line)
}
doc := &goast.CommentGroup{}
if decl.Doc != nil {
doc.List = append(doc.List, decl.Doc.List...)
doc.List = append(doc.List, &goast.Comment{Text: "//"})
}
doc.List = append(doc.List, &goast.Comment{Text: line})
fn.SetComments(ctx.pkg, doc)
} else if decl.Doc != nil {
fn.SetComments(ctx.pkg, decl.Doc)
}
}

func compileStmts(ctx *blockCtx, body []ast.Stmt) {
for _, stmt := range body {
if v, ok := stmt.(*ast.LabeledStmt); ok {
Expand Down

0 comments on commit fb14bb9

Please sign in to comment.