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); |
Sam McCall | 2eb6b40 | 2018-11-02 13:06:55 +0000 | [diff] [blame] | 44 | if (!Candidates.empty()) |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 45 | return std::move(Candidates.front()); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 46 | } else { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 47 | log("Failed to find compilation database for {0}", File); |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 48 | } |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 49 | return None; |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 50 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 51 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 52 | tooling::CompilationDatabase * |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 53 | DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { |
| 54 | // FIXME(ibiryukov): Invalidate cached compilation databases on changes |
| 55 | auto CachedIt = CompilationDatabases.find(Dir); |
| 56 | if (CachedIt != CompilationDatabases.end()) |
| 57 | return CachedIt->second.get(); |
| 58 | std::string Error = ""; |
| 59 | auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); |
| 60 | auto Result = CDB.get(); |
| 61 | CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB))); |
| 62 | return Result; |
| 63 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 64 | |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 65 | tooling::CompilationDatabase * |
| 66 | DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 67 | namespace path = sys::path; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 68 | assert((path::is_absolute(File, path::Style::posix) || |
| 69 | path::is_absolute(File, path::Style::windows)) && |
| 70 | "path must be absolute"); |
| 71 | |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 72 | std::lock_guard<std::mutex> Lock(Mutex); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 73 | if (CompileCommandsDir) |
| 74 | return getCDBInDirLocked(*CompileCommandsDir); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 75 | for (auto Path = path::parent_path(File); !Path.empty(); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 76 | Path = path::parent_path(Path)) |
| 77 | if (auto CDB = getCDBInDirLocked(Path)) |
| 78 | return CDB; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 79 | return nullptr; |
| 80 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 81 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 82 | Optional<tooling::CompileCommand> |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame^] | 83 | OverlayCDB::getCompileCommand(PathRef File) const { |
| 84 | { |
| 85 | std::lock_guard<std::mutex> Lock(Mutex); |
| 86 | auto It = Commands.find(File); |
| 87 | if (It != Commands.end()) |
| 88 | return It->second; |
| 89 | } |
| 90 | return Base ? Base->getCompileCommand(File) : None; |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame^] | 93 | tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const { |
| 94 | auto Cmd = Base ? Base->getFallbackCommand(File) |
| 95 | : GlobalCompilationDatabase::getFallbackCommand(File); |
| 96 | std::lock_guard<std::mutex> Lock(Mutex); |
| 97 | Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(), |
| 98 | FallbackFlags.end()); |
| 99 | return Cmd; |
| 100 | } |
| 101 | |
| 102 | void OverlayCDB::setCompileCommand( |
| 103 | PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) { |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 104 | std::unique_lock<std::mutex> Lock(Mutex); |
Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame^] | 105 | if (Cmd) |
| 106 | Commands[File] = std::move(*Cmd); |
| 107 | else |
| 108 | Commands.erase(File); |
Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 111 | } // namespace clangd |
| 112 | } // namespace clang |