OSDN Git Service

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