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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 6 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 8 | |
| 9 | #include "GlobalCompilationDatabase.h" |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 10 | #include "Logger.h" |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 11 | #include "clang/Frontend/CompilerInvocation.h" |
| 12 | #include "clang/Tooling/ArgumentsAdjusters.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 13 | #include "clang/Tooling/CompilationDatabase.h" |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Optional.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 15 | #include "llvm/Support/FileSystem.h" |
| 16 | #include "llvm/Support/Path.h" |
| 17 | |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 18 | namespace clang { |
| 19 | namespace clangd { |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | void adjustArguments(tooling::CompileCommand &Cmd, |
| 23 | llvm::StringRef ResourceDir) { |
| 24 | // Strip plugin related command line arguments. Clangd does |
| 25 | // not support plugins currently. Therefore it breaks if |
| 26 | // compiler tries to load plugins. |
| 27 | Cmd.CommandLine = |
| 28 | tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename); |
| 29 | // Inject the resource dir. |
| 30 | // FIXME: Don't overwrite it if it's already there. |
| 31 | if (!ResourceDir.empty()) |
| 32 | Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str()); |
| 33 | } |
| 34 | |
| 35 | std::string getStandardResourceDir() { |
| 36 | static int Dummy; // Just an address in this process. |
| 37 | return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy); |
| 38 | } |
| 39 | |
| 40 | } // namespace |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 41 | |
Haojian Wu | e1ced5c | 2019-01-07 12:35:02 +0000 | [diff] [blame] | 42 | static std::string getFallbackClangPath() { |
| 43 | static int Dummy; |
| 44 | std::string ClangdExecutable = |
| 45 | llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy); |
| 46 | SmallString<128> ClangPath; |
| 47 | ClangPath = llvm::sys::path::parent_path(ClangdExecutable); |
| 48 | llvm::sys::path::append(ClangPath, "clang"); |
| 49 | return ClangPath.str(); |
| 50 | } |
| 51 | |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 52 | tooling::CompileCommand |
| 53 | GlobalCompilationDatabase::getFallbackCommand(PathRef File) const { |
Haojian Wu | e1ced5c | 2019-01-07 12:35:02 +0000 | [diff] [blame] | 54 | std::vector<std::string> Argv = {getFallbackClangPath()}; |
Sam McCall | 690dcf1 | 2018-04-20 11:35:17 +0000 | [diff] [blame] | 55 | // Clang treats .h files as C by default, resulting in unhelpful diagnostics. |
| 56 | // Parsing as Objective C++ is friendly to more cases. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 57 | if (llvm::sys::path::extension(File) == ".h") |
Sam McCall | 690dcf1 | 2018-04-20 11:35:17 +0000 | [diff] [blame] | 58 | Argv.push_back("-xobjective-c++-header"); |
| 59 | Argv.push_back(File); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 60 | return tooling::CompileCommand(llvm::sys::path::parent_path(File), |
| 61 | llvm::sys::path::filename(File), |
| 62 | std::move(Argv), |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 63 | /*Output=*/""); |
| 64 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 65 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 66 | DirectoryBasedGlobalCompilationDatabase:: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 67 | DirectoryBasedGlobalCompilationDatabase( |
| 68 | llvm::Optional<Path> CompileCommandsDir) |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 69 | : CompileCommandsDir(std::move(CompileCommandsDir)) {} |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 70 | |
Sam McCall | 690dcf1 | 2018-04-20 11:35:17 +0000 | [diff] [blame] | 71 | DirectoryBasedGlobalCompilationDatabase:: |
| 72 | ~DirectoryBasedGlobalCompilationDatabase() = default; |
| 73 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 74 | llvm::Optional<tooling::CompileCommand> |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 75 | DirectoryBasedGlobalCompilationDatabase::getCompileCommand( |
| 76 | PathRef File, ProjectInfo *Project) const { |
| 77 | if (auto CDB = getCDBForFile(File, Project)) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 78 | auto Candidates = CDB->getCompileCommands(File); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 79 | if (!Candidates.empty()) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 80 | return std::move(Candidates.front()); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 81 | } |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 82 | } else { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 83 | log("Failed to find compilation database for {0}", File); |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 84 | } |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 85 | return None; |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 86 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 87 | |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 88 | std::pair<tooling::CompilationDatabase *, /*Cached*/ bool> |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 89 | DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { |
| 90 | // FIXME(ibiryukov): Invalidate cached compilation databases on changes |
| 91 | auto CachedIt = CompilationDatabases.find(Dir); |
| 92 | if (CachedIt != CompilationDatabases.end()) |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 93 | return {CachedIt->second.get(), true}; |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 94 | std::string Error = ""; |
| 95 | auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); |
| 96 | auto Result = CDB.get(); |
| 97 | CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB))); |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 98 | return {Result, false}; |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 99 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 100 | |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 101 | tooling::CompilationDatabase * |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 102 | DirectoryBasedGlobalCompilationDatabase::getCDBForFile( |
| 103 | PathRef File, ProjectInfo *Project) const { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 104 | namespace path = llvm::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 | |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 109 | tooling::CompilationDatabase *CDB = nullptr; |
| 110 | bool Cached = false; |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 111 | std::lock_guard<std::mutex> Lock(Mutex); |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 112 | if (CompileCommandsDir) { |
| 113 | std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 114 | if (Project && CDB) |
| 115 | Project->SourceRoot = *CompileCommandsDir; |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 116 | } else { |
| 117 | for (auto Path = path::parent_path(File); !CDB && !Path.empty(); |
| 118 | Path = path::parent_path(Path)) { |
| 119 | std::tie(CDB, Cached) = getCDBInDirLocked(Path); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 120 | if (Project && CDB) |
| 121 | Project->SourceRoot = Path; |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame] | 124 | // FIXME: getAllFiles() may return relative paths, we need absolute paths. |
| 125 | // Hopefully the fix is to change JSONCompilationDatabase and the interface. |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 126 | if (CDB && !Cached) |
| 127 | OnCommandChanged.broadcast(CDB->getAllFiles()); |
| 128 | return CDB; |
| 129 | } |
| 130 | |
| 131 | OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base, |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 132 | std::vector<std::string> FallbackFlags, |
| 133 | llvm::Optional<std::string> ResourceDir) |
| 134 | : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir) |
| 135 | : getStandardResourceDir()), |
| 136 | FallbackFlags(std::move(FallbackFlags)) { |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 137 | if (Base) |
| 138 | BaseChanged = Base->watch([this](const std::vector<std::string> Changes) { |
| 139 | OnCommandChanged.broadcast(Changes); |
| 140 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 141 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 142 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 143 | llvm::Optional<tooling::CompileCommand> |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 144 | OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const { |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 145 | llvm::Optional<tooling::CompileCommand> Cmd; |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 146 | { |
| 147 | std::lock_guard<std::mutex> Lock(Mutex); |
| 148 | auto It = Commands.find(File); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 149 | if (It != Commands.end()) { |
| 150 | if (Project) |
| 151 | Project->SourceRoot = ""; |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 152 | Cmd = It->second; |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 153 | } |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 154 | } |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 155 | if (!Cmd && Base) |
| 156 | Cmd = Base->getCompileCommand(File, Project); |
| 157 | if (!Cmd) |
| 158 | return llvm::None; |
| 159 | adjustArguments(*Cmd, ResourceDir); |
| 160 | return Cmd; |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 163 | tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const { |
| 164 | auto Cmd = Base ? Base->getFallbackCommand(File) |
| 165 | : GlobalCompilationDatabase::getFallbackCommand(File); |
| 166 | std::lock_guard<std::mutex> Lock(Mutex); |
| 167 | Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(), |
| 168 | FallbackFlags.end()); |
| 169 | return Cmd; |
| 170 | } |
| 171 | |
| 172 | void OverlayCDB::setCompileCommand( |
| 173 | PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) { |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 174 | { |
| 175 | std::unique_lock<std::mutex> Lock(Mutex); |
| 176 | if (Cmd) |
| 177 | Commands[File] = std::move(*Cmd); |
| 178 | else |
| 179 | Commands.erase(File); |
| 180 | } |
| 181 | OnCommandChanged.broadcast({File}); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 184 | } // namespace clangd |
| 185 | } // namespace clang |