blob: a6a65ab7ead25fdf6d5c594df45fef6ce1cbc5dc [file] [log] [blame]
Sam McCall3f0243f2018-07-03 08:09:29 +00001//===--- FileDistance.cpp - File contents container -------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam McCall3f0243f2018-07-03 08:09:29 +00006//
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>
Sam McCall3f0243f2018-07-03 08:09:29 +000037
38namespace clang {
39namespace clangd {
Sam McCall3f0243f2018-07-03 08:09:29 +000040
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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000046static llvm::SmallString<128> canonicalize(llvm::StringRef Path) {
47 llvm::SmallString<128> Result = Path.rtrim('/');
48 native(Result, llvm::sys::path::Style::posix);
Sam McCall3f0243f2018-07-03 08:09:29 +000049 if (Result.empty() || Result.front() != '/')
50 Result.insert(Result.begin(), '/');
51 return Result;
52}
53
Kirill Bobyrevc38b3f02018-08-31 08:29:48 +000054constexpr const unsigned FileDistance::Unreachable;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000055const llvm::hash_code FileDistance::RootHash =
56 llvm::hash_value(llvm::StringRef("/"));
Sam McCall3f0243f2018-07-03 08:09:29 +000057
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000058FileDistance::FileDistance(llvm::StringMap<SourceParams> Sources,
Sam McCall3f0243f2018-07-03 08:09:29 +000059 const FileDistanceOptions &Opts)
60 : Opts(Opts) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000061 llvm::DenseMap<llvm::hash_code, llvm::SmallVector<llvm::hash_code, 4>>
62 DownEdges;
Sam McCall3f0243f2018-07-03 08:09:29 +000063 // 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.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000070 llvm::StringRef Rest = Canonical;
71 llvm::hash_code Hash = llvm::hash_value(Rest);
Sam McCall3f0243f2018-07-03 08:09:29 +000072 for (unsigned I = 0; !Rest.empty(); ++I) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000073 Rest = parent_path(Rest, llvm::sys::path::Style::posix);
74 auto NextHash = llvm::hash_value(Rest);
Sam McCall7c96bb62018-07-04 08:27:28 +000075 auto &Down = DownEdges[NextHash];
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000076 if (!llvm::is_contained(Down, Hash))
Fangrui Song8380c9e2018-10-07 17:21:08 +000077 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).
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000099 std::queue<llvm::hash_code> Next;
100 for (auto Child : DownEdges.lookup(llvm::hash_value(llvm::StringRef(""))))
Sam McCall3f0243f2018-07-03 08:09:29 +0000101 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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000118unsigned FileDistance::distance(llvm::StringRef Path) {
Sam McCall3f0243f2018-07-03 08:09:29 +0000119 auto Canonical = canonicalize(Path);
Kirill Bobyrevc1bb7b92018-08-31 08:19:50 +0000120 unsigned Cost = Unreachable;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000121 llvm::SmallVector<llvm::hash_code, 16> Ancestors;
Sam McCall3f0243f2018-07-03 08:09:29 +0000122 // Walk up ancestors until we find a path we know the distance for.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000123 for (llvm::StringRef Rest = Canonical; !Rest.empty();
124 Rest = parent_path(Rest, llvm::sys::path::Style::posix)) {
125 auto Hash = llvm::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.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000140 for (llvm::hash_code Hash : llvm::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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000149unsigned URIDistance::distance(llvm::StringRef URI) {
150 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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000162FileDistance &URIDistance::forScheme(llvm::StringRef Scheme) {
Sam McCall3f0243f2018-07-03 08:09:29 +0000163 auto &Delegate = ByScheme[Scheme];
164 if (!Delegate) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000165 llvm::StringMap<SourceParams> SchemeSources;
Sam McCall3f0243f2018-07-03 08:09:29 +0000166 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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000170 llvm::consumeError(U.takeError());
Sam McCall3f0243f2018-07-03 08:09:29 +0000171 }
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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000179static std::pair<std::string, int> scopeToPath(llvm::StringRef Scope) {
180 llvm::SmallVector<llvm::StringRef, 4> Split;
Eric Liu3fac4ef2018-10-17 11:19:02 +0000181 Scope.split(Split, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000182 return {"/" + llvm::join(Split, "/"), Split.size()};
Eric Liu3fac4ef2018-10-17 11:19:02 +0000183}
184
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000185static FileDistance
186createScopeFileDistance(llvm::ArrayRef<std::string> QueryScopes) {
Eric Liu3fac4ef2018-10-17 11:19:02 +0000187 FileDistanceOptions Opts;
188 Opts.UpCost = 2;
189 Opts.DownCost = 4;
190 Opts.AllowDownTraversalFromRoot = false;
191
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000192 llvm::StringMap<SourceParams> Sources;
193 llvm::StringRef Preferred =
194 QueryScopes.empty() ? "" : QueryScopes.front().c_str();
195 for (llvm::StringRef S : QueryScopes) {
Eric Liu3fac4ef2018-10-17 11:19:02 +0000196 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)
Eric Liu4fac50e2018-11-26 12:12:01 +0000201 Param.Cost = S == "" ? 4 : 0;
Eric Liu3fac4ef2018-10-17 11:19:02 +0000202 else if (Preferred.startswith(S) && !S.empty())
203 continue; // just rely on up-traversals.
204 else
Eric Liu4fac50e2018-11-26 12:12:01 +0000205 Param.Cost = S == "" ? 6 : 2;
Eric Liu3fac4ef2018-10-17 11:19:02 +0000206 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(Sources, Opts);
212}
213
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000214ScopeDistance::ScopeDistance(llvm::ArrayRef<std::string> QueryScopes)
Eric Liu3fac4ef2018-10-17 11:19:02 +0000215 : Distance(createScopeFileDistance(QueryScopes)) {}
216
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000217unsigned ScopeDistance::distance(llvm::StringRef SymbolScope) {
Eric Liu3fac4ef2018-10-17 11:19:02 +0000218 return Distance.distance(scopeToPath(SymbolScope).first);
219}
220
Sam McCall3f0243f2018-07-03 08:09:29 +0000221} // namespace clangd
222} // namespace clang