gcc/libcpp/makedepend.c

207 lines
4.9 KiB
C
Raw Normal View History

Makefile.am: Add makedepend. libcpp: * Makefile.am: Add makedepend. * Makefile.in, aclocal.m4: Regenerate. * charset.c: Insert a space to avoid a warning. * directives.c: Include mkdeps.h. (_cpp_handle_directive): Reenable macro expander if appropriate. (undefine_macros): Inline body of _cpp_free_definition for speed. Do not call undef callback or _cpp_warn_if_unused_macro. (cpp_get_deps): New interface. * files.c (search_cache): Add pfile argument. Check for file that would be found by "" or <> search here... (_cpp_find_file): ...not here. Correct recorded start_dir of files found by directory-of-current-file search that would be found by "" or <> search. * init.c (cpp_add_dependency_target): Delete. * internal.h (struct lexer_state): Add discarding_output flag. * lex.c (lex_identifier): Compute hash function while scanning. * macro.c (cpp_scan_nooutput): Disable macro expansion outside directives. * makedepend.c: New file. * mkdeps.c (struct deps): Add vpath vector. (apply_vpath, deps_add_vpath): New function. (deps_free): Free vpath vector. (deps_add_dep, deps_add_target): Use apply_vpath. * symtab.c (calc_hash): Use HT_HASHSTEP and HT_FINISH. (ht_lookup_with_hash): New function. * cpplib.h, mkdeps.h: Update prototypes. * symtab.h: Update prototypes. (HT_HASHSTEP, HT_FINISH): New macros. gcc: * Makefile.in (MKDEPS_H): New shorthand. (c-opts.o): Update dependencies. * c-opts.c: Include mkdeps.h. (handle_deferred_opts): Use cpp_get_deps and deps_add_target, not cpp_add_dependency_target. From-SVN: r82654
2004-06-05 22:58:06 +02:00
/* Dependency generator utility.
Copyright (C) 2004 Free Software Foundation, Inc.
Contributed by Zack Weinberg, May 2004
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Makefile.am: Add makedepend. libcpp: * Makefile.am: Add makedepend. * Makefile.in, aclocal.m4: Regenerate. * charset.c: Insert a space to avoid a warning. * directives.c: Include mkdeps.h. (_cpp_handle_directive): Reenable macro expander if appropriate. (undefine_macros): Inline body of _cpp_free_definition for speed. Do not call undef callback or _cpp_warn_if_unused_macro. (cpp_get_deps): New interface. * files.c (search_cache): Add pfile argument. Check for file that would be found by "" or <> search here... (_cpp_find_file): ...not here. Correct recorded start_dir of files found by directory-of-current-file search that would be found by "" or <> search. * init.c (cpp_add_dependency_target): Delete. * internal.h (struct lexer_state): Add discarding_output flag. * lex.c (lex_identifier): Compute hash function while scanning. * macro.c (cpp_scan_nooutput): Disable macro expansion outside directives. * makedepend.c: New file. * mkdeps.c (struct deps): Add vpath vector. (apply_vpath, deps_add_vpath): New function. (deps_free): Free vpath vector. (deps_add_dep, deps_add_target): Use apply_vpath. * symtab.c (calc_hash): Use HT_HASHSTEP and HT_FINISH. (ht_lookup_with_hash): New function. * cpplib.h, mkdeps.h: Update prototypes. * symtab.h: Update prototypes. (HT_HASHSTEP, HT_FINISH): New macros. gcc: * Makefile.in (MKDEPS_H): New shorthand. (c-opts.o): Update dependencies. * c-opts.c: Include mkdeps.h. (handle_deferred_opts): Use cpp_get_deps and deps_add_target, not cpp_add_dependency_target. From-SVN: r82654
2004-06-05 22:58:06 +02:00
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 "line-map.h"
#include "cpplib.h"
#include "getopt.h"
#include "mkdeps.h"
Makefile.am: Add makedepend. libcpp: * Makefile.am: Add makedepend. * Makefile.in, aclocal.m4: Regenerate. * charset.c: Insert a space to avoid a warning. * directives.c: Include mkdeps.h. (_cpp_handle_directive): Reenable macro expander if appropriate. (undefine_macros): Inline body of _cpp_free_definition for speed. Do not call undef callback or _cpp_warn_if_unused_macro. (cpp_get_deps): New interface. * files.c (search_cache): Add pfile argument. Check for file that would be found by "" or <> search here... (_cpp_find_file): ...not here. Correct recorded start_dir of files found by directory-of-current-file search that would be found by "" or <> search. * init.c (cpp_add_dependency_target): Delete. * internal.h (struct lexer_state): Add discarding_output flag. * lex.c (lex_identifier): Compute hash function while scanning. * macro.c (cpp_scan_nooutput): Disable macro expansion outside directives. * makedepend.c: New file. * mkdeps.c (struct deps): Add vpath vector. (apply_vpath, deps_add_vpath): New function. (deps_free): Free vpath vector. (deps_add_dep, deps_add_target): Use apply_vpath. * symtab.c (calc_hash): Use HT_HASHSTEP and HT_FINISH. (ht_lookup_with_hash): New function. * cpplib.h, mkdeps.h: Update prototypes. * symtab.h: Update prototypes. (HT_HASHSTEP, HT_FINISH): New macros. gcc: * Makefile.in (MKDEPS_H): New shorthand. (c-opts.o): Update dependencies. * c-opts.c: Include mkdeps.h. (handle_deferred_opts): Use cpp_get_deps and deps_add_target, not cpp_add_dependency_target. From-SVN: r82654
2004-06-05 22:58:06 +02:00
const char *progname;
const char *vpath;
static const char *output_file;
static bool had_errors;
/* Option lists, to give to cpplib before each input file. */
struct cmd_line_macro
{
struct cmd_line_macro *next;
bool is_undef;
const char *macro;
};
static struct cmd_line_macro *cmd_line_macros;
static cpp_dir *cmd_line_searchpath;
static void
add_clm (const char *macro, bool is_undef)
{
configure.ac: Check declarations for asprintf and vasprintf. * configure.ac: Check declarations for asprintf and vasprintf. * config.in: Regenerate. * configure: Likewise. * charset.c (conversion_loop): Use XRESIZEVEC. (convert_no_conversion): Likewise. (convert_using_iconv): Likewise. (init_iconv_desc): Cast return value of alloca. (cpp_host_to_exec_charset): Use XNEWVEC. (emit_numeric_escape): Use XRESIZEVEC. (cpp_interpret_string): Use XNEWVEC. (cpp_interpret_string): Use XRESIZEVEC. (_cpp_interpret_identifier): Cast return value of alloca. (_cpp_convert_input): Use XNEWVEC and XRESIZEVEC. * directives.c (glue_header_name): Use XNEWVEC and XRESIZEVEC. (parse_include): Use XNEWVEC. (insert_pragma_entry): Rename local variable "new" to "new_entry". (save_registered_pragmas): Cast return value of xmemdup. (destringize_and_run): Same for alloca. (parse_assertion): Likewise. (do_assert): Cast allocated storage to proper type. (cpp_define): Likewise. (_cpp_define_builtin): Likewise. (cpp_undef): Likewise. (handle_assertion): Likewise. (cpp_push_buffer): Rename local variable "new" to "new_buffer". * expr.c (CPP_UPLUS): Cast value to type cpp_ttype. (CPP_UMINUS): Likewise. (struct cpp_operator): Rename from struct operator. (_cpp_expand_op_stack): Use XRESIZEVEC. * files.c (pch_open_file): Use XNEWVEC. (pch_open_file): Use XRESIZEVEC. (read_file_guts): Use XNEWVEC and XRESIZEVEC. (dir_name_of_file): Use XNEWVEC. (make_cpp_file): Use XCNEW. (make_cpp_dir): Likewise. (allocate_file_hash_entries): USE XNEWVEC. (cpp_included): Cast return value of htab_find_with_hash. (append_file_to_dir): Use XNEWVEC. (read_filename_string): Likewise. Use XRESIZEVEC too. (read_name_map): Cast return value of alloca. Use XRESIZEVEC. (remap_filename): Use XNEWVEC. (struct pchf_entry): Move definition out of struct pchf_data. (_cpp_save_file_entries): Use XCNEWVAR. (_cpp_read_file_entries): Use XNEWVAR. * identifiers.c (alloc_node): Use XOBNEW. * init.c (cpp_create_reader): Use XCNEW. (cpp_init_builtins): Cast of b->value to enum builtin_type. (read_original_directory): Cast return value of alloca. * lex.c (add_line_note): Use XRESIZEVEC. (warn_about_normalization): Use XNEWVEC. (_cpp_lex_direct): Cast node->directive_index to (enum cpp_ttype). (new_buff): Use XNEWVEC. * line-map.c (linemap_add): Use XRESIZEVEC. * macro.c (builtin_macro): Cast return value of alloca. (paste_tokens): Likewise. (expand_arg): Use XNEWVEC and XRESIZEVEC. (_cpp_save_parameter): Use XRESIZEVEC. (create_iso_definition): Cast allocated storage to proper type. (_cpp_create_definition): Likewise. (cpp_macro_definition): Use XRESIZEVEC. * makedepend.c (add_clm): Use XNEW. (add_dir): Likewise. * mkdeps.c (munge): Use XNEWVEC. (deps_init): Use XCNEW. (deps_add_target): Use XRESIZEVEC. (deps_add_default_target): Cast return value of alloca. (deps_add_dep): Use XRESIZEVEC. (deps_add_vpath): Likewise. Use XNEWVEC too. (deps_restore): Likewise. * pch.c (save_idents): Use XNEW and XNEWVEC. (cpp_save_state): Use XNEW. (count_defs): Cast return value of htab_find. (write_defs): Likewise. (cpp_write_pch_deps): Use XNEWVEC. (collect_ht_nodes): Use XRESIZEVEC. (cpp_valid_state): Use XNEWVEC. (save_macros): Use XRESIZEVEC. Cast return value of xmemdup. * symtab.c (ht_create): Use XCNEW. (ht_lookup_with_hash): Cast return value of obstack_copy0. (ht_expand): Use XCNEWVEC. * system.h (HAVE_DESIGNATED_INITIALIZERS): False if __cplusplus. (bool): Do not define if __cplusplus. From-SVN: r100295
2005-05-28 17:52:48 +02:00
struct cmd_line_macro *clm = XNEW (struct cmd_line_macro);
Makefile.am: Add makedepend. libcpp: * Makefile.am: Add makedepend. * Makefile.in, aclocal.m4: Regenerate. * charset.c: Insert a space to avoid a warning. * directives.c: Include mkdeps.h. (_cpp_handle_directive): Reenable macro expander if appropriate. (undefine_macros): Inline body of _cpp_free_definition for speed. Do not call undef callback or _cpp_warn_if_unused_macro. (cpp_get_deps): New interface. * files.c (search_cache): Add pfile argument. Check for file that would be found by "" or <> search here... (_cpp_find_file): ...not here. Correct recorded start_dir of files found by directory-of-current-file search that would be found by "" or <> search. * init.c (cpp_add_dependency_target): Delete. * internal.h (struct lexer_state): Add discarding_output flag. * lex.c (lex_identifier): Compute hash function while scanning. * macro.c (cpp_scan_nooutput): Disable macro expansion outside directives. * makedepend.c: New file. * mkdeps.c (struct deps): Add vpath vector. (apply_vpath, deps_add_vpath): New function. (deps_free): Free vpath vector. (deps_add_dep, deps_add_target): Use apply_vpath. * symtab.c (calc_hash): Use HT_HASHSTEP and HT_FINISH. (ht_lookup_with_hash): New function. * cpplib.h, mkdeps.h: Update prototypes. * symtab.h: Update prototypes. (HT_HASHSTEP, HT_FINISH): New macros. gcc: * Makefile.in (MKDEPS_H): New shorthand. (c-opts.o): Update dependencies. * c-opts.c: Include mkdeps.h. (handle_deferred_opts): Use cpp_get_deps and deps_add_target, not cpp_add_dependency_target. From-SVN: r82654
2004-06-05 22:58:06 +02:00
clm->next = cmd_line_macros;
clm->is_undef = is_undef;
clm->macro = macro;
cmd_line_macros = clm;
}
static void
add_dir (char *name, bool sysp)
{
configure.ac: Check declarations for asprintf and vasprintf. * configure.ac: Check declarations for asprintf and vasprintf. * config.in: Regenerate. * configure: Likewise. * charset.c (conversion_loop): Use XRESIZEVEC. (convert_no_conversion): Likewise. (convert_using_iconv): Likewise. (init_iconv_desc): Cast return value of alloca. (cpp_host_to_exec_charset): Use XNEWVEC. (emit_numeric_escape): Use XRESIZEVEC. (cpp_interpret_string): Use XNEWVEC. (cpp_interpret_string): Use XRESIZEVEC. (_cpp_interpret_identifier): Cast return value of alloca. (_cpp_convert_input): Use XNEWVEC and XRESIZEVEC. * directives.c (glue_header_name): Use XNEWVEC and XRESIZEVEC. (parse_include): Use XNEWVEC. (insert_pragma_entry): Rename local variable "new" to "new_entry". (save_registered_pragmas): Cast return value of xmemdup. (destringize_and_run): Same for alloca. (parse_assertion): Likewise. (do_assert): Cast allocated storage to proper type. (cpp_define): Likewise. (_cpp_define_builtin): Likewise. (cpp_undef): Likewise. (handle_assertion): Likewise. (cpp_push_buffer): Rename local variable "new" to "new_buffer". * expr.c (CPP_UPLUS): Cast value to type cpp_ttype. (CPP_UMINUS): Likewise. (struct cpp_operator): Rename from struct operator. (_cpp_expand_op_stack): Use XRESIZEVEC. * files.c (pch_open_file): Use XNEWVEC. (pch_open_file): Use XRESIZEVEC. (read_file_guts): Use XNEWVEC and XRESIZEVEC. (dir_name_of_file): Use XNEWVEC. (make_cpp_file): Use XCNEW. (make_cpp_dir): Likewise. (allocate_file_hash_entries): USE XNEWVEC. (cpp_included): Cast return value of htab_find_with_hash. (append_file_to_dir): Use XNEWVEC. (read_filename_string): Likewise. Use XRESIZEVEC too. (read_name_map): Cast return value of alloca. Use XRESIZEVEC. (remap_filename): Use XNEWVEC. (struct pchf_entry): Move definition out of struct pchf_data. (_cpp_save_file_entries): Use XCNEWVAR. (_cpp_read_file_entries): Use XNEWVAR. * identifiers.c (alloc_node): Use XOBNEW. * init.c (cpp_create_reader): Use XCNEW. (cpp_init_builtins): Cast of b->value to enum builtin_type. (read_original_directory): Cast return value of alloca. * lex.c (add_line_note): Use XRESIZEVEC. (warn_about_normalization): Use XNEWVEC. (_cpp_lex_direct): Cast node->directive_index to (enum cpp_ttype). (new_buff): Use XNEWVEC. * line-map.c (linemap_add): Use XRESIZEVEC. * macro.c (builtin_macro): Cast return value of alloca. (paste_tokens): Likewise. (expand_arg): Use XNEWVEC and XRESIZEVEC. (_cpp_save_parameter): Use XRESIZEVEC. (create_iso_definition): Cast allocated storage to proper type. (_cpp_create_definition): Likewise. (cpp_macro_definition): Use XRESIZEVEC. * makedepend.c (add_clm): Use XNEW. (add_dir): Likewise. * mkdeps.c (munge): Use XNEWVEC. (deps_init): Use XCNEW. (deps_add_target): Use XRESIZEVEC. (deps_add_default_target): Cast return value of alloca. (deps_add_dep): Use XRESIZEVEC. (deps_add_vpath): Likewise. Use XNEWVEC too. (deps_restore): Likewise. * pch.c (save_idents): Use XNEW and XNEWVEC. (cpp_save_state): Use XNEW. (count_defs): Cast return value of htab_find. (write_defs): Likewise. (cpp_write_pch_deps): Use XNEWVEC. (collect_ht_nodes): Use XRESIZEVEC. (cpp_valid_state): Use XNEWVEC. (save_macros): Use XRESIZEVEC. Cast return value of xmemdup. * symtab.c (ht_create): Use XCNEW. (ht_lookup_with_hash): Cast return value of obstack_copy0. (ht_expand): Use XCNEWVEC. * system.h (HAVE_DESIGNATED_INITIALIZERS): False if __cplusplus. (bool): Do not define if __cplusplus. From-SVN: r100295
2005-05-28 17:52:48 +02:00
cpp_dir *dir = XNEW (cpp_dir);
Makefile.am: Add makedepend. libcpp: * Makefile.am: Add makedepend. * Makefile.in, aclocal.m4: Regenerate. * charset.c: Insert a space to avoid a warning. * directives.c: Include mkdeps.h. (_cpp_handle_directive): Reenable macro expander if appropriate. (undefine_macros): Inline body of _cpp_free_definition for speed. Do not call undef callback or _cpp_warn_if_unused_macro. (cpp_get_deps): New interface. * files.c (search_cache): Add pfile argument. Check for file that would be found by "" or <> search here... (_cpp_find_file): ...not here. Correct recorded start_dir of files found by directory-of-current-file search that would be found by "" or <> search. * init.c (cpp_add_dependency_target): Delete. * internal.h (struct lexer_state): Add discarding_output flag. * lex.c (lex_identifier): Compute hash function while scanning. * macro.c (cpp_scan_nooutput): Disable macro expansion outside directives. * makedepend.c: New file. * mkdeps.c (struct deps): Add vpath vector. (apply_vpath, deps_add_vpath): New function. (deps_free): Free vpath vector. (deps_add_dep, deps_add_target): Use apply_vpath. * symtab.c (calc_hash): Use HT_HASHSTEP and HT_FINISH. (ht_lookup_with_hash): New function. * cpplib.h, mkdeps.h: Update prototypes. * symtab.h: Update prototypes. (HT_HASHSTEP, HT_FINISH): New macros. gcc: * Makefile.in (MKDEPS_H): New shorthand. (c-opts.o): Update dependencies. * c-opts.c: Include mkdeps.h. (handle_deferred_opts): Use cpp_get_deps and deps_add_target, not cpp_add_dependency_target. From-SVN: r82654
2004-06-05 22:58:06 +02:00
dir->next = cmd_line_searchpath;
dir->name = name;
dir->sysp = sysp;
dir->construct = 0;
dir->user_supplied_p = 1;
cmd_line_searchpath = dir;
}
/* Command line processing. */
static void ATTRIBUTE_NORETURN
usage (int errcode)
{
fprintf (stderr,
"usage: %s [-vh] [-V vpath] [-Dname[=def]...] [-Uname] [-Idir...] [-o file] sources...\n",
progname);
exit (errcode);
}
static int
parse_options (int argc, char **argv)
{
static const struct option longopts[] = {
{ "--help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
for (;;)
switch (getopt_long (argc, argv, "hD:U:I:J:o:V:", longopts, 0))
{
case 'h': usage (0);
case 'D': add_clm (optarg, false); break;
case 'U': add_clm (optarg, true); break;
case 'I': add_dir (optarg, false); break;
case 'J': add_dir (optarg, true); break;
case 'o':
if (output_file)
{
fprintf (stderr, "%s: too many output files\n", progname);
usage (2);
}
output_file = optarg;
break;
case 'V':
if (vpath)
{
fprintf (stderr, "%s: too many vpaths\n", progname);
usage (2);
}
vpath = optarg;
break;
case '?':
usage (2); /* getopt has issued the error message. */
case -1: /* end of options */
if (optind == argc)
{
fprintf (stderr, "%s: no input files\n", progname);
usage (2);
}
return optind;
Makefile.am: Add makedepend. libcpp: * Makefile.am: Add makedepend. * Makefile.in, aclocal.m4: Regenerate. * charset.c: Insert a space to avoid a warning. * directives.c: Include mkdeps.h. (_cpp_handle_directive): Reenable macro expander if appropriate. (undefine_macros): Inline body of _cpp_free_definition for speed. Do not call undef callback or _cpp_warn_if_unused_macro. (cpp_get_deps): New interface. * files.c (search_cache): Add pfile argument. Check for file that would be found by "" or <> search here... (_cpp_find_file): ...not here. Correct recorded start_dir of files found by directory-of-current-file search that would be found by "" or <> search. * init.c (cpp_add_dependency_target): Delete. * internal.h (struct lexer_state): Add discarding_output flag. * lex.c (lex_identifier): Compute hash function while scanning. * macro.c (cpp_scan_nooutput): Disable macro expansion outside directives. * makedepend.c: New file. * mkdeps.c (struct deps): Add vpath vector. (apply_vpath, deps_add_vpath): New function. (deps_free): Free vpath vector. (deps_add_dep, deps_add_target): Use apply_vpath. * symtab.c (calc_hash): Use HT_HASHSTEP and HT_FINISH. (ht_lookup_with_hash): New function. * cpplib.h, mkdeps.h: Update prototypes. * symtab.h: Update prototypes. (HT_HASHSTEP, HT_FINISH): New macros. gcc: * Makefile.in (MKDEPS_H): New shorthand. (c-opts.o): Update dependencies. * c-opts.c: Include mkdeps.h. (handle_deferred_opts): Use cpp_get_deps and deps_add_target, not cpp_add_dependency_target. From-SVN: r82654
2004-06-05 22:58:06 +02:00
default:
abort ();
}
}
/* Set up cpplib from command line options. */
static cpp_reader *
reader_init (struct line_maps *line_table)
{
cpp_reader *reader;
cpp_options *options;
linemap_init (line_table);
reader = cpp_create_reader (CLK_GNUC89, 0, line_table);
/* Ignore warnings and errors (we don't have access to system
headers). Request dependency output. */
options = cpp_get_options (reader);
options->inhibit_warnings = 1;
options->inhibit_errors = 1;
options->deps.style = DEPS_USER;
/* Further initialization. */
cpp_post_options (reader);
cpp_init_iconv (reader);
cpp_set_include_chains (reader, cmd_line_searchpath, cmd_line_searchpath,
false);
if (vpath)
{
struct deps *deps = cpp_get_deps (reader);
deps_add_vpath (deps, vpath);
}
return reader;
}
/* Process one input source file. */
static void
process_file (const char *file)
{
struct line_maps line_table;
cpp_reader *reader = reader_init (&line_table);
if (!cpp_read_main_file (reader, file))
had_errors = true;
else
{
struct cmd_line_macro *clm;
cpp_init_builtins (reader, true);
for (clm = cmd_line_macros; clm; clm = clm->next)
(clm->is_undef ? cpp_undef : cpp_define) (reader, clm->macro);
cpp_scan_nooutput (reader);
if (cpp_finish (reader, stdout))
had_errors = true;
}
cpp_destroy (reader);
linemap_free (&line_table);
}
/* Master control. */
int
main(int argc, char **argv)
{
int first_input, i;
progname = argv[0];
xmalloc_set_program_name (progname);
first_input = parse_options (argc, argv);
if (output_file)
if (!freopen (output_file, "w", stdout))
{
perror (output_file);
return 1;
}
for (i = first_input; i < argc; i++)
process_file (argv[i]);
return had_errors;
}