[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-27 Thread Artem Dergachev via cfe-commits

haoNoQ wrote:

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.

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-26 Thread David Blaikie via cfe-commits

dwblaikie wrote:

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 
`skip-precommit-approval` to indicate that the PR isn't intended for precommit 
review. But, yeah, otherwise it's good that if something /is/ sent for review, 
that it's not committed until it is reviewed. (code owners/domain experts 
sometimes get into the niche where this gets a bit fuzzy and it's "here's an 
idea I had, anyone got better ones/thoughts on this, otherwise I'll go ahead 
with it" - but yeah, usually the simplest way to deal with that is to have 
someone else you were asking say "yeah, sounds OK, I don't have any 
particularly better ideas about how to do this", etc)

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-20 Thread Erich Keane via cfe-commits

erichkeane wrote:

> 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.

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-20 Thread Artem Dergachev via cfe-commits

haoNoQ wrote:

Ok gotcha thanks! In any case, I'll do my best to handle this more gracefully 
in the future. Your advice is always appreciated!

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-20 Thread Erich Keane via cfe-commits

erichkeane wrote:

> 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.



https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-20 Thread Artem Dergachev via cfe-commits

haoNoQ wrote:

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?

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-20 Thread Erich Keane via cfe-commits

erichkeane wrote:

> 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?

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-20 Thread David Blaikie via cfe-commits

dwblaikie wrote:

did I miss something - it looks like this was committed without approval?

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-13 Thread Artem Dergachev via cfe-commits

https://github.com/haoNoQ closed https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-13 Thread Artem Dergachev via cfe-commits

haoNoQ wrote:

Ok I'll try to land!

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-02 Thread Artem Dergachev via cfe-commits


@@ -147,6 +147,20 @@ bool BugSuppression::isSuppressed(const 
PathDiagnosticLocation ,
 // 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) {

haoNoQ wrote:

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`](https://github.com/llvm/llvm-project/blob/llvmorg-19-init/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.

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-02 Thread Erich Keane via cfe-commits


@@ -147,6 +147,20 @@ bool BugSuppression::isSuppressed(const 
PathDiagnosticLocation ,
 // 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) {

erichkeane wrote:

What is going on here?  How does this differ from just 
`getTranslationUnitDecl`? 

https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-01 Thread Artem Dergachev via cfe-commits

https://github.com/haoNoQ updated 
https://github.com/llvm/llvm-project/pull/80371

>From b702fcca1b4c5924ce4740d054e396aa4bc67e97 Mon Sep 17 00:00:00 2001
From: Artem Dergachev 
Date: Thu, 25 Jan 2024 18:05:39 -0800
Subject: [PATCH 1/2] [attributes][analyzer] Generalize [[clang::suppress]] to
 declarations.

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.
---
 clang/include/clang/Basic/Attr.td |  7 ++
 clang/include/clang/Basic/AttrDocs.td | 23 +++
 clang/lib/Sema/SemaDecl.cpp   |  3 +
 clang/lib/Sema/SemaDeclAttr.cpp   |  5 --
 .../Checkers/ObjCUnusedIVarsChecker.cpp   |  2 +-
 .../StaticAnalyzer/Core/BugSuppression.cpp| 18 -
 .../WebKit/ref-cntbl-base-virtual-dtor.cpp| 10 +++
 .../WebKit/uncounted-lambda-captures.cpp  |  5 ++
 .../Checkers/WebKit/uncounted-local-vars.cpp  |  1 +
 .../Checkers/WebKit/uncounted-members.cpp |  9 +++
 clang/test/Analysis/ObjCRetSigs.m | 10 +++
 clang/test/Analysis/objc_invalidation.m   | 17 -
 clang/test/Analysis/suppression-attr-doc.cpp  | 14 
 clang/test/Analysis/suppression-attr.cpp  | 68 +++
 clang/test/Analysis/suppression-attr.m| 60 
 clang/test/Analysis/unused-ivars.m| 11 ++-
 clang/test/SemaCXX/attr-suppress.cpp  | 10 +--
 clang/test/SemaObjC/attr-suppress.m   | 19 ++
 18 files changed, 250 insertions(+), 42 deletions(-)
 create mode 100644 clang/test/Analysis/suppression-attr.cpp

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 , NamedDecl *D,
 S.mergeHLSLNumThreadsAttr(D, *NT, NT->getX(), NT->getY(), NT->getZ());
   else if (const auto *SA = dyn_cast(Attr))
 NewAttr = S.mergeHLSLShaderAttr(D, *SA, SA->getType());
+  else if (const auto *SupA = dyn_cast(Attr))
+// Do nothing. Each redeclaration should be suppressed separately.
+NewAttr = nullptr;
   else if (Attr->shouldInheritEvenIfAlreadyPresent() || !DeclHasAttr(D, Attr))
 NewAttr = cast(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 , Decl *D, 

[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-01 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 4d89356fef1a568de790ad8b3f53dc494b461e5b 
b702fcca1b4c5924ce4740d054e396aa4bc67e97 -- 
clang/test/Analysis/suppression-attr.cpp clang/lib/Sema/SemaDecl.cpp 
clang/lib/Sema/SemaDeclAttr.cpp 
clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp 
clang/lib/StaticAnalyzer/Core/BugSuppression.cpp 
clang/test/Analysis/Checkers/WebKit/ref-cntbl-base-virtual-dtor.cpp 
clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp 
clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp 
clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp 
clang/test/Analysis/suppression-attr-doc.cpp 
clang/test/SemaCXX/attr-suppress.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp 
b/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
index 4f8750d9f1..2f2df63468 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp
@@ -161,8 +161,8 @@ static void checkObjCUnusedIvar(const 
ObjCImplementationDecl *D,
 
   PathDiagnosticLocation L =
   PathDiagnosticLocation::create(Ivar, BR.getSourceManager());
-  BR.EmitBasicReport(ID, Checker, "Unused instance variable", 
"Optimization",
- os.str(), L);
+  BR.EmitBasicReport(ID, Checker, "Unused instance variable",
+ "Optimization", os.str(), L);
 }
 }
 

``




https://github.com/llvm/llvm-project/pull/80371
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-01 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Artem Dergachev (haoNoQ)


Changes

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.

---

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:

- (modified) clang/include/clang/Basic/Attr.td (+7) 
- (modified) clang/include/clang/Basic/AttrDocs.td (+23) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+3) 
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (-5) 
- (modified) clang/lib/StaticAnalyzer/Checkers/ObjCUnusedIVarsChecker.cpp 
(+1-1) 
- (modified) clang/lib/StaticAnalyzer/Core/BugSuppression.cpp (+16-2) 
- (modified) 
clang/test/Analysis/Checkers/WebKit/ref-cntbl-base-virtual-dtor.cpp (+10) 
- (modified) clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp 
(+5) 
- (modified) clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp (+1) 
- (modified) clang/test/Analysis/Checkers/WebKit/uncounted-members.cpp (+9) 
- (modified) clang/test/Analysis/ObjCRetSigs.m (+10) 
- (modified) clang/test/Analysis/objc_invalidation.m (+15-2) 
- (modified) clang/test/Analysis/suppression-attr-doc.cpp (+14) 
- (added) clang/test/Analysis/suppression-attr.cpp (+68) 
- (modified) clang/test/Analysis/suppression-attr.m (+45-15) 
- (modified) clang/test/Analysis/unused-ivars.m (+10-1) 
- (modified) clang/test/SemaCXX/attr-suppress.cpp (+6-4) 
- (modified) clang/test/SemaObjC/attr-suppress.m (+7-12) 


``diff
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 , NamedDecl *D,
 S.mergeHLSLNumThreadsAttr(D, *NT, NT->getX(), NT->getY(), NT->getZ());
   else if (const auto *SA = dyn_cast(Attr))
 NewAttr = S.mergeHLSLShaderAttr(D, *SA, SA->getType());
+  else if (const auto *SupA = dyn_cast(Attr))

[clang] [attributes][analyzer] Generalize [[clang::suppress]] to declarations. (PR #80371)

2024-02-01 Thread Artem Dergachev via cfe-commits

https://github.com/haoNoQ created 
https://github.com/llvm/llvm-project/pull/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.

>From b702fcca1b4c5924ce4740d054e396aa4bc67e97 Mon Sep 17 00:00:00 2001
From: Artem Dergachev 
Date: Thu, 25 Jan 2024 18:05:39 -0800
Subject: [PATCH] [attributes][analyzer] Generalize [[clang::suppress]] to
 declarations.

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.
---
 clang/include/clang/Basic/Attr.td |  7 ++
 clang/include/clang/Basic/AttrDocs.td | 23 +++
 clang/lib/Sema/SemaDecl.cpp   |  3 +
 clang/lib/Sema/SemaDeclAttr.cpp   |  5 --
 .../Checkers/ObjCUnusedIVarsChecker.cpp   |  2 +-
 .../StaticAnalyzer/Core/BugSuppression.cpp| 18 -
 .../WebKit/ref-cntbl-base-virtual-dtor.cpp| 10 +++
 .../WebKit/uncounted-lambda-captures.cpp  |  5 ++
 .../Checkers/WebKit/uncounted-local-vars.cpp  |  1 +
 .../Checkers/WebKit/uncounted-members.cpp |  9 +++
 clang/test/Analysis/ObjCRetSigs.m | 10 +++
 clang/test/Analysis/objc_invalidation.m   | 17 -
 clang/test/Analysis/suppression-attr-doc.cpp  | 14 
 clang/test/Analysis/suppression-attr.cpp  | 68 +++
 clang/test/Analysis/suppression-attr.m| 60 
 clang/test/Analysis/unused-ivars.m| 11 ++-
 clang/test/SemaCXX/attr-suppress.cpp  | 10 +--
 clang/test/SemaObjC/attr-suppress.m   | 19 ++
 18 files changed, 250 insertions(+), 42 deletions(-)
 create mode 100644 clang/test/Analysis/suppression-attr.cpp

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