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.
5 // The pkgfact package is a demonstration and test of the package fact
8 // The output of the pkgfact analysis is a set of key/values pairs
9 // gathered from the analyzed package and its imported dependencies.
10 // Each key/value pair comes from a top-level constant declaration
11 // whose name starts and ends with "_". For example:
15 // const _greeting_ = "hello"
16 // const _audience_ = "world"
18 // the pkgfact analysis output for package p would be:
20 // {"greeting": "hello", "audience": "world"}.
22 // In addition, the analysis reports a diagnostic at each import
23 // showing which key/value pairs it contributes.
35 "golang.org/x/tools/go/analysis"
38 var Analyzer = &analysis.Analyzer{
40 Doc: "gather name/value pairs from constant declarations",
42 FactTypes: []analysis.Fact{new(pairsFact)},
43 ResultType: reflect.TypeOf(map[string]string{}),
46 // A pairsFact is a package-level fact that records
47 // an set of key=value strings accumulated from constant
48 // declarations in this package and its dependencies.
49 // Elements are ordered by keys, which are unique.
50 type pairsFact []string
52 func (f *pairsFact) AFact() {}
53 func (f *pairsFact) String() string { return "pairs(" + strings.Join(*f, ", ") + ")" }
55 func run(pass *analysis.Pass) (interface{}, error) {
56 result := make(map[string]string)
58 // At each import, print the fact from the imported
59 // package and accumulate its information into the result.
60 // (Warning: accumulation leads to quadratic growth of work.)
61 doImport := func(spec *ast.ImportSpec) {
62 pkg := imported(pass.TypesInfo, spec)
64 if pass.ImportPackageFact(pkg, &fact) {
65 for _, pair := range fact {
66 eq := strings.IndexByte(pair, '=')
67 result[pair[:eq]] = pair[1+eq:]
69 pass.ReportRangef(spec, "%s", strings.Join(fact, " "))
73 // At each "const _name_ = value", add a fact into env.
74 doConst := func(spec *ast.ValueSpec) {
75 if len(spec.Names) == len(spec.Values) {
76 for i := range spec.Names {
77 name := spec.Names[i].Name
78 if strings.HasPrefix(name, "_") && strings.HasSuffix(name, "_") {
80 if key := strings.Trim(name, "_"); key != "" {
81 value := pass.TypesInfo.Types[spec.Values[i]].Value.String()
89 for _, f := range pass.Files {
90 for _, decl := range f.Decls {
91 if decl, ok := decl.(*ast.GenDecl); ok {
92 for _, spec := range decl.Specs {
95 doImport(spec.(*ast.ImportSpec))
97 doConst(spec.(*ast.ValueSpec))
104 // Sort/deduplicate the result and save it as a package fact.
105 keys := make([]string, 0, len(result))
106 for key := range result {
107 keys = append(keys, key)
111 for _, key := range keys {
112 fact = append(fact, fmt.Sprintf("%s=%s", key, result[key]))
115 pass.ExportPackageFact(&fact)
121 func imported(info *types.Info, spec *ast.ImportSpec) *types.Package {
122 obj, ok := info.Implicits[spec]
124 obj = info.Defs[spec.Name] // renaming import
126 return obj.(*types.PkgName).Imported()