Skip to content

Commit

Permalink
ARCMigrate: Migrate ObjCMT.cpp over to FileEntryRef
Browse files Browse the repository at this point in the history
Migrate ObjCMT.cpp from using `const FileEntry*` to `FileEntryRef`. This
is one of the blockers for changing `SourceManager` to use
`FileEntryRef`.

This adds an initial version of `SourceManager::getFileEntryRefForID`,
which uses to `FileEntry::getLastRef`; after `SourceManager` switches,
`SourceManager::getFileEntryForID` will need to call this function.

This also adds uses of `FileEntryRef` as a key in a `DenseMap`, and a
call to `hash_value(Optional)` in `DenseMapInfo<EditEntry>`; support for
these were added in prep commits.

Differential Revision: https://reviews.llvm.org/D92678
  • Loading branch information
dexonsmith committed Dec 9, 2020
1 parent a779050 commit 898d61b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/SourceManager.h
Expand Up @@ -1033,6 +1033,13 @@ class SourceManager : public RefCountedBase<SourceManager> {
return nullptr;
}

/// Returns the FileEntryRef for the provided FileID.
Optional<FileEntryRef> getFileEntryRefForID(FileID FID) const {
if (auto *Entry = getFileEntryForID(FID))
return Entry->getLastRef();
return None;
}

/// Returns the filename for the provided FileID, unless it's a built-in
/// buffer that's not represented by a filename.
///
Expand Down
25 changes: 12 additions & 13 deletions clang/lib/ARCMigrate/ObjCMT.cpp
Expand Up @@ -156,15 +156,15 @@ class ObjCMigrateASTConsumer : public ASTConsumer {
return WhiteListFilenames.find(llvm::sys::path::filename(Path))
!= WhiteListFilenames.end();
}
bool canModifyFile(const FileEntry *FE) {
bool canModifyFile(Optional<FileEntryRef> FE) {
if (!FE)
return false;
return canModifyFile(FE->getName());
}
bool canModifyFile(FileID FID) {
if (FID.isInvalid())
return false;
return canModifyFile(PP.getSourceManager().getFileEntryForID(FID));
return canModifyFile(PP.getSourceManager().getFileEntryRefForID(FID));
}

bool canModify(const Decl *D) {
Expand Down Expand Up @@ -1964,7 +1964,7 @@ void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
FileID FID = I->first;
RewriteBuffer &buf = I->second;
const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
Optional<FileEntryRef> file = Ctx.getSourceManager().getFileEntryRefForID(FID);
assert(file);
SmallString<512> newText;
llvm::raw_svector_ostream vecOS(newText);
Expand Down Expand Up @@ -2034,7 +2034,7 @@ MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {

namespace {
struct EditEntry {
const FileEntry *File = nullptr;
Optional<FileEntryRef> File;
unsigned Offset = 0;
unsigned RemoveLen = 0;
std::string Text;
Expand Down Expand Up @@ -2127,9 +2127,8 @@ class RemapFileParser {
StringRef Val = ValueString->getValue(ValueStorage);

if (Key == "file") {
auto FE = FileMgr.getFile(Val);
if (FE)
Entry.File = *FE;
if (auto File = FileMgr.getOptionalFileRef(Val))
Entry.File = File;
else
Ignore = true;
} else if (Key == "offset") {
Expand All @@ -2155,7 +2154,7 @@ static bool reportDiag(const Twine &Err, DiagnosticsEngine &Diag) {
return true;
}

static std::string applyEditsToTemp(const FileEntry *FE,
static std::string applyEditsToTemp(FileEntryRef FE,
ArrayRef<EditEntry> Edits,
FileManager &FileMgr,
DiagnosticsEngine &Diag) {
Expand Down Expand Up @@ -2199,8 +2198,8 @@ static std::string applyEditsToTemp(const FileEntry *FE,

SmallString<64> TempPath;
int FD;
if (fs::createTemporaryFile(path::filename(FE->getName()),
path::extension(FE->getName()).drop_front(), FD,
if (fs::createTemporaryFile(path::filename(FE.getName()),
path::extension(FE.getName()).drop_front(), FD,
TempPath)) {
reportDiag("Could not create file: " + TempPath.str(), Diag);
return std::string();
Expand Down Expand Up @@ -2228,7 +2227,7 @@ bool arcmt::getFileRemappingsFromFileList(
new DiagnosticsEngine(DiagID, new DiagnosticOptions,
DiagClient, /*ShouldOwnClient=*/false));

typedef llvm::DenseMap<const FileEntry *, std::vector<EditEntry> >
typedef llvm::DenseMap<FileEntryRef, std::vector<EditEntry> >
FileEditEntriesTy;
FileEditEntriesTy FileEditEntries;

Expand All @@ -2250,7 +2249,7 @@ bool arcmt::getFileRemappingsFromFileList(
if (!Insert.second)
continue;

FileEditEntries[Entry.File].push_back(Entry);
FileEditEntries[*Entry.File].push_back(Entry);
}
}

Expand All @@ -2263,7 +2262,7 @@ bool arcmt::getFileRemappingsFromFileList(
continue;
}

remap.emplace_back(std::string(I->first->getName()), TempFile);
remap.emplace_back(std::string(I->first.getName()), TempFile);
}

return hasErrorOccurred;
Expand Down

0 comments on commit 898d61b

Please sign in to comment.