https://github.com/MightyFilipns updated https://github.com/llvm/llvm-project/pull/137610
>From 2bbc9f699de5e73d899c5d58607ccfe36aa7a7d7 Mon Sep 17 00:00:00 2001 From: MightyFilipns <filip.milosevi...@gmail.com> Date: Mon, 28 Apr 2025 11:54:20 +0200 Subject: [PATCH] clang-format: Add SpaceAfterOperatorKeyword option --- clang/docs/ClangFormatStyleOptions.rst | 10 ++++++++++ clang/docs/ReleaseNotes.rst | 1 + clang/include/clang/Format/Format.h | 9 +++++++++ clang/lib/Format/Format.cpp | 3 +++ clang/lib/Format/TokenAnnotator.cpp | 2 +- clang/unittests/Format/ConfigParseTest.cpp | 1 + clang/unittests/Format/FormatTest.cpp | 6 ++++++ 7 files changed, 31 insertions(+), 1 deletion(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 3f8a5f49313b2..eb67166e822b9 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -6104,6 +6104,16 @@ the configuration (without a prefix: ``Auto``). true: false: ! someExpression(); vs. !someExpression(); +.. _SpaceAfterOperatorKeyword: + +**SpaceAfterOperatorKeyword** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <SpaceAfterOperatorKeyword>` + If ``true``, a space will be inserted after the ``operator`` keyword. + + .. code-block:: c++ + + true: false: + bool operator == (int a) vs. bool operator== (int a) + .. _SpaceAfterTemplateKeyword: **SpaceAfterTemplateKeyword** (``Boolean``) :versionbadge:`clang-format 4` :ref:`¶ <SpaceAfterTemplateKeyword>` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 3105d8b481560..a058f9b1d1a87 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -695,6 +695,7 @@ clang-format top of the file. - Add ``EnumTrailingComma`` option for inserting/removing commas at the end of ``enum`` enumerator lists. +- Add ``SpaceAfterOperatorKeyword`` option. libclang -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index f6ceef08b46da..c9557b0c6d65e 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -4463,6 +4463,14 @@ struct FormatStyle { /// \version 9 bool SpaceAfterLogicalNot; + /// If ``true``, a space will be inserted after the ``operator`` keyword. + /// \code + /// true: false: + /// bool operator ==(int a); vs. bool operator==(int a); + /// \endcode + /// \version 21 + bool SpaceAfterOperatorKeyword; + /// If \c true, a space will be inserted after the ``template`` keyword. /// \code /// true: false: @@ -5432,6 +5440,7 @@ struct FormatStyle { SortJavaStaticImport == R.SortJavaStaticImport && SpaceAfterCStyleCast == R.SpaceAfterCStyleCast && SpaceAfterLogicalNot == R.SpaceAfterLogicalNot && + SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword && SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && SpaceBeforeCaseColon == R.SpaceBeforeCaseColon && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 5a1c3f556b331..fdb26165beee0 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1151,6 +1151,8 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations); IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast); IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot); + IO.mapOptional("SpaceAfterOperatorKeyword", + Style.SpaceAfterOperatorKeyword); IO.mapOptional("SpaceAfterTemplateKeyword", Style.SpaceAfterTemplateKeyword); IO.mapOptional("SpaceAroundPointerQualifiers", @@ -1638,6 +1640,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric; LLVMStyle.SpaceAfterCStyleCast = false; LLVMStyle.SpaceAfterLogicalNot = false; + LLVMStyle.SpaceAfterOperatorKeyword = false; LLVMStyle.SpaceAfterTemplateKeyword = true; LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; LLVMStyle.SpaceBeforeAssignmentOperators = true; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e56cc92987af7..1c7a0d92fefb3 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5030,7 +5030,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, } if (Left.is(tok::kw_operator)) - return Right.is(tok::coloncolon); + return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword; 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 index 2b08b794792e9..d2717b2ff8a4f 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -204,6 +204,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(SpacesInContainerLiterals); 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 index 333d40d481025..69afe61b5a3f4 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -17289,6 +17289,12 @@ TEST_F(FormatTest, CalculatesOriginalColumn) { " comment */"); } +TEST_F(FormatTest, SpaceAfterOperatorKeyword) { + auto SpaceAfterOperatorKeyword = getLLVMStyle(); + SpaceAfterOperatorKeyword.SpaceAfterOperatorKeyword = true; + verifyFormat("bool operator ++(int a);", SpaceAfterOperatorKeyword); +} + TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { FormatStyle NoSpace = getLLVMStyle(); NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits