| 1 |
//===--- ContinuationIndenter.cpp - Format C++ code -----------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
/// |
| 9 |
/// \file |
| 10 |
/// This file implements the continuation indenter. |
| 11 |
/// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "ContinuationIndenter.h" |
| 15 |
#include "BreakableToken.h" |
| 16 |
#include "FormatInternal.h" |
| 17 |
#include "FormatToken.h" |
| 18 |
#include "WhitespaceManager.h" |
| 19 |
#include "clang/Basic/OperatorPrecedence.h" |
| 20 |
#include "clang/Basic/SourceManager.h" |
| 21 |
#include "clang/Format/Format.h" |
| 22 |
#include "llvm/ADT/StringSet.h" |
| 23 |
#include "llvm/Support/Debug.h" |
| 24 |
#include <optional> |
| 25 |
|
| 26 |
#define DEBUG_TYPE "format-indenter" |
| 27 |
|
| 28 |
namespace clang { |
| 29 |
namespace format { |
| 30 |
|
| 31 |
// Returns true if a TT_SelectorName should be indented when wrapped, |
| 32 |
// false otherwise. |
| 33 |
static bool shouldIndentWrappedSelectorName(const FormatStyle &Style, |
| 34 |
LineType LineType) { |
| 35 |
return Style.IndentWrappedFunctionNames || LineType == LT_ObjCMethodDecl; |
| 36 |
} |
| 37 |
|
| 38 |
// Returns the length of everything up to the first possible line break after |
| 39 |
// the ), ], } or > matching \c Tok. |
| 40 |
static unsigned getLengthToMatchingParen(const FormatToken &Tok, |
| 41 |
ArrayRef<ParenState> Stack) { |
| 42 |
// Normally whether or not a break before T is possible is calculated and |
| 43 |
// stored in T.CanBreakBefore. Braces, array initializers and text proto |
| 44 |
// messages like `key: < ... >` are an exception: a break is possible |
| 45 |
// before a closing brace R if a break was inserted after the corresponding |
| 46 |
// opening brace. The information about whether or not a break is needed |
| 47 |
// before a closing brace R is stored in the ParenState field |
| 48 |
// S.BreakBeforeClosingBrace where S is the state that R closes. |
| 49 |
// |
| 50 |
// In order to decide whether there can be a break before encountered right |
| 51 |
// braces, this implementation iterates over the sequence of tokens and over |
| 52 |
// the paren stack in lockstep, keeping track of the stack level which visited |
| 53 |
// right braces correspond to in MatchingStackIndex. |
| 54 |
// |
| 55 |
// For example, consider: |
| 56 |
// L. <- line number |
| 57 |
// 1. { |
| 58 |
// 2. {1}, |
| 59 |
// 3. {2}, |
| 60 |
// 4. {{3}}} |
| 61 |
// ^ where we call this method with this token. |
| 62 |
// The paren stack at this point contains 3 brace levels: |
| 63 |
// 0. { at line 1, BreakBeforeClosingBrace: true |
| 64 |
// 1. first { at line 4, BreakBeforeClosingBrace: false |
| 65 |
// 2. second { at line 4, BreakBeforeClosingBrace: false, |
| 66 |
// where there might be fake parens levels in-between these levels. |
| 67 |
// The algorithm will start at the first } on line 4, which is the matching |
| 68 |
// brace of the initial left brace and at level 2 of the stack. Then, |
| 69 |
// examining BreakBeforeClosingBrace: false at level 2, it will continue to |
| 70 |
// the second } on line 4, and will traverse the stack downwards until it |
| 71 |
// finds the matching { on level 1. Then, examining BreakBeforeClosingBrace: |
| 72 |
// false at level 1, it will continue to the third } on line 4 and will |
| 73 |
// traverse the stack downwards until it finds the matching { on level 0. |
| 74 |
// Then, examining BreakBeforeClosingBrace: true at level 0, the algorithm |
| 75 |
// will stop and will use the second } on line 4 to determine the length to |
| 76 |
// return, as in this example the range will include the tokens: {3}} |
| 77 |
// |
| 78 |
// The algorithm will only traverse the stack if it encounters braces, array |
| 79 |
// initializer squares or text proto angle brackets. |
| 80 |
if (!Tok.MatchingParen) |
| 81 |
return 0; |
| 82 |
FormatToken *End = Tok.MatchingParen; |
| 83 |
// Maintains a stack level corresponding to the current End token. |
| 84 |
int MatchingStackIndex = Stack.size() - 1; |
| 85 |
// Traverses the stack downwards, looking for the level to which LBrace |
| 86 |
// corresponds. Returns either a pointer to the matching level or nullptr if |
| 87 |
// LParen is not found in the initial portion of the stack up to |
| 88 |
// MatchingStackIndex. |
| 89 |
auto FindParenState = [&](const FormatToken *LBrace) -> const ParenState * { |
| 90 |
while (MatchingStackIndex >= 0 && Stack[MatchingStackIndex].Tok != LBrace) |
| 91 |
--MatchingStackIndex; |
| 92 |
return MatchingStackIndex >= 0 ? &Stack[MatchingStackIndex] : nullptr; |
| 93 |
}; |
| 94 |
for (; End->Next; End = End->Next) { |
| 95 |
if (End->Next->CanBreakBefore) |
| 96 |
break; |
| 97 |
if (!End->Next->closesScope()) |
| 98 |
continue; |
| 99 |
if (End->Next->MatchingParen && |
| 100 |
End->Next->MatchingParen->isOneOf( |
| 101 |
tok::l_brace, TT_ArrayInitializerLSquare, tok::less)) { |
| 102 |
const ParenState *State = FindParenState(End->Next->MatchingParen); |
| 103 |
if (State && State->BreakBeforeClosingBrace) |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
return End->TotalLength - Tok.TotalLength + 1; |
| 108 |
} |
| 109 |
|
| 110 |
static unsigned getLengthToNextOperator(const FormatToken &Tok) { |
| 111 |
if (!Tok.NextOperator) |
| 112 |
return 0; |
| 113 |
return Tok.NextOperator->TotalLength - Tok.TotalLength; |
| 114 |
} |
| 115 |
|
| 116 |
// Returns \c true if \c Tok is the "." or "->" of a call and starts the next |
| 117 |
// segment of a builder type call. |
| 118 |
static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) { |
| 119 |
return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope(); |
| 120 |
} |
| 121 |
|
| 122 |
// Returns \c true if \c Current starts a new parameter. |
| 123 |
static bool startsNextParameter(const FormatToken &Current, |
| 124 |
const FormatStyle &Style) { |
| 125 |
const FormatToken &Previous = *Current.Previous; |
| 126 |
if (Current.is(TT_CtorInitializerComma) && |
| 127 |
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName)) |
| 131 |
return true; |
| 132 |
return Previous.is(tok::comma) && !Current.isTrailingComment() && |
| 133 |
((Previous.isNot(TT_CtorInitializerComma) || |
| 134 |
Style.BreakConstructorInitializers != |
| 135 |
FormatStyle::BCIS_BeforeComma) && |
| 136 |
(Previous.isNot(TT_InheritanceComma) || |
| 137 |
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma)); |
| 138 |
} |
| 139 |
|
| 140 |
static bool opensProtoMessageField(const FormatToken &LessTok, |
| 141 |
const FormatStyle &Style) { |
| 142 |
if (LessTok.isNot(tok::less)) |
| 143 |
return false; |
| 144 |
return Style.Language == FormatStyle::LK_TextProto || |
| 145 |
(Style.Language == FormatStyle::LK_Proto && |
| 146 |
(LessTok.NestingLevel > 0 || |
| 147 |
(LessTok.Previous && LessTok.Previous->is(tok::equal)))); |
| 148 |
} |
| 149 |
|
| 150 |
// Returns the delimiter of a raw string literal, or std::nullopt if TokenText |
| 151 |
// is not the text of a raw string literal. The delimiter could be the empty |
| 152 |
// string. For example, the delimiter of R"deli(cont)deli" is deli. |
| 153 |
static std::optional<StringRef> getRawStringDelimiter(StringRef TokenText) { |
| 154 |
if (TokenText.size() < 5 // The smallest raw string possible is 'R"()"'. |
| 155 |
|| !TokenText.startswith("R\"") || !TokenText.endswith("\"")) { |
| 156 |
return std::nullopt; |
| 157 |
} |
| 158 |
|
| 159 |
// A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has |
| 160 |
// size at most 16 by the standard, so the first '(' must be among the first |
| 161 |
// 19 bytes. |
| 162 |
size_t LParenPos = TokenText.substr(0, 19).find_first_of('('); |
| 163 |
if (LParenPos == StringRef::npos) |
| 164 |
return std::nullopt; |
| 165 |
StringRef Delimiter = TokenText.substr(2, LParenPos - 2); |
| 166 |
|
| 167 |
// Check that the string ends in ')Delimiter"'. |
| 168 |
size_t RParenPos = TokenText.size() - Delimiter.size() - 2; |
| 169 |
if (TokenText[RParenPos] != ')') |
| 170 |
return std::nullopt; |
| 171 |
if (!TokenText.substr(RParenPos + 1).startswith(Delimiter)) |
| 172 |
return std::nullopt; |
| 173 |
return Delimiter; |
| 174 |
} |
| 175 |
|
| 176 |
// Returns the canonical delimiter for \p Language, or the empty string if no |
| 177 |
// canonical delimiter is specified. |
| 178 |
static StringRef |
| 179 |
getCanonicalRawStringDelimiter(const FormatStyle &Style, |
| 180 |
FormatStyle::LanguageKind Language) { |
| 181 |
for (const auto &Format : Style.RawStringFormats) |
| 182 |
if (Format.Language == Language) |
| 183 |
return StringRef(Format.CanonicalDelimiter); |
| 184 |
return ""; |
| 185 |
} |
| 186 |
|
| 187 |
RawStringFormatStyleManager::RawStringFormatStyleManager( |
| 188 |
const FormatStyle &CodeStyle) { |
| 189 |
for (const auto &RawStringFormat : CodeStyle.RawStringFormats) { |
| 190 |
std::optional<FormatStyle> LanguageStyle = |
| 191 |
CodeStyle.GetLanguageStyle(RawStringFormat.Language); |
| 192 |
if (!LanguageStyle) { |
| 193 |
FormatStyle PredefinedStyle; |
| 194 |
if (!getPredefinedStyle(RawStringFormat.BasedOnStyle, |
| 195 |
RawStringFormat.Language, &PredefinedStyle)) { |
| 196 |
PredefinedStyle = getLLVMStyle(); |
| 197 |
PredefinedStyle.Language = RawStringFormat.Language; |
| 198 |
} |
| 199 |
LanguageStyle = PredefinedStyle; |
| 200 |
} |
| 201 |
LanguageStyle->ColumnLimit = CodeStyle.ColumnLimit; |
| 202 |
for (StringRef Delimiter : RawStringFormat.Delimiters) |
| 203 |
DelimiterStyle.insert({Delimiter, *LanguageStyle}); |
| 204 |
for (StringRef EnclosingFunction : RawStringFormat.EnclosingFunctions) |
| 205 |
EnclosingFunctionStyle.insert({EnclosingFunction, *LanguageStyle}); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
std::optional<FormatStyle> |
| 210 |
RawStringFormatStyleManager::getDelimiterStyle(StringRef Delimiter) const { |
| 211 |
auto It = DelimiterStyle.find(Delimiter); |
| 212 |
if (It == DelimiterStyle.end()) |
| 213 |
return std::nullopt; |
| 214 |
return It->second; |
| 215 |
} |
| 216 |
|
| 217 |
std::optional<FormatStyle> |
| 218 |
RawStringFormatStyleManager::getEnclosingFunctionStyle( |
| 219 |
StringRef EnclosingFunction) const { |
| 220 |
auto It = EnclosingFunctionStyle.find(EnclosingFunction); |
| 221 |
if (It == EnclosingFunctionStyle.end()) |
| 222 |
return std::nullopt; |
| 223 |
return It->second; |
| 224 |
} |
| 225 |
|
| 226 |
ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, |
| 227 |
const AdditionalKeywords &Keywords, |
| 228 |
const SourceManager &SourceMgr, |
| 229 |
WhitespaceManager &Whitespaces, |
| 230 |
encoding::Encoding Encoding, |
| 231 |
bool BinPackInconclusiveFunctions) |
| 232 |
: Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), |
| 233 |
Whitespaces(Whitespaces), Encoding(Encoding), |
| 234 |
BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), |
| 235 |
CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} |
| 236 |
|
| 237 |
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, |
| 238 |
unsigned FirstStartColumn, |
| 239 |
const AnnotatedLine *Line, |
| 240 |
bool DryRun) { |
| 241 |
LineState State; |
| 242 |
State.FirstIndent = FirstIndent; |
| 243 |
if (FirstStartColumn && Line->First->NewlinesBefore == 0) |
| 244 |
State.Column = FirstStartColumn; |
| 245 |
else |
| 246 |
State.Column = FirstIndent; |
| 247 |
// With preprocessor directive indentation, the line starts on column 0 |
| 248 |
// since it's indented after the hash, but FirstIndent is set to the |
| 249 |
// preprocessor indent. |
| 250 |
if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && |
| 251 |
(Line->Type == LT_PreprocessorDirective || |
| 252 |
Line->Type == LT_ImportStatement)) { |
| 253 |
State.Column = 0; |
| 254 |
} |
| 255 |
State.Line = Line; |
| 256 |
State.NextToken = Line->First; |
| 257 |
State.Stack.push_back(ParenState(/*Tok=*/nullptr, FirstIndent, FirstIndent, |
| 258 |
/*AvoidBinPacking=*/false, |
| 259 |
/*NoLineBreak=*/false)); |
| 260 |
State.NoContinuation = false; |
| 261 |
State.StartOfStringLiteral = 0; |
| 262 |
State.StartOfLineLevel = 0; |
| 263 |
State.LowestLevelOnLine = 0; |
| 264 |
State.IgnoreStackForComparison = false; |
| 265 |
|
| 266 |
if (Style.Language == FormatStyle::LK_TextProto) { |
| 267 |
// We need this in order to deal with the bin packing of text fields at |
| 268 |
// global scope. |
| 269 |
auto &CurrentState = State.Stack.back(); |
| 270 |
CurrentState.AvoidBinPacking = true; |
| 271 |
CurrentState.BreakBeforeParameter = true; |
| 272 |
CurrentState.AlignColons = false; |
| 273 |
} |
| 274 |
|
| 275 |
// The first token has already been indented and thus consumed. |
| 276 |
moveStateToNextToken(State, DryRun, /*Newline=*/false); |
| 277 |
return State; |
| 278 |
} |
| 279 |
|
| 280 |
bool ContinuationIndenter::canBreak(const LineState &State) { |
| 281 |
const FormatToken &Current = *State.NextToken; |
| 282 |
const FormatToken &Previous = *Current.Previous; |
| 283 |
const auto &CurrentState = State.Stack.back(); |
| 284 |
assert(&Previous == Current.Previous); |
| 285 |
if (!Current.CanBreakBefore && !(CurrentState.BreakBeforeClosingBrace && |
| 286 |
Current.closesBlockOrBlockTypeList(Style))) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
// The opening "{" of a braced list has to be on the same line as the first |
| 290 |
// element if it is nested in another braced init list or function call. |
| 291 |
if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && |
| 292 |
Previous.isNot(TT_DictLiteral) && Previous.is(BK_BracedInit) && |
| 293 |
Previous.Previous && |
| 294 |
Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
// This prevents breaks like: |
| 298 |
// ... |
| 299 |
// SomeParameter, OtherParameter).DoSomething( |
| 300 |
// ... |
| 301 |
// As they hide "DoSomething" and are generally bad for readability. |
| 302 |
if (Previous.opensScope() && Previous.isNot(tok::l_brace) && |
| 303 |
State.LowestLevelOnLine < State.StartOfLineLevel && |
| 304 |
State.LowestLevelOnLine < Current.NestingLevel) { |
| 305 |
return false; |
| 306 |
} |
| 307 |
if (Current.isMemberAccess() && CurrentState.ContainsUnwrappedBuilder) |
| 308 |
return false; |
| 309 |
|
| 310 |
// Don't create a 'hanging' indent if there are multiple blocks in a single |
| 311 |
// statement. |
| 312 |
if (Previous.is(tok::l_brace) && State.Stack.size() > 1 && |
| 313 |
State.Stack[State.Stack.size() - 2].NestedBlockInlined && |
| 314 |
State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
// Don't break after very short return types (e.g. "void") as that is often |
| 319 |
// unexpected. |
| 320 |
if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) { |
| 321 |
if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
// If binary operators are moved to the next line (including commas for some |
| 326 |
// styles of constructor initializers), that's always ok. |
| 327 |
if (!Current.isOneOf(TT_BinaryOperator, tok::comma) && |
| 328 |
CurrentState.NoLineBreakInOperand) { |
| 329 |
return false; |
| 330 |
} |
| 331 |
|
| 332 |
if (Previous.is(tok::l_square) && Previous.is(TT_ObjCMethodExpr)) |
| 333 |
return false; |
| 334 |
|
| 335 |
if (Current.is(TT_ConditionalExpr) && Previous.is(tok::r_paren) && |
| 336 |
Previous.MatchingParen && Previous.MatchingParen->Previous && |
| 337 |
Previous.MatchingParen->Previous->MatchingParen && |
| 338 |
Previous.MatchingParen->Previous->MatchingParen->is(TT_LambdaLBrace)) { |
| 339 |
// We have a lambda within a conditional expression, allow breaking here. |
| 340 |
assert(Previous.MatchingParen->Previous->is(tok::r_brace)); |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
return !CurrentState.NoLineBreak; |
| 345 |
} |
| 346 |
|
| 347 |
bool ContinuationIndenter::mustBreak(const LineState &State) { |
| 348 |
const FormatToken &Current = *State.NextToken; |
| 349 |
const FormatToken &Previous = *Current.Previous; |
| 350 |
const auto &CurrentState = State.Stack.back(); |
| 351 |
if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore && |
| 352 |
Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) { |
| 353 |
auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack); |
| 354 |
return LambdaBodyLength > getColumnLimit(State); |
| 355 |
} |
| 356 |
if (Current.MustBreakBefore || |
| 357 |
(Current.is(TT_InlineASMColon) && |
| 358 |
(Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always || |
| 359 |
Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_OnlyMultiline))) { |
| 360 |
return true; |
| 361 |
} |
| 362 |
if (CurrentState.BreakBeforeClosingBrace && |
| 363 |
Current.closesBlockOrBlockTypeList(Style)) { |
| 364 |
return true; |
| 365 |
} |
| 366 |
if (CurrentState.BreakBeforeClosingParen && Current.is(tok::r_paren)) |
| 367 |
return true; |
| 368 |
if (Style.Language == FormatStyle::LK_ObjC && |
| 369 |
Style.ObjCBreakBeforeNestedBlockParam && |
| 370 |
Current.ObjCSelectorNameParts > 1 && |
| 371 |
Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) { |
| 372 |
return true; |
| 373 |
} |
| 374 |
// Avoid producing inconsistent states by requiring breaks where they are not |
| 375 |
// permitted for C# generic type constraints. |
| 376 |
if (CurrentState.IsCSharpGenericTypeConstraint && |
| 377 |
Previous.isNot(TT_CSharpGenericTypeConstraintComma)) { |
| 378 |
return false; |
| 379 |
} |
| 380 |
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || |
| 381 |
(Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) && |
| 382 |
Style.isCpp() && |
| 383 |
// FIXME: This is a temporary workaround for the case where clang-format |
| 384 |
// sets BreakBeforeParameter to avoid bin packing and this creates a |
| 385 |
// completely unnecessary line break after a template type that isn't |
| 386 |
// line-wrapped. |
| 387 |
(Previous.NestingLevel == 1 || Style.BinPackParameters)) || |
| 388 |
(Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) && |
| 389 |
Previous.isNot(tok::question)) || |
| 390 |
(!Style.BreakBeforeTernaryOperators && |
| 391 |
Previous.is(TT_ConditionalExpr))) && |
| 392 |
CurrentState.BreakBeforeParameter && !Current.isTrailingComment() && |
| 393 |
!Current.isOneOf(tok::r_paren, tok::r_brace)) { |
| 394 |
return true; |
| 395 |
} |
| 396 |
if (CurrentState.IsChainedConditional && |
| 397 |
((Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) && |
| 398 |
Current.is(tok::colon)) || |
| 399 |
(!Style.BreakBeforeTernaryOperators && Previous.is(TT_ConditionalExpr) && |
| 400 |
Previous.is(tok::colon)))) { |
| 401 |
return true; |
| 402 |
} |
| 403 |
if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) || |
| 404 |
(Previous.is(TT_ArrayInitializerLSquare) && |
| 405 |
Previous.ParameterCount > 1) || |
| 406 |
opensProtoMessageField(Previous, Style)) && |
| 407 |
Style.ColumnLimit > 0 && |
| 408 |
getLengthToMatchingParen(Previous, State.Stack) + State.Column - 1 > |
| 409 |
getColumnLimit(State)) { |
| 410 |
return true; |
| 411 |
} |
| 412 |
|
| 413 |
const FormatToken &BreakConstructorInitializersToken = |
| 414 |
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon |
| 415 |
? Previous |
| 416 |
: Current; |
| 417 |
if (BreakConstructorInitializersToken.is(TT_CtorInitializerColon) && |
| 418 |
(State.Column + State.Line->Last->TotalLength - Previous.TotalLength > |
| 419 |
getColumnLimit(State) || |
| 420 |
CurrentState.BreakBeforeParameter) && |
| 421 |
(!Current.isTrailingComment() || Current.NewlinesBefore > 0) && |
| 422 |
(Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All || |
| 423 |
Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon || |
| 424 |
Style.ColumnLimit != 0)) { |
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
if (Current.is(TT_ObjCMethodExpr) && !Previous.is(TT_SelectorName) && |
| 429 |
State.Line->startsWith(TT_ObjCMethodSpecifier)) { |
| 430 |
return true; |
| 431 |
} |
| 432 |
if (Current.is(TT_SelectorName) && !Previous.is(tok::at) && |
| 433 |
CurrentState.ObjCSelectorNameFound && CurrentState.BreakBeforeParameter && |
| 434 |
(Style.ObjCBreakBeforeNestedBlockParam || |
| 435 |
!Current.startsSequence(TT_SelectorName, tok::colon, tok::caret))) { |
| 436 |
return true; |
| 437 |
} |
| 438 |
|
| 439 |
unsigned NewLineColumn = getNewLineColumn(State); |
| 440 |
if (Current.isMemberAccess() && Style.ColumnLimit != 0 && |
| 441 |
State.Column + getLengthToNextOperator(Current) > Style.ColumnLimit && |
| 442 |
(State.Column > NewLineColumn || |
| 443 |
Current.NestingLevel < State.StartOfLineLevel)) { |
| 444 |
return true; |
| 445 |
} |
| 446 |
|
| 447 |
if (startsSegmentOfBuilderTypeCall(Current) && |
| 448 |
(CurrentState.CallContinuation != 0 || |
| 449 |
CurrentState.BreakBeforeParameter) && |
| 450 |
// JavaScript is treated different here as there is a frequent pattern: |
| 451 |
// SomeFunction(function() { |
| 452 |
// ... |
| 453 |
// }.bind(...)); |
| 454 |
// FIXME: We should find a more generic solution to this problem. |
| 455 |
!(State.Column <= NewLineColumn && Style.isJavaScript()) && |
| 456 |
!(Previous.closesScopeAfterBlock() && State.Column <= NewLineColumn)) { |
| 457 |
return true; |
| 458 |
} |
| 459 |
|
| 460 |
// If the template declaration spans multiple lines, force wrap before the |
| 461 |
// function/class declaration. |
| 462 |
if (Previous.ClosesTemplateDeclaration && CurrentState.BreakBeforeParameter && |
| 463 |
Current.CanBreakBefore) { |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
if (!State.Line->First->is(tok::kw_enum) && State.Column <= NewLineColumn) |
| 468 |
return false; |
| 469 |
|
| 470 |
if (Style.AlwaysBreakBeforeMultilineStrings && |
| 471 |
(NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth || |
| 472 |
Previous.is(tok::comma) || Current.NestingLevel < 2) && |
| 473 |
!Previous.isOneOf(tok::kw_return, tok::lessless, tok::at, |
| 474 |
Keywords.kw_dollar) && |
| 475 |
!Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) && |
| 476 |
nextIsMultilineString(State)) { |
| 477 |
return true; |
| 478 |
} |
| 479 |
|
| 480 |
// Using CanBreakBefore here and below takes care of the decision whether the |
| 481 |
// current style uses wrapping before or after operators for the given |
| 482 |
// operator. |
| 483 |
if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) { |
| 484 |
const auto PreviousPrecedence = Previous.getPrecedence(); |
| 485 |
if (PreviousPrecedence != prec::Assignment && |
| 486 |
CurrentState.BreakBeforeParameter && !Current.isTrailingComment()) { |
| 487 |
const bool LHSIsBinaryExpr = |
| 488 |
Previous.Previous && Previous.Previous->EndsBinaryExpression; |
| 489 |
if (LHSIsBinaryExpr) |
| 490 |
return true; |
| 491 |
// If we need to break somewhere inside the LHS of a binary expression, we |
| 492 |
// should also break after the operator. Otherwise, the formatting would |
| 493 |
// hide the operator precedence, e.g. in: |
| 494 |
// if (aaaaaaaaaaaaaa == |
| 495 |
// bbbbbbbbbbbbbb && c) {.. |
| 496 |
// For comparisons, we only apply this rule, if the LHS is a binary |
| 497 |
// expression itself as otherwise, the line breaks seem superfluous. |
| 498 |
// We need special cases for ">>" which we have split into two ">" while |
| 499 |
// lexing in order to make template parsing easier. |
| 500 |
const bool IsComparison = |
| 501 |
(PreviousPrecedence == prec::Relational || |
| 502 |
PreviousPrecedence == prec::Equality || |
| 503 |
PreviousPrecedence == prec::Spaceship) && |
| 504 |
Previous.Previous && |
| 505 |
Previous.Previous->isNot(TT_BinaryOperator); // For >>. |
| 506 |
if (!IsComparison) |
| 507 |
return true; |
| 508 |
} |
| 509 |
} else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore && |
| 510 |
CurrentState.BreakBeforeParameter) { |
| 511 |
return true; |
| 512 |
} |
| 513 |
|
| 514 |
// Same as above, but for the first "<<" operator. |
| 515 |
if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) && |
| 516 |
CurrentState.BreakBeforeParameter && CurrentState.FirstLessLess == 0) { |
| 517 |
return true; |
| 518 |
} |
| 519 |
|
| 520 |
if (Current.NestingLevel == 0 && !Current.isTrailingComment()) { |
| 521 |
// Always break after "template <...>"(*) and leading annotations. This is |
| 522 |
// only for cases where the entire line does not fit on a single line as a |
| 523 |
// different LineFormatter would be used otherwise. |
| 524 |
// *: Except when another option interferes with that, like concepts. |
| 525 |
if (Previous.ClosesTemplateDeclaration) { |
| 526 |
if (Current.is(tok::kw_concept)) { |
| 527 |
switch (Style.BreakBeforeConceptDeclarations) { |
| 528 |
case FormatStyle::BBCDS_Allowed: |
| 529 |
break; |
| 530 |
case FormatStyle::BBCDS_Always: |
| 531 |
return true; |
| 532 |
case FormatStyle::BBCDS_Never: |
| 533 |
return false; |
| 534 |
} |
| 535 |
} |
| 536 |
if (Current.is(TT_RequiresClause)) { |
| 537 |
switch (Style.RequiresClausePosition) { |
| 538 |
case FormatStyle::RCPS_SingleLine: |
| 539 |
case FormatStyle::RCPS_WithPreceding: |
| 540 |
return false; |
| 541 |
default: |
| 542 |
return true; |
| 543 |
} |
| 544 |
} |
| 545 |
return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No; |
| 546 |
} |
| 547 |
if (Previous.is(TT_FunctionAnnotationRParen) && |
| 548 |
State.Line->Type != LT_PreprocessorDirective) { |
| 549 |
return true; |
| 550 |
} |
| 551 |
if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) && |
| 552 |
Current.isNot(TT_LeadingJavaAnnotation)) { |
| 553 |
return true; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
if (Style.isJavaScript() && Previous.is(tok::r_paren) && |
| 558 |
Previous.is(TT_JavaAnnotation)) { |
| 559 |
// Break after the closing parenthesis of TypeScript decorators before |
| 560 |
// functions, getters and setters. |
| 561 |
static const llvm::StringSet<> BreakBeforeDecoratedTokens = {"get", "set", |
| 562 |
"function"}; |
| 563 |
if (BreakBeforeDecoratedTokens.contains(Current.TokenText)) |
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
// If the return type spans multiple lines, wrap before the function name. |
| 568 |
if (((Current.is(TT_FunctionDeclarationName) && |
| 569 |
!State.Line->ReturnTypeWrapped && |
| 570 |
// Don't break before a C# function when no break after return type. |
| 571 |
(!Style.isCSharp() || |
| 572 |
Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) && |
| 573 |
// Don't always break between a JavaScript `function` and the function |
| 574 |
// name. |
| 575 |
!Style.isJavaScript()) || |
| 576 |
(Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) && |
| 577 |
!Previous.is(tok::kw_template) && CurrentState.BreakBeforeParameter) { |
| 578 |
return true; |
| 579 |
} |
| 580 |
|
| 581 |
// The following could be precomputed as they do not depend on the state. |
| 582 |
// However, as they should take effect only if the UnwrappedLine does not fit |
| 583 |
// into the ColumnLimit, they are checked here in the ContinuationIndenter. |
| 584 |
if (Style.ColumnLimit != 0 && Previous.is(BK_Block) && |
| 585 |
Previous.is(tok::l_brace) && |
| 586 |
!Current.isOneOf(tok::r_brace, tok::comment)) { |
| 587 |
return true; |
| 588 |
} |
| 589 |
|
| 590 |
if (Current.is(tok::lessless) && |
| 591 |
((Previous.is(tok::identifier) && Previous.TokenText == "endl") || |
| 592 |
(Previous.Tok.isLiteral() && (Previous.TokenText.endswith("\\n\"") || |
| 593 |
Previous.TokenText == "\'\\n\'")))) { |
| 594 |
return true; |
| 595 |
} |
| 596 |
|
| 597 |
if (Previous.is(TT_BlockComment) && Previous.IsMultiline) |
| 598 |
return true; |
| 599 |
|
| 600 |
if (State.NoContinuation) |
| 601 |
return true; |
| 602 |
|
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, |
| 607 |
bool DryRun, |
| 608 |
unsigned ExtraSpaces) { |
| 609 |
const FormatToken &Current = *State.NextToken; |
| 610 |
assert(State.NextToken->Previous); |
| 611 |
const FormatToken &Previous = *State.NextToken->Previous; |
| 612 |
|
| 613 |
assert(!State.Stack.empty()); |
| 614 |
State.NoContinuation = false; |
| 615 |
|
| 616 |
if ((Current.is(TT_ImplicitStringLiteral) && |
| 617 |
(Previous.Tok.getIdentifierInfo() == nullptr || |
| 618 |
Previous.Tok.getIdentifierInfo()->getPPKeywordID() == |
| 619 |
tok::pp_not_keyword))) { |
| 620 |
unsigned EndColumn = |
| 621 |
SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd()); |
| 622 |
if (Current.LastNewlineOffset != 0) { |
| 623 |
// If there is a newline within this token, the final column will solely |
| 624 |
// determined by the current end column. |
| 625 |
State.Column = EndColumn; |
| 626 |
} else { |
| 627 |
unsigned StartColumn = |
| 628 |
SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin()); |
| 629 |
assert(EndColumn >= StartColumn); |
| 630 |
State.Column += EndColumn - StartColumn; |
| 631 |
} |
| 632 |
moveStateToNextToken(State, DryRun, /*Newline=*/false); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
unsigned Penalty = 0; |
| 637 |
if (Newline) |
| 638 |
Penalty = addTokenOnNewLine(State, DryRun); |
| 639 |
else |
| 640 |
addTokenOnCurrentLine(State, DryRun, ExtraSpaces); |
| 641 |
|
| 642 |
return moveStateToNextToken(State, DryRun, Newline) + Penalty; |
| 643 |
} |
| 644 |
|
| 645 |
void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, |
| 646 |
unsigned ExtraSpaces) { |
| 647 |
FormatToken &Current = *State.NextToken; |
| 648 |
assert(State.NextToken->Previous); |
| 649 |
const FormatToken &Previous = *State.NextToken->Previous; |
| 650 |
auto &CurrentState = State.Stack.back(); |
| 651 |
|
| 652 |
if (Current.is(tok::equal) && |
| 653 |
(State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) && |
| 654 |
CurrentState.VariablePos == 0) { |
| 655 |
CurrentState.VariablePos = State.Column; |
| 656 |
// Move over * and & if they are bound to the variable name. |
| 657 |
const FormatToken *Tok = &Previous; |
| 658 |
while (Tok && CurrentState.VariablePos >= Tok->ColumnWidth) { |
| 659 |
CurrentState.VariablePos -= Tok->ColumnWidth; |
| 660 |
if (Tok->SpacesRequiredBefore != 0) |
| 661 |
break; |
| 662 |
Tok = Tok->Previous; |
| 663 |
} |
| 664 |
if (Previous.PartOfMultiVariableDeclStmt) |
| 665 |
CurrentState.LastSpace = CurrentState.VariablePos; |
| 666 |
} |
| 667 |
|
| 668 |
unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces; |
| 669 |
|
| 670 |
// Indent preprocessor directives after the hash if required. |
| 671 |
int PPColumnCorrection = 0; |
| 672 |
if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && |
| 673 |
Previous.is(tok::hash) && State.FirstIndent > 0 && |
| 674 |
&Previous == State.Line->First && |
| 675 |
(State.Line->Type == LT_PreprocessorDirective || |
| 676 |
State.Line->Type == LT_ImportStatement)) { |
| 677 |
Spaces += State.FirstIndent; |
| 678 |
|
| 679 |
// For preprocessor indent with tabs, State.Column will be 1 because of the |
| 680 |
// hash. This causes second-level indents onward to have an extra space |
| 681 |
// after the tabs. We avoid this misalignment by subtracting 1 from the |
| 682 |
// column value passed to replaceWhitespace(). |
| 683 |
if (Style.UseTab != FormatStyle::UT_Never) |
| 684 |
PPColumnCorrection = -1; |
| 685 |
} |
| 686 |
|
| 687 |
if (!DryRun) { |
| 688 |
Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces, |
| 689 |
State.Column + Spaces + PPColumnCorrection); |
| 690 |
} |
| 691 |
|
| 692 |
// If "BreakBeforeInheritanceComma" mode, don't break within the inheritance |
| 693 |
// declaration unless there is multiple inheritance. |
| 694 |
if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma && |
| 695 |
Current.is(TT_InheritanceColon)) { |
| 696 |
CurrentState.NoLineBreak = true; |
| 697 |
} |
| 698 |
if (Style.BreakInheritanceList == FormatStyle::BILS_AfterColon && |
| 699 |
Previous.is(TT_InheritanceColon)) { |
| 700 |
CurrentState.NoLineBreak = true; |
| 701 |
} |
| 702 |
|
| 703 |
if (Current.is(TT_SelectorName) && !CurrentState.ObjCSelectorNameFound) { |
| 704 |
unsigned MinIndent = std::max( |
| 705 |
State.FirstIndent + Style.ContinuationIndentWidth, CurrentState.Indent); |
| 706 |
unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth; |
| 707 |
if (Current.LongestObjCSelectorName == 0) |
| 708 |
CurrentState.AlignColons = false; |
| 709 |
else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos) |
| 710 |
CurrentState.ColonPos = MinIndent + Current.LongestObjCSelectorName; |
| 711 |
else |
| 712 |
CurrentState.ColonPos = FirstColonPos; |
| 713 |
} |
| 714 |
|
| 715 |
// In "AlwaysBreak" or "BlockIndent" mode, enforce wrapping directly after the |
| 716 |
// parenthesis by disallowing any further line breaks if there is no line |
| 717 |
// break after the opening parenthesis. Don't break if it doesn't conserve |
| 718 |
// columns. |
| 719 |
if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || |
| 720 |
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) && |
| 721 |
(Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) || |
| 722 |
(Previous.is(tok::l_brace) && Previous.isNot(BK_Block) && |
| 723 |
Style.Cpp11BracedListStyle)) && |
| 724 |
State.Column > getNewLineColumn(State) && |
| 725 |
(!Previous.Previous || |
| 726 |
!Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while, |
| 727 |
tok::kw_switch)) && |
| 728 |
// Don't do this for simple (no expressions) one-argument function calls |
| 729 |
// as that feels like needlessly wasting whitespace, e.g.: |
| 730 |
// |
| 731 |
// caaaaaaaaaaaall( |
| 732 |
// caaaaaaaaaaaall( |
| 733 |
// caaaaaaaaaaaall( |
| 734 |
// caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa)))); |
| 735 |
Current.FakeLParens.size() > 0 && |
| 736 |
Current.FakeLParens.back() > prec::Unknown) { |
| 737 |
CurrentState.NoLineBreak = true; |
| 738 |
} |
| 739 |
if (Previous.is(TT_TemplateString) && Previous.opensScope()) |
| 740 |
CurrentState.NoLineBreak = true; |
| 741 |
|
| 742 |
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign && |
| 743 |
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() && |
| 744 |
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) && |
| 745 |
(Current.isNot(TT_LineComment) || Previous.is(BK_BracedInit))) { |
| 746 |
CurrentState.Indent = State.Column + Spaces; |
| 747 |
CurrentState.IsAligned = true; |
| 748 |
} |
| 749 |
if (CurrentState.AvoidBinPacking && startsNextParameter(Current, Style)) |
| 750 |
CurrentState.NoLineBreak = true; |
| 751 |
if (startsSegmentOfBuilderTypeCall(Current) && |
| 752 |
State.Column > getNewLineColumn(State)) { |
| 753 |
CurrentState.ContainsUnwrappedBuilder = true; |
| 754 |
} |
| 755 |
|
| 756 |
if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java) |
| 757 |
CurrentState.NoLineBreak = true; |
| 758 |
if (Current.isMemberAccess() && Previous.is(tok::r_paren) && |
| 759 |
(Previous.MatchingParen && |
| 760 |
(Previous.TotalLength - Previous.MatchingParen->TotalLength > 10))) { |
| 761 |
// If there is a function call with long parameters, break before trailing |
| 762 |
// calls. This prevents things like: |
| 763 |
// EXPECT_CALL(SomeLongParameter).Times( |
| 764 |
// 2); |
| 765 |
// We don't want to do this for short parameters as they can just be |
| 766 |
// indexes. |
| 767 |
CurrentState.NoLineBreak = true; |
| 768 |
} |
| 769 |
|
| 770 |
// Don't allow the RHS of an operator to be split over multiple lines unless |
| 771 |
// there is a line-break right after the operator. |
| 772 |
// Exclude relational operators, as there, it is always more desirable to |
| 773 |
// have the LHS 'left' of the RHS. |
| 774 |
const FormatToken *P = Current.getPreviousNonComment(); |
| 775 |
if (!Current.is(tok::comment) && P && |
| 776 |
(P->isOneOf(TT_BinaryOperator, tok::comma) || |
| 777 |
(P->is(TT_ConditionalExpr) && P->is(tok::colon))) && |
| 778 |
!P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) && |
| 779 |
P->getPrecedence() != prec::Assignment && |
| 780 |
P->getPrecedence() != prec::Relational && |
| 781 |
P->getPrecedence() != prec::Spaceship) { |
| 782 |
bool BreakBeforeOperator = |
| 783 |
P->MustBreakBefore || P->is(tok::lessless) || |
| 784 |
(P->is(TT_BinaryOperator) && |
| 785 |
Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) || |
| 786 |
(P->is(TT_ConditionalExpr) && Style.BreakBeforeTernaryOperators); |
| 787 |
// Don't do this if there are only two operands. In these cases, there is |
| 788 |
// always a nice vertical separation between them and the extra line break |
| 789 |
// does not help. |
| 790 |
bool HasTwoOperands = |
| 791 |
P->OperatorIndex == 0 && !P->NextOperator && !P->is(TT_ConditionalExpr); |
| 792 |
if ((!BreakBeforeOperator && |
| 793 |
!(HasTwoOperands && |
| 794 |
Style.AlignOperands != FormatStyle::OAS_DontAlign)) || |
| 795 |
(!CurrentState.LastOperatorWrapped && BreakBeforeOperator)) { |
| 796 |
CurrentState.NoLineBreakInOperand = true; |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
State.Column += Spaces; |
| 801 |
if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) && |
| 802 |
Previous.Previous && |
| 803 |
(Previous.Previous->is(tok::kw_for) || Previous.Previous->isIf())) { |
| 804 |
// Treat the condition inside an if as if it was a second function |
| 805 |
// parameter, i.e. let nested calls have a continuation indent. |
| 806 |
CurrentState.LastSpace = State.Column; |
| 807 |
CurrentState.NestedBlockIndent = State.Column; |
| 808 |
} else if (!Current.isOneOf(tok::comment, tok::caret) && |
| 809 |
((Previous.is(tok::comma) && |
| 810 |
!Previous.is(TT_OverloadedOperator)) || |
| 811 |
(Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) { |
| 812 |
CurrentState.LastSpace = State.Column; |
| 813 |
} else if (Previous.is(TT_CtorInitializerColon) && |
| 814 |
(!Current.isTrailingComment() || Current.NewlinesBefore > 0) && |
| 815 |
Style.BreakConstructorInitializers == |
| 816 |
FormatStyle::BCIS_AfterColon) { |
| 817 |
CurrentState.Indent = State.Column; |
| 818 |
CurrentState.LastSpace = State.Column; |
| 819 |
} else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr, |
| 820 |
TT_CtorInitializerColon)) && |
| 821 |
((Previous.getPrecedence() != prec::Assignment && |
| 822 |
(Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 || |
| 823 |
Previous.NextOperator)) || |
| 824 |
Current.StartsBinaryExpression)) { |
| 825 |
// Indent relative to the RHS of the expression unless this is a simple |
| 826 |
// assignment without binary expression on the RHS. Also indent relative to |
| 827 |
// unary operators and the colons of constructor initializers. |
| 828 |
if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None) |
| 829 |
CurrentState.LastSpace = State.Column; |
| 830 |
} else if (Previous.is(TT_InheritanceColon)) { |
| 831 |
CurrentState.Indent = State.Column; |
| 832 |
CurrentState.LastSpace = State.Column; |
| 833 |
} else if (Current.is(TT_CSharpGenericTypeConstraintColon)) { |
| 834 |
CurrentState.ColonPos = State.Column; |
| 835 |
} else if (Previous.opensScope()) { |
| 836 |
// If a function has a trailing call, indent all parameters from the |
| 837 |
// opening parenthesis. This avoids confusing indents like: |
| 838 |
// OuterFunction(InnerFunctionCall( // break |
| 839 |
// ParameterToInnerFunction)) // break |
| 840 |
// .SecondInnerFunctionCall(); |
| 841 |
if (Previous.MatchingParen) { |
| 842 |
const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); |
| 843 |
if (Next && Next->isMemberAccess() && State.Stack.size() > 1 && |
| 844 |
State.Stack[State.Stack.size() - 2].CallContinuation == 0) { |
| 845 |
CurrentState.LastSpace = State.Column; |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, |
| 852 |
bool DryRun) { |
| 853 |
FormatToken &Current = *State.NextToken; |
| 854 |
assert(State.NextToken->Previous); |
| 855 |
const FormatToken &Previous = *State.NextToken->Previous; |
| 856 |
auto &CurrentState = State.Stack.back(); |
| 857 |
|
| 858 |
// Extra penalty that needs to be added because of the way certain line |
| 859 |
// breaks are chosen. |
| 860 |
unsigned Penalty = 0; |
| 861 |
|
| 862 |
const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); |
| 863 |
const FormatToken *NextNonComment = Previous.getNextNonComment(); |
| 864 |
if (!NextNonComment) |
| 865 |
NextNonComment = &Current; |
| 866 |
// The first line break on any NestingLevel causes an extra penalty in order |
| 867 |
// prefer similar line breaks. |
| 868 |
if (!CurrentState.ContainsLineBreak) |
| 869 |
Penalty += 15; |
| 870 |
CurrentState.ContainsLineBreak = true; |
| 871 |
|
| 872 |
Penalty += State.NextToken->SplitPenalty; |
| 873 |
|
| 874 |
// Breaking before the first "<<" is generally not desirable if the LHS is |
| 875 |
// short. Also always add the penalty if the LHS is split over multiple lines |
| 876 |
// to avoid unnecessary line breaks that just work around this penalty. |
| 877 |
if (NextNonComment->is(tok::lessless) && CurrentState.FirstLessLess == 0 && |
| 878 |
(State.Column <= Style.ColumnLimit / 3 || |
| 879 |
CurrentState.BreakBeforeParameter)) { |
| 880 |
Penalty += Style.PenaltyBreakFirstLessLess; |
| 881 |
} |
| 882 |
|
| 883 |
State.Column = getNewLineColumn(State); |
| 884 |
|
| 885 |
// Add Penalty proportional to amount of whitespace away from FirstColumn |
| 886 |
// This tends to penalize several lines that are far-right indented, |
| 887 |
// and prefers a line-break prior to such a block, e.g: |
| 888 |
// |
| 889 |
// Constructor() : |
| 890 |
// member(value), looooooooooooooooong_member( |
| 891 |
// looooooooooong_call(param_1, param_2, param_3)) |
| 892 |
// would then become |
| 893 |
// Constructor() : |
| 894 |
// member(value), |
| 895 |
// looooooooooooooooong_member( |
| 896 |
// looooooooooong_call(param_1, param_2, param_3)) |
| 897 |
if (State.Column > State.FirstIndent) { |
| 898 |
Penalty += |
| 899 |
Style.PenaltyIndentedWhitespace * (State.Column - State.FirstIndent); |
| 900 |
} |
| 901 |
|
| 902 |
// Indent nested blocks relative to this column, unless in a very specific |
| 903 |
// JavaScript special case where: |
| 904 |
// |
| 905 |
// var loooooong_name = |
| 906 |
// function() { |
| 907 |
// // code |
| 908 |
// } |
| 909 |
// |
| 910 |
// is common and should be formatted like a free-standing function. The same |
| 911 |
// goes for wrapping before the lambda return type arrow. |
| 912 |
if (!Current.is(TT_LambdaArrow) && |
| 913 |
(!Style.isJavaScript() || Current.NestingLevel != 0 || |
| 914 |
!PreviousNonComment || !PreviousNonComment->is(tok::equal) || |
| 915 |
!Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) { |
| 916 |
CurrentState.NestedBlockIndent = State.Column; |
| 917 |
} |
| 918 |
|
| 919 |
if (NextNonComment->isMemberAccess()) { |
| 920 |
if (CurrentState.CallContinuation == 0) |
| 921 |
CurrentState.CallContinuation = State.Column; |
| 922 |
} else if (NextNonComment->is(TT_SelectorName)) { |
| 923 |
if (!CurrentState.ObjCSelectorNameFound) { |
| 924 |
if (NextNonComment->LongestObjCSelectorName == 0) { |
| 925 |
CurrentState.AlignColons = false; |
| 926 |
} else { |
| 927 |
CurrentState.ColonPos = |
| 928 |
(shouldIndentWrappedSelectorName(Style, State.Line->Type) |
| 929 |
? std::max(CurrentState.Indent, |
| 930 |
State.FirstIndent + Style.ContinuationIndentWidth) |
| 931 |
: CurrentState.Indent) + |
| 932 |
std::max(NextNonComment->LongestObjCSelectorName, |
| 933 |
NextNonComment->ColumnWidth); |
| 934 |
} |
| 935 |
} else if (CurrentState.AlignColons && |
| 936 |
CurrentState.ColonPos <= NextNonComment->ColumnWidth) { |
| 937 |
CurrentState.ColonPos = State.Column + NextNonComment->ColumnWidth; |
| 938 |
} |
| 939 |
} else if (PreviousNonComment && PreviousNonComment->is(tok::colon) && |
| 940 |
PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) { |
| 941 |
// FIXME: This is hacky, find a better way. The problem is that in an ObjC |
| 942 |
// method expression, the block should be aligned to the line starting it, |
| 943 |
// e.g.: |
| 944 |
// [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason |
| 945 |
// ^(int *i) { |
| 946 |
// // ... |
| 947 |
// }]; |
| 948 |
// Thus, we set LastSpace of the next higher NestingLevel, to which we move |
| 949 |
// when we consume all of the "}"'s FakeRParens at the "{". |
| 950 |
if (State.Stack.size() > 1) { |
| 951 |
State.Stack[State.Stack.size() - 2].LastSpace = |
| 952 |
std::max(CurrentState.LastSpace, CurrentState.Indent) + |
| 953 |
Style.ContinuationIndentWidth; |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
if ((PreviousNonComment && |
| 958 |
PreviousNonComment->isOneOf(tok::comma, tok::semi) && |
| 959 |
!CurrentState.AvoidBinPacking) || |
| 960 |
Previous.is(TT_BinaryOperator)) { |
| 961 |
CurrentState.BreakBeforeParameter = false; |
| 962 |
} |
| 963 |
if (PreviousNonComment && |
| 964 |
(PreviousNonComment->isOneOf(TT_TemplateCloser, TT_JavaAnnotation) || |
| 965 |
PreviousNonComment->ClosesRequiresClause) && |
| 966 |
Current.NestingLevel == 0) { |
| 967 |
CurrentState.BreakBeforeParameter = false; |
| 968 |
} |
| 969 |
if (NextNonComment->is(tok::question) || |
| 970 |
(PreviousNonComment && PreviousNonComment->is(tok::question))) { |
| 971 |
CurrentState.BreakBeforeParameter = true; |
| 972 |
} |
| 973 |
if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore) |
| 974 |
CurrentState.BreakBeforeParameter = false; |
| 975 |
|
| 976 |
if (!DryRun) { |
| 977 |
unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1; |
| 978 |
if (Current.is(tok::r_brace) && Current.MatchingParen && |
| 979 |
// Only strip trailing empty lines for l_braces that have children, i.e. |
| 980 |
// for function expressions (lambdas, arrows, etc). |
| 981 |
!Current.MatchingParen->Children.empty()) { |
| 982 |
// lambdas and arrow functions are expressions, thus their r_brace is not |
| 983 |
// on its own line, and thus not covered by UnwrappedLineFormatter's logic |
| 984 |
// about removing empty lines on closing blocks. Special case them here. |
| 985 |
MaxEmptyLinesToKeep = 1; |
| 986 |
} |
| 987 |
unsigned Newlines = |
| 988 |
std::max(1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep)); |
| 989 |
bool ContinuePPDirective = |
| 990 |
State.Line->InPPDirective && State.Line->Type != LT_ImportStatement; |
| 991 |
Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column, |
| 992 |
CurrentState.IsAligned, ContinuePPDirective); |
| 993 |
} |
| 994 |
|
| 995 |
if (!Current.isTrailingComment()) |
| 996 |
CurrentState.LastSpace = State.Column; |
| 997 |
if (Current.is(tok::lessless)) { |
| 998 |
// If we are breaking before a "<<", we always want to indent relative to |
| 999 |
// RHS. This is necessary only for "<<", as we special-case it and don't |
| 1000 |
// always indent relative to the RHS. |
| 1001 |
CurrentState.LastSpace += 3; // 3 -> width of "<< ". |
| 1002 |
} |
| 1003 |
|
| 1004 |
State.StartOfLineLevel = Current.NestingLevel; |
| 1005 |
State.LowestLevelOnLine = Current.NestingLevel; |
| 1006 |
|
| 1007 |
// Any break on this level means that the parent level has been broken |
| 1008 |
// and we need to avoid bin packing there. |
| 1009 |
bool NestedBlockSpecialCase = |
| 1010 |
(!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && |
| 1011 |
State.Stack[State.Stack.size() - 2].NestedBlockInlined) || |
| 1012 |
(Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) && |
| 1013 |
State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam); |
| 1014 |
// Do not force parameter break for statements with requires expressions. |
| 1015 |
NestedBlockSpecialCase = |
| 1016 |
NestedBlockSpecialCase || |
| 1017 |
(Current.MatchingParen && |
| 1018 |
Current.MatchingParen->is(TT_RequiresExpressionLBrace)); |
| 1019 |
if (!NestedBlockSpecialCase) |
| 1020 |
for (ParenState &PState : llvm::drop_end(State.Stack)) |
| 1021 |
PState.BreakBeforeParameter = true; |
| 1022 |
|
| 1023 |
if (PreviousNonComment && |
| 1024 |
!PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) && |
| 1025 |
((PreviousNonComment->isNot(TT_TemplateCloser) && |
| 1026 |
!PreviousNonComment->ClosesRequiresClause) || |
| 1027 |
Current.NestingLevel != 0) && |
| 1028 |
!PreviousNonComment->isOneOf( |
| 1029 |
TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation, |
| 1030 |
TT_LeadingJavaAnnotation) && |
| 1031 |
Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope()) { |
| 1032 |
CurrentState.BreakBeforeParameter = true; |
| 1033 |
} |
| 1034 |
|
| 1035 |
// If we break after { or the [ of an array initializer, we should also break |
| 1036 |
// before the corresponding } or ]. |
| 1037 |
if (PreviousNonComment && |
| 1038 |
(PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || |
| 1039 |
opensProtoMessageField(*PreviousNonComment, Style))) { |
| 1040 |
CurrentState.BreakBeforeClosingBrace = true; |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) { |
| 1044 |
CurrentState.BreakBeforeClosingParen = |
| 1045 |
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent; |
| 1046 |
} |
| 1047 |
|
| 1048 |
if (CurrentState.AvoidBinPacking) { |
| 1049 |
// If we are breaking after '(', '{', '<', or this is the break after a ':' |
| 1050 |
// to start a member initializater list in a constructor, this should not |
| 1051 |
// be considered bin packing unless the relevant AllowAll option is false or |
| 1052 |
// this is a dict/object literal. |
| 1053 |
bool PreviousIsBreakingCtorInitializerColon = |
| 1054 |
PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) && |
| 1055 |
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon; |
| 1056 |
if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) || |
| 1057 |
PreviousIsBreakingCtorInitializerColon) || |
| 1058 |
(!Style.AllowAllParametersOfDeclarationOnNextLine && |
| 1059 |
State.Line->MustBeDeclaration) || |
| 1060 |
(!Style.AllowAllArgumentsOnNextLine && |
| 1061 |
!State.Line->MustBeDeclaration) || |
| 1062 |
(Style.PackConstructorInitializers != FormatStyle::PCIS_NextLine && |
| 1063 |
PreviousIsBreakingCtorInitializerColon) || |
| 1064 |
Previous.is(TT_DictLiteral)) { |
| 1065 |
CurrentState.BreakBeforeParameter = true; |
| 1066 |
} |
| 1067 |
|
| 1068 |
// If we are breaking after a ':' to start a member initializer list, |
| 1069 |
// and we allow all arguments on the next line, we should not break |
| 1070 |
// before the next parameter. |
| 1071 |
if (PreviousIsBreakingCtorInitializerColon && |
| 1072 |
Style.PackConstructorInitializers == FormatStyle::PCIS_NextLine) { |
| 1073 |
CurrentState.BreakBeforeParameter = false; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
return Penalty; |
| 1078 |
} |
| 1079 |
|
| 1080 |
unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { |
| 1081 |
if (!State.NextToken || !State.NextToken->Previous) |
| 1082 |
return 0; |
| 1083 |
|
| 1084 |
FormatToken &Current = *State.NextToken; |
| 1085 |
const auto &CurrentState = State.Stack.back(); |
| 1086 |
|
| 1087 |
if (CurrentState.IsCSharpGenericTypeConstraint && |
| 1088 |
Current.isNot(TT_CSharpGenericTypeConstraint)) { |
| 1089 |
return CurrentState.ColonPos + 2; |
| 1090 |
} |
| 1091 |
|
| 1092 |
const FormatToken &Previous = *Current.Previous; |
| 1093 |
// If we are continuing an expression, we want to use the continuation indent. |
| 1094 |
unsigned ContinuationIndent = |
| 1095 |
std::max(CurrentState.LastSpace, CurrentState.Indent) + |
| 1096 |
Style.ContinuationIndentWidth; |
| 1097 |
const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); |
| 1098 |
const FormatToken *NextNonComment = Previous.getNextNonComment(); |
| 1099 |
if (!NextNonComment) |
| 1100 |
NextNonComment = &Current; |
| 1101 |
|
| 1102 |
// Java specific bits. |
| 1103 |
if (Style.Language == FormatStyle::LK_Java && |
| 1104 |
Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends)) { |
| 1105 |
return std::max(CurrentState.LastSpace, |
| 1106 |
CurrentState.Indent + Style.ContinuationIndentWidth); |
| 1107 |
} |
| 1108 |
|
| 1109 |
// After a goto label. Usually labels are on separate lines. However |
| 1110 |
// for Verilog the labels may be only recognized by the annotator and |
| 1111 |
// thus are on the same line as the current token. |
| 1112 |
if ((Style.isVerilog() && Keywords.isVerilogEndOfLabel(Previous)) || |
| 1113 |
(Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths && |
| 1114 |
State.Line->First->is(tok::kw_enum))) { |
| 1115 |
return (Style.IndentWidth * State.Line->First->IndentLevel) + |
| 1116 |
Style.IndentWidth; |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (NextNonComment->is(tok::l_brace) && NextNonComment->is(BK_Block)) |
| 1120 |
return Current.NestingLevel == 0 ? State.FirstIndent : CurrentState.Indent; |
| 1121 |
if ((Current.isOneOf(tok::r_brace, tok::r_square) || |
| 1122 |
(Current.is(tok::greater) && |
| 1123 |
(Style.Language == FormatStyle::LK_Proto || |
| 1124 |
Style.Language == FormatStyle::LK_TextProto))) && |
| 1125 |
State.Stack.size() > 1) { |
| 1126 |
if (Current.closesBlockOrBlockTypeList(Style)) |
| 1127 |
return State.Stack[State.Stack.size() - 2].NestedBlockIndent; |
| 1128 |
if (Current.MatchingParen && Current.MatchingParen->is(BK_BracedInit)) |
| 1129 |
return State.Stack[State.Stack.size() - 2].LastSpace; |
| 1130 |
return State.FirstIndent; |
| 1131 |
} |
| 1132 |
// Indent a closing parenthesis at the previous level if followed by a semi, |
| 1133 |
// const, or opening brace. This allows indentations such as: |
| 1134 |
// foo( |
| 1135 |
// a, |
| 1136 |
// ); |
| 1137 |
// int Foo::getter( |
| 1138 |
// // |
| 1139 |
// ) const { |
| 1140 |
// return foo; |
| 1141 |
// } |
| 1142 |
// function foo( |
| 1143 |
// a, |
| 1144 |
// ) { |
| 1145 |
// code(); // |
| 1146 |
// } |
| 1147 |
if (Current.is(tok::r_paren) && State.Stack.size() > 1 && |
| 1148 |
(!Current.Next || |
| 1149 |
Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace))) { |
| 1150 |
return State.Stack[State.Stack.size() - 2].LastSpace; |
| 1151 |
} |
| 1152 |
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent && |
| 1153 |
Current.is(tok::r_paren) && State.Stack.size() > 1) { |
| 1154 |
return State.Stack[State.Stack.size() - 2].LastSpace; |
| 1155 |
} |
| 1156 |
if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope()) |
| 1157 |
return State.Stack[State.Stack.size() - 2].LastSpace; |
| 1158 |
if (Current.is(tok::identifier) && Current.Next && |
| 1159 |
(Current.Next->is(TT_DictLiteral) || |
| 1160 |
((Style.Language == FormatStyle::LK_Proto || |
| 1161 |
Style.Language == FormatStyle::LK_TextProto) && |
| 1162 |
Current.Next->isOneOf(tok::less, tok::l_brace)))) { |
| 1163 |
return CurrentState.Indent; |
| 1164 |
} |
| 1165 |
if (NextNonComment->is(TT_ObjCStringLiteral) && |
| 1166 |
State.StartOfStringLiteral != 0) { |
| 1167 |
return State.StartOfStringLiteral - 1; |
| 1168 |
} |
| 1169 |
if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0) |
| 1170 |
return State.StartOfStringLiteral; |
| 1171 |
if (NextNonComment->is(tok::lessless) && CurrentState.FirstLessLess != 0) |
| 1172 |
return CurrentState.FirstLessLess; |
| 1173 |
if (NextNonComment->isMemberAccess()) { |
| 1174 |
if (CurrentState.CallContinuation == 0) |
| 1175 |
return ContinuationIndent; |
| 1176 |
return CurrentState.CallContinuation; |
| 1177 |
} |
| 1178 |
if (CurrentState.QuestionColumn != 0 && |
| 1179 |
((NextNonComment->is(tok::colon) && |
| 1180 |
NextNonComment->is(TT_ConditionalExpr)) || |
| 1181 |
Previous.is(TT_ConditionalExpr))) { |
| 1182 |
if (((NextNonComment->is(tok::colon) && NextNonComment->Next && |
| 1183 |
!NextNonComment->Next->FakeLParens.empty() && |
| 1184 |
NextNonComment->Next->FakeLParens.back() == prec::Conditional) || |
| 1185 |
(Previous.is(tok::colon) && !Current.FakeLParens.empty() && |
| 1186 |
Current.FakeLParens.back() == prec::Conditional)) && |
| 1187 |
!CurrentState.IsWrappedConditional) { |
| 1188 |
// NOTE: we may tweak this slightly: |
| 1189 |
// * not remove the 'lead' ContinuationIndentWidth |
| 1190 |
// * always un-indent by the operator when |
| 1191 |
// BreakBeforeTernaryOperators=true |
| 1192 |
unsigned Indent = CurrentState.Indent; |
| 1193 |
if (Style.AlignOperands != FormatStyle::OAS_DontAlign) |
| 1194 |
Indent -= Style.ContinuationIndentWidth; |
| 1195 |
if (Style.BreakBeforeTernaryOperators && CurrentState.UnindentOperator) |
| 1196 |
Indent -= 2; |
| 1197 |
return Indent; |
| 1198 |
} |
| 1199 |
return CurrentState.QuestionColumn; |
| 1200 |
} |
| 1201 |
if (Previous.is(tok::comma) && CurrentState.VariablePos != 0) |
| 1202 |
return CurrentState.VariablePos; |
| 1203 |
if (Current.is(TT_RequiresClause)) { |
| 1204 |
if (Style.IndentRequiresClause) |
| 1205 |
return CurrentState.Indent + Style.IndentWidth; |
| 1206 |
switch (Style.RequiresClausePosition) { |
| 1207 |
case FormatStyle::RCPS_OwnLine: |
| 1208 |
case FormatStyle::RCPS_WithFollowing: |
| 1209 |
return CurrentState.Indent; |
| 1210 |
default: |
| 1211 |
break; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon, |
| 1215 |
TT_InheritanceComma)) { |
| 1216 |
return State.FirstIndent + Style.ConstructorInitializerIndentWidth; |
| 1217 |
} |
| 1218 |
if ((PreviousNonComment && |
| 1219 |
(PreviousNonComment->ClosesTemplateDeclaration || |
| 1220 |
PreviousNonComment->ClosesRequiresClause || |
| 1221 |
PreviousNonComment->isOneOf( |
| 1222 |
TT_AttributeParen, TT_AttributeSquare, TT_FunctionAnnotationRParen, |
| 1223 |
TT_JavaAnnotation, TT_LeadingJavaAnnotation))) || |
| 1224 |
(!Style.IndentWrappedFunctionNames && |
| 1225 |
NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName))) { |
| 1226 |
return std::max(CurrentState.LastSpace, CurrentState.Indent); |
| 1227 |
} |
| 1228 |
if (NextNonComment->is(TT_SelectorName)) { |
| 1229 |
if (!CurrentState.ObjCSelectorNameFound) { |
| 1230 |
unsigned MinIndent = CurrentState.Indent; |
| 1231 |
if (shouldIndentWrappedSelectorName(Style, State.Line->Type)) { |
| 1232 |
MinIndent = std::max(MinIndent, |
| 1233 |
State.FirstIndent + Style.ContinuationIndentWidth); |
| 1234 |
} |
| 1235 |
// If LongestObjCSelectorName is 0, we are indenting the first |
| 1236 |
// part of an ObjC selector (or a selector component which is |
| 1237 |
// not colon-aligned due to block formatting). |
| 1238 |
// |
| 1239 |
// Otherwise, we are indenting a subsequent part of an ObjC |
| 1240 |
// selector which should be colon-aligned to the longest |
| 1241 |
// component of the ObjC selector. |
| 1242 |
// |
| 1243 |
// In either case, we want to respect Style.IndentWrappedFunctionNames. |
| 1244 |
return MinIndent + |
| 1245 |
std::max(NextNonComment->LongestObjCSelectorName, |
| 1246 |
NextNonComment->ColumnWidth) - |
| 1247 |
NextNonComment->ColumnWidth; |
| 1248 |
} |
| 1249 |
if (!CurrentState.AlignColons) |
| 1250 |
return CurrentState.Indent; |
| 1251 |
if (CurrentState.ColonPos > NextNonComment->ColumnWidth) |
| 1252 |
return CurrentState.ColonPos - NextNonComment->ColumnWidth; |
| 1253 |
return CurrentState.Indent; |
| 1254 |
} |
| 1255 |
if (NextNonComment->is(tok::colon) && NextNonComment->is(TT_ObjCMethodExpr)) |
| 1256 |
return CurrentState.ColonPos; |
| 1257 |
if (NextNonComment->is(TT_ArraySubscriptLSquare)) { |
| 1258 |
if (CurrentState.StartOfArraySubscripts != 0) { |
| 1259 |
return CurrentState.StartOfArraySubscripts; |
| 1260 |
} else if (Style.isCSharp()) { // C# allows `["key"] = value` inside object |
| 1261 |
// initializers. |
| 1262 |
return CurrentState.Indent; |
| 1263 |
} |
| 1264 |
return ContinuationIndent; |
| 1265 |
} |
| 1266 |
|
| 1267 |
if (State.Line->InPragmaDirective) |
| 1268 |
return CurrentState.Indent + Style.ContinuationIndentWidth; |
| 1269 |
|
| 1270 |
// This ensure that we correctly format ObjC methods calls without inputs, |
| 1271 |
// i.e. where the last element isn't selector like: [callee method]; |
| 1272 |
if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 && |
| 1273 |
NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr)) { |
| 1274 |
return CurrentState.Indent; |
| 1275 |
} |
| 1276 |
|
| 1277 |
if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) || |
| 1278 |
Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon)) { |
| 1279 |
return ContinuationIndent; |
| 1280 |
} |
| 1281 |
if (PreviousNonComment && PreviousNonComment->is(tok::colon) && |
| 1282 |
PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) { |
| 1283 |
return ContinuationIndent; |
| 1284 |
} |
| 1285 |
if (NextNonComment->is(TT_CtorInitializerComma)) |
| 1286 |
return CurrentState.Indent; |
| 1287 |
if (PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) && |
| 1288 |
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) { |
| 1289 |
return CurrentState.Indent; |
| 1290 |
} |
| 1291 |
if (PreviousNonComment && PreviousNonComment->is(TT_InheritanceColon) && |
| 1292 |
Style.BreakInheritanceList == FormatStyle::BILS_AfterColon) { |
| 1293 |
return CurrentState.Indent; |
| 1294 |
} |
| 1295 |
if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() && |
| 1296 |
!Current.isOneOf(tok::colon, tok::comment)) { |
| 1297 |
return ContinuationIndent; |
| 1298 |
} |
| 1299 |
if (Current.is(TT_ProtoExtensionLSquare)) |
| 1300 |
return CurrentState.Indent; |
| 1301 |
if (Current.isBinaryOperator() && CurrentState.UnindentOperator) { |
| 1302 |
return CurrentState.Indent - Current.Tok.getLength() - |
| 1303 |
Current.SpacesRequiredBefore; |
| 1304 |
} |
| 1305 |
if (Current.isOneOf(tok::comment, TT_BlockComment, TT_LineComment) && |
| 1306 |
NextNonComment->isBinaryOperator() && CurrentState.UnindentOperator) { |
| 1307 |
return CurrentState.Indent - NextNonComment->Tok.getLength() - |
| 1308 |
NextNonComment->SpacesRequiredBefore; |
| 1309 |
} |
| 1310 |
if (CurrentState.Indent == State.FirstIndent && PreviousNonComment && |
| 1311 |
!PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma)) { |
| 1312 |
// Ensure that we fall back to the continuation indent width instead of |
| 1313 |
// just flushing continuations left. |
| 1314 |
return CurrentState.Indent + Style.ContinuationIndentWidth; |
| 1315 |
} |
| 1316 |
return CurrentState.Indent; |
| 1317 |
} |
| 1318 |
|
| 1319 |
static bool hasNestedBlockInlined(const FormatToken *Previous, |
| 1320 |
const FormatToken &Current, |
| 1321 |
const FormatStyle &Style) { |
| 1322 |
if (Previous->isNot(tok::l_paren)) |
| 1323 |
return true; |
| 1324 |
if (Previous->ParameterCount > 1) |
| 1325 |
return true; |
| 1326 |
|
| 1327 |
// Also a nested block if contains a lambda inside function with 1 parameter. |
| 1328 |
return Style.BraceWrapping.BeforeLambdaBody && Current.is(TT_LambdaLSquare); |
| 1329 |
} |
| 1330 |
|
| 1331 |
unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, |
| 1332 |
bool DryRun, bool Newline) { |
| 1333 |
assert(State.Stack.size()); |
| 1334 |
const FormatToken &Current = *State.NextToken; |
| 1335 |
auto &CurrentState = State.Stack.back(); |
| 1336 |
|
| 1337 |
if (Current.is(TT_CSharpGenericTypeConstraint)) |
| 1338 |
CurrentState.IsCSharpGenericTypeConstraint = true; |
| 1339 |
if (Current.isOneOf(tok::comma, TT_BinaryOperator)) |
| 1340 |
CurrentState.NoLineBreakInOperand = false; |
| 1341 |
if (Current.isOneOf(TT_InheritanceColon, TT_CSharpGenericTypeConstraintColon)) |
| 1342 |
CurrentState.AvoidBinPacking = true; |
| 1343 |
if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) { |
| 1344 |
if (CurrentState.FirstLessLess == 0) |
| 1345 |
CurrentState.FirstLessLess = State.Column; |
| 1346 |
else |
| 1347 |
CurrentState.LastOperatorWrapped = Newline; |
| 1348 |
} |
| 1349 |
if (Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless)) |
| 1350 |
CurrentState.LastOperatorWrapped = Newline; |
| 1351 |
if (Current.is(TT_ConditionalExpr) && Current.Previous && |
| 1352 |
!Current.Previous->is(TT_ConditionalExpr)) { |
| 1353 |
CurrentState.LastOperatorWrapped = Newline; |
| 1354 |
} |
| 1355 |
if (Current.is(TT_ArraySubscriptLSquare) && |
| 1356 |
CurrentState.StartOfArraySubscripts == 0) { |
| 1357 |
CurrentState.StartOfArraySubscripts = State.Column; |
| 1358 |
} |
| 1359 |
|
| 1360 |
auto IsWrappedConditional = [](const FormatToken &Tok) { |
| 1361 |
if (!(Tok.is(TT_ConditionalExpr) && Tok.is(tok::question))) |
| 1362 |
return false; |
| 1363 |
if (Tok.MustBreakBefore) |
| 1364 |
return true; |
| 1365 |
|
| 1366 |
const FormatToken *Next = Tok.getNextNonComment(); |
| 1367 |
return Next && Next->MustBreakBefore; |
| 1368 |
}; |
| 1369 |
if (IsWrappedConditional(Current)) |
| 1370 |
CurrentState.IsWrappedConditional = true; |
| 1371 |
if (Style.BreakBeforeTernaryOperators && Current.is(tok::question)) |
| 1372 |
CurrentState.QuestionColumn = State.Column; |
| 1373 |
if (!Style.BreakBeforeTernaryOperators && Current.isNot(tok::colon)) { |
| 1374 |
const FormatToken *Previous = Current.Previous; |
| 1375 |
while (Previous && Previous->isTrailingComment()) |
| 1376 |
Previous = Previous->Previous; |
| 1377 |
if (Previous && Previous->is(tok::question)) |
| 1378 |
CurrentState.QuestionColumn = State.Column; |
| 1379 |
} |
| 1380 |
if (!Current.opensScope() && !Current.closesScope() && |
| 1381 |
!Current.is(TT_PointerOrReference)) { |
| 1382 |
State.LowestLevelOnLine = |
| 1383 |
std::min(State.LowestLevelOnLine, Current.NestingLevel); |
| 1384 |
} |
| 1385 |
if (Current.isMemberAccess()) |
| 1386 |
CurrentState.StartOfFunctionCall = !Current.NextOperator ? 0 : State.Column; |
| 1387 |
if (Current.is(TT_SelectorName)) |
| 1388 |
CurrentState.ObjCSelectorNameFound = true; |
| 1389 |
if (Current.is(TT_CtorInitializerColon) && |
| 1390 |
Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon) { |
| 1391 |
// Indent 2 from the column, so: |
| 1392 |
// SomeClass::SomeClass() |
| 1393 |
// : First(...), ... |
| 1394 |
// Next(...) |
| 1395 |
// ^ line up here. |
| 1396 |
CurrentState.Indent = State.Column + (Style.BreakConstructorInitializers == |
| 1397 |
FormatStyle::BCIS_BeforeComma |
| 1398 |
? 0 |
| 1399 |
: 2); |
| 1400 |
CurrentState.NestedBlockIndent = CurrentState.Indent; |
| 1401 |
if (Style.PackConstructorInitializers > FormatStyle::PCIS_BinPack) { |
| 1402 |
CurrentState.AvoidBinPacking = true; |
| 1403 |
CurrentState.BreakBeforeParameter = |
| 1404 |
Style.PackConstructorInitializers != FormatStyle::PCIS_NextLine; |
| 1405 |
} else { |
| 1406 |
CurrentState.BreakBeforeParameter = false; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
if (Current.is(TT_CtorInitializerColon) && |
| 1410 |
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) { |
| 1411 |
CurrentState.Indent = |
| 1412 |
State.FirstIndent + Style.ConstructorInitializerIndentWidth; |
| 1413 |
CurrentState.NestedBlockIndent = CurrentState.Indent; |
| 1414 |
if (Style.PackConstructorInitializers > FormatStyle::PCIS_BinPack) |
| 1415 |
CurrentState.AvoidBinPacking = true; |
| 1416 |
} |
| 1417 |
if (Current.is(TT_InheritanceColon)) { |
| 1418 |
CurrentState.Indent = |
| 1419 |
State.FirstIndent + Style.ConstructorInitializerIndentWidth; |
| 1420 |
} |
| 1421 |
if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline) |
| 1422 |
CurrentState.NestedBlockIndent = State.Column + Current.ColumnWidth + 1; |
| 1423 |
if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow)) |
| 1424 |
CurrentState.LastSpace = State.Column; |
| 1425 |
if (Current.is(TT_RequiresExpression) && |
| 1426 |
Style.RequiresExpressionIndentation == FormatStyle::REI_Keyword) { |
| 1427 |
CurrentState.NestedBlockIndent = State.Column; |
| 1428 |
} |
| 1429 |
|
| 1430 |
// Insert scopes created by fake parenthesis. |
| 1431 |
const FormatToken *Previous = Current.getPreviousNonComment(); |
| 1432 |
|
| 1433 |
// Add special behavior to support a format commonly used for JavaScript |
| 1434 |
// closures: |
| 1435 |
// SomeFunction(function() { |
| 1436 |
// foo(); |
| 1437 |
// bar(); |
| 1438 |
// }, a, b, c); |
| 1439 |
if (Current.isNot(tok::comment) && !Current.ClosesRequiresClause && |
| 1440 |
Previous && Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) && |
| 1441 |
!Previous->is(TT_DictLiteral) && State.Stack.size() > 1 && |
| 1442 |
!CurrentState.HasMultipleNestedBlocks) { |
| 1443 |
if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline) |
| 1444 |
for (ParenState &PState : llvm::drop_end(State.Stack)) |
| 1445 |
PState.NoLineBreak = true; |
| 1446 |
State.Stack[State.Stack.size() - 2].NestedBlockInlined = false; |
| 1447 |
} |
| 1448 |
if (Previous && (Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) || |
| 1449 |
(Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) && |
| 1450 |
!Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))) { |
| 1451 |
CurrentState.NestedBlockInlined = |
| 1452 |
!Newline && hasNestedBlockInlined(Previous, Current, Style); |
| 1453 |
} |
| 1454 |
|
| 1455 |
moveStatePastFakeLParens(State, Newline); |
| 1456 |
moveStatePastScopeCloser(State); |
| 1457 |
// Do not use CurrentState here, since the two functions before may change the |
| 1458 |
// Stack. |
| 1459 |
bool AllowBreak = !State.Stack.back().NoLineBreak && |
| 1460 |
!State.Stack.back().NoLineBreakInOperand; |
| 1461 |
moveStatePastScopeOpener(State, Newline); |
| 1462 |
moveStatePastFakeRParens(State); |
| 1463 |
|
| 1464 |
if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0) |
| 1465 |
State.StartOfStringLiteral = State.Column + 1; |
| 1466 |
if (Current.is(TT_CSharpStringLiteral) && State.StartOfStringLiteral == 0) { |
| 1467 |
State.StartOfStringLiteral = State.Column + 1; |
| 1468 |
} else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) { |
| 1469 |
State.StartOfStringLiteral = State.Column; |
| 1470 |
} else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) && |
| 1471 |
!Current.isStringLiteral()) { |
| 1472 |
State.StartOfStringLiteral = 0; |
| 1473 |
} |
| 1474 |
|
| 1475 |
State.Column += Current.ColumnWidth; |
| 1476 |
State.NextToken = State.NextToken->Next; |
| 1477 |
|
| 1478 |
unsigned Penalty = |
| 1479 |
handleEndOfLine(Current, State, DryRun, AllowBreak, Newline); |
| 1480 |
|
| 1481 |
if (Current.Role) |
| 1482 |
Current.Role->formatFromToken(State, this, DryRun); |
| 1483 |
// If the previous has a special role, let it consume tokens as appropriate. |
| 1484 |
// It is necessary to start at the previous token for the only implemented |
| 1485 |
// role (comma separated list). That way, the decision whether or not to break |
| 1486 |
// after the "{" is already done and both options are tried and evaluated. |
| 1487 |
// FIXME: This is ugly, find a better way. |
| 1488 |
if (Previous && Previous->Role) |
| 1489 |
Penalty += Previous->Role->formatAfterToken(State, this, DryRun); |
| 1490 |
|
| 1491 |
return Penalty; |
| 1492 |
} |
| 1493 |
|
| 1494 |
void ContinuationIndenter::moveStatePastFakeLParens(LineState &State, |
| 1495 |
bool Newline) { |
| 1496 |
const FormatToken &Current = *State.NextToken; |
| 1497 |
if (Current.FakeLParens.empty()) |
| 1498 |
return; |
| 1499 |
|
| 1500 |
const FormatToken *Previous = Current.getPreviousNonComment(); |
| 1501 |
|
| 1502 |
// Don't add extra indentation for the first fake parenthesis after |
| 1503 |
// 'return', assignments, opening <({[, or requires clauses. The indentation |
| 1504 |
// for these cases is special cased. |
| 1505 |
bool SkipFirstExtraIndent = |
| 1506 |
Previous && |
| 1507 |
(Previous->opensScope() || |
| 1508 |
Previous->isOneOf(tok::semi, tok::kw_return, TT_RequiresClause) || |
| 1509 |
(Previous->getPrecedence() == prec::Assignment && |
| 1510 |
Style.AlignOperands != FormatStyle::OAS_DontAlign) || |
| 1511 |
Previous->is(TT_ObjCMethodExpr)); |
| 1512 |
for (const auto &PrecedenceLevel : llvm::reverse(Current.FakeLParens)) { |
| 1513 |
const auto &CurrentState = State.Stack.back(); |
| 1514 |
ParenState NewParenState = CurrentState; |
| 1515 |
NewParenState.Tok = nullptr; |
| 1516 |
NewParenState.ContainsLineBreak = false; |
| 1517 |
NewParenState.LastOperatorWrapped = true; |
| 1518 |
NewParenState.IsChainedConditional = false; |
| 1519 |
NewParenState.IsWrappedConditional = false; |
| 1520 |
NewParenState.UnindentOperator = false; |
| 1521 |
NewParenState.NoLineBreak = |
| 1522 |
NewParenState.NoLineBreak || CurrentState.NoLineBreakInOperand; |
| 1523 |
|
| 1524 |
// Don't propagate AvoidBinPacking into subexpressions of arg/param lists. |
| 1525 |
if (PrecedenceLevel > prec::Comma) |
| 1526 |
NewParenState.AvoidBinPacking = false; |
| 1527 |
|
| 1528 |
// Indent from 'LastSpace' unless these are fake parentheses encapsulating |
| 1529 |
// a builder type call after 'return' or, if the alignment after opening |
| 1530 |
// brackets is disabled. |
| 1531 |
if (!Current.isTrailingComment() && |
| 1532 |
(Style.AlignOperands != FormatStyle::OAS_DontAlign || |
| 1533 |
PrecedenceLevel < prec::Assignment) && |
| 1534 |
(!Previous || Previous->isNot(tok::kw_return) || |
| 1535 |
(Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) && |
| 1536 |
(Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign || |
| 1537 |
PrecedenceLevel != prec::Comma || Current.NestingLevel == 0)) { |
| 1538 |
NewParenState.Indent = std::max( |
| 1539 |
std::max(State.Column, NewParenState.Indent), CurrentState.LastSpace); |
| 1540 |
} |
| 1541 |
|
| 1542 |
// Special case for generic selection expressions, its comma-separated |
| 1543 |
// expressions are not aligned to the opening paren like regular calls, but |
| 1544 |
// rather continuation-indented relative to the _Generic keyword. |
| 1545 |
if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic)) |
| 1546 |
NewParenState.Indent = CurrentState.LastSpace; |
| 1547 |
|
| 1548 |
if (Previous && |
| 1549 |
(Previous->getPrecedence() == prec::Assignment || |
| 1550 |
Previous->isOneOf(tok::kw_return, TT_RequiresClause) || |
| 1551 |
(PrecedenceLevel == prec::Conditional && Previous->is(tok::question) && |
| 1552 |
Previous->is(TT_ConditionalExpr))) && |
| 1553 |
!Newline) { |
| 1554 |
// If BreakBeforeBinaryOperators is set, un-indent a bit to account for |
| 1555 |
// the operator and keep the operands aligned. |
| 1556 |
if (Style.AlignOperands == FormatStyle::OAS_AlignAfterOperator) |
| 1557 |
NewParenState.UnindentOperator = true; |
| 1558 |
// Mark indentation as alignment if the expression is aligned. |
| 1559 |
if (Style.AlignOperands != FormatStyle::OAS_DontAlign) |
| 1560 |
NewParenState.IsAligned = true; |
| 1561 |
} |
| 1562 |
|
| 1563 |
// Do not indent relative to the fake parentheses inserted for "." or "->". |
| 1564 |
// This is a special case to make the following to statements consistent: |
| 1565 |
// OuterFunction(InnerFunctionCall( // break |
| 1566 |
// ParameterToInnerFunction)); |
| 1567 |
// OuterFunction(SomeObject.InnerFunctionCall( // break |
| 1568 |
// ParameterToInnerFunction)); |
| 1569 |
if (PrecedenceLevel > prec::Unknown) |
| 1570 |
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column); |
| 1571 |
if (PrecedenceLevel != prec::Conditional && !Current.is(TT_UnaryOperator) && |
| 1572 |
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) { |
| 1573 |
NewParenState.StartOfFunctionCall = State.Column; |
| 1574 |
} |
| 1575 |
|
| 1576 |
// Indent conditional expressions, unless they are chained "else-if" |
| 1577 |
// conditionals. Never indent expression where the 'operator' is ',', ';' or |
| 1578 |
// an assignment (i.e. *I <= prec::Assignment) as those have different |
| 1579 |
// indentation rules. Indent other expression, unless the indentation needs |
| 1580 |
// to be skipped. |
| 1581 |
if (PrecedenceLevel == prec::Conditional && Previous && |
| 1582 |
Previous->is(tok::colon) && Previous->is(TT_ConditionalExpr) && |
| 1583 |
&PrecedenceLevel == &Current.FakeLParens.back() && |
| 1584 |
!CurrentState.IsWrappedConditional) { |
| 1585 |
NewParenState.IsChainedConditional = true; |
| 1586 |
NewParenState.UnindentOperator = State.Stack.back().UnindentOperator; |
| 1587 |
} else if (PrecedenceLevel == prec::Conditional || |
| 1588 |
(!SkipFirstExtraIndent && PrecedenceLevel > prec::Assignment && |
| 1589 |
!Current.isTrailingComment())) { |
| 1590 |
NewParenState.Indent += Style.ContinuationIndentWidth; |
| 1591 |
} |
| 1592 |
if ((Previous && !Previous->opensScope()) || PrecedenceLevel != prec::Comma) |
| 1593 |
NewParenState.BreakBeforeParameter = false; |
| 1594 |
State.Stack.push_back(NewParenState); |
| 1595 |
SkipFirstExtraIndent = false; |
| 1596 |
} |
| 1597 |
} |
| 1598 |
|
| 1599 |
void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) { |
| 1600 |
for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) { |
| 1601 |
unsigned VariablePos = State.Stack.back().VariablePos; |
| 1602 |
if (State.Stack.size() == 1) { |
| 1603 |
// Do not pop the last element. |
| 1604 |
break; |
| 1605 |
} |
| 1606 |
State.Stack.pop_back(); |
| 1607 |
State.Stack.back().VariablePos = VariablePos; |
| 1608 |
} |
| 1609 |
|
| 1610 |
if (State.NextToken->ClosesRequiresClause && Style.IndentRequiresClause) { |
| 1611 |
// Remove the indentation of the requires clauses (which is not in Indent, |
| 1612 |
// but in LastSpace). |
| 1613 |
State.Stack.back().LastSpace -= Style.IndentWidth; |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, |
| 1618 |
bool Newline) { |
| 1619 |
const FormatToken &Current = *State.NextToken; |
| 1620 |
if (!Current.opensScope()) |
| 1621 |
return; |
| 1622 |
|
| 1623 |
const auto &CurrentState = State.Stack.back(); |
| 1624 |
|
| 1625 |
// Don't allow '<' or '(' in C# generic type constraints to start new scopes. |
| 1626 |
if (Current.isOneOf(tok::less, tok::l_paren) && |
| 1627 |
CurrentState.IsCSharpGenericTypeConstraint) { |
| 1628 |
return; |
| 1629 |
} |
| 1630 |
|
| 1631 |
if (Current.MatchingParen && Current.is(BK_Block)) { |
| 1632 |
moveStateToNewBlock(State); |
| 1633 |
return; |
| 1634 |
} |
| 1635 |
|
| 1636 |
unsigned NewIndent; |
| 1637 |
unsigned LastSpace = CurrentState.LastSpace; |
| 1638 |
bool AvoidBinPacking; |
| 1639 |
bool BreakBeforeParameter = false; |
| 1640 |
unsigned NestedBlockIndent = std::max(CurrentState.StartOfFunctionCall, |
| 1641 |
CurrentState.NestedBlockIndent); |
| 1642 |
if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || |
| 1643 |
opensProtoMessageField(Current, Style)) { |
| 1644 |
if (Current.opensBlockOrBlockTypeList(Style)) { |
| 1645 |
NewIndent = Style.IndentWidth + |
| 1646 |
std::min(State.Column, CurrentState.NestedBlockIndent); |
| 1647 |
} else { |
| 1648 |
NewIndent = CurrentState.LastSpace + Style.ContinuationIndentWidth; |
| 1649 |
} |
| 1650 |
const FormatToken *NextNoComment = Current.getNextNonComment(); |
| 1651 |
bool EndsInComma = Current.MatchingParen && |
| 1652 |
Current.MatchingParen->Previous && |
| 1653 |
Current.MatchingParen->Previous->is(tok::comma); |
| 1654 |
AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) || |
| 1655 |
Style.Language == FormatStyle::LK_Proto || |
| 1656 |
Style.Language == FormatStyle::LK_TextProto || |
| 1657 |
!Style.BinPackArguments || |
| 1658 |
(NextNoComment && |
| 1659 |
NextNoComment->isOneOf(TT_DesignatedInitializerPeriod, |
| 1660 |
TT_DesignatedInitializerLSquare)); |
| 1661 |
BreakBeforeParameter = EndsInComma; |
| 1662 |
if (Current.ParameterCount > 1) |
| 1663 |
NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1); |
| 1664 |
} else { |
| 1665 |
NewIndent = |
| 1666 |
Style.ContinuationIndentWidth + |
| 1667 |
std::max(CurrentState.LastSpace, CurrentState.StartOfFunctionCall); |
| 1668 |
|
| 1669 |
// Ensure that different different brackets force relative alignment, e.g.: |
| 1670 |
// void SomeFunction(vector< // break |
| 1671 |
// int> v); |
| 1672 |
// FIXME: We likely want to do this for more combinations of brackets. |
| 1673 |
if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) { |
| 1674 |
NewIndent = std::max(NewIndent, CurrentState.Indent); |
| 1675 |
LastSpace = std::max(LastSpace, CurrentState.Indent); |
| 1676 |
} |
| 1677 |
|
| 1678 |
bool EndsInComma = |
| 1679 |
Current.MatchingParen && |
| 1680 |
Current.MatchingParen->getPreviousNonComment() && |
| 1681 |
Current.MatchingParen->getPreviousNonComment()->is(tok::comma); |
| 1682 |
|
| 1683 |
// If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters |
| 1684 |
// for backwards compatibility. |
| 1685 |
bool ObjCBinPackProtocolList = |
| 1686 |
(Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto && |
| 1687 |
Style.BinPackParameters) || |
| 1688 |
Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always; |
| 1689 |
|
| 1690 |
bool BinPackDeclaration = |
| 1691 |
(State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) || |
| 1692 |
(State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList); |
| 1693 |
|
| 1694 |
bool GenericSelection = |
| 1695 |
Current.getPreviousNonComment() && |
| 1696 |
Current.getPreviousNonComment()->is(tok::kw__Generic); |
| 1697 |
|
| 1698 |
AvoidBinPacking = |
| 1699 |
(CurrentState.IsCSharpGenericTypeConstraint) || GenericSelection || |
| 1700 |
(Style.isJavaScript() && EndsInComma) || |
| 1701 |
(State.Line->MustBeDeclaration && !BinPackDeclaration) || |
| 1702 |
(!State.Line->MustBeDeclaration && !Style.BinPackArguments) || |
| 1703 |
(Style.ExperimentalAutoDetectBinPacking && |
| 1704 |
(Current.is(PPK_OnePerLine) || |
| 1705 |
(!BinPackInconclusiveFunctions && Current.is(PPK_Inconclusive)))); |
| 1706 |
|
| 1707 |
if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen && |
| 1708 |
Style.ObjCBreakBeforeNestedBlockParam) { |
| 1709 |
if (Style.ColumnLimit) { |
| 1710 |
// If this '[' opens an ObjC call, determine whether all parameters fit |
| 1711 |
// into one line and put one per line if they don't. |
| 1712 |
if (getLengthToMatchingParen(Current, State.Stack) + State.Column > |
| 1713 |
getColumnLimit(State)) { |
| 1714 |
BreakBeforeParameter = true; |
| 1715 |
} |
| 1716 |
} else { |
| 1717 |
// For ColumnLimit = 0, we have to figure out whether there is or has to |
| 1718 |
// be a line break within this call. |
| 1719 |
for (const FormatToken *Tok = &Current; |
| 1720 |
Tok && Tok != Current.MatchingParen; Tok = Tok->Next) { |
| 1721 |
if (Tok->MustBreakBefore || |
| 1722 |
(Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) { |
| 1723 |
BreakBeforeParameter = true; |
| 1724 |
break; |
| 1725 |
} |
| 1726 |
} |
| 1727 |
} |
| 1728 |
} |
| 1729 |
|
| 1730 |
if (Style.isJavaScript() && EndsInComma) |
| 1731 |
BreakBeforeParameter = true; |
| 1732 |
} |
| 1733 |
// Generally inherit NoLineBreak from the current scope to nested scope. |
| 1734 |
// However, don't do this for non-empty nested blocks, dict literals and |
| 1735 |
// array literals as these follow different indentation rules. |
| 1736 |
bool NoLineBreak = |
| 1737 |
Current.Children.empty() && |
| 1738 |
!Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) && |
| 1739 |
(CurrentState.NoLineBreak || CurrentState.NoLineBreakInOperand || |
| 1740 |
(Current.is(TT_TemplateOpener) && |
| 1741 |
CurrentState.ContainsUnwrappedBuilder)); |
| 1742 |
State.Stack.push_back( |
| 1743 |
ParenState(&Current, NewIndent, LastSpace, AvoidBinPacking, NoLineBreak)); |
| 1744 |
auto &NewState = State.Stack.back(); |
| 1745 |
NewState.NestedBlockIndent = NestedBlockIndent; |
| 1746 |
NewState.BreakBeforeParameter = BreakBeforeParameter; |
| 1747 |
NewState.HasMultipleNestedBlocks = (Current.BlockParameterCount > 1); |
| 1748 |
|
| 1749 |
if (Style.BraceWrapping.BeforeLambdaBody && Current.Next != nullptr && |
| 1750 |
Current.is(tok::l_paren)) { |
| 1751 |
// Search for any parameter that is a lambda. |
| 1752 |
FormatToken const *next = Current.Next; |
| 1753 |
while (next != nullptr) { |
| 1754 |
if (next->is(TT_LambdaLSquare)) { |
| 1755 |
NewState.HasMultipleNestedBlocks = true; |
| 1756 |
break; |
| 1757 |
} |
| 1758 |
next = next->Next; |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
NewState.IsInsideObjCArrayLiteral = Current.is(TT_ArrayInitializerLSquare) && |
| 1763 |
Current.Previous && |
| 1764 |
Current.Previous->is(tok::at); |
| 1765 |
} |
| 1766 |
|
| 1767 |
void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) { |
| 1768 |
const FormatToken &Current = *State.NextToken; |
| 1769 |
if (!Current.closesScope()) |
| 1770 |
return; |
| 1771 |
|
| 1772 |
// If we encounter a closing ), ], } or >, we can remove a level from our |
| 1773 |
// stacks. |
| 1774 |
if (State.Stack.size() > 1 && |
| 1775 |
(Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) || |
| 1776 |
(Current.is(tok::r_brace) && State.NextToken != State.Line->First) || |
| 1777 |
State.NextToken->is(TT_TemplateCloser) || |
| 1778 |
(Current.is(tok::greater) && Current.is(TT_DictLiteral)))) { |
| 1779 |
State.Stack.pop_back(); |
| 1780 |
} |
| 1781 |
|
| 1782 |
auto &CurrentState = State.Stack.back(); |
| 1783 |
|
| 1784 |
// Reevaluate whether ObjC message arguments fit into one line. |
| 1785 |
// If a receiver spans multiple lines, e.g.: |
| 1786 |
// [[object block:^{ |
| 1787 |
// return 42; |
| 1788 |
// }] a:42 b:42]; |
| 1789 |
// BreakBeforeParameter is calculated based on an incorrect assumption |
| 1790 |
// (it is checked whether the whole expression fits into one line without |
| 1791 |
// considering a line break inside a message receiver). |
| 1792 |
// We check whether arguments fit after receiver scope closer (into the same |
| 1793 |
// line). |
| 1794 |
if (CurrentState.BreakBeforeParameter && Current.MatchingParen && |
| 1795 |
Current.MatchingParen->Previous) { |
| 1796 |
const FormatToken &CurrentScopeOpener = *Current.MatchingParen->Previous; |
| 1797 |
if (CurrentScopeOpener.is(TT_ObjCMethodExpr) && |
| 1798 |
CurrentScopeOpener.MatchingParen) { |
| 1799 |
int NecessarySpaceInLine = |
| 1800 |
getLengthToMatchingParen(CurrentScopeOpener, State.Stack) + |
| 1801 |
CurrentScopeOpener.TotalLength - Current.TotalLength - 1; |
| 1802 |
if (State.Column + Current.ColumnWidth + NecessarySpaceInLine <= |
| 1803 |
Style.ColumnLimit) { |
| 1804 |
CurrentState.BreakBeforeParameter = false; |
| 1805 |
} |
| 1806 |
} |
| 1807 |
} |
| 1808 |
|
| 1809 |
if (Current.is(tok::r_square)) { |
| 1810 |
// If this ends the array subscript expr, reset the corresponding value. |
| 1811 |
const FormatToken *NextNonComment = Current.getNextNonComment(); |
| 1812 |
if (NextNonComment && NextNonComment->isNot(tok::l_square)) |
| 1813 |
CurrentState.StartOfArraySubscripts = 0; |
| 1814 |
} |
| 1815 |
} |
| 1816 |
|
| 1817 |
void ContinuationIndenter::moveStateToNewBlock(LineState &State) { |
| 1818 |
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent; |
| 1819 |
// ObjC block sometimes follow special indentation rules. |
| 1820 |
unsigned NewIndent = |
| 1821 |
NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace) |
| 1822 |
? Style.ObjCBlockIndentWidth |
| 1823 |
: Style.IndentWidth); |
| 1824 |
State.Stack.push_back(ParenState(State.NextToken, NewIndent, |
| 1825 |
State.Stack.back().LastSpace, |
| 1826 |
/*AvoidBinPacking=*/true, |
| 1827 |
/*NoLineBreak=*/false)); |
| 1828 |
State.Stack.back().NestedBlockIndent = NestedBlockIndent; |
| 1829 |
State.Stack.back().BreakBeforeParameter = true; |
| 1830 |
} |
| 1831 |
|
| 1832 |
static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn, |
| 1833 |
unsigned TabWidth, |
| 1834 |
encoding::Encoding Encoding) { |
| 1835 |
size_t LastNewlinePos = Text.find_last_of("\n"); |
| 1836 |
if (LastNewlinePos == StringRef::npos) { |
| 1837 |
return StartColumn + |
| 1838 |
encoding::columnWidthWithTabs(Text, StartColumn, TabWidth, Encoding); |
| 1839 |
} else { |
| 1840 |
return encoding::columnWidthWithTabs(Text.substr(LastNewlinePos), |
| 1841 |
/*StartColumn=*/0, TabWidth, Encoding); |
| 1842 |
} |
| 1843 |
} |
| 1844 |
|
| 1845 |
unsigned ContinuationIndenter::reformatRawStringLiteral( |
| 1846 |
const FormatToken &Current, LineState &State, |
| 1847 |
const FormatStyle &RawStringStyle, bool DryRun, bool Newline) { |
| 1848 |
unsigned StartColumn = State.Column - Current.ColumnWidth; |
| 1849 |
StringRef OldDelimiter = *getRawStringDelimiter(Current.TokenText); |
| 1850 |
StringRef NewDelimiter = |
| 1851 |
getCanonicalRawStringDelimiter(Style, RawStringStyle.Language); |
| 1852 |
if (NewDelimiter.empty()) |
| 1853 |
NewDelimiter = OldDelimiter; |
| 1854 |
// The text of a raw string is between the leading 'R"delimiter(' and the |
| 1855 |
// trailing 'delimiter)"'. |
| 1856 |
unsigned OldPrefixSize = 3 + OldDelimiter.size(); |
| 1857 |
unsigned OldSuffixSize = 2 + OldDelimiter.size(); |
| 1858 |
// We create a virtual text environment which expects a null-terminated |
| 1859 |
// string, so we cannot use StringRef. |
| 1860 |
std::string RawText = std::string( |
| 1861 |
Current.TokenText.substr(OldPrefixSize).drop_back(OldSuffixSize)); |
| 1862 |
if (NewDelimiter != OldDelimiter) { |
| 1863 |
// Don't update to the canonical delimiter 'deli' if ')deli"' occurs in the |
| 1864 |
// raw string. |
| 1865 |
std::string CanonicalDelimiterSuffix = (")" + NewDelimiter + "\"").str(); |
| 1866 |
if (StringRef(RawText).contains(CanonicalDelimiterSuffix)) |
| 1867 |
NewDelimiter = OldDelimiter; |
| 1868 |
} |
| 1869 |
|
| 1870 |
unsigned NewPrefixSize = 3 + NewDelimiter.size(); |
| 1871 |
unsigned NewSuffixSize = 2 + NewDelimiter.size(); |
| 1872 |
|
| 1873 |
// The first start column is the column the raw text starts after formatting. |
| 1874 |
unsigned FirstStartColumn = StartColumn + NewPrefixSize; |
| 1875 |
|
| 1876 |
// The next start column is the intended indentation a line break inside |
| 1877 |
// the raw string at level 0. It is determined by the following rules: |
| 1878 |
// - if the content starts on newline, it is one level more than the current |
| 1879 |
// indent, and |
| 1880 |
// - if the content does not start on a newline, it is the first start |
| 1881 |
// column. |
| 1882 |
// These rules have the advantage that the formatted content both does not |
| 1883 |
// violate the rectangle rule and visually flows within the surrounding |
| 1884 |
// source. |
| 1885 |
bool ContentStartsOnNewline = Current.TokenText[OldPrefixSize] == '\n'; |
| 1886 |
// If this token is the last parameter (checked by looking if it's followed by |
| 1887 |
// `)` and is not on a newline, the base the indent off the line's nested |
| 1888 |
// block indent. Otherwise, base the indent off the arguments indent, so we |
| 1889 |
// can achieve: |
| 1890 |
// |
| 1891 |
// fffffffffff(1, 2, 3, R"pb( |
| 1892 |
// key1: 1 # |
| 1893 |
// key2: 2)pb"); |
| 1894 |
// |
| 1895 |
// fffffffffff(1, 2, 3, |
| 1896 |
// R"pb( |
| 1897 |
// key1: 1 # |
| 1898 |
// key2: 2 |
| 1899 |
// )pb"); |
| 1900 |
// |
| 1901 |
// fffffffffff(1, 2, 3, |
| 1902 |
// R"pb( |
| 1903 |
// key1: 1 # |
| 1904 |
// key2: 2 |
| 1905 |
// )pb", |
| 1906 |
// 5); |
| 1907 |
unsigned CurrentIndent = |
| 1908 |
(!Newline && Current.Next && Current.Next->is(tok::r_paren)) |
| 1909 |
? State.Stack.back().NestedBlockIndent |
| 1910 |
: State.Stack.back().Indent; |
| 1911 |
unsigned NextStartColumn = ContentStartsOnNewline |
| 1912 |
? CurrentIndent + Style.IndentWidth |
| 1913 |
: FirstStartColumn; |
| 1914 |
|
| 1915 |
// The last start column is the column the raw string suffix starts if it is |
| 1916 |
// put on a newline. |
| 1917 |
// The last start column is the intended indentation of the raw string postfix |
| 1918 |
// if it is put on a newline. It is determined by the following rules: |
| 1919 |
// - if the raw string prefix starts on a newline, it is the column where |
| 1920 |
// that raw string prefix starts, and |
| 1921 |
// - if the raw string prefix does not start on a newline, it is the current |
| 1922 |
// indent. |
| 1923 |
unsigned LastStartColumn = |
| 1924 |
Current.NewlinesBefore ? FirstStartColumn - NewPrefixSize : CurrentIndent; |
| 1925 |
|
| 1926 |
std::pair<tooling::Replacements, unsigned> Fixes = internal::reformat( |
| 1927 |
RawStringStyle, RawText, {tooling::Range(0, RawText.size())}, |
| 1928 |
FirstStartColumn, NextStartColumn, LastStartColumn, "<stdin>", |
| 1929 |
/*Status=*/nullptr); |
| 1930 |
|
| 1931 |
auto NewCode = applyAllReplacements(RawText, Fixes.first); |
| 1932 |
tooling::Replacements NoFixes; |
| 1933 |
if (!NewCode) |
| 1934 |
return addMultilineToken(Current, State); |
| 1935 |
if (!DryRun) { |
| 1936 |
if (NewDelimiter != OldDelimiter) { |
| 1937 |
// In 'R"delimiter(...', the delimiter starts 2 characters after the start |
| 1938 |
// of the token. |
| 1939 |
SourceLocation PrefixDelimiterStart = |
| 1940 |
Current.Tok.getLocation().getLocWithOffset(2); |
| 1941 |
auto PrefixErr = Whitespaces.addReplacement(tooling::Replacement( |
| 1942 |
SourceMgr, PrefixDelimiterStart, OldDelimiter.size(), NewDelimiter)); |
| 1943 |
if (PrefixErr) { |
| 1944 |
llvm::errs() |
| 1945 |
<< "Failed to update the prefix delimiter of a raw string: " |
| 1946 |
<< llvm::toString(std::move(PrefixErr)) << "\n"; |
| 1947 |
} |
| 1948 |
// In 'R"delimiter(...)delimiter"', the suffix delimiter starts at |
| 1949 |
// position length - 1 - |delimiter|. |
| 1950 |
SourceLocation SuffixDelimiterStart = |
| 1951 |
Current.Tok.getLocation().getLocWithOffset(Current.TokenText.size() - |
| 1952 |
1 - OldDelimiter.size()); |
| 1953 |
auto SuffixErr = Whitespaces.addReplacement(tooling::Replacement( |
| 1954 |
SourceMgr, SuffixDelimiterStart, OldDelimiter.size(), NewDelimiter)); |
| 1955 |
if (SuffixErr) { |
| 1956 |
llvm::errs() |
| 1957 |
<< "Failed to update the suffix delimiter of a raw string: " |
| 1958 |
<< llvm::toString(std::move(SuffixErr)) << "\n"; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
SourceLocation OriginLoc = |
| 1962 |
Current.Tok.getLocation().getLocWithOffset(OldPrefixSize); |
| 1963 |
for (const tooling::Replacement &Fix : Fixes.first) { |
| 1964 |
auto Err = Whitespaces.addReplacement(tooling::Replacement( |
| 1965 |
SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()), |
| 1966 |
Fix.getLength(), Fix.getReplacementText())); |
| 1967 |
if (Err) { |
| 1968 |
llvm::errs() << "Failed to reformat raw string: " |
| 1969 |
<< llvm::toString(std::move(Err)) << "\n"; |
| 1970 |
} |
| 1971 |
} |
| 1972 |
} |
| 1973 |
unsigned RawLastLineEndColumn = getLastLineEndColumn( |
| 1974 |
*NewCode, FirstStartColumn, Style.TabWidth, Encoding); |
| 1975 |
State.Column = RawLastLineEndColumn + NewSuffixSize; |
| 1976 |
// Since we're updating the column to after the raw string literal here, we |
| 1977 |
// have to manually add the penalty for the prefix R"delim( over the column |
| 1978 |
// limit. |
| 1979 |
unsigned PrefixExcessCharacters = |
| 1980 |
StartColumn + NewPrefixSize > Style.ColumnLimit |
| 1981 |
? StartColumn + NewPrefixSize - Style.ColumnLimit |
| 1982 |
: 0; |
| 1983 |
bool IsMultiline = |
| 1984 |
ContentStartsOnNewline || (NewCode->find('\n') != std::string::npos); |
| 1985 |
if (IsMultiline) { |
| 1986 |
// Break before further function parameters on all levels. |
| 1987 |
for (ParenState &Paren : State.Stack) |
| 1988 |
Paren.BreakBeforeParameter = true; |
| 1989 |
} |
| 1990 |
return Fixes.second + PrefixExcessCharacters * Style.PenaltyExcessCharacter; |
| 1991 |
} |
| 1992 |
|
| 1993 |
unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, |
| 1994 |
LineState &State) { |
| 1995 |
// Break before further function parameters on all levels. |
| 1996 |
for (ParenState &Paren : State.Stack) |
| 1997 |
Paren.BreakBeforeParameter = true; |
| 1998 |
|
| 1999 |
unsigned ColumnsUsed = State.Column; |
| 2000 |
// We can only affect layout of the first and the last line, so the penalty |
| 2001 |
// for all other lines is constant, and we ignore it. |
| 2002 |
State.Column = Current.LastLineColumnWidth; |
| 2003 |
|
| 2004 |
if (ColumnsUsed > getColumnLimit(State)) |
| 2005 |
return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State)); |
| 2006 |
return 0; |
| 2007 |
} |
| 2008 |
|
| 2009 |
unsigned ContinuationIndenter::handleEndOfLine(const FormatToken &Current, |
| 2010 |
LineState &State, bool DryRun, |
| 2011 |
bool AllowBreak, bool Newline) { |
| 2012 |
unsigned Penalty = 0; |
| 2013 |
// Compute the raw string style to use in case this is a raw string literal |
| 2014 |
// that can be reformatted. |
| 2015 |
auto RawStringStyle = getRawStringStyle(Current, State); |
| 2016 |
if (RawStringStyle && !Current.Finalized) { |
| 2017 |
Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun, |
| 2018 |
Newline); |
| 2019 |
} else if (Current.IsMultiline && Current.isNot(TT_BlockComment)) { |
| 2020 |
// Don't break multi-line tokens other than block comments and raw string |
| 2021 |
// literals. Instead, just update the state. |
| 2022 |
Penalty = addMultilineToken(Current, State); |
| 2023 |
} else if (State.Line->Type != LT_ImportStatement) { |
| 2024 |
// We generally don't break import statements. |
| 2025 |
LineState OriginalState = State; |
| 2026 |
|
| 2027 |
// Whether we force the reflowing algorithm to stay strictly within the |
| 2028 |
// column limit. |
| 2029 |
bool Strict = false; |
| 2030 |
// Whether the first non-strict attempt at reflowing did intentionally |
| 2031 |
// exceed the column limit. |
| 2032 |
bool Exceeded = false; |
| 2033 |
std::tie(Penalty, Exceeded) = breakProtrudingToken( |
| 2034 |
Current, State, AllowBreak, /*DryRun=*/true, Strict); |
| 2035 |
if (Exceeded) { |
| 2036 |
// If non-strict reflowing exceeds the column limit, try whether strict |
| 2037 |
// reflowing leads to an overall lower penalty. |
| 2038 |
LineState StrictState = OriginalState; |
| 2039 |
unsigned StrictPenalty = |
| 2040 |
breakProtrudingToken(Current, StrictState, AllowBreak, |
| 2041 |
/*DryRun=*/true, /*Strict=*/true) |
| 2042 |
.first; |
| 2043 |
Strict = StrictPenalty <= Penalty; |
| 2044 |
if (Strict) { |
| 2045 |
Penalty = StrictPenalty; |
| 2046 |
State = StrictState; |
| 2047 |
} |
| 2048 |
} |
| 2049 |
if (!DryRun) { |
| 2050 |
// If we're not in dry-run mode, apply the changes with the decision on |
| 2051 |
// strictness made above. |
| 2052 |
breakProtrudingToken(Current, OriginalState, AllowBreak, /*DryRun=*/false, |
| 2053 |
Strict); |
| 2054 |
} |
| 2055 |
} |
| 2056 |
if (State.Column > getColumnLimit(State)) { |
| 2057 |
unsigned ExcessCharacters = State.Column - getColumnLimit(State); |
| 2058 |
Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; |
| 2059 |
} |
| 2060 |
return Penalty; |
| 2061 |
} |
| 2062 |
|
| 2063 |
// Returns the enclosing function name of a token, or the empty string if not |
| 2064 |
// found. |
| 2065 |
static StringRef getEnclosingFunctionName(const FormatToken &Current) { |
| 2066 |
// Look for: 'function(' or 'function<templates>(' before Current. |
| 2067 |
auto Tok = Current.getPreviousNonComment(); |
| 2068 |
if (!Tok || !Tok->is(tok::l_paren)) |
| 2069 |
return ""; |
| 2070 |
Tok = Tok->getPreviousNonComment(); |
| 2071 |
if (!Tok) |
| 2072 |
return ""; |
| 2073 |
if (Tok->is(TT_TemplateCloser)) { |
| 2074 |
Tok = Tok->MatchingParen; |
| 2075 |
if (Tok) |
| 2076 |
Tok = Tok->getPreviousNonComment(); |
| 2077 |
} |
| 2078 |
if (!Tok || !Tok->is(tok::identifier)) |
| 2079 |
return ""; |
| 2080 |
return Tok->TokenText; |
| 2081 |
} |
| 2082 |
|
| 2083 |
std::optional<FormatStyle> |
| 2084 |
ContinuationIndenter::getRawStringStyle(const FormatToken &Current, |
| 2085 |
const LineState &State) { |
| 2086 |
if (!Current.isStringLiteral()) |
| 2087 |
return std::nullopt; |
| 2088 |
auto Delimiter = getRawStringDelimiter(Current.TokenText); |
| 2089 |
if (!Delimiter) |
| 2090 |
return std::nullopt; |
| 2091 |
auto RawStringStyle = RawStringFormats.getDelimiterStyle(*Delimiter); |
| 2092 |
if (!RawStringStyle && Delimiter->empty()) { |
| 2093 |
RawStringStyle = RawStringFormats.getEnclosingFunctionStyle( |
| 2094 |
getEnclosingFunctionName(Current)); |
| 2095 |
} |
| 2096 |
if (!RawStringStyle) |
| 2097 |
return std::nullopt; |
| 2098 |
RawStringStyle->ColumnLimit = getColumnLimit(State); |
| 2099 |
return RawStringStyle; |
| 2100 |
} |
| 2101 |
|
| 2102 |
std::unique_ptr<BreakableToken> |
| 2103 |
ContinuationIndenter::createBreakableToken(const FormatToken &Current, |
| 2104 |
LineState &State, bool AllowBreak) { |
| 2105 |
unsigned StartColumn = State.Column - Current.ColumnWidth; |
| 2106 |
if (Current.isStringLiteral()) { |
| 2107 |
// FIXME: String literal breaking is currently disabled for C#, Java, Json |
| 2108 |
// and JavaScript, as it requires strings to be merged using "+" which we |
| 2109 |
// don't support. |
| 2110 |
if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() || |
| 2111 |
Style.isCSharp() || Style.isJson() || !Style.BreakStringLiterals || |
| 2112 |
!AllowBreak) { |
| 2113 |
return nullptr; |
| 2114 |
} |
| 2115 |
|
| 2116 |
// Don't break string literals inside preprocessor directives (except for |
| 2117 |
// #define directives, as their contents are stored in separate lines and |
| 2118 |
// are not affected by this check). |
| 2119 |
// This way we avoid breaking code with line directives and unknown |
| 2120 |
// preprocessor directives that contain long string literals. |
| 2121 |
if (State.Line->Type == LT_PreprocessorDirective) |
| 2122 |
return nullptr; |
| 2123 |
// Exempts unterminated string literals from line breaking. The user will |
| 2124 |
// likely want to terminate the string before any line breaking is done. |
| 2125 |
if (Current.IsUnterminatedLiteral) |
| 2126 |
return nullptr; |
| 2127 |
// Don't break string literals inside Objective-C array literals (doing so |
| 2128 |
// raises the warning -Wobjc-string-concatenation). |
| 2129 |
if (State.Stack.back().IsInsideObjCArrayLiteral) |
| 2130 |
return nullptr; |
| 2131 |
|
| 2132 |
StringRef Text = Current.TokenText; |
| 2133 |
StringRef Prefix; |
| 2134 |
StringRef Postfix; |
| 2135 |
// FIXME: Handle whitespace between '_T', '(', '"..."', and ')'. |
| 2136 |
// FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to |
| 2137 |
// reduce the overhead) for each FormatToken, which is a string, so that we |
| 2138 |
// don't run multiple checks here on the hot path. |
| 2139 |
if ((Text.endswith(Postfix = "\"") && |
| 2140 |
(Text.startswith(Prefix = "@\"") || Text.startswith(Prefix = "\"") || |
| 2141 |
Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") || |
| 2142 |
Text.startswith(Prefix = "u8\"") || |
| 2143 |
Text.startswith(Prefix = "L\""))) || |
| 2144 |
(Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) { |
| 2145 |
// We need this to address the case where there is an unbreakable tail |
| 2146 |
// only if certain other formatting decisions have been taken. The |
| 2147 |
// UnbreakableTailLength of Current is an overapproximation is that case |
| 2148 |
// and we need to be correct here. |
| 2149 |
unsigned UnbreakableTailLength = (State.NextToken && canBreak(State)) |
| 2150 |
? 0 |
| 2151 |
: Current.UnbreakableTailLength; |
| 2152 |
return std::make_unique<BreakableStringLiteral>( |
| 2153 |
Current, StartColumn, Prefix, Postfix, UnbreakableTailLength, |
| 2154 |
State.Line->InPPDirective, Encoding, Style); |
| 2155 |
} |
| 2156 |
} else if (Current.is(TT_BlockComment)) { |
| 2157 |
if (!Style.ReflowComments || |
| 2158 |
// If a comment token switches formatting, like |
| 2159 |
// /* clang-format on */, we don't want to break it further, |
| 2160 |
// but we may still want to adjust its indentation. |
| 2161 |
switchesFormatting(Current)) { |
| 2162 |
return nullptr; |
| 2163 |
} |
| 2164 |
return std::make_unique<BreakableBlockComment>( |
| 2165 |
Current, StartColumn, Current.OriginalColumn, !Current.Previous, |
| 2166 |
State.Line->InPPDirective, Encoding, Style, Whitespaces.useCRLF()); |
| 2167 |
} else if (Current.is(TT_LineComment) && |
| 2168 |
(Current.Previous == nullptr || |
| 2169 |
Current.Previous->isNot(TT_ImplicitStringLiteral))) { |
| 2170 |
bool RegularComments = [&]() { |
| 2171 |
for (const FormatToken *T = &Current; T && T->is(TT_LineComment); |
| 2172 |
T = T->Next) { |
| 2173 |
if (!(T->TokenText.startswith("//") || T->TokenText.startswith("#"))) |
| 2174 |
return false; |
| 2175 |
} |
| 2176 |
return true; |
| 2177 |
}(); |
| 2178 |
if (!Style.ReflowComments || |
| 2179 |
CommentPragmasRegex.match(Current.TokenText.substr(2)) || |
| 2180 |
switchesFormatting(Current) || !RegularComments) { |
| 2181 |
return nullptr; |
| 2182 |
} |
| 2183 |
return std::make_unique<BreakableLineCommentSection>( |
| 2184 |
Current, StartColumn, /*InPPDirective=*/false, Encoding, Style); |
| 2185 |
} |
| 2186 |
return nullptr; |
| 2187 |
} |
| 2188 |
|
| 2189 |
std::pair<unsigned, bool> |
| 2190 |
ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, |
| 2191 |
LineState &State, bool AllowBreak, |
| 2192 |
bool DryRun, bool Strict) { |
| 2193 |
std::unique_ptr<const BreakableToken> Token = |
| 2194 |
createBreakableToken(Current, State, AllowBreak); |
| 2195 |
if (!Token) |
| 2196 |
return {0, false}; |
| 2197 |
assert(Token->getLineCount() > 0); |
| 2198 |
unsigned ColumnLimit = getColumnLimit(State); |
| 2199 |
if (Current.is(TT_LineComment)) { |
| 2200 |
// We don't insert backslashes when breaking line comments. |
| 2201 |
ColumnLimit = Style.ColumnLimit; |
| 2202 |
} |
| 2203 |
if (ColumnLimit == 0) { |
| 2204 |
// To make the rest of the function easier set the column limit to the |
| 2205 |
// maximum, if there should be no limit. |
| 2206 |
ColumnLimit = std::numeric_limits<decltype(ColumnLimit)>::max(); |
| 2207 |
} |
| 2208 |
if (Current.UnbreakableTailLength >= ColumnLimit) |
| 2209 |
return {0, false}; |
| 2210 |
// ColumnWidth was already accounted into State.Column before calling |
| 2211 |
// breakProtrudingToken. |
| 2212 |
unsigned StartColumn = State.Column - Current.ColumnWidth; |
| 2213 |
unsigned NewBreakPenalty = Current.isStringLiteral() |
| 2214 |
? Style.PenaltyBreakString |
| 2215 |
: Style.PenaltyBreakComment; |
| 2216 |
// Stores whether we intentionally decide to let a line exceed the column |
| 2217 |
// limit. |
| 2218 |
bool Exceeded = false; |
| 2219 |
// Stores whether we introduce a break anywhere in the token. |
| 2220 |
bool BreakInserted = Token->introducesBreakBeforeToken(); |
| 2221 |
// Store whether we inserted a new line break at the end of the previous |
| 2222 |
// logical line. |
| 2223 |
bool NewBreakBefore = false; |
| 2224 |
// We use a conservative reflowing strategy. Reflow starts after a line is |
| 2225 |
// broken or the corresponding whitespace compressed. Reflow ends as soon as a |
| 2226 |
// line that doesn't get reflown with the previous line is reached. |
| 2227 |
bool Reflow = false; |
| 2228 |
// Keep track of where we are in the token: |
| 2229 |
// Where we are in the content of the current logical line. |
| 2230 |
unsigned TailOffset = 0; |
| 2231 |
// The column number we're currently at. |
| 2232 |
unsigned ContentStartColumn = |
| 2233 |
Token->getContentStartColumn(0, /*Break=*/false); |
| 2234 |
// The number of columns left in the current logical line after TailOffset. |
| 2235 |
unsigned RemainingTokenColumns = |
| 2236 |
Token->getRemainingLength(0, TailOffset, ContentStartColumn); |
| 2237 |
// Adapt the start of the token, for example indent. |
| 2238 |
if (!DryRun) |
| 2239 |
Token->adaptStartOfLine(0, Whitespaces); |
| 2240 |
|
| 2241 |
unsigned ContentIndent = 0; |
| 2242 |
unsigned Penalty = 0; |
| 2243 |
LLVM_DEBUG(llvm::dbgs() << "Breaking protruding token at column " |
| 2244 |
<< StartColumn << ".\n"); |
| 2245 |
for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); |
| 2246 |
LineIndex != EndIndex; ++LineIndex) { |
| 2247 |
LLVM_DEBUG(llvm::dbgs() |
| 2248 |
<< " Line: " << LineIndex << " (Reflow: " << Reflow << ")\n"); |
| 2249 |
NewBreakBefore = false; |
| 2250 |
// If we did reflow the previous line, we'll try reflowing again. Otherwise |
| 2251 |
// we'll start reflowing if the current line is broken or whitespace is |
| 2252 |
// compressed. |
| 2253 |
bool TryReflow = Reflow; |
| 2254 |
// Break the current token until we can fit the rest of the line. |
| 2255 |
while (ContentStartColumn + RemainingTokenColumns > ColumnLimit) { |
| 2256 |
LLVM_DEBUG(llvm::dbgs() << " Over limit, need: " |
| 2257 |
<< (ContentStartColumn + RemainingTokenColumns) |
| 2258 |
<< ", space: " << ColumnLimit |
| 2259 |
<< ", reflown prefix: " << ContentStartColumn |
| 2260 |
<< ", offset in line: " << TailOffset << "\n"); |
| 2261 |
// If the current token doesn't fit, find the latest possible split in the |
| 2262 |
// current line so that breaking at it will be under the column limit. |
| 2263 |
// FIXME: Use the earliest possible split while reflowing to correctly |
| 2264 |
// compress whitespace within a line. |
| 2265 |
BreakableToken::Split Split = |
| 2266 |
Token->getSplit(LineIndex, TailOffset, ColumnLimit, |
| 2267 |
ContentStartColumn, CommentPragmasRegex); |
| 2268 |
if (Split.first == StringRef::npos) { |
| 2269 |
// No break opportunity - update the penalty and continue with the next |
| 2270 |
// logical line. |
| 2271 |
if (LineIndex < EndIndex - 1) { |
| 2272 |
// The last line's penalty is handled in addNextStateToQueue() or when |
| 2273 |
// calling replaceWhitespaceAfterLastLine below. |
| 2274 |
Penalty += Style.PenaltyExcessCharacter * |
| 2275 |
(ContentStartColumn + RemainingTokenColumns - ColumnLimit); |
| 2276 |
} |
| 2277 |
LLVM_DEBUG(llvm::dbgs() << " No break opportunity.\n"); |
| 2278 |
break; |
| 2279 |
} |
| 2280 |
assert(Split.first != 0); |
| 2281 |
|
| 2282 |
if (Token->supportsReflow()) { |
| 2283 |
// Check whether the next natural split point after the current one can |
| 2284 |
// still fit the line, either because we can compress away whitespace, |
| 2285 |
// or because the penalty the excess characters introduce is lower than |
| 2286 |
// the break penalty. |
| 2287 |
// We only do this for tokens that support reflowing, and thus allow us |
| 2288 |
// to change the whitespace arbitrarily (e.g. comments). |
| 2289 |
// Other tokens, like string literals, can be broken on arbitrary |
| 2290 |
// positions. |
| 2291 |
|
| 2292 |
// First, compute the columns from TailOffset to the next possible split |
| 2293 |
// position. |
| 2294 |
// For example: |
| 2295 |
// ColumnLimit: | |
| 2296 |
// // Some text that breaks |
| 2297 |
// ^ tail offset |
| 2298 |
// ^-- split |
| 2299 |
// ^-------- to split columns |
| 2300 |
// ^--- next split |
| 2301 |
// ^--------------- to next split columns |
| 2302 |
unsigned ToSplitColumns = Token->getRangeLength( |
| 2303 |
LineIndex, TailOffset, Split.first, ContentStartColumn); |
| 2304 |
LLVM_DEBUG(llvm::dbgs() << " ToSplit: " << ToSplitColumns << "\n"); |
| 2305 |
|
| 2306 |
BreakableToken::Split NextSplit = Token->getSplit( |
| 2307 |
LineIndex, TailOffset + Split.first + Split.second, ColumnLimit, |
| 2308 |
ContentStartColumn + ToSplitColumns + 1, CommentPragmasRegex); |
| 2309 |
// Compute the columns necessary to fit the next non-breakable sequence |
| 2310 |
// into the current line. |
| 2311 |
unsigned ToNextSplitColumns = 0; |
| 2312 |
if (NextSplit.first == StringRef::npos) { |
| 2313 |
ToNextSplitColumns = Token->getRemainingLength(LineIndex, TailOffset, |
| 2314 |
ContentStartColumn); |
| 2315 |
} else { |
| 2316 |
ToNextSplitColumns = Token->getRangeLength( |
| 2317 |
LineIndex, TailOffset, |
| 2318 |
Split.first + Split.second + NextSplit.first, ContentStartColumn); |
| 2319 |
} |
| 2320 |
// Compress the whitespace between the break and the start of the next |
| 2321 |
// unbreakable sequence. |
| 2322 |
ToNextSplitColumns = |
| 2323 |
Token->getLengthAfterCompression(ToNextSplitColumns, Split); |
| 2324 |
LLVM_DEBUG(llvm::dbgs() |
| 2325 |
<< " ContentStartColumn: " << ContentStartColumn << "\n"); |
| 2326 |
LLVM_DEBUG(llvm::dbgs() |
| 2327 |
<< " ToNextSplit: " << ToNextSplitColumns << "\n"); |
| 2328 |
// If the whitespace compression makes us fit, continue on the current |
| 2329 |
// line. |
| 2330 |
bool ContinueOnLine = |
| 2331 |
ContentStartColumn + ToNextSplitColumns <= ColumnLimit; |
| 2332 |
unsigned ExcessCharactersPenalty = 0; |
| 2333 |
if (!ContinueOnLine && !Strict) { |
| 2334 |
// Similarly, if the excess characters' penalty is lower than the |
| 2335 |
// penalty of introducing a new break, continue on the current line. |
| 2336 |
ExcessCharactersPenalty = |
| 2337 |
(ContentStartColumn + ToNextSplitColumns - ColumnLimit) * |
| 2338 |
Style.PenaltyExcessCharacter; |
| 2339 |
LLVM_DEBUG(llvm::dbgs() |
| 2340 |
<< " Penalty excess: " << ExcessCharactersPenalty |
| 2341 |
<< "\n break : " << NewBreakPenalty << "\n"); |
| 2342 |
if (ExcessCharactersPenalty < NewBreakPenalty) { |
| 2343 |
Exceeded = true; |
| 2344 |
ContinueOnLine = true; |
| 2345 |
} |
| 2346 |
} |
| 2347 |
if (ContinueOnLine) { |
| 2348 |
LLVM_DEBUG(llvm::dbgs() << " Continuing on line...\n"); |
| 2349 |
// The current line fits after compressing the whitespace - reflow |
| 2350 |
// the next line into it if possible. |
| 2351 |
TryReflow = true; |
| 2352 |
if (!DryRun) { |
| 2353 |
Token->compressWhitespace(LineIndex, TailOffset, Split, |
| 2354 |
Whitespaces); |
| 2355 |
} |
| 2356 |
// When we continue on the same line, leave one space between content. |
| 2357 |
ContentStartColumn += ToSplitColumns + 1; |
| 2358 |
Penalty += ExcessCharactersPenalty; |
| 2359 |
TailOffset += Split.first + Split.second; |
| 2360 |
RemainingTokenColumns = Token->getRemainingLength( |
| 2361 |
LineIndex, TailOffset, ContentStartColumn); |
| 2362 |
continue; |
| 2363 |
} |
| 2364 |
} |
| 2365 |
LLVM_DEBUG(llvm::dbgs() << " Breaking...\n"); |
| 2366 |
// Update the ContentIndent only if the current line was not reflown with |
| 2367 |
// the previous line, since in that case the previous line should still |
| 2368 |
// determine the ContentIndent. Also never intent the last line. |
| 2369 |
if (!Reflow) |
| 2370 |
ContentIndent = Token->getContentIndent(LineIndex); |
| 2371 |
LLVM_DEBUG(llvm::dbgs() |
| 2372 |
<< " ContentIndent: " << ContentIndent << "\n"); |
| 2373 |
ContentStartColumn = ContentIndent + Token->getContentStartColumn( |
| 2374 |
LineIndex, /*Break=*/true); |
| 2375 |
|
| 2376 |
unsigned NewRemainingTokenColumns = Token->getRemainingLength( |
| 2377 |
LineIndex, TailOffset + Split.first + Split.second, |
| 2378 |
ContentStartColumn); |
| 2379 |
if (NewRemainingTokenColumns == 0) { |
| 2380 |
// No content to indent. |
| 2381 |
ContentIndent = 0; |
| 2382 |
ContentStartColumn = |
| 2383 |
Token->getContentStartColumn(LineIndex, /*Break=*/true); |
| 2384 |
NewRemainingTokenColumns = Token->getRemainingLength( |
| 2385 |
LineIndex, TailOffset + Split.first + Split.second, |
| 2386 |
ContentStartColumn); |
| 2387 |
} |
| 2388 |
|
| 2389 |
// When breaking before a tab character, it may be moved by a few columns, |
| 2390 |
// but will still be expanded to the next tab stop, so we don't save any |
| 2391 |
// columns. |
| 2392 |
if (NewRemainingTokenColumns >= RemainingTokenColumns) { |
| 2393 |
// FIXME: Do we need to adjust the penalty? |
| 2394 |
break; |
| 2395 |
} |
| 2396 |
|
| 2397 |
LLVM_DEBUG(llvm::dbgs() << " Breaking at: " << TailOffset + Split.first |
| 2398 |
<< ", " << Split.second << "\n"); |
| 2399 |
if (!DryRun) { |
| 2400 |
Token->insertBreak(LineIndex, TailOffset, Split, ContentIndent, |
| 2401 |
Whitespaces); |
| 2402 |
} |
| 2403 |
|
| 2404 |
Penalty += NewBreakPenalty; |
| 2405 |
TailOffset += Split.first + Split.second; |
| 2406 |
RemainingTokenColumns = NewRemainingTokenColumns; |
| 2407 |
BreakInserted = true; |
| 2408 |
NewBreakBefore = true; |
| 2409 |
} |
| 2410 |
// In case there's another line, prepare the state for the start of the next |
| 2411 |
// line. |
| 2412 |
if (LineIndex + 1 != EndIndex) { |
| 2413 |
unsigned NextLineIndex = LineIndex + 1; |
| 2414 |
if (NewBreakBefore) { |
| 2415 |
// After breaking a line, try to reflow the next line into the current |
| 2416 |
// one once RemainingTokenColumns fits. |
| 2417 |
TryReflow = true; |
| 2418 |
} |
| 2419 |
if (TryReflow) { |
| 2420 |
// We decided that we want to try reflowing the next line into the |
| 2421 |
// current one. |
| 2422 |
// We will now adjust the state as if the reflow is successful (in |
| 2423 |
// preparation for the next line), and see whether that works. If we |
| 2424 |
// decide that we cannot reflow, we will later reset the state to the |
| 2425 |
// start of the next line. |
| 2426 |
Reflow = false; |
| 2427 |
// As we did not continue breaking the line, RemainingTokenColumns is |
| 2428 |
// known to fit after ContentStartColumn. Adapt ContentStartColumn to |
| 2429 |
// the position at which we want to format the next line if we do |
| 2430 |
// actually reflow. |
| 2431 |
// When we reflow, we need to add a space between the end of the current |
| 2432 |
// line and the next line's start column. |
| 2433 |
ContentStartColumn += RemainingTokenColumns + 1; |
| 2434 |
// Get the split that we need to reflow next logical line into the end |
| 2435 |
// of the current one; the split will include any leading whitespace of |
| 2436 |
// the next logical line. |
| 2437 |
BreakableToken::Split SplitBeforeNext = |
| 2438 |
Token->getReflowSplit(NextLineIndex, CommentPragmasRegex); |
| 2439 |
LLVM_DEBUG(llvm::dbgs() |
| 2440 |
<< " Size of reflown text: " << ContentStartColumn |
| 2441 |
<< "\n Potential reflow split: "); |
| 2442 |
if (SplitBeforeNext.first != StringRef::npos) { |
| 2443 |
LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext.first << ", " |
| 2444 |
<< SplitBeforeNext.second << "\n"); |
| 2445 |
TailOffset = SplitBeforeNext.first + SplitBeforeNext.second; |
| 2446 |
// If the rest of the next line fits into the current line below the |
| 2447 |
// column limit, we can safely reflow. |
| 2448 |
RemainingTokenColumns = Token->getRemainingLength( |
| 2449 |
NextLineIndex, TailOffset, ContentStartColumn); |
| 2450 |
Reflow = true; |
| 2451 |
if (ContentStartColumn + RemainingTokenColumns > ColumnLimit) { |
| 2452 |
LLVM_DEBUG(llvm::dbgs() |
| 2453 |
<< " Over limit after reflow, need: " |
| 2454 |
<< (ContentStartColumn + RemainingTokenColumns) |
| 2455 |
<< ", space: " << ColumnLimit |
| 2456 |
<< ", reflown prefix: " << ContentStartColumn |
| 2457 |
<< ", offset in line: " << TailOffset << "\n"); |
| 2458 |
// If the whole next line does not fit, try to find a point in |
| 2459 |
// the next line at which we can break so that attaching the part |
| 2460 |
// of the next line to that break point onto the current line is |
| 2461 |
// below the column limit. |
| 2462 |
BreakableToken::Split Split = |
| 2463 |
Token->getSplit(NextLineIndex, TailOffset, ColumnLimit, |
| 2464 |
ContentStartColumn, CommentPragmasRegex); |
| 2465 |
if (Split.first == StringRef::npos) { |
| 2466 |
LLVM_DEBUG(llvm::dbgs() << " Did not find later break\n"); |
| 2467 |
Reflow = false; |
| 2468 |
} else { |
| 2469 |
// Check whether the first split point gets us below the column |
| 2470 |
// limit. Note that we will execute this split below as part of |
| 2471 |
// the normal token breaking and reflow logic within the line. |
| 2472 |
unsigned ToSplitColumns = Token->getRangeLength( |
| 2473 |
NextLineIndex, TailOffset, Split.first, ContentStartColumn); |
| 2474 |
if (ContentStartColumn + ToSplitColumns > ColumnLimit) { |
| 2475 |
LLVM_DEBUG(llvm::dbgs() << " Next split protrudes, need: " |
| 2476 |
<< (ContentStartColumn + ToSplitColumns) |
| 2477 |
<< ", space: " << ColumnLimit); |
| 2478 |
unsigned ExcessCharactersPenalty = |
| 2479 |
(ContentStartColumn + ToSplitColumns - ColumnLimit) * |
| 2480 |
Style.PenaltyExcessCharacter; |
| 2481 |
if (NewBreakPenalty < ExcessCharactersPenalty) |
| 2482 |
Reflow = false; |
| 2483 |
} |
| 2484 |
} |
| 2485 |
} |
| 2486 |
} else { |
| 2487 |
LLVM_DEBUG(llvm::dbgs() << "not found.\n"); |
| 2488 |
} |
| 2489 |
} |
| 2490 |
if (!Reflow) { |
| 2491 |
// If we didn't reflow into the next line, the only space to consider is |
| 2492 |
// the next logical line. Reset our state to match the start of the next |
| 2493 |
// line. |
| 2494 |
TailOffset = 0; |
| 2495 |
ContentStartColumn = |
| 2496 |
Token->getContentStartColumn(NextLineIndex, /*Break=*/false); |
| 2497 |
RemainingTokenColumns = Token->getRemainingLength( |
| 2498 |
NextLineIndex, TailOffset, ContentStartColumn); |
| 2499 |
// Adapt the start of the token, for example indent. |
| 2500 |
if (!DryRun) |
| 2501 |
Token->adaptStartOfLine(NextLineIndex, Whitespaces); |
| 2502 |
} else { |
| 2503 |
// If we found a reflow split and have added a new break before the next |
| 2504 |
// line, we are going to remove the line break at the start of the next |
| 2505 |
// logical line. For example, here we'll add a new line break after |
| 2506 |
// 'text', and subsequently delete the line break between 'that' and |
| 2507 |
// 'reflows'. |
| 2508 |
// // some text that |
| 2509 |
// // reflows |
| 2510 |
// -> |
| 2511 |
// // some text |
| 2512 |
// // that reflows |
| 2513 |
// When adding the line break, we also added the penalty for it, so we |
| 2514 |
// need to subtract that penalty again when we remove the line break due |
| 2515 |
// to reflowing. |
| 2516 |
if (NewBreakBefore) { |
| 2517 |
assert(Penalty >= NewBreakPenalty); |
| 2518 |
Penalty -= NewBreakPenalty; |
| 2519 |
} |
| 2520 |
if (!DryRun) |
| 2521 |
Token->reflow(NextLineIndex, Whitespaces); |
| 2522 |
} |
| 2523 |
} |
| 2524 |
} |
| 2525 |
|
| 2526 |
BreakableToken::Split SplitAfterLastLine = |
| 2527 |
Token->getSplitAfterLastLine(TailOffset); |
| 2528 |
if (SplitAfterLastLine.first != StringRef::npos) { |
| 2529 |
LLVM_DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n"); |
| 2530 |
|
| 2531 |
// We add the last line's penalty here, since that line is going to be split |
| 2532 |
// now. |
| 2533 |
Penalty += Style.PenaltyExcessCharacter * |
| 2534 |
(ContentStartColumn + RemainingTokenColumns - ColumnLimit); |
| 2535 |
|
| 2536 |
if (!DryRun) { |
| 2537 |
Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine, |
| 2538 |
Whitespaces); |
| 2539 |
} |
| 2540 |
ContentStartColumn = |
| 2541 |
Token->getContentStartColumn(Token->getLineCount() - 1, /*Break=*/true); |
| 2542 |
RemainingTokenColumns = Token->getRemainingLength( |
| 2543 |
Token->getLineCount() - 1, |
| 2544 |
TailOffset + SplitAfterLastLine.first + SplitAfterLastLine.second, |
| 2545 |
ContentStartColumn); |
| 2546 |
} |
| 2547 |
|
| 2548 |
State.Column = ContentStartColumn + RemainingTokenColumns - |
| 2549 |
Current.UnbreakableTailLength; |
| 2550 |
|
| 2551 |
if (BreakInserted) { |
| 2552 |
// If we break the token inside a parameter list, we need to break before |
| 2553 |
// the next parameter on all levels, so that the next parameter is clearly |
| 2554 |
// visible. Line comments already introduce a break. |
| 2555 |
if (Current.isNot(TT_LineComment)) |
| 2556 |
for (ParenState &Paren : State.Stack) |
| 2557 |
Paren.BreakBeforeParameter = true; |
| 2558 |
|
| 2559 |
if (Current.is(TT_BlockComment)) |
| 2560 |
State.NoContinuation = true; |
| 2561 |
|
| 2562 |
State.Stack.back().LastSpace = StartColumn; |
| 2563 |
} |
| 2564 |
|
| 2565 |
Token->updateNextToken(State); |
| 2566 |
|
| 2567 |
return {Penalty, Exceeded}; |
| 2568 |
} |
| 2569 |
|
| 2570 |
unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const { |
| 2571 |
// In preprocessor directives reserve two chars for trailing " \". |
| 2572 |
return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0); |
| 2573 |
} |
| 2574 |
|
| 2575 |
bool ContinuationIndenter::nextIsMultilineString(const LineState &State) { |
| 2576 |
const FormatToken &Current = *State.NextToken; |
| 2577 |
if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral)) |
| 2578 |
return false; |
| 2579 |
// We never consider raw string literals "multiline" for the purpose of |
| 2580 |
// AlwaysBreakBeforeMultilineStrings implementation as they are special-cased |
| 2581 |
// (see TokenAnnotator::mustBreakBefore(). |
| 2582 |
if (Current.TokenText.startswith("R\"")) |
| 2583 |
return false; |
| 2584 |
if (Current.IsMultiline) |
| 2585 |
return true; |
| 2586 |
if (Current.getNextNonComment() && |
| 2587 |
Current.getNextNonComment()->isStringLiteral()) { |
| 2588 |
return true; // Implicit concatenation. |
| 2589 |
} |
| 2590 |
if (Style.ColumnLimit != 0 && Style.BreakStringLiterals && |
| 2591 |
State.Column + Current.ColumnWidth + Current.UnbreakableTailLength > |
| 2592 |
Style.ColumnLimit) { |
| 2593 |
return true; // String will be split. |
| 2594 |
} |
| 2595 |
return false; |
| 2596 |
} |
| 2597 |
|
| 2598 |
} // namespace format |
| 2599 |
} // namespace clang |