[clang] e54af60 - [clang-format] Added AlignConsecutiveTableGenBreakingDAGArgColons option. (#86150)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 22 07:11:40 PDT 2024
Author: Hirofumi Nakamura
Date: 2024-03-22T23:11:36+09:00
New Revision: e54af608160350baa7ae1b8069f916eb625beadd
URL: https://github.com/llvm/llvm-project/commit/e54af608160350baa7ae1b8069f916eb625beadd
DIFF: https://github.com/llvm/llvm-project/commit/e54af608160350baa7ae1b8069f916eb625beadd.diff
LOG: [clang-format] Added AlignConsecutiveTableGenBreakingDAGArgColons option. (#86150)
The option to specify the style of alignment of the colons inside TableGen's DAGArg.
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/FormatToken.h
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/lib/Format/WhitespaceManager.h
clang/unittests/Format/FormatTestTableGen.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index be021dfc5c084c..2ee36f24d7ce4b 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -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>`
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 7ad2579bf7773b..0720c8283cd75c 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -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
@@ -4879,6 +4894,8 @@ struct FormatStyle {
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
AlignConsecutiveShortCaseStatements ==
R.AlignConsecutiveShortCaseStatements &&
+ AlignConsecutiveTableGenBreakingDAGArgColons ==
+ R.AlignConsecutiveTableGenBreakingDAGArgColons &&
AlignConsecutiveTableGenCondOperatorColons ==
R.AlignConsecutiveTableGenCondOperatorColons &&
AlignConsecutiveTableGenDefinitionColons ==
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 63ec3a88978dd9..46ed5baaeacead 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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",
@@ -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;
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 06f567059c3576..2ddcd5259446f6 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -152,6 +152,7 @@ namespace format {
TYPE(TableGenCondOperatorComma) \
TYPE(TableGenDAGArgCloser) \
TYPE(TableGenDAGArgListColon) \
+ TYPE(TableGenDAGArgListColonToAlign) \
TYPE(TableGenDAGArgListComma) \
TYPE(TableGenDAGArgListCommaToBreak) \
TYPE(TableGenDAGArgOpener) \
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 94d2266555f6b9..a5cafcbacaa591 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -975,12 +975,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();
}
@@ -1051,8 +1054,11 @@ class AnnotatingParser {
skipToNextNonComment();
return true;
}
- if (!parseTableGenDAGArg())
+ if (!parseTableGenDAGArg(
+ BreakInside &&
+ Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
return false;
+ }
FirstDAGArgListElm = false;
}
return false;
@@ -5130,8 +5136,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))
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 753be25bfd6758..fef85abf79a38c 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -112,6 +112,7 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() {
alignConsecutiveBitFields();
alignConsecutiveAssignments();
if (Style.isTableGen()) {
+ alignConsecutiveTableGenBreakingDAGArgColons();
alignConsecutiveTableGenCondOperatorColons();
alignConsecutiveTableGenDefinitions();
}
@@ -981,6 +982,11 @@ void WhitespaceManager::alignConsecutiveShortCaseStatements() {
Changes);
}
+void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
+ alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
+ TT_TableGenDAGArgListColonToAlign);
+}
+
void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
TT_TableGenCondOperatorColon);
diff --git a/clang/lib/Format/WhitespaceManager.h b/clang/lib/Format/WhitespaceManager.h
index 0ebc6cf8377cdb..98cf4a260cc468 100644
--- a/clang/lib/Format/WhitespaceManager.h
+++ b/clang/lib/Format/WhitespaceManager.h
@@ -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();
diff --git a/clang/unittests/Format/FormatTestTableGen.cpp b/clang/unittests/Format/FormatTestTableGen.cpp
index c96866f0840f00..8ca6bf97e5a6b1 100644
--- a/clang/unittests/Format/FormatTestTableGen.cpp
+++ b/clang/unittests/Format/FormatTestTableGen.cpp
@@ -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;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1aa855b3419877..2539d3d76ef019 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2424,6 +2424,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) {
More information about the cfe-commits
mailing list