handle constant format field widths (#3946)

This commit is contained in:
Todd Strader 2023-02-09 10:09:00 -05:00 committed by GitHub
parent b778784333
commit 4eb280601e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -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])) {

View File

@ -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