| 1 | // Copyright 2016 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 cfg_test |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "go/ast" |
| 11 | "go/format" |
| 12 | "go/parser" |
| 13 | "go/token" |
| 14 | "testing" |
| 15 | |
| 16 | "golang.org/x/tools/go/cfg" |
| 17 | "golang.org/x/tools/go/packages" |
| 18 | "golang.org/x/tools/internal/testenv" |
| 19 | ) |
| 20 | |
| 21 | const src = `package main |
| 22 | |
| 23 | import "log" |
| 24 | |
| 25 | func f1() { |
| 26 | live() |
| 27 | return |
| 28 | dead() |
| 29 | } |
| 30 | |
| 31 | func f2() { |
| 32 | for { |
| 33 | live() |
| 34 | } |
| 35 | dead() |
| 36 | } |
| 37 | |
| 38 | func f3() { |
| 39 | if true { // even known values are ignored |
| 40 | return |
| 41 | } |
| 42 | for true { // even known values are ignored |
| 43 | live() |
| 44 | } |
| 45 | for { |
| 46 | live() |
| 47 | } |
| 48 | dead() |
| 49 | } |
| 50 | |
| 51 | func f4(x int) { |
| 52 | switch x { |
| 53 | case 1: |
| 54 | live() |
| 55 | fallthrough |
| 56 | case 2: |
| 57 | live() |
| 58 | log.Fatal() |
| 59 | default: |
| 60 | panic("oops") |
| 61 | } |
| 62 | dead() |
| 63 | } |
| 64 | |
| 65 | func f4(ch chan int) { |
| 66 | select { |
| 67 | case <-ch: |
| 68 | live() |
| 69 | return |
| 70 | default: |
| 71 | live() |
| 72 | panic("oops") |
| 73 | } |
| 74 | dead() |
| 75 | } |
| 76 | |
| 77 | func f5(unknown bool) { |
| 78 | for { |
| 79 | if unknown { |
| 80 | break |
| 81 | } |
| 82 | continue |
| 83 | dead() |
| 84 | } |
| 85 | live() |
| 86 | } |
| 87 | |
| 88 | func f6(unknown bool) { |
| 89 | outer: |
| 90 | for { |
| 91 | for { |
| 92 | break outer |
| 93 | dead() |
| 94 | } |
| 95 | dead() |
| 96 | } |
| 97 | live() |
| 98 | } |
| 99 | |
| 100 | func f7() { |
| 101 | for { |
| 102 | break nosuchlabel |
| 103 | dead() |
| 104 | } |
| 105 | dead() |
| 106 | } |
| 107 | |
| 108 | func f8() { |
| 109 | select{} |
| 110 | dead() |
| 111 | } |
| 112 | |
| 113 | func f9(ch chan int) { |
| 114 | select { |
| 115 | case <-ch: |
| 116 | return |
| 117 | } |
| 118 | dead() |
| 119 | } |
| 120 | |
| 121 | func f10(ch chan int) { |
| 122 | select { |
| 123 | case <-ch: |
| 124 | return |
| 125 | dead() |
| 126 | default: |
| 127 | } |
| 128 | live() |
| 129 | } |
| 130 | ` |
| 131 | |
| 132 | func TestDeadCode(t *testing.T) { |
| 133 | // We'll use dead code detection to verify the CFG. |
| 134 | |
| 135 | fset := token.NewFileSet() |
| 136 | f, err := parser.ParseFile(fset, "dummy.go", src, parser.Mode(0)) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | for _, decl := range f.Decls { |
| 141 | if decl, ok := decl.(*ast.FuncDecl); ok { |
| 142 | g := cfg.New(decl.Body, mayReturn) |
| 143 | |
| 144 | // Print statements in unreachable blocks |
| 145 | // (in order determined by builder). |
| 146 | var buf bytes.Buffer |
| 147 | for _, b := range g.Blocks { |
| 148 | if !b.Live { |
| 149 | for _, n := range b.Nodes { |
| 150 | fmt.Fprintf(&buf, "\t%s\n", formatNode(fset, n)) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Check that the result contains "dead" at least once but not "live". |
| 156 | if !bytes.Contains(buf.Bytes(), []byte("dead")) || |
| 157 | bytes.Contains(buf.Bytes(), []byte("live")) { |
| 158 | t.Errorf("unexpected dead statements in function %s:\n%s", |
| 159 | decl.Name.Name, |
| 160 | &buf) |
| 161 | t.Logf("control flow graph:\n%s", g.Format(fset)) |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // TestSmoke runs the CFG builder on every FuncDecl in the standard |
| 168 | // library and x/tools. (This is all well-typed code, but it gives |
| 169 | // some coverage.) |
| 170 | func TestSmoke(t *testing.T) { |
| 171 | if testing.Short() { |
| 172 | t.Skip("skipping in short mode") |
| 173 | } |
| 174 | testenv.NeedsTool(t, "go") |
| 175 | |
| 176 | // The Mode API is just hateful. |
| 177 | // https://github.com/golang/go/issues/48226#issuecomment-1948792315 |
| 178 | mode := packages.NeedDeps | packages.NeedImports | packages.NeedSyntax | packages.NeedTypes |
| 179 | pkgs, err := packages.Load(&packages.Config{Mode: mode}, "std", "golang.org/x/tools/...") |
| 180 | if err != nil { |
| 181 | t.Fatal(err) |
| 182 | } |
| 183 | |
| 184 | for _, pkg := range pkgs { |
| 185 | for _, file := range pkg.Syntax { |
| 186 | for _, decl := range file.Decls { |
| 187 | if decl, ok := decl.(*ast.FuncDecl); ok && decl.Body != nil { |
| 188 | g := cfg.New(decl.Body, mayReturn) |
| 189 | |
| 190 | // Run a few quick sanity checks. |
| 191 | failed := false |
| 192 | for i, b := range g.Blocks { |
| 193 | errorf := func(format string, args ...any) { |
| 194 | if !failed { |
| 195 | t.Errorf("%s\n%s", pkg.Fset.Position(decl.Pos()), g.Format(pkg.Fset)) |
| 196 | failed = true |
| 197 | } |
| 198 | msg := fmt.Sprintf(format, args...) |
| 199 | t.Errorf("block %d: %s", i, msg) |
| 200 | } |
| 201 | |
| 202 | if b.Kind == cfg.KindInvalid { |
| 203 | errorf("invalid Block.Kind %v", b.Kind) |
| 204 | } |
| 205 | if b.Stmt == nil && b.Kind != cfg.KindLabel { |
| 206 | errorf("nil Block.Stmt (Kind=%v)", b.Kind) |
| 207 | } |
| 208 | if i != int(b.Index) { |
| 209 | errorf("invalid Block.Index") |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // A trivial mayReturn predicate that looks only at syntax, not types. |
| 219 | func mayReturn(call *ast.CallExpr) bool { |
| 220 | switch fun := call.Fun.(type) { |
| 221 | case *ast.Ident: |
| 222 | return fun.Name != "panic" |
| 223 | case *ast.SelectorExpr: |
| 224 | return fun.Sel.Name != "Fatal" |
| 225 | } |
| 226 | return true |
| 227 | } |
| 228 | |
| 229 | func formatNode(fset *token.FileSet, n ast.Node) string { |
| 230 | var buf bytes.Buffer |
| 231 | format.Node(&buf, fset, n) |
| 232 | // Indent secondary lines by a tab. |
| 233 | return string(bytes.Replace(buf.Bytes(), []byte("\n"), []byte("\n\t"), -1)) |
| 234 | } |