Source file src/cmd/vendor/golang.org/x/tools/go/analysis/passes/ctrlflow/ctrlflow.go

     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 ctrlflow is an analysis that provides a syntactic
     6  // control-flow graph (CFG) for the body of a function.
     7  // It records whether a function cannot return.
     8  // By itself, it does not report any diagnostics.
     9  package ctrlflow
    10  
    11  import (
    12  	"go/ast"
    13  	"go/types"
    14  	"log"
    15  	"reflect"
    16  
    17  	"golang.org/x/tools/go/analysis"
    18  	"golang.org/x/tools/go/analysis/passes/inspect"
    19  	"golang.org/x/tools/go/analysis/passes/internal/ctrlflowinternal"
    20  	"golang.org/x/tools/go/ast/inspector"
    21  	"golang.org/x/tools/go/cfg"
    22  	"golang.org/x/tools/go/types/typeutil"
    23  	"golang.org/x/tools/internal/cfginternal"
    24  	"golang.org/x/tools/internal/typesinternal"
    25  )
    26  
    27  var Analyzer = &analysis.Analyzer{
    28  	Name:       "ctrlflow",
    29  	Doc:        "build a control-flow graph",
    30  	URL:        "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/ctrlflow",
    31  	Run:        run,
    32  	ResultType: reflect.TypeFor[*CFGs](),
    33  	FactTypes:  []analysis.Fact{new(noReturn)},
    34  	Requires:   []*analysis.Analyzer{inspect.Analyzer},
    35  }
    36  
    37  // noReturn is a fact indicating that a function does not return.
    38  type noReturn struct{}
    39  
    40  func (*noReturn) AFact() {}
    41  
    42  func (*noReturn) String() string { return "noReturn" }
    43  
    44  // A CFGs holds the control-flow graphs
    45  // for all the functions of the current package.
    46  type CFGs struct {
    47  	defs      map[*ast.Ident]types.Object // from Pass.TypesInfo.Defs
    48  	funcDecls map[*types.Func]*declInfo
    49  	funcLits  map[*ast.FuncLit]*litInfo
    50  	noReturn  map[*types.Func]bool // functions lacking a reachable return statement
    51  	pass      *analysis.Pass       // transient; nil after construction
    52  }
    53  
    54  // TODO(adonovan): add (*CFGs).NoReturn to public API.
    55  func (c *CFGs) isNoReturn(fn *types.Func) bool {
    56  	return c.noReturn[fn]
    57  }
    58  
    59  func init() {
    60  	// Expose the hidden method to callers in x/tools.
    61  	ctrlflowinternal.NoReturn = func(c any, fn *types.Func) bool {
    62  		return c.(*CFGs).isNoReturn(fn)
    63  	}
    64  }
    65  
    66  // CFGs has two maps: funcDecls for named functions and funcLits for
    67  // unnamed ones. Unlike funcLits, the funcDecls map is not keyed by its
    68  // syntax node, *ast.FuncDecl, because callMayReturn needs to do a
    69  // look-up by *types.Func, and you can get from an *ast.FuncDecl to a
    70  // *types.Func but not the other way.
    71  
    72  type declInfo struct {
    73  	decl    *ast.FuncDecl
    74  	cfg     *cfg.CFG // iff decl.Body != nil
    75  	started bool     // to break cycles
    76  }
    77  
    78  type litInfo struct {
    79  	cfg      *cfg.CFG
    80  	noReturn bool // (currently unused)
    81  }
    82  
    83  // FuncDecl returns the control-flow graph for a named function.
    84  // It returns nil if decl.Body==nil.
    85  func (c *CFGs) FuncDecl(decl *ast.FuncDecl) *cfg.CFG {
    86  	if decl.Body == nil {
    87  		return nil
    88  	}
    89  	fn := c.defs[decl.Name].(*types.Func)
    90  	return c.funcDecls[fn].cfg
    91  }
    92  
    93  // FuncLit returns the control-flow graph for a literal function.
    94  func (c *CFGs) FuncLit(lit *ast.FuncLit) *cfg.CFG {
    95  	return c.funcLits[lit].cfg
    96  }
    97  
    98  func run(pass *analysis.Pass) (any, error) {
    99  	inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
   100  
   101  	// Because CFG construction consumes and produces noReturn
   102  	// facts, CFGs for exported FuncDecls must be built before 'run'
   103  	// returns; we cannot construct them lazily.
   104  	// (We could build CFGs for FuncLits lazily,
   105  	// but the benefit is marginal.)
   106  
   107  	// Pass 1. Map types.Funcs to ast.FuncDecls in this package.
   108  	funcDecls := make(map[*types.Func]*declInfo) // functions and methods
   109  	funcLits := make(map[*ast.FuncLit]*litInfo)
   110  
   111  	var decls []*types.Func // keys(funcDecls), in order
   112  	var lits []*ast.FuncLit // keys(funcLits), in order
   113  
   114  	nodeFilter := []ast.Node{
   115  		(*ast.FuncDecl)(nil),
   116  		(*ast.FuncLit)(nil),
   117  	}
   118  	inspect.Preorder(nodeFilter, func(n ast.Node) {
   119  		switch n := n.(type) {
   120  		case *ast.FuncDecl:
   121  			// Type information may be incomplete.
   122  			if fn, ok := pass.TypesInfo.Defs[n.Name].(*types.Func); ok {
   123  				funcDecls[fn] = &declInfo{decl: n}
   124  				decls = append(decls, fn)
   125  			}
   126  		case *ast.FuncLit:
   127  			funcLits[n] = new(litInfo)
   128  			lits = append(lits, n)
   129  		}
   130  	})
   131  
   132  	c := &CFGs{
   133  		defs:      pass.TypesInfo.Defs,
   134  		funcDecls: funcDecls,
   135  		funcLits:  funcLits,
   136  		noReturn:  make(map[*types.Func]bool),
   137  		pass:      pass,
   138  	}
   139  
   140  	// Pass 2. Build CFGs.
   141  
   142  	// Build CFGs for named functions.
   143  	// Cycles in the static call graph are broken
   144  	// arbitrarily but deterministically.
   145  	// We create noReturn facts as discovered.
   146  	for _, fn := range decls {
   147  		c.buildDecl(fn, funcDecls[fn])
   148  	}
   149  
   150  	// Build CFGs for literal functions.
   151  	// These aren't relevant to facts (since they aren't named)
   152  	// but are required for the CFGs.FuncLit API.
   153  	for _, lit := range lits {
   154  		li := funcLits[lit]
   155  		if li.cfg == nil {
   156  			li.cfg = cfg.New(lit.Body, c.callMayReturn)
   157  			if cfginternal.IsNoReturn(li.cfg) {
   158  				li.noReturn = true
   159  			}
   160  		}
   161  	}
   162  
   163  	// All CFGs are now built.
   164  	c.pass = nil
   165  
   166  	return c, nil
   167  }
   168  
   169  // di.cfg may be nil on return.
   170  func (c *CFGs) buildDecl(fn *types.Func, di *declInfo) {
   171  	// buildDecl may call itself recursively for the same function,
   172  	// because cfg.New is passed the callMayReturn method, which
   173  	// builds the CFG of the callee, leading to recursion.
   174  	// The buildDecl call tree thus resembles the static call graph.
   175  	// We mark each node when we start working on it to break cycles.
   176  
   177  	if di.started {
   178  		return // break cycle
   179  	}
   180  	di.started = true
   181  
   182  	noreturn := isIntrinsicNoReturn(fn)
   183  
   184  	if di.decl.Body != nil {
   185  		di.cfg = cfg.New(di.decl.Body, c.callMayReturn)
   186  		if cfginternal.IsNoReturn(di.cfg) {
   187  			noreturn = true
   188  		}
   189  	}
   190  	if noreturn {
   191  		c.pass.ExportObjectFact(fn, new(noReturn))
   192  		c.noReturn[fn] = true
   193  	}
   194  
   195  	// debugging
   196  	if false {
   197  		log.Printf("CFG for %s:\n%s (noreturn=%t)\n", fn, di.cfg.Format(c.pass.Fset), noreturn)
   198  	}
   199  }
   200  
   201  // callMayReturn reports whether the called function may return.
   202  // It is passed to the CFG builder.
   203  func (c *CFGs) callMayReturn(call *ast.CallExpr) (r bool) {
   204  	if id, ok := call.Fun.(*ast.Ident); ok && c.pass.TypesInfo.Uses[id] == panicBuiltin {
   205  		return false // panic never returns
   206  	}
   207  
   208  	// Is this a static call? Also includes static functions
   209  	// parameterized by a type. Such functions may or may not
   210  	// return depending on the parameter type, but in some
   211  	// cases the answer is definite. We let ctrlflow figure
   212  	// that out.
   213  	fn := typeutil.StaticCallee(c.pass.TypesInfo, call)
   214  	if fn == nil {
   215  		return true // callee not statically known; be conservative
   216  	}
   217  
   218  	// Function or method declared in this package?
   219  	if di, ok := c.funcDecls[fn]; ok {
   220  		c.buildDecl(fn, di)
   221  		return !c.noReturn[fn]
   222  	}
   223  
   224  	// Not declared in this package.
   225  	// Is there a fact from another package?
   226  	if c.pass.ImportObjectFact(fn, new(noReturn)) {
   227  		c.noReturn[fn] = true
   228  		return false
   229  	}
   230  
   231  	return true
   232  }
   233  
   234  var panicBuiltin = types.Universe.Lookup("panic").(*types.Builtin)
   235  
   236  // isIntrinsicNoReturn reports whether a function intrinsically never
   237  // returns because it stops execution of the calling thread.
   238  // It is the base case in the recursion.
   239  func isIntrinsicNoReturn(fn *types.Func) bool {
   240  	// Add functions here as the need arises, but don't allocate memory.
   241  	return typesinternal.IsFunctionNamed(fn, "syscall", "Exit", "ExitProcess", "ExitThread") ||
   242  		typesinternal.IsFunctionNamed(fn, "runtime", "Goexit")
   243  }
   244  

View as plain text