[PATCH] D23385: Implement __attribute__((require_constant_initialization)) for safe static initialization.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 15 16:23:49 PDT 2016


rsmith added inline comments.

================
Comment at: include/clang/Basic/AttrDocs.td:858
@@ +857,3 @@
+  };
+  SAFE_STATIC T x = {42}; // OK.
+  SAFE_STATIC T y = 42; // error: variable does not have a constant initializer
----------------
Sure, if that's a conscious design decision for the attribute.

================
Comment at: include/clang/Basic/DiagnosticSemaKinds.td:6842
@@ +6841,3 @@
+
+def err_require_constant_init_failed : Error<
+  "variable does not have a constant initializer as required by the "
----------------
A separate note pointed at the attribute would make it clearer how the attribute got involved (especially via the macro expansion backtrace).

================
Comment at: lib/Sema/SemaDecl.cpp:10519-10520
@@ +10518,4 @@
+      auto *CE = dyn_cast<CXXConstructExpr>(Init);
+      bool DiagErr = (var->isInitKnownICE() || (CE && CE->getConstructor()->isConstexpr()))
+          ? !var->checkInitIsICE() : !checkConstInit();
+      if (DiagErr)
----------------
In C++11 onwards, `checkInitIsICE` is the right thing to use in all cases. In C++98, `checkInitIsICE` is appropriate for globals of integral type; we do not have an implementation of strict constant expression checking for other types. Perhaps just documenting that (and maybe adding a FIXME here) is the best you can do for now, assuming you're not interested in implementing the C++98 rules.


https://reviews.llvm.org/D23385





More information about the cfe-commits mailing list