Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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 @@ -445,6 +445,29 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;

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

.. code-block:: c++

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

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

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

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


.. _AlignConsecutiveBitFields:

Expand Down Expand Up @@ -603,6 +626,29 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;

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

.. code-block:: c++

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

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

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

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


.. _AlignConsecutiveDeclarations:

Expand Down Expand Up @@ -761,6 +807,29 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;

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

.. code-block:: c++

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

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

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

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


.. _AlignConsecutiveMacros:

Expand Down Expand Up @@ -920,6 +989,29 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;

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

.. code-block:: c++

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

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

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

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


.. _AlignConsecutiveShortCaseStatements:

Expand Down Expand Up @@ -1198,6 +1290,29 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;

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

.. code-block:: c++

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

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

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

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


.. _AlignConsecutiveTableGenCondOperatorColons:

Expand Down Expand Up @@ -1354,6 +1469,29 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;

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

.. code-block:: c++

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

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

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

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


.. _AlignConsecutiveTableGenDefinitionColons:

Expand Down Expand Up @@ -1510,6 +1648,29 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;

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

.. code-block:: c++

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

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

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

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


.. _AlignEscapedNewlines:

Expand Down
25 changes: 24 additions & 1 deletion clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,36 @@ struct FormatStyle {
/// bbb >>= 2;
/// \endcode
bool PadOperators;
/// Only for ``AlignConsecutiveDeclarations``. Whether block comments
/// are aligned in declarations.
/// \code
/// true:
/// someLongFunction(int /*a*/,
/// bool b,
/// const std::string& c)
///
/// const bool ret = someLongFunction(4 /*a*/,
/// true /*b*/,
/// str /*c*/);
///
/// false:
/// someLongFunction(int /*a*/,
/// bool b,
/// const std::string& c)
///
/// const bool ret = someLongFunction(4 /*a*/,
/// true /*b*/,
/// str /*c*/);
/// \endcode
bool AlignBlockComments;
bool operator==(const AlignConsecutiveStyle &R) const {
return Enabled == R.Enabled && AcrossEmptyLines == R.AcrossEmptyLines &&
AcrossComments == R.AcrossComments &&
AlignCompound == R.AlignCompound &&
AlignFunctionDeclarations == R.AlignFunctionDeclarations &&
AlignFunctionPointers == R.AlignFunctionPointers &&
PadOperators == R.PadOperators;
PadOperators == R.PadOperators &&
AlignBlockComments == R.AlignBlockComments;
}
bool operator!=(const AlignConsecutiveStyle &R) const {
return !(*this == R);
Expand Down
21 changes: 16 additions & 5 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,43 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true,
/*AlignBlockComments=*/false}));
IO.enumCase(Value, "AcrossEmptyLines",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true,
/*AlignBlockComments=*/false}));
IO.enumCase(Value, "AcrossComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true,
/*AlignBlockComments=*/false}));
IO.enumCase(Value, "AcrossEmptyLinesAndComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*PadOperators=*/true,
/*AlignBlockComments=*/false}));

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

Expand All @@ -89,6 +99,7 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
Value.AlignFunctionDeclarations);
IO.mapOptional("AlignFunctionPointers", Value.AlignFunctionPointers);
IO.mapOptional("PadOperators", Value.PadOperators);
IO.mapOptional("AlignBlockComments", Value.AlignBlockComments);
}
};

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