OSDN Git Service

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