diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index c8cdac9ae32d61c47d524c3f26f3ebb76380e4c6..598eb1ae77b77d240bd14a6df19bbf223fb4a36b 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -959,6 +959,10 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, return 150; } + // Breaking before a trailing 'const' is bad. + if (Left.is(tok::r_paren) && Right.is(tok::kw_const)) + return 150; + // In for-loops, prefer breaking at ',' and ';'. if (Line.First.is(tok::kw_for) && Left.is(tok::equal)) return 4; @@ -1165,6 +1169,11 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, // change the "binding" behavior of a comment. return false; + // We only break before r_brace if there was a corresponding break before + // the l_brace, which is tracked by BreakBeforeClosingBrace. + if (Right.isOneOf(tok::r_brace, tok::r_paren, tok::greater)) + return false; + // Allow breaking after a trailing 'const', e.g. after a method declaration, // unless it is follow by ';', '{' or '='. if (Left.is(tok::kw_const) && Left.Parent != NULL && @@ -1174,10 +1183,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, if (Right.is(tok::kw___attribute)) return true; - // We only break before r_brace if there was a corresponding break before - // the l_brace, which is tracked by BreakBeforeClosingBrace. - if (Right.isOneOf(tok::r_brace, tok::r_paren, tok::greater)) - return false; if (Left.is(tok::identifier) && Right.is(tok::string_literal)) return true; return (Left.isBinaryOperator() && Left.isNot(tok::lessless)) || diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 29bb527740d6951d92a2eb3aa0e6a3a0d3203f28..4ffb0d67eb56037a588eeda009e5533d2b8e7566 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1929,9 +1929,23 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { } TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { - verifyFormat("void someLongFunction(int someLongParameter)\n" - " const;", - getLLVMStyleWithColumns(45)); + verifyFormat("void someLongFunction(\n" + " int someLongParameter) const {}", + getLLVMStyleWithColumns(46)); + FormatStyle Style = getGoogleStyle(); + Style.ColumnLimit = 47; + verifyFormat("void\n" + "someLongFunction(int someLongParameter) const {\n}", + getLLVMStyleWithColumns(47)); + verifyFormat("void someLongFunction(\n" + " int someLongParameter) const {}", + Style); + verifyFormat("LoooooongReturnType\n" + "someLoooooooongFunction() const {}", + getLLVMStyleWithColumns(47)); + verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" + " const {}", + Style); verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");