diff options
author | Neil Booth <neilb@earthling.net> | 2000-10-28 17:59:06 +0000 |
---|---|---|
committer | Neil Booth <neil@gcc.gnu.org> | 2000-10-28 17:59:06 +0000 |
commit | 93c80368d9a16c073f2b930bef4232661971765f (patch) | |
tree | b1f50a88de9fbdd6a784d0c03fa8cd31e4ac5916 /gcc/cppmain.c | |
parent | de48b52da8468f94c0bdffc11fa7e8fe49b4ba66 (diff) | |
download | gcc-93c80368d9a16c073f2b930bef4232661971765f.zip gcc-93c80368d9a16c073f2b930bef4232661971765f.tar.gz gcc-93c80368d9a16c073f2b930bef4232661971765f.tar.bz2 |
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
Diffstat (limited to 'gcc/cppmain.c')
-rw-r--r-- | gcc/cppmain.c | 357 |
1 files changed, 252 insertions, 105 deletions
diff --git a/gcc/cppmain.c b/gcc/cppmain.c index 5c88e95..fc58f6a 100644 --- a/gcc/cppmain.c +++ b/gcc/cppmain.c @@ -25,29 +25,45 @@ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "cpplib.h" #include "intl.h" -const char *progname; - -cpp_reader parse_in; -cpp_printer parse_out; +/* 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 unsigned char *, unsigned int, int)); - -static void cb_ident PARAMS ((cpp_reader *, const unsigned char *, - unsigned int)); + 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 *)); -static int dump_macros_helper PARAMS ((cpp_reader *, cpp_hashnode *)); + +const char *progname; +cpp_reader parse_in; +struct printer print; int main (argc, argv) @@ -56,7 +72,6 @@ main (argc, argv) { char *p; cpp_reader *pfile = &parse_in; - cpp_printer *print; int argi = 1; /* Next argument to handle. */ p = argv[0] + strlen (argv[0]); @@ -83,8 +98,7 @@ main (argc, argv) /* 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). */ - print = cpp_printer_init (pfile, &parse_out); - if (! print) + if (printer_init (pfile)) return (FATAL_EXIT_CODE); /* Set callbacks. */ @@ -116,134 +130,260 @@ main (argc, argv) cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation); cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation); - if (! cpp_start_read (pfile, print, CPP_OPTION (pfile, in_fname))) + if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname))) return (FATAL_EXIT_CODE); - if (CPP_OPTION (pfile, no_output)) - while (CPP_BUFFER (pfile) != NULL) - cpp_scan_buffer_nooutput (pfile); - else - while (CPP_BUFFER (pfile) != NULL) - cpp_scan_buffer (pfile, print); + 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_macros_helper); - - cpp_finish (pfile, print); + 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); } -/* Callbacks */ +/* 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 -cb_ident (pfile, str, len) +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; - const unsigned char *str; - unsigned int len; + 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; { - cpp_printf (pfile, &parse_out, "#ident \"%.*s\"\n", (int) len, str); - parse_out.lineno++; + 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, hash) +cb_define (pfile, node) cpp_reader *pfile; - cpp_hashnode *hash; + cpp_hashnode *node; { if (pfile->done_initializing) { - cpp_printf (pfile, &parse_out, "#define %s", hash->name); + 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) - cpp_dump_definition (pfile, parse_out.outf, hash); - putc ('\n', parse_out.outf); - parse_out.lineno++; + fputs ((const char *) cpp_macro_definition (pfile, node), print.outf); + + putc ('\n', print.outf); + print.lineno++; } } static void -cb_undef (pfile, hash) +cb_undef (pfile, node) cpp_reader *pfile; - cpp_hashnode *hash; + cpp_hashnode *node; { if (pfile->done_initializing) { - cpp_printf (pfile, &parse_out, "#undef %s\n", hash->name); - parse_out.lineno++; + 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, str, len, ab) - cpp_reader *pfile; +cb_include (pfile, dir, header) + cpp_reader *pfile ATTRIBUTE_UNUSED; const unsigned char *dir; - const unsigned char *str; - unsigned int len; - int ab; + const cpp_token *header; { - int l, r; - if (ab) - l = '<', r = '>'; - else - l = '"', r = '"'; - - cpp_printf (pfile, &parse_out, "#%s %c%.*s%c\n", dir, l, (int) len, str, r); - parse_out.lineno++; + 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; { - cpp_buffer *ip = CPP_BUFFER (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); - cpp_printf (pfile, &parse_out, "# 1 \"%s\"%s%s\n", ip->nominal_fname, - pfile->done_initializing ? " 1" : "", - cpp_syshdr_flags (pfile, ip)); - - parse_out.lineno = 1; - parse_out.last_fname = ip->nominal_fname; + move_printer (pfile, 1, pfile->done_initializing ? " 1": ""); } static void cb_leave_file (pfile) cpp_reader *pfile; { - cpp_buffer *ip = CPP_BUFFER (pfile); - - cpp_printf (pfile, &parse_out, "# %u \"%s\" 2%s\n", ip->lineno, - ip->nominal_fname, cpp_syshdr_flags (pfile, ip)); - - parse_out.lineno = ip->lineno; - parse_out.last_fname = ip->nominal_fname; + move_printer (pfile, pfile->buffer->lineno + 1, " 2"); } static void cb_rename_file (pfile) cpp_reader *pfile; { - cpp_buffer *ip = CPP_BUFFER (pfile); - - cpp_printf (pfile, &parse_out, "# %u \"%s\"%s\n", ip->lineno, - ip->nominal_fname, cpp_syshdr_flags (pfile, ip)); - - parse_out.lineno = ip->lineno; - parse_out.last_fname = ip->nominal_fname; + move_printer (pfile, pfile->buffer->lineno + 1, ""); } static void cb_def_pragma (pfile) cpp_reader *pfile; { - cpp_printf (pfile, &parse_out, "#pragma "); - cpp_output_list (pfile, parse_out.outf, &pfile->token_list, - pfile->first_directive_token + 2); - putc ('\n', parse_out.outf); - parse_out.lineno++; + maybe_print_line (cpp_get_line (pfile)->output_line); + fputs ("#pragma ", print.outf); + cpp_output_line (pfile, print.outf); + print.lineno++; } static void @@ -252,47 +392,54 @@ do_pragma_implementation (pfile) { /* Be quiet about `#pragma implementation' for a file only if it hasn't been included yet. */ - const cpp_token *tok = cpp_get_token (pfile); - char *copy; + cpp_token token; - if (tok->type != CPP_EOF) - { - if (tok->type != CPP_STRING || cpp_get_token (pfile)->type != CPP_EOF) - { - cpp_error (pfile, "malformed #pragma implementation"); - return; - } + cpp_start_lookahead (pfile); + cpp_get_token (pfile, &token); + cpp_stop_lookahead (pfile, 0); - /* Make a NUL-terminated copy of the string. */ - copy = alloca (tok->val.str.len + 1); - memcpy (copy, tok->val.str.text, tok->val.str.len); - copy[tok->val.str.len] = '\0'; - - if (cpp_included (pfile, copy)) + /* 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 implementation for %s appears after file is included", - copy); + "#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; } - /* forward to default-pragma handler. */ + /* Output? This is nasty, but we don't have [GCC] implementation in + the buffer. */ if (pfile->cb.def_pragma) - (*pfile->cb.def_pragma) (pfile); + { + 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_macros_helper (pfile, hp) +dump_macro (pfile, node) cpp_reader *pfile; - cpp_hashnode *hp; + cpp_hashnode *node; { - if (hp->type == T_MACRO) + if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) { - cpp_printf (pfile, &parse_out, "#define %s", hp->name); - cpp_dump_definition (pfile, parse_out.outf, hp); - putc ('\n', parse_out.outf); - parse_out.lineno++; + 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; } - |