* gcc.target/avr/progmem.h (pgm_read_char): Define depending on
[official-gcc.git] / libcpp / directives.c
blob0510c6e3a8ef1480b03c5e51f8b4d4fe7f8e9a09
1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 Contributed by Per Bothner, 1994-95.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
12 later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "internal.h"
27 #include "mkdeps.h"
28 #include "obstack.h"
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
32 struct if_stack
34 struct if_stack *next;
35 source_location line; /* Line where condition started. */
36 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
37 bool skip_elses; /* Can future #else / #elif be skipped? */
38 bool was_skipping; /* If were skipping on entry. */
39 int type; /* Most recent conditional for diagnostics. */
42 /* Contains a registered pragma or pragma namespace. */
43 typedef void (*pragma_cb) (cpp_reader *);
44 struct pragma_entry
46 struct pragma_entry *next;
47 const cpp_hashnode *pragma; /* Name and length. */
48 bool is_nspace;
49 bool is_internal;
50 bool is_deferred;
51 bool allow_expansion;
52 union {
53 pragma_cb handler;
54 struct pragma_entry *space;
55 unsigned int ident;
56 } u;
59 /* Values for the origin field of struct directive. KANDR directives
60 come from traditional (K&R) C. STDC89 directives come from the
61 1989 C standard. EXTENSION directives are extensions. */
62 #define KANDR 0
63 #define STDC89 1
64 #define EXTENSION 2
66 /* Values for the flags field of struct directive. COND indicates a
67 conditional; IF_COND an opening conditional. INCL means to treat
68 "..." and <...> as q-char and h-char sequences respectively. IN_I
69 means this directive should be handled even if -fpreprocessed is in
70 effect (these are the directives with callback hooks).
72 EXPAND is set on directives that are always macro-expanded. */
73 #define COND (1 << 0)
74 #define IF_COND (1 << 1)
75 #define INCL (1 << 2)
76 #define IN_I (1 << 3)
77 #define EXPAND (1 << 4)
78 #define DEPRECATED (1 << 5)
80 /* Defines one #-directive, including how to handle it. */
81 typedef void (*directive_handler) (cpp_reader *);
82 typedef struct directive directive;
83 struct directive
85 directive_handler handler; /* Function to handle directive. */
86 const uchar *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 (cpp_reader *);
95 static void check_eol (cpp_reader *, bool);
96 static void start_directive (cpp_reader *);
97 static void prepare_directive_trad (cpp_reader *);
98 static void end_directive (cpp_reader *, int);
99 static void directive_diagnostics (cpp_reader *, const directive *, int);
100 static void run_directive (cpp_reader *, int, const char *, size_t);
101 static char *glue_header_name (cpp_reader *);
102 static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
103 source_location *);
104 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
105 static unsigned int read_flag (cpp_reader *, unsigned int);
106 static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
107 static void do_diagnostic (cpp_reader *, int, int, int);
108 static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
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 int count_registered_pragmas (struct pragma_entry *);
114 static char ** save_registered_pragmas (struct pragma_entry *, char **);
115 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
116 char **);
117 static void do_pragma_once (cpp_reader *);
118 static void do_pragma_poison (cpp_reader *);
119 static void do_pragma_system_header (cpp_reader *);
120 static void do_pragma_dependency (cpp_reader *);
121 static void do_linemarker (cpp_reader *);
122 static const cpp_token *get_token_no_padding (cpp_reader *);
123 static const cpp_token *get__Pragma_string (cpp_reader *);
124 static void destringize_and_run (cpp_reader *, const cpp_string *);
125 static int parse_answer (cpp_reader *, struct answer **, int, source_location);
126 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
127 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
128 static void handle_assertion (cpp_reader *, const char *, int);
129 static void do_pragma_push_macro (cpp_reader *);
130 static void do_pragma_pop_macro (cpp_reader *);
131 static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *);
133 /* This is the table of directive handlers. It is ordered by
134 frequency of occurrence; the numbers at the end are directive
135 counts from all the source code I have lying around (egcs and libc
136 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
137 pcmcia-cs-3.0.9). This is no longer important as directive lookup
138 is now O(1). All extensions other than #warning, #include_next,
139 and #import are deprecated. The name is where the extension
140 appears to have come from. */
142 #define DIRECTIVE_TABLE \
143 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
144 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
145 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
146 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
147 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
148 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
149 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
150 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
151 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
152 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
153 D(error, T_ERROR, STDC89, 0) /* 475 */ \
154 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
155 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
156 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
157 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
158 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
159 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
160 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
161 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
163 /* #sccs is synonymous with #ident. */
164 #define do_sccs do_ident
166 /* Use the table to generate a series of prototypes, an enum for the
167 directive names, and an array of directive handlers. */
169 #define D(name, t, o, f) static void do_##name (cpp_reader *);
170 DIRECTIVE_TABLE
171 #undef D
173 #define D(n, tag, o, f) tag,
174 enum
176 DIRECTIVE_TABLE
177 N_DIRECTIVES
179 #undef D
181 #define D(name, t, origin, flags) \
182 { do_##name, (const uchar *) #name, \
183 sizeof #name - 1, origin, flags },
184 static const directive dtable[] =
186 DIRECTIVE_TABLE
188 #undef D
189 #undef DIRECTIVE_TABLE
191 /* Wrapper struct directive for linemarkers.
192 The origin is more or less true - the original K+R cpp
193 did use this notation in its preprocessed output. */
194 static const directive linemarker_dir =
196 do_linemarker, UC"#", 1, KANDR, IN_I
199 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
201 /* Skip any remaining tokens in a directive. */
202 static void
203 skip_rest_of_line (cpp_reader *pfile)
205 /* Discard all stacked contexts. */
206 while (pfile->context->prev)
207 _cpp_pop_context (pfile);
209 /* Sweep up all tokens remaining on the line. */
210 if (! SEEN_EOL ())
211 while (_cpp_lex_token (pfile)->type != CPP_EOF)
215 /* Ensure there are no stray tokens at the end of a directive. If
216 EXPAND is true, tokens macro-expanding to nothing are allowed. */
217 static void
218 check_eol (cpp_reader *pfile, bool expand)
220 if (! SEEN_EOL () && (expand
221 ? cpp_get_token (pfile)
222 : _cpp_lex_token (pfile))->type != CPP_EOF)
223 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
224 pfile->directive->name);
227 /* Ensure there are no stray tokens other than comments at the end of
228 a directive, and gather the comments. */
229 static const cpp_token **
230 check_eol_return_comments (cpp_reader *pfile)
232 size_t c;
233 size_t capacity = 8;
234 const cpp_token **buf;
236 buf = XNEWVEC (const cpp_token *, capacity);
237 c = 0;
238 if (! SEEN_EOL ())
240 while (1)
242 const cpp_token *tok;
244 tok = _cpp_lex_token (pfile);
245 if (tok->type == CPP_EOF)
246 break;
247 if (tok->type != CPP_COMMENT)
248 cpp_error (pfile, CPP_DL_PEDWARN,
249 "extra tokens at end of #%s directive",
250 pfile->directive->name);
251 else
253 if (c + 1 >= capacity)
255 capacity *= 2;
256 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
258 buf[c] = tok;
259 ++c;
263 buf[c] = NULL;
264 return buf;
267 /* Called when entering a directive, _Pragma or command-line directive. */
268 static void
269 start_directive (cpp_reader *pfile)
271 /* Setup in-directive state. */
272 pfile->state.in_directive = 1;
273 pfile->state.save_comments = 0;
274 pfile->directive_result.type = CPP_PADDING;
276 /* Some handlers need the position of the # for diagnostics. */
277 pfile->directive_line = pfile->line_table->highest_line;
280 /* Called when leaving a directive, _Pragma or command-line directive. */
281 static void
282 end_directive (cpp_reader *pfile, int skip_line)
284 if (CPP_OPTION (pfile, traditional))
286 /* Revert change of prepare_directive_trad. */
287 if (!pfile->state.in_deferred_pragma)
288 pfile->state.prevent_expansion--;
290 if (pfile->directive != &dtable[T_DEFINE])
291 _cpp_remove_overlay (pfile);
293 else if (pfile->state.in_deferred_pragma)
295 /* We don't skip for an assembler #. */
296 else if (skip_line)
298 skip_rest_of_line (pfile);
299 if (!pfile->keep_tokens)
301 pfile->cur_run = &pfile->base_run;
302 pfile->cur_token = pfile->base_run.base;
306 /* Restore state. */
307 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
308 pfile->state.in_directive = 0;
309 pfile->state.in_expression = 0;
310 pfile->state.angled_headers = 0;
311 pfile->directive = 0;
314 /* Prepare to handle the directive in pfile->directive. */
315 static void
316 prepare_directive_trad (cpp_reader *pfile)
318 if (pfile->directive != &dtable[T_DEFINE])
320 bool no_expand = (pfile->directive
321 && ! (pfile->directive->flags & EXPAND));
322 bool was_skipping = pfile->state.skipping;
324 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
325 || pfile->directive == &dtable[T_ELIF]);
326 if (pfile->state.in_expression)
327 pfile->state.skipping = false;
329 if (no_expand)
330 pfile->state.prevent_expansion++;
331 _cpp_scan_out_logical_line (pfile, NULL);
332 if (no_expand)
333 pfile->state.prevent_expansion--;
335 pfile->state.skipping = was_skipping;
336 _cpp_overlay_buffer (pfile, pfile->out.base,
337 pfile->out.cur - pfile->out.base);
340 /* Stop ISO C from expanding anything. */
341 pfile->state.prevent_expansion++;
344 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
345 the '#' was indented. */
346 static void
347 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
349 /* Issue -pedantic or deprecated warnings for extensions. We let
350 -pedantic take precedence if both are applicable. */
351 if (! pfile->state.skipping)
353 if (dir->origin == EXTENSION
354 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
355 && CPP_PEDANTIC (pfile))
356 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
357 else if (((dir->flags & DEPRECATED) != 0
358 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
359 && CPP_OPTION (pfile, cpp_warn_deprecated))
360 cpp_warning (pfile, CPP_W_DEPRECATED,
361 "#%s is a deprecated GCC extension", dir->name);
364 /* Traditionally, a directive is ignored unless its # is in
365 column 1. Therefore in code intended to work with K+R
366 compilers, directives added by C89 must have their #
367 indented, and directives present in traditional C must not.
368 This is true even of directives in skipped conditional
369 blocks. #elif cannot be used at all. */
370 if (CPP_WTRADITIONAL (pfile))
372 if (dir == &dtable[T_ELIF])
373 cpp_warning (pfile, CPP_W_TRADITIONAL,
374 "suggest not using #elif in traditional C");
375 else if (indented && dir->origin == KANDR)
376 cpp_warning (pfile, CPP_W_TRADITIONAL,
377 "traditional C ignores #%s with the # indented",
378 dir->name);
379 else if (!indented && dir->origin != KANDR)
380 cpp_warning (pfile, CPP_W_TRADITIONAL,
381 "suggest hiding #%s from traditional C with an indented #",
382 dir->name);
386 /* Check if we have a known directive. INDENTED is nonzero if the
387 '#' of the directive was indented. This function is in this file
388 to save unnecessarily exporting dtable etc. to lex.c. Returns
389 nonzero if the line of tokens has been handled, zero if we should
390 continue processing the line. */
392 _cpp_handle_directive (cpp_reader *pfile, int indented)
394 const directive *dir = 0;
395 const cpp_token *dname;
396 bool was_parsing_args = pfile->state.parsing_args;
397 bool was_discarding_output = pfile->state.discarding_output;
398 int skip = 1;
400 if (was_discarding_output)
401 pfile->state.prevent_expansion = 0;
403 if (was_parsing_args)
405 if (CPP_OPTION (pfile, cpp_pedantic))
406 cpp_error (pfile, CPP_DL_PEDWARN,
407 "embedding a directive within macro arguments is not portable");
408 pfile->state.parsing_args = 0;
409 pfile->state.prevent_expansion = 0;
411 start_directive (pfile);
412 dname = _cpp_lex_token (pfile);
414 if (dname->type == CPP_NAME)
416 if (dname->val.node.node->is_directive)
417 dir = &dtable[dname->val.node.node->directive_index];
419 /* We do not recognize the # followed by a number extension in
420 assembler code. */
421 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
423 dir = &linemarker_dir;
424 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
425 && ! pfile->state.skipping)
426 cpp_error (pfile, CPP_DL_PEDWARN,
427 "style of line directive is a GCC extension");
430 if (dir)
432 /* If we have a directive that is not an opening conditional,
433 invalidate any control macro. */
434 if (! (dir->flags & IF_COND))
435 pfile->mi_valid = false;
437 /* Kluge alert. In order to be sure that code like this
439 #define HASH #
440 HASH define foo bar
442 does not cause '#define foo bar' to get executed when
443 compiled with -save-temps, we recognize directives in
444 -fpreprocessed mode only if the # is in column 1. macro.c
445 puts a space in front of any '#' at the start of a macro.
447 We exclude the -fdirectives-only case because macro expansion
448 has not been performed yet, and block comments can cause spaces
449 to preceed the directive. */
450 if (CPP_OPTION (pfile, preprocessed)
451 && !CPP_OPTION (pfile, directives_only)
452 && (indented || !(dir->flags & IN_I)))
454 skip = 0;
455 dir = 0;
457 else
459 /* In failed conditional groups, all non-conditional
460 directives are ignored. Before doing that, whether
461 skipping or not, we should lex angle-bracketed headers
462 correctly, and maybe output some diagnostics. */
463 pfile->state.angled_headers = dir->flags & INCL;
464 pfile->state.directive_wants_padding = dir->flags & INCL;
465 if (! CPP_OPTION (pfile, preprocessed))
466 directive_diagnostics (pfile, dir, indented);
467 if (pfile->state.skipping && !(dir->flags & COND))
468 dir = 0;
471 else if (dname->type == CPP_EOF)
472 ; /* CPP_EOF is the "null directive". */
473 else
475 /* An unknown directive. Don't complain about it in assembly
476 source: we don't know where the comments are, and # may
477 introduce assembler pseudo-ops. Don't complain about invalid
478 directives in skipped conditional groups (6.10 p4). */
479 if (CPP_OPTION (pfile, lang) == CLK_ASM)
480 skip = 0;
481 else if (!pfile->state.skipping)
482 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
483 cpp_token_as_text (pfile, dname));
486 pfile->directive = dir;
487 if (CPP_OPTION (pfile, traditional))
488 prepare_directive_trad (pfile);
490 if (dir)
491 pfile->directive->handler (pfile);
492 else if (skip == 0)
493 _cpp_backup_tokens (pfile, 1);
495 end_directive (pfile, skip);
496 if (was_parsing_args && !pfile->state.in_deferred_pragma)
498 /* Restore state when within macro args. */
499 pfile->state.parsing_args = 2;
500 pfile->state.prevent_expansion = 1;
502 if (was_discarding_output)
503 pfile->state.prevent_expansion = 1;
504 return skip;
507 /* Directive handler wrapper used by the command line option
508 processor. BUF is \n terminated. */
509 static void
510 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
512 cpp_push_buffer (pfile, (const uchar *) buf, count,
513 /* from_stage3 */ true);
514 start_directive (pfile);
516 /* This is a short-term fix to prevent a leading '#' being
517 interpreted as a directive. */
518 _cpp_clean_line (pfile);
520 pfile->directive = &dtable[dir_no];
521 if (CPP_OPTION (pfile, traditional))
522 prepare_directive_trad (pfile);
523 pfile->directive->handler (pfile);
524 end_directive (pfile, 1);
525 _cpp_pop_buffer (pfile);
528 /* Checks for validity the macro name in #define, #undef, #ifdef and
529 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
530 processing a #define or #undefine directive, and false
531 otherwise. */
532 static cpp_hashnode *
533 lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
535 const cpp_token *token = _cpp_lex_token (pfile);
537 /* The token immediately after #define must be an identifier. That
538 identifier may not be "defined", per C99 6.10.8p4.
539 In C++, it may not be any of the "named operators" either,
540 per C++98 [lex.digraph], [lex.key].
541 Finally, the identifier may not have been poisoned. (In that case
542 the lexer has issued the error message for us.) */
544 if (token->type == CPP_NAME)
546 cpp_hashnode *node = token->val.node.node;
548 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
549 cpp_error (pfile, CPP_DL_ERROR,
550 "\"defined\" cannot be used as a macro name");
551 else if (! (node->flags & NODE_POISONED))
552 return node;
554 else if (token->flags & NAMED_OP)
555 cpp_error (pfile, CPP_DL_ERROR,
556 "\"%s\" cannot be used as a macro name as it is an operator in C++",
557 NODE_NAME (token->val.node.node));
558 else if (token->type == CPP_EOF)
559 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
560 pfile->directive->name);
561 else
562 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
564 return NULL;
567 /* Process a #define directive. Most work is done in macro.c. */
568 static void
569 do_define (cpp_reader *pfile)
571 cpp_hashnode *node = lex_macro_node (pfile, true);
573 if (node)
575 /* If we have been requested to expand comments into macros,
576 then re-enable saving of comments. */
577 pfile->state.save_comments =
578 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
580 if (pfile->cb.before_define)
581 pfile->cb.before_define (pfile);
583 if (_cpp_create_definition (pfile, node))
584 if (pfile->cb.define)
585 pfile->cb.define (pfile, pfile->directive_line, node);
587 node->flags &= ~NODE_USED;
591 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
592 static void
593 do_undef (cpp_reader *pfile)
595 cpp_hashnode *node = lex_macro_node (pfile, true);
597 if (node)
599 if (pfile->cb.before_define)
600 pfile->cb.before_define (pfile);
602 if (pfile->cb.undef)
603 pfile->cb.undef (pfile, pfile->directive_line, node);
605 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
606 identifier is not currently defined as a macro name. */
607 if (node->type == NT_MACRO)
609 if (node->flags & NODE_WARN)
610 cpp_error (pfile, CPP_DL_WARNING,
611 "undefining \"%s\"", NODE_NAME (node));
613 if (CPP_OPTION (pfile, warn_unused_macros))
614 _cpp_warn_if_unused_macro (pfile, node, NULL);
616 _cpp_free_definition (node);
620 check_eol (pfile, false);
623 /* Undefine a single macro/assertion/whatever. */
625 static int
626 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
627 void *data_p ATTRIBUTE_UNUSED)
629 /* Body of _cpp_free_definition inlined here for speed.
630 Macros and assertions no longer have anything to free. */
631 h->type = NT_VOID;
632 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
633 return 1;
636 /* Undefine all macros and assertions. */
638 void
639 cpp_undef_all (cpp_reader *pfile)
641 cpp_forall_identifiers (pfile, undefine_macros, NULL);
645 /* Helper routine used by parse_include. Reinterpret the current line
646 as an h-char-sequence (< ... >); we are looking at the first token
647 after the <. Returns a malloced filename. */
648 static char *
649 glue_header_name (cpp_reader *pfile)
651 const cpp_token *token;
652 char *buffer;
653 size_t len, total_len = 0, capacity = 1024;
655 /* To avoid lexed tokens overwriting our glued name, we can only
656 allocate from the string pool once we've lexed everything. */
657 buffer = XNEWVEC (char, capacity);
658 for (;;)
660 token = get_token_no_padding (pfile);
662 if (token->type == CPP_GREATER)
663 break;
664 if (token->type == CPP_EOF)
666 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
667 break;
670 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
671 if (total_len + len > capacity)
673 capacity = (capacity + len) * 2;
674 buffer = XRESIZEVEC (char, buffer, capacity);
677 if (token->flags & PREV_WHITE)
678 buffer[total_len++] = ' ';
680 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
681 true)
682 - (uchar *) buffer);
685 buffer[total_len] = '\0';
686 return buffer;
689 /* Returns the file name of #include, #include_next, #import and
690 #pragma dependency. The string is malloced and the caller should
691 free it. Returns NULL on error. LOCATION is the source location
692 of the file name. */
694 static const char *
695 parse_include (cpp_reader *pfile, int *pangle_brackets,
696 const cpp_token ***buf, source_location *location)
698 char *fname;
699 const cpp_token *header;
701 /* Allow macro expansion. */
702 header = get_token_no_padding (pfile);
703 *location = header->src_loc;
704 if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
705 || header->type == CPP_HEADER_NAME)
707 fname = XNEWVEC (char, header->val.str.len - 1);
708 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
709 fname[header->val.str.len - 2] = '\0';
710 *pangle_brackets = header->type == CPP_HEADER_NAME;
712 else if (header->type == CPP_LESS)
714 fname = glue_header_name (pfile);
715 *pangle_brackets = 1;
717 else
719 const unsigned char *dir;
721 if (pfile->directive == &dtable[T_PRAGMA])
722 dir = UC"pragma dependency";
723 else
724 dir = pfile->directive->name;
725 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
726 dir);
728 return NULL;
731 if (pfile->directive == &dtable[T_PRAGMA])
733 /* This pragma allows extra tokens after the file name. */
735 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
736 check_eol (pfile, true);
737 else
739 /* If we are not discarding comments, then gather them while
740 doing the eol check. */
741 *buf = check_eol_return_comments (pfile);
744 return fname;
747 /* Handle #include, #include_next and #import. */
748 static void
749 do_include_common (cpp_reader *pfile, enum include_type type)
751 const char *fname;
752 int angle_brackets;
753 const cpp_token **buf = NULL;
754 source_location location;
756 /* Re-enable saving of comments if requested, so that the include
757 callback can dump comments which follow #include. */
758 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
760 fname = parse_include (pfile, &angle_brackets, &buf, &location);
761 if (!fname)
763 if (buf)
764 XDELETEVEC (buf);
765 return;
768 if (!*fname)
770 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
771 "empty filename in #%s",
772 pfile->directive->name);
773 XDELETEVEC (fname);
774 if (buf)
775 XDELETEVEC (buf);
776 return;
779 /* Prevent #include recursion. */
780 if (pfile->line_table->depth >= CPP_STACK_MAX)
781 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
782 else
784 /* Get out of macro context, if we are. */
785 skip_rest_of_line (pfile);
787 if (pfile->cb.include)
788 pfile->cb.include (pfile, pfile->directive_line,
789 pfile->directive->name, fname, angle_brackets,
790 buf);
792 _cpp_stack_include (pfile, fname, angle_brackets, type);
795 XDELETEVEC (fname);
796 if (buf)
797 XDELETEVEC (buf);
800 static void
801 do_include (cpp_reader *pfile)
803 do_include_common (pfile, IT_INCLUDE);
806 static void
807 do_import (cpp_reader *pfile)
809 do_include_common (pfile, IT_IMPORT);
812 static void
813 do_include_next (cpp_reader *pfile)
815 enum include_type type = IT_INCLUDE_NEXT;
817 /* If this is the primary source file, warn and use the normal
818 search logic. */
819 if (cpp_in_primary_file (pfile))
821 cpp_error (pfile, CPP_DL_WARNING,
822 "#include_next in primary source file");
823 type = IT_INCLUDE;
825 do_include_common (pfile, type);
828 /* Subroutine of do_linemarker. Read possible flags after file name.
829 LAST is the last flag seen; 0 if this is the first flag. Return the
830 flag if it is valid, 0 at the end of the directive. Otherwise
831 complain. */
832 static unsigned int
833 read_flag (cpp_reader *pfile, unsigned int last)
835 const cpp_token *token = _cpp_lex_token (pfile);
837 if (token->type == CPP_NUMBER && token->val.str.len == 1)
839 unsigned int flag = token->val.str.text[0] - '0';
841 if (flag > last && flag <= 4
842 && (flag != 4 || last == 3)
843 && (flag != 2 || last == 0))
844 return flag;
847 if (token->type != CPP_EOF)
848 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
849 cpp_token_as_text (pfile, token));
850 return 0;
853 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
854 of length LEN, to binary; store it in NUMP, and return false if the
855 number was well-formed, true if not. WRAPPED is set to true if the
856 number did not fit into 'unsigned long'. */
857 static bool
858 strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
860 linenum_type reg = 0;
861 linenum_type reg_prev = 0;
863 uchar c;
864 *wrapped = false;
865 while (len--)
867 c = *str++;
868 if (!ISDIGIT (c))
869 return true;
870 reg *= 10;
871 reg += c - '0';
872 if (reg < reg_prev)
873 *wrapped = true;
874 reg_prev = reg;
876 *nump = reg;
877 return false;
880 /* Interpret #line command.
881 Note that the filename string (if any) is a true string constant
882 (escapes are interpreted), unlike in #line. */
883 static void
884 do_line (cpp_reader *pfile)
886 const struct line_maps *line_table = pfile->line_table;
887 const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
889 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
890 sysp right now. */
892 unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
893 const cpp_token *token;
894 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
895 linenum_type new_lineno;
897 /* C99 raised the minimum limit on #line numbers. */
898 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
899 bool wrapped;
901 /* #line commands expand macros. */
902 token = cpp_get_token (pfile);
903 if (token->type != CPP_NUMBER
904 || strtolinenum (token->val.str.text, token->val.str.len,
905 &new_lineno, &wrapped))
907 if (token->type == CPP_EOF)
908 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
909 else
910 cpp_error (pfile, CPP_DL_ERROR,
911 "\"%s\" after #line is not a positive integer",
912 cpp_token_as_text (pfile, token));
913 return;
916 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
917 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
918 else if (wrapped)
919 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
921 token = cpp_get_token (pfile);
922 if (token->type == CPP_STRING)
924 cpp_string s = { 0, 0 };
925 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
926 &s, CPP_STRING))
927 new_file = (const char *)s.text;
928 check_eol (pfile, true);
930 else if (token->type != CPP_EOF)
932 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
933 cpp_token_as_text (pfile, token));
934 return;
937 skip_rest_of_line (pfile);
938 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
939 map_sysp);
942 /* Interpret the # 44 "file" [flags] notation, which has slightly
943 different syntax and semantics from #line: Flags are allowed,
944 and we never complain about the line number being too big. */
945 static void
946 do_linemarker (cpp_reader *pfile)
948 const struct line_maps *line_table = pfile->line_table;
949 const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
950 const cpp_token *token;
951 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
952 linenum_type new_lineno;
953 unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
954 enum lc_reason reason = LC_RENAME_VERBATIM;
955 int flag;
956 bool wrapped;
958 /* Back up so we can get the number again. Putting this in
959 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
960 some circumstances, which can segfault. */
961 _cpp_backup_tokens (pfile, 1);
963 /* #line commands expand macros. */
964 token = cpp_get_token (pfile);
965 if (token->type != CPP_NUMBER
966 || strtolinenum (token->val.str.text, token->val.str.len,
967 &new_lineno, &wrapped))
969 /* Unlike #line, there does not seem to be a way to get an EOF
970 here. So, it should be safe to always spell the token. */
971 cpp_error (pfile, CPP_DL_ERROR,
972 "\"%s\" after # is not a positive integer",
973 cpp_token_as_text (pfile, token));
974 return;
977 token = cpp_get_token (pfile);
978 if (token->type == CPP_STRING)
980 cpp_string s = { 0, 0 };
981 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
982 1, &s, CPP_STRING))
983 new_file = (const char *)s.text;
985 new_sysp = 0;
986 flag = read_flag (pfile, 0);
987 if (flag == 1)
989 reason = LC_ENTER;
990 /* Fake an include for cpp_included (). */
991 _cpp_fake_include (pfile, new_file);
992 flag = read_flag (pfile, flag);
994 else if (flag == 2)
996 reason = LC_LEAVE;
997 flag = read_flag (pfile, flag);
999 if (flag == 3)
1001 new_sysp = 1;
1002 flag = read_flag (pfile, flag);
1003 if (flag == 4)
1004 new_sysp = 2;
1006 pfile->buffer->sysp = new_sysp;
1008 check_eol (pfile, false);
1010 else if (token->type != CPP_EOF)
1012 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
1013 cpp_token_as_text (pfile, token));
1014 return;
1017 skip_rest_of_line (pfile);
1019 /* Compensate for the increment in linemap_add that occurs in
1020 _cpp_do_file_change. We're currently at the start of the line
1021 *following* the #line directive. A separate source_location for this
1022 location makes no sense (until we do the LC_LEAVE), and
1023 complicates LAST_SOURCE_LINE_LOCATION. */
1024 pfile->line_table->highest_location--;
1026 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1029 /* Arrange the file_change callback. pfile->line has changed to
1030 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1031 header, 2 for a system header that needs to be extern "C" protected,
1032 and zero otherwise. */
1033 void
1034 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1035 const char *to_file, linenum_type file_line,
1036 unsigned int sysp)
1038 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1039 to_file, file_line);
1040 if (map != NULL)
1041 linemap_line_start (pfile->line_table,
1042 ORDINARY_MAP_STARTING_LINE_NUMBER (map),
1043 127);
1045 if (pfile->cb.file_change)
1046 pfile->cb.file_change (pfile, map);
1049 /* Report a warning or error detected by the program we are
1050 processing. Use the directive's tokens in the error message. */
1051 static void
1052 do_diagnostic (cpp_reader *pfile, int code, int reason, int print_dir)
1054 const unsigned char *dir_name;
1055 unsigned char *line;
1056 source_location src_loc = pfile->cur_token[-1].src_loc;
1058 if (print_dir)
1059 dir_name = pfile->directive->name;
1060 else
1061 dir_name = NULL;
1062 pfile->state.prevent_expansion++;
1063 line = cpp_output_line_to_string (pfile, dir_name);
1064 pfile->state.prevent_expansion--;
1066 if (code == CPP_DL_WARNING_SYSHDR && reason)
1067 cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line);
1068 else if (code == CPP_DL_WARNING && reason)
1069 cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line);
1070 else
1071 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1072 free (line);
1075 static void
1076 do_error (cpp_reader *pfile)
1078 do_diagnostic (pfile, CPP_DL_ERROR, 0, 1);
1081 static void
1082 do_warning (cpp_reader *pfile)
1084 /* We want #warning diagnostics to be emitted in system headers too. */
1085 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1);
1088 /* Report program identification. */
1089 static void
1090 do_ident (cpp_reader *pfile)
1092 const cpp_token *str = cpp_get_token (pfile);
1094 if (str->type != CPP_STRING)
1095 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1096 pfile->directive->name);
1097 else if (pfile->cb.ident)
1098 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1100 check_eol (pfile, false);
1103 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1104 matching entry, or NULL if none is found. The returned entry could
1105 be the start of a namespace chain, or a pragma. */
1106 static struct pragma_entry *
1107 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1109 while (chain && chain->pragma != pragma)
1110 chain = chain->next;
1112 return chain;
1115 /* Create and insert a blank pragma entry at the beginning of a
1116 singly-linked CHAIN. */
1117 static struct pragma_entry *
1118 new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1120 struct pragma_entry *new_entry;
1122 new_entry = (struct pragma_entry *)
1123 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1125 memset (new_entry, 0, sizeof (struct pragma_entry));
1126 new_entry->next = *chain;
1128 *chain = new_entry;
1129 return new_entry;
1132 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1133 goes in the global namespace. */
1134 static struct pragma_entry *
1135 register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1136 bool allow_name_expansion)
1138 struct pragma_entry **chain = &pfile->pragmas;
1139 struct pragma_entry *entry;
1140 const cpp_hashnode *node;
1142 if (space)
1144 node = cpp_lookup (pfile, UC space, strlen (space));
1145 entry = lookup_pragma_entry (*chain, node);
1146 if (!entry)
1148 entry = new_pragma_entry (pfile, chain);
1149 entry->pragma = node;
1150 entry->is_nspace = true;
1151 entry->allow_expansion = allow_name_expansion;
1153 else if (!entry->is_nspace)
1154 goto clash;
1155 else if (entry->allow_expansion != allow_name_expansion)
1157 cpp_error (pfile, CPP_DL_ICE,
1158 "registering pragmas in namespace \"%s\" with mismatched "
1159 "name expansion", space);
1160 return NULL;
1162 chain = &entry->u.space;
1164 else if (allow_name_expansion)
1166 cpp_error (pfile, CPP_DL_ICE,
1167 "registering pragma \"%s\" with name expansion "
1168 "and no namespace", name);
1169 return NULL;
1172 /* Check for duplicates. */
1173 node = cpp_lookup (pfile, UC name, strlen (name));
1174 entry = lookup_pragma_entry (*chain, node);
1175 if (entry == NULL)
1177 entry = new_pragma_entry (pfile, chain);
1178 entry->pragma = node;
1179 return entry;
1182 if (entry->is_nspace)
1183 clash:
1184 cpp_error (pfile, CPP_DL_ICE,
1185 "registering \"%s\" as both a pragma and a pragma namespace",
1186 NODE_NAME (node));
1187 else if (space)
1188 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1189 space, name);
1190 else
1191 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1193 return NULL;
1196 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1197 static void
1198 register_pragma_internal (cpp_reader *pfile, const char *space,
1199 const char *name, pragma_cb handler)
1201 struct pragma_entry *entry;
1203 entry = register_pragma_1 (pfile, space, name, false);
1204 entry->is_internal = true;
1205 entry->u.handler = handler;
1208 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1209 goes in the global namespace. HANDLER is the handler it will call,
1210 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1211 expansion while parsing pragma NAME. This function is exported
1212 from libcpp. */
1213 void
1214 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1215 pragma_cb handler, bool allow_expansion)
1217 struct pragma_entry *entry;
1219 if (!handler)
1221 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1222 return;
1225 entry = register_pragma_1 (pfile, space, name, false);
1226 if (entry)
1228 entry->allow_expansion = allow_expansion;
1229 entry->u.handler = handler;
1233 /* Similarly, but create mark the pragma for deferred processing.
1234 When found, a CPP_PRAGMA token will be insertted into the stream
1235 with IDENT in the token->u.pragma slot. */
1236 void
1237 cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1238 const char *name, unsigned int ident,
1239 bool allow_expansion, bool allow_name_expansion)
1241 struct pragma_entry *entry;
1243 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1244 if (entry)
1246 entry->is_deferred = true;
1247 entry->allow_expansion = allow_expansion;
1248 entry->u.ident = ident;
1252 /* Register the pragmas the preprocessor itself handles. */
1253 void
1254 _cpp_init_internal_pragmas (cpp_reader *pfile)
1256 /* Pragmas in the global namespace. */
1257 register_pragma_internal (pfile, 0, "once", do_pragma_once);
1258 register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
1259 register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
1261 /* New GCC-specific pragmas should be put in the GCC namespace. */
1262 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1263 register_pragma_internal (pfile, "GCC", "system_header",
1264 do_pragma_system_header);
1265 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1268 /* Return the number of registered pragmas in PE. */
1270 static int
1271 count_registered_pragmas (struct pragma_entry *pe)
1273 int ct = 0;
1274 for (; pe != NULL; pe = pe->next)
1276 if (pe->is_nspace)
1277 ct += count_registered_pragmas (pe->u.space);
1278 ct++;
1280 return ct;
1283 /* Save into SD the names of the registered pragmas referenced by PE,
1284 and return a pointer to the next free space in SD. */
1286 static char **
1287 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1289 for (; pe != NULL; pe = pe->next)
1291 if (pe->is_nspace)
1292 sd = save_registered_pragmas (pe->u.space, sd);
1293 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1294 HT_LEN (&pe->pragma->ident),
1295 HT_LEN (&pe->pragma->ident) + 1);
1297 return sd;
1300 /* Return a newly-allocated array which saves the names of the
1301 registered pragmas. */
1303 char **
1304 _cpp_save_pragma_names (cpp_reader *pfile)
1306 int ct = count_registered_pragmas (pfile->pragmas);
1307 char **result = XNEWVEC (char *, ct);
1308 (void) save_registered_pragmas (pfile->pragmas, result);
1309 return result;
1312 /* Restore from SD the names of the registered pragmas referenced by PE,
1313 and return a pointer to the next unused name in SD. */
1315 static char **
1316 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1317 char **sd)
1319 for (; pe != NULL; pe = pe->next)
1321 if (pe->is_nspace)
1322 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1323 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
1324 free (*sd);
1325 sd++;
1327 return sd;
1330 /* Restore the names of the registered pragmas from SAVED. */
1332 void
1333 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1335 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1336 free (saved);
1339 /* Pragmata handling. We handle some, and pass the rest on to the
1340 front end. C99 defines three pragmas and says that no macro
1341 expansion is to be performed on them; whether or not macro
1342 expansion happens for other pragmas is implementation defined.
1343 This implementation allows for a mix of both, since GCC did not
1344 traditionally macro expand its (few) pragmas, whereas OpenMP
1345 specifies that macro expansion should happen. */
1346 static void
1347 do_pragma (cpp_reader *pfile)
1349 const struct pragma_entry *p = NULL;
1350 const cpp_token *token, *pragma_token = pfile->cur_token;
1351 cpp_token ns_token;
1352 unsigned int count = 1;
1354 pfile->state.prevent_expansion++;
1356 token = cpp_get_token (pfile);
1357 ns_token = *token;
1358 if (token->type == CPP_NAME)
1360 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
1361 if (p && p->is_nspace)
1363 bool allow_name_expansion = p->allow_expansion;
1364 if (allow_name_expansion)
1366 pfile->state.prevent_expansion--;
1368 Kludge ahead.
1370 Consider this code snippet:
1372 #define P parallel
1373 #pragma omp P for
1374 ... a for loop ...
1376 Once we parsed the 'omp' namespace of the #pragma
1377 directive, we then parse the 'P' token that represents the
1378 pragma name. P being a macro, it is expanded into the
1379 resulting 'parallel' token.
1381 At this point the 'p' variable contains the 'parallel'
1382 pragma name. And pfile->context->macro is non-null
1383 because we are still right at the end of the macro
1384 context of 'P'. The problem is, if we are being
1385 (indirectly) called by cpp_get_token_with_location,
1386 that function might test pfile->context->macro to see
1387 if we are in the context of a macro expansion, (and we
1388 are) and then use pfile->invocation_location as the
1389 location of the macro invocation. So we must instruct
1390 cpp_get_token below to set
1391 pfile->invocation_location. */
1392 pfile->set_invocation_location = true;
1395 token = cpp_get_token (pfile);
1396 if (token->type == CPP_NAME)
1397 p = lookup_pragma_entry (p->u.space, token->val.node.node);
1398 else
1399 p = NULL;
1400 if (allow_name_expansion)
1401 pfile->state.prevent_expansion++;
1402 count = 2;
1406 if (p)
1408 if (p->is_deferred)
1410 pfile->directive_result.src_loc = pragma_token->src_loc;
1411 pfile->directive_result.type = CPP_PRAGMA;
1412 pfile->directive_result.flags = pragma_token->flags;
1413 pfile->directive_result.val.pragma = p->u.ident;
1414 pfile->state.in_deferred_pragma = true;
1415 pfile->state.pragma_allow_expansion = p->allow_expansion;
1416 if (!p->allow_expansion)
1417 pfile->state.prevent_expansion++;
1419 else
1421 /* Since the handler below doesn't get the line number, that
1422 it might need for diagnostics, make sure it has the right
1423 numbers in place. */
1424 if (pfile->cb.line_change)
1425 (*pfile->cb.line_change) (pfile, pragma_token, false);
1426 if (p->allow_expansion)
1427 pfile->state.prevent_expansion--;
1428 (*p->u.handler) (pfile);
1429 if (p->allow_expansion)
1430 pfile->state.prevent_expansion++;
1433 else if (pfile->cb.def_pragma)
1435 if (count == 1 || pfile->context->prev == NULL)
1436 _cpp_backup_tokens (pfile, count);
1437 else
1439 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1440 won't allow backing 2 tokens. */
1441 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1442 reads both tokens, we could perhaps free it, but if it doesn't,
1443 we don't know the exact lifespan. */
1444 cpp_token *toks = XNEWVEC (cpp_token, 2);
1445 toks[0] = ns_token;
1446 toks[0].flags |= NO_EXPAND;
1447 toks[1] = *token;
1448 toks[1].flags |= NO_EXPAND;
1449 _cpp_push_token_context (pfile, NULL, toks, 2);
1451 pfile->cb.def_pragma (pfile, pfile->directive_line);
1454 pfile->state.prevent_expansion--;
1457 /* Handle #pragma once. */
1458 static void
1459 do_pragma_once (cpp_reader *pfile)
1461 if (cpp_in_primary_file (pfile))
1462 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1464 check_eol (pfile, false);
1465 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1468 /* Handle #pragma push_macro(STRING). */
1469 static void
1470 do_pragma_push_macro (cpp_reader *pfile)
1472 cpp_hashnode *node;
1473 size_t defnlen;
1474 const uchar *defn = NULL;
1475 char *macroname, *dest;
1476 const char *limit, *src;
1477 const cpp_token *txt;
1478 struct def_pragma_macro *c;
1480 txt = get__Pragma_string (pfile);
1481 if (!txt)
1483 source_location src_loc = pfile->cur_token[-1].src_loc;
1484 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1485 "invalid #pragma push_macro directive");
1486 check_eol (pfile, false);
1487 skip_rest_of_line (pfile);
1488 return;
1490 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1491 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1492 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1493 while (src < limit)
1495 /* We know there is a character following the backslash. */
1496 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1497 src++;
1498 *dest++ = *src++;
1500 *dest = 0;
1501 check_eol (pfile, false);
1502 skip_rest_of_line (pfile);
1503 c = XNEW (struct def_pragma_macro);
1504 memset (c, 0, sizeof (struct def_pragma_macro));
1505 c->name = XNEWVAR (char, strlen (macroname) + 1);
1506 strcpy (c->name, macroname);
1507 c->next = pfile->pushed_macros;
1508 node = _cpp_lex_identifier (pfile, c->name);
1509 if (node->type == NT_VOID)
1510 c->is_undef = 1;
1511 else
1513 defn = cpp_macro_definition (pfile, node);
1514 defnlen = ustrlen (defn);
1515 c->definition = XNEWVEC (uchar, defnlen + 2);
1516 c->definition[defnlen] = '\n';
1517 c->definition[defnlen + 1] = 0;
1518 c->line = node->value.macro->line;
1519 c->syshdr = node->value.macro->syshdr;
1520 c->used = node->value.macro->used;
1521 memcpy (c->definition, defn, defnlen);
1524 pfile->pushed_macros = c;
1527 /* Handle #pragma pop_macro(STRING). */
1528 static void
1529 do_pragma_pop_macro (cpp_reader *pfile)
1531 char *macroname, *dest;
1532 const char *limit, *src;
1533 const cpp_token *txt;
1534 struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
1535 txt = get__Pragma_string (pfile);
1536 if (!txt)
1538 source_location src_loc = pfile->cur_token[-1].src_loc;
1539 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1540 "invalid #pragma pop_macro directive");
1541 check_eol (pfile, false);
1542 skip_rest_of_line (pfile);
1543 return;
1545 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1546 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1547 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1548 while (src < limit)
1550 /* We know there is a character following the backslash. */
1551 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1552 src++;
1553 *dest++ = *src++;
1555 *dest = 0;
1556 check_eol (pfile, false);
1557 skip_rest_of_line (pfile);
1559 while (c != NULL)
1561 if (!strcmp (c->name, macroname))
1563 if (!l)
1564 pfile->pushed_macros = c->next;
1565 else
1566 l->next = c->next;
1567 cpp_pop_definition (pfile, c);
1568 free (c->definition);
1569 free (c->name);
1570 free (c);
1571 break;
1573 l = c;
1574 c = c->next;
1578 /* Handle #pragma GCC poison, to poison one or more identifiers so
1579 that the lexer produces a hard error for each subsequent usage. */
1580 static void
1581 do_pragma_poison (cpp_reader *pfile)
1583 const cpp_token *tok;
1584 cpp_hashnode *hp;
1586 pfile->state.poisoned_ok = 1;
1587 for (;;)
1589 tok = _cpp_lex_token (pfile);
1590 if (tok->type == CPP_EOF)
1591 break;
1592 if (tok->type != CPP_NAME)
1594 cpp_error (pfile, CPP_DL_ERROR,
1595 "invalid #pragma GCC poison directive");
1596 break;
1599 hp = tok->val.node.node;
1600 if (hp->flags & NODE_POISONED)
1601 continue;
1603 if (hp->type == NT_MACRO)
1604 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1605 NODE_NAME (hp));
1606 _cpp_free_definition (hp);
1607 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1609 pfile->state.poisoned_ok = 0;
1612 /* Mark the current header as a system header. This will suppress
1613 some categories of warnings (notably those from -pedantic). It is
1614 intended for use in system libraries that cannot be implemented in
1615 conforming C, but cannot be certain that their headers appear in a
1616 system include directory. To prevent abuse, it is rejected in the
1617 primary source file. */
1618 static void
1619 do_pragma_system_header (cpp_reader *pfile)
1621 if (cpp_in_primary_file (pfile))
1622 cpp_error (pfile, CPP_DL_WARNING,
1623 "#pragma system_header ignored outside include file");
1624 else
1626 check_eol (pfile, false);
1627 skip_rest_of_line (pfile);
1628 cpp_make_system_header (pfile, 1, 0);
1632 /* Check the modified date of the current include file against a specified
1633 file. Issue a diagnostic, if the specified file is newer. We use this to
1634 determine if a fixed header should be refixed. */
1635 static void
1636 do_pragma_dependency (cpp_reader *pfile)
1638 const char *fname;
1639 int angle_brackets, ordering;
1640 source_location location;
1642 fname = parse_include (pfile, &angle_brackets, NULL, &location);
1643 if (!fname)
1644 return;
1646 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1647 if (ordering < 0)
1648 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1649 else if (ordering > 0)
1651 cpp_error (pfile, CPP_DL_WARNING,
1652 "current file is older than %s", fname);
1653 if (cpp_get_token (pfile)->type != CPP_EOF)
1655 _cpp_backup_tokens (pfile, 1);
1656 do_diagnostic (pfile, CPP_DL_WARNING, 0, 0);
1660 free ((void *) fname);
1663 /* Get a token but skip padding. */
1664 static const cpp_token *
1665 get_token_no_padding (cpp_reader *pfile)
1667 for (;;)
1669 const cpp_token *result = cpp_get_token (pfile);
1670 if (result->type != CPP_PADDING)
1671 return result;
1675 /* Check syntax is "(string-literal)". Returns the string on success,
1676 or NULL on failure. */
1677 static const cpp_token *
1678 get__Pragma_string (cpp_reader *pfile)
1680 const cpp_token *string;
1681 const cpp_token *paren;
1683 paren = get_token_no_padding (pfile);
1684 if (paren->type == CPP_EOF)
1685 _cpp_backup_tokens (pfile, 1);
1686 if (paren->type != CPP_OPEN_PAREN)
1687 return NULL;
1689 string = get_token_no_padding (pfile);
1690 if (string->type == CPP_EOF)
1691 _cpp_backup_tokens (pfile, 1);
1692 if (string->type != CPP_STRING && string->type != CPP_WSTRING
1693 && string->type != CPP_STRING32 && string->type != CPP_STRING16
1694 && string->type != CPP_UTF8STRING)
1695 return NULL;
1697 paren = get_token_no_padding (pfile);
1698 if (paren->type == CPP_EOF)
1699 _cpp_backup_tokens (pfile, 1);
1700 if (paren->type != CPP_CLOSE_PAREN)
1701 return NULL;
1703 return string;
1706 /* Destringize IN into a temporary buffer, by removing the first \ of
1707 \" and \\ sequences, and process the result as a #pragma directive. */
1708 static void
1709 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1711 const unsigned char *src, *limit;
1712 char *dest, *result;
1713 cpp_context *saved_context;
1714 cpp_token *saved_cur_token;
1715 tokenrun *saved_cur_run;
1716 cpp_token *toks;
1717 int count;
1718 const struct directive *save_directive;
1720 dest = result = (char *) alloca (in->len - 1);
1721 src = in->text + 1 + (in->text[0] == 'L');
1722 limit = in->text + in->len - 1;
1723 while (src < limit)
1725 /* We know there is a character following the backslash. */
1726 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1727 src++;
1728 *dest++ = *src++;
1730 *dest = '\n';
1732 /* Ugh; an awful kludge. We are really not set up to be lexing
1733 tokens when in the middle of a macro expansion. Use a new
1734 context to force cpp_get_token to lex, and so skip_rest_of_line
1735 doesn't go beyond the end of the text. Also, remember the
1736 current lexing position so we can return to it later.
1738 Something like line-at-a-time lexing should remove the need for
1739 this. */
1740 saved_context = pfile->context;
1741 saved_cur_token = pfile->cur_token;
1742 saved_cur_run = pfile->cur_run;
1744 pfile->context = XNEW (cpp_context);
1745 pfile->context->c.macro = 0;
1746 pfile->context->prev = 0;
1747 pfile->context->next = 0;
1749 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1750 until we've read all of the tokens that we want. */
1751 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1752 /* from_stage3 */ true);
1753 /* ??? Antique Disgusting Hack. What does this do? */
1754 if (pfile->buffer->prev)
1755 pfile->buffer->file = pfile->buffer->prev->file;
1757 start_directive (pfile);
1758 _cpp_clean_line (pfile);
1759 save_directive = pfile->directive;
1760 pfile->directive = &dtable[T_PRAGMA];
1761 do_pragma (pfile);
1762 end_directive (pfile, 1);
1763 pfile->directive = save_directive;
1765 /* We always insert at least one token, the directive result. It'll
1766 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1767 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1769 /* If we're not handling the pragma internally, read all of the tokens from
1770 the string buffer now, while the string buffer is still installed. */
1771 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1772 to me what the true lifespan of the tokens are. It would appear that
1773 the lifespan is the entire parse of the main input stream, in which case
1774 this may not be wrong. */
1775 if (pfile->directive_result.type == CPP_PRAGMA)
1777 int maxcount;
1779 count = 1;
1780 maxcount = 50;
1781 toks = XNEWVEC (cpp_token, maxcount);
1782 toks[0] = pfile->directive_result;
1786 if (count == maxcount)
1788 maxcount = maxcount * 3 / 2;
1789 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1791 toks[count] = *cpp_get_token (pfile);
1792 /* Macros have been already expanded by cpp_get_token
1793 if the pragma allowed expansion. */
1794 toks[count++].flags |= NO_EXPAND;
1796 while (toks[count-1].type != CPP_PRAGMA_EOL);
1798 else
1800 count = 1;
1801 toks = XNEW (cpp_token);
1802 toks[0] = pfile->directive_result;
1804 /* If we handled the entire pragma internally, make sure we get the
1805 line number correct for the next token. */
1806 if (pfile->cb.line_change)
1807 pfile->cb.line_change (pfile, pfile->cur_token, false);
1810 /* Finish inlining run_directive. */
1811 pfile->buffer->file = NULL;
1812 _cpp_pop_buffer (pfile);
1814 /* Reset the old macro state before ... */
1815 XDELETE (pfile->context);
1816 pfile->context = saved_context;
1817 pfile->cur_token = saved_cur_token;
1818 pfile->cur_run = saved_cur_run;
1820 /* ... inserting the new tokens we collected. */
1821 _cpp_push_token_context (pfile, NULL, toks, count);
1824 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1826 _cpp_do__Pragma (cpp_reader *pfile)
1828 const cpp_token *string = get__Pragma_string (pfile);
1829 pfile->directive_result.type = CPP_PADDING;
1831 if (string)
1833 destringize_and_run (pfile, &string->val.str);
1834 return 1;
1836 cpp_error (pfile, CPP_DL_ERROR,
1837 "_Pragma takes a parenthesized string literal");
1838 return 0;
1841 /* Handle #ifdef. */
1842 static void
1843 do_ifdef (cpp_reader *pfile)
1845 int skip = 1;
1847 if (! pfile->state.skipping)
1849 cpp_hashnode *node = lex_macro_node (pfile, false);
1851 if (node)
1853 /* Do not treat conditional macros as being defined. This is due to
1854 the powerpc and spu ports using conditional macros for 'vector',
1855 'bool', and 'pixel' to act as conditional keywords. This messes
1856 up tests like #ifndef bool. */
1857 skip = (node->type != NT_MACRO
1858 || ((node->flags & NODE_CONDITIONAL) != 0));
1859 _cpp_mark_macro_used (node);
1860 if (!(node->flags & NODE_USED))
1862 node->flags |= NODE_USED;
1863 if (node->type == NT_MACRO)
1865 if ((node->flags & NODE_BUILTIN)
1866 && pfile->cb.user_builtin_macro)
1867 pfile->cb.user_builtin_macro (pfile, node);
1868 if (pfile->cb.used_define)
1869 pfile->cb.used_define (pfile, pfile->directive_line, node);
1871 else
1873 if (pfile->cb.used_undef)
1874 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1877 if (pfile->cb.used)
1878 pfile->cb.used (pfile, pfile->directive_line, node);
1879 check_eol (pfile, false);
1883 push_conditional (pfile, skip, T_IFDEF, 0);
1886 /* Handle #ifndef. */
1887 static void
1888 do_ifndef (cpp_reader *pfile)
1890 int skip = 1;
1891 cpp_hashnode *node = 0;
1893 if (! pfile->state.skipping)
1895 node = lex_macro_node (pfile, false);
1897 if (node)
1899 /* Do not treat conditional macros as being defined. This is due to
1900 the powerpc and spu ports using conditional macros for 'vector',
1901 'bool', and 'pixel' to act as conditional keywords. This messes
1902 up tests like #ifndef bool. */
1903 skip = (node->type == NT_MACRO
1904 && ((node->flags & NODE_CONDITIONAL) == 0));
1905 _cpp_mark_macro_used (node);
1906 if (!(node->flags & NODE_USED))
1908 node->flags |= NODE_USED;
1909 if (node->type == NT_MACRO)
1911 if ((node->flags & NODE_BUILTIN)
1912 && pfile->cb.user_builtin_macro)
1913 pfile->cb.user_builtin_macro (pfile, node);
1914 if (pfile->cb.used_define)
1915 pfile->cb.used_define (pfile, pfile->directive_line, node);
1917 else
1919 if (pfile->cb.used_undef)
1920 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1923 if (pfile->cb.used)
1924 pfile->cb.used (pfile, pfile->directive_line, node);
1925 check_eol (pfile, false);
1929 push_conditional (pfile, skip, T_IFNDEF, node);
1932 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1933 pfile->mi_ind_cmacro so we can handle multiple-include
1934 optimizations. If macro expansion occurs in the expression, we
1935 cannot treat it as a controlling conditional, since the expansion
1936 could change in the future. That is handled by cpp_get_token. */
1937 static void
1938 do_if (cpp_reader *pfile)
1940 int skip = 1;
1942 if (! pfile->state.skipping)
1943 skip = _cpp_parse_expr (pfile, true) == false;
1945 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1948 /* Flip skipping state if appropriate and continue without changing
1949 if_stack; this is so that the error message for missing #endif's
1950 etc. will point to the original #if. */
1951 static void
1952 do_else (cpp_reader *pfile)
1954 cpp_buffer *buffer = pfile->buffer;
1955 struct if_stack *ifs = buffer->if_stack;
1957 if (ifs == NULL)
1958 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1959 else
1961 if (ifs->type == T_ELSE)
1963 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1964 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1965 "the conditional began here");
1967 ifs->type = T_ELSE;
1969 /* Skip any future (erroneous) #elses or #elifs. */
1970 pfile->state.skipping = ifs->skip_elses;
1971 ifs->skip_elses = true;
1973 /* Invalidate any controlling macro. */
1974 ifs->mi_cmacro = 0;
1976 /* Only check EOL if was not originally skipping. */
1977 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1978 check_eol (pfile, false);
1982 /* Handle a #elif directive by not changing if_stack either. See the
1983 comment above do_else. */
1984 static void
1985 do_elif (cpp_reader *pfile)
1987 cpp_buffer *buffer = pfile->buffer;
1988 struct if_stack *ifs = buffer->if_stack;
1990 if (ifs == NULL)
1991 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1992 else
1994 if (ifs->type == T_ELSE)
1996 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1997 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1998 "the conditional began here");
2000 ifs->type = T_ELIF;
2002 if (! ifs->was_skipping)
2004 bool value;
2005 /* The standard mandates that the expression be parsed even
2006 if we are skipping elses at this point -- the lexical
2007 restrictions on #elif only apply to skipped groups, but
2008 this group is not being skipped. Temporarily set
2009 skipping to false to get lexer warnings. */
2010 pfile->state.skipping = 0;
2011 value = _cpp_parse_expr (pfile, false);
2012 if (ifs->skip_elses)
2013 pfile->state.skipping = 1;
2014 else
2016 pfile->state.skipping = ! value;
2017 ifs->skip_elses = value;
2021 /* Invalidate any controlling macro. */
2022 ifs->mi_cmacro = 0;
2026 /* #endif pops the if stack and resets pfile->state.skipping. */
2027 static void
2028 do_endif (cpp_reader *pfile)
2030 cpp_buffer *buffer = pfile->buffer;
2031 struct if_stack *ifs = buffer->if_stack;
2033 if (ifs == NULL)
2034 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
2035 else
2037 /* Only check EOL if was not originally skipping. */
2038 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
2039 check_eol (pfile, false);
2041 /* If potential control macro, we go back outside again. */
2042 if (ifs->next == 0 && ifs->mi_cmacro)
2044 pfile->mi_valid = true;
2045 pfile->mi_cmacro = ifs->mi_cmacro;
2048 buffer->if_stack = ifs->next;
2049 pfile->state.skipping = ifs->was_skipping;
2050 obstack_free (&pfile->buffer_ob, ifs);
2054 /* Push an if_stack entry for a preprocessor conditional, and set
2055 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2056 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2057 we need to check here that we are at the top of the file. */
2058 static void
2059 push_conditional (cpp_reader *pfile, int skip, int type,
2060 const cpp_hashnode *cmacro)
2062 struct if_stack *ifs;
2063 cpp_buffer *buffer = pfile->buffer;
2065 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
2066 ifs->line = pfile->directive_line;
2067 ifs->next = buffer->if_stack;
2068 ifs->skip_elses = pfile->state.skipping || !skip;
2069 ifs->was_skipping = pfile->state.skipping;
2070 ifs->type = type;
2071 /* This condition is effectively a test for top-of-file. */
2072 if (pfile->mi_valid && pfile->mi_cmacro == 0)
2073 ifs->mi_cmacro = cmacro;
2074 else
2075 ifs->mi_cmacro = 0;
2077 pfile->state.skipping = skip;
2078 buffer->if_stack = ifs;
2081 /* Read the tokens of the answer into the macro pool, in a directive
2082 of type TYPE. Only commit the memory if we intend it as permanent
2083 storage, i.e. the #assert case. Returns 0 on success, and sets
2084 ANSWERP to point to the answer. PRED_LOC is the location of the
2085 predicate. */
2086 static int
2087 parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
2088 source_location pred_loc)
2090 const cpp_token *paren;
2091 struct answer *answer;
2092 unsigned int acount;
2094 /* In a conditional, it is legal to not have an open paren. We
2095 should save the following token in this case. */
2096 paren = cpp_get_token (pfile);
2098 /* If not a paren, see if we're OK. */
2099 if (paren->type != CPP_OPEN_PAREN)
2101 /* In a conditional no answer is a test for any answer. It
2102 could be followed by any token. */
2103 if (type == T_IF)
2105 _cpp_backup_tokens (pfile, 1);
2106 return 0;
2109 /* #unassert with no answer is valid - it removes all answers. */
2110 if (type == T_UNASSERT && paren->type == CPP_EOF)
2111 return 0;
2113 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
2114 "missing '(' after predicate");
2115 return 1;
2118 for (acount = 0;; acount++)
2120 size_t room_needed;
2121 const cpp_token *token = cpp_get_token (pfile);
2122 cpp_token *dest;
2124 if (token->type == CPP_CLOSE_PAREN)
2125 break;
2127 if (token->type == CPP_EOF)
2129 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
2130 return 1;
2133 /* struct answer includes the space for one token. */
2134 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
2136 if (BUFF_ROOM (pfile->a_buff) < room_needed)
2137 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
2139 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
2140 *dest = *token;
2142 /* Drop whitespace at start, for answer equivalence purposes. */
2143 if (acount == 0)
2144 dest->flags &= ~PREV_WHITE;
2147 if (acount == 0)
2149 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
2150 return 1;
2153 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
2154 answer->count = acount;
2155 answer->next = NULL;
2156 *answerp = answer;
2158 return 0;
2161 /* Parses an assertion directive of type TYPE, returning a pointer to
2162 the hash node of the predicate, or 0 on error. If an answer was
2163 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
2164 static cpp_hashnode *
2165 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
2167 cpp_hashnode *result = 0;
2168 const cpp_token *predicate;
2170 /* We don't expand predicates or answers. */
2171 pfile->state.prevent_expansion++;
2173 *answerp = 0;
2174 predicate = cpp_get_token (pfile);
2175 if (predicate->type == CPP_EOF)
2176 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
2177 else if (predicate->type != CPP_NAME)
2178 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2179 "predicate must be an identifier");
2180 else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
2182 unsigned int len = NODE_LEN (predicate->val.node.node);
2183 unsigned char *sym = (unsigned char *) alloca (len + 1);
2185 /* Prefix '#' to get it out of macro namespace. */
2186 sym[0] = '#';
2187 memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
2188 result = cpp_lookup (pfile, sym, len + 1);
2191 pfile->state.prevent_expansion--;
2192 return result;
2195 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2196 or a pointer to NULL if the answer is not in the chain. */
2197 static struct answer **
2198 find_answer (cpp_hashnode *node, const struct answer *candidate)
2200 unsigned int i;
2201 struct answer **result;
2203 for (result = &node->value.answers; *result; result = &(*result)->next)
2205 struct answer *answer = *result;
2207 if (answer->count == candidate->count)
2209 for (i = 0; i < answer->count; i++)
2210 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2211 break;
2213 if (i == answer->count)
2214 break;
2218 return result;
2221 /* Test an assertion within a preprocessor conditional. Returns
2222 nonzero on failure, zero on success. On success, the result of
2223 the test is written into VALUE, otherwise the value 0. */
2225 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2227 struct answer *answer;
2228 cpp_hashnode *node;
2230 node = parse_assertion (pfile, &answer, T_IF);
2232 /* For recovery, an erroneous assertion expression is handled as a
2233 failing assertion. */
2234 *value = 0;
2236 if (node)
2237 *value = (node->type == NT_ASSERTION &&
2238 (answer == 0 || *find_answer (node, answer) != 0));
2239 else if (pfile->cur_token[-1].type == CPP_EOF)
2240 _cpp_backup_tokens (pfile, 1);
2242 /* We don't commit the memory for the answer - it's temporary only. */
2243 return node == 0;
2246 /* Handle #assert. */
2247 static void
2248 do_assert (cpp_reader *pfile)
2250 struct answer *new_answer;
2251 cpp_hashnode *node;
2253 node = parse_assertion (pfile, &new_answer, T_ASSERT);
2254 if (node)
2256 size_t answer_size;
2258 /* Place the new answer in the answer list. First check there
2259 is not a duplicate. */
2260 new_answer->next = 0;
2261 if (node->type == NT_ASSERTION)
2263 if (*find_answer (node, new_answer))
2265 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2266 NODE_NAME (node) + 1);
2267 return;
2269 new_answer->next = node->value.answers;
2272 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2273 * sizeof (cpp_token));
2274 /* Commit or allocate storage for the object. */
2275 if (pfile->hash_table->alloc_subobject)
2277 struct answer *temp_answer = new_answer;
2278 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2279 (answer_size);
2280 memcpy (new_answer, temp_answer, answer_size);
2282 else
2283 BUFF_FRONT (pfile->a_buff) += answer_size;
2285 node->type = NT_ASSERTION;
2286 node->value.answers = new_answer;
2287 check_eol (pfile, false);
2291 /* Handle #unassert. */
2292 static void
2293 do_unassert (cpp_reader *pfile)
2295 cpp_hashnode *node;
2296 struct answer *answer;
2298 node = parse_assertion (pfile, &answer, T_UNASSERT);
2299 /* It isn't an error to #unassert something that isn't asserted. */
2300 if (node && node->type == NT_ASSERTION)
2302 if (answer)
2304 struct answer **p = find_answer (node, answer), *temp;
2306 /* Remove the answer from the list. */
2307 temp = *p;
2308 if (temp)
2309 *p = temp->next;
2311 /* Did we free the last answer? */
2312 if (node->value.answers == 0)
2313 node->type = NT_VOID;
2315 check_eol (pfile, false);
2317 else
2318 _cpp_free_definition (node);
2321 /* We don't commit the memory for the answer - it's temporary only. */
2324 /* These are for -D, -U, -A. */
2326 /* Process the string STR as if it appeared as the body of a #define.
2327 If STR is just an identifier, define it with value 1.
2328 If STR has anything after the identifier, then it should
2329 be identifier=definition. */
2330 void
2331 cpp_define (cpp_reader *pfile, const char *str)
2333 char *buf;
2334 const char *p;
2335 size_t count;
2337 /* Copy the entire option so we can modify it.
2338 Change the first "=" in the string to a space. If there is none,
2339 tack " 1" on the end. */
2341 count = strlen (str);
2342 buf = (char *) alloca (count + 3);
2343 memcpy (buf, str, count);
2345 p = strchr (str, '=');
2346 if (p)
2347 buf[p - str] = ' ';
2348 else
2350 buf[count++] = ' ';
2351 buf[count++] = '1';
2353 buf[count] = '\n';
2355 run_directive (pfile, T_DEFINE, buf, count);
2359 /* Use to build macros to be run through cpp_define() as
2360 described above.
2361 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2363 void
2364 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2366 char *ptr = NULL;
2368 va_list ap;
2369 va_start (ap, fmt);
2370 vasprintf (&ptr, fmt, ap);
2371 va_end (ap);
2373 cpp_define (pfile, ptr);
2374 free (ptr);
2378 /* Slight variant of the above for use by initialize_builtins. */
2379 void
2380 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2382 size_t len = strlen (str);
2383 char *buf = (char *) alloca (len + 1);
2384 memcpy (buf, str, len);
2385 buf[len] = '\n';
2386 run_directive (pfile, T_DEFINE, buf, len);
2389 /* Process MACRO as if it appeared as the body of an #undef. */
2390 void
2391 cpp_undef (cpp_reader *pfile, const char *macro)
2393 size_t len = strlen (macro);
2394 char *buf = (char *) alloca (len + 1);
2395 memcpy (buf, macro, len);
2396 buf[len] = '\n';
2397 run_directive (pfile, T_UNDEF, buf, len);
2400 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2401 or first element is zero, then the macro should be undefined. */
2402 static void
2403 cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
2405 cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
2406 if (node == NULL)
2407 return;
2409 if (pfile->cb.before_define)
2410 pfile->cb.before_define (pfile);
2412 if (node->type == NT_MACRO)
2414 if (pfile->cb.undef)
2415 pfile->cb.undef (pfile, pfile->directive_line, node);
2416 if (CPP_OPTION (pfile, warn_unused_macros))
2417 _cpp_warn_if_unused_macro (pfile, node, NULL);
2419 if (node->type != NT_VOID)
2420 _cpp_free_definition (node);
2422 if (c->is_undef)
2423 return;
2425 size_t namelen;
2426 const uchar *dn;
2427 cpp_hashnode *h = NULL;
2428 cpp_buffer *nbuf;
2430 namelen = ustrcspn (c->definition, "( \n");
2431 h = cpp_lookup (pfile, c->definition, namelen);
2432 dn = c->definition + namelen;
2434 h->type = NT_VOID;
2435 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
2436 nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true);
2437 if (nbuf != NULL)
2439 _cpp_clean_line (pfile);
2440 nbuf->sysp = 1;
2441 if (!_cpp_create_definition (pfile, h))
2442 abort ();
2443 _cpp_pop_buffer (pfile);
2445 else
2446 abort ();
2447 h->value.macro->line = c->line;
2448 h->value.macro->syshdr = c->syshdr;
2449 h->value.macro->used = c->used;
2453 /* Process the string STR as if it appeared as the body of a #assert. */
2454 void
2455 cpp_assert (cpp_reader *pfile, const char *str)
2457 handle_assertion (pfile, str, T_ASSERT);
2460 /* Process STR as if it appeared as the body of an #unassert. */
2461 void
2462 cpp_unassert (cpp_reader *pfile, const char *str)
2464 handle_assertion (pfile, str, T_UNASSERT);
2467 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2468 static void
2469 handle_assertion (cpp_reader *pfile, const char *str, int type)
2471 size_t count = strlen (str);
2472 const char *p = strchr (str, '=');
2474 /* Copy the entire option so we can modify it. Change the first
2475 "=" in the string to a '(', and tack a ')' on the end. */
2476 char *buf = (char *) alloca (count + 2);
2478 memcpy (buf, str, count);
2479 if (p)
2481 buf[p - str] = '(';
2482 buf[count++] = ')';
2484 buf[count] = '\n';
2485 str = buf;
2487 run_directive (pfile, type, str, count);
2490 /* The options structure. */
2491 cpp_options *
2492 cpp_get_options (cpp_reader *pfile)
2494 return &pfile->opts;
2497 /* The callbacks structure. */
2498 cpp_callbacks *
2499 cpp_get_callbacks (cpp_reader *pfile)
2501 return &pfile->cb;
2504 /* Copy the given callbacks structure to our own. */
2505 void
2506 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2508 pfile->cb = *cb;
2511 /* The dependencies structure. (Creates one if it hasn't already been.) */
2512 struct deps *
2513 cpp_get_deps (cpp_reader *pfile)
2515 if (!pfile->deps)
2516 pfile->deps = deps_init ();
2517 return pfile->deps;
2520 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2521 doesn't fail. It does not generate a file change call back; that
2522 is the responsibility of the caller. */
2523 cpp_buffer *
2524 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2525 int from_stage3)
2527 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2529 /* Clears, amongst other things, if_stack and mi_cmacro. */
2530 memset (new_buffer, 0, sizeof (cpp_buffer));
2532 new_buffer->next_line = new_buffer->buf = buffer;
2533 new_buffer->rlimit = buffer + len;
2534 new_buffer->from_stage3 = from_stage3;
2535 new_buffer->prev = pfile->buffer;
2536 new_buffer->need_line = true;
2538 pfile->buffer = new_buffer;
2540 return new_buffer;
2543 /* Pops a single buffer, with a file change call-back if appropriate.
2544 Then pushes the next -include file, if any remain. */
2545 void
2546 _cpp_pop_buffer (cpp_reader *pfile)
2548 cpp_buffer *buffer = pfile->buffer;
2549 struct _cpp_file *inc = buffer->file;
2550 struct if_stack *ifs;
2552 /* Walk back up the conditional stack till we reach its level at
2553 entry to this file, issuing error messages. */
2554 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2555 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2556 "unterminated #%s", dtable[ifs->type].name);
2558 /* In case of a missing #endif. */
2559 pfile->state.skipping = 0;
2561 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2562 pfile->buffer = buffer->prev;
2564 free (buffer->notes);
2566 /* Free the buffer object now; we may want to push a new buffer
2567 in _cpp_push_next_include_file. */
2568 obstack_free (&pfile->buffer_ob, buffer);
2570 if (inc)
2572 _cpp_pop_file_buffer (pfile, inc);
2574 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2578 /* Enter all recognized directives in the hash table. */
2579 void
2580 _cpp_init_directives (cpp_reader *pfile)
2582 unsigned int i;
2583 cpp_hashnode *node;
2585 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2587 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2588 node->is_directive = 1;
2589 node->directive_index = i;