From 7afca7f8e58476ed3d71639e3d653d32b07fc16f Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Wed, 12 May 2021 14:54:40 +0300 Subject: [PATCH] compiler: add support for `static-variables` in debug info --- pkg/compiler/codegen.go | 2 ++ pkg/compiler/debug.go | 13 ++++++++----- pkg/compiler/debug_test.go | 11 +++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 6bb8b2c62..41de79e05 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -44,6 +44,8 @@ type codegen struct { scope *funcScope globals map[string]int + // staticVariables contains global (static in NDX-DN11) variable names and types. + staticVariables []string // A mapping from label's names to their ids. labels map[labelWithType]uint16 diff --git a/pkg/compiler/debug.go b/pkg/compiler/debug.go index 042d47fd5..25ec62207 100644 --- a/pkg/compiler/debug.go +++ b/pkg/compiler/debug.go @@ -24,6 +24,8 @@ type DebugInfo struct { Events []EventDebugInfo `json:"events"` // EmittedEvents contains events occurring in code. EmittedEvents map[string][][]string `json:"-"` + // StaticVariables contains list of static variable names and types. + StaticVariables []string `json:"static-variables"` } // MethodDebugInfo represents smart-contract's method debug information. @@ -115,9 +117,10 @@ func (c *codegen) saveSequencePoint(n ast.Node) { func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo { d := &DebugInfo{ - MainPkg: c.mainPkg.Pkg.Name(), - Events: []EventDebugInfo{}, - Documents: c.documents, + MainPkg: c.mainPkg.Pkg.Name(), + Events: []EventDebugInfo{}, + Documents: c.documents, + StaticVariables: c.staticVariables, } if c.initEndOffset > 0 { d.Methods = append(d.Methods, MethodDebugInfo{ @@ -179,11 +182,11 @@ func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo { } func (c *codegen) registerDebugVariable(name string, expr ast.Expr) { + _, vt := c.scAndVMTypeFromExpr(expr) if c.scope == nil { - // do not save globals for now + c.staticVariables = append(c.staticVariables, name+","+vt.String()) return } - _, vt := c.scAndVMTypeFromExpr(expr) c.scope.variables = append(c.scope.variables, name+","+vt.String()) } diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index 1abbe0d07..8a876aaae 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -17,6 +17,7 @@ func TestCodeGen_DebugInfo(t *testing.T) { import "github.com/nspcc-dev/neo-go/pkg/interop" 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 Main(op string) bool { var s string _ = s @@ -79,6 +80,7 @@ func _deploy(data interface{}, isUpdate bool) {} "MethodOnPointerToStruct": "Void", "MethodParams": "Boolean", "_deploy": "Void", + manifest.MethodInit: "Void", } for i := range d.Methods { name := d.Methods[i].ID @@ -98,6 +100,10 @@ func _deploy(data interface{}, isUpdate bool) {} } }) + t.Run("static variables", func(t *testing.T) { + require.Equal(t, []string{"staticVar,Integer"}, d.StaticVariables) + }) + t.Run("param types", func(t *testing.T) { paramTypes := map[string][]DebugParam{ "_deploy": { @@ -162,6 +168,11 @@ func _deploy(data interface{}, isUpdate bool) {} Name: "MyCTR", ABI: manifest.ABI{ Methods: []manifest.Method{ + { + Name: manifest.MethodInit, + Parameters: []manifest.Parameter{}, + ReturnType: smartcontract.VoidType, + }, { Name: "_deploy", Parameters: []manifest.Parameter{