clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
//===--- FormatToken.cpp - Format C++ code --------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file implements specific functions of \c FormatTokens and their
|
|
|
|
/// roles.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FormatToken.h"
|
|
|
|
#include "ContinuationIndenter.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-05-11 21:52:13 +08:00
|
|
|
#include <climits>
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2015-07-14 00:19:34 +08:00
|
|
|
const char *getTokenTypeName(TokenType Type) {
|
|
|
|
static const char *const TokNames[] = {
|
|
|
|
#define TYPE(X) #X,
|
|
|
|
LIST_TOKEN_TYPES
|
|
|
|
#undef TYPE
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Type < NUM_TOKEN_TYPES)
|
|
|
|
return TokNames[Type];
|
|
|
|
llvm_unreachable("unknown TokenType");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-01-16 17:11:55 +08:00
|
|
|
// FIXME: This is copy&pasted from Sema. Put it in a common place and remove
|
|
|
|
// duplication.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
TokenRole::~TokenRole() {}
|
|
|
|
|
|
|
|
void TokenRole::precomputeFormattingInfos(const FormatToken *Token) {}
|
|
|
|
|
2014-01-09 21:42:56 +08:00
|
|
|
unsigned CommaSeparatedList::formatAfterToken(LineState &State,
|
|
|
|
ContinuationIndenter *Indenter,
|
|
|
|
bool DryRun) {
|
clang-format: Support column layout with comment after {.
Before:
vector<int> iiiiiiiiiiiiiii = { //
1111111111, 2222222222, 33333333333, 4444444444, //
111111111, 222222222, 3333333333, 444444444, //
11111111, 22222222, 333333333, 44444444};
After:
vector<int> iiiiiiiiiiiiiii = { //
1111111111, 2222222222, 33333333333, 4444444444, //
111111111, 222222222, 3333333333, 444444444, //
11111111, 22222222, 333333333, 44444444};
llvm-svn: 237233
2015-05-13 16:16:00 +08:00
|
|
|
if (State.NextToken == nullptr || !State.NextToken->Previous)
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Ensure that we start on the opening brace.
|
clang-format: Support column layout with comment after {.
Before:
vector<int> iiiiiiiiiiiiiii = { //
1111111111, 2222222222, 33333333333, 4444444444, //
111111111, 222222222, 3333333333, 444444444, //
11111111, 22222222, 333333333, 44444444};
After:
vector<int> iiiiiiiiiiiiiii = { //
1111111111, 2222222222, 33333333333, 4444444444, //
111111111, 222222222, 3333333333, 444444444, //
11111111, 22222222, 333333333, 44444444};
llvm-svn: 237233
2015-05-13 16:16:00 +08:00
|
|
|
const FormatToken *LBrace =
|
|
|
|
State.NextToken->Previous->getPreviousNonComment();
|
|
|
|
if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->BlockKind == BK_Block ||
|
2013-10-24 18:31:50 +08:00
|
|
|
LBrace->Type == TT_DictLiteral ||
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
LBrace->Next->Type == TT_DesignatedInitializerPeriod)
|
|
|
|
return 0;
|
|
|
|
|
2013-08-27 16:43:47 +08:00
|
|
|
// Calculate the number of code points we have to format this list. As the
|
|
|
|
// first token is already placed, we have to subtract it.
|
2014-05-09 21:11:16 +08:00
|
|
|
unsigned RemainingCodePoints =
|
|
|
|
Style.ColumnLimit - State.Column + State.NextToken->Previous->ColumnWidth;
|
2013-08-27 16:43:47 +08:00
|
|
|
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
// Find the best ColumnFormat, i.e. the best number of columns to use.
|
2013-08-27 16:43:47 +08:00
|
|
|
const ColumnFormat *Format = getColumnFormat(RemainingCodePoints);
|
clang-format: Prefer column layout if possible.
Add a severe penalty for not using column layout for braced lists. If
there are solutions with column layout, these are generally preferable
over bin-packed solutions.
Before:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa,
aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,
aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a };
After:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
aaaaaaa, aaaaaaaaaa,
aaaaa, aaaaaaaaaaaaaaa,
aaa, aaaaaaaaaa,
a, aaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a
};
llvm-svn: 195546
2013-11-23 18:22:59 +08:00
|
|
|
// If no ColumnFormat can be used, the braced list would generally be
|
|
|
|
// bin-packed. Add a severe penalty to this so that column layouts are
|
2013-12-06 00:25:25 +08:00
|
|
|
// preferred if possible.
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
if (!Format)
|
clang-format: Prefer column layout if possible.
Add a severe penalty for not using column layout for braced lists. If
there are solutions with column layout, these are generally preferable
over bin-packed solutions.
Before:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa,
aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,
aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a };
After:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
aaaaaaa, aaaaaaaaaa,
aaaaa, aaaaaaaaaaaaaaa,
aaa, aaaaaaaaaa,
a, aaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a
};
llvm-svn: 195546
2013-11-23 18:22:59 +08:00
|
|
|
return 10000;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
|
|
|
|
// Format the entire list.
|
|
|
|
unsigned Penalty = 0;
|
|
|
|
unsigned Column = 0;
|
|
|
|
unsigned Item = 0;
|
|
|
|
while (State.NextToken != LBrace->MatchingParen) {
|
|
|
|
bool NewLine = false;
|
|
|
|
unsigned ExtraSpaces = 0;
|
|
|
|
|
|
|
|
// If the previous token was one of our commas, we are now on the next item.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Place token using the continuation indenter and store the penalty.
|
|
|
|
Penalty += Indenter->addTokenToState(State, NewLine, DryRun, ExtraSpaces);
|
|
|
|
}
|
|
|
|
return Penalty;
|
|
|
|
}
|
|
|
|
|
2014-01-09 21:42:56 +08:00
|
|
|
unsigned CommaSeparatedList::formatFromToken(LineState &State,
|
|
|
|
ContinuationIndenter *Indenter,
|
|
|
|
bool DryRun) {
|
|
|
|
if (HasNestedBracedList)
|
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
// Returns the lengths in code points between Begin and End (both included),
|
|
|
|
// assuming that the entire sequence is put on a single line.
|
|
|
|
static unsigned CodePointsBetween(const FormatToken *Begin,
|
|
|
|
const FormatToken *End) {
|
2013-08-26 16:10:17 +08:00
|
|
|
assert(End->TotalLength >= Begin->TotalLength);
|
2013-09-10 17:38:25 +08:00
|
|
|
return End->TotalLength - Begin->TotalLength + Begin->ColumnWidth;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
|
2013-08-26 16:10:17 +08:00
|
|
|
// FIXME: At some point we might want to do this for other lists, too.
|
2014-01-09 21:42:56 +08:00
|
|
|
if (!Token->MatchingParen || Token->isNot(tok::l_brace))
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
return;
|
|
|
|
|
clang-format: Format long lists in columns if without bin-packing.
After (even with BinPacking = false):
const Aaaaaa aaaaa = {
aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,
iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,
ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,
};
Before:
<each element on its own line>
This fixes http://llvm.org/PR20623.
llvm-svn: 215529
2014-08-13 16:46:21 +08:00
|
|
|
// In C++11 braced list style, we should not format in columns unless they
|
2015-05-26 15:26:26 +08:00
|
|
|
// have many items (20 or more) or we allow bin-packing of function call
|
|
|
|
// arguments.
|
|
|
|
if (Style.Cpp11BracedListStyle && !Style.BinPackArguments &&
|
clang-format: Format long lists in columns if without bin-packing.
After (even with BinPacking = false):
const Aaaaaa aaaaa = {
aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,
iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,
ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,
};
Before:
<each element on its own line>
This fixes http://llvm.org/PR20623.
llvm-svn: 215529
2014-08-13 16:46:21 +08:00
|
|
|
Commas.size() < 19)
|
clang-format: Respect BinPackParameters in Cpp11BracedListStyle.
With BinPackParameters=false and Cpp11BracedListStyle=true (i.e. mostly
for Chromium):
Before:
const Aaaaaa aaaaa = {aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff,
ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk};
After:
const Aaaaaa aaaaa = {aaaaa,
bbbbb,
ccccc,
ddddd,
eeeee,
ffffff,
ggggg,
hhhhhh,
iiiiii,
jjjjjj,
kkkkkk};
This fixes llvm.org/PR19359. I am not sure we'll want this in all cases
in the long run, but I'll guess we'll get feedback on that.
llvm-svn: 206458
2014-04-17 19:32:02 +08:00
|
|
|
return;
|
|
|
|
|
2014-11-27 22:40:48 +08:00
|
|
|
// Column format doesn't really make sense if we don't align after brackets.
|
|
|
|
if (!Style.AlignAfterOpenBracket)
|
|
|
|
return;
|
|
|
|
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
FormatToken *ItemBegin = Token->Next;
|
clang-format: Support column layout with comment after {.
Before:
vector<int> iiiiiiiiiiiiiii = { //
1111111111, 2222222222, 33333333333, 4444444444, //
111111111, 222222222, 3333333333, 444444444, //
11111111, 22222222, 333333333, 44444444};
After:
vector<int> iiiiiiiiiiiiiii = { //
1111111111, 2222222222, 33333333333, 4444444444, //
111111111, 222222222, 3333333333, 444444444, //
11111111, 22222222, 333333333, 44444444};
llvm-svn: 237233
2015-05-13 16:16:00 +08:00
|
|
|
while (ItemBegin->isTrailingComment())
|
|
|
|
ItemBegin = ItemBegin->Next;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
SmallVector<bool, 8> MustBreakBeforeItem;
|
|
|
|
|
|
|
|
// The lengths of an item if it is put at the end of the line. This includes
|
|
|
|
// trailing comments which are otherwise ignored for column alignment.
|
|
|
|
SmallVector<unsigned, 8> EndOfLineItemLength;
|
|
|
|
|
2015-05-15 17:41:59 +08:00
|
|
|
bool HasSeparatingComment = false;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) {
|
|
|
|
// Skip comments on their own line.
|
2015-05-15 17:41:59 +08:00
|
|
|
while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment()) {
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
ItemBegin = ItemBegin->Next;
|
2015-05-15 17:41:59 +08:00
|
|
|
HasSeparatingComment = i > 0;
|
|
|
|
}
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
|
|
|
|
MustBreakBeforeItem.push_back(ItemBegin->MustBreakBefore);
|
2013-10-24 22:14:49 +08:00
|
|
|
if (ItemBegin->is(tok::l_brace))
|
|
|
|
HasNestedBracedList = true;
|
2014-05-09 16:15:10 +08:00
|
|
|
const FormatToken *ItemEnd = nullptr;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
if (i == Commas.size()) {
|
|
|
|
ItemEnd = Token->MatchingParen;
|
|
|
|
const FormatToken *NonCommentEnd = ItemEnd->getPreviousNonComment();
|
|
|
|
ItemLengths.push_back(CodePointsBetween(ItemBegin, NonCommentEnd));
|
|
|
|
if (Style.Cpp11BracedListStyle) {
|
|
|
|
// In Cpp11 braced list style, the } and possibly other subsequent
|
|
|
|
// tokens will need to stay on a line with the last element.
|
|
|
|
while (ItemEnd->Next && !ItemEnd->Next->CanBreakBefore)
|
|
|
|
ItemEnd = ItemEnd->Next;
|
|
|
|
} else {
|
|
|
|
// In other braced lists styles, the "}" can be wrapped to the new line.
|
|
|
|
ItemEnd = Token->MatchingParen->Previous;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ItemEnd = Commas[i];
|
|
|
|
// The comma is counted as part of the item when calculating the length.
|
2013-08-26 16:10:17 +08:00
|
|
|
ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd));
|
clang-format: Prevent column layout if elements aren't uniform enough.
This patch only considers the difference between the length of the
shortest and longest element, but we might want to look at other
features (token count, etc.) in future.
Before:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
aaaaaaa, aaaaaaaaaa,
aaaaa, aaaaaaaaaaaaaaa,
aaa, aaaaaaaaaa,
a, aaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a};
After:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,
aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};
llvm-svn: 218111
2014-09-19 16:28:43 +08:00
|
|
|
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
// 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));
|
2013-08-26 16:10:17 +08:00
|
|
|
// 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;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
ItemBegin = ItemEnd->Next;
|
|
|
|
}
|
|
|
|
|
2015-05-15 17:41:59 +08:00
|
|
|
// Don't use column layout for nested lists, lists with few elements and in
|
|
|
|
// presence of separating comments.
|
|
|
|
if (Token->NestingLevel != 0 || Commas.size() < 5 || HasSeparatingComment)
|
2014-01-09 21:42:56 +08:00
|
|
|
return;
|
|
|
|
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
// We can never place more than ColumnLimit / 3 items in a row (because of the
|
|
|
|
// spaces and the comma).
|
2015-06-12 21:06:57 +08:00
|
|
|
unsigned MaxItems = Style.ColumnLimit / 3;
|
|
|
|
std::vector<unsigned> MinSizeInColumn;
|
|
|
|
MinSizeInColumn.reserve(MaxItems);
|
|
|
|
for (unsigned Columns = 1; Columns <= MaxItems; ++Columns) {
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
ColumnFormat Format;
|
|
|
|
Format.Columns = Columns;
|
|
|
|
Format.ColumnSizes.resize(Columns);
|
2015-06-12 21:06:57 +08:00
|
|
|
MinSizeInColumn.assign(Columns, UINT_MAX);
|
2013-10-24 22:14:49 +08:00
|
|
|
Format.LineCount = 1;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
bool HasRowWithSufficientColumns = false;
|
|
|
|
unsigned Column = 0;
|
|
|
|
for (unsigned i = 0, e = ItemLengths.size(); i != e; ++i) {
|
2013-10-12 05:25:45 +08:00
|
|
|
assert(i < MustBreakBeforeItem.size());
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
if (MustBreakBeforeItem[i] || Column == Columns) {
|
|
|
|
++Format.LineCount;
|
|
|
|
Column = 0;
|
|
|
|
}
|
|
|
|
if (Column == Columns - 1)
|
|
|
|
HasRowWithSufficientColumns = true;
|
clang-format: Improve column layout.
Specifically, calculate the deviation between the shortest and longest
element (which is used to prevent excessive whitespace) per column, not
overall. This automatically handles the corner cases of a single column
and a single row so that the actualy implementation becomes simpler.
Before:
vector<int> x = {1,
aaaaaaaaaaaaaaaaaaaaaa,
2,
bbbbbbbbbbbbbbbbbbbbbb,
3,
cccccccccccccccccccccc};
After:
vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,
2, bbbbbbbbbbbbbbbbbbbbbb,
3, cccccccccccccccccccccc};
llvm-svn: 236992
2015-05-11 21:35:40 +08:00
|
|
|
unsigned Length =
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
(Column == Columns - 1) ? EndOfLineItemLength[i] : ItemLengths[i];
|
clang-format: Improve column layout.
Specifically, calculate the deviation between the shortest and longest
element (which is used to prevent excessive whitespace) per column, not
overall. This automatically handles the corner cases of a single column
and a single row so that the actualy implementation becomes simpler.
Before:
vector<int> x = {1,
aaaaaaaaaaaaaaaaaaaaaa,
2,
bbbbbbbbbbbbbbbbbbbbbb,
3,
cccccccccccccccccccccc};
After:
vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,
2, bbbbbbbbbbbbbbbbbbbbbb,
3, cccccccccccccccccccccc};
llvm-svn: 236992
2015-05-11 21:35:40 +08:00
|
|
|
Format.ColumnSizes[Column] = std::max(Format.ColumnSizes[Column], Length);
|
|
|
|
MinSizeInColumn[Column] = std::min(MinSizeInColumn[Column], Length);
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
++Column;
|
|
|
|
}
|
|
|
|
// If all rows are terminated early (e.g. by trailing comments), we don't
|
|
|
|
// need to look further.
|
|
|
|
if (!HasRowWithSufficientColumns)
|
|
|
|
break;
|
|
|
|
Format.TotalWidth = Columns - 1; // Width of the N-1 spaces.
|
clang-format: Improve column layout.
Specifically, calculate the deviation between the shortest and longest
element (which is used to prevent excessive whitespace) per column, not
overall. This automatically handles the corner cases of a single column
and a single row so that the actualy implementation becomes simpler.
Before:
vector<int> x = {1,
aaaaaaaaaaaaaaaaaaaaaa,
2,
bbbbbbbbbbbbbbbbbbbbbb,
3,
cccccccccccccccccccccc};
After:
vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,
2, bbbbbbbbbbbbbbbbbbbbbb,
3, cccccccccccccccccccccc};
llvm-svn: 236992
2015-05-11 21:35:40 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < Columns; ++i)
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
Format.TotalWidth += Format.ColumnSizes[i];
|
clang-format: Improve column layout.
Specifically, calculate the deviation between the shortest and longest
element (which is used to prevent excessive whitespace) per column, not
overall. This automatically handles the corner cases of a single column
and a single row so that the actualy implementation becomes simpler.
Before:
vector<int> x = {1,
aaaaaaaaaaaaaaaaaaaaaa,
2,
bbbbbbbbbbbbbbbbbbbbbb,
3,
cccccccccccccccccccccc};
After:
vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,
2, bbbbbbbbbbbbbbbbbbbbbb,
3, cccccccccccccccccccccc};
llvm-svn: 236992
2015-05-11 21:35:40 +08:00
|
|
|
|
|
|
|
// Don't use this Format, if the difference between the longest and shortest
|
|
|
|
// element in a column exceeds a threshold to avoid excessive spaces.
|
|
|
|
if ([&] {
|
|
|
|
for (unsigned i = 0; i < Columns - 1; ++i)
|
|
|
|
if (Format.ColumnSizes[i] - MinSizeInColumn[i] > 10)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}())
|
|
|
|
continue;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
|
|
|
|
// Ignore layouts that are bound to violate the column limit.
|
|
|
|
if (Format.TotalWidth > Style.ColumnLimit)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Formats.push_back(Format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CommaSeparatedList::ColumnFormat *
|
|
|
|
CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const {
|
2014-05-09 16:15:10 +08:00
|
|
|
const ColumnFormat *BestFormat = nullptr;
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|