From 40fa7c0f6e52eb42c971ab1f66d20a2297ab309d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 10 Aug 2020 13:10:35 +0300 Subject: [PATCH] compiler: emit all used files in DebugInfo.Documents --- pkg/compiler/analysis.go | 11 +++++++++++ pkg/compiler/codegen.go | 7 +++++++ pkg/compiler/compiler.go | 7 ------- pkg/compiler/debug.go | 8 +++++--- pkg/compiler/debug_test.go | 2 ++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index 899d9e35f..aad9ea6a7 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -3,6 +3,7 @@ package compiler import ( "errors" "go/ast" + "go/token" "go/types" "strings" @@ -163,6 +164,16 @@ func (c *codegen) visitPkg(pkg *types.Package, seen map[string]bool) { c.packages = append(c.packages, pkgPath) } +func (c *codegen) fillDocumentInfo() { + fset := c.buildInfo.program.Fset + fset.Iterate(func(f *token.File) bool { + filePath := f.Position(f.Pos(0)).Filename + c.docIndex[filePath] = len(c.documents) + c.documents = append(c.documents, filePath) + return true + }) +} + func (c *codegen) analyzeFuncUsage() funcUsage { usage := funcUsage{} diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 1f6f3bff4..453d55160 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -77,6 +77,11 @@ type codegen struct { // packages contains packages in the order they were loaded. packages []string + // documents contains paths to all files used by the program. + documents []string + // docIndex maps file path to an index in documents array. + docIndex map[string]int + // Label table for recording jump destinations. l []int } @@ -1541,6 +1546,7 @@ func (c *codegen) newLambda(u uint16, lit *ast.FuncLit) { func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error { c.mainPkg = pkg c.analyzePkgOrder() + c.fillDocumentInfo() funUsage := c.analyzeFuncUsage() // Bring all imported functions into scope. @@ -1588,6 +1594,7 @@ func newCodegen(info *buildInfo, pkg *loader.PackageInfo) *codegen { labels: map[labelWithType]uint16{}, typeInfo: &pkg.Info, constMap: map[string]types.TypeAndValue{}, + docIndex: map[string]int{}, sequencePoints: make(map[string][]DebugSeqPoint), } diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index db3164053..740c990bd 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -10,7 +10,6 @@ import ( "io" "io/ioutil" "os" - "path/filepath" "strings" "github.com/nspcc-dev/neo-go/pkg/smartcontract" @@ -159,12 +158,6 @@ func CompileAndSave(src string, o *Options) ([]byte, error) { return b, nil } - p, err := filepath.Abs(src) - if err != nil { - return b, err - } - di.Documents = append(di.Documents, p) - if o.DebugInfo != "" { data, err := json.Marshal(di) if err != nil { diff --git a/pkg/compiler/debug.go b/pkg/compiler/debug.go index 2edfc3150..7b297298d 100644 --- a/pkg/compiler/debug.go +++ b/pkg/compiler/debug.go @@ -94,6 +94,7 @@ func (c *codegen) saveSequencePoint(n ast.Node) { end := fset.Position(n.End()) c.sequencePoints[c.scope.name] = append(c.sequencePoints[c.scope.name], DebugSeqPoint{ Opcode: c.prog.Len(), + Document: c.docIndex[start.Filename], StartLine: start.Line, StartCol: start.Offset, EndLine: end.Line, @@ -103,9 +104,10 @@ func (c *codegen) saveSequencePoint(n ast.Node) { func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo { d := &DebugInfo{ - MainPkg: c.mainPkg.Pkg.Name(), - Hash: hash.Hash160(contract), - Events: []EventDebugInfo{}, + MainPkg: c.mainPkg.Pkg.Name(), + Hash: hash.Hash160(contract), + Events: []EventDebugInfo{}, + Documents: c.documents, } if c.initEndOffset > 0 { d.Methods = append(d.Methods, MethodDebugInfo{ diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index 3134d4f17..fee427df5 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -249,6 +249,8 @@ func TestSequencePoints(t *testing.T) { d := c.emitDebugInfo(buf) require.NotNil(t, d) + require.Equal(t, d.Documents, []string{"foo.go"}) + // Main func has 2 return on 4-th and 6-th lines. ps := d.Methods[0].SeqPoints require.Equal(t, 2, len(ps))