OSDN Git Service

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