| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"go/ast" |
| 5 |
"go/token" |
| 6 |
"go/types" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"golang.org/x/tools/go/analysis" |
| 11 |
"golang.org/x/tools/go/analysis/multichecker" |
| 12 |
) |
| 13 |
|
| 14 |
type indirectCalls struct { |
| 15 |
methods map[token.Pos]string |
| 16 |
functions map[string]token.Pos |
| 17 |
} |
| 18 |
|
| 19 |
var PanicAnalyzer = &analysis.Analyzer{ |
| 20 |
Name: "panic", |
| 21 |
Doc: "finds goroutines that do not initialize the panic handler", |
| 22 |
Run: runPanic, |
| 23 |
ResultType: reflect.TypeFor[*indirectCalls](), |
| 24 |
} |
| 25 |
|
| 26 |
var PanicIndirectAnalyzer = &analysis.Analyzer{ |
| 27 |
Name: "panicindirect", |
| 28 |
Doc: "finds functions called as goroutines that do not initialize the panic handler", |
| 29 |
Run: runPanicIndirect, |
| 30 |
Requires: []*analysis.Analyzer{PanicAnalyzer}, |
| 31 |
} |
| 32 |
|
| 33 |
func runPanic(pass *analysis.Pass) (any, error) { |
| 34 |
var calls indirectCalls |
| 35 |
|
| 36 |
calls.methods = make(map[token.Pos]string) |
| 37 |
calls.functions = make(map[string]token.Pos) |
| 38 |
|
| 39 |
for _, file := range pass.Files { |
| 40 |
ast.Inspect(file, func(n ast.Node) bool { |
| 41 |
g, ok := n.(*ast.GoStmt) |
| 42 |
if !ok { |
| 43 |
return true |
| 44 |
} |
| 45 |
|
| 46 |
var block *ast.BlockStmt |
| 47 |
|
| 48 |
expr := g.Call.Fun |
| 49 |
if e, ok := expr.(*ast.ParenExpr); ok { |
| 50 |
expr = e.X |
| 51 |
} |
| 52 |
|
| 53 |
switch e := expr.(type) { |
| 54 |
case *ast.FuncLit: |
| 55 |
block = e.Body |
| 56 |
case *ast.SelectorExpr: |
| 57 |
sel, ok := pass.TypesInfo.Selections[e] |
| 58 |
if ok { |
| 59 |
f, ok := sel.Obj().(*types.Func) |
| 60 |
if ok { |
| 61 |
calls.methods[f.Pos()] = f.Name() |
| 62 |
} |
| 63 |
} |
| 64 |
case *ast.Ident: |
| 65 |
block = inlineFuncBody(e) |
| 66 |
if block == nil { |
| 67 |
calls.functions[e.Name] = e.NamePos |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if block == nil { |
| 72 |
return true |
| 73 |
} |
| 74 |
|
| 75 |
if !isPanicHandlerInstall(block.List[0]) { |
| 76 |
path := pass.Fset.File(block.Pos()).Name() |
| 77 |
if !strings.HasSuffix(path, "_test.go") { |
| 78 |
pass.Report(panicDiag(block.Pos())) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return true |
| 83 |
}) |
| 84 |
} |
| 85 |
|
| 86 |
return &calls, nil |
| 87 |
} |
| 88 |
|
| 89 |
func runPanicIndirect(pass *analysis.Pass) (any, error) { |
| 90 |
calls := pass.ResultOf[PanicAnalyzer].(*indirectCalls) |
| 91 |
|
| 92 |
for _, file := range pass.Files { |
| 93 |
if strings.HasSuffix(file.Name.Name, "_test") { |
| 94 |
continue |
| 95 |
} |
| 96 |
for _, decl := range file.Decls { |
| 97 |
if f, ok := decl.(*ast.FuncDecl); ok { |
| 98 |
if _, ok := calls.methods[f.Name.Pos()]; ok { |
| 99 |
delete(calls.methods, f.Name.Pos()) |
| 100 |
} else if _, ok := calls.functions[f.Name.Name]; ok { |
| 101 |
delete(calls.functions, f.Name.Name) |
| 102 |
} else { |
| 103 |
continue |
| 104 |
} |
| 105 |
if !isPanicHandlerInstall(f.Body.List[0]) { |
| 106 |
path := pass.Fset.File(f.Body.Pos()).Name() |
| 107 |
if !strings.HasSuffix(path, "_test.go") { |
| 108 |
pass.Report(panicDiag(f.Body.Pos())) |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return nil, nil |
| 116 |
} |
| 117 |
|
| 118 |
func panicDiag(pos token.Pos) analysis.Diagnostic { |
| 119 |
return analysis.Diagnostic{ |
| 120 |
Pos: pos, |
| 121 |
Category: "panic", |
| 122 |
Message: "missing defer log.PanicHandler() as first statement", |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func inlineFuncBody(s *ast.Ident) *ast.BlockStmt { |
| 127 |
if s.Obj == nil || s.Obj.Decl == nil { |
| 128 |
return nil |
| 129 |
} |
| 130 |
d, ok := s.Obj.Decl.(*ast.AssignStmt) |
| 131 |
if !ok { |
| 132 |
return nil |
| 133 |
} |
| 134 |
for _, r := range d.Rhs { |
| 135 |
if f, ok := r.(*ast.FuncLit); ok { |
| 136 |
return f.Body |
| 137 |
} |
| 138 |
} |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
func isPanicHandlerInstall(stmt ast.Stmt) bool { |
| 143 |
d, ok := stmt.(*ast.DeferStmt) |
| 144 |
if !ok { |
| 145 |
return false |
| 146 |
} |
| 147 |
s, ok := d.Call.Fun.(*ast.SelectorExpr) |
| 148 |
if !ok { |
| 149 |
return false |
| 150 |
} |
| 151 |
i, ok := s.X.(*ast.Ident) |
| 152 |
if !ok { |
| 153 |
return false |
| 154 |
} |
| 155 |
return i.Name == "log" && s.Sel.Name == "PanicHandler" |
| 156 |
} |
| 157 |
|
| 158 |
func main() { |
| 159 |
multichecker.Main( |
| 160 |
PanicAnalyzer, |
| 161 |
PanicIndirectAnalyzer, |
| 162 |
) |
| 163 |
} |