clang-tools 10.0.0

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