OSDN Git Service

* gcc.c-torture/execute/20000412-1.c: Reduce some more.
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.c
1 /* CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4    Contributed by Per Bothner, 1994-95.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "hashtab.h"
28 #include "intl.h"
29 #include "symcat.h"
30
31 /* `struct directive' defines one #-directive, including how to handle it.  */
32
33 struct directive
34 {
35   int (*func)                   /* Function to handle directive */
36     PARAMS ((cpp_reader *));
37   const char *name;             /* Name of directive */
38   unsigned short length;        /* Length of name */
39   unsigned short origin;        /* Origin of this directive */
40 };
41
42 /* Stack of conditionals currently in progress
43    (including both successful and failing conditionals).  */
44
45 struct if_stack
46 {
47   struct if_stack *next;
48   int lineno;                   /* line number where condition started */
49   int if_succeeded;             /* truth of last condition in this group */
50   const U_CHAR *control_macro;  /* macro name for #ifndef around entire file */
51   int type;                     /* type of last directive seen in this group */
52 };
53 typedef struct if_stack IF_STACK;
54
55 /* Forward declarations.  */
56
57 static void validate_else               PARAMS ((cpp_reader *, const char *));
58 static int parse_ifdef                  PARAMS ((cpp_reader *, const char *));
59 static unsigned int parse_include       PARAMS ((cpp_reader *, const char *));
60 static int conditional_skip             PARAMS ((cpp_reader *, int, int,
61                                                  U_CHAR *));
62 static int skip_if_group                PARAMS ((cpp_reader *));
63 static void pass_thru_directive         PARAMS ((const U_CHAR *, size_t,
64                                                  cpp_reader *, int));
65 static int read_line_number             PARAMS ((cpp_reader *, int *));
66 static U_CHAR *detect_if_not_defined    PARAMS ((cpp_reader *));
67 static int consider_directive_while_skipping
68                                         PARAMS ((cpp_reader *, IF_STACK *));
69 static int get_macro_name               PARAMS ((cpp_reader *));
70
71 /* Values for the "origin" field of the table below.  KANDR and COND
72    directives come from traditional (K&R) C.  The difference is, if we
73    care about it while skipping a failed conditional block, its origin
74    is COND.  STDC89 directives come from the 1989 C standard.
75    EXTENSION directives are extensions, with origins noted below.  */
76 enum { KANDR = 0, COND, STDC89, EXTENSION };
77
78 #define TRAD_DIRECT_P(x) ((x) == KANDR || (x) == COND)
79
80 /* This is the table of directive handlers.  It is ordered by
81    frequency of occurrence; the numbers at the end are directive
82    counts from all the source code I have lying around (egcs and libc
83    CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
84    pcmcia-cs-3.0.9).
85
86    The entries with a dash and a name after the count are extensions,
87    of which all but #warning and #include_next are deprecated.  The name
88    is where the extension appears to have come from.  */
89
90 /* #sccs is not always recognized.  */
91 #ifdef SCCS_DIRECTIVE
92 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION)          /*     0 - SVR2? */
93 #else
94 # define SCCS_ENTRY /* nothing */
95 #endif
96
97 #define DIRECTIVE_TABLE                                                 \
98 D(define,       T_DEFINE = 0,   KANDR)          /* 270554 */            \
99 D(include,      T_INCLUDE,      KANDR)          /*  52262 */            \
100 D(endif,        T_ENDIF,        COND)           /*  45855 */            \
101 D(ifdef,        T_IFDEF,        COND)           /*  22000 */            \
102 D(if,           T_IF,           COND)           /*  18162 */            \
103 D(else,         T_ELSE,         COND)           /*   9863 */            \
104 D(ifndef,       T_IFNDEF,       COND)           /*   9675 */            \
105 D(undef,        T_UNDEF,        KANDR)          /*   4837 */            \
106 D(line,         T_LINE,         KANDR)          /*   2465 */            \
107 D(elif,         T_ELIF,         COND)           /*    610 */            \
108 D(error,        T_ERROR,        STDC89)         /*    475 */            \
109 D(pragma,       T_PRAGMA,       STDC89)         /*    195 */            \
110 D(warning,      T_WARNING,      EXTENSION)      /*     22 - GNU   */    \
111 D(include_next, T_INCLUDE_NEXT, EXTENSION)      /*     19 - GNU   */    \
112 D(ident,        T_IDENT,        EXTENSION)      /*     11 - SVR4  */    \
113 D(import,       T_IMPORT,       EXTENSION)      /*      0 - ObjC  */    \
114 D(assert,       T_ASSERT,       EXTENSION)      /*      0 - SVR4  */    \
115 D(unassert,     T_UNASSERT,     EXTENSION)      /*      0 - SVR4  */    \
116 SCCS_ENTRY
117
118 /* Use the table to generate a series of prototypes, an enum for the
119    directive names, and an array of directive handlers.  */
120
121 /* The directive-processing functions are declared to return int
122    instead of void, because some old compilers have trouble with
123    pointers to functions returning void.  */
124
125 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
126 #define D(name, t, o) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
127 DIRECTIVE_TABLE
128 #undef D
129
130 #define D(n, tag, o) tag,
131 enum
132 {
133   DIRECTIVE_TABLE
134   N_DIRECTIVES
135 };
136 #undef D
137
138 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
139 #define D(name, t, origin) \
140 { CONCAT2(do_,name), STRINGX(name), sizeof STRINGX(name) - 1, origin },
141 static const struct directive dtable[] =
142 {
143 DIRECTIVE_TABLE
144 };
145 #undef D
146 #undef DIRECTIVE_TABLE
147
148 /* Handle a possible # directive.
149    '#' has already been read.  */
150
151 int
152 _cpp_handle_directive (pfile)
153      cpp_reader *pfile;
154 {
155   int i;
156   int hash_at_bol;
157   unsigned int len;
158   U_CHAR *ident;
159   long old_written = CPP_WRITTEN (pfile);
160   enum cpp_ttype tok;
161
162   if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
163     {
164       cpp_ice (pfile, "handle_directive called on macro buffer");
165       return 0;
166     }
167
168   /* -traditional directives are recognized only with the # in column 1.  */
169   hash_at_bol = CPP_IN_COLUMN_1 (pfile);
170
171   /* Scan the next token, then pretend we didn't.  */
172   CPP_SET_MARK (pfile);
173   pfile->no_macro_expand++;
174   tok = _cpp_get_directive_token (pfile);
175   pfile->no_macro_expand--;
176
177   ident = pfile->token_buffer + old_written;
178   len = CPP_PWRITTEN (pfile) - ident;
179   CPP_SET_WRITTEN (pfile, old_written);
180   CPP_GOTO_MARK (pfile);
181
182   /* # followed by a number is equivalent to #line.  Do not recognize
183      this form in assembly language source files.  Complain about this
184      form if we're being pedantic, but not if this is regurgitated
185      input (preprocessed or fed back in by the C++ frontend).  */
186   if (tok == CPP_NUMBER)
187     {
188       if (CPP_OPTION (pfile, lang_asm))
189         return 0;
190
191       if (CPP_PEDANTIC (pfile)
192           && ! CPP_OPTION (pfile, preprocessed)
193           && ! CPP_BUFFER (pfile)->manual_pop)
194         cpp_pedwarn (pfile, "# followed by integer");
195       do_line (pfile);
196       return 1;
197     }
198
199   /* If we are rescanning preprocessed input, don't obey any directives
200      other than # nnn.  */
201   else if (CPP_OPTION (pfile, preprocessed))
202     return 0;
203
204   /* A line of just # becomes blank.  */
205   else if (tok == CPP_VSPACE)
206     return 1;
207
208   /* A NAME token might in fact be a directive!  */
209   else if (tok == CPP_NAME)
210     {
211       for (i = 0; i < N_DIRECTIVES; i++)
212         {
213           if (dtable[i].length == len
214               && !strncmp (dtable[i].name, ident, len)) 
215             goto real_directive;
216         }
217       /* Don't complain about invalid directives in assembly source,
218          we don't know where the comments are, and # may introduce
219          assembler pseudo-ops.  */
220       if (!CPP_OPTION (pfile, lang_asm))
221         cpp_error (pfile, "invalid preprocessing directive #%s", ident);
222       return 0;
223     }
224   /* And anything else means the # wasn't a directive marker.   */
225   else
226     return 0;
227
228  real_directive:
229
230   /* In -traditional mode, a directive is ignored unless its # is in
231      column 1.  */
232   if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
233     {
234       if (CPP_WTRADITIONAL (pfile))
235         cpp_warning (pfile, "ignoring #%s because of its indented #",
236                      dtable[i].name);
237       return 0;
238     }
239
240   /* no_directives is set when we are parsing macro arguments.  Directives
241      in macro arguments are undefined behavior (C99 6.10.3.11); this
242      implementation chooses to make them hard errors.  */
243   if (pfile->no_directives)
244     {
245       cpp_error (pfile, "#%s may not be used inside a macro argument",
246                  dtable[i].name);
247       _cpp_skip_rest_of_line (pfile);
248       return 1;
249     }
250
251   /* Issue -pedantic warnings for extended directives.   */
252   if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
253     cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
254
255   /* -Wtraditional gives warnings about directives with inappropriate
256      indentation of #.  */
257   if (CPP_WTRADITIONAL (pfile))
258     {
259       if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].origin))
260         cpp_warning (pfile, "traditional C ignores #%s with the # indented",
261                      dtable[i].name);
262       else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].origin))
263         cpp_warning (pfile,
264                 "suggest hiding #%s from traditional C with an indented #",
265                      dtable[i].name);
266     }
267
268   /* Unfortunately, it's necessary to scan the directive name again,
269      now we know we're going to consume it.  FIXME.  */
270
271   pfile->no_macro_expand++;
272   _cpp_get_directive_token (pfile);
273   pfile->no_macro_expand--;
274   CPP_SET_WRITTEN (pfile, old_written);
275
276   /* Some directives (e.g. #if) may return a request to execute
277      another directive handler immediately.  No directive ever
278      requests that #define be executed immediately, so it is safe for
279      the loop to terminate when some function returns 0 (== T_DEFINE).  */
280   while ((i = dtable[i].func (pfile)));
281   return 1;
282 }
283
284 /* Pass a directive through to the output file.
285    BUF points to the contents of the directive, as a contiguous string.
286    LEN is the length of the string pointed to by BUF.
287    KEYWORD is the keyword-table entry for the directive.  */
288
289 static void
290 pass_thru_directive (buf, len, pfile, keyword)
291      const U_CHAR *buf;
292      size_t len;
293      cpp_reader *pfile;
294      int keyword;
295 {
296   const struct directive *kt = &dtable[keyword];
297   register unsigned klen = kt->length;
298
299   CPP_RESERVE (pfile, 1 + klen + len);
300   CPP_PUTC_Q (pfile, '#');
301   CPP_PUTS_Q (pfile, kt->name, klen);
302   if (len != 0 && buf[0] != ' ')
303     CPP_PUTC_Q (pfile, ' ');
304   CPP_PUTS_Q (pfile, buf, len);
305 }
306
307 /* Subroutine of do_define: determine the name of the macro to be
308    defined.  */
309
310 static int
311 get_macro_name (pfile)
312      cpp_reader *pfile;
313 {
314   long here, len;
315
316   here = CPP_WRITTEN (pfile);
317   if (_cpp_get_directive_token (pfile) != CPP_NAME)
318     {
319       cpp_error (pfile, "`#define' must be followed by an identifier");
320       goto invalid;
321     }
322
323   len = CPP_WRITTEN (pfile) - here;
324   if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
325     {
326       cpp_error (pfile, "`defined' is not a legal macro name");
327       goto invalid;
328     }
329
330   return len;
331
332  invalid:
333   _cpp_skip_rest_of_line (pfile);
334   return 0;
335 }
336
337 /* Process a #define command.  */
338
339 static int
340 do_define (pfile)
341      cpp_reader *pfile;
342 {
343   HASHNODE **slot;
344   DEFINITION *def = 0;
345   long here;
346   unsigned long hash;
347   int len;
348   int funlike = 0, empty = 0;
349   U_CHAR *sym;
350   enum cpp_ttype token;
351
352   pfile->no_macro_expand++;
353   pfile->parsing_define_directive++;
354   CPP_OPTION (pfile, discard_comments)++;
355   CPP_OPTION (pfile, no_line_commands)++;
356
357   here = CPP_WRITTEN (pfile);
358   len = get_macro_name (pfile);
359   if (len == 0)
360     goto out;
361
362   /* Copy out the name so we can pop the token buffer.  */
363   len = CPP_WRITTEN (pfile) - here;
364   sym = (U_CHAR *) alloca (len + 1);
365   memcpy (sym, pfile->token_buffer + here, len);
366   sym[len] = '\0';
367
368   /* If the next character, with no intervening whitespace, is '(',
369      then this is a function-like macro.
370      XXX Layering violation.  */
371   CPP_SET_MARK (pfile);
372   token = _cpp_get_directive_token (pfile);
373   if (token == CPP_VSPACE)
374     empty = 0;  /* Empty definition of object like macro.  */
375   else if (token == CPP_LPAREN && ADJACENT_TO_MARK (pfile))
376     funlike = 1;
377   else if (ADJACENT_TO_MARK (pfile))
378     /* If this is an object-like macro, C99 requires white space after
379        the name.  */
380     cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
381   CPP_GOTO_MARK (pfile);
382   CPP_SET_WRITTEN (pfile, here);
383
384   if (! empty)
385     {
386       def = _cpp_create_definition (pfile, funlike);
387       if (def == 0)
388         goto out;
389     }
390
391   slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
392   if (*slot)
393     {
394       int ok;
395       HASHNODE *hp = *slot;
396
397       /* Redefining a macro is ok if the definitions are the same.  */
398       if (hp->type == T_MACRO)
399         ok = ! empty && ! _cpp_compare_defs (pfile, def, hp->value.defn);
400       else if (hp->type == T_EMPTY)
401         ok = empty;
402       /* Redefining a constant is ok with -D.  */
403       else if (hp->type == T_CONST || hp->type == T_STDC)
404         ok = ! pfile->done_initializing;
405       /* Otherwise it's not ok.  */
406       else
407         ok = 0;
408       /* Print the warning or error if it's not ok.  */
409       if (! ok)
410         {
411           if (hp->type == T_POISON)
412             cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
413           else
414             cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
415           if (hp->type == T_MACRO && pfile->done_initializing)
416             {
417               DEFINITION *d = hp->value.defn;
418               cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
419                         "this is the location of the previous definition");
420             }
421         }
422       if (hp->type != T_POISON)
423         {
424           /* Replace the old definition.  */
425           if (hp->type == T_MACRO)
426             _cpp_free_definition (hp->value.defn);
427           if (empty)
428             {
429               hp->type = T_EMPTY;
430               hp->value.defn = 0;
431             }
432           else
433             {
434               hp->type = T_MACRO;
435               hp->value.defn = def;
436             }
437         }
438     }
439   else
440     {
441       HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
442       hp->value.defn = def;
443       *slot = hp;
444     }
445
446   if (CPP_OPTION (pfile, debug_output)
447       || CPP_OPTION (pfile, dump_macros) == dump_definitions)
448     _cpp_dump_definition (pfile, sym, len, def);
449   else if (CPP_OPTION (pfile, dump_macros) == dump_names)
450     pass_thru_directive (sym, len, pfile, T_DEFINE);
451
452  out:
453   pfile->no_macro_expand--;
454   pfile->parsing_define_directive--;
455   CPP_OPTION (pfile, discard_comments)--;
456   CPP_OPTION (pfile, no_line_commands)--;
457   return 0;
458 }
459
460 /*
461  * write out a #line command, for instance, after an #include file.
462  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
463  */
464
465 void
466 _cpp_output_line_command (pfile, file_change)
467      cpp_reader *pfile;
468      enum file_change_code file_change;
469 {
470   unsigned int line;
471   cpp_buffer *ip;
472
473   if (CPP_OPTION (pfile, no_line_commands)
474       || CPP_OPTION (pfile, no_output))
475     return;
476
477   ip = cpp_file_buffer (pfile);
478   line = ip->lineno;
479
480   /* If the current file has not changed, we omit the #line if it would
481      appear to be a no-op, and we output a few newlines instead
482      if we want to increase the line number by a small amount.
483      We cannot do this if pfile->lineno is zero, because that means we
484      haven't output any line commands yet.  (The very first line command
485      output is a `same_file' command.)  */
486   if (file_change == same_file && pfile->lineno != 0)
487     {
488       if (line == pfile->lineno)
489         return;
490
491       /* If the inherited line number is a little too small,
492          output some newlines instead of a #line command.  */
493       if (line > pfile->lineno && line < pfile->lineno + 8)
494         {
495           CPP_RESERVE (pfile, 20);
496           while (line > pfile->lineno)
497             {
498               CPP_PUTC_Q (pfile, '\n');
499               pfile->lineno++;
500             }
501           return;
502         }
503     }
504
505   CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
506   CPP_PUTS_Q (pfile, "# ", 2);
507
508   sprintf ((char *) CPP_PWRITTEN (pfile), "%u ", line);
509   CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
510
511   _cpp_quote_string (pfile, ip->nominal_fname); 
512   if (file_change != same_file && file_change != rename_file)
513     {
514       CPP_PUTC_Q (pfile, ' ');
515       CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
516     }
517   /* Tell cc1 if following text comes from a system header file.  */
518   if (ip->system_header_p)
519     {
520       CPP_PUTC_Q (pfile, ' ');
521       CPP_PUTC_Q (pfile, '3');
522     }
523 #ifndef NO_IMPLICIT_EXTERN_C
524   /* Tell cc1plus if following text should be treated as C.  */
525   if (ip->system_header_p == 2 && CPP_OPTION (pfile, cplusplus))
526     {
527       CPP_PUTC_Q (pfile, ' ');
528       CPP_PUTC_Q (pfile, '4');
529     }
530 #endif
531   CPP_PUTC_Q (pfile, '\n');
532   pfile->lineno = line;
533 }
534
535 /* Handle #include and #import.  */
536
537 static unsigned int
538 parse_include (pfile, name)
539      cpp_reader *pfile;
540      const char *name;
541 {
542   long old_written = CPP_WRITTEN (pfile);
543   enum cpp_ttype token;
544   int len;
545
546   pfile->parsing_include_directive++;
547   token = _cpp_get_directive_token (pfile);
548   pfile->parsing_include_directive--;
549
550   len = CPP_WRITTEN (pfile) - old_written;
551
552   if (token == CPP_STRING)
553     ; /* No special treatment required.  */
554 #ifdef VMS
555   else if (token == CPP_NAME)
556     {
557       /* Support '#include xyz' like VAX-C.  It is taken as
558          '#include <xyz.h>' and generates a warning.  */
559       cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
560                    name, name);
561
562       /* Rewrite the token to <xyz.h>.  */
563       CPP_RESERVE (pfile, 4);
564       len += 4;
565       memmove (pfile->token_buffer + old_written + 1,
566                pfile->token_buffer + old_written,
567                CPP_WRITTEN (pfile) - old_written);
568       pfile->token_buffer[old_written] = '<';
569       CPP_PUTS_Q (pfile, ".h>", 2);
570     }
571 #endif
572   else
573     {
574       cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
575       CPP_SET_WRITTEN (pfile, old_written);
576       _cpp_skip_rest_of_line (pfile);
577       return 0;
578     }
579
580   CPP_NUL_TERMINATE (pfile);
581   CPP_ADJUST_WRITTEN (pfile, 1);
582
583   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
584     {
585       cpp_error (pfile, "junk at end of `#%s'", name);
586       _cpp_skip_rest_of_line (pfile);
587     }
588
589   CPP_SET_WRITTEN (pfile, old_written);
590
591   if (len == 0)
592     cpp_error (pfile, "empty file name in `#%s'", name);
593
594   return len;
595 }
596
597 static int
598 do_include (pfile)
599      cpp_reader *pfile;
600 {
601   unsigned int len;
602   char *token;
603
604   len = parse_include (pfile, dtable[T_INCLUDE].name);
605   if (len == 0)
606     return 0;
607   token = alloca (len + 1);
608   strcpy (token, CPP_PWRITTEN (pfile));
609   
610   if (CPP_OPTION (pfile, dump_includes))
611     pass_thru_directive (token, len, pfile, T_INCLUDE);
612
613   _cpp_execute_include (pfile, token, len, 0, 0);
614   return 0;
615 }
616
617 static int
618 do_import (pfile)
619      cpp_reader *pfile;
620 {
621   unsigned int len;
622   char *token;
623
624   if (CPP_OPTION (pfile, warn_import)
625       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
626     {
627       pfile->import_warning = 1;
628       cpp_warning (pfile,
629            "#import is obsolete, use an #ifndef wrapper in the header file");
630     }
631
632   len = parse_include (pfile, dtable[T_IMPORT].name);
633   if (len == 0)
634     return 0;
635   token = alloca (len + 1);
636   strcpy (token, CPP_PWRITTEN (pfile));
637   
638   if (CPP_OPTION (pfile, dump_includes))
639     pass_thru_directive (token, len, pfile, T_IMPORT);
640
641   _cpp_execute_include (pfile, token, len, 1, 0);
642   return 0;
643 }
644
645 static int
646 do_include_next (pfile)
647      cpp_reader *pfile;
648 {
649   unsigned int len;
650   char *token;
651   struct file_name_list *search_start = 0;
652
653   len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
654   if (len == 0)
655     return 0;
656   token = alloca (len + 1);
657   strcpy (token, CPP_PWRITTEN (pfile));
658   
659   if (CPP_OPTION (pfile, dump_includes))
660     pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
661
662   /* For #include_next, skip in the search path past the dir in which the
663      containing file was found.  Treat files specified using an absolute path
664      as if there are no more directories to search.  Treat the primary source
665      file like any other included source, but generate a warning.  */
666   if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
667     {
668       if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
669         search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
670     }
671   else
672     cpp_warning (pfile, "#include_next in primary source file");
673
674   _cpp_execute_include (pfile, token, len, 0, search_start);
675   return 0;
676 }
677
678 /* Subroutine of do_line.  Read next token from PFILE without adding it to
679    the output buffer.  If it is a number between 1 and 4, store it in *NUM
680    and return 1; otherwise, return 0 and complain if we aren't at the end
681    of the directive.  */
682
683 static int
684 read_line_number (pfile, num)
685      cpp_reader *pfile;
686      int *num;
687 {
688   long save_written = CPP_WRITTEN (pfile);
689   U_CHAR *p;
690   enum cpp_ttype token = _cpp_get_directive_token (pfile);
691   CPP_SET_WRITTEN (pfile, save_written);
692   p = pfile->token_buffer + save_written;
693
694   if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
695     {
696       *num = p[0] - '0';
697       return 1;
698     }
699   else
700     {
701       if (token != CPP_VSPACE && token != CPP_EOF)
702         cpp_error (pfile, "invalid format `#line' command");
703       return 0;
704     }
705 }
706
707 /* Interpret #line command.
708    Note that the filename string (if any) is treated as if it were an
709    include filename.  That means no escape handling.  */
710
711 static int
712 do_line (pfile)
713      cpp_reader *pfile;
714 {
715   cpp_buffer *ip = CPP_BUFFER (pfile);
716   int new_lineno;
717   long old_written = CPP_WRITTEN (pfile);
718   enum file_change_code file_change = same_file;
719   enum cpp_ttype token;
720   char *x;
721
722   token = _cpp_get_directive_token (pfile);
723
724   if (token != CPP_NUMBER)
725     {
726       cpp_error (pfile, "token after `#line' is not an integer");
727       goto bad_line_directive;
728     }
729
730   new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
731   if (x[0] != '\0')
732     {
733       cpp_error (pfile, "token after `#line' is not an integer");
734       goto bad_line_directive;
735     }      
736   CPP_SET_WRITTEN (pfile, old_written);
737
738   if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
739     cpp_pedwarn (pfile, "line number out of range in `#line' command");
740
741   token = _cpp_get_directive_token (pfile);
742
743   if (token == CPP_STRING)
744     {
745       U_CHAR *fname = pfile->token_buffer + old_written + 1;
746       U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
747       int action_number = 0;
748
749       file_change = rename_file;
750
751       if (read_line_number (pfile, &action_number))
752         {
753           if (CPP_PEDANTIC (pfile))
754             cpp_pedwarn (pfile, "garbage at end of `#line' command");
755
756           if (action_number == 1)
757             {
758               file_change = enter_file;
759               read_line_number (pfile, &action_number);
760             }
761           else if (action_number == 2)
762             {
763               file_change = leave_file;
764               read_line_number (pfile, &action_number);
765             }
766           if (action_number == 3)
767             {
768               ip->system_header_p = 1;
769               read_line_number (pfile, &action_number);
770             }
771           if (action_number == 4)
772             {
773               ip->system_header_p = 2;
774               read_line_number (pfile, &action_number);
775             }
776         }
777       
778       *end_name = '\0';
779       
780       if (strcmp (fname, ip->nominal_fname))
781         {
782           const char *newname, *oldname;
783           if (!strcmp (fname, ip->ihash->name))
784             newname = ip->ihash->name;
785           else if (ip->last_nominal_fname
786                    && !strcmp (fname, ip->last_nominal_fname))
787             newname = ip->last_nominal_fname;
788           else
789             newname = xstrdup (fname);
790
791           oldname = ip->nominal_fname;
792           ip->nominal_fname = newname;
793
794           if (ip->last_nominal_fname
795               && ip->last_nominal_fname != oldname
796               && ip->last_nominal_fname != newname
797               && ip->last_nominal_fname != ip->ihash->name)
798             free ((void *) ip->last_nominal_fname);
799
800           if (newname == ip->ihash->name)
801             ip->last_nominal_fname = NULL;
802           else
803             ip->last_nominal_fname = oldname;
804         } 
805     }
806   else if (token != CPP_VSPACE && token != CPP_EOF)
807     {
808       cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
809       goto bad_line_directive;
810     }
811
812   /* The Newline at the end of this line remains to be processed.
813      To put the next line at the specified line number,
814      we must store a line number now that is one less.  */
815   ip->lineno = new_lineno - 1;
816   CPP_SET_WRITTEN (pfile, old_written);
817   _cpp_output_line_command (pfile, file_change);
818   return 0;
819
820  bad_line_directive:
821   _cpp_skip_rest_of_line (pfile);
822   CPP_SET_WRITTEN (pfile, old_written);
823   return 0;
824 }
825
826 /* Remove the definition of a symbol from the symbol table.
827    According to the C standard, it is not an error to undef
828    something that has no definitions. */
829 static int
830 do_undef (pfile)
831      cpp_reader *pfile;
832 {
833   int len;
834   HASHNODE **slot;
835   U_CHAR *name;
836   long here = CPP_WRITTEN (pfile);
837   enum cpp_ttype token;
838
839   pfile->no_macro_expand++;
840   token = _cpp_get_directive_token (pfile);
841   pfile->no_macro_expand--;
842
843   if (token != CPP_NAME)
844     {
845       cpp_error (pfile, "token after #undef is not an identifier");
846       _cpp_skip_rest_of_line (pfile);
847       return 0;
848     }
849   len = CPP_WRITTEN (pfile) - here;
850
851   token = _cpp_get_directive_token (pfile);
852   if (token != CPP_VSPACE)
853   {
854       cpp_pedwarn (pfile, "junk on line after #undef");
855       _cpp_skip_rest_of_line (pfile);
856   }
857
858   name = pfile->token_buffer + here;
859   CPP_SET_WRITTEN (pfile, here);
860
861   slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
862   if (slot)
863     {
864       HASHNODE *hp = *slot;
865       if (hp->type == T_POISON)
866         cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
867       else
868         {
869           /* If we are generating additional info for debugging (with -g) we
870              need to pass through all effective #undef commands.  */
871           if (CPP_OPTION (pfile, debug_output))
872             pass_thru_directive (hp->name, len, pfile, T_UNDEF);
873
874           if (hp->type != T_MACRO)
875             cpp_warning (pfile, "undefining `%s'", hp->name);
876
877           htab_clear_slot (pfile->hashtab, (void **)slot);
878         }
879     }
880
881   return 0;
882 }
883
884 /*
885  * Report an error detected by the program we are processing.
886  * Use the text of the line in the error message.
887  * (We use error because it prints the filename & line#.)
888  */
889
890 static int
891 do_error (pfile)
892      cpp_reader *pfile;
893 {
894   const U_CHAR *text, *limit;
895
896   _cpp_skip_hspace (pfile);
897   text = CPP_BUFFER (pfile)->cur;
898   _cpp_skip_rest_of_line (pfile);
899   limit = CPP_BUFFER (pfile)->cur;
900
901   cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
902   return 0;
903 }
904
905 /*
906  * Report a warning detected by the program we are processing.
907  * Use the text of the line in the warning message, then continue.
908  */
909
910 static int
911 do_warning (pfile)
912      cpp_reader *pfile;
913 {
914   const U_CHAR *text, *limit;
915
916   _cpp_skip_hspace (pfile);
917   text = CPP_BUFFER (pfile)->cur;
918   _cpp_skip_rest_of_line (pfile);
919   limit = CPP_BUFFER (pfile)->cur;
920
921   cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
922   return 0;
923 }
924
925 /* Report program identification.  */
926
927 static int
928 do_ident (pfile)
929      cpp_reader *pfile;
930 {
931   long old_written = CPP_WRITTEN (pfile);
932
933   CPP_PUTS (pfile, "#ident ", 7);
934
935   /* Next token should be a string constant.  */
936   if (_cpp_get_directive_token (pfile) == CPP_STRING)
937     /* And then a newline.  */
938     if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
939       /* Good - ship it.  */
940       return 0;
941
942   cpp_error (pfile, "invalid #ident");
943   _cpp_skip_rest_of_line (pfile);
944   CPP_SET_WRITTEN (pfile, old_written);  /* discard directive */
945
946   return 0;
947 }
948
949 /* Pragmata handling.  We handle some of these, and pass the rest on
950    to the front end.  C99 defines three pragmas and says that no macro
951    expansion is to be performed on them; whether or not macro
952    expansion happens for other pragmas is implementation defined.
953    This implementation never macro-expands the text after #pragma.
954
955    We currently do not support the _Pragma operator.  Support for that
956    has to be coordinated with the front end.  Proposed implementation:
957    both #pragma blah blah and _Pragma("blah blah") become
958    __builtin_pragma(blah blah) and we teach the parser about that.  */
959
960 /* Sub-handlers for the pragmas needing treatment here.
961    They return 1 if the token buffer is to be popped, 0 if not. */
962 static int do_pragma_once               PARAMS ((cpp_reader *));
963 static int do_pragma_implementation     PARAMS ((cpp_reader *));
964 static int do_pragma_poison             PARAMS ((cpp_reader *));
965 static int do_pragma_default            PARAMS ((cpp_reader *));
966
967 static int
968 do_pragma (pfile)
969      cpp_reader *pfile;
970 {
971   long here, key;
972   U_CHAR *buf;
973   int pop;
974   enum cpp_ttype token;
975
976   here = CPP_WRITTEN (pfile);
977   CPP_PUTS (pfile, "#pragma ", 8);
978
979   key = CPP_WRITTEN (pfile);
980   pfile->no_macro_expand++;
981   token = _cpp_get_directive_token (pfile);
982   if (token != CPP_NAME)
983     {
984       if (token == CPP_VSPACE)
985         goto empty;
986       else
987         goto skip;
988     }
989
990   buf = pfile->token_buffer + key;
991   CPP_PUTC (pfile, ' ');
992
993 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
994   if (tokis ("once"))
995     pop = do_pragma_once (pfile);
996   else if (tokis ("implementation"))
997     pop = do_pragma_implementation (pfile);
998   else if (tokis ("poison"))
999     pop = do_pragma_poison (pfile);
1000   else
1001     pop = do_pragma_default (pfile);
1002 #undef tokis
1003
1004   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1005     goto skip;
1006
1007   if (pop)
1008     CPP_SET_WRITTEN (pfile, here);
1009   pfile->no_macro_expand--;
1010   return 0;
1011
1012  skip:
1013   cpp_error (pfile, "malformed #pragma directive");
1014   _cpp_skip_rest_of_line (pfile);
1015  empty:
1016   CPP_SET_WRITTEN (pfile, here);
1017   pfile->no_macro_expand--;
1018   return 0;
1019 }
1020
1021 static int
1022 do_pragma_default (pfile)
1023      cpp_reader *pfile;
1024 {
1025   while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1026     CPP_PUTC (pfile, ' ');
1027   return 0;
1028 }
1029
1030 static int
1031 do_pragma_once (pfile)
1032      cpp_reader *pfile;
1033 {
1034   cpp_buffer *ip = CPP_BUFFER (pfile);
1035
1036   /* Allow #pragma once in system headers, since that's not the user's
1037      fault.  */
1038   if (!ip->system_header_p)
1039     cpp_warning (pfile, "`#pragma once' is obsolete");
1040       
1041   if (CPP_PREV_BUFFER (ip) == NULL)
1042     cpp_warning (pfile, "`#pragma once' outside include file");
1043   else
1044     ip->ihash->control_macro = (const U_CHAR *) "";  /* never repeat */
1045
1046   return 1;
1047 }
1048
1049 static int
1050 do_pragma_implementation (pfile)
1051      cpp_reader *pfile;
1052 {
1053   /* Be quiet about `#pragma implementation' for a file only if it hasn't
1054      been included yet.  */
1055   enum cpp_ttype token;
1056   long written = CPP_WRITTEN (pfile);
1057   U_CHAR *name;
1058   U_CHAR *copy;
1059   size_t len;
1060
1061   token = _cpp_get_directive_token (pfile);
1062   if (token == CPP_VSPACE)
1063     return 0;
1064   else if (token != CPP_STRING)
1065     {
1066       cpp_error (pfile, "malformed #pragma implementation");
1067       return 1;
1068     }
1069
1070   /* Trim the leading and trailing quote marks from the string.  */
1071   name = pfile->token_buffer + written + 1;
1072   len = CPP_PWRITTEN (pfile) - name;
1073   copy = (U_CHAR *) alloca (len);
1074   memcpy (copy, name, len - 1);
1075   copy[len - 1] = '\0';
1076   
1077   if (cpp_included (pfile, copy))
1078     cpp_warning (pfile,
1079          "`#pragma implementation' for `%s' appears after file is included",
1080                  copy);
1081   return 0;
1082 }
1083
1084 static int
1085 do_pragma_poison (pfile)
1086      cpp_reader *pfile;
1087 {
1088   /* Poison these symbols so that all subsequent usage produces an
1089      error message.  */
1090   U_CHAR *p;
1091   HASHNODE **slot;
1092   long written;
1093   size_t len;
1094   enum cpp_ttype token;
1095   int writeit;
1096   unsigned long hash;
1097
1098   /* As a rule, don't include #pragma poison commands in output,  
1099      unless the user asks for them.  */
1100   writeit = (CPP_OPTION (pfile, debug_output)
1101              || CPP_OPTION (pfile, dump_macros) == dump_definitions
1102              || CPP_OPTION (pfile, dump_macros) == dump_names);
1103
1104   for (;;)
1105     {
1106       written = CPP_WRITTEN (pfile);
1107       token = _cpp_get_directive_token (pfile);
1108       if (token == CPP_VSPACE)
1109         break;
1110       if (token != CPP_NAME)
1111         {
1112           cpp_error (pfile, "invalid #pragma poison directive");
1113           _cpp_skip_rest_of_line (pfile);
1114           return 1;
1115         }
1116
1117       p = pfile->token_buffer + written;
1118       len = strlen (p);
1119       slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1120       if (*slot)
1121         {
1122           HASHNODE *hp = *slot;
1123           if (hp->type != T_POISON)
1124             {
1125               cpp_warning (pfile, "poisoning existing macro `%s'", p);
1126               if (hp->type == T_MACRO)
1127                 _cpp_free_definition (hp->value.defn);
1128               hp->value.defn = 0;
1129               hp->type = T_POISON;
1130             }
1131         }
1132       else
1133         {
1134           HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1135           hp->value.cpval = 0;
1136           *slot = hp;
1137         }
1138       if (writeit)
1139         CPP_PUTC (pfile, ' ');
1140     }
1141   return !writeit;
1142 }
1143  
1144 /* Just ignore #sccs, on systems where we define it at all.  */
1145 #ifdef SCCS_DIRECTIVE
1146 static int
1147 do_sccs (pfile)
1148      cpp_reader *pfile;
1149 {
1150   _cpp_skip_rest_of_line (pfile);
1151   return 0;
1152 }
1153 #endif
1154
1155 /* We've found an `#if' directive.  If the only thing before it in
1156    this file is white space, and if it is of the form
1157    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1158    for inclusion of this file.  (See redundant_include_p in cppfiles.c
1159    for an explanation of controlling macros.)  If so, return a
1160    malloc'd copy of SYMBOL.  Otherwise, return NULL.  */
1161
1162 static U_CHAR *
1163 detect_if_not_defined (pfile)
1164      cpp_reader *pfile;
1165 {
1166   U_CHAR *control_macro = 0;
1167
1168   if (pfile->only_seen_white == 2)
1169     {
1170       U_CHAR *ident;
1171       enum cpp_ttype token;
1172       int base_offset;
1173       int token_offset;
1174       int need_rparen = 0;
1175
1176       /* Save state required for restore.  */
1177       pfile->no_macro_expand++;
1178       CPP_SET_MARK (pfile);
1179       base_offset = CPP_WRITTEN (pfile);
1180
1181       /* Look for `!', */
1182       if (_cpp_get_directive_token (pfile) != CPP_OTHER
1183           || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1184           || CPP_PWRITTEN (pfile)[-1] != '!')
1185         goto restore;
1186
1187       /* ...then `defined', */
1188       token_offset = CPP_WRITTEN (pfile);
1189       token = _cpp_get_directive_token (pfile);
1190       if (token != CPP_NAME)
1191         goto restore;
1192       ident = pfile->token_buffer + token_offset;
1193       CPP_NUL_TERMINATE (pfile);
1194       if (strcmp (ident, "defined"))
1195         goto restore;
1196
1197       /* ...then an optional '(' and the name, */
1198       token_offset = CPP_WRITTEN (pfile);
1199       token = _cpp_get_directive_token (pfile);
1200       if (token == CPP_LPAREN)
1201         {
1202           token_offset = CPP_WRITTEN (pfile);
1203           token = _cpp_get_directive_token (pfile);
1204           if (token != CPP_NAME)
1205             goto restore;
1206           need_rparen = 1;
1207         }
1208       else if (token != CPP_NAME)
1209         goto restore;
1210
1211       ident = pfile->token_buffer + token_offset;
1212       CPP_NUL_TERMINATE (pfile);
1213
1214       /* ...then the ')', if necessary, */
1215       if ((!need_rparen || _cpp_get_directive_token (pfile) == CPP_RPAREN)
1216           /* ...and make sure there's nothing else on the line.  */
1217           && _cpp_get_directive_token (pfile) == CPP_VSPACE)
1218         control_macro = (U_CHAR *) xstrdup (ident);
1219
1220     restore:
1221       CPP_SET_WRITTEN (pfile, base_offset);
1222       pfile->no_macro_expand--;
1223       CPP_GOTO_MARK (pfile);
1224     }
1225
1226   return control_macro;
1227 }
1228
1229 /*
1230  * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1231  * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1232  */
1233
1234 static int
1235 do_if (pfile)
1236      cpp_reader *pfile;
1237 {
1238   U_CHAR *control_macro = detect_if_not_defined (pfile);
1239   int value = _cpp_parse_expr (pfile);
1240   return conditional_skip (pfile, value == 0, T_IF, control_macro);
1241 }
1242
1243 /*
1244  * handle a #elif directive by not changing  if_stack  either.
1245  * see the comment above do_else.
1246  */
1247
1248 static int
1249 do_elif (pfile)
1250      cpp_reader *pfile;
1251 {
1252   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1253     {
1254       cpp_error (pfile, "`#elif' not within a conditional");
1255       return 0;
1256     }
1257   else
1258     {
1259       if (pfile->if_stack->type == T_ELSE)
1260         {
1261           cpp_error (pfile, "`#elif' after `#else'");
1262           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1263                                "the conditional began here");
1264         }
1265       pfile->if_stack->type = T_ELIF;
1266     }
1267
1268   if (pfile->if_stack->if_succeeded)
1269     {
1270       _cpp_skip_rest_of_line (pfile);
1271       return skip_if_group (pfile);
1272     }
1273   if (_cpp_parse_expr (pfile) == 0)
1274     return skip_if_group (pfile);
1275
1276   ++pfile->if_stack->if_succeeded;      /* continue processing input */
1277   return 0;
1278 }
1279
1280 /* Parse an #ifdef or #ifndef directive.  Returns 1 for defined, 0 for
1281    not defined; the macro tested is left in the token buffer (but
1282    popped).  */
1283
1284 static int
1285 parse_ifdef (pfile, name)
1286      cpp_reader *pfile;
1287      const char *name;
1288 {
1289   U_CHAR *ident;
1290   unsigned int len;
1291   enum cpp_ttype token;
1292   long old_written = CPP_WRITTEN (pfile);
1293   int defined;
1294
1295   pfile->no_macro_expand++;
1296   token = _cpp_get_directive_token (pfile);
1297   pfile->no_macro_expand--;
1298
1299   ident = pfile->token_buffer + old_written;
1300   len = CPP_WRITTEN (pfile) - old_written;
1301
1302   if (token == CPP_VSPACE)
1303     {
1304       if (! CPP_TRADITIONAL (pfile))
1305         cpp_pedwarn (pfile, "`#%s' with no argument", name);
1306       defined = 0;
1307       goto done;
1308     }
1309   else if (token == CPP_NAME)
1310     {
1311       defined = cpp_defined (pfile, ident, len);
1312       CPP_NUL_TERMINATE (pfile);
1313       CPP_ADJUST_WRITTEN (pfile, 1);
1314     }
1315   else
1316     {
1317       defined = 0;
1318       if (! CPP_TRADITIONAL (pfile))
1319         cpp_error (pfile, "`#%s' with invalid argument", name);
1320     }
1321
1322   if (!CPP_TRADITIONAL (pfile))
1323     {
1324       if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1325         goto done;
1326       
1327       cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1328     }
1329   _cpp_skip_rest_of_line (pfile);
1330   
1331  done:
1332   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1333   return defined;
1334 }
1335
1336 /* #ifdef is dead simple.  */
1337
1338 static int
1339 do_ifdef (pfile)
1340      cpp_reader *pfile;
1341 {
1342   int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1343   return conditional_skip (pfile, skip, T_IFDEF, 0);
1344 }
1345
1346 /* #ifndef is a tad more complex, because we need to check for a
1347    no-reinclusion wrapper.  */
1348
1349 static int
1350 do_ifndef (pfile)
1351      cpp_reader *pfile;
1352 {
1353   int start_of_file, skip;
1354   U_CHAR *control_macro = 0;
1355
1356   start_of_file = pfile->only_seen_white == 2;
1357   skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1358
1359   if (start_of_file && !skip)
1360     control_macro = (U_CHAR *) xstrdup (CPP_PWRITTEN (pfile));
1361
1362   return conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1363 }
1364
1365 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1366    If this is a #ifndef starting at the beginning of a file,
1367    CONTROL_MACRO is the macro name tested by the #ifndef.
1368    Otherwise, CONTROL_MACRO is 0.  */
1369
1370 static int
1371 conditional_skip (pfile, skip, type, control_macro)
1372      cpp_reader *pfile;
1373      int skip;
1374      int type;
1375      U_CHAR *control_macro;
1376 {
1377   IF_STACK *temp;
1378
1379   temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1380   temp->lineno = CPP_BUFFER (pfile)->lineno;
1381   temp->next = pfile->if_stack;
1382   temp->control_macro = control_macro;
1383   pfile->if_stack = temp;
1384
1385   pfile->if_stack->type = type;
1386
1387   if (skip != 0)
1388     return skip_if_group (pfile);
1389
1390   ++pfile->if_stack->if_succeeded;
1391   return 0;
1392 }
1393
1394 /* Subroutine of skip_if_group.  Examine one preprocessing directive
1395    and return 0 if skipping should continue, or the directive number
1396    of the directive that ends the block if it should halt.
1397
1398    Also adjusts the if_stack as appropriate.  The `#' has been read,
1399    but not the identifier. */
1400
1401 static int
1402 consider_directive_while_skipping (pfile, stack)
1403     cpp_reader *pfile;
1404     IF_STACK *stack; 
1405 {
1406   long ident;
1407   int i, hash_at_bol;
1408   unsigned int len;
1409   IF_STACK *temp;
1410
1411   /* -traditional directives are recognized only with the # in column 1.  */
1412   hash_at_bol = CPP_IN_COLUMN_1 (pfile);
1413
1414   ident = CPP_WRITTEN (pfile);
1415   if (_cpp_get_directive_token (pfile) != CPP_NAME)
1416     return 0;
1417   len = CPP_WRITTEN (pfile) - ident;
1418
1419   for (i = 0; i < N_DIRECTIVES; i++)
1420     {
1421       if (dtable[i].length == len
1422           && !strncmp (dtable[i].name, pfile->token_buffer + ident, len)) 
1423         goto real_directive;
1424     }
1425   return 0;
1426
1427  real_directive:
1428
1429   /* If it's not a directive of interest to us, return now.  */
1430   if (dtable[i].origin != COND)
1431     return 0;
1432
1433   /* First, deal with -traditional and -Wtraditional.
1434      All COND directives are from K+R.  */
1435
1436   if (! hash_at_bol)
1437     {
1438       if (CPP_TRADITIONAL (pfile))
1439         {
1440           if (CPP_WTRADITIONAL (pfile))
1441             cpp_warning (pfile, "ignoring #%s because of its indented #",
1442                          dtable[i].name);
1443           return 0;
1444         }
1445       if (CPP_WTRADITIONAL (pfile))
1446         cpp_warning (pfile, "traditional C ignores %s with the # indented",
1447                      dtable[i].name);
1448     }
1449   
1450   switch (i)
1451     {
1452     default:
1453       cpp_ice (pfile, "non COND directive in switch in c_d_w_s");
1454       return 0;
1455
1456     case T_IF:
1457     case T_IFDEF:
1458     case T_IFNDEF:
1459       temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1460       temp->lineno = CPP_BUFFER (pfile)->lineno;
1461       temp->next = pfile->if_stack;
1462       temp->type = i;
1463       pfile->if_stack = temp;
1464       return 0;
1465
1466     case T_ELSE:
1467       if (pfile->if_stack != stack)
1468         validate_else (pfile, dtable[i].name);
1469       /* fall through */
1470     case T_ELIF:
1471       if (pfile->if_stack == stack)
1472         return i;
1473
1474       pfile->if_stack->type = i;
1475       return 0;
1476
1477     case T_ENDIF:
1478       if (pfile->if_stack != stack)
1479         validate_else (pfile, dtable[i].name);
1480
1481       if (pfile->if_stack == stack)
1482         return i;
1483                     
1484       temp = pfile->if_stack;
1485       pfile->if_stack = temp->next;
1486       free (temp);
1487       return 0;
1488     }
1489 }
1490
1491 /* Skip to #endif, #else, or #elif.  Consumes the directive that
1492    causes it to stop, but not its argument.  Returns the number of
1493    that directive, which must be passed back up to
1494    _cpp_handle_directive, which will execute it.  */
1495 static int
1496 skip_if_group (pfile)
1497     cpp_reader *pfile;
1498 {
1499   enum cpp_ttype token;
1500   IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1501   long old_written;
1502   int ret = 0;
1503
1504   /* We are no longer at the start of the file.  */
1505   pfile->only_seen_white = 0;
1506
1507   old_written = CPP_WRITTEN (pfile);
1508   pfile->no_macro_expand++;
1509   CPP_OPTION (pfile, no_line_commands)++;
1510   for (;;)
1511     {
1512       /* We are at the end of a line.  Only cpp_get_token knows how to
1513          advance the line number correctly.  */
1514       token = cpp_get_token (pfile);
1515       if (token == CPP_POP)
1516         break;  /* Caller will issue error.  */
1517       
1518       else if (token != CPP_VSPACE)
1519         cpp_ice (pfile, "cpp_get_token returned %d in skip_if_group", token);
1520       CPP_SET_WRITTEN (pfile, old_written);
1521
1522       token = _cpp_get_directive_token (pfile);
1523
1524       if (token == CPP_DIRECTIVE)
1525         {
1526           ret = consider_directive_while_skipping (pfile, save_if_stack);
1527           if (ret)
1528             break;
1529         }
1530
1531       if (token != CPP_VSPACE)
1532         _cpp_skip_rest_of_line (pfile);
1533     }
1534   CPP_SET_WRITTEN (pfile, old_written);
1535   pfile->no_macro_expand--;
1536   CPP_OPTION (pfile, no_line_commands)--;
1537   return ret;
1538 }
1539
1540 /*
1541  * handle a #else directive.  Do this by just continuing processing
1542  * without changing  if_stack ;  this is so that the error message
1543  * for missing #endif's etc. will point to the original #if.  It
1544  * is possible that something different would be better.
1545  */
1546
1547 static int
1548 do_else (pfile)
1549      cpp_reader *pfile;
1550 {
1551   validate_else (pfile, dtable[T_ELSE].name);
1552   _cpp_skip_rest_of_line (pfile);
1553
1554   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1555     {
1556       cpp_error (pfile, "`#else' not within a conditional");
1557       return 0;
1558     }
1559   else
1560     {
1561       /* #ifndef can't have its special treatment for containing the whole file
1562          if it has a #else clause.  */
1563       pfile->if_stack->control_macro = 0;
1564
1565       if (pfile->if_stack->type == T_ELSE)
1566         {
1567           cpp_error (pfile, "`#else' after `#else'");
1568           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1569                                "the conditional began here");
1570         }
1571       pfile->if_stack->type = T_ELSE;
1572     }
1573
1574   if (pfile->if_stack->if_succeeded)
1575     return skip_if_group (pfile);
1576   
1577   ++pfile->if_stack->if_succeeded;      /* continue processing input */
1578   return 0;
1579 }
1580
1581 /*
1582  * unstack after #endif command
1583  */
1584
1585 static int
1586 do_endif (pfile)
1587      cpp_reader *pfile;
1588 {
1589   validate_else (pfile, dtable[T_ENDIF].name);
1590   _cpp_skip_rest_of_line (pfile);
1591
1592   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1593     cpp_error (pfile, "`#endif' not within a conditional");
1594   else
1595     {
1596       IF_STACK *temp = pfile->if_stack;
1597       pfile->if_stack = temp->next;
1598       if (temp->control_macro != 0)
1599         pfile->potential_control_macro = temp->control_macro;
1600       free (temp);
1601     }
1602   return 0;
1603 }
1604
1605 /* Issue -pedantic warning for text which is not a comment following
1606    an #else or #endif.  Do not warn in system headers, as this is harmless
1607    and very common on old systems.  */
1608
1609 static void
1610 validate_else (pfile, directive)
1611      cpp_reader *pfile;
1612      const char *directive;
1613 {
1614   long old_written;
1615   if (! CPP_PEDANTIC (pfile))
1616     return;
1617
1618   old_written = CPP_WRITTEN (pfile);
1619   pfile->no_macro_expand++;
1620   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1621     cpp_pedwarn (pfile,
1622                  "text following `#%s' violates ANSI standard", directive);
1623   CPP_SET_WRITTEN (pfile, old_written);
1624   pfile->no_macro_expand--;
1625 }
1626
1627 void
1628 _cpp_handle_eof (pfile)
1629      cpp_reader *pfile;
1630 {
1631   cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
1632   struct if_stack *ifs, *nifs;
1633
1634   /* Unwind the conditional stack and generate error messages.  */
1635   for (ifs = pfile->if_stack;
1636        ifs != CPP_BUFFER (pfile)->if_stack;
1637        ifs = nifs)
1638     {
1639       cpp_error_with_line (pfile, ifs->lineno, -1,
1640                            "unterminated `#%s' conditional",
1641                            dtable[ifs->type].name);
1642
1643       nifs = ifs->next;
1644       free (ifs);
1645     }
1646   pfile->if_stack = ifs;
1647
1648   if (pfile->potential_control_macro)
1649     {
1650       CPP_BUFFER (pfile)->ihash->control_macro
1651         = pfile->potential_control_macro;
1652       pfile->potential_control_macro = 0;
1653     }
1654
1655   if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
1656     {
1657       /* We're about to return from an #include file.
1658          Emit #line information now (as part of the CPP_POP) result.
1659          But the #line refers to the file we will pop to.  */
1660       cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
1661       CPP_BUFFER (pfile) = next_buf;
1662       pfile->input_stack_listing_current = 0;
1663       _cpp_output_line_command (pfile, leave_file);
1664       CPP_BUFFER (pfile) = cur_buffer;
1665     }
1666
1667   CPP_BUFFER (pfile)->seen_eof = 1;
1668 }
1669
1670 static int
1671 do_assert (pfile)
1672      cpp_reader *pfile;
1673 {
1674   long old_written;
1675   U_CHAR *sym;
1676   int ret;
1677   HASHNODE *base, *this;
1678   HASHNODE **bslot, **tslot;
1679   size_t blen, tlen;
1680   unsigned long bhash, thash;
1681
1682   old_written = CPP_WRITTEN (pfile);    /* remember where it starts */
1683   ret = _cpp_parse_assertion (pfile);
1684   if (ret == 0)
1685     goto error;
1686   else if (ret == 1)
1687     {
1688       cpp_error (pfile, "missing token-sequence in #assert");
1689       goto error;
1690     }
1691   tlen = CPP_WRITTEN (pfile) - old_written;
1692
1693   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1694     {
1695       cpp_error (pfile, "junk at end of #assert");
1696       goto error;
1697     }
1698
1699   sym = pfile->token_buffer + old_written;
1700   blen = (U_CHAR *) strchr (sym, '(') - sym;
1701   tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
1702   if (*tslot)
1703     {
1704       cpp_warning (pfile, "%s re-asserted", sym);
1705       goto error;
1706     }
1707
1708   bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
1709   if (! *bslot)
1710     {
1711       *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1712       base->value.aschain = 0;
1713     }
1714   else
1715     {
1716       base = *bslot;
1717       if (base->type != T_ASSERT)
1718         {
1719           /* Token clash - but with what?! */
1720           cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1721           goto error;
1722         }
1723     }
1724   *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1725   this->value.aschain = base->value.aschain;
1726   base->value.aschain = this;
1727
1728  error:
1729   _cpp_skip_rest_of_line (pfile);
1730   CPP_SET_WRITTEN (pfile, old_written);
1731   return 0;
1732 }
1733
1734 static int
1735 do_unassert (pfile)
1736      cpp_reader *pfile;
1737 {
1738   int ret;
1739   long old_written;
1740   U_CHAR *sym;
1741   long baselen, thislen;
1742   HASHNODE *base, *this, *next;
1743
1744   old_written = CPP_WRITTEN (pfile);
1745   ret = _cpp_parse_assertion (pfile);
1746   if (ret == 0)
1747     goto error;
1748   thislen = CPP_WRITTEN (pfile) - old_written;
1749
1750   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1751     {
1752       cpp_error (pfile, "junk at end of #unassert");
1753       goto error;
1754     }
1755   sym = pfile->token_buffer + old_written;
1756   CPP_SET_WRITTEN (pfile, old_written);
1757
1758   if (ret == 1)
1759     {
1760       base = _cpp_lookup (pfile, sym, thislen);
1761       if (! base)
1762         goto error;  /* It isn't an error to #undef what isn't #defined,
1763                         so it isn't an error to #unassert what isn't
1764                         #asserted either. */
1765       
1766       for (this = base->value.aschain; this; this = next)
1767         {
1768           next = this->value.aschain;
1769           htab_remove_elt (pfile->hashtab, this);
1770         }
1771       htab_remove_elt (pfile->hashtab, base);
1772     }
1773   else
1774     {
1775       baselen = (U_CHAR *) strchr (sym, '(') - sym;
1776       base = _cpp_lookup (pfile, sym, baselen);
1777       if (! base) goto error;
1778       this = _cpp_lookup (pfile, sym, thislen);
1779       if (! this) goto error;
1780
1781       next = base;
1782       while (next->value.aschain != this)
1783         next = next->value.aschain;
1784
1785       next->value.aschain = this->value.aschain;
1786       htab_remove_elt (pfile->hashtab, this);
1787
1788       if (base->value.aschain == NULL)
1789         /* Last answer for this predicate deleted. */
1790         htab_remove_elt (pfile->hashtab, base);
1791     }
1792   return 0;
1793   
1794  error:
1795   _cpp_skip_rest_of_line (pfile);
1796   CPP_SET_WRITTEN (pfile, old_written);
1797   return 0;
1798 }
1799
1800 /* These are for -D, -U, -A.  */
1801
1802 /* Process the string STR as if it appeared as the body of a #define.
1803    If STR is just an identifier, define it with value 1.
1804    If STR has anything after the identifier, then it should
1805    be identifier=definition. */
1806
1807 void
1808 cpp_define (pfile, str)
1809      cpp_reader *pfile;
1810      const char *str;
1811 {
1812   char *buf, *p;
1813   size_t count;
1814
1815   p = strchr (str, '=');
1816   /* Copy the entire option so we can modify it. 
1817      Change the first "=" in the string to a space.  If there is none,
1818      tack " 1" on the end.  Then add a newline and a NUL.  */
1819   
1820   if (p)
1821     {
1822       count = strlen (str) + 2;
1823       buf = alloca (count);
1824       memcpy (buf, str, count - 2);
1825       buf[p - str] = ' ';
1826       buf[count - 2] = '\n';
1827       buf[count - 1] = '\0';
1828     }
1829   else
1830     {
1831       count = strlen (str) + 4;
1832       buf = alloca (count);
1833       memcpy (buf, str, count - 4);
1834       strcpy (&buf[count-4], " 1\n");
1835     }
1836
1837   if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1838     {
1839       do_define (pfile);
1840       cpp_pop_buffer (pfile);
1841     }
1842 }
1843
1844 /* Process MACRO as if it appeared as the body of an #undef.  */
1845 void
1846 cpp_undef (pfile, macro)
1847      cpp_reader *pfile;
1848      const char *macro;
1849 {
1850   /* Copy the string so we can append a newline.  */
1851   size_t len = strlen (macro);
1852   char *buf = alloca (len + 2);
1853   memcpy (buf, macro, len);
1854   buf[len]     = '\n';
1855   buf[len + 1] = '\0';
1856   if (cpp_push_buffer (pfile, buf, len + 1))
1857     {
1858       do_undef (pfile);
1859       cpp_pop_buffer (pfile);
1860     }
1861 }
1862
1863 /* Process the string STR as if it appeared as the body of a #assert. */
1864 void
1865 cpp_assert (pfile, str)
1866      cpp_reader *pfile;
1867      const char *str;
1868 {
1869   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1870     {
1871       do_assert (pfile);
1872       cpp_pop_buffer (pfile);
1873     }
1874 }
1875
1876 /* Process STR as if it appeared as the body of an #unassert. */
1877 void
1878 cpp_unassert (pfile, str)
1879      cpp_reader *pfile;
1880      const char *str;
1881 {
1882   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1883     {
1884       do_unassert (pfile);
1885       cpp_pop_buffer (pfile);
1886     }
1887 }  
1888
1889 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
1890 int
1891 cpp_defined (pfile, id, len)
1892      cpp_reader *pfile;
1893      const U_CHAR *id;
1894      int len;
1895 {
1896   HASHNODE *hp = _cpp_lookup (pfile, id, len);
1897   if (hp && hp->type == T_POISON)
1898     {
1899       cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1900       return 0;
1901     }
1902   return (hp != NULL);
1903 }