Skip to content

Commit 3817c8a

Browse files
committed
[clang] NFCI: Use FileEntryRef in FileRemapper (1/2)
1 parent 91a848b commit 3817c8a

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

clang/include/clang/ARCMigrate/FileRemapper.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
1010
#define LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
1111

12+
#include "clang/Basic/FileEntry.h"
1213
#include "clang/Basic/LLVM.h"
1314
#include "llvm/ADT/DenseMap.h"
1415
#include "llvm/ADT/PointerUnion.h"
@@ -23,7 +24,6 @@ namespace llvm {
2324

2425
namespace clang {
2526
class FileManager;
26-
class FileEntry;
2727
class DiagnosticsEngine;
2828
class PreprocessorOptions;
2929

@@ -34,10 +34,10 @@ class FileRemapper {
3434
std::unique_ptr<FileManager> FileMgr;
3535

3636
typedef llvm::PointerUnion<const FileEntry *, llvm::MemoryBuffer *> Target;
37-
typedef llvm::DenseMap<const FileEntry *, Target> MappingsTy;
37+
using MappingsTy = llvm::DenseMap<FileEntryRef, Target>;
3838
MappingsTy FromToMappings;
3939

40-
llvm::DenseMap<const FileEntry *, const FileEntry *> ToFromMappings;
40+
llvm::DenseMap<const FileEntry *, FileEntryRef> ToFromMappings;
4141

4242
public:
4343
FileRemapper();
@@ -66,10 +66,10 @@ class FileRemapper {
6666
void clear(StringRef outputDir = StringRef());
6767

6868
private:
69-
void remap(const FileEntry *file, std::unique_ptr<llvm::MemoryBuffer> memBuf);
70-
void remap(const FileEntry *file, const FileEntry *newfile);
69+
void remap(FileEntryRef file, std::unique_ptr<llvm::MemoryBuffer> memBuf);
70+
void remap(FileEntryRef file, const FileEntry *newfile);
7171

72-
const FileEntry *getOriginalFile(StringRef filePath);
72+
OptionalFileEntryRef getOriginalFile(StringRef filePath);
7373
void resetTarget(Target &targ);
7474

7575
bool report(const Twine &err, DiagnosticsEngine &Diag);

clang/lib/ARCMigrate/FileRemapper.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
6060
if (!llvm::sys::fs::exists(infoFile))
6161
return false;
6262

63-
std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
63+
std::vector<std::pair<FileEntryRef, const FileEntry *> > pairs;
6464

6565
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBuf =
6666
llvm::MemoryBuffer::getFile(infoFile, /*IsText=*/true);
@@ -78,7 +78,7 @@ bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
7878
Diag);
7979
StringRef toFilename = lines[idx+2];
8080

81-
llvm::ErrorOr<const FileEntry *> origFE = FileMgr->getFile(fromFilename);
81+
auto origFE = FileMgr->getOptionalFileRef(fromFilename);
8282
if (!origFE) {
8383
if (ignoreIfFilesChanged)
8484
continue;
@@ -91,7 +91,7 @@ bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
9191
return report("File does not exist: " + toFilename, Diag);
9292
}
9393

94-
if ((uint64_t)(*origFE)->getModificationTime() != timeModified) {
94+
if ((uint64_t)origFE->getModificationTime() != timeModified) {
9595
if (ignoreIfFilesChanged)
9696
continue;
9797
return report("File was modified: " + fromFilename, Diag);
@@ -128,11 +128,11 @@ bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
128128
for (MappingsTy::iterator
129129
I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
130130

131-
const FileEntry *origFE = I->first;
132-
SmallString<200> origPath = StringRef(origFE->getName());
131+
FileEntryRef origFE = I->first;
132+
SmallString<200> origPath = StringRef(origFE.getName());
133133
fs::make_absolute(origPath);
134134
infoOut << origPath << '\n';
135-
infoOut << (uint64_t)origFE->getModificationTime() << '\n';
135+
infoOut << (uint64_t)origFE.getModificationTime() << '\n';
136136

137137
if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
138138
SmallString<200> newPath = StringRef(FE->getName());
@@ -143,8 +143,8 @@ bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
143143
SmallString<64> tempPath;
144144
int fd;
145145
if (fs::createTemporaryFile(
146-
path::filename(origFE->getName()),
147-
path::extension(origFE->getName()).drop_front(), fd, tempPath,
146+
path::filename(origFE.getName()),
147+
path::extension(origFE.getName()).drop_front(), fd, tempPath,
148148
llvm::sys::fs::OF_Text))
149149
return report("Could not create file: " + tempPath.str(), Diag);
150150

@@ -171,14 +171,14 @@ bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
171171

172172
for (MappingsTy::iterator
173173
I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
174-
const FileEntry *origFE = I->first;
174+
FileEntryRef origFE = I->first;
175175
assert(I->second.is<llvm::MemoryBuffer *>());
176-
if (!fs::exists(origFE->getName()))
177-
return report(StringRef("File does not exist: ") + origFE->getName(),
176+
if (!fs::exists(origFE.getName()))
177+
return report(StringRef("File does not exist: ") + origFE.getName(),
178178
Diag);
179179

180180
std::error_code EC;
181-
llvm::raw_fd_ostream Out(origFE->getName(), EC, llvm::sys::fs::OF_None);
181+
llvm::raw_fd_ostream Out(origFE.getName(), EC, llvm::sys::fs::OF_None);
182182
if (EC)
183183
return report(EC.message(), Diag);
184184

@@ -197,11 +197,11 @@ void FileRemapper::forEachMapping(
197197
CaptureBuffer) const {
198198
for (auto &Mapping : FromToMappings) {
199199
if (const FileEntry *FE = Mapping.second.dyn_cast<const FileEntry *>()) {
200-
CaptureFile(Mapping.first->getName(), FE->getName());
200+
CaptureFile(Mapping.first.getName(), FE->getName());
201201
continue;
202202
}
203203
CaptureBuffer(
204-
Mapping.first->getName(),
204+
Mapping.first.getName(),
205205
Mapping.second.get<llvm::MemoryBuffer *>()->getMemBufferRef());
206206
}
207207
}
@@ -210,10 +210,10 @@ void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
210210
for (MappingsTy::const_iterator
211211
I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
212212
if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
213-
PPOpts.addRemappedFile(I->first->getName(), FE->getName());
213+
PPOpts.addRemappedFile(I->first.getName(), FE->getName());
214214
} else {
215215
llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
216-
PPOpts.addRemappedFile(I->first->getName(), mem);
216+
PPOpts.addRemappedFile(I->first.getName(), mem);
217217
}
218218
}
219219

@@ -222,38 +222,38 @@ void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
222222

223223
void FileRemapper::remap(StringRef filePath,
224224
std::unique_ptr<llvm::MemoryBuffer> memBuf) {
225-
remap(getOriginalFile(filePath), std::move(memBuf));
225+
OptionalFileEntryRef File = getOriginalFile(filePath);
226+
assert(File);
227+
remap(*File, std::move(memBuf));
226228
}
227229

228-
void FileRemapper::remap(const FileEntry *file,
230+
void FileRemapper::remap(FileEntryRef file,
229231
std::unique_ptr<llvm::MemoryBuffer> memBuf) {
230-
assert(file);
231232
Target &targ = FromToMappings[file];
232233
resetTarget(targ);
233234
targ = memBuf.release();
234235
}
235236

236-
void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
237-
assert(file && newfile);
237+
void FileRemapper::remap(FileEntryRef file, const FileEntry *newfile) {
238+
assert(newfile);
238239
Target &targ = FromToMappings[file];
239240
resetTarget(targ);
240241
targ = newfile;
241-
ToFromMappings[newfile] = file;
242+
ToFromMappings.insert({newfile, file});
242243
}
243244

244-
const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
245-
const FileEntry *file = nullptr;
246-
if (auto fileOrErr = FileMgr->getFile(filePath))
247-
file = *fileOrErr;
245+
OptionalFileEntryRef FileRemapper::getOriginalFile(StringRef filePath) {
246+
OptionalFileEntryRef File = FileMgr->getOptionalFileRef(filePath);
247+
if (!File)
248+
return std::nullopt;
248249
// If we are updating a file that overridden an original file,
249250
// actually update the original file.
250-
llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
251-
I = ToFromMappings.find(file);
251+
auto I = ToFromMappings.find(*File);
252252
if (I != ToFromMappings.end()) {
253-
file = I->second;
254-
assert(FromToMappings.contains(file) && "Original file not in mappings!");
253+
*File = I->second;
254+
assert(FromToMappings.contains(*File) && "Original file not in mappings!");
255255
}
256-
return file;
256+
return File;
257257
}
258258

259259
void FileRemapper::resetTarget(Target &targ) {

0 commit comments

Comments
 (0)