| 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 findcall defines an Analyzer that serves as a trivial |
| 6 | // example and test of the Analysis API. It reports a diagnostic for |
| 7 | // every call to a function or method of the name specified by its |
| 8 | // -name flag. It also exports a fact for each declaration that |
| 9 | // matches the name, plus a package-level fact if the package contained |
| 10 | // one or more such declarations. |
| 11 | package findcallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/ |
| 12 | |
| 13 | import ( |
| 14 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 15 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 16 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 17 | |
| 18 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 19 | ) |
| 20 | |
| 21 | const Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/Doc. = `find calls to a particular function |
| 22 | |
| 23 | The findcall analysis reports calls to functions or methods |
| 24 | of a particular name.` |
| 25 | |
| 26 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/Analyzer. = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 27 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "findcall", |
| 28 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/Doc., |
| 29 | URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/findcall", |
| 30 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/run()., |
| 31 | RunDespiteErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#RunDespiteErrors.: true, |
| 32 | FactTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#FactTypes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Factscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Fact#{new(foundFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact#)}, |
| 33 | } |
| 34 | |
| 35 | var namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/name. string // -name flag |
| 36 | |
| 37 | func initscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/init().() { |
| 38 | Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/Analyzer..Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Flags..StringVarscip-go gomod github.com/golang/go/src go1.25.0 flag/FlagSet#StringVar().(&namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/name., "name", namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/name., "name of the function to find") |
| 39 | } |
| 40 | |
| 41 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/run().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 42 | for _, f := range pass.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files. { |
| 43 | 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#) bool { |
| 44 | if call, ok := 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#); ok { |
| 45 | var id *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident# |
| 46 | switch fun := call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..(type) { |
| 47 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#: |
| 48 | id = fun |
| 49 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#: |
| 50 | id = fun.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel. |
| 51 | } |
| 52 | if id != nil && !pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[id].IsTypescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#IsType().() && id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/name. { |
| 53 | pass.Reportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Report.(analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#{ |
| 54 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: call.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Lparen., |
| 55 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: 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().("call of %s(...)", id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.), |
| 56 | SuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#SuggestedFixes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#{{ |
| 57 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: 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().("Add '_TEST_'"), |
| 58 | TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{{ |
| 59 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: call.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Lparen., |
| 60 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: call.Lparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Lparen., |
| 61 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte("_TEST_"), |
| 62 | }}, |
| 63 | }}, |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | return true |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | // Export a fact for each matching function. |
| 72 | // |
| 73 | // These facts are produced only to test the testing |
| 74 | // infrastructure in the analysistest package. |
| 75 | // They are not consumed by the findcall Analyzer |
| 76 | // itself, as would happen in a more realistic example. |
| 77 | for _, f := range pass.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files. { |
| 78 | for _, decl := range f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. { |
| 79 | if decl, ok := decl.(*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#); ok && decl.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/name. { |
| 80 | if obj, ok := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Defsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Defs.[decl.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name.].(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#); ok { |
| 81 | pass.ExportObjectFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ExportObjectFact.(obj, new(foundFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact#)) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if len(pass.AllObjectFactsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#AllObjectFacts.()) > 0 { |
| 88 | pass.ExportPackageFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ExportPackageFact.(new(foundFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact#)) |
| 89 | } |
| 90 | |
| 91 | return nil, nil |
| 92 | } |
| 93 | |
| 94 | // foundFact is a fact associated with functions that match -name. |
| 95 | // We use it to exercise the fact machinery in tests. |
| 96 | type foundFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact# struct{} |
| 97 | |
| 98 | func (*foundFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact#String().() string { return "found" } |
| 99 | func (*foundFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact#) AFactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/findcall`/foundFact#AFact().() {} |