-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-format] Allow custom pointer/ref alignment in return types #169160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5668,31 +5668,107 @@ the configuration (without a prefix: ``Auto``). | |
|
|
||
| .. _PointerAlignment: | ||
|
|
||
| **PointerAlignment** (``PointerAlignmentStyle``) :versionbadge:`clang-format 3.7` :ref:`¶ <PointerAlignment>` | ||
| **PointerAlignment** (``PointerAlignmentOptions``) :versionbadge:`clang-format 3.7` :ref:`¶ <PointerAlignment>` | ||
| Pointer and reference alignment style. | ||
|
|
||
| Possible values: | ||
| Acceptable values (configured as a single string or with suboptions): | ||
| * ``Left`` | ||
| * ``Right`` | ||
| * ``Middle`` | ||
|
|
||
| * ``PAS_Left`` (in configuration: ``Left``) | ||
| Align pointer to the left. | ||
| For example, to configure left pointer alignment: | ||
|
|
||
| .. code-block:: c++ | ||
| .. code-block:: yaml | ||
|
|
||
| int* a; | ||
| PointerAlignment: Left | ||
|
|
||
| * ``PAS_Right`` (in configuration: ``Right``) | ||
| Align pointer to the right. | ||
| # or | ||
|
|
||
| .. code-block:: c++ | ||
| PointerAlignment: | ||
| Default: Left | ||
|
|
||
| int *a; | ||
| Nested configuration flags: | ||
|
|
||
| * ``PAS_Middle`` (in configuration: ``Middle``) | ||
| Align pointer in the middle. | ||
| Pointer and reference alignment options. | ||
|
|
||
| .. code-block:: c++ | ||
| * ``PointerAlignmentStyle Default`` | ||
| The default alignment for pointers and references. | ||
|
|
||
| Possible values: | ||
|
|
||
| * ``PAS_Left`` (in configuration: ``Left``) | ||
| Align pointer to the left. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int* a; | ||
|
|
||
| * ``PAS_Right`` (in configuration: ``Right``) | ||
| Align pointer to the right. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int *a; | ||
|
|
||
| * ``PAS_Middle`` (in configuration: ``Middle``) | ||
| Align pointer in the middle. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int * a; | ||
|
|
||
|
|
||
| * ``ReturnTypeAlignmentStyle ReturnType`` | ||
| The alignment for pointers in function return types. | ||
|
|
||
| Possible values: | ||
|
|
||
| * ``RTAS_Default`` (in configuration: ``Default``) | ||
| Use default alignment. | ||
|
|
||
| * ``RTAS_Left`` (in configuration: ``Left``) | ||
| Align pointer/reference to the left. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int* a(void); | ||
|
|
||
| * ``RTAS_Right`` (in configuration: ``Right``) | ||
| Align pointer/reference to the right. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int *a(void); | ||
|
|
||
| * ``RTAS_Middle`` (in configuration: ``Middle``) | ||
| Align pointer/reference in the middle. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int * a(void); | ||
|
|
||
|
|
||
| * ``CastAlignmentStyle CStyleCast`` | ||
| The alignment for pointers in C-style casts. | ||
|
|
||
| Possible values: | ||
|
|
||
| * ``CAS_Default`` (in configuration: ``Default``) | ||
| Use default alignment. | ||
|
|
||
| * ``CAS_Left`` (in configuration: ``Left``) | ||
| Align pointer/reference to the left. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| (char*)s; | ||
|
|
||
| * ``CAS_Right`` (in configuration: ``Right``) | ||
| Align pointer/reference to the right. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int * a; | ||
| (char *)s; | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -5824,34 +5900,111 @@ the configuration (without a prefix: ``Auto``). | |
|
|
||
| .. _ReferenceAlignment: | ||
|
|
||
| **ReferenceAlignment** (``ReferenceAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ <ReferenceAlignment>` | ||
| **ReferenceAlignment** (``ReferenceAlignmentOptions``) :versionbadge:`clang-format 13` :ref:`¶ <ReferenceAlignment>` | ||
| Reference alignment style (overrides ``PointerAlignment`` for references). | ||
|
|
||
| Possible values: | ||
| Acceptable values (configured as a single string or with suboptions): | ||
| * ``Pointer`` | ||
| * ``Left`` | ||
| * ``Right`` | ||
| * ``Middle`` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto comment above |
||
|
|
||
| * ``RAS_Pointer`` (in configuration: ``Pointer``) | ||
| Align reference like ``PointerAlignment``. | ||
| For example, to configure right reference alignment: | ||
|
|
||
| * ``RAS_Left`` (in configuration: ``Left``) | ||
| Align reference to the left. | ||
| .. code-block:: yaml | ||
|
|
||
| .. code-block:: c++ | ||
| ReferenceAlignment: Right | ||
|
|
||
| int& a; | ||
| # or | ||
|
|
||
| * ``RAS_Right`` (in configuration: ``Right``) | ||
| Align reference to the right. | ||
| ReferenceAlignment: | ||
| Default: Right | ||
|
|
||
| .. code-block:: c++ | ||
| Nested configuration flags: | ||
|
|
||
| int &a; | ||
| Reference alignment options. | ||
|
|
||
| * ``RAS_Middle`` (in configuration: ``Middle``) | ||
| Align reference in the middle. | ||
| * ``ReferenceAlignmentStyle Default`` | ||
| The default alignment for references. | ||
|
|
||
| .. code-block:: c++ | ||
| Possible values: | ||
|
|
||
| * ``RAS_Pointer`` (in configuration: ``Pointer``) | ||
| Align reference like ``PointerAlignment``. | ||
|
|
||
| * ``RAS_Left`` (in configuration: ``Left``) | ||
| Align reference to the left. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int& a; | ||
|
|
||
| * ``RAS_Right`` (in configuration: ``Right``) | ||
| Align reference to the right. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int &a; | ||
|
|
||
| * ``RAS_Middle`` (in configuration: ``Middle``) | ||
| Align reference in the middle. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int & a; | ||
|
|
||
|
|
||
| * ``ReturnTypeAlignmentStyle ReturnType`` | ||
| The alignment for references in function return types. | ||
|
|
||
| Possible values: | ||
|
|
||
| * ``RTAS_Default`` (in configuration: ``Default``) | ||
| Use default alignment. | ||
|
|
||
| * ``RTAS_Left`` (in configuration: ``Left``) | ||
| Align pointer/reference to the left. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int* a(void); | ||
|
|
||
| * ``RTAS_Right`` (in configuration: ``Right``) | ||
| Align pointer/reference to the right. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int *a(void); | ||
|
|
||
| * ``RTAS_Middle`` (in configuration: ``Middle``) | ||
| Align pointer/reference in the middle. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int * a(void); | ||
|
|
||
|
|
||
| * ``CastAlignmentStyle CStyleCast`` | ||
| The alignment for references in C-style casts. | ||
|
|
||
| Possible values: | ||
|
|
||
| * ``CAS_Default`` (in configuration: ``Default``) | ||
| Use default alignment. | ||
|
|
||
| * ``CAS_Left`` (in configuration: ``Left``) | ||
| Align pointer/reference to the left. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| (char*)s; | ||
|
|
||
| * ``CAS_Right`` (in configuration: ``Right``) | ||
| Align pointer/reference to the right. | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| int & a; | ||
| (char *)s; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4036,9 +4036,78 @@ struct FormatStyle { | |
| PAS_Middle | ||
| }; | ||
|
|
||
| /// \brief The pointer/reference alignment style for function return types. | ||
| enum ReturnTypeAlignmentStyle : int8_t { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can these be alphabetic ordering |
||
| /// Use default alignment. | ||
| RTAS_Default, | ||
| /// Align pointer/reference to the left. | ||
| /// \code | ||
| /// int* a(void); | ||
| /// \endcode | ||
| RTAS_Left, | ||
| /// Align pointer/reference to the right. | ||
| /// \code | ||
| /// int *a(void); | ||
| /// \endcode | ||
| RTAS_Right, | ||
| /// Align pointer/reference in the middle. | ||
| /// \code | ||
| /// int * a(void); | ||
| /// \endcode | ||
| RTAS_Middle | ||
| }; | ||
|
|
||
| /// \brief The pointer/reference alignment style for C-style casts. | ||
| enum CastAlignmentStyle : int8_t { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C before R |
||
| /// Use default alignment. | ||
| CAS_Default, | ||
| /// Align pointer/reference to the left. | ||
| /// \code | ||
| /// (char*)s; | ||
| /// \endcode | ||
| CAS_Left, | ||
| /// Align pointer/reference to the right. | ||
| /// \code | ||
| /// (char *)s; | ||
| /// \endcode | ||
| CAS_Right, | ||
| }; | ||
|
|
||
| /// Pointer and reference alignment options. | ||
| struct PointerAlignmentOptions { | ||
| /// The default alignment for pointers and references. | ||
| PointerAlignmentStyle Default; | ||
| /// The alignment for pointers in function return types. | ||
| ReturnTypeAlignmentStyle ReturnType; | ||
| /// The alignment for pointers in C-style casts. | ||
| CastAlignmentStyle CStyleCast; | ||
| bool operator==(const PointerAlignmentOptions &R) const { | ||
| return Default == R.Default && ReturnType == R.ReturnType && | ||
| CStyleCast == R.CStyleCast; | ||
| } | ||
| bool operator!=(const PointerAlignmentOptions &R) const { | ||
| return !(*this == R); | ||
| } | ||
| }; | ||
|
|
||
| /// Pointer and reference alignment style. | ||
| /// | ||
| /// Acceptable values (configured as a single string or with suboptions): | ||
| /// * ``Left`` | ||
| /// * ``Right`` | ||
| /// * ``Middle`` | ||
| /// | ||
| /// For example, to configure left pointer alignment: | ||
| /// \code{.yaml} | ||
| /// PointerAlignment: Left | ||
| /// | ||
| /// # or | ||
| /// | ||
| /// PointerAlignment: | ||
| /// Default: Left | ||
| /// \endcode | ||
| /// \version 3.7 | ||
| PointerAlignmentStyle PointerAlignment; | ||
| PointerAlignmentOptions PointerAlignment; | ||
|
|
||
| /// The number of columns to use for indentation of preprocessor statements. | ||
| /// When set to -1 (default) ``IndentWidth`` is used also for preprocessor | ||
|
|
@@ -4208,9 +4277,42 @@ struct FormatStyle { | |
| RAS_Middle | ||
| }; | ||
|
|
||
| /// Reference alignment options. | ||
| struct ReferenceAlignmentOptions { | ||
| /// The default alignment for references. | ||
| ReferenceAlignmentStyle Default; | ||
| /// The alignment for references in function return types. | ||
| ReturnTypeAlignmentStyle ReturnType; | ||
| /// The alignment for references in C-style casts. | ||
| CastAlignmentStyle CStyleCast; | ||
| bool operator==(const ReferenceAlignmentOptions &R) const { | ||
| return Default == R.Default && ReturnType == R.ReturnType && | ||
| CStyleCast == R.CStyleCast; | ||
| } | ||
| bool operator!=(const ReferenceAlignmentOptions &R) const { | ||
| return !(*this == R); | ||
| } | ||
| }; | ||
|
|
||
| /// Reference alignment style (overrides ``PointerAlignment`` for references). | ||
| /// | ||
| /// Acceptable values (configured as a single string or with suboptions): | ||
| /// * ``Pointer`` | ||
| /// * ``Left`` | ||
| /// * ``Right`` | ||
| /// * ``Middle`` | ||
| /// | ||
| /// For example, to configure right reference alignment: | ||
| /// \code{.yaml} | ||
| /// ReferenceAlignment: Right | ||
| /// | ||
| /// # or | ||
| /// | ||
| /// ReferenceAlignment: | ||
| /// Default: Right | ||
| /// \endcode | ||
| /// \version 13 | ||
| ReferenceAlignmentStyle ReferenceAlignment; | ||
| ReferenceAlignmentOptions ReferenceAlignment; | ||
|
|
||
| // clang-format off | ||
| /// Types of comment reflow style. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like it should be Left/Right/Middle/Custom/Leave where Custom lets you change what you have here
I don't feel like the "Default" follows the pattern of other options as a struct
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mydeveloperday I'm not sure I follow, do you suggest
PointerAlignment: Customand then another optionPointerAlignmentOptionsstruct to configure the individual options?I'm fine to rework it like that if that's what you want for consistency but I also have to mention that I find my approach much nicer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if yes, doesn't that still need a
Defaultoption in the struct, unless you want me to add separate options for every possible case where pointer/reference alignment could be different?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel like the "Default" pattern is followed elsewhere, but the Custom pattern is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mydeveloperday Thanks for the response, but you didn't answer my questions from above. I do not see how the
Custompattern solves the problem of still having to set a default alignment.