From: Robert Pengelly Date: Sun, 23 Mar 2025 21:53:05 +0000 (+0000) Subject: Add source filename and line number to errors X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=69ad2cc99beb7309936dfb36b2fe9c63f2e882d0;p=xmake.git Add source filename and line number to errors --- diff --git a/read.c b/read.c index 50a254e..46d3778 100644 --- a/read.c +++ b/read.c @@ -21,6 +21,9 @@ struct linebuf { char *start, *memory; unsigned long size; + const char *filename; + unsigned long line_no; + FILE *f; }; @@ -65,8 +68,8 @@ static long read_line (struct linebuf *lbuf) { long lines_read = 0; - char *p = lbuf->start; char *end = lbuf->start + lbuf->size; + char *p = lbuf->start; while (lbuf->f ? fgets (p, end - p, lbuf->f) : memory_fgets (p, end - p, &(lbuf->memory))) { @@ -180,7 +183,7 @@ static void add_if_stack (void) { } -static int include_makefile (const char *filename, int bsd) { +static int include_makefile (const char *src_filename, unsigned long line_no, const char *filename, int bsd) { struct if_stack *saved_if_stack = cur_if_stack; char *path, *new_name; @@ -194,7 +197,7 @@ static int include_makefile (const char *filename, int bsd) { if (ch != '<' && ch != '"') { - fprintf (stderr, "%s: *** .include filename must be delimited by '\"' or '<'. Stop.\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** .include filename must be delimited by '\"' or '<'. Stop.\n", program_name, src_filename, line_no); exit (EXIT_FAILURE); } @@ -203,7 +206,7 @@ static int include_makefile (const char *filename, int bsd) { if (!(end = strrchr (filename, '>'))) { - fprintf (stderr, "%s: *** unclosed .include filename. '>' expected. Stop.\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** unclosed .include filename. '>' expected. Stop.\n", program_name, src_filename, line_no); exit (EXIT_FAILURE); } @@ -212,7 +215,7 @@ static int include_makefile (const char *filename, int bsd) { if (!(end = strrchr (filename, '"'))) { - fprintf (stderr, "%s: *** unclosed .include filename. '\"' expected. Stop.\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** unclosed .include filename. '\"' expected. Stop.\n", program_name, src_filename, line_no); exit (EXIT_FAILURE); } @@ -250,7 +253,7 @@ static int include_makefile (const char *filename, int bsd) { } - fprintf (stderr, "%s: *** failed to include '%s'. Stop.\n", program_name, filename); + fprintf (stderr, "%s: %s: %lu: *** failed to include '%s'. Stop.\n", program_name, src_filename, line_no, filename); exit (EXIT_FAILURE); } @@ -276,6 +279,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { char *colon, *semicolonp, *commentp; long lines_read; + unsigned long line_no = 1; cmds = xmalloc (cmds_sz); #define record_waiting_files() \ @@ -295,6 +299,9 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { char *p, *src, *dst, *comma, *arg1 = 0, *arg2 = 0; char ch; + line_no = lbuf->line_no; + lbuf->line_no += lines_read; + if (*line == '\0') { continue; } @@ -313,11 +320,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { remove_backslash_newlines (clean); remove_comment (clean); - p = clean; - - while (isspace ((int) *p)) { - p++; - } + p = skip_whitespace (clean); if (*p == '\0') { continue; @@ -341,7 +344,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { if (!cur_if_stack) { - fprintf (stderr, "%s: *** extraneous 'else'. Stop.\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** extraneous 'else'. Stop.\n", program_name, lbuf->filename, line_no); exit (EXIT_FAILURE); } @@ -351,12 +354,12 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } else { if (*p) { - fprintf (stderr, "%s: extraneous text after 'else' directive\n", program_name); + fprintf (stderr, "%s: %s: %lu: extraneous text after 'else' directive\n", program_name, lbuf->filename, line_no); } if (cur_if_stack->has_else) { - fprintf (stderr, "%s: *** only one 'else' per conditional. Stop.\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** only one 'else' per conditional. Stop.\n", program_name, lbuf->filename, line_no); exit (EXIT_FAILURE); } @@ -394,7 +397,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { if (!isspace ((int) *p)) { - fprintf (stderr, "%s: *** missing separator (%s must be followed by whitespace). Stop.\n", program_name, ifneq ? "ifneq" : "ifeq"); + fprintf (stderr, "%s: %s: %lu: *** missing separator (%s must be followed by whitespace). Stop.\n", program_name, lbuf->filename, line_no, ifneq ? "ifneq" : "ifeq"); exit (EXIT_FAILURE); } @@ -424,7 +427,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } else { - fprintf (stderr, "%s: currently only ifeq (arg,arg) is supported\n", program_name); + fprintf (stderr, "%s: %s: %lu: currently only ifeq (arg,arg) is supported\n", program_name, lbuf->filename, line_no); exit (EXIT_FAILURE); } @@ -437,7 +440,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { if (!isspace ((int) *p)) { - fprintf (stderr, "%s: *** missing separator (%s must be followed by whitespace). Stop.\n", program_name, ifneq ? "ifneq" : "ifeq"); + fprintf (stderr, "%s: %s: %lu: *** missing separator (%s must be followed by whitespace). Stop.\n", program_name, lbuf->filename, line_no, ifneq ? "ifneq" : "ifeq"); exit (EXIT_FAILURE); } @@ -488,7 +491,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { p = skip_whitespace (p); if (*p || !arg1 || !arg2) { - fprintf (stderr, "%s: extraneous text after '%s' directive\n", program_name, ifneq ? "ifneq" : "ifeq"); + fprintf (stderr, "%s: %s: %lu: extraneous text after '%s' directive\n", program_name, lbuf->filename, line_no, ifneq ? "ifneq" : "ifeq"); } p = variable_expand_line (xstrdup (arg1)); @@ -516,7 +519,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { ifeq_invalid_syntax: - fprintf (stderr, "%s: *** invalid syntax in conditional. Stop.", program_name); + fprintf (stderr, "%s: %s: %lu: *** invalid syntax in conditional. Stop.", program_name, lbuf->filename, line_no); exit (EXIT_FAILURE); } @@ -544,7 +547,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { if (!isspace ((int) *p)) { - fprintf (stderr, "%s: %s must be followed by whitespace. Stop.", program_name, ifndef ? "ifndef" : "ifdef"); + fprintf (stderr, "%s: %s: %lu: %s must be followed by whitespace. Stop.", program_name, lbuf->filename, line_no, ifndef ? "ifndef" : "ifdef"); exit (EXIT_FAILURE); } @@ -595,7 +598,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { if (!cur_if_stack) { - fprintf (stderr, "%s: *** extraneous 'endif'. Stop.\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** extraneous 'endif'. Stop.\n", program_name, lbuf->filename, line_no); exit (EXIT_FAILURE); } else { @@ -608,7 +611,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } if (*p) { - fprintf (stderr, "%s: *** extraneous text after 'endif' directive\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** extraneous text after 'endif' directive\n", program_name, lbuf->filename, line_no); } continue; @@ -704,7 +707,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { saved_ch = *q; *q = '\0'; - if ((ret = include_makefile (p, bsd))) { + if ((ret = include_makefile (lbuf->filename, lbuf->line_no, p, bsd))) { return ret; } @@ -747,12 +750,8 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { if (!(colon = strchr (line, ':'))) { - for (p = line; isspace ((int) *p); p++) { - ; - } - - if (*p) { - fprintf (stderr, "%s: missing ':' in rule line!\n", program_name); + if (*(p = skip_whitespace (line))) { + fprintf (stderr, "%s: %s: %lu: missing ':' in rule line!\n", program_name, lbuf->filename, line_no); } free (line); @@ -804,6 +803,10 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { record_waiting_files (); + if (*(skip_whitespace (lbuf->start))) { + line_no++; + } + free (clean); free (cmds); @@ -818,7 +821,7 @@ static int read_lbuf (struct linebuf *lbuf, int set_default) { } - fprintf (stderr, "%s: *** missing 'endif'. Stop.\n", program_name); + fprintf (stderr, "%s: %s: %lu: *** missing 'endif'. Stop.\n", program_name, lbuf->filename, line_no); exit (EXIT_FAILURE); } @@ -840,6 +843,10 @@ int read_makefile (const char *filename) { } lbuf.size = 256; + + lbuf.filename = filename; + lbuf.line_no = 1; + lbuf.start = xmalloc (lbuf.size); if ((makefile_list = variable_find ("MAKEFILE_LIST"))) {