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