Commit bf9d5852 by Neil Booth Committed by Neil Booth

cpphash.h (struct cpp_buffer): Remove saved_line_base.

	* cpphash.h (struct cpp_buffer): Remove saved_line_base.
	* cpptrad.c: Update comments.
	(skip_whitespace, copy_comment): Take a new parameter.
	(skip_escaped_newlines): Don't duplicate escaped newline test.
	(copy_comment): Different location for CUR, decide here how
	to copy / replace the comment.
	(skip_whitespace): Copy whitespace.
	(_cpp_overlay_buffer, _cpp_remove_overlay): Don't play with line_base.
	(scan_out_logical_line): Let copy comment handle keeping or
	replacing comments.
	(scan_parameters, _cpp_create_trad_definition): Update.

From-SVN: r54605
parent 57292ec3
2002-06-14 Neil Booth <neil@daikokuya.demon.co.uk>
* cpphash.h (struct cpp_buffer): Remove saved_line_base.
* cpptrad.c: Update comments.
(skip_whitespace, copy_comment): Take a new parameter.
(skip_escaped_newlines): Don't duplicate escaped newline test.
(copy_comment): Different location for CUR, decide here how
to copy / replace the comment.
(skip_whitespace): Copy whitespace.
(_cpp_overlay_buffer, _cpp_remove_overlay): Don't play with line_base.
(scan_out_logical_line): Let copy comment handle keeping or
replacing comments.
(scan_parameters, _cpp_create_trad_definition): Update.
2002-06-13 Alan Lehotsky <apl@alum.mit.edu> 2002-06-13 Alan Lehotsky <apl@alum.mit.edu>
* reload.c (get_secondary_mem,find_reloads_address, * reload.c (get_secondary_mem,find_reloads_address,
......
...@@ -298,7 +298,7 @@ struct cpp_buffer ...@@ -298,7 +298,7 @@ struct cpp_buffer
struct search_path dir; struct search_path dir;
/* Used for buffer overlays by cpptrad.c. */ /* Used for buffer overlays by cpptrad.c. */
const uchar *saved_cur, *saved_rlimit, *saved_line_base; const uchar *saved_cur, *saved_rlimit;
}; };
/* A cpp_reader encapsulates the "state" of a pre-processor run. /* A cpp_reader encapsulates the "state" of a pre-processor run.
......
...@@ -63,16 +63,16 @@ struct fun_macro ...@@ -63,16 +63,16 @@ struct fun_macro
unsigned int argc; unsigned int argc;
}; };
/* Lexing TODO: Maybe handle -CC and space in escaped newlines. Stop /* Lexing TODO: Maybe handle space in escaped newlines. Stop cpplex.c
cpplex.c from recognizing comments and directives during its lexing from recognizing comments and directives during its lexing pass. */
pass. Get rid of line_base usage - seems pointless? */
static const uchar *handle_newline PARAMS ((cpp_reader *, const uchar *)); static const uchar *handle_newline PARAMS ((cpp_reader *, const uchar *));
static const uchar *skip_escaped_newlines PARAMS ((cpp_reader *, static const uchar *skip_escaped_newlines PARAMS ((cpp_reader *,
const uchar *)); const uchar *));
static const uchar *skip_whitespace PARAMS ((cpp_reader *, const uchar *)); static const uchar *skip_whitespace PARAMS ((cpp_reader *, const uchar *,
int));
static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *)); static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *)); static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *, int));
static void scan_out_logical_line PARAMS ((cpp_reader *pfile, cpp_macro *)); static void scan_out_logical_line PARAMS ((cpp_reader *pfile, cpp_macro *));
static void check_output_buffer PARAMS ((cpp_reader *, size_t)); static void check_output_buffer PARAMS ((cpp_reader *, size_t));
static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *)); static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *));
...@@ -95,7 +95,7 @@ check_output_buffer (pfile, n) ...@@ -95,7 +95,7 @@ check_output_buffer (pfile, n)
size_t n; size_t n;
{ {
/* We might need two bytes to terminate an unterminated comment, and /* We might need two bytes to terminate an unterminated comment, and
one more to terminate with a NUL. */ one more to terminate the line with a NUL. */
n += 2 + 1; n += 2 + 1;
if (n > (size_t) (pfile->out.limit - pfile->out.cur)) if (n > (size_t) (pfile->out.limit - pfile->out.cur))
...@@ -111,8 +111,10 @@ check_output_buffer (pfile, n) ...@@ -111,8 +111,10 @@ check_output_buffer (pfile, n)
} }
/* To be called whenever a newline character is encountered in the /* To be called whenever a newline character is encountered in the
input file, at CUR. Handles DOS, MAC and Unix ends of line, and input file, at CUR. Handles DOS, Mac and Unix ends of line, and
returns the character after the newline sequence. */ increments pfile->line.
Returns a pointer the character after the newline sequence. */
static const uchar * static const uchar *
handle_newline (pfile, cur) handle_newline (pfile, cur)
cpp_reader *pfile; cpp_reader *pfile;
...@@ -121,50 +123,53 @@ handle_newline (pfile, cur) ...@@ -121,50 +123,53 @@ handle_newline (pfile, cur)
pfile->line++; pfile->line++;
if (cur[0] + cur[1] == '\r' + '\n') if (cur[0] + cur[1] == '\r' + '\n')
cur++; cur++;
pfile->buffer->line_base = cur + 1;
return cur + 1; return cur + 1;
} }
/* CUR points to any character in the buffer, not necessarily a /* CUR points to any character in the buffer, not necessarily a
backslash. Advances CUR until all escaped newlines are skipped, backslash. Advances CUR until all escaped newlines are skipped,
and returns the new position. */ and returns the new position.
Warns if a file buffer ends in an escaped newline. */
static const uchar * static const uchar *
skip_escaped_newlines (pfile, cur) skip_escaped_newlines (pfile, cur)
cpp_reader *pfile; cpp_reader *pfile;
const uchar *cur; const uchar *cur;
{ {
if (*cur == '\\' && is_vspace (cur[1])) const uchar *orig_cur = cur;
{
do
cur = handle_newline (pfile, cur + 1);
while (*cur == '\\' && is_vspace (cur[1]));
if (cur == RLIMIT (pfile->context)) while (*cur == '\\' && is_vspace (cur[1]))
cpp_error (pfile, DL_PEDWARN, cur = handle_newline (pfile, cur + 1);
"backslash-newline at end of file");
} if (cur != orig_cur && cur == RLIMIT (pfile->context) && pfile->buffer->inc)
cpp_error (pfile, DL_PEDWARN, "backslash-newline at end of file");
return cur; return cur;
} }
/* CUR points to the character after the asterisk introducing a /* CUR points to the asterisk introducing a comment in the input
comment in the input buffer. The remaining comment is copied to buffer. IN_DEFINE is true if we are in the replacement text
the buffer pointed to by pfile->out.cur, which must be of of a macro.
sufficient size, and pfile->out.cur is updated. Unterminated
comments are diagnosed, and correctly terminated in the output. The asterisk and following comment is copied to the buffer pointed
to by pfile->out.cur, which must be of sufficient size.
Unterminated comments are diagnosed, and correctly terminated in
the output. pfile->out.cur is updated depending upon IN_DEFINE,
-C, -CC and pfile->state.in_directive.
Returns a pointer to the first character after the comment in the Returns a pointer to the first character after the comment in the
input buffer. */ input buffer. */
static const uchar * static const uchar *
copy_comment (pfile, cur) copy_comment (pfile, cur, in_define)
cpp_reader *pfile; cpp_reader *pfile;
const uchar *cur; const uchar *cur;
int in_define;
{ {
unsigned int from_line = pfile->line; unsigned int from_line = pfile->line;
const uchar *limit = RLIMIT (pfile->context); const uchar *limit = RLIMIT (pfile->context);
uchar *out = pfile->out.cur; uchar *out = pfile->out.cur;
while (cur < limit) do
{ {
unsigned int c = *cur++; unsigned int c = *cur++;
*out++ = c; *out++ = c;
...@@ -172,7 +177,7 @@ copy_comment (pfile, cur) ...@@ -172,7 +177,7 @@ copy_comment (pfile, cur)
if (c == '/') if (c == '/')
{ {
/* An immediate slash does not terminate the comment. */ /* An immediate slash does not terminate the comment. */
if (out[-2] == '*' && out > pfile->out.cur + 1) if (out[-2] == '*' && out - 2 > pfile->out.cur)
goto done; goto done;
if (*cur == '*' && cur[1] != '/' if (*cur == '*' && cur[1] != '/'
...@@ -190,55 +195,91 @@ copy_comment (pfile, cur) ...@@ -190,55 +195,91 @@ copy_comment (pfile, cur)
out[-1] = '\n'; out[-1] = '\n';
} }
} }
while (cur < limit);
cpp_error_with_line (pfile, DL_ERROR, from_line, 0, "unterminated comment"); cpp_error_with_line (pfile, DL_ERROR, from_line, 0, "unterminated comment");
*out++ = '*'; *out++ = '*';
*out++ = '/'; *out++ = '/';
done: done:
pfile->out.cur = out; /* Comments in directives become spaces so that tokens are properly
separated when the ISO preprocessor re-lexes the line. The
exception is #define. */
if (pfile->state.in_directive)
{
if (in_define)
{
if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
pfile->out.cur--;
else
pfile->out.cur = out;
}
else
pfile->out.cur[-1] = ' ';
}
else if (CPP_OPTION (pfile, discard_comments))
pfile->out.cur--;
else
pfile->out.cur = out;
return cur; return cur;
} }
/* Skip any horizontal whitespace and comments beginning at CUR, /* CUR points to any character in the input buffer. Skips over all
returning the following character. */ contiguous horizontal white space and NULs, including comments if
SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
character or the end of the current context. Escaped newlines are
removed.
The whitespace is copied verbatim to the output buffer, except that
comments are handled as described in copy_comment().
pfile->out.cur is updated.
Returns a pointer to the first character after the whitespace in
the input buffer. */
static const uchar * static const uchar *
skip_whitespace (pfile, cur) skip_whitespace (pfile, cur, skip_comments)
cpp_reader *pfile; cpp_reader *pfile;
const uchar *cur; const uchar *cur;
int skip_comments;
{ {
const uchar *tmp; uchar *out = pfile->out.cur;
for (;;) for (;;)
{ {
while (is_nvspace (*cur) && *cur != 0) unsigned int c = *cur++;
cur++; *out++ = c;
if (*cur == '\0' && cur != RLIMIT (pfile->context)) if (is_nvspace (c) && c)
continue; continue;
if (*cur == '\\') if (!c && cur != RLIMIT (pfile->context))
{ continue;
tmp = cur;
cur = skip_escaped_newlines (pfile, cur);
if (tmp != cur)
continue;
}
if (*cur == '/') if (*cur == '/' && skip_comments)
{ {
tmp = skip_escaped_newlines (pfile, cur + 1); const uchar *tmp = skip_escaped_newlines (pfile, cur);
if (*tmp == '*') if (*tmp == '*')
{ {
cur = copy_comment (pfile, tmp + 1); pfile->out.cur = out;
cur = copy_comment (pfile, tmp, false /* in_define */);
out = pfile->out.cur;
continue; continue;
} }
} }
out--;
if (c == '\\' && is_vspace (*cur))
{
cur = skip_escaped_newlines (pfile, cur);
continue;
}
break; break;
} }
return cur; pfile->out.cur = out;
return cur - 1;
} }
/* Lexes and outputs an identifier starting at CUR, which is assumed /* Lexes and outputs an identifier starting at CUR, which is assumed
...@@ -283,10 +324,8 @@ _cpp_overlay_buffer (pfile, start, len) ...@@ -283,10 +324,8 @@ _cpp_overlay_buffer (pfile, start, len)
buffer->saved_cur = buffer->cur; buffer->saved_cur = buffer->cur;
buffer->saved_rlimit = buffer->rlimit; buffer->saved_rlimit = buffer->rlimit;
buffer->saved_line_base = buffer->line_base;
buffer->cur = start; buffer->cur = start;
buffer->line_base = start;
buffer->rlimit = start + len; buffer->rlimit = start + len;
pfile->saved_line = pfile->line; pfile->saved_line = pfile->line;
...@@ -301,7 +340,6 @@ _cpp_remove_overlay (pfile) ...@@ -301,7 +340,6 @@ _cpp_remove_overlay (pfile)
buffer->cur = buffer->saved_cur; buffer->cur = buffer->saved_cur;
buffer->rlimit = buffer->saved_rlimit; buffer->rlimit = buffer->saved_rlimit;
buffer->line_base = buffer->saved_line_base;
pfile->line = pfile->saved_line; pfile->line = pfile->saved_line;
} }
...@@ -474,20 +512,9 @@ scan_out_logical_line (pfile, macro) ...@@ -474,20 +512,9 @@ scan_out_logical_line (pfile, macro)
cur = skip_escaped_newlines (pfile, cur); cur = skip_escaped_newlines (pfile, cur);
if (*cur == '*') if (*cur == '*')
{ {
*out = '*'; pfile->out.cur = out;
pfile->out.cur = out + 1; cur = copy_comment (pfile, cur, macro != 0);
cur = copy_comment (pfile, cur + 1); out = pfile->out.cur;
/* Comments in directives become spaces so that
tokens are properly separated when the ISO
preprocessor re-lexes the line. The exception
is #define. */
if (pfile->state.in_directive && !macro)
out[-1] = ' ';
else if (CPP_OPTION (pfile, discard_comments))
out -= 1;
else
out = pfile->out.cur;
} }
} }
break; break;
...@@ -763,14 +790,15 @@ scan_parameters (pfile, macro) ...@@ -763,14 +790,15 @@ scan_parameters (pfile, macro)
for (;;) for (;;)
{ {
cur = skip_whitespace (pfile, cur); cur = skip_whitespace (pfile, cur, true /* skip_comments */);
if (is_idstart (*cur)) if (is_idstart (*cur))
{ {
ok = false; ok = false;
if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur))) if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
break; break;
cur = skip_whitespace (pfile, CUR (pfile->context)); cur = skip_whitespace (pfile, CUR (pfile->context),
true /* skip_comments */);
if (*cur == ',') if (*cur == ',')
{ {
cur++; cur++;
...@@ -871,7 +899,9 @@ _cpp_create_trad_definition (pfile, macro) ...@@ -871,7 +899,9 @@ _cpp_create_trad_definition (pfile, macro)
} }
/* Skip leading whitespace in the replacement text. */ /* Skip leading whitespace in the replacement text. */
CUR (pfile->context) = skip_whitespace (pfile, CUR (pfile->context)); CUR (pfile->context)
= skip_whitespace (pfile, CUR (pfile->context),
CPP_OPTION (pfile, discard_comments_in_macro_exp));
pfile->state.prevent_expansion++; pfile->state.prevent_expansion++;
scan_out_logical_line (pfile, macro); scan_out_logical_line (pfile, macro);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment