aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-09-02 21:17:18 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-12-08 17:34:50 +0000
commit06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e (patch)
tree62f873df87c7c675557a179e0c4c83fe9f3087bc /contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp
parentcf037972ea8863e2bab7461d77345367d2c1e054 (diff)
parent7fa27ce4a07f19b07799a767fc29416f3b625afb (diff)
Diffstat (limited to 'contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp')
-rw-r--r--contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp89
1 files changed, 67 insertions, 22 deletions
diff --git a/contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp b/contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp
index 412c57b850b5..0ca297a5f957 100644
--- a/contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp
+++ b/contrib/llvm-project/clang/lib/Format/ContinuationIndenter.cpp
@@ -18,6 +18,7 @@
#include "WhitespaceManager.h"
#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/TokenKinds.h"
#include "clang/Format/Format.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Debug.h"
@@ -356,11 +357,14 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
if (Current.MustBreakBefore ||
(Current.is(TT_InlineASMColon) &&
(Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always ||
- Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_OnlyMultiline))) {
+ (Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_OnlyMultiline &&
+ Style.ColumnLimit > 0)))) {
return true;
}
if (CurrentState.BreakBeforeClosingBrace &&
- Current.closesBlockOrBlockTypeList(Style)) {
+ (Current.closesBlockOrBlockTypeList(Style) ||
+ (Current.is(tok::r_brace) &&
+ Current.isBlockIndentedInitRBrace(Style)))) {
return true;
}
if (CurrentState.BreakBeforeClosingParen && Current.is(tok::r_paren))
@@ -613,10 +617,10 @@ unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
assert(!State.Stack.empty());
State.NoContinuation = false;
- if ((Current.is(TT_ImplicitStringLiteral) &&
- (Previous.Tok.getIdentifierInfo() == nullptr ||
- Previous.Tok.getIdentifierInfo()->getPPKeywordID() ==
- tok::pp_not_keyword))) {
+ if (Current.is(TT_ImplicitStringLiteral) &&
+ (!Previous.Tok.getIdentifierInfo() ||
+ Previous.Tok.getIdentifierInfo()->getPPKeywordID() ==
+ tok::pp_not_keyword)) {
unsigned EndColumn =
SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
if (Current.LastNewlineOffset != 0) {
@@ -739,10 +743,16 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
if (Previous.is(TT_TemplateString) && Previous.opensScope())
CurrentState.NoLineBreak = true;
+ // Align following lines within parentheses / brackets if configured.
+ // Note: This doesn't apply to macro expansion lines, which are MACRO( , , )
+ // with args as children of the '(' and ',' tokens. It does not make sense to
+ // align the commas with the opening paren.
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
- (Current.isNot(TT_LineComment) || Previous.is(BK_BracedInit))) {
+ !(Current.MacroParent && Previous.MacroParent) &&
+ (Current.isNot(TT_LineComment) ||
+ Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen))) {
CurrentState.Indent = State.Column + Spaces;
CurrentState.IsAligned = true;
}
@@ -1053,13 +1063,16 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
bool PreviousIsBreakingCtorInitializerColon =
PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
+ bool AllowAllConstructorInitializersOnNextLine =
+ Style.PackConstructorInitializers == FormatStyle::PCIS_NextLine ||
+ Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly;
if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
PreviousIsBreakingCtorInitializerColon) ||
(!Style.AllowAllParametersOfDeclarationOnNextLine &&
State.Line->MustBeDeclaration) ||
(!Style.AllowAllArgumentsOnNextLine &&
!State.Line->MustBeDeclaration) ||
- (Style.PackConstructorInitializers != FormatStyle::PCIS_NextLine &&
+ (!AllowAllConstructorInitializersOnNextLine &&
PreviousIsBreakingCtorInitializerColon) ||
Previous.is(TT_DictLiteral)) {
CurrentState.BreakBeforeParameter = true;
@@ -1069,7 +1082,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
// and we allow all arguments on the next line, we should not break
// before the next parameter.
if (PreviousIsBreakingCtorInitializerColon &&
- Style.PackConstructorInitializers == FormatStyle::PCIS_NextLine) {
+ AllowAllConstructorInitializersOnNextLine) {
CurrentState.BreakBeforeParameter = false;
}
}
@@ -1116,8 +1129,15 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
Style.IndentWidth;
}
- if (NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block))
- return Current.NestingLevel == 0 ? State.FirstIndent : CurrentState.Indent;
+ if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) ||
+ (Style.isVerilog() && Keywords.isVerilogBegin(*NextNonComment))) {
+ if (Current.NestingLevel == 0 ||
+ (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
+ State.NextToken->is(TT_LambdaLBrace))) {
+ return State.FirstIndent;
+ }
+ return CurrentState.Indent;
+ }
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
(Current.is(tok::greater) &&
(Style.Language == FormatStyle::LK_Proto ||
@@ -1150,12 +1170,23 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
- Current.is(tok::r_paren) && State.Stack.size() > 1) {
+ (Current.is(tok::r_paren) ||
+ (Current.is(tok::r_brace) &&
+ Current.MatchingParen->is(BK_BracedInit))) &&
+ State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
return State.Stack[State.Stack.size() - 2].LastSpace;
+ // Field labels in a nested type should be aligned to the brace. For example
+ // in ProtoBuf:
+ // optional int32 b = 2 [(foo_options) = {aaaaaaaaaaaaaaaaaaa: 123,
+ // bbbbbbbbbbbbbbbbbbbbbbbb:"baz"}];
+ // For Verilog, a quote following a brace is treated as an identifier. And
+ // Both braces and colons get annotated as TT_DictLiteral. So we have to
+ // check.
if (Current.is(tok::identifier) && Current.Next &&
+ (!Style.isVerilog() || Current.Next->is(tok::colon)) &&
(Current.Next->is(TT_DictLiteral) ||
((Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto) &&
@@ -1264,8 +1295,13 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
return ContinuationIndent;
}
- if (State.Line->InPragmaDirective)
- return CurrentState.Indent + Style.ContinuationIndentWidth;
+ // OpenMP clauses want to get additional indentation when they are pushed onto
+ // the next line.
+ if (State.Line->InPragmaDirective) {
+ FormatToken *PragmaType = State.Line->First->Next->Next;
+ if (PragmaType && PragmaType->TokenText.equals("omp"))
+ return CurrentState.Indent + Style.ContinuationIndentWidth;
+ }
// This ensure that we correctly format ObjC methods calls without inputs,
// i.e. where the last element isn't selector like: [callee method];
@@ -1401,7 +1437,8 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
if (Style.PackConstructorInitializers > FormatStyle::PCIS_BinPack) {
CurrentState.AvoidBinPacking = true;
CurrentState.BreakBeforeParameter =
- Style.PackConstructorInitializers != FormatStyle::PCIS_NextLine;
+ Style.PackConstructorInitializers != FormatStyle::PCIS_NextLine &&
+ Style.PackConstructorInitializers != FormatStyle::PCIS_NextLineOnly;
} else {
CurrentState.BreakBeforeParameter = false;
}
@@ -1644,10 +1681,14 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
if (Current.opensBlockOrBlockTypeList(Style)) {
NewIndent = Style.IndentWidth +
std::min(State.Column, CurrentState.NestedBlockIndent);
+ } else if (Current.is(tok::l_brace)) {
+ NewIndent =
+ CurrentState.LastSpace + Style.BracedInitializerIndentWidth.value_or(
+ Style.ContinuationIndentWidth);
} else {
NewIndent = CurrentState.LastSpace + Style.ContinuationIndentWidth;
}
- const FormatToken *NextNoComment = Current.getNextNonComment();
+ const FormatToken *NextNonComment = Current.getNextNonComment();
bool EndsInComma = Current.MatchingParen &&
Current.MatchingParen->Previous &&
Current.MatchingParen->Previous->is(tok::comma);
@@ -1655,9 +1696,9 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
Style.Language == FormatStyle::LK_Proto ||
Style.Language == FormatStyle::LK_TextProto ||
!Style.BinPackArguments ||
- (NextNoComment &&
- NextNoComment->isOneOf(TT_DesignatedInitializerPeriod,
- TT_DesignatedInitializerLSquare));
+ (NextNonComment && NextNonComment->isOneOf(
+ TT_DesignatedInitializerPeriod,
+ TT_DesignatedInitializerLSquare));
BreakBeforeParameter = EndsInComma;
if (Current.ParameterCount > 1)
NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
@@ -1746,11 +1787,11 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
NewState.BreakBeforeParameter = BreakBeforeParameter;
NewState.HasMultipleNestedBlocks = (Current.BlockParameterCount > 1);
- if (Style.BraceWrapping.BeforeLambdaBody && Current.Next != nullptr &&
+ if (Style.BraceWrapping.BeforeLambdaBody && Current.Next &&
Current.is(tok::l_paren)) {
// Search for any parameter that is a lambda.
FormatToken const *next = Current.Next;
- while (next != nullptr) {
+ while (next) {
if (next->is(TT_LambdaLSquare)) {
NewState.HasMultipleNestedBlocks = true;
break;
@@ -1815,6 +1856,10 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
}
void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
+ if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
+ State.NextToken->is(TT_LambdaLBrace)) {
+ State.Stack.back().NestedBlockIndent = State.FirstIndent;
+ }
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
// ObjC block sometimes follow special indentation rules.
unsigned NewIndent =
@@ -2165,7 +2210,7 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current,
Current, StartColumn, Current.OriginalColumn, !Current.Previous,
State.Line->InPPDirective, Encoding, Style, Whitespaces.useCRLF());
} else if (Current.is(TT_LineComment) &&
- (Current.Previous == nullptr ||
+ (!Current.Previous ||
Current.Previous->isNot(TT_ImplicitStringLiteral))) {
bool RegularComments = [&]() {
for (const FormatToken *T = &Current; T && T->is(TT_LineComment);