forked from system-pclub/GFuzz
/
instctx.go
47 lines (42 loc) · 1.04 KB
/
instctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package inst
import (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"log"
)
// NewInstContext creates a InstContext by given Golang source file
func NewInstContext(goSrcFile string) (*InstContext, error) {
oldSource, err := ioutil.ReadFile(goSrcFile)
if err != nil {
return nil, err
}
fs := token.NewFileSet()
astF, err := parser.ParseFile(fs, goSrcFile, oldSource, parser.ParseComments)
if err != nil {
return nil, err
}
conf := types.Config{
Importer: importer.ForCompiler(fs, "source", nil),
Error: func(err error) {
log.Printf("'%s' type checker: %s", goSrcFile, err)
},
}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
conf.Check(astF.Name.Name, fs, []*ast.File{astF}, info)
return &InstContext{
File: goSrcFile,
OriginalContent: oldSource,
FS: fs,
Type: info,
AstFile: astF,
Metadata: make(map[string]interface{}),
}, nil
}