OSDN Git Service

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