Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/ConfigFragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,15 @@ struct Fragment {
/// AngledHeaders (i.e. a header matches a regex in both QuotedHeaders and
/// AngledHeaders), system headers use <> and non-system headers use "".
/// These can match any suffix of the header file in question.
/// Matching is performed against the header text, not its absolute path
/// Matching is performed against the absolute path of the header
/// within the project.
std::vector<Located<std::string>> QuotedHeaders;
/// List of regexes for headers that should always be included with a
/// <>-style include. By default, and in case of a conflict with
/// AngledHeaders (i.e. a header matches a regex in both QuotedHeaders and
/// AngledHeaders), system headers use <> and non-system headers use "".
/// These can match any suffix of the header file in question.
/// Matching is performed against the header text, not its absolute path
/// Matching is performed against the absolute path of the header
/// within the project.
std::vector<Located<std::string>> AngledHeaders;
};
Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/clangd/Headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,17 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
// FIXME: should we allow (some limited number of) "../header.h"?
if (llvm::sys::path::is_absolute(Suggested))
return std::nullopt;
auto HeaderPath = llvm::sys::path::convert_to_slash(InsertedHeader.File);
bool IsAngled = false;
for (auto &Filter : AngledHeaders) {
if (Filter(Suggested)) {
if (Filter(HeaderPath)) {
IsAngled = true;
break;
}
}
bool IsQuoted = false;
for (auto &Filter : QuotedHeaders) {
if (Filter(Suggested)) {
if (Filter(HeaderPath)) {
IsQuoted = true;
break;
}
Expand All @@ -324,7 +325,7 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
if (IsAngled && IsQuoted) {
elog("Header '{0}' matches both quoted and angled regexes, default will "
"be used.",
Suggested);
HeaderPath);
}
IsAngled = IsAngledByDefault;
}
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ TEST(CompletionTest, IncludeInsertionRespectsQuotedAngledConfig) {
{
Config C;
C.Style.AngledHeaders.push_back(
[](auto header) { return header == "bar.h"; });
[](auto header) { return header.contains("bar.h"); });
WithContextValue WithCfg(Config::Key, std::move(C));
Results = completions(TU, Test.point(), {Sym});
EXPECT_THAT(Results.Completions,
Expand All @@ -947,7 +947,7 @@ TEST(CompletionTest, IncludeInsertionRespectsQuotedAngledConfig) {
{
Config C;
C.Style.QuotedHeaders.push_back(
[](auto header) { return header == "bar.h"; });
[](auto header) { return header.contains("bar.h"); });
WithContextValue WithCfg(Config::Key, std::move(C));
Results = completions(TU, Test.point(), {Sym});
EXPECT_THAT(Results.Completions,
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/clangd/unittests/HeadersTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ TEST_F(HeadersTest, ShortenIncludesInSearchPathBracketed) {
EXPECT_EQ(calculate(BarHeader), "<sub/bar.h>");
}

TEST_F(HeadersTest, ShortenIncludesInSearchPathBracketedFilterByFullPath) {
// The filter receives the full path of the header, so it is able to filter by
// the parent directory, even if it is part of the include search path
AngledHeaders.push_back([](auto Path) {
llvm::Regex Pattern("sub/.*");
return Pattern.match(Path);
});
std::string BarHeader = testPath("sub/bar.h");
EXPECT_EQ(calculate(BarHeader), "<bar.h>");
}

TEST_F(HeadersTest, ShortenedIncludeNotInSearchPath) {
std::string BarHeader =
llvm::sys::path::convert_to_slash(testPath("sub-2/bar.h"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ struct Header {
}
StringRef verbatim() const { return std::get<Verbatim>(Storage); }

/// For phiscal files, either absolute path or path relative to the execution
/// For physical files, either absolute path or path relative to the execution
/// root. Otherwise just the spelling without surrounding quotes/brackets.
llvm::StringRef resolvedPath() const;

Expand Down