OSDN Git Service

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