Skip to content

Commit

Permalink
[clang] NFCI: Use FileEntryRef in FileRemapper (1/2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jansvoboda11 committed Sep 10, 2023
1 parent 91a848b commit 3817c8a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
12 changes: 6 additions & 6 deletions clang/include/clang/ARCMigrate/FileRemapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,7 +24,6 @@ namespace llvm {

namespace clang {
class FileManager;
class FileEntry;
class DiagnosticsEngine;
class PreprocessorOptions;

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
62 changes: 31 additions & 31 deletions clang/lib/ARCMigrate/FileRemapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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());
}
}
Expand All @@ -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);
}
}

Expand All @@ -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) {
Expand Down

0 comments on commit 3817c8a

Please sign in to comment.