#include "FormatToken.h"
#include "ContinuationIndenter.h"
#include "clang/Format/Format.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"
namespace clang {
namespace format {
bool FormatToken::isSimpleTypeSpecifier() const {
switch (Tok.getKind()) {
case tok::kw_short:
case tok::kw_long:
case tok::kw___int64:
case tok::kw___int128:
case tok::kw_signed:
case tok::kw_unsigned:
case tok::kw_void:
case tok::kw_char:
case tok::kw_int:
case tok::kw_half:
case tok::kw_float:
case tok::kw_double:
case tok::kw_wchar_t:
case tok::kw_bool:
case tok::kw___underlying_type:
case tok::annot_typename:
case tok::kw_char16_t:
case tok::kw_char32_t:
case tok::kw_typeof:
case tok::kw_decltype:
return true;
default:
return false;
}
}
TokenRole::~TokenRole() {}
void TokenRole::precomputeFormattingInfos(const FormatToken *Token) {}
unsigned CommaSeparatedList::formatAfterToken(LineState &State,
ContinuationIndenter *Indenter,
bool DryRun) {
if (!State.NextToken->Previous || !State.NextToken->Previous->Previous)
return 0;
const FormatToken *LBrace = State.NextToken->Previous->Previous;
if (LBrace->isNot(tok::l_brace) ||
LBrace->BlockKind == BK_Block ||
LBrace->Type == TT_DictLiteral ||
LBrace->Next->Type == TT_DesignatedInitializerPeriod)
return 0;
unsigned RemainingCodePoints = Style.ColumnLimit - State.Column +
State.NextToken->Previous->ColumnWidth;
const ColumnFormat *Format = getColumnFormat(RemainingCodePoints);
if (!Format)
return 10000;
unsigned Penalty = 0;
unsigned Column = 0;
unsigned Item = 0;
while (State.NextToken != LBrace->MatchingParen) {
bool NewLine = false;
unsigned ExtraSpaces = 0;
if (Item < Commas.size() && State.NextToken->Previous == Commas[Item]) {
if (!State.NextToken->isTrailingComment()) {
ExtraSpaces += Format->ColumnSizes[Column] - ItemLengths[Item];
++Column;
}
++Item;
}
if (Column == Format->Columns || State.NextToken->MustBreakBefore) {
Column = 0;
NewLine = true;
}
Penalty += Indenter->addTokenToState(State, NewLine, DryRun, ExtraSpaces);
}
return Penalty;
}
unsigned CommaSeparatedList::formatFromToken(LineState &State,
ContinuationIndenter *Indenter,
bool DryRun) {
if (HasNestedBracedList)
State.Stack.back().AvoidBinPacking = true;
return 0;
}
static unsigned CodePointsBetween(const FormatToken *Begin,
const FormatToken *End) {
assert(End->TotalLength >= Begin->TotalLength);
return End->TotalLength - Begin->TotalLength + Begin->ColumnWidth;
}
void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
if (!Token->MatchingParen || Token->isNot(tok::l_brace))
return;
FormatToken *ItemBegin = Token->Next;
SmallVector<bool, 8> MustBreakBeforeItem;
SmallVector<unsigned, 8> EndOfLineItemLength;
for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) {
while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment())
ItemBegin = ItemBegin->Next;
MustBreakBeforeItem.push_back(ItemBegin->MustBreakBefore);
if (ItemBegin->is(tok::l_brace))
HasNestedBracedList = true;
const FormatToken *ItemEnd = NULL;
if (i == Commas.size()) {
ItemEnd = Token->MatchingParen;
const FormatToken *NonCommentEnd = ItemEnd->getPreviousNonComment();
ItemLengths.push_back(CodePointsBetween(ItemBegin, NonCommentEnd));
if (Style.Cpp11BracedListStyle) {
while (ItemEnd->Next && !ItemEnd->Next->CanBreakBefore)
ItemEnd = ItemEnd->Next;
} else {
ItemEnd = Token->MatchingParen->Previous;
}
} else {
ItemEnd = Commas[i];
ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd));
if (ItemEnd->Next && !ItemEnd->Next->HasUnescapedNewline &&
ItemEnd->Next->isTrailingComment())
ItemEnd = ItemEnd->Next;
}
EndOfLineItemLength.push_back(CodePointsBetween(ItemBegin, ItemEnd));
if (ItemEnd->getNextNonComment() == Token->MatchingParen)
break;
ItemBegin = ItemEnd->Next;
}
if (HasNestedBracedList || Commas.size() < 5 ||
Token->NestingLevel != 0)
return;
for (unsigned Columns = 1; Columns <= Style.ColumnLimit / 3; ++Columns) {
ColumnFormat Format;
Format.Columns = Columns;
Format.ColumnSizes.resize(Columns);
Format.LineCount = 1;
bool HasRowWithSufficientColumns = false;
unsigned Column = 0;
for (unsigned i = 0, e = ItemLengths.size(); i != e; ++i) {
assert(i < MustBreakBeforeItem.size());
if (MustBreakBeforeItem[i] || Column == Columns) {
++Format.LineCount;
Column = 0;
}
if (Column == Columns - 1)
HasRowWithSufficientColumns = true;
unsigned length =
(Column == Columns - 1) ? EndOfLineItemLength[i] : ItemLengths[i];
Format.ColumnSizes[Column] =
std::max(Format.ColumnSizes[Column], length);
++Column;
}
if (!HasRowWithSufficientColumns)
break;
Format.TotalWidth = Columns - 1; for (unsigned i = 0; i < Columns; ++i) {
Format.TotalWidth += Format.ColumnSizes[i];
}
if (Format.TotalWidth > Style.ColumnLimit)
continue;
Formats.push_back(Format);
}
}
const CommaSeparatedList::ColumnFormat *
CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const {
const ColumnFormat *BestFormat = NULL;
for (SmallVector<ColumnFormat, 4>::const_reverse_iterator
I = Formats.rbegin(),
E = Formats.rend();
I != E; ++I) {
if (I->TotalWidth <= RemainingCharacters) {
if (BestFormat && I->LineCount > BestFormat->LineCount)
break;
BestFormat = &*I;
}
}
return BestFormat;
}
} }