[clang] [analyzer] Add support for consteval in ConditionBRVisitor::VisitTerminator (PR #146859)
Imad Aldij via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 3 05:55:45 PDT 2025
https://github.com/imdj updated https://github.com/llvm/llvm-project/pull/146859
>From 1077e164158965e824097542dc4c3ecc8821d6dc Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Thu, 3 Jul 2025 13:50:55 +0300
Subject: [PATCH 1/2] Add support for consteval if in ConditionBRVisitor
---
clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
index 3686bd4488877..d81a3f5a2858b 100644
--- a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
@@ -2794,6 +2794,9 @@ PathDiagnosticPieceRef ConditionBRVisitor::VisitTerminator(
default:
return nullptr;
case Stmt::IfStmtClass:
+ // Handle if consteval which doesn't have a traditional condition
+ if (cast<IfStmt>(Term)->isConsteval())
+ return nullptr;
Cond = cast<IfStmt>(Term)->getCond();
break;
case Stmt::ConditionalOperatorClass:
>From 27b793fcf2347d631991f9f83df7ef5787df17d6 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Thu, 3 Jul 2025 15:52:35 +0300
Subject: [PATCH 2/2] add test case for consteval and ConditionBRVisitor
---
clang/test/Analysis/consteval-if.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
create mode 100644 clang/test/Analysis/consteval-if.cpp
diff --git a/clang/test/Analysis/consteval-if.cpp b/clang/test/Analysis/consteval-if.cpp
new file mode 100644
index 0000000000000..2ce9a69067951
--- /dev/null
+++ b/clang/test/Analysis/consteval-if.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_analyze_cc1 -std=c++23 -analyzer-checker=core -verify %s
+
+void test_consteval() {
+ if consteval {
+ int *ptr = nullptr;
+ *ptr = 42; // expected-warning{{Dereference of null pointer (loaded from variable 'ptr')}}
+ }
+}
\ No newline at end of file
More information about the cfe-commits
mailing list