c-opts.c (complain_wrong_lang, [...]): Remove.
[platform/upstream/gcc.git] / gcc / c-opts.c
1 /* C/ObjC/C++ command line option handling.
2    Copyright (C) 2002, 2003 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 2, 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 COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "c-common.h"
28 #include "c-pragma.h"
29 #include "flags.h"
30 #include "toplev.h"
31 #include "langhooks.h"
32 #include "tree-inline.h"
33 #include "diagnostic.h"
34 #include "intl.h"
35 #include "cppdefault.h"
36 #include "c-incpath.h"
37 #include "debug.h"              /* For debug_hooks.  */
38 #include "opts.h"
39 #include "options.h"
40
41 #ifndef DOLLARS_IN_IDENTIFIERS
42 # define DOLLARS_IN_IDENTIFIERS true
43 #endif
44
45 #ifndef TARGET_SYSTEM_ROOT
46 # define TARGET_SYSTEM_ROOT NULL
47 #endif
48
49 #ifndef TARGET_EBCDIC
50 # define TARGET_EBCDIC 0
51 #endif
52
53 static const int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
54
55 static int saved_lineno;
56
57 /* CPP's options.  */
58 static cpp_options *cpp_opts;
59
60 /* Input filename.  */
61 static const char *in_fname;
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 system root, if any.  Overridden by -isysroot.  */
83 static const char *sysroot = TARGET_SYSTEM_ROOT;
84
85 /* Zero disables all standard directories for headers.  */
86 static bool std_inc = true;
87
88 /* Zero disables the C++-specific standard directories for headers.  */
89 static bool std_cxx_inc = true;
90
91 /* If the quote chain has been split by -I-.  */
92 static bool quote_chain_split;
93
94 /* If -Wunused-macros.  */
95 static bool warn_unused_macros;
96
97 /* Number of deferred options, deferred options array size.  */
98 static size_t deferred_count, deferred_size;
99
100 /* Number of deferred options scanned for -include.  */
101 static size_t include_cursor;
102
103 static void missing_arg (enum opt_code);
104 static void set_Wimplicit (int);
105 static void print_help (void);
106 static void handle_OPT_d (const char *);
107 static void set_std_cxx98 (int);
108 static void set_std_c89 (int, int);
109 static void set_std_c99 (int);
110 static void check_deps_environment_vars (void);
111 static void handle_deferred_opts (void);
112 static void sanitize_cpp_opts (void);
113 static void add_prefixed_path (const char *, size_t);
114 static void push_command_line_include (void);
115 static void cb_file_change (cpp_reader *, const struct line_map *);
116 static void finish_options (void);
117
118 #ifndef STDC_0_IN_SYSTEM_HEADERS
119 #define STDC_0_IN_SYSTEM_HEADERS 0
120 #endif
121
122 /* Holds switches parsed by c_common_handle_option (), but whose
123    handling is deferred to c_common_post_options ().  */
124 static void defer_opt (enum opt_code, const char *);
125 static struct deferred_opt
126 {
127   enum opt_code code;
128   const char *arg;
129 } *deferred_opts;
130
131 /* Complain that switch OPT_INDEX expects an argument but none was
132    provided.  */
133 static void
134 missing_arg (enum opt_code code)
135 {
136   const char *opt_text = cl_options[code].opt_text;
137
138   switch (code)
139     {
140     case OPT__output_pch_:
141     case OPT_Wformat_:
142     case OPT_d:
143     case OPT_fabi_version_:
144     case OPT_fbuiltin_:
145     case OPT_fdump_:
146     case OPT_fname_mangling_version_:
147     case OPT_ftabstop_:
148     case OPT_ftemplate_depth_:
149     case OPT_iprefix:
150     case OPT_iwithprefix:
151     case OPT_iwithprefixbefore:
152     default:
153       error ("missing argument to \"-%s\"", opt_text);
154       break;
155
156     case OPT_fconstant_string_class_:
157       error ("no class name specified with \"-%s\"", opt_text);
158       break;
159
160     case OPT_A:
161       error ("assertion missing after \"-%s\"", opt_text);
162       break;
163
164     case OPT_D:
165     case OPT_U:
166       error ("macro name missing after \"-%s\"", opt_text);
167       break;
168
169     case OPT_I:
170     case OPT_idirafter:
171     case OPT_isysroot:
172     case OPT_isystem:
173       error ("missing path after \"-%s\"", opt_text);
174       break;
175
176     case OPT_MF:
177     case OPT_MD:
178     case OPT_MMD:
179     case OPT_include:
180     case OPT_imacros:
181     case OPT_o:
182       error ("missing filename after \"-%s\"", opt_text);
183       break;
184
185     case OPT_MQ:
186     case OPT_MT:
187       error ("missing target after \"-%s\"", opt_text);
188       break;
189     }
190 }
191
192 /* Defer option CODE with argument ARG.  */
193 static void
194 defer_opt (enum opt_code code, const char *arg)
195 {
196   /* FIXME: this should be in c_common_init_options, which should take
197      argc and argv.  */
198   if (!deferred_opts)
199     {
200       extern int save_argc;
201       deferred_size = save_argc;
202       deferred_opts = (struct deferred_opt *)
203         xmalloc (deferred_size * sizeof (struct deferred_opt));
204     }
205
206   if (deferred_count == deferred_size)
207     abort ();
208
209   deferred_opts[deferred_count].code = code;
210   deferred_opts[deferred_count].arg = arg;
211   deferred_count++;
212 }
213
214 /* Common initialization before parsing options.  */
215 int
216 c_common_init_options (enum c_language_kind lang)
217 {
218   c_language = lang;
219   parse_in = cpp_create_reader (lang == clk_c ? CLK_GNUC89 : CLK_GNUCXX,
220                                 ident_hash);
221   cpp_opts = cpp_get_options (parse_in);
222   cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
223
224   /* Reset to avoid warnings on internal definitions.  We set it just
225      before passing on command-line options to cpplib.  */
226   cpp_opts->warn_dollars = 0;
227
228   if (flag_objc)
229     cpp_opts->objc = 1;
230
231   flag_const_strings = (lang == clk_cplusplus);
232   warn_pointer_arith = (lang == clk_cplusplus);
233
234   return lang_flags[(c_language << 1) + flag_objc];
235 }
236
237 /* Handle switch SCODE with argument ARG.  ON is true, unless no-
238    form of an -f or -W option was given.  Returns 0 if the switch was
239    invalid, a negative number to prevent language-independent
240    processing in toplev.c (a hack necessary for the short-term).  */
241 int
242 c_common_handle_option (size_t scode, const char *arg, int value)
243 {
244   const struct cl_option *option = &cl_options[scode];
245   enum opt_code code = (enum opt_code) scode;
246   int result = 1;
247
248   if (code == N_OPTS)
249     {
250       if (!in_fname)
251         in_fname = arg;
252       else if (!out_fname)
253         out_fname = arg;
254       else
255           error ("too many filenames given.  Type %s --help for usage",
256                  progname);
257       return 1;
258     }
259
260   if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
261     {
262       missing_arg (code);
263       return 1;
264     }
265
266   switch (code)
267     {
268     default:
269       return 0;
270
271     case OPT__help:
272       print_help ();
273       break;
274
275     case OPT__output_pch_:
276       pch_file = arg;
277       break;
278
279     case OPT_A:
280       defer_opt (code, arg);
281       break;
282
283     case OPT_C:
284       cpp_opts->discard_comments = 0;
285       break;
286
287     case OPT_CC:
288       cpp_opts->discard_comments = 0;
289       cpp_opts->discard_comments_in_macro_exp = 0;
290       break;
291
292     case OPT_D:
293       defer_opt (code, arg);
294       break;
295
296     case OPT_E:
297       flag_preprocess_only = 1;
298       break;
299
300     case OPT_H:
301       cpp_opts->print_include_names = 1;
302       break;
303
304     case OPT_I:
305       if (strcmp (arg, "-"))
306         add_path (xstrdup (arg), BRACKET, 0);
307       else
308         {
309           if (quote_chain_split)
310             error ("-I- specified twice");
311           quote_chain_split = true;
312           split_quote_chain ();
313         }
314       break;
315
316     case OPT_M:
317     case OPT_MM:
318       /* When doing dependencies with -M or -MM, suppress normal
319          preprocessed output, but still do -dM etc. as software
320          depends on this.  Preprocessed output does occur if -MD, -MMD
321          or environment var dependency generation is used.  */
322       cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
323       flag_no_output = 1;
324       cpp_opts->inhibit_warnings = 1;
325       break;
326
327     case OPT_MD:
328     case OPT_MMD:
329       cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
330       deps_file = arg;
331       break;
332
333     case OPT_MF:
334       deps_seen = true;
335       deps_file = arg;
336       break;
337
338     case OPT_MG:
339       deps_seen = true;
340       cpp_opts->deps.missing_files = true;
341       break;
342
343     case OPT_MP:
344       deps_seen = true;
345       cpp_opts->deps.phony_targets = true;
346       break;
347
348     case OPT_MQ:
349     case OPT_MT:
350       deps_seen = true;
351       defer_opt (code, arg);
352       break;
353
354     case OPT_P:
355       flag_no_line_commands = 1;
356       break;
357
358     case OPT_U:
359       defer_opt (code, arg);
360       break;
361
362     case OPT_Wabi:
363       warn_abi = value;
364       break;
365
366     case OPT_Wall:
367       set_Wunused (value);
368       set_Wformat (value);
369       set_Wimplicit (value);
370       warn_char_subscripts = value;
371       warn_missing_braces = value;
372       warn_parentheses = value;
373       warn_return_type = value;
374       warn_sequence_point = value;      /* Was C only.  */
375       if (c_language == clk_cplusplus)
376         warn_sign_compare = value;
377       warn_switch = value;
378       warn_strict_aliasing = value;
379
380       /* Only warn about unknown pragmas that are not in system
381          headers.  */
382       warn_unknown_pragmas = value;
383
384       /* We save the value of warn_uninitialized, since if they put
385          -Wuninitialized on the command line, we need to generate a
386          warning about not using it without also specifying -O.  */
387       if (warn_uninitialized != 1)
388         warn_uninitialized = (value ? 2 : 0);
389
390       if (c_language == clk_c)
391         /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
392            can turn it off only if it's not explicit.  */
393         warn_main = value * 2;
394       else
395         {
396           /* C++-specific warnings.  */
397           warn_nonvdtor = value;
398           warn_reorder = value;
399           warn_nontemplate_friend = value;
400         }
401
402       cpp_opts->warn_trigraphs = value;
403       cpp_opts->warn_comments = value;
404       cpp_opts->warn_num_sign_change = value;
405       cpp_opts->warn_multichar = value; /* Was C++ only.  */
406       break;
407
408     case OPT_Wbad_function_cast:
409       warn_bad_function_cast = value;
410       break;
411
412     case OPT_Wcast_qual:
413       warn_cast_qual = value;
414       break;
415
416     case OPT_Wchar_subscripts:
417       warn_char_subscripts = value;
418       break;
419
420     case OPT_Wcomment:
421     case OPT_Wcomments:
422       cpp_opts->warn_comments = value;
423       break;
424
425     case OPT_Wconversion:
426       warn_conversion = value;
427       break;
428
429     case OPT_Wctor_dtor_privacy:
430       warn_ctor_dtor_privacy = value;
431       break;
432
433     case OPT_Wdeprecated:
434       warn_deprecated = value;
435       cpp_opts->warn_deprecated = value;
436       break;
437
438     case OPT_Wdiv_by_zero:
439       warn_div_by_zero = value;
440       break;
441
442     case OPT_Weffc__:
443       warn_ecpp = value;
444       break;
445
446     case OPT_Wendif_labels:
447       cpp_opts->warn_endif_labels = value;
448       break;
449
450     case OPT_Werror:
451       cpp_opts->warnings_are_errors = value;
452       break;
453
454     case OPT_Werror_implicit_function_declaration:
455       mesg_implicit_function_declaration = 2;
456       break;
457
458     case OPT_Wfloat_equal:
459       warn_float_equal = value;
460       break;
461
462     case OPT_Wformat:
463       set_Wformat (value);
464       break;
465
466     case OPT_Wformat_:
467       set_Wformat (atoi (arg));
468       break;
469
470     case OPT_Wformat_extra_args:
471       warn_format_extra_args = value;
472       break;
473
474     case OPT_Wformat_nonliteral:
475       warn_format_nonliteral = value;
476       break;
477
478     case OPT_Wformat_security:
479       warn_format_security = value;
480       break;
481
482     case OPT_Wformat_y2k:
483       warn_format_y2k = value;
484       break;
485
486     case OPT_Wformat_zero_length:
487       warn_format_zero_length = value;
488       break;
489
490     case OPT_Wimplicit:
491       set_Wimplicit (value);
492       break;
493
494     case OPT_Wimplicit_function_declaration:
495       mesg_implicit_function_declaration = value;
496       break;
497
498     case OPT_Wimplicit_int:
499       warn_implicit_int = value;
500       break;
501
502     case OPT_Wimport:
503       cpp_opts->warn_import = value;
504       break;
505
506     case OPT_Winvalid_offsetof:
507       warn_invalid_offsetof = value;
508       break;
509
510     case OPT_Winvalid_pch:
511       cpp_opts->warn_invalid_pch = value;
512       break;
513
514     case OPT_Wlong_long:
515       warn_long_long = value;
516       break;
517
518     case OPT_Wmain:
519       if (value)
520         warn_main = 1;
521       else
522         warn_main = -1;
523       break;
524
525     case OPT_Wmissing_braces:
526       warn_missing_braces = value;
527       break;
528
529     case OPT_Wmissing_declarations:
530       warn_missing_declarations = value;
531       break;
532
533     case OPT_Wmissing_format_attribute:
534       warn_missing_format_attribute = value;
535       break;
536
537     case OPT_Wmissing_prototypes:
538       warn_missing_prototypes = value;
539       break;
540
541     case OPT_Wmultichar:
542       cpp_opts->warn_multichar = value;
543       break;
544
545     case OPT_Wnested_externs:
546       warn_nested_externs = value;
547       break;
548
549     case OPT_Wnon_template_friend:
550       warn_nontemplate_friend = value;
551       break;
552
553     case OPT_Wnon_virtual_dtor:
554       warn_nonvdtor = value;
555       break;
556
557     case OPT_Wnonnull:
558       warn_nonnull = value;
559       break;
560
561     case OPT_Wold_style_cast:
562       warn_old_style_cast = value;
563       break;
564
565     case OPT_Woverloaded_virtual:
566       warn_overloaded_virtual = value;
567       break;
568
569     case OPT_Wparentheses:
570       warn_parentheses = value;
571       break;
572
573     case OPT_Wpmf_conversions:
574       warn_pmf2ptr = value;
575       break;
576
577     case OPT_Wpointer_arith:
578       warn_pointer_arith = value;
579       break;
580
581     case OPT_Wprotocol:
582       warn_protocol = value;
583       break;
584
585     case OPT_Wselector:
586       warn_selector = value;
587       break;
588
589     case OPT_Wredundant_decls:
590       warn_redundant_decls = value;
591       break;
592
593     case OPT_Wreorder:
594       warn_reorder = value;
595       break;
596
597     case OPT_Wreturn_type:
598       warn_return_type = value;
599       break;
600
601     case OPT_Wsequence_point:
602       warn_sequence_point = value;
603       break;
604
605     case OPT_Wsign_compare:
606       warn_sign_compare = value;
607       break;
608
609     case OPT_Wsign_promo:
610       warn_sign_promo = value;
611       break;
612
613     case OPT_Wstrict_prototypes:
614       warn_strict_prototypes = value;
615       break;
616
617     case OPT_Wsynth:
618       warn_synth = value;
619       break;
620
621     case OPT_Wsystem_headers:
622       cpp_opts->warn_system_headers = value;
623       break;
624
625     case OPT_Wtraditional:
626       warn_traditional = value;
627       cpp_opts->warn_traditional = value;
628       break;
629
630     case OPT_Wtrigraphs:
631       cpp_opts->warn_trigraphs = value;
632       break;
633
634     case OPT_Wundeclared_selector:
635       warn_undeclared_selector = value;
636       break;
637
638     case OPT_Wundef:
639       cpp_opts->warn_undef = value;
640       break;
641
642     case OPT_Wunknown_pragmas:
643       /* Set to greater than 1, so that even unknown pragmas in
644          system headers will be warned about.  */
645       warn_unknown_pragmas = value * 2;
646       break;
647
648     case OPT_Wunused_macros:
649       warn_unused_macros = value;
650       break;
651
652     case OPT_Wwrite_strings:
653       if (c_language == clk_c)
654         flag_const_strings = value;
655       else
656         warn_write_strings = value;
657       break;
658
659     case OPT_ansi:
660       if (c_language == clk_c)
661         set_std_c89 (false, true);
662       else
663         set_std_cxx98 (true);
664       break;
665
666     case OPT_d:
667       handle_OPT_d (arg);
668       break;
669
670     case OPT_fcond_mismatch:
671       if (c_language == clk_c)
672         {
673           flag_cond_mismatch = value;
674           break;
675         }
676       /* Fall through.  */
677
678     case OPT_fall_virtual:
679     case OPT_fenum_int_equiv:
680     case OPT_fguiding_decls:
681     case OPT_fhonor_std:
682     case OPT_fhuge_objects:
683     case OPT_flabels_ok:
684     case OPT_fname_mangling_version_:
685     case OPT_fnew_abi:
686     case OPT_fnonnull_objects:
687     case OPT_fsquangle:
688     case OPT_fstrict_prototype:
689     case OPT_fthis_is_variable:
690     case OPT_fvtable_thunks:
691     case OPT_fxref:
692       warning ("switch \"%s\" is no longer supported", option->opt_text);
693       break;
694
695     case OPT_fabi_version_:
696       flag_abi_version = value;
697       break;
698
699     case OPT_faccess_control:
700       flag_access_control = value;
701       break;
702
703     case OPT_falt_external_templates:
704       flag_alt_external_templates = value;
705       if (value)
706         flag_external_templates = true;
707     cp_deprecated:
708       warning ("switch \"%s\" is deprecated, please see documentation for details", option->opt_text);
709       break;
710
711     case OPT_fasm:
712       flag_no_asm = !value;
713       break;
714
715     case OPT_fbuiltin:
716       flag_no_builtin = !value;
717       break;
718
719     case OPT_fbuiltin_:
720       if (value)
721         result = 0;
722       else
723         disable_builtin_function (arg);
724       break;
725
726     case OPT_fdollars_in_identifiers:
727       cpp_opts->dollars_in_ident = value;
728       break;
729
730     case OPT_fdump_:
731       if (!dump_switch_p (arg))
732         result = 0;
733       break;
734
735     case OPT_ffreestanding:
736       value = !value;
737       /* Fall through...  */
738     case OPT_fhosted:
739       flag_hosted = value;
740       flag_no_builtin = !value;
741       /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
742       if (!value && warn_main == 2)
743         warn_main = 0;
744       break;
745
746     case OPT_fshort_double:
747       flag_short_double = value;
748       break;
749
750     case OPT_fshort_enums:
751       flag_short_enums = value;
752       break;
753
754     case OPT_fshort_wchar:
755       flag_short_wchar = value;
756       break;
757
758     case OPT_fsigned_bitfields:
759       flag_signed_bitfields = value;
760       explicit_flag_signed_bitfields = 1;
761       break;
762
763     case OPT_fsigned_char:
764       flag_signed_char = value;
765       break;
766
767     case OPT_funsigned_bitfields:
768       flag_signed_bitfields = !value;
769       explicit_flag_signed_bitfields = 1;
770       break;
771
772     case OPT_funsigned_char:
773       flag_signed_char = !value;
774       break;
775
776     case OPT_fcheck_new:
777       flag_check_new = value;
778       break;
779
780     case OPT_fconserve_space:
781       flag_conserve_space = value;
782       break;
783
784     case OPT_fconst_strings:
785       flag_const_strings = value;
786       break;
787
788     case OPT_fconstant_string_class_:
789       constant_string_class_name = arg;
790       break;
791
792     case OPT_fdefault_inline:
793       flag_default_inline = value;
794       break;
795
796     case OPT_felide_constructors:
797       flag_elide_constructors = value;
798       break;
799
800     case OPT_fenforce_eh_specs:
801       flag_enforce_eh_specs = value;
802       break;
803
804     case OPT_fexternal_templates:
805       flag_external_templates = value;
806       goto cp_deprecated;
807
808     case OPT_ffixed_form:
809     case OPT_ffixed_line_length_:
810       /* Fortran front end options ignored when preprocessing only.  */
811       if (!flag_preprocess_only)
812         result = 0;
813       break;
814
815     case OPT_ffor_scope:
816       flag_new_for_scope = value;
817       break;
818
819     case OPT_fgnu_keywords:
820       flag_no_gnu_keywords = !value;
821       break;
822
823     case OPT_fgnu_runtime:
824       flag_next_runtime = !value;
825       break;
826
827     case OPT_fhandle_exceptions:
828       warning ("-fhandle-exceptions has been renamed -fexceptions (and is now on by default)");
829       flag_exceptions = value;
830       break;
831
832     case OPT_fimplement_inlines:
833       flag_implement_inlines = value;
834       break;
835
836     case OPT_fimplicit_inline_templates:
837       flag_implicit_inline_templates = value;
838       break;
839
840     case OPT_fimplicit_templates:
841       flag_implicit_templates = value;
842       break;
843
844     case OPT_fms_extensions:
845       flag_ms_extensions = value;
846       break;
847
848     case OPT_fnext_runtime:
849       flag_next_runtime = value;
850       break;
851
852     case OPT_fnonansi_builtins:
853       flag_no_nonansi_builtin = !value;
854       break;
855
856     case OPT_foperator_names:
857       cpp_opts->operator_names = value;
858       break;
859
860     case OPT_foptional_diags:
861       flag_optional_diags = value;
862       break;
863
864     case OPT_fpch_deps:
865       cpp_opts->restore_pch_deps = value;
866       break;
867
868     case OPT_fpermissive:
869       flag_permissive = value;
870       break;
871
872     case OPT_fpreprocessed:
873       cpp_opts->preprocessed = value;
874       break;
875
876     case OPT_frepo:
877       flag_use_repository = value;
878       if (value)
879         flag_implicit_templates = 0;
880       break;
881
882     case OPT_frtti:
883       flag_rtti = value;
884       break;
885
886     case OPT_fshow_column:
887       cpp_opts->show_column = value;
888       break;
889
890     case OPT_fstats:
891       flag_detailed_statistics = value;
892       break;
893
894     case OPT_ftabstop_:
895       /* It is documented that we silently ignore silly values.  */
896       if (value >= 1 && value <= 100)
897         cpp_opts->tabstop = value;
898       break;
899
900     case OPT_ftemplate_depth_:
901       max_tinst_depth = value;
902       break;
903
904     case OPT_fvtable_gc:
905       flag_vtable_gc = value;
906       break;
907
908     case OPT_fuse_cxa_atexit:
909       flag_use_cxa_atexit = value;
910       break;
911
912     case OPT_fweak:
913       flag_weak = value;
914       break;
915
916     case OPT_gen_decls:
917       flag_gen_declaration = 1;
918       break;
919
920     case OPT_idirafter:
921       add_path (xstrdup (arg), AFTER, 0);
922       break;
923
924     case OPT_imacros:
925     case OPT_include:
926       defer_opt (code, arg);
927       break;
928
929     case OPT_iprefix:
930       iprefix = arg;
931       break;
932
933     case OPT_isysroot:
934       sysroot = arg;
935       break;
936
937     case OPT_isystem:
938       add_path (xstrdup (arg), SYSTEM, 0);
939       break;
940
941     case OPT_iwithprefix:
942       add_prefixed_path (arg, SYSTEM);
943       break;
944
945     case OPT_iwithprefixbefore:
946       add_prefixed_path (arg, BRACKET);
947       break;
948
949     case OPT_lang_asm:
950       cpp_set_lang (parse_in, CLK_ASM);
951       cpp_opts->dollars_in_ident = false;
952       break;
953
954     case OPT_lang_objc:
955       cpp_opts->objc = 1;
956       break;
957
958     case OPT_nostdinc:
959       std_inc = false;
960       break;
961
962     case OPT_nostdinc__:
963       std_cxx_inc = false;
964       break;
965
966     case OPT_o:
967       if (!out_fname)
968         out_fname = arg;
969       else
970         error ("output filename specified twice");
971       break;
972
973       /* We need to handle the -pedantic switches here, rather than in
974          c_common_post_options, so that a subsequent -Wno-endif-labels
975          is not overridden.  */
976     case OPT_pedantic_errors:
977       cpp_opts->pedantic_errors = 1;
978       /* fall through */
979     case OPT_pedantic:
980       cpp_opts->pedantic = 1;
981       cpp_opts->warn_endif_labels = 1;
982       break;
983
984     case OPT_print_objc_runtime_info:
985       print_struct_values = 1;
986       break;
987
988     case OPT_remap:
989       cpp_opts->remap = 1;
990       break;
991
992     case OPT_std_c__98:
993     case OPT_std_gnu__98:
994       set_std_cxx98 (code == OPT_std_c__98 /* ISO */);
995       break;
996
997     case OPT_std_c89:
998     case OPT_std_iso9899_1990:
999     case OPT_std_iso9899_199409:
1000       set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1001       break;
1002
1003     case OPT_std_gnu89:
1004       set_std_c89 (false /* c94 */, false /* ISO */);
1005       break;
1006
1007     case OPT_std_c99:
1008     case OPT_std_c9x:
1009     case OPT_std_iso9899_1999:
1010     case OPT_std_iso9899_199x:
1011       set_std_c99 (true /* ISO */);
1012       break;
1013
1014     case OPT_std_gnu99:
1015     case OPT_std_gnu9x:
1016       set_std_c99 (false /* ISO */);
1017       break;
1018
1019     case OPT_trigraphs:
1020       cpp_opts->trigraphs = 1;
1021       break;
1022
1023     case OPT_traditional_cpp:
1024       cpp_opts->traditional = 1;
1025       break;
1026
1027     case OPT_undef:
1028       flag_undef = 1;
1029       break;
1030
1031     case OPT_w:
1032       cpp_opts->inhibit_warnings = 1;
1033       break;
1034
1035     case OPT_v:
1036       verbose = true;
1037       break;
1038     }
1039
1040   return result;
1041 }
1042
1043 /* Post-switch processing.  */
1044 bool
1045 c_common_post_options (const char **pfilename)
1046 {
1047   /* Canonicalize the input and output filenames.  */
1048   if (in_fname == NULL || !strcmp (in_fname, "-"))
1049     in_fname = "";
1050
1051   if (out_fname == NULL || !strcmp (out_fname, "-"))
1052     out_fname = "";
1053
1054   if (cpp_opts->deps.style == DEPS_NONE)
1055     check_deps_environment_vars ();
1056
1057   handle_deferred_opts ();
1058
1059   sanitize_cpp_opts ();
1060
1061   register_include_chains (parse_in, sysroot, iprefix,
1062                            std_inc, std_cxx_inc && c_language == clk_cplusplus,
1063                            verbose);
1064
1065   flag_inline_trees = 1;
1066
1067   /* Use tree inlining if possible.  Function instrumentation is only
1068      done in the RTL level, so we disable tree inlining.  */
1069   if (! flag_instrument_function_entry_exit)
1070     {
1071       if (!flag_no_inline)
1072         flag_no_inline = 1;
1073       if (flag_inline_functions)
1074         {
1075           flag_inline_trees = 2;
1076           flag_inline_functions = 0;
1077         }
1078     }
1079
1080   /* -Wextra implies -Wsign-compare, but not if explicitly
1081       overridden.  */
1082   if (warn_sign_compare == -1)
1083     warn_sign_compare = extra_warnings;
1084
1085   /* Special format checking options don't work without -Wformat; warn if
1086      they are used.  */
1087   if (warn_format_y2k && !warn_format)
1088     warning ("-Wformat-y2k ignored without -Wformat");
1089   if (warn_format_extra_args && !warn_format)
1090     warning ("-Wformat-extra-args ignored without -Wformat");
1091   if (warn_format_zero_length && !warn_format)
1092     warning ("-Wformat-zero-length ignored without -Wformat");
1093   if (warn_format_nonliteral && !warn_format)
1094     warning ("-Wformat-nonliteral ignored without -Wformat");
1095   if (warn_format_security && !warn_format)
1096     warning ("-Wformat-security ignored without -Wformat");
1097   if (warn_missing_format_attribute && !warn_format)
1098     warning ("-Wmissing-format-attribute ignored without -Wformat");
1099
1100   if (flag_preprocess_only)
1101     {
1102       /* Open the output now.  We must do so even if flag_no_output is
1103          on, because there may be other output than from the actual
1104          preprocessing (e.g. from -dM).  */
1105       if (out_fname[0] == '\0')
1106         out_stream = stdout;
1107       else
1108         out_stream = fopen (out_fname, "w");
1109
1110       if (out_stream == NULL)
1111         {
1112           fatal_error ("opening output file %s: %m", out_fname);
1113           return false;
1114         }
1115
1116       init_pp_output (out_stream);
1117     }
1118   else
1119     {
1120       init_c_lex ();
1121
1122       /* Yuk.  WTF is this?  I do know ObjC relies on it somewhere.  */
1123       input_line = 0;
1124     }
1125
1126   cpp_get_callbacks (parse_in)->file_change = cb_file_change;
1127
1128   /* NOTE: we use in_fname here, not the one supplied.  */
1129   *pfilename = cpp_read_main_file (parse_in, in_fname);
1130
1131   saved_lineno = input_line;
1132   input_line = 0;
1133
1134   /* If an error has occurred in cpplib, note it so we fail
1135      immediately.  */
1136   errorcount += cpp_errors (parse_in);
1137
1138   return flag_preprocess_only;
1139 }
1140
1141 /* Front end initialization common to C, ObjC and C++.  */
1142 bool
1143 c_common_init (void)
1144 {
1145   input_line = saved_lineno;
1146
1147   /* Set up preprocessor arithmetic.  Must be done after call to
1148      c_common_nodes_and_builtins for type nodes to be good.  */
1149   cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1150   cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1151   cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1152   cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1153   cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1154   cpp_opts->EBCDIC = TARGET_EBCDIC;
1155
1156   if (flag_preprocess_only)
1157     {
1158       finish_options ();
1159       preprocess_file (parse_in);
1160       return false;
1161     }
1162
1163   /* Has to wait until now so that cpplib has its hash table.  */
1164   init_pragma ();
1165
1166   return true;
1167 }
1168
1169 /* A thin wrapper around the real parser that initializes the
1170    integrated preprocessor after debug output has been initialized.
1171    Also, make sure the start_source_file debug hook gets called for
1172    the primary source file.  */
1173 void
1174 c_common_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
1175 {
1176 #if YYDEBUG != 0
1177   yydebug = set_yydebug;
1178 #else
1179   warning ("YYDEBUG not defined");
1180 #endif
1181
1182   (*debug_hooks->start_source_file) (input_line, input_filename);
1183   finish_options();
1184   pch_init();
1185   yyparse ();
1186   free_parser_stacks ();
1187 }
1188
1189 /* Common finish hook for the C, ObjC and C++ front ends.  */
1190 void
1191 c_common_finish (void)
1192 {
1193   FILE *deps_stream = NULL;
1194
1195   if (cpp_opts->deps.style != DEPS_NONE)
1196     {
1197       /* If -M or -MM was seen without -MF, default output to the
1198          output stream.  */
1199       if (!deps_file)
1200         deps_stream = out_stream;
1201       else
1202         {
1203           deps_stream = fopen (deps_file, deps_append ? "a": "w");
1204           if (!deps_stream)
1205             fatal_error ("opening dependency file %s: %m", deps_file);
1206         }
1207     }
1208
1209   /* For performance, avoid tearing down cpplib's internal structures
1210      with cpp_destroy ().  */
1211   errorcount += cpp_finish (parse_in, deps_stream);
1212
1213   if (deps_stream && deps_stream != out_stream
1214       && (ferror (deps_stream) || fclose (deps_stream)))
1215     fatal_error ("closing dependency file %s: %m", deps_file);
1216
1217   if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1218     fatal_error ("when writing output to %s: %m", out_fname);
1219 }
1220
1221 /* Either of two environment variables can specify output of
1222    dependencies.  Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1223    DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1224    and DEPS_TARGET is the target to mention in the deps.  They also
1225    result in dependency information being appended to the output file
1226    rather than overwriting it, and like Sun's compiler
1227    SUNPRO_DEPENDENCIES suppresses the dependency on the main file.  */
1228 static void
1229 check_deps_environment_vars (void)
1230 {
1231   char *spec;
1232
1233   GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1234   if (spec)
1235     cpp_opts->deps.style = DEPS_USER;
1236   else
1237     {
1238       GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1239       if (spec)
1240         {
1241           cpp_opts->deps.style = DEPS_SYSTEM;
1242           cpp_opts->deps.ignore_main_file = true;
1243         }
1244     }
1245
1246   if (spec)
1247     {
1248       /* Find the space before the DEPS_TARGET, if there is one.  */
1249       char *s = strchr (spec, ' ');
1250       if (s)
1251         {
1252           /* Let the caller perform MAKE quoting.  */
1253           defer_opt (OPT_MT, s + 1);
1254           *s = '\0';
1255         }
1256
1257       /* Command line -MF overrides environment variables and default.  */
1258       if (!deps_file)
1259         deps_file = spec;
1260
1261       deps_append = 1;
1262     }
1263 }
1264
1265 /* Handle deferred command line switches.  */
1266 static void
1267 handle_deferred_opts (void)
1268 {
1269   size_t i;
1270
1271   for (i = 0; i < deferred_count; i++)
1272     {
1273       struct deferred_opt *opt = &deferred_opts[i];
1274
1275       if (opt->code == OPT_MT || opt->code == OPT_MQ)
1276         cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1277     }
1278 }
1279
1280 /* These settings are appropriate for GCC, but not necessarily so for
1281    cpplib as a library.  */
1282 static void
1283 sanitize_cpp_opts (void)
1284 {
1285   /* If we don't know what style of dependencies to output, complain
1286      if any other dependency switches have been given.  */
1287   if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1288     error ("to generate dependencies you must specify either -M or -MM");
1289
1290   /* -dM and dependencies suppress normal output; do it here so that
1291      the last -d[MDN] switch overrides earlier ones.  */
1292   if (flag_dump_macros == 'M')
1293     flag_no_output = 1;
1294
1295   /* Disable -dD, -dN and -dI if normal output is suppressed.  Allow
1296      -dM since at least glibc relies on -M -dM to work.  */
1297   if (flag_no_output)
1298     {
1299       if (flag_dump_macros != 'M')
1300         flag_dump_macros = 0;
1301       flag_dump_includes = 0;
1302     }
1303
1304   cpp_opts->unsigned_char = !flag_signed_char;
1305   cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1306
1307   /* We want -Wno-long-long to override -pedantic -std=non-c99
1308      and/or -Wtraditional, whatever the ordering.  */
1309   cpp_opts->warn_long_long
1310     = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1311 }
1312
1313 /* Add include path with a prefix at the front of its name.  */
1314 static void
1315 add_prefixed_path (const char *suffix, size_t chain)
1316 {
1317   char *path;
1318   const char *prefix;
1319   size_t prefix_len, suffix_len;
1320
1321   suffix_len = strlen (suffix);
1322   prefix     = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1323   prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1324
1325   path = xmalloc (prefix_len + suffix_len + 1);
1326   memcpy (path, prefix, prefix_len);
1327   memcpy (path + prefix_len, suffix, suffix_len);
1328   path[prefix_len + suffix_len] = '\0';
1329
1330   add_path (path, chain, 0);
1331 }
1332
1333 /* Handle -D, -U, -A, -imacros, and the first -include.  */
1334 static void
1335 finish_options (void)
1336 {
1337   if (!cpp_opts->preprocessed)
1338     {
1339       size_t i;
1340
1341       cpp_change_file (parse_in, LC_RENAME, _("<built-in>"));
1342       cpp_init_builtins (parse_in, flag_hosted);
1343       c_cpp_builtins (parse_in);
1344
1345       /* We're about to send user input to cpplib, so make it warn for
1346          things that we previously (when we sent it internal definitions)
1347          told it to not warn.
1348
1349          C99 permits implementation-defined characters in identifiers.
1350          The documented meaning of -std= is to turn off extensions that
1351          conflict with the specified standard, and since a strictly
1352          conforming program cannot contain a '$', we do not condition
1353          their acceptance on the -std= setting.  */
1354       cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99);
1355
1356       cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
1357       for (i = 0; i < deferred_count; i++)
1358         {
1359           struct deferred_opt *opt = &deferred_opts[i];
1360
1361           if (opt->code == OPT_D)
1362             cpp_define (parse_in, opt->arg);
1363           else if (opt->code == OPT_U)
1364             cpp_undef (parse_in, opt->arg);
1365           else if (opt->code == OPT_A)
1366             {
1367               if (opt->arg[0] == '-')
1368                 cpp_unassert (parse_in, opt->arg + 1);
1369               else
1370                 cpp_assert (parse_in, opt->arg);
1371             }
1372         }
1373
1374       /* Handle -imacros after -D and -U.  */
1375       for (i = 0; i < deferred_count; i++)
1376         {
1377           struct deferred_opt *opt = &deferred_opts[i];
1378
1379           if (opt->code == OPT_imacros
1380               && cpp_push_include (parse_in, opt->arg))
1381             cpp_scan_nooutput (parse_in);
1382         }
1383     }
1384
1385   push_command_line_include ();
1386 }
1387
1388 /* Give CPP the next file given by -include, if any.  */
1389 static void
1390 push_command_line_include (void)
1391 {
1392   if (cpp_opts->preprocessed)
1393     return;
1394
1395   while (include_cursor < deferred_count)
1396     {
1397       struct deferred_opt *opt = &deferred_opts[include_cursor++];
1398
1399       if (opt->code == OPT_include && cpp_push_include (parse_in, opt->arg))
1400         return;
1401     }
1402
1403   if (include_cursor == deferred_count)
1404     {
1405       /* Restore the line map from <command line>.  */
1406       cpp_change_file (parse_in, LC_RENAME, main_input_filename);
1407       /* -Wunused-macros should only warn about macros defined hereafter.  */
1408       cpp_opts->warn_unused_macros = warn_unused_macros;
1409       include_cursor++;
1410     }
1411 }
1412
1413 /* File change callback.  Has to handle -include files.  */
1414 static void
1415 cb_file_change (cpp_reader *pfile ATTRIBUTE_UNUSED,
1416                 const struct line_map *new_map)
1417 {
1418   if (flag_preprocess_only)
1419     pp_file_change (new_map);
1420   else
1421     fe_file_change (new_map);
1422
1423   if (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map))
1424     push_command_line_include ();
1425 }
1426
1427 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1428    extensions if ISO).  There is no concept of gnu94.  */
1429 static void
1430 set_std_c89 (int c94, int iso)
1431 {
1432   cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1433   flag_iso = iso;
1434   flag_no_asm = iso;
1435   flag_no_gnu_keywords = iso;
1436   flag_no_nonansi_builtin = iso;
1437   flag_noniso_default_format_attributes = !iso;
1438   flag_isoc94 = c94;
1439   flag_isoc99 = 0;
1440   flag_writable_strings = 0;
1441 }
1442
1443 /* Set the C 99 standard (without GNU extensions if ISO).  */
1444 static void
1445 set_std_c99 (int iso)
1446 {
1447   cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1448   flag_no_asm = iso;
1449   flag_no_nonansi_builtin = iso;
1450   flag_noniso_default_format_attributes = !iso;
1451   flag_iso = iso;
1452   flag_isoc99 = 1;
1453   flag_isoc94 = 1;
1454   flag_writable_strings = 0;
1455 }
1456
1457 /* Set the C++ 98 standard (without GNU extensions if ISO).  */
1458 static void
1459 set_std_cxx98 (int iso)
1460 {
1461   cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1462   flag_no_gnu_keywords = iso;
1463   flag_no_nonansi_builtin = iso;
1464   flag_noniso_default_format_attributes = !iso;
1465   flag_iso = iso;
1466 }
1467
1468 /* Handle setting implicit to ON.  */
1469 static void
1470 set_Wimplicit (int on)
1471 {
1472   warn_implicit = on;
1473   warn_implicit_int = on;
1474   if (on)
1475     {
1476       if (mesg_implicit_function_declaration != 2)
1477         mesg_implicit_function_declaration = 1;
1478     }
1479   else
1480     mesg_implicit_function_declaration = 0;
1481 }
1482
1483 /* Args to -d specify what to dump.  Silently ignore
1484    unrecognized options; they may be aimed at toplev.c.  */
1485 static void
1486 handle_OPT_d (const char *arg)
1487 {
1488   char c;
1489
1490   while ((c = *arg++) != '\0')
1491     switch (c)
1492       {
1493       case 'M':                 /* Dump macros only.  */
1494       case 'N':                 /* Dump names.  */
1495       case 'D':                 /* Dump definitions.  */
1496         flag_dump_macros = c;
1497         break;
1498
1499       case 'I':
1500         flag_dump_includes = 1;
1501         break;
1502       }
1503 }
1504
1505 /* Handle --help output.  */
1506 static void
1507 print_help (void)
1508 {
1509   /* To keep the lines from getting too long for some compilers, limit
1510      to about 500 characters (6 lines) per chunk.  */
1511   fputs (_("\
1512 Switches:\n\
1513   -include <file>           Include the contents of <file> before other files\n\
1514   -imacros <file>           Accept definition of macros in <file>\n\
1515   -iprefix <path>           Specify <path> as a prefix for next two options\n\
1516   -iwithprefix <dir>        Add <dir> to the end of the system include path\n\
1517   -iwithprefixbefore <dir>  Add <dir> to the end of the main include path\n\
1518   -isystem <dir>            Add <dir> to the start of the system include path\n\
1519 "), stdout);
1520   fputs (_("\
1521   -idirafter <dir>          Add <dir> to the end of the system include path\n\
1522   -I <dir>                  Add <dir> to the end of the main include path\n\
1523   -I-                       Fine-grained include path control; see info docs\n\
1524   -nostdinc                 Do not search system include directories\n\
1525                              (dirs specified with -isystem will still be used)\n\
1526   -nostdinc++               Do not search system include directories for C++\n\
1527   -o <file>                 Put output into <file>\n\
1528 "), stdout);
1529   fputs (_("\
1530   -trigraphs                Support ISO C trigraphs\n\
1531   -std=<std name>           Specify the conformance standard; one of:\n\
1532                             gnu89, gnu99, c89, c99, iso9899:1990,\n\
1533                             iso9899:199409, iso9899:1999, c++98\n\
1534   -w                        Inhibit warning messages\n\
1535   -W[no-]trigraphs          Warn if trigraphs are encountered\n\
1536   -W[no-]comment{s}         Warn if one comment starts inside another\n\
1537 "), stdout);
1538   fputs (_("\
1539   -W[no-]traditional        Warn about features not present in traditional C\n\
1540   -W[no-]undef              Warn if an undefined macro is used by #if\n\
1541   -W[no-]import             Warn about the use of the #import directive\n\
1542 "), stdout);
1543   fputs (_("\
1544   -W[no-]error              Treat all warnings as errors\n\
1545   -W[no-]system-headers     Do not suppress warnings from system headers\n\
1546   -W[no-]all                Enable most preprocessor warnings\n\
1547 "), stdout);
1548   fputs (_("\
1549   -M                        Generate make dependencies\n\
1550   -MM                       As -M, but ignore system header files\n\
1551   -MD                       Generate make dependencies and compile\n\
1552   -MMD                      As -MD, but ignore system header files\n\
1553   -MF <file>                Write dependency output to the given file\n\
1554   -MG                       Treat missing header file as generated files\n\
1555 "), stdout);
1556   fputs (_("\
1557   -MP                       Generate phony targets for all headers\n\
1558   -MQ <target>              Add a MAKE-quoted target\n\
1559   -MT <target>              Add an unquoted target\n\
1560 "), stdout);
1561   fputs (_("\
1562   -D<macro>                 Define a <macro> with string '1' as its value\n\
1563   -D<macro>=<val>           Define a <macro> with <val> as its value\n\
1564   -A<question>=<answer>     Assert the <answer> to <question>\n\
1565   -A-<question>=<answer>    Disable the <answer> to <question>\n\
1566   -U<macro>                 Undefine <macro> \n\
1567   -v                        Display the version number\n\
1568 "), stdout);
1569   fputs (_("\
1570   -H                        Print the name of header files as they are used\n\
1571   -C                        Do not discard comments\n\
1572   -dM                       Display a list of macro definitions active at end\n\
1573   -dD                       Preserve macro definitions in output\n\
1574   -dN                       As -dD except that only the names are preserved\n\
1575   -dI                       Include #include directives in the output\n\
1576 "), stdout);
1577   fputs (_("\
1578   -f[no-]preprocessed       Treat the input file as already preprocessed\n\
1579   -ftabstop=<number>        Distance between tab stops for column reporting\n\
1580   -isysroot <dir>           Set <dir> to be the system root directory\n\
1581   -P                        Do not generate #line directives\n\
1582   -remap                    Remap file names when including files\n\
1583   --help                    Display this information\n\
1584 "), stdout);
1585 }