Skip to content

Commit cbad576

Browse files
committed
[flang][msvc] Fix external-io unittest.
Fix the external-io unittest under Windows. In particular, fixes the following issues: 1. When creating a temporary file, open it with read+write permissions using the _O_RDWR flag. _S_IREAD and _S_IWRITE are for the file permissions of the created file. 2. _chsize returns 0 on success (just like ftruncate). 3. To set a std::optional, use its assign-operator overload instead of getting a reference to its value and overwrite that. The latter is invalid if the std::optional has no value, and is caught by msvc's debug STL. The non-GTest unittest is currently not executed under Windows because of the added .exe extension to the output file: external-io.text.exe. llvm-lit skips the file because .exe is not in the lists of test suffixes (.test is). D105315 is going to change that by converting it to a GTest-test. Reviewed By: awarzynski Differential Revision: https://reviews.llvm.org/D106726
1 parent 1901c98 commit cbad576

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

flang/runtime/file.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ static int openfile_mkstemp(IoErrorHandler &handler) {
4545
if (::GetTempFileNameA(tempDirName, "Fortran", uUnique, tempFileName) == 0) {
4646
return -1;
4747
}
48-
int fd{::_open(tempFileName, _O_CREAT | _O_TEMPORARY, _S_IREAD | _S_IWRITE)};
48+
int fd{::_open(
49+
tempFileName, _O_CREAT | _O_TEMPORARY | _O_RDWR, _S_IREAD | _S_IWRITE)};
4950
#else
5051
char path[]{"/tmp/Fortran-Scratch-XXXXXX"};
5152
int fd{::mkstemp(path)};
@@ -245,7 +246,7 @@ std::size_t OpenFile::Write(FileOffset at, const char *buffer,
245246

246247
inline static int openfile_ftruncate(int fd, OpenFile::FileOffset at) {
247248
#ifdef _WIN32
248-
return !::_chsize(fd, at);
249+
return ::_chsize(fd, at);
249250
#else
250251
return ::ftruncate(fd, at);
251252
#endif

flang/runtime/unit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,13 @@ void ExternalFileUnit::BackspaceVariableFormattedRecord(
687687
if (const char *p{
688688
FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
689689
recordOffsetInFrame_ = p - Frame() + 1;
690-
*recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
690+
recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
691691
break;
692692
}
693693
}
694694
if (frameOffsetInFile_ == 0) {
695695
recordOffsetInFrame_ = 0;
696-
*recordLength = prevNL;
696+
recordLength = prevNL;
697697
break;
698698
}
699699
frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);

0 commit comments

Comments
 (0)