8sa1-gcc/gcc/cppmain.c
Neil Booth 93c80368d9 New macro expander.
2000-10-28  Neil Booth  <neilb@earthling.net>

	New macro expander.

	* cpplib.c (struct answer): New.
	(struct if_stack): Use cpp_lexer_pos rather than line and col.
	Rename cmacro mi_cmacro.
	(struct directive, KANDR, STDC89, EXTENSION, COND, IF_COND, INCL,
	IN_I): New directive and flags.
	(skip_rest_of_line, check_eol, run_directive, glue_header_name,
	parse_answer, parse_assertion, find_answer): New functions.
	(parse_ifdef, detect_if_not_defined, validate_else): Remove.
	(lex_macro_node): New function to replace parse_ifdef and
	get_define_node.

	(_cpp_handle_directive): New function, combines _cpp_check_directive
	and _cpp_check_linemarker.

	(do_define, do_undef, parse_include, do_include, do_import,
	do_include_next, read_line_number, do_line, do_ident, do_pragma,
	do_pragma_once, do_pragma_poison, do_pragma_dependency):
	Update for new token getting interface.

	(do_ifdef, do_ifndef, do_if, do_else, do_endif, push_conditional)
	: Update for new multiple-include optimisation technique.
	(do_elif): Don't forget to invalidate controlling macros.

	(unwind_if_stack, cpp_defined, cpp_push_buffer, cpp_pop_buffer): Update.
	(parse_assertion, parse_answer, find_answer, _cpp_test_assertion):
	Functions to handle assertions with the new token interface.
	(do_assert, do_unassert): Use them.

	(cpp_define, _cpp_define_builtin, cpp_undef, cpp_assert, cpp_unassert):
	Use run_directive.

	(_cpp_init_stacks): Register directive names.  Don't register special
	nodes.

	* cpperror.c (print_containing_files, _cpp_begin_message): Update to
	new position recording regime.
	(cpp_ice, cpp_fatal, cpp_error, cpp_error_with_line, cpp_warning,
	cpp_warning_with_line, cpp_pedwarn, cpp_pedwarn_with_line,
	cpp_pedwarn_with_file_and_line): Update for _cpp_begin_message changes.
	(cpp_type2name): Move to cpplex.c.

	* cppexp.c (parse_charconst): spec_nodes is no longer a pointer.
	(parse_defined): Update to handle new multiple include optimisation
	method.  Remove poisoned identifier warning.
	(parse_assertion, TYPE_NAME): Delete.
	(lex): Update for multiple include optimisation, removal of
	CPP_DEFINED, to use _cpp_test_assertion for assertions and
	cpp_token_as_text.
	(_cpp_parse_expr): Update for MI optimisation, and to use op_as_text.
	(op_as_text): New function, to wrap cpp_token_as_text.

	* cppfiles.c (stack_include_file, _cpp_pop_file_buffer):
	Update for MI optimisation.
	(_cpp_execute_include): Take a token rather than 3 arguments.  Fix
	segfault on diagnostic.
	(_cpp_compare_file_date): Take a token rather than 3 args.
	(cpp_read_file): Work correctly for zero-length files.

	* cpphash.c (_cpp_init_macros, _cpp_cleanup_macros): Rename
	_cpp_init_hashtable and _cpp_cleanup_hashtable.
	(cpp_lookup): Place identifiers at front of identifier pool
	for _cpp_lookup_with_hash.
	(_cpp_lookup_with_hash): Require identifiers to be at the front of
	the identifier pool.  Commit the memory if not already in the
	hash table.

	* cppinit.c (cpp_reader_init): Move cpp_init_completed test to top.
	Initialise various members of cpp_reader, memory pools, and the
	special nodes.
	(cpp_printer_init): Delete.
	(cpp_cleanup): Update.
	(struct builtin, builtin_array, initialize_builtins): Update for new
	hashnode definition and builtin handling.
	(cpp_start_read, cpp_finish): Don't take or initialise a
	printer.  Update.

	* cpplib.h (cpp_printer, cpp_toklist, CPP_DEFINED, BOL,
	PASTED, VAR_ARGS, BEG_OF_FILE, IN_DIRECTIVE, KNOWN_DIRECTIVE,
	T_VOID, T_SPECLINE, T_DATE, T_FILE, T_BASE_FILE, T_INCLUDE_LEVEL,
	T_TIME, T_STDC, T_OPERATOR, T_POISON, T_MACRO, T_ASSERTION): Delete.
	(struct cpp_pool, struct cpp_macro, struct cpp_lexer_pos,
	struct cpp_lookahead, CPP_DHASH, enum mi_state, enum mi_ind,
	NO_EXPAND, VARARGS_FIRST, struct cpp_token_with_pos,
	struct toklist, struct cpp_context, struct specnodes,
	TOKEN_LOOKAHEAD, TOKEN_BUFFSIZE, NODE_OPERATOR, NODE_POISONED,
	NODE_BUILTIN, NODE_DIAGNOSTIC, NT_VOID, NT_MACRO, NT_ASSERTION,
	enum builtin_type, cpp_can_paste): New.
	(struct cpp_token): Delete line and col members.
	(struct cpp_buffer): New member output_lineno.
	(struct lexer_state): Delete indented, in_lex_line, seen_dot.
	Add va_args_ok, poisoned_ok, prevent_expansion, parsing_args.
	(struct cpp_reader): New members lexer_pos, macro_pos, directive_pos,
	ident_pool, temp_string_pool, macro_pool, argument_pool, string_pool,
	base_context, context, directive, mi_state, mi_if_not_defined,
	mi_lexed, mi_cmacro, mi_ind_cmacro, la_read, la_write, la_unused,
	mlstring_pos, macro_buffer, macro_buffer_len.
	Delete members mls_line, mls_column, token_list, potential_control_macro,
	temp_tokens, temp_cap, temp_alloced, temp_used, first_directive_token,
	context_cap, cur_context, no_expand_level, paste_level, contexts, args,
	save_parameter_spellings, need_newline, .
	Change type of date, time and spec_nodes members.
	Change prototypes for include and ident callbacks.
	(struct cpp_hashnode): Change type of name.  Remove union members
	expansion and code.  Add members macro, operator and builtin.

	(cpp_token_len, cpp_token_as_text, cpp_spell_token, cpp_start_read,
	cpp_finish, cpp_avoid_paste, cpp_get_token, cpp_get_line,
	cpp_get_output_line, cpp_macro_definition, cpp_start_lookahead,
	cpp_stop_lookahead): New prototypes.
	(cpp_printer_init, cpp_dump_definition): Delete prototypes.

	(U_CHAR, U, ustrcmp, ustrncmp, ustrlen, uxstrdup, ustrchr, ufputs):
	Move from cpphash.h.

	* cpphash.h (U_CHAR, U, ustrcmp, ustrncmp, ustrlen, uxstrdup, ustrchr,
	ufputs): Move to cpplib.h.
	(enum spell_type, struct token_spelling, _cpp_token_spellings, TOKEN_SPELL,
	TOKEN_NAME, struct answer, FREE_ANSWER, KANDR, STDC89, EXTENSION,
	COND, EXPAND, INCL, COMMENTS, IN_I, struct directive, directive_handler,
	struct spec_nodes, _cpp_digraph_spellings, _cpp_free_temp_tokens,
	_cpp_init_input_buffer, _cpp_grow_token_buffer, _cpp_init_toklist,
	_cpp_clear_toklist, _cpp_expand_token_space, _cpp_expand_name_space,
	_cpp_equiv_tokens, _cpp_equiv_toklists, _cpp_process_directive,
	_cpp_run_directive, _cpp_get_line, _cpp_get_raw_token, _cpp_glue_header_name,
	_cpp_can_paste, _cpp_check_directive, _cpp_check_linemarker,
	_cpp_parse_assertion, _cpp_find_answer): Delete.
	(VALID_SIGN, ALIGN, POOL_FRONT, POOL_LIMIT, POOL_BASE, POOL_SIZE,
	POOL_USED, POOL_COMMIT, struct cpp_chunk, _cpp_lex_token, _cpp_init_pool,
	_cpp_free_pool, _cpp_pool_reserve, _cpp_pool_alloc, _cpp_next_chunk,
	_cpp_lock_pool, _cpp_unlock_pool, _cpp_test_assertion,
	_cpp_handle_directive, DSC): New.
	(struct include_file): New member defined.

	(DO_NOT_REREAD, _cpp_begin_message, _cpp_execute_include,
	_cpp_compare_file_date): Update.
	(_cpp_pop_context, _cpp_get_token, _cpp_free_lookaheads, _cpp_push_token): New.
	(_cpp_init_macros, _cpp_cleanup_macros): Rename to _cpp_init_hashtable,
	_cpp_cleanup_hashtable.

	* Makefile.in: Remove cppoutput.c.

	* cppoutput.c: Delete

	* fixheader.c (read_scan_file): Update for new cpp_get_token
	prototype.
	(recognized_function): New argument LINE.

	* scan-decls.c (skip_to_closing_brace, scan_decls): Update for
	new cpp_get_token prototype.

	* scan.h (recognized_function): Update prototype.

	* po/POTFILES.in: Remove cppoutput.c.

From-SVN: r37098
2000-10-28 17:59:06 +00:00

446 lines
12 KiB
C

/* CPP main program, using CPP Library.
Copyright (C) 1995, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
Written by Per Bothner, 1994-95.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
#include "config.h"
#include "system.h"
#include "cpplib.h"
#include "intl.h"
/* Encapsulates state used to convert the stream of tokens coming from
cpp_get_token back into a text file. */
struct printer
{
FILE *outf; /* stream to write to. */
const char *last_fname; /* previous file name. */
const char *syshdr_flags; /* system header flags, if any. */
unsigned int lineno; /* line currently being written. */
unsigned char printed; /* nonzero if something output at lineno. */
unsigned char no_line_dirs; /* nonzero to output no line directives. */
};
int main PARAMS ((int, char **));
/* General output routines. */
static void scan_buffer PARAMS ((cpp_reader *));
static int printer_init PARAMS ((cpp_reader *));
static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *));
static void print_line PARAMS ((const char *));
static void maybe_print_line PARAMS ((unsigned int));
static void move_printer PARAMS ((cpp_reader *, unsigned int, const char *));
/* Callback routines for the parser. Most of these are active only
in specific modes. */
static void cb_define PARAMS ((cpp_reader *, cpp_hashnode *));
static void cb_undef PARAMS ((cpp_reader *, cpp_hashnode *));
static void cb_include PARAMS ((cpp_reader *, const unsigned char *,
const cpp_token *));
static void cb_ident PARAMS ((cpp_reader *, const cpp_string *));
static void cb_enter_file PARAMS ((cpp_reader *));
static void cb_leave_file PARAMS ((cpp_reader *));
static void cb_rename_file PARAMS ((cpp_reader *));
static void cb_def_pragma PARAMS ((cpp_reader *));
static void do_pragma_implementation PARAMS ((cpp_reader *));
const char *progname;
cpp_reader parse_in;
struct printer print;
int
main (argc, argv)
int argc;
char **argv;
{
char *p;
cpp_reader *pfile = &parse_in;
int argi = 1; /* Next argument to handle. */
p = argv[0] + strlen (argv[0]);
while (p != argv[0] && ! IS_DIR_SEPARATOR (p[-1])) --p;
progname = p;
xmalloc_set_program_name (progname);
#ifdef HAVE_LC_MESSAGES
setlocale (LC_MESSAGES, "");
#endif
(void) bindtextdomain (PACKAGE, localedir);
(void) textdomain (PACKAGE);
cpp_init ();
cpp_reader_init (pfile);
argi += cpp_handle_options (pfile, argc - argi , argv + argi);
if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
cpp_fatal (pfile, "Invalid option %s", argv[argi]);
if (CPP_FATAL_ERRORS (pfile))
return (FATAL_EXIT_CODE);
/* Open the output now. We must do so even if no_output is on,
because there may be other output than from the actual
preprocessing (e.g. from -dM). */
if (printer_init (pfile))
return (FATAL_EXIT_CODE);
/* Set callbacks. */
if (! CPP_OPTION (pfile, no_output))
{
pfile->cb.ident = cb_ident;
pfile->cb.def_pragma = cb_def_pragma;
if (! CPP_OPTION (pfile, no_line_commands))
{
pfile->cb.enter_file = cb_enter_file;
pfile->cb.leave_file = cb_leave_file;
pfile->cb.rename_file = cb_rename_file;
}
}
if (CPP_OPTION (pfile, dump_includes))
pfile->cb.include = cb_include;
if (CPP_OPTION (pfile, debug_output)
|| CPP_OPTION (pfile, dump_macros) == dump_names
|| CPP_OPTION (pfile, dump_macros) == dump_definitions)
{
pfile->cb.define = cb_define;
pfile->cb.undef = cb_undef;
pfile->cb.poison = cb_def_pragma;
}
/* Register one #pragma which needs special handling. */
cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation);
cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation);
if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname)))
return (FATAL_EXIT_CODE);
if (CPP_BUFFER (pfile))
{
if (CPP_OPTION (pfile, no_output))
cpp_scan_buffer_nooutput (pfile);
else
scan_buffer (pfile);
}
/* -dM command line option. */
if (CPP_OPTION (pfile, dump_macros) == dump_only)
cpp_forall_identifiers (pfile, dump_macro);
cpp_finish (pfile);
cpp_cleanup (pfile);
/* Flush any pending output. */
if (print.printed)
putc ('\n', print.outf);
if (ferror (print.outf) || fclose (print.outf))
cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
if (parse_in.errors)
return (FATAL_EXIT_CODE);
return (SUCCESS_EXIT_CODE);
}
/* Writes out the preprocessed file. Alternates between two tokens,
so that we can avoid accidental token pasting. */
static void
scan_buffer (pfile)
cpp_reader *pfile;
{
unsigned int index, line;
cpp_token tokens[2], *token;
do
{
for (index = 0;; index = 1 - index)
{
token = &tokens[index];
cpp_get_token (pfile, token);
if (token->type == CPP_EOF)
break;
line = cpp_get_line (pfile)->output_line;
if (print.lineno != line)
{
unsigned int col = cpp_get_line (pfile)->col;
/* Supply enough whitespace to put this token in its original
column. Don't bother trying to reconstruct tabs; we can't
get it right in general, and nothing ought to care. (Yes,
some things do care; the fault lies with them.) */
maybe_print_line (line);
if (col > 1)
{
if (token->flags & PREV_WHITE)
col--;
while (--col)
putc (' ', print.outf);
}
}
else if (print.printed && ! (token->flags & PREV_WHITE)
&& cpp_avoid_paste (pfile, &tokens[1 - index], token))
token->flags |= PREV_WHITE;
cpp_output_token (token, print.outf);
print.printed = 1;
}
}
while (cpp_pop_buffer (pfile) != 0);
}
/* Initialize a cpp_printer structure. As a side effect, open the
output file. */
static int
printer_init (pfile)
cpp_reader *pfile;
{
print.last_fname = 0;
print.lineno = 0;
print.printed = 0;
print.no_line_dirs = CPP_OPTION (pfile, no_line_commands);
if (CPP_OPTION (pfile, out_fname) == NULL)
CPP_OPTION (pfile, out_fname) = "";
if (CPP_OPTION (pfile, out_fname)[0] == '\0')
print.outf = stdout;
else
{
print.outf = fopen (CPP_OPTION (pfile, out_fname), "w");
if (! print.outf)
{
cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
return 1;
}
}
return 0;
}
/* Newline-terminate any output line currently in progress. If
appropriate, write the current line number to the output, or pad
with newlines so the output line matches the current line. */
static void
maybe_print_line (line)
unsigned int line;
{
/* End the previous line of text (probably only needed until we get
multi-line tokens fixed). */
if (print.printed)
{
putc ('\n', print.outf);
print.lineno++;
print.printed = 0;
}
if (print.no_line_dirs)
return;
if (line >= print.lineno && line < print.lineno + 8)
{
while (line > print.lineno)
{
putc ('\n', print.outf);
print.lineno++;
}
}
else
{
print.lineno = line;
print_line ("");
}
}
static void
print_line (special_flags)
const char *special_flags;
{
/* End any previous line of text. */
if (print.printed)
putc ('\n', print.outf);
print.printed = 0;
fprintf (print.outf, "# %u \"%s\"%s%s\n",
print.lineno, print.last_fname, special_flags, print.syshdr_flags);
}
static void
move_printer (pfile, line, special_flags)
cpp_reader *pfile;
unsigned int line;
const char *special_flags;
{
print.lineno = line;
print.last_fname = pfile->buffer->nominal_fname;
print.syshdr_flags = cpp_syshdr_flags (pfile, pfile->buffer);
print_line (special_flags);
}
/* Callbacks */
static void
cb_ident (pfile, str)
cpp_reader *pfile ATTRIBUTE_UNUSED;
const cpp_string * str;
{
maybe_print_line (cpp_get_line (pfile)->output_line);
fprintf (print.outf, "#ident \"%.*s\"\n", (int) str->len, str->text);
print.lineno++;
}
static void
cb_define (pfile, node)
cpp_reader *pfile;
cpp_hashnode *node;
{
if (pfile->done_initializing)
{
maybe_print_line (cpp_get_line (pfile)->output_line);
fprintf (print.outf, "#define %s", node->name);
/* -dD or -g3 command line options. */
if (CPP_OPTION (pfile, debug_output)
|| CPP_OPTION (pfile, dump_macros) == dump_definitions)
fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
putc ('\n', print.outf);
print.lineno++;
}
}
static void
cb_undef (pfile, node)
cpp_reader *pfile;
cpp_hashnode *node;
{
if (pfile->done_initializing)
{
maybe_print_line (cpp_get_line (pfile)->output_line);
fprintf (print.outf, "#undef %s\n", node->name);
print.lineno++;
}
}
static void
cb_include (pfile, dir, header)
cpp_reader *pfile ATTRIBUTE_UNUSED;
const unsigned char *dir;
const cpp_token *header;
{
maybe_print_line (cpp_get_line (pfile)->output_line);
fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
print.lineno++;
}
static void
cb_enter_file (pfile)
cpp_reader *pfile;
{
/* Bring current file to correct line (except main file). FIXME: we
may be using the same buffer via a # NUMBER "file" 1 directive. */
if (pfile->done_initializing && pfile->buffer->prev)
maybe_print_line (pfile->buffer->prev->lineno);
move_printer (pfile, 1, pfile->done_initializing ? " 1": "");
}
static void
cb_leave_file (pfile)
cpp_reader *pfile;
{
move_printer (pfile, pfile->buffer->lineno + 1, " 2");
}
static void
cb_rename_file (pfile)
cpp_reader *pfile;
{
move_printer (pfile, pfile->buffer->lineno + 1, "");
}
static void
cb_def_pragma (pfile)
cpp_reader *pfile;
{
maybe_print_line (cpp_get_line (pfile)->output_line);
fputs ("#pragma ", print.outf);
cpp_output_line (pfile, print.outf);
print.lineno++;
}
static void
do_pragma_implementation (pfile)
cpp_reader *pfile;
{
/* Be quiet about `#pragma implementation' for a file only if it hasn't
been included yet. */
cpp_token token;
cpp_start_lookahead (pfile);
cpp_get_token (pfile, &token);
cpp_stop_lookahead (pfile, 0);
/* If it's not a string, pass it through and let the front end complain. */
if (token.type == CPP_STRING)
{
/* Make a NUL-terminated copy of the string. */
char *filename = alloca (token.val.str.len + 1);
memcpy (filename, token.val.str.text, token.val.str.len);
filename[token.val.str.len] = '\0';
if (cpp_included (pfile, filename))
cpp_warning (pfile,
"#pragma GCC implementation for \"%s\" appears after file is included",
filename);
}
else if (token.type != CPP_EOF)
{
cpp_error (pfile, "malformed #pragma GCC implementation");
return;
}
/* Output? This is nasty, but we don't have [GCC] implementation in
the buffer. */
if (pfile->cb.def_pragma)
{
maybe_print_line (cpp_get_line (pfile)->output_line);
fputs ("#pragma GCC implementation ", print.outf);
cpp_output_line (pfile, print.outf);
print.lineno++;
}
}
/* Dump out the hash table. */
static int
dump_macro (pfile, node)
cpp_reader *pfile;
cpp_hashnode *node;
{
if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
{
fprintf (print.outf, "#define %s", node->name);
fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
putc ('\n', print.outf);
print.lineno++;
}
return 1;
}