From 2b5755178ff206978b0d587458669be4ca976c0f Mon Sep 17 00:00:00 2001 From: nella Date: Wed, 29 Jul 2026 11:02:59 +0200 Subject: [PATCH] Clean up diagnostics. --- frontends/verilog/const2ast.cc | 14 ++++++++++---- frontends/verilog/verilog_frontend.h | 2 ++ tests/various/const_cmdline.ys | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/various/const_cmdline.ys diff --git a/frontends/verilog/const2ast.cc b/frontends/verilog/const2ast.cc index 43d36cf2f..7093b97ee 100644 --- a/frontends/verilog/const2ast.cc +++ b/frontends/verilog/const2ast.cc @@ -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 &data, const char *str, // convert the Verilog code for a constant to an AST node std::unique_ptr 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()) diff --git a/frontends/verilog/verilog_frontend.h b/frontends/verilog/verilog_frontend.h index 83c0e37a1..112f0977a 100644 --- a/frontends/verilog/verilog_frontend.h +++ b/frontends/verilog/verilog_frontend.h @@ -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); diff --git a/tests/various/const_cmdline.ys b/tests/various/const_cmdline.ys new file mode 100644 index 000000000..5874c2ecf --- /dev/null +++ b/tests/various/const_cmdline.ys @@ -0,0 +1,15 @@ +# https://github.com/YosysHQ/yosys/issues/6009 +read_verilog <