* support/cpp2/libcpp/directives.c, support/cpp2/libcpp/identifiers.c,
[fw/sdcc] / support / cpp2 / sdcpp-opts.c
1 /* C/ObjC/C++ command line option handling.
2    Copyright (C) 2002, 2003, 2004, 2005, 2006 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "diagnostic.h"
25 #include "intl.h"
26 #include "cppdefault.h"
27 #include "c-incpath.h"
28 #include "opts.h"
29 #include "options.h"
30 #include "mkdeps.h"
31
32 #ifndef DOLLARS_IN_IDENTIFIERS
33 # define DOLLARS_IN_IDENTIFIERS true
34 #endif
35
36 #ifndef TARGET_SYSTEM_ROOT
37 # define TARGET_SYSTEM_ROOT NULL
38 #endif
39
40 static int saved_lineno;
41
42 /* CPP's options.  */
43 static cpp_options *cpp_opts;
44
45 /* Input filename.  */
46 static const char *this_input_filename;
47
48 /* Filename and stream for preprocessed output.  */
49 static const char *out_fname;
50 static FILE *out_stream;
51
52 /* Append dependencies to deps_file.  */
53 static bool deps_append;
54
55 /* If dependency switches (-MF etc.) have been given.  */
56 static bool deps_seen;
57
58 /* If -v seen.  */
59 static bool verbose;
60
61 /* Dependency output file.  */
62 static const char *deps_file;
63
64 /* The prefix given by -iprefix, if any.  */
65 static const char *iprefix;
66
67 /* The system root, if any.  Overridden by -isysroot.  */
68 static const char *sysroot = TARGET_SYSTEM_ROOT;
69
70 /* Zero disables all standard directories for headers.  */
71 static bool std_inc = true;
72
73 /* If the quote chain has been split by -I-.  */
74 static bool quote_chain_split;
75
76 /* If -Wunused-macros.  */
77 static bool warn_unused_macros;
78
79 /* If -Wvariadic-macros.  */
80 static bool warn_variadic_macros = true;
81
82 /* Number of deferred options.  */
83 static size_t deferred_count;
84
85 /* Number of deferred options scanned for -include.  */
86 static size_t include_cursor;
87
88 static void handle_OPT_d (const char *);
89 static void set_std_c89 (int, int);
90 static void set_std_c99 (int);
91 static void check_deps_environment_vars (void);
92 static void handle_deferred_opts (void);
93 static void sanitize_cpp_opts (void);
94 static void add_prefixed_path (const char *, size_t);
95 static void push_command_line_include (void);
96 static void cb_file_change (cpp_reader *, const struct line_map *);
97 static void cb_dir_change (cpp_reader *, const char *);
98 static void finish_options (void);
99
100 #ifndef STDC_0_IN_SYSTEM_HEADERS
101 #define STDC_0_IN_SYSTEM_HEADERS 0
102 #endif
103
104 /* Holds switches parsed by sdcpp_common_handle_option (), but whose
105    handling is deferred to sdcpp_common_post_options ().  */
106 static void defer_opt (enum opt_code, const char *);
107 static struct deferred_opt
108 {
109   enum opt_code code;
110   const char *arg;
111 } *deferred_opts;
112
113 /* Complain that switch CODE expects an argument but none was
114    provided.  OPT was the command-line option.  Return FALSE to get
115    the default message in opts.c, TRUE if we provide a specialized
116    one.  */
117 bool
118 sdcpp_common_missing_argument (const char *opt, size_t code)
119 {
120   switch (code)
121     {
122     default:
123       /* Pick up the default message.  */
124       return false;
125
126     case OPT_A:
127       error ("assertion missing after \"%s\"", opt);
128       break;
129
130     case OPT_D:
131     case OPT_U:
132       error ("macro name missing after \"%s\"", opt);
133       break;
134
135     case OPT_I:
136     case OPT_idirafter:
137     case OPT_isysroot:
138     case OPT_isystem:
139     case OPT_iquote:
140       error ("missing path after \"%s\"", opt);
141       break;
142
143     case OPT_MF:
144     case OPT_MD:
145     case OPT_MMD:
146     case OPT_include:
147     case OPT_imacros:
148     case OPT_o:
149       error ("missing filename after \"%s\"", opt);
150       break;
151
152     case OPT_MQ:
153     case OPT_MT:
154       error ("missing makefile target after \"%s\"", opt);
155       break;
156     }
157
158   return true;
159 }
160
161 /* Defer option CODE with argument ARG.  */
162 static void
163 defer_opt (enum opt_code code, const char *arg)
164 {
165   deferred_opts[deferred_count].code = code;
166   deferred_opts[deferred_count].arg = arg;
167   deferred_count++;
168 }
169
170 /* Common initialization before parsing options.  */
171 unsigned int
172 sdcpp_common_init_options (unsigned int argc, const char **argv ATTRIBUTE_UNUSED)
173 {
174   parse_in = cpp_create_reader (CLK_GNUC89, NULL, &line_table);
175
176   cpp_opts = cpp_get_options (parse_in);
177   cpp_opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
178   cpp_opts->objc = 0;
179
180   /* Reset to avoid warnings on internal definitions.  We set it just
181      before passing on command-line options to cpplib.  */
182   cpp_opts->warn_dollars = 0;
183
184   deferred_opts = XNEWVEC (struct deferred_opt, argc);
185
186   return CL_SDCPP;
187 }
188
189 /* Handle switch SCODE with argument ARG.  VALUE is true, unless no-
190    form of an -f or -W option was given.  Returns 0 if the switch was
191    invalid, a negative number to prevent language-independent
192    processing in toplev.c (a hack necessary for the short-term).  */
193 int
194 sdcpp_common_handle_option (size_t scode, const char *arg, int value)
195 {
196   const struct cl_option *option = &cl_options[scode];
197   enum opt_code code = (enum opt_code) scode;
198   int result = 1;
199
200   switch (code)
201     {
202     default:
203       result = 0;
204       break;
205
206 #if 0 // pch not supported by sdcpp
207     case OPT__output_pch_:
208       pch_file = arg;
209       break;
210 #endif
211
212     case OPT_A:
213       defer_opt (code, arg);
214       break;
215
216     case OPT_C:
217       cpp_opts->discard_comments = 0;
218       break;
219
220     case OPT_CC:
221       cpp_opts->discard_comments = 0;
222       cpp_opts->discard_comments_in_macro_exp = 0;
223       break;
224
225     case OPT_D:
226       defer_opt (code, arg);
227       break;
228
229     case OPT_H:
230       cpp_opts->print_include_names = 1;
231       break;
232
233     case OPT_I:
234       if (strcmp (arg, "-"))
235         add_path (xstrdup (arg), BRACKET, 0, true);
236       else
237         {
238           if (quote_chain_split)
239             error ("-I- specified twice");
240           quote_chain_split = true;
241           split_quote_chain ();
242           inform ("obsolete option -I- used, please use -iquote instead");
243         }
244       break;
245
246     case OPT_M:
247     case OPT_MM:
248       /* When doing dependencies with -M or -MM, suppress normal
249          preprocessed output, but still do -dM etc. as software
250          depends on this.  Preprocessed output does occur if -MD, -MMD
251          or environment var dependency generation is used.  */
252       cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
253       flag_no_output = 1;
254       cpp_opts->inhibit_warnings = 1;
255       break;
256
257     case OPT_MD:
258     case OPT_MMD:
259       cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
260       deps_file = arg;
261       break;
262
263     case OPT_MF:
264       deps_seen = true;
265       deps_file = arg;
266       break;
267
268     case OPT_MG:
269       deps_seen = true;
270       cpp_opts->deps.missing_files = true;
271       break;
272
273     case OPT_MP:
274       deps_seen = true;
275       cpp_opts->deps.phony_targets = true;
276       break;
277
278     case OPT_MQ:
279     case OPT_MT:
280       deps_seen = true;
281       defer_opt (code, arg);
282       break;
283
284     case OPT_P:
285       flag_no_line_commands = 1;
286       break;
287
288     case OPT_fworking_directory:
289       flag_working_directory = value;
290       break;
291
292     case OPT_U:
293       defer_opt (code, arg);
294       break;
295
296     case OPT_Wall:
297       cpp_opts->warn_trigraphs = value;
298       cpp_opts->warn_comments = value;
299       cpp_opts->warn_num_sign_change = value;
300       cpp_opts->warn_multichar = value; /* Was C++ only.  */
301       break;
302
303     case OPT_Wcomment:
304     case OPT_Wcomments:
305       cpp_opts->warn_comments = value;
306       break;
307
308     case OPT_Wdeprecated:
309       cpp_opts->warn_deprecated = value;
310       break;
311
312     case OPT_Wendif_labels:
313       cpp_opts->warn_endif_labels = value;
314       break;
315
316     case OPT_Werror:
317       cpp_opts->warnings_are_errors = value;
318       global_dc->warning_as_error_requested = value;
319       break;
320
321     case OPT_Wimport:
322       /* Silently ignore for now.  */
323       break;
324
325 #if 0 // pch not supported by sdcpp
326     case OPT_Winvalid_pch:
327       cpp_opts->warn_invalid_pch = value;
328       break;
329 #endif
330
331     case OPT_Wsystem_headers:
332       cpp_opts->warn_system_headers = value;
333       break;
334
335     case OPT_Wtraditional:
336       cpp_opts->warn_traditional = value;
337       break;
338
339     case OPT_Wtrigraphs:
340       cpp_opts->warn_trigraphs = value;
341       break;
342
343     case OPT_Wundef:
344       cpp_opts->warn_undef = value;
345       break;
346
347     case OPT_Wunused_macros:
348       warn_unused_macros = value;
349       break;
350
351     case OPT_Wvariadic_macros:
352       warn_variadic_macros = value;
353       break;
354
355     case OPT_ansi:
356       set_std_c89 (false, true);
357       break;
358
359     case OPT_d:
360       handle_OPT_d (arg);
361       break;
362
363     case OPT_fdollars_in_identifiers:
364       cpp_opts->dollars_in_ident = value;
365       break;
366
367     case OPT_fsigned_char:
368       flag_signed_char = value;
369       break;
370
371     case OPT_funsigned_char:
372       flag_signed_char = !value;
373       break;
374
375 #if 0 // pch not supported by sdcpp
376     case OPT_fpch_deps:
377       cpp_opts->restore_pch_deps = value;
378       break;
379
380     case OPT_fpch_preprocess:
381       flag_pch_preprocess = value;
382       break;
383 #endif
384
385     case OPT_fpreprocessed:
386       cpp_opts->preprocessed = value;
387       break;
388
389     case OPT_fshow_column:
390       cpp_opts->show_column = value;
391       break;
392
393     case OPT_ftabstop_:
394       /* It is documented that we silently ignore silly values.  */
395       if (value >= 1 && value <= 100)
396         cpp_opts->tabstop = value;
397       break;
398
399     case OPT_fexec_charset_:
400       cpp_opts->narrow_charset = arg;
401       break;
402
403     case OPT_fwide_exec_charset_:
404       cpp_opts->wide_charset = arg;
405       break;
406
407     case OPT_finput_charset_:
408       cpp_opts->input_charset = arg;
409       break;
410
411     case OPT_idirafter:
412       add_path (xstrdup (arg), AFTER, 0, true);
413       break;
414
415     case OPT_imacros:
416     case OPT_include:
417       defer_opt (code, arg);
418       break;
419
420     case OPT_iprefix:
421       iprefix = arg;
422       break;
423
424     case OPT_iquote:
425       add_path (xstrdup (arg), QUOTE, 0, true);
426       break;
427
428     case OPT_isysroot:
429       sysroot = arg;
430       break;
431
432     case OPT_isystem:
433       add_path (xstrdup (arg), SYSTEM, 0, true);
434       break;
435
436     case OPT_iwithprefix:
437       add_prefixed_path (arg, SYSTEM);
438       break;
439
440     case OPT_iwithprefixbefore:
441       add_prefixed_path (arg, BRACKET);
442       break;
443
444     case OPT_lang_asm:
445       cpp_set_lang (parse_in, CLK_ASM);
446       cpp_opts->dollars_in_ident = false;
447       break;
448
449     case OPT_lang_objc:
450       cpp_opts->objc = 1;
451       break;
452
453     case OPT_nostdinc:
454       std_inc = false;
455       break;
456
457     case OPT_o:
458       if (!out_fname)
459         out_fname = arg;
460       else
461         error ("output filename specified twice");
462       break;
463
464       /* SDCPP specfic */
465     case OPT_obj_ext_:
466       cpp_opts->obj_ext = arg;
467       break;
468
469       /* We need to handle the -pedantic switches here, rather than in
470          sdcpp_common_post_options, so that a subsequent -Wno-endif-labels
471          is not overridden.  */
472     case OPT_pedantic_errors:
473       cpp_opts->pedantic_errors = 1;
474       /* Fall through.  */
475     case OPT_pedantic:
476       cpp_opts->pedantic = 1;
477       cpp_opts->warn_endif_labels = 1;
478       break;
479
480       /* SDCPP specfic */
481     case OPT_pedantic_parse_number:
482       cpp_opts->pedantic_parse_number = 1;
483       break;
484
485 #if 0 // pch not supported by sdcpp
486     case OPT_print_pch_checksum:
487       c_common_print_pch_checksum (stdout);
488       exit_after_options = true;
489       break;
490 #endif
491
492     case OPT_remap:
493       cpp_opts->remap = 1;
494       break;
495
496     case OPT_std_c89:
497     case OPT_std_iso9899_1990:
498     case OPT_std_iso9899_199409:
499       set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
500       break;
501
502     case OPT_std_c99:
503     case OPT_std_iso9899_1999:
504       set_std_c99 (true /* ISO */);
505       break;
506
507     case OPT_trigraphs:
508       cpp_opts->trigraphs = 1;
509       break;
510
511     case OPT_traditional_cpp:
512       cpp_opts->traditional = 1;
513       break;
514
515     case OPT_w:
516       cpp_opts->inhibit_warnings = 1;
517       break;
518
519     case OPT_v:
520       verbose = true;
521       break;
522     }
523
524   return result;
525 }
526
527 /* Post-switch processing.  */
528 bool
529 sdcpp_common_post_options (const char **pfilename)
530 {
531   struct cpp_callbacks *cb;
532
533   /* Canonicalize the input and output filenames.  */
534   if (in_fnames == NULL)
535     {
536       in_fnames = XNEWVEC (const char *, 1);
537       in_fnames[0] = "";
538     }
539   else if (strcmp (in_fnames[0], "-") == 0)
540     in_fnames[0] = "";
541
542   if (out_fname == NULL || !strcmp (out_fname, "-"))
543     out_fname = "";
544
545   if (cpp_opts->deps.style == DEPS_NONE)
546     check_deps_environment_vars ();
547
548   handle_deferred_opts ();
549
550   sanitize_cpp_opts ();
551
552   register_include_chains (parse_in, sysroot, iprefix,
553                            std_inc, 0, verbose);
554
555   /* Open the output now.  We must do so even if flag_no_output is
556      on, because there may be other output than from the actual
557      preprocessing (e.g. from -dM).  */
558   if (out_fname[0] == '\0')
559     out_stream = stdout;
560   else
561     out_stream = fopen (out_fname, "w");
562
563   if (out_stream == NULL)
564     {
565       fatal_error ("opening output file %s: %m", out_fname);
566       return false;
567     }
568
569   if (num_in_fnames > 1)
570     error ("too many filenames given.  Type %s --help for usage",
571            progname);
572
573   init_pp_output (out_stream);
574
575   cb = cpp_get_callbacks (parse_in);
576   cb->file_change = cb_file_change;
577   cb->dir_change = cb_dir_change;
578   cpp_post_options (parse_in);
579
580   input_location = UNKNOWN_LOCATION;
581
582   /* If an error has occurred in cpplib, note it so we fail
583      immediately.  */
584   errorcount += cpp_errors (parse_in);
585
586   *pfilename = this_input_filename
587     = cpp_read_main_file (parse_in, in_fnames[0]);
588   /* Don't do any compilation or preprocessing if there is no input file.  */
589   if (this_input_filename == NULL)
590     {
591       errorcount++;
592       return false;
593     }
594
595   if (flag_working_directory && !flag_no_line_commands)
596     pp_dir_change (parse_in, get_src_pwd ());
597
598   return 1;
599 }
600
601 /* Front end initialization. */
602 bool
603 sdcpp_common_init (void)
604 {
605   /* Default CPP arithmetic to something sensible for the host for the
606      benefit of dumb users like fix-header.  */
607   cpp_opts->precision = CHAR_BIT * sizeof (long);
608   cpp_opts->char_precision = CHAR_BIT;
609   cpp_opts->int_precision = CHAR_BIT * sizeof (int);
610   cpp_opts->wchar_precision = CHAR_BIT * sizeof (int);
611   cpp_opts->unsigned_wchar = 1;
612   cpp_opts->bytes_big_endian = BYTES_BIG_ENDIAN;
613
614   /* This can't happen until after wchar_precision and bytes_big_endian
615      are known.  */
616   cpp_init_iconv (parse_in);
617
618 #if 0
619   if (version_flag)
620     c_common_print_pch_checksum (stderr);
621 #endif
622
623   finish_options ();
624   preprocess_file (parse_in);
625   return false;
626 }
627
628 /* Common finish hook for the C, ObjC and C++ front ends.  */
629 void
630 sdcpp_common_finish (void)
631 {
632   FILE *deps_stream = NULL;
633
634   if (cpp_opts->deps.style != DEPS_NONE)
635     {
636       /* If -M or -MM was seen without -MF, default output to the
637          output stream.  */
638       if (!deps_file)
639         deps_stream = out_stream;
640       else
641         {
642           deps_stream = fopen (deps_file, deps_append ? "a": "w");
643           if (!deps_stream)
644             fatal_error ("opening dependency file %s: %m", deps_file);
645         }
646     }
647
648   /* For performance, avoid tearing down cpplib's internal structures
649      with cpp_destroy ().  */
650   errorcount += cpp_finish (parse_in, deps_stream);
651
652   if (deps_stream && deps_stream != out_stream
653       && (ferror (deps_stream) || fclose (deps_stream)))
654     fatal_error ("closing dependency file %s: %m", deps_file);
655
656   if (out_stream && (ferror (out_stream) || fclose (out_stream)))
657     fatal_error ("when writing output to %s: %m", out_fname);
658 }
659
660 /* Either of two environment variables can specify output of
661    dependencies.  Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
662    DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
663    and DEPS_TARGET is the target to mention in the deps.  They also
664    result in dependency information being appended to the output file
665    rather than overwriting it, and like Sun's compiler
666    SUNPRO_DEPENDENCIES suppresses the dependency on the main file.  */
667 static void
668 check_deps_environment_vars (void)
669 {
670   char *spec;
671
672   GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
673   if (spec)
674     cpp_opts->deps.style = DEPS_USER;
675   else
676     {
677       GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
678       if (spec)
679         {
680           cpp_opts->deps.style = DEPS_SYSTEM;
681           cpp_opts->deps.ignore_main_file = true;
682         }
683     }
684
685   if (spec)
686     {
687       /* Find the space before the DEPS_TARGET, if there is one.  */
688       char *s = strchr (spec, ' ');
689       if (s)
690         {
691           /* Let the caller perform MAKE quoting.  */
692           defer_opt (OPT_MT, s + 1);
693           *s = '\0';
694         }
695
696       /* Command line -MF overrides environment variables and default.  */
697       if (!deps_file)
698         deps_file = spec;
699
700       deps_append = 1;
701       deps_seen = true;
702     }
703 }
704
705 /* Handle deferred command line switches.  */
706 static void
707 handle_deferred_opts (void)
708 {
709   size_t i;
710   struct deps *deps;
711
712   /* Avoid allocating the deps buffer if we don't need it.
713      (This flag may be true without there having been -MT or -MQ
714      options, but we'll still need the deps buffer.)  */
715   if (!deps_seen)
716     return;
717
718   deps = cpp_get_deps (parse_in);
719
720   for (i = 0; i < deferred_count; i++)
721     {
722       struct deferred_opt *opt = &deferred_opts[i];
723
724       if (opt->code == OPT_MT || opt->code == OPT_MQ)
725         deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
726     }
727 }
728
729 /* These settings are appropriate for GCC, but not necessarily so for
730    cpplib as a library.  */
731 static void
732 sanitize_cpp_opts (void)
733 {
734   /* If we don't know what style of dependencies to output, complain
735      if any other dependency switches have been given.  */
736   if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
737     error ("to generate dependencies you must specify either -M or -MM");
738
739   /* -dM and dependencies suppress normal output; do it here so that
740      the last -d[MDN] switch overrides earlier ones.  */
741   if (flag_dump_macros == 'M')
742     flag_no_output = 1;
743
744   /* Disable -dD, -dN and -dI if normal output is suppressed.  Allow
745      -dM since at least glibc relies on -M -dM to work.  */
746   /* Also, flag_no_output implies flag_no_line_commands, always. */
747   if (flag_no_output)
748     {
749       if (flag_dump_macros != 'M')
750         flag_dump_macros = 0;
751       flag_dump_includes = 0;
752       flag_no_line_commands = 1;
753     }
754
755   cpp_opts->unsigned_char = !flag_signed_char;
756   cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
757
758   /* Similarly with -Wno-variadic-macros.  No check for c99 here, since
759      this also turns off warnings about GCCs extension.  */
760   cpp_opts->warn_variadic_macros
761     = warn_variadic_macros && (pedantic || warn_traditional);
762
763   /* If we're generating preprocessor output, emit current directory
764      if explicitly requested  */
765   if (flag_working_directory == -1)
766     flag_working_directory = 0;
767 }
768
769 /* Add include path with a prefix at the front of its name.  */
770 static void
771 add_prefixed_path (const char *suffix, size_t chain)
772 {
773   char *path;
774   const char *prefix;
775   size_t prefix_len, suffix_len;
776
777   suffix_len = strlen (suffix);
778   prefix     = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
779   prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
780
781   path = (char *) xmalloc (prefix_len + suffix_len + 1);
782   memcpy (path, prefix, prefix_len);
783   memcpy (path + prefix_len, suffix, suffix_len);
784   path[prefix_len + suffix_len] = '\0';
785
786   add_path (path, chain, 0, false);
787 }
788
789 /* Handle -D, -U, -A, -imacros, and the first -include.  */
790 static void
791 finish_options (void)
792 {
793   if (!cpp_opts->preprocessed)
794     {
795       size_t i;
796
797       cb_file_change (parse_in,
798                       linemap_add (&line_table, LC_RENAME, 0,
799                                    _("<built-in>"), 0));
800
801       cpp_init_builtins (parse_in, 0 /*flag_hosted*/);
802
803       /* We're about to send user input to cpplib, so make it warn for
804          things that we previously (when we sent it internal definitions)
805          told it to not warn.
806
807          C99 permits implementation-defined characters in identifiers.
808          The documented meaning of -std= is to turn off extensions that
809          conflict with the specified standard, and since a strictly
810          conforming program cannot contain a '$', we do not condition
811          their acceptance on the -std= setting.  */
812       cpp_opts->warn_dollars = (cpp_opts->pedantic && !cpp_opts->c99);
813
814       cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
815       for (i = 0; i < deferred_count; i++)
816         {
817           struct deferred_opt *opt = &deferred_opts[i];
818
819           if (opt->code == OPT_D)
820             cpp_define (parse_in, opt->arg);
821           else if (opt->code == OPT_U)
822             cpp_undef (parse_in, opt->arg);
823           else if (opt->code == OPT_A)
824             {
825               if (opt->arg[0] == '-')
826                 cpp_unassert (parse_in, opt->arg + 1);
827               else
828                 cpp_assert (parse_in, opt->arg);
829             }
830         }
831
832       /* Handle -imacros after -D and -U.  */
833       for (i = 0; i < deferred_count; i++)
834         {
835           struct deferred_opt *opt = &deferred_opts[i];
836
837           if (opt->code == OPT_imacros
838               && cpp_push_include (parse_in, opt->arg))
839             {
840               /* Disable push_command_line_include callback for now.  */
841               include_cursor = deferred_count + 1;
842               cpp_scan_nooutput (parse_in);
843             }
844         }
845     }
846
847   include_cursor = 0;
848   push_command_line_include ();
849 }
850
851 /* Give CPP the next file given by -include, if any.  */
852 static void
853 push_command_line_include (void)
854 {
855   while (include_cursor < deferred_count)
856     {
857       struct deferred_opt *opt = &deferred_opts[include_cursor++];
858
859       if (!cpp_opts->preprocessed && opt->code == OPT_include
860           && cpp_push_include (parse_in, opt->arg))
861         return;
862     }
863
864   if (include_cursor == deferred_count)
865     {
866       include_cursor++;
867       /* -Wunused-macros should only warn about macros defined hereafter.  */
868       cpp_opts->warn_unused_macros = warn_unused_macros;
869       /* Restore the line map from <command line>.  */
870       if (!cpp_opts->preprocessed)
871         cpp_change_file (parse_in, LC_RENAME, this_input_filename);
872
873       /* Set this here so the client can change the option if it wishes,
874          and after stacking the main file so we don't trace the main file.  */
875       line_table.trace_includes = cpp_opts->print_include_names;
876     }
877 }
878
879 /* File change callback.  Has to handle -include files.  */
880 static void
881 cb_file_change (cpp_reader * ARG_UNUSED (pfile),
882                 const struct line_map *new_map)
883 {
884   pp_file_change (new_map);
885
886   if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
887     push_command_line_include ();
888 }
889
890 void
891 cb_dir_change (cpp_reader * ARG_UNUSED (pfile), const char *dir)
892 {
893   if (!set_src_pwd (dir))
894     warning (0, "too late for # directive to set debug directory");
895 }
896
897 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
898    extensions if ISO).  There is no concept of gnu94.  */
899 static void
900 set_std_c89 (int c94, int iso)
901 {
902   cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
903 }
904
905 /* Set the C 99 standard (without GNU extensions if ISO).  */
906 static void
907 set_std_c99 (int iso)
908 {
909   cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
910 }
911
912 /* Args to -d specify what to dump.  Silently ignore
913    unrecognized options; they may be aimed at toplev.c.  */
914 static void
915 handle_OPT_d (const char *arg)
916 {
917   char c;
918
919   while ((c = *arg++) != '\0')
920     switch (c)
921       {
922       case 'M':                 /* Dump macros only.  */
923       case 'N':                 /* Dump names.  */
924       case 'D':                 /* Dump definitions.  */
925         flag_dump_macros = c;
926         break;
927
928       case 'I':
929         flag_dump_includes = 1;
930         break;
931       }
932 }