diff options
author | Daniel Jasper <djasper@google.com> | 2013-11-08 00:57:11 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-11-08 00:57:11 +0000 |
commit | 165b29e2d2de33a49d75fc5b8649a244f766dfcc (patch) | |
tree | c97d3806b65ec9b360e3d5514c004140e84ddb11 /clang/lib | |
parent | 688b6c01ce7e0f7efb3683e0e20768ba6c6dae8d (diff) | |
download | bcm5719-llvm-165b29e2d2de33a49d75fc5b8649a244f766dfcc.tar.gz bcm5719-llvm-165b29e2d2de33a49d75fc5b8649a244f766dfcc.zip |
clang-format: Make breaking before ternary operators configurable.
llvm-svn: 194229
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Format/ContinuationIndenter.cpp | 21 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 21 |
3 files changed, 30 insertions, 16 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index a80e87ddfc8..442f9fa8505 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -125,8 +125,11 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) return true; if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || - Current.is(tok::question) || - (Current.Type == TT_ConditionalExpr && Previous.isNot(tok::question))) && + (Style.BreakBeforeTernaryOperators && + (Current.is(tok::question) || (Current.Type == TT_ConditionalExpr && + Previous.isNot(tok::question)))) || + (!Style.BreakBeforeTernaryOperators && + (Previous.is(tok::question) || Previous.Type == TT_ConditionalExpr))) && State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() && !Current.isOneOf(tok::r_paren, tok::r_brace)) return true; @@ -357,7 +360,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, } else { State.Column = State.Stack.back().CallContinuation; } - } else if (Current.Type == TT_ConditionalExpr) { + } else if (State.Stack.back().QuestionColumn != 0 && + (Current.Type == TT_ConditionalExpr || + Previous.Type == TT_ConditionalExpr)) { State.Column = State.Stack.back().QuestionColumn; } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) { State.Column = State.Stack.back().VariablePos; @@ -398,14 +403,15 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, State.Column += Style.ContinuationIndentWidth; } - if (Current.is(tok::question)) - State.Stack.back().BreakBeforeParameter = true; if ((Previous.isOneOf(tok::comma, tok::semi) && !State.Stack.back().AvoidBinPacking) || Previous.Type == TT_BinaryOperator) State.Stack.back().BreakBeforeParameter = false; if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0) State.Stack.back().BreakBeforeParameter = false; + if (Current.is(tok::question) || + (PreviousNonComment && PreviousNonComment->is(tok::question))) + State.Stack.back().BreakBeforeParameter = true; if (!DryRun) { unsigned Newlines = 1; @@ -465,7 +471,10 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, if (Current.Type == TT_ArraySubscriptLSquare && State.Stack.back().StartOfArraySubscripts == 0) State.Stack.back().StartOfArraySubscripts = State.Column; - if (Current.is(tok::question)) + if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) || + (Current.getPreviousNonComment() && Current.isNot(tok::colon) && + Current.getPreviousNonComment()->is(tok::question) && + !Style.BreakBeforeTernaryOperators)) State.Stack.back().QuestionColumn = State.Column; if (!Current.opensScope() && !Current.closesScope()) State.LowestLevelOnLine = diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 9443a60aa6d..f2899345fbd 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -123,6 +123,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> { Style.AlwaysBreakBeforeMultilineStrings); IO.mapOptional("BreakBeforeBinaryOperators", Style.BreakBeforeBinaryOperators); + IO.mapOptional("BreakBeforeTernaryOperators", + Style.BreakBeforeTernaryOperators); IO.mapOptional("BreakConstructorInitializersBeforeComma", Style.BreakConstructorInitializersBeforeComma); IO.mapOptional("BinPackParameters", Style.BinPackParameters); @@ -194,6 +196,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.AlwaysBreakTemplateDeclarations = false; LLVMStyle.BinPackParameters = true; LLVMStyle.BreakBeforeBinaryOperators = false; + LLVMStyle.BreakBeforeTernaryOperators = true; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; LLVMStyle.BreakConstructorInitializersBeforeComma = false; LLVMStyle.ColumnLimit = 80; @@ -240,6 +243,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.AlwaysBreakTemplateDeclarations = true; GoogleStyle.BinPackParameters = true; GoogleStyle.BreakBeforeBinaryOperators = false; + GoogleStyle.BreakBeforeTernaryOperators = true; GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; GoogleStyle.BreakConstructorInitializersBeforeComma = false; GoogleStyle.ColumnLimit = 80; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 5c70c8f914a..474f6a5adbb 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1417,6 +1417,16 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, const FormatToken &Left = *Right.Previous; if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) return true; + if (Right.isTrailingComment()) + // We rely on MustBreakBefore being set correctly here as we should not + // change the "binding" behavior of a comment. + return false; + if (Left.is(tok::question) && Right.is(tok::colon)) + return false; + if (Right.Type == TT_ConditionalExpr || Right.is(tok::question)) + return Style.BreakBeforeTernaryOperators; + if (Left.Type == TT_ConditionalExpr || Left.is(tok::question)) + return !Style.BreakBeforeTernaryOperators; if (Right.is(tok::colon) && (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr)) return false; @@ -1429,10 +1439,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return true; if (Left.ClosesTemplateDeclaration) return true; - if ((Right.Type == TT_ConditionalExpr && - !(Right.is(tok::colon) && Left.is(tok::question))) || - Right.is(tok::question)) - return true; if (Right.Type == TT_RangeBasedForLoopColon || Right.Type == TT_OverloadedOperatorLParen || Right.Type == TT_OverloadedOperator) @@ -1442,8 +1448,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, if (Right.Type == TT_RangeBasedForLoopColon) return false; if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || - Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr || - Left.isOneOf(tok::question, tok::kw_operator)) + Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator)) return false; if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) return false; @@ -1457,10 +1462,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, } if (Right.Type == TT_ImplicitStringLiteral) return false; - if (Right.isTrailingComment()) - // We rely on MustBreakBefore being set correctly here as we should not - // change the "binding" behavior of a comment. - return false; if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser) return false; |