Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / cpp / gcc / c-family / c-opts.cc
blobc5e5287be918d8c1de449a228130a26b24bb3c73
1 /* C/ObjC/C++ command line option handling.
2 Copyright (C) 2002-2022 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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "c-target.h"
27 #include "c-common.h"
28 #include "memmodel.h"
29 // sdcpp #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() */
46 // #include "../libcpp/internal.h" // BUG
47 #define untested() { fprintf (stderr, "@@#\n@@@:%s:%d:%s\n", __FILE__, __LINE__, __func__); }
49 #ifndef DOLLARS_IN_IDENTIFIERS
50 # define DOLLARS_IN_IDENTIFIERS true
51 #endif
53 #ifndef TARGET_SYSTEM_ROOT
54 # define TARGET_SYSTEM_ROOT NULL
55 #endif
57 #ifndef TARGET_OPTF
58 #define TARGET_OPTF(ARG)
59 #endif
61 /* CPP's options. */
62 cpp_options *cpp_opts = nullptr;
64 /* Input filename. */
65 static const char *this_input_filename;
67 /* Filename and stream for preprocessed output. */
68 static const char *out_fname;
69 static FILE *out_stream;
71 /* Append dependencies to deps_file. */
72 static bool deps_append;
74 /* If dependency switches (-MF etc.) have been given. */
75 static bool deps_seen;
77 /* If -v seen. */
78 static bool verbose;
80 /* Dependency output file. */
81 static const char *deps_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 /* Dump files/flags to use during parsing. */
108 static FILE *original_dump_file = NULL;
109 static dump_flags_t original_dump_flags;
111 /* Whether any standard preincluded header has been preincluded. */
112 static bool done_preinclude;
114 static void handle_OPT_d (const char *);
115 static void set_std_cxx98 (int);
116 static void set_std_cxx11 (int);
117 static void set_std_cxx14 (int);
118 static void set_std_cxx17 (int);
119 static void set_std_cxx20 (int);
120 static void set_std_cxx23 (int);
121 static void set_std_c89 (int, int);
122 static void set_std_c99 (int);
123 static void set_std_c11 (int);
124 static void set_std_c17 (int);
125 static void set_std_c23 (int);
126 static void check_deps_environment_vars (void);
127 static void handle_deferred_opts (void);
128 static void sanitize_cpp_opts (void);
129 static void add_prefixed_path (const char *, incpath_kind);
130 static void push_command_line_include (void);
131 static void cb_file_change (cpp_reader *, const line_map_ordinary *);
132 static void cb_dir_change (cpp_reader *, const char *);
133 static void c_finish_options (void);
135 #ifndef STDC_0_IN_SYSTEM_HEADERS
136 #define STDC_0_IN_SYSTEM_HEADERS 0
137 #endif
139 /* Holds switches parsed by c_common_handle_option (), but whose
140 handling is deferred to c_common_post_options (). */
141 static void defer_opt (enum opt_code, const char *);
142 static struct deferred_opt
144 enum opt_code code;
145 const char *arg;
146 } *deferred_opts;
149 extern const unsigned int
150 c_family_lang_mask = (CL_C | CL_CXX | CL_ObjC | CL_ObjCXX);
152 /* Defer option CODE with argument ARG. */
153 static void
154 defer_opt (enum opt_code code, const char *arg)
156 deferred_opts[deferred_count].code = code;
157 deferred_opts[deferred_count].arg = arg;
158 deferred_count++;
161 /* Return language mask for option parsing. */
162 unsigned int
163 c_common_option_lang_mask (void)
165 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
167 return lang_flags[c_language];
170 /* Diagnostic finalizer for C/C++/Objective-C/Objective-C++. */
171 static void
172 c_diagnostic_finalizer (diagnostic_context *context,
173 diagnostic_info *diagnostic,
174 diagnostic_t)
176 char *saved_prefix = pp_take_prefix (context->printer);
177 pp_set_prefix (context->printer, NULL);
178 pp_newline (context->printer);
179 diagnostic_show_locus (context, diagnostic->richloc, diagnostic->kind);
180 /* By default print macro expansion contexts in the diagnostic
181 finalizer -- for tokens resulting from macro expansion. */
182 virt_loc_aware_diagnostic_finalizer (context, diagnostic);
183 pp_set_prefix (context->printer, saved_prefix);
184 pp_flush (context->printer);
187 /* Common default settings for diagnostics. */
188 void
189 c_common_diagnostics_set_defaults (diagnostic_context *context)
191 diagnostic_finalizer (context) = c_diagnostic_finalizer;
192 context->opt_permissive = OPT_fpermissive;
195 /* Input charset configuration for diagnostics. */
196 static const char *
197 c_common_input_charset_cb (const char * /*filename*/)
199 const char *cs = cpp_opts->input_charset;
200 return cpp_input_conversion_is_trivial (cs) ? nullptr : cs;
203 /* Whether options from all C-family languages should be accepted
204 quietly. */
205 static bool accept_all_c_family_options = false;
207 /* Return whether to complain about a wrong-language option. */
208 bool
209 c_common_complain_wrong_lang_p (const struct cl_option *option)
211 if (accept_all_c_family_options
212 && (option->flags & c_family_lang_mask))
213 return false;
215 return true;
218 /* Initialize options structure OPTS. */
219 void
220 c_common_init_options_struct (struct gcc_options *opts)
222 opts->x_flag_exceptions = c_dialect_cxx ();
223 opts->x_warn_pointer_arith = c_dialect_cxx ();
224 opts->x_warn_write_strings = c_dialect_cxx ();
225 opts->x_flag_warn_unused_result = true;
227 /* By default, C99-like requirements for complex multiply and divide. */
228 opts->x_flag_complex_method = 2;
229 opts->x_flag_default_complex_method = opts->x_flag_complex_method;
232 /* Common initialization before calling option handlers. */
233 void
234 c_common_init_options (unsigned int decoded_options_count,
235 struct cl_decoded_option *decoded_options)
237 unsigned int i;
238 struct cpp_callbacks *cb;
240 g_string_concat_db
241 = new (ggc_alloc <string_concat_db> ()) string_concat_db ();
243 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
244 ident_hash, line_table);
245 cb = cpp_get_callbacks (parse_in);
246 cb->diagnostic = c_cpp_diagnostic;
248 cpp_opts = cpp_get_options (parse_in);
249 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
250 cpp_opts->objc = c_dialect_objc ();
251 cpp_opts->deps.modules = true;
253 /* Reset to avoid warnings on internal definitions. We set it just
254 before passing on command-line options to cpplib. */
255 cpp_opts->warn_dollars = 0;
257 deferred_opts = XNEWVEC (struct deferred_opt, decoded_options_count);
259 if (c_language == clk_c)
261 /* The default for C is gnu17. */
262 set_std_c17 (false /* ISO */);
264 /* If preprocessing assembly language, accept any of the C-family
265 front end options since the driver may pass them through. */
266 for (i = 1; i < decoded_options_count; i++)
267 if (decoded_options[i].opt_index == OPT_lang_asm)
269 accept_all_c_family_options = true;
270 break;
274 /* Set C++ standard to C++17 if not specified on the command line. */
275 if (c_dialect_cxx ())
276 set_std_cxx17 (/*ISO*/false);
278 global_dc->colorize_source_p = true;
280 cpp_opts->allow_naked_hash = 0;
281 cpp_opts->preproc_asm = 1;
282 cpp_opts->pedantic_parse_number = 0;
283 cpp_opts->obj_ext = NULL;
286 #if 0 // from sdcpp.c
288 /* Kevin abuse for SDCC. */
289 cpp_register_pragma(parse_in, 0, "sdcc_hash", do_pragma_sdcc_hash, false);
290 /* SDCC _asm specific */
291 cpp_register_pragma(parse_in, 0, "preproc_asm", do_pragma_preproc_asm, false);
292 /* SDCC specific */
293 cpp_register_pragma(parse_in, 0, "pedantic_parse_number", do_pragma_pedantic_parse_number, false);
295 #endif
298 # if 0 // sdcpp init option, from sdcpp-opts.c
299 struct cpp_callbacks *cb;
301 parse_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
302 cb = cpp_get_callbacks (parse_in);
303 cb->error = c_cpp_error;
305 cpp_opts = cpp_get_options (parse_in);
306 cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
307 cpp_opts->objc = 0;
309 /* Reset to avoid warnings on internal definitions. We set it just
310 before passing on command-line options to cpplib. */
311 cpp_opts->warn_dollars = 0;
313 deferred_opts = XNEWVEC (struct deferred_opt, argc);
315 return CL_SDCPP;
316 #endif
318 /* Handle switch SCODE with argument ARG. VALUE is true, unless no-
319 form of an -f or -W option was given. Returns false if the switch was
320 invalid, true if valid. Use HANDLERS in recursive handle_option calls. */
321 bool
322 c_common_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
323 int kind, location_t loc,
324 const struct cl_option_handlers *handlers)
326 const struct cl_option *option = &cl_options[scode];
327 enum opt_code code = (enum opt_code) scode;
328 bool result = true;
330 /* Prevent resetting the language standard to a C dialect when the driver
331 has already determined that we're looking at assembler input. */
332 bool preprocessing_asm_p = (cpp_get_options (parse_in)->lang == CLK_ASM);
334 switch (code)
336 default:
337 if (cl_options[code].flags & c_family_lang_mask)
339 if ((option->flags & CL_TARGET)
340 // sdcpp && ! targetcm.handle_c_option (scode, arg, value)
342 result = false;
343 break;
345 result = false;
346 break;
348 case OPT__output_pch_:
349 pch_file = arg;
350 break;
352 case OPT_A:
353 defer_opt (code, arg);
354 break;
356 case OPT_C:
357 cpp_opts->discard_comments = 0;
358 break;
360 case OPT_CC:
361 cpp_opts->discard_comments = 0;
362 cpp_opts->discard_comments_in_macro_exp = 0;
363 break;
365 case OPT_D:
366 defer_opt (code, arg);
367 break;
369 case OPT_H:
370 cpp_opts->print_include_names = 1;
371 break;
373 case OPT_F:
374 TARGET_OPTF (xstrdup (arg));
375 break;
377 case OPT_I:
378 if (strcmp (arg, "-"))
379 add_path (xstrdup (arg), INC_BRACKET, 0, true);
380 else
382 if (quote_chain_split)
383 error ("%<-I-%> specified twice");
384 quote_chain_split = true;
385 split_quote_chain ();
386 inform (input_location, "obsolete option %<-I-%> used, "
387 "please use %<-iquote%> instead");
389 break;
391 case OPT_M:
392 case OPT_MM:
393 /* When doing dependencies with -M or -MM, suppress normal
394 preprocessed output, but still do -dM etc. as software
395 depends on this. Preprocessed output does occur if -MD, -MMD
396 or environment var dependency generation is used. */
397 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
398 flag_no_output = 1;
399 break;
401 case OPT_MD:
402 case OPT_MMD:
403 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
404 cpp_opts->deps.need_preprocessor_output = true;
405 deps_file = arg;
406 break;
408 case OPT_MF:
409 deps_seen = true;
410 deps_file = arg;
411 break;
413 case OPT_MG:
414 deps_seen = true;
415 cpp_opts->deps.missing_files = true;
416 break;
418 case OPT_MP:
419 deps_seen = true;
420 cpp_opts->deps.phony_targets = true;
421 break;
423 case OPT_Mmodules:
424 /* Do not set deps_seen, so the user can unconditionally turn
425 this on or off. */
426 cpp_opts->deps.modules = true;
427 break;
429 case OPT_Mno_modules:
430 /* Do not set deps_seen, so the user can unconditionally turn
431 this on or off. */
432 cpp_opts->deps.modules = false;
433 break;
435 case OPT_MQ:
436 case OPT_MT:
437 deps_seen = true;
438 defer_opt (code, arg);
439 break;
441 case OPT_P:
442 flag_no_line_commands = 1;
443 break;
445 case OPT_U:
446 defer_opt (code, arg);
447 break;
449 case OPT_Wall:
450 /* ??? Don't add new options here. Use LangEnabledBy in c.opt. */
452 cpp_opts->warn_num_sign_change = value;
453 break;
455 case OPT_Wunknown_pragmas:
456 /* Set to greater than 1, so that even unknown pragmas in
457 system headers will be warned about. */
458 /* ??? There is no way to handle this automatically for now. */
459 warn_unknown_pragmas = value * 2;
460 break;
462 case OPT_ansi:
463 if (!c_dialect_cxx ())
464 set_std_c89 (false, true);
465 else
466 set_std_cxx98 (true);
467 break;
469 case OPT_d:
470 handle_OPT_d (arg);
471 break;
473 case OPT_Wabi_:
474 warn_abi = true;
475 if (value == 1)
477 warning (0, "%<-Wabi=1%> is not supported, using =2");
478 value = 2;
480 warn_abi_version = value;
481 break;
483 case OPT_fcanonical_system_headers:
484 cpp_opts->canonical_system_headers = value;
485 break;
487 case OPT_fcond_mismatch:
488 if (!c_dialect_cxx ())
490 flag_cond_mismatch = value;
491 break;
493 warning (0, "switch %qs is no longer supported", option->opt_text);
494 break;
496 case OPT_fbuiltin_:
497 if (value)
498 result = false;
499 else
500 disable_builtin_function (arg);
501 break;
503 case OPT_fdirectives_only:
504 cpp_opts->directives_only = value;
505 break;
507 case OPT_fdollars_in_identifiers:
508 cpp_opts->dollars_in_ident = value;
509 break;
511 case OPT_fmacro_prefix_map_:
512 add_macro_prefix_map (arg);
513 break;
515 case OPT_ffreestanding:
516 value = !value;
517 /* Fall through. */
518 case OPT_fhosted:
519 flag_hosted = value;
520 flag_no_builtin = !value;
521 break;
523 case OPT_fconstant_string_class_:
524 constant_string_class_name = arg;
525 break;
527 case OPT_fextended_identifiers:
528 cpp_opts->extended_identifiers = value;
529 break;
531 case OPT_fmax_include_depth_:
532 cpp_opts->max_include_depth = value;
533 break;
535 case OPT_foperator_names:
536 cpp_opts->operator_names = value;
537 break;
539 case OPT_fpch_deps:
540 cpp_opts->restore_pch_deps = value;
541 break;
543 case OPT_fpch_preprocess:
544 flag_pch_preprocess = value;
545 break;
547 case OPT_fpermissive:
548 flag_permissive = value;
549 global_dc->permissive = value;
550 break;
552 case OPT_fpreprocessed:
553 cpp_opts->preprocessed = value;
554 break;
556 case OPT_fdebug_cpp:
557 cpp_opts->debug = 1;
558 break;
560 case OPT_ftrack_macro_expansion:
561 if (value)
562 value = 2;
563 /* Fall Through. */
565 case OPT_ftrack_macro_expansion_:
566 if (arg && *arg != '\0')
567 cpp_opts->track_macro_expansion = value;
568 else
569 cpp_opts->track_macro_expansion = 2;
570 break;
572 case OPT_fexec_charset_:
573 cpp_opts->narrow_charset = arg;
574 break;
576 case OPT_fwide_exec_charset_:
577 cpp_opts->wide_charset = arg;
578 break;
580 case OPT_finput_charset_:
581 cpp_opts->input_charset = arg;
582 break;
584 case OPT_ftemplate_depth_:
585 max_tinst_depth = value;
586 break;
588 case OPT_fvisibility_inlines_hidden:
589 visibility_options.inlines_hidden = value;
590 break;
592 case OPT_femit_struct_debug_baseonly:
593 set_struct_debug_option (&global_options, loc, "base");
594 break;
596 case OPT_femit_struct_debug_reduced:
597 set_struct_debug_option (&global_options, loc,
598 "dir:ord:sys,dir:gen:any,ind:base");
599 break;
601 case OPT_femit_struct_debug_detailed_:
602 set_struct_debug_option (&global_options, loc, arg);
603 break;
605 case OPT_fext_numeric_literals:
606 cpp_opts->ext_numeric_literals = value;
607 break;
609 case OPT_idirafter:
610 add_path (xstrdup (arg), INC_AFTER, 0, true);
611 break;
613 case OPT_imacros:
614 case OPT_include:
615 defer_opt (code, arg);
616 break;
618 case OPT_imultilib:
619 imultilib = arg;
620 break;
622 case OPT_iprefix:
623 iprefix = arg;
624 break;
626 case OPT_iquote:
627 add_path (xstrdup (arg), INC_QUOTE, 0, true);
628 break;
630 case OPT_isysroot:
631 sysroot = arg;
632 break;
634 case OPT_isystem:
635 add_path (xstrdup (arg), INC_SYSTEM, 0, true);
636 break;
638 case OPT_iwithprefix:
639 add_prefixed_path (arg, INC_SYSTEM);
640 break;
642 case OPT_iwithprefixbefore:
643 add_prefixed_path (arg, INC_BRACKET);
644 break;
646 case OPT_lang_asm:
647 cpp_set_lang (parse_in, CLK_ASM);
648 cpp_opts->dollars_in_ident = false;
649 break;
651 case OPT_nostdinc:
652 std_inc = false;
653 break;
655 case OPT_nostdinc__:
656 std_cxx_inc = false;
657 break;
659 case OPT_o:
660 if (!out_fname)
661 out_fname = arg;
662 else
663 error ("output filename specified twice");
664 break;
666 case OPT_print_objc_runtime_info:
667 print_struct_values = 1;
668 break;
670 case OPT_remap:
671 cpp_opts->remap = 1;
672 break;
674 case OPT_std_c__98:
675 case OPT_std_gnu__98:
676 if (!preprocessing_asm_p)
677 set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
678 break;
680 case OPT_std_c__11:
681 case OPT_std_gnu__11:
682 if (!preprocessing_asm_p)
683 set_std_cxx11 (code == OPT_std_c__11 /* ISO */);
684 break;
686 case OPT_std_c__14:
687 case OPT_std_gnu__14:
688 if (!preprocessing_asm_p)
689 set_std_cxx14 (code == OPT_std_c__14 /* ISO */);
690 break;
692 case OPT_std_c__17:
693 case OPT_std_gnu__17:
694 if (!preprocessing_asm_p)
695 set_std_cxx17 (code == OPT_std_c__17 /* ISO */);
696 break;
698 case OPT_std_c__20:
699 case OPT_std_gnu__20:
700 if (!preprocessing_asm_p)
701 set_std_cxx20 (code == OPT_std_c__20 /* ISO */);
702 break;
704 case OPT_std_c__23:
705 case OPT_std_gnu__23:
706 if (!preprocessing_asm_p)
707 set_std_cxx23 (code == OPT_std_c__23 /* ISO */);
708 break;
710 case OPT_std_c90:
711 case OPT_std_iso9899_199409:
712 if (!preprocessing_asm_p)
713 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
714 break;
716 case OPT_std_gnu90:
717 if (!preprocessing_asm_p)
718 set_std_c89 (false /* c94 */, false /* ISO */);
719 break;
721 case OPT_std_c99:
722 if (!preprocessing_asm_p)
723 set_std_c99 (true /* ISO */);
724 break;
726 case OPT_std_gnu99:
727 if (!preprocessing_asm_p)
728 set_std_c99 (false /* ISO */);
729 break;
731 case OPT_std_c11:
732 if (!preprocessing_asm_p)
733 set_std_c11 (true /* ISO */);
734 break;
736 case OPT_std_gnu11:
737 if (!preprocessing_asm_p)
738 set_std_c11 (false /* ISO */);
739 break;
741 case OPT_std_c17:
742 if (!preprocessing_asm_p)
743 set_std_c17 (true /* ISO */);
744 break;
746 case OPT_std_gnu17:
747 if (!preprocessing_asm_p)
748 set_std_c17 (false /* ISO */);
749 break;
751 case OPT_std_c23:
752 if (!preprocessing_asm_p)
753 set_std_c23 (true /* ISO */);
754 break;
756 case OPT_std_gnu2x:
757 if (!preprocessing_asm_p)
758 set_std_c23 (false /* ISO */);
759 break;
761 case OPT_trigraphs:
762 cpp_opts->trigraphs = 1;
763 break;
765 case OPT_traditional_cpp:
766 cpp_opts->traditional = 1;
767 break;
769 case OPT_v:
770 verbose = true;
771 break;
774 switch (c_language)
776 case clk_c:
777 C_handle_option_auto (&global_options, &global_options_set,
778 scode, arg, value,
779 c_family_lang_mask, kind,
780 loc, handlers, global_dc);
781 break;
783 case clk_objc:
784 ObjC_handle_option_auto (&global_options, &global_options_set,
785 scode, arg, value,
786 c_family_lang_mask, kind,
787 loc, handlers, global_dc);
788 break;
790 case clk_cxx:
791 CXX_handle_option_auto (&global_options, &global_options_set,
792 scode, arg, value,
793 c_family_lang_mask, kind,
794 loc, handlers, global_dc);
795 break;
797 case clk_objcxx:
798 ObjCXX_handle_option_auto (&global_options, &global_options_set,
799 scode, arg, value,
800 c_family_lang_mask, kind,
801 loc, handlers, global_dc);
802 break;
804 default:
805 gcc_unreachable ();
808 cpp_handle_option_auto (&global_options, scode, cpp_opts);
809 return result;
812 /* Default implementation of TARGET_HANDLE_C_OPTION. */
814 bool
815 default_handle_c_option (size_t code ATTRIBUTE_UNUSED,
816 const char *arg ATTRIBUTE_UNUSED,
817 int value ATTRIBUTE_UNUSED)
819 return false;
822 /* Post-switch processing. */
823 bool
824 c_common_post_options (const char **pfilename)
826 /* Canonicalize the input and output filenames. */
827 if (in_fnames == NULL)
829 in_fnames = XNEWVEC (const char *, 1);
830 in_fnames[0] = "";
832 else if (strcmp (in_fnames[0], "-") == 0)
834 if (pch_file)
835 error ("cannot use %<-%> as input filename for a precompiled header");
837 in_fnames[0] = "";
840 if (out_fname == NULL || !strcmp (out_fname, "-"))
841 out_fname = "";
843 if (cpp_opts->deps.style == DEPS_NONE)
844 check_deps_environment_vars ();
846 handle_deferred_opts ();
848 sanitize_cpp_opts ();
850 register_include_chains (parse_in, sysroot, iprefix, imultilib,
851 std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
853 #ifdef C_COMMON_OVERRIDE_OPTIONS
854 /* Some machines may reject certain combinations of C
855 language-specific options. */
856 C_COMMON_OVERRIDE_OPTIONS;
857 #endif
859 /* Excess precision other than "fast" requires front-end
860 support. */
861 if (c_dialect_cxx ())
862 { untested();
863 if (flag_excess_precision == EXCESS_PRECISION_STANDARD)
864 sorry ("%<-fexcess-precision=standard%> for C++");
865 flag_excess_precision = EXCESS_PRECISION_FAST;
867 else if (flag_excess_precision == EXCESS_PRECISION_DEFAULT)
868 flag_excess_precision = (flag_iso ? EXCESS_PRECISION_STANDARD
869 : EXCESS_PRECISION_FAST);
871 /* ISO C restricts floating-point expression contraction to within
872 source-language expressions (-ffp-contract=on, currently an alias
873 for -ffp-contract=off). */
874 if (flag_iso
875 && !c_dialect_cxx ()
876 && (OPTION_SET_P (flag_fp_contract_mode)
877 == (enum fp_contract_mode) 0)
878 && flag_unsafe_math_optimizations == 0)
879 flag_fp_contract_mode = FP_CONTRACT_OFF;
881 /* If we are compiling C, and we are outside of a standards mode,
882 we can permit the new values from ISO/IEC TS 18661-3 for
883 FLT_EVAL_METHOD. Otherwise, we must restrict the possible values to
884 the set specified in ISO C99/C11. */
885 if (!flag_iso
886 && !c_dialect_cxx ()
887 && (OPTION_SET_P (flag_permitted_flt_eval_methods)
888 == PERMITTED_FLT_EVAL_METHODS_DEFAULT))
889 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_TS_18661;
890 else
891 flag_permitted_flt_eval_methods = PERMITTED_FLT_EVAL_METHODS_C11;
893 /* C2X Annex F does not permit certain built-in functions to raise
894 "inexact". */
895 if (flag_isoc23)
896 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
897 flag_fp_int_builtin_inexact, 0);
899 /* By default we use C99 inline semantics in GNU99 or C99 mode. C99
900 inline semantics are not supported in GNU89 or C89 mode. */
901 if (flag_gnu89_inline == -1)
902 flag_gnu89_inline = !flag_isoc99;
903 else if (!flag_gnu89_inline && !flag_isoc99)
904 error ("%<-fno-gnu89-inline%> is only supported in GNU99 or C99 mode");
906 /* Default to ObjC sjlj exception handling if NeXT runtime < v2. */
907 if (flag_objc_sjlj_exceptions < 0)
908 flag_objc_sjlj_exceptions = (flag_next_runtime && flag_objc_abi < 2);
909 if (flag_objc_exceptions && !flag_objc_sjlj_exceptions)
910 flag_exceptions = 1;
912 /* If -ffreestanding, -fno-hosted or -fno-builtin then disable
913 pattern recognition. */
914 if (flag_no_builtin)
915 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
916 flag_tree_loop_distribute_patterns, 0);
918 /* -Woverlength-strings is off by default, but is enabled by -Wpedantic.
919 It is never enabled in C++, as the minimum limit is not normative
920 in that standard. */
921 if (c_dialect_cxx ())
922 warn_overlength_strings = 0;
924 /* Wmain is enabled by default in C++ but not in C. */
925 /* Wmain is disabled by default for -ffreestanding (!flag_hosted),
926 even if -Wall or -Wpedantic was given (warn_main will be 2 if set
927 by -Wall, 1 if set by -Wmain). */
928 if (warn_main == -1)
929 warn_main = (c_dialect_cxx () && flag_hosted) ? 1 : 0;
930 else if (warn_main == 2)
931 warn_main = flag_hosted ? 1 : 0;
933 /* In C, -Wall and -Wc++-compat enable -Wenum-compare; if it has not
934 yet been set, it is disabled by default. In C++, it is enabled
935 by default. */
936 if (warn_enum_compare == -1)
937 warn_enum_compare = c_dialect_cxx () ? 1 : 0;
939 /* -Wpacked-bitfield-compat is on by default for the C languages. The
940 warning is issued in stor-layout.cc which is not part of the front-end so
941 we need to selectively turn it on here. */
942 if (warn_packed_bitfield_compat == -1)
943 warn_packed_bitfield_compat = 1;
945 /* Special format checking options don't work without -Wformat; warn if
946 they are used. */
947 if (!warn_format)
949 warning (OPT_Wformat_y2k,
950 "%<-Wformat-y2k%> ignored without %<-Wformat%>");
951 warning (OPT_Wformat_extra_args,
952 "%<-Wformat-extra-args%> ignored without %<-Wformat%>");
953 warning (OPT_Wformat_zero_length,
954 "%<-Wformat-zero-length%> ignored without %<-Wformat%>");
955 warning (OPT_Wformat_nonliteral,
956 "%<-Wformat-nonliteral%> ignored without %<-Wformat%>");
957 warning (OPT_Wformat_contains_nul,
958 "%<-Wformat-contains-nul%> ignored without %<-Wformat%>");
959 warning (OPT_Wformat_security,
960 "%<-Wformat-security%> ignored without %<-Wformat%>");
963 /* -Wimplicit-function-declaration is enabled by default for C99. */
964 if (warn_implicit_function_declaration == -1)
965 warn_implicit_function_declaration = flag_isoc99;
967 /* -Wimplicit-int is enabled by default for C99. */
968 if (warn_implicit_int == -1)
969 warn_implicit_int = flag_isoc99;
971 /* -Wold-style-definition is enabled by default for C2X. */
972 if (warn_old_style_definition == -1)
973 warn_old_style_definition = flag_isoc23;
975 /* -Wshift-overflow is enabled by default in C99 and C++11 modes. */
976 if (warn_shift_overflow == -1)
977 warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99;
979 /* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 to C++17
980 modes. */
981 if (warn_shift_negative_value == -1)
982 warn_shift_negative_value = (extra_warnings
983 && (cxx_dialect >= cxx11 || flag_isoc99)
984 && cxx_dialect < cxx20);
986 /* -Wregister is enabled by default in C++17. */
987 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_register,
988 cxx_dialect >= cxx17);
990 /* -Wcomma-subscript is enabled by default in C++20. */
991 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
992 warn_comma_subscript,
993 cxx_dialect >= cxx23
994 || (cxx_dialect == cxx20 && warn_deprecated));
996 /* -Wvolatile is enabled by default in C++20. */
997 SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_volatile,
998 cxx_dialect >= cxx20 && warn_deprecated);
1000 /* -Wdeprecated-enum-enum-conversion is enabled by default in C++20. */
1001 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1002 warn_deprecated_enum_enum_conv,
1003 cxx_dialect >= cxx20 && warn_deprecated);
1005 /* -Wdeprecated-enum-float-conversion is enabled by default in C++20. */
1006 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1007 warn_deprecated_enum_float_conv,
1008 cxx_dialect >= cxx20 && warn_deprecated);
1010 /* Declone C++ 'structors if -Os. */
1011 if (flag_declone_ctor_dtor == -1)
1012 flag_declone_ctor_dtor = optimize_size;
1014 if (flag_abi_compat_version == 1)
1016 warning (0, "%<-fabi-compat-version=1%> is not supported, using =2");
1017 flag_abi_compat_version = 2;
1020 /* Change flag_abi_version to be the actual current ABI level, for the
1021 benefit of c_cpp_builtins, and to make comparison simpler. */
1022 const int latest_abi_version = 17;
1023 /* Generate compatibility aliases for ABI v13 (8.2) by default. */
1024 const int abi_compat_default = 13;
1026 #define clamp(X) if (X == 0 || X > latest_abi_version) X = latest_abi_version
1027 clamp (flag_abi_version);
1028 clamp (warn_abi_version);
1029 clamp (flag_abi_compat_version);
1030 #undef clamp
1032 /* Default -Wabi= or -fabi-compat-version= from each other. */
1033 if (warn_abi_version == -1 && flag_abi_compat_version != -1)
1034 warn_abi_version = flag_abi_compat_version;
1035 else if (flag_abi_compat_version == -1 && warn_abi_version != -1)
1036 flag_abi_compat_version = warn_abi_version;
1037 else if (warn_abi_version == -1 && flag_abi_compat_version == -1)
1039 warn_abi_version = latest_abi_version;
1040 if (flag_abi_version == latest_abi_version)
1042 auto_diagnostic_group d;
1043 if (warning (OPT_Wabi, "%<-Wabi%> won%'t warn about anything"))
1045 inform (input_location, "%<-Wabi%> warns about differences "
1046 "from the most up-to-date ABI, which is also used "
1047 "by default");
1048 inform (input_location, "use e.g. %<-Wabi=11%> to warn about "
1049 "changes from GCC 7");
1051 flag_abi_compat_version = abi_compat_default;
1053 else
1054 flag_abi_compat_version = latest_abi_version;
1057 /* By default, enable the new inheriting constructor semantics along with ABI
1058 11. New and old should coexist fine, but it is a change in what
1059 artificial symbols are generated. */
1060 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1061 flag_new_inheriting_ctors,
1062 abi_version_at_least (11));
1064 /* For GCC 7, only enable DR150 resolution by default if -std=c++17. */
1065 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_new_ttp,
1066 cxx_dialect >= cxx17);
1068 /* C++11 guarantees forward progress. */
1069 SET_OPTION_IF_UNSET (&global_options, &global_options_set, flag_finite_loops,
1070 optimize >= 2 && cxx_dialect >= cxx11);
1072 /* It's OK to discard calls to pure/const functions that might throw. */
1073 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
1074 flag_delete_dead_exceptions, true);
1076 if (cxx_dialect >= cxx11)
1078 /* If we're allowing C++0x constructs, don't warn about C++98
1079 identifiers which are keywords in C++0x. */
1080 warn_cxx11_compat = 0;
1081 cpp_opts->cpp_warn_cxx11_compat = 0;
1083 if (warn_narrowing == -1)
1084 warn_narrowing = 1;
1086 /* Unless -f{,no-}ext-numeric-literals has been used explicitly,
1087 for -std=c++{11,14,17,20,23} default to -fno-ext-numeric-literals. */
1088 if (flag_iso && !OPTION_SET_P (flag_ext_numeric_literals))
1089 cpp_opts->ext_numeric_literals = 0;
1091 else if (warn_narrowing == -1)
1092 warn_narrowing = 0;
1094 /* C++17 has stricter evaluation order requirements; let's use some of them
1095 for earlier C++ as well, so chaining works as expected. */
1096 if (c_dialect_cxx ()
1097 && flag_strong_eval_order == -1)
1098 flag_strong_eval_order = (cxx_dialect >= cxx17 ? 2 : 1);
1100 if (flag_implicit_constexpr && cxx_dialect < cxx14)
1101 flag_implicit_constexpr = false;
1103 /* Global sized deallocation is new in C++14. */
1104 if (flag_sized_deallocation == -1)
1105 flag_sized_deallocation = (cxx_dialect >= cxx14);
1107 /* char8_t support is new in C++20. */
1108 if (flag_char8_t == -1)
1109 flag_char8_t = (cxx_dialect >= cxx20);
1111 if (flag_extern_tls_init)
1113 if (!TARGET_SUPPORTS_ALIASES || !SUPPORTS_WEAK)
1115 /* Lazy TLS initialization for a variable in another TU requires
1116 alias and weak reference support. */
1117 if (flag_extern_tls_init > 0)
1118 sorry ("external TLS initialization functions not supported "
1119 "on this target");
1121 flag_extern_tls_init = 0;
1123 else
1124 flag_extern_tls_init = 1;
1127 /* Enable by default only for C++ and C++ with ObjC extensions. */
1128 if (warn_return_type == -1 && c_dialect_cxx ())
1129 warn_return_type = 1;
1131 /* C++20 is the final version of concepts. We still use -fconcepts
1132 to know when concepts are enabled. Note that -fconcepts-ts can
1133 be used to include additional features, although modified to
1134 work with the standard. */
1135 if (cxx_dialect >= cxx20 || flag_concepts_ts)
1136 flag_concepts = 1;
1137 else if (flag_concepts)
1138 /* For -std=c++17 -fconcepts, imply -fconcepts-ts. */
1139 flag_concepts_ts = 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
1161 { untested();
1162 #if 0 // sdcpp
1163 init_c_lex ();
1165 /* When writing a PCH file, avoid reading some other PCH file,
1166 because the default address space slot then can't be used
1167 for the output PCH file. */
1168 if (pch_file)
1169 { untested();
1170 c_common_no_more_pch ();
1171 /* Only -g0 and -gdwarf* are supported with PCH, for other
1172 debug formats we warn here and refuse to load any PCH files. */
1173 if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1174 warning (OPT_Wdeprecated,
1175 "the %qs debug info cannot be used with "
1176 "pre-compiled headers",
1177 debug_set_names (write_symbols & ~DWARF2_DEBUG));
1179 else if (write_symbols != NO_DEBUG && write_symbols != DWARF2_DEBUG)
1180 c_common_no_more_pch ();
1182 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1183 input_location = UNKNOWN_LOCATION;
1184 #endif // sdcpp
1187 struct cpp_callbacks *cb = cpp_get_callbacks (parse_in);
1188 cb->file_change = cb_file_change;
1189 cb->dir_change = cb_dir_change;
1190 if (lang_hooks.preprocess_options)
1191 lang_hooks.preprocess_options (parse_in);
1192 cpp_post_options (parse_in);
1193 init_global_opts_from_cpp (&global_options, cpp_get_options (parse_in));
1195 /* Let diagnostics infrastructure know how to convert input files the same
1196 way libcpp will do it, namely using the configured input charset and
1197 skipping a UTF-8 BOM if present. */
1198 diagnostic_initialize_input_context (global_dc,
1199 c_common_input_charset_cb, true);
1200 input_location = UNKNOWN_LOCATION;
1202 *pfilename = this_input_filename
1203 = cpp_read_main_file (parse_in, in_fnames[0],
1204 /* We'll inject preamble pieces if this is
1205 not preprocessed. */
1206 !cpp_opts->preprocessed);
1208 /* Don't do any compilation or preprocessing if there is no input file. */
1209 if (this_input_filename == NULL)
1210 { untested();
1211 errorcount++;
1212 return false;
1215 if (flag_working_directory
1216 && flag_preprocess_only && !flag_no_line_commands)
1217 pp_dir_change (parse_in, get_src_pwd ());
1219 /* Disable LTO output when outputting a precompiled header. */
1220 if (pch_file && flag_lto)
1221 { untested();
1222 flag_lto = 0;
1223 flag_generate_lto = 0;
1226 return flag_preprocess_only;
1229 /* Front end initialization common to C, ObjC and C++. */
1230 // looks like sdcpp_common_init
1231 bool
1232 c_common_init (void)
1234 /* Set up preprocessor arithmetic. Must be done after call to
1235 c_common_nodes_and_builtins for type nodes to be good. */
1237 #if 1 // sdcpp
1238 cpp_opts->precision = CHAR_BIT * sizeof (long);
1239 cpp_opts->char_precision = CHAR_BIT;
1240 cpp_opts->int_precision = CHAR_BIT * sizeof (int);
1241 cpp_opts->wchar_precision = CHAR_BIT * sizeof (int);
1242 cpp_opts->unsigned_wchar = 1;
1243 #else // gcc
1244 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1245 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1246 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1247 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1248 cpp_opts->unsigned_wchar = TYPE_UNSIGNED (wchar_type_node);
1249 #endif
1250 cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
1252 /* This can't happen until after wchar_precision and bytes_big_endian
1253 are known. */
1254 cpp_init_iconv (parse_in);
1256 #if 0 //sdcpp
1257 if (version_flag)
1259 int i;
1260 fputs ("Compiler executable checksum: ", stderr);
1261 for (i = 0; i < 16; i++)
1262 fprintf (stderr, "%02x", executable_checksum[i]);
1263 putc ('\n', stderr);
1265 #endif
1267 /* Has to wait until now so that cpplib has its hash table. */
1268 init_pragma ();
1270 gcc_assert (flag_preprocess_only);
1271 if (flag_preprocess_only)
1273 c_finish_options ();
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 unsigned int i;
1288 i = 0;
1289 for (;;)
1291 c_finish_options ();
1292 /* Open the dump file to use for the original dump output
1293 here, to be used during parsing for the current file. */
1294 original_dump_file = dump_begin (TDI_original, &original_dump_flags);
1295 pch_init ();
1296 push_file_scope ();
1297 c_parse_file ();
1298 pop_file_scope ();
1299 /* And end the main input file, if the debug writer wants it */
1300 if (debug_hooks->start_end_main_source_file)
1301 (*debug_hooks->end_source_file) (0);
1302 if (++i >= num_in_fnames)
1303 break;
1304 cpp_undef_all (parse_in);
1305 cpp_clear_file_cache (parse_in);
1306 this_input_filename
1307 = cpp_read_main_file (parse_in, in_fnames[i]);
1308 if (original_dump_file)
1310 dump_end (TDI_original, original_dump_file);
1311 original_dump_file = NULL;
1313 /* If an input file is missing, abandon further compilation.
1314 cpplib has issued a diagnostic. */
1315 if (!this_input_filename)
1316 break;
1319 c_parse_final_cleanups ();
1322 /* Returns the appropriate dump file for PHASE to dump with FLAGS. */
1324 FILE *
1325 get_dump_info (int phase, dump_flags_t *flags)
1327 gcc_assert (phase == TDI_original);
1329 *flags = original_dump_flags;
1330 return original_dump_file;
1333 /* Common finish hook for the C, ObjC and C++ front ends. */
1334 void
1335 c_common_finish (void)
1337 FILE *deps_stream = NULL;
1339 /* Note that we write the dependencies even if there are errors. This is
1340 useful for handling outdated generated headers that now trigger errors
1341 (for example, with #error) which would be resolved by re-generating
1342 them. In a sense, this complements -MG. */
1343 if (cpp_opts->deps.style != DEPS_NONE)
1345 /* If -M or -MM was seen without -MF, default output to the
1346 output stream. */
1347 if (!deps_file)
1348 deps_stream = out_stream;
1349 else if (deps_file[0] == '-' && deps_file[1] == '\0')
1350 deps_stream = stdout;
1351 else
1353 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1354 if (!deps_stream)
1355 fatal_error (input_location, "opening dependency file %s: %m",
1356 deps_file);
1360 /* For performance, avoid tearing down cpplib's internal structures
1361 with cpp_destroy (). */
1362 cpp_finish (parse_in, deps_stream);
1364 if (deps_stream && deps_stream != out_stream && deps_stream != stdout
1365 && (ferror (deps_stream) || fclose (deps_stream)))
1366 fatal_error (input_location, "closing dependency file %s: %m", deps_file);
1368 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1369 fatal_error (input_location, "when writing output to %s: %m", out_fname);
1372 /* Either of two environment variables can specify output of
1373 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1374 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1375 and DEPS_TARGET is the target to mention in the deps. They also
1376 result in dependency information being appended to the output file
1377 rather than overwriting it, and like Sun's compiler
1378 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1379 static void
1380 check_deps_environment_vars (void)
1382 char *spec;
1384 spec = getenv ("DEPENDENCIES_OUTPUT");
1385 if (spec)
1386 cpp_opts->deps.style = DEPS_USER;
1387 else
1389 spec = getenv ("SUNPRO_DEPENDENCIES");
1390 if (spec)
1392 cpp_opts->deps.style = DEPS_SYSTEM;
1393 cpp_opts->deps.ignore_main_file = true;
1397 if (spec)
1399 /* Find the space before the DEPS_TARGET, if there is one. */
1400 char *s = strchr (spec, ' ');
1401 if (s)
1403 /* Let the caller perform MAKE quoting. */
1404 defer_opt (OPT_MT, s + 1);
1405 *s = '\0';
1408 /* Command line -MF overrides environment variables and default. */
1409 if (!deps_file)
1410 deps_file = spec;
1412 deps_append = 1;
1413 deps_seen = true;
1417 /* Handle deferred command line switches. */
1418 static void
1419 handle_deferred_opts (void)
1421 /* Avoid allocating the deps buffer if we don't need it.
1422 (This flag may be true without there having been -MT or -MQ
1423 options, but we'll still need the deps buffer.) */
1424 if (!deps_seen)
1425 return;
1427 if (mkdeps *deps = cpp_get_deps (parse_in))
1428 for (unsigned i = 0; i < deferred_count; i++)
1430 struct deferred_opt *opt = &deferred_opts[i];
1432 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1433 deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
1437 /* These settings are appropriate for GCC, but not necessarily so for
1438 cpplib as a library. */
1439 static void
1440 sanitize_cpp_opts (void)
1442 /* If we don't know what style of dependencies to output, complain
1443 if any other dependency switches have been given. */
1444 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1445 error ("to generate dependencies you must specify either %<-M%> "
1446 "or %<-MM%>");
1448 /* -dM and dependencies suppress normal output; do it here so that
1449 the last -d[MDN] switch overrides earlier ones. */
1450 if (flag_dump_macros == 'M')
1451 flag_no_output = 1;
1453 /* By default, -fdirectives-only implies -dD. This allows subsequent phases
1454 to perform proper macro expansion. */
1455 if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros)
1456 flag_dump_macros = 'D';
1458 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1459 -dM since at least glibc relies on -M -dM to work. */
1460 /* Also, flag_no_output implies flag_no_line_commands, always. */
1461 if (flag_no_output)
1463 if (flag_dump_macros != 'M')
1464 flag_dump_macros = 0;
1465 flag_dump_includes = 0;
1466 flag_no_line_commands = 1;
1468 else if (cpp_opts->deps.missing_files)
1469 error ("%<-MG%> may only be used with %<-M%> or %<-MM%>");
1471 cpp_opts->unsigned_char = !flag_signed_char;
1472 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1474 /* Wlong-long is disabled by default. It is enabled by:
1475 [-Wpedantic | -Wtraditional] -std=[gnu|c]++98 ; or
1476 [-Wpedantic | -Wtraditional] -std=non-c99
1478 Either -Wlong-long or -Wno-long-long override any other settings.
1479 ??? These conditions should be handled in c.opt. */
1480 if (warn_long_long == -1)
1482 warn_long_long = ((pedantic || warn_traditional)
1483 && (c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99));
1484 cpp_opts->cpp_warn_long_long = warn_long_long;
1487 /* If we're generating preprocessor output, emit current directory
1488 if explicitly requested or if debugging information is enabled.
1489 ??? Maybe we should only do it for debugging formats that
1490 actually output the current directory? */
1491 if (flag_working_directory == -1)
1492 flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
1494 if (warn_implicit_fallthrough < 5)
1495 cpp_opts->cpp_warn_implicit_fallthrough = warn_implicit_fallthrough;
1496 else
1497 cpp_opts->cpp_warn_implicit_fallthrough = 0;
1499 if (cpp_opts->directives_only)
1501 if (cpp_warn_unused_macros)
1502 error ("%<-fdirectives-only%> is incompatible "
1503 "with %<-Wunused-macros%>");
1504 if (cpp_opts->traditional)
1505 error ("%<-fdirectives-only%> is incompatible with %<-traditional%>");
1509 /* Add include path with a prefix at the front of its name. */
1510 static void
1511 add_prefixed_path (const char *suffix, incpath_kind chain)
1513 char *path;
1514 const char *prefix;
1515 size_t prefix_len, suffix_len;
1517 suffix_len = strlen (suffix);
1518 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1519 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1521 path = (char *) xmalloc (prefix_len + suffix_len + 1);
1522 memcpy (path, prefix, prefix_len);
1523 memcpy (path + prefix_len, suffix, suffix_len);
1524 path[prefix_len + suffix_len] = '\0';
1526 add_path (path, chain, 0, false);
1529 /* Handle -D, -U, -A, -imacros, and the first -include. */
1530 static void
1531 c_finish_options (void)
1533 if (!cpp_opts->preprocessed)
1535 const line_map_ordinary *bltin_map
1536 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1537 _("<built-in>"), 0));
1538 cb_file_change (parse_in, bltin_map);
1539 linemap_line_start (line_table, 0, 1);
1541 /* Make sure all of the builtins about to be declared have
1542 BUILTINS_LOCATION has their location_t. */
1543 cpp_force_token_locations (parse_in, BUILTINS_LOCATION);
1545 cpp_init_builtins (parse_in, flag_hosted);
1546 // sdcpp c_cpp_builtins (parse_in);
1548 /* We're about to send user input to cpplib, so make it warn for
1549 things that we previously (when we sent it internal definitions)
1550 told it to not warn.
1552 C99 permits implementation-defined characters in identifiers.
1553 The documented meaning of -std= is to turn off extensions that
1554 conflict with the specified standard, and since a strictly
1555 conforming program cannot contain a '$', we do not condition
1556 their acceptance on the -std= setting. */
1557 cpp_opts->warn_dollars = (cpp_opts->cpp_pedantic && !cpp_opts->c99);
1559 const line_map_ordinary *cmd_map
1560 = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
1561 _("<command-line>"), 0));
1562 cb_file_change (parse_in, cmd_map);
1563 linemap_line_start (line_table, 0, 1);
1565 /* All command line defines must have the same location. */
1566 cpp_force_token_locations (parse_in, line_table->highest_line);
1567 for (size_t i = 0; i < deferred_count; i++)
1569 struct deferred_opt *opt = &deferred_opts[i];
1571 if (opt->code == OPT_D)
1572 cpp_define (parse_in, opt->arg);
1573 else if (opt->code == OPT_U)
1574 cpp_undef (parse_in, opt->arg);
1575 else if (opt->code == OPT_A)
1577 if (opt->arg[0] == '-')
1578 cpp_unassert (parse_in, opt->arg + 1);
1579 else
1580 cpp_assert (parse_in, opt->arg);
1584 cpp_stop_forcing_token_locations (parse_in);
1586 else if (cpp_opts->directives_only)
1587 cpp_init_special_builtins (parse_in);
1589 /* Start the main input file, if the debug writer wants it. */
1590 if (debug_hooks->start_end_main_source_file
1591 && !flag_preprocess_only)
1592 (*debug_hooks->start_source_file) (0, this_input_filename);
1594 if (!cpp_opts->preprocessed)
1595 /* Handle -imacros after -D and -U. */
1596 for (size_t i = 0; i < deferred_count; i++)
1598 struct deferred_opt *opt = &deferred_opts[i];
1600 if (opt->code == OPT_imacros
1601 && cpp_push_include (parse_in, opt->arg))
1603 /* Disable push_command_line_include callback for now. */
1604 include_cursor = deferred_count + 1;
1605 cpp_scan_nooutput (parse_in);
1609 include_cursor = 0;
1610 push_command_line_include ();
1613 /* Give CPP the next file given by -include, if any. */
1614 static void
1615 push_command_line_include (void)
1617 /* This can happen if disabled by -imacros for example.
1618 Punt so that we don't set "<command-line>" as the filename for
1619 the header. */
1620 if (include_cursor > deferred_count)
1621 return;
1623 if (!done_preinclude)
1625 done_preinclude = true;
1626 if (flag_hosted && std_inc && !cpp_opts->preprocessed)
1628 const char *preinc = 0; // sdcpp targetcm.c_preinclude ();
1629 if (preinc && cpp_push_default_include (parse_in, preinc))
1630 return;
1634 pch_cpp_save_state ();
1636 while (include_cursor < deferred_count)
1638 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1640 if (!cpp_opts->preprocessed && opt->code == OPT_include
1641 && cpp_push_include (parse_in, opt->arg))
1642 return;
1645 if (include_cursor == deferred_count)
1647 include_cursor++;
1648 /* -Wunused-macros should only warn about macros defined hereafter. */
1649 cpp_opts->warn_unused_macros = cpp_warn_unused_macros;
1650 /* Restore the line map back to the main file. */
1651 if (!cpp_opts->preprocessed)
1653 cpp_change_file (parse_in, LC_RENAME, this_input_filename);
1654 if (lang_hooks.preprocess_main_file)
1655 /* We're starting the main file. Inform the FE of that. */
1656 lang_hooks.preprocess_main_file
1657 (parse_in, line_table, LINEMAPS_LAST_ORDINARY_MAP (line_table));
1660 /* Set this here so the client can change the option if it wishes,
1661 and after stacking the main file so we don't trace the main file. */
1662 line_table->trace_includes = cpp_opts->print_include_names;
1666 /* File change callback. Has to handle -include files. */
1667 static void
1668 cb_file_change (cpp_reader *reader, const line_map_ordinary *new_map)
1670 if (flag_preprocess_only)
1671 pp_file_change (new_map);
1672 else
1673 fe_file_change (new_map);
1675 if (new_map && cpp_opts->preprocessed
1676 && lang_hooks.preprocess_main_file && MAIN_FILE_P (new_map)
1677 && ORDINARY_MAP_STARTING_LINE_NUMBER (new_map))
1678 /* We're starting the main file. Inform the FE of that. */
1679 lang_hooks.preprocess_main_file (reader, line_table, new_map);
1681 if (new_map
1682 && (new_map->reason == LC_ENTER || new_map->reason == LC_RENAME))
1684 /* Signal to plugins that a file is included. This could happen
1685 several times with the same file path, e.g. because of
1686 several '#include' or '#line' directives... */
1687 invoke_plugin_callbacks
1688 (PLUGIN_INCLUDE_FILE,
1689 const_cast<char*> (ORDINARY_MAP_FILE_NAME (new_map)));
1692 if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
1694 pch_cpp_save_state ();
1695 push_command_line_include ();
1699 void
1700 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
1702 if (!set_src_pwd (dir))
1703 warning (0, "too late for # directive to set debug directory");
1706 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1707 extensions if ISO). There is no concept of gnu94. */
1708 static void
1709 set_std_c89 (int c94, int iso)
1711 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1712 flag_iso = iso;
1713 flag_no_asm = iso;
1714 flag_no_gnu_keywords = iso;
1715 flag_no_nonansi_builtin = iso;
1716 flag_isoc94 = c94;
1717 flag_isoc99 = 0;
1718 flag_isoc11 = 0;
1719 flag_isoc23 = 0;
1720 lang_hooks.name = "GNU C89";
1723 /* Set the C 99 standard (without GNU extensions if ISO). */
1724 static void
1725 set_std_c99 (int iso)
1727 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1728 flag_no_asm = iso;
1729 flag_no_nonansi_builtin = iso;
1730 flag_iso = iso;
1731 flag_isoc23 = 0;
1732 flag_isoc11 = 0;
1733 flag_isoc99 = 1;
1734 flag_isoc94 = 1;
1735 lang_hooks.name = "GNU C99";
1738 /* Set the C 11 standard (without GNU extensions if ISO). */
1739 static void
1740 set_std_c11 (int iso)
1742 cpp_set_lang (parse_in, iso ? CLK_STDC11: CLK_GNUC11);
1743 flag_no_asm = iso;
1744 flag_no_nonansi_builtin = iso;
1745 flag_iso = iso;
1746 flag_isoc23 = 0;
1747 flag_isoc11 = 1;
1748 flag_isoc99 = 1;
1749 flag_isoc94 = 1;
1750 lang_hooks.name = "GNU C11";
1753 /* Set the C 17 standard (without GNU extensions if ISO). */
1754 static void
1755 set_std_c17 (int iso)
1757 cpp_set_lang (parse_in, iso ? CLK_STDC17: CLK_GNUC17);
1758 flag_no_asm = iso;
1759 flag_no_nonansi_builtin = iso;
1760 flag_iso = iso;
1761 flag_isoc23 = 0;
1762 flag_isoc11 = 1;
1763 flag_isoc99 = 1;
1764 flag_isoc94 = 1;
1765 lang_hooks.name = "GNU C17";
1768 /* Set the C 2X standard (without GNU extensions if ISO). */
1769 static void
1770 set_std_c23 (int iso)
1772 cpp_set_lang (parse_in, iso ? CLK_STDC2X: CLK_GNUC2X);
1773 flag_no_asm = iso;
1774 flag_no_nonansi_builtin = iso;
1775 flag_iso = iso;
1776 flag_isoc23 = 1;
1777 flag_isoc11 = 1;
1778 flag_isoc99 = 1;
1779 flag_isoc94 = 1;
1780 lang_hooks.name = "GNU C2X";
1784 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1785 static void
1786 set_std_cxx98 (int iso)
1788 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1789 flag_no_gnu_keywords = iso;
1790 flag_no_nonansi_builtin = iso;
1791 flag_iso = iso;
1792 flag_isoc94 = 0;
1793 flag_isoc99 = 0;
1794 cxx_dialect = cxx98;
1795 lang_hooks.name = "GNU C++98";
1798 /* Set the C++ 2011 standard (without GNU extensions if ISO). */
1799 static void
1800 set_std_cxx11 (int iso)
1802 cpp_set_lang (parse_in, iso ? CLK_CXX11: CLK_GNUCXX11);
1803 flag_no_gnu_keywords = iso;
1804 flag_no_nonansi_builtin = iso;
1805 flag_iso = iso;
1806 /* C++11 includes the C99 standard library. */
1807 flag_isoc94 = 1;
1808 flag_isoc99 = 1;
1809 cxx_dialect = cxx11;
1810 lang_hooks.name = "GNU C++11";
1813 /* Set the C++ 2014 standard (without GNU extensions if ISO). */
1814 static void
1815 set_std_cxx14 (int iso)
1817 cpp_set_lang (parse_in, iso ? CLK_CXX14: CLK_GNUCXX14);
1818 flag_no_gnu_keywords = iso;
1819 flag_no_nonansi_builtin = iso;
1820 flag_iso = iso;
1821 /* C++14 includes the C99 standard library. */
1822 flag_isoc94 = 1;
1823 flag_isoc99 = 1;
1824 cxx_dialect = cxx14;
1825 lang_hooks.name = "GNU C++14";
1828 /* Set the C++ 2017 standard (without GNU extensions if ISO). */
1829 static void
1830 set_std_cxx17 (int iso)
1832 cpp_set_lang (parse_in, iso ? CLK_CXX17: CLK_GNUCXX17);
1833 flag_no_gnu_keywords = iso;
1834 flag_no_nonansi_builtin = iso;
1835 flag_iso = iso;
1836 /* C++17 includes the C11 standard library. */
1837 flag_isoc94 = 1;
1838 flag_isoc99 = 1;
1839 flag_isoc11 = 1;
1840 cxx_dialect = cxx17;
1841 lang_hooks.name = "GNU C++17";
1844 /* Set the C++ 2020 standard (without GNU extensions if ISO). */
1845 static void
1846 set_std_cxx20 (int iso)
1848 cpp_set_lang (parse_in, iso ? CLK_CXX20: CLK_GNUCXX20);
1849 flag_no_gnu_keywords = iso;
1850 flag_no_nonansi_builtin = iso;
1851 flag_iso = iso;
1852 /* C++20 includes the C11 standard library. */
1853 flag_isoc94 = 1;
1854 flag_isoc99 = 1;
1855 flag_isoc11 = 1;
1856 /* C++20 includes coroutines. */
1857 flag_coroutines = true;
1858 cxx_dialect = cxx20;
1859 lang_hooks.name = "GNU C++20";
1862 /* Set the C++ 2023 standard (without GNU extensions if ISO). */
1863 static void
1864 set_std_cxx23 (int iso)
1866 cpp_set_lang (parse_in, iso ? CLK_CXX23: CLK_GNUCXX23);
1867 flag_no_gnu_keywords = iso;
1868 flag_no_nonansi_builtin = iso;
1869 flag_iso = iso;
1870 /* C++23 includes the C11 standard library. */
1871 flag_isoc94 = 1;
1872 flag_isoc99 = 1;
1873 flag_isoc11 = 1;
1874 /* C++23 includes coroutines. */
1875 flag_coroutines = true;
1876 cxx_dialect = cxx23;
1877 lang_hooks.name = "GNU C++23";
1880 /* Args to -d specify what to dump. Silently ignore
1881 unrecognized options; they may be aimed at toplev.cc. */
1882 static void
1883 handle_OPT_d (const char *arg)
1885 char c;
1887 while ((c = *arg++) != '\0')
1888 switch (c)
1890 case 'M': /* Dump macros only. */
1891 case 'N': /* Dump names. */
1892 case 'D': /* Dump definitions. */
1893 case 'U': /* Dump used macros. */
1894 flag_dump_macros = c;
1895 break;
1897 case 'I':
1898 flag_dump_includes = 1;
1899 break;