Skip to content

Commit 5df01ab

Browse files
[clang-format] Add SpaceAfterOperatorKeyword option (#137610)
Add SpaceAfterOperatorKeyword option to clang-format
1 parent 26572ba commit 5df01ab

File tree

7 files changed

+31
-1
lines changed

7 files changed

+31
-1
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6127,6 +6127,16 @@ the configuration (without a prefix: ``Auto``).
61276127
true: false:
61286128
! someExpression(); vs. !someExpression();
61296129

6130+
.. _SpaceAfterOperatorKeyword:
6131+
6132+
**SpaceAfterOperatorKeyword** (``Boolean``) :versionbadge:`clang-format 21` :ref:`<SpaceAfterOperatorKeyword>`
6133+
If ``true``, a space will be inserted after the ``operator`` keyword.
6134+
6135+
.. code-block:: c++
6136+
6137+
true: false:
6138+
bool operator ==(int a); vs. bool operator==(int a);
6139+
61306140
.. _SpaceAfterTemplateKeyword:
61316141

61326142
**SpaceAfterTemplateKeyword** (``Boolean``) :versionbadge:`clang-format 4` :ref:`<SpaceAfterTemplateKeyword>`

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ clang-format
810810
- Add ``EnumTrailingComma`` option for inserting/removing commas at the end of
811811
``enum`` enumerator lists.
812812
- Add ``OneLineFormatOffRegex`` option for turning formatting off for one line.
813+
- Add ``SpaceAfterOperatorKeyword`` option.
813814

814815
libclang
815816
--------

clang/include/clang/Format/Format.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4484,6 +4484,14 @@ struct FormatStyle {
44844484
/// \version 9
44854485
bool SpaceAfterLogicalNot;
44864486

4487+
/// If ``true``, a space will be inserted after the ``operator`` keyword.
4488+
/// \code
4489+
/// true: false:
4490+
/// bool operator ==(int a); vs. bool operator==(int a);
4491+
/// \endcode
4492+
/// \version 21
4493+
bool SpaceAfterOperatorKeyword;
4494+
44874495
/// If \c true, a space will be inserted after the ``template`` keyword.
44884496
/// \code
44894497
/// true: false:
@@ -5454,6 +5462,7 @@ struct FormatStyle {
54545462
SortJavaStaticImport == R.SortJavaStaticImport &&
54555463
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
54565464
SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
5465+
SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword &&
54575466
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
54585467
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
54595468
SpaceBeforeCaseColon == R.SpaceBeforeCaseColon &&

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,8 @@ template <> struct MappingTraits<FormatStyle> {
11521152
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
11531153
IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
11541154
IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
1155+
IO.mapOptional("SpaceAfterOperatorKeyword",
1156+
Style.SpaceAfterOperatorKeyword);
11551157
IO.mapOptional("SpaceAfterTemplateKeyword",
11561158
Style.SpaceAfterTemplateKeyword);
11571159
IO.mapOptional("SpaceAroundPointerQualifiers",
@@ -1639,6 +1641,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16391641
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
16401642
LLVMStyle.SpaceAfterCStyleCast = false;
16411643
LLVMStyle.SpaceAfterLogicalNot = false;
1644+
LLVMStyle.SpaceAfterOperatorKeyword = false;
16421645
LLVMStyle.SpaceAfterTemplateKeyword = true;
16431646
LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
16441647
LLVMStyle.SpaceBeforeAssignmentOperators = true;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5033,7 +5033,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
50335033
}
50345034

50355035
if (Left.is(tok::kw_operator))
5036-
return Right.is(tok::coloncolon);
5036+
return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
50375037
if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
50385038
!Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
50395039
return true;

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
204204
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
205205
CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
206206
CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
207+
CHECK_PARSE_BOOL(SpaceAfterOperatorKeyword);
207208
CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
208209
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
209210
CHECK_PARSE_BOOL(SpaceBeforeCaseColon);

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17291,6 +17291,12 @@ TEST_F(FormatTest, CalculatesOriginalColumn) {
1729117291
" comment */");
1729217292
}
1729317293

17294+
TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
17295+
auto SpaceAfterOperatorKeyword = getLLVMStyle();
17296+
SpaceAfterOperatorKeyword.SpaceAfterOperatorKeyword = true;
17297+
verifyFormat("bool operator ++(int a);", SpaceAfterOperatorKeyword);
17298+
}
17299+
1729417300
TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1729517301
FormatStyle NoSpace = getLLVMStyle();
1729617302
NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;

0 commit comments

Comments
 (0)