Skip to content

Commit

Permalink
cmd/{guru,callgraph}: stop using go/pointer
Browse files Browse the repository at this point in the history
This change removes the -algo=pta option from cmd/callgraph,
and all the subcommands of cmd/guru, that use pointer analysis.
These features have been poorly supported for a long time,
and the pointer analysis package is about to be tagged and
deleted.

Updates golang/go#59676

Change-Id: Id4ded651b8385c588991d01377b2f087d14ae191
Reviewed-on: https://go-review.googlesource.com/c/tools/+/499696
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
adonovan committed May 31, 2023
1 parent cd694d8 commit 96844c3
Show file tree
Hide file tree
Showing 37 changed files with 24 additions and 2,553 deletions.
51 changes: 6 additions & 45 deletions cmd/callgraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ package main // import "golang.org/x/tools/cmd/callgraph"
// callee file/line/col

import (
"bufio"
"bytes"
"flag"
"fmt"
"go/build"
"go/token"
"io"
"log"
"os"
"runtime"
"text/template"
Expand All @@ -39,25 +37,21 @@ import (
"golang.org/x/tools/go/callgraph/static"
"golang.org/x/tools/go/callgraph/vta"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/pointer"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)

// flags
var (
algoFlag = flag.String("algo", "rta",
`Call graph construction algorithm (static, cha, rta, vta, pta)`)
`Call graph construction algorithm (static, cha, rta, vta)`)

testFlag = flag.Bool("test", false,
"Loads test code (*_test.go) for imported packages")

formatFlag = flag.String("format",
"{{.Caller}}\t--{{.Dynamic}}-{{.Line}}:{{.Column}}-->\t{{.Callee}}",
"A template expression specifying how to format an edge")

ptalogFlag = flag.String("ptalog", "",
"Location of the points-to analysis log file, or empty to disable logging.")
)

func init() {
Expand All @@ -68,7 +62,7 @@ const Usage = `callgraph: display the call graph of a Go program.
Usage:
callgraph [-algo=static|cha|rta|vta|pta] [-test] [-format=...] package...
callgraph [-algo=static|cha|rta|vta] [-test] [-format=...] package...
Flags:
Expand All @@ -78,11 +72,10 @@ Flags:
cha Class Hierarchy Analysis
rta Rapid Type Analysis
vta Variable Type Analysis
pta inclusion-based Points-To Analysis
The algorithms are ordered by increasing precision in their
treatment of dynamic calls (and thus also computational cost).
RTA and PTA require a whole program (main or test), and
RTA requires a whole program (main or test), and
include only functions reachable from main.
-test Include the package's tests in the analysis.
Expand Down Expand Up @@ -132,9 +125,9 @@ Examples:
$GOROOT/src/net/http/triv.go | sort | uniq
Show functions that make dynamic calls into the 'fmt' test package,
using the pointer analysis algorithm:
using the Rapid Type Analysis algorithm:
callgraph -format='{{.Caller}} -{{.Dynamic}}-> {{.Callee}}' -test -algo=pta fmt |
callgraph -format='{{.Caller}} -{{.Dynamic}}-> {{.Callee}}' -test -algo=rta fmt |
sed -ne 's/-dynamic-/--/p' |
sed -ne 's/-->.*fmt_test.*$//p' | sort | uniq
Expand Down Expand Up @@ -205,39 +198,7 @@ func doCallgraph(dir, gopath, algo, format string, tests bool, args []string) er
cg = cha.CallGraph(prog)

case "pta":
// Set up points-to analysis log file.
var ptalog io.Writer
if *ptalogFlag != "" {
if f, err := os.Create(*ptalogFlag); err != nil {
log.Fatalf("Failed to create PTA log file: %s", err)
} else {
buf := bufio.NewWriter(f)
ptalog = buf
defer func() {
if err := buf.Flush(); err != nil {
log.Printf("flush: %s", err)
}
if err := f.Close(); err != nil {
log.Printf("close: %s", err)
}
}()
}
}

mains, err := mainPackages(pkgs)
if err != nil {
return err
}
config := &pointer.Config{
Mains: mains,
BuildCallGraph: true,
Log: ptalog,
}
ptares, err := pointer.Analyze(config)
if err != nil {
return err // internal error in pointer analysis
}
cg = ptares.CallGraph
return fmt.Errorf("pointer analysis is no longer supported (see Go issue #59676)")

case "rta":
mains, err := mainPackages(pkgs)
Expand Down
16 changes: 0 additions & 16 deletions cmd/callgraph/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ func TestCallgraph(t *testing.T) {
"pkg.main --> pkg.main2",
"pkg.main2 --> (pkg.D).f",
}},
{"pta", false, []string{
// pta distinguishes main->C, main2->D. Also has a root node.
`<root> --> pkg.init`,
`<root> --> pkg.main`,
`pkg.main --> (pkg.C).f`,
`pkg.main --> pkg.main2`,
`pkg.main2 --> (pkg.D).f`,
}},
// tests: both the package's main and the test's main are called.
// The callgraph includes all the guts of the "testing" package.
{"rta", true, []string{
Expand All @@ -87,14 +79,6 @@ func TestCallgraph(t *testing.T) {
`pkg.Example --> (pkg.C).f`,
`pkg.main --> (pkg.C).f`,
}},
{"pta", true, []string{
`<root> --> pkg.test.main`,
`<root> --> pkg.main`,
`pkg.test.main --> testing.MainStart`,
`testing.runExample --> pkg.Example`,
`pkg.Example --> (pkg.C).f`,
`pkg.main --> (pkg.C).f`,
}},
} {
const format = "{{.Caller}} --> {{.Callee}}"
stdout = new(bytes.Buffer)
Expand Down
Loading

0 comments on commit 96844c3

Please sign in to comment.