OSDN Git Service

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