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

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 12 15:14:09 PDT 2020


efriedma added a comment.

I would like to kill off all the code you're modifying, and let ExprConstant be the final arbiter of whether a constant is in fact constant.  But that's probably a larger project than you really want to mess with.  The big missing piece of that is that we currently don't allow ExprConstant to evaluate structs/arrays in C, for the sake of compile-time performance. (Not sure what the performance impact for large arrays looks like at the moment.)



================
Comment at: clang/lib/AST/Expr.cpp:3164
+      const QualType &QT = cast<DeclRefExpr>(this)->getDecl()->getType();
+      if (QT->isStructureType() && QT.isConstQualified())
+        return true;
----------------
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?


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


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