[llvm-branch-commits] [clang] 062adaf - [clang] Reject if constexpr in C (#112685)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Oct 29 02:00:12 PDT 2024


Author: Mariya Podchishchaeva
Date: 2024-10-29T09:59:53+01:00
New Revision: 062adaf7d2f101ed9e83c09d192dd228dcbd9ffd

URL: https://github.com/llvm/llvm-project/commit/062adaf7d2f101ed9e83c09d192dd228dcbd9ffd
DIFF: https://github.com/llvm/llvm-project/commit/062adaf7d2f101ed9e83c09d192dd228dcbd9ffd.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 22d38adc28ebe9..3ac1f0fa27f83f 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -1508,10 +1508,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 8286cd2107d2f2..a874fd64808404 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -357,3 +357,10 @@ void infsNaNs() {
   constexpr double db5 = LD_SNAN; // expected-error {{constexpr initializer evaluates to nan which is not exactly representable in type 'const double'}}
   constexpr double db6 = INF;
 }
+
+void constexprif() {
+  if constexpr (300) {} //expected-error {{expected '(' after 'if'}}
+}
+void constevalif() {
+  if consteval (300) {} //expected-error {{expected '(' after 'if'}}
+}


        


More information about the llvm-branch-commits mailing list