]> granicus.if.org Git - clang/commitdiff
Fixed handling of comments before preprocessor directives.
authorAlexander Kornienko <alexfh@google.com>
Thu, 28 Mar 2013 18:40:55 +0000 (18:40 +0000)
committerAlexander Kornienko <alexfh@google.com>
Thu, 28 Mar 2013 18:40:55 +0000 (18:40 +0000)
Comments before preprocessor directives used to be stored with InPPDirective
flag set, which prevented correct comment splitting in this case. Fixed by
flushing comments before switching on InPPDirective. Added a new test and fixed
one of the existing tests.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@178261 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 8408ce3a0d8f64fee7a8e6733ac0c24f5a181be7..b24d5141e0a8f4ca100476c835a08e75129c3a90 100644 (file)
@@ -822,6 +822,7 @@ void UnwrappedLineParser::readToken() {
     while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
            ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
             FormatTok.IsFirst)) {
+      flushComments(FormatTok.NewlinesBefore > 0);
       // If there is an unfinished unwrapped line, we flush the preprocessor
       // directives only after that unwrapped line was finished later.
       bool SwitchToPreprocessorLines =
index 7fbb9f3ac4e305b6aeb3e0b2698090a0a370be7d..83c7a2adbf221d96325be351082e9336e368c1cf 100644 (file)
@@ -699,6 +699,13 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
       "//Don't add leading\n"
       "//whitespace",
       format("//Don't add leading whitespace", getLLVMStyleWithColumns(20)));
+  EXPECT_EQ("// A comment before\n"
+            "// a macro\n"
+            "// definition\n"
+            "#define a b",
+            format("// A comment before a macro definition\n"
+                   "#define a b",
+                   getLLVMStyleWithColumns(20)));
 }
 
 TEST_F(FormatTest, SplitsLongLinesInComments) {
@@ -1203,13 +1210,13 @@ TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
 }
 
 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
-  EXPECT_EQ("// some comment\n"
+  EXPECT_EQ("// somecomment\n"
             "#include \"a.h\"\n"
             "#define A(  \\\n"
             "    A, B)\n"
             "#include \"b.h\"\n"
             "// somecomment\n",
-            format("  // some comment\n"
+            format("  // somecomment\n"
                    "  #include \"a.h\"\n"
                    "#define A(A,\\\n"
                    "    B)\n"