Skip to content

Commit

Permalink
[clang][index] NFCI: Make CXFile a FileEntryRef
Browse files Browse the repository at this point in the history
This patch swaps out the `void *` behind `CXFile` from `FileEntry *` to `FileEntryRef::MapEntry *`. This allows us to remove some deprecated uses of `FileEntry::getName()`.

Depends on D151854.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D151938
  • Loading branch information
jansvoboda11 committed Jun 15, 2023
1 parent ed27d28 commit fa5788f
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 100 deletions.
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/Module.h
Expand Up @@ -217,7 +217,7 @@ class alignas(8) Module {
OptionalFileEntryRef ASTFile;

/// The top-level headers associated with this module.
llvm::SmallSetVector<const FileEntry *, 2> TopHeaders;
llvm::SmallSetVector<FileEntryRef, 2> TopHeaders;

/// top-level header filenames that aren't resolved to FileEntries yet.
std::vector<std::string> TopHeaderNames;
Expand Down Expand Up @@ -672,15 +672,15 @@ class alignas(8) Module {
OptionalDirectoryEntryRef getEffectiveUmbrellaDir() const;

/// Add a top-level header associated with this module.
void addTopHeader(const FileEntry *File);
void addTopHeader(FileEntryRef File);

/// Add a top-level header filename associated with this module.
void addTopHeaderFilename(StringRef Filename) {
TopHeaderNames.push_back(std::string(Filename));
}

/// The top-level headers associated with this module.
ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
ArrayRef<FileEntryRef> getTopHeaders(FileManager &FileMgr);

/// Determine whether this module has declared its intention to
/// directly use another module.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Frontend/ASTUnit.h
Expand Up @@ -643,7 +643,7 @@ class ASTUnit {
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);

/// Get the PCH file if one was included.
const FileEntry *getPCHFile();
OptionalFileEntryRef getPCHFile();

/// Returns true if the ASTUnit was constructed from a serialized
/// module file.
Expand Down
10 changes: 4 additions & 6 deletions clang/lib/Basic/Module.cpp
Expand Up @@ -271,18 +271,16 @@ OptionalDirectoryEntryRef Module::getEffectiveUmbrellaDir() const {
return std::nullopt;
}

void Module::addTopHeader(const FileEntry *File) {
void Module::addTopHeader(FileEntryRef File) {
assert(File);
TopHeaders.insert(File);
}

ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
ArrayRef<FileEntryRef> Module::getTopHeaders(FileManager &FileMgr) {
if (!TopHeaderNames.empty()) {
for (std::vector<std::string>::iterator
I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
if (auto FE = FileMgr.getFile(*I))
for (StringRef TopHeaderName : TopHeaderNames)
if (auto FE = FileMgr.getOptionalFileRef(TopHeaderName))
TopHeaders.insert(*FE);
}
TopHeaderNames.clear();
}

Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Frontend/ASTUnit.cpp
Expand Up @@ -2645,9 +2645,9 @@ bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) {
return true;
}

const FileEntry *ASTUnit::getPCHFile() {
OptionalFileEntryRef ASTUnit::getPCHFile() {
if (!Reader)
return nullptr;
return std::nullopt;

serialization::ModuleFile *Mod = nullptr;
Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) {
Expand All @@ -2670,7 +2670,7 @@ const FileEntry *ASTUnit::getPCHFile() {
if (Mod)
return Mod->File;

return nullptr;
return std::nullopt;
}

bool ASTUnit::isModuleFile() const {
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Frontend/FrontendAction.cpp
Expand Up @@ -379,9 +379,7 @@ static std::error_code collectModuleHeaderIncludes(
llvm::sys::path::native(UmbrellaDir->Entry.getName(), DirNative);

llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
SmallVector<
std::pair<std::string, OptionalFileEntryRefDegradesToFileEntryPtr>, 8>
Headers;
SmallVector<std::pair<std::string, FileEntryRef>, 8> Headers;
for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
Dir != End && !EC; Dir.increment(EC)) {
// Check whether this entry has an extension typically associated with
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Serialization/ASTWriter.cpp
Expand Up @@ -2879,10 +2879,9 @@ void ASTWriter::WriteSubmodules(Module *WritingModule) {

// Emit the top headers.
{
auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
for (auto *H : TopHeaders) {
SmallString<128> HeaderName(H->getName());
for (FileEntryRef H : Mod->getTopHeaders(PP->getFileManager())) {
SmallString<128> HeaderName(H.getName());
PreparePathForOutput(HeaderName);
Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, HeaderName);
}
Expand Down
58 changes: 26 additions & 32 deletions clang/tools/libclang/CIndex.cpp
Expand Up @@ -15,6 +15,7 @@
#include "CIndexer.h"
#include "CLog.h"
#include "CXCursor.h"
#include "CXFile.h"
#include "CXSourceLocation.h"
#include "CXString.h"
#include "CXTranslationUnit.h"
Expand Down Expand Up @@ -4662,16 +4663,16 @@ CXString clang_getFileName(CXFile SFile) {
if (!SFile)
return cxstring::createNull();

FileEntry *FEnt = static_cast<FileEntry *>(SFile);
return cxstring::createRef(FEnt->getName());
FileEntryRef FEnt = *cxfile::getFileEntryRef(SFile);
return cxstring::createRef(FEnt.getName());
}

time_t clang_getFileTime(CXFile SFile) {
if (!SFile)
return 0;

FileEntry *FEnt = static_cast<FileEntry *>(SFile);
return FEnt->getModificationTime();
FileEntryRef FEnt = *cxfile::getFileEntryRef(SFile);
return FEnt.getModificationTime();
}

CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
Expand All @@ -4683,10 +4684,7 @@ CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);

FileManager &FMgr = CXXUnit->getFileManager();
auto File = FMgr.getFile(file_name);
if (!File)
return nullptr;
return const_cast<FileEntry *>(*File);
return cxfile::makeCXFile(FMgr.getOptionalFileRef(file_name));
}

const char *clang_getFileContents(CXTranslationUnit TU, CXFile file,
Expand All @@ -4697,7 +4695,7 @@ const char *clang_getFileContents(CXTranslationUnit TU, CXFile file,
}

const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
FileID fid = SM.translateFile(static_cast<FileEntry *>(file));
FileID fid = SM.translateFile(*cxfile::getFileEntryRef(file));
std::optional<llvm::MemoryBufferRef> buf = SM.getBufferOrNone(fid);
if (!buf) {
if (size)
Expand All @@ -4719,7 +4717,7 @@ unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) {
return 0;

ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
FileEntry *FEnt = static_cast<FileEntry *>(file);
FileEntryRef FEnt = *cxfile::getFileEntryRef(file);
return CXXUnit->getPreprocessor()
.getHeaderSearchInfo()
.isFileMultipleIncludeGuarded(FEnt);
Expand All @@ -4729,11 +4727,11 @@ int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) {
if (!file || !outID)
return 1;

FileEntry *FEnt = static_cast<FileEntry *>(file);
const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID();
FileEntryRef FEnt = *cxfile::getFileEntryRef(file);
const llvm::sys::fs::UniqueID &ID = FEnt.getUniqueID();
outID->data[0] = ID.getDevice();
outID->data[1] = ID.getFile();
outID->data[2] = FEnt->getModificationTime();
outID->data[2] = FEnt.getModificationTime();
return 0;
}

Expand All @@ -4744,17 +4742,17 @@ int clang_File_isEqual(CXFile file1, CXFile file2) {
if (!file1 || !file2)
return false;

FileEntry *FEnt1 = static_cast<FileEntry *>(file1);
FileEntry *FEnt2 = static_cast<FileEntry *>(file2);
return FEnt1->getUniqueID() == FEnt2->getUniqueID();
FileEntryRef FEnt1 = *cxfile::getFileEntryRef(file1);
FileEntryRef FEnt2 = *cxfile::getFileEntryRef(file2);
return FEnt1.getUniqueID() == FEnt2.getUniqueID();
}

CXString clang_File_tryGetRealPathName(CXFile SFile) {
if (!SFile)
return cxstring::createNull();

FileEntry *FEnt = static_cast<FileEntry *>(SFile);
return cxstring::createRef(FEnt->tryGetRealPathName());
FileEntryRef FEnt = *cxfile::getFileEntryRef(SFile);
return cxstring::createRef(FEnt.getFileEntry().tryGetRealPathName());
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -8601,8 +8599,7 @@ CXFile clang_getIncludedFile(CXCursor cursor) {
return nullptr;

const InclusionDirective *ID = getCursorInclusionDirective(cursor);
OptionalFileEntryRef File = ID->getFile();
return const_cast<FileEntry *>(File ? &File->getFileEntry() : nullptr);
return cxfile::makeCXFile(ID->getFile());
}

unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) {
Expand Down Expand Up @@ -8798,12 +8795,11 @@ CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) {
}
if (!File)
return nullptr;
FileEntry *FE = static_cast<FileEntry *>(File);
FileEntryRef FE = *cxfile::getFileEntryRef(File);

ASTUnit &Unit = *cxtu::getASTUnit(TU);
HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo();
// TODO: Make CXFile a FileEntryRef.
ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE->getLastRef());
ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE);

return Header.getModule();
}
Expand All @@ -8812,9 +8808,7 @@ CXFile clang_Module_getASTFile(CXModule CXMod) {
if (!CXMod)
return nullptr;
Module *Mod = static_cast<Module *>(CXMod);
if (auto File = Mod->getASTFile())
return const_cast<FileEntry *>(&File->getFileEntry());
return nullptr;
return cxfile::makeCXFile(Mod->getASTFile());
}

CXModule clang_Module_getParent(CXModule CXMod) {
Expand Down Expand Up @@ -8855,7 +8849,7 @@ unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU,
return 0;
Module *Mod = static_cast<Module *>(CXMod);
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
ArrayRef<FileEntryRef> TopHeaders = Mod->getTopHeaders(FileMgr);
return TopHeaders.size();
}

Expand All @@ -8870,9 +8864,9 @@ CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, CXModule CXMod,
Module *Mod = static_cast<Module *>(CXMod);
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();

ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr);
ArrayRef<FileEntryRef> TopHeaders = Mod->getTopHeaders(FileMgr);
if (Index < TopHeaders.size())
return const_cast<FileEntry *>(TopHeaders[Index]);
return cxfile::makeCXFile(TopHeaders[Index]);

return nullptr;
}
Expand Down Expand Up @@ -9248,7 +9242,7 @@ CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) {

ASTContext &Ctx = astUnit->getASTContext();
SourceManager &sm = Ctx.getSourceManager();
FileEntry *fileEntry = static_cast<FileEntry *>(file);
FileEntryRef fileEntry = *cxfile::getFileEntryRef(file);
FileID wantedFileID = sm.translateFile(fileEntry);
bool isMainFile = wantedFileID == sm.getMainFileID();

Expand Down Expand Up @@ -9522,8 +9516,8 @@ Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) {
return *this;
}

Logger &cxindex::Logger::operator<<(const FileEntry *FE) {
*this << FE->getName();
Logger &cxindex::Logger::operator<<(FileEntryRef FE) {
*this << FE.getName();
return *this;
}

Expand Down
11 changes: 6 additions & 5 deletions clang/tools/libclang/CIndexHigh.cpp
Expand Up @@ -9,6 +9,7 @@
#include "CursorVisitor.h"
#include "CLog.h"
#include "CXCursor.h"
#include "CXFile.h"
#include "CXSourceLocation.h"
#include "CXTranslationUnit.h"
#include "clang/AST/DeclObjC.h"
Expand Down Expand Up @@ -432,7 +433,7 @@ CXResult clang_findReferencesInFile(CXCursor cursor, CXFile file,
}

if (Log)
*Log << cursor << " @" << static_cast<const FileEntry *>(file);
*Log << cursor << " @" << *cxfile::getFileEntryRef(file);

ASTUnit *CXXUnit = cxcursor::getCursorASTUnit(cursor);
if (!CXXUnit)
Expand All @@ -444,7 +445,7 @@ CXResult clang_findReferencesInFile(CXCursor cursor, CXFile file,
cursor.kind == CXCursor_MacroExpansion) {
if (findMacroRefsInFile(cxcursor::getCursorTU(cursor),
cursor,
static_cast<const FileEntry *>(file),
*cxfile::getFileEntryRef(file),
visitor))
return CXResult_VisitBreak;
return CXResult_Success;
Expand All @@ -469,7 +470,7 @@ CXResult clang_findReferencesInFile(CXCursor cursor, CXFile file,

if (findIdRefsInFile(cxcursor::getCursorTU(cursor),
refCursor,
static_cast<const FileEntry *>(file),
*cxfile::getFileEntryRef(file),
visitor))
return CXResult_VisitBreak;
return CXResult_Success;
Expand All @@ -495,15 +496,15 @@ CXResult clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
}

if (Log)
*Log << TU << " @" << static_cast<const FileEntry *>(file);
*Log << TU << " @" << *cxfile::getFileEntryRef(file);

ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
if (!CXXUnit)
return CXResult_Invalid;

ASTUnit::ConcurrencyCheck Check(*CXXUnit);

if (findIncludesInFile(TU, static_cast<const FileEntry *>(file), visitor))
if (findIncludesInFile(TU, *cxfile::getFileEntryRef(file), visitor))
return CXResult_VisitBreak;
return CXResult_Success;
}
Expand Down
5 changes: 2 additions & 3 deletions clang/tools/libclang/CIndexInclusionStack.cpp
Expand Up @@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//

#include "CIndexer.h"
#include "CXFile.h"
#include "CXSourceLocation.h"
#include "CXTranslationUnit.h"
#include "clang/AST/DeclVisitor.h"
Expand Down Expand Up @@ -58,9 +59,7 @@ void getInclusions(bool IsLocal, unsigned n, CXTranslationUnit TU,
InclusionStack.pop_back();

// Callback to the client.
// FIXME: We should have a function to construct CXFiles.
CB(static_cast<CXFile>(const_cast<FileEntry *>(
static_cast<const FileEntry *>(FI.getContentCache().OrigEntry))),
CB(cxfile::makeCXFile(*FI.getContentCache().OrigEntry),
InclusionStack.data(), InclusionStack.size(), clientData);
}
}
Expand Down
5 changes: 2 additions & 3 deletions clang/tools/libclang/CLog.h
Expand Up @@ -10,6 +10,7 @@
#define LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H

#include "clang-c/Index.h"
#include "clang/Basic/FileEntry.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallString.h"
Expand All @@ -23,8 +24,6 @@ class format_object_base;
}

namespace clang {
class FileEntry;

namespace cxindex {

class Logger;
Expand Down Expand Up @@ -65,7 +64,7 @@ class Logger : public RefCountedBase<Logger> {
~Logger();

Logger &operator<<(CXTranslationUnit);
Logger &operator<<(const FileEntry *FE);
Logger &operator<<(FileEntryRef FE);
Logger &operator<<(CXCursor cursor);
Logger &operator<<(CXSourceLocation);
Logger &operator<<(CXSourceRange);
Expand Down

0 comments on commit fa5788f

Please sign in to comment.