[clang] 8df194f - [Clang] Support includes translated to module imports in -header-include-filtering=direct-per-file (#156756)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 24 09:31:45 PDT 2025
Author: Sina Mahdavi
Date: 2025-09-24T09:31:42-07:00
New Revision: 8df194f6a933da6245053c4e89c6cabff1743319
URL: https://github.com/llvm/llvm-project/commit/8df194f6a933da6245053c4e89c6cabff1743319
DIFF: https://github.com/llvm/llvm-project/commit/8df194f6a933da6245053c4e89c6cabff1743319.diff
LOG: [Clang] Support includes translated to module imports in -header-include-filtering=direct-per-file (#156756)
This PR is a follow-up to
https://github.com/llvm/llvm-project/pull/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.
Added:
clang/test/Preprocessor/Inputs/print-header-json/module.modulemap
clang/test/Preprocessor/Inputs/print-header-json/system/module.modulemap
Modified:
clang/lib/Frontend/HeaderIncludeGen.cpp
clang/test/Preprocessor/print-header-json.c
Removed:
################################################################################
diff --git a/clang/lib/Frontend/HeaderIncludeGen.cpp b/clang/lib/Frontend/HeaderIncludeGen.cpp
index 8ab335905f9f2..7cd9c8a3a5bd7 100644
--- a/clang/lib/Frontend/HeaderIncludeGen.cpp
+++ b/clang/lib/Frontend/HeaderIncludeGen.cpp
@@ -112,11 +112,22 @@ 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;
+
+ HeaderIncludeInfo(SourceLocation Location, FileEntryRef File,
+ const Module *ImportedModule)
+ : Location(Location), File(File), ImportedModule(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:
@@ -295,8 +306,8 @@ void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
}
}
-void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
- Token &FilenameTok,
+void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile,
+ const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) {
if (!DepOpts.ShowSkippedHeaderIncludes)
return;
@@ -390,18 +401,41 @@ 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 (const auto &S : SourceFiles) {
+ 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([&] {
+ JOS.attribute("location", Deps[I].Location.printToString(SM));
+ 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([&] {
+ JOS.attribute("location", Deps[I].Location.printToString(SM));
+ 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 +461,18 @@ void HeaderIncludesDirectPerFileCallback::InclusionDirective(
if (!FromFile)
return;
- Dependencies[*FromFile].push_back(*File);
+ FileEntryRef HeaderOrModuleMapFile = *File;
+ if (ModuleImported && SuggestedModule) {
+ OptionalFileEntryRef ModuleMapFile =
+ HSI.getModuleMap().getModuleMapFileForUniquing(SuggestedModule);
+ if (ModuleMapFile) {
+ HeaderOrModuleMapFile = *ModuleMapFile;
+ }
+ }
+
+ HeaderIncludeInfo DependenciesEntry(
+ Loc, HeaderOrModuleMapFile, (ModuleImported ? SuggestedModule : nullptr));
+ Dependencies[*FromFile].push_back(DependenciesEntry);
}
void HeaderIncludesDirectPerFileCallback::moduleImport(SourceLocation ImportLoc,
@@ -448,5 +493,6 @@ void HeaderIncludesDirectPerFileCallback::moduleImport(SourceLocation ImportLoc,
if (!ModuleMapFile)
return;
- Dependencies[*FromFile].push_back(*ModuleMapFile);
+ HeaderIncludeInfo DependenciesEntry(Loc, *ModuleMapFile, Imported);
+ Dependencies[*FromFile].push_back(DependenciesEntry);
}
diff --git a/clang/test/Preprocessor/Inputs/print-header-json/module.modulemap b/clang/test/Preprocessor/Inputs/print-header-json/module.modulemap
new file mode 100644
index 0000000000000..024c43d89e278
--- /dev/null
+++ b/clang/test/Preprocessor/Inputs/print-header-json/module.modulemap
@@ -0,0 +1,5 @@
+module module0 {
+ header "header0.h"
+ header "header1.h"
+ export *
+}
diff --git a/clang/test/Preprocessor/Inputs/print-header-json/system/module.modulemap b/clang/test/Preprocessor/Inputs/print-header-json/system/module.modulemap
new file mode 100644
index 0000000000000..8ed45ab4dcdbe
--- /dev/null
+++ b/clang/test/Preprocessor/Inputs/print-header-json/system/module.modulemap
@@ -0,0 +1,4 @@
+module systemmodule0 {
+ header "system2.h"
+ export *
+}
diff --git a/clang/test/Preprocessor/print-header-json.c b/clang/test/Preprocessor/print-header-json.c
index bb1830e5030d8..057dcc27d8238 100644
--- a/clang/test/Preprocessor/print-header-json.c
+++ b/clang/test/Preprocessor/print-header-json.c
@@ -21,8 +21,13 @@
#include "header0.h"
#include "system2.h"
+// RUN: rm %t.txt
+// RUN: env CC_PRINT_HEADERS_FORMAT=json CC_PRINT_HEADERS_FILTERING=direct-per-file CC_PRINT_HEADERS_FILE=%t.txt %clang -fsyntax-only -I %S/Inputs/print-header-json -isystem %S/Inputs/print-header-json/system -fmodules -fimplicit-module-maps -fmodules-cache-path=%t %s -o /dev/null
+// RUN: cat %t.txt | FileCheck %s --check-prefix=SUPPORTED_PERFILE_MODULES
+
// SUPPORTED: {"source":"{{[^,]*}}print-header-json.c","includes":["{{[^,]*}}system0.h","{{[^,]*}}system3.h","{{[^,]*}}system2.h"]}
-// SUPPORTED_PERFILE: [{"source":"{{[^,]*}}print-header-json.c","includes":["{{[^,]*}}system0.h","{{[^,]*}}header0.h","{{[^,]*}}system2.h"]},{"source":"{{[^,]*}}header0.h","includes":["{{[^,]*}}system3.h","{{[^,]*}}header1.h","{{[^,]*}}header2.h"]}]
+// SUPPORTED_PERFILE: {"version":"2.0.0","dependencies":[{"source":"{{[^,]*}}print-header-json.c","includes":[{"location":"{{[^,]*}}print-header-json.c:20:1","file":"{{[^,]*}}system0.h"},{"location":"{{[^,]*}}print-header-json.c:21:1","file":"{{[^,]*}}header0.h"},{"location":"{{[^,]*}}print-header-json.c:22:1","file":"{{[^,]*}}system2.h"}],"imports":[]},{"source":"{{[^,]*}}header0.h","includes":[{"location":"{{[^,]*}}header0.h:1:1","file":"{{[^,]*}}system3.h"},{"location":"{{[^,]*}}header0.h:2:1","file":"{{[^,]*}}header1.h"},{"location":"{{[^,]*}}header0.h:3:1","file":"{{[^,]*}}header2.h"}],"imports":[]}]}
+// SUPPORTED_PERFILE_MODULES: {"version":"2.0.0","dependencies":[{"source":"{{[^,]*}}print-header-json.c","includes":[{"location":"{{[^,]*}}print-header-json.c:20:1","file":"{{[^,]*}}system0.h"}],"imports":[{"location":"{{[^,]*}}print-header-json.c:21:1","module":"module0","file":"{{[^,]*}}print-header-json{{[\/\\]+}}module.modulemap"},{"location":"{{[^,]*}}print-header-json.c:22:1","module":"systemmodule0","file":"{{[^,]*}}print-header-json{{[\/\\]+}}system{{[\/\\]+}}module.modulemap"}]}]}
// UNSUPPORTED0: error: unsupported combination: -header-include-format=textual and -header-include-filtering=only-direct-system
// UNSUPPORTED1: error: unsupported combination: -header-include-format=json and -header-include-filtering=none
More information about the cfe-commits
mailing list