Skip to content
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

[clang-format] Support of TableGen formatting. #76059

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
448 changes: 447 additions & 1 deletion clang/docs/ClangFormatStyleOptions.rst

Large diffs are not rendered by default.

117 changes: 114 additions & 3 deletions clang/include/clang/Format/Format.h
Copy link
Member

Choose a reason for hiding this comment

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

Your edits in Format.h won't show up in the documentation until after you've run the script in clang/docs/tools/dump_format_style.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the information. I added the document.

Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,39 @@ struct FormatStyle {
/// \version 17
ShortCaseStatementsAlignmentStyle AlignConsecutiveShortCaseStatements;

/// Style of aligning consecutive TableGen DAGArg operator colons.
/// This works only when TableGenBreakInsideDAGArgList is true and the DAGArg
/// is not excepted by TableGenBreakingDAGArgOperators's effect. Align the
/// colon inside DAGArg which have line break inside. \code{tablegen}
/// let dagarg = (ins
/// a :$src1,
/// aa :$src2,
/// aaa:$src3
/// )
/// \endcode
/// \version 18
AlignConsecutiveStyle AlignConsecutiveTableGenBreakingDAGArgColons;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please sort alphabetically.


/// Style of aligning consecutive TableGen cond operator colons.
/// Align the colons of each case inside !cond operators.
/// \code
/// !cond(!eq(size, 1) : 1,
/// !eq(size, 16): 1,
/// true : 0)
/// \endcode
/// \version 18
AlignConsecutiveStyle AlignConsecutiveTableGenCondOperatorColons;

/// Style of aligning consecutive TableGen def colons.
/// This aligns the inherits colons of consecutive definitions.
/// \code
/// def Def : Parent {}
/// def DefDef : Parent {}
/// def DefDefDef : Parent {}
/// \endcode
/// \version 18
AlignConsecutiveStyle AlignConsecutiveTableGenDefinitionColons;

/// Different styles for aligning escaped newlines.
enum EscapedNewlineAlignmentStyle : int8_t {
/// Don't align escaped newlines.
Expand Down Expand Up @@ -3037,6 +3070,7 @@ struct FormatStyle {
bool isProto() const {
return Language == LK_Proto || Language == LK_TextProto;
}
bool isTableGen() const { return Language == LK_TableGen; }

/// Language, this format style is targeted at.
/// \version 3.5
Expand Down Expand Up @@ -4656,6 +4690,69 @@ struct FormatStyle {
/// \version 8
std::vector<std::string> StatementMacros;

/// Works only when TableGenBreakInsideDAGArgList is true.
/// The list needs to be consists of identifiers in TableGen.
/// If any identifier is specified, this limits the effect of
/// TableGenBreakInsideDAGArgList only on DAGArgs beginning with the specified
/// identifiers. For example the configuration,
///
/// \code
/// TableGenBreakingDAGArgOperators: ['ins', 'outs']
/// \endcode
///
/// makes the line break only occurs inside DAGArgs beginning with the
/// specified identifiers 'ins' and 'outs'.
///
/// \code
/// let DAGArgIns = (ins
/// i32:$src1,
/// i32:$src2
/// );
/// let DAGArgOthers = (others i32:$other1, i32:$other2);
/// let DAGArgBang = (!cast<SomeType>("Some") i32:$src1, i32:$src2)
/// \endcode
/// \version 18
std::vector<std::string> TableGenBreakingDAGArgOperators;

/// Insert the line break after each case of !cond operator in TableGen.
/// \code
/// let CondOpe1 = !cond(!eq(size, 1): 1,
/// !eq(size, 16): 1,
/// true: 0);
/// \endcode
/// By default this is true.
/// \version 18
bool TableGenBreakInsideCondOperator;

/// Insert the line break after each element of DAGArg list in TableGen.
/// \code
/// let DAGArgIns = (ins
/// i32:$src1,
/// i32:$src2
/// );
/// \endcode
/// \version 18
bool TableGenBreakInsideDAGArgList;

/// Tend to break inside square bracket in TableGen.
/// For whom likes such a style.
/// \code
/// def Def : Parent<"Def",[
/// a, b, c
/// ]> {
/// ...
/// }
/// \endcode
/// \version 18
bool TableGenPreferBreakInsideSquareBracket;

/// Insert the space around the colon inside a DAGArg list in TableGen.
/// \code
/// let DAGArgIns = (ins i32 : $src1, i32 : $src2);
/// \endcode
/// \version 18
bool TableGenSpaceAroundDAGArgColon;

/// The number of columns used for tab stops.
/// \version 3.7
unsigned TabWidth;
Expand Down Expand Up @@ -4753,6 +4850,12 @@ struct FormatStyle {
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
AlignConsecutiveShortCaseStatements ==
R.AlignConsecutiveShortCaseStatements &&
AlignConsecutiveTableGenBreakingDAGArgColons ==
R.AlignConsecutiveTableGenBreakingDAGArgColons &&
AlignConsecutiveTableGenCondOperatorColons ==
R.AlignConsecutiveTableGenCondOperatorColons &&
AlignConsecutiveTableGenDefinitionColons ==
R.AlignConsecutiveTableGenDefinitionColons &&
AlignEscapedNewlines == R.AlignEscapedNewlines &&
AlignOperands == R.AlignOperands &&
AlignTrailingComments == R.AlignTrailingComments &&
Expand Down Expand Up @@ -4902,9 +5005,17 @@ struct FormatStyle {
SpacesInSquareBrackets == R.SpacesInSquareBrackets &&
Standard == R.Standard &&
StatementAttributeLikeMacros == R.StatementAttributeLikeMacros &&
StatementMacros == R.StatementMacros && TabWidth == R.TabWidth &&
TypeNames == R.TypeNames && TypenameMacros == R.TypenameMacros &&
UseTab == R.UseTab &&
StatementMacros == R.StatementMacros &&
TableGenBreakingDAGArgOperators ==
R.TableGenBreakingDAGArgOperators &&
TableGenBreakInsideCondOperator ==
R.TableGenBreakInsideCondOperator &&
TableGenBreakInsideDAGArgList == R.TableGenBreakInsideDAGArgList &&
TableGenPreferBreakInsideSquareBracket ==
R.TableGenPreferBreakInsideSquareBracket &&
TableGenSpaceAroundDAGArgColon == R.TableGenSpaceAroundDAGArgColon &&
TabWidth == R.TabWidth && TypeNames == R.TypeNames &&
TypenameMacros == R.TypenameMacros && UseTab == R.UseTab &&
VerilogBreakBetweenInstancePorts ==
R.VerilogBreakBetweenInstancePorts &&
WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros;
Expand Down
18 changes: 16 additions & 2 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
Previous.isNot(TT_TableGenDAGArgOpener) &&
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen))) {
Expand Down Expand Up @@ -1229,7 +1230,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
return CurrentState.Indent;
}
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
(Current.is(tok::greater) && Style.isProto())) &&
(Current.is(tok::greater) && (Style.isProto() || Style.isTableGen()))) &&
State.Stack.size() > 1) {
if (Current.closesBlockOrBlockTypeList(Style))
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
Expand Down Expand Up @@ -1257,6 +1258,12 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace))) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
// When DAGArg closer exists top of line, it must be aligned in the similar
// way as function call above.
if (Current.is(tok::r_paren) && State.Stack.size() > 1 &&
(Current.isOneOf(TT_TableGenDAGArgCloser, TT_TableGenParamAngleCloser))) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
(Current.is(tok::r_paren) ||
(Current.is(tok::r_brace) && Current.MatchingParen &&
Expand Down Expand Up @@ -1593,6 +1600,9 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
State.StartOfStringLiteral = State.Column + 1;
if (Current.is(TT_CSharpStringLiteral) && State.StartOfStringLiteral == 0) {
State.StartOfStringLiteral = State.Column + 1;
} else if (Current.is(TT_TableGenMultiLineString) &&
State.StartOfStringLiteral == 0) {
State.StartOfStringLiteral = State.Column + 1;
} else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) {
State.StartOfStringLiteral = State.Column;
} else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
Expand Down Expand Up @@ -1672,7 +1682,9 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
(!Previous || Previous->isNot(tok::kw_return) ||
(Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) &&
(Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
PrecedenceLevel != prec::Comma || Current.NestingLevel == 0)) {
PrecedenceLevel != prec::Comma || Current.NestingLevel == 0) &&
(!Style.isTableGen() || !Previous ||
!Previous->isNot(TT_TableGenDAGArgListComma))) {
NewParenState.Indent = std::max(
std::max(State.Column, NewParenState.Indent), CurrentState.LastSpace);
}
Expand Down Expand Up @@ -1915,6 +1927,8 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
(Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
(Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
State.NextToken->is(TT_TemplateCloser) ||
State.NextToken->is(TT_TableGenParamAngleCloser) ||
State.NextToken->is(TT_TableGenListCloser) ||
(Current.is(tok::greater) && Current.is(TT_DictLiteral)))) {
State.Stack.pop_back();
}
Expand Down
24 changes: 24 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,12 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros);
IO.mapOptional("AlignConsecutiveShortCaseStatements",
Style.AlignConsecutiveShortCaseStatements);
IO.mapOptional("AlignConsecutiveTableGenCondOperatorColons",
Style.AlignConsecutiveTableGenCondOperatorColons);
IO.mapOptional("AlignConsecutiveTableGenBreakingDAGArgColons",
Style.AlignConsecutiveTableGenBreakingDAGArgColons);
IO.mapOptional("AlignConsecutiveTableGenDefinitionColons",
Style.AlignConsecutiveTableGenDefinitionColons);
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Expand Down Expand Up @@ -1124,6 +1130,16 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("StatementAttributeLikeMacros",
Style.StatementAttributeLikeMacros);
IO.mapOptional("StatementMacros", Style.StatementMacros);
IO.mapOptional("TableGenBreakingDAGArgOperators",
Style.TableGenBreakingDAGArgOperators);
IO.mapOptional("TableGenBreakInsideCondOperator",
Style.TableGenBreakInsideCondOperator);
IO.mapOptional("TableGenBreakInsideDAGArgList",
Style.TableGenBreakInsideDAGArgList);
IO.mapOptional("TableGenPreferBreakInsideSquareBracket",
Style.TableGenPreferBreakInsideSquareBracket);
IO.mapOptional("TableGenSpaceAroundDAGArgColon",
Style.TableGenSpaceAroundDAGArgColon);
IO.mapOptional("TabWidth", Style.TabWidth);
IO.mapOptional("TypeNames", Style.TypeNames);
IO.mapOptional("TypenameMacros", Style.TypenameMacros);
Expand Down Expand Up @@ -1437,6 +1453,9 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignConsecutiveDeclarations = {};
LLVMStyle.AlignConsecutiveMacros = {};
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
LLVMStyle.AlignConsecutiveTableGenBreakingDAGArgColons = {};
LLVMStyle.AlignConsecutiveTableGenCondOperatorColons = {};
LLVMStyle.AlignConsecutiveTableGenDefinitionColons = {};
LLVMStyle.AlignTrailingComments = {};
LLVMStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
LLVMStyle.AlignTrailingComments.OverEmptyLines = 0;
Expand Down Expand Up @@ -1585,6 +1604,11 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.StatementAttributeLikeMacros.push_back("Q_EMIT");
LLVMStyle.StatementMacros.push_back("Q_UNUSED");
LLVMStyle.StatementMacros.push_back("QT_REQUIRE_VERSION");
LLVMStyle.TableGenBreakingDAGArgOperators = {};
LLVMStyle.TableGenBreakInsideCondOperator = true;
LLVMStyle.TableGenBreakInsideDAGArgList = false;
LLVMStyle.TableGenPreferBreakInsideSquareBracket = false;
LLVMStyle.TableGenSpaceAroundDAGArgColon = false;
LLVMStyle.TabWidth = 8;
LLVMStyle.UseTab = FormatStyle::UT_Never;
LLVMStyle.VerilogBreakBetweenInstancePorts = true;
Expand Down