Skip to content

Commit 22fd11f

Browse files
authored
[SystemZ][z/OS] Refactor AutoConvert.h to remove large MVS guard (#143174)
This AutoConvert.h header frequently gets mislabeled as an unused include because it is guarded by MVS internally and every usage is also guarded. This refactors the change to remove this guard and instead make these functions a noop on other non-z/OS platforms.
1 parent 5dafe9d commit 22fd11f

File tree

5 files changed

+78
-48
lines changed

5 files changed

+78
-48
lines changed

llvm/include/llvm/Support/AutoConvert.h

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#ifdef __MVS__
1818
#include <_Ccsid.h>
19+
#endif
1920
#ifdef __cplusplus
2021
#include "llvm/Support/ErrorOr.h"
2122
#include <system_error>
@@ -28,16 +29,58 @@
2829
#ifdef __cplusplus
2930
extern "C" {
3031
#endif /* __cplusplus */
32+
3133
int enablezOSAutoConversion(int FD);
3234
int disablezOSAutoConversion(int FD);
3335
int restorezOSStdHandleAutoConversion(int FD);
36+
3437
#ifdef __cplusplus
3538
}
3639
#endif /* __cplusplus */
3740

3841
#ifdef __cplusplus
3942
namespace llvm {
4043

44+
inline std::error_code disableAutoConversion(int FD) {
45+
#ifdef __MVS__
46+
if (::disablezOSAutoConversion(FD) == -1)
47+
return errnoAsErrorCode();
48+
#endif
49+
return std::error_code();
50+
}
51+
52+
inline std::error_code enableAutoConversion(int FD) {
53+
#ifdef __MVS__
54+
if (::enablezOSAutoConversion(FD) == -1)
55+
return errnoAsErrorCode();
56+
#endif
57+
return std::error_code();
58+
}
59+
60+
inline std::error_code restoreStdHandleAutoConversion(int FD) {
61+
#ifdef __MVS__
62+
if (::restorezOSStdHandleAutoConversion(FD) == -1)
63+
return errnoAsErrorCode();
64+
#endif
65+
return std::error_code();
66+
}
67+
68+
inline std::error_code setFileTag(int FD, int CCSID, bool Text) {
69+
#ifdef __MVS__
70+
return setzOSFileTag(FD, CCSID, Text);
71+
#endif
72+
return std::error_code();
73+
}
74+
75+
inline ErrorOr<bool> needConversion(const char *FileName, const int FD = -1) {
76+
#ifdef __MVS__
77+
return needzOSConversion(FileName, FD);
78+
#endif
79+
return false;
80+
}
81+
82+
#ifdef __MVS__
83+
4184
/** \brief Disable the z/OS enhanced ASCII auto-conversion for the file
4285
* descriptor.
4386
*/
@@ -63,9 +106,8 @@ ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD = -1);
63106
*/
64107
ErrorOr<bool> needzOSConversion(const char *FileName, const int FD = -1);
65108

109+
#endif /* __MVS__*/
66110
} /* namespace llvm */
67111
#endif /* __cplusplus */
68112

69-
#endif /* __MVS__ */
70-
71113
#endif /* LLVM_SUPPORT_AUTOCONVERT_H */

llvm/lib/Support/AutoConvert.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,6 @@ int enablezOSAutoConversion(int FD) {
8383
return fcntl(FD, F_CONTROL_CVT, &Query);
8484
}
8585

86-
std::error_code llvm::disablezOSAutoConversion(int FD) {
87-
if (::disablezOSAutoConversion(FD) == -1)
88-
return errnoAsErrorCode();
89-
90-
return std::error_code();
91-
}
92-
93-
std::error_code llvm::enablezOSAutoConversion(int FD) {
94-
if (::enablezOSAutoConversion(FD) == -1)
95-
return errnoAsErrorCode();
96-
97-
return std::error_code();
98-
}
99-
100-
std::error_code llvm::restorezOSStdHandleAutoConversion(int FD) {
101-
if (::restorezOSStdHandleAutoConversion(FD) == -1)
102-
return errnoAsErrorCode();
103-
104-
return std::error_code();
105-
}
106-
10786
std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
10887
assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
10988
"FT_UNTAGGED and FT_BINARY are not allowed for text files");

llvm/lib/Support/InitLLVM.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,28 @@
1818
#include "llvm/Support/Windows/WindowsSupport.h"
1919
#endif
2020

21-
#ifdef __MVS__
21+
#if defined(HAVE_UNISTD_H)
2222
#include <unistd.h>
23+
#else
24+
#ifndef STDIN_FILENO
25+
#define STDIN_FILENO 0
26+
#endif
27+
#ifndef STDOUT_FILENO
28+
#define STDOUT_FILENO 1
29+
#endif
30+
#ifndef STDERR_FILENO
31+
#define STDERR_FILENO 2
32+
#endif
33+
#endif
2334

2435
void CleanupStdHandles(void *Cookie) {
2536
llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs();
2637
Outs->flush();
2738
Errs->flush();
28-
llvm::restorezOSStdHandleAutoConversion(STDIN_FILENO);
29-
llvm::restorezOSStdHandleAutoConversion(STDOUT_FILENO);
30-
llvm::restorezOSStdHandleAutoConversion(STDERR_FILENO);
39+
llvm::restoreStdHandleAutoConversion(STDIN_FILENO);
40+
llvm::restoreStdHandleAutoConversion(STDOUT_FILENO);
41+
llvm::restoreStdHandleAutoConversion(STDERR_FILENO);
3142
}
32-
#endif
3343

3444
using namespace llvm;
3545
using namespace llvm::sys;
@@ -41,10 +51,10 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
4151
assert(!Initialized && "InitLLVM was already initialized!");
4252
Initialized = true;
4353
#endif
44-
#ifdef __MVS__
54+
4555
// Bring stdin/stdout/stderr into a known state.
4656
sys::AddSignalHandler(CleanupStdHandles, nullptr);
47-
#endif
57+
4858
if (InstallPipeSignalExitHandler)
4959
// The pipe signal handler must be installed before any other handlers are
5060
// registered. This is because the Unix \ref RegisterHandlers function does
@@ -68,8 +78,8 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
6878

6979
// If turning on conversion for stderr fails then the error message
7080
// may be garbled. There is no solution to this problem.
71-
ExitOnErr(errorCodeToError(llvm::enablezOSAutoConversion(STDERR_FILENO)));
72-
ExitOnErr(errorCodeToError(llvm::enablezOSAutoConversion(STDOUT_FILENO)));
81+
ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDERR_FILENO)));
82+
ExitOnErr(errorCodeToError(llvm::enableAutoConversion(STDOUT_FILENO)));
7383
#endif
7484

7585
#ifdef _WIN32
@@ -97,8 +107,6 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
97107
}
98108

99109
InitLLVM::~InitLLVM() {
100-
#ifdef __MVS__
101110
CleanupStdHandles(nullptr);
102-
#endif
103111
llvm_shutdown();
104112
}

llvm/lib/Support/MemoryBuffer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/SmallString.h"
1616
#include "llvm/Config/config.h"
1717
#include "llvm/Support/Alignment.h"
18+
#include "llvm/Support/AutoConvert.h"
1819
#include "llvm/Support/Errc.h"
1920
#include "llvm/Support/Error.h"
2021
#include "llvm/Support/ErrorHandling.h"
@@ -34,9 +35,6 @@
3435
#include <io.h>
3536
#endif
3637

37-
#ifdef __MVS__
38-
#include "llvm/Support/AutoConvert.h"
39-
#endif
4038
using namespace llvm;
4139

4240
//===----------------------------------------------------------------------===//
@@ -508,15 +506,15 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
508506
}
509507

510508
#ifdef __MVS__
511-
ErrorOr<bool> NeedConversion = needzOSConversion(Filename.str().c_str(), FD);
512-
if (std::error_code EC = NeedConversion.getError())
509+
ErrorOr<bool> NeedsConversion = needConversion(Filename.str().c_str(), FD);
510+
if (std::error_code EC = NeedsConversion.getError())
513511
return EC;
514512
// File size may increase due to EBCDIC -> UTF-8 conversion, therefore we
515513
// cannot trust the file size and we create the memory buffer by copying
516514
// off the stream.
517515
// Note: This only works with the assumption of reading a full file (i.e,
518516
// Offset == 0 and MapSize == FileSize). Reading a file slice does not work.
519-
if (Offset == 0 && MapSize == FileSize && *NeedConversion)
517+
if (*NeedsConversion && Offset == 0 && MapSize == FileSize)
520518
return getMemoryBufferForStream(FD, Filename);
521519
#endif
522520

llvm/lib/Support/raw_ostream.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -894,21 +894,24 @@ void raw_fd_ostream::anchor() {}
894894
raw_fd_ostream &llvm::outs() {
895895
// Set buffer settings to model stdout behavior.
896896
std::error_code EC;
897-
#ifdef __MVS__
898-
EC = enablezOSAutoConversion(STDOUT_FILENO);
899-
assert(!EC);
900-
#endif
897+
898+
// On z/OS we need to enable auto conversion
899+
static std::error_code EC1 = enableAutoConversion(STDOUT_FILENO);
900+
assert(!EC1);
901+
(void)EC1;
902+
901903
static raw_fd_ostream S("-", EC, sys::fs::OF_None);
902904
assert(!EC);
903905
return S;
904906
}
905907

906908
raw_fd_ostream &llvm::errs() {
907-
// Set standard error to be unbuffered.
908-
#ifdef __MVS__
909-
std::error_code EC = enablezOSAutoConversion(STDERR_FILENO);
909+
// On z/OS we need to enable auto conversion
910+
static std::error_code EC = enableAutoConversion(STDERR_FILENO);
910911
assert(!EC);
911-
#endif
912+
(void)EC;
913+
914+
// Set standard error to be unbuffered.
912915
static raw_fd_ostream S(STDERR_FILENO, false, true);
913916
return S;
914917
}

0 commit comments

Comments
 (0)