|
| 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 | +package yield |
| 6 | + |
| 7 | +// TODO(adonovan): also check for this pattern: |
| 8 | +// |
| 9 | +// for x := range seq { |
| 10 | +// yield(x) |
| 11 | +// } |
| 12 | +// |
| 13 | +// which should be entirely rewritten as |
| 14 | +// |
| 15 | +// seq(yield) |
| 16 | +// |
| 17 | +// to avoid unnecesary range desugaring and chains of dynamic calls. |
| 18 | + |
| 19 | +import ( |
| 20 | + _ "embed" |
| 21 | + "fmt" |
| 22 | + "go/ast" |
| 23 | + "go/token" |
| 24 | + "go/types" |
| 25 | + |
| 26 | + "golang.org/x/tools/go/analysis" |
| 27 | + "golang.org/x/tools/go/analysis/passes/buildssa" |
| 28 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 29 | + "golang.org/x/tools/go/ast/inspector" |
| 30 | + "golang.org/x/tools/go/ssa" |
| 31 | + "golang.org/x/tools/gopls/internal/util/safetoken" |
| 32 | + "golang.org/x/tools/internal/analysisinternal" |
| 33 | +) |
| 34 | + |
| 35 | +//go:embed doc.go |
| 36 | +var doc string |
| 37 | + |
| 38 | +var Analyzer = &analysis.Analyzer{ |
| 39 | + Name: "yield", |
| 40 | + Doc: analysisinternal.MustExtractDoc(doc, "yield"), |
| 41 | + Requires: []*analysis.Analyzer{inspect.Analyzer, buildssa.Analyzer}, |
| 42 | + Run: run, |
| 43 | + URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/yield", |
| 44 | +} |
| 45 | + |
| 46 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 47 | + inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 48 | + |
| 49 | + // Find all calls to yield of the right type. |
| 50 | + yieldCalls := make(map[token.Pos]*ast.CallExpr) // keyed by CallExpr.Lparen. |
| 51 | + nodeFilter := []ast.Node{(*ast.CallExpr)(nil)} |
| 52 | + inspector.Preorder(nodeFilter, func(n ast.Node) { |
| 53 | + call := n.(*ast.CallExpr) |
| 54 | + if id, ok := call.Fun.(*ast.Ident); ok && id.Name == "yield" { |
| 55 | + if sig, ok := pass.TypesInfo.TypeOf(id).(*types.Signature); ok && |
| 56 | + sig.Params().Len() < 3 && |
| 57 | + sig.Results().Len() == 1 && |
| 58 | + types.Identical(sig.Results().At(0).Type(), types.Typ[types.Bool]) { |
| 59 | + yieldCalls[call.Lparen] = call |
| 60 | + } |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + // Common case: nothing to do. |
| 65 | + if len(yieldCalls) == 0 { |
| 66 | + return nil, nil |
| 67 | + } |
| 68 | + |
| 69 | + // Study the control flow using SSA. |
| 70 | + buildssa := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) |
| 71 | + for _, fn := range buildssa.SrcFuncs { |
| 72 | + // TODO(adonovan): opt: skip functions that don't contain any yield calls. |
| 73 | + |
| 74 | + // Find the yield calls in SSA. |
| 75 | + type callInfo struct { |
| 76 | + syntax *ast.CallExpr |
| 77 | + index int // index of instruction within its block |
| 78 | + reported bool |
| 79 | + } |
| 80 | + ssaYieldCalls := make(map[*ssa.Call]*callInfo) |
| 81 | + for _, b := range fn.Blocks { |
| 82 | + for i, instr := range b.Instrs { |
| 83 | + if call, ok := instr.(*ssa.Call); ok { |
| 84 | + if syntax, ok := yieldCalls[call.Pos()]; ok { |
| 85 | + ssaYieldCalls[call] = &callInfo{syntax: syntax, index: i} |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Now search for a control path from the instruction after a |
| 92 | + // yield call to another yield call--possible the same one, |
| 93 | + // following all block successors except "if yield() { ... }"; |
| 94 | + // in such cases we know that yield returned true. |
| 95 | + for call, info := range ssaYieldCalls { |
| 96 | + visited := make([]bool, len(fn.Blocks)) // visited BasicBlock.Indexes |
| 97 | + |
| 98 | + // visit visits the instructions of a block (or a suffix if start > 0). |
| 99 | + var visit func(b *ssa.BasicBlock, start int) |
| 100 | + visit = func(b *ssa.BasicBlock, start int) { |
| 101 | + if !visited[b.Index] { |
| 102 | + if start == 0 { |
| 103 | + visited[b.Index] = true |
| 104 | + } |
| 105 | + for _, instr := range b.Instrs[start:] { |
| 106 | + switch instr := instr.(type) { |
| 107 | + case *ssa.Call: |
| 108 | + if !info.reported && ssaYieldCalls[instr] != nil { |
| 109 | + info.reported = true |
| 110 | + where := "" // "" => same yield call (a loop) |
| 111 | + if instr != call { |
| 112 | + otherLine := safetoken.StartPosition(pass.Fset, instr.Pos()).Line |
| 113 | + where = fmt.Sprintf("(on L%d) ", otherLine) |
| 114 | + } |
| 115 | + pass.Reportf(call.Pos(), "yield may be called again %safter returning false", where) |
| 116 | + } |
| 117 | + case *ssa.If: |
| 118 | + // Visit both successors, unless cond is yield() or its negation. |
| 119 | + // In that case visit only the "if !yield()" block. |
| 120 | + cond := instr.Cond |
| 121 | + t, f := b.Succs[0], b.Succs[1] |
| 122 | + if unop, ok := cond.(*ssa.UnOp); ok && unop.Op == token.NOT { |
| 123 | + cond, t, f = unop.X, f, t |
| 124 | + } |
| 125 | + if cond, ok := cond.(*ssa.Call); ok && ssaYieldCalls[cond] != nil { |
| 126 | + // Skip the successor reached by "if yield() { ... }". |
| 127 | + } else { |
| 128 | + visit(t, 0) |
| 129 | + } |
| 130 | + visit(f, 0) |
| 131 | + |
| 132 | + case *ssa.Jump: |
| 133 | + visit(b.Succs[0], 0) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Start at the instruction after the yield call. |
| 140 | + visit(call.Block(), info.index+1) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return nil, nil |
| 145 | +} |
0 commit comments