OSDN Git Service

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