Skip to content

Commit 7702f8a

Browse files
author
Martin Belanger
committed
generate-accessors: make accessor naming convention configurable
Replace hardcoded "%s%s_%s_get/set" format strings with SET_FMT/GET_FMT macros controlled by the SET_GET_AT_THE_END compile-time flag, allowing easy switching between nvme_foo_get()/nvme_foo_set() and nvme_get_foo()/nvme_set_foo() naming styles. Also fix a handful of comment issues. Signed-off-by: Martin Belanger <[email protected]>
1 parent 7d8d11d commit 7702f8a

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

libnvme/src/nvme/generate-accessors.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@
5858
#define SPACES " \t\n\r"
5959
#define streq(a, b) (strcmp((a), (b)) == 0)
6060

61+
/**
62+
* Function naming convention:
63+
* This controls whether to generate the functions as:
64+
* nvme_foo_get() / nvme_foo_set()
65+
* Or:
66+
* nvme_get_foo() / nvme_set_foo()
67+
*/
68+
#define SET_GET_AT_THE_END
69+
#ifdef SET_GET_AT_THE_END
70+
# define SET_FMT "%s%s_%s_set"
71+
# define GET_FMT "%s%s_%s_get"
72+
#else
73+
# define SET_FMT "%s%s_set_%s"
74+
# define GET_FMT "%s%s_get_%s"
75+
#endif
76+
6177
static const char *banner =
6278
"// SPDX-License-Identifier: LGPL-2.1-or-later\n"
6379
"/**\n"
@@ -207,8 +223,8 @@ static char *to_uppercase(char *s)
207223
* Any character that violates these rules is replaced with an underscore ('_').
208224
* The string is always modified in place; no new memory is allocated.
209225
*
210-
* @param s Pointer to the NUL-terminated string to sanitize.
211-
* If @p s is NULL or points to an empty string, the function does nothing.
226+
* @param s Pointer to the NUL-terminated string to sanitize. If @s is NULL or
227+
* points to an empty string, the function does nothing.
212228
*
213229
* @note This function does not check for C keywords or identifier length limits.
214230
*
@@ -808,7 +824,7 @@ typedef struct Conf {
808824
bool verbose;
809825
const char *c_fname; /* Generated output *.c file name */
810826
const char *h_fname; /* Generated output *.h file name */
811-
const char *l_fname; /* Generated ou5tput *.ld file name */
827+
const char *l_fname; /* Generated output *.ld file name */
812828
const char *prefix; /* Prefix added to each functions */
813829
StringList_t hdr_files; /* Input header file list */
814830
StringList_t incl_list; /* Inclusion list (read from --incl) */
@@ -1269,18 +1285,18 @@ static void generate_hdr(FILE *generated_hdr, StructInfo_t *si, Conf_t *conf)
12691285
if (!members->is_const) { /* No setter on const members */
12701286
if (members->is_char_array || streq(members->type, "const char *"))
12711287
fprintf(generated_hdr,
1272-
"void %s%s_%s_set(struct %s *p, const char *%s);\n",
1288+
"void " SET_FMT "(struct %s *p, const char *%s);\n",
12731289
conf->prefix, si->name,
12741290
members->name, si->name, members->name);
12751291
else
12761292
fprintf(generated_hdr,
1277-
"void %s%s_%s_set(struct %s *p, %s %s);\n",
1293+
"void " SET_FMT "(struct %s *p, %s %s);\n",
12781294
conf->prefix, si->name,
12791295
members->name, si->name, members->type, members->name);
12801296
}
12811297

12821298
/* Getter method */
1283-
fprintf(generated_hdr, "%s %s%s_%s_get(const struct %s *p);\n\n",
1299+
fprintf(generated_hdr, "%s " GET_FMT "(const struct %s *p);\n\n",
12841300
members->type, conf->prefix, si->name, members->name, si->name);
12851301
}
12861302
}
@@ -1309,7 +1325,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
13091325
if (!member->is_char_array && streq(member->type, "const char *")) {
13101326
/* dynamic string */
13111327
fprintf(generated_src,
1312-
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
1328+
"void " SET_FMT "(struct %s *p, const char *%s) {\n"
13131329
" free(p->%s);\n"
13141330
" p->%s = %s ? strdup(%s) : NULL;\n"
13151331
"}\n\n",
@@ -1323,7 +1339,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
13231339
unsigned long sz = strtoul(member->array_size, NULL, 10);
13241340

13251341
fprintf(generated_src,
1326-
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
1342+
"void " SET_FMT "(struct %s *p, const char *%s) {\n"
13271343
" strncpy(p->%s, %s, %lu);\n"
13281344
" p->%s[%lu] = '\\0';\n"
13291345
"}\n\n",
@@ -1333,7 +1349,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
13331349
member->name, sz - 1);
13341350
} else {
13351351
fprintf(generated_src,
1336-
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
1352+
"void " SET_FMT "(struct %s *p, const char *%s) {\n"
13371353
" strncpy(p->%s, %s, %s);\n"
13381354
" p->%s[%s - 1] = '\\0';\n"
13391355
"}\n\n",
@@ -1344,7 +1360,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
13441360
}
13451361
} else { /* numeric or struct */
13461362
fprintf(generated_src,
1347-
"void %s%s_%s_set(struct %s *p, %s %s) {\n"
1363+
"void " SET_FMT "(struct %s *p, %s %s) {\n"
13481364
" p->%s = %s;\n"
13491365
"}\n\n",
13501366
conf->prefix, si->name, member->name, si->name,
@@ -1355,7 +1371,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
13551371
}
13561372

13571373
/* Getter method */
1358-
fprintf(generated_src, "%s %s%s_%s_get(const struct %s *p) {\n"
1374+
fprintf(generated_src, "%s " GET_FMT "(const struct %s *p) {\n"
13591375
" return p->%s;\n"
13601376
"}\n\n",
13611377
member->type, conf->prefix, si->name, member->name, si->name,
@@ -1368,7 +1384,7 @@ static void generate_src(FILE *generated_src, StructInfo_t *si, Conf_t *conf)
13681384
* one struct.
13691385
*
13701386
* Writes linker entries for each member in @si to the provided output
1371-
* FILE (@generated_ld). Handles special
1387+
* FILE (@generated_ld).
13721388
*
13731389
* @param generated_ld: FILE* to write implementations to.
13741390
* @param si: Pointer to the struct description.
@@ -1380,8 +1396,8 @@ static void generate_ld(FILE *generated_ld, StructInfo_t *si, Conf_t *conf)
13801396
Member_t *member = &si->members[m];
13811397

13821398
fprintf(generated_ld,
1383-
"\t\t%s%s_%s_get;\n"
1384-
"\t\t%s%s_%s_set;\n",
1399+
"\t\t" GET_FMT ";\n"
1400+
"\t\t" SET_FMT ";\n",
13851401
conf->prefix, si->name, member->name,
13861402
conf->prefix, si->name, member->name);
13871403
}

0 commit comments

Comments
 (0)