[clang] [clang] Reject if constexpr in C (PR #112685)
Mariya Podchishchaeva via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 17 02:58:02 PDT 2024
https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/112685
Fixes https://github.com/llvm/llvm-project/issues/112587
>From 6fda18661ef51185a7d24dbfb66684f55dd20f26 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Thu, 17 Oct 2024 02:55:04 -0700
Subject: [PATCH] [clang] Reject if constexpr in C
Fixes https://github.com/llvm/llvm-project/issues/112587
---
clang/lib/Parse/ParseStmt.cpp | 11 +++++++----
clang/test/Sema/constexpr.c | 7 +++++++
2 files changed, 14 insertions(+), 4 deletions(-)
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