[clang] Feat/suppress leak reports (PR #199240)
Shuvakant Patra via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 11 08:51:44 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/5] [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/5] [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/5] [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) {
>From 1bf3a2304ed65a3e70538a8d6b561ce2a5c7675c Mon Sep 17 00:00:00 2001
From: shuvakant6623 <scientefic2612 at gmail.com>
Date: Sat, 23 May 2026 00:54:31 +0530
Subject: [PATCH 4/5] [analyzer] MallocChecker fixes
---
clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp | 4 ----
1 file changed, 4 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index d182a4eea3b9e..23faf1747d795 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -3029,11 +3029,7 @@ 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) {
>From 5808dab361a103e57972de9d94a842afe45956e3 Mon Sep 17 00:00:00 2001
From: shuvakant6623 <scientefic2612 at gmail.com>
Date: Wed, 27 May 2026 01:01:06 +0530
Subject: [PATCH 5/5] [analyzer] Add SuppressLeakReportsFor option and fix
review comments in MallocChecker
---
.../clang/StaticAnalyzer/Checkers/Checkers.td | 3 +-
.../StaticAnalyzer/Checkers/MallocChecker.cpp | 31 ++++++-------------
2 files changed, 12 insertions(+), 22 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
index cac465d69eac5..31a80efe4fd90 100644
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
@@ -515,7 +515,8 @@ def DynamicMemoryModeling: Checker<"DynamicMemoryModeling">,
Hide>,
CmdLineOption<String,
"SuppressLeakReportsFor",
- "Comma-separated list of ownership types for which leak reports should be suppressed",
+ "Comma-separated list of ownership types for which "
+ "leak reports should be suppressed",
"",
InAlpha>
]>,
diff --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index 23faf1747d795..cb624b12935a6 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -401,7 +401,8 @@ class MallocChecker
bool ShouldIncludeOwnershipAnnotatedFunctions = false;
bool ShouldRegisterNoOwnershipChangeVisitor = false;
-
+ std::vector<std::string> SuppressLeakTypes;
+
// This checker family implements many bug types and frontends, and several
// bug types are shared between multiple frontends, so most of the frontends
// are declared with the helper class DynMemFrontend.
@@ -459,9 +460,6 @@ 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) \
@@ -3031,15 +3029,9 @@ 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)) {
+ if (Family.Kind == AF_Custom && Family.CustomName && llvm::is_contained(SuppressLeakTypes, *Family.CustomName) {
return;
- }
}
- // --- END ---
const Leak *Frontend = getRelevantFrontendAs<Leak>(Family);
// Note that for leaks we don't add a sink when the relevant frontend is
@@ -4211,25 +4203,22 @@ void ento::registerInnerPointerCheckerAux(CheckerManager &Mgr) {
void ento::registerDynamicMemoryModeling(CheckerManager &Mgr) {
auto *Chk = Mgr.getChecker<MallocChecker>();
- auto ChkName = Mgr.getCurrentCheckerName();
+ // FIXME: This is a "hidden" undocumented frontend but there are public
+ // checker options which are attached to it.
+ CheckerNameRef DMMName = Mgr.getCurrentCheckerName();
StringRef SuppressList =
Mgr.getAnalyzerOptions().getCheckerStringOption(
- ChkName, "SuppressLeakReportsFor");
+ DMMName, "SuppressLeakReportsFor");
SmallVector<StringRef, 8> Split;
- SuppressList.split(Split, ',', -1, false);
+ SuppressList.split(Split, ',');
for (StringRef S : Split) {
S = S.trim();
- if (S.empty())
- continue;
- Chk->SuppressLeakTypes.push_back(S.str());
+ if (!S.empty())
+ 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();
Chk->ShouldIncludeOwnershipAnnotatedFunctions =
Mgr.getAnalyzerOptions().getCheckerBooleanOption(DMMName, "Optimistic");
Chk->ShouldRegisterNoOwnershipChangeVisitor =
More information about the cfe-commits
mailing list