diff --git a/frontends/verilog/const2ast.cc b/frontends/verilog/const2ast.cc index 7093b97ee..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 @@ -47,6 +48,9 @@ using namespace VERILOG_FRONTEND; void ConstParser::log_maybe_loc_error(std::string 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); } @@ -54,6 +58,9 @@ void ConstParser::log_maybe_loc_error(std::string msg) { void ConstParser::log_maybe_loc_warn(std::string 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); } 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 index 5874c2ecf..b620d9a50 100644 --- a/tests/various/const_cmdline.ys +++ b/tests/various/const_cmdline.ys @@ -7,9 +7,9 @@ EOT prep -top top # width mismatch -logger -expect warning "While parsing constant `2'd7'" 1 +logger -expect prefix-warning "const_cmdline.ys:[0-9]+: 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 +logger -expect prefix-error "const_cmdline.ys:[0-9]+: ERROR: Failed to parse constant `8'00000101'" 1 setattr -set foo 8'00000101 t:*