diff options
author | Daniel Jasper <djasper@google.com> | 2015-10-27 12:38:37 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2015-10-27 12:38:37 +0000 |
commit | 6501f7e8fdebcf2094d7506f121bf9be36621bdf (patch) | |
tree | 5d8bf08e233c1266722450c669c1fdb12cf44e95 /clang/lib | |
parent | 244d27149a2a89ef50c45c37cd907c73bf9b30df (diff) | |
download | bcm5719-llvm-6501f7e8fdebcf2094d7506f121bf9be36621bdf.tar.gz bcm5719-llvm-6501f7e8fdebcf2094d7506f121bf9be36621bdf.zip |
clang-format: Add an additional value to AlignAfterOpenBracket: AlwaysBreak.
Summary:
If this option is set, clang-format will always insert a line wrap, e.g.
before the first parameter of a function call unless all parameters fit
on the same line. This obviates the need to make a decision on the
alignment itself.
Use this style for Google's JavaScript style and add some minor tweaks
to correctly handle nested blocks etc. with it. Don't use this option
for for/while loops.
Reviewers: klimek
Subscribers: klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D14104
llvm-svn: 251405
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 29 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 23 | ||||
-rw-r--r-- | clang/lib/Format/FormatToken.cpp | 2 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 11 |
4 files changed, 44 insertions, 21 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index c5b768b5674..030f67ef8d7 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -327,8 +327,17 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth; } - if (Style.AlignAfterOpenBracket && Previous.opensScope() && - Previous.isNot(TT_ObjCMethodExpr) && + // In "AlwaysBreak" mode, enforce wrapping directly after the parenthesis by + // disallowing any further line breaks if there is no line break after the + // opening parenthesis. Don't break if it doesn't conserve columns. + if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak && + Previous.is(tok::l_paren) && State.Column > getNewLineColumn(State) && + (!Previous.Previous || + !Previous.Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch))) + State.Stack.back().NoLineBreak = true; + + if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign && + Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) && (Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit)) State.Stack.back().Indent = State.Column + Spaces; if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style)) @@ -794,8 +803,8 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State, (Style.AlignOperands || *I < prec::Assignment) && (!Previous || Previous->isNot(tok::kw_return) || (Style.Language != FormatStyle::LK_Java && *I > 0)) && - (Style.AlignAfterOpenBracket || *I != prec::Comma || - Current.NestingLevel == 0)) + (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign || + *I != prec::Comma || Current.NestingLevel == 0)) NewParenState.Indent = std::max(std::max(State.Column, NewParenState.Indent), State.Stack.back().LastSpace); @@ -874,8 +883,15 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, bool BreakBeforeParameter = false; unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall, State.Stack.back().NestedBlockIndent); + // Generally inherit NoLineBreak from the current scope to nested scope. + // However, don't do this for nested blocks, e.g. lambdas as these follow + // different indentation rules. + bool NoLineBreak = State.Stack.back().NoLineBreak || + (Current.is(TT_TemplateOpener) && + State.Stack.back().ContainsUnwrappedBuilder); if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) { if (Current.opensBlockTypeList(Style)) { + NoLineBreak = false; NewIndent = State.Stack.back().NestedBlockIndent + Style.IndentWidth; NewIndent = std::min(State.Column + 2, NewIndent); ++NewIndentLevel; @@ -933,9 +949,6 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, } } } - bool NoLineBreak = State.Stack.back().NoLineBreak || - (Current.is(TT_TemplateOpener) && - State.Stack.back().ContainsUnwrappedBuilder); State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, LastSpace, AvoidBinPacking, NoLineBreak)); State.Stack.back().NestedBlockIndent = NestedBlockIndent; @@ -974,7 +987,7 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State) { State.Stack.push_back(ParenState( NewIndent, /*NewIndentLevel=*/State.Stack.back().IndentLevel + 1, State.Stack.back().LastSpace, /*AvoidBinPacking=*/true, - State.Stack.back().NoLineBreak)); + /*NoLineBreak=*/false)); State.Stack.back().NestedBlockIndent = NestedBlockIndent; State.Stack.back().BreakBeforeParameter = true; } diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 365c22a8ec2..4b998c764be 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -128,6 +128,18 @@ struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> { } }; +template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> { + static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) { + IO.enumCase(Value, "Align", FormatStyle::BAS_Align); + IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign); + IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak); + + // For backward compatibility. + IO.enumCase(Value, "true", FormatStyle::BAS_Align); + IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign); + } +}; + template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> { static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) { IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle); @@ -425,7 +437,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.Language = FormatStyle::LK_Cpp; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AlignEscapedNewlinesLeft = false; - LLVMStyle.AlignAfterOpenBracket = true; + LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; LLVMStyle.AlignOperands = true; LLVMStyle.AlignTrailingComments = true; LLVMStyle.AlignConsecutiveAssignments = false; @@ -523,7 +535,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1; if (Language == FormatStyle::LK_Java) { - GoogleStyle.AlignAfterOpenBracket = false; + GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; GoogleStyle.AlignOperands = false; GoogleStyle.AlignTrailingComments = false; GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; @@ -534,11 +546,12 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.SpaceAfterCStyleCast = true; GoogleStyle.SpacesBeforeTrailingComments = 1; } else if (Language == FormatStyle::LK_JavaScript) { + GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; + GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; + GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; GoogleStyle.BreakBeforeTernaryOperators = false; GoogleStyle.MaxEmptyLinesToKeep = 3; GoogleStyle.SpacesInContainerLiterals = false; - GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; - GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; } else if (Language == FormatStyle::LK_Proto) { GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; GoogleStyle.SpacesInContainerLiterals = false; @@ -588,7 +601,7 @@ FormatStyle getMozillaStyle() { FormatStyle getWebKitStyle() { FormatStyle Style = getLLVMStyle(); Style.AccessModifierOffset = -4; - Style.AlignAfterOpenBracket = false; + Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; Style.AlignOperands = false; Style.AlignTrailingComments = false; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index b293cf25c35..b1c64b8efec 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -155,7 +155,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { return; // Column format doesn't really make sense if we don't align after brackets. - if (!Style.AlignAfterOpenBracket) + if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign) return; FormatToken *ItemBegin = Token->Next; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 8ec11ced1d4..d26f15b8be8 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -390,12 +390,8 @@ private: } void updateParameterCount(FormatToken *Left, FormatToken *Current) { - if (Current->is(TT_LambdaLSquare) || - (Current->is(tok::caret) && Current->is(TT_UnaryOperator)) || - (Style.Language == FormatStyle::LK_JavaScript && - Current->is(Keywords.kw_function))) { + if (Current->is(tok::l_brace) && !Current->is(TT_DictLiteral)) ++Left->BlockParameterCount; - } if (Current->is(tok::comma)) { ++Left->ParameterCount; if (!Left->Role) @@ -1760,7 +1756,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr)) return Line.MightBeFunctionDecl ? 50 : 500; - if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket) + if (Left.is(tok::l_paren) && InFunctionDecl && + Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) return 100; if (Left.is(tok::l_paren) && Left.Previous && Left.Previous->isOneOf(tok::kw_if, tok::kw_for)) @@ -1772,7 +1769,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if (Left.is(TT_TemplateOpener)) return 100; if (Left.opensScope()) { - if (!Style.AlignAfterOpenBracket) + if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign) return 0; return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter : 19; |