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