OSDN Git Service

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