Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / contrib / gcc-3.4 / gcc / cpplib.c
blob298f5e6618b5c6c094c4d88428a5369e7c6f9f86
1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 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"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "obstack.h"
28 /* Chained list of answers to an assertion. */
29 struct answer
31 struct answer *next;
32 unsigned int count;
33 cpp_token first[1];
36 /* Stack of conditionals currently in progress
37 (including both successful and failing conditionals). */
38 struct if_stack
40 struct if_stack *next;
41 unsigned int line; /* Line where condition started. */
42 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
43 bool skip_elses; /* Can future #else / #elif be skipped? */
44 bool was_skipping; /* If were skipping on entry. */
45 int type; /* Most recent conditional for diagnostics. */
48 /* Contains a registered pragma or pragma namespace. */
49 typedef void (*pragma_cb) (cpp_reader *);
50 struct pragma_entry
52 struct pragma_entry *next;
53 const cpp_hashnode *pragma; /* Name and length. */
54 int is_nspace;
55 union {
56 pragma_cb handler;
57 struct pragma_entry *space;
58 } u;
61 /* Values for the origin field of struct directive. KANDR directives
62 come from traditional (K&R) C. STDC89 directives come from the
63 1989 C standard. EXTENSION directives are extensions. */
64 #define KANDR 0
65 #define STDC89 1
66 #define EXTENSION 2
68 /* Values for the flags field of struct directive. COND indicates a
69 conditional; IF_COND an opening conditional. INCL means to treat
70 "..." and <...> as q-char and h-char sequences respectively. IN_I
71 means this directive should be handled even if -fpreprocessed is in
72 effect (these are the directives with callback hooks).
74 EXPAND is set on directives that are always macro-expanded. */
75 #define COND (1 << 0)
76 #define IF_COND (1 << 1)
77 #define INCL (1 << 2)
78 #define IN_I (1 << 3)
79 #define EXPAND (1 << 4)
81 /* Defines one #-directive, including how to handle it. */
82 typedef void (*directive_handler) (cpp_reader *);
83 typedef struct directive directive;
84 struct directive
86 directive_handler handler; /* Function to handle directive. */
87 const uchar *name; /* Name of directive. */
88 unsigned short length; /* Length of name. */
89 unsigned char origin; /* Origin of directive. */
90 unsigned char flags; /* Flags describing this directive. */
93 /* Forward declarations. */
95 static void skip_rest_of_line (cpp_reader *);
96 static void check_eol (cpp_reader *);
97 static void start_directive (cpp_reader *);
98 static void prepare_directive_trad (cpp_reader *);
99 static void end_directive (cpp_reader *, int);
100 static void directive_diagnostics (cpp_reader *, const directive *, int);
101 static void run_directive (cpp_reader *, int, const char *, size_t);
102 static char *glue_header_name (cpp_reader *);
103 static const char *parse_include (cpp_reader *, int *);
104 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
105 static unsigned int read_flag (cpp_reader *, unsigned int);
106 static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
107 static void do_diagnostic (cpp_reader *, int, int);
108 static cpp_hashnode *lex_macro_node (cpp_reader *);
109 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
110 static void do_include_common (cpp_reader *, enum include_type);
111 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
112 const cpp_hashnode *);
113 static struct pragma_entry *insert_pragma_entry (cpp_reader *,
114 struct pragma_entry **,
115 const cpp_hashnode *,
116 pragma_cb);
117 static int count_registered_pragmas (struct pragma_entry *);
118 static char ** save_registered_pragmas (struct pragma_entry *, char **);
119 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
120 char **);
121 static void do_pragma_once (cpp_reader *);
122 static void do_pragma_poison (cpp_reader *);
123 static void do_pragma_system_header (cpp_reader *);
124 static void do_pragma_dependency (cpp_reader *);
125 static void do_linemarker (cpp_reader *);
126 static const cpp_token *get_token_no_padding (cpp_reader *);
127 static const cpp_token *get__Pragma_string (cpp_reader *);
128 static void destringize_and_run (cpp_reader *, const cpp_string *);
129 static int parse_answer (cpp_reader *, struct answer **, int);
130 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
131 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
132 static void handle_assertion (cpp_reader *, const char *, int);
134 /* This is the table of directive handlers. It is ordered by
135 frequency of occurrence; the numbers at the end are directive
136 counts from all the source code I have lying around (egcs and libc
137 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
138 pcmcia-cs-3.0.9). This is no longer important as directive lookup
139 is now O(1). All extensions other than #warning and #include_next
140 are deprecated. The name is where the extension appears to have
141 come from. */
143 #define DIRECTIVE_TABLE \
144 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
145 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
146 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
147 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
148 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
149 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
150 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
151 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
152 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
153 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
154 D(error, T_ERROR, STDC89, 0) /* 475 */ \
155 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
156 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
157 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
158 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
159 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
160 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
161 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
162 D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
164 /* Use the table to generate a series of prototypes, an enum for the
165 directive names, and an array of directive handlers. */
167 #define D(name, t, o, f) static void do_##name (cpp_reader *);
168 DIRECTIVE_TABLE
169 #undef D
171 #define D(n, tag, o, f) tag,
172 enum
174 DIRECTIVE_TABLE
175 N_DIRECTIVES
177 #undef D
179 #define D(name, t, origin, flags) \
180 { do_##name, (const uchar *) #name, \
181 sizeof #name - 1, origin, flags },
182 static const directive dtable[] =
184 DIRECTIVE_TABLE
186 #undef D
187 #undef DIRECTIVE_TABLE
189 /* Wrapper struct directive for linemarkers.
190 The origin is more or less true - the original K+R cpp
191 did use this notation in its preprocessed output. */
192 static const directive linemarker_dir =
194 do_linemarker, U"#", 1, KANDR, IN_I
197 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
199 /* Skip any remaining tokens in a directive. */
200 static void
201 skip_rest_of_line (cpp_reader *pfile)
203 /* Discard all stacked contexts. */
204 while (pfile->context->prev)
205 _cpp_pop_context (pfile);
207 /* Sweep up all tokens remaining on the line. */
208 if (! SEEN_EOL ())
209 while (_cpp_lex_token (pfile)->type != CPP_EOF)
213 /* Ensure there are no stray tokens at the end of a directive. */
214 static void
215 check_eol (cpp_reader *pfile)
217 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
218 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
219 pfile->directive->name);
222 /* Called when entering a directive, _Pragma or command-line directive. */
223 static void
224 start_directive (cpp_reader *pfile)
226 /* Setup in-directive state. */
227 pfile->state.in_directive = 1;
228 pfile->state.save_comments = 0;
230 /* Some handlers need the position of the # for diagnostics. */
231 pfile->directive_line = pfile->line;
234 /* Called when leaving a directive, _Pragma or command-line directive. */
235 static void
236 end_directive (cpp_reader *pfile, int skip_line)
238 if (CPP_OPTION (pfile, traditional))
240 /* Revert change of prepare_directive_trad. */
241 pfile->state.prevent_expansion--;
243 if (pfile->directive != &dtable[T_DEFINE])
244 _cpp_remove_overlay (pfile);
246 /* We don't skip for an assembler #. */
247 else 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.in_expression = 0;
261 pfile->state.angled_headers = 0;
262 pfile->directive = 0;
265 /* Prepare to handle the directive in pfile->directive. */
266 static void
267 prepare_directive_trad (cpp_reader *pfile)
269 if (pfile->directive != &dtable[T_DEFINE])
271 bool no_expand = (pfile->directive
272 && ! (pfile->directive->flags & EXPAND));
273 bool was_skipping = pfile->state.skipping;
275 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
276 || pfile->directive == &dtable[T_ELIF]);
277 if (pfile->state.in_expression)
278 pfile->state.skipping = false;
280 if (no_expand)
281 pfile->state.prevent_expansion++;
282 _cpp_scan_out_logical_line (pfile, NULL);
283 if (no_expand)
284 pfile->state.prevent_expansion--;
286 pfile->state.skipping = was_skipping;
287 _cpp_overlay_buffer (pfile, pfile->out.base,
288 pfile->out.cur - pfile->out.base);
291 /* Stop ISO C from expanding anything. */
292 pfile->state.prevent_expansion++;
295 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
296 the '#' was indented. */
297 static void
298 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
300 /* Issue -pedantic warnings for extensions. */
301 if (CPP_PEDANTIC (pfile)
302 && ! pfile->state.skipping
303 && dir->origin == EXTENSION)
304 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
306 /* Traditionally, a directive is ignored unless its # is in
307 column 1. Therefore in code intended to work with K+R
308 compilers, directives added by C89 must have their #
309 indented, and directives present in traditional C must not.
310 This is true even of directives in skipped conditional
311 blocks. #elif cannot be used at all. */
312 if (CPP_WTRADITIONAL (pfile))
314 if (dir == &dtable[T_ELIF])
315 cpp_error (pfile, CPP_DL_WARNING,
316 "suggest not using #elif in traditional C");
317 else if (indented && dir->origin == KANDR)
318 cpp_error (pfile, CPP_DL_WARNING,
319 "traditional C ignores #%s with the # indented",
320 dir->name);
321 else if (!indented && dir->origin != KANDR)
322 cpp_error (pfile, CPP_DL_WARNING,
323 "suggest hiding #%s from traditional C with an indented #",
324 dir->name);
328 /* Check if we have a known directive. INDENTED is nonzero if the
329 '#' of the directive was indented. This function is in this file
330 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
331 nonzero if the line of tokens has been handled, zero if we should
332 continue processing the line. */
334 _cpp_handle_directive (cpp_reader *pfile, int indented)
336 const directive *dir = 0;
337 const cpp_token *dname;
338 bool was_parsing_args = pfile->state.parsing_args;
339 int skip = 1;
341 if (was_parsing_args)
343 if (CPP_OPTION (pfile, pedantic))
344 cpp_error (pfile, CPP_DL_PEDWARN,
345 "embedding a directive within macro arguments is not portable");
346 pfile->state.parsing_args = 0;
347 pfile->state.prevent_expansion = 0;
349 start_directive (pfile);
350 dname = _cpp_lex_token (pfile);
352 if (dname->type == CPP_NAME)
354 if (dname->val.node->is_directive)
355 dir = &dtable[dname->val.node->directive_index];
357 /* We do not recognize the # followed by a number extension in
358 assembler code. */
359 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
361 dir = &linemarker_dir;
362 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
363 && ! pfile->state.skipping)
364 cpp_error (pfile, CPP_DL_PEDWARN,
365 "style of line directive is a GCC extension");
368 if (dir)
370 /* If we have a directive that is not an opening conditional,
371 invalidate any control macro. */
372 if (! (dir->flags & IF_COND))
373 pfile->mi_valid = false;
375 /* Kluge alert. In order to be sure that code like this
377 #define HASH #
378 HASH define foo bar
380 does not cause '#define foo bar' to get executed when
381 compiled with -save-temps, we recognize directives in
382 -fpreprocessed mode only if the # is in column 1. cppmacro.c
383 puts a space in front of any '#' at the start of a macro. */
384 if (CPP_OPTION (pfile, preprocessed)
385 && (indented || !(dir->flags & IN_I)))
387 skip = 0;
388 dir = 0;
390 else
392 /* In failed conditional groups, all non-conditional
393 directives are ignored. Before doing that, whether
394 skipping or not, we should lex angle-bracketed headers
395 correctly, and maybe output some diagnostics. */
396 pfile->state.angled_headers = dir->flags & INCL;
397 pfile->state.directive_wants_padding = dir->flags & INCL;
398 if (! CPP_OPTION (pfile, preprocessed))
399 directive_diagnostics (pfile, dir, indented);
400 if (pfile->state.skipping && !(dir->flags & COND))
401 dir = 0;
404 else if (dname->type == CPP_EOF)
405 ; /* CPP_EOF is the "null directive". */
406 else
408 /* An unknown directive. Don't complain about it in assembly
409 source: we don't know where the comments are, and # may
410 introduce assembler pseudo-ops. Don't complain about invalid
411 directives in skipped conditional groups (6.10 p4). */
412 if (CPP_OPTION (pfile, lang) == CLK_ASM)
413 skip = 0;
414 else if (!pfile->state.skipping)
415 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
416 cpp_token_as_text (pfile, dname));
419 pfile->directive = dir;
420 if (CPP_OPTION (pfile, traditional))
421 prepare_directive_trad (pfile);
423 if (dir)
424 pfile->directive->handler (pfile);
425 else if (skip == 0)
426 _cpp_backup_tokens (pfile, 1);
428 end_directive (pfile, skip);
429 if (was_parsing_args)
431 /* Restore state when within macro args. */
432 pfile->state.parsing_args = 2;
433 pfile->state.prevent_expansion = 1;
435 return skip;
438 /* Directive handler wrapper used by the command line option
439 processor. BUF is \n terminated. */
440 static void
441 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
443 cpp_push_buffer (pfile, (const uchar *) buf, count,
444 /* from_stage3 */ true);
445 /* Disgusting hack. */
446 if (dir_no == T_PRAGMA)
447 pfile->buffer->file = pfile->buffer->prev->file;
448 start_directive (pfile);
450 /* This is a short-term fix to prevent a leading '#' being
451 interpreted as a directive. */
452 _cpp_clean_line (pfile);
454 pfile->directive = &dtable[dir_no];
455 if (CPP_OPTION (pfile, traditional))
456 prepare_directive_trad (pfile);
457 pfile->directive->handler (pfile);
458 end_directive (pfile, 1);
459 if (dir_no == T_PRAGMA)
460 pfile->buffer->file = NULL;
461 _cpp_pop_buffer (pfile);
464 /* Checks for validity the macro name in #define, #undef, #ifdef and
465 #ifndef directives. */
466 static cpp_hashnode *
467 lex_macro_node (cpp_reader *pfile)
469 const cpp_token *token = _cpp_lex_token (pfile);
471 /* The token immediately after #define must be an identifier. That
472 identifier may not be "defined", per C99 6.10.8p4.
473 In C++, it may not be any of the "named operators" either,
474 per C++98 [lex.digraph], [lex.key].
475 Finally, the identifier may not have been poisoned. (In that case
476 the lexer has issued the error message for us.) */
478 if (token->type == CPP_NAME)
480 cpp_hashnode *node = token->val.node;
482 if (node == pfile->spec_nodes.n_defined)
483 cpp_error (pfile, CPP_DL_ERROR,
484 "\"defined\" cannot be used as a macro name");
485 else if (! (node->flags & NODE_POISONED))
486 return node;
488 else if (token->flags & NAMED_OP)
489 cpp_error (pfile, CPP_DL_ERROR,
490 "\"%s\" cannot be used as a macro name as it is an operator in C++",
491 NODE_NAME (token->val.node));
492 else if (token->type == CPP_EOF)
493 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
494 pfile->directive->name);
495 else
496 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
498 return NULL;
501 /* Process a #define directive. Most work is done in cppmacro.c. */
502 static void
503 do_define (cpp_reader *pfile)
505 cpp_hashnode *node = lex_macro_node (pfile);
507 if (node)
509 /* If we have been requested to expand comments into macros,
510 then re-enable saving of comments. */
511 pfile->state.save_comments =
512 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
514 if (_cpp_create_definition (pfile, node))
515 if (pfile->cb.define)
516 pfile->cb.define (pfile, pfile->directive_line, node);
520 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
521 static void
522 do_undef (cpp_reader *pfile)
524 cpp_hashnode *node = lex_macro_node (pfile);
526 if (node)
528 if (pfile->cb.undef)
529 pfile->cb.undef (pfile, pfile->directive_line, node);
531 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
532 identifier is not currently defined as a macro name. */
533 if (node->type == NT_MACRO)
535 if (node->flags & NODE_WARN)
536 cpp_error (pfile, CPP_DL_WARNING,
537 "undefining \"%s\"", NODE_NAME (node));
539 if (CPP_OPTION (pfile, warn_unused_macros))
540 _cpp_warn_if_unused_macro (pfile, node, NULL);
542 _cpp_free_definition (node);
546 check_eol (pfile);
549 /* Undefine a single macro/assertion/whatever. */
551 static int
552 undefine_macros (cpp_reader *pfile, cpp_hashnode *h,
553 void *data_p ATTRIBUTE_UNUSED)
555 switch (h->type)
557 case NT_VOID:
558 break;
560 case NT_MACRO:
561 if (pfile->cb.undef)
562 (*pfile->cb.undef) (pfile, pfile->directive_line, h);
564 if (CPP_OPTION (pfile, warn_unused_macros))
565 _cpp_warn_if_unused_macro (pfile, h, NULL);
567 /* And fall through.... */
568 case NT_ASSERTION:
569 _cpp_free_definition (h);
570 break;
572 default:
573 abort ();
575 h->flags &= ~NODE_POISONED;
576 return 1;
579 /* Undefine all macros and assertions. */
581 void
582 cpp_undef_all (cpp_reader *pfile)
584 cpp_forall_identifiers (pfile, undefine_macros, NULL);
588 /* Helper routine used by parse_include. Reinterpret the current line
589 as an h-char-sequence (< ... >); we are looking at the first token
590 after the <. Returns a malloced filename. */
591 static char *
592 glue_header_name (cpp_reader *pfile)
594 const cpp_token *token;
595 char *buffer;
596 size_t len, total_len = 0, capacity = 1024;
598 /* To avoid lexed tokens overwriting our glued name, we can only
599 allocate from the string pool once we've lexed everything. */
600 buffer = xmalloc (capacity);
601 for (;;)
603 token = get_token_no_padding (pfile);
605 if (token->type == CPP_GREATER)
606 break;
607 if (token->type == CPP_EOF)
609 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
610 break;
613 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
614 if (total_len + len > capacity)
616 capacity = (capacity + len) * 2;
617 buffer = xrealloc (buffer, capacity);
620 if (token->flags & PREV_WHITE)
621 buffer[total_len++] = ' ';
623 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
624 - (uchar *) buffer);
627 buffer[total_len] = '\0';
628 return buffer;
631 /* Returns the file name of #include, #include_next, #import and
632 #pragma dependency. The string is malloced and the caller should
633 free it. Returns NULL on error. */
634 static const char *
635 parse_include (cpp_reader *pfile, int *pangle_brackets)
637 char *fname;
638 const cpp_token *header;
640 /* Allow macro expansion. */
641 header = get_token_no_padding (pfile);
642 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
644 fname = xmalloc (header->val.str.len - 1);
645 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
646 fname[header->val.str.len - 2] = '\0';
647 *pangle_brackets = header->type == CPP_HEADER_NAME;
649 else if (header->type == CPP_LESS)
651 fname = glue_header_name (pfile);
652 *pangle_brackets = 1;
654 else
656 const unsigned char *dir;
658 if (pfile->directive == &dtable[T_PRAGMA])
659 dir = U"pragma dependency";
660 else
661 dir = pfile->directive->name;
662 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
663 dir);
665 return NULL;
668 check_eol (pfile);
669 return fname;
672 /* Handle #include, #include_next and #import. */
673 static void
674 do_include_common (cpp_reader *pfile, enum include_type type)
676 const char *fname;
677 int angle_brackets;
679 fname = parse_include (pfile, &angle_brackets);
680 if (!fname)
681 return;
683 if (!*fname)
685 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
686 pfile->directive->name);
687 free ((void *) fname);
688 return;
691 /* Prevent #include recursion. */
692 if (pfile->line_maps.depth >= CPP_STACK_MAX)
693 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
694 else
696 /* Get out of macro context, if we are. */
697 skip_rest_of_line (pfile);
699 if (pfile->cb.include)
700 pfile->cb.include (pfile, pfile->directive_line,
701 pfile->directive->name, fname, angle_brackets);
703 _cpp_stack_include (pfile, fname, angle_brackets, type);
706 free ((void *) fname);
709 static void
710 do_include (cpp_reader *pfile)
712 do_include_common (pfile, IT_INCLUDE);
715 static void
716 do_import (cpp_reader *pfile)
718 do_include_common (pfile, IT_IMPORT);
721 static void
722 do_include_next (cpp_reader *pfile)
724 enum include_type type = IT_INCLUDE_NEXT;
726 /* If this is the primary source file, warn and use the normal
727 search logic. */
728 if (! pfile->buffer->prev)
730 cpp_error (pfile, CPP_DL_WARNING,
731 "#include_next in primary source file");
732 type = IT_INCLUDE;
734 do_include_common (pfile, type);
737 /* Subroutine of do_linemarker. Read possible flags after file name.
738 LAST is the last flag seen; 0 if this is the first flag. Return the
739 flag if it is valid, 0 at the end of the directive. Otherwise
740 complain. */
741 static unsigned int
742 read_flag (cpp_reader *pfile, unsigned int last)
744 const cpp_token *token = _cpp_lex_token (pfile);
746 if (token->type == CPP_NUMBER && token->val.str.len == 1)
748 unsigned int flag = token->val.str.text[0] - '0';
750 if (flag > last && flag <= 4
751 && (flag != 4 || last == 3)
752 && (flag != 2 || last == 0))
753 return flag;
756 if (token->type != CPP_EOF)
757 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
758 cpp_token_as_text (pfile, token));
759 return 0;
762 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
763 of length LEN, to binary; store it in NUMP, and return 0 if the
764 number was well-formed, 1 if not. Temporary, hopefully. */
765 static int
766 strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
768 unsigned long reg = 0;
769 uchar c;
770 while (len--)
772 c = *str++;
773 if (!ISDIGIT (c))
774 return 1;
775 reg *= 10;
776 reg += c - '0';
778 *nump = reg;
779 return 0;
782 /* Interpret #line command.
783 Note that the filename string (if any) is a true string constant
784 (escapes are interpreted), unlike in #line. */
785 static void
786 do_line (cpp_reader *pfile)
788 const cpp_token *token;
789 const char *new_file = pfile->map->to_file;
790 unsigned long new_lineno;
792 /* C99 raised the minimum limit on #line numbers. */
793 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
795 /* #line commands expand macros. */
796 token = cpp_get_token (pfile);
797 if (token->type != CPP_NUMBER
798 || strtoul_for_line (token->val.str.text, token->val.str.len,
799 &new_lineno))
801 cpp_error (pfile, CPP_DL_ERROR,
802 "\"%s\" after #line is not a positive integer",
803 cpp_token_as_text (pfile, token));
804 return;
807 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
808 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
810 token = cpp_get_token (pfile);
811 if (token->type == CPP_STRING)
813 cpp_string s = { 0, 0 };
814 if (_cpp_interpret_string_notranslate (pfile, &token->val.str, &s))
815 new_file = (const char *)s.text;
816 check_eol (pfile);
818 else if (token->type != CPP_EOF)
820 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
821 cpp_token_as_text (pfile, token));
822 return;
825 skip_rest_of_line (pfile);
826 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
827 pfile->map->sysp);
830 /* Interpret the # 44 "file" [flags] notation, which has slightly
831 different syntax and semantics from #line: Flags are allowed,
832 and we never complain about the line number being too big. */
833 static void
834 do_linemarker (cpp_reader *pfile)
836 const cpp_token *token;
837 const char *new_file = pfile->map->to_file;
838 unsigned long new_lineno;
839 unsigned int new_sysp = pfile->map->sysp;
840 enum lc_reason reason = LC_RENAME;
841 int flag;
843 /* Back up so we can get the number again. Putting this in
844 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
845 some circumstances, which can segfault. */
846 _cpp_backup_tokens (pfile, 1);
848 /* #line commands expand macros. */
849 token = cpp_get_token (pfile);
850 if (token->type != CPP_NUMBER
851 || strtoul_for_line (token->val.str.text, token->val.str.len,
852 &new_lineno))
854 cpp_error (pfile, CPP_DL_ERROR,
855 "\"%s\" after # is not a positive integer",
856 cpp_token_as_text (pfile, token));
857 return;
860 token = cpp_get_token (pfile);
861 if (token->type == CPP_STRING)
863 cpp_string s = { 0, 0 };
864 if (_cpp_interpret_string_notranslate (pfile, &token->val.str, &s))
865 new_file = (const char *)s.text;
867 new_sysp = 0;
868 flag = read_flag (pfile, 0);
869 if (flag == 1)
871 reason = LC_ENTER;
872 /* Fake an include for cpp_included (). */
873 _cpp_fake_include (pfile, new_file);
874 flag = read_flag (pfile, flag);
876 else if (flag == 2)
878 reason = LC_LEAVE;
879 flag = read_flag (pfile, flag);
881 if (flag == 3)
883 new_sysp = 1;
884 flag = read_flag (pfile, flag);
885 if (flag == 4)
886 new_sysp = 2;
889 check_eol (pfile);
891 else if (token->type != CPP_EOF)
893 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
894 cpp_token_as_text (pfile, token));
895 return;
898 skip_rest_of_line (pfile);
899 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
902 /* Arrange the file_change callback. pfile->line has changed to
903 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
904 header, 2 for a system header that needs to be extern "C" protected,
905 and zero otherwise. */
906 void
907 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
908 const char *to_file, unsigned int file_line,
909 unsigned int sysp)
911 pfile->map = linemap_add (&pfile->line_maps, reason, sysp,
912 pfile->line, to_file, file_line);
914 if (pfile->cb.file_change)
915 pfile->cb.file_change (pfile, pfile->map);
918 /* Report a warning or error detected by the program we are
919 processing. Use the directive's tokens in the error message. */
920 static void
921 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
923 if (_cpp_begin_message (pfile, code,
924 pfile->cur_token[-1].line,
925 pfile->cur_token[-1].col))
927 if (print_dir)
928 fprintf (stderr, "#%s ", pfile->directive->name);
929 pfile->state.prevent_expansion++;
930 cpp_output_line (pfile, stderr);
931 pfile->state.prevent_expansion--;
935 static void
936 do_error (cpp_reader *pfile)
938 do_diagnostic (pfile, CPP_DL_ERROR, 1);
941 static void
942 do_warning (cpp_reader *pfile)
944 /* We want #warning diagnostics to be emitted in system headers too. */
945 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
948 /* Report program identification. */
949 static void
950 do_ident (cpp_reader *pfile)
952 const cpp_token *str = cpp_get_token (pfile);
954 if (str->type != CPP_STRING)
955 cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive");
956 else if (pfile->cb.ident)
957 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
959 check_eol (pfile);
962 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
963 matching entry, or NULL if none is found. The returned entry could
964 be the start of a namespace chain, or a pragma. */
965 static struct pragma_entry *
966 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
968 while (chain && chain->pragma != pragma)
969 chain = chain->next;
971 return chain;
974 /* Create and insert a pragma entry for NAME at the beginning of a
975 singly-linked CHAIN. If handler is NULL, it is a namespace,
976 otherwise it is a pragma and its handler. */
977 static struct pragma_entry *
978 insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
979 const cpp_hashnode *pragma, pragma_cb handler)
981 struct pragma_entry *new;
983 new = (struct pragma_entry *)
984 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
985 new->pragma = pragma;
986 if (handler)
988 new->is_nspace = 0;
989 new->u.handler = handler;
991 else
993 new->is_nspace = 1;
994 new->u.space = NULL;
997 new->next = *chain;
998 *chain = new;
999 return new;
1002 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1003 goes in the global namespace. HANDLER is the handler it will call,
1004 which must be non-NULL. */
1005 void
1006 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1007 pragma_cb handler)
1009 struct pragma_entry **chain = &pfile->pragmas;
1010 struct pragma_entry *entry;
1011 const cpp_hashnode *node;
1013 if (!handler)
1014 abort ();
1016 if (space)
1018 node = cpp_lookup (pfile, U space, strlen (space));
1019 entry = lookup_pragma_entry (*chain, node);
1020 if (!entry)
1021 entry = insert_pragma_entry (pfile, chain, node, NULL);
1022 else if (!entry->is_nspace)
1023 goto clash;
1024 chain = &entry->u.space;
1027 /* Check for duplicates. */
1028 node = cpp_lookup (pfile, U name, strlen (name));
1029 entry = lookup_pragma_entry (*chain, node);
1030 if (entry)
1032 if (entry->is_nspace)
1033 clash:
1034 cpp_error (pfile, CPP_DL_ICE,
1035 "registering \"%s\" as both a pragma and a pragma namespace",
1036 NODE_NAME (node));
1037 else if (space)
1038 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1039 space, name);
1040 else
1041 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1043 else
1044 insert_pragma_entry (pfile, chain, node, handler);
1047 /* Register the pragmas the preprocessor itself handles. */
1048 void
1049 _cpp_init_internal_pragmas (cpp_reader *pfile)
1051 /* Pragmas in the global namespace. */
1052 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1054 /* New GCC-specific pragmas should be put in the GCC namespace. */
1055 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1056 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1057 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
1060 /* Return the number of registered pragmas in PE. */
1062 static int
1063 count_registered_pragmas (struct pragma_entry *pe)
1065 int ct = 0;
1066 for (; pe != NULL; pe = pe->next)
1068 if (pe->is_nspace)
1069 ct += count_registered_pragmas (pe->u.space);
1070 ct++;
1072 return ct;
1075 /* Save into SD the names of the registered pragmas referenced by PE,
1076 and return a pointer to the next free space in SD. */
1078 static char **
1079 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1081 for (; pe != NULL; pe = pe->next)
1083 if (pe->is_nspace)
1084 sd = save_registered_pragmas (pe->u.space, sd);
1085 *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1086 HT_LEN (&pe->pragma->ident),
1087 HT_LEN (&pe->pragma->ident) + 1);
1089 return sd;
1092 /* Return a newly-allocated array which saves the names of the
1093 registered pragmas. */
1095 char **
1096 _cpp_save_pragma_names (cpp_reader *pfile)
1098 int ct = count_registered_pragmas (pfile->pragmas);
1099 char **result = xnewvec (char *, ct);
1100 (void) save_registered_pragmas (pfile->pragmas, result);
1101 return result;
1104 /* Restore from SD the names of the registered pragmas referenced by PE,
1105 and return a pointer to the next unused name in SD. */
1107 static char **
1108 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1109 char **sd)
1111 for (; pe != NULL; pe = pe->next)
1113 if (pe->is_nspace)
1114 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1115 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1116 free (*sd);
1117 sd++;
1119 return sd;
1122 /* Restore the names of the registered pragmas from SAVED. */
1124 void
1125 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1127 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1128 free (saved);
1131 /* Pragmata handling. We handle some, and pass the rest on to the
1132 front end. C99 defines three pragmas and says that no macro
1133 expansion is to be performed on them; whether or not macro
1134 expansion happens for other pragmas is implementation defined.
1135 This implementation never macro-expands the text after #pragma. */
1136 static void
1137 do_pragma (cpp_reader *pfile)
1139 const struct pragma_entry *p = NULL;
1140 const cpp_token *token, *pragma_token = pfile->cur_token;
1141 unsigned int count = 1;
1143 pfile->state.prevent_expansion++;
1145 token = cpp_get_token (pfile);
1146 if (token->type == CPP_NAME)
1148 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1149 if (p && p->is_nspace)
1151 count = 2;
1152 token = cpp_get_token (pfile);
1153 if (token->type == CPP_NAME)
1154 p = lookup_pragma_entry (p->u.space, token->val.node);
1155 else
1156 p = NULL;
1160 if (p)
1162 /* Since the handler below doesn't get the line number, that it
1163 might need for diagnostics, make sure it has the right
1164 numbers in place. */
1165 if (pfile->cb.line_change)
1166 (*pfile->cb.line_change) (pfile, pragma_token, false);
1167 (*p->u.handler) (pfile);
1169 else if (pfile->cb.def_pragma)
1171 _cpp_backup_tokens (pfile, count);
1172 pfile->cb.def_pragma (pfile, pfile->directive_line);
1175 pfile->state.prevent_expansion--;
1178 /* Handle #pragma once. */
1179 static void
1180 do_pragma_once (cpp_reader *pfile)
1182 if (pfile->buffer->prev == NULL)
1183 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1185 check_eol (pfile);
1186 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1189 /* Handle #pragma GCC poison, to poison one or more identifiers so
1190 that the lexer produces a hard error for each subsequent usage. */
1191 static void
1192 do_pragma_poison (cpp_reader *pfile)
1194 const cpp_token *tok;
1195 cpp_hashnode *hp;
1197 pfile->state.poisoned_ok = 1;
1198 for (;;)
1200 tok = _cpp_lex_token (pfile);
1201 if (tok->type == CPP_EOF)
1202 break;
1203 if (tok->type != CPP_NAME)
1205 cpp_error (pfile, CPP_DL_ERROR,
1206 "invalid #pragma GCC poison directive");
1207 break;
1210 hp = tok->val.node;
1211 if (hp->flags & NODE_POISONED)
1212 continue;
1214 if (hp->type == NT_MACRO)
1215 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1216 NODE_NAME (hp));
1217 _cpp_free_definition (hp);
1218 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1220 pfile->state.poisoned_ok = 0;
1223 /* Mark the current header as a system header. This will suppress
1224 some categories of warnings (notably those from -pedantic). It is
1225 intended for use in system libraries that cannot be implemented in
1226 conforming C, but cannot be certain that their headers appear in a
1227 system include directory. To prevent abuse, it is rejected in the
1228 primary source file. */
1229 static void
1230 do_pragma_system_header (cpp_reader *pfile)
1232 cpp_buffer *buffer = pfile->buffer;
1234 if (buffer->prev == 0)
1235 cpp_error (pfile, CPP_DL_WARNING,
1236 "#pragma system_header ignored outside include file");
1237 else
1239 check_eol (pfile);
1240 skip_rest_of_line (pfile);
1241 cpp_make_system_header (pfile, 1, 0);
1245 /* Check the modified date of the current include file against a specified
1246 file. Issue a diagnostic, if the specified file is newer. We use this to
1247 determine if a fixed header should be refixed. */
1248 static void
1249 do_pragma_dependency (cpp_reader *pfile)
1251 const char *fname;
1252 int angle_brackets, ordering;
1254 fname = parse_include (pfile, &angle_brackets);
1255 if (!fname)
1256 return;
1258 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1259 if (ordering < 0)
1260 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1261 else if (ordering > 0)
1263 cpp_error (pfile, CPP_DL_WARNING,
1264 "current file is older than %s", fname);
1265 if (cpp_get_token (pfile)->type != CPP_EOF)
1267 _cpp_backup_tokens (pfile, 1);
1268 do_diagnostic (pfile, CPP_DL_WARNING, 0);
1272 free ((void *) fname);
1275 /* Get a token but skip padding. */
1276 static const cpp_token *
1277 get_token_no_padding (cpp_reader *pfile)
1279 for (;;)
1281 const cpp_token *result = cpp_get_token (pfile);
1282 if (result->type != CPP_PADDING)
1283 return result;
1287 /* Check syntax is "(string-literal)". Returns the string on success,
1288 or NULL on failure. */
1289 static const cpp_token *
1290 get__Pragma_string (cpp_reader *pfile)
1292 const cpp_token *string;
1294 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1295 return NULL;
1297 string = get_token_no_padding (pfile);
1298 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1299 return NULL;
1301 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1302 return NULL;
1304 return string;
1307 /* Destringize IN into a temporary buffer, by removing the first \ of
1308 \" and \\ sequences, and process the result as a #pragma directive. */
1309 static void
1310 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1312 const unsigned char *src, *limit;
1313 char *dest, *result;
1315 dest = result = alloca (in->len - 1);
1316 src = in->text + 1 + (in->text[0] == 'L');
1317 limit = in->text + in->len - 1;
1318 while (src < limit)
1320 /* We know there is a character following the backslash. */
1321 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1322 src++;
1323 *dest++ = *src++;
1325 *dest = '\n';
1327 /* Ugh; an awful kludge. We are really not set up to be lexing
1328 tokens when in the middle of a macro expansion. Use a new
1329 context to force cpp_get_token to lex, and so skip_rest_of_line
1330 doesn't go beyond the end of the text. Also, remember the
1331 current lexing position so we can return to it later.
1333 Something like line-at-a-time lexing should remove the need for
1334 this. */
1336 cpp_context *saved_context = pfile->context;
1337 cpp_token *saved_cur_token = pfile->cur_token;
1338 tokenrun *saved_cur_run = pfile->cur_run;
1340 pfile->context = xnew (cpp_context);
1341 pfile->context->macro = 0;
1342 pfile->context->prev = 0;
1343 run_directive (pfile, T_PRAGMA, result, dest - result);
1344 free (pfile->context);
1345 pfile->context = saved_context;
1346 pfile->cur_token = saved_cur_token;
1347 pfile->cur_run = saved_cur_run;
1348 pfile->line--;
1351 /* See above comment. For the moment, we'd like
1353 token1 _Pragma ("foo") token2
1355 to be output as
1357 token1
1358 # 7 "file.c"
1359 #pragma foo
1360 # 7 "file.c"
1361 token2
1363 Getting the line markers is a little tricky. */
1364 if (pfile->cb.line_change)
1365 pfile->cb.line_change (pfile, pfile->cur_token, false);
1368 /* Handle the _Pragma operator. */
1369 void
1370 _cpp_do__Pragma (cpp_reader *pfile)
1372 const cpp_token *string = get__Pragma_string (pfile);
1374 if (string)
1375 destringize_and_run (pfile, &string->val.str);
1376 else
1377 cpp_error (pfile, CPP_DL_ERROR,
1378 "_Pragma takes a parenthesized string literal");
1381 /* Ignore #sccs on all systems. */
1382 static void
1383 do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
1387 /* Handle #ifdef. */
1388 static void
1389 do_ifdef (cpp_reader *pfile)
1391 int skip = 1;
1393 if (! pfile->state.skipping)
1395 const cpp_hashnode *node = lex_macro_node (pfile);
1397 if (node)
1399 skip = node->type != NT_MACRO;
1400 _cpp_mark_macro_used (node);
1401 check_eol (pfile);
1405 push_conditional (pfile, skip, T_IFDEF, 0);
1408 /* Handle #ifndef. */
1409 static void
1410 do_ifndef (cpp_reader *pfile)
1412 int skip = 1;
1413 const cpp_hashnode *node = 0;
1415 if (! pfile->state.skipping)
1417 node = lex_macro_node (pfile);
1419 if (node)
1421 skip = node->type == NT_MACRO;
1422 _cpp_mark_macro_used (node);
1423 check_eol (pfile);
1427 push_conditional (pfile, skip, T_IFNDEF, node);
1430 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1431 pfile->mi_ind_cmacro so we can handle multiple-include
1432 optimizations. If macro expansion occurs in the expression, we
1433 cannot treat it as a controlling conditional, since the expansion
1434 could change in the future. That is handled by cpp_get_token. */
1435 static void
1436 do_if (cpp_reader *pfile)
1438 int skip = 1;
1440 if (! pfile->state.skipping)
1441 skip = _cpp_parse_expr (pfile) == false;
1443 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1446 /* Flip skipping state if appropriate and continue without changing
1447 if_stack; this is so that the error message for missing #endif's
1448 etc. will point to the original #if. */
1449 static void
1450 do_else (cpp_reader *pfile)
1452 cpp_buffer *buffer = pfile->buffer;
1453 struct if_stack *ifs = buffer->if_stack;
1455 if (ifs == NULL)
1456 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1457 else
1459 if (ifs->type == T_ELSE)
1461 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1462 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1463 "the conditional began here");
1465 ifs->type = T_ELSE;
1467 /* Skip any future (erroneous) #elses or #elifs. */
1468 pfile->state.skipping = ifs->skip_elses;
1469 ifs->skip_elses = true;
1471 /* Invalidate any controlling macro. */
1472 ifs->mi_cmacro = 0;
1474 /* Only check EOL if was not originally skipping. */
1475 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1476 check_eol (pfile);
1480 /* Handle a #elif directive by not changing if_stack either. See the
1481 comment above do_else. */
1482 static void
1483 do_elif (cpp_reader *pfile)
1485 cpp_buffer *buffer = pfile->buffer;
1486 struct if_stack *ifs = buffer->if_stack;
1488 if (ifs == NULL)
1489 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1490 else
1492 if (ifs->type == T_ELSE)
1494 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1495 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1496 "the conditional began here");
1498 ifs->type = T_ELIF;
1500 /* Only evaluate this if we aren't skipping elses. During
1501 evaluation, set skipping to false to get lexer warnings. */
1502 if (ifs->skip_elses)
1503 pfile->state.skipping = 1;
1504 else
1506 pfile->state.skipping = 0;
1507 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1508 ifs->skip_elses = ! pfile->state.skipping;
1511 /* Invalidate any controlling macro. */
1512 ifs->mi_cmacro = 0;
1516 /* #endif pops the if stack and resets pfile->state.skipping. */
1517 static void
1518 do_endif (cpp_reader *pfile)
1520 cpp_buffer *buffer = pfile->buffer;
1521 struct if_stack *ifs = buffer->if_stack;
1523 if (ifs == NULL)
1524 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1525 else
1527 /* Only check EOL if was not originally skipping. */
1528 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1529 check_eol (pfile);
1531 /* If potential control macro, we go back outside again. */
1532 if (ifs->next == 0 && ifs->mi_cmacro)
1534 pfile->mi_valid = true;
1535 pfile->mi_cmacro = ifs->mi_cmacro;
1538 buffer->if_stack = ifs->next;
1539 pfile->state.skipping = ifs->was_skipping;
1540 obstack_free (&pfile->buffer_ob, ifs);
1544 /* Push an if_stack entry for a preprocessor conditional, and set
1545 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1546 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1547 we need to check here that we are at the top of the file. */
1548 static void
1549 push_conditional (cpp_reader *pfile, int skip, int type,
1550 const cpp_hashnode *cmacro)
1552 struct if_stack *ifs;
1553 cpp_buffer *buffer = pfile->buffer;
1555 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1556 ifs->line = pfile->directive_line;
1557 ifs->next = buffer->if_stack;
1558 ifs->skip_elses = pfile->state.skipping || !skip;
1559 ifs->was_skipping = pfile->state.skipping;
1560 ifs->type = type;
1561 /* This condition is effectively a test for top-of-file. */
1562 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1563 ifs->mi_cmacro = cmacro;
1564 else
1565 ifs->mi_cmacro = 0;
1567 pfile->state.skipping = skip;
1568 buffer->if_stack = ifs;
1571 /* Read the tokens of the answer into the macro pool, in a directive
1572 of type TYPE. Only commit the memory if we intend it as permanent
1573 storage, i.e. the #assert case. Returns 0 on success, and sets
1574 ANSWERP to point to the answer. */
1575 static int
1576 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1578 const cpp_token *paren;
1579 struct answer *answer;
1580 unsigned int acount;
1582 /* In a conditional, it is legal to not have an open paren. We
1583 should save the following token in this case. */
1584 paren = cpp_get_token (pfile);
1586 /* If not a paren, see if we're OK. */
1587 if (paren->type != CPP_OPEN_PAREN)
1589 /* In a conditional no answer is a test for any answer. It
1590 could be followed by any token. */
1591 if (type == T_IF)
1593 _cpp_backup_tokens (pfile, 1);
1594 return 0;
1597 /* #unassert with no answer is valid - it removes all answers. */
1598 if (type == T_UNASSERT && paren->type == CPP_EOF)
1599 return 0;
1601 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1602 return 1;
1605 for (acount = 0;; acount++)
1607 size_t room_needed;
1608 const cpp_token *token = cpp_get_token (pfile);
1609 cpp_token *dest;
1611 if (token->type == CPP_CLOSE_PAREN)
1612 break;
1614 if (token->type == CPP_EOF)
1616 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1617 return 1;
1620 /* struct answer includes the space for one token. */
1621 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1623 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1624 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1626 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1627 *dest = *token;
1629 /* Drop whitespace at start, for answer equivalence purposes. */
1630 if (acount == 0)
1631 dest->flags &= ~PREV_WHITE;
1634 if (acount == 0)
1636 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1637 return 1;
1640 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1641 answer->count = acount;
1642 answer->next = NULL;
1643 *answerp = answer;
1645 return 0;
1648 /* Parses an assertion directive of type TYPE, returning a pointer to
1649 the hash node of the predicate, or 0 on error. If an answer was
1650 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1651 static cpp_hashnode *
1652 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1654 cpp_hashnode *result = 0;
1655 const cpp_token *predicate;
1657 /* We don't expand predicates or answers. */
1658 pfile->state.prevent_expansion++;
1660 *answerp = 0;
1661 predicate = cpp_get_token (pfile);
1662 if (predicate->type == CPP_EOF)
1663 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1664 else if (predicate->type != CPP_NAME)
1665 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1666 else if (parse_answer (pfile, answerp, type) == 0)
1668 unsigned int len = NODE_LEN (predicate->val.node);
1669 unsigned char *sym = alloca (len + 1);
1671 /* Prefix '#' to get it out of macro namespace. */
1672 sym[0] = '#';
1673 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1674 result = cpp_lookup (pfile, sym, len + 1);
1677 pfile->state.prevent_expansion--;
1678 return result;
1681 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1682 or a pointer to NULL if the answer is not in the chain. */
1683 static struct answer **
1684 find_answer (cpp_hashnode *node, const struct answer *candidate)
1686 unsigned int i;
1687 struct answer **result;
1689 for (result = &node->value.answers; *result; result = &(*result)->next)
1691 struct answer *answer = *result;
1693 if (answer->count == candidate->count)
1695 for (i = 0; i < answer->count; i++)
1696 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1697 break;
1699 if (i == answer->count)
1700 break;
1704 return result;
1707 /* Test an assertion within a preprocessor conditional. Returns
1708 nonzero on failure, zero on success. On success, the result of
1709 the test is written into VALUE, otherwise the value 0. */
1711 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1713 struct answer *answer;
1714 cpp_hashnode *node;
1716 node = parse_assertion (pfile, &answer, T_IF);
1718 /* For recovery, an erroneous assertion expression is handled as a
1719 failing assertion. */
1720 *value = 0;
1722 if (node)
1723 *value = (node->type == NT_ASSERTION &&
1724 (answer == 0 || *find_answer (node, answer) != 0));
1725 else if (pfile->cur_token[-1].type == CPP_EOF)
1726 _cpp_backup_tokens (pfile, 1);
1728 /* We don't commit the memory for the answer - it's temporary only. */
1729 return node == 0;
1732 /* Handle #assert. */
1733 static void
1734 do_assert (cpp_reader *pfile)
1736 struct answer *new_answer;
1737 cpp_hashnode *node;
1739 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1740 if (node)
1742 /* Place the new answer in the answer list. First check there
1743 is not a duplicate. */
1744 new_answer->next = 0;
1745 if (node->type == NT_ASSERTION)
1747 if (*find_answer (node, new_answer))
1749 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
1750 NODE_NAME (node) + 1);
1751 return;
1753 new_answer->next = node->value.answers;
1756 node->type = NT_ASSERTION;
1757 node->value.answers = new_answer;
1758 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1759 + (new_answer->count - 1)
1760 * sizeof (cpp_token));
1761 check_eol (pfile);
1765 /* Handle #unassert. */
1766 static void
1767 do_unassert (cpp_reader *pfile)
1769 cpp_hashnode *node;
1770 struct answer *answer;
1772 node = parse_assertion (pfile, &answer, T_UNASSERT);
1773 /* It isn't an error to #unassert something that isn't asserted. */
1774 if (node && node->type == NT_ASSERTION)
1776 if (answer)
1778 struct answer **p = find_answer (node, answer), *temp;
1780 /* Remove the answer from the list. */
1781 temp = *p;
1782 if (temp)
1783 *p = temp->next;
1785 /* Did we free the last answer? */
1786 if (node->value.answers == 0)
1787 node->type = NT_VOID;
1789 check_eol (pfile);
1791 else
1792 _cpp_free_definition (node);
1795 /* We don't commit the memory for the answer - it's temporary only. */
1798 /* These are for -D, -U, -A. */
1800 /* Process the string STR as if it appeared as the body of a #define.
1801 If STR is just an identifier, define it with value 1.
1802 If STR has anything after the identifier, then it should
1803 be identifier=definition. */
1804 void
1805 cpp_define (cpp_reader *pfile, const char *str)
1807 char *buf, *p;
1808 size_t count;
1810 /* Copy the entire option so we can modify it.
1811 Change the first "=" in the string to a space. If there is none,
1812 tack " 1" on the end. */
1814 count = strlen (str);
1815 buf = alloca (count + 3);
1816 memcpy (buf, str, count);
1818 p = strchr (str, '=');
1819 if (p)
1820 buf[p - str] = ' ';
1821 else
1823 buf[count++] = ' ';
1824 buf[count++] = '1';
1826 buf[count] = '\n';
1828 run_directive (pfile, T_DEFINE, buf, count);
1831 /* Slight variant of the above for use by initialize_builtins. */
1832 void
1833 _cpp_define_builtin (cpp_reader *pfile, const char *str)
1835 size_t len = strlen (str);
1836 char *buf = alloca (len + 1);
1837 memcpy (buf, str, len);
1838 buf[len] = '\n';
1839 run_directive (pfile, T_DEFINE, buf, len);
1842 /* Process MACRO as if it appeared as the body of an #undef. */
1843 void
1844 cpp_undef (cpp_reader *pfile, const char *macro)
1846 size_t len = strlen (macro);
1847 char *buf = alloca (len + 1);
1848 memcpy (buf, macro, len);
1849 buf[len] = '\n';
1850 run_directive (pfile, T_UNDEF, buf, len);
1853 /* Process the string STR as if it appeared as the body of a #assert. */
1854 void
1855 cpp_assert (cpp_reader *pfile, const char *str)
1857 handle_assertion (pfile, str, T_ASSERT);
1860 /* Process STR as if it appeared as the body of an #unassert. */
1861 void
1862 cpp_unassert (cpp_reader *pfile, const char *str)
1864 handle_assertion (pfile, str, T_UNASSERT);
1867 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1868 static void
1869 handle_assertion (cpp_reader *pfile, const char *str, int type)
1871 size_t count = strlen (str);
1872 const char *p = strchr (str, '=');
1874 /* Copy the entire option so we can modify it. Change the first
1875 "=" in the string to a '(', and tack a ')' on the end. */
1876 char *buf = alloca (count + 2);
1878 memcpy (buf, str, count);
1879 if (p)
1881 buf[p - str] = '(';
1882 buf[count++] = ')';
1884 buf[count] = '\n';
1885 str = buf;
1887 run_directive (pfile, type, str, count);
1890 /* The number of errors for a given reader. */
1891 unsigned int
1892 cpp_errors (cpp_reader *pfile)
1894 return pfile->errors;
1897 /* The options structure. */
1898 cpp_options *
1899 cpp_get_options (cpp_reader *pfile)
1901 return &pfile->opts;
1904 /* The callbacks structure. */
1905 cpp_callbacks *
1906 cpp_get_callbacks (cpp_reader *pfile)
1908 return &pfile->cb;
1911 /* The line map set. */
1912 struct line_maps *
1913 cpp_get_line_maps (cpp_reader *pfile)
1915 return &pfile->line_maps;
1918 /* Copy the given callbacks structure to our own. */
1919 void
1920 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
1922 pfile->cb = *cb;
1925 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1926 doesn't fail. It does not generate a file change call back; that
1927 is the responsibility of the caller. */
1928 cpp_buffer *
1929 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
1930 int from_stage3)
1932 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1934 /* Clears, amongst other things, if_stack and mi_cmacro. */
1935 memset (new, 0, sizeof (cpp_buffer));
1937 new->next_line = new->buf = buffer;
1938 new->rlimit = buffer + len;
1939 new->from_stage3 = from_stage3;
1940 new->prev = pfile->buffer;
1941 new->need_line = true;
1943 pfile->buffer = new;
1944 return new;
1947 /* Pops a single buffer, with a file change call-back if appropriate.
1948 Then pushes the next -include file, if any remain. */
1949 void
1950 _cpp_pop_buffer (cpp_reader *pfile)
1952 cpp_buffer *buffer = pfile->buffer;
1953 struct _cpp_file *inc = buffer->file;
1954 struct if_stack *ifs;
1956 /* Walk back up the conditional stack till we reach its level at
1957 entry to this file, issuing error messages. */
1958 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1959 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1960 "unterminated #%s", dtable[ifs->type].name);
1962 /* In case of a missing #endif. */
1963 pfile->state.skipping = 0;
1965 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
1966 pfile->buffer = buffer->prev;
1968 free (buffer->notes);
1970 /* Free the buffer object now; we may want to push a new buffer
1971 in _cpp_push_next_include_file. */
1972 obstack_free (&pfile->buffer_ob, buffer);
1974 if (inc)
1976 _cpp_pop_file_buffer (pfile, inc);
1978 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
1982 /* Enter all recognized directives in the hash table. */
1983 void
1984 _cpp_init_directives (cpp_reader *pfile)
1986 unsigned int i;
1987 cpp_hashnode *node;
1989 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1991 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1992 node->is_directive = 1;
1993 node->directive_index = i;