Skip to content

Commit 52f2a94

Browse files
tltaoTony Tao
andauthored
[Support] Prevent loss of file type flags when creating temporary (#167939)
Non-binary output files from the compiler need the `OF_Text` flag set for encoding conversion to be performed correctly on z/OS. --------- Co-authored-by: Tony Tao <[email protected]>
1 parent 420d56a commit 52f2a94

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Checks encoding of output file
2+
// This is only required for z/OS.
3+
//
4+
// REQUIRES: system-zos, systemz-registered-target
5+
// RUN: %clang_cc1 -triple s390x-ibm-zos -S %s -o %t.s
6+
// RUN: ls -T %t.s | FileCheck %s
7+
8+
// CHECK: t IBM-1047 T=on
9+
void foo() { return; }

llvm/lib/Support/VirtualOutputBackends.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,20 @@ static Error createDirectoriesOnDemand(StringRef OutputPath,
254254
});
255255
}
256256

257+
static sys::fs::OpenFlags generateFlagsFromConfig(OutputConfig Config) {
258+
sys::fs::OpenFlags OF = sys::fs::OF_None;
259+
if (Config.getTextWithCRLF())
260+
OF |= sys::fs::OF_TextWithCRLF;
261+
else if (Config.getText())
262+
OF |= sys::fs::OF_Text;
263+
// Don't pass OF_Append if writting to temporary since OF_Append is
264+
// not Atomic Append
265+
if (Config.getAppend() && !Config.getAtomicWrite())
266+
OF |= sys::fs::OF_Append;
267+
268+
return OF;
269+
}
270+
257271
Error OnDiskOutputFile::tryToCreateTemporary(std::optional<int> &FD) {
258272
// Create a temporary file.
259273
// Insert -%%%%%%%% before the extension (if any), and because some tools
@@ -269,8 +283,9 @@ Error OnDiskOutputFile::tryToCreateTemporary(std::optional<int> &FD) {
269283
return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error {
270284
int NewFD;
271285
SmallString<128> UniquePath;
286+
sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
272287
if (std::error_code EC =
273-
sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath))
288+
sys::fs::createUniqueFile(ModelPath, NewFD, UniquePath, OF))
274289
return make_error<TempFileOutputError>(ModelPath, OutputPath, EC);
275290

276291
if (Config.getDiscardOnSignal())
@@ -312,13 +327,7 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
312327
// Not using a temporary file. Open the final output file.
313328
return createDirectoriesOnDemand(OutputPath, Config, [&]() -> Error {
314329
int NewFD;
315-
sys::fs::OpenFlags OF = sys::fs::OF_None;
316-
if (Config.getTextWithCRLF())
317-
OF |= sys::fs::OF_TextWithCRLF;
318-
else if (Config.getText())
319-
OF |= sys::fs::OF_Text;
320-
if (Config.getAppend())
321-
OF |= sys::fs::OF_Append;
330+
sys::fs::OpenFlags OF = generateFlagsFromConfig(Config);
322331
if (std::error_code EC = sys::fs::openFileForWrite(
323332
OutputPath, NewFD, sys::fs::CD_CreateAlways, OF))
324333
return convertToOutputError(OutputPath, EC);

0 commit comments

Comments
 (0)