diff options
| author | Keith Randall <keithr@alum.mit.edu> | 2019-01-25 10:21:40 -0800 |
|---|---|---|
| committer | Keith Randall <khr@golang.org> | 2019-01-27 04:02:46 +0000 |
| commit | eafe9a186c84dcfb7db1038cc43d1f0dfd1ea781 (patch) | |
| tree | 0b5437cda2e07de7066adf8fe328722dccac3a86 /src | |
| parent | d585f04fd39f86edaa2aafd776e9ad037d075396 (diff) | |
| download | go-eafe9a186c84dcfb7db1038cc43d1f0dfd1ea781.tar.xz | |
cmd/compile: hide init functions in tracebacks
Treat compiler-generated init functions as wrappers, so they will not
be shown in tracebacks.
The exception to this rule is that we'd like to show the line number
of initializers for global variables in tracebacks. In order to
preserve line numbers for those cases, separate out the code for those
initializers into a separate function (which is not marked as
autogenerated).
This CL makes the go binary 0.2% bigger.
Fixes #29919
Change-Id: I0f1fbfc03d10d764ce3a8ddb48fb387ca8453386
Reviewed-on: https://go-review.googlesource.com/c/159717
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/compile/internal/gc/init.go | 32 | ||||
| -rw-r--r-- | src/cmd/internal/objabi/funcid.go | 2 |
2 files changed, 31 insertions, 3 deletions
diff --git a/src/cmd/compile/internal/gc/init.go b/src/cmd/compile/internal/gc/init.go index ae87488075..e981f83653 100644 --- a/src/cmd/compile/internal/gc/init.go +++ b/src/cmd/compile/internal/gc/init.go @@ -57,6 +57,9 @@ func anyinit(n []*Node) bool { // fninit hand-crafts package initialization code. // +// func init.ializers() { (0) +// <init stmts> +// } // var initdone· uint8 (1) // func init() { (2) // if initdone· > 1 { (3) @@ -68,7 +71,7 @@ func anyinit(n []*Node) bool { // initdone· = 1 (5) // // over all matching imported symbols // <pkg>.init() (6) -// { <init stmts> } (7) +// init.ializers() (7) // init.<n>() // if any (8) // initdone· = 2 (9) // return (10) @@ -80,6 +83,27 @@ func fninit(n []*Node) { return } + // (0) + // Make a function that contains all the initialization statements. + // This is a separate function because we want it to appear in + // stack traces, where the init function itself does not. + var initializers *types.Sym + if len(nf) > 0 { + lineno = nf[0].Pos // prolog/epilog gets line number of first init stmt + initializers = lookup("init.ializers") + disableExport(initializers) + fn := dclfunc(initializers, nod(OTFUNC, nil, nil)) + fn.Nbody.Set(nf) + funcbody() + + fn = typecheck(fn, ctxStmt) + Curfn = fn + typecheckslice(nf, ctxStmt) + Curfn = nil + funccompile(fn) + lineno = autogeneratedPos + } + var r []*Node // (1) @@ -130,7 +154,11 @@ func fninit(n []*Node) { } // (7) - r = append(r, nf...) + if initializers != nil { + n := newname(initializers) + addvar(n, functype(nil, nil, nil), PFUNC) + r = append(r, nod(OCALL, n, nil)) + } // (8) diff --git a/src/cmd/internal/objabi/funcid.go b/src/cmd/internal/objabi/funcid.go index 1792df7cc1..a30bc3fa05 100644 --- a/src/cmd/internal/objabi/funcid.go +++ b/src/cmd/internal/objabi/funcid.go @@ -83,7 +83,7 @@ func GetFuncID(name, file string) FuncID { case "runtime.panicwrap": return FuncID_panicwrap } - if file == "<autogenerated>" && !strings.HasSuffix(name, ".init") { + if file == "<autogenerated>" { return FuncID_wrapper } if strings.HasPrefix(name, "runtime.call") { |
