Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / c-family / c-opts.cc
blobc68a2a2746918e2d71ebea49917f952a1edb3dea
1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002-2023 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() */
44 #include "context.h"
46 #ifndef DOLLARS_IN_IDENTIFIERS
47 # define DOLLARS_IN_IDENTIFIERS true
48 #endif
50 #ifndef TARGET_SYSTEM_ROOT
51 # define TARGET_SYSTEM_ROOT NULL
52 #endif
54 #ifndef TARGET_OPTF
55 #define TARGET_OPTF(ARG)
56 #endif
58 /* CPP's options. */
59 cpp_options *cpp_opts;
61 /* Input filename. */
62 static const char *this_input_filename;
64 /* Filename and stream for preprocessed output. */
65 static const char *out_fname;
66 static FILE *out_stream;
68 /* Append dependencies to deps_file. */
69 static bool deps_append;
71 /* If dependency switches (-MF etc.) have been given. */
72 static bool deps_seen;
74 /* If -v seen. */
75 static bool verbose;
77 /* Dependency output file. */
78 static const char *deps_file;
80 /* The prefix given by -iprefix, if any. */
81 static const char *iprefix;
83 /* The multilib directory given by -imultilib, if any. */
84 static const char *imultilib;
86 /* The system root, if any. Overridden by -isysroot. */
87 static const char *sysroot = TARGET_SYSTEM_ROOT;
89 /* Zero disables all standard directories for headers. */
90 static bool std_inc = true;
92 /* Zero disables the C++-specific standard directories for headers. */
93 static bool std_cxx_inc = true;
95 /* If the quote chain has been split by -I-. */
96 static bool quote_chain_split;
98 /* Number of deferred options. */
99 static size_t deferred_count;
101 /* Number of deferred options scanned for -include. */
102 static size_t include_cursor;
104 /* Whether any standard preincluded header has been preincluded. */
105 static bool done_preinclude;
107 static void handle_OPT_d (const char *);
108 static void set_std_cxx98 (int);
109 static void set_std_cxx11 (int);
110 static void set_std_cxx14 (int);
111 static void set_std_cxx17 (int);
112 static void set_std_cxx20 (int);
113 static void set_std_cxx23 (int);
114 static void set_std_c89 (int, int);
115 static void set_std_c99 (int);
116 static void set_std_c11 (int);
117 static void set_std_c17 (int);
118 static void set_std_c2x (int);
119 static void check_deps_environment_vars (void);
120 static void handle_deferred_opts (void);
121 static void sanitize_cpp_opts (void);
122 static void add_prefixed_path (const char *, incpath_kind);
123 static void push_command_line_include (void);
124 static void cb_file_change (cpp_reader *, const line_map_ordinary *);
125 static void cb_dir_change (cpp_reader *, const char *);
126 static void c_finish_options (void);
128 #ifndef STDC_0_IN_SYSTEM_HEADERS
129 #define STDC_0_IN_SYSTEM_HEADERS 0
130 #endif
132 /* Holds switches parsed by c_common_handle_option (), but whose
133 handling is deferred to c_common_post_options (). */
134 static void defer_opt (enum opt_code, const char *);
135 static struct deferred_opt
137 enum opt_code code;
138 const char *arg;
139 } *deferred_opts;
142 extern const unsigned int
143 c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
145 /* Defer option CODE with argument ARG. */
146 static void
147 defer_opt (enum opt_code code, const char *arg)
149 deferred_opts[deferred_count].code = code;
150 deferred_opts[deferred_count].arg = arg;
151 deferred_count++;
154 /* Return language mask for option parsing. */
155 unsigned int
156 c_common_option_lang_mask (void)
158 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
160 return lang_flags[c_language];
163 /* Diagnostic finalizer for C/C++/Objective-C/Objective-C++. */
164 static void
165 c_diagnostic_finalizer (diagnostic_context *context,
166 diagnostic_info *diagnostic,
167 diagnostic_t)
169 char *saved_prefix = pp_take_prefix (context->printer);
170 pp_set_prefix (context->printer, NULL);
171 pp_newline (context->printer);
172 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
173 /* By default print macro expansion contexts in the diagnostic
174 finalizer -- for tokens resulting from macro expansion. */
175 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
176 pp_set_prefix (context->printer, saved_prefix);
177 pp_flush (context->printer);
180 /* Common default settings for diagnostics. */
181 void
182 c_common_diagnostics_set_defaults (diagnostic_context *context)
184 diagnostic_finalizer (context) = c_diagnostic_finalizer;
185 context->opt_permissive = OPT_fpermissive;
188 /* Input charset configuration for diagnostics. */
189 static const char *
190 c_common_input_charset_cb (const char * /*filename*/)
192 const char *cs = cpp_opts->input_charset;
193 return cpp_input_conversion_is_trivial (cs) ? nullptr : cs;
196 /* Whether options from all C-family languages should be accepted
197 quietly. */
198 static bool accept_all_c_family_options = false;
200 /* Return whether to complain about a wrong-language option. */
201 bool
202 c_common_complain_wrong_lang_p (const struct cl_option *option)
204 if (accept_all_c_family_options
205 && (option->flags & c_family_lang_mask))
206 return false;
208 return true;
211 /* Initialize options structure OPTS. */
212 void
213 c_common_init_options_struct (struct gcc_options *opts)
215 opts->x_flag_exceptions = c_dialect_cxx ();
216 opts->x_warn_pointer_arith = c_dialect_cxx ();
217 opts->x_warn_write_strings = c_dialect_cxx ();
218 opts->x_flag_warn_unused_result = true;
220 /* By default, C99-like requirements for complex multiply and divide. */
221 opts->x_flag_complex_method = 2;
222 opts->x_flag_default_complex_method = opts->x_flag_complex_method;
225 /* Common initialization before calling option handlers. */
226 void
227 c_common_init_options (unsigned int decoded_options_count,
228 struct cl_decoded_option *decoded_options)
230 unsigned int i;
231 struct cpp_callbacks *cb;
233 g_string_concat_db
234 = new (ggc_alloc <string_concat_db> ()) string_concat_db ();
236 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
237 ident_hash, line_table);
238 cb = cpp_get_callbacks (parse_in);
239 cb->diagnostic = c_cpp_diagnostic;
241 cpp_opts = cpp_get_options (parse_in);
242 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
243 cpp_opts->objc = c_dialect_objc ();
244 cpp_opts->deps.modules = true;
246 /* Reset to avoid warnings on internal definitions. We set it just
247 before passing on command-line options to cpplib. */
248 cpp_opts->warn_dollars = 0;
250 deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
252 if (c_language == clk_c)
254 /* The default for C is gnu17. */
255 set_std_c17 (false /* ISO */);
257 /* If preprocessing assembly language, accept any of the C-family
258 front end options since the driver may pass them through. */
259 for (i = 1; i < decoded_options_count; i++)
260 if (decoded_options[i].opt_index == OPT_lang_asm)
262 accept_all_c_family_options = true;
263 break;
267 /* Set C++ standard to C++17 if not specified on the command line. */
268 if (c_dialect_cxx ())
269 set_std_cxx17 (/*ISO*/false);
271 global_dc->colorize_source_p = true;
274 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
275 form of an -f or -W option was given. Returns false if the switch was
276 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
277 bool
278 c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
279 int kind, location_t loc,
280 const struct cl_option_handlers *handlers)
282 const struct cl_option *option = &cl_options[scode];
283 enum opt_code code = (enum opt_code) scode;
284 bool result = true;
286 /* Prevent resetting the language standard to a C dialect when the driver
287 has already determined that we're looking at assembler input. */
288 bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM);
290 switch (code)
292 default:
293 if (cl_options[code].flags & c_family_lang_mask)
295 if ((option->flags & CL_TARGET)
296 && ! targetcm.handle_c_option (scode, arg, value))
297 result = false;
298 break;
300 result = false;
301 break;
303 case OPT__output_pch:
304 pch_file = arg;
305 break;
307 case OPT_A:
308 defer_opt (code, arg);
309 break;
311 case OPT_C:
312 cpp_opts->discard_comments = 0;
313 break;
315 case OPT_CC:
316 cpp_opts->discard_comments = 0;
317 cpp_opts->discard_comments_in_macro_exp = 0;
318 break;
320 case OPT_D:
321 defer_opt (code, arg);
322 break;
324 case OPT_H:
325 cpp_opts->print_include_names = 1;
326 break;
328 case OPT_F:
329 TARGET_OPTF (xstrdup (arg));
330 break;
332 case OPT_I:
333 if (strcmp (arg, "-"))
334 add_path (xstrdup (arg), INC_BRACKET, 0, true);
335 else
337 if (quote_chain_split)
338 error ("%<-I-%> specified twice");
339 quote_chain_split = true;
340 split_quote_chain ();
341 inform (input_location, "obsolete option %<-I-%> used, "
342 "please use %<-iquote%> instead");
344 break;
346 case OPT_M:
347 case OPT_MM:
348 /* When doing dependencies with -M or -MM, suppress normal
349 preprocessed output, but still do -dM etc. as software
350 depends on this. Preprocessed output does occur if -MD, -MMD
351 or environment var dependency generation is used. */
352 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
353 flag_no_output = 1;
354 break;
356 case OPT_MD:
357 case OPT_MMD:
358 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
359 cpp_opts->deps.need_preprocessor_output = true;
360 deps_file = arg;
361 break;
363 case OPT_MF:
364 deps_seen = true;
365 deps_file = arg;
366 break;
368 case OPT_MG:
369 deps_seen = true;
370 cpp_opts->deps.missing_files = true;
371 break;
373 case OPT_MP:
374 deps_seen = true;
375 cpp_opts->deps.phony_targets = true;
376 break;
378 case OPT_Mmodules:
379 /* Do not set deps_seen, so the user can unconditionally turn
380 this on or off. */
381 cpp_opts->deps.modules = true;
382 break;
384 case OPT_Mno_modules:
385 /* Do not set deps_seen, so the user can unconditionally turn
386 this on or off. */
387 cpp_opts->deps.modules = false;
388 break;
390 case OPT_MQ:
391 case OPT_MT:
392 deps_seen = true;
393 defer_opt (code, arg);
394 break;
396 case OPT_P:
397 flag_no_line_commands = 1;
398 break;
400 case OPT_U:
401 defer_opt (code, arg);
402 break;
404 case OPT_Wall:
405 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
407 cpp_opts->warn_num_sign_change = value;
408 break;
410 case OPT_Wunknown_pragmas:
411 /* Set to greater than 1, so that even unknown pragmas in
412 system headers will be warned about. */
413 /* ??? There is no way to handle this automatically for now. */
414 warn_unknown_pragmas = value * 2;
415 break;
417 case OPT_ansi:
418 if (!c_dialect_cxx ())
419 set_std_c89 (false, true);
420 else
421 set_std_cxx98 (true);
422 break;
424 case OPT_d:
425 handle_OPT_d (arg);
426 break;
428 case OPT_Wabi_:
429 warn_abi = true;
430 if (value == 1)
432 warning (0, "%<-Wabi=1%> is not supported, using =2");
433 value = 2;
435 warn_abi_version = value;
436 break;
438 case OPT_fcanonical_system_headers:
439 cpp_opts->canonical_system_headers = value;
440 break;
442 case OPT_fcond_mismatch:
443 if (!c_dialect_cxx ())
445 flag_cond_mismatch = value;
446 break;
448 warning (0, "switch %qs is no longer supported", option->opt_text);
449 break;
451 case OPT_fbuiltin_:
452 if (value)
453 result = false;
454 else
455 disable_builtin_function (arg);
456 break;
458 case OPT_fdirectives_only:
459 cpp_opts->directives_only = value;
460 break;
462 case OPT_fdollars_in_identifiers:
463 cpp_opts->dollars_in_ident = value;
464 break;
466 case OPT_fmacro_prefix_map_:
467 add_macro_prefix_map (arg);
468 break;
470 case OPT_ffreestanding:
471 value = !value;
472 /* Fall through. */
473 case OPT_fhosted:
474 flag_hosted = value;
475 flag_no_builtin = !value;
476 break;
478 case OPT_fconstant_string_class_:
479 constant_string_class_name = arg;
480 break;
482 case OPT_fextended_identifiers:
483 cpp_opts->extended_identifiers = value;
484 break;
486 case OPT_fmax_include_depth_:
487 cpp_opts->max_include_depth = value;
488 break;
490 case OPT_foperator_names:
491 cpp_opts->operator_names = value;
492 break;
494 case OPT_fpch_deps:
495 cpp_opts->restore_pch_deps = value;
496 break;
498 case OPT_fpch_preprocess:
499 flag_pch_preprocess = value;
500 break;
502 case OPT_fpermissive:
503 flag_permissive = value;
504 global_dc->permissive = value;
505 break;
507 case OPT_fpreprocessed:
508 cpp_opts->preprocessed = value;
509 break;
511 case OPT_fdebug_cpp:
512 cpp_opts->debug = 1;
513 break;
515 case OPT_ftrack_macro_expansion:
516 if (value)
517 value = 2;
518 /* Fall Through. */
520 case OPT_ftrack_macro_expansion_:
521 if (arg && *arg != '\0')
522 cpp_opts->track_macro_expansion = value;
523 else
524 cpp_opts->track_macro_expansion = 2;
525 break;
527 case OPT_fexec_charset_:
528 cpp_opts->narrow_charset = arg;
529 break;
531 case OPT_fwide_exec_charset_:
532 cpp_opts->wide_charset = arg;
533 break;
535 case OPT_finput_charset_:
536 cpp_opts->input_charset = arg;
537 cpp_opts->cpp_input_charset_explicit = 1;
538 break;
540 case OPT_ftemplate_depth_:
541 max_tinst_depth = value;
542 break;
544 case OPT_fvisibility_inlines_hidden:
545 visibility_options.inlines_hidden = value;
546 break;
548 case OPT_femit_struct_debug_baseonly:
549 set_struct_debug_option (&global_options, loc, "base");
550 break;
552 case OPT_femit_struct_debug_reduced:
553 set_struct_debug_option (&global_options, loc,
554 "dir:ord:sys,dir:gen:any,ind:base");
555 break;
557 case OPT_femit_struct_debug_detailed_:
558 set_struct_debug_option (&global_options, loc, arg);
559 break;
561 case OPT_fext_numeric_literals:
562 cpp_opts->ext_numeric_literals = value;
563 break;
565 case OPT_idirafter:
566 add_path (xstrdup (arg), INC_AFTER, 0, true);
567 break;
569 case OPT_imacros:
570 case OPT_include:
571 defer_opt (code, arg);
572 break;
574 case OPT_imultilib:
575 imultilib = arg;
576 break;
578 case OPT_iprefix:
579 iprefix = arg;
580 break;
582 case OPT_iquote:
583 add_path (xstrdup (arg), INC_QUOTE, 0, true);
584 break;
586 case OPT_isysroot:
587 sysroot = arg;
588 break;
590 case OPT_isystem:
591 add_path (xstrdup (arg), INC_SYSTEM, 0, true);
592 break;
594 case OPT_iwithprefix:
595 add_prefixed_path (arg, INC_SYSTEM);
596 break;
598 case OPT_iwithprefixbefore:
599 add_prefixed_path (arg, INC_BRACKET);
600 break;
602 case OPT_lang_asm:
603 cpp_set_lang (parse_in, CLK_ASM);
604 cpp_opts->dollars_in_ident = false;
605 break;
607 case OPT_nostdinc:
608 std_inc = false;
609 break;
611 case OPT_nostdinc__:
612 std_cxx_inc = false;
613 break;
615 case OPT_o:
616 if (!out_fname)
617 out_fname = arg;
618 else
619 error ("output filename specified twice");
620 break;
622 case OPT_print_objc_runtime_info:
623 print_struct_values = 1;
624 break;
626 case OPT_remap:
627 cpp_opts->remap = 1;
628 break;
630 case OPT_std_c__98:
631 case OPT_std_gnu__98:
632 if (!preprocessing_asm_p)
633 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
634 break;
636 case OPT_std_c__11:
637 case OPT_std_gnu__11:
638 if (!preprocessing_asm_p)
639 set_std_cxx11 (code == OPT_std_c__11 /* ISO */);
640 break;
642 case OPT_std_c__14:
643 case OPT_std_gnu__14:
644 if (!preprocessing_asm_p)
645 set_std_cxx14 (code == OPT_std_c__14 /* ISO */);
646 break;
648 case OPT_std_c__17:
649 case OPT_std_gnu__17:
650 if (!preprocessing_asm_p)
651 set_std_cxx17 (code == OPT_std_c__17 /* ISO */);
652 break;
654 case OPT_std_c__20:
655 case OPT_std_gnu__20:
656 if (!preprocessing_asm_p)
657 set_std_cxx20 (code == OPT_std_c__20 /* ISO */);
658 break;
660 case OPT_std_c__23:
661 case OPT_std_gnu__23:
662 if (!preprocessing_asm_p)
663 set_std_cxx23 (code == OPT_std_c__23 /* ISO */);
664 break;
666 case OPT_std_c90:
667 case OPT_std_iso9899_199409:
668 if (!preprocessing_asm_p)
669 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
670 break;
672 case OPT_std_gnu90:
673 if (!preprocessing_asm_p)
674 set_std_c89 (false /* c94 */, false /* ISO */);
675 break;
677 case OPT_std_c99:
678 if (!preprocessing_asm_p)
679 set_std_c99 (true /* ISO */);
680 break;
682 case OPT_std_gnu99:
683 if (!preprocessing_asm_p)
684 set_std_c99 (false /* ISO */);
685 break;
687 case OPT_std_c11:
688 if (!preprocessing_asm_p)
689 set_std_c11 (true /* ISO */);
690 break;
692 case OPT_std_gnu11:
693 if (!preprocessing_asm_p)
694 set_std_c11 (false /* ISO */);
695 break;
697 case OPT_std_c17:
698 if (!preprocessing_asm_p)
699 set_std_c17 (true /* ISO */);
700 break;
702 case OPT_std_gnu17:
703 if (!preprocessing_asm_p)
704 set_std_c17 (false /* ISO */);
705 break;
707 case OPT_std_c2x:
708 if (!preprocessing_asm_p)
709 set_std_c2x (true /* ISO */);
710 break;
712 case OPT_std_gnu2x:
713 if (!preprocessing_asm_p)
714 set_std_c2x (false /* ISO */);
715 break;
717 case OPT_trigraphs:
718 cpp_opts->trigraphs = 1;
719 break;
721 case OPT_traditional_cpp:
722 cpp_opts->traditional = 1;
723 break;
725 case OPT_v:
726 verbose = true;
727 break;
730 switch (c_language)
732 case clk_c:
733 C_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_objc:
740 ObjC_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_cxx:
747 CXX_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 case clk_objcxx:
754 ObjCXX_handle_option_auto (&global_options, &global_options_set,
755 scode, arg, value,
756 c_family_lang_mask, kind,
757 loc, handlers, global_dc);
758 break;
760 default:
761 gcc_unreachable ();
764 cpp_handle_option_auto (&global_options, scode, cpp_opts);
765 return result;
768 /* Default implementation of TARGET_HANDLE_C_OPTION. */
770 bool
771 default_handle_c_option (size_t code ATTRIBUTE_UNUSED,
772 const char *arg ATTRIBUTE_UNUSED,
773 int value ATTRIBUTE_UNUSED)
775 return false;
778 /* Post-switch processing. */
779 bool
780 c_common_post_options (const char **pfilename)
782 /* Canonicalize the input and output filenames. */
783 if (in_fnames == NULL)
785 in_fnames = XNEWVEC (const char *, 1);
786 in_fnames[0] = "";
788 else if (strcmp (in_fnames[0], "-") == 0)
790 if (pch_file)
791 error ("cannot use %<-%> as input filename for a precompiled header");
793 in_fnames[0] = "";
796 if (out_fname == NULL || !strcmp (out_fname, "-"))
797 out_fname = "";
799 if (cpp_opts->deps.style == DEPS_NONE)
800 check_deps_environment_vars ();
802 handle_deferred_opts ();
804 sanitize_cpp_opts ();
806 register_include_chains (parse_in, sysroot, iprefix, imultilib,
807 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
809 #ifdef C_COMMON_OVERRIDE_OPTIONS
810 /* Some machines may reject certain combinations of C
811 language-specific options. */
812 C_COMMON_OVERRIDE_OPTIONS;
813 #endif
815 if (flag_excess_precision == EXCESS_PRECISION_DEFAULT)
816 flag_excess_precision = (flag_iso ? EXCESS_PRECISION_STANDARD
817 : EXCESS_PRECISION_FAST);
819 /* ISO C restricts floating-point expression contraction to within
820 source-language expressions (-ffp-contract=on, currently an alias
821 for -ffp-contract=off). */
822 if (flag_iso
823 && !c_dialect_cxx ()
824 && (OPTION_SET_P (flag_fp_contract_mode)
825 == (enum fp_contract_mode) 0)
826 && flag_unsafe_math_optimizations == 0)
827 flag_fp_contract_mode = FP_CONTRACT_OFF;
829 /* If we are compiling C, and we are outside of a standards mode,
830 we can permit the new values from ISO/IEC TS 18661-3 for
831 FLT_EVAL_METHOD. Otherwise, we must restrict the possible values to
832 the set specified in ISO C99/C11. */
833 if (!flag_iso
834 && !c_dialect_cxx ()
835 && (OPTION_SET_P (flag_permitted_flt_eval_methods)
836 == PERMITTED_FLT_EVAL_METHODS_DEFAULT))
837 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_TS_18661;
838 else
839 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_C11;
841 /* C2X Annex F does not permit certain built-in functions to raise
842 "inexact". */
843 if (flag_isoc2x)
844 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
845 flag_fp_int_builtin_inexact, 0);
847 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
848 inline semantics are not supported in GNU89 or C89 mode. */
849 if (flag_gnu89_inline == -1)
850 flag_gnu89_inline = !flag_isoc99;
851 else if (!flag_gnu89_inline && !flag_isoc99)
852 error ("%<-fno-gnu89-inline%> is only supported in GNU99 or C99 mode");
854 /* Default to ObjC sjlj exception handling if NeXT runtime < v2. */
855 if (flag_objc_sjlj_exceptions < 0)
856 flag_objc_sjlj_exceptions = (flag_next_runtime && flag_objc_abi < 2);
857 if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
858 flag_exceptions = 1;
860 /* If -ffreestanding, -fno-hosted or -fno-builtin then disable
861 pattern recognition. */
862 if (flag_no_builtin)
863 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
864 flag_tree_loop_distribute_patterns, 0);
866 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
867 It is never enabled in C++, as the minimum limit is not normative
868 in that standard. */
869 if (c_dialect_cxx ())
870 warn_overlength_strings = 0;
872 /* Wmain is enabled by default in C++ but not in C. */
873 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
874 even if -Wall or -Wpedantic was given (warn_main will be 2 if set
875 by -Wall, 1 if set by -Wmain). */
876 if (warn_main == -1)
877 warn_main = (c_dialect_cxx () && flag_hosted) ? 1 : 0;
878 else if (warn_main == 2)
879 warn_main = flag_hosted ? 1 : 0;
881 /* In C, -Wall and -Wc++-compat enable -Wenum-compare; if it has not
882 yet been set, it is disabled by default. In C++, it is enabled
883 by default. */
884 if (warn_enum_compare == -1)
885 warn_enum_compare = c_dialect_cxx () ? 1 : 0;
887 /* -Wpacked-bitfield-compat is on by default for the C languages. The
888 warning is issued in stor-layout.cc which is not part of the front-end so
889 we need to selectively turn it on here. */
890 if (warn_packed_bitfield_compat == -1)
891 warn_packed_bitfield_compat = 1;
893 /* Special format checking options don't work without -Wformat; warn if
894 they are used. */
895 if (!warn_format)
897 warning (OPT_Wformat_y2k,
898 "%<-Wformat-y2k%> ignored without %<-Wformat%>");
899 warning (OPT_Wformat_extra_args,
900 "%<-Wformat-extra-args%> ignored without %<-Wformat%>");
901 warning (OPT_Wformat_zero_length,
902 "%<-Wformat-zero-length%> ignored without %<-Wformat%>");
903 warning (OPT_Wformat_nonliteral,
904 "%<-Wformat-nonliteral%> ignored without %<-Wformat%>");
905 warning (OPT_Wformat_contains_nul,
906 "%<-Wformat-contains-nul%> ignored without %<-Wformat%>");
907 warning (OPT_Wformat_security,
908 "%<-Wformat-security%> ignored without %<-Wformat%>");
911 /* -Wimplicit-function-declaration is enabled by default for C99. */
912 if (warn_implicit_function_declaration == -1)
913 warn_implicit_function_declaration = flag_isoc99;
915 /* -Wimplicit-int is enabled by default for C99. */
916 if (warn_implicit_int == -1)
917 warn_implicit_int = flag_isoc99;
919 /* -Wold-style-definition is enabled by default for C2X. */
920 if (warn_old_style_definition == -1)
921 warn_old_style_definition = flag_isoc2x;
923 /* -Wshift-overflow is enabled by default in C99 and C++11 modes. */
924 if (warn_shift_overflow == -1)
925 warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99;
927 /* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 to C++17
928 modes. */
929 if (warn_shift_negative_value == -1)
930 warn_shift_negative_value = (extra_warnings
931 && (cxx_dialect >= cxx11 || flag_isoc99)
932 && cxx_dialect < cxx20);
934 /* -Wregister is enabled by default in C++17. */
935 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_register,
936 cxx_dialect >= cxx17);
938 /* -Wcomma-subscript is enabled by default in C++20. */
939 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
940 warn_comma_subscript,
941 cxx_dialect >= cxx23
942 || (cxx_dialect == cxx20 && warn_deprecated));
944 /* -Wvolatile is enabled by default in C++20. */
945 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_volatile,
946 cxx_dialect >= cxx20 && warn_deprecated);
948 /* -Wdeprecated-enum-enum-conversion is enabled by default in C++20. */
949 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
950 warn_deprecated_enum_enum_conv,
951 cxx_dialect >= cxx20 && warn_deprecated);
953 /* -Wdeprecated-enum-float-conversion is enabled by default in C++20. */
954 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
955 warn_deprecated_enum_float_conv,
956 cxx_dialect >= cxx20 && warn_deprecated);
958 /* Declone C++ 'structors if -Os. */
959 if (flag_declone_ctor_dtor == -1)
960 flag_declone_ctor_dtor = optimize_size;
962 if (flag_abi_compat_version == 1)
964 warning (0, "%<-fabi-compat-version=1%> is not supported, using =2");
965 flag_abi_compat_version = 2;
968 /* Change flag_abi_version to be the actual current ABI level, for the
969 benefit of c_cpp_builtins, and to make comparison simpler. */
970 const int latest_abi_version = 18;
971 /* Generate compatibility aliases for ABI v13 (8.2) by default. */
972 const int abi_compat_default = 13;
974 #define clamp(X) if (X == 0 || X > latest_abi_version) X = latest_abi_version
975 clamp (flag_abi_version);
976 clamp (warn_abi_version);
977 clamp (flag_abi_compat_version);
978 #undef clamp
980 /* Default -Wabi= or -fabi-compat-version= from each other. */
981 if (warn_abi_version == -1 && flag_abi_compat_version != -1)
982 warn_abi_version = flag_abi_compat_version;
983 else if (flag_abi_compat_version == -1 && warn_abi_version != -1)
984 flag_abi_compat_version = warn_abi_version;
985 else if (warn_abi_version == -1 && flag_abi_compat_version == -1)
987 warn_abi_version = latest_abi_version;
988 if (flag_abi_version == latest_abi_version)
990 auto_diagnostic_group d;
991 if (warning (OPT_Wabi, "%<-Wabi%> won%'t warn about anything"))
993 inform (input_location, "%<-Wabi%> warns about differences "
994 "from the most up-to-date ABI, which is also used "
995 "by default");
996 inform (input_location, "use e.g. %<-Wabi=11%> to warn about "
997 "changes from GCC 7");
999 flag_abi_compat_version = abi_compat_default;
1001 else
1002 flag_abi_compat_version = latest_abi_version;
1005 /* By default, enable the new inheriting constructor semantics along with ABI
1006 11. New and old should coexist fine, but it is a change in what
1007 artificial symbols are generated. */
1008 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1009 flag_new_inheriting_ctors,
1010 abi_version_at_least (11));
1012 /* For GCC 7, only enable DR150 resolution by default if -std=c++17. */
1013 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_new_ttp,
1014 cxx_dialect >= cxx17);
1016 /* C++11 guarantees forward progress. */
1017 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_finite_loops,
1018 optimize >= 2 && cxx_dialect >= cxx11);
1020 /* It's OK to discard calls to pure/const functions that might throw. */
1021 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1022 flag_delete_dead_exceptions, true);
1024 if (cxx_dialect >= cxx11)
1026 /* If we're allowing C++0x constructs, don't warn about C++98
1027 identifiers which are keywords in C++0x. */
1028 warn_cxx11_compat = 0;
1029 cpp_opts->cpp_warn_cxx11_compat = 0;
1031 if (warn_narrowing == -1)
1032 warn_narrowing = 1;
1034 /* Unless -f{,no-}ext-numeric-literals has been used explicitly,
1035 for -std=c++{11,14,17,20,23} default to -fno-ext-numeric-literals. */
1036 if (flag_iso && !OPTION_SET_P (flag_ext_numeric_literals))
1037 cpp_opts->ext_numeric_literals = 0;
1039 else if (warn_narrowing == -1)
1040 warn_narrowing = 0;
1042 if (cxx_dialect >= cxx20)
1044 /* Don't warn about C++20 compatibility changes in C++20 or later. */
1045 warn_cxx20_compat = 0;
1046 cpp_opts->cpp_warn_cxx20_compat = 0;
1049 /* C++17 has stricter evaluation order requirements; let's use some of them
1050 for earlier C++ as well, so chaining works as expected. */
1051 if (c_dialect_cxx ()
1052 && flag_strong_eval_order == -1)
1053 flag_strong_eval_order = (cxx_dialect >= cxx17 ? 2 : 1);
1055 if (flag_implicit_constexpr && cxx_dialect < cxx14)
1056 flag_implicit_constexpr = false;
1058 /* Global sized deallocation is new in C++14. */
1059 if (flag_sized_deallocation == -1)
1060 flag_sized_deallocation = (cxx_dialect >= cxx14);
1062 /* Pedwarn about invalid constexpr functions before C++23. */
1063 if (warn_invalid_constexpr == -1)
1064 warn_invalid_constexpr = (cxx_dialect < cxx23);
1066 /* char8_t support is implicitly enabled in C++20 and C2X. */
1067 if (flag_char8_t == -1)
1068 flag_char8_t = (cxx_dialect >= cxx20) || flag_isoc2x;
1069 cpp_opts->unsigned_utf8char = flag_char8_t ? 1 : cpp_opts->unsigned_char;
1071 if (flag_extern_tls_init)
1073 if (!TARGET_SUPPORTS_ALIASES || !SUPPORTS_WEAK)
1075 /* Lazy TLS initialization for a variable in another TU requires
1076 alias and weak reference support. */
1077 if (flag_extern_tls_init > 0)
1078 sorry ("external TLS initialization functions not supported "
1079 "on this target");
1081 flag_extern_tls_init = 0;
1083 else
1084 flag_extern_tls_init = 1;
1087 /* Enable by default only for C++ and C++ with ObjC extensions. */
1088 if (warn_return_type == -1 && c_dialect_cxx ())
1089 warn_return_type = 1;
1091 /* C++20 is the final version of concepts. We still use -fconcepts
1092 to know when concepts are enabled. Note that -fconcepts-ts can
1093 be used to include additional features, although modified to
1094 work with the standard. */
1095 if (cxx_dialect >= cxx20 || flag_concepts_ts)
1096 flag_concepts = 1;
1098 if (num_in_fnames > 1)
1099 error ("too many filenames given; type %<%s %s%> for usage",
1100 progname, "--help");
1102 if (flag_preprocess_only)
1104 /* Open the output now. We must do so even if flag_no_output is
1105 on, because there may be other output than from the actual
1106 preprocessing (e.g. from -dM). */
1107 if (out_fname[0] == '\0')
1108 out_stream = stdout;
1109 else
1110 out_stream = fopen (out_fname, "w");
1112 if (out_stream == NULL)
1113 fatal_error (input_location, "opening output file %s: %m", out_fname);
1115 init_pp_output (out_stream);
1117 else
1119 init_c_lex ();
1121 /* When writing a PCH file, avoid reading some other PCH file,
1122 because the default address space slot then can't be used
1123 for the output PCH file. */
1124 if (pch_file)
1126 c_common_no_more_pch ();
1127 /* Only -g0 and -gdwarf* are supported with PCH, for other
1128 debug formats we warn here and refuse to load any PCH files. */
1129 if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1130 warning (OPT_Wdeprecated,
1131 "the %qs debug info cannot be used with "
1132 "pre-compiled headers",
1133 debug_set_names (write_symbols & ~DWARF2_DEBUG));
1135 else if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1136 c_common_no_more_pch ();
1138 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1139 input_location = UNKNOWN_LOCATION;
1142 struct cpp_callbacks *cb = cpp_get_callbacks (parse_in);
1143 cb->file_change = cb_file_change;
1144 cb->dir_change = cb_dir_change;
1145 if (lang_hooks.preprocess_options)
1146 lang_hooks.preprocess_options (parse_in);
1147 cpp_post_options (parse_in);
1148 init_global_opts_from_cpp (&global_options, cpp_get_options (parse_in));
1149 /* For C++23 and explicit -finput-charset=UTF-8, turn on -Winvalid-utf8
1150 by default and make it a pedwarn unless -Wno-invalid-utf8. */
1151 if (cxx_dialect >= cxx23
1152 && cpp_opts->cpp_input_charset_explicit
1153 && strcmp (cpp_opts->input_charset, "UTF-8") == 0
1154 && (cpp_opts->cpp_warn_invalid_utf8
1155 || !global_options_set.x_warn_invalid_utf8))
1157 global_options.x_warn_invalid_utf8 = 1;
1158 cpp_opts->cpp_warn_invalid_utf8 = cpp_opts->cpp_pedantic ? 2 : 1;
1161 /* Let diagnostics infrastructure know how to convert input files the same
1162 way libcpp will do it, namely using the configured input charset and
1163 skipping a UTF-8 BOM if present. */
1164 diagnostic_initialize_input_context (global_dc,
1165 c_common_input_charset_cb, true);
1166 input_location = UNKNOWN_LOCATION;
1168 *pfilename = this_input_filename
1169 = cpp_read_main_file (parse_in, in_fnames[0],
1170 /* We'll inject preamble pieces if this is
1171 not preprocessed. */
1172 !cpp_opts->preprocessed);
1174 /* Don't do any compilation or preprocessing if there is no input file. */
1175 if (this_input_filename == NULL)
1177 errorcount++;
1178 return false;
1181 if (flag_working_directory
1182 && flag_preprocess_only && !flag_no_line_commands)
1183 pp_dir_change (parse_in, get_src_pwd ());
1185 /* Disable LTO output when outputting a precompiled header. */
1186 if (pch_file && flag_lto)
1188 flag_lto = 0;
1189 flag_generate_lto = 0;
1192 return flag_preprocess_only;
1195 /* Front end initialization common to C, ObjC and C++. */
1196 bool
1197 c_common_init (void)
1199 /* Set up preprocessor arithmetic. Must be done after call to
1200 c_common_nodes_and_builtins for type nodes to be good. */
1201 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1202 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1203 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1204 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1205 cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node);
1206 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1208 /* This can't happen until after wchar_precision and bytes_big_endian
1209 are known. */
1210 cpp_init_iconv (parse_in);
1212 if (version_flag)
1214 int i;
1215 fputs ("Compiler executable checksum: ", stderr);
1216 for (i = 0; i < 16; i++)
1217 fprintf (stderr, "%02x", executable_checksum[i]);
1218 putc ('\n', stderr);
1221 /* Has to wait until now so that cpplib has its hash table. */
1222 init_pragma ();
1224 if (flag_preprocess_only)
1226 c_finish_options ();
1227 preprocess_file (parse_in);
1228 return false;
1231 return true;
1234 /* Initialize the integrated preprocessor after debug output has been
1235 initialized; loop over each input file. */
1236 void
1237 c_common_parse_file (void)
1239 auto dumps = g->get_dumps ();
1240 for (unsigned int i = 0;;)
1242 c_finish_options ();
1243 /* Open the dump file to use for the original dump output
1244 here, to be used during parsing for the current file. */
1245 dumps->dump_start (TDI_original, &dump_flags);
1246 pch_init ();
1247 push_file_scope ();
1248 c_parse_file ();
1249 pop_file_scope ();
1250 /* And end the main input file, if the debug writer wants it */
1251 if (debug_hooks->start_end_main_source_file)
1252 (*debug_hooks->end_source_file) (0);
1253 if (++i >= num_in_fnames)
1254 break;
1255 cpp_undef_all (parse_in);
1256 cpp_clear_file_cache (parse_in);
1257 this_input_filename
1258 = cpp_read_main_file (parse_in, in_fnames[i]);
1259 /* If an input file is missing, abandon further compilation.
1260 cpplib has issued a diagnostic. */
1261 if (!this_input_filename)
1262 break;
1263 dumps->dump_finish (TDI_original);
1266 c_parse_final_cleanups ();
1267 dumps->dump_finish (TDI_original);
1270 /* Common finish hook for the C, ObjC and C++ front ends. */
1271 void
1272 c_common_finish (void)
1274 FILE *deps_stream = NULL;
1276 /* Note that we write the dependencies even if there are errors. This is
1277 useful for handling outdated generated headers that now trigger errors
1278 (for example, with #error) which would be resolved by re-generating
1279 them. In a sense, this complements -MG. */
1280 if (cpp_opts->deps.style != DEPS_NONE)
1282 /* If -M or -MM was seen without -MF, default output to the
1283 output stream. */
1284 if (!deps_file)
1285 deps_stream = out_stream;
1286 else if (deps_file[0] == '-' && deps_file[1] == '\0')
1287 deps_stream = stdout;
1288 else
1290 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1291 if (!deps_stream)
1292 fatal_error (input_location, "opening dependency file %s: %m",
1293 deps_file);
1297 /* When we call cpp_finish (), it may generate some diagnostics using
1298 locations it remembered from the preprocessing phase, e.g. for
1299 -Wunused-macros. So inform c_cpp_diagnostic () not to override those
1300 locations with input_location, which would be incorrect now. */
1301 override_libcpp_locations = false;
1303 /* For performance, avoid tearing down cpplib's internal structures
1304 with cpp_destroy (). */
1305 cpp_finish (parse_in, deps_stream);
1307 if (deps_stream && deps_stream != out_stream && deps_stream != stdout
1308 && (ferror (deps_stream) || fclose (deps_stream)))
1309 fatal_error (input_location, "closing dependency file %s: %m", deps_file);
1311 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1312 fatal_error (input_location, "when writing output to %s: %m", out_fname);
1315 /* Either of two environment variables can specify output of
1316 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1317 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1318 and DEPS_TARGET is the target to mention in the deps. They also
1319 result in dependency information being appended to the output file
1320 rather than overwriting it, and like Sun's compiler
1321 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1322 static void
1323 check_deps_environment_vars (void)
1325 char *spec;
1327 spec = getenv ("DEPENDENCIES_OUTPUT");
1328 if (spec)
1329 cpp_opts->deps.style = DEPS_USER;
1330 else
1332 spec = getenv ("SUNPRO_DEPENDENCIES");
1333 if (spec)
1335 cpp_opts->deps.style = DEPS_SYSTEM;
1336 cpp_opts->deps.ignore_main_file = true;
1340 if (spec)
1342 /* Find the space before the DEPS_TARGET, if there is one. */
1343 char *s = strchr (spec, ' ');
1344 if (s)
1346 /* Let the caller perform MAKE quoting. */
1347 defer_opt (OPT_MT, s + 1);
1348 *s = '\0';
1351 /* Command line -MF overrides environment variables and default. */
1352 if (!deps_file)
1353 deps_file = spec;
1355 deps_append = 1;
1356 deps_seen = true;
1360 /* Handle deferred command line switches. */
1361 static void
1362 handle_deferred_opts (void)
1364 /* Avoid allocating the deps buffer if we don't need it.
1365 (This flag may be true without there having been -MT or -MQ
1366 options, but we'll still need the deps buffer.) */
1367 if (!deps_seen)
1368 return;
1370 if (mkdeps *deps = cpp_get_deps (parse_in))
1371 for (unsigned i = 0; i < deferred_count; i++)
1373 struct deferred_opt *opt = &deferred_opts[i];
1375 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1376 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
1380 /* These settings are appropriate for GCC, but not necessarily so for
1381 cpplib as a library. */
1382 static void
1383 sanitize_cpp_opts (void)
1385 /* If we don't know what style of dependencies to output, complain
1386 if any other dependency switches have been given. */
1387 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1388 error ("to generate dependencies you must specify either %<-M%> "
1389 "or %<-MM%>");
1391 /* -dM and dependencies suppress normal output; do it here so that
1392 the last -d[MDN] switch overrides earlier ones. */
1393 if (flag_dump_macros == 'M')
1394 flag_no_output = 1;
1396 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1397 to perform proper macro expansion. */
1398 if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
1399 flag_dump_macros = 'D';
1401 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1402 -dM since at least glibc relies on -M -dM to work. */
1403 /* Also, flag_no_output implies flag_no_line_commands, always. */
1404 if (flag_no_output)
1406 if (flag_dump_macros != 'M')
1407 flag_dump_macros = 0;
1408 flag_dump_includes = 0;
1409 flag_no_line_commands = 1;
1411 else if (cpp_opts->deps.missing_files)
1412 error ("%<-MG%> may only be used with %<-M%> or %<-MM%>");
1414 cpp_opts->unsigned_char = !flag_signed_char;
1415 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1417 /* Wlong-long is disabled by default. It is enabled by:
1418 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1419 [-Wpedantic | -Wtraditional] -std=non-c99
1421 Either -Wlong-long or -Wno-long-long override any other settings.
1422 ??? These conditions should be handled in c.opt. */
1423 if (warn_long_long == -1)
1425 warn_long_long = ((pedantic || warn_traditional)
1426 && (c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99));
1427 cpp_opts->cpp_warn_long_long = warn_long_long;
1430 /* If we're generating preprocessor output, emit current directory
1431 if explicitly requested or if debugging information is enabled.
1432 ??? Maybe we should only do it for debugging formats that
1433 actually output the current directory? */
1434 if (flag_working_directory == -1)
1435 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1437 if (warn_implicit_fallthrough < 5)
1438 cpp_opts->cpp_warn_implicit_fallthrough = warn_implicit_fallthrough;
1439 else
1440 cpp_opts->cpp_warn_implicit_fallthrough = 0;
1442 if (cpp_opts->directives_only)
1444 if (cpp_warn_unused_macros)
1445 error ("%<-fdirectives-only%> is incompatible "
1446 "with %<-Wunused-macros%>");
1447 if (cpp_opts->traditional)
1448 error ("%<-fdirectives-only%> is incompatible with %<-traditional%>");
1452 /* Add include path with a prefix at the front of its name. */
1453 static void
1454 add_prefixed_path (const char *suffix, incpath_kind chain)
1456 char *path;
1457 const char *prefix;
1458 size_t prefix_len, suffix_len;
1460 suffix_len = strlen (suffix);
1461 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1462 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1464 path = (char *) xmalloc (prefix_len + suffix_len + 1);
1465 memcpy (path, prefix, prefix_len);
1466 memcpy (path + prefix_len, suffix, suffix_len);
1467 path[prefix_len + suffix_len] = '\0';
1469 add_path (path, chain, 0, false);
1472 /* Handle -D, -U, -A, -imacros, and the first -include. */
1473 static void
1474 c_finish_options (void)
1476 if (!cpp_opts->preprocessed)
1478 const line_map_ordinary *bltin_map
1479 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1480 special_fname_builtin (), 0));
1481 cb_file_change (parse_in, bltin_map);
1482 linemap_line_start (line_table, 0, 1);
1484 /* Make sure all of the builtins about to be declared have
1485 BUILTINS_LOCATION has their location_t. */
1486 cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
1488 cpp_init_builtins (parse_in, flag_hosted);
1489 c_cpp_builtins (parse_in);
1491 /* We're about to send user input to cpplib, so make it warn for
1492 things that we previously (when we sent it internal definitions)
1493 told it to not warn.
1495 C99 permits implementation-defined characters in identifiers.
1496 The documented meaning of -std= is to turn off extensions that
1497 conflict with the specified standard, and since a strictly
1498 conforming program cannot contain a '$', we do not condition
1499 their acceptance on the -std= setting. */
1500 cpp_opts->warn_dollars = (cpp_opts->cpp_pedantic && !cpp_opts->c99);
1502 const line_map_ordinary *cmd_map
1503 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1504 _("<command-line>"), 0));
1505 cb_file_change (parse_in, cmd_map);
1506 linemap_line_start (line_table, 0, 1);
1508 /* All command line defines must have the same location. */
1509 cpp_force_token_locations (parse_in, line_table->highest_line);
1510 for (size_t i = 0; i < deferred_count; i++)
1512 struct deferred_opt *opt = &deferred_opts[i];
1514 if (opt->code == OPT_D)
1515 cpp_define (parse_in, opt->arg);
1516 else if (opt->code == OPT_U)
1517 cpp_undef (parse_in, opt->arg);
1518 else if (opt->code == OPT_A)
1520 if (opt->arg[0] == '-')
1521 cpp_unassert (parse_in, opt->arg + 1);
1522 else
1523 cpp_assert (parse_in, opt->arg);
1527 cpp_stop_forcing_token_locations (parse_in);
1529 else if (cpp_opts->directives_only)
1530 cpp_init_special_builtins (parse_in);
1532 /* Start the main input file, if the debug writer wants it. */
1533 if (debug_hooks->start_end_main_source_file
1534 && !flag_preprocess_only)
1535 (*debug_hooks->start_source_file) (0, this_input_filename);
1537 if (!cpp_opts->preprocessed)
1538 /* Handle -imacros after -D and -U. */
1539 for (size_t i = 0; i < deferred_count; i++)
1541 struct deferred_opt *opt = &deferred_opts[i];
1543 if (opt->code == OPT_imacros
1544 && cpp_push_include (parse_in, opt->arg))
1546 /* Disable push_command_line_include callback for now. */
1547 include_cursor = deferred_count + 1;
1548 cpp_scan_nooutput (parse_in);
1552 include_cursor = 0;
1553 push_command_line_include ();
1556 /* Give CPP the next file given by -include, if any. */
1557 static void
1558 push_command_line_include (void)
1560 /* This can happen if disabled by -imacros for example.
1561 Punt so that we don't set "<command-line>" as the filename for
1562 the header. */
1563 if (include_cursor > deferred_count)
1564 return;
1566 if (!done_preinclude)
1568 done_preinclude = true;
1569 if (flag_hosted && std_inc && !cpp_opts->preprocessed)
1571 const char *preinc = targetcm.c_preinclude ();
1572 if (preinc && cpp_push_default_include (parse_in, preinc))
1573 return;
1577 pch_cpp_save_state ();
1579 while (include_cursor < deferred_count)
1581 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1583 if (!cpp_opts->preprocessed && opt->code == OPT_include
1584 && cpp_push_include (parse_in, opt->arg))
1585 return;
1588 if (include_cursor == deferred_count)
1590 include_cursor++;
1591 /* -Wunused-macros should only warn about macros defined hereafter. */
1592 cpp_opts->warn_unused_macros = cpp_warn_unused_macros;
1593 /* Restore the line map back to the main file. */
1594 if (!cpp_opts->preprocessed)
1596 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
1597 if (lang_hooks.preprocess_main_file)
1598 /* We're starting the main file. Inform the FE of that. */
1599 lang_hooks.preprocess_main_file
1600 (parse_in, line_table, LINEMAPS_LAST_ORDINARY_MAP (line_table));
1603 /* Set this here so the client can change the option if it wishes,
1604 and after stacking the main file so we don't trace the main file. */
1605 line_table->trace_includes = cpp_opts->print_include_names;
1609 /* File change callback. Has to handle -include files. */
1610 static void
1611 cb_file_change (cpp_reader *reader, const line_map_ordinary *new_map)
1613 if (flag_preprocess_only)
1614 pp_file_change (new_map);
1615 else
1616 fe_file_change (new_map);
1618 if (new_map && cpp_opts->preprocessed
1619 && lang_hooks.preprocess_main_file && MAIN_FILE_P (new_map)
1620 && ORDINARY_MAP_STARTING_LINE_NUMBER (new_map))
1621 /* We're starting the main file. Inform the FE of that. */
1622 lang_hooks.preprocess_main_file (reader, line_table, new_map);
1624 if (new_map
1625 && (new_map->reason == LC_ENTER || new_map->reason == LC_RENAME))
1627 /* Signal to plugins that a file is included. This could happen
1628 several times with the same file path, e.g. because of
1629 several '#include' or '#line' directives... */
1630 invoke_plugin_callbacks
1631 (PLUGIN_INCLUDE_FILE,
1632 const_cast<char*> (ORDINARY_MAP_FILE_NAME (new_map)));
1635 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1637 pch_cpp_save_state ();
1638 push_command_line_include ();
1642 void
1643 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
1645 if (!set_src_pwd (dir))
1646 warning (0, "too late for # directive to set debug directory");
1649 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1650 extensions if ISO). There is no concept of gnu94. */
1651 static void
1652 set_std_c89 (int c94, int iso)
1654 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1655 flag_iso = iso;
1656 flag_no_asm = iso;
1657 flag_no_gnu_keywords = iso;
1658 flag_no_nonansi_builtin = iso;
1659 flag_isoc94 = c94;
1660 flag_isoc99 = 0;
1661 flag_isoc11 = 0;
1662 flag_isoc2x = 0;
1663 lang_hooks.name = "GNU C89";
1666 /* Set the C 99 standard (without GNU extensions if ISO). */
1667 static void
1668 set_std_c99 (int iso)
1670 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1671 flag_no_asm = iso;
1672 flag_no_nonansi_builtin = iso;
1673 flag_iso = iso;
1674 flag_isoc2x = 0;
1675 flag_isoc11 = 0;
1676 flag_isoc99 = 1;
1677 flag_isoc94 = 1;
1678 lang_hooks.name = "GNU C99";
1681 /* Set the C 11 standard (without GNU extensions if ISO). */
1682 static void
1683 set_std_c11 (int iso)
1685 cpp_set_lang (parse_in, iso ? CLK_STDC11: CLK_GNUC11);
1686 flag_no_asm = iso;
1687 flag_no_nonansi_builtin = iso;
1688 flag_iso = iso;
1689 flag_isoc2x = 0;
1690 flag_isoc11 = 1;
1691 flag_isoc99 = 1;
1692 flag_isoc94 = 1;
1693 lang_hooks.name = "GNU C11";
1696 /* Set the C 17 standard (without GNU extensions if ISO). */
1697 static void
1698 set_std_c17 (int iso)
1700 cpp_set_lang (parse_in, iso ? CLK_STDC17: CLK_GNUC17);
1701 flag_no_asm = iso;
1702 flag_no_nonansi_builtin = iso;
1703 flag_iso = iso;
1704 flag_isoc2x = 0;
1705 flag_isoc11 = 1;
1706 flag_isoc99 = 1;
1707 flag_isoc94 = 1;
1708 lang_hooks.name = "GNU C17";
1711 /* Set the C 2X standard (without GNU extensions if ISO). */
1712 static void
1713 set_std_c2x (int iso)
1715 cpp_set_lang (parse_in, iso ? CLK_STDC2X: CLK_GNUC2X);
1716 flag_no_asm = iso;
1717 flag_no_nonansi_builtin = iso;
1718 flag_iso = iso;
1719 flag_isoc2x = 1;
1720 flag_isoc11 = 1;
1721 flag_isoc99 = 1;
1722 flag_isoc94 = 1;
1723 lang_hooks.name = "GNU C2X";
1727 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1728 static void
1729 set_std_cxx98 (int iso)
1731 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1732 flag_no_gnu_keywords = iso;
1733 flag_no_nonansi_builtin = iso;
1734 flag_iso = iso;
1735 flag_isoc94 = 0;
1736 flag_isoc99 = 0;
1737 cxx_dialect = cxx98;
1738 lang_hooks.name = "GNU C++98";
1741 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1742 static void
1743 set_std_cxx11 (int iso)
1745 cpp_set_lang (parse_in, iso ? CLK_CXX11: CLK_GNUCXX11);
1746 flag_no_gnu_keywords = iso;
1747 flag_no_nonansi_builtin = iso;
1748 flag_iso = iso;
1749 /* C++11 includes the C99 standard library. */
1750 flag_isoc94 = 1;
1751 flag_isoc99 = 1;
1752 cxx_dialect = cxx11;
1753 lang_hooks.name = "GNU C++11";
1756 /* Set the C++ 2014 standard (without GNU extensions if ISO). */
1757 static void
1758 set_std_cxx14 (int iso)
1760 cpp_set_lang (parse_in, iso ? CLK_CXX14: CLK_GNUCXX14);
1761 flag_no_gnu_keywords = iso;
1762 flag_no_nonansi_builtin = iso;
1763 flag_iso = iso;
1764 /* C++14 includes the C99 standard library. */
1765 flag_isoc94 = 1;
1766 flag_isoc99 = 1;
1767 cxx_dialect = cxx14;
1768 lang_hooks.name = "GNU C++14";
1771 /* Set the C++ 2017 standard (without GNU extensions if ISO). */
1772 static void
1773 set_std_cxx17 (int iso)
1775 cpp_set_lang (parse_in, iso ? CLK_CXX17: CLK_GNUCXX17);
1776 flag_no_gnu_keywords = iso;
1777 flag_no_nonansi_builtin = iso;
1778 flag_iso = iso;
1779 /* C++17 includes the C11 standard library. */
1780 flag_isoc94 = 1;
1781 flag_isoc99 = 1;
1782 flag_isoc11 = 1;
1783 cxx_dialect = cxx17;
1784 lang_hooks.name = "GNU C++17";
1787 /* Set the C++ 2020 standard (without GNU extensions if ISO). */
1788 static void
1789 set_std_cxx20 (int iso)
1791 cpp_set_lang (parse_in, iso ? CLK_CXX20: CLK_GNUCXX20);
1792 flag_no_gnu_keywords = iso;
1793 flag_no_nonansi_builtin = iso;
1794 flag_iso = iso;
1795 /* C++20 includes the C11 standard library. */
1796 flag_isoc94 = 1;
1797 flag_isoc99 = 1;
1798 flag_isoc11 = 1;
1799 /* C++20 includes coroutines. */
1800 flag_coroutines = true;
1801 cxx_dialect = cxx20;
1802 lang_hooks.name = "GNU C++20";
1805 /* Set the C++ 2023 standard (without GNU extensions if ISO). */
1806 static void
1807 set_std_cxx23 (int iso)
1809 cpp_set_lang (parse_in, iso ? CLK_CXX23: CLK_GNUCXX23);
1810 flag_no_gnu_keywords = iso;
1811 flag_no_nonansi_builtin = iso;
1812 flag_iso = iso;
1813 /* C++23 includes the C11 standard library. */
1814 flag_isoc94 = 1;
1815 flag_isoc99 = 1;
1816 flag_isoc11 = 1;
1817 /* C++23 includes coroutines. */
1818 flag_coroutines = true;
1819 cxx_dialect = cxx23;
1820 lang_hooks.name = "GNU C++23";
1823 /* Args to -d specify what to dump. Silently ignore
1824 unrecognized options; they may be aimed at toplev.cc. */
1825 static void
1826 handle_OPT_d (const char *arg)
1828 char c;
1830 while ((c = *arg++) != '\0')
1831 switch (c)
1833 case 'M': /* Dump macros only. */
1834 case 'N': /* Dump names. */
1835 case 'D': /* Dump definitions. */
1836 case 'U': /* Dump used macros. */
1837 flag_dump_macros = c;
1838 break;
1840 case 'I':
1841 flag_dump_includes = 1;
1842 break;