r312437 - clang-format: Fix formatting of for loops with multiple increments.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 3 01:56:24 PDT 2017


Author: djasper
Date: Sun Sep  3 01:56:24 2017
New Revision: 312437

URL: http://llvm.org/viewvc/llvm-project?rev=312437&view=rev
Log:
clang-format: Fix formatting of for loops with multiple increments.

This fixes llvm.org/PR34366.

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=312437&r1=312436&r2=312437&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Sun Sep  3 01:56:24 2017
@@ -1116,6 +1116,11 @@ private:
                (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
       Contexts.back().FirstStartOfName = &Current;
       Current.Type = TT_StartOfName;
+    } else if (Current.is(tok::semi)) {
+      // Reset FirstStartOfName after finding a semicolon so that a for loop
+      // with multiple increment statements is not confused with a for loop
+      // having multiple variable declarations.
+      Contexts.back().FirstStartOfName = nullptr;
     } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
       AutoFound = true;
     } else if (Current.is(tok::arrow) &&

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=312437&r1=312436&r2=312437&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Sep  3 01:56:24 2017
@@ -657,6 +657,10 @@ TEST_F(FormatTest, FormatsForLoop) {
       "     I != E;\n"
       "     ++I) {\n}",
       NoBinPacking);
+
+  FormatStyle AlignLeft = getLLVMStyle();
+  AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
+  verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
 }
 
 TEST_F(FormatTest, RangeBasedForLoops) {




More information about the cfe-commits mailing list