OSDN Git Service

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