OSDN Git Service

* c-common.h (enum c_language_kind): Emphasize that clk_c is 0.
[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
32 /* Windows does not natively support inodes, and neither does MSDOS.
33    Cygwin's emulation can generate non-unique inodes, so don't use it.
34    VMS has non-numeric inodes.  */
35 #ifdef VMS
36 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
37 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
38 #else
39 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
40 #  define INO_T_EQ(A, B) 0
41 # else
42 #  define INO_T_EQ(A, B) ((A) == (B))
43 # endif
44 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
45 #endif
46
47 /* Internal structures and prototypes.  */
48
49 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
50    -imacros switch.  */
51 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
52 struct pending_option
53 {
54   struct pending_option *next;
55   const char *arg;
56   cl_directive_handler handler;
57 };
58
59 /* The `pending' structure accumulates all the options that are not
60    actually processed until we hit cpp_read_main_file.  It consists of
61    several lists, one for each type of option.  We keep both head and
62    tail pointers for quick insertion.  */
63 struct cpp_pending
64 {
65   struct pending_option *directive_head, *directive_tail;
66
67   struct search_path *quote_head, *quote_tail;
68   struct search_path *brack_head, *brack_tail;
69   struct search_path *systm_head, *systm_tail;
70   struct search_path *after_head, *after_tail;
71
72   struct pending_option *imacros_head, *imacros_tail;
73   struct pending_option *include_head, *include_tail;
74 };
75
76 #ifdef __STDC__
77 #define APPEND(pend, list, elt) \
78   do {  if (!(pend)->list##_head) (pend)->list##_head = (elt); \
79         else (pend)->list##_tail->next = (elt); \
80         (pend)->list##_tail = (elt); \
81   } while (0)
82 #else
83 #define APPEND(pend, list, elt) \
84   do {  if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
85         else (pend)->list/**/_tail->next = (elt); \
86         (pend)->list/**/_tail = (elt); \
87   } while (0)
88 #endif
89
90 static void path_include                PARAMS ((cpp_reader *,
91                                                  char *, int));
92 static void init_library                PARAMS ((void));
93 static void init_builtins               PARAMS ((cpp_reader *));
94 static void mark_named_operators        PARAMS ((cpp_reader *));
95 static void append_include_chain        PARAMS ((cpp_reader *,
96                                                  char *, int, int));
97 static struct search_path * remove_dup_dir      PARAMS ((cpp_reader *,
98                                                  struct search_path *));
99 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
100                                                  struct search_path *));
101 static void merge_include_chains        PARAMS ((cpp_reader *));
102 static bool push_include                PARAMS ((cpp_reader *,
103                                                  struct pending_option *));
104 static void free_chain                  PARAMS ((struct pending_option *));
105 static void init_dependency_output      PARAMS ((cpp_reader *));
106 static void init_standard_includes      PARAMS ((cpp_reader *));
107 static void read_original_filename      PARAMS ((cpp_reader *));
108 static void new_pending_directive       PARAMS ((struct cpp_pending *,
109                                                  const char *,
110                                                  cl_directive_handler));
111 static void output_deps                 PARAMS ((cpp_reader *));
112 static int parse_option                 PARAMS ((const char *));
113
114 /* Fourth argument to append_include_chain: chain to use.
115    Note it's never asked to append to the quote chain.  */
116 enum { BRACKET = 0, SYSTEM, AFTER };
117
118 /* If we have designated initializers (GCC >2.7) these tables can be
119    initialized, constant data.  Otherwise, they have to be filled in at
120    runtime.  */
121 #if HAVE_DESIGNATED_INITIALIZERS
122
123 #define init_trigraph_map()  /* Nothing.  */
124 #define TRIGRAPH_MAP \
125 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
126
127 #define END };
128 #define s(p, v) [p] = v,
129
130 #else
131
132 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
133  static void init_trigraph_map PARAMS ((void)) { \
134  unsigned char *x = _cpp_trigraph_map;
135
136 #define END }
137 #define s(p, v) x[p] = v;
138
139 #endif
140
141 TRIGRAPH_MAP
142   s('=', '#')   s(')', ']')     s('!', '|')
143   s('(', '[')   s('\'', '^')    s('>', '}')
144   s('/', '\\')  s('<', '{')     s('-', '~')
145 END
146
147 #undef s
148 #undef END
149 #undef TRIGRAPH_MAP
150
151 /* Given a colon-separated list of file names PATH,
152    add all the names to the search path for include files.  */
153 static void
154 path_include (pfile, list, path)
155      cpp_reader *pfile;
156      char *list;
157      int path;
158 {
159   char *p, *q, *name;
160
161   p = list;
162
163   do
164     {
165       /* Find the end of this name.  */
166       q = p;
167       while (*q != 0 && *q != PATH_SEPARATOR) q++;
168       if (q == p)
169         {
170           /* An empty name in the path stands for the current directory.  */
171           name = (char *) xmalloc (2);
172           name[0] = '.';
173           name[1] = 0;
174         }
175       else
176         {
177           /* Otherwise use the directory that is named.  */
178           name = (char *) xmalloc (q - p + 1);
179           memcpy (name, p, q - p);
180           name[q - p] = 0;
181         }
182
183       append_include_chain (pfile, name, path, 0);
184
185       /* Advance past this name.  */
186       if (*q == 0)
187         break;
188       p = q + 1;
189     }
190   while (1);
191 }
192
193 /* Append DIR to include path PATH.  DIR must be allocated on the
194    heap; this routine takes responsibility for freeing it.  CXX_AWARE
195    is non-zero if the header contains extern "C" guards for C++,
196    otherwise it is zero.  */
197 static void
198 append_include_chain (pfile, dir, path, cxx_aware)
199      cpp_reader *pfile;
200      char *dir;
201      int path;
202      int cxx_aware;
203 {
204   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
205   struct search_path *new;
206   struct stat st;
207   unsigned int len;
208
209   if (*dir == '\0')
210     {
211       free (dir);
212       dir = xstrdup (".");
213     }
214   _cpp_simplify_pathname (dir);
215
216   if (stat (dir, &st))
217     {
218       /* Dirs that don't exist are silently ignored.  */
219       if (errno != ENOENT)
220         cpp_errno (pfile, DL_ERROR, dir);
221       else if (CPP_OPTION (pfile, verbose))
222         fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
223       free (dir);
224       return;
225     }
226
227   if (!S_ISDIR (st.st_mode))
228     {
229       cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
230       free (dir);
231       return;
232     }
233
234   len = strlen (dir);
235   if (len > pfile->max_include_len)
236     pfile->max_include_len = len;
237
238   new = (struct search_path *) xmalloc (sizeof (struct search_path));
239   new->name = dir;
240   new->len = len;
241   INO_T_COPY (new->ino, st.st_ino);
242   new->dev  = st.st_dev;
243   /* Both systm and after include file lists should be treated as system
244      include files since these two lists are really just a concatenation
245      of one "system" list.  */
246   if (path == SYSTEM || path == AFTER)
247     new->sysp = cxx_aware ? 1 : 2;
248   else
249     new->sysp = 0;
250   new->name_map = NULL;
251   new->next = NULL;
252
253   switch (path)
254     {
255     case BRACKET:       APPEND (pend, brack, new); break;
256     case SYSTEM:        APPEND (pend, systm, new); break;
257     case AFTER:         APPEND (pend, after, new); break;
258     }
259 }
260
261 /* Handle a duplicated include path.  PREV is the link in the chain
262    before the duplicate.  The duplicate is removed from the chain and
263    freed.  Returns PREV.  */
264 static struct search_path *
265 remove_dup_dir (pfile, prev)
266      cpp_reader *pfile;
267      struct search_path *prev;
268 {
269   struct search_path *cur = prev->next;
270
271   if (CPP_OPTION (pfile, verbose))
272     fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
273
274   prev->next = cur->next;
275   free ((PTR) cur->name);
276   free (cur);
277
278   return prev;
279 }
280
281 /* Remove duplicate directories from a chain.  Returns the tail of the
282    chain, or NULL if the chain is empty.  This algorithm is quadratic
283    in the number of -I switches, which is acceptable since there
284    aren't usually that many of them.  */
285 static struct search_path *
286 remove_dup_dirs (pfile, head)
287      cpp_reader *pfile;
288      struct search_path *head;
289 {
290   struct search_path *prev = NULL, *cur, *other;
291
292   for (cur = head; cur; cur = cur->next)
293     {
294       for (other = head; other != cur; other = other->next)
295         if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
296           {
297             if (cur->sysp && !other->sysp)
298               {
299                 cpp_error (pfile, DL_WARNING,
300                            "changing search order for system directory \"%s\"",
301                            cur->name);
302                 if (strcmp (cur->name, other->name))
303                   cpp_error (pfile, DL_WARNING,
304                              "  as it is the same as non-system directory \"%s\"",
305                              other->name);
306                 else
307                   cpp_error (pfile, DL_WARNING,
308                              "  as it has already been specified as a non-system directory");
309               }
310             cur = remove_dup_dir (pfile, prev);
311             break;
312           }
313       prev = cur;
314     }
315
316   return prev;
317 }
318
319 /* Merge the four include chains together in the order quote, bracket,
320    system, after.  Remove duplicate dirs (as determined by
321    INO_T_EQ()).  The system_include and after_include chains are never
322    referred to again after this function; all access is through the
323    bracket_include path.  */
324 static void
325 merge_include_chains (pfile)
326      cpp_reader *pfile;
327 {
328   struct search_path *quote, *brack, *systm, *qtail;
329
330   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
331
332   quote = pend->quote_head;
333   brack = pend->brack_head;
334   systm = pend->systm_head;
335   qtail = pend->quote_tail;
336
337   /* Paste together bracket, system, and after include chains.  */
338   if (systm)
339     pend->systm_tail->next = pend->after_head;
340   else
341     systm = pend->after_head;
342
343   if (brack)
344     pend->brack_tail->next = systm;
345   else
346     brack = systm;
347
348   /* This is a bit tricky.  First we drop dupes from the quote-include
349      list.  Then we drop dupes from the bracket-include list.
350      Finally, if qtail and brack are the same directory, we cut out
351      brack and move brack up to point to qtail.
352
353      We can't just merge the lists and then uniquify them because
354      then we may lose directories from the <> search path that should
355      be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
356      safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
357      -Ibar -I- -Ifoo -Iquux.  */
358
359   remove_dup_dirs (pfile, brack);
360   qtail = remove_dup_dirs (pfile, quote);
361
362   if (quote)
363     {
364       qtail->next = brack;
365
366       /* If brack == qtail, remove brack as it's simpler.  */
367       if (brack && INO_T_EQ (qtail->ino, brack->ino)
368           && qtail->dev == brack->dev)
369         brack = remove_dup_dir (pfile, qtail);
370     }
371   else
372     quote = brack;
373
374   CPP_OPTION (pfile, quote_include) = quote;
375   CPP_OPTION (pfile, bracket_include) = brack;
376 }
377
378 /* A set of booleans indicating what CPP features each source language
379    requires.  */
380 struct lang_flags
381 {
382   char c99;
383   char cplusplus;
384   char extended_numbers;
385   char std;
386   char dollars_in_ident;
387   char cplusplus_comments;
388   char digraphs;
389 };
390
391 /* ??? Enable $ in identifiers in assembly? */
392 static const struct lang_flags lang_defaults[] =
393 { /*              c99 c++ xnum std dollar c++comm digr  */
394   /* GNUC89 */  { 0,  0,  1,   0,   1,     1,      1     },
395   /* GNUC99 */  { 1,  0,  1,   0,   1,     1,      1     },
396   /* STDC89 */  { 0,  0,  0,   1,   0,     0,      0     },
397   /* STDC94 */  { 0,  0,  0,   1,   0,     0,      1     },
398   /* STDC99 */  { 1,  0,  1,   1,   0,     1,      1     },
399   /* GNUCXX */  { 0,  1,  1,   0,   1,     1,      1     },
400   /* CXX98  */  { 0,  1,  1,   1,   0,     1,      1     },
401   /* ASM    */  { 0,  0,  1,   0,   0,     1,      0     }
402 };
403
404 /* Sets internal flags correctly for a given language.  */
405 void
406 cpp_set_lang (pfile, lang)
407      cpp_reader *pfile;
408      enum c_lang lang;
409 {
410   const struct lang_flags *l = &lang_defaults[(int) lang];
411
412   CPP_OPTION (pfile, lang) = lang;
413
414   CPP_OPTION (pfile, c99)                = l->c99;
415   CPP_OPTION (pfile, cplusplus)          = l->cplusplus;
416   CPP_OPTION (pfile, extended_numbers)   = l->extended_numbers;
417   CPP_OPTION (pfile, std)                = l->std;
418   CPP_OPTION (pfile, trigraphs)          = l->std;
419   CPP_OPTION (pfile, dollars_in_ident)   = l->dollars_in_ident;
420   CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
421   CPP_OPTION (pfile, digraphs)           = l->digraphs;
422 }
423
424 #ifdef HOST_EBCDIC
425 static int opt_comp PARAMS ((const void *, const void *));
426
427 /* Run-time sorting of options array.  */
428 static int
429 opt_comp (p1, p2)
430      const void *p1, *p2;
431 {
432   return strcmp (((struct cl_option *) p1)->opt_text,
433                  ((struct cl_option *) p2)->opt_text);
434 }
435 #endif
436
437 /* init initializes library global state.  It might not need to
438    do anything depending on the platform and compiler.  */
439 static void
440 init_library ()
441 {
442   static int initialized = 0;
443
444   if (! initialized)
445     {
446       initialized = 1;
447
448 #ifdef HOST_EBCDIC
449       /* For non-ASCII hosts, the cl_options array needs to be sorted at
450          runtime.  */
451       qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
452 #endif
453
454       /* Set up the trigraph map.  This doesn't need to do anything if
455          we were compiled with a compiler that supports C99 designated
456          initializers.  */
457       init_trigraph_map ();
458     }
459 }
460
461 /* Initialize a cpp_reader structure.  */
462 cpp_reader *
463 cpp_create_reader (lang)
464      enum c_lang lang;
465 {
466   cpp_reader *pfile;
467
468   /* Initialise this instance of the library if it hasn't been already.  */
469   init_library ();
470
471   pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
472
473   cpp_set_lang (pfile, lang);
474   CPP_OPTION (pfile, warn_import) = 1;
475   CPP_OPTION (pfile, warn_multichar) = 1;
476   CPP_OPTION (pfile, discard_comments) = 1;
477   CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
478   CPP_OPTION (pfile, show_column) = 1;
479   CPP_OPTION (pfile, tabstop) = 8;
480   CPP_OPTION (pfile, operator_names) = 1;
481   CPP_OPTION (pfile, warn_endif_labels) = 1;
482
483   CPP_OPTION (pfile, pending) =
484     (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
485
486   /* Default CPP arithmetic to something sensible for the host for the
487      benefit of dumb users like fix-header.  */
488   CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
489   CPP_OPTION (pfile, char_precision) = CHAR_BIT;
490   CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
491   CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
492   CPP_OPTION (pfile, unsigned_char) = 0;
493   CPP_OPTION (pfile, unsigned_wchar) = 1;
494
495   /* It's simplest to just create this struct whether or not it will
496      be needed.  */
497   pfile->deps = deps_init ();
498
499   /* Initialise the line map.  Start at logical line 1, so we can use
500      a line number of zero for special states.  */
501   init_line_maps (&pfile->line_maps);
502   pfile->line = 1;
503
504   /* Initialize lexer state.  */
505   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
506
507   /* Set up static tokens.  */
508   pfile->avoid_paste.type = CPP_PADDING;
509   pfile->avoid_paste.val.source = NULL;
510   pfile->eof.type = CPP_EOF;
511   pfile->eof.flags = 0;
512
513   /* Create a token buffer for the lexer.  */
514   _cpp_init_tokenrun (&pfile->base_run, 250);
515   pfile->cur_run = &pfile->base_run;
516   pfile->cur_token = pfile->base_run.base;
517
518   /* Initialise the base context.  */
519   pfile->context = &pfile->base_context;
520   pfile->base_context.macro = 0;
521   pfile->base_context.prev = pfile->base_context.next = 0;
522
523   /* Aligned and unaligned storage.  */
524   pfile->a_buff = _cpp_get_buff (pfile, 0);
525   pfile->u_buff = _cpp_get_buff (pfile, 0);
526
527   /* The expression parser stack.  */
528   _cpp_expand_op_stack (pfile);
529
530   /* Initialise the buffer obstack.  */
531   gcc_obstack_init (&pfile->buffer_ob);
532
533   _cpp_init_includes (pfile);
534
535   return pfile;
536 }
537
538 /* Free resources used by PFILE.  Accessing PFILE after this function
539    returns leads to undefined behaviour.  Returns the error count.  */
540 int
541 cpp_destroy (pfile)
542      cpp_reader *pfile;
543 {
544   int result;
545   struct search_path *dir, *dirn;
546   cpp_context *context, *contextn;
547   tokenrun *run, *runn;
548
549   free_chain (CPP_OPTION (pfile, pending)->include_head);
550   free (CPP_OPTION (pfile, pending));
551   free (pfile->op_stack);
552
553   while (CPP_BUFFER (pfile) != NULL)
554     _cpp_pop_buffer (pfile);
555
556   if (pfile->out.base)
557     free (pfile->out.base);
558
559   if (pfile->macro_buffer)
560     {
561       free ((PTR) pfile->macro_buffer);
562       pfile->macro_buffer = NULL;
563       pfile->macro_buffer_len = 0;
564     }
565
566   deps_free (pfile->deps);
567   obstack_free (&pfile->buffer_ob, 0);
568
569   _cpp_destroy_hashtable (pfile);
570   _cpp_cleanup_includes (pfile);
571
572   _cpp_free_buff (pfile->a_buff);
573   _cpp_free_buff (pfile->u_buff);
574   _cpp_free_buff (pfile->free_buffs);
575
576   for (run = &pfile->base_run; run; run = runn)
577     {
578       runn = run->next;
579       free (run->base);
580       if (run != &pfile->base_run)
581         free (run);
582     }
583
584   for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
585     {
586       dirn = dir->next;
587       free ((PTR) dir->name);
588       free (dir);
589     }
590
591   for (context = pfile->base_context.next; context; context = contextn)
592     {
593       contextn = context->next;
594       free (context);
595     }
596
597   free_line_maps (&pfile->line_maps);
598
599   result = pfile->errors;
600   free (pfile);
601
602   return result;
603 }
604
605 /* This structure defines one built-in identifier.  A node will be
606    entered in the hash table under the name NAME, with value VALUE.
607
608    There are two tables of these.  builtin_array holds all the
609    "builtin" macros: these are handled by builtin_macro() in
610    cppmacro.c.  Builtin is somewhat of a misnomer -- the property of
611    interest is that these macros require special code to compute their
612    expansions.  The value is a "builtin_type" enumerator.
613
614    operator_array holds the C++ named operators.  These are keywords
615    which act as aliases for punctuators.  In C++, they cannot be
616    altered through #define, and #if recognizes them as operators.  In
617    C, these are not entered into the hash table at all (but see
618    <iso646.h>).  The value is a token-type enumerator.  */
619 struct builtin
620 {
621   const uchar *name;
622   unsigned short len;
623   unsigned short value;
624 };
625
626 #define B(n, t)    { DSC(n), t }
627 static const struct builtin builtin_array[] =
628 {
629   B("__TIME__",          BT_TIME),
630   B("__DATE__",          BT_DATE),
631   B("__FILE__",          BT_FILE),
632   B("__BASE_FILE__",     BT_BASE_FILE),
633   B("__LINE__",          BT_SPECLINE),
634   B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
635   /* Keep builtins not used for -traditional-cpp at the end, and
636      update init_builtins() if any more are added.  */
637   B("_Pragma",           BT_PRAGMA),
638   B("__STDC__",          BT_STDC),
639 };
640
641 static const struct builtin operator_array[] =
642 {
643   B("and",      CPP_AND_AND),
644   B("and_eq",   CPP_AND_EQ),
645   B("bitand",   CPP_AND),
646   B("bitor",    CPP_OR),
647   B("compl",    CPP_COMPL),
648   B("not",      CPP_NOT),
649   B("not_eq",   CPP_NOT_EQ),
650   B("or",       CPP_OR_OR),
651   B("or_eq",    CPP_OR_EQ),
652   B("xor",      CPP_XOR),
653   B("xor_eq",   CPP_XOR_EQ)
654 };
655 #undef B
656
657 /* Mark the C++ named operators in the hash table.  */
658 static void
659 mark_named_operators (pfile)
660      cpp_reader *pfile;
661 {
662   const struct builtin *b;
663
664   for (b = operator_array;
665        b < (operator_array + ARRAY_SIZE (operator_array));
666        b++)
667     {
668       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
669       hp->flags |= NODE_OPERATOR;
670       hp->value.operator = b->value;
671     }
672 }
673
674 /* Subroutine of cpp_read_main_file; reads the builtins table above and
675    enters them, and language-specific macros, into the hash table.  */
676 static void
677 init_builtins (pfile)
678      cpp_reader *pfile;
679 {
680   const struct builtin *b;
681   size_t n = ARRAY_SIZE (builtin_array);
682
683   if (CPP_OPTION (pfile, traditional))
684     n -= 2;
685
686   for(b = builtin_array; b < builtin_array + n; b++)
687     {
688       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
689       hp->type = NT_MACRO;
690       hp->flags |= NODE_BUILTIN | NODE_WARN;
691       hp->value.builtin = b->value;
692     }
693
694   if (CPP_OPTION (pfile, cplusplus))
695     _cpp_define_builtin (pfile, "__cplusplus 1");
696   else if (CPP_OPTION (pfile, lang) == CLK_ASM)
697     _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
698   else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
699     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
700   else if (CPP_OPTION (pfile, c99))
701     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
702
703   if (CPP_OPTION (pfile, objc))
704     _cpp_define_builtin (pfile, "__OBJC__ 1");
705
706   if (pfile->cb.register_builtins)
707     (*pfile->cb.register_builtins) (pfile);
708 }
709
710 /* And another subroutine.  This one sets up the standard include path.  */
711 static void
712 init_standard_includes (pfile)
713      cpp_reader *pfile;
714 {
715   char *path;
716   const struct default_include *p;
717   const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
718
719   /* Several environment variables may add to the include search path.
720      CPATH specifies an additional list of directories to be searched
721      as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
722      etc. specify an additional list of directories to be searched as
723      if specified with -isystem, for the language indicated.  */
724
725   GET_ENVIRONMENT (path, "CPATH");
726   if (path != 0 && *path != 0)
727     path_include (pfile, path, BRACKET);
728
729   switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
730     {
731     case 0:
732       GET_ENVIRONMENT (path, "C_INCLUDE_PATH");
733       break;
734     case 1:
735       GET_ENVIRONMENT (path, "CPLUS_INCLUDE_PATH");
736       break;
737     case 2:
738       GET_ENVIRONMENT (path, "OBJC_INCLUDE_PATH");
739       break;
740     case 3:
741       GET_ENVIRONMENT (path, "OBJCPLUS_INCLUDE_PATH");
742       break;
743     }
744   if (path != 0 && *path != 0)
745     path_include (pfile, path, SYSTEM);
746
747   /* Search "translated" versions of GNU directories.
748      These have /usr/local/lib/gcc... replaced by specd_prefix.  */
749   if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
750     {
751       /* Remove the `include' from /usr/local/lib/gcc.../include.
752          GCC_INCLUDE_DIR will always end in /include.  */
753       int default_len = cpp_GCC_INCLUDE_DIR_len;
754       char *default_prefix = (char *) alloca (default_len + 1);
755       int specd_len = strlen (specd_prefix);
756
757       memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
758       default_prefix[default_len] = '\0';
759
760       for (p = cpp_include_defaults; p->fname; p++)
761         {
762           /* Some standard dirs are only for C++.  */
763           if (!p->cplusplus
764               || (CPP_OPTION (pfile, cplusplus)
765                   && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
766             {
767               /* Does this dir start with the prefix?  */
768               if (!memcmp (p->fname, default_prefix, default_len))
769                 {
770                   /* Yes; change prefix and add to search list.  */
771                   int flen = strlen (p->fname);
772                   int this_len = specd_len + flen - default_len;
773                   char *str = (char *) xmalloc (this_len + 1);
774                   memcpy (str, specd_prefix, specd_len);
775                   memcpy (str + specd_len,
776                           p->fname + default_len,
777                           flen - default_len + 1);
778
779                   append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
780                 }
781             }
782         }
783     }
784
785   /* Search ordinary names for GNU include directories.  */
786   for (p = cpp_include_defaults; p->fname; p++)
787     {
788       /* Some standard dirs are only for C++.  */
789       if (!p->cplusplus
790           || (CPP_OPTION (pfile, cplusplus)
791               && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
792         {
793           char *str = update_path (p->fname, p->component);
794           append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
795         }
796     }
797 }
798
799 /* Pushes a command line -imacro and -include file indicated by P onto
800    the buffer stack.  Returns non-zero if successful.  */
801 static bool
802 push_include (pfile, p)
803      cpp_reader *pfile;
804      struct pending_option *p;
805 {
806   cpp_token header;
807
808   /* Later: maybe update this to use the #include "" search path
809      if cpp_read_file fails.  */
810   header.type = CPP_STRING;
811   header.val.str.text = (const unsigned char *) p->arg;
812   header.val.str.len = strlen (p->arg);
813   /* Make the command line directive take up a line.  */
814   pfile->line++;
815
816   return _cpp_execute_include (pfile, &header, IT_CMDLINE);
817 }
818
819 /* Frees a pending_option chain.  */
820 static void
821 free_chain (head)
822      struct pending_option *head;
823 {
824   struct pending_option *next;
825
826   while (head)
827     {
828       next = head->next;
829       free (head);
830       head = next;
831     }
832 }
833
834 /* Sanity-checks are dependent on command-line options, so it is
835    called as a subroutine of cpp_read_main_file ().  */
836 #if ENABLE_CHECKING
837 static void sanity_checks PARAMS ((cpp_reader *));
838 static void sanity_checks (pfile)
839      cpp_reader *pfile;
840 {
841   cppchar_t test = 0;
842   size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
843
844   /* Sanity checks for assumptions about CPP arithmetic and target
845      type precisions made by cpplib.  */
846   test--;
847   if (test < 1)
848     cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
849
850   if (CPP_OPTION (pfile, precision) > max_precision)
851     cpp_error (pfile, DL_ICE,
852                "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
853                (unsigned long) max_precision,
854                (unsigned long) CPP_OPTION (pfile, precision));
855
856   if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
857     cpp_error (pfile, DL_ICE,
858                "CPP arithmetic must be at least as precise as a target int");
859
860   if (CPP_OPTION (pfile, char_precision) < 8)
861     cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
862
863   if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
864     cpp_error (pfile, DL_ICE,
865                "target wchar_t is narrower than target char");
866
867   if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
868     cpp_error (pfile, DL_ICE,
869                "target int is narrower than target char");
870
871   /* This is assumed in eval_token() and could be fixed if necessary.  */
872   if (sizeof (cppchar_t) > sizeof (cpp_num_part))
873     cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
874
875   if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
876     cpp_error (pfile, DL_ICE,
877                "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
878                (unsigned long) BITS_PER_CPPCHAR_T,
879                (unsigned long) CPP_OPTION (pfile, wchar_precision));
880 }
881 #else
882 # define sanity_checks(PFILE)
883 #endif
884
885 /* This is called after options have been parsed, and partially
886    processed.  Setup for processing input from the file named FNAME,
887    or stdin if it is the empty string.  Return the original filename
888    on success (e.g. foo.i->foo.c), or NULL on failure.  */
889 const char *
890 cpp_read_main_file (pfile, fname, table)
891      cpp_reader *pfile;
892      const char *fname;
893      hash_table *table;
894 {
895   sanity_checks (pfile);
896
897   /* The front ends don't set up the hash table until they have
898      finished processing the command line options, so initializing the
899      hashtable is deferred until now.  */
900   _cpp_init_hashtable (pfile, table);
901
902   /* Set up the include search path now.  */
903   if (! CPP_OPTION (pfile, no_standard_includes))
904     init_standard_includes (pfile);
905
906   merge_include_chains (pfile);
907
908   /* With -v, print the list of dirs to search.  */
909   if (CPP_OPTION (pfile, verbose))
910     {
911       struct search_path *l;
912       fprintf (stderr, _("#include \"...\" search starts here:\n"));
913       for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
914         {
915           if (l == CPP_OPTION (pfile, bracket_include))
916             fprintf (stderr, _("#include <...> search starts here:\n"));
917           fprintf (stderr, " %s\n", l->name);
918         }
919       fprintf (stderr, _("End of search list.\n"));
920     }
921
922   if (CPP_OPTION (pfile, print_deps))
923     /* Set the default target (if there is none already).  */
924     deps_add_default_target (pfile->deps, fname);
925
926   /* Open the main input file.  */
927   if (!_cpp_read_file (pfile, fname))
928     return NULL;
929
930   /* Set this after cpp_post_options so the client can change the
931      option if it wishes, and after stacking the main file so we don't
932      trace the main file.  */
933   pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
934
935   /* For foo.i, read the original filename foo.c now, for the benefit
936      of the front ends.  */
937   if (CPP_OPTION (pfile, preprocessed))
938     read_original_filename (pfile);
939   /* Overlay an empty buffer to seed traditional preprocessing.  */
940   else if (CPP_OPTION (pfile, traditional)
941            && !CPP_OPTION (pfile, preprocess_only))
942     _cpp_overlay_buffer (pfile, U"", 0);
943
944   return pfile->map->to_file;
945 }
946
947 /* For preprocessed files, if the first tokens are of the form # NUM.
948    handle the directive so we know the original file name.  This will
949    generate file_change callbacks, which the front ends must handle
950    appropriately given their state of initialization.  */
951 static void
952 read_original_filename (pfile)
953      cpp_reader *pfile;
954 {
955   const cpp_token *token, *token1;
956
957   /* Lex ahead; if the first tokens are of the form # NUM, then
958      process the directive, otherwise back up.  */
959   token = _cpp_lex_direct (pfile);
960   if (token->type == CPP_HASH)
961     {
962       token1 = _cpp_lex_direct (pfile);
963       _cpp_backup_tokens (pfile, 1);
964
965       /* If it's a #line directive, handle it.  */
966       if (token1->type == CPP_NUMBER)
967         {
968           _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
969           return;
970         }
971     }
972
973   /* Backup as if nothing happened.  */
974   _cpp_backup_tokens (pfile, 1);
975 }
976
977 /* Handle pending command line options: -D, -U, -A, -imacros and
978    -include.  This should be called after debugging has been properly
979    set up in the front ends.  */
980 void
981 cpp_finish_options (pfile)
982      cpp_reader *pfile;
983 {
984   /* Mark named operators before handling command line macros.  */
985   if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
986     mark_named_operators (pfile);
987
988   /* Install builtins and process command line macros etc. in the order
989      they appeared, but only if not already preprocessed.  */
990   if (! CPP_OPTION (pfile, preprocessed))
991     {
992       struct pending_option *p;
993
994       _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
995       init_builtins (pfile);
996       _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
997       for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
998         (*p->handler) (pfile, p->arg);
999
1000       /* Scan -imacros files after -D, -U, but before -include.
1001          pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1002          push -include files.  */
1003       for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1004         if (push_include (pfile, p))
1005           cpp_scan_nooutput (pfile);
1006
1007       pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1008       _cpp_maybe_push_include_file (pfile);
1009     }
1010
1011   pfile->first_unused_line = pfile->line;
1012
1013   free_chain (CPP_OPTION (pfile, pending)->imacros_head);
1014   free_chain (CPP_OPTION (pfile, pending)->directive_head);
1015 }
1016
1017 /* Push the next buffer on the stack given by -include, if any.  */
1018 void
1019 _cpp_maybe_push_include_file (pfile)
1020      cpp_reader *pfile;
1021 {
1022   if (pfile->next_include_file)
1023     {
1024       struct pending_option *head = *pfile->next_include_file;
1025
1026       while (head && !push_include (pfile, head))
1027         head = head->next;
1028
1029       if (head)
1030         pfile->next_include_file = &head->next;
1031       else
1032         {
1033           /* All done; restore the line map from <command line>.  */
1034           _cpp_do_file_change (pfile, LC_RENAME,
1035                                pfile->line_maps.maps[0].to_file, 1, 0);
1036           /* Don't come back here again.  */
1037           pfile->next_include_file = NULL;
1038         }
1039     }
1040 }
1041
1042 /* Use mkdeps.c to output dependency information.  */
1043 static void
1044 output_deps (pfile)
1045      cpp_reader *pfile;
1046 {
1047   /* Stream on which to print the dependency information.  */
1048   FILE *deps_stream = 0;
1049   const char *const deps_mode =
1050     CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1051
1052   if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1053     deps_stream = stdout;
1054   else
1055     {
1056       deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1057       if (deps_stream == 0)
1058         {
1059           cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
1060           return;
1061         }
1062     }
1063
1064   deps_write (pfile->deps, deps_stream, 72);
1065
1066   if (CPP_OPTION (pfile, deps_phony_targets))
1067     deps_phony_targets (pfile->deps, deps_stream);
1068
1069   /* Don't close stdout.  */
1070   if (deps_stream != stdout)
1071     {
1072       if (ferror (deps_stream) || fclose (deps_stream) != 0)
1073         cpp_error (pfile, DL_ERROR, "I/O error on output");
1074     }
1075 }
1076
1077 /* This is called at the end of preprocessing.  It pops the
1078    last buffer and writes dependency output.  It should also
1079    clear macro definitions, such that you could call cpp_start_read
1080    with a new filename to restart processing.  */
1081 void
1082 cpp_finish (pfile)
1083      cpp_reader *pfile;
1084 {
1085   /* Warn about unused macros before popping the final buffer.  */
1086   if (CPP_OPTION (pfile, warn_unused_macros))
1087     cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
1088
1089   /* cpplex.c leaves the final buffer on the stack.  This it so that
1090      it returns an unending stream of CPP_EOFs to the client.  If we
1091      popped the buffer, we'd dereference a NULL buffer pointer and
1092      segfault.  It's nice to allow the client to do worry-free excess
1093      cpp_get_token calls.  */
1094   while (pfile->buffer)
1095     _cpp_pop_buffer (pfile);
1096
1097   /* Don't write the deps file if preprocessing has failed.  */
1098   if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1099     output_deps (pfile);
1100
1101   /* Report on headers that could use multiple include guards.  */
1102   if (CPP_OPTION (pfile, print_include_names))
1103     _cpp_report_missing_guards (pfile);
1104 }
1105
1106 /* Add a directive to be handled later in the initialization phase.  */
1107 static void
1108 new_pending_directive (pend, text, handler)
1109      struct cpp_pending *pend;
1110      const char *text;
1111      cl_directive_handler handler;
1112 {
1113   struct pending_option *o = (struct pending_option *)
1114     xmalloc (sizeof (struct pending_option));
1115
1116   o->arg = text;
1117   o->next = NULL;
1118   o->handler = handler;
1119   APPEND (pend, directive, o);
1120 }
1121
1122 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1123    I.e. a const string initializer with parens around it.  That is
1124    what N_("string") resolves to, so we make no_* be macros instead.  */
1125 #define no_ass N_("assertion missing after %s")
1126 #define no_dir N_("directory name missing after %s")
1127 #define no_fil N_("file name missing after %s")
1128 #define no_mac N_("macro name missing after %s")
1129 #define no_pth N_("path name missing after %s")
1130 #define no_tgt N_("target missing after %s")
1131
1132 /* This is the list of all command line options, with the leading
1133    "-" removed.  It must be sorted in ASCII collating order.  */
1134 #define COMMAND_LINE_OPTIONS                                                  \
1135   DEF_OPT("A",                        no_ass, OPT_A)                          \
1136   DEF_OPT("D",                        no_mac, OPT_D)                          \
1137   DEF_OPT("I",                        no_dir, OPT_I)                          \
1138   DEF_OPT("M",                        0,      OPT_M)                          \
1139   DEF_OPT("MD",                       no_fil, OPT_MD)                         \
1140   DEF_OPT("MF",                       no_fil, OPT_MF)                         \
1141   DEF_OPT("MG",                       0,      OPT_MG)                         \
1142   DEF_OPT("MM",                       0,      OPT_MM)                         \
1143   DEF_OPT("MMD",                      no_fil, OPT_MMD)                        \
1144   DEF_OPT("MP",                       0,      OPT_MP)                         \
1145   DEF_OPT("MQ",                       no_tgt, OPT_MQ)                         \
1146   DEF_OPT("MT",                       no_tgt, OPT_MT)                         \
1147   DEF_OPT("U",                        no_mac, OPT_U)                          \
1148   DEF_OPT("idirafter",                no_dir, OPT_idirafter)                  \
1149   DEF_OPT("imacros",                  no_fil, OPT_imacros)                    \
1150   DEF_OPT("include",                  no_fil, OPT_include)                    \
1151   DEF_OPT("iprefix",                  no_pth, OPT_iprefix)                    \
1152   DEF_OPT("isystem",                  no_dir, OPT_isystem)                    \
1153   DEF_OPT("iwithprefix",              no_dir, OPT_iwithprefix)                \
1154   DEF_OPT("iwithprefixbefore",        no_dir, OPT_iwithprefixbefore)
1155
1156 #define DEF_OPT(text, msg, code) code,
1157 enum opt_code
1158 {
1159   COMMAND_LINE_OPTIONS
1160   N_OPTS
1161 };
1162 #undef DEF_OPT
1163
1164 struct cl_option
1165 {
1166   const char *opt_text;
1167   const char *msg;
1168   size_t opt_len;
1169   enum opt_code opt_code;
1170 };
1171
1172 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1173 #ifdef HOST_EBCDIC
1174 static struct cl_option cl_options[] =
1175 #else
1176 static const struct cl_option cl_options[] =
1177 #endif
1178 {
1179   COMMAND_LINE_OPTIONS
1180 };
1181 #undef DEF_OPT
1182 #undef COMMAND_LINE_OPTIONS
1183
1184 /* Perform a binary search to find which, if any, option the given
1185    command-line matches.  Returns its index in the option array,
1186    negative on failure.  Complications arise since some options can be
1187    suffixed with an argument, and multiple complete matches can occur,
1188    e.g. -pedantic and -pedantic-errors.  */
1189 static int
1190 parse_option (input)
1191      const char *input;
1192 {
1193   unsigned int md, mn, mx;
1194   size_t opt_len;
1195   int comp;
1196
1197   mn = 0;
1198   mx = N_OPTS;
1199
1200   while (mx > mn)
1201     {
1202       md = (mn + mx) / 2;
1203
1204       opt_len = cl_options[md].opt_len;
1205       comp = memcmp (input, cl_options[md].opt_text, opt_len);
1206
1207       if (comp > 0)
1208         mn = md + 1;
1209       else if (comp < 0)
1210         mx = md;
1211       else
1212         {
1213           if (input[opt_len] == '\0')
1214             return md;
1215           /* We were passed more text.  If the option takes an argument,
1216              we may match a later option or we may have been passed the
1217              argument.  The longest possible option match succeeds.
1218              If the option takes no arguments we have not matched and
1219              continue the search (e.g. input="stdc++" match was "stdc").  */
1220           mn = md + 1;
1221           if (cl_options[md].msg)
1222             {
1223               /* Scan forwards.  If we get an exact match, return it.
1224                  Otherwise, return the longest option-accepting match.
1225                  This loops no more than twice with current options.  */
1226               mx = md;
1227               for (; mn < (unsigned int) N_OPTS; mn++)
1228                 {
1229                   opt_len = cl_options[mn].opt_len;
1230                   if (memcmp (input, cl_options[mn].opt_text, opt_len))
1231                     break;
1232                   if (input[opt_len] == '\0')
1233                     return mn;
1234                   if (cl_options[mn].msg)
1235                     mx = mn;
1236                 }
1237               return mx;
1238             }
1239         }
1240     }
1241
1242   return -1;
1243 }
1244
1245 /* Handle one command-line option in (argc, argv).
1246    Can be called multiple times, to handle multiple sets of options.
1247    Returns number of strings consumed.  */
1248 int
1249 cpp_handle_option (pfile, argc, argv)
1250      cpp_reader *pfile;
1251      int argc;
1252      char **argv;
1253 {
1254   int i = 0;
1255   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1256
1257     {
1258       enum opt_code opt_code;
1259       int opt_index;
1260       const char *arg = 0;
1261
1262       /* Skip over '-'.  */
1263       opt_index = parse_option (&argv[i][1]);
1264       if (opt_index < 0)
1265         return i;
1266
1267       opt_code = cl_options[opt_index].opt_code;
1268       if (cl_options[opt_index].msg)
1269         {
1270           arg = &argv[i][cl_options[opt_index].opt_len + 1];
1271           if (arg[0] == '\0')
1272             {
1273               arg = argv[++i];
1274               if (!arg)
1275                 {
1276                   cpp_error (pfile, DL_ERROR,
1277                              cl_options[opt_index].msg, argv[i - 1]);
1278                   return argc;
1279                 }
1280             }
1281         }
1282
1283       switch (opt_code)
1284         {
1285         case N_OPTS: /* Shut GCC up.  */
1286           break;
1287
1288         case OPT_D:
1289           new_pending_directive (pend, arg, cpp_define);
1290           break;
1291         case OPT_iprefix:
1292           CPP_OPTION (pfile, include_prefix) = arg;
1293           CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1294           break;
1295
1296         case OPT_MG:
1297           CPP_OPTION (pfile, print_deps_missing_files) = 1;
1298           break;
1299         case OPT_M:
1300           /* When doing dependencies with -M or -MM, suppress normal
1301              preprocessed output, but still do -dM etc. as software
1302              depends on this.  Preprocessed output occurs if -MD, -MMD
1303              or environment var dependency generation is used.  */
1304           CPP_OPTION (pfile, print_deps) = 2;
1305           CPP_OPTION (pfile, no_output) = 1;
1306           CPP_OPTION (pfile, inhibit_warnings) = 1;
1307           break;
1308         case OPT_MM:
1309           CPP_OPTION (pfile, print_deps) = 1;
1310           CPP_OPTION (pfile, no_output) = 1;
1311           CPP_OPTION (pfile, inhibit_warnings) = 1;
1312           break;
1313         case OPT_MF:
1314           CPP_OPTION (pfile, deps_file) = arg;
1315           break;
1316         case OPT_MP:
1317           CPP_OPTION (pfile, deps_phony_targets) = 1;
1318           break;
1319         case OPT_MQ:
1320         case OPT_MT:
1321           /* Add a target.  -MQ quotes for Make.  */
1322           deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1323           break;
1324
1325         case OPT_MD:
1326           CPP_OPTION (pfile, print_deps) = 2;
1327           CPP_OPTION (pfile, deps_file) = arg;
1328           break;
1329         case OPT_MMD:
1330           CPP_OPTION (pfile, print_deps) = 1;
1331           CPP_OPTION (pfile, deps_file) = arg;
1332           break;
1333
1334         case OPT_A:
1335           if (arg[0] == '-')
1336             {
1337               /* -A with an argument beginning with '-' acts as
1338                  #unassert on whatever immediately follows the '-'.
1339                  If "-" is the whole argument, we eliminate all
1340                  predefined macros and assertions, including those
1341                  that were specified earlier on the command line.
1342                  That way we can get rid of any that were passed
1343                  automatically in from GCC.  */
1344
1345               if (arg[1] == '\0')
1346                 {
1347                   free_chain (pend->directive_head);
1348                   pend->directive_head = NULL;
1349                   pend->directive_tail = NULL;
1350                 }
1351               else
1352                 new_pending_directive (pend, arg + 1, cpp_unassert);
1353             }
1354           else
1355             new_pending_directive (pend, arg, cpp_assert);
1356           break;
1357         case OPT_U:
1358           new_pending_directive (pend, arg, cpp_undef);
1359           break;
1360         case OPT_I:           /* Add directory to path for includes.  */
1361           if (!strcmp (arg, "-"))
1362             {
1363               /* -I- means:
1364                  Use the preceding -I directories for #include "..."
1365                  but not #include <...>.
1366                  Don't search the directory of the present file
1367                  for #include "...".  (Note that -I. -I- is not the same as
1368                  the default setup; -I. uses the compiler's working dir.)  */
1369               if (! CPP_OPTION (pfile, ignore_srcdir))
1370                 {
1371                   pend->quote_head = pend->brack_head;
1372                   pend->quote_tail = pend->brack_tail;
1373                   pend->brack_head = 0;
1374                   pend->brack_tail = 0;
1375                   CPP_OPTION (pfile, ignore_srcdir) = 1;
1376                 }
1377               else
1378                 {
1379                   cpp_error (pfile, DL_ERROR, "-I- specified twice");
1380                   return argc;
1381                 }
1382             }
1383           else
1384             append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1385           break;
1386         case OPT_isystem:
1387           /* Add directory to beginning of system include path, as a system
1388              include directory.  */
1389           append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1390           break;
1391         case OPT_include:
1392         case OPT_imacros:
1393           {
1394             struct pending_option *o = (struct pending_option *)
1395               xmalloc (sizeof (struct pending_option));
1396             o->arg = arg;
1397             o->next = NULL;
1398
1399             if (opt_code == OPT_include)
1400               APPEND (pend, include, o);
1401             else
1402               APPEND (pend, imacros, o);
1403           }
1404           break;
1405         case OPT_iwithprefix:
1406           /* Add directory to end of path for includes,
1407              with the default prefix at the front of its name.  */
1408           /* fall through */
1409         case OPT_iwithprefixbefore:
1410           /* Add directory to main path for includes,
1411              with the default prefix at the front of its name.  */
1412           {
1413             char *fname;
1414             int len;
1415
1416             len = strlen (arg);
1417
1418             if (CPP_OPTION (pfile, include_prefix) != 0)
1419               {
1420                 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1421                 fname = xmalloc (ipl + len + 1);
1422                 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1423                 memcpy (fname + ipl, arg, len + 1);
1424               }
1425             else if (cpp_GCC_INCLUDE_DIR_len)
1426               {
1427                 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1428                 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1429                 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1430               }
1431             else
1432               fname = xstrdup (arg);
1433
1434             append_include_chain (pfile, fname,
1435                           opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1436           }
1437           break;
1438         case OPT_idirafter:
1439           /* Add directory to end of path for includes.  */
1440           append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1441           break;
1442         }
1443     }
1444   return i + 1;
1445 }
1446
1447 /* Handle command-line options in (argc, argv).
1448    Can be called multiple times, to handle multiple sets of options.
1449    Returns if an unrecognized option is seen.
1450    Returns number of strings consumed.  */
1451 int
1452 cpp_handle_options (pfile, argc, argv)
1453      cpp_reader *pfile;
1454      int argc;
1455      char **argv;
1456 {
1457   int i;
1458   int strings_processed;
1459
1460   for (i = 0; i < argc; i += strings_processed)
1461     {
1462       strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1463       if (strings_processed == 0)
1464         break;
1465     }
1466
1467   return i;
1468 }
1469
1470 /* Extra processing when all options are parsed, after all calls to
1471    cpp_handle_option[s].  Consistency checks etc.  */
1472 void
1473 cpp_post_options (pfile)
1474      cpp_reader *pfile;
1475 {
1476   /* Canonicalize in_fname and out_fname.  We guarantee they are not
1477      NULL, and that the empty string represents stdin / stdout.  */
1478   if (CPP_OPTION (pfile, in_fname) == NULL
1479       || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1480     CPP_OPTION (pfile, in_fname) = "";
1481
1482   if (CPP_OPTION (pfile, out_fname) == NULL
1483       || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1484     CPP_OPTION (pfile, out_fname) = "";
1485
1486   /* -Wtraditional is not useful in C++ mode.  */
1487   if (CPP_OPTION (pfile, cplusplus))
1488     CPP_OPTION (pfile, warn_traditional) = 0;
1489
1490   /* The compiler front ends override this, but I think this is the
1491      appropriate setting for the library.  */
1492   CPP_OPTION (pfile, warn_long_long)
1493      = ((CPP_OPTION (pfile, pedantic) && !CPP_OPTION (pfile, c99))
1494         || CPP_OPTION (pfile, warn_traditional));
1495
1496   /* Permanently disable macro expansion if we are rescanning
1497      preprocessed text.  Read preprocesed source in ISO mode.  */
1498   if (CPP_OPTION (pfile, preprocessed))
1499     {
1500       pfile->state.prevent_expansion = 1;
1501       CPP_OPTION (pfile, traditional) = 0;
1502     }
1503
1504   /* Traditional CPP does not accurately track column information.  */
1505   if (CPP_OPTION (pfile, traditional))
1506     CPP_OPTION (pfile, show_column) = 0;
1507
1508   /* -dM and dependencies suppress normal output; do it here so that
1509      the last -d[MDN] switch overrides earlier ones.  */
1510   if (CPP_OPTION (pfile, dump_macros) == dump_only)
1511     CPP_OPTION (pfile, no_output) = 1;
1512
1513   /* Disable -dD, -dN and -dI if normal output is suppressed.  Allow
1514      -dM since at least glibc relies on -M -dM to work.  */
1515   if (CPP_OPTION (pfile, no_output))
1516     {
1517       if (CPP_OPTION (pfile, dump_macros) != dump_only)
1518         CPP_OPTION (pfile, dump_macros) = dump_none;
1519       CPP_OPTION (pfile, dump_includes) = 0;
1520     }
1521
1522   /* Intialize, and check environment variables for, dependency
1523      output.  */
1524   init_dependency_output (pfile);
1525
1526   /* If we're not outputting dependencies, complain if other -M
1527      options have been given.  */
1528   if (!CPP_OPTION (pfile, print_deps)
1529       && (CPP_OPTION (pfile, print_deps_missing_files)
1530           || CPP_OPTION (pfile, deps_file)
1531           || CPP_OPTION (pfile, deps_phony_targets)))
1532     cpp_error (pfile, DL_ERROR,
1533                "you must additionally specify either -M or -MM");
1534 }
1535
1536 /* Set up dependency-file output.  On exit, if print_deps is non-zero
1537    then deps_file is not NULL; stdout is the empty string.  */
1538 static void
1539 init_dependency_output (pfile)
1540      cpp_reader *pfile;
1541 {
1542   char *spec, *s, *output_file;
1543
1544   /* Either of two environment variables can specify output of deps.
1545      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1546      where OUTPUT_FILE is the file to write deps info to
1547      and DEPS_TARGET is the target to mention in the deps.  */
1548
1549   if (CPP_OPTION (pfile, print_deps) == 0)
1550     {
1551       spec = getenv ("DEPENDENCIES_OUTPUT");
1552       if (spec)
1553         CPP_OPTION (pfile, print_deps) = 1;
1554       else
1555         {
1556           spec = getenv ("SUNPRO_DEPENDENCIES");
1557           if (spec)
1558             CPP_OPTION (pfile, print_deps) = 2;
1559           else
1560             return;
1561         }
1562
1563       /* Find the space before the DEPS_TARGET, if there is one.  */
1564       s = strchr (spec, ' ');
1565       if (s)
1566         {
1567           /* Let the caller perform MAKE quoting.  */
1568           deps_add_target (pfile->deps, s + 1, 0);
1569           output_file = (char *) xmalloc (s - spec + 1);
1570           memcpy (output_file, spec, s - spec);
1571           output_file[s - spec] = 0;
1572         }
1573       else
1574         output_file = spec;
1575
1576       /* Command line -MF overrides environment variables and default.  */
1577       if (CPP_OPTION (pfile, deps_file) == 0)
1578         CPP_OPTION (pfile, deps_file) = output_file;
1579
1580       CPP_OPTION (pfile, print_deps_append) = 1;
1581     }
1582   else if (CPP_OPTION (pfile, deps_file) == 0)
1583     /* If -M or -MM was seen without -MF, default output to wherever
1584        was specified with -o.  out_fname is non-NULL here.  */
1585     CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1586 }