-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] Support includes translated to module imports in -header-include-filtering=direct-per-file #156756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang] Support includes translated to module imports in -header-include-filtering=direct-per-file #156756
Conversation
…ude-filtering=direct-per-file
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 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. |
@llvm/pr-subscribers-clang Author: Sina Mahdavi (sina-mahdavi) ChangesThis 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:
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
+ });
}
|
There was a problem hiding this 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.
There was a problem hiding this 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.
I think I've resolved all of the comments except adding tests in my new commit. I just need to write some tests. |
I changed the old test to expect the new output format, and added a new test too for modules. |
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Jan Svoboda <[email protected]>
✅ With the latest revision this PR passed the C/C++ code formatter. |
d498d91
to
abf1163
Compare
@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! |
…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)
[Clang] Support includes translated to module imports in -header-include-filtering=direct-per-file (llvm#156756)
…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.
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.