OSDN Git Service

* cpplib.h (TTYPE_TABLE): Rearrange to use only two per-entry
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.c
1 /* CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4    Contributed by Per Bothner, 1994-95.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "intl.h"
28 #include "obstack.h"
29 #include "symcat.h"
30
31 /* Stack of conditionals currently in progress
32    (including both successful and failing conditionals).  */
33
34 struct if_stack
35 {
36   struct if_stack *next;
37   unsigned int lineno;          /* line number where condition started */
38   unsigned int colno;           /* and column */
39   int was_skipping;             /* value of pfile->skipping before this if */
40   const cpp_hashnode *cmacro;   /* macro name for #ifndef around entire file */
41   int type;                     /* type of last directive seen in this group */
42 };
43
44 /* Forward declarations.  */
45
46 static void validate_else       PARAMS ((cpp_reader *, const U_CHAR *));
47 static int  parse_include       PARAMS ((cpp_reader *, const U_CHAR *, int,
48                                          const U_CHAR **, unsigned int *,
49                                          int *));
50 static void push_conditional    PARAMS ((cpp_reader *, int, int,
51                                          const cpp_hashnode *));
52 static void pass_thru_directive PARAMS ((cpp_reader *));
53 static int  read_line_number    PARAMS ((cpp_reader *, int *));
54 static int  strtoul_for_line    PARAMS ((const U_CHAR *, unsigned int,
55                                          unsigned long *));
56
57 static const cpp_hashnode *
58             parse_ifdef         PARAMS ((cpp_reader *, const U_CHAR *));
59 static const cpp_hashnode *
60             detect_if_not_defined PARAMS ((cpp_reader *));
61 static cpp_hashnode *
62             get_define_node     PARAMS ((cpp_reader *));
63 static void dump_macro_name     PARAMS ((cpp_reader *, cpp_hashnode *));
64 static void unwind_if_stack     PARAMS ((cpp_reader *, cpp_buffer *));
65
66 /* Utility.  */
67 #define str_match(sym, len, str) \
68 ((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
69
70 /* This is the table of directive handlers.  It is ordered by
71    frequency of occurrence; the numbers at the end are directive
72    counts from all the source code I have lying around (egcs and libc
73    CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
74    pcmcia-cs-3.0.9).
75
76    The entries with a dash and a name after the count are extensions,
77    of which all but #warning and #include_next are deprecated.  The name
78    is where the extension appears to have come from.  */
79
80 /* #sccs is not always recognized.  */
81 #ifdef SCCS_DIRECTIVE
82 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
83 #else
84 # define SCCS_ENTRY /* nothing */
85 #endif
86
87 #define DIRECTIVE_TABLE                                                 \
88 D(define,       T_DEFINE = 0,   KANDR,     COMMENTS)       /* 270554 */ \
89 D(include,      T_INCLUDE,      KANDR,     EXPAND | INCL)  /*  52262 */ \
90 D(endif,        T_ENDIF,        KANDR,     COND)           /*  45855 */ \
91 D(ifdef,        T_IFDEF,        KANDR,     COND)           /*  22000 */ \
92 D(if,           T_IF,           KANDR,     COND | EXPAND)  /*  18162 */ \
93 D(else,         T_ELSE,         KANDR,     COND)           /*   9863 */ \
94 D(ifndef,       T_IFNDEF,       KANDR,     COND)           /*   9675 */ \
95 D(undef,        T_UNDEF,        KANDR,     0)              /*   4837 */ \
96 D(line,         T_LINE,         KANDR,     EXPAND)         /*   2465 */ \
97 D(elif,         T_ELIF,         KANDR,     COND | EXPAND)  /*    610 */ \
98 D(error,        T_ERROR,        STDC89,    0)              /*    475 */ \
99 D(pragma,       T_PRAGMA,       STDC89,    0)              /*    195 */ \
100 D(warning,      T_WARNING,      EXTENSION, 0)              /*     22 GNU   */ \
101 D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL)  /*     19 GNU   */ \
102 D(ident,        T_IDENT,        EXTENSION, 0)              /*     11 SVR4  */ \
103 D(import,       T_IMPORT,       EXTENSION, EXPAND | INCL)  /*      0 ObjC  */ \
104 D(assert,       T_ASSERT,       EXTENSION, 0)              /*      0 SVR4  */ \
105 D(unassert,     T_UNASSERT,     EXTENSION, 0)              /*      0 SVR4  */ \
106 SCCS_ENTRY                                                 /*      0 SVR2? */
107
108 /* Use the table to generate a series of prototypes, an enum for the
109    directive names, and an array of directive handlers.  */
110
111 /* The directive-processing functions are declared to return int
112    instead of void, because some old compilers have trouble with
113    pointers to functions returning void.  */
114
115 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
116 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
117 DIRECTIVE_TABLE
118 #undef D
119
120 #define D(n, tag, o, f) tag,
121 enum
122 {
123   DIRECTIVE_TABLE
124   N_DIRECTIVES
125 };
126 #undef D
127
128 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
129 #define D(name, t, origin, flags) \
130 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
131   sizeof STRINGX(name) - 1, origin, flags },
132 static const struct directive dtable[] =
133 {
134 DIRECTIVE_TABLE
135 };
136 #undef D
137 #undef DIRECTIVE_TABLE
138
139 /* Check if a token's name matches that of a known directive.  Put in
140    this file to save exporting dtable and other unneeded information.  */
141 const struct directive *
142 _cpp_check_directive (pfile, token, bol)
143      cpp_reader *pfile;
144      const cpp_token *token;
145      int bol;
146 {
147   unsigned int i;
148
149   /* If we are rescanning preprocessed input, don't obey any directives
150      other than # nnn.  */
151   if (CPP_OPTION (pfile, preprocessed))
152     return 0;
153
154   for (i = 0; i < N_DIRECTIVES; i++)
155     if (pfile->spec_nodes->dirs[i] == token->val.node)
156       {
157         /* If we are skipping a failed conditional group, all non-conditional
158            directives are ignored.  */
159         if (pfile->skipping && !(dtable[i].flags & COND))
160           return 0;
161
162         /* In -traditional mode, a directive is ignored unless its #
163            is in column 1.  */
164         if (!bol && dtable[i].origin == KANDR && CPP_WTRADITIONAL (pfile))
165           cpp_warning (pfile, "traditional C ignores #%s with the # indented",
166                        dtable[i].name);
167
168         /* Issue -pedantic warnings for extended directives.   */
169         if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
170           cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
171
172         /* -Wtraditional gives warnings about directives with inappropriate
173            indentation of #.  */
174         if (bol && dtable[i].origin != KANDR && CPP_WTRADITIONAL (pfile))
175           cpp_warning (pfile,
176                     "suggest hiding #%s from traditional C with an indented #",
177                        dtable[i].name);
178
179         return &dtable[i];
180       }
181
182   return 0;
183 }
184
185 const struct directive *
186 _cpp_check_linemarker (pfile, token, bol)
187      cpp_reader *pfile;
188      const cpp_token *token ATTRIBUTE_UNUSED;
189      int bol;
190 {
191   /* # followed by a number is equivalent to #line.  Do not recognize
192      this form in assembly language source files or skipped
193      conditional groups.  Complain about this form if we're being
194      pedantic, but not if this is regurgitated input (preprocessed or
195      fed back in by the C++ frontend).  */
196   if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
197     return 0;
198
199   if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
200       && ! CPP_OPTION (pfile, preprocessed))
201     cpp_pedwarn (pfile, "# followed by integer");
202
203   /* In -traditional mode, a directive is ignored unless its #
204      is in column 1.  */
205   if (!bol && CPP_WTRADITIONAL (pfile))
206     cpp_warning (pfile, "traditional C ignores #%s with the # indented",
207                  dtable[T_LINE].name);
208
209   return &dtable[T_LINE];
210 }  
211
212 static void
213 dump_macro_name (pfile, node)
214      cpp_reader *pfile;
215      cpp_hashnode *node;
216 {
217   CPP_PUTS (pfile, "#define ", sizeof "#define " - 1);
218   CPP_PUTS (pfile, node->name, node->length);
219 }
220
221 /* Pass the current directive through to the output file.  */
222 static void
223 pass_thru_directive (pfile)
224      cpp_reader *pfile;
225 {
226   /* XXX This output may be genuinely needed even when there is no
227      printer.  */
228   if (! pfile->printer)
229     return;
230   /* Flush first (temporary).  */
231   cpp_output_tokens (pfile, pfile->printer, pfile->token_list.line);
232   _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 1);
233 }
234
235 static cpp_hashnode *
236 get_define_node (pfile)
237      cpp_reader *pfile;
238 {
239   cpp_hashnode *node;
240   const cpp_token *token;
241
242   /* Skip any -C comments.  */
243   while ((token = _cpp_get_token (pfile))->type == CPP_COMMENT)
244     ;
245
246   if (token->type != CPP_NAME)
247     {
248       cpp_error_with_line (pfile, token->line, token->col,
249                            "macro names must be identifiers");
250       return 0;
251     }
252
253   /* That identifier is not allowed to be "defined".  See predefined
254      macro names (6.10.8.4).  */
255   node = token->val.node;
256
257   if (node == pfile->spec_nodes->n_defined)
258     {
259       cpp_error_with_line (pfile, pfile->token_list.line, token->col,
260                            "\"defined\" is not a legal macro name");
261       return 0;
262     }
263
264   /* Check for poisoned identifiers now.  */
265   if (node->type == T_POISON)
266     {
267       cpp_error (pfile, "attempt to use poisoned \"%s\"", node->name);
268       return 0;
269     }
270
271   return node;
272 }
273
274 /* Process a #define command.  */
275 static void
276 do_define (pfile)
277      cpp_reader *pfile;
278 {
279   cpp_hashnode *node;
280
281   if ((node = get_define_node (pfile)))
282     if (_cpp_create_definition (pfile, node))
283       {
284         if (CPP_OPTION (pfile, debug_output)
285             || CPP_OPTION (pfile, dump_macros) == dump_definitions)
286           _cpp_dump_definition (pfile, node);
287         else if (CPP_OPTION (pfile, dump_macros) == dump_names)
288           dump_macro_name (pfile, node);
289       }
290 }
291
292 /* Remove the definition of a symbol from the symbol table.  */
293 static void
294 do_undef (pfile)
295      cpp_reader *pfile;
296 {
297   cpp_hashnode *node = get_define_node (pfile);  
298
299   if (_cpp_get_token (pfile)->type != CPP_EOF)
300     cpp_pedwarn (pfile, "junk on line after #undef");
301
302   /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
303      is not currently defined as a macro name.  */
304   if (node && node->type != T_VOID)
305     {
306       /* If we are generating additional info for debugging (with -g) we
307          need to pass through all effective #undef commands.  */
308       if (CPP_OPTION (pfile, debug_output)
309           || CPP_OPTION (pfile, dump_macros) == dump_definitions
310           || CPP_OPTION (pfile, dump_macros) == dump_names)
311         pass_thru_directive (pfile);
312
313       if (node->type != T_MACRO)
314         cpp_warning (pfile, "undefining \"%s\"", node->name);
315
316       _cpp_free_definition (node);
317       node->type = T_VOID;
318     }
319 }
320
321
322 /* Handle #include and #import.  */
323
324 static int
325 parse_include (pfile, dir, trail, strp, lenp, abp)
326      cpp_reader *pfile;
327      const U_CHAR *dir;
328      int trail;
329      const U_CHAR **strp;
330      unsigned int *lenp;
331      int *abp;
332 {
333   const cpp_token *name = _cpp_get_token (pfile);
334
335   if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
336     {
337       if (name->type == CPP_LESS)
338         name = _cpp_glue_header_name (pfile);
339       else
340         {
341           cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
342           return 1;
343         }
344     }
345   if (name->val.str.len == 0)
346     {
347       cpp_error (pfile, "empty file name in #%s", dir);
348       return 1;
349     }
350
351   if (!trail && _cpp_get_token (pfile)->type != CPP_EOF)
352     cpp_error (pfile, "junk at end of #%s", dir);
353
354   *lenp = name->val.str.len;
355   *strp = name->val.str.text;
356   *abp = (name->type == CPP_HEADER_NAME);
357   return 0;
358 }
359
360 static void
361 do_include (pfile)
362      cpp_reader *pfile;
363 {
364   unsigned int len;
365   const U_CHAR *str;
366   int ab;
367
368   if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
369     return;
370
371   _cpp_execute_include (pfile, str, len, 0, 0, ab);
372   if (CPP_OPTION (pfile, dump_includes))
373     pass_thru_directive (pfile);
374 }
375
376 static void
377 do_import (pfile)
378      cpp_reader *pfile;
379 {
380   unsigned int len;
381   const U_CHAR *str;
382   int ab;
383
384   if (CPP_OPTION (pfile, warn_import)
385       && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
386     {
387       pfile->import_warning = 1;
388       cpp_warning (pfile,
389            "#import is obsolete, use an #ifndef wrapper in the header file");
390     }
391
392   if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
393     return;
394
395   _cpp_execute_include (pfile, str, len, 1, 0, ab);
396   if (CPP_OPTION (pfile, dump_includes))
397     pass_thru_directive (pfile);
398 }
399
400 static void
401 do_include_next (pfile)
402      cpp_reader *pfile;
403 {
404   unsigned int len;
405   const U_CHAR *str;
406   struct file_name_list *search_start = 0;
407   int ab;
408
409   if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
410     return;
411
412   /* For #include_next, skip in the search path past the dir in which
413      the current file was found.  If this is the last directory in the
414      search path, don't include anything.  If the current file was
415      specified with an absolute path, use the normal search logic.  If
416      this is the primary source file, use the normal search logic and
417      generate a warning.  */
418   if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
419     {
420       if (CPP_BUFFER (pfile)->inc->foundhere)
421         {
422           search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
423           if (!search_start)
424             return;
425         }
426     }
427   else
428     cpp_warning (pfile, "#include_next in primary source file");
429
430   _cpp_execute_include (pfile, str, len, 0, search_start, ab);
431   if (CPP_OPTION (pfile, dump_includes))
432     pass_thru_directive (pfile);
433 }
434
435 /* Subroutine of do_line.  Read next token from PFILE without adding it to
436    the output buffer.  If it is a number between 1 and 4, store it in *NUM
437    and return 1; otherwise, return 0 and complain if we aren't at the end
438    of the directive.  */
439
440 static int
441 read_line_number (pfile, num)
442      cpp_reader *pfile;
443      int *num;
444 {
445   const cpp_token *tok = _cpp_get_token (pfile);
446   enum cpp_ttype type = tok->type;
447   const U_CHAR *p = tok->val.str.text;
448   unsigned int len = tok->val.str.len;
449
450   if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
451     {
452       *num = p[0] - '0';
453       return 1;
454     }
455   else
456     {
457       if (type != CPP_EOF)
458         cpp_error (pfile, "invalid format #line");
459       return 0;
460     }
461 }
462
463 /* Another subroutine of do_line.  Convert a number in STR, of length
464    LEN, to binary; store it in NUMP, and return 0 if the number was
465    legal, 1 if not.  Temporary, hopefully.  */
466 static int
467 strtoul_for_line (str, len, nump)
468      const U_CHAR *str;
469      unsigned int len;
470      unsigned long *nump;
471 {
472   unsigned long reg = 0;
473   U_CHAR c;
474   while (len--)
475     {
476       c = *str++;
477       if (!ISDIGIT (c))
478         return 1;
479       reg *= 10;
480       reg += c - '0';
481     }
482   *nump = reg;
483   return 0;
484 }
485
486 /* Interpret #line command.
487    Note that the filename string (if any) is treated as if it were an
488    include filename.  That means no escape handling.  */
489
490 static void
491 do_line (pfile)
492      cpp_reader *pfile;
493 {
494   cpp_buffer *ip = CPP_BUFFER (pfile);
495   unsigned long new_lineno, old_lineno;
496   /* C99 raised the minimum limit on #line numbers.  */
497   unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
498   int action_number = 0;
499   enum cpp_ttype type;
500   const U_CHAR *str;
501   char *fname;
502   unsigned int len;
503   const cpp_token *tok;
504
505   tok = _cpp_get_token (pfile);
506   type = tok->type;
507   str = tok->val.str.text;
508   len = tok->val.str.len;
509
510   if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
511     {
512       cpp_error (pfile, "token after #line is not a positive integer");
513       goto done;
514     }      
515
516   if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
517     cpp_pedwarn (pfile, "line number out of range");
518
519   old_lineno = ip->lineno;
520   ip->lineno = new_lineno;
521   tok = _cpp_get_token (pfile);
522   type = tok->type;
523   str = tok->val.str.text;
524   len = tok->val.str.len;
525
526   if (type == CPP_EOF)
527     goto done;
528   else if (type != CPP_STRING)
529     {
530       cpp_error (pfile, "second token after #line is not a string");
531       ip->lineno = old_lineno;  /* malformed #line should have no effect */
532       goto done;
533     }
534
535   fname = alloca (len + 1);
536   memcpy (fname, str, len);
537   fname[len] = '\0';
538     
539   if (strcmp (fname, ip->nominal_fname))
540     {
541       if (!strcmp (fname, ip->inc->name))
542         ip->nominal_fname = ip->inc->name;
543       else
544         ip->nominal_fname = _cpp_fake_include (pfile, fname);
545     }
546
547   if (read_line_number (pfile, &action_number) == 0)
548     return;
549
550   if (CPP_PEDANTIC (pfile))
551     cpp_pedwarn (pfile, "garbage at end of #line");
552
553   /* This is somewhat questionable: change the buffer stack
554      depth so that output_line_command thinks we've stacked
555      another buffer. */
556   if (action_number == 1)
557     {
558       pfile->buffer_stack_depth++;
559       cpp_make_system_header (pfile, ip, 0);
560       read_line_number (pfile, &action_number);
561     }
562   else if (action_number == 2)
563     {
564       pfile->buffer_stack_depth--;
565       cpp_make_system_header (pfile, ip, 0);
566       read_line_number (pfile, &action_number);
567     }
568   if (action_number == 3)
569     {
570       cpp_make_system_header (pfile, ip, 1);
571       read_line_number (pfile, &action_number);
572     }
573   if (action_number == 4)
574     {
575       cpp_make_system_header (pfile, ip, 2);
576       read_line_number (pfile, &action_number);
577     }
578
579  done:
580   return;
581 }
582
583 /*
584  * Report an error detected by the program we are processing.
585  * Use the text of the line in the error message.
586  * (We use error because it prints the filename & line#.)
587  */
588
589 static void
590 do_error (pfile)
591      cpp_reader *pfile;
592 {
593   U_CHAR *text, *limit;
594
595   text = pfile->limit;
596   _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
597   limit = pfile->limit;
598   pfile->limit = text;
599   cpp_error (pfile, "%.*s", (int)(limit - text), text);
600 }
601
602 /*
603  * Report a warning detected by the program we are processing.
604  * Use the text of the line in the warning message, then continue.
605  */
606
607 static void
608 do_warning (pfile)
609      cpp_reader *pfile;
610 {
611   U_CHAR *text, *limit;
612
613   text = pfile->limit;
614   _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
615   limit = pfile->limit;
616   pfile->limit = text;
617   cpp_warning (pfile, "%.*s", (int)(limit - text), text);
618 }
619
620 /* Report program identification.  */
621
622 static void
623 do_ident (pfile)
624      cpp_reader *pfile;
625 {
626   /* Next token should be a string constant.  */
627   if (_cpp_get_token (pfile)->type == CPP_STRING)
628     /* And then a newline.  */
629     if (_cpp_get_token (pfile)->type == CPP_EOF)
630       {
631         /* Good - ship it.  */
632         pass_thru_directive (pfile);
633         return;
634       }
635
636   cpp_error (pfile, "invalid #ident");
637 }
638
639 /* Pragmata handling.  We handle some of these, and pass the rest on
640    to the front end.  C99 defines three pragmas and says that no macro
641    expansion is to be performed on them; whether or not macro
642    expansion happens for other pragmas is implementation defined.
643    This implementation never macro-expands the text after #pragma.
644
645    We currently do not support the _Pragma operator.  Support for that
646    has to be coordinated with the front end.  Proposed implementation:
647    both #pragma blah blah and _Pragma("blah blah") become
648    __builtin_pragma(blah blah) and we teach the parser about that.  */
649
650 /* Sub-handlers for the pragmas needing treatment here.
651    They return 1 if the token buffer is to be popped, 0 if not. */
652 struct pragma_entry
653 {
654   const char *name;
655   int (*handler) PARAMS ((cpp_reader *));
656 };
657
658 static int pragma_dispatch             
659     PARAMS ((cpp_reader *, const struct pragma_entry *, const cpp_hashnode *));
660 static int do_pragma_once               PARAMS ((cpp_reader *));
661 static int do_pragma_implementation     PARAMS ((cpp_reader *));
662 static int do_pragma_poison             PARAMS ((cpp_reader *));
663 static int do_pragma_system_header      PARAMS ((cpp_reader *));
664 static int do_pragma_gcc                PARAMS ((cpp_reader *));
665 static int do_pragma_dependency         PARAMS ((cpp_reader *));
666
667 static const struct pragma_entry top_pragmas[] =
668 {
669   {"once", do_pragma_once},
670   {"implementation", do_pragma_implementation},
671   {"poison", do_pragma_poison},
672   {"GCC", do_pragma_gcc},
673   {NULL, NULL}
674 };
675
676 static const struct pragma_entry gcc_pragmas[] =
677 {
678   {"implementation", do_pragma_implementation},
679   {"poison", do_pragma_poison},
680   {"system_header", do_pragma_system_header},
681   {"dependency", do_pragma_dependency},
682   {NULL, NULL}
683 };
684
685 static int pragma_dispatch (pfile, table, node)
686      cpp_reader *pfile;
687      const struct pragma_entry *table;
688      const cpp_hashnode *node;
689 {
690   const U_CHAR *p = node->name;
691   size_t len = node->length;
692   
693   for (; table->name; table++)
694     if (strlen (table->name) == len && !memcmp (p, table->name, len))
695       return (*table->handler) (pfile);
696   return 0;
697 }
698
699 static void
700 do_pragma (pfile)
701      cpp_reader *pfile;
702 {
703   const cpp_token *tok;
704   int pop;
705
706   tok = _cpp_get_token (pfile);
707   if (tok->type == CPP_EOF)
708     return;
709   else if (tok->type != CPP_NAME)
710     {
711       cpp_error (pfile, "malformed #pragma directive");
712       return;
713     }
714
715   pop = pragma_dispatch (pfile, top_pragmas, tok->val.node);
716   if (!pop)
717     pass_thru_directive (pfile);
718 }
719
720 static int
721 do_pragma_gcc (pfile)
722      cpp_reader *pfile;
723 {
724   const cpp_token *tok;
725
726   tok = _cpp_get_token (pfile);
727   if (tok->type == CPP_EOF)
728     return 1;
729   else if (tok->type != CPP_NAME)
730     return 0;
731   
732   return pragma_dispatch (pfile, gcc_pragmas, tok->val.node);
733 }
734
735 static int
736 do_pragma_once (pfile)
737      cpp_reader *pfile;
738 {
739   cpp_buffer *ip = CPP_BUFFER (pfile);
740
741   /* Allow #pragma once in system headers, since that's not the user's
742      fault.  */
743   if (!CPP_IN_SYSTEM_HEADER (pfile))
744     cpp_warning (pfile, "#pragma once is obsolete");
745       
746   if (CPP_PREV_BUFFER (ip) == NULL)
747     cpp_warning (pfile, "#pragma once outside include file");
748   else
749     ip->inc->cmacro = NEVER_REREAD;
750
751   return 1;
752 }
753
754 static int
755 do_pragma_implementation (pfile)
756      cpp_reader *pfile;
757 {
758   /* Be quiet about `#pragma implementation' for a file only if it hasn't
759      been included yet.  */
760   const cpp_token *tok = _cpp_get_token (pfile);
761   char *copy;
762
763   if (tok->type == CPP_EOF)
764     return 0;
765   else if (tok->type != CPP_STRING
766            || _cpp_get_token (pfile)->type != CPP_EOF)
767     {
768       cpp_error (pfile, "malformed #pragma implementation");
769       return 1;
770     }
771
772   /* Make a NUL-terminated copy of the string.  */
773   copy = alloca (tok->val.str.len + 1);
774   memcpy (copy, tok->val.str.text, tok->val.str.len);
775   copy[tok->val.str.len] = '\0';
776   
777   if (cpp_included (pfile, copy))
778     cpp_warning (pfile,
779          "#pragma implementation for %s appears after file is included",
780                  copy);
781   return 0;
782 }
783
784 static int
785 do_pragma_poison (pfile)
786      cpp_reader *pfile;
787 {
788   /* Poison these symbols so that all subsequent usage produces an
789      error message.  */
790   const cpp_token *tok;
791   cpp_hashnode *hp;
792   int writeit;
793
794   /* As a rule, don't include #pragma poison commands in output,  
795      unless the user asks for them.  */
796   writeit = (CPP_OPTION (pfile, debug_output)
797              || CPP_OPTION (pfile, dump_macros) == dump_definitions
798              || CPP_OPTION (pfile, dump_macros) == dump_names);
799
800   for (;;)
801     {
802       tok = _cpp_get_token (pfile);
803       if (tok->type == CPP_EOF)
804         break;
805       if (tok->type != CPP_NAME)
806         {
807           cpp_error (pfile, "invalid #pragma poison directive");
808           return 1;
809         }
810
811       hp = tok->val.node;
812       if (hp->type == T_POISON)
813         ;  /* It is allowed to poison the same identifier twice.  */
814       else
815         {
816           if (hp->type != T_VOID)
817             cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
818           _cpp_free_definition (hp);
819           hp->type = T_POISON;
820         }
821     }
822   return !writeit;
823 }
824
825 /* Mark the current header as a system header.  This will suppress
826    some categories of warnings (notably those from -pedantic).  It is
827    intended for use in system libraries that cannot be implemented in
828    conforming C, but cannot be certain that their headers appear in a
829    system include directory.  To prevent abuse, it is rejected in the
830    primary source file.  */
831 static int
832 do_pragma_system_header (pfile)
833      cpp_reader *pfile;
834 {
835   cpp_buffer *ip = CPP_BUFFER (pfile);
836   if (CPP_PREV_BUFFER (ip) == NULL)
837     cpp_warning (pfile, "#pragma system_header outside include file");
838   else
839     cpp_make_system_header (pfile, ip, 1);
840
841   return 1;
842 }
843
844 /* Check the modified date of the current include file against a specified
845    file. Issue a diagnostic, if the specified file is newer. We use this to
846    determine if a fixed header should be refixed.  */
847 static int
848 do_pragma_dependency (pfile)
849      cpp_reader *pfile;
850 {
851   const U_CHAR *name;
852   unsigned int len;
853   int ordering, ab;
854   char left, right;
855  
856   if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
857     return 1;
858
859   left = ab ? '<' : '"';
860   right = ab ? '>' : '"';
861  
862   ordering = _cpp_compare_file_date (pfile, name, len, ab);
863   if (ordering < 0)
864     cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
865   else if (ordering > 0)
866     {
867       const cpp_token *msg = _cpp_get_token (pfile);
868       
869       cpp_warning (pfile, "current file is older than %c%.*s%c",
870                    left, (int)len, name, right);
871       if (msg->type != CPP_EOF)
872         {
873           U_CHAR *text, *limit;
874
875           text = pfile->limit;
876           _cpp_dump_list (pfile, &pfile->token_list, msg, 0);
877           limit = pfile->limit;
878           pfile->limit = text;
879           while (*text == ' ')
880             /* There must be something non-whitespace after. */
881             text++; 
882           cpp_warning (pfile, "%.*s", (int)(limit - text), text);
883         }
884     }
885   return 1;
886 }
887
888 /* Just ignore #sccs, on systems where we define it at all.  */
889 #ifdef SCCS_DIRECTIVE
890 static void
891 do_sccs (pfile)
892      cpp_reader *pfile ATTRIBUTE_UNUSED;
893 {
894 }
895 #endif
896
897 /* We've found an `#if' directive.  If the only thing before it in
898    this file is white space, and if it is of the form
899    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
900    for inclusion of this file.  (See redundant_include_p in cppfiles.c
901    for an explanation of controlling macros.)  If so, return the
902    hash node for SYMBOL.  Otherwise, return NULL.  */
903
904 static const cpp_hashnode *
905 detect_if_not_defined (pfile)
906      cpp_reader *pfile;
907 {
908   const cpp_token *token;
909   cpp_hashnode *cmacro = 0;
910
911   /* We are guaranteed that tokens are consecutive and end in CPP_EOF.  */
912   token = pfile->first_directive_token + 2;
913
914   if (token->type != CPP_NOT)
915     return 0;
916
917   token++;
918   if (token->type != CPP_NAME
919       || token->val.node != pfile->spec_nodes->n_defined)
920     return 0;
921
922   token++;
923   if (token->type == CPP_OPEN_PAREN)
924     token++;
925
926   if (token->type != CPP_NAME)
927     return 0;
928
929   cmacro = token->val.node;
930
931   if (token[-1].type == CPP_OPEN_PAREN)
932     {
933       token++;
934       if (token->type != CPP_CLOSE_PAREN)
935         return 0;
936     }
937
938   token++;
939   if (token->type != CPP_EOF)
940     return 0;
941
942   return cmacro;
943 }
944
945 /* Parse an #ifdef or #ifndef directive.  Returns the hash node of the
946    macro being tested, and issues various error messages.  */
947
948 static const cpp_hashnode *
949 parse_ifdef (pfile, name)
950      cpp_reader *pfile;
951      const U_CHAR *name;
952 {
953   enum cpp_ttype type;
954   const cpp_hashnode *node = 0;
955
956   const cpp_token *token = _cpp_get_token (pfile);
957   type = token->type;
958
959   if (type == CPP_EOF)
960     cpp_pedwarn (pfile, "#%s with no argument", name);
961   else if (type != CPP_NAME)
962     cpp_pedwarn (pfile, "#%s with invalid argument", name);
963   else if (_cpp_get_token (pfile)->type != CPP_EOF)
964     cpp_pedwarn (pfile, "garbage at end of #%s", name);
965
966   if (type == CPP_NAME)
967     node = token->val.node;
968   if (node && node->type == T_POISON)
969     {
970       cpp_error (pfile, "attempt to use poisoned identifier \"%s\"",
971                  node->name);
972       node = 0;
973     }
974
975   return node;
976 }
977
978 /* #ifdef is dead simple.  */
979
980 static void
981 do_ifdef (pfile)
982      cpp_reader *pfile;
983 {
984   const cpp_hashnode *node = 0;
985
986   if (! pfile->skipping)
987     node = parse_ifdef (pfile, dtable[T_IFDEF].name);
988
989   push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0);
990 }
991
992 /* #ifndef is a tad more complex, because we need to check for a
993    no-reinclusion wrapper.  */
994
995 static void
996 do_ifndef (pfile)
997      cpp_reader *pfile;
998 {
999   int start_of_file = 0;
1000   const cpp_hashnode *node = 0;
1001
1002   if (! pfile->skipping)
1003     {
1004       start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
1005       node = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1006     }
1007
1008   push_conditional (pfile, node && node->type != T_VOID,
1009                     T_IFNDEF, start_of_file ? node : 0);
1010 }
1011
1012 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1013    Also, check for a reinclude preventer of the form #if !defined (MACRO).  */
1014
1015 static void
1016 do_if (pfile)
1017      cpp_reader *pfile;
1018 {
1019   const cpp_hashnode *cmacro = 0;
1020   int value = 0;
1021
1022   if (! pfile->skipping)
1023     {
1024       if (pfile->token_list.flags & BEG_OF_FILE)
1025         cmacro = detect_if_not_defined (pfile);
1026       value = _cpp_parse_expr (pfile);
1027     }
1028   push_conditional (pfile, value == 0, T_IF, cmacro);
1029 }
1030
1031 /* #else flips pfile->skipping and continues without changing
1032    if_stack; this is so that the error message for missing #endif's
1033    etc. will point to the original #if.  */
1034
1035 static void
1036 do_else (pfile)
1037      cpp_reader *pfile;
1038 {
1039   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1040   validate_else (pfile, dtable[T_ELSE].name);
1041
1042   if (ifs == NULL)
1043     {
1044       cpp_error (pfile, "#else without #if");
1045       return;
1046     }
1047   if (ifs->type == T_ELSE)
1048     {
1049       cpp_error (pfile, "#else after #else");
1050       cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1051                            "the conditional began here");
1052     }
1053
1054   /* #ifndef can't have its special treatment for containing the whole file
1055      if it has a #else clause.  */
1056   ifs->cmacro = 0;
1057   ifs->type = T_ELSE;
1058   if (! ifs->was_skipping)
1059     {
1060       /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1061          succeeded, so we mustn't do the else block.  */
1062       if (pfile->skipping < 2)
1063         pfile->skipping = ! pfile->skipping;
1064     }
1065 }
1066
1067 /*
1068  * handle a #elif directive by not changing if_stack either.
1069  * see the comment above do_else.
1070  */
1071
1072 static void
1073 do_elif (pfile)
1074      cpp_reader *pfile;
1075 {
1076   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1077
1078   if (ifs == NULL)
1079     {
1080       cpp_error (pfile, "#elif without #if");
1081       return;
1082     }
1083   if (ifs->type == T_ELSE)
1084     {
1085       cpp_error (pfile, "#elif after #else");
1086       cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1087                            "the conditional began here");
1088     }
1089
1090   ifs->type = T_ELIF;
1091   if (ifs->was_skipping)
1092     return;  /* Don't evaluate a nested #if */
1093
1094   if (pfile->skipping != 1)
1095     {
1096       pfile->skipping = 2;  /* one block succeeded, so don't do any others */
1097       return;
1098     }
1099
1100   pfile->skipping = ! _cpp_parse_expr (pfile);
1101 }
1102
1103 /* #endif pops the if stack and resets pfile->skipping.  */
1104
1105 static void
1106 do_endif (pfile)
1107      cpp_reader *pfile;
1108 {
1109   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1110
1111   validate_else (pfile, dtable[T_ENDIF].name);
1112
1113   if (ifs == NULL)
1114     cpp_error (pfile, "#endif without #if");
1115   else
1116     {
1117       CPP_BUFFER (pfile)->if_stack = ifs->next;
1118       pfile->skipping = ifs->was_skipping;
1119       pfile->potential_control_macro = ifs->cmacro;
1120       obstack_free (pfile->buffer_ob, ifs);
1121     }
1122 }
1123
1124
1125 /* Push an if_stack entry and set pfile->skipping accordingly.
1126    If this is a #ifndef starting at the beginning of a file,
1127    CMACRO is the macro name tested by the #ifndef.  */
1128
1129 static void
1130 push_conditional (pfile, skip, type, cmacro)
1131      cpp_reader *pfile;
1132      int skip;
1133      int type;
1134      const cpp_hashnode *cmacro;
1135 {
1136   struct if_stack *ifs;
1137
1138   ifs = xobnew (pfile->buffer_ob, struct if_stack);
1139   ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
1140   ifs->next = CPP_BUFFER (pfile)->if_stack;
1141   ifs->cmacro = cmacro;
1142   ifs->was_skipping = pfile->skipping;
1143   ifs->type = type;
1144
1145   if (!pfile->skipping)
1146     pfile->skipping = skip;
1147
1148   CPP_BUFFER (pfile)->if_stack = ifs;
1149 }
1150
1151 /* Issue -pedantic warning for text which is not a comment following
1152    an #else or #endif.  */
1153
1154 static void
1155 validate_else (pfile, directive)
1156      cpp_reader *pfile;
1157      const U_CHAR *directive;
1158 {
1159   if (CPP_PEDANTIC (pfile) && _cpp_get_token (pfile)->type != CPP_EOF)
1160     cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1161 }
1162
1163 /* Called when we reach the end of a file.  Walk back up the
1164    conditional stack till we reach its level at entry to this file,
1165    issuing error messages.  Then force skipping off.  */
1166 static void
1167 unwind_if_stack (pfile, pbuf)
1168      cpp_reader *pfile;
1169      cpp_buffer *pbuf;
1170 {
1171   struct if_stack *ifs, *nifs;
1172
1173   for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1174     {
1175       cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
1176                            dtable[ifs->type].name);
1177       nifs = ifs->next;
1178       /* No need to free - they'll all go away with the buffer.  */
1179     }
1180   pfile->skipping = 0;
1181 }
1182
1183 /* Parses an assertion, returning a pointer to the hash node of the
1184    predicate, or 0 on error.  If an answer was supplied, it is
1185    allocated and placed in ANSWERP, otherwise it is set to 0.  We use
1186    _cpp_get_raw_token, since we cannot assume tokens are consecutive
1187    in a #if statement (we may be in a macro), and we don't want to
1188    macro expand.  */
1189 cpp_hashnode *
1190 _cpp_parse_assertion (pfile, answerp)
1191      cpp_reader *pfile;
1192      struct answer **answerp;
1193 {
1194   struct answer *answer = 0;
1195   cpp_toklist *list;
1196   U_CHAR *sym;
1197   const cpp_token *token, *predicate;
1198   const struct directive *d = pfile->token_list.directive;
1199   unsigned int len = 0;
1200
1201   predicate = _cpp_get_raw_token (pfile);
1202   if (predicate->type == CPP_EOF)
1203     {
1204       cpp_error (pfile, "assertion without predicate");
1205       return 0;
1206     }
1207   else if (predicate->type != CPP_NAME)
1208     {
1209       cpp_error (pfile, "predicate must be an identifier");
1210       return 0;
1211     }
1212
1213   token = _cpp_get_raw_token (pfile);
1214   if (token->type != CPP_OPEN_PAREN)
1215     {
1216       /* #unassert and #if are OK without predicate.  */
1217       if (d == &dtable[T_UNASSERT])
1218         {
1219           if (token->type == CPP_EOF)
1220             goto lookup_node;
1221         }
1222       else if (d != &dtable[T_ASSERT])
1223         {
1224           _cpp_push_token (pfile, token);
1225           goto lookup_node;
1226         }
1227       cpp_error (pfile, "missing '(' after predicate");
1228       return 0;
1229     }
1230
1231   /* Allocate a struct answer, and copy the answer to it.  */
1232   answer = (struct answer *) xmalloc (sizeof (struct answer));
1233   list = &answer->list;
1234   _cpp_init_toklist (list, NO_DUMMY_TOKEN);
1235
1236   for (;;)
1237     {
1238       cpp_token *dest;
1239
1240       token = _cpp_get_raw_token (pfile);
1241
1242       if (token->type == CPP_EOF)
1243         {
1244           cpp_error (pfile, "missing ')' to complete answer");
1245           goto error;
1246         }
1247       if (token->type == CPP_CLOSE_PAREN)
1248         break;
1249
1250       /* Copy the token.  */
1251       _cpp_expand_token_space (list, 1);
1252       dest = &list->tokens[list->tokens_used++];
1253       *dest = *token;
1254
1255       if (TOKEN_SPELL (token) == SPELL_STRING)
1256         {
1257           _cpp_expand_name_space (list, token->val.str.len);
1258           dest->val.str.text = list->namebuf + list->name_used;
1259           memcpy (list->namebuf + list->name_used,
1260                   token->val.str.text, token->val.str.len);
1261           list->name_used += token->val.str.len;
1262         }
1263     }
1264
1265   if (list->tokens_used == 0)
1266     {
1267       cpp_error (pfile, "predicate's answer is empty");
1268       goto error;
1269     }
1270
1271   /* Drop whitespace at start.  */
1272   list->tokens[0].flags &= ~PREV_WHITE;
1273
1274   if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1275       && token[1].type != CPP_EOF)
1276     {
1277       cpp_error (pfile, "junk at end of assertion");
1278       goto error;
1279     }
1280
1281  lookup_node:
1282   *answerp = answer;
1283   len = predicate->val.node->length;
1284   sym = alloca (len + 1);
1285
1286   /* Prefix '#' to get it out of macro namespace.  */
1287   sym[0] = '#';
1288   memcpy (sym + 1, predicate->val.node->name, len);
1289   return cpp_lookup (pfile, sym, len + 1);
1290
1291  error:
1292   FREE_ANSWER (answer);
1293   return 0;
1294 }
1295
1296 /* Returns a pointer to the pointer to the answer in the answer chain,
1297    or a pointer to NULL if the answer is not in the chain.  */
1298 struct answer **
1299 _cpp_find_answer (node, candidate)
1300      cpp_hashnode *node;
1301      const cpp_toklist *candidate;
1302 {
1303   struct answer **result;
1304
1305   for (result = &node->value.answers; *result; result = &(*result)->next)
1306     if (_cpp_equiv_toklists (&(*result)->list, candidate))
1307       break;
1308
1309   return result;
1310 }
1311
1312 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1313 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1314 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1315 static void
1316 do_assert (pfile)
1317      cpp_reader *pfile;
1318 {
1319   struct answer *new_answer;
1320   cpp_hashnode *node;
1321   
1322   node = _cpp_parse_assertion (pfile, &new_answer);
1323   if (node)
1324     {
1325       new_answer->next = 0;
1326       new_answer->list.line = pfile->token_list.line;
1327       new_answer->list.file = pfile->token_list.file;
1328
1329       if (node->type == T_ASSERTION)
1330         {
1331           if (*_cpp_find_answer (node, &new_answer->list))
1332             goto err;
1333           new_answer->next = node->value.answers;
1334         }
1335       node->type = T_ASSERTION;
1336       node->value.answers = new_answer;
1337     }
1338   return;
1339
1340  err:
1341   cpp_warning (pfile, "\"%.*s\" re-asserted",
1342                node->length - 1, node->name + 1);
1343   FREE_ANSWER (new_answer);
1344 }
1345
1346 static void
1347 do_unassert (pfile)
1348      cpp_reader *pfile;
1349 {
1350   cpp_hashnode *node;
1351   struct answer *answer, *temp, *next;
1352   
1353   node = _cpp_parse_assertion (pfile, &answer);
1354   if (node)
1355     {
1356       /* It isn't an error to #unassert something that isn't asserted.  */
1357       if (node->type == T_ASSERTION)
1358         {
1359           if (answer)
1360             {
1361               struct answer **p = _cpp_find_answer (node, &answer->list);
1362
1363               temp = *p;
1364               if (temp)
1365                 {
1366                   *p = temp->next;
1367                   FREE_ANSWER (temp);
1368                 }
1369               if (node->value.answers == 0)
1370                 node->type = T_VOID;
1371             }
1372           else
1373             {
1374               for (temp = node->value.answers; temp; temp = next)
1375                 {
1376                   next = temp->next;
1377                   FREE_ANSWER (temp);
1378                 }
1379               node->type = T_VOID;
1380             }
1381         }
1382
1383       if (answer)
1384         FREE_ANSWER (answer);
1385     }
1386 }
1387
1388 /* These are for -D, -U, -A.  */
1389
1390 /* Process the string STR as if it appeared as the body of a #define.
1391    If STR is just an identifier, define it with value 1.
1392    If STR has anything after the identifier, then it should
1393    be identifier=definition. */
1394
1395 void
1396 cpp_define (pfile, str)
1397      cpp_reader *pfile;
1398      const char *str;
1399 {
1400   char *buf, *p;
1401   size_t count;
1402
1403   p = strchr (str, '=');
1404   /* Copy the entire option so we can modify it. 
1405      Change the first "=" in the string to a space.  If there is none,
1406      tack " 1" on the end.  Then add a newline and a NUL.  */
1407   
1408   if (p)
1409     {
1410       count = strlen (str) + 2;
1411       buf = (char *) alloca (count);
1412       memcpy (buf, str, count - 2);
1413       buf[p - str] = ' ';
1414       buf[count - 2] = '\n';
1415       buf[count - 1] = '\0';
1416     }
1417   else
1418     {
1419       count = strlen (str) + 4;
1420       buf = (char *) alloca (count);
1421       memcpy (buf, str, count - 4);
1422       strcpy (&buf[count-4], " 1\n");
1423     }
1424
1425   _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1);
1426 }
1427
1428 /* Process MACRO as if it appeared as the body of an #undef.  */
1429 void
1430 cpp_undef (pfile, macro)
1431      cpp_reader *pfile;
1432      const char *macro;
1433 {
1434   _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro));
1435 }
1436
1437 /* Process the string STR as if it appeared as the body of a #assert. */
1438 void
1439 cpp_assert (pfile, str)
1440      cpp_reader *pfile;
1441      const char *str;
1442 {
1443   _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str));
1444 }
1445
1446 /* Process STR as if it appeared as the body of an #unassert. */
1447 void
1448 cpp_unassert (pfile, str)
1449      cpp_reader *pfile;
1450      const char *str;
1451 {
1452   _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str));
1453 }  
1454
1455 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
1456 int
1457 cpp_defined (pfile, id, len)
1458      cpp_reader *pfile;
1459      const U_CHAR *id;
1460      int len;
1461 {
1462   cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1463   if (hp->type == T_POISON)
1464     {
1465       cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
1466       return 0;
1467     }
1468   return (hp->type != T_VOID);
1469 }
1470
1471 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1472    If BUFFER != NULL, then use the LENGTH characters in BUFFER
1473    as the new input buffer.
1474    Return the new buffer, or NULL on failure.  */
1475
1476 cpp_buffer *
1477 cpp_push_buffer (pfile, buffer, length)
1478      cpp_reader *pfile;
1479      const U_CHAR *buffer;
1480      long length;
1481 {
1482   cpp_buffer *buf = CPP_BUFFER (pfile);
1483   cpp_buffer *new;
1484   if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1485     {
1486       cpp_fatal (pfile, "#include nested too deep");
1487       return NULL;
1488     }
1489   if (pfile->cur_context > 0)
1490     {
1491       cpp_ice (pfile, "buffer pushed with contexts stacked");
1492       _cpp_skip_rest_of_line (pfile);
1493     }
1494
1495   new = xobnew (pfile->buffer_ob, cpp_buffer);
1496   memset (new, 0, sizeof (cpp_buffer));
1497
1498   new->line_base = new->buf = new->cur = buffer;
1499   new->rlimit = buffer + length;
1500   new->prev = buf;
1501
1502   CPP_BUFFER (pfile) = new;
1503   return new;
1504 }
1505
1506 cpp_buffer *
1507 cpp_pop_buffer (pfile)
1508      cpp_reader *pfile;
1509 {
1510   cpp_buffer *buf = CPP_BUFFER (pfile);
1511
1512   unwind_if_stack (pfile, buf);
1513   if (buf->inc)
1514     _cpp_pop_file_buffer (pfile, buf);
1515
1516   CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1517   obstack_free (pfile->buffer_ob, buf);
1518   pfile->buffer_stack_depth--;
1519   return CPP_BUFFER (pfile);
1520 }
1521
1522 #define obstack_chunk_alloc xmalloc
1523 #define obstack_chunk_free free
1524 #define DSC(x) U x, sizeof x - 1
1525 void
1526 _cpp_init_stacks (pfile)
1527      cpp_reader *pfile;
1528 {
1529   int i;
1530   struct spec_nodes *s;
1531
1532   pfile->buffer_ob = xnew (struct obstack);
1533   obstack_init (pfile->buffer_ob);
1534
1535   /* Perhaps not the ideal place to put this.  */
1536   pfile->spec_nodes = s = xnew (struct spec_nodes);
1537   s->n_L                = cpp_lookup (pfile, DSC("L"));
1538   s->n_defined          = cpp_lookup (pfile, DSC("defined"));
1539   s->n__STRICT_ANSI__   = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
1540   s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
1541   s->n__VA_ARGS__       = cpp_lookup (pfile, DSC("__VA_ARGS__"));
1542   for (i = 0; i < N_DIRECTIVES; i++)
1543     s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1544 }
1545
1546 void
1547 _cpp_cleanup_stacks (pfile)
1548      cpp_reader *pfile;
1549 {
1550   obstack_free (pfile->buffer_ob, 0);
1551   free (pfile->buffer_ob);
1552 }