Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 1 | //===--- 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 McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 38 | |
| 39 | namespace clang { |
| 40 | namespace clangd { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 41 | |
| 42 | // Convert a path into the canonical form. |
| 43 | // Canonical form is either "/", or "/segment" * N: |
| 44 | // C:\foo\bar --> /c:/foo/bar |
| 45 | // /foo/ --> /foo |
| 46 | // a/b/c --> /a/b/c |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 47 | static llvm::SmallString<128> canonicalize(llvm::StringRef Path) { |
| 48 | llvm::SmallString<128> Result = Path.rtrim('/'); |
| 49 | native(Result, llvm::sys::path::Style::posix); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 50 | if (Result.empty() || Result.front() != '/') |
| 51 | Result.insert(Result.begin(), '/'); |
| 52 | return Result; |
| 53 | } |
| 54 | |
Kirill Bobyrev | c38b3f0 | 2018-08-31 08:29:48 +0000 | [diff] [blame] | 55 | constexpr const unsigned FileDistance::Unreachable; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 56 | const llvm::hash_code FileDistance::RootHash = |
| 57 | llvm::hash_value(llvm::StringRef("/")); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 58 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 59 | FileDistance::FileDistance(llvm::StringMap<SourceParams> Sources, |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 60 | const FileDistanceOptions &Opts) |
| 61 | : Opts(Opts) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 62 | llvm::DenseMap<llvm::hash_code, llvm::SmallVector<llvm::hash_code, 4>> |
| 63 | DownEdges; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 64 | // Compute the best distance following only up edges. |
| 65 | // Keep track of down edges, in case we can use them to improve on this. |
| 66 | for (const auto &S : Sources) { |
| 67 | auto Canonical = canonicalize(S.getKey()); |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 68 | dlog("Source {0} = {1}, MaxUp = {2}", Canonical, S.second.Cost, |
| 69 | S.second.MaxUpTraversals); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 70 | // Walk up to ancestors of this source, assigning cost. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 71 | llvm::StringRef Rest = Canonical; |
| 72 | llvm::hash_code Hash = llvm::hash_value(Rest); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 73 | for (unsigned I = 0; !Rest.empty(); ++I) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 74 | Rest = parent_path(Rest, llvm::sys::path::Style::posix); |
| 75 | auto NextHash = llvm::hash_value(Rest); |
Sam McCall | 7c96bb6 | 2018-07-04 08:27:28 +0000 | [diff] [blame] | 76 | auto &Down = DownEdges[NextHash]; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 77 | if (!llvm::is_contained(Down, Hash)) |
Fangrui Song | 8380c9e | 2018-10-07 17:21:08 +0000 | [diff] [blame] | 78 | Down.push_back(Hash); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 79 | // We can't just break after MaxUpTraversals, must still set DownEdges. |
| 80 | if (I > S.getValue().MaxUpTraversals) { |
| 81 | if (Cache.find(Hash) != Cache.end()) |
| 82 | break; |
| 83 | } else { |
| 84 | unsigned Cost = S.getValue().Cost + I * Opts.UpCost; |
| 85 | auto R = Cache.try_emplace(Hash, Cost); |
| 86 | if (!R.second) { |
| 87 | if (Cost < R.first->second) { |
| 88 | R.first->second = Cost; |
| 89 | } else { |
| 90 | // If we're not the best way to get to this path, stop assigning. |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | Hash = NextHash; |
| 96 | } |
| 97 | } |
| 98 | // Now propagate scores parent -> child if that's an improvement. |
| 99 | // BFS ensures we propagate down chains (must visit parents before children). |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 100 | std::queue<llvm::hash_code> Next; |
| 101 | for (auto Child : DownEdges.lookup(llvm::hash_value(llvm::StringRef("")))) |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 102 | Next.push(Child); |
| 103 | while (!Next.empty()) { |
Eric Liu | 0c29722c | 2018-10-16 10:41:17 +0000 | [diff] [blame] | 104 | auto Parent = Next.front(); |
| 105 | Next.pop(); |
| 106 | auto ParentCost = Cache.lookup(Parent); |
| 107 | for (auto Child : DownEdges.lookup(Parent)) { |
| 108 | if (Parent != RootHash || Opts.AllowDownTraversalFromRoot) { |
| 109 | auto &ChildCost = |
| 110 | Cache.try_emplace(Child, Unreachable).first->getSecond(); |
| 111 | if (ParentCost + Opts.DownCost < ChildCost) |
| 112 | ChildCost = ParentCost + Opts.DownCost; |
| 113 | } |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 114 | Next.push(Child); |
| 115 | } |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 119 | unsigned FileDistance::distance(llvm::StringRef Path) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 120 | auto Canonical = canonicalize(Path); |
Kirill Bobyrev | c1bb7b9 | 2018-08-31 08:19:50 +0000 | [diff] [blame] | 121 | unsigned Cost = Unreachable; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 122 | llvm::SmallVector<llvm::hash_code, 16> Ancestors; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 123 | // Walk up ancestors until we find a path we know the distance for. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 124 | for (llvm::StringRef Rest = Canonical; !Rest.empty(); |
| 125 | Rest = parent_path(Rest, llvm::sys::path::Style::posix)) { |
| 126 | auto Hash = llvm::hash_value(Rest); |
Eric Liu | 0c29722c | 2018-10-16 10:41:17 +0000 | [diff] [blame] | 127 | if (Hash == RootHash && !Ancestors.empty() && |
| 128 | !Opts.AllowDownTraversalFromRoot) { |
| 129 | Cost = Unreachable; |
| 130 | break; |
| 131 | } |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 132 | auto It = Cache.find(Hash); |
| 133 | if (It != Cache.end()) { |
| 134 | Cost = It->second; |
| 135 | break; |
| 136 | } |
| 137 | Ancestors.push_back(Hash); |
| 138 | } |
| 139 | // Now we know the costs for (known node, queried node]. |
| 140 | // Fill these in, walking down the directory tree. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 141 | for (llvm::hash_code Hash : llvm::reverse(Ancestors)) { |
Kirill Bobyrev | c1bb7b9 | 2018-08-31 08:19:50 +0000 | [diff] [blame] | 142 | if (Cost != Unreachable) |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 143 | Cost += Opts.DownCost; |
| 144 | Cache.try_emplace(Hash, Cost); |
| 145 | } |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 146 | dlog("distance({0} = {1})", Path, Cost); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 147 | return Cost; |
| 148 | } |
| 149 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 150 | unsigned URIDistance::distance(llvm::StringRef URI) { |
| 151 | auto R = Cache.try_emplace(llvm::hash_value(URI), FileDistance::Unreachable); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 152 | if (!R.second) |
| 153 | return R.first->getSecond(); |
| 154 | if (auto U = clangd::URI::parse(URI)) { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 155 | dlog("distance({0} = {1})", URI, U->body()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 156 | R.first->second = forScheme(U->scheme()).distance(U->body()); |
| 157 | } else { |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 158 | log("URIDistance::distance() of unparseable {0}: {1}", URI, U.takeError()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 159 | } |
| 160 | return R.first->second; |
| 161 | } |
| 162 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 163 | FileDistance &URIDistance::forScheme(llvm::StringRef Scheme) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 164 | auto &Delegate = ByScheme[Scheme]; |
| 165 | if (!Delegate) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 166 | llvm::StringMap<SourceParams> SchemeSources; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 167 | for (const auto &Source : Sources) { |
| 168 | if (auto U = clangd::URI::create(Source.getKey(), Scheme)) |
| 169 | SchemeSources.try_emplace(U->body(), Source.getValue()); |
| 170 | else |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 171 | llvm::consumeError(U.takeError()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 172 | } |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 173 | dlog("FileDistance for scheme {0}: {1}/{2} sources", Scheme, |
| 174 | SchemeSources.size(), Sources.size()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 175 | Delegate.reset(new FileDistance(std::move(SchemeSources), Opts)); |
| 176 | } |
| 177 | return *Delegate; |
| 178 | } |
| 179 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 180 | static std::pair<std::string, int> scopeToPath(llvm::StringRef Scope) { |
| 181 | llvm::SmallVector<llvm::StringRef, 4> Split; |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 182 | Scope.split(Split, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 183 | return {"/" + llvm::join(Split, "/"), Split.size()}; |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 186 | static FileDistance |
| 187 | createScopeFileDistance(llvm::ArrayRef<std::string> QueryScopes) { |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 188 | FileDistanceOptions Opts; |
| 189 | Opts.UpCost = 2; |
| 190 | Opts.DownCost = 4; |
| 191 | Opts.AllowDownTraversalFromRoot = false; |
| 192 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 193 | llvm::StringMap<SourceParams> Sources; |
| 194 | llvm::StringRef Preferred = |
| 195 | QueryScopes.empty() ? "" : QueryScopes.front().c_str(); |
| 196 | for (llvm::StringRef S : QueryScopes) { |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 197 | SourceParams Param; |
| 198 | // Penalize the global scope even it's preferred, as all projects can define |
| 199 | // symbols in it, and there is pattern where using-namespace is used in |
| 200 | // place of enclosing namespaces (e.g. in implementation files). |
| 201 | if (S == Preferred) |
Eric Liu | 4fac50e | 2018-11-26 12:12:01 +0000 | [diff] [blame] | 202 | Param.Cost = S == "" ? 4 : 0; |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 203 | else if (Preferred.startswith(S) && !S.empty()) |
| 204 | continue; // just rely on up-traversals. |
| 205 | else |
Eric Liu | 4fac50e | 2018-11-26 12:12:01 +0000 | [diff] [blame] | 206 | Param.Cost = S == "" ? 6 : 2; |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 207 | auto Path = scopeToPath(S); |
| 208 | // The global namespace is not 'near' its children. |
| 209 | Param.MaxUpTraversals = std::max(Path.second - 1, 0); |
| 210 | Sources[Path.first] = std::move(Param); |
| 211 | } |
| 212 | return FileDistance(Sources, Opts); |
| 213 | } |
| 214 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 215 | ScopeDistance::ScopeDistance(llvm::ArrayRef<std::string> QueryScopes) |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 216 | : Distance(createScopeFileDistance(QueryScopes)) {} |
| 217 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame^] | 218 | unsigned ScopeDistance::distance(llvm::StringRef SymbolScope) { |
Eric Liu | 3fac4ef | 2018-10-17 11:19:02 +0000 | [diff] [blame] | 219 | return Distance.distance(scopeToPath(SymbolScope).first); |
| 220 | } |
| 221 | |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 222 | } // namespace clangd |
| 223 | } // namespace clang |