Skip to content

gen: replace anonymous namespace with per-type name in enum impls#246

Merged
webern merged 2 commits into
mainfrom
unity-builds
Jun 27, 2026
Merged

gen: replace anonymous namespace with per-type name in enum impls#246
webern merged 2 commits into
mainfrom
unity-builds

Conversation

@webern

@webern webern commented Jun 27, 2026

Copy link
Copy Markdown
Owner

Summary

Each generated enum `.cpp` file (136 of them) contained an anonymous `namespace {}` wrapping a
`constexpr std::string_view kWire[]` lookup table. In a normal build this is fine — each
translation unit gets its own anonymous namespace and the names never conflict. In a unity build,
however, all included `.cpp` files share one translation unit, so the anonymous namespaces merge
and 136 definitions of `kWire` collide.

The fix is minimal: drop the anonymous namespace and give the array a per-type name
(`kYesNoWire`, `kAboveBelowWire`, etc.) so the names are unique within `mx::core` and unity
builds can include multiple enum files without redefinition errors.

Changes:

  • gen/cpp/templates/enum.cpp.tmpl — remove the anonymous namespace, rename kWire to k{{ident}}Wire
  • src/private/mx/core/generated/*.cpp — 97 regenerated files (the enum-class strategy files that had the anonymous namespace)

Testing

  • make test passes

References

@webern webern added non-breaking fixes or implementation that do not require breaking changes area/mx::core area/codegen ai Issues opened by, or through, a coding agent. labels Jun 27, 2026
@github-actions

Copy link
Copy Markdown

gen-quality gen/

gen-quality: 84.5 / 100   (floor 84.5, +0.0)

  structure     86.5  x0.50   [fn 90.5 / file 82.6]
  cyclomatic    88.4  x0.25
  cognitive     76.6  x0.25

  409 functions across 31 files, 7702 lines (largest file 1044)
  max cc 56  max cognitive 44  max fn loc 152

Worst offenders (top 5 per axis; full lists in score.json):
  cyclomatic gen/xsd/analyze.py:311     report                             56
  cyclomatic gen/plates/build.py:956    _validate_config_against_ir        35
  cyclomatic gen/press/context.py:145   plate_context                      34
  cyclomatic gen/__main__.py:46         _ir                                23
  cyclomatic gen/tests/test_ir.py:102   _check_references                  20
  cognitive  gen/xsd/analyze.py:311     report                             44
  cognitive  gen/ir/resolve.py:119      flat_elements                      40
  cognitive  gen/tests/test_ir.py:102   _check_references                  38
  cognitive  gen/press/context.py:145   plate_context                      37
  cognitive  gen/xsd/analyze.py:207     _sccs                              37
  size       gen/xsd/analyze.py:311     report                             152
  size       gen/press/context.py:145   plate_context                      96
  size       gen/plates/build.py:533    _value_plate                       89
  size       gen/plates/build.py:956    _validate_config_against_ir        89
  size       gen/ir/resolve.py:119      flat_elements                      78

Commit c1a3fe515d7910a48eebe53c79dd6f0c903dabf3.

webern added 2 commits June 27, 2026 18:14
Replace the anonymous namespace wrapping kWire[] with a per-type
name (k{{ident}}Wire) so that unity builds can include multiple
generated enum .cpp files in a single translation unit without
redefinition errors.
@github-actions

Copy link
Copy Markdown

gen-quality gen/

gen-quality: 84.5 / 100   (floor 84.5, +0.0)

  structure     86.5  x0.50   [fn 90.5 / file 82.6]
  cyclomatic    88.4  x0.25
  cognitive     76.6  x0.25

  409 functions across 31 files, 7702 lines (largest file 1044)
  max cc 56  max cognitive 44  max fn loc 152

Worst offenders (top 5 per axis; full lists in score.json):
  cyclomatic gen/xsd/analyze.py:311     report                             56
  cyclomatic gen/plates/build.py:956    _validate_config_against_ir        35
  cyclomatic gen/press/context.py:145   plate_context                      34
  cyclomatic gen/__main__.py:46         _ir                                23
  cyclomatic gen/tests/test_ir.py:102   _check_references                  20
  cognitive  gen/xsd/analyze.py:311     report                             44
  cognitive  gen/ir/resolve.py:119      flat_elements                      40
  cognitive  gen/tests/test_ir.py:102   _check_references                  38
  cognitive  gen/press/context.py:145   plate_context                      37
  cognitive  gen/xsd/analyze.py:207     _sccs                              37
  size       gen/xsd/analyze.py:311     report                             152
  size       gen/press/context.py:145   plate_context                      96
  size       gen/plates/build.py:533    _value_plate                       89
  size       gen/plates/build.py:956    _validate_config_against_ir        89
  size       gen/ir/resolve.py:119      flat_elements                      78

Commit c4b2a5ecfb3699a6f48e735a44837625763f3bec.

@rpatters1

Copy link
Copy Markdown
Contributor

I left this comment at the issue as well: we should probably do the same for the 5 hand-crafted mx::core TUs that have anonymous namespaces. With that change, I can unity build the entire MX subsystem and it is much faster and less noisy.

@github-actions

Copy link
Copy Markdown

Coverage report

Core-dev coverage src/private/mx/core/

Metric Coverage Covered / Total
Lines 77.8% 28487 / 36624
Functions 74.3% 6349 / 8550
Branches 50.6% 22632 / 44725

API coverage src/private/mx/{api,impl,utility}/

Metric Coverage Covered / Total
Lines 78.5% 5913 / 7535
Functions 63.5% 2006 / 3159
Branches 47.8% 4998 / 10455

Core HTML report | API HTML report

Commit c4b2a5ecfb3699a6f48e735a44837625763f3bec.

@webern webern merged commit 2ba61db into main Jun 27, 2026
7 checks passed
@webern webern deleted the unity-builds branch June 27, 2026 17:18
webern added a commit that referenced this pull request Jun 27, 2026
## Summary

Follow-up to #246. The previous PR fixed the generated enum, number,
string, and smufl_prefixed
templates. This PR covers the remaining cases found by doing an actual
unity build
(`CMAKE_UNITY_BUILD=ON`, `BATCH_SIZE=0`):

- `src/private/mx/core/Token.cpp` and `NameToken.cpp` — both defined
`isNameChar` in an anonymous
namespace inside `mx::core`. Renamed to `tokenIsNameChar` /
`nameTokenIsNameChar`.
- `gen/cpp/templates/nmtoken_string.cpp.tmpl` and `smufl_wavy.cpp.tmpl`
— same pattern, both
generating `isNameChar` in anonymous namespaces. Renamed to
`isNameChar{{ident}}`.
- `AGENTS.md` — added a project-wide rule prohibiting anonymous
namespaces, with the unity build
  test command so contributors can verify.

No CMake changes are needed by contributors wanting unity builds. The
standard CMake flags work
as-is:

```
cmake -S . -B build/unity -DCMAKE_UNITY_BUILD=ON -DCMAKE_UNITY_BUILD_BATCH_SIZE=0
cmake --build build/unity --target mx
```

## Testing

- Unity build passes clean (`cmake --build` with `CMAKE_UNITY_BUILD=ON
BATCH_SIZE=0`, target `mx`)
- [ ] `make test` passes

## References

- Closes #245
- Follow-up to #246
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Issues opened by, or through, a coding agent. area/codegen area/mx::core non-breaking fixes or implementation that do not require breaking changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for unity builds

2 participants