diff --git a/frontends/verilog/const2ast.cc b/frontends/verilog/const2ast.cc index 573af336b..450a3ea1b 100644 --- a/frontends/verilog/const2ast.cc +++ b/frontends/verilog/const2ast.cc @@ -36,6 +36,7 @@ #include "verilog_frontend.h" #include "kernel/log.h" +#include "kernel/register.h" #include #include @@ -45,11 +46,23 @@ using namespace AST; using namespace VERILOG_FRONTEND; void ConstParser::log_maybe_loc_error(std::string msg) { - log_file_error(*loc.begin.filename, loc.begin.line, "%s", msg); + if (loc.begin.filename) + log_file_error(*loc.begin.filename, loc.begin.line, "%s", msg); + else if (!Frontend::current_script_filename.empty()) + log_file_error(Frontend::current_script_filename, Frontend::current_script_lineno, + "Failed to parse constant `%s': %s", code_str, 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.line, "%s", msg); + if (loc.begin.filename) + log_file_warning(*loc.begin.filename, loc.begin.line, "%s", msg); + else if (!Frontend::current_script_filename.empty()) + log_file_warning(Frontend::current_script_filename, Frontend::current_script_lineno, + "While parsing constant `%s': %s", code_str, msg); + else + log_warning("While parsing constant `%s': %s", code_str, msg); } // divide an arbitrary length decimal number by two and return the rest @@ -158,6 +171,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/kernel/register.cc b/kernel/register.cc index db42de008..d3bfd42ad 100644 --- a/kernel/register.cc +++ b/kernel/register.cc @@ -459,6 +459,8 @@ void Frontend::execute(std::vector args, RTLIL::Design *design) FILE *Frontend::current_script_file = NULL; std::string Frontend::last_here_document; +std::string Frontend::current_script_filename; +int Frontend::current_script_lineno = 0; void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector args, size_t argidx, bool bin_input) { @@ -496,6 +498,7 @@ void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector 0 && (buffer[buffer.size() - 1] == '\n' || buffer[buffer.size() - 1] == '\r')) break; } + Frontend::current_script_lineno++; size_t indent = buffer.find_first_not_of(" \t\r\n"); if (indent != std::string::npos && buffer.compare(indent, eot_marker.size(), eot_marker) == 0) break; diff --git a/kernel/register.h b/kernel/register.h index ae3df395a..4b8da06b1 100644 --- a/kernel/register.h +++ b/kernel/register.h @@ -150,6 +150,10 @@ struct Frontend : Pass static FILE *current_script_file; static std::string last_here_document; + // script file and last line read from it + static std::string current_script_filename; + static int current_script_lineno; + std::string frontend_name; Frontend(std::string name, std::string short_help = "** document me **", source_location location = source_location::current()); diff --git a/kernel/yosys.cc b/kernel/yosys.cc index 167052340..d47610800 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -792,15 +792,21 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi log_error("Can't open script file `%s' for reading: %s\n", filename, strerror(errno)); FILE *backup_script_file = Frontend::current_script_file; + std::string backup_script_filename = Frontend::current_script_filename; + int backup_script_lineno = Frontend::current_script_lineno; Frontend::current_script_file = f; + Frontend::current_script_filename = filename; + Frontend::current_script_lineno = 0; try { std::string command; while (fgetline(f, command)) { + Frontend::current_script_lineno++; while (!command.empty() && command[command.size()-1] == '\\') { std::string next_line; if (!fgetline(f, next_line)) break; + Frontend::current_script_lineno++; command.resize(command.size()-1); command += next_line; } @@ -814,6 +820,7 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi if (!command.empty()) { handle_label(command, from_to_active, run_from, run_to); if (from_to_active) { + Frontend::current_script_lineno++; Pass::call(design, command); design->check(); } @@ -821,10 +828,14 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi } catch (...) { Frontend::current_script_file = backup_script_file; + Frontend::current_script_filename = backup_script_filename; + Frontend::current_script_lineno = backup_script_lineno; throw; } Frontend::current_script_file = backup_script_file; + Frontend::current_script_filename = backup_script_filename; + Frontend::current_script_lineno = backup_script_lineno; if (filename != "-") fclose(f); diff --git a/tests/various/const_cmdline.ys b/tests/various/const_cmdline.ys new file mode 100644 index 000000000..b620d9a50 --- /dev/null +++ b/tests/various/const_cmdline.ys @@ -0,0 +1,15 @@ +# https://github.com/YosysHQ/yosys/issues/6009 +read_verilog <