aboutsummaryrefslogtreecommitdiff
path: root/go/analysis/passes/pkgfact/pkgfact.go
blob: 2262fc4f13dfbaef5cb6a4c3ba33d7a92e22f4d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// The pkgfact package is a demonstration and test of the package fact
// mechanism.
//
// The output of the pkgfact analysis is a set of key/values pairs
// gathered from the analyzed package and its imported dependencies.
// Each key/value pair comes from a top-level constant declaration
// whose name starts and ends with "_".  For example:
//
//      package p
//
// 	const _greeting_  = "hello"
// 	const _audience_  = "world"
//
// the pkgfact analysis output for package p would be:
//
//   {"greeting": "hello", "audience": "world"}.
//
// In addition, the analysis reports a diagnostic at each import
// showing which key/value pairs it contributes.
package pkgfact

import (
	"fmt"
	"go/ast"
	"go/token"
	"go/types"
	"reflect"
	"sort"
	"strings"

	"golang.org/x/tools/go/analysis"
)

var Analyzer = &analysis.Analyzer{
	Name:       "pkgfact",
	Doc:        "gather name/value pairs from constant declarations",
	Run:        run,
	FactTypes:  []analysis.Fact{new(pairsFact)},
	ResultType: reflect.TypeOf(map[string]string{}),
}

// A pairsFact is a package-level fact that records
// an set of key=value strings accumulated from constant
// declarations in this package and its dependencies.
// Elements are ordered by keys, which are unique.
type pairsFact []string

func (f *pairsFact) AFact()         {}
func (f *pairsFact) String() string { return "pairs(" + strings.Join(*f, ", ") + ")" }

func run(pass *analysis.Pass) (interface{}, error) {
	result := make(map[string]string)

	// At each import, print the fact from the imported
	// package and accumulate its information into the result.
	// (Warning: accumulation leads to quadratic growth of work.)
	doImport := func(spec *ast.ImportSpec) {
		pkg := imported(pass.TypesInfo, spec)
		var fact pairsFact
		if pass.ImportPackageFact(pkg, &fact) {
			for _, pair := range fact {
				eq := strings.IndexByte(pair, '=')
				result[pair[:eq]] = pair[1+eq:]
			}
			pass.ReportRangef(spec, "%s", strings.Join(fact, " "))
		}
	}

	// At each "const _name_ = value", add a fact into env.
	doConst := func(spec *ast.ValueSpec) {
		if len(spec.Names) == len(spec.Values) {
			for i := range spec.Names {
				name := spec.Names[i].Name
				if strings.HasPrefix(name, "_") && strings.HasSuffix(name, "_") {

					if key := strings.Trim(name, "_"); key != "" {
						value := pass.TypesInfo.Types[spec.Values[i]].Value.String()
						result[key] = value
					}
				}
			}
		}
	}

	for _, f := range pass.Files {
		for _, decl := range f.Decls {
			if decl, ok := decl.(*ast.GenDecl); ok {
				for _, spec := range decl.Specs {
					switch decl.Tok {
					case token.IMPORT:
						doImport(spec.(*ast.ImportSpec))
					case token.CONST:
						doConst(spec.(*ast.ValueSpec))
					}
				}
			}
		}
	}

	// Sort/deduplicate the result and save it as a package fact.
	keys := make([]string, 0, len(result))
	for key := range result {
		keys = append(keys, key)
	}
	sort.Strings(keys)
	var fact pairsFact
	for _, key := range keys {
		fact = append(fact, fmt.Sprintf("%s=%s", key, result[key]))
	}
	if len(fact) > 0 {
		pass.ExportPackageFact(&fact)
	}

	return result, nil
}

func imported(info *types.Info, spec *ast.ImportSpec) *types.Package {
	obj, ok := info.Implicits[spec]
	if !ok {
		obj = info.Defs[spec.Name] // renaming import
	}
	return obj.(*types.PkgName).Imported()
}