diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 8b21f6bb783ad4dd0adf08d3b949dbe90f4ef583..79977d1ea2150ae4f241bbbb6a2dad2fc4107b58 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,36 @@ +2004-02-04 Per Bothner <per@bothner.com> + + Partially revert/redo 2003-10-01 change; fix -fworking-directory. + * c-ppoutput.c (pp_dir_change): New function. + * c-common.h (pp_dir_change): New declaration. + * cpplib.h (struct cpp_options): Remove working_directory field. + * cppinit.c (cpp_find_main_file, cpp_push_main_file): Merge back to + (cpp_read_main_file): as before 10-01. Call _cpp_stack_file. + Don't handle -fworking_directory here, but in c_common_post_options. + (read_original_directory): Don't back up when done. + Don't clear no-longer used working_directory flag. + * cpplib.h: Update declarations to match. + * c-lex.c (cb_dir_change): Move to c-opts.c. + (init_c_lex): Don't set dir_change callback here, since we want + to set it even if flag_preprocess_only. + * c-opts.c (cb_dir_change): Function moved from c-lex.c. + (c_common_post_options): Set dir_change callback. + Call pp_dir_change if approporiate. + (finish_options): Don't call cpp_find_main_file here. Hence remove + unneeded parameter and result. Do LC_RENAME for <built-in>. + (c_common_post_options): Call cpp_read_main_file here instead. + (c_common_init): Update accordingly. + (push_command_line_include): Don't cpp_push_main_file. + Do LC_RENAME rather than LC_LEASE to get back to main file. + Compared to pre-10-01 version, inline cpp_rename_to_main_file. + (c_common_parse_file): Call cpp_read_main_file for subsequent main + files, but call finish_options for all files. + * c-opts.c (sanitize_cpp_opts): Don't set cpp_opts->working_directory. + * fix-header.c (read_scan_file): Call cpp_read_main_file instead of + cpp_find_main_file + cpp_push_main_file. + * c-lex.c (fe_file_change): Don't set main_input_filename here. + * opts.c (handle_options): Only set main_input_filename first time. + 2004-02-05 Ian Lance Taylor <ian@wasabisystems.com> * config/arm/arm.h (REG_CLASS_NAMES): Add missing comma. diff --git a/gcc/c-common.h b/gcc/c-common.h index aec0fc569e0c5b0b6de903232f70951382fb3a63..f948e44c6949d14079d6e4ff878538156fe2afd0 100644 --- a/gcc/c-common.h +++ b/gcc/c-common.h @@ -1340,5 +1340,6 @@ extern void objc_mark_locals_volatile (void *); extern void init_pp_output (FILE *); extern void preprocess_file (cpp_reader *); extern void pp_file_change (const struct line_map *); +extern void pp_dir_change (cpp_reader *, const char *); #endif /* ! GCC_C_COMMON_H */ diff --git a/gcc/c-lex.c b/gcc/c-lex.c index 64471259e439d4cf250ff4dc124b08b4b9562d48..aba571fab9a211eb73b9cca1c6e48f0642d3fba4 100644 --- a/gcc/c-lex.c +++ b/gcc/c-lex.c @@ -69,7 +69,6 @@ static tree lex_charconst (const cpp_token *); static void update_header_times (const char *); static int dump_one_header (splay_tree_node, void *); static void cb_line_change (cpp_reader *, const cpp_token *, int); -static void cb_dir_change (cpp_reader *, const char *); static void cb_ident (cpp_reader *, unsigned int, const cpp_string *); static void cb_def_pragma (cpp_reader *, unsigned int); static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *); @@ -96,7 +95,6 @@ init_c_lex (void) cb = cpp_get_callbacks (parse_in); cb->line_change = cb_line_change; - cb->dir_change = cb_dir_change; cb->ident = cb_ident; cb->def_pragma = cb_def_pragma; cb->valid_pch = c_common_valid_pch; @@ -202,13 +200,6 @@ cb_line_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const cpp_token *token, input_line = SOURCE_LINE (map, token->line); } -static void -cb_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir) -{ - if (! set_src_pwd (dir)) - warning ("too late for # directive to set debug directory"); -} - void fe_file_change (const struct line_map *new_map) { @@ -222,9 +213,7 @@ fe_file_change (const struct line_map *new_map) { /* Don't stack the main buffer on the input stack; we already did in compile_file. */ - if (map == NULL) - main_input_filename = new_map->to_file; - else + if (map != NULL) { int included_at = SOURCE_LINE (new_map - 1, new_map->from_line - 1); diff --git a/gcc/c-opts.c b/gcc/c-opts.c index c89ba0040e68bdc6f0be8b0f74c5fdc30c0f9801..6fbfd1cee6c3c0970b2f0090d9f215da5462d96f 100644 --- a/gcc/c-opts.c +++ b/gcc/c-opts.c @@ -108,7 +108,8 @@ static void sanitize_cpp_opts (void); static void add_prefixed_path (const char *, size_t); static void push_command_line_include (void); static void cb_file_change (cpp_reader *, const struct line_map *); -static bool finish_options (const char *); +static void cb_dir_change (cpp_reader *, const char *); +static void finish_options (void); #ifndef STDC_0_IN_SYSTEM_HEADERS #define STDC_0_IN_SYSTEM_HEADERS 0 @@ -1053,8 +1054,10 @@ c_common_handle_option (size_t scode, const char *arg, int value) /* Post-switch processing. */ bool -c_common_post_options (const char **pfilename ATTRIBUTE_UNUSED) +c_common_post_options (const char **pfilename) { + struct cpp_callbacks *cb; + /* Canonicalize the input and output filenames. */ if (in_fnames == NULL) { @@ -1142,7 +1145,9 @@ c_common_post_options (const char **pfilename ATTRIBUTE_UNUSED) input_line = 0; } - cpp_get_callbacks (parse_in)->file_change = cb_file_change; + cb = cpp_get_callbacks (parse_in); + cb->file_change = cb_file_change; + cb->dir_change = cb_dir_change; cpp_post_options (parse_in); saved_lineno = input_line; @@ -1152,6 +1157,14 @@ c_common_post_options (const char **pfilename ATTRIBUTE_UNUSED) immediately. */ errorcount += cpp_errors (parse_in); + *pfilename = this_input_filename + = cpp_read_main_file (parse_in, in_fnames[0]); + if (this_input_filename == NULL) + return true; + + if (flag_preprocess_only && flag_working_directory) + pp_dir_change (parse_in, get_src_pwd ()); + return flag_preprocess_only; } @@ -1176,8 +1189,8 @@ c_common_init (void) if (flag_preprocess_only) { - if (finish_options (in_fnames[0])) - preprocess_file (parse_in); + finish_options (); + preprocess_file (parse_in); return false; } @@ -1211,10 +1224,12 @@ c_common_parse_file (int set_yydebug ATTRIBUTE_UNUSED) /* Reset cpplib's macros and start a new file. */ cpp_undef_all (parse_in); + main_input_filename = this_input_filename + = cpp_read_main_file (parse_in, in_fnames[file_index]); + if (this_input_filename == NULL) + break; } - - if (! finish_options(in_fnames[file_index])) - break; + finish_options (); if (file_index == 0) pch_init(); c_parse_file (); @@ -1355,8 +1370,6 @@ sanitize_cpp_opts (void) actually output the current directory? */ if (flag_working_directory == -1) flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE); - cpp_opts->working_directory - = flag_preprocess_only && flag_working_directory; } /* Add include path with a prefix at the front of its name. */ @@ -1379,21 +1392,15 @@ add_prefixed_path (const char *suffix, size_t chain) add_path (path, chain, 0); } -/* Handle -D, -U, -A, -imacros, and the first -include. - TIF is the input file to which we will return after processing all - the includes. Returns true on success. */ -static bool -finish_options (const char *tif) +/* Handle -D, -U, -A, -imacros, and the first -include. */ +static void +finish_options (void) { - this_input_filename = tif; - if (! cpp_find_main_file (parse_in, this_input_filename)) - return false; - if (!cpp_opts->preprocessed) { size_t i; - cpp_change_file (parse_in, LC_ENTER, _("<built-in>")); + cpp_change_file (parse_in, LC_RENAME, _("<built-in>")); cpp_init_builtins (parse_in, flag_hosted); c_cpp_builtins (parse_in); @@ -1443,7 +1450,6 @@ finish_options (const char *tif) include_cursor = 0; push_command_line_include (); - return true; } /* Give CPP the next file given by -include, if any. */ @@ -1462,12 +1468,15 @@ push_command_line_include (void) if (include_cursor == deferred_count) { include_cursor++; - /* Restore the line map from <command line>. */ - if (! cpp_opts->preprocessed) - cpp_change_file (parse_in, LC_LEAVE, NULL); /* -Wunused-macros should only warn about macros defined hereafter. */ cpp_opts->warn_unused_macros = warn_unused_macros; - cpp_push_main_file (parse_in); + /* Restore the line map from <command line>. */ + if (! cpp_opts->preprocessed) + cpp_change_file (parse_in, LC_RENAME, main_input_filename); + + /* Set this here so the client can change the option if it wishes, + and after stacking the main file so we don't trace the main file. */ + line_table.trace_includes = cpp_opts->print_include_names; } } @@ -1485,6 +1494,13 @@ cb_file_change (cpp_reader *pfile ATTRIBUTE_UNUSED, push_command_line_include (); } +void +cb_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir) +{ + if (! set_src_pwd (dir)) + warning ("too late for # directive to set debug directory"); +} + /* Set the C 89 standard (with 1994 amendments if C94, without GNU extensions if ISO). There is no concept of gnu94. */ static void diff --git a/gcc/c-ppoutput.c b/gcc/c-ppoutput.c index 22e595f95411fd90a32abc35e244fdccfb0d1aad..f3f8f81c1c5904ed6a49cf8c828ce5bdf782711a 100644 --- a/gcc/c-ppoutput.c +++ b/gcc/c-ppoutput.c @@ -334,6 +334,22 @@ cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, fileline line, print.line++; } +/* Callback called when -fworking-director and -E to emit working + diretory in cpp output file. */ + +void +pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir) +{ + size_t to_file_len = strlen (dir); + unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1); + unsigned char *p; + + /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */ + p = cpp_quote_string (to_file_quoted, (unsigned char *) dir, to_file_len); + *p = '\0'; + fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted); +} + /* The file name, line number or system header flags have changed, as described in MAP. From this point on, the old print.map might be pointing to freed memory, and so must not be dereferenced. */ diff --git a/gcc/cppinit.c b/gcc/cppinit.c index d6509fd6a3f04255ff93b24173c871d383d08aed..0fc9c682baf5ab113661eeed6fac90149eedd03e 100644 --- a/gcc/cppinit.c +++ b/gcc/cppinit.c @@ -453,9 +453,10 @@ cpp_post_options (cpp_reader *pfile) } /* Setup for processing input from the file named FNAME, or stdin if - it is the empty string. Returns true if the file was found. */ -bool -cpp_find_main_file (cpp_reader *pfile, const char *fname) + it is the empty string. Return the original filename + on success (e.g. foo.i->foo.c), or NULL on failure. */ +const char * +cpp_read_main_file (cpp_reader *pfile, const char *fname) { if (CPP_OPTION (pfile, deps.style) != DEPS_NONE) { @@ -471,37 +472,16 @@ cpp_find_main_file (cpp_reader *pfile, const char *fname) if (_cpp_find_failed (pfile->main_file)) return false; - if (CPP_OPTION (pfile, working_directory)) - { - const char *dir = getpwd (); - char *dir_with_slashes = alloca (strlen (dir) + 3); - - memcpy (dir_with_slashes, dir, strlen (dir)); - memcpy (dir_with_slashes + strlen (dir), "//", 3); - - if (pfile->cb.dir_change) - pfile->cb.dir_change (pfile, dir); - } - return true; -} - -/* This function reads the file, but does not start preprocessing. - This will generate at least one file change callback, and possibly - a line change callback. */ -void -cpp_push_main_file (cpp_reader *pfile) -{ _cpp_stack_file (pfile, pfile->main_file, false); /* For foo.i, read the original filename foo.c now, for the benefit of the front ends. */ if (CPP_OPTION (pfile, preprocessed)) - read_original_filename (pfile); - - /* Set this here so the client can change the option if it wishes, - and after stacking the main file so we don't trace the main - file. */ - pfile->line_table->trace_includes = CPP_OPTION (pfile, print_include_names); + { + read_original_filename (pfile); + fname = pfile->map->to_file; + } + return fname; } /* For preprocessed files, if the first tokens are of the form # NUM. @@ -579,13 +559,7 @@ read_original_directory (cpp_reader *pfile) debugdir[token->val.str.len - 4] = '\0'; pfile->cb.dir_change (pfile, debugdir); - } - - /* We want to process the fake line changes as regular changes, to - get them output. */ - _cpp_backup_tokens (pfile, 3); - - CPP_OPTION (pfile, working_directory) = false; + } } /* This is called at the end of preprocessing. It pops the last diff --git a/gcc/cpplib.h b/gcc/cpplib.h index b3277c151e8626ffdba00d99d26b48f7f906c1ed..fd512b6412c3b776e4733d98ab2993df49cab584 100644 --- a/gcc/cpplib.h +++ b/gcc/cpplib.h @@ -373,11 +373,6 @@ struct cpp_options /* Nonzero means __STDC__ should have the value 0 in system headers. */ unsigned char stdc_0_in_system_headers; - - /* Nonzero means output a directory line marker right after the - initial file name line marker, and before a duplicate initial - line marker. */ - bool working_directory; }; /* Call backs to cpplib client. */ @@ -536,14 +531,12 @@ extern const struct line_maps *cpp_get_line_maps (cpp_reader *); extern cpp_callbacks *cpp_get_callbacks (cpp_reader *); extern void cpp_set_callbacks (cpp_reader *, cpp_callbacks *); -/* This function finds the main file, but does not start reading it. - Returns true iff the file was found. */ -extern bool cpp_find_main_file (cpp_reader *, const char *); - -/* This function reads the file, but does not start preprocessing. - This will generate at least one file change callback, and possibly - a line change callback. */ -extern void cpp_push_main_file (cpp_reader *); +/* This function reads the file, but does not start preprocessing. It + returns the name of the original file; this is the same as the + input file, except for preprocessed input. This will generate at + least one file change callback, and possibly a line change callback + too. If there was an error opening the file, it returns NULL. */ +extern const char *cpp_read_main_file (cpp_reader *, const char *); /* Set up built-ins like __FILE__. */ extern void cpp_init_builtins (cpp_reader *, int); diff --git a/gcc/fix-header.c b/gcc/fix-header.c index 2852ef50483081d166a8d68f17c7710362b82559..43916c54789100460a8a9130792ef4a543e0a4ac 100644 --- a/gcc/fix-header.c +++ b/gcc/fix-header.c @@ -606,9 +606,8 @@ read_scan_file (char *in_fname, int argc, char **argv) options->inhibit_errors = 1; cpp_post_options (scan_in); - if (!cpp_find_main_file (scan_in, in_fname)) + if (!cpp_read_main_file (scan_in, in_fname)) exit (FATAL_EXIT_CODE); - cpp_push_main_file (scan_in); cpp_change_file (scan_in, LC_RENAME, "<built-in>"); cpp_init_builtins (scan_in, true); diff --git a/gcc/opts.c b/gcc/opts.c index 2f07d6d7f0257d78d80c6bb75fcbe96e1b54ca18..f49228b8cc1eb7dd0a03c8601f2d70fcfb8ed4d5 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -448,7 +448,8 @@ handle_options (unsigned int argc, const char **argv, unsigned int lang_mask) /* Interpret "-" or a non-switch as a file name. */ if (opt[0] != '-' || opt[1] == '\0') { - main_input_filename = opt; + if (main_input_filename == NULL) + main_input_filename = opt; add_input_filename (opt); n = 1; continue;