summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/Format/ContinuationIndenter.cpp43
-rw-r--r--clang/lib/Format/ContinuationIndenter.h12
-rw-r--r--clang/lib/Format/Format.cpp2
3 files changed, 27 insertions, 30 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 40b50dde851..825a88ebae0 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -117,9 +117,8 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
// Don't create a 'hanging' indent if there are multiple blocks in a single
// statement.
- if (Style.Language == FormatStyle::LK_JavaScript &&
- Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
- State.Stack[State.Stack.size() - 2].JSFunctionInlined &&
+ if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
+ State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks)
return false;
@@ -453,11 +452,10 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
// Any break on this level means that the parent level has been broken
// and we need to avoid bin packing there.
- bool JavaScriptFormat = Style.Language == FormatStyle::LK_JavaScript &&
- Current.is(tok::r_brace) &&
- State.Stack.size() > 1 &&
- State.Stack[State.Stack.size() - 2].JSFunctionInlined;
- if (!JavaScriptFormat) {
+ bool NestedBlockSpecialCase =
+ Current.is(tok::r_brace) && State.Stack.size() > 1 &&
+ State.Stack[State.Stack.size() - 2].NestedBlockInlined;
+ if (!NestedBlockSpecialCase) {
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
State.Stack[i].BreakBeforeParameter = true;
}
@@ -520,7 +518,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
: State.Stack.back().Indent;
if (Current.isOneOf(tok::r_brace, tok::r_square)) {
if (State.Stack.size() > 1 &&
- State.Stack[State.Stack.size() - 2].JSFunctionInlined)
+ State.Stack[State.Stack.size() - 2].NestedBlockInlined)
return State.FirstIndent;
if (Current.closesBlockTypeList(Style) ||
(Current.MatchingParen &&
@@ -666,22 +664,21 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
// foo();
// bar();
// }, a, b, c);
- if (Style.Language == FormatStyle::LK_JavaScript) {
- if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) &&
- State.Stack.size() > 1) {
- if (State.Stack[State.Stack.size() - 2].JSFunctionInlined && Newline) {
- for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
- State.Stack[i].NoLineBreak = true;
- }
+ if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) &&
+ State.Stack.size() > 1) {
+ if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline) {
+ for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
+ State.Stack[i].NoLineBreak = true;
}
- State.Stack[State.Stack.size() - 2].JSFunctionInlined = false;
}
- if (Current.is(Keywords.kw_function))
- State.Stack.back().JSFunctionInlined =
- !Newline && Previous && Previous->Type != TT_DictLiteral &&
- // If the unnamed function is the only parameter to another function,
- // we can likely inline it and come up with a good format.
- (Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1);
+ 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 &&
+ (Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1) && !Newline;
}
moveStatePastFakeLParens(State, Newline);
diff --git a/clang/lib/Format/ContinuationIndenter.h b/clang/lib/Format/ContinuationIndenter.h
index 5abbe7e9a21..004ddebac4a 100644
--- a/clang/lib/Format/ContinuationIndenter.h
+++ b/clang/lib/Format/ContinuationIndenter.h
@@ -155,7 +155,7 @@ struct ParenState {
NestedNameSpecifierContinuation(0), CallContinuation(0), VariablePos(0),
ContainsLineBreak(false), ContainsUnwrappedBuilder(0),
AlignColons(true), ObjCSelectorNameFound(false),
- HasMultipleNestedBlocks(false), JSFunctionInlined(false) {}
+ HasMultipleNestedBlocks(false), NestedBlockInlined(false) {}
/// \brief The position to which a specific parenthesis level needs to be
/// indented.
@@ -256,9 +256,9 @@ struct ParenState {
/// the same token.
bool HasMultipleNestedBlocks;
- // \brief The previous JavaScript 'function' keyword is not wrapped to a new
- // line.
- bool JSFunctionInlined;
+ // \brief The start of a nested block (e.g. lambda introducer in C++ or
+ // "function" in JavaScript) is not wrapped to a new line.
+ bool NestedBlockInlined;
bool operator<(const ParenState &Other) const {
if (Indent != Other.Indent)
@@ -293,8 +293,8 @@ struct ParenState {
return ContainsLineBreak < Other.ContainsLineBreak;
if (ContainsUnwrappedBuilder != Other.ContainsUnwrappedBuilder)
return ContainsUnwrappedBuilder < Other.ContainsUnwrappedBuilder;
- if (JSFunctionInlined != Other.JSFunctionInlined)
- return JSFunctionInlined < Other.JSFunctionInlined;
+ if (NestedBlockInlined != Other.NestedBlockInlined)
+ return NestedBlockInlined < Other.NestedBlockInlined;
return false;
}
};
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b96c17abbc0..7c062d68376 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1285,7 +1285,7 @@ private:
int AdditionalIndent =
State.FirstIndent - State.Line->Level * Style.IndentWidth;
if (State.Stack.size() < 2 ||
- !State.Stack[State.Stack.size() - 2].JSFunctionInlined) {
+ !State.Stack[State.Stack.size() - 2].NestedBlockInlined) {
AdditionalIndent = State.Stack.back().Indent -
Previous.Children[0]->Level * Style.IndentWidth;
}
OpenPOWER on IntegriCloud