Skip to content

Commit cedf7e2

Browse files
committed
refactor AutoConvert.h to remove MVS guard
1 parent cef5a31 commit cedf7e2

File tree

7 files changed

+59
-27
lines changed

7 files changed

+59
-27
lines changed

clang/tools/c-index-test/c-index-test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5199,13 +5199,13 @@ static void flush_atexit(void) {
51995199
int main(int argc, const char **argv) {
52005200
thread_info client_data;
52015201

5202-
#ifdef __MVS__
5202+
// On z/OS we need to enable auto conversion
52035203
if (enablezOSAutoConversion(fileno(stdout)) == -1)
52045204
fprintf(stderr, "Setting conversion on stdout failed\n");
52055205

5206+
// On z/OS we need to enable auto conversion
52065207
if (enablezOSAutoConversion(fileno(stderr)) == -1)
52075208
fprintf(stderr, "Setting conversion on stderr failed\n");
5208-
#endif
52095209

52105210
atexit(flush_atexit);
52115211

llvm/include/llvm/Support/AutoConvert.h

Lines changed: 13 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>
@@ -25,6 +26,16 @@
2526
#define CCSID_UTF_8 1208
2627
#define CCSID_ISO8859_1 819
2728

29+
#ifndef STDIN_FILENO
30+
# define STDIN_FILENO 0
31+
#endif
32+
#ifndef STDOUT_FILENO
33+
# define STDOUT_FILENO 1
34+
#endif
35+
#ifndef STDERR_FILENO
36+
# define STDERR_FILENO 2
37+
#endif
38+
2839
#ifdef __cplusplus
2940
extern "C" {
3041
#endif /* __cplusplus */
@@ -55,8 +66,10 @@ std::error_code restorezOSStdHandleAutoConversion(int FD);
5566
/** \brief Set the tag information for a file descriptor. */
5667
std::error_code setzOSFileTag(int FD, int CCSID, bool Text);
5768

69+
#ifdef __MVS__
5870
/** \brief Get the the tag ccsid for a file name or a file descriptor. */
5971
ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD = -1);
72+
#endif
6073

6174
/** \brief Query the file tag to determine if it needs conversion to UTF-8
6275
* codepage.
@@ -66,6 +79,4 @@ ErrorOr<bool> needzOSConversion(const char *FileName, const int FD = -1);
6679
} /* namespace llvm */
6780
#endif /* __cplusplus */
6881

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

llvm/lib/Support/AutoConvert.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,37 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#ifdef __MVS__
15-
1614
#include "llvm/Support/AutoConvert.h"
1715
#include "llvm/Support/Error.h"
1816
#include <cassert>
1917
#include <fcntl.h>
2018
#include <sys/stat.h>
19+
#if defined(HAVE_UNISTD_H)
2120
#include <unistd.h>
21+
#endif
2222

2323
using namespace llvm;
2424

2525
static int savedStdHandleAutoConversionMode[3] = {-1, -1, -1};
2626

2727
int disablezOSAutoConversion(int FD) {
28+
#ifndef __MVS__
29+
return 0;
30+
#else
2831
static const struct f_cnvrt Convert = {
2932
SETCVTOFF, // cvtcmd
3033
0, // pccsid
3134
0, // fccsid
3235
};
3336

3437
return fcntl(FD, F_CONTROL_CVT, &Convert);
38+
#endif
3539
}
3640

3741
int restorezOSStdHandleAutoConversion(int FD) {
42+
#ifndef __MVS__
43+
return 0;
44+
#else
3845
assert(FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO);
3946
if (savedStdHandleAutoConversionMode[FD] == -1)
4047
return 0;
@@ -44,9 +51,13 @@ int restorezOSStdHandleAutoConversion(int FD) {
4451
0, // fccsid
4552
};
4653
return (fcntl(FD, F_CONTROL_CVT, &Cvt));
54+
#endif
4755
}
4856

4957
int enablezOSAutoConversion(int FD) {
58+
#ifndef __MVS__
59+
return 0;
60+
#else
5061
struct f_cnvrt Query = {
5162
QUERYCVT, // cvtcmd
5263
0, // pccsid
@@ -81,30 +92,35 @@ int enablezOSAutoConversion(int FD) {
8192
// Assume untagged files to be IBM-1047 encoded.
8293
Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid;
8394
return fcntl(FD, F_CONTROL_CVT, &Query);
95+
#endif
8496
}
8597

8698
std::error_code llvm::disablezOSAutoConversion(int FD) {
99+
#ifdef __MVS__
87100
if (::disablezOSAutoConversion(FD) == -1)
88101
return errnoAsErrorCode();
89-
102+
#endif
90103
return std::error_code();
91104
}
92105

93106
std::error_code llvm::enablezOSAutoConversion(int FD) {
107+
#ifdef __MVS__
94108
if (::enablezOSAutoConversion(FD) == -1)
95109
return errnoAsErrorCode();
96-
110+
#endif
97111
return std::error_code();
98112
}
99113

100114
std::error_code llvm::restorezOSStdHandleAutoConversion(int FD) {
115+
#ifdef __MVS__
101116
if (::restorezOSStdHandleAutoConversion(FD) == -1)
102117
return errnoAsErrorCode();
103-
118+
#endif
104119
return std::error_code();
105120
}
106121

107122
std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
123+
#ifdef __MVS__
108124
assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
109125
"FT_UNTAGGED and FT_BINARY are not allowed for text files");
110126
struct file_tag Tag;
@@ -115,9 +131,11 @@ std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
115131

116132
if (fcntl(FD, F_SETTAG, &Tag) == -1)
117133
return errnoAsErrorCode();
134+
#endif
118135
return std::error_code();
119136
}
120137

138+
#ifdef __MVS__
121139
ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
122140
// If we have a file descriptor, use it to find out file tagging. Otherwise we
123141
// need to use stat() with the file path.
@@ -136,8 +154,12 @@ ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
136154
return std::error_code(errno, std::generic_category());
137155
return Attr.st_tag.ft_ccsid;
138156
}
157+
#endif
139158

140159
ErrorOr<bool> llvm::needzOSConversion(const char *FileName, const int FD) {
160+
#ifndef __MVS__
161+
return false;
162+
#else
141163
ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(FileName, FD);
142164
if (std::error_code EC = Ccsid.getError())
143165
return EC;
@@ -152,6 +174,5 @@ ErrorOr<bool> llvm::needzOSConversion(const char *FileName, const int FD) {
152174
default:
153175
return true;
154176
}
177+
#endif
155178
}
156-
157-
#endif //__MVS__

llvm/lib/Support/InitLLVM.cpp

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

21-
#ifdef __MVS__
21+
#if defined(HAVE_UNISTD_H)
2222
#include <unistd.h>
23+
#endif
2324

2425
void CleanupStdHandles(void *Cookie) {
2526
llvm::raw_ostream *Outs = &llvm::outs(), *Errs = &llvm::errs();
@@ -29,7 +30,6 @@ void CleanupStdHandles(void *Cookie) {
2930
llvm::restorezOSStdHandleAutoConversion(STDOUT_FILENO);
3031
llvm::restorezOSStdHandleAutoConversion(STDERR_FILENO);
3132
}
32-
#endif
3333

3434
using namespace llvm;
3535
using namespace llvm::sys;
@@ -41,10 +41,10 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
4141
assert(!Initialized && "InitLLVM was already initialized!");
4242
Initialized = true;
4343
#endif
44-
#ifdef __MVS__
44+
4545
// Bring stdin/stdout/stderr into a known state.
4646
sys::AddSignalHandler(CleanupStdHandles, nullptr);
47-
#endif
47+
4848
if (InstallPipeSignalExitHandler)
4949
// The pipe signal handler must be installed before any other handlers are
5050
// registered. This is because the Unix \ref RegisterHandlers function does
@@ -97,8 +97,6 @@ InitLLVM::InitLLVM(int &Argc, const char **&Argv,
9797
}
9898

9999
InitLLVM::~InitLLVM() {
100-
#ifdef __MVS__
101100
CleanupStdHandles(nullptr);
102-
#endif
103101
llvm_shutdown();
104102
}

llvm/lib/Support/MemoryBuffer.cpp

Lines changed: 2 additions & 4 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
//===----------------------------------------------------------------------===//
@@ -516,7 +514,7 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
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 (*NeedConversion && Offset == 0 && MapSize == FileSize)
520518
return getMemoryBufferForStream(FD, Filename);
521519
#endif
522520

llvm/lib/Support/raw_ostream.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,21 +894,23 @@ 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__
897+
898+
// On z/OS we need to enable auto conversion
898899
EC = enablezOSAutoConversion(STDOUT_FILENO);
899900
assert(!EC);
900-
#endif
901+
901902
static raw_fd_ostream S("-", EC, sys::fs::OF_None);
902903
assert(!EC);
903904
return S;
904905
}
905906

906907
raw_fd_ostream &llvm::errs() {
907908
// Set standard error to be unbuffered.
908-
#ifdef __MVS__
909+
910+
// On z/OS we need to enable auto conversion
909911
std::error_code EC = enablezOSAutoConversion(STDERR_FILENO);
910912
assert(!EC);
911-
#endif
913+
912914
static raw_fd_ostream S(STDERR_FILENO, false, true);
913915
return S;
914916
}

llvm/utils/count/count.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
#include <stdlib.h>
1212

1313
int main(int argc, char **argv) {
14-
#ifdef __MVS__
14+
15+
// On z/OS we need to enable auto conversion
1516
if (enablezOSAutoConversion(fileno(stdin)) == -1)
1617
fprintf(stderr, "Setting conversion on stdin failed\n");
1718

19+
// On z/OS we need to enable auto conversion
1820
if (enablezOSAutoConversion(fileno(stderr)) == -1)
1921
fprintf(stdout, "Setting conversion on stderr failed\n");
20-
#endif
22+
2123
size_t Count, NumLines, NumRead;
2224
char Buffer[4096], *End;
2325

0 commit comments

Comments
 (0)