https://github.com/abhina-sree updated https://github.com/llvm/llvm-project/pull/143174
>From cedf7e22c9d1ee7af74aa9c8e3e77c4d86fe7767 Mon Sep 17 00:00:00 2001 From: Abhina Sreeskantharajan <abhina.sreeskanthara...@ibm.com> Date: Fri, 6 Jun 2025 12:16:52 -0400 Subject: [PATCH] refactor AutoConvert.h to remove MVS guard --- clang/tools/c-index-test/c-index-test.c | 4 +-- llvm/include/llvm/Support/AutoConvert.h | 15 +++++++++-- llvm/lib/Support/AutoConvert.cpp | 35 ++++++++++++++++++++----- llvm/lib/Support/InitLLVM.cpp | 10 +++---- llvm/lib/Support/MemoryBuffer.cpp | 6 ++--- llvm/lib/Support/raw_ostream.cpp | 10 ++++--- llvm/utils/count/count.c | 6 +++-- 7 files changed, 59 insertions(+), 27 deletions(-) diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c index 4a887cd0c1e2e..3922aa9c31386 100644 --- a/clang/tools/c-index-test/c-index-test.c +++ b/clang/tools/c-index-test/c-index-test.c @@ -5199,13 +5199,13 @@ static void flush_atexit(void) { int main(int argc, const char **argv) { thread_info client_data; -#ifdef __MVS__ + // On z/OS we need to enable auto conversion if (enablezOSAutoConversion(fileno(stdout)) == -1) fprintf(stderr, "Setting conversion on stdout failed\n"); + // On z/OS we need to enable auto conversion if (enablezOSAutoConversion(fileno(stderr)) == -1) fprintf(stderr, "Setting conversion on stderr failed\n"); -#endif atexit(flush_atexit); diff --git a/llvm/include/llvm/Support/AutoConvert.h b/llvm/include/llvm/Support/AutoConvert.h index 352493e9be25f..425a46c2fa7cf 100644 --- a/llvm/include/llvm/Support/AutoConvert.h +++ b/llvm/include/llvm/Support/AutoConvert.h @@ -16,6 +16,7 @@ #ifdef __MVS__ #include <_Ccsid.h> +#endif #ifdef __cplusplus #include "llvm/Support/ErrorOr.h" #include <system_error> @@ -25,6 +26,16 @@ #define CCSID_UTF_8 1208 #define CCSID_ISO8859_1 819 +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif +#ifndef STDOUT_FILENO +# define STDOUT_FILENO 1 +#endif +#ifndef STDERR_FILENO +# define STDERR_FILENO 2 +#endif + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ @@ -55,8 +66,10 @@ std::error_code restorezOSStdHandleAutoConversion(int FD); /** \brief Set the tag information for a file descriptor. */ std::error_code setzOSFileTag(int FD, int CCSID, bool Text); +#ifdef __MVS__ /** \brief Get the the tag ccsid for a file name or a file descriptor. */ ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD = -1); +#endif /** \brief Query the file tag to determine if it needs conversion to UTF-8 * codepage. @@ -66,6 +79,4 @@ ErrorOr<bool> needzOSConversion(const char *FileName, const int FD = -1); } /* namespace llvm */ #endif /* __cplusplus */ -#endif /* __MVS__ */ - #endif /* LLVM_SUPPORT_AUTOCONVERT_H */ diff --git a/llvm/lib/Support/AutoConvert.cpp b/llvm/lib/Support/AutoConvert.cpp index f7918548df1d0..b4b501d70f976 100644 --- a/llvm/lib/Support/AutoConvert.cpp +++ b/llvm/lib/Support/AutoConvert.cpp @@ -11,20 +11,23 @@ // //===----------------------------------------------------------------------===// -#ifdef __MVS__ - #include "llvm/Support/AutoConvert.h" #include "llvm/Support/Error.h" #include <cassert> #include <fcntl.h> #include <sys/stat.h> +#if defined(HAVE_UNISTD_H) #include <unistd.h> +#endif using namespace llvm; static int savedStdHandleAutoConversionMode[3] = {-1, -1, -1}; int disablezOSAutoConversion(int FD) { +#ifndef __MVS__ + return 0; +#else static const struct f_cnvrt Convert = { SETCVTOFF, // cvtcmd 0, // pccsid @@ -32,9 +35,13 @@ int disablezOSAutoConversion(int FD) { }; return fcntl(FD, F_CONTROL_CVT, &Convert); +#endif } int restorezOSStdHandleAutoConversion(int FD) { +#ifndef __MVS__ + return 0; +#else assert(FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO); if (savedStdHandleAutoConversionMode[FD] == -1) return 0; @@ -44,9 +51,13 @@ int restorezOSStdHandleAutoConversion(int FD) { 0, // fccsid }; return (fcntl(FD, F_CONTROL_CVT, &Cvt)); +#endif } int enablezOSAutoConversion(int FD) { +#ifndef __MVS__ + return 0; +#else struct f_cnvrt Query = { QUERYCVT, // cvtcmd 0, // pccsid @@ -81,30 +92,35 @@ int enablezOSAutoConversion(int FD) { // Assume untagged files to be IBM-1047 encoded. Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid; return fcntl(FD, F_CONTROL_CVT, &Query); +#endif } std::error_code llvm::disablezOSAutoConversion(int FD) { +#ifdef __MVS__ if (::disablezOSAutoConversion(FD) == -1) return errnoAsErrorCode(); - +#endif return std::error_code(); } std::error_code llvm::enablezOSAutoConversion(int FD) { +#ifdef __MVS__ if (::enablezOSAutoConversion(FD) == -1) return errnoAsErrorCode(); - +#endif return std::error_code(); } std::error_code llvm::restorezOSStdHandleAutoConversion(int FD) { +#ifdef __MVS__ if (::restorezOSStdHandleAutoConversion(FD) == -1) return errnoAsErrorCode(); - +#endif return std::error_code(); } std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) { +#ifdef __MVS__ assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) && "FT_UNTAGGED and FT_BINARY are not allowed for text files"); struct file_tag Tag; @@ -115,9 +131,11 @@ std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) { if (fcntl(FD, F_SETTAG, &Tag) == -1) return errnoAsErrorCode(); +#endif return std::error_code(); } +#ifdef __MVS__ ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) { // If we have a file descriptor, use it to find out file tagging. Otherwise we // need to use stat() with the file path. @@ -136,8 +154,12 @@ ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) { return std::error_code(errno, std::generic_category()); return Attr.st_tag.ft_ccsid; } +#endif ErrorOr<bool> llvm::needzOSConversion(const char *FileName, const int FD) { +#ifndef __MVS__ + return false; +#else ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(FileName, FD); if (std::error_code EC = Ccsid.getError()) return EC; @@ -152,6 +174,5 @@ ErrorOr<bool> llvm::needzOSConversion(const char *FileName, const int FD) { default: return true; } +#endif } - -#endif //__MVS__ diff --git a/llvm/lib/Support/InitLLVM.cpp b/llvm/lib/Support/InitLLVM.cpp index 50f7a43cc34a7..27749ba822719 100644 --- a/llvm/lib/Support/InitLLVM.cpp +++ b/llvm/lib/Support/InitLLVM.cpp @@ -18,8 +18,9 @@ #include "llvm/Support/Windows/WindowsSupport.h" #endif -#ifdef __MVS__ +#if defined(HAVE_UNISTD_H) #include <unistd.h> +#endif void CleanupStdHandles(void *Cookie) { llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs(); @@ -29,7 +30,6 @@ void CleanupStdHandles(void *Cookie) { llvm::restorezOSStdHandleAutoConversion(STDOUT_FILENO); llvm::restorezOSStdHandleAutoConversion(STDERR_FILENO); } -#endif using namespace llvm; using namespace llvm::sys; @@ -41,10 +41,10 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv, assert(!Initialized && "InitLLVM was already initialized!"); Initialized = true; #endif -#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 @@ -97,8 +97,6 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv, } InitLLVM::~InitLLVM() { -#ifdef __MVS__ CleanupStdHandles(nullptr); -#endif llvm_shutdown(); } diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp index e2044bcc4e4f0..b2905b5b7ef11 100644 --- a/llvm/lib/Support/MemoryBuffer.cpp +++ b/llvm/lib/Support/MemoryBuffer.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/Config/config.h" #include "llvm/Support/Alignment.h" +#include "llvm/Support/AutoConvert.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" @@ -34,9 +35,6 @@ #include <io.h> #endif -#ifdef __MVS__ -#include "llvm/Support/AutoConvert.h" -#endif using namespace llvm; //===----------------------------------------------------------------------===// @@ -516,7 +514,7 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, // off the stream. // Note: This only works with the assumption of reading a full file (i.e, // Offset == 0 and MapSize == FileSize). Reading a file slice does not work. - if (Offset == 0 && MapSize == FileSize && *NeedConversion) + if (*NeedConversion && Offset == 0 && MapSize == FileSize) return getMemoryBufferForStream(FD, Filename); #endif diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index 16631a63d1921..129723dd11653 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -894,10 +894,11 @@ void raw_fd_ostream::anchor() {} raw_fd_ostream &llvm::outs() { // Set buffer settings to model stdout behavior. std::error_code EC; -#ifdef __MVS__ + + // On z/OS we need to enable auto conversion EC = enablezOSAutoConversion(STDOUT_FILENO); assert(!EC); -#endif + static raw_fd_ostream S("-", EC, sys::fs::OF_None); assert(!EC); return S; @@ -905,10 +906,11 @@ raw_fd_ostream &llvm::outs() { raw_fd_ostream &llvm::errs() { // Set standard error to be unbuffered. -#ifdef __MVS__ + + // On z/OS we need to enable auto conversion std::error_code EC = enablezOSAutoConversion(STDERR_FILENO); assert(!EC); -#endif + static raw_fd_ostream S(STDERR_FILENO, false, true); return S; } diff --git a/llvm/utils/count/count.c b/llvm/utils/count/count.c index 9166145fcc10a..a1faa3a642284 100644 --- a/llvm/utils/count/count.c +++ b/llvm/utils/count/count.c @@ -11,13 +11,15 @@ #include <stdlib.h> int main(int argc, char **argv) { -#ifdef __MVS__ + + // On z/OS we need to enable auto conversion if (enablezOSAutoConversion(fileno(stdin)) == -1) fprintf(stderr, "Setting conversion on stdin failed\n"); + // On z/OS we need to enable auto conversion if (enablezOSAutoConversion(fileno(stderr)) == -1) fprintf(stdout, "Setting conversion on stderr failed\n"); -#endif + size_t Count, NumLines, NumRead; char Buffer[4096], *End; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits