This is an archive of the discontinued LLVM Phabricator instance.

[Modules] Add module structure output to -module-file-info.
ClosedPublic

Authored by iains on Feb 15 2022, 2:26 AM.

Details

Summary

It is useful to be able to visualise the C++20 modules content of a PCM file
both for inspection and for testing. In particular, when adding more module
types to support C++20 Partitions and Header Units, we would like to be able
to confirm that the output PCM has the intended structure.

The existing scheme for dumping data is restricted to the content of the AST
file control block, which does not include structural data beyond imports.

The change here makes use of the AST unit that is set up by BeginSourceFile
to query for the information on the primary and sub-modules. We can then
inspect each of these in turn, accounting for Global, Private, Imported and
Exported modules/fragments and then showing the sub-stucture of the main
module(s).

The disadvantage of this mechanism is that it has no easy method to control
the granularity of the output. Perhaps more detailed inspection would be
better handled by a stand-alone module inspection tool.

Diff Detail

Event Timeline

iains created this revision.Feb 15 2022, 2:26 AM
iains published this revision for review.Feb 15 2022, 2:29 AM
iains added reviewers: dblaikie, aprantl, urnathan, ChuanqiXu.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2022, 2:30 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
urnathan added inline comments.Feb 15 2022, 5:20 AM
clang/lib/Frontend/FrontendActions.cpp
817–824

belongs in a later patch?

iains updated this revision to Diff 408930.Feb 15 2022, 9:39 AM

address review comment, fix formatting.

iains marked an inline comment as done.Feb 15 2022, 9:44 AM
ChuanqiXu added inline comments.Feb 15 2022, 6:20 PM
clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

According to P1857R3, it might not be good to add macro declarative before module declaration. Although clang didn't implement it and there are old test case uses this style, I think it might be better to split into files. @urnathan how do you think about this?

iains added inline comments.Feb 16 2022, 1:15 AM
clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

I see this in the Tony tables, but OTOH I cannot exactly see where the wording forbids it - in fact it permits preprocesssor directives before the import (including in the example in 5.7)... perhaps I'm misreading.

This way of writing the test cases does make them much easier to manage (and for a reader to see the intent) - of course, if we should split into files - then that is what we should do.

ChuanqiXu added inline comments.Feb 16 2022, 1:23 AM
clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

I think the grammar in [[ http://eel.is/c++draft/cpp | [cpp.pre] ]] forbid it.

First, the file could only be:

preprocessing-file:
group_opt
module-file

Then it is clear that we couldn't put macro declarative before module declaration. I agree the current style is easier and more convenient. In fact, I prefer it too. But I think we would better follow it since it is standard. Otherwise, it would be more work in later maintenance stage.

urnathan added inline comments.Feb 16 2022, 7:01 AM
clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

ChuanqiXu is correct about preprocessor directives not being allowed before the initial module decl, but I don;t think compilers implement that. There are a couple of issues.

a) some users have a need to have #pragma charset-or-similar before any tokens
b) forced headers

I do find the #if use here somewhat confusing. A bunch of cxx20-file-info-[1-n].cpp might be more straightforwards? (It does seem that directory is using cxx20 as a prefix rather than suffix btw.)

urnathan added inline comments.Feb 16 2022, 7:03 AM
clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

or maybe splitting this up make it harder to understand the tests as a whole. I don;t think it terribly importand. If we do ban pre-module directives, we can always split it then,

iains added inline comments.Feb 16 2022, 7:06 AM
clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

OK ( actually, I find it easier to grok the test case intent if the various required inputs are built in order, in the single file - but that's the nature of preferences :) )

I suppose that (a) and (b) mean that we cannot reasonably diagnose this?

Probably some of the files (e.g. ones making dummy modules to be consumed by the actual tests) could be in 'Inputs'. Will rework this accordingly.

urnathan added inline comments.Feb 16 2022, 8:18 AM
clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

I suppose that (a) and (b) mean that we cannot reasonably diagnose this?

The #pragma item came up in stdization and IIRC there was some handwaving that such a pragma is not actually in the source text -- because it's telling you something about the encoding. One could probably make a similar argument about forced headers -- they're implementation detail (that the user might be able to control).

Anyway, IMHO this is orthogonal, and we might even be able to deploy something like:

#pragma GCC system_header
#look ma! random directive
module;

if we do start being pickier.

ChuanqiXu accepted this revision.Feb 16 2022, 7:34 PM

LGTM.

clang/test/Modules/module-file-info-cxx20.cpp
26 ↗(On Diff #408930)

OK, so I summarize the conclusion here is to allow use of macro declarative in test now and we could refactor in one shot once compiler implement the constraint. It is good for me as long as we get in consensus.

This revision is now accepted and ready to land.Feb 16 2022, 7:34 PM
iains updated this revision to Diff 410568.Feb 22 2022, 9:32 AM

rebased, amended testcases to use split-file, handle partition enumerations.

iains added a comment.Feb 22 2022, 9:34 AM

this now uses the split-file approach to the test case so that we have the test case self-contained, but avoid the use of preprocessor directives before the module lines.

iains updated this revision to Diff 410599.Feb 22 2022, 11:48 AM

reformat.

This revision was landed with ongoing or failed builds.Feb 23 2022, 2:27 AM
This revision was automatically updated to reflect the committed changes.