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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 15 09:37:42 PST 2024


================
@@ -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);
----------------
AaronBallman 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?

Sorry, I meant that your changes here might be fine as-is, not that no changes were needed here.

> 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?

I'm thinking more that we should use `err_constexpr_wrong_decl_kind` in C as well (I had suggested that above). But perhaps that's painful because of `consteval` and `constinit` support in C++, so maybe it's best to ignore that suggestion here and above...

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


More information about the cfe-commits mailing list