[analyzer] Add support for consteval in ConditionBRVisitor::VisitTerminator (#146859)
Fix crash when ConditionBRVisitor::VisitTerminator is faced with `if
consteval` which doesn't have the expected traditional condition
Fixes #139130
diff --git a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
index 3686bd4..4631e0c 100644
--- a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
+++ b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
@@ -2793,9 +2793,14 @@
// more tricky because there are more than two branches to account for.
default:
return nullptr;
- case Stmt::IfStmtClass:
- Cond = cast<IfStmt>(Term)->getCond();
+ 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 = IfStatement->getCond();
break;
+ }
case Stmt::ConditionalOperatorClass:
Cond = cast<ConditionalOperator>(Term)->getCond();
break;
diff --git a/clang/test/Analysis/consteval-if.cpp b/clang/test/Analysis/consteval-if.cpp
new file mode 100644
index 0000000..e673680
--- /dev/null
+++ b/clang/test/Analysis/consteval-if.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_analyze_cc1 -std=c++23 -analyzer-checker=core -verify %s
+// RUN: %clang_analyze_cc1 -std=c++26 -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')}}
+ }
+}
+
+void test_not_consteval() {
+ if !consteval {
+ int *ptr = nullptr;
+ *ptr = 42; // expected-warning{{Dereference of null pointer (loaded from variable 'ptr')}}
+ }
+}