| 1 |
// Copyright 2024 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 |
//go:build !wasm |
| 6 |
|
| 7 |
// The example command demonstrates a simple go/packages-based |
| 8 |
// analysis driver program. |
| 9 |
package checker_test |
| 10 |
|
| 11 |
import ( |
| 12 |
"fmt" |
| 13 |
"log" |
| 14 |
"maps" |
| 15 |
"os" |
| 16 |
|
| 17 |
"golang.org/x/tools/go/analysis" |
| 18 |
"golang.org/x/tools/go/analysis/checker" |
| 19 |
"golang.org/x/tools/go/packages" |
| 20 |
"golang.org/x/tools/txtar" |
| 21 |
) |
| 22 |
|
| 23 |
const testdata = ` |
| 24 |
-- go.mod -- |
| 25 |
module example.com |
| 26 |
go 1.21 |
| 27 |
|
| 28 |
-- a/a.go -- |
| 29 |
package a |
| 30 |
|
| 31 |
import _ "example.com/b" |
| 32 |
import _ "example.com/c" |
| 33 |
|
| 34 |
func A1() |
| 35 |
func A2() |
| 36 |
func A3() |
| 37 |
|
| 38 |
-- b/b.go -- |
| 39 |
package b |
| 40 |
|
| 41 |
func B1() |
| 42 |
func B2() |
| 43 |
|
| 44 |
-- c/c.go -- |
| 45 |
package c |
| 46 |
|
| 47 |
import _ "example.com/d" |
| 48 |
|
| 49 |
func C1() |
| 50 |
|
| 51 |
-- d/d.go -- |
| 52 |
package d |
| 53 |
|
| 54 |
func D1() |
| 55 |
func D2() |
| 56 |
func D3() |
| 57 |
func D4() |
| 58 |
` |
| 59 |
|
| 60 |
func Example() { |
| 61 |
// Extract a tree of Go source files. |
| 62 |
// (Avoid the standard library as it is always evolving.) |
| 63 |
dir, err := os.MkdirTemp("", "") |
| 64 |
if err != nil { |
| 65 |
log.Fatal(err) |
| 66 |
} |
| 67 |
fs, err := txtar.FS(txtar.Parse([]byte(testdata))) |
| 68 |
if err != nil { |
| 69 |
log.Fatal(err) |
| 70 |
} |
| 71 |
if err := os.CopyFS(dir, fs); err != nil { |
| 72 |
log.Fatal(err) |
| 73 |
} |
| 74 |
|
| 75 |
// Load packages: example.com/a + dependencies |
| 76 |
// |
| •77 |
cfg := &packages.Config{Mode: packages.LoadAllSyntax, Dir: dir, Env: []string{"GO111MODULE=on", "GOCACHE=/tmp"}} |
| 78 |
initial, err := packages.Load(cfg, "example.com/a") |
| 79 |
if err != nil { |
| 80 |
log.Fatal(err) // failure to enumerate packages |
| 81 |
} |
| 82 |
|
| 83 |
// There may be parse or type errors among the |
| 84 |
// initial packages or their dependencies, |
| 85 |
// but the analysis driver can handle faulty inputs, |
| 86 |
// as can some analyzers. |
| 87 |
packages.PrintErrors(initial) |
| 88 |
|
| 89 |
if len(initial) == 0 { |
| 90 |
log.Fatalf("no initial packages") |
| 91 |
} |
| 92 |
|
| 93 |
// Run analyzers (just one) on example.com packages. |
| 94 |
analyzers := []*analysis.Analyzer{pkgdecls} |
| 95 |
graph, err := checker.Analyze(analyzers, initial, nil) |
| 96 |
if err != nil { |
| 97 |
log.Fatal(err) |
| 98 |
} |
| 99 |
|
| 100 |
// Inspect the result of each analysis action, |
| 101 |
// including those for all dependencies. |
| 102 |
// |
| 103 |
// A realistic client would use Result, Err, Diagnostics, |
| 104 |
// but for test stability, we just print the action string |
| 105 |
// ("analyzer@package"). |
| 106 |
for act := range graph.All() { |
| 107 |
fmt.Println("printing", act) |
| 108 |
} |
| 109 |
|
| 110 |
// Print the package fact for the sole initial package. |
| 111 |
root := graph.Roots[0] |
| 112 |
fact := new(pkgdeclsFact) |
| 113 |
if root.PackageFact(root.Package.Types, fact) { |
| 114 |
for k, v := range fact.numdecls { |
| 115 |
fmt.Printf("%s:\t%d decls\n", k, v) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Unordered Output: |
| 120 |
// printing [email protected]/a |
| 121 |
// printing [email protected]/b |
| 122 |
// printing [email protected]/c |
| 123 |
// printing [email protected]/d |
| 124 |
// example.com/a: 3 decls |
| 125 |
// example.com/b: 2 decls |
| 126 |
// example.com/c: 1 decls |
| 127 |
// example.com/d: 4 decls |
| 128 |
} |
| 129 |
|
| 130 |
// pkgdecls is a trivial example analyzer that uses package facts to |
| 131 |
// compute information from the entire dependency graph. |
| 132 |
var pkgdecls = &analysis.Analyzer{ |
| 133 |
Name: "pkgdecls", |
| 134 |
Doc: "Computes a package fact mapping each package to its number of declarations.", |
| 135 |
Run: run, |
| 136 |
FactTypes: []analysis.Fact{(*pkgdeclsFact)(nil)}, |
| 137 |
} |
| 138 |
|
| 139 |
type pkgdeclsFact struct{ numdecls map[string]int } |
| 140 |
|
| 141 |
func (*pkgdeclsFact) AFact() {} |
| 142 |
|
| 143 |
func run(pass *analysis.Pass) (any, error) { |
| 144 |
numdecls := map[string]int{ |
| 145 |
pass.Pkg.Path(): pass.Pkg.Scope().Len(), |
| 146 |
} |
| 147 |
|
| 148 |
// Compute the union across all dependencies. |
| 149 |
for _, imp := range pass.Pkg.Imports() { |
| 150 |
if depFact := new(pkgdeclsFact); pass.ImportPackageFact(imp, depFact) { |
| 151 |
maps.Copy(numdecls, depFact.numdecls) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
pass.ExportPackageFact(&pkgdeclsFact{numdecls}) |
| 156 |
|
| 157 |
return nil, nil |
| 158 |
} |