Commit c45da1ca by Zack Weinberg Committed by Zack Weinberg

cppfiles.c (cpp_read_file): New function.

	* cppfiles.c (cpp_read_file): New function.

	* cpphash.c (collect_expansion): Make sure to reset last_token
	to NORM when we hit a string.  Handle trailing whitespace
	properly when the expansion is empty.
	(create_definition): Disable line commands while parsing the
	directive line.
	(dump_definition): If pfile->lineno == 0, output a line
	command ahead of the dump, and add a trailing newline.

	* cppinit.c (append_include_chain): Add fifth argument, which
	indicates whether or not system headers are C++ aware.
	(initialize_standard_includes): New function,
	broken out of read_and_prescan.  Pass 'cxx_aware' value from
	the include_defaults_array on to append_include_chain.
	(dump_special_to_buffer): Const-ify char array.
	(builtin_array): Don't dump __BASE_FILE__.
	(cpp_start_read): Use cpp_read_file.  Reorder code for
	clarity.  Don't output line commands here for -D/-A/-U
	switches.  Don't call deps_output for files included with
	-include or -imacros.

	* cpplib.c (do_define): Don't pay any attention to the second
	argument.
	(cpp_expand_to_buffer): Disable line commands while scanning.
	(output_line_command): Work in the file buffer.
	* cpplib.h: Remove no_record_file flag from struct cpp_reader.
	Fix formatting of comments.  Prototype cpp_read_file.

From-SVN: r32293
parent e97f22c9
2000-03-02 Zack Weinberg <zack@wolery.cumb.org>
* cppfiles.c (cpp_read_file): New function.
* cpphash.c (collect_expansion): Make sure to reset last_token
to NORM when we hit a string. Handle trailing whitespace
properly when the expansion is empty.
(create_definition): Disable line commands while parsing the
directive line.
(dump_definition): If pfile->lineno == 0, output a line
command ahead of the dump, and add a trailing newline.
* cppinit.c (append_include_chain): Add fifth argument, which
indicates whether or not system headers are C++ aware.
(initialize_standard_includes): New function,
broken out of read_and_prescan. Pass 'cxx_aware' value from
the include_defaults_array on to append_include_chain.
(dump_special_to_buffer): Const-ify char array.
(builtin_array): Don't dump __BASE_FILE__.
(cpp_start_read): Use cpp_read_file. Reorder code for
clarity. Don't output line commands here for -D/-A/-U
switches. Don't call deps_output for files included with
-include or -imacros.
* cpplib.c (do_define): Don't pay any attention to the second
argument.
(cpp_expand_to_buffer): Disable line commands while scanning.
(output_line_command): Work in the file buffer.
* cpplib.h: Remove no_record_file flag from struct cpp_reader.
Fix formatting of comments. Prototype cpp_read_file.
Thu Mar 2 13:29:46 2000 Richard Kenner <kenner@vlsi1.ultra.nyu.edu>
* c-common.c (c_common_nodes_and_builtins): Make sizetype_endlink
......
......@@ -609,6 +609,62 @@ remap_filename (pfile, name, loc)
return name;
}
/* Push an input buffer and load it up with the contents of FNAME.
If FNAME is "" or NULL, read standard input. */
int
cpp_read_file (pfile, fname)
cpp_reader *pfile;
const char *fname;
{
struct include_hash *ih_fake;
int f;
if (fname == NULL || *fname == 0)
{
fname = "";
f = 0;
}
/* Open the file in nonblocking mode, so we don't get stuck if
someone clever has asked cpp to process /dev/rmt0. finclude()
will check that we have a real file to work with. Also take
care not to acquire a controlling terminal by mistake (this can't
happen on sane systems, but paranoia is a virtue). */
else if ((f = open (fname, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666)) < 0)
{
cpp_notice_from_errno (pfile, fname);
return 0;
}
/* Push the buffer. */
if (!cpp_push_buffer (pfile, NULL, 0))
goto failed_push;
/* Gin up an include_hash structure for this file and feed it
to finclude. */
ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
ih_fake->next = 0;
ih_fake->next_this_file = 0;
ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
ih_fake->name = fname;
ih_fake->control_macro = 0;
ih_fake->buf = (char *)-1;
ih_fake->limit = 0;
if (!finclude (pfile, f, ih_fake))
goto failed_finclude;
return 1;
failed_finclude:
/* If finclude fails, it pops the buffer. */
free (ih_fake);
failed_push:
if (f)
close (f);
return 0;
}
/* Read the contents of FD into the buffer on the top of PFILE's stack.
IHASH points to the include hash entry for the file associated with
FD.
......
......@@ -424,6 +424,7 @@ collect_expansion (pfile, arglist)
continue;
maybe_trad_stringify:
last_token = NORM;
{
U_CHAR *base, *p, *limit;
struct reflist *tpat;
......@@ -487,21 +488,31 @@ collect_expansion (pfile, arglist)
else if (last_token == PASTE)
cpp_error (pfile, "`##' at end of macro definition");
/* Trim trailing white space from definition. */
here = CPP_WRITTEN (pfile);
while (here > last && is_hspace (pfile->token_buffer [here-1]))
here--;
CPP_SET_WRITTEN (pfile, here);
if (last_token == START)
{
/* Empty macro definition. */
exp = xstrdup ("\r \r ");
len = 1;
}
else
{
/* Trim trailing white space from definition. */
here = CPP_WRITTEN (pfile);
while (here > last && is_hspace (pfile->token_buffer [here-1]))
here--;
CPP_SET_WRITTEN (pfile, here);
CPP_NUL_TERMINATE (pfile);
len = CPP_WRITTEN (pfile) - start + 1;
exp = xmalloc (len + 4); /* space for no-concat markers at either end */
exp[0] = '\r';
exp[1] = ' ';
exp[len + 1] = '\r';
exp[len + 2] = ' ';
exp[len + 3] = '\0';
memcpy (&exp[2], pfile->token_buffer + start, len - 1);
CPP_NUL_TERMINATE (pfile);
len = CPP_WRITTEN (pfile) - start + 1;
exp = xmalloc (len + 4); /* space for no-concat markers at either end */
exp[0] = '\r';
exp[1] = ' ';
exp[len + 1] = '\r';
exp[len + 2] = ' ';
exp[len + 3] = '\0';
memcpy (&exp[2], pfile->token_buffer + start, len - 1);
}
CPP_SET_WRITTEN (pfile, start);
defn = (DEFINITION *) xmalloc (sizeof (DEFINITION));
......@@ -700,6 +711,7 @@ create_definition (pfile, funlike)
pfile->no_macro_expand++;
pfile->parsing_define_directive++;
CPP_OPTIONS (pfile)->discard_comments++;
CPP_OPTIONS (pfile)->no_line_commands++;
if (funlike)
{
......@@ -719,12 +731,14 @@ create_definition (pfile, funlike)
pfile->no_macro_expand--;
pfile->parsing_define_directive--;
CPP_OPTIONS (pfile)->discard_comments--;
CPP_OPTIONS (pfile)->no_line_commands--;
return defn;
err:
pfile->no_macro_expand--;
pfile->parsing_define_directive--;
CPP_OPTIONS (pfile)->discard_comments--;
CPP_OPTIONS (pfile)->no_line_commands--;
return 0;
}
......@@ -1560,6 +1574,9 @@ dump_definition (pfile, sym, len, defn)
long len;
DEFINITION *defn;
{
if (pfile->lineno == 0)
output_line_command (pfile, same_file);
CPP_RESERVE (pfile, len + sizeof "#define ");
CPP_PUTS_Q (pfile, "#define ", sizeof "#define " -1);
CPP_PUTS_Q (pfile, sym, len);
......@@ -1573,7 +1590,6 @@ dump_definition (pfile, sym, len, defn)
So we need length-4 chars of space, plus one for the NUL. */
CPP_RESERVE (pfile, defn->length - 4 + 1);
CPP_PUTS_Q (pfile, defn->expansion + 2, defn->length - 4);
CPP_NUL_TERMINATE_Q (pfile);
}
else
{
......@@ -1644,6 +1660,9 @@ dump_definition (pfile, sym, len, defn)
i = defn->length - (x - defn->expansion) - 2;
if (*x == '\r') x += 2, i -= 2;
if (i > 0) CPP_PUTS (pfile, x, i);
CPP_NUL_TERMINATE (pfile);
}
if (pfile->lineno == 0)
CPP_PUTC (pfile, '\n');
CPP_NUL_TERMINATE (pfile);
}
......@@ -654,7 +654,7 @@ get_macro_name (pfile)
static int
do_define (pfile, keyword)
cpp_reader *pfile;
const struct directive *keyword;
const struct directive *keyword ATTRIBUTE_UNUSED;
{
HASHNODE *hp;
DEFINITION *def;
......@@ -728,14 +728,11 @@ do_define (pfile, keyword)
else
cpp_install (pfile, sym, len, T_MACRO, (char *) def);
if (keyword != NULL && keyword->type == T_DEFINE)
{
if (CPP_OPTIONS (pfile)->debug_output
|| CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
dump_definition (pfile, sym, len, def);
else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
pass_thru_directive (sym, len, pfile, keyword);
}
if (CPP_OPTIONS (pfile)->debug_output
|| CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
dump_definition (pfile, sym, len, def);
else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
pass_thru_directive (sym, len, pfile, keyword);
return 0;
}
......@@ -876,7 +873,9 @@ cpp_expand_to_buffer (pfile, buf, length)
/* Scan the input, create the output. */
save_no_output = CPP_OPTIONS (pfile)->no_output;
CPP_OPTIONS (pfile)->no_output = 0;
CPP_OPTIONS (pfile)->no_line_commands++;
cpp_scan_buffer (pfile);
CPP_OPTIONS (pfile)->no_line_commands--;
CPP_OPTIONS (pfile)->no_output = save_no_output;
CPP_NUL_TERMINATE (pfile);
......@@ -926,16 +925,14 @@ output_line_command (pfile, file_change)
enum file_change_code file_change;
{
long line;
cpp_buffer *ip = CPP_BUFFER (pfile);
if (ip->fname == NULL)
return;
cpp_buffer *ip;
if (CPP_OPTIONS (pfile)->no_line_commands
|| CPP_OPTIONS (pfile)->no_output)
return;
cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
ip = cpp_file_buffer (pfile);
cpp_buf_line_and_col (ip, &line, NULL);
/* If the current file has not changed, we omit the #line if it would
appear to be a no-op, and we output a few newlines instead
......
......@@ -34,7 +34,8 @@ typedef struct cpp_reader cpp_reader;
typedef struct cpp_buffer cpp_buffer;
typedef struct cpp_options cpp_options;
enum cpp_token {
enum cpp_token
{
CPP_EOF = -1,
CPP_OTHER = 0,
CPP_COMMENT = 1,
......@@ -188,23 +189,23 @@ struct cpp_reader
struct if_stack *if_stack;
/* Nonzero means we have printed (while error reporting) a list of
containing files that matches the current status. */
containing files that matches the current status. */
char input_stack_listing_current;
/* If non-zero, macros are not expanded. */
/* If non-zero, macros are not expanded. */
char no_macro_expand;
/* If non-zero, directives cause a hard error. Used when parsing
macro arguments. */
char no_directives;
/* Print column number in error messages. */
/* Print column number in error messages. */
char show_column;
/* We're printed a warning recommending against using #import. */
/* We're printed a warning recommending against using #import. */
char import_warning;
/* If true, character between '<' and '>' are a single (string) token. */
/* If true, character between '<' and '>' are a single (string) token. */
char parsing_include_directive;
/* If true, # introduces an assertion (see do_assert) */
......@@ -214,18 +215,13 @@ struct cpp_reader
char parsing_define_directive;
/* True if escape sequences (as described for has_escapes in
parse_buffer) should be emitted. */
parse_buffer) should be emitted. */
char output_escapes;
/* 0: Have seen non-white-space on this line.
1: Only seen white space so far on this line.
2: Only seen white space so far in this file. */
char only_seen_white;
/* Nonzero means this file was included with a -imacros or -include
command line and should not be recorded as an include file. */
char no_record_file;
2: Only seen white space so far in this file. */
char only_seen_white;
long lineno;
......@@ -432,7 +428,6 @@ struct cpp_options {
char remap;
/* Nonzero means don't output line number information. */
char no_line_commands;
/* Nonzero means -I- has been seen,
......@@ -713,6 +708,7 @@ extern int find_include_file PARAMS ((cpp_reader *, const char *,
int *));
extern int finclude PARAMS ((cpp_reader *, int,
struct include_hash *));
extern int cpp_read_file PARAMS ((cpp_reader *, const char *));
extern void deps_output PARAMS ((cpp_reader *,
const char *, int));
extern struct include_hash *include_hash PARAMS ((cpp_reader *, const char *, int));
......
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