Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 1 | //===-- Background.cpp - Build an index in a background thread ------------===// |
| 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 "index/Background.h" |
| 11 | #include "ClangdUnit.h" |
| 12 | #include "Compiler.h" |
| 13 | #include "Logger.h" |
Kadir Cetinkaya | 6675be8 | 2018-10-30 12:13:27 +0000 | [diff] [blame] | 14 | #include "Threading.h" |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 15 | #include "Trace.h" |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 16 | #include "URI.h" |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 17 | #include "index/IndexAction.h" |
| 18 | #include "index/MemIndex.h" |
| 19 | #include "index/Serialization.h" |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 20 | #include "index/SymbolCollector.h" |
| 21 | #include "clang/Basic/SourceLocation.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
| 23 | #include "llvm/ADT/STLExtras.h" |
| 24 | #include "llvm/ADT/StringMap.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 26 | #include "llvm/Support/SHA1.h" |
Kadir Cetinkaya | cb8407c | 2018-11-15 10:31:15 +0000 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <queue> |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 29 | #include <random> |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 30 | #include <string> |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | namespace clang { |
| 34 | namespace clangd { |
| 35 | |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | static BackgroundIndex::FileDigest digest(StringRef Content) { |
| 39 | return SHA1::hash({(const uint8_t *)Content.data(), Content.size()}); |
| 40 | } |
| 41 | |
| 42 | static Optional<BackgroundIndex::FileDigest> digestFile(const SourceManager &SM, |
| 43 | FileID FID) { |
| 44 | bool Invalid = false; |
| 45 | StringRef Content = SM.getBufferData(FID, &Invalid); |
| 46 | if (Invalid) |
| 47 | return None; |
| 48 | return digest(Content); |
| 49 | } |
| 50 | |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 51 | std::string getShardPathFromFilePath(llvm::StringRef ShardRoot, |
| 52 | llvm::StringRef FilePath) { |
| 53 | llvm::SmallString<128> ShardRootSS(ShardRoot); |
| 54 | sys::path::append(ShardRootSS, sys::path::filename(FilePath) + |
| 55 | toHex(digest(FilePath)) + ".idx"); |
| 56 | return ShardRoot.str(); |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 59 | // Uses disk as a storage for index shards. Creates a directory called |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 60 | // ".clangd-index/" under the path provided during construction. |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 61 | // This class is thread-safe. |
| 62 | class DiskBackedIndexStorage : public BackgroundIndexStorage { |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 63 | std::string DiskShardRoot; |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 64 | |
| 65 | public: |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 66 | llvm::Expected<IndexFileIn> loadShard(llvm::StringRef ShardIdentifier) const { |
| 67 | const std::string ShardPath = |
| 68 | getShardPathFromFilePath(DiskShardRoot, ShardIdentifier); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 69 | auto Buffer = MemoryBuffer::getFile(ShardPath); |
| 70 | if (!Buffer) { |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 71 | elog("Couldn't load {0}: {1}", ShardPath, Buffer.getError().message()); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 72 | return llvm::make_error<llvm::StringError>(Buffer.getError()); |
| 73 | } |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 74 | return readIndexFile(Buffer->get()->getBuffer()); |
| 75 | } |
| 76 | |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 77 | llvm::Error storeShard(llvm::StringRef ShardIdentifier, |
| 78 | IndexFileOut Shard) const override { |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 79 | auto ShardPath = getShardPathFromFilePath(DiskShardRoot, ShardIdentifier); |
| 80 | std::error_code EC; |
| 81 | llvm::raw_fd_ostream OS(ShardPath, EC); |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 82 | if (EC) |
| 83 | return errorCodeToError(EC); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 84 | OS << Shard; |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 85 | return llvm::Error::success(); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 88 | // Sets DiskShardRoot to (Directory + ".clangd-index/") which is the base |
| 89 | // directory for all shard files. |
| 90 | DiskBackedIndexStorage(llvm::StringRef Directory) { |
| 91 | llvm::SmallString<128> CDBDirectory(Directory); |
| 92 | sys::path::append(CDBDirectory, ".clangd-index/"); |
| 93 | DiskShardRoot = CDBDirectory.str(); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 94 | if (!llvm::sys::fs::exists(DiskShardRoot)) { |
| 95 | std::error_code OK; |
| 96 | std::error_code EC = llvm::sys::fs::create_directory(DiskShardRoot); |
| 97 | if (EC != OK) { |
| 98 | elog("Failed to create {0}: {1}", DiskShardRoot, EC.message()); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | }; |
| 103 | |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 104 | std::string getAbsoluteFilePath(const tooling::CompileCommand &Cmd) { |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 105 | SmallString<128> AbsolutePath; |
| 106 | if (sys::path::is_absolute(Cmd.Filename)) { |
| 107 | AbsolutePath = Cmd.Filename; |
| 108 | } else { |
| 109 | AbsolutePath = Cmd.Directory; |
| 110 | sys::path::append(AbsolutePath, Cmd.Filename); |
| 111 | } |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 112 | return AbsolutePath.str(); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 115 | } // namespace |
| 116 | |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 117 | BackgroundIndex::BackgroundIndex(Context BackgroundContext, |
| 118 | StringRef ResourceDir, |
| 119 | const FileSystemProvider &FSProvider, |
| 120 | ArrayRef<std::string> URISchemes, |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 121 | IndexStorageFactory IndexStorageCreator, |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 122 | size_t ThreadPoolSize) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 123 | : SwapIndex(make_unique<MemIndex>()), ResourceDir(ResourceDir), |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 124 | FSProvider(FSProvider), BackgroundContext(std::move(BackgroundContext)), |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 125 | URISchemes(URISchemes), |
| 126 | IndexStorageCreator(std::move(IndexStorageCreator)) { |
Kadir Cetinkaya | 6675be8 | 2018-10-30 12:13:27 +0000 | [diff] [blame] | 127 | assert(ThreadPoolSize > 0 && "Thread pool size can't be zero."); |
| 128 | while (ThreadPoolSize--) { |
| 129 | ThreadPool.emplace_back([this] { run(); }); |
| 130 | // Set priority to low, since background indexing is a long running task we |
| 131 | // do not want to eat up cpu when there are any other high priority threads. |
| 132 | // FIXME: In the future we might want a more general way of handling this to |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 133 | // support tasks with various priorities. |
Kadir Cetinkaya | 6675be8 | 2018-10-30 12:13:27 +0000 | [diff] [blame] | 134 | setThreadPriority(ThreadPool.back(), ThreadPriority::Low); |
| 135 | } |
| 136 | } |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 137 | |
| 138 | BackgroundIndex::~BackgroundIndex() { |
| 139 | stop(); |
Kadir Cetinkaya | 6675be8 | 2018-10-30 12:13:27 +0000 | [diff] [blame] | 140 | for (auto &Thread : ThreadPool) |
| 141 | Thread.join(); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void BackgroundIndex::stop() { |
| 145 | { |
| 146 | std::lock_guard<std::mutex> Lock(QueueMu); |
| 147 | ShouldStop = true; |
| 148 | } |
| 149 | QueueCV.notify_all(); |
| 150 | } |
| 151 | |
| 152 | void BackgroundIndex::run() { |
Kadir Cetinkaya | 6675be8 | 2018-10-30 12:13:27 +0000 | [diff] [blame] | 153 | WithContext Background(BackgroundContext.clone()); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 154 | while (true) { |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 155 | Optional<Task> Task; |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 156 | { |
| 157 | std::unique_lock<std::mutex> Lock(QueueMu); |
| 158 | QueueCV.wait(Lock, [&] { return ShouldStop || !Queue.empty(); }); |
| 159 | if (ShouldStop) { |
| 160 | Queue.clear(); |
| 161 | QueueCV.notify_all(); |
| 162 | return; |
| 163 | } |
| 164 | ++NumActiveTasks; |
| 165 | Task = std::move(Queue.front()); |
| 166 | Queue.pop_front(); |
| 167 | } |
| 168 | (*Task)(); |
| 169 | { |
| 170 | std::unique_lock<std::mutex> Lock(QueueMu); |
| 171 | assert(NumActiveTasks > 0 && "before decrementing"); |
| 172 | --NumActiveTasks; |
| 173 | } |
| 174 | QueueCV.notify_all(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void BackgroundIndex::blockUntilIdleForTest() { |
| 179 | std::unique_lock<std::mutex> Lock(QueueMu); |
| 180 | QueueCV.wait(Lock, [&] { return Queue.empty() && NumActiveTasks == 0; }); |
| 181 | } |
| 182 | |
| 183 | void BackgroundIndex::enqueue(StringRef Directory, |
| 184 | tooling::CompileCommand Cmd) { |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 185 | BackgroundIndexStorage *IndexStorage = getIndexStorage(Directory); |
| 186 | if (!IndexStorage) |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 187 | elog("No index storage for: {0}", Directory); |
Sam McCall | bca624a | 2018-10-16 09:05:13 +0000 | [diff] [blame] | 188 | { |
| 189 | std::lock_guard<std::mutex> Lock(QueueMu); |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 190 | enqueueLocked(std::move(Cmd), IndexStorage); |
Sam McCall | bca624a | 2018-10-16 09:05:13 +0000 | [diff] [blame] | 191 | } |
| 192 | QueueCV.notify_all(); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void BackgroundIndex::enqueueAll(StringRef Directory, |
| 196 | const tooling::CompilationDatabase &CDB) { |
| 197 | trace::Span Tracer("BackgroundIndexEnqueueCDB"); |
| 198 | // FIXME: this function may be slow. Perhaps enqueue a task to re-read the CDB |
| 199 | // from disk and enqueue the commands asynchronously? |
| 200 | auto Cmds = CDB.getAllCompileCommands(); |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 201 | BackgroundIndexStorage *IndexStorage = getIndexStorage(Directory); |
| 202 | if (!IndexStorage) |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 203 | elog("No index storage for: {0}", Directory); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 204 | SPAN_ATTACH(Tracer, "commands", int64_t(Cmds.size())); |
| 205 | std::mt19937 Generator(std::random_device{}()); |
| 206 | std::shuffle(Cmds.begin(), Cmds.end(), Generator); |
| 207 | log("Enqueueing {0} commands for indexing from {1}", Cmds.size(), Directory); |
| 208 | { |
| 209 | std::lock_guard<std::mutex> Lock(QueueMu); |
| 210 | for (auto &Cmd : Cmds) |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 211 | enqueueLocked(std::move(Cmd), IndexStorage); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 212 | } |
| 213 | QueueCV.notify_all(); |
| 214 | } |
| 215 | |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 216 | void BackgroundIndex::enqueueLocked(tooling::CompileCommand Cmd, |
| 217 | BackgroundIndexStorage *IndexStorage) { |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 218 | Queue.push_back(Bind( |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 219 | [this, IndexStorage](tooling::CompileCommand Cmd) { |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 220 | std::string Filename = Cmd.Filename; |
| 221 | Cmd.CommandLine.push_back("-resource-dir=" + ResourceDir); |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 222 | if (auto Error = index(std::move(Cmd), IndexStorage)) |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 223 | log("Indexing {0} failed: {1}", Filename, std::move(Error)); |
| 224 | }, |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 225 | std::move(Cmd))); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 228 | // Resolves URI to file paths with cache. |
| 229 | class URIToFileCache { |
| 230 | public: |
| 231 | URIToFileCache(llvm::StringRef HintPath) : HintPath(HintPath) {} |
| 232 | |
| 233 | llvm::StringRef resolve(llvm::StringRef FileURI) { |
| 234 | auto I = URIToPathCache.try_emplace(FileURI); |
| 235 | if (I.second) { |
| 236 | auto U = URI::parse(FileURI); |
| 237 | if (!U) { |
| 238 | elog("Failed to parse URI {0}: {1}", FileURI, U.takeError()); |
| 239 | assert(false && "Failed to parse URI"); |
| 240 | return ""; |
| 241 | } |
| 242 | auto Path = URI::resolve(*U, HintPath); |
| 243 | if (!Path) { |
| 244 | elog("Failed to resolve URI {0}: {1}", FileURI, Path.takeError()); |
| 245 | assert(false && "Failed to resolve URI"); |
| 246 | return ""; |
| 247 | } |
| 248 | I.first->second = *Path; |
| 249 | } |
| 250 | return I.first->second; |
| 251 | } |
| 252 | |
| 253 | private: |
| 254 | std::string HintPath; |
| 255 | llvm::StringMap<std::string> URIToPathCache; |
| 256 | }; |
| 257 | |
| 258 | /// Given index results from a TU, only update files in \p FilesToUpdate. |
| 259 | void BackgroundIndex::update(StringRef MainFile, SymbolSlab Symbols, |
| 260 | RefSlab Refs, |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 261 | const StringMap<FileDigest> &FilesToUpdate, |
| 262 | BackgroundIndexStorage *IndexStorage) { |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 263 | // Partition symbols/references into files. |
| 264 | struct File { |
| 265 | DenseSet<const Symbol *> Symbols; |
| 266 | DenseSet<const Ref *> Refs; |
| 267 | }; |
| 268 | StringMap<File> Files; |
| 269 | URIToFileCache URICache(MainFile); |
| 270 | for (const auto &Sym : Symbols) { |
| 271 | if (Sym.CanonicalDeclaration) { |
| 272 | auto DeclPath = URICache.resolve(Sym.CanonicalDeclaration.FileURI); |
| 273 | if (FilesToUpdate.count(DeclPath) != 0) |
| 274 | Files[DeclPath].Symbols.insert(&Sym); |
| 275 | } |
| 276 | // For symbols with different declaration and definition locations, we store |
| 277 | // the full symbol in both the header file and the implementation file, so |
| 278 | // that merging can tell the preferred symbols (from canonical headers) from |
| 279 | // other symbols (e.g. forward declarations). |
| 280 | if (Sym.Definition && |
| 281 | Sym.Definition.FileURI != Sym.CanonicalDeclaration.FileURI) { |
| 282 | auto DefPath = URICache.resolve(Sym.Definition.FileURI); |
| 283 | if (FilesToUpdate.count(DefPath) != 0) |
| 284 | Files[DefPath].Symbols.insert(&Sym); |
| 285 | } |
| 286 | } |
| 287 | DenseMap<const Ref *, SymbolID> RefToIDs; |
| 288 | for (const auto &SymRefs : Refs) { |
| 289 | for (const auto &R : SymRefs.second) { |
| 290 | auto Path = URICache.resolve(R.Location.FileURI); |
| 291 | if (FilesToUpdate.count(Path) != 0) { |
| 292 | auto &F = Files[Path]; |
| 293 | RefToIDs[&R] = SymRefs.first; |
| 294 | F.Refs.insert(&R); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Build and store new slabs for each updated file. |
| 300 | for (const auto &F : Files) { |
| 301 | StringRef Path = F.first(); |
| 302 | vlog("Update symbols in {0}", Path); |
| 303 | SymbolSlab::Builder Syms; |
| 304 | RefSlab::Builder Refs; |
| 305 | for (const auto *S : F.second.Symbols) |
| 306 | Syms.insert(*S); |
| 307 | for (const auto *R : F.second.Refs) |
| 308 | Refs.insert(RefToIDs[R], *R); |
| 309 | |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 310 | auto SS = llvm::make_unique<SymbolSlab>(std::move(Syms).build()); |
| 311 | auto RS = llvm::make_unique<RefSlab>(std::move(Refs).build()); |
| 312 | |
| 313 | auto Hash = FilesToUpdate.lookup(Path); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 314 | // We need to store shards before updating the index, since the latter |
| 315 | // consumes slabs. |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 316 | // FIXME: Store Hash in the Shard. |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 317 | if (IndexStorage) { |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 318 | IndexFileOut Shard; |
| 319 | Shard.Symbols = SS.get(); |
| 320 | Shard.Refs = RS.get(); |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 321 | if (auto Error = IndexStorage->storeShard(Path, Shard)) |
| 322 | elog("Failed to store shard for {0}: {1}", Path, std::move(Error)); |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 325 | std::lock_guard<std::mutex> Lock(DigestsMu); |
| 326 | // This can override a newer version that is added in another thread, |
| 327 | // if this thread sees the older version but finishes later. This should be |
| 328 | // rare in practice. |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 329 | IndexedFileDigests[Path] = Hash; |
| 330 | IndexedSymbols.update(Path, std::move(SS), std::move(RS)); |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | // Creates a filter to not collect index results from files with unchanged |
| 335 | // digests. |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 336 | // \p FileDigests contains file digests for the current indexed files, and all |
| 337 | // changed files will be added to \p FilesToUpdate. |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 338 | decltype(SymbolCollector::Options::FileFilter) createFileFilter( |
| 339 | const llvm::StringMap<BackgroundIndex::FileDigest> &FileDigests, |
| 340 | llvm::StringMap<BackgroundIndex::FileDigest> &FilesToUpdate) { |
| 341 | return [&FileDigests, &FilesToUpdate](const SourceManager &SM, FileID FID) { |
| 342 | StringRef Path; |
| 343 | if (const auto *F = SM.getFileEntryForID(FID)) |
| 344 | Path = F->getName(); |
| 345 | if (Path.empty()) |
| 346 | return false; // Skip invalid files. |
| 347 | SmallString<128> AbsPath(Path); |
| 348 | if (std::error_code EC = |
| 349 | SM.getFileManager().getVirtualFileSystem()->makeAbsolute(AbsPath)) { |
| 350 | elog("Warning: could not make absolute file: {0}", EC.message()); |
| 351 | return false; // Skip files without absolute path. |
| 352 | } |
| 353 | sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true); |
| 354 | auto Digest = digestFile(SM, FID); |
| 355 | if (!Digest) |
| 356 | return false; |
| 357 | auto D = FileDigests.find(AbsPath); |
| 358 | if (D != FileDigests.end() && D->second == Digest) |
| 359 | return false; // Skip files that haven't changed. |
| 360 | |
| 361 | FilesToUpdate[AbsPath] = *Digest; |
| 362 | return true; |
| 363 | }; |
| 364 | } |
| 365 | |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 366 | Error BackgroundIndex::index(tooling::CompileCommand Cmd, |
| 367 | BackgroundIndexStorage *IndexStorage) { |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 368 | trace::Span Tracer("BackgroundIndex"); |
| 369 | SPAN_ATTACH(Tracer, "file", Cmd.Filename); |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 370 | const std::string AbsolutePath = getAbsoluteFilePath(Cmd); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 371 | |
| 372 | auto FS = FSProvider.getFileSystem(); |
| 373 | auto Buf = FS->getBufferForFile(AbsolutePath); |
| 374 | if (!Buf) |
| 375 | return errorCodeToError(Buf.getError()); |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 376 | auto Hash = digest(Buf->get()->getBuffer()); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 377 | |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 378 | // Take a snapshot of the digests to avoid locking for each file in the TU. |
| 379 | llvm::StringMap<FileDigest> DigestsSnapshot; |
| 380 | { |
| 381 | std::lock_guard<std::mutex> Lock(DigestsMu); |
| 382 | if (IndexedFileDigests.lookup(AbsolutePath) == Hash) { |
| 383 | vlog("No need to index {0}, already up to date", AbsolutePath); |
| 384 | return Error::success(); |
| 385 | } |
| 386 | |
| 387 | DigestsSnapshot = IndexedFileDigests; |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | log("Indexing {0}", Cmd.Filename, toHex(Hash)); |
| 391 | ParseInputs Inputs; |
| 392 | Inputs.FS = std::move(FS); |
| 393 | Inputs.FS->setCurrentWorkingDirectory(Cmd.Directory); |
| 394 | Inputs.CompileCommand = std::move(Cmd); |
| 395 | auto CI = buildCompilerInvocation(Inputs); |
| 396 | if (!CI) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 397 | return createStringError(inconvertibleErrorCode(), |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 398 | "Couldn't build compiler invocation"); |
| 399 | IgnoreDiagnostics IgnoreDiags; |
| 400 | auto Clang = prepareCompilerInstance( |
| 401 | std::move(CI), /*Preamble=*/nullptr, std::move(*Buf), |
| 402 | std::make_shared<PCHContainerOperations>(), Inputs.FS, IgnoreDiags); |
| 403 | if (!Clang) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 404 | return createStringError(inconvertibleErrorCode(), |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 405 | "Couldn't build compiler instance"); |
| 406 | |
| 407 | SymbolCollector::Options IndexOpts; |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 408 | IndexOpts.URISchemes = URISchemes; |
| 409 | StringMap<FileDigest> FilesToUpdate; |
| 410 | IndexOpts.FileFilter = createFileFilter(DigestsSnapshot, FilesToUpdate); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 411 | SymbolSlab Symbols; |
| 412 | RefSlab Refs; |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 413 | auto Action = createStaticIndexingAction( |
| 414 | IndexOpts, [&](SymbolSlab S) { Symbols = std::move(S); }, |
| 415 | [&](RefSlab R) { Refs = std::move(R); }); |
| 416 | |
| 417 | // We're going to run clang here, and it could potentially crash. |
| 418 | // We could use CrashRecoveryContext to try to make indexing crashes nonfatal, |
| 419 | // but the leaky "recovery" is pretty scary too in a long-running process. |
| 420 | // If crashes are a real problem, maybe we should fork a child process. |
| 421 | |
| 422 | const FrontendInputFile &Input = Clang->getFrontendOpts().Inputs.front(); |
| 423 | if (!Action->BeginSourceFile(*Clang, Input)) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 424 | return createStringError(inconvertibleErrorCode(), |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 425 | "BeginSourceFile() failed"); |
| 426 | if (!Action->Execute()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 427 | return createStringError(inconvertibleErrorCode(), "Execute() failed"); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 428 | Action->EndSourceFile(); |
| 429 | |
| 430 | log("Indexed {0} ({1} symbols, {2} refs)", Inputs.CompileCommand.Filename, |
Haojian Wu | 6ece6e7 | 2018-10-18 15:33:20 +0000 | [diff] [blame] | 431 | Symbols.size(), Refs.numRefs()); |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 432 | SPAN_ATTACH(Tracer, "symbols", int(Symbols.size())); |
Haojian Wu | 6ece6e7 | 2018-10-18 15:33:20 +0000 | [diff] [blame] | 433 | SPAN_ATTACH(Tracer, "refs", int(Refs.numRefs())); |
Kadir Cetinkaya | 89a7691 | 2018-11-15 10:31:19 +0000 | [diff] [blame] | 434 | update(AbsolutePath, std::move(Symbols), std::move(Refs), FilesToUpdate, |
| 435 | IndexStorage); |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 436 | { |
| 437 | // Make sure hash for the main file is always updated even if there is no |
| 438 | // index data in it. |
| 439 | std::lock_guard<std::mutex> Lock(DigestsMu); |
| 440 | IndexedFileDigests[AbsolutePath] = Hash; |
| 441 | } |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 442 | |
| 443 | // FIXME: this should rebuild once-in-a-while, not after every file. |
| 444 | // At that point we should use Dex, too. |
| 445 | vlog("Rebuilding automatic index"); |
Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 446 | reset(IndexedSymbols.buildIndex(IndexType::Light, DuplicateHandling::Merge, |
| 447 | URISchemes)); |
| 448 | |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 449 | return Error::success(); |
| 450 | } |
| 451 | |
Kadir Cetinkaya | 2bed2cf | 2018-11-15 10:31:23 +0000 | [diff] [blame^] | 452 | BackgroundIndexStorage * |
| 453 | BackgroundIndex::getIndexStorage(llvm::StringRef CDBDirectory) { |
| 454 | if (!IndexStorageCreator) |
| 455 | return nullptr; |
| 456 | auto IndexStorageIt = IndexStorageMap.find(CDBDirectory); |
| 457 | if (IndexStorageIt == IndexStorageMap.end()) |
| 458 | IndexStorageIt = |
| 459 | IndexStorageMap |
| 460 | .insert({CDBDirectory, IndexStorageCreator(CDBDirectory)}) |
| 461 | .first; |
| 462 | return IndexStorageIt->second.get(); |
| 463 | } |
Kadir Cetinkaya | 3e5a475 | 2018-11-15 10:31:10 +0000 | [diff] [blame] | 464 | |
Sam McCall | 8dc9dbb | 2018-10-15 13:34:10 +0000 | [diff] [blame] | 465 | } // namespace clangd |
| 466 | } // namespace clang |