From: Daniel Jasper Date: Fri, 23 Aug 2013 10:05:49 +0000 (+0000) Subject: clang-format: Handle trailing commas in column layout of braced list. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ed51c02f4c87ddb2d2f45193e4041921ac363f76;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189094 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 8f7bb50060..005eec82b7 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -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 Token; unsigned StartColumn = State.Column - Current.CodePointCount; unsigned OriginalStartColumn = diff --git a/lib/Format/FormatToken.cpp b/lib/Format/FormatToken.cpp index 4e232afda5..1b6d360190 100644 --- a/lib/Format/FormatToken.cpp +++ b/lib/Format/FormatToken.cpp @@ -92,6 +92,11 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { SmallVector 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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 252364ec23..94d7b34632 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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 x = { 1, 1, 1, 1,\n" + " 1, 1, 1, 1, };", + getLLVMStyleWithColumns(39)); + verifyFormat("vector x = {\n" + " 1, 1, 1, 1, 1, 1, 1, 1, //\n" + "};", + getLLVMStyleWithColumns(39)); } TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {