tests: fix coding style

* tests/cxx-type.at, tests/glr-regression.at:
Prefer strdup to malloc+strcpy.
Use snake_case.
This commit is contained in:
Akim Demaille
2021-10-23 06:20:10 +02:00
parent 7558cbf373
commit c9b5b46d74
2 changed files with 40 additions and 44 deletions

View File

@@ -34,33 +34,33 @@ $1
%code requires %code requires
{ {
#include <stdio.h> #include <stdio.h>
union Node { union node_t {
struct { struct {
int isNterm; int is_nterm;
int parents; int parents;
} nodeInfo; } node_info;
struct { struct {
int isNterm; /* 1 */ int is_nterm; /* 1 */
int parents; int parents;
char const *form; char const *form;
union Node *children[3]; union node_t *children[3];
} nterm; } nterm;
struct { struct {
int isNterm; /* 0 */ int is_nterm; /* 0 */
int parents; int parents;
char *text; char *text;
} term; } term;
}; };
typedef union Node Node; typedef union node_t node_t;
#define YYSTYPE Node * #define YYSTYPE node_t *
} }
%code %code
{ {
static Node *new_nterm (char const *, Node *, Node *, Node *); static node_t *new_nterm (char const *, node_t *, node_t *, node_t *);
static Node *new_term (char *); static node_t *new_term (char *);
static void free_node (Node *); static void free_node (node_t *);
static char *node_to_string (Node *); static char *node_to_string (node_t *);
]m4_bmatch([$2], [stmt_merge], ]m4_bmatch([$2], [stmt_merge],
[ static YYSTYPE stmt_merge (YYSTYPE x0, YYSTYPE x1);])[ [ static YYSTYPE stmt_merge (YYSTYPE x0, YYSTYPE x1);])[
#define YYINITDEPTH 10 #define YYINITDEPTH 10
@@ -200,7 +200,7 @@ main (int argc, char **argv)
ungetc (c, stdin); ungetc (c, stdin);
buffer[i++] = 0; buffer[i++] = 0;
tok = isupper (YY_CAST (unsigned char, buffer[0])) ? TYPENAME : ID; tok = isupper (YY_CAST (unsigned char, buffer[0])) ? TYPENAME : ID;
yylval = new_term (strcpy (YY_CAST (char *, malloc (i)), buffer)); yylval = new_term (strdup (buffer));
} }
else else
{ {
@@ -215,45 +215,45 @@ main (int argc, char **argv)
} }
} }
static Node * static node_t *
new_nterm (char const *form, Node *child0, Node *child1, Node *child2) new_nterm (char const *form, node_t *child0, node_t *child1, node_t *child2)
{ {
Node *node = YY_CAST (Node *, malloc (sizeof (Node))); node_t *res = YY_CAST (node_t *, malloc (sizeof *res));
node->nterm.isNterm = 1; res->nterm.is_nterm = 1;
node->nterm.parents = 0; res->nterm.parents = 0;
node->nterm.form = form; res->nterm.form = form;
node->nterm.children[0] = child0; res->nterm.children[0] = child0;
if (child0) if (child0)
child0->nodeInfo.parents += 1; child0->node_info.parents += 1;
node->nterm.children[1] = child1; res->nterm.children[1] = child1;
if (child1) if (child1)
child1->nodeInfo.parents += 1; child1->node_info.parents += 1;
node->nterm.children[2] = child2; res->nterm.children[2] = child2;
if (child2) if (child2)
child2->nodeInfo.parents += 1; child2->node_info.parents += 1;
return node; return res;
} }
static Node * static node_t *
new_term (char *text) new_term (char *text)
{ {
Node *node = YY_CAST (Node *, malloc (sizeof (Node))); node_t *res = YY_CAST (node_t *, malloc (sizeof *res));
node->term.isNterm = 0; res->term.is_nterm = 0;
node->term.parents = 0; res->term.parents = 0;
node->term.text = text; res->term.text = text;
return node; return res;
} }
static void static void
free_node (Node *node) free_node (node_t *node)
{ {
if (!node) if (!node)
return; return;
node->nodeInfo.parents -= 1; node->node_info.parents -= 1;
/* Free only if 0 (last parent) or -1 (no parents). */ /* Free only if 0 (last parent) or -1 (no parents). */
if (node->nodeInfo.parents > 0) if (node->node_info.parents > 0)
return; return;
if (node->nodeInfo.isNterm == 1) if (node->node_info.is_nterm == 1)
{ {
free_node (node->nterm.children[0]); free_node (node->nterm.children[0]);
free_node (node->nterm.children[1]); free_node (node->nterm.children[1]);
@@ -265,15 +265,12 @@ free_node (Node *node)
} }
static char * static char *
node_to_string (Node *node) node_to_string (node_t *node)
{ {
char *res; char *res;
if (!node) if (!node)
{ res = strdup ("");
res = YY_CAST (char *, malloc (1)); else if (node->node_info.is_nterm)
res[0] = 0;
}
else if (node->nodeInfo.isNterm == 1)
{ {
char *child0 = node_to_string (node->nterm.children[0]); char *child0 = node_to_string (node->nterm.children[0]);
char *child1 = node_to_string (node->nterm.children[1]); char *child1 = node_to_string (node->nterm.children[1]);

View File

@@ -294,9 +294,8 @@ FILE *input;
return 0; return 0;
else else
{ {
char *s;
assert (strlen (buf) < sizeof buf - 1); assert (strlen (buf) < sizeof buf - 1);
s = YY_CAST (char *, malloc (strlen (buf) + 1)); char *s = YY_CAST (char *, malloc (strlen (buf) + 1));
strcpy (s, buf); strcpy (s, buf);
]AT_VAL[ = s; ]AT_VAL[ = s;
return 'V'; return 'V';