r174517 - Fix handling of comments in macros.

Manuel Klimek klimek at google.com
Wed Feb 6 08:40:57 PST 2013


Author: klimek
Date: Wed Feb  6 10:40:56 2013
New Revision: 174517

URL: http://llvm.org/viewvc/llvm-project?rev=174517&view=rev
Log:
Fix handling of comments in macros.

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)

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=174517&r1=174516&r2=174517&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Feb  6 10:40:56 2013
@@ -784,7 +784,7 @@ void UnwrappedLineParser::flushComments(
            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(
 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) {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174517&r1=174516&r2=174517&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Feb  6 10:40:56 2013
@@ -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); }");





More information about the cfe-commits mailing list