diff options
| author | Daniel Jasper <djasper@google.com> | 2013-12-16 15:01:54 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2013-12-16 15:01:54 +0000 |
| commit | 43e6a28d1ff3b877c788d35a112aa0a4cc9d8ba4 (patch) | |
| tree | fa0fb3266950b39c3492a56cc9ae8dcfc1341e25 | |
| parent | cb98c5f6f0f04ff121e8e76a22d41d1c406bdf9f (diff) | |
| download | bcm5719-llvm-43e6a28d1ff3b877c788d35a112aa0a4cc9d8ba4.tar.gz bcm5719-llvm-43e6a28d1ff3b877c788d35a112aa0a4cc9d8ba4.zip | |
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;
llvm-svn: 197391
| -rw-r--r-- | clang/lib/Format/FormatToken.h | 1 | ||||
| -rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 19 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 5 |
3 files changed, 19 insertions, 6 deletions
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index fa676b39736..60265509db9 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/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/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e2615dbb6a2..db071f101e4 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/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/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 5e38f5974a7..e9b2a9c9add 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/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" |

