blob: 0d7d7fcc8cb6054af53737e452d2f2ab08f8c470 [file] [log] [blame]
Rui Ueyama197194b2018-04-13 18:26:06 +00001//===-- InitLLVM.cpp -----------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rui Ueyama197194b2018-04-13 18:26:06 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/InitLLVM.h"
10#include "llvm/Support/Error.h"
11#include "llvm/Support/ManagedStatic.h"
12#include "llvm/Support/PrettyStackTrace.h"
13#include "llvm/Support/Process.h"
14#include "llvm/Support/Signals.h"
15#include <string>
16
Rui Ueyamae6ac9f52018-04-17 21:09:16 +000017#ifdef _WIN32
18#include "Windows/WindowsSupport.h"
19#endif
20
Rui Ueyama197194b2018-04-13 18:26:06 +000021using namespace llvm;
Rui Ueyamae6ac9f52018-04-17 21:09:16 +000022using namespace llvm::sys;
Rui Ueyama197194b2018-04-13 18:26:06 +000023
24InitLLVM::InitLLVM(int &Argc, const char **&Argv) : StackPrinter(Argc, Argv) {
25 sys::PrintStackTraceOnErrorSignal(Argv[0]);
Fangrui Songe99dee82019-07-12 16:23:25 +000026 install_out_of_memory_new_handler();
Rui Ueyama197194b2018-04-13 18:26:06 +000027
28#ifdef _WIN32
29 // We use UTF-8 as the internal character encoding. On Windows,
30 // arguments passed to main() may not be encoded in UTF-8. In order
31 // to reliably detect encoding of command line arguments, we use an
32 // Windows API to obtain arguments, convert them to UTF-8, and then
33 // write them back to the Argv vector.
34 //
35 // There's probably other way to do the same thing (e.g. using
36 // wmain() instead of main()), but this way seems less intrusive
37 // than that.
38 std::string Banner = std::string(Argv[0]) + ": ";
39 ExitOnError ExitOnErr(Banner);
40
Rui Ueyamae6ac9f52018-04-17 21:09:16 +000041 ExitOnErr(errorCodeToError(windows::GetCommandLineArguments(Args, Alloc)));
Rui Ueyama197194b2018-04-13 18:26:06 +000042
Rui Ueyamae6ac9f52018-04-17 21:09:16 +000043 // GetCommandLineArguments doesn't terminate the vector with a
44 // nullptr. Do it to make it compatible with the real argv.
Rui Ueyama197194b2018-04-13 18:26:06 +000045 Args.push_back(nullptr);
46
47 Argc = Args.size() - 1;
48 Argv = Args.data();
49#endif
50}
51
52InitLLVM::~InitLLVM() { llvm_shutdown(); }