Skip to content

fix(c++): auto-include Generators.hpp and use real type names in multi-source usage comments#2978

Merged
schani merged 1 commit into
masterfrom
agent/fix-issue-2438
Jul 21, 2026
Merged

fix(c++): auto-include Generators.hpp and use real type names in multi-source usage comments#2978
schani merged 1 commit into
masterfrom
agent/fix-issue-2438

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

Bug

In C++ --source-style multi-source output, two related problems made generated code confusing or uncompilable without manual workarounds:

  1. Missing Generators.hpp include in the umbrella header. Generators.hpp holds all the to_json/from_json overloads for the generated types, but the umbrella header (e.g. myapp.hpp, built by emitMultiSourceStructure) never included it. Any code that only includes the umbrella header and calls nlohmann::json::parse(...).get<SomeType>() fails to compile/link because the required to_json/from_json overloads aren't visible — users had to manually discover and add #include "Generators.hpp".
  2. Misleading usage comments. Every generated file's leading "how to use this" comment used the current file's own name instead of a real top-level type name, e.g. myapp.hpp data = nlohmann::json::parse(jsonString); or helper.hpp data = nlohmann::json::parse(jsonString); — neither of which is valid or meaningful C++.

Root cause

Both bugs are in packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts:

  1. emitMultiSourceStructure calls this.startFile("Generators.hpp", true) but never adds "Generators.hpp" to this._generatedFiles. The umbrella header's #include list is built later by iterating _generatedFiles, so Generators.hpp was silently omitted.
  2. startFile only used forEachTopLevel to emit one usage-comment line per real top-level type name when typeSourceStyle was true (single-source). For multi-source (typeSourceStyle false), it fell back to using the file's own basename instead.

Fix

  • Add the missing this._generatedFiles.add("Generators.hpp") call so the umbrella header includes it.
  • Always use forEachTopLevel to emit the usage-comment lines with real top-level type names, regardless of typeSourceStyle.

Test coverage

  • test/unit/cplusplus-multi-source.test.ts (new): asserts the multi-source umbrella header contains #include "Generators.hpp", and that usage comments across generated files name the real top-level types (ChunkCache, BufferPath) rather than the generated filename.
  • test/languages.ts / test/fixtures.ts: added a new cplusplus-multi-source fixture (CPlusPlusMultiSourceLanguage) that runs the existing C++ JSON fixture suite with source-style: multi-source against pokedex.json (multiple top-level types), so the generated multi-source output is actually compiled and round-tripped by the fixture harness.
  • test/fixtures/cplusplus/main.cpp: changed to #include only the umbrella header (previously it manually included both the umbrella header and Generators.hpp, which masked bug WIP: Nested types #1 from ever being caught by the fixture suite).

Verification

  • npm run build passes.
  • npx vitest run test/unit — 165/165 unit tests pass, including the new cplusplus-multi-source.test.ts.
  • QUICKTEST=true FIXTURE=cplusplus-multi-source script/test — passes locally (compiles with g++, runs, round-trips pokedex.json through the multi-source generated C++ code).
  • Manually re-ran the exact repro from the issue/triage comment: the umbrella header now contains #include "Generators.hpp" and usage comments read // ChunkCache data = nlohmann::json::parse(jsonString); / // BufferPath data = nlohmann::json::parse(jsonString); instead of myapp.hpp data = ... / helper.hpp data = ....
  • CI will additionally validate the full C++ fixture matrix (including other renderer-option combinations) across languages this PR doesn't specifically target.

Fixes #2438

🤖 Generated with Claude Code

…usage comments (#2438)

Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
@schani
schani merged commit d29168a into master Jul 21, 2026
28 checks passed
@schani
schani deleted the agent/fix-issue-2438 branch July 21, 2026 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] Generated headers need more helpful comments and should auto-include "Generators.hpp"

1 participant