blob: 42167e56bc3e291b8490a64c0385f574cc5b9e4d [file] [log] [blame]
Ilya Biryukov3ef08a92018-01-25 14:30:46 +00001//===--- CompileArgsCache.cpp --------------------------------------------===//
Ilya Biryukov0d05e2d2018-01-25 14:29:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===---------------------------------------------------------------------===//
9
10#include "CompileArgsCache.h"
11
12namespace clang {
13namespace clangd {
14namespace {
15tooling::CompileCommand getCompileCommand(GlobalCompilationDatabase &CDB,
16 PathRef File, PathRef ResourceDir) {
17 llvm::Optional<tooling::CompileCommand> C = CDB.getCompileCommand(File);
18 if (!C) // FIXME: Suppress diagnostics? Let the user know?
19 C = CDB.getFallbackCommand(File);
20
21 // Inject the resource dir.
22 // FIXME: Don't overwrite it if it's already there.
23 C->CommandLine.push_back("-resource-dir=" + ResourceDir.str());
24 return std::move(*C);
25}
26} // namespace
27
28CompileArgsCache::CompileArgsCache(GlobalCompilationDatabase &CDB,
29 Path ResourceDir)
30 : CDB(CDB), ResourceDir(std::move(ResourceDir)) {}
31
32tooling::CompileCommand CompileArgsCache::getCompileCommand(PathRef File) {
33 auto It = Cached.find(File);
34 if (It == Cached.end()) {
35 It = Cached.insert({File, clangd::getCompileCommand(CDB, File, ResourceDir)})
36 .first;
37 }
38 return It->second;
39}
40
41void CompileArgsCache::invalidate(PathRef File) { Cached.erase(File); }
42
43} // namespace clangd
44} // namespace clang