diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4704,6 +4704,32 @@ true: false: ! someExpression(); vs. !someExpression(); +.. _SpaceAfterOperatorCall: + +**SpaceAfterOperatorCall** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ ` + If ``true``, a space will be inserted after the 'operator' keyword + in every operator call expression. + + .. code-block:: c++ + + true: false: + foo.operator ==(...); vs. foo.operator==(...); + foo.Foo::operator ==(...); vs. foo.Foo::operator==(...); + foo->operator ==(...); vs. foo->operator==(...); + foo->Foo::operator ==(...); vs. foo->Foo::operator==(...); + +.. _SpaceAfterOperatorOverload: + +**SpaceAfterOperatorOverload** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ ` + If ``true``, a space will be inserted after the 'operator' keyword + in every operator overload declaration. + + .. code-block:: c++ + + true: false: + bool operator ==(...); vs. bool operator==(...); + bool Foo::operator ==(...); vs. bool Foo::operator ==(...); + .. _SpaceAfterTemplateKeyword: **SpaceAfterTemplateKeyword** (``Boolean``) :versionbadge:`clang-format 4` :ref:`¶ ` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -708,6 +708,10 @@ - Add ``BracedInitializerIndentWidth`` which can be used to configure the indentation level of the contents of braced init lists. - Add ``KeepEmptyLinesAtEOF`` to keep empty lines at end of file. +- Add ``SpaceAfterOperatorOverload`` style option to allow inserting a space + after the operator keyword in an operator overload declaration. +- Add ``SpaceAfterOperatorCall`` style option to allow inserting a space after + the operator keyword in an operator call expression. libclang -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3692,6 +3692,28 @@ /// \version 9 bool SpaceAfterLogicalNot; + /// If \c true, a space will be inserted after the 'operator' keyword + /// in every operator overload declaration. + /// \code + /// true: false: + /// bool operator ==(...); vs. bool operator==(...); + /// bool Foo::operator ==(...); vs. bool Foo::operator ==(...); + /// \endcode + /// \version 17 + bool SpaceAfterOperatorOverload; + + /// If \c true, a space will be inserted after the 'operator' keyword + /// in every operator call expression. + /// \code + /// true: false: + /// foo.operator ==(...); vs. foo.operator==(...); + /// foo.Foo::operator ==(...); vs. foo.Foo::operator==(...); + /// foo->operator ==(...); vs. foo->operator==(...); + /// foo->Foo::operator ==(...); vs. foo->Foo::operator==(...); + /// \endcode + /// \version 17 + bool SpaceAfterOperatorCall; + /// If \c true, a space will be inserted after the 'template' keyword. /// \code /// true: false: @@ -4411,6 +4433,8 @@ SortJavaStaticImport == R.SortJavaStaticImport && SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && SpaceAfterLogicalNot == R.SpaceAfterLogicalNot && + SpaceAfterOperatorCall == R.SpaceAfterOperatorCall && + SpaceAfterOperatorOverload == R.SpaceAfterOperatorOverload && SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && SpaceBeforeCaseColon == R.SpaceBeforeCaseColon && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1000,6 +1000,9 @@ IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations); IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot); + IO.mapOptional("SpaceAfterOperatorCall", Style.SpaceAfterOperatorCall); + IO.mapOptional("SpaceAfterOperatorOverload", + Style.SpaceAfterOperatorOverload); IO.mapOptional("SpaceAfterTemplateKeyword", Style.SpaceAfterTemplateKeyword); IO.mapOptional("SpaceAroundPointerQualifiers", @@ -1439,6 +1442,8 @@ LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric; LLVMStyle.SpaceAfterCStyleCast = false; LLVMStyle.SpaceAfterLogicalNot = false; + LLVMStyle.SpaceAfterOperatorOverload = false; + LLVMStyle.SpaceAfterOperatorCall = false; LLVMStyle.SpaceAfterTemplateKeyword = true; LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; LLVMStyle.SpaceBeforeCaseColon = false; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -4200,9 +4200,21 @@ // Space in __attribute__((attr)) ::type. if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon)) return true; - - if (Left.is(tok::kw_operator)) - return Right.is(tok::coloncolon); + if (Left.is(tok::kw_operator)) { + if (Left.hasWhitespaceBefore()) + return Style.SpaceAfterOperatorOverload || Right.is(tok::coloncolon); + if (!Left.Previous) + return Right.is(tok::coloncolon); + FormatToken const *Previous = Left.Previous; + while (Previous) { + if (!Previous->hasWhitespaceBefore()) { + Previous = Previous->Previous; + continue; + } + return Style.SpaceAfterOperatorOverload || Right.is(tok::coloncolon); + } + return Style.SpaceAfterOperatorCall || Right.is(tok::coloncolon); + } if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) && !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) { return true; diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -183,6 +183,8 @@ CHECK_PARSE_BOOL(SpacesInContainerLiterals); CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); CHECK_PARSE_BOOL(SpaceAfterCStyleCast); + CHECK_PARSE_BOOL(SpaceAfterOperatorOverload); + CHECK_PARSE_BOOL(SpaceAfterOperatorCall); CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); CHECK_PARSE_BOOL(SpaceAfterLogicalNot); CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -22901,8 +22901,43 @@ Spaces); } +TEST_F(FormatTest, SpaceAfterOperatorOverload) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("bool operator==();", Style); + verifyFormat("bool Foo::operator==();", Style); + verifyFormat("foo.operator==();", Style); + verifyFormat("foo.Foo::operator==();", Style); + verifyFormat("foo->operator==();", Style); + verifyFormat("foo->Foo::operator==();", Style); + Style.SpaceAfterOperatorOverload = true; + verifyFormat("bool operator ==();", Style); + verifyFormat("bool Foo::operator ==();", Style); + verifyFormat("foo.operator==();", Style); + verifyFormat("foo.Foo::operator==();", Style); + verifyFormat("foo->operator==();", Style); + verifyFormat("foo->Foo::operator==();", Style); +} + +TEST_F(FormatTest, SpaceAfterOperatorCall) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("bool operator==();", Style); + verifyFormat("bool Foo::operator==();", Style); + verifyFormat("foo.operator==();", Style); + verifyFormat("foo.Foo::operator==();", Style); + verifyFormat("foo->operator==();", Style); + verifyFormat("foo->Foo::operator==();", Style); + Style.SpaceAfterOperatorCall = true; + verifyFormat("bool operator==();", Style); + verifyFormat("bool Foo::operator==();", Style); + verifyFormat("foo.operator ==();", Style); + verifyFormat("foo.Foo::operator ==();", Style); + verifyFormat("foo->operator ==();", Style); + verifyFormat("foo->Foo::operator ==();", Style); +} + TEST_F(FormatTest, SpaceAfterTemplateKeyword) { FormatStyle Style = getLLVMStyle(); + verifyFormat("template void foo();", Style); Style.SpaceAfterTemplateKeyword = false; verifyFormat("template void foo();", Style); }