Daily bump.
[official-gcc.git] / libcpp / directives.c
blob22a034dd234f45854f8ec356835916c5498f2873
1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986-2014 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "internal.h"
25 #include "mkdeps.h"
26 #include "obstack.h"
28 /* Stack of conditionals currently in progress
29 (including both successful and failing conditionals). */
30 struct if_stack
32 struct if_stack *next;
33 source_location line; /* Line where condition started. */
34 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
35 bool skip_elses; /* Can future #else / #elif be skipped? */
36 bool was_skipping; /* If were skipping on entry. */
37 int type; /* Most recent conditional for diagnostics. */
40 /* Contains a registered pragma or pragma namespace. */
41 typedef void (*pragma_cb) (cpp_reader *);
42 struct pragma_entry
44 struct pragma_entry *next;
45 const cpp_hashnode *pragma; /* Name and length. */
46 bool is_nspace;
47 bool is_internal;
48 bool is_deferred;
49 bool allow_expansion;
50 union {
51 pragma_cb handler;
52 struct pragma_entry *space;
53 unsigned int ident;
54 } u;
57 /* Values for the origin field of struct directive. KANDR directives
58 come from traditional (K&R) C. STDC89 directives come from the
59 1989 C standard. EXTENSION directives are extensions. */
60 #define KANDR 0
61 #define STDC89 1
62 #define EXTENSION 2
64 /* Values for the flags field of struct directive. COND indicates a
65 conditional; IF_COND an opening conditional. INCL means to treat
66 "..." and <...> as q-char and h-char sequences respectively. IN_I
67 means this directive should be handled even if -fpreprocessed is in
68 effect (these are the directives with callback hooks).
70 EXPAND is set on directives that are always macro-expanded. */
71 #define COND (1 << 0)
72 #define IF_COND (1 << 1)
73 #define INCL (1 << 2)
74 #define IN_I (1 << 3)
75 #define EXPAND (1 << 4)
76 #define DEPRECATED (1 << 5)
78 /* Defines one #-directive, including how to handle it. */
79 typedef void (*directive_handler) (cpp_reader *);
80 typedef struct directive directive;
81 struct directive
83 directive_handler handler; /* Function to handle directive. */
84 const uchar *name; /* Name of directive. */
85 unsigned short length; /* Length of name. */
86 unsigned char origin; /* Origin of directive. */
87 unsigned char flags; /* Flags describing this directive. */
90 /* Forward declarations. */
92 static void skip_rest_of_line (cpp_reader *);
93 static void check_eol (cpp_reader *, bool);
94 static void start_directive (cpp_reader *);
95 static void prepare_directive_trad (cpp_reader *);
96 static void end_directive (cpp_reader *, int);
97 static void directive_diagnostics (cpp_reader *, const directive *, int);
98 static void run_directive (cpp_reader *, int, const char *, size_t);
99 static char *glue_header_name (cpp_reader *);
100 static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
101 source_location *);
102 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
103 static unsigned int read_flag (cpp_reader *, unsigned int);
104 static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
105 static void do_diagnostic (cpp_reader *, int, int, int);
106 static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
107 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
108 static void do_include_common (cpp_reader *, enum include_type);
109 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
110 const cpp_hashnode *);
111 static int count_registered_pragmas (struct pragma_entry *);
112 static char ** save_registered_pragmas (struct pragma_entry *, char **);
113 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
114 char **);
115 static void do_pragma_once (cpp_reader *);
116 static void do_pragma_poison (cpp_reader *);
117 static void do_pragma_system_header (cpp_reader *);
118 static void do_pragma_dependency (cpp_reader *);
119 static void do_pragma_warning_or_error (cpp_reader *, bool error);
120 static void do_pragma_warning (cpp_reader *);
121 static void do_pragma_error (cpp_reader *);
122 static void do_linemarker (cpp_reader *);
123 static const cpp_token *get_token_no_padding (cpp_reader *);
124 static const cpp_token *get__Pragma_string (cpp_reader *);
125 static void destringize_and_run (cpp_reader *, const cpp_string *);
126 static int parse_answer (cpp_reader *, struct answer **, int, source_location);
127 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
128 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
129 static void handle_assertion (cpp_reader *, const char *, int);
130 static void do_pragma_push_macro (cpp_reader *);
131 static void do_pragma_pop_macro (cpp_reader *);
132 static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *);
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, #include_next,
140 and #import are deprecated. The name is where the extension
141 appears to have 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, DEPRECATED) /* 0 SVR4 */ \
161 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
162 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
164 /* #sccs is synonymous with #ident. */
165 #define do_sccs do_ident
167 /* Use the table to generate a series of prototypes, an enum for the
168 directive names, and an array of directive handlers. */
170 #define D(name, t, o, f) static void do_##name (cpp_reader *);
171 DIRECTIVE_TABLE
172 #undef D
174 #define D(n, tag, o, f) tag,
175 enum
177 DIRECTIVE_TABLE
178 N_DIRECTIVES
180 #undef D
182 #define D(name, t, origin, flags) \
183 { do_##name, (const uchar *) #name, \
184 sizeof #name - 1, origin, flags },
185 static const directive dtable[] =
187 DIRECTIVE_TABLE
189 #undef D
190 #undef DIRECTIVE_TABLE
192 /* Wrapper struct directive for linemarkers.
193 The origin is more or less true - the original K+R cpp
194 did use this notation in its preprocessed output. */
195 static const directive linemarker_dir =
197 do_linemarker, UC"#", 1, KANDR, IN_I
200 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
202 /* Skip any remaining tokens in a directive. */
203 static void
204 skip_rest_of_line (cpp_reader *pfile)
206 /* Discard all stacked contexts. */
207 while (pfile->context->prev)
208 _cpp_pop_context (pfile);
210 /* Sweep up all tokens remaining on the line. */
211 if (! SEEN_EOL ())
212 while (_cpp_lex_token (pfile)->type != CPP_EOF)
216 /* Ensure there are no stray tokens at the end of a directive. If
217 EXPAND is true, tokens macro-expanding to nothing are allowed. */
218 static void
219 check_eol (cpp_reader *pfile, bool expand)
221 if (! SEEN_EOL () && (expand
222 ? cpp_get_token (pfile)
223 : _cpp_lex_token (pfile))->type != CPP_EOF)
224 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
225 pfile->directive->name);
228 /* Ensure there are no stray tokens other than comments at the end of
229 a directive, and gather the comments. */
230 static const cpp_token **
231 check_eol_return_comments (cpp_reader *pfile)
233 size_t c;
234 size_t capacity = 8;
235 const cpp_token **buf;
237 buf = XNEWVEC (const cpp_token *, capacity);
238 c = 0;
239 if (! SEEN_EOL ())
241 while (1)
243 const cpp_token *tok;
245 tok = _cpp_lex_token (pfile);
246 if (tok->type == CPP_EOF)
247 break;
248 if (tok->type != CPP_COMMENT)
249 cpp_error (pfile, CPP_DL_PEDWARN,
250 "extra tokens at end of #%s directive",
251 pfile->directive->name);
252 else
254 if (c + 1 >= capacity)
256 capacity *= 2;
257 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
259 buf[c] = tok;
260 ++c;
264 buf[c] = NULL;
265 return buf;
268 /* Called when entering a directive, _Pragma or command-line directive. */
269 static void
270 start_directive (cpp_reader *pfile)
272 /* Setup in-directive state. */
273 pfile->state.in_directive = 1;
274 pfile->state.save_comments = 0;
275 pfile->directive_result.type = CPP_PADDING;
277 /* Some handlers need the position of the # for diagnostics. */
278 pfile->directive_line = pfile->line_table->highest_line;
281 /* Called when leaving a directive, _Pragma or command-line directive. */
282 static void
283 end_directive (cpp_reader *pfile, int skip_line)
285 if (CPP_OPTION (pfile, traditional))
287 /* Revert change of prepare_directive_trad. */
288 if (!pfile->state.in_deferred_pragma)
289 pfile->state.prevent_expansion--;
291 if (pfile->directive != &dtable[T_DEFINE])
292 _cpp_remove_overlay (pfile);
294 else if (pfile->state.in_deferred_pragma)
296 /* We don't skip for an assembler #. */
297 else if (skip_line)
299 skip_rest_of_line (pfile);
300 if (!pfile->keep_tokens)
302 pfile->cur_run = &pfile->base_run;
303 pfile->cur_token = pfile->base_run.base;
307 /* Restore state. */
308 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
309 pfile->state.in_directive = 0;
310 pfile->state.in_expression = 0;
311 pfile->state.angled_headers = 0;
312 pfile->directive = 0;
315 /* Prepare to handle the directive in pfile->directive. */
316 static void
317 prepare_directive_trad (cpp_reader *pfile)
319 if (pfile->directive != &dtable[T_DEFINE])
321 bool no_expand = (pfile->directive
322 && ! (pfile->directive->flags & EXPAND));
323 bool was_skipping = pfile->state.skipping;
325 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
326 || pfile->directive == &dtable[T_ELIF]);
327 if (pfile->state.in_expression)
328 pfile->state.skipping = false;
330 if (no_expand)
331 pfile->state.prevent_expansion++;
332 _cpp_scan_out_logical_line (pfile, NULL);
333 if (no_expand)
334 pfile->state.prevent_expansion--;
336 pfile->state.skipping = was_skipping;
337 _cpp_overlay_buffer (pfile, pfile->out.base,
338 pfile->out.cur - pfile->out.base);
341 /* Stop ISO C from expanding anything. */
342 pfile->state.prevent_expansion++;
345 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
346 the '#' was indented. */
347 static void
348 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
350 /* Issue -pedantic or deprecated warnings for extensions. We let
351 -pedantic take precedence if both are applicable. */
352 if (! pfile->state.skipping)
354 if (dir->origin == EXTENSION
355 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
356 && CPP_PEDANTIC (pfile))
357 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
358 else if (((dir->flags & DEPRECATED) != 0
359 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
360 && CPP_OPTION (pfile, cpp_warn_deprecated))
361 cpp_warning (pfile, CPP_W_DEPRECATED,
362 "#%s is a deprecated GCC extension", dir->name);
365 /* Traditionally, a directive is ignored unless its # is in
366 column 1. Therefore in code intended to work with K+R
367 compilers, directives added by C89 must have their #
368 indented, and directives present in traditional C must not.
369 This is true even of directives in skipped conditional
370 blocks. #elif cannot be used at all. */
371 if (CPP_WTRADITIONAL (pfile))
373 if (dir == &dtable[T_ELIF])
374 cpp_warning (pfile, CPP_W_TRADITIONAL,
375 "suggest not using #elif in traditional C");
376 else if (indented && dir->origin == KANDR)
377 cpp_warning (pfile, CPP_W_TRADITIONAL,
378 "traditional C ignores #%s with the # indented",
379 dir->name);
380 else if (!indented && dir->origin != KANDR)
381 cpp_warning (pfile, CPP_W_TRADITIONAL,
382 "suggest hiding #%s from traditional C with an indented #",
383 dir->name);
387 /* Check if we have a known directive. INDENTED is nonzero if the
388 '#' of the directive was indented. This function is in this file
389 to save unnecessarily exporting dtable etc. to lex.c. Returns
390 nonzero if the line of tokens has been handled, zero if we should
391 continue processing the line. */
393 _cpp_handle_directive (cpp_reader *pfile, int indented)
395 const directive *dir = 0;
396 const cpp_token *dname;
397 bool was_parsing_args = pfile->state.parsing_args;
398 bool was_discarding_output = pfile->state.discarding_output;
399 int skip = 1;
401 if (was_discarding_output)
402 pfile->state.prevent_expansion = 0;
404 if (was_parsing_args)
406 if (CPP_OPTION (pfile, cpp_pedantic))
407 cpp_error (pfile, CPP_DL_PEDWARN,
408 "embedding a directive within macro arguments is not portable");
409 pfile->state.parsing_args = 0;
410 pfile->state.prevent_expansion = 0;
412 start_directive (pfile);
413 dname = _cpp_lex_token (pfile);
415 if (dname->type == CPP_NAME)
417 if (dname->val.node.node->is_directive)
418 dir = &dtable[dname->val.node.node->directive_index];
420 /* We do not recognize the # followed by a number extension in
421 assembler code. */
422 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
424 dir = &linemarker_dir;
425 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
426 && ! pfile->state.skipping)
427 cpp_error (pfile, CPP_DL_PEDWARN,
428 "style of line directive is a GCC extension");
431 if (dir)
433 /* If we have a directive that is not an opening conditional,
434 invalidate any control macro. */
435 if (! (dir->flags & IF_COND))
436 pfile->mi_valid = false;
438 /* Kluge alert. In order to be sure that code like this
440 #define HASH #
441 HASH define foo bar
443 does not cause '#define foo bar' to get executed when
444 compiled with -save-temps, we recognize directives in
445 -fpreprocessed mode only if the # is in column 1. macro.c
446 puts a space in front of any '#' at the start of a macro.
448 We exclude the -fdirectives-only case because macro expansion
449 has not been performed yet, and block comments can cause spaces
450 to precede the directive. */
451 if (CPP_OPTION (pfile, preprocessed)
452 && !CPP_OPTION (pfile, directives_only)
453 && (indented || !(dir->flags & IN_I)))
455 skip = 0;
456 dir = 0;
458 else
460 /* In failed conditional groups, all non-conditional
461 directives are ignored. Before doing that, whether
462 skipping or not, we should lex angle-bracketed headers
463 correctly, and maybe output some diagnostics. */
464 pfile->state.angled_headers = dir->flags & INCL;
465 pfile->state.directive_wants_padding = dir->flags & INCL;
466 if (! CPP_OPTION (pfile, preprocessed))
467 directive_diagnostics (pfile, dir, indented);
468 if (pfile->state.skipping && !(dir->flags & COND))
469 dir = 0;
472 else if (dname->type == CPP_EOF)
473 ; /* CPP_EOF is the "null directive". */
474 else
476 /* An unknown directive. Don't complain about it in assembly
477 source: we don't know where the comments are, and # may
478 introduce assembler pseudo-ops. Don't complain about invalid
479 directives in skipped conditional groups (6.10 p4). */
480 if (CPP_OPTION (pfile, lang) == CLK_ASM)
481 skip = 0;
482 else if (!pfile->state.skipping)
483 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
484 cpp_token_as_text (pfile, dname));
487 pfile->directive = dir;
488 if (CPP_OPTION (pfile, traditional))
489 prepare_directive_trad (pfile);
491 if (dir)
492 pfile->directive->handler (pfile);
493 else if (skip == 0)
494 _cpp_backup_tokens (pfile, 1);
496 end_directive (pfile, skip);
497 if (was_parsing_args && !pfile->state.in_deferred_pragma)
499 /* Restore state when within macro args. */
500 pfile->state.parsing_args = 2;
501 pfile->state.prevent_expansion = 1;
503 if (was_discarding_output)
504 pfile->state.prevent_expansion = 1;
505 return skip;
508 /* Directive handler wrapper used by the command line option
509 processor. BUF is \n terminated. */
510 static void
511 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
513 cpp_push_buffer (pfile, (const uchar *) buf, count,
514 /* from_stage3 */ true);
515 start_directive (pfile);
517 /* This is a short-term fix to prevent a leading '#' being
518 interpreted as a directive. */
519 _cpp_clean_line (pfile);
521 pfile->directive = &dtable[dir_no];
522 if (CPP_OPTION (pfile, traditional))
523 prepare_directive_trad (pfile);
524 pfile->directive->handler (pfile);
525 end_directive (pfile, 1);
526 _cpp_pop_buffer (pfile);
529 /* Checks for validity the macro name in #define, #undef, #ifdef and
530 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
531 processing a #define or #undefine directive, and false
532 otherwise. */
533 static cpp_hashnode *
534 lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
536 const cpp_token *token = _cpp_lex_token (pfile);
538 /* The token immediately after #define must be an identifier. That
539 identifier may not be "defined", per C99 6.10.8p4.
540 In C++, it may not be any of the "named operators" either,
541 per C++98 [lex.digraph], [lex.key].
542 Finally, the identifier may not have been poisoned. (In that case
543 the lexer has issued the error message for us.) */
545 if (token->type == CPP_NAME)
547 cpp_hashnode *node = token->val.node.node;
549 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
550 cpp_error (pfile, CPP_DL_ERROR,
551 "\"defined\" cannot be used as a macro name");
552 else if (is_def_or_undef
553 && (node == pfile->spec_nodes.n__has_include__
554 || node == pfile->spec_nodes.n__has_include_next__))
555 cpp_error (pfile, CPP_DL_ERROR,
556 "\"__has_include__\" cannot be used as a macro name");
557 else if (! (node->flags & NODE_POISONED))
558 return node;
560 else if (token->flags & NAMED_OP)
561 cpp_error (pfile, CPP_DL_ERROR,
562 "\"%s\" cannot be used as a macro name as it is an operator in C++",
563 NODE_NAME (token->val.node.node));
564 else if (token->type == CPP_EOF)
565 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
566 pfile->directive->name);
567 else
568 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
570 return NULL;
573 /* Process a #define directive. Most work is done in macro.c. */
574 static void
575 do_define (cpp_reader *pfile)
577 cpp_hashnode *node = lex_macro_node (pfile, true);
579 if (node)
581 /* If we have been requested to expand comments into macros,
582 then re-enable saving of comments. */
583 pfile->state.save_comments =
584 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
586 if (pfile->cb.before_define)
587 pfile->cb.before_define (pfile);
589 if (_cpp_create_definition (pfile, node))
590 if (pfile->cb.define)
591 pfile->cb.define (pfile, pfile->directive_line, node);
593 node->flags &= ~NODE_USED;
597 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
598 static void
599 do_undef (cpp_reader *pfile)
601 cpp_hashnode *node = lex_macro_node (pfile, true);
603 if (node)
605 if (pfile->cb.before_define)
606 pfile->cb.before_define (pfile);
608 if (pfile->cb.undef)
609 pfile->cb.undef (pfile, pfile->directive_line, node);
611 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
612 identifier is not currently defined as a macro name. */
613 if (node->type == NT_MACRO)
615 if (node->flags & NODE_WARN)
616 cpp_error (pfile, CPP_DL_WARNING,
617 "undefining \"%s\"", NODE_NAME (node));
619 if (CPP_OPTION (pfile, warn_unused_macros))
620 _cpp_warn_if_unused_macro (pfile, node, NULL);
622 _cpp_free_definition (node);
626 check_eol (pfile, false);
629 /* Undefine a single macro/assertion/whatever. */
631 static int
632 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
633 void *data_p ATTRIBUTE_UNUSED)
635 /* Body of _cpp_free_definition inlined here for speed.
636 Macros and assertions no longer have anything to free. */
637 h->type = NT_VOID;
638 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
639 return 1;
642 /* Undefine all macros and assertions. */
644 void
645 cpp_undef_all (cpp_reader *pfile)
647 cpp_forall_identifiers (pfile, undefine_macros, NULL);
651 /* Helper routine used by parse_include. Reinterpret the current line
652 as an h-char-sequence (< ... >); we are looking at the first token
653 after the <. Returns a malloced filename. */
654 static char *
655 glue_header_name (cpp_reader *pfile)
657 const cpp_token *token;
658 char *buffer;
659 size_t len, total_len = 0, capacity = 1024;
661 /* To avoid lexed tokens overwriting our glued name, we can only
662 allocate from the string pool once we've lexed everything. */
663 buffer = XNEWVEC (char, capacity);
664 for (;;)
666 token = get_token_no_padding (pfile);
668 if (token->type == CPP_GREATER)
669 break;
670 if (token->type == CPP_EOF)
672 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
673 break;
676 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
677 if (total_len + len > capacity)
679 capacity = (capacity + len) * 2;
680 buffer = XRESIZEVEC (char, buffer, capacity);
683 if (token->flags & PREV_WHITE)
684 buffer[total_len++] = ' ';
686 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
687 true)
688 - (uchar *) buffer);
691 buffer[total_len] = '\0';
692 return buffer;
695 /* Returns the file name of #include, #include_next, #import and
696 #pragma dependency. The string is malloced and the caller should
697 free it. Returns NULL on error. LOCATION is the source location
698 of the file name. */
700 static const char *
701 parse_include (cpp_reader *pfile, int *pangle_brackets,
702 const cpp_token ***buf, source_location *location)
704 char *fname;
705 const cpp_token *header;
707 /* Allow macro expansion. */
708 header = get_token_no_padding (pfile);
709 *location = header->src_loc;
710 if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
711 || header->type == CPP_HEADER_NAME)
713 fname = XNEWVEC (char, header->val.str.len - 1);
714 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
715 fname[header->val.str.len - 2] = '\0';
716 *pangle_brackets = header->type == CPP_HEADER_NAME;
718 else if (header->type == CPP_LESS)
720 fname = glue_header_name (pfile);
721 *pangle_brackets = 1;
723 else
725 const unsigned char *dir;
727 if (pfile->directive == &dtable[T_PRAGMA])
728 dir = UC"pragma dependency";
729 else
730 dir = pfile->directive->name;
731 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
732 dir);
734 return NULL;
737 if (pfile->directive == &dtable[T_PRAGMA])
739 /* This pragma allows extra tokens after the file name. */
741 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
742 check_eol (pfile, true);
743 else
745 /* If we are not discarding comments, then gather them while
746 doing the eol check. */
747 *buf = check_eol_return_comments (pfile);
750 return fname;
753 /* Handle #include, #include_next and #import. */
754 static void
755 do_include_common (cpp_reader *pfile, enum include_type type)
757 const char *fname;
758 int angle_brackets;
759 const cpp_token **buf = NULL;
760 source_location location;
762 /* Re-enable saving of comments if requested, so that the include
763 callback can dump comments which follow #include. */
764 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
766 fname = parse_include (pfile, &angle_brackets, &buf, &location);
767 if (!fname)
769 if (buf)
770 XDELETEVEC (buf);
771 return;
774 if (!*fname)
776 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
777 "empty filename in #%s",
778 pfile->directive->name);
779 XDELETEVEC (fname);
780 if (buf)
781 XDELETEVEC (buf);
782 return;
785 /* Prevent #include recursion. */
786 if (pfile->line_table->depth >= CPP_STACK_MAX)
787 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
788 else
790 /* Get out of macro context, if we are. */
791 skip_rest_of_line (pfile);
793 if (pfile->cb.include)
794 pfile->cb.include (pfile, pfile->directive_line,
795 pfile->directive->name, fname, angle_brackets,
796 buf);
798 _cpp_stack_include (pfile, fname, angle_brackets, type);
801 XDELETEVEC (fname);
802 if (buf)
803 XDELETEVEC (buf);
806 static void
807 do_include (cpp_reader *pfile)
809 do_include_common (pfile, IT_INCLUDE);
812 static void
813 do_import (cpp_reader *pfile)
815 do_include_common (pfile, IT_IMPORT);
818 static void
819 do_include_next (cpp_reader *pfile)
821 enum include_type type = IT_INCLUDE_NEXT;
823 /* If this is the primary source file, warn and use the normal
824 search logic. */
825 if (cpp_in_primary_file (pfile))
827 cpp_error (pfile, CPP_DL_WARNING,
828 "#include_next in primary source file");
829 type = IT_INCLUDE;
831 do_include_common (pfile, type);
834 /* Subroutine of do_linemarker. Read possible flags after file name.
835 LAST is the last flag seen; 0 if this is the first flag. Return the
836 flag if it is valid, 0 at the end of the directive. Otherwise
837 complain. */
838 static unsigned int
839 read_flag (cpp_reader *pfile, unsigned int last)
841 const cpp_token *token = _cpp_lex_token (pfile);
843 if (token->type == CPP_NUMBER && token->val.str.len == 1)
845 unsigned int flag = token->val.str.text[0] - '0';
847 if (flag > last && flag <= 4
848 && (flag != 4 || last == 3)
849 && (flag != 2 || last == 0))
850 return flag;
853 if (token->type != CPP_EOF)
854 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
855 cpp_token_as_text (pfile, token));
856 return 0;
859 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
860 of length LEN, to binary; store it in NUMP, and return false if the
861 number was well-formed, true if not. WRAPPED is set to true if the
862 number did not fit into 'unsigned long'. */
863 static bool
864 strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
866 linenum_type reg = 0;
867 linenum_type reg_prev = 0;
869 uchar c;
870 *wrapped = false;
871 while (len--)
873 c = *str++;
874 if (!ISDIGIT (c))
875 return true;
876 reg *= 10;
877 reg += c - '0';
878 if (reg < reg_prev)
879 *wrapped = true;
880 reg_prev = reg;
882 *nump = reg;
883 return false;
886 /* Interpret #line command.
887 Note that the filename string (if any) is a true string constant
888 (escapes are interpreted), unlike in #line. */
889 static void
890 do_line (cpp_reader *pfile)
892 const struct line_maps *line_table = pfile->line_table;
893 const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
895 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
896 sysp right now. */
898 unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
899 const cpp_token *token;
900 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
901 linenum_type new_lineno;
903 /* C99 raised the minimum limit on #line numbers. */
904 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
905 bool wrapped;
907 /* #line commands expand macros. */
908 token = cpp_get_token (pfile);
909 if (token->type != CPP_NUMBER
910 || strtolinenum (token->val.str.text, token->val.str.len,
911 &new_lineno, &wrapped))
913 if (token->type == CPP_EOF)
914 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
915 else
916 cpp_error (pfile, CPP_DL_ERROR,
917 "\"%s\" after #line is not a positive integer",
918 cpp_token_as_text (pfile, token));
919 return;
922 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
923 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
924 else if (wrapped)
925 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
927 token = cpp_get_token (pfile);
928 if (token->type == CPP_STRING)
930 cpp_string s = { 0, 0 };
931 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
932 &s, CPP_STRING))
933 new_file = (const char *)s.text;
934 check_eol (pfile, true);
936 else if (token->type != CPP_EOF)
938 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
939 cpp_token_as_text (pfile, token));
940 return;
943 skip_rest_of_line (pfile);
944 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
945 map_sysp);
948 /* Interpret the # 44 "file" [flags] notation, which has slightly
949 different syntax and semantics from #line: Flags are allowed,
950 and we never complain about the line number being too big. */
951 static void
952 do_linemarker (cpp_reader *pfile)
954 const struct line_maps *line_table = pfile->line_table;
955 const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
956 const cpp_token *token;
957 const char *new_file = ORDINARY_MAP_FILE_NAME (map);
958 linenum_type new_lineno;
959 unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
960 enum lc_reason reason = LC_RENAME_VERBATIM;
961 int flag;
962 bool wrapped;
964 /* Back up so we can get the number again. Putting this in
965 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
966 some circumstances, which can segfault. */
967 _cpp_backup_tokens (pfile, 1);
969 /* #line commands expand macros. */
970 token = cpp_get_token (pfile);
971 if (token->type != CPP_NUMBER
972 || strtolinenum (token->val.str.text, token->val.str.len,
973 &new_lineno, &wrapped))
975 /* Unlike #line, there does not seem to be a way to get an EOF
976 here. So, it should be safe to always spell the token. */
977 cpp_error (pfile, CPP_DL_ERROR,
978 "\"%s\" after # is not a positive integer",
979 cpp_token_as_text (pfile, token));
980 return;
983 token = cpp_get_token (pfile);
984 if (token->type == CPP_STRING)
986 cpp_string s = { 0, 0 };
987 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
988 1, &s, CPP_STRING))
989 new_file = (const char *)s.text;
991 new_sysp = 0;
992 flag = read_flag (pfile, 0);
993 if (flag == 1)
995 reason = LC_ENTER;
996 /* Fake an include for cpp_included (). */
997 _cpp_fake_include (pfile, new_file);
998 flag = read_flag (pfile, flag);
1000 else if (flag == 2)
1002 reason = LC_LEAVE;
1003 flag = read_flag (pfile, flag);
1005 if (flag == 3)
1007 new_sysp = 1;
1008 flag = read_flag (pfile, flag);
1009 if (flag == 4)
1010 new_sysp = 2;
1012 pfile->buffer->sysp = new_sysp;
1014 check_eol (pfile, false);
1016 else if (token->type != CPP_EOF)
1018 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
1019 cpp_token_as_text (pfile, token));
1020 return;
1023 skip_rest_of_line (pfile);
1025 /* Compensate for the increment in linemap_add that occurs in
1026 _cpp_do_file_change. We're currently at the start of the line
1027 *following* the #line directive. A separate source_location for this
1028 location makes no sense (until we do the LC_LEAVE), and
1029 complicates LAST_SOURCE_LINE_LOCATION. */
1030 pfile->line_table->highest_location--;
1032 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1035 /* Arrange the file_change callback. pfile->line has changed to
1036 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1037 header, 2 for a system header that needs to be extern "C" protected,
1038 and zero otherwise. */
1039 void
1040 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1041 const char *to_file, linenum_type file_line,
1042 unsigned int sysp)
1044 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1045 to_file, file_line);
1046 if (map != NULL)
1047 linemap_line_start (pfile->line_table,
1048 ORDINARY_MAP_STARTING_LINE_NUMBER (map),
1049 127);
1051 if (pfile->cb.file_change)
1052 pfile->cb.file_change (pfile, map);
1055 /* Report a warning or error detected by the program we are
1056 processing. Use the directive's tokens in the error message. */
1057 static void
1058 do_diagnostic (cpp_reader *pfile, int code, int reason, int print_dir)
1060 const unsigned char *dir_name;
1061 unsigned char *line;
1062 source_location src_loc = pfile->cur_token[-1].src_loc;
1064 if (print_dir)
1065 dir_name = pfile->directive->name;
1066 else
1067 dir_name = NULL;
1068 pfile->state.prevent_expansion++;
1069 line = cpp_output_line_to_string (pfile, dir_name);
1070 pfile->state.prevent_expansion--;
1072 if (code == CPP_DL_WARNING_SYSHDR && reason)
1073 cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line);
1074 else if (code == CPP_DL_WARNING && reason)
1075 cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line);
1076 else
1077 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1078 free (line);
1081 static void
1082 do_error (cpp_reader *pfile)
1084 do_diagnostic (pfile, CPP_DL_ERROR, 0, 1);
1087 static void
1088 do_warning (cpp_reader *pfile)
1090 /* We want #warning diagnostics to be emitted in system headers too. */
1091 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1);
1094 /* Report program identification. */
1095 static void
1096 do_ident (cpp_reader *pfile)
1098 const cpp_token *str = cpp_get_token (pfile);
1100 if (str->type != CPP_STRING)
1101 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1102 pfile->directive->name);
1103 else if (pfile->cb.ident)
1104 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1106 check_eol (pfile, false);
1109 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1110 matching entry, or NULL if none is found. The returned entry could
1111 be the start of a namespace chain, or a pragma. */
1112 static struct pragma_entry *
1113 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1115 while (chain && chain->pragma != pragma)
1116 chain = chain->next;
1118 return chain;
1121 /* Create and insert a blank pragma entry at the beginning of a
1122 singly-linked CHAIN. */
1123 static struct pragma_entry *
1124 new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1126 struct pragma_entry *new_entry;
1128 new_entry = (struct pragma_entry *)
1129 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1131 memset (new_entry, 0, sizeof (struct pragma_entry));
1132 new_entry->next = *chain;
1134 *chain = new_entry;
1135 return new_entry;
1138 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1139 goes in the global namespace. */
1140 static struct pragma_entry *
1141 register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1142 bool allow_name_expansion)
1144 struct pragma_entry **chain = &pfile->pragmas;
1145 struct pragma_entry *entry;
1146 const cpp_hashnode *node;
1148 if (space)
1150 node = cpp_lookup (pfile, UC space, strlen (space));
1151 entry = lookup_pragma_entry (*chain, node);
1152 if (!entry)
1154 entry = new_pragma_entry (pfile, chain);
1155 entry->pragma = node;
1156 entry->is_nspace = true;
1157 entry->allow_expansion = allow_name_expansion;
1159 else if (!entry->is_nspace)
1160 goto clash;
1161 else if (entry->allow_expansion != allow_name_expansion)
1163 cpp_error (pfile, CPP_DL_ICE,
1164 "registering pragmas in namespace \"%s\" with mismatched "
1165 "name expansion", space);
1166 return NULL;
1168 chain = &entry->u.space;
1170 else if (allow_name_expansion)
1172 cpp_error (pfile, CPP_DL_ICE,
1173 "registering pragma \"%s\" with name expansion "
1174 "and no namespace", name);
1175 return NULL;
1178 /* Check for duplicates. */
1179 node = cpp_lookup (pfile, UC name, strlen (name));
1180 entry = lookup_pragma_entry (*chain, node);
1181 if (entry == NULL)
1183 entry = new_pragma_entry (pfile, chain);
1184 entry->pragma = node;
1185 return entry;
1188 if (entry->is_nspace)
1189 clash:
1190 cpp_error (pfile, CPP_DL_ICE,
1191 "registering \"%s\" as both a pragma and a pragma namespace",
1192 NODE_NAME (node));
1193 else if (space)
1194 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1195 space, name);
1196 else
1197 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1199 return NULL;
1202 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1203 static void
1204 register_pragma_internal (cpp_reader *pfile, const char *space,
1205 const char *name, pragma_cb handler)
1207 struct pragma_entry *entry;
1209 entry = register_pragma_1 (pfile, space, name, false);
1210 entry->is_internal = true;
1211 entry->u.handler = handler;
1214 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1215 goes in the global namespace. HANDLER is the handler it will call,
1216 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1217 expansion while parsing pragma NAME. This function is exported
1218 from libcpp. */
1219 void
1220 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1221 pragma_cb handler, bool allow_expansion)
1223 struct pragma_entry *entry;
1225 if (!handler)
1227 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1228 return;
1231 entry = register_pragma_1 (pfile, space, name, false);
1232 if (entry)
1234 entry->allow_expansion = allow_expansion;
1235 entry->u.handler = handler;
1239 /* Similarly, but create mark the pragma for deferred processing.
1240 When found, a CPP_PRAGMA token will be insertted into the stream
1241 with IDENT in the token->u.pragma slot. */
1242 void
1243 cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1244 const char *name, unsigned int ident,
1245 bool allow_expansion, bool allow_name_expansion)
1247 struct pragma_entry *entry;
1249 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1250 if (entry)
1252 entry->is_deferred = true;
1253 entry->allow_expansion = allow_expansion;
1254 entry->u.ident = ident;
1258 /* Register the pragmas the preprocessor itself handles. */
1259 void
1260 _cpp_init_internal_pragmas (cpp_reader *pfile)
1262 /* Pragmas in the global namespace. */
1263 register_pragma_internal (pfile, 0, "once", do_pragma_once);
1264 register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
1265 register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
1267 /* New GCC-specific pragmas should be put in the GCC namespace. */
1268 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1269 register_pragma_internal (pfile, "GCC", "system_header",
1270 do_pragma_system_header);
1271 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1272 register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning);
1273 register_pragma_internal (pfile, "GCC", "error", do_pragma_error);
1276 /* Return the number of registered pragmas in PE. */
1278 static int
1279 count_registered_pragmas (struct pragma_entry *pe)
1281 int ct = 0;
1282 for (; pe != NULL; pe = pe->next)
1284 if (pe->is_nspace)
1285 ct += count_registered_pragmas (pe->u.space);
1286 ct++;
1288 return ct;
1291 /* Save into SD the names of the registered pragmas referenced by PE,
1292 and return a pointer to the next free space in SD. */
1294 static char **
1295 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1297 for (; pe != NULL; pe = pe->next)
1299 if (pe->is_nspace)
1300 sd = save_registered_pragmas (pe->u.space, sd);
1301 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1302 HT_LEN (&pe->pragma->ident),
1303 HT_LEN (&pe->pragma->ident) + 1);
1305 return sd;
1308 /* Return a newly-allocated array which saves the names of the
1309 registered pragmas. */
1311 char **
1312 _cpp_save_pragma_names (cpp_reader *pfile)
1314 int ct = count_registered_pragmas (pfile->pragmas);
1315 char **result = XNEWVEC (char *, ct);
1316 (void) save_registered_pragmas (pfile->pragmas, result);
1317 return result;
1320 /* Restore from SD the names of the registered pragmas referenced by PE,
1321 and return a pointer to the next unused name in SD. */
1323 static char **
1324 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1325 char **sd)
1327 for (; pe != NULL; pe = pe->next)
1329 if (pe->is_nspace)
1330 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1331 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
1332 free (*sd);
1333 sd++;
1335 return sd;
1338 /* Restore the names of the registered pragmas from SAVED. */
1340 void
1341 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1343 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1344 free (saved);
1347 /* Pragmata handling. We handle some, and pass the rest on to the
1348 front end. C99 defines three pragmas and says that no macro
1349 expansion is to be performed on them; whether or not macro
1350 expansion happens for other pragmas is implementation defined.
1351 This implementation allows for a mix of both, since GCC did not
1352 traditionally macro expand its (few) pragmas, whereas OpenMP
1353 specifies that macro expansion should happen. */
1354 static void
1355 do_pragma (cpp_reader *pfile)
1357 const struct pragma_entry *p = NULL;
1358 const cpp_token *token, *pragma_token;
1359 source_location pragma_token_virt_loc = 0;
1360 cpp_token ns_token;
1361 unsigned int count = 1;
1363 pfile->state.prevent_expansion++;
1365 pragma_token = token = cpp_get_token_with_location (pfile,
1366 &pragma_token_virt_loc);
1367 ns_token = *token;
1368 if (token->type == CPP_NAME)
1370 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
1371 if (p && p->is_nspace)
1373 bool allow_name_expansion = p->allow_expansion;
1374 if (allow_name_expansion)
1375 pfile->state.prevent_expansion--;
1377 token = cpp_get_token (pfile);
1378 if (token->type == CPP_NAME)
1379 p = lookup_pragma_entry (p->u.space, token->val.node.node);
1380 else
1381 p = NULL;
1382 if (allow_name_expansion)
1383 pfile->state.prevent_expansion++;
1384 count = 2;
1388 if (p)
1390 if (p->is_deferred)
1392 pfile->directive_result.src_loc = pragma_token_virt_loc;
1393 pfile->directive_result.type = CPP_PRAGMA;
1394 pfile->directive_result.flags = pragma_token->flags;
1395 pfile->directive_result.val.pragma = p->u.ident;
1396 pfile->state.in_deferred_pragma = true;
1397 pfile->state.pragma_allow_expansion = p->allow_expansion;
1398 if (!p->allow_expansion)
1399 pfile->state.prevent_expansion++;
1401 else
1403 /* Since the handler below doesn't get the line number, that
1404 it might need for diagnostics, make sure it has the right
1405 numbers in place. */
1406 if (pfile->cb.line_change)
1407 (*pfile->cb.line_change) (pfile, pragma_token, false);
1408 if (p->allow_expansion)
1409 pfile->state.prevent_expansion--;
1410 (*p->u.handler) (pfile);
1411 if (p->allow_expansion)
1412 pfile->state.prevent_expansion++;
1415 else if (pfile->cb.def_pragma)
1417 if (count == 1 || pfile->context->prev == NULL)
1418 _cpp_backup_tokens (pfile, count);
1419 else
1421 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1422 won't allow backing 2 tokens. */
1423 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1424 reads both tokens, we could perhaps free it, but if it doesn't,
1425 we don't know the exact lifespan. */
1426 cpp_token *toks = XNEWVEC (cpp_token, 2);
1427 toks[0] = ns_token;
1428 toks[0].flags |= NO_EXPAND;
1429 toks[1] = *token;
1430 toks[1].flags |= NO_EXPAND;
1431 _cpp_push_token_context (pfile, NULL, toks, 2);
1433 pfile->cb.def_pragma (pfile, pfile->directive_line);
1436 pfile->state.prevent_expansion--;
1439 /* Handle #pragma once. */
1440 static void
1441 do_pragma_once (cpp_reader *pfile)
1443 if (cpp_in_primary_file (pfile))
1444 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1446 check_eol (pfile, false);
1447 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1450 /* Handle #pragma push_macro(STRING). */
1451 static void
1452 do_pragma_push_macro (cpp_reader *pfile)
1454 cpp_hashnode *node;
1455 size_t defnlen;
1456 const uchar *defn = NULL;
1457 char *macroname, *dest;
1458 const char *limit, *src;
1459 const cpp_token *txt;
1460 struct def_pragma_macro *c;
1462 txt = get__Pragma_string (pfile);
1463 if (!txt)
1465 source_location src_loc = pfile->cur_token[-1].src_loc;
1466 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1467 "invalid #pragma push_macro directive");
1468 check_eol (pfile, false);
1469 skip_rest_of_line (pfile);
1470 return;
1472 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1473 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1474 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1475 while (src < limit)
1477 /* We know there is a character following the backslash. */
1478 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1479 src++;
1480 *dest++ = *src++;
1482 *dest = 0;
1483 check_eol (pfile, false);
1484 skip_rest_of_line (pfile);
1485 c = XNEW (struct def_pragma_macro);
1486 memset (c, 0, sizeof (struct def_pragma_macro));
1487 c->name = XNEWVAR (char, strlen (macroname) + 1);
1488 strcpy (c->name, macroname);
1489 c->next = pfile->pushed_macros;
1490 node = _cpp_lex_identifier (pfile, c->name);
1491 if (node->type == NT_VOID)
1492 c->is_undef = 1;
1493 else
1495 defn = cpp_macro_definition (pfile, node);
1496 defnlen = ustrlen (defn);
1497 c->definition = XNEWVEC (uchar, defnlen + 2);
1498 c->definition[defnlen] = '\n';
1499 c->definition[defnlen + 1] = 0;
1500 c->line = node->value.macro->line;
1501 c->syshdr = node->value.macro->syshdr;
1502 c->used = node->value.macro->used;
1503 memcpy (c->definition, defn, defnlen);
1506 pfile->pushed_macros = c;
1509 /* Handle #pragma pop_macro(STRING). */
1510 static void
1511 do_pragma_pop_macro (cpp_reader *pfile)
1513 char *macroname, *dest;
1514 const char *limit, *src;
1515 const cpp_token *txt;
1516 struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
1517 txt = get__Pragma_string (pfile);
1518 if (!txt)
1520 source_location src_loc = pfile->cur_token[-1].src_loc;
1521 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1522 "invalid #pragma pop_macro directive");
1523 check_eol (pfile, false);
1524 skip_rest_of_line (pfile);
1525 return;
1527 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1528 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1529 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1530 while (src < limit)
1532 /* We know there is a character following the backslash. */
1533 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1534 src++;
1535 *dest++ = *src++;
1537 *dest = 0;
1538 check_eol (pfile, false);
1539 skip_rest_of_line (pfile);
1541 while (c != NULL)
1543 if (!strcmp (c->name, macroname))
1545 if (!l)
1546 pfile->pushed_macros = c->next;
1547 else
1548 l->next = c->next;
1549 cpp_pop_definition (pfile, c);
1550 free (c->definition);
1551 free (c->name);
1552 free (c);
1553 break;
1555 l = c;
1556 c = c->next;
1560 /* Handle #pragma GCC poison, to poison one or more identifiers so
1561 that the lexer produces a hard error for each subsequent usage. */
1562 static void
1563 do_pragma_poison (cpp_reader *pfile)
1565 const cpp_token *tok;
1566 cpp_hashnode *hp;
1568 pfile->state.poisoned_ok = 1;
1569 for (;;)
1571 tok = _cpp_lex_token (pfile);
1572 if (tok->type == CPP_EOF)
1573 break;
1574 if (tok->type != CPP_NAME)
1576 cpp_error (pfile, CPP_DL_ERROR,
1577 "invalid #pragma GCC poison directive");
1578 break;
1581 hp = tok->val.node.node;
1582 if (hp->flags & NODE_POISONED)
1583 continue;
1585 if (hp->type == NT_MACRO)
1586 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1587 NODE_NAME (hp));
1588 _cpp_free_definition (hp);
1589 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1591 pfile->state.poisoned_ok = 0;
1594 /* Mark the current header as a system header. This will suppress
1595 some categories of warnings (notably those from -pedantic). It is
1596 intended for use in system libraries that cannot be implemented in
1597 conforming C, but cannot be certain that their headers appear in a
1598 system include directory. To prevent abuse, it is rejected in the
1599 primary source file. */
1600 static void
1601 do_pragma_system_header (cpp_reader *pfile)
1603 if (cpp_in_primary_file (pfile))
1604 cpp_error (pfile, CPP_DL_WARNING,
1605 "#pragma system_header ignored outside include file");
1606 else
1608 check_eol (pfile, false);
1609 skip_rest_of_line (pfile);
1610 cpp_make_system_header (pfile, 1, 0);
1614 /* Check the modified date of the current include file against a specified
1615 file. Issue a diagnostic, if the specified file is newer. We use this to
1616 determine if a fixed header should be refixed. */
1617 static void
1618 do_pragma_dependency (cpp_reader *pfile)
1620 const char *fname;
1621 int angle_brackets, ordering;
1622 source_location location;
1624 fname = parse_include (pfile, &angle_brackets, NULL, &location);
1625 if (!fname)
1626 return;
1628 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1629 if (ordering < 0)
1630 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1631 else if (ordering > 0)
1633 cpp_error (pfile, CPP_DL_WARNING,
1634 "current file is older than %s", fname);
1635 if (cpp_get_token (pfile)->type != CPP_EOF)
1637 _cpp_backup_tokens (pfile, 1);
1638 do_diagnostic (pfile, CPP_DL_WARNING, 0, 0);
1642 free ((void *) fname);
1645 /* Issue a diagnostic with the message taken from the pragma. If
1646 ERROR is true, the diagnostic is a warning, otherwise, it is an
1647 error. */
1648 static void
1649 do_pragma_warning_or_error (cpp_reader *pfile, bool error)
1651 const cpp_token *tok = _cpp_lex_token (pfile);
1652 cpp_string str;
1653 if (tok->type != CPP_STRING
1654 || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str,
1655 CPP_STRING)
1656 || str.len == 0)
1658 cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive",
1659 error ? "error" : "warning");
1660 return;
1662 cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING,
1663 "%s", str.text);
1664 free ((void *)str.text);
1667 /* Issue a warning diagnostic. */
1668 static void
1669 do_pragma_warning (cpp_reader *pfile)
1671 do_pragma_warning_or_error (pfile, false);
1674 /* Issue an error diagnostic. */
1675 static void
1676 do_pragma_error (cpp_reader *pfile)
1678 do_pragma_warning_or_error (pfile, true);
1681 /* Get a token but skip padding. */
1682 static const cpp_token *
1683 get_token_no_padding (cpp_reader *pfile)
1685 for (;;)
1687 const cpp_token *result = cpp_get_token (pfile);
1688 if (result->type != CPP_PADDING)
1689 return result;
1693 /* Check syntax is "(string-literal)". Returns the string on success,
1694 or NULL on failure. */
1695 static const cpp_token *
1696 get__Pragma_string (cpp_reader *pfile)
1698 const cpp_token *string;
1699 const cpp_token *paren;
1701 paren = get_token_no_padding (pfile);
1702 if (paren->type == CPP_EOF)
1703 _cpp_backup_tokens (pfile, 1);
1704 if (paren->type != CPP_OPEN_PAREN)
1705 return NULL;
1707 string = get_token_no_padding (pfile);
1708 if (string->type == CPP_EOF)
1709 _cpp_backup_tokens (pfile, 1);
1710 if (string->type != CPP_STRING && string->type != CPP_WSTRING
1711 && string->type != CPP_STRING32 && string->type != CPP_STRING16
1712 && string->type != CPP_UTF8STRING)
1713 return NULL;
1715 paren = get_token_no_padding (pfile);
1716 if (paren->type == CPP_EOF)
1717 _cpp_backup_tokens (pfile, 1);
1718 if (paren->type != CPP_CLOSE_PAREN)
1719 return NULL;
1721 return string;
1724 /* Destringize IN into a temporary buffer, by removing the first \ of
1725 \" and \\ sequences, and process the result as a #pragma directive. */
1726 static void
1727 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1729 const unsigned char *src, *limit;
1730 char *dest, *result;
1731 cpp_context *saved_context;
1732 cpp_token *saved_cur_token;
1733 tokenrun *saved_cur_run;
1734 cpp_token *toks;
1735 int count;
1736 const struct directive *save_directive;
1738 dest = result = (char *) alloca (in->len - 1);
1739 src = in->text + 1 + (in->text[0] == 'L');
1740 limit = in->text + in->len - 1;
1741 while (src < limit)
1743 /* We know there is a character following the backslash. */
1744 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1745 src++;
1746 *dest++ = *src++;
1748 *dest = '\n';
1750 /* Ugh; an awful kludge. We are really not set up to be lexing
1751 tokens when in the middle of a macro expansion. Use a new
1752 context to force cpp_get_token to lex, and so skip_rest_of_line
1753 doesn't go beyond the end of the text. Also, remember the
1754 current lexing position so we can return to it later.
1756 Something like line-at-a-time lexing should remove the need for
1757 this. */
1758 saved_context = pfile->context;
1759 saved_cur_token = pfile->cur_token;
1760 saved_cur_run = pfile->cur_run;
1762 pfile->context = XCNEW (cpp_context);
1764 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1765 until we've read all of the tokens that we want. */
1766 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1767 /* from_stage3 */ true);
1768 /* ??? Antique Disgusting Hack. What does this do? */
1769 if (pfile->buffer->prev)
1770 pfile->buffer->file = pfile->buffer->prev->file;
1772 start_directive (pfile);
1773 _cpp_clean_line (pfile);
1774 save_directive = pfile->directive;
1775 pfile->directive = &dtable[T_PRAGMA];
1776 do_pragma (pfile);
1777 end_directive (pfile, 1);
1778 pfile->directive = save_directive;
1780 /* We always insert at least one token, the directive result. It'll
1781 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1782 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1784 /* If we're not handling the pragma internally, read all of the tokens from
1785 the string buffer now, while the string buffer is still installed. */
1786 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1787 to me what the true lifespan of the tokens are. It would appear that
1788 the lifespan is the entire parse of the main input stream, in which case
1789 this may not be wrong. */
1790 if (pfile->directive_result.type == CPP_PRAGMA)
1792 int maxcount;
1794 count = 1;
1795 maxcount = 50;
1796 toks = XNEWVEC (cpp_token, maxcount);
1797 toks[0] = pfile->directive_result;
1801 if (count == maxcount)
1803 maxcount = maxcount * 3 / 2;
1804 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1806 toks[count] = *cpp_get_token (pfile);
1807 /* Macros have been already expanded by cpp_get_token
1808 if the pragma allowed expansion. */
1809 toks[count++].flags |= NO_EXPAND;
1811 while (toks[count-1].type != CPP_PRAGMA_EOL);
1813 else
1815 count = 1;
1816 toks = XNEW (cpp_token);
1817 toks[0] = pfile->directive_result;
1819 /* If we handled the entire pragma internally, make sure we get the
1820 line number correct for the next token. */
1821 if (pfile->cb.line_change)
1822 pfile->cb.line_change (pfile, pfile->cur_token, false);
1825 /* Finish inlining run_directive. */
1826 pfile->buffer->file = NULL;
1827 _cpp_pop_buffer (pfile);
1829 /* Reset the old macro state before ... */
1830 XDELETE (pfile->context);
1831 pfile->context = saved_context;
1832 pfile->cur_token = saved_cur_token;
1833 pfile->cur_run = saved_cur_run;
1835 /* ... inserting the new tokens we collected. */
1836 _cpp_push_token_context (pfile, NULL, toks, count);
1839 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1841 _cpp_do__Pragma (cpp_reader *pfile)
1843 const cpp_token *string = get__Pragma_string (pfile);
1844 pfile->directive_result.type = CPP_PADDING;
1846 if (string)
1848 destringize_and_run (pfile, &string->val.str);
1849 return 1;
1851 cpp_error (pfile, CPP_DL_ERROR,
1852 "_Pragma takes a parenthesized string literal");
1853 return 0;
1856 /* Handle #ifdef. */
1857 static void
1858 do_ifdef (cpp_reader *pfile)
1860 int skip = 1;
1862 if (! pfile->state.skipping)
1864 cpp_hashnode *node = lex_macro_node (pfile, false);
1866 if (node)
1868 /* Do not treat conditional macros as being defined. This is due to
1869 the powerpc and spu ports using conditional macros for 'vector',
1870 'bool', and 'pixel' to act as conditional keywords. This messes
1871 up tests like #ifndef bool. */
1872 skip = (node->type != NT_MACRO
1873 || ((node->flags & NODE_CONDITIONAL) != 0));
1874 _cpp_mark_macro_used (node);
1875 if (!(node->flags & NODE_USED))
1877 node->flags |= NODE_USED;
1878 if (node->type == NT_MACRO)
1880 if ((node->flags & NODE_BUILTIN)
1881 && pfile->cb.user_builtin_macro)
1882 pfile->cb.user_builtin_macro (pfile, node);
1883 if (pfile->cb.used_define)
1884 pfile->cb.used_define (pfile, pfile->directive_line, node);
1886 else
1888 if (pfile->cb.used_undef)
1889 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1892 if (pfile->cb.used)
1893 pfile->cb.used (pfile, pfile->directive_line, node);
1894 check_eol (pfile, false);
1898 push_conditional (pfile, skip, T_IFDEF, 0);
1901 /* Handle #ifndef. */
1902 static void
1903 do_ifndef (cpp_reader *pfile)
1905 int skip = 1;
1906 cpp_hashnode *node = 0;
1908 if (! pfile->state.skipping)
1910 node = lex_macro_node (pfile, false);
1912 if (node)
1914 /* Do not treat conditional macros as being defined. This is due to
1915 the powerpc and spu ports using conditional macros for 'vector',
1916 'bool', and 'pixel' to act as conditional keywords. This messes
1917 up tests like #ifndef bool. */
1918 skip = (node->type == NT_MACRO
1919 && ((node->flags & NODE_CONDITIONAL) == 0));
1920 _cpp_mark_macro_used (node);
1921 if (!(node->flags & NODE_USED))
1923 node->flags |= NODE_USED;
1924 if (node->type == NT_MACRO)
1926 if ((node->flags & NODE_BUILTIN)
1927 && pfile->cb.user_builtin_macro)
1928 pfile->cb.user_builtin_macro (pfile, node);
1929 if (pfile->cb.used_define)
1930 pfile->cb.used_define (pfile, pfile->directive_line, node);
1932 else
1934 if (pfile->cb.used_undef)
1935 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1938 if (pfile->cb.used)
1939 pfile->cb.used (pfile, pfile->directive_line, node);
1940 check_eol (pfile, false);
1944 push_conditional (pfile, skip, T_IFNDEF, node);
1947 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1948 pfile->mi_ind_cmacro so we can handle multiple-include
1949 optimizations. If macro expansion occurs in the expression, we
1950 cannot treat it as a controlling conditional, since the expansion
1951 could change in the future. That is handled by cpp_get_token. */
1952 static void
1953 do_if (cpp_reader *pfile)
1955 int skip = 1;
1957 if (! pfile->state.skipping)
1958 skip = _cpp_parse_expr (pfile, true) == false;
1960 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1963 /* Flip skipping state if appropriate and continue without changing
1964 if_stack; this is so that the error message for missing #endif's
1965 etc. will point to the original #if. */
1966 static void
1967 do_else (cpp_reader *pfile)
1969 cpp_buffer *buffer = pfile->buffer;
1970 struct if_stack *ifs = buffer->if_stack;
1972 if (ifs == NULL)
1973 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1974 else
1976 if (ifs->type == T_ELSE)
1978 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1979 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1980 "the conditional began here");
1982 ifs->type = T_ELSE;
1984 /* Skip any future (erroneous) #elses or #elifs. */
1985 pfile->state.skipping = ifs->skip_elses;
1986 ifs->skip_elses = true;
1988 /* Invalidate any controlling macro. */
1989 ifs->mi_cmacro = 0;
1991 /* Only check EOL if was not originally skipping. */
1992 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1993 check_eol (pfile, false);
1997 /* Handle a #elif directive by not changing if_stack either. See the
1998 comment above do_else. */
1999 static void
2000 do_elif (cpp_reader *pfile)
2002 cpp_buffer *buffer = pfile->buffer;
2003 struct if_stack *ifs = buffer->if_stack;
2005 if (ifs == NULL)
2006 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
2007 else
2009 if (ifs->type == T_ELSE)
2011 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
2012 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2013 "the conditional began here");
2015 ifs->type = T_ELIF;
2017 if (! ifs->was_skipping)
2019 bool value;
2020 /* The standard mandates that the expression be parsed even
2021 if we are skipping elses at this point -- the lexical
2022 restrictions on #elif only apply to skipped groups, but
2023 this group is not being skipped. Temporarily set
2024 skipping to false to get lexer warnings. */
2025 pfile->state.skipping = 0;
2026 value = _cpp_parse_expr (pfile, false);
2027 if (ifs->skip_elses)
2028 pfile->state.skipping = 1;
2029 else
2031 pfile->state.skipping = ! value;
2032 ifs->skip_elses = value;
2036 /* Invalidate any controlling macro. */
2037 ifs->mi_cmacro = 0;
2041 /* #endif pops the if stack and resets pfile->state.skipping. */
2042 static void
2043 do_endif (cpp_reader *pfile)
2045 cpp_buffer *buffer = pfile->buffer;
2046 struct if_stack *ifs = buffer->if_stack;
2048 if (ifs == NULL)
2049 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
2050 else
2052 /* Only check EOL if was not originally skipping. */
2053 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
2054 check_eol (pfile, false);
2056 /* If potential control macro, we go back outside again. */
2057 if (ifs->next == 0 && ifs->mi_cmacro)
2059 pfile->mi_valid = true;
2060 pfile->mi_cmacro = ifs->mi_cmacro;
2063 buffer->if_stack = ifs->next;
2064 pfile->state.skipping = ifs->was_skipping;
2065 obstack_free (&pfile->buffer_ob, ifs);
2069 /* Push an if_stack entry for a preprocessor conditional, and set
2070 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2071 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2072 we need to check here that we are at the top of the file. */
2073 static void
2074 push_conditional (cpp_reader *pfile, int skip, int type,
2075 const cpp_hashnode *cmacro)
2077 struct if_stack *ifs;
2078 cpp_buffer *buffer = pfile->buffer;
2080 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
2081 ifs->line = pfile->directive_line;
2082 ifs->next = buffer->if_stack;
2083 ifs->skip_elses = pfile->state.skipping || !skip;
2084 ifs->was_skipping = pfile->state.skipping;
2085 ifs->type = type;
2086 /* This condition is effectively a test for top-of-file. */
2087 if (pfile->mi_valid && pfile->mi_cmacro == 0)
2088 ifs->mi_cmacro = cmacro;
2089 else
2090 ifs->mi_cmacro = 0;
2092 pfile->state.skipping = skip;
2093 buffer->if_stack = ifs;
2096 /* Read the tokens of the answer into the macro pool, in a directive
2097 of type TYPE. Only commit the memory if we intend it as permanent
2098 storage, i.e. the #assert case. Returns 0 on success, and sets
2099 ANSWERP to point to the answer. PRED_LOC is the location of the
2100 predicate. */
2101 static int
2102 parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
2103 source_location pred_loc)
2105 const cpp_token *paren;
2106 struct answer *answer;
2107 unsigned int acount;
2109 /* In a conditional, it is legal to not have an open paren. We
2110 should save the following token in this case. */
2111 paren = cpp_get_token (pfile);
2113 /* If not a paren, see if we're OK. */
2114 if (paren->type != CPP_OPEN_PAREN)
2116 /* In a conditional no answer is a test for any answer. It
2117 could be followed by any token. */
2118 if (type == T_IF)
2120 _cpp_backup_tokens (pfile, 1);
2121 return 0;
2124 /* #unassert with no answer is valid - it removes all answers. */
2125 if (type == T_UNASSERT && paren->type == CPP_EOF)
2126 return 0;
2128 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
2129 "missing '(' after predicate");
2130 return 1;
2133 for (acount = 0;; acount++)
2135 size_t room_needed;
2136 const cpp_token *token = cpp_get_token (pfile);
2137 cpp_token *dest;
2139 if (token->type == CPP_CLOSE_PAREN)
2140 break;
2142 if (token->type == CPP_EOF)
2144 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
2145 return 1;
2148 /* struct answer includes the space for one token. */
2149 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
2151 if (BUFF_ROOM (pfile->a_buff) < room_needed)
2152 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
2154 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
2155 *dest = *token;
2157 /* Drop whitespace at start, for answer equivalence purposes. */
2158 if (acount == 0)
2159 dest->flags &= ~PREV_WHITE;
2162 if (acount == 0)
2164 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
2165 return 1;
2168 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
2169 answer->count = acount;
2170 answer->next = NULL;
2171 *answerp = answer;
2173 return 0;
2176 /* Parses an assertion directive of type TYPE, returning a pointer to
2177 the hash node of the predicate, or 0 on error. If an answer was
2178 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
2179 static cpp_hashnode *
2180 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
2182 cpp_hashnode *result = 0;
2183 const cpp_token *predicate;
2185 /* We don't expand predicates or answers. */
2186 pfile->state.prevent_expansion++;
2188 *answerp = 0;
2189 predicate = cpp_get_token (pfile);
2190 if (predicate->type == CPP_EOF)
2191 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
2192 else if (predicate->type != CPP_NAME)
2193 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2194 "predicate must be an identifier");
2195 else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
2197 unsigned int len = NODE_LEN (predicate->val.node.node);
2198 unsigned char *sym = (unsigned char *) alloca (len + 1);
2200 /* Prefix '#' to get it out of macro namespace. */
2201 sym[0] = '#';
2202 memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
2203 result = cpp_lookup (pfile, sym, len + 1);
2206 pfile->state.prevent_expansion--;
2207 return result;
2210 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2211 or a pointer to NULL if the answer is not in the chain. */
2212 static struct answer **
2213 find_answer (cpp_hashnode *node, const struct answer *candidate)
2215 unsigned int i;
2216 struct answer **result;
2218 for (result = &node->value.answers; *result; result = &(*result)->next)
2220 struct answer *answer = *result;
2222 if (answer->count == candidate->count)
2224 for (i = 0; i < answer->count; i++)
2225 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2226 break;
2228 if (i == answer->count)
2229 break;
2233 return result;
2236 /* Test an assertion within a preprocessor conditional. Returns
2237 nonzero on failure, zero on success. On success, the result of
2238 the test is written into VALUE, otherwise the value 0. */
2240 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2242 struct answer *answer;
2243 cpp_hashnode *node;
2245 node = parse_assertion (pfile, &answer, T_IF);
2247 /* For recovery, an erroneous assertion expression is handled as a
2248 failing assertion. */
2249 *value = 0;
2251 if (node)
2252 *value = (node->type == NT_ASSERTION &&
2253 (answer == 0 || *find_answer (node, answer) != 0));
2254 else if (pfile->cur_token[-1].type == CPP_EOF)
2255 _cpp_backup_tokens (pfile, 1);
2257 /* We don't commit the memory for the answer - it's temporary only. */
2258 return node == 0;
2261 /* Handle #assert. */
2262 static void
2263 do_assert (cpp_reader *pfile)
2265 struct answer *new_answer;
2266 cpp_hashnode *node;
2268 node = parse_assertion (pfile, &new_answer, T_ASSERT);
2269 if (node)
2271 size_t answer_size;
2273 /* Place the new answer in the answer list. First check there
2274 is not a duplicate. */
2275 new_answer->next = 0;
2276 if (node->type == NT_ASSERTION)
2278 if (*find_answer (node, new_answer))
2280 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2281 NODE_NAME (node) + 1);
2282 return;
2284 new_answer->next = node->value.answers;
2287 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2288 * sizeof (cpp_token));
2289 /* Commit or allocate storage for the object. */
2290 if (pfile->hash_table->alloc_subobject)
2292 struct answer *temp_answer = new_answer;
2293 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2294 (answer_size);
2295 memcpy (new_answer, temp_answer, answer_size);
2297 else
2298 BUFF_FRONT (pfile->a_buff) += answer_size;
2300 node->type = NT_ASSERTION;
2301 node->value.answers = new_answer;
2302 check_eol (pfile, false);
2306 /* Handle #unassert. */
2307 static void
2308 do_unassert (cpp_reader *pfile)
2310 cpp_hashnode *node;
2311 struct answer *answer;
2313 node = parse_assertion (pfile, &answer, T_UNASSERT);
2314 /* It isn't an error to #unassert something that isn't asserted. */
2315 if (node && node->type == NT_ASSERTION)
2317 if (answer)
2319 struct answer **p = find_answer (node, answer), *temp;
2321 /* Remove the answer from the list. */
2322 temp = *p;
2323 if (temp)
2324 *p = temp->next;
2326 /* Did we free the last answer? */
2327 if (node->value.answers == 0)
2328 node->type = NT_VOID;
2330 check_eol (pfile, false);
2332 else
2333 _cpp_free_definition (node);
2336 /* We don't commit the memory for the answer - it's temporary only. */
2339 /* These are for -D, -U, -A. */
2341 /* Process the string STR as if it appeared as the body of a #define.
2342 If STR is just an identifier, define it with value 1.
2343 If STR has anything after the identifier, then it should
2344 be identifier=definition. */
2345 void
2346 cpp_define (cpp_reader *pfile, const char *str)
2348 char *buf;
2349 const char *p;
2350 size_t count;
2352 /* Copy the entire option so we can modify it.
2353 Change the first "=" in the string to a space. If there is none,
2354 tack " 1" on the end. */
2356 count = strlen (str);
2357 buf = (char *) alloca (count + 3);
2358 memcpy (buf, str, count);
2360 p = strchr (str, '=');
2361 if (p)
2362 buf[p - str] = ' ';
2363 else
2365 buf[count++] = ' ';
2366 buf[count++] = '1';
2368 buf[count] = '\n';
2370 run_directive (pfile, T_DEFINE, buf, count);
2374 /* Use to build macros to be run through cpp_define() as
2375 described above.
2376 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2378 void
2379 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2381 char *ptr = NULL;
2383 va_list ap;
2384 va_start (ap, fmt);
2385 vasprintf (&ptr, fmt, ap);
2386 va_end (ap);
2388 cpp_define (pfile, ptr);
2389 free (ptr);
2393 /* Slight variant of the above for use by initialize_builtins. */
2394 void
2395 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2397 size_t len = strlen (str);
2398 char *buf = (char *) alloca (len + 1);
2399 memcpy (buf, str, len);
2400 buf[len] = '\n';
2401 run_directive (pfile, T_DEFINE, buf, len);
2404 /* Process MACRO as if it appeared as the body of an #undef. */
2405 void
2406 cpp_undef (cpp_reader *pfile, const char *macro)
2408 size_t len = strlen (macro);
2409 char *buf = (char *) alloca (len + 1);
2410 memcpy (buf, macro, len);
2411 buf[len] = '\n';
2412 run_directive (pfile, T_UNDEF, buf, len);
2415 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2416 or first element is zero, then the macro should be undefined. */
2417 static void
2418 cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
2420 cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
2421 if (node == NULL)
2422 return;
2424 if (pfile->cb.before_define)
2425 pfile->cb.before_define (pfile);
2427 if (node->type == NT_MACRO)
2429 if (pfile->cb.undef)
2430 pfile->cb.undef (pfile, pfile->directive_line, node);
2431 if (CPP_OPTION (pfile, warn_unused_macros))
2432 _cpp_warn_if_unused_macro (pfile, node, NULL);
2434 if (node->type != NT_VOID)
2435 _cpp_free_definition (node);
2437 if (c->is_undef)
2438 return;
2440 size_t namelen;
2441 const uchar *dn;
2442 cpp_hashnode *h = NULL;
2443 cpp_buffer *nbuf;
2445 namelen = ustrcspn (c->definition, "( \n");
2446 h = cpp_lookup (pfile, c->definition, namelen);
2447 dn = c->definition + namelen;
2449 h->type = NT_VOID;
2450 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
2451 nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true);
2452 if (nbuf != NULL)
2454 _cpp_clean_line (pfile);
2455 nbuf->sysp = 1;
2456 if (!_cpp_create_definition (pfile, h))
2457 abort ();
2458 _cpp_pop_buffer (pfile);
2460 else
2461 abort ();
2462 h->value.macro->line = c->line;
2463 h->value.macro->syshdr = c->syshdr;
2464 h->value.macro->used = c->used;
2468 /* Process the string STR as if it appeared as the body of a #assert. */
2469 void
2470 cpp_assert (cpp_reader *pfile, const char *str)
2472 handle_assertion (pfile, str, T_ASSERT);
2475 /* Process STR as if it appeared as the body of an #unassert. */
2476 void
2477 cpp_unassert (cpp_reader *pfile, const char *str)
2479 handle_assertion (pfile, str, T_UNASSERT);
2482 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2483 static void
2484 handle_assertion (cpp_reader *pfile, const char *str, int type)
2486 size_t count = strlen (str);
2487 const char *p = strchr (str, '=');
2489 /* Copy the entire option so we can modify it. Change the first
2490 "=" in the string to a '(', and tack a ')' on the end. */
2491 char *buf = (char *) alloca (count + 2);
2493 memcpy (buf, str, count);
2494 if (p)
2496 buf[p - str] = '(';
2497 buf[count++] = ')';
2499 buf[count] = '\n';
2500 str = buf;
2502 run_directive (pfile, type, str, count);
2505 /* The options structure. */
2506 cpp_options *
2507 cpp_get_options (cpp_reader *pfile)
2509 return &pfile->opts;
2512 /* The callbacks structure. */
2513 cpp_callbacks *
2514 cpp_get_callbacks (cpp_reader *pfile)
2516 return &pfile->cb;
2519 /* Copy the given callbacks structure to our own. */
2520 void
2521 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2523 pfile->cb = *cb;
2526 /* The dependencies structure. (Creates one if it hasn't already been.) */
2527 struct deps *
2528 cpp_get_deps (cpp_reader *pfile)
2530 if (!pfile->deps)
2531 pfile->deps = deps_init ();
2532 return pfile->deps;
2535 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2536 doesn't fail. It does not generate a file change call back; that
2537 is the responsibility of the caller. */
2538 cpp_buffer *
2539 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2540 int from_stage3)
2542 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2544 /* Clears, amongst other things, if_stack and mi_cmacro. */
2545 memset (new_buffer, 0, sizeof (cpp_buffer));
2547 new_buffer->next_line = new_buffer->buf = buffer;
2548 new_buffer->rlimit = buffer + len;
2549 new_buffer->from_stage3 = from_stage3;
2550 new_buffer->prev = pfile->buffer;
2551 new_buffer->need_line = true;
2553 pfile->buffer = new_buffer;
2555 return new_buffer;
2558 /* Pops a single buffer, with a file change call-back if appropriate.
2559 Then pushes the next -include file, if any remain. */
2560 void
2561 _cpp_pop_buffer (cpp_reader *pfile)
2563 cpp_buffer *buffer = pfile->buffer;
2564 struct _cpp_file *inc = buffer->file;
2565 struct if_stack *ifs;
2566 const unsigned char *to_free;
2568 /* Walk back up the conditional stack till we reach its level at
2569 entry to this file, issuing error messages. */
2570 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2571 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2572 "unterminated #%s", dtable[ifs->type].name);
2574 /* In case of a missing #endif. */
2575 pfile->state.skipping = 0;
2577 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2578 pfile->buffer = buffer->prev;
2580 to_free = buffer->to_free;
2581 free (buffer->notes);
2583 /* Free the buffer object now; we may want to push a new buffer
2584 in _cpp_push_next_include_file. */
2585 obstack_free (&pfile->buffer_ob, buffer);
2587 if (inc)
2589 _cpp_pop_file_buffer (pfile, inc, to_free);
2591 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2595 /* Enter all recognized directives in the hash table. */
2596 void
2597 _cpp_init_directives (cpp_reader *pfile)
2599 unsigned int i;
2600 cpp_hashnode *node;
2602 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2604 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2605 node->is_directive = 1;
2606 node->directive_index = i;
2610 /* Extract header file from a bracket include. Parsing starts after '<'.
2611 The string is malloced and must be freed by the caller. */
2612 char *
2613 _cpp_bracket_include(cpp_reader *pfile)
2615 return glue_header_name (pfile);