1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002, 2003 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
24 #include "coretypes.h"
31 #include "langhooks.h"
32 #include "tree-inline.h"
33 #include "diagnostic.h"
35 #include "cppdefault.h"
36 #include "c-incpath.h"
37 #include "debug.h" /* For debug_hooks. */
38 #include "c-options.h"
40 #ifndef DOLLARS_IN_IDENTIFIERS
41 # define DOLLARS_IN_IDENTIFIERS true
44 #ifndef TARGET_SYSTEM_ROOT
45 # define TARGET_SYSTEM_ROOT NULL
49 # define TARGET_EBCDIC 0
52 static int saved_lineno;
55 static cpp_options *cpp_opts;
58 static const char *in_fname;
60 /* Filename and stream for preprocessed output. */
61 static const char *out_fname;
62 static FILE *out_stream;
64 /* Append dependencies to deps_file. */
65 static bool deps_append;
67 /* If dependency switches (-MF etc.) have been given. */
68 static bool deps_seen;
73 /* Dependency output file. */
74 static const char *deps_file;
76 /* The prefix given by -iprefix, if any. */
77 static const char *iprefix;
79 /* The system root, if any. Overridden by -isysroot. */
80 static const char *sysroot = TARGET_SYSTEM_ROOT;
82 /* Zero disables all standard directories for headers. */
83 static bool std_inc = true;
85 /* Zero disables the C++-specific standard directories for headers. */
86 static bool std_cxx_inc = true;
88 /* If the quote chain has been split by -I-. */
89 static bool quote_chain_split;
91 /* If -Wunused-macros. */
92 static bool warn_unused_macros;
94 /* Number of deferred options, deferred options array size. */
95 static size_t deferred_count, deferred_size;
97 /* Number of deferred options scanned for -include. */
98 static size_t include_cursor;
100 static void missing_arg PARAMS ((size_t));
101 static size_t find_opt PARAMS ((const char *, int));
102 static void set_Wimplicit PARAMS ((int));
103 static void complain_wrong_lang PARAMS ((size_t));
104 static void write_langs PARAMS ((char *, int));
105 static void print_help PARAMS ((void));
106 static void handle_OPT_d PARAMS ((const char *));
107 static void set_std_cxx98 PARAMS ((int));
108 static void set_std_c89 PARAMS ((int, int));
109 static void set_std_c99 PARAMS ((int));
110 static void check_deps_environment_vars PARAMS ((void));
111 static void handle_deferred_opts PARAMS ((void));
112 static void sanitize_cpp_opts PARAMS ((void));
113 static void add_prefixed_path PARAMS ((const char *, size_t));
114 static void push_command_line_include PARAMS ((void));
115 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
116 static void finish_options PARAMS ((void));
118 #ifndef STDC_0_IN_SYSTEM_HEADERS
119 #define STDC_0_IN_SYSTEM_HEADERS 0
122 #define CL_C (1 << 0) /* Only C. */
123 #define CL_OBJC (1 << 1) /* Only ObjC. */
124 #define CL_CXX (1 << 2) /* Only C++. */
125 #define CL_OBJCXX (1 << 3) /* Only ObjC++. */
126 #define CL_JOINED (1 << 4) /* If takes joined argument. */
127 #define CL_SEPARATE (1 << 5) /* If takes a separate argument. */
129 #include "c-options.c"
131 /* If the user gives an option to a front end that doesn't support it,
132 an error is output, mentioning which front ends the option is valid
133 for. If you don't want this, you must accept it for all front
134 ends, and test for the front end in the option handler. See, for
135 example, the handling of -fcond-mismatch.
137 If you requested a joined or separate argument, it is stored in the
138 variable "arg", which is guaranteed to be non-NULL and to not be an
139 empty string. It points to the argument either within the argv[]
140 vector or within one of that vector's strings, and so the text is
141 permanent and copies need not be made. Be sure to add an error
142 message in missing_arg() if the default is not appropriate. */
144 /* Holds switches parsed by c_common_decode_option (), but whose
145 handling is deferred to c_common_post_options (). */
146 static void defer_opt PARAMS ((enum opt_code, const char *));
147 static struct deferred_opt
153 /* Complain that switch OPT_INDEX expects an argument but none was
156 missing_arg (opt_index)
159 const char *opt_text = cl_options[opt_index].opt_text;
163 case OPT__output_pch_:
166 case OPT_fabi_version_:
169 case OPT_fname_mangling_version_:
171 case OPT_ftemplate_depth_:
173 case OPT_iwithprefix:
174 case OPT_iwithprefixbefore:
176 error ("missing argument to \"-%s\"", opt_text);
179 case OPT_fconstant_string_class_:
180 error ("no class name specified with \"-%s\"", opt_text);
184 error ("assertion missing after \"-%s\"", opt_text);
189 error ("macro name missing after \"-%s\"", opt_text);
196 error ("missing path after \"-%s\"", opt_text);
205 error ("missing filename after \"-%s\"", opt_text);
210 error ("missing target after \"-%s\"", opt_text);
215 /* Perform a binary search to find which option the command-line INPUT
216 matches. Returns its index in the option array, and N_OPTS on
219 Complications arise since some options can be suffixed with an
220 argument, and multiple complete matches can occur, e.g. -pedantic
221 and -pedantic-errors. Also, some options are only accepted by some
222 languages. If a switch matches for a different language and
223 doesn't match any alternatives for the true front end, the index of
224 the matched switch is returned anyway. The caller should check for
227 find_opt (input, lang_flag)
233 size_t result = N_OPTS;
243 opt_len = cl_options[md].opt_len;
244 comp = strncmp (input, cl_options[md].opt_text, opt_len);
252 /* The switch matches. It it an exact match? */
253 if (input[opt_len] == '\0')
259 /* If the switch takes no arguments this is not a proper
260 match, so we continue the search (e.g. input="stdc++"
261 match was "stdc"). */
262 if (!(cl_options[md].flags & CL_JOINED))
265 /* Is this switch valid for this front end? */
266 if (!(cl_options[md].flags & lang_flag))
268 /* If subsequently we don't find a better match,
269 return this and let the caller report it as a bad
275 /* Two scenarios remain: we have the switch's argument,
276 or we match a longer option. This can happen with
277 -iwithprefix and -withprefixbefore. The longest
278 possible option match succeeds.
280 Scan forwards, and return an exact match. Otherwise
281 return the longest valid option-accepting match (mx).
282 This loops at most twice with current options. */
284 for (md = md + 1; md < (size_t) N_OPTS; md++)
286 opt_len = cl_options[md].opt_len;
287 if (strncmp (input, cl_options[md].opt_text, opt_len))
289 if (input[opt_len] == '\0')
291 if (cl_options[md].flags & lang_flag
292 && cl_options[md].flags & CL_JOINED)
304 /* Defer option CODE with argument ARG. */
306 defer_opt (code, arg)
310 /* FIXME: this should be in c_common_init_options, which should take
314 extern int save_argc;
315 deferred_size = save_argc;
316 deferred_opts = (struct deferred_opt *)
317 xmalloc (deferred_size * sizeof (struct deferred_opt));
320 if (deferred_count == deferred_size)
323 deferred_opts[deferred_count].code = code;
324 deferred_opts[deferred_count].arg = arg;
328 /* Common initialization before parsing options. */
330 c_common_init_options (lang)
331 enum c_language_kind lang;
334 parse_in = cpp_create_reader (lang == clk_c ? CLK_GNUC89 : CLK_GNUCXX,
336 cpp_opts = cpp_get_options (parse_in);
337 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
341 flag_const_strings = (lang == clk_cplusplus);
342 warn_pointer_arith = (lang == clk_cplusplus);
345 /* Handle one command-line option in (argc, argv).
346 Can be called multiple times, to handle multiple sets of options.
347 Returns number of strings consumed. */
349 c_common_decode_option (argc, argv)
353 static const int lang_flags[] = {CL_C, CL_OBJC, CL_CXX, CL_OBJCXX};
355 const char *opt, *arg = 0;
358 int result = 0, lang_flag;
359 const struct cl_option *option;
364 /* Interpret "-" or a non-switch as a file name. */
365 if (opt[0] != '-' || opt[1] == '\0')
373 error ("too many filenames given. Type %s --help for usage",
381 /* Drop the "no-" from negative switches. */
382 if ((opt[1] == 'W' || opt[1] == 'f')
383 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
385 size_t len = strlen (opt) - 3;
387 dup = xmalloc (len + 1);
390 memcpy (dup + 2, opt + 5, len - 2 + 1);
396 lang_flag = lang_flags[(c_language << 1) + flag_objc];
397 opt_index = find_opt (opt + 1, lang_flag);
398 if (opt_index == N_OPTS)
402 option = &cl_options[opt_index];
404 /* Sort out any argument the switch takes. */
405 if (option->flags & (CL_JOINED | CL_SEPARATE))
407 if (option->flags & CL_JOINED)
409 /* Have arg point to the original switch. This is because
410 some code, such as disable_builtin_function, expects its
411 argument to be persistent until the program exits. */
412 arg = argv[0] + cl_options[opt_index].opt_len + 1;
414 arg += strlen ("no-");
417 /* If we don't have an argument, and CL_SEPARATE, try the next
418 argument in the vector. */
419 if (!arg || (*arg == '\0' && option->flags & CL_SEPARATE))
425 if (!arg || *arg == '\0')
427 missing_arg (opt_index);
433 /* Complain about the wrong language after we've swallowed any
434 necessary extra argument. Eventually make this a hard error
435 after the call to find_opt, and return argc. */
436 if (!(cl_options[opt_index].flags & lang_flag))
438 complain_wrong_lang (opt_index);
445 case N_OPTS: /* Shut GCC up. */
452 case OPT__output_pch_:
457 defer_opt (code, arg);
461 cpp_opts->discard_comments = 0;
465 cpp_opts->discard_comments = 0;
466 cpp_opts->discard_comments_in_macro_exp = 0;
470 defer_opt (code, arg);
474 flag_preprocess_only = 1;
478 cpp_opts->print_include_names = 1;
482 if (strcmp (arg, "-"))
483 add_path (xstrdup (arg), BRACKET, 0);
486 if (quote_chain_split)
487 error ("-I- specified twice");
488 quote_chain_split = true;
489 split_quote_chain ();
495 /* When doing dependencies with -M or -MM, suppress normal
496 preprocessed output, but still do -dM etc. as software
497 depends on this. Preprocessed output does occur if -MD, -MMD
498 or environment var dependency generation is used. */
499 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
501 cpp_opts->inhibit_warnings = 1;
506 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
517 cpp_opts->deps.missing_files = true;
522 cpp_opts->deps.phony_targets = true;
528 defer_opt (code, arg);
532 flag_no_line_commands = 1;
536 defer_opt (code, arg);
547 warn_char_subscripts = on;
548 warn_missing_braces = on;
549 warn_parentheses = on;
550 warn_return_type = on;
551 warn_sequence_point = on; /* Was C only. */
552 if (c_language == clk_cplusplus)
553 warn_sign_compare = on;
555 warn_strict_aliasing = on;
557 /* Only warn about unknown pragmas that are not in system
559 warn_unknown_pragmas = on;
561 /* We save the value of warn_uninitialized, since if they put
562 -Wuninitialized on the command line, we need to generate a
563 warning about not using it without also specifying -O. */
564 if (warn_uninitialized != 1)
565 warn_uninitialized = (on ? 2 : 0);
567 if (c_language == clk_c)
568 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
569 can turn it off only if it's not explicit. */
573 /* C++-specific warnings. */
576 warn_nontemplate_friend = on;
579 cpp_opts->warn_trigraphs = on;
580 cpp_opts->warn_comments = on;
581 cpp_opts->warn_num_sign_change = on;
582 cpp_opts->warn_multichar = on; /* Was C++ only. */
585 case OPT_Wbad_function_cast:
586 warn_bad_function_cast = on;
593 case OPT_Wchar_subscripts:
594 warn_char_subscripts = on;
599 cpp_opts->warn_comments = on;
602 case OPT_Wconversion:
603 warn_conversion = on;
606 case OPT_Wctor_dtor_privacy:
607 warn_ctor_dtor_privacy = on;
610 case OPT_Wdeprecated:
611 warn_deprecated = on;
612 cpp_opts->warn_deprecated = on;
615 case OPT_Wdiv_by_zero:
616 warn_div_by_zero = on;
623 case OPT_Wendif_labels:
624 cpp_opts->warn_endif_labels = on;
628 cpp_opts->warnings_are_errors = on;
631 case OPT_Werror_implicit_function_declaration:
635 mesg_implicit_function_declaration = 2;
638 case OPT_Wfloat_equal:
639 warn_float_equal = on;
647 set_Wformat (atoi (arg));
650 case OPT_Wformat_extra_args:
651 warn_format_extra_args = on;
654 case OPT_Wformat_nonliteral:
655 warn_format_nonliteral = on;
658 case OPT_Wformat_security:
659 warn_format_security = on;
662 case OPT_Wformat_y2k:
663 warn_format_y2k = on;
666 case OPT_Wformat_zero_length:
667 warn_format_zero_length = on;
674 case OPT_Wimplicit_function_declaration:
675 mesg_implicit_function_declaration = on;
678 case OPT_Wimplicit_int:
679 warn_implicit_int = on;
683 cpp_opts->warn_import = on;
686 case OPT_Winvalid_offsetof:
687 warn_invalid_offsetof = on;
690 case OPT_Winvalid_pch:
691 cpp_opts->warn_invalid_pch = on;
705 case OPT_Wmissing_braces:
706 warn_missing_braces = on;
709 case OPT_Wmissing_declarations:
710 warn_missing_declarations = on;
713 case OPT_Wmissing_format_attribute:
714 warn_missing_format_attribute = on;
717 case OPT_Wmissing_prototypes:
718 warn_missing_prototypes = on;
722 cpp_opts->warn_multichar = on;
725 case OPT_Wnested_externs:
726 warn_nested_externs = on;
729 case OPT_Wnon_template_friend:
730 warn_nontemplate_friend = on;
733 case OPT_Wnon_virtual_dtor:
741 case OPT_Wold_style_cast:
742 warn_old_style_cast = on;
745 case OPT_Woverloaded_virtual:
746 warn_overloaded_virtual = on;
749 case OPT_Wparentheses:
750 warn_parentheses = on;
753 case OPT_Wpmf_conversions:
757 case OPT_Wpointer_arith:
758 warn_pointer_arith = on;
769 case OPT_Wredundant_decls:
770 warn_redundant_decls = on;
777 case OPT_Wreturn_type:
778 warn_return_type = on;
781 case OPT_Wsequence_point:
782 warn_sequence_point = on;
785 case OPT_Wsign_compare:
786 warn_sign_compare = on;
789 case OPT_Wsign_promo:
790 warn_sign_promo = on;
793 case OPT_Wstrict_prototypes:
794 warn_strict_prototypes = on;
801 case OPT_Wsystem_headers:
802 cpp_opts->warn_system_headers = on;
805 case OPT_Wtraditional:
806 warn_traditional = on;
807 cpp_opts->warn_traditional = on;
811 cpp_opts->warn_trigraphs = on;
814 case OPT_Wundeclared_selector:
815 warn_undeclared_selector = on;
819 cpp_opts->warn_undef = on;
822 case OPT_Wunknown_pragmas:
823 /* Set to greater than 1, so that even unknown pragmas in
824 system headers will be warned about. */
825 warn_unknown_pragmas = on * 2;
828 case OPT_Wunused_macros:
829 warn_unused_macros = on;
832 case OPT_Wwrite_strings:
833 if (c_language == clk_c)
834 flag_const_strings = on;
836 warn_write_strings = on;
840 if (c_language == clk_c)
841 set_std_c89 (false, true);
843 set_std_cxx98 (true);
850 case OPT_fcond_mismatch:
851 if (c_language == clk_c)
853 flag_cond_mismatch = on;
858 case OPT_fall_virtual:
859 case OPT_fenum_int_equiv:
860 case OPT_fguiding_decls:
862 case OPT_fhuge_objects:
864 case OPT_fname_mangling_version_:
866 case OPT_fnonnull_objects:
868 case OPT_fstrict_prototype:
869 case OPT_fthis_is_variable:
870 case OPT_fvtable_thunks:
872 warning ("switch \"%s\" is no longer supported", argv[0]);
875 case OPT_fabi_version_:
876 flag_abi_version = read_integral_parameter (arg, argv[0], 1);
879 case OPT_faccess_control:
880 flag_access_control = on;
883 case OPT_falt_external_templates:
884 flag_alt_external_templates = on;
886 flag_external_templates = true;
888 warning ("switch \"%s\" is deprecated, please see documentation for details", argv[0]);
896 flag_no_builtin = !on;
903 disable_builtin_function (arg);
906 case OPT_fdollars_in_identifiers:
907 cpp_opts->dollars_in_ident = on;
911 if (!on || !dump_switch_p (argv[0] + strlen ("-f")))
915 case OPT_ffreestanding:
917 /* Fall through... */
920 flag_no_builtin = !on;
921 /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
922 if (!on && warn_main == 2)
926 case OPT_fshort_double:
927 flag_short_double = on;
930 case OPT_fshort_enums:
931 flag_short_enums = on;
934 case OPT_fshort_wchar:
935 flag_short_wchar = on;
938 case OPT_fsigned_bitfields:
939 flag_signed_bitfields = on;
940 explicit_flag_signed_bitfields = 1;
943 case OPT_fsigned_char:
944 flag_signed_char = on;
947 case OPT_funsigned_bitfields:
948 flag_signed_bitfields = !on;
949 explicit_flag_signed_bitfields = 1;
952 case OPT_funsigned_char:
953 flag_signed_char = !on;
960 case OPT_fconserve_space:
961 flag_conserve_space = on;
964 case OPT_fconst_strings:
965 flag_const_strings = on;
968 case OPT_fconstant_string_class_:
969 constant_string_class_name = arg;
972 case OPT_fdefault_inline:
973 flag_default_inline = on;
976 case OPT_felide_constructors:
977 flag_elide_constructors = on;
980 case OPT_fenforce_eh_specs:
981 flag_enforce_eh_specs = on;
984 case OPT_fexternal_templates:
985 flag_external_templates = on;
988 case OPT_ffixed_form:
989 case OPT_ffixed_line_length_:
990 /* Fortran front end options ignored when preprocessing only. */
991 if (flag_preprocess_only)
996 flag_new_for_scope = on;
999 case OPT_fgnu_keywords:
1000 flag_no_gnu_keywords = !on;
1003 case OPT_fgnu_runtime:
1004 flag_next_runtime = !on;
1007 case OPT_fhandle_exceptions:
1008 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
1009 flag_exceptions = on;
1012 case OPT_fimplement_inlines:
1013 flag_implement_inlines = on;
1016 case OPT_fimplicit_inline_templates:
1017 flag_implicit_inline_templates = on;
1020 case OPT_fimplicit_templates:
1021 flag_implicit_templates = on;
1024 case OPT_fms_extensions:
1025 flag_ms_extensions = on;
1028 case OPT_fnext_runtime:
1029 flag_next_runtime = on;
1032 case OPT_fnonansi_builtins:
1033 flag_no_nonansi_builtin = !on;
1036 case OPT_foperator_names:
1037 cpp_opts->operator_names = on;
1040 case OPT_foptional_diags:
1041 flag_optional_diags = on;
1045 cpp_opts->restore_pch_deps = on;
1048 case OPT_fpermissive:
1049 flag_permissive = on;
1052 case OPT_fpreprocessed:
1053 cpp_opts->preprocessed = on;
1057 flag_use_repository = on;
1059 flag_implicit_templates = 0;
1066 case OPT_fshow_column:
1067 cpp_opts->show_column = on;
1071 flag_detailed_statistics = on;
1075 /* Don't recognize -fno-tabstop=. */
1079 /* It is documented that we silently ignore silly values. */
1082 long tabstop = strtol (arg, &endptr, 10);
1083 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1084 cpp_opts->tabstop = tabstop;
1088 case OPT_ftemplate_depth_:
1089 max_tinst_depth = read_integral_parameter (arg, argv[0], 0);
1092 case OPT_fvtable_gc:
1093 flag_vtable_gc = on;
1096 case OPT_fuse_cxa_atexit:
1097 flag_use_cxa_atexit = on;
1105 flag_gen_declaration = 1;
1109 add_path (xstrdup (arg), AFTER, 0);
1114 defer_opt (code, arg);
1126 add_path (xstrdup (arg), SYSTEM, 0);
1129 case OPT_iwithprefix:
1130 add_prefixed_path (arg, SYSTEM);
1133 case OPT_iwithprefixbefore:
1134 add_prefixed_path (arg, BRACKET);
1138 cpp_set_lang (parse_in, CLK_ASM);
1139 cpp_opts->dollars_in_ident = false;
1150 case OPT_nostdinc__:
1151 std_cxx_inc = false;
1159 error ("output filename specified twice");
1164 /* We need to handle the -pedantic switches here, rather than in
1165 c_common_post_options, so that a subsequent -Wno-endif-labels
1166 is not overridden. */
1167 case OPT_pedantic_errors:
1168 cpp_opts->pedantic_errors = 1;
1171 cpp_opts->pedantic = 1;
1172 cpp_opts->warn_endif_labels = 1;
1175 case OPT_print_objc_runtime_info:
1176 print_struct_values = 1;
1180 cpp_opts->remap = 1;
1184 case OPT_std_gnu__98:
1185 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
1189 case OPT_std_iso9899_1990:
1190 case OPT_std_iso9899_199409:
1191 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1195 set_std_c89 (false /* c94 */, false /* ISO */);
1200 case OPT_std_iso9899_1999:
1201 case OPT_std_iso9899_199x:
1202 set_std_c99 (true /* ISO */);
1207 set_std_c99 (false /* ISO */);
1211 cpp_opts->trigraphs = 1;
1214 case OPT_traditional_cpp:
1215 cpp_opts->traditional = 1;
1223 cpp_opts->inhibit_warnings = 1;
1237 /* Post-switch processing. */
1239 c_common_post_options (pfilename)
1240 const char **pfilename;
1242 /* Canonicalize the input and output filenames. */
1243 if (in_fname == NULL || !strcmp (in_fname, "-"))
1246 if (out_fname == NULL || !strcmp (out_fname, "-"))
1249 if (cpp_opts->deps.style == DEPS_NONE)
1250 check_deps_environment_vars ();
1252 handle_deferred_opts ();
1254 sanitize_cpp_opts ();
1256 register_include_chains (parse_in, sysroot, iprefix,
1257 std_inc, std_cxx_inc && c_language == clk_cplusplus,
1260 flag_inline_trees = 1;
1262 /* Use tree inlining if possible. Function instrumentation is only
1263 done in the RTL level, so we disable tree inlining. */
1264 if (! flag_instrument_function_entry_exit)
1266 if (!flag_no_inline)
1268 if (flag_inline_functions)
1270 flag_inline_trees = 2;
1271 flag_inline_functions = 0;
1275 /* -Wextra implies -Wsign-compare, but not if explicitly
1277 if (warn_sign_compare == -1)
1278 warn_sign_compare = extra_warnings;
1280 /* Special format checking options don't work without -Wformat; warn if
1282 if (warn_format_y2k && !warn_format)
1283 warning ("-Wformat-y2k ignored without -Wformat");
1284 if (warn_format_extra_args && !warn_format)
1285 warning ("-Wformat-extra-args ignored without -Wformat");
1286 if (warn_format_zero_length && !warn_format)
1287 warning ("-Wformat-zero-length ignored without -Wformat");
1288 if (warn_format_nonliteral && !warn_format)
1289 warning ("-Wformat-nonliteral ignored without -Wformat");
1290 if (warn_format_security && !warn_format)
1291 warning ("-Wformat-security ignored without -Wformat");
1292 if (warn_missing_format_attribute && !warn_format)
1293 warning ("-Wmissing-format-attribute ignored without -Wformat");
1295 if (flag_preprocess_only)
1297 /* Open the output now. We must do so even if flag_no_output is
1298 on, because there may be other output than from the actual
1299 preprocessing (e.g. from -dM). */
1300 if (out_fname[0] == '\0')
1301 out_stream = stdout;
1303 out_stream = fopen (out_fname, "w");
1305 if (out_stream == NULL)
1307 fatal_error ("opening output file %s: %m", out_fname);
1311 init_pp_output (out_stream);
1317 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1321 cpp_get_callbacks (parse_in)->file_change = cb_file_change;
1323 /* NOTE: we use in_fname here, not the one supplied. */
1324 *pfilename = cpp_read_main_file (parse_in, in_fname);
1326 saved_lineno = input_line;
1329 /* If an error has occurred in cpplib, note it so we fail
1331 errorcount += cpp_errors (parse_in);
1333 return flag_preprocess_only;
1336 /* Front end initialization common to C, ObjC and C++. */
1340 input_line = saved_lineno;
1342 /* Set up preprocessor arithmetic. Must be done after call to
1343 c_common_nodes_and_builtins for type nodes to be good. */
1344 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1345 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1346 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1347 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1348 cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1349 cpp_opts->EBCDIC = TARGET_EBCDIC;
1351 if (flag_preprocess_only)
1354 preprocess_file (parse_in);
1358 /* Has to wait until now so that cpplib has its hash table. */
1364 /* A thin wrapper around the real parser that initializes the
1365 integrated preprocessor after debug output has been initialized.
1366 Also, make sure the start_source_file debug hook gets called for
1367 the primary source file. */
1369 c_common_parse_file (set_yydebug)
1370 int set_yydebug ATTRIBUTE_UNUSED;
1373 yydebug = set_yydebug;
1375 warning ("YYDEBUG not defined");
1378 (*debug_hooks->start_source_file) (input_line, input_filename);
1382 free_parser_stacks ();
1385 /* Common finish hook for the C, ObjC and C++ front ends. */
1389 FILE *deps_stream = NULL;
1391 if (cpp_opts->deps.style != DEPS_NONE)
1393 /* If -M or -MM was seen without -MF, default output to the
1396 deps_stream = out_stream;
1399 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1401 fatal_error ("opening dependency file %s: %m", deps_file);
1405 /* For performance, avoid tearing down cpplib's internal structures
1406 with cpp_destroy (). */
1407 errorcount += cpp_finish (parse_in, deps_stream);
1409 if (deps_stream && deps_stream != out_stream
1410 && (ferror (deps_stream) || fclose (deps_stream)))
1411 fatal_error ("closing dependency file %s: %m", deps_file);
1413 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1414 fatal_error ("when writing output to %s: %m", out_fname);
1417 /* Either of two environment variables can specify output of
1418 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1419 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1420 and DEPS_TARGET is the target to mention in the deps. They also
1421 result in dependency information being appended to the output file
1422 rather than overwriting it, and like Sun's compiler
1423 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1425 check_deps_environment_vars ()
1429 GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1431 cpp_opts->deps.style = DEPS_USER;
1434 GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1437 cpp_opts->deps.style = DEPS_SYSTEM;
1438 cpp_opts->deps.ignore_main_file = true;
1444 /* Find the space before the DEPS_TARGET, if there is one. */
1445 char *s = strchr (spec, ' ');
1448 /* Let the caller perform MAKE quoting. */
1449 defer_opt (OPT_MT, s + 1);
1453 /* Command line -MF overrides environment variables and default. */
1461 /* Handle deferred command line switches. */
1463 handle_deferred_opts ()
1467 for (i = 0; i < deferred_count; i++)
1469 struct deferred_opt *opt = &deferred_opts[i];
1471 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1472 cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1476 /* These settings are appropriate for GCC, but not necessarily so for
1477 cpplib as a library. */
1479 sanitize_cpp_opts ()
1481 /* If we don't know what style of dependencies to output, complain
1482 if any other dependency switches have been given. */
1483 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1484 error ("to generate dependencies you must specify either -M or -MM");
1486 /* -dM and dependencies suppress normal output; do it here so that
1487 the last -d[MDN] switch overrides earlier ones. */
1488 if (flag_dump_macros == 'M')
1491 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1492 -dM since at least glibc relies on -M -dM to work. */
1495 if (flag_dump_macros != 'M')
1496 flag_dump_macros = 0;
1497 flag_dump_includes = 0;
1500 cpp_opts->unsigned_char = !flag_signed_char;
1501 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1503 /* We want -Wno-long-long to override -pedantic -std=non-c99
1504 and/or -Wtraditional, whatever the ordering. */
1505 cpp_opts->warn_long_long
1506 = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1509 /* Add include path with a prefix at the front of its name. */
1511 add_prefixed_path (suffix, chain)
1517 size_t prefix_len, suffix_len;
1519 suffix_len = strlen (suffix);
1520 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1521 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1523 path = xmalloc (prefix_len + suffix_len + 1);
1524 memcpy (path, prefix, prefix_len);
1525 memcpy (path + prefix_len, suffix, suffix_len);
1526 path[prefix_len + suffix_len] = '\0';
1528 add_path (path, chain, 0);
1531 /* Handle -D, -U, -A, -imacros, and the first -include. */
1535 if (!cpp_opts->preprocessed)
1539 cpp_change_file (parse_in, LC_RENAME, _("<built-in>"));
1540 cpp_init_builtins (parse_in, flag_hosted);
1541 c_cpp_builtins (parse_in);
1542 cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
1543 for (i = 0; i < deferred_count; i++)
1545 struct deferred_opt *opt = &deferred_opts[i];
1547 if (opt->code == OPT_D)
1548 cpp_define (parse_in, opt->arg);
1549 else if (opt->code == OPT_U)
1550 cpp_undef (parse_in, opt->arg);
1551 else if (opt->code == OPT_A)
1553 if (opt->arg[0] == '-')
1554 cpp_unassert (parse_in, opt->arg + 1);
1556 cpp_assert (parse_in, opt->arg);
1560 /* Handle -imacros after -D and -U. */
1561 for (i = 0; i < deferred_count; i++)
1563 struct deferred_opt *opt = &deferred_opts[i];
1565 if (opt->code == OPT_imacros
1566 && cpp_push_include (parse_in, opt->arg))
1567 cpp_scan_nooutput (parse_in);
1571 push_command_line_include ();
1574 /* Give CPP the next file given by -include, if any. */
1576 push_command_line_include ()
1578 if (cpp_opts->preprocessed)
1581 while (include_cursor < deferred_count)
1583 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1585 if (opt->code == OPT_include && cpp_push_include (parse_in, opt->arg))
1589 if (include_cursor == deferred_count)
1591 /* Restore the line map from <command line>. */
1592 cpp_change_file (parse_in, LC_RENAME, main_input_filename);
1593 /* -Wunused-macros should only warn about macros defined hereafter. */
1594 cpp_opts->warn_unused_macros = warn_unused_macros;
1599 /* File change callback. Has to handle -include files. */
1601 cb_file_change (pfile, new_map)
1602 cpp_reader *pfile ATTRIBUTE_UNUSED;
1603 const struct line_map *new_map;
1605 if (flag_preprocess_only)
1606 pp_file_change (new_map);
1608 fe_file_change (new_map);
1610 if (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map))
1611 push_command_line_include ();
1614 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1615 extensions if ISO). There is no concept of gnu94. */
1617 set_std_c89 (c94, iso)
1620 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1623 flag_no_gnu_keywords = iso;
1624 flag_no_nonansi_builtin = iso;
1625 flag_noniso_default_format_attributes = !iso;
1628 flag_writable_strings = 0;
1631 /* Set the C 99 standard (without GNU extensions if ISO). */
1636 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1638 flag_no_nonansi_builtin = iso;
1639 flag_noniso_default_format_attributes = !iso;
1643 flag_writable_strings = 0;
1646 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1651 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1652 flag_no_gnu_keywords = iso;
1653 flag_no_nonansi_builtin = iso;
1654 flag_noniso_default_format_attributes = !iso;
1658 /* Handle setting implicit to ON. */
1664 warn_implicit_int = on;
1667 if (mesg_implicit_function_declaration != 2)
1668 mesg_implicit_function_declaration = 1;
1671 mesg_implicit_function_declaration = 0;
1674 /* Args to -d specify what to dump. Silently ignore
1675 unrecognized options; they may be aimed at toplev.c. */
1682 while ((c = *arg++) != '\0')
1685 case 'M': /* Dump macros only. */
1686 case 'N': /* Dump names. */
1687 case 'D': /* Dump definitions. */
1688 flag_dump_macros = c;
1692 flag_dump_includes = 1;
1697 /* Write a slash-separated list of languages in FLAGS to BUF. */
1699 write_langs (buf, flags)
1706 if (flags & CL_OBJC)
1710 strcat (buf, "ObjC");
1716 strcat (buf, "C++");
1720 /* Complain that switch OPT_INDEX does not apply to this front end. */
1722 complain_wrong_lang (opt_index)
1725 char ok_langs[60], bad_langs[60];
1726 int ok_flags = cl_options[opt_index].flags;
1728 write_langs (ok_langs, ok_flags);
1729 write_langs (bad_langs, ~ok_flags);
1730 warning ("\"-%s\" is valid for %s but not for %s",
1731 cl_options[opt_index].opt_text, ok_langs, bad_langs);
1734 /* Handle --help output. */
1738 /* To keep the lines from getting too long for some compilers, limit
1739 to about 500 characters (6 lines) per chunk. */
1742 -include <file> Include the contents of <file> before other files\n\
1743 -imacros <file> Accept definition of macros in <file>\n\
1744 -iprefix <path> Specify <path> as a prefix for next two options\n\
1745 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1746 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1747 -isystem <dir> Add <dir> to the start of the system include path\n\
1750 -idirafter <dir> Add <dir> to the end of the system include path\n\
1751 -I <dir> Add <dir> to the end of the main include path\n\
1752 -I- Fine-grained include path control; see info docs\n\
1753 -nostdinc Do not search system include directories\n\
1754 (dirs specified with -isystem will still be used)\n\
1755 -nostdinc++ Do not search system include directories for C++\n\
1756 -o <file> Put output into <file>\n\
1759 -trigraphs Support ISO C trigraphs\n\
1760 -std=<std name> Specify the conformance standard; one of:\n\
1761 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1762 iso9899:199409, iso9899:1999, c++98\n\
1763 -w Inhibit warning messages\n\
1764 -W[no-]trigraphs Warn if trigraphs are encountered\n\
1765 -W[no-]comment{s} Warn if one comment starts inside another\n\
1768 -W[no-]traditional Warn about features not present in traditional C\n\
1769 -W[no-]undef Warn if an undefined macro is used by #if\n\
1770 -W[no-]import Warn about the use of the #import directive\n\
1773 -W[no-]error Treat all warnings as errors\n\
1774 -W[no-]system-headers Do not suppress warnings from system headers\n\
1775 -W[no-]all Enable most preprocessor warnings\n\
1778 -M Generate make dependencies\n\
1779 -MM As -M, but ignore system header files\n\
1780 -MD Generate make dependencies and compile\n\
1781 -MMD As -MD, but ignore system header files\n\
1782 -MF <file> Write dependency output to the given file\n\
1783 -MG Treat missing header file as generated files\n\
1786 -MP Generate phony targets for all headers\n\
1787 -MQ <target> Add a MAKE-quoted target\n\
1788 -MT <target> Add an unquoted target\n\
1791 -D<macro> Define a <macro> with string '1' as its value\n\
1792 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1793 -A<question>=<answer> Assert the <answer> to <question>\n\
1794 -A-<question>=<answer> Disable the <answer> to <question>\n\
1795 -U<macro> Undefine <macro> \n\
1796 -v Display the version number\n\
1799 -H Print the name of header files as they are used\n\
1800 -C Do not discard comments\n\
1801 -dM Display a list of macro definitions active at end\n\
1802 -dD Preserve macro definitions in output\n\
1803 -dN As -dD except that only the names are preserved\n\
1804 -dI Include #include directives in the output\n\
1807 -f[no-]preprocessed Treat the input file as already preprocessed\n\
1808 -ftabstop=<number> Distance between tab stops for column reporting\n\
1809 -isysroot <dir> Set <dir> to be the system root directory\n\
1810 -P Do not generate #line directives\n\
1811 -remap Remap file names when including files\n\
1812 --help Display this information\n\