From 44f02162c2d436f97f86b4ef2df38b066dafe851 Mon Sep 17 00:00:00 2001 From: Lukasz Dalek Date: Thu, 15 Apr 2021 19:04:44 +0200 Subject: [PATCH] formatter: Fix constraint block expansion Constraint block is not expaned when constraint block item list contains only one expression. This is caused by flattening partition with one children and skipping force expansion on partition with one or zero childrens (even with always-expand policy). This patch fixes this by propagation of always-expand policy to its parent, e.g. kConstraintDeclaration. Signed-off-by: Lukasz Dalek --- verilog/formatting/formatter_test.cc | 29 ++++++++++++++++++++++++++-- verilog/formatting/tree_unwrapper.cc | 11 +++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/verilog/formatting/formatter_test.cc b/verilog/formatting/formatter_test.cc index eb9dae1cb..e144cae9c 100644 --- a/verilog/formatting/formatter_test.cc +++ b/verilog/formatting/formatter_test.cc @@ -9240,8 +9240,24 @@ TEST(FormatterEndToEndTest, ConstraintExpressions) { {"constraint only_vec_instr_c {soft only_vec_instr == 0;}", "constraint only_vec_instr_c {soft only_vec_instr == 0;}\n"}, - {"constraint\nnum_trans_c\n\n\n{\n\n\nnum_trans inside{[800:1000]};}", - "constraint num_trans_c {num_trans inside {[800 : 1000]};}\n"}, + // constraint with brackets inside block item list + {"constraint\nnum_trans_c\n\n\n{num_trans inside{[800:1000]};}", + "constraint num_trans_c {\n" + " num_trans inside {[800 : 1000]};\n" + "}\n"}, + + {"constraint data_size_c {data.size() inside {[1 : 65536]};}", + "constraint data_size_c {\n" + " data.size() inside {[1 : 65536]};\n" + "}\n"}, + + {"constraint data_size_c {data.size() inside {[1 : 65536]};a==b;" + "num_trans inside{[800:1000]};}\n", + "constraint data_size_c {\n" + " data.size() inside {[1 : 65536]};\n" + " a == b;\n" + " num_trans inside {[800 : 1000]};\n" + "}\n"}, // if-vs-concatenation expression {"constraint c_operation{ if(fixed_operation_en){" @@ -9280,6 +9296,15 @@ TEST(FormatterEndToEndTest, ConstraintExpressions) { "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;\n" " ccccccccccccccccccccccc == dddddddddddddddddddddd;\n" "}\n"}, + + // FIXME: Find-out why formatter is preserving those newlines in + // constraint block + {"constraint\nnum_trans_c\n\n\n{\n\n\nnum_trans inside{[800:1000]};}", + "constraint num_trans_c {\n" + "\n" + "\n" + " num_trans inside {[800 : 1000]};\n" + "}\n"}, }; FormatStyle style; style.column_limit = diff --git a/verilog/formatting/tree_unwrapper.cc b/verilog/formatting/tree_unwrapper.cc index 11b6a7300..162764066 100644 --- a/verilog/formatting/tree_unwrapper.cc +++ b/verilog/formatting/tree_unwrapper.cc @@ -1680,6 +1680,17 @@ void TreeUnwrapper::ReshapeTokenPartitions( if (PartitionIsCloseBrace(last)) { verible::MergeLeafIntoPreviousLeaf(&last); } + } else if (partition.Children().size() == 3) { + // Propagate partition expansion on flattened partitions + const auto& block_partition = partition.Children()[1]; + const auto block_partition_policy = + block_partition.Value().PartitionPolicy(); + + if (block_partition.Children().size() == 0 && + block_partition_policy == PartitionPolicyEnum::kAlwaysExpand) { + auto& uwline = partition.Value(); + uwline.SetPartitionPolicy(PartitionPolicyEnum::kAlwaysExpand); + } } break; }