From 119ff533e4c37a85782c6ffe121e391d063218bd Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 14 Nov 2014 12:31:14 +0000 Subject: [PATCH] clang-format: Improve indentation of comments in expressions. Before: int i = (a) // comment + b; return aaaa == bbbb // comment ? aaaa : bbbb; After: int i = (a) // comment + b; return aaaa == bbbb // comment ? aaaa : bbbb; llvm-svn: 221985 --- clang/lib/Format/ContinuationIndenter.cpp | 2 +- clang/lib/Format/TokenAnnotator.cpp | 7 +++++-- clang/unittests/Format/FormatTest.cpp | 8 ++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 9e6014a..7fdc1af 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -594,7 +594,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { if (NextNonComment->Type == TT_CtorInitializerComma) return State.Stack.back().Indent; if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() && - Current.isNot(tok::colon)) + !Current.isOneOf(tok::colon, tok::comment)) return ContinuationIndent; if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment && PreviousNonComment->isNot(tok::r_brace)) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 48ce19e..cb945c6 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1186,9 +1186,12 @@ private: if (Precedence > prec::Unknown) Start->StartsBinaryExpression = true; if (Current) { - ++Current->Previous->FakeRParens; + FormatToken *Previous = Current->Previous; + if (Previous->is(tok::comment) && Previous->Previous) + Previous = Previous->Previous; + ++Previous->FakeRParens; if (Precedence > prec::Unknown) - Current->Previous->EndsBinaryExpression = true; + Previous->EndsBinaryExpression = true; } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index b8ab1df..d2e577a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3266,6 +3266,10 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { " > ccccc) {\n" "}", Style); + verifyFormat("return (a)\n" + " // comment\n" + " + b;", + Style); // Forced by comments. verifyFormat( @@ -4070,6 +4074,10 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " aaaaaaaaa\n" " ? b\n" " : c);"); + verifyFormat("return aaaa == bbbb\n" + " // comment\n" + " ? aaaa\n" + " : bbbb;"); verifyFormat( "unsigned Indent =\n" " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" -- 2.7.4