Skip to content

Commit ea19ffc

Browse files
committed
clang-format: Add SpaceAfterOperatorKeyword option
1 parent 5147b83 commit ea19ffc

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

clang/docs/ClangFormatStyleOptions.rst

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

6107+
.. _SpaceAfterOperatorKeyword:
6108+
6109+
**SpaceAfterOperatorKeyword** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<SpaceAfterOperatorKeyword>`
6110+
If ``true``, a space will be inserted after the ``operator`` keyword.
6111+
6112+
.. code-block:: c++
6113+
6114+
true: false:
6115+
bool operator == (int a) vs. bool operator== (int a)
6116+
61076117
.. _SpaceAfterTemplateKeyword:
61086118

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

clang/include/clang/Format/Format.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,6 +4463,14 @@ struct FormatStyle {
44634463
/// \version 9
44644464
bool SpaceAfterLogicalNot;
44654465

4466+
/// If \c true, a space will be inserted after the ``operator`` keyword.
4467+
/// \code
4468+
/// true: false:
4469+
/// bool operator == (int a) vs. bool operator== (int a)
4470+
/// \endcode
4471+
/// \version 22
4472+
bool SpaceAfterOperatorKeyword;
4473+
44664474
/// If \c true, a space will be inserted after the ``template`` keyword.
44674475
/// \code
44684476
/// true: false:
@@ -5433,6 +5441,7 @@ struct FormatStyle {
54335441
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
54345442
SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
54355443
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
5444+
SpaceAfterOperatorKeyword == R.SpaceAfterOperatorKeyword &&
54365445
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
54375446
SpaceBeforeCaseColon == R.SpaceBeforeCaseColon &&
54385447
SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList &&

clang/lib/Format/Format.cpp

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

clang/lib/Format/TokenAnnotator.cpp

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

50325032
if (Left.is(tok::kw_operator))
5033-
return Right.is(tok::coloncolon);
5033+
return Right.is(tok::coloncolon) || Style.SpaceAfterOperatorKeyword;
50345034
if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
50355035
!Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
50365036
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17630,6 +17630,10 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1763017630
verifyFormat("int x = int (y);", SomeSpace2);
1763117631
verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
1763217632

17633+
FormatStyle SpaceAfterOperatorKeyword = getLLVMStyle();
17634+
SpaceAfterOperatorKeyword.SpaceAfterOperatorKeyword = true;
17635+
verifyFormat("bool operator ++(int a)", SpaceAfterOperatorKeyword);
17636+
1763317637
FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
1763417638
SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
1763517639
SpaceAfterOverloadedOperator.SpaceBeforeParensOptions

0 commit comments

Comments
 (0)