2018-01-22 19:48:20 +08:00
|
|
|
//===--- URI.h - File URIs with schemes --------------------------*- 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-01-22 19:48:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHURI_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHURI_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/Registry.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
/// A URI describes the location of a source file.
|
|
|
|
/// In the simplest case, this is a "file" URI that directly encodes the
|
|
|
|
/// absolute path to a file. More abstract cases are possible: a shared index
|
|
|
|
/// service might expose repo:// URIs that are relative to the source control
|
|
|
|
/// root.
|
|
|
|
///
|
|
|
|
/// Clangd handles URIs of the form <scheme>:[//<authority>]<body>. It doesn't
|
|
|
|
/// further split the authority or body into constituent parts (e.g. query
|
|
|
|
/// strings is included in the body).
|
2018-01-29 23:37:46 +08:00
|
|
|
class URI {
|
2018-01-22 19:48:20 +08:00
|
|
|
public:
|
2018-01-29 23:37:46 +08:00
|
|
|
URI(llvm::StringRef Scheme, llvm::StringRef Authority, llvm::StringRef Body);
|
|
|
|
|
2018-01-22 19:48:20 +08:00
|
|
|
/// Returns decoded scheme e.g. "https"
|
|
|
|
llvm::StringRef scheme() const { return Scheme; }
|
|
|
|
/// Returns decoded authority e.g. "reviews.lvm.org"
|
|
|
|
llvm::StringRef authority() const { return Authority; }
|
|
|
|
/// Returns decoded body e.g. "/D41946"
|
|
|
|
llvm::StringRef body() const { return Body; }
|
|
|
|
|
|
|
|
/// Returns a string URI with all components percent-encoded.
|
|
|
|
std::string toString() const;
|
|
|
|
|
2018-01-29 23:37:46 +08:00
|
|
|
/// Creates a URI for a file in the given scheme. \p Scheme must be
|
2018-01-22 19:48:20 +08:00
|
|
|
/// registered. The URI is percent-encoded.
|
2018-01-29 23:37:46 +08:00
|
|
|
static llvm::Expected<URI> create(llvm::StringRef AbsolutePath,
|
|
|
|
llvm::StringRef Scheme);
|
|
|
|
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
// Similar to above except this picks a registered scheme that works. If none
|
|
|
|
// works, this falls back to "file" scheme.
|
|
|
|
static URI create(llvm::StringRef AbsolutePath);
|
2018-09-06 20:54:43 +08:00
|
|
|
|
2018-01-29 23:37:46 +08:00
|
|
|
/// This creates a file:// URI for \p AbsolutePath. The path must be absolute.
|
|
|
|
static URI createFile(llvm::StringRef AbsolutePath);
|
2018-01-22 19:48:20 +08:00
|
|
|
|
|
|
|
/// Parse a URI string "<scheme>:[//<authority>/]<path>". Percent-encoded
|
|
|
|
/// characters in the URI will be decoded.
|
2018-01-29 23:37:46 +08:00
|
|
|
static llvm::Expected<URI> parse(llvm::StringRef Uri);
|
2018-01-22 19:48:20 +08:00
|
|
|
|
|
|
|
/// Resolves the absolute path of \p U. If there is no matching scheme, or the
|
|
|
|
/// URI is invalid in the scheme, this returns an error.
|
|
|
|
///
|
|
|
|
/// \p HintPath A related path, such as the current file or working directory,
|
|
|
|
/// which can help disambiguate when the same file exists in many workspaces.
|
2018-01-29 23:37:46 +08:00
|
|
|
static llvm::Expected<std::string> resolve(const URI &U,
|
2018-01-22 19:48:20 +08:00
|
|
|
llvm::StringRef HintPath = "");
|
|
|
|
|
2019-09-23 22:39:37 +08:00
|
|
|
/// 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 = "");
|
|
|
|
|
2018-11-28 18:30:42 +08:00
|
|
|
/// 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
|
|
|
|
/// file path.
|
|
|
|
static llvm::Expected<std::string> resolvePath(llvm::StringRef AbsPath,
|
|
|
|
llvm::StringRef HintPath = "");
|
|
|
|
|
2018-04-09 23:09:44 +08:00
|
|
|
/// Gets the preferred spelling of this file for #include, if there is one,
|
|
|
|
/// e.g. <system_header.h>, "path/to/x.h".
|
|
|
|
///
|
|
|
|
/// This allows URI schemas to provide their customized include paths.
|
|
|
|
///
|
|
|
|
/// Returns an empty string if normal include-shortening based on the absolute
|
|
|
|
/// path should be used.
|
|
|
|
/// Fails if the URI is not valid in the schema.
|
|
|
|
static llvm::Expected<std::string> includeSpelling(const URI &U);
|
|
|
|
|
2018-01-29 23:37:46 +08:00
|
|
|
friend bool operator==(const URI &LHS, const URI &RHS) {
|
2018-01-22 19:48:20 +08:00
|
|
|
return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) ==
|
|
|
|
std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:37:46 +08:00
|
|
|
friend bool operator<(const URI &LHS, const URI &RHS) {
|
|
|
|
return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) <
|
|
|
|
std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:48:20 +08:00
|
|
|
private:
|
2018-01-29 23:37:46 +08:00
|
|
|
URI() = default;
|
2018-01-22 19:48:20 +08:00
|
|
|
|
|
|
|
std::string Scheme;
|
|
|
|
std::string Authority;
|
|
|
|
std::string Body;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// URIScheme is an extension point for teaching clangd to recognize a custom
|
|
|
|
/// URI scheme. This is expected to be implemented and exposed via the
|
|
|
|
/// URISchemeRegistry.
|
|
|
|
class URIScheme {
|
|
|
|
public:
|
|
|
|
virtual ~URIScheme() = default;
|
|
|
|
|
|
|
|
/// Returns the absolute path of the file corresponding to the URI
|
2018-01-29 23:37:46 +08:00
|
|
|
/// authority+body in the file system. See URI::resolve for semantics of
|
2018-01-22 19:48:20 +08:00
|
|
|
/// \p HintPath.
|
|
|
|
virtual llvm::Expected<std::string>
|
|
|
|
getAbsolutePath(llvm::StringRef Authority, llvm::StringRef Body,
|
|
|
|
llvm::StringRef HintPath) const = 0;
|
|
|
|
|
2018-01-29 23:37:46 +08:00
|
|
|
virtual llvm::Expected<URI>
|
2018-01-22 19:48:20 +08:00
|
|
|
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const = 0;
|
2018-04-09 23:09:44 +08:00
|
|
|
|
|
|
|
/// Returns the include path of the file (e.g. <path>, "path"), which can be
|
|
|
|
/// #included directly. See URI::includeSpelling for details.
|
2018-05-30 20:41:19 +08:00
|
|
|
virtual llvm::Expected<std::string> getIncludeSpelling(const URI &U) const {
|
|
|
|
return ""; // no customized include path for this scheme.
|
2018-04-09 23:09:44 +08:00
|
|
|
}
|
2018-01-22 19:48:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// By default, a "file" scheme is supported where URI paths are always absolute
|
|
|
|
/// in the file system.
|
|
|
|
typedef llvm::Registry<URIScheme> URISchemeRegistry;
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHURI_H
|