Skip to content

Commit a088e74

Browse files
authored
[analyzer] Fix [[clang::suppress]] for template instantiations (#168954)
BugSuppression works by traversing the lexical decl context of the decl-with-issue to record what source ranges should be suppressed by some attribute. Note that the decl-with-issue will be changed to the lexical decl context of the original decl-with-issue, to make suppression attributes work that were attached to the CXXRecordDecl containing the CXXMethodDecl (bug report's DeclWithIssue). It happens so that it uses a DynamicRecursiveASTVisitor, which has a couple of traversal options. Namely: - ShouldVisitTemplateInstantiations - ShouldWalkTypesOfTypeLocs - ShouldVisitImplicitCode - ShouldVisitLambdaBody By default, these have the correct values, except for ShouldVisitTemplateInstantiations. We should traverse template instantiations because that might be where the bug is reported - thus, where we might have a [[clang::suppress]] that we should honor. In this patch I'll explicitly set these traversal options to avoid further confusion. rdar://164646398
1 parent 08f72fe commit a088e74

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

clang/lib/StaticAnalyzer/Core/BugSuppression.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ class CacheInitializer : public DynamicRecursiveASTVisitor {
117117
}
118118
}
119119

120-
CacheInitializer(Ranges &R) : Result(R) {}
120+
CacheInitializer(Ranges &R) : Result(R) {
121+
ShouldVisitTemplateInstantiations = true;
122+
ShouldWalkTypesOfTypeLocs = false;
123+
ShouldVisitImplicitCode = false;
124+
ShouldVisitLambdaBody = true;
125+
}
121126
Ranges &Result;
122127
};
123128

clang/test/Analysis/suppression-attr.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1-
// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
2+
3+
void clang_analyzer_warnIfReached();
4+
5+
struct Clazz {
6+
template <typename T>
7+
static void templated_memfn();
8+
};
9+
10+
// This must come before the 'templated_memfn' is defined!
11+
static void instantiate() {
12+
Clazz::templated_memfn<int>();
13+
}
14+
15+
template <typename T>
16+
void Clazz::templated_memfn() {
17+
// When we report a bug in a function, we traverse the lexical decl context
18+
// of it while looking for suppression attributes to record what source
19+
// ranges should the suppression apply to.
20+
// In the past, that traversal didn't follow template instantiations, only
21+
// primary templates.
22+
[[clang::suppress]] clang_analyzer_warnIfReached(); // no-warning
23+
24+
}
225

326
namespace [[clang::suppress]]
427
suppressed_namespace {

0 commit comments

Comments
 (0)