From 9b4de85e2f47a01974f451d21fed0276ff912e32 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 25 Sep 2013 15:15:02 +0000 Subject: [PATCH] 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 --- docs/ClangFormatStyleOptions.rst | 3 +++ include/clang/Format/Format.h | 6 +++++- lib/Format/Format.cpp | 4 ++++ lib/Format/TokenAnnotator.cpp | 3 +++ unittests/Format/FormatTest.cpp | 13 +++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst index 69b6cee9ab..0ce4eb7f15 100644 --- a/docs/ClangFormatStyleOptions.rst +++ b/docs/ClangFormatStyleOptions.rst @@ -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 '()'. diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index e5785b8779..1feddf15d6 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -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; } }; diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 5271a62e8f..6166ecc1b3 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -147,6 +147,8 @@ template <> struct MappingTraits { 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; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 6386bc16dd..e1e4f79d17 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index a1136825b1..0175413a50 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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", -- 2.40.0