OSDN Git Service

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