Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[clang] NFCI: Use FileEntryRef in SourceManager::FileInfos (#67742)
Browse files Browse the repository at this point in the history
  • Loading branch information
jansvoboda11 authored Sep 29, 2023
1 parent cd03d97 commit 8a2fb13
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
20 changes: 10 additions & 10 deletions clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ namespace clang::tooling {
class ExpandModularHeadersPPCallbacks::FileRecorder {
public:
/// Records that a given file entry is needed for replaying callbacks.
void addNecessaryFile(const FileEntry *File) {
void addNecessaryFile(FileEntryRef File) {
// Don't record modulemap files because it breaks same file detection.
if (!(File->getName().endswith("module.modulemap") ||
File->getName().endswith("module.private.modulemap") ||
File->getName().endswith("module.map") ||
File->getName().endswith("module_private.map")))
if (!(File.getName().endswith("module.modulemap") ||
File.getName().endswith("module.private.modulemap") ||
File.getName().endswith("module.map") ||
File.getName().endswith("module_private.map")))
FilesToRecord.insert(File);
}

/// Records content for a file and adds it to the FileSystem.
void recordFileContent(const FileEntry *File,
void recordFileContent(FileEntryRef File,
const SrcMgr::ContentCache &ContentCache,
llvm::vfs::InMemoryFileSystem &InMemoryFs) {
// Return if we are not interested in the contents of this file.
Expand All @@ -43,7 +43,7 @@ class ExpandModularHeadersPPCallbacks::FileRecorder {
if (!Data)
return;

InMemoryFs.addFile(File->getName(), /*ModificationTime=*/0,
InMemoryFs.addFile(File.getName(), /*ModificationTime=*/0,
llvm::MemoryBuffer::getMemBufferCopy(*Data));
// Remove the file from the set of necessary files.
FilesToRecord.erase(File);
Expand All @@ -55,13 +55,13 @@ class ExpandModularHeadersPPCallbacks::FileRecorder {
LLVM_DEBUG({
for (auto FileEntry : FilesToRecord)
llvm::dbgs() << "Did not record contents for input file: "
<< FileEntry->getName() << "\n";
<< FileEntry.getName() << "\n";
});
}

private:
/// A set of files whose contents are to be recorded.
llvm::DenseSet<const FileEntry *> FilesToRecord;
llvm::DenseSet<FileEntryRef> FilesToRecord;
};

ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks(
Expand Down Expand Up @@ -125,7 +125,7 @@ void ExpandModularHeadersPPCallbacks::handleModuleFile(
Compiler.getASTReader()->visitInputFiles(
*MF, true, false,
[this](const serialization::InputFile &IF, bool /*IsSystem*/) {
Recorder->addNecessaryFile(IF.getFile());
Recorder->addNecessaryFile(*IF.getFile());
});
// Recursively handle all transitively imported modules.
for (auto *Import : MF->Imports)
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/Basic/FileEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ template <> struct DenseMapInfo<clang::FileEntryRef> {
// It's safe to use operator==.
return LHS == RHS;
}

/// Support for finding `const FileEntry *` in a `DenseMap<FileEntryRef, T>`.
/// @{
static unsigned getHashValue(const clang::FileEntry *Val) {
return llvm::hash_value(Val);
}
static bool isEqual(const clang::FileEntry *LHS, clang::FileEntryRef RHS) {
if (RHS.isSpecialDenseMapKey())
return false;
return LHS == RHS;
}
/// @}
};

} // end namespace llvm
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// This map allows us to merge ContentCache entries based
/// on their FileEntry*. All ContentCache objects will thus have unique,
/// non-null, FileEntry pointers.
llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
llvm::DenseMap<FileEntryRef, SrcMgr::ContentCache*> FileInfos;

/// True if the ContentCache for files that are overridden by other
/// files, should report the original file name. Defaults to true.
Expand Down Expand Up @@ -1680,12 +1680,12 @@ class SourceManager : public RefCountedBase<SourceManager> {

// Iterators over FileInfos.
using fileinfo_iterator =
llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::const_iterator;
llvm::DenseMap<FileEntryRef, SrcMgr::ContentCache *>::const_iterator;

fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
bool hasFileInfo(const FileEntry *File) const {
return FileInfos.contains(File);
return FileInfos.find_as(File) != FileInfos.end();
}

/// Print statistics to stderr.
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Basic/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ SourceManager::~SourceManager() {
ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
}
}
for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
for (auto I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
if (I->second) {
I->second->~ContentCache();
ContentCacheAlloc.Deallocate(I->second);
Expand Down Expand Up @@ -702,7 +701,7 @@ void SourceManager::overrideFileContents(const FileEntry *SourceFile,
assert(SourceFile->getSize() == NewFile.getSize() &&
"Different sizes, use the FileManager to create a virtual file with "
"the correct size");
assert(FileInfos.count(SourceFile) == 0 &&
assert(FileInfos.find_as(SourceFile) == FileInfos.end() &&
"This function should be called at the initialization stage, before "
"any parsing occurs.");
// FileEntryRef is not default-constructible.
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2913,8 +2913,8 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
for (auto I = CGM.getContext().getSourceManager().fileinfo_begin(),
E = CGM.getContext().getSourceManager().fileinfo_end();
I != E; ++I) {
if (I->getFirst()->getUniqueID().getDevice() == EntryInfo.DeviceID &&
I->getFirst()->getUniqueID().getFile() == EntryInfo.FileID) {
if (I->getFirst().getUniqueID().getDevice() == EntryInfo.DeviceID &&
I->getFirst().getUniqueID().getFile() == EntryInfo.FileID) {
Loc = CGM.getContext().getSourceManager().translateFileLineCol(
I->getFirst(), EntryInfo.Line, 1);
break;
Expand Down
40 changes: 40 additions & 0 deletions clang/unittests/Basic/FileEntryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,46 @@ TEST(FileEntryTest, DenseMapInfo) {
EXPECT_TRUE(Set.find(R1)->isSameRef(R1));
EXPECT_TRUE(Set.find(R2)->isSameRef(R2));
}

// Insert R1Also first and confirm it "wins" when looked up as FileEntry.
{
SmallDenseSet<FileEntryRef, 8> Set;
Set.insert(R1Also);
Set.insert(R1);
Set.insert(R2);

auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
ASSERT_TRUE(R1AlsoIt != Set.end());
EXPECT_TRUE(R1AlsoIt->isSameRef(R1Also));

auto R1It = Set.find_as(&R1.getFileEntry());
ASSERT_TRUE(R1It != Set.end());
EXPECT_TRUE(R1It->isSameRef(R1Also));

auto R2It = Set.find_as(&R2.getFileEntry());
ASSERT_TRUE(R2It != Set.end());
EXPECT_TRUE(R2It->isSameRef(R2));
}

// Insert R1Also second and confirm R1 "wins" when looked up as FileEntry.
{
SmallDenseSet<FileEntryRef, 8> Set;
Set.insert(R1);
Set.insert(R1Also);
Set.insert(R2);

auto R1AlsoIt = Set.find_as(&R1Also.getFileEntry());
ASSERT_TRUE(R1AlsoIt != Set.end());
EXPECT_TRUE(R1AlsoIt->isSameRef(R1));

auto R1It = Set.find_as(&R1.getFileEntry());
ASSERT_TRUE(R1It != Set.end());
EXPECT_TRUE(R1It->isSameRef(R1));

auto R2It = Set.find_as(&R2.getFileEntry());
ASSERT_TRUE(R2It != Set.end());
EXPECT_TRUE(R2It->isSameRef(R2));
}
}

TEST(DirectoryEntryTest, isSameRef) {
Expand Down

0 comments on commit 8a2fb13

Please sign in to comment.