[clang] fa5788f - [clang][index] NFCI: Make `CXFile` a `FileEntryRef`
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 15 03:35:02 PDT 2023
Author: Jan Svoboda
Date: 2023-06-15T12:34:54+02:00
New Revision: fa5788ff8dc10f36e0947757e335cd180a1a63c9
URL: https://github.com/llvm/llvm-project/commit/fa5788ff8dc10f36e0947757e335cd180a1a63c9
DIFF: https://github.com/llvm/llvm-project/commit/fa5788ff8dc10f36e0947757e335cd180a1a63c9.diff
LOG: [clang][index] NFCI: Make `CXFile` a `FileEntryRef`
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
Added:
clang/tools/libclang/CXFile.h
Modified:
clang/include/clang/Basic/Module.h
clang/include/clang/Frontend/ASTUnit.h
clang/lib/Basic/Module.cpp
clang/lib/Frontend/ASTUnit.cpp
clang/lib/Frontend/FrontendAction.cpp
clang/lib/Serialization/ASTWriter.cpp
clang/tools/libclang/CIndex.cpp
clang/tools/libclang/CIndexHigh.cpp
clang/tools/libclang/CIndexInclusionStack.cpp
clang/tools/libclang/CLog.h
clang/tools/libclang/CXIndexDataConsumer.cpp
clang/tools/libclang/CXIndexDataConsumer.h
clang/tools/libclang/CXLoadedDiagnostic.cpp
clang/tools/libclang/CXSourceLocation.cpp
clang/tools/libclang/Indexing.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/Module.h b/clang/include/clang/Basic/Module.h
index 9625a682c3549..399ed92a325e5 100644
--- a/clang/include/clang/Basic/Module.h
+++ b/clang/include/clang/Basic/Module.h
@@ -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;
@@ -672,7 +672,7 @@ 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) {
@@ -680,7 +680,7 @@ class alignas(8) Module {
}
/// 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.
diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h
index 05d149ee6dff8..db8b598866c25 100644
--- a/clang/include/clang/Frontend/ASTUnit.h
+++ b/clang/include/clang/Frontend/ASTUnit.h
@@ -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.
diff --git a/clang/lib/Basic/Module.cpp b/clang/lib/Basic/Module.cpp
index 057fc77d0e993..3f9b8d0c775b9 100644
--- a/clang/lib/Basic/Module.cpp
+++ b/clang/lib/Basic/Module.cpp
@@ -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();
}
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 32ae76d673b6c..a42df66148518 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -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) {
@@ -2670,7 +2670,7 @@ const FileEntry *ASTUnit::getPCHFile() {
if (Mod)
return Mod->File;
- return nullptr;
+ return std::nullopt;
}
bool ASTUnit::isModuleFile() const {
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index cfac2f8c4e5a6..c6f958a6077bf 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -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
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 5ac3030a7a676..ff5bab7c99f78 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -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);
}
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 40c7586fe44b5..25118c702f1be 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -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"
@@ -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) {
@@ -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,
@@ -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)
@@ -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);
@@ -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;
}
@@ -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());
}
//===----------------------------------------------------------------------===//
@@ -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) {
@@ -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();
}
@@ -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) {
@@ -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();
}
@@ -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;
}
@@ -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();
@@ -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;
}
diff --git a/clang/tools/libclang/CIndexHigh.cpp b/clang/tools/libclang/CIndexHigh.cpp
index 0b49d1842e12a..c055ff91da557 100644
--- a/clang/tools/libclang/CIndexHigh.cpp
+++ b/clang/tools/libclang/CIndexHigh.cpp
@@ -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"
@@ -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)
@@ -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;
@@ -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;
@@ -495,7 +496,7 @@ 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)
@@ -503,7 +504,7 @@ CXResult clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
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;
}
diff --git a/clang/tools/libclang/CIndexInclusionStack.cpp b/clang/tools/libclang/CIndexInclusionStack.cpp
index 9cb9e18401f0c..8c7e1758c606a 100644
--- a/clang/tools/libclang/CIndexInclusionStack.cpp
+++ b/clang/tools/libclang/CIndexInclusionStack.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "CIndexer.h"
+#include "CXFile.h"
#include "CXSourceLocation.h"
#include "CXTranslationUnit.h"
#include "clang/AST/DeclVisitor.h"
@@ -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);
}
}
diff --git a/clang/tools/libclang/CLog.h b/clang/tools/libclang/CLog.h
index ffd0940e7a1b3..6ce43a01ee8f2 100644
--- a/clang/tools/libclang/CLog.h
+++ b/clang/tools/libclang/CLog.h
@@ -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"
@@ -23,8 +24,6 @@ class format_object_base;
}
namespace clang {
- class FileEntry;
-
namespace cxindex {
class Logger;
@@ -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);
diff --git a/clang/tools/libclang/CXFile.h b/clang/tools/libclang/CXFile.h
new file mode 100644
index 0000000000000..8cb28a7ad0051
--- /dev/null
+++ b/clang/tools/libclang/CXFile.h
@@ -0,0 +1,29 @@
+//===- CXFile.h - Routines for manipulating CXFile --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXFILE_H
+#define LLVM_CLANG_TOOLS_LIBCLANG_CXFILE_H
+
+#include "clang-c/CXFile.h"
+#include "clang/Basic/FileEntry.h"
+
+namespace clang {
+namespace cxfile {
+inline CXFile makeCXFile(OptionalFileEntryRef FE) {
+ return CXFile(FE ? &FE->getMapEntry() : nullptr);
+}
+
+inline OptionalFileEntryRef getFileEntryRef(CXFile File) {
+ if (!File)
+ return std::nullopt;
+ return FileEntryRef(*reinterpret_cast<const FileEntryRef::MapEntry *>(File));
+}
+} // namespace cxfile
+} // namespace clang
+
+#endif
diff --git a/clang/tools/libclang/CXIndexDataConsumer.cpp b/clang/tools/libclang/CXIndexDataConsumer.cpp
index 83f1f72cb7850..cb9c81328b397 100644
--- a/clang/tools/libclang/CXIndexDataConsumer.cpp
+++ b/clang/tools/libclang/CXIndexDataConsumer.cpp
@@ -8,6 +8,7 @@
#include "CXIndexDataConsumer.h"
#include "CIndexDiagnostic.h"
+#include "CXFile.h"
#include "CXTranslationUnit.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclCXX.h"
@@ -451,13 +452,11 @@ bool CXIndexDataConsumer::shouldAbort() {
return CB.abortQuery(ClientData, nullptr);
}
-void CXIndexDataConsumer::enteredMainFile(const FileEntry *File) {
+void CXIndexDataConsumer::enteredMainFile(OptionalFileEntryRef File) {
if (File && CB.enteredMainFile) {
CXIdxClientFile idxFile =
- CB.enteredMainFile(ClientData,
- static_cast<CXFile>(const_cast<FileEntry *>(File)),
- nullptr);
- FileMap[File] = idxFile;
+ CB.enteredMainFile(ClientData, cxfile::makeCXFile(*File), nullptr);
+ FileMap[*File] = idxFile;
}
}
@@ -474,8 +473,7 @@ void CXIndexDataConsumer::ppIncludedFile(SourceLocation hashLoc,
ScratchAlloc SA(*this);
CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
SA.toCStr(filename),
- static_cast<CXFile>(
- const_cast<FileEntry *>(FE)),
+ cxfile::makeCXFile(File),
isImport, isAngled, isModuleImport };
CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
FileMap[FE] = idxFile;
@@ -497,23 +495,20 @@ void CXIndexDataConsumer::importedModule(const ImportDecl *ImportD) {
if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule())
return;
- FileEntry *FE = nullptr;
- if (auto File = Mod->getASTFile())
- FE = const_cast<FileEntry *>(&File->getFileEntry());
- CXIdxImportedASTFileInfo Info = {static_cast<CXFile>(FE), Mod,
+ OptionalFileEntryRef FE = Mod->getASTFile();
+ CXIdxImportedASTFileInfo Info = {cxfile::makeCXFile(FE), Mod,
getIndexLoc(ImportD->getLocation()),
ImportD->isImplicit()};
CXIdxClientASTFile astFile = CB.importedASTFile(ClientData, &Info);
(void)astFile;
}
-void CXIndexDataConsumer::importedPCH(const FileEntry *File) {
+void CXIndexDataConsumer::importedPCH(FileEntryRef File) {
if (!CB.importedASTFile)
return;
CXIdxImportedASTFileInfo Info = {
- static_cast<CXFile>(
- const_cast<FileEntry *>(File)),
+ cxfile::makeCXFile(File),
/*module=*/nullptr,
getIndexLoc(SourceLocation()),
/*isImplicit=*/false
@@ -1108,11 +1103,11 @@ void CXIndexDataConsumer::translateLoc(SourceLocation Loc,
if (FID.isInvalid())
return;
- const FileEntry *FE = SM.getFileEntryForID(FID);
+ OptionalFileEntryRefDegradesToFileEntryPtr FE = SM.getFileEntryRefForID(FID);
if (indexFile)
*indexFile = getIndexFile(FE);
if (file)
- *file = const_cast<FileEntry *>(FE);
+ *file = cxfile::makeCXFile(FE);
if (line)
*line = SM.getLineNumber(FID, FileOffset);
if (column)
diff --git a/clang/tools/libclang/CXIndexDataConsumer.h b/clang/tools/libclang/CXIndexDataConsumer.h
index b78ef367eeaa1..afa2239ed653f 100644
--- a/clang/tools/libclang/CXIndexDataConsumer.h
+++ b/clang/tools/libclang/CXIndexDataConsumer.h
@@ -360,14 +360,14 @@ class CXIndexDataConsumer : public index::IndexDataConsumer {
bool hasDiagnosticCallback() const { return CB.diagnostic; }
- void enteredMainFile(const FileEntry *File);
+ void enteredMainFile(OptionalFileEntryRef File);
void ppIncludedFile(SourceLocation hashLoc, StringRef filename,
OptionalFileEntryRef File, bool isImport, bool isAngled,
bool isModuleImport);
void importedModule(const ImportDecl *ImportD);
- void importedPCH(const FileEntry *File);
+ void importedPCH(FileEntryRef File);
void startedTranslationUnit();
diff --git a/clang/tools/libclang/CXLoadedDiagnostic.cpp b/clang/tools/libclang/CXLoadedDiagnostic.cpp
index bb6942a45f460..ed89a6a7754da 100644
--- a/clang/tools/libclang/CXLoadedDiagnostic.cpp
+++ b/clang/tools/libclang/CXLoadedDiagnostic.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "CXLoadedDiagnostic.h"
+#include "CXFile.h"
#include "CXString.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
@@ -44,7 +45,7 @@ class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl {
FileSystemOptions FO;
FileManager FakeFiles;
- llvm::DenseMap<unsigned, const FileEntry *> Files;
+ llvm::DenseMap<unsigned, FileEntryRef> Files;
/// Copy the string into our own allocator.
const char *copyString(StringRef Blob) {
@@ -275,9 +276,10 @@ DiagLoader::readLocation(const serialized_diags::Location &SDLoc,
if (FileID == 0)
LoadedLoc.file = nullptr;
else {
- LoadedLoc.file = const_cast<FileEntry *>(TopDiags->Files[FileID]);
- if (!LoadedLoc.file)
+ auto It = TopDiags->Files.find(FileID);
+ if (It == TopDiags->Files.end())
return reportInvalidFile("Corrupted file entry in source location");
+ LoadedLoc.file = cxfile::makeCXFile(It->second);
}
LoadedLoc.line = SDLoc.Line;
LoadedLoc.column = SDLoc.Col;
@@ -342,8 +344,8 @@ std::error_code DiagLoader::visitFilenameRecord(unsigned ID, unsigned Size,
if (Name.size() > 65536)
return reportInvalidFile("Out-of-bounds string in filename");
TopDiags->FileNames[ID] = TopDiags->copyString(Name);
- TopDiags->Files[ID] =
- TopDiags->FakeFiles.getVirtualFile(Name, Size, Timestamp);
+ TopDiags->Files.insert(
+ {ID, TopDiags->FakeFiles.getVirtualFileRef(Name, Size, Timestamp)});
return std::error_code();
}
diff --git a/clang/tools/libclang/CXSourceLocation.cpp b/clang/tools/libclang/CXSourceLocation.cpp
index 39c2194f7df17..ba70cbfee8995 100644
--- a/clang/tools/libclang/CXSourceLocation.cpp
+++ b/clang/tools/libclang/CXSourceLocation.cpp
@@ -13,6 +13,7 @@
#include "CXSourceLocation.h"
#include "CIndexer.h"
#include "CLog.h"
+#include "CXFile.h"
#include "CXLoadedDiagnostic.h"
#include "CXString.h"
#include "CXTranslationUnit.h"
@@ -128,19 +129,19 @@ CXSourceLocation clang_getLocation(CXTranslationUnit TU,
LogRef Log = Logger::make(__func__);
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
- const FileEntry *File = static_cast<const FileEntry *>(file);
+ FileEntryRef File = *cxfile::getFileEntryRef(file);
SourceLocation SLoc = CXXUnit->getLocation(File, line, column);
if (SLoc.isInvalid()) {
if (Log)
*Log << llvm::format("(\"%s\", %d, %d) = invalid",
- File->getName().str().c_str(), line, column);
+ File.getName().str().c_str(), line, column);
return clang_getNullLocation();
}
CXSourceLocation CXLoc =
cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
if (Log)
- *Log << llvm::format("(\"%s\", %d, %d) = ", File->getName().str().c_str(),
+ *Log << llvm::format("(\"%s\", %d, %d) = ", File.getName().str().c_str(),
line, column)
<< CXLoc;
@@ -160,7 +161,7 @@ CXSourceLocation clang_getLocationForOffset(CXTranslationUnit TU,
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
SourceLocation SLoc
- = CXXUnit->getLocation(static_cast<const FileEntry *>(file), offset);
+ = CXXUnit->getLocation(*cxfile::getFileEntryRef(file), offset);
if (SLoc.isInvalid())
return clang_getNullLocation();
@@ -251,7 +252,7 @@ void clang_getExpansionLocation(CXSourceLocation location,
}
if (file)
- *file = const_cast<FileEntry *>(SM.getFileEntryForSLocEntry(sloc));
+ *file = cxfile::makeCXFile(SM.getFileEntryRefForID(fileID));
if (line)
*line = SM.getExpansionLineNumber(ExpansionLoc);
if (column)
@@ -328,7 +329,7 @@ void clang_getSpellingLocation(CXSourceLocation location,
return createNullLocation(file, line, column, offset);
if (file)
- *file = const_cast<FileEntry *>(SM.getFileEntryForID(FID));
+ *file = cxfile::makeCXFile(SM.getFileEntryRefForID(FID));
if (line)
*line = SM.getLineNumber(FID, FileOffset);
if (column)
@@ -364,7 +365,7 @@ void clang_getFileLocation(CXSourceLocation location,
return createNullLocation(file, line, column, offset);
if (file)
- *file = const_cast<FileEntry *>(SM.getFileEntryForID(FID));
+ *file = cxfile::makeCXFile(SM.getFileEntryRefForID(FID));
if (line)
*line = SM.getLineNumber(FID, FileOffset);
if (column)
diff --git a/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp
index b6dfb1f47b598..5fdc3de0d2bfa 100644
--- a/clang/tools/libclang/Indexing.cpp
+++ b/clang/tools/libclang/Indexing.cpp
@@ -255,7 +255,8 @@ class IndexPPCallbacks : public PPCallbacks {
if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
IsMainFileEntered = true;
- DataConsumer.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID()));
+ DataConsumer.enteredMainFile(
+ *SM.getFileEntryRefForID(SM.getMainFileID()));
}
}
@@ -350,8 +351,8 @@ class IndexingFrontendAction : public ASTFrontendAction {
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
if (!PPOpts.ImplicitPCHInclude.empty()) {
- auto File = CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude);
- if (File)
+ if (auto File =
+ CI.getFileManager().getOptionalFileRef(PPOpts.ImplicitPCHInclude))
DataConsumer->importedPCH(*File);
}
@@ -694,17 +695,18 @@ static CXErrorCode clang_indexTranslationUnit_Impl(
ASTUnit::ConcurrencyCheck Check(*Unit);
- if (const FileEntry *PCHFile = Unit->getPCHFile())
- DataConsumer.importedPCH(PCHFile);
+ if (OptionalFileEntryRef PCHFile = Unit->getPCHFile())
+ DataConsumer.importedPCH(*PCHFile);
FileManager &FileMgr = Unit->getFileManager();
if (Unit->getOriginalSourceFileName().empty())
- DataConsumer.enteredMainFile(nullptr);
- else if (auto MainFile = FileMgr.getFile(Unit->getOriginalSourceFileName()))
+ DataConsumer.enteredMainFile(std::nullopt);
+ else if (auto MainFile =
+ FileMgr.getFileRef(Unit->getOriginalSourceFileName()))
DataConsumer.enteredMainFile(*MainFile);
else
- DataConsumer.enteredMainFile(nullptr);
+ DataConsumer.enteredMainFile(std::nullopt);
DataConsumer.setASTContext(Unit->getASTContext());
DataConsumer.startedTranslationUnit();
More information about the cfe-commits
mailing list