Skip to content

Commit 062adaf

Browse files
Fznamznontru
authored andcommitted
[clang] Reject if constexpr in C (llvm#112685)
Fixes llvm#112587
1 parent 6ee4908 commit 062adaf

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

clang/lib/Parse/ParseStmt.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -1508,10 +1508,13 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
15081508
SourceLocation ConstevalLoc;
15091509

15101510
if (Tok.is(tok::kw_constexpr)) {
1511-
Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
1512-
: diag::ext_constexpr_if);
1513-
IsConstexpr = true;
1514-
ConsumeToken();
1511+
// C23 supports constexpr keyword, but only for object definitions.
1512+
if (getLangOpts().CPlusPlus) {
1513+
Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
1514+
: diag::ext_constexpr_if);
1515+
IsConstexpr = true;
1516+
ConsumeToken();
1517+
}
15151518
} else {
15161519
if (Tok.is(tok::exclaim)) {
15171520
NotLocation = ConsumeToken();

clang/test/Sema/constexpr.c

+7
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,10 @@ void infsNaNs() {
357357
constexpr double db5 = LD_SNAN; // expected-error {{constexpr initializer evaluates to nan which is not exactly representable in type 'const double'}}
358358
constexpr double db6 = INF;
359359
}
360+
361+
void constexprif() {
362+
if constexpr (300) {} //expected-error {{expected '(' after 'if'}}
363+
}
364+
void constevalif() {
365+
if consteval (300) {} //expected-error {{expected '(' after 'if'}}
366+
}

0 commit comments

Comments
 (0)