[clang][bytecode] Fix reporting failed local constexpr initializers (#123588)
[llvm-project.git] / llvm / lib / Support / AutoConvert.cpp
blobf7918548df1d0d3e5e455ddebc4cf76918303312
1 //===- AutoConvert.cpp - Auto conversion between ASCII/EBCDIC -------------===//
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 //
9 // This file contains functions used for auto conversion between
10 // ASCII/EBCDIC codepages specific to z/OS.
12 //===----------------------------------------------------------------------===//
14 #ifdef __MVS__
16 #include "llvm/Support/AutoConvert.h"
17 #include "llvm/Support/Error.h"
18 #include <cassert>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
23 using namespace llvm;
25 static int savedStdHandleAutoConversionMode[3] = {-1, -1, -1};
27 int disablezOSAutoConversion(int FD) {
28 static const struct f_cnvrt Convert = {
29 SETCVTOFF, // cvtcmd
30 0, // pccsid
31 0, // fccsid
34 return fcntl(FD, F_CONTROL_CVT, &Convert);
37 int restorezOSStdHandleAutoConversion(int FD) {
38 assert(FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO);
39 if (savedStdHandleAutoConversionMode[FD] == -1)
40 return 0;
41 struct f_cnvrt Cvt = {
42 savedStdHandleAutoConversionMode[FD], // cvtcmd
43 0, // pccsid
44 0, // fccsid
46 return (fcntl(FD, F_CONTROL_CVT, &Cvt));
49 int enablezOSAutoConversion(int FD) {
50 struct f_cnvrt Query = {
51 QUERYCVT, // cvtcmd
52 0, // pccsid
53 0, // fccsid
56 if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
57 return -1;
59 // We don't need conversion for UTF-8 tagged files.
60 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
61 // problems related to UTF-8 tagged source files.
62 // When the pccsid is not ISO8859-1, autoconversion is still needed.
63 if (Query.pccsid == CCSID_ISO8859_1 &&
64 (Query.fccsid == CCSID_UTF_8 || Query.fccsid == CCSID_ISO8859_1))
65 return 0;
67 // Save the state of std handles before we make changes to it.
68 if ((FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO) &&
69 savedStdHandleAutoConversionMode[FD] == -1)
70 savedStdHandleAutoConversionMode[FD] = Query.cvtcmd;
72 if (FD == STDOUT_FILENO || FD == STDERR_FILENO)
73 Query.cvtcmd = SETCVTON;
74 else
75 Query.cvtcmd = SETCVTALL;
77 Query.pccsid =
78 (FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO)
79 ? 0
80 : CCSID_UTF_8;
81 // Assume untagged files to be IBM-1047 encoded.
82 Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid;
83 return fcntl(FD, F_CONTROL_CVT, &Query);
86 std::error_code llvm::disablezOSAutoConversion(int FD) {
87 if (::disablezOSAutoConversion(FD) == -1)
88 return errnoAsErrorCode();
90 return std::error_code();
93 std::error_code llvm::enablezOSAutoConversion(int FD) {
94 if (::enablezOSAutoConversion(FD) == -1)
95 return errnoAsErrorCode();
97 return std::error_code();
100 std::error_code llvm::restorezOSStdHandleAutoConversion(int FD) {
101 if (::restorezOSStdHandleAutoConversion(FD) == -1)
102 return errnoAsErrorCode();
104 return std::error_code();
107 std::error_code llvm::setzOSFileTag(int FD, int CCSID, bool Text) {
108 assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
109 "FT_UNTAGGED and FT_BINARY are not allowed for text files");
110 struct file_tag Tag;
111 Tag.ft_ccsid = CCSID;
112 Tag.ft_txtflag = Text;
113 Tag.ft_deferred = 0;
114 Tag.ft_rsvflags = 0;
116 if (fcntl(FD, F_SETTAG, &Tag) == -1)
117 return errnoAsErrorCode();
118 return std::error_code();
121 ErrorOr<__ccsid_t> llvm::getzOSFileTag(const char *FileName, const int FD) {
122 // If we have a file descriptor, use it to find out file tagging. Otherwise we
123 // need to use stat() with the file path.
124 if (FD != -1) {
125 struct f_cnvrt Query = {
126 QUERYCVT, // cvtcmd
127 0, // pccsid
128 0, // fccsid
130 if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
131 return std::error_code(errno, std::generic_category());
132 return Query.fccsid;
134 struct stat Attr;
135 if (stat(FileName, &Attr) == -1)
136 return std::error_code(errno, std::generic_category());
137 return Attr.st_tag.ft_ccsid;
140 ErrorOr<bool> llvm::needzOSConversion(const char *FileName, const int FD) {
141 ErrorOr<__ccsid_t> Ccsid = getzOSFileTag(FileName, FD);
142 if (std::error_code EC = Ccsid.getError())
143 return EC;
144 // We don't need conversion for UTF-8 tagged files or binary files.
145 // TODO: Remove the assumption of ISO8859-1 = UTF-8 here when we fully resolve
146 // problems related to UTF-8 tagged source files.
147 switch (*Ccsid) {
148 case CCSID_UTF_8:
149 case CCSID_ISO8859_1:
150 case FT_BINARY:
151 return false;
152 default:
153 return true;
157 #endif //__MVS__