From: Daniel Jasper Date: Mon, 27 Oct 2014 17:13:59 +0000 (+0000) Subject: clang-format: Don't break after very short return types. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7cb454ac1aaf72e28a53aece6d98922712c068bb;p=clang clang-format: Don't break after very short return types. Before: void SomeFunction(int parameter); After: void SomeFunction( int parameter); (Unless AlwaysBreakAfterDefinitionReturnType after type is set). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220686 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 82dffd6fff..a6fb40ece7 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -122,6 +122,12 @@ bool ContinuationIndenter::canBreak(const LineState &State) { State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks) return false; + // Don't break after very short return types (e.g. "void") as that is often + // unexpected. + if (Current.Type == TT_FunctionDeclarationName && + !Style.AlwaysBreakAfterDefinitionReturnType && State.Column < 6) + return false; + return !State.Stack.back().NoLineBreak; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index f29aff8f0e..56163f7007 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1207,8 +1207,8 @@ TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { } TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { - EXPECT_EQ("void\n" - "ffffffffff(int aaaaa /* test */);", + EXPECT_EQ("void ffffffffff(\n" + " int aaaaa /* test */);", format("void ffffffffff(int aaaaa /* test */);", getLLVMStyleWithColumns(35))); } @@ -1258,11 +1258,11 @@ TEST_F(FormatTest, SplitsLongCxxComments) { format("// A comment before a macro definition\n" "#define a b", getLLVMStyleWithColumns(20))); - EXPECT_EQ("void\n" - "ffffff(int aaaaaaaaa, // wwww\n" - " int bbbbbbbbbb, // xxxxxxx\n" - " // yyyyyyyyyy\n" - " int c, int d, int e) {}", + EXPECT_EQ("void ffffff(\n" + " int aaaaaaaaa, // wwww\n" + " int bbbbbbbbbb, // xxxxxxx\n" + " // yyyyyyyyyy\n" + " int c, int d, int e) {}", format("void ffffff(\n" " int aaaaaaaaa, // wwww\n" " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" @@ -3538,8 +3538,8 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { // they are not function-like. FormatStyle Style = getGoogleStyle(); Style.ColumnLimit = 47; - verifyFormat("void\n" - "someLongFunction(int someLongParameter) const {\n}", + verifyFormat("void someLongFunction(\n" + " int someLoooooooooooooongParameter) const {\n}", getLLVMStyleWithColumns(47)); verifyFormat("LoooooongReturnType\n" "someLoooooooongFunction() const {}", @@ -7400,7 +7400,7 @@ TEST_F(FormatTest, ConfigurableIndentWidth) { } TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { - verifyFormat("void\n" + verifyFormat("double\n" "f();", getLLVMStyleWithColumns(8)); }