OSDN Git Service

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