Add partial support for IA-64 unwind sections.
[official-gcc.git] / gcc / cpplib.c
blob2d466ff1d55e4a1687d5a9031484d76bc05cf946
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 "hashtab.h"
26 #include "cpplib.h"
27 #include "cpphash.h"
28 #include "hashtab.h"
29 #include "intl.h"
30 #include "symcat.h"
32 /* `struct directive' defines one #-directive, including how to handle it. */
34 struct directive
36 directive_handler func; /* Function to handle directive. */
37 const char *name; /* Name of directive. */
38 unsigned short length; /* Length of name. */
39 unsigned short flags; /* Flags describing this directive. */
42 /* Stack of conditionals currently in progress
43 (including both successful and failing conditionals). */
45 struct if_stack
47 struct if_stack *next;
48 int lineno; /* line number where condition started */
49 int if_succeeded; /* truth of last condition in this group */
50 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
51 int type; /* type of last directive seen in this group */
53 typedef struct if_stack IF_STACK;
55 /* Forward declarations. */
57 static void validate_else PARAMS ((cpp_reader *, const char *));
58 static int parse_ifdef PARAMS ((cpp_reader *, const char *));
59 static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
60 static int conditional_skip PARAMS ((cpp_reader *, int, int,
61 U_CHAR *));
62 static int skip_if_group PARAMS ((cpp_reader *));
63 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
64 cpp_reader *, int));
65 static int read_line_number PARAMS ((cpp_reader *, int *));
66 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
67 static int consider_directive_while_skipping
68 PARAMS ((cpp_reader *, IF_STACK *));
70 /* Values for the flags field of the table below. KANDR and COND
71 directives come from traditional (K&R) C. The difference is, if we
72 care about it while skipping a failed conditional block, its origin
73 is COND. STDC89 directives come from the 1989 C standard.
74 EXTENSION directives are extensions, with origins noted below. */
76 #define KANDR 0
77 #define COND 1
78 #define STDC89 2
79 #define EXTENSION 3
81 #define ORIGIN_MASK 3
82 #define ORIGIN(f) ((f) & ORIGIN_MASK)
83 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
85 /* This is the table of directive handlers. It is ordered by
86 frequency of occurrence; the numbers at the end are directive
87 counts from all the source code I have lying around (egcs and libc
88 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
89 pcmcia-cs-3.0.9).
91 The entries with a dash and a name after the count are extensions,
92 of which all but #warning and #include_next are deprecated. The name
93 is where the extension appears to have come from. */
95 /* #sccs is not always recognized. */
96 #ifdef SCCS_DIRECTIVE
97 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
98 #else
99 # define SCCS_ENTRY /* nothing */
100 #endif
102 #define DIRECTIVE_TABLE \
103 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
104 D(include, T_INCLUDE, KANDR | SYNTAX_INCLUDE) /* 52262 */ \
105 D(endif, T_ENDIF, COND) /* 45855 */ \
106 D(ifdef, T_IFDEF, COND) /* 22000 */ \
107 D(if, T_IF, COND) /* 18162 */ \
108 D(else, T_ELSE, COND) /* 9863 */ \
109 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
110 D(undef, T_UNDEF, KANDR) /* 4837 */ \
111 D(line, T_LINE, KANDR) /* 2465 */ \
112 D(elif, T_ELIF, COND) /* 610 */ \
113 D(error, T_ERROR, STDC89) /* 475 */ \
114 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
115 D(warning, T_WARNING, EXTENSION) /* 22 GNU */ \
116 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /* 19 GNU */ \
117 D(ident, T_IDENT, EXTENSION) /* 11 SVR4 */ \
118 D(import, T_IMPORT, EXTENSION | SYNTAX_INCLUDE) /* 0 ObjC */ \
119 D(assert, T_ASSERT, EXTENSION | SYNTAX_ASSERT) /* 0 SVR4 */ \
120 D(unassert, T_UNASSERT, EXTENSION | SYNTAX_ASSERT) /* 0 SVR4 */ \
121 SCCS_ENTRY
123 /* Use the table to generate a series of prototypes, an enum for the
124 directive names, and an array of directive handlers. */
126 /* The directive-processing functions are declared to return int
127 instead of void, because some old compilers have trouble with
128 pointers to functions returning void. */
130 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
131 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
132 DIRECTIVE_TABLE
133 #undef D
135 #define D(n, tag, f) tag,
136 enum
138 DIRECTIVE_TABLE
139 N_DIRECTIVES
141 #undef D
143 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
144 #define D(name, t, flags) \
145 { CONCAT2(do_,name), STRINGX(name), sizeof STRINGX(name) - 1, flags },
146 static const struct directive dtable[] =
148 DIRECTIVE_TABLE
150 #undef D
151 #undef DIRECTIVE_TABLE
153 /* Check if a token's name matches that of a known directive. Put in
154 this file to save exporting dtable and other unneeded information. */
155 void
156 _cpp_check_directive (list, token)
157 cpp_toklist *list;
158 cpp_token *token;
160 const char *name = list->namebuf + token->val.name.offset;
161 size_t len = token->val.name.len;
162 unsigned int i;
164 list->dir_handler = 0;
165 list->dir_flags = 0;
167 for (i = 0; i < N_DIRECTIVES; i++)
168 if (dtable[i].length == len && !strncmp (dtable[i].name, name, len))
170 list->dir_handler = dtable[i].func;
171 list->dir_flags = dtable[i].flags;
172 break;
176 /* Handle a possible # directive.
177 '#' has already been read. */
180 _cpp_handle_directive (pfile)
181 cpp_reader *pfile;
183 int i;
184 int hash_at_bol;
185 unsigned int len;
186 U_CHAR *ident;
187 long old_written = CPP_WRITTEN (pfile);
188 enum cpp_ttype tok;
190 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
192 cpp_ice (pfile, "handle_directive called on macro buffer");
193 return 0;
196 /* -traditional directives are recognized only with the # in column 1. */
197 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
199 /* Scan the next token, then pretend we didn't. */
200 CPP_SET_MARK (pfile);
201 pfile->no_macro_expand++;
202 tok = _cpp_get_directive_token (pfile);
203 pfile->no_macro_expand--;
205 ident = pfile->token_buffer + old_written;
206 len = CPP_PWRITTEN (pfile) - ident;
207 CPP_SET_WRITTEN (pfile, old_written);
208 CPP_GOTO_MARK (pfile);
210 /* # followed by a number is equivalent to #line. Do not recognize
211 this form in assembly language source files. Complain about this
212 form if we're being pedantic, but not if this is regurgitated
213 input (preprocessed or fed back in by the C++ frontend). */
214 if (tok == CPP_NUMBER)
216 if (CPP_OPTION (pfile, lang_asm))
217 return 0;
219 if (CPP_PEDANTIC (pfile)
220 && ! CPP_OPTION (pfile, preprocessed)
221 && ! CPP_BUFFER (pfile)->manual_pop)
222 cpp_pedwarn (pfile, "# followed by integer");
223 do_line (pfile);
224 return 1;
227 /* If we are rescanning preprocessed input, don't obey any directives
228 other than # nnn. */
229 else if (CPP_OPTION (pfile, preprocessed))
230 return 0;
232 /* A line of just # becomes blank. */
233 else if (tok == CPP_VSPACE)
234 return 1;
236 /* A NAME token might in fact be a directive! */
237 else if (tok == CPP_NAME)
239 for (i = 0; i < N_DIRECTIVES; i++)
241 if (dtable[i].length == len
242 && !strncmp (dtable[i].name, ident, len))
243 goto real_directive;
245 /* Don't complain about invalid directives in assembly source,
246 we don't know where the comments are, and # may introduce
247 assembler pseudo-ops. */
248 if (!CPP_OPTION (pfile, lang_asm))
249 cpp_error (pfile, "invalid preprocessing directive #%s", ident);
250 return 0;
252 /* And anything else means the # wasn't a directive marker. */
253 else
254 return 0;
256 real_directive:
258 /* In -traditional mode, a directive is ignored unless its # is in
259 column 1. */
260 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
262 if (CPP_WTRADITIONAL (pfile))
263 cpp_warning (pfile, "ignoring #%s because of its indented #",
264 dtable[i].name);
265 return 0;
268 /* no_directives is set when we are parsing macro arguments. Directives
269 in macro arguments are undefined behavior (C99 6.10.3.11); this
270 implementation chooses to make them hard errors. */
271 if (pfile->no_directives)
273 cpp_error (pfile, "#%s may not be used inside a macro argument",
274 dtable[i].name);
275 _cpp_skip_rest_of_line (pfile);
276 return 1;
279 /* Issue -pedantic warnings for extended directives. */
280 if (CPP_PEDANTIC (pfile) && ORIGIN (dtable[i].flags) == EXTENSION)
281 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
283 /* -Wtraditional gives warnings about directives with inappropriate
284 indentation of #. */
285 if (CPP_WTRADITIONAL (pfile))
287 if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].flags))
288 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
289 dtable[i].name);
290 else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].flags))
291 cpp_warning (pfile,
292 "suggest hiding #%s from traditional C with an indented #",
293 dtable[i].name);
296 /* Unfortunately, it's necessary to scan the directive name again,
297 now we know we're going to consume it. FIXME. */
299 pfile->no_macro_expand++;
300 _cpp_get_directive_token (pfile);
301 pfile->no_macro_expand--;
302 CPP_SET_WRITTEN (pfile, old_written);
304 /* Some directives (e.g. #if) may return a request to execute
305 another directive handler immediately. No directive ever
306 requests that #define be executed immediately, so it is safe for
307 the loop to terminate when some function returns 0 (== T_DEFINE). */
308 while ((i = dtable[i].func (pfile)));
309 return 1;
312 /* Pass a directive through to the output file.
313 BUF points to the contents of the directive, as a contiguous string.
314 LEN is the length of the string pointed to by BUF.
315 KEYWORD is the keyword-table entry for the directive. */
317 static void
318 pass_thru_directive (buf, len, pfile, keyword)
319 const U_CHAR *buf;
320 size_t len;
321 cpp_reader *pfile;
322 int keyword;
324 const struct directive *kt = &dtable[keyword];
325 register unsigned klen = kt->length;
327 CPP_RESERVE (pfile, 1 + klen + len);
328 CPP_PUTC_Q (pfile, '#');
329 CPP_PUTS_Q (pfile, kt->name, klen);
330 if (len != 0 && buf[0] != ' ')
331 CPP_PUTC_Q (pfile, ' ');
332 CPP_PUTS_Q (pfile, buf, len);
335 /* Process a #define command. */
337 static int
338 do_define (pfile)
339 cpp_reader *pfile;
341 HASHNODE **slot;
342 unsigned long hash;
343 int len;
344 U_CHAR *sym;
345 cpp_toklist *list = &pfile->directbuf;
347 pfile->no_macro_expand++;
348 pfile->parsing_define_directive++;
349 CPP_OPTION (pfile, discard_comments)++;
351 _cpp_scan_line (pfile, list);
353 /* First token on the line must be a NAME. There must be at least
354 one token (the VSPACE at the end). */
355 if (list->tokens[0].type != CPP_NAME)
357 cpp_error_with_line (pfile, list->line, list->tokens[0].col,
358 "#define must be followed by an identifier");
359 goto out;
362 sym = list->namebuf + list->tokens[0].val.name.offset;
363 len = list->tokens[0].val.name.len;
365 /* That NAME is not allowed to be "defined". (Not clear if the
366 standard requires this.) */
367 if (len == 7 && !strncmp (sym, "defined", 7))
369 cpp_error_with_line (pfile, list->line, list->tokens[0].col,
370 "\"defined\" is not a legal macro name");
371 goto out;
374 slot = _cpp_lookup_slot (pfile, sym, len, INSERT, &hash);
375 if (*slot)
377 /* Check for poisoned identifiers now. All other checks
378 are done in cpphash.c. */
379 if ((*slot)->type == T_POISON)
381 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
382 goto out;
385 else
386 *slot = _cpp_make_hashnode (sym, len, T_VOID, hash);
388 if (_cpp_create_definition (pfile, list, *slot) == 0)
389 goto out;
391 if (CPP_OPTION (pfile, debug_output)
392 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
393 _cpp_dump_definition (pfile, *slot);
394 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
395 pass_thru_directive (sym, len, pfile, T_DEFINE);
397 out:
398 pfile->no_macro_expand--;
399 pfile->parsing_define_directive--;
400 CPP_OPTION (pfile, discard_comments)--;
401 return 0;
404 /* Handle #include and #import. */
406 static unsigned int
407 parse_include (pfile, name)
408 cpp_reader *pfile;
409 const char *name;
411 long old_written = CPP_WRITTEN (pfile);
412 enum cpp_ttype token;
413 int len;
415 pfile->parsing_include_directive++;
416 token = _cpp_get_directive_token (pfile);
417 pfile->parsing_include_directive--;
419 len = CPP_WRITTEN (pfile) - old_written;
421 if (token == CPP_STRING)
422 ; /* No special treatment required. */
423 #ifdef VMS
424 else if (token == CPP_NAME)
426 /* Support '#include xyz' like VAX-C. It is taken as
427 '#include <xyz.h>' and generates a warning. */
428 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
429 name, name);
431 /* Rewrite the token to <xyz.h>. */
432 CPP_RESERVE (pfile, 4);
433 len += 4;
434 memmove (pfile->token_buffer + old_written + 1,
435 pfile->token_buffer + old_written,
436 CPP_WRITTEN (pfile) - old_written);
437 pfile->token_buffer[old_written] = '<';
438 CPP_PUTS_Q (pfile, ".h>", 2);
440 #endif
441 else
443 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
444 CPP_SET_WRITTEN (pfile, old_written);
445 _cpp_skip_rest_of_line (pfile);
446 return 0;
449 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
451 cpp_error (pfile, "junk at end of `#%s'", name);
452 _cpp_skip_rest_of_line (pfile);
455 CPP_SET_WRITTEN (pfile, old_written);
457 if (len == 0)
458 cpp_error (pfile, "empty file name in `#%s'", name);
460 return len;
463 static int
464 do_include (pfile)
465 cpp_reader *pfile;
467 unsigned int len;
468 char *token;
470 len = parse_include (pfile, dtable[T_INCLUDE].name);
471 if (len == 0)
472 return 0;
473 token = alloca (len + 1);
474 memcpy (token, CPP_PWRITTEN (pfile), len);
475 token[len] = '\0';
477 if (CPP_OPTION (pfile, dump_includes))
478 pass_thru_directive (token, len, pfile, T_INCLUDE);
480 _cpp_execute_include (pfile, token, len, 0, 0);
481 return 0;
484 static int
485 do_import (pfile)
486 cpp_reader *pfile;
488 unsigned int len;
489 char *token;
491 if (CPP_OPTION (pfile, warn_import)
492 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
494 pfile->import_warning = 1;
495 cpp_warning (pfile,
496 "#import is obsolete, use an #ifndef wrapper in the header file");
499 len = parse_include (pfile, dtable[T_IMPORT].name);
500 if (len == 0)
501 return 0;
502 token = alloca (len + 1);
503 memcpy (token, CPP_PWRITTEN (pfile), len);
504 token[len] = '\0';
506 if (CPP_OPTION (pfile, dump_includes))
507 pass_thru_directive (token, len, pfile, T_IMPORT);
509 _cpp_execute_include (pfile, token, len, 1, 0);
510 return 0;
513 static int
514 do_include_next (pfile)
515 cpp_reader *pfile;
517 unsigned int len;
518 char *token;
519 struct file_name_list *search_start = 0;
521 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
522 if (len == 0)
523 return 0;
524 token = alloca (len + 1);
525 memcpy (token, CPP_PWRITTEN (pfile), len);
526 token[len] = '\0';
528 if (CPP_OPTION (pfile, dump_includes))
529 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
531 /* For #include_next, skip in the search path past the dir in which the
532 containing file was found. Treat files specified using an absolute path
533 as if there are no more directories to search. Treat the primary source
534 file like any other included source, but generate a warning. */
535 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
537 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
538 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
540 else
541 cpp_warning (pfile, "#include_next in primary source file");
543 _cpp_execute_include (pfile, token, len, 0, search_start);
544 return 0;
547 /* Subroutine of do_line. Read next token from PFILE without adding it to
548 the output buffer. If it is a number between 1 and 4, store it in *NUM
549 and return 1; otherwise, return 0 and complain if we aren't at the end
550 of the directive. */
552 static int
553 read_line_number (pfile, num)
554 cpp_reader *pfile;
555 int *num;
557 long save_written = CPP_WRITTEN (pfile);
558 U_CHAR *p;
559 enum cpp_ttype token = _cpp_get_directive_token (pfile);
560 p = pfile->token_buffer + save_written;
562 if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
563 && p[0] >= '1' && p[0] <= '4')
565 *num = p[0] - '0';
566 CPP_SET_WRITTEN (pfile, save_written);
567 return 1;
569 else
571 if (token != CPP_VSPACE && token != CPP_EOF)
572 cpp_error (pfile, "invalid format `#line' command");
573 CPP_SET_WRITTEN (pfile, save_written);
574 return 0;
578 /* Interpret #line command.
579 Note that the filename string (if any) is treated as if it were an
580 include filename. That means no escape handling. */
582 static int
583 do_line (pfile)
584 cpp_reader *pfile;
586 cpp_buffer *ip = CPP_BUFFER (pfile);
587 unsigned int new_lineno;
588 long old_written = CPP_WRITTEN (pfile);
589 enum cpp_ttype token;
590 char *x;
592 token = _cpp_get_directive_token (pfile);
594 if (token != CPP_NUMBER)
596 cpp_error (pfile, "token after `#line' is not an integer");
597 goto bad_line_directive;
600 CPP_PUTC (pfile, '\0'); /* not terminated for us */
601 new_lineno = strtoul (pfile->token_buffer + old_written, &x, 10);
602 if (x[0] != '\0')
604 cpp_error (pfile, "token after `#line' is not an integer");
605 goto bad_line_directive;
607 CPP_SET_WRITTEN (pfile, old_written);
609 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
610 cpp_pedwarn (pfile, "line number out of range in `#line' command");
612 token = _cpp_get_directive_token (pfile);
614 if (token == CPP_STRING)
616 U_CHAR *fname = pfile->token_buffer + old_written + 1;
617 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
618 int action_number = 0;
620 if (read_line_number (pfile, &action_number))
622 if (CPP_PEDANTIC (pfile))
623 cpp_pedwarn (pfile, "garbage at end of `#line' command");
625 /* This is somewhat questionable: change the buffer stack
626 depth so that output_line_command thinks we've stacked
627 another buffer. */
628 if (action_number == 1)
630 pfile->buffer_stack_depth++;
631 ip->system_header_p = 0;
632 read_line_number (pfile, &action_number);
634 else if (action_number == 2)
636 pfile->buffer_stack_depth--;
637 ip->system_header_p = 0;
638 read_line_number (pfile, &action_number);
640 if (action_number == 3)
642 ip->system_header_p = 1;
643 read_line_number (pfile, &action_number);
645 if (action_number == 4)
647 ip->system_header_p = 2;
648 read_line_number (pfile, &action_number);
652 *end_name = '\0';
654 if (strcmp (fname, ip->nominal_fname))
656 if (!strcmp (fname, ip->ihash->name))
657 ip->nominal_fname = ip->ihash->name;
658 else
659 ip->nominal_fname = _cpp_fake_ihash (pfile, fname);
662 else if (token != CPP_VSPACE && token != CPP_EOF)
664 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
665 goto bad_line_directive;
668 /* The Newline at the end of this line remains to be processed.
669 To put the next line at the specified line number,
670 we must store a line number now that is one less. */
671 ip->lineno = new_lineno - 1;
672 CPP_SET_WRITTEN (pfile, old_written);
673 return 0;
675 bad_line_directive:
676 _cpp_skip_rest_of_line (pfile);
677 CPP_SET_WRITTEN (pfile, old_written);
678 return 0;
681 /* Remove the definition of a symbol from the symbol table.
682 According to the C standard, it is not an error to undef
683 something that has no definitions. */
684 static int
685 do_undef (pfile)
686 cpp_reader *pfile;
688 int len;
689 HASHNODE **slot;
690 U_CHAR *name;
691 long here = CPP_WRITTEN (pfile);
692 enum cpp_ttype token;
694 pfile->no_macro_expand++;
695 token = _cpp_get_directive_token (pfile);
696 pfile->no_macro_expand--;
698 if (token != CPP_NAME)
700 cpp_error (pfile, "token after #undef is not an identifier");
701 _cpp_skip_rest_of_line (pfile);
702 return 0;
704 len = CPP_WRITTEN (pfile) - here;
706 token = _cpp_get_directive_token (pfile);
707 if (token != CPP_VSPACE)
709 cpp_pedwarn (pfile, "junk on line after #undef");
710 _cpp_skip_rest_of_line (pfile);
713 name = pfile->token_buffer + here;
714 CPP_SET_WRITTEN (pfile, here);
716 slot = _cpp_lookup_slot (pfile, name, len, NO_INSERT, 0);
717 if (slot)
719 HASHNODE *hp = *slot;
720 if (hp->type == T_POISON)
721 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
722 else
724 /* If we are generating additional info for debugging (with -g) we
725 need to pass through all effective #undef commands. */
726 if (CPP_OPTION (pfile, debug_output))
727 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
729 if (hp->type != T_MACRO && hp->type != T_FMACRO
730 && hp->type != T_MCONST
731 && hp->type != T_EMPTY && hp->type != T_IDENTITY)
732 cpp_warning (pfile, "undefining `%s'", hp->name);
734 htab_clear_slot (pfile->hashtab, (void **)slot);
738 return 0;
742 * Report an error detected by the program we are processing.
743 * Use the text of the line in the error message.
744 * (We use error because it prints the filename & line#.)
747 static int
748 do_error (pfile)
749 cpp_reader *pfile;
751 const U_CHAR *text, *limit;
753 _cpp_skip_hspace (pfile);
754 text = CPP_BUFFER (pfile)->cur;
755 _cpp_skip_rest_of_line (pfile);
756 limit = CPP_BUFFER (pfile)->cur;
758 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
759 return 0;
763 * Report a warning detected by the program we are processing.
764 * Use the text of the line in the warning message, then continue.
767 static int
768 do_warning (pfile)
769 cpp_reader *pfile;
771 const U_CHAR *text, *limit;
773 _cpp_skip_hspace (pfile);
774 text = CPP_BUFFER (pfile)->cur;
775 _cpp_skip_rest_of_line (pfile);
776 limit = CPP_BUFFER (pfile)->cur;
778 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
779 return 0;
782 /* Report program identification. */
784 static int
785 do_ident (pfile)
786 cpp_reader *pfile;
788 long old_written = CPP_WRITTEN (pfile);
790 CPP_PUTS (pfile, "#ident ", 7);
792 /* Next token should be a string constant. */
793 if (_cpp_get_directive_token (pfile) == CPP_STRING)
794 /* And then a newline. */
795 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
796 /* Good - ship it. */
797 return 0;
799 cpp_error (pfile, "invalid #ident");
800 _cpp_skip_rest_of_line (pfile);
801 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
803 return 0;
806 /* Pragmata handling. We handle some of these, and pass the rest on
807 to the front end. C99 defines three pragmas and says that no macro
808 expansion is to be performed on them; whether or not macro
809 expansion happens for other pragmas is implementation defined.
810 This implementation never macro-expands the text after #pragma.
812 We currently do not support the _Pragma operator. Support for that
813 has to be coordinated with the front end. Proposed implementation:
814 both #pragma blah blah and _Pragma("blah blah") become
815 __builtin_pragma(blah blah) and we teach the parser about that. */
817 /* Sub-handlers for the pragmas needing treatment here.
818 They return 1 if the token buffer is to be popped, 0 if not. */
819 static int do_pragma_once PARAMS ((cpp_reader *));
820 static int do_pragma_implementation PARAMS ((cpp_reader *));
821 static int do_pragma_poison PARAMS ((cpp_reader *));
822 static int do_pragma_default PARAMS ((cpp_reader *));
824 static int
825 do_pragma (pfile)
826 cpp_reader *pfile;
828 long here, key;
829 U_CHAR *buf;
830 int pop;
831 enum cpp_ttype token;
833 here = CPP_WRITTEN (pfile);
834 CPP_PUTS (pfile, "#pragma ", 8);
836 key = CPP_WRITTEN (pfile);
837 pfile->no_macro_expand++;
838 token = _cpp_get_directive_token (pfile);
839 if (token != CPP_NAME)
841 if (token == CPP_VSPACE)
842 goto empty;
843 else
844 goto skip;
847 buf = pfile->token_buffer + key;
848 CPP_PUTC (pfile, ' ');
850 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
851 if (tokis ("once"))
852 pop = do_pragma_once (pfile);
853 else if (tokis ("implementation"))
854 pop = do_pragma_implementation (pfile);
855 else if (tokis ("poison"))
856 pop = do_pragma_poison (pfile);
857 else
858 pop = do_pragma_default (pfile);
859 #undef tokis
861 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
862 goto skip;
864 if (pop)
865 CPP_SET_WRITTEN (pfile, here);
866 pfile->no_macro_expand--;
867 return 0;
869 skip:
870 cpp_error (pfile, "malformed #pragma directive");
871 _cpp_skip_rest_of_line (pfile);
872 empty:
873 CPP_SET_WRITTEN (pfile, here);
874 pfile->no_macro_expand--;
875 return 0;
878 static int
879 do_pragma_default (pfile)
880 cpp_reader *pfile;
882 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
883 CPP_PUTC (pfile, ' ');
884 return 0;
887 static int
888 do_pragma_once (pfile)
889 cpp_reader *pfile;
891 cpp_buffer *ip = CPP_BUFFER (pfile);
893 /* Allow #pragma once in system headers, since that's not the user's
894 fault. */
895 if (!ip->system_header_p)
896 cpp_warning (pfile, "`#pragma once' is obsolete");
898 if (CPP_PREV_BUFFER (ip) == NULL)
899 cpp_warning (pfile, "`#pragma once' outside include file");
900 else
901 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
903 return 1;
906 static int
907 do_pragma_implementation (pfile)
908 cpp_reader *pfile;
910 /* Be quiet about `#pragma implementation' for a file only if it hasn't
911 been included yet. */
912 enum cpp_ttype token;
913 long written = CPP_WRITTEN (pfile);
914 U_CHAR *name;
915 U_CHAR *copy;
916 size_t len;
918 token = _cpp_get_directive_token (pfile);
919 if (token == CPP_VSPACE)
920 return 0;
921 else if (token != CPP_STRING)
923 cpp_error (pfile, "malformed #pragma implementation");
924 return 1;
927 /* Trim the leading and trailing quote marks from the string. */
928 name = pfile->token_buffer + written + 1;
929 len = CPP_PWRITTEN (pfile) - name;
930 copy = (U_CHAR *) alloca (len);
931 memcpy (copy, name, len - 1);
932 copy[len - 1] = '\0';
934 if (cpp_included (pfile, copy))
935 cpp_warning (pfile,
936 "`#pragma implementation' for `%s' appears after file is included",
937 copy);
938 return 0;
941 static int
942 do_pragma_poison (pfile)
943 cpp_reader *pfile;
945 /* Poison these symbols so that all subsequent usage produces an
946 error message. */
947 U_CHAR *p;
948 HASHNODE **slot;
949 long written;
950 size_t len;
951 enum cpp_ttype token;
952 int writeit;
953 unsigned long hash;
955 /* As a rule, don't include #pragma poison commands in output,
956 unless the user asks for them. */
957 writeit = (CPP_OPTION (pfile, debug_output)
958 || CPP_OPTION (pfile, dump_macros) == dump_definitions
959 || CPP_OPTION (pfile, dump_macros) == dump_names);
961 for (;;)
963 written = CPP_WRITTEN (pfile);
964 token = _cpp_get_directive_token (pfile);
965 if (token == CPP_VSPACE)
966 break;
967 if (token != CPP_NAME)
969 cpp_error (pfile, "invalid #pragma poison directive");
970 _cpp_skip_rest_of_line (pfile);
971 return 1;
974 p = pfile->token_buffer + written;
975 len = CPP_PWRITTEN (pfile) - p;
976 slot = _cpp_lookup_slot (pfile, p, len, INSERT, &hash);
977 if (*slot)
979 HASHNODE *hp = *slot;
980 if (hp->type != T_POISON)
982 cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
983 if (hp->type == T_MACRO)
984 _cpp_free_definition (hp->value.defn);
985 hp->value.defn = 0;
986 hp->type = T_POISON;
989 else
991 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
992 hp->value.cpval = 0;
993 *slot = hp;
995 if (writeit)
996 CPP_PUTC (pfile, ' ');
998 return !writeit;
1001 /* Just ignore #sccs, on systems where we define it at all. */
1002 #ifdef SCCS_DIRECTIVE
1003 static int
1004 do_sccs (pfile)
1005 cpp_reader *pfile;
1007 _cpp_skip_rest_of_line (pfile);
1008 return 0;
1010 #endif
1012 /* We've found an `#if' directive. If the only thing before it in
1013 this file is white space, and if it is of the form
1014 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1015 for inclusion of this file. (See redundant_include_p in cppfiles.c
1016 for an explanation of controlling macros.) If so, return a
1017 malloced copy of SYMBOL. Otherwise, return NULL. */
1019 static U_CHAR *
1020 detect_if_not_defined (pfile)
1021 cpp_reader *pfile;
1023 U_CHAR *control_macro = 0;
1024 enum cpp_ttype token;
1025 unsigned int base_offset;
1026 unsigned int token_offset;
1027 unsigned int need_rparen = 0;
1028 unsigned int token_len;
1030 if (pfile->only_seen_white != 2)
1031 return NULL;
1033 /* Save state required for restore. */
1034 pfile->no_macro_expand++;
1035 CPP_SET_MARK (pfile);
1036 base_offset = CPP_WRITTEN (pfile);
1038 /* Look for `!', */
1039 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1040 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1041 || CPP_PWRITTEN (pfile)[-1] != '!')
1042 goto restore;
1044 /* ...then `defined', */
1045 token_offset = CPP_WRITTEN (pfile);
1046 token = _cpp_get_directive_token (pfile);
1047 if (token != CPP_NAME)
1048 goto restore;
1049 if (strncmp (pfile->token_buffer + token_offset, "defined", 7))
1050 goto restore;
1052 /* ...then an optional '(' and the name, */
1053 token_offset = CPP_WRITTEN (pfile);
1054 token = _cpp_get_directive_token (pfile);
1055 if (token == CPP_OPEN_PAREN)
1057 token_offset = CPP_WRITTEN (pfile);
1058 need_rparen = 1;
1059 token = _cpp_get_directive_token (pfile);
1061 if (token != CPP_NAME)
1062 goto restore;
1064 token_len = CPP_WRITTEN (pfile) - token_offset;
1066 /* ...then the ')', if necessary, */
1067 if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1068 goto restore;
1070 /* ...and make sure there's nothing else on the line. */
1071 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1072 goto restore;
1074 /* We have a legitimate controlling macro for this header. */
1075 control_macro = (U_CHAR *) xmalloc (token_len + 1);
1076 memcpy (control_macro, pfile->token_buffer + token_offset, token_len);
1077 control_macro[token_len] = '\0';
1079 restore:
1080 CPP_SET_WRITTEN (pfile, base_offset);
1081 pfile->no_macro_expand--;
1082 CPP_GOTO_MARK (pfile);
1084 return control_macro;
1088 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1089 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1092 static int
1093 do_if (pfile)
1094 cpp_reader *pfile;
1096 U_CHAR *control_macro = detect_if_not_defined (pfile);
1097 int value = _cpp_parse_expr (pfile);
1098 return conditional_skip (pfile, value == 0, T_IF, control_macro);
1102 * handle a #elif directive by not changing if_stack either.
1103 * see the comment above do_else.
1106 static int
1107 do_elif (pfile)
1108 cpp_reader *pfile;
1110 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1112 cpp_error (pfile, "`#elif' not within a conditional");
1113 return 0;
1115 else
1117 if (pfile->if_stack->type == T_ELSE)
1119 cpp_error (pfile, "`#elif' after `#else'");
1120 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1121 "the conditional began here");
1123 pfile->if_stack->type = T_ELIF;
1126 if (pfile->if_stack->if_succeeded)
1128 _cpp_skip_rest_of_line (pfile);
1129 return skip_if_group (pfile);
1131 if (_cpp_parse_expr (pfile) == 0)
1132 return skip_if_group (pfile);
1134 ++pfile->if_stack->if_succeeded; /* continue processing input */
1135 return 0;
1138 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1139 not defined; the macro tested is left in the token buffer (but
1140 popped). */
1142 static int
1143 parse_ifdef (pfile, name)
1144 cpp_reader *pfile;
1145 const char *name;
1147 U_CHAR *ident;
1148 unsigned int len;
1149 enum cpp_ttype token;
1150 long old_written = CPP_WRITTEN (pfile);
1151 int defined;
1153 pfile->no_macro_expand++;
1154 token = _cpp_get_directive_token (pfile);
1155 pfile->no_macro_expand--;
1157 ident = pfile->token_buffer + old_written;
1158 len = CPP_WRITTEN (pfile) - old_written;
1160 if (token == CPP_VSPACE)
1162 if (! CPP_TRADITIONAL (pfile))
1163 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1164 defined = 0;
1165 goto done;
1167 else if (token == CPP_NAME)
1169 defined = cpp_defined (pfile, ident, len);
1170 CPP_PUTC (pfile, '\0'); /* so it can be copied with xstrdup */
1172 else
1174 defined = 0;
1175 if (! CPP_TRADITIONAL (pfile))
1176 cpp_error (pfile, "`#%s' with invalid argument", name);
1179 if (!CPP_TRADITIONAL (pfile))
1181 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1182 goto done;
1184 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1186 _cpp_skip_rest_of_line (pfile);
1188 done:
1189 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1190 return defined;
1193 /* #ifdef is dead simple. */
1195 static int
1196 do_ifdef (pfile)
1197 cpp_reader *pfile;
1199 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1200 return conditional_skip (pfile, skip, T_IFDEF, 0);
1203 /* #ifndef is a tad more complex, because we need to check for a
1204 no-reinclusion wrapper. */
1206 static int
1207 do_ifndef (pfile)
1208 cpp_reader *pfile;
1210 int start_of_file, skip;
1211 U_CHAR *control_macro = 0;
1213 start_of_file = pfile->only_seen_white == 2;
1214 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1216 if (start_of_file && !skip)
1217 control_macro = (U_CHAR *) xstrdup (CPP_PWRITTEN (pfile));
1219 return conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1222 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1223 If this is a #ifndef starting at the beginning of a file,
1224 CONTROL_MACRO is the macro name tested by the #ifndef.
1225 Otherwise, CONTROL_MACRO is 0. */
1227 static int
1228 conditional_skip (pfile, skip, type, control_macro)
1229 cpp_reader *pfile;
1230 int skip;
1231 int type;
1232 U_CHAR *control_macro;
1234 IF_STACK *temp;
1236 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1237 temp->lineno = CPP_BUFFER (pfile)->lineno;
1238 temp->next = pfile->if_stack;
1239 temp->control_macro = control_macro;
1240 pfile->if_stack = temp;
1242 pfile->if_stack->type = type;
1244 if (skip != 0)
1245 return skip_if_group (pfile);
1247 ++pfile->if_stack->if_succeeded;
1248 return 0;
1251 /* Subroutine of skip_if_group. Examine one preprocessing directive
1252 and return 0 if skipping should continue, or the directive number
1253 of the directive that ends the block if it should halt.
1255 Also adjusts the if_stack as appropriate. The `#' has been read,
1256 but not the identifier. */
1258 static int
1259 consider_directive_while_skipping (pfile, stack)
1260 cpp_reader *pfile;
1261 IF_STACK *stack;
1263 long ident;
1264 int i, hash_at_bol;
1265 unsigned int len;
1266 IF_STACK *temp;
1268 /* -traditional directives are recognized only with the # in column 1. */
1269 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
1271 ident = CPP_WRITTEN (pfile);
1272 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1273 return 0;
1274 len = CPP_WRITTEN (pfile) - ident;
1276 for (i = 0; i < N_DIRECTIVES; i++)
1278 if (dtable[i].length == len
1279 && !strncmp (dtable[i].name, pfile->token_buffer + ident, len))
1280 goto real_directive;
1282 return 0;
1284 real_directive:
1286 /* If it's not a directive of interest to us, return now. */
1287 if (ORIGIN (dtable[i].flags) != COND)
1288 return 0;
1290 /* First, deal with -traditional and -Wtraditional.
1291 All COND directives are from K+R. */
1293 if (! hash_at_bol)
1295 if (CPP_TRADITIONAL (pfile))
1297 if (CPP_WTRADITIONAL (pfile))
1298 cpp_warning (pfile, "ignoring #%s because of its indented #",
1299 dtable[i].name);
1300 return 0;
1302 if (CPP_WTRADITIONAL (pfile))
1303 cpp_warning (pfile, "traditional C ignores %s with the # indented",
1304 dtable[i].name);
1307 switch (i)
1309 default:
1310 cpp_ice (pfile, "non COND directive in switch in c_d_w_s");
1311 return 0;
1313 case T_IF:
1314 case T_IFDEF:
1315 case T_IFNDEF:
1316 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1317 temp->lineno = CPP_BUFFER (pfile)->lineno;
1318 temp->next = pfile->if_stack;
1319 temp->type = i;
1320 pfile->if_stack = temp;
1321 return 0;
1323 case T_ELSE:
1324 if (pfile->if_stack != stack)
1325 validate_else (pfile, dtable[i].name);
1326 /* fall through */
1327 case T_ELIF:
1328 if (pfile->if_stack == stack)
1329 return i;
1331 pfile->if_stack->type = i;
1332 return 0;
1334 case T_ENDIF:
1335 if (pfile->if_stack != stack)
1336 validate_else (pfile, dtable[i].name);
1338 if (pfile->if_stack == stack)
1339 return i;
1341 temp = pfile->if_stack;
1342 pfile->if_stack = temp->next;
1343 free (temp);
1344 return 0;
1348 /* Skip to #endif, #else, or #elif. Consumes the directive that
1349 causes it to stop, but not its argument. Returns the number of
1350 that directive, which must be passed back up to
1351 _cpp_handle_directive, which will execute it. */
1352 static int
1353 skip_if_group (pfile)
1354 cpp_reader *pfile;
1356 enum cpp_ttype token;
1357 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1358 long old_written;
1359 int ret = 0;
1361 /* We are no longer at the start of the file. */
1362 pfile->only_seen_white = 0;
1364 old_written = CPP_WRITTEN (pfile);
1365 pfile->no_macro_expand++;
1366 for (;;)
1368 /* We are at the end of a line. Only cpp_get_token knows how to
1369 advance the line number correctly. */
1370 token = cpp_get_token (pfile);
1371 if (token == CPP_POP)
1372 break; /* Caller will issue error. */
1373 else if (token != CPP_VSPACE)
1374 cpp_ice (pfile, "cpp_get_token returned %d in skip_if_group", token);
1375 CPP_SET_WRITTEN (pfile, old_written);
1377 token = _cpp_get_directive_token (pfile);
1379 if (token == CPP_DIRECTIVE)
1381 ret = consider_directive_while_skipping (pfile, save_if_stack);
1382 if (ret)
1383 break;
1386 if (token != CPP_VSPACE)
1387 _cpp_skip_rest_of_line (pfile);
1389 CPP_SET_WRITTEN (pfile, old_written);
1390 pfile->no_macro_expand--;
1391 return ret;
1395 * handle a #else directive. Do this by just continuing processing
1396 * without changing if_stack ; this is so that the error message
1397 * for missing #endif's etc. will point to the original #if. It
1398 * is possible that something different would be better.
1401 static int
1402 do_else (pfile)
1403 cpp_reader *pfile;
1405 validate_else (pfile, dtable[T_ELSE].name);
1406 _cpp_skip_rest_of_line (pfile);
1408 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1410 cpp_error (pfile, "`#else' not within a conditional");
1411 return 0;
1413 else
1415 /* #ifndef can't have its special treatment for containing the whole file
1416 if it has a #else clause. */
1417 pfile->if_stack->control_macro = 0;
1419 if (pfile->if_stack->type == T_ELSE)
1421 cpp_error (pfile, "`#else' after `#else'");
1422 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1423 "the conditional began here");
1425 pfile->if_stack->type = T_ELSE;
1428 if (pfile->if_stack->if_succeeded)
1429 return skip_if_group (pfile);
1431 ++pfile->if_stack->if_succeeded; /* continue processing input */
1432 return 0;
1436 * unstack after #endif command
1439 static int
1440 do_endif (pfile)
1441 cpp_reader *pfile;
1443 validate_else (pfile, dtable[T_ENDIF].name);
1444 _cpp_skip_rest_of_line (pfile);
1446 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1447 cpp_error (pfile, "`#endif' not within a conditional");
1448 else
1450 IF_STACK *temp = pfile->if_stack;
1451 pfile->if_stack = temp->next;
1452 if (temp->control_macro != 0)
1453 pfile->potential_control_macro = temp->control_macro;
1454 free (temp);
1456 return 0;
1459 /* Issue -pedantic warning for text which is not a comment following
1460 an #else or #endif. Do not warn in system headers, as this is harmless
1461 and very common on old systems. */
1463 static void
1464 validate_else (pfile, directive)
1465 cpp_reader *pfile;
1466 const char *directive;
1468 long old_written;
1469 if (! CPP_PEDANTIC (pfile))
1470 return;
1472 old_written = CPP_WRITTEN (pfile);
1473 pfile->no_macro_expand++;
1474 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1475 cpp_pedwarn (pfile,
1476 "text following `#%s' violates ANSI standard", directive);
1477 CPP_SET_WRITTEN (pfile, old_written);
1478 pfile->no_macro_expand--;
1481 void
1482 _cpp_handle_eof (pfile)
1483 cpp_reader *pfile;
1485 struct if_stack *ifs, *nifs;
1487 /* Unwind the conditional stack and generate error messages. */
1488 for (ifs = pfile->if_stack;
1489 ifs != CPP_BUFFER (pfile)->if_stack;
1490 ifs = nifs)
1492 cpp_error_with_line (pfile, ifs->lineno, 0,
1493 "unterminated `#%s' conditional",
1494 dtable[ifs->type].name);
1496 nifs = ifs->next;
1497 free (ifs);
1499 pfile->if_stack = ifs;
1500 CPP_BUFFER (pfile)->seen_eof = 1;
1503 static int
1504 do_assert (pfile)
1505 cpp_reader *pfile;
1507 long old_written;
1508 U_CHAR *sym;
1509 int ret;
1510 HASHNODE *base, *this;
1511 HASHNODE **bslot, **tslot;
1512 size_t blen, tlen;
1513 unsigned long bhash, thash;
1515 old_written = CPP_WRITTEN (pfile); /* remember where it starts */
1516 ret = _cpp_parse_assertion (pfile);
1517 if (ret == 0)
1518 goto error;
1519 else if (ret == 1)
1521 cpp_error (pfile, "missing token-sequence in #assert");
1522 goto error;
1524 tlen = CPP_WRITTEN (pfile) - old_written;
1526 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1528 cpp_error (pfile, "junk at end of #assert");
1529 goto error;
1532 sym = pfile->token_buffer + old_written;
1533 blen = (U_CHAR *) strchr (sym, '(') - sym;
1534 tslot = _cpp_lookup_slot (pfile, sym, tlen, INSERT, &thash);
1535 if (*tslot)
1537 cpp_warning (pfile, "%s re-asserted", sym);
1538 goto error;
1541 bslot = _cpp_lookup_slot (pfile, sym, blen, INSERT, &bhash);
1542 if (! *bslot)
1544 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1545 base->value.aschain = 0;
1547 else
1549 base = *bslot;
1550 if (base->type != T_ASSERT)
1552 /* Token clash - but with what?! */
1553 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1554 goto error;
1557 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1558 this->value.aschain = base->value.aschain;
1559 base->value.aschain = this;
1561 error:
1562 _cpp_skip_rest_of_line (pfile);
1563 CPP_SET_WRITTEN (pfile, old_written);
1564 return 0;
1567 static int
1568 do_unassert (pfile)
1569 cpp_reader *pfile;
1571 int ret;
1572 long old_written;
1573 U_CHAR *sym;
1574 long baselen, thislen;
1575 HASHNODE *base, *this, *next;
1577 old_written = CPP_WRITTEN (pfile);
1578 ret = _cpp_parse_assertion (pfile);
1579 if (ret == 0)
1580 goto error;
1581 thislen = CPP_WRITTEN (pfile) - old_written;
1583 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1585 cpp_error (pfile, "junk at end of #unassert");
1586 goto error;
1588 sym = pfile->token_buffer + old_written;
1589 CPP_SET_WRITTEN (pfile, old_written);
1591 if (ret == 1)
1593 base = _cpp_lookup (pfile, sym, thislen);
1594 if (! base)
1595 goto error; /* It isn't an error to #undef what isn't #defined,
1596 so it isn't an error to #unassert what isn't
1597 #asserted either. */
1599 for (this = base->value.aschain; this; this = next)
1601 next = this->value.aschain;
1602 htab_remove_elt (pfile->hashtab, this);
1604 htab_remove_elt (pfile->hashtab, base);
1606 else
1608 baselen = (U_CHAR *) strchr (sym, '(') - sym;
1609 base = _cpp_lookup (pfile, sym, baselen);
1610 if (! base) goto error;
1611 this = _cpp_lookup (pfile, sym, thislen);
1612 if (! this) goto error;
1614 next = base;
1615 while (next->value.aschain != this)
1616 next = next->value.aschain;
1618 next->value.aschain = this->value.aschain;
1619 htab_remove_elt (pfile->hashtab, this);
1621 if (base->value.aschain == NULL)
1622 /* Last answer for this predicate deleted. */
1623 htab_remove_elt (pfile->hashtab, base);
1625 return 0;
1627 error:
1628 _cpp_skip_rest_of_line (pfile);
1629 CPP_SET_WRITTEN (pfile, old_written);
1630 return 0;
1633 /* These are for -D, -U, -A. */
1635 /* Process the string STR as if it appeared as the body of a #define.
1636 If STR is just an identifier, define it with value 1.
1637 If STR has anything after the identifier, then it should
1638 be identifier=definition. */
1640 void
1641 cpp_define (pfile, str)
1642 cpp_reader *pfile;
1643 const char *str;
1645 char *buf, *p;
1646 size_t count;
1648 p = strchr (str, '=');
1649 /* Copy the entire option so we can modify it.
1650 Change the first "=" in the string to a space. If there is none,
1651 tack " 1" on the end. Then add a newline and a NUL. */
1653 if (p)
1655 count = strlen (str) + 2;
1656 buf = (char *) alloca (count);
1657 memcpy (buf, str, count - 2);
1658 buf[p - str] = ' ';
1659 buf[count - 2] = '\n';
1660 buf[count - 1] = '\0';
1662 else
1664 count = strlen (str) + 4;
1665 buf = (char *) alloca (count);
1666 memcpy (buf, str, count - 4);
1667 strcpy (&buf[count-4], " 1\n");
1670 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1672 do_define (pfile);
1673 cpp_pop_buffer (pfile);
1677 /* Process MACRO as if it appeared as the body of an #undef. */
1678 void
1679 cpp_undef (pfile, macro)
1680 cpp_reader *pfile;
1681 const char *macro;
1683 /* Copy the string so we can append a newline. */
1684 size_t len = strlen (macro);
1685 char *buf = (char *) alloca (len + 2);
1686 memcpy (buf, macro, len);
1687 buf[len] = '\n';
1688 buf[len + 1] = '\0';
1689 if (cpp_push_buffer (pfile, buf, len + 1))
1691 do_undef (pfile);
1692 cpp_pop_buffer (pfile);
1696 /* Process the string STR as if it appeared as the body of a #assert. */
1697 void
1698 cpp_assert (pfile, str)
1699 cpp_reader *pfile;
1700 const char *str;
1702 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1704 do_assert (pfile);
1705 cpp_pop_buffer (pfile);
1709 /* Process STR as if it appeared as the body of an #unassert. */
1710 void
1711 cpp_unassert (pfile, str)
1712 cpp_reader *pfile;
1713 const char *str;
1715 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1717 do_unassert (pfile);
1718 cpp_pop_buffer (pfile);
1722 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1724 cpp_defined (pfile, id, len)
1725 cpp_reader *pfile;
1726 const U_CHAR *id;
1727 int len;
1729 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1730 if (hp && hp->type == T_POISON)
1732 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1733 return 0;
1735 return (hp != NULL);