OSDN Git Service

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