[PATCH] D76096: [clang] allow const structs to be constant expressions in initializer lists

Nick Desaulniers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 12 16:18:32 PDT 2020


nickdesaulniers marked an inline comment as done and an inline comment as not done.
nickdesaulniers added a comment.

In D76096#1920477 <https://reviews.llvm.org/D76096#1920477>, @efriedma wrote:

> But that's probably a larger project than you really want to mess with.


Maybe with some coaching; but if this is already supported today in C++, maybe it's just a small incision to support this in C?



================
Comment at: clang/lib/AST/Expr.cpp:3164
+      const QualType &QT = cast<DeclRefExpr>(this)->getDecl()->getType();
+      if (QT->isStructureType() && QT.isConstQualified())
+        return true;
----------------
efriedma wrote:
> nickdesaulniers wrote:
> > nickdesaulniers wrote:
> > > Interesting, playing with this more in godbolt, it looks like the struct doesn't even have to be const qualified.
> > Or, rather, behaves differently between C and C++ mode;
> > 
> > C -> const required
> > C++ -> const not required
> In C++, global variable initializers don't have to be constant expressions at all.
> 
> Do we really need to check GNUMode here? We try to avoid it except for cases where we would otherwise reject valid code.
> 
> Do we need to worry about arrays here?
> In C++, global variable initializers don't have to be constant expressions at all.

It looks like my test cases are supported already in Clang today, in C++ mode only and not C.  Maybe there's some alternative code path that I should be looking to reuse?

> Do we really need to check GNUMode here?

Maybe a `-Wpedantic` diag would be more appropriate otherwise? (GCC does not warn for these cases with `-Wpedantic`.  If the answer to your question is `no`, then that means supporting these regardless of language mode.  (I'm ok with that, was just being maybe overly cautious with `GNUMode`, but maybe folks with better knowledge of the language standards have better thoughts?)

> Do we need to worry about arrays here?

I don't think arrays are supported: https://godbolt.org/z/RiZPpM


================
Comment at: clang/lib/CodeGen/CGExprConstant.cpp:1013
+      if (V->hasInit())
+        return Visit(V->getInit(), V->getType());
+    return nullptr;
----------------
efriedma wrote:
> You need to be more careful here; we can call ConstExprEmitter for arbitrary expressions.
"Be more careful" how?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D76096/new/

https://reviews.llvm.org/D76096





More information about the cfe-commits mailing list