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
161 changes: 161 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,29 @@ the configuration (without a prefix: ``Auto``).
/* A comment. */
double e = 4;

* ``bool AlignBlockComments`` Only for ``AlignConsecutiveDeclarations``. Whether block comments
are aligned in declarations.

.. code-block:: c++

true:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

false:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

Expand Down Expand Up @@ -538,6 +561,29 @@ the configuration (without a prefix: ``Auto``).
/* A comment. */
double e = 4;

* ``bool AlignBlockComments`` Only for ``AlignConsecutiveDeclarations``. Whether block comments
are aligned in declarations.

.. code-block:: c++

true:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

false:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

Expand Down Expand Up @@ -696,6 +742,29 @@ the configuration (without a prefix: ``Auto``).
/* A comment. */
double e = 4;

* ``bool AlignBlockComments`` Only for ``AlignConsecutiveDeclarations``. Whether block comments
are aligned in declarations.

.. code-block:: c++

true:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

false:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

Expand Down Expand Up @@ -855,6 +924,29 @@ the configuration (without a prefix: ``Auto``).
/* A comment. */
double e = 4;

* ``bool AlignBlockComments`` Only for ``AlignConsecutiveDeclarations``. Whether block comments
are aligned in declarations.

.. code-block:: c++

true:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

false:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

Expand Down Expand Up @@ -1133,6 +1225,29 @@ the configuration (without a prefix: ``Auto``).
/* A comment. */
double e = 4;

* ``bool AlignBlockComments`` Only for ``AlignConsecutiveDeclarations``. Whether block comments
are aligned in declarations.

.. code-block:: c++

true:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

false:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

Expand Down Expand Up @@ -1289,6 +1404,29 @@ the configuration (without a prefix: ``Auto``).
/* A comment. */
double e = 4;

* ``bool AlignBlockComments`` Only for ``AlignConsecutiveDeclarations``. Whether block comments
are aligned in declarations.

.. code-block:: c++

true:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

false:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

Expand Down Expand Up @@ -1445,6 +1583,29 @@ the configuration (without a prefix: ``Auto``).
/* A comment. */
double e = 4;

* ``bool AlignBlockComments`` Only for ``AlignConsecutiveDeclarations``. Whether block comments
are aligned in declarations.

.. code-block:: c++

true:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

false:
bool someLongFunction(int /*a*/,
bool b,
const std::string& c);

const bool ret = someLongFunction(4 /*a*/,
true /*b*/,
str /*c*/);

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,7 @@ clang-format
- Adds ``VariableTemplates`` option.
- Adds support for bash globstar in ``.clang-format-ignore``.
- Adds ``WrapNamespaceBodyWithEmptyLines`` option.
- Adds ``AlignBlockComments`` option.

libclang
--------
Expand Down
23 changes: 23 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ struct FormatStyle {
/// double e = 4;
/// \endcode
bool AcrossComments;
/// Only for ``AlignConsecutiveDeclarations``. Whether block comments
/// are aligned in declarations.
/// \code
/// true:
/// bool someLongFunction(int /*a*/,
/// bool b,
/// const std::string& c);
///
/// const bool ret = someLongFunction(4 /*a*/,
/// true /*b*/,
/// str /*c*/);
///
/// false:
/// bool someLongFunction(int /*a*/,
/// bool b,
/// const std::string& c);
///
/// const bool ret = someLongFunction(4 /*a*/,
/// true /*b*/,
/// str /*c*/);
/// \endcode
bool AlignBlockComments;
/// Only for ``AlignConsecutiveAssignments``. Whether compound assignments
/// like ``+=`` are aligned along with ``=``.
/// \code
Expand Down Expand Up @@ -278,6 +300,7 @@ struct FormatStyle {
bool operator==(const AlignConsecutiveStyle &R) const {
return Enabled == R.Enabled && AcrossEmptyLines == R.AcrossEmptyLines &&
AcrossComments == R.AcrossComments &&
AlignBlockComments == R.AlignBlockComments &&
AlignCompound == R.AlignCompound &&
AlignFunctionDeclarations == R.AlignFunctionDeclarations &&
AlignFunctionPointers == R.AlignFunctionPointers &&
Expand Down
36 changes: 26 additions & 10 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,58 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
IO.enumCase(Value, "Consecutive",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AcrossComments=*/false,
/*AlignBlockComments=*/false,
/*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLines",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AcrossComments=*/false,
/*AlignBlockComments=*/false,
/*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true}));
IO.enumCase(Value, "AcrossComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AcrossComments=*/true,
/*AlignBlockComments=*/false,
/*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLinesAndComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AcrossComments=*/true,
/*AlignBlockComments=*/false,
/*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true}));

// For backward compatibility.
IO.enumCase(Value, "true",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AcrossComments=*/false,
/*AlignBlockComments=*/false,
/*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true}));
IO.enumCase(Value, "false", FormatStyle::AlignConsecutiveStyle({}));
}

static void mapping(IO &IO, FormatStyle::AlignConsecutiveStyle &Value) {
IO.mapOptional("Enabled", Value.Enabled);
IO.mapOptional("AcrossEmptyLines", Value.AcrossEmptyLines);
IO.mapOptional("AcrossComments", Value.AcrossComments);
IO.mapOptional("AlignBlockComments", Value.AlignBlockComments);
IO.mapOptional("AlignCompound", Value.AlignCompound);
IO.mapOptional("AlignFunctionDeclarations",
Value.AlignFunctionDeclarations);
Expand Down
11 changes: 10 additions & 1 deletion clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,12 @@ void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
TT_InheritanceColon);
}

static bool shouldAlignBlockComment(const FormatToken &Tok) {
return Tok.is(TT_BlockComment) && Tok.Previous &&
!Tok.Previous->isOneOf(TT_StartOfName, TT_BlockComment) && Tok.Next &&
Tok.Next->isOneOf(tok::comma, tok::r_paren, TT_BlockComment);
}

void WhitespaceManager::alignConsecutiveDeclarations() {
if (!Style.AlignConsecutiveDeclarations.Enabled)
return;
Expand All @@ -1022,7 +1028,10 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
if (C.Tok->is(TT_FunctionDeclarationName))
return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
if (C.Tok->isNot(TT_StartOfName))
return false;
if (!Style.AlignConsecutiveDeclarations.AlignBlockComments ||
!shouldAlignBlockComment(*C.Tok)) {
return false;
}
Comment on lines 1030 to +1034
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fold the nested if statements or run ninja clang-format-check-format and fix the formatting error below:

--- /Users/Owen/llvm-project/clang/lib/Format/WhitespaceManager.cpp	2025-01-03 21:22:11
+++ -	2025-01-03 22:07:03
@@ -1027,11 +1027,12 @@
         }
         if (C.Tok->is(TT_FunctionDeclarationName))
           return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
-        if (C.Tok->isNot(TT_StartOfName))
+        if (C.Tok->isNot(TT_StartOfName)) {
           if (!Style.AlignConsecutiveDeclarations.AlignBlockComments ||
               !shouldAlignBlockComment(*C.Tok)) {
             return false;
           }
+        }
         if (C.Tok->Previous &&
             C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
           return false;

if (C.Tok->Previous &&
C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
return false;
Expand Down
Loading
Loading