Skip to content

Commit

Permalink
skip analysis when seeing generics
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chressie committed May 5, 2022
1 parent 2b1d76b commit 51c4ee7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion internal/pkg/earpointer/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/analysis/passes/usesgenerics"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/static"
"golang.org/x/tools/go/ssa"
Expand All @@ -49,7 +50,7 @@ var Analyzer = &analysis.Analyzer{
Flags: config.FlagSet,
Run: run,
ResultType: reflect.TypeOf(new(Partitions)),
Requires: []*analysis.Analyzer{buildssa.Analyzer},
Requires: []*analysis.Analyzer{buildssa.Analyzer, usesgenerics.Analyzer},
}

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

func run(pass *analysis.Pass) (interface{}, error) {
if r, ok := pass.ResultOf[usesgenerics.Analyzer].(*usesgenerics.Result); ok && r.Transitive != 0 {
// Generics are not supported yet (https://github.com/google/go-flow-levee/issues/323).
// TODO: Remove this check once the analyzers support generics.
return &Partitions{}, nil
}
conf, err := config.ReadConfig()
if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/fieldpropagator/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/google/go-flow-levee/internal/pkg/utils"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/analysis/passes/usesgenerics"
"golang.org/x/tools/go/ssa"
)

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

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

Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/source/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/go-flow-levee/internal/pkg/fieldtags"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/analysis/passes/usesgenerics"
"golang.org/x/tools/go/ssa"
)

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

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

0 comments on commit 51c4ee7

Please sign in to comment.