From: Daniel Jasper Date: Fri, 14 Nov 2014 12:31:14 +0000 (+0000) Subject: clang-format: Improve indentation of comments in expressions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=712b8b0eeac5f2fc449f1df4e9c51b18e0e706be;p=clang 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; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221985 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 9e6014a683..7fdc1af09b 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/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/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 48ce19e132..cb945c6161 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index b8ab1df139..d2e577aacb 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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"