Skip to content

Commit 69f6e74

Browse files
committed
Use resolved path when filtering in IncludeInserter
1 parent 351b38f commit 69f6e74

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,15 @@ struct Fragment {
315315
/// AngledHeaders (i.e. a header matches a regex in both QuotedHeaders and
316316
/// AngledHeaders), system headers use <> and non-system headers use "".
317317
/// These can match any suffix of the header file in question.
318-
/// Matching is performed against the header text, not its absolute path
318+
/// Matching is performed against the absolute path of the header
319319
/// within the project.
320320
std::vector<Located<std::string>> QuotedHeaders;
321321
/// List of regexes for headers that should always be included with a
322322
/// <>-style include. By default, and in case of a conflict with
323323
/// AngledHeaders (i.e. a header matches a regex in both QuotedHeaders and
324324
/// AngledHeaders), system headers use <> and non-system headers use "".
325325
/// These can match any suffix of the header file in question.
326-
/// Matching is performed against the header text, not its absolute path
326+
/// Matching is performed against the absolute path of the header
327327
/// within the project.
328328
std::vector<Located<std::string>> AngledHeaders;
329329
};

clang-tools-extra/clangd/Headers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,14 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
306306
return std::nullopt;
307307
bool IsAngled = false;
308308
for (auto &Filter : AngledHeaders) {
309-
if (Filter(Suggested)) {
309+
if (Filter(InsertedHeader.File)) {
310310
IsAngled = true;
311311
break;
312312
}
313313
}
314314
bool IsQuoted = false;
315315
for (auto &Filter : QuotedHeaders) {
316-
if (Filter(Suggested)) {
316+
if (Filter(InsertedHeader.File)) {
317317
IsQuoted = true;
318318
break;
319319
}
@@ -324,7 +324,7 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
324324
if (IsAngled && IsQuoted) {
325325
elog("Header '{0}' matches both quoted and angled regexes, default will "
326326
"be used.",
327-
Suggested);
327+
InsertedHeader.File);
328328
}
329329
IsAngled = IsAngledByDefault;
330330
}

clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ TEST(CompletionTest, IncludeInsertionRespectsQuotedAngledConfig) {
938938
{
939939
Config C;
940940
C.Style.AngledHeaders.push_back(
941-
[](auto header) { return header == "bar.h"; });
941+
[](auto header) { return header.contains("bar.h"); });
942942
WithContextValue WithCfg(Config::Key, std::move(C));
943943
Results = completions(TU, Test.point(), {Sym});
944944
EXPECT_THAT(Results.Completions,
@@ -947,7 +947,7 @@ TEST(CompletionTest, IncludeInsertionRespectsQuotedAngledConfig) {
947947
{
948948
Config C;
949949
C.Style.QuotedHeaders.push_back(
950-
[](auto header) { return header == "bar.h"; });
950+
[](auto header) { return header.contains("bar.h"); });
951951
WithContextValue WithCfg(Config::Key, std::move(C));
952952
Results = completions(TU, Test.point(), {Sym});
953953
EXPECT_THAT(Results.Completions,

clang-tools-extra/clangd/unittests/HeadersTests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ TEST_F(HeadersTest, ShortenIncludesInSearchPathBracketed) {
344344
EXPECT_EQ(calculate(BarHeader), "<sub/bar.h>");
345345
}
346346

347+
TEST_F(HeadersTest, ShortenIncludesInSearchPathBracketedFilterByFullPath) {
348+
// The filter receives the full path of the header, so it is able to filter by
349+
// the parent directory, even if it is part of the include search path
350+
AngledHeaders.push_back([](auto Path) {
351+
#if defined(_WIN32)
352+
llvm::Regex Pattern("sub\\.*");
353+
#else
354+
llvm::Regex Pattern("sub/.*");
355+
#endif
356+
return Pattern.match(Path);
357+
});
358+
std::string BarHeader = testPath("sub/bar.h");
359+
EXPECT_EQ(calculate(BarHeader), "<bar.h>");
360+
}
361+
347362
TEST_F(HeadersTest, ShortenedIncludeNotInSearchPath) {
348363
std::string BarHeader =
349364
llvm::sys::path::convert_to_slash(testPath("sub-2/bar.h"));

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ struct Header {
136136
}
137137
StringRef verbatim() const { return std::get<Verbatim>(Storage); }
138138

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

0 commit comments

Comments
 (0)