| 1 | // Copyright 2025 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 inspector_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/" |
| 11 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 12 | "iterscip-go gomod github.com/golang/go/src go1.25.0 iter/" |
| 13 | "math/randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/" |
| 14 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 15 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 16 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 17 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 18 | |
| 19 | "golang.org/x/tools/go/ast/edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/" |
| 20 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 21 | ) |
| 22 | |
| 23 | func TestCursor_Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_Preorder().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 24 | inspect := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect. |
| 25 | |
| 26 | nodeFilter := []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#{(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#)(nil), (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#)(nil)} |
| 27 | |
| 28 | // reference implementation |
| 29 | var want []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# |
| 30 | for cur := range inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(nodeFilter...) { |
| 31 | want = append(want, cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) |
| 32 | } |
| 33 | |
| 34 | // Check entire sequence. |
| 35 | got := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(inspect.PreorderSeqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#PreorderSeq().(nodeFilter...)) |
| 36 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, got, want) |
| 37 | |
| 38 | // Check that break works. |
| 39 | got = got[:0] |
| 40 | for _, c := range firstNscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/firstN().(10, inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(nodeFilter...)) { |
| 41 | got = append(got, c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) |
| 42 | } |
| 43 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, got, want[:10]) |
| 44 | } |
| 45 | |
| 46 | func TestCursor_nestedTraversalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_nestedTraversal().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 47 | const src = `package a |
| 48 | func f() { |
| 49 | print("hello") |
| 50 | } |
| 51 | func g() { |
| 52 | print("goodbye") |
| 53 | panic("oops") |
| 54 | } |
| 55 | ` |
| 56 | fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 57 | f, _ := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "a.go", src, 0) |
| 58 | inspect := inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().([]*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}) |
| 59 | |
| 60 | var ( |
| 61 | funcDecls = []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#{(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#)(nil)} |
| 62 | callExprs = []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#{(*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#)(nil)} |
| 63 | nfuncs = 0 |
| 64 | ncalls = 0 |
| 65 | ) |
| 66 | |
| 67 | for curFunc := range inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(funcDecls...) { |
| 68 | _ = curFunc.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) |
| 69 | |
| 70 | // Check edge and index. |
| 71 | if k, idx := curFunc.ParentEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdge().(); k != edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.File_Declsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/File_Decls. || idx != nfuncs { |
| 72 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%v.ParentEdge() = (%v, %d), want edge.File_Decls, %d", curFunc, k, idx, nfuncs) |
| 73 | } |
| 74 | |
| 75 | nfuncs++ |
| 76 | stack := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(curFunc.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().()) |
| 77 | |
| 78 | // Stacks are convenient to print! |
| 79 | if got, want := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(stack), "[*ast.FuncDecl *ast.File]"; got != want { |
| 80 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("curFunc.Enclosing() = %q, want %q", got, want) |
| 81 | } |
| 82 | |
| 83 | // Parent, iterated, is Enclosing stack. |
| 84 | i := 0 |
| 85 | for c := curFunc; c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() != nil; c = c.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() { |
| 86 | if got, want := stack[i], c; got != want { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Enclosing[%d] = %v; Parent()^%d = %v", i, got, i, want) |
| 88 | } |
| 89 | i++ |
| 90 | } |
| 91 | |
| 92 | wantStack := "[*ast.CallExpr *ast.ExprStmt *ast.BlockStmt *ast.FuncDecl *ast.File]" |
| 93 | |
| 94 | // nested Preorder traversal |
| 95 | preorderCount := 0 |
| 96 | for curCall := range curFunc.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(callExprs...) { |
| 97 | _ = curCall.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*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#) |
| 98 | preorderCount++ |
| 99 | stack := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(curCall.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().()) |
| 100 | if got := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(stack); got != wantStack { |
| 101 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("curCall.Enclosing() = %q, want %q", got, wantStack) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // nested Inspect traversal |
| 106 | inspectCount := 0 |
| 107 | curFunc.Inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Inspect().(callExprs, func(curCall inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) (proceed bool) { |
| 108 | _ = curCall.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*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#) |
| 109 | inspectCount++ |
| 110 | stack := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(curCall.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().()) |
| 111 | if got := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(stack); got != wantStack { |
| 112 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("curCall.Enclosing() = %q, want %q", got, wantStack) |
| 113 | } |
| 114 | return true |
| 115 | }) |
| 116 | |
| 117 | if inspectCount != preorderCount { |
| 118 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Inspect (%d) and Preorder (%d) events are not consistent", inspectCount, preorderCount) |
| 119 | } |
| 120 | |
| 121 | ncalls += preorderCount |
| 122 | } |
| 123 | |
| 124 | if nfuncs != 2 { |
| 125 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Found %d FuncDecls, want 2", nfuncs) |
| 126 | } |
| 127 | if ncalls != 3 { |
| 128 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Found %d CallExprs, want 3", ncalls) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestCursor_Childrenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_Children().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 133 | inspect := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect. |
| 134 | |
| 135 | // Assert that Cursor.Children agrees with |
| 136 | // reference implementation for every node. |
| 137 | var want, got []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# |
| 138 | for c := range inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().() { |
| 139 | |
| 140 | // reference implementation |
| 141 | want = want[:0] |
| 142 | { |
| 143 | parent := c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() |
| 144 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(parent, func(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#) bool { |
| 145 | if n != nil && n != parent { |
| 146 | want = append(want, n) |
| 147 | } |
| 148 | return n == parent // descend only into parent |
| 149 | }) |
| 150 | } |
| 151 | |
| 152 | // Check cursor-based implementation |
| 153 | // (uses FirstChild+NextSibling). |
| 154 | got = got[:0] |
| 155 | for child := range c.Childrenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Children().() { |
| 156 | got = append(got, child.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) |
| 157 | } |
| 158 | |
| 159 | if !slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Equalscip-go gomod github.com/golang/go/src go1.25.0 slices/Equal().(got, want) { |
| 160 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("For %v\n"+ |
| 161 | "Using FirstChild+NextSibling: %v\n"+ |
| 162 | "Using ast.Inspect: %v", |
| 163 | c, sliceTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/sliceTypes().(got), sliceTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/sliceTypes().(want)) |
| 164 | } |
| 165 | |
| 166 | // Second cursor-based implementation |
| 167 | // using LastChild+PrevSibling+reverse. |
| 168 | got = got[:0] |
| 169 | for c, ok := c.LastChildscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#LastChild().(); ok; c, ok = c.PrevSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#PrevSibling().() { |
| 170 | got = append(got, c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) |
| 171 | } |
| 172 | slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Reversescip-go gomod github.com/golang/go/src go1.25.0 slices/Reverse().(got) |
| 173 | |
| 174 | if !slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Equalscip-go gomod github.com/golang/go/src go1.25.0 slices/Equal().(got, want) { |
| 175 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("For %v\n"+ |
| 176 | "Using LastChild+PrevSibling: %v\n"+ |
| 177 | "Using ast.Inspect: %v", |
| 178 | c, sliceTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/sliceTypes().(got), sliceTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/sliceTypes().(want)) |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | func TestCursor_Inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_Inspect().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 184 | inspect := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect. |
| 185 | |
| 186 | // In all three loops, we'll gather both kinds of type switches, |
| 187 | // but we'll prune the traversal from descending into (value) switches. |
| 188 | switches := []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#{(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#)(nil), (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#)(nil)} |
| 189 | |
| 190 | // reference implementation (ast.Inspect) |
| 191 | var nodesA []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# |
| 192 | for _, f := range netFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFiles. { |
| 193 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(f, func(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#) (proceed bool) { |
| 194 | switch n.(type) { |
| 195 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#, *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#: |
| 196 | nodesA = append(nodesA, n) |
| 197 | return !isscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/is().[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#](n) // descend only into TypeSwitchStmt |
| 198 | } |
| 199 | return true |
| 200 | }) |
| 201 | } |
| 202 | |
| 203 | // Test Cursor.Inspect implementation. |
| 204 | var nodesB []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# |
| 205 | inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Inspect().(switches, func(c inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#) (proceed bool) { |
| 206 | n := c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() |
| 207 | nodesB = append(nodesB, n) |
| 208 | return !isscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/is().[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#](n) // descend only into TypeSwitchStmt |
| 209 | }) |
| 210 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, nodesA, nodesB) |
| 211 | |
| 212 | // Test WithStack implementation. |
| 213 | var nodesC []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# |
| 214 | inspect.WithStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#WithStack().(switches, func(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#, push bool, stack []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#) (proceed bool) { |
| 215 | if push { |
| 216 | nodesC = append(nodesC, n) |
| 217 | return !isscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/is().[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#](n) // descend only into TypeSwitchStmt |
| 218 | } |
| 219 | return false |
| 220 | }) |
| 221 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, nodesA, nodesC) |
| 222 | } |
| 223 | |
| 224 | func TestCursor_FindNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_FindNode().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 225 | inspect := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect. |
| 226 | |
| 227 | // Enumerate all nodes of a particular type, |
| 228 | // then check that FindByPos can find them, |
| 229 | // starting at the root. |
| 230 | // |
| 231 | // (We use BasicLit because they are numerous.) |
| 232 | root := inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().() |
| 233 | for c := range root.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#)(nil)) { |
| 234 | node := c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() |
| 235 | got, ok := root.FindNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindNode().(node) |
| 236 | if !ok { |
| 237 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("root.FindNode failed") |
| 238 | } else if got != c { |
| 239 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("root.FindNode returned %v, want %v", got, c) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Same thing, but searching only within subtrees (each FuncDecl). |
| 244 | for funcDecl := range root.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#)(nil)) { |
| 245 | for c := range funcDecl.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#)(nil)) { |
| 246 | node := c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() |
| 247 | got, ok := funcDecl.FindNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindNode().(node) |
| 248 | if !ok { |
| 249 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("funcDecl.FindNode failed") |
| 250 | } else if got != c { |
| 251 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("funcDecl.FindNode returned %v, want %v", got, c) |
| 252 | } |
| 253 | |
| 254 | // Also, check that we cannot find the BasicLit |
| 255 | // beneath a different FuncDecl. |
| 256 | if prevFunc, ok := funcDecl.PrevSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#PrevSibling().(); ok { |
| 257 | got, ok := prevFunc.FindNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindNode().(node) |
| 258 | if ok { |
| 259 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("prevFunc.FindNode succeeded unexpectedly: %v", got) |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // TestCursor_FindByPos_order ensures that FindByPos does not assume files are in Pos order. |
| 267 | func TestCursor_FindByPos_orderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_FindByPos_order().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 268 | // Pick an arbitrary decl. |
| 269 | target := netFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFiles.[7].Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[0] |
| 270 | |
| 271 | // Find the target decl by its position. |
| 272 | cur, ok := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect..Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().FindByPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindByPos().(target.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), target.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) |
| 273 | if !ok || cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() != target { |
| 274 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unshuffled: FindByPos(%T) = (%v, %t)", target, cur, ok) |
| 275 | } |
| 276 | |
| 277 | // Shuffle the files out of Pos order. |
| 278 | files := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Clonescip-go gomod github.com/golang/go/src go1.25.0 slices/Clone().(netFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFiles.) |
| 279 | randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/.Shufflescip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Shuffle().(len(files), func(i, j int) { |
| 280 | files[i], files[j] = files[j], files[i] |
| 281 | }) |
| 282 | |
| 283 | // Find it again. |
| 284 | inspect := inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().(files) |
| 285 | cur, ok = inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().FindByPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindByPos().(target.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), target.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) |
| 286 | if !ok || cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() != target { |
| 287 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("shuffled: FindByPos(%T) = (%v, %t)", target, cur, ok) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | func TestCursor_Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_Edge().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 292 | root := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect..Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().() |
| 293 | for cur := range root.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().() { |
| 294 | if cur == root { |
| 295 | continue // root node |
| 296 | } |
| 297 | |
| 298 | var ( |
| 299 | parent = cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 300 | e, idx = cur.ParentEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdge().() |
| 301 | ) |
| 302 | |
| 303 | // ast.File, child of root? |
| 304 | if parent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() == nil { |
| 305 | if e != edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.Invalidscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Invalid. || idx != -1 { |
| 306 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%v.Edge = (%v, %d), want (Invalid, -1)", cur, e, idx) |
| 307 | } |
| 308 | continue |
| 309 | } |
| 310 | |
| 311 | // Check Edge.NodeType matches type of Parent.Node. |
| 312 | if e.NodeTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#NodeType().() != reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(parent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) { |
| 313 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Edge.NodeType = %v, Parent.Node has type %T", |
| 314 | e.NodeTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#NodeType().(), parent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) |
| 315 | } |
| 316 | |
| 317 | // Check c.Edge.Get(c.Parent.Node) == c.Node. |
| 318 | if got := e.Getscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#Get().(parent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().(), idx); got != cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() { |
| 319 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("cur=%v@%s: %s.Get(cur.Parent().Node(), %d) = %T@%s, want cur.Node()", |
| 320 | cur, netFsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()), e, idx, got, netFsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(got.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().())) |
| 321 | } |
| 322 | |
| 323 | // Check c.Parent.ChildAt(c.ParentEdge()) == c. |
| 324 | if got := parent.ChildAtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ChildAt().(e, idx); got != cur { |
| 325 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("cur=%v@%s: cur.Parent().ChildAt(%v, %d) = %T@%s, want cur", |
| 326 | cur, netFsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().()), e, idx, got.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().(), netFsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(got.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().())) |
| 327 | } |
| 328 | |
| 329 | // Check that reflection on the parent finds the current node. |
| 330 | fv := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(parent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()).Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().().FieldByNamescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByName().(e.FieldNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/Kind#FieldName().()) |
| 331 | if idx >= 0 { |
| 332 | fv = fv.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(idx) // element of []ast.Node |
| 333 | } |
| 334 | if fv.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Interface. { |
| 335 | fv = fv.Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().() // e.g. ast.Expr -> *ast.Ident |
| 336 | } |
| 337 | got := fv.Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Interface().().(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#) |
| 338 | if got != cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() { |
| 339 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%v.Edge = (%v, %d); FieldName/Index reflection gave %T@%s, not original node", |
| 340 | cur, e, idx, got, netFsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(got.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().())) |
| 341 | } |
| 342 | |
| 343 | // Check that Cursor.Child is the reverse of Parent. |
| 344 | if cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Childscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Child().(cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) != cur { |
| 345 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Cursor.Parent.Child = %v, want %v", cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Childscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Child().(cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()), cur) |
| 346 | } |
| 347 | |
| 348 | // Check invariants of Contains: |
| 349 | |
| 350 | // A cursor contains itself. |
| 351 | if !cur.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Contains().(cur) { |
| 352 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("!cur.Contains(cur): %v", cur) |
| 353 | } |
| 354 | // A parent contains its child, but not the inverse. |
| 355 | if !parent.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Contains().(cur) { |
| 356 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("!cur.Parent().Contains(cur): %v", cur) |
| 357 | } |
| 358 | if cur.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Contains().(parent) { |
| 359 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("cur.Contains(cur.Parent()): %v", cur) |
| 360 | } |
| 361 | // A grandparent contains its grandchild, but not the inverse. |
| 362 | if grandparent := cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().(); grandparent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() != nil { |
| 363 | if !grandparent.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Contains().(cur) { |
| 364 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("!cur.Parent().Parent().Contains(cur): %v", cur) |
| 365 | } |
| 366 | if cur.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Contains().(grandparent) { |
| 367 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("cur.Contains(cur.Parent().Parent()): %v", cur) |
| 368 | } |
| 369 | } |
| 370 | // A cursor and its uncle/aunt do not contain each other. |
| 371 | if uncle, ok := parent.NextSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#NextSibling().(); ok { |
| 372 | if uncle.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Contains().(cur) { |
| 373 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("cur.Parent().NextSibling().Contains(cur): %v", cur) |
| 374 | } |
| 375 | if cur.Containsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Contains().(uncle) { |
| 376 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("cur.Contains(cur.Parent().NextSibling()): %v", cur) |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // Regression test for multiple matching nodes in FindByPos (#76872). |
| 383 | func TestCursor_FindByPos_Boundaryscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_FindByPos_Boundary().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 384 | // This test verifies that when a cursor position is on the boundary of two |
| 385 | // adjacent nodes (e.g. "foo|("), FindByPos returns the first node |
| 386 | // encountered in traversal order (which is usually the node "to the left"). |
| 387 | // |
| 388 | // Note: The source is intentionally unformatted (no space between ')' and |
| 389 | // '{') to ensure the nodes are strictly adjacent at the boundary. |
| 390 | const src = `package p; func foo(a int){}` |
| 391 | var ( |
| 392 | fset = tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 393 | f, _ = parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "p.go", src, 0) |
| 394 | tokFile = fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.FileStartscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileStart.) |
| 395 | inspect = inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().([]*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}) |
| 396 | ) |
| 397 | |
| 398 | d := f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[0].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) |
| 399 | |
| 400 | format := func(pos tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) string { |
| 401 | off := tokFile.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Offset().(pos) |
| 402 | 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().("...%s<<>>%s...", src[off-1:off], src[off:off+1]) |
| 403 | } |
| 404 | |
| 405 | for _, test := range []struct { |
| 406 | name string |
| 407 | pos tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos# |
| 408 | want 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# |
| 409 | }{ |
| 410 | { |
| 411 | // "foo|(" Ident |
| 412 | pos: d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Openingscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Opening., |
| 413 | want: d.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name., |
| 414 | }, |
| 415 | { |
| 416 | // ")|{" FieldList |
| 417 | pos: d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Pos().(), |
| 418 | want: d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params., |
| 419 | }, |
| 420 | } { |
| 421 | cur, ok := inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().FindByPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindByPos().(test.pos, test.pos) |
| 422 | if !ok { |
| 423 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("FindByPos(%d) %s found nothing", test.pos, format(test.pos)) |
| 424 | } |
| 425 | |
| 426 | if cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() != test.want { |
| 427 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("FindByPos(%d) %s:\ngot %T (%v)\nwant %T (%v)", |
| 428 | test.pos, format(test.pos), |
| 429 | cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().(), cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().(), |
| 430 | test.want, test.want) |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Regression test for FuncDecl.Type irregularity in FindByPos (#75997). |
| 436 | func TestCursor_FindByPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestCursor_FindByPos().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 437 | // Observe that the range of FuncType has a hole between |
| 438 | // the "func" token and the start of Type.Params. |
| 439 | // The hole contains FuncDecl.{Recv,Name}. |
| 440 | // |
| 441 | // ~~~~~~~~~~~~FuncDecl~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 442 | // ~Recv~ ~Name~ |
| 443 | // ~~~~--------------~~~~FuncType~~~~~~ |
| 444 | // ~Params~ ~Results~ |
| 445 | const src = `package a; func (recv) method(params) (results) { body }` |
| 446 | var ( |
| 447 | fset = tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 448 | f, _ = parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "a.go", src, 0) // ignore parse errors |
| 449 | tokFile = fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.FileStartscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileStart.) |
| 450 | inspect = inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().([]*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}) |
| 451 | ) |
| 452 | format := func(start, end tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) string { |
| 453 | var ( |
| 454 | startOffset = tokFile.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Offset().(start) |
| 455 | endOffset = tokFile.Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Offset().(end) |
| 456 | ) |
| 457 | 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().("%s<<%s>>%s", src[:startOffset], src[startOffset:endOffset], src[endOffset:]) |
| 458 | } |
| 459 | |
| 460 | d := f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls.[0].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) |
| 461 | |
| 462 | // Each test case specifies a [pos-end) range for |
| 463 | // FindByPos and the syntax node it should find. |
| 464 | for _, test := range []struct { |
| 465 | start, end tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos# |
| 466 | want 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# |
| 467 | }{ |
| 468 | // pure subtrees |
| 469 | {d.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), d.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#End().(), d}, // decl |
| 470 | {d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().(), d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#End().(), d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv.}, // recv |
| 471 | {d.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), d.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#End().(), d.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name.}, // name |
| 472 | // (A FuncDecl can't have both Recv and TypeParams, so skip this one.) |
| 473 | // {d.Type.TypeParams.Pos(), d.Type.TypeParams.End(), d.Type.TypeParams}, |
| 474 | {d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#End().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params.}, // params |
| 475 | {d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#End().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results.}, // results |
| 476 | {d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Pos().(), d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#End().(), d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body.}, // body |
| 477 | |
| 478 | // single tokens |
| 479 | { |
| 480 | // "func" |
| 481 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Func., d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Func. + 4, |
| 482 | d, // arguably this should be d.Type |
| 483 | }, |
| 484 | { |
| 485 | // "(" FieldList |
| 486 | d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().(), d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().() + 1, |
| 487 | d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv., |
| 488 | }, |
| 489 | { |
| 490 | // "recv" Ident |
| 491 | d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Pos().(), d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Pos().() + 1, |
| 492 | d.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type., |
| 493 | }, |
| 494 | { |
| 495 | // "name" Ident |
| 496 | d.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().(), d.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Pos().() + 1, |
| 497 | d.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name., |
| 498 | }, |
| 499 | { |
| 500 | // "(" FieldList |
| 501 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().() + 1, |
| 502 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params., |
| 503 | }, |
| 504 | { |
| 505 | // "params" Ident |
| 506 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Pos().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Pos().() + 1, |
| 507 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type., |
| 508 | }, |
| 509 | { |
| 510 | // "(" FieldList |
| 511 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Pos().() + 1, |
| 512 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results., |
| 513 | }, |
| 514 | { |
| 515 | // "results" Ident |
| 516 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Pos().(), d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Pos().() + 1, |
| 517 | d.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type., |
| 518 | }, |
| 519 | { |
| 520 | // "{" BlockStmt |
| 521 | d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Pos().(), d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#Pos().() + 1, |
| 522 | d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body., |
| 523 | }, |
| 524 | { |
| 525 | // "body" Ident |
| 526 | d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() + 1, |
| 527 | d.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.[0].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ExprStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#).Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#X., |
| 528 | }, |
| 529 | } { |
| 530 | cur, ok := inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().FindByPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindByPos().(test.start, test.end) |
| 531 | if !ok || cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() == nil { |
| 532 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: FindByPos failed", format(test.start, test.end)) |
| 533 | continue |
| 534 | } |
| 535 | got := cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() |
| 536 | if got != test.want { |
| 537 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("FindByPos:\ninput:\t%s\ngot:\t%s (%T)\nwant:\t%s (%T)", |
| 538 | format(test.start, test.end), |
| 539 | format(got.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), got.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()), got, |
| 540 | format(test.want.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), test.want.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()), test.want) |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | func isscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/is().[T any](x any) bool { |
| 546 | _, ok := x.(T) |
| 547 | return ok |
| 548 | } |
| 549 | |
| 550 | // sliceTypes is a debugging helper that formats each slice element with %T. |
| 551 | func sliceTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/sliceTypes().[T any](slice []T) string { |
| 552 | var buf stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder# |
| 553 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().('[') |
| 554 | for i, elem := range slice { |
| 555 | if i > 0 { |
| 556 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().(' ') |
| 557 | } |
| 558 | 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", elem) |
| 559 | } |
| 560 | buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().(']') |
| 561 | return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#String().() |
| 562 | } |
| 563 | |
| 564 | func BenchmarkInspectCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/BenchmarkInspectCalls().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 565 | inspect := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect. |
| 566 | |
| 567 | // Measure marginal cost of traversal. |
| 568 | |
| 569 | callExprs := []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#{(*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#)(nil)} |
| 570 | |
| 571 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("Preorder", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 572 | var ncalls int |
| 573 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 574 | inspect.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Preorder().(callExprs, func(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#) { |
| 575 | _ = n.(*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#) |
| 576 | ncalls++ |
| 577 | }) |
| 578 | } |
| 579 | }) |
| 580 | |
| 581 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("WithStack", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 582 | var ncalls int |
| 583 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 584 | inspect.WithStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#WithStack().(callExprs, func(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#, push bool, stack []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#) (proceed bool) { |
| 585 | _ = n.(*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#) |
| 586 | if push { |
| 587 | ncalls++ |
| 588 | } |
| 589 | return true |
| 590 | }) |
| 591 | } |
| 592 | }) |
| 593 | |
| 594 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("Cursor", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 595 | var ncalls int |
| 596 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 597 | for cur := range inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(callExprs...) { |
| 598 | _ = cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*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#) |
| 599 | ncalls++ |
| 600 | } |
| 601 | } |
| 602 | }) |
| 603 | |
| 604 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("CursorEnclosing", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 605 | var ncalls int |
| 606 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 607 | for cur := range inspect.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().().Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(callExprs...) { |
| 608 | _ = cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*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#) |
| 609 | for range cur.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().() { |
| 610 | } |
| 611 | ncalls++ |
| 612 | } |
| 613 | } |
| 614 | }) |
| 615 | } |
| 616 | |
| 617 | // This benchmark compares methods for finding a known node in a tree. |
| 618 | func BenchmarkCursor_FindNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/BenchmarkCursor_FindNode().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 619 | root := netInspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netInspect..Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Root().() |
| 620 | |
| 621 | callExprs := []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#{(*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#)(nil)} |
| 622 | |
| 623 | // Choose a needle in the haystack to use as the search target: |
| 624 | // a CallExpr not too near the start nor at too shallow a depth. |
| 625 | var needle inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# |
| 626 | { |
| 627 | count := 0 |
| 628 | found := false |
| 629 | for c := range root.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(callExprs...) { |
| 630 | count++ |
| 631 | if count >= 1000 && iterlenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/iterlen().(c.Enclosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Enclosing().()) >= 6 { |
| 632 | needle = c |
| 633 | found = true |
| 634 | break |
| 635 | } |
| 636 | } |
| 637 | if !found { |
| 638 | b.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("can't choose needle") |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | b.ResetTimerscip-go gomod github.com/golang/go/src go1.25.0 testing/B#ResetTimer().() |
| 643 | |
| 644 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("Cursor.Preorder", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 645 | needleNode := needle.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() |
| 646 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 647 | var found inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Cursorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor# |
| 648 | for c := range root.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().(callExprs...) { |
| 649 | if c.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() == needleNode { |
| 650 | found = c |
| 651 | break |
| 652 | } |
| 653 | } |
| 654 | if found != needle { |
| 655 | b.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Preorder search failed: got %v, want %v", found, needle) |
| 656 | } |
| 657 | } |
| 658 | }) |
| 659 | |
| 660 | // This method is about 10-15% faster than Cursor.Preorder. |
| 661 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("Cursor.FindNode", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 662 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 663 | found, ok := root.FindNodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindNode().(needle.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().()) |
| 664 | if !ok || found != needle { |
| 665 | b.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("FindNode search failed: got %v, want %v", found, needle) |
| 666 | } |
| 667 | } |
| 668 | }) |
| 669 | |
| 670 | // This method is about 100x (!) faster than Cursor.Preorder. |
| 671 | b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("Cursor.FindByPos", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 672 | needleNode := needle.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() |
| 673 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 674 | found, ok := root.FindByPosscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#FindByPos().(needleNode.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), needleNode.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()) |
| 675 | if !ok || found != needle { |
| 676 | b.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("FindByPos search failed: got %v, want %v", found, needle) |
| 677 | } |
| 678 | } |
| 679 | }) |
| 680 | } |
| 681 | |
| 682 | func iterlenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/iterlen().[T any](seq iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[T]) (len int) { |
| 683 | for range seq { |
| 684 | len++ |
| 685 | } |
| 686 | return |
| 687 | } |