r184563 - Add an option to not indent declarations when breaking after the type.
Manuel Klimek
klimek at google.com
Fri Jun 21 10:25:42 PDT 2013
Author: klimek
Date: Fri Jun 21 12:25:42 2013
New Revision: 184563
URL: http://llvm.org/viewvc/llvm-project?rev=184563&view=rev
Log:
Add an option to not indent declarations when breaking after the type.
Make that option the default for LLVM style.
Modified:
cfe/trunk/include/clang/Format/Format.h
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=184563&r1=184562&r2=184563&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Fri Jun 21 12:25:42 2013
@@ -131,6 +131,10 @@ struct FormatStyle {
/// \brief If \c true, format { 1 }, otherwise {1}.
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 {
return AccessModifierOffset == R.AccessModifierOffset &&
AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
@@ -157,9 +161,10 @@ struct FormatStyle {
PointerBindsToType == R.PointerBindsToType &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
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:
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=184563&r1=184562&r2=184563&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Jun 21 12:25:42 2013
@@ -109,6 +109,8 @@ template <> struct MappingTraits<clang::
IO.mapOptional("IndentWidth", Style.IndentWidth);
IO.mapOptional("UseTab", Style.UseTab);
IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
+ IO.mapOptional("IndentFunctionDeclarationAfterType",
+ Style.IndentFunctionDeclarationAfterType);
}
};
}
@@ -143,6 +145,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.IndentWidth = 2;
LLVMStyle.UseTab = false;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
+ LLVMStyle.IndentFunctionDeclarationAfterType = false;
return LLVMStyle;
}
@@ -172,6 +175,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.IndentWidth = 2;
GoogleStyle.UseTab = false;
GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
+ GoogleStyle.IndentFunctionDeclarationAfterType = true;
return GoogleStyle;
}
@@ -524,7 +528,8 @@ private:
State.Column = State.Stack.back().VariablePos;
} else if (Previous.ClosesTemplateDeclaration ||
(Current.Type == TT_StartOfName && State.ParenLevel == 0 &&
- Line.StartsDefinition)) {
+ (!Style.IndentFunctionDeclarationAfterType ||
+ Line.StartsDefinition))) {
State.Column = State.Stack.back().Indent;
} else if (Current.Type == TT_ObjCSelectorName) {
if (State.Stack.back().ColonPos > Current.CodePointCount) {
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=184563&r1=184562&r2=184563&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Jun 21 12:25:42 2013
@@ -2008,7 +2008,7 @@ TEST_F(FormatTest, LayoutStatementsAroun
EXPECT_EQ("int\n"
"#define A\n"
" a;",
- format("int\n#define A\na;"));
+ format("int\n#define A\na;", getGoogleStyle()));
verifyFormat("functionCallTo(\n"
" someOtherFunction(\n"
" withSomeParameters, whichInSequence,\n"
@@ -2337,19 +2337,22 @@ TEST_F(FormatTest, BreaksFunctionDeclara
// 2) break after return type.
verifyFormat(
- "Aaaaaaaaaaaaaaaaaaaaaaaa\n"
- " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
+ "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
+ getGoogleStyle());
// 3) break after (.
verifyFormat(
"Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
- " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
+ " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
+ getGoogleStyle());
// 4) break before after nested name specifiers.
verifyFormat(
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" SomeClasssssssssssssssssssssssssssssssssssssss::\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
+ getGoogleStyle());
// However, there are exceptions, if a sufficient amount of lines can be
// saved.
@@ -2361,10 +2364,11 @@ TEST_F(FormatTest, BreaksFunctionDeclara
" Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc);");
verifyFormat(
- "Aaaaaaaaaaaaaaaaaa\n"
+ "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
- " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
+ " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
+ getGoogleStyle());
verifyFormat(
"Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
" Cccccccccccccc cccccccccc,\n"
@@ -2419,11 +2423,11 @@ TEST_F(FormatTest, BreaksFunctionDeclara
verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" __attribute__((unused));");
verifyFormat(
- "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
- " GUARDED_BY(aaaaaaaaaaaa);");
+ "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+ " GUARDED_BY(aaaaaaaaaaaa);",
+ getGoogleStyle());
}
-
TEST_F(FormatTest, BreaksDesireably) {
verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
" aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
@@ -2745,9 +2749,10 @@ TEST_F(FormatTest, DeclarationsOfMultipl
" ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
// 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.
- verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
+ verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
- " *b = bbbbbbbbbbbbbbbbbbb;");
+ " *b = bbbbbbbbbbbbbbbbbbb;",
+ getGoogleStyle());
// Not ideal, but pointer-with-type does not allow much here.
verifyGoogleFormat(
@@ -3400,17 +3405,21 @@ TEST_F(FormatTest, FormatsFunctionTypes)
TEST_F(FormatTest, BreaksLongDeclarations) {
verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
- " AnotherNameForTheLongType;");
+ " AnotherNameForTheLongType;",
+ getGoogleStyle());
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
- " LoooooooooooooooooooooooooooooooooooooooongVariable;");
+ " LoooooooooooooooooooooooooooooooooooooooongVariable;",
+ getGoogleStyle());
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
- " LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
+ " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
+ getGoogleStyle());
verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
"LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
// FIXME: Without the comment, this breaks after "(".
- verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
- " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
+ verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
+ " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
+ getGoogleStyle());
verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
" int LoooooooooooooooooooongParam2) {}");
@@ -3431,7 +3440,8 @@ TEST_F(FormatTest, BreaksLongDeclaration
"Function() {}");
verifyFormat(
"aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
- " aaaaaaaaaaaaaaaaaaaaaaa;");
+ " aaaaaaaaaaaaaaaaaaaaaaa;",
+ getGoogleStyle());
verifyGoogleFormat(
"TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
@@ -4808,6 +4818,12 @@ TEST_F(FormatTest, ConfigurableIndentWid
EightIndent);
}
+TEST_F(FormatTest, ConfigureableFunctionDeclarationIndentAfterType) {
+ verifyFormat("void\n"
+ "f();",
+ getLLVMStyleWithColumns(8));
+}
+
TEST_F(FormatTest, ConfigurableUseOfTab) {
FormatStyle Tab = getLLVMStyleWithColumns(42);
Tab.IndentWidth = 8;
@@ -4975,6 +4991,7 @@ TEST_F(FormatTest, ParsesConfiguration)
CHECK_PARSE_BOOL(PointerBindsToType);
CHECK_PARSE_BOOL(SpacesInBracedLists);
CHECK_PARSE_BOOL(UseTab);
+ CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
More information about the cfe-commits
mailing list