Add a new option NoSpacesInCStyleCastParentheses to resolve issue raised 
about previous diff.
  Also add a little punctuation in the comments.

Hi djasper,

http://llvm-reviews.chandlerc.com/D881

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D881?vs=2162&id=3554#toc

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
@@ -194,6 +194,20 @@
   /// are not also definitions after the type.
   bool IndentFunctionDeclarationAfterType;

+  /// \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 false, spaces may be inserted into C style casts.
+  bool NoSpacesInCStyleCastParentheses;
+
+  /// \brief If true, spaces will be inserted between 'for'/'if'/'while'/...
+  /// and '('.
+  bool SpaceAfterControlStatementKeyword;
+
   bool operator==(const FormatStyle &R) const {
     return AccessModifierOffset == R.AccessModifierOffset &&
            ConstructorInitializerIndentWidth ==
@@ -231,7 +245,13 @@
            Cpp11BracedListStyle == R.Cpp11BracedListStyle &&
            Standard == R.Standard && UseTab == R.UseTab &&
            IndentFunctionDeclarationAfterType ==
-               R.IndentFunctionDeclarationAfterType;
+               R.IndentFunctionDeclarationAfterType &&
+           SpacesInParentheses == R.SpacesInParentheses &&
+           NoSpaceInEmptyParentheses == R.NoSpaceInEmptyParentheses &&
+           NoSpacesInCStyleCastParentheses ==
+               R.NoSpacesInCStyleCastParentheses &&
+           SpaceAfterControlStatementKeyword ==
+               R.SpaceAfterControlStatementKeyword;
   }
 };

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -137,6 +137,13 @@
     IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
     IO.mapOptional("IndentFunctionDeclarationAfterType",
                    Style.IndentFunctionDeclarationAfterType);
+    IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
+    IO.mapOptional("NoSpaceInEmptyParentheses",
+                   Style.NoSpaceInEmptyParentheses);
+    IO.mapOptional("NoSpacesInCStyleCastParentheses",
+                   Style.NoSpacesInCStyleCastParentheses);
+    IO.mapOptional("SpaceAfterControlStatementKeyword",
+                   Style.SpaceAfterControlStatementKeyword);
   }
 };
 }
@@ -182,6 +189,10 @@
   LLVMStyle.SpacesBeforeTrailingComments = 1;
   LLVMStyle.Standard = FormatStyle::LS_Cpp03;
   LLVMStyle.UseTab = false;
+  LLVMStyle.SpacesInParentheses = false;
+  LLVMStyle.NoSpaceInEmptyParentheses = true;
+  LLVMStyle.NoSpacesInCStyleCastParentheses = true;
+  LLVMStyle.SpaceAfterControlStatementKeyword = true;

   setDefaultPenalties(LLVMStyle);
   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
@@ -219,6 +230,10 @@
   GoogleStyle.SpacesBeforeTrailingComments = 2;
   GoogleStyle.Standard = FormatStyle::LS_Auto;
   GoogleStyle.UseTab = false;
+  GoogleStyle.SpacesInParentheses = false;
+  GoogleStyle.NoSpaceInEmptyParentheses = true;
+  GoogleStyle.NoSpacesInCStyleCastParentheses = true;
+  GoogleStyle.SpaceAfterControlStatementKeyword = true;

   setDefaultPenalties(GoogleStyle);
   GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1117,7 +1117,16 @@
     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 &&
+           !((Right.Type == TT_CastRParen ||
+             (Left.MatchingParen &&
+              Left.MatchingParen->Type == TT_CastRParen)) &&
+             Style.NoSpacesInCStyleCastParentheses);
+  if (Right.isOneOf(tok::semi, tok::comma))
     return false;
   if (Right.is(tok::less) &&
       (Left.is(tok::kw_template) ||
@@ -1165,17 +1174,17 @@
     return Left.Type != TT_ObjCMethodExpr;
   if (Right.is(tok::colon))
     return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
-  if (Left.is(tok::l_paren))
-    return false;
   if (Right.is(tok::l_paren)) {
     if (Left.is(tok::r_paren) && Left.MatchingParen &&
         Left.MatchingParen->Previous &&
         Left.MatchingParen->Previous->is(tok::kw___attribute))
       return true;
     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.Tok.getObjCKeywordID() != tok::objc_not_keyword)
     return false;
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -5455,6 +5455,76 @@
                    "}"));
 }

+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;
+  Spaces.NoSpacesInCStyleCastParentheses = 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);
+
+  // Check NoSpacesInCStyleCastParentheses = false.
+  verifyFormat("Type *A = ( Type * )P;", Spaces);
+  verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
+  verifyFormat("x = ( int32 )y;", Spaces);
+  verifyFormat("int a = ( int )( 2.0f );", Spaces);
+  verifyFormat("#define AA( X ) sizeof( ( ( X * )NULL )->a )", Spaces);
+  verifyFormat("my_int a = ( my_int )sizeof( int );", Spaces);
+  verifyFormat("#define x ( ( int )-1 )", Spaces);
+
+  // Check NoSpacesInCStyleCastParentheses = true.
+  Spaces.NoSpacesInCStyleCastParentheses = true;
+  verifyFormat("Type *A = (Type *)P;", Spaces);
+  verifyFormat("Type *A = (vector<Type *, int *>)P;", Spaces);
+  verifyFormat("x = (int32)y;", Spaces);
+  verifyFormat("int a = (int)( 2.0f );", Spaces);
+  verifyFormat("#define AA( X ) sizeof( ( (X *)NULL )->a )", Spaces);
+  verifyFormat("my_int a = (my_int)sizeof( int );", Spaces);
+  verifyFormat("#define x ( (int)-1 )", Spaces);
+
+  // Check NoSpaceInEmptyParentheses = 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;
@@ -5692,6 +5762,9 @@
   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
   CHECK_PARSE_BOOL(UseTab);
   CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
+  CHECK_PARSE_BOOL(SpacesInParentheses);
+  CHECK_PARSE_BOOL(NoSpaceInEmptyParentheses);
+  CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);

   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to