From 4eb280601ea6ec14e1ad2387af3cb8a8906f3f44 Mon Sep 17 00:00:00 2001 From: Todd Strader Date: Thu, 9 Feb 2023 10:09:00 -0500 Subject: [PATCH] handle constant format field widths (#3946) --- src/V3Simulate.h | 9 ++++++++- test_regress/t/t_const_string_func.v | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/V3Simulate.h b/src/V3Simulate.h index 90f846cfb..d6ea92e26 100644 --- a/src/V3Simulate.h +++ b/src/V3Simulate.h @@ -1084,12 +1084,19 @@ private: const string format = nodep->text(); auto pos = format.cbegin(); bool inPct = false; + string width; for (; pos != format.cend(); ++pos) { if (!inPct && pos[0] == '%') { inPct = true; + width = ""; } else if (!inPct) { // Normal text result += *pos; } else { // Format character + if (std::isdigit(pos[0])) { + width += pos[0]; + continue; + } + inPct = false; if (V3Number::displayedFmtLegal(tolower(pos[0]), false)) { @@ -1101,7 +1108,7 @@ private: nodep, "Argument for $display like statement is not constant"); break; } - const string pformat = std::string{"%"} + pos[0]; + const string pformat = std::string{"%"} + width + pos[0]; result += constp->num().displayed(nodep, pformat); } else { switch (tolower(pos[0])) { diff --git a/test_regress/t/t_const_string_func.v b/test_regress/t/t_const_string_func.v index c3ed6d12b..d410d7a27 100644 --- a/test_regress/t/t_const_string_func.v +++ b/test_regress/t/t_const_string_func.v @@ -8,12 +8,15 @@ module t (); function automatic string foo_func(); foo_func = "FOO"; + foo_func = $sformatf("%sBAR", foo_func); + for (int i = 0; i < 4; i++) + foo_func = $sformatf("%s%0d", foo_func, i); endfunction localparam string the_foo = foo_func(); initial begin - if (the_foo != "FOO") $stop; + if (the_foo != "FOOBAR0123") $stop; $write("*-* All Finished *-*\n"); $finish; end