forked from OSchip/llvm-project
[clangd] Allow using customized include path in URI.
Summary: Calculating the include path from absolute file path does not always work for all build system, e.g. bazel uses symlink as the build working directory. The absolute file path from editor and clang is diverged from each other. We need to address it properly in build sysmtem integration. This patch worksarounds the issue by providing a hook in URI which allows clients to provide their customized include path. Reviewers: sammccall Subscribers: klimek, ilya-biryukov, jkorous-apple, ioeric, MaskRay, cfe-commits Differential Revision: https://reviews.llvm.org/D45426 llvm-svn: 329578
This commit is contained in:
parent
935474fef5
commit
e9a5a2f10c
clang-tools-extra
|
@ -286,6 +286,13 @@ static llvm::Expected<HeaderFile> toHeaderFile(StringRef Header,
|
||||||
auto U = URI::parse(Header);
|
auto U = URI::parse(Header);
|
||||||
if (!U)
|
if (!U)
|
||||||
return U.takeError();
|
return U.takeError();
|
||||||
|
|
||||||
|
auto IncludePath = URI::includeSpelling(*U);
|
||||||
|
if (!IncludePath)
|
||||||
|
return IncludePath.takeError();
|
||||||
|
if (!IncludePath->empty())
|
||||||
|
return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
|
||||||
|
|
||||||
auto Resolved = URI::resolve(*U, HintPath);
|
auto Resolved = URI::resolve(*U, HintPath);
|
||||||
if (!Resolved)
|
if (!Resolved)
|
||||||
return Resolved.takeError();
|
return Resolved.takeError();
|
||||||
|
|
|
@ -196,5 +196,12 @@ llvm::Expected<std::string> URI::resolve(const URI &Uri,
|
||||||
return S->get()->getAbsolutePath(Uri.Authority, Uri.Body, HintPath);
|
return S->get()->getAbsolutePath(Uri.Authority, Uri.Body, HintPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
llvm::Expected<std::string> URI::includeSpelling(const URI &Uri) {
|
||||||
|
auto S = findSchemeByName(Uri.Scheme);
|
||||||
|
if (!S)
|
||||||
|
return S.takeError();
|
||||||
|
return S->get()->getIncludeSpelling(Uri);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace clangd
|
} // namespace clangd
|
||||||
} // namespace clang
|
} // namespace clang
|
||||||
|
|
|
@ -60,6 +60,16 @@ public:
|
||||||
static llvm::Expected<std::string> resolve(const URI &U,
|
static llvm::Expected<std::string> resolve(const URI &U,
|
||||||
llvm::StringRef HintPath = "");
|
llvm::StringRef HintPath = "");
|
||||||
|
|
||||||
|
/// 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);
|
||||||
|
|
||||||
friend bool operator==(const URI &LHS, const URI &RHS) {
|
friend bool operator==(const URI &LHS, const URI &RHS) {
|
||||||
return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) ==
|
return std::tie(LHS.Scheme, LHS.Authority, LHS.Body) ==
|
||||||
std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
|
std::tie(RHS.Scheme, RHS.Authority, RHS.Body);
|
||||||
|
@ -94,6 +104,13 @@ public:
|
||||||
|
|
||||||
virtual llvm::Expected<URI>
|
virtual llvm::Expected<URI>
|
||||||
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const = 0;
|
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const = 0;
|
||||||
|
|
||||||
|
/// Returns the include path of the file (e.g. <path>, "path"), which can be
|
||||||
|
/// #included directly. See URI::includeSpelling for details.
|
||||||
|
virtual llvm::Expected<std::string>
|
||||||
|
getIncludeSpelling(const URI& U) const {
|
||||||
|
return ""; // no customized include path for this scheme.
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// By default, a "file" scheme is supported where URI paths are always absolute
|
/// By default, a "file" scheme is supported where URI paths are always absolute
|
||||||
|
|
|
@ -152,6 +152,28 @@ protected:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constexpr const char* ClangdTestScheme = "ClangdTests";
|
||||||
|
class TestURIScheme : public URIScheme {
|
||||||
|
public:
|
||||||
|
llvm::Expected<std::string>
|
||||||
|
getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body,
|
||||||
|
llvm::StringRef /*HintPath*/) const override {
|
||||||
|
llvm_unreachable("ClangdTests never makes absolute path.");
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::Expected<URI>
|
||||||
|
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override {
|
||||||
|
llvm_unreachable("ClangdTest never creates a test URI.");
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::Expected<std::string> getIncludeSpelling(const URI &U) const override {
|
||||||
|
return ("\"" + U.body().trim("/") + "\"").str();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static URISchemeRegistry::Add<TestURIScheme>
|
||||||
|
X(ClangdTestScheme, "Test scheme for ClangdTests.");
|
||||||
|
|
||||||
TEST_F(ClangdVFSTest, Parse) {
|
TEST_F(ClangdVFSTest, Parse) {
|
||||||
// FIXME: figure out a stable format for AST dumps, so that we can check the
|
// FIXME: figure out a stable format for AST dumps, so that we can check the
|
||||||
// output of the dump itself is equal to the expected one, not just that it's
|
// output of the dump itself is equal to the expected one, not just that it's
|
||||||
|
@ -961,6 +983,10 @@ void f() {}
|
||||||
/*Preferred=*/"<Y.h>", "<Y.h>"));
|
/*Preferred=*/"<Y.h>", "<Y.h>"));
|
||||||
EXPECT_TRUE(Inserted(OriginalHeader, PreferredHeader, "\"Y.h\""));
|
EXPECT_TRUE(Inserted(OriginalHeader, PreferredHeader, "\"Y.h\""));
|
||||||
EXPECT_TRUE(Inserted("<y.h>", PreferredHeader, "\"Y.h\""));
|
EXPECT_TRUE(Inserted("<y.h>", PreferredHeader, "\"Y.h\""));
|
||||||
|
auto TestURIHeader =
|
||||||
|
URI::parse(llvm::formatv("{0}:///x/y/z.h", ClangdTestScheme).str());
|
||||||
|
EXPECT_TRUE(static_cast<bool>(TestURIHeader));
|
||||||
|
EXPECT_TRUE(Inserted(TestURIHeader->toString(), "", "\"x/y/z.h\""));
|
||||||
|
|
||||||
// Check that includes are sorted.
|
// Check that includes are sorted.
|
||||||
const auto Expected = R"cpp(
|
const auto Expected = R"cpp(
|
||||||
|
|
Loading…
Reference in New Issue