Skip to content

Commit 23ed570

Browse files
committed
Split out llvm/Support/FileSystem/UniqueID.h and clang/Basic/FileEntry.h, NFC
Split `FileEntry` and `FileEntryRef` out into a new file `clang/Basic/FileEntry.h`. This allows current users of a forward-declared `FileEntry` to transition to `FileEntryRef` without adding more includers of `FileManager.h`. Also split `UniqueID` out to llvm/Support/FileSystem/UniqueID.h, so `FileEntry.h` doesn't need to include all of `FileSystem.h` for just that type. Differential Revision: https://reviews.llvm.org/D89761
1 parent 49cddb9 commit 23ed570

File tree

6 files changed

+245
-157
lines changed

6 files changed

+245
-157
lines changed

clang/include/clang/Basic/FileEntry.h

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
//===- clang/Basic/FileEntry.h - File references ----------------*- C++ -*-===//
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+
/// Defines interfaces for clang::FileEntry and clang::FileEntryRef.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_BASIC_FILEENTRY_H
15+
#define LLVM_CLANG_BASIC_FILEENTRY_H
16+
17+
#include "clang/Basic/LLVM.h"
18+
#include "llvm/ADT/PointerUnion.h"
19+
#include "llvm/ADT/StringMap.h"
20+
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/Support/ErrorOr.h"
22+
#include "llvm/Support/FileSystem/UniqueID.h"
23+
24+
namespace llvm {
25+
namespace vfs {
26+
27+
class File;
28+
29+
} // namespace vfs
30+
} // namespace llvm
31+
32+
namespace clang {
33+
34+
class DirectoryEntry;
35+
class FileEntry;
36+
37+
/// A reference to a \c FileEntry that includes the name of the file as it was
38+
/// accessed by the FileManager's client.
39+
class FileEntryRef {
40+
public:
41+
const StringRef getName() const { return Entry->first(); }
42+
const FileEntry &getFileEntry() const {
43+
return *Entry->second->V.get<FileEntry *>();
44+
}
45+
46+
inline bool isValid() const;
47+
inline off_t getSize() const;
48+
inline unsigned getUID() const;
49+
inline const llvm::sys::fs::UniqueID &getUniqueID() const;
50+
inline time_t getModificationTime() const;
51+
52+
friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS) {
53+
return LHS.Entry == RHS.Entry;
54+
}
55+
friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS) {
56+
return !(LHS == RHS);
57+
}
58+
59+
struct MapValue;
60+
61+
/// Type used in the StringMap.
62+
using MapEntry = llvm::StringMapEntry<llvm::ErrorOr<MapValue>>;
63+
64+
/// Type stored in the StringMap.
65+
struct MapValue {
66+
/// The pointer at another MapEntry is used when the FileManager should
67+
/// silently forward from one name to another, which occurs in Redirecting
68+
/// VFSs that use external names. In that case, the \c FileEntryRef
69+
/// returned by the \c FileManager will have the external name, and not the
70+
/// name that was used to lookup the file.
71+
///
72+
/// The second type is really a `const MapEntry *`, but that confuses
73+
/// gcc5.3. Once that's no longer supported, change this back.
74+
llvm::PointerUnion<FileEntry *, const void *> V;
75+
76+
MapValue() = delete;
77+
MapValue(FileEntry &FE) : V(&FE) {}
78+
MapValue(MapEntry &ME) : V(&ME) {}
79+
};
80+
81+
private:
82+
friend class FileManager;
83+
84+
FileEntryRef() = delete;
85+
explicit FileEntryRef(const MapEntry &Entry)
86+
: Entry(&Entry) {
87+
assert(Entry.second && "Expected payload");
88+
assert(Entry.second->V && "Expected non-null");
89+
assert(Entry.second->V.is<FileEntry *>() && "Expected FileEntry");
90+
}
91+
92+
const MapEntry *Entry;
93+
};
94+
95+
/// Cached information about one file (either on disk
96+
/// or in the virtual file system).
97+
///
98+
/// If the 'File' member is valid, then this FileEntry has an open file
99+
/// descriptor for the file.
100+
class FileEntry {
101+
friend class FileManager;
102+
103+
std::string RealPathName; // Real path to the file; could be empty.
104+
off_t Size; // File size in bytes.
105+
time_t ModTime; // Modification time of file.
106+
const DirectoryEntry *Dir; // Directory file lives in.
107+
llvm::sys::fs::UniqueID UniqueID;
108+
unsigned UID; // A unique (small) ID for the file.
109+
bool IsNamedPipe;
110+
bool IsValid; // Is this \c FileEntry initialized and valid?
111+
112+
/// The open file, if it is owned by the \p FileEntry.
113+
mutable std::unique_ptr<llvm::vfs::File> File;
114+
115+
// First access name for this FileEntry.
116+
//
117+
// This is Optional only to allow delayed construction (FileEntryRef has no
118+
// default constructor). It should always have a value in practice.
119+
//
120+
// TODO: remove this once everyone that needs a name uses FileEntryRef.
121+
Optional<FileEntryRef> LastRef;
122+
123+
public:
124+
FileEntry();
125+
~FileEntry();
126+
127+
FileEntry(const FileEntry &) = delete;
128+
FileEntry &operator=(const FileEntry &) = delete;
129+
130+
StringRef getName() const { return LastRef->getName(); }
131+
FileEntryRef getLastRef() const { return *LastRef; }
132+
133+
StringRef tryGetRealPathName() const { return RealPathName; }
134+
bool isValid() const { return IsValid; }
135+
off_t getSize() const { return Size; }
136+
unsigned getUID() const { return UID; }
137+
const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
138+
time_t getModificationTime() const { return ModTime; }
139+
140+
/// Return the directory the file lives in.
141+
const DirectoryEntry *getDir() const { return Dir; }
142+
143+
bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; }
144+
145+
/// Check whether the file is a named pipe (and thus can't be opened by
146+
/// the native FileManager methods).
147+
bool isNamedPipe() const { return IsNamedPipe; }
148+
149+
void closeFile() const;
150+
151+
// Only for use in tests to see if deferred opens are happening, rather than
152+
// relying on RealPathName being empty.
153+
bool isOpenForTests() const { return File != nullptr; }
154+
};
155+
156+
bool FileEntryRef::isValid() const { return getFileEntry().isValid(); }
157+
158+
off_t FileEntryRef::getSize() const { return getFileEntry().getSize(); }
159+
160+
unsigned FileEntryRef::getUID() const { return getFileEntry().getUID(); }
161+
162+
const llvm::sys::fs::UniqueID &FileEntryRef::getUniqueID() const {
163+
return getFileEntry().getUniqueID();
164+
}
165+
166+
time_t FileEntryRef::getModificationTime() const {
167+
return getFileEntry().getModificationTime();
168+
}
169+
170+
} // end namespace clang
171+
172+
#endif // LLVM_CLANG_BASIC_FILEENTRY_H

clang/include/clang/Basic/FileManager.h

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_BASIC_FILEMANAGER_H
1515
#define LLVM_CLANG_BASIC_FILEMANAGER_H
1616

17+
#include "clang/Basic/FileEntry.h"
1718
#include "clang/Basic/FileSystemOptions.h"
1819
#include "clang/Basic/LLVM.h"
1920
#include "llvm/ADT/DenseMap.h"
@@ -71,143 +72,6 @@ class DirectoryEntryRef {
7172
const llvm::StringMapEntry<llvm::ErrorOr<DirectoryEntry &>> *Entry;
7273
};
7374

74-
class FileEntry;
75-
76-
/// A reference to a \c FileEntry that includes the name of the file as it was
77-
/// accessed by the FileManager's client.
78-
class FileEntryRef {
79-
public:
80-
const StringRef getName() const { return Entry->first(); }
81-
const FileEntry &getFileEntry() const {
82-
return *Entry->second->V.get<FileEntry *>();
83-
}
84-
85-
inline bool isValid() const;
86-
inline off_t getSize() const;
87-
inline unsigned getUID() const;
88-
inline const llvm::sys::fs::UniqueID &getUniqueID() const;
89-
inline time_t getModificationTime() const;
90-
91-
friend bool operator==(const FileEntryRef &LHS, const FileEntryRef &RHS) {
92-
return LHS.Entry == RHS.Entry;
93-
}
94-
friend bool operator!=(const FileEntryRef &LHS, const FileEntryRef &RHS) {
95-
return !(LHS == RHS);
96-
}
97-
98-
struct MapValue;
99-
100-
/// Type used in the StringMap.
101-
using MapEntry = llvm::StringMapEntry<llvm::ErrorOr<MapValue>>;
102-
103-
/// Type stored in the StringMap.
104-
struct MapValue {
105-
/// The pointer at another MapEntry is used when the FileManager should
106-
/// silently forward from one name to another, which occurs in Redirecting
107-
/// VFSs that use external names. In that case, the \c FileEntryRef
108-
/// returned by the \c FileManager will have the external name, and not the
109-
/// name that was used to lookup the file.
110-
// The second type is really a `const MapEntry *`, but that confuses gcc5.3.
111-
// Once that's no longer supported, change this back.
112-
llvm::PointerUnion<FileEntry *, const void *> V;
113-
114-
MapValue() = delete;
115-
MapValue(FileEntry &FE) : V(&FE) {}
116-
MapValue(MapEntry &ME) : V(&ME) {}
117-
};
118-
119-
private:
120-
friend class FileManager;
121-
122-
FileEntryRef() = delete;
123-
explicit FileEntryRef(const MapEntry &Entry)
124-
: Entry(&Entry) {
125-
assert(Entry.second && "Expected payload");
126-
assert(Entry.second->V && "Expected non-null");
127-
assert(Entry.second->V.is<FileEntry *>() && "Expected FileEntry");
128-
}
129-
130-
const MapEntry *Entry;
131-
};
132-
133-
/// Cached information about one file (either on disk
134-
/// or in the virtual file system).
135-
///
136-
/// If the 'File' member is valid, then this FileEntry has an open file
137-
/// descriptor for the file.
138-
class FileEntry {
139-
friend class FileManager;
140-
141-
std::string RealPathName; // Real path to the file; could be empty.
142-
off_t Size; // File size in bytes.
143-
time_t ModTime; // Modification time of file.
144-
const DirectoryEntry *Dir; // Directory file lives in.
145-
llvm::sys::fs::UniqueID UniqueID;
146-
unsigned UID; // A unique (small) ID for the file.
147-
bool IsNamedPipe;
148-
bool IsValid; // Is this \c FileEntry initialized and valid?
149-
150-
/// The open file, if it is owned by the \p FileEntry.
151-
mutable std::unique_ptr<llvm::vfs::File> File;
152-
153-
// First access name for this FileEntry.
154-
//
155-
// This is Optional only to allow delayed construction (FileEntryRef has no
156-
// default constructor). It should always have a value in practice.
157-
//
158-
// TODO: remote this once everyone that needs a name uses FileEntryRef.
159-
Optional<FileEntryRef> LastRef;
160-
161-
public:
162-
FileEntry()
163-
: UniqueID(0, 0), IsNamedPipe(false), IsValid(false)
164-
{}
165-
166-
FileEntry(const FileEntry &) = delete;
167-
FileEntry &operator=(const FileEntry &) = delete;
168-
169-
StringRef getName() const { return LastRef->getName(); }
170-
FileEntryRef getLastRef() const { return *LastRef; }
171-
172-
StringRef tryGetRealPathName() const { return RealPathName; }
173-
bool isValid() const { return IsValid; }
174-
off_t getSize() const { return Size; }
175-
unsigned getUID() const { return UID; }
176-
const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
177-
time_t getModificationTime() const { return ModTime; }
178-
179-
/// Return the directory the file lives in.
180-
const DirectoryEntry *getDir() const { return Dir; }
181-
182-
bool operator<(const FileEntry &RHS) const { return UniqueID < RHS.UniqueID; }
183-
184-
/// Check whether the file is a named pipe (and thus can't be opened by
185-
/// the native FileManager methods).
186-
bool isNamedPipe() const { return IsNamedPipe; }
187-
188-
void closeFile() const {
189-
File.reset(); // rely on destructor to close File
190-
}
191-
192-
// Only for use in tests to see if deferred opens are happening, rather than
193-
// relying on RealPathName being empty.
194-
bool isOpenForTests() const { return File != nullptr; }
195-
};
196-
197-
bool FileEntryRef::isValid() const { return getFileEntry().isValid(); }
198-
199-
off_t FileEntryRef::getSize() const { return getFileEntry().getSize(); }
200-
201-
unsigned FileEntryRef::getUID() const { return getFileEntry().getUID(); }
202-
203-
const llvm::sys::fs::UniqueID &FileEntryRef::getUniqueID() const {
204-
return getFileEntry().getUniqueID();
205-
}
206-
207-
time_t FileEntryRef::getModificationTime() const {
208-
return getFileEntry().getModificationTime();
209-
}
210-
21175
/// Implements support for file system lookup, file system caching,
21276
/// and directory search management.
21377
///

clang/lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_clang_library(clangBasic
4646
DiagnosticIDs.cpp
4747
DiagnosticOptions.cpp
4848
ExpressionTraits.cpp
49+
FileEntry.cpp
4950
FileManager.cpp
5051
FileSystemStatCache.cpp
5152
IdentifierTable.cpp

clang/lib/Basic/FileEntry.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===- FileEntry.cpp - File references --------------------------*- C++ -*-===//
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+
/// Defines implementation for clang::FileEntry and clang::FileEntryRef.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "clang/Basic/FileEntry.h"
15+
#include "llvm/Support/VirtualFileSystem.h"
16+
17+
using namespace clang;
18+
19+
FileEntry::FileEntry() : UniqueID(0, 0), IsNamedPipe(false), IsValid(false) {}
20+
21+
FileEntry::~FileEntry() = default;
22+
23+
void FileEntry::closeFile() const { File.reset(); }

llvm/include/llvm/Support/FileSystem.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "llvm/Support/Error.h"
3535
#include "llvm/Support/ErrorHandling.h"
3636
#include "llvm/Support/ErrorOr.h"
37+
#include "llvm/Support/FileSystem/UniqueID.h"
3738
#include "llvm/Support/MD5.h"
3839
#include <cassert>
3940
#include <cstdint>
@@ -131,26 +132,6 @@ inline perms operator~(perms x) {
131132
static_cast<unsigned short>(~static_cast<unsigned short>(x)));
132133
}
133134

134-
class UniqueID {
135-
uint64_t Device;
136-
uint64_t File;
137-
138-
public:
139-
UniqueID() = default;
140-
UniqueID(uint64_t Device, uint64_t File) : Device(Device), File(File) {}
141-
142-
bool operator==(const UniqueID &Other) const {
143-
return Device == Other.Device && File == Other.File;
144-
}
145-
bool operator!=(const UniqueID &Other) const { return !(*this == Other); }
146-
bool operator<(const UniqueID &Other) const {
147-
return std::tie(Device, File) < std::tie(Other.Device, Other.File);
148-
}
149-
150-
uint64_t getDevice() const { return Device; }
151-
uint64_t getFile() const { return File; }
152-
};
153-
154135
/// Represents the result of a call to directory_iterator::status(). This is a
155136
/// subset of the information returned by a regular sys::fs::status() call, and
156137
/// represents the information provided by Windows FileFirstFile/FindNextFile.

0 commit comments

Comments
 (0)