[cfe-commits] r170316 - in /cfe/trunk: lib/Format/Format.cpp lib/Format/UnwrappedLineParser.cpp lib/Format/UnwrappedLineParser.h unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Mon Dec 17 03:29:42 PST 2012


Author: djasper
Date: Mon Dec 17 05:29:41 2012
New Revision: 170316

URL: http://llvm.org/viewvc/llvm-project?rev=170316&view=rev
Log:
Fix several formatting problems.

More specifically:
- Improve formatting of static initializers.
- Fix formatting of lines comments in enums.
- Fix formmating of trailing line comments.

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=170316&r1=170315&r2=170316&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Dec 17 05:29:41 2012
@@ -734,15 +734,16 @@
   }
 
   bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) {
-    if (Right.Tok.is(tok::r_paren))
+    if (Right.Tok.is(tok::r_paren) || Right.Tok.is(tok::l_brace) ||
+        Right.Tok.is(tok::comment) || Right.Tok.is(tok::greater))
       return false;
     if (isBinaryOperator(Left))
       return true;
     if (Right.Tok.is(tok::lessless))
       return true;
-    return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) ||
-        Left.Tok.is(tok::semi) || Left.Tok.is(tok::equal) ||
-        Left.Tok.is(tok::ampamp) || Left.Tok.is(tok::pipepipe) ||
+    return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || Left.Tok.is(
+        tok::semi) || Left.Tok.is(tok::equal) || Left.Tok.is(tok::ampamp) ||
+        Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::l_brace) ||
         (Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren));
   }
 

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=170316&r1=170315&r2=170316&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Dec 17 05:29:41 2012
@@ -43,7 +43,8 @@
       parsePPDirective();
       break;
     case tok::comment:
-      parseComment();
+      nextToken();
+      addUnwrappedLine();
       break;
     case tok::l_brace:
       Error |= parseBlock();
@@ -90,22 +91,16 @@
   }
 }
 
-void UnwrappedLineParser::parseComment() {
-  while (!eof()) {
-    nextToken();
-    if (FormatTok.NewlinesBefore > 0) {
-      addUnwrappedLine();
-      return;
-    }
-  }
-}
-
-void UnwrappedLineParser::parseStatement() {
+void UnwrappedLineParser::parseComments() {
   // Consume leading line comments, e.g. for branches without compounds.
   while (FormatTok.Tok.is(tok::comment)) {
     nextToken();
     addUnwrappedLine();
   }
+}
+
+void UnwrappedLineParser::parseStatement() {
+  parseComments();
 
   switch (FormatTok.Tok.getKind()) {
   case tok::kw_namespace:
@@ -164,6 +159,12 @@
         return;
       }
       break;
+    case tok::equal:
+      nextToken();
+      // Skip initializers as they will be formatted by a later step.
+      if (FormatTok.Tok.is(tok::l_brace))
+        nextToken();
+      break;
     default:
       nextToken();
       break;
@@ -325,6 +326,7 @@
       nextToken();
       addUnwrappedLine();
       ++Line.Level;
+      parseComments();
       break;
     case tok::l_paren:
       parseParens();
@@ -332,6 +334,7 @@
     case tok::comma:
       nextToken();
       addUnwrappedLine();
+      parseComments();
       break;
     case tok::r_brace:
       if (HasContents)

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=170316&r1=170315&r2=170316&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Mon Dec 17 05:29:41 2012
@@ -96,7 +96,7 @@
   bool parseLevel();
   bool parseBlock(unsigned AddLevels = 1);
   void parsePPDirective();
-  void parseComment();
+  void parseComments();
   void parseStatement();
   void parseParens();
   void parseIfThenElse();

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=170316&r1=170315&r2=170316&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Dec 17 05:29:41 2012
@@ -245,6 +245,19 @@
   EXPECT_EQ("int i  // This is a fancy variable\n    = 5;",
             format("int i  // This is a fancy variable\n= 5;"));
 
+  EXPECT_EQ("enum E {\n"
+            "  // comment\n"
+            "  VAL_A,  // comment\n"
+            "  VAL_B\n"
+            "};",
+            format("enum E{\n// comment\nVAL_A,// comment\nVAL_B};"));
+
+  verifyFormat(
+      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
+      "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;  // Trailing comment");
+}
+
+TEST_F(FormatTest, UnderstandsMultiLineComments) {
   verifyFormat("f(/*test=*/ true);");
 }
 
@@ -319,6 +332,15 @@
                "}");
 }
 
+TEST_F(FormatTest, StaticInitializers) {
+  verifyFormat("static SomeClass SC = { 1, 'a' };");
+
+  // FIXME: Format like enums if the static initializer does not fit on a line.
+  verifyFormat(
+      "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n"
+      "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
+}
+
 //===----------------------------------------------------------------------===//
 // Line break tests.
 //===----------------------------------------------------------------------===//





More information about the cfe-commits mailing list