]> granicus.if.org Git - clang/commitdiff
clang-format: Fix infinite loop in macro special case.
authorDaniel Jasper <djasper@google.com>
Wed, 28 Aug 2013 09:07:32 +0000 (09:07 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 28 Aug 2013 09:07:32 +0000 (09:07 +0000)
If escaped newlines are aligned right
(FormatStyle.AlignEscapedNewlinesLeft == false), and a line contained
too many characters to fit into the column limit, this would result in
a (virtually) endless loop creating a negative number of spaces.

Instead, allow the escaped newlines to be pushed past the column limit
in this case.

This fixes llvm.org/PR16515.

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

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

index 94aca027b8cf9c8c67d4ea4dd695befa328fff12..41519b64c463360f506d8e95f73c0b42ba687a27 100644 (file)
@@ -185,19 +185,17 @@ void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
 }
 
 void WhitespaceManager::alignEscapedNewlines() {
-  unsigned MaxEndOfLine = 0;
+  unsigned MaxEndOfLine =
+      Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
   unsigned StartOfMacro = 0;
   for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
     Change &C = Changes[i];
     if (C.NewlinesBefore > 0) {
       if (C.ContinuesPPDirective) {
-        if (Style.AlignEscapedNewlinesLeft)
-          MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
-        else
-          MaxEndOfLine = Style.ColumnLimit;
+        MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
       } else {
         alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
-        MaxEndOfLine = 0;
+        MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
         StartOfMacro = i;
       }
     }
index f5afbd3d7c4978a3700f2a6bdd9e98a2c334b525..d80011ba1755132f71f68b037fdd07b749404b74 100644 (file)
@@ -1814,7 +1814,7 @@ TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
   // FIXME: We never break before the macro name.
-  verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
+  verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
 
   verifyFormat("#define A A\n#define A A");
   verifyFormat("#define A(X) A\n#define A A");
@@ -1887,9 +1887,9 @@ TEST_F(FormatTest, MacroDefinitionInsideStatement) {
 
 TEST_F(FormatTest, HashInMacroDefinition) {
   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
-  verifyFormat("#define A \\\n"
-               "  {       \\\n"
-               "    f(#c);\\\n"
+  verifyFormat("#define A  \\\n"
+               "  {        \\\n"
+               "    f(#c); \\\n"
                "  }",
                getLLVMStyleWithColumns(11));
 
@@ -4396,7 +4396,7 @@ TEST_F(FormatTest, BlockComments) {
   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
             format("/* *//* */  /* */\n/* *//* */  /* */"));
   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
-  EXPECT_EQ("#define A /*123*/\\\n"
+  EXPECT_EQ("#define A /*123*/ \\\n"
             "  b\n"
             "/* */\n"
             "someCall(\n"