[clang-tools-extra] [include-cleaner] Always keep non-self-contained files (PR #65499)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 6 09:44:11 PDT 2023


https://github.com/kadircet created https://github.com/llvm/llvm-project/pull/65499:

None

>From c1f9b400497b78a5283d75da6f7ee0c14320e541 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Wed, 6 Sep 2023 18:42:21 +0200
Subject: [PATCH] [include-cleaner] Always keep non-self-contained files

---
 .../clangd/unittests/IncludeCleanerTests.cpp     |  8 ++++----
 clang-tools-extra/include-cleaner/lib/Record.cpp |  3 ++-
 .../include-cleaner/unittests/RecordTest.cpp     |  2 +-
 .../unittests/clang-tidy/IncludeCleanerTest.cpp  | 16 +++++++++-------
 4 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
index 1d6b99af081429..493a7480bd5b1e 100644
--- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -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);
diff --git a/clang-tools-extra/include-cleaner/lib/Record.cpp b/clang-tools-extra/include-cleaner/lib/Record.cpp
index d7237325f701bb..c87408f213816c 100644
--- a/clang-tools-extra/include-cleaner/lib/Record.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -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 {
diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
index 69bec04ed60194..3f6767270998bc 100644
--- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -320,7 +320,7 @@ class PragmaIncludeTest : public ::testing::Test {
 
   void createEmptyFiles(llvm::ArrayRef<StringRef> FileNames) {
     for (llvm::StringRef File : FileNames)
-      Inputs.ExtraFiles[File] = "";
+      Inputs.ExtraFiles[File] = "#pragma once";
   }
 };
 
diff --git a/clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp b/clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp
index f84133b01a3a49..0c29b469b617b7 100644
--- a/clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/IncludeCleanerTest.cpp
@@ -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) {



More information about the cfe-commits mailing list