Skip to content

Commit

Permalink
Adapt llvm/llvm-project#74910: FileEntry::getName
Browse files Browse the repository at this point in the history
Based on a patch by @zsrkmyn
  • Loading branch information
MaskRay committed Feb 2, 2024
1 parent f36ecb0 commit f8d2778
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/clang_tu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@
using namespace clang;

namespace ccls {
#if LLVM_VERSION_MAJOR < 19
std::string pathFromFileEntry(const FileEntry &file) {
#else
std::string pathFromFileEntry(FileEntryRef file) {
#endif
std::string ret;
if (file.getName().startswith("/../")) {
// Resolve symlinks outside of working folders. This handles leading path
// components, e.g. (/lib -> /usr/lib) in
// /../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/utility
#if LLVM_VERSION_MAJOR < 19
ret = file.tryGetRealPathName();
#else
ret = file.getFileEntry().tryGetRealPathName();
#endif
} else {
// If getName() refers to a file within a workspace folder, we prefer it
// (which may be a symlink).
Expand Down
4 changes: 4 additions & 0 deletions src/clang_tu.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ namespace vfs = clang::vfs;
#endif

namespace ccls {
#if LLVM_VERSION_MAJOR < 19
std::string pathFromFileEntry(const clang::FileEntry &file);
#else
std::string pathFromFileEntry(clang::FileEntryRef file);
#endif

bool isInsideMainFile(const clang::SourceManager &sm, clang::SourceLocation sl);

Expand Down
19 changes: 18 additions & 1 deletion src/indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ struct IndexParam {
// generating an index for it):
auto [it, inserted] = uid2file.try_emplace(fid);
if (inserted) {
#if LLVM_VERSION_MAJOR < 19
const FileEntry *fe = ctx->getSourceManager().getFileEntryForID(fid);
#else
OptionalFileEntryRef fe = ctx->getSourceManager().getFileEntryRefForID(fid);
#endif
if (!fe)
return;
std::string path = pathFromFileEntry(*fe);
Expand All @@ -94,9 +98,14 @@ struct IndexParam {

bool useMultiVersion(FileID 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))
#else
if (OptionalFileEntryRef fe = ctx->getSourceManager().getFileEntryRefForID(fid))
#endif
it.first->second = multiVersionMatcher->matches(pathFromFileEntry(*fe));
}
return it.first->second;
}
};
Expand Down Expand Up @@ -636,7 +645,11 @@ class IndexDataConsumer : public index::IndexDataConsumer {
static int getFileLID(IndexFile *db, SourceManager &sm, FileID fid) {
auto [it, inserted] = db->uid2lid_and_path.try_emplace(fid);
if (inserted) {
#if LLVM_VERSION_MAJOR < 19
const FileEntry *fe = sm.getFileEntryForID(fid);
#else
OptionalFileEntryRef fe = sm.getFileEntryRefForID(fid);
#endif
if (!fe) {
it->second.first = -1;
return -1;
Expand Down Expand Up @@ -1124,7 +1137,11 @@ class IndexPPCallbacks : public PPCallbacks {
filenameRange, nullptr);
FileID fid = sm.getFileID(filenameRange.getBegin());
if (IndexFile *db = param.consumeFile(fid)) {
#if LLVM_VERSION_MAJOR < 19
std::string path = pathFromFileEntry(*file);
#else
std::string path = pathFromFileEntry(*fileRef);
#endif
if (path.size())
db->includes.push_back({spell.start.line, intern(path)});
}
Expand Down
8 changes: 8 additions & 0 deletions src/sema_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ class StoreInclude : public PPCallbacks {
const FileEntry *file = fileRef ? &fileRef->getFileEntry() : nullptr;
#endif
if (file && seen.insert(file).second)
#if LLVM_VERSION_MAJOR < 19
out.emplace_back(pathFromFileEntry(*file), file->getModificationTime());
#else
out.emplace_back(pathFromFileEntry(*fileRef), file->getModificationTime());
#endif
}
};

Expand Down Expand Up @@ -236,7 +240,11 @@ class StoreDiags : public DiagnosticConsumer {
FileID fid = sm.getFileID(l);
auto it = fID2concerned.try_emplace(fid.getHashValue());
if (it.second) {
#if LLVM_VERSION_MAJOR < 19
const FileEntry *fe = sm.getFileEntryForID(fid);
#else
OptionalFileEntryRef fe = sm.getFileEntryRefForID(fid);
#endif
it.first->second = fe && pathFromFileEntry(*fe) == path;
}
return it.first->second;
Expand Down

0 comments on commit f8d2778

Please sign in to comment.