OSDN Git Service

* config/sparc/sparc.md (movtf_insn_sp32, movtf_insn_vis_sp32,
[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_token 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_token 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   long 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   cpp_buf_line_and_col (ip, &line, NULL);
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), "%ld ", 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_token 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_token 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_token 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_token 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_token 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_token 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_token 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_token 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     return skip_if_group (pfile);
1270   if (_cpp_parse_expr (pfile) == 0)
1271     return skip_if_group (pfile);
1272
1273   ++pfile->if_stack->if_succeeded;      /* continue processing input */
1274   return 0;
1275 }
1276
1277 /* Parse an #ifdef or #ifndef directive.  Returns 1 for defined, 0 for
1278    not defined; the macro tested is left in the token buffer (but
1279    popped).  */
1280
1281 static int
1282 parse_ifdef (pfile, name)
1283      cpp_reader *pfile;
1284      const char *name;
1285 {
1286   U_CHAR *ident;
1287   unsigned int len;
1288   enum cpp_token token;
1289   long old_written = CPP_WRITTEN (pfile);
1290   int defined;
1291
1292   pfile->no_macro_expand++;
1293   token = _cpp_get_directive_token (pfile);
1294   pfile->no_macro_expand--;
1295
1296   ident = pfile->token_buffer + old_written;
1297   len = CPP_WRITTEN (pfile) - old_written;
1298
1299   if (token == CPP_VSPACE)
1300     {
1301       if (! CPP_TRADITIONAL (pfile))
1302         cpp_pedwarn (pfile, "`#%s' with no argument", name);
1303       defined = 0;
1304       goto done;
1305     }
1306   else if (token == CPP_NAME)
1307     {
1308       defined = cpp_defined (pfile, ident, len);
1309       CPP_NUL_TERMINATE (pfile);
1310       CPP_ADJUST_WRITTEN (pfile, 1);
1311     }
1312   else
1313     {
1314       defined = 0;
1315       if (! CPP_TRADITIONAL (pfile))
1316         cpp_error (pfile, "`#%s' with invalid argument", name);
1317     }
1318
1319   if (!CPP_TRADITIONAL (pfile))
1320     {
1321       if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1322         goto done;
1323       
1324       cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1325     }
1326   _cpp_skip_rest_of_line (pfile);
1327   
1328  done:
1329   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1330   return defined;
1331 }
1332
1333 /* #ifdef is dead simple.  */
1334
1335 static int
1336 do_ifdef (pfile)
1337      cpp_reader *pfile;
1338 {
1339   int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1340   return conditional_skip (pfile, skip, T_IFDEF, 0);
1341 }
1342
1343 /* #ifndef is a tad more complex, because we need to check for a
1344    no-reinclusion wrapper.  */
1345
1346 static int
1347 do_ifndef (pfile)
1348      cpp_reader *pfile;
1349 {
1350   int start_of_file, skip;
1351   U_CHAR *control_macro = 0;
1352
1353   start_of_file = pfile->only_seen_white == 2;
1354   skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1355
1356   if (start_of_file && !skip)
1357     control_macro = (U_CHAR *) xstrdup (CPP_PWRITTEN (pfile));
1358
1359   return conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1360 }
1361
1362 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1363    If this is a #ifndef starting at the beginning of a file,
1364    CONTROL_MACRO is the macro name tested by the #ifndef.
1365    Otherwise, CONTROL_MACRO is 0.  */
1366
1367 static int
1368 conditional_skip (pfile, skip, type, control_macro)
1369      cpp_reader *pfile;
1370      int skip;
1371      int type;
1372      U_CHAR *control_macro;
1373 {
1374   IF_STACK *temp;
1375
1376   temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1377   temp->lineno = CPP_BUFFER (pfile)->lineno;
1378   temp->next = pfile->if_stack;
1379   temp->control_macro = control_macro;
1380   pfile->if_stack = temp;
1381
1382   pfile->if_stack->type = type;
1383
1384   if (skip != 0)
1385     return skip_if_group (pfile);
1386
1387   ++pfile->if_stack->if_succeeded;
1388   return 0;
1389 }
1390
1391 /* Subroutine of skip_if_group.  Examine one preprocessing directive
1392    and return 0 if skipping should continue, or the directive number
1393    of the directive that ends the block if it should halt.
1394
1395    Also adjusts the if_stack as appropriate.  The `#' has been read,
1396    but not the identifier. */
1397
1398 static int
1399 consider_directive_while_skipping (pfile, stack)
1400     cpp_reader *pfile;
1401     IF_STACK *stack; 
1402 {
1403   long ident;
1404   int i, hash_at_bol;
1405   unsigned int len;
1406   IF_STACK *temp;
1407
1408   /* -traditional directives are recognized only with the # in column 1.  */
1409   hash_at_bol = CPP_IN_COLUMN_1 (pfile);
1410
1411   ident = CPP_WRITTEN (pfile);
1412   if (_cpp_get_directive_token (pfile) != CPP_NAME)
1413     return 0;
1414   len = CPP_WRITTEN (pfile) - ident;
1415
1416   for (i = 0; i < N_DIRECTIVES; i++)
1417     {
1418       if (dtable[i].length == len
1419           && !strncmp (dtable[i].name, pfile->token_buffer + ident, len)) 
1420         goto real_directive;
1421     }
1422   return 0;
1423
1424  real_directive:
1425
1426   /* If it's not a directive of interest to us, return now.  */
1427   if (dtable[i].origin != COND)
1428     return 0;
1429
1430   /* First, deal with -traditional and -Wtraditional.
1431      All COND directives are from K+R.  */
1432
1433   if (! hash_at_bol)
1434     {
1435       if (CPP_TRADITIONAL (pfile))
1436         {
1437           if (CPP_WTRADITIONAL (pfile))
1438             cpp_warning (pfile, "ignoring #%s because of its indented #",
1439                          dtable[i].name);
1440           return 0;
1441         }
1442       if (CPP_WTRADITIONAL (pfile))
1443         cpp_warning (pfile, "traditional C ignores %s with the # indented",
1444                      dtable[i].name);
1445     }
1446   
1447   switch (i)
1448     {
1449     default:
1450       cpp_ice (pfile, "non COND directive in switch in c_d_w_s");
1451       return 0;
1452
1453     case T_IF:
1454     case T_IFDEF:
1455     case T_IFNDEF:
1456       temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1457       temp->lineno = CPP_BUFFER (pfile)->lineno;
1458       temp->next = pfile->if_stack;
1459       temp->type = i;
1460       pfile->if_stack = temp;
1461       return 0;
1462
1463     case T_ELSE:
1464       if (pfile->if_stack != stack)
1465         validate_else (pfile, dtable[i].name);
1466       /* fall through */
1467     case T_ELIF:
1468       if (pfile->if_stack == stack)
1469         return i;
1470
1471       pfile->if_stack->type = i;
1472       return 0;
1473
1474     case T_ENDIF:
1475       if (pfile->if_stack != stack)
1476         validate_else (pfile, dtable[i].name);
1477
1478       if (pfile->if_stack == stack)
1479         return i;
1480                     
1481       temp = pfile->if_stack;
1482       pfile->if_stack = temp->next;
1483       free (temp);
1484       return 0;
1485     }
1486 }
1487
1488 /* Skip to #endif, #else, or #elif.  Consumes the directive that
1489    causes it to stop, but not its argument.  Returns the number of
1490    that directive, which must be passed back up to
1491    _cpp_handle_directive, which will execute it.  */
1492 static int
1493 skip_if_group (pfile)
1494     cpp_reader *pfile;
1495 {
1496   enum cpp_token token;
1497   IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1498   long old_written;
1499   int ret = 0;
1500
1501   old_written = CPP_WRITTEN (pfile);
1502   pfile->no_macro_expand++;
1503   CPP_OPTION (pfile, no_line_commands)++;
1504   for (;;)
1505     {
1506       token = _cpp_get_directive_token (pfile);
1507
1508       if (token == CPP_DIRECTIVE)
1509         {
1510           ret = consider_directive_while_skipping (pfile, save_if_stack);
1511           if (ret)
1512             break;
1513         }
1514
1515       if (token != CPP_VSPACE)
1516         _cpp_skip_rest_of_line (pfile);
1517
1518       /* Only cpp_get_token knows how to advance the line number
1519          properly.  */
1520       token = cpp_get_token (pfile);
1521       if (token == CPP_POP)
1522         break;  /* Caller will issue error.  */
1523       
1524       else if (token != CPP_VSPACE)
1525         cpp_ice (pfile, "cpp_get_token returned %d in skip_if_group", token);
1526       CPP_SET_WRITTEN (pfile, old_written);
1527     }
1528   CPP_SET_WRITTEN (pfile, old_written);
1529   pfile->no_macro_expand--;
1530   CPP_OPTION (pfile, no_line_commands)--;
1531   return ret;
1532 }
1533
1534 /*
1535  * handle a #else directive.  Do this by just continuing processing
1536  * without changing  if_stack ;  this is so that the error message
1537  * for missing #endif's etc. will point to the original #if.  It
1538  * is possible that something different would be better.
1539  */
1540
1541 static int
1542 do_else (pfile)
1543      cpp_reader *pfile;
1544 {
1545   validate_else (pfile, dtable[T_ELSE].name);
1546   _cpp_skip_rest_of_line (pfile);
1547
1548   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1549     {
1550       cpp_error (pfile, "`#else' not within a conditional");
1551       return 0;
1552     }
1553   else
1554     {
1555       /* #ifndef can't have its special treatment for containing the whole file
1556          if it has a #else clause.  */
1557       pfile->if_stack->control_macro = 0;
1558
1559       if (pfile->if_stack->type == T_ELSE)
1560         {
1561           cpp_error (pfile, "`#else' after `#else'");
1562           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1563                                "the conditional began here");
1564         }
1565       pfile->if_stack->type = T_ELSE;
1566     }
1567
1568   if (pfile->if_stack->if_succeeded)
1569     return skip_if_group (pfile);
1570   
1571   ++pfile->if_stack->if_succeeded;      /* continue processing input */
1572   return 0;
1573 }
1574
1575 /*
1576  * unstack after #endif command
1577  */
1578
1579 static int
1580 do_endif (pfile)
1581      cpp_reader *pfile;
1582 {
1583   validate_else (pfile, dtable[T_ENDIF].name);
1584   _cpp_skip_rest_of_line (pfile);
1585
1586   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1587     cpp_error (pfile, "`#endif' not within a conditional");
1588   else
1589     {
1590       IF_STACK *temp = pfile->if_stack;
1591       pfile->if_stack = temp->next;
1592       if (temp->control_macro != 0)
1593         pfile->potential_control_macro = temp->control_macro;
1594       free (temp);
1595     }
1596   return 0;
1597 }
1598
1599 /* Issue -pedantic warning for text which is not a comment following
1600    an #else or #endif.  Do not warn in system headers, as this is harmless
1601    and very common on old systems.  */
1602
1603 static void
1604 validate_else (pfile, directive)
1605      cpp_reader *pfile;
1606      const char *directive;
1607 {
1608   long old_written;
1609   if (! CPP_PEDANTIC (pfile))
1610     return;
1611
1612   old_written = CPP_WRITTEN (pfile);
1613   pfile->no_macro_expand++;
1614   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1615     cpp_pedwarn (pfile,
1616                  "text following `#%s' violates ANSI standard", directive);
1617   CPP_SET_WRITTEN (pfile, old_written);
1618   pfile->no_macro_expand--;
1619 }
1620
1621 void
1622 _cpp_handle_eof (pfile)
1623      cpp_reader *pfile;
1624 {
1625   cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
1626   struct if_stack *ifs, *nifs;
1627
1628   /* Unwind the conditional stack and generate error messages.  */
1629   for (ifs = pfile->if_stack;
1630        ifs != CPP_BUFFER (pfile)->if_stack;
1631        ifs = nifs)
1632     {
1633       cpp_error_with_line (pfile, ifs->lineno, -1,
1634                            "unterminated `#%s' conditional",
1635                            dtable[ifs->type].name);
1636
1637       nifs = ifs->next;
1638       free (ifs);
1639     }
1640   pfile->if_stack = ifs;
1641
1642   if (pfile->potential_control_macro)
1643     {
1644       CPP_BUFFER (pfile)->ihash->control_macro
1645         = pfile->potential_control_macro;
1646       pfile->potential_control_macro = 0;
1647     }
1648
1649   if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
1650     {
1651       /* We're about to return from an #include file.
1652          Emit #line information now (as part of the CPP_POP) result.
1653          But the #line refers to the file we will pop to.  */
1654       cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
1655       CPP_BUFFER (pfile) = next_buf;
1656       pfile->input_stack_listing_current = 0;
1657       _cpp_output_line_command (pfile, leave_file);
1658       CPP_BUFFER (pfile) = cur_buffer;
1659     }
1660
1661   CPP_BUFFER (pfile)->seen_eof = 1;
1662 }
1663
1664 static int
1665 do_assert (pfile)
1666      cpp_reader *pfile;
1667 {
1668   long old_written;
1669   U_CHAR *sym;
1670   int ret;
1671   HASHNODE *base, *this;
1672   HASHNODE **bslot, **tslot;
1673   size_t blen, tlen;
1674   unsigned long bhash, thash;
1675
1676   old_written = CPP_WRITTEN (pfile);    /* remember where it starts */
1677   ret = _cpp_parse_assertion (pfile);
1678   if (ret == 0)
1679     goto error;
1680   else if (ret == 1)
1681     {
1682       cpp_error (pfile, "missing token-sequence in #assert");
1683       goto error;
1684     }
1685   tlen = CPP_WRITTEN (pfile) - old_written;
1686
1687   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1688     {
1689       cpp_error (pfile, "junk at end of #assert");
1690       goto error;
1691     }
1692
1693   sym = pfile->token_buffer + old_written;
1694   blen = (U_CHAR *) strchr (sym, '(') - sym;
1695   tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
1696   if (*tslot)
1697     {
1698       cpp_warning (pfile, "%s re-asserted", sym);
1699       goto error;
1700     }
1701
1702   bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
1703   if (! *bslot)
1704     {
1705       *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1706       base->value.aschain = 0;
1707     }
1708   else
1709     {
1710       base = *bslot;
1711       if (base->type != T_ASSERT)
1712         {
1713           /* Token clash - but with what?! */
1714           cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1715           goto error;
1716         }
1717     }
1718   *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1719   this->value.aschain = base->value.aschain;
1720   base->value.aschain = this;
1721
1722  error:
1723   _cpp_skip_rest_of_line (pfile);
1724   CPP_SET_WRITTEN (pfile, old_written);
1725   return 0;
1726 }
1727
1728 static int
1729 do_unassert (pfile)
1730      cpp_reader *pfile;
1731 {
1732   int ret;
1733   long old_written;
1734   U_CHAR *sym;
1735   long baselen, thislen;
1736   HASHNODE *base, *this, *next;
1737
1738   old_written = CPP_WRITTEN (pfile);
1739   ret = _cpp_parse_assertion (pfile);
1740   if (ret == 0)
1741     goto error;
1742   thislen = CPP_WRITTEN (pfile) - old_written;
1743
1744   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1745     {
1746       cpp_error (pfile, "junk at end of #unassert");
1747       goto error;
1748     }
1749   sym = pfile->token_buffer + old_written;
1750   CPP_SET_WRITTEN (pfile, old_written);
1751
1752   if (ret == 1)
1753     {
1754       base = _cpp_lookup (pfile, sym, thislen);
1755       if (! base)
1756         goto error;  /* It isn't an error to #undef what isn't #defined,
1757                         so it isn't an error to #unassert what isn't
1758                         #asserted either. */
1759       
1760       for (this = base->value.aschain; this; this = next)
1761         {
1762           next = this->value.aschain;
1763           htab_remove_elt (pfile->hashtab, this);
1764         }
1765       htab_remove_elt (pfile->hashtab, base);
1766     }
1767   else
1768     {
1769       baselen = (U_CHAR *) strchr (sym, '(') - sym;
1770       base = _cpp_lookup (pfile, sym, baselen);
1771       if (! base) goto error;
1772       this = _cpp_lookup (pfile, sym, thislen);
1773       if (! this) goto error;
1774
1775       next = base;
1776       while (next->value.aschain != this)
1777         next = next->value.aschain;
1778
1779       next->value.aschain = this->value.aschain;
1780       htab_remove_elt (pfile->hashtab, this);
1781
1782       if (base->value.aschain == NULL)
1783         /* Last answer for this predicate deleted. */
1784         htab_remove_elt (pfile->hashtab, base);
1785     }
1786   return 0;
1787   
1788  error:
1789   _cpp_skip_rest_of_line (pfile);
1790   CPP_SET_WRITTEN (pfile, old_written);
1791   return 0;
1792 }
1793
1794 /* These are for -D, -U, -A.  */
1795
1796 /* Process the string STR as if it appeared as the body of a #define.
1797    If STR is just an identifier, define it with value 1.
1798    If STR has anything after the identifier, then it should
1799    be identifier=definition. */
1800
1801 void
1802 cpp_define (pfile, str)
1803      cpp_reader *pfile;
1804      const char *str;
1805 {
1806   char *buf, *p;
1807   size_t count;
1808
1809   p = strchr (str, '=');
1810   /* Copy the entire option so we can modify it. 
1811      Change the first "=" in the string to a space.  If there is none,
1812      tack " 1" on the end.  Then add a newline and a NUL.  */
1813   
1814   if (p)
1815     {
1816       count = strlen (str) + 2;
1817       buf = alloca (count);
1818       memcpy (buf, str, count - 2);
1819       buf[p - str] = ' ';
1820       buf[count - 2] = '\n';
1821       buf[count - 1] = '\0';
1822     }
1823   else
1824     {
1825       count = strlen (str) + 4;
1826       buf = alloca (count);
1827       memcpy (buf, str, count - 4);
1828       strcpy (&buf[count-4], " 1\n");
1829     }
1830
1831   if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1832     {
1833       do_define (pfile);
1834       cpp_pop_buffer (pfile);
1835     }
1836 }
1837
1838 /* Process MACRO as if it appeared as the body of an #undef.  */
1839 void
1840 cpp_undef (pfile, macro)
1841      cpp_reader *pfile;
1842      const char *macro;
1843 {
1844   /* Copy the string so we can append a newline.  */
1845   size_t len = strlen (macro);
1846   char *buf = alloca (len + 2);
1847   memcpy (buf, macro, len);
1848   buf[len]     = '\n';
1849   buf[len + 1] = '\0';
1850   if (cpp_push_buffer (pfile, buf, len + 1))
1851     {
1852       do_undef (pfile);
1853       cpp_pop_buffer (pfile);
1854     }
1855 }
1856
1857 /* Process the string STR as if it appeared as the body of a #assert. */
1858 void
1859 cpp_assert (pfile, str)
1860      cpp_reader *pfile;
1861      const char *str;
1862 {
1863   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1864     {
1865       do_assert (pfile);
1866       cpp_pop_buffer (pfile);
1867     }
1868 }
1869
1870 /* Process STR as if it appeared as the body of an #unassert. */
1871 void
1872 cpp_unassert (pfile, str)
1873      cpp_reader *pfile;
1874      const char *str;
1875 {
1876   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1877     {
1878       do_unassert (pfile);
1879       cpp_pop_buffer (pfile);
1880     }
1881 }  
1882
1883 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
1884 int
1885 cpp_defined (pfile, id, len)
1886      cpp_reader *pfile;
1887      const U_CHAR *id;
1888      int len;
1889 {
1890   HASHNODE *hp = _cpp_lookup (pfile, id, len);
1891   if (hp && hp->type == T_POISON)
1892     {
1893       cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1894       return 0;
1895     }
1896   return (hp != NULL);
1897 }