[clang] [analyzer] Add support for consteval in ConditionBRVisitor::VisitTerminator (PR #146859)
Imad Aldij via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 7 01:53:26 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/5] 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/5] 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
>From 90a235dfff9c6eaaa47f62d542de03868154a806 Mon Sep 17 00:00:00 2001
From: Imad Aldij <mail at imadij.com>
Date: Thu, 3 Jul 2025 16:30:32 +0300
Subject: [PATCH 3/5] Update test formatting
Co-authored-by: Balazs Benics <benicsbalazs at gmail.com>
---
clang/test/Analysis/consteval-if.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/Analysis/consteval-if.cpp b/clang/test/Analysis/consteval-if.cpp
index 2ce9a69067951..b7eae9db6a239 100644
--- a/clang/test/Analysis/consteval-if.cpp
+++ b/clang/test/Analysis/consteval-if.cpp
@@ -5,4 +5,4 @@ void test_consteval() {
int *ptr = nullptr;
*ptr = 42; // expected-warning{{Dereference of null pointer (loaded from variable 'ptr')}}
}
-}
\ No newline at end of file
+}
>From d5015902c9faadffff0858c99b7b462055e48cc4 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Mon, 7 Jul 2025 10:47:18 +0300
Subject: [PATCH 4/5] Improve code style
---
clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
index d81a3f5a2858b..4631e0cad5546 100644
--- a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
@@ -2793,12 +2793,14 @@ PathDiagnosticPieceRef ConditionBRVisitor::VisitTerminator(
// more tricky because there are more than two branches to account for.
default:
return nullptr;
- case Stmt::IfStmtClass:
- // Handle if consteval which doesn't have a traditional condition
- if (cast<IfStmt>(Term)->isConsteval())
+ case Stmt::IfStmtClass: {
+ const auto *IfStatement = cast<IfStmt>(Term);
+ // Handle if consteval which doesn't have a traditional condition.
+ if (IfStatement->isConsteval())
return nullptr;
- Cond = cast<IfStmt>(Term)->getCond();
+ Cond = IfStatement->getCond();
break;
+ }
case Stmt::ConditionalOperatorClass:
Cond = cast<ConditionalOperator>(Term)->getCond();
break;
>From 82a47298049a11d918e4e0d2a62bbd2683ce9aa1 Mon Sep 17 00:00:00 2001
From: Imad Aldij <os at imadij.com>
Date: Mon, 7 Jul 2025 10:47:23 +0300
Subject: [PATCH 5/5] Add test for not consteval
---
clang/test/Analysis/consteval-if.cpp | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/clang/test/Analysis/consteval-if.cpp b/clang/test/Analysis/consteval-if.cpp
index b7eae9db6a239..f9caacf2949c9 100644
--- a/clang/test/Analysis/consteval-if.cpp
+++ b/clang/test/Analysis/consteval-if.cpp
@@ -6,3 +6,10 @@ void test_consteval() {
*ptr = 42; // expected-warning{{Dereference of null pointer (loaded from variable 'ptr')}}
}
}
+
+void test_not_consteval() {
+ if !consteval {
+ int *ptr = nullptr;
+ *ptr = 42; // expected-warning{{Dereference of null pointer (loaded from variable 'ptr')}}
+ }
+}
More information about the cfe-commits
mailing list