@@ -109,9 +109,20 @@ Flags:
109
109
110
110
Caller and Callee are *ssa.Function values, which print as
111
111
"(*sync/atomic.Mutex).Lock", but other attributes may be
112
- derived from them, e.g. Caller.Pkg.Pkg.Path yields the
113
- import path of the enclosing package. Consult the go/ssa
114
- API documentation for details.
112
+ derived from them. For example:
113
+
114
+ - {{.Caller.Pkg.Pkg.Path}} yields the import path of the
115
+ enclosing package; and
116
+
117
+ - {{(.Caller.Prog.Fset.Position .Caller.Pos).Filename}}
118
+ yields the name of the file that declares the caller.
119
+
120
+ - The 'posn' template function returns the token.Position
121
+ of an ssa.Function, so the previous example can be
122
+ reduced to {{(posn .Caller).Filename}}.
123
+
124
+ Consult the documentation for go/token, text/template, and
125
+ golang.org/x/tools/go/ssa for more detail.
115
126
116
127
Examples:
117
128
@@ -238,7 +249,12 @@ func doCallgraph(dir, gopath, algo, format string, tests bool, args []string) er
238
249
format = ` {{printf "%q" .Caller}} -> {{printf "%q" .Callee}}`
239
250
}
240
251
241
- tmpl , err := template .New ("-format" ).Parse (format )
252
+ funcMap := template.FuncMap {
253
+ "posn" : func (f * ssa.Function ) token.Position {
254
+ return f .Prog .Fset .Position (f .Pos ())
255
+ },
256
+ }
257
+ tmpl , err := template .New ("-format" ).Funcs (funcMap ).Parse (format )
242
258
if err != nil {
243
259
return fmt .Errorf ("invalid -format template: %v" , err )
244
260
}
0 commit comments