debug: supports scope (if, for)
This commit is contained in:
@@ -118,26 +118,33 @@ func FuncWithAllTypeParams(
|
||||
) (int, error) {
|
||||
// Expected:
|
||||
// all variables: i8 i16 i32 i64 i u8 u16 u32 u64 u f32 f64 b c64 c128 slice arr arr2 s e f pf pi intr m c err fn
|
||||
// i8: '\x01'
|
||||
// i16: 2
|
||||
// i32: 3
|
||||
// i64: 4
|
||||
// i: 5
|
||||
// u8: '\x06'
|
||||
// u16: 7
|
||||
// u32: 8
|
||||
// u64: 9
|
||||
// u: 10
|
||||
// f32: 11
|
||||
// f64: 12
|
||||
// b: true
|
||||
// c64: complex64{real = 13, imag = 14}
|
||||
// c128: complex128{real = 15, imag = 16}
|
||||
// slice: []int{21, 22, 23}
|
||||
// arr: [3]int{24, 25, 26}
|
||||
// arr2: [3]github.com/goplus/llgo/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}}
|
||||
// s: "hello"
|
||||
// e: github.com/goplus/llgo/cl/_testdata/debug.E{i = 30}
|
||||
// i8: '\b'
|
||||
// i16: 2
|
||||
// u8: '\x06'
|
||||
// u16: 7
|
||||
// b: true
|
||||
// c64: complex64{real = 13, imag = 14}
|
||||
// c128: complex128{real = 15, imag = 16}
|
||||
// s: "hello"
|
||||
|
||||
// Expected(skip):
|
||||
// i8: '\b'
|
||||
// i16: 2
|
||||
// u8: '\x06'
|
||||
// u16: 7
|
||||
// b: true
|
||||
println(
|
||||
i8, i16, i32, i64, i, u8, u16, u32, u64, u,
|
||||
f32, f64, b,
|
||||
@@ -352,6 +359,63 @@ func FuncStructPtrParams(t *TinyStruct, s *SmallStruct, m *MidStruct, b *BigStru
|
||||
println("done")
|
||||
}
|
||||
|
||||
func ScopeIf(branch int) {
|
||||
a := 1
|
||||
// Expected:
|
||||
// all variables: a branch
|
||||
// a: 1
|
||||
if branch == 1 {
|
||||
b := 2
|
||||
c := 3
|
||||
// Expected:
|
||||
// all variables: a b c branch
|
||||
// a: 1
|
||||
// b: 2
|
||||
// c: 3
|
||||
// branch: 1
|
||||
println(a, b, c)
|
||||
} else {
|
||||
c := 3
|
||||
d := 4
|
||||
// Expected:
|
||||
// all variables: a c d branch
|
||||
// a: 1
|
||||
// c: 3
|
||||
// d: 4
|
||||
// branch: 0
|
||||
println(c, d)
|
||||
}
|
||||
// Expected:
|
||||
// all variables: a branch
|
||||
// a: 1
|
||||
println("a:", a)
|
||||
}
|
||||
|
||||
func ScopeFor() {
|
||||
a := 1
|
||||
for i := 0; i < 10; i++ {
|
||||
switch i {
|
||||
case 0:
|
||||
println("i is 0")
|
||||
// Expected:
|
||||
// all variables: i a
|
||||
// i: 0
|
||||
// a: 1
|
||||
println("i:", i)
|
||||
case 1:
|
||||
println("i is 1")
|
||||
// Expected:
|
||||
// all variables: i a
|
||||
// i: 1
|
||||
// a: 1
|
||||
println("i:", i)
|
||||
default:
|
||||
println("i is", i)
|
||||
}
|
||||
}
|
||||
println("a:", a)
|
||||
}
|
||||
|
||||
func main() {
|
||||
FuncStructParams(TinyStruct{I: 1}, SmallStruct{I: 2, J: 3}, MidStruct{I: 4, J: 5, K: 6}, BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
|
||||
FuncStructPtrParams(&TinyStruct{I: 1}, &SmallStruct{I: 2, J: 3}, &MidStruct{I: 4, J: 5, K: 6}, &BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
|
||||
@@ -392,7 +456,7 @@ func main() {
|
||||
pad2: 200,
|
||||
}
|
||||
// Expected:
|
||||
// all variables: globalInt globalStruct globalStructPtr s i err
|
||||
// all variables: s i err
|
||||
// s.i8: '\x01'
|
||||
// s.i16: 2
|
||||
// s.i32: 3
|
||||
@@ -419,6 +483,8 @@ func main() {
|
||||
globalStructPtr = &s
|
||||
globalStruct = s
|
||||
println("globalInt:", globalInt)
|
||||
// Expected(skip):
|
||||
// all variables: globalInt globalStruct globalStructPtr s i err
|
||||
println("s:", &s)
|
||||
FuncWithAllTypeStructParam(s)
|
||||
println("called function with struct")
|
||||
@@ -437,14 +503,18 @@ func main() {
|
||||
s.fn,
|
||||
)
|
||||
println(i, err)
|
||||
println("called function with types")
|
||||
ScopeIf(1)
|
||||
ScopeIf(0)
|
||||
ScopeFor()
|
||||
println(globalStructPtr)
|
||||
println(&globalStruct)
|
||||
s.i8 = 0x12
|
||||
println(s.i8)
|
||||
// Expected:
|
||||
// all variables: globalInt globalStruct globalStructPtr s i err
|
||||
// all variables: s i err
|
||||
// s.i8: '\x12'
|
||||
|
||||
// Expected(skip):
|
||||
// globalStruct.i8: '\x01'
|
||||
println((*globalStructPtr).i8)
|
||||
println("done")
|
||||
|
||||
@@ -238,6 +238,9 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
|
||||
sig = types.NewSignatureType(nil, nil, nil, params, results, false)
|
||||
}
|
||||
fn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), hasCtx, f.Origin() != nil)
|
||||
if debugSymbols {
|
||||
fn.Inline(llssa.NoInline)
|
||||
}
|
||||
}
|
||||
|
||||
if nblk := len(f.Blocks); nblk > 0 {
|
||||
@@ -292,18 +295,6 @@ func (p *context) compileFuncDecl(pkg llssa.Package, f *ssa.Function) (llssa.Fun
|
||||
return fn, nil, goFunc
|
||||
}
|
||||
|
||||
func (p *context) getDebugPosition(f *ssa.Function, pos token.Pos) token.Position {
|
||||
ppos := p.goProg.Fset.Position(pos)
|
||||
bodyPos := p.getFuncBodyPos(f)
|
||||
if bodyPos.Filename == "" {
|
||||
return ppos
|
||||
}
|
||||
if ppos.Line < bodyPos.Line {
|
||||
return bodyPos
|
||||
}
|
||||
return ppos
|
||||
}
|
||||
|
||||
func (p *context) getFuncBodyPos(f *ssa.Function) token.Position {
|
||||
if f.Object() != nil {
|
||||
return p.goProg.Fset.Position(f.Object().(*types.Func).Scope().Pos())
|
||||
@@ -311,6 +302,11 @@ func (p *context) getFuncBodyPos(f *ssa.Function) token.Position {
|
||||
return p.goProg.Fset.Position(f.Pos())
|
||||
}
|
||||
|
||||
func isGlobal(v *types.Var) bool {
|
||||
// TODO(lijie): better implementation
|
||||
return strings.HasPrefix(v.Parent().String(), "package ")
|
||||
}
|
||||
|
||||
func (p *context) debugRef(b llssa.Builder, v *ssa.DebugRef) {
|
||||
object := v.Object()
|
||||
variable, ok := object.(*types.Var)
|
||||
@@ -322,15 +318,21 @@ func (p *context) debugRef(b llssa.Builder, v *ssa.DebugRef) {
|
||||
// skip *ssa.FieldAddr
|
||||
return
|
||||
}
|
||||
if isGlobal(variable) {
|
||||
// avoid generate local variable debug info of global variable in function
|
||||
return
|
||||
}
|
||||
pos := p.goProg.Fset.Position(v.Pos())
|
||||
value := p.compileValue(b, v.X)
|
||||
fn := v.Parent()
|
||||
dbgVar := p.getLocalVariable(b, fn, variable)
|
||||
scope := variable.Parent()
|
||||
diScope := b.DIScope(p.fn, scope)
|
||||
if v.IsAddr {
|
||||
// *ssa.Alloc
|
||||
b.DIDeclare(variable, value, dbgVar, p.fn, pos, b.Func.Block(v.Block().Index))
|
||||
b.DIDeclare(variable, value, dbgVar, diScope, pos, b.Func.Block(v.Block().Index))
|
||||
} else {
|
||||
b.DIValue(variable, value, dbgVar, p.fn, pos, b.Func.Block(v.Block().Index))
|
||||
b.DIValue(variable, value, dbgVar, diScope, pos, b.Func.Block(v.Block().Index))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -527,10 +529,6 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
|
||||
}
|
||||
log.Panicln("unreachable:", iv)
|
||||
}
|
||||
if debugSymbols {
|
||||
pos := p.getDebugPosition(iv.Parent(), iv.Pos())
|
||||
b.DISetCurrentDebugLocation(p.fn, pos)
|
||||
}
|
||||
switch v := iv.(type) {
|
||||
case *ssa.Call:
|
||||
ret = p.call(b, llssa.Call, &v.Call)
|
||||
@@ -698,11 +696,27 @@ func (p *context) jumpTo(v *ssa.Jump) llssa.BasicBlock {
|
||||
return fn.Block(succs[0].Index)
|
||||
}
|
||||
|
||||
func (p *context) getDebugLocScope(v *ssa.Function, pos token.Pos) *types.Scope {
|
||||
if v.Object() == nil {
|
||||
return nil
|
||||
}
|
||||
funcScope := v.Object().(*types.Func).Scope()
|
||||
return funcScope.Innermost(pos)
|
||||
}
|
||||
|
||||
func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
|
||||
if iv, ok := instr.(instrOrValue); ok {
|
||||
p.compileInstrOrValue(b, iv, false)
|
||||
return
|
||||
}
|
||||
if debugSymbols {
|
||||
scope := p.getDebugLocScope(instr.Parent(), instr.Pos())
|
||||
if scope != nil {
|
||||
diScope := b.DIScope(p.fn, scope)
|
||||
pos := p.fset.Position(instr.Pos())
|
||||
b.DISetCurrentDebugLocation(diScope, pos)
|
||||
}
|
||||
}
|
||||
switch v := instr.(type) {
|
||||
case *ssa.Store:
|
||||
va := v.Addr
|
||||
@@ -779,7 +793,8 @@ func (p *context) getLocalVariable(b llssa.Builder, fn *ssa.Function, v *types.V
|
||||
return b.DIVarParam(p.fn, pos, v.Name(), t, argNo)
|
||||
}
|
||||
}
|
||||
return b.DIVarAuto(p.fn, pos, v.Name(), t)
|
||||
scope := b.DIScope(p.fn, v.Parent())
|
||||
return b.DIVarAuto(scope, pos, v.Name(), t)
|
||||
}
|
||||
|
||||
func (p *context) compileFunction(v *ssa.Function) (goFn llssa.Function, pyFn llssa.PyObjRef, kind int) {
|
||||
|
||||
@@ -267,6 +267,9 @@ func (p *context) funcOf(fn *ssa.Function) (aFn llssa.Function, pyFn llssa.PyObj
|
||||
}
|
||||
sig := fn.Signature
|
||||
aFn = pkg.NewFuncEx(name, sig, llssa.Background(ftype), false, fn.Origin() != nil)
|
||||
if debugSymbols {
|
||||
aFn.Inline(llssa.NoInline)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user