OSDN Git Service

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