Attr file:line.

This commit is contained in:
nella 2026-07-29 11:14:55 +02:00 committed by nella
parent c6d38a6a1b
commit c82a024b20
5 changed files with 27 additions and 2 deletions

View File

@ -36,6 +36,7 @@
#include "verilog_frontend.h"
#include "kernel/log.h"
#include "kernel/register.h"
#include <string.h>
#include <math.h>
@ -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);
}

View File

@ -459,6 +459,8 @@ void Frontend::execute(std::vector<std::string> 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<std::string> args, size_t argidx, bool bin_input)
{
@ -496,6 +498,7 @@ void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector<s
if (buffer.size() > 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;

View File

@ -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());

View File

@ -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);

View File

@ -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:*