[PATCH] FixIt for constexpr initializer_list errors

Richard Smith richard at metafoo.co.uk
Fri Feb 28 14:30:49 PST 2014


  I'm not really convinced this is the right place to issue this diagnostic. Have you considered checking for this case in ExprConstant.cpp? That way you can easily check whether the problem is an `initializer_list` object binding to a non-static-storage-duration array.


================
Comment at: lib/Sema/SemaDecl.cpp:8896
@@ +8895,3 @@
+	CXXRecordDecl *RD =
+	  baseType.withoutLocalFastQualifiers()->getAsCXXRecordDecl();
+	if (RD && RD->getName().startswith("initializer_list")) {
----------------
You don't need the `withoutLocalFastQualifiers` call here.

================
Comment at: lib/Sema/SemaDecl.cpp:8897
@@ +8896,3 @@
+	  baseType.withoutLocalFastQualifiers()->getAsCXXRecordDecl();
+	if (RD && RD->getName().startswith("initializer_list")) {
+	  SourceLocation initStart = var->getLocStart();
----------------
This isn't the right way to detect `std::initializer_list`. Use `isStdInitializerList(baseType)`.

================
Comment at: lib/Sema/SemaDecl.cpp:8901-8908
@@ +8900,10 @@
+	      var->getLocEnd(), 0, getSourceManager(), LangOptions());
+	  std::string expression(
+	      getSourceManager().getCharacterData(initStart),
+	      getSourceManager().getCharacterData(initEnd) -
+	      getSourceManager().getCharacterData(initStart));
+	  Diag(DiagLoc, diag::err_constexpr_initializer_list_requires_static)
+	    << var << baseType << Init->getSourceRange()
+	    << FixItHint::CreateReplacement(var->getSourceRange(),
+					    "static " + expression);
+	} else {
----------------
Just suggest inserting the `static`, not rewriting the whole declaration.

================
Comment at: lib/Sema/SemaDecl.cpp:8898-8899
@@ +8897,4 @@
+	if (RD && RD->getName().startswith("initializer_list")) {
+	  SourceLocation initStart = var->getLocStart();
+	  SourceLocation initEnd = Lexer::getLocForEndOfToken(
+	      var->getLocEnd(), 0, getSourceManager(), LangOptions());
----------------
Variables should have a leading capital letter. (Existing code doesn't consistently do this, sadly...)

================
Comment at: lib/Sema/SemaDecl.cpp:8897
@@ +8896,3 @@
+	  baseType.withoutLocalFastQualifiers()->getAsCXXRecordDecl();
+	if (RD && RD->getName().startswith("initializer_list")) {
+	  SourceLocation initStart = var->getLocStart();
----------------
Richard Smith wrote:
> This isn't the right way to detect `std::initializer_list`. Use `isStdInitializerList(baseType)`.
You should also check whether the variable has automatic storage duration, and whether the initializer_list's array is itself a constant expression, before issuing this diagnostic.


http://llvm-reviews.chandlerc.com/D2855



More information about the cfe-commits mailing list