From 51081c3cd259398e9b578fe4714060e257b36faf Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 10 Oct 2018 09:52:41 -0700 Subject: [PATCH] Add namespace alias clang::vfs = llvm::vfs to adapt D52783 vfs::x should be written as llvm::vfs::x to work around a [namepace.udir] bug before GCC 8 when namespace alias is used --- src/clang_complete.cc | 38 ++++++++++++++++++++------------------ src/clang_complete.hh | 4 ++-- src/clang_tu.cc | 2 +- src/clang_tu.hh | 9 ++++++++- src/indexer.cc | 2 +- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/clang_complete.cc b/src/clang_complete.cc index 32ef2ef7..317d0908 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -84,20 +84,21 @@ lsTextEdit ToTextEdit(const clang::SourceManager &SM, } struct PreambleStatCache { - llvm::StringMap> Cache; + llvm::StringMap> Cache; - void Update(Twine Path, ErrorOr S) { + void Update(Twine Path, ErrorOr S) { Cache.try_emplace(Path.str(), std::move(S)); } - IntrusiveRefCntPtr - Producer(IntrusiveRefCntPtr FS) { - struct VFS : vfs::ProxyFileSystem { + IntrusiveRefCntPtr + Producer(IntrusiveRefCntPtr FS) { + struct VFS : llvm::vfs::ProxyFileSystem { PreambleStatCache &Cache; - VFS(IntrusiveRefCntPtr FS, PreambleStatCache &Cache) + VFS(IntrusiveRefCntPtr FS, + PreambleStatCache &Cache) : ProxyFileSystem(std::move(FS)), Cache(Cache) {} - llvm::ErrorOr> + llvm::ErrorOr> openFileForRead(const Twine &Path) override { auto File = getUnderlyingFS().openFileForRead(Path); if (!File || !*File) @@ -105,7 +106,7 @@ struct PreambleStatCache { Cache.Update(Path, File->get()->status()); return File; } - llvm::ErrorOr status(const Twine &Path) override { + llvm::ErrorOr status(const Twine &Path) override { auto S = getUnderlyingFS().status(Path); Cache.Update(Path, S); return S; @@ -114,14 +115,14 @@ struct PreambleStatCache { return new VFS(std::move(FS), *this); } - IntrusiveRefCntPtr - Consumer(IntrusiveRefCntPtr FS) { - struct VFS : vfs::ProxyFileSystem { + IntrusiveRefCntPtr + Consumer(IntrusiveRefCntPtr FS) { + struct VFS : llvm::vfs::ProxyFileSystem { const PreambleStatCache &Cache; - VFS(IntrusiveRefCntPtr FS, + VFS(IntrusiveRefCntPtr FS, const PreambleStatCache &Cache) : ProxyFileSystem(std::move(FS)), Cache(Cache) {} - llvm::ErrorOr status(const Twine &Path) override { + llvm::ErrorOr status(const Twine &Path) override { auto I = Cache.Cache.find(Path.str()); if (I != Cache.Cache.end()) return I->getValue(); @@ -271,7 +272,7 @@ public: std::unique_ptr BuildCompilerInstance( CompletionSession &session, std::unique_ptr CI, - IntrusiveRefCntPtr FS, DiagnosticConsumer &DC, + IntrusiveRefCntPtr FS, DiagnosticConsumer &DC, const PreambleData *preamble, const WorkingFiles::Snapshot &snapshot, std::vector> &Bufs) { std::string main = ResolveIfRelative( @@ -319,7 +320,7 @@ bool Parse(CompilerInstance &Clang) { } void BuildPreamble(CompletionSession &session, CompilerInvocation &CI, - IntrusiveRefCntPtr FS, + IntrusiveRefCntPtr FS, const std::string &main, std::unique_ptr stat_cache) { std::shared_ptr OldP = session.GetPreamble(); @@ -364,7 +365,8 @@ void *CompletionPreloadMain(void *manager_) { WorkingFiles::Snapshot snapshot = session->wfiles->AsSnapshot({StripFileType(session->file.filename)}); auto stat_cache = std::make_unique(); - IntrusiveRefCntPtr FS = stat_cache->Producer(session->FS); + IntrusiveRefCntPtr FS = + stat_cache->Producer(session->FS); if (std::unique_ptr CI = BuildCompilerInvocation(args, FS)) BuildPreamble(*session, *CI, FS, request.path, std::move(stat_cache)); @@ -402,7 +404,7 @@ void *CompletionMain(void *manager_) { std::shared_ptr session = manager->TryGetSession(path, false); std::shared_ptr preamble = session->GetPreamble(); - IntrusiveRefCntPtr FS = + IntrusiveRefCntPtr FS = preamble ? preamble->stat_cache->Consumer(session->FS) : session->FS; std::unique_ptr CI = BuildCompilerInvocation(session->file.args, FS); @@ -482,7 +484,7 @@ void *DiagnosticMain(void *manager_) { std::shared_ptr session = manager->TryGetSession(path, false); std::shared_ptr preamble = session->GetPreamble(); - IntrusiveRefCntPtr FS = + IntrusiveRefCntPtr FS = preamble ? preamble->stat_cache->Consumer(session->FS) : session->FS; std::unique_ptr CI = BuildCompilerInvocation(session->file.args, FS); diff --git a/src/clang_complete.hh b/src/clang_complete.hh index 7d610d2d..822ea6ca 100644 --- a/src/clang_complete.hh +++ b/src/clang_complete.hh @@ -63,8 +63,8 @@ struct CompletionSession bool inferred = false; // TODO share - llvm::IntrusiveRefCntPtr FS = - clang::vfs::getRealFileSystem(); + llvm::IntrusiveRefCntPtr FS = + llvm::vfs::getRealFileSystem(); std::shared_ptr PCH; CompletionSession(const Project::Entry &file, WorkingFiles *wfiles, diff --git a/src/clang_tu.cc b/src/clang_tu.cc index d1dd3f02..5392ee52 100644 --- a/src/clang_tu.cc +++ b/src/clang_tu.cc @@ -83,7 +83,7 @@ Range FromTokenRange(const SourceManager &SM, const LangOptions &LangOpts, std::unique_ptr BuildCompilerInvocation(std::vector args, - IntrusiveRefCntPtr VFS) { + IntrusiveRefCntPtr VFS) { std::string save = "-resource-dir=" + g_config->clang.resourceDir; args.push_back(save.c_str()); IntrusiveRefCntPtr Diags( diff --git a/src/clang_tu.hh b/src/clang_tu.hh index 96325466..30d0f464 100644 --- a/src/clang_tu.hh +++ b/src/clang_tu.hh @@ -22,6 +22,13 @@ limitations under the License. #include #include +#if LLVM_VERSION_MAJOR < 8 +// D52783 Lift VFS from clang to llvm +namespace llvm { +namespace vfs = clang::vfs; +} +#endif + std::string PathFromFileEntry(const clang::FileEntry &file); Range FromCharSourceRange(const clang::SourceManager &SM, @@ -39,6 +46,6 @@ Range FromTokenRange(const clang::SourceManager &SM, std::unique_ptr BuildCompilerInvocation(std::vector args, - llvm::IntrusiveRefCntPtr VFS); + llvm::IntrusiveRefCntPtr VFS); const char *ClangBuiltinTypeName(int); diff --git a/src/indexer.cc b/src/indexer.cc index 34137630..1b9aaac1 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1225,7 +1225,7 @@ Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs, const std::vector> &remapped, bool &ok) { ok = true; auto PCH = std::make_shared(); - llvm::IntrusiveRefCntPtr FS = vfs::getRealFileSystem(); + llvm::IntrusiveRefCntPtr FS = llvm::vfs::getRealFileSystem(); std::shared_ptr CI = BuildCompilerInvocation(args, FS); // e.g. .s if (!CI)