Skip to content

Commit d947839

Browse files
committed
[clang-format] Add SpaceInComments controls for block comment delimiters
1 parent c6a4e84 commit d947839

File tree

12 files changed

+903
-43
lines changed

12 files changed

+903
-43
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6612,6 +6612,38 @@ the configuration (without a prefix: ``Auto``).
66126612
int a [5]; vs. int a[5];
66136613
int a [5][5]; vs. int a[5][5];
66146614

6615+
.. _SpaceInComments:
6616+
6617+
**SpaceInComments** (``Struct``) :versionbadge:`clang-format 21` :ref:`<SpaceInComments>`
6618+
Controls whitespace around block comment delimiters and parameter-style
6619+
inline comments. Each field accepts a ``CommentSpaceMode``: ``Leave``
6620+
(preserve existing spacing, the default), ``Always`` (insert a single space),
6621+
or ``Never`` (remove all spaces).
6622+
6623+
The available controls are:
6624+
6625+
``AfterOpeningComment``
6626+
Governs the space immediately after ``/*`` in regular block comments.
6627+
``BeforeClosingComment``
6628+
Governs the space before ``*/`` in regular block comments.
6629+
``AfterOpeningParamComment``
6630+
Governs the space after ``/*`` in parameter comments such as ``/*param=*/``.
6631+
``BeforeClosingParamComment``
6632+
Governs the space before ``*/`` in parameter comments.
6633+
6634+
.. code-block:: yaml
6635+
6636+
SpaceInComments:
6637+
BeforeClosingComment: Always
6638+
BeforeClosingParamComment: Never
6639+
6640+
.. code-block:: c++
6641+
6642+
// BeforeClosingComment: Always
6643+
auto Value = foo(/* comment */);
6644+
// BeforeClosingParamComment: Never
6645+
auto Number = foo(/*param=*/42);
6646+
66156647
.. _SpaceInEmptyBlock:
66166648

66176649
**SpaceInEmptyBlock** (``Boolean``) :versionbadge:`clang-format 10` :ref:`<SpaceInEmptyBlock>`

clang/include/clang/Format/Format.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4684,6 +4684,36 @@ struct FormatStyle {
46844684
/// \version 17
46854685
bool SpaceBeforeJsonColon;
46864686

4687+
/// Defines how clang-format should treat spaces around block comment
4688+
/// delimiters and specialized inline comments (such as parameter name
4689+
/// annotations). The default `Leave` mode preserves existing whitespace.
4690+
enum class CommentSpaceMode : int8_t {
4691+
/// Preserve existing whitespace, making no formatting changes.
4692+
Leave,
4693+
/// Ensure exactly one space is present.
4694+
Always,
4695+
/// Ensure no space is present.
4696+
Never,
4697+
};
4698+
4699+
/// Specifies spacing behavior for different block comment forms.
4700+
struct SpaceInCommentsOptions {
4701+
CommentSpaceMode AfterOpeningComment = CommentSpaceMode::Leave;
4702+
CommentSpaceMode BeforeClosingComment = CommentSpaceMode::Leave;
4703+
CommentSpaceMode AfterOpeningParamComment = CommentSpaceMode::Leave;
4704+
CommentSpaceMode BeforeClosingParamComment = CommentSpaceMode::Leave;
4705+
4706+
constexpr bool operator==(const SpaceInCommentsOptions &R) const {
4707+
return AfterOpeningComment == R.AfterOpeningComment &&
4708+
BeforeClosingComment == R.BeforeClosingComment &&
4709+
AfterOpeningParamComment == R.AfterOpeningParamComment &&
4710+
BeforeClosingParamComment == R.BeforeClosingParamComment;
4711+
}
4712+
};
4713+
4714+
/// Controls spacing inside block comments.
4715+
SpaceInCommentsOptions SpaceInComments;
4716+
46874717
/// Different ways to put a space before opening parentheses.
46884718
enum SpaceBeforeParensStyle : int8_t {
46894719
/// This is **deprecated** and replaced by ``Custom`` below, with all
@@ -5612,6 +5642,7 @@ struct FormatStyle {
56125642
SpaceBeforeRangeBasedForLoopColon ==
56135643
R.SpaceBeforeRangeBasedForLoopColon &&
56145644
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
5645+
SpaceInComments == R.SpaceInComments &&
56155646
SpaceInEmptyBraces == R.SpaceInEmptyBraces &&
56165647
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
56175648
SpacesInAngles == R.SpacesInAngles &&

0 commit comments

Comments
 (0)