]> granicus.if.org Git - clang/commitdiff
clang-format: Option to removing the space before assignment operators.
authorDaniel Jasper <djasper@google.com>
Wed, 25 Sep 2013 15:15:02 +0000 (15:15 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 25 Sep 2013 15:15:02 +0000 (15:15 +0000)
Patch contributed by Aaron Wishnick. Thank you!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191375 91177308-0d34-0410-b5e6-96231b3b80d8

docs/ClangFormatStyleOptions.rst
include/clang/Format/Format.h
lib/Format/Format.cpp
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 69b6cee9aba9e72925f9994114509a23a8dbf516..0ce4eb7f15bb584061a3fa2f0367f36ad6f1e872 100644 (file)
@@ -249,6 +249,9 @@ the configuration (without a prefix: ``Auto``).
   If ``true``, spaces will be inserted between 'for'/'if'/'while'/...
   and '('.
 
+**SpaceBeforeAssignmentOperators** (``bool``)
+  If ``false``, spaces will be removed before '=', '+=', etc.
+
 **SpaceInEmptyParentheses** (``bool``)
   If ``false``, spaces may be inserted into '()'.
 
index e5785b8779622ebd4e74b03d823028d2ddce729d..1feddf15d64672ba37099cd5056d57d65b41c1fd 100644 (file)
@@ -222,6 +222,9 @@ struct FormatStyle {
   /// and '('.
   bool SpaceAfterControlStatementKeyword;
 
+  /// \brief If \c false, spaces will be removed before assignment operators.
+  bool SpaceBeforeAssignmentOperators;
+
   bool operator==(const FormatStyle &R) const {
     return AccessModifierOffset == R.AccessModifierOffset &&
            ConstructorInitializerIndentWidth ==
@@ -268,7 +271,8 @@ struct FormatStyle {
            SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
            SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
            SpaceAfterControlStatementKeyword ==
-               R.SpaceAfterControlStatementKeyword;
+               R.SpaceAfterControlStatementKeyword &&
+           SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators;
   }
 };
 
index 5271a62e8fbefd44521a4ff397817dafee99edd4..6166ecc1b37326e310ba2a1ddb6e47dc1f04abf1 100644 (file)
@@ -147,6 +147,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
                    Style.SpacesInCStyleCastParentheses);
     IO.mapOptional("SpaceAfterControlStatementKeyword",
                    Style.SpaceAfterControlStatementKeyword);
+    IO.mapOptional("SpaceBeforeAssignmentOperators",
+                   Style.SpaceBeforeAssignmentOperators);
   }
 };
 }
@@ -197,6 +199,7 @@ FormatStyle getLLVMStyle() {
   LLVMStyle.SpaceInEmptyParentheses = false;
   LLVMStyle.SpacesInCStyleCastParentheses = false;
   LLVMStyle.SpaceAfterControlStatementKeyword = true;
+  LLVMStyle.SpaceBeforeAssignmentOperators = true;
 
   setDefaultPenalties(LLVMStyle);
   LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
@@ -239,6 +242,7 @@ FormatStyle getGoogleStyle() {
   GoogleStyle.SpaceInEmptyParentheses = false;
   GoogleStyle.SpacesInCStyleCastParentheses = false;
   GoogleStyle.SpaceAfterControlStatementKeyword = true;
+  GoogleStyle.SpaceBeforeAssignmentOperators = true;
 
   setDefaultPenalties(GoogleStyle);
   GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
index 6386bc16dd318ec62cb4e8ca7da4bd3f62a511b8..e1e4f79d1773574bbf4abbe4e4be96038bf66c3a 100644 (file)
@@ -1318,6 +1318,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
   if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
       Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
     return false;
+  if (!Style.SpaceBeforeAssignmentOperators &&
+      Tok.getPrecedence() == prec::Assignment)
+    return false;
   if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
       Tok.Previous->Type == TT_BinaryOperator)
     return true;
index a1136825b1bb31cf8c4c68a6f931bc7f16cf9361..0175413a50c4366f33885a356b832eac0d538946 100644 (file)
@@ -5996,6 +5996,18 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
                "}", Spaces);
 }
 
+TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
+  verifyFormat("int a = 5;");
+  verifyFormat("a += 42;");
+  verifyFormat("a or_eq 8;");
+
+  FormatStyle Spaces = getLLVMStyle();
+  Spaces.SpaceBeforeAssignmentOperators = false;
+  verifyFormat("int a= 5;", Spaces);
+  verifyFormat("a+= 42;", Spaces);
+  verifyFormat("a or_eq 8;", Spaces);
+}
+
 TEST_F(FormatTest, LinuxBraceBreaking) {
   FormatStyle BreakBeforeBrace = getLLVMStyle();
   BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
@@ -6237,6 +6249,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
+  CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
 
   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",