diff options
Diffstat (limited to 'llvm/lib/Support/InitLLVM.cpp')
| -rw-r--r-- | llvm/lib/Support/InitLLVM.cpp | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/llvm/lib/Support/InitLLVM.cpp b/llvm/lib/Support/InitLLVM.cpp index 2b7173b28940..7f475f42f3cb 100644 --- a/llvm/lib/Support/InitLLVM.cpp +++ b/llvm/lib/Support/InitLLVM.cpp @@ -8,6 +8,8 @@ #include "llvm/Support/InitLLVM.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/AutoConvert.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/PrettyStackTrace.h" @@ -15,15 +17,31 @@ #include "llvm/Support/SwapByteOrder.h" #ifdef _WIN32 -#include "llvm/Support/Error.h" #include "llvm/Support/Windows/WindowsSupport.h" #endif +#ifdef __MVS__ +#include <unistd.h> + +void CleanupStdHandles(void *Cookie) { + llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs(); + Outs->flush(); + Errs->flush(); + llvm::restoreStdHandleAutoConversion(STDIN_FILENO); + llvm::restoreStdHandleAutoConversion(STDOUT_FILENO); + llvm::restoreStdHandleAutoConversion(STDERR_FILENO); +} +#endif + using namespace llvm; using namespace llvm::sys; InitLLVM::InitLLVM(int &Argc, const char **&Argv, bool InstallPipeSignalExitHandler) { +#ifdef __MVS__ + // Bring stdin/stdout/stderr into a known state. + sys::AddSignalHandler(CleanupStdHandles, nullptr); +#endif if (InstallPipeSignalExitHandler) // The pipe signal handler must be installed before any other handlers are // registered. This is because the Unix \ref RegisterHandlers function does @@ -37,6 +55,20 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv, sys::PrintStackTraceOnErrorSignal(Argv[0]); install_out_of_memory_new_handler(); +#ifdef __MVS__ + + // We use UTF-8 as the internal character encoding. On z/OS, all external + // output is encoded in EBCDIC. In order to be able to read all + // error messages, we turn conversion to EBCDIC on for stderr fd. + std::string Banner = std::string(Argv[0]) + ": "; + ExitOnError ExitOnErr(Banner); + + // If turning on conversion for stderr fails then the error message + // may be garbled. There is no solution to this problem. + ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDERR_FILENO))); + ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDOUT_FILENO))); +#endif + #ifdef _WIN32 // We use UTF-8 as the internal character encoding. On Windows, // arguments passed to main() may not be encoded in UTF-8. In order @@ -61,4 +93,9 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv, #endif } -InitLLVM::~InitLLVM() { llvm_shutdown(); } +InitLLVM::~InitLLVM() { +#ifdef __MVS__ + CleanupStdHandles(nullptr); +#endif + llvm_shutdown(); +} |
