[include-cleaner] Always keep non-self-contained files (#65499)

This commit is contained in:
kadir çetinkaya 2023-09-11 07:22:52 +02:00 committed by GitHub
parent 1f15155d5e
commit d3cfc7126c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 13 deletions

View File

@ -88,10 +88,10 @@ TEST(IncludeCleaner, StdlibUnused) {
template <typename> class vector {};
}
)cpp";
TU.AdditionalFiles["list"] = "#include <bits>";
TU.AdditionalFiles["queue"] = "#include <bits>";
TU.AdditionalFiles["vector"] = "#include <bits>";
TU.AdditionalFiles["string"] = "#include <bits>";
TU.AdditionalFiles["list"] = guard("#include <bits>");
TU.AdditionalFiles["queue"] = guard("#include <bits>");
TU.AdditionalFiles["vector"] = guard("#include <bits>");
TU.AdditionalFiles["string"] = guard("#include <bits>");
TU.ExtraArgs = {"-isystem", testRoot()};
auto AST = TU.build();
IncludeCleanerFindings Findings = computeIncludeCleanerFindings(AST);

View File

@ -430,7 +430,8 @@ bool PragmaIncludes::isPrivate(const FileEntry *FE) const {
}
bool PragmaIncludes::shouldKeep(const FileEntry *FE) const {
return ShouldKeep.contains(FE->getUniqueID());
return ShouldKeep.contains(FE->getUniqueID()) ||
NonSelfContainedFiles.contains(FE->getUniqueID());
}
namespace {

View File

@ -320,7 +320,7 @@ protected:
void createEmptyFiles(llvm::ArrayRef<StringRef> FileNames) {
for (llvm::StringRef File : FileNames)
Inputs.ExtraFiles[File] = "";
Inputs.ExtraFiles[File] = "#pragma once";
}
};

View File

@ -47,9 +47,10 @@ TEST(IncludeCleanerCheckTest, BasicUnusedIncludes) {
const char *PostCode = "\n";
std::vector<ClangTidyError> Errors;
EXPECT_EQ(PostCode, runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt,
ClangTidyOptions(), {{"bar.h", ""}, {"vector", ""}}));
EXPECT_EQ(PostCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
{{"bar.h", "#pragma once"}, {"vector", "#pragma once"}}));
}
TEST(IncludeCleanerCheckTest, SuppressUnusedIncludes) {
@ -76,10 +77,11 @@ TEST(IncludeCleanerCheckTest, SuppressUnusedIncludes) {
PostCode,
runCheckOnCode<IncludeCleanerCheck>(
PreCode, &Errors, "file.cpp", std::nullopt, Opts,
{{"bar.h", ""},
{"vector", ""},
{appendPathFileSystemIndependent({"foo", "qux.h"}), ""},
{appendPathFileSystemIndependent({"baz", "qux", "qux.h"}), ""}}));
{{"bar.h", "#pragma once"},
{"vector", "#pragma once"},
{appendPathFileSystemIndependent({"foo", "qux.h"}), "#pragma once"},
{appendPathFileSystemIndependent({"baz", "qux", "qux.h"}),
"#pragma once"}}));
}
TEST(IncludeCleanerCheckTest, BasicMissingIncludes) {