1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
3 2012 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. */
40 #include "tm.h" /* For BYTES_BIG_ENDIAN,
41 DOLLARS_IN_IDENTIFIERS,
42 STDC_0_IN_SYSTEM_HEADERS,
43 TARGET_FLT_EVAL_METHOD_NON_DEFAULT and
45 #include "tm_p.h" /* For C_COMMON_OVERRIDE_OPTIONS. */
47 #ifndef DOLLARS_IN_IDENTIFIERS
48 # define DOLLARS_IN_IDENTIFIERS true
51 #ifndef TARGET_SYSTEM_ROOT
52 # define TARGET_SYSTEM_ROOT NULL
56 #define TARGET_OPTF(ARG)
60 cpp_options
*cpp_opts
;
63 static const char *this_input_filename
;
65 /* Filename and stream for preprocessed output. */
66 static const char *out_fname
;
67 static FILE *out_stream
;
69 /* Append dependencies to deps_file. */
70 static bool deps_append
;
72 /* If dependency switches (-MF etc.) have been given. */
73 static bool deps_seen
;
78 /* Dependency output file. */
79 static const char *deps_file
;
81 /* The prefix given by -iprefix, if any. */
82 static const char *iprefix
;
84 /* The multilib directory given by -imultilib, if any. */
85 static const char *imultilib
;
87 /* The system root, if any. Overridden by -isysroot. */
88 static const char *sysroot
= TARGET_SYSTEM_ROOT
;
90 /* Zero disables all standard directories for headers. */
91 static bool std_inc
= true;
93 /* Zero disables the C++-specific standard directories for headers. */
94 static bool std_cxx_inc
= true;
96 /* If the quote chain has been split by -I-. */
97 static bool quote_chain_split
;
99 /* If -Wunused-macros. */
100 static bool warn_unused_macros
;
102 /* If -Wvariadic-macros. */
103 static bool warn_variadic_macros
= true;
105 /* Number of deferred options. */
106 static size_t deferred_count
;
108 /* Number of deferred options scanned for -include. */
109 static size_t include_cursor
;
111 static void handle_OPT_d (const char *);
112 static void set_std_cxx98 (int);
113 static void set_std_cxx11 (int);
114 static void set_std_cxx1y (int);
115 static void set_std_c89 (int, int);
116 static void set_std_c99 (int);
117 static void set_std_c11 (int);
118 static void check_deps_environment_vars (void);
119 static void handle_deferred_opts (void);
120 static void sanitize_cpp_opts (void);
121 static void add_prefixed_path (const char *, size_t);
122 static void push_command_line_include (void);
123 static void cb_file_change (cpp_reader
*, const struct line_map
*);
124 static void cb_dir_change (cpp_reader
*, const char *);
125 static void c_finish_options (void);
127 #ifndef STDC_0_IN_SYSTEM_HEADERS
128 #define STDC_0_IN_SYSTEM_HEADERS 0
131 /* Holds switches parsed by c_common_handle_option (), but whose
132 handling is deferred to c_common_post_options (). */
133 static void defer_opt (enum opt_code
, const char *);
134 static struct deferred_opt
141 extern const unsigned int
142 c_family_lang_mask
= (CL_C
| CL_CXX
| CL_ObjC
| CL_ObjCXX
);
144 /* Defer option CODE with argument ARG. */
146 defer_opt (enum opt_code code
, const char *arg
)
148 deferred_opts
[deferred_count
].code
= code
;
149 deferred_opts
[deferred_count
].arg
= arg
;
153 /* Return language mask for option parsing. */
155 c_common_option_lang_mask (void)
157 static const unsigned int lang_flags
[] = {CL_C
, CL_ObjC
, CL_CXX
, CL_ObjCXX
};
159 return lang_flags
[c_language
];
162 /* Common diagnostics initialization. */
164 c_common_initialize_diagnostics (diagnostic_context
*context
)
166 /* This is conditionalized only because that is the way the front
167 ends used to do it. Maybe this should be unconditional? */
168 if (c_dialect_cxx ())
170 /* By default wrap lines at 80 characters. Is getenv
171 ("COLUMNS") preferable? */
172 diagnostic_line_cutoff (context
) = 80;
173 /* By default, emit location information once for every
174 diagnostic message. */
175 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
178 context
->opt_permissive
= OPT_fpermissive
;
181 /* Whether options from all C-family languages should be accepted
183 static bool accept_all_c_family_options
= false;
185 /* Return whether to complain about a wrong-language option. */
187 c_common_complain_wrong_lang_p (const struct cl_option
*option
)
189 if (accept_all_c_family_options
190 && (option
->flags
& c_family_lang_mask
))
196 /* Initialize options structure OPTS. */
198 c_common_init_options_struct (struct gcc_options
*opts
)
200 opts
->x_flag_exceptions
= c_dialect_cxx ();
201 opts
->x_warn_pointer_arith
= c_dialect_cxx ();
202 opts
->x_warn_write_strings
= c_dialect_cxx ();
203 opts
->x_flag_warn_unused_result
= true;
205 /* By default, C99-like requirements for complex multiply and divide. */
206 opts
->x_flag_complex_method
= 2;
209 /* Common initialization before calling option handlers. */
211 c_common_init_options (unsigned int decoded_options_count
,
212 struct cl_decoded_option
*decoded_options
)
215 struct cpp_callbacks
*cb
;
217 parse_in
= cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX
: CLK_GNUC89
,
218 ident_hash
, line_table
);
219 cb
= cpp_get_callbacks (parse_in
);
220 cb
->error
= c_cpp_error
;
222 cpp_opts
= cpp_get_options (parse_in
);
223 cpp_opts
->dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
;
224 cpp_opts
->objc
= c_dialect_objc ();
226 /* Reset to avoid warnings on internal definitions. We set it just
227 before passing on command-line options to cpplib. */
228 cpp_opts
->warn_dollars
= 0;
230 deferred_opts
= XNEWVEC (struct deferred_opt
, decoded_options_count
);
232 if (c_language
== clk_c
)
234 /* If preprocessing assembly language, accept any of the C-family
235 front end options since the driver may pass them through. */
236 for (i
= 1; i
< decoded_options_count
; i
++)
237 if (decoded_options
[i
].opt_index
== OPT_lang_asm
)
239 accept_all_c_family_options
= true;
245 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
246 form of an -f or -W option was given. Returns false if the switch was
247 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
249 c_common_handle_option (size_t scode
, const char *arg
, int value
,
250 int kind
, location_t loc
,
251 const struct cl_option_handlers
*handlers
)
253 const struct cl_option
*option
= &cl_options
[scode
];
254 enum opt_code code
= (enum opt_code
) scode
;
257 /* Prevent resetting the language standard to a C dialect when the driver
258 has already determined that we're looking at assembler input. */
259 bool preprocessing_asm_p
= (cpp_get_options (parse_in
)->lang
== CLK_ASM
);
264 if (cl_options
[code
].flags
& c_family_lang_mask
)
266 if ((option
->flags
& CL_TARGET
)
267 && ! targetcm
.handle_c_option (scode
, arg
, value
))
274 case OPT__output_pch_
:
279 defer_opt (code
, arg
);
283 cpp_opts
->discard_comments
= 0;
287 cpp_opts
->discard_comments
= 0;
288 cpp_opts
->discard_comments_in_macro_exp
= 0;
292 defer_opt (code
, arg
);
296 cpp_opts
->print_include_names
= 1;
300 TARGET_OPTF (xstrdup (arg
));
304 if (strcmp (arg
, "-"))
305 add_path (xstrdup (arg
), BRACKET
, 0, true);
308 if (quote_chain_split
)
309 error ("-I- specified twice");
310 quote_chain_split
= true;
311 split_quote_chain ();
312 inform (input_location
, "obsolete option -I- used, please use -iquote instead");
318 /* When doing dependencies with -M or -MM, suppress normal
319 preprocessed output, but still do -dM etc. as software
320 depends on this. Preprocessed output does occur if -MD, -MMD
321 or environment var dependency generation is used. */
322 cpp_opts
->deps
.style
= (code
== OPT_M
? DEPS_SYSTEM
: DEPS_USER
);
328 cpp_opts
->deps
.style
= (code
== OPT_MD
? DEPS_SYSTEM
: DEPS_USER
);
329 cpp_opts
->deps
.need_preprocessor_output
= true;
340 cpp_opts
->deps
.missing_files
= true;
345 cpp_opts
->deps
.phony_targets
= true;
351 defer_opt (code
, arg
);
355 flag_no_line_commands
= 1;
359 defer_opt (code
, arg
);
363 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
364 handle_generated_option (&global_options
, &global_options_set
,
365 OPT_Wunused
, NULL
, value
,
366 c_family_lang_mask
, kind
, loc
,
367 handlers
, global_dc
);
369 handle_generated_option (&global_options
, &global_options_set
,
370 OPT_Wimplicit
, NULL
, value
,
371 c_family_lang_mask
, kind
, loc
,
372 handlers
, global_dc
);
373 warn_char_subscripts
= value
;
374 warn_parentheses
= value
;
375 warn_return_type
= value
;
376 warn_sequence_point
= value
; /* Was C only. */
378 warn_sizeof_pointer_memaccess
= value
;
379 warn_address
= value
;
380 warn_array_bounds
= value
;
381 warn_volatile_register_var
= value
;
383 /* Only warn about unknown pragmas that are not in system
385 warn_unknown_pragmas
= value
;
387 warn_uninitialized
= value
;
388 warn_maybe_uninitialized
= value
;
390 if (!c_dialect_cxx ())
392 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
393 can turn it off only if it's not explicit. */
395 warn_main
= (value
? 2 : 0);
397 /* In C, -Wall and -Wc++-compat turns on -Wenum-compare,
398 which we do here. In C++ it is on by default, which is
399 done in c_common_post_options. */
400 if (warn_enum_compare
== -1)
401 warn_enum_compare
= value
;
405 /* C++-specific warnings. */
406 warn_sign_compare
= value
;
407 warn_narrowing
= value
;
410 cpp_opts
->warn_trigraphs
= value
;
411 cpp_opts
->warn_comments
= value
;
412 cpp_opts
->warn_num_sign_change
= value
;
414 if (warn_pointer_sign
== -1)
415 warn_pointer_sign
= value
;
418 case OPT_Wbuiltin_macro_redefined
:
419 cpp_opts
->warn_builtin_macro_redefined
= value
;
423 cpp_opts
->warn_comments
= value
;
426 case OPT_Wc___compat
:
427 /* Because -Wenum-compare is the default in C++, -Wc++-compat
428 implies -Wenum-compare. */
429 if (warn_enum_compare
== -1 && value
)
430 warn_enum_compare
= value
;
431 cpp_opts
->warn_cxx_operator_names
= value
;
434 case OPT_Wc__0x_compat
:
435 warn_narrowing
= value
;
438 case OPT_Wdeprecated
:
439 cpp_opts
->cpp_warn_deprecated
= value
;
442 case OPT_Wendif_labels
:
443 cpp_opts
->warn_endif_labels
= value
;
451 set_Wformat (atoi (arg
));
455 gcc_assert (value
== 0 || value
== 1);
456 if (warn_implicit_int
== -1)
457 handle_generated_option (&global_options
, &global_options_set
,
458 OPT_Wimplicit_int
, NULL
, value
,
459 c_family_lang_mask
, kind
, loc
, handlers
,
461 if (warn_implicit_function_declaration
== -1)
462 handle_generated_option (&global_options
, &global_options_set
,
463 OPT_Wimplicit_function_declaration
, NULL
,
464 value
, c_family_lang_mask
, kind
, loc
,
465 handlers
, global_dc
);
468 case OPT_Winvalid_pch
:
469 cpp_opts
->warn_invalid_pch
= value
;
472 case OPT_Wliteral_suffix
:
473 cpp_opts
->warn_literal_suffix
= value
;
477 cpp_opts
->cpp_warn_long_long
= value
;
480 case OPT_Wmissing_include_dirs
:
481 cpp_opts
->warn_missing_include_dirs
= value
;
485 cpp_opts
->warn_multichar
= value
;
488 case OPT_Wnormalized_
:
489 if (kind
== DK_ERROR
)
492 inform (input_location
, "-Werror=normalized=: set -Wnormalized=nfc");
493 cpp_opts
->warn_normalize
= normalized_C
;
497 if (!value
|| (arg
&& strcasecmp (arg
, "none") == 0))
498 cpp_opts
->warn_normalize
= normalized_none
;
499 else if (!arg
|| strcasecmp (arg
, "nfkc") == 0)
500 cpp_opts
->warn_normalize
= normalized_KC
;
501 else if (strcasecmp (arg
, "id") == 0)
502 cpp_opts
->warn_normalize
= normalized_identifier_C
;
503 else if (strcasecmp (arg
, "nfc") == 0)
504 cpp_opts
->warn_normalize
= normalized_C
;
506 error ("argument %qs to %<-Wnormalized%> not recognized", arg
);
510 case OPT_Wtraditional
:
511 cpp_opts
->cpp_warn_traditional
= value
;
515 cpp_opts
->warn_trigraphs
= value
;
519 cpp_opts
->warn_undef
= value
;
522 case OPT_Wunknown_pragmas
:
523 /* Set to greater than 1, so that even unknown pragmas in
524 system headers will be warned about. */
525 warn_unknown_pragmas
= value
* 2;
528 case OPT_Wunused_macros
:
529 warn_unused_macros
= value
;
532 case OPT_Wvariadic_macros
:
533 warn_variadic_macros
= value
;
538 warn_nonvdtor
= true;
542 if (!c_dialect_cxx ())
543 set_std_c89 (false, true);
545 set_std_cxx98 (true);
552 case OPT_fcond_mismatch
:
553 if (!c_dialect_cxx ())
555 flag_cond_mismatch
= value
;
558 warning (0, "switch %qs is no longer supported", option
->opt_text
);
565 disable_builtin_function (arg
);
568 case OPT_fdirectives_only
:
569 cpp_opts
->directives_only
= value
;
572 case OPT_fdollars_in_identifiers
:
573 cpp_opts
->dollars_in_ident
= value
;
576 case OPT_ffreestanding
:
578 /* Fall through.... */
581 flag_no_builtin
= !value
;
584 case OPT_fconstant_string_class_
:
585 constant_string_class_name
= arg
;
588 case OPT_fextended_identifiers
:
589 cpp_opts
->extended_identifiers
= value
;
592 case OPT_foperator_names
:
593 cpp_opts
->operator_names
= value
;
597 cpp_opts
->restore_pch_deps
= value
;
600 case OPT_fpch_preprocess
:
601 flag_pch_preprocess
= value
;
604 case OPT_fpermissive
:
605 flag_permissive
= value
;
606 global_dc
->permissive
= value
;
609 case OPT_fpreprocessed
:
610 cpp_opts
->preprocessed
= value
;
617 case OPT_ftrack_macro_expansion
:
622 case OPT_ftrack_macro_expansion_
:
623 if (arg
&& *arg
!= '\0')
624 cpp_opts
->track_macro_expansion
= value
;
626 cpp_opts
->track_macro_expansion
= 2;
630 flag_use_repository
= value
;
632 flag_implicit_templates
= 0;
636 /* It is documented that we silently ignore silly values. */
637 if (value
>= 1 && value
<= 100)
638 cpp_opts
->tabstop
= value
;
641 case OPT_fexec_charset_
:
642 cpp_opts
->narrow_charset
= arg
;
645 case OPT_fwide_exec_charset_
:
646 cpp_opts
->wide_charset
= arg
;
649 case OPT_finput_charset_
:
650 cpp_opts
->input_charset
= arg
;
653 case OPT_ftemplate_depth_
:
654 max_tinst_depth
= value
;
657 case OPT_fvisibility_inlines_hidden
:
658 visibility_options
.inlines_hidden
= value
;
661 case OPT_femit_struct_debug_baseonly
:
662 set_struct_debug_option (&global_options
, loc
, "base");
665 case OPT_femit_struct_debug_reduced
:
666 set_struct_debug_option (&global_options
, loc
,
667 "dir:ord:sys,dir:gen:any,ind:base");
670 case OPT_femit_struct_debug_detailed_
:
671 set_struct_debug_option (&global_options
, loc
, arg
);
675 add_path (xstrdup (arg
), AFTER
, 0, true);
680 defer_opt (code
, arg
);
692 add_path (xstrdup (arg
), QUOTE
, 0, true);
700 add_path (xstrdup (arg
), SYSTEM
, 0, true);
703 case OPT_iwithprefix
:
704 add_prefixed_path (arg
, SYSTEM
);
707 case OPT_iwithprefixbefore
:
708 add_prefixed_path (arg
, BRACKET
);
712 cpp_set_lang (parse_in
, CLK_ASM
);
713 cpp_opts
->dollars_in_ident
= false;
728 error ("output filename specified twice");
731 /* We need to handle the -Wpedantic switch here, rather than in
732 c_common_post_options, so that a subsequent -Wno-endif-labels
733 is not overridden. */
735 cpp_opts
->cpp_pedantic
= 1;
736 cpp_opts
->warn_endif_labels
= 1;
737 if (warn_pointer_sign
== -1)
738 warn_pointer_sign
= 1;
739 if (warn_overlength_strings
== -1)
740 warn_overlength_strings
= 1;
745 case OPT_print_objc_runtime_info
:
746 print_struct_values
= 1;
754 case OPT_std_gnu__98
:
755 if (!preprocessing_asm_p
)
756 set_std_cxx98 (code
== OPT_std_c__98
/* ISO */);
760 case OPT_std_gnu__11
:
761 if (!preprocessing_asm_p
)
762 set_std_cxx11 (code
== OPT_std_c__11
/* ISO */);
766 case OPT_std_gnu__1y
:
767 if (!preprocessing_asm_p
)
768 set_std_cxx1y (code
== OPT_std_c__11
/* ISO */);
772 case OPT_std_iso9899_199409
:
773 if (!preprocessing_asm_p
)
774 set_std_c89 (code
== OPT_std_iso9899_199409
/* c94 */, true /* ISO */);
778 if (!preprocessing_asm_p
)
779 set_std_c89 (false /* c94 */, false /* ISO */);
783 if (!preprocessing_asm_p
)
784 set_std_c99 (true /* ISO */);
788 if (!preprocessing_asm_p
)
789 set_std_c99 (false /* ISO */);
793 if (!preprocessing_asm_p
)
794 set_std_c11 (true /* ISO */);
798 if (!preprocessing_asm_p
)
799 set_std_c11 (false /* ISO */);
803 cpp_opts
->trigraphs
= 1;
806 case OPT_traditional_cpp
:
807 cpp_opts
->traditional
= 1;
822 C_handle_option_auto (&global_options
, &global_options_set
,
824 c_family_lang_mask
, kind
,
825 loc
, handlers
, global_dc
);
829 ObjC_handle_option_auto (&global_options
, &global_options_set
,
831 c_family_lang_mask
, kind
,
832 loc
, handlers
, global_dc
);
836 CXX_handle_option_auto (&global_options
, &global_options_set
,
838 c_family_lang_mask
, kind
,
839 loc
, handlers
, global_dc
);
843 ObjCXX_handle_option_auto (&global_options
, &global_options_set
,
845 c_family_lang_mask
, kind
,
846 loc
, handlers
, global_dc
);
856 /* Default implementation of TARGET_HANDLE_C_OPTION. */
859 default_handle_c_option (size_t code ATTRIBUTE_UNUSED
,
860 const char *arg ATTRIBUTE_UNUSED
,
861 int value ATTRIBUTE_UNUSED
)
866 /* Post-switch processing. */
868 c_common_post_options (const char **pfilename
)
870 struct cpp_callbacks
*cb
;
872 /* Canonicalize the input and output filenames. */
873 if (in_fnames
== NULL
)
875 in_fnames
= XNEWVEC (const char *, 1);
878 else if (strcmp (in_fnames
[0], "-") == 0)
881 if (out_fname
== NULL
|| !strcmp (out_fname
, "-"))
884 if (cpp_opts
->deps
.style
== DEPS_NONE
)
885 check_deps_environment_vars ();
887 handle_deferred_opts ();
889 sanitize_cpp_opts ();
891 register_include_chains (parse_in
, sysroot
, iprefix
, imultilib
,
892 std_inc
, std_cxx_inc
&& c_dialect_cxx (), verbose
);
894 #ifdef C_COMMON_OVERRIDE_OPTIONS
895 /* Some machines may reject certain combinations of C
896 language-specific options. */
897 C_COMMON_OVERRIDE_OPTIONS
;
900 /* Excess precision other than "fast" requires front-end
902 if (c_dialect_cxx ())
904 if (flag_excess_precision_cmdline
== EXCESS_PRECISION_STANDARD
905 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT
)
906 sorry ("-fexcess-precision=standard for C++");
907 flag_excess_precision_cmdline
= EXCESS_PRECISION_FAST
;
909 else if (flag_excess_precision_cmdline
== EXCESS_PRECISION_DEFAULT
)
910 flag_excess_precision_cmdline
= (flag_iso
911 ? EXCESS_PRECISION_STANDARD
912 : EXCESS_PRECISION_FAST
);
914 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
915 inline semantics are not supported in GNU89 or C89 mode. */
916 if (flag_gnu89_inline
== -1)
917 flag_gnu89_inline
= !flag_isoc99
;
918 else if (!flag_gnu89_inline
&& !flag_isoc99
)
919 error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode");
921 /* Default to ObjC sjlj exception handling if NeXT runtime. */
922 if (flag_objc_sjlj_exceptions
< 0)
923 flag_objc_sjlj_exceptions
= flag_next_runtime
;
924 if (flag_objc_exceptions
&& !flag_objc_sjlj_exceptions
)
927 /* -Wextra implies the following flags
928 unless explicitly overridden. */
930 /* Wsign-compare is also enabled by -Wall in C++. */
931 if (warn_sign_compare
== -1)
932 warn_sign_compare
= extra_warnings
;
934 /* -Wpointer-sign is disabled by default, but it is enabled if any
935 of -Wall or -Wpedantic are given. */
936 if (warn_pointer_sign
== -1)
937 warn_pointer_sign
= 0;
939 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
940 It is never enabled in C++, as the minimum limit is not normative
942 if (warn_overlength_strings
== -1 || c_dialect_cxx ())
943 warn_overlength_strings
= 0;
945 /* Wmain is enabled by default in C++ but not in C. */
946 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
947 even if -Wall was given (warn_main will be 2 if set by -Wall, 1
948 if set by -Wmain). */
950 warn_main
= (c_dialect_cxx () && flag_hosted
) ? 1 : 0;
951 else if (warn_main
== 2)
952 warn_main
= flag_hosted
? 1 : 0;
954 /* In C, -Wconversion enables -Wsign-conversion (unless disabled
955 through -Wno-sign-conversion). While in C++,
956 -Wsign-conversion needs to be requested explicitly. */
957 if (warn_sign_conversion
== -1)
958 warn_sign_conversion
= (c_dialect_cxx ()) ? 0 : warn_conversion
;
960 /* In C, -Wall and -Wc++-compat enable -Wenum-compare, which we do
961 in c_common_handle_option; if it has not yet been set, it is
962 disabled by default. In C++, it is enabled by default. */
963 if (warn_enum_compare
== -1)
964 warn_enum_compare
= c_dialect_cxx () ? 1 : 0;
966 /* -Wpacked-bitfield-compat is on by default for the C languages. The
967 warning is issued in stor-layout.c which is not part of the front-end so
968 we need to selectively turn it on here. */
969 if (warn_packed_bitfield_compat
== -1)
970 warn_packed_bitfield_compat
= 1;
972 /* Special format checking options don't work without -Wformat; warn if
976 warning (OPT_Wformat_y2k
,
977 "-Wformat-y2k ignored without -Wformat");
978 warning (OPT_Wformat_extra_args
,
979 "-Wformat-extra-args ignored without -Wformat");
980 warning (OPT_Wformat_zero_length
,
981 "-Wformat-zero-length ignored without -Wformat");
982 warning (OPT_Wformat_nonliteral
,
983 "-Wformat-nonliteral ignored without -Wformat");
984 warning (OPT_Wformat_contains_nul
,
985 "-Wformat-contains-nul ignored without -Wformat");
986 warning (OPT_Wformat_security
,
987 "-Wformat-security ignored without -Wformat");
990 if (warn_implicit
== -1)
993 if (warn_implicit_int
== -1)
994 warn_implicit_int
= 0;
996 /* -Wimplicit-function-declaration is enabled by default for C99. */
997 if (warn_implicit_function_declaration
== -1)
998 warn_implicit_function_declaration
= flag_isoc99
;
1000 if (cxx_dialect
>= cxx0x
)
1002 /* If we're allowing C++0x constructs, don't warn about C++98
1003 identifiers which are keywords in C++0x. */
1004 warn_cxx0x_compat
= 0;
1006 if (warn_narrowing
== -1)
1009 else if (warn_narrowing
== -1)
1012 if (flag_preprocess_only
)
1014 /* Open the output now. We must do so even if flag_no_output is
1015 on, because there may be other output than from the actual
1016 preprocessing (e.g. from -dM). */
1017 if (out_fname
[0] == '\0')
1018 out_stream
= stdout
;
1020 out_stream
= fopen (out_fname
, "w");
1022 if (out_stream
== NULL
)
1024 fatal_error ("opening output file %s: %m", out_fname
);
1028 if (num_in_fnames
> 1)
1029 error ("too many filenames given. Type %s --help for usage",
1032 init_pp_output (out_stream
);
1038 /* When writing a PCH file, avoid reading some other PCH file,
1039 because the default address space slot then can't be used
1040 for the output PCH file. */
1042 c_common_no_more_pch ();
1044 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1045 input_location
= UNKNOWN_LOCATION
;
1048 cb
= cpp_get_callbacks (parse_in
);
1049 cb
->file_change
= cb_file_change
;
1050 cb
->dir_change
= cb_dir_change
;
1051 cpp_post_options (parse_in
);
1053 input_location
= UNKNOWN_LOCATION
;
1055 *pfilename
= this_input_filename
1056 = cpp_read_main_file (parse_in
, in_fnames
[0]);
1057 /* Don't do any compilation or preprocessing if there is no input file. */
1058 if (this_input_filename
== NULL
)
1064 if (flag_working_directory
1065 && flag_preprocess_only
&& !flag_no_line_commands
)
1066 pp_dir_change (parse_in
, get_src_pwd ());
1068 /* Disable LTO output when outputting a precompiled header. */
1069 if (pch_file
&& flag_lto
)
1072 flag_generate_lto
= 0;
1075 return flag_preprocess_only
;
1078 /* Front end initialization common to C, ObjC and C++. */
1080 c_common_init (void)
1082 /* Set up preprocessor arithmetic. Must be done after call to
1083 c_common_nodes_and_builtins for type nodes to be good. */
1084 cpp_opts
->precision
= TYPE_PRECISION (intmax_type_node
);
1085 cpp_opts
->char_precision
= TYPE_PRECISION (char_type_node
);
1086 cpp_opts
->int_precision
= TYPE_PRECISION (integer_type_node
);
1087 cpp_opts
->wchar_precision
= TYPE_PRECISION (wchar_type_node
);
1088 cpp_opts
->unsigned_wchar
= TYPE_UNSIGNED (wchar_type_node
);
1089 cpp_opts
->bytes_big_endian
= BYTES_BIG_ENDIAN
;
1091 /* This can't happen until after wchar_precision and bytes_big_endian
1093 cpp_init_iconv (parse_in
);
1098 fputs ("Compiler executable checksum: ", stderr
);
1099 for (i
= 0; i
< 16; i
++)
1100 fprintf (stderr
, "%02x", executable_checksum
[i
]);
1101 putc ('\n', stderr
);
1104 /* Has to wait until now so that cpplib has its hash table. */
1107 if (flag_preprocess_only
)
1109 c_finish_options ();
1110 preprocess_file (parse_in
);
1117 /* Initialize the integrated preprocessor after debug output has been
1118 initialized; loop over each input file. */
1120 c_common_parse_file (void)
1127 c_finish_options ();
1132 /* And end the main input file, if the debug writer wants it */
1133 if (debug_hooks
->start_end_main_source_file
)
1134 (*debug_hooks
->end_source_file
) (0);
1135 if (++i
>= num_in_fnames
)
1137 cpp_undef_all (parse_in
);
1138 cpp_clear_file_cache (parse_in
);
1140 = cpp_read_main_file (parse_in
, in_fnames
[i
]);
1141 /* If an input file is missing, abandon further compilation.
1142 cpplib has issued a diagnostic. */
1143 if (!this_input_filename
)
1148 /* Common finish hook for the C, ObjC and C++ front ends. */
1150 c_common_finish (void)
1152 FILE *deps_stream
= NULL
;
1154 /* Don't write the deps file if there are errors. */
1155 if (cpp_opts
->deps
.style
!= DEPS_NONE
&& !seen_error ())
1157 /* If -M or -MM was seen without -MF, default output to the
1160 deps_stream
= out_stream
;
1163 deps_stream
= fopen (deps_file
, deps_append
? "a": "w");
1165 fatal_error ("opening dependency file %s: %m", deps_file
);
1169 /* For performance, avoid tearing down cpplib's internal structures
1170 with cpp_destroy (). */
1171 cpp_finish (parse_in
, deps_stream
);
1173 if (deps_stream
&& deps_stream
!= out_stream
1174 && (ferror (deps_stream
) || fclose (deps_stream
)))
1175 fatal_error ("closing dependency file %s: %m", deps_file
);
1177 if (out_stream
&& (ferror (out_stream
) || fclose (out_stream
)))
1178 fatal_error ("when writing output to %s: %m", out_fname
);
1181 /* Either of two environment variables can specify output of
1182 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1183 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1184 and DEPS_TARGET is the target to mention in the deps. They also
1185 result in dependency information being appended to the output file
1186 rather than overwriting it, and like Sun's compiler
1187 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1189 check_deps_environment_vars (void)
1193 spec
= getenv ("DEPENDENCIES_OUTPUT");
1195 cpp_opts
->deps
.style
= DEPS_USER
;
1198 spec
= getenv ("SUNPRO_DEPENDENCIES");
1201 cpp_opts
->deps
.style
= DEPS_SYSTEM
;
1202 cpp_opts
->deps
.ignore_main_file
= true;
1208 /* Find the space before the DEPS_TARGET, if there is one. */
1209 char *s
= strchr (spec
, ' ');
1212 /* Let the caller perform MAKE quoting. */
1213 defer_opt (OPT_MT
, s
+ 1);
1217 /* Command line -MF overrides environment variables and default. */
1226 /* Handle deferred command line switches. */
1228 handle_deferred_opts (void)
1233 /* Avoid allocating the deps buffer if we don't need it.
1234 (This flag may be true without there having been -MT or -MQ
1235 options, but we'll still need the deps buffer.) */
1239 deps
= cpp_get_deps (parse_in
);
1241 for (i
= 0; i
< deferred_count
; i
++)
1243 struct deferred_opt
*opt
= &deferred_opts
[i
];
1245 if (opt
->code
== OPT_MT
|| opt
->code
== OPT_MQ
)
1246 deps_add_target (deps
, opt
->arg
, opt
->code
== OPT_MQ
);
1250 /* These settings are appropriate for GCC, but not necessarily so for
1251 cpplib as a library. */
1253 sanitize_cpp_opts (void)
1255 /* If we don't know what style of dependencies to output, complain
1256 if any other dependency switches have been given. */
1257 if (deps_seen
&& cpp_opts
->deps
.style
== DEPS_NONE
)
1258 error ("to generate dependencies you must specify either -M or -MM");
1260 /* -dM and dependencies suppress normal output; do it here so that
1261 the last -d[MDN] switch overrides earlier ones. */
1262 if (flag_dump_macros
== 'M')
1265 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1266 to perform proper macro expansion. */
1267 if (cpp_opts
->directives_only
&& !cpp_opts
->preprocessed
&& !flag_dump_macros
)
1268 flag_dump_macros
= 'D';
1270 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1271 -dM since at least glibc relies on -M -dM to work. */
1272 /* Also, flag_no_output implies flag_no_line_commands, always. */
1275 if (flag_dump_macros
!= 'M')
1276 flag_dump_macros
= 0;
1277 flag_dump_includes
= 0;
1278 flag_no_line_commands
= 1;
1280 else if (cpp_opts
->deps
.missing_files
)
1281 error ("-MG may only be used with -M or -MM");
1283 cpp_opts
->unsigned_char
= !flag_signed_char
;
1284 cpp_opts
->stdc_0_in_system_headers
= STDC_0_IN_SYSTEM_HEADERS
;
1286 /* Wlong-long is disabled by default. It is enabled by:
1287 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1288 [-Wpedantic | -Wtraditional] -std=non-c99 .
1290 Either -Wlong-long or -Wno-long-long override any other settings. */
1291 if (warn_long_long
== -1)
1292 warn_long_long
= ((pedantic
|| warn_traditional
)
1293 && (c_dialect_cxx () ? cxx_dialect
== cxx98
: !flag_isoc99
));
1294 cpp_opts
->cpp_warn_long_long
= warn_long_long
;
1296 /* Similarly with -Wno-variadic-macros. No check for c99 here, since
1297 this also turns off warnings about GCCs extension. */
1298 cpp_opts
->warn_variadic_macros
1299 = warn_variadic_macros
&& (pedantic
|| warn_traditional
);
1301 /* If we're generating preprocessor output, emit current directory
1302 if explicitly requested or if debugging information is enabled.
1303 ??? Maybe we should only do it for debugging formats that
1304 actually output the current directory? */
1305 if (flag_working_directory
== -1)
1306 flag_working_directory
= (debug_info_level
!= DINFO_LEVEL_NONE
);
1308 if (cpp_opts
->directives_only
)
1310 if (warn_unused_macros
)
1311 error ("-fdirectives-only is incompatible with -Wunused_macros");
1312 if (cpp_opts
->traditional
)
1313 error ("-fdirectives-only is incompatible with -traditional");
1317 /* Add include path with a prefix at the front of its name. */
1319 add_prefixed_path (const char *suffix
, size_t chain
)
1323 size_t prefix_len
, suffix_len
;
1325 suffix_len
= strlen (suffix
);
1326 prefix
= iprefix
? iprefix
: cpp_GCC_INCLUDE_DIR
;
1327 prefix_len
= iprefix
? strlen (iprefix
) : cpp_GCC_INCLUDE_DIR_len
;
1329 path
= (char *) xmalloc (prefix_len
+ suffix_len
+ 1);
1330 memcpy (path
, prefix
, prefix_len
);
1331 memcpy (path
+ prefix_len
, suffix
, suffix_len
);
1332 path
[prefix_len
+ suffix_len
] = '\0';
1334 add_path (path
, chain
, 0, false);
1337 /* Handle -D, -U, -A, -imacros, and the first -include. */
1339 c_finish_options (void)
1341 if (!cpp_opts
->preprocessed
)
1346 /* Make sure all of the builtins about to be declared have
1347 BUILTINS_LOCATION has their source_location. */
1348 source_location builtins_loc
= BUILTINS_LOCATION
;
1349 cpp_force_token_locations (parse_in
, &builtins_loc
);
1351 cpp_init_builtins (parse_in
, flag_hosted
);
1352 c_cpp_builtins (parse_in
);
1354 cpp_stop_forcing_token_locations (parse_in
);
1357 /* We're about to send user input to cpplib, so make it warn for
1358 things that we previously (when we sent it internal definitions)
1359 told it to not warn.
1361 C99 permits implementation-defined characters in identifiers.
1362 The documented meaning of -std= is to turn off extensions that
1363 conflict with the specified standard, and since a strictly
1364 conforming program cannot contain a '$', we do not condition
1365 their acceptance on the -std= setting. */
1366 cpp_opts
->warn_dollars
= (cpp_opts
->cpp_pedantic
&& !cpp_opts
->c99
);
1368 cb_file_change (parse_in
,
1369 linemap_add (line_table
, LC_RENAME
, 0,
1370 _("<command-line>"), 0));
1372 for (i
= 0; i
< deferred_count
; i
++)
1374 struct deferred_opt
*opt
= &deferred_opts
[i
];
1376 if (opt
->code
== OPT_D
)
1377 cpp_define (parse_in
, opt
->arg
);
1378 else if (opt
->code
== OPT_U
)
1379 cpp_undef (parse_in
, opt
->arg
);
1380 else if (opt
->code
== OPT_A
)
1382 if (opt
->arg
[0] == '-')
1383 cpp_unassert (parse_in
, opt
->arg
+ 1);
1385 cpp_assert (parse_in
, opt
->arg
);
1389 /* Start the main input file, if the debug writer wants it. */
1390 if (debug_hooks
->start_end_main_source_file
1391 && !flag_preprocess_only
)
1392 (*debug_hooks
->start_source_file
) (0, this_input_filename
);
1394 /* Handle -imacros after -D and -U. */
1395 for (i
= 0; i
< deferred_count
; i
++)
1397 struct deferred_opt
*opt
= &deferred_opts
[i
];
1399 if (opt
->code
== OPT_imacros
1400 && cpp_push_include (parse_in
, opt
->arg
))
1402 /* Disable push_command_line_include callback for now. */
1403 include_cursor
= deferred_count
+ 1;
1404 cpp_scan_nooutput (parse_in
);
1410 if (cpp_opts
->directives_only
)
1411 cpp_init_special_builtins (parse_in
);
1413 /* Start the main input file, if the debug writer wants it. */
1414 if (debug_hooks
->start_end_main_source_file
1415 && !flag_preprocess_only
)
1416 (*debug_hooks
->start_source_file
) (0, this_input_filename
);
1420 push_command_line_include ();
1423 /* Give CPP the next file given by -include, if any. */
1425 push_command_line_include (void)
1427 while (include_cursor
< deferred_count
)
1429 struct deferred_opt
*opt
= &deferred_opts
[include_cursor
++];
1431 if (!cpp_opts
->preprocessed
&& opt
->code
== OPT_include
1432 && cpp_push_include (parse_in
, opt
->arg
))
1436 if (include_cursor
== deferred_count
)
1439 /* -Wunused-macros should only warn about macros defined hereafter. */
1440 cpp_opts
->warn_unused_macros
= warn_unused_macros
;
1441 /* Restore the line map from <command line>. */
1442 if (!cpp_opts
->preprocessed
)
1443 cpp_change_file (parse_in
, LC_RENAME
, this_input_filename
);
1445 /* Set this here so the client can change the option if it wishes,
1446 and after stacking the main file so we don't trace the main file. */
1447 line_table
->trace_includes
= cpp_opts
->print_include_names
;
1451 /* File change callback. Has to handle -include files. */
1453 cb_file_change (cpp_reader
* ARG_UNUSED (pfile
),
1454 const struct line_map
*new_map
)
1456 if (flag_preprocess_only
)
1457 pp_file_change (new_map
);
1459 fe_file_change (new_map
);
1461 if (new_map
== 0 || (new_map
->reason
== LC_LEAVE
&& MAIN_FILE_P (new_map
)))
1462 push_command_line_include ();
1466 cb_dir_change (cpp_reader
* ARG_UNUSED (pfile
), const char *dir
)
1468 if (!set_src_pwd (dir
))
1469 warning (0, "too late for # directive to set debug directory");
1472 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1473 extensions if ISO). There is no concept of gnu94. */
1475 set_std_c89 (int c94
, int iso
)
1477 cpp_set_lang (parse_in
, c94
? CLK_STDC94
: iso
? CLK_STDC89
: CLK_GNUC89
);
1480 flag_no_gnu_keywords
= iso
;
1481 flag_no_nonansi_builtin
= iso
;
1487 /* Set the C 99 standard (without GNU extensions if ISO). */
1489 set_std_c99 (int iso
)
1491 cpp_set_lang (parse_in
, iso
? CLK_STDC99
: CLK_GNUC99
);
1493 flag_no_nonansi_builtin
= iso
;
1500 /* Set the C 11 standard (without GNU extensions if ISO). */
1502 set_std_c11 (int iso
)
1504 cpp_set_lang (parse_in
, iso
? CLK_STDC11
: CLK_GNUC11
);
1506 flag_no_nonansi_builtin
= iso
;
1513 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1515 set_std_cxx98 (int iso
)
1517 cpp_set_lang (parse_in
, iso
? CLK_CXX98
: CLK_GNUCXX
);
1518 flag_no_gnu_keywords
= iso
;
1519 flag_no_nonansi_builtin
= iso
;
1521 cxx_dialect
= cxx98
;
1524 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1526 set_std_cxx11 (int iso
)
1528 cpp_set_lang (parse_in
, iso
? CLK_CXX11
: CLK_GNUCXX11
);
1529 flag_no_gnu_keywords
= iso
;
1530 flag_no_nonansi_builtin
= iso
;
1532 /* C++11 includes the C99 standard library. */
1535 cxx_dialect
= cxx11
;
1538 /* Set the C++ 201y draft standard (without GNU extensions if ISO). */
1540 set_std_cxx1y (int iso
)
1542 cpp_set_lang (parse_in
, iso
? CLK_CXX11
: CLK_GNUCXX11
);
1543 flag_no_gnu_keywords
= iso
;
1544 flag_no_nonansi_builtin
= iso
;
1546 /* C++11 includes the C99 standard library. */
1549 cxx_dialect
= cxx1y
;
1552 /* Args to -d specify what to dump. Silently ignore
1553 unrecognized options; they may be aimed at toplev.c. */
1555 handle_OPT_d (const char *arg
)
1559 while ((c
= *arg
++) != '\0')
1562 case 'M': /* Dump macros only. */
1563 case 'N': /* Dump names. */
1564 case 'D': /* Dump definitions. */
1565 case 'U': /* Dump used macros. */
1566 flag_dump_macros
= c
;
1570 flag_dump_includes
= 1;