Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions libnvme/src/nvme/generate-accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,24 @@
#define SPACES " \t\n\r"
#define streq(a, b) (strcmp((a), (b)) == 0)

/**
* Function naming convention:
* This controls whether to generate the functions as:
* nvme_foo_get() / nvme_foo_set()
* Or:
* nvme_get_foo() / nvme_set_foo()
*/
#define SET_GET_AT_THE_END
#ifdef SET_GET_AT_THE_END
# define SET_FMT "%s%s_%s_set"
# define GET_FMT "%s%s_%s_get"
#else
# define SET_FMT "%s%s_set_%s"
# define GET_FMT "%s%s_get_%s"
#endif

static const char *banner =
"// SPDX-License-Identifier: LGPL-2.1-or-later\n"

Check failure on line 78 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: Misplaced SPDX-License-Identifier tag - use line 1 instead
"/**\n"
" * This file is part of libnvme.\n"
" *\n"
Expand Down Expand Up @@ -207,8 +223,8 @@
* Any character that violates these rules is replaced with an underscore ('_').
* The string is always modified in place; no new memory is allocated.
*
* @param s Pointer to the NUL-terminated string to sanitize.
* If @p s is NULL or points to an empty string, the function does nothing.
* @param s Pointer to the NUL-terminated string to sanitize. If @s is NULL or
* points to an empty string, the function does nothing.
*
* @note This function does not check for C keywords or identifier length limits.
*
Expand Down Expand Up @@ -808,7 +824,7 @@
bool verbose;
const char *c_fname; /* Generated output *.c file name */
const char *h_fname; /* Generated output *.h file name */
const char *l_fname; /* Generated ou5tput *.ld file name */
const char *l_fname; /* Generated output *.ld file name */
const char *prefix; /* Prefix added to each functions */
StringList_t hdr_files; /* Input header file list */
StringList_t incl_list; /* Inclusion list (read from --incl) */
Expand Down Expand Up @@ -1269,18 +1285,18 @@
if (!members->is_const) { /* No setter on const members */
if (members->is_char_array || streq(members->type, "const char *"))
fprintf(generated_hdr,
"void %s%s_%s_set(struct %s *p, const char *%s);\n",
"void " SET_FMT "(struct %s *p, const char *%s);\n",

Check failure on line 1288 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 92 exceeds 80 columns
conf->prefix, si->name,
members->name, si->name, members->name);
else
fprintf(generated_hdr,
"void %s%s_%s_set(struct %s *p, %s %s);\n",
"void " SET_FMT "(struct %s *p, %s %s);\n",

Check failure on line 1293 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 83 exceeds 80 columns
conf->prefix, si->name,
members->name, si->name, members->type, members->name);
}

/* Getter method */
fprintf(generated_hdr, "%s %s%s_%s_get(const struct %s *p);\n\n",
fprintf(generated_hdr, "%s " GET_FMT "(const struct %s *p);\n\n",

Check failure on line 1299 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 81 exceeds 80 columns
members->type, conf->prefix, si->name, members->name, si->name);
}
}
Expand Down Expand Up @@ -1309,7 +1325,7 @@
if (!member->is_char_array && streq(member->type, "const char *")) {
/* dynamic string */
fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
"void " SET_FMT "(struct %s *p, const char *%s) {\n"

Check failure on line 1328 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 92 exceeds 80 columns
" free(p->%s);\n"
" p->%s = %s ? strdup(%s) : NULL;\n"
"}\n\n",
Expand All @@ -1323,7 +1339,7 @@
unsigned long sz = strtoul(member->array_size, NULL, 10);

fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
"void " SET_FMT "(struct %s *p, const char *%s) {\n"

Check failure on line 1342 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 100 exceeds 80 columns
" strncpy(p->%s, %s, %lu);\n"
" p->%s[%lu] = '\\0';\n"
"}\n\n",
Expand All @@ -1333,7 +1349,7 @@
member->name, sz - 1);
} else {
fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, const char *%s) {\n"
"void " SET_FMT "(struct %s *p, const char *%s) {\n"

Check failure on line 1352 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 100 exceeds 80 columns
" strncpy(p->%s, %s, %s);\n"
" p->%s[%s - 1] = '\\0';\n"
"}\n\n",
Expand All @@ -1344,7 +1360,7 @@
}
} else { /* numeric or struct */
fprintf(generated_src,
"void %s%s_%s_set(struct %s *p, %s %s) {\n"
"void " SET_FMT "(struct %s *p, %s %s) {\n"

Check failure on line 1363 in libnvme/src/nvme/generate-accessors.c

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 83 exceeds 80 columns
" p->%s = %s;\n"
"}\n\n",
conf->prefix, si->name, member->name, si->name,
Expand All @@ -1355,7 +1371,7 @@
}

/* Getter method */
fprintf(generated_src, "%s %s%s_%s_get(const struct %s *p) {\n"
fprintf(generated_src, "%s " GET_FMT "(const struct %s *p) {\n"
" return p->%s;\n"
"}\n\n",
member->type, conf->prefix, si->name, member->name, si->name,
Expand All @@ -1368,7 +1384,7 @@
* one struct.
*
* Writes linker entries for each member in @si to the provided output
* FILE (@generated_ld). Handles special
* FILE (@generated_ld).
*
* @param generated_ld: FILE* to write implementations to.
* @param si: Pointer to the struct description.
Expand All @@ -1380,8 +1396,8 @@
Member_t *member = &si->members[m];

fprintf(generated_ld,
"\t\t%s%s_%s_get;\n"
"\t\t%s%s_%s_set;\n",
"\t\t" GET_FMT ";\n"
"\t\t" SET_FMT ";\n",
conf->prefix, si->name, member->name,
conf->prefix, si->name, member->name);
}
Expand Down