[clang] [C23] [Diagnostics] No longer warn with -Wc++98-compat when using constexpr in C23 (PR #199537)
Mital Ashok via cfe-commits
cfe-commits at lists.llvm.org
Mon May 25 07:55:19 PDT 2026
https://github.com/MitalAshok created https://github.com/llvm/llvm-project/pull/199537
Also changes the generic -Wpre-c23-compat warning about using the constexpr keyword to a more specific one about using the constexpr specifier on a declaration, to match the warning in C++ mode (and warn in Sema instead of reusing a Parse warning)
>From 0062c6907033723c3edf8bfb19281b774ade5a71 Mon Sep 17 00:00:00 2001
From: Mital Ashok <mital at mitalashok.co.uk>
Date: Mon, 25 May 2026 15:38:28 +0100
Subject: [PATCH] [C23] [Diagnostics] No longer warn with -Wc++98-compat when
using constexpr in C23
Also changes the generic -Wpre-c23-compat warning about using the constexpr keyword to a more specific one about using the constexpr specifier on a declaration, to match the warning in C++ mode (and warn in Sema instead of reusing a Parse warning)
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 +++
clang/lib/Parse/ParseDecl.cpp | 2 --
clang/lib/Sema/DeclSpec.cpp | 2 +-
clang/test/Sema/constexpr-compat.c | 8 ++++++++
5 files changed, 14 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Sema/constexpr-compat.c
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3c1eacfc05dc8..b71bd180413ad 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -558,6 +558,8 @@ Improvements to Clang's diagnostics
- Clang now emits error when attribute is missing closing ``]]`` followed by ``;;``. (#GH187223)
+- No longer warn with about C++98 compatibility when using ``constexpr`` in C23.
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index dbe6cb2c3a41c..439641b81da2a 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3135,6 +3135,9 @@ def note_private_extern : Note<
"use __attribute__((visibility(\"hidden\"))) attribute instead">;
// C23 constexpr
+def warn_c23_compat_constexpr : Warning<
+ "'constexpr' specifier is incompatible with C standards before C23">,
+ InGroup<CPre23Compat>, DefaultIgnore;
def err_c23_constexpr_not_variable : Error<
"'constexpr' can only be used in variable declarations">;
def err_c23_constexpr_invalid_type : Error<
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 405dddf7991b4..04c3e9dd8b58e 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4275,8 +4275,6 @@ void Parser::ParseDeclarationSpecifiers(
// constexpr, consteval, constinit specifiers
case tok::kw_constexpr:
- if (getLangOpts().C23)
- Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName();
isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
PrevSpec, DiagID);
break;
diff --git a/clang/lib/Sema/DeclSpec.cpp b/clang/lib/Sema/DeclSpec.cpp
index 660b1805c450e..f9be1402d5d6c 100644
--- a/clang/lib/Sema/DeclSpec.cpp
+++ b/clang/lib/Sema/DeclSpec.cpp
@@ -1436,7 +1436,7 @@ void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
S.Diag(TSTLoc, diag::warn_cxx98_compat_unicode_type)
<< (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
if (getConstexprSpecifier() == ConstexprSpecKind::Constexpr)
- S.Diag(ConstexprLoc, diag::warn_cxx98_compat_constexpr);
+ S.Diag(ConstexprLoc, S.getLangOpts().CPlusPlus ? diag::warn_cxx98_compat_constexpr : diag::warn_c23_compat_constexpr);
else if (getConstexprSpecifier() == ConstexprSpecKind::Consteval)
S.Diag(ConstexprLoc, diag::warn_cxx20_compat_consteval);
else if (getConstexprSpecifier() == ConstexprSpecKind::Constinit)
diff --git a/clang/test/Sema/constexpr-compat.c b/clang/test/Sema/constexpr-compat.c
new file mode 100644
index 0000000000000..76fa08e79a7c1
--- /dev/null
+++ b/clang/test/Sema/constexpr-compat.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -std=c17 -verify=c17 -fsyntax-only -pedantic -Weverything %s
+// RUN: %clang_cc1 -std=c23 -verify=c23 -fsyntax-only -pedantic -Weverything %s
+
+constexpr int x = 1;
+// c23-warning at -1 {{'constexpr' specifier is incompatible with C standards before C23}}
+// c23-warning at -2 {{unused variable 'x'}}
+// c17-warning at -3 {{'constexpr' is a keyword in C23}}
+// c17-error at -4 {{unknown type name 'constexpr'}}
More information about the cfe-commits
mailing list