From a6074110bb8b1a7ae6fa718cabe08ea11f5caf24 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 26 May 2015 07:03:42 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@238182 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineFormatter.cpp | 12 ++++++++---- unittests/Format/FormatTest.cpp | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index 9c8b5ba08e..cbf8c6c922 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -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); } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 4684d1d746..5100c8e930 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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) { -- 2.40.0