Adapt llvm/llvm-project#74910: FileEntry::getName

Based on a patch by @zsrkmyn
This commit is contained in:
Fangrui Song 2024-02-01 23:58:38 -08:00
parent f36ecb0c0e
commit f8d2778b65
4 changed files with 38 additions and 1 deletions

View File

@ -23,13 +23,21 @@
using namespace clang; using namespace clang;
namespace ccls { namespace ccls {
#if LLVM_VERSION_MAJOR < 19
std::string pathFromFileEntry(const FileEntry &file) { std::string pathFromFileEntry(const FileEntry &file) {
#else
std::string pathFromFileEntry(FileEntryRef file) {
#endif
std::string ret; std::string ret;
if (file.getName().startswith("/../")) { if (file.getName().startswith("/../")) {
// Resolve symlinks outside of working folders. This handles leading path // Resolve symlinks outside of working folders. This handles leading path
// components, e.g. (/lib -> /usr/lib) in // components, e.g. (/lib -> /usr/lib) in
// /../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/utility // /../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/utility
#if LLVM_VERSION_MAJOR < 19
ret = file.tryGetRealPathName(); ret = file.tryGetRealPathName();
#else
ret = file.getFileEntry().tryGetRealPathName();
#endif
} else { } else {
// If getName() refers to a file within a workspace folder, we prefer it // If getName() refers to a file within a workspace folder, we prefer it
// (which may be a symlink). // (which may be a symlink).

View File

@ -22,7 +22,11 @@ namespace vfs = clang::vfs;
#endif #endif
namespace ccls { namespace ccls {
#if LLVM_VERSION_MAJOR < 19
std::string pathFromFileEntry(const clang::FileEntry &file); std::string pathFromFileEntry(const clang::FileEntry &file);
#else
std::string pathFromFileEntry(clang::FileEntryRef file);
#endif
bool isInsideMainFile(const clang::SourceManager &sm, clang::SourceLocation sl); bool isInsideMainFile(const clang::SourceManager &sm, clang::SourceLocation sl);

View File

@ -68,7 +68,11 @@ struct IndexParam {
// generating an index for it): // generating an index for it):
auto [it, inserted] = uid2file.try_emplace(fid); auto [it, inserted] = uid2file.try_emplace(fid);
if (inserted) { if (inserted) {
#if LLVM_VERSION_MAJOR < 19
const FileEntry *fe = ctx->getSourceManager().getFileEntryForID(fid); const FileEntry *fe = ctx->getSourceManager().getFileEntryForID(fid);
#else
OptionalFileEntryRef fe = ctx->getSourceManager().getFileEntryRefForID(fid);
#endif
if (!fe) if (!fe)
return; return;
std::string path = pathFromFileEntry(*fe); std::string path = pathFromFileEntry(*fe);
@ -94,9 +98,14 @@ struct IndexParam {
bool useMultiVersion(FileID fid) { bool useMultiVersion(FileID fid) {
auto it = uid2multi.try_emplace(fid); auto it = uid2multi.try_emplace(fid);
if (it.second) if (it.second) {
#if LLVM_VERSION_MAJOR < 19
if (const FileEntry *fe = ctx->getSourceManager().getFileEntryForID(fid)) if (const FileEntry *fe = ctx->getSourceManager().getFileEntryForID(fid))
#else
if (OptionalFileEntryRef fe = ctx->getSourceManager().getFileEntryRefForID(fid))
#endif
it.first->second = multiVersionMatcher->matches(pathFromFileEntry(*fe)); it.first->second = multiVersionMatcher->matches(pathFromFileEntry(*fe));
}
return it.first->second; return it.first->second;
} }
}; };
@ -636,7 +645,11 @@ public:
static int getFileLID(IndexFile *db, SourceManager &sm, FileID fid) { static int getFileLID(IndexFile *db, SourceManager &sm, FileID fid) {
auto [it, inserted] = db->uid2lid_and_path.try_emplace(fid); auto [it, inserted] = db->uid2lid_and_path.try_emplace(fid);
if (inserted) { if (inserted) {
#if LLVM_VERSION_MAJOR < 19
const FileEntry *fe = sm.getFileEntryForID(fid); const FileEntry *fe = sm.getFileEntryForID(fid);
#else
OptionalFileEntryRef fe = sm.getFileEntryRefForID(fid);
#endif
if (!fe) { if (!fe) {
it->second.first = -1; it->second.first = -1;
return -1; return -1;
@ -1124,7 +1137,11 @@ public:
filenameRange, nullptr); filenameRange, nullptr);
FileID fid = sm.getFileID(filenameRange.getBegin()); FileID fid = sm.getFileID(filenameRange.getBegin());
if (IndexFile *db = param.consumeFile(fid)) { if (IndexFile *db = param.consumeFile(fid)) {
#if LLVM_VERSION_MAJOR < 19
std::string path = pathFromFileEntry(*file); std::string path = pathFromFileEntry(*file);
#else
std::string path = pathFromFileEntry(*fileRef);
#endif
if (path.size()) if (path.size())
db->includes.push_back({spell.start.line, intern(path)}); db->includes.push_back({spell.start.line, intern(path)});
} }

View File

@ -194,7 +194,11 @@ public:
const FileEntry *file = fileRef ? &fileRef->getFileEntry() : nullptr; const FileEntry *file = fileRef ? &fileRef->getFileEntry() : nullptr;
#endif #endif
if (file && seen.insert(file).second) if (file && seen.insert(file).second)
#if LLVM_VERSION_MAJOR < 19
out.emplace_back(pathFromFileEntry(*file), file->getModificationTime()); out.emplace_back(pathFromFileEntry(*file), file->getModificationTime());
#else
out.emplace_back(pathFromFileEntry(*fileRef), file->getModificationTime());
#endif
} }
}; };
@ -236,7 +240,11 @@ public:
FileID fid = sm.getFileID(l); FileID fid = sm.getFileID(l);
auto it = fID2concerned.try_emplace(fid.getHashValue()); auto it = fID2concerned.try_emplace(fid.getHashValue());
if (it.second) { if (it.second) {
#if LLVM_VERSION_MAJOR < 19
const FileEntry *fe = sm.getFileEntryForID(fid); const FileEntry *fe = sm.getFileEntryForID(fid);
#else
OptionalFileEntryRef fe = sm.getFileEntryRefForID(fid);
#endif
it.first->second = fe && pathFromFileEntry(*fe) == path; it.first->second = fe && pathFromFileEntry(*fe) == path;
} }
return it.first->second; return it.first->second;