clang-tools 10.0.0

FileDistance.cpp
Go to the documentation of this file.
1 //===--- FileDistance.cpp - File contents container -------------*- C++ -*-===//
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 // The FileDistance structure allows calculating the minimum distance to paths
10 // in a single tree.
11 // We simply walk up the path's ancestors until we find a node whose cost is
12 // known, and add the cost of walking back down. Initialization ensures this
13 // gives the correct path to the roots.
14 // We cache the results, so that the runtime is O(|A|), where A is the set of
15 // all distinct ancestors of visited paths.
16 //
17 // Example after initialization with /=2, /bar=0, DownCost = 1:
18 // / = 2
19 // /bar = 0
20 //
21 // After querying /foo/bar and /bar/foo:
22 // / = 2
23 // /bar = 0
24 // /bar/foo = 1
25 // /foo = 3
26 // /foo/bar = 4
27 //
28 // URIDistance creates FileDistance lazily for each URI scheme encountered. In
29 // practice this is a small constant factor.
30 //
31 //===-------------------------------------------------------------------------//
32
33 #include "FileDistance.h"
34 #include "Logger.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include <queue>
37
38 namespace clang {
39 namespace clangd {
40
41 // Convert a path into the canonical form.
42 // Canonical form is either "/", or "/segment" * N:
43 // C:\foo\bar --> /c:/foo/bar
44 // /foo/ --> /foo
45 // a/b/c --> /a/b/c
46 static llvm::SmallString<128> canonicalize(llvm::StringRef Path) {
47 llvm::SmallString<128> Result = Path.rtrim('/');
48 native(Result, llvm::sys::path::Style::posix);
49 if (Result.empty() || Result.front() != '/')
50 Result.insert(Result.begin(), '/');
51 return Result;
52 }
53
54 constexpr const unsigned FileDistance::Unreachable;
55 const llvm::hash_code FileDistance::RootHash =
56 llvm::hash_value(llvm::StringRef("/"));
57
58 FileDistance::FileDistance(llvm::StringMap<SourceParams> Sources,
59 const FileDistanceOptions &Opts)
60 : Opts(Opts) {
61 llvm::DenseMap<llvm::hash_code, llvm::SmallVector<llvm::hash_code, 4>>
62 DownEdges;
63 // Compute the best distance following only up edges.
64 // Keep track of down edges, in case we can use them to improve on this.
65 for (const auto &S : Sources) {
66 auto Canonical = canonicalize(S.getKey());
67 dlog("Source {0} = {1}, MaxUp = {2}", Canonical, S.second.Cost,
68 S.second.MaxUpTraversals);
69 // Walk up to ancestors of this source, assigning cost.
70 llvm::StringRef Rest = Canonical;
71 llvm::hash_code Hash = llvm::hash_value(Rest);
72 for (unsigned I = 0; !Rest.empty(); ++I) {
73 Rest = parent_path(Rest, llvm::sys::path::Style::posix);
74 auto NextHash = llvm::hash_value(Rest);
75 auto &Down = DownEdges[NextHash];
76 if (!llvm::is_contained(Down, Hash))
77 Down.push_back(Hash);
78 // We can't just break after MaxUpTraversals, must still set DownEdges.
79 if (I > S.getValue().MaxUpTraversals) {
80 if (Cache.find(Hash) != Cache.end())
81 break;
82 } else {
83 unsigned Cost = S.getValue().Cost + I * Opts.UpCost;
84 auto R = Cache.try_emplace(Hash, Cost);
85 if (!R.second) {
86 if (Cost < R.first->second) {
87 R.first->second = Cost;
88 } else {
89 // If we're not the best way to get to this path, stop assigning.
90 break;
91 }
92 }
93 }
94 Hash = NextHash;
95 }
96 }
97 // Now propagate scores parent -> child if that's an improvement.
98 // BFS ensures we propagate down chains (must visit parents before children).
99 std::queue<llvm::hash_code> Next;
100 for (auto Child : DownEdges.lookup(llvm::hash_value(llvm::StringRef(""))))
101 Next.push(Child);
102 while (!Next.empty()) {
103 auto Parent = Next.front();
104 Next.pop();
105 auto ParentCost = Cache.lookup(Parent);
106 for (auto Child : DownEdges.lookup(Parent)) {
107 if (Parent != RootHash || Opts.AllowDownTraversalFromRoot) {
108 auto &ChildCost =
109 Cache.try_emplace(Child, Unreachable).first->getSecond();
110 if (ParentCost + Opts.DownCost < ChildCost)
111 ChildCost = ParentCost + Opts.DownCost;
112 }
113 Next.push(Child);
114 }
115 }
116 }
117
118 unsigned FileDistance::distance(llvm::StringRef Path) {
119 auto Canonical = canonicalize(Path);
120 unsigned Cost = Unreachable;
121 llvm::SmallVector<llvm::hash_code, 16> Ancestors;
122 // Walk up ancestors until we find a path we know the distance for.
123 for (llvm::StringRef Rest = Canonical; !Rest.empty();
124 Rest = parent_path(Rest, llvm::sys::path::Style::posix)) {
125 auto Hash = llvm::hash_value(Rest);
126 if (Hash == RootHash && !Ancestors.empty() &&
127 !Opts.AllowDownTraversalFromRoot) {
128 Cost = Unreachable;
129 break;
130 }
131 auto It = Cache.find(Hash);
132 if (It != Cache.end()) {
133 Cost = It->second;
134 break;
135 }
136 Ancestors.push_back(Hash);
137 }
138 // Now we know the costs for (known node, queried node].
139 // Fill these in, walking down the directory tree.
140 for (llvm::hash_code Hash : llvm::reverse(Ancestors)) {
141 if (Cost != Unreachable)
142 Cost += Opts.DownCost;
143 Cache.try_emplace(Hash, Cost);
144 }
145 dlog("distance({0} = {1})", Path, Cost);
146 return Cost;
147 }
148
149 unsigned URIDistance::distance(llvm::StringRef URI) {
150 auto R = Cache.try_emplace(llvm::hash_value(URI), FileDistance::Unreachable);
151 if (!R.second)
152 return R.first->getSecond();
153 if (auto U = clangd::URI::parse(URI)) {
154 dlog("distance({0} = {1})", URI, U->body());
155 R.first->second = forScheme(U->scheme()).distance(U->body());
156 } else {
157 log("URIDistance::distance() of unparseable {0}: {1}", URI, U.takeError());
158 }
159 return R.first->second;
160 }
161
162 FileDistance &URIDistance::forScheme(llvm::StringRef Scheme) {
163 auto &Delegate = ByScheme[Scheme];
164 if (!Delegate) {
165 llvm::StringMap<SourceParams> SchemeSources;
166 for (const auto &Source : Sources) {
167 if (auto U = clangd::URI::create(Source.getKey(), Scheme))
168 SchemeSources.try_emplace(U->body(), Source.getValue());
169 else
170 llvm::consumeError(U.takeError());
171 }
172 dlog("FileDistance for scheme {0}: {1}/{2} sources", Scheme,
173 SchemeSources.size(), Sources.size());
174 Delegate.reset(new FileDistance(std::move(SchemeSources), Opts));
175 }
176 return *Delegate;
177 }
178
179 static std::pair<std::string, int> scopeToPath(llvm::StringRef Scope) {
180 llvm::SmallVector<llvm::StringRef, 4> Split;
181 Scope.split(Split, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
182 return {"/" + llvm::join(Split, "/"), Split.size()};
183 }
184
185 static FileDistance
186 createScopeFileDistance(llvm::ArrayRef<std::string> QueryScopes) {
187 FileDistanceOptions Opts;
188 Opts.UpCost = 2;
189 Opts.DownCost = 4;
190 Opts.AllowDownTraversalFromRoot = false;
191
192 llvm::StringMap<SourceParams> Sources;
193 llvm::StringRef Preferred =
194 QueryScopes.empty() ? "" : QueryScopes.front().c_str();
195 for (llvm::StringRef S : QueryScopes) {
196 SourceParams Param;
197 // Penalize the global scope even it's preferred, as all projects can define
198 // symbols in it, and there is pattern where using-namespace is used in
199 // place of enclosing namespaces (e.g. in implementation files).
200 if (S == Preferred)
201 Param.Cost = S == "" ? 4 : 0;
202 else if (Preferred.startswith(S) && !S.empty())
203 continue; // just rely on up-traversals.
204 else
205 Param.Cost = S == "" ? 6 : 2;
206 auto Path = scopeToPath(S);
207 // The global namespace is not 'near' its children.
208 Param.MaxUpTraversals = std::max(Path.second - 1, 0);
209 Sources[Path.first] = std::move(Param);
210 }
211 return FileDistance(std::move(Sources), Opts);
212 }
213
214 ScopeDistance::ScopeDistance(llvm::ArrayRef<std::string> QueryScopes)
215 : Distance(createScopeFileDistance(QueryScopes)) {}
216
217 unsigned ScopeDistance::distance(llvm::StringRef SymbolScope) {
218 return Distance.distance(scopeToPath(SymbolScope).first);
219 }
220
221 } // namespace clangd
222 } // namespace clang
clang::clangd::FileDistanceOptions
Definition: FileDistance.h:56
clang::clangd::ScopeDistance::ScopeDistance
ScopeDistance(llvm::ArrayRef< std::string > QueryScopes)
QueryScopes[0] is the preferred scope.
Definition: FileDistance.cpp:214
clang::clangd::ScopeDistance::distance
unsigned distance(llvm::StringRef SymbolScope)
Definition: FileDistance.cpp:217
Parent
const Node * Parent
Definition: ExtractFunction.cpp:148
clang::clangd::scopeToPath
static std::pair< std::string, int > scopeToPath(llvm::StringRef Scope)
Definition: FileDistance.cpp:179
clang::clangd::FileDistance::Unreachable
static constexpr unsigned Unreachable
Definition: FileDistance.h:74
clang::clangd::SourceParams
Definition: FileDistance.h:63
clang::clangd::SourceParams::Cost
unsigned Cost
Definition: FileDistance.h:65
clang::clangd::FileDistanceOptions::DownCost
unsigned DownCost
Definition: FileDistance.h:58
clang::clangd::FileDistance
Definition: FileDistance.h:72
FileDistance.h
clang::clangd::log
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:62
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:20
clang::clangd::FileDistanceOptions::UpCost
unsigned UpCost
Definition: FileDistance.h:57
dlog
#define dlog(...)
Definition: Logger.h:72
clang::clangd::canonicalize
static llvm::SmallString< 128 > canonicalize(llvm::StringRef Path)
Definition: FileDistance.cpp:46
clang::clangd::FileDistance::FileDistance
FileDistance(llvm::StringMap< SourceParams > Sources, const FileDistanceOptions &Opts={})
Definition: FileDistance.cpp:58
clang::clangd::URIDistance::distance
unsigned distance(llvm::StringRef URI)
Definition: FileDistance.cpp:149
Logger.h
clang::clangd::URI::create
static llvm::Expected< URI > create(llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
Creates a URI for a file in the given scheme.
Definition: URI.cpp:197
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::createScopeFileDistance
static FileDistance createScopeFileDistance(llvm::ArrayRef< std::string > QueryScopes)
Definition: FileDistance.cpp:186
clang::clangd::URI
A URI describes the location of a source file.
Definition: URI.h:28
clang::clangd::hash_value
llvm::hash_code hash_value(const SymbolID &ID)
Definition: SymbolID.cpp:50
clang::clangd::URI::parse
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Parse a URI string "<scheme>:[//<authority>/]<path>".
Definition: URI.cpp:164
clang::clangd::SourceParams::MaxUpTraversals
unsigned MaxUpTraversals
Definition: FileDistance.h:67
clang::tidy::cppcoreguidelines::join
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
Definition: SpecialMemberFunctionsCheck.cpp:81
clang::clangd::FileDistance::distance
unsigned distance(llvm::StringRef Path)
Definition: FileDistance.cpp:118
clang::clangd::FileDistanceOptions::AllowDownTraversalFromRoot
bool AllowDownTraversalFromRoot
Definition: FileDistance.h:60
clang::clangd::FileDistance::RootHash
static const llvm::hash_code RootHash
Definition: FileDistance.h:75