compiler: emit all used files in DebugInfo.Documents

This commit is contained in:
Evgenii Stratonikov 2020-08-10 13:10:35 +03:00
parent 057e1c6e3c
commit 40fa7c0f6e
5 changed files with 25 additions and 10 deletions

View file

@ -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{}

View file

@ -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),
}

View file

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

View file

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

View file

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