Skip to content

Commit 2f9ef51

Browse files
[𝘀𝗽𝗿] initial version
Created using spr 1.3.7
1 parent 8df194f commit 2f9ef51

File tree

10 files changed

+97
-54
lines changed

10 files changed

+97
-54
lines changed

llvm/include/llvm/Support/File.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===----------------------------------------------------------------------===//
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+
/// \file
10+
/// This file declares llvm::sys::fs::file_t type.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_SUPPORT_FILE_H
15+
#define LLVM_SUPPORT_FILE_H
16+
17+
namespace llvm::sys::fs {
18+
19+
/// This class wraps the platform specific file handle/descriptor type to
20+
/// provide an unified representation.
21+
struct file_t {
22+
#if defined(_WIN32)
23+
// A Win32 HANDLE is a typedef of void*
24+
using value_type = void *;
25+
static const value_type Invalid;
26+
#else
27+
// A file descriptor on UNIX.
28+
using value_type = int;
29+
static constexpr value_type Invalid = -1;
30+
#endif
31+
value_type Value;
32+
33+
/// Default constructor to invalid file.
34+
file_t() : Value(Invalid) {}
35+
36+
/// Implicit constructor from underlying value.
37+
// TODO: Make this explicit to flush out type mismatches.
38+
file_t(value_type Value) : Value(Value) {}
39+
40+
/// Is a valid file.
41+
bool isValid() const { return Value != Invalid; }
42+
43+
/// Get the underlying value and return a platform specific value.
44+
value_type get() const { return Value; }
45+
};
46+
47+
inline bool operator==(file_t LHS, file_t RHS) {
48+
return LHS.get() == RHS.get();
49+
}
50+
51+
inline bool operator!=(file_t LHS, file_t RHS) { return !(LHS == RHS); }
52+
53+
} // namespace llvm::sys::fs
54+
55+
#endif

llvm/include/llvm/Support/FileSystem.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/Support/Error.h"
3636
#include "llvm/Support/ErrorHandling.h"
3737
#include "llvm/Support/ErrorOr.h"
38+
#include "llvm/Support/File.h"
3839
#include "llvm/Support/FileSystem/UniqueID.h"
3940
#include "llvm/Support/MD5.h"
4041
#include <cassert>
@@ -49,15 +50,6 @@ namespace llvm {
4950
namespace sys {
5051
namespace fs {
5152

52-
#if defined(_WIN32)
53-
// A Win32 HANDLE is a typedef of void*
54-
using file_t = void *;
55-
#else
56-
using file_t = int;
57-
#endif
58-
59-
LLVM_ABI extern const file_t kInvalidFile;
60-
6153
/// An enumeration for the file system's view of the type.
6254
enum class file_type {
6355
status_error,
@@ -645,13 +637,11 @@ LLVM_ABI std::error_code is_other(const Twine &path, bool &result);
645637
LLVM_ABI std::error_code status(const Twine &path, file_status &result,
646638
bool follow = true);
647639

648-
/// A version for when a file descriptor is already available.
649-
LLVM_ABI std::error_code status(int FD, file_status &Result);
640+
/// A version for when a file is already available.
641+
LLVM_ABI std::error_code status(file_t F, file_status &Result);
650642

651-
#ifdef _WIN32
652643
/// A version for when a file descriptor is already available.
653-
LLVM_ABI std::error_code status(file_t FD, file_status &Result);
654-
#endif
644+
LLVM_ABI std::error_code status(int FD, file_status &Result);
655645

656646
/// Get file creation mode mask of the process.
657647
///
@@ -1000,15 +990,15 @@ LLVM_ABI Expected<file_t> openNativeFile(const Twine &Name,
1000990
LLVM_ABI file_t convertFDToNativeFile(int FD);
1001991

1002992
#ifndef _WIN32
1003-
inline file_t convertFDToNativeFile(int FD) { return FD; }
993+
inline file_t convertFDToNativeFile(int FD) { return file_t(FD); }
1004994
#endif
1005995

1006996
/// Return an open handle to standard in. On Unix, this is typically FD 0.
1007-
/// Returns kInvalidFile when the stream is closed.
997+
/// Returns Invalid file_t when the stream is closed.
1008998
LLVM_ABI file_t getStdinHandle();
1009999

10101000
/// Return an open handle to standard out. On Unix, this is typically FD 1.
1011-
/// Returns kInvalidFile when the stream is closed.
1001+
/// Returns Invalid file_t when the stream is closed.
10121002
LLVM_ABI file_t getStdoutHandle();
10131003

10141004
/// Return an open handle to standard error. On Unix, this is typically FD 2.

llvm/include/llvm/Support/MemoryBuffer.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,13 @@
2121
#include "llvm/Support/CBindingWrapping.h"
2222
#include "llvm/Support/Compiler.h"
2323
#include "llvm/Support/ErrorOr.h"
24+
#include "llvm/Support/File.h"
2425
#include "llvm/Support/MemoryBufferRef.h"
2526
#include <cstddef>
2627
#include <cstdint>
2728
#include <memory>
2829

2930
namespace llvm {
30-
namespace sys {
31-
namespace fs {
32-
// Duplicated from FileSystem.h to avoid a dependency.
33-
#if defined(_WIN32)
34-
// A Win32 HANDLE is a typedef of void*
35-
using file_t = void *;
36-
#else
37-
using file_t = int;
38-
#endif
39-
} // namespace fs
40-
} // namespace sys
4131

4232
/// This interface provides simple read-only access to a block of memory, and
4333
/// provides simple methods for reading files and standard input into a memory

llvm/lib/CAS/MappedFileRegionArena.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ Expected<int64_t> MappedFileRegionArena::allocateOffset(uint64_t AllocSize) {
373373
ErrorOr<FileSizeInfo> FileSizeInfo::get(sys::fs::file_t File) {
374374
#if LLVM_ON_UNIX && defined(MAPPED_FILE_BSIZE)
375375
struct stat Status;
376-
int StatRet = ::fstat(File, &Status);
376+
int StatRet = ::fstat(File.get(), &Status);
377377
if (StatRet)
378378
return errnoAsErrorCode();
379379
uint64_t AllocatedSize = uint64_t(Status.st_blksize) * MAPPED_FILE_BSIZE;

llvm/lib/Object/ArchiveWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
132132
if (!FDOrErr)
133133
return FDOrErr.takeError();
134134
sys::fs::file_t FD = *FDOrErr;
135-
assert(FD != sys::fs::kInvalidFile);
135+
assert(FD.isValid());
136136

137137
if (auto EC = sys::fs::status(FD, Status))
138138
return errorCodeToError(EC);

llvm/lib/Support/FileUtilities.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ Error FilePermissionsApplier::apply(
306306
return createFileError(OutputFilename, EC);
307307

308308
sys::fs::file_status OStat;
309-
if (std::error_code EC = sys::fs::status(FD, OStat))
309+
if (std::error_code EC =
310+
sys::fs::status(sys::fs::convertFDToNativeFile(FD), OStat))
310311
return createFileError(OutputFilename, EC);
311312
if (OStat.type() == sys::fs::file_type::regular_file) {
312313
#ifndef _WIN32

llvm/lib/Support/Unix/Path.inc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ namespace llvm {
120120
namespace sys {
121121
namespace fs {
122122

123-
const file_t kInvalidFile = -1;
124-
125123
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
126124
defined(__FreeBSD_kernel__) || defined(__linux__) || \
127125
defined(__CYGWIN__) || defined(__DragonFly__) || defined(_AIX) || \
@@ -768,6 +766,10 @@ std::error_code status(const Twine &Path, file_status &Result, bool Follow) {
768766
return fillStatus(StatRet, Status, Result);
769767
}
770768

769+
std::error_code status(file_t F, file_status &Result) {
770+
return status(F.get(), Result);
771+
}
772+
771773
std::error_code status(int FD, file_status &Result) {
772774
struct stat Status;
773775
int StatRet = ::fstat(FD, &Status);
@@ -832,7 +834,7 @@ std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime,
832834
#endif
833835
}
834836

835-
std::error_code mapped_file_region::init(int FD, uint64_t Offset,
837+
std::error_code mapped_file_region::init(file_t FD, uint64_t Offset,
836838
mapmode Mode) {
837839
assert(Size != 0);
838840

@@ -861,13 +863,13 @@ std::error_code mapped_file_region::init(int FD, uint64_t Offset,
861863
}
862864
#endif // #if defined (__APPLE__)
863865

864-
Mapping = ::mmap(nullptr, Size, prot, flags, FD, Offset);
866+
Mapping = ::mmap(nullptr, Size, prot, flags, FD.get(), Offset);
865867
if (Mapping == MAP_FAILED)
866868
return errnoAsErrorCode();
867869
return std::error_code();
868870
}
869871

870-
mapped_file_region::mapped_file_region(int fd, mapmode mode, size_t length,
872+
mapped_file_region::mapped_file_region(file_t fd, mapmode mode, size_t length,
871873
uint64_t offset, std::error_code &ec)
872874
: Size(length), Mode(mode) {
873875
(void)Mode;
@@ -1128,9 +1130,9 @@ std::error_code openFile(const Twine &Name, int &ResultFD,
11281130
return std::error_code();
11291131
}
11301132

1131-
Expected<int> openNativeFile(const Twine &Name, CreationDisposition Disp,
1132-
FileAccess Access, OpenFlags Flags,
1133-
unsigned Mode) {
1133+
Expected<file_t> openNativeFile(const Twine &Name, CreationDisposition Disp,
1134+
FileAccess Access, OpenFlags Flags,
1135+
unsigned Mode) {
11341136

11351137
int FD;
11361138
std::error_code EC = openFile(Name, FD, Disp, Access, Flags, Mode);
@@ -1183,7 +1185,7 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
11831185

11841186
Expected<file_t> openNativeFileForRead(const Twine &Name, OpenFlags Flags,
11851187
SmallVectorImpl<char> *RealPath) {
1186-
file_t ResultFD;
1188+
int ResultFD;
11871189
std::error_code EC = openFileForRead(Name, ResultFD, Flags, RealPath);
11881190
if (EC)
11891191
return errorCodeToError(EC);
@@ -1200,7 +1202,7 @@ Expected<size_t> readNativeFile(file_t FD, MutableArrayRef<char> Buf) {
12001202
#else
12011203
size_t Size = Buf.size();
12021204
#endif
1203-
ssize_t NumRead = sys::RetryAfterSignal(-1, ::read, FD, Buf.data(), Size);
1205+
ssize_t NumRead = sys::RetryAfterSignal(-1, ::read, FD.get(), Buf.data(), Size);
12041206
if (NumRead == -1)
12051207
return errorCodeToError(errnoAsErrorCode());
12061208
// The underlying operation on these platforms allow opening directories
@@ -1224,7 +1226,7 @@ Expected<size_t> readNativeFileSlice(file_t FD, MutableArrayRef<char> Buf,
12241226
#endif
12251227
#ifdef HAVE_PREAD
12261228
ssize_t NumRead =
1227-
sys::RetryAfterSignal(-1, ::pread, FD, Buf.data(), Size, Offset);
1229+
sys::RetryAfterSignal(-1, ::pread, FD.get(), Buf.data(), Size, Offset);
12281230
#else
12291231
if (lseek(FD, Offset, SEEK_SET) == -1)
12301232
return errorCodeToError(errnoAsErrorCode());
@@ -1297,8 +1299,8 @@ std::error_code unlockFile(int FD) {
12971299

12981300
std::error_code closeFile(file_t &F) {
12991301
file_t TmpF = F;
1300-
F = kInvalidFile;
1301-
return Process::SafelyCloseFileDescriptor(TmpF);
1302+
F = file_t::Invalid;
1303+
return Process::SafelyCloseFileDescriptor(TmpF.get());
13021304
}
13031305

13041306
template <typename T>

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ using namespace llvm::vfs;
5656
using llvm::sys::fs::file_t;
5757
using llvm::sys::fs::file_status;
5858
using llvm::sys::fs::file_type;
59-
using llvm::sys::fs::kInvalidFile;
6059
using llvm::sys::fs::perms;
6160
using llvm::sys::fs::UniqueID;
6261

@@ -198,7 +197,7 @@ class RealFile : public File {
198197
: FD(RawFD), S(NewName, {}, {}, {}, {}, {},
199198
llvm::sys::fs::file_type::status_error, {}),
200199
RealName(NewRealPathName.str()) {
201-
assert(FD != kInvalidFile && "Invalid or inactive file descriptor");
200+
assert(FD.isValid() && "Invalid or inactive file descriptor");
202201
}
203202

204203
public:
@@ -219,7 +218,7 @@ class RealFile : public File {
219218
RealFile::~RealFile() { close(); }
220219

221220
ErrorOr<Status> RealFile::status() {
222-
assert(FD != kInvalidFile && "cannot stat closed file");
221+
assert(FD.isValid() && "cannot stat closed file");
223222
if (!S.isStatusKnown()) {
224223
file_status RealStatus;
225224
if (std::error_code EC = sys::fs::status(FD, RealStatus))
@@ -236,14 +235,14 @@ ErrorOr<std::string> RealFile::getName() {
236235
ErrorOr<std::unique_ptr<MemoryBuffer>>
237236
RealFile::getBuffer(const Twine &Name, int64_t FileSize,
238237
bool RequiresNullTerminator, bool IsVolatile) {
239-
assert(FD != kInvalidFile && "cannot get buffer for closed file");
238+
assert(FD.isValid() && "cannot get buffer for closed file");
240239
return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
241240
IsVolatile);
242241
}
243242

244243
std::error_code RealFile::close() {
245244
std::error_code EC = sys::fs::closeFile(FD);
246-
FD = kInvalidFile;
245+
FD = file_t::Invalid;
247246
return EC;
248247
}
249248

llvm/lib/Support/Windows/Path.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16,
130130

131131
namespace fs {
132132

133-
const file_t kInvalidFile = INVALID_HANDLE_VALUE;
133+
const file_t::value_type file_t::Invalid = INVALID_HANDLE_VALUE;
134134

135135
std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
136136
SmallVector<wchar_t, MAX_PATH> PathName;
@@ -1397,7 +1397,7 @@ std::error_code unlockFile(int FD) {
13971397

13981398
std::error_code closeFile(file_t &F) {
13991399
file_t TmpF = F;
1400-
F = kInvalidFile;
1400+
F = file_t::Invalid;
14011401
if (!::CloseHandle(TmpF))
14021402
return mapWindowsError(::GetLastError());
14031403
return std::error_code();

llvm/unittests/Support/MemoryBufferTest.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,22 @@ TEST_F(MemoryBufferTest, copy) {
170170

171171
#if LLVM_ENABLE_THREADS
172172
TEST_F(MemoryBufferTest, createFromPipe) {
173-
sys::fs::file_t pipes[2];
173+
int pipes[2];
174174
#if LLVM_ON_UNIX
175175
ASSERT_EQ(::pipe(pipes), 0) << strerror(errno);
176176
#else
177177
ASSERT_TRUE(::CreatePipe(&pipes[0], &pipes[1], nullptr, 0))
178178
<< ::GetLastError();
179179
#endif
180-
auto ReadCloser = make_scope_exit([&] { sys::fs::closeFile(pipes[0]); });
180+
auto ReadCloser = make_scope_exit([&] {
181+
sys::fs::file_t F(pipes[0]);
182+
sys::fs::closeFile(F);
183+
});
181184
std::thread Writer([&] {
182-
auto WriteCloser = make_scope_exit([&] { sys::fs::closeFile(pipes[1]); });
185+
auto WriteCloser = make_scope_exit([&] {
186+
sys::fs::file_t F(pipes[1]);
187+
sys::fs::closeFile(F);
188+
});
183189
for (unsigned i = 0; i < 5; ++i) {
184190
std::this_thread::sleep_for(std::chrono::milliseconds(10));
185191
#if LLVM_ON_UNIX

0 commit comments

Comments
 (0)