Fix handling of comments in macros.
authorManuel Klimek <klimek@google.com>
Wed, 6 Feb 2013 16:40:56 +0000 (16:40 +0000)
committerManuel Klimek <klimek@google.com>
Wed, 6 Feb 2013 16:40:56 +0000 (16:40 +0000)
We now correctly format:
 // Written as a macro, it is reformatted from:
 #define foo(a)                                                                \
   do {                                                                        \
     /* Initialize num to zero. */                                             \
     int num = 10;                                                             \
     /* This line ensures a is never zero. */                                  \
     int i = a == 0 ? 1 : a;                                                   \
     i = num / i; /* This division is OK. */                                   \
     return i;                                                                 \
   } while (false)

llvm-svn: 174517

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

index 5af60a4..76e912c 100644 (file)
@@ -784,7 +784,7 @@ void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
            I = CommentsBeforeNextToken.begin(),
            E = CommentsBeforeNextToken.end();
        I != E; ++I) {
-    if (I->HasUnescapedNewline && JustComments) {
+    if (I->NewlinesBefore && JustComments) {
       addUnwrappedLine();
     }
     pushToken(*I);
@@ -798,7 +798,7 @@ void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
 void UnwrappedLineParser::nextToken() {
   if (eof())
     return;
-  flushComments(FormatTok.HasUnescapedNewline);
+  flushComments(FormatTok.NewlinesBefore > 0);
   pushToken(FormatTok);
   readToken();
 }
@@ -819,7 +819,7 @@ void UnwrappedLineParser::readToken() {
     }
     if (!FormatTok.Tok.is(tok::comment))
       return;
-    if (FormatTok.HasUnescapedNewline || FormatTok.IsFirst) {
+    if (FormatTok.NewlinesBefore > 0 || FormatTok.IsFirst) {
       CommentsInCurrentLine = false;
     }
     if (CommentsInCurrentLine) {
index 4dc6ea7..d2a1bef 100644 (file)
@@ -1987,6 +1987,32 @@ TEST_F(FormatTest, BlockComments) {
           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
 }
 
+TEST_F(FormatTest, BlockCommentsInMacros) {
+  EXPECT_EQ("#define A          \\\n"
+            "  {                \\\n"
+            "    /* one line */ \\\n"
+            "    someCall();",
+            format("#define A {        \\\n"
+                   "  /* one line */   \\\n"
+                   "  someCall();", getLLVMStyleWithColumns(20)));
+  EXPECT_EQ("#define A          \\\n"
+            "  {                \\\n"
+            "    /* previous */ \\\n"
+            "    /* one line */ \\\n"
+            "    someCall();",
+            format("#define A {        \\\n"
+                   "  /* previous */   \\\n"
+                   "  /* one line */   \\\n"
+                   "  someCall();", getLLVMStyleWithColumns(20)));
+}
+
+TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
+  // FIXME: This is not what we want...
+  verifyFormat("{\n"
+               "// a"
+               "// b");
+}
+
 TEST_F(FormatTest, FormatStarDependingOnContext) {
   verifyFormat("void f(int *a);");
   verifyFormat("void f() { f(fint * b); }");