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