* cppfiles.c (cpp_make_system_header): New function.
[official-gcc.git] / gcc / cpplib.c
blob93798fac36faaf607096a8dbd8d9732d326199ba
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 "symcat.h"
30 /* `struct directive' defines one #-directive, including how to handle it. */
32 struct directive
34 directive_handler func; /* Function to handle directive. */
35 const U_CHAR *name; /* Name of directive. */
36 unsigned short length; /* Length of name. */
37 unsigned short flags; /* Flags describing this directive. */
40 /* Stack of conditionals currently in progress
41 (including both successful and failing conditionals). */
43 struct if_stack
45 struct if_stack *next;
46 int lineno; /* line number where condition started */
47 int was_skipping; /* value of pfile->skipping before this if */
48 const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */
49 int type; /* type of last directive seen in this group */
52 /* Forward declarations. */
54 static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
55 static unsigned int parse_include PARAMS ((cpp_reader *, const U_CHAR *));
56 static void push_conditional PARAMS ((cpp_reader *, int, int,
57 const cpp_hashnode *));
58 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
59 cpp_reader *, int));
60 static int read_line_number PARAMS ((cpp_reader *, int *));
61 static const cpp_hashnode *parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
62 static const cpp_hashnode *detect_if_not_defined PARAMS ((cpp_reader *));
64 /* Values for the flags field of the table below. KANDR and COND
65 directives come from traditional (K&R) C. The difference is, if we
66 care about it while skipping a failed conditional block, its origin
67 is COND. STDC89 directives come from the 1989 C standard.
68 EXTENSION directives are extensions, with origins noted below. */
70 #define KANDR 0
71 #define COND 1
72 #define STDC89 2
73 #define EXTENSION 3
75 #define ORIGIN_MASK 3
76 #define ORIGIN(f) ((f) & ORIGIN_MASK)
77 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
79 /* This is the table of directive handlers. It is ordered by
80 frequency of occurrence; the numbers at the end are directive
81 counts from all the source code I have lying around (egcs and libc
82 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
83 pcmcia-cs-3.0.9).
85 The entries with a dash and a name after the count are extensions,
86 of which all but #warning and #include_next are deprecated. The name
87 is where the extension appears to have come from. */
89 /* #sccs is not always recognized. */
90 #ifdef SCCS_DIRECTIVE
91 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
92 #else
93 # define SCCS_ENTRY /* nothing */
94 #endif
96 #define DIRECTIVE_TABLE \
97 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
98 D(include, T_INCLUDE, KANDR | SYNTAX_INCLUDE) /* 52262 */ \
99 D(endif, T_ENDIF, COND) /* 45855 */ \
100 D(ifdef, T_IFDEF, COND) /* 22000 */ \
101 D(if, T_IF, COND) /* 18162 */ \
102 D(else, T_ELSE, COND) /* 9863 */ \
103 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
104 D(undef, T_UNDEF, KANDR) /* 4837 */ \
105 D(line, T_LINE, KANDR) /* 2465 */ \
106 D(elif, T_ELIF, COND) /* 610 */ \
107 D(error, T_ERROR, STDC89) /* 475 */ \
108 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
109 D(warning, T_WARNING, EXTENSION) /* 22 GNU */ \
110 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /* 19 GNU */ \
111 D(ident, T_IDENT, EXTENSION) /* 11 SVR4 */ \
112 D(import, T_IMPORT, EXTENSION | SYNTAX_INCLUDE) /* 0 ObjC */ \
113 D(assert, T_ASSERT, EXTENSION) /* 0 SVR4 */ \
114 D(unassert, T_UNASSERT, EXTENSION) /* 0 SVR4 */ \
115 SCCS_ENTRY
117 /* Use the table to generate a series of prototypes, an enum for the
118 directive names, and an array of directive handlers. */
120 /* The directive-processing functions are declared to return int
121 instead of void, because some old compilers have trouble with
122 pointers to functions returning void. */
124 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
125 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
126 DIRECTIVE_TABLE
127 #undef D
129 #define D(n, tag, f) tag,
130 enum
132 DIRECTIVE_TABLE
133 N_DIRECTIVES
135 #undef D
137 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
138 #define D(name, t, flags) \
139 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
140 sizeof STRINGX(name) - 1, flags },
141 static const struct directive dtable[] =
143 DIRECTIVE_TABLE
145 #undef D
146 #undef DIRECTIVE_TABLE
148 /* Check if a token's name matches that of a known directive. Put in
149 this file to save exporting dtable and other unneeded information. */
150 void
151 _cpp_check_directive (list, token)
152 cpp_toklist *list;
153 cpp_token *token;
155 const U_CHAR *name = token->val.name.text;
156 size_t len = token->val.name.len;
157 unsigned int i;
159 list->dirno = -1;
160 list->flags &= ~SYNTAX_INCLUDE;
162 for (i = 0; i < N_DIRECTIVES; i++)
163 if (dtable[i].length == len && !ustrncmp (dtable[i].name, name, len))
165 list->dirno = i;
166 if (dtable[i].flags & SYNTAX_INCLUDE)
167 list->flags |= SYNTAX_INCLUDE;
168 break;
172 /* Handle a possible # directive.
173 '#' has already been read. */
176 _cpp_handle_directive (pfile)
177 cpp_reader *pfile;
179 int i;
180 int hash_at_bol;
181 unsigned int len;
182 U_CHAR *ident;
183 long old_written = CPP_WRITTEN (pfile);
184 enum cpp_ttype tok;
186 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
188 cpp_ice (pfile, "handle_directive called on macro buffer");
189 return 0;
192 /* -traditional directives are recognized only with the # in column 1. */
193 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
195 /* Scan the next token, then pretend we didn't. */
196 CPP_SET_MARK (pfile);
197 pfile->no_macro_expand++;
198 tok = _cpp_get_directive_token (pfile);
199 pfile->no_macro_expand--;
201 ident = pfile->token_buffer + old_written;
202 len = CPP_PWRITTEN (pfile) - ident;
203 CPP_SET_WRITTEN (pfile, old_written);
204 CPP_GOTO_MARK (pfile);
206 /* # followed by a number is equivalent to #line. Do not recognize
207 this form in assembly language source files or skipped
208 conditional groups. Complain about this form if we're being
209 pedantic, but not if this is regurgitated input (preprocessed or
210 fed back in by the C++ frontend). */
211 if (tok == CPP_NUMBER)
213 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
214 return 0;
216 if (CPP_PEDANTIC (pfile)
217 && CPP_BUFFER (pfile)->inc
218 && ! CPP_OPTION (pfile, preprocessed))
219 cpp_pedwarn (pfile, "# followed by integer");
220 i = T_LINE;
221 goto process_directive;
224 /* If we are rescanning preprocessed input, don't obey any directives
225 other than # nnn. */
226 else if (CPP_OPTION (pfile, preprocessed))
227 return 0;
229 /* A line of just # becomes blank. */
230 else if (tok == CPP_VSPACE)
231 return 1;
233 /* A NAME token might in fact be a directive! */
234 else if (tok == CPP_NAME)
236 for (i = 0; i < N_DIRECTIVES; i++)
238 if (dtable[i].length == len
239 && !ustrncmp (dtable[i].name, ident, len))
240 goto real_directive;
242 /* Don't complain about invalid directives in assembly source,
243 we don't know where the comments are, and # may introduce
244 assembler pseudo-ops. Don't complain about invalid directives
245 in skipped conditional groups (6.10 p4). */
246 if (!pfile->skipping && !CPP_OPTION (pfile, lang_asm))
247 cpp_error (pfile, "invalid preprocessing directive #%.*s",
248 (int) len, ident);
249 return 0;
251 /* And anything else means the # wasn't a directive marker. */
252 else
253 return 0;
255 real_directive:
257 /* If we are skipping a failed conditional group, all non-conditional
258 directives are ignored. */
259 if (pfile->skipping && ORIGIN (dtable[i].flags) != COND)
260 return 0;
262 /* In -traditional mode, a directive is ignored unless its # is in
263 column 1. */
264 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
266 if (CPP_WTRADITIONAL (pfile))
267 cpp_warning (pfile, "ignoring #%s because of its indented #",
268 dtable[i].name);
269 return 0;
272 /* no_directives is set when we are parsing macro arguments. Directives
273 in macro arguments are undefined behavior (C99 6.10.3.11); this
274 implementation chooses to make them hard errors. */
275 if (pfile->no_directives)
277 cpp_error (pfile, "#%s may not be used inside a macro argument",
278 dtable[i].name);
279 _cpp_skip_rest_of_line (pfile);
280 return 1;
283 /* Issue -pedantic warnings for extended directives. */
284 if (CPP_PEDANTIC (pfile) && ORIGIN (dtable[i].flags) == EXTENSION)
285 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
287 /* -Wtraditional gives warnings about directives with inappropriate
288 indentation of #. */
289 if (CPP_WTRADITIONAL (pfile))
291 if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].flags))
292 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
293 dtable[i].name);
294 else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].flags))
295 cpp_warning (pfile,
296 "suggest hiding #%s from traditional C with an indented #",
297 dtable[i].name);
300 /* Unfortunately, it's necessary to scan the directive name again,
301 now we know we're going to consume it. FIXME. */
303 pfile->no_macro_expand++;
304 _cpp_get_directive_token (pfile);
305 pfile->no_macro_expand--;
306 CPP_SET_WRITTEN (pfile, old_written);
308 process_directive:
309 (void) (*dtable[i].func) (pfile);
310 return 1;
313 /* Pass a directive through to the output file.
314 BUF points to the contents of the directive, as a contiguous string.
315 LEN is the length of the string pointed to by BUF.
316 KEYWORD is the keyword-table entry for the directive. */
318 static void
319 pass_thru_directive (buf, len, pfile, keyword)
320 const U_CHAR *buf;
321 size_t len;
322 cpp_reader *pfile;
323 int keyword;
325 const struct directive *kt = &dtable[keyword];
326 register unsigned klen = kt->length;
328 CPP_RESERVE (pfile, 1 + klen + len);
329 CPP_PUTC_Q (pfile, '#');
330 CPP_PUTS_Q (pfile, kt->name, klen);
331 if (len != 0 && buf[0] != ' ')
332 CPP_PUTC_Q (pfile, ' ');
333 CPP_PUTS_Q (pfile, buf, len);
336 /* Process a #define command. */
338 static int
339 do_define (pfile)
340 cpp_reader *pfile;
342 cpp_hashnode *node;
343 int len;
344 const U_CHAR *sym;
345 cpp_toklist *list = &pfile->directbuf;
347 pfile->no_macro_expand++;
348 CPP_OPTION (pfile, discard_comments)++;
350 _cpp_scan_until (pfile, list, CPP_VSPACE);
352 /* First token on the line must be a NAME. There may not be any
353 tokens in the list (if we had #define all by itself on a line). */
354 if (list->tokens_used == 0
355 || TOK_TYPE (list, 0) != CPP_NAME)
357 cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
358 "#define must be followed by an identifier");
359 goto out;
362 sym = TOK_NAME (list, 0);
363 len = TOK_LEN (list, 0);
365 /* That NAME is not allowed to be "defined". (Not clear if the
366 standard requires this.) */
367 if (len == 7 && !ustrncmp (sym, U"defined", 7))
369 cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
370 "\"defined\" is not a legal macro name");
371 goto out;
374 node = cpp_lookup (pfile, sym, len);
375 /* Check for poisoned identifiers now. All other checks
376 are done in cpphash.c. */
377 if (node->type == T_POISON)
379 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
380 goto out;
383 if (_cpp_create_definition (pfile, list, node) == 0)
384 goto out;
386 if (CPP_OPTION (pfile, debug_output)
387 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
388 _cpp_dump_definition (pfile, node);
389 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
390 pass_thru_directive (sym, len, pfile, T_DEFINE);
392 out:
393 pfile->no_macro_expand--;
394 CPP_OPTION (pfile, discard_comments)--;
395 return 0;
398 /* Handle #include and #import. */
400 static unsigned int
401 parse_include (pfile, name)
402 cpp_reader *pfile;
403 const U_CHAR *name;
405 long old_written = CPP_WRITTEN (pfile);
406 enum cpp_ttype token;
407 int len;
409 pfile->parsing_include_directive++;
410 token = _cpp_get_directive_token (pfile);
411 pfile->parsing_include_directive--;
413 len = CPP_WRITTEN (pfile) - old_written;
415 if (token != CPP_STRING)
417 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", name);
418 CPP_SET_WRITTEN (pfile, old_written);
419 _cpp_skip_rest_of_line (pfile);
420 return 0;
423 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
425 cpp_error (pfile, "junk at end of #%s", name);
426 _cpp_skip_rest_of_line (pfile);
429 CPP_SET_WRITTEN (pfile, old_written);
431 if (len == 0)
432 cpp_error (pfile, "empty file name in #%s", name);
434 return len;
437 static int
438 do_include (pfile)
439 cpp_reader *pfile;
441 unsigned int len;
442 U_CHAR *token;
444 len = parse_include (pfile, dtable[T_INCLUDE].name);
445 if (len == 0)
446 return 0;
447 token = (U_CHAR *) alloca (len + 1);
448 memcpy (token, CPP_PWRITTEN (pfile), len);
449 token[len] = '\0';
451 if (CPP_OPTION (pfile, dump_includes))
452 pass_thru_directive (token, len, pfile, T_INCLUDE);
454 _cpp_execute_include (pfile, token, len, 0, 0);
455 return 0;
458 static int
459 do_import (pfile)
460 cpp_reader *pfile;
462 unsigned int len;
463 U_CHAR *token;
465 if (CPP_OPTION (pfile, warn_import)
466 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
468 pfile->import_warning = 1;
469 cpp_warning (pfile,
470 "#import is obsolete, use an #ifndef wrapper in the header file");
473 len = parse_include (pfile, dtable[T_IMPORT].name);
474 if (len == 0)
475 return 0;
476 token = (U_CHAR *) alloca (len + 1);
477 memcpy (token, CPP_PWRITTEN (pfile), len);
478 token[len] = '\0';
480 if (CPP_OPTION (pfile, dump_includes))
481 pass_thru_directive (token, len, pfile, T_IMPORT);
483 _cpp_execute_include (pfile, token, len, 1, 0);
484 return 0;
487 static int
488 do_include_next (pfile)
489 cpp_reader *pfile;
491 unsigned int len;
492 U_CHAR *token;
493 struct file_name_list *search_start = 0;
495 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
496 if (len == 0)
497 return 0;
498 token = (U_CHAR *) alloca (len + 1);
499 memcpy (token, CPP_PWRITTEN (pfile), len);
500 token[len] = '\0';
502 if (CPP_OPTION (pfile, dump_includes))
503 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
505 /* For #include_next, skip in the search path past the dir in which the
506 containing file was found. Treat files specified using an absolute path
507 as if there are no more directories to search. Treat the primary source
508 file like any other included source, but generate a warning. */
509 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
511 if (CPP_BUFFER (pfile)->inc->foundhere)
512 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
514 else
515 cpp_warning (pfile, "#include_next in primary source file");
517 _cpp_execute_include (pfile, token, len, 0, search_start);
518 return 0;
521 /* Subroutine of do_line. Read next token from PFILE without adding it to
522 the output buffer. If it is a number between 1 and 4, store it in *NUM
523 and return 1; otherwise, return 0 and complain if we aren't at the end
524 of the directive. */
526 static int
527 read_line_number (pfile, num)
528 cpp_reader *pfile;
529 int *num;
531 long save_written = CPP_WRITTEN (pfile);
532 U_CHAR *p;
533 enum cpp_ttype token = _cpp_get_directive_token (pfile);
534 p = pfile->token_buffer + save_written;
536 if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
537 && p[0] >= '1' && p[0] <= '4')
539 *num = p[0] - '0';
540 CPP_SET_WRITTEN (pfile, save_written);
541 return 1;
543 else
545 if (token != CPP_VSPACE && token != CPP_EOF)
546 cpp_error (pfile, "invalid format #line");
547 CPP_SET_WRITTEN (pfile, save_written);
548 return 0;
552 /* Interpret #line command.
553 Note that the filename string (if any) is treated as if it were an
554 include filename. That means no escape handling. */
556 static int
557 do_line (pfile)
558 cpp_reader *pfile;
560 cpp_buffer *ip = CPP_BUFFER (pfile);
561 unsigned int new_lineno;
562 long old_written = CPP_WRITTEN (pfile);
563 enum cpp_ttype token;
564 char *x;
566 token = _cpp_get_directive_token (pfile);
568 if (token != CPP_NUMBER)
570 cpp_error (pfile, "token after #line is not an integer");
571 goto bad_line_directive;
574 CPP_PUTC (pfile, '\0'); /* not terminated for us */
575 new_lineno = strtoul ((const char *) (pfile->token_buffer + old_written),
576 &x, 10);
577 if (x[0] != '\0')
579 cpp_error (pfile, "token after #line is not an integer");
580 goto bad_line_directive;
582 CPP_SET_WRITTEN (pfile, old_written);
584 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
585 cpp_pedwarn (pfile, "line number out of range in #line");
587 token = _cpp_get_directive_token (pfile);
589 if (token == CPP_STRING)
591 U_CHAR *fname = pfile->token_buffer + old_written + 1;
592 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
593 int action_number = 0;
595 if (read_line_number (pfile, &action_number))
597 if (CPP_PEDANTIC (pfile))
598 cpp_pedwarn (pfile, "garbage at end of #line");
600 /* This is somewhat questionable: change the buffer stack
601 depth so that output_line_command thinks we've stacked
602 another buffer. */
603 if (action_number == 1)
605 pfile->buffer_stack_depth++;
606 cpp_make_system_header (pfile, ip, 0);
607 read_line_number (pfile, &action_number);
609 else if (action_number == 2)
611 pfile->buffer_stack_depth--;
612 cpp_make_system_header (pfile, ip, 0);
613 read_line_number (pfile, &action_number);
615 if (action_number == 3)
617 cpp_make_system_header (pfile, ip, 1);
618 read_line_number (pfile, &action_number);
620 if (action_number == 4)
622 cpp_make_system_header (pfile, ip, 2);
623 read_line_number (pfile, &action_number);
627 *end_name = '\0';
629 if (strcmp ((const char *)fname, ip->nominal_fname))
631 if (!strcmp ((const char *)fname, ip->inc->name))
632 ip->nominal_fname = ip->inc->name;
633 else
634 ip->nominal_fname = _cpp_fake_include (pfile, (const char *)fname);
637 else if (token != CPP_VSPACE && token != CPP_EOF)
639 cpp_error (pfile, "second token after #line is not a string");
640 goto bad_line_directive;
643 /* The Newline at the end of this line remains to be processed.
644 To put the next line at the specified line number,
645 we must store a line number now that is one less. */
646 ip->lineno = new_lineno - 1;
647 CPP_SET_WRITTEN (pfile, old_written);
648 return 0;
650 bad_line_directive:
651 _cpp_skip_rest_of_line (pfile);
652 CPP_SET_WRITTEN (pfile, old_written);
653 return 0;
656 /* Remove the definition of a symbol from the symbol table.
657 According to the C standard, it is not an error to undef
658 something that has no definitions. */
659 static int
660 do_undef (pfile)
661 cpp_reader *pfile;
663 int len;
664 cpp_hashnode *hp;
665 U_CHAR *name;
666 long here = CPP_WRITTEN (pfile);
667 enum cpp_ttype token;
669 pfile->no_macro_expand++;
670 token = _cpp_get_directive_token (pfile);
671 pfile->no_macro_expand--;
673 if (token != CPP_NAME)
675 cpp_error (pfile, "token after #undef is not an identifier");
676 _cpp_skip_rest_of_line (pfile);
677 return 0;
679 len = CPP_WRITTEN (pfile) - here;
681 token = _cpp_get_directive_token (pfile);
682 if (token != CPP_VSPACE)
684 cpp_pedwarn (pfile, "junk on line after #undef");
685 _cpp_skip_rest_of_line (pfile);
688 name = pfile->token_buffer + here;
689 CPP_SET_WRITTEN (pfile, here);
691 hp = cpp_lookup (pfile, name, len);
692 if (hp->type == T_VOID)
693 ; /* Not defined in the first place - do nothing. */
694 else if (hp->type == T_POISON)
695 cpp_error (pfile, "cannot undefine poisoned \"%s\"", hp->name);
696 else
698 /* If we are generating additional info for debugging (with -g) we
699 need to pass through all effective #undef commands. */
700 if (CPP_OPTION (pfile, debug_output))
701 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
703 if (hp->type != T_MACRO && hp->type != T_FMACRO
704 && hp->type != T_EMPTY && hp->type != T_IDENTITY)
705 cpp_warning (pfile, "undefining `%s'", hp->name);
707 _cpp_free_definition (hp);
708 hp->type = T_VOID;
711 return 0;
715 * Report an error detected by the program we are processing.
716 * Use the text of the line in the error message.
717 * (We use error because it prints the filename & line#.)
720 static int
721 do_error (pfile)
722 cpp_reader *pfile;
724 const U_CHAR *text, *limit;
726 _cpp_skip_hspace (pfile);
727 text = CPP_BUFFER (pfile)->cur;
728 _cpp_skip_rest_of_line (pfile);
729 limit = CPP_BUFFER (pfile)->cur;
731 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
732 return 0;
736 * Report a warning detected by the program we are processing.
737 * Use the text of the line in the warning message, then continue.
740 static int
741 do_warning (pfile)
742 cpp_reader *pfile;
744 const U_CHAR *text, *limit;
746 _cpp_skip_hspace (pfile);
747 text = CPP_BUFFER (pfile)->cur;
748 _cpp_skip_rest_of_line (pfile);
749 limit = CPP_BUFFER (pfile)->cur;
751 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
752 return 0;
755 /* Report program identification. */
757 static int
758 do_ident (pfile)
759 cpp_reader *pfile;
761 long old_written = CPP_WRITTEN (pfile);
763 CPP_PUTS (pfile, "#ident ", 7);
765 /* Next token should be a string constant. */
766 if (_cpp_get_directive_token (pfile) == CPP_STRING)
767 /* And then a newline. */
768 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
769 /* Good - ship it. */
770 return 0;
772 cpp_error (pfile, "invalid #ident");
773 _cpp_skip_rest_of_line (pfile);
774 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
776 return 0;
779 /* Pragmata handling. We handle some of these, and pass the rest on
780 to the front end. C99 defines three pragmas and says that no macro
781 expansion is to be performed on them; whether or not macro
782 expansion happens for other pragmas is implementation defined.
783 This implementation never macro-expands the text after #pragma.
785 We currently do not support the _Pragma operator. Support for that
786 has to be coordinated with the front end. Proposed implementation:
787 both #pragma blah blah and _Pragma("blah blah") become
788 __builtin_pragma(blah blah) and we teach the parser about that. */
790 /* Sub-handlers for the pragmas needing treatment here.
791 They return 1 if the token buffer is to be popped, 0 if not. */
792 static int do_pragma_once PARAMS ((cpp_reader *));
793 static int do_pragma_implementation PARAMS ((cpp_reader *));
794 static int do_pragma_poison PARAMS ((cpp_reader *));
795 static int do_pragma_system_header PARAMS ((cpp_reader *));
796 static int do_pragma_default PARAMS ((cpp_reader *));
798 static int
799 do_pragma (pfile)
800 cpp_reader *pfile;
802 long here, key;
803 U_CHAR *buf;
804 int pop;
805 enum cpp_ttype token;
807 here = CPP_WRITTEN (pfile);
808 CPP_PUTS (pfile, "#pragma ", 8);
810 key = CPP_WRITTEN (pfile);
811 pfile->no_macro_expand++;
812 token = _cpp_get_directive_token (pfile);
813 if (token != CPP_NAME)
815 if (token == CPP_VSPACE)
816 goto empty;
817 else
818 goto skip;
821 buf = pfile->token_buffer + key;
822 CPP_PUTC (pfile, ' ');
824 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
825 if (tokis ("once"))
826 pop = do_pragma_once (pfile);
827 else if (tokis ("implementation"))
828 pop = do_pragma_implementation (pfile);
829 else if (tokis ("poison"))
830 pop = do_pragma_poison (pfile);
831 else if (tokis ("system_header"))
832 pop = do_pragma_system_header (pfile);
833 else
834 pop = do_pragma_default (pfile);
835 #undef tokis
837 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
838 goto skip;
840 if (pop)
841 CPP_SET_WRITTEN (pfile, here);
842 pfile->no_macro_expand--;
843 return 0;
845 skip:
846 cpp_error (pfile, "malformed #pragma directive");
847 _cpp_skip_rest_of_line (pfile);
848 empty:
849 CPP_SET_WRITTEN (pfile, here);
850 pfile->no_macro_expand--;
851 return 0;
854 static int
855 do_pragma_default (pfile)
856 cpp_reader *pfile;
858 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
859 CPP_PUTC (pfile, ' ');
860 return 0;
863 static int
864 do_pragma_once (pfile)
865 cpp_reader *pfile;
867 cpp_buffer *ip = CPP_BUFFER (pfile);
869 /* Allow #pragma once in system headers, since that's not the user's
870 fault. */
871 if (!CPP_IN_SYSTEM_HEADER (pfile))
872 cpp_warning (pfile, "#pragma once is obsolete");
874 if (CPP_PREV_BUFFER (ip) == NULL)
875 cpp_warning (pfile, "#pragma once outside include file");
876 else
877 ip->inc->cmacro = NEVER_REREAD;
879 return 1;
882 static int
883 do_pragma_implementation (pfile)
884 cpp_reader *pfile;
886 /* Be quiet about `#pragma implementation' for a file only if it hasn't
887 been included yet. */
888 enum cpp_ttype token;
889 long written = CPP_WRITTEN (pfile);
890 U_CHAR *name;
891 char *copy;
892 size_t len;
894 token = _cpp_get_directive_token (pfile);
895 if (token == CPP_VSPACE)
896 return 0;
897 else if (token != CPP_STRING)
899 cpp_error (pfile, "malformed #pragma implementation");
900 return 1;
903 /* Trim the leading and trailing quote marks from the string. */
904 name = pfile->token_buffer + written + 1;
905 len = CPP_PWRITTEN (pfile) - name;
906 copy = alloca (len);
907 memcpy (copy, name, len - 1);
908 copy[len - 1] = '\0';
910 if (cpp_included (pfile, copy))
911 cpp_warning (pfile,
912 "#pragma implementation for %s appears after file is included",
913 copy);
914 return 0;
917 static int
918 do_pragma_poison (pfile)
919 cpp_reader *pfile;
921 /* Poison these symbols so that all subsequent usage produces an
922 error message. */
923 U_CHAR *p;
924 cpp_hashnode *hp;
925 long written;
926 size_t len;
927 enum cpp_ttype token;
928 int writeit;
930 /* As a rule, don't include #pragma poison commands in output,
931 unless the user asks for them. */
932 writeit = (CPP_OPTION (pfile, debug_output)
933 || CPP_OPTION (pfile, dump_macros) == dump_definitions
934 || CPP_OPTION (pfile, dump_macros) == dump_names);
936 for (;;)
938 written = CPP_WRITTEN (pfile);
939 token = _cpp_get_directive_token (pfile);
940 if (token == CPP_VSPACE)
941 break;
942 if (token != CPP_NAME)
944 cpp_error (pfile, "invalid #pragma poison directive");
945 _cpp_skip_rest_of_line (pfile);
946 return 1;
949 p = pfile->token_buffer + written;
950 len = CPP_PWRITTEN (pfile) - p;
951 hp = cpp_lookup (pfile, p, len);
952 if (hp->type == T_POISON)
953 ; /* It is allowed to poison the same identifier twice. */
954 else
956 if (hp->type != T_VOID)
957 cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
958 _cpp_free_definition (hp);
959 hp->type = T_POISON;
961 if (writeit)
962 CPP_PUTC (pfile, ' ');
964 return !writeit;
967 /* Mark the current header as a system header. This will suppress
968 some categories of warnings (notably those from -pedantic). It is
969 intended for use in system libraries that cannot be implemented in
970 conforming C, but cannot be certain that their headers appear in a
971 system include directory. To prevent abuse, it is rejected in the
972 primary source file. */
973 static int
974 do_pragma_system_header (pfile)
975 cpp_reader *pfile;
977 cpp_buffer *ip = cpp_file_buffer (pfile);
978 if (CPP_PREV_BUFFER (ip) == NULL)
979 cpp_warning (pfile, "#pragma system_header outside include file");
980 else
981 cpp_make_system_header (pfile, ip, 1);
983 return 1;
986 /* Just ignore #sccs, on systems where we define it at all. */
987 #ifdef SCCS_DIRECTIVE
988 static int
989 do_sccs (pfile)
990 cpp_reader *pfile;
992 _cpp_skip_rest_of_line (pfile);
993 return 0;
995 #endif
997 /* We've found an `#if' directive. If the only thing before it in
998 this file is white space, and if it is of the form
999 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1000 for inclusion of this file. (See redundant_include_p in cppfiles.c
1001 for an explanation of controlling macros.) If so, return the
1002 hash node for SYMBOL. Otherwise, return NULL. */
1004 static const cpp_hashnode *
1005 detect_if_not_defined (pfile)
1006 cpp_reader *pfile;
1008 const cpp_hashnode *cmacro = 0;
1009 enum cpp_ttype token;
1010 unsigned int base_offset;
1011 unsigned int token_offset;
1012 unsigned int need_rparen = 0;
1013 unsigned int token_len;
1015 if (pfile->skipping || pfile->only_seen_white != 2)
1016 return NULL;
1018 /* Save state required for restore. */
1019 pfile->no_macro_expand++;
1020 CPP_SET_MARK (pfile);
1021 base_offset = CPP_WRITTEN (pfile);
1023 /* Look for `!', */
1024 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1025 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1026 || CPP_PWRITTEN (pfile)[-1] != '!')
1027 goto restore;
1029 /* ...then `defined', */
1030 token_offset = CPP_WRITTEN (pfile);
1031 token = _cpp_get_directive_token (pfile);
1032 if (token != CPP_NAME)
1033 goto restore;
1034 if (ustrncmp (pfile->token_buffer + token_offset, U"defined", 7))
1035 goto restore;
1037 /* ...then an optional '(' and the name, */
1038 token_offset = CPP_WRITTEN (pfile);
1039 token = _cpp_get_directive_token (pfile);
1040 if (token == CPP_OPEN_PAREN)
1042 token_offset = CPP_WRITTEN (pfile);
1043 need_rparen = 1;
1044 token = _cpp_get_directive_token (pfile);
1046 if (token != CPP_NAME)
1047 goto restore;
1049 token_len = CPP_WRITTEN (pfile) - token_offset;
1051 /* ...then the ')', if necessary, */
1052 if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1053 goto restore;
1055 /* ...and make sure there's nothing else on the line. */
1056 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1057 goto restore;
1059 /* We have a legitimate controlling macro for this header. */
1060 cmacro = cpp_lookup (pfile, pfile->token_buffer + token_offset, token_len);
1062 restore:
1063 CPP_SET_WRITTEN (pfile, base_offset);
1064 pfile->no_macro_expand--;
1065 CPP_GOTO_MARK (pfile);
1067 return cmacro;
1070 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1071 not defined; the macro tested is left in the token buffer (but
1072 popped). */
1074 static const cpp_hashnode *
1075 parse_ifdef (pfile, name)
1076 cpp_reader *pfile;
1077 const U_CHAR *name;
1079 U_CHAR *ident;
1080 unsigned int len;
1081 enum cpp_ttype token;
1082 long old_written = CPP_WRITTEN (pfile);
1083 const cpp_hashnode *node = 0;
1085 pfile->no_macro_expand++;
1086 token = _cpp_get_directive_token (pfile);
1087 pfile->no_macro_expand--;
1089 ident = pfile->token_buffer + old_written;
1090 len = CPP_WRITTEN (pfile) - old_written;
1092 if (token == CPP_VSPACE)
1094 if (! CPP_TRADITIONAL (pfile))
1095 cpp_pedwarn (pfile, "#%s with no argument", name);
1096 goto done;
1098 else if (token == CPP_NAME)
1100 node = cpp_lookup (pfile, ident, len);
1102 else
1104 if (! CPP_TRADITIONAL (pfile))
1105 cpp_error (pfile, "#%s with invalid argument", name);
1108 if (!CPP_TRADITIONAL (pfile))
1110 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1111 goto done;
1113 cpp_pedwarn (pfile, "garbage at end of #%s", name);
1115 _cpp_skip_rest_of_line (pfile);
1117 done:
1118 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1119 return node;
1122 /* #ifdef is dead simple. */
1124 static int
1125 do_ifdef (pfile)
1126 cpp_reader *pfile;
1128 int def = 0;
1129 const cpp_hashnode *node = parse_ifdef (pfile, dtable[T_IFDEF].name);
1130 if (node)
1132 if (node->type == T_POISON)
1133 cpp_error (pfile, "attempt to use poisoned `%s'", node->name);
1134 else
1135 def = (node->type != T_VOID);
1137 push_conditional (pfile, !def, T_IFDEF, 0);
1138 return 0;
1141 /* #ifndef is a tad more complex, because we need to check for a
1142 no-reinclusion wrapper. */
1144 static int
1145 do_ifndef (pfile)
1146 cpp_reader *pfile;
1148 int start_of_file;
1149 int def = 0;
1150 const cpp_hashnode *cmacro;
1152 start_of_file = pfile->only_seen_white == 2;
1153 cmacro = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1154 if (cmacro)
1156 if (cmacro->type == T_POISON)
1157 cpp_error (pfile, "attempt to use poisoned `%s'", cmacro->name);
1158 else
1159 def = (cmacro->type != T_VOID);
1161 push_conditional (pfile, def, T_IFNDEF,
1162 start_of_file ? cmacro : 0);
1163 return 0;
1166 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1167 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1169 static int
1170 do_if (pfile)
1171 cpp_reader *pfile;
1173 const cpp_hashnode *cmacro = 0;
1174 int value = 0;
1176 if (! pfile->skipping)
1178 cmacro = detect_if_not_defined (pfile);
1179 value = _cpp_parse_expr (pfile);
1181 push_conditional (pfile, value == 0, T_IF, cmacro);
1182 return 0;
1185 /* #else flips pfile->skipping and continues without changing
1186 if_stack; this is so that the error message for missing #endif's
1187 etc. will point to the original #if. */
1189 static int
1190 do_else (pfile)
1191 cpp_reader *pfile;
1193 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1195 validate_else (pfile, dtable[T_ELSE].name);
1197 if (ifs == NULL)
1199 cpp_error (pfile, "#else without #if");
1200 return 0;
1202 if (ifs->type == T_ELSE)
1204 cpp_error (pfile, "#else after #else");
1205 cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1208 /* #ifndef can't have its special treatment for containing the whole file
1209 if it has a #else clause. */
1210 ifs->cmacro = 0;
1212 ifs->type = T_ELSE;
1213 if (! ifs->was_skipping)
1215 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1216 succeeded, so we mustn't do the else block. */
1217 if (pfile->skipping < 2)
1218 pfile->skipping = ! pfile->skipping;
1220 return 0;
1224 * handle a #elif directive by not changing if_stack either.
1225 * see the comment above do_else.
1228 static int
1229 do_elif (pfile)
1230 cpp_reader *pfile;
1232 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1234 if (ifs == NULL)
1236 cpp_error (pfile, "#elif without #if");
1237 return 0;
1239 if (ifs->type == T_ELSE)
1241 cpp_error (pfile, "#elif after #else");
1242 cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1245 ifs->type = T_ELIF;
1246 if (ifs->was_skipping)
1247 _cpp_skip_rest_of_line (pfile);
1248 else if (pfile->skipping != 1)
1250 _cpp_skip_rest_of_line (pfile);
1251 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1253 else
1254 pfile->skipping = ! _cpp_parse_expr (pfile);
1256 return 0;
1260 /* #endif pops the if stack and resets pfile->skipping. */
1262 static int
1263 do_endif (pfile)
1264 cpp_reader *pfile;
1266 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1268 validate_else (pfile, dtable[T_ENDIF].name);
1270 if (ifs == NULL)
1271 cpp_error (pfile, "#endif without #if");
1272 else
1274 CPP_BUFFER (pfile)->if_stack = ifs->next;
1275 pfile->skipping = ifs->was_skipping;
1276 pfile->potential_control_macro = ifs->cmacro;
1277 free (ifs);
1279 return 0;
1282 /* Push an if_stack entry and set pfile->skipping accordingly.
1283 If this is a #ifndef starting at the beginning of a file,
1284 CMACRO is the macro name tested by the #ifndef. */
1286 static void
1287 push_conditional (pfile, skip, type, cmacro)
1288 cpp_reader *pfile;
1289 int skip;
1290 int type;
1291 const cpp_hashnode *cmacro;
1293 struct if_stack *ifs;
1295 ifs = (struct if_stack *) xmalloc (sizeof (struct if_stack));
1296 ifs->lineno = CPP_BUFFER (pfile)->lineno;
1297 ifs->next = CPP_BUFFER (pfile)->if_stack;
1298 ifs->cmacro = cmacro;
1299 ifs->was_skipping = pfile->skipping;
1300 ifs->type = type;
1302 if (!pfile->skipping)
1303 pfile->skipping = skip;
1305 CPP_BUFFER (pfile)->if_stack = ifs;
1308 /* Issue -pedantic warning for text which is not a comment following
1309 an #else or #endif. */
1311 static void
1312 validate_else (pfile, directive)
1313 cpp_reader *pfile;
1314 const U_CHAR *directive;
1316 if (CPP_PEDANTIC (pfile))
1318 long old_written = CPP_WRITTEN (pfile);
1319 pfile->no_macro_expand++;
1320 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1321 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1322 CPP_SET_WRITTEN (pfile, old_written);
1323 pfile->no_macro_expand--;
1325 _cpp_skip_rest_of_line (pfile);
1328 /* Called when we reach the end of a macro buffer. Walk back up the
1329 conditional stack till we reach its level at entry to this file,
1330 issuing error messages. Then force skipping off. */
1331 void
1332 _cpp_unwind_if_stack (pfile, pbuf)
1333 cpp_reader *pfile;
1334 cpp_buffer *pbuf;
1336 struct if_stack *ifs, *nifs;
1338 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1340 cpp_error_with_line (pfile, ifs->lineno, 1, "unterminated #%s",
1341 dtable[ifs->type].name);
1342 nifs = ifs->next;
1343 free (ifs);
1345 pfile->skipping = 0;
1348 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1349 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1350 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1351 static int
1352 do_assert (pfile)
1353 cpp_reader *pfile;
1355 long old_written;
1356 U_CHAR *sym;
1357 size_t len;
1358 cpp_hashnode *hp;
1359 struct predicate *pred = 0;
1360 enum cpp_ttype type;
1362 old_written = CPP_WRITTEN (pfile);
1363 pfile->no_macro_expand++;
1365 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1366 type = _cpp_get_directive_token (pfile);
1367 if (type == CPP_VSPACE)
1368 ERROR ("#assert without predicate");
1369 else if (type != CPP_NAME)
1370 ERROR ("assertion predicate is not an identifier");
1372 sym = pfile->token_buffer + old_written;
1373 len = CPP_WRITTEN (pfile) - old_written;
1374 hp = cpp_lookup (pfile, sym, len);
1376 if (_cpp_get_directive_token (pfile) != CPP_OPEN_PAREN)
1377 ERROR ("missing token-sequence in #assert");
1379 pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1380 _cpp_init_toklist (&pred->answer, NO_DUMMY_TOKEN);
1382 if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1383 != CPP_CLOSE_PAREN)
1384 ERROR ("missing close paren in #assert");
1386 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1387 ICE ("impossible token, expecting ) in do_assert");
1389 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1390 ERROR ("junk at end of #assert");
1392 if (hp->type == T_ASSERTION)
1394 /* Check for reassertion. */
1395 const struct predicate *old;
1397 for (old = hp->value.pred; old; old = old->next)
1398 if (_cpp_equiv_toklists (&pred->answer, &old->answer))
1399 /* We used to warn about this, but SVR4 cc doesn't, so let's
1400 match that (also consistent with #define). goto error will
1401 clean up. */
1402 goto error;
1403 pred->next = hp->value.pred;
1405 else
1407 hp->type = T_ASSERTION;
1408 pred->next = 0;
1411 _cpp_squeeze_toklist (&pred->answer);
1412 hp->value.pred = pred;
1413 pfile->no_macro_expand--;
1414 CPP_SET_WRITTEN (pfile, old_written);
1415 return 0;
1417 error:
1418 _cpp_skip_rest_of_line (pfile);
1419 pfile->no_macro_expand--;
1420 CPP_SET_WRITTEN (pfile, old_written);
1421 if (pred)
1423 _cpp_free_toklist (&pred->answer);
1424 free (pred);
1426 return 0;
1429 static int
1430 do_unassert (pfile)
1431 cpp_reader *pfile;
1433 long old_written;
1434 U_CHAR *sym;
1435 size_t len;
1436 cpp_hashnode *hp;
1437 cpp_toklist ans;
1438 enum cpp_ttype type;
1439 int specific = 0;
1441 old_written = CPP_WRITTEN (pfile);
1442 pfile->no_macro_expand++;
1444 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1445 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1446 ERROR ("#unassert must be followed by an identifier");
1448 sym = pfile->token_buffer + old_written;
1449 len = CPP_WRITTEN (pfile) - old_written;
1450 hp = cpp_lookup (pfile, sym, len);
1452 type = _cpp_get_directive_token (pfile);
1453 if (type == CPP_OPEN_PAREN)
1455 specific = 1;
1456 _cpp_init_toklist (&ans, NO_DUMMY_TOKEN);
1458 if (_cpp_scan_until (pfile, &ans, CPP_CLOSE_PAREN)
1459 != CPP_CLOSE_PAREN)
1460 ERROR ("missing close paren in #unassert");
1462 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1463 ICE ("impossible token, expecting ) in do_unassert");
1465 type = _cpp_get_directive_token (pfile);
1468 if (type != CPP_VSPACE)
1469 ERROR ("junk at end of #unassert");
1471 if (hp->type != T_ASSERTION)
1472 /* Not an error to #unassert something that isn't asserted.
1473 goto error to clean up. */
1474 goto error;
1476 if (specific)
1478 /* Find this specific answer and remove it. */
1479 struct predicate *o, *p;
1481 for (p = NULL, o = hp->value.pred; o; p = o, o = o->next)
1482 if (_cpp_equiv_toklists (&ans, &o->answer))
1484 if (p)
1485 p->next = o->next;
1486 else
1487 hp->value.pred = o->next;
1489 _cpp_free_toklist (&o->answer);
1490 free (o);
1491 break;
1494 else
1496 struct predicate *o, *p;
1497 for (o = hp->value.pred; o; o = p)
1499 p = o->next;
1500 _cpp_free_toklist ((cpp_toklist *) &o->answer);
1501 free (o);
1503 hp->value.pred = NULL;
1506 if (hp->value.pred == NULL)
1507 hp->type = T_VOID; /* Last answer for this predicate deleted. */
1509 error:
1510 _cpp_skip_rest_of_line (pfile);
1511 pfile->no_macro_expand--;
1512 CPP_SET_WRITTEN (pfile, old_written);
1513 if (specific)
1514 _cpp_free_toklist (&ans);
1515 return 0;
1518 /* These are for -D, -U, -A. */
1520 /* Process the string STR as if it appeared as the body of a #define.
1521 If STR is just an identifier, define it with value 1.
1522 If STR has anything after the identifier, then it should
1523 be identifier=definition. */
1525 void
1526 cpp_define (pfile, str)
1527 cpp_reader *pfile;
1528 const char *str;
1530 char *buf, *p;
1531 size_t count;
1533 p = strchr (str, '=');
1534 /* Copy the entire option so we can modify it.
1535 Change the first "=" in the string to a space. If there is none,
1536 tack " 1" on the end. Then add a newline and a NUL. */
1538 if (p)
1540 count = strlen (str) + 2;
1541 buf = (char *) alloca (count);
1542 memcpy (buf, str, count - 2);
1543 buf[p - str] = ' ';
1544 buf[count - 2] = '\n';
1545 buf[count - 1] = '\0';
1547 else
1549 count = strlen (str) + 4;
1550 buf = (char *) alloca (count);
1551 memcpy (buf, str, count - 4);
1552 strcpy (&buf[count-4], " 1\n");
1555 if (cpp_push_buffer (pfile, (U_CHAR *)buf, count - 1) != NULL)
1557 do_define (pfile);
1558 cpp_pop_buffer (pfile);
1562 /* Process MACRO as if it appeared as the body of an #undef. */
1563 void
1564 cpp_undef (pfile, macro)
1565 cpp_reader *pfile;
1566 const char *macro;
1568 /* Copy the string so we can append a newline. */
1569 size_t len = strlen (macro);
1570 char *buf = (char *) alloca (len + 2);
1571 memcpy (buf, macro, len);
1572 buf[len] = '\n';
1573 buf[len + 1] = '\0';
1574 if (cpp_push_buffer (pfile, (U_CHAR *)buf, len + 1) != NULL)
1576 do_undef (pfile);
1577 cpp_pop_buffer (pfile);
1581 /* Process the string STR as if it appeared as the body of a #assert. */
1582 void
1583 cpp_assert (pfile, str)
1584 cpp_reader *pfile;
1585 const char *str;
1587 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1589 do_assert (pfile);
1590 cpp_pop_buffer (pfile);
1594 /* Process STR as if it appeared as the body of an #unassert. */
1595 void
1596 cpp_unassert (pfile, str)
1597 cpp_reader *pfile;
1598 const char *str;
1600 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1602 do_unassert (pfile);
1603 cpp_pop_buffer (pfile);
1607 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1609 cpp_defined (pfile, id, len)
1610 cpp_reader *pfile;
1611 const U_CHAR *id;
1612 int len;
1614 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1615 if (hp->type == T_POISON)
1617 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1618 return 0;
1620 return (hp->type != T_VOID);