Skip to content

[clang-format] Added AlignConsecutiveTableGenBreakingDAGArgColons option. #86150

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

Merged
merged 1 commit into from
Mar 22, 2024
Merged
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
145 changes: 145 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,151 @@ the configuration (without a prefix: ``Auto``).
}


.. _AlignConsecutiveTableGenBreakingDAGArgColons:

**AlignConsecutiveTableGenBreakingDAGArgColons** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 19` :ref:`¶ <AlignConsecutiveTableGenBreakingDAGArgColons>`
Style of aligning consecutive TableGen DAGArg operator colons.
If enabled, align the colon inside DAGArg which have line break inside.
This works only when TableGenBreakInsideDAGArg is BreakElements or
BreakAll and the DAGArg is not excepted by
TableGenBreakingDAGArgOperators's effect.

.. code-block:: c++

let dagarg = (ins
a :$src1,
aa :$src2,
aaa:$src3
)

Nested configuration flags:

Alignment options.

They can also be read as a whole for compatibility. The choices are:
- None
- Consecutive
- AcrossEmptyLines
- AcrossComments
- AcrossEmptyLinesAndComments

For example, to align across empty lines and not across comments, either
of these work.

.. code-block:: c++

AlignConsecutiveTableGenBreakingDAGArgColons: AcrossEmptyLines

AlignConsecutiveTableGenBreakingDAGArgColons:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false

* ``bool Enabled`` Whether aligning is enabled.

.. code-block:: c++

#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)

int a = 1;
int somelongname = 2;
double c = 3;

int aaaa : 1;
int b : 12;
int ccc : 8;

int aaaa = 12;
float b = 23;
std::string ccc;

* ``bool AcrossEmptyLines`` Whether to align across empty lines.

.. code-block:: c++

true:
int a = 1;
int somelongname = 2;
double c = 3;

int d = 3;

false:
int a = 1;
int somelongname = 2;
double c = 3;

int d = 3;

* ``bool AcrossComments`` Whether to align across comments.

.. code-block:: c++

true:
int d = 3;
/* A comment. */
double e = 4;

false:
int d = 3;
/* A comment. */
double e = 4;

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

.. code-block:: c++

true:
a &= 2;
bbb = 2;

false:
a &= 2;
bbb = 2;

* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.

.. code-block:: c++

true:
unsigned i;
int &r;
int *p;
int (*f)();

false:
unsigned i;
int &r;
int *p;
int (*f)();

* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
operators are left-padded to the same length as long ones in order to
put all assignment operators to the right of the left hand side.

.. code-block:: c++

true:
a >>= 2;
bbb = 2;

a = 2;
bbb >>= 2;

false:
a >>= 2;
bbb = 2;

a = 2;
bbb >>= 2;


.. _AlignConsecutiveTableGenCondOperatorColons:

**AlignConsecutiveTableGenCondOperatorColons** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 19` :ref:`¶ <AlignConsecutiveTableGenCondOperatorColons>`
Expand Down
17 changes: 17 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,21 @@ struct FormatStyle {
/// \version 17
ShortCaseStatementsAlignmentStyle AlignConsecutiveShortCaseStatements;

/// Style of aligning consecutive TableGen DAGArg operator colons.
/// If enabled, align the colon inside DAGArg which have line break inside.
/// This works only when TableGenBreakInsideDAGArg is BreakElements or
/// BreakAll and the DAGArg is not excepted by
/// TableGenBreakingDAGArgOperators's effect.
/// \code
/// let dagarg = (ins
/// a :$src1,
/// aa :$src2,
/// aaa:$src3
/// )
/// \endcode
/// \version 19
AlignConsecutiveStyle AlignConsecutiveTableGenBreakingDAGArgColons;

/// Style of aligning consecutive TableGen cond operator colons.
/// Align the colons of cases inside !cond operators.
/// \code
Expand Down Expand Up @@ -4879,6 +4894,8 @@ struct FormatStyle {
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
AlignConsecutiveShortCaseStatements ==
R.AlignConsecutiveShortCaseStatements &&
AlignConsecutiveTableGenBreakingDAGArgColons ==
R.AlignConsecutiveTableGenBreakingDAGArgColons &&
AlignConsecutiveTableGenCondOperatorColons ==
R.AlignConsecutiveTableGenCondOperatorColons &&
AlignConsecutiveTableGenDefinitionColons ==
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros);
IO.mapOptional("AlignConsecutiveShortCaseStatements",
Style.AlignConsecutiveShortCaseStatements);
IO.mapOptional("AlignConsecutiveTableGenBreakingDAGArgColons",
Style.AlignConsecutiveTableGenBreakingDAGArgColons);
IO.mapOptional("AlignConsecutiveTableGenCondOperatorColons",
Style.AlignConsecutiveTableGenCondOperatorColons);
IO.mapOptional("AlignConsecutiveTableGenDefinitionColons",
Expand Down Expand Up @@ -1408,6 +1410,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignConsecutiveDeclarations = {};
LLVMStyle.AlignConsecutiveMacros = {};
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
LLVMStyle.AlignConsecutiveTableGenBreakingDAGArgColons = {};
LLVMStyle.AlignConsecutiveTableGenCondOperatorColons = {};
LLVMStyle.AlignConsecutiveTableGenDefinitionColons = {};
LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ extern bool IsCpp;
TYPE(TableGenCondOperatorComma) \
TYPE(TableGenDAGArgCloser) \
TYPE(TableGenDAGArgListColon) \
TYPE(TableGenDAGArgListColonToAlign) \
TYPE(TableGenDAGArgListComma) \
TYPE(TableGenDAGArgListCommaToBreak) \
TYPE(TableGenDAGArgOpener) \
Expand Down
18 changes: 13 additions & 5 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,12 +972,15 @@ class AnnotatingParser {

// DagArg ::= Value [":" TokVarName] | TokVarName
// Appears as a part of SimpleValue6.
bool parseTableGenDAGArg() {
bool parseTableGenDAGArg(bool AlignColon = false) {
if (tryToParseTableGenTokVar())
return true;
if (parseTableGenValue()) {
if (CurrentToken && CurrentToken->is(tok::colon)) {
CurrentToken->setType(TT_TableGenDAGArgListColon);
if (AlignColon)
CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
else
CurrentToken->setType(TT_TableGenDAGArgListColon);
skipToNextNonComment();
return tryToParseTableGenTokVar();
}
Expand Down Expand Up @@ -1048,8 +1051,11 @@ class AnnotatingParser {
skipToNextNonComment();
return true;
}
if (!parseTableGenDAGArg())
if (!parseTableGenDAGArg(
BreakInside &&
Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
return false;
}
FirstDAGArgListElm = false;
}
return false;
Expand Down Expand Up @@ -5123,8 +5129,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
if (Left.is(tok::r_brace) && Right.is(tok::r_square))
return true;
// Do not insert around colon in DAGArg and cond operator.
if (Right.is(TT_TableGenDAGArgListColon) ||
Left.is(TT_TableGenDAGArgListColon)) {
if (Right.isOneOf(TT_TableGenDAGArgListColon,
TT_TableGenDAGArgListColonToAlign) ||
Left.isOneOf(TT_TableGenDAGArgListColon,
TT_TableGenDAGArgListColonToAlign)) {
return false;
}
if (Right.is(TT_TableGenCondOperatorColon))
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() {
alignConsecutiveBitFields();
alignConsecutiveAssignments();
if (Style.isTableGen()) {
alignConsecutiveTableGenBreakingDAGArgColons();
alignConsecutiveTableGenCondOperatorColons();
alignConsecutiveTableGenDefinitions();
}
Expand Down Expand Up @@ -978,6 +979,11 @@ void WhitespaceManager::alignConsecutiveShortCaseStatements() {
Changes);
}

void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
TT_TableGenDAGArgListColonToAlign);
}

void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
TT_TableGenCondOperatorColon);
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/WhitespaceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ class WhitespaceManager {
/// Align consecutive short case statements over all \c Changes.
void alignConsecutiveShortCaseStatements();

/// Align consecutive TableGen DAGArg colon over all \c Changes.
void alignConsecutiveTableGenBreakingDAGArgColons();

/// Align consecutive TableGen cond operator colon over all \c Changes.
void alignConsecutiveTableGenCondOperatorColons();

Expand Down
32 changes: 32 additions & 0 deletions clang/unittests/Format/FormatTestTableGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,38 @@ TEST_F(FormatTestTableGen, DAGArgBreakAll) {
Style);
}

TEST_F(FormatTestTableGen, DAGArgAlignment) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
Style.ColumnLimit = 60;
Style.TableGenBreakInsideDAGArg = FormatStyle::DAS_BreakAll;
Style.TableGenBreakingDAGArgOperators = {"ins", "outs"};
verifyFormat("def Def : Parent {\n"
" let dagarg = (ins\n"
" a:$src1,\n"
" aa:$src2,\n"
" aaa:$src3\n"
" )\n"
"}\n",
Style);
verifyFormat("def Def : Parent {\n"
" let dagarg = (not a:$src1, aa:$src2, aaa:$src2)\n"
"}\n",
Style);
Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled = true;
verifyFormat("def Def : Parent {\n"
" let dagarg = (ins\n"
" a :$src1,\n"
" aa :$src2,\n"
" aaa:$src3\n"
" )\n"
"}\n",
Style);
verifyFormat("def Def : Parent {\n"
" let dagarg = (not a:$src1, aa:$src2, aaa:$src2)\n"
"}\n",
Style);
}

TEST_F(FormatTestTableGen, CondOperatorAlignment) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
Style.ColumnLimit = 60;
Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,22 @@ TEST_F(TokenAnnotatorTest, UnderstandTableGenTokens) {
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_Unknown); // other
EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListComma);
EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);

// If TableGenBreakingDAGArgOperators is enabled, it uses
// TT_TableGenDAGArgListColonToAlign to annotate the colon to align.
Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled = true;
Tokens = AnnotateValue("(ins type1:$src1, type2:$src2)");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier,
TT_TableGenDAGArgOperatorToBreak); // ins
EXPECT_TOKEN(Tokens[3], tok::colon, TT_TableGenDAGArgListColonToAlign);
EXPECT_TOKEN(Tokens[7], tok::colon, TT_TableGenDAGArgListColonToAlign);

Tokens = AnnotateValue("(other type1:$src1, type2:$src2)");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_Unknown); // other
EXPECT_TOKEN(Tokens[3], tok::colon, TT_TableGenDAGArgListColon);
EXPECT_TOKEN(Tokens[7], tok::colon, TT_TableGenDAGArgListColon);
}

TEST_F(TokenAnnotatorTest, UnderstandConstructors) {
Expand Down