OSDN Git Service

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