Skip to content

[IncludeCleaner] Also check for spellings of physical headers #99843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2024
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
20 changes: 20 additions & 0 deletions clang-tools-extra/clangd/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,26 @@ computeIncludeCleanerFindings(ParsedAST &AST, bool AnalyzeAngledIncludes) {
Ref.RT != include_cleaner::RefType::Explicit)
return;

// Check if we have any headers with the same spelling, in edge cases
// like `#include_next "foo.h"`, the user can't ever include the
// physical foo.h, but can have a spelling that refers to it.
// We postpone this check because spelling a header for every usage is
// expensive.
std::string Spelling = include_cleaner::spellHeader(
{Providers.front(), AST.getPreprocessor().getHeaderSearchInfo(),
MainFile});
for (auto *Inc :
ConvertedIncludes.match(include_cleaner::Header{Spelling})) {
Satisfied = true;
auto HeaderID =
AST.getIncludeStructure().getID(&Inc->Resolved->getFileEntry());
assert(HeaderID.has_value() &&
"ConvertedIncludes only contains resolved includes.");
Used.insert(*HeaderID);
}
if (Satisfied)
return;

// We actually always want to map usages to their spellings, but
// spelling locations can point into preamble section. Using these
// offsets could lead into crashes in presence of stale preambles. Hence
Expand Down
22 changes: 22 additions & 0 deletions clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,28 @@ TEST(IncludeCleaner, ResourceDirIsIgnored) {
EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
}

TEST(IncludeCleaner, DifferentHeaderSameSpelling) {
// `foo` is declared in foo_inner/foo.h, but there's no way to spell it
// directly. Make sure we don't generate unusued/missing include findings in
// such cases.
auto TU = TestTU::withCode(R"cpp(
#include <foo.h>
void baz() {
foo();
}
)cpp");
TU.AdditionalFiles["foo/foo.h"] = guard("#include_next <foo.h>");
TU.AdditionalFiles["foo_inner/foo.h"] = guard(R"cpp(
void foo();
)cpp");
TU.ExtraArgs.push_back("-Ifoo");
TU.ExtraArgs.push_back("-Ifoo_inner");

auto AST = TU.build();
auto Findings = computeIncludeCleanerFindings(AST);
EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
EXPECT_THAT(Findings.MissingIncludes, IsEmpty());
}
} // namespace
} // namespace clangd
} // namespace clang
17 changes: 14 additions & 3 deletions clang-tools-extra/include-cleaner/lib/Analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,20 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
}
if (!Satisfied && !Providers.empty() &&
Ref.RT == RefType::Explicit &&
!HeaderFilter(Providers.front().resolvedPath()))
Missing.insert(spellHeader(
{Providers.front(), PP.getHeaderSearchInfo(), MainFile}));
!HeaderFilter(Providers.front().resolvedPath())) {
// Check if we have any headers with the same spelling, in edge
// cases like `#include_next "foo.h"`, the user can't ever
// include the physical foo.h, but can have a spelling that
// refers to it.
auto Spelling = spellHeader(
{Providers.front(), PP.getHeaderSearchInfo(), MainFile});
for (const Include *I : Inc.match(Header{Spelling})) {
Used.insert(I);
Satisfied = true;
}
if (!Satisfied)
Missing.insert(std::move(Spelling));
}
});

AnalysisResults Results;
Expand Down
26 changes: 26 additions & 0 deletions clang-tools-extra/include-cleaner/unittests/AnalysisTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "clang-include-cleaner/Record.h"
#include "clang-include-cleaner/Types.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclBase.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/SourceLocation.h"
Expand Down Expand Up @@ -296,6 +297,31 @@ TEST_F(AnalyzeTest, ResourceDirIsIgnored) {
EXPECT_THAT(Results.Missing, testing::IsEmpty());
}

TEST_F(AnalyzeTest, DifferentHeaderSameSpelling) {
Inputs.ExtraArgs.push_back("-Ifoo");
Inputs.ExtraArgs.push_back("-Ifoo_inner");
// `foo` is declared in foo_inner/foo.h, but there's no way to spell it
// directly. Make sure we don't generate unusued/missing include findings in
// such cases.
Inputs.Code = R"cpp(
#include <foo.h>
void baz() {
foo();
}
)cpp";
Inputs.ExtraFiles["foo/foo.h"] = guard("#include_next <foo.h>");
Inputs.ExtraFiles["foo_inner/foo.h"] = guard(R"cpp(
void foo();
)cpp");
TestAST AST(Inputs);
std::vector<Decl *> DeclsInTU;
for (auto *D : AST.context().getTranslationUnitDecl()->decls())
DeclsInTU.push_back(D);
auto Results = analyze(DeclsInTU, {}, PP.Includes, &PI, AST.preprocessor());
EXPECT_THAT(Results.Unused, testing::IsEmpty());
EXPECT_THAT(Results.Missing, testing::IsEmpty());
}

TEST(FixIncludes, Basic) {
llvm::StringRef Code = R"cpp(#include "d.h"
#include "a.h"
Expand Down
Loading