]> granicus.if.org Git - clang/commitdiff
clang-format: Correctly mark preprocessor lines in child blocks.
authorDaniel Jasper <djasper@google.com>
Sun, 8 Feb 2015 09:34:49 +0000 (09:34 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 8 Feb 2015 09:34:49 +0000 (09:34 +0000)
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

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp

index 4bfdaaf8c3ab12b00be3687b6d94bd8e9d1f85b4..19e591b7031ade16eb81f6e53cdf81e68524a6a2 100644 (file)
@@ -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
index 7404c85de627291f6a6d2b5f74164eb3c8964753..242d2ba15e8a09f31bf646f02fad8d2a0ba21beb 100644 (file)
@@ -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) {