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. llvm-svn: 189459
This commit is contained in:
parent
be133a8757
commit
a49393f54d
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue