PR target/41993
[official-gcc.git] / gcc / c-family / c-opts.c
blobb420d2a2cc14a5d920a196ecb8896124f52f6d21
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
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "c-common.h"
27 #include "c-pragma.h"
28 #include "flags.h"
29 #include "toplev.h"
30 #include "langhooks.h"
31 #include "diagnostic.h"
32 #include "intl.h"
33 #include "cppdefault.h"
34 #include "incpath.h"
35 #include "debug.h" /* For debug_hooks. */
36 #include "opts.h"
37 #include "options.h"
38 #include "mkdeps.h"
39 #include "c-target.h"
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
44 TARGET_OPTF. */
45 #include "tm_p.h" /* For C_COMMON_OVERRIDE_OPTIONS. */
47 #ifndef DOLLARS_IN_IDENTIFIERS
48 # define DOLLARS_IN_IDENTIFIERS true
49 #endif
51 #ifndef TARGET_SYSTEM_ROOT
52 # define TARGET_SYSTEM_ROOT NULL
53 #endif
55 #ifndef TARGET_OPTF
56 #define TARGET_OPTF(ARG)
57 #endif
59 /* CPP's options. */
60 cpp_options *cpp_opts;
62 /* Input filename. */
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;
75 /* If -v seen. */
76 static bool verbose;
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
132 #endif
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
139 enum opt_code code;
140 const char *arg;
141 } *deferred_opts;
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. */
148 static void
149 defer_opt (enum opt_code code, const char *arg)
151 deferred_opts[deferred_count].code = code;
152 deferred_opts[deferred_count].arg = arg;
153 deferred_count++;
156 /* Return language mask for option parsing. */
157 unsigned int
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. */
166 void
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
185 quietly. */
186 static bool accept_all_c_family_options = false;
188 /* Return whether to complain about a wrong-language option. */
189 bool
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))
194 return false;
196 return true;
199 /* Initialize options structure OPTS. */
200 void
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. */
213 void
214 c_common_init_options (unsigned int decoded_options_count,
215 struct cl_decoded_option *decoded_options)
217 unsigned int i;
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;
243 break;
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. */
251 bool
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;
258 bool result = true;
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);
264 switch (code)
266 default:
267 if (cl_options[code].flags & c_family_lang_mask)
269 if ((option->flags & CL_TARGET)
270 && ! targetcm.handle_c_option (scode, arg, value))
271 result = false;
272 break;
274 result = false;
275 break;
277 case OPT__output_pch_:
278 pch_file = arg;
279 break;
281 case OPT_A:
282 defer_opt (code, arg);
283 break;
285 case OPT_C:
286 cpp_opts->discard_comments = 0;
287 break;
289 case OPT_CC:
290 cpp_opts->discard_comments = 0;
291 cpp_opts->discard_comments_in_macro_exp = 0;
292 break;
294 case OPT_D:
295 defer_opt (code, arg);
296 break;
298 case OPT_H:
299 cpp_opts->print_include_names = 1;
300 break;
302 case OPT_F:
303 TARGET_OPTF (xstrdup (arg));
304 break;
306 case OPT_I:
307 if (strcmp (arg, "-"))
308 add_path (xstrdup (arg), BRACKET, 0, true);
309 else
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");
317 break;
319 case OPT_M:
320 case OPT_MM:
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);
326 flag_no_output = 1;
327 break;
329 case OPT_MD:
330 case OPT_MMD:
331 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
332 cpp_opts->deps.need_preprocessor_output = true;
333 deps_file = arg;
334 break;
336 case OPT_MF:
337 deps_seen = true;
338 deps_file = arg;
339 break;
341 case OPT_MG:
342 deps_seen = true;
343 cpp_opts->deps.missing_files = true;
344 break;
346 case OPT_MP:
347 deps_seen = true;
348 cpp_opts->deps.phony_targets = true;
349 break;
351 case OPT_MQ:
352 case OPT_MT:
353 deps_seen = true;
354 defer_opt (code, arg);
355 break;
357 case OPT_P:
358 flag_no_line_commands = 1;
359 break;
361 case OPT_U:
362 defer_opt (code, arg);
363 break;
365 case OPT_Wall:
366 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
367 set_Wformat (value);
368 warn_switch = value;
369 warn_array_bounds = value;
371 /* Only warn about unknown pragmas that are not in system
372 headers. */
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. */
379 if (warn_main == -1)
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;
392 break;
394 case OPT_Wbuiltin_macro_redefined:
395 cpp_opts->warn_builtin_macro_redefined = value;
396 break;
398 case OPT_Wcomment:
399 cpp_opts->warn_comments = value;
400 break;
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;
408 break;
410 case OPT_Wdeprecated:
411 cpp_opts->cpp_warn_deprecated = value;
412 break;
414 case OPT_Wendif_labels:
415 cpp_opts->warn_endif_labels = value;
416 break;
418 case OPT_Wformat:
419 set_Wformat (value);
420 break;
422 case OPT_Wformat_:
423 set_Wformat (atoi (arg));
424 break;
426 case OPT_Winvalid_pch:
427 cpp_opts->warn_invalid_pch = value;
428 break;
430 case OPT_Wliteral_suffix:
431 cpp_opts->warn_literal_suffix = value;
432 break;
434 case OPT_Wlong_long:
435 cpp_opts->cpp_warn_long_long = value;
436 break;
438 case OPT_Wmissing_include_dirs:
439 cpp_opts->warn_missing_include_dirs = value;
440 break;
442 case OPT_Wmultichar:
443 cpp_opts->warn_multichar = value;
444 break;
446 case OPT_Wnormalized_:
447 if (kind == DK_ERROR)
449 gcc_assert (!arg);
450 inform (input_location, "-Werror=normalized=: set -Wnormalized=nfc");
451 cpp_opts->warn_normalize = normalized_C;
453 else
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;
463 else
464 error ("argument %qs to %<-Wnormalized%> not recognized", arg);
465 break;
468 case OPT_Wtraditional:
469 cpp_opts->cpp_warn_traditional = value;
470 break;
472 case OPT_Wtrigraphs:
473 cpp_opts->warn_trigraphs = value;
474 break;
476 case OPT_Wundef:
477 cpp_opts->warn_undef = value;
478 break;
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;
484 break;
486 case OPT_Wunused_macros:
487 warn_unused_macros = value;
488 break;
490 case OPT_Wvariadic_macros:
491 warn_variadic_macros = value;
492 break;
494 case OPT_Weffc__:
495 if (value)
496 warn_nonvdtor = true;
497 break;
499 case OPT_ansi:
500 if (!c_dialect_cxx ())
501 set_std_c89 (false, true);
502 else
503 set_std_cxx98 (true);
504 break;
506 case OPT_d:
507 handle_OPT_d (arg);
508 break;
510 case OPT_fcond_mismatch:
511 if (!c_dialect_cxx ())
513 flag_cond_mismatch = value;
514 break;
516 warning (0, "switch %qs is no longer supported", option->opt_text);
517 break;
519 case OPT_fbuiltin_:
520 if (value)
521 result = false;
522 else
523 disable_builtin_function (arg);
524 break;
526 case OPT_fdirectives_only:
527 cpp_opts->directives_only = value;
528 break;
530 case OPT_fdollars_in_identifiers:
531 cpp_opts->dollars_in_ident = value;
532 break;
534 case OPT_ffreestanding:
535 value = !value;
536 /* Fall through.... */
537 case OPT_fhosted:
538 flag_hosted = value;
539 flag_no_builtin = !value;
540 break;
542 case OPT_fconstant_string_class_:
543 constant_string_class_name = arg;
544 break;
546 case OPT_fextended_identifiers:
547 cpp_opts->extended_identifiers = value;
548 break;
550 case OPT_foperator_names:
551 cpp_opts->operator_names = value;
552 break;
554 case OPT_fpch_deps:
555 cpp_opts->restore_pch_deps = value;
556 break;
558 case OPT_fpch_preprocess:
559 flag_pch_preprocess = value;
560 break;
562 case OPT_fpermissive:
563 flag_permissive = value;
564 global_dc->permissive = value;
565 break;
567 case OPT_fpreprocessed:
568 cpp_opts->preprocessed = value;
569 break;
571 case OPT_fdebug_cpp:
572 cpp_opts->debug = 1;
573 break;
575 case OPT_ftrack_macro_expansion:
576 if (value)
577 value = 2;
578 /* Fall Through. */
580 case OPT_ftrack_macro_expansion_:
581 if (arg && *arg != '\0')
582 cpp_opts->track_macro_expansion = value;
583 else
584 cpp_opts->track_macro_expansion = 2;
585 break;
587 case OPT_frepo:
588 flag_use_repository = value;
589 if (value)
590 flag_implicit_templates = 0;
591 break;
593 case OPT_ftabstop_:
594 /* It is documented that we silently ignore silly values. */
595 if (value >= 1 && value <= 100)
596 cpp_opts->tabstop = value;
597 break;
599 case OPT_fexec_charset_:
600 cpp_opts->narrow_charset = arg;
601 break;
603 case OPT_fwide_exec_charset_:
604 cpp_opts->wide_charset = arg;
605 break;
607 case OPT_finput_charset_:
608 cpp_opts->input_charset = arg;
609 break;
611 case OPT_ftemplate_depth_:
612 max_tinst_depth = value;
613 break;
615 case OPT_fvisibility_inlines_hidden:
616 visibility_options.inlines_hidden = value;
617 break;
619 case OPT_femit_struct_debug_baseonly:
620 set_struct_debug_option (&global_options, loc, "base");
621 break;
623 case OPT_femit_struct_debug_reduced:
624 set_struct_debug_option (&global_options, loc,
625 "dir:ord:sys,dir:gen:any,ind:base");
626 break;
628 case OPT_femit_struct_debug_detailed_:
629 set_struct_debug_option (&global_options, loc, arg);
630 break;
632 case OPT_idirafter:
633 add_path (xstrdup (arg), AFTER, 0, true);
634 break;
636 case OPT_imacros:
637 case OPT_include:
638 defer_opt (code, arg);
639 break;
641 case OPT_imultilib:
642 imultilib = arg;
643 break;
645 case OPT_iprefix:
646 iprefix = arg;
647 break;
649 case OPT_iquote:
650 add_path (xstrdup (arg), QUOTE, 0, true);
651 break;
653 case OPT_isysroot:
654 sysroot = arg;
655 break;
657 case OPT_isystem:
658 add_path (xstrdup (arg), SYSTEM, 0, true);
659 break;
661 case OPT_iwithprefix:
662 add_prefixed_path (arg, SYSTEM);
663 break;
665 case OPT_iwithprefixbefore:
666 add_prefixed_path (arg, BRACKET);
667 break;
669 case OPT_lang_asm:
670 cpp_set_lang (parse_in, CLK_ASM);
671 cpp_opts->dollars_in_ident = false;
672 break;
674 case OPT_nostdinc:
675 std_inc = false;
676 break;
678 case OPT_nostdinc__:
679 std_cxx_inc = false;
680 break;
682 case OPT_o:
683 if (!out_fname)
684 out_fname = arg;
685 else
686 error ("output filename specified twice");
687 break;
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. */
692 case OPT_Wpedantic:
693 cpp_opts->cpp_pedantic = 1;
694 cpp_opts->warn_endif_labels = 1;
695 if (warn_overlength_strings == -1)
696 warn_overlength_strings = 1;
697 if (warn_main == -1)
698 warn_main = 2;
699 break;
701 case OPT_print_objc_runtime_info:
702 print_struct_values = 1;
703 break;
705 case OPT_remap:
706 cpp_opts->remap = 1;
707 break;
709 case OPT_std_c__98:
710 case OPT_std_gnu__98:
711 if (!preprocessing_asm_p)
712 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
713 break;
715 case OPT_std_c__11:
716 case OPT_std_gnu__11:
717 if (!preprocessing_asm_p)
718 set_std_cxx11 (code == OPT_std_c__11 /* ISO */);
719 break;
721 case OPT_std_c__1y:
722 case OPT_std_gnu__1y:
723 if (!preprocessing_asm_p)
724 set_std_cxx1y (code == OPT_std_c__11 /* ISO */);
725 break;
727 case OPT_std_c90:
728 case OPT_std_iso9899_199409:
729 if (!preprocessing_asm_p)
730 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
731 break;
733 case OPT_std_gnu90:
734 if (!preprocessing_asm_p)
735 set_std_c89 (false /* c94 */, false /* ISO */);
736 break;
738 case OPT_std_c99:
739 if (!preprocessing_asm_p)
740 set_std_c99 (true /* ISO */);
741 break;
743 case OPT_std_gnu99:
744 if (!preprocessing_asm_p)
745 set_std_c99 (false /* ISO */);
746 break;
748 case OPT_std_c11:
749 if (!preprocessing_asm_p)
750 set_std_c11 (true /* ISO */);
751 break;
753 case OPT_std_gnu11:
754 if (!preprocessing_asm_p)
755 set_std_c11 (false /* ISO */);
756 break;
758 case OPT_trigraphs:
759 cpp_opts->trigraphs = 1;
760 break;
762 case OPT_traditional_cpp:
763 cpp_opts->traditional = 1;
764 break;
766 case OPT_v:
767 verbose = true;
768 break;
770 case OPT_Wabi:
771 warn_psabi = value;
772 break;
775 switch (c_language)
777 case clk_c:
778 C_handle_option_auto (&global_options, &global_options_set,
779 scode, arg, value,
780 c_family_lang_mask, kind,
781 loc, handlers, global_dc);
782 break;
784 case clk_objc:
785 ObjC_handle_option_auto (&global_options, &global_options_set,
786 scode, arg, value,
787 c_family_lang_mask, kind,
788 loc, handlers, global_dc);
789 break;
791 case clk_cxx:
792 CXX_handle_option_auto (&global_options, &global_options_set,
793 scode, arg, value,
794 c_family_lang_mask, kind,
795 loc, handlers, global_dc);
796 break;
798 case clk_objcxx:
799 ObjCXX_handle_option_auto (&global_options, &global_options_set,
800 scode, arg, value,
801 c_family_lang_mask, kind,
802 loc, handlers, global_dc);
803 break;
805 default:
806 gcc_unreachable ();
809 return result;
812 /* Default implementation of TARGET_HANDLE_C_OPTION. */
814 bool
815 default_handle_c_option (size_t code ATTRIBUTE_UNUSED,
816 const char *arg ATTRIBUTE_UNUSED,
817 int value ATTRIBUTE_UNUSED)
819 return false;
822 /* Post-switch processing. */
823 bool
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);
832 in_fnames[0] = "";
834 else if (strcmp (in_fnames[0], "-") == 0)
835 in_fnames[0] = "";
837 if (out_fname == NULL || !strcmp (out_fname, "-"))
838 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;
854 #endif
856 /* Excess precision other than "fast" requires front-end
857 support. */
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)
881 flag_exceptions = 1;
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
885 in that standard. */
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). */
893 if (warn_main == -1)
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
911 they are used. */
912 if (!warn_format)
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)
939 warn_narrowing = 1;
941 else if (warn_narrowing == -1)
942 warn_narrowing = 0;
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')
950 out_stream = stdout;
951 else
952 out_stream = fopen (out_fname, "w");
954 if (out_stream == NULL)
956 fatal_error ("opening output file %s: %m", out_fname);
957 return false;
960 if (num_in_fnames > 1)
961 error ("too many filenames given. Type %s --help for usage",
962 progname);
964 init_pp_output (out_stream);
966 else
968 init_c_lex ();
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. */
973 if (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)
992 errorcount++;
993 return false;
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)
1003 flag_lto = 0;
1004 flag_generate_lto = 0;
1007 return flag_preprocess_only;
1010 /* Front end initialization common to C, ObjC and C++. */
1011 bool
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
1024 are known. */
1025 cpp_init_iconv (parse_in);
1027 if (version_flag)
1029 int i;
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. */
1037 init_pragma ();
1039 if (flag_preprocess_only)
1041 c_finish_options ();
1042 preprocess_file (parse_in);
1043 return false;
1046 return true;
1049 /* Initialize the integrated preprocessor after debug output has been
1050 initialized; loop over each input file. */
1051 void
1052 c_common_parse_file (void)
1054 unsigned int i;
1056 i = 0;
1057 for (;;)
1059 c_finish_options ();
1060 pch_init ();
1061 push_file_scope ();
1062 c_parse_file ();
1063 pop_file_scope ();
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)
1068 break;
1069 cpp_undef_all (parse_in);
1070 cpp_clear_file_cache (parse_in);
1071 this_input_filename
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)
1076 break;
1080 /* Common finish hook for the C, ObjC and C++ front ends. */
1081 void
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
1090 output stream. */
1091 if (!deps_file)
1092 deps_stream = out_stream;
1093 else
1095 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1096 if (!deps_stream)
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. */
1120 static void
1121 check_deps_environment_vars (void)
1123 char *spec;
1125 spec = getenv ("DEPENDENCIES_OUTPUT");
1126 if (spec)
1127 cpp_opts->deps.style = DEPS_USER;
1128 else
1130 spec = getenv ("SUNPRO_DEPENDENCIES");
1131 if (spec)
1133 cpp_opts->deps.style = DEPS_SYSTEM;
1134 cpp_opts->deps.ignore_main_file = true;
1138 if (spec)
1140 /* Find the space before the DEPS_TARGET, if there is one. */
1141 char *s = strchr (spec, ' ');
1142 if (s)
1144 /* Let the caller perform MAKE quoting. */
1145 defer_opt (OPT_MT, s + 1);
1146 *s = '\0';
1149 /* Command line -MF overrides environment variables and default. */
1150 if (!deps_file)
1151 deps_file = spec;
1153 deps_append = 1;
1154 deps_seen = true;
1158 /* Handle deferred command line switches. */
1159 static void
1160 handle_deferred_opts (void)
1162 size_t i;
1163 struct deps *deps;
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.) */
1168 if (!deps_seen)
1169 return;
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. */
1184 static void
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')
1195 flag_no_output = 1;
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. */
1205 if (flag_no_output)
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. */
1250 static void
1251 add_prefixed_path (const char *suffix, size_t chain)
1253 char *path;
1254 const char *prefix;
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. */
1270 static void
1271 c_finish_options (void)
1273 if (!cpp_opts->preprocessed)
1275 size_t i;
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);
1316 else
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);
1340 else
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);
1351 include_cursor = 0;
1352 push_command_line_include ();
1355 /* Give CPP the next file given by -include, if any. */
1356 static void
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))
1366 return;
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))
1378 return;
1381 if (include_cursor == deferred_count)
1383 include_cursor++;
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. */
1397 static void
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);
1403 else
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 ();
1413 void
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. */
1422 static void
1423 set_std_c89 (int c94, int iso)
1425 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1426 flag_iso = iso;
1427 flag_no_asm = iso;
1428 flag_no_gnu_keywords = iso;
1429 flag_no_nonansi_builtin = iso;
1430 flag_isoc94 = c94;
1431 flag_isoc99 = 0;
1432 flag_isoc11 = 0;
1435 /* Set the C 99 standard (without GNU extensions if ISO). */
1436 static void
1437 set_std_c99 (int iso)
1439 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1440 flag_no_asm = iso;
1441 flag_no_nonansi_builtin = iso;
1442 flag_iso = iso;
1443 flag_isoc11 = 0;
1444 flag_isoc99 = 1;
1445 flag_isoc94 = 1;
1448 /* Set the C 11 standard (without GNU extensions if ISO). */
1449 static void
1450 set_std_c11 (int iso)
1452 cpp_set_lang (parse_in, iso ? CLK_STDC11: CLK_GNUC11);
1453 flag_no_asm = iso;
1454 flag_no_nonansi_builtin = iso;
1455 flag_iso = iso;
1456 flag_isoc11 = 1;
1457 flag_isoc99 = 1;
1458 flag_isoc94 = 1;
1461 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1462 static void
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;
1468 flag_iso = iso;
1469 cxx_dialect = cxx98;
1472 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1473 static void
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;
1479 flag_iso = iso;
1480 /* C++11 includes the C99 standard library. */
1481 flag_isoc94 = 1;
1482 flag_isoc99 = 1;
1483 cxx_dialect = cxx11;
1486 /* Set the C++ 201y draft standard (without GNU extensions if ISO). */
1487 static void
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;
1493 flag_iso = iso;
1494 /* C++11 includes the C99 standard library. */
1495 flag_isoc94 = 1;
1496 flag_isoc99 = 1;
1497 cxx_dialect = cxx1y;
1500 /* Args to -d specify what to dump. Silently ignore
1501 unrecognized options; they may be aimed at toplev.c. */
1502 static void
1503 handle_OPT_d (const char *arg)
1505 char c;
1507 while ((c = *arg++) != '\0')
1508 switch (c)
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;
1515 break;
1517 case 'I':
1518 flag_dump_includes = 1;
1519 break;