Skip to content

Commit b3cb740

Browse files
committed
compiler: honor //line directives in DWARF variable file/line attrs
During DWARF debug generation, the DW_AT_decl_line / DW_AT_decl_file attributes for variable DIEs were being computed without taking into account the possibility of "//line" directives. Fix things up to use the correct src.Pos methods to pick up this info. Fixes #23704. Change-Id: I88c21a0e0a9602392be229252d856a6d665868e2 Reviewed-on: https://go-review.googlesource.com/92255 Run-TryBot: Than McIntosh <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 1ae22d8 commit b3cb740

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

src/cmd/compile/internal/gc/pgen.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ func createSimpleVars(automDecls []*Node) ([]*Node, []*dwarf.Var, map[*Node]bool
424424
Abbrev: abbrev,
425425
StackOffset: int32(offs),
426426
Type: Ctxt.Lookup(typename),
427-
DeclFile: declpos.Base().SymFilename(),
428-
DeclLine: declpos.Line(),
427+
DeclFile: declpos.RelFilename(),
428+
DeclLine: declpos.RelLine(),
429429
DeclCol: declpos.Col(),
430430
InlIndex: int32(inlIndex),
431431
ChildIndex: -1,
@@ -519,8 +519,8 @@ func createDwarfVars(fnsym *obj.LSym, fn *Func, automDecls []*Node) ([]*Node, []
519519
Abbrev: abbrev,
520520
StackOffset: int32(n.Xoffset),
521521
Type: Ctxt.Lookup(typename),
522-
DeclFile: declpos.Base().SymFilename(),
523-
DeclLine: declpos.Line(),
522+
DeclFile: declpos.RelFilename(),
523+
DeclLine: declpos.RelLine(),
524524
DeclCol: declpos.Col(),
525525
InlIndex: int32(inlIndex),
526526
ChildIndex: -1,
@@ -651,8 +651,8 @@ func createComplexVar(fn *Func, varID ssa.VarID) *dwarf.Var {
651651
// This won't work well if the first slot hasn't been assigned a stack
652652
// location, but it's not obvious how to do better.
653653
StackOffset: stackOffset(*debug.Slots[debug.VarSlots[varID][0]]),
654-
DeclFile: declpos.Base().SymFilename(),
655-
DeclLine: declpos.Line(),
654+
DeclFile: declpos.RelFilename(),
655+
DeclLine: declpos.RelLine(),
656656
DeclCol: declpos.Col(),
657657
InlIndex: int32(inlIndex),
658658
ChildIndex: -1,

src/cmd/link/internal/ld/dwarf_test.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -308,22 +308,11 @@ func main() {
308308
}
309309
}
310310

311-
func TestVarDeclCoordsAndSubrogramDeclFile(t *testing.T) {
312-
testenv.MustHaveGoBuild(t)
311+
func varDeclCoordsAndSubrogramDeclFile(t *testing.T, testpoint string, expectFile int, expectLine int, directive string) {
313312

314-
if runtime.GOOS == "plan9" {
315-
t.Skip("skipping on plan9; no DWARF symbol table in executables")
316-
}
317-
318-
const prog = `
319-
package main
313+
prog := fmt.Sprintf("package main\n\nfunc main() {\n%s\nvar i int\ni = i\n}\n", directive)
320314

321-
func main() {
322-
var i int
323-
i = i
324-
}
325-
`
326-
dir, err := ioutil.TempDir("", "TestVarDeclCoords")
315+
dir, err := ioutil.TempDir("", testpoint)
327316
if err != nil {
328317
t.Fatalf("could not create directory: %v", err)
329318
}
@@ -373,14 +362,35 @@ func main() {
373362

374363
// Verify line/file attributes.
375364
line := iEntry.Val(dwarf.AttrDeclLine)
376-
if line == nil || line.(int64) != 5 {
377-
t.Errorf("DW_AT_decl_line for i is %v, want 5", line)
365+
if line == nil || line.(int64) != int64(expectLine) {
366+
t.Errorf("DW_AT_decl_line for i is %v, want %d", line, expectLine)
378367
}
379368

380369
file := maindie.Val(dwarf.AttrDeclFile)
381370
if file == nil || file.(int64) != 1 {
382-
t.Errorf("DW_AT_decl_file for main is %v, want 1", file)
371+
t.Errorf("DW_AT_decl_file for main is %v, want %d", file, expectFile)
372+
}
373+
}
374+
375+
func TestVarDeclCoordsAndSubrogramDeclFile(t *testing.T) {
376+
testenv.MustHaveGoBuild(t)
377+
378+
if runtime.GOOS == "plan9" {
379+
t.Skip("skipping on plan9; no DWARF symbol table in executables")
380+
}
381+
382+
varDeclCoordsAndSubrogramDeclFile(t, "TestVarDeclCoords", 1, 5, "")
383+
}
384+
385+
func TestVarDeclCoordsWithLineDirective(t *testing.T) {
386+
testenv.MustHaveGoBuild(t)
387+
388+
if runtime.GOOS == "plan9" {
389+
t.Skip("skipping on plan9; no DWARF symbol table in executables")
383390
}
391+
392+
varDeclCoordsAndSubrogramDeclFile(t, "TestVarDeclCoordsWithLineDirective",
393+
2, 200, "//line /foobar.go:200")
384394
}
385395

386396
// Helper class for supporting queries on DIEs within a DWARF .debug_info

0 commit comments

Comments
 (0)