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

Richard Smith richard at metafoo.co.uk
Wed May 7 16:50:56 PDT 2014


Looks good with some minor tweaks.

================
Comment at: lib/Parse/ParseDecl.cpp:1835-1836
@@ +1834,4 @@
+      // our best guess is that the user meant ':' instead of '='.
+      if (Tok.is(tok::r_paren) && getLangOpts().CPlusPlus11 &&
+          D.getContext() == Declarator::ForContext && D.isFirstDeclarator()) {
+        Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
----------------
Instead of checking for C++11 and `ForContext` here, just check `FRI`.

================
Comment at: lib/Parse/ParseDecl.cpp:1839-1841
@@ +1838,5 @@
+            << FixItHint::CreateReplacement(
+                EqualLoc.isMacroID()
+                    ? PP.getSourceManager().getSpellingLoc(EqualLoc)
+                    : EqualLoc,
+                ":");
----------------
Please don't deviate from the usual convention here -- if we choose to apply typo-corrections within macros, that should be a centralized decision -- just pass in `EqualLoc`.

================
Comment at: lib/Parse/ParseDecl.cpp:1845-1846
@@ +1844,4 @@
+        // statement, therefore preventing spurious errors to be issued.
+        FRI->ColonLoc = EqualLoc;
+        Init = ExprError();
+      }
----------------
Also `FRI->RangeExpr = Init;` ?

================
Comment at: lib/Parse/ParseDecl.cpp:1855-1856
@@ -1837,3 +1854,4 @@
       if (Init.isInvalid()) {
-        SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
+        if (!FRI || FRI->ColonLoc.isInvalid() || Tok.isNot(tok::r_paren))
+          SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
         Actions.ActOnInitializerError(ThisDecl);
----------------
I think the right thing to do here is to add `tok::r_paren` to the stop set if the context is `ForContext`.

http://reviews.llvm.org/D3370






More information about the cfe-commits mailing list