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
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

View file

@ -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.
@ -118,6 +120,7 @@ func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo {
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())
}

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/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{