]> granicus.if.org Git - clang/commitdiff
clang-format: Handle trailing commas in column layout of braced list.
authorDaniel Jasper <djasper@google.com>
Fri, 23 Aug 2013 10:05:49 +0000 (10:05 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 23 Aug 2013 10:05:49 +0000 (10:05 +0000)
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.

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

lib/Format/ContinuationIndenter.cpp
lib/Format/FormatToken.cpp
unittests/Format/FormatTest.cpp

index 8f7bb50060ef6c1092411fca6db8ce7a602b024a..005eec82b7bb0237b05a31632fcb86ad1fdcf028 100644 (file)
@@ -569,6 +569,9 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
 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 =
index 4e232afda52de26f0c5844af5e50c5e36575ac24..1b6d360190510d9d88252bfafaa59573a00a049c 100644 (file)
@@ -92,6 +92,11 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
   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;
index 252364ec23971b90bc9b0e8301eacc3714471bf4..94d7b34632afebe92e22c945a67c7cf2169e65e4 100644 (file)
@@ -4178,6 +4178,15 @@ TEST_F(FormatTest, FormatsBracedListsinColumnLayout) {
                "  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) {