...
1
2
3
4
5
6
7 package usesgenerics
8
9 import (
10 "reflect"
11
12 "golang.org/x/tools/go/analysis"
13 "golang.org/x/tools/go/analysis/passes/inspect"
14 "golang.org/x/tools/go/ast/inspector"
15 "golang.org/x/tools/internal/typeparams/genericfeatures"
16 )
17
18 var Analyzer = &analysis.Analyzer{
19 Name: "usesgenerics",
20 Doc: Doc,
21 Requires: []*analysis.Analyzer{inspect.Analyzer},
22 Run: run,
23 ResultType: reflect.TypeOf((*Result)(nil)),
24 FactTypes: []analysis.Fact{new(featuresFact)},
25 }
26
27 const Doc = `detect whether a package uses generics features
28
29 The usesgenerics analysis reports whether a package directly or transitively
30 uses certain features associated with generic programming in Go.`
31
32 type Features = genericfeatures.Features
33
34 const (
35 GenericTypeDecls = genericfeatures.GenericTypeDecls
36 GenericFuncDecls = genericfeatures.GenericFuncDecls
37 EmbeddedTypeSets = genericfeatures.EmbeddedTypeSets
38 TypeInstantiation = genericfeatures.TypeInstantiation
39 FuncInstantiation = genericfeatures.FuncInstantiation
40 )
41
42
43
44
45
46 type Result struct {
47 Direct, Transitive Features
48 }
49
50 type featuresFact struct {
51 Features Features
52 }
53
54 func (f *featuresFact) AFact() {}
55 func (f *featuresFact) String() string { return f.Features.String() }
56
57 func run(pass *analysis.Pass) (interface{}, error) {
58 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
59
60 direct := genericfeatures.ForPackage(inspect, pass.TypesInfo)
61
62 transitive := direct | importedTransitiveFeatures(pass)
63 if transitive != 0 {
64 pass.ExportPackageFact(&featuresFact{transitive})
65 }
66
67 return &Result{
68 Direct: direct,
69 Transitive: transitive,
70 }, nil
71 }
72
73
74
75 func importedTransitiveFeatures(pass *analysis.Pass) Features {
76 var feats Features
77 for _, imp := range pass.Pkg.Imports() {
78 var importedFact featuresFact
79 if pass.ImportPackageFact(imp, &importedFact) {
80 feats |= importedFact.Features
81 }
82 }
83 return feats
84 }
85
View as plain text