Skip to content

Commit

Permalink
[clang] Reject if constexpr in C (#112685)
Browse files Browse the repository at this point in the history
Fixes #112587
  • Loading branch information
Fznamznon authored Oct 17, 2024
1 parent 2f0b4f4 commit e21c80a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 7 additions & 4 deletions clang/lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1518,10 +1518,13 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
SourceLocation ConstevalLoc;

if (Tok.is(tok::kw_constexpr)) {
Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
: diag::ext_constexpr_if);
IsConstexpr = true;
ConsumeToken();
// C23 supports constexpr keyword, but only for object definitions.
if (getLangOpts().CPlusPlus) {
Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
: diag::ext_constexpr_if);
IsConstexpr = true;
ConsumeToken();
}
} else {
if (Tok.is(tok::exclaim)) {
NotLocation = ConsumeToken();
Expand Down
7 changes: 7 additions & 0 deletions clang/test/Sema/constexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,10 @@ struct S10 {
constexpr struct S10 c = { 255 };
// FIXME-expected-error@-1 {{constexpr initializer evaluates to 255 which is not exactly representable in 'long long' bit-field with width 8}}
// See: GH#101299

void constexprif() {
if constexpr (300) {} //expected-error {{expected '(' after 'if'}}
}
void constevalif() {
if consteval (300) {} //expected-error {{expected '(' after 'if'}}
}

0 comments on commit e21c80a

Please sign in to comment.