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

source code of gcc/c-family/c-opts.cc