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); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 66 | return tooling::CompileCommand(llvm::sys::path::parent_path(File), |
| 67 | llvm::sys::path::filename(File), |
| 68 | std::move(Argv), |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 69 | /*Output=*/""); |
| 70 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 71 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 72 | DirectoryBasedGlobalCompilationDatabase:: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 73 | DirectoryBasedGlobalCompilationDatabase( |
| 74 | llvm::Optional<Path> CompileCommandsDir) |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 75 | : CompileCommandsDir(std::move(CompileCommandsDir)) {} |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 76 | |
Sam McCall | 690dcf1 | 2018-04-20 11:35:17 +0000 | [diff] [blame] | 77 | DirectoryBasedGlobalCompilationDatabase:: |
| 78 | ~DirectoryBasedGlobalCompilationDatabase() = default; |
| 79 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 80 | llvm::Optional<tooling::CompileCommand> |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 81 | DirectoryBasedGlobalCompilationDatabase::getCompileCommand( |
| 82 | PathRef File, ProjectInfo *Project) const { |
| 83 | if (auto CDB = getCDBForFile(File, Project)) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 84 | auto Candidates = CDB->getCompileCommands(File); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 85 | if (!Candidates.empty()) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 86 | return std::move(Candidates.front()); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 87 | } |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 88 | } else { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 89 | log("Failed to find compilation database for {0}", File); |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 90 | } |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 91 | return None; |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 92 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 93 | |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 94 | std::pair<tooling::CompilationDatabase *, /*Cached*/ bool> |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 95 | DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { |
| 96 | // FIXME(ibiryukov): Invalidate cached compilation databases on changes |
| 97 | auto CachedIt = CompilationDatabases.find(Dir); |
| 98 | if (CachedIt != CompilationDatabases.end()) |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 99 | return {CachedIt->second.get(), true}; |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 100 | std::string Error = ""; |
| 101 | auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); |
| 102 | auto Result = CDB.get(); |
| 103 | CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB))); |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 104 | return {Result, false}; |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 105 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 106 | |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 107 | tooling::CompilationDatabase * |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 108 | DirectoryBasedGlobalCompilationDatabase::getCDBForFile( |
| 109 | PathRef File, ProjectInfo *Project) const { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 110 | namespace path = llvm::sys::path; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 111 | assert((path::is_absolute(File, path::Style::posix) || |
| 112 | path::is_absolute(File, path::Style::windows)) && |
| 113 | "path must be absolute"); |
| 114 | |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 115 | tooling::CompilationDatabase *CDB = nullptr; |
| 116 | bool Cached = false; |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 117 | std::lock_guard<std::mutex> Lock(Mutex); |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 118 | if (CompileCommandsDir) { |
| 119 | std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 120 | if (Project && CDB) |
| 121 | Project->SourceRoot = *CompileCommandsDir; |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 122 | } else { |
| 123 | for (auto Path = path::parent_path(File); !CDB && !Path.empty(); |
| 124 | Path = path::parent_path(Path)) { |
| 125 | std::tie(CDB, Cached) = getCDBInDirLocked(Path); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 126 | if (Project && CDB) |
| 127 | Project->SourceRoot = Path; |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame] | 130 | // FIXME: getAllFiles() may return relative paths, we need absolute paths. |
| 131 | // Hopefully the fix is to change JSONCompilationDatabase and the interface. |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 132 | if (CDB && !Cached) |
| 133 | OnCommandChanged.broadcast(CDB->getAllFiles()); |
| 134 | return CDB; |
| 135 | } |
| 136 | |
| 137 | OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base, |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 138 | std::vector<std::string> FallbackFlags, |
| 139 | llvm::Optional<std::string> ResourceDir) |
| 140 | : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir) |
| 141 | : getStandardResourceDir()), |
| 142 | FallbackFlags(std::move(FallbackFlags)) { |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 143 | if (Base) |
| 144 | BaseChanged = Base->watch([this](const std::vector<std::string> Changes) { |
| 145 | OnCommandChanged.broadcast(Changes); |
| 146 | }); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 147 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 148 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 149 | llvm::Optional<tooling::CompileCommand> |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 150 | OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const { |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 151 | llvm::Optional<tooling::CompileCommand> Cmd; |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 152 | { |
| 153 | std::lock_guard<std::mutex> Lock(Mutex); |
| 154 | auto It = Commands.find(File); |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 155 | if (It != Commands.end()) { |
| 156 | if (Project) |
| 157 | Project->SourceRoot = ""; |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 158 | Cmd = It->second; |
Sam McCall | 6e2d2a3 | 2018-11-26 09:51:50 +0000 | [diff] [blame] | 159 | } |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 160 | } |
Kadir Cetinkaya | be6b35d | 2019-01-22 09:10:20 +0000 | [diff] [blame] | 161 | if (!Cmd && Base) |
| 162 | Cmd = Base->getCompileCommand(File, Project); |
| 163 | if (!Cmd) |
| 164 | return llvm::None; |
| 165 | adjustArguments(*Cmd, ResourceDir); |
| 166 | return Cmd; |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 169 | tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const { |
| 170 | auto Cmd = Base ? Base->getFallbackCommand(File) |
| 171 | : GlobalCompilationDatabase::getFallbackCommand(File); |
| 172 | std::lock_guard<std::mutex> Lock(Mutex); |
| 173 | Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(), |
| 174 | FallbackFlags.end()); |
| 175 | return Cmd; |
| 176 | } |
| 177 | |
| 178 | void OverlayCDB::setCompileCommand( |
| 179 | PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) { |
Sam McCall | 2bebc3d | 2018-11-20 10:56:03 +0000 | [diff] [blame] | 180 | { |
| 181 | std::unique_lock<std::mutex> Lock(Mutex); |
| 182 | if (Cmd) |
| 183 | Commands[File] = std::move(*Cmd); |
| 184 | else |
| 185 | Commands.erase(File); |
| 186 | } |
| 187 | OnCommandChanged.broadcast({File}); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 190 | } // namespace clangd |
| 191 | } // namespace clang |