Add constexpr variant of --bfbs-gen-embed for compile-time schema processing#9063
Open
BenniRip wants to merge 1 commit intogoogle:masterfrom
Open
Add constexpr variant of --bfbs-gen-embed for compile-time schema processing#9063BenniRip wants to merge 1 commit intogoogle:masterfrom
BenniRip wants to merge 1 commit intogoogle:masterfrom
Conversation
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.
|
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. |
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.
Summary
Add
--bfbs-gen-embed-constexprflag that generates a file-scopeinline constexprarray for the binary schema, as an alternative to the existing--bfbs-gen-embedstruct wrapper.Motivation
The current
--bfbs-gen-embedwraps the binary schema in a struct with a function-local static: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-constexprthat produces:The existing
--bfbs-gen-embedoutput is unchanged. The two flags are mutually exclusive (same output filename, different content).Changes
include/flatbuffers/idl.h— newbinary_schema_gen_embed_constexproptionsrc/flatc.cpp— flag declaration and parsingsrc/idl_gen_cpp.cpp— conditional codegen ingenerate_bfbs_embed(); skipBinarySchematypedef inGenBinarySchemaTypeDef()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 needconstexpr.Testing
flattests— ALL TESTS PASSEDscripts/generate_code.py— no C++ diffscripts/clang-format-git.sh— no modificationsmonster_test.fbswith the new flagNote 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