2013-08-16 19:20:30 +08:00
|
|
|
//===--- ContinuationIndenter.cpp - Format C++ code -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file implements the continuation indenter.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BreakableToken.h"
|
|
|
|
#include "ContinuationIndenter.h"
|
|
|
|
#include "WhitespaceManager.h"
|
|
|
|
#include "clang/Basic/OperatorPrecedence.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include <string>
|
|
|
|
|
2014-04-22 11:17:02 +08:00
|
|
|
#define DEBUG_TYPE "format-formatter"
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
// Returns the length of everything up to the first possible line break after
|
|
|
|
// the ), ], } or > matching \c Tok.
|
|
|
|
static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!Tok.MatchingParen)
|
2013-08-16 19:20:30 +08:00
|
|
|
return 0;
|
|
|
|
FormatToken *End = Tok.MatchingParen;
|
|
|
|
while (End->Next && !End->Next->CanBreakBefore) {
|
|
|
|
End = End->Next;
|
|
|
|
}
|
|
|
|
return End->TotalLength - Tok.TotalLength + 1;
|
|
|
|
}
|
|
|
|
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
// Returns \c true if \c Tok is the "." or "->" of a call and starts the next
|
|
|
|
// segment of a builder type call.
|
|
|
|
static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
|
|
|
|
return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:11:18 +08:00
|
|
|
// Returns \c true if \c Current starts a new parameter.
|
|
|
|
static bool startsNextParameter(const FormatToken &Current,
|
|
|
|
const FormatStyle &Style) {
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_CtorInitializerComma) &&
|
2013-10-08 13:11:18 +08:00
|
|
|
Style.BreakConstructorInitializersBeforeComma)
|
|
|
|
return true;
|
|
|
|
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(Previous.isNot(TT_CtorInitializerComma) ||
|
2013-10-08 13:11:18 +08:00
|
|
|
!Style.BreakConstructorInitializersBeforeComma);
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords,
|
2013-08-16 19:20:30 +08:00
|
|
|
SourceManager &SourceMgr,
|
|
|
|
WhitespaceManager &Whitespaces,
|
|
|
|
encoding::Encoding Encoding,
|
|
|
|
bool BinPackInconclusiveFunctions)
|
2014-11-04 20:41:02 +08:00
|
|
|
: Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
|
|
|
|
Whitespaces(Whitespaces), Encoding(Encoding),
|
2014-01-02 23:13:14 +08:00
|
|
|
BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
|
|
|
|
CommentPragmasRegex(Style.CommentPragmas) {}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
|
2013-09-06 15:54:20 +08:00
|
|
|
const AnnotatedLine *Line,
|
|
|
|
bool DryRun) {
|
2013-08-16 19:20:30 +08:00
|
|
|
LineState State;
|
2013-09-05 17:29:45 +08:00
|
|
|
State.FirstIndent = FirstIndent;
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Column = FirstIndent;
|
2013-09-05 17:29:45 +08:00
|
|
|
State.Line = Line;
|
|
|
|
State.NextToken = Line->First;
|
2013-10-14 08:46:35 +08:00
|
|
|
State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent,
|
2013-08-16 19:20:30 +08:00
|
|
|
/*AvoidBinPacking=*/false,
|
|
|
|
/*NoLineBreak=*/false));
|
|
|
|
State.LineContainsContinuedForLoopSection = false;
|
|
|
|
State.StartOfStringLiteral = 0;
|
2014-05-08 20:21:30 +08:00
|
|
|
State.StartOfLineLevel = 0;
|
|
|
|
State.LowestLevelOnLine = 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
State.IgnoreStackForComparison = false;
|
|
|
|
|
|
|
|
// The first token has already been indented and thus consumed.
|
2013-09-06 15:54:20 +08:00
|
|
|
moveStateToNextToken(State, DryRun, /*Newline=*/false);
|
2013-08-16 19:20:30 +08:00
|
|
|
return State;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContinuationIndenter::canBreak(const LineState &State) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
|
|
|
assert(&Previous == Current.Previous);
|
2015-01-10 07:25:06 +08:00
|
|
|
if (!Current.CanBreakBefore &&
|
|
|
|
!(State.Stack.back().BreakBeforeClosingBrace &&
|
|
|
|
Current.closesBlockTypeList(Style)))
|
2013-08-16 19:20:30 +08:00
|
|
|
return false;
|
|
|
|
// The opening "{" of a braced list has to be on the same line as the first
|
|
|
|
// element if it is nested in another braced init list or function call.
|
|
|
|
if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.isNot(TT_DictLiteral) && Previous.BlockKind == BK_BracedInit &&
|
2014-05-09 21:11:16 +08:00
|
|
|
Previous.Previous &&
|
2013-08-16 19:20:30 +08:00
|
|
|
Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
|
|
|
|
return false;
|
|
|
|
// This prevents breaks like:
|
|
|
|
// ...
|
|
|
|
// SomeParameter, OtherParameter).DoSomething(
|
|
|
|
// ...
|
|
|
|
// As they hide "DoSomething" and are generally bad for readability.
|
2014-03-11 19:03:26 +08:00
|
|
|
if (Previous.opensScope() && Previous.isNot(tok::l_brace) &&
|
2014-07-15 17:00:34 +08:00
|
|
|
State.LowestLevelOnLine < State.StartOfLineLevel &&
|
|
|
|
State.LowestLevelOnLine < Current.NestingLevel)
|
2013-08-16 19:20:30 +08:00
|
|
|
return false;
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
|
|
|
|
return false;
|
2014-06-03 20:02:45 +08:00
|
|
|
|
|
|
|
// Don't create a 'hanging' indent if there are multiple blocks in a single
|
|
|
|
// statement.
|
2014-11-21 21:38:53 +08:00
|
|
|
if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
|
|
|
|
State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
|
2014-06-03 20:02:45 +08:00
|
|
|
State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks)
|
|
|
|
return false;
|
|
|
|
|
2014-10-28 01:13:59 +08:00
|
|
|
// Don't break after very short return types (e.g. "void") as that is often
|
|
|
|
// unexpected.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_FunctionDeclarationName) &&
|
2015-06-29 23:30:42 +08:00
|
|
|
Style.AlwaysBreakAfterDefinitionReturnType == FormatStyle::DRTBS_None &&
|
|
|
|
State.Column < 6)
|
2014-10-28 01:13:59 +08:00
|
|
|
return false;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
return !State.Stack.back().NoLineBreak;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ContinuationIndenter::mustBreak(const LineState &State) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
|
2013-08-16 19:20:30 +08:00
|
|
|
return true;
|
2013-10-22 23:30:28 +08:00
|
|
|
if (State.Stack.back().BreakBeforeClosingBrace &&
|
|
|
|
Current.closesBlockTypeList(Style))
|
2013-08-16 19:20:30 +08:00
|
|
|
return true;
|
|
|
|
if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
|
|
|
|
return true;
|
2013-10-08 13:11:18 +08:00
|
|
|
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
|
2015-05-27 13:37:40 +08:00
|
|
|
(Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
|
|
|
|
Previous.isNot(tok::question)) ||
|
2013-11-08 08:57:11 +08:00
|
|
|
(!Style.BreakBeforeTernaryOperators &&
|
2015-05-27 13:37:40 +08:00
|
|
|
Previous.is(TT_ConditionalExpr))) &&
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
|
|
|
|
!Current.isOneOf(tok::r_paren, tok::r_brace))
|
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) ||
|
|
|
|
Previous.is(TT_ArrayInitializerLSquare)) &&
|
2014-04-14 20:11:07 +08:00
|
|
|
Style.ColumnLimit > 0 &&
|
2015-06-02 23:14:21 +08:00
|
|
|
getLengthToMatchingParen(Previous) + State.Column - 1 >
|
|
|
|
getColumnLimit(State))
|
2013-10-21 00:45:46 +08:00
|
|
|
return true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_CtorInitializerColon) &&
|
2015-08-27 19:59:31 +08:00
|
|
|
(State.Column + State.Line->Last->TotalLength - Current.TotalLength + 2 >
|
|
|
|
getColumnLimit(State) ||
|
|
|
|
State.Stack.back().BreakBeforeParameter) &&
|
2014-04-08 20:46:38 +08:00
|
|
|
((Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All) ||
|
2014-03-28 00:14:13 +08:00
|
|
|
Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
|
|
|
|
return true;
|
2015-05-06 21:13:03 +08:00
|
|
|
if (Current.is(TT_SelectorName) && State.Stack.back().ObjCSelectorNameFound &&
|
|
|
|
State.Stack.back().BreakBeforeParameter)
|
|
|
|
return true;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2015-06-18 17:12:47 +08:00
|
|
|
unsigned NewLineColumn = getNewLineColumn(State);
|
|
|
|
if (State.Column < NewLineColumn)
|
2014-03-28 00:14:13 +08:00
|
|
|
return false;
|
2015-05-11 05:15:07 +08:00
|
|
|
|
2015-06-18 17:12:47 +08:00
|
|
|
if (Style.AlwaysBreakBeforeMultilineStrings &&
|
2015-06-19 00:05:17 +08:00
|
|
|
(NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth ||
|
2015-06-19 18:32:28 +08:00
|
|
|
Previous.is(tok::comma) || Current.NestingLevel < 2) &&
|
2015-06-18 17:12:47 +08:00
|
|
|
!Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) &&
|
|
|
|
!Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
|
|
|
|
nextIsMultilineString(State))
|
|
|
|
return true;
|
|
|
|
|
2015-05-11 05:15:07 +08:00
|
|
|
// Using CanBreakBefore here and below takes care of the decision whether the
|
|
|
|
// current style uses wrapping before or after operators for the given
|
|
|
|
// operator.
|
|
|
|
if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) {
|
2013-08-16 19:20:30 +08:00
|
|
|
// If we need to break somewhere inside the LHS of a binary expression, we
|
|
|
|
// should also break after the operator. Otherwise, the formatting would
|
|
|
|
// hide the operator precedence, e.g. in:
|
|
|
|
// if (aaaaaaaaaaaaaa ==
|
|
|
|
// bbbbbbbbbbbbbb && c) {..
|
|
|
|
// For comparisons, we only apply this rule, if the LHS is a binary
|
|
|
|
// expression itself as otherwise, the line breaks seem superfluous.
|
|
|
|
// We need special cases for ">>" which we have split into two ">" while
|
|
|
|
// lexing in order to make template parsing easier.
|
|
|
|
bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
|
|
|
|
Previous.getPrecedence() == prec::Equality) &&
|
|
|
|
Previous.Previous &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.Previous->isNot(TT_BinaryOperator); // For >>.
|
2013-08-16 19:20:30 +08:00
|
|
|
bool LHSIsBinaryExpr =
|
2013-09-06 16:08:14 +08:00
|
|
|
Previous.Previous && Previous.Previous->EndsBinaryExpression;
|
2015-05-11 05:15:07 +08:00
|
|
|
if ((!IsComparison || LHSIsBinaryExpr) && !Current.isTrailingComment() &&
|
2013-08-16 19:20:30 +08:00
|
|
|
Previous.getPrecedence() != prec::Assignment &&
|
|
|
|
State.Stack.back().BreakBeforeParameter)
|
|
|
|
return true;
|
2015-05-11 05:15:07 +08:00
|
|
|
} else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore &&
|
|
|
|
State.Stack.back().BreakBeforeParameter) {
|
|
|
|
return true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same as above, but for the first "<<" operator.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) &&
|
2014-03-06 23:13:08 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter &&
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().FirstLessLess == 0)
|
|
|
|
return true;
|
|
|
|
|
2014-12-09 04:08:04 +08:00
|
|
|
if (Current.NestingLevel == 0 && !Current.isTrailingComment()) {
|
2015-05-18 17:47:22 +08:00
|
|
|
// Always break after "template <...>" and leading annotations. This is only
|
|
|
|
// for cases where the entire line does not fit on a single line as a
|
|
|
|
// different LineFormatter would be used otherwise.
|
2014-12-09 04:08:04 +08:00
|
|
|
if (Previous.ClosesTemplateDeclaration)
|
|
|
|
return true;
|
2015-05-18 21:47:23 +08:00
|
|
|
if (Previous.is(TT_FunctionAnnotationRParen))
|
2015-05-18 17:47:22 +08:00
|
|
|
return true;
|
2015-01-10 07:25:06 +08:00
|
|
|
if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
|
|
|
|
Current.isNot(TT_LeadingJavaAnnotation))
|
2014-12-09 04:08:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2014-07-09 15:50:33 +08:00
|
|
|
// If the return type spans multiple lines, wrap before the function name.
|
2015-07-16 03:11:58 +08:00
|
|
|
if ((Current.is(TT_FunctionDeclarationName) ||
|
|
|
|
(Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) &&
|
2014-07-09 15:50:33 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter)
|
2013-08-16 19:20:30 +08:00
|
|
|
return true;
|
2014-07-09 15:50:33 +08:00
|
|
|
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
if (startsSegmentOfBuilderTypeCall(Current) &&
|
2013-08-30 15:12:40 +08:00
|
|
|
(State.Stack.back().CallContinuation != 0 ||
|
2015-03-27 02:46:28 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter))
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
return true;
|
2014-01-05 20:38:10 +08:00
|
|
|
|
|
|
|
// The following could be precomputed as they do not depend on the state.
|
|
|
|
// However, as they should take effect only if the UnwrappedLine does not fit
|
|
|
|
// into the ColumnLimit, they are checked here in the ContinuationIndenter.
|
2014-04-29 22:05:20 +08:00
|
|
|
if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block &&
|
|
|
|
Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
|
2014-01-05 20:38:10 +08:00
|
|
|
return true;
|
|
|
|
|
2015-02-17 18:05:15 +08:00
|
|
|
if (Current.is(tok::lessless) && Previous.is(tok::identifier) &&
|
|
|
|
Previous.TokenText == "endl")
|
|
|
|
return true;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
bool DryRun,
|
|
|
|
unsigned ExtraSpaces) {
|
2013-08-16 19:20:30 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
|
2014-03-18 19:22:45 +08:00
|
|
|
assert(!State.Stack.empty());
|
2014-11-25 18:05:17 +08:00
|
|
|
if ((Current.is(TT_ImplicitStringLiteral) &&
|
2014-05-09 16:15:10 +08:00
|
|
|
(Current.Previous->Tok.getIdentifierInfo() == nullptr ||
|
2013-10-30 21:54:53 +08:00
|
|
|
Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
|
|
|
|
tok::pp_not_keyword))) {
|
2015-01-31 15:05:46 +08:00
|
|
|
unsigned EndColumn =
|
|
|
|
SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
|
|
|
|
if (Current.LastNewlineOffset != 0) {
|
|
|
|
// If there is a newline within this token, the final column will solely
|
|
|
|
// determined by the current end column.
|
|
|
|
State.Column = EndColumn;
|
|
|
|
} else {
|
|
|
|
unsigned StartColumn =
|
|
|
|
SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin());
|
|
|
|
assert(EndColumn >= StartColumn);
|
|
|
|
State.Column += EndColumn - StartColumn;
|
|
|
|
}
|
2014-03-31 22:23:49 +08:00
|
|
|
moveStateToNextToken(State, DryRun, /*Newline=*/false);
|
2013-08-16 19:20:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
unsigned Penalty = 0;
|
|
|
|
if (Newline)
|
|
|
|
Penalty = addTokenOnNewLine(State, DryRun);
|
|
|
|
else
|
2013-11-20 22:54:39 +08:00
|
|
|
addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
|
2013-10-01 22:41:18 +08:00
|
|
|
|
|
|
|
return moveStateToNextToken(State, DryRun, Newline) + Penalty;
|
|
|
|
}
|
|
|
|
|
2013-11-20 22:54:39 +08:00
|
|
|
void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
|
|
|
|
unsigned ExtraSpaces) {
|
2013-10-12 05:25:45 +08:00
|
|
|
FormatToken &Current = *State.NextToken;
|
2013-10-01 22:41:18 +08:00
|
|
|
const FormatToken &Previous = *State.NextToken->Previous;
|
|
|
|
if (Current.is(tok::equal) &&
|
2014-05-08 20:21:30 +08:00
|
|
|
(State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().VariablePos == 0) {
|
|
|
|
State.Stack.back().VariablePos = State.Column;
|
|
|
|
// Move over * and & if they are bound to the variable name.
|
|
|
|
const FormatToken *Tok = &Previous;
|
|
|
|
while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
|
|
|
|
State.Stack.back().VariablePos -= Tok->ColumnWidth;
|
|
|
|
if (Tok->SpacesRequiredBefore != 0)
|
|
|
|
break;
|
|
|
|
Tok = Tok->Previous;
|
|
|
|
}
|
|
|
|
if (Previous.PartOfMultiVariableDeclStmt)
|
|
|
|
State.Stack.back().LastSpace = State.Stack.back().VariablePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
|
|
|
|
|
|
|
|
if (!DryRun)
|
|
|
|
Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0,
|
|
|
|
Spaces, State.Column + Spaces);
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_SelectorName) &&
|
2013-12-23 15:29:06 +08:00
|
|
|
!State.Stack.back().ObjCSelectorNameFound) {
|
|
|
|
if (Current.LongestObjCSelectorName == 0)
|
|
|
|
State.Stack.back().AlignColons = false;
|
|
|
|
else if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
|
|
|
|
State.Column + Spaces + Current.ColumnWidth)
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().ColonPos =
|
2015-03-13 06:13:45 +08:00
|
|
|
std::max(State.FirstIndent + Style.ContinuationIndentWidth,
|
|
|
|
State.Stack.back().Indent) +
|
|
|
|
Current.LongestObjCSelectorName;
|
2013-10-01 22:41:18 +08:00
|
|
|
else
|
|
|
|
State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth;
|
|
|
|
}
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Style.AlignAfterOpenBracket && Previous.opensScope() &&
|
|
|
|
Previous.isNot(TT_ObjCMethodExpr) &&
|
|
|
|
(Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().Indent = State.Column + Spaces;
|
2013-10-08 13:11:18 +08:00
|
|
|
if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().NoLineBreak = true;
|
2015-04-24 18:08:09 +08:00
|
|
|
if (startsSegmentOfBuilderTypeCall(Current) &&
|
|
|
|
State.Column > getNewLineColumn(State))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().ContainsUnwrappedBuilder = true;
|
|
|
|
|
2015-06-05 21:18:09 +08:00
|
|
|
if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
|
2015-03-27 02:46:28 +08:00
|
|
|
State.Stack.back().NoLineBreak = true;
|
2014-09-13 00:35:28 +08:00
|
|
|
if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
|
|
|
|
(Previous.MatchingParen &&
|
|
|
|
(Previous.TotalLength - Previous.MatchingParen->TotalLength > 10))) {
|
|
|
|
// If there is a function call with long parameters, break before trailing
|
|
|
|
// calls. This prevents things like:
|
|
|
|
// EXPECT_CALL(SomeLongParameter).Times(
|
|
|
|
// 2);
|
|
|
|
// We don't want to do this for short parameters as they can just be
|
|
|
|
// indexes.
|
|
|
|
State.Stack.back().NoLineBreak = true;
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Column += Spaces;
|
2014-05-07 17:23:05 +08:00
|
|
|
if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
|
2014-12-12 17:40:58 +08:00
|
|
|
Previous.Previous &&
|
|
|
|
Previous.Previous->isOneOf(tok::kw_if, tok::kw_for)) {
|
2013-10-01 22:41:18 +08:00
|
|
|
// Treat the condition inside an if as if it was a second function
|
2013-10-18 18:38:14 +08:00
|
|
|
// parameter, i.e. let nested calls have a continuation indent.
|
2014-05-07 17:23:05 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2014-12-12 17:40:58 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = State.Column;
|
|
|
|
} else if (!Current.isOneOf(tok::comment, tok::caret) &&
|
|
|
|
(Previous.is(tok::comma) ||
|
|
|
|
(Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) {
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2014-12-12 17:40:58 +08:00
|
|
|
} else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
|
|
|
|
TT_CtorInitializerColon)) &&
|
|
|
|
((Previous.getPrecedence() != prec::Assignment &&
|
|
|
|
(Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
|
|
|
|
!Previous.LastOperator)) ||
|
|
|
|
Current.StartsBinaryExpression)) {
|
2013-10-01 22:41:18 +08:00
|
|
|
// Always indent relative to the RHS of the expression unless this is a
|
|
|
|
// simple assignment without binary expression on the RHS. Also indent
|
|
|
|
// relative to unary operators and the colons of constructor initializers.
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2014-12-12 17:40:58 +08:00
|
|
|
} else if (Previous.is(TT_InheritanceColon)) {
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().Indent = State.Column;
|
2013-10-09 00:24:07 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
|
|
|
} else if (Previous.opensScope()) {
|
2013-10-01 22:41:18 +08:00
|
|
|
// If a function has a trailing call, indent all parameters from the
|
|
|
|
// opening parenthesis. This avoids confusing indents like:
|
|
|
|
// OuterFunction(InnerFunctionCall( // break
|
|
|
|
// ParameterToInnerFunction)) // break
|
|
|
|
// .SecondInnerFunctionCall();
|
|
|
|
bool HasTrailingCall = false;
|
|
|
|
if (Previous.MatchingParen) {
|
|
|
|
const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
|
|
|
|
HasTrailingCall = Next && Next->isMemberAccess();
|
|
|
|
}
|
2015-02-17 17:58:03 +08:00
|
|
|
if (HasTrailingCall && State.Stack.size() > 1 &&
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack[State.Stack.size() - 2].CallContinuation == 0)
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
|
|
|
|
bool DryRun) {
|
2013-10-12 05:25:45 +08:00
|
|
|
FormatToken &Current = *State.NextToken;
|
2013-10-01 22:41:18 +08:00
|
|
|
const FormatToken &Previous = *State.NextToken->Previous;
|
2014-03-27 22:33:30 +08:00
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
// Extra penalty that needs to be added because of the way certain line
|
|
|
|
// breaks are chosen.
|
|
|
|
unsigned Penalty = 0;
|
|
|
|
|
2014-02-11 18:08:11 +08:00
|
|
|
const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
|
|
|
|
const FormatToken *NextNonComment = Previous.getNextNonComment();
|
|
|
|
if (!NextNonComment)
|
|
|
|
NextNonComment = &Current;
|
2014-05-08 20:21:30 +08:00
|
|
|
// The first line break on any NestingLevel causes an extra penalty in order
|
2013-10-01 22:41:18 +08:00
|
|
|
// prefer similar line breaks.
|
|
|
|
if (!State.Stack.back().ContainsLineBreak)
|
|
|
|
Penalty += 15;
|
|
|
|
State.Stack.back().ContainsLineBreak = true;
|
|
|
|
|
|
|
|
Penalty += State.NextToken->SplitPenalty;
|
|
|
|
|
|
|
|
// Breaking before the first "<<" is generally not desirable if the LHS is
|
2013-11-20 22:54:39 +08:00
|
|
|
// short. Also always add the penalty if the LHS is split over mutliple lines
|
2014-04-03 20:00:27 +08:00
|
|
|
// to avoid unnecessary line breaks that just work around this penalty.
|
2014-02-11 18:08:11 +08:00
|
|
|
if (NextNonComment->is(tok::lessless) &&
|
|
|
|
State.Stack.back().FirstLessLess == 0 &&
|
2013-12-20 00:06:40 +08:00
|
|
|
(State.Column <= Style.ColumnLimit / 3 ||
|
2013-11-20 22:54:39 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter))
|
2013-10-01 22:41:18 +08:00
|
|
|
Penalty += Style.PenaltyBreakFirstLessLess;
|
|
|
|
|
2014-03-27 22:33:30 +08:00
|
|
|
State.Column = getNewLineColumn(State);
|
2015-06-18 20:32:59 +08:00
|
|
|
|
|
|
|
// Indent nested blocks relative to this column, unless in a very specific
|
|
|
|
// JavaScript special case where:
|
|
|
|
//
|
|
|
|
// var loooooong_name =
|
|
|
|
// function() {
|
|
|
|
// // code
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// is common and should be formatted like a free-standing function.
|
|
|
|
if (Style.Language != FormatStyle::LK_JavaScript ||
|
|
|
|
Current.NestingLevel != 0 || !PreviousNonComment->is(tok::equal) ||
|
|
|
|
!Current.is(Keywords.kw_function))
|
|
|
|
State.Stack.back().NestedBlockIndent = State.Column;
|
|
|
|
|
2014-03-27 22:33:30 +08:00
|
|
|
if (NextNonComment->isMemberAccess()) {
|
|
|
|
if (State.Stack.back().CallContinuation == 0)
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().CallContinuation = State.Column;
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (NextNonComment->is(TT_SelectorName)) {
|
2013-12-23 15:29:06 +08:00
|
|
|
if (!State.Stack.back().ObjCSelectorNameFound) {
|
2014-02-11 18:08:11 +08:00
|
|
|
if (NextNonComment->LongestObjCSelectorName == 0) {
|
2013-12-23 15:29:06 +08:00
|
|
|
State.Stack.back().AlignColons = false;
|
|
|
|
} else {
|
|
|
|
State.Stack.back().ColonPos =
|
2015-05-13 17:38:25 +08:00
|
|
|
(Style.IndentWrappedFunctionNames
|
|
|
|
? std::max(State.Stack.back().Indent,
|
|
|
|
State.FirstIndent + Style.ContinuationIndentWidth)
|
|
|
|
: State.Stack.back().Indent) +
|
|
|
|
NextNonComment->LongestObjCSelectorName;
|
2013-12-23 15:29:06 +08:00
|
|
|
}
|
2014-03-27 22:33:30 +08:00
|
|
|
} else if (State.Stack.back().AlignColons &&
|
|
|
|
State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) {
|
2014-02-11 18:08:11 +08:00
|
|
|
State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
2014-03-17 22:32:47 +08:00
|
|
|
} else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
|
2013-12-23 15:29:06 +08:00
|
|
|
// FIXME: This is hacky, find a better way. The problem is that in an ObjC
|
|
|
|
// method expression, the block should be aligned to the line starting it,
|
|
|
|
// e.g.:
|
|
|
|
// [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
|
|
|
|
// ^(int *i) {
|
|
|
|
// // ...
|
|
|
|
// }];
|
2014-05-08 20:21:30 +08:00
|
|
|
// Thus, we set LastSpace of the next higher NestingLevel, to which we move
|
2013-12-23 15:29:06 +08:00
|
|
|
// when we consume all of the "}"'s FakeRParens at the "{".
|
2013-12-23 19:25:40 +08:00
|
|
|
if (State.Stack.size() > 1)
|
2014-03-27 22:33:30 +08:00
|
|
|
State.Stack[State.Stack.size() - 2].LastSpace =
|
|
|
|
std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
|
|
|
|
Style.ContinuationIndentWidth;
|
2013-10-01 22:41:18 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
if ((Previous.isOneOf(tok::comma, tok::semi) &&
|
|
|
|
!State.Stack.back().AvoidBinPacking) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.is(TT_BinaryOperator))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Previous.isOneOf(TT_TemplateCloser, TT_JavaAnnotation) &&
|
2014-11-03 10:27:28 +08:00
|
|
|
Current.NestingLevel == 0)
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2014-02-11 18:08:11 +08:00
|
|
|
if (NextNonComment->is(tok::question) ||
|
2013-11-08 08:57:11 +08:00
|
|
|
(PreviousNonComment && PreviousNonComment->is(tok::question)))
|
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2015-06-03 17:26:03 +08:00
|
|
|
if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore)
|
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2013-10-01 22:41:18 +08:00
|
|
|
|
|
|
|
if (!DryRun) {
|
2014-06-04 20:40:57 +08:00
|
|
|
unsigned Newlines = std::max(
|
|
|
|
1u, std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1));
|
2013-10-14 08:46:35 +08:00
|
|
|
Whitespaces.replaceWhitespace(Current, Newlines,
|
|
|
|
State.Stack.back().IndentLevel, State.Column,
|
|
|
|
State.Column, State.Line->InPPDirective);
|
2013-10-01 22:41:18 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
if (!Current.isTrailingComment())
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2014-05-08 20:21:30 +08:00
|
|
|
State.StartOfLineLevel = Current.NestingLevel;
|
|
|
|
State.LowestLevelOnLine = Current.NestingLevel;
|
2013-10-01 22:41:18 +08:00
|
|
|
|
|
|
|
// Any break on this level means that the parent level has been broken
|
|
|
|
// and we need to avoid bin packing there.
|
2014-11-21 21:38:53 +08:00
|
|
|
bool NestedBlockSpecialCase =
|
2015-08-21 19:44:57 +08:00
|
|
|
Style.Language != FormatStyle::LK_Cpp &&
|
2014-11-21 21:38:53 +08:00
|
|
|
Current.is(tok::r_brace) && State.Stack.size() > 1 &&
|
|
|
|
State.Stack[State.Stack.size() - 2].NestedBlockInlined;
|
2015-06-01 17:56:32 +08:00
|
|
|
if (!NestedBlockSpecialCase)
|
|
|
|
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
|
2014-05-21 20:51:23 +08:00
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
|
2013-11-09 03:56:28 +08:00
|
|
|
if (PreviousNonComment &&
|
|
|
|
!PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(PreviousNonComment->isNot(TT_TemplateCloser) ||
|
2014-11-14 21:14:45 +08:00
|
|
|
Current.NestingLevel != 0) &&
|
2015-05-18 21:47:23 +08:00
|
|
|
!PreviousNonComment->isOneOf(
|
|
|
|
TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
|
|
|
|
TT_LeadingJavaAnnotation) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope())
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
|
|
|
|
2013-10-22 23:30:28 +08:00
|
|
|
// If we break after { or the [ of an array initializer, we should also break
|
|
|
|
// before the corresponding } or ].
|
2014-06-10 18:42:26 +08:00
|
|
|
if (PreviousNonComment &&
|
2014-11-25 18:05:17 +08:00
|
|
|
(PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeClosingBrace = true;
|
|
|
|
|
|
|
|
if (State.Stack.back().AvoidBinPacking) {
|
|
|
|
// If we are breaking after '(', '{', '<', this is not bin packing
|
2014-05-21 21:26:58 +08:00
|
|
|
// unless AllowAllParametersOfDeclarationOnNextLine is false or this is a
|
|
|
|
// dict/object literal.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
|
2013-10-01 22:41:18 +08:00
|
|
|
(!Style.AllowAllParametersOfDeclarationOnNextLine &&
|
2014-05-21 21:26:58 +08:00
|
|
|
State.Line->MustBeDeclaration) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous.is(TT_DictLiteral))
|
2013-10-01 22:41:18 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2013-10-01 22:41:18 +08:00
|
|
|
return Penalty;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-03-27 22:33:30 +08:00
|
|
|
unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
|
2014-03-28 00:14:13 +08:00
|
|
|
if (!State.NextToken || !State.NextToken->Previous)
|
|
|
|
return 0;
|
2014-03-27 22:33:30 +08:00
|
|
|
FormatToken &Current = *State.NextToken;
|
2014-10-07 22:45:34 +08:00
|
|
|
const FormatToken &Previous = *Current.Previous;
|
2014-03-27 22:33:30 +08:00
|
|
|
// If we are continuing an expression, we want to use the continuation indent.
|
|
|
|
unsigned ContinuationIndent =
|
|
|
|
std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
|
|
|
|
Style.ContinuationIndentWidth;
|
|
|
|
const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
|
|
|
|
const FormatToken *NextNonComment = Previous.getNextNonComment();
|
|
|
|
if (!NextNonComment)
|
|
|
|
NextNonComment = &Current;
|
2014-11-03 03:16:41 +08:00
|
|
|
|
|
|
|
// Java specific bits.
|
2014-11-04 20:41:02 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Java &&
|
|
|
|
Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends))
|
2014-11-03 03:16:41 +08:00
|
|
|
return std::max(State.Stack.back().LastSpace,
|
|
|
|
State.Stack.back().Indent + Style.ContinuationIndentWidth);
|
|
|
|
|
2014-05-09 21:11:16 +08:00
|
|
|
if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
|
2014-05-08 20:21:30 +08:00
|
|
|
return Current.NestingLevel == 0 ? State.FirstIndent
|
|
|
|
: State.Stack.back().Indent;
|
2015-01-19 18:51:23 +08:00
|
|
|
if (Current.isOneOf(tok::r_brace, tok::r_square) && State.Stack.size() > 1) {
|
2014-12-12 17:40:58 +08:00
|
|
|
if (Current.closesBlockTypeList(Style))
|
|
|
|
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
|
|
|
|
if (Current.MatchingParen &&
|
|
|
|
Current.MatchingParen->BlockKind == BK_BracedInit)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack[State.Stack.size() - 2].LastSpace;
|
2014-12-11 01:24:34 +08:00
|
|
|
return State.FirstIndent;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
2014-04-15 17:54:30 +08:00
|
|
|
if (Current.is(tok::identifier) && Current.Next &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Current.Next->is(TT_DictLiteral))
|
2014-04-15 17:54:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2015-05-17 16:13:23 +08:00
|
|
|
if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.StartOfStringLiteral;
|
2015-05-17 16:13:23 +08:00
|
|
|
if (NextNonComment->is(TT_ObjCStringLiteral) &&
|
|
|
|
State.StartOfStringLiteral != 0)
|
|
|
|
return State.StartOfStringLiteral - 1;
|
2014-03-27 22:33:30 +08:00
|
|
|
if (NextNonComment->is(tok::lessless) &&
|
|
|
|
State.Stack.back().FirstLessLess != 0)
|
|
|
|
return State.Stack.back().FirstLessLess;
|
|
|
|
if (NextNonComment->isMemberAccess()) {
|
2014-12-11 01:24:34 +08:00
|
|
|
if (State.Stack.back().CallContinuation == 0)
|
2014-03-27 22:33:30 +08:00
|
|
|
return ContinuationIndent;
|
2014-12-11 01:24:34 +08:00
|
|
|
return State.Stack.back().CallContinuation;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
|
|
|
if (State.Stack.back().QuestionColumn != 0 &&
|
2014-04-14 19:08:45 +08:00
|
|
|
((NextNonComment->is(tok::colon) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
NextNonComment->is(TT_ConditionalExpr)) ||
|
|
|
|
Previous.is(TT_ConditionalExpr)))
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().QuestionColumn;
|
|
|
|
if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
|
|
|
|
return State.Stack.back().VariablePos;
|
2014-11-01 02:23:49 +08:00
|
|
|
if ((PreviousNonComment &&
|
|
|
|
(PreviousNonComment->ClosesTemplateDeclaration ||
|
2015-05-18 21:47:23 +08:00
|
|
|
PreviousNonComment->isOneOf(
|
|
|
|
TT_AttributeParen, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
|
|
|
|
TT_LeadingJavaAnnotation))) ||
|
2014-07-09 16:42:42 +08:00
|
|
|
(!Style.IndentWrappedFunctionNames &&
|
2014-11-25 18:05:17 +08:00
|
|
|
NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)))
|
2014-03-27 22:33:30 +08:00
|
|
|
return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextNonComment->is(TT_SelectorName)) {
|
2014-03-27 22:33:30 +08:00
|
|
|
if (!State.Stack.back().ObjCSelectorNameFound) {
|
2014-12-11 01:24:34 +08:00
|
|
|
if (NextNonComment->LongestObjCSelectorName == 0)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2015-05-13 17:38:25 +08:00
|
|
|
return (Style.IndentWrappedFunctionNames
|
|
|
|
? std::max(State.Stack.back().Indent,
|
|
|
|
State.FirstIndent + Style.ContinuationIndentWidth)
|
|
|
|
: State.Stack.back().Indent) +
|
2014-12-11 01:24:34 +08:00
|
|
|
NextNonComment->LongestObjCSelectorName -
|
|
|
|
NextNonComment->ColumnWidth;
|
|
|
|
}
|
|
|
|
if (!State.Stack.back().AlignColons)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2014-12-11 01:24:34 +08:00
|
|
|
if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth)
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
|
2014-12-11 01:24:34 +08:00
|
|
|
return State.Stack.back().Indent;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
|
2014-03-27 22:33:30 +08:00
|
|
|
if (State.Stack.back().StartOfArraySubscripts != 0)
|
|
|
|
return State.Stack.back().StartOfArraySubscripts;
|
2014-12-11 01:24:34 +08:00
|
|
|
return ContinuationIndent;
|
2014-03-27 22:33:30 +08:00
|
|
|
}
|
2015-05-07 22:19:59 +08:00
|
|
|
|
|
|
|
// This ensure that we correctly format ObjC methods calls without inputs,
|
|
|
|
// i.e. where the last element isn't selector like: [callee method];
|
|
|
|
if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 &&
|
|
|
|
NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr))
|
2015-05-06 20:48:06 +08:00
|
|
|
return State.Stack.back().Indent;
|
2015-05-07 22:19:59 +08:00
|
|
|
|
2015-03-12 23:04:53 +08:00
|
|
|
if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
|
2015-07-06 22:07:51 +08:00
|
|
|
Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon))
|
2014-03-27 22:33:30 +08:00
|
|
|
return ContinuationIndent;
|
|
|
|
if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
|
2014-11-25 18:05:17 +08:00
|
|
|
PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))
|
2014-03-27 22:33:30 +08:00
|
|
|
return ContinuationIndent;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextNonComment->is(TT_CtorInitializerColon))
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (NextNonComment->is(TT_CtorInitializerComma))
|
2014-03-27 22:33:30 +08:00
|
|
|
return State.Stack.back().Indent;
|
2014-08-06 21:14:58 +08:00
|
|
|
if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() &&
|
2014-11-14 20:31:14 +08:00
|
|
|
!Current.isOneOf(tok::colon, tok::comment))
|
2014-08-06 21:14:58 +08:00
|
|
|
return ContinuationIndent;
|
2014-03-28 00:14:13 +08:00
|
|
|
if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
|
2014-03-27 22:33:30 +08:00
|
|
|
PreviousNonComment->isNot(tok::r_brace))
|
|
|
|
// Ensure that we fall back to the continuation indent width instead of
|
|
|
|
// just flushing continuations left.
|
|
|
|
return State.Stack.back().Indent + Style.ContinuationIndentWidth;
|
|
|
|
return State.Stack.back().Indent;
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
|
|
|
|
bool DryRun, bool Newline) {
|
|
|
|
assert(State.Stack.size());
|
2014-05-26 21:10:39 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_InheritanceColon))
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) {
|
2014-04-14 19:08:45 +08:00
|
|
|
if (State.Stack.back().FirstLessLess == 0)
|
|
|
|
State.Stack.back().FirstLessLess = State.Column;
|
|
|
|
else
|
|
|
|
State.Stack.back().LastOperatorWrapped = Newline;
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
if ((Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless)) ||
|
|
|
|
Current.is(TT_ConditionalExpr))
|
2014-04-14 19:08:45 +08:00
|
|
|
State.Stack.back().LastOperatorWrapped = Newline;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_ArraySubscriptLSquare) &&
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().StartOfArraySubscripts == 0)
|
|
|
|
State.Stack.back().StartOfArraySubscripts = State.Column;
|
2013-11-08 08:57:11 +08:00
|
|
|
if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) ||
|
|
|
|
(Current.getPreviousNonComment() && Current.isNot(tok::colon) &&
|
|
|
|
Current.getPreviousNonComment()->is(tok::question) &&
|
|
|
|
!Style.BreakBeforeTernaryOperators))
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().QuestionColumn = State.Column;
|
|
|
|
if (!Current.opensScope() && !Current.closesScope())
|
|
|
|
State.LowestLevelOnLine =
|
2014-05-08 20:21:30 +08:00
|
|
|
std::min(State.LowestLevelOnLine, Current.NestingLevel);
|
clang-format: Format segments of builder-type calls one per line.
This fixes llvm.org/PR14818.
Before:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT).StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH).Default(ORDER_TEXT);
After:
return llvm::StringSwitch<Reference::Kind>(name)
.StartsWith(".eh_frame_hdr", ORDER_EH_FRAMEHDR)
.StartsWith(".eh_frame", ORDER_EH_FRAME)
.StartsWith(".init", ORDER_INIT)
.StartsWith(".fini", ORDER_FINI)
.StartsWith(".hash", ORDER_HASH)
.Default(ORDER_TEXT);
llvm-svn: 189353
2013-08-27 22:24:43 +08:00
|
|
|
if (Current.isMemberAccess())
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().StartOfFunctionCall =
|
2015-04-07 14:41:24 +08:00
|
|
|
Current.LastOperator ? 0 : State.Column;
|
2015-07-17 06:58:24 +08:00
|
|
|
if (Current.is(TT_SelectorName)) {
|
2013-12-23 15:29:06 +08:00
|
|
|
State.Stack.back().ObjCSelectorNameFound = true;
|
2015-07-17 06:58:24 +08:00
|
|
|
if (Style.IndentWrappedFunctionNames) {
|
|
|
|
State.Stack.back().Indent =
|
|
|
|
State.FirstIndent + Style.ContinuationIndentWidth;
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_CtorInitializerColon)) {
|
2013-08-16 19:20:30 +08:00
|
|
|
// Indent 2 from the column, so:
|
|
|
|
// SomeClass::SomeClass()
|
|
|
|
// : First(...), ...
|
|
|
|
// Next(...)
|
|
|
|
// ^ line up here.
|
|
|
|
State.Stack.back().Indent =
|
|
|
|
State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
|
2015-01-12 18:23:24 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
|
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
|
|
|
}
|
2015-05-14 00:09:21 +08:00
|
|
|
if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
|
|
|
|
State.Stack.back().NestedBlockIndent =
|
|
|
|
State.Column + Current.ColumnWidth + 1;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
// Insert scopes created by fake parenthesis.
|
|
|
|
const FormatToken *Previous = Current.getPreviousNonComment();
|
2014-05-21 20:51:23 +08:00
|
|
|
|
|
|
|
// Add special behavior to support a format commonly used for JavaScript
|
|
|
|
// closures:
|
|
|
|
// SomeFunction(function() {
|
|
|
|
// foo();
|
|
|
|
// bar();
|
|
|
|
// }, a, b, c);
|
2015-06-15 17:23:17 +08:00
|
|
|
if (Current.isNot(tok::comment) && Previous &&
|
|
|
|
Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
|
2014-11-21 21:38:53 +08:00
|
|
|
State.Stack.size() > 1) {
|
2015-06-01 17:56:32 +08:00
|
|
|
if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
|
|
|
|
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
|
2014-11-21 21:38:53 +08:00
|
|
|
State.Stack[i].NoLineBreak = true;
|
|
|
|
State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
|
|
|
|
}
|
|
|
|
if (Previous && (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) ||
|
|
|
|
Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr)) &&
|
|
|
|
!Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
|
|
|
|
State.Stack.back().NestedBlockInlined =
|
|
|
|
!Newline &&
|
2014-12-12 17:40:58 +08:00
|
|
|
(Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1);
|
2014-05-21 20:51:23 +08:00
|
|
|
}
|
|
|
|
|
2014-05-26 21:10:39 +08:00
|
|
|
moveStatePastFakeLParens(State, Newline);
|
|
|
|
moveStatePastScopeOpener(State, Newline);
|
|
|
|
moveStatePastScopeCloser(State);
|
|
|
|
moveStatePastFakeRParens(State);
|
|
|
|
|
2015-05-17 16:13:23 +08:00
|
|
|
if (Current.isStringLiteral() && State.StartOfStringLiteral == 0)
|
2014-05-26 21:10:39 +08:00
|
|
|
State.StartOfStringLiteral = State.Column;
|
2015-05-17 16:13:23 +08:00
|
|
|
if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0)
|
|
|
|
State.StartOfStringLiteral = State.Column + 1;
|
|
|
|
else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
|
2015-06-17 21:08:06 +08:00
|
|
|
!Current.isStringLiteral())
|
2014-05-26 21:10:39 +08:00
|
|
|
State.StartOfStringLiteral = 0;
|
|
|
|
|
|
|
|
State.Column += Current.ColumnWidth;
|
|
|
|
State.NextToken = State.NextToken->Next;
|
|
|
|
unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
|
|
|
|
if (State.Column > getColumnLimit(State)) {
|
|
|
|
unsigned ExcessCharacters = State.Column - getColumnLimit(State);
|
|
|
|
Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Current.Role)
|
|
|
|
Current.Role->formatFromToken(State, this, DryRun);
|
|
|
|
// If the previous has a special role, let it consume tokens as appropriate.
|
|
|
|
// It is necessary to start at the previous token for the only implemented
|
|
|
|
// role (comma separated list). That way, the decision whether or not to break
|
|
|
|
// after the "{" is already done and both options are tried and evaluated.
|
|
|
|
// FIXME: This is ugly, find a better way.
|
|
|
|
if (Previous && Previous->Role)
|
|
|
|
Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
|
|
|
|
|
|
|
|
return Penalty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
|
|
|
|
bool Newline) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken *Previous = Current.getPreviousNonComment();
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
// Don't add extra indentation for the first fake parenthesis after
|
2014-05-02 01:19:34 +08:00
|
|
|
// 'return', assignments or opening <({[. The indentation for these cases
|
2013-08-16 19:20:30 +08:00
|
|
|
// is special cased.
|
|
|
|
bool SkipFirstExtraIndent =
|
2015-03-06 18:57:12 +08:00
|
|
|
(Previous && (Previous->opensScope() ||
|
|
|
|
Previous->isOneOf(tok::semi, tok::kw_return) ||
|
2014-12-02 21:24:51 +08:00
|
|
|
(Previous->getPrecedence() == prec::Assignment &&
|
|
|
|
Style.AlignOperands) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous->is(TT_ObjCMethodExpr)));
|
2013-08-16 19:20:30 +08:00
|
|
|
for (SmallVectorImpl<prec::Level>::const_reverse_iterator
|
|
|
|
I = Current.FakeLParens.rbegin(),
|
|
|
|
E = Current.FakeLParens.rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
ParenState NewParenState = State.Stack.back();
|
|
|
|
NewParenState.ContainsLineBreak = false;
|
2013-09-30 16:29:03 +08:00
|
|
|
|
2014-11-19 07:55:27 +08:00
|
|
|
// Indent from 'LastSpace' unless these are fake parentheses encapsulating
|
|
|
|
// a builder type call after 'return' or, if the alignment after opening
|
|
|
|
// brackets is disabled.
|
2014-10-07 22:45:34 +08:00
|
|
|
if (!Current.isTrailingComment() &&
|
2014-12-02 21:24:51 +08:00
|
|
|
(Style.AlignOperands || *I < prec::Assignment) &&
|
2014-11-20 17:54:49 +08:00
|
|
|
(!Previous || Previous->isNot(tok::kw_return) ||
|
|
|
|
(Style.Language != FormatStyle::LK_Java && *I > 0)) &&
|
2014-11-19 07:55:27 +08:00
|
|
|
(Style.AlignAfterOpenBracket || *I != prec::Comma ||
|
|
|
|
Current.NestingLevel == 0))
|
2013-09-30 16:29:03 +08:00
|
|
|
NewParenState.Indent =
|
|
|
|
std::max(std::max(State.Column, NewParenState.Indent),
|
|
|
|
State.Stack.back().LastSpace);
|
|
|
|
|
2014-04-03 20:00:33 +08:00
|
|
|
// Don't allow the RHS of an operator to be split over multiple lines unless
|
|
|
|
// there is a line-break right after the operator.
|
|
|
|
// Exclude relational operators, as there, it is always more desirable to
|
|
|
|
// have the LHS 'left' of the RHS.
|
2014-04-14 19:08:45 +08:00
|
|
|
if (Previous && Previous->getPrecedence() > prec::Assignment &&
|
2014-11-25 18:05:17 +08:00
|
|
|
Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
|
2014-04-14 19:08:45 +08:00
|
|
|
Previous->getPrecedence() != prec::Relational) {
|
2014-12-02 17:46:56 +08:00
|
|
|
bool BreakBeforeOperator =
|
|
|
|
Previous->is(tok::lessless) ||
|
|
|
|
(Previous->is(TT_BinaryOperator) &&
|
|
|
|
Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) ||
|
|
|
|
(Previous->is(TT_ConditionalExpr) &&
|
|
|
|
Style.BreakBeforeTernaryOperators);
|
2014-04-14 19:08:45 +08:00
|
|
|
if ((!Newline && !BreakBeforeOperator) ||
|
|
|
|
(!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator))
|
|
|
|
NewParenState.NoLineBreak = true;
|
|
|
|
}
|
2014-04-03 20:00:33 +08:00
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
// Do not indent relative to the fake parentheses inserted for "." or "->".
|
|
|
|
// This is a special case to make the following to statements consistent:
|
|
|
|
// OuterFunction(InnerFunctionCall( // break
|
|
|
|
// ParameterToInnerFunction));
|
|
|
|
// OuterFunction(SomeObject.InnerFunctionCall( // break
|
|
|
|
// ParameterToInnerFunction));
|
|
|
|
if (*I > prec::Unknown)
|
|
|
|
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
|
2015-06-29 18:42:59 +08:00
|
|
|
if (*I != prec::Conditional && !Current.is(TT_UnaryOperator))
|
2014-12-09 05:28:31 +08:00
|
|
|
NewParenState.StartOfFunctionCall = State.Column;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
// Always indent conditional expressions. Never indent expression where
|
|
|
|
// the 'operator' is ',', ';' or an assignment (i.e. *I <=
|
|
|
|
// prec::Assignment) as those have different indentation rules. Indent
|
|
|
|
// other expression, unless the indentation needs to be skipped.
|
|
|
|
if (*I == prec::Conditional ||
|
|
|
|
(!SkipFirstExtraIndent && *I > prec::Assignment &&
|
2014-12-02 17:46:56 +08:00
|
|
|
!Current.isTrailingComment()))
|
2013-10-18 18:38:14 +08:00
|
|
|
NewParenState.Indent += Style.ContinuationIndentWidth;
|
2013-10-22 23:30:28 +08:00
|
|
|
if ((Previous && !Previous->opensScope()) || *I > prec::Comma)
|
2013-08-16 19:20:30 +08:00
|
|
|
NewParenState.BreakBeforeParameter = false;
|
|
|
|
State.Stack.push_back(NewParenState);
|
|
|
|
SkipFirstExtraIndent = false;
|
|
|
|
}
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2014-12-12 17:40:58 +08:00
|
|
|
void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
|
|
|
|
for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) {
|
2014-05-28 17:11:53 +08:00
|
|
|
unsigned VariablePos = State.Stack.back().VariablePos;
|
|
|
|
if (State.Stack.size() == 1) {
|
|
|
|
// Do not pop the last element.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
State.Stack.pop_back();
|
|
|
|
State.Stack.back().VariablePos = VariablePos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 21:10:39 +08:00
|
|
|
void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
|
|
|
|
bool Newline) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
if (!Current.opensScope())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Current.MatchingParen && Current.BlockKind == BK_Block) {
|
|
|
|
moveStateToNewBlock(State);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NewIndent;
|
|
|
|
unsigned NewIndentLevel = State.Stack.back().IndentLevel;
|
2015-05-04 15:39:00 +08:00
|
|
|
unsigned LastSpace = State.Stack.back().LastSpace;
|
2014-05-26 21:10:39 +08:00
|
|
|
bool AvoidBinPacking;
|
|
|
|
bool BreakBeforeParameter = false;
|
2015-07-14 19:26:14 +08:00
|
|
|
unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall,
|
|
|
|
State.Stack.back().NestedBlockIndent);
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) {
|
2014-05-26 21:10:39 +08:00
|
|
|
if (Current.opensBlockTypeList(Style)) {
|
2014-12-12 17:40:58 +08:00
|
|
|
NewIndent = State.Stack.back().NestedBlockIndent + Style.IndentWidth;
|
2014-05-26 21:10:39 +08:00
|
|
|
NewIndent = std::min(State.Column + 2, NewIndent);
|
|
|
|
++NewIndentLevel;
|
|
|
|
} else {
|
2014-12-12 17:40:58 +08:00
|
|
|
NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth;
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
|
|
|
const FormatToken *NextNoComment = Current.getNextNonComment();
|
2014-11-25 18:05:17 +08:00
|
|
|
AvoidBinPacking =
|
|
|
|
Current.isOneOf(TT_ArrayInitializerLSquare, TT_DictLiteral) ||
|
2015-05-26 15:26:26 +08:00
|
|
|
Style.Language == FormatStyle::LK_Proto || !Style.BinPackArguments ||
|
2014-11-25 18:05:17 +08:00
|
|
|
(NextNoComment && NextNoComment->is(TT_DesignatedInitializerPeriod));
|
2015-07-14 19:26:14 +08:00
|
|
|
if (Current.ParameterCount > 1)
|
|
|
|
NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
|
2014-05-26 21:10:39 +08:00
|
|
|
} else {
|
|
|
|
NewIndent = Style.ContinuationIndentWidth +
|
|
|
|
std::max(State.Stack.back().LastSpace,
|
|
|
|
State.Stack.back().StartOfFunctionCall);
|
2015-05-04 15:39:00 +08:00
|
|
|
|
|
|
|
// Ensure that different different brackets force relative alignment, e.g.:
|
|
|
|
// void SomeFunction(vector< // break
|
|
|
|
// int> v);
|
|
|
|
// FIXME: We likely want to do this for more combinations of brackets.
|
|
|
|
// Verify that it is wanted for ObjC, too.
|
|
|
|
if (Current.Tok.getKind() == tok::less &&
|
|
|
|
Current.ParentBracket == tok::l_paren) {
|
|
|
|
NewIndent = std::max(NewIndent, State.Stack.back().Indent);
|
|
|
|
LastSpace = std::max(LastSpace, State.Stack.back().Indent);
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:52:05 +08:00
|
|
|
AvoidBinPacking =
|
|
|
|
(State.Line->MustBeDeclaration && !Style.BinPackParameters) ||
|
|
|
|
(!State.Line->MustBeDeclaration && !Style.BinPackArguments) ||
|
|
|
|
(Style.ExperimentalAutoDetectBinPacking &&
|
|
|
|
(Current.PackingKind == PPK_OnePerLine ||
|
|
|
|
(!BinPackInconclusiveFunctions &&
|
|
|
|
Current.PackingKind == PPK_Inconclusive)));
|
2015-04-23 17:23:17 +08:00
|
|
|
if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen) {
|
|
|
|
if (Style.ColumnLimit) {
|
|
|
|
// If this '[' opens an ObjC call, determine whether all parameters fit
|
|
|
|
// into one line and put one per line if they don't.
|
|
|
|
if (getLengthToMatchingParen(Current) + State.Column >
|
2014-05-26 21:10:39 +08:00
|
|
|
getColumnLimit(State))
|
2015-04-23 17:23:17 +08:00
|
|
|
BreakBeforeParameter = true;
|
|
|
|
} else {
|
|
|
|
// For ColumnLimit = 0, we have to figure out whether there is or has to
|
|
|
|
// be a line break within this call.
|
|
|
|
for (const FormatToken *Tok = &Current;
|
|
|
|
Tok && Tok != Current.MatchingParen; Tok = Tok->Next) {
|
2015-06-17 21:08:06 +08:00
|
|
|
if (Tok->MustBreakBefore ||
|
2015-04-23 17:23:17 +08:00
|
|
|
(Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) {
|
|
|
|
BreakBeforeParameter = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
2014-09-19 16:01:25 +08:00
|
|
|
bool NoLineBreak = State.Stack.back().NoLineBreak ||
|
2014-11-25 18:05:17 +08:00
|
|
|
(Current.is(TT_TemplateOpener) &&
|
2014-09-19 16:01:25 +08:00
|
|
|
State.Stack.back().ContainsUnwrappedBuilder);
|
2015-05-04 15:39:00 +08:00
|
|
|
State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, LastSpace,
|
2014-05-26 21:10:39 +08:00
|
|
|
AvoidBinPacking, NoLineBreak));
|
2014-12-12 17:40:58 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = NestedBlockIndent;
|
2014-05-26 21:10:39 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
|
2014-06-03 20:02:45 +08:00
|
|
|
State.Stack.back().HasMultipleNestedBlocks = Current.BlockParameterCount > 1;
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
|
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
if (!Current.closesScope())
|
|
|
|
return;
|
2013-08-16 19:20:30 +08:00
|
|
|
|
|
|
|
// If we encounter a closing ), ], } or >, we can remove a level from our
|
|
|
|
// stacks.
|
2013-08-28 17:17:37 +08:00
|
|
|
if (State.Stack.size() > 1 &&
|
|
|
|
(Current.isOneOf(tok::r_paren, tok::r_square) ||
|
2013-09-05 17:29:45 +08:00
|
|
|
(Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
|
2014-11-25 18:05:17 +08:00
|
|
|
State.NextToken->is(TT_TemplateCloser)))
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.pop_back();
|
2014-05-28 17:11:53 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
if (Current.is(tok::r_square)) {
|
|
|
|
// If this ends the array subscript expr, reset the corresponding value.
|
|
|
|
const FormatToken *NextNonComment = Current.getNextNonComment();
|
|
|
|
if (NextNonComment && NextNonComment->isNot(tok::l_square))
|
|
|
|
State.Stack.back().StartOfArraySubscripts = 0;
|
|
|
|
}
|
2014-05-26 21:10:39 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2014-05-26 21:10:39 +08:00
|
|
|
void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
|
2014-12-12 17:40:58 +08:00
|
|
|
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
|
2014-10-29 00:53:38 +08:00
|
|
|
// ObjC block sometimes follow special indentation rules.
|
2014-11-25 18:05:17 +08:00
|
|
|
unsigned NewIndent =
|
2014-12-12 17:40:58 +08:00
|
|
|
NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
|
|
|
|
? Style.ObjCBlockIndentWidth
|
|
|
|
: Style.IndentWidth);
|
2014-05-26 21:10:39 +08:00
|
|
|
State.Stack.push_back(ParenState(
|
|
|
|
NewIndent, /*NewIndentLevel=*/State.Stack.back().IndentLevel + 1,
|
|
|
|
State.Stack.back().LastSpace, /*AvoidBinPacking=*/true,
|
|
|
|
State.Stack.back().NoLineBreak));
|
2014-12-12 17:40:58 +08:00
|
|
|
State.Stack.back().NestedBlockIndent = NestedBlockIndent;
|
2014-05-26 21:10:39 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2013-09-10 20:29:48 +08:00
|
|
|
unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
|
|
|
|
LineState &State) {
|
2013-08-30 01:32:57 +08:00
|
|
|
// Break before further function parameters on all levels.
|
|
|
|
for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
|
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
|
2013-09-10 17:38:25 +08:00
|
|
|
unsigned ColumnsUsed = State.Column;
|
2013-09-02 21:58:14 +08:00
|
|
|
// We can only affect layout of the first and the last line, so the penalty
|
|
|
|
// for all other lines is constant, and we ignore it.
|
2013-09-05 22:08:34 +08:00
|
|
|
State.Column = Current.LastLineColumnWidth;
|
2013-09-02 21:58:14 +08:00
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
if (ColumnsUsed > getColumnLimit(State))
|
|
|
|
return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
|
2013-08-30 01:32:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
|
|
|
|
LineState &State,
|
|
|
|
bool DryRun) {
|
2013-09-10 20:29:48 +08:00
|
|
|
// Don't break multi-line tokens other than block comments. Instead, just
|
|
|
|
// update the state.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.isNot(TT_BlockComment) && Current.IsMultiline)
|
2013-09-10 20:29:48 +08:00
|
|
|
return addMultilineToken(Current, State);
|
|
|
|
|
2014-11-24 00:46:28 +08:00
|
|
|
// Don't break implicit string literals or import statements.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.is(TT_ImplicitStringLiteral) ||
|
2014-11-24 00:46:28 +08:00
|
|
|
State.Line->Type == LT_ImportStatement)
|
2013-10-30 21:54:53 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-12-20 14:22:01 +08:00
|
|
|
if (!Current.isStringLiteral() && !Current.is(tok::comment))
|
2013-08-23 18:05:49 +08:00
|
|
|
return 0;
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<BreakableToken> Token;
|
2013-09-10 17:38:25 +08:00
|
|
|
unsigned StartColumn = State.Column - Current.ColumnWidth;
|
2013-11-13 01:30:49 +08:00
|
|
|
unsigned ColumnLimit = getColumnLimit(State);
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-12-20 14:22:01 +08:00
|
|
|
if (Current.isStringLiteral()) {
|
2015-01-04 17:11:17 +08:00
|
|
|
// FIXME: String literal breaking is currently disabled for Java and JS, as
|
|
|
|
// it requires strings to be merged using "+" which we don't support.
|
|
|
|
if (Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript)
|
|
|
|
return 0;
|
|
|
|
|
2013-10-12 05:43:05 +08:00
|
|
|
// Don't break string literals inside preprocessor directives (except for
|
|
|
|
// #define directives, as their contents are stored in separate lines and
|
|
|
|
// are not affected by this check).
|
|
|
|
// This way we avoid breaking code with line directives and unknown
|
|
|
|
// preprocessor directives that contain long string literals.
|
|
|
|
if (State.Line->Type == LT_PreprocessorDirective)
|
|
|
|
return 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
// Exempts unterminated string literals from line breaking. The user will
|
|
|
|
// likely want to terminate the string before any line breaking is done.
|
|
|
|
if (Current.IsUnterminatedLiteral)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-17 04:20:49 +08:00
|
|
|
StringRef Text = Current.TokenText;
|
|
|
|
StringRef Prefix;
|
|
|
|
StringRef Postfix;
|
2014-01-09 22:18:12 +08:00
|
|
|
bool IsNSStringLiteral = false;
|
2013-09-17 04:20:49 +08:00
|
|
|
// FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
|
|
|
|
// FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
|
|
|
|
// reduce the overhead) for each FormatToken, which is a string, so that we
|
|
|
|
// don't run multiple checks here on the hot path.
|
2014-01-09 22:18:12 +08:00
|
|
|
if (Text.startswith("\"") && Current.Previous &&
|
|
|
|
Current.Previous->is(tok::at)) {
|
|
|
|
IsNSStringLiteral = true;
|
|
|
|
Prefix = "@\"";
|
|
|
|
}
|
2013-09-17 04:20:49 +08:00
|
|
|
if ((Text.endswith(Postfix = "\"") &&
|
2014-01-09 22:18:12 +08:00
|
|
|
(IsNSStringLiteral || Text.startswith(Prefix = "\"") ||
|
|
|
|
Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") ||
|
|
|
|
Text.startswith(Prefix = "u8\"") ||
|
2013-09-17 04:20:49 +08:00
|
|
|
Text.startswith(Prefix = "L\""))) ||
|
2014-12-15 04:47:11 +08:00
|
|
|
(Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) {
|
2013-09-28 00:14:22 +08:00
|
|
|
Token.reset(new BreakableStringLiteral(
|
|
|
|
Current, State.Line->Level, StartColumn, Prefix, Postfix,
|
|
|
|
State.Line->InPPDirective, Encoding, Style));
|
2013-09-17 04:20:49 +08:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.is(TT_BlockComment) && Current.isTrailingComment()) {
|
2014-01-02 23:13:14 +08:00
|
|
|
if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
|
|
|
|
return 0;
|
2013-08-16 19:20:30 +08:00
|
|
|
Token.reset(new BreakableBlockComment(
|
2013-09-28 00:14:22 +08:00
|
|
|
Current, State.Line->Level, StartColumn, Current.OriginalColumn,
|
|
|
|
!Current.Previous, State.Line->InPPDirective, Encoding, Style));
|
2014-11-25 18:05:17 +08:00
|
|
|
} else if (Current.is(TT_LineComment) &&
|
2014-05-09 16:15:10 +08:00
|
|
|
(Current.Previous == nullptr ||
|
2014-11-25 18:05:17 +08:00
|
|
|
Current.Previous->isNot(TT_ImplicitStringLiteral))) {
|
2014-01-02 23:13:14 +08:00
|
|
|
if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
|
|
|
|
return 0;
|
2013-09-28 00:14:22 +08:00
|
|
|
Token.reset(new BreakableLineComment(Current, State.Line->Level,
|
2013-11-13 01:30:49 +08:00
|
|
|
StartColumn, /*InPPDirective=*/false,
|
2013-09-28 00:14:22 +08:00
|
|
|
Encoding, Style));
|
2013-11-13 01:30:49 +08:00
|
|
|
// We don't insert backslashes when breaking line comments.
|
|
|
|
ColumnLimit = Style.ColumnLimit;
|
2013-08-16 19:20:30 +08:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-13 01:30:49 +08:00
|
|
|
if (Current.UnbreakableTailLength >= ColumnLimit)
|
2013-08-16 19:20:30 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-11-13 01:30:49 +08:00
|
|
|
unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength;
|
2013-08-16 19:20:30 +08:00
|
|
|
bool BreakInserted = false;
|
|
|
|
unsigned Penalty = 0;
|
|
|
|
unsigned RemainingTokenColumns = 0;
|
|
|
|
for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
|
|
|
|
LineIndex != EndIndex; ++LineIndex) {
|
|
|
|
if (!DryRun)
|
|
|
|
Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
|
|
|
|
unsigned TailOffset = 0;
|
|
|
|
RemainingTokenColumns =
|
|
|
|
Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
|
|
|
|
while (RemainingTokenColumns > RemainingSpace) {
|
|
|
|
BreakableToken::Split Split =
|
2013-11-13 01:30:49 +08:00
|
|
|
Token->getSplit(LineIndex, TailOffset, ColumnLimit);
|
2013-08-16 19:20:30 +08:00
|
|
|
if (Split.first == StringRef::npos) {
|
|
|
|
// The last line's penalty is handled in addNextStateToQueue().
|
|
|
|
if (LineIndex < EndIndex - 1)
|
|
|
|
Penalty += Style.PenaltyExcessCharacter *
|
|
|
|
(RemainingTokenColumns - RemainingSpace);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(Split.first != 0);
|
|
|
|
unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
|
|
|
|
LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
|
2013-11-13 01:50:13 +08:00
|
|
|
|
|
|
|
// We can remove extra whitespace instead of breaking the line.
|
|
|
|
if (RemainingTokenColumns + 1 - Split.second <= RemainingSpace) {
|
|
|
|
RemainingTokenColumns = 0;
|
|
|
|
if (!DryRun)
|
|
|
|
Token->replaceWhitespace(LineIndex, TailOffset, Split, Whitespaces);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-15 22:52:43 +08:00
|
|
|
// When breaking before a tab character, it may be moved by a few columns,
|
|
|
|
// but will still be expanded to the next tab stop, so we don't save any
|
|
|
|
// columns.
|
|
|
|
if (NewRemainingTokenColumns == RemainingTokenColumns)
|
|
|
|
break;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
assert(NewRemainingTokenColumns < RemainingTokenColumns);
|
|
|
|
if (!DryRun)
|
|
|
|
Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
|
2013-08-28 18:03:58 +08:00
|
|
|
Penalty += Current.SplitPenalty;
|
2013-08-16 19:20:30 +08:00
|
|
|
unsigned ColumnsUsed =
|
|
|
|
Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
|
2013-11-13 01:30:49 +08:00
|
|
|
if (ColumnsUsed > ColumnLimit) {
|
|
|
|
Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit);
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
TailOffset += Split.first + Split.second;
|
|
|
|
RemainingTokenColumns = NewRemainingTokenColumns;
|
|
|
|
BreakInserted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
State.Column = RemainingTokenColumns;
|
|
|
|
|
|
|
|
if (BreakInserted) {
|
|
|
|
// If we break the token inside a parameter list, we need to break before
|
|
|
|
// the next parameter on all levels, so that the next parameter is clearly
|
|
|
|
// visible. Line comments already introduce a break.
|
2014-11-25 18:05:17 +08:00
|
|
|
if (Current.isNot(TT_LineComment)) {
|
2013-08-16 19:20:30 +08:00
|
|
|
for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
|
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
}
|
|
|
|
|
2013-12-20 14:22:01 +08:00
|
|
|
Penalty += Current.isStringLiteral() ? Style.PenaltyBreakString
|
|
|
|
: Style.PenaltyBreakComment;
|
2013-08-28 18:03:58 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
State.Stack.back().LastSpace = StartColumn;
|
|
|
|
}
|
|
|
|
return Penalty;
|
|
|
|
}
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
|
2013-08-16 19:20:30 +08:00
|
|
|
// In preprocessor directives reserve two chars for trailing " \"
|
2013-09-05 17:29:45 +08:00
|
|
|
return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 15:23:08 +08:00
|
|
|
bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
|
2013-08-23 19:57:34 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
2014-11-25 18:05:17 +08:00
|
|
|
if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral))
|
2013-08-23 19:57:34 +08:00
|
|
|
return false;
|
2013-08-30 01:32:57 +08:00
|
|
|
// We never consider raw string literals "multiline" for the purpose of
|
2013-12-16 15:23:08 +08:00
|
|
|
// AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
|
|
|
|
// (see TokenAnnotator::mustBreakBefore().
|
2013-08-30 01:32:57 +08:00
|
|
|
if (Current.TokenText.startswith("R\""))
|
|
|
|
return false;
|
2013-09-10 17:38:25 +08:00
|
|
|
if (Current.IsMultiline)
|
2013-08-30 01:32:57 +08:00
|
|
|
return true;
|
2013-08-23 19:57:34 +08:00
|
|
|
if (Current.getNextNonComment() &&
|
2013-12-20 14:22:01 +08:00
|
|
|
Current.getNextNonComment()->isStringLiteral())
|
2013-08-23 19:57:34 +08:00
|
|
|
return true; // Implicit concatenation.
|
2015-01-20 20:59:20 +08:00
|
|
|
if (Style.ColumnLimit != 0 &&
|
|
|
|
State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
|
|
|
|
Style.ColumnLimit)
|
2013-08-23 19:57:34 +08:00
|
|
|
return true; // String will be split.
|
2013-08-30 01:32:57 +08:00
|
|
|
return false;
|
2013-08-23 19:57:34 +08:00
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|