Index: lld/tools/lld/lld.cpp =================================================================== --- lld/tools/lld/lld.cpp +++ lld/tools/lld/lld.cpp @@ -20,10 +20,8 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" -#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/InitLLVM.h" #include "llvm/Support/Path.h" -#include "llvm/Support/PrettyStackTrace.h" -#include "llvm/Support/Signals.h" #include using namespace lld; @@ -114,10 +112,7 @@ /// Universal linker main(). This linker emulates the gnu, darwin, or /// windows linker based on the argv[0] or -flavor option. int main(int Argc, const char **Argv) { - // Standard set up, so program fails gracefully. - sys::PrintStackTraceOnErrorSignal(Argv[0]); - PrettyStackTraceProgram StackPrinter(Argc, Argv); - llvm_shutdown_obj Shutdown; + InitLLVM X(Argc, Argv); std::vector Args(Argv, Argv + Argc); switch (parseFlavor(Args)) { Index: llvm/include/llvm/Support/InitLLVM.h =================================================================== --- /dev/null +++ llvm/include/llvm/Support/InitLLVM.h @@ -0,0 +1,30 @@ +//===- InitLLVM.h -----------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_LLVM_H +#define LLVM_SUPPORT_LLVM_H + +#include "llvm/ADT/SmallVector.h" +#include "llvm/Support/PrettyStackTrace.h" + +namespace llvm { +class InitLLVM { +public: + InitLLVM(int &Argc, const char **&Argv); + ~InitLLVM(); + +private: + SmallVector Buf; + SmallVector Args; + std::string Banner; + PrettyStackTraceProgram StackPrinter; +}; +} + +#endif Index: llvm/lib/Support/CMakeLists.txt =================================================================== --- llvm/lib/Support/CMakeLists.txt +++ llvm/lib/Support/CMakeLists.txt @@ -76,6 +76,7 @@ GlobPattern.cpp GraphWriter.cpp Hashing.cpp + InitLLVM.cpp IntEqClasses.cpp IntervalMap.cpp JamCRC.cpp Index: llvm/lib/Support/InitLLVM.cpp =================================================================== --- /dev/null +++ llvm/lib/Support/InitLLVM.cpp @@ -0,0 +1,49 @@ +//===-- InitLLVM.cpp -----------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/StringSaver.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Process.h" + +using namespace llvm; + +#ifdef _WIN32 + +static ExitOnError ExitOnErr; + +InitLLVM::InitLLVM(int &Argc, const char **&Argv) + : StackPrinter(Argc, Argv) { + sys::PrintStackTraceOnErrorSignal(Argv[0]); + + Banner = (StringRef(Argv[0]) + ": ").str(); + ExitOnErr.setBanner(Banner); + + SpecificBumpPtrAllocator Alloc; + ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector( + Args, makeArrayRef(Argv, Argc), Alloc))); + + Argc = Args.size(); + Argv = Args.data(); +} + +#else + +InitLLVM::InitLLVM(int &Argc, const char **&Argv) + : StackPrinter(Argc, Argv) { + sys::PrintStackTraceOnErrorSignal(Argv[0]); +} + +#endif + +InitLLVM::~InitLLVM() { llvm_shutdown(); } Index: llvm/tools/llvm-cvtres/llvm-cvtres.cpp =================================================================== --- llvm/tools/llvm-cvtres/llvm-cvtres.cpp +++ llvm/tools/llvm-cvtres/llvm-cvtres.cpp @@ -14,6 +14,7 @@ #include "llvm/ADT/StringSwitch.h" #include "llvm/Object/Binary.h" +#include "llvm/Support/InitLLVM.h" #include "llvm/Object/WindowsResource.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" @@ -63,8 +64,6 @@ public: CvtResOptTable() : OptTable(InfoTable, true) {} }; - -static ExitOnError ExitOnErr; } LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { @@ -95,22 +94,12 @@ return std::move(EC.get()); } -int main(int argc_, const char *argv_[]) { - sys::PrintStackTraceOnErrorSignal(argv_[0]); - PrettyStackTraceProgram X(argc_, argv_); - - ExitOnErr.setBanner("llvm-cvtres: "); - - SmallVector argv; - SpecificBumpPtrAllocator ArgAllocator; - ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector( - argv, makeArrayRef(argv_, argc_), ArgAllocator))); - - llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. +int main(int Argc, const char **Argv) { + InitLLVM X(Argc, Argv); CvtResOptTable T; unsigned MAI, MAC; - ArrayRef ArgsArr = makeArrayRef(argv_ + 1, argc_); + ArrayRef ArgsArr = makeArrayRef(Argv + 1, Argc); opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC); if (InputArgs.hasArg(OPT_HELP)) { Index: llvm/tools/llvm-mt/llvm-mt.cpp =================================================================== --- llvm/tools/llvm-mt/llvm-mt.cpp +++ llvm/tools/llvm-mt/llvm-mt.cpp @@ -14,6 +14,7 @@ #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" +#include "llvm/Support/InitLLVM.h" #include "llvm/Option/Option.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileOutputBuffer.h" @@ -61,8 +62,6 @@ public: CvtResOptTable() : OptTable(InfoTable, true) {} }; - -static ExitOnError ExitOnErr; } // namespace LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { @@ -86,21 +85,12 @@ }); } -int main(int argc, const char **argv) { - sys::PrintStackTraceOnErrorSignal(argv[0]); - PrettyStackTraceProgram X(argc, argv); - - ExitOnErr.setBanner("llvm-mt: "); - - SmallVector argv_buf; - SpecificBumpPtrAllocator ArgAllocator; - ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector( - argv_buf, makeArrayRef(argv, argc), ArgAllocator))); - llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. +int main(int Argc, const char **Argv) { + InitLLVM X(Argc, Argv); CvtResOptTable T; unsigned MAI, MAC; - ArrayRef ArgsArr = makeArrayRef(argv + 1, argc); + ArrayRef ArgsArr = makeArrayRef(Argv + 1, Argc); opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC); for (auto *Arg : InputArgs.filtered(OPT_INPUT)) { Index: llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp =================================================================== --- llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp +++ llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp @@ -30,6 +30,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/BitVector.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/Support/InitLLVM.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/BinaryFormat/Magic.h" @@ -656,7 +657,7 @@ } // namespace exportstream } -static ExitOnError ExitOnErr; +static ExitOnError ExitOnErr("llvm-pdbutil: "); static void yamlToPdb(StringRef Path) { BumpPtrAllocator Allocator; @@ -1205,21 +1206,10 @@ Chunks.push_back(opts::ModuleSubsection::All); } -int main(int argc_, const char *argv_[]) { - // Print a stack trace if we signal out. - sys::PrintStackTraceOnErrorSignal(argv_[0]); - PrettyStackTraceProgram X(argc_, argv_); +int main(int Argc, const char **Argv) { + InitLLVM X(Argc, Argv); - ExitOnErr.setBanner("llvm-pdbutil: "); - - SmallVector argv; - SpecificBumpPtrAllocator ArgAllocator; - ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector( - argv, makeArrayRef(argv_, argc_), ArgAllocator))); - - llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. - - cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n"); + cl::ParseCommandLineOptions(Argc, Argv, "LLVM PDB Dumper\n"); if (opts::BytesSubcommand) { if (!parseRange(opts::bytes::DumpBlockRangeOpt, Index: llvm/tools/llvm-rc/llvm-rc.cpp =================================================================== --- llvm/tools/llvm-rc/llvm-rc.cpp +++ llvm/tools/llvm-rc/llvm-rc.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Process.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/InitLLVM.h" #include "llvm/Support/raw_ostream.h" #include @@ -67,7 +68,7 @@ RcOptTable() : OptTable(InfoTable, /* IgnoreCase = */ true) {} }; -static ExitOnError ExitOnErr; +static ExitOnError ExitOnErr("llvm-rc: "); LLVM_ATTRIBUTE_NORETURN static void fatalError(const Twine &Message) { errs() << Message << "\n"; @@ -76,22 +77,12 @@ } // anonymous namespace -int main(int argc_, const char *argv_[]) { - sys::PrintStackTraceOnErrorSignal(argv_[0]); - PrettyStackTraceProgram X(argc_, argv_); - - ExitOnErr.setBanner("llvm-rc: "); - - SmallVector argv; - SpecificBumpPtrAllocator ArgAllocator; - ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector( - argv, makeArrayRef(argv_, argc_), ArgAllocator))); - - llvm_shutdown_obj Y; +int main(int Argc, const char **Argv) { + InitLLVM X(Argc, Argv); RcOptTable T; unsigned MAI, MAC; - ArrayRef ArgsArr = makeArrayRef(argv_ + 1, argc_); + ArrayRef ArgsArr = makeArrayRef(Argv + 1, Argc); opt::InputArgList InputArgs = T.ParseArgs(ArgsArr, MAI, MAC); // The tool prints nothing when invoked with no command-line arguments.