[clang-tools-extra] r323423 - [clangd] Added forgotten files

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 25 06:29:30 PST 2018


Author: ibiryukov
Date: Thu Jan 25 06:29:29 2018
New Revision: 323423

URL: http://llvm.org/viewvc/llvm-project?rev=323423&view=rev
Log:
[clangd] Added forgotten files

Added:
    clang-tools-extra/trunk/clangd/CompileArgsCache.cpp
    clang-tools-extra/trunk/clangd/CompileArgsCache.h

Added: clang-tools-extra/trunk/clangd/CompileArgsCache.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CompileArgsCache.cpp?rev=323423&view=auto
==============================================================================
--- clang-tools-extra/trunk/clangd/CompileArgsCache.cpp (added)
+++ clang-tools-extra/trunk/clangd/CompileArgsCache.cpp Thu Jan 25 06:29:29 2018
@@ -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

Added: clang-tools-extra/trunk/clangd/CompileArgsCache.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CompileArgsCache.h?rev=323423&view=auto
==============================================================================
--- clang-tools-extra/trunk/clangd/CompileArgsCache.h (added)
+++ clang-tools-extra/trunk/clangd/CompileArgsCache.h Thu Jan 25 06:29:29 2018
@@ -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




More information about the cfe-commits mailing list