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
This commit is contained in:
Fangrui Song 2018-10-10 09:52:41 -07:00
parent 7b19c87485
commit 82deedf8c3
5 changed files with 32 additions and 23 deletions

View File

@ -72,20 +72,21 @@ lsTextEdit ToTextEdit(const clang::SourceManager &SM,
} }
struct PreambleStatCache { struct PreambleStatCache {
llvm::StringMap<ErrorOr<vfs::Status>> Cache; llvm::StringMap<ErrorOr<llvm::vfs::Status>> Cache;
void Update(Twine Path, ErrorOr<vfs::Status> S) { void Update(Twine Path, ErrorOr<llvm::vfs::Status> S) {
Cache.try_emplace(Path.str(), std::move(S)); Cache.try_emplace(Path.str(), std::move(S));
} }
IntrusiveRefCntPtr<vfs::FileSystem> IntrusiveRefCntPtr<llvm::vfs::FileSystem>
Producer(IntrusiveRefCntPtr<vfs::FileSystem> FS) { Producer(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
struct VFS : vfs::ProxyFileSystem { struct VFS : llvm::vfs::ProxyFileSystem {
PreambleStatCache &Cache; PreambleStatCache &Cache;
VFS(IntrusiveRefCntPtr<vfs::FileSystem> FS, PreambleStatCache &Cache) VFS(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
PreambleStatCache &Cache)
: ProxyFileSystem(std::move(FS)), Cache(Cache) {} : ProxyFileSystem(std::move(FS)), Cache(Cache) {}
llvm::ErrorOr<std::unique_ptr<vfs::File>> llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
openFileForRead(const Twine &Path) override { openFileForRead(const Twine &Path) override {
auto File = getUnderlyingFS().openFileForRead(Path); auto File = getUnderlyingFS().openFileForRead(Path);
if (!File || !*File) if (!File || !*File)
@ -93,7 +94,7 @@ struct PreambleStatCache {
Cache.Update(Path, File->get()->status()); Cache.Update(Path, File->get()->status());
return File; return File;
} }
llvm::ErrorOr<vfs::Status> status(const Twine &Path) override { llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override {
auto S = getUnderlyingFS().status(Path); auto S = getUnderlyingFS().status(Path);
Cache.Update(Path, S); Cache.Update(Path, S);
return S; return S;
@ -102,14 +103,14 @@ struct PreambleStatCache {
return new VFS(std::move(FS), *this); return new VFS(std::move(FS), *this);
} }
IntrusiveRefCntPtr<vfs::FileSystem> IntrusiveRefCntPtr<llvm::vfs::FileSystem>
Consumer(IntrusiveRefCntPtr<vfs::FileSystem> FS) { Consumer(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
struct VFS : vfs::ProxyFileSystem { struct VFS : llvm::vfs::ProxyFileSystem {
const PreambleStatCache &Cache; const PreambleStatCache &Cache;
VFS(IntrusiveRefCntPtr<vfs::FileSystem> FS, VFS(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const PreambleStatCache &Cache) const PreambleStatCache &Cache)
: ProxyFileSystem(std::move(FS)), Cache(Cache) {} : ProxyFileSystem(std::move(FS)), Cache(Cache) {}
llvm::ErrorOr<vfs::Status> status(const Twine &Path) override { llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override {
auto I = Cache.Cache.find(Path.str()); auto I = Cache.Cache.find(Path.str());
if (I != Cache.Cache.end()) if (I != Cache.Cache.end())
return I->getValue(); return I->getValue();
@ -259,7 +260,7 @@ public:
std::unique_ptr<CompilerInstance> BuildCompilerInstance( std::unique_ptr<CompilerInstance> BuildCompilerInstance(
CompletionSession &session, std::unique_ptr<CompilerInvocation> CI, CompletionSession &session, std::unique_ptr<CompilerInvocation> CI,
IntrusiveRefCntPtr<vfs::FileSystem> FS, DiagnosticConsumer &DC, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, DiagnosticConsumer &DC,
const PreambleData *preamble, const WorkingFiles::Snapshot &snapshot, const PreambleData *preamble, const WorkingFiles::Snapshot &snapshot,
std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Bufs) { std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Bufs) {
std::string main = ResolveIfRelative( std::string main = ResolveIfRelative(
@ -307,7 +308,7 @@ bool Parse(CompilerInstance &Clang) {
} }
void BuildPreamble(CompletionSession &session, CompilerInvocation &CI, void BuildPreamble(CompletionSession &session, CompilerInvocation &CI,
IntrusiveRefCntPtr<vfs::FileSystem> FS, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const std::string &main, const std::string &main,
std::unique_ptr<PreambleStatCache> stat_cache) { std::unique_ptr<PreambleStatCache> stat_cache) {
std::shared_ptr<PreambleData> OldP = session.GetPreamble(); std::shared_ptr<PreambleData> OldP = session.GetPreamble();
@ -353,7 +354,8 @@ void *CompletionPreloadMain(void *manager_) {
WorkingFiles::Snapshot snapshot = WorkingFiles::Snapshot snapshot =
session->wfiles->AsSnapshot({StripFileType(session->file.filename)}); session->wfiles->AsSnapshot({StripFileType(session->file.filename)});
auto stat_cache = std::make_unique<PreambleStatCache>(); auto stat_cache = std::make_unique<PreambleStatCache>();
IntrusiveRefCntPtr<vfs::FileSystem> FS = stat_cache->Producer(session->FS); IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
stat_cache->Producer(session->FS);
if (std::unique_ptr<CompilerInvocation> CI = if (std::unique_ptr<CompilerInvocation> CI =
BuildCompilerInvocation(args, FS)) BuildCompilerInvocation(args, FS))
BuildPreamble(*session, *CI, FS, request.path, std::move(stat_cache)); BuildPreamble(*session, *CI, FS, request.path, std::move(stat_cache));
@ -391,7 +393,7 @@ void *CompletionMain(void *manager_) {
std::shared_ptr<CompletionSession> session = std::shared_ptr<CompletionSession> session =
manager->TryGetSession(path, false); manager->TryGetSession(path, false);
std::shared_ptr<PreambleData> preamble = session->GetPreamble(); std::shared_ptr<PreambleData> preamble = session->GetPreamble();
IntrusiveRefCntPtr<vfs::FileSystem> FS = IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
preamble ? preamble->stat_cache->Consumer(session->FS) : session->FS; preamble ? preamble->stat_cache->Consumer(session->FS) : session->FS;
std::unique_ptr<CompilerInvocation> CI = std::unique_ptr<CompilerInvocation> CI =
BuildCompilerInvocation(session->file.args, FS); BuildCompilerInvocation(session->file.args, FS);
@ -471,7 +473,7 @@ void *DiagnosticMain(void *manager_) {
std::shared_ptr<CompletionSession> session = std::shared_ptr<CompletionSession> session =
manager->TryGetSession(path, false); manager->TryGetSession(path, false);
std::shared_ptr<PreambleData> preamble = session->GetPreamble(); std::shared_ptr<PreambleData> preamble = session->GetPreamble();
IntrusiveRefCntPtr<vfs::FileSystem> FS = IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
preamble ? preamble->stat_cache->Consumer(session->FS) : session->FS; preamble ? preamble->stat_cache->Consumer(session->FS) : session->FS;
std::unique_ptr<CompilerInvocation> CI = std::unique_ptr<CompilerInvocation> CI =
BuildCompilerInvocation(session->file.args, FS); BuildCompilerInvocation(session->file.args, FS);

View File

@ -51,8 +51,8 @@ struct CompletionSession
bool inferred = false; bool inferred = false;
// TODO share // TODO share
llvm::IntrusiveRefCntPtr<clang::vfs::FileSystem> FS = llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
clang::vfs::getRealFileSystem(); llvm::vfs::getRealFileSystem();
std::shared_ptr<clang::PCHContainerOperations> PCH; std::shared_ptr<clang::PCHContainerOperations> PCH;
CompletionSession(const Project::Entry &file, WorkingFiles *wfiles, CompletionSession(const Project::Entry &file, WorkingFiles *wfiles,

View File

@ -71,7 +71,7 @@ Range FromTokenRange(const SourceManager &SM, const LangOptions &LangOpts,
std::unique_ptr<CompilerInvocation> std::unique_ptr<CompilerInvocation>
BuildCompilerInvocation(std::vector<const char *> args, BuildCompilerInvocation(std::vector<const char *> args,
IntrusiveRefCntPtr<vfs::FileSystem> VFS) { IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
std::string save = "-resource-dir=" + g_config->clang.resourceDir; std::string save = "-resource-dir=" + g_config->clang.resourceDir;
args.push_back(save.c_str()); args.push_back(save.c_str());
IntrusiveRefCntPtr<DiagnosticsEngine> Diags( IntrusiveRefCntPtr<DiagnosticsEngine> Diags(

View File

@ -10,6 +10,13 @@
#include <clang/Basic/SourceManager.h> #include <clang/Basic/SourceManager.h>
#include <clang/Frontend/CompilerInstance.h> #include <clang/Frontend/CompilerInstance.h>
#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); std::string PathFromFileEntry(const clang::FileEntry &file);
Range FromCharSourceRange(const clang::SourceManager &SM, Range FromCharSourceRange(const clang::SourceManager &SM,
@ -27,6 +34,6 @@ Range FromTokenRange(const clang::SourceManager &SM,
std::unique_ptr<clang::CompilerInvocation> std::unique_ptr<clang::CompilerInvocation>
BuildCompilerInvocation(std::vector<const char *> args, BuildCompilerInvocation(std::vector<const char *> args,
llvm::IntrusiveRefCntPtr<clang::vfs::FileSystem> VFS); llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);
const char *ClangBuiltinTypeName(int); const char *ClangBuiltinTypeName(int);

View File

@ -1211,7 +1211,7 @@ Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs,
const std::vector<std::pair<std::string, std::string>> &remapped, bool &ok) { const std::vector<std::pair<std::string, std::string>> &remapped, bool &ok) {
ok = true; ok = true;
auto PCH = std::make_shared<PCHContainerOperations>(); auto PCH = std::make_shared<PCHContainerOperations>();
llvm::IntrusiveRefCntPtr<vfs::FileSystem> FS = vfs::getRealFileSystem(); llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS = llvm::vfs::getRealFileSystem();
std::shared_ptr<CompilerInvocation> CI = BuildCompilerInvocation(args, FS); std::shared_ptr<CompilerInvocation> CI = BuildCompilerInvocation(args, FS);
// e.g. .s // e.g. .s
if (!CI) if (!CI)