[clang] [C23] [Diagnostics] No longer warn with -Wc++98-compat when using constexpr in C23 (PR #199537)
via cfe-commits
cfe-commits at lists.llvm.org
Mon May 25 07:55:59 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mital Ashok (MitalAshok)
<details>
<summary>Changes</summary>
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)
---
Full diff: https://github.com/llvm/llvm-project/pull/199537.diff
5 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+2)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+3)
- (modified) clang/lib/Parse/ParseDecl.cpp (-2)
- (modified) clang/lib/Sema/DeclSpec.cpp (+1-1)
- (added) clang/test/Sema/constexpr-compat.c (+8)
``````````diff
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'}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/199537
More information about the cfe-commits
mailing list