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