Clean up diagnostics.

This commit is contained in:
nella 2026-07-29 11:02:59 +02:00 committed by nella
parent dfa887637a
commit 2b5755178f
3 changed files with 27 additions and 4 deletions

View File

@ -45,13 +45,17 @@ using namespace AST;
using namespace VERILOG_FRONTEND;
void ConstParser::log_maybe_loc_error(std::string msg) {
log_file_error(loc.begin.filename ? loc.begin.filename->c_str() : "INTERNAL",
loc.begin.line, "%s", msg);
if (loc.begin.filename)
log_file_error(*loc.begin.filename, loc.begin.line, "%s", msg);
else
log_error("Failed to parse constant `%s': %s", code_str, msg);
}
void ConstParser::log_maybe_loc_warn(std::string msg) {
log_file_warning(loc.begin.filename ? loc.begin.filename->c_str() : "INTERNAL",
loc.begin.line, "%s", msg);
if (loc.begin.filename)
log_file_warning(*loc.begin.filename, loc.begin.line, "%s", msg);
else
log_warning("While parsing constant `%s': %s", code_str, msg);
}
// divide an arbitrary length decimal number by two and return the rest
@ -160,6 +164,8 @@ void ConstParser::my_strtobin(std::vector<RTLIL::State> &data, const char *str,
// convert the Verilog code for a constant to an AST node
std::unique_ptr<AstNode> ConstParser::const2ast(std::string code, char case_type, bool warn_z)
{
code_str = code;
if (warn_z) {
auto ret = const2ast(code, case_type);
if (ret != nullptr && std::find(ret->bits.begin(), ret->bits.end(), RTLIL::State::Sz) != ret->bits.end())

View File

@ -42,6 +42,8 @@ namespace VERILOG_FRONTEND
/* Ephemeral context class */
struct ConstParser {
AST::AstSrcLocType loc;
// original constant text, used in diagnostics when loc has no source file
std::string code_str = {};
private:
void log_maybe_loc_error(std::string msg);
void log_maybe_loc_warn(std::string msg);

View File

@ -0,0 +1,15 @@
# https://github.com/YosysHQ/yosys/issues/6009
read_verilog <<EOT
module top(output [7:0] o);
assign o = 8'hAA;
endmodule
EOT
prep -top top
# width mismatch
logger -expect warning "While parsing constant `2'd7'" 1
setattr -set foo 2'd7 t:*
# multi-bit unsized const
logger -expect error "Failed to parse constant `8'00000101'" 1
setattr -set foo 8'00000101 t:*