OSDN Git Service

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