Skip to content

Commit 6ddb91b

Browse files
author
Martin Belanger
committed
generate-accessors: fix stale examples and redundant code
The examples in generate-accessors.md were generated with an older version of the tool: - excluded members leave no trace; the phantom "no accessors" comment has been removed - car struct accessors were missing their KernelDoc comments - accessors.c used 4-space indentation instead of tabs - accessors.ld was not shown at all In generate-accessors.py, collapse the redundant two-branch isdigit() check in emit_src_setter_chararray() — both branches produced identical output. Signed-off-by: Martin Belanger <[email protected]>
1 parent d4727ef commit 6ddb91b

2 files changed

Lines changed: 94 additions & 19 deletions

File tree

libnvme/tools/generator/generate-accessors.md

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ int person_get_age(const struct person *p);
205205
*/
206206
const char *person_get_id(const struct person *p);
207207

208-
/* secret: no accessors (//!accessors:none) */
209-
210208
/**
211209
* person_get_role() - Get role.
212210
* @p: The &struct person instance to query.
@@ -219,18 +217,48 @@ const char *person_get_role(const struct person *p);
219217
* Accessors for: struct car
220218
****************************************************************************/
221219

220+
/**
221+
* car_set_model() - Set model.
222+
* @p: The &struct car instance to update.
223+
* @model: New string; a copy is stored. Pass NULL to clear.
224+
*/
222225
void car_set_model(struct car *p, const char *model);
226+
227+
/**
228+
* car_get_model() - Get model.
229+
* @p: The &struct car instance to query.
230+
*
231+
* Return: The value of the model field, or NULL if not set.
232+
*/
223233
const char *car_get_model(const struct car *p);
224234

235+
/**
236+
* car_set_year() - Set year.
237+
* @p: The &struct car instance to update.
238+
* @year: Value to assign to the year field.
239+
*/
225240
void car_set_year(struct car *p, int year);
241+
242+
/**
243+
* car_get_year() - Get year.
244+
* @p: The &struct car instance to query.
245+
*
246+
* Return: The value of the year field.
247+
*/
226248
int car_get_year(const struct car *p);
227249

250+
/**
251+
* car_get_vin() - Get vin.
252+
* @p: The &struct car instance to query.
253+
*
254+
* Return: The value of the vin field, or NULL if not set.
255+
*/
228256
const char *car_get_vin(const struct car *p);
229257

230258
#endif /* _ACCESSORS_H_ */
231259
```
232260

233-
> **Note:** The `secret` member is absent because of `//!accessors:none`. The `role` member has only a getter because of `//!accessors:readonly`. The `id` and `vin` members have only getters because they are declared `const`.
261+
> **Note:** The `secret` member is absent because of `//!accessors:none` — excluded members leave no trace in the output. The `role` member has only a getter because of `//!accessors:readonly`. The `id` and `vin` members have only getters because they are declared `const`.
234262
235263
### Generated `accessors.c`
236264

@@ -251,37 +279,89 @@ const char *car_get_vin(const struct car *p);
251279

252280
__public void person_set_name(struct person *p, const char *name)
253281
{
254-
free(p->name);
255-
p->name = name ? strdup(name) : NULL;
282+
free(p->name);
283+
p->name = name ? strdup(name) : NULL;
256284
}
257285

258286
__public const char *person_get_name(const struct person *p)
259287
{
260-
return p->name;
288+
return p->name;
261289
}
262290

263291
__public void person_set_age(struct person *p, int age)
264292
{
265-
p->age = age;
293+
p->age = age;
266294
}
267295

268296
__public int person_get_age(const struct person *p)
269297
{
270-
return p->age;
298+
return p->age;
271299
}
272300

273301
__public const char *person_get_id(const struct person *p)
274302
{
275-
return p->id;
303+
return p->id;
276304
}
277305

278306
__public const char *person_get_role(const struct person *p)
279307
{
280-
return p->role;
308+
return p->role;
309+
}
310+
311+
/****************************************************************************
312+
* Accessors for: struct car
313+
****************************************************************************/
314+
315+
__public void car_set_model(struct car *p, const char *model)
316+
{
317+
free(p->model);
318+
p->model = model ? strdup(model) : NULL;
319+
}
320+
321+
__public const char *car_get_model(const struct car *p)
322+
{
323+
return p->model;
324+
}
325+
326+
__public void car_set_year(struct car *p, int year)
327+
{
328+
p->year = year;
329+
}
330+
331+
__public int car_get_year(const struct car *p)
332+
{
333+
return p->year;
281334
}
282335

283-
/* ... struct car accessors follow the same pattern ... */
336+
__public const char *car_get_vin(const struct car *p)
337+
{
338+
return p->vin;
339+
}
340+
```
341+
342+
### Generated `accessors.ld`
343+
284344
```
345+
# SPDX-License-Identifier: LGPL-2.1-or-later
346+
/* ... banner ... */
347+
348+
LIBNVME_ACCESSORS_3 {
349+
global:
350+
person_get_name;
351+
person_set_name;
352+
person_get_age;
353+
person_set_age;
354+
person_get_id;
355+
person_get_role;
356+
car_get_model;
357+
car_set_model;
358+
car_get_year;
359+
car_set_year;
360+
car_get_vin;
361+
};
362+
```
363+
364+
> **Note:** Only symbols for members that have accessors generated appear in the linker script. The `secret` member (excluded via `//!accessors:none`) and the write-only `token` example would have no getter entry. The version node name `LIBNVME_ACCESSORS_3` is hardcoded in the generator.
285365
286366
------
287367

libnvme/tools/generator/generate-accessors.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,9 @@ def emit_src_setter_chararray(f, prefix, sname, mname, array_size):
556556
f'\t\tconst char *{mname})\n'
557557
)
558558

559-
if array_size.isdigit():
560-
f.write(
561-
f'{{\n\tsnprintf(p->{mname}, {int(array_size)}, "%s", {mname});\n}}\n\n'
562-
)
563-
else:
564-
f.write(
565-
f'{{\n\tsnprintf(p->{mname}, {array_size}, "%s", {mname});\n}}\n\n'
566-
)
559+
f.write(
560+
f'{{\n\tsnprintf(p->{mname}, {array_size}, "%s", {mname});\n}}\n\n'
561+
)
567562

568563

569564
def emit_src_setter_str_array(f, prefix, sname, mname):

0 commit comments

Comments
 (0)