| 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 constructs a simple control-flow graph (CFG) of the |
| 6 | // statements and expressions within a single function. |
| 7 | // |
| 8 | // Use cfg.New to construct the CFG for a function body. |
| 9 | // |
| 10 | // The blocks of the CFG contain all the function's non-control |
| 11 | // statements. The CFG does not contain control statements such as If, |
| 12 | // Switch, Select, and Branch, but does contain their subexpressions; |
| 13 | // also, each block records the control statement (Block.Stmt) that |
| 14 | // gave rise to it and its relationship (Block.Kind) to that statement. |
| 15 | // |
| 16 | // For example, this source code: |
| 17 | // |
| 18 | // if x := f(); x != nil { |
| 19 | // T() |
| 20 | // } else { |
| 21 | // F() |
| 22 | // } |
| 23 | // |
| 24 | // produces this CFG: |
| 25 | // |
| 26 | // 1: x := f() Body |
| 27 | // x != nil |
| 28 | // succs: 2, 3 |
| 29 | // 2: T() IfThen |
| 30 | // succs: 4 |
| 31 | // 3: F() IfElse |
| 32 | // succs: 4 |
| 33 | // 4: IfDone |
| 34 | // |
| 35 | // The CFG does contain Return statements; even implicit returns are |
| 36 | // materialized (at the position of the function's closing brace). |
| 37 | // |
| 38 | // The CFG does not record conditions associated with conditional branch |
| 39 | // edges, nor the short-circuit semantics of the && and || operators, |
| 40 | // nor abnormal control flow caused by panic. If you need this |
| 41 | // information, use golang.org/x/tools/go/ssa instead. |
| 42 | package cfgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/ |
| 43 | |
| 44 | import ( |
| 45 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 46 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 47 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 48 | "go/formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/" |
| 49 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 50 | ) |
| 51 | |
| 52 | // A CFG represents the control-flow graph of a single function. |
| 53 | // |
| 54 | // The entry point is Blocks[0]; there may be multiple return blocks. |
| 55 | type CFGscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG# struct { |
| 56 | Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#Blocks. []*Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block# // block[0] is entry; order otherwise undefined |
| 57 | noreturnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#noreturn. bool // function body lacks a reachable return statement |
| 58 | } |
| 59 | |
| 60 | // NoReturn reports whether the function has no reachable return. |
| 61 | func (cfg *CFGscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#) NoReturnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#NoReturn().() bool { return cfg.noreturnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#noreturn. } |
| 62 | |
| 63 | // A Block represents a basic block: a list of statements and |
| 64 | // expressions that are always evaluated sequentially. |
| 65 | // |
| 66 | // A block may have 0-2 successors: zero for a return block or a block |
| 67 | // that calls a function such as panic that never returns; one for a |
| 68 | // normal (jump) block; and two for a conditional (if) block. |
| 69 | // |
| 70 | // In a conditional block, the last entry in Nodes is the condition and always |
| 71 | // an [ast.Expr], Succs[0] is the successor if the condition is true, and |
| 72 | // Succs[1] is the successor if the condition is false. |
| 73 | type Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block# struct { |
| 74 | Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Nodes. []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node# // statements, expressions, and ValueSpecs |
| 75 | Succsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Succs. []*Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block# // successor nodes in the graph |
| 76 | Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Index. int32 // index within CFG.Blocks |
| 77 | Livescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Live. bool // block is reachable from entry |
| 78 | returnsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#returns. bool // block contains return or defer (which may recover and return) |
| 79 | Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Kind. BlockKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/BlockKind# // block kind |
| 80 | Stmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Stmt. astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt# // statement that gave rise to this block (see BlockKind for details) |
| 81 | |
| 82 | succs2scip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#succs2. [2]*Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block# // underlying array for Succs |
| 83 | } |
| 84 | |
| 85 | // A BlockKind identifies the purpose of a block. |
| 86 | // It also determines the possible types of its Stmt field. |
| 87 | type BlockKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/BlockKind# uint8 |
| 88 | |
| 89 | const ( |
| 90 | KindInvalidscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindInvalid. BlockKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/BlockKind# = iota // Stmt=nil |
| 91 | |
| 92 | KindUnreachablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindUnreachable. // unreachable block after {Branch,Return}Stmt / no-return call ExprStmt |
| 93 | KindBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindBody. // function body BlockStmt |
| 94 | KindForBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForBody. // body of ForStmt |
| 95 | KindForDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForDone. // block after ForStmt |
| 96 | KindForLoopscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForLoop. // head of ForStmt |
| 97 | KindForPostscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForPost. // post condition of ForStmt |
| 98 | KindIfDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindIfDone. // block after IfStmt |
| 99 | KindIfElsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindIfElse. // else block of IfStmt |
| 100 | KindIfThenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindIfThen. // then block of IfStmt |
| 101 | KindLabelscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindLabel. // labeled block of BranchStmt (Stmt may be nil for dangling label) |
| 102 | KindRangeBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindRangeBody. // body of RangeStmt |
| 103 | KindRangeDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindRangeDone. // block after RangeStmt |
| 104 | KindRangeLoopscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindRangeLoop. // head of RangeStmt |
| 105 | KindSelectCaseBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSelectCaseBody. // body of SelectStmt |
| 106 | KindSelectDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSelectDone. // block after SelectStmt |
| 107 | KindSelectAfterCasescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSelectAfterCase. // block after a CommClause |
| 108 | KindSwitchCaseBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSwitchCaseBody. // body of CaseClause |
| 109 | KindSwitchDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSwitchDone. // block after {Type.}SwitchStmt |
| 110 | KindSwitchNextCasescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSwitchNextCase. // secondary expression of a multi-expression CaseClause |
| 111 | ) |
| 112 | |
| 113 | func (kind BlockKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/BlockKind#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/BlockKind#String().() string { |
| 114 | return [...]string{ |
| 115 | KindInvalidscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindInvalid.: "Invalid", |
| 116 | KindUnreachablescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindUnreachable.: "Unreachable", |
| 117 | KindBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindBody.: "Body", |
| 118 | KindForBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForBody.: "ForBody", |
| 119 | KindForDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForDone.: "ForDone", |
| 120 | KindForLoopscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForLoop.: "ForLoop", |
| 121 | KindForPostscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindForPost.: "ForPost", |
| 122 | KindIfDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindIfDone.: "IfDone", |
| 123 | KindIfElsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindIfElse.: "IfElse", |
| 124 | KindIfThenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindIfThen.: "IfThen", |
| 125 | KindLabelscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindLabel.: "Label", |
| 126 | KindRangeBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindRangeBody.: "RangeBody", |
| 127 | KindRangeDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindRangeDone.: "RangeDone", |
| 128 | KindRangeLoopscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindRangeLoop.: "RangeLoop", |
| 129 | KindSelectCaseBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSelectCaseBody.: "SelectCaseBody", |
| 130 | KindSelectDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSelectDone.: "SelectDone", |
| 131 | KindSelectAfterCasescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSelectAfterCase.: "SelectAfterCase", |
| 132 | KindSwitchCaseBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSwitchCaseBody.: "SwitchCaseBody", |
| 133 | KindSwitchDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSwitchDone.: "SwitchDone", |
| 134 | KindSwitchNextCasescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindSwitchNextCase.: "SwitchNextCase", |
| 135 | }[kind] |
| 136 | } |
| 137 | |
| 138 | // New returns a new control-flow graph for the specified function body, |
| 139 | // which must be non-nil. |
| 140 | // |
| 141 | // The CFG builder calls mayReturn to determine whether a given function |
| 142 | // call may return. For example, calls to panic, os.Exit, and log.Fatal |
| 143 | // do not return, so the builder can remove infeasible graph edges |
| 144 | // following such calls. The builder calls mayReturn only for a |
| 145 | // CallExpr beneath an ExprStmt. |
| 146 | func Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/New().(body *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#, mayReturn func(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) bool) *CFGscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG# { |
| 147 | b := builderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#{ |
| 148 | mayReturnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#mayReturn.: mayReturn, |
| 149 | } |
| 150 | b.currentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#current. = b.newBlockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#newBlock().(KindBodyscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/KindBody., body) |
| 151 | b.stmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#stmt().(body) |
| 152 | |
| 153 | // Compute liveness (reachability from entry point), |
| 154 | // breadth-first, marking Block.Live flags. |
| 155 | q := make([]*Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#, 0, len(b.blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#blocks.)) |
| 156 | q = append(q, b.blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#blocks.[0]) // entry point |
| 157 | for len(q) > 0 { |
| 158 | b := q[len(q)-1] |
| 159 | q = q[:len(q)-1] |
| 160 | |
| 161 | if !b.Livescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Live. { |
| 162 | b.Livescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Live. = true |
| 163 | q = append(q, b.Succsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Succs....) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Does control fall off the end of the function's body? |
| 168 | // Make implicit return explicit. |
| 169 | if b.currentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#current. != nil && b.currentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#current..Livescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Live. { |
| 170 | b.currentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#current..returnsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#returns. = true |
| 171 | b.addscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#add().(&astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ReturnStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ReturnStmt#{ |
| 172 | Returnscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ReturnStmt#Return.: body.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#End().() - 1, |
| 173 | }) |
| 174 | } |
| 175 | |
| 176 | // Is any return (or defer+recover) block reachable? |
| 177 | noreturn := true |
| 178 | for _, bl := range b.blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#blocks. { |
| 179 | if bl.Livescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Live. && bl.returnsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#returns. { |
| 180 | noreturn = false |
| 181 | break |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return &CFGscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#{Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#Blocks.: b.blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/builder#blocks., noreturnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#noreturn.: noreturn} |
| 186 | } |
| 187 | |
| 188 | func (b *Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#String().() string { |
| 189 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("block %d (%s)", b.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Index., b.commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#comment().(nil)) |
| 190 | } |
| 191 | |
| 192 | func (b *Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#) commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#comment().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#) string { |
| 193 | s := b.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Kind..Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/BlockKind#String().() |
| 194 | if fset != nil && b.Stmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Stmt. != nil { |
| 195 | s = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s@L%d", s, fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(b.Stmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Stmt..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line.) |
| 196 | } |
| 197 | return s |
| 198 | } |
| 199 | |
| 200 | // Return returns the return statement at the end of this block if present, nil |
| 201 | // otherwise. |
| 202 | // |
| 203 | // When control falls off the end of the function, the ReturnStmt is synthetic |
| 204 | // and its [ast.Node.End] position may be beyond the end of the file. |
| 205 | // |
| 206 | // A function that contains no return statement (explicit or implied) |
| 207 | // may yet return normally, and may even return a nonzero value. For example: |
| 208 | // |
| 209 | // func() (res any) { |
| 210 | // defer func() { res = recover() }() |
| 211 | // panic(123) |
| 212 | // } |
| 213 | func (b *Blockscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#) Returnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Return().() (ret *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ReturnStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ReturnStmt#) { |
| 214 | if len(b.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Nodes.) > 0 { |
| 215 | ret, _ = b.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Nodes.[len(b.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Nodes.)-1].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ReturnStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ReturnStmt#) |
| 216 | } |
| 217 | return |
| 218 | } |
| 219 | |
| 220 | // Format formats the control-flow graph for ease of debugging. |
| 221 | func (g *CFGscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#) Formatscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#Format().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#) string { |
| 222 | var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 223 | for _, b := range g.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#Blocks. { |
| 224 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, ".%d: # %s\n", b.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Index., b.commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#comment().(fset)) |
| 225 | for _, n := range b.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Nodes. { |
| 226 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, "\t%s\n", formatNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/formatNode().(fset, n)) |
| 227 | } |
| 228 | if len(b.Succsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Succs.) > 0 { |
| 229 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, "\tsuccs:") |
| 230 | for _, succ := range b.Succsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Succs. { |
| 231 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, " %d", succ.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Index.) |
| 232 | } |
| 233 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteByte().('\n') |
| 234 | } |
| 235 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteByte().('\n') |
| 236 | } |
| 237 | return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().() |
| 238 | } |
| 239 | |
| 240 | // Dot returns the control-flow graph in the [Dot graph description language]. |
| 241 | // Use a command such as 'dot -Tsvg' to render it in a form viewable in a browser. |
| 242 | // This method is provided as a debugging aid; the details of the |
| 243 | // output are unspecified and may change. |
| 244 | // |
| 245 | // [Dot graph description language]: https://en.wikipedia.org/wiki/DOT_(graph_description_language) |
| 246 | func (g *CFGscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#) Dotscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#Dot().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#) string { |
| 247 | var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 248 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteString().("digraph CFG {\n") |
| 249 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteString().(" node [shape=box];\n") |
| 250 | for _, b := range g.Blocksscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/CFG#Blocks. { |
| 251 | // node label |
| 252 | var text bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 253 | text.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteString().(b.commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#comment().(fset)) |
| 254 | for _, n := range b.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Nodes. { |
| 255 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&text, "\n%s", formatNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/formatNode().(fset, n)) |
| 256 | } |
| 257 | |
| 258 | // node and edges |
| 259 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, " n%d [label=%q];\n", b.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Index., &text) |
| 260 | for _, succ := range b.Succsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Succs. { |
| 261 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&buf, " n%d -> n%d;\n", b.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Index., succ.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/Block#Index.) |
| 262 | } |
| 263 | } |
| 264 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#WriteString().("}\n") |
| 265 | return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().() |
| 266 | } |
| 267 | |
| 268 | func formatNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/cfg`/formatNode().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) string { |
| 269 | var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 270 | formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/format`/Node().(&buf, fset, n) |
| 271 | // Indent secondary lines by a tab. |
| 272 | return string(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Replacescip-go gomod github.com/golang/go/src go1.25.0 bytes/Replace().(buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), []byte("\n"), []byte("\n\t"), -1)) |
| 273 | } |