From e068ac77a223588faf7d80b36e516a656d9b52f6 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 27 Oct 2014 17:13:59 +0000 Subject: [PATCH] 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). llvm-svn: 220686 --- clang/lib/Format/ContinuationIndenter.cpp | 6 ++++++ clang/unittests/Format/FormatTest.cpp | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 82dffd6fff58..a6fb40ece7a9 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/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/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index f29aff8f0e94..56163f70070f 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/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)); }