[clang] [C23] Implement N3018: The constexpr specifier for object definitions (PR #73099)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 29 08:03:33 PST 2023


================
@@ -9237,6 +9282,22 @@ static FunctionDecl *CreateNewFunctionDecl(Sema &SemaRef, Declarator &D,
   FunctionDecl *NewFD = nullptr;
   bool isInline = D.getDeclSpec().isInlineSpecified();
 
+  ConstexprSpecKind ConstexprKind = D.getDeclSpec().getConstexprSpecifier();
+  if (ConstexprKind == ConstexprSpecKind::Constinit ||
+      (SemaRef.getLangOpts().C23 &&
+       ConstexprKind == ConstexprSpecKind::Constexpr)) {
+
+    if (SemaRef.getLangOpts().C23)
+      SemaRef.Diag(D.getDeclSpec().getConstexprSpecLoc(),
+                   diag::err_c23_constexpr_not_variable);
----------------
Fznamznon wrote:

> Hmmm, I'm not certain we need to change anything here

Well, without that move for C we silently create `FunctionDecl` and exit whether it has `constexpr` specifier or not.

> it would be more clear to say that a function cannot be constexpr in C instead of saying you declared something constexpr that isn't a variable. WDYT?

I don't seem to have a problem with this particular message, for C++ similar invalid use cases clang says and does the same. But If that will help users, ok.
I also use `err_c23_constexpr_not_variable` message when diagnosing constexpr applied to a type declaration. Should I add more specific message for this case as well?

https://github.com/llvm/llvm-project/pull/73099


More information about the cfe-commits mailing list