[clang] e21c80a - [clang] Reject if constexpr in C (#112685)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 17 04:42:38 PDT 2024


Author: Mariya Podchishchaeva
Date: 2024-10-17T13:42:35+02:00
New Revision: e21c80ac73a9da5c86c20dbce37c9227a17ab06a

URL: https://github.com/llvm/llvm-project/commit/e21c80ac73a9da5c86c20dbce37c9227a17ab06a
DIFF: https://github.com/llvm/llvm-project/commit/e21c80ac73a9da5c86c20dbce37c9227a17ab06a.diff

LOG: [clang] Reject if constexpr in C (#112685)

Fixes https://github.com/llvm/llvm-project/issues/112587

Added: 
    

Modified: 
    clang/lib/Parse/ParseStmt.cpp
    clang/test/Sema/constexpr.c

Removed: 
    


################################################################################
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 at -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'}}
+}


        


More information about the cfe-commits mailing list