OSDN Git Service

* gcc.dg/cpp/charconst-3.c: Run, don't compile.
[pf3gnuchains/gcc-fork.git] / gcc / cppinit.c
1 /* CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4    Contributed by Per Bothner, 1994-95.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "prefix.h"
27 #include "intl.h"
28 #include "version.h"
29 #include "mkdeps.h"
30 #include "cppdefault.h"
31 #include "except.h"     /* for USING_SJLJ_EXCEPTIONS */
32
33 /* Predefined symbols, built-in macros, and the default include path.  */
34
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME)     do { (VAR) = getenv (NAME); } while (0)
37 #endif
38
39 /* Windows does not natively support inodes, and neither does MSDOS.
40    Cygwin's emulation can generate non-unique inodes, so don't use it.
41    VMS has non-numeric inodes.  */
42 #ifdef VMS
43 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
44 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
45 #else
46 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
47 #  define INO_T_EQ(A, B) 0
48 # else
49 #  define INO_T_EQ(A, B) ((A) == (B))
50 # endif
51 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
52 #endif
53
54 /* Internal structures and prototypes.  */
55
56 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
57    -imacros switch.  */
58 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
59 struct pending_option
60 {
61   struct pending_option *next;
62   const char *arg;
63   cl_directive_handler handler;
64 };
65
66 /* The `pending' structure accumulates all the options that are not
67    actually processed until we hit cpp_read_main_file.  It consists of
68    several lists, one for each type of option.  We keep both head and
69    tail pointers for quick insertion.  */
70 struct cpp_pending
71 {
72   struct pending_option *directive_head, *directive_tail;
73
74   struct search_path *quote_head, *quote_tail;
75   struct search_path *brack_head, *brack_tail;
76   struct search_path *systm_head, *systm_tail;
77   struct search_path *after_head, *after_tail;
78
79   struct pending_option *imacros_head, *imacros_tail;
80   struct pending_option *include_head, *include_tail;
81 };
82
83 #ifdef __STDC__
84 #define APPEND(pend, list, elt) \
85   do {  if (!(pend)->list##_head) (pend)->list##_head = (elt); \
86         else (pend)->list##_tail->next = (elt); \
87         (pend)->list##_tail = (elt); \
88   } while (0)
89 #else
90 #define APPEND(pend, list, elt) \
91   do {  if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
92         else (pend)->list/**/_tail->next = (elt); \
93         (pend)->list/**/_tail = (elt); \
94   } while (0)
95 #endif
96
97 static void print_help                  PARAMS ((void));
98 static void path_include                PARAMS ((cpp_reader *,
99                                                  char *, int));
100 static void init_library                PARAMS ((void));
101 static void init_builtins               PARAMS ((cpp_reader *));
102 static void append_include_chain        PARAMS ((cpp_reader *,
103                                                  char *, int, int));
104 static struct search_path * remove_dup_dir      PARAMS ((cpp_reader *,
105                                                  struct search_path *));
106 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
107                                                  struct search_path *));
108 static void merge_include_chains        PARAMS ((cpp_reader *));
109 static bool push_include                PARAMS ((cpp_reader *,
110                                                  struct pending_option *));
111 static void free_chain                  PARAMS ((struct pending_option *));
112 static void set_lang                    PARAMS ((cpp_reader *, enum c_lang));
113 static void init_dependency_output      PARAMS ((cpp_reader *));
114 static void init_standard_includes      PARAMS ((cpp_reader *));
115 static void read_original_filename      PARAMS ((cpp_reader *));
116 static void new_pending_directive       PARAMS ((struct cpp_pending *,
117                                                  const char *,
118                                                  cl_directive_handler));
119 static void output_deps                 PARAMS ((cpp_reader *));
120 static int parse_option                 PARAMS ((const char *));
121
122 /* Fourth argument to append_include_chain: chain to use.
123    Note it's never asked to append to the quote chain.  */
124 enum { BRACKET = 0, SYSTEM, AFTER };
125
126 /* If we have designated initializers (GCC >2.7) these tables can be
127    initialized, constant data.  Otherwise, they have to be filled in at
128    runtime.  */
129 #if HAVE_DESIGNATED_INITIALIZERS
130
131 #define init_trigraph_map()  /* Nothing.  */
132 #define TRIGRAPH_MAP \
133 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
134
135 #define END };
136 #define s(p, v) [p] = v,
137
138 #else
139
140 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
141  static void init_trigraph_map PARAMS ((void)) { \
142  unsigned char *x = _cpp_trigraph_map;
143
144 #define END }
145 #define s(p, v) x[p] = v;
146
147 #endif
148
149 TRIGRAPH_MAP
150   s('=', '#')   s(')', ']')     s('!', '|')
151   s('(', '[')   s('\'', '^')    s('>', '}')
152   s('/', '\\')  s('<', '{')     s('-', '~')
153 END
154
155 #undef s
156 #undef END
157 #undef TRIGRAPH_MAP
158
159 /* Given a colon-separated list of file names PATH,
160    add all the names to the search path for include files.  */
161 static void
162 path_include (pfile, list, path)
163      cpp_reader *pfile;
164      char *list;
165      int path;
166 {
167   char *p, *q, *name;
168
169   p = list;
170
171   do
172     {
173       /* Find the end of this name.  */
174       q = p;
175       while (*q != 0 && *q != PATH_SEPARATOR) q++;
176       if (q == p)
177         {
178           /* An empty name in the path stands for the current directory.  */
179           name = (char *) xmalloc (2);
180           name[0] = '.';
181           name[1] = 0;
182         }
183       else
184         {
185           /* Otherwise use the directory that is named.  */
186           name = (char *) xmalloc (q - p + 1);
187           memcpy (name, p, q - p);
188           name[q - p] = 0;
189         }
190
191       append_include_chain (pfile, name, path, 0);
192
193       /* Advance past this name.  */
194       if (*q == 0)
195         break;
196       p = q + 1;
197     }
198   while (1);
199 }
200
201 /* Append DIR to include path PATH.  DIR must be allocated on the
202    heap; this routine takes responsibility for freeing it.  CXX_AWARE
203    is non-zero if the header contains extern "C" guards for C++,
204    otherwise it is zero.  */
205 static void
206 append_include_chain (pfile, dir, path, cxx_aware)
207      cpp_reader *pfile;
208      char *dir;
209      int path;
210      int cxx_aware ATTRIBUTE_UNUSED;
211 {
212   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
213   struct search_path *new;
214   struct stat st;
215   unsigned int len;
216
217   if (*dir == '\0')
218     {
219       free (dir);
220       dir = xstrdup (".");
221     }
222   _cpp_simplify_pathname (dir);
223
224   if (stat (dir, &st))
225     {
226       /* Dirs that don't exist are silently ignored.  */
227       if (errno != ENOENT)
228         cpp_errno (pfile, DL_ERROR, dir);
229       else if (CPP_OPTION (pfile, verbose))
230         fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
231       free (dir);
232       return;
233     }
234
235   if (!S_ISDIR (st.st_mode))
236     {
237       cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
238       free (dir);
239       return;
240     }
241
242   len = strlen (dir);
243   if (len > pfile->max_include_len)
244     pfile->max_include_len = len;
245
246   new = (struct search_path *) xmalloc (sizeof (struct search_path));
247   new->name = dir;
248   new->len = len;
249   INO_T_COPY (new->ino, st.st_ino);
250   new->dev  = st.st_dev;
251   /* Both systm and after include file lists should be treated as system
252      include files since these two lists are really just a concatenation
253      of one "system" list.  */
254   if (path == SYSTEM || path == AFTER)
255 #ifdef NO_IMPLICIT_EXTERN_C
256     new->sysp = 1;
257 #else
258     new->sysp = cxx_aware ? 1 : 2;
259 #endif
260   else
261     new->sysp = 0;
262   new->name_map = NULL;
263   new->next = NULL;
264
265   switch (path)
266     {
267     case BRACKET:       APPEND (pend, brack, new); break;
268     case SYSTEM:        APPEND (pend, systm, new); break;
269     case AFTER:         APPEND (pend, after, new); break;
270     }
271 }
272
273 /* Handle a duplicated include path.  PREV is the link in the chain
274    before the duplicate.  The duplicate is removed from the chain and
275    freed.  Returns PREV.  */
276 static struct search_path *
277 remove_dup_dir (pfile, prev)
278      cpp_reader *pfile;
279      struct search_path *prev;
280 {
281   struct search_path *cur = prev->next;
282
283   if (CPP_OPTION (pfile, verbose))
284     fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
285
286   prev->next = cur->next;
287   free ((PTR) cur->name);
288   free (cur);
289
290   return prev;
291 }
292
293 /* Remove duplicate directories from a chain.  Returns the tail of the
294    chain, or NULL if the chain is empty.  This algorithm is quadratic
295    in the number of -I switches, which is acceptable since there
296    aren't usually that many of them.  */
297 static struct search_path *
298 remove_dup_dirs (pfile, head)
299      cpp_reader *pfile;
300      struct search_path *head;
301 {
302   struct search_path *prev = NULL, *cur, *other;
303
304   for (cur = head; cur; cur = cur->next)
305     {
306       for (other = head; other != cur; other = other->next)
307         if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
308           {
309             if (cur->sysp && !other->sysp)
310               {
311                 cpp_error (pfile, DL_WARNING,
312                            "changing search order for system directory \"%s\"",
313                            cur->name);
314                 if (strcmp (cur->name, other->name))
315                   cpp_error (pfile, DL_WARNING,
316                              "  as it is the same as non-system directory \"%s\"",
317                              other->name);
318                 else
319                   cpp_error (pfile, DL_WARNING,
320                              "  as it has already been specified as a non-system directory");
321               }
322             cur = remove_dup_dir (pfile, prev);
323             break;
324           }
325       prev = cur;
326     }
327
328   return prev;
329 }
330
331 /* Merge the four include chains together in the order quote, bracket,
332    system, after.  Remove duplicate dirs (as determined by
333    INO_T_EQ()).  The system_include and after_include chains are never
334    referred to again after this function; all access is through the
335    bracket_include path.  */
336 static void
337 merge_include_chains (pfile)
338      cpp_reader *pfile;
339 {
340   struct search_path *quote, *brack, *systm, *qtail;
341
342   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
343
344   quote = pend->quote_head;
345   brack = pend->brack_head;
346   systm = pend->systm_head;
347   qtail = pend->quote_tail;
348
349   /* Paste together bracket, system, and after include chains.  */
350   if (systm)
351     pend->systm_tail->next = pend->after_head;
352   else
353     systm = pend->after_head;
354
355   if (brack)
356     pend->brack_tail->next = systm;
357   else
358     brack = systm;
359
360   /* This is a bit tricky.  First we drop dupes from the quote-include
361      list.  Then we drop dupes from the bracket-include list.
362      Finally, if qtail and brack are the same directory, we cut out
363      brack and move brack up to point to qtail.
364
365      We can't just merge the lists and then uniquify them because
366      then we may lose directories from the <> search path that should
367      be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
368      safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
369      -Ibar -I- -Ifoo -Iquux.  */
370
371   remove_dup_dirs (pfile, brack);
372   qtail = remove_dup_dirs (pfile, quote);
373
374   if (quote)
375     {
376       qtail->next = brack;
377
378       /* If brack == qtail, remove brack as it's simpler.  */
379       if (brack && INO_T_EQ (qtail->ino, brack->ino)
380           && qtail->dev == brack->dev)
381         brack = remove_dup_dir (pfile, qtail);
382     }
383   else
384     quote = brack;
385
386   CPP_OPTION (pfile, quote_include) = quote;
387   CPP_OPTION (pfile, bracket_include) = brack;
388 }
389
390 /* A set of booleans indicating what CPP features each source language
391    requires.  */
392 struct lang_flags
393 {
394   char c99;
395   char objc;
396   char cplusplus;
397   char extended_numbers;
398   char trigraphs;
399   char dollars_in_ident;
400   char cplusplus_comments;
401   char digraphs;
402 };
403
404 /* ??? Enable $ in identifiers in assembly? */
405 static const struct lang_flags lang_defaults[] =
406 { /*              c99 objc c++ xnum trig dollar c++comm digr  */
407   /* GNUC89 */  { 0,  0,   0,  1,   0,   1,     1,      1     },
408   /* GNUC99 */  { 1,  0,   0,  1,   0,   1,     1,      1     },
409   /* STDC89 */  { 0,  0,   0,  0,   1,   0,     0,      0     },
410   /* STDC94 */  { 0,  0,   0,  0,   1,   0,     0,      1     },
411   /* STDC99 */  { 1,  0,   0,  1,   1,   0,     1,      1     },
412   /* GNUCXX */  { 0,  0,   1,  1,   0,   1,     1,      1     },
413   /* CXX98  */  { 0,  0,   1,  1,   1,   0,     1,      1     },
414   /* OBJC   */  { 0,  1,   0,  1,   0,   1,     1,      1     },
415   /* OBJCXX */  { 0,  1,   1,  1,   0,   1,     1,      1     },
416   /* ASM    */  { 0,  0,   0,  1,   0,   0,     1,      0     }
417 };
418
419 /* Sets internal flags correctly for a given language.  */
420 static void
421 set_lang (pfile, lang)
422      cpp_reader *pfile;
423      enum c_lang lang;
424 {
425   const struct lang_flags *l = &lang_defaults[(int) lang];
426   
427   CPP_OPTION (pfile, lang) = lang;
428
429   CPP_OPTION (pfile, c99)                = l->c99;
430   CPP_OPTION (pfile, objc)               = l->objc;
431   CPP_OPTION (pfile, cplusplus)          = l->cplusplus;
432   CPP_OPTION (pfile, extended_numbers)   = l->extended_numbers;
433   CPP_OPTION (pfile, trigraphs)          = l->trigraphs;
434   CPP_OPTION (pfile, dollars_in_ident)   = l->dollars_in_ident;
435   CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
436   CPP_OPTION (pfile, digraphs)           = l->digraphs;
437 }
438
439 #ifdef HOST_EBCDIC
440 static int opt_comp PARAMS ((const void *, const void *));
441
442 /* Run-time sorting of options array.  */
443 static int
444 opt_comp (p1, p2)
445      const void *p1, *p2;
446 {
447   return strcmp (((struct cl_option *) p1)->opt_text,
448                  ((struct cl_option *) p2)->opt_text);
449 }
450 #endif
451
452 /* init initializes library global state.  It might not need to
453    do anything depending on the platform and compiler.  */
454 static void
455 init_library ()
456 {
457   static int initialized = 0;
458
459   if (! initialized)
460     {
461       initialized = 1;
462
463 #ifdef HOST_EBCDIC
464       /* For non-ASCII hosts, the cl_options array needs to be sorted at
465          runtime.  */
466       qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
467 #endif
468
469       /* Set up the trigraph map.  This doesn't need to do anything if
470          we were compiled with a compiler that supports C99 designated
471          initializers.  */
472       init_trigraph_map ();
473     }
474 }
475
476 /* Initialize a cpp_reader structure.  */
477 cpp_reader *
478 cpp_create_reader (lang)
479      enum c_lang lang;
480 {
481   cpp_reader *pfile;
482
483   /* Initialise this instance of the library if it hasn't been already.  */
484   init_library ();
485
486   pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
487
488   set_lang (pfile, lang);
489   CPP_OPTION (pfile, warn_import) = 1;
490   CPP_OPTION (pfile, discard_comments) = 1;
491   CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
492   CPP_OPTION (pfile, show_column) = 1;
493   CPP_OPTION (pfile, tabstop) = 8;
494   CPP_OPTION (pfile, operator_names) = 1;
495   CPP_OPTION (pfile, warn_endif_labels) = 1;
496 #if DEFAULT_SIGNED_CHAR
497   CPP_OPTION (pfile, signed_char) = 1;
498 #else
499   CPP_OPTION (pfile, signed_char) = 0;
500 #endif
501
502   CPP_OPTION (pfile, pending) =
503     (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
504
505   /* Default CPP arithmetic to something sensible for the host for the
506      benefit of dumb users like fix-header.  */
507 #define BITS_PER_HOST_WIDEST_INT (CHAR_BIT * sizeof (HOST_WIDEST_INT))
508   CPP_OPTION (pfile, precision) = BITS_PER_HOST_WIDEST_INT;
509   CPP_OPTION (pfile, char_precision) = CHAR_BIT;
510   CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
511   CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
512
513   /* It's simplest to just create this struct whether or not it will
514      be needed.  */
515   pfile->deps = deps_init ();
516
517   /* Initialise the line map.  Start at logical line 1, so we can use
518      a line number of zero for special states.  */
519   init_line_maps (&pfile->line_maps);
520   pfile->line = 1;
521
522   /* Initialize lexer state.  */
523   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
524
525   /* Set up static tokens.  */
526   pfile->date.type = CPP_EOF;
527   pfile->avoid_paste.type = CPP_PADDING;
528   pfile->avoid_paste.val.source = NULL;
529   pfile->eof.type = CPP_EOF;
530   pfile->eof.flags = 0;
531
532   /* Create a token buffer for the lexer.  */
533   _cpp_init_tokenrun (&pfile->base_run, 250);
534   pfile->cur_run = &pfile->base_run;
535   pfile->cur_token = pfile->base_run.base;
536
537   /* Initialise the base context.  */
538   pfile->context = &pfile->base_context;
539   pfile->base_context.macro = 0;
540   pfile->base_context.prev = pfile->base_context.next = 0;
541
542   /* Aligned and unaligned storage.  */
543   pfile->a_buff = _cpp_get_buff (pfile, 0);
544   pfile->u_buff = _cpp_get_buff (pfile, 0);
545
546   /* The expression parser stack.  */
547   _cpp_expand_op_stack (pfile);
548
549   /* Initialise the buffer obstack.  */
550   gcc_obstack_init (&pfile->buffer_ob);
551
552   _cpp_init_includes (pfile);
553
554   return pfile;
555 }
556
557 /* Free resources used by PFILE.  Accessing PFILE after this function
558    returns leads to undefined behaviour.  Returns the error count.  */
559 int
560 cpp_destroy (pfile)
561      cpp_reader *pfile;
562 {
563   int result;
564   struct search_path *dir, *dirn;
565   cpp_context *context, *contextn;
566   tokenrun *run, *runn;
567
568   free_chain (CPP_OPTION (pfile, pending)->include_head);
569   free (CPP_OPTION (pfile, pending));
570   free (pfile->op_stack);
571
572   while (CPP_BUFFER (pfile) != NULL)
573     _cpp_pop_buffer (pfile);
574
575   if (pfile->macro_buffer)
576     {
577       free ((PTR) pfile->macro_buffer);
578       pfile->macro_buffer = NULL;
579       pfile->macro_buffer_len = 0;
580     }
581
582   deps_free (pfile->deps);
583   obstack_free (&pfile->buffer_ob, 0);
584
585   _cpp_destroy_hashtable (pfile);
586   _cpp_cleanup_includes (pfile);
587
588   _cpp_free_buff (pfile->a_buff);
589   _cpp_free_buff (pfile->u_buff);
590   _cpp_free_buff (pfile->free_buffs);
591
592   for (run = &pfile->base_run; run; run = runn)
593     {
594       runn = run->next;
595       free (run->base);
596       if (run != &pfile->base_run)
597         free (run);
598     }
599
600   for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
601     {
602       dirn = dir->next;
603       free ((PTR) dir->name);
604       free (dir);
605     }
606
607   for (context = pfile->base_context.next; context; context = contextn)
608     {
609       contextn = context->next;
610       free (context);
611     }
612
613   free_line_maps (&pfile->line_maps);
614
615   result = pfile->errors;
616   free (pfile);
617
618   return result;
619 }
620
621
622 /* This structure defines one built-in identifier.  A node will be
623    entered in the hash table under the name NAME, with value VALUE (if
624    any).  If flags has OPERATOR, the node's operator field is used; if
625    flags has BUILTIN the node's builtin field is used.  Macros that are
626    known at build time should not be flagged BUILTIN, as then they do
627    not appear in macro dumps with e.g. -dM or -dD.
628
629    Two values are not compile time constants, so we tag
630    them in the FLAGS field instead:
631    VERS         value is the global version_string, quoted
632    ULP          value is the global user_label_prefix
633
634    Also, macros with CPLUS set in the flags field are entered only for C++.  */
635 struct builtin
636 {
637   const uchar *name;
638   const char *value;
639   unsigned char builtin;
640   unsigned char operator;
641   unsigned short flags;
642   unsigned short len;
643 };
644 #define VERS            0x01
645 #define ULP             0x02
646 #define CPLUS           0x04
647 #define BUILTIN         0x08
648 #define OPERATOR        0x10
649
650 #define B(n, t)       { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
651 #define C(n, v)       { U n, v, 0, 0, 0, sizeof n - 1 }
652 #define X(n, f)       { U n, 0, 0, 0, f, sizeof n - 1 }
653 #define O(n, c, f)    { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
654 static const struct builtin builtin_array[] =
655 {
656   B("__TIME__",          BT_TIME),
657   B("__DATE__",          BT_DATE),
658   B("__FILE__",          BT_FILE),
659   B("__BASE_FILE__",     BT_BASE_FILE),
660   B("__LINE__",          BT_SPECLINE),
661   B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
662   B("_Pragma",           BT_PRAGMA),
663
664   X("__VERSION__",              VERS),
665   X("__USER_LABEL_PREFIX__",    ULP),
666   C("__REGISTER_PREFIX__",      REGISTER_PREFIX),
667   C("__HAVE_BUILTIN_SETJMP__",  "1"),
668 #if USING_SJLJ_EXCEPTIONS
669   /* libgcc needs to know this.  */
670   C("__USING_SJLJ_EXCEPTIONS__","1"),
671 #endif
672 #ifndef NO_BUILTIN_SIZE_TYPE
673   C("__SIZE_TYPE__",            SIZE_TYPE),
674 #endif
675 #ifndef NO_BUILTIN_PTRDIFF_TYPE
676   C("__PTRDIFF_TYPE__",         PTRDIFF_TYPE),
677 #endif
678 #ifndef NO_BUILTIN_WCHAR_TYPE
679   C("__WCHAR_TYPE__",           WCHAR_TYPE),
680 #endif
681 #ifndef NO_BUILTIN_WINT_TYPE
682   C("__WINT_TYPE__",            WINT_TYPE),
683 #endif
684 #ifdef STDC_0_IN_SYSTEM_HEADERS
685   B("__STDC__",          BT_STDC),
686 #else
687   C("__STDC__",          "1"),
688 #endif
689
690   /* Named operators known to the preprocessor.  These cannot be #defined
691      and always have their stated meaning.  They are treated like normal
692      identifiers except for the type code and the meaning.  Most of them
693      are only for C++ (but see iso646.h).  */
694   O("and",      CPP_AND_AND, CPLUS),
695   O("and_eq",   CPP_AND_EQ,  CPLUS),
696   O("bitand",   CPP_AND,     CPLUS),
697   O("bitor",    CPP_OR,      CPLUS),
698   O("compl",    CPP_COMPL,   CPLUS),
699   O("not",      CPP_NOT,     CPLUS),
700   O("not_eq",   CPP_NOT_EQ,  CPLUS),
701   O("or",       CPP_OR_OR,   CPLUS),
702   O("or_eq",    CPP_OR_EQ,   CPLUS),
703   O("xor",      CPP_XOR,     CPLUS),
704   O("xor_eq",   CPP_XOR_EQ,  CPLUS)
705 };
706 #undef B
707 #undef C
708 #undef X
709 #undef O
710 #define builtin_array_end (builtin_array + ARRAY_SIZE (builtin_array))
711
712 /* Subroutine of cpp_read_main_file; reads the builtins table above and
713    enters them, and language-specific macros, into the hash table.  */
714 static void
715 init_builtins (pfile)
716      cpp_reader *pfile;
717 {
718   const struct builtin *b;
719
720   for(b = builtin_array; b < builtin_array_end; b++)
721     {
722       if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
723         continue;
724
725       if ((b->flags & OPERATOR) && ! CPP_OPTION (pfile, operator_names))
726         continue;
727
728       if (b->flags & (OPERATOR | BUILTIN))
729         {
730           cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
731           if (b->flags & OPERATOR)
732             {
733               hp->flags |= NODE_OPERATOR;
734               hp->value.operator = b->operator;
735             }
736           else
737             {
738               hp->type = NT_MACRO;
739               hp->flags |= NODE_BUILTIN | NODE_WARN;
740               hp->value.builtin = b->builtin;
741             }
742         }
743       else                      /* A standard macro of some kind.  */
744         {
745           const char *val;
746           char *str;
747
748           if (b->flags & VERS)
749             {
750               /* Allocate enough space for 'name "value"\n\0'.  */
751               str = alloca (b->len + strlen (version_string) + 5);
752               sprintf (str, "%s \"%s\"\n", b->name, version_string);
753             }
754           else
755             {
756               if (b->flags & ULP)
757                 val = CPP_OPTION (pfile, user_label_prefix);
758               else
759                 val = b->value;
760
761               /* Allocate enough space for "name value\n\0".  */
762               str = alloca (b->len + strlen (val) + 3);
763               sprintf(str, "%s %s\n", b->name, val);
764             }
765
766           _cpp_define_builtin (pfile, str);
767         }
768     }
769
770   if (CPP_OPTION (pfile, cplusplus))
771     _cpp_define_builtin (pfile, "__cplusplus 1");
772
773   if (CPP_OPTION (pfile, objc))
774     _cpp_define_builtin (pfile, "__OBJC__ 1");
775
776   if (CPP_OPTION (pfile, lang) == CLK_STDC94)
777     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
778   else if (CPP_OPTION (pfile, c99))
779     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
780
781   if (CPP_OPTION (pfile, signed_char) == 0)
782     _cpp_define_builtin (pfile, "__CHAR_UNSIGNED__ 1");
783
784   if (CPP_OPTION (pfile, lang) == CLK_STDC89
785       || CPP_OPTION (pfile, lang) == CLK_STDC94
786       || CPP_OPTION (pfile, lang) == CLK_STDC99)
787     _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
788   else if (CPP_OPTION (pfile, lang) == CLK_ASM)
789     _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
790
791   if (pfile->cb.register_builtins)
792     (*pfile->cb.register_builtins) (pfile);
793 }
794 #undef BUILTIN
795 #undef OPERATOR
796 #undef VERS
797 #undef ULP
798 #undef CPLUS
799 #undef builtin_array_end
800
801 /* And another subroutine.  This one sets up the standard include path.  */
802 static void
803 init_standard_includes (pfile)
804      cpp_reader *pfile;
805 {
806   char *path;
807   const struct default_include *p;
808   const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
809
810   /* Several environment variables may add to the include search path.
811      CPATH specifies an additional list of directories to be searched
812      as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
813      etc. specify an additional list of directories to be searched as
814      if specified with -isystem, for the language indicated.  */
815
816   GET_ENV_PATH_LIST (path, "CPATH");
817   if (path != 0 && *path != 0)
818     path_include (pfile, path, BRACKET);
819
820   switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
821     {
822     case 0:
823       GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
824       break;
825     case 1:
826       GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
827       break;
828     case 2:
829       GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
830       break;
831     case 3:
832       GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
833       break;
834     }
835   if (path != 0 && *path != 0)
836     path_include (pfile, path, SYSTEM);
837
838   /* Search "translated" versions of GNU directories.
839      These have /usr/local/lib/gcc... replaced by specd_prefix.  */
840   if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
841     {
842       /* Remove the `include' from /usr/local/lib/gcc.../include.
843          GCC_INCLUDE_DIR will always end in /include.  */
844       int default_len = cpp_GCC_INCLUDE_DIR_len;
845       char *default_prefix = (char *) alloca (default_len + 1);
846       int specd_len = strlen (specd_prefix);
847
848       memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
849       default_prefix[default_len] = '\0';
850
851       for (p = cpp_include_defaults; p->fname; p++)
852         {
853           /* Some standard dirs are only for C++.  */
854           if (!p->cplusplus
855               || (CPP_OPTION (pfile, cplusplus)
856                   && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
857             {
858               /* Does this dir start with the prefix?  */
859               if (!memcmp (p->fname, default_prefix, default_len))
860                 {
861                   /* Yes; change prefix and add to search list.  */
862                   int flen = strlen (p->fname);
863                   int this_len = specd_len + flen - default_len;
864                   char *str = (char *) xmalloc (this_len + 1);
865                   memcpy (str, specd_prefix, specd_len);
866                   memcpy (str + specd_len,
867                           p->fname + default_len,
868                           flen - default_len + 1);
869
870                   append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
871                 }
872             }
873         }
874     }
875
876   /* Search ordinary names for GNU include directories.  */
877   for (p = cpp_include_defaults; p->fname; p++)
878     {
879       /* Some standard dirs are only for C++.  */
880       if (!p->cplusplus
881           || (CPP_OPTION (pfile, cplusplus)
882               && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
883         {
884           char *str = update_path (p->fname, p->component);
885           append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
886         }
887     }
888 }
889
890 /* Pushes a command line -imacro and -include file indicated by P onto
891    the buffer stack.  Returns non-zero if successful.  */
892 static bool
893 push_include (pfile, p)
894      cpp_reader *pfile;
895      struct pending_option *p;
896 {
897   cpp_token header;
898
899   /* Later: maybe update this to use the #include "" search path
900      if cpp_read_file fails.  */
901   header.type = CPP_STRING;
902   header.val.str.text = (const unsigned char *) p->arg;
903   header.val.str.len = strlen (p->arg);
904   /* Make the command line directive take up a line.  */
905   pfile->line++;
906
907   return _cpp_execute_include (pfile, &header, IT_CMDLINE);
908 }
909
910 /* Frees a pending_option chain.  */
911 static void
912 free_chain (head)
913      struct pending_option *head;
914 {
915   struct pending_option *next;
916
917   while (head)
918     {
919       next = head->next;
920       free (head);
921       head = next;
922     }
923 }
924
925 /* Sanity-checks are dependent on command-line options, so it is
926    called as a subroutine of cpp_read_main_file ().  */
927 #if ENABLE_CHECKING
928 static void sanity_checks PARAMS ((cpp_reader *));
929 static void sanity_checks (pfile)
930      cpp_reader *pfile;
931 {
932   cppchar_t test = 0;
933
934   /* Sanity checks for assumptions about CPP arithmetic and target
935      type precisions made by cpplib.  */
936   test--;
937   if (test < 1)
938     cpp_error (pfile, DL_FATAL, "cppchar_t must be an unsigned type");
939
940   if (CPP_OPTION (pfile, precision) > BITS_PER_HOST_WIDEST_INT)
941     cpp_error (pfile, DL_FATAL,
942                "preprocessor arithmetic has maximum precision of %u bits; target requires %u bits",
943                BITS_PER_HOST_WIDEST_INT, CPP_OPTION (pfile, precision));
944
945   if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
946     cpp_error (pfile, DL_FATAL,
947                "CPP arithmetic must be at least as precise as a target int");
948
949   if (CPP_OPTION (pfile, char_precision) < 8)
950     cpp_error (pfile, DL_FATAL, "target char is less than 8 bits wide");
951
952   if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
953     cpp_error (pfile, DL_FATAL,
954                "target wchar_t is narrower than target char");
955
956   if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
957     cpp_error (pfile, DL_FATAL,
958                "target int is narrower than target char");
959
960   if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
961     cpp_error (pfile, DL_FATAL,
962                "CPP on this host cannot handle wide character constants over %u bits, but the target requires %u bits",
963                BITS_PER_CPPCHAR_T, CPP_OPTION (pfile, wchar_precision));
964 }
965 #else
966 # define sanity_checks(PFILE)
967 #endif
968
969 /* This is called after options have been parsed, and partially
970    processed.  Setup for processing input from the file named FNAME,
971    or stdin if it is the empty string.  Return the original filename
972    on success (e.g. foo.i->foo.c), or NULL on failure.  */
973 const char *
974 cpp_read_main_file (pfile, fname, table)
975      cpp_reader *pfile;
976      const char *fname;
977      hash_table *table;
978 {
979   sanity_checks (pfile);
980
981   /* The front ends don't set up the hash table until they have
982      finished processing the command line options, so initializing the
983      hashtable is deferred until now.  */
984   _cpp_init_hashtable (pfile, table);
985
986   /* Set up the include search path now.  */
987   if (! CPP_OPTION (pfile, no_standard_includes))
988     init_standard_includes (pfile);
989
990   merge_include_chains (pfile);
991
992   /* With -v, print the list of dirs to search.  */
993   if (CPP_OPTION (pfile, verbose))
994     {
995       struct search_path *l;
996       fprintf (stderr, _("#include \"...\" search starts here:\n"));
997       for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
998         {
999           if (l == CPP_OPTION (pfile, bracket_include))
1000             fprintf (stderr, _("#include <...> search starts here:\n"));
1001           fprintf (stderr, " %s\n", l->name);
1002         }
1003       fprintf (stderr, _("End of search list.\n"));
1004     }
1005
1006   if (CPP_OPTION (pfile, print_deps))
1007     /* Set the default target (if there is none already).  */
1008     deps_add_default_target (pfile->deps, fname);
1009
1010   /* Open the main input file.  */
1011   if (!_cpp_read_file (pfile, fname))
1012     return NULL;
1013
1014   /* Set this after cpp_post_options so the client can change the
1015      option if it wishes, and after stacking the main file so we don't
1016      trace the main file.  */
1017   pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
1018
1019   /* For foo.i, read the original filename foo.c now, for the benefit
1020      of the front ends.  */
1021   if (CPP_OPTION (pfile, preprocessed))
1022     read_original_filename (pfile);
1023
1024   return pfile->map->to_file;
1025 }
1026
1027 /* For preprocessed files, if the first tokens are of the form # NUM.
1028    handle the directive so we know the original file name.  This will
1029    generate file_change callbacks, which the front ends must handle
1030    appropriately given their state of initialization.  */
1031 static void
1032 read_original_filename (pfile)
1033      cpp_reader *pfile;
1034 {
1035   const cpp_token *token, *token1;
1036
1037   /* Lex ahead; if the first tokens are of the form # NUM, then
1038      process the directive, otherwise back up.  */
1039   token = _cpp_lex_direct (pfile);
1040   if (token->type == CPP_HASH)
1041     {
1042       token1 = _cpp_lex_direct (pfile);
1043       _cpp_backup_tokens (pfile, 1);
1044
1045       /* If it's a #line directive, handle it.  */
1046       if (token1->type == CPP_NUMBER)
1047         {
1048           _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
1049           return;
1050         }
1051     }
1052
1053   /* Backup as if nothing happened.  */
1054   _cpp_backup_tokens (pfile, 1);
1055 }
1056
1057 /* Handle pending command line options: -D, -U, -A, -imacros and
1058    -include.  This should be called after debugging has been properly
1059    set up in the front ends.  */
1060 void
1061 cpp_finish_options (pfile)
1062      cpp_reader *pfile;
1063 {
1064   /* Install builtins and process command line macros etc. in the order
1065      they appeared, but only if not already preprocessed.  */
1066   if (! CPP_OPTION (pfile, preprocessed))
1067     {
1068       struct pending_option *p;
1069
1070       _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1071       init_builtins (pfile);
1072       _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1073       for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1074         (*p->handler) (pfile, p->arg);
1075
1076       /* Scan -imacros files after -D, -U, but before -include.
1077          pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1078          push -include files.  */
1079       for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1080         if (push_include (pfile, p))
1081           cpp_scan_nooutput (pfile);
1082
1083       pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1084       _cpp_maybe_push_include_file (pfile);
1085     }
1086
1087   free_chain (CPP_OPTION (pfile, pending)->imacros_head);
1088   free_chain (CPP_OPTION (pfile, pending)->directive_head);
1089 }
1090
1091 /* Push the next buffer on the stack given by -include, if any.  */
1092 void
1093 _cpp_maybe_push_include_file (pfile)
1094      cpp_reader *pfile;
1095 {
1096   if (pfile->next_include_file)
1097     {
1098       struct pending_option *head = *pfile->next_include_file;
1099   
1100       while (head && !push_include (pfile, head))
1101         head = head->next;
1102
1103       if (head)
1104         pfile->next_include_file = &head->next;
1105       else
1106         {
1107           /* All done; restore the line map from <command line>.  */
1108           _cpp_do_file_change (pfile, LC_RENAME,
1109                                pfile->line_maps.maps[0].to_file, 1, 0);
1110           /* Don't come back here again.  */
1111           pfile->next_include_file = NULL;
1112         }
1113     }
1114 }
1115
1116 /* Use mkdeps.c to output dependency information.  */
1117 static void
1118 output_deps (pfile)
1119      cpp_reader *pfile;
1120 {
1121   /* Stream on which to print the dependency information.  */
1122   FILE *deps_stream = 0;
1123   const char *const deps_mode =
1124     CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1125
1126   if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1127     deps_stream = stdout;
1128   else
1129     {
1130       deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1131       if (deps_stream == 0)
1132         {
1133           cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
1134           return;
1135         }
1136     }
1137
1138   deps_write (pfile->deps, deps_stream, 72);
1139
1140   if (CPP_OPTION (pfile, deps_phony_targets))
1141     deps_phony_targets (pfile->deps, deps_stream);
1142
1143   /* Don't close stdout.  */
1144   if (deps_stream != stdout)
1145     {
1146       if (ferror (deps_stream) || fclose (deps_stream) != 0)
1147         cpp_error (pfile, DL_FATAL, "I/O error on output");
1148     }
1149 }
1150
1151 /* This is called at the end of preprocessing.  It pops the
1152    last buffer and writes dependency output.  It should also
1153    clear macro definitions, such that you could call cpp_start_read
1154    with a new filename to restart processing.  */
1155 void
1156 cpp_finish (pfile)
1157      cpp_reader *pfile;
1158 {
1159   /* cpplex.c leaves the final buffer on the stack.  This it so that
1160      it returns an unending stream of CPP_EOFs to the client.  If we
1161      popped the buffer, we'd dereference a NULL buffer pointer and
1162      segfault.  It's nice to allow the client to do worry-free excess
1163      cpp_get_token calls.  */
1164   while (pfile->buffer)
1165     _cpp_pop_buffer (pfile);
1166
1167   /* Don't write the deps file if preprocessing has failed.  */
1168   if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1169     output_deps (pfile);
1170
1171   /* Report on headers that could use multiple include guards.  */
1172   if (CPP_OPTION (pfile, print_include_names))
1173     _cpp_report_missing_guards (pfile);
1174 }
1175
1176 /* Add a directive to be handled later in the initialization phase.  */
1177 static void
1178 new_pending_directive (pend, text, handler)
1179      struct cpp_pending *pend;
1180      const char *text;
1181      cl_directive_handler handler;
1182 {
1183   struct pending_option *o = (struct pending_option *)
1184     xmalloc (sizeof (struct pending_option));
1185
1186   o->arg = text;
1187   o->next = NULL;
1188   o->handler = handler;
1189   APPEND (pend, directive, o);
1190 }
1191
1192 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1193    I.e. a const string initializer with parens around it.  That is
1194    what N_("string") resolves to, so we make no_* be macros instead.  */
1195 #define no_arg N_("argument missing after %s")
1196 #define no_ass N_("assertion missing after %s")
1197 #define no_dir N_("directory name missing after %s")
1198 #define no_fil N_("file name missing after %s")
1199 #define no_mac N_("macro name missing after %s")
1200 #define no_pth N_("path name missing after %s")
1201 #define no_num N_("number missing after %s")
1202 #define no_tgt N_("target missing after %s")
1203
1204 /* This is the list of all command line options, with the leading
1205    "-" removed.  It must be sorted in ASCII collating order.  */
1206 #define COMMAND_LINE_OPTIONS                                                  \
1207   DEF_OPT("$",                        0,      OPT_dollar)                     \
1208   DEF_OPT("+",                        0,      OPT_plus)                       \
1209   DEF_OPT("-help",                    0,      OPT__help)                      \
1210   DEF_OPT("-target-help",             0,      OPT_target__help)               \
1211   DEF_OPT("-version",                 0,      OPT__version)                   \
1212   DEF_OPT("A",                        no_ass, OPT_A)                          \
1213   DEF_OPT("C",                        0,      OPT_C)                          \
1214   DEF_OPT("CC",                       0,      OPT_CC)                         \
1215   DEF_OPT("D",                        no_mac, OPT_D)                          \
1216   DEF_OPT("H",                        0,      OPT_H)                          \
1217   DEF_OPT("I",                        no_dir, OPT_I)                          \
1218   DEF_OPT("M",                        0,      OPT_M)                          \
1219   DEF_OPT("MD",                       no_fil, OPT_MD)                         \
1220   DEF_OPT("MF",                       no_fil, OPT_MF)                         \
1221   DEF_OPT("MG",                       0,      OPT_MG)                         \
1222   DEF_OPT("MM",                       0,      OPT_MM)                         \
1223   DEF_OPT("MMD",                      no_fil, OPT_MMD)                        \
1224   DEF_OPT("MP",                       0,      OPT_MP)                         \
1225   DEF_OPT("MQ",                       no_tgt, OPT_MQ)                         \
1226   DEF_OPT("MT",                       no_tgt, OPT_MT)                         \
1227   DEF_OPT("P",                        0,      OPT_P)                          \
1228   DEF_OPT("U",                        no_mac, OPT_U)                          \
1229   DEF_OPT("W",                        no_arg, OPT_W)  /* arg optional */      \
1230   DEF_OPT("d",                        no_arg, OPT_d)                          \
1231   DEF_OPT("fleading-underscore",      0,      OPT_fleading_underscore)        \
1232   DEF_OPT("fno-leading-underscore",   0,      OPT_fno_leading_underscore)     \
1233   DEF_OPT("fno-operator-names",       0,      OPT_fno_operator_names)         \
1234   DEF_OPT("fno-preprocessed",         0,      OPT_fno_preprocessed)           \
1235   DEF_OPT("fno-show-column",          0,      OPT_fno_show_column)            \
1236   DEF_OPT("fpreprocessed",            0,      OPT_fpreprocessed)              \
1237   DEF_OPT("fshow-column",             0,      OPT_fshow_column)               \
1238   DEF_OPT("fsigned-char",             0,      OPT_fsigned_char)               \
1239   DEF_OPT("ftabstop=",                no_num, OPT_ftabstop)                   \
1240   DEF_OPT("funsigned-char",           0,      OPT_funsigned_char)             \
1241   DEF_OPT("h",                        0,      OPT_h)                          \
1242   DEF_OPT("idirafter",                no_dir, OPT_idirafter)                  \
1243   DEF_OPT("imacros",                  no_fil, OPT_imacros)                    \
1244   DEF_OPT("include",                  no_fil, OPT_include)                    \
1245   DEF_OPT("iprefix",                  no_pth, OPT_iprefix)                    \
1246   DEF_OPT("isystem",                  no_dir, OPT_isystem)                    \
1247   DEF_OPT("iwithprefix",              no_dir, OPT_iwithprefix)                \
1248   DEF_OPT("iwithprefixbefore",        no_dir, OPT_iwithprefixbefore)          \
1249   DEF_OPT("lang-asm",                 0,      OPT_lang_asm)                   \
1250   DEF_OPT("lang-c",                   0,      OPT_lang_c)                     \
1251   DEF_OPT("lang-c++",                 0,      OPT_lang_cplusplus)             \
1252   DEF_OPT("lang-c89",                 0,      OPT_lang_c89)                   \
1253   DEF_OPT("lang-objc",                0,      OPT_lang_objc)                  \
1254   DEF_OPT("lang-objc++",              0,      OPT_lang_objcplusplus)          \
1255   DEF_OPT("nostdinc",                 0,      OPT_nostdinc)                   \
1256   DEF_OPT("nostdinc++",               0,      OPT_nostdincplusplus)           \
1257   DEF_OPT("o",                        no_fil, OPT_o)                          \
1258   DEF_OPT("pedantic",                 0,      OPT_pedantic)                   \
1259   DEF_OPT("pedantic-errors",          0,      OPT_pedantic_errors)            \
1260   DEF_OPT("remap",                    0,      OPT_remap)                      \
1261   DEF_OPT("std=c++98",                0,      OPT_std_cplusplus98)            \
1262   DEF_OPT("std=c89",                  0,      OPT_std_c89)                    \
1263   DEF_OPT("std=c99",                  0,      OPT_std_c99)                    \
1264   DEF_OPT("std=c9x",                  0,      OPT_std_c9x)                    \
1265   DEF_OPT("std=gnu89",                0,      OPT_std_gnu89)                  \
1266   DEF_OPT("std=gnu99",                0,      OPT_std_gnu99)                  \
1267   DEF_OPT("std=gnu9x",                0,      OPT_std_gnu9x)                  \
1268   DEF_OPT("std=iso9899:1990",         0,      OPT_std_iso9899_1990)           \
1269   DEF_OPT("std=iso9899:199409",       0,      OPT_std_iso9899_199409)         \
1270   DEF_OPT("std=iso9899:1999",         0,      OPT_std_iso9899_1999)           \
1271   DEF_OPT("std=iso9899:199x",         0,      OPT_std_iso9899_199x)           \
1272   DEF_OPT("trigraphs",                0,      OPT_trigraphs)                  \
1273   DEF_OPT("v",                        0,      OPT_v)                          \
1274   DEF_OPT("version",                  0,      OPT_version)                    \
1275   DEF_OPT("w",                        0,      OPT_w)
1276
1277 #define DEF_OPT(text, msg, code) code,
1278 enum opt_code
1279 {
1280   COMMAND_LINE_OPTIONS
1281   N_OPTS
1282 };
1283 #undef DEF_OPT
1284
1285 struct cl_option
1286 {
1287   const char *opt_text;
1288   const char *msg;
1289   size_t opt_len;
1290   enum opt_code opt_code;
1291 };
1292
1293 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1294 #ifdef HOST_EBCDIC
1295 static struct cl_option cl_options[] =
1296 #else
1297 static const struct cl_option cl_options[] =
1298 #endif
1299 {
1300   COMMAND_LINE_OPTIONS
1301 };
1302 #undef DEF_OPT
1303 #undef COMMAND_LINE_OPTIONS
1304
1305 /* Perform a binary search to find which, if any, option the given
1306    command-line matches.  Returns its index in the option array,
1307    negative on failure.  Complications arise since some options can be
1308    suffixed with an argument, and multiple complete matches can occur,
1309    e.g. -iwithprefix and -iwithprefixbefore.  Moreover, we need to
1310    accept options beginning with -W that we do not recognise, but not
1311    to swallow any subsequent command line argument; this is handled as
1312    special cases in cpp_handle_option.  */
1313 static int
1314 parse_option (input)
1315      const char *input;
1316 {
1317   unsigned int md, mn, mx;
1318   size_t opt_len;
1319   int comp;
1320
1321   mn = 0;
1322   mx = N_OPTS;
1323
1324   while (mx > mn)
1325     {
1326       md = (mn + mx) / 2;
1327
1328       opt_len = cl_options[md].opt_len;
1329       comp = memcmp (input, cl_options[md].opt_text, opt_len);
1330
1331       if (comp > 0)
1332         mn = md + 1;
1333       else if (comp < 0)
1334         mx = md;
1335       else
1336         {
1337           if (input[opt_len] == '\0')
1338             return md;
1339           /* We were passed more text.  If the option takes an argument,
1340              we may match a later option or we may have been passed the
1341              argument.  The longest possible option match succeeds.
1342              If the option takes no arguments we have not matched and
1343              continue the search (e.g. input="stdc++" match was "stdc").  */
1344           mn = md + 1;
1345           if (cl_options[md].msg)
1346             {
1347               /* Scan forwards.  If we get an exact match, return it.
1348                  Otherwise, return the longest option-accepting match.
1349                  This loops no more than twice with current options.  */
1350               mx = md;
1351               for (; mn < (unsigned int) N_OPTS; mn++)
1352                 {
1353                   opt_len = cl_options[mn].opt_len;
1354                   if (memcmp (input, cl_options[mn].opt_text, opt_len))
1355                     break;
1356                   if (input[opt_len] == '\0')
1357                     return mn;
1358                   if (cl_options[mn].msg)
1359                     mx = mn;
1360                 }
1361               return mx;
1362             }
1363         }
1364     }
1365
1366   return -1;
1367 }
1368
1369 /* Handle one command-line option in (argc, argv).
1370    Can be called multiple times, to handle multiple sets of options.
1371    If ignore is non-zero, this will ignore unrecognized -W* options.
1372    Returns number of strings consumed.  */
1373 int
1374 cpp_handle_option (pfile, argc, argv, ignore)
1375      cpp_reader *pfile;
1376      int argc;
1377      char **argv;
1378      int ignore;
1379 {
1380   int i = 0;
1381   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1382
1383   /* Interpret "-" or a non-option as a file name.  */
1384   if (argv[i][0] != '-' || argv[i][1] == '\0')
1385     {
1386       if (CPP_OPTION (pfile, in_fname) == NULL)
1387         CPP_OPTION (pfile, in_fname) = argv[i];
1388       else if (CPP_OPTION (pfile, out_fname) == NULL)
1389         CPP_OPTION (pfile, out_fname) = argv[i];
1390       else
1391         cpp_error (pfile, DL_FATAL,
1392                    "too many filenames. Type %s --help for usage info",
1393                    progname);
1394     }
1395   else
1396     {
1397       enum opt_code opt_code;
1398       int opt_index;
1399       const char *arg = 0;
1400
1401       /* Skip over '-'.  */
1402       opt_index = parse_option (&argv[i][1]);
1403       if (opt_index < 0)
1404         return i;
1405
1406       opt_code = cl_options[opt_index].opt_code;
1407       if (cl_options[opt_index].msg)
1408         {
1409           arg = &argv[i][cl_options[opt_index].opt_len + 1];
1410
1411           /* Yuk. Special case for -W as it must not swallow
1412              up any following argument.  If this becomes common, add
1413              another field to the cl_options table.  */
1414           if (arg[0] == '\0' && opt_code != OPT_W)
1415             {
1416               arg = argv[++i];
1417               if (!arg)
1418                 {
1419                   cpp_error (pfile, DL_FATAL,
1420                              cl_options[opt_index].msg, argv[i - 1]);
1421                   return argc;
1422                 }
1423             }
1424         }
1425
1426       switch (opt_code)
1427         {
1428         case N_OPTS: /* Shut GCC up.  */
1429           break;
1430         case OPT_fleading_underscore:
1431           CPP_OPTION (pfile, user_label_prefix) = "_";
1432           break;
1433         case OPT_fno_leading_underscore:
1434           CPP_OPTION (pfile, user_label_prefix) = "";
1435           break;
1436         case OPT_fno_operator_names:
1437           CPP_OPTION (pfile, operator_names) = 0;
1438           break;
1439         case OPT_fpreprocessed:
1440           CPP_OPTION (pfile, preprocessed) = 1;
1441           break;
1442         case OPT_fno_preprocessed:
1443           CPP_OPTION (pfile, preprocessed) = 0;
1444           break;
1445         case OPT_fshow_column:
1446           CPP_OPTION (pfile, show_column) = 1;
1447           break;
1448         case OPT_fno_show_column:
1449           CPP_OPTION (pfile, show_column) = 0;
1450           break;
1451         case OPT_fsigned_char:
1452           CPP_OPTION (pfile, signed_char) = 1;
1453           break;
1454         case OPT_funsigned_char:
1455           CPP_OPTION (pfile, signed_char) = 0;
1456           break;
1457         case OPT_ftabstop:
1458           /* Silently ignore empty string, non-longs and silly values.  */
1459           if (arg[0] != '\0')
1460             {
1461               char *endptr;
1462               long tabstop = strtol (arg, &endptr, 10);
1463               if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1464                 CPP_OPTION (pfile, tabstop) = tabstop;
1465             }
1466           break;
1467         case OPT_w:
1468           CPP_OPTION (pfile, inhibit_warnings) = 1;
1469           break;
1470         case OPT_h:
1471         case OPT__help:
1472           print_help ();
1473           CPP_OPTION (pfile, help_only) = 1;
1474           break;
1475         case OPT_target__help:
1476           /* Print if any target specific options. cpplib has none, but
1477              make sure help_only gets set.  */
1478           CPP_OPTION (pfile, help_only) = 1;
1479           break;
1480
1481           /* --version inhibits compilation, -version doesn't. -v means
1482              verbose and -version.  Historical reasons, don't ask.  */
1483         case OPT__version:
1484           CPP_OPTION (pfile, help_only) = 1;
1485           pfile->print_version = 1;
1486           break;
1487         case OPT_v:
1488           CPP_OPTION (pfile, verbose) = 1;
1489           pfile->print_version = 1;
1490           break;
1491         case OPT_version:
1492           pfile->print_version = 1;
1493           break;
1494
1495         case OPT_C:
1496           CPP_OPTION (pfile, discard_comments) = 0;
1497           break;
1498         case OPT_CC:
1499           CPP_OPTION (pfile, discard_comments) = 0;
1500           CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0;
1501           break;
1502         case OPT_P:
1503           CPP_OPTION (pfile, no_line_commands) = 1;
1504           break;
1505         case OPT_dollar:        /* Don't include $ in identifiers.  */
1506           CPP_OPTION (pfile, dollars_in_ident) = 0;
1507           break;
1508         case OPT_H:
1509           CPP_OPTION (pfile, print_include_names) = 1;
1510           break;
1511         case OPT_D:
1512           new_pending_directive (pend, arg, cpp_define);
1513           break;
1514         case OPT_pedantic_errors:
1515           CPP_OPTION (pfile, pedantic_errors) = 1;
1516           /* fall through */
1517         case OPT_pedantic:
1518           CPP_OPTION (pfile, pedantic) = 1;
1519           CPP_OPTION (pfile, warn_endif_labels) = 1;
1520           break;
1521         case OPT_trigraphs:
1522           CPP_OPTION (pfile, trigraphs) = 1;
1523           break;
1524         case OPT_plus:
1525           CPP_OPTION (pfile, cplusplus) = 1;
1526           CPP_OPTION (pfile, cplusplus_comments) = 1;
1527           break;
1528         case OPT_remap:
1529           CPP_OPTION (pfile, remap) = 1;
1530           break;
1531         case OPT_iprefix:
1532           CPP_OPTION (pfile, include_prefix) = arg;
1533           CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1534           break;
1535         case OPT_lang_c:
1536           set_lang (pfile, CLK_GNUC89);
1537           break;
1538         case OPT_lang_cplusplus:
1539           set_lang (pfile, CLK_GNUCXX);
1540           break;
1541         case OPT_lang_objc:
1542           set_lang (pfile, CLK_OBJC);
1543           break;
1544         case OPT_lang_objcplusplus:
1545           set_lang (pfile, CLK_OBJCXX);
1546           break;
1547         case OPT_lang_asm:
1548           set_lang (pfile, CLK_ASM);
1549           break;
1550         case OPT_std_cplusplus98:
1551           set_lang (pfile, CLK_CXX98);
1552           break;
1553         case OPT_std_gnu89:
1554           set_lang (pfile, CLK_GNUC89);
1555           break;
1556         case OPT_std_gnu9x:
1557         case OPT_std_gnu99:
1558           set_lang (pfile, CLK_GNUC99);
1559           break;
1560         case OPT_std_iso9899_199409:
1561           set_lang (pfile, CLK_STDC94);
1562           break;
1563         case OPT_std_iso9899_1990:
1564         case OPT_std_c89:
1565         case OPT_lang_c89:
1566           set_lang (pfile, CLK_STDC89);
1567           break;
1568         case OPT_std_iso9899_199x:
1569         case OPT_std_iso9899_1999:
1570         case OPT_std_c9x:
1571         case OPT_std_c99:
1572           set_lang (pfile, CLK_STDC99);
1573           break;
1574         case OPT_nostdinc:
1575           /* -nostdinc causes no default include directories.
1576              You must specify all include-file directories with -I.  */
1577           CPP_OPTION (pfile, no_standard_includes) = 1;
1578           break;
1579         case OPT_nostdincplusplus:
1580           /* -nostdinc++ causes no default C++-specific include directories.  */
1581           CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1582           break;
1583         case OPT_o:
1584           if (CPP_OPTION (pfile, out_fname) == NULL)
1585             CPP_OPTION (pfile, out_fname) = arg;
1586           else
1587             {
1588               cpp_error (pfile, DL_FATAL, "output filename specified twice");
1589               return argc;
1590             }
1591           break;
1592         case OPT_d:
1593           /* Args to -d specify what parts of macros to dump.
1594              Silently ignore unrecognised options; they may
1595              be aimed at the compiler proper.  */
1596           {
1597             char c;
1598
1599             while ((c = *arg++) != '\0')
1600               switch (c)
1601                 {
1602                 case 'M':
1603                   CPP_OPTION (pfile, dump_macros) = dump_only;
1604                   break;
1605                 case 'N':
1606                   CPP_OPTION (pfile, dump_macros) = dump_names;
1607                   break;
1608                 case 'D':
1609                   CPP_OPTION (pfile, dump_macros) = dump_definitions;
1610                   break;
1611                 case 'I':
1612                   CPP_OPTION (pfile, dump_includes) = 1;
1613                   break;
1614                 }
1615           }
1616           break;
1617
1618         case OPT_MG:
1619           CPP_OPTION (pfile, print_deps_missing_files) = 1;
1620           break;
1621         case OPT_M:
1622           /* When doing dependencies with -M or -MM, suppress normal
1623              preprocessed output, but still do -dM etc. as software
1624              depends on this.  Preprocessed output occurs if -MD, -MMD
1625              or environment var dependency generation is used.  */
1626           CPP_OPTION (pfile, print_deps) = 2;
1627           CPP_OPTION (pfile, no_output) = 1;
1628           break;
1629         case OPT_MM:
1630           CPP_OPTION (pfile, print_deps) = 1;
1631           CPP_OPTION (pfile, no_output) = 1;
1632           break;
1633         case OPT_MF:
1634           CPP_OPTION (pfile, deps_file) = arg;
1635           break;
1636         case OPT_MP:
1637           CPP_OPTION (pfile, deps_phony_targets) = 1;
1638           break;
1639         case OPT_MQ:
1640         case OPT_MT:
1641           /* Add a target.  -MQ quotes for Make.  */
1642           deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1643           break;
1644
1645         case OPT_MD:
1646           CPP_OPTION (pfile, print_deps) = 2;
1647           CPP_OPTION (pfile, deps_file) = arg;
1648           break;
1649         case OPT_MMD:
1650           CPP_OPTION (pfile, print_deps) = 1;
1651           CPP_OPTION (pfile, deps_file) = arg;
1652           break;
1653
1654         case OPT_A:
1655           if (arg[0] == '-')
1656             {
1657               /* -A with an argument beginning with '-' acts as
1658                  #unassert on whatever immediately follows the '-'.
1659                  If "-" is the whole argument, we eliminate all
1660                  predefined macros and assertions, including those
1661                  that were specified earlier on the command line.
1662                  That way we can get rid of any that were passed
1663                  automatically in from GCC.  */
1664
1665               if (arg[1] == '\0')
1666                 {
1667                   free_chain (pend->directive_head);
1668                   pend->directive_head = NULL;
1669                   pend->directive_tail = NULL;
1670                 }
1671               else
1672                 new_pending_directive (pend, arg + 1, cpp_unassert);
1673             }
1674           else
1675             new_pending_directive (pend, arg, cpp_assert);
1676           break;
1677         case OPT_U:
1678           new_pending_directive (pend, arg, cpp_undef);
1679           break;
1680         case OPT_I:           /* Add directory to path for includes.  */
1681           if (!strcmp (arg, "-"))
1682             {
1683               /* -I- means:
1684                  Use the preceding -I directories for #include "..."
1685                  but not #include <...>.
1686                  Don't search the directory of the present file
1687                  for #include "...".  (Note that -I. -I- is not the same as
1688                  the default setup; -I. uses the compiler's working dir.)  */
1689               if (! CPP_OPTION (pfile, ignore_srcdir))
1690                 {
1691                   pend->quote_head = pend->brack_head;
1692                   pend->quote_tail = pend->brack_tail;
1693                   pend->brack_head = 0;
1694                   pend->brack_tail = 0;
1695                   CPP_OPTION (pfile, ignore_srcdir) = 1;
1696                 }
1697               else
1698                 {
1699                   cpp_error (pfile, DL_FATAL, "-I- specified twice");
1700                   return argc;
1701                 }
1702             }
1703           else
1704             append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1705           break;
1706         case OPT_isystem:
1707           /* Add directory to beginning of system include path, as a system
1708              include directory.  */
1709           append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1710           break;
1711         case OPT_include:
1712         case OPT_imacros:
1713           {
1714             struct pending_option *o = (struct pending_option *)
1715               xmalloc (sizeof (struct pending_option));
1716             o->arg = arg;
1717             o->next = NULL;
1718
1719             if (opt_code == OPT_include)
1720               APPEND (pend, include, o);
1721             else
1722               APPEND (pend, imacros, o);
1723           }
1724           break;
1725         case OPT_iwithprefix:
1726           /* Add directory to end of path for includes,
1727              with the default prefix at the front of its name.  */
1728           /* fall through */
1729         case OPT_iwithprefixbefore:
1730           /* Add directory to main path for includes,
1731              with the default prefix at the front of its name.  */
1732           {
1733             char *fname;
1734             int len;
1735
1736             len = strlen (arg);
1737
1738             if (CPP_OPTION (pfile, include_prefix) != 0)
1739               {
1740                 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1741                 fname = xmalloc (ipl + len + 1);
1742                 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1743                 memcpy (fname + ipl, arg, len + 1);
1744               }
1745             else if (cpp_GCC_INCLUDE_DIR_len)
1746               {
1747                 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1748                 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1749                 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1750               }
1751             else
1752               fname = xstrdup (arg);
1753
1754             append_include_chain (pfile, fname,
1755                           opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1756           }
1757           break;
1758         case OPT_idirafter:
1759           /* Add directory to end of path for includes.  */
1760           append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1761           break;
1762         case OPT_W:
1763           /* Silently ignore unrecognised options.  */
1764           if (!strcmp (argv[i], "-Wall"))
1765             {
1766               CPP_OPTION (pfile, warn_trigraphs) = 1;
1767               CPP_OPTION (pfile, warn_comments) = 1;
1768             }
1769           else if (!strcmp (argv[i], "-Wtraditional"))
1770             CPP_OPTION (pfile, warn_traditional) = 1;
1771           else if (!strcmp (argv[i], "-Wtrigraphs"))
1772             CPP_OPTION (pfile, warn_trigraphs) = 1;
1773           else if (!strcmp (argv[i], "-Wcomment"))
1774             CPP_OPTION (pfile, warn_comments) = 1;
1775           else if (!strcmp (argv[i], "-Wcomments"))
1776             CPP_OPTION (pfile, warn_comments) = 1;
1777           else if (!strcmp (argv[i], "-Wundef"))
1778             CPP_OPTION (pfile, warn_undef) = 1;
1779           else if (!strcmp (argv[i], "-Wimport"))
1780             CPP_OPTION (pfile, warn_import) = 1;
1781           else if (!strcmp (argv[i], "-Werror"))
1782             CPP_OPTION (pfile, warnings_are_errors) = 1;
1783           else if (!strcmp (argv[i], "-Wsystem-headers"))
1784             CPP_OPTION (pfile, warn_system_headers) = 1;
1785           else if (!strcmp (argv[i], "-Wendif-labels"))
1786             CPP_OPTION (pfile, warn_endif_labels) = 1;
1787           else if (!strcmp (argv[i], "-Wno-traditional"))
1788             CPP_OPTION (pfile, warn_traditional) = 0;
1789           else if (!strcmp (argv[i], "-Wno-trigraphs"))
1790             CPP_OPTION (pfile, warn_trigraphs) = 0;
1791           else if (!strcmp (argv[i], "-Wno-comment"))
1792             CPP_OPTION (pfile, warn_comments) = 0;
1793           else if (!strcmp (argv[i], "-Wno-comments"))
1794             CPP_OPTION (pfile, warn_comments) = 0;
1795           else if (!strcmp (argv[i], "-Wno-undef"))
1796             CPP_OPTION (pfile, warn_undef) = 0;
1797           else if (!strcmp (argv[i], "-Wno-import"))
1798             CPP_OPTION (pfile, warn_import) = 0;
1799           else if (!strcmp (argv[i], "-Wno-error"))
1800             CPP_OPTION (pfile, warnings_are_errors) = 0;
1801           else if (!strcmp (argv[i], "-Wno-system-headers"))
1802             CPP_OPTION (pfile, warn_system_headers) = 0;
1803           else if (!strcmp (argv[i], "-Wno-endif-labels"))
1804             CPP_OPTION (pfile, warn_endif_labels) = 0;
1805           else if (! ignore)
1806             return i;
1807           break;
1808         }
1809     }
1810   return i + 1;
1811 }
1812
1813 /* Handle command-line options in (argc, argv).
1814    Can be called multiple times, to handle multiple sets of options.
1815    Returns if an unrecognized option is seen.
1816    Returns number of strings consumed.  */
1817 int
1818 cpp_handle_options (pfile, argc, argv)
1819      cpp_reader *pfile;
1820      int argc;
1821      char **argv;
1822 {
1823   int i;
1824   int strings_processed;
1825
1826   for (i = 0; i < argc; i += strings_processed)
1827     {
1828       strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
1829       if (strings_processed == 0)
1830         break;
1831     }
1832
1833   return i;
1834 }
1835
1836 /* Extra processing when all options are parsed, after all calls to
1837    cpp_handle_option[s].  Consistency checks etc.  */
1838 void
1839 cpp_post_options (pfile)
1840      cpp_reader *pfile;
1841 {
1842   if (pfile->print_version)
1843     {
1844       fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1845 #ifdef TARGET_VERSION
1846       TARGET_VERSION;
1847 #endif
1848       fputc ('\n', stderr);
1849     }
1850
1851   /* Canonicalize in_fname and out_fname.  We guarantee they are not
1852      NULL, and that the empty string represents stdin / stdout.  */
1853   if (CPP_OPTION (pfile, in_fname) == NULL
1854       || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1855     CPP_OPTION (pfile, in_fname) = "";
1856
1857   if (CPP_OPTION (pfile, out_fname) == NULL
1858       || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1859     CPP_OPTION (pfile, out_fname) = "";
1860
1861   /* -Wtraditional is not useful in C++ mode.  */
1862   if (CPP_OPTION (pfile, cplusplus))
1863     CPP_OPTION (pfile, warn_traditional) = 0;
1864
1865   /* Set this if it hasn't been set already.  */
1866   if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1867     CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1868
1869   /* Permanently disable macro expansion if we are rescanning
1870      preprocessed text.  */
1871   if (CPP_OPTION (pfile, preprocessed))
1872     pfile->state.prevent_expansion = 1;
1873
1874   /* -dM makes no normal output.  This is set here so that -dM -dD
1875      works as expected.  */
1876   if (CPP_OPTION (pfile, dump_macros) == dump_only)
1877     CPP_OPTION (pfile, no_output) = 1;
1878
1879   /* Disable -dD, -dN and -dI if we should make no normal output
1880      (such as with -M). Allow -M -dM since some software relies on
1881      this.  */
1882   if (CPP_OPTION (pfile, no_output))
1883     {
1884       if (CPP_OPTION (pfile, dump_macros) != dump_only)
1885         CPP_OPTION (pfile, dump_macros) = dump_none;
1886       CPP_OPTION (pfile, dump_includes) = 0;
1887     }
1888
1889   /* We need to do this after option processing and before
1890      cpp_start_read, as cppmain.c relies on the options->no_output to
1891      set its callbacks correctly before calling cpp_start_read.  */
1892   init_dependency_output (pfile);
1893
1894   /* After checking the environment variables, check if -M or -MM has
1895      not been specified, but other -M options have.  */
1896   if (CPP_OPTION (pfile, print_deps) == 0 &&
1897       (CPP_OPTION (pfile, print_deps_missing_files)
1898        || CPP_OPTION (pfile, deps_file)
1899        || CPP_OPTION (pfile, deps_phony_targets)))
1900     cpp_error (pfile, DL_FATAL,
1901                "you must additionally specify either -M or -MM");
1902 }
1903
1904 /* Set up dependency-file output.  On exit, if print_deps is non-zero
1905    then deps_file is not NULL; stdout is the empty string.  */
1906 static void
1907 init_dependency_output (pfile)
1908      cpp_reader *pfile;
1909 {
1910   char *spec, *s, *output_file;
1911
1912   /* Either of two environment variables can specify output of deps.
1913      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1914      where OUTPUT_FILE is the file to write deps info to
1915      and DEPS_TARGET is the target to mention in the deps.  */
1916
1917   if (CPP_OPTION (pfile, print_deps) == 0)
1918     {
1919       spec = getenv ("DEPENDENCIES_OUTPUT");
1920       if (spec)
1921         CPP_OPTION (pfile, print_deps) = 1;
1922       else
1923         {
1924           spec = getenv ("SUNPRO_DEPENDENCIES");
1925           if (spec)
1926             CPP_OPTION (pfile, print_deps) = 2;
1927           else
1928             return;
1929         }
1930
1931       /* Find the space before the DEPS_TARGET, if there is one.  */
1932       s = strchr (spec, ' ');
1933       if (s)
1934         {
1935           /* Let the caller perform MAKE quoting.  */
1936           deps_add_target (pfile->deps, s + 1, 0);
1937           output_file = (char *) xmalloc (s - spec + 1);
1938           memcpy (output_file, spec, s - spec);
1939           output_file[s - spec] = 0;
1940         }
1941       else
1942         output_file = spec;
1943
1944       /* Command line -MF overrides environment variables and default.  */
1945       if (CPP_OPTION (pfile, deps_file) == 0)
1946         CPP_OPTION (pfile, deps_file) = output_file;
1947
1948       CPP_OPTION (pfile, print_deps_append) = 1;
1949     }
1950   else if (CPP_OPTION (pfile, deps_file) == 0)
1951     /* If -M or -MM was seen without -MF, default output to wherever
1952        was specified with -o.  out_fname is non-NULL here.  */
1953     CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1954 }
1955
1956 /* Handle --help output.  */
1957 static void
1958 print_help ()
1959 {
1960   /* To keep the lines from getting too long for some compilers, limit
1961      to about 500 characters (6 lines) per chunk.  */
1962   fputs (_("\
1963 Switches:\n\
1964   -include <file>           Include the contents of <file> before other files\n\
1965   -imacros <file>           Accept definition of macros in <file>\n\
1966   -iprefix <path>           Specify <path> as a prefix for next two options\n\
1967   -iwithprefix <dir>        Add <dir> to the end of the system include path\n\
1968   -iwithprefixbefore <dir>  Add <dir> to the end of the main include path\n\
1969   -isystem <dir>            Add <dir> to the start of the system include path\n\
1970 "), stdout);
1971   fputs (_("\
1972   -idirafter <dir>          Add <dir> to the end of the system include path\n\
1973   -I <dir>                  Add <dir> to the end of the main include path\n\
1974   -I-                       Fine-grained include path control; see info docs\n\
1975   -nostdinc                 Do not search system include directories\n\
1976                              (dirs specified with -isystem will still be used)\n\
1977   -nostdinc++               Do not search system include directories for C++\n\
1978   -o <file>                 Put output into <file>\n\
1979 "), stdout);
1980   fputs (_("\
1981   -pedantic                 Issue all warnings demanded by strict ISO C\n\
1982   -pedantic-errors          Issue -pedantic warnings as errors instead\n\
1983   -trigraphs                Support ISO C trigraphs\n\
1984   -lang-c                   Assume that the input sources are in C\n\
1985   -lang-c89                 Assume that the input sources are in C89\n\
1986 "), stdout);
1987   fputs (_("\
1988   -lang-c++                 Assume that the input sources are in C++\n\
1989   -lang-objc                Assume that the input sources are in ObjectiveC\n\
1990   -lang-objc++              Assume that the input sources are in ObjectiveC++\n\
1991   -lang-asm                 Assume that the input sources are in assembler\n\
1992 "), stdout);
1993   fputs (_("\
1994   -std=<std name>           Specify the conformance standard; one of:\n\
1995                             gnu89, gnu99, c89, c99, iso9899:1990,\n\
1996                             iso9899:199409, iso9899:1999\n\
1997   -+                        Allow parsing of C++ style features\n\
1998   -w                        Inhibit warning messages\n\
1999   -Wtrigraphs               Warn if trigraphs are encountered\n\
2000   -Wno-trigraphs            Do not warn about trigraphs\n\
2001   -Wcomment{s}              Warn if one comment starts inside another\n\
2002 "), stdout);
2003   fputs (_("\
2004   -Wno-comment{s}           Do not warn about comments\n\
2005   -Wtraditional             Warn about features not present in traditional C\n\
2006   -Wno-traditional          Do not warn about traditional C\n\
2007   -Wundef                   Warn if an undefined macro is used by #if\n\
2008   -Wno-undef                Do not warn about testing undefined macros\n\
2009   -Wimport                  Warn about the use of the #import directive\n\
2010 "), stdout);
2011   fputs (_("\
2012   -Wno-import               Do not warn about the use of #import\n\
2013   -Werror                   Treat all warnings as errors\n\
2014   -Wno-error                Do not treat warnings as errors\n\
2015   -Wsystem-headers          Do not suppress warnings from system headers\n\
2016   -Wno-system-headers       Suppress warnings from system headers\n\
2017   -Wall                     Enable all preprocessor warnings\n\
2018 "), stdout);
2019   fputs (_("\
2020   -M                        Generate make dependencies\n\
2021   -MM                       As -M, but ignore system header files\n\
2022   -MD                       Generate make dependencies and compile\n\
2023   -MMD                      As -MD, but ignore system header files\n\
2024   -MF <file>                Write dependency output to the given file\n\
2025   -MG                       Treat missing header file as generated files\n\
2026 "), stdout);
2027   fputs (_("\
2028   -MP                       Generate phony targets for all headers\n\
2029   -MQ <target>              Add a MAKE-quoted target\n\
2030   -MT <target>              Add an unquoted target\n\
2031 "), stdout);
2032   fputs (_("\
2033   -D<macro>                 Define a <macro> with string '1' as its value\n\
2034   -D<macro>=<val>           Define a <macro> with <val> as its value\n\
2035   -A<question>=<answer>     Assert the <answer> to <question>\n\
2036   -A-<question>=<answer>    Disable the <answer> to <question>\n\
2037   -U<macro>                 Undefine <macro> \n\
2038   -v                        Display the version number\n\
2039 "), stdout);
2040   fputs (_("\
2041   -H                        Print the name of header files as they are used\n\
2042   -C                        Do not discard comments\n\
2043   -dM                       Display a list of macro definitions active at end\n\
2044   -dD                       Preserve macro definitions in output\n\
2045   -dN                       As -dD except that only the names are preserved\n\
2046   -dI                       Include #include directives in the output\n\
2047 "), stdout);
2048   fputs (_("\
2049   -fpreprocessed            Treat the input file as already preprocessed\n\
2050   -ftabstop=<number>        Distance between tab stops for column reporting\n\
2051   -P                        Do not generate #line directives\n\
2052   -$                        Do not allow '$' in identifiers\n\
2053   -remap                    Remap file names when including files\n\
2054   --version                 Display version information\n\
2055   -h or --help              Display this information\n\
2056 "), stdout);
2057 }