From: Daniel Jasper Date: Wed, 29 May 2013 12:07:31 +0000 (+0000) Subject: Add option to always break template declarations. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bbc8776493fc0176d925a5b528e61ee400895047;p=clang Add option to always break template declarations. With option enabled (e.g. in Google-style): template void f() {} With option disabled: template void f() {} Enabling this for Google-style and Chromium-style, not sure which other styles would prefer that. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182849 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index d644fbdfd1..73258cd64b 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -100,6 +100,10 @@ struct FormatStyle { /// \brief The number of characters to use for indentation. unsigned IndentWidth; + /// \brief If \c true, always break after the \c template<...> of a template + /// declaration. + bool AlwaysBreakTemplateDeclarations; + /// \brief If true, \c IndentWidth consecutive spaces will be replaced with /// tab characters. bool UseTab; @@ -128,6 +132,8 @@ struct FormatStyle { R.AllowAllParametersOfDeclarationOnNextLine && AllowShortIfStatementsOnASingleLine == R.AllowShortIfStatementsOnASingleLine && + AlwaysBreakTemplateDeclarations == + R.AlwaysBreakTemplateDeclarations && BinPackParameters == R.BinPackParameters && BreakBeforeBraces == R.BreakBeforeBraces && ColumnLimit == R.ColumnLimit && @@ -143,8 +149,7 @@ struct FormatStyle { PointerBindsToType == R.PointerBindsToType && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && SpacesInBracedLists == R.SpacesInBracedLists && - Standard == R.Standard && - UseTab == R.UseTab; + Standard == R.Standard && UseTab == R.UseTab; } }; diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 5e74ed188d..797410ab11 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -85,6 +85,8 @@ template <> struct MappingTraits { Style.AllowShortIfStatementsOnASingleLine); IO.mapOptional("AllowShortLoopsOnASingleLine", Style.AllowShortLoopsOnASingleLine); + IO.mapOptional("AlwaysBreakTemplateDeclarations", + Style.AlwaysBreakTemplateDeclarations); IO.mapOptional("BinPackParameters", Style.BinPackParameters); IO.mapOptional("ColumnLimit", Style.ColumnLimit); IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", @@ -120,6 +122,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowShortIfStatementsOnASingleLine = false; LLVMStyle.AllowShortLoopsOnASingleLine = false; + LLVMStyle.AlwaysBreakTemplateDeclarations = false; LLVMStyle.BinPackParameters = true; LLVMStyle.ColumnLimit = 80; LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; @@ -146,6 +149,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true; GoogleStyle.AllowShortIfStatementsOnASingleLine = true; GoogleStyle.AllowShortLoopsOnASingleLine = true; + GoogleStyle.AlwaysBreakTemplateDeclarations = true; GoogleStyle.BinPackParameters = true; GoogleStyle.ColumnLimit = 80; GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 5a6d2ddef9..ebbafcd381 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -922,6 +922,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { Current->Parent->is(tok::string_literal) && Current->Children[0].is(tok::string_literal)) { Current->MustBreakBefore = true; + } else if (Current->Parent->ClosesTemplateDeclaration && + Style.AlwaysBreakTemplateDeclarations) { + Current->MustBreakBefore = true; } else { Current->MustBreakBefore = false; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 44873f75df..0d3f2807a9 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2774,6 +2774,16 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { verifyFormat("a(\n" " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); + + verifyFormat("template class C {\n};"); + verifyFormat("template void f();"); + verifyFormat("template void f() {}"); + + FormatStyle AlwaysBreak = getLLVMStyle(); + AlwaysBreak.AlwaysBreakTemplateDeclarations = true; + verifyFormat("template \nclass C {\n};", AlwaysBreak); + verifyFormat("template \nvoid f();", AlwaysBreak); + verifyFormat("template \nvoid f() {}", AlwaysBreak); } TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { @@ -4646,6 +4656,7 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); + CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); CHECK_PARSE_BOOL(BinPackParameters); CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); CHECK_PARSE_BOOL(DerivePointerBinding);