This commit is contained in:
Emil J 2026-07-29 11:17:14 +02:00 committed by GitHub
commit 388e7ebc81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 52 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>
@ -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<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

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

@ -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 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 prefix-error "const_cmdline.ys:[0-9]+: ERROR: Failed to parse constant `8'00000101'" 1
setattr -set foo 8'00000101 t:*