c++: enable -fdelete-dead-exceptions by default
[official-gcc.git] / gcc / c-family / c-opts.c
blob1c4e832c7ed09c60ba847ad8ef2d7c34bdca83b6
1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
3 Contributed by Neil Booth.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "c-target.h"
26 #include "c-common.h"
27 #include "memmodel.h"
28 #include "tm_p.h" /* For C_COMMON_OVERRIDE_OPTIONS. */
29 #include "diagnostic.h"
30 #include "c-pragma.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "langhooks.h"
34 #include "tree-diagnostic.h" /* for virt_loc_aware_diagnostic_finalizer */
35 #include "intl.h"
36 #include "cppdefault.h"
37 #include "incpath.h"
38 #include "debug.h" /* For debug_hooks. */
39 #include "opts.h"
40 #include "plugin.h" /* For PLUGIN_INCLUDE_FILE event. */
41 #include "mkdeps.h"
42 #include "dumpfile.h"
43 #include "file-prefix-map.h" /* add_*_prefix_map() */
45 #ifndef DOLLARS_IN_IDENTIFIERS
46 # define DOLLARS_IN_IDENTIFIERS true
47 #endif
49 #ifndef TARGET_SYSTEM_ROOT
50 # define TARGET_SYSTEM_ROOT NULL
51 #endif
53 #ifndef TARGET_OPTF
54 #define TARGET_OPTF(ARG)
55 #endif
57 /* CPP's options. */
58 cpp_options *cpp_opts;
60 /* Input filename. */
61 static const char *this_input_filename;
63 /* Filename and stream for preprocessed output. */
64 static const char *out_fname;
65 static FILE *out_stream;
67 /* Append dependencies to deps_file. */
68 static bool deps_append;
70 /* If dependency switches (-MF etc.) have been given. */
71 static bool deps_seen;
73 /* If -v seen. */
74 static bool verbose;
76 /* Dependency output file. */
77 static const char *deps_file;
79 /* The prefix given by -iprefix, if any. */
80 static const char *iprefix;
82 /* The multilib directory given by -imultilib, if any. */
83 static const char *imultilib;
85 /* The system root, if any. Overridden by -isysroot. */
86 static const char *sysroot = TARGET_SYSTEM_ROOT;
88 /* Zero disables all standard directories for headers. */
89 static bool std_inc = true;
91 /* Zero disables the C++-specific standard directories for headers. */
92 static bool std_cxx_inc = true;
94 /* If the quote chain has been split by -I-. */
95 static bool quote_chain_split;
97 /* Number of deferred options. */
98 static size_t deferred_count;
100 /* Number of deferred options scanned for -include. */
101 static size_t include_cursor;
103 /* Dump files/flags to use during parsing. */
104 static FILE *original_dump_file = NULL;
105 static dump_flags_t original_dump_flags;
107 /* Whether any standard preincluded header has been preincluded. */
108 static bool done_preinclude;
110 static void handle_OPT_d (const char *);
111 static void set_std_cxx98 (int);
112 static void set_std_cxx11 (int);
113 static void set_std_cxx14 (int);
114 static void set_std_cxx17 (int);
115 static void set_std_cxx20 (int);
116 static void set_std_cxx23 (int);
117 static void set_std_c89 (int, int);
118 static void set_std_c99 (int);
119 static void set_std_c11 (int);
120 static void set_std_c17 (int);
121 static void set_std_c2x (int);
122 static void check_deps_environment_vars (void);
123 static void handle_deferred_opts (void);
124 static void sanitize_cpp_opts (void);
125 static void add_prefixed_path (const char *, incpath_kind);
126 static void push_command_line_include (void);
127 static void cb_file_change (cpp_reader *, const line_map_ordinary *);
128 static void cb_dir_change (cpp_reader *, const char *);
129 static void c_finish_options (void);
131 #ifndef STDC_0_IN_SYSTEM_HEADERS
132 #define STDC_0_IN_SYSTEM_HEADERS 0
133 #endif
135 /* Holds switches parsed by c_common_handle_option (), but whose
136 handling is deferred to c_common_post_options (). */
137 static void defer_opt (enum opt_code, const char *);
138 static struct deferred_opt
140 enum opt_code code;
141 const char *arg;
142 } *deferred_opts;
145 extern const unsigned int
146 c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
148 /* Defer option CODE with argument ARG. */
149 static void
150 defer_opt (enum opt_code code, const char *arg)
152 deferred_opts[deferred_count].code = code;
153 deferred_opts[deferred_count].arg = arg;
154 deferred_count++;
157 /* Return language mask for option parsing. */
158 unsigned int
159 c_common_option_lang_mask (void)
161 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
163 return lang_flags[c_language];
166 /* Diagnostic finalizer for C/C++/Objective-C/Objective-C++. */
167 static void
168 c_diagnostic_finalizer (diagnostic_context *context,
169 diagnostic_info *diagnostic,
170 diagnostic_t)
172 char *saved_prefix = pp_take_prefix (context->printer);
173 pp_set_prefix (context->printer, NULL);
174 pp_newline (context->printer);
175 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
176 /* By default print macro expansion contexts in the diagnostic
177 finalizer -- for tokens resulting from macro expansion. */
178 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
179 pp_set_prefix (context->printer, saved_prefix);
180 pp_flush (context->printer);
183 /* Common default settings for diagnostics. */
184 void
185 c_common_diagnostics_set_defaults (diagnostic_context *context)
187 diagnostic_finalizer (context) = c_diagnostic_finalizer;
188 context->opt_permissive = OPT_fpermissive;
191 /* Whether options from all C-family languages should be accepted
192 quietly. */
193 static bool accept_all_c_family_options = false;
195 /* Return whether to complain about a wrong-language option. */
196 bool
197 c_common_complain_wrong_lang_p (const struct cl_option *option)
199 if (accept_all_c_family_options
200 && (option->flags & c_family_lang_mask))
201 return false;
203 return true;
206 /* Initialize options structure OPTS. */
207 void
208 c_common_init_options_struct (struct gcc_options *opts)
210 opts->x_flag_exceptions = c_dialect_cxx ();
211 opts->x_warn_pointer_arith = c_dialect_cxx ();
212 opts->x_warn_write_strings = c_dialect_cxx ();
213 opts->x_flag_warn_unused_result = true;
215 /* By default, C99-like requirements for complex multiply and divide. */
216 opts->x_flag_complex_method = 2;
219 /* Common initialization before calling option handlers. */
220 void
221 c_common_init_options (unsigned int decoded_options_count,
222 struct cl_decoded_option *decoded_options)
224 unsigned int i;
225 struct cpp_callbacks *cb;
227 g_string_concat_db
228 = new (ggc_alloc <string_concat_db> ()) string_concat_db ();
230 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
231 ident_hash, line_table);
232 cb = cpp_get_callbacks (parse_in);
233 cb->diagnostic = c_cpp_diagnostic;
235 cpp_opts = cpp_get_options (parse_in);
236 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
237 cpp_opts->objc = c_dialect_objc ();
238 cpp_opts->deps.modules = true;
240 /* Reset to avoid warnings on internal definitions. We set it just
241 before passing on command-line options to cpplib. */
242 cpp_opts->warn_dollars = 0;
244 deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
246 if (c_language == clk_c)
248 /* The default for C is gnu17. */
249 set_std_c17 (false /* ISO */);
251 /* If preprocessing assembly language, accept any of the C-family
252 front end options since the driver may pass them through. */
253 for (i = 1; i < decoded_options_count; i++)
254 if (decoded_options[i].opt_index == OPT_lang_asm)
256 accept_all_c_family_options = true;
257 break;
261 /* Set C++ standard to C++17 if not specified on the command line. */
262 if (c_dialect_cxx ())
263 set_std_cxx17 (/*ISO*/false);
265 global_dc->colorize_source_p = true;
268 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
269 form of an -f or -W option was given. Returns false if the switch was
270 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
271 bool
272 c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
273 int kind, location_t loc,
274 const struct cl_option_handlers *handlers)
276 const struct cl_option *option = &cl_options[scode];
277 enum opt_code code = (enum opt_code) scode;
278 bool result = true;
280 /* Prevent resetting the language standard to a C dialect when the driver
281 has already determined that we're looking at assembler input. */
282 bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM);
284 switch (code)
286 default:
287 if (cl_options[code].flags & c_family_lang_mask)
289 if ((option->flags & CL_TARGET)
290 && ! targetcm.handle_c_option (scode, arg, value))
291 result = false;
292 break;
294 result = false;
295 break;
297 case OPT__output_pch_:
298 pch_file = arg;
299 break;
301 case OPT_A:
302 defer_opt (code, arg);
303 break;
305 case OPT_C:
306 cpp_opts->discard_comments = 0;
307 break;
309 case OPT_CC:
310 cpp_opts->discard_comments = 0;
311 cpp_opts->discard_comments_in_macro_exp = 0;
312 break;
314 case OPT_D:
315 defer_opt (code, arg);
316 break;
318 case OPT_H:
319 cpp_opts->print_include_names = 1;
320 break;
322 case OPT_F:
323 TARGET_OPTF (xstrdup (arg));
324 break;
326 case OPT_I:
327 if (strcmp (arg, "-"))
328 add_path (xstrdup (arg), INC_BRACKET, 0, true);
329 else
331 if (quote_chain_split)
332 error ("%<-I-%> specified twice");
333 quote_chain_split = true;
334 split_quote_chain ();
335 inform (input_location, "obsolete option %<-I-%> used, "
336 "please use %<-iquote%> instead");
338 break;
340 case OPT_M:
341 case OPT_MM:
342 /* When doing dependencies with -M or -MM, suppress normal
343 preprocessed output, but still do -dM etc. as software
344 depends on this. Preprocessed output does occur if -MD, -MMD
345 or environment var dependency generation is used. */
346 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
347 flag_no_output = 1;
348 break;
350 case OPT_MD:
351 case OPT_MMD:
352 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
353 cpp_opts->deps.need_preprocessor_output = true;
354 deps_file = arg;
355 break;
357 case OPT_MF:
358 deps_seen = true;
359 deps_file = arg;
360 break;
362 case OPT_MG:
363 deps_seen = true;
364 cpp_opts->deps.missing_files = true;
365 break;
367 case OPT_MP:
368 deps_seen = true;
369 cpp_opts->deps.phony_targets = true;
370 break;
372 case OPT_Mmodules:
373 /* Do not set deps_seen, so the user can unconditionally turn
374 this on or off. */
375 cpp_opts->deps.modules = true;
376 break;
378 case OPT_Mno_modules:
379 /* Do not set deps_seen, so the user can unconditionally turn
380 this on or off. */
381 cpp_opts->deps.modules = false;
382 break;
384 case OPT_MQ:
385 case OPT_MT:
386 deps_seen = true;
387 defer_opt (code, arg);
388 break;
390 case OPT_P:
391 flag_no_line_commands = 1;
392 break;
394 case OPT_U:
395 defer_opt (code, arg);
396 break;
398 case OPT_Wall:
399 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
401 cpp_opts->warn_num_sign_change = value;
402 break;
404 case OPT_Wunknown_pragmas:
405 /* Set to greater than 1, so that even unknown pragmas in
406 system headers will be warned about. */
407 /* ??? There is no way to handle this automatically for now. */
408 warn_unknown_pragmas = value * 2;
409 break;
411 case OPT_ansi:
412 if (!c_dialect_cxx ())
413 set_std_c89 (false, true);
414 else
415 set_std_cxx98 (true);
416 break;
418 case OPT_d:
419 handle_OPT_d (arg);
420 break;
422 case OPT_Wabi_:
423 warn_abi = true;
424 if (value == 1)
426 warning (0, "%<-Wabi=1%> is not supported, using =2");
427 value = 2;
429 warn_abi_version = value;
430 break;
432 case OPT_fcanonical_system_headers:
433 cpp_opts->canonical_system_headers = value;
434 break;
436 case OPT_fcond_mismatch:
437 if (!c_dialect_cxx ())
439 flag_cond_mismatch = value;
440 break;
442 warning (0, "switch %qs is no longer supported", option->opt_text);
443 break;
445 case OPT_fbuiltin_:
446 if (value)
447 result = false;
448 else
449 disable_builtin_function (arg);
450 break;
452 case OPT_fdirectives_only:
453 cpp_opts->directives_only = value;
454 break;
456 case OPT_fdollars_in_identifiers:
457 cpp_opts->dollars_in_ident = value;
458 break;
460 case OPT_fmacro_prefix_map_:
461 add_macro_prefix_map (arg);
462 break;
464 case OPT_ffreestanding:
465 value = !value;
466 /* Fall through. */
467 case OPT_fhosted:
468 flag_hosted = value;
469 flag_no_builtin = !value;
470 break;
472 case OPT_fconstant_string_class_:
473 constant_string_class_name = arg;
474 break;
476 case OPT_fextended_identifiers:
477 cpp_opts->extended_identifiers = value;
478 break;
480 case OPT_fmax_include_depth_:
481 cpp_opts->max_include_depth = value;
482 break;
484 case OPT_foperator_names:
485 cpp_opts->operator_names = value;
486 break;
488 case OPT_fpch_deps:
489 cpp_opts->restore_pch_deps = value;
490 break;
492 case OPT_fpch_preprocess:
493 flag_pch_preprocess = value;
494 break;
496 case OPT_fpermissive:
497 flag_permissive = value;
498 global_dc->permissive = value;
499 break;
501 case OPT_fpreprocessed:
502 cpp_opts->preprocessed = value;
503 break;
505 case OPT_fdebug_cpp:
506 cpp_opts->debug = 1;
507 break;
509 case OPT_ftrack_macro_expansion:
510 if (value)
511 value = 2;
512 /* Fall Through. */
514 case OPT_ftrack_macro_expansion_:
515 if (arg && *arg != '\0')
516 cpp_opts->track_macro_expansion = value;
517 else
518 cpp_opts->track_macro_expansion = 2;
519 break;
521 case OPT_fexec_charset_:
522 cpp_opts->narrow_charset = arg;
523 break;
525 case OPT_fwide_exec_charset_:
526 cpp_opts->wide_charset = arg;
527 break;
529 case OPT_finput_charset_:
530 cpp_opts->input_charset = arg;
531 break;
533 case OPT_ftemplate_depth_:
534 max_tinst_depth = value;
535 break;
537 case OPT_fvisibility_inlines_hidden:
538 visibility_options.inlines_hidden = value;
539 break;
541 case OPT_femit_struct_debug_baseonly:
542 set_struct_debug_option (&global_options, loc, "base");
543 break;
545 case OPT_femit_struct_debug_reduced:
546 set_struct_debug_option (&global_options, loc,
547 "dir:ord:sys,dir:gen:any,ind:base");
548 break;
550 case OPT_femit_struct_debug_detailed_:
551 set_struct_debug_option (&global_options, loc, arg);
552 break;
554 case OPT_fext_numeric_literals:
555 cpp_opts->ext_numeric_literals = value;
556 break;
558 case OPT_idirafter:
559 add_path (xstrdup (arg), INC_AFTER, 0, true);
560 break;
562 case OPT_imacros:
563 case OPT_include:
564 defer_opt (code, arg);
565 break;
567 case OPT_imultilib:
568 imultilib = arg;
569 break;
571 case OPT_iprefix:
572 iprefix = arg;
573 break;
575 case OPT_iquote:
576 add_path (xstrdup (arg), INC_QUOTE, 0, true);
577 break;
579 case OPT_isysroot:
580 sysroot = arg;
581 break;
583 case OPT_isystem:
584 add_path (xstrdup (arg), INC_SYSTEM, 0, true);
585 break;
587 case OPT_iwithprefix:
588 add_prefixed_path (arg, INC_SYSTEM);
589 break;
591 case OPT_iwithprefixbefore:
592 add_prefixed_path (arg, INC_BRACKET);
593 break;
595 case OPT_lang_asm:
596 cpp_set_lang (parse_in, CLK_ASM);
597 cpp_opts->dollars_in_ident = false;
598 break;
600 case OPT_nostdinc:
601 std_inc = false;
602 break;
604 case OPT_nostdinc__:
605 std_cxx_inc = false;
606 break;
608 case OPT_o:
609 if (!out_fname)
610 out_fname = arg;
611 else
612 error ("output filename specified twice");
613 break;
615 case OPT_print_objc_runtime_info:
616 print_struct_values = 1;
617 break;
619 case OPT_remap:
620 cpp_opts->remap = 1;
621 break;
623 case OPT_std_c__98:
624 case OPT_std_gnu__98:
625 if (!preprocessing_asm_p)
626 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
627 break;
629 case OPT_std_c__11:
630 case OPT_std_gnu__11:
631 if (!preprocessing_asm_p)
632 set_std_cxx11 (code == OPT_std_c__11 /* ISO */);
633 break;
635 case OPT_std_c__14:
636 case OPT_std_gnu__14:
637 if (!preprocessing_asm_p)
638 set_std_cxx14 (code == OPT_std_c__14 /* ISO */);
639 break;
641 case OPT_std_c__17:
642 case OPT_std_gnu__17:
643 if (!preprocessing_asm_p)
644 set_std_cxx17 (code == OPT_std_c__17 /* ISO */);
645 break;
647 case OPT_std_c__20:
648 case OPT_std_gnu__20:
649 if (!preprocessing_asm_p)
650 set_std_cxx20 (code == OPT_std_c__20 /* ISO */);
651 break;
653 case OPT_std_c__23:
654 case OPT_std_gnu__23:
655 if (!preprocessing_asm_p)
656 set_std_cxx23 (code == OPT_std_c__23 /* ISO */);
657 break;
659 case OPT_std_c90:
660 case OPT_std_iso9899_199409:
661 if (!preprocessing_asm_p)
662 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
663 break;
665 case OPT_std_gnu90:
666 if (!preprocessing_asm_p)
667 set_std_c89 (false /* c94 */, false /* ISO */);
668 break;
670 case OPT_std_c99:
671 if (!preprocessing_asm_p)
672 set_std_c99 (true /* ISO */);
673 break;
675 case OPT_std_gnu99:
676 if (!preprocessing_asm_p)
677 set_std_c99 (false /* ISO */);
678 break;
680 case OPT_std_c11:
681 if (!preprocessing_asm_p)
682 set_std_c11 (true /* ISO */);
683 break;
685 case OPT_std_gnu11:
686 if (!preprocessing_asm_p)
687 set_std_c11 (false /* ISO */);
688 break;
690 case OPT_std_c17:
691 if (!preprocessing_asm_p)
692 set_std_c17 (true /* ISO */);
693 break;
695 case OPT_std_gnu17:
696 if (!preprocessing_asm_p)
697 set_std_c17 (false /* ISO */);
698 break;
700 case OPT_std_c2x:
701 if (!preprocessing_asm_p)
702 set_std_c2x (true /* ISO */);
703 break;
705 case OPT_std_gnu2x:
706 if (!preprocessing_asm_p)
707 set_std_c2x (false /* ISO */);
708 break;
710 case OPT_trigraphs:
711 cpp_opts->trigraphs = 1;
712 break;
714 case OPT_traditional_cpp:
715 cpp_opts->traditional = 1;
716 break;
718 case OPT_v:
719 verbose = true;
720 break;
723 switch (c_language)
725 case clk_c:
726 C_handle_option_auto (&global_options, &global_options_set,
727 scode, arg, value,
728 c_family_lang_mask, kind,
729 loc, handlers, global_dc);
730 break;
732 case clk_objc:
733 ObjC_handle_option_auto (&global_options, &global_options_set,
734 scode, arg, value,
735 c_family_lang_mask, kind,
736 loc, handlers, global_dc);
737 break;
739 case clk_cxx:
740 CXX_handle_option_auto (&global_options, &global_options_set,
741 scode, arg, value,
742 c_family_lang_mask, kind,
743 loc, handlers, global_dc);
744 break;
746 case clk_objcxx:
747 ObjCXX_handle_option_auto (&global_options, &global_options_set,
748 scode, arg, value,
749 c_family_lang_mask, kind,
750 loc, handlers, global_dc);
751 break;
753 default:
754 gcc_unreachable ();
757 cpp_handle_option_auto (&global_options, scode, cpp_opts);
758 return result;
761 /* Default implementation of TARGET_HANDLE_C_OPTION. */
763 bool
764 default_handle_c_option (size_t code ATTRIBUTE_UNUSED,
765 const char *arg ATTRIBUTE_UNUSED,
766 int value ATTRIBUTE_UNUSED)
768 return false;
771 /* Post-switch processing. */
772 bool
773 c_common_post_options (const char **pfilename)
775 /* Canonicalize the input and output filenames. */
776 if (in_fnames == NULL)
778 in_fnames = XNEWVEC (const char *, 1);
779 in_fnames[0] = "";
781 else if (strcmp (in_fnames[0], "-") == 0)
783 if (pch_file)
784 error ("cannot use %<-%> as input filename for a precompiled header");
786 in_fnames[0] = "";
789 if (out_fname == NULL || !strcmp (out_fname, "-"))
790 out_fname = "";
792 if (cpp_opts->deps.style == DEPS_NONE)
793 check_deps_environment_vars ();
795 handle_deferred_opts ();
797 sanitize_cpp_opts ();
799 register_include_chains (parse_in, sysroot, iprefix, imultilib,
800 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
802 #ifdef C_COMMON_OVERRIDE_OPTIONS
803 /* Some machines may reject certain combinations of C
804 language-specific options. */
805 C_COMMON_OVERRIDE_OPTIONS;
806 #endif
808 /* Excess precision other than "fast" requires front-end
809 support. */
810 if (c_dialect_cxx ())
812 if (flag_excess_precision == EXCESS_PRECISION_STANDARD)
813 sorry ("%<-fexcess-precision=standard%> for C++");
814 flag_excess_precision = EXCESS_PRECISION_FAST;
816 else if (flag_excess_precision == EXCESS_PRECISION_DEFAULT)
817 flag_excess_precision = (flag_iso ? EXCESS_PRECISION_STANDARD
818 : EXCESS_PRECISION_FAST);
820 /* ISO C restricts floating-point expression contraction to within
821 source-language expressions (-ffp-contract=on, currently an alias
822 for -ffp-contract=off). */
823 if (flag_iso
824 && !c_dialect_cxx ()
825 && (global_options_set.x_flag_fp_contract_mode
826 == (enum fp_contract_mode) 0)
827 && flag_unsafe_math_optimizations == 0)
828 flag_fp_contract_mode = FP_CONTRACT_OFF;
830 /* If we are compiling C, and we are outside of a standards mode,
831 we can permit the new values from ISO/IEC TS 18661-3 for
832 FLT_EVAL_METHOD. Otherwise, we must restrict the possible values to
833 the set specified in ISO C99/C11. */
834 if (!flag_iso
835 && !c_dialect_cxx ()
836 && (global_options_set.x_flag_permitted_flt_eval_methods
837 == PERMITTED_FLT_EVAL_METHODS_DEFAULT))
838 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_TS_18661;
839 else
840 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_C11;
842 /* C2X Annex F does not permit certain built-in functions to raise
843 "inexact". */
844 if (flag_isoc2x)
845 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
846 flag_fp_int_builtin_inexact, 0);
848 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
849 inline semantics are not supported in GNU89 or C89 mode. */
850 if (flag_gnu89_inline == -1)
851 flag_gnu89_inline = !flag_isoc99;
852 else if (!flag_gnu89_inline && !flag_isoc99)
853 error ("%<-fno-gnu89-inline%> is only supported in GNU99 or C99 mode");
855 /* Default to ObjC sjlj exception handling if NeXT runtime. */
856 if (flag_objc_sjlj_exceptions < 0)
857 flag_objc_sjlj_exceptions = flag_next_runtime;
858 if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
859 flag_exceptions = 1;
861 /* If -ffreestanding, -fno-hosted or -fno-builtin then disable
862 pattern recognition. */
863 if (flag_no_builtin)
864 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
865 flag_tree_loop_distribute_patterns, 0);
867 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
868 It is never enabled in C++, as the minimum limit is not normative
869 in that standard. */
870 if (c_dialect_cxx ())
871 warn_overlength_strings = 0;
873 /* Wmain is enabled by default in C++ but not in C. */
874 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
875 even if -Wall or -Wpedantic was given (warn_main will be 2 if set
876 by -Wall, 1 if set by -Wmain). */
877 if (warn_main == -1)
878 warn_main = (c_dialect_cxx () && flag_hosted) ? 1 : 0;
879 else if (warn_main == 2)
880 warn_main = flag_hosted ? 1 : 0;
882 /* In C, -Wall and -Wc++-compat enable -Wenum-compare; if it has not
883 yet been set, it is disabled by default. In C++, it is enabled
884 by default. */
885 if (warn_enum_compare == -1)
886 warn_enum_compare = c_dialect_cxx () ? 1 : 0;
888 /* -Wpacked-bitfield-compat is on by default for the C languages. The
889 warning is issued in stor-layout.c which is not part of the front-end so
890 we need to selectively turn it on here. */
891 if (warn_packed_bitfield_compat == -1)
892 warn_packed_bitfield_compat = 1;
894 /* Special format checking options don't work without -Wformat; warn if
895 they are used. */
896 if (!warn_format)
898 warning (OPT_Wformat_y2k,
899 "%<-Wformat-y2k%> ignored without %<-Wformat%>");
900 warning (OPT_Wformat_extra_args,
901 "%<-Wformat-extra-args%> ignored without %<-Wformat%>");
902 warning (OPT_Wformat_zero_length,
903 "%<-Wformat-zero-length%> ignored without %<-Wformat%>");
904 warning (OPT_Wformat_nonliteral,
905 "%<-Wformat-nonliteral%> ignored without %<-Wformat%>");
906 warning (OPT_Wformat_contains_nul,
907 "%<-Wformat-contains-nul%> ignored without %<-Wformat%>");
908 warning (OPT_Wformat_security,
909 "%<-Wformat-security%> ignored without %<-Wformat%>");
912 /* -Wimplicit-function-declaration is enabled by default for C99. */
913 if (warn_implicit_function_declaration == -1)
914 warn_implicit_function_declaration = flag_isoc99;
916 /* -Wimplicit-int is enabled by default for C99. */
917 if (warn_implicit_int == -1)
918 warn_implicit_int = flag_isoc99;
920 /* -Wold-style-definition is enabled by default for C2X. */
921 if (warn_old_style_definition == -1)
922 warn_old_style_definition = flag_isoc2x;
924 /* -Wshift-overflow is enabled by default in C99 and C++11 modes. */
925 if (warn_shift_overflow == -1)
926 warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99;
928 /* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 modes. */
929 if (warn_shift_negative_value == -1)
930 warn_shift_negative_value = (extra_warnings
931 && (cxx_dialect >= cxx11 || flag_isoc99));
933 /* -Wregister is enabled by default in C++17. */
934 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_register,
935 cxx_dialect >= cxx17);
937 /* -Wcomma-subscript is enabled by default in C++20. */
938 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
939 warn_comma_subscript,
940 cxx_dialect >= cxx20 && warn_deprecated);
942 /* -Wvolatile is enabled by default in C++20. */
943 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_volatile,
944 cxx_dialect >= cxx20 && warn_deprecated);
946 /* -Wdeprecated-enum-enum-conversion is enabled by default in C++20. */
947 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
948 warn_deprecated_enum_enum_conv,
949 cxx_dialect >= cxx20 && warn_deprecated);
951 /* -Wdeprecated-enum-float-conversion is enabled by default in C++20. */
952 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
953 warn_deprecated_enum_float_conv,
954 cxx_dialect >= cxx20 && warn_deprecated);
956 /* Declone C++ 'structors if -Os. */
957 if (flag_declone_ctor_dtor == -1)
958 flag_declone_ctor_dtor = optimize_size;
960 if (flag_abi_compat_version == 1)
962 warning (0, "%<-fabi-compat-version=1%> is not supported, using =2");
963 flag_abi_compat_version = 2;
966 /* Change flag_abi_version to be the actual current ABI level, for the
967 benefit of c_cpp_builtins, and to make comparison simpler. */
968 const int latest_abi_version = 16;
969 /* Generate compatibility aliases for ABI v11 (7.1) by default. */
970 const int abi_compat_default = 11;
972 #define clamp(X) if (X == 0 || X > latest_abi_version) X = latest_abi_version
973 clamp (flag_abi_version);
974 clamp (warn_abi_version);
975 clamp (flag_abi_compat_version);
976 #undef clamp
978 /* Default -Wabi= or -fabi-compat-version= from each other. */
979 if (warn_abi_version == -1 && flag_abi_compat_version != -1)
980 warn_abi_version = flag_abi_compat_version;
981 else if (flag_abi_compat_version == -1 && warn_abi_version != -1)
982 flag_abi_compat_version = warn_abi_version;
983 else if (warn_abi_version == -1 && flag_abi_compat_version == -1)
985 warn_abi_version = latest_abi_version;
986 if (flag_abi_version == latest_abi_version)
988 auto_diagnostic_group d;
989 if (warning (OPT_Wabi, "%<-Wabi%> won%'t warn about anything"))
991 inform (input_location, "%<-Wabi%> warns about differences "
992 "from the most up-to-date ABI, which is also used "
993 "by default");
994 inform (input_location, "use e.g. %<-Wabi=11%> to warn about "
995 "changes from GCC 7");
997 flag_abi_compat_version = abi_compat_default;
999 else
1000 flag_abi_compat_version = latest_abi_version;
1003 /* By default, enable the new inheriting constructor semantics along with ABI
1004 11. New and old should coexist fine, but it is a change in what
1005 artificial symbols are generated. */
1006 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1007 flag_new_inheriting_ctors,
1008 abi_version_at_least (11));
1010 /* For GCC 7, only enable DR150 resolution by default if -std=c++17. */
1011 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_new_ttp,
1012 cxx_dialect >= cxx17);
1014 /* C++11 guarantees forward progress. */
1015 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_finite_loops,
1016 optimize >= 2 && cxx_dialect >= cxx11);
1018 /* It's OK to discard calls to pure/const functions that might throw. */
1019 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1020 flag_delete_dead_exceptions, true);
1022 if (cxx_dialect >= cxx11)
1024 /* If we're allowing C++0x constructs, don't warn about C++98
1025 identifiers which are keywords in C++0x. */
1026 warn_cxx11_compat = 0;
1027 cpp_opts->cpp_warn_cxx11_compat = 0;
1029 if (warn_narrowing == -1)
1030 warn_narrowing = 1;
1032 /* Unless -f{,no-}ext-numeric-literals has been used explicitly,
1033 for -std=c++{11,14,17,20,23} default to -fno-ext-numeric-literals. */
1034 if (flag_iso && !global_options_set.x_flag_ext_numeric_literals)
1035 cpp_opts->ext_numeric_literals = 0;
1037 else if (warn_narrowing == -1)
1038 warn_narrowing = 0;
1040 /* C++17 has stricter evaluation order requirements; let's use some of them
1041 for earlier C++ as well, so chaining works as expected. */
1042 if (c_dialect_cxx ()
1043 && flag_strong_eval_order == -1)
1044 flag_strong_eval_order = (cxx_dialect >= cxx17 ? 2 : 1);
1046 /* Global sized deallocation is new in C++14. */
1047 if (flag_sized_deallocation == -1)
1048 flag_sized_deallocation = (cxx_dialect >= cxx14);
1050 /* char8_t support is new in C++20. */
1051 if (flag_char8_t == -1)
1052 flag_char8_t = (cxx_dialect >= cxx20);
1054 if (flag_extern_tls_init)
1056 if (!TARGET_SUPPORTS_ALIASES || !SUPPORTS_WEAK)
1058 /* Lazy TLS initialization for a variable in another TU requires
1059 alias and weak reference support. */
1060 if (flag_extern_tls_init > 0)
1061 sorry ("external TLS initialization functions not supported "
1062 "on this target");
1064 flag_extern_tls_init = 0;
1066 else
1067 flag_extern_tls_init = 1;
1070 /* Enable by default only for C++ and C++ with ObjC extensions. */
1071 if (warn_return_type == -1 && c_dialect_cxx ())
1072 warn_return_type = 1;
1074 /* C++20 is the final version of concepts. We still use -fconcepts
1075 to know when concepts are enabled. Note that -fconcepts-ts can
1076 be used to include additional features, although modified to
1077 work with the standard. */
1078 if (cxx_dialect >= cxx20 || flag_concepts_ts)
1079 flag_concepts = 1;
1080 else if (flag_concepts)
1081 /* For -std=c++17 -fconcepts, imply -fconcepts-ts. */
1082 flag_concepts_ts = 1;
1084 if (num_in_fnames > 1)
1085 error ("too many filenames given; type %<%s %s%> for usage",
1086 progname, "--help");
1088 if (flag_preprocess_only)
1090 /* Open the output now. We must do so even if flag_no_output is
1091 on, because there may be other output than from the actual
1092 preprocessing (e.g. from -dM). */
1093 if (out_fname[0] == '\0')
1094 out_stream = stdout;
1095 else
1096 out_stream = fopen (out_fname, "w");
1098 if (out_stream == NULL)
1100 fatal_error (input_location, "opening output file %s: %m", out_fname);
1101 return false;
1104 init_pp_output (out_stream);
1106 else
1108 init_c_lex ();
1110 /* When writing a PCH file, avoid reading some other PCH file,
1111 because the default address space slot then can't be used
1112 for the output PCH file. */
1113 if (pch_file)
1115 c_common_no_more_pch ();
1116 /* Only -g0 and -gdwarf* are supported with PCH, for other
1117 debug formats we warn here and refuse to load any PCH files. */
1118 if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1119 warning (OPT_Wdeprecated,
1120 "the %qs debug info cannot be used with "
1121 "pre-compiled headers",
1122 debug_set_names (write_symbols & ~DWARF2_DEBUG));
1124 else if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1125 c_common_no_more_pch ();
1127 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1128 input_location = UNKNOWN_LOCATION;
1131 struct cpp_callbacks *cb = cpp_get_callbacks (parse_in);
1132 cb->file_change = cb_file_change;
1133 cb->dir_change = cb_dir_change;
1134 if (lang_hooks.preprocess_options)
1135 lang_hooks.preprocess_options (parse_in);
1136 cpp_post_options (parse_in);
1137 init_global_opts_from_cpp (&global_options, cpp_get_options (parse_in));
1139 input_location = UNKNOWN_LOCATION;
1141 *pfilename = this_input_filename
1142 = cpp_read_main_file (parse_in, in_fnames[0],
1143 /* We'll inject preamble pieces if this is
1144 not preprocessed. */
1145 !cpp_opts->preprocessed);
1147 /* Don't do any compilation or preprocessing if there is no input file. */
1148 if (this_input_filename == NULL)
1150 errorcount++;
1151 return false;
1154 if (flag_working_directory
1155 && flag_preprocess_only && !flag_no_line_commands)
1156 pp_dir_change (parse_in, get_src_pwd ());
1158 /* Disable LTO output when outputting a precompiled header. */
1159 if (pch_file && flag_lto)
1161 flag_lto = 0;
1162 flag_generate_lto = 0;
1165 return flag_preprocess_only;
1168 /* Front end initialization common to C, ObjC and C++. */
1169 bool
1170 c_common_init (void)
1172 /* Set up preprocessor arithmetic. Must be done after call to
1173 c_common_nodes_and_builtins for type nodes to be good. */
1174 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1175 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1176 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1177 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1178 cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node);
1179 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1181 /* This can't happen until after wchar_precision and bytes_big_endian
1182 are known. */
1183 cpp_init_iconv (parse_in);
1185 if (version_flag)
1187 int i;
1188 fputs ("Compiler executable checksum: ", stderr);
1189 for (i = 0; i < 16; i++)
1190 fprintf (stderr, "%02x", executable_checksum[i]);
1191 putc ('\n', stderr);
1194 /* Has to wait until now so that cpplib has its hash table. */
1195 init_pragma ();
1197 if (flag_preprocess_only)
1199 c_finish_options ();
1200 preprocess_file (parse_in);
1201 return false;
1204 return true;
1207 /* Initialize the integrated preprocessor after debug output has been
1208 initialized; loop over each input file. */
1209 void
1210 c_common_parse_file (void)
1212 unsigned int i;
1214 i = 0;
1215 for (;;)
1217 c_finish_options ();
1218 /* Open the dump file to use for the original dump output
1219 here, to be used during parsing for the current file. */
1220 original_dump_file = dump_begin (TDI_original, &original_dump_flags);
1221 pch_init ();
1222 push_file_scope ();
1223 c_parse_file ();
1224 pop_file_scope ();
1225 /* And end the main input file, if the debug writer wants it */
1226 if (debug_hooks->start_end_main_source_file)
1227 (*debug_hooks->end_source_file) (0);
1228 if (++i >= num_in_fnames)
1229 break;
1230 cpp_undef_all (parse_in);
1231 cpp_clear_file_cache (parse_in);
1232 this_input_filename
1233 = cpp_read_main_file (parse_in, in_fnames[i]);
1234 if (original_dump_file)
1236 dump_end (TDI_original, original_dump_file);
1237 original_dump_file = NULL;
1239 /* If an input file is missing, abandon further compilation.
1240 cpplib has issued a diagnostic. */
1241 if (!this_input_filename)
1242 break;
1245 c_parse_final_cleanups ();
1248 /* Returns the appropriate dump file for PHASE to dump with FLAGS. */
1250 FILE *
1251 get_dump_info (int phase, dump_flags_t *flags)
1253 gcc_assert (phase == TDI_original);
1255 *flags = original_dump_flags;
1256 return original_dump_file;
1259 /* Common finish hook for the C, ObjC and C++ front ends. */
1260 void
1261 c_common_finish (void)
1263 FILE *deps_stream = NULL;
1265 /* Note that we write the dependencies even if there are errors. This is
1266 useful for handling outdated generated headers that now trigger errors
1267 (for example, with #error) which would be resolved by re-generating
1268 them. In a sense, this complements -MG. */
1269 if (cpp_opts->deps.style != DEPS_NONE)
1271 /* If -M or -MM was seen without -MF, default output to the
1272 output stream. */
1273 if (!deps_file)
1274 deps_stream = out_stream;
1275 else if (deps_file[0] == '-' && deps_file[1] == '\0')
1276 deps_stream = stdout;
1277 else
1279 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1280 if (!deps_stream)
1281 fatal_error (input_location, "opening dependency file %s: %m",
1282 deps_file);
1286 /* For performance, avoid tearing down cpplib's internal structures
1287 with cpp_destroy (). */
1288 cpp_finish (parse_in, deps_stream);
1290 if (deps_stream && deps_stream != out_stream && deps_stream != stdout
1291 && (ferror (deps_stream) || fclose (deps_stream)))
1292 fatal_error (input_location, "closing dependency file %s: %m", deps_file);
1294 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1295 fatal_error (input_location, "when writing output to %s: %m", out_fname);
1298 /* Either of two environment variables can specify output of
1299 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1300 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1301 and DEPS_TARGET is the target to mention in the deps. They also
1302 result in dependency information being appended to the output file
1303 rather than overwriting it, and like Sun's compiler
1304 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1305 static void
1306 check_deps_environment_vars (void)
1308 char *spec;
1310 spec = getenv ("DEPENDENCIES_OUTPUT");
1311 if (spec)
1312 cpp_opts->deps.style = DEPS_USER;
1313 else
1315 spec = getenv ("SUNPRO_DEPENDENCIES");
1316 if (spec)
1318 cpp_opts->deps.style = DEPS_SYSTEM;
1319 cpp_opts->deps.ignore_main_file = true;
1323 if (spec)
1325 /* Find the space before the DEPS_TARGET, if there is one. */
1326 char *s = strchr (spec, ' ');
1327 if (s)
1329 /* Let the caller perform MAKE quoting. */
1330 defer_opt (OPT_MT, s + 1);
1331 *s = '\0';
1334 /* Command line -MF overrides environment variables and default. */
1335 if (!deps_file)
1336 deps_file = spec;
1338 deps_append = 1;
1339 deps_seen = true;
1343 /* Handle deferred command line switches. */
1344 static void
1345 handle_deferred_opts (void)
1347 /* Avoid allocating the deps buffer if we don't need it.
1348 (This flag may be true without there having been -MT or -MQ
1349 options, but we'll still need the deps buffer.) */
1350 if (!deps_seen)
1351 return;
1353 if (mkdeps *deps = cpp_get_deps (parse_in))
1354 for (unsigned i = 0; i < deferred_count; i++)
1356 struct deferred_opt *opt = &deferred_opts[i];
1358 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1359 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
1363 /* These settings are appropriate for GCC, but not necessarily so for
1364 cpplib as a library. */
1365 static void
1366 sanitize_cpp_opts (void)
1368 /* If we don't know what style of dependencies to output, complain
1369 if any other dependency switches have been given. */
1370 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1371 error ("to generate dependencies you must specify either %<-M%> "
1372 "or %<-MM%>");
1374 /* -dM and dependencies suppress normal output; do it here so that
1375 the last -d[MDN] switch overrides earlier ones. */
1376 if (flag_dump_macros == 'M')
1377 flag_no_output = 1;
1379 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1380 to perform proper macro expansion. */
1381 if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
1382 flag_dump_macros = 'D';
1384 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1385 -dM since at least glibc relies on -M -dM to work. */
1386 /* Also, flag_no_output implies flag_no_line_commands, always. */
1387 if (flag_no_output)
1389 if (flag_dump_macros != 'M')
1390 flag_dump_macros = 0;
1391 flag_dump_includes = 0;
1392 flag_no_line_commands = 1;
1394 else if (cpp_opts->deps.missing_files)
1395 error ("%<-MG%> may only be used with %<-M%> or %<-MM%>");
1397 cpp_opts->unsigned_char = !flag_signed_char;
1398 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1400 /* Wlong-long is disabled by default. It is enabled by:
1401 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1402 [-Wpedantic | -Wtraditional] -std=non-c99
1404 Either -Wlong-long or -Wno-long-long override any other settings.
1405 ??? These conditions should be handled in c.opt. */
1406 if (warn_long_long == -1)
1408 warn_long_long = ((pedantic || warn_traditional)
1409 && (c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99));
1410 cpp_opts->cpp_warn_long_long = warn_long_long;
1413 /* If we're generating preprocessor output, emit current directory
1414 if explicitly requested or if debugging information is enabled.
1415 ??? Maybe we should only do it for debugging formats that
1416 actually output the current directory? */
1417 if (flag_working_directory == -1)
1418 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1420 if (warn_implicit_fallthrough < 5)
1421 cpp_opts->cpp_warn_implicit_fallthrough = warn_implicit_fallthrough;
1422 else
1423 cpp_opts->cpp_warn_implicit_fallthrough = 0;
1425 if (cpp_opts->directives_only)
1427 if (cpp_warn_unused_macros)
1428 error ("%<-fdirectives-only%> is incompatible "
1429 "with %<-Wunused-macros%>");
1430 if (cpp_opts->traditional)
1431 error ("%<-fdirectives-only%> is incompatible with %<-traditional%>");
1435 /* Add include path with a prefix at the front of its name. */
1436 static void
1437 add_prefixed_path (const char *suffix, incpath_kind chain)
1439 char *path;
1440 const char *prefix;
1441 size_t prefix_len, suffix_len;
1443 suffix_len = strlen (suffix);
1444 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1445 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1447 path = (char *) xmalloc (prefix_len + suffix_len + 1);
1448 memcpy (path, prefix, prefix_len);
1449 memcpy (path + prefix_len, suffix, suffix_len);
1450 path[prefix_len + suffix_len] = '\0';
1452 add_path (path, chain, 0, false);
1455 /* Handle -D, -U, -A, -imacros, and the first -include. */
1456 static void
1457 c_finish_options (void)
1459 if (!cpp_opts->preprocessed)
1461 const line_map_ordinary *bltin_map
1462 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1463 _("<built-in>"), 0));
1464 cb_file_change (parse_in, bltin_map);
1465 linemap_line_start (line_table, 0, 1);
1467 /* Make sure all of the builtins about to be declared have
1468 BUILTINS_LOCATION has their location_t. */
1469 cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
1471 cpp_init_builtins (parse_in, flag_hosted);
1472 c_cpp_builtins (parse_in);
1474 /* We're about to send user input to cpplib, so make it warn for
1475 things that we previously (when we sent it internal definitions)
1476 told it to not warn.
1478 C99 permits implementation-defined characters in identifiers.
1479 The documented meaning of -std= is to turn off extensions that
1480 conflict with the specified standard, and since a strictly
1481 conforming program cannot contain a '$', we do not condition
1482 their acceptance on the -std= setting. */
1483 cpp_opts->warn_dollars = (cpp_opts->cpp_pedantic && !cpp_opts->c99);
1485 const line_map_ordinary *cmd_map
1486 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1487 _("<command-line>"), 0));
1488 cb_file_change (parse_in, cmd_map);
1489 linemap_line_start (line_table, 0, 1);
1491 /* All command line defines must have the same location. */
1492 cpp_force_token_locations (parse_in, line_table->highest_line);
1493 for (size_t i = 0; i < deferred_count; i++)
1495 struct deferred_opt *opt = &deferred_opts[i];
1497 if (opt->code == OPT_D)
1498 cpp_define (parse_in, opt->arg);
1499 else if (opt->code == OPT_U)
1500 cpp_undef (parse_in, opt->arg);
1501 else if (opt->code == OPT_A)
1503 if (opt->arg[0] == '-')
1504 cpp_unassert (parse_in, opt->arg + 1);
1505 else
1506 cpp_assert (parse_in, opt->arg);
1510 cpp_stop_forcing_token_locations (parse_in);
1512 else if (cpp_opts->directives_only)
1513 cpp_init_special_builtins (parse_in);
1515 /* Start the main input file, if the debug writer wants it. */
1516 if (debug_hooks->start_end_main_source_file
1517 && !flag_preprocess_only)
1518 (*debug_hooks->start_source_file) (0, this_input_filename);
1520 if (!cpp_opts->preprocessed)
1521 /* Handle -imacros after -D and -U. */
1522 for (size_t i = 0; i < deferred_count; i++)
1524 struct deferred_opt *opt = &deferred_opts[i];
1526 if (opt->code == OPT_imacros
1527 && cpp_push_include (parse_in, opt->arg))
1529 /* Disable push_command_line_include callback for now. */
1530 include_cursor = deferred_count + 1;
1531 cpp_scan_nooutput (parse_in);
1535 include_cursor = 0;
1536 push_command_line_include ();
1539 /* Give CPP the next file given by -include, if any. */
1540 static void
1541 push_command_line_include (void)
1543 /* This can happen if disabled by -imacros for example.
1544 Punt so that we don't set "<command-line>" as the filename for
1545 the header. */
1546 if (include_cursor > deferred_count)
1547 return;
1549 if (!done_preinclude)
1551 done_preinclude = true;
1552 if (flag_hosted && std_inc && !cpp_opts->preprocessed)
1554 const char *preinc = targetcm.c_preinclude ();
1555 if (preinc && cpp_push_default_include (parse_in, preinc))
1556 return;
1560 pch_cpp_save_state ();
1562 while (include_cursor < deferred_count)
1564 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1566 if (!cpp_opts->preprocessed && opt->code == OPT_include
1567 && cpp_push_include (parse_in, opt->arg))
1568 return;
1571 if (include_cursor == deferred_count)
1573 include_cursor++;
1574 /* -Wunused-macros should only warn about macros defined hereafter. */
1575 cpp_opts->warn_unused_macros = cpp_warn_unused_macros;
1576 /* Restore the line map back to the main file. */
1577 if (!cpp_opts->preprocessed)
1579 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
1580 if (lang_hooks.preprocess_main_file)
1581 /* We're starting the main file. Inform the FE of that. */
1582 lang_hooks.preprocess_main_file
1583 (parse_in, line_table, LINEMAPS_LAST_ORDINARY_MAP (line_table));
1586 /* Set this here so the client can change the option if it wishes,
1587 and after stacking the main file so we don't trace the main file. */
1588 line_table->trace_includes = cpp_opts->print_include_names;
1592 /* File change callback. Has to handle -include files. */
1593 static void
1594 cb_file_change (cpp_reader *reader, const line_map_ordinary *new_map)
1596 if (flag_preprocess_only)
1597 pp_file_change (new_map);
1598 else
1599 fe_file_change (new_map);
1601 if (new_map && cpp_opts->preprocessed
1602 && lang_hooks.preprocess_main_file && MAIN_FILE_P (new_map)
1603 && ORDINARY_MAP_STARTING_LINE_NUMBER (new_map))
1604 /* We're starting the main file. Inform the FE of that. */
1605 lang_hooks.preprocess_main_file (reader, line_table, new_map);
1607 if (new_map
1608 && (new_map->reason == LC_ENTER || new_map->reason == LC_RENAME))
1610 /* Signal to plugins that a file is included. This could happen
1611 several times with the same file path, e.g. because of
1612 several '#include' or '#line' directives... */
1613 invoke_plugin_callbacks
1614 (PLUGIN_INCLUDE_FILE,
1615 const_cast<char*> (ORDINARY_MAP_FILE_NAME (new_map)));
1618 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1620 pch_cpp_save_state ();
1621 push_command_line_include ();
1625 void
1626 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
1628 if (!set_src_pwd (dir))
1629 warning (0, "too late for # directive to set debug directory");
1632 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1633 extensions if ISO). There is no concept of gnu94. */
1634 static void
1635 set_std_c89 (int c94, int iso)
1637 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1638 flag_iso = iso;
1639 flag_no_asm = iso;
1640 flag_no_gnu_keywords = iso;
1641 flag_no_nonansi_builtin = iso;
1642 flag_isoc94 = c94;
1643 flag_isoc99 = 0;
1644 flag_isoc11 = 0;
1645 flag_isoc2x = 0;
1646 lang_hooks.name = "GNU C89";
1649 /* Set the C 99 standard (without GNU extensions if ISO). */
1650 static void
1651 set_std_c99 (int iso)
1653 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1654 flag_no_asm = iso;
1655 flag_no_nonansi_builtin = iso;
1656 flag_iso = iso;
1657 flag_isoc2x = 0;
1658 flag_isoc11 = 0;
1659 flag_isoc99 = 1;
1660 flag_isoc94 = 1;
1661 lang_hooks.name = "GNU C99";
1664 /* Set the C 11 standard (without GNU extensions if ISO). */
1665 static void
1666 set_std_c11 (int iso)
1668 cpp_set_lang (parse_in, iso ? CLK_STDC11: CLK_GNUC11);
1669 flag_no_asm = iso;
1670 flag_no_nonansi_builtin = iso;
1671 flag_iso = iso;
1672 flag_isoc2x = 0;
1673 flag_isoc11 = 1;
1674 flag_isoc99 = 1;
1675 flag_isoc94 = 1;
1676 lang_hooks.name = "GNU C11";
1679 /* Set the C 17 standard (without GNU extensions if ISO). */
1680 static void
1681 set_std_c17 (int iso)
1683 cpp_set_lang (parse_in, iso ? CLK_STDC17: CLK_GNUC17);
1684 flag_no_asm = iso;
1685 flag_no_nonansi_builtin = iso;
1686 flag_iso = iso;
1687 flag_isoc2x = 0;
1688 flag_isoc11 = 1;
1689 flag_isoc99 = 1;
1690 flag_isoc94 = 1;
1691 lang_hooks.name = "GNU C17";
1694 /* Set the C 2X standard (without GNU extensions if ISO). */
1695 static void
1696 set_std_c2x (int iso)
1698 cpp_set_lang (parse_in, iso ? CLK_STDC2X: CLK_GNUC2X);
1699 flag_no_asm = iso;
1700 flag_no_nonansi_builtin = iso;
1701 flag_iso = iso;
1702 flag_isoc2x = 1;
1703 flag_isoc11 = 1;
1704 flag_isoc99 = 1;
1705 flag_isoc94 = 1;
1706 lang_hooks.name = "GNU C2X";
1710 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1711 static void
1712 set_std_cxx98 (int iso)
1714 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1715 flag_no_gnu_keywords = iso;
1716 flag_no_nonansi_builtin = iso;
1717 flag_iso = iso;
1718 flag_isoc94 = 0;
1719 flag_isoc99 = 0;
1720 cxx_dialect = cxx98;
1721 lang_hooks.name = "GNU C++98";
1724 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1725 static void
1726 set_std_cxx11 (int iso)
1728 cpp_set_lang (parse_in, iso ? CLK_CXX11: CLK_GNUCXX11);
1729 flag_no_gnu_keywords = iso;
1730 flag_no_nonansi_builtin = iso;
1731 flag_iso = iso;
1732 /* C++11 includes the C99 standard library. */
1733 flag_isoc94 = 1;
1734 flag_isoc99 = 1;
1735 cxx_dialect = cxx11;
1736 lang_hooks.name = "GNU C++11";
1739 /* Set the C++ 2014 standard (without GNU extensions if ISO). */
1740 static void
1741 set_std_cxx14 (int iso)
1743 cpp_set_lang (parse_in, iso ? CLK_CXX14: CLK_GNUCXX14);
1744 flag_no_gnu_keywords = iso;
1745 flag_no_nonansi_builtin = iso;
1746 flag_iso = iso;
1747 /* C++14 includes the C99 standard library. */
1748 flag_isoc94 = 1;
1749 flag_isoc99 = 1;
1750 cxx_dialect = cxx14;
1751 lang_hooks.name = "GNU C++14";
1754 /* Set the C++ 2017 standard (without GNU extensions if ISO). */
1755 static void
1756 set_std_cxx17 (int iso)
1758 cpp_set_lang (parse_in, iso ? CLK_CXX17: CLK_GNUCXX17);
1759 flag_no_gnu_keywords = iso;
1760 flag_no_nonansi_builtin = iso;
1761 flag_iso = iso;
1762 /* C++17 includes the C11 standard library. */
1763 flag_isoc94 = 1;
1764 flag_isoc99 = 1;
1765 flag_isoc11 = 1;
1766 cxx_dialect = cxx17;
1767 lang_hooks.name = "GNU C++17";
1770 /* Set the C++ 2020 standard (without GNU extensions if ISO). */
1771 static void
1772 set_std_cxx20 (int iso)
1774 cpp_set_lang (parse_in, iso ? CLK_CXX20: CLK_GNUCXX20);
1775 flag_no_gnu_keywords = iso;
1776 flag_no_nonansi_builtin = iso;
1777 flag_iso = iso;
1778 /* C++20 includes the C11 standard library. */
1779 flag_isoc94 = 1;
1780 flag_isoc99 = 1;
1781 flag_isoc11 = 1;
1782 /* C++20 includes coroutines. */
1783 flag_coroutines = true;
1784 cxx_dialect = cxx20;
1785 lang_hooks.name = "GNU C++20";
1788 /* Set the C++ 2023 standard (without GNU extensions if ISO). */
1789 static void
1790 set_std_cxx23 (int iso)
1792 cpp_set_lang (parse_in, iso ? CLK_CXX23: CLK_GNUCXX23);
1793 flag_no_gnu_keywords = iso;
1794 flag_no_nonansi_builtin = iso;
1795 flag_iso = iso;
1796 /* C++23 includes the C11 standard library. */
1797 flag_isoc94 = 1;
1798 flag_isoc99 = 1;
1799 flag_isoc11 = 1;
1800 /* C++23 includes coroutines. */
1801 flag_coroutines = true;
1802 cxx_dialect = cxx23;
1803 lang_hooks.name = "GNU C++23";
1806 /* Args to -d specify what to dump. Silently ignore
1807 unrecognized options; they may be aimed at toplev.c. */
1808 static void
1809 handle_OPT_d (const char *arg)
1811 char c;
1813 while ((c = *arg++) != '\0')
1814 switch (c)
1816 case 'M': /* Dump macros only. */
1817 case 'N': /* Dump names. */
1818 case 'D': /* Dump definitions. */
1819 case 'U': /* Dump used macros. */
1820 flag_dump_macros = c;
1821 break;
1823 case 'I':
1824 flag_dump_includes = 1;
1825 break;