r251824 - clang-format: Simplify and improve stop condition for formatting

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 2 12:02:49 PST 2015


Author: djasper
Date: Mon Nov  2 14:02:49 2015
New Revision: 251824

URL: http://llvm.org/viewvc/llvm-project?rev=251824&view=rev
Log:
clang-format: Simplify and improve stop condition for formatting
unaffected lines with incorrect initial indent.

Starting from:
  namespace {
    int i; // There shouldn't be indentation here.
    int j; // <- call clang-format on this line.
  }

Before:
  namespace {
    int i;
    int j;
    }

After:
  namespace {
    int i;
    int j;
  }

Modified:
    cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
    cfe/trunk/unittests/Format/FormatTestSelective.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp?rev=251824&r1=251823&r2=251824&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp Mon Nov  2 14:02:49 2015
@@ -815,8 +815,6 @@ UnwrappedLineFormatter::format(const Sma
 
   // The minimum level of consecutive lines that have been formatted.
   unsigned RangeMinLevel = UINT_MAX;
-  // The level of the previous line.
-  unsigned PreviousLineLevel = Lines.front()->Level;
 
   for (const AnnotatedLine *Line =
            Joiner.getNextMergedLine(DryRun, IndentTracker);
@@ -830,8 +828,7 @@ UnwrappedLineFormatter::format(const Sma
     // remaining file if it currently missing a closing brace.
     bool ContinueFormatting =
         TheLine.Level > RangeMinLevel ||
-        (TheLine.Level == RangeMinLevel && PreviousLineLevel <= TheLine.Level);
-    PreviousLineLevel = TheLine.Level;
+        (TheLine.Level == RangeMinLevel && !TheLine.startsWith(tok::r_brace));
 
     bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
                           Indent != TheLine.First->OriginalColumn;

Modified: cfe/trunk/unittests/Format/FormatTestSelective.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestSelective.cpp?rev=251824&r1=251823&r2=251824&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestSelective.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestSelective.cpp Mon Nov  2 14:02:49 2015
@@ -273,6 +273,27 @@ TEST_F(FormatTestSelective, IndividualSt
                    0, 0));
 }
 
+TEST_F(FormatTestSelective, WrongIndent) {
+  EXPECT_EQ("namespace {\n"
+            "int i;\n"
+            "int j;\n"
+            "}",
+            format("namespace {\n"
+                   "  int i;\n" // Format here.
+                   "  int j;\n"
+                   "}",
+                   15, 0));
+  EXPECT_EQ("namespace {\n"
+            "  int i;\n"
+            "  int j;\n"
+            "}",
+            format("namespace {\n"
+                   "  int i;\n"
+                   "  int j;\n" // Format here.
+                   "}",
+                   24, 0));
+}
+
 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
   Style.AlignEscapedNewlinesLeft = true;
   EXPECT_EQ("int  i;\n"




More information about the cfe-commits mailing list