[clang] 5df01ab - [clang-format] Add SpaceAfterOperatorKeyword option (#137610)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 7 22:47:59 PDT 2025
Author: Filip Milosevic
Date: 2025-05-07T22:47:56-07:00
New Revision: 5df01abe191ff4f848566e239798a2b4d26e1cf4
URL: https://github.com/llvm/llvm-project/commit/5df01abe191ff4f848566e239798a2b4d26e1cf4
DIFF: https://github.com/llvm/llvm-project/commit/5df01abe191ff4f848566e239798a2b4d26e1cf4.diff
LOG: [clang-format] Add SpaceAfterOperatorKeyword option (#137610)
Add SpaceAfterOperatorKeyword option to clang-format
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/ConfigParseTest.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index b47291599649d..a4c381bf583b6 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -6127,6 +6127,16 @@ the configuration (without a prefix: ``Auto``).
true: false:
! someExpression(); vs. !someExpression();
+.. _SpaceAfterOperatorKeyword:
+
+**SpaceAfterOperatorKeyword** (``Boolean``) :versionbadge:`clang-format 21` :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 e5b463173dcf4..c52e285bde627 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -810,6 +810,7 @@ clang-format
- Add ``EnumTrailingComma`` option for inserting/removing commas at the end of
``enum`` enumerator lists.
- Add ``OneLineFormatOffRegex`` option for turning formatting off for one line.
+- Add ``SpaceAfterOperatorKeyword`` option.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 7fe41d800ccb3..b86c4bd00eb91 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4484,6 +4484,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:
@@ -5454,6 +5462,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 2f4b64ef4f5fe..20b5352b83a9e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1152,6 +1152,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",
@@ -1639,6 +1641,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 f0f9207564ab1..542c362ccacae 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5033,7 +5033,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 f7ab5546c7193..bd27a9f60ffcc 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 f85e078696730..436beaf68bd2a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17291,6 +17291,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;
More information about the cfe-commits
mailing list