From 36e5d4c3d5cb22d453a79aa9b8f6a8045ec0108d Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:27:51 +1300 Subject: [PATCH] log_help: Include source_location Use `std::experimental::source_location` because clang support is `??` Add `ENABLE_SOURCE_LOCATION` make variable and corresponding `YOSYS_ENABLE_SOURCE_LOCATION` define. Dummy out the struct if disabled and check for null instead of using `#ifdef` blocks everywhere. --- Makefile | 3 +++ kernel/log_help.cc | 18 ++++++++++++------ kernel/log_help.h | 45 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 55126969e..eb6eb8a38 100644 --- a/Makefile +++ b/Makefile @@ -511,6 +511,9 @@ LIBS_VERIFIC += -Wl,--whole-archive $(patsubst %,$(VERIFIC_DIR)/%/*-linux.a,$(VE endif endif +ifeq ($(ENABLE_SOURCE_LOCATION),1) +CXXFLAGS += -DYOSYS_ENABLE_SOURCE_LOCATION +endif ifeq ($(ENABLE_COVER),1) CXXFLAGS += -DYOSYS_ENABLE_COVER diff --git a/kernel/log_help.cc b/kernel/log_help.cc index a6c6ad778..2b3fb5dfc 100644 --- a/kernel/log_help.cc +++ b/kernel/log_help.cc @@ -78,13 +78,15 @@ bool PrettyHelp::has_content() return false; } -void PrettyHelp::usage(const string &usage) +void PrettyHelp::usage(const string &usage, + const source_location location) { log_pass_str(usage, current_indent+1, true); log("\n"); } -void PrettyHelp::option(const string &text, const string &description) +void PrettyHelp::option(const string &text, const string &description, + const source_location location) { open_option(text); if (description.length()) { @@ -94,23 +96,27 @@ void PrettyHelp::option(const string &text, const string &description) close(1); } -void PrettyHelp::codeblock(const string &code, const string &) +void PrettyHelp::codeblock(const string &code, const string &, + const source_location location) { log("%s\n", code.c_str()); } -void PrettyHelp::paragraph(const string &text) +void PrettyHelp::paragraph(const string &text, + const source_location location) { log_pass_str(text, current_indent); log("\n"); } -void PrettyHelp::open_optiongroup(const string &) +void PrettyHelp::open_optiongroup(const string &, + const source_location location) { current_indent += 1; } -void PrettyHelp::open_option(const string &text) +void PrettyHelp::open_option(const string &text, + const source_location location) { log_pass_str(text, current_indent); current_indent += 1; diff --git a/kernel/log_help.h b/kernel/log_help.h index 850c1516d..ea88c72e0 100644 --- a/kernel/log_help.h +++ b/kernel/log_help.h @@ -23,6 +23,19 @@ #include "kernel/yosys_common.h" #include "kernel/json.h" +#ifdef YOSYS_ENABLE_SOURCE_LOCATION +#include +using std::experimental::source_location; +#else +struct source_location { // dummy placeholder + int line() const { return 0; } + int column() const { return 0; } + const char* file_name() const { return nullptr; } + const char* function_name() const { return nullptr; } + static const source_location current(...) { return source_location(); } +}; +#endif + YOSYS_NAMESPACE_BEGIN class PrettyHelp @@ -37,13 +50,33 @@ public: bool has_content(); - void usage(const string &usage); - void option(const string &text, const string &description = ""); - void codeblock(const string &code, const string &language = "none"); - void paragraph(const string &text); + void usage( + const string &usage, + const source_location location = source_location::current() + ); + void option( + const string &text, + const string &description = "", + const source_location location = source_location::current() + ); + void codeblock( + const string &code, + const string &language = "none", + const source_location location = source_location::current() + ); + void paragraph( + const string &text, + const source_location location = source_location::current() + ); - void open_optiongroup(const string &group = ""); - void open_option(const string &text); + void open_optiongroup( + const string &group = "", + const source_location location = source_location::current() + ); + void open_option( + const string &text, + const source_location location = source_location::current() + ); void close(int levels = 1); };