From add3811f46f4eb38cacd30ca7817160868da3e00 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sat, 10 Jul 2021 12:03:51 +0100 Subject: [PATCH] Internals: Fix debug prints racing with option parsing. debug() declared by VL_DEGUB_FUNC used to cache the result of the debug level lookup (which depends on options) in a static. This meant that if the debug() function was called before option parsing, the default debug level of 0 would be used for the rest of the program, even if a --debug option was given. Fixed by not caching the debug level until after option parsing is complete. --- src/V3Error.h | 6 +++++- src/V3Options.cpp | 3 +++ src/V3Options.h | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/V3Error.h b/src/V3Error.h index 870893c8e..cac812106 100644 --- a/src/V3Error.h +++ b/src/V3Error.h @@ -417,7 +417,11 @@ inline void v3errorEndFatal(std::ostringstream& sstr) { #define VL_DEBUG_FUNC \ static int debug() { \ static int level = -1; \ - if (VL_UNLIKELY(level < 0)) level = v3Global.opt.debugSrcLevel(__FILE__); \ + if (VL_UNLIKELY(level < 0)) { \ + const int debugSrcLevel = v3Global.opt.debugSrcLevel(__FILE__); \ + if (!v3Global.opt.available()) return debugSrcLevel; \ + level = debugSrcLevel; \ + } \ return level; \ } diff --git a/src/V3Options.cpp b/src/V3Options.cpp index f5e141701..8e52d8761 100644 --- a/src/V3Options.cpp +++ b/src/V3Options.cpp @@ -791,6 +791,9 @@ void V3Options::notify() { if (coverage() && savable()) { cmdfl->v3error("--coverage and --savable not supported together"); } + + // Mark options as available + m_available = true; } //###################################################################### diff --git a/src/V3Options.h b/src/V3Options.h index 3668fe605..6d42ccabe 100644 --- a/src/V3Options.h +++ b/src/V3Options.h @@ -361,6 +361,8 @@ private: bool m_oTable; // main switch: -Oa: lookup table creation // clang-format on + bool m_available = false; // Set to true at the end of option parsing + private: // METHODS void addArg(const string& arg); @@ -402,6 +404,7 @@ public: void addVFile(const string& filename); void addForceInc(const string& filename); void notify(); + bool available() const { return m_available; } // ACCESSORS (options) bool preprocOnly() const { return m_preprocOnly; }