Skip to content

Commit 930c7bd

Browse files
committed
lx: Ensure prefix.api & prefix.lx are used in the generated code.
In some cases this was hardcoding "lx_" in the generated code, which could lead to build failures if 'lx -e' was used to override the default prefix.
1 parent 8137220 commit 930c7bd

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/lx/print/c.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ print_io(FILE *f, const struct fsm_options *opt)
454454
}
455455

456456
fprintf(f, "static int\n");
457-
fprintf(f, "lx_advance_end(struct lx *lx, int c)\n");
457+
fprintf(f, "%sadvance_end(struct %slx *lx, int c)\n", prefix.api, prefix.lx);
458458
fprintf(f, "{\n");
459459
if (~api_exclude & API_POS) {
460460
fprintf(f, "\tlx->end.byte++;\n");
@@ -498,7 +498,7 @@ print_io(FILE *f, const struct fsm_options *opt)
498498
fprintf(f, "inline\n");
499499
fprintf(f, "#endif\n");
500500
fprintf(f, "static int\n");
501-
fprintf(f, "lx_getc(struct %slx *lx)\n", prefix.lx);
501+
fprintf(f, "%sgetc(struct %slx *lx)\n", prefix.api, prefix.lx);
502502
fprintf(f, "{\n");
503503
fprintf(f, "\tint c;\n");
504504
fprintf(f, "\n");
@@ -520,7 +520,7 @@ print_io(FILE *f, const struct fsm_options *opt)
520520

521521
/* FIXME: This should distinguish between alloc failure
522522
* and EOF, but will require layers of interface changes. */
523-
fprintf(f, "\tif (!lx_advance_end(lx, c)) { return EOF; }\n");
523+
fprintf(f, "\tif (!%sadvance_end(lx, c)) { return EOF; }\n", prefix.api);
524524
fprintf(f, "\n");
525525

526526
fprintf(f, "\treturn c;\n");
@@ -529,13 +529,13 @@ print_io(FILE *f, const struct fsm_options *opt)
529529

530530
/* Add an implementation of fsm_getc that calls back
531531
* into lx_getc with the lx handle. */
532-
fprintf(f, "/* This wrapper adapts calling lx_getc to the interface\n");
532+
fprintf(f, "/* This wrapper adapts calling %sgetc to the interface\n", prefix.api);
533533
fprintf(f, " * in libfsm's generated code. */\n");
534534
fprintf(f, "static int\n");
535535
fprintf(f, "fsm_getc(void *getc_opaque)\n");
536536
fprintf(f, "{\n");
537537

538-
fprintf(f, "\treturn lx_getc((struct lx *)getc_opaque);\n");
538+
fprintf(f, "\treturn %sgetc((struct %slx *)getc_opaque);\n", prefix.api, prefix.api);
539539
fprintf(f, "}\n");
540540
fprintf(f, "\n");
541541
break;
@@ -545,7 +545,7 @@ print_io(FILE *f, const struct fsm_options *opt)
545545
/* When libfsm's generated code advances a character, update
546546
* lx's token name buffer and position bookkeeping. */
547547
fprintf(f, "#ifndef FSM_ADVANCE_HOOK\n");
548-
fprintf(f, "#define FSM_ADVANCE_HOOK(C) if (!lx_advance_end(lx, C)) { return %sERROR; }\n", prefix.tok);
548+
fprintf(f, "#define FSM_ADVANCE_HOOK(C) if (!%sadvance_end(lx, C)) { return %sERROR; }\n", prefix.api, prefix.tok);
549549
fprintf(f, "#endif\n");
550550
fprintf(f, "\n");
551551
break;

src/lx/print/dump.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
6868
fprintf(f, "main(int argc, char *argv[])\n");
6969
fprintf(f, "{\n");
7070

71-
fprintf(f, "\tenum lx_token t;\n");
72-
fprintf(f, "\tstruct lx lx = { 0 };\n");
71+
fprintf(f, "\tenum %stoken t;\n", prefix.api);
72+
fprintf(f, "\tstruct %slx lx = { 0 };\n", prefix.lx);
7373

7474
switch (opt->io) {
7575
case FSM_IO_GETC:
76-
fprintf(f, "\tint (*lgetc)(struct lx *lx);\n");
76+
fprintf(f, "\tint (*lgetc)(struct %slx *lx);\n", prefix.lx);
7777
fprintf(f, "\tvoid *getc_opaque;\n");
7878
break;
7979

@@ -135,7 +135,7 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
135135

136136
switch (api_getc) {
137137
case API_FGETC:
138-
fprintf(f, "\tlgetc = lx_fgetc;\n");
138+
fprintf(f, "\tlgetc = %sfgetc;\n", prefix.api);
139139
fprintf(f, "\tgetc_opaque = stdin;\n");
140140
fprintf(f, "\n");
141141
break;
@@ -144,7 +144,7 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
144144
fprintf(f, "\ts = argv[1];\n");
145145
fprintf(f, "\n");
146146

147-
fprintf(f, "\tlgetc = lx_sgetc;\n");
147+
fprintf(f, "\tlgetc = %ssgetc;\n", prefix.api);
148148
fprintf(f, "\tgetc_opaque = &s;\n");
149149
fprintf(f, "\n");
150150
break;
@@ -154,7 +154,7 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
154154
fprintf(f, "\tarr.len = strlen(arr.p);\n");
155155
fprintf(f, "\n");
156156

157-
fprintf(f, "\tlgetc = lx_agetc;\n");
157+
fprintf(f, "\tlgetc = %sagetc;\n", prefix.api);
158158
fprintf(f, "\tgetc_opaque = &arr;\n");
159159
fprintf(f, "\n");
160160
break;
@@ -167,13 +167,13 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
167167
fprintf(f, "\td.fd = fileno(stdin);\n");
168168
fprintf(f, "\n");
169169

170-
fprintf(f, "\tlgetc = lx_dgetc;\n");
170+
fprintf(f, "\tlgetc = %sdgetc;\n", prefix.api);
171171
fprintf(f, "\tgetc_opaque = &d;\n");
172172
fprintf(f, "\n");
173173
break;
174174
}
175175

176-
fprintf(f, "\tlx_init(&lx);\n");
176+
fprintf(f, "\t%sinit(&lx);\n", prefix.api);
177177
fprintf(f, "\n");
178178

179179
switch (opt->io) {
@@ -203,9 +203,9 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
203203

204204
if (~api_exclude & API_BUF) {
205205
fprintf(f, "\tlx.buf_opaque = &buf;\n");
206-
fprintf(f, "\tlx.push = lx_dynpush;\n");
207-
fprintf(f, "\tlx.clear = lx_dynclear;\n");
208-
fprintf(f, "\tlx.free = lx_dynfree;\n");
206+
fprintf(f, "\tlx.push = %sdynpush;\n", prefix.api);
207+
fprintf(f, "\tlx.clear = %sdynclear;\n", prefix.api);
208+
fprintf(f, "\tlx.free = %sdynfree;\n", prefix.api);
209209
}
210210
fprintf(f, "\n");
211211
break;
@@ -218,8 +218,8 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
218218

219219
if (~api_exclude & API_BUF) {
220220
fprintf(f, "\tlx.buf_opaque = &buf;\n");
221-
fprintf(f, "\tlx.push = lx_fixedpush;\n");
222-
fprintf(f, "\tlx.clear = lx_fixedclear;\n");
221+
fprintf(f, "\tlx.push = %sfixedpush;\n", prefix.api);
222+
fprintf(f, "\tlx.clear = %sfixedclear;\n", prefix.api);
223223
fprintf(f, "\tlx.free = NULL;\n");
224224
}
225225
fprintf(f, "\n");
@@ -231,7 +231,7 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
231231
fprintf(f, "\t\tconst char *q;\n");
232232
fprintf(f, "\n");
233233

234-
fprintf(f, "\t\tt = lx_next(&lx);\n");
234+
fprintf(f, "\t\tt = %snext(&lx);\n", prefix.api);
235235
fprintf(f, "\n");
236236

237237
switch (api_tokbuf) {
@@ -278,7 +278,7 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
278278
fprintf(f, "\n");
279279

280280
fprintf(f, "\t\tcase TOK_ERROR:\n");
281-
fprintf(f, "\t\t\tperror(\"lx_next\");\n");
281+
fprintf(f, "\t\t\tperror(\"%snext\");\n", prefix.api);
282282
fprintf(f, "\t\t\tbreak;\n");
283283
fprintf(f, "\n");
284284

@@ -291,7 +291,7 @@ lx_print_dump(FILE *f, const struct ast *ast, const struct fsm_options *opt)
291291

292292
fprintf(f, "\t\tdefault:\n");
293293
if (~api_exclude & API_NAME) {
294-
fprintf(f, "\t\t\tprintf(\"<%%s\", lx_name(t));\n");
294+
fprintf(f, "\t\t\tprintf(\"<%%s\", %sname(t));\n", prefix.api);
295295
fprintf(f, "\t\t\tdump_buf(q, l);\n");
296296
fprintf(f, "\t\t\tprintf(\">\\n\");\n");
297297
} else {

0 commit comments

Comments
 (0)