Skip to content

Commit

Permalink
[clang-format][NFC] Be more careful about the layout of FormatToken.
Browse files Browse the repository at this point in the history
The underlying ABI forces FormatToken to have a lot of padding.

Currently (on x86-64 linux) `sizeof(FormatToken) == 288`. After this patch
`sizeof(FormatToken) == 232`.

No functional changes.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D84306
  • Loading branch information
riccibruno committed Jul 28, 2020
1 parent e2f5444 commit f5acd11
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 140 deletions.
20 changes: 9 additions & 11 deletions clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -284,7 +284,7 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
// The opening "{" of a braced list has to be on the same line as the first
// element if it is nested in another braced init list or function call.
if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
Previous.isNot(TT_DictLiteral) && Previous.BlockKind == BK_BracedInit &&
Previous.isNot(TT_DictLiteral) && Previous.is(BK_BracedInit) &&
Previous.Previous &&
Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
return false;
Expand Down Expand Up @@ -501,7 +501,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
// The following could be precomputed as they do not depend on the state.
// However, as they should take effect only if the UnwrappedLine does not fit
// into the ColumnLimit, they are checked here in the ContinuationIndenter.
if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block &&
if (Style.ColumnLimit != 0 && Previous.is(BK_Block) &&
Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
return true;

Expand Down Expand Up @@ -627,7 +627,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// opening parenthesis. Don't break if it doesn't conserve columns.
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak &&
(Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
(Previous.is(tok::l_brace) && Previous.BlockKind != BK_Block &&
(Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
Style.Cpp11BracedListStyle)) &&
State.Column > getNewLineColumn(State) &&
(!Previous.Previous || !Previous.Previous->isOneOf(
Expand All @@ -648,7 +648,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!State.Stack.back().IsCSharpGenericTypeConstraint &&
Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) &&
(Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit)) {
(Current.isNot(TT_LineComment) || Previous.is(BK_BracedInit))) {
State.Stack.back().Indent = State.Column + Spaces;
State.Stack.back().IsAligned = true;
}
Expand Down Expand Up @@ -972,7 +972,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
return (Style.IndentWidth * State.Line->First->IndentLevel) +
Style.IndentWidth;

if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
if (NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block))
return Current.NestingLevel == 0 ? State.FirstIndent
: State.Stack.back().Indent;
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
Expand All @@ -982,8 +982,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
State.Stack.size() > 1) {
if (Current.closesBlockOrBlockTypeList(Style))
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
if (Current.MatchingParen &&
Current.MatchingParen->BlockKind == BK_BracedInit)
if (Current.MatchingParen && Current.MatchingParen->is(BK_BracedInit))
return State.Stack[State.Stack.size() - 2].LastSpace;
return State.FirstIndent;
}
Expand Down Expand Up @@ -1417,7 +1416,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
State.Stack.back().IsCSharpGenericTypeConstraint)
return;

if (Current.MatchingParen && Current.BlockKind == BK_Block) {
if (Current.MatchingParen && Current.is(BK_Block)) {
moveStateToNewBlock(State);
return;
}
Expand Down Expand Up @@ -1486,9 +1485,8 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
(State.Line->MustBeDeclaration && !BinPackDeclaration) ||
(!State.Line->MustBeDeclaration && !Style.BinPackArguments) ||
(Style.ExperimentalAutoDetectBinPacking &&
(Current.PackingKind == PPK_OnePerLine ||
(!BinPackInconclusiveFunctions &&
Current.PackingKind == PPK_Inconclusive)));
(Current.is(PPK_OnePerLine) ||
(!BinPackInconclusiveFunctions && Current.is(PPK_Inconclusive))));

if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen &&
Style.ObjCBreakBeforeNestedBlockParam) {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Format/Format.cpp
Expand Up @@ -1575,9 +1575,9 @@ class Formatter : public TokenAnalyzer {
continue;
FormatToken *Tok = AnnotatedLines[i]->First->Next;
while (Tok->Next) {
if (Tok->PackingKind == PPK_BinPacked)
if (Tok->is(PPK_BinPacked))
HasBinPackedFunction = true;
if (Tok->PackingKind == PPK_OnePerLine)
if (Tok->is(PPK_OnePerLine))
HasOnePerLineFunction = true;

Tok = Tok->Next;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Format/FormatToken.cpp
Expand Up @@ -85,8 +85,8 @@ unsigned CommaSeparatedList::formatAfterToken(LineState &State,
const FormatToken *LBrace =
State.NextToken->Previous->getPreviousNonComment();
if (!LBrace || !LBrace->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
LBrace->BlockKind == BK_Block || LBrace->getType() == TT_DictLiteral ||
LBrace->Next->getType() == TT_DesignatedInitializerPeriod)
LBrace->is(BK_Block) || LBrace->is(TT_DictLiteral) ||
LBrace->Next->is(TT_DesignatedInitializerPeriod))
return 0;

// Calculate the number of code points we have to format this list. As the
Expand Down

0 comments on commit f5acd11

Please sign in to comment.