* gcc.h (lang_specific_driver): Constify second argument.
[official-gcc.git] / gcc / cpplib.c
blobfe96d4ed37684c0587960395b1f54bb055ec9c16
1 /* CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "intl.h"
28 #include "obstack.h"
29 #include "symcat.h"
31 /* Stack of conditionals currently in progress
32 (including both successful and failing conditionals). */
34 struct if_stack
36 struct if_stack *next;
37 unsigned int lineno; /* line number where condition started */
38 unsigned int colno; /* and column */
39 int was_skipping; /* value of pfile->skipping before this if */
40 const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */
41 int type; /* type of last directive seen in this group */
44 /* Forward declarations. */
46 static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
47 static int parse_include PARAMS ((cpp_reader *, const U_CHAR *, int,
48 const U_CHAR **, unsigned int *,
49 int *));
50 static void push_conditional PARAMS ((cpp_reader *, int, int,
51 const cpp_hashnode *));
52 static int read_line_number PARAMS ((cpp_reader *, int *));
53 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
54 unsigned long *));
56 static const cpp_hashnode *
57 parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
58 static const cpp_hashnode *
59 detect_if_not_defined PARAMS ((cpp_reader *));
60 static cpp_hashnode *
61 get_define_node PARAMS ((cpp_reader *));
62 static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *));
64 /* Utility. */
65 #define str_match(sym, len, str) \
66 ((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
68 /* This is the table of directive handlers. It is ordered by
69 frequency of occurrence; the numbers at the end are directive
70 counts from all the source code I have lying around (egcs and libc
71 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
72 pcmcia-cs-3.0.9).
74 The entries with a dash and a name after the count are extensions,
75 of which all but #warning and #include_next are deprecated. The name
76 is where the extension appears to have come from. */
78 /* #sccs is not always recognized. */
79 #ifdef SCCS_DIRECTIVE
80 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
81 #else
82 # define SCCS_ENTRY /* nothing */
83 #endif
85 #define DIRECTIVE_TABLE \
86 D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \
87 D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
88 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
89 D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
90 D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
91 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
92 D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
93 D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \
94 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
95 D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
96 D(error, T_ERROR, STDC89, 0) /* 475 */ \
97 D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \
98 D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
99 D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
100 D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \
101 D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
102 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
103 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
104 SCCS_ENTRY /* 0 SVR2? */
106 /* Use the table to generate a series of prototypes, an enum for the
107 directive names, and an array of directive handlers. */
109 /* The directive-processing functions are declared to return int
110 instead of void, because some old compilers have trouble with
111 pointers to functions returning void. */
113 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
114 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
115 DIRECTIVE_TABLE
116 #undef D
118 #define D(n, tag, o, f) tag,
119 enum
121 DIRECTIVE_TABLE
122 N_DIRECTIVES
124 #undef D
126 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
127 #define D(name, t, origin, flags) \
128 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
129 sizeof STRINGX(name) - 1, origin, flags },
130 static const struct directive dtable[] =
132 DIRECTIVE_TABLE
134 #undef D
135 #undef DIRECTIVE_TABLE
137 /* Check if a token's name matches that of a known directive. Put in
138 this file to save exporting dtable and other unneeded information. */
139 const struct directive *
140 _cpp_check_directive (pfile, token, bol)
141 cpp_reader *pfile;
142 const cpp_token *token;
143 int bol;
145 unsigned int i;
147 /* If we are rescanning preprocessed input, don't obey any directives
148 other than # nnn. */
149 if (CPP_OPTION (pfile, preprocessed))
150 return 0;
152 for (i = 0; i < N_DIRECTIVES; i++)
153 if (pfile->spec_nodes->dirs[i] == token->val.node)
155 /* In -traditional mode, a directive is ignored unless its #
156 is in column 1. In code intended to work with K+R compilers,
157 therefore, directives added by C89 must have their # indented,
158 and directives present in traditional C must not. This is true
159 even of directives in skipped conditional blocks. */
160 if (CPP_WTRADITIONAL (pfile))
162 if (!bol && dtable[i].origin == KANDR)
163 cpp_warning (pfile,
164 "traditional C ignores #%s with the # indented",
165 dtable[i].name);
167 if (bol && dtable[i].origin != KANDR)
168 cpp_warning (pfile,
169 "suggest hiding #%s from traditional C with an indented #",
170 dtable[i].name);
173 /* If we are skipping a failed conditional group, all non-conditional
174 directives are ignored. */
175 if (pfile->skipping && !(dtable[i].flags & COND))
176 return 0;
178 /* Issue -pedantic warnings for extended directives. */
179 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
180 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
182 return &dtable[i];
185 return 0;
188 const struct directive *
189 _cpp_check_linemarker (pfile, token, bol)
190 cpp_reader *pfile;
191 const cpp_token *token ATTRIBUTE_UNUSED;
192 int bol;
194 /* # followed by a number is equivalent to #line. Do not recognize
195 this form in assembly language source files or skipped
196 conditional groups. Complain about this form if we're being
197 pedantic, but not if this is regurgitated input (preprocessed or
198 fed back in by the C++ frontend). */
199 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
200 return 0;
202 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
203 && ! CPP_OPTION (pfile, preprocessed))
204 cpp_pedwarn (pfile, "# followed by integer");
206 /* In -traditional mode, a directive is ignored unless its #
207 is in column 1. */
208 if (!bol && CPP_WTRADITIONAL (pfile))
209 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
210 dtable[T_LINE].name);
212 return &dtable[T_LINE];
215 static cpp_hashnode *
216 get_define_node (pfile)
217 cpp_reader *pfile;
219 const cpp_token *token;
221 /* Skip any -C comments. */
222 while ((token = _cpp_get_token (pfile))->type == CPP_COMMENT)
225 /* The token immediately after #define must be an identifier. That
226 identifier is not allowed to be "defined". See predefined macro
227 names (6.10.8.4). In C++, it is not allowed to be any of the
228 <iso646.h> macro names (which are keywords in C++) either. */
230 if (token->type != CPP_NAME)
232 if (token->type == CPP_DEFINED)
233 cpp_error_with_line (pfile, token->line, token->col,
234 "\"defined\" cannot be used as a macro name");
235 else if (token->flags & NAMED_OP)
236 cpp_error_with_line (pfile, token->line, token->col,
237 "\"%s\" cannot be used as a macro name in C++",
238 token->val.node->name);
239 else
240 cpp_error_with_line (pfile, token->line, token->col,
241 "macro names must be identifiers");
242 return 0;
245 /* Check for poisoned identifiers now. */
246 if (token->val.node->type == T_POISON)
248 cpp_error_with_line (pfile, token->line, token->col,
249 "attempt to use poisoned \"%s\"",
250 token->val.node->name);
251 return 0;
254 return token->val.node;
257 /* Process a #define command. */
258 static void
259 do_define (pfile)
260 cpp_reader *pfile;
262 cpp_hashnode *node;
264 if ((node = get_define_node (pfile)))
265 if (_cpp_create_definition (pfile, node))
266 if (pfile->cb.define)
267 (*pfile->cb.define) (pfile, node);
270 /* Remove the definition of a symbol from the symbol table. */
271 static void
272 do_undef (pfile)
273 cpp_reader *pfile;
275 cpp_hashnode *node = get_define_node (pfile);
277 if (_cpp_get_token (pfile)->type != CPP_EOF)
278 cpp_pedwarn (pfile, "junk on line after #undef");
280 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
281 is not currently defined as a macro name. */
282 if (node && node->type != T_VOID)
284 if (pfile->cb.undef)
285 (*pfile->cb.undef) (pfile, node);
287 if (node->type != T_MACRO)
288 cpp_warning (pfile, "undefining \"%s\"", node->name);
290 _cpp_free_definition (node);
291 node->type = T_VOID;
296 /* Handle #include and #import. */
298 static int
299 parse_include (pfile, dir, trail, strp, lenp, abp)
300 cpp_reader *pfile;
301 const U_CHAR *dir;
302 int trail;
303 const U_CHAR **strp;
304 unsigned int *lenp;
305 int *abp;
307 const cpp_token *name = _cpp_get_token (pfile);
309 if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
311 if (name->type == CPP_LESS)
312 name = _cpp_glue_header_name (pfile);
313 else
315 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
316 return 1;
319 if (name->val.str.len == 0)
321 cpp_error (pfile, "empty file name in #%s", dir);
322 return 1;
325 if (!trail && _cpp_get_token (pfile)->type != CPP_EOF)
326 cpp_error (pfile, "junk at end of #%s", dir);
328 *lenp = name->val.str.len;
329 *strp = name->val.str.text;
330 *abp = (name->type == CPP_HEADER_NAME);
332 if (pfile->cb.include)
333 (*pfile->cb.include) (pfile, dir, *strp, *lenp, *abp);
334 return 0;
337 static void
338 do_include (pfile)
339 cpp_reader *pfile;
341 unsigned int len;
342 const U_CHAR *str;
343 int ab;
345 if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
346 return;
348 _cpp_execute_include (pfile, str, len, 0, 0, ab);
351 static void
352 do_import (pfile)
353 cpp_reader *pfile;
355 unsigned int len;
356 const U_CHAR *str;
357 int ab;
359 if (CPP_OPTION (pfile, warn_import)
360 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
362 pfile->import_warning = 1;
363 cpp_warning (pfile,
364 "#import is obsolete, use an #ifndef wrapper in the header file");
367 if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
368 return;
370 _cpp_execute_include (pfile, str, len, 1, 0, ab);
373 static void
374 do_include_next (pfile)
375 cpp_reader *pfile;
377 unsigned int len;
378 const U_CHAR *str;
379 struct file_name_list *search_start = 0;
380 int ab;
382 if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
383 return;
385 /* For #include_next, skip in the search path past the dir in which
386 the current file was found. If this is the last directory in the
387 search path, don't include anything. If the current file was
388 specified with an absolute path, use the normal search logic. If
389 this is the primary source file, use the normal search logic and
390 generate a warning. */
391 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
393 if (CPP_BUFFER (pfile)->inc->foundhere)
395 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
396 if (!search_start)
397 return;
400 else
401 cpp_warning (pfile, "#include_next in primary source file");
403 _cpp_execute_include (pfile, str, len, 0, search_start, ab);
406 /* Subroutine of do_line. Read next token from PFILE without adding it to
407 the output buffer. If it is a number between 1 and 4, store it in *NUM
408 and return 1; otherwise, return 0 and complain if we aren't at the end
409 of the directive. */
411 static int
412 read_line_number (pfile, num)
413 cpp_reader *pfile;
414 int *num;
416 const cpp_token *tok = _cpp_get_token (pfile);
417 enum cpp_ttype type = tok->type;
418 const U_CHAR *p = tok->val.str.text;
419 unsigned int len = tok->val.str.len;
421 if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
423 *num = p[0] - '0';
424 return 1;
426 else
428 if (type != CPP_EOF)
429 cpp_error (pfile, "invalid format #line");
430 return 0;
434 /* Another subroutine of do_line. Convert a number in STR, of length
435 LEN, to binary; store it in NUMP, and return 0 if the number was
436 well-formed, 1 if not. Temporary, hopefully. */
437 static int
438 strtoul_for_line (str, len, nump)
439 const U_CHAR *str;
440 unsigned int len;
441 unsigned long *nump;
443 unsigned long reg = 0;
444 U_CHAR c;
445 while (len--)
447 c = *str++;
448 if (!ISDIGIT (c))
449 return 1;
450 reg *= 10;
451 reg += c - '0';
453 *nump = reg;
454 return 0;
457 /* Interpret #line command.
458 Note that the filename string (if any) is treated as if it were an
459 include filename. That means no escape handling. */
461 static void
462 do_line (pfile)
463 cpp_reader *pfile;
465 cpp_buffer *ip = CPP_BUFFER (pfile);
466 unsigned long new_lineno, old_lineno;
467 /* C99 raised the minimum limit on #line numbers. */
468 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
469 int action_number = 0;
470 int enter = 0, leave = 0;
471 enum cpp_ttype type;
472 const U_CHAR *str;
473 char *fname;
474 unsigned int len;
475 const cpp_token *tok;
477 tok = _cpp_get_token (pfile);
478 type = tok->type;
479 str = tok->val.str.text;
480 len = tok->val.str.len;
482 if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
484 cpp_error (pfile, "token after #line is not a positive integer");
485 goto done;
488 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
489 cpp_pedwarn (pfile, "line number out of range");
491 old_lineno = ip->lineno;
492 ip->lineno = new_lineno;
493 tok = _cpp_get_token (pfile);
494 type = tok->type;
495 str = tok->val.str.text;
496 len = tok->val.str.len;
498 if (type == CPP_EOF)
499 goto done;
500 else if (type != CPP_STRING)
502 cpp_error (pfile, "second token after #line is not a string");
503 ip->lineno = old_lineno; /* malformed #line should have no effect */
504 goto done;
507 fname = alloca (len + 1);
508 memcpy (fname, str, len);
509 fname[len] = '\0';
511 if (strcmp (fname, ip->nominal_fname))
513 if (!strcmp (fname, ip->inc->name))
514 ip->nominal_fname = ip->inc->name;
515 else
516 ip->nominal_fname = _cpp_fake_include (pfile, fname);
519 if (read_line_number (pfile, &action_number) == 0)
520 return;
522 if (CPP_PEDANTIC (pfile))
523 cpp_pedwarn (pfile, "garbage at end of #line");
525 if (action_number == 1)
527 enter = 1;
528 cpp_make_system_header (pfile, ip, 0);
529 read_line_number (pfile, &action_number);
531 else if (action_number == 2)
533 leave = 1;
534 cpp_make_system_header (pfile, ip, 0);
535 read_line_number (pfile, &action_number);
537 if (action_number == 3)
539 cpp_make_system_header (pfile, ip, 1);
540 read_line_number (pfile, &action_number);
542 if (action_number == 4)
544 cpp_make_system_header (pfile, ip, 2);
545 read_line_number (pfile, &action_number);
548 if (enter && pfile->cb.enter_file)
549 (*pfile->cb.enter_file) (pfile);
550 if (leave && pfile->cb.leave_file)
551 (*pfile->cb.leave_file) (pfile);
553 done:
554 return;
558 * Report an error detected by the program we are processing.
559 * Use the text of the line in the error message.
560 * (We use error because it prints the filename & line#.)
563 static void
564 do_error (pfile)
565 cpp_reader *pfile;
567 if (_cpp_begin_message (pfile, ERROR, NULL, 0, 0))
569 cpp_output_list (pfile, stderr, &pfile->token_list,
570 pfile->first_directive_token);
571 putc ('\n', stderr);
576 * Report a warning detected by the program we are processing.
577 * Use the text of the line in the warning message, then continue.
580 static void
581 do_warning (pfile)
582 cpp_reader *pfile;
584 if (_cpp_begin_message (pfile, WARNING, NULL, 0, 0))
586 cpp_output_list (pfile, stderr, &pfile->token_list,
587 pfile->first_directive_token);
588 putc ('\n', stderr);
592 /* Report program identification. */
594 static void
595 do_ident (pfile)
596 cpp_reader *pfile;
598 const cpp_token *str = _cpp_get_token (pfile);
600 if (str->type == CPP_STRING && _cpp_get_token (pfile)->type == CPP_EOF)
602 if (pfile->cb.ident)
603 (*pfile->cb.ident) (pfile, str);
604 return;
607 cpp_error (pfile, "invalid #ident");
610 /* Pragmata handling. We handle some of these, and pass the rest on
611 to the front end. C99 defines three pragmas and says that no macro
612 expansion is to be performed on them; whether or not macro
613 expansion happens for other pragmas is implementation defined.
614 This implementation never macro-expands the text after #pragma.
616 We currently do not support the _Pragma operator. Support for that
617 has to be coordinated with the front end. Proposed implementation:
618 both #pragma blah blah and _Pragma("blah blah") become
619 __builtin_pragma(blah blah) and we teach the parser about that. */
621 /* Sub-handlers for the pragmas needing treatment here.
622 They return 1 if the token buffer is to be popped, 0 if not. */
623 struct pragma_entry
625 struct pragma_entry *next;
626 const char *name;
627 size_t len;
628 int isnspace;
629 union {
630 void (*handler) PARAMS ((cpp_reader *));
631 struct pragma_entry *space;
632 } u;
635 void
636 cpp_register_pragma (pfile, space, name, handler)
637 cpp_reader *pfile;
638 const char *space;
639 const char *name;
640 void (*handler) PARAMS ((cpp_reader *));
642 struct pragma_entry **x, *new;
643 size_t len;
645 x = &pfile->pragmas;
646 if (space)
648 struct pragma_entry *p = pfile->pragmas;
649 len = strlen (space);
650 while (p)
652 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
654 x = &p->u.space;
655 goto found;
657 p = p->next;
659 cpp_ice (pfile, "unknown #pragma namespace %s", space);
660 return;
663 found:
664 new = xnew (struct pragma_entry);
665 new->name = name;
666 new->len = strlen (name);
667 new->isnspace = 0;
668 new->u.handler = handler;
670 new->next = *x;
671 *x = new;
674 void
675 cpp_register_pragma_space (pfile, space)
676 cpp_reader *pfile;
677 const char *space;
679 struct pragma_entry *new;
680 const struct pragma_entry *p = pfile->pragmas;
681 size_t len = strlen (space);
683 while (p)
685 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
687 cpp_ice (pfile, "#pragma namespace %s already registered", space);
688 return;
690 p = p->next;
693 new = xnew (struct pragma_entry);
694 new->name = space;
695 new->len = len;
696 new->isnspace = 1;
697 new->u.space = 0;
699 new->next = pfile->pragmas;
700 pfile->pragmas = new;
703 static void do_pragma_once PARAMS ((cpp_reader *));
704 static void do_pragma_poison PARAMS ((cpp_reader *));
705 static void do_pragma_system_header PARAMS ((cpp_reader *));
706 static void do_pragma_dependency PARAMS ((cpp_reader *));
708 void
709 _cpp_init_internal_pragmas (pfile)
710 cpp_reader *pfile;
712 /* top level */
713 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
714 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
716 /* GCC namespace */
717 cpp_register_pragma_space (pfile, "GCC");
719 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
720 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
721 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
724 static void
725 do_pragma (pfile)
726 cpp_reader *pfile;
728 const struct pragma_entry *p;
729 const cpp_token *tok;
730 const cpp_hashnode *node;
731 const U_CHAR *name;
732 size_t len;
734 p = pfile->pragmas;
736 new_space:
737 tok = _cpp_get_token (pfile);
738 if (tok->type == CPP_EOF)
739 return;
741 if (tok->type != CPP_NAME)
743 cpp_error (pfile, "malformed #pragma directive");
744 return;
747 node = tok->val.node;
748 name = node->name;
749 len = node->length;
750 while (p)
752 if (strlen (p->name) == len && !memcmp (p->name, name, len))
754 if (p->isnspace)
756 p = p->u.space;
757 goto new_space;
759 else
761 (*p->u.handler) (pfile);
762 return;
765 p = p->next;
768 if (pfile->cb.def_pragma)
769 (*pfile->cb.def_pragma) (pfile);
772 static void
773 do_pragma_once (pfile)
774 cpp_reader *pfile;
776 cpp_buffer *ip = CPP_BUFFER (pfile);
778 /* Allow #pragma once in system headers, since that's not the user's
779 fault. */
780 if (!CPP_IN_SYSTEM_HEADER (pfile))
781 cpp_warning (pfile, "#pragma once is obsolete");
783 if (CPP_PREV_BUFFER (ip) == NULL)
784 cpp_warning (pfile, "#pragma once outside include file");
785 else
786 ip->inc->cmacro = NEVER_REREAD;
789 static void
790 do_pragma_poison (pfile)
791 cpp_reader *pfile;
793 /* Poison these symbols so that all subsequent usage produces an
794 error message. */
795 const cpp_token *tok;
796 cpp_hashnode *hp;
798 for (;;)
800 tok = _cpp_get_token (pfile);
801 if (tok->type == CPP_EOF)
802 break;
803 if (tok->type != CPP_NAME)
805 cpp_error (pfile, "invalid #pragma poison directive");
806 return;
809 hp = tok->val.node;
810 if (hp->type == T_POISON)
811 ; /* It is allowed to poison the same identifier twice. */
812 else
814 if (hp->type != T_VOID)
815 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
816 _cpp_free_definition (hp);
817 hp->type = T_POISON;
821 if (pfile->cb.poison)
822 (*pfile->cb.poison) (pfile);
825 /* Mark the current header as a system header. This will suppress
826 some categories of warnings (notably those from -pedantic). It is
827 intended for use in system libraries that cannot be implemented in
828 conforming C, but cannot be certain that their headers appear in a
829 system include directory. To prevent abuse, it is rejected in the
830 primary source file. */
831 static void
832 do_pragma_system_header (pfile)
833 cpp_reader *pfile;
835 cpp_buffer *ip = CPP_BUFFER (pfile);
836 if (CPP_PREV_BUFFER (ip) == NULL)
837 cpp_warning (pfile, "#pragma system_header outside include file");
838 else
839 cpp_make_system_header (pfile, ip, 1);
842 /* Check the modified date of the current include file against a specified
843 file. Issue a diagnostic, if the specified file is newer. We use this to
844 determine if a fixed header should be refixed. */
845 static void
846 do_pragma_dependency (pfile)
847 cpp_reader *pfile;
849 const U_CHAR *name;
850 unsigned int len;
851 int ordering, ab;
852 char left, right;
854 if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
855 return;
857 left = ab ? '<' : '"';
858 right = ab ? '>' : '"';
860 ordering = _cpp_compare_file_date (pfile, name, len, ab);
861 if (ordering < 0)
862 cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
863 else if (ordering > 0)
865 const cpp_token *msg = _cpp_get_token (pfile);
867 cpp_warning (pfile, "current file is older than %c%.*s%c",
868 left, (int)len, name, right);
869 if (msg->type != CPP_EOF
870 && _cpp_begin_message (pfile, WARNING, NULL, msg->line, msg->col))
872 cpp_output_list (pfile, stderr, &pfile->token_list, msg);
873 putc ('\n', stderr);
878 /* Just ignore #sccs, on systems where we define it at all. */
879 #ifdef SCCS_DIRECTIVE
880 static void
881 do_sccs (pfile)
882 cpp_reader *pfile ATTRIBUTE_UNUSED;
885 #endif
887 /* We've found an `#if' directive. If the only thing before it in
888 this file is white space, and if it is of the form
889 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
890 for inclusion of this file. (See redundant_include_p in cppfiles.c
891 for an explanation of controlling macros.) If so, return the
892 hash node for SYMBOL. Otherwise, return NULL. */
894 static const cpp_hashnode *
895 detect_if_not_defined (pfile)
896 cpp_reader *pfile;
898 const cpp_token *token;
899 cpp_hashnode *cmacro = 0;
901 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
902 token = pfile->first_directive_token + 2;
904 if (token->type != CPP_NOT)
905 return 0;
907 token++;
908 if (token->type != CPP_DEFINED)
909 return 0;
911 token++;
912 if (token->type == CPP_OPEN_PAREN)
913 token++;
915 if (token->type != CPP_NAME)
916 return 0;
918 cmacro = token->val.node;
920 if (token[-1].type == CPP_OPEN_PAREN)
922 token++;
923 if (token->type != CPP_CLOSE_PAREN)
924 return 0;
927 token++;
928 if (token->type != CPP_EOF)
929 return 0;
931 return cmacro;
934 /* Parse an #ifdef or #ifndef directive. Returns the hash node of the
935 macro being tested, and issues various error messages. */
937 static const cpp_hashnode *
938 parse_ifdef (pfile, name)
939 cpp_reader *pfile;
940 const U_CHAR *name;
942 enum cpp_ttype type;
943 const cpp_hashnode *node = 0;
945 const cpp_token *token = _cpp_get_token (pfile);
946 type = token->type;
948 if (type == CPP_EOF)
949 cpp_pedwarn (pfile, "#%s with no argument", name);
950 else if (type != CPP_NAME)
951 cpp_pedwarn (pfile, "#%s with invalid argument", name);
952 else if (_cpp_get_token (pfile)->type != CPP_EOF)
953 cpp_pedwarn (pfile, "garbage at end of #%s", name);
955 if (type == CPP_NAME)
956 node = token->val.node;
957 if (node && node->type == T_POISON)
959 cpp_error (pfile, "attempt to use poisoned identifier \"%s\"",
960 node->name);
961 node = 0;
964 return node;
967 /* #ifdef is dead simple. */
969 static void
970 do_ifdef (pfile)
971 cpp_reader *pfile;
973 const cpp_hashnode *node = 0;
975 if (! pfile->skipping)
976 node = parse_ifdef (pfile, dtable[T_IFDEF].name);
978 push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0);
981 /* #ifndef is a tad more complex, because we need to check for a
982 no-reinclusion wrapper. */
984 static void
985 do_ifndef (pfile)
986 cpp_reader *pfile;
988 int start_of_file = 0;
989 const cpp_hashnode *node = 0;
991 if (! pfile->skipping)
993 start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
994 node = parse_ifdef (pfile, dtable[T_IFNDEF].name);
997 push_conditional (pfile, node && node->type != T_VOID,
998 T_IFNDEF, start_of_file ? node : 0);
1001 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1002 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1004 static void
1005 do_if (pfile)
1006 cpp_reader *pfile;
1008 const cpp_hashnode *cmacro = 0;
1009 int value = 0;
1011 if (! pfile->skipping)
1013 if (pfile->token_list.flags & BEG_OF_FILE)
1014 cmacro = detect_if_not_defined (pfile);
1015 value = _cpp_parse_expr (pfile);
1017 push_conditional (pfile, value == 0, T_IF, cmacro);
1020 /* #else flips pfile->skipping and continues without changing
1021 if_stack; this is so that the error message for missing #endif's
1022 etc. will point to the original #if. */
1024 static void
1025 do_else (pfile)
1026 cpp_reader *pfile;
1028 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1029 validate_else (pfile, dtable[T_ELSE].name);
1031 if (ifs == NULL)
1033 cpp_error (pfile, "#else without #if");
1034 return;
1036 if (ifs->type == T_ELSE)
1038 cpp_error (pfile, "#else after #else");
1039 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1040 "the conditional began here");
1043 /* #ifndef can't have its special treatment for containing the whole file
1044 if it has a #else clause. */
1045 ifs->cmacro = 0;
1046 ifs->type = T_ELSE;
1047 if (! ifs->was_skipping)
1049 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1050 succeeded, so we mustn't do the else block. */
1051 if (pfile->skipping < 2)
1052 pfile->skipping = ! pfile->skipping;
1057 * handle a #elif directive by not changing if_stack either.
1058 * see the comment above do_else.
1061 static void
1062 do_elif (pfile)
1063 cpp_reader *pfile;
1065 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1067 if (ifs == NULL)
1069 cpp_error (pfile, "#elif without #if");
1070 return;
1072 if (ifs->type == T_ELSE)
1074 cpp_error (pfile, "#elif after #else");
1075 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1076 "the conditional began here");
1079 ifs->type = T_ELIF;
1080 if (ifs->was_skipping)
1081 return; /* Don't evaluate a nested #if */
1083 if (pfile->skipping != 1)
1085 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1086 return;
1089 pfile->skipping = ! _cpp_parse_expr (pfile);
1092 /* #endif pops the if stack and resets pfile->skipping. */
1094 static void
1095 do_endif (pfile)
1096 cpp_reader *pfile;
1098 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1100 validate_else (pfile, dtable[T_ENDIF].name);
1102 if (ifs == NULL)
1103 cpp_error (pfile, "#endif without #if");
1104 else
1106 CPP_BUFFER (pfile)->if_stack = ifs->next;
1107 pfile->skipping = ifs->was_skipping;
1108 pfile->potential_control_macro = ifs->cmacro;
1109 obstack_free (pfile->buffer_ob, ifs);
1114 /* Push an if_stack entry and set pfile->skipping accordingly.
1115 If this is a #ifndef starting at the beginning of a file,
1116 CMACRO is the macro name tested by the #ifndef. */
1118 static void
1119 push_conditional (pfile, skip, type, cmacro)
1120 cpp_reader *pfile;
1121 int skip;
1122 int type;
1123 const cpp_hashnode *cmacro;
1125 struct if_stack *ifs;
1127 ifs = xobnew (pfile->buffer_ob, struct if_stack);
1128 ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
1129 ifs->next = CPP_BUFFER (pfile)->if_stack;
1130 ifs->cmacro = cmacro;
1131 ifs->was_skipping = pfile->skipping;
1132 ifs->type = type;
1134 if (!pfile->skipping)
1135 pfile->skipping = skip;
1137 CPP_BUFFER (pfile)->if_stack = ifs;
1140 /* Issue -pedantic warning for text which is not a comment following
1141 an #else or #endif. */
1143 static void
1144 validate_else (pfile, directive)
1145 cpp_reader *pfile;
1146 const U_CHAR *directive;
1148 if (CPP_PEDANTIC (pfile) && _cpp_get_token (pfile)->type != CPP_EOF)
1149 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1152 /* Called when we reach the end of a file. Walk back up the
1153 conditional stack till we reach its level at entry to this file,
1154 issuing error messages. Then force skipping off. */
1155 static void
1156 unwind_if_stack (pfile, pbuf)
1157 cpp_reader *pfile;
1158 cpp_buffer *pbuf;
1160 struct if_stack *ifs, *nifs;
1162 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1164 cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
1165 dtable[ifs->type].name);
1166 nifs = ifs->next;
1167 /* No need to free - they'll all go away with the buffer. */
1169 pfile->skipping = 0;
1172 /* Parses an assertion, returning a pointer to the hash node of the
1173 predicate, or 0 on error. If an answer was supplied, it is
1174 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1175 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1176 in a #if statement (we may be in a macro), and we don't want to
1177 macro expand. */
1178 cpp_hashnode *
1179 _cpp_parse_assertion (pfile, answerp)
1180 cpp_reader *pfile;
1181 struct answer **answerp;
1183 struct answer *answer = 0;
1184 cpp_toklist *list;
1185 U_CHAR *sym;
1186 const cpp_token *token, *predicate;
1187 const struct directive *d = pfile->token_list.directive;
1188 unsigned int len = 0;
1190 predicate = _cpp_get_raw_token (pfile);
1191 if (predicate->type == CPP_EOF)
1193 cpp_error (pfile, "assertion without predicate");
1194 return 0;
1196 else if (predicate->type != CPP_NAME)
1198 cpp_error (pfile, "predicate must be an identifier");
1199 return 0;
1202 token = _cpp_get_raw_token (pfile);
1203 if (token->type != CPP_OPEN_PAREN)
1205 /* #unassert and #if are OK without predicate. */
1206 if (d == &dtable[T_UNASSERT])
1208 if (token->type == CPP_EOF)
1209 goto lookup_node;
1211 else if (d != &dtable[T_ASSERT])
1213 _cpp_push_token (pfile, token);
1214 goto lookup_node;
1216 cpp_error (pfile, "missing '(' after predicate");
1217 return 0;
1220 /* Allocate a struct answer, and copy the answer to it. */
1221 answer = (struct answer *) xmalloc (sizeof (struct answer));
1222 list = &answer->list;
1223 _cpp_init_toklist (list, NO_DUMMY_TOKEN);
1225 for (;;)
1227 cpp_token *dest;
1229 token = _cpp_get_raw_token (pfile);
1231 if (token->type == CPP_EOF)
1233 cpp_error (pfile, "missing ')' to complete answer");
1234 goto error;
1236 if (token->type == CPP_CLOSE_PAREN)
1237 break;
1239 /* Copy the token. */
1240 _cpp_expand_token_space (list, 1);
1241 dest = &list->tokens[list->tokens_used++];
1242 *dest = *token;
1244 if (TOKEN_SPELL (token) == SPELL_STRING)
1246 _cpp_expand_name_space (list, token->val.str.len);
1247 dest->val.str.text = list->namebuf + list->name_used;
1248 memcpy (list->namebuf + list->name_used,
1249 token->val.str.text, token->val.str.len);
1250 list->name_used += token->val.str.len;
1254 if (list->tokens_used == 0)
1256 cpp_error (pfile, "predicate's answer is empty");
1257 goto error;
1260 /* Drop whitespace at start. */
1261 list->tokens[0].flags &= ~PREV_WHITE;
1263 if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1264 && token[1].type != CPP_EOF)
1266 cpp_error (pfile, "junk at end of assertion");
1267 goto error;
1270 lookup_node:
1271 *answerp = answer;
1272 len = predicate->val.node->length;
1273 sym = alloca (len + 1);
1275 /* Prefix '#' to get it out of macro namespace. */
1276 sym[0] = '#';
1277 memcpy (sym + 1, predicate->val.node->name, len);
1278 return cpp_lookup (pfile, sym, len + 1);
1280 error:
1281 FREE_ANSWER (answer);
1282 return 0;
1285 /* Returns a pointer to the pointer to the answer in the answer chain,
1286 or a pointer to NULL if the answer is not in the chain. */
1287 struct answer **
1288 _cpp_find_answer (node, candidate)
1289 cpp_hashnode *node;
1290 const cpp_toklist *candidate;
1292 struct answer **result;
1294 for (result = &node->value.answers; *result; result = &(*result)->next)
1295 if (_cpp_equiv_toklists (&(*result)->list, candidate))
1296 break;
1298 return result;
1301 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1302 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1303 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1304 static void
1305 do_assert (pfile)
1306 cpp_reader *pfile;
1308 struct answer *new_answer;
1309 cpp_hashnode *node;
1311 node = _cpp_parse_assertion (pfile, &new_answer);
1312 if (node)
1314 new_answer->next = 0;
1315 new_answer->list.line = pfile->token_list.line;
1316 new_answer->list.file = pfile->token_list.file;
1318 if (node->type == T_ASSERTION)
1320 if (*_cpp_find_answer (node, &new_answer->list))
1321 goto err;
1322 new_answer->next = node->value.answers;
1324 node->type = T_ASSERTION;
1325 node->value.answers = new_answer;
1327 return;
1329 err:
1330 cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
1331 FREE_ANSWER (new_answer);
1334 static void
1335 do_unassert (pfile)
1336 cpp_reader *pfile;
1338 cpp_hashnode *node;
1339 struct answer *answer, *temp, *next;
1341 node = _cpp_parse_assertion (pfile, &answer);
1342 if (node)
1344 /* It isn't an error to #unassert something that isn't asserted. */
1345 if (node->type == T_ASSERTION)
1347 if (answer)
1349 struct answer **p = _cpp_find_answer (node, &answer->list);
1351 temp = *p;
1352 if (temp)
1354 *p = temp->next;
1355 FREE_ANSWER (temp);
1357 if (node->value.answers == 0)
1358 node->type = T_VOID;
1360 else
1362 for (temp = node->value.answers; temp; temp = next)
1364 next = temp->next;
1365 FREE_ANSWER (temp);
1367 node->type = T_VOID;
1371 if (answer)
1372 FREE_ANSWER (answer);
1376 /* These are for -D, -U, -A. */
1378 /* Process the string STR as if it appeared as the body of a #define.
1379 If STR is just an identifier, define it with value 1.
1380 If STR has anything after the identifier, then it should
1381 be identifier=definition. */
1383 void
1384 cpp_define (pfile, str)
1385 cpp_reader *pfile;
1386 const char *str;
1388 char *buf, *p;
1389 size_t count;
1391 p = strchr (str, '=');
1392 /* Copy the entire option so we can modify it.
1393 Change the first "=" in the string to a space. If there is none,
1394 tack " 1" on the end. Then add a newline and a NUL. */
1396 if (p)
1398 count = strlen (str) + 2;
1399 buf = (char *) alloca (count);
1400 memcpy (buf, str, count - 2);
1401 buf[p - str] = ' ';
1402 buf[count - 2] = '\n';
1403 buf[count - 1] = '\0';
1405 else
1407 count = strlen (str) + 4;
1408 buf = (char *) alloca (count);
1409 memcpy (buf, str, count - 4);
1410 strcpy (&buf[count-4], " 1\n");
1413 _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1);
1416 /* Process MACRO as if it appeared as the body of an #undef. */
1417 void
1418 cpp_undef (pfile, macro)
1419 cpp_reader *pfile;
1420 const char *macro;
1422 _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro));
1425 /* Process the string STR as if it appeared as the body of a #assert. */
1426 void
1427 cpp_assert (pfile, str)
1428 cpp_reader *pfile;
1429 const char *str;
1431 _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str));
1434 /* Process STR as if it appeared as the body of an #unassert. */
1435 void
1436 cpp_unassert (pfile, str)
1437 cpp_reader *pfile;
1438 const char *str;
1440 _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str));
1443 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1445 cpp_defined (pfile, id, len)
1446 cpp_reader *pfile;
1447 const U_CHAR *id;
1448 int len;
1450 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1451 if (hp->type == T_POISON)
1453 cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
1454 return 0;
1456 return (hp->type != T_VOID);
1459 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1460 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1461 as the new input buffer.
1462 Return the new buffer, or NULL on failure. */
1464 cpp_buffer *
1465 cpp_push_buffer (pfile, buffer, length)
1466 cpp_reader *pfile;
1467 const U_CHAR *buffer;
1468 long length;
1470 cpp_buffer *buf = CPP_BUFFER (pfile);
1471 cpp_buffer *new;
1472 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1474 cpp_fatal (pfile, "#include nested too deep");
1475 return NULL;
1477 if (pfile->cur_context > 0)
1479 cpp_ice (pfile, "buffer pushed with contexts stacked");
1480 _cpp_skip_rest_of_line (pfile);
1483 new = xobnew (pfile->buffer_ob, cpp_buffer);
1484 memset (new, 0, sizeof (cpp_buffer));
1486 new->line_base = new->buf = new->cur = buffer;
1487 new->rlimit = buffer + length;
1488 new->prev = buf;
1490 CPP_BUFFER (pfile) = new;
1491 return new;
1494 cpp_buffer *
1495 cpp_pop_buffer (pfile)
1496 cpp_reader *pfile;
1498 int wfb;
1499 cpp_buffer *buf = CPP_BUFFER (pfile);
1501 unwind_if_stack (pfile, buf);
1502 wfb = (buf->inc != 0);
1503 if (wfb)
1504 _cpp_pop_file_buffer (pfile, buf);
1506 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1507 obstack_free (pfile->buffer_ob, buf);
1508 pfile->buffer_stack_depth--;
1510 if (wfb && pfile->cb.leave_file && CPP_BUFFER (pfile))
1511 (*pfile->cb.leave_file) (pfile);
1513 return CPP_BUFFER (pfile);
1516 #define obstack_chunk_alloc xmalloc
1517 #define obstack_chunk_free free
1518 #define DSC(x) U x, sizeof x - 1
1519 void
1520 _cpp_init_stacks (pfile)
1521 cpp_reader *pfile;
1523 int i;
1524 struct spec_nodes *s;
1526 pfile->buffer_ob = xnew (struct obstack);
1527 obstack_init (pfile->buffer_ob);
1529 /* Perhaps not the ideal place to put this. */
1530 pfile->spec_nodes = s = xnew (struct spec_nodes);
1531 s->n_L = cpp_lookup (pfile, DSC("L"));
1532 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
1533 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
1534 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
1535 for (i = 0; i < N_DIRECTIVES; i++)
1536 s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1539 void
1540 _cpp_cleanup_stacks (pfile)
1541 cpp_reader *pfile;
1543 obstack_free (pfile->buffer_ob, 0);
1544 free (pfile->buffer_ob);