diff options
Diffstat (limited to 'clang-tools-extra/clangd/URI.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/URI.cpp | 89 |
1 files changed, 47 insertions, 42 deletions
diff --git a/clang-tools-extra/clangd/URI.cpp b/clang-tools-extra/clangd/URI.cpp index 64540c66798..888acb7f21d 100644 --- a/clang-tools-extra/clangd/URI.cpp +++ b/clang-tools-extra/clangd/URI.cpp @@ -20,13 +20,13 @@ LLVM_INSTANTIATE_REGISTRY(clang::clangd::URISchemeRegistry) -using namespace llvm; namespace clang { namespace clangd { namespace { -inline Error make_string_error(const Twine &Message) { - return make_error<StringError>(Message, inconvertibleErrorCode()); +inline llvm::Error make_string_error(const llvm::Twine &Message) { + return llvm::make_error<llvm::StringError>(Message, + llvm::inconvertibleErrorCode()); } /// \brief This manages file paths in the file system. All paths in the scheme @@ -35,8 +35,9 @@ inline Error make_string_error(const Twine &Message) { /// registry. class FileSystemScheme : public URIScheme { public: - Expected<std::string> getAbsolutePath(StringRef /*Authority*/, StringRef Body, - StringRef /*HintPath*/) const override { + llvm::Expected<std::string> + getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body, + llvm::StringRef /*HintPath*/) const override { if (!Body.startswith("/")) return make_string_error("File scheme: expect body to be an absolute " "path starting with '/': " + @@ -44,26 +45,26 @@ public: // For Windows paths e.g. /X: if (Body.size() > 2 && Body[0] == '/' && Body[2] == ':') Body.consume_front("/"); - SmallVector<char, 16> Path(Body.begin(), Body.end()); - sys::path::native(Path); + llvm::SmallVector<char, 16> Path(Body.begin(), Body.end()); + llvm::sys::path::native(Path); return std::string(Path.begin(), Path.end()); } - Expected<URI> uriFromAbsolutePath(StringRef AbsolutePath) const override { - using namespace llvm::sys; - + llvm::Expected<URI> + uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override { std::string Body; // For Windows paths e.g. X: if (AbsolutePath.size() > 1 && AbsolutePath[1] == ':') Body = "/"; - Body += path::convert_to_slash(AbsolutePath); + Body += llvm::sys::path::convert_to_slash(AbsolutePath); return URI("file", /*Authority=*/"", Body); } }; -Expected<std::unique_ptr<URIScheme>> findSchemeByName(StringRef Scheme) { +llvm::Expected<std::unique_ptr<URIScheme>> +findSchemeByName(llvm::StringRef Scheme) { if (Scheme == "file") - return make_unique<FileSystemScheme>(); + return llvm::make_unique<FileSystemScheme>(); for (auto I = URISchemeRegistry::begin(), E = URISchemeRegistry::end(); I != E; ++I) { @@ -96,12 +97,12 @@ bool shouldEscape(unsigned char C) { /// - Unreserved characters are not escaped. /// - Reserved characters always escaped with exceptions like '/'. /// - All other characters are escaped. -std::string percentEncode(StringRef Content) { +std::string percentEncode(llvm::StringRef Content) { std::string Result; - raw_string_ostream OS(Result); + llvm::raw_string_ostream OS(Result); for (unsigned char C : Content) if (shouldEscape(C)) - OS << '%' << format_hex_no_prefix(C, 2, /*Upper = */ true); + OS << '%' << llvm::format_hex_no_prefix(C, 2, /*Upper = */ true); else OS << C; @@ -110,16 +111,16 @@ std::string percentEncode(StringRef Content) { } /// Decodes a string according to percent-encoding. -std::string percentDecode(StringRef Content) { +std::string percentDecode(llvm::StringRef Content) { std::string Result; for (auto I = Content.begin(), E = Content.end(); I != E; ++I) { if (*I != '%') { Result += *I; continue; } - if (*I == '%' && I + 2 < Content.end() && isHexDigit(*(I + 1)) && - isHexDigit(*(I + 2))) { - Result.push_back(hexFromNibbles(*(I + 1), *(I + 2))); + if (*I == '%' && I + 2 < Content.end() && llvm::isHexDigit(*(I + 1)) && + llvm::isHexDigit(*(I + 2))) { + Result.push_back(llvm::hexFromNibbles(*(I + 1), *(I + 2))); I += 2; } else Result.push_back(*I); @@ -127,19 +128,20 @@ std::string percentDecode(StringRef Content) { return Result; } -bool isValidScheme(StringRef Scheme) { +bool isValidScheme(llvm::StringRef Scheme) { if (Scheme.empty()) return false; - if (!isAlpha(Scheme[0])) + if (!llvm::isAlpha(Scheme[0])) return false; return std::all_of(Scheme.begin() + 1, Scheme.end(), [](char C) { - return isAlnum(C) || C == '+' || C == '.' || C == '-'; + return llvm::isAlnum(C) || C == '+' || C == '.' || C == '-'; }); } } // namespace -URI::URI(StringRef Scheme, StringRef Authority, StringRef Body) +URI::URI(llvm::StringRef Scheme, llvm::StringRef Authority, + llvm::StringRef Body) : Scheme(Scheme), Authority(Authority), Body(Body) { assert(!Scheme.empty()); assert((Authority.empty() || Body.startswith("/")) && @@ -148,31 +150,31 @@ URI::URI(StringRef Scheme, StringRef Authority, StringRef Body) std::string URI::toString() const { std::string Result; - raw_string_ostream OS(Result); + llvm::raw_string_ostream OS(Result); OS << percentEncode(Scheme) << ":"; if (Authority.empty() && Body.empty()) return OS.str(); // If authority if empty, we only print body if it starts with "/"; otherwise, // the URI is invalid. - if (!Authority.empty() || StringRef(Body).startswith("/")) + if (!Authority.empty() || llvm::StringRef(Body).startswith("/")) OS << "//" << percentEncode(Authority); OS << percentEncode(Body); OS.flush(); return Result; } -Expected<URI> URI::parse(StringRef OrigUri) { +llvm::Expected<URI> URI::parse(llvm::StringRef OrigUri) { URI U; - StringRef Uri = OrigUri; + llvm::StringRef Uri = OrigUri; auto Pos = Uri.find(':'); - if (Pos == StringRef::npos) + if (Pos == llvm::StringRef::npos) return make_string_error("Scheme must be provided in URI: " + OrigUri); auto SchemeStr = Uri.substr(0, Pos); U.Scheme = percentDecode(SchemeStr); if (!isValidScheme(U.Scheme)) - return make_string_error( - formatv("Invalid scheme: {0} (decoded: {1})", SchemeStr, U.Scheme)); + return make_string_error(llvm::formatv("Invalid scheme: {0} (decoded: {1})", + SchemeStr, U.Scheme)); Uri = Uri.substr(Pos + 1); if (Uri.consume_front("//")) { Pos = Uri.find('/'); @@ -183,8 +185,9 @@ Expected<URI> URI::parse(StringRef OrigUri) { return U; } -Expected<URI> URI::create(StringRef AbsolutePath, StringRef Scheme) { - if (!sys::path::is_absolute(AbsolutePath)) +llvm::Expected<URI> URI::create(llvm::StringRef AbsolutePath, + llvm::StringRef Scheme) { + if (!llvm::sys::path::is_absolute(AbsolutePath)) return make_string_error("Not a valid absolute path: " + AbsolutePath); auto S = findSchemeByName(Scheme); if (!S) @@ -192,8 +195,8 @@ Expected<URI> URI::create(StringRef AbsolutePath, StringRef Scheme) { return S->get()->uriFromAbsolutePath(AbsolutePath); } -URI URI::create(StringRef AbsolutePath) { - if (!sys::path::is_absolute(AbsolutePath)) +URI URI::create(llvm::StringRef AbsolutePath) { + if (!llvm::sys::path::is_absolute(AbsolutePath)) llvm_unreachable( ("Not a valid absolute path: " + AbsolutePath).str().c_str()); for (auto &Entry : URISchemeRegistry::entries()) { @@ -202,7 +205,7 @@ URI URI::create(StringRef AbsolutePath) { // should be just skipped. if (!URI) { // Ignore the error. - consumeError(URI.takeError()); + llvm::consumeError(URI.takeError()); continue; } return std::move(*URI); @@ -211,22 +214,24 @@ URI URI::create(StringRef AbsolutePath) { return URI::createFile(AbsolutePath); } -URI URI::createFile(StringRef AbsolutePath) { +URI URI::createFile(llvm::StringRef AbsolutePath) { auto U = FileSystemScheme().uriFromAbsolutePath(AbsolutePath); if (!U) llvm_unreachable(llvm::toString(U.takeError()).c_str()); return std::move(*U); } -Expected<std::string> URI::resolve(const URI &Uri, StringRef HintPath) { +llvm::Expected<std::string> URI::resolve(const URI &Uri, + llvm::StringRef HintPath) { auto S = findSchemeByName(Uri.Scheme); if (!S) return S.takeError(); return S->get()->getAbsolutePath(Uri.Authority, Uri.Body, HintPath); } -Expected<std::string> URI::resolvePath(StringRef AbsPath, StringRef HintPath) { - if (!sys::path::is_absolute(AbsPath)) +llvm::Expected<std::string> URI::resolvePath(llvm::StringRef AbsPath, + llvm::StringRef HintPath) { + if (!llvm::sys::path::is_absolute(AbsPath)) llvm_unreachable(("Not a valid absolute path: " + AbsPath).str().c_str()); for (auto &Entry : URISchemeRegistry::entries()) { auto S = Entry.instantiate(); @@ -235,7 +240,7 @@ Expected<std::string> URI::resolvePath(StringRef AbsPath, StringRef HintPath) { // should be just skipped. if (!U) { // Ignore the error. - consumeError(U.takeError()); + llvm::consumeError(U.takeError()); continue; } return S->getAbsolutePath(U->Authority, U->Body, HintPath); @@ -244,7 +249,7 @@ Expected<std::string> URI::resolvePath(StringRef AbsPath, StringRef HintPath) { return AbsPath; } -Expected<std::string> URI::includeSpelling(const URI &Uri) { +llvm::Expected<std::string> URI::includeSpelling(const URI &Uri) { auto S = findSchemeByName(Uri.Scheme); if (!S) return S.takeError(); |

