diff --git a/analysis.go b/analysis.go index 6042c61..e3b6a9b 100644 --- a/analysis.go +++ b/analysis.go @@ -14,7 +14,8 @@ import ( func Analysis(path string, analyzer *analysis.Analyzer) error { packages, err := packages.Load(&packages.Config{ - Mode: packages.LoadAllSyntax, // nolint:staticcheck + Mode: packages.LoadAllSyntax, // nolint:staticcheck + BuildFlags: strings.Split(buildFlag, " "), }, getAllPackageName(path)...) if err != nil { return err diff --git a/analysis_callgraph.go b/analysis_callgraph.go index 2990815..575b390 100644 --- a/analysis_callgraph.go +++ b/analysis_callgraph.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "golang.org/x/tools/go/callgraph" "golang.org/x/tools/go/callgraph/cha" @@ -15,8 +16,9 @@ import ( func doCallgraph(algo string, tests bool, args []string) (*callgraph.Graph, *ssa.Program, error) { cfg := &packages.Config{ - Mode: packages.LoadAllSyntax, // nolint:staticcheck - Tests: tests, + Mode: packages.LoadAllSyntax, // nolint:staticcheck + Tests: tests, + BuildFlags: strings.Split(buildFlag, " "), } initial, err := packages.Load(cfg, args...) diff --git a/main.go b/main.go index 1850a7b..900a8e0 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,11 @@ import ( ) var path string +var buildFlag string func main() { flag.StringVar(&path, "path", ".", "package path") + flag.StringVar(&buildFlag, "buildflag", "--tags=", "build flag") flag.Parse() cg, prog, err := doCallgraph("vta", false, getAllPackageName(path)) diff --git a/test/test1.go b/test/test1.go index f2c76de..cee170d 100644 --- a/test/test1.go +++ b/test/test1.go @@ -1,11 +1,15 @@ package test -// var m1 sync.Mutex // a -// var a int +import ( + "fmt" + "sync" +) -// func f1() { -// m1.Lock() -// // defer m1.Unlock() -// a++ -// fmt.Print(a) -// } +var m1 sync.Mutex // a1 +var a1 int + +func f1() { + m1.Lock() + // defer m1.Unlock() + fmt.Print(a1) +} diff --git a/test/test8.go b/test/test8.go index 4827db5..76454b1 100644 --- a/test/test8.go +++ b/test/test8.go @@ -6,14 +6,14 @@ import ( ) type b struct { - *sync.Mutex // a - a int + *sync.Mutex // a8 + a8 int } func (b1 *b) f1() { b1.Lock() // defer b1.Unlock() - fmt.Print(b1.a) + fmt.Print(b1.a8) } func init() { diff --git a/test/test9.go b/test/test9.go new file mode 100644 index 0000000..2a6f5e1 --- /dev/null +++ b/test/test9.go @@ -0,0 +1,6 @@ +//go:build p1 +// +build p1 + +package test + +func f() {}