Skip to content

Commit 8c1c94e

Browse files
authored
[clang-format] Support of TableGen basic format restrictions. (#81611)
- Allow/force to break the line or not. - Allow/force to insert space or not.
1 parent bfda580 commit 8c1c94e

File tree

3 files changed

+335
-2
lines changed

3 files changed

+335
-2
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
821821
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
822822
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
823823
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
824+
Previous.isNot(TT_TableGenDAGArgOpener) &&
824825
!(Current.MacroParent && Previous.MacroParent) &&
825826
(Current.isNot(TT_LineComment) ||
826827
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen))) {
@@ -1250,7 +1251,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
12501251
return CurrentState.Indent;
12511252
}
12521253
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
1253-
(Current.is(tok::greater) && Style.isProto())) &&
1254+
(Current.is(tok::greater) && (Style.isProto() || Style.isTableGen()))) &&
12541255
State.Stack.size() > 1) {
12551256
if (Current.closesBlockOrBlockTypeList(Style))
12561257
return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
@@ -1278,6 +1279,12 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
12781279
Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace))) {
12791280
return State.Stack[State.Stack.size() - 2].LastSpace;
12801281
}
1282+
// When DAGArg closer exists top of line, it should be aligned in the similar
1283+
// way as function call above.
1284+
if (Style.isTableGen() && Current.is(TT_TableGenDAGArgCloser) &&
1285+
State.Stack.size() > 1) {
1286+
return State.Stack[State.Stack.size() - 2].LastSpace;
1287+
}
12811288
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
12821289
(Current.is(tok::r_paren) ||
12831290
(Current.is(tok::r_brace) && Current.MatchingParen &&
@@ -1696,7 +1703,9 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
16961703
(!Previous || Previous->isNot(tok::kw_return) ||
16971704
(Style.Language != FormatStyle::LK_Java && PrecedenceLevel > 0)) &&
16981705
(Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
1699-
PrecedenceLevel != prec::Comma || Current.NestingLevel == 0)) {
1706+
PrecedenceLevel != prec::Comma || Current.NestingLevel == 0) &&
1707+
(!Style.isTableGen() ||
1708+
(Previous && Previous->is(TT_TableGenDAGArgListComma)))) {
17001709
NewParenState.Indent = std::max(
17011710
std::max(State.Column, NewParenState.Indent), CurrentState.LastSpace);
17021711
}
@@ -1942,6 +1951,7 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
19421951
(Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
19431952
(Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
19441953
State.NextToken->is(TT_TemplateCloser) ||
1954+
State.NextToken->is(TT_TableGenListCloser) ||
19451955
(Current.is(tok::greater) && Current.is(TT_DictLiteral)))) {
19461956
State.Stack.pop_back();
19471957
}

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5072,7 +5072,38 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
50725072
Left.endsSequence(tok::greatergreater, tok::l_brace))) {
50735073
return false;
50745074
}
5075+
} else if (Style.isTableGen()) {
5076+
// Avoid to connect [ and {. [{ is start token of multiline string.
5077+
if (Left.is(tok::l_square) && Right.is(tok::l_brace))
5078+
return true;
5079+
if (Left.is(tok::r_brace) && Right.is(tok::r_square))
5080+
return true;
5081+
// Do not insert around colon in DAGArg and cond operator.
5082+
if (Right.is(TT_TableGenDAGArgListColon) ||
5083+
Left.is(TT_TableGenDAGArgListColon)) {
5084+
return false;
5085+
}
5086+
if (Right.is(TT_TableGenCondOperatorColon))
5087+
return false;
5088+
// Do not insert bang operators and consequent openers.
5089+
if (Right.isOneOf(tok::l_paren, tok::less) &&
5090+
Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5091+
return false;
5092+
}
5093+
// Trailing paste requires space before '{' or ':', the case in name values.
5094+
// Not before ';', the case in normal values.
5095+
if (Left.is(TT_TableGenTrailingPasteOperator) &&
5096+
Right.isOneOf(tok::l_brace, tok::colon)) {
5097+
return true;
5098+
}
5099+
// Otherwise paste operator does not prefer space around.
5100+
if (Left.is(tok::hash) || Right.is(tok::hash))
5101+
return false;
5102+
// Sure not to connect after defining keywords.
5103+
if (Keywords.isTableGenDefinition(Left))
5104+
return true;
50755105
}
5106+
50765107
if (Left.is(TT_ImplicitStringLiteral))
50775108
return Right.hasWhitespaceBefore();
50785109
if (Line.Type == LT_ObjCMethodDecl) {
@@ -5424,6 +5455,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54245455
return Style.BreakArrays;
54255456
}
54265457
}
5458+
if (Style.isTableGen()) {
5459+
// Break the comma in side cond operators.
5460+
// !cond(case1:1,
5461+
// case2:0);
5462+
if (Left.is(TT_TableGenCondOperatorComma))
5463+
return true;
5464+
}
54275465

54285466
if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
54295467
Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
@@ -5822,6 +5860,21 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
58225860
return false;
58235861
if (Left.is(TT_TemplateString) && Left.opensScope())
58245862
return true;
5863+
} else if (Style.isTableGen()) {
5864+
// Avoid to break after "def", "class", "let" and so on.
5865+
if (Keywords.isTableGenDefinition(Left))
5866+
return false;
5867+
// Avoid to break after '(' in the cases that is in bang operators.
5868+
if (Right.is(tok::l_paren)) {
5869+
return !Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
5870+
TT_TemplateCloser);
5871+
}
5872+
// Avoid to break between the value and its suffix part.
5873+
if (Left.is(TT_TableGenValueSuffix))
5874+
return false;
5875+
// Avoid to break around paste operator.
5876+
if (Left.is(tok::hash) || Right.is(tok::hash))
5877+
return false;
58255878
}
58265879

58275880
if (Left.is(tok::at))

0 commit comments

Comments
 (0)