[clang] [clang-format] Add SpaceAfterOperatorKeyword option (PR #137610)
Filip Milosevic via cfe-commits
cfe-commits at lists.llvm.org
Sun May 4 12:41:44 PDT 2025
https://github.com/MightyFilipns updated https://github.com/llvm/llvm-project/pull/137610
>From aabc71f63914ec58e312fc89f6102919333f0b7f Mon Sep 17 00:00:00 2001
From: MightyFilipns <filip.milosevicns at gmail.com>
Date: Mon, 28 Apr 2025 11:54:20 +0200
Subject: [PATCH] clang-format: Add SpaceAfterOperatorKeyword option
# Conflicts:
# clang/docs/ReleaseNotes.rst
---
clang/docs/ClangFormatStyleOptions.rst | 10 ++
clang/docs/ReleaseNotes.rst | 105 ++-------------------
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, 38 insertions(+), 98 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index b47291599649d..b1d2e09ddaad1 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 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 918ff952bb2c3..436d34dd5bb7d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -134,8 +134,6 @@ Resolutions to C++ Defect Reports
- Bumped the ``__cpp_constexpr`` feature-test macro to ``202002L`` in C++20 mode as indicated in
`P2493R0 <https://wg21.link/P2493R0>`_.
-- Implemented `CWG3005 Function parameters should never be name-independent <https://wg21.link/CWG3005>`_.
-
C Language Changes
------------------
@@ -143,75 +141,15 @@ C Language Changes
function type in Microsoft compatibility mode. #GH124869
- Clang now allows ``restrict`` qualifier for array types with pointer elements (#GH92847).
- Clang now diagnoses ``const``-qualified object definitions without an
- initializer. If the object is a variable or field which is zero-initialized,
- it will be diagnosed under the new warning ``-Wdefault-const-init-var`` or
- ``-Wdefault-const-init-field``, respectively. Similarly, if the variable or
- field is not zero-initialized, it will be diagnosed under the new diagnostic
- ``-Wdefault-const-init-var-unsafe`` or ``-Wdefault-const-init-field-unsafe``,
- respectively. The unsafe diagnostic variants are grouped under a new
- diagnostic ``-Wdefault-const-init-unsafe``, which itself is grouped under the
- new diagnostic ``-Wdefault-const-init``. Finally, ``-Wdefault-const-init`` is
- grouped under ``-Wc++-compat`` because these constructs are not compatible
- with C++. #GH19297
+ initializer. If the object is zero-initialized, it will be diagnosed under
+ the new warning ``-Wdefault-const-init`` (which is grouped under
+ ``-Wc++-compat`` because this construct is not compatible with C++). If the
+ object is left uninitialized, it will be diagnosed unsed the new warning
+ ``-Wdefault-const-init-unsafe`` (which is grouped under
+ ``-Wdefault-const-init``). #GH19297
- Added ``-Wimplicit-void-ptr-cast``, grouped under ``-Wc++-compat``, which
diagnoses implicit conversion from ``void *`` to another pointer type as
being incompatible with C++. (#GH17792)
-- Added ``-Wc++-keyword``, grouped under ``-Wc++-compat``, which diagnoses when
- a C++ keyword is used as an identifier in C. (#GH21898)
-- Added ``-Wc++-hidden-decl``, grouped under ``-Wc++-compat``, which diagnoses
- use of tag types which are visible in C but not visible in C++ due to scoping
- rules. e.g.,
-
- .. code-block:: c
-
- struct S {
- struct T {
- int x;
- } t;
- };
- struct T t; // Invalid C++, valid C, now diagnosed
-- Added ``-Wimplicit-int-enum-cast``, grouped under ``-Wc++-compat``, which
- diagnoses implicit conversion from integer types to an enumeration type in C,
- which is not compatible with C++. #GH37027
-- Split "implicit conversion from enum type to different enum type" diagnostic
- from ``-Wenum-conversion`` into its own diagnostic group,
- ``-Wimplicit-enum-enum-cast``, which is grouped under both
- ``-Wenum-conversion`` and ``-Wimplicit-int-enum-cast``. This conversion is an
- int-to-enum conversion because the enumeration on the right-hand side is
- promoted to ``int`` before the assignment.
-- Added ``-Wtentative-definition-compat``, grouped under ``-Wc++-compat``,
- which diagnoses tentative definitions in C with multiple declarations as
- being incompatible with C++. e.g.,
-
- .. code-block:: c
-
- // File scope
- int i;
- int i; // Vaild C, invalid C++, now diagnosed
-- Added ``-Wunterminated-string-initialization``, grouped under ``-Wextra``,
- which diagnoses an initialization from a string literal where only the null
- terminator cannot be stored. e.g.,
-
- .. code-block:: c
-
-
- char buf1[3] = "foo"; // -Wunterminated-string-initialization
- char buf2[3] = "flarp"; // -Wexcess-initializers
-
- This diagnostic can be suppressed by adding the new ``nonstring`` attribute
- to the field or variable being initialized. #GH137705
-- Added ``-Wc++-unterminated-string-initialization``, grouped under
- ``-Wc++-compat``, which also diagnoses the same cases as
- ``-Wunterminated-string-initialization``. However, this diagnostic is not
- silenced by the ``nonstring`` attribute as these initializations are always
- incompatible with C++.
-- Added ``-Wjump-bypasses-init``, which is off by default and grouped under
- ``-Wc++-compat``. It diagnoses when a jump (``goto`` to its label, ``switch``
- to its ``case``) will bypass the initialization of a local variable, which is
- invalid in C++.
-- Added the existing ``-Wduplicate-decl-specifier`` diagnostic, which is on by
- default, to ``-Wc++-compat`` because duplicated declaration specifiers are
- not valid in C++.
C2y Feature Support
^^^^^^^^^^^^^^^^^^^
@@ -251,10 +189,6 @@ C23 Feature Support
scope.
- Fixed a bug where you could not cast a null pointer constant to type
``nullptr_t``. Fixes #GH133644.
-- Implemented `WG14 N3037 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3037.pdf>`_
- which allows tag types to be redefined within the same translation unit so
- long as both definitions are structurally equivalent (same tag types, same
- tag names, same tag members, etc).
- Fixed a failed assertion with an invalid parameter to the ``#embed``
directive. Fixes #GH126940.
@@ -277,12 +211,6 @@ Non-comprehensive list of changes in this release
- Added `__builtin_elementwise_exp10`.
- For AMDPGU targets, added `__builtin_v_cvt_off_f32_i4` that maps to the `v_cvt_off_f32_i4` instruction.
- Added `__builtin_elementwise_minnum` and `__builtin_elementwise_maxnum`.
-- No longer crashing on invalid Objective-C categories and extensions when
- dumping the AST as JSON. (#GH137320)
-- Clang itself now uses split stacks instead of threads for allocating more
- stack space when running on Apple AArch64 based platforms. This means that
- stack traces of Clang from debuggers, crashes, and profilers may look
- different than before.
New Compiler Flags
------------------
@@ -293,8 +221,6 @@ New Compiler Flags
The feature has `existed <https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program>`_)
for a while and this is just a user facing option.
-- New option ``-ftime-report-json`` added which outputs the same timing data as ``-ftime-report`` but formatted as JSON.
-
Deprecated Compiler Flags
-------------------------
@@ -401,7 +327,6 @@ Improvements to Clang's diagnostics
under the subgroup ``-Wunsafe-buffer-usage-in-libc-call``.
- Diagnostics on chained comparisons (``a < b < c``) are now an error by default. This can be disabled with
``-Wno-error=parentheses``.
-- Similarly, fold expressions over a comparison operator are now an error by default.
- Clang now better preserves the sugared types of pointers to member.
- Clang now better preserves the presence of the template keyword with dependent
prefixes.
@@ -494,8 +419,6 @@ Improvements to Clang's diagnostics
- ``-Winitializer-overrides`` and ``-Wreorder-init-list`` are now grouped under
the ``-Wc99-designator`` diagnostic group, as they also are about the
behavior of the C99 feature as it was introduced into C++20. Fixes #GH47037
-- ``-Wreserved-identifier`` now fires on reserved parameter names in a function
- declaration which is not a definition.
Improvements to Clang's time-trace
----------------------------------
@@ -548,9 +471,6 @@ Bug Fixes in This Version
evaluation. The crashes were happening during diagnostics emission due to
unimplemented statement printer. (#GH132641)
- Fixed visibility calculation for template functions. (#GH103477)
-- Fixed a bug where an attribute before a ``pragma clang attribute`` or
- ``pragma clang __debug`` would cause an assertion. Instead, this now diagnoses
- the invalid attribute location appropriately. (#GH137861)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -561,9 +481,6 @@ Bug Fixes to Compiler Builtins
``void(char *, char *)`` to ``void(void *, void *)`` to match GCC's signature
for the same builtin. (#GH47833)
-- ``__has_unique_object_representations(Incomplete[])`` is no longer accepted, per
- `LWG4113 <https://cplusplus.github.io/LWG/issue4113>`_.
-
Bug Fixes to Attribute Support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed crash when a parameter to the ``clang::annotate`` attribute evaluates to ``void``. See #GH119125
@@ -637,12 +554,7 @@ Bug Fixes to C++ Support
- Clang no longer crashes when establishing subsumption between some constraint expressions. (#GH122581)
- Clang now issues an error when placement new is used to modify a const-qualified variable
in a ``constexpr`` function. (#GH131432)
-- Fixed an incorrect TreeTransform for calls to ``consteval`` functions if a conversion template is present. (#GH137885)
- Clang now emits a warning when class template argument deduction for alias templates is used in C++17. (#GH133806)
-- Fix a crash when checking the template template parameters of a dependent lambda appearing in an alias declaration.
- (#GH136432), (#GH137014), (#GH138018)
-- Fixed an assertion when trying to constant-fold various builtins when the argument
- referred to a reference to an incomplete type. (#GH129397)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -696,8 +608,6 @@ Arm and AArch64 Support
- The ``+nosimd`` attribute is now fully supported for ARM. Previously, this had no effect when being used with
ARM targets, however this will now disable NEON instructions being generated. The ``simd`` option is
also now printed when the ``--print-supported-extensions`` option is used.
-- When a feature that depends on NEON (``simd``) is used, NEON is now automatically enabled.
-- When NEON is disabled (``+nosimd``), all features that depend on NEON will now be disabled.
- Support for __ptrauth type qualifier has been added.
@@ -786,10 +696,11 @@ 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
--------
-- Fixed a bug in ``clang_File_isEqual`` that sometimes led to different
+- Fixed a bug in ``clang_File_isEqual`` that sometimes led to different
in-memory files to be considered as equal.
- Added ``clang_visitCXXMethods``, which allows visiting the methods
of a class.
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 caf386cffd25b..50a22149f7bd9 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5039,7 +5039,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