alias.c: Fix typos in comments.
[gcc.git] / gcc / cpplib.c
1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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
7
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.
12
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.
17
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. */
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "obstack.h"
28
29 /* Chained list of answers to an assertion. */
30 struct answer
31 {
32 struct answer *next;
33 unsigned int count;
34 cpp_token first[1];
35 };
36
37 /* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
39
40 struct if_stack
41 {
42 struct if_stack *next;
43 unsigned int line; /* Line where condition started. */
44 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
45 bool skip_elses; /* Can future #else / #elif be skipped? */
46 bool was_skipping; /* If were skipping on entry. */
47 int type; /* Most recent conditional, for diagnostics. */
48 };
49
50 /* Values for the origin field of struct directive. KANDR directives
51 come from traditional (K&R) C. STDC89 directives come from the
52 1989 C standard. EXTENSION directives are extensions. */
53 #define KANDR 0
54 #define STDC89 1
55 #define EXTENSION 2
56
57 /* Values for the flags field of struct directive. COND indicates a
58 conditional; IF_COND an opening conditional. INCL means to treat
59 "..." and <...> as q-char and h-char sequences respectively. IN_I
60 means this directive should be handled even if -fpreprocessed is in
61 effect (these are the directives with callback hooks). */
62 #define COND (1 << 0)
63 #define IF_COND (1 << 1)
64 #define INCL (1 << 2)
65 #define IN_I (1 << 3)
66
67 /* Defines one #-directive, including how to handle it. */
68 typedef void (*directive_handler) PARAMS ((cpp_reader *));
69 typedef struct directive directive;
70 struct directive
71 {
72 directive_handler handler; /* Function to handle directive. */
73 const U_CHAR *name; /* Name of directive. */
74 unsigned short length; /* Length of name. */
75 unsigned char origin; /* Origin of directive. */
76 unsigned char flags; /* Flags describing this directive. */
77 };
78
79 /* Forward declarations. */
80
81 static void skip_rest_of_line PARAMS ((cpp_reader *));
82 static void check_eol PARAMS ((cpp_reader *));
83 static void start_directive PARAMS ((cpp_reader *));
84 static void end_directive PARAMS ((cpp_reader *, int));
85 static void directive_diagnostics
86 PARAMS ((cpp_reader *, const directive *, int));
87 static void run_directive PARAMS ((cpp_reader *, int,
88 const char *, size_t));
89 static int glue_header_name PARAMS ((cpp_reader *, cpp_token *));
90 static int parse_include PARAMS ((cpp_reader *, cpp_token *));
91 static void push_conditional PARAMS ((cpp_reader *, int, int,
92 const cpp_hashnode *));
93 static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
94 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
95 unsigned long *));
96 static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
97 static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
98 static void do_include_common PARAMS ((cpp_reader *, enum include_type));
99 static void do_pragma_once PARAMS ((cpp_reader *));
100 static void do_pragma_poison PARAMS ((cpp_reader *));
101 static void do_pragma_system_header PARAMS ((cpp_reader *));
102 static void do_pragma_dependency PARAMS ((cpp_reader *));
103 static int get__Pragma_string PARAMS ((cpp_reader *, cpp_token *));
104 static unsigned char *destringize PARAMS ((const cpp_string *,
105 unsigned int *));
106 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
107 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
108 int));
109 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
110 const struct answer *));
111 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
112
113 /* This is the table of directive handlers. It is ordered by
114 frequency of occurrence; the numbers at the end are directive
115 counts from all the source code I have lying around (egcs and libc
116 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
117 pcmcia-cs-3.0.9). This is no longer important as directive lookup
118 is now O(1). All extensions other than #warning and #include_next
119 are deprecated. The name is where the extension appears to have
120 come from. */
121
122 #define DIRECTIVE_TABLE \
123 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
124 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
125 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
126 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
127 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
128 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
129 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
130 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
131 D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
132 D(elif, T_ELIF, STDC89, COND) /* 610 */ \
133 D(error, T_ERROR, STDC89, 0) /* 475 */ \
134 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
135 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
136 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
137 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
138 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
139 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
140 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
141 SCCS_ENTRY /* 0 SVR4? */
142
143 /* #sccs is not always recognized. */
144 #ifdef SCCS_DIRECTIVE
145 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
146 #else
147 # define SCCS_ENTRY /* nothing */
148 #endif
149
150 /* Use the table to generate a series of prototypes, an enum for the
151 directive names, and an array of directive handlers. */
152
153 /* The directive-processing functions are declared to return int
154 instead of void, because some old compilers have trouble with
155 pointers to functions returning void. */
156
157 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
158 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
159 DIRECTIVE_TABLE
160 #undef D
161
162 #define D(n, tag, o, f) tag,
163 enum
164 {
165 DIRECTIVE_TABLE
166 N_DIRECTIVES
167 };
168 #undef D
169
170 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
171 #define D(name, t, origin, flags) \
172 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
173 sizeof STRINGX(name) - 1, origin, flags },
174 static const directive dtable[] =
175 {
176 DIRECTIVE_TABLE
177 };
178 #undef D
179 #undef DIRECTIVE_TABLE
180
181 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
182
183 /* Skip any remaining tokens in a directive. */
184 static void
185 skip_rest_of_line (pfile)
186 cpp_reader *pfile;
187 {
188 /* Discard all stacked contexts. */
189 while (pfile->context != &pfile->base_context)
190 _cpp_pop_context (pfile);
191
192 /* Sweep up all tokens remaining on the line. */
193 if (! SEEN_EOL ())
194 while (_cpp_lex_token (pfile)->type != CPP_EOF)
195 ;
196 }
197
198 /* Ensure there are no stray tokens at the end of a directive. */
199 static void
200 check_eol (pfile)
201 cpp_reader *pfile;
202 {
203 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
204 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
205 pfile->directive->name);
206 }
207
208 /* Called when entering a directive, _Pragma or command-line directive. */
209 static void
210 start_directive (pfile)
211 cpp_reader *pfile;
212 {
213 /* Setup in-directive state. */
214 pfile->state.in_directive = 1;
215 pfile->state.save_comments = 0;
216
217 /* Some handlers need the position of the # for diagnostics. */
218 pfile->directive_line = pfile->line;
219 }
220
221 /* Called when leaving a directive, _Pragma or command-line directive. */
222 static void
223 end_directive (pfile, skip_line)
224 cpp_reader *pfile;
225 int skip_line;
226 {
227 /* We don't skip for an assembler #. */
228 if (skip_line)
229 {
230 skip_rest_of_line (pfile);
231 if (!pfile->keep_tokens)
232 {
233 pfile->cur_run = &pfile->base_run;
234 pfile->cur_token = pfile->base_run.base;
235 }
236 }
237
238 /* Restore state. */
239 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
240 pfile->state.in_directive = 0;
241 pfile->state.angled_headers = 0;
242 pfile->state.line_extension = 0;
243 pfile->directive = 0;
244 }
245
246 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
247 the '#' was indented. */
248
249 static void
250 directive_diagnostics (pfile, dir, indented)
251 cpp_reader *pfile;
252 const directive *dir;
253 int indented;
254 {
255 if (pfile->state.line_extension)
256 {
257 if (CPP_PEDANTIC (pfile)
258 && ! pfile->state.skipping)
259 cpp_pedwarn (pfile, "style of line directive is a GCC extension");
260 }
261 else
262 {
263 /* Issue -pedantic warnings for extensions. */
264 if (CPP_PEDANTIC (pfile)
265 && ! pfile->state.skipping
266 && dir->origin == EXTENSION)
267 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
268
269 /* Traditionally, a directive is ignored unless its # is in
270 column 1. Therefore in code intended to work with K+R
271 compilers, directives added by C89 must have their #
272 indented, and directives present in traditional C must not.
273 This is true even of directives in skipped conditional
274 blocks. */
275 if (CPP_WTRADITIONAL (pfile))
276 {
277 if (dir == &dtable[T_ELIF])
278 cpp_warning (pfile, "suggest not using #elif in traditional C");
279 else if (indented && dir->origin == KANDR)
280 cpp_warning (pfile,
281 "traditional C ignores #%s with the # indented",
282 dir->name);
283 else if (!indented && dir->origin != KANDR)
284 cpp_warning (pfile,
285 "suggest hiding #%s from traditional C with an indented #",
286 dir->name);
287 }
288 }
289 }
290
291 /* Check if we have a known directive. INDENTED is non-zero if the
292 '#' of the directive was indented. This function is in this file
293 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
294 non-zero if the line of tokens has been handled, zero if we should
295 continue processing the line. */
296
297 int
298 _cpp_handle_directive (pfile, indented)
299 cpp_reader *pfile;
300 int indented;
301 {
302 const directive *dir = 0;
303 const cpp_token *dname;
304 int skip = 1;
305
306 start_directive (pfile);
307 dname = _cpp_lex_token (pfile);
308
309 if (dname->type == CPP_NAME)
310 {
311 if (dname->val.node->directive_index)
312 dir = &dtable[dname->val.node->directive_index - 1];
313 }
314 /* We do not recognise the # followed by a number extension in
315 assembler code. */
316 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
317 {
318 dir = &dtable[T_LINE];
319 pfile->state.line_extension = 1;
320 }
321
322 if (dir)
323 {
324 /* If we have a directive that is not an opening conditional,
325 invalidate any control macro. */
326 if (! (dir->flags & IF_COND))
327 pfile->mi_valid = false;
328
329 /* Kluge alert. In order to be sure that code like this
330
331 #define HASH #
332 HASH define foo bar
333
334 does not cause '#define foo bar' to get executed when
335 compiled with -save-temps, we recognize directives in
336 -fpreprocessed mode only if the # is in column 1. cppmacro.c
337 puts a space in fron of any '#' at the start of a macro. */
338 if (CPP_OPTION (pfile, preprocessed)
339 && (indented || !(dir->flags & IN_I)))
340 {
341 skip = 0;
342 dir = 0;
343 }
344 else
345 {
346 /* In failed conditional groups, all non-conditional
347 directives are ignored. Before doing that, whether
348 skipping or not, we should lex angle-bracketed headers
349 correctly, and maybe output some diagnostics. */
350 pfile->state.angled_headers = dir->flags & INCL;
351 if (! CPP_OPTION (pfile, preprocessed))
352 directive_diagnostics (pfile, dir, indented);
353 if (pfile->state.skipping && !(dir->flags & COND))
354 dir = 0;
355 }
356 }
357 else if (dname->type == CPP_EOF)
358 ; /* CPP_EOF is the "null directive". */
359 else
360 {
361 /* An unknown directive. Don't complain about it in assembly
362 source: we don't know where the comments are, and # may
363 introduce assembler pseudo-ops. Don't complain about invalid
364 directives in skipped conditional groups (6.10 p4). */
365 if (CPP_OPTION (pfile, lang) == CLK_ASM)
366 skip = 0;
367 else if (!pfile->state.skipping)
368 cpp_error (pfile, "invalid preprocessing directive #%s",
369 cpp_token_as_text (pfile, dname));
370 }
371
372 if (dir)
373 {
374 pfile->directive = dir;
375 (*pfile->directive->handler) (pfile);
376 }
377 else if (skip == 0)
378 _cpp_backup_tokens (pfile, 1);
379
380 end_directive (pfile, skip);
381 return skip;
382 }
383
384 /* Directive handler wrapper used by the command line option
385 processor. */
386 static void
387 run_directive (pfile, dir_no, buf, count)
388 cpp_reader *pfile;
389 int dir_no;
390 const char *buf;
391 size_t count;
392 {
393 cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
394 /* from_stage3 */ true, 1);
395 start_directive (pfile);
396 /* We don't want a leading # to be interpreted as a directive. */
397 pfile->buffer->saved_flags = 0;
398 pfile->directive = &dtable[dir_no];
399 (void) (*pfile->directive->handler) (pfile);
400 end_directive (pfile, 1);
401 _cpp_pop_buffer (pfile);
402 }
403
404 /* Checks for validity the macro name in #define, #undef, #ifdef and
405 #ifndef directives. */
406 static cpp_hashnode *
407 lex_macro_node (pfile)
408 cpp_reader *pfile;
409 {
410 cpp_hashnode *node;
411 const cpp_token *token = _cpp_lex_token (pfile);
412
413 /* The token immediately after #define must be an identifier. That
414 identifier may not be "defined", per C99 6.10.8p4.
415 In C++, it may not be any of the "named operators" either,
416 per C++98 [lex.digraph], [lex.key].
417 Finally, the identifier may not have been poisoned. (In that case
418 the lexer has issued the error message for us.) */
419
420 if (token->type != CPP_NAME)
421 {
422 if (token->type == CPP_EOF)
423 cpp_error (pfile, "no macro name given in #%s directive",
424 pfile->directive->name);
425 else if (token->flags & NAMED_OP)
426 cpp_error (pfile,
427 "\"%s\" cannot be used as a macro name as it is an operator in C++",
428 NODE_NAME (token->val.node));
429 else
430 cpp_error (pfile, "macro names must be identifiers");
431
432 return 0;
433 }
434
435 node = token->val.node;
436 if (node->flags & NODE_POISONED)
437 return 0;
438
439 if (node == pfile->spec_nodes.n_defined)
440 {
441 cpp_error (pfile, "\"%s\" cannot be used as a macro name",
442 NODE_NAME (node));
443 return 0;
444 }
445
446 return node;
447 }
448
449 /* Process a #define directive. Most work is done in cppmacro.c. */
450 static void
451 do_define (pfile)
452 cpp_reader *pfile;
453 {
454 cpp_hashnode *node = lex_macro_node (pfile);
455
456 if (node)
457 {
458 if (_cpp_create_definition (pfile, node))
459 if (pfile->cb.define)
460 (*pfile->cb.define) (pfile, pfile->directive_line, node);
461 }
462 }
463
464 /* Handle #undef. Marks the identifier NT_VOID in the hash table. */
465 static void
466 do_undef (pfile)
467 cpp_reader *pfile;
468 {
469 cpp_hashnode *node = lex_macro_node (pfile);
470
471 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
472 is not currently defined as a macro name. */
473 if (node && node->type == NT_MACRO)
474 {
475 if (pfile->cb.undef)
476 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
477
478 if (node->flags & NODE_WARN)
479 cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
480
481 _cpp_free_definition (node);
482 }
483 check_eol (pfile);
484 }
485
486 /* Helper routine used by parse_include. Reinterpret the current line
487 as an h-char-sequence (< ... >); we are looking at the first token
488 after the <. Returns zero on success. */
489 static int
490 glue_header_name (pfile, header)
491 cpp_reader *pfile;
492 cpp_token *header;
493 {
494 cpp_token token;
495 unsigned char *buffer, *token_mem;
496 size_t len, total_len = 0, capacity = 1024;
497
498 /* To avoid lexed tokens overwriting our glued name, we can only
499 allocate from the string pool once we've lexed everything. */
500
501 buffer = (unsigned char *) xmalloc (capacity);
502 for (;;)
503 {
504 cpp_get_token (pfile, &token);
505
506 if (token.type == CPP_GREATER || token.type == CPP_EOF)
507 break;
508
509 len = cpp_token_len (&token);
510 if (total_len + len > capacity)
511 {
512 capacity = (capacity + len) * 2;
513 buffer = (unsigned char *) xrealloc (buffer, capacity);
514 }
515
516 if (token.flags & PREV_WHITE)
517 buffer[total_len++] = ' ';
518
519 total_len = cpp_spell_token (pfile, &token, &buffer[total_len]) - buffer;
520 }
521
522 if (token.type == CPP_EOF)
523 cpp_error (pfile, "missing terminating > character");
524 else
525 {
526 token_mem = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1);
527 memcpy (token_mem, buffer, total_len);
528 token_mem[total_len] = '\0';
529
530 header->type = CPP_HEADER_NAME;
531 header->flags &= ~PREV_WHITE;
532 header->val.str.len = total_len;
533 header->val.str.text = token_mem;
534 }
535
536 free ((PTR) buffer);
537 return token.type == CPP_EOF;
538 }
539
540 /* Parse the header name of #include, #include_next, #import and
541 #pragma dependency. Returns zero on success. */
542 static int
543 parse_include (pfile, header)
544 cpp_reader *pfile;
545 cpp_token *header;
546 {
547 const unsigned char *dir;
548
549 if (pfile->directive == &dtable[T_PRAGMA])
550 dir = U"pragma dependency";
551 else
552 dir = pfile->directive->name;
553
554 /* Allow macro expansion. */
555 cpp_get_token (pfile, header);
556 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
557 {
558 if (header->type != CPP_LESS)
559 {
560 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
561 return 1;
562 }
563 if (glue_header_name (pfile, header))
564 return 1;
565 }
566
567 if (header->val.str.len == 0)
568 {
569 cpp_error (pfile, "empty file name in #%s", dir);
570 return 1;
571 }
572
573 return 0;
574 }
575
576 /* Handle #include, #include_next and #import. */
577 static void
578 do_include_common (pfile, type)
579 cpp_reader *pfile;
580 enum include_type type;
581 {
582 cpp_token header;
583
584 /* For #include_next, if this is the primary source file, warn and
585 use the normal search logic. */
586 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
587 {
588 cpp_warning (pfile, "#include_next in primary source file");
589 type = IT_INCLUDE;
590 }
591 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
592 {
593 CPP_OPTION (pfile, warn_import) = 0;
594 cpp_warning (pfile,
595 "#import is obsolete, use an #ifndef wrapper in the header file");
596 }
597
598 if (!parse_include (pfile, &header))
599 {
600 /* Prevent #include recursion. */
601 if (pfile->line_maps.depth >= CPP_STACK_MAX)
602 cpp_fatal (pfile, "#include nested too deeply");
603 else
604 {
605 check_eol (pfile);
606 /* Get out of macro context, if we are. */
607 skip_rest_of_line (pfile);
608 if (pfile->cb.include)
609 (*pfile->cb.include) (pfile, pfile->directive_line,
610 pfile->directive->name, &header);
611
612 _cpp_execute_include (pfile, &header, type);
613 }
614 }
615 }
616
617 static void
618 do_include (pfile)
619 cpp_reader *pfile;
620 {
621 do_include_common (pfile, IT_INCLUDE);
622 }
623
624 static void
625 do_import (pfile)
626 cpp_reader *pfile;
627 {
628 do_include_common (pfile, IT_IMPORT);
629 }
630
631 static void
632 do_include_next (pfile)
633 cpp_reader *pfile;
634 {
635 do_include_common (pfile, IT_INCLUDE_NEXT);
636 }
637
638 /* Subroutine of do_line. Read possible flags after file name. LAST
639 is the last flag seen; 0 if this is the first flag. Return the flag
640 if it is valid, 0 at the end of the directive. Otherwise complain. */
641
642 static unsigned int
643 read_flag (pfile, last)
644 cpp_reader *pfile;
645 unsigned int last;
646 {
647 const cpp_token *token = _cpp_lex_token (pfile);
648
649 if (token->type == CPP_NUMBER && token->val.str.len == 1)
650 {
651 unsigned int flag = token->val.str.text[0] - '0';
652
653 if (flag > last && flag <= 4
654 && (flag != 4 || last == 3)
655 && (flag != 2 || last == 0))
656 return flag;
657 }
658
659 if (token->type != CPP_EOF)
660 cpp_error (pfile, "invalid flag \"%s\" in line directive",
661 cpp_token_as_text (pfile, token));
662 return 0;
663 }
664
665 /* Another subroutine of do_line. Convert a number in STR, of length
666 LEN, to binary; store it in NUMP, and return 0 if the number was
667 well-formed, 1 if not. Temporary, hopefully. */
668 static int
669 strtoul_for_line (str, len, nump)
670 const U_CHAR *str;
671 unsigned int len;
672 unsigned long *nump;
673 {
674 unsigned long reg = 0;
675 U_CHAR c;
676 while (len--)
677 {
678 c = *str++;
679 if (!ISDIGIT (c))
680 return 1;
681 reg *= 10;
682 reg += c - '0';
683 }
684 *nump = reg;
685 return 0;
686 }
687
688 /* Interpret #line command.
689 Note that the filename string (if any) is treated as if it were an
690 include filename. That means no escape handling. */
691
692 static void
693 do_line (pfile)
694 cpp_reader *pfile;
695 {
696 cpp_token token;
697 const char *new_file = pfile->map->to_file;
698 unsigned long new_lineno;
699 unsigned int cap, new_sysp = pfile->map->sysp;
700 enum lc_reason reason = LC_RENAME;
701
702 /* C99 raised the minimum limit on #line numbers. */
703 cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
704
705 /* Putting this in _cpp_handle_directive risks two calls to
706 _cpp_backup_tokens in some circumstances, which can segfault. */
707 if (pfile->state.line_extension)
708 _cpp_backup_tokens (pfile, 1);
709
710 /* #line commands expand macros. */
711 cpp_get_token (pfile, &token);
712 if (token.type != CPP_NUMBER
713 || strtoul_for_line (token.val.str.text, token.val.str.len, &new_lineno))
714 {
715 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
716 cpp_token_as_text (pfile, &token));
717 return;
718 }
719
720 if (CPP_PEDANTIC (pfile) && ! pfile->state.line_extension
721 && (new_lineno == 0 || new_lineno > cap))
722 cpp_pedwarn (pfile, "line number out of range");
723
724 cpp_get_token (pfile, &token);
725 if (token.type == CPP_STRING)
726 {
727 new_file = (const char *) token.val.str.text;
728
729 /* Only accept flags for the # 55 form. */
730 if (pfile->state.line_extension)
731 {
732 int flag;
733
734 new_sysp = 0;
735 flag = read_flag (pfile, 0);
736 if (flag == 1)
737 {
738 reason = LC_ENTER;
739 /* Fake an include for cpp_included (). */
740 _cpp_fake_include (pfile, new_file);
741 flag = read_flag (pfile, flag);
742 }
743 else if (flag == 2)
744 {
745 reason = LC_LEAVE;
746 flag = read_flag (pfile, flag);
747 }
748 if (flag == 3)
749 {
750 new_sysp = 1;
751 flag = read_flag (pfile, flag);
752 if (flag == 4)
753 new_sysp = 2;
754 }
755 }
756 check_eol (pfile);
757 }
758 else if (token.type != CPP_EOF)
759 {
760 cpp_error (pfile, "\"%s\" is not a valid filename",
761 cpp_token_as_text (pfile, &token));
762 return;
763 }
764
765 skip_rest_of_line (pfile);
766 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
767 }
768
769 /* Arrange the file_change callback. pfile->line has changed to
770 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
771 header, 2 for a sytem header that needs to be extern "C" protected,
772 and zero otherwise. */
773 void
774 _cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
775 cpp_reader *pfile;
776 enum lc_reason reason;
777 const char *to_file;
778 unsigned int file_line;
779 unsigned int sysp;
780 {
781 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
782 pfile->line, to_file, file_line);
783
784 if (pfile->cb.file_change)
785 (*pfile->cb.file_change) (pfile, pfile->map);
786 }
787
788 /*
789 * Report a warning or error detected by the program we are
790 * processing. Use the directive's tokens in the error message.
791 */
792
793 static void
794 do_diagnostic (pfile, code, print_dir)
795 cpp_reader *pfile;
796 enum error_type code;
797 int print_dir;
798 {
799 if (_cpp_begin_message (pfile, code, 0, 0))
800 {
801 if (print_dir)
802 fprintf (stderr, "#%s ", pfile->directive->name);
803 pfile->state.prevent_expansion++;
804 cpp_output_line (pfile, stderr);
805 pfile->state.prevent_expansion--;
806 }
807 }
808
809 static void
810 do_error (pfile)
811 cpp_reader *pfile;
812 {
813 do_diagnostic (pfile, ERROR, 1);
814 }
815
816 static void
817 do_warning (pfile)
818 cpp_reader *pfile;
819 {
820 /* We want #warning diagnostics to be emitted in system headers too. */
821 do_diagnostic (pfile, WARNING_SYSHDR, 1);
822 }
823
824 /* Report program identification. */
825
826 static void
827 do_ident (pfile)
828 cpp_reader *pfile;
829 {
830 cpp_token str;
831
832 cpp_get_token (pfile, &str);
833 if (str.type != CPP_STRING)
834 cpp_error (pfile, "invalid #ident");
835 else if (pfile->cb.ident)
836 (*pfile->cb.ident) (pfile, pfile->directive_line, &str.val.str);
837
838 check_eol (pfile);
839 }
840
841 /* Pragmata handling. We handle some of these, and pass the rest on
842 to the front end. C99 defines three pragmas and says that no macro
843 expansion is to be performed on them; whether or not macro
844 expansion happens for other pragmas is implementation defined.
845 This implementation never macro-expands the text after #pragma. */
846
847 /* Sub-handlers for the pragmas needing treatment here.
848 They return 1 if the token buffer is to be popped, 0 if not. */
849 typedef void (*pragma_cb) PARAMS ((cpp_reader *));
850 struct pragma_entry
851 {
852 struct pragma_entry *next;
853 const char *name;
854 size_t len;
855 int isnspace;
856 union {
857 pragma_cb handler;
858 struct pragma_entry *space;
859 } u;
860 };
861
862 void
863 cpp_register_pragma (pfile, space, name, handler)
864 cpp_reader *pfile;
865 const char *space;
866 const char *name;
867 pragma_cb handler;
868 {
869 struct pragma_entry **x, *new;
870 size_t len;
871
872 x = &pfile->pragmas;
873 if (space)
874 {
875 struct pragma_entry *p = pfile->pragmas;
876 len = strlen (space);
877 while (p)
878 {
879 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
880 {
881 x = &p->u.space;
882 goto found;
883 }
884 p = p->next;
885 }
886 cpp_ice (pfile, "unknown #pragma namespace %s", space);
887 return;
888 }
889
890 found:
891 new = (struct pragma_entry *)
892 _cpp_pool_alloc (&pfile->macro_pool, sizeof (struct pragma_entry));
893 new->name = name;
894 new->len = strlen (name);
895 new->isnspace = 0;
896 new->u.handler = handler;
897
898 new->next = *x;
899 *x = new;
900 }
901
902 void
903 cpp_register_pragma_space (pfile, space)
904 cpp_reader *pfile;
905 const char *space;
906 {
907 struct pragma_entry *new;
908 const struct pragma_entry *p = pfile->pragmas;
909 size_t len = strlen (space);
910
911 while (p)
912 {
913 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
914 /* Multiple different callers are allowed to register the same
915 namespace. */
916 return;
917 p = p->next;
918 }
919
920 new = (struct pragma_entry *)
921 _cpp_pool_alloc (&pfile->macro_pool, sizeof (struct pragma_entry));
922 new->name = space;
923 new->len = len;
924 new->isnspace = 1;
925 new->u.space = 0;
926
927 new->next = pfile->pragmas;
928 pfile->pragmas = new;
929 }
930
931 void
932 _cpp_init_internal_pragmas (pfile)
933 cpp_reader *pfile;
934 {
935 /* top level */
936 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
937 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
938
939 /* GCC namespace */
940 cpp_register_pragma_space (pfile, "GCC");
941
942 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
943 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
944 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
945 }
946
947 static void
948 do_pragma (pfile)
949 cpp_reader *pfile;
950 {
951 pragma_cb handler = NULL;
952 const struct pragma_entry *p;
953 cpp_token tok;
954 unsigned int count = 0;
955
956 p = pfile->pragmas;
957 pfile->state.prevent_expansion++;
958
959 new_space:
960 count++;
961 cpp_get_token (pfile, &tok);
962 if (tok.type == CPP_NAME)
963 {
964 const cpp_hashnode *node = tok.val.node;
965 size_t len = NODE_LEN (node);
966
967 while (p)
968 {
969 if (strlen (p->name) == len
970 && !memcmp (p->name, NODE_NAME (node), len))
971 {
972 if (p->isnspace)
973 {
974 p = p->u.space;
975 goto new_space;
976 }
977 else
978 {
979 handler = p->u.handler;
980 break;
981 }
982 }
983 p = p->next;
984 }
985 }
986
987 /* FIXME. This is an awful kludge to get the front ends to update
988 their notion of line number for diagnostic purposes. The line
989 number should be passed to the handler and they should do it
990 themselves. Stand-alone CPP must ignore us, otherwise it will
991 prefix the directive with spaces, hence the 1. Ugh. */
992 if (pfile->cb.line_change)
993 (*pfile->cb.line_change)(pfile, &tok, 1);
994
995 if (handler)
996 (*handler) (pfile);
997 else if (pfile->cb.def_pragma)
998 {
999 _cpp_backup_tokens (pfile, count);
1000 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1001 }
1002 pfile->state.prevent_expansion--;
1003 }
1004
1005 static void
1006 do_pragma_once (pfile)
1007 cpp_reader *pfile;
1008 {
1009 cpp_warning (pfile, "#pragma once is obsolete");
1010
1011 if (pfile->buffer->prev == NULL)
1012 cpp_warning (pfile, "#pragma once in main file");
1013 else
1014 _cpp_never_reread (pfile->buffer->inc);
1015
1016 check_eol (pfile);
1017 }
1018
1019 static void
1020 do_pragma_poison (pfile)
1021 cpp_reader *pfile;
1022 {
1023 /* Poison these symbols so that all subsequent usage produces an
1024 error message. */
1025 const cpp_token *tok;
1026 cpp_hashnode *hp;
1027
1028 pfile->state.poisoned_ok = 1;
1029 for (;;)
1030 {
1031 tok = _cpp_lex_token (pfile);
1032 if (tok->type == CPP_EOF)
1033 break;
1034 if (tok->type != CPP_NAME)
1035 {
1036 cpp_error (pfile, "invalid #pragma GCC poison directive");
1037 break;
1038 }
1039
1040 hp = tok->val.node;
1041 if (hp->flags & NODE_POISONED)
1042 continue;
1043
1044 if (hp->type == NT_MACRO)
1045 cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
1046 _cpp_free_definition (hp);
1047 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1048 }
1049 pfile->state.poisoned_ok = 0;
1050 }
1051
1052 /* Mark the current header as a system header. This will suppress
1053 some categories of warnings (notably those from -pedantic). It is
1054 intended for use in system libraries that cannot be implemented in
1055 conforming C, but cannot be certain that their headers appear in a
1056 system include directory. To prevent abuse, it is rejected in the
1057 primary source file. */
1058 static void
1059 do_pragma_system_header (pfile)
1060 cpp_reader *pfile;
1061 {
1062 cpp_buffer *buffer = pfile->buffer;
1063
1064 if (buffer->prev == 0)
1065 cpp_warning (pfile, "#pragma system_header ignored outside include file");
1066 else
1067 {
1068 check_eol (pfile);
1069 skip_rest_of_line (pfile);
1070 cpp_make_system_header (pfile, 1, 0);
1071 }
1072 }
1073
1074 /* Check the modified date of the current include file against a specified
1075 file. Issue a diagnostic, if the specified file is newer. We use this to
1076 determine if a fixed header should be refixed. */
1077 static void
1078 do_pragma_dependency (pfile)
1079 cpp_reader *pfile;
1080 {
1081 cpp_token header, msg;
1082 int ordering;
1083
1084 if (parse_include (pfile, &header))
1085 return;
1086
1087 ordering = _cpp_compare_file_date (pfile, &header);
1088 if (ordering < 0)
1089 cpp_warning (pfile, "cannot find source %s",
1090 cpp_token_as_text (pfile, &header));
1091 else if (ordering > 0)
1092 {
1093 cpp_warning (pfile, "current file is older than %s",
1094 cpp_token_as_text (pfile, &header));
1095 cpp_get_token (pfile, &msg);
1096 if (msg.type != CPP_EOF)
1097 {
1098 _cpp_backup_tokens (pfile, 1);
1099 do_diagnostic (pfile, WARNING, 0);
1100 }
1101 }
1102 }
1103
1104 /* Check syntax is "(string-literal)". Returns 0 on success. */
1105 static int
1106 get__Pragma_string (pfile, string)
1107 cpp_reader *pfile;
1108 cpp_token *string;
1109 {
1110 cpp_token paren;
1111
1112 cpp_get_token (pfile, &paren);
1113 if (paren.type != CPP_OPEN_PAREN)
1114 return 1;
1115
1116 cpp_get_token (pfile, string);
1117 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1118 return 1;
1119
1120 cpp_get_token (pfile, &paren);
1121 return paren.type != CPP_CLOSE_PAREN;
1122 }
1123
1124 /* Returns a malloced buffer containing a destringized cpp_string by
1125 removing the first \ of \" and \\ sequences. */
1126 static unsigned char *
1127 destringize (in, len)
1128 const cpp_string *in;
1129 unsigned int *len;
1130 {
1131 const unsigned char *src, *limit;
1132 unsigned char *dest, *result;
1133
1134 dest = result = (unsigned char *) xmalloc (in->len);
1135 for (src = in->text, limit = src + in->len; src < limit;)
1136 {
1137 /* We know there is a character following the backslash. */
1138 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1139 src++;
1140 *dest++ = *src++;
1141 }
1142
1143 *len = dest - result;
1144 return result;
1145 }
1146
1147 void
1148 _cpp_do__Pragma (pfile)
1149 cpp_reader *pfile;
1150 {
1151 cpp_token string;
1152 unsigned char *buffer;
1153 unsigned int len;
1154
1155 if (get__Pragma_string (pfile, &string))
1156 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1157 else
1158 {
1159 /* Ideally, we'd like
1160 token1 _Pragma ("foo") token2
1161 to be output as
1162 token1
1163 # 7 "file.c"
1164 #pragma foo
1165 # 7 "file.c"
1166 token2
1167 Getting these correct line markers is a little tricky. */
1168
1169 unsigned int orig_line = pfile->line;
1170 buffer = destringize (&string.val.str, &len);
1171 run_directive (pfile, T_PRAGMA, (char *) buffer, len);
1172 free ((PTR) buffer);
1173 pfile->line = orig_line;
1174 pfile->buffer->saved_flags = BOL;
1175 }
1176 }
1177
1178 /* Just ignore #sccs, on systems where we define it at all. */
1179 #ifdef SCCS_DIRECTIVE
1180 static void
1181 do_sccs (pfile)
1182 cpp_reader *pfile ATTRIBUTE_UNUSED;
1183 {
1184 }
1185 #endif
1186
1187 static void
1188 do_ifdef (pfile)
1189 cpp_reader *pfile;
1190 {
1191 int skip = 1;
1192
1193 if (! pfile->state.skipping)
1194 {
1195 const cpp_hashnode *node = lex_macro_node (pfile);
1196
1197 if (node)
1198 skip = node->type != NT_MACRO;
1199
1200 if (node)
1201 check_eol (pfile);
1202 }
1203
1204 push_conditional (pfile, skip, T_IFDEF, 0);
1205 }
1206
1207 static void
1208 do_ifndef (pfile)
1209 cpp_reader *pfile;
1210 {
1211 int skip = 1;
1212 const cpp_hashnode *node = 0;
1213
1214 if (! pfile->state.skipping)
1215 {
1216 node = lex_macro_node (pfile);
1217 if (node)
1218 skip = node->type == NT_MACRO;
1219
1220 if (node)
1221 check_eol (pfile);
1222 }
1223
1224 push_conditional (pfile, skip, T_IFNDEF, node);
1225 }
1226
1227 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1228 pfile->mi_ind_cmacro so we can handle multiple-include
1229 optimisations. If macro expansion occurs in the expression, we
1230 cannot treat it as a controlling conditional, since the expansion
1231 could change in the future. That is handled by cpp_get_token. */
1232
1233 static void
1234 do_if (pfile)
1235 cpp_reader *pfile;
1236 {
1237 int skip = 1;
1238
1239 if (! pfile->state.skipping)
1240 skip = _cpp_parse_expr (pfile) == 0;
1241
1242 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1243 }
1244
1245 /* Flip skipping state if appropriate and continue without changing
1246 if_stack; this is so that the error message for missing #endif's
1247 etc. will point to the original #if. */
1248
1249 static void
1250 do_else (pfile)
1251 cpp_reader *pfile;
1252 {
1253 cpp_buffer *buffer = pfile->buffer;
1254 struct if_stack *ifs = buffer->if_stack;
1255
1256 if (ifs == NULL)
1257 cpp_error (pfile, "#else without #if");
1258 else
1259 {
1260 if (ifs->type == T_ELSE)
1261 {
1262 cpp_error (pfile, "#else after #else");
1263 cpp_error_with_line (pfile, ifs->line, 0,
1264 "the conditional began here");
1265 }
1266 ifs->type = T_ELSE;
1267
1268 /* Skip any future (erroneous) #elses or #elifs. */
1269 pfile->state.skipping = ifs->skip_elses;
1270 ifs->skip_elses = true;
1271
1272 /* Invalidate any controlling macro. */
1273 ifs->mi_cmacro = 0;
1274
1275 /* Only check EOL if was not originally skipping. */
1276 if (!ifs->was_skipping)
1277 check_eol (pfile);
1278 }
1279 }
1280
1281 /* handle a #elif directive by not changing if_stack either. see the
1282 comment above do_else. */
1283
1284 static void
1285 do_elif (pfile)
1286 cpp_reader *pfile;
1287 {
1288 cpp_buffer *buffer = pfile->buffer;
1289 struct if_stack *ifs = buffer->if_stack;
1290
1291 if (ifs == NULL)
1292 cpp_error (pfile, "#elif without #if");
1293 else
1294 {
1295 if (ifs->type == T_ELSE)
1296 {
1297 cpp_error (pfile, "#elif after #else");
1298 cpp_error_with_line (pfile, ifs->line, 0,
1299 "the conditional began here");
1300 }
1301 ifs->type = T_ELIF;
1302
1303 /* Only evaluate this if we aren't skipping elses. During
1304 evaluation, set skipping to false to get lexer warnings. */
1305 if (ifs->skip_elses)
1306 pfile->state.skipping = 1;
1307 else
1308 {
1309 pfile->state.skipping = 0;
1310 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1311 ifs->skip_elses = ! pfile->state.skipping;
1312 }
1313
1314 /* Invalidate any controlling macro. */
1315 ifs->mi_cmacro = 0;
1316 }
1317 }
1318
1319 /* #endif pops the if stack and resets pfile->state.skipping. */
1320
1321 static void
1322 do_endif (pfile)
1323 cpp_reader *pfile;
1324 {
1325 cpp_buffer *buffer = pfile->buffer;
1326 struct if_stack *ifs = buffer->if_stack;
1327
1328 if (ifs == NULL)
1329 cpp_error (pfile, "#endif without #if");
1330 else
1331 {
1332 /* Only check EOL if was not originally skipping. */
1333 if (!ifs->was_skipping)
1334 check_eol (pfile);
1335
1336 /* If potential control macro, we go back outside again. */
1337 if (ifs->next == 0 && ifs->mi_cmacro)
1338 {
1339 pfile->mi_valid = true;
1340 pfile->mi_cmacro = ifs->mi_cmacro;
1341 }
1342
1343 buffer->if_stack = ifs->next;
1344 pfile->state.skipping = ifs->was_skipping;
1345 obstack_free (&pfile->buffer_ob, ifs);
1346 }
1347 }
1348
1349 /* Push an if_stack entry and set pfile->state.skipping accordingly.
1350 If this is a #if or #ifndef, CMACRO is a potentially controlling
1351 macro - we need to check here that we are at the top of the file. */
1352
1353 static void
1354 push_conditional (pfile, skip, type, cmacro)
1355 cpp_reader *pfile;
1356 int skip;
1357 int type;
1358 const cpp_hashnode *cmacro;
1359 {
1360 struct if_stack *ifs;
1361 cpp_buffer *buffer = pfile->buffer;
1362
1363 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1364 ifs->line = pfile->directive_line;
1365 ifs->next = buffer->if_stack;
1366 ifs->skip_elses = pfile->state.skipping || !skip;
1367 ifs->was_skipping = pfile->state.skipping;
1368 ifs->type = type;
1369 /* This condition is effectively a test for top-of-file. */
1370 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1371 ifs->mi_cmacro = cmacro;
1372 else
1373 ifs->mi_cmacro = 0;
1374
1375 pfile->state.skipping = skip;
1376 buffer->if_stack = ifs;
1377 }
1378
1379 /* Read the tokens of the answer into the macro pool. Only commit the
1380 memory if we intend it as permanent storage, i.e. the #assert case.
1381 Returns 0 on success. */
1382
1383 static int
1384 parse_answer (pfile, answerp, type)
1385 cpp_reader *pfile;
1386 struct answer **answerp;
1387 int type;
1388 {
1389 cpp_token paren, *token;
1390 struct answer *answer;
1391
1392 if (POOL_FRONT (&pfile->macro_pool) + sizeof (struct answer) >
1393 POOL_LIMIT (&pfile->macro_pool))
1394 _cpp_next_chunk (&pfile->macro_pool, sizeof (struct answer), 0);
1395 answer = (struct answer *) POOL_FRONT (&pfile->macro_pool);
1396 answer->count = 0;
1397
1398 /* In a conditional, it is legal to not have an open paren. We
1399 should save the following token in this case. */
1400 cpp_get_token (pfile, &paren);
1401
1402 /* If not a paren, see if we're OK. */
1403 if (paren.type != CPP_OPEN_PAREN)
1404 {
1405 /* In a conditional no answer is a test for any answer. It
1406 could be followed by any token. */
1407 if (type == T_IF)
1408 {
1409 _cpp_backup_tokens (pfile, 1);
1410 return 0;
1411 }
1412
1413 /* #unassert with no answer is valid - it removes all answers. */
1414 if (type == T_UNASSERT && paren.type == CPP_EOF)
1415 return 0;
1416
1417 cpp_error (pfile, "missing '(' after predicate");
1418 return 1;
1419 }
1420
1421 for (;;)
1422 {
1423 token = &answer->first[answer->count];
1424 /* Check we have room for the token. */
1425 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1426 {
1427 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1428 (unsigned char **) &answer);
1429 token = &answer->first[answer->count];
1430 }
1431
1432 cpp_get_token (pfile, token);
1433 if (token->type == CPP_CLOSE_PAREN)
1434 break;
1435
1436 if (token->type == CPP_EOF)
1437 {
1438 cpp_error (pfile, "missing ')' to complete answer");
1439 return 1;
1440 }
1441 answer->count++;
1442 }
1443
1444 if (answer->count == 0)
1445 {
1446 cpp_error (pfile, "predicate's answer is empty");
1447 return 1;
1448 }
1449
1450 /* Drop whitespace at start. */
1451 answer->first->flags &= ~PREV_WHITE;
1452 *answerp = answer;
1453
1454 if (type == T_ASSERT || type == T_UNASSERT)
1455 check_eol (pfile);
1456 return 0;
1457 }
1458
1459 /* Parses an assertion, returning a pointer to the hash node of the
1460 predicate, or 0 on error. If an answer was supplied, it is placed
1461 in ANSWERP, otherwise it is set to 0. */
1462 static cpp_hashnode *
1463 parse_assertion (pfile, answerp, type)
1464 cpp_reader *pfile;
1465 struct answer **answerp;
1466 int type;
1467 {
1468 cpp_hashnode *result = 0;
1469 cpp_token predicate;
1470
1471 /* We don't expand predicates or answers. */
1472 pfile->state.prevent_expansion++;
1473
1474 *answerp = 0;
1475 cpp_get_token (pfile, &predicate);
1476 if (predicate.type == CPP_EOF)
1477 cpp_error (pfile, "assertion without predicate");
1478 else if (predicate.type != CPP_NAME)
1479 cpp_error (pfile, "predicate must be an identifier");
1480 else if (parse_answer (pfile, answerp, type) == 0)
1481 {
1482 unsigned int len = NODE_LEN (predicate.val.node);
1483 unsigned char *sym = alloca (len + 1);
1484
1485 /* Prefix '#' to get it out of macro namespace. */
1486 sym[0] = '#';
1487 memcpy (sym + 1, NODE_NAME (predicate.val.node), len);
1488 result = cpp_lookup (pfile, sym, len + 1);
1489 }
1490
1491 pfile->state.prevent_expansion--;
1492 return result;
1493 }
1494
1495 /* Returns a pointer to the pointer to the answer in the answer chain,
1496 or a pointer to NULL if the answer is not in the chain. */
1497 static struct answer **
1498 find_answer (node, candidate)
1499 cpp_hashnode *node;
1500 const struct answer *candidate;
1501 {
1502 unsigned int i;
1503 struct answer **result;
1504
1505 for (result = &node->value.answers; *result; result = &(*result)->next)
1506 {
1507 struct answer *answer = *result;
1508
1509 if (answer->count == candidate->count)
1510 {
1511 for (i = 0; i < answer->count; i++)
1512 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1513 break;
1514
1515 if (i == answer->count)
1516 break;
1517 }
1518 }
1519
1520 return result;
1521 }
1522
1523 /* Test an assertion within a preprocessor conditional. Returns
1524 non-zero on failure, zero on success. On success, the result of
1525 the test is written into VALUE. */
1526 int
1527 _cpp_test_assertion (pfile, value)
1528 cpp_reader *pfile;
1529 int *value;
1530 {
1531 struct answer *answer;
1532 cpp_hashnode *node;
1533
1534 node = parse_assertion (pfile, &answer, T_IF);
1535 if (node)
1536 *value = (node->type == NT_ASSERTION &&
1537 (answer == 0 || *find_answer (node, answer) != 0));
1538
1539 /* We don't commit the memory for the answer - it's temporary only. */
1540 return node == 0;
1541 }
1542
1543 static void
1544 do_assert (pfile)
1545 cpp_reader *pfile;
1546 {
1547 struct answer *new_answer;
1548 cpp_hashnode *node;
1549
1550 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1551 if (node)
1552 {
1553 /* Place the new answer in the answer list. First check there
1554 is not a duplicate. */
1555 new_answer->next = 0;
1556 if (node->type == NT_ASSERTION)
1557 {
1558 if (*find_answer (node, new_answer))
1559 {
1560 cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
1561 return;
1562 }
1563 new_answer->next = node->value.answers;
1564 }
1565 node->type = NT_ASSERTION;
1566 node->value.answers = new_answer;
1567 POOL_COMMIT (&pfile->macro_pool, (sizeof (struct answer)
1568 + (new_answer->count - 1)
1569 * sizeof (cpp_token)));
1570 }
1571 }
1572
1573 static void
1574 do_unassert (pfile)
1575 cpp_reader *pfile;
1576 {
1577 cpp_hashnode *node;
1578 struct answer *answer;
1579
1580 node = parse_assertion (pfile, &answer, T_UNASSERT);
1581 /* It isn't an error to #unassert something that isn't asserted. */
1582 if (node && node->type == NT_ASSERTION)
1583 {
1584 if (answer)
1585 {
1586 struct answer **p = find_answer (node, answer), *temp;
1587
1588 /* Remove the answer from the list. */
1589 temp = *p;
1590 if (temp)
1591 *p = temp->next;
1592
1593 /* Did we free the last answer? */
1594 if (node->value.answers == 0)
1595 node->type = NT_VOID;
1596 }
1597 else
1598 _cpp_free_definition (node);
1599 }
1600
1601 /* We don't commit the memory for the answer - it's temporary only. */
1602 }
1603
1604 /* These are for -D, -U, -A. */
1605
1606 /* Process the string STR as if it appeared as the body of a #define.
1607 If STR is just an identifier, define it with value 1.
1608 If STR has anything after the identifier, then it should
1609 be identifier=definition. */
1610
1611 void
1612 cpp_define (pfile, str)
1613 cpp_reader *pfile;
1614 const char *str;
1615 {
1616 char *buf, *p;
1617 size_t count;
1618
1619 /* Copy the entire option so we can modify it.
1620 Change the first "=" in the string to a space. If there is none,
1621 tack " 1" on the end. */
1622
1623 /* Length including the null. */
1624 count = strlen (str);
1625 buf = (char *) alloca (count + 2);
1626 memcpy (buf, str, count);
1627
1628 p = strchr (str, '=');
1629 if (p)
1630 buf[p - str] = ' ';
1631 else
1632 {
1633 buf[count++] = ' ';
1634 buf[count++] = '1';
1635 }
1636
1637 run_directive (pfile, T_DEFINE, buf, count);
1638 }
1639
1640 /* Slight variant of the above for use by initialize_builtins. */
1641 void
1642 _cpp_define_builtin (pfile, str)
1643 cpp_reader *pfile;
1644 const char *str;
1645 {
1646 run_directive (pfile, T_DEFINE, str, strlen (str));
1647 }
1648
1649 /* Process MACRO as if it appeared as the body of an #undef. */
1650 void
1651 cpp_undef (pfile, macro)
1652 cpp_reader *pfile;
1653 const char *macro;
1654 {
1655 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1656 }
1657
1658 /* Process the string STR as if it appeared as the body of a #assert. */
1659 void
1660 cpp_assert (pfile, str)
1661 cpp_reader *pfile;
1662 const char *str;
1663 {
1664 handle_assertion (pfile, str, T_ASSERT);
1665 }
1666
1667 /* Process STR as if it appeared as the body of an #unassert. */
1668 void
1669 cpp_unassert (pfile, str)
1670 cpp_reader *pfile;
1671 const char *str;
1672 {
1673 handle_assertion (pfile, str, T_UNASSERT);
1674 }
1675
1676 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1677 static void
1678 handle_assertion (pfile, str, type)
1679 cpp_reader *pfile;
1680 const char *str;
1681 int type;
1682 {
1683 size_t count = strlen (str);
1684 const char *p = strchr (str, '=');
1685
1686 if (p)
1687 {
1688 /* Copy the entire option so we can modify it. Change the first
1689 "=" in the string to a '(', and tack a ')' on the end. */
1690 char *buf = (char *) alloca (count + 1);
1691
1692 memcpy (buf, str, count);
1693 buf[p - str] = '(';
1694 buf[count++] = ')';
1695 str = buf;
1696 }
1697
1698 run_directive (pfile, type, str, count);
1699 }
1700
1701 /* The number of errors for a given reader. */
1702 unsigned int
1703 cpp_errors (pfile)
1704 cpp_reader *pfile;
1705 {
1706 return pfile->errors;
1707 }
1708
1709 /* The options structure. */
1710 cpp_options *
1711 cpp_get_options (pfile)
1712 cpp_reader *pfile;
1713 {
1714 return &pfile->opts;
1715 }
1716
1717 /* The callbacks structure. */
1718 cpp_callbacks *
1719 cpp_get_callbacks (pfile)
1720 cpp_reader *pfile;
1721 {
1722 return &pfile->cb;
1723 }
1724
1725 /* The line map set. */
1726 const struct line_maps *
1727 cpp_get_line_maps (pfile)
1728 cpp_reader *pfile;
1729 {
1730 return &pfile->line_maps;
1731 }
1732
1733 /* Copy the given callbacks structure to our own. */
1734 void
1735 cpp_set_callbacks (pfile, cb)
1736 cpp_reader *pfile;
1737 cpp_callbacks *cb;
1738 {
1739 pfile->cb = *cb;
1740 }
1741
1742 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1743 doesn't fail. It does not generate a file change call back; that
1744 is the responsibility of the caller. */
1745 cpp_buffer *
1746 cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
1747 cpp_reader *pfile;
1748 const U_CHAR *buffer;
1749 size_t len;
1750 int from_stage3;
1751 int return_at_eof;
1752 {
1753 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1754
1755 /* Clears, amongst other things, if_stack and mi_cmacro. */
1756 memset (new, 0, sizeof (cpp_buffer));
1757
1758 new->line_base = new->buf = new->cur = buffer;
1759 new->rlimit = buffer + len;
1760
1761 /* No read ahead or extra char initially. */
1762 new->read_ahead = EOF;
1763 new->extra_char = EOF;
1764 new->from_stage3 = from_stage3;
1765 new->prev = pfile->buffer;
1766 new->return_at_eof = return_at_eof;
1767 new->saved_flags = BOL;
1768
1769 pfile->buffer = new;
1770
1771 return new;
1772 }
1773
1774 /* If called from do_line, pops a single buffer. Otherwise pops all
1775 buffers until a real file is reached. Generates appropriate
1776 call-backs. */
1777 void
1778 _cpp_pop_buffer (pfile)
1779 cpp_reader *pfile;
1780 {
1781 cpp_buffer *buffer = pfile->buffer;
1782 struct if_stack *ifs;
1783
1784 /* Walk back up the conditional stack till we reach its level at
1785 entry to this file, issuing error messages. */
1786 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1787 cpp_error_with_line (pfile, ifs->line, 0,
1788 "unterminated #%s", dtable[ifs->type].name);
1789
1790 /* In case of a missing #endif. */
1791 pfile->state.skipping = 0;
1792
1793 /* Update the reader's buffer before _cpp_do_file_change. */
1794 pfile->buffer = buffer->prev;
1795
1796 if (buffer->inc)
1797 _cpp_pop_file_buffer (pfile, buffer->inc);
1798
1799 obstack_free (&pfile->buffer_ob, buffer);
1800 }
1801
1802 void
1803 _cpp_init_directives (pfile)
1804 cpp_reader *pfile;
1805 {
1806 unsigned int i;
1807 cpp_hashnode *node;
1808
1809 /* Register the directives. */
1810 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1811 {
1812 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1813 node->directive_index = i + 1;
1814 }
1815 }