Skip to content

Conversation

@Fznamznon
Copy link
Contributor

Fixes #112587

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2024

@llvm/pr-subscribers-clang

Author: Mariya Podchishchaeva (Fznamznon)

Changes

Fixes #112587


Full diff: https://github.com/llvm/llvm-project/pull/112685.diff

2 Files Affected:

  • (modified) clang/lib/Parse/ParseStmt.cpp (+7-4)
  • (modified) clang/test/Sema/constexpr.c (+7)
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 6480e88316a7d5..60d647da48f053 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -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();
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index 0cf9491c4a42bf..eaa000b3b97758 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -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'}}
+}

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - I think we should backport that to 19 (so we don't need a changelog)

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@AaronBallman
Copy link
Collaborator

LGTM - I think we should backport that to 19 (so we don't need a changelog)

Agreed

@Fznamznon Fznamznon merged commit e21c80a into llvm:main Oct 17, 2024
11 checks passed
@Fznamznon Fznamznon added this to the LLVM 19.X Release milestone Oct 17, 2024
@Fznamznon
Copy link
Contributor Author

/cherry-pick e21c80a

@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2024

Failed to cherry-pick: e21c80a

https://github.com/llvm/llvm-project/actions/runs/11384260535

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

@Fznamznon
Copy link
Contributor Author

sobs while doing manual cherry-pick

Fznamznon added a commit to Fznamznon/llvm-project that referenced this pull request Oct 17, 2024
@Fznamznon
Copy link
Contributor Author

#112697

tru pushed a commit to Fznamznon/llvm-project that referenced this pull request Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category release:cherry-pick-failed

Projects

Development

Successfully merging this pull request may close these issues.

[Clang] Clang erroneously allows if constexpr in C23 mode

4 participants