From: Daniel Jasper Date: Wed, 3 Feb 2016 17:27:10 +0000 (+0000) Subject: clang-format: Fix formatting of ternary expressions with comments. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7d018c0578a34bf413106bf72c47152b2cb83937;p=clang clang-format: Fix formatting of ternary expressions with comments. Before: int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ? /*bbbbbbbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb : ccccccccccccccccccccccccccc; After: int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ? /*bbbbbbbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb : ccccccccccccccccccccccccccc; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@259670 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index fa51f05543..b65c0e0664 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -705,11 +705,15 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, if (Current.is(TT_ArraySubscriptLSquare) && State.Stack.back().StartOfArraySubscripts == 0) State.Stack.back().StartOfArraySubscripts = State.Column; - if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) || - (Current.getPreviousNonComment() && Current.isNot(tok::colon) && - Current.getPreviousNonComment()->is(tok::question) && - !Style.BreakBeforeTernaryOperators)) + if (Style.BreakBeforeTernaryOperators && Current.is(tok::question)) State.Stack.back().QuestionColumn = State.Column; + if (!Style.BreakBeforeTernaryOperators && Current.isNot(tok::colon)) { + const FormatToken *Previous = Current.Previous; + while (Previous && Previous->isTrailingComment()) + Previous = Previous->Previous; + if (Previous && Previous->is(tok::question)) + State.Stack.back().QuestionColumn = State.Column; + } if (!Current.opensScope() && !Current.closesScope()) State.LowestLevelOnLine = std::min(State.LowestLevelOnLine, Current.NestingLevel); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 6ffeb65280..e03a484f29 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4694,6 +4694,10 @@ TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { " ccccccccccccccc :\n" " ddddddddddddddd);", Style); + verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" + " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" + " ccccccccccccccccccccccccccc;", + Style); } TEST_F(FormatTest, DeclarationsOfMultipleVariables) {