| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H |
| 11 |
|
| 12 |
#include "support/Function.h" |
| 13 |
#include "support/Path.h" |
| 14 |
#include "support/Threading.h" |
| 15 |
#include "support/ThreadsafeFS.h" |
| 16 |
#include "clang/Tooling/ArgumentsAdjusters.h" |
| 17 |
#include "clang/Tooling/CompilationDatabase.h" |
| 18 |
#include "llvm/ADT/FunctionExtras.h" |
| 19 |
#include "llvm/ADT/StringMap.h" |
| 20 |
#include <memory> |
| 21 |
#include <mutex> |
| 22 |
#include <optional> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
namespace clang { |
| 26 |
namespace clangd { |
| 27 |
|
| 28 |
struct ProjectInfo { |
| 29 |
|
| 30 |
|
| 31 |
std::string SourceRoot; |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
class GlobalCompilationDatabase { |
| 36 |
public: |
| 37 |
virtual ~GlobalCompilationDatabase() = default; |
| 38 |
|
| 39 |
|
| 40 |
virtual std::optional<tooling::CompileCommand> |
| 41 |
getCompileCommand(PathRef File) const = 0; |
| 42 |
|
| 43 |
|
| 44 |
virtual std::optional<ProjectInfo> getProjectInfo(PathRef File) const { |
| 45 |
return std::nullopt; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
virtual bool blockUntilIdle(Deadline D) const { return true; } |
| 56 |
|
| 57 |
using CommandChanged = Event<std::vector<std::string>>; |
| 58 |
|
| 59 |
|
| 60 |
CommandChanged::Subscription watch(CommandChanged::Listener L) const { |
| 61 |
return OnCommandChanged.observe(std::move(L)); |
| 62 |
} |
| 63 |
|
| 64 |
protected: |
| 65 |
mutable CommandChanged OnCommandChanged; |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
class DelegatingCDB : public GlobalCompilationDatabase { |
| 70 |
public: |
| 71 |
DelegatingCDB(const GlobalCompilationDatabase *Base); |
| 72 |
DelegatingCDB(std::unique_ptr<GlobalCompilationDatabase> Base); |
| 73 |
|
| 74 |
std::optional<tooling::CompileCommand> |
| 75 |
getCompileCommand(PathRef File) const override; |
| 76 |
|
| 77 |
std::optional<ProjectInfo> getProjectInfo(PathRef File) const override; |
| 78 |
|
| 79 |
tooling::CompileCommand getFallbackCommand(PathRef File) const override; |
| 80 |
|
| 81 |
bool blockUntilIdle(Deadline D) const override; |
| 82 |
|
| 83 |
private: |
| 84 |
const GlobalCompilationDatabase *Base; |
| 85 |
std::unique_ptr<GlobalCompilationDatabase> BaseOwner; |
| 86 |
CommandChanged::Subscription BaseChanged; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
class DirectoryBasedGlobalCompilationDatabase |
| 92 |
: public GlobalCompilationDatabase { |
| 93 |
public: |
| 94 |
struct Options { |
| 95 |
Options(const ThreadsafeFS &TFS) : TFS(TFS) {} |
| 96 |
|
| 97 |
const ThreadsafeFS &TFS; |
| 98 |
|
| 99 |
std::chrono::steady_clock::duration RevalidateAfter = |
| 100 |
std::chrono::seconds(5); |
| 101 |
|
| 102 |
|
| 103 |
std::chrono::steady_clock::duration RevalidateMissingAfter = |
| 104 |
std::chrono::seconds(30); |
| 105 |
|
| 106 |
std::function<Context(llvm::StringRef)> ContextProvider; |
| 107 |
|
| 108 |
|
| 109 |
std::optional<Path> CompileCommandsDir; |
| 110 |
}; |
| 111 |
|
| 112 |
DirectoryBasedGlobalCompilationDatabase(const Options &Opts); |
| 113 |
~DirectoryBasedGlobalCompilationDatabase() override; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
std::optional<tooling::CompileCommand> |
| 119 |
getCompileCommand(PathRef File) const override; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
std::optional<ProjectInfo> getProjectInfo(PathRef File) const override; |
| 124 |
|
| 125 |
bool blockUntilIdle(Deadline Timeout) const override; |
| 126 |
|
| 127 |
private: |
| 128 |
Options Opts; |
| 129 |
|
| 130 |
class DirectoryCache; |
| 131 |
|
| 132 |
|
| 133 |
mutable llvm::StringMap<DirectoryCache> DirCaches; |
| 134 |
mutable std::mutex DirCachesMutex; |
| 135 |
|
| 136 |
std::vector<DirectoryCache *> |
| 137 |
getDirectoryCaches(llvm::ArrayRef<llvm::StringRef> Dirs) const; |
| 138 |
|
| 139 |
struct CDBLookupRequest { |
| 140 |
PathRef FileName; |
| 141 |
|
| 142 |
bool ShouldBroadcast = false; |
| 143 |
|
| 144 |
|
| 145 |
std::chrono::steady_clock::time_point FreshTime; |
| 146 |
std::chrono::steady_clock::time_point FreshTimeMissing; |
| 147 |
}; |
| 148 |
struct CDBLookupResult { |
| 149 |
std::shared_ptr<const tooling::CompilationDatabase> CDB; |
| 150 |
ProjectInfo PI; |
| 151 |
}; |
| 152 |
std::optional<CDBLookupResult> lookupCDB(CDBLookupRequest Request) const; |
| 153 |
|
| 154 |
class BroadcastThread; |
| 155 |
std::unique_ptr<BroadcastThread> Broadcaster; |
| 156 |
|
| 157 |
|
| 158 |
void broadcastCDB(CDBLookupResult Res) const; |
| 159 |
|
| 160 |
|
| 161 |
friend class DirectoryBasedGlobalCompilationDatabaseCacheTest; |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
using SystemIncludeExtractorFn = llvm::unique_function<void( |
| 168 |
tooling::CompileCommand &, llvm::StringRef) const>; |
| 169 |
SystemIncludeExtractorFn |
| 170 |
getSystemIncludeExtractor(llvm::ArrayRef<std::string> QueryDriverGlobs); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
class OverlayCDB : public DelegatingCDB { |
| 175 |
public: |
| 176 |
|
| 177 |
|
| 178 |
using CommandMangler = llvm::unique_function<void(tooling::CompileCommand &, |
| 179 |
StringRef File) const>; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
OverlayCDB(const GlobalCompilationDatabase *Base, |
| 185 |
std::vector<std::string> FallbackFlags = {}, |
| 186 |
CommandMangler Mangler = nullptr); |
| 187 |
|
| 188 |
std::optional<tooling::CompileCommand> |
| 189 |
getCompileCommand(PathRef File) const override; |
| 190 |
tooling::CompileCommand getFallbackCommand(PathRef File) const override; |
| 191 |
|
| 192 |
|
| 193 |
void |
| 194 |
setCompileCommand(PathRef File, |
| 195 |
std::optional<tooling::CompileCommand> CompilationCommand); |
| 196 |
|
| 197 |
private: |
| 198 |
mutable std::mutex Mutex; |
| 199 |
llvm::StringMap<tooling::CompileCommand> Commands; |
| 200 |
CommandMangler Mangler; |
| 201 |
std::vector<std::string> FallbackFlags; |
| 202 |
}; |
| 203 |
|
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
#endif |