r189094 - clang-format: Handle trailing commas in column layout of braced list.

Daniel Jasper djasper at google.com
Fri Aug 23 03:05:49 PDT 2013


Author: djasper
Date: Fri Aug 23 05:05:49 2013
New Revision: 189094

URL: http://llvm.org/viewvc/llvm-project?rev=189094&view=rev
Log:
clang-format: Handle trailing commas in column layout of braced list.

Before, this was causing errors.

Also exit early in breakProtrudingToken() (before the expensive call to
SourceManager::getSpellingColumnNumber()). This makes formatting huge
(100k+-item) braced lists possible.

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/FormatToken.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=189094&r1=189093&r2=189094&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Fri Aug 23 05:05:49 2013
@@ -569,6 +569,9 @@ unsigned ContinuationIndenter::moveState
 unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
                                                     LineState &State,
                                                     bool DryRun) {
+  if (!Current.isOneOf(tok::string_literal, tok::comment))
+    return 0;
+
   llvm::OwningPtr<BreakableToken> Token;
   unsigned StartColumn = State.Column - Current.CodePointCount;
   unsigned OriginalStartColumn =

Modified: cfe/trunk/lib/Format/FormatToken.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.cpp?rev=189094&r1=189093&r2=189094&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.cpp (original)
+++ cfe/trunk/lib/Format/FormatToken.cpp Fri Aug 23 05:05:49 2013
@@ -92,6 +92,11 @@ void CommaSeparatedList::precomputeForma
   SmallVector<unsigned, 8> EndOfLineItemLength;
 
   for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) {
+    // If there is a trailing comma in the list, the next item will start at the
+    // closing brace. Don't create an extra item for this.
+    if (ItemBegin == Token->MatchingParen)
+      break;
+
     // Skip comments on their own line.
     while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment())
       ItemBegin = ItemBegin->Next;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=189094&r1=189093&r2=189094&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Aug 23 05:05:49 2013
@@ -4178,6 +4178,15 @@ TEST_F(FormatTest, FormatsBracedListsinC
                "  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n"
                "};",
                getLLVMStyleWithColumns(40));
+
+  // Trailing commas.
+  verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
+               "                  1, 1, 1, 1, };",
+               getLLVMStyleWithColumns(39));
+  verifyFormat("vector<int> x = {\n"
+               "  1, 1, 1, 1, 1, 1, 1, 1, //\n"
+               "};",
+               getLLVMStyleWithColumns(39));
 }
 
 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {





More information about the cfe-commits mailing list