Skip to content

Commit e21c80a

Browse files
authored
[clang] Reject if constexpr in C (#112685)
Fixes #112587
1 parent 2f0b4f4 commit e21c80a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

clang/lib/Parse/ParseStmt.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,10 +1518,13 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
15181518
SourceLocation ConstevalLoc;
15191519

15201520
if (Tok.is(tok::kw_constexpr)) {
1521-
Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
1522-
: diag::ext_constexpr_if);
1523-
IsConstexpr = true;
1524-
ConsumeToken();
1521+
// C23 supports constexpr keyword, but only for object definitions.
1522+
if (getLangOpts().CPlusPlus) {
1523+
Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
1524+
: diag::ext_constexpr_if);
1525+
IsConstexpr = true;
1526+
ConsumeToken();
1527+
}
15251528
} else {
15261529
if (Tok.is(tok::exclaim)) {
15271530
NotLocation = ConsumeToken();

clang/test/Sema/constexpr.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,10 @@ struct S10 {
367367
constexpr struct S10 c = { 255 };
368368
// FIXME-expected-error@-1 {{constexpr initializer evaluates to 255 which is not exactly representable in 'long long' bit-field with width 8}}
369369
// See: GH#101299
370+
371+
void constexprif() {
372+
if constexpr (300) {} //expected-error {{expected '(' after 'if'}}
373+
}
374+
void constevalif() {
375+
if consteval (300) {} //expected-error {{expected '(' after 'if'}}
376+
}

0 commit comments

Comments
 (0)