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 /* Whether any standard preincluded header has been preincluded. */
112 static bool done_preinclude
;
114 static void handle_OPT_d (const char *);
115 static void set_std_cxx98 (int);
116 static void set_std_cxx11 (int);
117 static void set_std_cxx1y (int);
118 static void set_std_c89 (int, int);
119 static void set_std_c99 (int);
120 static void set_std_c11 (int);
121 static void check_deps_environment_vars (void);
122 static void handle_deferred_opts (void);
123 static void sanitize_cpp_opts (void);
124 static void add_prefixed_path (const char *, size_t);
125 static void push_command_line_include (void);
126 static void cb_file_change (cpp_reader
*, const struct line_map
*);
127 static void cb_dir_change (cpp_reader
*, const char *);
128 static void c_finish_options (void);
130 #ifndef STDC_0_IN_SYSTEM_HEADERS
131 #define STDC_0_IN_SYSTEM_HEADERS 0
134 /* Holds switches parsed by c_common_handle_option (), but whose
135 handling is deferred to c_common_post_options (). */
136 static void defer_opt (enum opt_code
, const char *);
137 static struct deferred_opt
144 extern const unsigned int
145 c_family_lang_mask
= (CL_C
| CL_CXX
| CL_ObjC
| CL_ObjCXX
);
147 /* Defer option CODE with argument ARG. */
149 defer_opt (enum opt_code code
, const char *arg
)
151 deferred_opts
[deferred_count
].code
= code
;
152 deferred_opts
[deferred_count
].arg
= arg
;
156 /* Return language mask for option parsing. */
158 c_common_option_lang_mask (void)
160 static const unsigned int lang_flags
[] = {CL_C
, CL_ObjC
, CL_CXX
, CL_ObjCXX
};
162 return lang_flags
[c_language
];
165 /* Common diagnostics initialization. */
167 c_common_initialize_diagnostics (diagnostic_context
*context
)
169 /* This is conditionalized only because that is the way the front
170 ends used to do it. Maybe this should be unconditional? */
171 if (c_dialect_cxx ())
173 /* By default wrap lines at 80 characters. Is getenv
174 ("COLUMNS") preferable? */
175 diagnostic_line_cutoff (context
) = 80;
176 /* By default, emit location information once for every
177 diagnostic message. */
178 diagnostic_prefixing_rule (context
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
181 context
->opt_permissive
= OPT_fpermissive
;
184 /* Whether options from all C-family languages should be accepted
186 static bool accept_all_c_family_options
= false;
188 /* Return whether to complain about a wrong-language option. */
190 c_common_complain_wrong_lang_p (const struct cl_option
*option
)
192 if (accept_all_c_family_options
193 && (option
->flags
& c_family_lang_mask
))
199 /* Initialize options structure OPTS. */
201 c_common_init_options_struct (struct gcc_options
*opts
)
203 opts
->x_flag_exceptions
= c_dialect_cxx ();
204 opts
->x_warn_pointer_arith
= c_dialect_cxx ();
205 opts
->x_warn_write_strings
= c_dialect_cxx ();
206 opts
->x_flag_warn_unused_result
= true;
208 /* By default, C99-like requirements for complex multiply and divide. */
209 opts
->x_flag_complex_method
= 2;
212 /* Common initialization before calling option handlers. */
214 c_common_init_options (unsigned int decoded_options_count
,
215 struct cl_decoded_option
*decoded_options
)
218 struct cpp_callbacks
*cb
;
220 parse_in
= cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX
: CLK_GNUC89
,
221 ident_hash
, line_table
);
222 cb
= cpp_get_callbacks (parse_in
);
223 cb
->error
= c_cpp_error
;
225 cpp_opts
= cpp_get_options (parse_in
);
226 cpp_opts
->dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
;
227 cpp_opts
->objc
= c_dialect_objc ();
229 /* Reset to avoid warnings on internal definitions. We set it just
230 before passing on command-line options to cpplib. */
231 cpp_opts
->warn_dollars
= 0;
233 deferred_opts
= XNEWVEC (struct deferred_opt
, decoded_options_count
);
235 if (c_language
== clk_c
)
237 /* If preprocessing assembly language, accept any of the C-family
238 front end options since the driver may pass them through. */
239 for (i
= 1; i
< decoded_options_count
; i
++)
240 if (decoded_options
[i
].opt_index
== OPT_lang_asm
)
242 accept_all_c_family_options
= true;
248 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
249 form of an -f or -W option was given. Returns false if the switch was
250 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
252 c_common_handle_option (size_t scode
, const char *arg
, int value
,
253 int kind
, location_t loc
,
254 const struct cl_option_handlers
*handlers
)
256 const struct cl_option
*option
= &cl_options
[scode
];
257 enum opt_code code
= (enum opt_code
) scode
;
260 /* Prevent resetting the language standard to a C dialect when the driver
261 has already determined that we're looking at assembler input. */
262 bool preprocessing_asm_p
= (cpp_get_options (parse_in
)->lang
== CLK_ASM
);
267 if (cl_options
[code
].flags
& c_family_lang_mask
)
269 if ((option
->flags
& CL_TARGET
)
270 && ! targetcm
.handle_c_option (scode
, arg
, value
))
277 case OPT__output_pch_
:
282 defer_opt (code
, arg
);
286 cpp_opts
->discard_comments
= 0;
290 cpp_opts
->discard_comments
= 0;
291 cpp_opts
->discard_comments_in_macro_exp
= 0;
295 defer_opt (code
, arg
);
299 cpp_opts
->print_include_names
= 1;
303 TARGET_OPTF (xstrdup (arg
));
307 if (strcmp (arg
, "-"))
308 add_path (xstrdup (arg
), BRACKET
, 0, true);
311 if (quote_chain_split
)
312 error ("-I- specified twice");
313 quote_chain_split
= true;
314 split_quote_chain ();
315 inform (input_location
, "obsolete option -I- used, please use -iquote instead");
321 /* When doing dependencies with -M or -MM, suppress normal
322 preprocessed output, but still do -dM etc. as software
323 depends on this. Preprocessed output does occur if -MD, -MMD
324 or environment var dependency generation is used. */
325 cpp_opts
->deps
.style
= (code
== OPT_M
? DEPS_SYSTEM
: DEPS_USER
);
331 cpp_opts
->deps
.style
= (code
== OPT_MD
? DEPS_SYSTEM
: DEPS_USER
);
332 cpp_opts
->deps
.need_preprocessor_output
= true;
343 cpp_opts
->deps
.missing_files
= true;
348 cpp_opts
->deps
.phony_targets
= true;
354 defer_opt (code
, arg
);
358 flag_no_line_commands
= 1;
362 defer_opt (code
, arg
);
366 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
369 warn_array_bounds
= value
;
371 /* Only warn about unknown pragmas that are not in system
373 warn_unknown_pragmas
= value
;
375 if (!c_dialect_cxx ())
377 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
378 can turn it off only if it's not explicit. */
380 warn_main
= (value
? 2 : 0);
382 /* In C, -Wall and -Wc++-compat turns on -Wenum-compare,
383 which we do here. In C++ it is on by default, which is
384 done in c_common_post_options. */
385 if (warn_enum_compare
== -1)
386 warn_enum_compare
= value
;
389 cpp_opts
->warn_trigraphs
= value
;
390 cpp_opts
->warn_comments
= value
;
391 cpp_opts
->warn_num_sign_change
= value
;
394 case OPT_Wbuiltin_macro_redefined
:
395 cpp_opts
->warn_builtin_macro_redefined
= value
;
399 cpp_opts
->warn_comments
= value
;
402 case OPT_Wc___compat
:
403 /* Because -Wenum-compare is the default in C++, -Wc++-compat
404 implies -Wenum-compare. */
405 if (warn_enum_compare
== -1 && value
)
406 warn_enum_compare
= value
;
407 cpp_opts
->warn_cxx_operator_names
= value
;
410 case OPT_Wdeprecated
:
411 cpp_opts
->cpp_warn_deprecated
= value
;
414 case OPT_Wendif_labels
:
415 cpp_opts
->warn_endif_labels
= value
;
423 set_Wformat (atoi (arg
));
426 case OPT_Winvalid_pch
:
427 cpp_opts
->warn_invalid_pch
= value
;
430 case OPT_Wliteral_suffix
:
431 cpp_opts
->warn_literal_suffix
= value
;
435 cpp_opts
->cpp_warn_long_long
= value
;
438 case OPT_Wmissing_include_dirs
:
439 cpp_opts
->warn_missing_include_dirs
= value
;
443 cpp_opts
->warn_multichar
= value
;
446 case OPT_Wnormalized_
:
447 if (kind
== DK_ERROR
)
450 inform (input_location
, "-Werror=normalized=: set -Wnormalized=nfc");
451 cpp_opts
->warn_normalize
= normalized_C
;
455 if (!value
|| (arg
&& strcasecmp (arg
, "none") == 0))
456 cpp_opts
->warn_normalize
= normalized_none
;
457 else if (!arg
|| strcasecmp (arg
, "nfkc") == 0)
458 cpp_opts
->warn_normalize
= normalized_KC
;
459 else if (strcasecmp (arg
, "id") == 0)
460 cpp_opts
->warn_normalize
= normalized_identifier_C
;
461 else if (strcasecmp (arg
, "nfc") == 0)
462 cpp_opts
->warn_normalize
= normalized_C
;
464 error ("argument %qs to %<-Wnormalized%> not recognized", arg
);
468 case OPT_Wtraditional
:
469 cpp_opts
->cpp_warn_traditional
= value
;
473 cpp_opts
->warn_trigraphs
= value
;
477 cpp_opts
->warn_undef
= value
;
480 case OPT_Wunknown_pragmas
:
481 /* Set to greater than 1, so that even unknown pragmas in
482 system headers will be warned about. */
483 warn_unknown_pragmas
= value
* 2;
486 case OPT_Wunused_macros
:
487 warn_unused_macros
= value
;
490 case OPT_Wvariadic_macros
:
491 warn_variadic_macros
= value
;
496 warn_nonvdtor
= true;
500 if (!c_dialect_cxx ())
501 set_std_c89 (false, true);
503 set_std_cxx98 (true);
510 case OPT_fcond_mismatch
:
511 if (!c_dialect_cxx ())
513 flag_cond_mismatch
= value
;
516 warning (0, "switch %qs is no longer supported", option
->opt_text
);
523 disable_builtin_function (arg
);
526 case OPT_fdirectives_only
:
527 cpp_opts
->directives_only
= value
;
530 case OPT_fdollars_in_identifiers
:
531 cpp_opts
->dollars_in_ident
= value
;
534 case OPT_ffreestanding
:
536 /* Fall through.... */
539 flag_no_builtin
= !value
;
542 case OPT_fconstant_string_class_
:
543 constant_string_class_name
= arg
;
546 case OPT_fextended_identifiers
:
547 cpp_opts
->extended_identifiers
= value
;
550 case OPT_foperator_names
:
551 cpp_opts
->operator_names
= value
;
555 cpp_opts
->restore_pch_deps
= value
;
558 case OPT_fpch_preprocess
:
559 flag_pch_preprocess
= value
;
562 case OPT_fpermissive
:
563 flag_permissive
= value
;
564 global_dc
->permissive
= value
;
567 case OPT_fpreprocessed
:
568 cpp_opts
->preprocessed
= value
;
575 case OPT_ftrack_macro_expansion
:
580 case OPT_ftrack_macro_expansion_
:
581 if (arg
&& *arg
!= '\0')
582 cpp_opts
->track_macro_expansion
= value
;
584 cpp_opts
->track_macro_expansion
= 2;
588 flag_use_repository
= value
;
590 flag_implicit_templates
= 0;
594 /* It is documented that we silently ignore silly values. */
595 if (value
>= 1 && value
<= 100)
596 cpp_opts
->tabstop
= value
;
599 case OPT_fexec_charset_
:
600 cpp_opts
->narrow_charset
= arg
;
603 case OPT_fwide_exec_charset_
:
604 cpp_opts
->wide_charset
= arg
;
607 case OPT_finput_charset_
:
608 cpp_opts
->input_charset
= arg
;
611 case OPT_ftemplate_depth_
:
612 max_tinst_depth
= value
;
615 case OPT_fvisibility_inlines_hidden
:
616 visibility_options
.inlines_hidden
= value
;
619 case OPT_femit_struct_debug_baseonly
:
620 set_struct_debug_option (&global_options
, loc
, "base");
623 case OPT_femit_struct_debug_reduced
:
624 set_struct_debug_option (&global_options
, loc
,
625 "dir:ord:sys,dir:gen:any,ind:base");
628 case OPT_femit_struct_debug_detailed_
:
629 set_struct_debug_option (&global_options
, loc
, arg
);
633 add_path (xstrdup (arg
), AFTER
, 0, true);
638 defer_opt (code
, arg
);
650 add_path (xstrdup (arg
), QUOTE
, 0, true);
658 add_path (xstrdup (arg
), SYSTEM
, 0, true);
661 case OPT_iwithprefix
:
662 add_prefixed_path (arg
, SYSTEM
);
665 case OPT_iwithprefixbefore
:
666 add_prefixed_path (arg
, BRACKET
);
670 cpp_set_lang (parse_in
, CLK_ASM
);
671 cpp_opts
->dollars_in_ident
= false;
686 error ("output filename specified twice");
689 /* We need to handle the -Wpedantic switch here, rather than in
690 c_common_post_options, so that a subsequent -Wno-endif-labels
691 is not overridden. */
693 cpp_opts
->cpp_pedantic
= 1;
694 cpp_opts
->warn_endif_labels
= 1;
695 if (warn_overlength_strings
== -1)
696 warn_overlength_strings
= 1;
701 case OPT_print_objc_runtime_info
:
702 print_struct_values
= 1;
710 case OPT_std_gnu__98
:
711 if (!preprocessing_asm_p
)
712 set_std_cxx98 (code
== OPT_std_c__98
/* ISO */);
716 case OPT_std_gnu__11
:
717 if (!preprocessing_asm_p
)
718 set_std_cxx11 (code
== OPT_std_c__11
/* ISO */);
722 case OPT_std_gnu__1y
:
723 if (!preprocessing_asm_p
)
724 set_std_cxx1y (code
== OPT_std_c__11
/* ISO */);
728 case OPT_std_iso9899_199409
:
729 if (!preprocessing_asm_p
)
730 set_std_c89 (code
== OPT_std_iso9899_199409
/* c94 */, true /* ISO */);
734 if (!preprocessing_asm_p
)
735 set_std_c89 (false /* c94 */, false /* ISO */);
739 if (!preprocessing_asm_p
)
740 set_std_c99 (true /* ISO */);
744 if (!preprocessing_asm_p
)
745 set_std_c99 (false /* ISO */);
749 if (!preprocessing_asm_p
)
750 set_std_c11 (true /* ISO */);
754 if (!preprocessing_asm_p
)
755 set_std_c11 (false /* ISO */);
759 cpp_opts
->trigraphs
= 1;
762 case OPT_traditional_cpp
:
763 cpp_opts
->traditional
= 1;
778 C_handle_option_auto (&global_options
, &global_options_set
,
780 c_family_lang_mask
, kind
,
781 loc
, handlers
, global_dc
);
785 ObjC_handle_option_auto (&global_options
, &global_options_set
,
787 c_family_lang_mask
, kind
,
788 loc
, handlers
, global_dc
);
792 CXX_handle_option_auto (&global_options
, &global_options_set
,
794 c_family_lang_mask
, kind
,
795 loc
, handlers
, global_dc
);
799 ObjCXX_handle_option_auto (&global_options
, &global_options_set
,
801 c_family_lang_mask
, kind
,
802 loc
, handlers
, global_dc
);
812 /* Default implementation of TARGET_HANDLE_C_OPTION. */
815 default_handle_c_option (size_t code ATTRIBUTE_UNUSED
,
816 const char *arg ATTRIBUTE_UNUSED
,
817 int value ATTRIBUTE_UNUSED
)
822 /* Post-switch processing. */
824 c_common_post_options (const char **pfilename
)
826 struct cpp_callbacks
*cb
;
828 /* Canonicalize the input and output filenames. */
829 if (in_fnames
== NULL
)
831 in_fnames
= XNEWVEC (const char *, 1);
834 else if (strcmp (in_fnames
[0], "-") == 0)
837 if (out_fname
== NULL
|| !strcmp (out_fname
, "-"))
840 if (cpp_opts
->deps
.style
== DEPS_NONE
)
841 check_deps_environment_vars ();
843 handle_deferred_opts ();
845 sanitize_cpp_opts ();
847 register_include_chains (parse_in
, sysroot
, iprefix
, imultilib
,
848 std_inc
, std_cxx_inc
&& c_dialect_cxx (), verbose
);
850 #ifdef C_COMMON_OVERRIDE_OPTIONS
851 /* Some machines may reject certain combinations of C
852 language-specific options. */
853 C_COMMON_OVERRIDE_OPTIONS
;
856 /* Excess precision other than "fast" requires front-end
858 if (c_dialect_cxx ())
860 if (flag_excess_precision_cmdline
== EXCESS_PRECISION_STANDARD
861 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT
)
862 sorry ("-fexcess-precision=standard for C++");
863 flag_excess_precision_cmdline
= EXCESS_PRECISION_FAST
;
865 else if (flag_excess_precision_cmdline
== EXCESS_PRECISION_DEFAULT
)
866 flag_excess_precision_cmdline
= (flag_iso
867 ? EXCESS_PRECISION_STANDARD
868 : EXCESS_PRECISION_FAST
);
870 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
871 inline semantics are not supported in GNU89 or C89 mode. */
872 if (flag_gnu89_inline
== -1)
873 flag_gnu89_inline
= !flag_isoc99
;
874 else if (!flag_gnu89_inline
&& !flag_isoc99
)
875 error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode");
877 /* Default to ObjC sjlj exception handling if NeXT runtime. */
878 if (flag_objc_sjlj_exceptions
< 0)
879 flag_objc_sjlj_exceptions
= flag_next_runtime
;
880 if (flag_objc_exceptions
&& !flag_objc_sjlj_exceptions
)
883 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
884 It is never enabled in C++, as the minimum limit is not normative
886 if (warn_overlength_strings
== -1 || c_dialect_cxx ())
887 warn_overlength_strings
= 0;
889 /* Wmain is enabled by default in C++ but not in C. */
890 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
891 even if -Wall was given (warn_main will be 2 if set by -Wall, 1
892 if set by -Wmain). */
894 warn_main
= (c_dialect_cxx () && flag_hosted
) ? 1 : 0;
895 else if (warn_main
== 2)
896 warn_main
= flag_hosted
? 1 : 0;
898 /* In C, -Wall and -Wc++-compat enable -Wenum-compare, which we do
899 in c_common_handle_option; if it has not yet been set, it is
900 disabled by default. In C++, it is enabled by default. */
901 if (warn_enum_compare
== -1)
902 warn_enum_compare
= c_dialect_cxx () ? 1 : 0;
904 /* -Wpacked-bitfield-compat is on by default for the C languages. The
905 warning is issued in stor-layout.c which is not part of the front-end so
906 we need to selectively turn it on here. */
907 if (warn_packed_bitfield_compat
== -1)
908 warn_packed_bitfield_compat
= 1;
910 /* Special format checking options don't work without -Wformat; warn if
914 warning (OPT_Wformat_y2k
,
915 "-Wformat-y2k ignored without -Wformat");
916 warning (OPT_Wformat_extra_args
,
917 "-Wformat-extra-args ignored without -Wformat");
918 warning (OPT_Wformat_zero_length
,
919 "-Wformat-zero-length ignored without -Wformat");
920 warning (OPT_Wformat_nonliteral
,
921 "-Wformat-nonliteral ignored without -Wformat");
922 warning (OPT_Wformat_contains_nul
,
923 "-Wformat-contains-nul ignored without -Wformat");
924 warning (OPT_Wformat_security
,
925 "-Wformat-security ignored without -Wformat");
928 /* -Wimplicit-function-declaration is enabled by default for C99. */
929 if (warn_implicit_function_declaration
== -1)
930 warn_implicit_function_declaration
= flag_isoc99
;
932 if (cxx_dialect
>= cxx0x
)
934 /* If we're allowing C++0x constructs, don't warn about C++98
935 identifiers which are keywords in C++0x. */
936 warn_cxx0x_compat
= 0;
938 if (warn_narrowing
== -1)
941 else if (warn_narrowing
== -1)
944 if (flag_preprocess_only
)
946 /* Open the output now. We must do so even if flag_no_output is
947 on, because there may be other output than from the actual
948 preprocessing (e.g. from -dM). */
949 if (out_fname
[0] == '\0')
952 out_stream
= fopen (out_fname
, "w");
954 if (out_stream
== NULL
)
956 fatal_error ("opening output file %s: %m", out_fname
);
960 if (num_in_fnames
> 1)
961 error ("too many filenames given. Type %s --help for usage",
964 init_pp_output (out_stream
);
970 /* When writing a PCH file, avoid reading some other PCH file,
971 because the default address space slot then can't be used
972 for the output PCH file. */
974 c_common_no_more_pch ();
976 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
977 input_location
= UNKNOWN_LOCATION
;
980 cb
= cpp_get_callbacks (parse_in
);
981 cb
->file_change
= cb_file_change
;
982 cb
->dir_change
= cb_dir_change
;
983 cpp_post_options (parse_in
);
985 input_location
= UNKNOWN_LOCATION
;
987 *pfilename
= this_input_filename
988 = cpp_read_main_file (parse_in
, in_fnames
[0]);
989 /* Don't do any compilation or preprocessing if there is no input file. */
990 if (this_input_filename
== NULL
)
996 if (flag_working_directory
997 && flag_preprocess_only
&& !flag_no_line_commands
)
998 pp_dir_change (parse_in
, get_src_pwd ());
1000 /* Disable LTO output when outputting a precompiled header. */
1001 if (pch_file
&& flag_lto
)
1004 flag_generate_lto
= 0;
1007 return flag_preprocess_only
;
1010 /* Front end initialization common to C, ObjC and C++. */
1012 c_common_init (void)
1014 /* Set up preprocessor arithmetic. Must be done after call to
1015 c_common_nodes_and_builtins for type nodes to be good. */
1016 cpp_opts
->precision
= TYPE_PRECISION (intmax_type_node
);
1017 cpp_opts
->char_precision
= TYPE_PRECISION (char_type_node
);
1018 cpp_opts
->int_precision
= TYPE_PRECISION (integer_type_node
);
1019 cpp_opts
->wchar_precision
= TYPE_PRECISION (wchar_type_node
);
1020 cpp_opts
->unsigned_wchar
= TYPE_UNSIGNED (wchar_type_node
);
1021 cpp_opts
->bytes_big_endian
= BYTES_BIG_ENDIAN
;
1023 /* This can't happen until after wchar_precision and bytes_big_endian
1025 cpp_init_iconv (parse_in
);
1030 fputs ("Compiler executable checksum: ", stderr
);
1031 for (i
= 0; i
< 16; i
++)
1032 fprintf (stderr
, "%02x", executable_checksum
[i
]);
1033 putc ('\n', stderr
);
1036 /* Has to wait until now so that cpplib has its hash table. */
1039 if (flag_preprocess_only
)
1041 c_finish_options ();
1042 preprocess_file (parse_in
);
1049 /* Initialize the integrated preprocessor after debug output has been
1050 initialized; loop over each input file. */
1052 c_common_parse_file (void)
1059 c_finish_options ();
1064 /* And end the main input file, if the debug writer wants it */
1065 if (debug_hooks
->start_end_main_source_file
)
1066 (*debug_hooks
->end_source_file
) (0);
1067 if (++i
>= num_in_fnames
)
1069 cpp_undef_all (parse_in
);
1070 cpp_clear_file_cache (parse_in
);
1072 = cpp_read_main_file (parse_in
, in_fnames
[i
]);
1073 /* If an input file is missing, abandon further compilation.
1074 cpplib has issued a diagnostic. */
1075 if (!this_input_filename
)
1080 /* Common finish hook for the C, ObjC and C++ front ends. */
1082 c_common_finish (void)
1084 FILE *deps_stream
= NULL
;
1086 /* Don't write the deps file if there are errors. */
1087 if (cpp_opts
->deps
.style
!= DEPS_NONE
&& !seen_error ())
1089 /* If -M or -MM was seen without -MF, default output to the
1092 deps_stream
= out_stream
;
1095 deps_stream
= fopen (deps_file
, deps_append
? "a": "w");
1097 fatal_error ("opening dependency file %s: %m", deps_file
);
1101 /* For performance, avoid tearing down cpplib's internal structures
1102 with cpp_destroy (). */
1103 cpp_finish (parse_in
, deps_stream
);
1105 if (deps_stream
&& deps_stream
!= out_stream
1106 && (ferror (deps_stream
) || fclose (deps_stream
)))
1107 fatal_error ("closing dependency file %s: %m", deps_file
);
1109 if (out_stream
&& (ferror (out_stream
) || fclose (out_stream
)))
1110 fatal_error ("when writing output to %s: %m", out_fname
);
1113 /* Either of two environment variables can specify output of
1114 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1115 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1116 and DEPS_TARGET is the target to mention in the deps. They also
1117 result in dependency information being appended to the output file
1118 rather than overwriting it, and like Sun's compiler
1119 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1121 check_deps_environment_vars (void)
1125 spec
= getenv ("DEPENDENCIES_OUTPUT");
1127 cpp_opts
->deps
.style
= DEPS_USER
;
1130 spec
= getenv ("SUNPRO_DEPENDENCIES");
1133 cpp_opts
->deps
.style
= DEPS_SYSTEM
;
1134 cpp_opts
->deps
.ignore_main_file
= true;
1140 /* Find the space before the DEPS_TARGET, if there is one. */
1141 char *s
= strchr (spec
, ' ');
1144 /* Let the caller perform MAKE quoting. */
1145 defer_opt (OPT_MT
, s
+ 1);
1149 /* Command line -MF overrides environment variables and default. */
1158 /* Handle deferred command line switches. */
1160 handle_deferred_opts (void)
1165 /* Avoid allocating the deps buffer if we don't need it.
1166 (This flag may be true without there having been -MT or -MQ
1167 options, but we'll still need the deps buffer.) */
1171 deps
= cpp_get_deps (parse_in
);
1173 for (i
= 0; i
< deferred_count
; i
++)
1175 struct deferred_opt
*opt
= &deferred_opts
[i
];
1177 if (opt
->code
== OPT_MT
|| opt
->code
== OPT_MQ
)
1178 deps_add_target (deps
, opt
->arg
, opt
->code
== OPT_MQ
);
1182 /* These settings are appropriate for GCC, but not necessarily so for
1183 cpplib as a library. */
1185 sanitize_cpp_opts (void)
1187 /* If we don't know what style of dependencies to output, complain
1188 if any other dependency switches have been given. */
1189 if (deps_seen
&& cpp_opts
->deps
.style
== DEPS_NONE
)
1190 error ("to generate dependencies you must specify either -M or -MM");
1192 /* -dM and dependencies suppress normal output; do it here so that
1193 the last -d[MDN] switch overrides earlier ones. */
1194 if (flag_dump_macros
== 'M')
1197 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1198 to perform proper macro expansion. */
1199 if (cpp_opts
->directives_only
&& !cpp_opts
->preprocessed
&& !flag_dump_macros
)
1200 flag_dump_macros
= 'D';
1202 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1203 -dM since at least glibc relies on -M -dM to work. */
1204 /* Also, flag_no_output implies flag_no_line_commands, always. */
1207 if (flag_dump_macros
!= 'M')
1208 flag_dump_macros
= 0;
1209 flag_dump_includes
= 0;
1210 flag_no_line_commands
= 1;
1212 else if (cpp_opts
->deps
.missing_files
)
1213 error ("-MG may only be used with -M or -MM");
1215 cpp_opts
->unsigned_char
= !flag_signed_char
;
1216 cpp_opts
->stdc_0_in_system_headers
= STDC_0_IN_SYSTEM_HEADERS
;
1218 /* Wlong-long is disabled by default. It is enabled by:
1219 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1220 [-Wpedantic | -Wtraditional] -std=non-c99 .
1222 Either -Wlong-long or -Wno-long-long override any other settings. */
1223 if (warn_long_long
== -1)
1224 warn_long_long
= ((pedantic
|| warn_traditional
)
1225 && (c_dialect_cxx () ? cxx_dialect
== cxx98
: !flag_isoc99
));
1226 cpp_opts
->cpp_warn_long_long
= warn_long_long
;
1228 /* Similarly with -Wno-variadic-macros. No check for c99 here, since
1229 this also turns off warnings about GCCs extension. */
1230 cpp_opts
->warn_variadic_macros
1231 = warn_variadic_macros
&& (pedantic
|| warn_traditional
);
1233 /* If we're generating preprocessor output, emit current directory
1234 if explicitly requested or if debugging information is enabled.
1235 ??? Maybe we should only do it for debugging formats that
1236 actually output the current directory? */
1237 if (flag_working_directory
== -1)
1238 flag_working_directory
= (debug_info_level
!= DINFO_LEVEL_NONE
);
1240 if (cpp_opts
->directives_only
)
1242 if (warn_unused_macros
)
1243 error ("-fdirectives-only is incompatible with -Wunused_macros");
1244 if (cpp_opts
->traditional
)
1245 error ("-fdirectives-only is incompatible with -traditional");
1249 /* Add include path with a prefix at the front of its name. */
1251 add_prefixed_path (const char *suffix
, size_t chain
)
1255 size_t prefix_len
, suffix_len
;
1257 suffix_len
= strlen (suffix
);
1258 prefix
= iprefix
? iprefix
: cpp_GCC_INCLUDE_DIR
;
1259 prefix_len
= iprefix
? strlen (iprefix
) : cpp_GCC_INCLUDE_DIR_len
;
1261 path
= (char *) xmalloc (prefix_len
+ suffix_len
+ 1);
1262 memcpy (path
, prefix
, prefix_len
);
1263 memcpy (path
+ prefix_len
, suffix
, suffix_len
);
1264 path
[prefix_len
+ suffix_len
] = '\0';
1266 add_path (path
, chain
, 0, false);
1269 /* Handle -D, -U, -A, -imacros, and the first -include. */
1271 c_finish_options (void)
1273 if (!cpp_opts
->preprocessed
)
1278 /* Make sure all of the builtins about to be declared have
1279 BUILTINS_LOCATION has their source_location. */
1280 source_location builtins_loc
= BUILTINS_LOCATION
;
1281 cpp_force_token_locations (parse_in
, &builtins_loc
);
1283 cpp_init_builtins (parse_in
, flag_hosted
);
1284 c_cpp_builtins (parse_in
);
1286 cpp_stop_forcing_token_locations (parse_in
);
1289 /* We're about to send user input to cpplib, so make it warn for
1290 things that we previously (when we sent it internal definitions)
1291 told it to not warn.
1293 C99 permits implementation-defined characters in identifiers.
1294 The documented meaning of -std= is to turn off extensions that
1295 conflict with the specified standard, and since a strictly
1296 conforming program cannot contain a '$', we do not condition
1297 their acceptance on the -std= setting. */
1298 cpp_opts
->warn_dollars
= (cpp_opts
->cpp_pedantic
&& !cpp_opts
->c99
);
1300 cb_file_change (parse_in
,
1301 linemap_add (line_table
, LC_RENAME
, 0,
1302 _("<command-line>"), 0));
1304 for (i
= 0; i
< deferred_count
; i
++)
1306 struct deferred_opt
*opt
= &deferred_opts
[i
];
1308 if (opt
->code
== OPT_D
)
1309 cpp_define (parse_in
, opt
->arg
);
1310 else if (opt
->code
== OPT_U
)
1311 cpp_undef (parse_in
, opt
->arg
);
1312 else if (opt
->code
== OPT_A
)
1314 if (opt
->arg
[0] == '-')
1315 cpp_unassert (parse_in
, opt
->arg
+ 1);
1317 cpp_assert (parse_in
, opt
->arg
);
1321 /* Start the main input file, if the debug writer wants it. */
1322 if (debug_hooks
->start_end_main_source_file
1323 && !flag_preprocess_only
)
1324 (*debug_hooks
->start_source_file
) (0, this_input_filename
);
1326 /* Handle -imacros after -D and -U. */
1327 for (i
= 0; i
< deferred_count
; i
++)
1329 struct deferred_opt
*opt
= &deferred_opts
[i
];
1331 if (opt
->code
== OPT_imacros
1332 && cpp_push_include (parse_in
, opt
->arg
))
1334 /* Disable push_command_line_include callback for now. */
1335 include_cursor
= deferred_count
+ 1;
1336 cpp_scan_nooutput (parse_in
);
1342 if (cpp_opts
->directives_only
)
1343 cpp_init_special_builtins (parse_in
);
1345 /* Start the main input file, if the debug writer wants it. */
1346 if (debug_hooks
->start_end_main_source_file
1347 && !flag_preprocess_only
)
1348 (*debug_hooks
->start_source_file
) (0, this_input_filename
);
1352 push_command_line_include ();
1355 /* Give CPP the next file given by -include, if any. */
1357 push_command_line_include (void)
1359 if (!done_preinclude
)
1361 done_preinclude
= true;
1362 if (flag_hosted
&& std_inc
&& !cpp_opts
->preprocessed
)
1364 const char *preinc
= targetcm
.c_preinclude ();
1365 if (preinc
&& cpp_push_default_include (parse_in
, preinc
))
1370 pch_cpp_save_state ();
1372 while (include_cursor
< deferred_count
)
1374 struct deferred_opt
*opt
= &deferred_opts
[include_cursor
++];
1376 if (!cpp_opts
->preprocessed
&& opt
->code
== OPT_include
1377 && cpp_push_include (parse_in
, opt
->arg
))
1381 if (include_cursor
== deferred_count
)
1384 /* -Wunused-macros should only warn about macros defined hereafter. */
1385 cpp_opts
->warn_unused_macros
= warn_unused_macros
;
1386 /* Restore the line map from <command line>. */
1387 if (!cpp_opts
->preprocessed
)
1388 cpp_change_file (parse_in
, LC_RENAME
, this_input_filename
);
1390 /* Set this here so the client can change the option if it wishes,
1391 and after stacking the main file so we don't trace the main file. */
1392 line_table
->trace_includes
= cpp_opts
->print_include_names
;
1396 /* File change callback. Has to handle -include files. */
1398 cb_file_change (cpp_reader
* ARG_UNUSED (pfile
),
1399 const struct line_map
*new_map
)
1401 if (flag_preprocess_only
)
1402 pp_file_change (new_map
);
1404 fe_file_change (new_map
);
1406 if (new_map
== 0 || (new_map
->reason
== LC_LEAVE
&& MAIN_FILE_P (new_map
)))
1408 pch_cpp_save_state ();
1409 push_command_line_include ();
1414 cb_dir_change (cpp_reader
* ARG_UNUSED (pfile
), const char *dir
)
1416 if (!set_src_pwd (dir
))
1417 warning (0, "too late for # directive to set debug directory");
1420 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1421 extensions if ISO). There is no concept of gnu94. */
1423 set_std_c89 (int c94
, int iso
)
1425 cpp_set_lang (parse_in
, c94
? CLK_STDC94
: iso
? CLK_STDC89
: CLK_GNUC89
);
1428 flag_no_gnu_keywords
= iso
;
1429 flag_no_nonansi_builtin
= iso
;
1435 /* Set the C 99 standard (without GNU extensions if ISO). */
1437 set_std_c99 (int iso
)
1439 cpp_set_lang (parse_in
, iso
? CLK_STDC99
: CLK_GNUC99
);
1441 flag_no_nonansi_builtin
= iso
;
1448 /* Set the C 11 standard (without GNU extensions if ISO). */
1450 set_std_c11 (int iso
)
1452 cpp_set_lang (parse_in
, iso
? CLK_STDC11
: CLK_GNUC11
);
1454 flag_no_nonansi_builtin
= iso
;
1461 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1463 set_std_cxx98 (int iso
)
1465 cpp_set_lang (parse_in
, iso
? CLK_CXX98
: CLK_GNUCXX
);
1466 flag_no_gnu_keywords
= iso
;
1467 flag_no_nonansi_builtin
= iso
;
1469 cxx_dialect
= cxx98
;
1472 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1474 set_std_cxx11 (int iso
)
1476 cpp_set_lang (parse_in
, iso
? CLK_CXX11
: CLK_GNUCXX11
);
1477 flag_no_gnu_keywords
= iso
;
1478 flag_no_nonansi_builtin
= iso
;
1480 /* C++11 includes the C99 standard library. */
1483 cxx_dialect
= cxx11
;
1486 /* Set the C++ 201y draft standard (without GNU extensions if ISO). */
1488 set_std_cxx1y (int iso
)
1490 cpp_set_lang (parse_in
, iso
? CLK_CXX11
: CLK_GNUCXX11
);
1491 flag_no_gnu_keywords
= iso
;
1492 flag_no_nonansi_builtin
= iso
;
1494 /* C++11 includes the C99 standard library. */
1497 cxx_dialect
= cxx1y
;
1500 /* Args to -d specify what to dump. Silently ignore
1501 unrecognized options; they may be aimed at toplev.c. */
1503 handle_OPT_d (const char *arg
)
1507 while ((c
= *arg
++) != '\0')
1510 case 'M': /* Dump macros only. */
1511 case 'N': /* Dump names. */
1512 case 'D': /* Dump definitions. */
1513 case 'U': /* Dump used macros. */
1514 flag_dump_macros
= c
;
1518 flag_dump_includes
= 1;