From 64cf0328063cd97dec09536a8f2bb35ce7350ffe Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 5 Oct 2020 10:59:29 -0400 Subject: [PATCH] Eliminate a lurking cpp-ism from scan.l Also, remove now-unused functions from buf.c. And corral another global. Produces no diffs in generated test code. #63 in the retargeting patch series --- src/buf.c | 106 ---------------------------------------------- src/cpp_backend.c | 4 +- src/flexdef.h | 10 +---- src/main.c | 6 +-- src/scan.l | 8 +++- 5 files changed, 13 insertions(+), 121 deletions(-) diff --git a/src/buf.c b/src/buf.c index 04fad267..6f049b4a 100644 --- a/src/buf.c +++ b/src/buf.c @@ -47,25 +47,9 @@ /* global buffers. */ struct Buf userdef_buf; /**< for user #definitions triggered by cmd-line. */ -struct Buf defs_buf; /**< for #define's autogenerated. List of strings. */ struct Buf yydmap_buf; /**< string buffer to hold yydmap elements */ struct Buf top_buf; /**< contains %top code. String buffer. */ -struct Buf *buf_print_strings(struct Buf * buf, FILE* out) -{ - int i; - - if(!buf || !out) - return buf; - - for (i=0; i < buf->nelts; i++){ - const char * s = ((char**)buf->elts)[i]; - if(s) - fprintf(out, "%s", s); - } - return buf; -} - /* Append a "%s" formatted string to a string buffer */ struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) { @@ -82,52 +66,6 @@ struct Buf *buf_prints (struct Buf *buf, const char *fmt, const char *s) return buf; } -/** Append a line directive to the string buffer. - * @param buf A string buffer. - * @param filename file name - * @param lineno line number - * @return buf - */ -struct Buf *buf_linedir (struct Buf *buf, const char* filename, int lineno) -{ - char *dst, *t; - const char *src; - size_t tsz; - - if (ctrl.gen_line_dirs) - return buf; - - tsz = strlen("#line \"\"\n") + /* constant parts */ - 2 * strlen (filename) + /* filename with possibly all backslashes escaped */ - (size_t) (1 + ceil (log10 (abs (lineno)))) + /* line number */ - 1; /* NUL */ - t = malloc(tsz); - if (!t) - flexfatal (_("Allocation of buffer for line directive failed")); - for (dst = t + snprintf (t, tsz, "#line %d \"", lineno), src = filename; *src; *dst++ = *src++) - if (*src == '\\') /* escape backslashes */ - *dst++ = '\\'; - *dst++ = '"'; - *dst++ = '\n'; - *dst = '\0'; - buf = buf_strappend (buf, t); - free(t); - return buf; -} - - -/** Append the contents of @a src to @a dest. - * @param @a dest the destination buffer - * @param @a dest the source buffer - * @return @a dest - */ -struct Buf *buf_concat(struct Buf* dest, const struct Buf* src) -{ - buf_append(dest, src->elts, src->nelts); - return dest; -} - - /* Appends n characters in str to buf. */ struct Buf *buf_strnappend (struct Buf *buf, const char *str, int n) { @@ -145,50 +83,6 @@ struct Buf *buf_strappend (struct Buf *buf, const char *str) return buf_strnappend (buf, str, (int) strlen (str)); } -/** Pushes "m4_define( [[def]], [[val]])m4_dnl" to end of buffer. - * @param buf A buffer as a list of strings. - * @param def The m4 symbol to define. - * @param val The definition; may be NULL. - * @return buf - */ -struct Buf *buf_m4_define (struct Buf *buf, const char* def, const char* val) -{ - const char * fmt = "m4_define( [[%s]], [[[[%s]]]])m4_dnl\n"; - char * str; - size_t strsz; - - val = val?val:""; - strsz = strlen(fmt) + strlen(def) + strlen(val) + 2; - str = malloc(strsz); - if (!str) - flexfatal (_("Allocation of buffer for m4 def failed")); - - snprintf(str, strsz, fmt, def, val); - buf_append(buf, &str, 1); - return buf; -} - -/** Pushes "m4_undefine([[def]])m4_dnl" to end of buffer. - * @param buf A buffer as a list of strings. - * @param def The m4 symbol to undefine. - * @return buf - */ -struct Buf *buf_m4_undefine (struct Buf *buf, const char* def) -{ - const char * fmt = "m4_undefine( [[%s]])m4_dnl\n"; - char * str; - size_t strsz; - - strsz = strlen(fmt) + strlen(def) + 2; - str = malloc(strsz); - if (!str) - flexfatal (_("Allocation of buffer for m4 undef failed")); - - snprintf(str, strsz, fmt, def); - buf_append(buf, &str, 1); - return buf; -} - /* create buf with 0 elements, each of size elem_size. */ void buf_init (struct Buf *buf, size_t elem_size) { diff --git a/src/cpp_backend.c b/src/cpp_backend.c index 40a0549e..8446b714 100644 --- a/src/cpp_backend.c +++ b/src/cpp_backend.c @@ -38,12 +38,12 @@ /* These typedefs are only used for computing footprint sizes, * You need to make sure they match reality in the skeleton file to * get accurate numbers, but they don't otherwise matter. - * FIXME" This shiould go away when tFkex ships only macros. + * FIXME: This shiould go away when tFkex ships only macros. */ typedef char YY_CHAR; struct yy_trans_info {int32_t yy_verify; int32_t yy_nxt;}; -/* Helper fubctions */ +/* Helper functions */ static const char *cpp_get_int16_decl (void) { diff --git a/src/flexdef.h b/src/flexdef.h index 9a4090cd..4e44ba5e 100644 --- a/src/flexdef.h +++ b/src/flexdef.h @@ -357,7 +357,7 @@ struct ctrl_bundle_t { bool fulltbl; // (-Cf flag) don't compress the DFA state table bool gen_line_dirs; // (no -L flag) generate #line directives trit interactive; // (-I) generate an interactive scanner - bool never_interactive; // always use buffered input, don't check for tty/ + bool never_interactive; // always use buffered input, don't check for tty. bool lex_compat; // (-l), maximize compatibility with AT&T lex bool long_align; // (-Ca flag), favor long-word alignment for speed bool no_input; // Suppress use of imnput() @@ -366,6 +366,7 @@ struct ctrl_bundle_t { char *prefix; // prefix for externally visible names, default "yy" bool reentrant; // if true (-R), generate a reentrant C scanner bool stack_used; // Enable use of start-condition stacks + bool no_section3_escape;// True if the undocumented option --unsafe-no-m4-sect3-escape was passed bool spprdflt; // (-s) suppress the default rule bool useecs; // (-Ce flag) use equivalence classes bool usemecs; // (-Cm flag), use meta-equivalence classes @@ -461,7 +462,6 @@ extern struct flex_backend_t *backend; * dataline - number of contiguous lines of data in current data * statement. Used to generate readable -f output * linenum - current input line number - * skel - compiled-in skeleton array * skel_ind - index into "skel" array, if skelfile is nil * yyin - input file * infilename - name of input file @@ -1100,19 +1100,13 @@ struct Buf { extern void buf_init(struct Buf * buf, size_t elem_size); extern void buf_destroy(struct Buf * buf); extern struct Buf *buf_append(struct Buf * buf, const void *ptr, int n_elem); -extern struct Buf *buf_concat(struct Buf* dest, const struct Buf* src); extern struct Buf *buf_strappend(struct Buf *, const char *str); extern struct Buf *buf_strnappend(struct Buf *, const char *str, int nchars); extern struct Buf *buf_prints(struct Buf *buf, const char *fmt, const char* s); -extern struct Buf *buf_m4_define(struct Buf *buf, const char* def, const char* val); -extern struct Buf *buf_m4_undefine(struct Buf *buf, const char* def); -extern struct Buf *buf_print_strings(struct Buf * buf, FILE* out); -extern struct Buf *buf_linedir(struct Buf *buf, const char* filename, int lineno); extern struct Buf userdef_buf; /* a string buffer for #define's generated by user-options on cmd line. */ extern struct Buf yydmap_buf; /* a string buffer to hold yydmap elements */ extern struct Buf top_buf; /* contains %top code. String buffer. */ -extern bool no_section3_escape; /* True if the undocumented option --unsafe-no-m4-sect3-escape was passed */ /* For blocking out code from the header file. */ #define OUT_BEGIN_CODE() outn("m4_ifdef( [[M4_YY_IN_HEADER]],,[[m4_dnl") diff --git a/src/main.c b/src/main.c index 02a8d2ca..0d612dc2 100644 --- a/src/main.c +++ b/src/main.c @@ -257,10 +257,10 @@ int flex_main (int argc, char *argv[]) if (sectnum == 3) { OUT_BEGIN_CODE (); - if (!no_section3_escape) + if (!ctrl.no_section3_escape) fputs("[[", stdout); (void) flexscan (); /* copy remainder of input to output */ - if (!no_section3_escape) + if (!ctrl.no_section3_escape) fputs("]]", stdout); OUT_END_CODE (); } @@ -1172,7 +1172,7 @@ void flexinit (int argc, char **argv) env.trace_hex = true; break; case OPT_NO_SECT3_ESCAPE: - no_section3_escape = true; + ctrl.no_section3_escape = true; break; } /* switch */ } /* while scanopt() */ diff --git a/src/scan.l b/src/scan.l index 099d153a..82d57dfb 100644 --- a/src/scan.l +++ b/src/scan.l @@ -164,9 +164,13 @@ M4QEND "]""]" ^"%x"{NAME}? return XSCDECL; ^"%{".*{NL} START_CODEBLOCK(false); ^"%top"[[:blank:]]*"{"[[:blank:]]*{NL} { + char trampoline[512]; brace_start_line = linenum; ++linenum; - buf_linedir( &top_buf, infilename?infilename:"", linenum); + snprintf(trampoline, sizeof(trampoline), + "M4_HOOK_TRACE_LINE_FORMAT(%d, [[%s]])", + linenum, infilename?infilename:""); + buf_strappend(&top_buf, trampoline); brace_depth = 1; yy_push_state(CODEBLOCK_MATCH_BRACE); } @@ -619,7 +623,7 @@ M4QEND "]""]" ^"%%".* { sectnum = 3; - BEGIN(no_section3_escape ? SECT3_NOESCAPE : SECT3); + BEGIN(ctrl.no_section3_escape ? SECT3_NOESCAPE : SECT3); yyterminate(); /* to stop the parser */ }