Skip to content

Commit

Permalink
Improve hex too many digits error (verilator#5419).
Browse files Browse the repository at this point in the history
  • Loading branch information
wsnyder committed Aug 30, 2024
1 parent cdfb222 commit 0ec5e02
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
24 changes: 14 additions & 10 deletions src/V3Number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,7 @@ void V3Number::create(const char* sourcep) {
product.opMul(*this, ten);
opAdd(product, addend);
if (product.bitsValue(width(), 4)) { // Overflowed
static int warned = 0;
v3error("Too many digits for "
<< width() << " bit number: " << sourcep << '\n'
<< ((!sized() && !warned++) ? (
V3Error::warnMore() + "... As that number was unsized"
+ " ('d...) it is limited to 32 bits"
" (IEEE 1800-2023 5.7.1)\n"
+ V3Error::warnMore() + "... Suggest adding a size to it.")
: ""));
warnTooMany(sourcep);
while (*(cp + 1)) cp++; // Skip ahead so don't get multiple warnings
}
}
Expand Down Expand Up @@ -291,7 +283,7 @@ void V3Number::create(const char* sourcep) {
for (const char* cp = value_startp + std::strlen(value_startp) - 1; cp >= value_startp;
cp--) {
if (*cp != '_' && *cp != '0' && obit >= width()) {
v3error("Too many digits for " << width() << " bit number: " << sourcep);
warnTooMany(sourcep);
break;
}
switch (std::tolower(base)) {
Expand Down Expand Up @@ -379,6 +371,18 @@ void V3Number::create(const char* sourcep) {
// m_value[0]);
}

void V3Number::warnTooMany(const string& value) {
static int warned = 0;
v3error("Too many digits for "
<< width() << " bit number: '" << value << "'\n"
<< ((!sized() && !warned++)
? (V3Error::warnMore() + "... As that number was unsized"
+ " ('...) it is limited to 32 bits"
" (IEEE 1800-2023 5.7.1)\n"
+ V3Error::warnMore() + "... Suggest adding a size to it.")
: ""));
}

void V3Number::nodep(AstNode* nodep) VL_MT_STABLE {
m_nodep = nodep;
if (!nodep) return;
Expand Down
1 change: 1 addition & 0 deletions src/V3Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ class V3Number final {
string displayed(const string& vformat) const VL_MT_STABLE {
return displayed(m_fileline, vformat);
}
void warnTooMany(const string& value);

public:
void v3errorEnd(const std::ostringstream& sstr) const VL_RELEASE(V3Error::s().m_mutex);
Expand Down
10 changes: 5 additions & 5 deletions test_regress/t/t_const_overflow_bad.out
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
%Error: t/t_const_overflow_bad.v:9:34: Too many digits for 94 bit number: 94'd123456789012345678901234567890
%Error: t/t_const_overflow_bad.v:9:34: Too many digits for 94 bit number: '94'd123456789012345678901234567890'
9 | parameter [200:0] TOO_SMALL = 94'd123456789012345678901234567890;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Error: t/t_const_overflow_bad.v:11:31: Too many digits for 8 bit number: 8'habc
%Error: t/t_const_overflow_bad.v:11:31: Too many digits for 8 bit number: '8'habc'
11 | parameter [200:0] SMALLH = 8'habc;
| ^~~~~~
%Error: t/t_const_overflow_bad.v:12:31: Too many digits for 6 bit number: 6'o1234
%Error: t/t_const_overflow_bad.v:12:31: Too many digits for 6 bit number: '6'o1234'
12 | parameter [200:0] SMALLO = 6'o1234;
| ^~~~~~~
%Error: t/t_const_overflow_bad.v:13:31: Too many digits for 3 bit number: 3'b1111
%Error: t/t_const_overflow_bad.v:13:31: Too many digits for 3 bit number: '3'b1111'
13 | parameter [200:0] SMALLB = 3'b1111;
| ^~~~~~~
%Error: t/t_const_overflow_bad.v:19:35: Too many digits for 129 bit number: 129'hdeadbeefc001f00ddeadbeefc001f00ddeadbeefc001f00ddeadbeefc001f00d
%Error: t/t_const_overflow_bad.v:19:35: Too many digits for 129 bit number: '129'hdeadbeefc001f00ddeadbeefc001f00ddeadbeefc001f00ddeadbeefc001f00d'
19 | parameter [128:0] ALSO_SMALL = 129'hdeadbeefc001f00ddeadbeefc001f00ddeadbeefc001f00ddeadbeefc001f00d;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Error: Exiting due to
16 changes: 14 additions & 2 deletions test_regress/t/t_lint_unsized_bad.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
%Error: t/t_lint_unsized_bad.v:8:22: Too many digits for 32 bit number: 'd123456789123456789123456789
... As that number was unsized ('d...) it is limited to 32 bits (IEEE 1800-2023 5.7.1)
%Error: t/t_lint_unsized_bad.v:8:22: Too many digits for 32 bit number: ''d123456789123456789123456789'
... As that number was unsized ('...) it is limited to 32 bits (IEEE 1800-2023 5.7.1)
... Suggest adding a size to it.
8 | bit [256:0] num = 'd123456789123456789123456789;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Error: t/t_lint_unsized_bad.v:9:22: Too many digits for 32 bit number: ''h123456789123456789123456789'
9 | bit [256:0] num = 'h123456789123456789123456789;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Error: t/t_lint_unsized_bad.v:10:22: Illegal character in octal constant
10 | bit [256:0] num = 'o123456789123456789123456789;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Error: t/t_lint_unsized_bad.v:10:22: Too many digits for 32 bit number: ''o123456789123456789123456789'
10 | bit [256:0] num = 'o123456789123456789123456789;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Error: t/t_lint_unsized_bad.v:11:22: Too many digits for 32 bit number: ''b10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010'
11 | bit [256:0] num = 'b10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Error: Exiting due to
3 changes: 3 additions & 0 deletions test_regress/t/t_lint_unsized_bad.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

module t;
bit [256:0] num = 'd123456789123456789123456789;
bit [256:0] num = 'h123456789123456789123456789;
bit [256:0] num = 'o123456789123456789123456789;
bit [256:0] num = 'b10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010;
endmodule
2 changes: 1 addition & 1 deletion test_regress/t/t_number_bad.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
%Error: t/t_number_bad.v:17:33: Illegal character in binary constant: 4
17 | parameter logic [3:0] FOO7 = 1'b1?4'hF:4'h1;
| ^~~~~~
%Error: t/t_number_bad.v:17:33: Too many digits for 1 bit number: 1'b1?4
%Error: t/t_number_bad.v:17:33: Too many digits for 1 bit number: '1'b1?4'
17 | parameter logic [3:0] FOO7 = 1'b1?4'hF:4'h1;
| ^~~~~~
%Error: t/t_number_bad.v:17:39: syntax error, unexpected INTEGER NUMBER, expecting ';'
Expand Down

0 comments on commit 0ec5e02

Please sign in to comment.