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