clang-format: Fix child-formatting in macros.

This fixes a case where the column limit was incorrectly calculated
leading to a macro like this:

  #define A                                       \
    [] {                                          \
      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \
          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \
    }

exceeding the column limit.

llvm-svn: 238182
This commit is contained in:
Daniel Jasper 2015-05-26 07:03:42 +00:00
parent e59cd0b9ec
commit 1a02822704
2 changed files with 17 additions and 4 deletions

View File

@ -934,10 +934,14 @@ UnwrappedLineFormatter::getColumnLimit(bool InPPDirective,
// In preprocessor directives reserve two chars for trailing " \" if the
// next line continues the preprocessor directive.
bool ContinuesPPDirective =
InPPDirective && NextLine && NextLine->InPPDirective &&
// If there is an unescaped newline between this line and the next, the
// next line starts a new preprocessor directive.
!NextLine->First->HasUnescapedNewline;
InPPDirective &&
// If there is no next line, this is likely a child line and the parent
// continues the preprocessor directive.
(!NextLine ||
(NextLine->InPPDirective &&
// If there is an unescaped newline between this line and the next, the
// next line starts a new preprocessor directive.
!NextLine->First->HasUnescapedNewline));
return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);
}

View File

@ -3303,6 +3303,15 @@ TEST_F(FormatTest, FormatNestedBlocksInMacros) {
format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
" { int i; int j; })",
getGoogleStyle()));
EXPECT_EQ("#define A \\\n"
" [] { \\\n"
" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
" }",
format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
getGoogleStyle()));
}
TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {