17#include "llvm/ADT/SmallVector.h"
18#include "llvm/Support/Debug.h"
25 static const char *
const TokNames[] = {
33 llvm_unreachable(
"unknown TokenType");
39 "clock_t",
"int16_t",
"int32_t",
"int64_t",
"int8_t",
40 "intptr_t",
"ptrdiff_t",
"size_t",
"time_t",
"uint16_t",
41 "uint32_t",
"uint64_t",
"uint8_t",
"uintptr_t",
45 const bool IsCpp = LangOpts.CXXOperatorNames;
47 (IsCpp &&
is(tok::identifier) &&
57 assert(
is(tok::r_brace));
58 if (!Style.Cpp11BracedListStyle ||
63 assert(LBrace && LBrace->is(tok::l_brace));
66 if (LBrace->Previous && LBrace->Previous->is(tok::equal))
77 return is(TT_ArrayInitializerLSquare) ||
is(TT_ProtoExtensionLSquare) ||
81 (
is(tok::less) && Style.isProto());
91 if (!State.NextToken || !State.NextToken->Previous)
94 if (Formats.size() <= 1)
99 State.NextToken->Previous->getPreviousNonComment();
100 if (!LBrace || !LBrace->
isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
102 LBrace->
Next->
is(TT_DesignatedInitializerPeriod)) {
108 unsigned RemainingCodePoints =
112 const ColumnFormat *Format = getColumnFormat(RemainingCodePoints);
121 unsigned Penalty = 0;
126 unsigned ExtraSpaces = 0;
129 if (Item < Commas.size() && State.NextToken->Previous == Commas[Item]) {
130 if (!State.NextToken->isTrailingComment()) {
131 ExtraSpaces += Format->ColumnSizes[
Column] - ItemLengths[Item];
137 if (
Column == Format->Columns || State.NextToken->MustBreakBefore) {
143 Penalty +=
Indenter->addTokenToState(State,
NewLine, DryRun, ExtraSpaces);
153 if (Formats.size() == 1 || HasNestedBracedList)
154 State.Stack.back().AvoidBinPacking =
true;
162 assert(End->TotalLength >=
Begin->TotalLength);
163 return End->TotalLength -
Begin->TotalLength +
Begin->ColumnWidth;
168 if (!
Token->MatchingParen ||
169 !
Token->
isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) {
177 Commas.size() < 19) {
184 if (
Token->
is(TT_ArrayInitializerLSquare) && Commas.size() < 19)
193 ItemBegin = ItemBegin->
Next;
199 MustBreakBeforeItem.reserve(Commas.size() + 1);
200 EndOfLineItemLength.reserve(Commas.size() + 1);
201 ItemLengths.reserve(Commas.size() + 1);
203 bool HasSeparatingComment =
false;
204 for (
unsigned i = 0, e = Commas.size() + 1; i != e; ++i) {
208 ItemBegin = ItemBegin->
Next;
209 HasSeparatingComment = i > 0;
213 if (ItemBegin->
is(tok::l_brace))
214 HasNestedBracedList =
true;
216 if (i == Commas.size()) {
217 ItemEnd =
Token->MatchingParen;
225 ItemEnd = ItemEnd->
Next;
228 ItemEnd =
Token->MatchingParen->Previous;
238 ItemEnd = ItemEnd->
Next;
246 ItemBegin = ItemEnd->
Next;
251 if (Commas.size() < 5 || HasSeparatingComment)
254 if (
Token->NestingLevel != 0 &&
Token->
is(tok::l_brace) && Commas.size() < 19)
261 MinSizeInColumn.reserve(MaxItems);
262 for (
unsigned Columns = 1; Columns <= MaxItems; ++Columns) {
264 Format.Columns = Columns;
265 Format.ColumnSizes.resize(Columns);
266 MinSizeInColumn.assign(Columns,
UINT_MAX);
267 Format.LineCount = 1;
268 bool HasRowWithSufficientColumns =
false;
270 for (
unsigned i = 0, e = ItemLengths.size(); i != e; ++i) {
271 assert(i < MustBreakBeforeItem.size());
272 if (MustBreakBeforeItem[i] ||
Column == Columns) {
276 if (
Column == Columns - 1)
277 HasRowWithSufficientColumns =
true;
279 (
Column == Columns - 1) ? EndOfLineItemLength[i] : ItemLengths[i];
280 Format.ColumnSizes[
Column] = std::max(Format.ColumnSizes[
Column], Length);
281 MinSizeInColumn[
Column] = std::min(MinSizeInColumn[
Column], Length);
286 if (!HasRowWithSufficientColumns)
288 Format.TotalWidth = Columns - 1;
290 for (
unsigned i = 0; i < Columns; ++i)
291 Format.TotalWidth += Format.ColumnSizes[i];
296 for (
unsigned i = 0; i < Columns - 1; ++i)
297 if (Format.ColumnSizes[i] - MinSizeInColumn[i] > 10)
308 Formats.push_back(Format);
312const CommaSeparatedList::ColumnFormat *
313CommaSeparatedList::getColumnFormat(
unsigned RemainingCharacters)
const {
314 const ColumnFormat *BestFormat =
nullptr;
315 for (
const ColumnFormat &Format : llvm::reverse(Formats)) {
316 if (Format.TotalWidth <= RemainingCharacters || Format.Columns == 1) {
317 if (BestFormat && Format.LineCount > BestFormat->LineCount)
319 BestFormat = &Format;
328 if (Current.
is(TT_CtorInitializerComma) &&
335 ((
Previous.isNot(TT_CtorInitializerComma) ||
338 (
Previous.isNot(TT_InheritanceComma) ||
This file implements an indenter that manages the indentation of continuations.
static constexpr bool isOneOf()
static const char *const TokNames[]
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Token - This structure provides full information about a lexed token.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
bool isSimpleTypeSpecifier(const LangOptions &LangOpts) const
Determine whether the token kind starts a simple-type-specifier.
The base class of the type hierarchy.
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))