Skip to content

Commit

Permalink
支持 go build flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fananchong committed Dec 18, 2023
1 parent 65c5ce5 commit 7f5da99
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions analysis_callgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"

"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/cha"
Expand All @@ -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...)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
20 changes: 12 additions & 8 deletions test/test1.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 3 additions & 3 deletions test/test8.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions test/test9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build p1
// +build p1

package test

func f() {}

0 comments on commit 7f5da99

Please sign in to comment.