r251474 - clang-format: When a line is formatted, also format subsequence lines if their indent is off.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 27 18:08:22 PDT 2015


Author: djasper
Date: Tue Oct 27 20:08:22 2015
New Revision: 251474

URL: http://llvm.org/viewvc/llvm-project?rev=251474&view=rev
Log:
clang-format: When a line is formatted, also format subsequence lines if their indent is off.

Summary: This is especially important so that if a change is solely inserting a block around a few statements, clang-format-diff.py will still clean up and add indentation to the inner parts.

Reviewers: klimek

Subscribers: cfe-commits, klimek

Differential Revision: http://reviews.llvm.org/D14105

Added:
    cfe/trunk/test/Format/adjust-indent.cpp
Modified:
    cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
    cfe/trunk/test/Format/line-ranges.cpp
    cfe/trunk/test/Format/ranges.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=251474&r1=251473&r2=251474&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp Tue Oct 27 20:08:22 2015
@@ -812,13 +812,14 @@ UnwrappedLineFormatter::format(const Sma
                                    AdditionalIndent);
   const AnnotatedLine *PreviousLine = nullptr;
   const AnnotatedLine *NextLine = nullptr;
+  bool PreviousLineFormatted = false;
   for (const AnnotatedLine *Line =
            Joiner.getNextMergedLine(DryRun, IndentTracker);
        Line; Line = NextLine) {
     const AnnotatedLine &TheLine = *Line;
     unsigned Indent = IndentTracker.getIndent();
-    bool FixIndentation =
-        FixBadIndentation && (Indent != TheLine.First->OriginalColumn);
+    bool FixIndentation = (FixBadIndentation || PreviousLineFormatted) &&
+                          Indent != TheLine.First->OriginalColumn;
     bool ShouldFormat = TheLine.Affected || FixIndentation;
     // We cannot format this line; if the reason is that the line had a
     // parsing error, remember that.
@@ -845,6 +846,7 @@ UnwrappedLineFormatter::format(const Sma
       else
         Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
                        .formatLine(TheLine, Indent, DryRun);
+      PreviousLineFormatted = true;
     } else {
       // If no token in the current line is affected, we still need to format
       // affected children.
@@ -875,6 +877,7 @@ UnwrappedLineFormatter::format(const Sma
           Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
       }
       NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
+      PreviousLineFormatted = false;
     }
     if (!DryRun)
       markFinalized(TheLine.First);

Added: cfe/trunk/test/Format/adjust-indent.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/adjust-indent.cpp?rev=251474&view=auto
==============================================================================
--- cfe/trunk/test/Format/adjust-indent.cpp (added)
+++ cfe/trunk/test/Format/adjust-indent.cpp Tue Oct 27 20:08:22 2015
@@ -0,0 +1,10 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s | clang-format -lines=2:2 \
+// RUN:   | FileCheck -strict-whitespace %s
+
+void  f() {
+// CHECK: void f() {
+int i;
+// CHECK: {{^  int\ i;}}
+ int j;
+// CHECK: {{^  int\ j;}}
+}

Modified: cfe/trunk/test/Format/line-ranges.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/line-ranges.cpp?rev=251474&r1=251473&r2=251474&view=diff
==============================================================================
--- cfe/trunk/test/Format/line-ranges.cpp (original)
+++ cfe/trunk/test/Format/line-ranges.cpp Tue Oct 27 20:08:22 2015
@@ -4,8 +4,8 @@
 // CHECK: {{^int\ \*i;$}}
   int*i;
 
-// CHECK: {{^\ \ int\ \ \*\ \ i;$}}
-  int  *  i; 
+// CHECK: {{^int\ \ \*\ \ i;$}}
+int  *  i; 
 
-// CHECK: {{^\ \ int\ \*i;$}}
-  int   *   i;
+// CHECK: {{^int\ \*i;$}}
+int   *   i;

Modified: cfe/trunk/test/Format/ranges.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Format/ranges.cpp?rev=251474&r1=251473&r2=251474&view=diff
==============================================================================
--- cfe/trunk/test/Format/ranges.cpp (original)
+++ cfe/trunk/test/Format/ranges.cpp Tue Oct 27 20:08:22 2015
@@ -2,10 +2,10 @@
 // RUN:   | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \
 // RUN:   | FileCheck -strict-whitespace %s
 // CHECK: {{^int\ \*i;$}}
-  int*i;
+int*i;
 
-// CHECK: {{^\ \ int\ \ \*\ \ i;$}}
-  int  *  i; 
+// CHECK: {{^int\ \ \*\ \ i;$}}
+int  *  i; 
 
-// CHECK: {{^\ \ int\ \*i;$}}
-  int   *   i;
+// CHECK: {{^int\ \*i;$}}
+int   *   i;

Modified: cfe/trunk/unittests/Format/FormatTestSelective.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestSelective.cpp?rev=251474&r1=251473&r2=251474&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestSelective.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestSelective.cpp Tue Oct 27 20:08:22 2015
@@ -45,8 +45,14 @@ TEST_F(FormatTestSelective, RemovesTrail
 }
 
 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
-  EXPECT_EQ("int b;\nint a;", format("int b;\n   int a;", 7, 0));
-  EXPECT_EQ("int b;\n   int a;", format("int b;\n   int a;", 6, 0));
+  EXPECT_EQ("{int b;\n"
+            "  int a;\n"
+            "}",
+            format("{int b;\n  int  a;}", 8, 0));
+  EXPECT_EQ("{\n"
+            "  int b;\n"
+            "  int  a;}",
+            format("{int b;\n  int  a;}", 7, 0));
 
   Style.ColumnLimit = 12;
   EXPECT_EQ("#define A  \\\n"
@@ -84,11 +90,11 @@ TEST_F(FormatTestSelective, ReformatsMov
       "template <typename T> T *getFETokenInfo() const {\n"
       "  return static_cast<T *>(FETokenInfo);\n"
       "}\n"
-      "  int a; // <- Should not be formatted",
+      "int  a; // <- Should not be formatted",
       format(
           "template<typename T>\n"
           "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
-          "  int a; // <- Should not be formatted",
+          "int  a; // <- Should not be formatted",
           9, 5));
 }
 
@@ -142,12 +148,12 @@ TEST_F(FormatTestSelective, FormatsComme
             "       // is\n"
             "       // a\n"
             "\n"
-            "  // This is unrelated",
+            "//This is unrelated",
             format("int a; // This\n"
                    "     // is\n"
                    "     // a\n"
                    "\n"
-                   "  // This is unrelated",
+                   "//This is unrelated",
                    0, 0));
   EXPECT_EQ("int a;\n"
             "// This is\n"
@@ -310,13 +316,17 @@ TEST_F(FormatTestSelective, ReformatRegi
   EXPECT_EQ("{\n"
             "{\n"
             "  a;\n"
-            "b;\n"
+            "  b;\n"
+            "  c;\n"
+            " d;\n"
             "}\n"
             "}",
             format("{\n"
                    "{\n"
                    "     a;\n"
-                   "b;\n"
+                   "   b;\n"
+                   "  c;\n"
+                   " d;\n"
                    "}\n"
                    "}",
                    9, 2));




More information about the cfe-commits mailing list