Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 1 | //===-- InitLLVM.cpp -----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Support/InitLLVM.h" |
| 11 | #include "llvm/Support/Error.h" |
| 12 | #include "llvm/Support/ManagedStatic.h" |
| 13 | #include "llvm/Support/PrettyStackTrace.h" |
| 14 | #include "llvm/Support/Process.h" |
| 15 | #include "llvm/Support/Signals.h" |
| 16 | #include <string> |
| 17 | |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 18 | #ifdef _WIN32 |
| 19 | #include "Windows/WindowsSupport.h" |
| 20 | #endif |
| 21 | |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 23 | using namespace llvm::sys; |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 24 | |
| 25 | InitLLVM::InitLLVM(int &Argc, const char **&Argv) : StackPrinter(Argc, Argv) { |
| 26 | sys::PrintStackTraceOnErrorSignal(Argv[0]); |
| 27 | |
| 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 Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 41 | ExitOnErr(errorCodeToError(windows::GetCommandLineArguments(Args, Alloc))); |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 42 | |
Rui Ueyama | e6ac9f5 | 2018-04-17 21:09:16 +0000 | [diff] [blame] | 43 | // GetCommandLineArguments doesn't terminate the vector with a |
| 44 | // nullptr. Do it to make it compatible with the real argv. |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 45 | Args.push_back(nullptr); |
| 46 | |
| 47 | Argc = Args.size() - 1; |
| 48 | Argv = Args.data(); |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | InitLLVM::~InitLLVM() { llvm_shutdown(); } |