diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h --- a/llvm/include/llvm/Support/CommandLine.h +++ b/llvm/include/llvm/Support/CommandLine.h @@ -339,7 +339,10 @@ inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; } public: - virtual ~Option() = default; + virtual ~Option() { + if (FullyInitialized) + removeArgument(); + } // Register this argument with the commandline system. // @@ -347,8 +350,6 @@ /// Unregisters this option from the CommandLine system. /// - /// This option must have been the last option registered. - /// For testing purposes only. void removeArgument(); // Return the width of the option tag for printing... diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -448,7 +448,12 @@ FullyInitialized = true; } -void Option::removeArgument() { GlobalParser->removeOption(this); } +void Option::removeArgument() { + // This method may be called from global destructors. Do nothing if the global + // parser has already been destroyed. + if (auto *Parser = GlobalParser.peek()) + Parser->removeOption(this); +} void Option::setArgStr(StringRef S) { if (FullyInitialized)