From: Daniel Jasper Date: Mon, 16 Dec 2013 15:01:54 +0000 (+0000) Subject: clang-format: Keep trailing annotations together. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1f69f4c27bdb72c82109450ebd4c2a2803a4aaa7;p=clang clang-format: Keep trailing annotations together. Before: virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa, aaaaaaaaaaa aaaaa) const override; virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() const override; After: virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa, aaaaaaaaaaa aaaaa) const override; virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() const override; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@197391 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index fa676b3973..60265509db 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -58,6 +58,7 @@ enum TokenType { TT_StartOfName, TT_TemplateCloser, TT_TemplateOpener, + TT_TrailingAnnotation, TT_TrailingReturnArrow, TT_TrailingUnaryOperator, TT_UnaryOperator, diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index e2615dbb6a..db071f101e 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -367,7 +367,7 @@ private: if (!parseParens()) return false; if (Line.MustBeDeclaration && Contexts.size() == 1 && - !Contexts.back().IsExpression) + !Contexts.back().IsExpression && Line.First->Type != TT_ObjCProperty) Line.MightBeFunctionDecl = true; break; case tok::l_square: @@ -715,6 +715,11 @@ private: if (PreviousNoComment && PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) Current.Type = TT_DesignatedInitializerPeriod; + } else if (Current.isOneOf(tok::identifier, tok::kw_const) && + Line.MightBeFunctionDecl && Contexts.size() == 1) { + // Line.MightBeFunctionDecl can only be true after the parentheses of a + // function declaration have been found. + Current.Type = TT_TrailingAnnotation; } } } @@ -1171,11 +1176,13 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, return 150; } - // Breaking before a trailing 'const' or not-function-like annotation is bad. - if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty && - (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next && - Right.Next->isNot(tok::l_paren)))) - return 100; + if (Right.Type == TT_TrailingAnnotation && Right.Next && + Right.Next->isNot(tok::l_paren)) { + // Breaking before a trailing annotation is bad unless it is function-like. + // Use a slightly higher penalty after ")" so that annotations like + // "const override" are kept together. + return Left.is(tok::r_paren) ? 100 : 120; + } // In for-loops, prefer breaking at ',' and ';'. if (Line.First->is(tok::kw_for) && Left.is(tok::equal)) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5e38f5974a..e9b2a9c9ad 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3066,6 +3066,11 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); + verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" + " aaaaaaaaaaa aaaaa) const override;"); + verifyGoogleFormat( + "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" + " const override;"); // Unless this would lead to the first parameter being broken. verifyFormat("void someLongFunction(int someLongParameter)\n"