blob: ecf8200f7f2ed163a01be357461d3efc776416e8 [file] [log] [blame]
Sam McCall3f0243f2018-07-03 08:09:29 +00001//===--- FileDistance.cpp - File contents container -------------*- C++ -*-===//
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// The FileDistance structure allows calculating the minimum distance to paths
11// in a single tree.
12// We simply walk up the path's ancestors until we find a node whose cost is
13// known, and add the cost of walking back down. Initialization ensures this
14// gives the correct path to the roots.
15// We cache the results, so that the runtime is O(|A|), where A is the set of
16// all distinct ancestors of visited paths.
17//
18// Example after initialization with /=2, /bar=0, DownCost = 1:
19// / = 2
20// /bar = 0
21//
22// After querying /foo/bar and /bar/foo:
23// / = 2
24// /bar = 0
25// /bar/foo = 1
26// /foo = 3
27// /foo/bar = 4
28//
29// URIDistance creates FileDistance lazily for each URI scheme encountered. In
30// practice this is a small constant factor.
31//
32//===-------------------------------------------------------------------------//
33
34#include "FileDistance.h"
35#include "Logger.h"
36#include "llvm/ADT/STLExtras.h"
37#include <queue>
Sam McCall3f0243f2018-07-03 08:09:29 +000038
39namespace clang {
40namespace clangd {
41using namespace llvm;
42
43// Convert a path into the canonical form.
44// Canonical form is either "/", or "/segment" * N:
45// C:\foo\bar --> /c:/foo/bar
46// /foo/ --> /foo
47// a/b/c --> /a/b/c
48static SmallString<128> canonicalize(StringRef Path) {
49 SmallString<128> Result = Path.rtrim('/');
50 native(Result, sys::path::Style::posix);
51 if (Result.empty() || Result.front() != '/')
52 Result.insert(Result.begin(), '/');
53 return Result;
54}
55
Kirill Bobyrevc38b3f02018-08-31 08:29:48 +000056constexpr const unsigned FileDistance::Unreachable;
Eric Liu0c29722c2018-10-16 10:41:17 +000057const llvm::hash_code FileDistance::RootHash = hash_value(StringRef("/"));
Sam McCall3f0243f2018-07-03 08:09:29 +000058
59FileDistance::FileDistance(StringMap<SourceParams> Sources,
60 const FileDistanceOptions &Opts)
61 : Opts(Opts) {
62 llvm::DenseMap<hash_code, SmallVector<hash_code, 4>> 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());
Sam McCallbed58852018-07-11 10:35:11 +000067 dlog("Source {0} = {1}, MaxUp = {2}", Canonical, S.second.Cost,
68 S.second.MaxUpTraversals);
Sam McCall3f0243f2018-07-03 08:09:29 +000069 // Walk up to ancestors of this source, assigning cost.
70 StringRef Rest = Canonical;
71 llvm::hash_code Hash = hash_value(Rest);
72 for (unsigned I = 0; !Rest.empty(); ++I) {
73 Rest = parent_path(Rest, sys::path::Style::posix);
74 auto NextHash = hash_value(Rest);
Sam McCall7c96bb62018-07-04 08:27:28 +000075 auto &Down = DownEdges[NextHash];
Fangrui Song8380c9e2018-10-07 17:21:08 +000076 if (!llvm::is_contained(Down, Hash))
77 Down.push_back(Hash);
Sam McCall3f0243f2018-07-03 08:09:29 +000078 // 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<hash_code> Next;
100 for (auto Child : DownEdges.lookup(hash_value(llvm::StringRef(""))))
101 Next.push(Child);
102 while (!Next.empty()) {
Eric Liu0c29722c2018-10-16 10:41:17 +0000103 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 }
Sam McCall3f0243f2018-07-03 08:09:29 +0000113 Next.push(Child);
114 }
Sam McCall3f0243f2018-07-03 08:09:29 +0000115 }
116}
117
118unsigned FileDistance::distance(StringRef Path) {
119 auto Canonical = canonicalize(Path);
Kirill Bobyrevc1bb7b92018-08-31 08:19:50 +0000120 unsigned Cost = Unreachable;
Sam McCall3f0243f2018-07-03 08:09:29 +0000121 SmallVector<hash_code, 16> Ancestors;
122 // Walk up ancestors until we find a path we know the distance for.
123 for (StringRef Rest = Canonical; !Rest.empty();
124 Rest = parent_path(Rest, sys::path::Style::posix)) {
125 auto Hash = hash_value(Rest);
Eric Liu0c29722c2018-10-16 10:41:17 +0000126 if (Hash == RootHash && !Ancestors.empty() &&
127 !Opts.AllowDownTraversalFromRoot) {
128 Cost = Unreachable;
129 break;
130 }
Sam McCall3f0243f2018-07-03 08:09:29 +0000131 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 (hash_code Hash : reverse(Ancestors)) {
Kirill Bobyrevc1bb7b92018-08-31 08:19:50 +0000141 if (Cost != Unreachable)
Sam McCall3f0243f2018-07-03 08:09:29 +0000142 Cost += Opts.DownCost;
143 Cache.try_emplace(Hash, Cost);
144 }
Sam McCallbed58852018-07-11 10:35:11 +0000145 dlog("distance({0} = {1})", Path, Cost);
Sam McCall3f0243f2018-07-03 08:09:29 +0000146 return Cost;
147}
148
149unsigned URIDistance::distance(llvm::StringRef URI) {
Kirill Bobyrevc1bb7b92018-08-31 08:19:50 +0000150 auto R = Cache.try_emplace(llvm::hash_value(URI), FileDistance::Unreachable);
Sam McCall3f0243f2018-07-03 08:09:29 +0000151 if (!R.second)
152 return R.first->getSecond();
153 if (auto U = clangd::URI::parse(URI)) {
Sam McCallbed58852018-07-11 10:35:11 +0000154 dlog("distance({0} = {1})", URI, U->body());
Sam McCall3f0243f2018-07-03 08:09:29 +0000155 R.first->second = forScheme(U->scheme()).distance(U->body());
156 } else {
Sam McCallbed58852018-07-11 10:35:11 +0000157 log("URIDistance::distance() of unparseable {0}: {1}", URI, U.takeError());
Sam McCall3f0243f2018-07-03 08:09:29 +0000158 }
159 return R.first->second;
160}
161
162FileDistance &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 consumeError(U.takeError());
171 }
Sam McCallbed58852018-07-11 10:35:11 +0000172 dlog("FileDistance for scheme {0}: {1}/{2} sources", Scheme,
173 SchemeSources.size(), Sources.size());
Sam McCall3f0243f2018-07-03 08:09:29 +0000174 Delegate.reset(new FileDistance(std::move(SchemeSources), Opts));
175 }
176 return *Delegate;
177}
178
Eric Liu3fac4ef2018-10-17 11:19:02 +0000179static std::pair<std::string, int> scopeToPath(llvm::StringRef Scope) {
180 SmallVector<StringRef, 4> Split;
181 Scope.split(Split, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
182 return {"/" + llvm::join(Split, "/"), Split.size()};
183}
184
185static FileDistance
186createScopeFileDistance(llvm::ArrayRef<std::string> QueryScopes) {
187 FileDistanceOptions Opts;
188 Opts.UpCost = 2;
189 Opts.DownCost = 4;
190 Opts.AllowDownTraversalFromRoot = false;
191
192 StringMap<SourceParams> Sources;
193 StringRef Preferred = QueryScopes.empty() ? "" : QueryScopes.front().c_str();
194 for (StringRef S : QueryScopes) {
195 SourceParams Param;
196 // Penalize the global scope even it's preferred, as all projects can define
197 // symbols in it, and there is pattern where using-namespace is used in
198 // place of enclosing namespaces (e.g. in implementation files).
199 if (S == Preferred)
200 Param.Cost = S == "" ? 2 : 0;
201 else if (Preferred.startswith(S) && !S.empty())
202 continue; // just rely on up-traversals.
203 else
204 Param.Cost = S == "" ? 5 : 2;
205 auto Path = scopeToPath(S);
206 // The global namespace is not 'near' its children.
207 Param.MaxUpTraversals = std::max(Path.second - 1, 0);
208 Sources[Path.first] = std::move(Param);
209 }
210 return FileDistance(Sources, Opts);
211}
212
213ScopeDistance::ScopeDistance(llvm::ArrayRef<std::string> QueryScopes)
214 : Distance(createScopeFileDistance(QueryScopes)) {}
215
216unsigned ScopeDistance::distance(llvm::StringRef SymbolScope) {
217 return Distance.distance(scopeToPath(SymbolScope).first);
218}
219
Sam McCall3f0243f2018-07-03 08:09:29 +0000220} // namespace clangd
221} // namespace clang