[clang] 3817c8a - [clang] NFCI: Use `FileEntryRef` in `FileRemapper` (1/2)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 10 08:19:31 PDT 2023
Author: Jan Svoboda
Date: 2023-09-10T08:19:26-07:00
New Revision: 3817c8a4e628344bc237434fcf9838da1857f69d
URL: https://github.com/llvm/llvm-project/commit/3817c8a4e628344bc237434fcf9838da1857f69d
DIFF: https://github.com/llvm/llvm-project/commit/3817c8a4e628344bc237434fcf9838da1857f69d.diff
LOG: [clang] NFCI: Use `FileEntryRef` in `FileRemapper` (1/2)
Added:
Modified:
clang/include/clang/ARCMigrate/FileRemapper.h
clang/lib/ARCMigrate/FileRemapper.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/ARCMigrate/FileRemapper.h b/clang/include/clang/ARCMigrate/FileRemapper.h
index 4da68a678be2027..776ac4700b73bac 100644
--- a/clang/include/clang/ARCMigrate/FileRemapper.h
+++ b/clang/include/clang/ARCMigrate/FileRemapper.h
@@ -9,6 +9,7 @@
#ifndef LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
#define LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
+#include "clang/Basic/FileEntry.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerUnion.h"
@@ -23,7 +24,6 @@ namespace llvm {
namespace clang {
class FileManager;
- class FileEntry;
class DiagnosticsEngine;
class PreprocessorOptions;
@@ -34,10 +34,10 @@ class FileRemapper {
std::unique_ptr<FileManager> FileMgr;
typedef llvm::PointerUnion<const FileEntry *, llvm::MemoryBuffer *> Target;
- typedef llvm::DenseMap<const FileEntry *, Target> MappingsTy;
+ using MappingsTy = llvm::DenseMap<FileEntryRef, Target>;
MappingsTy FromToMappings;
- llvm::DenseMap<const FileEntry *, const FileEntry *> ToFromMappings;
+ llvm::DenseMap<const FileEntry *, FileEntryRef> ToFromMappings;
public:
FileRemapper();
@@ -66,10 +66,10 @@ class FileRemapper {
void clear(StringRef outputDir = StringRef());
private:
- void remap(const FileEntry *file, std::unique_ptr<llvm::MemoryBuffer> memBuf);
- void remap(const FileEntry *file, const FileEntry *newfile);
+ void remap(FileEntryRef file, std::unique_ptr<llvm::MemoryBuffer> memBuf);
+ void remap(FileEntryRef file, const FileEntry *newfile);
- const FileEntry *getOriginalFile(StringRef filePath);
+ OptionalFileEntryRef getOriginalFile(StringRef filePath);
void resetTarget(Target &targ);
bool report(const Twine &err, DiagnosticsEngine &Diag);
diff --git a/clang/lib/ARCMigrate/FileRemapper.cpp b/clang/lib/ARCMigrate/FileRemapper.cpp
index 7e56e0683c12931..413caf1e9653d4c 100644
--- a/clang/lib/ARCMigrate/FileRemapper.cpp
+++ b/clang/lib/ARCMigrate/FileRemapper.cpp
@@ -60,7 +60,7 @@ bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
if (!llvm::sys::fs::exists(infoFile))
return false;
- std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
+ std::vector<std::pair<FileEntryRef, const FileEntry *> > pairs;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBuf =
llvm::MemoryBuffer::getFile(infoFile, /*IsText=*/true);
@@ -78,7 +78,7 @@ bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
Diag);
StringRef toFilename = lines[idx+2];
- llvm::ErrorOr<const FileEntry *> origFE = FileMgr->getFile(fromFilename);
+ auto origFE = FileMgr->getOptionalFileRef(fromFilename);
if (!origFE) {
if (ignoreIfFilesChanged)
continue;
@@ -91,7 +91,7 @@ bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
return report("File does not exist: " + toFilename, Diag);
}
- if ((uint64_t)(*origFE)->getModificationTime() != timeModified) {
+ if ((uint64_t)origFE->getModificationTime() != timeModified) {
if (ignoreIfFilesChanged)
continue;
return report("File was modified: " + fromFilename, Diag);
@@ -128,11 +128,11 @@ bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
for (MappingsTy::iterator
I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
- const FileEntry *origFE = I->first;
- SmallString<200> origPath = StringRef(origFE->getName());
+ FileEntryRef origFE = I->first;
+ SmallString<200> origPath = StringRef(origFE.getName());
fs::make_absolute(origPath);
infoOut << origPath << '\n';
- infoOut << (uint64_t)origFE->getModificationTime() << '\n';
+ infoOut << (uint64_t)origFE.getModificationTime() << '\n';
if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
SmallString<200> newPath = StringRef(FE->getName());
@@ -143,8 +143,8 @@ bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
SmallString<64> tempPath;
int fd;
if (fs::createTemporaryFile(
- path::filename(origFE->getName()),
- path::extension(origFE->getName()).drop_front(), fd, tempPath,
+ path::filename(origFE.getName()),
+ path::extension(origFE.getName()).drop_front(), fd, tempPath,
llvm::sys::fs::OF_Text))
return report("Could not create file: " + tempPath.str(), Diag);
@@ -171,14 +171,14 @@ bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
for (MappingsTy::iterator
I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
- const FileEntry *origFE = I->first;
+ FileEntryRef origFE = I->first;
assert(I->second.is<llvm::MemoryBuffer *>());
- if (!fs::exists(origFE->getName()))
- return report(StringRef("File does not exist: ") + origFE->getName(),
+ if (!fs::exists(origFE.getName()))
+ return report(StringRef("File does not exist: ") + origFE.getName(),
Diag);
std::error_code EC;
- llvm::raw_fd_ostream Out(origFE->getName(), EC, llvm::sys::fs::OF_None);
+ llvm::raw_fd_ostream Out(origFE.getName(), EC, llvm::sys::fs::OF_None);
if (EC)
return report(EC.message(), Diag);
@@ -197,11 +197,11 @@ void FileRemapper::forEachMapping(
CaptureBuffer) const {
for (auto &Mapping : FromToMappings) {
if (const FileEntry *FE = Mapping.second.dyn_cast<const FileEntry *>()) {
- CaptureFile(Mapping.first->getName(), FE->getName());
+ CaptureFile(Mapping.first.getName(), FE->getName());
continue;
}
CaptureBuffer(
- Mapping.first->getName(),
+ Mapping.first.getName(),
Mapping.second.get<llvm::MemoryBuffer *>()->getMemBufferRef());
}
}
@@ -210,10 +210,10 @@ void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
for (MappingsTy::const_iterator
I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
- PPOpts.addRemappedFile(I->first->getName(), FE->getName());
+ PPOpts.addRemappedFile(I->first.getName(), FE->getName());
} else {
llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
- PPOpts.addRemappedFile(I->first->getName(), mem);
+ PPOpts.addRemappedFile(I->first.getName(), mem);
}
}
@@ -222,38 +222,38 @@ void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
void FileRemapper::remap(StringRef filePath,
std::unique_ptr<llvm::MemoryBuffer> memBuf) {
- remap(getOriginalFile(filePath), std::move(memBuf));
+ OptionalFileEntryRef File = getOriginalFile(filePath);
+ assert(File);
+ remap(*File, std::move(memBuf));
}
-void FileRemapper::remap(const FileEntry *file,
+void FileRemapper::remap(FileEntryRef file,
std::unique_ptr<llvm::MemoryBuffer> memBuf) {
- assert(file);
Target &targ = FromToMappings[file];
resetTarget(targ);
targ = memBuf.release();
}
-void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
- assert(file && newfile);
+void FileRemapper::remap(FileEntryRef file, const FileEntry *newfile) {
+ assert(newfile);
Target &targ = FromToMappings[file];
resetTarget(targ);
targ = newfile;
- ToFromMappings[newfile] = file;
+ ToFromMappings.insert({newfile, file});
}
-const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
- const FileEntry *file = nullptr;
- if (auto fileOrErr = FileMgr->getFile(filePath))
- file = *fileOrErr;
+OptionalFileEntryRef FileRemapper::getOriginalFile(StringRef filePath) {
+ OptionalFileEntryRef File = FileMgr->getOptionalFileRef(filePath);
+ if (!File)
+ return std::nullopt;
// If we are updating a file that overridden an original file,
// actually update the original file.
- llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
- I = ToFromMappings.find(file);
+ auto I = ToFromMappings.find(*File);
if (I != ToFromMappings.end()) {
- file = I->second;
- assert(FromToMappings.contains(file) && "Original file not in mappings!");
+ *File = I->second;
+ assert(FromToMappings.contains(*File) && "Original file not in mappings!");
}
- return file;
+ return File;
}
void FileRemapper::resetTarget(Target &targ) {
More information about the cfe-commits
mailing list