From: Daniel Jasper Date: Wed, 28 Aug 2013 09:07:32 +0000 (+0000) Subject: clang-format: Fix infinite loop in macro special case. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c9346c996c9d5de130541c84206c4a4d5bba24e9;p=clang clang-format: Fix infinite loop in macro special case. 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 --- diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index 94aca027b8..41519b64c4 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -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; } } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index f5afbd3d7c..d80011ba17 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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"