1 | // Copyright 2013 The Go Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style |
3 | // license that can be found in the LICENSE file. |
4 | |
5 | package main |
6 | |
7 | import ( |
8 | "fmt" |
9 | "go/token" |
10 | "go/types" |
11 | |
12 | "golang.org/x/tools/cmd/guru/serial" |
13 | "golang.org/x/tools/go/callgraph" |
14 | "golang.org/x/tools/go/loader" |
15 | "golang.org/x/tools/go/ssa" |
16 | "golang.org/x/tools/go/ssa/ssautil" |
17 | ) |
18 | |
19 | // The callers function reports the possible callers of the function |
20 | // immediately enclosing the specified source location. |
21 | func callers(q *Query) error { |
22 | lconf := loader.Config{Build: q.Build} |
23 | |
24 | if err := setPTAScope(&lconf, q.Scope); err != nil { |
25 | return err |
26 | } |
27 | |
28 | // Load/parse/type-check the program. |
29 | lprog, err := loadWithSoftErrors(&lconf) |
30 | if err != nil { |
31 | return err |
32 | } |
33 | |
34 | qpos, err := parseQueryPos(lprog, q.Pos, false) |
35 | if err != nil { |
36 | return err |
37 | } |
38 | |
39 | prog := ssautil.CreateProgram(lprog, 0) |
40 | |
41 | ptaConfig, err := setupPTA(prog, lprog, q.PTALog, q.Reflection) |
42 | if err != nil { |
43 | return err |
44 | } |
45 | |
46 | pkg := prog.Package(qpos.info.Pkg) |
47 | if pkg == nil { |
48 | return fmt.Errorf("no SSA package") |
49 | } |
50 | if !ssa.HasEnclosingFunction(pkg, qpos.path) { |
51 | return fmt.Errorf("this position is not inside a function") |
52 | } |
53 | |
54 | // Defer SSA construction till after errors are reported. |
55 | prog.Build() |
56 | |
57 | target := ssa.EnclosingFunction(pkg, qpos.path) |
58 | if target == nil { |
59 | return fmt.Errorf("no SSA function built for this location (dead code?)") |
60 | } |
61 | |
62 | // If the function is never address-taken, all calls are direct |
63 | // and can be found quickly by inspecting the whole SSA program. |
64 | cg := directCallsTo(target, entryPoints(ptaConfig.Mains)) |
65 | if cg == nil { |
66 | // Run the pointer analysis, recording each |
67 | // call found to originate from target. |
68 | // (Pointer analysis may return fewer results than |
69 | // directCallsTo because it ignores dead code.) |
70 | ptaConfig.BuildCallGraph = true |
71 | cg = ptrAnalysis(ptaConfig).CallGraph |
72 | } |
73 | cg.DeleteSyntheticNodes() |
74 | edges := cg.CreateNode(target).In |
75 | |
76 | // TODO(adonovan): sort + dedup calls to ensure test determinism. |
77 | |
78 | q.Output(lprog.Fset, &callersResult{ |
79 | target: target, |
80 | callgraph: cg, |
81 | edges: edges, |
82 | }) |
83 | return nil |
84 | } |
85 | |
86 | // directCallsTo inspects the whole program and returns a callgraph |
87 | // containing edges for all direct calls to the target function. |
88 | // directCallsTo returns nil if the function is ever address-taken. |
89 | func directCallsTo(target *ssa.Function, entrypoints []*ssa.Function) *callgraph.Graph { |
90 | cg := callgraph.New(nil) // use nil as root *Function |
91 | targetNode := cg.CreateNode(target) |
92 | |
93 | // Is the function a program entry point? |
94 | // If so, add edge from callgraph root. |
95 | for _, f := range entrypoints { |
96 | if f == target { |
97 | callgraph.AddEdge(cg.Root, nil, targetNode) |
98 | } |
99 | } |
100 | |
101 | // Find receiver type (for methods). |
102 | var recvType types.Type |
103 | if recv := target.Signature.Recv(); recv != nil { |
104 | recvType = recv.Type() |
105 | } |
106 | |
107 | // Find all direct calls to function, |
108 | // or a place where its address is taken. |
109 | var space [32]*ssa.Value // preallocate |
110 | for fn := range ssautil.AllFunctions(target.Prog) { |
111 | for _, b := range fn.Blocks { |
112 | for _, instr := range b.Instrs { |
113 | // Is this a method (T).f of a concrete type T |
114 | // whose runtime type descriptor is address-taken? |
115 | // (To be fully sound, we would have to check that |
116 | // the type doesn't make it to reflection as a |
117 | // subelement of some other address-taken type.) |
118 | if recvType != nil { |
119 | if mi, ok := instr.(*ssa.MakeInterface); ok { |
120 | if types.Identical(mi.X.Type(), recvType) { |
121 | return nil // T is address-taken |
122 | } |
123 | if ptr, ok := mi.X.Type().(*types.Pointer); ok && |
124 | types.Identical(ptr.Elem(), recvType) { |
125 | return nil // *T is address-taken |
126 | } |
127 | } |
128 | } |
129 | |
130 | // Direct call to target? |
131 | rands := instr.Operands(space[:0]) |
132 | if site, ok := instr.(ssa.CallInstruction); ok && |
133 | site.Common().Value == target { |
134 | callgraph.AddEdge(cg.CreateNode(fn), site, targetNode) |
135 | rands = rands[1:] // skip .Value (rands[0]) |
136 | } |
137 | |
138 | // Address-taken? |
139 | for _, rand := range rands { |
140 | if rand != nil && *rand == target { |
141 | return nil |
142 | } |
143 | } |
144 | } |
145 | } |
146 | } |
147 | |
148 | return cg |
149 | } |
150 | |
151 | func entryPoints(mains []*ssa.Package) []*ssa.Function { |
152 | var entrypoints []*ssa.Function |
153 | for _, pkg := range mains { |
154 | entrypoints = append(entrypoints, pkg.Func("init")) |
155 | if main := pkg.Func("main"); main != nil && pkg.Pkg.Name() == "main" { |
156 | entrypoints = append(entrypoints, main) |
157 | } |
158 | } |
159 | return entrypoints |
160 | } |
161 | |
162 | type callersResult struct { |
163 | target *ssa.Function |
164 | callgraph *callgraph.Graph |
165 | edges []*callgraph.Edge |
166 | } |
167 | |
168 | func (r *callersResult) PrintPlain(printf printfFunc) { |
169 | root := r.callgraph.Root |
170 | if r.edges == nil { |
171 | printf(r.target, "%s is not reachable in this program.", r.target) |
172 | } else { |
173 | printf(r.target, "%s is called from these %d sites:", r.target, len(r.edges)) |
174 | for _, edge := range r.edges { |
175 | if edge.Caller == root { |
176 | printf(r.target, "the root of the call graph") |
177 | } else { |
178 | printf(edge, "\t%s from %s", edge.Description(), edge.Caller.Func) |
179 | } |
180 | } |
181 | } |
182 | } |
183 | |
184 | func (r *callersResult) JSON(fset *token.FileSet) []byte { |
185 | var callers []serial.Caller |
186 | for _, edge := range r.edges { |
187 | callers = append(callers, serial.Caller{ |
188 | Caller: edge.Caller.Func.String(), |
189 | Pos: fset.Position(edge.Pos()).String(), |
190 | Desc: edge.Description(), |
191 | }) |
192 | } |
193 | return toJSON(callers) |
194 | } |
195 |
Members