[clang] [clang] Reject if constexpr in C (PR #112685)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 17 02:58:37 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mariya Podchishchaeva (Fznamznon)
<details>
<summary>Changes</summary>
Fixes https://github.com/llvm/llvm-project/issues/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
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'}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/112685
More information about the cfe-commits
mailing list