Ilya Biryukov | 3ef08a9 | 2018-01-25 14:30:46 +0000 | [diff] [blame] | 1 | //===--- CompileArgsCache.cpp --------------------------------------------===// |
Ilya Biryukov | 0d05e2d | 2018-01-25 14:29:29 +0000 | [diff] [blame] | 2 | // |
| 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 | |
| 12 | namespace clang { |
| 13 | namespace clangd { |
| 14 | namespace { |
| 15 | tooling::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 | |
| 28 | CompileArgsCache::CompileArgsCache(GlobalCompilationDatabase &CDB, |
| 29 | Path ResourceDir) |
| 30 | : CDB(CDB), ResourceDir(std::move(ResourceDir)) {} |
| 31 | |
| 32 | tooling::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 | |
| 41 | void CompileArgsCache::invalidate(PathRef File) { Cached.erase(File); } |
| 42 | |
| 43 | } // namespace clangd |
| 44 | } // namespace clang |