[clang-tools-extra] [include-cleaner] Make sure exports of stdlib also works for physical files (PR #72246)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 04:16:34 PST 2023


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

This was creating discrepancy in cases where we might have a physical
file entry (e.g. because we followed a source location from a stdlib
file) and tried to find its exporters.


>From 49ea3ae3468c5d67b2b6b03339d642e25d4fa6b0 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Tue, 14 Nov 2023 13:08:52 +0100
Subject: [PATCH] [include-cleaner] Make sure exports of stdlib also works for
 physical files

This was creating discrepancy in cases where we might have a physical
file entry (e.g. because we followed a source location from a stdlib
file) and tried to find its exporters.
---
 .../include-cleaner/lib/Record.cpp            | 26 +++++++++----------
 .../include-cleaner/unittests/RecordTest.cpp  |  2 ++
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/clang-tools-extra/include-cleaner/lib/Record.cpp b/clang-tools-extra/include-cleaner/lib/Record.cpp
index 7a8e10a9c675496..a98129452e2bdde 100644
--- a/clang-tools-extra/include-cleaner/lib/Record.cpp
+++ b/clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -232,6 +232,17 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
   void checkForExport(FileID IncludingFile, int HashLine,
                       std::optional<Header> IncludedHeader,
                       OptionalFileEntryRef IncludedFile) {
+    auto AddExport = [&] {
+      auto ExportingFileName = SM.getFileEntryForID(IncludingFile)->getName();
+      if (IncludedFile) {
+        Out->IWYUExportBy[IncludedFile->getUniqueID()].push_back(
+            ExportingFileName);
+      }
+      if (IncludedHeader && IncludedHeader->kind() == Header::Standard) {
+        Out->StdIWYUExportBy[IncludedHeader->standard()].push_back(
+            ExportingFileName);
+      }
+    };
     if (ExportStack.empty())
       return;
     auto &Top = ExportStack.back();
@@ -240,20 +251,7 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
     // Make sure current include is covered by the export pragma.
     if ((Top.Block && HashLine > Top.SeenAtLine) ||
         Top.SeenAtLine == HashLine) {
-      if (IncludedHeader) {
-        switch (IncludedHeader->kind()) {
-        case Header::Physical:
-          Out->IWYUExportBy[IncludedHeader->physical().getUniqueID()]
-              .push_back(Top.Path);
-          break;
-        case Header::Standard:
-          Out->StdIWYUExportBy[IncludedHeader->standard()].push_back(Top.Path);
-          break;
-        case Header::Verbatim:
-          assert(false && "unexpected Verbatim header");
-          break;
-        }
-      }
+      AddExport();
       // main-file #include with export pragma should never be removed.
       if (Top.SeenAtFile == SM.getMainFileID() && IncludedFile)
         Out->ShouldKeep.insert(IncludedFile->getUniqueID());
diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
index 36850731d514539..dfefa66887b0f24 100644
--- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -452,6 +452,8 @@ TEST_F(PragmaIncludeTest, IWYUExportForStandardHeaders) {
   auto &FM = Processed.fileManager();
   EXPECT_THAT(PI.getExporters(*tooling::stdlib::Header::named("<string>"), FM),
               testing::UnorderedElementsAre(FileNamed("export.h")));
+  EXPECT_THAT(PI.getExporters(llvm::cantFail(FM.getFileRef("string")), FM),
+              testing::UnorderedElementsAre(FileNamed("export.h")));
 }
 
 TEST_F(PragmaIncludeTest, IWYUExportBlock) {



More information about the cfe-commits mailing list