/
main.go
225 lines (192 loc) · 6.39 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// guru: a tool for answering questions about Go source code.
//
// http://golang.org/s/using-guru
//
// Run with -help flag or help subcommand for usage information.
//
package main // import "github.com/april1989/origin-go-tools/cmd/guru"
import (
"bufio"
"flag"
"fmt"
"go/build"
"go/token"
"io"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"sync"
"github.com/april1989/origin-go-tools/go/buildutil"
)
// flags
var (
modifiedFlag = flag.Bool("modified", false, "read archive of modified files from standard input")
scopeFlag = flag.String("scope", "", "comma-separated list of `packages` the analysis should be limited to")
ptalogFlag = flag.String("ptalog", "", "write points-to analysis log to `file`")
jsonFlag = flag.Bool("json", false, "emit output in JSON format")
reflectFlag = flag.Bool("reflect", false, "analyze reflection soundly (slow)")
cpuprofileFlag = flag.String("cpuprofile", "", "write CPU profile to `file`")
)
func init() {
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
// gccgo does not provide a GOROOT with standard library sources.
// If we have one in the environment, force gc mode.
if build.Default.Compiler == "gccgo" {
if _, err := os.Stat(filepath.Join(runtime.GOROOT(), "src", "runtime", "runtime.go")); err == nil {
build.Default.Compiler = "gc"
}
}
}
const useHelp = "Run 'guru -help' for more information.\n"
const helpMessage = `Go source code guru.
Usage: guru [flags] <mode> <position>
The mode argument determines the query to perform:
callees show possible targets of selected function call
callers show possible callers of selected function
callstack show path from callgraph root to selected function
definition show declaration of selected identifier
describe describe selected syntax: definition, methods, etc
freevars show free variables of selection
implements show 'implements' relation for selected type or method
peers show send/receive corresponding to selected channel op
pointsto show variables the selected pointer may point to
referrers show all refs to entity denoted by selected identifier
what show basic information about the selected syntax node
whicherrs show possible values of the selected error variable
The position argument specifies the filename and byte offset (or range)
of the syntax element to query. For example:
foo.go:#123,#128
bar.go:#123
The -json flag causes guru to emit output in JSON format;
github.com/april1989/origin-go-tools/cmd/guru/serial defines its schema.
Otherwise, the output is in an editor-friendly format in which
every line has the form "pos: text", where pos is "-" if unknown.
The -modified flag causes guru to read an archive from standard input.
Files in this archive will be used in preference to those in
the file system. In this way, a text editor may supply guru
with the contents of its unsaved buffers. Each archive entry
consists of the file name, a newline, the decimal file size,
another newline, and the contents of the file.
The -scope flag restricts analysis to the specified packages.
Its value is a comma-separated list of patterns of these forms:
github.com/april1989/origin-go-tools/cmd/guru # a single package
github.com/april1989/origin-go-tools/... # all packages beneath dir
... # the entire workspace.
A pattern preceded by '-' is negative, so the scope
encoding/...,-encoding/xml
matches all encoding packages except encoding/xml.
User manual: http://golang.org/s/using-guru
Example: describe syntax at offset 530 in this file (an import spec):
$ guru describe src/github.com/april1989/origin-go-tools/cmd/guru/main.go:#530
`
func printHelp() {
fmt.Fprintln(os.Stderr, helpMessage)
fmt.Fprintln(os.Stderr, "Flags:")
flag.PrintDefaults()
}
func main() {
log.SetPrefix("guru: ")
log.SetFlags(0)
// Don't print full help unless -help was requested.
// Just gently remind users that it's there.
flag.Usage = func() { fmt.Fprint(os.Stderr, useHelp) }
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError) // hack
if err := flag.CommandLine.Parse(os.Args[1:]); err != nil {
// (err has already been printed)
if err == flag.ErrHelp {
printHelp()
}
os.Exit(2)
}
args := flag.Args()
if len(args) != 2 {
flag.Usage()
os.Exit(2)
}
mode, posn := args[0], args[1]
if mode == "help" {
printHelp()
os.Exit(2)
}
// 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)
}
}()
}
}
// Profiling support.
if *cpuprofileFlag != "" {
f, err := os.Create(*cpuprofileFlag)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
ctxt := &build.Default
// If there were modified files,
// read them from the standard input and
// overlay them on the build context.
if *modifiedFlag {
modified, err := buildutil.ParseOverlayArchive(os.Stdin)
if err != nil {
log.Fatal(err)
}
// All I/O done by guru needs to consult the modified map.
// The ReadFile done by referrers does,
// but the loader's cgo preprocessing currently does not.
if len(modified) > 0 {
ctxt = buildutil.OverlayContext(ctxt, modified)
}
}
var outputMu sync.Mutex
output := func(fset *token.FileSet, qr QueryResult) {
outputMu.Lock()
defer outputMu.Unlock()
if *jsonFlag {
// JSON output
fmt.Printf("%s\n", qr.JSON(fset))
} else {
// plain output
printf := func(pos interface{}, format string, args ...interface{}) {
fprintf(os.Stdout, fset, pos, format, args...)
}
qr.PrintPlain(printf)
}
}
// Avoid corner case of split("").
var scope []string
if *scopeFlag != "" {
scope = strings.Split(*scopeFlag, ",")
}
// Ask the guru.
query := Query{
Pos: posn,
Build: ctxt,
Scope: scope,
PTALog: ptalog,
Reflection: *reflectFlag,
Output: output,
}
if err := Run(mode, &query); err != nil {
log.Fatal(err)
}
}