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

Richard Smith richard at metafoo.co.uk
Mon Apr 14 17:53:07 PDT 2014



================
Comment at: lib/Parse/ParseStmt.cpp:1476-1481
@@ -1474,1 +1475,8 @@
+    SourceLocation EqualLoc;
+    if (MightBeForRangeStmt && RangeInitIsAuto) {
+      TentativeParsingAction FindEqual(*this);
+      if (SkipUntil(tok::equal, StopAtSemi | StopBeforeMatch))
+        EqualLoc = Tok.getLocation();
+      FindEqual.Revert();
+    }
     ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
----------------
It's not really OK to use a tentative parse every time you see `for (auto`. Tentative parses aren't free, and that's a really common pattern.

An alternative approach would be to track (possibly on Declarator) whether we're parsing the first variable declared in a `for` statement. In that case, when we get to the end of the initializer, if the next token is `)`, then recover by correcting the `=` to a `:`.

That'll do the wrong thing for cases like `int x[] = {1, 2, 3}; for (auto x = x)` (name lookup will find the wrong `x`), but it's close enough.


http://reviews.llvm.org/D3370






More information about the cfe-commits mailing list