-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow report suppression via comments (#283)
Reports can be suppressed using a code comment containing the string "levee.DoNotReport" at the beginning of a line in the comment. The comment has to be associated with the call, not with one of the arguments.
- Loading branch information
mlevesquedion
authored
Feb 17, 2021
1 parent
a278ac7
commit a885620
Showing
9 changed files
with
369 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
internal/pkg/levee/testdata/src/levee_analysistest/example/suppression/tests.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package suppression contains tests for false positive suppression. | ||
package suppression | ||
|
||
import ( | ||
"fmt" | ||
"levee_analysistest/example/core" | ||
) | ||
|
||
func TestNotSuppressed(s core.Source) { | ||
core.Sink(s) // want "a source has reached a sink" | ||
} | ||
|
||
func TestOnelineLineComment(s core.Source) { | ||
// levee.DoNotReport | ||
core.Sink(s) | ||
} | ||
|
||
func TestOnelineGeneralComment(s core.Source) { | ||
/* levee.DoNotReport */ | ||
core.Sink(s) | ||
} | ||
|
||
func TestInlineLineComment(s core.Source) { | ||
core.Sink(s) // levee.DoNotReport | ||
} | ||
|
||
func TestInlineGeneralComment(s core.Source) { | ||
core.Sink(s) /* levee.DoNotReport */ | ||
} | ||
|
||
func TestMultilineLineComment(s core.Source) { | ||
// Line 1 | ||
// levee.DoNotReport | ||
// Line 3 | ||
core.Sink(s) | ||
} | ||
|
||
func TestMultilineGeneralComment(s core.Source) { | ||
/* | ||
Line 1 | ||
levee.DoNotReport | ||
Line 3 | ||
*/ | ||
core.Sink(s) | ||
} | ||
|
||
func TestAdjacentReports(s core.Source) { | ||
core.Sink(s) // levee.DoNotReport | ||
core.Sink(s) // want "a source has reached a sink" | ||
} | ||
|
||
func TestReportsSeparatedByLineComment(s core.Source) { | ||
core.Sink(s) // want "a source has reached a sink" | ||
// levee.DoNotReport | ||
core.Sink(s) | ||
} | ||
|
||
func TestReportsSeparatedByGeneralComment(s core.Source) { | ||
core.Sink(s) /* | ||
levee.DoNotReport | ||
*/ | ||
core.Sink(s) // want "a source has reached a sink" | ||
} | ||
|
||
func TestLineCommentBeforeGeneralComment(s core.Source) { | ||
// levee.DoNotReport | ||
/* | ||
The line comment above and this comment | ||
are part of the same comment group. | ||
*/ | ||
core.Sink(s) | ||
} | ||
|
||
func TestSuppressPanic(s core.Source) { | ||
// levee.DoNotReport | ||
panic(s) | ||
panic( // levee.DoNotReport | ||
s, | ||
) | ||
} | ||
|
||
func TestSuppressMultilineCall(s core.Source) { | ||
// levee.DoNotReport | ||
core.Sink( | ||
"arg 1", | ||
s, | ||
"arg 3", | ||
) | ||
|
||
core.Sink( // levee.DoNotReport | ||
"arg 1", | ||
s, | ||
"arg 3") | ||
|
||
core.Sink("arg 1", | ||
s, | ||
) // levee.DoNotReport | ||
|
||
core.Sink( | ||
"arg 1", | ||
s) // levee.DoNotReport | ||
} | ||
|
||
func TestIncorrectSuppressionViaArgument(s core.Source) { | ||
core.Sink( // want "a source has reached a sink" | ||
"arg 1", | ||
s, // levee.DoNotReport | ||
) | ||
|
||
core.Sink("arg1", // levee.DoNotReport // want "a source has reached a sink" | ||
s, | ||
) | ||
} | ||
|
||
func TestSuppressNestedCall(s core.Source) { | ||
fmt.Println( | ||
// levee.DoNotReport | ||
core.SinkAndReturn(s), | ||
) | ||
|
||
// TODO(#284): we don't actually want a report here | ||
fmt.Println( | ||
core.SinkAndReturn(s), // levee.DoNotReport // want "a source has reached a sink" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package suppression defines an analyzer that identifies calls | ||
// suppressed by a comment. | ||
package suppression | ||
|
||
import ( | ||
"go/ast" | ||
"reflect" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
const suppressionString = "levee.DoNotReport" | ||
|
||
// ResultType is a set of nodes that are suppressed by a comment. | ||
type ResultType map[ast.Node]bool | ||
|
||
// IsSuppressed determines whether the given node is suppressed. | ||
func (rt ResultType) IsSuppressed(n ast.Node) bool { | ||
_, ok := rt[n] | ||
return ok | ||
} | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: "suppression", | ||
Doc: "This analyzer identifies ast nodes that are suppressed by comments.", | ||
Run: run, | ||
ResultType: reflect.TypeOf(new(ResultType)).Elem(), | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
result := map[ast.Node]bool{} | ||
|
||
for _, f := range pass.Files { | ||
for node, commentGroups := range ast.NewCommentMap(pass.Fset, f, f.Comments) { | ||
for _, cg := range commentGroups { | ||
if isSuppressingCommentGroup(cg) { | ||
result[node] = true | ||
pass.Reportf(node.Pos(), "suppressed") | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ResultType(result), nil | ||
} | ||
|
||
// isSuppressingCommentGroup determines whether a comment group contains | ||
// the suppression string at the beginning of a line in the comment text. | ||
func isSuppressingCommentGroup(commentGroup *ast.CommentGroup) bool { | ||
for _, line := range strings.Split(commentGroup.Text(), "\n") { | ||
trimmed := strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(line, "//"), "/*")) | ||
if strings.HasPrefix(trimmed, suppressionString) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.