-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrunner.go
56 lines (47 loc) · 1.14 KB
/
runner.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
48
49
50
51
52
53
54
55
56
package constantpropagation
import (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"github.com/cokeBeer/goot/pkg/dataflow/toolkits/graph"
"github.com/cokeBeer/goot/pkg/dataflow/toolkits/solver"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
// Runner represents a constant propagation runner
type Runner struct {
Src string
Function string
}
func NewRunner(src string, function string) *Runner {
runner := new(Runner)
runner.Src = src
runner.Function = function
return runner
}
// Run kick off the analysis
func (r *Runner) Run() {
// Generate ast
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", r.Src, parser.Mode(0))
if err != nil {
log.Println(err)
}
files := []*ast.File{f}
// Build package
pkg := types.NewPackage("constantpropagtionanalysis", "")
hello, _, err := ssautil.BuildPackage(
&types.Config{Importer: importer.Default()}, fset, pkg, files, ssa.SanityCheckFunctions)
if err != nil {
log.Println(err)
}
// Build graph
graph := graph.New(hello.Func(r.Function))
// Build analysis
analysis := New(graph)
// Solve analysis
solver.Solve(analysis, true)
}