Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 1 | //===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===// |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +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 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 9 | |
| 10 | #include "GlobalCompilationDatabase.h" |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 11 | #include "Logger.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 12 | #include "clang/Tooling/CompilationDatabase.h" |
| 13 | #include "llvm/Support/FileSystem.h" |
| 14 | #include "llvm/Support/Path.h" |
| 15 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 16 | using namespace llvm; |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 17 | namespace clang { |
| 18 | namespace clangd { |
| 19 | |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 20 | tooling::CompileCommand |
| 21 | GlobalCompilationDatabase::getFallbackCommand(PathRef File) const { |
Sam McCall | 690dcf1 | 2018-04-20 11:35:17 +0000 | [diff] [blame] | 22 | std::vector<std::string> Argv = {"clang"}; |
| 23 | // Clang treats .h files as C by default, resulting in unhelpful diagnostics. |
| 24 | // Parsing as Objective C++ is friendly to more cases. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 25 | if (sys::path::extension(File) == ".h") |
Sam McCall | 690dcf1 | 2018-04-20 11:35:17 +0000 | [diff] [blame] | 26 | Argv.push_back("-xobjective-c++-header"); |
| 27 | Argv.push_back(File); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 28 | return tooling::CompileCommand(sys::path::parent_path(File), |
| 29 | sys::path::filename(File), std::move(Argv), |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 30 | /*Output=*/""); |
| 31 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 32 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 33 | DirectoryBasedGlobalCompilationDatabase:: |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 34 | DirectoryBasedGlobalCompilationDatabase(Optional<Path> CompileCommandsDir) |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 35 | : CompileCommandsDir(std::move(CompileCommandsDir)) {} |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 36 | |
Sam McCall | 690dcf1 | 2018-04-20 11:35:17 +0000 | [diff] [blame] | 37 | DirectoryBasedGlobalCompilationDatabase:: |
| 38 | ~DirectoryBasedGlobalCompilationDatabase() = default; |
| 39 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 40 | Optional<tooling::CompileCommand> |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 41 | DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const { |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 42 | if (auto CDB = getCDBForFile(File)) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 43 | auto Candidates = CDB->getCompileCommands(File); |
| 44 | if (!Candidates.empty()) { |
| 45 | addExtraFlags(File, Candidates.front()); |
| 46 | return std::move(Candidates.front()); |
| 47 | } |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 48 | } else { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 49 | log("Failed to find compilation database for {0}", File); |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 50 | } |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 51 | return None; |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 52 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 53 | |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 54 | tooling::CompileCommand |
| 55 | DirectoryBasedGlobalCompilationDatabase::getFallbackCommand( |
| 56 | PathRef File) const { |
| 57 | auto C = GlobalCompilationDatabase::getFallbackCommand(File); |
| 58 | addExtraFlags(File, C); |
| 59 | return C; |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 62 | void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) { |
| 63 | std::lock_guard<std::mutex> Lock(Mutex); |
| 64 | CompileCommandsDir = P; |
| 65 | CompilationDatabases.clear(); |
| 66 | } |
| 67 | |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 68 | void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile( |
| 69 | PathRef File, std::vector<std::string> ExtraFlags) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 70 | std::lock_guard<std::mutex> Lock(Mutex); |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 71 | ExtraFlagsForFile[File] = std::move(ExtraFlags); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 74 | void DirectoryBasedGlobalCompilationDatabase::addExtraFlags( |
| 75 | PathRef File, tooling::CompileCommand &C) const { |
| 76 | std::lock_guard<std::mutex> Lock(Mutex); |
| 77 | |
| 78 | auto It = ExtraFlagsForFile.find(File); |
| 79 | if (It == ExtraFlagsForFile.end()) |
| 80 | return; |
| 81 | |
| 82 | auto &Args = C.CommandLine; |
| 83 | assert(Args.size() >= 2 && "Expected at least [compiler, source file]"); |
| 84 | // The last argument of CommandLine is the name of the input file. |
| 85 | // Add ExtraFlags before it. |
| 86 | Args.insert(Args.end() - 1, It->second.begin(), It->second.end()); |
| 87 | } |
| 88 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 89 | tooling::CompilationDatabase * |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 90 | DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { |
| 91 | // FIXME(ibiryukov): Invalidate cached compilation databases on changes |
| 92 | auto CachedIt = CompilationDatabases.find(Dir); |
| 93 | if (CachedIt != CompilationDatabases.end()) |
| 94 | return CachedIt->second.get(); |
| 95 | std::string Error = ""; |
| 96 | auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); |
| 97 | auto Result = CDB.get(); |
| 98 | CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB))); |
| 99 | return Result; |
| 100 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 101 | |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 102 | tooling::CompilationDatabase * |
| 103 | DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 104 | namespace path = sys::path; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 105 | assert((path::is_absolute(File, path::Style::posix) || |
| 106 | path::is_absolute(File, path::Style::windows)) && |
| 107 | "path must be absolute"); |
| 108 | |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 109 | std::lock_guard<std::mutex> Lock(Mutex); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 110 | if (CompileCommandsDir) |
| 111 | return getCDBInDirLocked(*CompileCommandsDir); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 112 | for (auto Path = path::parent_path(File); !Path.empty(); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 113 | Path = path::parent_path(Path)) |
| 114 | if (auto CDB = getCDBInDirLocked(Path)) |
| 115 | return CDB; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 116 | return nullptr; |
| 117 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 118 | |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 119 | CachingCompilationDb::CachingCompilationDb( |
| 120 | const GlobalCompilationDatabase &InnerCDB) |
| 121 | : InnerCDB(InnerCDB) {} |
| 122 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 123 | Optional<tooling::CompileCommand> |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 124 | CachingCompilationDb::getCompileCommand(PathRef File) const { |
| 125 | std::unique_lock<std::mutex> Lock(Mut); |
| 126 | auto It = Cached.find(File); |
| 127 | if (It != Cached.end()) |
| 128 | return It->second; |
| 129 | |
| 130 | Lock.unlock(); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 131 | Optional<tooling::CompileCommand> Command = InnerCDB.getCompileCommand(File); |
Ilya Biryukov | b10ef47 | 2018-06-13 09:20:41 +0000 | [diff] [blame] | 132 | Lock.lock(); |
| 133 | return Cached.try_emplace(File, std::move(Command)).first->getValue(); |
| 134 | } |
| 135 | |
| 136 | tooling::CompileCommand |
| 137 | CachingCompilationDb::getFallbackCommand(PathRef File) const { |
| 138 | return InnerCDB.getFallbackCommand(File); |
| 139 | } |
| 140 | |
| 141 | void CachingCompilationDb::invalidate(PathRef File) { |
| 142 | std::unique_lock<std::mutex> Lock(Mut); |
| 143 | Cached.erase(File); |
| 144 | } |
| 145 | |
| 146 | void CachingCompilationDb::clear() { |
| 147 | std::unique_lock<std::mutex> Lock(Mut); |
| 148 | Cached.clear(); |
| 149 | } |
| 150 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame^] | 151 | Optional<tooling::CompileCommand> |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 152 | InMemoryCompilationDb::getCompileCommand(PathRef File) const { |
| 153 | std::lock_guard<std::mutex> Lock(Mutex); |
| 154 | auto It = Commands.find(File); |
| 155 | if (It == Commands.end()) |
| 156 | return None; |
| 157 | return It->second; |
| 158 | } |
| 159 | |
| 160 | bool InMemoryCompilationDb::setCompilationCommandForFile( |
| 161 | PathRef File, tooling::CompileCommand CompilationCommand) { |
| 162 | std::unique_lock<std::mutex> Lock(Mutex); |
| 163 | auto ItInserted = Commands.insert(std::make_pair(File, CompilationCommand)); |
| 164 | if (ItInserted.second) |
| 165 | return true; |
| 166 | ItInserted.first->setValue(std::move(CompilationCommand)); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | void InMemoryCompilationDb::invalidate(PathRef File) { |
| 171 | std::unique_lock<std::mutex> Lock(Mutex); |
| 172 | Commands.erase(File); |
| 173 | } |
| 174 | |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 175 | } // namespace clangd |
| 176 | } // namespace clang |