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