[clang] Feat/suppress leak reports (PR #199240)

Shuvakant Patra via cfe-commits cfe-commits at lists.llvm.org
Fri May 22 12:19:35 PDT 2026


https://github.com/shuvakant6623 updated https://github.com/llvm/llvm-project/pull/199240

>From 284f714d45b57089310002ef8f30c35d3d8c69f5 Mon Sep 17 00:00:00 2001
From: shuvakant6623 <scientefic2612 at gmail.com>
Date: Sat, 23 May 2026 00:42:54 +0530
Subject: [PATCH 1/3] [analyzer] Add option to suppress leak reports for
 selected ownership types

---
 .../StaticAnalyzer/Checkers/MallocChecker.cpp | 30 +++++++++++++++++++
 clang/test/Analysis/analyzer-config.c         |  1 +
 .../Analysis/suppress-leak-custom-allocator.c | 10 +++++++
 3 files changed, 41 insertions(+)
 create mode 100644 clang/test/Analysis/suppress-leak-custom-allocator.c

diff --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index a0a8bd4dfc90d..23faf1747d795 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -48,6 +48,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/STLExtras.h"
 #include "AllocationState.h"
 #include "InterCheckerAPI.h"
 #include "NoOwnershipChangeVisitor.h"
@@ -458,6 +459,9 @@ class MallocChecker
 
   StringRef getDebugTag() const override { return "MallocChecker"; }
 
+private:
+  std::vector<std::string> SuppressLeakTypes;
+
 private:
 #define CHECK_FN(NAME)                                                         \
   void NAME(ProgramStateRef State, const CallEvent &Call, CheckerContext &C)   \
@@ -3027,6 +3031,16 @@ void MallocChecker::HandleLeak(SymbolRef Sym, ExplodedNode *N,
   if (Family.Kind == AF_Alloca)
     return;
 
+  // --- BEGIN: Suppress leak reports for selected ownership types ---
+  if (Family.Kind == AF_Custom && Family.CustomName) {
+    StringRef Type = *Family.CustomName;
+
+    if (llvm::is_contained(this->SuppressLeakTypes, Type)) {
+      return;
+    }
+  }
+  // --- END ---
+
   const Leak *Frontend = getRelevantFrontendAs<Leak>(Family);
   // Note that for leaks we don't add a sink when the relevant frontend is
   // disabled because the leak is reported with a non-fatal error node, while
@@ -4197,6 +4211,22 @@ void ento::registerInnerPointerCheckerAux(CheckerManager &Mgr) {
 
 void ento::registerDynamicMemoryModeling(CheckerManager &Mgr) {
   auto *Chk = Mgr.getChecker<MallocChecker>();
+  auto ChkName = Mgr.getCurrentCheckerName();
+
+  StringRef SuppressList =
+    Mgr.getAnalyzerOptions().getCheckerStringOption(
+        ChkName, "SuppressLeakReportsFor");
+
+  SmallVector<StringRef, 8> Split;
+  SuppressList.split(Split, ',', -1, false);
+
+  for (StringRef S : Split) {
+    S = S.trim();
+    if (S.empty())
+      continue;
+    Chk->SuppressLeakTypes.push_back(S.str());
+  }
+
   // FIXME: This is a "hidden" undocumented frontend but there are public
   // checker options which are attached to it.
   CheckerNameRef DMMName = Mgr.getCurrentCheckerName();
diff --git a/clang/test/Analysis/analyzer-config.c b/clang/test/Analysis/analyzer-config.c
index 04dc8c24421bc..972f3ecc8089a 100644
--- a/clang/test/Analysis/analyzer-config.c
+++ b/clang/test/Analysis/analyzer-config.c
@@ -136,6 +136,7 @@
 // CHECK-NEXT: track-conditions-debug = false
 // CHECK-NEXT: unix.DynamicMemoryModeling:AddNoOwnershipChangeNotes = true
 // CHECK-NEXT: unix.DynamicMemoryModeling:Optimistic = false
+// CHECK-NEXT: unix.DynamicMemoryModeling:SuppressLeakReportsFor = ""
 // CHECK-NEXT: unix.Errno:AllowErrnoReadOutsideConditionExpressions = true
 // CHECK-NEXT: unix.StdCLibraryFunctions:DisplayLoadedSummaries = false
 // CHECK-NEXT: unix.StdCLibraryFunctions:ModelPOSIX = true
diff --git a/clang/test/Analysis/suppress-leak-custom-allocator.c b/clang/test/Analysis/suppress-leak-custom-allocator.c
new file mode 100644
index 0000000000000..45ac9b7aee336
--- /dev/null
+++ b/clang/test/Analysis/suppress-leak-custom-allocator.c
@@ -0,0 +1,10 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=unix.Malloc,unix.DynamicMemoryModeling \
+// RUN: -analyzer-config unix.DynamicMemoryModeling:SuppressLeakReportsFor=arena \
+// RUN: %s -verify
+
+void *arena_alloc(void) __attribute__((ownership_returns(arena)));
+
+void test() {
+  void *p = arena_alloc();
+  // expected-no-diagnostics
+}
\ No newline at end of file

>From 70bef36f308a106b027d2340c831d46c60551656 Mon Sep 17 00:00:00 2001
From: shuvakant6623 <scientefic2612 at gmail.com>
Date: Sat, 23 May 2026 00:44:01 +0530
Subject: [PATCH 2/3] [analyzer] updated Checkers.td

---
 clang/include/clang/StaticAnalyzer/Checkers/Checkers.td | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index 6b9e0b50e1f59..cac465d69eac5 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -512,7 +512,12 @@ def DynamicMemoryModeling: Checker<"DynamicMemoryModeling">,
                   "NoStoreFuncVisitor.",
                   "true",
                   Released,
-                  Hide>
+                  Hide>,
+    CmdLineOption<String,
+                  "SuppressLeakReportsFor",
+                  "Comma-separated list of ownership types for which leak reports should be suppressed",
+                  "",
+                  InAlpha>
   ]>,
   Dependencies<[CStringModeling]>,
   Documentation<NotDocumented>,

>From ea7e716c3eceb3ed22c30a016f8565a2d00e1e1e Mon Sep 17 00:00:00 2001
From: shuvakant6623 <scientefic2612 at gmail.com>
Date: Sat, 23 May 2026 00:48:29 +0530
Subject: [PATCH 3/3] [analyzer] Add option to suppress leak reports for
 selected ownership types

---
 clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index 23faf1747d795..d182a4eea3b9e 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -3029,7 +3029,11 @@ void MallocChecker::HandleLeak(SymbolRef Sym, ExplodedNode *N,
   AllocationFamily Family = RS->getAllocationFamily();
 
   if (Family.Kind == AF_Alloca)
+<<<<<<< HEAD
     return;
+=======
+  return;
+>>>>>>> 85c273dfcf50 ([analyzer] Address review feedback)
 
   // --- BEGIN: Suppress leak reports for selected ownership types ---
   if (Family.Kind == AF_Custom && Family.CustomName) {



More information about the cfe-commits mailing list