Merge from vendor branch GCC:
[dragonfly.git] / contrib / gcc-4.0 / libcpp / directives.c
1 /* CPP Library. (Directive handling.)
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005 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 "internal.h"
26 #include "mkdeps.h"
27 #include "obstack.h"
28
29 /* Stack of conditionals currently in progress
30    (including both successful and failing conditionals).  */
31 struct if_stack
32 {
33   struct if_stack *next;
34   unsigned int line;            /* Line where condition started.  */
35   const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
36   bool skip_elses;              /* Can future #else / #elif be skipped?  */
37   bool was_skipping;            /* If were skipping on entry.  */
38   int type;                     /* Most recent conditional for diagnostics.  */
39 };
40
41 /* Contains a registered pragma or pragma namespace.  */
42 typedef void (*pragma_cb) (cpp_reader *);
43 struct pragma_entry
44 {
45   struct pragma_entry *next;
46   const cpp_hashnode *pragma;   /* Name and length.  */
47   bool is_nspace;
48   bool allow_expansion;
49   bool is_internal;
50   union {
51     pragma_cb handler;
52     struct pragma_entry *space;
53   } u;
54 };
55
56 /* Values for the origin field of struct directive.  KANDR directives
57    come from traditional (K&R) C.  STDC89 directives come from the
58    1989 C standard.  EXTENSION directives are extensions.  */
59 #define KANDR           0
60 #define STDC89          1
61 #define EXTENSION       2
62
63 /* Values for the flags field of struct directive.  COND indicates a
64    conditional; IF_COND an opening conditional.  INCL means to treat
65    "..." and <...> as q-char and h-char sequences respectively.  IN_I
66    means this directive should be handled even if -fpreprocessed is in
67    effect (these are the directives with callback hooks).
68
69    EXPAND is set on directives that are always macro-expanded.  */
70 #define COND            (1 << 0)
71 #define IF_COND         (1 << 1)
72 #define INCL            (1 << 2)
73 #define IN_I            (1 << 3)
74 #define EXPAND          (1 << 4)
75
76 /* Defines one #-directive, including how to handle it.  */
77 typedef void (*directive_handler) (cpp_reader *);
78 typedef struct directive directive;
79 struct directive
80 {
81   directive_handler handler;    /* Function to handle directive.  */
82   const uchar *name;            /* Name of directive.  */
83   unsigned short length;        /* Length of name.  */
84   unsigned char origin;         /* Origin of directive.  */
85   unsigned char flags;          /* Flags describing this directive.  */
86 };
87
88 /* Forward declarations.  */
89
90 static void skip_rest_of_line (cpp_reader *);
91 static void check_eol (cpp_reader *);
92 static void start_directive (cpp_reader *);
93 static void prepare_directive_trad (cpp_reader *);
94 static void end_directive (cpp_reader *, int);
95 static void directive_diagnostics (cpp_reader *, const directive *, int);
96 static void run_directive (cpp_reader *, int, const char *, size_t);
97 static char *glue_header_name (cpp_reader *);
98 static const char *parse_include (cpp_reader *, int *);
99 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
100 static unsigned int read_flag (cpp_reader *, unsigned int);
101 static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
102 static void do_diagnostic (cpp_reader *, int, int);
103 static cpp_hashnode *lex_macro_node (cpp_reader *);
104 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
105 static void do_include_common (cpp_reader *, enum include_type);
106 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
107                                                  const cpp_hashnode *);
108 static struct pragma_entry *insert_pragma_entry (cpp_reader *,
109                                                  struct pragma_entry **,
110                                                  const cpp_hashnode *,
111                                                  pragma_cb,
112                                                  bool, bool);
113 static void register_pragma (cpp_reader *, const char *, const char *,
114                              pragma_cb, bool, bool);
115 static int count_registered_pragmas (struct pragma_entry *);
116 static char ** save_registered_pragmas (struct pragma_entry *, char **);
117 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
118                                            char **);
119 static void do_pragma_once (cpp_reader *);
120 static void do_pragma_poison (cpp_reader *);
121 static void do_pragma_system_header (cpp_reader *);
122 static void do_pragma_dependency (cpp_reader *);
123 static void do_linemarker (cpp_reader *);
124 static const cpp_token *get_token_no_padding (cpp_reader *);
125 static const cpp_token *get__Pragma_string (cpp_reader *);
126 static void destringize_and_run (cpp_reader *, const cpp_string *);
127 static int parse_answer (cpp_reader *, struct answer **, int);
128 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
129 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
130 static void handle_assertion (cpp_reader *, const char *, int);
131
132 /* This is the table of directive handlers.  It is ordered by
133    frequency of occurrence; the numbers at the end are directive
134    counts from all the source code I have lying around (egcs and libc
135    CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
136    pcmcia-cs-3.0.9).  This is no longer important as directive lookup
137    is now O(1).  All extensions other than #warning and #include_next
138    are deprecated.  The name is where the extension appears to have
139    come from.  */
140
141 #define DIRECTIVE_TABLE                                                 \
142 D(define,       T_DEFINE = 0,   KANDR,     IN_I)           /* 270554 */ \
143 D(include,      T_INCLUDE,      KANDR,     INCL | EXPAND)  /*  52262 */ \
144 D(endif,        T_ENDIF,        KANDR,     COND)           /*  45855 */ \
145 D(ifdef,        T_IFDEF,        KANDR,     COND | IF_COND) /*  22000 */ \
146 D(if,           T_IF,           KANDR, COND | IF_COND | EXPAND) /*  18162 */ \
147 D(else,         T_ELSE,         KANDR,     COND)           /*   9863 */ \
148 D(ifndef,       T_IFNDEF,       KANDR,     COND | IF_COND) /*   9675 */ \
149 D(undef,        T_UNDEF,        KANDR,     IN_I)           /*   4837 */ \
150 D(line,         T_LINE,         KANDR,     EXPAND)         /*   2465 */ \
151 D(elif,         T_ELIF,         STDC89,    COND | EXPAND)  /*    610 */ \
152 D(error,        T_ERROR,        STDC89,    0)              /*    475 */ \
153 D(pragma,       T_PRAGMA,       STDC89,    IN_I)           /*    195 */ \
154 D(warning,      T_WARNING,      EXTENSION, 0)              /*     22 */ \
155 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND)  /*     19 */ \
156 D(ident,        T_IDENT,        EXTENSION, IN_I)           /*     11 */ \
157 D(import,       T_IMPORT,       EXTENSION, INCL | EXPAND)  /* 0 ObjC */ \
158 D(assert,       T_ASSERT,       EXTENSION, 0)              /* 0 SVR4 */ \
159 D(unassert,     T_UNASSERT,     EXTENSION, 0)              /* 0 SVR4 */ \
160 D(sccs,         T_SCCS,         EXTENSION, 0)              /* 0 SVR4? */
161
162 /* Use the table to generate a series of prototypes, an enum for the
163    directive names, and an array of directive handlers.  */
164
165 #define D(name, t, o, f) static void do_##name (cpp_reader *);
166 DIRECTIVE_TABLE
167 #undef D
168
169 #define D(n, tag, o, f) tag,
170 enum
171 {
172   DIRECTIVE_TABLE
173   N_DIRECTIVES
174 };
175 #undef D
176
177 #define D(name, t, origin, flags) \
178 { do_##name, (const uchar *) #name, \
179   sizeof #name - 1, origin, flags },
180 static const directive dtable[] =
181 {
182 DIRECTIVE_TABLE
183 };
184 #undef D
185 #undef DIRECTIVE_TABLE
186
187 /* Wrapper struct directive for linemarkers.
188    The origin is more or less true - the original K+R cpp
189    did use this notation in its preprocessed output.  */
190 static const directive linemarker_dir =
191 {
192   do_linemarker, U"#", 1, KANDR, IN_I
193 };
194
195 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
196
197 /* Skip any remaining tokens in a directive.  */
198 static void
199 skip_rest_of_line (cpp_reader *pfile)
200 {
201   /* Discard all stacked contexts.  */
202   while (pfile->context->prev)
203     _cpp_pop_context (pfile);
204
205   /* Sweep up all tokens remaining on the line.  */
206   if (! SEEN_EOL ())
207     while (_cpp_lex_token (pfile)->type != CPP_EOF)
208       ;
209 }
210
211 /* Ensure there are no stray tokens at the end of a directive.  */
212 static void
213 check_eol (cpp_reader *pfile)
214 {
215   if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
216     cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
217                pfile->directive->name);
218 }
219
220 /* Called when entering a directive, _Pragma or command-line directive.  */
221 static void
222 start_directive (cpp_reader *pfile)
223 {
224   /* Setup in-directive state.  */
225   pfile->state.in_directive = 1;
226   pfile->state.save_comments = 0;
227   pfile->directive_result.type = CPP_PADDING;
228
229   /* Some handlers need the position of the # for diagnostics.  */
230   pfile->directive_line = pfile->line_table->highest_line;
231 }
232
233 /* Called when leaving a directive, _Pragma or command-line directive.  */
234 static void
235 end_directive (cpp_reader *pfile, int skip_line)
236 {
237   if (CPP_OPTION (pfile, traditional))
238     {
239       /* Revert change of prepare_directive_trad.  */
240       pfile->state.prevent_expansion--;
241
242       if (pfile->directive != &dtable[T_DEFINE])
243         _cpp_remove_overlay (pfile);
244     }
245   /* We don't skip for an assembler #.  */
246   else if (skip_line)
247     {
248       skip_rest_of_line (pfile);
249       if (!pfile->keep_tokens)
250         {
251           pfile->cur_run = &pfile->base_run;
252           pfile->cur_token = pfile->base_run.base;
253         }
254     }
255
256   /* Restore state.  */
257   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
258   pfile->state.in_directive = 0;
259   pfile->state.in_expression = 0;
260   pfile->state.angled_headers = 0;
261   pfile->directive = 0;
262 }
263
264 /* Prepare to handle the directive in pfile->directive.  */
265 static void
266 prepare_directive_trad (cpp_reader *pfile)
267 {
268   if (pfile->directive != &dtable[T_DEFINE])
269     {
270       bool no_expand = (pfile->directive
271                         && ! (pfile->directive->flags & EXPAND));
272       bool was_skipping = pfile->state.skipping;
273
274       pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
275                                     || pfile->directive == &dtable[T_ELIF]);
276       if (pfile->state.in_expression)
277         pfile->state.skipping = false;
278
279       if (no_expand)
280         pfile->state.prevent_expansion++;
281       _cpp_scan_out_logical_line (pfile, NULL);
282       if (no_expand)
283         pfile->state.prevent_expansion--;
284
285       pfile->state.skipping = was_skipping;
286       _cpp_overlay_buffer (pfile, pfile->out.base,
287                            pfile->out.cur - pfile->out.base);
288     }
289
290   /* Stop ISO C from expanding anything.  */
291   pfile->state.prevent_expansion++;
292 }
293
294 /* Output diagnostics for a directive DIR.  INDENTED is nonzero if
295    the '#' was indented.  */
296 static void
297 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
298 {
299   /* Issue -pedantic warnings for extensions.  */
300   if (CPP_PEDANTIC (pfile)
301       && ! pfile->state.skipping
302       && dir->origin == EXTENSION)
303     cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
304
305   /* Traditionally, a directive is ignored unless its # is in
306      column 1.  Therefore in code intended to work with K+R
307      compilers, directives added by C89 must have their #
308      indented, and directives present in traditional C must not.
309      This is true even of directives in skipped conditional
310      blocks.  #elif cannot be used at all.  */
311   if (CPP_WTRADITIONAL (pfile))
312     {
313       if (dir == &dtable[T_ELIF])
314         cpp_error (pfile, CPP_DL_WARNING,
315                    "suggest not using #elif in traditional C");
316       else if (indented && dir->origin == KANDR)
317         cpp_error (pfile, CPP_DL_WARNING,
318                    "traditional C ignores #%s with the # indented",
319                    dir->name);
320       else if (!indented && dir->origin != KANDR)
321         cpp_error (pfile, CPP_DL_WARNING,
322                    "suggest hiding #%s from traditional C with an indented #",
323                    dir->name);
324     }
325 }
326
327 /* Check if we have a known directive.  INDENTED is nonzero if the
328    '#' of the directive was indented.  This function is in this file
329    to save unnecessarily exporting dtable etc. to lex.c.  Returns
330    nonzero if the line of tokens has been handled, zero if we should
331    continue processing the line.  */
332 int
333 _cpp_handle_directive (cpp_reader *pfile, int indented)
334 {
335   const directive *dir = 0;
336   const cpp_token *dname;
337   bool was_parsing_args = pfile->state.parsing_args;
338   bool was_discarding_output = pfile->state.discarding_output;
339   int skip = 1;
340
341   if (was_discarding_output)
342     pfile->state.prevent_expansion = 0;
343
344   if (was_parsing_args)
345     {
346       if (CPP_OPTION (pfile, pedantic))
347         cpp_error (pfile, CPP_DL_PEDWARN,
348              "embedding a directive within macro arguments is not portable");
349       pfile->state.parsing_args = 0;
350       pfile->state.prevent_expansion = 0;
351     }
352   start_directive (pfile);
353   dname = _cpp_lex_token (pfile);
354
355   if (dname->type == CPP_NAME)
356     {
357       if (dname->val.node->is_directive)
358         dir = &dtable[dname->val.node->directive_index];
359     }
360   /* We do not recognize the # followed by a number extension in
361      assembler code.  */
362   else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
363     {
364       dir = &linemarker_dir;
365       if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
366           && ! pfile->state.skipping)
367         cpp_error (pfile, CPP_DL_PEDWARN,
368                    "style of line directive is a GCC extension");
369     }
370
371   if (dir)
372     {
373       /* If we have a directive that is not an opening conditional,
374          invalidate any control macro.  */
375       if (! (dir->flags & IF_COND))
376         pfile->mi_valid = false;
377
378       /* Kluge alert.  In order to be sure that code like this
379
380          #define HASH #
381          HASH define foo bar
382
383          does not cause '#define foo bar' to get executed when
384          compiled with -save-temps, we recognize directives in
385          -fpreprocessed mode only if the # is in column 1.  macro.c
386          puts a space in front of any '#' at the start of a macro.  */
387       if (CPP_OPTION (pfile, preprocessed)
388           && (indented || !(dir->flags & IN_I)))
389         {
390           skip = 0;
391           dir = 0;
392         }
393       else
394         {
395           /* In failed conditional groups, all non-conditional
396              directives are ignored.  Before doing that, whether
397              skipping or not, we should lex angle-bracketed headers
398              correctly, and maybe output some diagnostics.  */
399           pfile->state.angled_headers = dir->flags & INCL;
400           pfile->state.directive_wants_padding = dir->flags & INCL;
401           if (! CPP_OPTION (pfile, preprocessed))
402             directive_diagnostics (pfile, dir, indented);
403           if (pfile->state.skipping && !(dir->flags & COND))
404             dir = 0;
405         }
406     }
407   else if (dname->type == CPP_EOF)
408     ;   /* CPP_EOF is the "null directive".  */
409   else
410     {
411       /* An unknown directive.  Don't complain about it in assembly
412          source: we don't know where the comments are, and # may
413          introduce assembler pseudo-ops.  Don't complain about invalid
414          directives in skipped conditional groups (6.10 p4).  */
415       if (CPP_OPTION (pfile, lang) == CLK_ASM)
416         skip = 0;
417       else if (!pfile->state.skipping)
418         cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
419                    cpp_token_as_text (pfile, dname));
420     }
421
422   pfile->directive = dir;
423   if (CPP_OPTION (pfile, traditional))
424     prepare_directive_trad (pfile);
425
426   if (dir)
427     pfile->directive->handler (pfile);
428   else if (skip == 0)
429     _cpp_backup_tokens (pfile, 1);
430
431   end_directive (pfile, skip);
432   if (was_parsing_args)
433     {
434       /* Restore state when within macro args.  */
435       pfile->state.parsing_args = 2;
436       pfile->state.prevent_expansion = 1;
437     }
438   if (was_discarding_output)
439     pfile->state.prevent_expansion = 1;
440   return skip;
441 }
442
443 /* Directive handler wrapper used by the command line option
444    processor.  BUF is \n terminated.  */
445 static void
446 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
447 {
448   cpp_push_buffer (pfile, (const uchar *) buf, count,
449                    /* from_stage3 */ true);
450   /* Disgusting hack.  */
451   if (dir_no == T_PRAGMA && pfile->buffer->prev)
452     pfile->buffer->file = pfile->buffer->prev->file;
453   start_directive (pfile);
454
455   /* This is a short-term fix to prevent a leading '#' being
456      interpreted as a directive.  */
457   _cpp_clean_line (pfile);
458
459   pfile->directive = &dtable[dir_no];
460   if (CPP_OPTION (pfile, traditional))
461     prepare_directive_trad (pfile);
462   pfile->directive->handler (pfile);
463   end_directive (pfile, 1);
464   if (dir_no == T_PRAGMA)
465     pfile->buffer->file = NULL;
466   _cpp_pop_buffer (pfile);
467 }
468
469 /* Checks for validity the macro name in #define, #undef, #ifdef and
470    #ifndef directives.  */
471 static cpp_hashnode *
472 lex_macro_node (cpp_reader *pfile)
473 {
474   const cpp_token *token = _cpp_lex_token (pfile);
475
476   /* The token immediately after #define must be an identifier.  That
477      identifier may not be "defined", per C99 6.10.8p4.
478      In C++, it may not be any of the "named operators" either,
479      per C++98 [lex.digraph], [lex.key].
480      Finally, the identifier may not have been poisoned.  (In that case
481      the lexer has issued the error message for us.)  */
482
483   if (token->type == CPP_NAME)
484     {
485       cpp_hashnode *node = token->val.node;
486
487       if (node == pfile->spec_nodes.n_defined)
488         cpp_error (pfile, CPP_DL_ERROR,
489                    "\"defined\" cannot be used as a macro name");
490       else if (! (node->flags & NODE_POISONED))
491         return node;
492     }
493   else if (token->flags & NAMED_OP)
494     cpp_error (pfile, CPP_DL_ERROR,
495        "\"%s\" cannot be used as a macro name as it is an operator in C++",
496                NODE_NAME (token->val.node));
497   else if (token->type == CPP_EOF)
498     cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
499                pfile->directive->name);
500   else
501     cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
502
503   return NULL;
504 }
505
506 /* Process a #define directive.  Most work is done in macro.c.  */
507 static void
508 do_define (cpp_reader *pfile)
509 {
510   cpp_hashnode *node = lex_macro_node (pfile);
511
512   if (node)
513     {
514       /* If we have been requested to expand comments into macros,
515          then re-enable saving of comments.  */
516       pfile->state.save_comments =
517         ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
518
519       if (_cpp_create_definition (pfile, node))
520         if (pfile->cb.define)
521           pfile->cb.define (pfile, pfile->directive_line, node);
522     }
523 }
524
525 /* Handle #undef.  Mark the identifier NT_VOID in the hash table.  */
526 static void
527 do_undef (cpp_reader *pfile)
528 {
529   cpp_hashnode *node = lex_macro_node (pfile);
530
531   if (node)
532     {
533       if (pfile->cb.undef)
534         pfile->cb.undef (pfile, pfile->directive_line, node);
535
536       /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
537          identifier is not currently defined as a macro name.  */
538       if (node->type == NT_MACRO)
539         {
540           if (node->flags & NODE_WARN)
541             cpp_error (pfile, CPP_DL_WARNING,
542                        "undefining \"%s\"", NODE_NAME (node));
543
544           if (CPP_OPTION (pfile, warn_unused_macros))
545             _cpp_warn_if_unused_macro (pfile, node, NULL);
546
547           _cpp_free_definition (node);
548         }
549     }
550
551   check_eol (pfile);
552 }
553
554 /* Undefine a single macro/assertion/whatever.  */
555
556 static int
557 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
558                  void *data_p ATTRIBUTE_UNUSED)
559 {
560   /* Body of _cpp_free_definition inlined here for speed.
561      Macros and assertions no longer have anything to free.  */
562   h->type = NT_VOID;
563   h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
564   return 1;
565 }
566
567 /* Undefine all macros and assertions.  */
568
569 void
570 cpp_undef_all (cpp_reader *pfile)
571 {
572   cpp_forall_identifiers (pfile, undefine_macros, NULL);
573 }
574
575
576 /* Helper routine used by parse_include.  Reinterpret the current line
577    as an h-char-sequence (< ... >); we are looking at the first token
578    after the <.  Returns a malloced filename.  */
579 static char *
580 glue_header_name (cpp_reader *pfile)
581 {
582   const cpp_token *token;
583   char *buffer;
584   size_t len, total_len = 0, capacity = 1024;
585
586   /* To avoid lexed tokens overwriting our glued name, we can only
587      allocate from the string pool once we've lexed everything.  */
588   buffer = xmalloc (capacity);
589   for (;;)
590     {
591       token = get_token_no_padding (pfile);
592
593       if (token->type == CPP_GREATER)
594         break;
595       if (token->type == CPP_EOF)
596         {
597           cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
598           break;
599         }
600
601       len = cpp_token_len (token) + 2; /* Leading space, terminating \0.  */
602       if (total_len + len > capacity)
603         {
604           capacity = (capacity + len) * 2;
605           buffer = xrealloc (buffer, capacity);
606         }
607
608       if (token->flags & PREV_WHITE)
609         buffer[total_len++] = ' ';
610
611       total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
612                    - (uchar *) buffer);
613     }
614
615   buffer[total_len] = '\0';
616   return buffer;
617 }
618
619 /* Returns the file name of #include, #include_next, #import and
620    #pragma dependency.  The string is malloced and the caller should
621    free it.  Returns NULL on error.  */
622 static const char *
623 parse_include (cpp_reader *pfile, int *pangle_brackets)
624 {
625   char *fname;
626   const cpp_token *header;
627
628   /* Allow macro expansion.  */
629   header = get_token_no_padding (pfile);
630   if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
631     {
632       fname = xmalloc (header->val.str.len - 1);
633       memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
634       fname[header->val.str.len - 2] = '\0';
635       *pangle_brackets = header->type == CPP_HEADER_NAME;
636     }
637   else if (header->type == CPP_LESS)
638     {
639       fname = glue_header_name (pfile);
640       *pangle_brackets = 1;
641     }
642   else
643     {
644       const unsigned char *dir;
645
646       if (pfile->directive == &dtable[T_PRAGMA])
647         dir = U"pragma dependency";
648       else
649         dir = pfile->directive->name;
650       cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
651                  dir);
652
653       return NULL;
654     }
655
656   check_eol (pfile);
657   return fname;
658 }
659
660 /* Handle #include, #include_next and #import.  */
661 static void
662 do_include_common (cpp_reader *pfile, enum include_type type)
663 {
664   const char *fname;
665   int angle_brackets;
666
667   fname = parse_include (pfile, &angle_brackets);
668   if (!fname)
669     return;
670
671   if (!*fname)
672   {
673     cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
674                pfile->directive->name);
675     free ((void *) fname);
676     return;
677   }
678
679   /* Prevent #include recursion.  */
680   if (pfile->line_table->depth >= CPP_STACK_MAX)
681     cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
682   else
683     {
684       /* Get out of macro context, if we are.  */
685       skip_rest_of_line (pfile);
686
687       if (pfile->cb.include)
688         pfile->cb.include (pfile, pfile->directive_line,
689                            pfile->directive->name, fname, angle_brackets);
690
691       _cpp_stack_include (pfile, fname, angle_brackets, type);
692     }
693
694   free ((void *) fname);
695 }
696
697 static void
698 do_include (cpp_reader *pfile)
699 {
700   do_include_common (pfile, IT_INCLUDE);
701 }
702
703 static void
704 do_import (cpp_reader *pfile)
705 {
706   do_include_common (pfile, IT_IMPORT);
707 }
708
709 static void
710 do_include_next (cpp_reader *pfile)
711 {
712   enum include_type type = IT_INCLUDE_NEXT;
713
714   /* If this is the primary source file, warn and use the normal
715      search logic.  */
716   if (! pfile->buffer->prev)
717     {
718       cpp_error (pfile, CPP_DL_WARNING,
719                  "#include_next in primary source file");
720       type = IT_INCLUDE;
721     }
722   do_include_common (pfile, type);
723 }
724
725 /* Subroutine of do_linemarker.  Read possible flags after file name.
726    LAST is the last flag seen; 0 if this is the first flag. Return the
727    flag if it is valid, 0 at the end of the directive. Otherwise
728    complain.  */
729 static unsigned int
730 read_flag (cpp_reader *pfile, unsigned int last)
731 {
732   const cpp_token *token = _cpp_lex_token (pfile);
733
734   if (token->type == CPP_NUMBER && token->val.str.len == 1)
735     {
736       unsigned int flag = token->val.str.text[0] - '0';
737
738       if (flag > last && flag <= 4
739           && (flag != 4 || last == 3)
740           && (flag != 2 || last == 0))
741         return flag;
742     }
743
744   if (token->type != CPP_EOF)
745     cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
746                cpp_token_as_text (pfile, token));
747   return 0;
748 }
749
750 /* Subroutine of do_line and do_linemarker.  Convert a number in STR,
751    of length LEN, to binary; store it in NUMP, and return 0 if the
752    number was well-formed, 1 if not.  Temporary, hopefully.  */
753 static int
754 strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
755 {
756   unsigned long reg = 0;
757   uchar c;
758   while (len--)
759     {
760       c = *str++;
761       if (!ISDIGIT (c))
762         return 1;
763       reg *= 10;
764       reg += c - '0';
765     }
766   *nump = reg;
767   return 0;
768 }
769
770 /* Interpret #line command.
771    Note that the filename string (if any) is a true string constant
772    (escapes are interpreted), unlike in #line.  */
773 static void
774 do_line (cpp_reader *pfile)
775 {
776   const struct line_maps *line_table = pfile->line_table;
777   const struct line_map *map = &line_table->maps[line_table->used - 1];
778
779   /* skip_rest_of_line() may cause line table to be realloc()ed so note down
780      sysp right now.  */
781
782   unsigned char map_sysp = map->sysp;
783   const cpp_token *token;
784   const char *new_file = map->to_file;
785   unsigned long new_lineno;
786
787   /* C99 raised the minimum limit on #line numbers.  */
788   unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
789
790   /* #line commands expand macros.  */
791   token = cpp_get_token (pfile);
792   if (token->type != CPP_NUMBER
793       || strtoul_for_line (token->val.str.text, token->val.str.len,
794                            &new_lineno))
795     {
796       cpp_error (pfile, CPP_DL_ERROR,
797                  "\"%s\" after #line is not a positive integer",
798                  cpp_token_as_text (pfile, token));
799       return;
800     }
801
802   if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
803     cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
804
805   token = cpp_get_token (pfile);
806   if (token->type == CPP_STRING)
807     {
808       cpp_string s = { 0, 0 };
809       if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
810                                             &s, false))
811         new_file = (const char *)s.text;
812       check_eol (pfile);
813     }
814   else if (token->type != CPP_EOF)
815     {
816       cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
817                  cpp_token_as_text (pfile, token));
818       return;
819     }
820
821   skip_rest_of_line (pfile);
822   _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
823                        map_sysp);
824 }
825
826 /* Interpret the # 44 "file" [flags] notation, which has slightly
827    different syntax and semantics from #line:  Flags are allowed,
828    and we never complain about the line number being too big.  */
829 static void
830 do_linemarker (cpp_reader *pfile)
831 {
832   const struct line_maps *line_table = pfile->line_table;
833   const struct line_map *map = &line_table->maps[line_table->used - 1];
834   const cpp_token *token;
835   const char *new_file = map->to_file;
836   unsigned long new_lineno;
837   unsigned int new_sysp = map->sysp;
838   enum lc_reason reason = LC_RENAME;
839   int flag;
840
841   /* Back up so we can get the number again.  Putting this in
842      _cpp_handle_directive risks two calls to _cpp_backup_tokens in
843      some circumstances, which can segfault.  */
844   _cpp_backup_tokens (pfile, 1);
845
846   /* #line commands expand macros.  */
847   token = cpp_get_token (pfile);
848   if (token->type != CPP_NUMBER
849       || strtoul_for_line (token->val.str.text, token->val.str.len,
850                            &new_lineno))
851     {
852       cpp_error (pfile, CPP_DL_ERROR,
853                  "\"%s\" after # is not a positive integer",
854                  cpp_token_as_text (pfile, token));
855       return;
856     }
857
858   token = cpp_get_token (pfile);
859   if (token->type == CPP_STRING)
860     {
861       cpp_string s = { 0, 0 };
862       if (cpp_interpret_string_notranslate (pfile, &token->val.str,
863                                             1, &s, false))
864         new_file = (const char *)s.text;
865
866       new_sysp = 0;
867       flag = read_flag (pfile, 0);
868       if (flag == 1)
869         {
870           reason = LC_ENTER;
871           /* Fake an include for cpp_included ().  */
872           _cpp_fake_include (pfile, new_file);
873           flag = read_flag (pfile, flag);
874         }
875       else if (flag == 2)
876         {
877           reason = LC_LEAVE;
878           flag = read_flag (pfile, flag);
879         }
880       if (flag == 3)
881         {
882           new_sysp = 1;
883           flag = read_flag (pfile, flag);
884           if (flag == 4)
885             new_sysp = 2;
886           pfile->buffer->sysp = new_sysp;
887         }
888
889       check_eol (pfile);
890     }
891   else if (token->type != CPP_EOF)
892     {
893       cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
894                  cpp_token_as_text (pfile, token));
895       return;
896     }
897
898   skip_rest_of_line (pfile);
899   _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
900 }
901
902 /* Arrange the file_change callback.  pfile->line has changed to
903    FILE_LINE of TO_FILE, for reason REASON.  SYSP is 1 for a system
904    header, 2 for a system header that needs to be extern "C" protected,
905    and zero otherwise.  */
906 void
907 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
908                      const char *to_file, unsigned int file_line,
909                      unsigned int sysp)
910 {
911   const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
912                                             to_file, file_line);
913   if (map != NULL)
914     linemap_line_start (pfile->line_table, map->to_line, 127);
915
916   if (pfile->cb.file_change)
917     pfile->cb.file_change (pfile, map);
918 }
919
920 /* Report a warning or error detected by the program we are
921    processing.  Use the directive's tokens in the error message.  */
922 static void
923 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
924 {
925   if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
926     {
927       if (print_dir)
928         fprintf (stderr, "#%s ", pfile->directive->name);
929       pfile->state.prevent_expansion++;
930       cpp_output_line (pfile, stderr);
931       pfile->state.prevent_expansion--;
932     }
933 }
934
935 static void
936 do_error (cpp_reader *pfile)
937 {
938   do_diagnostic (pfile, CPP_DL_ERROR, 1);
939 }
940
941 static void
942 do_warning (cpp_reader *pfile)
943 {
944   /* We want #warning diagnostics to be emitted in system headers too.  */
945   do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
946 }
947
948 /* Report program identification.  */
949 static void
950 do_ident (cpp_reader *pfile)
951 {
952   const cpp_token *str = cpp_get_token (pfile);
953
954   if (str->type != CPP_STRING)
955     cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive");
956   else if (pfile->cb.ident)
957     pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
958
959   check_eol (pfile);
960 }
961
962 /* Lookup a PRAGMA name in a singly-linked CHAIN.  Returns the
963    matching entry, or NULL if none is found.  The returned entry could
964    be the start of a namespace chain, or a pragma.  */
965 static struct pragma_entry *
966 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
967 {
968   while (chain && chain->pragma != pragma)
969     chain = chain->next;
970
971   return chain;
972 }
973
974 /* Create and insert a pragma entry for NAME at the beginning of a
975    singly-linked CHAIN.  If handler is NULL, it is a namespace,
976    otherwise it is a pragma and its handler.  If INTERNAL is true
977    this pragma is being inserted by libcpp itself. */
978 static struct pragma_entry *
979 insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
980                      const cpp_hashnode *pragma, pragma_cb handler,
981                      bool allow_expansion, bool internal)
982 {
983   struct pragma_entry *new;
984
985   new = (struct pragma_entry *)
986     _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
987   new->pragma = pragma;
988   if (handler)
989     {
990       new->is_nspace = 0;
991       new->u.handler = handler;
992     }
993   else
994     {
995       new->is_nspace = 1;
996       new->u.space = NULL;
997     }
998
999   new->allow_expansion = allow_expansion;
1000   new->is_internal = internal;
1001   new->next = *chain;
1002   *chain = new;
1003   return new;
1004 }
1005
1006 /* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1007    goes in the global namespace.  HANDLER is the handler it will call,
1008    which must be non-NULL.  If ALLOW_EXPANSION is set, allow macro
1009    expansion while parsing pragma NAME.  INTERNAL is true if this is a
1010    pragma registered by cpplib itself, false if it is registered via
1011    cpp_register_pragma */
1012 static void
1013 register_pragma (cpp_reader *pfile, const char *space, const char *name,
1014                  pragma_cb handler, bool allow_expansion, bool internal)
1015 {
1016   struct pragma_entry **chain = &pfile->pragmas;
1017   struct pragma_entry *entry;
1018   const cpp_hashnode *node;
1019
1020   if (!handler)
1021     abort ();
1022
1023   if (space)
1024     {
1025       node = cpp_lookup (pfile, U space, strlen (space));
1026       entry = lookup_pragma_entry (*chain, node);
1027       if (!entry)
1028         entry = insert_pragma_entry (pfile, chain, node, NULL, 
1029                                      allow_expansion, internal);
1030       else if (!entry->is_nspace)
1031         goto clash;
1032       chain = &entry->u.space;
1033     }
1034
1035   /* Check for duplicates.  */
1036   node = cpp_lookup (pfile, U name, strlen (name));
1037   entry = lookup_pragma_entry (*chain, node);
1038   if (entry)
1039     {
1040       if (entry->is_nspace)
1041         clash:
1042         cpp_error (pfile, CPP_DL_ICE,
1043                  "registering \"%s\" as both a pragma and a pragma namespace",
1044                  NODE_NAME (node));
1045       else if (space)
1046         cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1047                    space, name);
1048       else
1049         cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1050     }
1051   else
1052     insert_pragma_entry (pfile, chain, node, handler, allow_expansion, 
1053                          internal);
1054 }
1055
1056 /* Register a pragma NAME in namespace SPACE.  If SPACE is null, it
1057    goes in the global namespace.  HANDLER is the handler it will call,
1058    which must be non-NULL.  If ALLOW_EXPANSION is set, allow macro
1059    expansion while parsing pragma NAME.  This function is exported
1060    from libcpp. */
1061 void
1062 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1063                      pragma_cb handler, bool allow_expansion)
1064 {
1065   register_pragma (pfile, space, name, handler, allow_expansion, false);
1066 }
1067
1068 /* Register the pragmas the preprocessor itself handles.  */
1069 void
1070 _cpp_init_internal_pragmas (cpp_reader *pfile)
1071 {
1072   /* Pragmas in the global namespace.  */
1073   register_pragma (pfile, 0, "once", do_pragma_once, false, true);
1074
1075   /* New GCC-specific pragmas should be put in the GCC namespace.  */
1076   register_pragma (pfile, "GCC", "poison", do_pragma_poison, false, true);
1077   register_pragma (pfile, "GCC", "system_header", do_pragma_system_header, 
1078                    false, true);
1079   register_pragma (pfile, "GCC", "dependency", do_pragma_dependency, 
1080                    false, true);
1081 }
1082
1083 /* Return the number of registered pragmas in PE.  */
1084
1085 static int
1086 count_registered_pragmas (struct pragma_entry *pe)
1087 {
1088   int ct = 0;
1089   for (; pe != NULL; pe = pe->next)
1090     {
1091       if (pe->is_nspace)
1092         ct += count_registered_pragmas (pe->u.space);
1093       ct++;
1094     }
1095   return ct;
1096 }
1097
1098 /* Save into SD the names of the registered pragmas referenced by PE,
1099    and return a pointer to the next free space in SD.  */
1100
1101 static char **
1102 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1103 {
1104   for (; pe != NULL; pe = pe->next)
1105     {
1106       if (pe->is_nspace)
1107         sd = save_registered_pragmas (pe->u.space, sd);
1108       *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1109                        HT_LEN (&pe->pragma->ident),
1110                        HT_LEN (&pe->pragma->ident) + 1);
1111     }
1112   return sd;
1113 }
1114
1115 /* Return a newly-allocated array which saves the names of the
1116    registered pragmas.  */
1117
1118 char **
1119 _cpp_save_pragma_names (cpp_reader *pfile)
1120 {
1121   int ct = count_registered_pragmas (pfile->pragmas);
1122   char **result = XNEWVEC (char *, ct);
1123   (void) save_registered_pragmas (pfile->pragmas, result);
1124   return result;
1125 }
1126
1127 /* Restore from SD the names of the registered pragmas referenced by PE,
1128    and return a pointer to the next unused name in SD.  */
1129
1130 static char **
1131 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1132                             char **sd)
1133 {
1134   for (; pe != NULL; pe = pe->next)
1135     {
1136       if (pe->is_nspace)
1137         sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1138       pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1139       free (*sd);
1140       sd++;
1141     }
1142   return sd;
1143 }
1144
1145 /* Restore the names of the registered pragmas from SAVED.  */
1146
1147 void
1148 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1149 {
1150   (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1151   free (saved);
1152 }
1153
1154 /* Pragmata handling.  We handle some, and pass the rest on to the
1155    front end.  C99 defines three pragmas and says that no macro
1156    expansion is to be performed on them; whether or not macro
1157    expansion happens for other pragmas is implementation defined.
1158    This implementation never macro-expands the text after #pragma.
1159
1160    The library user has the option of deferring execution of
1161    #pragmas not handled by cpplib, in which case they are converted
1162    to CPP_PRAGMA tokens and inserted into the output stream.  */
1163 static void
1164 do_pragma (cpp_reader *pfile)
1165 {
1166   const struct pragma_entry *p = NULL;
1167   const cpp_token *token, *pragma_token = pfile->cur_token;
1168   unsigned int count = 1;
1169
1170   /* Save the current position so that defer_pragmas mode can
1171      copy the entire current line to a string.  It will not work
1172      to use _cpp_backup_tokens as that does not reverse buffer->cur.  */
1173   const uchar *line_start = CPP_BUFFER (pfile)->cur;
1174
1175   pfile->state.prevent_expansion++;
1176
1177   token = cpp_get_token (pfile);
1178   if (token->type == CPP_NAME)
1179     {
1180       p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1181       if (p && p->is_nspace)
1182         {
1183           count = 2;
1184           token = cpp_get_token (pfile);
1185           if (token->type == CPP_NAME)
1186             p = lookup_pragma_entry (p->u.space, token->val.node);
1187           else
1188             p = NULL;
1189         }
1190     }
1191
1192   if (p)
1193     {
1194       if (p->is_internal || !CPP_OPTION (pfile, defer_pragmas))
1195         {
1196           /* Since the handler below doesn't get the line number, that it
1197              might need for diagnostics, make sure it has the right
1198              numbers in place.  */
1199           if (pfile->cb.line_change)
1200             (*pfile->cb.line_change) (pfile, pragma_token, false);
1201           /* Never expand macros if handling a deferred pragma, since
1202              the macro definitions now applicable may be different
1203              from those at the point the pragma appeared.  */
1204           if (p->allow_expansion && !pfile->state.in_deferred_pragma)
1205             pfile->state.prevent_expansion--;
1206           (*p->u.handler) (pfile);
1207           if (p->allow_expansion && !pfile->state.in_deferred_pragma)
1208             pfile->state.prevent_expansion++;
1209         }
1210       else
1211         {
1212           /* Squirrel away the pragma text.  Pragmas are
1213              newline-terminated. */
1214           const uchar *line_end;
1215           uchar *s;
1216           cpp_string body;
1217           cpp_token *ptok;
1218
1219           line_end = ustrchr (line_start, '\n');
1220
1221           body.len = (line_end - line_start) + 1;
1222           s = _cpp_unaligned_alloc (pfile, body.len + 1);
1223           memcpy (s, line_start, body.len);
1224           s[body.len] = '\0';
1225           body.text = s;
1226
1227           /* Create a CPP_PRAGMA token.  */
1228           ptok = &pfile->directive_result;
1229           ptok->src_loc = pragma_token->src_loc;
1230           ptok->type = CPP_PRAGMA;
1231           ptok->flags = pragma_token->flags | NO_EXPAND;
1232           ptok->val.str = body;
1233         }
1234     }
1235   else if (pfile->cb.def_pragma)
1236     {
1237       _cpp_backup_tokens (pfile, count);
1238       pfile->cb.def_pragma (pfile, pfile->directive_line);
1239     }
1240
1241   pfile->state.prevent_expansion--;
1242 }
1243
1244 /* Handle #pragma once.  */
1245 static void
1246 do_pragma_once (cpp_reader *pfile)
1247 {
1248   if (pfile->buffer->prev == NULL)
1249     cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1250
1251   check_eol (pfile);
1252   _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1253 }
1254
1255 /* Handle #pragma GCC poison, to poison one or more identifiers so
1256    that the lexer produces a hard error for each subsequent usage.  */
1257 static void
1258 do_pragma_poison (cpp_reader *pfile)
1259 {
1260   const cpp_token *tok;
1261   cpp_hashnode *hp;
1262
1263   pfile->state.poisoned_ok = 1;
1264   for (;;)
1265     {
1266       tok = _cpp_lex_token (pfile);
1267       if (tok->type == CPP_EOF)
1268         break;
1269       if (tok->type != CPP_NAME)
1270         {
1271           cpp_error (pfile, CPP_DL_ERROR,
1272                      "invalid #pragma GCC poison directive");
1273           break;
1274         }
1275
1276       hp = tok->val.node;
1277       if (hp->flags & NODE_POISONED)
1278         continue;
1279
1280       if (hp->type == NT_MACRO)
1281         cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1282                    NODE_NAME (hp));
1283       _cpp_free_definition (hp);
1284       hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1285     }
1286   pfile->state.poisoned_ok = 0;
1287 }
1288
1289 /* Mark the current header as a system header.  This will suppress
1290    some categories of warnings (notably those from -pedantic).  It is
1291    intended for use in system libraries that cannot be implemented in
1292    conforming C, but cannot be certain that their headers appear in a
1293    system include directory.  To prevent abuse, it is rejected in the
1294    primary source file.  */
1295 static void
1296 do_pragma_system_header (cpp_reader *pfile)
1297 {
1298   cpp_buffer *buffer = pfile->buffer;
1299
1300   if (buffer->prev == 0)
1301     cpp_error (pfile, CPP_DL_WARNING,
1302                "#pragma system_header ignored outside include file");
1303   else
1304     {
1305       check_eol (pfile);
1306       skip_rest_of_line (pfile);
1307       cpp_make_system_header (pfile, 1, 0);
1308     }
1309 }
1310
1311 /* Check the modified date of the current include file against a specified
1312    file. Issue a diagnostic, if the specified file is newer. We use this to
1313    determine if a fixed header should be refixed.  */
1314 static void
1315 do_pragma_dependency (cpp_reader *pfile)
1316 {
1317   const char *fname;
1318   int angle_brackets, ordering;
1319
1320   fname = parse_include (pfile, &angle_brackets);
1321   if (!fname)
1322     return;
1323
1324   ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1325   if (ordering < 0)
1326     cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1327   else if (ordering > 0)
1328     {
1329       cpp_error (pfile, CPP_DL_WARNING,
1330                  "current file is older than %s", fname);
1331       if (cpp_get_token (pfile)->type != CPP_EOF)
1332         {
1333           _cpp_backup_tokens (pfile, 1);
1334           do_diagnostic (pfile, CPP_DL_WARNING, 0);
1335         }
1336     }
1337
1338   free ((void *) fname);
1339 }
1340
1341 /* Get a token but skip padding.  */
1342 static const cpp_token *
1343 get_token_no_padding (cpp_reader *pfile)
1344 {
1345   for (;;)
1346     {
1347       const cpp_token *result = cpp_get_token (pfile);
1348       if (result->type != CPP_PADDING)
1349         return result;
1350     }
1351 }
1352
1353 /* Check syntax is "(string-literal)".  Returns the string on success,
1354    or NULL on failure.  */
1355 static const cpp_token *
1356 get__Pragma_string (cpp_reader *pfile)
1357 {
1358   const cpp_token *string;
1359
1360   if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1361     return NULL;
1362
1363   string = get_token_no_padding (pfile);
1364   if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1365     return NULL;
1366
1367   if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1368     return NULL;
1369
1370   return string;
1371 }
1372
1373 /* Destringize IN into a temporary buffer, by removing the first \ of
1374    \" and \\ sequences, and process the result as a #pragma directive.  */
1375 static void
1376 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1377 {
1378   const unsigned char *src, *limit;
1379   char *dest, *result;
1380
1381   dest = result = alloca (in->len - 1);
1382   src = in->text + 1 + (in->text[0] == 'L');
1383   limit = in->text + in->len - 1;
1384   while (src < limit)
1385     {
1386       /* We know there is a character following the backslash.  */
1387       if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1388         src++;
1389       *dest++ = *src++;
1390     }
1391   *dest = '\n';
1392
1393   /* Ugh; an awful kludge.  We are really not set up to be lexing
1394      tokens when in the middle of a macro expansion.  Use a new
1395      context to force cpp_get_token to lex, and so skip_rest_of_line
1396      doesn't go beyond the end of the text.  Also, remember the
1397      current lexing position so we can return to it later.
1398
1399      Something like line-at-a-time lexing should remove the need for
1400      this.  */
1401   {
1402     cpp_context *saved_context = pfile->context;
1403     cpp_token *saved_cur_token = pfile->cur_token;
1404     tokenrun *saved_cur_run = pfile->cur_run;
1405
1406     pfile->context = XNEW (cpp_context);
1407     pfile->context->macro = 0;
1408     pfile->context->prev = 0;
1409     run_directive (pfile, T_PRAGMA, result, dest - result);
1410     XDELETE (pfile->context);
1411     pfile->context = saved_context;
1412     pfile->cur_token = saved_cur_token;
1413     pfile->cur_run = saved_cur_run;
1414   }
1415
1416   /* See above comment.  For the moment, we'd like
1417
1418      token1 _Pragma ("foo") token2
1419
1420      to be output as
1421
1422                 token1
1423                 # 7 "file.c"
1424                 #pragma foo
1425                 # 7 "file.c"
1426                                token2
1427
1428       Getting the line markers is a little tricky.  */
1429   if (pfile->cb.line_change)
1430     pfile->cb.line_change (pfile, pfile->cur_token, false);
1431 }
1432
1433 /* Handle the _Pragma operator.  */
1434 void
1435 _cpp_do__Pragma (cpp_reader *pfile)
1436 {
1437   const cpp_token *string = get__Pragma_string (pfile);
1438   pfile->directive_result.type = CPP_PADDING;
1439
1440   if (string)
1441     destringize_and_run (pfile, &string->val.str);
1442   else
1443     cpp_error (pfile, CPP_DL_ERROR,
1444                "_Pragma takes a parenthesized string literal");
1445 }
1446
1447 /* Handle a pragma that the front end deferred until now. */
1448 void
1449 cpp_handle_deferred_pragma (cpp_reader *pfile, const cpp_string *s)
1450 {
1451   cpp_context *saved_context = pfile->context;
1452   cpp_token *saved_cur_token = pfile->cur_token;
1453   tokenrun *saved_cur_run = pfile->cur_run;
1454   bool saved_defer_pragmas = CPP_OPTION (pfile, defer_pragmas);
1455   void (*saved_line_change) (cpp_reader *, const cpp_token *, int)
1456     = pfile->cb.line_change;
1457
1458   pfile->context = XNEW (cpp_context);
1459   pfile->context->macro = 0;
1460   pfile->context->prev = 0;
1461   pfile->cb.line_change = NULL;
1462   pfile->state.in_deferred_pragma = true;
1463   CPP_OPTION (pfile, defer_pragmas) = false;
1464
1465   run_directive (pfile, T_PRAGMA, (const char *)s->text, s->len);
1466
1467   XDELETE (pfile->context);
1468   pfile->context = saved_context;
1469   pfile->cur_token = saved_cur_token;
1470   pfile->cur_run = saved_cur_run;
1471   pfile->cb.line_change = saved_line_change;
1472   pfile->state.in_deferred_pragma = false;
1473   CPP_OPTION (pfile, defer_pragmas) = saved_defer_pragmas;
1474 }
1475
1476 /* Ignore #sccs on all systems.  */
1477 static void
1478 do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
1479 {
1480 }
1481
1482 /* Handle #ifdef.  */
1483 static void
1484 do_ifdef (cpp_reader *pfile)
1485 {
1486   int skip = 1;
1487
1488   if (! pfile->state.skipping)
1489     {
1490       const cpp_hashnode *node = lex_macro_node (pfile);
1491
1492       if (node)
1493         {
1494           skip = node->type != NT_MACRO;
1495           _cpp_mark_macro_used (node);
1496           check_eol (pfile);
1497         }
1498     }
1499
1500   push_conditional (pfile, skip, T_IFDEF, 0);
1501 }
1502
1503 /* Handle #ifndef.  */
1504 static void
1505 do_ifndef (cpp_reader *pfile)
1506 {
1507   int skip = 1;
1508   const cpp_hashnode *node = 0;
1509
1510   if (! pfile->state.skipping)
1511     {
1512       node = lex_macro_node (pfile);
1513
1514       if (node)
1515         {
1516           skip = node->type == NT_MACRO;
1517           _cpp_mark_macro_used (node);
1518           check_eol (pfile);
1519         }
1520     }
1521
1522   push_conditional (pfile, skip, T_IFNDEF, node);
1523 }
1524
1525 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1526    pfile->mi_ind_cmacro so we can handle multiple-include
1527    optimizations.  If macro expansion occurs in the expression, we
1528    cannot treat it as a controlling conditional, since the expansion
1529    could change in the future.  That is handled by cpp_get_token.  */
1530 static void
1531 do_if (cpp_reader *pfile)
1532 {
1533   int skip = 1;
1534
1535   if (! pfile->state.skipping)
1536     skip = _cpp_parse_expr (pfile) == false;
1537
1538   push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1539 }
1540
1541 /* Flip skipping state if appropriate and continue without changing
1542    if_stack; this is so that the error message for missing #endif's
1543    etc. will point to the original #if.  */
1544 static void
1545 do_else (cpp_reader *pfile)
1546 {
1547   cpp_buffer *buffer = pfile->buffer;
1548   struct if_stack *ifs = buffer->if_stack;
1549
1550   if (ifs == NULL)
1551     cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1552   else
1553     {
1554       if (ifs->type == T_ELSE)
1555         {
1556           cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1557           cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1558                                "the conditional began here");
1559         }
1560       ifs->type = T_ELSE;
1561
1562       /* Skip any future (erroneous) #elses or #elifs.  */
1563       pfile->state.skipping = ifs->skip_elses;
1564       ifs->skip_elses = true;
1565
1566       /* Invalidate any controlling macro.  */
1567       ifs->mi_cmacro = 0;
1568
1569       /* Only check EOL if was not originally skipping.  */
1570       if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1571         check_eol (pfile);
1572     }
1573 }
1574
1575 /* Handle a #elif directive by not changing if_stack either.  See the
1576    comment above do_else.  */
1577 static void
1578 do_elif (cpp_reader *pfile)
1579 {
1580   cpp_buffer *buffer = pfile->buffer;
1581   struct if_stack *ifs = buffer->if_stack;
1582
1583   if (ifs == NULL)
1584     cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1585   else
1586     {
1587       if (ifs->type == T_ELSE)
1588         {
1589           cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1590           cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1591                                "the conditional began here");
1592         }
1593       ifs->type = T_ELIF;
1594
1595       /* Only evaluate this if we aren't skipping elses.  During
1596          evaluation, set skipping to false to get lexer warnings.  */
1597       if (ifs->skip_elses)
1598         pfile->state.skipping = 1;
1599       else
1600         {
1601           pfile->state.skipping = 0;
1602           pfile->state.skipping = ! _cpp_parse_expr (pfile);
1603           ifs->skip_elses = ! pfile->state.skipping;
1604         }
1605
1606       /* Invalidate any controlling macro.  */
1607       ifs->mi_cmacro = 0;
1608     }
1609 }
1610
1611 /* #endif pops the if stack and resets pfile->state.skipping.  */
1612 static void
1613 do_endif (cpp_reader *pfile)
1614 {
1615   cpp_buffer *buffer = pfile->buffer;
1616   struct if_stack *ifs = buffer->if_stack;
1617
1618   if (ifs == NULL)
1619     cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1620   else
1621     {
1622       /* Only check EOL if was not originally skipping.  */
1623       if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1624         check_eol (pfile);
1625
1626       /* If potential control macro, we go back outside again.  */
1627       if (ifs->next == 0 && ifs->mi_cmacro)
1628         {
1629           pfile->mi_valid = true;
1630           pfile->mi_cmacro = ifs->mi_cmacro;
1631         }
1632
1633       buffer->if_stack = ifs->next;
1634       pfile->state.skipping = ifs->was_skipping;
1635       obstack_free (&pfile->buffer_ob, ifs);
1636     }
1637 }
1638
1639 /* Push an if_stack entry for a preprocessor conditional, and set
1640    pfile->state.skipping to SKIP.  If TYPE indicates the conditional
1641    is #if or #ifndef, CMACRO is a potentially controlling macro, and
1642    we need to check here that we are at the top of the file.  */
1643 static void
1644 push_conditional (cpp_reader *pfile, int skip, int type,
1645                   const cpp_hashnode *cmacro)
1646 {
1647   struct if_stack *ifs;
1648   cpp_buffer *buffer = pfile->buffer;
1649
1650   ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1651   ifs->line = pfile->directive_line;
1652   ifs->next = buffer->if_stack;
1653   ifs->skip_elses = pfile->state.skipping || !skip;
1654   ifs->was_skipping = pfile->state.skipping;
1655   ifs->type = type;
1656   /* This condition is effectively a test for top-of-file.  */
1657   if (pfile->mi_valid && pfile->mi_cmacro == 0)
1658     ifs->mi_cmacro = cmacro;
1659   else
1660     ifs->mi_cmacro = 0;
1661
1662   pfile->state.skipping = skip;
1663   buffer->if_stack = ifs;
1664 }
1665
1666 /* Read the tokens of the answer into the macro pool, in a directive
1667    of type TYPE.  Only commit the memory if we intend it as permanent
1668    storage, i.e. the #assert case.  Returns 0 on success, and sets
1669    ANSWERP to point to the answer.  */
1670 static int
1671 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1672 {
1673   const cpp_token *paren;
1674   struct answer *answer;
1675   unsigned int acount;
1676
1677   /* In a conditional, it is legal to not have an open paren.  We
1678      should save the following token in this case.  */
1679   paren = cpp_get_token (pfile);
1680
1681   /* If not a paren, see if we're OK.  */
1682   if (paren->type != CPP_OPEN_PAREN)
1683     {
1684       /* In a conditional no answer is a test for any answer.  It
1685          could be followed by any token.  */
1686       if (type == T_IF)
1687         {
1688           _cpp_backup_tokens (pfile, 1);
1689           return 0;
1690         }
1691
1692       /* #unassert with no answer is valid - it removes all answers.  */
1693       if (type == T_UNASSERT && paren->type == CPP_EOF)
1694         return 0;
1695
1696       cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1697       return 1;
1698     }
1699
1700   for (acount = 0;; acount++)
1701     {
1702       size_t room_needed;
1703       const cpp_token *token = cpp_get_token (pfile);
1704       cpp_token *dest;
1705
1706       if (token->type == CPP_CLOSE_PAREN)
1707         break;
1708
1709       if (token->type == CPP_EOF)
1710         {
1711           cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1712           return 1;
1713         }
1714
1715       /* struct answer includes the space for one token.  */
1716       room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1717
1718       if (BUFF_ROOM (pfile->a_buff) < room_needed)
1719         _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1720
1721       dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1722       *dest = *token;
1723
1724       /* Drop whitespace at start, for answer equivalence purposes.  */
1725       if (acount == 0)
1726         dest->flags &= ~PREV_WHITE;
1727     }
1728
1729   if (acount == 0)
1730     {
1731       cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1732       return 1;
1733     }
1734
1735   answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1736   answer->count = acount;
1737   answer->next = NULL;
1738   *answerp = answer;
1739
1740   return 0;
1741 }
1742
1743 /* Parses an assertion directive of type TYPE, returning a pointer to
1744    the hash node of the predicate, or 0 on error.  If an answer was
1745    supplied, it is placed in ANSWERP, otherwise it is set to 0.  */
1746 static cpp_hashnode *
1747 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1748 {
1749   cpp_hashnode *result = 0;
1750   const cpp_token *predicate;
1751
1752   /* We don't expand predicates or answers.  */
1753   pfile->state.prevent_expansion++;
1754
1755   *answerp = 0;
1756   predicate = cpp_get_token (pfile);
1757   if (predicate->type == CPP_EOF)
1758     cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1759   else if (predicate->type != CPP_NAME)
1760     cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1761   else if (parse_answer (pfile, answerp, type) == 0)
1762     {
1763       unsigned int len = NODE_LEN (predicate->val.node);
1764       unsigned char *sym = alloca (len + 1);
1765
1766       /* Prefix '#' to get it out of macro namespace.  */
1767       sym[0] = '#';
1768       memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1769       result = cpp_lookup (pfile, sym, len + 1);
1770     }
1771
1772   pfile->state.prevent_expansion--;
1773   return result;
1774 }
1775
1776 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1777    or a pointer to NULL if the answer is not in the chain.  */
1778 static struct answer **
1779 find_answer (cpp_hashnode *node, const struct answer *candidate)
1780 {
1781   unsigned int i;
1782   struct answer **result;
1783
1784   for (result = &node->value.answers; *result; result = &(*result)->next)
1785     {
1786       struct answer *answer = *result;
1787
1788       if (answer->count == candidate->count)
1789         {
1790           for (i = 0; i < answer->count; i++)
1791             if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1792               break;
1793
1794           if (i == answer->count)
1795             break;
1796         }
1797     }
1798
1799   return result;
1800 }
1801
1802 /* Test an assertion within a preprocessor conditional.  Returns
1803    nonzero on failure, zero on success.  On success, the result of
1804    the test is written into VALUE, otherwise the value 0.  */
1805 int
1806 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1807 {
1808   struct answer *answer;
1809   cpp_hashnode *node;
1810
1811   node = parse_assertion (pfile, &answer, T_IF);
1812
1813   /* For recovery, an erroneous assertion expression is handled as a
1814      failing assertion.  */
1815   *value = 0;
1816
1817   if (node)
1818     *value = (node->type == NT_ASSERTION &&
1819               (answer == 0 || *find_answer (node, answer) != 0));
1820   else if (pfile->cur_token[-1].type == CPP_EOF)
1821     _cpp_backup_tokens (pfile, 1);
1822
1823   /* We don't commit the memory for the answer - it's temporary only.  */
1824   return node == 0;
1825 }
1826
1827 /* Handle #assert.  */
1828 static void
1829 do_assert (cpp_reader *pfile)
1830 {
1831   struct answer *new_answer;
1832   cpp_hashnode *node;
1833
1834   node = parse_assertion (pfile, &new_answer, T_ASSERT);
1835   if (node)
1836     {
1837       size_t answer_size;
1838
1839       /* Place the new answer in the answer list.  First check there
1840          is not a duplicate.  */
1841       new_answer->next = 0;
1842       if (node->type == NT_ASSERTION)
1843         {
1844           if (*find_answer (node, new_answer))
1845             {
1846               cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
1847                          NODE_NAME (node) + 1);
1848               return;
1849             }
1850           new_answer->next = node->value.answers;
1851         }
1852
1853       answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1854                                               * sizeof (cpp_token));
1855       /* Commit or allocate storage for the object.  */
1856       if (pfile->hash_table->alloc_subobject)
1857         {
1858           struct answer *temp_answer = new_answer;
1859           new_answer = pfile->hash_table->alloc_subobject (answer_size);
1860           memcpy (new_answer, temp_answer, answer_size);
1861         }
1862       else
1863         BUFF_FRONT (pfile->a_buff) += answer_size;
1864
1865       node->type = NT_ASSERTION;
1866       node->value.answers = new_answer;
1867       check_eol (pfile);
1868     }
1869 }
1870
1871 /* Handle #unassert.  */
1872 static void
1873 do_unassert (cpp_reader *pfile)
1874 {
1875   cpp_hashnode *node;
1876   struct answer *answer;
1877
1878   node = parse_assertion (pfile, &answer, T_UNASSERT);
1879   /* It isn't an error to #unassert something that isn't asserted.  */
1880   if (node && node->type == NT_ASSERTION)
1881     {
1882       if (answer)
1883         {
1884           struct answer **p = find_answer (node, answer), *temp;
1885
1886           /* Remove the answer from the list.  */
1887           temp = *p;
1888           if (temp)
1889             *p = temp->next;
1890
1891           /* Did we free the last answer?  */
1892           if (node->value.answers == 0)
1893             node->type = NT_VOID;
1894
1895           check_eol (pfile);
1896         }
1897       else
1898         _cpp_free_definition (node);
1899     }
1900
1901   /* We don't commit the memory for the answer - it's temporary only.  */
1902 }
1903
1904 /* These are for -D, -U, -A.  */
1905
1906 /* Process the string STR as if it appeared as the body of a #define.
1907    If STR is just an identifier, define it with value 1.
1908    If STR has anything after the identifier, then it should
1909    be identifier=definition.  */
1910 void
1911 cpp_define (cpp_reader *pfile, const char *str)
1912 {
1913   char *buf, *p;
1914   size_t count;
1915
1916   /* Copy the entire option so we can modify it.
1917      Change the first "=" in the string to a space.  If there is none,
1918      tack " 1" on the end.  */
1919
1920   count = strlen (str);
1921   buf = alloca (count + 3);
1922   memcpy (buf, str, count);
1923
1924   p = strchr (str, '=');
1925   if (p)
1926     buf[p - str] = ' ';
1927   else
1928     {
1929       buf[count++] = ' ';
1930       buf[count++] = '1';
1931     }
1932   buf[count] = '\n';
1933
1934   run_directive (pfile, T_DEFINE, buf, count);
1935 }
1936
1937 /* Slight variant of the above for use by initialize_builtins.  */
1938 void
1939 _cpp_define_builtin (cpp_reader *pfile, const char *str)
1940 {
1941   size_t len = strlen (str);
1942   char *buf = alloca (len + 1);
1943   memcpy (buf, str, len);
1944   buf[len] = '\n';
1945   run_directive (pfile, T_DEFINE, buf, len);
1946 }
1947
1948 /* Process MACRO as if it appeared as the body of an #undef.  */
1949 void
1950 cpp_undef (cpp_reader *pfile, const char *macro)
1951 {
1952   size_t len = strlen (macro);
1953   char *buf = alloca (len + 1);
1954   memcpy (buf, macro, len);
1955   buf[len] = '\n';
1956   run_directive (pfile, T_UNDEF, buf, len);
1957 }
1958
1959 /* Process the string STR as if it appeared as the body of a #assert.  */
1960 void
1961 cpp_assert (cpp_reader *pfile, const char *str)
1962 {
1963   handle_assertion (pfile, str, T_ASSERT);
1964 }
1965
1966 /* Process STR as if it appeared as the body of an #unassert.  */
1967 void
1968 cpp_unassert (cpp_reader *pfile, const char *str)
1969 {
1970   handle_assertion (pfile, str, T_UNASSERT);
1971 }
1972
1973 /* Common code for cpp_assert (-A) and cpp_unassert (-A-).  */
1974 static void
1975 handle_assertion (cpp_reader *pfile, const char *str, int type)
1976 {
1977   size_t count = strlen (str);
1978   const char *p = strchr (str, '=');
1979
1980   /* Copy the entire option so we can modify it.  Change the first
1981      "=" in the string to a '(', and tack a ')' on the end.  */
1982   char *buf = alloca (count + 2);
1983
1984   memcpy (buf, str, count);
1985   if (p)
1986     {
1987       buf[p - str] = '(';
1988       buf[count++] = ')';
1989     }
1990   buf[count] = '\n';
1991   str = buf;
1992
1993   run_directive (pfile, type, str, count);
1994 }
1995
1996 /* The number of errors for a given reader.  */
1997 unsigned int
1998 cpp_errors (cpp_reader *pfile)
1999 {
2000   return pfile->errors;
2001 }
2002
2003 /* The options structure.  */
2004 cpp_options *
2005 cpp_get_options (cpp_reader *pfile)
2006 {
2007   return &pfile->opts;
2008 }
2009
2010 /* The callbacks structure.  */
2011 cpp_callbacks *
2012 cpp_get_callbacks (cpp_reader *pfile)
2013 {
2014   return &pfile->cb;
2015 }
2016
2017 /* Copy the given callbacks structure to our own.  */
2018 void
2019 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2020 {
2021   pfile->cb = *cb;
2022 }
2023
2024 /* The dependencies structure.  (Creates one if it hasn't already been.)  */
2025 struct deps *
2026 cpp_get_deps (cpp_reader *pfile)
2027 {
2028   if (!pfile->deps)
2029     pfile->deps = deps_init ();
2030   return pfile->deps;
2031 }
2032
2033 /* Push a new buffer on the buffer stack.  Returns the new buffer; it
2034    doesn't fail.  It does not generate a file change call back; that
2035    is the responsibility of the caller.  */
2036 cpp_buffer *
2037 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2038                  int from_stage3)
2039 {
2040   cpp_buffer *new = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2041
2042   /* Clears, amongst other things, if_stack and mi_cmacro.  */
2043   memset (new, 0, sizeof (cpp_buffer));
2044
2045   new->next_line = new->buf = buffer;
2046   new->rlimit = buffer + len;
2047   new->from_stage3 = from_stage3;
2048   new->prev = pfile->buffer;
2049   new->need_line = true;
2050
2051   pfile->buffer = new;
2052
2053   return new;
2054 }
2055
2056 /* Pops a single buffer, with a file change call-back if appropriate.
2057    Then pushes the next -include file, if any remain.  */
2058 void
2059 _cpp_pop_buffer (cpp_reader *pfile)
2060 {
2061   cpp_buffer *buffer = pfile->buffer;
2062   struct _cpp_file *inc = buffer->file;
2063   struct if_stack *ifs;
2064
2065   /* Walk back up the conditional stack till we reach its level at
2066      entry to this file, issuing error messages.  */
2067   for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2068     cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2069                          "unterminated #%s", dtable[ifs->type].name);
2070
2071   /* In case of a missing #endif.  */
2072   pfile->state.skipping = 0;
2073
2074   /* _cpp_do_file_change expects pfile->buffer to be the new one.  */
2075   pfile->buffer = buffer->prev;
2076
2077   free (buffer->notes);
2078
2079   /* Free the buffer object now; we may want to push a new buffer
2080      in _cpp_push_next_include_file.  */
2081   obstack_free (&pfile->buffer_ob, buffer);
2082
2083   if (inc)
2084     {
2085       _cpp_pop_file_buffer (pfile, inc);
2086
2087       _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2088     }
2089 }
2090
2091 /* Enter all recognized directives in the hash table.  */
2092 void
2093 _cpp_init_directives (cpp_reader *pfile)
2094 {
2095   unsigned int i;
2096   cpp_hashnode *node;
2097
2098   for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2099     {
2100       node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2101       node->is_directive = 1;
2102       node->directive_index = i;
2103     }
2104 }