Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,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
--------
Expand Down
9 changes: 9 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 &&
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down