OSDN Git Service

* config/sparc/sparc.md: Remove all traces of TARGET_LIVE_G0.
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.c
1 /* CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4    Contributed by Per Bothner, 1994-95.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "hashtab.h"
28 #include "intl.h"
29 #include "mkdeps.h"
30
31 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
32
33 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
34 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
35 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
36 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
37 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
38    (Note that it is false while we're expanding macro *arguments*.) */
39 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
40
41 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
42    case CPP_BUMP_LINE must not be called.  */
43 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
44
45 /* `struct directive' defines one #-directive, including how to handle it.  */
46
47 struct directive
48 {
49   int length;                   /* Length of name */
50   int (*func)                   /* Function to handle directive */
51     PARAMS ((cpp_reader *, const struct directive *));
52   const char *name;             /* Name of directive */
53   enum node_type type;          /* Code which describes which directive.  */
54 };
55
56 /* Stack of conditionals currently in progress
57    (including both successful and failing conditionals).  */
58
59 struct if_stack
60 {
61   struct if_stack *next;
62   int lineno;                   /* line number where condition started */
63   int if_succeeded;             /* truth of last condition in this group */
64   const U_CHAR *control_macro;  /* macro name for #ifndef around entire file */
65   enum node_type type;          /* type of last directive seen in this group */
66 };
67 typedef struct if_stack IF_STACK;
68
69
70 /* These functions are declared to return int instead of void since they
71    are going to be placed in a table and some old compilers have trouble with
72    pointers to functions returning void.  */
73
74 static int do_define PARAMS ((cpp_reader *, const struct directive *));
75 static int do_line PARAMS ((cpp_reader *, const struct directive *));
76 static int do_include PARAMS ((cpp_reader *, const struct directive *));
77 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
78 static int do_error PARAMS ((cpp_reader *, const struct directive *));
79 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
80 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
81 static int do_if PARAMS ((cpp_reader *, const struct directive *));
82 static int do_ifdef PARAMS ((cpp_reader *, const struct directive *));
83 static int do_else PARAMS ((cpp_reader *, const struct directive *));
84 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
85 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
86 #ifdef SCCS_DIRECTIVE
87 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
88 #endif
89 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
90 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
91 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
92
93 /* Forward declarations.  */
94
95 static void validate_else               PARAMS ((cpp_reader *, const char *));
96 static int eval_if_expr                 PARAMS ((cpp_reader *));
97 static void conditional_skip            PARAMS ((cpp_reader *, int,
98                                                 enum node_type, U_CHAR *));
99 static void skip_if_group               PARAMS ((cpp_reader *));
100 static void parse_name                  PARAMS ((cpp_reader *, int));
101 static void parse_string                PARAMS ((cpp_reader *, int));
102 static int parse_assertion              PARAMS ((cpp_reader *));
103 static const char *if_directive_name    PARAMS ((cpp_reader *,
104                                                  struct if_stack *));
105 static int null_cleanup                 PARAMS ((cpp_buffer *, cpp_reader *));
106 static int skip_comment                 PARAMS ((cpp_reader *, int));
107 static int copy_comment                 PARAMS ((cpp_reader *, int));
108 static void skip_string                 PARAMS ((cpp_reader *, int));
109 static void skip_rest_of_line           PARAMS ((cpp_reader *));
110 static void cpp_skip_hspace             PARAMS ((cpp_reader *));
111 static int handle_directive             PARAMS ((cpp_reader *));
112 static void pass_thru_directive         PARAMS ((const U_CHAR *, size_t,
113                                                  cpp_reader *,
114                                                  const struct directive *));
115 static int read_line_number             PARAMS ((cpp_reader *, int *));
116 static U_CHAR *detect_if_not_defined    PARAMS ((cpp_reader *));
117 static int consider_directive_while_skipping
118                                         PARAMS ((cpp_reader *, IF_STACK *));
119 static void skip_block_comment          PARAMS ((cpp_reader *));
120 static void skip_line_comment           PARAMS ((cpp_reader *));
121 static void parse_set_mark              PARAMS ((cpp_reader *));
122 static void parse_goto_mark             PARAMS ((cpp_reader *));
123 static int get_macro_name               PARAMS ((cpp_reader *));
124
125 /* Here is the actual list of #-directives.
126    This table is ordered by frequency of occurrence; the numbers
127    at the end are directive counts from all the source code I have
128    lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
129    linux-2.2.9, and pcmcia-cs-3.0.9).  */
130
131 static const struct directive directive_table[] = {
132   /* In C89 */
133   {  6, do_define,   "define",       T_DEFINE },        /* 270554 */
134   {  7, do_include,  "include",      T_INCLUDE },       /*  52262 */
135   {  5, do_endif,    "endif",        T_ENDIF },         /*  45855 */
136   {  5, do_ifdef,   "ifdef",        T_IFDEF },          /*  22000 */
137   {  2, do_if,       "if",           T_IF },            /*  18162 */
138   {  4, do_else,     "else",         T_ELSE },          /*   9863 */
139   {  6, do_ifdef,   "ifndef",       T_IFNDEF }, /*   9675 */
140   {  5, do_undef,    "undef",        T_UNDEF },         /*   4837 */
141   {  4, do_line,     "line",         T_LINE },          /*   2465 */
142   {  4, do_elif,     "elif",         T_ELIF },          /*    610 */
143   {  5, do_error,    "error",        T_ERROR },         /*    475 */
144   {  6, do_pragma,   "pragma",       T_PRAGMA },        /*    195 */
145
146   /* Extensions.  All deprecated except #warning and #include_next.  */
147   {  7, do_warning,  "warning",      T_WARNING },       /*     22 - GNU   */
148   { 12, do_include,  "include_next", T_INCLUDE_NEXT },  /*     19 - GNU   */
149   {  5, do_ident,    "ident",        T_IDENT },         /*     11 - SVR4  */
150   {  6, do_include,  "import",       T_IMPORT },        /*      0 - ObjC  */
151   {  6, do_assert,   "assert",       T_ASSERT },        /*      0 - SVR4  */
152   {  8, do_unassert, "unassert",     T_UNASSERT },      /*      0 - SVR4  */
153 #ifdef SCCS_DIRECTIVE
154   {  4, do_sccs,     "sccs",         T_SCCS },          /*      0 - SVR2? */
155 #endif
156   {  -1, 0, "", T_UNUSED }
157 };
158
159 /* Place into PFILE a quoted string representing the string SRC.
160    Caller must reserve enough space in pfile->token_buffer.  */
161
162 void
163 quote_string (pfile, src)
164      cpp_reader *pfile;
165      const char *src;
166 {
167   U_CHAR c;
168
169   CPP_PUTC_Q (pfile, '\"');
170   for (;;)
171     switch ((c = *src++))
172       {
173       default:
174         if (ISPRINT (c))
175           CPP_PUTC_Q (pfile, c);
176         else
177           {
178             sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
179             CPP_ADJUST_WRITTEN (pfile, 4);
180           }
181         break;
182
183       case '\"':
184       case '\\':
185         CPP_PUTC_Q (pfile, '\\');
186         CPP_PUTC_Q (pfile, c);
187         break;
188       
189       case '\0':
190         CPP_PUTC_Q (pfile, '\"');
191         CPP_NUL_TERMINATE_Q (pfile);
192         return;
193       }
194 }
195
196 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars.  */
197
198 void
199 cpp_grow_buffer (pfile, n)
200      cpp_reader *pfile;
201      long n;
202 {
203   long old_written = CPP_WRITTEN (pfile);
204   pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
205   pfile->token_buffer = (U_CHAR *)
206     xrealloc(pfile->token_buffer, pfile->token_buffer_size);
207   CPP_SET_WRITTEN (pfile, old_written);
208 }
209
210 /* Process the string STR as if it appeared as the body of a #define.
211    If STR is just an identifier, define it with value 1.
212    If STR has anything after the identifier, then it should
213    be identifier=definition. */
214
215 void
216 cpp_define (pfile, str)
217      cpp_reader *pfile;
218      const char *str;
219 {
220   char *buf, *p;
221   size_t count;
222
223   p = strchr (str, '=');
224   /* Copy the entire option so we can modify it. 
225      Change the first "=" in the string to a space.  If there is none,
226      tack " 1" on the end.  Then add a newline and a NUL.  */
227   
228   if (p)
229     {
230       count = strlen (str) + 2;
231       buf = alloca (count);
232       memcpy (buf, str, count - 2);
233       buf[p - str] = ' ';
234       buf[count - 2] = '\n';
235       buf[count - 1] = '\0';
236     }
237   else
238     {
239       count = strlen (str) + 4;
240       buf = alloca (count);
241       memcpy (buf, str, count - 4);
242       strcpy (&buf[count-4], " 1\n");
243     }
244
245   if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
246     {
247       do_define (pfile, NULL);
248       cpp_pop_buffer (pfile);
249     }
250 }
251
252 /* Process the string STR as if it appeared as the body of a #assert. */
253 void
254 cpp_assert (pfile, str)
255      cpp_reader *pfile;
256      const char *str;
257 {
258   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
259     {
260       do_assert (pfile, NULL);
261       cpp_pop_buffer (pfile);
262     }
263 }
264
265 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
266 int
267 cpp_defined (pfile, id, len)
268      cpp_reader *pfile;
269      const U_CHAR *id;
270      int len;
271 {
272   HASHNODE *hp = _cpp_lookup (pfile, id, len);
273   if (hp && hp->type == T_POISON)
274     {
275       cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
276       return 0;
277     }
278   return (hp != NULL);
279 }
280
281 static int
282 null_cleanup (pbuf, pfile)
283      cpp_buffer *pbuf ATTRIBUTE_UNUSED;
284      cpp_reader *pfile ATTRIBUTE_UNUSED;
285 {
286   return 0;
287 }
288
289 /* Skip a C-style block comment.  We know it's a comment, and point is
290    at the second character of the starter.  */
291 static void
292 skip_block_comment (pfile)
293      cpp_reader *pfile;
294 {
295   int c, prev_c = -1;
296   long line, col;
297
298   FORWARD(1);
299   cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
300   for (;;)
301     {
302       c = GETC ();
303       if (c == EOF)
304         {
305           cpp_error_with_line (pfile, line, col, "unterminated comment");
306           return;
307         }
308       else if (c == '\n' || c == '\r')
309         {
310           /* \r cannot be a macro escape marker here. */
311           if (!ACTIVE_MARK_P())
312             CPP_BUMP_LINE (pfile);
313         }
314       else if (c == '/' && prev_c == '*')
315         return;
316       else if (c == '*' && prev_c == '/'
317                && CPP_OPTIONS (pfile)->warn_comments)
318         cpp_warning (pfile, "`/*' within comment");
319
320       prev_c = c;
321     }
322 }
323
324 /* Skip a C++/Chill line comment.  We know it's a comment, and point
325    is at the second character of the initiator.  */
326 static void
327 skip_line_comment (pfile)
328      cpp_reader *pfile;
329 {
330   FORWARD(1);
331   for (;;)
332     {
333       int c = GETC ();
334
335       /* We don't have to worry about EOF in here.  */
336       if (c == '\n')
337         {
338           /* Don't consider final '\n' to be part of comment.  */
339           FORWARD(-1);
340           return;
341         }
342       else if (c == '\r')
343         {
344           /* \r cannot be a macro escape marker here. */
345           if (!ACTIVE_MARK_P())
346             CPP_BUMP_LINE (pfile);
347           if (CPP_OPTIONS (pfile)->warn_comments)
348             cpp_warning (pfile, "backslash-newline within line comment");
349         }
350     }
351 }
352
353 /* Skip a comment - C, C++, or Chill style.  M is the first character
354    of the comment marker.  If this really is a comment, skip to its
355    end and return ' '.  If this is not a comment, return M (which will
356    be '/' or '-').  */
357
358 static int
359 skip_comment (pfile, m)
360      cpp_reader *pfile;
361      int m;
362 {
363   if (m == '/' && PEEKC() == '*')
364     {
365       skip_block_comment (pfile);
366       return ' ';
367     }
368   else if (m == '/' && PEEKC() == '/')
369     {
370       if (CPP_BUFFER (pfile)->system_header_p)
371         {
372           /* We silently allow C++ comments in system headers, irrespective
373              of conformance mode, because lots of busted systems do that
374              and trying to clean it up in fixincludes is a nightmare.  */
375           skip_line_comment (pfile);
376           return ' ';
377         }
378       else if (CPP_OPTIONS (pfile)->cplusplus_comments)
379         {
380           if (CPP_OPTIONS (pfile)->c89
381               && CPP_PEDANTIC (pfile)
382               && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
383             {
384               cpp_pedwarn (pfile,
385                            "C++ style comments are not allowed in ISO C89");
386               cpp_pedwarn (pfile,
387                            "(this will be reported only once per input file)");
388               CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
389             }
390           skip_line_comment (pfile);
391           return ' ';
392         }
393       else
394         return m;
395     }
396   else if (m == '-' && PEEKC() == '-'
397            && CPP_OPTIONS (pfile)->chill)
398     {
399       skip_line_comment (pfile);
400       return ' ';
401     }
402   else
403     return m;
404 }
405
406 /* Identical to skip_comment except that it copies the comment into the
407    token_buffer.  This is used if !discard_comments.  */
408 static int
409 copy_comment (pfile, m)
410      cpp_reader *pfile;
411      int m;
412 {
413   const U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
414   const U_CHAR *limit;
415
416   if (skip_comment (pfile, m) == m)
417     return m;
418
419   limit = CPP_BUFFER (pfile)->cur;
420   CPP_RESERVE (pfile, limit - start + 2);
421   CPP_PUTC_Q (pfile, m);
422   for (; start <= limit; start++)
423     if (*start != '\r')
424       CPP_PUTC_Q (pfile, *start);
425
426   return ' ';
427 }
428
429 /* Skip whitespace \-newline and comments.  Does not macro-expand.  */
430
431 static void
432 cpp_skip_hspace (pfile)
433      cpp_reader *pfile;
434 {
435   int c;
436   while (1)
437     {
438       c = GETC();
439       if (c == EOF)
440         return;
441       else if (is_hspace(c))
442         {
443           if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
444             cpp_pedwarn (pfile, "%s in preprocessing directive",
445                          c == '\f' ? "formfeed" : "vertical tab");
446         }
447       else if (c == '\r')
448         {
449           /* \r is a backslash-newline marker if !has_escapes, and
450              a deletable-whitespace or no-reexpansion marker otherwise. */
451           if (CPP_BUFFER (pfile)->has_escapes)
452             {
453               if (PEEKC() == ' ')
454                 FORWARD(1);
455               else
456                 break;
457             }
458           else
459             CPP_BUMP_LINE (pfile);
460         }
461       else if (c == '/' || c == '-')
462         {
463           c = skip_comment (pfile, c);
464           if (c  != ' ')
465             break;
466         }
467       else
468         break;
469     }
470   FORWARD(-1);
471 }
472
473 /* Read and discard the rest of the current line.  */
474
475 static void
476 skip_rest_of_line (pfile)
477      cpp_reader *pfile;
478 {
479   for (;;)
480     {
481       int c = GETC();
482       switch (c)
483         {
484         case '\n':
485           FORWARD(-1);
486         case EOF:
487           return;
488
489         case '\r':
490           if (! CPP_BUFFER (pfile)->has_escapes)
491             CPP_BUMP_LINE (pfile);
492           break;
493           
494         case '\'':
495         case '\"':
496           skip_string (pfile, c);
497           break;
498
499         case '/':
500         case '-':
501           skip_comment (pfile, c);
502           break;
503
504         case '\f':
505         case '\v':
506           if (CPP_PEDANTIC (pfile))
507             cpp_pedwarn (pfile, "%s in preprocessing directive",
508                          c == '\f' ? "formfeed" : "vertical tab");
509           break;
510
511         }
512     }
513 }
514
515 /* Handle a possible # directive.
516    '#' has already been read.  */
517
518 static int
519 handle_directive (pfile)
520      cpp_reader *pfile;
521 {
522   int c;
523   register const struct directive *kt;
524   int ident_length;
525   U_CHAR *ident;
526   long old_written = CPP_WRITTEN (pfile);
527
528   if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
529     {
530       cpp_ice (pfile, "handle_directive called on macro buffer");
531       return 0;
532     }
533
534   cpp_skip_hspace (pfile);
535
536   c = PEEKC ();
537   /* # followed by a number is equivalent to #line.  Do not recognize
538      this form in assembly language source files.  Complain about this
539      form if we're being pedantic, but not if this is regurgitated
540      input (preprocessed or fed back in by the C++ frontend).  */
541   if (c >= '0' && c <= '9')
542     {
543       if (CPP_OPTIONS (pfile)->lang_asm)
544         return 0;
545
546       if (CPP_PEDANTIC (pfile)
547           && ! CPP_OPTIONS (pfile)->preprocessed
548           && ! CPP_BUFFER (pfile)->manual_pop)
549         cpp_pedwarn (pfile, "`#' followed by integer");
550       do_line (pfile, NULL);
551       return 1;
552     }
553
554   /* If we are rescanning preprocessed input, don't obey any directives
555      other than # nnn.  */
556   if (CPP_OPTIONS (pfile)->preprocessed)
557     return 0;
558
559   /* Now find the directive name.  */
560   CPP_PUTC (pfile, '#');
561   parse_name (pfile, GETC());
562   ident = pfile->token_buffer + old_written + 1;
563   ident_length = CPP_PWRITTEN (pfile) - ident;
564   if (ident_length == 0)
565     {
566       /* A line of just `#' becomes blank.  A line with something
567          other than an identifier after the # is reparsed as a non-
568          directive line.  */
569       CPP_SET_WRITTEN (pfile, old_written);
570       return (PEEKC() == '\n');
571     }
572
573   /* Decode the keyword and call the appropriate expansion routine.  */
574   for (kt = directive_table; ; kt++)
575     {
576       if (kt->length <= 0)
577         /* # identifier, but not a legit directive.  Pass onward as a
578            CPP_DIRECTIVE token anyway - let the consumer worry about it.  */
579         return 1;
580       if (kt->length == ident_length
581           && !strncmp (kt->name, ident, ident_length)) 
582         break;
583     }
584
585   CPP_SET_WRITTEN (pfile, old_written);
586
587   if (pfile->no_directives)
588     {
589       cpp_error (pfile, "`#%s' may not be used inside a macro argument",
590                  kt->name);
591       skip_rest_of_line (pfile);
592     }
593   else
594     (*kt->func) (pfile, kt);
595
596   return 1;
597 }
598
599 /* Pass a directive through to the output file.
600    BUF points to the contents of the directive, as a contiguous string.
601    LEN is the length of the string pointed to by BUF.
602    KEYWORD is the keyword-table entry for the directive.  */
603
604 static void
605 pass_thru_directive (buf, len, pfile, keyword)
606      const U_CHAR *buf;
607      size_t len;
608      cpp_reader *pfile;
609      const struct directive *keyword;
610 {
611   register unsigned keyword_length = keyword->length;
612
613   CPP_RESERVE (pfile, 1 + keyword_length + len);
614   CPP_PUTC_Q (pfile, '#');
615   CPP_PUTS_Q (pfile, keyword->name, keyword_length);
616   if (len != 0 && buf[0] != ' ')
617     CPP_PUTC_Q (pfile, ' ');
618   CPP_PUTS_Q (pfile, buf, len);
619 }
620
621 /* Subroutine of do_define: determine the name of the macro to be
622    defined.  */
623
624 static int
625 get_macro_name (pfile)
626      cpp_reader *pfile;
627 {
628   long here, len;
629
630   here = CPP_WRITTEN (pfile);
631   pfile->no_macro_expand++;
632   if (get_directive_token (pfile) != CPP_NAME)
633     {
634       cpp_error (pfile, "`#define' must be followed by an identifier");
635       goto invalid;
636     }
637
638   len = CPP_WRITTEN (pfile) - here;
639   if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
640     {
641       cpp_error (pfile, "`defined' is not a legal macro name");
642       goto invalid;
643     }
644
645   pfile->no_macro_expand--;
646   return len;
647
648  invalid:
649   skip_rest_of_line (pfile);
650   pfile->no_macro_expand--;
651   return 0;
652 }
653
654 /* Process a #define command.
655    KEYWORD is the keyword-table entry for #define,
656    or NULL for a "predefined" macro.  */
657
658 static int
659 do_define (pfile, keyword)
660      cpp_reader *pfile;
661      const struct directive *keyword ATTRIBUTE_UNUSED;
662 {
663   HASHNODE **slot;
664   DEFINITION *def;
665   long here;
666   unsigned long hash;
667   int len, c;
668   int funlike = 0;
669   U_CHAR *sym;
670
671   here = CPP_WRITTEN (pfile);
672   len = get_macro_name (pfile);
673   if (len == 0)
674     return 0;
675
676   /* Copy out the name so we can pop the token buffer.  */
677   len = CPP_WRITTEN (pfile) - here;
678   sym = (U_CHAR *) alloca (len + 1);
679   memcpy (sym, pfile->token_buffer + here, len);
680   sym[len] = '\0';
681   CPP_SET_WRITTEN (pfile, here);
682
683   /* If the next character, with no intervening whitespace, is '(',
684      then this is a function-like macro.  */
685   c = PEEKC ();
686   if (c == '(')
687     funlike = 1;
688   else if (c != '\n' && !is_hspace (c))
689     /* Otherwise, C99 requires white space after the name.  We treat it
690        as an object-like macro if this happens, with a warning.  */
691     cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
692
693   def = _cpp_create_definition (pfile, funlike);
694   if (def == 0)
695     return 0;
696
697   slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
698   if (*slot)
699     {
700       int ok;
701       HASHNODE *hp = *slot;
702
703       /* Redefining a macro is ok if the definitions are the same.  */
704       if (hp->type == T_MACRO)
705         ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
706       /* Redefining a constant is ok with -D.  */
707       else if (hp->type == T_CONST || hp->type == T_STDC)
708         ok = ! CPP_OPTIONS (pfile)->done_initializing;
709       /* Otherwise it's not ok.  */
710       else
711         ok = 0;
712       /* Print the warning or error if it's not ok.  */
713       if (! ok)
714         {
715           if (hp->type == T_POISON)
716             cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
717           else
718             cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
719           if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
720             {
721               DEFINITION *d = hp->value.defn;
722               cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
723                         "this is the location of the previous definition");
724             }
725         }
726       if (hp->type != T_POISON)
727         {
728           /* Replace the old definition.  */
729           if (hp->type == T_MACRO)
730             _cpp_free_definition (hp->value.defn);
731           hp->type = T_MACRO;
732           hp->value.defn = def;
733         }
734     }
735   else
736     {
737       HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
738       hp->value.defn = def;
739       *slot = hp;
740     }
741
742   if (CPP_OPTIONS (pfile)->debug_output
743       || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
744     _cpp_dump_definition (pfile, sym, len, def);
745   else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
746     pass_thru_directive (sym, len, pfile, keyword);
747
748   return 0;
749 }
750
751
752 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
753    If BUFFER != NULL, then use the LENGTH characters in BUFFER
754    as the new input buffer.
755    Return the new buffer, or NULL on failure.  */
756
757 cpp_buffer *
758 cpp_push_buffer (pfile, buffer, length)
759      cpp_reader *pfile;
760      const U_CHAR *buffer;
761      long length;
762 {
763   cpp_buffer *buf = CPP_BUFFER (pfile);
764   cpp_buffer *new;
765   if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
766     {
767       cpp_fatal (pfile, "macro or `#include' recursion too deep");
768       return NULL;
769     }
770
771   new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
772
773   new->if_stack = pfile->if_stack;
774   new->cleanup = null_cleanup;
775   new->buf = new->cur = buffer;
776   new->alimit = new->rlimit = buffer + length;
777   new->prev = buf;
778   new->mark = -1;
779   new->line_base = NULL;
780
781   CPP_BUFFER (pfile) = new;
782   return new;
783 }
784
785 cpp_buffer *
786 cpp_pop_buffer (pfile)
787      cpp_reader *pfile;
788 {
789   cpp_buffer *buf = CPP_BUFFER (pfile);
790   if (ACTIVE_MARK_P())
791     cpp_ice (pfile, "mark active in cpp_pop_buffer");
792   (*buf->cleanup) (buf, pfile);
793   CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
794   free (buf);
795   pfile->buffer_stack_depth--;
796   return CPP_BUFFER (pfile);
797 }
798
799 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
800    Pop the buffer when done.  */
801
802 void
803 cpp_scan_buffer (pfile)
804      cpp_reader *pfile;
805 {
806   cpp_buffer *buffer = CPP_BUFFER (pfile);
807   enum cpp_token token;
808   if (CPP_OPTIONS (pfile)->no_output)
809     {
810       long old_written = CPP_WRITTEN (pfile);
811       /* In no-output mode, we can ignore everything but directives.  */
812       for (;;)
813         {
814           if (! pfile->only_seen_white)
815             skip_rest_of_line (pfile);
816           token = cpp_get_token (pfile);
817           if (token == CPP_EOF) /* Should not happen ...  */
818             break;
819           if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
820             {
821               if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
822                 cpp_pop_buffer (pfile);
823               break;
824             }
825         }
826       CPP_SET_WRITTEN (pfile, old_written);
827     }
828   else
829     {
830       for (;;)
831         {
832           token = cpp_get_token (pfile);
833           if (token == CPP_EOF) /* Should not happen ...  */
834             break;
835           if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
836             {
837               if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
838                 cpp_pop_buffer (pfile);
839               break;
840             }
841         }
842     }
843 }
844
845 /*
846  * Rescan a string (which may have escape marks) into pfile's buffer.
847  * Place the result in pfile->token_buffer.
848  *
849  * The input is copied before it is scanned, so it is safe to pass
850  * it something from the token_buffer that will get overwritten
851  * (because it follows CPP_WRITTEN).  This is used by do_include.
852  */
853
854 void
855 cpp_expand_to_buffer (pfile, buf, length)
856      cpp_reader *pfile;
857      const U_CHAR *buf;
858      int length;
859 {
860   register cpp_buffer *ip;
861   U_CHAR *buf1;
862   int save_no_output;
863
864   if (length < 0)
865     {
866       cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
867       return;
868     }
869
870   /* Set up the input on the input stack.  */
871
872   buf1 = (U_CHAR *) alloca (length + 1);
873   memcpy (buf1, buf, length);
874   buf1[length] = 0;
875
876   ip = cpp_push_buffer (pfile, buf1, length);
877   if (ip == NULL)
878     return;
879   ip->has_escapes = 1;
880
881   /* Scan the input, create the output.  */
882   save_no_output = CPP_OPTIONS (pfile)->no_output;
883   CPP_OPTIONS (pfile)->no_output = 0;
884   CPP_OPTIONS (pfile)->no_line_commands++;
885   cpp_scan_buffer (pfile);
886   CPP_OPTIONS (pfile)->no_line_commands--;
887   CPP_OPTIONS (pfile)->no_output = save_no_output;
888
889   CPP_NUL_TERMINATE (pfile);
890 }
891
892 void
893 cpp_buf_line_and_col (pbuf, linep, colp)
894      register cpp_buffer *pbuf;
895      long *linep, *colp;
896 {
897   if (pbuf)
898     {
899       *linep = pbuf->lineno;
900       if (colp)
901         *colp = pbuf->cur - pbuf->line_base;
902     }
903   else
904     {
905       *linep = 0;
906       if (colp)
907         *colp = 0;
908     }
909 }
910
911 /* Return the cpp_buffer that corresponds to a file (not a macro).  */
912
913 cpp_buffer *
914 cpp_file_buffer (pfile)
915      cpp_reader *pfile;
916 {
917   cpp_buffer *ip;
918
919   for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
920     if (ip->ihash != NULL)
921       return ip;
922   return NULL;
923 }
924
925 /*
926  * write out a #line command, for instance, after an #include file.
927  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
928  */
929
930 void
931 output_line_command (pfile, file_change)
932      cpp_reader *pfile;
933      enum file_change_code file_change;
934 {
935   long line;
936   cpp_buffer *ip;
937
938   if (CPP_OPTIONS (pfile)->no_line_commands
939       || CPP_OPTIONS (pfile)->no_output)
940     return;
941
942   ip = cpp_file_buffer (pfile);
943   cpp_buf_line_and_col (ip, &line, NULL);
944
945   /* If the current file has not changed, we omit the #line if it would
946      appear to be a no-op, and we output a few newlines instead
947      if we want to increase the line number by a small amount.
948      We cannot do this if pfile->lineno is zero, because that means we
949      haven't output any line commands yet.  (The very first line command
950      output is a `same_file' command.)  */
951   if (file_change == same_file && pfile->lineno != 0)
952     {
953       if (line == pfile->lineno)
954         return;
955
956       /* If the inherited line number is a little too small,
957          output some newlines instead of a #line command.  */
958       if (line > pfile->lineno && line < pfile->lineno + 8)
959         {
960           CPP_RESERVE (pfile, 20);
961           while (line > pfile->lineno)
962             {
963               CPP_PUTC_Q (pfile, '\n');
964               pfile->lineno++;
965             }
966           return;
967         }
968     }
969
970   CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
971   CPP_PUTS_Q (pfile, "# ", 2);
972
973   sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
974   CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
975
976   quote_string (pfile, ip->nominal_fname); 
977   if (file_change != same_file && file_change != rename_file)
978     {
979       CPP_PUTC_Q (pfile, ' ');
980       CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
981     }
982   /* Tell cc1 if following text comes from a system header file.  */
983   if (ip->system_header_p)
984     {
985       CPP_PUTC_Q (pfile, ' ');
986       CPP_PUTC_Q (pfile, '3');
987     }
988 #ifndef NO_IMPLICIT_EXTERN_C
989   /* Tell cc1plus if following text should be treated as C.  */
990   if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
991     {
992       CPP_PUTC_Q (pfile, ' ');
993       CPP_PUTC_Q (pfile, '4');
994     }
995 #endif
996   CPP_PUTC_Q (pfile, '\n');
997   pfile->lineno = line;
998 }
999
1000
1001 /* Like cpp_get_token, except that it does not read past end-of-line.
1002    Also, horizontal space is skipped, and macros are popped.  */
1003
1004 enum cpp_token
1005 get_directive_token (pfile)
1006      cpp_reader *pfile;
1007 {
1008   long old_written = CPP_WRITTEN (pfile);
1009   enum cpp_token token;
1010
1011   for (;;)
1012     {
1013       cpp_skip_hspace (pfile);
1014       if (PEEKC () == '\n')
1015         return CPP_VSPACE;
1016
1017       token = cpp_get_token (pfile);
1018       /* token could be hspace at the beginning of a macro.  */
1019       if (token == CPP_HSPACE || token == CPP_COMMENT)
1020         {
1021           CPP_SET_WRITTEN (pfile, old_written);
1022           continue;
1023         }
1024
1025       /* token cannot be vspace, it would have been caught above.  */
1026       if (token == CPP_VSPACE)
1027         {
1028           cpp_ice (pfile, "VSPACE in get_directive_token");
1029           return token;
1030         }
1031
1032       /* token cannot be POP unless the buffer is a macro buffer.  */
1033       if (token != CPP_POP)
1034         return token;
1035
1036       if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1037         {
1038           cpp_ice (pfile, "POP of file buffer in get_directive_token");
1039           return token;
1040         }
1041
1042       /* We must pop the buffer by hand, or else cpp_get_token might
1043          hand us white space or newline on the next invocation.  */
1044       cpp_pop_buffer (pfile);
1045     }
1046 }
1047 \f
1048 /* Handle #include and #import.
1049    This function expects to see "fname" or <fname> on the input.
1050
1051    The input is normally in part of the output_buffer following
1052    CPP_WRITTEN, and will get overwritten by output_line_command.
1053    I.e. in input file specification has been popped by handle_directive.
1054    This is safe.  */
1055
1056 static int
1057 do_include (pfile, keyword)
1058      cpp_reader *pfile;
1059      const struct directive *keyword;
1060 {
1061   int importing = (keyword->type == T_IMPORT);
1062   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1063   int angle_brackets = 0;       /* 0 for "...", 1 for <...> */
1064   int before;  /* included before? */
1065   long flen;
1066   unsigned char *ftok;
1067   cpp_buffer *fp;
1068
1069   enum cpp_token token;
1070
1071   /* Chain of dirs to search */
1072   IHASH *ihash;
1073   struct file_name_list *search_start;
1074   
1075   long old_written = CPP_WRITTEN (pfile);
1076
1077   int fd;
1078
1079   if (CPP_PEDANTIC (pfile))
1080     {
1081       if (importing)
1082         cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1083       if (skip_dirs)
1084         cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1085     }
1086
1087   if (importing && CPP_OPTIONS (pfile)->warn_import
1088       && !CPP_OPTIONS (pfile)->inhibit_warnings
1089       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1090     {
1091       pfile->import_warning = 1;
1092       cpp_warning (pfile,
1093            "#import is obsolete, use an #ifndef wrapper in the header file");
1094     }
1095
1096   pfile->parsing_include_directive++;
1097   token = get_directive_token (pfile);
1098   pfile->parsing_include_directive--;
1099
1100   if (token == CPP_STRING)
1101     {
1102       if (pfile->token_buffer[old_written] == '<')
1103         angle_brackets = 1;
1104     }
1105 #ifdef VMS
1106   else if (token == CPP_NAME)
1107     {
1108       /* Support '#include xyz' like VAX-C.  It is taken as
1109          '#include <xyz.h>' and generates a warning.  */
1110       cpp_warning (pfile,
1111                "`#include filename' is obsolete, use `#include <filename.h>'");
1112       angle_brackets = 1;
1113
1114       /* Append the missing `.h' to the name. */
1115       CPP_PUTS (pfile, ".h", 2);
1116     }
1117 #endif
1118   else
1119     {
1120       cpp_error (pfile,
1121                  "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1122       CPP_SET_WRITTEN (pfile, old_written);
1123       skip_rest_of_line (pfile);
1124       return 0;
1125     }
1126
1127   flen = CPP_WRITTEN (pfile) - old_written;
1128   ftok = (unsigned char *) alloca (flen + 1);
1129   memcpy (ftok, pfile->token_buffer + old_written, flen);
1130   ftok[flen] = '\0';
1131
1132   if (get_directive_token (pfile) != CPP_VSPACE)
1133     {
1134       cpp_error (pfile, "junk at end of `#include'");
1135       skip_rest_of_line (pfile);
1136     }
1137
1138   CPP_SET_WRITTEN (pfile, old_written);
1139
1140   if (flen == 0)
1141     {
1142       cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1143       return 0;
1144     }
1145
1146   if (CPP_OPTIONS (pfile)->dump_includes)
1147     pass_thru_directive (ftok,
1148                          flen
1149 #ifdef VMS
1150           - ((token == CPP_NAME) ? 2 : 0)
1151 #endif
1152                          , pfile, keyword);
1153
1154 #ifdef VMS
1155   if (token == CPP_STRING)
1156 #endif
1157     {
1158       ftok++;
1159       flen -= 2;
1160       ftok[flen] = '\0';
1161     }
1162
1163   search_start = 0;
1164
1165   fp = cpp_file_buffer (pfile);
1166
1167   /* For #include_next, skip in the search path past the dir in which the
1168      containing file was found.  Treat files specified using an absolute path
1169      as if there are no more directories to search.  Treat the primary source
1170      file like any other included source, but generate a warning.  */
1171   if (skip_dirs && CPP_PREV_BUFFER (fp))
1172     {
1173       if (fp->ihash->foundhere != ABSOLUTE_PATH)
1174         search_start = fp->ihash->foundhere->next;
1175     }
1176   else
1177     {
1178       if (skip_dirs)
1179         cpp_warning (pfile, "#include_next in primary source file");
1180       
1181       if (angle_brackets)
1182         search_start = CPP_OPTIONS (pfile)->bracket_include;
1183       else
1184         {
1185           if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1186             {
1187               if (fp)
1188                 search_start = fp->actual_dir;
1189             }
1190           else
1191             search_start = CPP_OPTIONS (pfile)->quote_include;
1192         }
1193     }
1194
1195   if (!search_start)
1196     {
1197       cpp_error (pfile, "No include path in which to find %s", ftok);
1198       return 0;
1199     }
1200
1201   fd = _cpp_find_include_file (pfile, ftok, search_start, &ihash, &before);
1202
1203   if (fd == -2)
1204     return 0;
1205   
1206   if (fd == -1)
1207     {
1208       if (CPP_OPTIONS (pfile)->print_deps_missing_files
1209           && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1210                                        (pfile->system_include_depth > 0)))
1211         {
1212           if (!angle_brackets)
1213             deps_add_dep (pfile->deps, ftok);
1214           else
1215             {
1216               char *p;
1217               struct file_name_list *ptr;
1218               /* If requested as a system header, assume it belongs in
1219                  the first system header directory. */
1220               if (CPP_OPTIONS (pfile)->bracket_include)
1221                 ptr = CPP_OPTIONS (pfile)->bracket_include;
1222               else
1223                 ptr = CPP_OPTIONS (pfile)->quote_include;
1224
1225               p = (char *) alloca (strlen (ptr->name)
1226                                    + strlen (ftok) + 2);
1227               if (*ptr->name != '\0')
1228                 {
1229                   strcpy (p, ptr->name);
1230                   strcat (p, "/");
1231                 }
1232               strcat (p, ftok);
1233               deps_add_dep (pfile->deps, p);
1234             }
1235         }
1236       /* If -M was specified, and this header file won't be added to
1237          the dependency list, then don't count this as an error,
1238          because we can still produce correct output.  Otherwise, we
1239          can't produce correct output, because there may be
1240          dependencies we need inside the missing file, and we don't
1241          know what directory this missing file exists in. */
1242       else if (CPP_PRINT_DEPS (pfile)
1243                && (CPP_PRINT_DEPS (pfile)
1244                    <= (angle_brackets || (pfile->system_include_depth > 0))))
1245         cpp_warning (pfile, "No include path in which to find %s", ftok);
1246       else
1247         cpp_error_from_errno (pfile, ftok);
1248
1249       return 0;
1250     }
1251
1252   /* For -M, add the file to the dependencies on its first inclusion. */
1253   if (!before && (CPP_PRINT_DEPS (pfile)
1254                   > (angle_brackets || (pfile->system_include_depth > 0))))
1255     deps_add_dep (pfile->deps, ihash->name);
1256
1257   /* Handle -H option.  */
1258   if (CPP_OPTIONS(pfile)->print_include_names)
1259     {
1260       fp = CPP_BUFFER (pfile);
1261       while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
1262         putc ('.', stderr);
1263       fprintf (stderr, " %s\n", ihash->name);
1264     }
1265
1266   /* Actually process the file */
1267
1268   if (importing)
1269     ihash->control_macro = (const U_CHAR *) "";
1270   
1271   if (_cpp_read_include_file (pfile, fd, ihash))
1272     {
1273       output_line_command (pfile, enter_file);
1274       if (angle_brackets)
1275         pfile->system_include_depth++;   /* Decremented in file_cleanup. */
1276     }
1277   return 0;
1278 }
1279
1280 /* Subroutine of do_line.  Read next token from PFILE without adding it to
1281    the output buffer.  If it is a number between 1 and 4, store it in *NUM
1282    and return 1; otherwise, return 0 and complain if we aren't at the end
1283    of the directive.  */
1284
1285 static int
1286 read_line_number (pfile, num)
1287      cpp_reader *pfile;
1288      int *num;
1289 {
1290   long save_written = CPP_WRITTEN (pfile);
1291   U_CHAR *p = pfile->token_buffer + save_written;
1292   enum cpp_token token = get_directive_token (pfile);
1293   CPP_SET_WRITTEN (pfile, save_written);
1294
1295   if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1296     {
1297       *num = p[0] - '0';
1298       return 1;
1299     }
1300   else
1301     {
1302       if (token != CPP_VSPACE && token != CPP_EOF)
1303         cpp_error (pfile, "invalid format `#line' command");
1304       return 0;
1305     }
1306 }
1307
1308 /* Interpret #line command.
1309    Note that the filename string (if any) is treated as if it were an
1310    include filename.  That means no escape handling.  */
1311
1312 static int
1313 do_line (pfile, keyword)
1314      cpp_reader *pfile;
1315      const struct directive *keyword ATTRIBUTE_UNUSED;
1316 {
1317   cpp_buffer *ip = CPP_BUFFER (pfile);
1318   int new_lineno;
1319   long old_written = CPP_WRITTEN (pfile);
1320   enum file_change_code file_change = same_file;
1321   enum cpp_token token;
1322   char *x;
1323
1324   token = get_directive_token (pfile);
1325
1326   if (token != CPP_NUMBER)
1327     {
1328       cpp_error (pfile, "token after `#line' is not an integer");
1329       goto bad_line_directive;
1330     }
1331
1332   new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1333   if (x[0] != '\0')
1334     {
1335       cpp_error (pfile, "token after `#line' is not an integer");
1336       goto bad_line_directive;
1337     }      
1338   CPP_SET_WRITTEN (pfile, old_written);
1339
1340   if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1341     cpp_pedwarn (pfile, "line number out of range in `#line' command");
1342
1343   token = get_directive_token (pfile);
1344
1345   if (token == CPP_STRING)
1346     {
1347       U_CHAR *fname = pfile->token_buffer + old_written + 1;
1348       U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1349       int action_number = 0;
1350
1351       file_change = rename_file;
1352
1353       if (read_line_number (pfile, &action_number))
1354         {
1355           if (CPP_PEDANTIC (pfile))
1356             cpp_pedwarn (pfile, "garbage at end of `#line' command");
1357
1358           if (action_number == 1)
1359             {
1360               file_change = enter_file;
1361               read_line_number (pfile, &action_number);
1362             }
1363           else if (action_number == 2)
1364             {
1365               file_change = leave_file;
1366               read_line_number (pfile, &action_number);
1367             }
1368           if (action_number == 3)
1369             {
1370               ip->system_header_p = 1;
1371               read_line_number (pfile, &action_number);
1372             }
1373           if (action_number == 4)
1374             {
1375               ip->system_header_p = 2;
1376               read_line_number (pfile, &action_number);
1377             }
1378         }
1379       
1380       *end_name = '\0';
1381       
1382       if (strcmp (fname, ip->nominal_fname))
1383         {
1384           const char *newname, *oldname;
1385           if (!strcmp (fname, ip->ihash->name))
1386             newname = ip->ihash->name;
1387           else if (ip->last_nominal_fname
1388                    && !strcmp (fname, ip->last_nominal_fname))
1389             newname = ip->last_nominal_fname;
1390           else
1391             newname = xstrdup (fname);
1392
1393           oldname = ip->nominal_fname;
1394           ip->nominal_fname = newname;
1395
1396           if (ip->last_nominal_fname
1397               && ip->last_nominal_fname != oldname
1398               && ip->last_nominal_fname != newname
1399               && ip->last_nominal_fname != ip->ihash->name)
1400             free ((void *) ip->last_nominal_fname);
1401
1402           if (newname == ip->ihash->name)
1403             ip->last_nominal_fname = NULL;
1404           else
1405             ip->last_nominal_fname = oldname;
1406         } 
1407     }
1408   else if (token != CPP_VSPACE && token != CPP_EOF)
1409     {
1410       cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1411       goto bad_line_directive;
1412     }
1413
1414   /* The Newline at the end of this line remains to be processed.
1415      To put the next line at the specified line number,
1416      we must store a line number now that is one less.  */
1417   ip->lineno = new_lineno - 1;
1418   CPP_SET_WRITTEN (pfile, old_written);
1419   output_line_command (pfile, file_change);
1420   return 0;
1421
1422  bad_line_directive:
1423   skip_rest_of_line (pfile);
1424   CPP_SET_WRITTEN (pfile, old_written);
1425   return 0;
1426 }
1427
1428 /* Remove the definition of a symbol from the symbol table.
1429    According to the C standard, it is not an error to undef
1430    something that has no definitions. */
1431 static int
1432 do_undef (pfile, keyword)
1433      cpp_reader *pfile;
1434      const struct directive *keyword;
1435 {
1436   int len;
1437   HASHNODE **slot;
1438   U_CHAR *buf, *name, *limit;
1439   int c;
1440   long here = CPP_WRITTEN (pfile);
1441   enum cpp_token token;
1442
1443   cpp_skip_hspace (pfile);
1444   c = GETC();
1445   if (! is_idstart(c))
1446   {
1447       cpp_error (pfile, "token after #undef is not an identifier");
1448       skip_rest_of_line (pfile);
1449       return 1;
1450   }
1451
1452   parse_name (pfile, c);
1453   buf = pfile->token_buffer + here;
1454   limit = CPP_PWRITTEN(pfile);
1455
1456   /* Copy out the token so we can pop the token buffer. */
1457   len = limit - buf;
1458   name = (U_CHAR *) alloca (len + 1);
1459   memcpy (name, buf, len);
1460   name[len] = '\0';
1461
1462   token = get_directive_token (pfile);
1463   if (token != CPP_VSPACE)
1464   {
1465       cpp_pedwarn (pfile, "junk on line after #undef");
1466       skip_rest_of_line (pfile);
1467   }
1468   CPP_SET_WRITTEN (pfile, here);
1469
1470   slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
1471   if (slot)
1472     {
1473       HASHNODE *hp = *slot;
1474       /* If we are generating additional info for debugging (with -g) we
1475          need to pass through all effective #undef commands.  */
1476       if (CPP_OPTIONS (pfile)->debug_output && keyword)
1477         pass_thru_directive (name, len, pfile, keyword);
1478       if (hp->type == T_POISON)
1479         cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1480       else 
1481         {
1482           if (hp->type != T_MACRO)
1483             cpp_warning (pfile, "undefining `%s'", hp->name);
1484
1485           htab_clear_slot (pfile->hashtab, (void **)slot);
1486         }
1487     }
1488
1489   return 0;
1490 }
1491
1492 /* Wrap do_undef for -U processing. */
1493 void
1494 cpp_undef (pfile, macro)
1495      cpp_reader *pfile;
1496      const char *macro;
1497 {
1498   /* Copy the string so we can append a newline.  */
1499   size_t len = strlen (macro);
1500   char *buf = alloca (len + 2);
1501   memcpy (buf, macro, len);
1502   buf[len]     = '\n';
1503   buf[len + 1] = '\0';
1504   if (cpp_push_buffer (pfile, buf, len + 1))
1505     {
1506       do_undef (pfile, NULL);
1507       cpp_pop_buffer (pfile);
1508     }
1509 }
1510
1511 /*
1512  * Report an error detected by the program we are processing.
1513  * Use the text of the line in the error message.
1514  * (We use error because it prints the filename & line#.)
1515  */
1516
1517 static int
1518 do_error (pfile, keyword)
1519      cpp_reader *pfile;
1520      const struct directive *keyword ATTRIBUTE_UNUSED;
1521 {
1522   const U_CHAR *text, *limit;
1523
1524   cpp_skip_hspace (pfile);
1525   text = CPP_BUFFER (pfile)->cur;
1526   skip_rest_of_line (pfile);
1527   limit = CPP_BUFFER (pfile)->cur;
1528
1529   cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1530   return 0;
1531 }
1532
1533 /*
1534  * Report a warning detected by the program we are processing.
1535  * Use the text of the line in the warning message, then continue.
1536  */
1537
1538 static int
1539 do_warning (pfile, keyword)
1540      cpp_reader *pfile;
1541      const struct directive *keyword ATTRIBUTE_UNUSED;
1542 {
1543   const U_CHAR *text, *limit;
1544
1545   cpp_skip_hspace (pfile);
1546   text = CPP_BUFFER (pfile)->cur;
1547   skip_rest_of_line (pfile);
1548   limit = CPP_BUFFER (pfile)->cur;
1549
1550   if (CPP_PEDANTIC (pfile))
1551     cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1552
1553   cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1554   return 0;
1555 }
1556
1557 /* Report program identification.  */
1558
1559 static int
1560 do_ident (pfile, keyword)
1561      cpp_reader *pfile;
1562      const struct directive *keyword ATTRIBUTE_UNUSED;
1563 {
1564   long old_written = CPP_WRITTEN (pfile);
1565
1566   /* Allow #ident in system headers, since that's not user's fault.  */
1567   if (CPP_PEDANTIC (pfile))
1568     cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1569
1570   CPP_PUTS (pfile, "#ident ", 7);
1571
1572   /* Next token should be a string constant.  */
1573   if (get_directive_token (pfile) == CPP_STRING)
1574     /* And then a newline.  */
1575     if (get_directive_token (pfile) == CPP_VSPACE)
1576       /* Good - ship it.  */
1577       return 0;
1578
1579   cpp_error (pfile, "invalid #ident");
1580   skip_rest_of_line (pfile);
1581   CPP_SET_WRITTEN (pfile, old_written);  /* discard directive */
1582
1583   return 0;
1584 }
1585
1586 /* Pragmata handling.  We handle some of these, and pass the rest on
1587    to the front end.  C99 defines three pragmas and says that no macro
1588    expansion is to be performed on them; whether or not macro
1589    expansion happens for other pragmas is implementation defined.
1590    This implementation never macro-expands the text after #pragma.
1591
1592    We currently do not support the _Pragma operator.  Support for that
1593    has to be coordinated with the front end.  Proposed implementation:
1594    both #pragma blah blah and _Pragma("blah blah") become
1595    __builtin_pragma(blah blah) and we teach the parser about that.  */
1596
1597 /* Sub-handlers for the pragmas needing treatment here.
1598    They return 1 if the token buffer is to be popped, 0 if not. */
1599 static int do_pragma_once               PARAMS ((cpp_reader *));
1600 static int do_pragma_implementation     PARAMS ((cpp_reader *));
1601 static int do_pragma_poison             PARAMS ((cpp_reader *));
1602 static int do_pragma_default            PARAMS ((cpp_reader *));
1603
1604 static int
1605 do_pragma (pfile, keyword)
1606      cpp_reader *pfile;
1607      const struct directive *keyword ATTRIBUTE_UNUSED;
1608 {
1609   long here, key;
1610   U_CHAR *buf;
1611   int pop;
1612   enum cpp_token token;
1613
1614   here = CPP_WRITTEN (pfile);
1615   CPP_PUTS (pfile, "#pragma ", 8);
1616
1617   key = CPP_WRITTEN (pfile);
1618   pfile->no_macro_expand++;
1619   token = get_directive_token (pfile);
1620   if (token != CPP_NAME)
1621     {
1622       if (token == CPP_VSPACE)
1623         goto empty;
1624       else
1625         goto skip;
1626     }
1627
1628   buf = pfile->token_buffer + key;
1629   CPP_PUTC (pfile, ' ');
1630
1631 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
1632   if (tokis ("once"))
1633     pop = do_pragma_once (pfile);
1634   else if (tokis ("implementation"))
1635     pop = do_pragma_implementation (pfile);
1636   else if (tokis ("poison"))
1637     pop = do_pragma_poison (pfile);
1638   else
1639     pop = do_pragma_default (pfile);
1640 #undef tokis
1641
1642   if (get_directive_token (pfile) != CPP_VSPACE)
1643     goto skip;
1644
1645   if (pop)
1646     CPP_SET_WRITTEN (pfile, here);
1647   pfile->no_macro_expand--;
1648   return 0;
1649
1650  skip:
1651   cpp_error (pfile, "malformed #pragma directive");
1652   skip_rest_of_line (pfile);
1653  empty:
1654   CPP_SET_WRITTEN (pfile, here);
1655   pfile->no_macro_expand--;
1656   return 0;
1657 }
1658
1659 static int
1660 do_pragma_default (pfile)
1661      cpp_reader *pfile;
1662 {
1663   while (get_directive_token (pfile) != CPP_VSPACE)
1664     CPP_PUTC (pfile, ' ');
1665   return 0;
1666 }
1667
1668 static int
1669 do_pragma_once (pfile)
1670      cpp_reader *pfile;
1671 {
1672   cpp_buffer *ip = CPP_BUFFER (pfile);
1673
1674   /* Allow #pragma once in system headers, since that's not the user's
1675      fault.  */
1676   if (!ip->system_header_p)
1677     cpp_warning (pfile, "`#pragma once' is obsolete");
1678       
1679   if (CPP_PREV_BUFFER (ip) == NULL)
1680     cpp_warning (pfile, "`#pragma once' outside include file");
1681   else
1682     ip->ihash->control_macro = (const U_CHAR *) "";  /* never repeat */
1683
1684   return 1;
1685 }
1686
1687 static int
1688 do_pragma_implementation (pfile)
1689      cpp_reader *pfile;
1690 {
1691   /* Be quiet about `#pragma implementation' for a file only if it hasn't
1692      been included yet.  */
1693   enum cpp_token token;
1694   long written = CPP_WRITTEN (pfile);
1695   U_CHAR *name;
1696   U_CHAR *copy;
1697   size_t len;
1698
1699   token = get_directive_token (pfile);
1700   if (token == CPP_VSPACE)
1701     return 0;
1702   else if (token != CPP_STRING)
1703     {
1704       cpp_error (pfile, "malformed #pragma implementation");
1705       return 1;
1706     }
1707
1708   /* Trim the leading and trailing quote marks from the string.  */
1709   name = pfile->token_buffer + written + 1;
1710   len = CPP_PWRITTEN (pfile) - name;
1711   copy = (U_CHAR *) alloca (len);
1712   memcpy (copy, name, len - 1);
1713   copy[len - 1] = '\0';
1714   
1715   if (cpp_included (pfile, copy))
1716     cpp_warning (pfile,
1717          "`#pragma implementation' for `%s' appears after file is included",
1718                  copy);
1719   return 0;
1720 }
1721
1722 static int
1723 do_pragma_poison (pfile)
1724      cpp_reader *pfile;
1725 {
1726   /* Poison these symbols so that all subsequent usage produces an
1727      error message.  */
1728   U_CHAR *p;
1729   HASHNODE **slot;
1730   long written;
1731   size_t len;
1732   enum cpp_token token;
1733   int writeit;
1734   unsigned long hash;
1735
1736   /* As a rule, don't include #pragma poison commands in output,  
1737      unless the user asks for them.  */
1738   writeit = (CPP_OPTIONS (pfile)->debug_output
1739              || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1740              || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1741
1742   for (;;)
1743     {
1744       written = CPP_WRITTEN (pfile);
1745       token = get_directive_token (pfile);
1746       if (token == CPP_VSPACE)
1747         break;
1748       if (token != CPP_NAME)
1749         {
1750           cpp_error (pfile, "invalid #pragma poison directive");
1751           skip_rest_of_line (pfile);
1752           return 1;
1753         }
1754
1755       p = pfile->token_buffer + written;
1756       len = strlen (p);
1757       slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1758       if (*slot)
1759         {
1760           HASHNODE *hp = *slot;
1761           if (hp->type != T_POISON)
1762             {
1763               cpp_warning (pfile, "poisoning existing macro `%s'", p);
1764               if (hp->type == T_MACRO)
1765                 _cpp_free_definition (hp->value.defn);
1766               hp->value.defn = 0;
1767               hp->type = T_POISON;
1768             }
1769         }
1770       else
1771         {
1772           HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1773           hp->value.cpval = 0;
1774           *slot = hp;
1775         }
1776       if (writeit)
1777         CPP_PUTC (pfile, ' ');
1778     }
1779   return !writeit;
1780 }
1781  
1782 #ifdef SCCS_DIRECTIVE
1783 /* Just ignore #sccs, on systems where we define it at all.  */
1784
1785 static int
1786 do_sccs (pfile, keyword)
1787      cpp_reader *pfile;
1788      const struct directive *keyword ATTRIBUTE_UNUSED;
1789 {
1790   if (CPP_PEDANTIC (pfile))
1791     cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1792   skip_rest_of_line (pfile);
1793   return 0;
1794 }
1795 #endif
1796
1797 /* We've found an `#if' directive.  If the only thing before it in
1798    this file is white space, and if it is of the form
1799    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1800    for inclusion of this file.  (See redundant_include_p in cppfiles.c
1801    for an explanation of controlling macros.)  If so, return a
1802    malloc'd copy of SYMBOL.  Otherwise, return NULL.  */
1803
1804 static U_CHAR *
1805 detect_if_not_defined (pfile)
1806      cpp_reader *pfile;
1807 {
1808   U_CHAR *control_macro = 0;
1809
1810   if (pfile->only_seen_white == 2)
1811     {
1812       U_CHAR *ident;
1813       enum cpp_token token;
1814       int base_offset;
1815       int token_offset;
1816       int need_rparen = 0;
1817
1818       /* Save state required for restore.  */
1819       pfile->no_macro_expand++;
1820       parse_set_mark (pfile);
1821       base_offset = CPP_WRITTEN (pfile);
1822
1823       /* Look for `!', */
1824       if (get_directive_token (pfile) != CPP_OTHER
1825           || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1826           || CPP_PWRITTEN (pfile)[-1] != '!')
1827         goto restore;
1828
1829       /* ...then `defined', */
1830       token_offset = CPP_WRITTEN (pfile);
1831       token = get_directive_token (pfile);
1832       if (token != CPP_NAME)
1833         goto restore;
1834       ident = pfile->token_buffer + token_offset;
1835       CPP_NUL_TERMINATE (pfile);
1836       if (strcmp (ident, "defined"))
1837         goto restore;
1838
1839       /* ...then an optional '(' and the name, */
1840       token_offset = CPP_WRITTEN (pfile);
1841       token = get_directive_token (pfile);
1842       if (token == CPP_LPAREN)
1843         {
1844           token_offset = CPP_WRITTEN (pfile);
1845           token = get_directive_token (pfile);
1846           if (token != CPP_NAME)
1847             goto restore;
1848           need_rparen = 1;
1849         }
1850       else if (token != CPP_NAME)
1851         goto restore;
1852
1853       ident = pfile->token_buffer + token_offset;
1854       CPP_NUL_TERMINATE (pfile);
1855
1856       /* ...then the ')', if necessary, */
1857       if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1858           /* ...and make sure there's nothing else on the line.  */
1859           && get_directive_token (pfile) == CPP_VSPACE)
1860         control_macro = (U_CHAR *) xstrdup (ident);
1861
1862     restore:
1863       CPP_SET_WRITTEN (pfile, base_offset);
1864       pfile->no_macro_expand--;
1865       parse_goto_mark (pfile);
1866     }
1867
1868   return control_macro;
1869 }
1870
1871 /*
1872  * #if is straightforward; just call eval_if_expr, then conditional_skip.
1873  * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1874  */
1875
1876 static int
1877 do_if (pfile, keyword)
1878      cpp_reader *pfile;
1879      const struct directive *keyword ATTRIBUTE_UNUSED;
1880 {
1881   U_CHAR *control_macro = detect_if_not_defined (pfile);
1882   int value = eval_if_expr (pfile);
1883   conditional_skip (pfile, value == 0, T_IF, control_macro);
1884   return 0;
1885 }
1886
1887 /*
1888  * handle a #elif directive by not changing  if_stack  either.
1889  * see the comment above do_else.
1890  */
1891
1892 static int
1893 do_elif (pfile, keyword)
1894      cpp_reader *pfile;
1895      const struct directive *keyword ATTRIBUTE_UNUSED;
1896 {
1897   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1898     {
1899       cpp_error (pfile, "`#elif' not within a conditional");
1900       return 0;
1901     }
1902   else
1903     {
1904       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1905         {
1906           cpp_error (pfile, "`#elif' after `#else'");
1907           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1908                                "the conditional began here");
1909         }
1910       pfile->if_stack->type = T_ELIF;
1911     }
1912
1913   if (pfile->if_stack->if_succeeded)
1914     skip_if_group (pfile);
1915   else
1916     {
1917       int value = eval_if_expr (pfile);
1918       if (value == 0)
1919         skip_if_group (pfile);
1920       else
1921         {
1922           ++pfile->if_stack->if_succeeded;      /* continue processing input */
1923           output_line_command (pfile, same_file);
1924         }
1925     }
1926   return 0;
1927 }
1928
1929 /* Thin wrapper around _cpp_parse_expr, which doesn't have access to
1930  * skip_rest_of_line.  Also centralizes toggling parsing_if_directive.
1931  */
1932
1933 static int
1934 eval_if_expr (pfile)
1935      cpp_reader *pfile;
1936 {
1937   int value;
1938   long old_written = CPP_WRITTEN (pfile);
1939
1940   pfile->parsing_if_directive++;
1941   value = _cpp_parse_expr (pfile);
1942   pfile->parsing_if_directive--;
1943
1944   skip_rest_of_line (pfile);
1945   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1946
1947   return value;
1948 }
1949
1950 /*
1951  * routine to handle ifdef/ifndef.  Try to look up the symbol,
1952  * then do or don't skip to the #endif/#else/#elif depending
1953  * on what directive is actually being processed.
1954  */
1955
1956 static int
1957 do_ifdef (pfile, keyword)
1958      cpp_reader *pfile;
1959      const struct directive *keyword;
1960 {
1961   int skip;
1962   U_CHAR *ident;
1963   int ident_length;
1964   enum cpp_token token;
1965   int start_of_file = 0;
1966   U_CHAR *control_macro = 0;
1967   int old_written = CPP_WRITTEN (pfile);
1968
1969   /* Detect a #ifndef at start of file (not counting comments).  */
1970   if (keyword->type == T_IFNDEF)
1971     start_of_file = pfile->only_seen_white == 2;
1972
1973   pfile->no_macro_expand++;
1974   token = get_directive_token (pfile);
1975   pfile->no_macro_expand--;
1976
1977   ident = pfile->token_buffer + old_written;
1978   ident_length = CPP_WRITTEN (pfile) - old_written;
1979   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1980
1981   if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1982     {
1983       skip = (keyword->type == T_IFDEF);
1984       if (! CPP_TRADITIONAL (pfile))
1985         cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1986     }
1987   else if (token == CPP_NAME)
1988     {
1989       skip = cpp_defined (pfile, ident, ident_length);
1990       if (keyword->type == T_IFDEF)
1991         skip = !skip;
1992
1993       if (start_of_file && !skip)
1994         {
1995           control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1996           memcpy (control_macro, ident, ident_length + 1);
1997         }
1998     }
1999   else
2000     {
2001       skip = (keyword->type == T_IFDEF);
2002       if (! CPP_TRADITIONAL (pfile))
2003         cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2004     }
2005
2006   if (!CPP_TRADITIONAL (pfile))
2007     { int c;
2008       cpp_skip_hspace (pfile);
2009       c = PEEKC ();
2010       if (c != EOF && c != '\n')
2011         cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2012     }
2013   skip_rest_of_line (pfile);
2014
2015   conditional_skip (pfile, skip, T_IF, control_macro);
2016   return 0;
2017 }
2018
2019 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2020    If this is a #ifndef starting at the beginning of a file,
2021    CONTROL_MACRO is the macro name tested by the #ifndef.
2022    Otherwise, CONTROL_MACRO is 0.  */
2023
2024 static void
2025 conditional_skip (pfile, skip, type, control_macro)
2026      cpp_reader *pfile;
2027      int skip;
2028      enum node_type type;
2029      U_CHAR *control_macro;
2030 {
2031   IF_STACK *temp;
2032
2033   temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
2034   temp->lineno = CPP_BUFFER (pfile)->lineno;
2035   temp->next = pfile->if_stack;
2036   temp->control_macro = control_macro;
2037   pfile->if_stack = temp;
2038
2039   pfile->if_stack->type = type;
2040
2041   if (skip != 0) {
2042     skip_if_group (pfile);
2043     return;
2044   } else {
2045     ++pfile->if_stack->if_succeeded;
2046     output_line_command (pfile, same_file);
2047   }
2048 }
2049
2050 /* Subroutine of skip_if_group.  Examine one preprocessing directive and
2051    return 0 if skipping should continue, 1 if it should halt.  Also
2052    adjusts the if_stack as appropriate.
2053    The `#' has been read, but not the identifier. */
2054
2055 static int
2056 consider_directive_while_skipping (pfile, stack)
2057     cpp_reader *pfile;
2058     IF_STACK *stack; 
2059 {
2060   long ident_len, ident;
2061   const struct directive *kt;
2062   IF_STACK *temp;
2063     
2064   cpp_skip_hspace (pfile);
2065
2066   ident = CPP_WRITTEN (pfile);
2067   parse_name (pfile, GETC());
2068   ident_len = CPP_WRITTEN (pfile) - ident;
2069
2070   CPP_SET_WRITTEN (pfile, ident);
2071
2072   for (kt = directive_table; kt->length >= 0; kt++)
2073     if (kt->length == ident_len
2074         && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2075       switch (kt->type)
2076         {
2077         case T_IF:
2078         case T_IFDEF:
2079         case T_IFNDEF:
2080             temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
2081             temp->next = pfile->if_stack;
2082             pfile->if_stack = temp;
2083             temp->type = kt->type;
2084             return 0;
2085
2086         case T_ELSE:
2087             if (pfile->if_stack != stack)
2088               validate_else (pfile, "#else");
2089             /* fall through */
2090         case T_ELIF:
2091             if (pfile->if_stack == stack)
2092               return 1;
2093             else
2094               {
2095                 pfile->if_stack->type = kt->type;
2096                 return 0;
2097               }
2098
2099             case T_ENDIF:
2100                 if (pfile->if_stack != stack)
2101                   validate_else (pfile, "#endif");
2102
2103                 if (pfile->if_stack == stack)
2104                   return 1;
2105                     
2106                 temp = pfile->if_stack;
2107                 pfile->if_stack = temp->next;
2108                 free (temp);
2109                 return 0;
2110
2111             default:
2112                 return 0;
2113             }
2114
2115     /* Don't let erroneous code go by.  */
2116     if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2117         cpp_pedwarn (pfile, "invalid preprocessor directive name");
2118     return 0;
2119 }
2120
2121 /* skip to #endif, #else, or #elif.  adjust line numbers, etc.
2122  * leaves input ptr at the sharp sign found.
2123  */
2124 static void
2125 skip_if_group (pfile)
2126     cpp_reader *pfile;
2127 {
2128   int c;
2129   IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
2130   const U_CHAR *beg_of_line;
2131   long old_written;
2132
2133   old_written = CPP_WRITTEN (pfile);
2134   
2135   for (;;)
2136     {
2137       beg_of_line = CPP_BUFFER (pfile)->cur;
2138
2139       if (! CPP_TRADITIONAL (pfile))
2140         cpp_skip_hspace (pfile);
2141       c = GETC();
2142       if (c == '\n')
2143         {
2144           CPP_BUMP_LINE (pfile);
2145           continue;
2146         }
2147       else if (c == '#')
2148         {
2149           if (consider_directive_while_skipping (pfile, save_if_stack))
2150             break;
2151         }
2152       else if (c == EOF)
2153         return;  /* Caller will issue error. */
2154
2155       FORWARD(-1);
2156       skip_rest_of_line (pfile);
2157
2158       c = GETC();
2159       if (c == EOF)
2160         return;  /* Caller will issue error. */
2161       else
2162         CPP_BUMP_LINE (pfile);
2163     }     
2164
2165   /* Back up to the beginning of this line.  Caller will process the
2166      directive. */
2167   CPP_BUFFER (pfile)->cur = beg_of_line;
2168   pfile->only_seen_white = 1;
2169 }
2170
2171 /*
2172  * handle a #else directive.  Do this by just continuing processing
2173  * without changing  if_stack ;  this is so that the error message
2174  * for missing #endif's etc. will point to the original #if.  It
2175  * is possible that something different would be better.
2176  */
2177
2178 static int
2179 do_else (pfile, keyword)
2180      cpp_reader *pfile;
2181      const struct directive *keyword ATTRIBUTE_UNUSED;
2182 {
2183   validate_else (pfile, "#else");
2184   skip_rest_of_line (pfile);
2185
2186   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2187     {
2188       cpp_error (pfile, "`#else' not within a conditional");
2189       return 0;
2190     }
2191   else
2192     {
2193       /* #ifndef can't have its special treatment for containing the whole file
2194          if it has a #else clause.  */
2195       pfile->if_stack->control_macro = 0;
2196
2197       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2198         {
2199           cpp_error (pfile, "`#else' after `#else'");
2200           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2201                                "the conditional began here");
2202         }
2203       pfile->if_stack->type = T_ELSE;
2204     }
2205
2206   if (pfile->if_stack->if_succeeded)
2207     skip_if_group (pfile);
2208   else
2209     {
2210       ++pfile->if_stack->if_succeeded;  /* continue processing input */
2211       output_line_command (pfile, same_file);
2212     }
2213   return 0;
2214 }
2215
2216 /*
2217  * unstack after #endif command
2218  */
2219
2220 static int
2221 do_endif (pfile, keyword)
2222      cpp_reader *pfile;
2223      const struct directive *keyword ATTRIBUTE_UNUSED;
2224 {
2225   validate_else (pfile, "#endif");
2226   skip_rest_of_line (pfile);
2227
2228   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2229     cpp_error (pfile, "`#endif' not within a conditional");
2230   else
2231     {
2232       IF_STACK *temp = pfile->if_stack;
2233       pfile->if_stack = temp->next;
2234       if (temp->control_macro != 0)
2235         {
2236           /* This #endif matched a #ifndef at the start of the file.
2237              See if it is at the end of the file.  */
2238           int c;
2239
2240           parse_set_mark (pfile);
2241
2242           for (;;)
2243             {
2244               cpp_skip_hspace (pfile);
2245               c = GETC ();
2246               if (c != '\n')
2247                 break;
2248             }
2249           parse_goto_mark (pfile);
2250
2251           if (c == EOF)
2252             {
2253               /* This #endif ends a #ifndef
2254                  that contains all of the file (aside from whitespace).
2255                  Arrange not to include the file again
2256                  if the macro that was tested is defined. */
2257               CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
2258             }
2259         }
2260       free (temp);
2261       output_line_command (pfile, same_file);
2262     }
2263   return 0;
2264 }
2265
2266 /* Issue -pedantic warning for text which is not a comment following
2267    an #else or #endif.  Do not warn in system headers, as this is harmless
2268    and very common on old systems.  */
2269
2270 static void
2271 validate_else (pfile, directive)
2272      cpp_reader *pfile;
2273      const char *directive;
2274 {
2275   if (! CPP_PEDANTIC (pfile))
2276     return;
2277
2278   cpp_skip_hspace (pfile);
2279   if (PEEKC () != '\n')
2280     cpp_pedwarn (pfile,
2281                  "text following `%s' violates ANSI standard", directive);
2282 }
2283
2284 /* Convert T_IF, etc. to a string.   Used in error messages.  */
2285 static const char *
2286 if_directive_name (pfile, ifs)
2287      cpp_reader *pfile;
2288      struct if_stack *ifs;
2289 {
2290   switch (ifs->type)
2291     {
2292     case T_IF:      return "#if";
2293     case T_IFDEF:   return "#ifdef";
2294     case T_IFNDEF:  return "#ifndef";
2295     case T_ELIF:    return "#elif";
2296     case T_ELSE:    return "#else";
2297     default:
2298       cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2299       return "unknown";
2300     }
2301 }
2302
2303 /* Get the next token, and add it to the text in pfile->token_buffer.
2304    Return the kind of token we got.  */
2305
2306 enum cpp_token
2307 cpp_get_token (pfile)
2308      cpp_reader *pfile;
2309 {
2310   register int c, c2, c3;
2311   enum cpp_token token;
2312   struct cpp_options *opts = CPP_OPTIONS (pfile);
2313
2314  get_next:
2315   c = GETC();
2316   if (c == EOF)
2317     {
2318       if (CPP_BUFFER (pfile)->manual_pop)
2319         /* If we've been reading from redirected input, the
2320            frontend will pop the buffer.  */
2321         return CPP_EOF;
2322       else if (CPP_BUFFER (pfile)->seen_eof)
2323         {
2324           if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == NULL)
2325             return CPP_EOF;
2326
2327           cpp_pop_buffer (pfile);
2328           goto get_next;
2329         }
2330       else
2331         {
2332           cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2333           struct if_stack *ifs, *nifs;
2334
2335           /* Unwind the conditional stack and generate error messages.  */
2336           for (ifs = pfile->if_stack;
2337                ifs != CPP_BUFFER (pfile)->if_stack;
2338                ifs = nifs)
2339             {
2340               cpp_error_with_line (pfile, ifs->lineno, -1,
2341                                    "unterminated `%s' conditional",
2342                                    if_directive_name (pfile, ifs));
2343
2344               nifs = ifs->next;
2345               free (ifs);
2346             }
2347           pfile->if_stack = ifs;
2348
2349           if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
2350             {
2351               /* We're about to return from an #include file.
2352                  Emit #line information now (as part of the CPP_POP) result.
2353                  But the #line refers to the file we will pop to.  */
2354               cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2355               CPP_BUFFER (pfile) = next_buf;
2356               pfile->input_stack_listing_current = 0;
2357               output_line_command (pfile, leave_file);
2358               CPP_BUFFER (pfile) = cur_buffer;
2359             }
2360
2361           CPP_BUFFER (pfile)->seen_eof = 1;
2362           return CPP_POP;
2363         }
2364     }
2365   else
2366     {
2367       switch (c)
2368         {
2369         case '/':
2370           if (PEEKC () == '=')
2371             goto op2;
2372
2373         comment:
2374           if (opts->discard_comments)
2375             c = skip_comment (pfile, c);
2376           else
2377             c = copy_comment (pfile, c);
2378           if (c != ' ')
2379             goto randomchar;
2380           
2381           /* Comments are equivalent to spaces.
2382              For -traditional, a comment is equivalent to nothing.  */
2383           if (opts->traditional || !opts->discard_comments)
2384             return CPP_COMMENT;
2385           else
2386             {
2387               CPP_PUTC (pfile, c);
2388               return CPP_HSPACE;
2389             }
2390
2391         case '#':
2392           if (pfile->parsing_if_directive)
2393             {
2394               cpp_skip_hspace (pfile);
2395               parse_assertion (pfile);
2396               return CPP_ASSERTION;
2397             }
2398
2399           if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2400             {
2401               CPP_RESERVE (pfile, 3);
2402               CPP_PUTC_Q (pfile, '#');
2403               CPP_NUL_TERMINATE_Q (pfile);
2404               if (PEEKC () != '#')
2405                 return CPP_STRINGIZE;
2406               
2407               FORWARD (1);
2408               CPP_PUTC_Q (pfile, '#');
2409               CPP_NUL_TERMINATE_Q (pfile);
2410               return CPP_TOKPASTE;
2411             }
2412
2413           if (!pfile->only_seen_white)
2414             goto randomchar;
2415           /* -traditional directives are recognized only with the # in
2416              column 1.
2417              XXX Layering violation.  */
2418           if (CPP_TRADITIONAL (pfile)
2419               && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2420             goto randomchar;
2421           if (handle_directive (pfile))
2422             return CPP_DIRECTIVE;
2423           pfile->only_seen_white = 0;
2424           goto randomchar;
2425
2426         case '\"':
2427         case '\'':
2428           parse_string (pfile, c);
2429           pfile->only_seen_white = 0;
2430           return c == '\'' ? CPP_CHAR : CPP_STRING;
2431
2432         case '$':
2433           if (!opts->dollars_in_ident)
2434             goto randomchar;
2435           goto letter;
2436
2437         case ':':
2438           if (opts->cplusplus && PEEKC () == ':')
2439             goto op2;
2440           goto randomchar;
2441
2442         case '&':
2443         case '+':
2444         case '|':
2445           c2 = PEEKC ();
2446           if (c2 == c || c2 == '=')
2447             goto op2;
2448           goto randomchar;
2449
2450         case '*':
2451         case '!':
2452         case '%':
2453         case '=':
2454         case '^':
2455           if (PEEKC () == '=')
2456             goto op2;
2457           goto randomchar;
2458
2459         case '-':
2460           c2 = PEEKC ();
2461           if (c2 == '-' && opts->chill)
2462             goto comment;  /* Chill style comment */
2463           if (c2 == '-' || c2 == '=')
2464             goto op2;
2465           if (c2 == '>')
2466             {
2467               if (opts->cplusplus && PEEKN (1) == '*')
2468                 {
2469                   /* In C++, there's a ->* operator.  */
2470                   token = CPP_OTHER;
2471                   pfile->only_seen_white = 0;
2472                   CPP_RESERVE (pfile, 4);
2473                   CPP_PUTC_Q (pfile, c);
2474                   CPP_PUTC_Q (pfile, GETC ());
2475                   CPP_PUTC_Q (pfile, GETC ());
2476                   CPP_NUL_TERMINATE_Q (pfile);
2477                   return token;
2478                 }
2479               goto op2;
2480             }
2481           goto randomchar;
2482
2483         case '<':
2484           if (pfile->parsing_include_directive)
2485             {
2486               for (;;)
2487                 {
2488                   CPP_PUTC (pfile, c);
2489                   if (c == '>')
2490                     break;
2491                   c = GETC ();
2492                   if (c == '\n' || c == EOF)
2493                     {
2494                       cpp_error (pfile,
2495                                  "missing '>' in `#include <FILENAME>'");
2496                       break;
2497                     }
2498                   else if (c == '\r')
2499                     {
2500                       if (!CPP_BUFFER (pfile)->has_escapes)
2501                         {
2502                           /* Backslash newline is replaced by nothing. */
2503                           CPP_ADJUST_WRITTEN (pfile, -1);
2504                           CPP_BUMP_LINE (pfile);
2505                         }
2506                       else
2507                         {
2508                           /* We might conceivably get \r- or \r<space> in
2509                              here.  Just delete 'em. */
2510                           int d = GETC();
2511                           if (d != '-' && d != ' ')
2512                             cpp_ice (pfile, "unrecognized escape \\r%c", d);
2513                           CPP_ADJUST_WRITTEN (pfile, -1);
2514                         }                         
2515                     }
2516                 }
2517               return CPP_STRING;
2518             }
2519           /* else fall through */
2520         case '>':
2521           c2 = PEEKC ();
2522           if (c2 == '=')
2523             goto op2;
2524           /* GNU C++ supports MIN and MAX operators <? and >?.  */
2525           if (c2 != c && (!opts->cplusplus || c2 != '?'))
2526             goto randomchar;
2527           FORWARD(1);
2528           CPP_RESERVE (pfile, 4);
2529           CPP_PUTC (pfile, c);
2530           CPP_PUTC (pfile, c2);
2531           c3 = PEEKC ();
2532           if (c3 == '=')
2533             CPP_PUTC_Q (pfile, GETC ());
2534           CPP_NUL_TERMINATE_Q (pfile);
2535           pfile->only_seen_white = 0;
2536           return CPP_OTHER;
2537
2538         case '.':
2539           c2 = PEEKC ();
2540           if (ISDIGIT(c2))
2541             {
2542               CPP_RESERVE(pfile, 2);
2543               CPP_PUTC_Q (pfile, '.');
2544               c = GETC ();
2545               goto number;
2546             }
2547
2548           /* In C++ there's a .* operator.  */
2549           if (opts->cplusplus && c2 == '*')
2550             goto op2;
2551
2552           if (c2 == '.' && PEEKN(1) == '.')
2553             {
2554               CPP_RESERVE(pfile, 4);
2555               CPP_PUTC_Q (pfile, '.');
2556               CPP_PUTC_Q (pfile, '.');
2557               CPP_PUTC_Q (pfile, '.');
2558               FORWARD (2);
2559               CPP_NUL_TERMINATE_Q (pfile);
2560               pfile->only_seen_white = 0;
2561               return CPP_3DOTS;
2562             }
2563           goto randomchar;
2564
2565         op2:
2566           token = CPP_OTHER;
2567           pfile->only_seen_white = 0;
2568           CPP_RESERVE(pfile, 3);
2569           CPP_PUTC_Q (pfile, c);
2570           CPP_PUTC_Q (pfile, GETC ());
2571           CPP_NUL_TERMINATE_Q (pfile);
2572           return token;
2573
2574         case 'L':
2575           c2 = PEEKC ();
2576           if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2577             {
2578               CPP_PUTC (pfile, c);
2579               c = GETC ();
2580               parse_string (pfile, c);
2581               pfile->only_seen_white = 0;
2582               return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2583             }
2584           goto letter;
2585
2586         case '0': case '1': case '2': case '3': case '4':
2587         case '5': case '6': case '7': case '8': case '9':
2588         number:
2589           c2  = '.';
2590           for (;;)
2591             {
2592               CPP_RESERVE (pfile, 2);
2593               CPP_PUTC_Q (pfile, c);
2594               c = PEEKC ();
2595               if (c == EOF)
2596                 break;
2597               if (!is_numchar(c) && c != '.'
2598                   && ((c2 != 'e' && c2 != 'E'
2599                        && ((c2 != 'p' && c2 != 'P')
2600                            || CPP_OPTIONS (pfile)->c89))
2601                       || (c != '+' && c != '-')))
2602                 break;
2603               FORWARD(1);
2604               c2= c;
2605             }
2606           CPP_NUL_TERMINATE_Q (pfile);
2607           pfile->only_seen_white = 0;
2608           return CPP_NUMBER;
2609         case 'b': case 'c': case 'd': case 'h': case 'o':
2610         case 'B': case 'C': case 'D': case 'H': case 'O':
2611           if (opts->chill && PEEKC () == '\'')
2612             {
2613               pfile->only_seen_white = 0;
2614               CPP_RESERVE (pfile, 2);
2615               CPP_PUTC_Q (pfile, c);
2616               CPP_PUTC_Q (pfile, '\'');
2617               FORWARD(1);
2618               for (;;)
2619                 {
2620                   c = GETC();
2621                   if (c == EOF)
2622                     goto chill_number_eof;
2623                   if (!is_numchar(c))
2624                     break;
2625                   CPP_PUTC (pfile, c);
2626                 }
2627               if (c == '\'')
2628                 {
2629                   CPP_RESERVE (pfile, 2);
2630                   CPP_PUTC_Q (pfile, c);
2631                   CPP_NUL_TERMINATE_Q (pfile);
2632                   return CPP_STRING;
2633                 }
2634               else
2635                 {
2636                   FORWARD(-1);
2637                 chill_number_eof:
2638                   CPP_NUL_TERMINATE (pfile);
2639                   return CPP_NUMBER;
2640                 }
2641             }
2642           else
2643             goto letter;
2644         case '_':
2645         case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2646         case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2647         case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2648         case 'x': case 'y': case 'z':
2649         case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2650         case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2651         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2652         case 'Y': case 'Z':
2653         letter:
2654           {
2655             HASHNODE *hp;
2656             unsigned char *ident;
2657             int before_name_written = CPP_WRITTEN (pfile);
2658             int ident_len;
2659             parse_name (pfile, c);
2660             pfile->only_seen_white = 0;
2661             if (pfile->no_macro_expand)
2662               return CPP_NAME;
2663             ident = pfile->token_buffer + before_name_written;
2664             ident_len = CPP_PWRITTEN (pfile) - ident;
2665             hp = _cpp_lookup (pfile, ident, ident_len);
2666             if (!hp)
2667               return CPP_NAME;
2668             if (hp->type == T_DISABLED)
2669               {
2670                 if (pfile->output_escapes)
2671                   { /* Return "\r-IDENT", followed by '\0'.  */
2672                     int i;
2673                     CPP_RESERVE (pfile, 3);
2674                     ident = pfile->token_buffer + before_name_written;
2675                     CPP_ADJUST_WRITTEN (pfile, 2);
2676                     for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2677                     ident[0] = '\r';
2678                     ident[1] = '-';
2679                   }
2680                 return CPP_NAME;
2681               }
2682
2683             /* If macro wants an arglist, verify that a '(' follows.  */
2684             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2685             {
2686               int macbuf_whitespace = 0;
2687
2688               while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2689                 {
2690                   const U_CHAR *point = CPP_BUFFER (pfile)->cur;
2691                   for (;;)
2692                     {
2693                       cpp_skip_hspace (pfile);
2694                       c = PEEKC ();
2695                       if (c == '\n')
2696                         FORWARD(1);
2697                       else
2698                         break;
2699                     }
2700                   if (point != CPP_BUFFER (pfile)->cur)
2701                     macbuf_whitespace = 1;
2702                   if (c == '(')
2703                     goto is_macro_call;
2704                   else if (c != EOF)
2705                     goto not_macro_call;
2706                   cpp_pop_buffer (pfile);
2707                 }
2708
2709               parse_set_mark (pfile);
2710               for (;;)
2711                 {
2712                   cpp_skip_hspace (pfile);
2713                   c = PEEKC ();
2714                   if (c == '\n')
2715                     FORWARD(1);
2716                   else
2717                     break;
2718                 }
2719               parse_goto_mark (pfile);
2720
2721               if (c == '(')
2722                 goto is_macro_call;
2723
2724             not_macro_call:
2725               if (macbuf_whitespace)
2726                 CPP_PUTC (pfile, ' ');
2727               return CPP_NAME;
2728             }
2729           is_macro_call:
2730             /* This is now known to be a macro call.
2731                Expand the macro, reading arguments as needed,
2732                and push the expansion on the input stack.  */
2733             _cpp_macroexpand (pfile, hp);
2734             CPP_SET_WRITTEN (pfile, before_name_written);
2735           }
2736           goto get_next;
2737
2738         case ' ':  case '\t':  case '\v':
2739           for (;;)
2740             {
2741               CPP_PUTC (pfile, c);
2742               c = PEEKC ();
2743               if (c == EOF || !is_hspace(c))
2744                 break;
2745               FORWARD(1);
2746             }
2747           return CPP_HSPACE;
2748
2749         case '\r':
2750           if (CPP_BUFFER (pfile)->has_escapes)
2751             {
2752               c = GETC ();
2753               if (c == '-')
2754                 {
2755                   if (pfile->output_escapes)
2756                     CPP_PUTS (pfile, "\r-", 2);
2757                   parse_name (pfile, GETC ());
2758                   return CPP_NAME;
2759                 }
2760               else if (c == ' ')
2761                 {
2762                   CPP_RESERVE (pfile, 2);
2763                   if (pfile->output_escapes)
2764                     CPP_PUTC_Q (pfile, '\r');
2765                   CPP_PUTC_Q (pfile, c);
2766                   return CPP_HSPACE;
2767                 }
2768               else
2769                 {
2770                   cpp_ice (pfile, "unrecognized escape \\r%c", c);
2771                   goto get_next;
2772                 }
2773             }
2774           else
2775             {
2776               /* Backslash newline is ignored. */
2777               CPP_BUMP_LINE (pfile);
2778               goto get_next;
2779             }
2780
2781         case '\n':
2782           CPP_PUTC (pfile, c);
2783           if (pfile->only_seen_white == 0)
2784             pfile->only_seen_white = 1;
2785           CPP_BUMP_LINE (pfile);
2786           if (! CPP_OPTIONS (pfile)->no_line_commands)
2787             {
2788               pfile->lineno++;
2789               if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2790                 output_line_command (pfile, same_file);
2791             }
2792           return CPP_VSPACE;
2793
2794         case '(': token = CPP_LPAREN;    goto char1;
2795         case ')': token = CPP_RPAREN;    goto char1;
2796         case '{': token = CPP_LBRACE;    goto char1;
2797         case '}': token = CPP_RBRACE;    goto char1;
2798         case ',': token = CPP_COMMA;     goto char1;
2799         case ';': token = CPP_SEMICOLON; goto char1;
2800
2801         randomchar:
2802         default:
2803           token = CPP_OTHER;
2804         char1:
2805           pfile->only_seen_white = 0;
2806           CPP_PUTC (pfile, c);
2807           return token;
2808         }
2809     }
2810 }
2811
2812 /* Like cpp_get_token, but skip spaces and comments.  */
2813
2814 enum cpp_token
2815 cpp_get_non_space_token (pfile)
2816      cpp_reader *pfile;
2817 {
2818   int old_written = CPP_WRITTEN (pfile);
2819   for (;;)
2820     {
2821       enum cpp_token token = cpp_get_token (pfile);
2822       if (token != CPP_COMMENT && token != CPP_POP
2823           && token != CPP_HSPACE && token != CPP_VSPACE)
2824         return token;
2825       CPP_SET_WRITTEN (pfile, old_written);
2826     }
2827 }
2828
2829 /* Parse an identifier starting with C.  */
2830
2831 static void
2832 parse_name (pfile, c)
2833      cpp_reader *pfile;
2834      int c;
2835 {
2836   for (;;)
2837   {
2838       if (! is_idchar(c))
2839       {
2840           FORWARD (-1);
2841           break;
2842       }
2843
2844       if (c == '$' && CPP_PEDANTIC (pfile))
2845         cpp_pedwarn (pfile, "`$' in identifier");
2846
2847       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
2848       CPP_PUTC_Q (pfile, c);
2849       c = GETC();
2850       if (c == EOF)
2851         break;
2852   }
2853   CPP_NUL_TERMINATE_Q (pfile);
2854   return;
2855 }
2856
2857 /* Parse and skip over a string starting with C.  A single quoted
2858    string is treated like a double -- some programs (e.g., troff) are
2859    perverse this way.  (However, a single quoted string is not allowed
2860    to extend over multiple lines.)  */
2861 static void
2862 skip_string (pfile, c)
2863      cpp_reader *pfile;
2864      int c;
2865 {
2866   long start_line, start_column;
2867   cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2868
2869   while (1)
2870     {
2871       int cc = GETC();
2872       switch (cc)
2873         {
2874         case EOF:
2875           cpp_error_with_line (pfile, start_line, start_column,
2876                                "unterminated string or character constant");
2877           if (pfile->multiline_string_line != start_line
2878               && pfile->multiline_string_line != 0)
2879             cpp_error_with_line (pfile,
2880                                  pfile->multiline_string_line, -1,
2881                          "possible real start of unterminated constant");
2882           pfile->multiline_string_line = 0;
2883           return;
2884
2885         case '\n':
2886           CPP_BUMP_LINE (pfile);
2887           /* In Fortran and assembly language, silently terminate
2888              strings of either variety at end of line.  This is a
2889              kludge around not knowing where comments are in these
2890              languages.  */
2891           if (CPP_OPTIONS (pfile)->lang_fortran
2892               || CPP_OPTIONS (pfile)->lang_asm)
2893             {
2894               FORWARD(-1);
2895               return;
2896             }
2897           /* Character constants may not extend over multiple lines.
2898              In Standard C, neither may strings.  We accept multiline
2899              strings as an extension.  */
2900           if (c == '\'')
2901             {
2902               cpp_error_with_line (pfile, start_line, start_column,
2903                                    "unterminated character constant");
2904               FORWARD(-1);
2905               return;
2906             }
2907           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2908             cpp_pedwarn_with_line (pfile, start_line, start_column,
2909                                    "string constant runs past end of line");
2910           if (pfile->multiline_string_line == 0)
2911             pfile->multiline_string_line = start_line;
2912           break;
2913
2914         case '\r':
2915           if (CPP_BUFFER (pfile)->has_escapes)
2916             {
2917               cpp_ice (pfile, "\\r escape inside string constant");
2918               FORWARD(1);
2919             }
2920           else
2921             /* Backslash newline is replaced by nothing at all.  */
2922             CPP_BUMP_LINE (pfile);
2923           break;
2924
2925         case '\\':
2926           FORWARD(1);
2927           break;
2928
2929         case '\"':
2930         case '\'':
2931           if (cc == c)
2932             return;
2933           break;
2934         }
2935     }
2936 }
2937
2938 /* Parse a string and copy it to the output.  */
2939
2940 static void
2941 parse_string (pfile, c)
2942      cpp_reader *pfile;
2943      int c;
2944 {
2945   const U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
2946   const U_CHAR *limit;
2947
2948   skip_string (pfile, c);
2949
2950   limit = CPP_BUFFER (pfile)->cur;
2951   CPP_RESERVE (pfile, limit - start + 2);
2952   CPP_PUTC_Q (pfile, c);
2953   for (; start < limit; start++)
2954     if (*start != '\r')
2955       CPP_PUTC_Q (pfile, *start);
2956 }
2957
2958 /* Read an assertion into the token buffer, converting to
2959    canonical form: `#predicate(a n swe r)'  The next non-whitespace
2960    character to read should be the first letter of the predicate.
2961    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2962    with answer (see callers for why). In case of 0, an error has been
2963    printed. */
2964 static int
2965 parse_assertion (pfile)
2966      cpp_reader *pfile;
2967 {
2968   int c, dropwhite;
2969   cpp_skip_hspace (pfile);
2970   c = PEEKC();
2971   if (! is_idstart(c))
2972     {
2973       cpp_error (pfile, "assertion predicate is not an identifier");
2974       return 0;
2975     }
2976   CPP_PUTC(pfile, '#');
2977   FORWARD(1);
2978   parse_name(pfile, c);
2979
2980   c = PEEKC();
2981   if (c != '(')
2982     {
2983       if (is_hspace(c) || c == '\r')
2984         cpp_skip_hspace (pfile);
2985       c = PEEKC();
2986     }
2987   if (c != '(')
2988     return 1;
2989
2990   CPP_PUTC(pfile, '(');
2991   FORWARD(1);
2992   dropwhite = 1;
2993   while ((c = GETC()) != ')')
2994     {
2995       if (is_space(c))
2996         {
2997           if (! dropwhite)
2998             {
2999               CPP_PUTC(pfile, ' ');
3000               dropwhite = 1;
3001             }
3002         }
3003       else if (c == '\n' || c == EOF)
3004         {
3005           if (c == '\n') FORWARD(-1);
3006           cpp_error (pfile, "un-terminated assertion answer");
3007           return 0;
3008         }
3009       else if (c == '\r')
3010         /* \r cannot be a macro escape here. */
3011         CPP_BUMP_LINE (pfile);
3012       else
3013         {
3014           CPP_PUTC (pfile, c);
3015           dropwhite = 0;
3016         }
3017     }
3018
3019   if (pfile->limit[-1] == ' ')
3020     pfile->limit[-1] = ')';
3021   else if (pfile->limit[-1] == '(')
3022     {
3023       cpp_error (pfile, "empty token sequence in assertion");
3024       return 0;
3025     }
3026   else
3027     CPP_PUTC (pfile, ')');
3028
3029   CPP_NUL_TERMINATE (pfile);
3030   return 2;
3031 }
3032
3033 static int
3034 do_assert (pfile, keyword)
3035      cpp_reader *pfile;
3036      const struct directive *keyword ATTRIBUTE_UNUSED;
3037 {
3038   U_CHAR *sym;
3039   int ret, c;
3040   HASHNODE *base, *this;
3041   HASHNODE **bslot, **tslot;
3042   size_t blen, tlen;
3043   unsigned long bhash, thash;
3044
3045   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3046     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3047
3048   cpp_skip_hspace (pfile);
3049   sym = CPP_PWRITTEN (pfile);   /* remember where it starts */
3050   ret = parse_assertion (pfile);
3051   if (ret == 0)
3052     goto error;
3053   else if (ret == 1)
3054     {
3055       cpp_error (pfile, "missing token-sequence in `#assert'");
3056       goto error;
3057     }
3058
3059   cpp_skip_hspace (pfile);
3060   c = PEEKC();
3061   if (c != EOF && c != '\n')
3062     {
3063       cpp_error (pfile, "junk at end of `#assert'");
3064       goto error;
3065     }
3066
3067   tlen = strlen (sym);
3068   blen = (U_CHAR *) strchr (sym, '(') - sym;
3069   tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
3070   if (*tslot)
3071     {
3072       cpp_warning (pfile, "`%s' re-asserted", sym);
3073       goto error;
3074     }
3075
3076   bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
3077   if (! *bslot)
3078     *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
3079   else
3080     {
3081       base = *bslot;
3082       if (base->type != T_ASSERT)
3083         {
3084           /* Token clash - but with what?! */
3085           cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3086           goto error;
3087         }
3088     }
3089   *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
3090   this->value.aschain = base->value.aschain;
3091   base->value.aschain = this;
3092   
3093   pfile->limit = sym;           /* Pop */
3094   return 0;
3095
3096  error:
3097   skip_rest_of_line (pfile);
3098   pfile->limit = sym;           /* Pop */
3099   return 0;
3100 }
3101
3102 static int
3103 do_unassert (pfile, keyword)
3104      cpp_reader *pfile;
3105      const struct directive *keyword ATTRIBUTE_UNUSED;
3106 {
3107   int c, ret;
3108   U_CHAR *sym;
3109   long baselen, thislen;
3110   HASHNODE *base, *this, *next;
3111   
3112   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3113     cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3114
3115   cpp_skip_hspace (pfile);
3116
3117   sym = CPP_PWRITTEN (pfile);   /* remember where it starts */
3118   ret = parse_assertion (pfile);
3119   if (ret == 0)
3120     goto error;
3121   
3122   cpp_skip_hspace (pfile);
3123   c = PEEKC ();
3124   if (c != EOF && c != '\n')
3125       cpp_error (pfile, "junk at end of `#unassert'");
3126
3127   thislen = strlen (sym);
3128   if (ret == 1)
3129     {
3130       base = _cpp_lookup (pfile, sym, thislen);
3131       if (! base)
3132         goto error;  /* It isn't an error to #undef what isn't #defined,
3133                         so it isn't an error to #unassert what isn't
3134                         #asserted either. */
3135       
3136       for (this = base->value.aschain; this; this = next)
3137         {
3138           next = this->value.aschain;
3139           htab_remove_elt (pfile->hashtab, this);
3140         }
3141       htab_remove_elt (pfile->hashtab, base);
3142     }
3143   else
3144     {
3145       baselen = (U_CHAR *) strchr (sym, '(') - sym;
3146       base = _cpp_lookup (pfile, sym, baselen);
3147       if (! base) goto error;
3148       this = _cpp_lookup (pfile, sym, thislen);
3149       if (! this) goto error;
3150
3151       next = base;
3152       while (next->value.aschain != this)
3153         next = next->value.aschain;
3154
3155       next->value.aschain = this->value.aschain;
3156       htab_remove_elt (pfile->hashtab, this);
3157
3158       if (base->value.aschain == NULL)
3159         /* Last answer for this predicate deleted. */
3160         htab_remove_elt (pfile->hashtab, base);
3161     }
3162   
3163   pfile->limit = sym;           /* Pop */
3164   return 0;
3165  error:
3166   skip_rest_of_line (pfile);
3167   pfile->limit = sym;           /* Pop */
3168   return 0;
3169 }
3170
3171 /* Process STR as if it appeared as the body of an #unassert. */
3172 void
3173 cpp_unassert (pfile, str)
3174      cpp_reader *pfile;
3175      const char *str;
3176 {
3177   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3178     {
3179       do_unassert (pfile, NULL);
3180       cpp_pop_buffer (pfile);
3181     }
3182 }  
3183
3184 /* Remember the current position of PFILE so it may be returned to
3185    after looking ahead a bit.
3186
3187    Note that when you set a mark, you _must_ return to that mark.  You
3188    may not forget about it and continue parsing.  You may not pop a
3189    buffer with an active mark.  You may not call CPP_BUMP_LINE while a
3190    mark is active.  */
3191
3192 static void
3193 parse_set_mark (pfile)
3194      cpp_reader *pfile;
3195 {
3196   cpp_buffer *ip = CPP_BUFFER (pfile);
3197   if (ACTIVE_MARK_P())
3198       cpp_ice (pfile, "mark active in parse_set_mark");
3199
3200   ip->mark = ip->cur - ip->buf;
3201 }
3202
3203 /* Backup the current position of PFILE to that saved in its mark,
3204    and clear the mark.  */
3205
3206 static void
3207 parse_goto_mark (pfile)
3208      cpp_reader *pfile;
3209 {
3210   cpp_buffer *ip = CPP_BUFFER (pfile);
3211   if (!ACTIVE_MARK_P())
3212       cpp_ice (pfile, "mark not active in parse_goto_mark");
3213
3214   ip->cur = ip->buf + ip->mark;
3215   ip->mark = -1;
3216 }