1197194b6SRui Ueyama //===-- InitLLVM.cpp -----------------------------------------------------===//
2197194b6SRui Ueyama //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6197194b6SRui Ueyama //
7197194b6SRui Ueyama //===----------------------------------------------------------------------===//
8197194b6SRui Ueyama 
9197194b6SRui Ueyama #include "llvm/Support/InitLLVM.h"
1075e164f6Sserge-sans-paille #include "llvm/ADT/StringRef.h"
1175e164f6Sserge-sans-paille #include "llvm/Support/ErrorHandling.h"
12197194b6SRui Ueyama #include "llvm/Support/ManagedStatic.h"
13197194b6SRui Ueyama #include "llvm/Support/PrettyStackTrace.h"
14197194b6SRui Ueyama #include "llvm/Support/Signals.h"
1575e164f6Sserge-sans-paille #include "llvm/Support/SwapByteOrder.h"
16197194b6SRui Ueyama 
17e6ac9f5eSRui Ueyama #ifdef _WIN32
18*2a9e33dbSserge-sans-paille #include "llvm/Support/Error.h"
1901f9abbbSHans Wennborg #include "llvm/Support/Windows/WindowsSupport.h"
20e6ac9f5eSRui Ueyama #endif
21e6ac9f5eSRui Ueyama 
22197194b6SRui Ueyama using namespace llvm;
23e6ac9f5eSRui Ueyama using namespace llvm::sys;
24197194b6SRui Ueyama 
InitLLVM(int & Argc,const char ** & Argv,bool InstallPipeSignalExitHandler)254624e83cSVedant Kumar InitLLVM::InitLLVM(int &Argc, const char **&Argv,
26e05baf40SVedant Kumar                    bool InstallPipeSignalExitHandler) {
274624e83cSVedant Kumar   if (InstallPipeSignalExitHandler)
28e05baf40SVedant Kumar     // The pipe signal handler must be installed before any other handlers are
29e05baf40SVedant Kumar     // registered. This is because the Unix \ref RegisterHandlers function does
30e05baf40SVedant Kumar     // not perform a sigaction() for SIGPIPE unless a one-shot handler is
31e05baf40SVedant Kumar     // present, to allow long-lived processes (like lldb) to fully opt-out of
32e05baf40SVedant Kumar     // llvm's SIGPIPE handling and ignore the signal safely.
334624e83cSVedant Kumar     sys::SetOneShotPipeSignalFunction(sys::DefaultOneShotPipeSignalHandler);
34e05baf40SVedant Kumar   // Initialize the stack printer after installing the one-shot pipe signal
35e05baf40SVedant Kumar   // handler, so we can perform a sigaction() for SIGPIPE on Unix if requested.
36e05baf40SVedant Kumar   StackPrinter.emplace(Argc, Argv);
37197194b6SRui Ueyama   sys::PrintStackTraceOnErrorSignal(Argv[0]);
38e99dee82SFangrui Song   install_out_of_memory_new_handler();
39197194b6SRui Ueyama 
40197194b6SRui Ueyama #ifdef _WIN32
41197194b6SRui Ueyama   // We use UTF-8 as the internal character encoding. On Windows,
42197194b6SRui Ueyama   // arguments passed to main() may not be encoded in UTF-8. In order
43197194b6SRui Ueyama   // to reliably detect encoding of command line arguments, we use an
44197194b6SRui Ueyama   // Windows API to obtain arguments, convert them to UTF-8, and then
45197194b6SRui Ueyama   // write them back to the Argv vector.
46197194b6SRui Ueyama   //
47197194b6SRui Ueyama   // There's probably other way to do the same thing (e.g. using
48197194b6SRui Ueyama   // wmain() instead of main()), but this way seems less intrusive
49197194b6SRui Ueyama   // than that.
50197194b6SRui Ueyama   std::string Banner = std::string(Argv[0]) + ": ";
51197194b6SRui Ueyama   ExitOnError ExitOnErr(Banner);
52197194b6SRui Ueyama 
53e6ac9f5eSRui Ueyama   ExitOnErr(errorCodeToError(windows::GetCommandLineArguments(Args, Alloc)));
54197194b6SRui Ueyama 
55e6ac9f5eSRui Ueyama   // GetCommandLineArguments doesn't terminate the vector with a
56e6ac9f5eSRui Ueyama   // nullptr.  Do it to make it compatible with the real argv.
57197194b6SRui Ueyama   Args.push_back(nullptr);
58197194b6SRui Ueyama 
59197194b6SRui Ueyama   Argc = Args.size() - 1;
60197194b6SRui Ueyama   Argv = Args.data();
61197194b6SRui Ueyama #endif
62197194b6SRui Ueyama }
63197194b6SRui Ueyama 
~InitLLVM()64d9941f74SNoah Shutty InitLLVM::~InitLLVM() { llvm_shutdown(); }
65