generate-accessors: add lifecycle, defaults, and annotation redesign#3283
Open
martin-belanger wants to merge 1 commit intolinux-nvme:masterfrom
Open
generate-accessors: add lifecycle, defaults, and annotation redesign#3283martin-belanger wants to merge 1 commit intolinux-nvme:masterfrom
martin-belanger wants to merge 1 commit intolinux-nvme:masterfrom
Conversation
1fc0236 to
97aeff5
Compare
97aeff5 to
8c4a0bf
Compare
Extend the accessor generator with three new capabilities.
1. Lifecycle (constructor + destructor): annotate a struct's opening brace
with //!generate-lifecycle to generate foo_new() and foo_free().
foo_new() allocates a zeroed instance with calloc() and returns
-EINVAL / -ENOMEM on error. foo_free() frees all owned char* and char**
members then frees the struct. Passing NULL to foo_free() is safe:
destructors that dereference members guard with if (!p) return; those
with no members to dereference rely on free(NULL) being a no-op.
//!lifecycle:none on a member excludes it from the destructor. const
char* members are never freed (assumed externally owned).
2. Defaults (init function): annotate individual members with
//!default:VALUE to generate foo_init_defaults(), which assigns each
annotated field its compile-time default. Any valid C expression is
accepted as the value. When combined with //!generate-lifecycle,
foo_new() calls foo_init_defaults() after allocation. foo_init_defaults()
is also useful standalone to re-initialise a struct without reallocating.
3. Annotation style: drop support for the /*!annotation*/ block-comment
style. Annotations now use only the // line-comment style. The parser
was redesigned accordingly: after //, each !keyword token is treated as
a command, so multiple annotations may share one comment:
struct foo { //!generate-accessors !generate-lifecycle
private.h and private-fabrics.h are updated throughout to use the new
style.
The generated .c files now include <errno.h> for EINVAL/ENOMEM.
Apply these features to struct libnvmf_discovery_args, replacing the
manually-written libnvmf_discovery_args_create() and
libnvmf_discovery_args_free() with generated equivalents. The
constructor is renamed _new (consistent with the generated naming
convention).
Signed-off-by: Martin Belanger <martin.belanger@dell.com>
Assisted-by: Claude Sonnet 4.6 <noreply@anthropic.com>
8c4a0bf to
3fdc002
Compare
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.
Extend the accessor generator with three new capabilities.
Lifecycle (constructor + destructor): annotate a struct's opening brace with
//!generate-lifecycleto generatefoo_new()andfoo_free().foo_new()allocates a zeroed instance withcalloc()and returns-EINVAL/-ENOMEMon error.foo_free()frees all ownedchar*andchar**members then frees the struct. PassingNULLtofoo_free()is safe: destructors that dereference members guard withif (!p) return;those with no members to dereference rely onfree(NULL)being a no-op.//!lifecycle:noneon a member excludes it from the destructor.const char*members are never freed (assumed externally owned).Defaults (init function): annotate individual members with
//!default:VALUEto generatefoo_init_defaults(), which assigns each annotated field its compile-time default. Any valid C expression is accepted as the value. When combined with//!generate-lifecycle,foo_new()callsfoo_init_defaults()after allocation.foo_init_defaults()is also useful standalone to re-initialise a struct without reallocating.Annotation style: drop support for the
/*!annotation*/block-comment style. Annotations now use only the//line-comment style. The parser was redesigned accordingly: after//, each!keywordtoken is treated as a command, so multiple annotations may share one comment:private.handprivate-fabrics.hare updated throughout to use the new style.The generated
.cfiles now include<errno.h>forEINVAL/ENOMEM.Apply these features to
struct libnvmf_discovery_args, replacing the manually-writtenlibnvmf_discovery_args_create()andlibnvmf_discovery_args_free()with generated equivalents. The constructor is renamed_new(consistent with the generated naming convention).