diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4714,6 +4714,16 @@ true: false: template void foo(); vs. template void foo(); +.. _SpaceAfterOperatorKeyword: + +**SpaceAfterOperatorKeyword** (``Boolean``) :versionbadge:`clang-format 17` :ref:`¶ ` + If ``true``, a space will be inserted after the 'operator' keyword. + + .. code-block:: c++ + + true: false: + bool operator ==(...); vs. bool operator==(...); + .. _SpaceAroundPointerQualifiers: **SpaceAroundPointerQualifiers** (``SpaceAroundPointerQualifiersStyle``) :versionbadge:`clang-format 12` :ref:`¶ ` 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 @@ -3700,6 +3700,14 @@ /// \version 4 bool SpaceAfterTemplateKeyword; + /// If \c true, a space will be inserted after the 'operator' keyword. + /// \code + /// true: false: + /// bool operator ==(...); vs. bool operator==(...); + /// \endcode + /// \version 17 + bool SpaceAfterOperatorKeyword; + /// Different ways to put a space before opening parentheses. enum SpaceAroundPointerQualifiersStyle : int8_t { /// Don't ensure spaces around pointer qualifiers and use PointerAlignment @@ -4412,6 +4420,7 @@ SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && SpaceAfterLogicalNot == R.SpaceAfterLogicalNot && SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && + SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && SpaceBeforeCaseColon == R.SpaceBeforeCaseColon && SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList && 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 @@ -1002,6 +1002,8 @@ IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot); IO.mapOptional("SpaceAfterTemplateKeyword", Style.SpaceAfterTemplateKeyword); + IO.mapOptional("SpaceAfterOperatorKeyword", + Style.SpaceAfterOperatorKeyword); IO.mapOptional("SpaceAroundPointerQualifiers", Style.SpaceAroundPointerQualifiers); IO.mapOptional("SpaceBeforeAssignmentOperators", @@ -1440,6 +1442,7 @@ LLVMStyle.SpaceAfterCStyleCast = false; LLVMStyle.SpaceAfterLogicalNot = false; LLVMStyle.SpaceAfterTemplateKeyword = true; + LLVMStyle.SpaceAfterOperatorKeyword = false; LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; LLVMStyle.SpaceBeforeCaseColon = false; LLVMStyle.SpaceBeforeCtorInitializerColon = true; @@ -1731,6 +1734,7 @@ MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; MozillaStyle.PointerAlignment = FormatStyle::PAS_Left; MozillaStyle.SpaceAfterTemplateKeyword = false; + MozillaStyle.SpaceAfterOperatorKeyword = false; return MozillaStyle; } 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 @@ -3781,6 +3781,8 @@ } if (Right.is(tok::less) && Left.is(tok::kw_template)) return Style.SpaceAfterTemplateKeyword; + if (Left.is(tok::kw_operator)) + return Style.SpaceAfterOperatorKeyword; if (Left.isOneOf(tok::exclaim, tok::tilde)) return false; if (Left.is(tok::at) && 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 @@ -184,6 +184,7 @@ CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); CHECK_PARSE_BOOL(SpaceAfterCStyleCast); CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); + CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword); CHECK_PARSE_BOOL(SpaceAfterLogicalNot); CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 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 @@ -22907,6 +22907,12 @@ verifyFormat("template void foo();", Style); } +TEST_F(FormatTest, SpaceAfterOperatorKeyword) { + FormatStyle Style = getLLVMStyle(); + Style.SpaceAfterOperatorKeyword = false; + verifyFormat("bool operator==(void*);", Style); +} + TEST_F(FormatTest, TripleAngleBrackets) { verifyFormat("f<<<1, 1>>>();"); verifyFormat("f<<<1, 1, 1, s>>>();");