clang-tools 20.0.0git
Public Member Functions | Static Public Member Functions | Friends | List of all members
clang::clangd::URI Class Reference

A URI describes the location of a source file. More...

#include <URI.h>

Public Member Functions

 URI (llvm::StringRef Scheme, llvm::StringRef Authority, llvm::StringRef Body)
 
llvm::StringRef scheme () const
 Returns decoded scheme e.g. "https".
 
llvm::StringRef authority () const
 Returns decoded authority e.g. "reviews.lvm.org".
 
llvm::StringRef body () const
 Returns decoded body e.g. "/D41946".
 
std::string toString () const
 Returns a string URI with all components percent-encoded.
 

Static Public Member Functions

static llvm::Expected< URIcreate (llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
 Creates a URI for a file in the given scheme.
 
static URI create (llvm::StringRef AbsolutePath)
 
static URI createFile (llvm::StringRef AbsolutePath)
 This creates a file:// URI for AbsolutePath. The path must be absolute.
 
static llvm::Expected< URIparse (llvm::StringRef Uri)
 Parse a URI string "<scheme>:[//<authority>/]<path>".
 
static llvm::Expected< std::string > resolve (const URI &U, llvm::StringRef HintPath="")
 Resolves the absolute path of U.
 
static llvm::Expected< std::string > resolve (llvm::StringRef FileURI, llvm::StringRef HintPath="")
 Same as above, in addition it parses the FileURI using URI::parse.
 
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 to get th canonical path.
 
static llvm::Expected< std::string > includeSpelling (const URI &U)
 Gets the preferred spelling of this file for #include, if there is one, e.g.
 

Friends

bool operator== (const URI &LHS, const URI &RHS)
 
bool operator< (const URI &LHS, const URI &RHS)
 

Detailed Description

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).

Definition at line 28 of file URI.h.

Constructor & Destructor Documentation

◆ URI()

clang::clangd::URI::URI ( llvm::StringRef  Scheme,
llvm::StringRef  Authority,
llvm::StringRef  Body 
)

Definition at line 152 of file URI.cpp.

Member Function Documentation

◆ authority()

llvm::StringRef clang::clangd::URI::authority ( ) const
inline

Returns decoded authority e.g. "reviews.lvm.org".

Definition at line 35 of file URI.h.

◆ body()

llvm::StringRef clang::clangd::URI::body ( ) const
inline

Returns decoded body e.g. "/D41946".

Definition at line 37 of file URI.h.

Referenced by clang::clangd::URIDistance::distance().

◆ create() [1/2]

URI clang::clangd::URI::create ( llvm::StringRef  AbsolutePath)
static

Definition at line 218 of file URI.cpp.

References createFile().

◆ create() [2/2]

llvm::Expected< URI > clang::clangd::URI::create ( llvm::StringRef  AbsolutePath,
llvm::StringRef  Scheme 
)
static

Creates a URI for a file in the given scheme.

Scheme must be registered. The URI is percent-encoded.

Definition at line 208 of file URI.cpp.

References clang::clangd::error().

Referenced by clang::clangd::FileIndex::updateMain().

◆ createFile()

URI clang::clangd::URI::createFile ( llvm::StringRef  AbsolutePath)
static

This creates a file:// URI for AbsolutePath. The path must be absolute.

Definition at line 237 of file URI.cpp.

Referenced by create(), clang::clangd::remote::Marshaller::relativePathToURI(), and clang::clangd::URIForFile::uri().

◆ includeSpelling()

llvm::Expected< std::string > clang::clangd::URI::includeSpelling ( const URI U)
static

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.

Definition at line 272 of file URI.cpp.

Referenced by clang::clangd::toHeaderFile().

◆ parse()

llvm::Expected< URI > clang::clangd::URI::parse ( llvm::StringRef  Uri)
static

Parse a URI string "<scheme>:[//<authority>/]<path>".

Percent-encoded characters in the URI will be decoded.

Definition at line 176 of file URI.cpp.

References clang::clangd::error(), and Pos.

Referenced by clang::clangd::URIDistance::distance(), clang::clangd::doPathMapping(), clang::clangd::fromJSON(), resolve(), clang::clangd::toHeaderFile(), and clang::clangd::remote::Marshaller::uriToRelativePath().

◆ resolve() [1/2]

llvm::Expected< std::string > clang::clangd::URI::resolve ( const URI U,
llvm::StringRef  HintPath = "" 
)
static

Resolves the absolute path of U.

If there is no matching scheme, or the URI is invalid in the scheme, this returns an error.

HintPath A related path, such as the current file or working directory, which can help disambiguate when the same file exists in many workspaces.

Definition at line 244 of file URI.cpp.

Referenced by clang::clangd::URIForFile::fromURI(), clang::clangd::getCorrespondingHeaderOrSource(), clang::clangd::indexToLSPLocation(), resolve(), and clang::clangd::toHeaderFile().

◆ resolve() [2/2]

llvm::Expected< std::string > clang::clangd::URI::resolve ( llvm::StringRef  FileURI,
llvm::StringRef  HintPath = "" 
)
static

Same as above, in addition it parses the FileURI using URI::parse.

Definition at line 197 of file URI.cpp.

References parse(), and resolve().

◆ resolvePath()

llvm::Expected< std::string > clang::clangd::URI::resolvePath ( llvm::StringRef  AbsPath,
llvm::StringRef  HintPath = "" 
)
static

Resolves AbsPath into a canonical path of its URI, by converting 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.

Definition at line 252 of file URI.cpp.

Referenced by clang::clangd::URIForFile::canonicalize().

◆ scheme()

llvm::StringRef clang::clangd::URI::scheme ( ) const
inline

Returns decoded scheme e.g. "https".

Definition at line 33 of file URI.h.

◆ toString()

std::string clang::clangd::URI::toString ( ) const

Returns a string URI with all components percent-encoded.

Definition at line 160 of file URI.cpp.

Referenced by clang::clangd::doPathMapping(), and clang::clangd::URIForFile::uri().

Friends And Related Function Documentation

◆ operator<

bool operator< ( const URI LHS,
const URI RHS 
)
friend

Definition at line 92 of file URI.h.

◆ operator==

bool operator== ( const URI LHS,
const URI RHS 
)
friend

Definition at line 87 of file URI.h.


The documentation for this class was generated from the following files: