Skip to content

Commit

Permalink
cl: config add NoAutoGenMain for auto generate main func if no entry
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jan 25, 2022
1 parent 883ae4d commit 895ad6c
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type Config struct {

// RelativePath = true means to generate file line comments with relative file path.
RelativePath bool

// NoAutoGenMain = true means not to auto generate main func is no entry.
NoAutoGenMain bool
}

type nodeInterp struct {
Expand Down Expand Up @@ -396,6 +399,15 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
load()
}
err = ctx.complete()

if !conf.NoAutoGenMain && pkg.Name == "main" {
if obj := p.Types.Scope().Lookup("main"); obj == nil {
old := p.SetInTestingFile(false)
p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
p.SetInTestingFile(old)
}
}

return
}

Expand Down Expand Up @@ -542,6 +554,26 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
},
}}}
}
// check class project no MainEntry and auto added
if !conf.NoAutoGenMain && f.IsProj && !f.NoEntrypoint && f.Name.Name == "main" {
entry := getEntrypoint(f, false)
var hasEntry bool
for _, decl := range f.Decls {
switch d := decl.(type) {
case *ast.FuncDecl:
if d.Name.Name == entry {
hasEntry = true
}
}
}
if !hasEntry {
f.Decls = append(f.Decls, &ast.FuncDecl{
Name: ast.NewIdent(entry),
Type: &ast.FuncType{Params: &ast.FieldList{}},
Body: &ast.BlockStmt{},
})
}
}
for _, decl := range f.Decls {
switch d := decl.(type) {
case *ast.FuncDecl:
Expand Down

0 comments on commit 895ad6c

Please sign in to comment.