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