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