[PATCH] Added SpaceAfterFunctionName option

Alexander Kornienko alexfh at google.com
Wed Dec 4 02:16:37 PST 2013


Hi djasper,

Added SpaceAfterFunctionName, which is needed for GNU coding style.

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

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
@@ -260,6 +260,9 @@
   /// and '('.
   bool SpaceAfterControlStatementKeyword;
 
+  /// \brief If \c true, spaces will be inserted between function name and '('.
+  bool SpaceAfterFunctionName;
+
   /// \brief If \c false, spaces will be removed before assignment operators.
   bool SpaceBeforeAssignmentOperators;
 
@@ -317,6 +320,7 @@
            SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
            SpaceAfterControlStatementKeyword ==
                R.SpaceAfterControlStatementKeyword &&
+           SpaceAfterFunctionName == R.SpaceAfterFunctionName &&
            SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
            ContinuationIndentWidth == R.ContinuationIndentWidth;
   }
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -181,6 +181,7 @@
                    Style.SpacesInCStyleCastParentheses);
     IO.mapOptional("SpaceAfterControlStatementKeyword",
                    Style.SpaceAfterControlStatementKeyword);
+    IO.mapOptional("SpaceAfterFunctionName", Style.SpaceAfterFunctionName);
     IO.mapOptional("SpaceBeforeAssignmentOperators",
                    Style.SpaceBeforeAssignmentOperators);
     IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
@@ -267,6 +268,7 @@
   LLVMStyle.SpaceInEmptyParentheses = false;
   LLVMStyle.SpacesInCStyleCastParentheses = false;
   LLVMStyle.SpaceAfterControlStatementKeyword = true;
+  LLVMStyle.SpaceAfterFunctionName = false;
   LLVMStyle.SpaceBeforeAssignmentOperators = true;
   LLVMStyle.ContinuationIndentWidth = 4;
   LLVMStyle.SpacesInAngles = false;
@@ -316,6 +318,7 @@
   GoogleStyle.SpaceInEmptyParentheses = false;
   GoogleStyle.SpacesInCStyleCastParentheses = false;
   GoogleStyle.SpaceAfterControlStatementKeyword = true;
+  GoogleStyle.SpaceAfterFunctionName = false;
   GoogleStyle.SpaceBeforeAssignmentOperators = true;
   GoogleStyle.ContinuationIndentWidth = 4;
   GoogleStyle.SpacesInAngles = false;
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1295,7 +1295,8 @@
                         tok::semi) ||
            (Style.SpaceAfterControlStatementKeyword &&
             Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
-                         tok::kw_catch));
+                         tok::kw_catch)) ||
+           (Style.SpaceAfterFunctionName && Left.is(tok::identifier));
   }
   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
@@ -6637,6 +6637,33 @@
                "}", NoSpace);
 }
 
+TEST_F(FormatTest, ConfigurableSpaceAfterFunctionName) {
+  FormatStyle Space = getLLVMStyle();
+  Space.SpaceAfterControlStatementKeyword = false;
+  Space.SpaceAfterFunctionName = true;
+
+  verifyFormat("int f ();", Space);
+  verifyFormat("void f (int a, T b) {\n"
+               "  while(true)\n"
+               "    continue;\n"
+               "}",
+               Space);
+  verifyFormat("if(true)\n"
+               "  f ();\n"
+               "else if(true)\n"
+               "  f ();",
+               Space);
+  verifyFormat("do {\n"
+               "  do_something ();\n"
+               "} while(something ());",
+               Space);
+  verifyFormat("switch(x) {\n"
+               "default:\n"
+               "  break;\n"
+               "}",
+               Space);
+}
+
 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
   FormatStyle Spaces = getLLVMStyle();
 
@@ -6945,6 +6972,7 @@
   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
+  CHECK_PARSE_BOOL(SpaceAfterFunctionName);
   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
 
   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2326.1.patch
Type: text/x-patch
Size: 4360 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131204/bad3bf32/attachment.bin>


More information about the cfe-commits mailing list