Skip to content
Open
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
213 changes: 183 additions & 30 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Copy link
Contributor

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

Copy link
Contributor Author

@DaanDeMeyer DaanDeMeyer Nov 25, 2025

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: Custom and then another option PointerAlignmentOptions struct 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.

Copy link
Contributor Author

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 Default option in the struct, unless you want me to add separate options for every possible case where pointer/reference alignment could be different?

Copy link
Contributor

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

Copy link
Contributor Author

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 Custom pattern solves the problem of still having to set a default alignment.


* ``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;



Expand Down Expand Up @@ -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``
Copy link
Contributor

Choose a reason for hiding this comment

The 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;



Expand Down
106 changes: 104 additions & 2 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4036,9 +4036,78 @@ struct FormatStyle {
PAS_Middle
};

/// \brief The pointer/reference alignment style for function return types.
enum ReturnTypeAlignmentStyle : int8_t {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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.
Expand Down
Loading