From 5ad72bb8eb8e5cc4c061ccd28632295213d319db Mon Sep 17 00:00:00 2001 From: Daniel Jasper <djasper@google.com> Date: Wed, 22 May 2013 08:28:26 +0000 Subject: [PATCH] Improve handling of trailing 'const'. Reduce the preference for breaking before a trailing 'const' according to review comments on r182362. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182455 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 13 +++++++++---- unittests/Format/FormatTest.cpp | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index c8cdac9ae32..598eb1ae77b 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 29bb527740d..4ffb0d67eb5 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);"); -- GitLab