-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[attributes][analyzer] Generalize [[clang::suppress]] to declarations. #80371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[attributes][analyzer] Generalize [[clang::suppress]] to declarations. #80371
Conversation
The attribute is now allowed on an assortment of declarations, to suppress warnings related to declarations themselves, or all warnings in the lexical scope of the declaration. I don't necessarily see a reason to have a list at all, but it does look as if some of those more niche items aren't properly supported by the compiler itself so let's maintain a short safe list for now.
@llvm/pr-subscribers-clang-static-analyzer-1 @llvm/pr-subscribers-clang Author: Artem Dergachev (haoNoQ) ChangesThe attribute is now allowed on an assortment of declarations, to suppress warnings related to declarations themselves, or all warnings in the lexical scope of the declaration. I don't necessarily see a reason to have a list at all, but it does look as if some of those more niche items aren't properly supported by the compiler itself so let's maintain a short safe list for now. The initial implementation raised a question whether the attribute should apply to lexical declaration context vs. "actual" declaration context. I'm using "lexical" here because it results in less warnings suppressed, which is the conservative behavior: we can always expand it later if we think this is wrong, without breaking any existing code. I also think that this is the correct behavior that we will probably never want to change, given that the user typically desires to keep the suppressions as localized as possible. Patch is 21.09 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/80371.diff 18 Files Affected:
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 58838b01b4fd7..1b37b01ba6a3f 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -2891,6 +2891,13 @@ def Suppress : DeclOrStmtAttr {
let Spellings = [CXX11<"gsl", "suppress">, Clang<"suppress">];
let Args = [VariadicStringArgument<"DiagnosticIdentifiers">];
let Accessors = [Accessor<"isGSL", [CXX11<"gsl", "suppress">]>];
+ // There's no fundamental reason why we can't simply accept all Decls
+ // but let's make a short list so that to avoid supporting something weird
+ // by accident. We can always expand the list later.
+ let Subjects = SubjectList<[
+ Stmt, Var, Field, ObjCProperty, Function, ObjCMethod, Record, ObjCInterface,
+ ObjCImplementation, Namespace, Empty
+ ], ErrorDiag, "variables, functions, structs, interfaces, and namespaces">;
let Documentation = [SuppressDocs];
}
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index e02a1201e2ad7..a98d4b1f8d84d 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -5313,6 +5313,29 @@ Putting the attribute on a compound statement suppresses all warnings in scope:
}
}
+The attribute can also be placed on entire declarations of functions, classes,
+variables, member variables, and so on, to suppress warnings related
+to the declarations themselves. When used this way, the attribute additionally
+suppresses all warnings in the lexical scope of the declaration:
+
+.. code-block:: c++
+
+ class [[clang::suppress]] C {
+ int foo() {
+ int *x = nullptr;
+ ...
+ return *x; // warnings suppressed in the entire class scope
+ }
+
+ int bar();
+ };
+
+ int C::bar() {
+ int *x = nullptr;
+ ...
+ return *x; // warning NOT suppressed! - not lexically nested in 'class C{}'
+ }
+
Some static analysis warnings are accompanied by one or more notes, and the
line of code against which the warning is emitted isn't necessarily the best
for suppression purposes. In such cases the tools are allowed to implement
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index fd1c47008d685..ca36d64cb077a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -2960,6 +2960,9 @@ static bool mergeDeclAttribute(Sema &S, NamedDecl *D,
S.mergeHLSLNumThreadsAttr(D, *NT, NT->getX(), NT->getY(), NT->getZ());
else if (const auto *SA = dyn_cast<HLSLShaderAttr>(Attr))
NewAttr = S.mergeHLSLShaderAttr(D, *SA, SA->getType());
+ else if (const auto *SupA = dyn_cast<SuppressAttr>(Attr))
+ // Do nothing. Each redeclaration should be suppressed separately.
+ NewAttr = nullptr;
else if (Attr->shouldInheritEvenIfAlreadyPresent() || !DeclHasAttr(D, Attr))
NewAttr = cast<InheritableAttr>(Attr->clone(S.Context));
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 069571fcf7864..3291ad732e98d 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -5245,11 +5245,6 @@ static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
// Suppression attribute with GSL spelling requires at least 1 argument.
if (!AL.checkAtLeastNumArgs(S, 1))
return;
- } else if (!isa<VarDecl>(D)) {
- // Analyzer suppression applies only to variables and statements.
- S.Diag(AL.getLoc(), diag::err_attribute_wrong_decl_type_str)
- << AL << 0 << "variables and statements";
- return;
}
std::vector<StringRef> DiagnosticIdentifiers;
diff --git a/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
index 1c2d84254d464..4f8750d9f1966 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
@@ -161,7 +161,7 @@ static void checkObjCUnusedIvar(const ObjCImplementationDecl *D,
PathDiagnosticLocation L =
PathDiagnosticLocation::create(Ivar, BR.getSourceManager());
- BR.EmitBasicReport(D, Checker, "Unused instance variable", "Optimization",
+ BR.EmitBasicReport(ID, Checker, "Unused instance variable", "Optimization",
os.str(), L);
}
}
diff --git a/clang/lib/StaticAnalyzer/Core/BugSuppression.cpp b/clang/lib/StaticAnalyzer/Core/BugSuppression.cpp
index fded071567f95..84004b8e5c1cd 100644
--- a/clang/lib/StaticAnalyzer/Core/BugSuppression.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugSuppression.cpp
@@ -82,12 +82,12 @@ class CacheInitializer : public RecursiveASTVisitor<CacheInitializer> {
CacheInitializer(ToInit).TraverseDecl(const_cast<Decl *>(D));
}
- bool VisitVarDecl(VarDecl *VD) {
+ bool VisitDecl(Decl *D) {
// Bug location could be somewhere in the init value of
// a freshly declared variable. Even though it looks like the
// user applied attribute to a statement, it will apply to a
// variable declaration, and this is where we check for it.
- return VisitAttributedNode(VD);
+ return VisitAttributedNode(D);
}
bool VisitAttributedStmt(AttributedStmt *AS) {
@@ -147,6 +147,20 @@ bool BugSuppression::isSuppressed(const PathDiagnosticLocation &Location,
// done as well as perform a lot of work we'll never need.
// Gladly, none of our on-by-default checkers currently need it.
DeclWithIssue = ACtx.getTranslationUnitDecl();
+ } else {
+ // This is the fast path. However, we should still consider the topmost
+ // declaration that isn't TranslationUnitDecl, because we should respect
+ // attributes on the entire declaration chain.
+ while (true) {
+ // Use the "lexical" parent. Eg., if the attribute is on a class, suppress
+ // warnings in inline methods but not in out-of-line methods.
+ const Decl *Parent =
+ dyn_cast_or_null<Decl>(DeclWithIssue->getLexicalDeclContext());
+ if (Parent == nullptr || isa<TranslationUnitDecl>(Parent))
+ break;
+
+ DeclWithIssue = Parent;
+ }
}
// While some warnings are attached to AST nodes (mostly path-sensitive
diff --git a/clang/test/Analysis/Checkers/WebKit/ref-cntbl-base-virtual-dtor.cpp b/clang/test/Analysis/Checkers/WebKit/ref-cntbl-base-virtual-dtor.cpp
index 1fc59c108b0e8..5cf7e7614d06e 100644
--- a/clang/test/Analysis/Checkers/WebKit/ref-cntbl-base-virtual-dtor.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/ref-cntbl-base-virtual-dtor.cpp
@@ -13,7 +13,17 @@ struct DerivedWithVirtualDtor : RefCntblBase {
virtual ~DerivedWithVirtualDtor() {}
};
+// Confirm that the checker respects [[clang::suppress]]
+struct [[clang::suppress]] SuppressedDerived : RefCntblBase { };
+struct [[clang::suppress]] SuppressedDerivedWithVirtualDtor : RefCntblBase {
+ virtual ~SuppressedDerivedWithVirtualDtor() {}
+};
+// FIXME: Support attributes on base specifiers? Currently clang
+// doesn't support such attributes at all, even though it knows
+// how to parse them.
+//
+// struct SuppressedBaseSpecDerived : [[clang::suppress]] RefCntblBase { };
template<class T>
struct DerivedClassTmpl : T { };
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
index 30798793ceab1..27e0a74d583cd 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
@@ -15,6 +15,11 @@ void raw_ptr() {
// CHECK-NEXT:{{^ | }} ^
auto foo4 = [=](){ (void) ref_countable; };
// CHECK: warning: Implicitly captured raw-pointer 'ref_countable' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]
+
+ // Confirm that the checker respects [[clang::suppress]].
+ RefCountable* suppressed_ref_countable = nullptr;
+ [[clang::suppress]] auto foo5 = [suppressed_ref_countable](){};
+ // CHECK-NOT: warning: Captured raw-pointer 'suppressed_ref_countable' to uncounted type is unsafe [webkit.UncountedLambdaCapturesChecker]
}
void references() {
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
index 8694d5fb85b8b..0fcd3b21376ca 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
@@ -60,6 +60,7 @@ class Foo {
// expected-warning@-1{{Local variable 'baz' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
auto *baz2 = this->provide_ref_ctnbl();
// expected-warning@-1{{Local variable 'baz2' is uncounted and unsafe [alpha.webkit.UncountedLocalVarsChecker]}}
+ [[clang::suppress]] auto *baz_suppressed = provide_ref_ctnbl(); // no-warning
}
};
} // namespace auto_keyword
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp
index a0ea61e0e2a13..108d5effdd2e8 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp
@@ -8,6 +8,9 @@ namespace members {
RefCountable* a = nullptr;
// expected-warning@-1{{Member variable 'a' in 'members::Foo' is a raw pointer to ref-countable type 'RefCountable'}}
+ [[clang::suppress]]
+ RefCountable* a_suppressed = nullptr;
+
protected:
RefPtr<RefCountable> b;
@@ -25,8 +28,14 @@ namespace members {
};
void forceTmplToInstantiate(FooTmpl<RefCountable>) {}
+
+ struct [[clang::suppress]] FooSuppressed {
+ private:
+ RefCountable* a = nullptr;
+ };
}
+
namespace ignore_unions {
union Foo {
RefCountable* a;
diff --git a/clang/test/Analysis/ObjCRetSigs.m b/clang/test/Analysis/ObjCRetSigs.m
index 97d33f9f5467b..f92506a834195 100644
--- a/clang/test/Analysis/ObjCRetSigs.m
+++ b/clang/test/Analysis/ObjCRetSigs.m
@@ -4,10 +4,12 @@
@interface MyBase
-(long long)length;
+-(long long)suppressedLength;
@end
@interface MySub : MyBase{}
-(double)length;
+-(double)suppressedLength;
@end
@implementation MyBase
@@ -15,6 +17,10 @@ -(long long)length{
printf("Called MyBase -length;\n");
return 3;
}
+-(long long)suppressedLength{
+ printf("Called MyBase -length;\n");
+ return 3;
+}
@end
@implementation MySub
@@ -22,4 +28,8 @@ -(double)length{ // expected-warning{{types are incompatible}}
printf("Called MySub -length;\n");
return 3.3;
}
+-(double)suppressedLength [[clang::suppress]]{ // no-warning
+ printf("Called MySub -length;\n");
+ return 3.3;
+}
@end
diff --git a/clang/test/Analysis/objc_invalidation.m b/clang/test/Analysis/objc_invalidation.m
index 52a79d8f34baa..e61b0897646a2 100644
--- a/clang/test/Analysis/objc_invalidation.m
+++ b/clang/test/Analysis/objc_invalidation.m
@@ -257,6 +257,17 @@ @interface MissingInvalidationMethod : Foo <FooBar_Protocol>
@implementation MissingInvalidationMethod
@end
+@interface SuppressedMissingInvalidationMethod : Foo <FooBar_Protocol>
+@property (assign) [[clang::suppress]] SuppressedMissingInvalidationMethod *foobar16_warn;
+// FIXME: Suppression should have worked but decl-with-issue is the ivar, not the property.
+#if RUN_IVAR_INVALIDATION
+// expected-warning@-3 {{Property foobar16_warn needs to be invalidated; no invalidation method is defined in the @implementation for SuppressedMissingInvalidationMethod}}
+#endif
+
+@end
+@implementation SuppressedMissingInvalidationMethod
+@end
+
@interface MissingInvalidationMethod2 : Foo <FooBar_Protocol> {
Foo *Ivar1;
#if RUN_IVAR_INVALIDATION
@@ -290,8 +301,10 @@ @implementation MissingInvalidationMethodDecl2
@end
@interface InvalidatedInPartial : SomeInvalidationImplementingObject {
- SomeInvalidationImplementingObject *Ivar1;
- SomeInvalidationImplementingObject *Ivar2;
+ SomeInvalidationImplementingObject *Ivar1;
+ SomeInvalidationImplementingObject *Ivar2;
+ [[clang::suppress]]
+ SomeInvalidationImplementingObject *Ivar3; // no-warning
}
-(void)partialInvalidator __attribute__((annotate("objc_instance_variable_invalidator_partial")));
@end
diff --git a/clang/test/Analysis/suppression-attr-doc.cpp b/clang/test/Analysis/suppression-attr-doc.cpp
index 1208842799ed9..ca4e665a082ce 100644
--- a/clang/test/Analysis/suppression-attr-doc.cpp
+++ b/clang/test/Analysis/suppression-attr-doc.cpp
@@ -52,3 +52,17 @@ int bar2(bool coin_flip) {
__attribute__((suppress))
return *result; // leak warning is suppressed only on this path
}
+
+class [[clang::suppress]] C {
+ int foo() {
+ int *x = nullptr;
+ return *x; // warnings suppressed in the entire class
+ }
+
+ int bar();
+};
+
+int C::bar() {
+ int *x = nullptr;
+ return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
+}
diff --git a/clang/test/Analysis/suppression-attr.cpp b/clang/test/Analysis/suppression-attr.cpp
new file mode 100644
index 0000000000000..89bc3c47dbd51
--- /dev/null
+++ b/clang/test/Analysis/suppression-attr.cpp
@@ -0,0 +1,68 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
+
+namespace [[clang::suppress]]
+suppressed_namespace {
+ int foo() {
+ int *x = 0;
+ return *x;
+ }
+
+ int foo_forward();
+}
+
+int suppressed_namespace::foo_forward() {
+ int *x = 0;
+ return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
+}
+
+// Another instance of the same namespace.
+namespace suppressed_namespace {
+ int bar() {
+ int *x = 0;
+ return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
+ }
+}
+
+void lambda() {
+ [[clang::suppress]] {
+ auto lam = []() {
+ int *x = 0;
+ return *x;
+ };
+ }
+}
+
+class [[clang::suppress]] SuppressedClass {
+ int foo() {
+ int *x = 0;
+ return *x;
+ }
+
+ int bar();
+};
+
+int SuppressedClass::bar() {
+ int *x = 0;
+ return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
+}
+
+class SuppressedMethodClass {
+ [[clang::suppress]] int foo() {
+ int *x = 0;
+ return *x;
+ }
+
+ [[clang::suppress]] int bar1();
+ int bar2();
+};
+
+int SuppressedMethodClass::bar1() {
+ int *x = 0;
+ return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
+}
+
+[[clang::suppress]]
+int SuppressedMethodClass::bar2() {
+ int *x = 0;
+ return *x; // no-warning
+}
diff --git a/clang/test/Analysis/suppression-attr.m b/clang/test/Analysis/suppression-attr.m
index 8ba8dda722721..acef4b34fb09f 100644
--- a/clang/test/Analysis/suppression-attr.m
+++ b/clang/test/Analysis/suppression-attr.m
@@ -168,17 +168,15 @@ void malloc_leak_suppression_2_1() {
*x = 42;
}
-// TODO: reassess when we decide what to do with declaration annotations
-void malloc_leak_suppression_2_2() /* SUPPRESS */ {
+void malloc_leak_suppression_2_2() SUPPRESS {
int *x = (int *)malloc(sizeof(int));
*x = 42;
-} // expected-warning{{Potential leak of memory pointed to by 'x'}}
+} // no-warning
-// TODO: reassess when we decide what to do with declaration annotations
-/* SUPPRESS */ void malloc_leak_suppression_2_3() {
+SUPPRESS void malloc_leak_suppression_2_3() {
int *x = (int *)malloc(sizeof(int));
*x = 42;
-} // expected-warning{{Potential leak of memory pointed to by 'x'}}
+} // no-warning
void malloc_leak_suppression_2_4(int cond) {
int *x = (int *)malloc(sizeof(int));
@@ -233,20 +231,15 @@ - (void)methodWhichMayFail:(NSError **)error {
@interface TestSuppress : UIResponder {
}
-// TODO: reassess when we decide what to do with declaration annotations
-@property(copy) /* SUPPRESS */ NSMutableString *mutableStr;
-// expected-warning@-1 {{Property of mutable type 'NSMutableString' has 'copy' attribute; an immutable object will be stored instead}}
+@property(copy) SUPPRESS NSMutableString *mutableStr; // no-warning
@end
@implementation TestSuppress
-// TODO: reassess when we decide what to do with declaration annotations
-- (BOOL)resignFirstResponder /* SUPPRESS */ {
+- (BOOL)resignFirstResponder SUPPRESS { // no-warning
return 0;
-} // expected-warning {{The 'resignFirstResponder' instance method in UIResponder subclass 'TestSuppress' is missing a [super resignFirstResponder] call}}
+}
-// TODO: reassess when we decide what to do with declaration annotations
-- (void)methodWhichMayFail:(NSError **)error /* SUPPRESS */ {
- // expected-warning@-1 {{Method accepting NSError** should have a non-void return value to indicate whether or not an error occurred}}
+- (void)methodWhichMayFail:(NSError **)error SUPPRESS { // no-warning
}
@end
@@ -269,3 +262,40 @@ void ast_checker_suppress_1() {
struct ABC *Abc;
SUPPRESS { Abc = (struct ABC *)&Ab; }
}
+
+SUPPRESS int suppressed_function() {
+ int *x = 0;
+ return *x; // no-warning
+}
+
+SUPPRESS int suppressed_function_forward();
+int suppressed_function_forward() {
+ int *x = 0;
+ return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
+}
+
+int suppressed_function_backward();
+SUPPRESS int suppressed_function_backward() {
+ int *x = 0;
+ return *x; // no-warning
+}
+
+SUPPRESS
+@interface SuppressedInterface
+-(int)suppressedMethod;
+-(int)regularMethod SUPPRESS;
+@end
+
+@implementation SuppressedInterface
+-(int)suppressedMethod SUPPRESS {
+ int *x = 0;
+ return *x; // no-warning
+}
+
+// This one is NOT suppressed by the attribute on the forward declaration,
+// and it's also NOT suppressed by the attribute on the entire interface.
+-(int)regularMethod {
+ int *x = 0;
+ return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
+}
+@end
diff --git a/clang/test/Analysis/unused-ivars.m b/clang/test/Analysis/unused-ivars.m
index 32e7e80fc4276..8788804bf0c33 100644
--- a/clang/test/Analysis/unused-ivars.m
+++ b/clang/test/Analysis/unused-ivars.m
@@ -44,6 +44,15 @@ - (void)setIvar:(id)newValue {
}
@end
+// Confirm that the checker respects [[clang::suppress]].
+@interface TestC {
+@private
+ [[clang::suppress]] int x; // no-warning
+}
+@end
+@implementation TestC @end
+
+
//===----------------------------------------------------------------------===//
// Detect that ivar is in use, if used in category in the same file as the
// implementation.
@@ -125,4 +134,4 @@ @implementation Radar11059352
- (void)useWorkspace {
NSString *workspacePathString = _workspacePath.pathString;
}
-@end
\ No newline at end of file
+@end
diff --git a/clang/test/SemaCXX/attr-suppress.cpp b/clang/test/SemaCXX/attr-suppress.cpp
index fb5e2ac7ce206..e8f6d97988091 100644
--- a/clang/test/SemaCXX/attr-suppress.cpp
+++ b/clang/test/SemaCXX/attr-suppress.cpp
@@ -23,18 +23,16 @@ union [[gsl::suppress("type.1")]] U {
float f;
};
+// This doesn't really suppress anything but why not?
[[clang::suppress]];
-// expected-error@-1 {{'suppress' attribute only applies to variables and statements}}
namespace N {
[[clang::suppress("in-a-namespace")]];
-// expected-error@-1 {{'suppress' attribute only applies to variables and statements}}
} // namespace N
[[clang::suppress]] int global = 42;
[[clang::suppress]] void foo() {
- // expected-error@-1 {{'suppress' attribute only applies to variables and statements}}
[[clang::suppress]] int *p;
[[clang::suppress]] int a = 0; // no-warning
@@ -56,7 +54,11 @@ namespace N {
}
class [[clang::suppress("type.1")]] V {
- // expected-error@-1 {{'suppress' attribute only applies to variables and statements}}
int i;
float f;
};
+
+// FIXME: There's no good reason why we shouldn't support this case.
+// But it doesn't look like clang generally supports such attributes yet.
+class W : [[clang::suppress]] public V { // expected-error{{'suppress' attribute cannot be applied to a base specifier}}
+};
diff --git a/clang/test/SemaObjC/attr-suppress.m b/clang/test/SemaObjC/attr-suppress.m
index ade8f94ec5895..c12da097bf844 100644
--- a/clang/test/SemaObjC/attr-suppress.m
+++ b/clang/test/SemaObjC/attr-suppress.m
@@ -6,8 +6,7 @@
SUPPRESS1 int global = 42;
SUPPRESS1 void foo() {
- // expected-error@-1 {{'suppress' attribute only applies to variables and statements}}
- SUPPRESS1 int *p;
+ SUPPRESS1 int *p; // no-warning
SUPPRESS1 int a = 0; // no-warning
SUPPRESS2()
@@ -28,23 +27,19 @@ SUPPRESS1 switch (a) { // no-warning
// GNU-style ...
[truncated]
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
// This is the fast path. However, we should still consider the topmost | ||
// declaration that isn't TranslationUnitDecl, because we should respect | ||
// attributes on the entire declaration chain. | ||
while (true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is going on here? How does this differ from just getTranslationUnitDecl
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is, uh, a somewhat premature performance optimization. I think I want to get rid of it, as discussed in #79398 (it's incorrect as well), but it probably requires a separate discussion.
The static analyzer normally always takes the fast path as every single "supported" checker fills in the data correctly.
This isn't just about scanning the TU here for discovering suppressed ranges, it's more about the fact that the static analyzer is so careful about not analyzing non-user code that normally it doesn't even deserialize PCHs if they aren't directly required for analysis. This is demonstrated by the lovely test case in clang/test/Analysis/check-deserialization.cpp
. A full TU scan here will cause these PCHs to be deserialized even though the rest of the analysis never needed to look at them.
I have some data that this can have significant performance impact. However, other tools such as clang-tidy never had such optimization (they always scan the entire TU indiscriminately) and this never stopped them from satisfying the usual performance expectations of static analysis tools. We've also abandoned this optimization in a downstream fork for a while and it seems perfectly livable.
So I want to either abandon this non-deserialization guarantee (simply always scan the entire TU), or find a different way to satisfy it (eg., collect those ranges eagerly during AST construction so that to avoid the after-the-fact scan; then if PCHs/modules are used, suppression ranges will make it into PCHs and get deserialized together with them as-needed). I'll probably try to do some of that in the coming weeks but I wanted this patch to be independent from that so that to get the attribute's behavior to reasonable shape without breaking anything else.
Ok I'll try to land! |
llvm#80371) The attribute is now allowed on an assortment of declarations, to suppress warnings related to declarations themselves, or all warnings in the lexical scope of the declaration. I don't necessarily see a reason to have a list at all, but it does look as if some of those more niche items aren't properly supported by the compiler itself so let's maintain a short safe list for now. The initial implementation raised a question whether the attribute should apply to lexical declaration context vs. "actual" declaration context. I'm using "lexical" here because it results in less warnings suppressed, which is the conservative behavior: we can always expand it later if we think this is wrong, without breaking any existing code. I also think that this is the correct behavior that we will probably never want to change, given that the user typically desires to keep the suppressions as localized as possible. (cherry picked from commit 017675f)
did I miss something - it looks like this was committed without approval? |
It looks that way to me @haoNoQ : Did you commit this instead of something else? Can you revert this until we get approval? |
Hmm, no, I landed it because I made an assumption that there's simply not that much interest in this work (I'm quite depressed about this in general lately) so as a code owner I just made a call that it's probably good enough to go and rely on post-commit review. Now that you bring this up, it does sound a lot like I should get myself out of this mindset and at least ping people first. Especially Erich who I specifically invited as the code owner of clang attributes. Absolutely my bad. I definitely see how this isn't great moving forward and I will do better from now on. Should I also revert and give you folks time to properly review and course-correct me? |
I hadn't noticed that you were the Analysis code owner. I don't really see anything in the Clang stuff (which IS attributes and my perview) that require a revert. Sorry for letting this drop off my backlog, I did one review on it at one point, then for some reason didn't come back to it. A ping would have been appreciated (for next time), but no need to revert this time. |
Ok gotcha thanks! In any case, I'll do my best to handle this more gracefully in the future. Your advice is always appreciated! |
Perfect! I'll try to be better about this in the future as well. |
Commit without precommit review is fine, especially from a code owner - if you only wanted the PR for automated precommit checking, you can add the |
Yes I like this perspective: "Schrödinger's need-for-pre-commit-approval" isn't a great way to communicate 😅 I'll be more clear in the future. |
The attribute is now allowed on an assortment of declarations, to suppress warnings related to declarations themselves, or all warnings in the lexical scope of the declaration.
I don't necessarily see a reason to have a list at all, but it does look as if some of those more niche items aren't properly supported by the compiler itself so let's maintain a short safe list for now.
The initial implementation raised a question whether the attribute should apply to lexical declaration context vs. "actual" declaration context. I'm using "lexical" here because it results in less warnings suppressed, which is the conservative behavior: we can always expand it later if we think this is wrong, without breaking any existing code. I also think that this is the correct behavior that we will probably never want to change, given that the user typically desires to keep the suppressions as localized as possible.