[PATCH] Suggest fix-it ':' when '=' used in for-range-declaration while initializing an auto

Richard Smith richard at metafoo.co.uk
Tue May 6 18:14:18 PDT 2014


================
Comment at: include/clang/Basic/DiagnosticParseKinds.td:207
@@ -206,1 +206,3 @@
 def err_expected_semi_for : Error<"expected ';' in 'for' statement specifier">;
+def warn_single_decl_assign_in_for_range : Warning<
+  "range based for statement requires ':' after range declaration">,
----------------
This should be an `ExtWarn` or (strongly preferably) just an `Error`. I don't see any good reason to support this as an extension.

================
Comment at: include/clang/Parse/Parser.h:1728
@@ -1727,1 +1727,3 @@
+      const ParsedTemplateInfo &TemplateInfo = ParsedTemplateInfo(),
+      bool IsFirstDeclInGroup = false);
   bool ParseAsmAttributesAfterDeclarator(Declarator &D);
----------------
You don't need this new flag. Use `Declarator::isFirstDeclarator` instead.

================
Comment at: lib/Parse/ParseDecl.cpp:1830
@@ +1829,3 @@
+      if (Tok.is(tok::r_paren) && getLangOpts().CPlusPlus11 &&
+          D.getContext() == Declarator::ForContext && TypeContainsAuto &&
+          IsFirstDeclInGroup) {
----------------
Why check `TypeContainsAuto`? I don't think that's a good indicator either way.

================
Comment at: lib/Parse/ParseDecl.cpp:1832-1833
@@ +1831,4 @@
+          IsFirstDeclInGroup) {
+        Diag(EqualLoc, diag::warn_single_decl_assign_in_for_range)
+            << FixItHint::CreateReplacement(EqualLoc, ":");
+      }
----------------
Please try to avoid follow-on errors here. After giving your error message, perhaps mark `Init` as invalid, and make sure that the rest of the parsing code handles this without producing spurious diagnostics.

http://reviews.llvm.org/D3370






More information about the cfe-commits mailing list