C++20 module support v2 #1575
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1575"
general
weight: 0.35
YexuanXiao
C++20 Module Support v2: Per-Namespace Modules (
import winrt.Windows.Foundation;)Overview
This PR adds per-namespace C++20 module support to C++/WinRT, building on the approach pioneered by @YexuanXiao's cppwinrtplus fork. Instead of a single monolithic
import winrt;module (v1), each WinRT namespace gets its own named module:This provides finer-grained imports, better build parallelism, and first-class support for component authoring with modules.
---
Key Design Decisions
1. Unconditional guards, flag-controlled generation
Module awareness (guards,
WINRT_EXPORT) is always present in projection headers. The-modulesflag controls.ixxgeneration and whether generated component files (module.g.cpp, stub.cpp) useimportvs#include. Projection headers themselves are identical regardless of-modules.2. All three projections generate modules uniformly
When
CppWinRTBuildModule=true,-modulesis passed to the platform, reference, and component projections. There is no special module-specific handling that distinguishes these projections — the existing differences (e.g.,-optfor components,-compfor component stubs) are pre-existing behavior.3. Per-ProjectReference IFC consumption
CppWinRTConsumeModuleis metadata on aProjectReference, not a project-level property. When set, it suppresses-moduleson the platform projection (since IFCs are already available from the referenced project) but does not affect the reference or component projections. This gives precise control over which pre-built IFCs to reuse.4. SCC consolidation for cyclic namespaces
Tarjan's algorithm groups cyclic namespaces. The alphabetically-first namespace owns the module; others re-export it. Users can
import winrt.AnyNamespace;regardless of SCC membership.---
What's in This PR
Code generator (
cppwinrt/)| File | Changes |
|-|-|
|
main.cpp| Added-modules,-module_include,-module_excludeCLI options. Two-pass namespace loop: first populates module namespace set, then generates headers with dependency collection. Tarjan's SCC algorithm. .ixx generation loop. ||
file_writers.h|write_macros_h(),write_module_preamble(),write_base_ixx(),write_numerics_ixx(),write_namespace_ixx(),write_namespace_scc_owner_ixx(),write_namespace_reexport_ixx(). UnconditionalWINRT_IMPORT_MODULE/WINRT_IMPL_BUILD_MODULEguards on all generated headers. Component.g.hdual-path (import vs include).base.hnow#includeswinrt/base_macros.hinstead of inlining macros. ||
code_writers.h|wrap_module_aware_includes_guard()updated comment.wrap_impl_namespace()usesWINRT_EXPORT namespace winrt::impl. ||
settings.h| Addedbool modules,std::set module_include/exclude,winmd::reader::filter module_filter. |String literals (
strings/)| Change | Files |
|-|-|
|
namespace winrt::impl→WINRT_EXPORT namespace winrt::impl| All files with impl namespace ||
namespace std→WINRT_IMPL_STD_EXPORT namespace std| 4 files (hash/coroutine_traits specializations) ||
base_macros.hrefactored as canonical shared macros | Generated aswinrt/base_macros.h, used by bothbase.hand.ixxglobal fragments || New
base_source_location.h| Source location macros +slim_source_location(split frombase_macros.h) || New
base_module_ixx_preamble.h| Global module fragment for.ixxfiles || New
base_module_base_ixx.h|winrt_base.ixxtemplate || New
base_module_numerics_ixx.h|winrt_numerics.ixxtemplate ||
base_extern.h| AddedWINRT_EXPORTto handler function pointers for module/non-module linkage ||
base_fast_forward.h| AddedWINRT_EXPORTfallback definition |MSBuild integration (
nuget/)User-facing properties:
| Property / Metadata | Scope | Default | Description |
|-|-|-|-|
|
CppWinRTBuildModule| Project property | false | Enables-modulesfor all three projections, generating.ixxfiles alongside headers ||
CppWinRTModuleInclude| Project property | (all) | Semicolon-delimited namespace prefixes to include in module generation ||
CppWinRTModuleExclude| Project property | (none) | Semicolon-delimited namespace prefixes to exclude from module generation ||
CppWinRTConsumeModule| ProjectReference metadata | false | Suppresses platform.ixxgeneration and uses pre-built IFCs from the referenced project |Internal targets (not directly configured by users):
| Target | Purpose |
|-|-|
|
CppWinRTAddModuleInterfaces| Discovers generated.ixxfiles and adds them as ClCompile items with module compilation metadata ||
CppWinRTGetModuleOutputs| Exports GeneratedFilesDir / IntDir / OutDir for cross-project IFC resolution ||
CppWinRTResolveModuleReferences| CallsCppWinRTGetModuleOutputson ProjectReferences tagged withCppWinRTConsumeModule|Test projects
| Project | Description |
|-|-|
|
test/test_cpp20_module/| Standalone test in main solution (9 test cases: URI, events, collections, coroutines, source_location) ||
test/nuget/TestModuleBuilder/| Static library — pre-builds platform modules ||
test/nuget/TestModuleComponent1/| Component DLL — Greeter class with Uri return ||
test/nuget/TestModuleComponent2/| Component DLL — GreeterGroup, depends on Component1 ||
test/nuget/TestModuleConsumerApp/| Console app — consumes builder + both components end-to-end ||
test/nuget/TestModuleApp/| Single-project — builds and consumes its own modules |Documentation
| File | Description |
|-|-|
|
nuget/modules.md| User-facing adoption guide (Quick Start, properties, troubleshooting) ||
docs/modules-design.md| Maintainer design document (architecture, SCC, MSBuild flow) ||
.github/instructions/cppwinrt.instructions.md| AI agent instructions for the codebase ||
.github/instructions/modules.instructions.md| AI agent instructions for module-specific work |---
Before / After: User Code
Header mode (unchanged)
Module mode (new)
Component authoring with modules
---
Before / After: Generated Code
Namespace header (
winrt/Windows.Foundation.h) — guard changesBefore (no module awareness):
After (unconditional module guards):
New: Generated module interface (
winrt.Windows.Foundation.ixx)New: SCC owner module (cycle breaker)
When namespaces form dependency cycles (e.g.,
Windows.Foundation↔Windows.Foundation.Collections), the alphabetically-first namespace becomes the SCC owner:New: Component
.g.h(dual-path)---
Differences from the cppwinrtplus Fork
This implementation shares the same core architecture as @YexuanXiao's cppwinrtplus fork — per-namespace modules, Tarjan SCC cycle breaking,
WINRT_EXPORTmacro — but diverges in several areas:Module naming
| | cppwinrtplus | This PR |
|-|-|-|
| Base module |
winrt.base|winrt_base|| Numerics module |
winrt.numerics|winrt_numerics|| Namespace modules |
<Namespace>.ixx(e.g.Windows.Foundation.ixx) |winrt.<Namespace>.ixx(e.g.winrt.Windows.Foundation.ixx) |The
winrt.prefix on namespace modules avoids potential collisions if other code generators produce modules with the same namespace names. Thewinrt_base/winrt_numericsunderscore form avoids a dotted name that could imply a namespace hierarchy.Module namespace filtering
cppwinrtplus uses a
CppWinRT.configXML file in the solution directory with<exclude><prefix>elements to exclude namespaces from module generation. This PR uses-module_include/-module_excludeCLI flags with prefix matching, exposed asCppWinRTModuleInclude/CppWinRTModuleExcludeMSBuild properties. The CLI approach integrates directly into MSBuild response files without needing a separate config file, and supports both include and exclude semantics.Guard macros
| | cppwinrtplus | This PR |
|-|-|-|
| Build-side guard |
WINRT_MODULE|WINRT_IMPL_BUILD_MODULE|| Consume-side guard |
WINRT_CONSUME_MODULE|WINRT_IMPORT_MODULE|| Guard conditionality | Conditional on
-modulesflag | Unconditional — always emitted |Making guards unconditional means the
-modulesflag controls.ixxgeneration and module-aware component code generation (.g.cpp,module.g.cpp, stub.cpp), but does NOT change the content of projection headers — they work in both module and header mode without regeneration.Builder/consumer architecture
cppwinrtplus does not have a concept of sharing pre-built IFCs across projects — each project builds all its own modules. This PR introduces:
CppWinRTBuildModule— project-level property to enable module generation for all three projectionsCppWinRTConsumeModule— per-ProjectReference metadata that suppresses platform.ixxgeneration and lets the project use pre-built IFCs from the referenced project insteadCppWinRTGetModuleOutputs/CppWinRTResolveModuleReferences— Internal MSBuild targets for cross-project IFC path resolution---
Acknowledgements
Credit to @YexuanXiao and the cppwinrtplus fork for the per-namespace module architecture, SCC algorithm, and initial demonstration of viability. Also @sylveon, @Scottj1s, and @zadjii-msft for early module exploration and feedback on v1.