forked from OSchip/llvm-project
[clangd] Simplify the callside of URI::resolve, NFC.
Summary: - Add a overrloded URI::resolve, which accepts a string URI; - also fixed some callside that don't check the error; Reviewers: kadircet Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D67916 llvm-svn: 372617
This commit is contained in:
parent
f97fdf5792
commit
b70323e5d3
|
@ -318,11 +318,8 @@ struct CodeCompletionBuilder {
|
||||||
// Turn absolute path into a literal string that can be #included.
|
// Turn absolute path into a literal string that can be #included.
|
||||||
auto Inserted = [&](llvm::StringRef Header)
|
auto Inserted = [&](llvm::StringRef Header)
|
||||||
-> llvm::Expected<std::pair<std::string, bool>> {
|
-> llvm::Expected<std::pair<std::string, bool>> {
|
||||||
auto DeclaringURI =
|
auto ResolvedDeclaring =
|
||||||
URI::parse(C.IndexResult->CanonicalDeclaration.FileURI);
|
URI::resolve(C.IndexResult->CanonicalDeclaration.FileURI, FileName);
|
||||||
if (!DeclaringURI)
|
|
||||||
return DeclaringURI.takeError();
|
|
||||||
auto ResolvedDeclaring = URI::resolve(*DeclaringURI, FileName);
|
|
||||||
if (!ResolvedDeclaring)
|
if (!ResolvedDeclaring)
|
||||||
return ResolvedDeclaring.takeError();
|
return ResolvedDeclaring.takeError();
|
||||||
auto ResolvedInserted = toHeaderFile(Header, FileName);
|
auto ResolvedInserted = toHeaderFile(Header, FileName);
|
||||||
|
|
|
@ -43,18 +43,11 @@ llvm::Expected<Location> symbolToLocation(const Symbol &Sym,
|
||||||
llvm::StringRef HintPath) {
|
llvm::StringRef HintPath) {
|
||||||
// Prefer the definition over e.g. a function declaration in a header
|
// Prefer the definition over e.g. a function declaration in a header
|
||||||
auto &CD = Sym.Definition ? Sym.Definition : Sym.CanonicalDeclaration;
|
auto &CD = Sym.Definition ? Sym.Definition : Sym.CanonicalDeclaration;
|
||||||
auto Uri = URI::parse(CD.FileURI);
|
auto Path = URI::resolve(CD.FileURI, HintPath);
|
||||||
if (!Uri) {
|
|
||||||
return llvm::make_error<llvm::StringError>(
|
|
||||||
formatv("Could not parse URI '{0}' for symbol '{1}'.", CD.FileURI,
|
|
||||||
Sym.Name),
|
|
||||||
llvm::inconvertibleErrorCode());
|
|
||||||
}
|
|
||||||
auto Path = URI::resolve(*Uri, HintPath);
|
|
||||||
if (!Path) {
|
if (!Path) {
|
||||||
return llvm::make_error<llvm::StringError>(
|
return llvm::make_error<llvm::StringError>(
|
||||||
formatv("Could not resolve path for URI '{0}' for symbol '{1}'.",
|
formatv("Could not resolve path for symbol '{0}': {1}",
|
||||||
Uri->toString(), Sym.Name),
|
Sym.Name, llvm::toString(Path.takeError())),
|
||||||
llvm::inconvertibleErrorCode());
|
llvm::inconvertibleErrorCode());
|
||||||
}
|
}
|
||||||
Location L;
|
Location L;
|
||||||
|
|
|
@ -144,10 +144,8 @@ std::vector<Fix> IncludeFixer::fixIncompleteType(const Type &T) const {
|
||||||
std::vector<Fix> IncludeFixer::fixesForSymbols(const SymbolSlab &Syms) const {
|
std::vector<Fix> IncludeFixer::fixesForSymbols(const SymbolSlab &Syms) const {
|
||||||
auto Inserted = [&](const Symbol &Sym, llvm::StringRef Header)
|
auto Inserted = [&](const Symbol &Sym, llvm::StringRef Header)
|
||||||
-> llvm::Expected<std::pair<std::string, bool>> {
|
-> llvm::Expected<std::pair<std::string, bool>> {
|
||||||
auto DeclaringURI = URI::parse(Sym.CanonicalDeclaration.FileURI);
|
auto ResolvedDeclaring =
|
||||||
if (!DeclaringURI)
|
URI::resolve(Sym.CanonicalDeclaration.FileURI, File);
|
||||||
return DeclaringURI.takeError();
|
|
||||||
auto ResolvedDeclaring = URI::resolve(*DeclaringURI, File);
|
|
||||||
if (!ResolvedDeclaring)
|
if (!ResolvedDeclaring)
|
||||||
return ResolvedDeclaring.takeError();
|
return ResolvedDeclaring.takeError();
|
||||||
auto ResolvedInserted = toHeaderFile(Header, File);
|
auto ResolvedInserted = toHeaderFile(Header, File);
|
||||||
|
|
|
@ -183,6 +183,17 @@ llvm::Expected<URI> URI::parse(llvm::StringRef OrigUri) {
|
||||||
return U;
|
return U;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
llvm::Expected<std::string> URI::resolve(llvm::StringRef FileURI,
|
||||||
|
llvm::StringRef HintPath) {
|
||||||
|
auto Uri = URI::parse(FileURI);
|
||||||
|
if (!Uri)
|
||||||
|
return Uri.takeError();
|
||||||
|
auto Path = URI::resolve(*Uri, HintPath);
|
||||||
|
if (!Path)
|
||||||
|
return Path.takeError();
|
||||||
|
return *Path;
|
||||||
|
}
|
||||||
|
|
||||||
llvm::Expected<URI> URI::create(llvm::StringRef AbsolutePath,
|
llvm::Expected<URI> URI::create(llvm::StringRef AbsolutePath,
|
||||||
llvm::StringRef Scheme) {
|
llvm::StringRef Scheme) {
|
||||||
if (!llvm::sys::path::is_absolute(AbsolutePath))
|
if (!llvm::sys::path::is_absolute(AbsolutePath))
|
||||||
|
|
|
@ -63,6 +63,10 @@ public:
|
||||||
static llvm::Expected<std::string> resolve(const URI &U,
|
static llvm::Expected<std::string> resolve(const URI &U,
|
||||||
llvm::StringRef HintPath = "");
|
llvm::StringRef HintPath = "");
|
||||||
|
|
||||||
|
/// Same as above, in addition it parses the \p FileURI using URI::parse.
|
||||||
|
static llvm::Expected<std::string> resolve(llvm::StringRef FileURI,
|
||||||
|
llvm::StringRef HintPath = "");
|
||||||
|
|
||||||
/// Resolves \p AbsPath into a canonical path of its URI, by converting
|
/// Resolves \p AbsPath into a canonical path of its URI, by converting
|
||||||
/// \p AbsPath to URI and resolving the URI to get th canonical path.
|
/// \p AbsPath to URI and resolving the URI to get th canonical path.
|
||||||
/// This ensures that paths with the same URI are resolved into consistent
|
/// This ensures that paths with the same URI are resolved into consistent
|
||||||
|
|
|
@ -69,13 +69,7 @@ public:
|
||||||
llvm::StringRef resolve(llvm::StringRef FileURI) {
|
llvm::StringRef resolve(llvm::StringRef FileURI) {
|
||||||
auto I = URIToPathCache.try_emplace(FileURI);
|
auto I = URIToPathCache.try_emplace(FileURI);
|
||||||
if (I.second) {
|
if (I.second) {
|
||||||
auto U = URI::parse(FileURI);
|
auto Path = URI::resolve(FileURI, HintPath);
|
||||||
if (!U) {
|
|
||||||
elog("Failed to parse URI {0}: {1}", FileURI, U.takeError());
|
|
||||||
assert(false && "Failed to parse URI");
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
auto Path = URI::resolve(*U, HintPath);
|
|
||||||
if (!Path) {
|
if (!Path) {
|
||||||
elog("Failed to resolve URI {0}: {1}", FileURI, Path.takeError());
|
elog("Failed to resolve URI {0}: {1}", FileURI, Path.takeError());
|
||||||
assert(false && "Failed to resolve URI");
|
assert(false && "Failed to resolve URI");
|
||||||
|
|
|
@ -24,16 +24,6 @@ namespace clang {
|
||||||
namespace clangd {
|
namespace clangd {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
llvm::Optional<Path> uriToAbsolutePath(llvm::StringRef URI, PathRef HintPath) {
|
|
||||||
auto U = URI::parse(URI);
|
|
||||||
if (!U)
|
|
||||||
return llvm::None;
|
|
||||||
auto AbsolutePath = URI::resolve(*U, HintPath);
|
|
||||||
if (!AbsolutePath)
|
|
||||||
return llvm::None;
|
|
||||||
return *AbsolutePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A helper class to cache BackgroundIndexStorage operations and keep the
|
/// A helper class to cache BackgroundIndexStorage operations and keep the
|
||||||
/// inverse dependency mapping.
|
/// inverse dependency mapping.
|
||||||
class BackgroundIndexLoader {
|
class BackgroundIndexLoader {
|
||||||
|
@ -79,9 +69,11 @@ BackgroundIndexLoader::loadShard(PathRef StartSourceFile, PathRef DependentTU) {
|
||||||
|
|
||||||
LS.Shard = std::move(Shard);
|
LS.Shard = std::move(Shard);
|
||||||
for (const auto &It : *LS.Shard->Sources) {
|
for (const auto &It : *LS.Shard->Sources) {
|
||||||
auto AbsPath = uriToAbsolutePath(It.getKey(), StartSourceFile);
|
auto AbsPath = URI::resolve(It.getKey(), StartSourceFile);
|
||||||
if (!AbsPath)
|
if (!AbsPath) {
|
||||||
|
elog("Failed to resolve URI: {0}", AbsPath.takeError());
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
// A shard contains only edges for non main-file sources.
|
// A shard contains only edges for non main-file sources.
|
||||||
if (*AbsPath != StartSourceFile) {
|
if (*AbsPath != StartSourceFile) {
|
||||||
Edges.push_back(*AbsPath);
|
Edges.push_back(*AbsPath);
|
||||||
|
|
Loading…
Reference in New Issue