OSDN Git Service

* configure.in: Delete three unused variables. Move a variable
[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, 2003 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 "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "cpphash.h"
28 #include "intl.h"
29 #include "mkdeps.h"
30
31 /* Internal structures and prototypes.  */
32
33 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
34    -imacros switch.  */
35 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
36 struct pending_option
37 {
38   struct pending_option *next;
39   const char *arg;
40   cl_directive_handler handler;
41 };
42
43 /* The `pending' structure accumulates all the options that are not
44    actually processed until we hit cpp_read_main_file.  It consists of
45    several lists, one for each type of option.  We keep both head and
46    tail pointers for quick insertion.  */
47 struct cpp_pending
48 {
49   struct pending_option *directive_head, *directive_tail;
50   struct pending_option *imacros_head, *imacros_tail;
51   struct pending_option *include_head, *include_tail;
52 };
53
54 #ifdef __STDC__
55 #define APPEND(pend, list, elt) \
56   do {  if (!(pend)->list##_head) (pend)->list##_head = (elt); \
57         else (pend)->list##_tail->next = (elt); \
58         (pend)->list##_tail = (elt); \
59   } while (0)
60 #else
61 #define APPEND(pend, list, elt) \
62   do {  if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
63         else (pend)->list/**/_tail->next = (elt); \
64         (pend)->list/**/_tail = (elt); \
65   } while (0)
66 #endif
67
68 static void init_library                PARAMS ((void));
69 static void init_builtins               PARAMS ((cpp_reader *));
70 static void mark_named_operators        PARAMS ((cpp_reader *));
71 static bool push_include                PARAMS ((cpp_reader *,
72                                                  struct pending_option *));
73 static void free_chain                  PARAMS ((struct pending_option *));
74 static void read_original_filename      PARAMS ((cpp_reader *));
75 static void new_pending_directive       PARAMS ((struct cpp_pending *,
76                                                  const char *,
77                                                  cl_directive_handler));
78 static int parse_option                 PARAMS ((const char *));
79 static void post_options                PARAMS ((cpp_reader *));
80
81 /* If we have designated initializers (GCC >2.7) these tables can be
82    initialized, constant data.  Otherwise, they have to be filled in at
83    runtime.  */
84 #if HAVE_DESIGNATED_INITIALIZERS
85
86 #define init_trigraph_map()  /* Nothing.  */
87 #define TRIGRAPH_MAP \
88 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
89
90 #define END };
91 #define s(p, v) [p] = v,
92
93 #else
94
95 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
96  static void init_trigraph_map PARAMS ((void)) { \
97  unsigned char *x = _cpp_trigraph_map;
98
99 #define END }
100 #define s(p, v) x[p] = v;
101
102 #endif
103
104 TRIGRAPH_MAP
105   s('=', '#')   s(')', ']')     s('!', '|')
106   s('(', '[')   s('\'', '^')    s('>', '}')
107   s('/', '\\')  s('<', '{')     s('-', '~')
108 END
109
110 #undef s
111 #undef END
112 #undef TRIGRAPH_MAP
113
114 /* A set of booleans indicating what CPP features each source language
115    requires.  */
116 struct lang_flags
117 {
118   char c99;
119   char cplusplus;
120   char extended_numbers;
121   char std;
122   char dollars_in_ident;
123   char cplusplus_comments;
124   char digraphs;
125 };
126
127 /* ??? Enable $ in identifiers in assembly? */
128 static const struct lang_flags lang_defaults[] =
129 { /*              c99 c++ xnum std dollar c++comm digr  */
130   /* GNUC89 */  { 0,  0,  1,   0,   1,     1,      1     },
131   /* GNUC99 */  { 1,  0,  1,   0,   1,     1,      1     },
132   /* STDC89 */  { 0,  0,  0,   1,   0,     0,      0     },
133   /* STDC94 */  { 0,  0,  0,   1,   0,     0,      1     },
134   /* STDC99 */  { 1,  0,  1,   1,   0,     1,      1     },
135   /* GNUCXX */  { 0,  1,  1,   0,   1,     1,      1     },
136   /* CXX98  */  { 0,  1,  1,   1,   0,     1,      1     },
137   /* ASM    */  { 0,  0,  1,   0,   0,     1,      0     }
138 };
139
140 /* Sets internal flags correctly for a given language.  */
141 void
142 cpp_set_lang (pfile, lang)
143      cpp_reader *pfile;
144      enum c_lang lang;
145 {
146   const struct lang_flags *l = &lang_defaults[(int) lang];
147
148   CPP_OPTION (pfile, lang) = lang;
149
150   CPP_OPTION (pfile, c99)                = l->c99;
151   CPP_OPTION (pfile, cplusplus)          = l->cplusplus;
152   CPP_OPTION (pfile, extended_numbers)   = l->extended_numbers;
153   CPP_OPTION (pfile, std)                = l->std;
154   CPP_OPTION (pfile, trigraphs)          = l->std;
155   CPP_OPTION (pfile, dollars_in_ident)   = l->dollars_in_ident;
156   CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
157   CPP_OPTION (pfile, digraphs)           = l->digraphs;
158 }
159
160 #ifdef HOST_EBCDIC
161 static int opt_comp PARAMS ((const void *, const void *));
162
163 /* Run-time sorting of options array.  */
164 static int
165 opt_comp (p1, p2)
166      const void *p1, *p2;
167 {
168   return strcmp (((struct cl_option *) p1)->opt_text,
169                  ((struct cl_option *) p2)->opt_text);
170 }
171 #endif
172
173 /* init initializes library global state.  It might not need to
174    do anything depending on the platform and compiler.  */
175 static void
176 init_library ()
177 {
178   static int initialized = 0;
179
180   if (! initialized)
181     {
182       initialized = 1;
183
184 #ifdef HOST_EBCDIC
185       /* For non-ASCII hosts, the cl_options array needs to be sorted at
186          runtime.  */
187       qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
188 #endif
189
190       /* Set up the trigraph map.  This doesn't need to do anything if
191          we were compiled with a compiler that supports C99 designated
192          initializers.  */
193       init_trigraph_map ();
194     }
195 }
196
197 /* Initialize a cpp_reader structure.  */
198 cpp_reader *
199 cpp_create_reader (lang)
200      enum c_lang lang;
201 {
202   cpp_reader *pfile;
203
204   /* Initialize this instance of the library if it hasn't been already.  */
205   init_library ();
206
207   pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
208
209   cpp_set_lang (pfile, lang);
210   CPP_OPTION (pfile, warn_import) = 1;
211   CPP_OPTION (pfile, warn_multichar) = 1;
212   CPP_OPTION (pfile, discard_comments) = 1;
213   CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
214   CPP_OPTION (pfile, show_column) = 1;
215   CPP_OPTION (pfile, tabstop) = 8;
216   CPP_OPTION (pfile, operator_names) = 1;
217   CPP_OPTION (pfile, warn_endif_labels) = 1;
218   CPP_OPTION (pfile, warn_deprecated) = 1;
219   CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
220
221   CPP_OPTION (pfile, pending) =
222     (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
223
224   /* Default CPP arithmetic to something sensible for the host for the
225      benefit of dumb users like fix-header.  */
226   CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
227   CPP_OPTION (pfile, char_precision) = CHAR_BIT;
228   CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
229   CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
230   CPP_OPTION (pfile, unsigned_char) = 0;
231   CPP_OPTION (pfile, unsigned_wchar) = 1;
232
233   /* Initialize the line map.  Start at logical line 1, so we can use
234      a line number of zero for special states.  */
235   init_line_maps (&pfile->line_maps);
236   pfile->line = 1;
237
238   /* Initialize lexer state.  */
239   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
240
241   /* Set up static tokens.  */
242   pfile->avoid_paste.type = CPP_PADDING;
243   pfile->avoid_paste.val.source = NULL;
244   pfile->eof.type = CPP_EOF;
245   pfile->eof.flags = 0;
246
247   /* Create a token buffer for the lexer.  */
248   _cpp_init_tokenrun (&pfile->base_run, 250);
249   pfile->cur_run = &pfile->base_run;
250   pfile->cur_token = pfile->base_run.base;
251
252   /* Initialize the base context.  */
253   pfile->context = &pfile->base_context;
254   pfile->base_context.macro = 0;
255   pfile->base_context.prev = pfile->base_context.next = 0;
256
257   /* Aligned and unaligned storage.  */
258   pfile->a_buff = _cpp_get_buff (pfile, 0);
259   pfile->u_buff = _cpp_get_buff (pfile, 0);
260
261   /* The expression parser stack.  */
262   _cpp_expand_op_stack (pfile);
263
264   /* Initialize the buffer obstack.  */
265   gcc_obstack_init (&pfile->buffer_ob);
266
267   _cpp_init_includes (pfile);
268
269   return pfile;
270 }
271
272 /* Free resources used by PFILE.  Accessing PFILE after this function
273    returns leads to undefined behavior.  Returns the error count.  */
274 void
275 cpp_destroy (pfile)
276      cpp_reader *pfile;
277 {
278   cpp_context *context, *contextn;
279   tokenrun *run, *runn;
280
281   free_chain (CPP_OPTION (pfile, pending)->include_head);
282   free (CPP_OPTION (pfile, pending));
283   free (pfile->op_stack);
284
285   while (CPP_BUFFER (pfile) != NULL)
286     _cpp_pop_buffer (pfile);
287
288   if (pfile->out.base)
289     free (pfile->out.base);
290
291   if (pfile->macro_buffer)
292     {
293       free ((PTR) pfile->macro_buffer);
294       pfile->macro_buffer = NULL;
295       pfile->macro_buffer_len = 0;
296     }
297
298   if (pfile->deps)
299     deps_free (pfile->deps);
300   obstack_free (&pfile->buffer_ob, 0);
301
302   _cpp_destroy_hashtable (pfile);
303   _cpp_cleanup_includes (pfile);
304
305   _cpp_free_buff (pfile->a_buff);
306   _cpp_free_buff (pfile->u_buff);
307   _cpp_free_buff (pfile->free_buffs);
308
309   for (run = &pfile->base_run; run; run = runn)
310     {
311       runn = run->next;
312       free (run->base);
313       if (run != &pfile->base_run)
314         free (run);
315     }
316
317   for (context = pfile->base_context.next; context; context = contextn)
318     {
319       contextn = context->next;
320       free (context);
321     }
322
323   free_line_maps (&pfile->line_maps);
324   free (pfile);
325 }
326
327 /* This structure defines one built-in identifier.  A node will be
328    entered in the hash table under the name NAME, with value VALUE.
329
330    There are two tables of these.  builtin_array holds all the
331    "builtin" macros: these are handled by builtin_macro() in
332    cppmacro.c.  Builtin is somewhat of a misnomer -- the property of
333    interest is that these macros require special code to compute their
334    expansions.  The value is a "builtin_type" enumerator.
335
336    operator_array holds the C++ named operators.  These are keywords
337    which act as aliases for punctuators.  In C++, they cannot be
338    altered through #define, and #if recognizes them as operators.  In
339    C, these are not entered into the hash table at all (but see
340    <iso646.h>).  The value is a token-type enumerator.  */
341 struct builtin
342 {
343   const uchar *name;
344   unsigned short len;
345   unsigned short value;
346 };
347
348 #define B(n, t)    { DSC(n), t }
349 static const struct builtin builtin_array[] =
350 {
351   B("__TIME__",          BT_TIME),
352   B("__DATE__",          BT_DATE),
353   B("__FILE__",          BT_FILE),
354   B("__BASE_FILE__",     BT_BASE_FILE),
355   B("__LINE__",          BT_SPECLINE),
356   B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
357   /* Keep builtins not used for -traditional-cpp at the end, and
358      update init_builtins() if any more are added.  */
359   B("_Pragma",           BT_PRAGMA),
360   B("__STDC__",          BT_STDC),
361 };
362
363 static const struct builtin operator_array[] =
364 {
365   B("and",      CPP_AND_AND),
366   B("and_eq",   CPP_AND_EQ),
367   B("bitand",   CPP_AND),
368   B("bitor",    CPP_OR),
369   B("compl",    CPP_COMPL),
370   B("not",      CPP_NOT),
371   B("not_eq",   CPP_NOT_EQ),
372   B("or",       CPP_OR_OR),
373   B("or_eq",    CPP_OR_EQ),
374   B("xor",      CPP_XOR),
375   B("xor_eq",   CPP_XOR_EQ)
376 };
377 #undef B
378
379 /* Mark the C++ named operators in the hash table.  */
380 static void
381 mark_named_operators (pfile)
382      cpp_reader *pfile;
383 {
384   const struct builtin *b;
385
386   for (b = operator_array;
387        b < (operator_array + ARRAY_SIZE (operator_array));
388        b++)
389     {
390       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
391       hp->flags |= NODE_OPERATOR;
392       hp->is_directive = 0;
393       hp->directive_index = b->value;
394     }
395 }
396
397 /* Subroutine of cpp_read_main_file; reads the builtins table above and
398    enters them, and language-specific macros, into the hash table.  */
399 static void
400 init_builtins (pfile)
401      cpp_reader *pfile;
402 {
403   const struct builtin *b;
404   size_t n = ARRAY_SIZE (builtin_array);
405
406   if (CPP_OPTION (pfile, traditional))
407     n -= 2;
408
409   for(b = builtin_array; b < builtin_array + n; b++)
410     {
411       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
412       hp->type = NT_MACRO;
413       hp->flags |= NODE_BUILTIN | NODE_WARN;
414       hp->value.builtin = b->value;
415     }
416
417   if (CPP_OPTION (pfile, cplusplus))
418     _cpp_define_builtin (pfile, "__cplusplus 1");
419   else if (CPP_OPTION (pfile, lang) == CLK_ASM)
420     _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
421   else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
422     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
423   else if (CPP_OPTION (pfile, c99))
424     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
425
426   if (CPP_OPTION (pfile, objc))
427     _cpp_define_builtin (pfile, "__OBJC__ 1");
428
429   if (pfile->cb.register_builtins)
430     (*pfile->cb.register_builtins) (pfile);
431 }
432
433 /* Pushes a command line -imacro and -include file indicated by P onto
434    the buffer stack.  Returns nonzero if successful.  */
435 static bool
436 push_include (pfile, p)
437      cpp_reader *pfile;
438      struct pending_option *p;
439 {
440   cpp_token header;
441
442   /* Later: maybe update this to use the #include "" search path
443      if cpp_read_file fails.  */
444   header.type = CPP_STRING;
445   header.val.str.text = (const unsigned char *) p->arg;
446   header.val.str.len = strlen (p->arg);
447   /* Make the command line directive take up a line.  */
448   pfile->line++;
449
450   return _cpp_execute_include (pfile, &header, IT_CMDLINE);
451 }
452
453 /* Frees a pending_option chain.  */
454 static void
455 free_chain (head)
456      struct pending_option *head;
457 {
458   struct pending_option *next;
459
460   while (head)
461     {
462       next = head->next;
463       free (head);
464       head = next;
465     }
466 }
467
468 /* Sanity-checks are dependent on command-line options, so it is
469    called as a subroutine of cpp_read_main_file ().  */
470 #if ENABLE_CHECKING
471 static void sanity_checks PARAMS ((cpp_reader *));
472 static void sanity_checks (pfile)
473      cpp_reader *pfile;
474 {
475   cppchar_t test = 0;
476   size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
477
478   /* Sanity checks for assumptions about CPP arithmetic and target
479      type precisions made by cpplib.  */
480   test--;
481   if (test < 1)
482     cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
483
484   if (CPP_OPTION (pfile, precision) > max_precision)
485     cpp_error (pfile, DL_ICE,
486                "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
487                (unsigned long) max_precision,
488                (unsigned long) CPP_OPTION (pfile, precision));
489
490   if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
491     cpp_error (pfile, DL_ICE,
492                "CPP arithmetic must be at least as precise as a target int");
493
494   if (CPP_OPTION (pfile, char_precision) < 8)
495     cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
496
497   if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
498     cpp_error (pfile, DL_ICE,
499                "target wchar_t is narrower than target char");
500
501   if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
502     cpp_error (pfile, DL_ICE,
503                "target int is narrower than target char");
504
505   /* This is assumed in eval_token() and could be fixed if necessary.  */
506   if (sizeof (cppchar_t) > sizeof (cpp_num_part))
507     cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
508
509   if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
510     cpp_error (pfile, DL_ICE,
511                "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
512                (unsigned long) BITS_PER_CPPCHAR_T,
513                (unsigned long) CPP_OPTION (pfile, wchar_precision));
514 }
515 #else
516 # define sanity_checks(PFILE)
517 #endif
518
519 /* Add a dependency target.  Can be called any number of times before
520    cpp_read_main_file().  If no targets have been added before
521    cpp_read_main_file(), then the default target is used.  */
522 void
523 cpp_add_dependency_target (pfile, target, quote)
524      cpp_reader *pfile;
525      const char *target;
526      int quote;
527 {
528   if (!pfile->deps)
529     pfile->deps = deps_init ();
530
531   deps_add_target (pfile->deps, target, quote);
532 }
533
534 /* This is called after options have been parsed, and partially
535    processed.  Setup for processing input from the file named FNAME,
536    or stdin if it is the empty string.  Return the original filename
537    on success (e.g. foo.i->foo.c), or NULL on failure.  */
538 const char *
539 cpp_read_main_file (pfile, fname, table)
540      cpp_reader *pfile;
541      const char *fname;
542      hash_table *table;
543 {
544   sanity_checks (pfile);
545
546   post_options (pfile);
547
548   /* The front ends don't set up the hash table until they have
549      finished processing the command line options, so initializing the
550      hashtable is deferred until now.  */
551   _cpp_init_hashtable (pfile, table);
552
553   if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
554     {
555       if (!pfile->deps)
556         pfile->deps = deps_init ();
557
558       /* Set the default target (if there is none already).  */
559       deps_add_default_target (pfile->deps, fname);
560     }
561
562   /* Open the main input file.  */
563   if (!_cpp_read_file (pfile, fname))
564     return NULL;
565
566   /* Set this here so the client can change the option if it wishes,
567      and after stacking the main file so we don't trace the main
568      file.  */
569   pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
570
571   /* For foo.i, read the original filename foo.c now, for the benefit
572      of the front ends.  */
573   if (CPP_OPTION (pfile, preprocessed))
574     read_original_filename (pfile);
575
576   return pfile->map->to_file;
577 }
578
579 /* For preprocessed files, if the first tokens are of the form # NUM.
580    handle the directive so we know the original file name.  This will
581    generate file_change callbacks, which the front ends must handle
582    appropriately given their state of initialization.  */
583 static void
584 read_original_filename (pfile)
585      cpp_reader *pfile;
586 {
587   const cpp_token *token, *token1;
588
589   /* Lex ahead; if the first tokens are of the form # NUM, then
590      process the directive, otherwise back up.  */
591   token = _cpp_lex_direct (pfile);
592   if (token->type == CPP_HASH)
593     {
594       token1 = _cpp_lex_direct (pfile);
595       _cpp_backup_tokens (pfile, 1);
596
597       /* If it's a #line directive, handle it.  */
598       if (token1->type == CPP_NUMBER)
599         {
600           _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
601           return;
602         }
603     }
604
605   /* Backup as if nothing happened.  */
606   _cpp_backup_tokens (pfile, 1);
607 }
608
609 /* Handle pending command line options: -D, -U, -A, -imacros and
610    -include.  This should be called after debugging has been properly
611    set up in the front ends.  */
612 void
613 cpp_finish_options (pfile)
614      cpp_reader *pfile;
615 {
616   /* Mark named operators before handling command line macros.  */
617   if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
618     mark_named_operators (pfile);
619
620   /* Install builtins and process command line macros etc. in the order
621      they appeared, but only if not already preprocessed.  */
622   if (! CPP_OPTION (pfile, preprocessed))
623     {
624       struct pending_option *p;
625
626       _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
627       init_builtins (pfile);
628       _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
629       for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
630         (*p->handler) (pfile, p->arg);
631
632       /* Scan -imacros files after -D, -U, but before -include.
633          pfile->next_include_file is NULL, so _cpp_pop_buffer does not
634          push -include files.  */
635       for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
636         if (push_include (pfile, p))
637           cpp_scan_nooutput (pfile);
638
639       pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
640       _cpp_maybe_push_include_file (pfile);
641     }
642
643   pfile->first_unused_line = pfile->line;
644
645   free_chain (CPP_OPTION (pfile, pending)->imacros_head);
646   free_chain (CPP_OPTION (pfile, pending)->directive_head);
647 }
648
649 /* Push the next buffer on the stack given by -include, if any.  */
650 void
651 _cpp_maybe_push_include_file (pfile)
652      cpp_reader *pfile;
653 {
654   if (pfile->next_include_file)
655     {
656       struct pending_option *head = *pfile->next_include_file;
657
658       while (head && !push_include (pfile, head))
659         head = head->next;
660
661       if (head)
662         pfile->next_include_file = &head->next;
663       else
664         {
665           /* All done; restore the line map from <command line>.  */
666           _cpp_do_file_change (pfile, LC_RENAME,
667                                pfile->line_maps.maps[0].to_file, 1, 0);
668           /* Don't come back here again.  */
669           pfile->next_include_file = NULL;
670         }
671     }
672 }
673
674 /* This is called at the end of preprocessing.  It pops the last
675    buffer and writes dependency output, and returns the number of
676    errors.
677  
678    Maybe it should also reset state, such that you could call
679    cpp_start_read with a new filename to restart processing.  */
680 int
681 cpp_finish (pfile, deps_stream)
682      cpp_reader *pfile;
683      FILE *deps_stream;
684 {
685   /* Warn about unused macros before popping the final buffer.  */
686   if (CPP_OPTION (pfile, warn_unused_macros))
687     cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
688
689   /* cpplex.c leaves the final buffer on the stack.  This it so that
690      it returns an unending stream of CPP_EOFs to the client.  If we
691      popped the buffer, we'd dereference a NULL buffer pointer and
692      segfault.  It's nice to allow the client to do worry-free excess
693      cpp_get_token calls.  */
694   while (pfile->buffer)
695     _cpp_pop_buffer (pfile);
696
697   /* Don't write the deps file if there are errors.  */
698   if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
699       && deps_stream && pfile->errors == 0)
700     {
701       deps_write (pfile->deps, deps_stream, 72);
702
703       if (CPP_OPTION (pfile, deps.phony_targets))
704         deps_phony_targets (pfile->deps, deps_stream);
705     }
706
707   /* Report on headers that could use multiple include guards.  */
708   if (CPP_OPTION (pfile, print_include_names))
709     _cpp_report_missing_guards (pfile);
710
711   return pfile->errors;
712 }
713
714 /* Add a directive to be handled later in the initialization phase.  */
715 static void
716 new_pending_directive (pend, text, handler)
717      struct cpp_pending *pend;
718      const char *text;
719      cl_directive_handler handler;
720 {
721   struct pending_option *o = (struct pending_option *)
722     xmalloc (sizeof (struct pending_option));
723
724   o->arg = text;
725   o->next = NULL;
726   o->handler = handler;
727   APPEND (pend, directive, o);
728 }
729
730 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
731    I.e. a const string initializer with parens around it.  That is
732    what N_("string") resolves to, so we make no_* be macros instead.  */
733 #define no_ass N_("assertion missing after %s")
734 #define no_fil N_("file name missing after %s")
735 #define no_mac N_("macro name missing after %s")
736
737 /* This is the list of all command line options, with the leading
738    "-" removed.  It must be sorted in ASCII collating order.  */
739 #define COMMAND_LINE_OPTIONS                                                  \
740   DEF_OPT("A",                        no_ass, OPT_A)                          \
741   DEF_OPT("D",                        no_mac, OPT_D)                          \
742   DEF_OPT("U",                        no_mac, OPT_U)                          \
743   DEF_OPT("imacros",                  no_fil, OPT_imacros)                    \
744   DEF_OPT("include",                  no_fil, OPT_include)
745
746 #define DEF_OPT(text, msg, code) code,
747 enum opt_code
748 {
749   COMMAND_LINE_OPTIONS
750   N_OPTS
751 };
752 #undef DEF_OPT
753
754 struct cl_option
755 {
756   const char *opt_text;
757   const char *msg;
758   size_t opt_len;
759   enum opt_code opt_code;
760 };
761
762 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
763 #ifdef HOST_EBCDIC
764 static struct cl_option cl_options[] =
765 #else
766 static const struct cl_option cl_options[] =
767 #endif
768 {
769   COMMAND_LINE_OPTIONS
770 };
771 #undef DEF_OPT
772 #undef COMMAND_LINE_OPTIONS
773
774 /* Perform a binary search to find which, if any, option the given
775    command-line matches.  Returns its index in the option array,
776    negative on failure.  Complications arise since some options can be
777    suffixed with an argument, and multiple complete matches can occur,
778    e.g. -pedantic and -pedantic-errors.  */
779 static int
780 parse_option (input)
781      const char *input;
782 {
783   unsigned int md, mn, mx;
784   size_t opt_len;
785   int comp;
786
787   mn = 0;
788   mx = N_OPTS;
789
790   while (mx > mn)
791     {
792       md = (mn + mx) / 2;
793
794       opt_len = cl_options[md].opt_len;
795       comp = strncmp (input, cl_options[md].opt_text, opt_len);
796
797       if (comp > 0)
798         mn = md + 1;
799       else if (comp < 0)
800         mx = md;
801       else
802         {
803           if (input[opt_len] == '\0')
804             return md;
805           /* We were passed more text.  If the option takes an argument,
806              we may match a later option or we may have been passed the
807              argument.  The longest possible option match succeeds.
808              If the option takes no arguments we have not matched and
809              continue the search (e.g. input="stdc++" match was "stdc").  */
810           mn = md + 1;
811           if (cl_options[md].msg)
812             {
813               /* Scan forwards.  If we get an exact match, return it.
814                  Otherwise, return the longest option-accepting match.
815                  This loops no more than twice with current options.  */
816               mx = md;
817               for (; mn < (unsigned int) N_OPTS; mn++)
818                 {
819                   opt_len = cl_options[mn].opt_len;
820                   if (strncmp (input, cl_options[mn].opt_text, opt_len))
821                     break;
822                   if (input[opt_len] == '\0')
823                     return mn;
824                   if (cl_options[mn].msg)
825                     mx = mn;
826                 }
827               return mx;
828             }
829         }
830     }
831
832   return -1;
833 }
834
835 /* Handle one command-line option in (argc, argv).
836    Can be called multiple times, to handle multiple sets of options.
837    Returns number of strings consumed.  */
838 int
839 cpp_handle_option (pfile, argc, argv)
840      cpp_reader *pfile;
841      int argc;
842      char **argv;
843 {
844   int i = 0;
845   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
846
847     {
848       enum opt_code opt_code;
849       int opt_index;
850       const char *arg = 0;
851
852       /* Skip over '-'.  */
853       opt_index = parse_option (&argv[i][1]);
854       if (opt_index < 0)
855         return i;
856
857       opt_code = cl_options[opt_index].opt_code;
858       if (cl_options[opt_index].msg)
859         {
860           arg = &argv[i][cl_options[opt_index].opt_len + 1];
861           if (arg[0] == '\0')
862             {
863               arg = argv[++i];
864               if (!arg)
865                 {
866                   cpp_error (pfile, DL_ERROR,
867                              cl_options[opt_index].msg, argv[i - 1]);
868                   return argc;
869                 }
870             }
871         }
872
873       switch (opt_code)
874         {
875         case N_OPTS: /* Shut GCC up.  */
876           break;
877
878         case OPT_D:
879           new_pending_directive (pend, arg, cpp_define);
880           break;
881
882         case OPT_A:
883           if (arg[0] == '-')
884             {
885               /* -A with an argument beginning with '-' acts as
886                  #unassert on whatever immediately follows the '-'.
887                  If "-" is the whole argument, we eliminate all
888                  predefined macros and assertions, including those
889                  that were specified earlier on the command line.
890                  That way we can get rid of any that were passed
891                  automatically in from GCC.  */
892
893               if (arg[1] == '\0')
894                 {
895                   free_chain (pend->directive_head);
896                   pend->directive_head = NULL;
897                   pend->directive_tail = NULL;
898                 }
899               else
900                 new_pending_directive (pend, arg + 1, cpp_unassert);
901             }
902           else
903             new_pending_directive (pend, arg, cpp_assert);
904           break;
905         case OPT_U:
906           new_pending_directive (pend, arg, cpp_undef);
907           break;
908         case OPT_include:
909         case OPT_imacros:
910           {
911             struct pending_option *o = (struct pending_option *)
912               xmalloc (sizeof (struct pending_option));
913             o->arg = arg;
914             o->next = NULL;
915
916             if (opt_code == OPT_include)
917               APPEND (pend, include, o);
918             else
919               APPEND (pend, imacros, o);
920           }
921           break;
922         }
923     }
924   return i + 1;
925 }
926
927 static void
928 post_options (pfile)
929      cpp_reader *pfile;
930 {
931   /* -Wtraditional is not useful in C++ mode.  */
932   if (CPP_OPTION (pfile, cplusplus))
933     CPP_OPTION (pfile, warn_traditional) = 0;
934
935   /* Permanently disable macro expansion if we are rescanning
936      preprocessed text.  Read preprocesed source in ISO mode.  */
937   if (CPP_OPTION (pfile, preprocessed))
938     {
939       pfile->state.prevent_expansion = 1;
940       CPP_OPTION (pfile, traditional) = 0;
941     }
942
943   /* Traditional CPP does not accurately track column information.  */
944   if (CPP_OPTION (pfile, traditional))
945     CPP_OPTION (pfile, show_column) = 0;
946 }