Skip to content

Commit cb54026

Browse files
authored
[clang-format] Recognize wait fork in Verilog (#132042)
before ```Verilog wait fork ; wait fork ; wait fork ; ``` after ```Verilog wait fork; wait fork; wait fork; ``` The `wait fork` statement should not start a block. Previously the formatter treated the `fork` part as the start of a new block. Now the problem is fixed.
1 parent 9b32f3d commit cb54026

File tree

2 files changed

+53
-66
lines changed

2 files changed

+53
-66
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ struct AdditionalKeywords {
12431243
kw_unique0 = &IdentTable.get("unique0");
12441244
kw_uwire = &IdentTable.get("uwire");
12451245
kw_vectored = &IdentTable.get("vectored");
1246+
kw_wait = &IdentTable.get("wait");
12461247
kw_wand = &IdentTable.get("wand");
12471248
kw_weak0 = &IdentTable.get("weak0");
12481249
kw_weak1 = &IdentTable.get("weak1");
@@ -1299,70 +1300,49 @@ struct AdditionalKeywords {
12991300
// treatment like `showcancelled` or they should be treated as identifiers
13001301
// like `int` and `logic`.
13011302
VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1302-
{kw_always, kw_always_comb,
1303-
kw_always_ff, kw_always_latch,
1304-
kw_assert, kw_assign,
1305-
kw_assume, kw_automatic,
1306-
kw_before, kw_begin,
1307-
kw_bins, kw_binsof,
1308-
kw_casex, kw_casez,
1309-
kw_celldefine, kw_checker,
1310-
kw_clocking, kw_constraint,
1311-
kw_cover, kw_covergroup,
1312-
kw_coverpoint, kw_disable,
1313-
kw_dist, kw_edge,
1314-
kw_end, kw_endcase,
1315-
kw_endchecker, kw_endclass,
1316-
kw_endclocking, kw_endfunction,
1317-
kw_endgenerate, kw_endgroup,
1318-
kw_endinterface, kw_endmodule,
1319-
kw_endpackage, kw_endprimitive,
1320-
kw_endprogram, kw_endproperty,
1321-
kw_endsequence, kw_endspecify,
1322-
kw_endtable, kw_endtask,
1323-
kw_extends, kw_final,
1324-
kw_foreach, kw_forever,
1325-
kw_fork, kw_function,
1326-
kw_generate, kw_highz0,
1327-
kw_highz1, kw_iff,
1328-
kw_ifnone, kw_ignore_bins,
1329-
kw_illegal_bins, kw_implements,
1330-
kw_import, kw_initial,
1331-
kw_inout, kw_input,
1332-
kw_inside, kw_interconnect,
1333-
kw_interface, kw_intersect,
1334-
kw_join, kw_join_any,
1335-
kw_join_none, kw_large,
1336-
kw_let, kw_local,
1337-
kw_localparam, kw_macromodule,
1338-
kw_matches, kw_medium,
1339-
kw_negedge, kw_output,
1340-
kw_package, kw_packed,
1341-
kw_parameter, kw_posedge,
1342-
kw_primitive, kw_priority,
1343-
kw_program, kw_property,
1344-
kw_pull0, kw_pull1,
1345-
kw_pure, kw_rand,
1346-
kw_randc, kw_randcase,
1347-
kw_randsequence, kw_ref,
1348-
kw_repeat, kw_sample,
1349-
kw_scalared, kw_sequence,
1350-
kw_small, kw_soft,
1351-
kw_solve, kw_specify,
1352-
kw_specparam, kw_strong0,
1353-
kw_strong1, kw_supply0,
1354-
kw_supply1, kw_table,
1355-
kw_tagged, kw_task,
1356-
kw_tri, kw_tri0,
1357-
kw_tri1, kw_triand,
1358-
kw_trior, kw_trireg,
1359-
kw_unique, kw_unique0,
1360-
kw_uwire, kw_var,
1361-
kw_vectored, kw_wand,
1362-
kw_weak0, kw_weak1,
1363-
kw_wildcard, kw_wire,
1364-
kw_with, kw_wor,
1365-
kw_verilogHash, kw_verilogHashHash});
1303+
{kw_always, kw_always_comb, kw_always_ff,
1304+
kw_always_latch, kw_assert, kw_assign,
1305+
kw_assume, kw_automatic, kw_before,
1306+
kw_begin, kw_bins, kw_binsof,
1307+
kw_casex, kw_casez, kw_celldefine,
1308+
kw_checker, kw_clocking, kw_constraint,
1309+
kw_cover, kw_covergroup, kw_coverpoint,
1310+
kw_disable, kw_dist, kw_edge,
1311+
kw_end, kw_endcase, kw_endchecker,
1312+
kw_endclass, kw_endclocking, kw_endfunction,
1313+
kw_endgenerate, kw_endgroup, kw_endinterface,
1314+
kw_endmodule, kw_endpackage, kw_endprimitive,
1315+
kw_endprogram, kw_endproperty, kw_endsequence,
1316+
kw_endspecify, kw_endtable, kw_endtask,
1317+
kw_extends, kw_final, kw_foreach,
1318+
kw_forever, kw_fork, kw_function,
1319+
kw_generate, kw_highz0, kw_highz1,
1320+
kw_iff, kw_ifnone, kw_ignore_bins,
1321+
kw_illegal_bins, kw_implements, kw_import,
1322+
kw_initial, kw_inout, kw_input,
1323+
kw_inside, kw_interconnect, kw_interface,
1324+
kw_intersect, kw_join, kw_join_any,
1325+
kw_join_none, kw_large, kw_let,
1326+
kw_local, kw_localparam, kw_macromodule,
1327+
kw_matches, kw_medium, kw_negedge,
1328+
kw_output, kw_package, kw_packed,
1329+
kw_parameter, kw_posedge, kw_primitive,
1330+
kw_priority, kw_program, kw_property,
1331+
kw_pull0, kw_pull1, kw_pure,
1332+
kw_rand, kw_randc, kw_randcase,
1333+
kw_randsequence, kw_ref, kw_repeat,
1334+
kw_sample, kw_scalared, kw_sequence,
1335+
kw_small, kw_soft, kw_solve,
1336+
kw_specify, kw_specparam, kw_strong0,
1337+
kw_strong1, kw_supply0, kw_supply1,
1338+
kw_table, kw_tagged, kw_task,
1339+
kw_tri, kw_tri0, kw_tri1,
1340+
kw_triand, kw_trior, kw_trireg,
1341+
kw_unique, kw_unique0, kw_uwire,
1342+
kw_var, kw_vectored, kw_wait,
1343+
kw_wand, kw_weak0, kw_weak1,
1344+
kw_wildcard, kw_wire, kw_with,
1345+
kw_wor, kw_verilogHash, kw_verilogHashHash});
13661346

13671347
TableGenExtraKeywords = std::unordered_set<IdentifierInfo *>({
13681348
kw_assert,
@@ -1614,6 +1594,7 @@ struct AdditionalKeywords {
16141594
IdentifierInfo *kw_unique0;
16151595
IdentifierInfo *kw_uwire;
16161596
IdentifierInfo *kw_vectored;
1597+
IdentifierInfo *kw_wait;
16171598
IdentifierInfo *kw_wand;
16181599
IdentifierInfo *kw_weak0;
16191600
IdentifierInfo *kw_weak1;
@@ -1849,8 +1830,12 @@ struct AdditionalKeywords {
18491830
/// Returns whether \p Tok is a Verilog keyword that opens a block.
18501831
bool isVerilogBegin(const FormatToken &Tok) const {
18511832
// `table` is not included since it needs to be treated specially.
1852-
return !Tok.endsSequence(kw_fork, kw_disable) &&
1853-
Tok.isOneOf(kw_begin, kw_fork, kw_generate, kw_specify);
1833+
if (Tok.isOneOf(kw_begin, kw_generate, kw_specify))
1834+
return true;
1835+
if (Tok.isNot(kw_fork))
1836+
return false;
1837+
const auto *Prev = Tok.getPreviousNonComment();
1838+
return !(Prev && Prev->isOneOf(kw_disable, kw_wait));
18541839
}
18551840

18561841
/// Returns whether \p Tok is a Verilog keyword that closes a block.

clang/unittests/Format/FormatTestVerilog.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ TEST_F(FormatTestVerilog, Block) {
160160
// Test that 'disable fork' and 'rand join' don't get mistaken as blocks.
161161
verifyFormat("disable fork;\n"
162162
"x = x;");
163+
verifyFormat("wait fork;\n"
164+
"x = x;");
163165
verifyFormat("rand join x x;\n"
164166
"x = x;");
165167
// The begin keyword should not be indented if it is too long to fit on the

0 commit comments

Comments
 (0)