1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Neil Booth.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "langhooks.h"
31 #include "diagnostic.h"
33 #include "cppdefault.h"
35 #include "debug.h" /* For debug_hooks. */
39 #include "target.h" /* For gcc_targetcm. */
40 #include "tm_p.h" /* For C_COMMON_OVERRIDE_OPTIONS. */
42 #ifndef DOLLARS_IN_IDENTIFIERS
43 # define DOLLARS_IN_IDENTIFIERS true
46 #ifndef TARGET_SYSTEM_ROOT
47 # define TARGET_SYSTEM_ROOT NULL
51 #define TARGET_OPTF(ARG)
55 cpp_options *cpp_opts;
58 static const char *this_input_filename;
60 /* Filename and stream for preprocessed output. */
61 static const char *out_fname;
62 static FILE *out_stream;
64 /* Append dependencies to deps_file. */
65 static bool deps_append;
67 /* If dependency switches (-MF etc.) have been given. */
68 static bool deps_seen;
73 /* Dependency output file. */
74 static const char *deps_file;
76 /* The prefix given by -iprefix, if any. */
77 static const char *iprefix;
79 /* The multilib directory given by -imultilib, if any. */
80 static const char *imultilib;
82 /* The system root, if any. Overridden by -isysroot. */
83 static const char *sysroot = TARGET_SYSTEM_ROOT;
85 /* Zero disables all standard directories for headers. */
86 static bool std_inc = true;
88 /* Zero disables the C++-specific standard directories for headers. */
89 static bool std_cxx_inc = true;
91 /* If the quote chain has been split by -I-. */
92 static bool quote_chain_split;
94 /* If -Wunused-macros. */
95 static bool warn_unused_macros;
97 /* If -Wvariadic-macros. */
98 static bool warn_variadic_macros = true;
100 /* Number of deferred options. */
101 static size_t deferred_count;
103 /* Number of deferred options scanned for -include. */
104 static size_t include_cursor;
106 static void handle_OPT_d (const char *);
107 static void set_std_cxx98 (int);
108 static void set_std_cxx0x (int);
109 static void set_std_c89 (int, int);
110 static void set_std_c99 (int);
111 static void set_std_c1x (int);
112 static void check_deps_environment_vars (void);
113 static void handle_deferred_opts (void);
114 static void sanitize_cpp_opts (void);
115 static void add_prefixed_path (const char *, size_t);
116 static void push_command_line_include (void);
117 static void cb_file_change (cpp_reader *, const struct line_map *);
118 static void cb_dir_change (cpp_reader *, const char *);
119 static void c_finish_options (void);
121 #ifndef STDC_0_IN_SYSTEM_HEADERS
122 #define STDC_0_IN_SYSTEM_HEADERS 0
125 /* Holds switches parsed by c_common_handle_option (), but whose
126 handling is deferred to c_common_post_options (). */
127 static void defer_opt (enum opt_code, const char *);
128 static struct deferred_opt
135 extern const unsigned int
136 c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
138 /* Defer option CODE with argument ARG. */
140 defer_opt (enum opt_code code, const char *arg)
142 deferred_opts[deferred_count].code = code;
143 deferred_opts[deferred_count].arg = arg;
147 /* Return language mask for option parsing. */
149 c_common_option_lang_mask (void)
151 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
153 return lang_flags[c_language];
156 /* Common diagnostics initialization. */
158 c_common_initialize_diagnostics (diagnostic_context *context)
160 /* This is conditionalized only because that is the way the front
161 ends used to do it. Maybe this should be unconditional? */
162 if (c_dialect_cxx ())
164 /* By default wrap lines at 80 characters. Is getenv
165 ("COLUMNS") preferable? */
166 diagnostic_line_cutoff (context) = 80;
167 /* By default, emit location information once for every
168 diagnostic message. */
169 diagnostic_prefixing_rule (context) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
172 context->opt_permissive = OPT_fpermissive;
175 /* Whether options from all C-family languages should be accepted
177 static bool accept_all_c_family_options = false;
179 /* Return whether to complain about a wrong-language option. */
181 c_common_complain_wrong_lang_p (const struct cl_option *option)
183 if (accept_all_c_family_options
184 && (option->flags & c_family_lang_mask))
190 /* Initialize options structure OPTS. */
192 c_common_init_options_struct (struct gcc_options *opts)
194 opts->x_flag_exceptions = c_dialect_cxx ();
195 opts->x_warn_pointer_arith = c_dialect_cxx ();
196 opts->x_warn_write_strings = c_dialect_cxx ();
197 opts->x_flag_warn_unused_result = true;
199 /* By default, C99-like requirements for complex multiply and divide. */
200 opts->x_flag_complex_method = 2;
203 /* Common initialization before calling option handlers. */
205 c_common_init_options (unsigned int decoded_options_count,
206 struct cl_decoded_option *decoded_options)
209 struct cpp_callbacks *cb;
211 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
212 ident_hash, line_table);
213 cb = cpp_get_callbacks (parse_in);
214 cb->error = c_cpp_error;
216 cpp_opts = cpp_get_options (parse_in);
217 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
218 cpp_opts->objc = c_dialect_objc ();
220 /* Reset to avoid warnings on internal definitions. We set it just
221 before passing on command-line options to cpplib. */
222 cpp_opts->warn_dollars = 0;
224 deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
226 if (c_language == clk_c)
228 /* If preprocessing assembly language, accept any of the C-family
229 front end options since the driver may pass them through. */
230 for (i = 1; i < decoded_options_count; i++)
231 if (decoded_options[i].opt_index == OPT_lang_asm)
233 accept_all_c_family_options = true;
239 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
240 form of an -f or -W option was given. Returns false if the switch was
241 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
243 c_common_handle_option (size_t scode, const char *arg, int value,
244 int kind, location_t loc,
245 const struct cl_option_handlers *handlers)
247 const struct cl_option *option = &cl_options[scode];
248 enum opt_code code = (enum opt_code) scode;
251 /* Prevent resetting the language standard to a C dialect when the driver
252 has already determined that we're looking at assembler input. */
253 bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM);
258 if (cl_options[code].flags & c_family_lang_mask)
260 if ((option->flags & CL_TARGET)
261 && ! targetcm.handle_c_option (scode, arg, value))
268 case OPT__output_pch_:
273 defer_opt (code, arg);
277 cpp_opts->discard_comments = 0;
281 cpp_opts->discard_comments = 0;
282 cpp_opts->discard_comments_in_macro_exp = 0;
286 defer_opt (code, arg);
290 cpp_opts->print_include_names = 1;
294 TARGET_OPTF (xstrdup (arg));
298 if (strcmp (arg, "-"))
299 add_path (xstrdup (arg), BRACKET, 0, true);
302 if (quote_chain_split)
303 error ("-I- specified twice");
304 quote_chain_split = true;
305 split_quote_chain ();
306 inform (input_location, "obsolete option -I- used, please use -iquote instead");
312 /* When doing dependencies with -M or -MM, suppress normal
313 preprocessed output, but still do -dM etc. as software
314 depends on this. Preprocessed output does occur if -MD, -MMD
315 or environment var dependency generation is used. */
316 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
322 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
323 cpp_opts->deps.need_preprocessor_output = true;
334 cpp_opts->deps.missing_files = true;
339 cpp_opts->deps.phony_targets = true;
345 defer_opt (code, arg);
349 flag_no_line_commands = 1;
353 defer_opt (code, arg);
359 handle_generated_option (&global_options, &global_options_set,
360 OPT_Wimplicit, NULL, value,
361 c_family_lang_mask, kind, loc,
362 handlers, global_dc);
363 warn_char_subscripts = value;
364 warn_missing_braces = value;
365 warn_parentheses = value;
366 warn_return_type = value;
367 warn_sequence_point = value; /* Was C only. */
369 if (warn_strict_aliasing == -1)
370 set_Wstrict_aliasing (&global_options, value);
371 warn_address = value;
372 if (warn_strict_overflow == -1)
373 warn_strict_overflow = value;
374 warn_array_bounds = value;
375 warn_volatile_register_var = value;
377 /* Only warn about unknown pragmas that are not in system
379 warn_unknown_pragmas = value;
381 warn_uninitialized = value;
383 if (!c_dialect_cxx ())
385 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
386 can turn it off only if it's not explicit. */
388 warn_main = (value ? 2 : 0);
390 /* In C, -Wall turns on -Wenum-compare, which we do here.
391 In C++ it is on by default, which is done in
392 c_common_post_options. */
393 if (warn_enum_compare == -1)
394 warn_enum_compare = value;
398 /* C++-specific warnings. */
399 warn_sign_compare = value;
400 warn_reorder = value;
401 warn_cxx0x_compat = value;
404 cpp_opts->warn_trigraphs = value;
405 cpp_opts->warn_comments = value;
406 cpp_opts->warn_num_sign_change = value;
408 if (warn_pointer_sign == -1)
409 warn_pointer_sign = value;
412 case OPT_Wbuiltin_macro_redefined:
413 cpp_opts->warn_builtin_macro_redefined = value;
417 cpp_opts->warn_comments = value;
420 case OPT_Wc___compat:
421 /* Because -Wenum-compare is the default in C++, -Wc++-compat
422 implies -Wenum-compare. */
423 if (warn_enum_compare == -1 && value)
424 warn_enum_compare = value;
425 /* Because C++ always warns about a goto which misses an
426 initialization, -Wc++-compat turns on -Wjump-misses-init. */
427 if (warn_jump_misses_init == -1 && value)
428 warn_jump_misses_init = value;
429 cpp_opts->warn_cxx_operator_names = value;
432 case OPT_Wdeprecated:
433 cpp_opts->cpp_warn_deprecated = value;
436 case OPT_Wendif_labels:
437 cpp_opts->warn_endif_labels = value;
441 global_dc->warning_as_error_requested = value;
449 set_Wformat (atoi (arg));
453 gcc_assert (value == 0 || value == 1);
454 if (warn_implicit_int == -1)
455 handle_generated_option (&global_options, &global_options_set,
456 OPT_Wimplicit_int, NULL, value,
457 c_family_lang_mask, kind, loc, handlers,
459 if (warn_implicit_function_declaration == -1)
460 handle_generated_option (&global_options, &global_options_set,
461 OPT_Wimplicit_function_declaration, NULL,
462 value, c_family_lang_mask, kind, loc,
463 handlers, global_dc);
466 case OPT_Winvalid_pch:
467 cpp_opts->warn_invalid_pch = value;
471 cpp_opts->cpp_warn_long_long = value;
474 case OPT_Wmissing_include_dirs:
475 cpp_opts->warn_missing_include_dirs = value;
479 cpp_opts->warn_multichar = value;
482 case OPT_Wnormalized_:
483 if (kind == DK_ERROR)
486 inform (input_location, "-Werror=normalized=: set -Wnormalized=nfc");
487 cpp_opts->warn_normalize = normalized_C;
491 if (!value || (arg && strcasecmp (arg, "none") == 0))
492 cpp_opts->warn_normalize = normalized_none;
493 else if (!arg || strcasecmp (arg, "nfkc") == 0)
494 cpp_opts->warn_normalize = normalized_KC;
495 else if (strcasecmp (arg, "id") == 0)
496 cpp_opts->warn_normalize = normalized_identifier_C;
497 else if (strcasecmp (arg, "nfc") == 0)
498 cpp_opts->warn_normalize = normalized_C;
500 error ("argument %qs to %<-Wnormalized%> not recognized", arg);
504 case OPT_Wreturn_type:
505 warn_return_type = value;
508 case OPT_Wtraditional:
509 cpp_opts->cpp_warn_traditional = value;
513 cpp_opts->warn_trigraphs = value;
517 cpp_opts->warn_undef = value;
520 case OPT_Wunknown_pragmas:
521 /* Set to greater than 1, so that even unknown pragmas in
522 system headers will be warned about. */
523 warn_unknown_pragmas = value * 2;
526 case OPT_Wunused_macros:
527 warn_unused_macros = value;
530 case OPT_Wvariadic_macros:
531 warn_variadic_macros = value;
534 case OPT_Wwrite_strings:
535 warn_write_strings = value;
541 warn_nonvdtor = true;
545 if (!c_dialect_cxx ())
546 set_std_c89 (false, true);
548 set_std_cxx98 (true);
555 case OPT_fcond_mismatch:
556 if (!c_dialect_cxx ())
558 flag_cond_mismatch = value;
561 warning (0, "switch %qs is no longer supported", option->opt_text);
568 disable_builtin_function (arg);
571 case OPT_fdirectives_only:
572 cpp_opts->directives_only = value;
575 case OPT_fdollars_in_identifiers:
576 cpp_opts->dollars_in_ident = value;
579 case OPT_ffreestanding:
581 /* Fall through.... */
584 flag_no_builtin = !value;
587 case OPT_fconstant_string_class_:
588 constant_string_class_name = arg;
591 case OPT_fextended_identifiers:
592 cpp_opts->extended_identifiers = value;
595 case OPT_fgnu_runtime:
596 flag_next_runtime = !value;
599 case OPT_fnext_runtime:
600 flag_next_runtime = value;
603 case OPT_foperator_names:
604 cpp_opts->operator_names = value;
608 cpp_opts->restore_pch_deps = value;
611 case OPT_fpch_preprocess:
612 flag_pch_preprocess = value;
615 case OPT_fpermissive:
616 flag_permissive = value;
617 global_dc->permissive = value;
620 case OPT_fpreprocessed:
621 cpp_opts->preprocessed = value;
625 flag_use_repository = value;
627 flag_implicit_templates = 0;
631 /* It is documented that we silently ignore silly values. */
632 if (value >= 1 && value <= 100)
633 cpp_opts->tabstop = value;
636 case OPT_fexec_charset_:
637 cpp_opts->narrow_charset = arg;
640 case OPT_fwide_exec_charset_:
641 cpp_opts->wide_charset = arg;
644 case OPT_finput_charset_:
645 cpp_opts->input_charset = arg;
648 case OPT_ftemplate_depth_:
649 max_tinst_depth = value;
652 case OPT_fvisibility_inlines_hidden:
653 visibility_options.inlines_hidden = value;
656 case OPT_femit_struct_debug_baseonly:
657 set_struct_debug_option (&global_options, loc, "base");
660 case OPT_femit_struct_debug_reduced:
661 set_struct_debug_option (&global_options, loc,
662 "dir:ord:sys,dir:gen:any,ind:base");
665 case OPT_femit_struct_debug_detailed_:
666 set_struct_debug_option (&global_options, loc, arg);
670 add_path (xstrdup (arg), AFTER, 0, true);
675 defer_opt (code, arg);
687 add_path (xstrdup (arg), QUOTE, 0, true);
695 add_path (xstrdup (arg), SYSTEM, 0, true);
698 case OPT_iwithprefix:
699 add_prefixed_path (arg, SYSTEM);
702 case OPT_iwithprefixbefore:
703 add_prefixed_path (arg, BRACKET);
707 cpp_set_lang (parse_in, CLK_ASM);
708 cpp_opts->dollars_in_ident = false;
723 error ("output filename specified twice");
726 /* We need to handle the -pedantic switches here, rather than in
727 c_common_post_options, so that a subsequent -Wno-endif-labels
728 is not overridden. */
729 case OPT_pedantic_errors:
731 cpp_opts->cpp_pedantic = 1;
732 cpp_opts->warn_endif_labels = 1;
733 if (warn_pointer_sign == -1)
734 warn_pointer_sign = 1;
735 if (warn_overlength_strings == -1)
736 warn_overlength_strings = 1;
741 case OPT_print_objc_runtime_info:
742 print_struct_values = 1;
750 case OPT_std_gnu__98:
751 if (!preprocessing_asm_p)
752 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
756 case OPT_std_gnu__0x:
757 if (!preprocessing_asm_p)
758 set_std_cxx0x (code == OPT_std_c__0x /* ISO */);
762 case OPT_std_iso9899_199409:
763 if (!preprocessing_asm_p)
764 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
768 if (!preprocessing_asm_p)
769 set_std_c89 (false /* c94 */, false /* ISO */);
773 if (!preprocessing_asm_p)
774 set_std_c99 (true /* ISO */);
778 if (!preprocessing_asm_p)
779 set_std_c99 (false /* ISO */);
783 if (!preprocessing_asm_p)
784 set_std_c1x (true /* ISO */);
788 if (!preprocessing_asm_p)
789 set_std_c1x (false /* ISO */);
793 cpp_opts->trigraphs = 1;
796 case OPT_traditional_cpp:
797 cpp_opts->traditional = 1;
812 /* Post-switch processing. */
814 c_common_post_options (const char **pfilename)
816 struct cpp_callbacks *cb;
818 /* Canonicalize the input and output filenames. */
819 if (in_fnames == NULL)
821 in_fnames = XNEWVEC (const char *, 1);
824 else if (strcmp (in_fnames[0], "-") == 0)
827 if (out_fname == NULL || !strcmp (out_fname, "-"))
830 if (cpp_opts->deps.style == DEPS_NONE)
831 check_deps_environment_vars ();
833 handle_deferred_opts ();
835 sanitize_cpp_opts ();
837 register_include_chains (parse_in, sysroot, iprefix, imultilib,
838 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
840 #ifdef C_COMMON_OVERRIDE_OPTIONS
841 /* Some machines may reject certain combinations of C
842 language-specific options. */
843 C_COMMON_OVERRIDE_OPTIONS;
846 /* Excess precision other than "fast" requires front-end
848 if (c_dialect_cxx ())
850 if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
851 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT)
852 sorry ("-fexcess-precision=standard for C++");
853 flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
855 else if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
856 flag_excess_precision_cmdline = (flag_iso
857 ? EXCESS_PRECISION_STANDARD
858 : EXCESS_PRECISION_FAST);
860 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
861 inline semantics are not supported in GNU89 or C89 mode. */
862 if (flag_gnu89_inline == -1)
863 flag_gnu89_inline = !flag_isoc99;
864 else if (!flag_gnu89_inline && !flag_isoc99)
865 error ("-fno-gnu89-inline is only supported in GNU99 or C99 mode");
867 /* Default to ObjC sjlj exception handling if NeXT runtime. */
868 if (flag_objc_sjlj_exceptions < 0)
869 flag_objc_sjlj_exceptions = flag_next_runtime;
870 if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
873 /* -Wextra implies the following flags
874 unless explicitly overridden. */
875 if (warn_type_limits == -1)
876 warn_type_limits = extra_warnings;
877 if (warn_clobbered == -1)
878 warn_clobbered = extra_warnings;
879 if (warn_empty_body == -1)
880 warn_empty_body = extra_warnings;
881 if (warn_sign_compare == -1)
882 warn_sign_compare = extra_warnings;
883 if (warn_missing_field_initializers == -1)
884 warn_missing_field_initializers = extra_warnings;
885 if (warn_missing_parameter_type == -1)
886 warn_missing_parameter_type = extra_warnings;
887 if (warn_old_style_declaration == -1)
888 warn_old_style_declaration = extra_warnings;
889 if (warn_override_init == -1)
890 warn_override_init = extra_warnings;
891 if (warn_ignored_qualifiers == -1)
892 warn_ignored_qualifiers = extra_warnings;
894 /* -Wpointer-sign is disabled by default, but it is enabled if any
895 of -Wall or -pedantic are given. */
896 if (warn_pointer_sign == -1)
897 warn_pointer_sign = 0;
899 if (warn_strict_aliasing == -1)
900 warn_strict_aliasing = 0;
901 if (warn_strict_overflow == -1)
902 warn_strict_overflow = 0;
903 if (warn_jump_misses_init == -1)
904 warn_jump_misses_init = 0;
906 /* -Woverlength-strings is off by default, but is enabled by -pedantic.
907 It is never enabled in C++, as the minimum limit is not normative
909 if (warn_overlength_strings == -1 || c_dialect_cxx ())
910 warn_overlength_strings = 0;
912 /* Wmain is enabled by default in C++ but not in C. */
913 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
914 even if -Wall was given (warn_main will be 2 if set by -Wall, 1
915 if set by -Wmain). */
917 warn_main = (c_dialect_cxx () && flag_hosted) ? 1 : 0;
918 else if (warn_main == 2)
919 warn_main = flag_hosted ? 1 : 0;
921 /* In C, -Wconversion enables -Wsign-conversion (unless disabled
922 through -Wno-sign-conversion). While in C++,
923 -Wsign-conversion needs to be requested explicitly. */
924 if (warn_sign_conversion == -1)
925 warn_sign_conversion = (c_dialect_cxx ()) ? 0 : warn_conversion;
927 /* In C, -Wall and -Wc++-compat enable -Wenum-compare, which we do
928 in c_common_handle_option; if it has not yet been set, it is
929 disabled by default. In C++, it is enabled by default. */
930 if (warn_enum_compare == -1)
931 warn_enum_compare = c_dialect_cxx () ? 1 : 0;
933 /* -Wpacked-bitfield-compat is on by default for the C languages. The
934 warning is issued in stor-layout.c which is not part of the front-end so
935 we need to selectively turn it on here. */
936 if (warn_packed_bitfield_compat == -1)
937 warn_packed_bitfield_compat = 1;
939 /* Special format checking options don't work without -Wformat; warn if
943 warning (OPT_Wformat_y2k,
944 "-Wformat-y2k ignored without -Wformat");
945 warning (OPT_Wformat_extra_args,
946 "-Wformat-extra-args ignored without -Wformat");
947 warning (OPT_Wformat_zero_length,
948 "-Wformat-zero-length ignored without -Wformat");
949 warning (OPT_Wformat_nonliteral,
950 "-Wformat-nonliteral ignored without -Wformat");
951 warning (OPT_Wformat_contains_nul,
952 "-Wformat-contains-nul ignored without -Wformat");
953 warning (OPT_Wformat_security,
954 "-Wformat-security ignored without -Wformat");
957 if (warn_implicit == -1)
960 if (warn_implicit_int == -1)
961 warn_implicit_int = 0;
963 /* -Wimplicit-function-declaration is enabled by default for C99. */
964 if (warn_implicit_function_declaration == -1)
965 warn_implicit_function_declaration = flag_isoc99;
967 /* If we're allowing C++0x constructs, don't warn about C++0x
968 compatibility problems. */
969 if (cxx_dialect == cxx0x)
970 warn_cxx0x_compat = 0;
972 if (flag_preprocess_only)
974 /* Open the output now. We must do so even if flag_no_output is
975 on, because there may be other output than from the actual
976 preprocessing (e.g. from -dM). */
977 if (out_fname[0] == '\0')
980 out_stream = fopen (out_fname, "w");
982 if (out_stream == NULL)
984 fatal_error ("opening output file %s: %m", out_fname);
988 if (num_in_fnames > 1)
989 error ("too many filenames given. Type %s --help for usage",
992 init_pp_output (out_stream);
998 /* When writing a PCH file, avoid reading some other PCH file,
999 because the default address space slot then can't be used
1000 for the output PCH file. */
1002 c_common_no_more_pch ();
1004 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1005 input_location = UNKNOWN_LOCATION;
1008 cb = cpp_get_callbacks (parse_in);
1009 cb->file_change = cb_file_change;
1010 cb->dir_change = cb_dir_change;
1011 cpp_post_options (parse_in);
1013 input_location = UNKNOWN_LOCATION;
1015 *pfilename = this_input_filename
1016 = cpp_read_main_file (parse_in, in_fnames[0]);
1017 /* Don't do any compilation or preprocessing if there is no input file. */
1018 if (this_input_filename == NULL)
1024 if (flag_working_directory
1025 && flag_preprocess_only && !flag_no_line_commands)
1026 pp_dir_change (parse_in, get_src_pwd ());
1028 return flag_preprocess_only;
1031 /* Front end initialization common to C, ObjC and C++. */
1033 c_common_init (void)
1035 /* Set up preprocessor arithmetic. Must be done after call to
1036 c_common_nodes_and_builtins for type nodes to be good. */
1037 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1038 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1039 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1040 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1041 cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node);
1042 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1044 /* This can't happen until after wchar_precision and bytes_big_endian
1046 cpp_init_iconv (parse_in);
1049 c_common_print_pch_checksum (stderr);
1051 /* Has to wait until now so that cpplib has its hash table. */
1054 if (flag_preprocess_only)
1056 c_finish_options ();
1057 preprocess_file (parse_in);
1064 /* Initialize the integrated preprocessor after debug output has been
1065 initialized; loop over each input file. */
1067 c_common_parse_file (void)
1074 c_finish_options ();
1079 /* And end the main input file, if the debug writer wants it */
1080 if (debug_hooks->start_end_main_source_file)
1081 (*debug_hooks->end_source_file) (0);
1082 if (++i >= num_in_fnames)
1084 cpp_undef_all (parse_in);
1085 cpp_clear_file_cache (parse_in);
1087 = cpp_read_main_file (parse_in, in_fnames[i]);
1088 /* If an input file is missing, abandon further compilation.
1089 cpplib has issued a diagnostic. */
1090 if (!this_input_filename)
1095 /* Common finish hook for the C, ObjC and C++ front ends. */
1097 c_common_finish (void)
1099 FILE *deps_stream = NULL;
1101 /* Don't write the deps file if there are errors. */
1102 if (cpp_opts->deps.style != DEPS_NONE && !seen_error ())
1104 /* If -M or -MM was seen without -MF, default output to the
1107 deps_stream = out_stream;
1110 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1112 fatal_error ("opening dependency file %s: %m", deps_file);
1116 /* For performance, avoid tearing down cpplib's internal structures
1117 with cpp_destroy (). */
1118 cpp_finish (parse_in, deps_stream);
1120 if (deps_stream && deps_stream != out_stream
1121 && (ferror (deps_stream) || fclose (deps_stream)))
1122 fatal_error ("closing dependency file %s: %m", deps_file);
1124 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1125 fatal_error ("when writing output to %s: %m", out_fname);
1128 /* Either of two environment variables can specify output of
1129 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1130 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1131 and DEPS_TARGET is the target to mention in the deps. They also
1132 result in dependency information being appended to the output file
1133 rather than overwriting it, and like Sun's compiler
1134 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1136 check_deps_environment_vars (void)
1140 spec = getenv ("DEPENDENCIES_OUTPUT");
1142 cpp_opts->deps.style = DEPS_USER;
1145 spec = getenv ("SUNPRO_DEPENDENCIES");
1148 cpp_opts->deps.style = DEPS_SYSTEM;
1149 cpp_opts->deps.ignore_main_file = true;
1155 /* Find the space before the DEPS_TARGET, if there is one. */
1156 char *s = strchr (spec, ' ');
1159 /* Let the caller perform MAKE quoting. */
1160 defer_opt (OPT_MT, s + 1);
1164 /* Command line -MF overrides environment variables and default. */
1173 /* Handle deferred command line switches. */
1175 handle_deferred_opts (void)
1180 /* Avoid allocating the deps buffer if we don't need it.
1181 (This flag may be true without there having been -MT or -MQ
1182 options, but we'll still need the deps buffer.) */
1186 deps = cpp_get_deps (parse_in);
1188 for (i = 0; i < deferred_count; i++)
1190 struct deferred_opt *opt = &deferred_opts[i];
1192 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1193 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
1197 /* These settings are appropriate for GCC, but not necessarily so for
1198 cpplib as a library. */
1200 sanitize_cpp_opts (void)
1202 /* If we don't know what style of dependencies to output, complain
1203 if any other dependency switches have been given. */
1204 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1205 error ("to generate dependencies you must specify either -M or -MM");
1207 /* -dM and dependencies suppress normal output; do it here so that
1208 the last -d[MDN] switch overrides earlier ones. */
1209 if (flag_dump_macros == 'M')
1212 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1213 to perform proper macro expansion. */
1214 if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
1215 flag_dump_macros = 'D';
1217 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1218 -dM since at least glibc relies on -M -dM to work. */
1219 /* Also, flag_no_output implies flag_no_line_commands, always. */
1222 if (flag_dump_macros != 'M')
1223 flag_dump_macros = 0;
1224 flag_dump_includes = 0;
1225 flag_no_line_commands = 1;
1227 else if (cpp_opts->deps.missing_files)
1228 error ("-MG may only be used with -M or -MM");
1230 cpp_opts->unsigned_char = !flag_signed_char;
1231 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1233 /* Wlong-long is disabled by default. It is enabled by:
1234 [-pedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1235 [-pedantic | -Wtraditional] -std=non-c99 .
1237 Either -Wlong-long or -Wno-long-long override any other settings. */
1238 if (warn_long_long == -1)
1239 warn_long_long = ((pedantic || warn_traditional)
1240 && (c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99));
1241 cpp_opts->cpp_warn_long_long = warn_long_long;
1243 /* Similarly with -Wno-variadic-macros. No check for c99 here, since
1244 this also turns off warnings about GCCs extension. */
1245 cpp_opts->warn_variadic_macros
1246 = warn_variadic_macros && (pedantic || warn_traditional);
1248 /* If we're generating preprocessor output, emit current directory
1249 if explicitly requested or if debugging information is enabled.
1250 ??? Maybe we should only do it for debugging formats that
1251 actually output the current directory? */
1252 if (flag_working_directory == -1)
1253 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1255 if (cpp_opts->directives_only)
1257 if (warn_unused_macros)
1258 error ("-fdirectives-only is incompatible with -Wunused_macros");
1259 if (cpp_opts->traditional)
1260 error ("-fdirectives-only is incompatible with -traditional");
1264 /* Add include path with a prefix at the front of its name. */
1266 add_prefixed_path (const char *suffix, size_t chain)
1270 size_t prefix_len, suffix_len;
1272 suffix_len = strlen (suffix);
1273 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1274 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1276 path = (char *) xmalloc (prefix_len + suffix_len + 1);
1277 memcpy (path, prefix, prefix_len);
1278 memcpy (path + prefix_len, suffix, suffix_len);
1279 path[prefix_len + suffix_len] = '\0';
1281 add_path (path, chain, 0, false);
1284 /* Handle -D, -U, -A, -imacros, and the first -include. */
1286 c_finish_options (void)
1288 if (!cpp_opts->preprocessed)
1292 cb_file_change (parse_in,
1293 linemap_add (line_table, LC_RENAME, 0,
1294 _("<built-in>"), 0));
1296 cpp_init_builtins (parse_in, flag_hosted);
1297 c_cpp_builtins (parse_in);
1299 /* We're about to send user input to cpplib, so make it warn for
1300 things that we previously (when we sent it internal definitions)
1301 told it to not warn.
1303 C99 permits implementation-defined characters in identifiers.
1304 The documented meaning of -std= is to turn off extensions that
1305 conflict with the specified standard, and since a strictly
1306 conforming program cannot contain a '$', we do not condition
1307 their acceptance on the -std= setting. */
1308 cpp_opts->warn_dollars = (cpp_opts->cpp_pedantic && !cpp_opts->c99);
1310 cb_file_change (parse_in,
1311 linemap_add (line_table, LC_RENAME, 0,
1312 _("<command-line>"), 0));
1314 for (i = 0; i < deferred_count; i++)
1316 struct deferred_opt *opt = &deferred_opts[i];
1318 if (opt->code == OPT_D)
1319 cpp_define (parse_in, opt->arg);
1320 else if (opt->code == OPT_U)
1321 cpp_undef (parse_in, opt->arg);
1322 else if (opt->code == OPT_A)
1324 if (opt->arg[0] == '-')
1325 cpp_unassert (parse_in, opt->arg + 1);
1327 cpp_assert (parse_in, opt->arg);
1331 /* Start the main input file, if the debug writer wants it. */
1332 if (debug_hooks->start_end_main_source_file
1333 && !flag_preprocess_only)
1334 (*debug_hooks->start_source_file) (0, this_input_filename);
1336 /* Handle -imacros after -D and -U. */
1337 for (i = 0; i < deferred_count; i++)
1339 struct deferred_opt *opt = &deferred_opts[i];
1341 if (opt->code == OPT_imacros
1342 && cpp_push_include (parse_in, opt->arg))
1344 /* Disable push_command_line_include callback for now. */
1345 include_cursor = deferred_count + 1;
1346 cpp_scan_nooutput (parse_in);
1352 if (cpp_opts->directives_only)
1353 cpp_init_special_builtins (parse_in);
1355 /* Start the main input file, if the debug writer wants it. */
1356 if (debug_hooks->start_end_main_source_file
1357 && !flag_preprocess_only)
1358 (*debug_hooks->start_source_file) (0, this_input_filename);
1362 push_command_line_include ();
1365 /* Give CPP the next file given by -include, if any. */
1367 push_command_line_include (void)
1369 while (include_cursor < deferred_count)
1371 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1373 if (!cpp_opts->preprocessed && opt->code == OPT_include
1374 && cpp_push_include (parse_in, opt->arg))
1378 if (include_cursor == deferred_count)
1381 /* -Wunused-macros should only warn about macros defined hereafter. */
1382 cpp_opts->warn_unused_macros = warn_unused_macros;
1383 /* Restore the line map from <command line>. */
1384 if (!cpp_opts->preprocessed)
1385 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
1387 /* Set this here so the client can change the option if it wishes,
1388 and after stacking the main file so we don't trace the main file. */
1389 line_table->trace_includes = cpp_opts->print_include_names;
1393 /* File change callback. Has to handle -include files. */
1395 cb_file_change (cpp_reader * ARG_UNUSED (pfile),
1396 const struct line_map *new_map)
1398 if (flag_preprocess_only)
1399 pp_file_change (new_map);
1401 fe_file_change (new_map);
1403 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1404 push_command_line_include ();
1408 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
1410 if (!set_src_pwd (dir))
1411 warning (0, "too late for # directive to set debug directory");
1414 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1415 extensions if ISO). There is no concept of gnu94. */
1417 set_std_c89 (int c94, int iso)
1419 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1422 flag_no_gnu_keywords = iso;
1423 flag_no_nonansi_builtin = iso;
1429 /* Set the C 99 standard (without GNU extensions if ISO). */
1431 set_std_c99 (int iso)
1433 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1435 flag_no_nonansi_builtin = iso;
1442 /* Set the C 1X standard draft (without GNU extensions if ISO). */
1444 set_std_c1x (int iso)
1446 cpp_set_lang (parse_in, iso ? CLK_STDC1X: CLK_GNUC1X);
1448 flag_no_nonansi_builtin = iso;
1455 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1457 set_std_cxx98 (int iso)
1459 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1460 flag_no_gnu_keywords = iso;
1461 flag_no_nonansi_builtin = iso;
1463 cxx_dialect = cxx98;
1466 /* Set the C++ 0x working draft "standard" (without GNU extensions if ISO). */
1468 set_std_cxx0x (int iso)
1470 cpp_set_lang (parse_in, iso ? CLK_CXX0X: CLK_GNUCXX0X);
1471 flag_no_gnu_keywords = iso;
1472 flag_no_nonansi_builtin = iso;
1474 cxx_dialect = cxx0x;
1477 /* Args to -d specify what to dump. Silently ignore
1478 unrecognized options; they may be aimed at toplev.c. */
1480 handle_OPT_d (const char *arg)
1484 while ((c = *arg++) != '\0')
1487 case 'M': /* Dump macros only. */
1488 case 'N': /* Dump names. */
1489 case 'D': /* Dump definitions. */
1490 case 'U': /* Dump used macros. */
1491 flag_dump_macros = c;
1495 flag_dump_includes = 1;