1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Neil Booth.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "coretypes.h"
33 #include "langhooks.h"
34 #include "tree-inline.h"
35 #include "diagnostic.h"
37 #include "cppdefault.h"
38 #include "c-incpath.h"
39 #include "debug.h" /* For debug_hooks. */
43 #ifndef DOLLARS_IN_IDENTIFIERS
44 # define DOLLARS_IN_IDENTIFIERS true
47 #ifndef TARGET_SYSTEM_ROOT
48 # define TARGET_SYSTEM_ROOT NULL
51 static int saved_lineno;
54 static cpp_options *cpp_opts;
57 static const char *this_input_filename;
59 /* Filename and stream for preprocessed output. */
60 static const char *out_fname;
61 static FILE *out_stream;
63 /* Append dependencies to deps_file. */
64 static bool deps_append;
66 /* If dependency switches (-MF etc.) have been given. */
67 static bool deps_seen;
72 /* Dependency output file. */
73 static const char *deps_file;
75 /* The prefix given by -iprefix, if any. */
76 static const char *iprefix;
78 /* The system root, if any. Overridden by -isysroot. */
79 static const char *sysroot = TARGET_SYSTEM_ROOT;
81 /* Zero disables all standard directories for headers. */
82 static bool std_inc = true;
84 /* Zero disables the C++-specific standard directories for headers. */
85 static bool std_cxx_inc = true;
87 /* If the quote chain has been split by -I-. */
88 static bool quote_chain_split;
90 /* If -Wunused-macros. */
91 static bool warn_unused_macros;
93 /* Number of deferred options. */
94 static size_t deferred_count;
96 /* Number of deferred options scanned for -include. */
97 static size_t include_cursor;
99 /* Permit Fotran front-end options. */
100 static bool permit_fortran_options;
102 static void set_Wimplicit (int);
103 static void handle_OPT_d (const char *);
104 static void set_std_cxx98 (int);
105 static void set_std_c89 (int, int);
106 static void set_std_c99 (int);
107 static void check_deps_environment_vars (void);
108 static void handle_deferred_opts (void);
109 static void sanitize_cpp_opts (void);
110 static void add_prefixed_path (const char *, size_t);
111 static void push_command_line_include (void);
112 static void cb_file_change (cpp_reader *, const struct line_map *);
113 static void cb_dir_change (cpp_reader *, const char *);
114 static void finish_options (void);
116 #ifndef STDC_0_IN_SYSTEM_HEADERS
117 #define STDC_0_IN_SYSTEM_HEADERS 0
120 /* Holds switches parsed by c_common_handle_option (), but whose
121 handling is deferred to c_common_post_options (). */
122 static void defer_opt (enum opt_code, const char *);
123 static struct deferred_opt
129 /* Complain that switch CODE expects an argument but none was
130 provided. OPT was the command-line option. Return FALSE to get
131 the default message in opts.c, TRUE if we provide a specialized
134 c_common_missing_argument (const char *opt, size_t code)
139 /* Pick up the default message. */
142 case OPT_fconstant_string_class_:
143 error ("no class name specified with \"%s\"", opt);
147 error ("assertion missing after \"%s\"", opt);
152 error ("macro name missing after \"%s\"", opt);
159 error ("missing path after \"%s\"", opt);
168 error ("missing filename after \"%s\"", opt);
173 error ("missing makefile target after \"%s\"", opt);
180 /* Defer option CODE with argument ARG. */
182 defer_opt (enum opt_code code, const char *arg)
184 deferred_opts[deferred_count].code = code;
185 deferred_opts[deferred_count].arg = arg;
189 /* Common initialization before parsing options. */
191 c_common_init_options (unsigned int argc, const char **argv)
193 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
194 unsigned int i, result;
196 /* This is conditionalized only because that is the way the front
197 ends used to do it. Maybe this should be unconditional? */
198 if (c_dialect_cxx ())
200 /* By default wrap lines at 80 characters. Is getenv
201 ("COLUMNS") preferable? */
202 diagnostic_line_cutoff (global_dc) = 80;
203 /* By default, emit location information once for every
204 diagnostic message. */
205 diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
208 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
211 cpp_opts = cpp_get_options (parse_in);
212 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
213 cpp_opts->objc = c_dialect_objc ();
215 /* Reset to avoid warnings on internal definitions. We set it just
216 before passing on command-line options to cpplib. */
217 cpp_opts->warn_dollars = 0;
219 flag_const_strings = c_dialect_cxx ();
220 flag_exceptions = c_dialect_cxx ();
221 warn_pointer_arith = c_dialect_cxx ();
223 deferred_opts = xmalloc (argc * sizeof (struct deferred_opt));
225 result = lang_flags[c_language];
227 if (c_language == clk_c)
229 for (i = 1; i < argc; i++)
231 /* If preprocessing assembly language, accept any of the C-family
232 front end options since the driver may pass them through. */
233 if (! strcmp (argv[i], "-lang-asm"))
234 result |= CL_C | CL_ObjC | CL_CXX | CL_ObjCXX;
236 /* If potentially preprocessing Fortran we have to accept its
237 front end options since the driver may them through. */
238 else if (! strcmp (argv[i], "-traditional-cpp"))
240 permit_fortran_options = true;
249 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
250 form of an -f or -W option was given. Returns 0 if the switch was
251 invalid, a negative number to prevent language-independent
252 processing in toplev.c (a hack necessary for the short-term). */
254 c_common_handle_option (size_t scode, const char *arg, int value)
256 const struct cl_option *option = &cl_options[scode];
257 enum opt_code code = (enum opt_code) scode;
263 result = permit_fortran_options;
266 case OPT__output_pch_:
271 defer_opt (code, arg);
275 cpp_opts->discard_comments = 0;
279 cpp_opts->discard_comments = 0;
280 cpp_opts->discard_comments_in_macro_exp = 0;
284 defer_opt (code, arg);
288 flag_preprocess_only = 1;
292 cpp_opts->print_include_names = 1;
296 if (strcmp (arg, "-"))
297 add_path (xstrdup (arg), BRACKET, 0);
300 if (quote_chain_split)
301 error ("-I- specified twice");
302 quote_chain_split = true;
303 split_quote_chain ();
309 /* When doing dependencies with -M or -MM, suppress normal
310 preprocessed output, but still do -dM etc. as software
311 depends on this. Preprocessed output does occur if -MD, -MMD
312 or environment var dependency generation is used. */
313 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
315 cpp_opts->inhibit_warnings = 1;
320 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
331 cpp_opts->deps.missing_files = true;
336 cpp_opts->deps.phony_targets = true;
342 defer_opt (code, arg);
346 flag_no_line_commands = 1;
349 case OPT_fworking_directory:
350 flag_working_directory = value;
354 defer_opt (code, arg);
364 set_Wimplicit (value);
365 warn_char_subscripts = value;
366 warn_missing_braces = value;
367 warn_parentheses = value;
368 warn_return_type = value;
369 warn_sequence_point = value; /* Was C only. */
370 if (c_dialect_cxx ())
371 warn_sign_compare = value;
373 warn_strict_aliasing = value;
375 /* Only warn about unknown pragmas that are not in system
377 warn_unknown_pragmas = value;
379 /* We save the value of warn_uninitialized, since if they put
380 -Wuninitialized on the command line, we need to generate a
381 warning about not using it without also specifying -O. */
382 if (warn_uninitialized != 1)
383 warn_uninitialized = (value ? 2 : 0);
385 if (!c_dialect_cxx ())
386 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
387 can turn it off only if it's not explicit. */
388 warn_main = value * 2;
391 /* C++-specific warnings. */
392 warn_nonvdtor = value;
393 warn_reorder = value;
394 warn_nontemplate_friend = value;
397 cpp_opts->warn_trigraphs = value;
398 cpp_opts->warn_comments = value;
399 cpp_opts->warn_num_sign_change = value;
400 cpp_opts->warn_multichar = value; /* Was C++ only. */
403 case OPT_Wbad_function_cast:
404 warn_bad_function_cast = value;
408 warn_cast_qual = value;
411 case OPT_Wchar_subscripts:
412 warn_char_subscripts = value;
417 cpp_opts->warn_comments = value;
420 case OPT_Wconversion:
421 warn_conversion = value;
424 case OPT_Wctor_dtor_privacy:
425 warn_ctor_dtor_privacy = value;
428 case OPT_Wdeclaration_after_statement:
429 warn_declaration_after_statement = value;
432 case OPT_Wdeprecated:
433 warn_deprecated = value;
434 cpp_opts->warn_deprecated = value;
437 case OPT_Wdiv_by_zero:
438 warn_div_by_zero = value;
445 case OPT_Wendif_labels:
446 cpp_opts->warn_endif_labels = value;
450 cpp_opts->warnings_are_errors = value;
453 case OPT_Werror_implicit_function_declaration:
454 mesg_implicit_function_declaration = 2;
457 case OPT_Wfloat_equal:
458 warn_float_equal = value;
466 set_Wformat (atoi (arg));
469 case OPT_Wformat_extra_args:
470 warn_format_extra_args = value;
473 case OPT_Wformat_nonliteral:
474 warn_format_nonliteral = value;
477 case OPT_Wformat_security:
478 warn_format_security = value;
481 case OPT_Wformat_y2k:
482 warn_format_y2k = value;
485 case OPT_Wformat_zero_length:
486 warn_format_zero_length = value;
490 warn_init_self = value;
494 set_Wimplicit (value);
497 case OPT_Wimplicit_function_declaration:
498 mesg_implicit_function_declaration = value;
501 case OPT_Wimplicit_int:
502 warn_implicit_int = value;
506 /* Silently ignore for now. */
509 case OPT_Winvalid_offsetof:
510 warn_invalid_offsetof = value;
513 case OPT_Winvalid_pch:
514 cpp_opts->warn_invalid_pch = value;
518 warn_long_long = value;
528 case OPT_Wmissing_braces:
529 warn_missing_braces = value;
532 case OPT_Wmissing_declarations:
533 warn_missing_declarations = value;
536 case OPT_Wmissing_format_attribute:
537 warn_missing_format_attribute = value;
540 case OPT_Wmissing_prototypes:
541 warn_missing_prototypes = value;
545 cpp_opts->warn_multichar = value;
548 case OPT_Wnested_externs:
549 warn_nested_externs = value;
552 case OPT_Wnon_template_friend:
553 warn_nontemplate_friend = value;
556 case OPT_Wnon_virtual_dtor:
557 warn_nonvdtor = value;
561 warn_nonnull = value;
564 case OPT_Wold_style_definition:
565 warn_old_style_definition = value;
568 case OPT_Wold_style_cast:
569 warn_old_style_cast = value;
572 case OPT_Woverloaded_virtual:
573 warn_overloaded_virtual = value;
576 case OPT_Wparentheses:
577 warn_parentheses = value;
580 case OPT_Wpmf_conversions:
581 warn_pmf2ptr = value;
584 case OPT_Wpointer_arith:
585 warn_pointer_arith = value;
589 warn_protocol = value;
593 warn_selector = value;
596 case OPT_Wredundant_decls:
597 warn_redundant_decls = value;
601 warn_reorder = value;
604 case OPT_Wreturn_type:
605 warn_return_type = value;
608 case OPT_Wsequence_point:
609 warn_sequence_point = value;
612 case OPT_Wsign_compare:
613 warn_sign_compare = value;
616 case OPT_Wsign_promo:
617 warn_sign_promo = value;
620 case OPT_Wstrict_prototypes:
621 warn_strict_prototypes = value;
628 case OPT_Wsystem_headers:
629 cpp_opts->warn_system_headers = value;
632 case OPT_Wtraditional:
633 warn_traditional = value;
634 cpp_opts->warn_traditional = value;
638 cpp_opts->warn_trigraphs = value;
641 case OPT_Wundeclared_selector:
642 warn_undeclared_selector = value;
646 cpp_opts->warn_undef = value;
649 case OPT_Wunknown_pragmas:
650 /* Set to greater than 1, so that even unknown pragmas in
651 system headers will be warned about. */
652 warn_unknown_pragmas = value * 2;
655 case OPT_Wunused_macros:
656 warn_unused_macros = value;
659 case OPT_Wwrite_strings:
660 if (!c_dialect_cxx ())
661 flag_const_strings = value;
663 warn_write_strings = value;
667 if (!c_dialect_cxx ())
668 set_std_c89 (false, true);
670 set_std_cxx98 (true);
677 case OPT_fcond_mismatch:
678 if (!c_dialect_cxx ())
680 flag_cond_mismatch = value;
685 case OPT_fall_virtual:
686 case OPT_falt_external_templates:
687 case OPT_fenum_int_equiv:
688 case OPT_fexternal_templates:
689 case OPT_fguiding_decls:
691 case OPT_fhuge_objects:
693 case OPT_fname_mangling_version_:
695 case OPT_fnonnull_objects:
697 case OPT_fstrict_prototype:
698 case OPT_fthis_is_variable:
699 case OPT_fvtable_thunks:
702 warning ("switch \"%s\" is no longer supported", option->opt_text);
705 case OPT_faccess_control:
706 flag_access_control = value;
710 flag_no_asm = !value;
714 flag_no_builtin = !value;
721 disable_builtin_function (arg);
724 case OPT_fdollars_in_identifiers:
725 cpp_opts->dollars_in_ident = value;
729 if (!dump_switch_p (arg))
733 case OPT_ffreestanding:
735 /* Fall through.... */
738 flag_no_builtin = !value;
739 /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
740 if (!value && warn_main == 2)
744 case OPT_fshort_double:
745 flag_short_double = value;
748 case OPT_fshort_enums:
749 flag_short_enums = value;
752 case OPT_fshort_wchar:
753 flag_short_wchar = value;
756 case OPT_fsigned_bitfields:
757 flag_signed_bitfields = value;
758 explicit_flag_signed_bitfields = 1;
761 case OPT_fsigned_char:
762 flag_signed_char = value;
765 case OPT_funsigned_bitfields:
766 flag_signed_bitfields = !value;
767 explicit_flag_signed_bitfields = 1;
770 case OPT_funsigned_char:
771 flag_signed_char = !value;
775 flag_check_new = value;
778 case OPT_fconserve_space:
779 flag_conserve_space = value;
782 case OPT_fconst_strings:
783 flag_const_strings = value;
786 case OPT_fconstant_string_class_:
787 constant_string_class_name = arg;
790 case OPT_fdefault_inline:
791 flag_default_inline = value;
794 case OPT_felide_constructors:
795 flag_elide_constructors = value;
798 case OPT_fenforce_eh_specs:
799 flag_enforce_eh_specs = value;
802 case OPT_ffixed_form:
803 case OPT_ffixed_line_length_:
804 /* Fortran front end options ignored when preprocessing only. */
805 if (!flag_preprocess_only)
810 flag_new_for_scope = value;
813 case OPT_fgnu_keywords:
814 flag_no_gnu_keywords = !value;
817 case OPT_fgnu_runtime:
818 flag_next_runtime = !value;
821 case OPT_fhandle_exceptions:
822 warning ("-fhandle-exceptions has been renamed -fexceptions (and is now on by default)");
823 flag_exceptions = value;
826 case OPT_fimplement_inlines:
827 flag_implement_inlines = value;
830 case OPT_fimplicit_inline_templates:
831 flag_implicit_inline_templates = value;
834 case OPT_fimplicit_templates:
835 flag_implicit_templates = value;
838 case OPT_fms_extensions:
839 flag_ms_extensions = value;
842 case OPT_fnext_runtime:
843 flag_next_runtime = value;
846 case OPT_fnil_receivers:
847 flag_nil_receivers = value;
850 case OPT_fnonansi_builtins:
851 flag_no_nonansi_builtin = !value;
854 case OPT_fobjc_exceptions:
855 flag_objc_exceptions = value;
858 case OPT_foperator_names:
859 cpp_opts->operator_names = value;
862 case OPT_foptional_diags:
863 flag_optional_diags = value;
867 cpp_opts->restore_pch_deps = value;
870 case OPT_fpermissive:
871 flag_permissive = value;
874 case OPT_fpreprocessed:
875 cpp_opts->preprocessed = value;
878 case OPT_freplace_objc_classes:
879 flag_replace_objc_classes = value;
883 flag_use_repository = value;
885 flag_implicit_templates = 0;
892 case OPT_fshow_column:
893 cpp_opts->show_column = value;
897 flag_detailed_statistics = value;
901 /* It is documented that we silently ignore silly values. */
902 if (value >= 1 && value <= 100)
903 cpp_opts->tabstop = value;
906 case OPT_fexec_charset_:
907 cpp_opts->narrow_charset = arg;
910 case OPT_fwide_exec_charset_:
911 cpp_opts->wide_charset = arg;
914 case OPT_finput_charset_:
915 cpp_opts->input_charset = arg;
918 case OPT_ftemplate_depth_:
919 max_tinst_depth = value;
922 case OPT_fuse_cxa_atexit:
923 flag_use_cxa_atexit = value;
931 flag_zero_link = value;
935 flag_gen_declaration = 1;
939 add_path (xstrdup (arg), AFTER, 0);
944 defer_opt (code, arg);
956 add_path (xstrdup (arg), SYSTEM, 0);
959 case OPT_iwithprefix:
960 add_prefixed_path (arg, SYSTEM);
963 case OPT_iwithprefixbefore:
964 add_prefixed_path (arg, BRACKET);
968 cpp_set_lang (parse_in, CLK_ASM);
969 cpp_opts->dollars_in_ident = false;
988 error ("output filename specified twice");
991 /* We need to handle the -pedantic switches here, rather than in
992 c_common_post_options, so that a subsequent -Wno-endif-labels
993 is not overridden. */
994 case OPT_pedantic_errors:
995 cpp_opts->pedantic_errors = 1;
998 cpp_opts->pedantic = 1;
999 cpp_opts->warn_endif_labels = 1;
1002 case OPT_print_objc_runtime_info:
1003 print_struct_values = 1;
1007 cpp_opts->remap = 1;
1011 case OPT_std_gnu__98:
1012 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
1016 case OPT_std_iso9899_1990:
1017 case OPT_std_iso9899_199409:
1018 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1022 set_std_c89 (false /* c94 */, false /* ISO */);
1027 case OPT_std_iso9899_1999:
1028 case OPT_std_iso9899_199x:
1029 set_std_c99 (true /* ISO */);
1034 set_std_c99 (false /* ISO */);
1038 cpp_opts->trigraphs = 1;
1041 case OPT_traditional_cpp:
1042 cpp_opts->traditional = 1;
1050 cpp_opts->inhibit_warnings = 1;
1057 case OPT_fformat_extensions:
1064 /* Post-switch processing. */
1066 c_common_post_options (const char **pfilename)
1068 struct cpp_callbacks *cb;
1070 /* Canonicalize the input and output filenames. */
1071 if (in_fnames == NULL)
1073 in_fnames = xmalloc (sizeof (in_fnames[0]));
1076 else if (strcmp (in_fnames[0], "-") == 0)
1079 if (out_fname == NULL || !strcmp (out_fname, "-"))
1082 if (cpp_opts->deps.style == DEPS_NONE)
1083 check_deps_environment_vars ();
1085 handle_deferred_opts ();
1087 sanitize_cpp_opts ();
1089 register_include_chains (parse_in, sysroot, iprefix,
1090 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
1092 flag_inline_trees = 1;
1094 /* Use tree inlining if possible. Function instrumentation is only
1095 done in the RTL level, so we disable tree inlining. */
1096 if (flag_instrument_function_entry_exit)
1099 flag_really_no_inline = 1;
1103 if (!flag_no_inline)
1105 if (flag_inline_functions)
1107 flag_inline_trees = 2;
1108 flag_inline_functions = 0;
1112 /* -Wextra implies -Wsign-compare, but not if explicitly
1114 if (warn_sign_compare == -1)
1115 warn_sign_compare = extra_warnings;
1117 /* Special format checking options don't work without -Wformat; warn if
1119 if (warn_format_y2k && !warn_format)
1120 warning ("-Wformat-y2k ignored without -Wformat");
1121 if (warn_format_extra_args && !warn_format)
1122 warning ("-Wformat-extra-args ignored without -Wformat");
1123 if (warn_format_zero_length && !warn_format)
1124 warning ("-Wformat-zero-length ignored without -Wformat");
1125 if (warn_format_nonliteral && !warn_format)
1126 warning ("-Wformat-nonliteral ignored without -Wformat");
1127 if (warn_format_security && !warn_format)
1128 warning ("-Wformat-security ignored without -Wformat");
1129 if (warn_missing_format_attribute && !warn_format)
1130 warning ("-Wmissing-format-attribute ignored without -Wformat");
1132 if (flag_preprocess_only)
1134 /* Open the output now. We must do so even if flag_no_output is
1135 on, because there may be other output than from the actual
1136 preprocessing (e.g. from -dM). */
1137 if (out_fname[0] == '\0')
1138 out_stream = stdout;
1140 out_stream = fopen (out_fname, "w");
1142 if (out_stream == NULL)
1144 fatal_error ("opening output file %s: %m", out_fname);
1148 if (num_in_fnames > 1)
1149 error ("too many filenames given. Type %s --help for usage",
1152 init_pp_output (out_stream);
1158 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1162 cb = cpp_get_callbacks (parse_in);
1163 cb->file_change = cb_file_change;
1164 cb->dir_change = cb_dir_change;
1165 cpp_post_options (parse_in);
1167 saved_lineno = input_line;
1170 /* If an error has occurred in cpplib, note it so we fail
1172 errorcount += cpp_errors (parse_in);
1174 *pfilename = this_input_filename
1175 = cpp_read_main_file (parse_in, in_fnames[0]);
1176 /* Don't do any compilation or preprocessing if there is no input file. */
1177 if (this_input_filename == NULL)
1183 if (flag_working_directory
1184 && flag_preprocess_only && ! flag_no_line_commands)
1185 pp_dir_change (parse_in, get_src_pwd ());
1187 return flag_preprocess_only;
1190 /* Front end initialization common to C, ObjC and C++. */
1192 c_common_init (void)
1194 input_line = saved_lineno;
1196 /* Set up preprocessor arithmetic. Must be done after call to
1197 c_common_nodes_and_builtins for type nodes to be good. */
1198 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1199 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1200 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1201 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1202 cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1203 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1205 /* This can't happen until after wchar_precision and bytes_big_endian
1207 cpp_init_iconv (parse_in);
1209 if (flag_preprocess_only)
1212 preprocess_file (parse_in);
1216 /* Has to wait until now so that cpplib has its hash table. */
1222 /* Initialize the integrated preprocessor after debug output has been
1223 initialized; loop over each input file. */
1225 c_common_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
1227 unsigned file_index;
1230 yydebug = set_yydebug;
1232 warning ("YYDEBUG not defined");
1241 /* Reset the state of the parser. */
1244 /* Reset cpplib's macros and start a new file. */
1245 cpp_undef_all (parse_in);
1246 main_input_filename = this_input_filename
1247 = cpp_read_main_file (parse_in, in_fnames[file_index]);
1248 if (this_input_filename == NULL)
1252 if (file_index == 0)
1257 } while (file_index < num_in_fnames);
1262 /* Common finish hook for the C, ObjC and C++ front ends. */
1264 c_common_finish (void)
1266 FILE *deps_stream = NULL;
1268 if (cpp_opts->deps.style != DEPS_NONE)
1270 /* If -M or -MM was seen without -MF, default output to the
1273 deps_stream = out_stream;
1276 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1278 fatal_error ("opening dependency file %s: %m", deps_file);
1282 /* For performance, avoid tearing down cpplib's internal structures
1283 with cpp_destroy (). */
1284 errorcount += cpp_finish (parse_in, deps_stream);
1286 if (deps_stream && deps_stream != out_stream
1287 && (ferror (deps_stream) || fclose (deps_stream)))
1288 fatal_error ("closing dependency file %s: %m", deps_file);
1290 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1291 fatal_error ("when writing output to %s: %m", out_fname);
1294 /* Either of two environment variables can specify output of
1295 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1296 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1297 and DEPS_TARGET is the target to mention in the deps. They also
1298 result in dependency information being appended to the output file
1299 rather than overwriting it, and like Sun's compiler
1300 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1302 check_deps_environment_vars (void)
1306 GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1308 cpp_opts->deps.style = DEPS_USER;
1311 GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1314 cpp_opts->deps.style = DEPS_SYSTEM;
1315 cpp_opts->deps.ignore_main_file = true;
1321 /* Find the space before the DEPS_TARGET, if there is one. */
1322 char *s = strchr (spec, ' ');
1325 /* Let the caller perform MAKE quoting. */
1326 defer_opt (OPT_MT, s + 1);
1330 /* Command line -MF overrides environment variables and default. */
1338 /* Handle deferred command line switches. */
1340 handle_deferred_opts (void)
1344 for (i = 0; i < deferred_count; i++)
1346 struct deferred_opt *opt = &deferred_opts[i];
1348 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1349 cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1353 /* These settings are appropriate for GCC, but not necessarily so for
1354 cpplib as a library. */
1356 sanitize_cpp_opts (void)
1358 /* If we don't know what style of dependencies to output, complain
1359 if any other dependency switches have been given. */
1360 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1361 error ("to generate dependencies you must specify either -M or -MM");
1363 /* -dM and dependencies suppress normal output; do it here so that
1364 the last -d[MDN] switch overrides earlier ones. */
1365 if (flag_dump_macros == 'M')
1368 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1369 -dM since at least glibc relies on -M -dM to work. */
1370 /* Also, flag_no_output implies flag_no_line_commands, always. */
1373 if (flag_dump_macros != 'M')
1374 flag_dump_macros = 0;
1375 flag_dump_includes = 0;
1376 flag_no_line_commands = 1;
1379 cpp_opts->unsigned_char = !flag_signed_char;
1380 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1382 /* We want -Wno-long-long to override -pedantic -std=non-c99
1383 and/or -Wtraditional, whatever the ordering. */
1384 cpp_opts->warn_long_long
1385 = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1387 /* If we're generating preprocessor output, emit current directory
1388 if explicitly requested or if debugging information is enabled.
1389 ??? Maybe we should only do it for debugging formats that
1390 actually output the current directory? */
1391 if (flag_working_directory == -1)
1392 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1395 /* Add include path with a prefix at the front of its name. */
1397 add_prefixed_path (const char *suffix, size_t chain)
1401 size_t prefix_len, suffix_len;
1403 suffix_len = strlen (suffix);
1404 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1405 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1407 path = xmalloc (prefix_len + suffix_len + 1);
1408 memcpy (path, prefix, prefix_len);
1409 memcpy (path + prefix_len, suffix, suffix_len);
1410 path[prefix_len + suffix_len] = '\0';
1412 add_path (path, chain, 0);
1415 /* Handle -D, -U, -A, -imacros, and the first -include. */
1417 finish_options (void)
1419 if (!cpp_opts->preprocessed)
1423 cpp_change_file (parse_in, LC_RENAME, _("<built-in>"));
1424 cpp_init_builtins (parse_in, flag_hosted);
1425 c_cpp_builtins (parse_in);
1427 /* We're about to send user input to cpplib, so make it warn for
1428 things that we previously (when we sent it internal definitions)
1429 told it to not warn.
1431 C99 permits implementation-defined characters in identifiers.
1432 The documented meaning of -std= is to turn off extensions that
1433 conflict with the specified standard, and since a strictly
1434 conforming program cannot contain a '$', we do not condition
1435 their acceptance on the -std= setting. */
1436 cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99);
1438 cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
1439 for (i = 0; i < deferred_count; i++)
1441 struct deferred_opt *opt = &deferred_opts[i];
1443 if (opt->code == OPT_D)
1444 cpp_define (parse_in, opt->arg);
1445 else if (opt->code == OPT_U)
1446 cpp_undef (parse_in, opt->arg);
1447 else if (opt->code == OPT_A)
1449 if (opt->arg[0] == '-')
1450 cpp_unassert (parse_in, opt->arg + 1);
1452 cpp_assert (parse_in, opt->arg);
1456 /* Handle -imacros after -D and -U. */
1457 for (i = 0; i < deferred_count; i++)
1459 struct deferred_opt *opt = &deferred_opts[i];
1461 if (opt->code == OPT_imacros
1462 && cpp_push_include (parse_in, opt->arg))
1464 /* Disable push_command_line_include callback for now. */
1465 include_cursor = deferred_count + 1;
1466 cpp_scan_nooutput (parse_in);
1472 push_command_line_include ();
1475 /* Give CPP the next file given by -include, if any. */
1477 push_command_line_include (void)
1479 while (include_cursor < deferred_count)
1481 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1483 if (! cpp_opts->preprocessed && opt->code == OPT_include
1484 && cpp_push_include (parse_in, opt->arg))
1488 if (include_cursor == deferred_count)
1491 /* -Wunused-macros should only warn about macros defined hereafter. */
1492 cpp_opts->warn_unused_macros = warn_unused_macros;
1493 /* Restore the line map from <command line>. */
1494 if (! cpp_opts->preprocessed)
1495 cpp_change_file (parse_in, LC_RENAME, main_input_filename);
1497 /* Set this here so the client can change the option if it wishes,
1498 and after stacking the main file so we don't trace the main file. */
1499 cpp_get_line_maps (parse_in)->trace_includes
1500 = cpp_opts->print_include_names;
1504 /* File change callback. Has to handle -include files. */
1506 cb_file_change (cpp_reader *pfile ATTRIBUTE_UNUSED,
1507 const struct line_map *new_map)
1509 if (flag_preprocess_only)
1510 pp_file_change (new_map);
1512 fe_file_change (new_map);
1514 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1515 push_command_line_include ();
1519 cb_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
1521 if (! set_src_pwd (dir))
1522 warning ("too late for # directive to set debug directory");
1525 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1526 extensions if ISO). There is no concept of gnu94. */
1528 set_std_c89 (int c94, int iso)
1530 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1533 flag_no_gnu_keywords = iso;
1534 flag_no_nonansi_builtin = iso;
1537 flag_writable_strings = 0;
1540 /* Set the C 99 standard (without GNU extensions if ISO). */
1542 set_std_c99 (int iso)
1544 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1546 flag_no_nonansi_builtin = iso;
1550 flag_writable_strings = 0;
1553 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1555 set_std_cxx98 (int iso)
1557 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1558 flag_no_gnu_keywords = iso;
1559 flag_no_nonansi_builtin = iso;
1563 /* Handle setting implicit to ON. */
1565 set_Wimplicit (int on)
1568 warn_implicit_int = on;
1571 if (mesg_implicit_function_declaration != 2)
1572 mesg_implicit_function_declaration = 1;
1575 mesg_implicit_function_declaration = 0;
1578 /* Args to -d specify what to dump. Silently ignore
1579 unrecognized options; they may be aimed at toplev.c. */
1581 handle_OPT_d (const char *arg)
1585 while ((c = *arg++) != '\0')
1588 case 'M': /* Dump macros only. */
1589 case 'N': /* Dump names. */
1590 case 'D': /* Dump definitions. */
1591 flag_dump_macros = c;
1595 flag_dump_includes = 1;