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 { |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 21 | return tooling::CompileCommand(llvm::sys::path::parent_path(File), |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 22 | llvm::sys::path::filename(File), |
| 23 | {"clang", File.str()}, |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 24 | /*Output=*/""); |
| 25 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 26 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 27 | DirectoryBasedGlobalCompilationDatabase:: |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 28 | DirectoryBasedGlobalCompilationDatabase( |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 29 | llvm::Optional<Path> CompileCommandsDir) |
| 30 | : CompileCommandsDir(std::move(CompileCommandsDir)) {} |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 31 | |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 32 | llvm::Optional<tooling::CompileCommand> |
| 33 | DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const { |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 34 | if (auto CDB = getCDBForFile(File)) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 35 | auto Candidates = CDB->getCompileCommands(File); |
| 36 | if (!Candidates.empty()) { |
| 37 | addExtraFlags(File, Candidates.front()); |
| 38 | return std::move(Candidates.front()); |
| 39 | } |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 40 | } else { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 41 | log("Failed to find compilation database for " + Twine(File)); |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 42 | } |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 43 | return llvm::None; |
| 44 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 45 | |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 46 | tooling::CompileCommand |
| 47 | DirectoryBasedGlobalCompilationDatabase::getFallbackCommand( |
| 48 | PathRef File) const { |
| 49 | auto C = GlobalCompilationDatabase::getFallbackCommand(File); |
| 50 | addExtraFlags(File, C); |
| 51 | return C; |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 54 | void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) { |
| 55 | std::lock_guard<std::mutex> Lock(Mutex); |
| 56 | CompileCommandsDir = P; |
| 57 | CompilationDatabases.clear(); |
| 58 | } |
| 59 | |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 60 | void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile( |
| 61 | PathRef File, std::vector<std::string> ExtraFlags) { |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 62 | std::lock_guard<std::mutex> Lock(Mutex); |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 63 | ExtraFlagsForFile[File] = std::move(ExtraFlags); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Sam McCall | ecbeab0 | 2017-12-04 10:08:45 +0000 | [diff] [blame] | 66 | void DirectoryBasedGlobalCompilationDatabase::addExtraFlags( |
| 67 | PathRef File, tooling::CompileCommand &C) const { |
| 68 | std::lock_guard<std::mutex> Lock(Mutex); |
| 69 | |
| 70 | auto It = ExtraFlagsForFile.find(File); |
| 71 | if (It == ExtraFlagsForFile.end()) |
| 72 | return; |
| 73 | |
| 74 | auto &Args = C.CommandLine; |
| 75 | assert(Args.size() >= 2 && "Expected at least [compiler, source file]"); |
| 76 | // The last argument of CommandLine is the name of the input file. |
| 77 | // Add ExtraFlags before it. |
| 78 | Args.insert(Args.end() - 1, It->second.begin(), It->second.end()); |
| 79 | } |
| 80 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 81 | tooling::CompilationDatabase * |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 82 | DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { |
| 83 | // FIXME(ibiryukov): Invalidate cached compilation databases on changes |
| 84 | auto CachedIt = CompilationDatabases.find(Dir); |
| 85 | if (CachedIt != CompilationDatabases.end()) |
| 86 | return CachedIt->second.get(); |
| 87 | std::string Error = ""; |
| 88 | auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); |
Sam McCall | 307c483 | 2018-04-09 15:22:08 +0000 | [diff] [blame^] | 89 | if (CDB) |
| 90 | CDB = tooling::inferMissingCompileCommands(std::move(CDB)); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 91 | auto Result = CDB.get(); |
| 92 | CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB))); |
| 93 | return Result; |
| 94 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 95 | |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 96 | tooling::CompilationDatabase * |
| 97 | DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 98 | namespace path = llvm::sys::path; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 99 | assert((path::is_absolute(File, path::Style::posix) || |
| 100 | path::is_absolute(File, path::Style::windows)) && |
| 101 | "path must be absolute"); |
| 102 | |
Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 103 | std::lock_guard<std::mutex> Lock(Mutex); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 104 | if (CompileCommandsDir) |
| 105 | return getCDBInDirLocked(*CompileCommandsDir); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 106 | for (auto Path = path::parent_path(File); !Path.empty(); |
Sam McCall | c02ba72 | 2017-12-22 09:47:34 +0000 | [diff] [blame] | 107 | Path = path::parent_path(Path)) |
| 108 | if (auto CDB = getCDBInDirLocked(Path)) |
| 109 | return CDB; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 110 | return nullptr; |
| 111 | } |
Krasimir Georgiev | c2a16a3 | 2017-07-06 08:44:54 +0000 | [diff] [blame] | 112 | |
| 113 | } // namespace clangd |
| 114 | } // namespace clang |