Skip to content

Conversation

sina-mahdavi
Copy link
Contributor

This PR is a follow-up to #137087 that extends the output format generated by -header-include-filtering=direct-per-file to include information about the source location where those include/imports happended, as well as include information about the imported module when an include is translated to a module import.

Copy link

github-actions bot commented Sep 3, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Sep 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2025

@llvm/pr-subscribers-clang

Author: Sina Mahdavi (sina-mahdavi)

Changes

This PR is a follow-up to #137087 that extends the output format generated by -header-include-filtering=direct-per-file to include information about the source location where those include/imports happended, as well as include information about the imported module when an include is translated to a module import.


Full diff: https://github.com/llvm/llvm-project/pull/156756.diff

1 Files Affected:

  • (modified) clang/lib/Frontend/HeaderIncludeGen.cpp (+60-13)
diff --git a/clang/lib/Frontend/HeaderIncludeGen.cpp b/clang/lib/Frontend/HeaderIncludeGen.cpp
index 8ab335905f9f2..8de8d61b6262c 100644
--- a/clang/lib/Frontend/HeaderIncludeGen.cpp
+++ b/clang/lib/Frontend/HeaderIncludeGen.cpp
@@ -112,11 +112,17 @@ class HeaderIncludesJSONCallback : public PPCallbacks {
 /// an array of separate entries, one for each non-system source file used in
 /// the compilation showing only the direct includes and imports from that file.
 class HeaderIncludesDirectPerFileCallback : public PPCallbacks {
+  struct HeaderIncludeInfo {
+    SourceLocation location;
+    FileEntryRef file;
+    const Module *importedModule;
+  };
+
   SourceManager &SM;
   HeaderSearch &HSI;
   raw_ostream *OutputFile;
   bool OwnsOutputFile;
-  using DependencyMap = llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef>>;
+  using DependencyMap = llvm::DenseMap<FileEntryRef, SmallVector<HeaderIncludeInfo>>;
   DependencyMap Dependencies;
 
 public:
@@ -390,18 +396,43 @@ void HeaderIncludesDirectPerFileCallback::EndOfMainFile() {
   std::string Str;
   llvm::raw_string_ostream OS(Str);
   llvm::json::OStream JOS(OS);
-  JOS.array([&] {
-    for (auto S = SourceFiles.begin(), SE = SourceFiles.end(); S != SE; ++S) {
-      JOS.object([&] {
-        SmallVector<FileEntryRef> &Deps = Dependencies[*S];
-        JOS.attribute("source", S->getName().str());
-        JOS.attributeArray("includes", [&] {
-          for (unsigned I = 0, N = Deps.size(); I != N; ++I)
-            JOS.value(Deps[I].getName().str());
+  JOS.object([&] {
+    JOS.attribute("version", "2.0.0");
+    JOS.attributeArray("dependencies", [&] {
+      for (auto S = SourceFiles.begin(), SE = SourceFiles.end(); S != SE; ++S) {
+        JOS.object([&] {
+          SmallVector<HeaderIncludeInfo> &Deps = Dependencies[*S];
+          JOS.attribute("source", S->getName().str());
+          JOS.attributeArray("includes", [&] {
+            for (unsigned I = 0, N = Deps.size(); I != N; ++I) {
+              if (!Deps[I].importedModule) {
+                JOS.object([&] {
+                  PresumedLoc PLoc = SM.getPresumedLoc(Deps[I].location);
+                  std::string locationStr = PLoc.isInvalid() ? "<invalid>" : std::to_string(PLoc.getLine()) + ":" + std::to_string(PLoc.getColumn());
+                  JOS.attribute("location", locationStr);
+                  JOS.attribute("file", Deps[I].file.getName());
+                });
+              }
+            }
+          });
+          JOS.attributeArray("imports", [&] {
+            for (unsigned I = 0, N = Deps.size(); I != N; ++I) {
+              if (Deps[I].importedModule) {
+                JOS.object([&] {
+                  PresumedLoc PLoc = SM.getPresumedLoc(Deps[I].location);
+                  std::string locationStr = PLoc.isInvalid() ? "<invalid>" : std::to_string(PLoc.getLine()) + ":" + std::to_string(PLoc.getColumn());
+                  JOS.attribute("location", locationStr);
+                  JOS.attribute("module", Deps[I].importedModule->getTopLevelModuleName());
+                  JOS.attribute("file", Deps[I].file.getName());
+                });
+              }
+            }
+          });
         });
-      });
-    }
+      }
+    });
   });
+  
   OS << "\n";
 
   if (OutputFile->get_kind() == raw_ostream::OStreamKind::OK_FDStream) {
@@ -427,7 +458,19 @@ void HeaderIncludesDirectPerFileCallback::InclusionDirective(
   if (!FromFile)
     return;
 
-  Dependencies[*FromFile].push_back(*File);
+  FileEntryRef headerOrModule = *File;
+  if (ModuleImported && SuggestedModule) {
+    OptionalFileEntryRef ModuleMapFile = HSI.getModuleMap().getModuleMapFileForUniquing(SuggestedModule);
+    if (ModuleMapFile) {
+      headerOrModule = *ModuleMapFile;
+    }
+  }
+
+  Dependencies[*FromFile].push_back({
+    .location = Loc,
+    .file = headerOrModule,
+    .importedModule = (ModuleImported ? SuggestedModule : nullptr)
+  });
 }
 
 void HeaderIncludesDirectPerFileCallback::moduleImport(SourceLocation ImportLoc,
@@ -448,5 +491,9 @@ void HeaderIncludesDirectPerFileCallback::moduleImport(SourceLocation ImportLoc,
   if (!ModuleMapFile)
     return;
 
-  Dependencies[*FromFile].push_back(*ModuleMapFile);
+  Dependencies[*FromFile].push_back({
+    .location = Loc,
+    .file = *ModuleMapFile,
+    .importedModule = Imported
+  });
 }

Copy link
Contributor

@bob-wilson bob-wilson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reviewed these changes and they look OK to me. This is a new option and as far as I know, swift-build is the only thing using it. Sina has a change to update swift-build to work with the new format.

Copy link
Contributor

@jansvoboda11 jansvoboda11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests for the new behavior.

@sina-mahdavi
Copy link
Contributor Author

I think I've resolved all of the comments except adding tests in my new commit. I just need to write some tests.

@sina-mahdavi
Copy link
Contributor Author

I changed the old test to expect the new output format, and added a new test too for modules.

Copy link
Contributor

@Bigcheese Bigcheese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable to me, but I'd like to make sure Jan is happy with it.

Copy link
Contributor

@jansvoboda11 jansvoboda11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

github-actions bot commented Sep 18, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@sina-mahdavi sina-mahdavi force-pushed the sina-mahdavi/module-include-tracing branch from d498d91 to abf1163 Compare September 22, 2025 18:13
@jansvoboda11 jansvoboda11 merged commit 8df194f into llvm:main Sep 24, 2025
9 checks passed
Copy link

@sina-mahdavi Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

bob-wilson pushed a commit to swiftlang/llvm-project that referenced this pull request Sep 26, 2025
…ude-filtering=direct-per-file (llvm#156756)

This PR is a follow-up to
llvm#137087 that extends the output
format generated by -header-include-filtering=direct-per-file to include
information about the source location where those include/imports
happended, as well as include information about the imported module when
an include is translated to a module import.

rdar://161359514

(cherry-picked from commit 8df194f)
bob-wilson added a commit to swiftlang/llvm-project that referenced this pull request Sep 29, 2025
[Clang] Support includes translated to module imports in -header-include-filtering=direct-per-file (llvm#156756)
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…ude-filtering=direct-per-file (llvm#156756)

This PR is a follow-up to
llvm#137087 that extends the output
format generated by -header-include-filtering=direct-per-file to include
information about the source location where those include/imports
happended, as well as include information about the imported module when
an include is translated to a module import.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants