| 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 buildir defines an Analyzer that constructs the IR |
| 6 |
// 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 |
// |
| 10 |
// THIS INTERFACE IS EXPERIMENTAL AND MAY BE SUBJECT TO INCOMPATIBLE CHANGE. |
| 11 |
package buildir |
| 12 |
|
| 13 |
import ( |
| 14 |
"go/types" |
| 15 |
"reflect" |
| 16 |
|
| 17 |
"honnef.co/go/tools/go/ir" |
| 18 |
|
| 19 |
"golang.org/x/tools/go/analysis" |
| 20 |
"golang.org/x/tools/go/analysis/passes/ctrlflow" |
| 21 |
) |
| 22 |
|
| 23 |
var Debug = struct { |
| 24 |
Mode ir.BuilderMode |
| 25 |
}{} |
| 26 |
|
| 27 |
var Analyzer = &analysis.Analyzer{ |
| 28 |
Name: "buildir", |
| 29 |
Doc: "build IR for later passes", |
| 30 |
Run: run, |
| 31 |
ResultType: reflect.TypeFor[*IR](), |
| 32 |
Requires: []*analysis.Analyzer{ctrlflow.Analyzer}, |
| 33 |
} |
| 34 |
|
| 35 |
// IR provides intermediate representation for all the |
| 36 |
// source functions in the current package. |
| 37 |
type IR struct { |
| 38 |
Pkg *ir.Package |
| 39 |
SrcFuncs []*ir.Function |
| 40 |
} |
| 41 |
|
| 42 |
func run(pass *analysis.Pass) (any, error) { |
| 43 |
cfgs := pass.ResultOf[ctrlflow.Analyzer].(*ctrlflow.CFGs) |
| 44 |
|
| 45 |
// Plundered from ssautil.BuildPackage. |
| 46 |
|
| 47 |
// We must create a new Program for each Package because the |
| 48 |
// analysis API provides no place to hang a Program shared by |
| 49 |
// all Packages. Consequently, IR Packages and Functions do not |
| 50 |
// have a canonical representation across an analysis session of |
| 51 |
// multiple packages. This is unlikely to be a problem in |
| 52 |
// practice because the analysis API essentially forces all |
| 53 |
// packages to be analysed independently, so any given call to |
| 54 |
// Analysis.Run on a package will see only IR objects belonging |
| 55 |
// to a single Program. |
| 56 |
|
| 57 |
mode := ir.GlobalDebug |
| 58 |
if Debug.Mode != 0 { |
| 59 |
mode = Debug.Mode |
| 60 |
} |
| 61 |
|
| 62 |
prog := ir.NewProgram(pass.Fset, mode) |
| 63 |
|
| 64 |
prog.SetNoReturn(cfgs.NoReturn) |
| 65 |
|
| 66 |
// Create IR packages for all imports. |
| 67 |
// Order is not significant. |
| 68 |
created := make(map[*types.Package]bool) |
| 69 |
var createAll func(pkgs []*types.Package) |
| 70 |
createAll = func(pkgs []*types.Package) { |
| 71 |
for _, p := range pkgs { |
| 72 |
if !created[p] { |
| 73 |
created[p] = true |
| 74 |
prog.CreatePackage(p, nil, nil, true) |
| 75 |
createAll(p.Imports()) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
createAll(pass.Pkg.Imports()) |
| 80 |
|
| 81 |
// Create and build the primary package. |
| 82 |
irpkg := prog.CreatePackage(pass.Pkg, pass.Files, pass.TypesInfo, false) |
| 83 |
irpkg.Build() |
| 84 |
|
| 85 |
// Compute list of source functions, including literals, |
| 86 |
// in source order. |
| 87 |
var addAnons func(f *ir.Function) |
| 88 |
funcs := make([]*ir.Function, len(irpkg.Functions)) |
| 89 |
copy(funcs, irpkg.Functions) |
| 90 |
addAnons = func(f *ir.Function) { |
| 91 |
for _, anon := range f.AnonFuncs { |
| 92 |
funcs = append(funcs, anon) |
| 93 |
addAnons(anon) |
| 94 |
} |
| 95 |
} |
| 96 |
for _, fn := range irpkg.Functions { |
| 97 |
addAnons(fn) |
| 98 |
} |
| 99 |
|
| 100 |
return &IR{Pkg: irpkg, SrcFuncs: funcs}, nil |
| 101 |
} |