From d7bc7a9850ac55871f1108c491585118e10afd04 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Tue, 21 Aug 2001 06:20:18 +0000 Subject: [PATCH] cpphash.h (_cpp_push_next_buffer): New. * cpphash.h (_cpp_push_next_buffer): New. * cppinit.c (do_includes): Remove. (push_include, free_chain, _cpp_push_next_buffer): New. (cpp_start_read): Use them to rework command line option handling. (cpp_handle_option): Combine handling of -include and -imacros. * cpplex.c (_cpp_lex_token): Push a new -include buffer if appropriate. Always insert missing \n at EOF. * cpplib.c (start_directive): Get the directive position right. * cppmain.c (cb_file_change): Always print the first line, unless preprocessed. From-SVN: r45070 --- gcc/ChangeLog | 13 ++++ gcc/cpphash.h | 3 + gcc/cppinit.c | 166 ++++++++++++++++++++++++++++---------------------- gcc/cpplex.c | 27 ++++---- gcc/cpplib.c | 1 + gcc/cppmain.c | 15 +++-- 6 files changed, 136 insertions(+), 89 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index d8275ad1f14..8f715052e83 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,16 @@ +2001-08-21 Neil Booth + + * cpphash.h (_cpp_push_next_buffer): New. + * cppinit.c (do_includes): Remove. + (push_include, free_chain, _cpp_push_next_buffer): New. + (cpp_start_read): Use them to rework command line option handling. + (cpp_handle_option): Combine handling of -include and -imacros. + * cpplex.c (_cpp_lex_token): Push a new -include buffer if + appropriate. Always insert missing \n at EOF. + * cpplib.c (start_directive): Get the directive position right. + * cppmain.c (cb_file_change): Always print the first line, unless + preprocessed. + 2001-08-20 Andreas Jaeger * profile.c (compute_branch_probabilities): Remove extra new-line diff --git a/gcc/cpphash.h b/gcc/cpphash.h index 0a24f4d6c5c..f530e5596ea 100644 --- a/gcc/cpphash.h +++ b/gcc/cpphash.h @@ -419,6 +419,9 @@ extern unsigned char *_cpp_next_chunk PARAMS ((cpp_pool *, unsigned int, extern void _cpp_lock_pool PARAMS ((cpp_pool *)); extern void _cpp_unlock_pool PARAMS ((cpp_pool *)); +/* In cppinit.c. */ +extern bool _cpp_push_next_buffer PARAMS ((cpp_reader *)); + /* In cpplib.c */ extern int _cpp_test_assertion PARAMS ((cpp_reader *, int *)); extern int _cpp_handle_directive PARAMS ((cpp_reader *, int)); diff --git a/gcc/cppinit.c b/gcc/cppinit.c index 4a78a938987..0c4aa01da97 100644 --- a/gcc/cppinit.c +++ b/gcc/cppinit.c @@ -105,9 +105,9 @@ static struct search_path * remove_dup_dir PARAMS ((cpp_reader *, static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *, struct search_path *)); static void merge_include_chains PARAMS ((cpp_reader *)); -static void do_includes PARAMS ((cpp_reader *, - struct pending_option *, - int)); +static bool push_include PARAMS ((cpp_reader *, + struct pending_option *)); +static void free_chain PARAMS ((struct pending_option *)); static void set_lang PARAMS ((cpp_reader *, enum c_lang)); static void init_dependency_output PARAMS ((cpp_reader *)); static void init_standard_includes PARAMS ((cpp_reader *)); @@ -867,36 +867,38 @@ init_standard_includes (pfile) } } -/* Handles -imacro and -include from the command line. */ -static void -do_includes (pfile, p, scan) +/* Pushes a -imacro and -include file given on the command line onto + the buffer stack. Returns non-zero if successful. */ +static bool +push_include (pfile, p) cpp_reader *pfile; struct pending_option *p; - int scan; { - while (p) - { - struct pending_option *q; + cpp_token header; - /* Don't handle if -fpreprocessed. Later: maybe update this to - use the #include "" search path if cpp_read_file fails. */ - if (CPP_OPTION (pfile, preprocessed)) - cpp_error (pfile, "-include and -imacros cannot be used with -fpreprocessed"); - else - { - cpp_token header; - header.type = CPP_STRING; - header.val.str.text = (const unsigned char *) p->arg; - header.val.str.len = strlen (p->arg); - if (_cpp_execute_include (pfile, &header, IT_CMDLINE) && scan) - { - pfile->buffer->return_at_eof = true; - cpp_scan_nooutput (pfile); - } - } - q = p->next; - free (p); - p = q; + /* Later: maybe update this to use the #include "" search path + if cpp_read_file fails. */ + header.type = CPP_STRING; + header.val.str.text = (const unsigned char *) p->arg; + header.val.str.len = strlen (p->arg); + /* Make the command line directive take up a line. */ + pfile->lexer_pos.line = pfile->lexer_pos.output_line = ++pfile->line; + + return _cpp_execute_include (pfile, &header, IT_CMDLINE); +} + +/* Frees a pending_option chain. */ +static void +free_chain (head) + struct pending_option *head; +{ + struct pending_option *next; + + while (head) + { + next = head->next; + free (head); + head = next; } } @@ -908,8 +910,6 @@ cpp_start_read (pfile, fname) cpp_reader *pfile; const char *fname; { - struct pending_option *p, *q; - /* Set up the include search path now. */ if (! CPP_OPTION (pfile, no_standard_includes)) init_standard_includes (pfile); @@ -939,42 +939,73 @@ cpp_start_read (pfile, fname) if (!_cpp_read_file (pfile, fname)) return 0; - /* FIXME: we want to set up linemaps with _("") and - _("") somewhere round here. Harder than it looks. */ - - /* If already preprocessed, don't install __LINE__, etc., and ignore - command line definitions and assertions. Handle -U's, -D's and - -A's in the order they were seen. */ + /* Install builtins and process command line macros etc. in the order + they appeared, but only if not already preprocessed. */ if (! CPP_OPTION (pfile, preprocessed)) - init_builtins (pfile); - - p = CPP_OPTION (pfile, pending)->directive_head; - while (p) { - if (! CPP_OPTION (pfile, preprocessed)) + struct pending_option *p; + + _cpp_do_file_change (pfile, LC_RENAME, _(""), 1, 0); + init_builtins (pfile); + _cpp_do_file_change (pfile, LC_RENAME, _(""), 1, 0); + for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next) (*p->handler) (pfile, p->arg); - q = p->next; - free (p); - p = q; + + /* Scan -imacros files after command line defines, but before + files given with -include. */ + for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next) + { + if (push_include (pfile, p)) + { + pfile->buffer->return_at_eof = true; + cpp_scan_nooutput (pfile); + } + } } - /* Hopefully a short-term kludge. We stacked the main file at line - zero. The intervening macro definitions have messed up line - numbering, so we need to restore it. */ - pfile->lexer_pos.output_line = pfile->line = 0; + free_chain (CPP_OPTION (pfile, pending)->directive_head); + free_chain (CPP_OPTION (pfile, pending)->imacros_head); + _cpp_push_next_buffer (pfile); - /* The -imacros files can be scanned now, but the -include files - have to be pushed onto the buffer stack and processed later, - otherwise cppmain.c won't see the tokens. include_head was built - up as a stack, and popping this stack onto the buffer stack means - we preserve the order of the command line. */ - do_includes (pfile, CPP_OPTION (pfile, pending)->imacros_head, 1); - do_includes (pfile, CPP_OPTION (pfile, pending)->include_head, 0); + return 1; +} - free (CPP_OPTION (pfile, pending)); - CPP_OPTION (pfile, pending) = NULL; +/* Called to push the next buffer on the stack given by -include. If + there are none, free the pending structure and restore the line map + for the main file. */ +bool +_cpp_push_next_buffer (pfile) + cpp_reader *pfile; +{ + bool pushed = false; - return 1; + if (CPP_OPTION (pfile, pending)) + { + while (!pushed) + { + struct pending_option *p = CPP_OPTION (pfile, pending)->include_head; + + if (p == NULL) + break; + if (! CPP_OPTION (pfile, preprocessed)) + pushed = push_include (pfile, p); + CPP_OPTION (pfile, pending)->include_head = p->next; + free (p); + } + + if (!pushed) + { + free (CPP_OPTION (pfile, pending)); + CPP_OPTION (pfile, pending) = NULL; + + /* Restore the line map for the main file. */ + if (! CPP_OPTION (pfile, preprocessed)) + _cpp_do_file_change (pfile, LC_RENAME, + pfile->line_maps.maps[0].to_file, 1, 0); + } + } + + return pushed; } /* Use mkdeps.c to output dependency information. */ @@ -1563,18 +1594,6 @@ cpp_handle_option (pfile, argc, argv) append_include_chain (pfile, xstrdup (arg), SYSTEM, 0); break; case OPT_include: - { - struct pending_option *o = (struct pending_option *) - xmalloc (sizeof (struct pending_option)); - o->arg = arg; - - /* This list has to be built in reverse order so that - when cpp_start_read pushes all the -include files onto - the buffer stack, they will be scanned in forward order. */ - o->next = pend->include_head; - pend->include_head = o; - } - break; case OPT_imacros: { struct pending_option *o = (struct pending_option *) @@ -1582,7 +1601,10 @@ cpp_handle_option (pfile, argc, argv) o->arg = arg; o->next = NULL; - APPEND (pend, imacros, o); + if (opt_code == OPT_include) + APPEND (pend, include, o); + else + APPEND (pend, imacros, o); } break; case OPT_iwithprefix: diff --git a/gcc/cpplex.c b/gcc/cpplex.c index e1657a783ce..a24acabd578 100644 --- a/gcc/cpplex.c +++ b/gcc/cpplex.c @@ -890,6 +890,17 @@ _cpp_lex_token (pfile, result) switch (c) { case EOF: + /* Non-empty files should end in a newline. Don't warn for + command line and _Pragma buffers. */ + if (pfile->lexer_pos.col != 0) + { + /* Account for the missing \n, prevent multiple warnings. */ + pfile->line++; + pfile->lexer_pos.col = 0; + if (!buffer->from_stage3) + cpp_pedwarn (pfile, "no newline at end of file"); + } + /* To prevent bogus diagnostics, only pop the buffer when in-progress directives and arguments have been taken care of. Decrement the line to terminate an in-progress directive. */ @@ -897,23 +908,15 @@ _cpp_lex_token (pfile, result) pfile->lexer_pos.output_line = pfile->line--; else if (! pfile->state.parsing_args) { - /* Non-empty files should end in a newline. Don't warn for - command line and _Pragma buffers. */ - if (pfile->lexer_pos.col != 0) - { - /* Account for the missing \n, prevent multiple warnings. */ - pfile->line++; - pfile->lexer_pos.col = 0; - if (!buffer->from_stage3) - cpp_pedwarn (pfile, "no newline at end of file"); - } - - /* Don't pop the last file. */ + /* Don't pop the last buffer. */ if (buffer->prev) { unsigned char stop = buffer->return_at_eof; _cpp_pop_buffer (pfile); + /* Push the next -included file, if any. */ + if (!pfile->buffer->prev) + _cpp_push_next_buffer (pfile); if (!stop) goto next_token; } diff --git a/gcc/cpplib.c b/gcc/cpplib.c index eab9f31c3d6..e02ab696c85 100644 --- a/gcc/cpplib.c +++ b/gcc/cpplib.c @@ -226,6 +226,7 @@ start_directive (pfile) /* Some handlers need the position of the # for diagnostics. */ pfile->directive_pos = pfile->lexer_pos; + pfile->directive_pos.line = pfile->line; pfile->directive_line = pfile->line; /* Don't save directive tokens for external clients. */ diff --git a/gcc/cppmain.c b/gcc/cppmain.c index b1166155908..b710dadc576 100644 --- a/gcc/cppmain.c +++ b/gcc/cppmain.c @@ -399,11 +399,17 @@ cb_file_change (pfile, map) cpp_reader *pfile ATTRIBUTE_UNUSED; const struct line_map *map; { - /* Not first time? */ - if (print.map) - { - const char *flags = ""; + const char *flags = ""; + /* First time? */ + if (print.map == NULL) + { + /* Avoid printing foo.i when the main file is foo.c. */ + if (!options->preprocessed) + print_line (map, map->from_line, flags); + } + else + { /* Bring current file to correct line when entering a new file. */ if (map->reason == LC_ENTER) maybe_print_line (map - 1, map->from_line - 1); @@ -412,7 +418,6 @@ cb_file_change (pfile, map) flags = " 1"; else if (map->reason == LC_LEAVE) flags = " 2"; - print_line (map, map->from_line, flags); } -- 2.30.2