diff --git a/llvm/include/llvm/Support/DebugCounter.h b/llvm/include/llvm/Support/DebugCounter.h --- a/llvm/include/llvm/Support/DebugCounter.h +++ b/llvm/include/llvm/Support/DebugCounter.h @@ -55,8 +55,6 @@ class DebugCounter { public: - ~DebugCounter(); - /// Returns a reference to the singleton instance. static DebugCounter &instance(); @@ -150,6 +148,8 @@ static void enableAllCounters() { instance().Enabled = true; } private: + struct Owner; + static bool isCountingEnabled() { // Compile to nothing when debugging is off #ifdef NDEBUG diff --git a/llvm/lib/Support/DebugCounter.cpp b/llvm/lib/Support/DebugCounter.cpp --- a/llvm/lib/Support/DebugCounter.cpp +++ b/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,43 @@ } }; -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())); +} // anonymous namespace + +// All global objects associated to the DebugCounter, including the DebugCounter +// itself, are owned by a single global instance of the Owner struct. This +// makes it easier to control the order in which constructors and destructors +// are run. +struct DebugCounter::Owner { + 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 PrintDebugCounter{ + "print-debug-counter", cl::Hidden, cl::init(false), cl::Optional, + cl::desc("Print out debug counter info after all counters accumulated")}; + + Owner() { + // Our destructor uses the debug stream. By referencing it here, we + // ensure that its destructor runs after our destructor. + (void)dbgs(); } -}; -} // namespace - -static ManagedStatic - DebugCounterOption; -static bool PrintDebugCounter; - -void llvm::initDebugCounterOptions() { - *DebugCounterOption; - static cl::opt 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 DC; + // Print information when destroyed, iff command line option is specified. + ~Owner() { + if (DC.isCountingEnabled() && PrintDebugCounter) + DC.print(dbgs()); + } -// Print information when destroyed, iff command line option is specified. -DebugCounter::~DebugCounter() { - if (isCountingEnabled() && PrintDebugCounter) - print(dbgs()); -} + static Owner &instance() { + static Owner O; + return O; + } +}; + +void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); } -DebugCounter &DebugCounter::instance() { return *DC; } +DebugCounter &DebugCounter::instance() { return Owner::instance().DC; } // This is called by the command line parser when it sees a value for the // debug-counter option defined above.