dwarf: ignore DeclLine for function args (#3400)

This commit is contained in:
Andrei Matei 2023-05-31 12:58:47 -04:00 committed by GitHub
parent 9d6bce467c
commit 6a56d0eedc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 10 deletions

@ -11,6 +11,9 @@ func main() {
f1(a, b) f1(a, b)
} }
func f1(a, b int) { func f1(
a int,
b int,
) {
fmt.Printf("%d %d\n", a, b) fmt.Printf("%d %d\n", a, b)
} }

@ -54,14 +54,18 @@ func variablesInternal(v []Variable, root *godwarf.Tree, depth int, pc uint64, l
} }
return v return v
default: default:
o := 0 // Variables are considered to be visible starting on the line after the
if root.Tag != dwarf.TagFormalParameter && (flags&VariablesTrustDeclLine != 0) { // line they are declared on. Function arguments are an exception - the line
// visibility for variables starts the line after declaration line, // they are declared on does not matter; they are considered to be
// except for formal parameters, which are visible on the same line they // visible throughout the function.
// are defined. declLine, varHasDeclLine := root.Val(dwarf.AttrDeclLine).(int64)
o = 1 checkDeclLine :=
} root.Tag != dwarf.TagFormalParameter && // var is not a function argument
if declLine, ok := root.Val(dwarf.AttrDeclLine).(int64); (flags&VariablesNoDeclLineCheck != 0) || !ok || line >= int(declLine)+o { varHasDeclLine && // we know the DeclLine
(flags&VariablesNoDeclLineCheck == 0) // we were not explicitly instructed to ignore DeclLine
varVisible := !checkDeclLine || (line >= int(declLine)+1) // +1 because visibility starts on the line after DeclLine
if varVisible {
return append(v, Variable{root, depth}) return append(v, Variable{root, depth})
} }
return v return v

@ -3612,7 +3612,11 @@ func TestDeclLine(t *testing.T) {
setFileBreakpoint(p, t, fixture.Source, 9) setFileBreakpoint(p, t, fixture.Source, 9)
setFileBreakpoint(p, t, fixture.Source, 10) setFileBreakpoint(p, t, fixture.Source, 10)
setFileBreakpoint(p, t, fixture.Source, 11) setFileBreakpoint(p, t, fixture.Source, 11)
setFileBreakpoint(p, t, fixture.Source, 14) b := setFunctionBreakpoint(p, t, "main.f1")
if b.Line != 14 {
// Line 14 is hard-coded below.
t.Fatalf("expected \"main.f1\" breakpoint to be set on line 14, but got line: %d", b.Line)
}
assertNoError(grp.Continue(), t, "Continue 1") assertNoError(grp.Continue(), t, "Continue 1")
if goversion.VersionAfterOrEqual(runtime.Version(), 1, 15) { if goversion.VersionAfterOrEqual(runtime.Version(), 1, 15) {
@ -3635,6 +3639,9 @@ func TestDeclLine(t *testing.T) {
testDeclLineCount(t, p, 11, []string{"a", "b"}) testDeclLineCount(t, p, 11, []string{"a", "b"})
assertNoError(grp.Continue(), t, "Continue 5") assertNoError(grp.Continue(), t, "Continue 5")
// On line 14, we expect the function's arguments to be available, even
// though their DW_AT_decl_line declares higher line numbers. The decl_line
// is supposed to be ignored for the visibility of arguments.
testDeclLineCount(t, p, 14, []string{"a", "b"}) testDeclLineCount(t, p, 14, []string{"a", "b"})
}) })
} }