From 5cf83fe89203ad4f508771fb12cac9433c56adba Mon Sep 17 00:00:00 2001 From: David Fang Date: Thu, 23 Apr 2020 11:34:45 -0700 Subject: [PATCH] Attach ';' to macro-call statements' partitions. fixes #278 PiperOrigin-RevId: 308096014 --- verilog/formatting/formatter_test.cc | 24 +++++++++++++++++ verilog/formatting/tree_unwrapper.cc | 5 ++++ verilog/formatting/tree_unwrapper_test.cc | 32 +++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/verilog/formatting/formatter_test.cc b/verilog/formatting/formatter_test.cc index fb1ae58fb..06089ac1a 100644 --- a/verilog/formatting/formatter_test.cc +++ b/verilog/formatting/formatter_test.cc @@ -2258,6 +2258,30 @@ static const std::initializer_list kFormatterTestCases = { "task t;\n" " ##`DELAY_VALUE;\n" "endtask\n"}, + {"task t;\n" + "`uvm_error( foo,bar);\n" + "endtask\n", + "task t;\n" + " `uvm_error(foo, bar);\n" + "endtask\n"}, + {"task t;\n" + "`uvm_error(foo,bar)\n" + ";\n" // null statement + "endtask\n", + "task t;\n" + " `uvm_error(foo, bar)\n" + " ;\n" + "endtask\n"}, + {"task t;\n" + "if(expr)begin\t\n" + "`uvm_error(foo,bar);\n" + "end\n" + "endtask\n", + "task t;\n" + " if (expr) begin\n" + " `uvm_error(foo, bar);\n" + " end\n" + "endtask\n"}, {"task\nrabbit;$kill(the,\nrabbit)\n;endtask: rabbit\n", "task rabbit;\n" " $kill(the, rabbit);\n" diff --git a/verilog/formatting/tree_unwrapper.cc b/verilog/formatting/tree_unwrapper.cc index d52d5dc9f..b40490634 100644 --- a/verilog/formatting/tree_unwrapper.cc +++ b/verilog/formatting/tree_unwrapper.cc @@ -1256,6 +1256,11 @@ void TreeUnwrapper::ReshapeTokenPartitions( AttachTrailingSemicolonToPreviousPartition(&partition); break; } + case NodeEnum::kStatement: { + // This handles cases like macro-calls followed by a semicolon. + AttachTrailingSemicolonToPreviousPartition(&partition); + break; + } case NodeEnum::kModuleHeader: { // If there were any parameters or ports at all, expand. // TODO(fangism): This should be done by inspecting the CST node, diff --git a/verilog/formatting/tree_unwrapper_test.cc b/verilog/formatting/tree_unwrapper_test.cc index 63066c205..a59fd3e44 100644 --- a/verilog/formatting/tree_unwrapper_test.cc +++ b/verilog/formatting/tree_unwrapper_test.cc @@ -2440,6 +2440,38 @@ const TreeUnwrapperTestData kUnwrapUvmTestCases[] = { N(2, L(2, {"l1b", ","}), L(2, {"UVM_DEFAULT", ")"}))), L(0, {"`uvm_field_utils_end"}), }, + + { + "uvm macro statement test, with semicolon on same line", + "task t;\n" + "`uvm_error(foo, bar);\n" + "endtask\n", + TaskDeclaration(0, TaskHeader(0, {"task", "t", ";"}), + N(1, // + L(1, {"`uvm_error", "("}), // + N(3, // + L(3, {"foo", ","}), // + L(3, {"bar", ")", ";"}))), + L(0, {"endtask"})), + }, + { + "uvm macro statement test, detached null statement semicolon", + "task t;\n" + "`uvm_error(foo, bar)\n" + ";\n" + "endtask\n", + TaskDeclaration( + 0, TaskHeader(0, {"task", "t", ";"}), + StatementList(1, + N(1, // + L(1, {"`uvm_error", "("}), // + N(3, // + L(3, {"foo", ","}), // + L(3, {"bar", ")"}))), // + L(1, {";"}) // null statement stays detached + ), + L(0, {"endtask"})), + }, }; // Test that TreeUnwrapper produces the correct UnwrappedLines from code with