Support C99

Open
#384 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
c, python
Domain
tooling

Research direction

Start with the CLI standards in src/nunavut/cli/__init__.py, C language configuration in src/nunavut/lang/properties.json, and support and base templates in src/nunavut/lang/c/support/serialization.j2 and src/nunavut/lang/c/templates/base.j2. Add C99 selection at generation time, then audit generated code and extend strict C and C++ verification for the listed cases. Done means the C99 matrix passes, incompatible modes retain clear diagnostics, and C11/C17/C23 output has no C99 fallback.

Written by the indexing model from the issue text.

Description

Issue from public_regulated_data_types.

Add C99 as a supported output standard for the C generator. The original report identifies two problems: nnvg -std=c99 is rejected, and the generated serialization support header requires C11 or newer. This requires implementation and verification, rather than guidance to upgrade to the 3.0 preview.

Findings

Checked main at 43fec8ad5646f978f3619b4623a32be7f0c1a6ec and 3.0.preview at b803f24b531be6f1ce9d8b53f4936cb4f977c45d:

  • The CLI accepts c11, c17, and c23, but not c99.
  • The C language configuration defaults to C11 and uses _Static_assert for C11/C17 and static_assert for C23.
  • The support header explicitly rejects C compilation when __STDC_VERSION__ < 201112L.
  • The current C verification targets cover C11/C17/C23, with no C99 target. The earlier comment in the linked issue that C99 compatibility was tested is not supported by the current matrix.
  • Reproduced with a generated sample containing an integer, floating-point field, and variable-length array: -std=c99 fails argument parsing; C11 output fails clang -std=c99 -pedantic-errors -fsyntax-only on the version guard and _Static_assert; the same output passes under C11.

Relevant sources: CLI standards, C language configuration, support-header guard and assertions.

Select compatibility code at generation time

We are generating code. Select the C99 implementation in the generator/templates, based on the requested output standard.

  • Do not emit runtime if c99 branches or compiler-version conditionals that choose between C99 and newer implementations in every generated header.
  • Emit C99 compatibility helpers/macros only when generating with C99 support. A C99-targeted header can use its generated assertion helper directly.
  • C11/C17/C23 output must retain its native static assertions and must not acquire a C99 fallback that silently allows compilation in an incompatible language mode.
  • Keep explicit compiler-language/version guards with clear diagnostics for unsupported compilation modes. These guards reject incompatible use; they do not select a compatibility implementation. Check the requirement for each selected output standard rather than assuming the shared C11 minimum is sufficient for every target.
  • Preserve the configured C++ compatibility behavior and test it separately.

Some protection already exists: the support header has a clear C11 minimum-version #error, and generated type headers have native static assertions rejecting serialization support built with different language options (template). Preserve these protections. For an older compiler that cannot parse native static assertions, the explicit version #error is needed for the clear diagnostic; unsupported assertion syntax alone is not sufficient.

Robust C99 compile-time assertions

A named structure with a bit-field provides a portable C99 assertion at file or block scope:

#include <limits.h>
#include <stdint.h>

/* Emit this helper only for C99-targeted output. */
#define NUNAVUT_C99_ASSERT(condition, name)                 \
    struct nunavut_assert_##name {                        \
        unsigned int check : ((condition) ? 1 : -1);       \
    }

NUNAVUT_C99_ASSERT(sizeof(uint64_t) * CHAR_BIT == 64,
                   uint64_must_have_64_bits);

C99 requires a bit-field width to be an integer constant expression with a nonnegative value (C99 committee draft, section 6.7.2.1):

  • A true condition produces a valid width of 1.
  • A false condition produces an invalid width of -1.
  • A nonconstant condition is also invalid, so the check cannot accidentally become a runtime check.
  • This declares a type only: no object, storage, or runtime work is introduced.

Generate unique, descriptive names, including the type/version and assertion identity where needed to avoid collisions across generated headers. Alternatively, emit the structure declaration directly for each C99 assertion and avoid the helper macro altogether. Diagnostics are less expressive than native static-assert message strings, so preserve useful context in the generated names/comments.

The common typedef char name[(condition) ? 1 : -1] approach can become a variable-length array at block scope if its condition is not constant. The bit-field approach avoids that trap. Checked the bit-field mechanism at file and block scope with GCC and Clang using -std=c99 -pedantic-errors -Wall -Wextra -Werror: true conditions pass; false and nonconstant conditions fail. This validates the mechanism, not full C99 support for all generated code.

Work required to close
  • Accept -std=c99 and add the corresponding C language configuration.
  • Generate the C99 version guard and compile-time assertion implementation at generation time; audit the remaining generated constructs for strict C99 compatibility.
  • Keep native assertions and clear rejection diagnostics for incompatible use of other output standards, including mismatched type/support-header generation options.
  • Add strict C99 compilation and serialization verification, including floats, arrays, unions, and delimited types.
  • Test assertion success/failure/nonconstant cases, multiple assertions and generated headers in one translation unit, and the existing C++ compatibility mode.
  • Verify C11/C17/C23 output contains no new C99 fallback helpers or implementation-selection branches.
  • Update supported-standard documentation and CLI help. The current help mentions C99 even though the CLI rejects it.
Dominant language
Python
Stars
48
Forks
26
Avg merge
1d 18h
Merged PRs (30d)
9

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OpenCyphal/nunavut

All issues in OpenCyphal/nunavut

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.