OSDN Git Service

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