Skip to content

Commit b3e80d8

Browse files
authored
[clang-format] Add space in Verilog tagged unions (llvm#71354)
In a tagged union expression, there should be a space between the field name and the data. Previously, the tag could be recognized as part of a dotted identifier or a struct literal, and the space would be omitted.
1 parent 5602636 commit b3e80d8

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4721,8 +4721,15 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
47214721
Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
47224722
return true;
47234723
}
4724+
// In a tagged union expression, there should be a space after the tag.
4725+
if (Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
4726+
Keywords.isVerilogIdentifier(Left) && Left.getPreviousNonComment() &&
4727+
Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
4728+
return true;
4729+
}
47244730
// Don't add spaces between a casting type and the quote or repetition count
4725-
// and the brace.
4731+
// and the brace. The case of tagged union expressions is handled by the
4732+
// previous rule.
47264733
if ((Right.is(Keywords.kw_apostrophe) ||
47274734
(Right.is(BK_BracedInit) && Right.is(tok::l_brace))) &&
47284735
!(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||

clang/unittests/Format/FormatTestVerilog.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ TEST_F(FormatTestVerilog, Case) {
373373
" arg);\n"
374374
"endcase",
375375
Style);
376+
377+
verifyFormat("case (v) matches\n"
378+
" tagged Valid .n:\n"
379+
" ;\n"
380+
"endcase");
376381
}
377382

378383
TEST_F(FormatTestVerilog, Coverage) {
@@ -1292,12 +1297,17 @@ TEST_F(FormatTestVerilog, StructLiteral) {
12921297
verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};");
12931298
verifyFormat("c = '{a: 0, b: 0.0};");
12941299
verifyFormat("c = '{a: 0, b: 0.0, default: 0};");
1300+
verifyFormat("d = {int: 1, shortreal: 1.0};");
1301+
verifyFormat("c = '{default: 0};");
1302+
1303+
// The identifier before the quote can be either a tag or a type case. There
1304+
// should be a space between the tag and the quote.
12951305
verifyFormat("c = ab'{a: 0, b: 0.0};");
12961306
verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};");
12971307
verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};");
1298-
verifyFormat("d = {int: 1, shortreal: 1.0};");
12991308
verifyFormat("d = ab'{int: 1, shortreal: 1.0};");
1300-
verifyFormat("c = '{default: 0};");
1309+
verifyFormat("x = tagged Add '{e1, 4, ed};");
1310+
13011311
auto Style = getDefaultStyle();
13021312
Style.SpacesInContainerLiterals = true;
13031313
verifyFormat("c = '{a : 0, b : 0.0};", Style);

0 commit comments

Comments
 (0)