Changes in directory llvm/lib/Support:
CommandLine.cpp updated: 1.75 -> 1.76 Timer.cpp updated: 1.45 -> 1.46 --- Log message: Fix more static dtor issues --- Diffs of the changes: (+62 -73) CommandLine.cpp | 104 ++++++++++++++++++++++++-------------------------------- Timer.cpp | 31 ++++++++-------- 2 files changed, 62 insertions(+), 73 deletions(-) Index: llvm/lib/Support/CommandLine.cpp diff -u llvm/lib/Support/CommandLine.cpp:1.75 llvm/lib/Support/CommandLine.cpp:1.76 --- llvm/lib/Support/CommandLine.cpp:1.75 Sun Aug 27 17:10:29 2006 +++ llvm/lib/Support/CommandLine.cpp Wed Oct 4 16:50:14 2006 @@ -18,6 +18,7 @@ #include "llvm/Config/config.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/ManagedStatic.h" #include "llvm/System/Path.h" #include <algorithm> #include <functional> @@ -61,36 +62,23 @@ static const char *ProgramOverview = 0; // This collects additional help to be printed. -static std::vector<const char*> &MoreHelp() { - static std::vector<const char*> moreHelp; - return moreHelp; -} +static ManagedStatic<std::vector<const char*> > MoreHelp; -extrahelp::extrahelp(const char* Help) +extrahelp::extrahelp(const char *Help) : morehelp(Help) { - MoreHelp().push_back(Help); + MoreHelp->push_back(Help); } //===----------------------------------------------------------------------===// // Basic, shared command line option processing machinery. // -// Return the global command line option vector. Making it a function scoped -// static ensures that it will be initialized correctly before its first use. -// -static std::map<std::string, Option*> &getOpts() { - static std::map<std::string, Option*> CommandLineOptions; - return CommandLineOptions; -} +static ManagedStatic<std::map<std::string, Option*> > Options; +static ManagedStatic<std::vector<Option*> > PositionalOptions; static Option *getOption(const std::string &Str) { - std::map<std::string,Option*>::iterator I = getOpts().find(Str); - return I != getOpts().end() ? I->second : 0; -} - -static std::vector<Option*> &getPositionalOpts() { - static std::vector<Option*> Positional; - return Positional; + std::map<std::string,Option*>::iterator I = Options->find(Str); + return I != Options->end() ? I->second : 0; } static void AddArgument(const char *ArgName, Option *Opt) { @@ -99,7 +87,7 @@ << ArgName << "' defined more than once!\n"; } else { // Add argument to the argument map! - getOpts()[ArgName] = Opt; + (*Options)[ArgName] = Opt; } } @@ -107,7 +95,7 @@ // options have already been processed and the map has been deleted! // static void RemoveArgument(const char *ArgName, Option *Opt) { - if(getOpts().empty()) return; + if (Options->empty()) return; #ifndef NDEBUG // This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's @@ -115,7 +103,7 @@ std::string Tmp = ArgName; assert(getOption(Tmp) == Opt && "Arg not in map!"); #endif - getOpts().erase(ArgName); + Options->erase(ArgName); } static inline bool ProvideOption(Option *Handler, const char *ArgName, @@ -303,7 +291,7 @@ if (*Arg == 0) return 0; // Look up the option. - std::map<std::string, Option*> &Opts = getOpts(); + std::map<std::string, Option*> &Opts = *Options; std::map<std::string, Option*>::iterator I = Opts.find(std::string(Arg, ArgEnd)); return (I != Opts.end()) ? I->second : 0; @@ -311,7 +299,7 @@ void cl::ParseCommandLineOptions(int &argc, char **argv, const char *Overview) { - assert((!getOpts().empty() || !getPositionalOpts().empty()) && + assert((!Options->empty() || !PositionalOptions->empty()) && "No options specified, or ParseCommandLineOptions called more" " than once!"); sys::Path progname(argv[0]); @@ -319,8 +307,8 @@ ProgramOverview = Overview; bool ErrorParsing = false; - std::map<std::string, Option*> &Opts = getOpts(); - std::vector<Option*> &PositionalOpts = getPositionalOpts(); + std::map<std::string, Option*> &Opts = *Options; + std::vector<Option*> &PositionalOpts = *PositionalOptions; // Check out the positional arguments to collect information about them. unsigned NumPositionalRequired = 0; @@ -608,9 +596,9 @@ // Free all of the memory allocated to the map. Command line options may only // be processed once! - getOpts().clear(); + Opts.clear(); PositionalOpts.clear(); - MoreHelp().clear(); + MoreHelp->clear(); // If we had an error processing our arguments, don't let the program execute if (ErrorParsing) exit(1); @@ -661,12 +649,12 @@ AddArgument(ArgStr, this); if (getFormattingFlag() == Positional) - getPositionalOpts().push_back(this); + PositionalOptions->push_back(this); else if (getNumOccurrencesFlag() == ConsumeAfter) { - if (!getPositionalOpts().empty() && - getPositionalOpts().front()->getNumOccurrencesFlag() == ConsumeAfter) + if (!PositionalOptions->empty() && + PositionalOptions->front()->getNumOccurrencesFlag() == ConsumeAfter) error("Cannot specify more than one option with cl::ConsumeAfter!"); - getPositionalOpts().insert(getPositionalOpts().begin(), this); + PositionalOptions->insert(PositionalOptions->begin(), this); } } @@ -676,13 +664,13 @@ if (getFormattingFlag() == Positional) { std::vector<Option*>::iterator I = - std::find(getPositionalOpts().begin(), getPositionalOpts().end(), this); - assert(I != getPositionalOpts().end() && "Arg not registered!"); - getPositionalOpts().erase(I); + std::find(PositionalOptions->begin(), PositionalOptions->end(), this); + assert(I != PositionalOptions->end() && "Arg not registered!"); + PositionalOptions->erase(I); } else if (getNumOccurrencesFlag() == ConsumeAfter) { - assert(!getPositionalOpts().empty() && getPositionalOpts()[0] == this && + assert(!PositionalOptions->empty() && (*PositionalOptions)[0] == this && "Arg not registered correctly!"); - getPositionalOpts().erase(getPositionalOpts().begin()); + PositionalOptions->erase(PositionalOptions->begin()); } } @@ -904,22 +892,22 @@ if (Value == false) return; // Copy Options into a vector so we can sort them as we like... - std::vector<std::pair<std::string, Option*> > Options; - copy(getOpts().begin(), getOpts().end(), std::back_inserter(Options)); + std::vector<std::pair<std::string, Option*> > Opts; + copy(Options->begin(), Options->end(), std::back_inserter(Opts)); // Eliminate Hidden or ReallyHidden arguments, depending on ShowHidden - Options.erase(std::remove_if(Options.begin(), Options.end(), - std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)), - Options.end()); + Opts.erase(std::remove_if(Opts.begin(), Opts.end(), + std::ptr_fun(ShowHidden ? isReallyHidden : isHidden)), + Opts.end()); // Eliminate duplicate entries in table (from enum flags options, f.e.) { // Give OptionSet a scope std::set<Option*> OptionSet; - for (unsigned i = 0; i != Options.size(); ++i) - if (OptionSet.count(Options[i].second) == 0) - OptionSet.insert(Options[i].second); // Add new entry to set + for (unsigned i = 0; i != Opts.size(); ++i) + if (OptionSet.count(Opts[i].second) == 0) + OptionSet.insert(Opts[i].second); // Add new entry to set else - Options.erase(Options.begin()+i--); // Erase duplicate + Opts.erase(Opts.begin()+i--); // Erase duplicate } if (ProgramOverview) @@ -927,8 +915,8 @@ std::cout << "USAGE: " << ProgramName << " [options]"; - // Print out the positional options... - std::vector<Option*> &PosOpts = getPositionalOpts(); + // Print out the positional options. + std::vector<Option*> &PosOpts = *PositionalOptions; Option *CAOpt = 0; // The cl::ConsumeAfter option, if it exists... if (!PosOpts.empty() && PosOpts[0]->getNumOccurrencesFlag() == ConsumeAfter) CAOpt = PosOpts[0]; @@ -946,21 +934,21 @@ // Compute the maximum argument length... MaxArgLen = 0; - for (unsigned i = 0, e = Options.size(); i != e; ++i) - MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth()); + for (unsigned i = 0, e = Opts.size(); i != e; ++i) + MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth()); std::cout << "OPTIONS:\n"; - for (unsigned i = 0, e = Options.size(); i != e; ++i) - Options[i].second->printOptionInfo(MaxArgLen); + for (unsigned i = 0, e = Opts.size(); i != e; ++i) + Opts[i].second->printOptionInfo(MaxArgLen); // Print any extra help the user has declared. - for (std::vector<const char *>::iterator I = MoreHelp().begin(), - E = MoreHelp().end(); I != E; ++I) + for (std::vector<const char *>::iterator I = MoreHelp->begin(), + E = MoreHelp->end(); I != E; ++I) std::cout << *I; - MoreHelp().clear(); + MoreHelp->clear(); // Halt the program since help information was printed - getOpts().clear(); // Don't bother making option dtors remove from map. + Options->clear(); // Don't bother making option dtors remove from map. exit(1); } }; @@ -1001,7 +989,7 @@ std::cout << " with assertions"; #endif std::cout << ".\n"; - getOpts().clear(); // Don't bother making option dtors remove from map. + Options->clear(); // Don't bother making option dtors remove from map. exit(1); } else { (*OverrideVersionPrinter)(); Index: llvm/lib/Support/Timer.cpp diff -u llvm/lib/Support/Timer.cpp:1.45 llvm/lib/Support/Timer.cpp:1.46 --- llvm/lib/Support/Timer.cpp:1.45 Thu Apr 21 17:52:05 2005 +++ llvm/lib/Support/Timer.cpp Wed Oct 4 16:50:14 2006 @@ -13,6 +13,7 @@ #include "llvm/Support/Timer.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/ManagedStatic.h" #include "llvm/System/Process.h" #include <algorithm> #include <fstream> @@ -32,8 +33,8 @@ // would get destroyed before the Statistic, causing havoc to ensue. We "fix" // this by creating the string the first time it is needed and never destroying // it. +static ManagedStatic<std::string> LibSupportInfoOutputFilename; static std::string &getLibSupportInfoOutputFilename() { - static std::string *LibSupportInfoOutputFilename = new std::string(); return *LibSupportInfoOutputFilename; } @@ -127,7 +128,7 @@ return Result; } -static std::vector<Timer*> ActiveTimers; +static ManagedStatic<std::vector<Timer*> > ActiveTimers; void Timer::startTimer() { Started = true; @@ -137,7 +138,7 @@ SystemTime -= TR.SystemTime; MemUsed -= TR.MemUsed; PeakMemBase = TR.MemUsed; - ActiveTimers.push_back(this); + ActiveTimers->push_back(this); } void Timer::stopTimer() { @@ -147,13 +148,13 @@ SystemTime += TR.SystemTime; MemUsed += TR.MemUsed; - if (ActiveTimers.back() == this) { - ActiveTimers.pop_back(); + if (ActiveTimers->back() == this) { + ActiveTimers->pop_back(); } else { std::vector<Timer*>::iterator I = - std::find(ActiveTimers.begin(), ActiveTimers.end(), this); - assert(I != ActiveTimers.end() && "stop but no startTimer?"); - ActiveTimers.erase(I); + std::find(ActiveTimers->begin(), ActiveTimers->end(), this); + assert(I != ActiveTimers->end() && "stop but no startTimer?"); + ActiveTimers->erase(I); } } @@ -172,8 +173,8 @@ void Timer::addPeakMemoryMeasurement() { size_t MemUsed = getMemUsage(); - for (std::vector<Timer*>::iterator I = ActiveTimers.begin(), - E = ActiveTimers.end(); I != E; ++I) + for (std::vector<Timer*>::iterator I = ActiveTimers->begin(), + E = ActiveTimers->end(); I != E; ++I) (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase); } @@ -181,14 +182,14 @@ // NamedRegionTimer Implementation //===----------------------------------------------------------------------===// -static Timer &getNamedRegionTimer(const std::string &Name) { - static std::map<std::string, Timer> NamedTimers; +static ManagedStatic<std::map<std::string, Timer> > NamedTimers; - std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name); - if (I != NamedTimers.end() && I->first == Name) +static Timer &getNamedRegionTimer(const std::string &Name) { + std::map<std::string, Timer>::iterator I = NamedTimers->lower_bound(Name); + if (I != NamedTimers->end() && I->first == Name) return I->second; - return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second; + return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second; } NamedRegionTimer::NamedRegionTimer(const std::string &Name) _______________________________________________ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits