fix(c++): auto-include Generators.hpp and use real type names in multi-source usage comments#2978
Merged
Conversation
…usage comments (#2438) Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
In C++
--source-style multi-sourceoutput, two related problems made generated code confusing or uncompilable without manual workarounds:Generators.hppinclude in the umbrella header.Generators.hppholds all theto_json/from_jsonoverloads for the generated types, but the umbrella header (e.g.myapp.hpp, built byemitMultiSourceStructure) never included it. Any code that only includes the umbrella header and callsnlohmann::json::parse(...).get<SomeType>()fails to compile/link because the requiredto_json/from_jsonoverloads aren't visible — users had to manually discover and add#include "Generators.hpp".myapp.hpp data = nlohmann::json::parse(jsonString);orhelper.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:emitMultiSourceStructurecallsthis.startFile("Generators.hpp", true)but never adds"Generators.hpp"tothis._generatedFiles. The umbrella header's#includelist is built later by iterating_generatedFiles, soGenerators.hppwas silently omitted.startFileonly usedforEachTopLevelto emit one usage-comment line per real top-level type name whentypeSourceStylewas true (single-source). For multi-source (typeSourceStylefalse), it fell back to using the file's ownbasenameinstead.Fix
this._generatedFiles.add("Generators.hpp")call so the umbrella header includes it.forEachTopLevelto emit the usage-comment lines with real top-level type names, regardless oftypeSourceStyle.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 newcplusplus-multi-sourcefixture (CPlusPlusMultiSourceLanguage) that runs the existing C++ JSON fixture suite withsource-style: multi-sourceagainstpokedex.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#includeonly the umbrella header (previously it manually included both the umbrella header andGenerators.hpp, which masked bug WIP: Nested types #1 from ever being caught by the fixture suite).Verification
npm run buildpasses.npx vitest run test/unit— 165/165 unit tests pass, including the newcplusplus-multi-source.test.ts.QUICKTEST=true FIXTURE=cplusplus-multi-source script/test— passes locally (compiles with g++, runs, round-tripspokedex.jsonthrough the multi-source generated C++ code).#include "Generators.hpp"and usage comments read// ChunkCache data = nlohmann::json::parse(jsonString);/// BufferPath data = nlohmann::json::parse(jsonString);instead ofmyapp.hpp data = .../helper.hpp data = ....Fixes #2438
🤖 Generated with Claude Code