Skip to content

Commit 599adf3

Browse files
authored
[include-cleaner] Dont apply name-match for non-owning headers (#82625)
1 parent 77dd435 commit 599adf3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

clang-tools-extra/include-cleaner/lib/FindHeaders.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ llvm::SmallVector<Header> headersForSymbol(const Symbol &S,
275275
// are already ranked in the stdlib mapping.
276276
if (H.kind() == Header::Standard)
277277
continue;
278+
// Don't apply name match hints to exporting headers. As they usually have
279+
// names similar to the original header, e.g. foo_wrapper/foo.h vs
280+
// foo/foo.h, but shouldn't be preferred (unless marked as the public
281+
// interface).
282+
if ((H.Hint & Hints::OriginHeader) == Hints::None)
283+
continue;
278284
if (nameMatch(SymbolName, H))
279285
H.Hint |= Hints::PreferredHeader;
280286
}

clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,5 +628,24 @@ TEST_F(HeadersForSymbolTest, StandardHeaders) {
628628
tooling::stdlib::Header::named("<assert.h>")));
629629
}
630630

631+
TEST_F(HeadersForSymbolTest, ExporterNoNameMatch) {
632+
Inputs.Code = R"cpp(
633+
#include "exporter/foo.h"
634+
#include "foo_public.h"
635+
)cpp";
636+
Inputs.ExtraArgs.emplace_back("-I.");
637+
// Deliberately named as foo_public to make sure it doesn't get name-match
638+
// boost and also gets lexicographically bigger order than "exporter/foo.h".
639+
Inputs.ExtraFiles["foo_public.h"] = guard(R"cpp(
640+
struct foo {};
641+
)cpp");
642+
Inputs.ExtraFiles["exporter/foo.h"] = guard(R"cpp(
643+
#include "foo_public.h" // IWYU pragma: export
644+
)cpp");
645+
buildAST();
646+
EXPECT_THAT(headersForFoo(), ElementsAre(physicalHeader("foo_public.h"),
647+
physicalHeader("exporter/foo.h")));
648+
}
649+
631650
} // namespace
632651
} // namespace clang::include_cleaner

0 commit comments

Comments
 (0)