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 @@ -3759,6 +3759,7 @@ Left.Previous->is(tok::kw_operator)) { return false; } + // co_await (x), co_yield (x), co_return (x) if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) && !Right.isOneOf(tok::semi, tok::r_paren)) { @@ -4202,7 +4203,7 @@ return true; if (Left.is(tok::kw_operator)) - return Right.is(tok::coloncolon); + return Style.SpaceAfterOperatorKeyword || 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 @@ -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 @@ -22905,6 +22905,16 @@ FormatStyle Style = getLLVMStyle(); Style.SpaceAfterTemplateKeyword = false; verifyFormat("template void foo();", Style); + Style.SpaceAfterTemplateKeyword = true; + verifyFormat("template void foo();", Style); +} + +TEST_F(FormatTest, SpaceAfterOperatorKeyword) { + FormatStyle Style = getLLVMStyle(); + Style.SpaceAfterOperatorKeyword = false; + verifyFormat("bool operator==();", Style); + Style.SpaceAfterOperatorKeyword = true; + verifyFormat("bool operator ==();", Style); } TEST_F(FormatTest, TripleAngleBrackets) {