[clang] [clang][StaticAnalyzer] Implement per checker suppression for CSA (PR #247788)

Balázs Benics via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 2 04:50:04 PST 2026


================
@@ -19,24 +19,37 @@ using namespace ento;
 
 namespace {
 
-using Ranges = llvm::SmallVectorImpl<SourceRange>;
-
-inline bool hasSuppression(const Decl *D) {
-  // FIXME: Implement diagnostic identifier arguments
-  // (checker names, "hashtags").
-  if (const auto *Suppression = D->getAttr<SuppressAttr>())
-    return !Suppression->isGSL() &&
-           (Suppression->diagnosticIdentifiers().empty());
-  return false;
+using Suppressions = llvm::SmallVectorImpl<BugSuppression::Suppression>;
+
+// A checker name matches an identifier attached to `[[clang::suppress]]`
+// (e.g. "core.NullDereference") if the identifier names the checker exactly,
+// or names an enclosing package of it (e.g. "core" or "unix.Malloc" matching
+// "unix.MallocSizeof").
+inline bool checkerNameMatches(StringRef CheckerName, StringRef Identifier) {
+  if (!CheckerName.starts_with(Identifier))
+    return false;
+  return CheckerName.size() == Identifier.size() ||
+         CheckerName[Identifier.size()] == '.';
 }
-inline bool hasSuppression(const AttributedStmt *S) {
-  // FIXME: Implement diagnostic identifier arguments
-  // (checker names, "hashtags").
-  return llvm::any_of(S->getAttrs(), [](const Attr *A) {
-    const auto *Suppression = dyn_cast<SuppressAttr>(A);
-    return Suppression && !Suppression->isGSL() &&
-           (Suppression->diagnosticIdentifiers().empty());
-  });
+
+using SuppressAttrs = llvm::SmallVector<const SuppressAttr *, 2>;
+
+// A node may carry several [[clang::suppress]] attributes, and each of them
+// contributes its own identifiers.
+inline SuppressAttrs getSuppressions(const Decl *D) {
----------------
steakhal wrote:

Here too, we are in an anonymous namespace within a CPP file. Inline doesn't do much in this context.
We should either close the anon namespace and use `static` everywhere or just not use `inline`.
I also wonder why do we need to materialize this vector if we could lazily iterate them when needed.

>From what I can see, you would only iterate the resulting vector once and that's it, so I think we could avoid materializing this list and just compose `make_filter_range` and `map_range`.

https://github.com/llvm/llvm-project/pull/247788


More information about the cfe-commits mailing list