diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2023-04-14 21:41:27 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2023-06-22 18:20:56 +0000 |
commit | bdd1243df58e60e85101c09001d9812a789b6bc4 (patch) | |
tree | a1ce621c7301dd47ba2ddc3b8eaa63b441389481 /contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp | |
parent | 781624ca2d054430052c828ba8d2c2eaf2d733e7 (diff) | |
parent | e3b557809604d036af6e00c60f012c2025b59a5e (diff) |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp')
-rw-r--r-- | contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp b/contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp index bc2df37e773d..26293bf92a42 100644 --- a/contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp +++ b/contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp @@ -4,7 +4,6 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Format.h" -#include "llvm/Support/ManagedStatic.h" using namespace llvm; @@ -44,37 +43,41 @@ private: } }; -struct CreateDebugCounterOption { - static void *call() { - return new DebugCounterList( - "debug-counter", cl::Hidden, - cl::desc("Comma separated list of debug counter skip and count"), - cl::CommaSeparated, cl::location(DebugCounter::instance())); +// All global objects associated to the DebugCounter, including the DebugCounter +// itself, are owned by a single global instance of the DebugCounterOwner +// struct. This makes it easier to control the order in which constructors and +// destructors are run. +struct DebugCounterOwner { + DebugCounter DC; + DebugCounterList DebugCounterOption{ + "debug-counter", cl::Hidden, + cl::desc("Comma separated list of debug counter skip and count"), + cl::CommaSeparated, cl::location(DC)}; + cl::opt<bool> PrintDebugCounter{ + "print-debug-counter", cl::Hidden, cl::init(false), cl::Optional, + cl::desc("Print out debug counter info after all counters accumulated")}; + + DebugCounterOwner() { + // Our destructor uses the debug stream. By referencing it here, we + // ensure that its destructor runs after our destructor. + (void)dbgs(); + } + + // Print information when destroyed, iff command line option is specified. + ~DebugCounterOwner() { + if (DC.isCountingEnabled() && PrintDebugCounter) + DC.print(dbgs()); } }; -} // namespace - -static ManagedStatic<DebugCounterList, CreateDebugCounterOption> - DebugCounterOption; -static bool PrintDebugCounter; - -void llvm::initDebugCounterOptions() { - *DebugCounterOption; - static cl::opt<bool, true> RegisterPrintDebugCounter( - "print-debug-counter", cl::Hidden, cl::location(PrintDebugCounter), - cl::init(false), cl::Optional, - cl::desc("Print out debug counter info after all counters accumulated")); -} -static ManagedStatic<DebugCounter> DC; +} // anonymous namespace -// Print information when destroyed, iff command line option is specified. -DebugCounter::~DebugCounter() { - if (isCountingEnabled() && PrintDebugCounter) - print(dbgs()); -} +void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); } -DebugCounter &DebugCounter::instance() { return *DC; } +DebugCounter &DebugCounter::instance() { + static DebugCounterOwner O; + return O.DC; +} // This is called by the command line parser when it sees a value for the // debug-counter option defined above. |