OSDN Git Service

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