2018-07-03 16:09:29 +08:00
|
|
|
//===--- FileDistance.cpp - File contents container -------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-07-03 16:09:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The FileDistance structure allows calculating the minimum distance to paths
|
|
|
|
// in a single tree.
|
|
|
|
// We simply walk up the path's ancestors until we find a node whose cost is
|
|
|
|
// known, and add the cost of walking back down. Initialization ensures this
|
|
|
|
// gives the correct path to the roots.
|
|
|
|
// We cache the results, so that the runtime is O(|A|), where A is the set of
|
|
|
|
// all distinct ancestors of visited paths.
|
|
|
|
//
|
|
|
|
// Example after initialization with /=2, /bar=0, DownCost = 1:
|
|
|
|
// / = 2
|
|
|
|
// /bar = 0
|
|
|
|
//
|
|
|
|
// After querying /foo/bar and /bar/foo:
|
|
|
|
// / = 2
|
|
|
|
// /bar = 0
|
|
|
|
// /bar/foo = 1
|
|
|
|
// /foo = 3
|
|
|
|
// /foo/bar = 4
|
|
|
|
//
|
|
|
|
// URIDistance creates FileDistance lazily for each URI scheme encountered. In
|
|
|
|
// practice this is a small constant factor.
|
|
|
|
//
|
|
|
|
//===-------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
#include "FileDistance.h"
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
// Convert a path into the canonical form.
|
|
|
|
// Canonical form is either "/", or "/segment" * N:
|
|
|
|
// C:\foo\bar --> /c:/foo/bar
|
|
|
|
// /foo/ --> /foo
|
|
|
|
// a/b/c --> /a/b/c
|
2019-01-07 23:45:19 +08:00
|
|
|
static llvm::SmallString<128> canonicalize(llvm::StringRef Path) {
|
|
|
|
llvm::SmallString<128> Result = Path.rtrim('/');
|
|
|
|
native(Result, llvm::sys::path::Style::posix);
|
2018-07-03 16:09:29 +08:00
|
|
|
if (Result.empty() || Result.front() != '/')
|
|
|
|
Result.insert(Result.begin(), '/');
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:29:48 +08:00
|
|
|
constexpr const unsigned FileDistance::Unreachable;
|
2019-01-07 23:45:19 +08:00
|
|
|
const llvm::hash_code FileDistance::RootHash =
|
|
|
|
llvm::hash_value(llvm::StringRef("/"));
|
2018-07-03 16:09:29 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
FileDistance::FileDistance(llvm::StringMap<SourceParams> Sources,
|
2018-07-03 16:09:29 +08:00
|
|
|
const FileDistanceOptions &Opts)
|
|
|
|
: Opts(Opts) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::DenseMap<llvm::hash_code, llvm::SmallVector<llvm::hash_code, 4>>
|
|
|
|
DownEdges;
|
2018-07-03 16:09:29 +08:00
|
|
|
// Compute the best distance following only up edges.
|
|
|
|
// Keep track of down edges, in case we can use them to improve on this.
|
|
|
|
for (const auto &S : Sources) {
|
|
|
|
auto Canonical = canonicalize(S.getKey());
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
dlog("Source {0} = {1}, MaxUp = {2}", Canonical, S.second.Cost,
|
|
|
|
S.second.MaxUpTraversals);
|
2018-07-03 16:09:29 +08:00
|
|
|
// Walk up to ancestors of this source, assigning cost.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef Rest = Canonical;
|
|
|
|
llvm::hash_code Hash = llvm::hash_value(Rest);
|
2018-07-03 16:09:29 +08:00
|
|
|
for (unsigned I = 0; !Rest.empty(); ++I) {
|
2019-01-07 23:45:19 +08:00
|
|
|
Rest = parent_path(Rest, llvm::sys::path::Style::posix);
|
|
|
|
auto NextHash = llvm::hash_value(Rest);
|
2018-07-04 16:27:28 +08:00
|
|
|
auto &Down = DownEdges[NextHash];
|
2019-01-07 23:45:19 +08:00
|
|
|
if (!llvm::is_contained(Down, Hash))
|
2018-10-08 01:21:08 +08:00
|
|
|
Down.push_back(Hash);
|
2018-07-03 16:09:29 +08:00
|
|
|
// We can't just break after MaxUpTraversals, must still set DownEdges.
|
|
|
|
if (I > S.getValue().MaxUpTraversals) {
|
|
|
|
if (Cache.find(Hash) != Cache.end())
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
unsigned Cost = S.getValue().Cost + I * Opts.UpCost;
|
|
|
|
auto R = Cache.try_emplace(Hash, Cost);
|
|
|
|
if (!R.second) {
|
|
|
|
if (Cost < R.first->second) {
|
|
|
|
R.first->second = Cost;
|
|
|
|
} else {
|
|
|
|
// If we're not the best way to get to this path, stop assigning.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Hash = NextHash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now propagate scores parent -> child if that's an improvement.
|
|
|
|
// BFS ensures we propagate down chains (must visit parents before children).
|
2019-01-07 23:45:19 +08:00
|
|
|
std::queue<llvm::hash_code> Next;
|
|
|
|
for (auto Child : DownEdges.lookup(llvm::hash_value(llvm::StringRef(""))))
|
2018-07-03 16:09:29 +08:00
|
|
|
Next.push(Child);
|
|
|
|
while (!Next.empty()) {
|
2018-10-16 18:41:17 +08:00
|
|
|
auto Parent = Next.front();
|
|
|
|
Next.pop();
|
|
|
|
auto ParentCost = Cache.lookup(Parent);
|
|
|
|
for (auto Child : DownEdges.lookup(Parent)) {
|
|
|
|
if (Parent != RootHash || Opts.AllowDownTraversalFromRoot) {
|
|
|
|
auto &ChildCost =
|
|
|
|
Cache.try_emplace(Child, Unreachable).first->getSecond();
|
|
|
|
if (ParentCost + Opts.DownCost < ChildCost)
|
|
|
|
ChildCost = ParentCost + Opts.DownCost;
|
|
|
|
}
|
2018-07-03 16:09:29 +08:00
|
|
|
Next.push(Child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
unsigned FileDistance::distance(llvm::StringRef Path) {
|
2018-07-03 16:09:29 +08:00
|
|
|
auto Canonical = canonicalize(Path);
|
2018-08-31 16:19:50 +08:00
|
|
|
unsigned Cost = Unreachable;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallVector<llvm::hash_code, 16> Ancestors;
|
2018-07-03 16:09:29 +08:00
|
|
|
// Walk up ancestors until we find a path we know the distance for.
|
2019-01-07 23:45:19 +08:00
|
|
|
for (llvm::StringRef Rest = Canonical; !Rest.empty();
|
|
|
|
Rest = parent_path(Rest, llvm::sys::path::Style::posix)) {
|
|
|
|
auto Hash = llvm::hash_value(Rest);
|
2018-10-16 18:41:17 +08:00
|
|
|
if (Hash == RootHash && !Ancestors.empty() &&
|
|
|
|
!Opts.AllowDownTraversalFromRoot) {
|
|
|
|
Cost = Unreachable;
|
|
|
|
break;
|
|
|
|
}
|
2018-07-03 16:09:29 +08:00
|
|
|
auto It = Cache.find(Hash);
|
|
|
|
if (It != Cache.end()) {
|
|
|
|
Cost = It->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ancestors.push_back(Hash);
|
|
|
|
}
|
|
|
|
// Now we know the costs for (known node, queried node].
|
|
|
|
// Fill these in, walking down the directory tree.
|
2019-01-07 23:45:19 +08:00
|
|
|
for (llvm::hash_code Hash : llvm::reverse(Ancestors)) {
|
2018-08-31 16:19:50 +08:00
|
|
|
if (Cost != Unreachable)
|
2018-07-03 16:09:29 +08:00
|
|
|
Cost += Opts.DownCost;
|
|
|
|
Cache.try_emplace(Hash, Cost);
|
|
|
|
}
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
dlog("distance({0} = {1})", Path, Cost);
|
2018-07-03 16:09:29 +08:00
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
unsigned URIDistance::distance(llvm::StringRef URI) {
|
|
|
|
auto R = Cache.try_emplace(llvm::hash_value(URI), FileDistance::Unreachable);
|
2018-07-03 16:09:29 +08:00
|
|
|
if (!R.second)
|
|
|
|
return R.first->getSecond();
|
|
|
|
if (auto U = clangd::URI::parse(URI)) {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
dlog("distance({0} = {1})", URI, U->body());
|
2018-07-03 16:09:29 +08:00
|
|
|
R.first->second = forScheme(U->scheme()).distance(U->body());
|
|
|
|
} else {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
log("URIDistance::distance() of unparseable {0}: {1}", URI, U.takeError());
|
2018-07-03 16:09:29 +08:00
|
|
|
}
|
|
|
|
return R.first->second;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
FileDistance &URIDistance::forScheme(llvm::StringRef Scheme) {
|
2018-07-03 16:09:29 +08:00
|
|
|
auto &Delegate = ByScheme[Scheme];
|
|
|
|
if (!Delegate) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<SourceParams> SchemeSources;
|
2018-07-03 16:09:29 +08:00
|
|
|
for (const auto &Source : Sources) {
|
|
|
|
if (auto U = clangd::URI::create(Source.getKey(), Scheme))
|
|
|
|
SchemeSources.try_emplace(U->body(), Source.getValue());
|
|
|
|
else
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::consumeError(U.takeError());
|
2018-07-03 16:09:29 +08:00
|
|
|
}
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
dlog("FileDistance for scheme {0}: {1}/{2} sources", Scheme,
|
|
|
|
SchemeSources.size(), Sources.size());
|
2018-07-03 16:09:29 +08:00
|
|
|
Delegate.reset(new FileDistance(std::move(SchemeSources), Opts));
|
|
|
|
}
|
|
|
|
return *Delegate;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
static std::pair<std::string, int> scopeToPath(llvm::StringRef Scope) {
|
|
|
|
llvm::SmallVector<llvm::StringRef, 4> Split;
|
2018-10-17 19:19:02 +08:00
|
|
|
Scope.split(Split, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
|
2019-01-07 23:45:19 +08:00
|
|
|
return {"/" + llvm::join(Split, "/"), Split.size()};
|
2018-10-17 19:19:02 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
static FileDistance
|
|
|
|
createScopeFileDistance(llvm::ArrayRef<std::string> QueryScopes) {
|
2018-10-17 19:19:02 +08:00
|
|
|
FileDistanceOptions Opts;
|
|
|
|
Opts.UpCost = 2;
|
|
|
|
Opts.DownCost = 4;
|
|
|
|
Opts.AllowDownTraversalFromRoot = false;
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<SourceParams> Sources;
|
|
|
|
llvm::StringRef Preferred =
|
|
|
|
QueryScopes.empty() ? "" : QueryScopes.front().c_str();
|
|
|
|
for (llvm::StringRef S : QueryScopes) {
|
2018-10-17 19:19:02 +08:00
|
|
|
SourceParams Param;
|
|
|
|
// Penalize the global scope even it's preferred, as all projects can define
|
|
|
|
// symbols in it, and there is pattern where using-namespace is used in
|
|
|
|
// place of enclosing namespaces (e.g. in implementation files).
|
|
|
|
if (S == Preferred)
|
2018-11-26 20:12:01 +08:00
|
|
|
Param.Cost = S == "" ? 4 : 0;
|
2018-10-17 19:19:02 +08:00
|
|
|
else if (Preferred.startswith(S) && !S.empty())
|
|
|
|
continue; // just rely on up-traversals.
|
|
|
|
else
|
2018-11-26 20:12:01 +08:00
|
|
|
Param.Cost = S == "" ? 6 : 2;
|
2018-10-17 19:19:02 +08:00
|
|
|
auto Path = scopeToPath(S);
|
|
|
|
// The global namespace is not 'near' its children.
|
|
|
|
Param.MaxUpTraversals = std::max(Path.second - 1, 0);
|
|
|
|
Sources[Path.first] = std::move(Param);
|
|
|
|
}
|
|
|
|
return FileDistance(Sources, Opts);
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
ScopeDistance::ScopeDistance(llvm::ArrayRef<std::string> QueryScopes)
|
2018-10-17 19:19:02 +08:00
|
|
|
: Distance(createScopeFileDistance(QueryScopes)) {}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
unsigned ScopeDistance::distance(llvm::StringRef SymbolScope) {
|
2018-10-17 19:19:02 +08:00
|
|
|
return Distance.distance(scopeToPath(SymbolScope).first);
|
|
|
|
}
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|