OSDN Git Service

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