1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002-2015 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
31 #include "langhooks.h"
32 #include "diagnostic.h"
33 #include "tree-diagnostic.h" /* for virt_loc_aware_diagnostic_finalizer */
35 #include "cppdefault.h"
37 #include "debug.h" /* For debug_hooks. */
39 #include "plugin.h" /* For PLUGIN_INCLUDE_FILE event. */
42 #include "tm.h" /* For BYTES_BIG_ENDIAN,
43 DOLLARS_IN_IDENTIFIERS,
44 STDC_0_IN_SYSTEM_HEADERS,
45 TARGET_FLT_EVAL_METHOD_NON_DEFAULT and
47 #include "tm_p.h" /* For C_COMMON_OVERRIDE_OPTIONS. */
50 #ifndef DOLLARS_IN_IDENTIFIERS
51 # define DOLLARS_IN_IDENTIFIERS true
54 #ifndef TARGET_SYSTEM_ROOT
55 # define TARGET_SYSTEM_ROOT NULL
59 #define TARGET_OPTF(ARG)
63 cpp_options
*cpp_opts
;
66 static const char *this_input_filename
;
68 /* Filename and stream for preprocessed output. */
69 static const char *out_fname
;
70 static FILE *out_stream
;
72 /* Append dependencies to deps_file. */
73 static bool deps_append
;
75 /* If dependency switches (-MF etc.) have been given. */
76 static bool deps_seen
;
81 /* Dependency output file. */
82 static const char *deps_file
;
84 /* The prefix given by -iprefix, if any. */
85 static const char *iprefix
;
87 /* The multilib directory given by -imultilib, if any. */
88 static const char *imultilib
;
90 /* The system root, if any. Overridden by -isysroot. */
91 static const char *sysroot
= TARGET_SYSTEM_ROOT
;
93 /* Zero disables all standard directories for headers. */
94 static bool std_inc
= true;
96 /* Zero disables the C++-specific standard directories for headers. */
97 static bool std_cxx_inc
= true;
99 /* If the quote chain has been split by -I-. */
100 static bool quote_chain_split
;
102 /* Number of deferred options. */
103 static size_t deferred_count
;
105 /* Number of deferred options scanned for -include. */
106 static size_t include_cursor
;
108 /* Dump files/flags to use during parsing. */
109 static FILE *original_dump_file
= NULL
;
110 static int original_dump_flags
;
111 static FILE *class_dump_file
= NULL
;
112 static int class_dump_flags
;
114 /* Whether any standard preincluded header has been preincluded. */
115 static bool done_preinclude
;
117 static void handle_OPT_d (const char *);
118 static void set_std_cxx98 (int);
119 static void set_std_cxx11 (int);
120 static void set_std_cxx14 (int);
121 static void set_std_cxx1z (int);
122 static void set_std_c89 (int, int);
123 static void set_std_c99 (int);
124 static void set_std_c11 (int);
125 static void check_deps_environment_vars (void);
126 static void handle_deferred_opts (void);
127 static void sanitize_cpp_opts (void);
128 static void add_prefixed_path (const char *, size_t);
129 static void push_command_line_include (void);
130 static void cb_file_change (cpp_reader
*, const line_map_ordinary
*);
131 static void cb_dir_change (cpp_reader
*, const char *);
132 static void c_finish_options (void);
134 #ifndef STDC_0_IN_SYSTEM_HEADERS
135 #define STDC_0_IN_SYSTEM_HEADERS 0
138 /* Holds switches parsed by c_common_handle_option (), but whose
139 handling is deferred to c_common_post_options (). */
140 static void defer_opt (enum opt_code
, const char *);
141 static struct deferred_opt
148 extern const unsigned int
149 c_family_lang_mask
= (CL_C
| CL_CXX
| CL_ObjC
| CL_ObjCXX
);
151 /* Defer option CODE with argument ARG. */
153 defer_opt (enum opt_code code
, const char *arg
)
155 deferred_opts
[deferred_count
].code
= code
;
156 deferred_opts
[deferred_count
].arg
= arg
;
160 /* Return language mask for option parsing. */
162 c_common_option_lang_mask (void)
164 static const unsigned int lang_flags
[] = {CL_C
, CL_ObjC
, CL_CXX
, CL_ObjCXX
};
166 return lang_flags
[c_language
];
169 /* Diagnostic finalizer for C/C++/Objective-C/Objective-C++. */
171 c_diagnostic_finalizer (diagnostic_context
*context
,
172 diagnostic_info
*diagnostic
)
174 diagnostic_show_locus (context
, diagnostic
);
175 /* By default print macro expansion contexts in the diagnostic
176 finalizer -- for tokens resulting from macro expansion. */
177 virt_loc_aware_diagnostic_finalizer (context
, diagnostic
);
178 pp_destroy_prefix (context
->printer
);
179 pp_newline_and_flush (context
->printer
);
182 /* Common default settings for diagnostics. */
184 c_common_diagnostics_set_defaults (diagnostic_context
*context
)
186 diagnostic_finalizer (context
) = c_diagnostic_finalizer
;
187 context
->opt_permissive
= OPT_fpermissive
;
190 /* Whether options from all C-family languages should be accepted
192 static bool accept_all_c_family_options
= false;
194 /* Return whether to complain about a wrong-language option. */
196 c_common_complain_wrong_lang_p (const struct cl_option
*option
)
198 if (accept_all_c_family_options
199 && (option
->flags
& c_family_lang_mask
))
205 /* Initialize options structure OPTS. */
207 c_common_init_options_struct (struct gcc_options
*opts
)
209 opts
->x_flag_exceptions
= c_dialect_cxx ();
210 opts
->x_warn_pointer_arith
= c_dialect_cxx ();
211 opts
->x_warn_write_strings
= c_dialect_cxx ();
212 opts
->x_flag_warn_unused_result
= true;
214 /* By default, C99-like requirements for complex multiply and divide. */
215 opts
->x_flag_complex_method
= 2;
218 /* Common initialization before calling option handlers. */
220 c_common_init_options (unsigned int decoded_options_count
,
221 struct cl_decoded_option
*decoded_options
)
224 struct cpp_callbacks
*cb
;
226 parse_in
= cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX
: CLK_GNUC89
,
227 ident_hash
, line_table
);
228 cb
= cpp_get_callbacks (parse_in
);
229 cb
->error
= c_cpp_error
;
231 cpp_opts
= cpp_get_options (parse_in
);
232 cpp_opts
->dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
;
233 cpp_opts
->objc
= c_dialect_objc ();
235 /* Reset to avoid warnings on internal definitions. We set it just
236 before passing on command-line options to cpplib. */
237 cpp_opts
->warn_dollars
= 0;
239 deferred_opts
= XNEWVEC (struct deferred_opt
, decoded_options_count
);
241 if (c_language
== clk_c
)
243 /* The default for C is gnu11. */
244 set_std_c11 (false /* ISO */);
246 /* If preprocessing assembly language, accept any of the C-family
247 front end options since the driver may pass them through. */
248 for (i
= 1; i
< decoded_options_count
; i
++)
249 if (decoded_options
[i
].opt_index
== OPT_lang_asm
)
251 accept_all_c_family_options
= true;
257 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
258 form of an -f or -W option was given. Returns false if the switch was
259 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
261 c_common_handle_option (size_t scode
, const char *arg
, int value
,
262 int kind
, location_t loc
,
263 const struct cl_option_handlers
*handlers
)
265 const struct cl_option
*option
= &cl_options
[scode
];
266 enum opt_code code
= (enum opt_code
) scode
;
269 /* Prevent resetting the language standard to a C dialect when the driver
270 has already determined that we're looking at assembler input. */
271 bool preprocessing_asm_p
= (cpp_get_options (parse_in
)->lang
== CLK_ASM
);
276 if (cl_options
[code
].flags
& c_family_lang_mask
)
278 if ((option
->flags
& CL_TARGET
)
279 && ! targetcm
.handle_c_option (scode
, arg
, value
))
286 case OPT__output_pch_
:
291 defer_opt (code
, arg
);
295 cpp_opts
->discard_comments
= 0;
299 cpp_opts
->discard_comments
= 0;
300 cpp_opts
->discard_comments_in_macro_exp
= 0;
304 defer_opt (code
, arg
);
308 cpp_opts
->print_include_names
= 1;
312 TARGET_OPTF (xstrdup (arg
));
316 if (strcmp (arg
, "-"))
317 add_path (xstrdup (arg
), BRACKET
, 0, true);
320 if (quote_chain_split
)
321 error ("-I- specified twice");
322 quote_chain_split
= true;
323 split_quote_chain ();
324 inform (input_location
, "obsolete option -I- used, please use -iquote instead");
330 /* When doing dependencies with -M or -MM, suppress normal
331 preprocessed output, but still do -dM etc. as software
332 depends on this. Preprocessed output does occur if -MD, -MMD
333 or environment var dependency generation is used. */
334 cpp_opts
->deps
.style
= (code
== OPT_M
? DEPS_SYSTEM
: DEPS_USER
);
340 cpp_opts
->deps
.style
= (code
== OPT_MD
? DEPS_SYSTEM
: DEPS_USER
);
341 cpp_opts
->deps
.need_preprocessor_output
= true;
352 cpp_opts
->deps
.missing_files
= true;
357 cpp_opts
->deps
.phony_targets
= true;
363 defer_opt (code
, arg
);
367 flag_no_line_commands
= 1;
371 defer_opt (code
, arg
);
375 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
377 cpp_opts
->warn_num_sign_change
= value
;
380 case OPT_Wunknown_pragmas
:
381 /* Set to greater than 1, so that even unknown pragmas in
382 system headers will be warned about. */
383 /* ??? There is no way to handle this automatically for now. */
384 warn_unknown_pragmas
= value
* 2;
388 if (!c_dialect_cxx ())
389 set_std_c89 (false, true);
391 set_std_cxx98 (true);
402 warning (0, "%<-Wabi=1%> is not supported, using =2");
405 flag_abi_compat_version
= value
;
408 case OPT_fcanonical_system_headers
:
409 cpp_opts
->canonical_system_headers
= value
;
412 case OPT_fcond_mismatch
:
413 if (!c_dialect_cxx ())
415 flag_cond_mismatch
= value
;
418 warning (0, "switch %qs is no longer supported", option
->opt_text
);
425 disable_builtin_function (arg
);
428 case OPT_fdirectives_only
:
429 cpp_opts
->directives_only
= value
;
432 case OPT_fdollars_in_identifiers
:
433 cpp_opts
->dollars_in_ident
= value
;
436 case OPT_ffreestanding
:
438 /* Fall through.... */
441 flag_no_builtin
= !value
;
444 case OPT_fconstant_string_class_
:
445 constant_string_class_name
= arg
;
448 case OPT_fextended_identifiers
:
449 cpp_opts
->extended_identifiers
= value
;
452 case OPT_foperator_names
:
453 cpp_opts
->operator_names
= value
;
457 cpp_opts
->restore_pch_deps
= value
;
460 case OPT_fpch_preprocess
:
461 flag_pch_preprocess
= value
;
464 case OPT_fpermissive
:
465 flag_permissive
= value
;
466 global_dc
->permissive
= value
;
469 case OPT_fpreprocessed
:
470 cpp_opts
->preprocessed
= value
;
477 case OPT_ftrack_macro_expansion
:
482 case OPT_ftrack_macro_expansion_
:
483 if (arg
&& *arg
!= '\0')
484 cpp_opts
->track_macro_expansion
= value
;
486 cpp_opts
->track_macro_expansion
= 2;
490 flag_use_repository
= value
;
492 flag_implicit_templates
= 0;
496 /* It is documented that we silently ignore silly values. */
497 if (value
>= 1 && value
<= 100)
498 cpp_opts
->tabstop
= value
;
501 case OPT_fexec_charset_
:
502 cpp_opts
->narrow_charset
= arg
;
505 case OPT_fwide_exec_charset_
:
506 cpp_opts
->wide_charset
= arg
;
509 case OPT_finput_charset_
:
510 cpp_opts
->input_charset
= arg
;
513 case OPT_ftemplate_depth_
:
514 max_tinst_depth
= value
;
517 case OPT_fvisibility_inlines_hidden
:
518 visibility_options
.inlines_hidden
= value
;
521 case OPT_femit_struct_debug_baseonly
:
522 set_struct_debug_option (&global_options
, loc
, "base");
525 case OPT_femit_struct_debug_reduced
:
526 set_struct_debug_option (&global_options
, loc
,
527 "dir:ord:sys,dir:gen:any,ind:base");
530 case OPT_femit_struct_debug_detailed_
:
531 set_struct_debug_option (&global_options
, loc
, arg
);
534 case OPT_fext_numeric_literals
:
535 cpp_opts
->ext_numeric_literals
= value
;
539 add_path (xstrdup (arg
), AFTER
, 0, true);
544 defer_opt (code
, arg
);
556 add_path (xstrdup (arg
), QUOTE
, 0, true);
564 add_path (xstrdup (arg
), SYSTEM
, 0, true);
567 case OPT_iwithprefix
:
568 add_prefixed_path (arg
, SYSTEM
);
571 case OPT_iwithprefixbefore
:
572 add_prefixed_path (arg
, BRACKET
);
576 cpp_set_lang (parse_in
, CLK_ASM
);
577 cpp_opts
->dollars_in_ident
= false;
592 error ("output filename specified twice");
595 case OPT_print_objc_runtime_info
:
596 print_struct_values
= 1;
604 case OPT_std_gnu__98
:
605 if (!preprocessing_asm_p
)
606 set_std_cxx98 (code
== OPT_std_c__98
/* ISO */);
610 case OPT_std_gnu__11
:
611 if (!preprocessing_asm_p
)
613 set_std_cxx11 (code
== OPT_std_c__11
/* ISO */);
614 if (code
== OPT_std_c__11
)
615 cpp_opts
->ext_numeric_literals
= 0;
620 case OPT_std_gnu__14
:
621 if (!preprocessing_asm_p
)
623 set_std_cxx14 (code
== OPT_std_c__14
/* ISO */);
624 if (code
== OPT_std_c__14
)
625 cpp_opts
->ext_numeric_literals
= 0;
630 case OPT_std_gnu__1z
:
631 if (!preprocessing_asm_p
)
633 set_std_cxx1z (code
== OPT_std_c__1z
/* ISO */);
634 if (code
== OPT_std_c__1z
)
635 cpp_opts
->ext_numeric_literals
= 0;
640 case OPT_std_iso9899_199409
:
641 if (!preprocessing_asm_p
)
642 set_std_c89 (code
== OPT_std_iso9899_199409
/* c94 */, true /* ISO */);
646 if (!preprocessing_asm_p
)
647 set_std_c89 (false /* c94 */, false /* ISO */);
651 if (!preprocessing_asm_p
)
652 set_std_c99 (true /* ISO */);
656 if (!preprocessing_asm_p
)
657 set_std_c99 (false /* ISO */);
661 if (!preprocessing_asm_p
)
662 set_std_c11 (true /* ISO */);
666 if (!preprocessing_asm_p
)
667 set_std_c11 (false /* ISO */);
671 cpp_opts
->trigraphs
= 1;
674 case OPT_traditional_cpp
:
675 cpp_opts
->traditional
= 1;
686 C_handle_option_auto (&global_options
, &global_options_set
,
688 c_family_lang_mask
, kind
,
689 loc
, handlers
, global_dc
);
693 ObjC_handle_option_auto (&global_options
, &global_options_set
,
695 c_family_lang_mask
, kind
,
696 loc
, handlers
, global_dc
);
700 CXX_handle_option_auto (&global_options
, &global_options_set
,
702 c_family_lang_mask
, kind
,
703 loc
, handlers
, global_dc
);
707 ObjCXX_handle_option_auto (&global_options
, &global_options_set
,
709 c_family_lang_mask
, kind
,
710 loc
, handlers
, global_dc
);
717 cpp_handle_option_auto (&global_options
, scode
, cpp_opts
);
721 /* Default implementation of TARGET_HANDLE_C_OPTION. */
724 default_handle_c_option (size_t code ATTRIBUTE_UNUSED
,
725 const char *arg ATTRIBUTE_UNUSED
,
726 int value ATTRIBUTE_UNUSED
)
731 /* Post-switch processing. */
733 c_common_post_options (const char **pfilename
)
735 struct cpp_callbacks
*cb
;
737 /* Canonicalize the input and output filenames. */
738 if (in_fnames
== NULL
)
740 in_fnames
= XNEWVEC (const char *, 1);
743 else if (strcmp (in_fnames
[0], "-") == 0)
746 if (out_fname
== NULL
|| !strcmp (out_fname
, "-"))
749 if (cpp_opts
->deps
.style
== DEPS_NONE
)
750 check_deps_environment_vars ();
752 handle_deferred_opts ();
754 sanitize_cpp_opts ();
756 register_include_chains (parse_in
, sysroot
, iprefix
, imultilib
,
757 std_inc
, std_cxx_inc
&& c_dialect_cxx (), verbose
);
759 #ifdef C_COMMON_OVERRIDE_OPTIONS
760 /* Some machines may reject certain combinations of C
761 language-specific options. */
762 C_COMMON_OVERRIDE_OPTIONS
;
765 /* Excess precision other than "fast" requires front-end
767 if (c_dialect_cxx ())
769 if (flag_excess_precision_cmdline
== EXCESS_PRECISION_STANDARD
770 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT
)
771 sorry ("-fexcess-precision=standard for C++");
772 flag_excess_precision_cmdline
= EXCESS_PRECISION_FAST
;
774 else if (flag_excess_precision_cmdline
== EXCESS_PRECISION_DEFAULT
)
775 flag_excess_precision_cmdline
= (flag_iso
776 ? EXCESS_PRECISION_STANDARD
777 : EXCESS_PRECISION_FAST
);
779 /* ISO C restricts floating-point expression contraction to within
780 source-language expressions (-ffp-contract=on, currently an alias
781 for -ffp-contract=off). */
784 && (global_options_set
.x_flag_fp_contract_mode
785 == (enum fp_contract_mode
) 0)
786 && flag_unsafe_math_optimizations
== 0)
787 flag_fp_contract_mode
= FP_CONTRACT_OFF
;
789 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
790 inline semantics are not supported in GNU89 or C89 mode. */
791 if (flag_gnu89_inline
== -1)
792 flag_gnu89_inline
= !flag_isoc99
;
793 else if (!flag_gnu89_inline
&& !flag_isoc99
)
794 error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode");
796 /* Default to ObjC sjlj exception handling if NeXT runtime. */
797 if (flag_objc_sjlj_exceptions
< 0)
798 flag_objc_sjlj_exceptions
= flag_next_runtime
;
799 if (flag_objc_exceptions
&& !flag_objc_sjlj_exceptions
)
802 /* If -ffreestanding, -fno-hosted or -fno-builtin then disable
803 pattern recognition. */
804 if (!global_options_set
.x_flag_tree_loop_distribute_patterns
806 flag_tree_loop_distribute_patterns
= 0;
808 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
809 It is never enabled in C++, as the minimum limit is not normative
811 if (c_dialect_cxx ())
812 warn_overlength_strings
= 0;
814 /* Wmain is enabled by default in C++ but not in C. */
815 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
816 even if -Wall or -Wpedantic was given (warn_main will be 2 if set
817 by -Wall, 1 if set by -Wmain). */
819 warn_main
= (c_dialect_cxx () && flag_hosted
) ? 1 : 0;
820 else if (warn_main
== 2)
821 warn_main
= flag_hosted
? 1 : 0;
823 /* In C, -Wall and -Wc++-compat enable -Wenum-compare; if it has not
824 yet been set, it is disabled by default. In C++, it is enabled
826 if (warn_enum_compare
== -1)
827 warn_enum_compare
= c_dialect_cxx () ? 1 : 0;
829 /* -Wpacked-bitfield-compat is on by default for the C languages. The
830 warning is issued in stor-layout.c which is not part of the front-end so
831 we need to selectively turn it on here. */
832 if (warn_packed_bitfield_compat
== -1)
833 warn_packed_bitfield_compat
= 1;
835 /* Special format checking options don't work without -Wformat; warn if
839 warning (OPT_Wformat_y2k
,
840 "-Wformat-y2k ignored without -Wformat");
841 warning (OPT_Wformat_extra_args
,
842 "-Wformat-extra-args ignored without -Wformat");
843 warning (OPT_Wformat_zero_length
,
844 "-Wformat-zero-length ignored without -Wformat");
845 warning (OPT_Wformat_nonliteral
,
846 "-Wformat-nonliteral ignored without -Wformat");
847 warning (OPT_Wformat_contains_nul
,
848 "-Wformat-contains-nul ignored without -Wformat");
849 warning (OPT_Wformat_security
,
850 "-Wformat-security ignored without -Wformat");
853 /* -Wimplicit-function-declaration is enabled by default for C99. */
854 if (warn_implicit_function_declaration
== -1)
855 warn_implicit_function_declaration
= flag_isoc99
;
857 /* -Wimplicit-int is enabled by default for C99. */
858 if (warn_implicit_int
== -1)
859 warn_implicit_int
= flag_isoc99
;
861 /* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 modes. */
862 if (warn_shift_negative_value
== -1)
863 warn_shift_negative_value
= (extra_warnings
864 && (cxx_dialect
>= cxx11
|| flag_isoc99
));
866 /* Declone C++ 'structors if -Os. */
867 if (flag_declone_ctor_dtor
== -1)
868 flag_declone_ctor_dtor
= optimize_size
;
870 if (flag_abi_compat_version
== 1)
872 warning (0, "%<-fabi-compat-version=1%> is not supported, using =2");
873 flag_abi_compat_version
= 2;
875 else if (flag_abi_compat_version
== -1)
877 /* Generate compatibility aliases for ABI v2 (3.4-4.9) by default. */
878 flag_abi_compat_version
= (flag_abi_version
== 0 ? 2 : 0);
880 /* But don't warn about backward compatibility unless explicitly
881 requested with -Wabi=n. */
882 if (flag_abi_version
== 0)
886 /* Change flag_abi_version to be the actual current ABI level for the
887 benefit of c_cpp_builtins. */
888 if (flag_abi_version
== 0)
889 flag_abi_version
= 10;
891 /* Set C++ standard to C++14 if not specified on the command line. */
892 if (c_dialect_cxx () && cxx_dialect
== cxx_unset
)
893 set_std_cxx14 (/*ISO*/false);
895 if (cxx_dialect
>= cxx11
)
897 /* If we're allowing C++0x constructs, don't warn about C++98
898 identifiers which are keywords in C++0x. */
899 warn_cxx11_compat
= 0;
900 cpp_opts
->cpp_warn_cxx11_compat
= 0;
902 if (warn_narrowing
== -1)
905 else if (warn_narrowing
== -1)
908 /* Global sized deallocation is new in C++14. */
909 if (flag_sized_deallocation
== -1)
910 flag_sized_deallocation
= (cxx_dialect
>= cxx14
);
912 if (flag_extern_tls_init
)
914 #if !defined (ASM_OUTPUT_DEF) || !SUPPORTS_WEAK
915 /* Lazy TLS initialization for a variable in another TU requires
916 alias and weak reference support. */
917 if (flag_extern_tls_init
> 0)
918 sorry ("external TLS initialization functions not supported "
920 flag_extern_tls_init
= 0;
922 flag_extern_tls_init
= 1;
926 if (flag_preprocess_only
)
928 /* Open the output now. We must do so even if flag_no_output is
929 on, because there may be other output than from the actual
930 preprocessing (e.g. from -dM). */
931 if (out_fname
[0] == '\0')
934 out_stream
= fopen (out_fname
, "w");
936 if (out_stream
== NULL
)
938 fatal_error (input_location
, "opening output file %s: %m", out_fname
);
942 if (num_in_fnames
> 1)
943 error ("too many filenames given. Type %s --help for usage",
946 init_pp_output (out_stream
);
952 /* When writing a PCH file, avoid reading some other PCH file,
953 because the default address space slot then can't be used
954 for the output PCH file. */
957 c_common_no_more_pch ();
958 /* Only -g0 and -gdwarf* are supported with PCH, for other
959 debug formats we warn here and refuse to load any PCH files. */
960 if (write_symbols
!= NO_DEBUG
&& write_symbols
!= DWARF2_DEBUG
)
961 warning (OPT_Wdeprecated
,
962 "the \"%s\" debug format cannot be used with "
963 "pre-compiled headers", debug_type_names
[write_symbols
]);
965 else if (write_symbols
!= NO_DEBUG
&& write_symbols
!= DWARF2_DEBUG
)
966 c_common_no_more_pch ();
968 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
969 input_location
= UNKNOWN_LOCATION
;
972 cb
= cpp_get_callbacks (parse_in
);
973 cb
->file_change
= cb_file_change
;
974 cb
->dir_change
= cb_dir_change
;
975 cpp_post_options (parse_in
);
976 init_global_opts_from_cpp (&global_options
, cpp_get_options (parse_in
));
978 input_location
= UNKNOWN_LOCATION
;
980 *pfilename
= this_input_filename
981 = cpp_read_main_file (parse_in
, in_fnames
[0]);
982 /* Don't do any compilation or preprocessing if there is no input file. */
983 if (this_input_filename
== NULL
)
989 if (flag_working_directory
990 && flag_preprocess_only
&& !flag_no_line_commands
)
991 pp_dir_change (parse_in
, get_src_pwd ());
993 /* Disable LTO output when outputting a precompiled header. */
994 if (pch_file
&& flag_lto
)
997 flag_generate_lto
= 0;
1000 return flag_preprocess_only
;
1003 /* Front end initialization common to C, ObjC and C++. */
1005 c_common_init (void)
1007 /* Set up preprocessor arithmetic. Must be done after call to
1008 c_common_nodes_and_builtins for type nodes to be good. */
1009 cpp_opts
->precision
= TYPE_PRECISION (intmax_type_node
);
1010 cpp_opts
->char_precision
= TYPE_PRECISION (char_type_node
);
1011 cpp_opts
->int_precision
= TYPE_PRECISION (integer_type_node
);
1012 cpp_opts
->wchar_precision
= TYPE_PRECISION (wchar_type_node
);
1013 cpp_opts
->unsigned_wchar
= TYPE_UNSIGNED (wchar_type_node
);
1014 cpp_opts
->bytes_big_endian
= BYTES_BIG_ENDIAN
;
1016 /* This can't happen until after wchar_precision and bytes_big_endian
1018 cpp_init_iconv (parse_in
);
1023 fputs ("Compiler executable checksum: ", stderr
);
1024 for (i
= 0; i
< 16; i
++)
1025 fprintf (stderr
, "%02x", executable_checksum
[i
]);
1026 putc ('\n', stderr
);
1029 /* Has to wait until now so that cpplib has its hash table. */
1032 if (flag_preprocess_only
)
1034 c_finish_options ();
1035 preprocess_file (parse_in
);
1042 /* Initialize the integrated preprocessor after debug output has been
1043 initialized; loop over each input file. */
1045 c_common_parse_file (void)
1052 c_finish_options ();
1053 /* Open the dump files to use for the original and class dump output
1054 here, to be used during parsing for the current file. */
1055 original_dump_file
= dump_begin (TDI_original
, &original_dump_flags
);
1056 class_dump_file
= dump_begin (TDI_class
, &class_dump_flags
);
1061 /* And end the main input file, if the debug writer wants it */
1062 if (debug_hooks
->start_end_main_source_file
)
1063 (*debug_hooks
->end_source_file
) (0);
1064 if (++i
>= num_in_fnames
)
1066 cpp_undef_all (parse_in
);
1067 cpp_clear_file_cache (parse_in
);
1069 = cpp_read_main_file (parse_in
, in_fnames
[i
]);
1070 if (original_dump_file
)
1072 dump_end (TDI_original
, original_dump_file
);
1073 original_dump_file
= NULL
;
1075 if (class_dump_file
)
1077 dump_end (TDI_class
, class_dump_file
);
1078 class_dump_file
= NULL
;
1080 /* If an input file is missing, abandon further compilation.
1081 cpplib has issued a diagnostic. */
1082 if (!this_input_filename
)
1086 c_parse_final_cleanups ();
1089 /* Returns the appropriate dump file for PHASE to dump with FLAGS. */
1091 get_dump_info (int phase
, int *flags
)
1093 gcc_assert (phase
== TDI_original
|| phase
== TDI_class
);
1094 if (phase
== TDI_original
)
1096 *flags
= original_dump_flags
;
1097 return original_dump_file
;
1101 *flags
= class_dump_flags
;
1102 return class_dump_file
;
1106 /* Common finish hook for the C, ObjC and C++ front ends. */
1108 c_common_finish (void)
1110 FILE *deps_stream
= NULL
;
1112 /* Don't write the deps file if there are errors. */
1113 if (cpp_opts
->deps
.style
!= DEPS_NONE
&& !seen_error ())
1115 /* If -M or -MM was seen without -MF, default output to the
1118 deps_stream
= out_stream
;
1121 deps_stream
= fopen (deps_file
, deps_append
? "a": "w");
1123 fatal_error (input_location
, "opening dependency file %s: %m",
1128 /* For performance, avoid tearing down cpplib's internal structures
1129 with cpp_destroy (). */
1130 cpp_finish (parse_in
, deps_stream
);
1132 if (deps_stream
&& deps_stream
!= out_stream
1133 && (ferror (deps_stream
) || fclose (deps_stream
)))
1134 fatal_error (input_location
, "closing dependency file %s: %m", deps_file
);
1136 if (out_stream
&& (ferror (out_stream
) || fclose (out_stream
)))
1137 fatal_error (input_location
, "when writing output to %s: %m", out_fname
);
1140 /* Either of two environment variables can specify output of
1141 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1142 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1143 and DEPS_TARGET is the target to mention in the deps. They also
1144 result in dependency information being appended to the output file
1145 rather than overwriting it, and like Sun's compiler
1146 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1148 check_deps_environment_vars (void)
1152 spec
= getenv ("DEPENDENCIES_OUTPUT");
1154 cpp_opts
->deps
.style
= DEPS_USER
;
1157 spec
= getenv ("SUNPRO_DEPENDENCIES");
1160 cpp_opts
->deps
.style
= DEPS_SYSTEM
;
1161 cpp_opts
->deps
.ignore_main_file
= true;
1167 /* Find the space before the DEPS_TARGET, if there is one. */
1168 char *s
= strchr (spec
, ' ');
1171 /* Let the caller perform MAKE quoting. */
1172 defer_opt (OPT_MT
, s
+ 1);
1176 /* Command line -MF overrides environment variables and default. */
1185 /* Handle deferred command line switches. */
1187 handle_deferred_opts (void)
1192 /* Avoid allocating the deps buffer if we don't need it.
1193 (This flag may be true without there having been -MT or -MQ
1194 options, but we'll still need the deps buffer.) */
1198 deps
= cpp_get_deps (parse_in
);
1200 for (i
= 0; i
< deferred_count
; i
++)
1202 struct deferred_opt
*opt
= &deferred_opts
[i
];
1204 if (opt
->code
== OPT_MT
|| opt
->code
== OPT_MQ
)
1205 deps_add_target (deps
, opt
->arg
, opt
->code
== OPT_MQ
);
1209 /* These settings are appropriate for GCC, but not necessarily so for
1210 cpplib as a library. */
1212 sanitize_cpp_opts (void)
1214 /* If we don't know what style of dependencies to output, complain
1215 if any other dependency switches have been given. */
1216 if (deps_seen
&& cpp_opts
->deps
.style
== DEPS_NONE
)
1217 error ("to generate dependencies you must specify either -M or -MM");
1219 /* -dM and dependencies suppress normal output; do it here so that
1220 the last -d[MDN] switch overrides earlier ones. */
1221 if (flag_dump_macros
== 'M')
1224 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1225 to perform proper macro expansion. */
1226 if (cpp_opts
->directives_only
&& !cpp_opts
->preprocessed
&& !flag_dump_macros
)
1227 flag_dump_macros
= 'D';
1229 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1230 -dM since at least glibc relies on -M -dM to work. */
1231 /* Also, flag_no_output implies flag_no_line_commands, always. */
1234 if (flag_dump_macros
!= 'M')
1235 flag_dump_macros
= 0;
1236 flag_dump_includes
= 0;
1237 flag_no_line_commands
= 1;
1239 else if (cpp_opts
->deps
.missing_files
)
1240 error ("-MG may only be used with -M or -MM");
1242 cpp_opts
->unsigned_char
= !flag_signed_char
;
1243 cpp_opts
->stdc_0_in_system_headers
= STDC_0_IN_SYSTEM_HEADERS
;
1245 /* Wlong-long is disabled by default. It is enabled by:
1246 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1247 [-Wpedantic | -Wtraditional] -std=non-c99
1249 Either -Wlong-long or -Wno-long-long override any other settings.
1250 ??? These conditions should be handled in c.opt. */
1251 if (warn_long_long
== -1)
1253 warn_long_long
= ((pedantic
|| warn_traditional
)
1254 && (c_dialect_cxx () ? cxx_dialect
== cxx98
: !flag_isoc99
));
1255 cpp_opts
->cpp_warn_long_long
= warn_long_long
;
1258 /* If we're generating preprocessor output, emit current directory
1259 if explicitly requested or if debugging information is enabled.
1260 ??? Maybe we should only do it for debugging formats that
1261 actually output the current directory? */
1262 if (flag_working_directory
== -1)
1263 flag_working_directory
= (debug_info_level
!= DINFO_LEVEL_NONE
);
1265 if (cpp_opts
->directives_only
)
1267 if (cpp_warn_unused_macros
)
1268 error ("-fdirectives-only is incompatible with -Wunused_macros");
1269 if (cpp_opts
->traditional
)
1270 error ("-fdirectives-only is incompatible with -traditional");
1274 /* Add include path with a prefix at the front of its name. */
1276 add_prefixed_path (const char *suffix
, size_t chain
)
1280 size_t prefix_len
, suffix_len
;
1282 suffix_len
= strlen (suffix
);
1283 prefix
= iprefix
? iprefix
: cpp_GCC_INCLUDE_DIR
;
1284 prefix_len
= iprefix
? strlen (iprefix
) : cpp_GCC_INCLUDE_DIR_len
;
1286 path
= (char *) xmalloc (prefix_len
+ suffix_len
+ 1);
1287 memcpy (path
, prefix
, prefix_len
);
1288 memcpy (path
+ prefix_len
, suffix
, suffix_len
);
1289 path
[prefix_len
+ suffix_len
] = '\0';
1291 add_path (path
, chain
, 0, false);
1294 /* Handle -D, -U, -A, -imacros, and the first -include. */
1296 c_finish_options (void)
1298 if (!cpp_opts
->preprocessed
)
1302 cb_file_change (parse_in
,
1303 linemap_check_ordinary (linemap_add (line_table
,
1307 /* Make sure all of the builtins about to be declared have
1308 BUILTINS_LOCATION has their source_location. */
1309 source_location builtins_loc
= BUILTINS_LOCATION
;
1310 cpp_force_token_locations (parse_in
, &builtins_loc
);
1312 cpp_init_builtins (parse_in
, flag_hosted
);
1313 c_cpp_builtins (parse_in
);
1315 cpp_stop_forcing_token_locations (parse_in
);
1317 /* We're about to send user input to cpplib, so make it warn for
1318 things that we previously (when we sent it internal definitions)
1319 told it to not warn.
1321 C99 permits implementation-defined characters in identifiers.
1322 The documented meaning of -std= is to turn off extensions that
1323 conflict with the specified standard, and since a strictly
1324 conforming program cannot contain a '$', we do not condition
1325 their acceptance on the -std= setting. */
1326 cpp_opts
->warn_dollars
= (cpp_opts
->cpp_pedantic
&& !cpp_opts
->c99
);
1328 cb_file_change (parse_in
,
1329 linemap_check_ordinary (linemap_add (line_table
, LC_RENAME
, 0,
1330 _("<command-line>"), 0)));
1332 for (i
= 0; i
< deferred_count
; i
++)
1334 struct deferred_opt
*opt
= &deferred_opts
[i
];
1336 if (opt
->code
== OPT_D
)
1337 cpp_define (parse_in
, opt
->arg
);
1338 else if (opt
->code
== OPT_U
)
1339 cpp_undef (parse_in
, opt
->arg
);
1340 else if (opt
->code
== OPT_A
)
1342 if (opt
->arg
[0] == '-')
1343 cpp_unassert (parse_in
, opt
->arg
+ 1);
1345 cpp_assert (parse_in
, opt
->arg
);
1349 /* Start the main input file, if the debug writer wants it. */
1350 if (debug_hooks
->start_end_main_source_file
1351 && !flag_preprocess_only
)
1352 (*debug_hooks
->start_source_file
) (0, this_input_filename
);
1354 /* Handle -imacros after -D and -U. */
1355 for (i
= 0; i
< deferred_count
; i
++)
1357 struct deferred_opt
*opt
= &deferred_opts
[i
];
1359 if (opt
->code
== OPT_imacros
1360 && cpp_push_include (parse_in
, opt
->arg
))
1362 /* Disable push_command_line_include callback for now. */
1363 include_cursor
= deferred_count
+ 1;
1364 cpp_scan_nooutput (parse_in
);
1370 if (cpp_opts
->directives_only
)
1371 cpp_init_special_builtins (parse_in
);
1373 /* Start the main input file, if the debug writer wants it. */
1374 if (debug_hooks
->start_end_main_source_file
1375 && !flag_preprocess_only
)
1376 (*debug_hooks
->start_source_file
) (0, this_input_filename
);
1380 push_command_line_include ();
1383 /* Give CPP the next file given by -include, if any. */
1385 push_command_line_include (void)
1387 /* This can happen if disabled by -imacros for example.
1388 Punt so that we don't set "<command-line>" as the filename for
1390 if (include_cursor
> deferred_count
)
1393 if (!done_preinclude
)
1395 done_preinclude
= true;
1396 if (flag_hosted
&& std_inc
&& !cpp_opts
->preprocessed
)
1398 const char *preinc
= targetcm
.c_preinclude ();
1399 if (preinc
&& cpp_push_default_include (parse_in
, preinc
))
1404 pch_cpp_save_state ();
1406 while (include_cursor
< deferred_count
)
1408 struct deferred_opt
*opt
= &deferred_opts
[include_cursor
++];
1410 if (!cpp_opts
->preprocessed
&& opt
->code
== OPT_include
1411 && cpp_push_include (parse_in
, opt
->arg
))
1415 if (include_cursor
== deferred_count
)
1418 /* -Wunused-macros should only warn about macros defined hereafter. */
1419 cpp_opts
->warn_unused_macros
= cpp_warn_unused_macros
;
1420 /* Restore the line map from <command line>. */
1421 if (!cpp_opts
->preprocessed
)
1422 cpp_change_file (parse_in
, LC_RENAME
, this_input_filename
);
1424 /* Set this here so the client can change the option if it wishes,
1425 and after stacking the main file so we don't trace the main file. */
1426 line_table
->trace_includes
= cpp_opts
->print_include_names
;
1430 /* File change callback. Has to handle -include files. */
1432 cb_file_change (cpp_reader
* ARG_UNUSED (pfile
),
1433 const line_map_ordinary
*new_map
)
1435 if (flag_preprocess_only
)
1436 pp_file_change (new_map
);
1438 fe_file_change (new_map
);
1441 && (new_map
->reason
== LC_ENTER
|| new_map
->reason
== LC_RENAME
))
1443 /* Signal to plugins that a file is included. This could happen
1444 several times with the same file path, e.g. because of
1445 several '#include' or '#line' directives... */
1446 invoke_plugin_callbacks
1447 (PLUGIN_INCLUDE_FILE
,
1448 const_cast<char*> (ORDINARY_MAP_FILE_NAME (new_map
)));
1451 if (new_map
== 0 || (new_map
->reason
== LC_LEAVE
&& MAIN_FILE_P (new_map
)))
1453 pch_cpp_save_state ();
1454 push_command_line_include ();
1459 cb_dir_change (cpp_reader
* ARG_UNUSED (pfile
), const char *dir
)
1461 if (!set_src_pwd (dir
))
1462 warning (0, "too late for # directive to set debug directory");
1465 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1466 extensions if ISO). There is no concept of gnu94. */
1468 set_std_c89 (int c94
, int iso
)
1470 cpp_set_lang (parse_in
, c94
? CLK_STDC94
: iso
? CLK_STDC89
: CLK_GNUC89
);
1473 flag_no_gnu_keywords
= iso
;
1474 flag_no_nonansi_builtin
= iso
;
1478 lang_hooks
.name
= "GNU C89";
1481 /* Set the C 99 standard (without GNU extensions if ISO). */
1483 set_std_c99 (int iso
)
1485 cpp_set_lang (parse_in
, iso
? CLK_STDC99
: CLK_GNUC99
);
1487 flag_no_nonansi_builtin
= iso
;
1492 lang_hooks
.name
= "GNU C99";
1495 /* Set the C 11 standard (without GNU extensions if ISO). */
1497 set_std_c11 (int iso
)
1499 cpp_set_lang (parse_in
, iso
? CLK_STDC11
: CLK_GNUC11
);
1501 flag_no_nonansi_builtin
= iso
;
1506 lang_hooks
.name
= "GNU C11";
1509 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1511 set_std_cxx98 (int iso
)
1513 cpp_set_lang (parse_in
, iso
? CLK_CXX98
: CLK_GNUCXX
);
1514 flag_no_gnu_keywords
= iso
;
1515 flag_no_nonansi_builtin
= iso
;
1517 cxx_dialect
= cxx98
;
1518 lang_hooks
.name
= "GNU C++98";
1521 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1523 set_std_cxx11 (int iso
)
1525 cpp_set_lang (parse_in
, iso
? CLK_CXX11
: CLK_GNUCXX11
);
1526 flag_no_gnu_keywords
= iso
;
1527 flag_no_nonansi_builtin
= iso
;
1529 /* C++11 includes the C99 standard library. */
1532 cxx_dialect
= cxx11
;
1533 lang_hooks
.name
= "GNU C++11";
1536 /* Set the C++ 2014 draft standard (without GNU extensions if ISO). */
1538 set_std_cxx14 (int iso
)
1540 cpp_set_lang (parse_in
, iso
? CLK_CXX14
: CLK_GNUCXX14
);
1541 flag_no_gnu_keywords
= iso
;
1542 flag_no_nonansi_builtin
= iso
;
1544 /* C++11 includes the C99 standard library. */
1547 cxx_dialect
= cxx14
;
1548 lang_hooks
.name
= "GNU C++14";
1551 /* Set the C++ 201z draft standard (without GNU extensions if ISO). */
1553 set_std_cxx1z (int iso
)
1555 cpp_set_lang (parse_in
, iso
? CLK_CXX1Z
: CLK_GNUCXX1Z
);
1556 flag_no_gnu_keywords
= iso
;
1557 flag_no_nonansi_builtin
= iso
;
1559 /* C++11 includes the C99 standard library. */
1563 cxx_dialect
= cxx1z
;
1564 lang_hooks
.name
= "GNU C++14"; /* Pretend C++14 till standarization. */
1567 /* Args to -d specify what to dump. Silently ignore
1568 unrecognized options; they may be aimed at toplev.c. */
1570 handle_OPT_d (const char *arg
)
1574 while ((c
= *arg
++) != '\0')
1577 case 'M': /* Dump macros only. */
1578 case 'N': /* Dump names. */
1579 case 'D': /* Dump definitions. */
1580 case 'U': /* Dump used macros. */
1581 flag_dump_macros
= c
;
1585 flag_dump_includes
= 1;