|
| 1 | +// Copyright 2024 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 | +//go:build ignore |
| 6 | + |
| 7 | +// The fmtstr command parses the format strings of calls to selected |
| 8 | +// printf-like functions in the specified source file, and prints the |
| 9 | +// formatting operations and their operands. |
| 10 | +// |
| 11 | +// It is intended only for debugging and is not a supported interface. |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "flag" |
| 16 | + "fmt" |
| 17 | + "go/ast" |
| 18 | + "go/parser" |
| 19 | + "go/printer" |
| 20 | + "go/token" |
| 21 | + "log" |
| 22 | + "strconv" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "golang.org/x/tools/internal/fmtstr" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + log.SetPrefix("fmtstr: ") |
| 30 | + log.SetFlags(0) |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + fset := token.NewFileSet() |
| 34 | + f, err := parser.ParseFile(fset, flag.Args()[0], nil, 0) |
| 35 | + if err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + functions := map[string]int{ |
| 40 | + "fmt.Errorf": 0, |
| 41 | + "fmt.Fprintf": 1, |
| 42 | + "fmt.Printf": 0, |
| 43 | + "fmt.Sprintf": 0, |
| 44 | + "log.Printf": 0, |
| 45 | + } |
| 46 | + |
| 47 | + ast.Inspect(f, func(n ast.Node) bool { |
| 48 | + if call, ok := n.(*ast.CallExpr); ok && !call.Ellipsis.IsValid() { |
| 49 | + if sel, ok := call.Fun.(*ast.SelectorExpr); ok && is[*ast.Ident](sel.X) { |
| 50 | + name := sel.X.(*ast.Ident).Name + "." + sel.Sel.Name // e.g. "fmt.Printf" |
| 51 | + if fmtstrIndex, ok := functions[name]; ok && |
| 52 | + len(call.Args) > fmtstrIndex { |
| 53 | + // Is it a string literal? |
| 54 | + if fmtstrArg, ok := call.Args[fmtstrIndex].(*ast.BasicLit); ok && |
| 55 | + fmtstrArg.Kind == token.STRING { |
| 56 | + // Have fmt.Printf("format", ...) |
| 57 | + format, _ := strconv.Unquote(fmtstrArg.Value) |
| 58 | + |
| 59 | + ops, err := fmtstr.Parse(format, 0) |
| 60 | + if err != nil { |
| 61 | + log.Printf("%s: %v", fset.Position(fmtstrArg.Pos()), err) |
| 62 | + return true |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Printf("%s: %s(%s, ...)\n", |
| 66 | + fset.Position(fmtstrArg.Pos()), |
| 67 | + name, |
| 68 | + fmtstrArg.Value) |
| 69 | + for _, op := range ops { |
| 70 | + // TODO(adonovan): show more detail. |
| 71 | + fmt.Printf("\t%q\t%v\n", |
| 72 | + op.Text, |
| 73 | + formatNode(fset, call.Args[op.Verb.ArgIndex])) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return true |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func is[T any](x any) bool { |
| 84 | + _, ok := x.(T) |
| 85 | + return ok |
| 86 | +} |
| 87 | + |
| 88 | +func formatNode(fset *token.FileSet, n ast.Node) string { |
| 89 | + var buf strings.Builder |
| 90 | + if err := printer.Fprint(&buf, fset, n); err != nil { |
| 91 | + return "<error>" |
| 92 | + } |
| 93 | + return buf.String() |
| 94 | +} |
0 commit comments