From 8cd3c069d662d12e727f75bb40bbfdc5d05e0308 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Fri, 12 Sep 2025 06:13:13 +0000 Subject: [PATCH] Use C++ stringf machinery in verilog_error --- frontends/verilog/verilog_error.cc | 35 +++++------------------------- frontends/verilog/verilog_error.h | 16 ++++++++++++-- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/frontends/verilog/verilog_error.cc b/frontends/verilog/verilog_error.cc index 491b8c7f5..b968b3e21 100644 --- a/frontends/verilog/verilog_error.cc +++ b/frontends/verilog/verilog_error.cc @@ -32,37 +32,14 @@ USING_YOSYS_NAMESPACE */ [[noreturn]] -static void verr_at(std::string filename, int begin_line, char const *fmt, va_list ap) +void VERILOG_FRONTEND::formatted_err_at_loc(Location loc, std::string str) { - char buffer[1024]; - char *p = buffer; - p += vsnprintf(p, buffer + sizeof(buffer) - p, fmt, ap); - p += snprintf(p, buffer + sizeof(buffer) - p, "\n"); - YOSYS_NAMESPACE_PREFIX log_file_error(filename, begin_line, "%s", buffer); - exit(1); + YOSYS_NAMESPACE_PREFIX log_file_error(loc.begin.filename ? *(loc.begin.filename) : "UNKNOWN", loc.begin.line, + "%s\n", std::move(str)); } -static void vwarn_at(std::string filename, int begin_line, char const *fmt, va_list ap) +void VERILOG_FRONTEND::formatted_warn_at_loc(Location loc, std::string str) { - char buffer[1024]; - char *p = buffer; - p += vsnprintf(p, buffer + sizeof(buffer) - p, fmt, ap); - p += snprintf(p, buffer + sizeof(buffer) - p, "\n"); - YOSYS_NAMESPACE_PREFIX log_file_warning(filename, begin_line, "%s", buffer); + YOSYS_NAMESPACE_PREFIX log_file_warning(loc.begin.filename ? *(loc.begin.filename) : "UNKNOWN", loc.begin.line, + "%s\n", std::move(str)); } - -[[noreturn]] -void VERILOG_FRONTEND::err_at_loc(Location loc, char const *fmt, ...) -{ - va_list args; - va_start(args, fmt); - verr_at(loc.begin.filename ? *(loc.begin.filename) : "UNKNOWN", loc.begin.line, fmt, args); -} -void VERILOG_FRONTEND::warn_at_loc(Location loc, char const *fmt, ...) -{ - va_list args; - va_start(args, fmt); - vwarn_at(loc.begin.filename ? *(loc.begin.filename) : "UNKNOWN", loc.begin.line, fmt, args); - va_end(args); -} - diff --git a/frontends/verilog/verilog_error.h b/frontends/verilog/verilog_error.h index b36de19b8..ede489b26 100644 --- a/frontends/verilog/verilog_error.h +++ b/frontends/verilog/verilog_error.h @@ -10,8 +10,20 @@ YOSYS_NAMESPACE_BEGIN namespace VERILOG_FRONTEND { [[noreturn]] - void err_at_loc(Location loc, char const *fmt, ...); - void warn_at_loc(Location loc, char const *fmt, ...); + void formatted_err_at_loc(Location loc, std::string str); + template + [[noreturn]] + void err_at_loc(Location loc, FmtString...> fmt, const Args &... args) + { + formatted_err_at_loc(std::move(loc), fmt.format(args...)); + } + + void formatted_warn_at_loc(Location loc, std::string str); + template + void warn_at_loc(Location loc, FmtString...> fmt, const Args &... args) + { + formatted_warn_at_loc(std::move(loc), fmt.format(args...)); + } }; YOSYS_NAMESPACE_END