14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/Support/Path.h"
29 class BackgroundIndexLoader {
32 : IndexStorageFactory(IndexStorageFactory) {}
37 std::vector<LoadedShard> takeResult() &&;
43 std::pair<const LoadedShard &, std::vector<Path>>
47 llvm::StringMap<LoadedShard> LoadedShards;
52 std::pair<const LoadedShard &, std::vector<Path>>
53 BackgroundIndexLoader::loadShard(
PathRef StartSourceFile,
PathRef DependentTU) {
54 auto It = LoadedShards.try_emplace(StartSourceFile);
55 LoadedShard &LS = It.first->getValue();
56 std::vector<Path> Edges = {};
61 LS.AbsolutePath = StartSourceFile.str();
62 LS.DependentTU = std::string(DependentTU);
63 BackgroundIndexStorage *Storage = IndexStorageFactory(LS.AbsolutePath);
64 auto Shard = Storage->loadShard(StartSourceFile);
65 if (!Shard || !Shard->Sources) {
66 vlog(
"Failed to load shard: {0}", StartSourceFile);
70 LS.Shard = std::move(Shard);
71 for (
const auto &It : *LS.Shard->Sources) {
72 auto AbsPath =
URI::resolve(It.getKey(), StartSourceFile);
74 elog(
"Failed to resolve URI: {0}", AbsPath.takeError());
78 if (*AbsPath != StartSourceFile) {
79 Edges.push_back(*AbsPath);
84 const IncludeGraphNode &IGN = It.getValue();
85 LS.Digest = IGN.Digest;
89 assert(LS.Digest !=
FileDigest{{0}} &&
"Digest is empty?");
94 llvm::StringSet<> InQueue;
96 std::queue<PathRef> ToVisit;
100 while (!ToVisit.empty()) {
101 PathRef SourceFile = ToVisit.front();
104 auto ShardAndEdges = loadShard(SourceFile,
MainFile);
105 for (
PathRef Edge : ShardAndEdges.second) {
106 auto It = InQueue.insert(Edge);
108 ToVisit.push(It.first->getKey());
113 std::vector<LoadedShard> BackgroundIndexLoader::takeResult() && {
114 std::vector<LoadedShard> Result;
115 Result.reserve(LoadedShards.size());
116 for (
auto &It : LoadedShards)
117 Result.push_back(std::move(It.getValue()));
122 std::vector<LoadedShard>
126 BackgroundIndexLoader Loader(IndexStorageFactory);
127 for (llvm::StringRef
MainFile : MainFiles) {
128 assert(llvm::sys::path::is_absolute(
MainFile));
131 return std::move(Loader).takeResult();