Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 1 | //===- InitLLVM.h -----------------------------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // 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 |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_SUPPORT_LLVM_H |
| 10 | #define LLVM_SUPPORT_LLVM_H |
| 11 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 12 | #include "llvm/ADT/Optional.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/Support/Allocator.h" |
| 15 | #include "llvm/Support/PrettyStackTrace.h" |
| 16 | |
| 17 | // The main() functions in typical LLVM tools start with InitLLVM which does |
| 18 | // the following one-time initializations: |
| 19 | // |
| 20 | // 1. Setting up a signal handler so that pretty stack trace is printed out |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 21 | // if a process crashes. A signal handler that exits when a failed write to |
| 22 | // a pipe occurs may optionally be installed: this is on-by-default. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 23 | // |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 24 | // 2. Set up the global new-handler which is called when a memory allocation |
| 25 | // attempt fails. |
| 26 | // |
| 27 | // 3. If running on Windows, obtain command line arguments using a |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 28 | // multibyte character-aware API and convert arguments into UTF-8 |
| 29 | // encoding, so that you can assume that command line arguments are |
| 30 | // always encoded in UTF-8 on any platform. |
| 31 | // |
| 32 | // InitLLVM calls llvm_shutdown() on destruction, which cleans up |
| 33 | // ManagedStatic objects. |
| 34 | namespace llvm { |
| 35 | class InitLLVM { |
| 36 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 37 | InitLLVM(int &Argc, const char **&Argv, |
| 38 | bool InstallPipeSignalExitHandler = true); |
| 39 | InitLLVM(int &Argc, char **&Argv, bool InstallPipeSignalExitHandler = true) |
| 40 | : InitLLVM(Argc, const_cast<const char **&>(Argv), |
| 41 | InstallPipeSignalExitHandler) {} |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 42 | |
| 43 | ~InitLLVM(); |
| 44 | |
| 45 | private: |
| 46 | BumpPtrAllocator Alloc; |
| 47 | SmallVector<const char *, 0> Args; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 48 | Optional<PrettyStackTraceProgram> StackPrinter; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 49 | }; |
| 50 | } // namespace llvm |
| 51 | |
| 52 | #endif |