]> granicus.if.org Git - clang/commitdiff
clang-format: Be more aggressive on incorrect code.
authorDaniel Jasper <djasper@google.com>
Fri, 18 Oct 2013 17:20:57 +0000 (17:20 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 18 Oct 2013 17:20:57 +0000 (17:20 +0000)
Before, clang-format would not adjust leading indents if it found a
structural error (e.g. unmatched {}). It seems, however, that
clang-format has gotten good enough at parsing the code structure that
this hurts in almost all cases. Commonly, while writing code, it is
very useful to be able to correclty indent incomplete if statements or
for loops.

In case this leads to errors that we don't anticipate, we need to find
out and fix those.

This fixed llvm.org/PR17594.

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

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 3496712e3cf074feb6c83f340564990af1ee1f20..90c3e9e96d5bbdb3cc79757db4c4190f82836fc6 100644 (file)
@@ -915,10 +915,7 @@ public:
       } else if (TheLine.Type != LT_Invalid &&
                  (WasMoved || FormatPPDirective || touchesLine(TheLine))) {
         unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
-        if (FirstTok->WhitespaceRange.isValid() &&
-            // Insert a break even if there is a structural error in case where
-            // we break apart a line consisting of multiple unwrapped lines.
-            (FirstTok->NewlinesBefore == 0 || !StructuralError)) {
+        if (FirstTok->WhitespaceRange.isValid()) {
           formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, Indent,
                            TheLine.InPPDirective);
         } else {
index 6bcf44c4e8dbbfccc575b6ea97626808b15511f7..23a2203b015a4550aa278433ecf152334ee31a29 100644 (file)
@@ -4373,16 +4373,16 @@ TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
 }
 
 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
-  EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
+  EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
-  EXPECT_EQ("{\n  {}\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
+  EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
 
   EXPECT_EQ("{\n"
-            "    {\n"
-            " breakme(\n"
-            "     qwe);\n"
-            "}\n",
+            "  {\n"
+            "    breakme(\n"
+            "        qwe);\n"
+            "  }\n",
             format("{\n"
                    "    {\n"
                    " breakme(qwe);\n"