LLVM 20.0.0git
InitLLVM.cpp
Go to the documentation of this file.
1//===-- InitLLVM.cpp -----------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/StringRef.h"
12#include "llvm/Support/Error.h"
18
19#ifdef _WIN32
21#endif
22
23#ifdef __MVS__
24#include <unistd.h>
25
26void CleanupStdHandles(void *Cookie) {
27 llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs();
28 Outs->flush();
29 Errs->flush();
30 llvm::restoreStdHandleAutoConversion(STDIN_FILENO);
31 llvm::restoreStdHandleAutoConversion(STDOUT_FILENO);
32 llvm::restoreStdHandleAutoConversion(STDERR_FILENO);
33}
34#endif
35
36using namespace llvm;
37using namespace llvm::sys;
38
39InitLLVM::InitLLVM(int &Argc, const char **&Argv,
40 bool InstallPipeSignalExitHandler) {
41#ifndef NDEBUG
42 static std::atomic<bool> Initialized{false};
43 assert(!Initialized && "InitLLVM was already initialized!");
44 Initialized = true;
45#endif
46#ifdef __MVS__
47 // Bring stdin/stdout/stderr into a known state.
48 sys::AddSignalHandler(CleanupStdHandles, nullptr);
49#endif
50 if (InstallPipeSignalExitHandler)
51 // The pipe signal handler must be installed before any other handlers are
52 // registered. This is because the Unix \ref RegisterHandlers function does
53 // not perform a sigaction() for SIGPIPE unless a one-shot handler is
54 // present, to allow long-lived processes (like lldb) to fully opt-out of
55 // llvm's SIGPIPE handling and ignore the signal safely.
57 // Initialize the stack printer after installing the one-shot pipe signal
58 // handler, so we can perform a sigaction() for SIGPIPE on Unix if requested.
59 StackPrinter.emplace(Argc, Argv);
62
63#ifdef __MVS__
64
65 // We use UTF-8 as the internal character encoding. On z/OS, all external
66 // output is encoded in EBCDIC. In order to be able to read all
67 // error messages, we turn conversion to EBCDIC on for stderr fd.
68 std::string Banner = std::string(Argv[0]) + ": ";
69 ExitOnError ExitOnErr(Banner);
70
71 // If turning on conversion for stderr fails then the error message
72 // may be garbled. There is no solution to this problem.
73 ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDERR_FILENO)));
74 ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDOUT_FILENO)));
75#endif
76
77#ifdef _WIN32
78 // We use UTF-8 as the internal character encoding. On Windows,
79 // arguments passed to main() may not be encoded in UTF-8. In order
80 // to reliably detect encoding of command line arguments, we use an
81 // Windows API to obtain arguments, convert them to UTF-8, and then
82 // write them back to the Argv vector.
83 //
84 // There's probably other way to do the same thing (e.g. using
85 // wmain() instead of main()), but this way seems less intrusive
86 // than that.
87 std::string Banner = std::string(Argv[0]) + ": ";
88 ExitOnError ExitOnErr(Banner);
89
91
92 // GetCommandLineArguments doesn't terminate the vector with a
93 // nullptr. Do it to make it compatible with the real argv.
94 Args.push_back(nullptr);
95
96 Argc = Args.size() - 1;
97 Argv = Args.data();
98#endif
99}
100
102#ifdef __MVS__
103 CleanupStdHandles(nullptr);
104#endif
106}
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Helper for check-and-exit error handling.
Definition: Error.h:1408
InitLLVM(int &Argc, const char **&Argv, bool InstallPipeSignalExitHandler=true)
Definition: InitLLVM.cpp:39
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
std::error_code GetCommandLineArguments(SmallVectorImpl< const char * > &Args, BumpPtrAllocator &Alloc)
void SetOneShotPipeSignalFunction(void(*Handler)())
Registers a function to be called in a "one-shot" manner when a pipe signal is delivered to the proce...
void DefaultOneShotPipeSignalHandler()
On Unix systems and Windows, this function exits with an "IO error" exit code.
void AddSignalHandler(SignalHandlerCallback FnPtr, void *Cookie)
Add a function to be called when an abort/kill signal is delivered to the process.
void PrintStackTraceOnErrorSignal(StringRef Argv0, bool DisableCrashReporting=false)
When an error signal (such as SIGABRT or SIGSEGV) is delivered to the process, print a stack trace an...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
void install_out_of_memory_new_handler()
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void llvm_shutdown()
llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:111