tree-optimization/112618 - unused .MASK_CALL
[official-gcc.git] / gcc / c-family / c-opts.cc
blob10403c03bd6e902cd97eddef463a8743a6378e0d
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 /* Structured dependency output file. */
81 static const char *fdeps_file;
83 /* The prefix given by -iprefix, if any. */
84 static const char *iprefix;
86 /* The multilib directory given by -imultilib, if any. */
87 static const char *imultilib;
89 /* The system root, if any. Overridden by -isysroot. */
90 static const char *sysroot = TARGET_SYSTEM_ROOT;
92 /* Zero disables all standard directories for headers. */
93 static bool std_inc = true;
95 /* Zero disables the C++-specific standard directories for headers. */
96 static bool std_cxx_inc = true;
98 /* If the quote chain has been split by -I-. */
99 static bool quote_chain_split;
101 /* Number of deferred options. */
102 static size_t deferred_count;
104 /* Number of deferred options scanned for -include. */
105 static size_t include_cursor;
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_cxx26 (int);
118 static void set_std_c89 (int, int);
119 static void set_std_c99 (int);
120 static void set_std_c11 (int);
121 static void set_std_c17 (int);
122 static void set_std_c23 (int);
123 static void check_deps_environment_vars (void);
124 static void handle_deferred_opts (void);
125 static void sanitize_cpp_opts (void);
126 static void add_prefixed_path (const char *, incpath_kind);
127 static void push_command_line_include (void);
128 static void cb_file_change (cpp_reader *, const line_map_ordinary *);
129 static void cb_dir_change (cpp_reader *, const char *);
130 static void c_finish_options (void);
132 #ifndef STDC_0_IN_SYSTEM_HEADERS
133 #define STDC_0_IN_SYSTEM_HEADERS 0
134 #endif
136 /* Holds switches parsed by c_common_handle_option (), but whose
137 handling is deferred to c_common_post_options (). */
138 static void defer_opt (enum opt_code, const char *);
139 static struct deferred_opt
141 enum opt_code code;
142 const char *arg;
143 } *deferred_opts;
146 extern const unsigned int
147 c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
149 /* Defer option CODE with argument ARG. */
150 static void
151 defer_opt (enum opt_code code, const char *arg)
153 deferred_opts[deferred_count].code = code;
154 deferred_opts[deferred_count].arg = arg;
155 deferred_count++;
158 /* Return language mask for option parsing. */
159 unsigned int
160 c_common_option_lang_mask (void)
162 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
164 return lang_flags[c_language];
167 /* Diagnostic finalizer for C/C++/Objective-C/Objective-C++. */
168 static void
169 c_diagnostic_finalizer (diagnostic_context *context,
170 diagnostic_info *diagnostic,
171 diagnostic_t)
173 char *saved_prefix = pp_take_prefix (context->printer);
174 pp_set_prefix (context->printer, NULL);
175 pp_newline (context->printer);
176 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
177 /* By default print macro expansion contexts in the diagnostic
178 finalizer -- for tokens resulting from macro expansion. */
179 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
180 pp_set_prefix (context->printer, saved_prefix);
181 pp_flush (context->printer);
184 /* Common default settings for diagnostics. */
185 void
186 c_common_diagnostics_set_defaults (diagnostic_context *context)
188 diagnostic_finalizer (context) = c_diagnostic_finalizer;
189 context->m_opt_permissive = OPT_fpermissive;
192 /* Input charset configuration for diagnostics. */
193 static const char *
194 c_common_input_charset_cb (const char * /*filename*/)
196 const char *cs = cpp_opts->input_charset;
197 return cpp_input_conversion_is_trivial (cs) ? nullptr : cs;
200 /* Whether options from all C-family languages should be accepted
201 quietly. */
202 static bool accept_all_c_family_options = false;
204 /* Return whether to complain about a wrong-language option. */
205 bool
206 c_common_complain_wrong_lang_p (const struct cl_option *option)
208 if (accept_all_c_family_options
209 && (option->flags & c_family_lang_mask))
210 return false;
212 return true;
215 /* Initialize options structure OPTS. */
216 void
217 c_common_init_options_struct (struct gcc_options *opts)
219 opts->x_flag_exceptions = c_dialect_cxx ();
220 opts->x_warn_pointer_arith = c_dialect_cxx ();
221 opts->x_warn_write_strings = c_dialect_cxx ();
222 opts->x_flag_warn_unused_result = true;
224 /* By default, C99-like requirements for complex multiply and divide. */
225 opts->x_flag_complex_method = 2;
226 opts->x_flag_default_complex_method = opts->x_flag_complex_method;
229 /* Common initialization before calling option handlers. */
230 void
231 c_common_init_options (unsigned int decoded_options_count,
232 struct cl_decoded_option *decoded_options)
234 unsigned int i;
235 struct cpp_callbacks *cb;
237 g_string_concat_db
238 = new (ggc_alloc <string_concat_db> ()) string_concat_db ();
240 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
241 ident_hash, line_table, ident_hash_extra);
242 cb = cpp_get_callbacks (parse_in);
243 cb->diagnostic = c_cpp_diagnostic;
245 cpp_opts = cpp_get_options (parse_in);
246 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
247 cpp_opts->objc = c_dialect_objc ();
248 cpp_opts->deps.modules = true;
250 /* Reset to avoid warnings on internal definitions. We set it just
251 before passing on command-line options to cpplib. */
252 cpp_opts->warn_dollars = 0;
254 deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
256 if (c_language == clk_c)
258 /* The default for C is gnu17. */
259 set_std_c17 (false /* ISO */);
261 /* If preprocessing assembly language, accept any of the C-family
262 front end options since the driver may pass them through. */
263 for (i = 1; i < decoded_options_count; i++)
264 if (decoded_options[i].opt_index == OPT_lang_asm)
266 accept_all_c_family_options = true;
267 break;
271 /* Set C++ standard to C++17 if not specified on the command line. */
272 if (c_dialect_cxx ())
273 set_std_cxx17 (/*ISO*/false);
275 global_dc->m_source_printing.colorize_source_p = true;
278 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
279 form of an -f or -W option was given. Returns false if the switch was
280 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
281 bool
282 c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
283 int kind, location_t loc,
284 const struct cl_option_handlers *handlers)
286 const struct cl_option *option = &cl_options[scode];
287 enum opt_code code = (enum opt_code) scode;
288 bool result = true;
290 /* Prevent resetting the language standard to a C dialect when the driver
291 has already determined that we're looking at assembler input. */
292 bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM);
294 switch (code)
296 default:
297 if (cl_options[code].flags & c_family_lang_mask)
299 if ((option->flags & CL_TARGET)
300 && ! targetcm.handle_c_option (scode, arg, value))
301 result = false;
302 break;
304 result = false;
305 break;
307 case OPT__output_pch:
308 pch_file = arg;
309 break;
311 case OPT_A:
312 defer_opt (code, arg);
313 break;
315 case OPT_C:
316 cpp_opts->discard_comments = 0;
317 break;
319 case OPT_CC:
320 cpp_opts->discard_comments = 0;
321 cpp_opts->discard_comments_in_macro_exp = 0;
322 break;
324 case OPT_D:
325 defer_opt (code, arg);
326 break;
328 case OPT_H:
329 cpp_opts->print_include_names = 1;
330 break;
332 case OPT_F:
333 TARGET_OPTF (xstrdup (arg));
334 break;
336 case OPT_I:
337 if (strcmp (arg, "-"))
338 add_path (xstrdup (arg), INC_BRACKET, 0, true);
339 else
341 if (quote_chain_split)
342 error ("%<-I-%> specified twice");
343 quote_chain_split = true;
344 split_quote_chain ();
345 inform (input_location, "obsolete option %<-I-%> used, "
346 "please use %<-iquote%> instead");
348 break;
350 case OPT_M:
351 case OPT_MM:
352 /* When doing dependencies with -M or -MM, suppress normal
353 preprocessed output, but still do -dM etc. as software
354 depends on this. Preprocessed output does occur if -MD, -MMD
355 or environment var dependency generation is used. */
356 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
357 flag_no_output = 1;
358 break;
360 case OPT_MD:
361 case OPT_MMD:
362 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
363 cpp_opts->deps.need_preprocessor_output = true;
364 deps_file = arg;
365 break;
367 case OPT_fdeps_format_:
368 /* https://wg21.link/p1689r5 */
369 if (!strcmp (arg, "p1689r5"))
370 cpp_opts->deps.fdeps_format = FDEPS_FMT_P1689R5;
371 else
372 error ("%<-fdeps-format=%> unknown format %<%s%>", arg);
373 break;
375 case OPT_fdeps_file_:
376 deps_seen = true;
377 fdeps_file = arg;
378 break;
380 case OPT_fdeps_target_:
381 deps_seen = true;
382 defer_opt (code, arg);
383 break;
385 case OPT_MF:
386 deps_seen = true;
387 deps_file = arg;
388 break;
390 case OPT_MG:
391 deps_seen = true;
392 cpp_opts->deps.missing_files = true;
393 break;
395 case OPT_MP:
396 deps_seen = true;
397 cpp_opts->deps.phony_targets = true;
398 break;
400 case OPT_Mmodules:
401 /* Do not set deps_seen, so the user can unconditionally turn
402 this on or off. */
403 cpp_opts->deps.modules = true;
404 break;
406 case OPT_Mno_modules:
407 /* Do not set deps_seen, so the user can unconditionally turn
408 this on or off. */
409 cpp_opts->deps.modules = false;
410 break;
412 case OPT_MQ:
413 case OPT_MT:
414 deps_seen = true;
415 defer_opt (code, arg);
416 break;
418 case OPT_P:
419 flag_no_line_commands = 1;
420 break;
422 case OPT_U:
423 defer_opt (code, arg);
424 break;
426 case OPT_Wall:
427 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
429 cpp_opts->warn_num_sign_change = value;
430 break;
432 case OPT_Wunknown_pragmas:
433 /* Set to greater than 1, so that even unknown pragmas in
434 system headers will be warned about. */
435 /* ??? There is no way to handle this automatically for now. */
436 warn_unknown_pragmas = value * 2;
437 break;
439 case OPT_ansi:
440 if (!c_dialect_cxx ())
441 set_std_c89 (false, true);
442 else
443 set_std_cxx98 (true);
444 break;
446 case OPT_d:
447 handle_OPT_d (arg);
448 break;
450 case OPT_Wabi_:
451 warn_abi = true;
452 if (value == 1)
454 warning (0, "%<-Wabi=1%> is not supported, using =2");
455 value = 2;
457 warn_abi_version = value;
458 break;
460 case OPT_fcanonical_system_headers:
461 cpp_opts->canonical_system_headers = value;
462 break;
464 case OPT_fcond_mismatch:
465 if (!c_dialect_cxx ())
467 flag_cond_mismatch = value;
468 break;
470 warning (0, "switch %qs is no longer supported", option->opt_text);
471 break;
473 case OPT_fbuiltin_:
474 if (value)
475 result = false;
476 else
477 disable_builtin_function (arg);
478 break;
480 case OPT_fdirectives_only:
481 cpp_opts->directives_only = value;
482 break;
484 case OPT_fdollars_in_identifiers:
485 cpp_opts->dollars_in_ident = value;
486 break;
488 case OPT_fmacro_prefix_map_:
489 add_macro_prefix_map (arg);
490 break;
492 case OPT_ffreestanding:
493 value = !value;
494 /* Fall through. */
495 case OPT_fhosted:
496 flag_hosted = value;
497 flag_no_builtin = !value;
498 break;
500 case OPT_fconstant_string_class_:
501 constant_string_class_name = arg;
502 break;
504 case OPT_fextended_identifiers:
505 cpp_opts->extended_identifiers = value;
506 break;
508 case OPT_fmax_include_depth_:
509 cpp_opts->max_include_depth = value;
510 break;
512 case OPT_foperator_names:
513 cpp_opts->operator_names = value;
514 break;
516 case OPT_fpch_deps:
517 cpp_opts->restore_pch_deps = value;
518 break;
520 case OPT_fpch_preprocess:
521 flag_pch_preprocess = value;
522 break;
524 case OPT_fpermissive:
525 flag_permissive = value;
526 global_dc->m_permissive = value;
527 break;
529 case OPT_fpreprocessed:
530 cpp_opts->preprocessed = value;
531 break;
533 case OPT_fdebug_cpp:
534 cpp_opts->debug = 1;
535 break;
537 case OPT_ftrack_macro_expansion:
538 if (value)
539 value = 2;
540 /* Fall Through. */
542 case OPT_ftrack_macro_expansion_:
543 if (arg && *arg != '\0')
544 cpp_opts->track_macro_expansion = value;
545 else
546 cpp_opts->track_macro_expansion = 2;
547 break;
549 case OPT_fexec_charset_:
550 cpp_opts->narrow_charset = arg;
551 break;
553 case OPT_fwide_exec_charset_:
554 cpp_opts->wide_charset = arg;
555 break;
557 case OPT_finput_charset_:
558 cpp_opts->input_charset = arg;
559 cpp_opts->cpp_input_charset_explicit = 1;
560 break;
562 case OPT_ftemplate_depth_:
563 max_tinst_depth = value;
564 break;
566 case OPT_fvisibility_inlines_hidden:
567 visibility_options.inlines_hidden = value;
568 break;
570 case OPT_femit_struct_debug_baseonly:
571 set_struct_debug_option (&global_options, loc, "base");
572 break;
574 case OPT_femit_struct_debug_reduced:
575 set_struct_debug_option (&global_options, loc,
576 "dir:ord:sys,dir:gen:any,ind:base");
577 break;
579 case OPT_femit_struct_debug_detailed_:
580 set_struct_debug_option (&global_options, loc, arg);
581 break;
583 case OPT_fext_numeric_literals:
584 cpp_opts->ext_numeric_literals = value;
585 break;
587 case OPT_idirafter:
588 add_path (xstrdup (arg), INC_AFTER, 0, true);
589 break;
591 case OPT_imacros:
592 case OPT_include:
593 defer_opt (code, arg);
594 break;
596 case OPT_imultilib:
597 imultilib = arg;
598 break;
600 case OPT_iprefix:
601 iprefix = arg;
602 break;
604 case OPT_iquote:
605 add_path (xstrdup (arg), INC_QUOTE, 0, true);
606 break;
608 case OPT_isysroot:
609 sysroot = arg;
610 break;
612 case OPT_isystem:
613 add_path (xstrdup (arg), INC_SYSTEM, 0, true);
614 break;
616 case OPT_iwithprefix:
617 add_prefixed_path (arg, INC_SYSTEM);
618 break;
620 case OPT_iwithprefixbefore:
621 add_prefixed_path (arg, INC_BRACKET);
622 break;
624 case OPT_lang_asm:
625 cpp_set_lang (parse_in, CLK_ASM);
626 cpp_opts->dollars_in_ident = false;
627 break;
629 case OPT_nostdinc:
630 std_inc = false;
631 break;
633 case OPT_nostdinc__:
634 std_cxx_inc = false;
635 break;
637 case OPT_o:
638 if (!out_fname)
639 out_fname = arg;
640 else
641 error ("output filename specified twice");
642 break;
644 case OPT_print_objc_runtime_info:
645 print_struct_values = 1;
646 break;
648 case OPT_remap:
649 cpp_opts->remap = 1;
650 break;
652 case OPT_std_c__98:
653 case OPT_std_gnu__98:
654 if (!preprocessing_asm_p)
655 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
656 break;
658 case OPT_std_c__11:
659 case OPT_std_gnu__11:
660 if (!preprocessing_asm_p)
661 set_std_cxx11 (code == OPT_std_c__11 /* ISO */);
662 break;
664 case OPT_std_c__14:
665 case OPT_std_gnu__14:
666 if (!preprocessing_asm_p)
667 set_std_cxx14 (code == OPT_std_c__14 /* ISO */);
668 break;
670 case OPT_std_c__17:
671 case OPT_std_gnu__17:
672 if (!preprocessing_asm_p)
673 set_std_cxx17 (code == OPT_std_c__17 /* ISO */);
674 break;
676 case OPT_std_c__20:
677 case OPT_std_gnu__20:
678 if (!preprocessing_asm_p)
679 set_std_cxx20 (code == OPT_std_c__20 /* ISO */);
680 break;
682 case OPT_std_c__23:
683 case OPT_std_gnu__23:
684 if (!preprocessing_asm_p)
685 set_std_cxx23 (code == OPT_std_c__23 /* ISO */);
686 break;
688 case OPT_std_c__26:
689 case OPT_std_gnu__26:
690 if (!preprocessing_asm_p)
691 set_std_cxx26 (code == OPT_std_c__26 /* ISO */);
692 break;
694 case OPT_std_c90:
695 case OPT_std_iso9899_199409:
696 if (!preprocessing_asm_p)
697 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
698 break;
700 case OPT_std_gnu90:
701 if (!preprocessing_asm_p)
702 set_std_c89 (false /* c94 */, false /* ISO */);
703 break;
705 case OPT_std_c99:
706 if (!preprocessing_asm_p)
707 set_std_c99 (true /* ISO */);
708 break;
710 case OPT_std_gnu99:
711 if (!preprocessing_asm_p)
712 set_std_c99 (false /* ISO */);
713 break;
715 case OPT_std_c11:
716 if (!preprocessing_asm_p)
717 set_std_c11 (true /* ISO */);
718 break;
720 case OPT_std_gnu11:
721 if (!preprocessing_asm_p)
722 set_std_c11 (false /* ISO */);
723 break;
725 case OPT_std_c17:
726 if (!preprocessing_asm_p)
727 set_std_c17 (true /* ISO */);
728 break;
730 case OPT_std_gnu17:
731 if (!preprocessing_asm_p)
732 set_std_c17 (false /* ISO */);
733 break;
735 case OPT_std_c23:
736 if (!preprocessing_asm_p)
737 set_std_c23 (true /* ISO */);
738 break;
740 case OPT_std_gnu23:
741 if (!preprocessing_asm_p)
742 set_std_c23 (false /* ISO */);
743 break;
745 case OPT_trigraphs:
746 cpp_opts->trigraphs = 1;
747 break;
749 case OPT_traditional_cpp:
750 cpp_opts->traditional = 1;
751 break;
753 case OPT_v:
754 verbose = true;
755 break;
758 switch (c_language)
760 case clk_c:
761 C_handle_option_auto (&global_options, &global_options_set,
762 scode, arg, value,
763 c_family_lang_mask, kind,
764 loc, handlers, global_dc);
765 break;
767 case clk_objc:
768 ObjC_handle_option_auto (&global_options, &global_options_set,
769 scode, arg, value,
770 c_family_lang_mask, kind,
771 loc, handlers, global_dc);
772 break;
774 case clk_cxx:
775 CXX_handle_option_auto (&global_options, &global_options_set,
776 scode, arg, value,
777 c_family_lang_mask, kind,
778 loc, handlers, global_dc);
779 break;
781 case clk_objcxx:
782 ObjCXX_handle_option_auto (&global_options, &global_options_set,
783 scode, arg, value,
784 c_family_lang_mask, kind,
785 loc, handlers, global_dc);
786 break;
788 default:
789 gcc_unreachable ();
792 cpp_handle_option_auto (&global_options, scode, cpp_opts);
793 return result;
796 /* Default implementation of TARGET_HANDLE_C_OPTION. */
798 bool
799 default_handle_c_option (size_t code ATTRIBUTE_UNUSED,
800 const char *arg ATTRIBUTE_UNUSED,
801 int value ATTRIBUTE_UNUSED)
803 return false;
806 /* Post-switch processing. */
807 bool
808 c_common_post_options (const char **pfilename)
810 /* Canonicalize the input and output filenames. */
811 if (in_fnames == NULL)
813 in_fnames = XNEWVEC (const char *, 1);
814 in_fnames[0] = "";
816 else if (strcmp (in_fnames[0], "-") == 0)
818 if (pch_file)
819 error ("cannot use %<-%> as input filename for a precompiled header");
821 in_fnames[0] = "";
824 if (out_fname == NULL || !strcmp (out_fname, "-"))
825 out_fname = "";
827 if (cpp_opts->deps.style == DEPS_NONE)
828 check_deps_environment_vars ();
830 handle_deferred_opts ();
832 sanitize_cpp_opts ();
834 register_include_chains (parse_in, sysroot, iprefix, imultilib,
835 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
837 #ifdef C_COMMON_OVERRIDE_OPTIONS
838 /* Some machines may reject certain combinations of C
839 language-specific options. */
840 C_COMMON_OVERRIDE_OPTIONS;
841 #endif
843 if (flag_excess_precision == EXCESS_PRECISION_DEFAULT)
844 flag_excess_precision = (flag_iso ? EXCESS_PRECISION_STANDARD
845 : EXCESS_PRECISION_FAST);
847 /* ISO C restricts floating-point expression contraction to within
848 source-language expressions (-ffp-contract=on, currently an alias
849 for -ffp-contract=off). */
850 if (flag_iso
851 && !c_dialect_cxx ()
852 && (OPTION_SET_P (flag_fp_contract_mode)
853 == (enum fp_contract_mode) 0)
854 && flag_unsafe_math_optimizations == 0)
855 flag_fp_contract_mode = FP_CONTRACT_OFF;
857 /* C language modes before C99 enable -fpermissive by default, but
858 only if -pedantic-errors is not specified. Also treat
859 -fno-permissive as a subset of -pedantic-errors that does not
860 reject certain GNU extensions also present the defaults for later
861 language modes. */
862 if (!c_dialect_cxx ()
863 && !flag_isoc99
864 && !global_dc->m_pedantic_errors
865 && !OPTION_SET_P (flag_permissive))
867 flag_permissive = 1;
868 global_dc->m_permissive = 1;
871 /* If we are compiling C, and we are outside of a standards mode,
872 we can permit the new values from ISO/IEC TS 18661-3 for
873 FLT_EVAL_METHOD. Otherwise, we must restrict the possible values to
874 the set specified in ISO C99/C11. */
875 if (!flag_iso
876 && !c_dialect_cxx ()
877 && (OPTION_SET_P (flag_permitted_flt_eval_methods)
878 == PERMITTED_FLT_EVAL_METHODS_DEFAULT))
879 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_TS_18661;
880 else
881 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_C11;
883 /* C23 Annex F does not permit certain built-in functions to raise
884 "inexact". */
885 if (flag_isoc23)
886 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
887 flag_fp_int_builtin_inexact, 0);
889 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
890 inline semantics are not supported in GNU89 or C89 mode. */
891 if (flag_gnu89_inline == -1)
892 flag_gnu89_inline = !flag_isoc99;
893 else if (!flag_gnu89_inline && !flag_isoc99)
894 error ("%<-fno-gnu89-inline%> is only supported in GNU99 or C99 mode");
896 /* Default to ObjC sjlj exception handling if NeXT runtime < v2. */
897 if (flag_objc_sjlj_exceptions < 0)
898 flag_objc_sjlj_exceptions = (flag_next_runtime && flag_objc_abi < 2);
899 if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
900 flag_exceptions = 1;
902 /* If -ffreestanding, -fno-hosted or -fno-builtin then disable
903 pattern recognition. */
904 if (flag_no_builtin)
905 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
906 flag_tree_loop_distribute_patterns, 0);
908 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
909 It is never enabled in C++, as the minimum limit is not normative
910 in that standard. */
911 if (c_dialect_cxx ())
912 warn_overlength_strings = 0;
914 /* Wmain is enabled by default in C++ but not in C. */
915 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
916 even if -Wall or -Wpedantic was given (warn_main will be 2 if set
917 by -Wall, 1 if set by -Wmain). */
918 if (warn_main == -1)
919 warn_main = (c_dialect_cxx () && flag_hosted) ? 1 : 0;
920 else if (warn_main == 2)
921 warn_main = flag_hosted ? 1 : 0;
923 /* In C, -Wall and -Wc++-compat enable -Wenum-compare; if it has not
924 yet been set, it is disabled by default. In C++, it is enabled
925 by default. */
926 if (warn_enum_compare == -1)
927 warn_enum_compare = c_dialect_cxx () ? 1 : 0;
929 /* -Wpacked-bitfield-compat is on by default for the C languages. The
930 warning is issued in stor-layout.cc which is not part of the front-end so
931 we need to selectively turn it on here. */
932 if (warn_packed_bitfield_compat == -1)
933 warn_packed_bitfield_compat = 1;
935 /* Special format checking options don't work without -Wformat; warn if
936 they are used. */
937 if (!warn_format)
939 warning (OPT_Wformat_y2k,
940 "%<-Wformat-y2k%> ignored without %<-Wformat%>");
941 warning (OPT_Wformat_extra_args,
942 "%<-Wformat-extra-args%> ignored without %<-Wformat%>");
943 warning (OPT_Wformat_zero_length,
944 "%<-Wformat-zero-length%> ignored without %<-Wformat%>");
945 warning (OPT_Wformat_nonliteral,
946 "%<-Wformat-nonliteral%> ignored without %<-Wformat%>");
947 warning (OPT_Wformat_contains_nul,
948 "%<-Wformat-contains-nul%> ignored without %<-Wformat%>");
949 warning (OPT_Wformat_security,
950 "%<-Wformat-security%> ignored without %<-Wformat%>");
953 /* -Wimplicit-function-declaration is enabled by default for C99. */
954 if (warn_implicit_function_declaration == -1)
955 warn_implicit_function_declaration = flag_isoc99;
957 /* -Wimplicit-int is enabled by default for C99. */
958 if (warn_implicit_int == -1)
959 warn_implicit_int = flag_isoc99;
961 /* -Wold-style-definition is enabled by default for C23. */
962 if (warn_old_style_definition == -1)
963 warn_old_style_definition = flag_isoc23;
965 /* -Wshift-overflow is enabled by default in C99 and C++11 modes. */
966 if (warn_shift_overflow == -1)
967 warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99;
969 /* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 to C++17
970 modes. */
971 if (warn_shift_negative_value == -1)
972 warn_shift_negative_value = (extra_warnings
973 && (cxx_dialect >= cxx11 || flag_isoc99)
974 && cxx_dialect < cxx20);
976 /* -Wregister is enabled by default in C++17. */
977 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_register,
978 cxx_dialect >= cxx17);
980 /* -Wcomma-subscript is enabled by default in C++20. */
981 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
982 warn_comma_subscript,
983 cxx_dialect >= cxx23
984 || (cxx_dialect == cxx20 && warn_deprecated));
986 /* -Wvolatile is enabled by default in C++20. */
987 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_volatile,
988 cxx_dialect >= cxx20 && warn_deprecated);
990 /* -Wdeprecated-enum-enum-conversion is enabled by default in C++20. */
991 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
992 warn_deprecated_enum_enum_conv,
993 cxx_dialect >= cxx20 && warn_deprecated);
995 /* -Wdeprecated-enum-float-conversion is enabled by default in C++20. */
996 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
997 warn_deprecated_enum_float_conv,
998 cxx_dialect >= cxx20 && warn_deprecated);
1000 /* Declone C++ 'structors if -Os. */
1001 if (flag_declone_ctor_dtor == -1)
1002 flag_declone_ctor_dtor = optimize_size;
1004 if (flag_abi_compat_version == 1)
1006 warning (0, "%<-fabi-compat-version=1%> is not supported, using =2");
1007 flag_abi_compat_version = 2;
1010 /* Change flag_abi_version to be the actual current ABI level, for the
1011 benefit of c_cpp_builtins, and to make comparison simpler. */
1012 const int latest_abi_version = 19;
1013 /* Generate compatibility aliases for ABI v13 (8.2) by default. */
1014 const int abi_compat_default = 13;
1016 #define clamp(X) if (X == 0 || X > latest_abi_version) X = latest_abi_version
1017 clamp (flag_abi_version);
1018 clamp (warn_abi_version);
1019 clamp (flag_abi_compat_version);
1020 #undef clamp
1022 /* Default -Wabi= or -fabi-compat-version= from each other. */
1023 if (warn_abi_version == -1 && flag_abi_compat_version != -1)
1024 warn_abi_version = flag_abi_compat_version;
1025 else if (flag_abi_compat_version == -1 && warn_abi_version != -1)
1026 flag_abi_compat_version = warn_abi_version;
1027 else if (warn_abi_version == -1 && flag_abi_compat_version == -1)
1029 warn_abi_version = latest_abi_version;
1030 if (flag_abi_version == latest_abi_version)
1032 auto_diagnostic_group d;
1033 if (warning (OPT_Wabi, "%<-Wabi%> won%'t warn about anything"))
1035 inform (input_location, "%<-Wabi%> warns about differences "
1036 "from the most up-to-date ABI, which is also used "
1037 "by default");
1038 inform (input_location, "use e.g. %<-Wabi=11%> to warn about "
1039 "changes from GCC 7");
1041 flag_abi_compat_version = abi_compat_default;
1043 else
1044 flag_abi_compat_version = latest_abi_version;
1047 /* By default, enable the new inheriting constructor semantics along with ABI
1048 11. New and old should coexist fine, but it is a change in what
1049 artificial symbols are generated. */
1050 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1051 flag_new_inheriting_ctors,
1052 abi_version_at_least (11));
1054 /* For GCC 7, only enable DR150 resolution by default if -std=c++17. */
1055 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_new_ttp,
1056 cxx_dialect >= cxx17);
1058 /* C++11 guarantees forward progress. */
1059 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_finite_loops,
1060 optimize >= 2 && cxx_dialect >= cxx11);
1062 /* It's OK to discard calls to pure/const functions that might throw. */
1063 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1064 flag_delete_dead_exceptions, true);
1066 if (cxx_dialect >= cxx11)
1068 /* If we're allowing C++0x constructs, don't warn about C++98
1069 identifiers which are keywords in C++0x. */
1070 warn_cxx11_compat = 0;
1071 cpp_opts->cpp_warn_cxx11_compat = 0;
1073 if (warn_narrowing == -1)
1074 warn_narrowing = 1;
1076 /* Unless -f{,no-}ext-numeric-literals has been used explicitly,
1077 for -std=c++{11,14,17,20,23,26} default to
1078 -fno-ext-numeric-literals. */
1079 if (flag_iso && !OPTION_SET_P (flag_ext_numeric_literals))
1080 cpp_opts->ext_numeric_literals = 0;
1082 else if (warn_narrowing == -1)
1083 warn_narrowing = 0;
1085 if (cxx_dialect >= cxx20)
1087 /* Don't warn about C++20 compatibility changes in C++20 or later. */
1088 warn_cxx20_compat = 0;
1089 cpp_opts->cpp_warn_cxx20_compat = 0;
1092 /* C++17 has stricter evaluation order requirements; let's use some of them
1093 for earlier C++ as well, so chaining works as expected. */
1094 if (c_dialect_cxx ()
1095 && flag_strong_eval_order == -1)
1096 flag_strong_eval_order = (cxx_dialect >= cxx17 ? 2 : 1);
1098 if (flag_implicit_constexpr && cxx_dialect < cxx14)
1099 flag_implicit_constexpr = false;
1101 /* Global sized deallocation is new in C++14. */
1102 if (flag_sized_deallocation == -1)
1103 flag_sized_deallocation = (cxx_dialect >= cxx14);
1105 /* Pedwarn about invalid constexpr functions before C++23. */
1106 if (warn_invalid_constexpr == -1)
1107 warn_invalid_constexpr = (cxx_dialect < cxx23);
1109 /* char8_t support is implicitly enabled in C++20 and C23. */
1110 if (flag_char8_t == -1)
1111 flag_char8_t = (cxx_dialect >= cxx20) || flag_isoc23;
1112 cpp_opts->unsigned_utf8char = flag_char8_t ? 1 : cpp_opts->unsigned_char;
1114 if (flag_extern_tls_init)
1116 if (!TARGET_SUPPORTS_ALIASES || !SUPPORTS_WEAK)
1118 /* Lazy TLS initialization for a variable in another TU requires
1119 alias and weak reference support. */
1120 if (flag_extern_tls_init > 0)
1121 sorry ("external TLS initialization functions not supported "
1122 "on this target");
1124 flag_extern_tls_init = 0;
1126 else
1127 flag_extern_tls_init = 1;
1130 /* Enable by default only for C++ and C++ with ObjC extensions. */
1131 if (warn_return_type == -1 && c_dialect_cxx ())
1132 warn_return_type = 1;
1134 /* C++20 is the final version of concepts. We still use -fconcepts
1135 to know when concepts are enabled. Note that -fconcepts-ts can
1136 be used to include additional features, although modified to
1137 work with the standard. */
1138 if (cxx_dialect >= cxx20 || flag_concepts_ts)
1139 flag_concepts = 1;
1141 if (num_in_fnames > 1)
1142 error ("too many filenames given; type %<%s %s%> for usage",
1143 progname, "--help");
1145 if (flag_preprocess_only)
1147 /* Open the output now. We must do so even if flag_no_output is
1148 on, because there may be other output than from the actual
1149 preprocessing (e.g. from -dM). */
1150 if (out_fname[0] == '\0')
1151 out_stream = stdout;
1152 else
1153 out_stream = fopen (out_fname, "w");
1155 if (out_stream == NULL)
1156 fatal_error (input_location, "opening output file %s: %m", out_fname);
1158 init_pp_output (out_stream);
1160 else
1162 init_c_lex ();
1164 /* When writing a PCH file, avoid reading some other PCH file,
1165 because the default address space slot then can't be used
1166 for the output PCH file. */
1167 if (pch_file)
1169 c_common_no_more_pch ();
1170 /* Only -g0 and -gdwarf* are supported with PCH, for other
1171 debug formats we warn here and refuse to load any PCH files. */
1172 if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1173 warning (OPT_Wdeprecated,
1174 "the %qs debug info cannot be used with "
1175 "pre-compiled headers",
1176 debug_set_names (write_symbols & ~DWARF2_DEBUG));
1177 /* Let libcpp know that the main file is a header so it won't
1178 complain about things like #include_next and #pragma once. */
1179 cpp_opts->main_search = CMS_header;
1181 else if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1182 c_common_no_more_pch ();
1184 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1185 input_location = UNKNOWN_LOCATION;
1188 struct cpp_callbacks *cb = cpp_get_callbacks (parse_in);
1189 cb->file_change = cb_file_change;
1190 cb->dir_change = cb_dir_change;
1191 if (lang_hooks.preprocess_options)
1192 lang_hooks.preprocess_options (parse_in);
1193 cpp_post_options (parse_in);
1194 init_global_opts_from_cpp (&global_options, cpp_get_options (parse_in));
1195 /* For C++23 and explicit -finput-charset=UTF-8, turn on -Winvalid-utf8
1196 by default and make it a pedwarn unless -Wno-invalid-utf8. */
1197 if (cxx_dialect >= cxx23
1198 && cpp_opts->cpp_input_charset_explicit
1199 && strcmp (cpp_opts->input_charset, "UTF-8") == 0
1200 && (cpp_opts->cpp_warn_invalid_utf8
1201 || !global_options_set.x_warn_invalid_utf8))
1203 global_options.x_warn_invalid_utf8 = 1;
1204 cpp_opts->cpp_warn_invalid_utf8 = cpp_opts->cpp_pedantic ? 2 : 1;
1207 /* Let diagnostics infrastructure know how to convert input files the same
1208 way libcpp will do it, namely using the configured input charset and
1209 skipping a UTF-8 BOM if present. */
1210 diagnostic_initialize_input_context (global_dc,
1211 c_common_input_charset_cb, true);
1212 input_location = UNKNOWN_LOCATION;
1214 *pfilename = this_input_filename
1215 = cpp_read_main_file (parse_in, in_fnames[0],
1216 /* We'll inject preamble pieces if this is
1217 not preprocessed. */
1218 !cpp_opts->preprocessed);
1220 /* Don't do any compilation or preprocessing if there is no input file. */
1221 if (this_input_filename == NULL)
1223 errorcount++;
1224 return false;
1227 if (flag_working_directory
1228 && flag_preprocess_only && !flag_no_line_commands)
1229 pp_dir_change (parse_in, get_src_pwd ());
1231 /* Disable LTO output when outputting a precompiled header. */
1232 if (pch_file && flag_lto)
1234 flag_lto = 0;
1235 flag_generate_lto = 0;
1238 return flag_preprocess_only;
1241 /* Front end initialization common to C, ObjC and C++. */
1242 bool
1243 c_common_init (void)
1245 /* Set up preprocessor arithmetic. Must be done after call to
1246 c_common_nodes_and_builtins for type nodes to be good. */
1247 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1248 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1249 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1250 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1251 cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node);
1252 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1254 /* This can't happen until after wchar_precision and bytes_big_endian
1255 are known. */
1256 cpp_init_iconv (parse_in);
1258 if (version_flag)
1260 int i;
1261 fputs ("Compiler executable checksum: ", stderr);
1262 for (i = 0; i < 16; i++)
1263 fprintf (stderr, "%02x", executable_checksum[i]);
1264 putc ('\n', stderr);
1267 /* Has to wait until now so that cpplib has its hash table. */
1268 init_pragma ();
1270 if (flag_preprocess_only)
1272 c_finish_options ();
1273 c_init_preprocess ();
1274 preprocess_file (parse_in);
1275 return false;
1278 return true;
1281 /* Initialize the integrated preprocessor after debug output has been
1282 initialized; loop over each input file. */
1283 void
1284 c_common_parse_file (void)
1286 auto dumps = g->get_dumps ();
1287 for (unsigned int i = 0;;)
1289 c_finish_options ();
1290 /* Open the dump file to use for the original dump output
1291 here, to be used during parsing for the current file. */
1292 dumps->dump_start (TDI_original, &dump_flags);
1293 pch_init ();
1294 push_file_scope ();
1295 c_parse_file ();
1296 pop_file_scope ();
1297 /* And end the main input file, if the debug writer wants it */
1298 if (debug_hooks->start_end_main_source_file)
1299 (*debug_hooks->end_source_file) (0);
1300 if (++i >= num_in_fnames)
1301 break;
1302 cpp_undef_all (parse_in);
1303 cpp_clear_file_cache (parse_in);
1304 this_input_filename
1305 = cpp_read_main_file (parse_in, in_fnames[i]);
1306 /* If an input file is missing, abandon further compilation.
1307 cpplib has issued a diagnostic. */
1308 if (!this_input_filename)
1309 break;
1310 dumps->dump_finish (TDI_original);
1313 c_parse_final_cleanups ();
1314 dumps->dump_finish (TDI_original);
1317 /* Common finish hook for the C, ObjC and C++ front ends. */
1318 void
1319 c_common_finish (void)
1321 FILE *deps_stream = NULL;
1322 FILE *fdeps_stream = NULL;
1324 /* Note that we write the dependencies even if there are errors. This is
1325 useful for handling outdated generated headers that now trigger errors
1326 (for example, with #error) which would be resolved by re-generating
1327 them. In a sense, this complements -MG. */
1328 if (cpp_opts->deps.style != DEPS_NONE)
1330 /* If -M or -MM was seen without -MF, default output to the
1331 output stream. */
1332 if (!deps_file)
1333 deps_stream = out_stream;
1334 else if (deps_file[0] == '-' && deps_file[1] == '\0')
1335 deps_stream = stdout;
1336 else
1338 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1339 if (!deps_stream)
1340 fatal_error (input_location, "opening dependency file %s: %m",
1341 deps_file);
1345 /* When we call cpp_finish (), it may generate some diagnostics using
1346 locations it remembered from the preprocessing phase, e.g. for
1347 -Wunused-macros. So inform c_cpp_diagnostic () not to override those
1348 locations with input_location, which would be incorrect now. */
1349 override_libcpp_locations = false;
1351 if (cpp_opts->deps.fdeps_format != FDEPS_FMT_NONE)
1353 if (!fdeps_file)
1354 fdeps_stream = out_stream;
1355 else if (fdeps_file[0] == '-' && fdeps_file[1] == '\0')
1356 fdeps_stream = stdout;
1357 else
1359 fdeps_stream = fopen (fdeps_file, "w");
1360 if (!fdeps_stream)
1361 fatal_error (input_location, "opening dependency file %s: %m",
1362 fdeps_file);
1364 if (fdeps_stream == deps_stream && fdeps_stream != stdout)
1365 fatal_error (input_location, "%<-MF%> and %<-fdeps-file=%> cannot share an output file %s: %m",
1366 fdeps_file);
1369 /* For performance, avoid tearing down cpplib's internal structures
1370 with cpp_destroy (). */
1371 cpp_finish (parse_in, deps_stream, fdeps_stream);
1373 if (deps_stream && deps_stream != out_stream && deps_stream != stdout
1374 && (ferror (deps_stream) || fclose (deps_stream)))
1375 fatal_error (input_location, "closing dependency file %s: %m", deps_file);
1377 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1378 fatal_error (input_location, "when writing output to %s: %m", out_fname);
1381 /* Either of two environment variables can specify output of
1382 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1383 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1384 and DEPS_TARGET is the target to mention in the deps. They also
1385 result in dependency information being appended to the output file
1386 rather than overwriting it, and like Sun's compiler
1387 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1388 static void
1389 check_deps_environment_vars (void)
1391 char *spec;
1393 spec = getenv ("DEPENDENCIES_OUTPUT");
1394 if (spec)
1395 cpp_opts->deps.style = DEPS_USER;
1396 else
1398 spec = getenv ("SUNPRO_DEPENDENCIES");
1399 if (spec)
1401 cpp_opts->deps.style = DEPS_SYSTEM;
1402 cpp_opts->deps.ignore_main_file = true;
1406 if (spec)
1408 /* Find the space before the DEPS_TARGET, if there is one. */
1409 char *s = strchr (spec, ' ');
1410 if (s)
1412 /* Let the caller perform MAKE quoting. */
1413 defer_opt (OPT_MT, s + 1);
1414 *s = '\0';
1417 /* Command line -MF overrides environment variables and default. */
1418 if (!deps_file)
1419 deps_file = spec;
1421 deps_append = 1;
1422 deps_seen = true;
1426 /* Handle deferred command line switches. */
1427 static void
1428 handle_deferred_opts (void)
1430 /* Avoid allocating the deps buffer if we don't need it.
1431 (This flag may be true without there having been -MT or -MQ
1432 options, but we'll still need the deps buffer.) */
1433 if (!deps_seen)
1434 return;
1436 if (mkdeps *deps = cpp_get_deps (parse_in))
1437 for (unsigned i = 0; i < deferred_count; i++)
1439 struct deferred_opt *opt = &deferred_opts[i];
1441 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1442 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
1443 else if (opt->code == OPT_fdeps_target_)
1444 fdeps_add_target (deps, opt->arg, true);
1448 /* These settings are appropriate for GCC, but not necessarily so for
1449 cpplib as a library. */
1450 static void
1451 sanitize_cpp_opts (void)
1453 /* If we don't know what style of dependencies to output, complain
1454 if any other dependency switches have been given. */
1455 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1456 error ("to generate dependencies you must specify either %<-M%> "
1457 "or %<-MM%>");
1459 /* -dM and dependencies suppress normal output; do it here so that
1460 the last -d[MDN] switch overrides earlier ones. */
1461 if (flag_dump_macros == 'M')
1462 flag_no_output = 1;
1464 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1465 to perform proper macro expansion. */
1466 if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
1467 flag_dump_macros = 'D';
1469 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1470 -dM since at least glibc relies on -M -dM to work. */
1471 /* Also, flag_no_output implies flag_no_line_commands, always. */
1472 if (flag_no_output)
1474 if (flag_dump_macros != 'M')
1475 flag_dump_macros = 0;
1476 flag_dump_includes = 0;
1477 flag_no_line_commands = 1;
1479 else if (cpp_opts->deps.missing_files)
1480 error ("%<-MG%> may only be used with %<-M%> or %<-MM%>");
1482 cpp_opts->unsigned_char = !flag_signed_char;
1483 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1485 /* Wlong-long is disabled by default. It is enabled by:
1486 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1487 [-Wpedantic | -Wtraditional] -std=non-c99
1489 Either -Wlong-long or -Wno-long-long override any other settings.
1490 ??? These conditions should be handled in c.opt. */
1491 if (warn_long_long == -1)
1493 warn_long_long = ((pedantic || warn_traditional)
1494 && (c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99));
1495 cpp_opts->cpp_warn_long_long = warn_long_long;
1498 /* If we're generating preprocessor output, emit current directory
1499 if explicitly requested or if debugging information is enabled.
1500 ??? Maybe we should only do it for debugging formats that
1501 actually output the current directory? */
1502 if (flag_working_directory == -1)
1503 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1505 if (warn_implicit_fallthrough < 5)
1506 cpp_opts->cpp_warn_implicit_fallthrough = warn_implicit_fallthrough;
1507 else
1508 cpp_opts->cpp_warn_implicit_fallthrough = 0;
1510 if (cpp_opts->directives_only)
1512 if (cpp_warn_unused_macros)
1513 error ("%<-fdirectives-only%> is incompatible "
1514 "with %<-Wunused-macros%>");
1515 if (cpp_opts->traditional)
1516 error ("%<-fdirectives-only%> is incompatible with %<-traditional%>");
1520 /* Add include path with a prefix at the front of its name. */
1521 static void
1522 add_prefixed_path (const char *suffix, incpath_kind chain)
1524 char *path;
1525 const char *prefix;
1526 size_t prefix_len, suffix_len;
1528 suffix_len = strlen (suffix);
1529 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1530 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1532 path = (char *) xmalloc (prefix_len + suffix_len + 1);
1533 memcpy (path, prefix, prefix_len);
1534 memcpy (path + prefix_len, suffix, suffix_len);
1535 path[prefix_len + suffix_len] = '\0';
1537 add_path (path, chain, 0, false);
1540 /* Handle -D, -U, -A, -imacros, and the first -include. */
1541 static void
1542 c_finish_options (void)
1544 if (!cpp_opts->preprocessed)
1546 const line_map_ordinary *bltin_map
1547 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1548 special_fname_builtin (), 0));
1549 cb_file_change (parse_in, bltin_map);
1550 linemap_line_start (line_table, 0, 1);
1552 /* Make sure all of the builtins about to be declared have
1553 BUILTINS_LOCATION has their location_t. */
1554 cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
1556 cpp_init_builtins (parse_in, flag_hosted);
1557 c_cpp_builtins (parse_in);
1559 /* We're about to send user input to cpplib, so make it warn for
1560 things that we previously (when we sent it internal definitions)
1561 told it to not warn.
1563 C99 permits implementation-defined characters in identifiers.
1564 The documented meaning of -std= is to turn off extensions that
1565 conflict with the specified standard, and since a strictly
1566 conforming program cannot contain a '$', we do not condition
1567 their acceptance on the -std= setting. */
1568 cpp_opts->warn_dollars = (cpp_opts->cpp_pedantic && !cpp_opts->c99);
1570 const line_map_ordinary *cmd_map
1571 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1572 _("<command-line>"), 0));
1573 cb_file_change (parse_in, cmd_map);
1574 linemap_line_start (line_table, 0, 1);
1576 /* All command line defines must have the same location. */
1577 cpp_force_token_locations (parse_in, line_table->highest_line);
1578 for (size_t i = 0; i < deferred_count; i++)
1580 struct deferred_opt *opt = &deferred_opts[i];
1582 if (opt->code == OPT_D)
1583 cpp_define (parse_in, opt->arg);
1584 else if (opt->code == OPT_U)
1585 cpp_undef (parse_in, opt->arg);
1586 else if (opt->code == OPT_A)
1588 if (opt->arg[0] == '-')
1589 cpp_unassert (parse_in, opt->arg + 1);
1590 else
1591 cpp_assert (parse_in, opt->arg);
1595 cpp_stop_forcing_token_locations (parse_in);
1597 else if (cpp_opts->directives_only)
1598 cpp_init_special_builtins (parse_in);
1600 /* Start the main input file, if the debug writer wants it. */
1601 if (debug_hooks->start_end_main_source_file
1602 && !flag_preprocess_only)
1603 (*debug_hooks->start_source_file) (0, this_input_filename);
1605 if (!cpp_opts->preprocessed)
1606 /* Handle -imacros after -D and -U. */
1607 for (size_t i = 0; i < deferred_count; i++)
1609 struct deferred_opt *opt = &deferred_opts[i];
1611 if (opt->code == OPT_imacros
1612 && cpp_push_include (parse_in, opt->arg))
1614 /* Disable push_command_line_include callback for now. */
1615 include_cursor = deferred_count + 1;
1616 cpp_scan_nooutput (parse_in);
1620 include_cursor = 0;
1621 push_command_line_include ();
1624 /* Give CPP the next file given by -include, if any. */
1625 static void
1626 push_command_line_include (void)
1628 /* This can happen if disabled by -imacros for example.
1629 Punt so that we don't set "<command-line>" as the filename for
1630 the header. */
1631 if (include_cursor > deferred_count)
1632 return;
1634 if (!done_preinclude)
1636 done_preinclude = true;
1637 if (flag_hosted && std_inc && !cpp_opts->preprocessed)
1639 const char *preinc = targetcm.c_preinclude ();
1640 if (preinc && cpp_push_default_include (parse_in, preinc))
1641 return;
1645 pch_cpp_save_state ();
1647 while (include_cursor < deferred_count)
1649 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1651 if (!cpp_opts->preprocessed && opt->code == OPT_include
1652 && cpp_push_include (parse_in, opt->arg))
1653 return;
1656 if (include_cursor == deferred_count)
1658 include_cursor++;
1659 /* -Wunused-macros should only warn about macros defined hereafter. */
1660 cpp_opts->warn_unused_macros = cpp_warn_unused_macros;
1661 /* Restore the line map back to the main file. */
1662 if (!cpp_opts->preprocessed)
1664 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
1665 if (lang_hooks.preprocess_main_file)
1666 /* We're starting the main file. Inform the FE of that. */
1667 lang_hooks.preprocess_main_file
1668 (parse_in, line_table, LINEMAPS_LAST_ORDINARY_MAP (line_table));
1671 /* Set this here so the client can change the option if it wishes,
1672 and after stacking the main file so we don't trace the main file. */
1673 line_table->trace_includes = cpp_opts->print_include_names;
1677 /* File change callback. Has to handle -include files. */
1678 static void
1679 cb_file_change (cpp_reader *reader, const line_map_ordinary *new_map)
1681 if (flag_preprocess_only)
1682 pp_file_change (new_map);
1683 else
1684 fe_file_change (new_map);
1686 if (new_map && cpp_opts->preprocessed
1687 && lang_hooks.preprocess_main_file && MAIN_FILE_P (new_map)
1688 && ORDINARY_MAP_STARTING_LINE_NUMBER (new_map))
1689 /* We're starting the main file. Inform the FE of that. */
1690 lang_hooks.preprocess_main_file (reader, line_table, new_map);
1692 if (new_map
1693 && (new_map->reason == LC_ENTER || new_map->reason == LC_RENAME))
1695 /* Signal to plugins that a file is included. This could happen
1696 several times with the same file path, e.g. because of
1697 several '#include' or '#line' directives... */
1698 invoke_plugin_callbacks
1699 (PLUGIN_INCLUDE_FILE,
1700 const_cast<char*> (ORDINARY_MAP_FILE_NAME (new_map)));
1703 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1705 pch_cpp_save_state ();
1706 push_command_line_include ();
1710 void
1711 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
1713 if (!set_src_pwd (dir))
1714 warning (0, "too late for # directive to set debug directory");
1717 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1718 extensions if ISO). There is no concept of gnu94. */
1719 static void
1720 set_std_c89 (int c94, int iso)
1722 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1723 flag_iso = iso;
1724 flag_no_asm = iso;
1725 flag_no_gnu_keywords = iso;
1726 flag_no_nonansi_builtin = iso;
1727 flag_isoc94 = c94;
1728 flag_isoc99 = 0;
1729 flag_isoc11 = 0;
1730 flag_isoc23 = 0;
1731 lang_hooks.name = "GNU C89";
1734 /* Set the C 99 standard (without GNU extensions if ISO). */
1735 static void
1736 set_std_c99 (int iso)
1738 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1739 flag_no_asm = iso;
1740 flag_no_nonansi_builtin = iso;
1741 flag_iso = iso;
1742 flag_isoc23 = 0;
1743 flag_isoc11 = 0;
1744 flag_isoc99 = 1;
1745 flag_isoc94 = 1;
1746 lang_hooks.name = "GNU C99";
1749 /* Set the C 11 standard (without GNU extensions if ISO). */
1750 static void
1751 set_std_c11 (int iso)
1753 cpp_set_lang (parse_in, iso ? CLK_STDC11: CLK_GNUC11);
1754 flag_no_asm = iso;
1755 flag_no_nonansi_builtin = iso;
1756 flag_iso = iso;
1757 flag_isoc23 = 0;
1758 flag_isoc11 = 1;
1759 flag_isoc99 = 1;
1760 flag_isoc94 = 1;
1761 lang_hooks.name = "GNU C11";
1764 /* Set the C 17 standard (without GNU extensions if ISO). */
1765 static void
1766 set_std_c17 (int iso)
1768 cpp_set_lang (parse_in, iso ? CLK_STDC17: CLK_GNUC17);
1769 flag_no_asm = iso;
1770 flag_no_nonansi_builtin = iso;
1771 flag_iso = iso;
1772 flag_isoc23 = 0;
1773 flag_isoc11 = 1;
1774 flag_isoc99 = 1;
1775 flag_isoc94 = 1;
1776 lang_hooks.name = "GNU C17";
1779 /* Set the C 2X standard (without GNU extensions if ISO). */
1780 static void
1781 set_std_c23 (int iso)
1783 cpp_set_lang (parse_in, iso ? CLK_STDC23: CLK_GNUC23);
1784 flag_no_asm = iso;
1785 flag_no_nonansi_builtin = iso;
1786 flag_iso = iso;
1787 flag_isoc23 = 1;
1788 flag_isoc11 = 1;
1789 flag_isoc99 = 1;
1790 flag_isoc94 = 1;
1791 lang_hooks.name = "GNU C23";
1795 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1796 static void
1797 set_std_cxx98 (int iso)
1799 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1800 flag_no_gnu_keywords = iso;
1801 flag_no_nonansi_builtin = iso;
1802 flag_iso = iso;
1803 flag_isoc94 = 0;
1804 flag_isoc99 = 0;
1805 cxx_dialect = cxx98;
1806 lang_hooks.name = "GNU C++98";
1809 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1810 static void
1811 set_std_cxx11 (int iso)
1813 cpp_set_lang (parse_in, iso ? CLK_CXX11: CLK_GNUCXX11);
1814 flag_no_gnu_keywords = iso;
1815 flag_no_nonansi_builtin = iso;
1816 flag_iso = iso;
1817 /* C++11 includes the C99 standard library. */
1818 flag_isoc94 = 1;
1819 flag_isoc99 = 1;
1820 cxx_dialect = cxx11;
1821 lang_hooks.name = "GNU C++11";
1824 /* Set the C++ 2014 standard (without GNU extensions if ISO). */
1825 static void
1826 set_std_cxx14 (int iso)
1828 cpp_set_lang (parse_in, iso ? CLK_CXX14: CLK_GNUCXX14);
1829 flag_no_gnu_keywords = iso;
1830 flag_no_nonansi_builtin = iso;
1831 flag_iso = iso;
1832 /* C++14 includes the C99 standard library. */
1833 flag_isoc94 = 1;
1834 flag_isoc99 = 1;
1835 cxx_dialect = cxx14;
1836 lang_hooks.name = "GNU C++14";
1839 /* Set the C++ 2017 standard (without GNU extensions if ISO). */
1840 static void
1841 set_std_cxx17 (int iso)
1843 cpp_set_lang (parse_in, iso ? CLK_CXX17: CLK_GNUCXX17);
1844 flag_no_gnu_keywords = iso;
1845 flag_no_nonansi_builtin = iso;
1846 flag_iso = iso;
1847 /* C++17 includes the C11 standard library. */
1848 flag_isoc94 = 1;
1849 flag_isoc99 = 1;
1850 flag_isoc11 = 1;
1851 cxx_dialect = cxx17;
1852 lang_hooks.name = "GNU C++17";
1855 /* Set the C++ 2020 standard (without GNU extensions if ISO). */
1856 static void
1857 set_std_cxx20 (int iso)
1859 cpp_set_lang (parse_in, iso ? CLK_CXX20: CLK_GNUCXX20);
1860 flag_no_gnu_keywords = iso;
1861 flag_no_nonansi_builtin = iso;
1862 flag_iso = iso;
1863 /* C++20 includes the C11 standard library. */
1864 flag_isoc94 = 1;
1865 flag_isoc99 = 1;
1866 flag_isoc11 = 1;
1867 /* C++20 includes coroutines. */
1868 flag_coroutines = true;
1869 cxx_dialect = cxx20;
1870 lang_hooks.name = "GNU C++20";
1873 /* Set the C++ 2023 standard (without GNU extensions if ISO). */
1874 static void
1875 set_std_cxx23 (int iso)
1877 cpp_set_lang (parse_in, iso ? CLK_CXX23: CLK_GNUCXX23);
1878 flag_no_gnu_keywords = iso;
1879 flag_no_nonansi_builtin = iso;
1880 flag_iso = iso;
1881 /* C++23 includes the C11 standard library. */
1882 flag_isoc94 = 1;
1883 flag_isoc99 = 1;
1884 flag_isoc11 = 1;
1885 /* C++23 includes coroutines. */
1886 flag_coroutines = true;
1887 cxx_dialect = cxx23;
1888 lang_hooks.name = "GNU C++23";
1891 /* Set the C++ 2026 standard (without GNU extensions if ISO). */
1892 static void
1893 set_std_cxx26 (int iso)
1895 cpp_set_lang (parse_in, iso ? CLK_CXX26: CLK_GNUCXX26);
1896 flag_no_gnu_keywords = iso;
1897 flag_no_nonansi_builtin = iso;
1898 flag_iso = iso;
1899 /* C++26 includes the C11 standard library. */
1900 flag_isoc94 = 1;
1901 flag_isoc99 = 1;
1902 flag_isoc11 = 1;
1903 /* C++26 includes coroutines. */
1904 flag_coroutines = true;
1905 cxx_dialect = cxx26;
1906 lang_hooks.name = "GNU C++26";
1909 /* Args to -d specify what to dump. Silently ignore
1910 unrecognized options; they may be aimed at toplev.cc. */
1911 static void
1912 handle_OPT_d (const char *arg)
1914 char c;
1916 while ((c = *arg++) != '\0')
1917 switch (c)
1919 case 'M': /* Dump macros only. */
1920 case 'N': /* Dump names. */
1921 case 'D': /* Dump definitions. */
1922 case 'U': /* Dump used macros. */
1923 flag_dump_macros = c;
1924 break;
1926 case 'I':
1927 flag_dump_includes = 1;
1928 break;