From b72f6be9e992d50663218e389a0e34fbcca64eb7 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Wed, 12 May 2021 15:08:22 +0300 Subject: [PATCH] compiler: emit debug variable info for `init()` --- pkg/compiler/codegen.go | 6 ++++++ pkg/compiler/debug.go | 1 + pkg/compiler/debug_test.go | 12 +++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 41de79e05..fb6a23d34 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -46,6 +46,8 @@ type codegen struct { globals map[string]int // staticVariables contains global (static in NDX-DN11) variable names and types. staticVariables []string + // initVariables contains variables local to `_initialize` method. + initVariables []string // A mapping from label's names to their ids. labels map[labelWithType]uint16 @@ -460,6 +462,10 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl, pkg *types. emit.Opcodes(c.prog.BinWriter, opcode.RET) } + if isInit { + c.initVariables = append(c.initVariables, f.variables...) + } + f.rng.End = uint16(c.prog.Len() - 1) if !isLambda { diff --git a/pkg/compiler/debug.go b/pkg/compiler/debug.go index 25ec62207..fb3beae80 100644 --- a/pkg/compiler/debug.go +++ b/pkg/compiler/debug.go @@ -138,6 +138,7 @@ func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo { ReturnType: "Void", ReturnTypeSC: smartcontract.VoidType, SeqPoints: c.sequencePoints["init"], + Variables: c.initVariables, }) } if c.deployEndOffset >= 0 { diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index 8a876aaae..80cb885ed 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -18,6 +18,15 @@ func TestCodeGen_DebugInfo(t *testing.T) { import "github.com/nspcc-dev/neo-go/pkg/interop/storage" import "github.com/nspcc-dev/neo-go/pkg/interop/native/ledger" var staticVar int +func init() { + a := 1 + _ = a +} +func init() { + x := "" + _ = x + staticVar = 1 +} func Main(op string) bool { var s string _ = s @@ -90,7 +99,8 @@ func _deploy(data interface{}, isUpdate bool) {} t.Run("variables", func(t *testing.T) { vars := map[string][]string{ - "Main": {"s,ByteString", "res,Integer"}, + "Main": {"s,ByteString", "res,Integer"}, + manifest.MethodInit: {"a,Integer", "x,ByteString"}, } for i := range d.Methods { v, ok := vars[d.Methods[i].ID]