Skip to content

Commit 51c4ee7

Browse files
committed
skip analysis when seeing generics
The analyzers (or helpers that the analyzers use) currently do not support generics. Use the `usesgenerics` analyzer from the x/tools repository to detect usage of generics and return a synthesized empty result in that case. Updates google/issues/323
1 parent 2b1d76b commit 51c4ee7

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

internal/pkg/earpointer/analysis.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"golang.org/x/tools/go/analysis"
2828
"golang.org/x/tools/go/analysis/passes/buildssa"
29+
"golang.org/x/tools/go/analysis/passes/usesgenerics"
2930
"golang.org/x/tools/go/callgraph"
3031
"golang.org/x/tools/go/callgraph/static"
3132
"golang.org/x/tools/go/ssa"
@@ -49,7 +50,7 @@ var Analyzer = &analysis.Analyzer{
4950
Flags: config.FlagSet,
5051
Run: run,
5152
ResultType: reflect.TypeOf(new(Partitions)),
52-
Requires: []*analysis.Analyzer{buildssa.Analyzer},
53+
Requires: []*analysis.Analyzer{buildssa.Analyzer, usesgenerics.Analyzer},
5354
}
5455

5556
// visitor traverse the instructions in a function and perform unifications
@@ -63,6 +64,11 @@ type visitor struct {
6364
}
6465

6566
func run(pass *analysis.Pass) (interface{}, error) {
67+
if r, ok := pass.ResultOf[usesgenerics.Analyzer].(*usesgenerics.Result); ok && r.Transitive != 0 {
68+
// Generics are not supported yet (https://github.com/google/go-flow-levee/issues/323).
69+
// TODO: Remove this check once the analyzers support generics.
70+
return &Partitions{}, nil
71+
}
6672
conf, err := config.ReadConfig()
6773
if err != nil {
6874
return nil, err

internal/pkg/fieldpropagator/analyzer.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/google/go-flow-levee/internal/pkg/utils"
2727
"golang.org/x/tools/go/analysis"
2828
"golang.org/x/tools/go/analysis/passes/buildssa"
29+
"golang.org/x/tools/go/analysis/passes/usesgenerics"
2930
"golang.org/x/tools/go/ssa"
3031
)
3132

@@ -53,12 +54,17 @@ var Analyzer = &analysis.Analyzer{
5354
A field propagator is a function that returns a value that is tainted by a source field.`,
5455
Flags: config.FlagSet,
5556
Run: run,
56-
Requires: []*analysis.Analyzer{buildssa.Analyzer, fieldtags.Analyzer},
57+
Requires: []*analysis.Analyzer{buildssa.Analyzer, fieldtags.Analyzer, usesgenerics.Analyzer},
5758
ResultType: reflect.TypeOf(new(ResultType)).Elem(),
5859
FactTypes: []analysis.Fact{new(isFieldPropagator)},
5960
}
6061

6162
func run(pass *analysis.Pass) (interface{}, error) {
63+
if r := pass.ResultOf[usesgenerics.Analyzer].(*usesgenerics.Result); r.Transitive != 0 {
64+
// Generics are not supported yet (https://github.com/google/go-flow-levee/issues/323).
65+
// TODO: Remove this check once the analyzers support generics.
66+
return ResultType{}, nil
67+
}
6268
taggedFields := pass.ResultOf[fieldtags.Analyzer].(fieldtags.ResultType)
6369
ssaInput := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
6470

internal/pkg/source/analyzer.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/google/go-flow-levee/internal/pkg/fieldtags"
2424
"golang.org/x/tools/go/analysis"
2525
"golang.org/x/tools/go/analysis/passes/buildssa"
26+
"golang.org/x/tools/go/analysis/passes/usesgenerics"
2627
"golang.org/x/tools/go/ssa"
2728
)
2829

@@ -33,11 +34,16 @@ var Analyzer = &analysis.Analyzer{
3334
Doc: "This analyzer identifies ssa.Values that are sources.",
3435
Flags: config.FlagSet,
3536
Run: run,
36-
Requires: []*analysis.Analyzer{buildssa.Analyzer, fieldtags.Analyzer, fieldpropagator.Analyzer},
37+
Requires: []*analysis.Analyzer{buildssa.Analyzer, fieldtags.Analyzer, fieldpropagator.Analyzer, usesgenerics.Analyzer},
3738
ResultType: reflect.TypeOf(new(ResultType)).Elem(),
3839
}
3940

4041
func run(pass *analysis.Pass) (interface{}, error) {
42+
if r := pass.ResultOf[usesgenerics.Analyzer].(*usesgenerics.Result); r.Transitive != 0 {
43+
// Generics are not supported yet (https://github.com/google/go-flow-levee/issues/323).
44+
// TODO: Remove this check once the analyzers support generics.
45+
return map[*ssa.Function][]*Source{}, nil
46+
}
4147
ssaInput := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)
4248
taggedFields := pass.ResultOf[fieldtags.Analyzer].(fieldtags.ResultType)
4349
fieldPropagators := pass.ResultOf[fieldpropagator.Analyzer].(fieldpropagator.ResultType)

0 commit comments

Comments
 (0)