clang-format: Option to removing the space before assignment operators.
Patch contributed by Aaron Wishnick. Thank you!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191375 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst
index 69b6cee..0ce4eb7 100644
--- a/docs/ClangFormatStyleOptions.rst
+++ b/docs/ClangFormatStyleOptions.rst
@@ -249,6 +249,9 @@
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 '()'.
diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h
index e5785b8..1feddf1 100644
--- a/include/clang/Format/Format.h
+++ b/include/clang/Format/Format.h
@@ -222,6 +222,9 @@
/// 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 @@
SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
SpaceAfterControlStatementKeyword ==
- R.SpaceAfterControlStatementKeyword;
+ R.SpaceAfterControlStatementKeyword &&
+ SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators;
}
};
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 5271a62..6166ecc 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -147,6 +147,8 @@
Style.SpacesInCStyleCastParentheses);
IO.mapOptional("SpaceAfterControlStatementKeyword",
Style.SpaceAfterControlStatementKeyword);
+ IO.mapOptional("SpaceBeforeAssignmentOperators",
+ Style.SpaceBeforeAssignmentOperators);
}
};
}
@@ -197,6 +199,7 @@
LLVMStyle.SpaceInEmptyParentheses = false;
LLVMStyle.SpacesInCStyleCastParentheses = false;
LLVMStyle.SpaceAfterControlStatementKeyword = true;
+ LLVMStyle.SpaceBeforeAssignmentOperators = true;
setDefaultPenalties(LLVMStyle);
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
@@ -239,6 +242,7 @@
GoogleStyle.SpaceInEmptyParentheses = false;
GoogleStyle.SpacesInCStyleCastParentheses = false;
GoogleStyle.SpaceAfterControlStatementKeyword = true;
+ GoogleStyle.SpaceBeforeAssignmentOperators = true;
setDefaultPenalties(GoogleStyle);
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 6386bc1..e1e4f79 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -1318,6 +1318,9 @@
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;
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index a113682..0175413 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -5996,6 +5996,18 @@
"}", 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 @@
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",