OSDN Git Service

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