Skip to content

Commit

Permalink
Merge pull request #1742 from xushiwei/t
Browse files Browse the repository at this point in the history
gmxMainFunc: force line of main code = (last comment line + 1)
  • Loading branch information
xushiwei authored Feb 14, 2024
2 parents e3e161a + fd47c73 commit 89e9bf8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
5 changes: 5 additions & 0 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ func gmxMainFunc(pkg *gox.Package, ctx *pkgCtx, noAutoGenMain bool) func() {
new := pkg.Builtin().Ref("new")
cb := fn.BodyStart(pkg).Val(new).Val(o).Call(1).MemberVal("Main")

// force line of main code = (last comment line + 1)
if ctx.lastStmt != nil {
commentStmtEx(cb, ctx, ctx.lastStmt, true)
}

sig := cb.Get(-1).Type.(*types.Signature)
narg := gmxMainNarg(sig)
if narg > 0 {
Expand Down
9 changes: 6 additions & 3 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ type pkgCtx struct {
generics map[string]bool // generic type record
idents []*ast.Ident // toType ident recored
inInst int // toType in generic instance

lastStmt ast.Stmt // set by commentStmt for line:xxx
}

type pkgImp struct {
Expand All @@ -368,9 +370,10 @@ type blockCtx struct {
classRecv *ast.FieldList // available when gmxSettings != nil
fileScope *types.Scope // only valid when isGopFile
rec *typesRecorder
fileLine bool
isClass bool
isGopFile bool // is Go+ file or not

fileLine bool
isClass bool
isGopFile bool // is Go+ file or not
}

func (bc *blockCtx) recorder() *typesRecorder {
Expand Down
32 changes: 22 additions & 10 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,29 @@ func relFile(dir string, absFile string) string {

func commentStmt(ctx *blockCtx, stmt ast.Stmt) {
if ctx.fileLine {
start := stmt.Pos()
pos := ctx.fset.Position(start)
if ctx.relBaseDir != "" {
pos.Filename = fileLineFile(ctx.relBaseDir, pos.Filename)
}
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)
ctx.lastStmt = stmt // for gmxMainFunc
commentStmtEx(ctx.cb, ctx.pkgCtx, stmt, false)
}
}

func commentStmtEx(cb *gox.CodeBuilder, ctx *pkgCtx, stmt ast.Stmt, next bool) {
var start token.Pos
var delta int
if next {
start = stmt.End() - 1
delta = 1
} else {
start = stmt.Pos()
}
pos := ctx.fset.Position(start)
if ctx.relBaseDir != "" {
pos.Filename = fileLineFile(ctx.relBaseDir, pos.Filename)
}
line := fmt.Sprintf("\n//line %s:%d:1", pos.Filename, pos.Line+delta)
comments := &goast.CommentGroup{
List: []*goast.Comment{{Text: line}},
}
cb.SetComments(comments, false)
}

func commentFunc(ctx *blockCtx, fn *gox.Func, decl *ast.FuncDecl) {
Expand Down

0 comments on commit 89e9bf8

Please sign in to comment.