diff options
| author | Haojian Wu <hokein@google.com> | 2019-09-23 14:39:37 +0000 | 
|---|---|---|
| committer | Haojian Wu <hokein@google.com> | 2019-09-23 14:39:37 +0000 | 
| commit | b70323e5d35171ae32ea10ff49e86cdb4b118c30 (patch) | |
| tree | 2b01ec272559b3391658190787cd90760586311b | |
| parent | f97fdf5792c39fd2fe47e7c64e0e97adbf281b2b (diff) | |
| download | bcm5719-llvm-b70323e5d35171ae32ea10ff49e86cdb4b118c30.tar.gz bcm5719-llvm-b70323e5d35171ae32ea10ff49e86cdb4b118c30.zip  | |
[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
| -rw-r--r-- | clang-tools-extra/clangd/CodeComplete.cpp | 7 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/FindSymbols.cpp | 13 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/IncludeFixer.cpp | 6 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/URI.cpp | 11 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/URI.h | 4 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/index/Background.cpp | 8 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/index/BackgroundIndexLoader.cpp | 16 | 
7 files changed, 27 insertions, 38 deletions
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index b96636aaf63..2b6c3b46270 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -318,11 +318,8 @@ struct CodeCompletionBuilder {      // Turn absolute path into a literal string that can be #included.      auto Inserted = [&](llvm::StringRef Header)          -> llvm::Expected<std::pair<std::string, bool>> { -      auto DeclaringURI = -          URI::parse(C.IndexResult->CanonicalDeclaration.FileURI); -      if (!DeclaringURI) -        return DeclaringURI.takeError(); -      auto ResolvedDeclaring = URI::resolve(*DeclaringURI, FileName); +      auto ResolvedDeclaring = +          URI::resolve(C.IndexResult->CanonicalDeclaration.FileURI, FileName);        if (!ResolvedDeclaring)          return ResolvedDeclaring.takeError();        auto ResolvedInserted = toHeaderFile(Header, FileName); diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp index 287a8a4df0b..619f1a5cfdb 100644 --- a/clang-tools-extra/clangd/FindSymbols.cpp +++ b/clang-tools-extra/clangd/FindSymbols.cpp @@ -43,18 +43,11 @@ llvm::Expected<Location> symbolToLocation(const Symbol &Sym,                                            llvm::StringRef HintPath) {    // Prefer the definition over e.g. a function declaration in a header    auto &CD = Sym.Definition ? Sym.Definition : Sym.CanonicalDeclaration; -  auto Uri = URI::parse(CD.FileURI); -  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); +  auto Path = URI::resolve(CD.FileURI, HintPath);    if (!Path) {      return llvm::make_error<llvm::StringError>( -        formatv("Could not resolve path for URI '{0}' for symbol '{1}'.", -                Uri->toString(), Sym.Name), +        formatv("Could not resolve path for symbol '{0}': {1}", +                Sym.Name, llvm::toString(Path.takeError())),          llvm::inconvertibleErrorCode());    }    Location L; diff --git a/clang-tools-extra/clangd/IncludeFixer.cpp b/clang-tools-extra/clangd/IncludeFixer.cpp index 081dad83583..93dee2eabfc 100644 --- a/clang-tools-extra/clangd/IncludeFixer.cpp +++ b/clang-tools-extra/clangd/IncludeFixer.cpp @@ -144,10 +144,8 @@ std::vector<Fix> IncludeFixer::fixIncompleteType(const Type &T) const {  std::vector<Fix> IncludeFixer::fixesForSymbols(const SymbolSlab &Syms) const {    auto Inserted = [&](const Symbol &Sym, llvm::StringRef Header)        -> llvm::Expected<std::pair<std::string, bool>> { -    auto DeclaringURI = URI::parse(Sym.CanonicalDeclaration.FileURI); -    if (!DeclaringURI) -      return DeclaringURI.takeError(); -    auto ResolvedDeclaring = URI::resolve(*DeclaringURI, File); +    auto ResolvedDeclaring = +        URI::resolve(Sym.CanonicalDeclaration.FileURI, File);      if (!ResolvedDeclaring)        return ResolvedDeclaring.takeError();      auto ResolvedInserted = toHeaderFile(Header, File); diff --git a/clang-tools-extra/clangd/URI.cpp b/clang-tools-extra/clangd/URI.cpp index 9f5c92b684f..f8a9f59ca13 100644 --- a/clang-tools-extra/clangd/URI.cpp +++ b/clang-tools-extra/clangd/URI.cpp @@ -183,6 +183,17 @@ llvm::Expected<URI> URI::parse(llvm::StringRef OrigUri) {    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::StringRef Scheme) {    if (!llvm::sys::path::is_absolute(AbsolutePath)) diff --git a/clang-tools-extra/clangd/URI.h b/clang-tools-extra/clangd/URI.h index 110b39af663..5f120ef9e38 100644 --- a/clang-tools-extra/clangd/URI.h +++ b/clang-tools-extra/clangd/URI.h @@ -63,6 +63,10 @@ public:    static llvm::Expected<std::string> resolve(const URI &U,                                               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    /// \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 diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp index 9ea21fdf6c5..ff6a9e52268 100644 --- a/clang-tools-extra/clangd/index/Background.cpp +++ b/clang-tools-extra/clangd/index/Background.cpp @@ -69,13 +69,7 @@ public:    llvm::StringRef resolve(llvm::StringRef FileURI) {      auto I = URIToPathCache.try_emplace(FileURI);      if (I.second) { -      auto U = URI::parse(FileURI); -      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); +      auto Path = URI::resolve(FileURI, HintPath);        if (!Path) {          elog("Failed to resolve URI {0}: {1}", FileURI, Path.takeError());          assert(false && "Failed to resolve URI"); diff --git a/clang-tools-extra/clangd/index/BackgroundIndexLoader.cpp b/clang-tools-extra/clangd/index/BackgroundIndexLoader.cpp index 585d36716d2..ff690f90257 100644 --- a/clang-tools-extra/clangd/index/BackgroundIndexLoader.cpp +++ b/clang-tools-extra/clangd/index/BackgroundIndexLoader.cpp @@ -24,16 +24,6 @@ namespace clang {  namespace clangd {  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  /// inverse dependency mapping.  class BackgroundIndexLoader { @@ -79,9 +69,11 @@ BackgroundIndexLoader::loadShard(PathRef StartSourceFile, PathRef DependentTU) {    LS.Shard = std::move(Shard);    for (const auto &It : *LS.Shard->Sources) { -    auto AbsPath = uriToAbsolutePath(It.getKey(), StartSourceFile); -    if (!AbsPath) +    auto AbsPath = URI::resolve(It.getKey(), StartSourceFile); +    if (!AbsPath) { +      elog("Failed to resolve URI: {0}", AbsPath.takeError());        continue; +    }      // A shard contains only edges for non main-file sources.      if (*AbsPath != StartSourceFile) {        Edges.push_back(*AbsPath);  | 

