Add an option to not indent declarations when breaking after the type.

Make that option the default for LLVM style.

llvm-svn: 184563
This commit is contained in:
Manuel Klimek 2013-06-21 17:25:42 +00:00
parent 72a54eab35
commit 836c2868f9
3 changed files with 48 additions and 21 deletions

View File

@ -131,6 +131,10 @@ struct FormatStyle {
/// \brief If \c true, format { 1 }, otherwise {1}. /// \brief If \c true, format { 1 }, otherwise {1}.
bool SpacesInBracedLists; bool SpacesInBracedLists;
/// \brief If \c true, indent when breaking function declarations which
/// are not also definitions after the type.
bool IndentFunctionDeclarationAfterType;
bool operator==(const FormatStyle &R) const { bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset && return AccessModifierOffset == R.AccessModifierOffset &&
AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft && AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
@ -157,9 +161,10 @@ struct FormatStyle {
PointerBindsToType == R.PointerBindsToType && PointerBindsToType == R.PointerBindsToType &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
SpacesInBracedLists == R.SpacesInBracedLists && SpacesInBracedLists == R.SpacesInBracedLists &&
Standard == R.Standard && UseTab == R.UseTab; Standard == R.Standard && UseTab == R.UseTab &&
IndentFunctionDeclarationAfterType ==
R.IndentFunctionDeclarationAfterType;
} }
}; };
/// \brief Returns a format style complying with the LLVM coding standards: /// \brief Returns a format style complying with the LLVM coding standards:

View File

@ -109,6 +109,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
IO.mapOptional("IndentWidth", Style.IndentWidth); IO.mapOptional("IndentWidth", Style.IndentWidth);
IO.mapOptional("UseTab", Style.UseTab); IO.mapOptional("UseTab", Style.UseTab);
IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
IO.mapOptional("IndentFunctionDeclarationAfterType",
Style.IndentFunctionDeclarationAfterType);
} }
}; };
} }
@ -143,6 +145,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.IndentWidth = 2; LLVMStyle.IndentWidth = 2;
LLVMStyle.UseTab = false; LLVMStyle.UseTab = false;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
LLVMStyle.IndentFunctionDeclarationAfterType = false;
return LLVMStyle; return LLVMStyle;
} }
@ -172,6 +175,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.IndentWidth = 2; GoogleStyle.IndentWidth = 2;
GoogleStyle.UseTab = false; GoogleStyle.UseTab = false;
GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
GoogleStyle.IndentFunctionDeclarationAfterType = true;
return GoogleStyle; return GoogleStyle;
} }
@ -524,7 +528,8 @@ private:
State.Column = State.Stack.back().VariablePos; State.Column = State.Stack.back().VariablePos;
} else if (Previous.ClosesTemplateDeclaration || } else if (Previous.ClosesTemplateDeclaration ||
(Current.Type == TT_StartOfName && State.ParenLevel == 0 && (Current.Type == TT_StartOfName && State.ParenLevel == 0 &&
Line.StartsDefinition)) { (!Style.IndentFunctionDeclarationAfterType ||
Line.StartsDefinition))) {
State.Column = State.Stack.back().Indent; State.Column = State.Stack.back().Indent;
} else if (Current.Type == TT_ObjCSelectorName) { } else if (Current.Type == TT_ObjCSelectorName) {
if (State.Stack.back().ColonPos > Current.CodePointCount) { if (State.Stack.back().ColonPos > Current.CodePointCount) {

View File

@ -2008,7 +2008,7 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
EXPECT_EQ("int\n" EXPECT_EQ("int\n"
"#define A\n" "#define A\n"
" a;", " a;",
format("int\n#define A\na;")); format("int\n#define A\na;", getGoogleStyle()));
verifyFormat("functionCallTo(\n" verifyFormat("functionCallTo(\n"
" someOtherFunction(\n" " someOtherFunction(\n"
" withSomeParameters, whichInSequence,\n" " withSomeParameters, whichInSequence,\n"
@ -2337,19 +2337,22 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
// 2) break after return type. // 2) break after return type.
verifyFormat( verifyFormat(
"Aaaaaaaaaaaaaaaaaaaaaaaa\n" "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
getGoogleStyle());
// 3) break after (. // 3) break after (.
verifyFormat( verifyFormat(
"Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
" Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
getGoogleStyle());
// 4) break before after nested name specifiers. // 4) break before after nested name specifiers.
verifyFormat( verifyFormat(
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" SomeClasssssssssssssssssssssssssssssssssssssss::\n" " SomeClasssssssssssssssssssssssssssssssssssssss::\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
getGoogleStyle());
// However, there are exceptions, if a sufficient amount of lines can be // However, there are exceptions, if a sufficient amount of lines can be
// saved. // saved.
@ -2361,10 +2364,11 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
" Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc);"); " Cccccccccccccc cccccccccc);");
verifyFormat( verifyFormat(
"Aaaaaaaaaaaaaaaaaa\n" "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
getGoogleStyle());
verifyFormat( verifyFormat(
"Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc,\n"
@ -2419,11 +2423,11 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" __attribute__((unused));"); " __attribute__((unused));");
verifyFormat( verifyFormat(
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" GUARDED_BY(aaaaaaaaaaaa);"); " GUARDED_BY(aaaaaaaaaaaa);",
getGoogleStyle());
} }
TEST_F(FormatTest, BreaksDesireably) { TEST_F(FormatTest, BreaksDesireably) {
verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
" aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
@ -2745,9 +2749,10 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
" ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
// FIXME: If multiple variables are defined, the "*" needs to move to the new // FIXME: If multiple variables are defined, the "*" needs to move to the new
// line. Also fix indent for breaking after the type, this looks bad. // line. Also fix indent for breaking after the type, this looks bad.
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
" *b = bbbbbbbbbbbbbbbbbbb;"); " *b = bbbbbbbbbbbbbbbbbbb;",
getGoogleStyle());
// Not ideal, but pointer-with-type does not allow much here. // Not ideal, but pointer-with-type does not allow much here.
verifyGoogleFormat( verifyGoogleFormat(
@ -3400,17 +3405,21 @@ TEST_F(FormatTest, FormatsFunctionTypes) {
TEST_F(FormatTest, BreaksLongDeclarations) { TEST_F(FormatTest, BreaksLongDeclarations) {
verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
" AnotherNameForTheLongType;"); " AnotherNameForTheLongType;",
getGoogleStyle());
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
" LoooooooooooooooooooooooooooooooooooooooongVariable;"); " LoooooooooooooooooooooooooooooooooooooooongVariable;",
getGoogleStyle());
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
" LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
getGoogleStyle());
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
"LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
// FIXME: Without the comment, this breaks after "(". // FIXME: Without the comment, this breaks after "(".
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
" (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
getGoogleStyle());
verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
" int LoooooooooooooooooooongParam2) {}"); " int LoooooooooooooooooooongParam2) {}");
@ -3431,7 +3440,8 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
"Function() {}"); "Function() {}");
verifyFormat( verifyFormat(
"aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
" aaaaaaaaaaaaaaaaaaaaaaa;"); " aaaaaaaaaaaaaaaaaaaaaaa;",
getGoogleStyle());
verifyGoogleFormat( verifyGoogleFormat(
"TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
@ -4808,6 +4818,12 @@ TEST_F(FormatTest, ConfigurableIndentWidth) {
EightIndent); EightIndent);
} }
TEST_F(FormatTest, ConfigureableFunctionDeclarationIndentAfterType) {
verifyFormat("void\n"
"f();",
getLLVMStyleWithColumns(8));
}
TEST_F(FormatTest, ConfigurableUseOfTab) { TEST_F(FormatTest, ConfigurableUseOfTab) {
FormatStyle Tab = getLLVMStyleWithColumns(42); FormatStyle Tab = getLLVMStyleWithColumns(42);
Tab.IndentWidth = 8; Tab.IndentWidth = 8;
@ -4975,6 +4991,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE_BOOL(PointerBindsToType); CHECK_PARSE_BOOL(PointerBindsToType);
CHECK_PARSE_BOOL(SpacesInBracedLists); CHECK_PARSE_BOOL(SpacesInBracedLists);
CHECK_PARSE_BOOL(UseTab); CHECK_PARSE_BOOL(UseTab);
CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);