]> granicus.if.org Git - clang/commitdiff
clang-format: Simplify and improve stop condition for formatting
authorDaniel Jasper <djasper@google.com>
Mon, 2 Nov 2015 20:02:49 +0000 (20:02 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 2 Nov 2015 20:02:49 +0000 (20:02 +0000)
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;
  }

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251824 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineFormatter.cpp
unittests/Format/FormatTestSelective.cpp

index 5b4f5d5b09c331a559a67d0ac35636c7716ffa08..04087e84871ada194f50d2882c536fcfd9d6fd44 100644 (file)
@@ -815,8 +815,6 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
 
   // 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 SmallVectorImpl<AnnotatedLine *> &Lines,
     // 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;
index 9c4d1c3d2b011ad50a7026b9442afe5ad3b0700f..d53d1c0429211d5d1715715cd6ff2b93295fac85 100644 (file)
@@ -273,6 +273,27 @@ TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
                    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"