[clangd] Added forgotten files

llvm-svn: 323423
This commit is contained in:
Ilya Biryukov 2018-01-25 14:29:29 +00:00
parent e59bf81e74
commit 0d05e2d064
2 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,44 @@
//===--- CompileArgsCache.cpp - Main clangd server code ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===---------------------------------------------------------------------===//
#include "CompileArgsCache.h"
namespace clang {
namespace clangd {
namespace {
tooling::CompileCommand getCompileCommand(GlobalCompilationDatabase &CDB,
PathRef File, PathRef ResourceDir) {
llvm::Optional<tooling::CompileCommand> C = CDB.getCompileCommand(File);
if (!C) // FIXME: Suppress diagnostics? Let the user know?
C = CDB.getFallbackCommand(File);
// Inject the resource dir.
// FIXME: Don't overwrite it if it's already there.
C->CommandLine.push_back("-resource-dir=" + ResourceDir.str());
return std::move(*C);
}
} // namespace
CompileArgsCache::CompileArgsCache(GlobalCompilationDatabase &CDB,
Path ResourceDir)
: CDB(CDB), ResourceDir(std::move(ResourceDir)) {}
tooling::CompileCommand CompileArgsCache::getCompileCommand(PathRef File) {
auto It = Cached.find(File);
if (It == Cached.end()) {
It = Cached.insert({File, clangd::getCompileCommand(CDB, File, ResourceDir)})
.first;
}
return It->second;
}
void CompileArgsCache::invalidate(PathRef File) { Cached.erase(File); }
} // namespace clangd
} // namespace clang

View File

@ -0,0 +1,43 @@
//===--- CompileArgsCache.h -------------------------------------*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===---------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILEARGSCACHE_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILEARGSCACHE_H
#include "GlobalCompilationDatabase.h"
#include "Path.h"
#include "clang/Tooling/CompilationDatabase.h"
namespace clang {
namespace clangd {
/// A helper class used by ClangdServer to get compile commands from CDB.
/// Also caches CompileCommands produced by compilation database on per-file
/// basis. This avoids queries to CDB that can be much more expensive than a
/// table lookup.
class CompileArgsCache {
public:
CompileArgsCache(GlobalCompilationDatabase &CDB, Path ResourceDir);
/// Gets compile command for \p File from cache or CDB if it's not in the
/// cache.
tooling::CompileCommand getCompileCommand(PathRef File);
/// Removes a cache entry for \p File, if it's present in the cache.
void invalidate(PathRef File);
private:
GlobalCompilationDatabase &CDB;
const Path ResourceDir;
llvm::StringMap<tooling::CompileCommand> Cached;
};
} // namespace clangd
} // namespace clang
#endif