1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
//===-- BackgroundIndexLoader.cpp - ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "index/BackgroundIndexLoader.h"
#include "GlobalCompilationDatabase.h"
#include "index/Background.h"
#include "support/Logger.h"
#include "support/Path.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Path.h"
#include <string>
#include <utility>
#include <vector>
namespace clang {
namespace clangd {
namespace {
/// A helper class to cache BackgroundIndexStorage operations and keep the
/// inverse dependency mapping.
class BackgroundIndexLoader {
public:
BackgroundIndexLoader(BackgroundIndexStorage::Factory &IndexStorageFactory)
: IndexStorageFactory(IndexStorageFactory) {}
/// Load the shards for \p MainFile and all of its dependencies.
void load(PathRef MainFile);
/// Consumes the loader and returns all shards.
std::vector<LoadedShard> takeResult() &&;
private:
/// Returns the Shard for \p StartSourceFile from cache or loads it from \p
/// Storage. Also returns paths for dependencies of \p StartSourceFile if it
/// wasn't cached yet.
std::pair<const LoadedShard &, std::vector<Path>>
loadShard(PathRef StartSourceFile, PathRef DependentTU);
/// Cache for Storage lookups.
llvm::StringMap<LoadedShard> LoadedShards;
BackgroundIndexStorage::Factory &IndexStorageFactory;
};
std::pair<const LoadedShard &, std::vector<Path>>
BackgroundIndexLoader::loadShard(PathRef StartSourceFile, PathRef DependentTU) {
auto It = LoadedShards.try_emplace(StartSourceFile);
LoadedShard &LS = It.first->getValue();
std::vector<Path> Edges = {};
// Return the cached shard.
if (!It.second)
return {LS, Edges};
LS.AbsolutePath = StartSourceFile.str();
LS.DependentTU = std::string(DependentTU);
BackgroundIndexStorage *Storage = IndexStorageFactory(LS.AbsolutePath);
auto Shard = Storage->loadShard(StartSourceFile);
if (!Shard || !Shard->Sources) {
vlog("Failed to load shard: {0}", StartSourceFile);
return {LS, Edges};
}
LS.Shard = std::move(Shard);
for (const auto &It : *LS.Shard->Sources) {
auto AbsPath = URI::resolve(It.getKey(), StartSourceFile);
if (!AbsPath) {
elog("Failed to resolve URI: {0}", AbsPath.takeError());
continue;
}
// A shard contains only edges for non main-file sources.
if (*AbsPath != StartSourceFile) {
Edges.push_back(*AbsPath);
continue;
}
// Fill in shard metadata.
const IncludeGraphNode &IGN = It.getValue();
LS.Digest = IGN.Digest;
LS.CountReferences = IGN.Flags & IncludeGraphNode::SourceFlag::IsTU;
LS.HadErrors = IGN.Flags & IncludeGraphNode::SourceFlag::HadErrors;
}
assert(LS.Digest != FileDigest{{0}} && "Digest is empty?");
return {LS, Edges};
}
void BackgroundIndexLoader::load(PathRef MainFile) {
llvm::StringSet<> InQueue;
// Following containers points to strings inside InQueue.
std::queue<PathRef> ToVisit;
InQueue.insert(MainFile);
ToVisit.push(MainFile);
while (!ToVisit.empty()) {
PathRef SourceFile = ToVisit.front();
ToVisit.pop();
auto ShardAndEdges = loadShard(SourceFile, MainFile);
for (PathRef Edge : ShardAndEdges.second) {
auto It = InQueue.insert(Edge);
if (It.second)
ToVisit.push(It.first->getKey());
}
}
}
std::vector<LoadedShard> BackgroundIndexLoader::takeResult() && {
std::vector<LoadedShard> Result;
Result.reserve(LoadedShards.size());
for (auto &It : LoadedShards)
Result.push_back(std::move(It.getValue()));
return Result;
}
} // namespace
std::vector<LoadedShard>
loadIndexShards(llvm::ArrayRef<Path> MainFiles,
BackgroundIndexStorage::Factory &IndexStorageFactory,
const GlobalCompilationDatabase &CDB) {
BackgroundIndexLoader Loader(IndexStorageFactory);
for (llvm::StringRef MainFile : MainFiles) {
assert(llvm::sys::path::is_absolute(MainFile));
Loader.load(MainFile);
}
return std::move(Loader).takeResult();
}
} // namespace clangd
} // namespace clang
|