Skip to content

Commit 778f60d

Browse files
authored
[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
1 parent dc8e89b commit 778f60d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,9 +2793,14 @@ PathDiagnosticPieceRef ConditionBRVisitor::VisitTerminator(
27932793
// more tricky because there are more than two branches to account for.
27942794
default:
27952795
return nullptr;
2796-
case Stmt::IfStmtClass:
2797-
Cond = cast<IfStmt>(Term)->getCond();
2796+
case Stmt::IfStmtClass: {
2797+
const auto *IfStatement = cast<IfStmt>(Term);
2798+
// Handle if consteval which doesn't have a traditional condition.
2799+
if (IfStatement->isConsteval())
2800+
return nullptr;
2801+
Cond = IfStatement->getCond();
27982802
break;
2803+
}
27992804
case Stmt::ConditionalOperatorClass:
28002805
Cond = cast<ConditionalOperator>(Term)->getCond();
28012806
break;

clang/test/Analysis/consteval-if.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_analyze_cc1 -std=c++23 -analyzer-checker=core -verify %s
2+
// RUN: %clang_analyze_cc1 -std=c++26 -analyzer-checker=core -verify %s
3+
4+
void test_consteval() {
5+
if consteval {
6+
int *ptr = nullptr;
7+
*ptr = 42; // expected-warning{{Dereference of null pointer (loaded from variable 'ptr')}}
8+
}
9+
}
10+
11+
void test_not_consteval() {
12+
if !consteval {
13+
int *ptr = nullptr;
14+
*ptr = 42; // expected-warning{{Dereference of null pointer (loaded from variable 'ptr')}}
15+
}
16+
}

0 commit comments

Comments
 (0)