summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/CompileArgsCache.cpp
blob: 42167e56bc3e291b8490a64c0385f574cc5b9e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//===--- CompileArgsCache.cpp --------------------------------------------===//
//
//                     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
OpenPOWER on IntegriCloud