[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
This commit is contained in:
Michael Kruse 2021-07-26 15:02:08 -05:00
parent 1901c98dd8
commit cbad57613e
2 changed files with 5 additions and 4 deletions

View File

@ -45,7 +45,8 @@ static int openfile_mkstemp(IoErrorHandler &handler) {
if (::GetTempFileNameA(tempDirName, "Fortran", uUnique, tempFileName) == 0) {
return -1;
}
int fd{::_open(tempFileName, _O_CREAT | _O_TEMPORARY, _S_IREAD | _S_IWRITE)};
int fd{::_open(
tempFileName, _O_CREAT | _O_TEMPORARY | _O_RDWR, _S_IREAD | _S_IWRITE)};
#else
char path[]{"/tmp/Fortran-Scratch-XXXXXX"};
int fd{::mkstemp(path)};
@ -245,7 +246,7 @@ std::size_t OpenFile::Write(FileOffset at, const char *buffer,
inline static int openfile_ftruncate(int fd, OpenFile::FileOffset at) {
#ifdef _WIN32
return !::_chsize(fd, at);
return ::_chsize(fd, at);
#else
return ::ftruncate(fd, at);
#endif

View File

@ -687,13 +687,13 @@ void ExternalFileUnit::BackspaceVariableFormattedRecord(
if (const char *p{
FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
recordOffsetInFrame_ = p - Frame() + 1;
*recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
break;
}
}
if (frameOffsetInFile_ == 0) {
recordOffsetInFrame_ = 0;
*recordLength = prevNL;
recordLength = prevNL;
break;
}
frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);