From: Daniel Jasper Date: Sun, 8 Feb 2015 09:34:49 +0000 (+0000) Subject: clang-format: Correctly mark preprocessor lines in child blocks. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=41397d283c5fd39b7b6ef2f29d6a0feb4169730b;p=clang clang-format: Correctly mark preprocessor lines in child blocks. This prevents contracting: auto lambda = []() { int a = 2 #if A + 2 #endif ; }; into: auto lambda = []() { int a = 2 #if A + 2 #endif ; }; Which is obviously BAD. This fixes llvm.org/PR22496. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@228522 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 4bfdaaf8c3..19e591b703 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -1677,8 +1677,7 @@ void UnwrappedLineParser::readToken() { (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { // If there is an unfinished unwrapped line, we flush the preprocessor // directives only after that unwrapped line was finished later. - bool SwitchToPreprocessorLines = - !Line->Tokens.empty() && CurrentLines == &Lines; + bool SwitchToPreprocessorLines = !Line->Tokens.empty(); ScopedLineState BlockState(*this, SwitchToPreprocessorLines); // Comments stored before the preprocessor directive need to be output // before the preprocessor directive, at the same level as the diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 7404c85de6..242d2ba15e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -9529,6 +9529,13 @@ TEST_F(FormatTest, FormatsLambdas) { " doo_dah();\n" " })) {\n" "}"); + verifyFormat("auto lambda = []() {\n" + " int a = 2\n" + "#if A\n" + " + 2\n" + "#endif\n" + " ;\n" + "};"); } TEST_F(FormatTest, FormatsBlocks) {