1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Neil Booth.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "langhooks.h"
31 #include "diagnostic.h"
33 #include "cppdefault.h"
35 #include "debug.h" /* For debug_hooks. */
39 #include "target.h" /* For gcc_targetcm. */
40 #include "tm_p.h" /* For C_COMMON_OVERRIDE_OPTIONS. */
42 #ifndef DOLLARS_IN_IDENTIFIERS
43 # define DOLLARS_IN_IDENTIFIERS true
46 #ifndef TARGET_SYSTEM_ROOT
47 # define TARGET_SYSTEM_ROOT NULL
51 #define TARGET_OPTF(ARG)
55 cpp_options *cpp_opts;
58 static const char *this_input_filename;
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 multilib directory given by -imultilib, if any. */
80 static const char *imultilib;
82 /* The system root, if any. Overridden by -isysroot. */
83 static const char *sysroot = TARGET_SYSTEM_ROOT;
85 /* Zero disables all standard directories for headers. */
86 static bool std_inc = true;
88 /* Zero disables the C++-specific standard directories for headers. */
89 static bool std_cxx_inc = true;
91 /* If the quote chain has been split by -I-. */
92 static bool quote_chain_split;
94 /* If -Wunused-macros. */
95 static bool warn_unused_macros;
97 /* If -Wvariadic-macros. */
98 static bool warn_variadic_macros = true;
100 /* Number of deferred options. */
101 static size_t deferred_count;
103 /* Number of deferred options scanned for -include. */
104 static size_t include_cursor;
106 static void handle_OPT_d (const char *);
107 static void set_std_cxx98 (int);
108 static void set_std_cxx0x (int);
109 static void set_std_c89 (int, int);
110 static void set_std_c99 (int);
111 static void set_std_c1x (int);
112 static void check_deps_environment_vars (void);
113 static void handle_deferred_opts (void);
114 static void sanitize_cpp_opts (void);
115 static void add_prefixed_path (const char *, size_t);
116 static void push_command_line_include (void);
117 static void cb_file_change (cpp_reader *, const struct line_map *);
118 static void cb_dir_change (cpp_reader *, const char *);
119 static void finish_options (void);
121 #ifndef STDC_0_IN_SYSTEM_HEADERS
122 #define STDC_0_IN_SYSTEM_HEADERS 0
125 /* Holds switches parsed by c_common_handle_option (), but whose
126 handling is deferred to c_common_post_options (). */
127 static void defer_opt (enum opt_code, const char *);
128 static struct deferred_opt
135 static const unsigned int
136 c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
138 /* Defer option CODE with argument ARG. */
140 defer_opt (enum opt_code code, const char *arg)
142 deferred_opts[deferred_count].code = code;
143 deferred_opts[deferred_count].arg = arg;
147 /* -Werror= may set a warning option to enable a warning that is emitted
148 by the preprocessor. Set any corresponding flag in cpp_opts. */
151 warning_as_error_callback (int option_index)
153 switch (option_index)
156 /* Ignore options not associated with the preprocessor. */
159 case OPT_Wdeprecated:
160 cpp_opts->cpp_warn_deprecated = 1;
164 cpp_opts->warn_comments = 1;
168 cpp_opts->warn_trigraphs = 1;
172 cpp_opts->warn_multichar = 1;
175 case OPT_Wtraditional:
176 cpp_opts->cpp_warn_traditional = 1;
180 cpp_opts->cpp_warn_long_long = 1;
183 case OPT_Wendif_labels:
184 cpp_opts->warn_endif_labels = 1;
187 case OPT_Wvariadic_macros:
188 /* Set the local flag that is used later to update cpp_opts. */
189 warn_variadic_macros = 1;
192 case OPT_Wbuiltin_macro_redefined:
193 cpp_opts->warn_builtin_macro_redefined = 1;
197 cpp_opts->warn_undef = 1;
200 case OPT_Wunused_macros:
201 /* Set the local flag that is used later to update cpp_opts. */
202 warn_unused_macros = 1;
205 case OPT_Wc___compat:
206 /* Add warnings in the same way as c_common_handle_option below. */
207 if (warn_enum_compare == -1)
208 warn_enum_compare = 1;
209 if (warn_jump_misses_init == -1)
210 warn_jump_misses_init = 1;
211 cpp_opts->warn_cxx_operator_names = 1;
214 case OPT_Wnormalized_:
215 inform (input_location, "-Werror=normalized=: Set -Wnormalized=nfc");
216 cpp_opts->warn_normalize = normalized_C;
219 case OPT_Winvalid_pch:
220 cpp_opts->warn_invalid_pch = 1;
224 /* Handled by standard diagnostics using the option's associated
230 /* Return language mask for option parsing. */
232 c_common_option_lang_mask (void)
234 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
236 return lang_flags[c_language];
239 /* Common diagnostics initialization. */
241 c_common_initialize_diagnostics (diagnostic_context *context)
243 /* Register callback for warnings enabled by -Werror=. */
244 register_warning_as_error_callback (warning_as_error_callback);
246 /* This is conditionalized only because that is the way the front
247 ends used to do it. Maybe this should be unconditional? */
248 if (c_dialect_cxx ())
250 /* By default wrap lines at 80 characters. Is getenv
251 ("COLUMNS") preferable? */
252 diagnostic_line_cutoff (context) = 80;
253 /* By default, emit location information once for every
254 diagnostic message. */
255 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
258 context->opt_permissive = OPT_fpermissive;
261 /* Whether options from all C-family languages should be accepted
263 static bool accept_all_c_family_options = false;
265 /* Return whether to complain about a wrong-language option. */
267 c_common_complain_wrong_lang_p (const struct cl_option *option)
269 if (accept_all_c_family_options
270 && (option->flags & c_family_lang_mask))
276 /* Initialize options structure OPTS. */
278 c_common_init_options_struct (struct gcc_options *opts)
280 opts->x_flag_exceptions = c_dialect_cxx ();
281 opts->x_warn_pointer_arith = c_dialect_cxx ();
282 opts->x_warn_write_strings = c_dialect_cxx ();
283 opts->x_flag_warn_unused_result = true;
285 /* By default, C99-like requirements for complex multiply and divide. */
286 opts->x_flag_complex_method = 2;
289 /* Common initialization before calling option handlers. */
291 c_common_init_options (unsigned int decoded_options_count,
292 struct cl_decoded_option *decoded_options)
295 struct cpp_callbacks *cb;
297 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
298 ident_hash, line_table);
299 cb = cpp_get_callbacks (parse_in);
300 cb->error = c_cpp_error;
302 cpp_opts = cpp_get_options (parse_in);
303 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
304 cpp_opts->objc = c_dialect_objc ();
306 /* Reset to avoid warnings on internal definitions. We set it just
307 before passing on command-line options to cpplib. */
308 cpp_opts->warn_dollars = 0;
310 deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
312 if (c_language == clk_c)
314 /* If preprocessing assembly language, accept any of the C-family
315 front end options since the driver may pass them through. */
316 for (i = 1; i < decoded_options_count; i++)
317 if (decoded_options[i].opt_index == OPT_lang_asm)
319 accept_all_c_family_options = true;
325 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
326 form of an -f or -W option was given. Returns false if the switch was
327 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
329 c_common_handle_option (size_t scode, const char *arg, int value,
330 int kind, const struct cl_option_handlers *handlers)
332 const struct cl_option *option = &cl_options[scode];
333 enum opt_code code = (enum opt_code) scode;
336 /* Prevent resetting the language standard to a C dialect when the driver
337 has already determined that we're looking at assembler input. */
338 bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM);
343 if (cl_options[code].flags & c_family_lang_mask)
345 if ((option->flags & CL_TARGET)
346 && ! targetcm.handle_c_option (scode, arg, value))
353 case OPT__output_pch_:
358 defer_opt (code, arg);
362 cpp_opts->discard_comments = 0;
366 cpp_opts->discard_comments = 0;
367 cpp_opts->discard_comments_in_macro_exp = 0;
371 defer_opt (code, arg);
375 cpp_opts->print_include_names = 1;
379 TARGET_OPTF (xstrdup (arg));
383 if (strcmp (arg, "-"))
384 add_path (xstrdup (arg), BRACKET, 0, true);
387 if (quote_chain_split)
388 error ("-I- specified twice");
389 quote_chain_split = true;
390 split_quote_chain ();
391 inform (input_location, "obsolete option -I- used, please use -iquote instead");
397 /* When doing dependencies with -M or -MM, suppress normal
398 preprocessed output, but still do -dM etc. as software
399 depends on this. Preprocessed output does occur if -MD, -MMD
400 or environment var dependency generation is used. */
401 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
407 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
408 cpp_opts->deps.need_preprocessor_output = true;
419 cpp_opts->deps.missing_files = true;
424 cpp_opts->deps.phony_targets = true;
430 defer_opt (code, arg);
434 flag_no_line_commands = 1;
438 defer_opt (code, arg);
444 handle_generated_option (&global_options, &global_options_set,
445 OPT_Wimplicit, NULL, value,
446 c_family_lang_mask, kind, handlers, global_dc);
447 warn_char_subscripts = value;
448 warn_missing_braces = value;
449 warn_parentheses = value;
450 warn_return_type = value;
451 warn_sequence_point = value; /* Was C only. */
453 if (warn_strict_aliasing == -1)
454 set_Wstrict_aliasing (value);
455 warn_address = value;
456 if (warn_strict_overflow == -1)
457 warn_strict_overflow = value;
458 warn_array_bounds = value;
459 warn_volatile_register_var = value;
461 /* Only warn about unknown pragmas that are not in system
463 warn_unknown_pragmas = value;
465 warn_uninitialized = value;
467 if (!c_dialect_cxx ())
469 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
470 can turn it off only if it's not explicit. */
472 warn_main = (value ? 2 : 0);
474 /* In C, -Wall turns on -Wenum-compare, which we do here.
475 In C++ it is on by default, which is done in
476 c_common_post_options. */
477 if (warn_enum_compare == -1)
478 warn_enum_compare = value;
482 /* C++-specific warnings. */
483 warn_sign_compare = value;
484 warn_reorder = value;
485 warn_cxx0x_compat = value;
488 cpp_opts->warn_trigraphs = value;
489 cpp_opts->warn_comments = value;
490 cpp_opts->warn_num_sign_change = value;
492 if (warn_pointer_sign == -1)
493 warn_pointer_sign = value;
496 case OPT_Wbuiltin_macro_redefined:
497 cpp_opts->warn_builtin_macro_redefined = value;
501 cpp_opts->warn_comments = value;
504 case OPT_Wc___compat:
505 /* Because -Wenum-compare is the default in C++, -Wc++-compat
506 implies -Wenum-compare. */
507 if (warn_enum_compare == -1 && value)
508 warn_enum_compare = value;
509 /* Because C++ always warns about a goto which misses an
510 initialization, -Wc++-compat turns on -Wjump-misses-init. */
511 if (warn_jump_misses_init == -1 && value)
512 warn_jump_misses_init = value;
513 cpp_opts->warn_cxx_operator_names = value;
516 case OPT_Wdeprecated:
517 cpp_opts->cpp_warn_deprecated = value;
520 case OPT_Wendif_labels:
521 cpp_opts->warn_endif_labels = value;
525 global_dc->warning_as_error_requested = value;
533 set_Wformat (atoi (arg));
537 gcc_assert (value == 0 || value == 1);
538 if (warn_implicit_int == -1)
539 handle_generated_option (&global_options, &global_options_set,
540 OPT_Wimplicit_int, NULL, value,
541 c_family_lang_mask, kind, handlers,
543 if (warn_implicit_function_declaration == -1)
544 handle_generated_option (&global_options, &global_options_set,
545 OPT_Wimplicit_function_declaration, NULL,
546 value, c_family_lang_mask, kind, handlers,
550 case OPT_Winvalid_pch:
551 cpp_opts->warn_invalid_pch = value;
554 case OPT_Wmissing_include_dirs:
555 cpp_opts->warn_missing_include_dirs = value;
559 cpp_opts->warn_multichar = value;
562 case OPT_Wnormalized_:
563 if (!value || (arg && strcasecmp (arg, "none") == 0))
564 cpp_opts->warn_normalize = normalized_none;
565 else if (!arg || strcasecmp (arg, "nfkc") == 0)
566 cpp_opts->warn_normalize = normalized_KC;
567 else if (strcasecmp (arg, "id") == 0)
568 cpp_opts->warn_normalize = normalized_identifier_C;
569 else if (strcasecmp (arg, "nfc") == 0)
570 cpp_opts->warn_normalize = normalized_C;
572 error ("argument %qs to %<-Wnormalized%> not recognized", arg);
575 case OPT_Wreturn_type:
576 warn_return_type = value;
579 case OPT_Wtraditional:
580 cpp_opts->cpp_warn_traditional = value;
584 cpp_opts->warn_trigraphs = value;
588 cpp_opts->warn_undef = value;
591 case OPT_Wunknown_pragmas:
592 /* Set to greater than 1, so that even unknown pragmas in
593 system headers will be warned about. */
594 warn_unknown_pragmas = value * 2;
597 case OPT_Wunused_macros:
598 warn_unused_macros = value;
601 case OPT_Wvariadic_macros:
602 warn_variadic_macros = value;
605 case OPT_Wwrite_strings:
606 warn_write_strings = value;
612 warn_nonvdtor = true;
616 if (!c_dialect_cxx ())
617 set_std_c89 (false, true);
619 set_std_cxx98 (true);
626 case OPT_fcond_mismatch:
627 if (!c_dialect_cxx ())
629 flag_cond_mismatch = value;
632 warning (0, "switch %qs is no longer supported", option->opt_text);
639 disable_builtin_function (arg);
642 case OPT_fdirectives_only:
643 cpp_opts->directives_only = value;
646 case OPT_fdollars_in_identifiers:
647 cpp_opts->dollars_in_ident = value;
650 case OPT_ffreestanding:
652 /* Fall through.... */
655 flag_no_builtin = !value;
658 case OPT_fconstant_string_class_:
659 constant_string_class_name = arg;
662 case OPT_fextended_identifiers:
663 cpp_opts->extended_identifiers = value;
666 case OPT_fgnu_runtime:
667 flag_next_runtime = !value;
670 case OPT_fnext_runtime:
671 flag_next_runtime = value;
674 case OPT_foperator_names:
675 cpp_opts->operator_names = value;
679 cpp_opts->restore_pch_deps = value;
682 case OPT_fpch_preprocess:
683 flag_pch_preprocess = value;
686 case OPT_fpermissive:
687 flag_permissive = value;
688 global_dc->permissive = value;
691 case OPT_fpreprocessed:
692 cpp_opts->preprocessed = value;
696 flag_use_repository = value;
698 flag_implicit_templates = 0;
702 /* It is documented that we silently ignore silly values. */
703 if (value >= 1 && value <= 100)
704 cpp_opts->tabstop = value;
707 case OPT_fexec_charset_:
708 cpp_opts->narrow_charset = arg;
711 case OPT_fwide_exec_charset_:
712 cpp_opts->wide_charset = arg;
715 case OPT_finput_charset_:
716 cpp_opts->input_charset = arg;
719 case OPT_ftemplate_depth_:
720 max_tinst_depth = value;
723 case OPT_fvisibility_inlines_hidden:
724 visibility_options.inlines_hidden = value;
727 case OPT_femit_struct_debug_baseonly:
728 set_struct_debug_option ("base");
731 case OPT_femit_struct_debug_reduced:
732 set_struct_debug_option ("dir:ord:sys,dir:gen:any,ind:base");
735 case OPT_femit_struct_debug_detailed_:
736 set_struct_debug_option (arg);
740 add_path (xstrdup (arg), AFTER, 0, true);
745 defer_opt (code, arg);
757 add_path (xstrdup (arg), QUOTE, 0, true);
765 add_path (xstrdup (arg), SYSTEM, 0, true);
768 case OPT_iwithprefix:
769 add_prefixed_path (arg, SYSTEM);
772 case OPT_iwithprefixbefore:
773 add_prefixed_path (arg, BRACKET);
777 cpp_set_lang (parse_in, CLK_ASM);
778 cpp_opts->dollars_in_ident = false;
793 error ("output filename specified twice");
796 /* We need to handle the -pedantic switches here, rather than in
797 c_common_post_options, so that a subsequent -Wno-endif-labels
798 is not overridden. */
799 case OPT_pedantic_errors:
801 cpp_opts->cpp_pedantic = 1;
802 cpp_opts->warn_endif_labels = 1;
803 if (warn_pointer_sign == -1)
804 warn_pointer_sign = 1;
805 if (warn_overlength_strings == -1)
806 warn_overlength_strings = 1;
811 case OPT_print_objc_runtime_info:
812 print_struct_values = 1;
820 case OPT_std_gnu__98:
821 if (!preprocessing_asm_p)
822 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
826 case OPT_std_gnu__0x:
827 if (!preprocessing_asm_p)
828 set_std_cxx0x (code == OPT_std_c__0x /* ISO */);
832 case OPT_std_iso9899_199409:
833 if (!preprocessing_asm_p)
834 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
838 if (!preprocessing_asm_p)
839 set_std_c89 (false /* c94 */, false /* ISO */);
843 if (!preprocessing_asm_p)
844 set_std_c99 (true /* ISO */);
848 if (!preprocessing_asm_p)
849 set_std_c99 (false /* ISO */);
853 if (!preprocessing_asm_p)
854 set_std_c1x (true /* ISO */);
858 if (!preprocessing_asm_p)
859 set_std_c1x (false /* ISO */);
863 cpp_opts->trigraphs = 1;
866 case OPT_traditional_cpp:
867 cpp_opts->traditional = 1;
882 /* Post-switch processing. */
884 c_common_post_options (const char **pfilename)
886 struct cpp_callbacks *cb;
888 /* Canonicalize the input and output filenames. */
889 if (in_fnames == NULL)
891 in_fnames = XNEWVEC (const char *, 1);
894 else if (strcmp (in_fnames[0], "-") == 0)
897 if (out_fname == NULL || !strcmp (out_fname, "-"))
900 if (cpp_opts->deps.style == DEPS_NONE)
901 check_deps_environment_vars ();
903 handle_deferred_opts ();
905 sanitize_cpp_opts ();
907 register_include_chains (parse_in, sysroot, iprefix, imultilib,
908 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
910 #ifdef C_COMMON_OVERRIDE_OPTIONS
911 /* Some machines may reject certain combinations of C
912 language-specific options. */
913 C_COMMON_OVERRIDE_OPTIONS;
916 /* Excess precision other than "fast" requires front-end
918 if (c_dialect_cxx ())
920 if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
921 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT)
922 sorry ("-fexcess-precision=standard for C++");
923 flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
925 else if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
926 flag_excess_precision_cmdline = (flag_iso
927 ? EXCESS_PRECISION_STANDARD
928 : EXCESS_PRECISION_FAST);
930 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
931 inline semantics are not supported in GNU89 or C89 mode. */
932 if (flag_gnu89_inline == -1)
933 flag_gnu89_inline = !flag_isoc99;
934 else if (!flag_gnu89_inline && !flag_isoc99)
935 error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode");
937 /* Default to ObjC sjlj exception handling if NeXT runtime. */
938 if (flag_objc_sjlj_exceptions < 0)
939 flag_objc_sjlj_exceptions = flag_next_runtime;
940 if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
943 /* -Wextra implies the following flags
944 unless explicitly overridden. */
945 if (warn_type_limits == -1)
946 warn_type_limits = extra_warnings;
947 if (warn_clobbered == -1)
948 warn_clobbered = extra_warnings;
949 if (warn_empty_body == -1)
950 warn_empty_body = extra_warnings;
951 if (warn_sign_compare == -1)
952 warn_sign_compare = extra_warnings;
953 if (warn_missing_field_initializers == -1)
954 warn_missing_field_initializers = extra_warnings;
955 if (warn_missing_parameter_type == -1)
956 warn_missing_parameter_type = extra_warnings;
957 if (warn_old_style_declaration == -1)
958 warn_old_style_declaration = extra_warnings;
959 if (warn_override_init == -1)
960 warn_override_init = extra_warnings;
961 if (warn_ignored_qualifiers == -1)
962 warn_ignored_qualifiers = extra_warnings;
964 /* -Wpointer-sign is disabled by default, but it is enabled if any
965 of -Wall or -pedantic are given. */
966 if (warn_pointer_sign == -1)
967 warn_pointer_sign = 0;
969 if (warn_strict_aliasing == -1)
970 warn_strict_aliasing = 0;
971 if (warn_strict_overflow == -1)
972 warn_strict_overflow = 0;
973 if (warn_jump_misses_init == -1)
974 warn_jump_misses_init = 0;
976 /* -Woverlength-strings is off by default, but is enabled by -pedantic.
977 It is never enabled in C++, as the minimum limit is not normative
979 if (warn_overlength_strings == -1 || c_dialect_cxx ())
980 warn_overlength_strings = 0;
982 /* Wmain is enabled by default in C++ but not in C. */
983 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
984 even if -Wall was given (warn_main will be 2 if set by -Wall, 1
985 if set by -Wmain). */
987 warn_main = (c_dialect_cxx () && flag_hosted) ? 1 : 0;
988 else if (warn_main == 2)
989 warn_main = flag_hosted ? 1 : 0;
991 /* In C, -Wconversion enables -Wsign-conversion (unless disabled
992 through -Wno-sign-conversion). While in C++,
993 -Wsign-conversion needs to be requested explicitly. */
994 if (warn_sign_conversion == -1)
995 warn_sign_conversion = (c_dialect_cxx ()) ? 0 : warn_conversion;
997 /* In C, -Wall and -Wc++-compat enable -Wenum-compare, which we do
998 in c_common_handle_option; if it has not yet been set, it is
999 disabled by default. In C++, it is enabled by default. */
1000 if (warn_enum_compare == -1)
1001 warn_enum_compare = c_dialect_cxx () ? 1 : 0;
1003 /* -Wpacked-bitfield-compat is on by default for the C languages. The
1004 warning is issued in stor-layout.c which is not part of the front-end so
1005 we need to selectively turn it on here. */
1006 if (warn_packed_bitfield_compat == -1)
1007 warn_packed_bitfield_compat = 1;
1009 /* Special format checking options don't work without -Wformat; warn if
1013 warning (OPT_Wformat_y2k,
1014 "-Wformat-y2k ignored without -Wformat");
1015 warning (OPT_Wformat_extra_args,
1016 "-Wformat-extra-args ignored without -Wformat");
1017 warning (OPT_Wformat_zero_length,
1018 "-Wformat-zero-length ignored without -Wformat");
1019 warning (OPT_Wformat_nonliteral,
1020 "-Wformat-nonliteral ignored without -Wformat");
1021 warning (OPT_Wformat_contains_nul,
1022 "-Wformat-contains-nul ignored without -Wformat");
1023 warning (OPT_Wformat_security,
1024 "-Wformat-security ignored without -Wformat");
1027 if (warn_implicit == -1)
1030 if (warn_implicit_int == -1)
1031 warn_implicit_int = 0;
1033 /* -Wimplicit-function-declaration is enabled by default for C99. */
1034 if (warn_implicit_function_declaration == -1)
1035 warn_implicit_function_declaration = flag_isoc99;
1037 /* If we're allowing C++0x constructs, don't warn about C++0x
1038 compatibility problems. */
1039 if (cxx_dialect == cxx0x)
1040 warn_cxx0x_compat = 0;
1042 if (flag_preprocess_only)
1044 /* Open the output now. We must do so even if flag_no_output is
1045 on, because there may be other output than from the actual
1046 preprocessing (e.g. from -dM). */
1047 if (out_fname[0] == '\0')
1048 out_stream = stdout;
1050 out_stream = fopen (out_fname, "w");
1052 if (out_stream == NULL)
1054 fatal_error ("opening output file %s: %m", out_fname);
1058 if (num_in_fnames > 1)
1059 error ("too many filenames given. Type %s --help for usage",
1062 init_pp_output (out_stream);
1068 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1069 input_location = UNKNOWN_LOCATION;
1072 cb = cpp_get_callbacks (parse_in);
1073 cb->file_change = cb_file_change;
1074 cb->dir_change = cb_dir_change;
1075 cpp_post_options (parse_in);
1077 input_location = UNKNOWN_LOCATION;
1079 *pfilename = this_input_filename
1080 = cpp_read_main_file (parse_in, in_fnames[0]);
1081 /* Don't do any compilation or preprocessing if there is no input file. */
1082 if (this_input_filename == NULL)
1088 if (flag_working_directory
1089 && flag_preprocess_only && !flag_no_line_commands)
1090 pp_dir_change (parse_in, get_src_pwd ());
1092 return flag_preprocess_only;
1095 /* Front end initialization common to C, ObjC and C++. */
1097 c_common_init (void)
1099 /* Set up preprocessor arithmetic. Must be done after call to
1100 c_common_nodes_and_builtins for type nodes to be good. */
1101 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1102 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1103 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1104 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1105 cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node);
1106 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1108 /* This can't happen until after wchar_precision and bytes_big_endian
1110 cpp_init_iconv (parse_in);
1113 c_common_print_pch_checksum (stderr);
1115 /* Has to wait until now so that cpplib has its hash table. */
1118 if (flag_preprocess_only)
1121 preprocess_file (parse_in);
1128 /* Initialize the integrated preprocessor after debug output has been
1129 initialized; loop over each input file. */
1131 c_common_parse_file (int set_yydebug)
1139 warning(0, "The C parser does not support -dy, option ignored");
1143 "The Objective-C parser does not support -dy, option ignored");
1146 warning(0, "The C++ parser does not support -dy, option ignored");
1150 "The Objective-C++ parser does not support -dy, option ignored");
1164 /* And end the main input file, if the debug writer wants it */
1165 if (debug_hooks->start_end_main_source_file)
1166 (*debug_hooks->end_source_file) (0);
1167 if (++i >= num_in_fnames)
1169 cpp_undef_all (parse_in);
1170 cpp_clear_file_cache (parse_in);
1172 = cpp_read_main_file (parse_in, in_fnames[i]);
1173 /* If an input file is missing, abandon further compilation.
1174 cpplib has issued a diagnostic. */
1175 if (!this_input_filename)
1180 /* Common finish hook for the C, ObjC and C++ front ends. */
1182 c_common_finish (void)
1184 FILE *deps_stream = NULL;
1186 /* Don't write the deps file if there are errors. */
1187 if (cpp_opts->deps.style != DEPS_NONE && !seen_error ())
1189 /* If -M or -MM was seen without -MF, default output to the
1192 deps_stream = out_stream;
1195 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1197 fatal_error ("opening dependency file %s: %m", deps_file);
1201 /* For performance, avoid tearing down cpplib's internal structures
1202 with cpp_destroy (). */
1203 cpp_finish (parse_in, deps_stream);
1205 if (deps_stream && deps_stream != out_stream
1206 && (ferror (deps_stream) || fclose (deps_stream)))
1207 fatal_error ("closing dependency file %s: %m", deps_file);
1209 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1210 fatal_error ("when writing output to %s: %m", out_fname);
1213 /* Either of two environment variables can specify output of
1214 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1215 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1216 and DEPS_TARGET is the target to mention in the deps. They also
1217 result in dependency information being appended to the output file
1218 rather than overwriting it, and like Sun's compiler
1219 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1221 check_deps_environment_vars (void)
1225 GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1227 cpp_opts->deps.style = DEPS_USER;
1230 GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1233 cpp_opts->deps.style = DEPS_SYSTEM;
1234 cpp_opts->deps.ignore_main_file = true;
1240 /* Find the space before the DEPS_TARGET, if there is one. */
1241 char *s = strchr (spec, ' ');
1244 /* Let the caller perform MAKE quoting. */
1245 defer_opt (OPT_MT, s + 1);
1249 /* Command line -MF overrides environment variables and default. */
1258 /* Handle deferred command line switches. */
1260 handle_deferred_opts (void)
1265 /* Avoid allocating the deps buffer if we don't need it.
1266 (This flag may be true without there having been -MT or -MQ
1267 options, but we'll still need the deps buffer.) */
1271 deps = cpp_get_deps (parse_in);
1273 for (i = 0; i < deferred_count; i++)
1275 struct deferred_opt *opt = &deferred_opts[i];
1277 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1278 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
1282 /* These settings are appropriate for GCC, but not necessarily so for
1283 cpplib as a library. */
1285 sanitize_cpp_opts (void)
1287 /* If we don't know what style of dependencies to output, complain
1288 if any other dependency switches have been given. */
1289 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1290 error ("to generate dependencies you must specify either -M or -MM");
1292 /* -dM and dependencies suppress normal output; do it here so that
1293 the last -d[MDN] switch overrides earlier ones. */
1294 if (flag_dump_macros == 'M')
1297 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1298 to perform proper macro expansion. */
1299 if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
1300 flag_dump_macros = 'D';
1302 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1303 -dM since at least glibc relies on -M -dM to work. */
1304 /* Also, flag_no_output implies flag_no_line_commands, always. */
1307 if (flag_dump_macros != 'M')
1308 flag_dump_macros = 0;
1309 flag_dump_includes = 0;
1310 flag_no_line_commands = 1;
1312 else if (cpp_opts->deps.missing_files)
1313 error ("-MG may only be used with -M or -MM");
1315 cpp_opts->unsigned_char = !flag_signed_char;
1316 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1318 /* Wlong-long is disabled by default. It is enabled by:
1319 [-pedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1320 [-pedantic | -Wtraditional] -std=non-c99 .
1322 Either -Wlong-long or -Wno-long-long override any other settings. */
1323 if (warn_long_long == -1)
1324 warn_long_long = ((pedantic || warn_traditional)
1325 && (c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99));
1326 cpp_opts->cpp_warn_long_long = warn_long_long;
1328 /* Similarly with -Wno-variadic-macros. No check for c99 here, since
1329 this also turns off warnings about GCCs extension. */
1330 cpp_opts->warn_variadic_macros
1331 = warn_variadic_macros && (pedantic || warn_traditional);
1333 /* If we're generating preprocessor output, emit current directory
1334 if explicitly requested or if debugging information is enabled.
1335 ??? Maybe we should only do it for debugging formats that
1336 actually output the current directory? */
1337 if (flag_working_directory == -1)
1338 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1340 if (cpp_opts->directives_only)
1342 if (warn_unused_macros)
1343 error ("-fdirectives-only is incompatible with -Wunused_macros");
1344 if (cpp_opts->traditional)
1345 error ("-fdirectives-only is incompatible with -traditional");
1349 /* Add include path with a prefix at the front of its name. */
1351 add_prefixed_path (const char *suffix, size_t chain)
1355 size_t prefix_len, suffix_len;
1357 suffix_len = strlen (suffix);
1358 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1359 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1361 path = (char *) xmalloc (prefix_len + suffix_len + 1);
1362 memcpy (path, prefix, prefix_len);
1363 memcpy (path + prefix_len, suffix, suffix_len);
1364 path[prefix_len + suffix_len] = '\0';
1366 add_path (path, chain, 0, false);
1369 /* Handle -D, -U, -A, -imacros, and the first -include. */
1371 finish_options (void)
1373 if (!cpp_opts->preprocessed)
1377 cb_file_change (parse_in,
1378 linemap_add (line_table, LC_RENAME, 0,
1379 _("<built-in>"), 0));
1381 cpp_init_builtins (parse_in, flag_hosted);
1382 c_cpp_builtins (parse_in);
1384 /* We're about to send user input to cpplib, so make it warn for
1385 things that we previously (when we sent it internal definitions)
1386 told it to not warn.
1388 C99 permits implementation-defined characters in identifiers.
1389 The documented meaning of -std= is to turn off extensions that
1390 conflict with the specified standard, and since a strictly
1391 conforming program cannot contain a '$', we do not condition
1392 their acceptance on the -std= setting. */
1393 cpp_opts->warn_dollars = (cpp_opts->cpp_pedantic && !cpp_opts->c99);
1395 cb_file_change (parse_in,
1396 linemap_add (line_table, LC_RENAME, 0,
1397 _("<command-line>"), 0));
1399 for (i = 0; i < deferred_count; i++)
1401 struct deferred_opt *opt = &deferred_opts[i];
1403 if (opt->code == OPT_D)
1404 cpp_define (parse_in, opt->arg);
1405 else if (opt->code == OPT_U)
1406 cpp_undef (parse_in, opt->arg);
1407 else if (opt->code == OPT_A)
1409 if (opt->arg[0] == '-')
1410 cpp_unassert (parse_in, opt->arg + 1);
1412 cpp_assert (parse_in, opt->arg);
1416 /* Start the main input file, if the debug writer wants it. */
1417 if (debug_hooks->start_end_main_source_file
1418 && !flag_preprocess_only)
1419 (*debug_hooks->start_source_file) (0, this_input_filename);
1421 /* Handle -imacros after -D and -U. */
1422 for (i = 0; i < deferred_count; i++)
1424 struct deferred_opt *opt = &deferred_opts[i];
1426 if (opt->code == OPT_imacros
1427 && cpp_push_include (parse_in, opt->arg))
1429 /* Disable push_command_line_include callback for now. */
1430 include_cursor = deferred_count + 1;
1431 cpp_scan_nooutput (parse_in);
1437 if (cpp_opts->directives_only)
1438 cpp_init_special_builtins (parse_in);
1440 /* Start the main input file, if the debug writer wants it. */
1441 if (debug_hooks->start_end_main_source_file
1442 && !flag_preprocess_only)
1443 (*debug_hooks->start_source_file) (0, this_input_filename);
1447 push_command_line_include ();
1450 /* Give CPP the next file given by -include, if any. */
1452 push_command_line_include (void)
1454 while (include_cursor < deferred_count)
1456 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1458 if (!cpp_opts->preprocessed && opt->code == OPT_include
1459 && cpp_push_include (parse_in, opt->arg))
1463 if (include_cursor == deferred_count)
1466 /* -Wunused-macros should only warn about macros defined hereafter. */
1467 cpp_opts->warn_unused_macros = warn_unused_macros;
1468 /* Restore the line map from <command line>. */
1469 if (!cpp_opts->preprocessed)
1470 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
1472 /* Set this here so the client can change the option if it wishes,
1473 and after stacking the main file so we don't trace the main file. */
1474 line_table->trace_includes = cpp_opts->print_include_names;
1478 /* File change callback. Has to handle -include files. */
1480 cb_file_change (cpp_reader * ARG_UNUSED (pfile),
1481 const struct line_map *new_map)
1483 if (flag_preprocess_only)
1484 pp_file_change (new_map);
1486 fe_file_change (new_map);
1488 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1489 push_command_line_include ();
1493 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
1495 if (!set_src_pwd (dir))
1496 warning (0, "too late for # directive to set debug directory");
1499 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1500 extensions if ISO). There is no concept of gnu94. */
1502 set_std_c89 (int c94, int iso)
1504 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1507 flag_no_gnu_keywords = iso;
1508 flag_no_nonansi_builtin = iso;
1514 /* Set the C 99 standard (without GNU extensions if ISO). */
1516 set_std_c99 (int iso)
1518 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1520 flag_no_nonansi_builtin = iso;
1527 /* Set the C 1X standard draft (without GNU extensions if ISO). */
1529 set_std_c1x (int iso)
1531 cpp_set_lang (parse_in, iso ? CLK_STDC1X: CLK_GNUC1X);
1533 flag_no_nonansi_builtin = iso;
1540 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1542 set_std_cxx98 (int iso)
1544 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1545 flag_no_gnu_keywords = iso;
1546 flag_no_nonansi_builtin = iso;
1548 cxx_dialect = cxx98;
1551 /* Set the C++ 0x working draft "standard" (without GNU extensions if ISO). */
1553 set_std_cxx0x (int iso)
1555 cpp_set_lang (parse_in, iso ? CLK_CXX0X: CLK_GNUCXX0X);
1556 flag_no_gnu_keywords = iso;
1557 flag_no_nonansi_builtin = iso;
1559 cxx_dialect = cxx0x;
1562 /* Args to -d specify what to dump. Silently ignore
1563 unrecognized options; they may be aimed at toplev.c. */
1565 handle_OPT_d (const char *arg)
1569 while ((c = *arg++) != '\0')
1572 case 'M': /* Dump macros only. */
1573 case 'N': /* Dump names. */
1574 case 'D': /* Dump definitions. */
1575 case 'U': /* Dump used macros. */
1576 flag_dump_macros = c;
1580 flag_dump_includes = 1;