]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/obj: cleanup linkgetlineFromPos
authorMichael Pratt <mpratt@google.com>
Fri, 28 Oct 2022 19:15:25 +0000 (15:15 -0400)
committerMichael Pratt <mpratt@google.com>
Mon, 31 Oct 2022 20:45:15 +0000 (20:45 +0000)
Make linkgetlineFromPos and getFileIndexAndLine methods on Link, and
give the former a more descriptive name.

The docs are expanded to make it more clear that these are final
file/line visible in programs.

In getFileSymbolAndLine use ctxt.InnermostPos instead of ctxt.PosTable
direct, which makes it more clear that we want the semantics of
InnermostPos.

Change-Id: I7c3d344dec60407fa54b191be8a09c117cb87dd0
Reviewed-on: https://go-review.googlesource.com/c/go/+/446301
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/internal/obj/dwarf.go
src/cmd/internal/obj/line.go
src/cmd/internal/obj/line_test.go
src/cmd/internal/obj/objfile.go
src/cmd/internal/obj/pcln.go
src/cmd/internal/obj/plist.go
src/cmd/internal/obj/sym.go

index 322938235385f9dd2a90ddc045699ebe8584415d..a9c13fdc8c5544d117464c01fcfad8947d9d04e0 100644 (file)
@@ -58,7 +58,7 @@ func (ctxt *Link) generateDebugLinesSymbol(s, lines *LSym) {
                        continue
                }
                newStmt := p.Pos.IsStmt() != src.PosNotStmt
-               newName, newLine := linkgetlineFromPos(ctxt, p.Pos)
+               newName, newLine := ctxt.getFileSymbolAndLine(p.Pos)
 
                // Output debug info.
                wrote := false
@@ -335,7 +335,7 @@ func (s *LSym) Length(dwarfContext interface{}) int64 {
 func (ctxt *Link) fileSymbol(fn *LSym) *LSym {
        p := fn.Func().Text
        if p != nil {
-               f, _ := linkgetlineFromPos(ctxt, p.Pos)
+               f, _ := ctxt.getFileSymbolAndLine(p.Pos)
                fsym := ctxt.Lookup(f)
                return fsym
        }
index 87cd32ba7ea774243a136579892f7a31a1827f95..20f03d985340fe63876932a4c1cc134a3fd16b4d 100644 (file)
@@ -14,17 +14,22 @@ func (ctxt *Link) AddImport(pkg string, fingerprint goobj.FingerprintType) {
        ctxt.Imports = append(ctxt.Imports, goobj.ImportedPkg{Pkg: pkg, Fingerprint: fingerprint})
 }
 
-func linkgetlineFromPos(ctxt *Link, xpos src.XPos) (f string, l int32) {
-       pos := ctxt.PosTable.Pos(xpos)
+// getFileSymbolAndLine returns the relative file symbol and relative line
+// number for a position (i.e., as adjusted by a //line directive). This is the
+// file/line visible in the final binary (pcfile, pcln, etc).
+func (ctxt *Link) getFileSymbolAndLine(xpos src.XPos) (f string, l int32) {
+       pos := ctxt.InnermostPos(xpos)
        if !pos.IsKnown() {
                pos = src.Pos{}
        }
-       // TODO(gri) Should this use relative or absolute line number?
        return pos.SymFilename(), int32(pos.RelLine())
 }
 
-// getFileIndexAndLine returns the file index (local to the CU), and the line number for a position.
-func getFileIndexAndLine(ctxt *Link, xpos src.XPos) (int, int32) {
-       f, l := linkgetlineFromPos(ctxt, xpos)
+// getFileIndexAndLine returns the relative file index (local to the CU), and
+// the relative line number for a position (i.e., as adjusted by a //line
+// directive). This is the file/line visible in the final binary (pcfile, pcln,
+// etc).
+func (ctxt *Link) getFileIndexAndLine(xpos src.XPos) (int, int32) {
+       f, l := ctxt.getFileSymbolAndLine(xpos)
        return ctxt.PosTable.FileIndex(f), l
 }
index e0db7f3420ff1af8aa33770835426b43b456835f..d3bb4e2639ecdb1bd90eaa418ce741a45504b94b 100644 (file)
@@ -10,7 +10,7 @@ import (
        "testing"
 )
 
-func TestLinkgetlineFromPos(t *testing.T) {
+func TestGetFileSymbolAndLine(t *testing.T) {
        ctxt := new(Link)
        ctxt.hash = make(map[string]*LSym)
        ctxt.statichash = make(map[string]*LSym)
@@ -31,10 +31,10 @@ func TestLinkgetlineFromPos(t *testing.T) {
        }
 
        for _, test := range tests {
-               f, l := linkgetlineFromPos(ctxt, ctxt.PosTable.XPos(test.pos))
+               f, l := ctxt.getFileSymbolAndLine(ctxt.PosTable.XPos(test.pos))
                got := fmt.Sprintf("%s:%d", f, l)
                if got != src.FileSymPrefix+test.want {
-                       t.Errorf("linkgetline(%v) = %q, want %q", test.pos, got, test.want)
+                       t.Errorf("ctxt.getFileSymbolAndLine(%v) = %q, want %q", test.pos, got, test.want)
                }
        }
 }
index 4c7a0c037906bc32df1223a2e63e0211299cef7e..d75708a3901a5f2ccbf27a13fc7c09cd958526a6 100644 (file)
@@ -721,7 +721,7 @@ func genFuncInfoSyms(ctxt *Link) {
                sort.Slice(o.File, func(i, j int) bool { return o.File[i] < o.File[j] })
                o.InlTree = make([]goobj.InlTreeNode, len(pc.InlTree.nodes))
                for i, inl := range pc.InlTree.nodes {
-                       f, l := getFileIndexAndLine(ctxt, inl.Pos)
+                       f, l := ctxt.getFileIndexAndLine(inl.Pos)
                        o.InlTree[i] = goobj.InlTreeNode{
                                Parent:   int32(inl.Parent),
                                File:     goobj.CUFileIndex(f),
index 30cf43f17295e3ed0b0d100042d4f8fb25e8b3e8..67a078091c54b640c003257bfa867d27eb093076 100644 (file)
@@ -142,7 +142,7 @@ func pctofileline(ctxt *Link, sym *LSym, oldval int32, p *Prog, phase int32, arg
        if p.As == ATEXT || p.As == ANOP || p.Pos.Line() == 0 || phase == 1 {
                return oldval
        }
-       f, l := getFileIndexAndLine(ctxt, p.Pos)
+       f, l := ctxt.getFileIndexAndLine(p.Pos)
        if arg == nil {
                return l
        }
index 30a6d929d52511a2868befd1d484c7c89803b7f5..751d231100a64d425557a461581bc00a64e7c8b1 100644 (file)
@@ -173,7 +173,10 @@ func (ctxt *Link) InitTextSym(s *LSym, flag int, start src.XPos) {
                ctxt.Diag("symbol %s listed multiple times", s.Name)
        }
 
-       _, startLine := linkgetlineFromPos(ctxt, start)
+       // startLine should be the same line number that would be displayed via
+       // pcln, etc for the declaration (i.e., relative line number, as
+       // adjusted by //line).
+       _, startLine := ctxt.getFileSymbolAndLine(start)
 
        // TODO(mdempsky): Remove once cmd/asm stops writing "" symbols.
        name := strings.Replace(s.Name, "\"\"", ctxt.Pkgpath, -1)
index b3eeedb59d00e1a85f5a988b6ba10ca75839e350..e5b052c5377c40fc0675b1c9cc419844c7df3005 100644 (file)
@@ -410,7 +410,7 @@ func (ctxt *Link) traverseFuncAux(flag traverseFlag, fsym *LSym, fn func(parent
                if call.Func != nil {
                        fn(fsym, call.Func)
                }
-               f, _ := linkgetlineFromPos(ctxt, call.Pos)
+               f, _ := ctxt.getFileSymbolAndLine(call.Pos)
                if filesym := ctxt.Lookup(f); filesym != nil {
                        fn(fsym, filesym)
                }