From: Daniel Jasper Date: Tue, 5 Aug 2014 12:16:31 +0000 (+0000) Subject: clang-format: Add option to always break after a function's return type. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6bb72d4d138f146b8b44eac02e8bb5457ee3e2f1;p=clang clang-format: Add option to always break after a function's return type. This is required for GNU coding style, among others. Also update the configuration documentation. Modified from an original patch by Jarkko Hietaniemi, thank you! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@214858 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst index 07febaf389..470db9d321 100644 --- a/docs/ClangFormatStyleOptions.rst +++ b/docs/ClangFormatStyleOptions.rst @@ -133,6 +133,13 @@ the configuration (without a prefix: ``Auto``). If ``true``, ``while (true) continue;`` can be put on a single line. +**AlwaysBreakAfterDefinitionReturnType** (``bool``) + If ``true``, always break after function definition return types. + + More truthfully called 'break before the identifier following the type + in a function definition'. PenaltyReturnTypeOnItsOwnLine becomes + irrelevant. + **AlwaysBreakBeforeMultilineStrings** (``bool``) If ``true``, always break before multiline string literals. @@ -158,7 +165,7 @@ the configuration (without a prefix: ``Auto``). Like ``Attach``, but break before braces on function, namespace and class definitions. * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) - Like ``Attach``, but break before function definitions. + Like ``Attach``, but break before function definitions, and 'else'. * ``BS_Allman`` (in configuration: ``Allman``) Always break before braces. * ``BS_GNU`` (in configuration: ``GNU``) @@ -213,7 +220,7 @@ the configuration (without a prefix: ``Auto``). **DerivePointerAlignment** (``bool``) If ``true``, analyze the formatted file for the most common - alignment of & and \*. ``PointerAlignment`` is then used only as fallback. + alignment of & and *. ``PointerAlignment`` is then used only as fallback. **DisableFormat** (``bool``) Disables formatting at all. diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index b21efa132d..8800a208b7 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -235,6 +235,13 @@ struct FormatStyle { /// initializer lists. unsigned ConstructorInitializerIndentWidth; + /// \brief If \c true, always break after function definition return types. + /// + /// More truthfully called 'break before the identifier following the type + /// in a function definition'. PenaltyReturnTypeOnItsOwnLine becomes + /// irrelevant. + bool AlwaysBreakAfterDefinitionReturnType; + /// \brief If \c true, always break after the template<...> of a /// template declaration. bool AlwaysBreakTemplateDeclarations; @@ -370,6 +377,8 @@ struct FormatStyle { AllowShortIfStatementsOnASingleLine == R.AllowShortIfStatementsOnASingleLine && AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine && + AlwaysBreakAfterDefinitionReturnType == + R.AlwaysBreakAfterDefinitionReturnType && AlwaysBreakTemplateDeclarations == R.AlwaysBreakTemplateDeclarations && AlwaysBreakBeforeMultilineStrings == diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 58dd5604e4..8f119d3b65 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -173,6 +173,8 @@ template <> struct MappingTraits { Style.AllowShortLoopsOnASingleLine); IO.mapOptional("AllowShortFunctionsOnASingleLine", Style.AllowShortFunctionsOnASingleLine); + IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", + Style.AlwaysBreakAfterDefinitionReturnType); IO.mapOptional("AlwaysBreakTemplateDeclarations", Style.AlwaysBreakTemplateDeclarations); IO.mapOptional("AlwaysBreakBeforeMultilineStrings", @@ -311,6 +313,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.AllowShortBlocksOnASingleLine = false; LLVMStyle.AllowShortIfStatementsOnASingleLine = false; LLVMStyle.AllowShortLoopsOnASingleLine = false; + LLVMStyle.AlwaysBreakAfterDefinitionReturnType = false; LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; LLVMStyle.AlwaysBreakTemplateDeclarations = false; LLVMStyle.BinPackParameters = true; @@ -443,6 +446,7 @@ FormatStyle getWebKitStyle() { FormatStyle getGNUStyle() { FormatStyle Style = getLLVMStyle(); + Style.AlwaysBreakAfterDefinitionReturnType = true; Style.BreakBeforeBinaryOperators = true; Style.BreakBeforeBraces = FormatStyle::BS_GNU; Style.BreakBeforeTernaryOperators = true; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 0d7e6bfbcb..ed6938fdbc 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1288,6 +1288,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { Current->MustBreakBefore = Current->MustBreakBefore || mustBreakBefore(Line, *Current); + if (Style.AlwaysBreakAfterDefinitionReturnType && + InFunctionDecl && Current->Type == TT_FunctionDeclarationName && + Line.Last->is(tok::l_brace)) // Only for definitions. + Current->MustBreakBefore = true; + Current->CanBreakBefore = Current->MustBreakBefore || canBreakBefore(Line, *Current); unsigned ChildSize = 0; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index e0841caa4c..74cca4fafb 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4147,6 +4147,17 @@ TEST_F(FormatTest, AlignsStringLiterals) { getLLVMStyleWithColumns(25)); } +TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) { + FormatStyle AfterType = getLLVMStyle(); + AfterType.AlwaysBreakAfterDefinitionReturnType = true; + verifyFormat("const char *\n" + "f(void) {\n" // Break here. + " return \"\";\n" + "}\n" + "const char *bar(void);\n", // No break here. + AfterType); +} + TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { FormatStyle NoBreak = getLLVMStyle(); NoBreak.AlwaysBreakBeforeMultilineStrings = false; @@ -8148,6 +8159,7 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); + CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType); CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); CHECK_PARSE_BOOL(BinPackParameters); CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);