[PATCH] Added some options related to inserting spaces around parentheses
Joe Hermaszewski
expipiplus1 at gmail.com
Tue May 28 07:28:48 PDT 2013
Hi djasper,
Three new options:
SpaceAfterControlStatementKeyword:
If this is set, a space will be inserted between control statement keywords ('if', 'while', etc..) and the opening l_paren. This is true by default in all the built in configurations.
SpacesInParentheses:
If this is set a space will be inserted after every l_paren and before every r_paren. This is false by default in all the built in configurations.
NoSpaceInEmptyParentheses:
If this is set, it stops SpacesInParentheses from inserting a space between a l_paren and an r_paren. This is (redundantly) true by default in the built in configurations.
http://llvm-reviews.chandlerc.com/D881
Files:
include/clang/Format/Format.h
lib/Format/Format.cpp
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -123,6 +123,17 @@
/// \brief If \c true, format { 1 }, otherwise {1}.
bool SpacesInBracedLists;
+ /// \brief If true, spaces will be inserted after every '(' and before every
+ /// ')'
+ bool SpacesInParentheses;
+
+ /// \brief If false, spaces may be inserted into '()'
+ bool NoSpaceInEmptyParentheses;
+
+ /// \brief If true, spaces will be inserted between 'for'/'if'/'while'/...
+ /// and '('
+ bool SpaceAfterControlStatementKeyword;
+
bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -106,6 +106,11 @@
IO.mapOptional("IndentWidth", Style.IndentWidth);
IO.mapOptional("UseTab", Style.UseTab);
IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
+ IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
+ IO.mapOptional("NoSpaceInEmptyParentheses",
+ Style.NoSpaceInEmptyParentheses);
+ IO.mapOptional("SpaceAfterControlStatementKeyword",
+ Style.SpaceAfterControlStatementKeyword);
}
};
}
@@ -137,6 +142,9 @@
LLVMStyle.IndentWidth = 2;
LLVMStyle.UseTab = false;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
+ LLVMStyle.SpacesInParentheses = false;
+ LLVMStyle.NoSpaceInEmptyParentheses = true;
+ LLVMStyle.SpaceAfterControlStatementKeyword = true;
return LLVMStyle;
}
@@ -163,6 +171,9 @@
GoogleStyle.IndentWidth = 2;
GoogleStyle.UseTab = false;
GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
+ GoogleStyle.SpacesInParentheses = false;
+ GoogleStyle.NoSpaceInEmptyParentheses = true;
+ GoogleStyle.SpaceAfterControlStatementKeyword = true;
return GoogleStyle;
}
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1043,7 +1043,12 @@
return Left.is(tok::hash);
if (Left.isOneOf(tok::hashhash, tok::hash))
return Right.is(tok::hash);
- if (Right.isOneOf(tok::r_paren, tok::semi, tok::comma))
+ if (Left.is(tok::l_paren) && Right.is(tok::r_paren) &&
+ Style.NoSpaceInEmptyParentheses)
+ return false;
+ if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
+ return Style.SpacesInParentheses;
+ if (Right.isOneOf(tok::semi, tok::comma))
return false;
if (Right.is(tok::less) &&
(Left.is(tok::kw_template) ||
@@ -1091,13 +1096,13 @@
return Left.Type != TT_ObjCMethodExpr;
if (Right.is(tok::colon))
return Right.Type != TT_ObjCMethodExpr;
- if (Left.is(tok::l_paren))
- return false;
if (Right.is(tok::l_paren)) {
return Line.Type == LT_ObjCDecl ||
- Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
- tok::kw_return, tok::kw_catch, tok::kw_new,
- tok::kw_delete, tok::semi);
+ Left.isOneOf(tok::kw_return, tok::kw_new,
+ tok::kw_delete, tok::semi) ||
+ (Style.SpaceAfterControlStatementKeyword &&
+ Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
+ tok::kw_catch));
}
if (Left.is(tok::at) &&
Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -4517,6 +4517,56 @@
// Tab));
}
+TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) {
+ FormatStyle NoSpace = getLLVMStyle();
+ NoSpace.SpaceAfterControlStatementKeyword = false;
+
+ verifyFormat("while(true)\n"
+ " continue;", NoSpace);
+ verifyFormat("for(;;)\n"
+ " continue;", NoSpace);
+ verifyFormat("if(true)\n"
+ " f();\n"
+ "else if(true)\n"
+ " f();", NoSpace);
+ verifyFormat("do {\n"
+ " do_something();\n"
+ "} while(something());", NoSpace);
+ verifyFormat("switch(x) {\n"
+ "default:\n"
+ " break;\n"
+ "}", NoSpace);
+}
+
+TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
+ FormatStyle Spaces = getLLVMStyle();
+ Spaces.SpacesInParentheses = true;
+ Spaces.NoSpaceInEmptyParentheses = false;
+
+ verifyFormat("call( x, y, z );", Spaces);
+ verifyFormat("while ( true )\n"
+ " continue;", Spaces);
+ verifyFormat("for ( ;; )\n"
+ " continue;", Spaces);
+ verifyFormat("if ( true )\n"
+ " f( );\n"
+ "else if ( true )\n"
+ " f( );", Spaces);
+ verifyFormat("do {\n"
+ " do_something( );\n"
+ "} while ( something( ) );", Spaces);
+ verifyFormat("switch ( x ) {\n"
+ "default:\n"
+ " break;\n"
+ "}", Spaces);
+
+ Spaces.SpacesInParentheses = true;
+ Spaces.NoSpaceInEmptyParentheses = true;
+ verifyFormat("call( x, y, z );", Spaces);
+ verifyFormat("call()", Spaces);
+
+}
+
TEST_F(FormatTest, LinuxBraceBreaking) {
FormatStyle BreakBeforeBrace = getLLVMStyle();
BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
@@ -4644,6 +4694,9 @@
CHECK_PARSE_BOOL(PointerBindsToType);
CHECK_PARSE_BOOL(SpacesInBracedLists);
CHECK_PARSE_BOOL(UseTab);
+ CHECK_PARSE_BOOL(SpacesInParentheses);
+ CHECK_PARSE_BOOL(NoSpaceInEmptyParentheses);
+ CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D881.1.patch
Type: text/x-patch
Size: 6056 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130528/08927829/attachment.bin>
More information about the cfe-commits
mailing list