Skip to content

Add constexpr variant of --bfbs-gen-embed for compile-time schema processing#9063

Open
BenniRip wants to merge 1 commit intogoogle:masterfrom
BenniRip:feat/bfbs-gen-embed-constexpr
Open

Add constexpr variant of --bfbs-gen-embed for compile-time schema processing#9063
BenniRip wants to merge 1 commit intogoogle:masterfrom
BenniRip:feat/bfbs-gen-embed-constexpr

Conversation

@BenniRip
Copy link
Copy Markdown

Summary

Add --bfbs-gen-embed-constexpr flag that generates a file-scope inline constexpr array for the binary schema, as an alternative to the existing --bfbs-gen-embed struct wrapper.

Motivation

The current --bfbs-gen-embed wraps the binary schema in a struct with a function-local static:

struct FooBinarySchema {
  static const uint8_t *data() {
    static const uint8_t bfbsData[N] = { ... };
    return bfbsData;
  }
};

Function-local statics are not usable in constant expressions which prevents compile-time schema processing, e.g. computing worst-case serialized sizes from (capacity: N) attributes at compile time.

What this PR does

Adds a new flag --bfbs-gen-embed-constexpr that produces:

inline constexpr uint8_t FooBinarySchema[] = { ... };
inline constexpr size_t FooBinarySchemaSize = N;

The existing --bfbs-gen-embed output is unchanged. The two flags are mutually exclusive (same output filename, different content).

Changes

  • include/flatbuffers/idl.h — new binary_schema_gen_embed_constexpr option
  • src/flatc.cpp — flag declaration and parsing
  • src/idl_gen_cpp.cpp — conditional codegen in generate_bfbs_embed(); skip BinarySchema typedef in GenBinarySchemaTypeDef() in constexpr mode (the typedef references a struct type, but constexpr mode generates a bare array instead)

The generated output requires C++17 (inline constexpr), which is expected since the flag is opt-in for users who need constexpr.

Testing

  • Built flatc, ran flattests — ALL TESTS PASSED
  • Ran scripts/generate_code.py — no C++ diff
  • Ran scripts/clang-format-git.sh — no modifications
  • Verified generated output for monster_test.fbs with the new flag

Note on goldens

Both flags produce the same output filename (<name>_bfbs_generated.h). Happy to add a golden for the new flag if you'd like. Would you prefer a separate filename suffix (e.g. _bfbs_constexpr_generated.h) so both can coexist, or is the current shared filename appropriate?

Generated output for basic.fbs
// automatically generated by the FlatBuffers compiler, do not modify


#ifndef FLATBUFFERS_GENERATED_BASIC_FLATBUFFERS_GOLDENS_BFBS_H_
#define FLATBUFFERS_GENERATED_BASIC_FLATBUFFERS_GOLDENS_BFBS_H_

#include <cstddef>
#include <cstdint>
namespace flatbuffers {
namespace goldens {

inline constexpr uint8_t UniverseBinarySchema[] = {
  0x1C,0x00,0x00,0x00,0x42,0x46,0x42,0x53,0x14,0x00,0x20,0x00,0x04,0x00,0x08,0x00,0x0C,0x00,0x10,0x00,0x14,
  0x00,0x18,0x00,0x00,0x00,0x1C,0x00,0x14,0x00,0x00,0x00,0x34,0x00,0x00,0x00,0x2C,0x00,0x00,0x00,0x20,0x00,
  0x00,0x00,0x14,0x00,0x00,0x00,0x50,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x02,0x00,0x00,0x00,0x08,0x01,0x00,0x00,0x24,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x08,
  0x00,0x0C,0x00,0x04,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x34,0xFF,0xFF,0xFF,0x1C,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0xE4,0x00,0x00,
  0x00,0x02,0x00,0x00,0x00,0x8C,0x00,0x00,0x00,0x44,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x66,0x6C,0x61,0x74,
  0x62,0x75,0x66,0x66,0x65,0x72,0x73,0x2E,0x67,0x6F,0x6C,0x64,0x65,0x6E,0x73,0x2E,0x55,0x6E,0x69,0x76,0x65,
  0x72,0x73,0x65,0x00,0x00,0x00,0x00,0x1C,0x00,0x14,0x00,0x0C,0x00,0x10,0x00,0x08,0x00,0x0A,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,
  0x01,0x01,0x00,0x06,0x00,0x28,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x06,0x00,0x07,0x00,
  0x08,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x0E,0x0F,0x00,0x00,0x00,0x00,0x04,
  0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x67,0x61,0x6C,0x61,0x78,0x69,0x65,0x73,0x00,0x00,0x00,0x00,0x78,0xFF,
  0xFF,0xFF,0x00,0x00,0x04,0x00,0x18,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x6C,0xFF,0xFF,0xFF,0x00,0x00,0x00,
  0x0C,0x08,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x61,0x67,0x65,0x00,0x14,0x00,0x14,0x00,
  0x04,0x00,0x08,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x14,0x00,0x00,0x00,0x28,
  0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x0B,0x00,0x00,0x00,0x2F,0x2F,
  0x62,0x61,0x73,0x69,0x63,0x2E,0x66,0x62,0x73,0x00,0x01,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x1A,0x00,0x00,
  0x00,0x66,0x6C,0x61,0x74,0x62,0x75,0x66,0x66,0x65,0x72,0x73,0x2E,0x67,0x6F,0x6C,0x64,0x65,0x6E,0x73,0x2E,
  0x47,0x61,0x6C,0x61,0x78,0x79,0x00,0x00,0x0C,0x00,0x10,0x00,0x08,0x00,0x0C,0x00,0x00,0x00,0x06,0x00,0x0C,
  0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x28,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x07,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x0C,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x08,0x00,0x00,
  0x00,0x01,0x00,0x00,0x00,0x09,0x00,0x00,0x00,0x6E,0x75,0x6D,0x5F,0x73,0x74,0x61,0x72,0x73,0x00,0x00,0x00
};
inline constexpr size_t UniverseBinarySchemaSize = 504;

}  // namespace goldens
}  // namespace flatbuffers

#endif  // FLATBUFFERS_GENERATED_BASIC_FLATBUFFERS_GOLDENS_BFBS_H_  

Why: The existing --bfbs-gen-embed generates a struct with a
function-local static array, making the binary schema inaccessible
in constexpr contexts (e.g. compile-time schema processing).

How: A new flag --bfbs-gen-embed-constexpr generates an inline
constexpr file-scope array and size constant instead:

  inline constexpr uint8_t FooBinarySchema[] = { ... };
  inline constexpr size_t FooBinarySchemaSize = N;

The existing --bfbs-gen-embed output is unchanged.
@github-actions github-actions Bot added c++ codegen Involving generating code from schema labels Apr 25, 2026
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 25, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@BenniRip BenniRip marked this pull request as ready for review April 25, 2026 22:20
@BenniRip BenniRip requested a review from dbaileychess as a code owner April 25, 2026 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant