diff options
author | Daniel Jasper <djasper@google.com> | 2013-08-26 08:10:17 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-08-26 08:10:17 +0000 |
commit | 8863ada85bc8c89643136f2de2fd177317526358 (patch) | |
tree | 3430390db7ac3d6362942b7f5d0c52cada31f543 /clang/lib/Format/FormatToken.cpp | |
parent | aeb642276ea0d5993ade0364b02127e6c92759b1 (diff) | |
download | llvm-8863ada85bc8c89643136f2de2fd177317526358.zip llvm-8863ada85bc8c89643136f2de2fd177317526358.tar.gz llvm-8863ada85bc8c89643136f2de2fd177317526358.tar.bz2 |
clang-format: Fix bug in column-layout formatting.
Specific arrangements of comments after trailing commas could confuse
the column width calculation, e.g. in:
vector<int> x = { a, b,
/* some */ /* comment */ };
llvm-svn: 189211
Diffstat (limited to 'clang/lib/Format/FormatToken.cpp')
-rw-r--r-- | clang/lib/Format/FormatToken.cpp | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index 1b6d360..24a296a 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -77,11 +77,13 @@ unsigned CommaSeparatedList::format(LineState &State, // assuming that the entire sequence is put on a single line. static unsigned CodePointsBetween(const FormatToken *Begin, const FormatToken *End) { + assert(End->TotalLength >= Begin->TotalLength); return End->TotalLength - Begin->TotalLength + Begin->CodePointCount; } void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { - if (!Token->MatchingParen) + // FIXME: At some point we might want to do this for other lists, too. + if (!Token->MatchingParen || Token->isNot(tok::l_brace)) return; FormatToken *ItemBegin = Token->Next; @@ -92,11 +94,6 @@ 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; @@ -119,14 +116,17 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { } else { ItemEnd = Commas[i]; // The comma is counted as part of the item when calculating the length. - ItemLengths.push_back(ItemEnd->TotalLength - ItemBegin->TotalLength + - ItemBegin->CodePointCount); + ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd)); // Consume trailing comments so the are included in EndOfLineItemLength. if (ItemEnd->Next && !ItemEnd->Next->HasUnescapedNewline && ItemEnd->Next->isTrailingComment()) ItemEnd = ItemEnd->Next; } EndOfLineItemLength.push_back(CodePointsBetween(ItemBegin, ItemEnd)); + // 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 (ItemEnd->getNextNonComment() == Token->MatchingParen) + break; ItemBegin = ItemEnd->Next; } |