clang-tools 8.0.0

URI.h
Go to the documentation of this file.
1 //===--- URI.h - File URIs with schemes --------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHURI_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHURI_H
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/Registry.h"
16
17 namespace clang {
18 namespace clangd {
19
20 /// A URI describes the location of a source file.
21 /// In the simplest case, this is a "file" URI that directly encodes the
22 /// absolute path to a file. More abstract cases are possible: a shared index
23 /// service might expose repo:// URIs that are relative to the source control
24 /// root.
25 ///
26 /// Clangd handles URIs of the form <scheme>:[//<authority>]<body>. It doesn't
27 /// further split the authority or body into constituent parts (e.g. query
28 /// strings is included in the body).
29 class URI {
30 public:
31 URI(llvm::StringRef Scheme, llvm::StringRef Authority, llvm::StringRef Body);
32
33 /// Returns decoded scheme e.g. "https"
34 llvm::StringRef scheme() const { return Scheme; }
35 /// Returns decoded authority e.g. "reviews.lvm.org"
36 llvm::StringRef authority() const { return Authority; }
37 /// Returns decoded body e.g. "/D41946"
38 llvm::StringRef body() const { return Body; }
39
40 /// Returns a string URI with all components percent-encoded.
41 std::string toString() const;
42
43 /// Creates a URI for a file in the given scheme. \p Scheme must be
44 /// registered. The URI is percent-encoded.
45 static llvm::Expected<URI> create(llvm::StringRef AbsolutePath,
46 llvm::StringRef Scheme);
47
48 // Similar to above except this picks a registered scheme that works. If none
49 // works, this falls back to "file" scheme.
50 static URI create(llvm::StringRef AbsolutePath);
51
52 /// This creates a file:// URI for \p AbsolutePath. The path must be absolute.
53 static URI createFile(llvm::StringRef AbsolutePath);
54
55 /// Parse a URI string "<scheme>:[//<authority>/]<path>". Percent-encoded
56 /// characters in the URI will be decoded.
57 static llvm::Expected<URI> parse(llvm::StringRef Uri);
58
59 /// Resolves the absolute path of \p U. If there is no matching scheme, or the
60 /// URI is invalid in the scheme, this returns an error.
61 ///
62 /// \p HintPath A related path, such as the current file or working directory,
63 /// which can help disambiguate when the same file exists in many workspaces.
64 static llvm::Expected<std::string> resolve(const URI &U,
65 llvm::StringRef HintPath = "");
66
67 /// Resolves \p AbsPath into a canonical path of its URI, by converting
68 /// \p AbsPath to URI and resolving the URI to get th canonical path.
69 /// This ensures that paths with the same URI are resolved into consistent
70 /// file path.
71 static llvm::Expected<std::string> resolvePath(llvm::StringRef AbsPath,
72 llvm::StringRef HintPath = "");
73
74 /// Gets the preferred spelling of this file for #include, if there is one,
75 /// e.g. <system_header.h>, "path/to/x.h".
76 ///
77 /// This allows URI schemas to provide their customized include paths.
78 ///
79 /// Returns an empty string if normal include-shortening based on the absolute
80 /// path should be used.
81 /// Fails if the URI is not valid in the schema.
82 static llvm::Expected<std::string> includeSpelling(const URI &U);
83
84 friend bool operator==(const URI &LHS, const URI &RHS) {
85 return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) ==
86 std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
87 }
88
89 friend bool operator<(const URI &LHS, const URI &RHS) {
90 return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) <
91 std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
92 }
93
94 private:
95 URI() = default;
96
97 std::string Scheme;
98 std::string Authority;
99 std::string Body;
100 };
101
102 /// URIScheme is an extension point for teaching clangd to recognize a custom
103 /// URI scheme. This is expected to be implemented and exposed via the
104 /// URISchemeRegistry.
105 class URIScheme {
106 public:
107 virtual ~URIScheme() = default;
108
109 /// Returns the absolute path of the file corresponding to the URI
110 /// authority+body in the file system. See URI::resolve for semantics of
111 /// \p HintPath.
112 virtual llvm::Expected<std::string>
113 getAbsolutePath(llvm::StringRef Authority, llvm::StringRef Body,
114 llvm::StringRef HintPath) const = 0;
115
116 virtual llvm::Expected<URI>
117 uriFromAbsolutePath(llvm::StringRef AbsolutePath) const = 0;
118
119 /// Returns the include path of the file (e.g. <path>, "path"), which can be
120 /// #included directly. See URI::includeSpelling for details.
121 virtual llvm::Expected<std::string> getIncludeSpelling(const URI &U) const {
122 return ""; // no customized include path for this scheme.
123 }
124 };
125
126 /// By default, a "file" scheme is supported where URI paths are always absolute
127 /// in the file system.
128 typedef llvm::Registry<URIScheme> URISchemeRegistry;
129
130 } // namespace clangd
131 } // namespace clang
132
133 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PATHURI_H
clang::clangd::URI::operator==
friend bool operator==(const URI &LHS, const URI &RHS)
Definition: URI.h:84
clang::clangd::URIScheme
URIScheme is an extension point for teaching clangd to recognize a custom URI scheme.
Definition: URI.h:105
clang::clangd::URIScheme::getIncludeSpelling
virtual llvm::Expected< std::string > getIncludeSpelling(const URI &U) const
Returns the include path of the file (e.g.
Definition: URI.h:121
clang::clangd::URI::authority
llvm::StringRef authority() const
Returns decoded authority e.g. "reviews.lvm.org".
Definition: URI.h:36
clang::clangd::URI::createFile
static URI createFile(llvm::StringRef AbsolutePath)
This creates a file:// URI for AbsolutePath. The path must be absolute.
Definition: URI.cpp:217
clang::clangd::URISchemeRegistry
llvm::Registry< URIScheme > URISchemeRegistry
By default, a "file" scheme is supported where URI paths are always absolute in the file system...
Definition: URI.h:128
clang::clangd::URI::scheme
llvm::StringRef scheme() const
Returns decoded scheme e.g. "https".
Definition: URI.h:34
clang::clangd::URI::resolvePath
static llvm::Expected< std::string > resolvePath(llvm::StringRef AbsPath, llvm::StringRef HintPath="")
Resolves AbsPath into a canonical path of its URI, by converting AbsPath to URI and resolving the URI...
Definition: URI.cpp:232
clang::clangd::URI::body
llvm::StringRef body() const
Returns decoded body e.g. "/D41946".
Definition: URI.h:38
clang::clangd::URI::create
static llvm::Expected< URI > create(llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
Creates a URI for a file in the given scheme.
Definition: URI.cpp:188
clang::clangd::URI::operator<
friend bool operator<(const URI &LHS, const URI &RHS)
Definition: URI.h:89
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::URI
A URI describes the location of a source file.
Definition: URI.h:29
clang::clangd::URI::resolve
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
Definition: URI.cpp:224
clang::clangd::URI::includeSpelling
static llvm::Expected< std::string > includeSpelling(const URI &U)
Gets the preferred spelling of this file for #include, if there is one, e.g.
Definition: URI.cpp:252
clang::clangd::URI::parse
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Parse a URI string "<scheme>:[//<authority>/]<path>".
Definition: URI.cpp:166
clang::clangd::URI::toString
std::string toString() const
Returns a string URI with all components percent-encoded.
Definition: URI.cpp:151