compiler: add support for static-variables in debug info

This commit is contained in:
Evgeniy Stratonikov 2021-05-12 14:54:40 +03:00
parent e8ba386e58
commit 7afca7f8e5
3 changed files with 21 additions and 5 deletions

View file

@ -44,6 +44,8 @@ type codegen struct {
scope *funcScope scope *funcScope
globals map[string]int 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. // A mapping from label's names to their ids.
labels map[labelWithType]uint16 labels map[labelWithType]uint16

View file

@ -24,6 +24,8 @@ type DebugInfo struct {
Events []EventDebugInfo `json:"events"` Events []EventDebugInfo `json:"events"`
// EmittedEvents contains events occurring in code. // EmittedEvents contains events occurring in code.
EmittedEvents map[string][][]string `json:"-"` 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. // 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 { func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo {
d := &DebugInfo{ d := &DebugInfo{
MainPkg: c.mainPkg.Pkg.Name(), MainPkg: c.mainPkg.Pkg.Name(),
Events: []EventDebugInfo{}, Events: []EventDebugInfo{},
Documents: c.documents, Documents: c.documents,
StaticVariables: c.staticVariables,
} }
if c.initEndOffset > 0 { if c.initEndOffset > 0 {
d.Methods = append(d.Methods, MethodDebugInfo{ 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) { func (c *codegen) registerDebugVariable(name string, expr ast.Expr) {
_, vt := c.scAndVMTypeFromExpr(expr)
if c.scope == nil { if c.scope == nil {
// do not save globals for now c.staticVariables = append(c.staticVariables, name+","+vt.String())
return return
} }
_, vt := c.scAndVMTypeFromExpr(expr)
c.scope.variables = append(c.scope.variables, name+","+vt.String()) c.scope.variables = append(c.scope.variables, name+","+vt.String())
} }

View file

@ -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"
import "github.com/nspcc-dev/neo-go/pkg/interop/storage" import "github.com/nspcc-dev/neo-go/pkg/interop/storage"
import "github.com/nspcc-dev/neo-go/pkg/interop/native/ledger" import "github.com/nspcc-dev/neo-go/pkg/interop/native/ledger"
var staticVar int
func Main(op string) bool { func Main(op string) bool {
var s string var s string
_ = s _ = s
@ -79,6 +80,7 @@ func _deploy(data interface{}, isUpdate bool) {}
"MethodOnPointerToStruct": "Void", "MethodOnPointerToStruct": "Void",
"MethodParams": "Boolean", "MethodParams": "Boolean",
"_deploy": "Void", "_deploy": "Void",
manifest.MethodInit: "Void",
} }
for i := range d.Methods { for i := range d.Methods {
name := d.Methods[i].ID 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) { t.Run("param types", func(t *testing.T) {
paramTypes := map[string][]DebugParam{ paramTypes := map[string][]DebugParam{
"_deploy": { "_deploy": {
@ -162,6 +168,11 @@ func _deploy(data interface{}, isUpdate bool) {}
Name: "MyCTR", Name: "MyCTR",
ABI: manifest.ABI{ ABI: manifest.ABI{
Methods: []manifest.Method{ Methods: []manifest.Method{
{
Name: manifest.MethodInit,
Parameters: []manifest.Parameter{},
ReturnType: smartcontract.VoidType,
},
{ {
Name: "_deploy", Name: "_deploy",
Parameters: []manifest.Parameter{ Parameters: []manifest.Parameter{