* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / cpplib.c
blob093c9dc806f4a2dc806942cf50387827097083d9
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
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "obstack.h"
29 /* Chained list of answers to an assertion. */
30 struct answer
32 struct answer *next;
33 unsigned int count;
34 cpp_token first[1];
37 /* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
40 struct if_stack
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. */
50 /* Contains a registered pragma or pragma namespace. */
51 typedef void (*pragma_cb) PARAMS ((cpp_reader *));
52 struct pragma_entry
54 struct pragma_entry *next;
55 const cpp_hashnode *pragma; /* Name and length. */
56 int is_nspace;
57 union {
58 pragma_cb handler;
59 struct pragma_entry *space;
60 } u;
63 /* Values for the origin field of struct directive. KANDR directives
64 come from traditional (K&R) C. STDC89 directives come from the
65 1989 C standard. EXTENSION directives are extensions. */
66 #define KANDR 0
67 #define STDC89 1
68 #define EXTENSION 2
70 /* Values for the flags field of struct directive. COND indicates a
71 conditional; IF_COND an opening conditional. INCL means to treat
72 "..." and <...> as q-char and h-char sequences respectively. IN_I
73 means this directive should be handled even if -fpreprocessed is in
74 effect (these are the directives with callback hooks). */
75 #define COND (1 << 0)
76 #define IF_COND (1 << 1)
77 #define INCL (1 << 2)
78 #define IN_I (1 << 3)
80 /* Defines one #-directive, including how to handle it. */
81 typedef void (*directive_handler) PARAMS ((cpp_reader *));
82 typedef struct directive directive;
83 struct directive
85 directive_handler handler; /* Function to handle directive. */
86 const U_CHAR *name; /* Name of directive. */
87 unsigned short length; /* Length of name. */
88 unsigned char origin; /* Origin of directive. */
89 unsigned char flags; /* Flags describing this directive. */
92 /* Forward declarations. */
94 static void skip_rest_of_line PARAMS ((cpp_reader *));
95 static void check_eol PARAMS ((cpp_reader *));
96 static void start_directive PARAMS ((cpp_reader *));
97 static void end_directive PARAMS ((cpp_reader *, int));
98 static void directive_diagnostics
99 PARAMS ((cpp_reader *, const directive *, int));
100 static void run_directive PARAMS ((cpp_reader *, int,
101 const char *, size_t));
102 static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
103 static const cpp_token *parse_include PARAMS ((cpp_reader *));
104 static void push_conditional PARAMS ((cpp_reader *, int, int,
105 const cpp_hashnode *));
106 static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
107 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
108 unsigned long *));
109 static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
110 static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
111 static void do_include_common PARAMS ((cpp_reader *, enum include_type));
112 static struct pragma_entry *lookup_pragma_entry
113 PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma));
114 static struct pragma_entry *insert_pragma_entry
115 PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *,
116 pragma_cb));
117 static void do_pragma_once PARAMS ((cpp_reader *));
118 static void do_pragma_poison PARAMS ((cpp_reader *));
119 static void do_pragma_system_header PARAMS ((cpp_reader *));
120 static void do_pragma_dependency PARAMS ((cpp_reader *));
121 static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
122 static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
123 static unsigned char *destringize PARAMS ((const cpp_string *,
124 unsigned int *));
125 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
126 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
127 int));
128 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
129 const struct answer *));
130 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
132 /* This is the table of directive handlers. It is ordered by
133 frequency of occurrence; the numbers at the end are directive
134 counts from all the source code I have lying around (egcs and libc
135 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
136 pcmcia-cs-3.0.9). This is no longer important as directive lookup
137 is now O(1). All extensions other than #warning and #include_next
138 are deprecated. The name is where the extension appears to have
139 come from. */
141 #define DIRECTIVE_TABLE \
142 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
143 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
144 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
145 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
146 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
147 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
148 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
149 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
150 D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
151 D(elif, T_ELIF, STDC89, COND) /* 610 */ \
152 D(error, T_ERROR, STDC89, 0) /* 475 */ \
153 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
154 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
155 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
156 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
157 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
158 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
159 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
160 SCCS_ENTRY /* 0 SVR4? */
162 /* #sccs is not always recognized. */
163 #ifdef SCCS_DIRECTIVE
164 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
165 #else
166 # define SCCS_ENTRY /* nothing */
167 #endif
169 /* Use the table to generate a series of prototypes, an enum for the
170 directive names, and an array of directive handlers. */
172 /* The directive-processing functions are declared to return int
173 instead of void, because some old compilers have trouble with
174 pointers to functions returning void. */
176 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
177 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
178 DIRECTIVE_TABLE
179 #undef D
181 #define D(n, tag, o, f) tag,
182 enum
184 DIRECTIVE_TABLE
185 N_DIRECTIVES
187 #undef D
189 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
190 #define D(name, t, origin, flags) \
191 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
192 sizeof STRINGX(name) - 1, origin, flags },
193 static const directive dtable[] =
195 DIRECTIVE_TABLE
197 #undef D
198 #undef DIRECTIVE_TABLE
200 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
202 /* Skip any remaining tokens in a directive. */
203 static void
204 skip_rest_of_line (pfile)
205 cpp_reader *pfile;
207 /* Discard all stacked contexts. */
208 while (pfile->context != &pfile->base_context)
209 _cpp_pop_context (pfile);
211 /* Sweep up all tokens remaining on the line. */
212 if (! SEEN_EOL ())
213 while (_cpp_lex_token (pfile)->type != CPP_EOF)
217 /* Ensure there are no stray tokens at the end of a directive. */
218 static void
219 check_eol (pfile)
220 cpp_reader *pfile;
222 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
223 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
224 pfile->directive->name);
227 /* Called when entering a directive, _Pragma or command-line directive. */
228 static void
229 start_directive (pfile)
230 cpp_reader *pfile;
232 /* Setup in-directive state. */
233 pfile->state.in_directive = 1;
234 pfile->state.save_comments = 0;
236 /* Some handlers need the position of the # for diagnostics. */
237 pfile->directive_line = pfile->line;
240 /* Called when leaving a directive, _Pragma or command-line directive. */
241 static void
242 end_directive (pfile, skip_line)
243 cpp_reader *pfile;
244 int skip_line;
246 /* We don't skip for an assembler #. */
247 if (skip_line)
249 skip_rest_of_line (pfile);
250 if (!pfile->keep_tokens)
252 pfile->cur_run = &pfile->base_run;
253 pfile->cur_token = pfile->base_run.base;
257 /* Restore state. */
258 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
259 pfile->state.in_directive = 0;
260 pfile->state.angled_headers = 0;
261 pfile->state.line_extension = 0;
262 pfile->directive = 0;
265 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
266 the '#' was indented. */
268 static void
269 directive_diagnostics (pfile, dir, indented)
270 cpp_reader *pfile;
271 const directive *dir;
272 int indented;
274 if (pfile->state.line_extension)
276 if (CPP_PEDANTIC (pfile)
277 && ! pfile->state.skipping)
278 cpp_pedwarn (pfile, "style of line directive is a GCC extension");
280 else
282 /* Issue -pedantic warnings for extensions. */
283 if (CPP_PEDANTIC (pfile)
284 && ! pfile->state.skipping
285 && dir->origin == EXTENSION)
286 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
288 /* Traditionally, a directive is ignored unless its # is in
289 column 1. Therefore in code intended to work with K+R
290 compilers, directives added by C89 must have their #
291 indented, and directives present in traditional C must not.
292 This is true even of directives in skipped conditional
293 blocks. */
294 if (CPP_WTRADITIONAL (pfile))
296 if (dir == &dtable[T_ELIF])
297 cpp_warning (pfile, "suggest not using #elif in traditional C");
298 else if (indented && dir->origin == KANDR)
299 cpp_warning (pfile,
300 "traditional C ignores #%s with the # indented",
301 dir->name);
302 else if (!indented && dir->origin != KANDR)
303 cpp_warning (pfile,
304 "suggest hiding #%s from traditional C with an indented #",
305 dir->name);
310 /* Check if we have a known directive. INDENTED is non-zero if the
311 '#' of the directive was indented. This function is in this file
312 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
313 non-zero if the line of tokens has been handled, zero if we should
314 continue processing the line. */
317 _cpp_handle_directive (pfile, indented)
318 cpp_reader *pfile;
319 int indented;
321 const directive *dir = 0;
322 const cpp_token *dname;
323 int skip = 1;
325 start_directive (pfile);
326 dname = _cpp_lex_token (pfile);
328 if (dname->type == CPP_NAME)
330 if (dname->val.node->directive_index)
331 dir = &dtable[dname->val.node->directive_index - 1];
333 /* We do not recognise the # followed by a number extension in
334 assembler code. */
335 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
337 dir = &dtable[T_LINE];
338 pfile->state.line_extension = 1;
341 if (dir)
343 /* If we have a directive that is not an opening conditional,
344 invalidate any control macro. */
345 if (! (dir->flags & IF_COND))
346 pfile->mi_valid = false;
348 /* Kluge alert. In order to be sure that code like this
350 #define HASH #
351 HASH define foo bar
353 does not cause '#define foo bar' to get executed when
354 compiled with -save-temps, we recognize directives in
355 -fpreprocessed mode only if the # is in column 1. cppmacro.c
356 puts a space in fron of any '#' at the start of a macro. */
357 if (CPP_OPTION (pfile, preprocessed)
358 && (indented || !(dir->flags & IN_I)))
360 skip = 0;
361 dir = 0;
363 else
365 /* In failed conditional groups, all non-conditional
366 directives are ignored. Before doing that, whether
367 skipping or not, we should lex angle-bracketed headers
368 correctly, and maybe output some diagnostics. */
369 pfile->state.angled_headers = dir->flags & INCL;
370 if (! CPP_OPTION (pfile, preprocessed))
371 directive_diagnostics (pfile, dir, indented);
372 if (pfile->state.skipping && !(dir->flags & COND))
373 dir = 0;
376 else if (dname->type == CPP_EOF)
377 ; /* CPP_EOF is the "null directive". */
378 else
380 /* An unknown directive. Don't complain about it in assembly
381 source: we don't know where the comments are, and # may
382 introduce assembler pseudo-ops. Don't complain about invalid
383 directives in skipped conditional groups (6.10 p4). */
384 if (CPP_OPTION (pfile, lang) == CLK_ASM)
385 skip = 0;
386 else if (!pfile->state.skipping)
387 cpp_error (pfile, "invalid preprocessing directive #%s",
388 cpp_token_as_text (pfile, dname));
391 if (dir)
393 pfile->directive = dir;
394 (*pfile->directive->handler) (pfile);
396 else if (skip == 0)
397 _cpp_backup_tokens (pfile, 1);
399 end_directive (pfile, skip);
400 return skip;
403 /* Directive handler wrapper used by the command line option
404 processor. */
405 static void
406 run_directive (pfile, dir_no, buf, count)
407 cpp_reader *pfile;
408 int dir_no;
409 const char *buf;
410 size_t count;
412 cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
413 /* from_stage3 */ true, 1);
414 start_directive (pfile);
415 /* We don't want a leading # to be interpreted as a directive. */
416 pfile->buffer->saved_flags = 0;
417 pfile->directive = &dtable[dir_no];
418 (void) (*pfile->directive->handler) (pfile);
419 end_directive (pfile, 1);
420 _cpp_pop_buffer (pfile);
423 /* Checks for validity the macro name in #define, #undef, #ifdef and
424 #ifndef directives. */
425 static cpp_hashnode *
426 lex_macro_node (pfile)
427 cpp_reader *pfile;
429 cpp_hashnode *node;
430 const cpp_token *token = _cpp_lex_token (pfile);
432 /* The token immediately after #define must be an identifier. That
433 identifier may not be "defined", per C99 6.10.8p4.
434 In C++, it may not be any of the "named operators" either,
435 per C++98 [lex.digraph], [lex.key].
436 Finally, the identifier may not have been poisoned. (In that case
437 the lexer has issued the error message for us.) */
439 if (token->type != CPP_NAME)
441 if (token->type == CPP_EOF)
442 cpp_error (pfile, "no macro name given in #%s directive",
443 pfile->directive->name);
444 else if (token->flags & NAMED_OP)
445 cpp_error (pfile,
446 "\"%s\" cannot be used as a macro name as it is an operator in C++",
447 NODE_NAME (token->val.node));
448 else
449 cpp_error (pfile, "macro names must be identifiers");
451 return 0;
454 node = token->val.node;
455 if (node->flags & NODE_POISONED)
456 return 0;
458 if (node == pfile->spec_nodes.n_defined)
460 cpp_error (pfile, "\"%s\" cannot be used as a macro name",
461 NODE_NAME (node));
462 return 0;
465 return node;
468 /* Process a #define directive. Most work is done in cppmacro.c. */
469 static void
470 do_define (pfile)
471 cpp_reader *pfile;
473 cpp_hashnode *node = lex_macro_node (pfile);
475 if (node)
477 if (_cpp_create_definition (pfile, node))
478 if (pfile->cb.define)
479 (*pfile->cb.define) (pfile, pfile->directive_line, node);
483 /* Handle #undef. Marks the identifier NT_VOID in the hash table. */
484 static void
485 do_undef (pfile)
486 cpp_reader *pfile;
488 cpp_hashnode *node = lex_macro_node (pfile);
490 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
491 is not currently defined as a macro name. */
492 if (node && node->type == NT_MACRO)
494 if (pfile->cb.undef)
495 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
497 if (node->flags & NODE_WARN)
498 cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
500 _cpp_free_definition (node);
502 check_eol (pfile);
505 /* Helper routine used by parse_include. Reinterpret the current line
506 as an h-char-sequence (< ... >); we are looking at the first token
507 after the <. Returns the header as a token, or NULL on failure. */
508 static const cpp_token *
509 glue_header_name (pfile)
510 cpp_reader *pfile;
512 cpp_token *header = NULL;
513 const cpp_token *token;
514 unsigned char *dest;
515 size_t len;
517 /* To avoid lexed tokens overwriting our glued name, we can only
518 allocate from the string pool once we've lexed everything. */
520 dest = BUFF_FRONT (pfile->u_buff);
521 for (;;)
523 token = cpp_get_token (pfile);
525 if (token->type == CPP_GREATER || token->type == CPP_EOF)
526 break;
528 /* + 1 for terminating NUL. */
529 len = cpp_token_len (token) + 1;
530 if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
532 size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
533 _cpp_extend_buff (pfile, &pfile->u_buff, len);
534 dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
537 if (token->flags & PREV_WHITE)
538 *dest++ = ' ';
540 dest = cpp_spell_token (pfile, token, dest);
543 if (token->type == CPP_EOF)
544 cpp_error (pfile, "missing terminating > character");
545 else
547 header = _cpp_temp_token (pfile);
548 header->type = CPP_HEADER_NAME;
549 header->flags = 0;
550 header->val.str.len = dest - BUFF_FRONT (pfile->u_buff);
551 header->val.str.text = BUFF_FRONT (pfile->u_buff);
552 *dest++ = '\0';
553 BUFF_FRONT (pfile->u_buff) = dest;
556 return header;
559 /* Returns the header string of #include, #include_next, #import and
560 #pragma dependency. Returns NULL on error. */
561 static const cpp_token *
562 parse_include (pfile)
563 cpp_reader *pfile;
565 const unsigned char *dir;
566 const cpp_token *header;
568 if (pfile->directive == &dtable[T_PRAGMA])
569 dir = U"pragma dependency";
570 else
571 dir = pfile->directive->name;
573 /* Allow macro expansion. */
574 header = cpp_get_token (pfile);
575 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
577 if (header->type != CPP_LESS)
579 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
580 return NULL;
583 header = glue_header_name (pfile);
584 if (header == NULL)
585 return header;
588 if (header->val.str.len == 0)
590 cpp_error (pfile, "empty file name in #%s", dir);
591 return NULL;
594 return header;
597 /* Handle #include, #include_next and #import. */
598 static void
599 do_include_common (pfile, type)
600 cpp_reader *pfile;
601 enum include_type type;
603 const cpp_token *header;
605 /* For #include_next, if this is the primary source file, warn and
606 use the normal search logic. */
607 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
609 cpp_warning (pfile, "#include_next in primary source file");
610 type = IT_INCLUDE;
612 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
614 CPP_OPTION (pfile, warn_import) = 0;
615 cpp_warning (pfile,
616 "#import is obsolete, use an #ifndef wrapper in the header file");
619 header = parse_include (pfile);
620 if (header)
622 /* Prevent #include recursion. */
623 if (pfile->line_maps.depth >= CPP_STACK_MAX)
624 cpp_fatal (pfile, "#include nested too deeply");
625 else
627 check_eol (pfile);
628 /* Get out of macro context, if we are. */
629 skip_rest_of_line (pfile);
630 if (pfile->cb.include)
631 (*pfile->cb.include) (pfile, pfile->directive_line,
632 pfile->directive->name, header);
634 _cpp_execute_include (pfile, header, type);
639 static void
640 do_include (pfile)
641 cpp_reader *pfile;
643 do_include_common (pfile, IT_INCLUDE);
646 static void
647 do_import (pfile)
648 cpp_reader *pfile;
650 do_include_common (pfile, IT_IMPORT);
653 static void
654 do_include_next (pfile)
655 cpp_reader *pfile;
657 do_include_common (pfile, IT_INCLUDE_NEXT);
660 /* Subroutine of do_line. Read possible flags after file name. LAST
661 is the last flag seen; 0 if this is the first flag. Return the flag
662 if it is valid, 0 at the end of the directive. Otherwise complain. */
664 static unsigned int
665 read_flag (pfile, last)
666 cpp_reader *pfile;
667 unsigned int last;
669 const cpp_token *token = _cpp_lex_token (pfile);
671 if (token->type == CPP_NUMBER && token->val.str.len == 1)
673 unsigned int flag = token->val.str.text[0] - '0';
675 if (flag > last && flag <= 4
676 && (flag != 4 || last == 3)
677 && (flag != 2 || last == 0))
678 return flag;
681 if (token->type != CPP_EOF)
682 cpp_error (pfile, "invalid flag \"%s\" in line directive",
683 cpp_token_as_text (pfile, token));
684 return 0;
687 /* Another subroutine of do_line. Convert a number in STR, of length
688 LEN, to binary; store it in NUMP, and return 0 if the number was
689 well-formed, 1 if not. Temporary, hopefully. */
690 static int
691 strtoul_for_line (str, len, nump)
692 const U_CHAR *str;
693 unsigned int len;
694 unsigned long *nump;
696 unsigned long reg = 0;
697 U_CHAR c;
698 while (len--)
700 c = *str++;
701 if (!ISDIGIT (c))
702 return 1;
703 reg *= 10;
704 reg += c - '0';
706 *nump = reg;
707 return 0;
710 /* Interpret #line command.
711 Note that the filename string (if any) is treated as if it were an
712 include filename. That means no escape handling. */
714 static void
715 do_line (pfile)
716 cpp_reader *pfile;
718 const cpp_token *token;
719 const char *new_file = pfile->map->to_file;
720 unsigned long new_lineno;
721 unsigned int cap, new_sysp = pfile->map->sysp;
722 enum lc_reason reason = LC_RENAME;
724 /* C99 raised the minimum limit on #line numbers. */
725 cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
727 /* Putting this in _cpp_handle_directive risks two calls to
728 _cpp_backup_tokens in some circumstances, which can segfault. */
729 if (pfile->state.line_extension)
730 _cpp_backup_tokens (pfile, 1);
732 /* #line commands expand macros. */
733 token = cpp_get_token (pfile);
734 if (token->type != CPP_NUMBER
735 || strtoul_for_line (token->val.str.text, token->val.str.len,
736 &new_lineno))
738 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
739 cpp_token_as_text (pfile, token));
740 return;
743 if (CPP_PEDANTIC (pfile) && ! pfile->state.line_extension
744 && (new_lineno == 0 || new_lineno > cap))
745 cpp_pedwarn (pfile, "line number out of range");
747 token = cpp_get_token (pfile);
748 if (token->type == CPP_STRING)
750 new_file = (const char *) token->val.str.text;
752 /* Only accept flags for the # 55 form. */
753 if (pfile->state.line_extension)
755 int flag;
757 new_sysp = 0;
758 flag = read_flag (pfile, 0);
759 if (flag == 1)
761 reason = LC_ENTER;
762 /* Fake an include for cpp_included (). */
763 _cpp_fake_include (pfile, new_file);
764 flag = read_flag (pfile, flag);
766 else if (flag == 2)
768 reason = LC_LEAVE;
769 flag = read_flag (pfile, flag);
771 if (flag == 3)
773 new_sysp = 1;
774 flag = read_flag (pfile, flag);
775 if (flag == 4)
776 new_sysp = 2;
779 check_eol (pfile);
781 else if (token->type != CPP_EOF)
783 cpp_error (pfile, "\"%s\" is not a valid filename",
784 cpp_token_as_text (pfile, token));
785 return;
788 skip_rest_of_line (pfile);
789 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
792 /* Arrange the file_change callback. pfile->line has changed to
793 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
794 header, 2 for a sytem header that needs to be extern "C" protected,
795 and zero otherwise. */
796 void
797 _cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
798 cpp_reader *pfile;
799 enum lc_reason reason;
800 const char *to_file;
801 unsigned int file_line;
802 unsigned int sysp;
804 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
805 pfile->line, to_file, file_line);
807 if (pfile->cb.file_change)
808 (*pfile->cb.file_change) (pfile, pfile->map);
812 * Report a warning or error detected by the program we are
813 * processing. Use the directive's tokens in the error message.
816 static void
817 do_diagnostic (pfile, code, print_dir)
818 cpp_reader *pfile;
819 enum error_type code;
820 int print_dir;
822 if (_cpp_begin_message (pfile, code, 0, 0))
824 if (print_dir)
825 fprintf (stderr, "#%s ", pfile->directive->name);
826 pfile->state.prevent_expansion++;
827 cpp_output_line (pfile, stderr);
828 pfile->state.prevent_expansion--;
832 static void
833 do_error (pfile)
834 cpp_reader *pfile;
836 do_diagnostic (pfile, ERROR, 1);
839 static void
840 do_warning (pfile)
841 cpp_reader *pfile;
843 /* We want #warning diagnostics to be emitted in system headers too. */
844 do_diagnostic (pfile, WARNING_SYSHDR, 1);
847 /* Report program identification. */
849 static void
850 do_ident (pfile)
851 cpp_reader *pfile;
853 const cpp_token *str = cpp_get_token (pfile);
855 if (str->type != CPP_STRING)
856 cpp_error (pfile, "invalid #ident directive");
857 else if (pfile->cb.ident)
858 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
860 check_eol (pfile);
863 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
864 matching entry, or NULL if none is found. The returned entry could
865 be the start of a namespace chain, or a pragma. */
866 static struct pragma_entry *
867 lookup_pragma_entry (chain, pragma)
868 struct pragma_entry *chain;
869 const cpp_hashnode *pragma;
871 while (chain && chain->pragma != pragma)
872 chain = chain->next;
874 return chain;
877 /* Create and insert a pragma entry for NAME at the beginning of a
878 singly-linked CHAIN. If handler is NULL, it is a namespace,
879 otherwise it is a pragma and its handler. */
880 static struct pragma_entry *
881 insert_pragma_entry (pfile, chain, pragma, handler)
882 cpp_reader *pfile;
883 struct pragma_entry **chain;
884 const cpp_hashnode *pragma;
885 pragma_cb handler;
887 struct pragma_entry *new;
889 new = (struct pragma_entry *)
890 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
891 new->pragma = pragma;
892 if (handler)
894 new->is_nspace = 0;
895 new->u.handler = handler;
897 else
899 new->is_nspace = 1;
900 new->u.space = NULL;
903 new->next = *chain;
904 *chain = new;
905 return new;
908 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
909 goes in the global namespace. HANDLER is the handler it will call,
910 which must be non-NULL. */
911 void
912 cpp_register_pragma (pfile, space, name, handler)
913 cpp_reader *pfile;
914 const char *space;
915 const char *name;
916 pragma_cb handler;
918 struct pragma_entry **chain = &pfile->pragmas;
919 struct pragma_entry *entry;
920 const cpp_hashnode *node;
922 if (!handler)
923 abort ();
925 if (space)
927 node = cpp_lookup (pfile, U space, strlen (space));
928 entry = lookup_pragma_entry (*chain, node);
929 if (!entry)
930 entry = insert_pragma_entry (pfile, chain, node, NULL);
931 else if (!entry->is_nspace)
932 goto clash;
933 chain = &entry->u.space;
936 /* Check for duplicates. */
937 node = cpp_lookup (pfile, U name, strlen (name));
938 entry = lookup_pragma_entry (*chain, node);
939 if (entry)
941 if (entry->is_nspace)
942 clash:
943 cpp_ice (pfile,
944 "registering \"%s\" as both a pragma and a pragma namespace",
945 NODE_NAME (node));
946 else if (space)
947 cpp_ice (pfile, "#pragma %s %s is already registered", space, name);
948 else
949 cpp_ice (pfile, "#pragma %s is already registered", name);
951 else
952 insert_pragma_entry (pfile, chain, node, handler);
955 /* Register the pragmas the preprocessor itself handles. */
956 void
957 _cpp_init_internal_pragmas (pfile)
958 cpp_reader *pfile;
960 /* Pragmas in the global namespace. */
961 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
962 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
964 /* New GCC-specific pragmas should be put in the GCC namespace. */
965 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
966 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
967 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
970 /* Pragmata handling. We handle some, and pass the rest on to the
971 front end. C99 defines three pragmas and says that no macro
972 expansion is to be performed on them; whether or not macro
973 expansion happens for other pragmas is implementation defined.
974 This implementation never macro-expands the text after #pragma. */
975 static void
976 do_pragma (pfile)
977 cpp_reader *pfile;
979 const struct pragma_entry *p = NULL;
980 const cpp_token *token;
981 unsigned int count = 1;
983 pfile->state.prevent_expansion++;
985 token = cpp_get_token (pfile);
986 if (token->type == CPP_NAME)
988 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
989 if (p && p->is_nspace)
991 count = 2;
992 token = cpp_get_token (pfile);
993 if (token->type == CPP_NAME)
994 p = lookup_pragma_entry (p->u.space, token->val.node);
995 else
996 p = NULL;
1000 /* FIXME. This is an awful kludge to get the front ends to update
1001 their notion of line number for diagnostic purposes. The line
1002 number should be passed to the handler and they should do it
1003 themselves. Stand-alone CPP must ignore us, otherwise it will
1004 prefix the directive with spaces, hence the 1. Ugh. */
1005 if (pfile->cb.line_change)
1006 (*pfile->cb.line_change)(pfile, token, 1);
1008 if (p)
1009 (*p->u.handler) (pfile);
1010 else if (pfile->cb.def_pragma)
1012 _cpp_backup_tokens (pfile, count);
1013 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1016 pfile->state.prevent_expansion--;
1019 static void
1020 do_pragma_once (pfile)
1021 cpp_reader *pfile;
1023 cpp_warning (pfile, "#pragma once is obsolete");
1025 if (pfile->buffer->prev == NULL)
1026 cpp_warning (pfile, "#pragma once in main file");
1027 else
1028 _cpp_never_reread (pfile->buffer->inc);
1030 check_eol (pfile);
1033 static void
1034 do_pragma_poison (pfile)
1035 cpp_reader *pfile;
1037 /* Poison these symbols so that all subsequent usage produces an
1038 error message. */
1039 const cpp_token *tok;
1040 cpp_hashnode *hp;
1042 pfile->state.poisoned_ok = 1;
1043 for (;;)
1045 tok = _cpp_lex_token (pfile);
1046 if (tok->type == CPP_EOF)
1047 break;
1048 if (tok->type != CPP_NAME)
1050 cpp_error (pfile, "invalid #pragma GCC poison directive");
1051 break;
1054 hp = tok->val.node;
1055 if (hp->flags & NODE_POISONED)
1056 continue;
1058 if (hp->type == NT_MACRO)
1059 cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
1060 _cpp_free_definition (hp);
1061 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1063 pfile->state.poisoned_ok = 0;
1066 /* Mark the current header as a system header. This will suppress
1067 some categories of warnings (notably those from -pedantic). It is
1068 intended for use in system libraries that cannot be implemented in
1069 conforming C, but cannot be certain that their headers appear in a
1070 system include directory. To prevent abuse, it is rejected in the
1071 primary source file. */
1072 static void
1073 do_pragma_system_header (pfile)
1074 cpp_reader *pfile;
1076 cpp_buffer *buffer = pfile->buffer;
1078 if (buffer->prev == 0)
1079 cpp_warning (pfile, "#pragma system_header ignored outside include file");
1080 else
1082 check_eol (pfile);
1083 skip_rest_of_line (pfile);
1084 cpp_make_system_header (pfile, 1, 0);
1088 /* Check the modified date of the current include file against a specified
1089 file. Issue a diagnostic, if the specified file is newer. We use this to
1090 determine if a fixed header should be refixed. */
1091 static void
1092 do_pragma_dependency (pfile)
1093 cpp_reader *pfile;
1095 const cpp_token *header;
1096 int ordering;
1098 header = parse_include (pfile);
1099 if (!header)
1100 return;
1102 ordering = _cpp_compare_file_date (pfile, header);
1103 if (ordering < 0)
1104 cpp_warning (pfile, "cannot find source %s",
1105 cpp_token_as_text (pfile, header));
1106 else if (ordering > 0)
1108 cpp_warning (pfile, "current file is older than %s",
1109 cpp_token_as_text (pfile, header));
1110 if (cpp_get_token (pfile)->type != CPP_EOF)
1112 _cpp_backup_tokens (pfile, 1);
1113 do_diagnostic (pfile, WARNING, 0);
1118 /* Get a token but skip padding. */
1119 static const cpp_token *
1120 get_token_no_padding (pfile)
1121 cpp_reader *pfile;
1123 for (;;)
1125 const cpp_token *result = cpp_get_token (pfile);
1126 if (result->type != CPP_PADDING)
1127 return result;
1131 /* Check syntax is "(string-literal)". Returns the string on success,
1132 or NULL on failure. */
1133 static const cpp_token *
1134 get__Pragma_string (pfile)
1135 cpp_reader *pfile;
1137 const cpp_token *string;
1139 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1140 return NULL;
1142 string = get_token_no_padding (pfile);
1143 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1144 return NULL;
1146 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1147 return NULL;
1149 return string;
1152 /* Returns a malloced buffer containing a destringized cpp_string by
1153 removing the first \ of \" and \\ sequences. */
1154 static unsigned char *
1155 destringize (in, len)
1156 const cpp_string *in;
1157 unsigned int *len;
1159 const unsigned char *src, *limit;
1160 unsigned char *dest, *result;
1162 dest = result = (unsigned char *) xmalloc (in->len);
1163 for (src = in->text, limit = src + in->len; src < limit;)
1165 /* We know there is a character following the backslash. */
1166 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1167 src++;
1168 *dest++ = *src++;
1171 *len = dest - result;
1172 return result;
1175 void
1176 _cpp_do__Pragma (pfile)
1177 cpp_reader *pfile;
1179 const cpp_token *string = get__Pragma_string (pfile);
1180 unsigned char *buffer;
1181 unsigned int len;
1183 if (!string)
1184 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1185 else
1187 /* Ideally, we'd like
1188 token1 _Pragma ("foo") token2
1189 to be output as
1190 token1
1191 # 7 "file.c"
1192 #pragma foo
1193 # 7 "file.c"
1194 token2
1195 Getting these correct line markers is a little tricky. */
1197 unsigned int orig_line = pfile->line;
1198 buffer = destringize (&string->val.str, &len);
1199 run_directive (pfile, T_PRAGMA, (char *) buffer, len);
1200 free ((PTR) buffer);
1201 pfile->line = orig_line;
1202 pfile->buffer->saved_flags = BOL;
1206 /* Just ignore #sccs, on systems where we define it at all. */
1207 #ifdef SCCS_DIRECTIVE
1208 static void
1209 do_sccs (pfile)
1210 cpp_reader *pfile ATTRIBUTE_UNUSED;
1213 #endif
1215 static void
1216 do_ifdef (pfile)
1217 cpp_reader *pfile;
1219 int skip = 1;
1221 if (! pfile->state.skipping)
1223 const cpp_hashnode *node = lex_macro_node (pfile);
1225 if (node)
1226 skip = node->type != NT_MACRO;
1228 if (node)
1229 check_eol (pfile);
1232 push_conditional (pfile, skip, T_IFDEF, 0);
1235 static void
1236 do_ifndef (pfile)
1237 cpp_reader *pfile;
1239 int skip = 1;
1240 const cpp_hashnode *node = 0;
1242 if (! pfile->state.skipping)
1244 node = lex_macro_node (pfile);
1245 if (node)
1246 skip = node->type == NT_MACRO;
1248 if (node)
1249 check_eol (pfile);
1252 push_conditional (pfile, skip, T_IFNDEF, node);
1255 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1256 pfile->mi_ind_cmacro so we can handle multiple-include
1257 optimisations. If macro expansion occurs in the expression, we
1258 cannot treat it as a controlling conditional, since the expansion
1259 could change in the future. That is handled by cpp_get_token. */
1261 static void
1262 do_if (pfile)
1263 cpp_reader *pfile;
1265 int skip = 1;
1267 if (! pfile->state.skipping)
1268 skip = _cpp_parse_expr (pfile) == 0;
1270 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1273 /* Flip skipping state if appropriate and continue without changing
1274 if_stack; this is so that the error message for missing #endif's
1275 etc. will point to the original #if. */
1277 static void
1278 do_else (pfile)
1279 cpp_reader *pfile;
1281 cpp_buffer *buffer = pfile->buffer;
1282 struct if_stack *ifs = buffer->if_stack;
1284 if (ifs == NULL)
1285 cpp_error (pfile, "#else without #if");
1286 else
1288 if (ifs->type == T_ELSE)
1290 cpp_error (pfile, "#else after #else");
1291 cpp_error_with_line (pfile, ifs->line, 0,
1292 "the conditional began here");
1294 ifs->type = T_ELSE;
1296 /* Skip any future (erroneous) #elses or #elifs. */
1297 pfile->state.skipping = ifs->skip_elses;
1298 ifs->skip_elses = true;
1300 /* Invalidate any controlling macro. */
1301 ifs->mi_cmacro = 0;
1303 /* Only check EOL if was not originally skipping. */
1304 if (!ifs->was_skipping)
1305 check_eol (pfile);
1309 /* handle a #elif directive by not changing if_stack either. see the
1310 comment above do_else. */
1312 static void
1313 do_elif (pfile)
1314 cpp_reader *pfile;
1316 cpp_buffer *buffer = pfile->buffer;
1317 struct if_stack *ifs = buffer->if_stack;
1319 if (ifs == NULL)
1320 cpp_error (pfile, "#elif without #if");
1321 else
1323 if (ifs->type == T_ELSE)
1325 cpp_error (pfile, "#elif after #else");
1326 cpp_error_with_line (pfile, ifs->line, 0,
1327 "the conditional began here");
1329 ifs->type = T_ELIF;
1331 /* Only evaluate this if we aren't skipping elses. During
1332 evaluation, set skipping to false to get lexer warnings. */
1333 if (ifs->skip_elses)
1334 pfile->state.skipping = 1;
1335 else
1337 pfile->state.skipping = 0;
1338 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1339 ifs->skip_elses = ! pfile->state.skipping;
1342 /* Invalidate any controlling macro. */
1343 ifs->mi_cmacro = 0;
1347 /* #endif pops the if stack and resets pfile->state.skipping. */
1349 static void
1350 do_endif (pfile)
1351 cpp_reader *pfile;
1353 cpp_buffer *buffer = pfile->buffer;
1354 struct if_stack *ifs = buffer->if_stack;
1356 if (ifs == NULL)
1357 cpp_error (pfile, "#endif without #if");
1358 else
1360 /* Only check EOL if was not originally skipping. */
1361 if (!ifs->was_skipping)
1362 check_eol (pfile);
1364 /* If potential control macro, we go back outside again. */
1365 if (ifs->next == 0 && ifs->mi_cmacro)
1367 pfile->mi_valid = true;
1368 pfile->mi_cmacro = ifs->mi_cmacro;
1371 buffer->if_stack = ifs->next;
1372 pfile->state.skipping = ifs->was_skipping;
1373 obstack_free (&pfile->buffer_ob, ifs);
1377 /* Push an if_stack entry and set pfile->state.skipping accordingly.
1378 If this is a #if or #ifndef, CMACRO is a potentially controlling
1379 macro - we need to check here that we are at the top of the file. */
1381 static void
1382 push_conditional (pfile, skip, type, cmacro)
1383 cpp_reader *pfile;
1384 int skip;
1385 int type;
1386 const cpp_hashnode *cmacro;
1388 struct if_stack *ifs;
1389 cpp_buffer *buffer = pfile->buffer;
1391 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1392 ifs->line = pfile->directive_line;
1393 ifs->next = buffer->if_stack;
1394 ifs->skip_elses = pfile->state.skipping || !skip;
1395 ifs->was_skipping = pfile->state.skipping;
1396 ifs->type = type;
1397 /* This condition is effectively a test for top-of-file. */
1398 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1399 ifs->mi_cmacro = cmacro;
1400 else
1401 ifs->mi_cmacro = 0;
1403 pfile->state.skipping = skip;
1404 buffer->if_stack = ifs;
1407 /* Read the tokens of the answer into the macro pool. Only commit the
1408 memory if we intend it as permanent storage, i.e. the #assert case.
1409 Returns 0 on success. */
1411 static int
1412 parse_answer (pfile, answerp, type)
1413 cpp_reader *pfile;
1414 struct answer **answerp;
1415 int type;
1417 const cpp_token *paren;
1418 struct answer *answer;
1419 unsigned int acount;
1421 /* In a conditional, it is legal to not have an open paren. We
1422 should save the following token in this case. */
1423 paren = cpp_get_token (pfile);
1425 /* If not a paren, see if we're OK. */
1426 if (paren->type != CPP_OPEN_PAREN)
1428 /* In a conditional no answer is a test for any answer. It
1429 could be followed by any token. */
1430 if (type == T_IF)
1432 _cpp_backup_tokens (pfile, 1);
1433 return 0;
1436 /* #unassert with no answer is valid - it removes all answers. */
1437 if (type == T_UNASSERT && paren->type == CPP_EOF)
1438 return 0;
1440 cpp_error (pfile, "missing '(' after predicate");
1441 return 1;
1444 for (acount = 0;; acount++)
1446 size_t room_needed;
1447 const cpp_token *token = cpp_get_token (pfile);
1448 cpp_token *dest;
1450 if (token->type == CPP_CLOSE_PAREN)
1451 break;
1453 if (token->type == CPP_EOF)
1455 cpp_error (pfile, "missing ')' to complete answer");
1456 return 1;
1459 /* struct answer includes the space for one token. */
1460 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1462 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1463 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1465 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1466 *dest = *token;
1468 /* Drop whitespace at start, for answer equivalence purposes. */
1469 if (acount == 0)
1470 dest->flags &= ~PREV_WHITE;
1473 if (acount == 0)
1475 cpp_error (pfile, "predicate's answer is empty");
1476 return 1;
1479 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1480 answer->count = acount;
1481 answer->next = NULL;
1482 *answerp = answer;
1484 return 0;
1487 /* Parses an assertion, returning a pointer to the hash node of the
1488 predicate, or 0 on error. If an answer was supplied, it is placed
1489 in ANSWERP, otherwise it is set to 0. */
1490 static cpp_hashnode *
1491 parse_assertion (pfile, answerp, type)
1492 cpp_reader *pfile;
1493 struct answer **answerp;
1494 int type;
1496 cpp_hashnode *result = 0;
1497 const cpp_token *predicate;
1499 /* We don't expand predicates or answers. */
1500 pfile->state.prevent_expansion++;
1502 *answerp = 0;
1503 predicate = cpp_get_token (pfile);
1504 if (predicate->type == CPP_EOF)
1505 cpp_error (pfile, "assertion without predicate");
1506 else if (predicate->type != CPP_NAME)
1507 cpp_error (pfile, "predicate must be an identifier");
1508 else if (parse_answer (pfile, answerp, type) == 0)
1510 unsigned int len = NODE_LEN (predicate->val.node);
1511 unsigned char *sym = alloca (len + 1);
1513 /* Prefix '#' to get it out of macro namespace. */
1514 sym[0] = '#';
1515 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1516 result = cpp_lookup (pfile, sym, len + 1);
1519 pfile->state.prevent_expansion--;
1520 return result;
1523 /* Returns a pointer to the pointer to the answer in the answer chain,
1524 or a pointer to NULL if the answer is not in the chain. */
1525 static struct answer **
1526 find_answer (node, candidate)
1527 cpp_hashnode *node;
1528 const struct answer *candidate;
1530 unsigned int i;
1531 struct answer **result;
1533 for (result = &node->value.answers; *result; result = &(*result)->next)
1535 struct answer *answer = *result;
1537 if (answer->count == candidate->count)
1539 for (i = 0; i < answer->count; i++)
1540 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1541 break;
1543 if (i == answer->count)
1544 break;
1548 return result;
1551 /* Test an assertion within a preprocessor conditional. Returns
1552 non-zero on failure, zero on success. On success, the result of
1553 the test is written into VALUE. */
1555 _cpp_test_assertion (pfile, value)
1556 cpp_reader *pfile;
1557 int *value;
1559 struct answer *answer;
1560 cpp_hashnode *node;
1562 node = parse_assertion (pfile, &answer, T_IF);
1563 if (node)
1564 *value = (node->type == NT_ASSERTION &&
1565 (answer == 0 || *find_answer (node, answer) != 0));
1567 /* We don't commit the memory for the answer - it's temporary only. */
1568 return node == 0;
1571 static void
1572 do_assert (pfile)
1573 cpp_reader *pfile;
1575 struct answer *new_answer;
1576 cpp_hashnode *node;
1578 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1579 if (node)
1581 /* Place the new answer in the answer list. First check there
1582 is not a duplicate. */
1583 new_answer->next = 0;
1584 if (node->type == NT_ASSERTION)
1586 if (*find_answer (node, new_answer))
1588 cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
1589 return;
1591 new_answer->next = node->value.answers;
1594 node->type = NT_ASSERTION;
1595 node->value.answers = new_answer;
1596 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1597 + (new_answer->count - 1)
1598 * sizeof (cpp_token));
1599 check_eol (pfile);
1603 static void
1604 do_unassert (pfile)
1605 cpp_reader *pfile;
1607 cpp_hashnode *node;
1608 struct answer *answer;
1610 node = parse_assertion (pfile, &answer, T_UNASSERT);
1611 /* It isn't an error to #unassert something that isn't asserted. */
1612 if (node && node->type == NT_ASSERTION)
1614 if (answer)
1616 struct answer **p = find_answer (node, answer), *temp;
1618 /* Remove the answer from the list. */
1619 temp = *p;
1620 if (temp)
1621 *p = temp->next;
1623 /* Did we free the last answer? */
1624 if (node->value.answers == 0)
1625 node->type = NT_VOID;
1627 check_eol (pfile);
1629 else
1630 _cpp_free_definition (node);
1633 /* We don't commit the memory for the answer - it's temporary only. */
1636 /* These are for -D, -U, -A. */
1638 /* Process the string STR as if it appeared as the body of a #define.
1639 If STR is just an identifier, define it with value 1.
1640 If STR has anything after the identifier, then it should
1641 be identifier=definition. */
1643 void
1644 cpp_define (pfile, str)
1645 cpp_reader *pfile;
1646 const char *str;
1648 char *buf, *p;
1649 size_t count;
1651 /* Copy the entire option so we can modify it.
1652 Change the first "=" in the string to a space. If there is none,
1653 tack " 1" on the end. */
1655 /* Length including the null. */
1656 count = strlen (str);
1657 buf = (char *) alloca (count + 2);
1658 memcpy (buf, str, count);
1660 p = strchr (str, '=');
1661 if (p)
1662 buf[p - str] = ' ';
1663 else
1665 buf[count++] = ' ';
1666 buf[count++] = '1';
1669 run_directive (pfile, T_DEFINE, buf, count);
1672 /* Slight variant of the above for use by initialize_builtins. */
1673 void
1674 _cpp_define_builtin (pfile, str)
1675 cpp_reader *pfile;
1676 const char *str;
1678 run_directive (pfile, T_DEFINE, str, strlen (str));
1681 /* Process MACRO as if it appeared as the body of an #undef. */
1682 void
1683 cpp_undef (pfile, macro)
1684 cpp_reader *pfile;
1685 const char *macro;
1687 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1690 /* Process the string STR as if it appeared as the body of a #assert. */
1691 void
1692 cpp_assert (pfile, str)
1693 cpp_reader *pfile;
1694 const char *str;
1696 handle_assertion (pfile, str, T_ASSERT);
1699 /* Process STR as if it appeared as the body of an #unassert. */
1700 void
1701 cpp_unassert (pfile, str)
1702 cpp_reader *pfile;
1703 const char *str;
1705 handle_assertion (pfile, str, T_UNASSERT);
1708 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1709 static void
1710 handle_assertion (pfile, str, type)
1711 cpp_reader *pfile;
1712 const char *str;
1713 int type;
1715 size_t count = strlen (str);
1716 const char *p = strchr (str, '=');
1718 if (p)
1720 /* Copy the entire option so we can modify it. Change the first
1721 "=" in the string to a '(', and tack a ')' on the end. */
1722 char *buf = (char *) alloca (count + 1);
1724 memcpy (buf, str, count);
1725 buf[p - str] = '(';
1726 buf[count++] = ')';
1727 str = buf;
1730 run_directive (pfile, type, str, count);
1733 /* The number of errors for a given reader. */
1734 unsigned int
1735 cpp_errors (pfile)
1736 cpp_reader *pfile;
1738 return pfile->errors;
1741 /* The options structure. */
1742 cpp_options *
1743 cpp_get_options (pfile)
1744 cpp_reader *pfile;
1746 return &pfile->opts;
1749 /* The callbacks structure. */
1750 cpp_callbacks *
1751 cpp_get_callbacks (pfile)
1752 cpp_reader *pfile;
1754 return &pfile->cb;
1757 /* The line map set. */
1758 const struct line_maps *
1759 cpp_get_line_maps (pfile)
1760 cpp_reader *pfile;
1762 return &pfile->line_maps;
1765 /* Copy the given callbacks structure to our own. */
1766 void
1767 cpp_set_callbacks (pfile, cb)
1768 cpp_reader *pfile;
1769 cpp_callbacks *cb;
1771 pfile->cb = *cb;
1774 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1775 doesn't fail. It does not generate a file change call back; that
1776 is the responsibility of the caller. */
1777 cpp_buffer *
1778 cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
1779 cpp_reader *pfile;
1780 const U_CHAR *buffer;
1781 size_t len;
1782 int from_stage3;
1783 int return_at_eof;
1785 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1787 /* Clears, amongst other things, if_stack and mi_cmacro. */
1788 memset (new, 0, sizeof (cpp_buffer));
1790 new->line_base = new->buf = new->cur = buffer;
1791 new->rlimit = buffer + len;
1793 /* No read ahead or extra char initially. */
1794 new->read_ahead = EOF;
1795 new->extra_char = EOF;
1796 new->from_stage3 = from_stage3;
1797 new->prev = pfile->buffer;
1798 new->return_at_eof = return_at_eof;
1799 new->saved_flags = BOL;
1801 pfile->buffer = new;
1803 return new;
1806 /* If called from do_line, pops a single buffer. Otherwise pops all
1807 buffers until a real file is reached. Generates appropriate
1808 call-backs. */
1809 void
1810 _cpp_pop_buffer (pfile)
1811 cpp_reader *pfile;
1813 cpp_buffer *buffer = pfile->buffer;
1814 struct if_stack *ifs;
1816 /* Walk back up the conditional stack till we reach its level at
1817 entry to this file, issuing error messages. */
1818 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1819 cpp_error_with_line (pfile, ifs->line, 0,
1820 "unterminated #%s", dtable[ifs->type].name);
1822 /* In case of a missing #endif. */
1823 pfile->state.skipping = 0;
1825 /* Update the reader's buffer before _cpp_do_file_change. */
1826 pfile->buffer = buffer->prev;
1828 if (buffer->inc)
1829 _cpp_pop_file_buffer (pfile, buffer->inc);
1831 obstack_free (&pfile->buffer_ob, buffer);
1834 void
1835 _cpp_init_directives (pfile)
1836 cpp_reader *pfile;
1838 unsigned int i;
1839 cpp_hashnode *node;
1841 /* Register the directives. */
1842 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1844 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1845 node->directive_index = i + 1;