Skip to content

Commit d3cfc71

Browse files
authored
[include-cleaner] Always keep non-self-contained files (#65499)
1 parent 1f15155 commit d3cfc71

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ TEST(IncludeCleaner, StdlibUnused) {
8888
template <typename> class vector {};
8989
}
9090
)cpp";
91-
TU.AdditionalFiles["list"] = "#include <bits>";
92-
TU.AdditionalFiles["queue"] = "#include <bits>";
93-
TU.AdditionalFiles["vector"] = "#include <bits>";
94-
TU.AdditionalFiles["string"] = "#include <bits>";
91+
TU.AdditionalFiles["list"] = guard("#include <bits>");
92+
TU.AdditionalFiles["queue"] = guard("#include <bits>");
93+
TU.AdditionalFiles["vector"] = guard("#include <bits>");
94+
TU.AdditionalFiles["string"] = guard("#include <bits>");
9595
TU.ExtraArgs = {"-isystem", testRoot()};
9696
auto AST = TU.build();
9797
IncludeCleanerFindings Findings = computeIncludeCleanerFindings(AST);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ bool PragmaIncludes::isPrivate(const FileEntry *FE) const {
430430
}
431431

432432
bool PragmaIncludes::shouldKeep(const FileEntry *FE) const {
433-
return ShouldKeep.contains(FE->getUniqueID());
433+
return ShouldKeep.contains(FE->getUniqueID()) ||
434+
NonSelfContainedFiles.contains(FE->getUniqueID());
434435
}
435436

436437
namespace {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class PragmaIncludeTest : public ::testing::Test {
320320

321321
void createEmptyFiles(llvm::ArrayRef<StringRef> FileNames) {
322322
for (llvm::StringRef File : FileNames)
323-
Inputs.ExtraFiles[File] = "";
323+
Inputs.ExtraFiles[File] = "#pragma once";
324324
}
325325
};
326326

clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ TEST(IncludeCleanerCheckTest, BasicUnusedIncludes) {
4747
const char *PostCode = "\n";
4848

4949
std::vector<ClangTidyError> Errors;
50-
EXPECT_EQ(PostCode, runCheckOnCode<IncludeCleanerCheck>(
51-
PreCode, &Errors, "file.cpp", std::nullopt,
52-
ClangTidyOptions(), {{"bar.h", ""}, {"vector", ""}}));
50+
EXPECT_EQ(PostCode,
51+
runCheckOnCode<IncludeCleanerCheck>(
52+
PreCode, &Errors, "file.cpp", std::nullopt, ClangTidyOptions(),
53+
{{"bar.h", "#pragma once"}, {"vector", "#pragma once"}}));
5354
}
5455

5556
TEST(IncludeCleanerCheckTest, SuppressUnusedIncludes) {
@@ -76,10 +77,11 @@ TEST(IncludeCleanerCheckTest, SuppressUnusedIncludes) {
7677
PostCode,
7778
runCheckOnCode<IncludeCleanerCheck>(
7879
PreCode, &Errors, "file.cpp", std::nullopt, Opts,
79-
{{"bar.h", ""},
80-
{"vector", ""},
81-
{appendPathFileSystemIndependent({"foo", "qux.h"}), ""},
82-
{appendPathFileSystemIndependent({"baz", "qux", "qux.h"}), ""}}));
80+
{{"bar.h", "#pragma once"},
81+
{"vector", "#pragma once"},
82+
{appendPathFileSystemIndependent({"foo", "qux.h"}), "#pragma once"},
83+
{appendPathFileSystemIndependent({"baz", "qux", "qux.h"}),
84+
"#pragma once"}}));
8385
}
8486

8587
TEST(IncludeCleanerCheckTest, BasicMissingIncludes) {

0 commit comments

Comments
 (0)