Skip to content

[FEATURE]: cJSON - generate source/header pairs#2618

Merged
schani merged 7 commits into
glideapps:masterfrom
thives:cjson-split-c-h-files
Jul 19, 2026
Merged

[FEATURE]: cJSON - generate source/header pairs#2618
schani merged 7 commits into
glideapps:masterfrom
thives:cjson-split-c-h-files

Conversation

@thives

@thives thives commented Jun 15, 2024

Copy link
Copy Markdown
Contributor

Description

  • Add command line switch to turn on generation of source/header pairs.
  • Generate definitions in header files
  • Generate implementations in source files
  • Name source files and header files the same with .h and .c extensions

Motivation and Context

  • Improve code structure of the generated code
  • Better encapsulation of complexity by keeping the implementations contained in the source files
  • Build cleaner API libraries from the generated code where only the definitions are exposed.

closes #2617

Previous Behaviour / Output

Currently Quicktype generates one or more header files containing both definitions and implementations.

New Behaviour / Output

Quicktype should generate header files containing only definitions, and source files containing only implementations.

How Has This Been Tested?

I have added the command line option to the test configuration and run the tests with and without this new feature enabled by manually toggling it in the configuration file. To make the tests compile the compileCommand have to be manually edited as well where the generated .c file must appear here depending on the value of the new command line option.

After running the tests I inspected the generated test files to verify that definitions indeed ended up in the header file, and that the implementations ended up in the corresponding source file.

Unfortunately I am unfamiliar with this test suite, and I don't know how to test all the different permutations of command line options.

@thives

thives commented Aug 6, 2024

Copy link
Copy Markdown
Contributor Author

It seems the CI didn't trigger. Is there something I must do to have it run, or is there an issue with it?

schani and others added 5 commits July 9, 2026 15:58
Fixes for the header/source split feature (header-only=false):

- startSourceFile now takes the header filename at every call site and
  derives the source filename itself.  Previously the multi-source call
  sites passed the source filename, so every generated .c file began
  with an unguarded self-include (#include <ClassName.c>) that made
  compilation recurse infinitely.
- The redundant unconditional emitIncludeLine(headerFilename, true)
  after startSourceFile is gone.  In header-only mode it made every
  generated header include itself (an unintended change to the
  pre-existing multi-source output); in split mode it duplicated the
  include emitted by startSourceFile.
- The source file's include of its own header is now a quoted include,
  matching the existing convention: quoted includes for generated
  files, angle brackets only for system and vendored headers.
- getSourceNameFromHeaderName anchors the extension swap (/\.h$/), so
  a header named my.house.h maps to my.house.c, not my.couse.h.
- The header-only option is a BooleanOption instead of a string
  EnumOption, giving the usual --[no-]header-only CLI flags.
- The five duplicated finishHeaderFile/finishSourceFile blocks are
  factored into finishCurrentFile, and a comment typo is fixed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- The vendored dependencies (cJSON, hashtable, list) are downloaded
  into a deps/ subdirectory and included with -isystem instead of -I..
  Angle-bracket includes now resolve only in deps/, while quoted
  includes resolve relative to the including file, so a generated
  #include <TopLevel.h> fails to compile and the include-style
  convention is enforced by the build.
- second.c is a second translation unit including TopLevel.h.  It is
  compiled and linked in the split-mode compile commands, verifying
  that split output supports multi-TU builds (issue glideapps#2617).
- Three minimal mode fixtures run one complex input (nbl-stats.json,
  which produces enums, unions and 22 type pairs) through the
  remaining option combinations:
    - cjson-default: single-source, header-only (the pre-existing
      default output mode)
    - cjson-multi-header: multi-source, header-only (single TU;
      header-only output cannot link from multiple TUs)
    - cjson-multi-split: multi-source with header/source pairs,
      compiled with *.c so every generated source file is linked
- The cjson comment-injection target's output is TopLevel.h; with
  header-only=false a proposed filename of TopLevel.c would have named
  both generated files TopLevel.c.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Renders a small input through the quicktype API and asserts, for the
multi-source modes, that every header gets a source file, that no
generated file includes itself, that generated files are referenced
with quoted includes (never angle brackets), that each source file
includes its own header first, and that header-only mode emits no
source files.  Also pins the output filenames for a proposed filename
with an inner ".h" (my.house.h -> my.house.c).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@schani
schani merged commit 5a94eaf into glideapps:master Jul 19, 2026
26 checks passed
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.

[FEATURE]: cJSON - generate source/header pairs

2 participants