From a5b759aab8b9e866c0bf2dd91ffde68b6bc6766b Mon Sep 17 00:00:00 2001 From: David Chase Date: Mon, 27 Nov 2017 18:06:17 -0500 Subject: [PATCH] cmd/compile: adjust lineno during import to get Pos right Binary import sometimes constructs nodes using functions that use the global lineno for the Position. This causes spurious numbers to appear in the assembly and the debugging output. Fix (targeted, because late in the cycle): save and restore lineno around bimport calls known to use lineno-sensitive functions. Updates #22600. (Comment: "This is a weird line to step through") Change-Id: I9c4094670380609fe4b6696443fb02579521c596 Reviewed-on: https://go-review.googlesource.com/80115 Run-TryBot: David Chase TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/bimport.go | 34 +++++++++++++------ .../internal/ssa/testdata/hist.opt-dlv.nexts | 2 -- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/cmd/compile/internal/gc/bimport.go b/src/cmd/compile/internal/gc/bimport.go index 2b95ac5375..71d20ec37b 100644 --- a/src/cmd/compile/internal/gc/bimport.go +++ b/src/cmd/compile/internal/gc/bimport.go @@ -202,6 +202,13 @@ func Import(imp *types.Pkg, in *bufio.Reader) { } f.Func.Inl.Set(body) f.Func.InlCost = int32(inlCost) + if Debug['E'] > 0 && Debug['m'] > 2 && f.Func.Inl.Len() != 0 { + if Debug['m'] > 3 { + fmt.Printf("inl body for %v: %+v\n", f, f.Func.Inl) + } else { + fmt.Printf("inl body for %v: %v\n", f, f.Func.Inl) + } + } funcbody() } else { // function already imported - read body but discard declarations @@ -369,7 +376,11 @@ func (p *importer) obj(tag int) { n := newfuncnamel(pos, sym) n.Type = sig + // TODO(mdempsky): Stop clobbering n.Pos in declare. + savedlineno := lineno + lineno = pos declare(n, PFUNC) + lineno = savedlineno p.funcList = append(p.funcList, n) importlist = append(importlist, n) @@ -377,9 +388,6 @@ func (p *importer) obj(tag int) { if Debug['E'] > 0 { fmt.Printf("import [%q] func %v \n", p.imp.Path, n) - if Debug['m'] > 2 && n.Func.Inl.Len() != 0 { - fmt.Printf("inl body: %v\n", n.Func.Inl) - } } default: @@ -493,7 +501,11 @@ func (p *importer) typ() *types.Type { // read underlying type t0 := p.typ() + // TODO(mdempsky): Stop clobbering n.Pos in declare. + savedlineno := lineno + lineno = pos p.importtype(t, t0) + lineno = savedlineno // interfaces don't have associated methods if t0.IsInterface() { @@ -547,9 +559,6 @@ func (p *importer) typ() *types.Type { if Debug['E'] > 0 { fmt.Printf("import [%q] meth %v \n", p.imp.Path, n) - if Debug['m'] > 2 && n.Func.Inl.Len() != 0 { - fmt.Printf("inl body: %v\n", n.Func.Inl) - } } } @@ -963,21 +972,26 @@ func (p *importer) node() *Node { // unimplemented case OPTRLIT: - n := npos(p.pos(), p.expr()) + pos := p.pos() + n := npos(pos, p.expr()) if !p.bool() /* !implicit, i.e. '&' operator */ { if n.Op == OCOMPLIT { // Special case for &T{...}: turn into (*T){...}. - n.Right = nod(OIND, n.Right, nil) + n.Right = nodl(pos, OIND, n.Right, nil) n.Right.SetImplicit(true) } else { - n = nod(OADDR, n, nil) + n = nodl(pos, OADDR, n, nil) } } return n case OSTRUCTLIT: - n := nodl(p.pos(), OCOMPLIT, nil, typenod(p.typ())) + // TODO(mdempsky): Export position information for OSTRUCTKEY nodes. + savedlineno := lineno + lineno = p.pos() + n := nodl(lineno, OCOMPLIT, nil, typenod(p.typ())) n.List.Set(p.elemList()) // special handling of field names + lineno = savedlineno return n // case OARRAYLIT, OSLICELIT, OMAPLIT: diff --git a/src/cmd/compile/internal/ssa/testdata/hist.opt-dlv.nexts b/src/cmd/compile/internal/ssa/testdata/hist.opt-dlv.nexts index a6321d904b..7aab219552 100644 --- a/src/cmd/compile/internal/ssa/testdata/hist.opt-dlv.nexts +++ b/src/cmd/compile/internal/ssa/testdata/hist.opt-dlv.nexts @@ -8,9 +8,7 @@ 61: sink = dx + dy //gdb-opt=(dx,dy) 63: hist := make([]int, 7) //gdb-opt=(sink,dx/O,dy/O) 64: var reader io.Reader = strings.NewReader(cannedInput) //gdb-dbg=(hist/A,cannedInput/A) -19: "strings" 65: if len(os.Args) > 1 { -14: "bufio" 74: for scanner.Scan() { //gdb-opt=(scanner/A) 76: i, err := strconv.ParseInt(s, 10, 64) 77: if err != nil { //gdb-dbg=(i) //gdb-opt=(err,hist,i) -- 2.48.1