| 1 | // Copyright 2018 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 buildssa defines an Analyzer that constructs the SSA |
| 6 | // representation of an error-free package and returns the set of all |
| 7 | // functions within it. It does not report any diagnostics itself but |
| 8 | // may be used as an input to other analyzers. |
| 9 | package buildssa |
| 10 | |
| 11 | import ( |
| 12 | "go/ast" |
| 13 | "go/types" |
| 14 | "iter" |
| 15 | "reflect" |
| 16 | |
| 17 | "golang.org/x/tools/go/analysis" |
| 18 | "golang.org/x/tools/go/analysis/passes/ctrlflow" |
| 19 | "golang.org/x/tools/go/ssa" |
| 20 | ) |
| 21 | |
| 22 | var Analyzer = &analysis.Analyzer{ |
| 23 | Name: "buildssa", |
| 24 | Doc: "build SSA-form IR for later passes", |
| 25 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/buildssa", |
| 26 | Run: run, |
| 27 | Requires: []*analysis.Analyzer{ctrlflow.Analyzer}, |
| 28 | ResultType: reflect.TypeFor[*SSA](), |
| 29 | // Do not add FactTypes here: SSA construction of P must not |
| 30 | // require SSA construction of all of P's dependencies. |
| 31 | // (That's why we enlist the cheaper ctrlflow pass to compute |
| 32 | // noreturn instead of having go/ssa + buildssa do it.) |
| 33 | FactTypes: nil, |
| 34 | } |
| 35 | |
| 36 | // SSA provides SSA-form intermediate representation for all the |
| 37 | // source functions in the current package. |
| 38 | type SSA struct { |
| 39 | Pkg *ssa.Package |
| 40 | SrcFuncs []*ssa.Function |
| 41 | } |
| 42 | |
| 43 | func run(pass *analysis.Pass) (any, error) { |
| 44 | cfgs := pass.ResultOf[ctrlflow.Analyzer].(*ctrlflow.CFGs) |
| 45 | |
| 46 | // We must create a new Program for each Package because the |
| 47 | // analysis API provides no place to hang a Program shared by |
| 48 | // all Packages. Consequently, SSA Packages and Functions do not |
| 49 | // have a canonical representation across an analysis session of |
| 50 | // multiple packages. This is unlikely to be a problem in |
| 51 | // practice because the analysis API essentially forces all |
| 52 | // packages to be analysed independently, so any given call to |
| 53 | // Analysis.Run on a package will see only SSA objects belonging |
| 54 | // to a single Program. |
| 55 | |
| 56 | // Some Analyzers may need GlobalDebug, in which case we'll have |
| 57 | // to set it globally, but let's wait till we need it. |
| 58 | mode := ssa.BuilderMode(0) |
| 59 | |
| 60 | prog := ssa.NewProgram(pass.Fset, mode) |
| 61 | |
| 62 | // Use the result of the ctrlflow analysis to improve the SSA CFG. |
| 63 | prog.SetNoReturn(cfgs.NoReturn) |
| 64 | |
| 65 | // Create SSA packages for direct imports. |
| 66 | for _, p := range pass.Pkg.Imports() { |
| 67 | prog.CreatePackage(p, nil, nil, true) |
| 68 | } |
| 69 | |
| 70 | // Create and build the primary package. |
| 71 | ssapkg := prog.CreatePackage(pass.Pkg, pass.Files, pass.TypesInfo, false) |
| 72 | ssapkg.Build() |
| 73 | |
| 74 | // Compute list of source functions, including literals, |
| 75 | // in source order. |
| 76 | var funcs []*ssa.Function |
| 77 | for _, fn := range allFunctions(pass) { |
| 78 | // (init functions have distinct Func |
| 79 | // objects named "init" and distinct |
| 80 | // ssa.Functions named "init#1", ...) |
| 81 | |
| 82 | f := ssapkg.Prog.FuncValue(fn) |
| 83 | if f == nil { |
| 84 | panic(fn) |
| 85 | } |
| 86 | |
| 87 | var addAnons func(f *ssa.Function) |
| 88 | addAnons = func(f *ssa.Function) { |
| 89 | funcs = append(funcs, f) |
| 90 | for _, anon := range f.AnonFuncs { |
| 91 | addAnons(anon) |
| 92 | } |
| 93 | } |
| 94 | addAnons(f) |
| 95 | } |
| 96 | |
| 97 | return &SSA{Pkg: ssapkg, SrcFuncs: funcs}, nil |
| 98 | } |
| 99 | |
| 100 | // allFunctions returns an iterator over all named functions. |
| 101 | func allFunctions(pass *analysis.Pass) iter.Seq2[*ast.FuncDecl, *types.Func] { |
| 102 | return func(yield func(*ast.FuncDecl, *types.Func) bool) { |
| 103 | for _, file := range pass.Files { |
| 104 | for _, decl := range file.Decls { |
| 105 | if decl, ok := decl.(*ast.FuncDecl); ok { |
| 106 | fn := pass.TypesInfo.Defs[decl.Name].(*types.Func) |
| 107 | if !yield(decl, fn) { |
| 108 | return |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |