diff --git a/oracle/oracle_test.go b/oracle/oracle_test.go index e7934c70..60a4a258 100644 --- a/oracle/oracle_test.go +++ b/oracle/oracle_test.go @@ -200,6 +200,7 @@ func TestOracle(t *testing.T) { for _, filename := range []string{ "testdata/src/main/calls.go", "testdata/src/main/callgraph.go", + "testdata/src/main/callgraph2.go", "testdata/src/main/describe.go", "testdata/src/main/freevars.go", "testdata/src/main/implements.go", diff --git a/oracle/testdata/src/main/callgraph2.go b/oracle/testdata/src/main/callgraph2.go new file mode 100644 index 00000000..a5f31d37 --- /dev/null +++ b/oracle/testdata/src/main/callgraph2.go @@ -0,0 +1,14 @@ +package main + +// Tests of call-graph queries. +// See go.tools/oracle/oracle_test.go for explanation. +// See callgraph2.golden for expected query results. + +import _ "reflect" + +func f() {} +func main() { + f() +} + +// @callgraph callgraph "^" diff --git a/oracle/testdata/src/main/callgraph2.golden b/oracle/testdata/src/main/callgraph2.golden new file mode 100644 index 00000000..c069956d --- /dev/null +++ b/oracle/testdata/src/main/callgraph2.golden @@ -0,0 +1,15 @@ +-------- @callgraph callgraph -------- + +Below is a call graph of the entire program. +The numbered nodes form a spanning tree. +Non-numbered nodes indicate back- or cross-edges to the node whose + number follows in parentheses. +Some nodes may appear multiple times due to context-sensitive + treatment of some calls. + +0 +1 main.init +2 reflect.init +3 main.main +4 main.f + diff --git a/pointer/analysis.go b/pointer/analysis.go index d645365f..c7d666ae 100644 --- a/pointer/analysis.go +++ b/pointer/analysis.go @@ -267,7 +267,7 @@ func Analyze(config *Config) *Result { fmt.Fprintln(a.log, "======== NEW ANALYSIS ========") } - a.generate() + root := a.generate() //a.optimize() @@ -304,7 +304,7 @@ func Analyze(config *Config) *Result { var callgraph *cgraph if a.config.BuildCallGraph { - callgraph = &cgraph{a.cgnodes} + callgraph = &cgraph{root, a.cgnodes} } return &Result{ diff --git a/pointer/callgraph.go b/pointer/callgraph.go index e3d8bcea..d209b98b 100644 --- a/pointer/callgraph.go +++ b/pointer/callgraph.go @@ -16,6 +16,7 @@ import ( // cgraph implements call.Graph. type cgraph struct { + root *cgnode nodes []*cgnode } @@ -28,7 +29,7 @@ func (g *cgraph) Nodes() []call.GraphNode { } func (g *cgraph) Root() call.GraphNode { - return g.nodes[0] + return g.root } // cgnode implements call.GraphNode.