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