2009-10-02 Tobias Burnus <burnus@net-b.de>
[official-gcc/alias-decl.git] / libcpp / directives.c
blobc5a1895b86eedb05a31a958abc835d8c20d3682b
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 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 linenum_type 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);
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);
130 /* This is the table of directive handlers. It is ordered by
131 frequency of occurrence; the numbers at the end are directive
132 counts from all the source code I have lying around (egcs and libc
133 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
134 pcmcia-cs-3.0.9). This is no longer important as directive lookup
135 is now O(1). All extensions other than #warning, #include_next,
136 and #import are deprecated. The name is where the extension
137 appears to have come from. */
139 #define DIRECTIVE_TABLE \
140 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
141 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
142 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
143 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
144 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
145 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
146 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
147 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
148 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
149 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
150 D(error, T_ERROR, STDC89, 0) /* 475 */ \
151 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
152 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
153 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
154 D(ident, T_IDENT, EXTENSION, IN_I | DEPRECATED) /* 11 */ \
155 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
156 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
157 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
158 D(sccs, T_SCCS, EXTENSION, IN_I | DEPRECATED) /* 0 SVR4? */
160 /* #sccs is synonymous with #ident. */
161 #define do_sccs do_ident
163 /* Use the table to generate a series of prototypes, an enum for the
164 directive names, and an array of directive handlers. */
166 #define D(name, t, o, f) static void do_##name (cpp_reader *);
167 DIRECTIVE_TABLE
168 #undef D
170 #define D(n, tag, o, f) tag,
171 enum
173 DIRECTIVE_TABLE
174 N_DIRECTIVES
176 #undef D
178 #define D(name, t, origin, flags) \
179 { do_##name, (const uchar *) #name, \
180 sizeof #name - 1, origin, flags },
181 static const directive dtable[] =
183 DIRECTIVE_TABLE
185 #undef D
186 #undef DIRECTIVE_TABLE
188 /* Wrapper struct directive for linemarkers.
189 The origin is more or less true - the original K+R cpp
190 did use this notation in its preprocessed output. */
191 static const directive linemarker_dir =
193 do_linemarker, UC"#", 1, KANDR, IN_I
196 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
198 /* Skip any remaining tokens in a directive. */
199 static void
200 skip_rest_of_line (cpp_reader *pfile)
202 /* Discard all stacked contexts. */
203 while (pfile->context->prev)
204 _cpp_pop_context (pfile);
206 /* Sweep up all tokens remaining on the line. */
207 if (! SEEN_EOL ())
208 while (_cpp_lex_token (pfile)->type != CPP_EOF)
212 /* Ensure there are no stray tokens at the end of a directive. If
213 EXPAND is true, tokens macro-expanding to nothing are allowed. */
214 static void
215 check_eol (cpp_reader *pfile, bool expand)
217 if (! SEEN_EOL () && (expand
218 ? cpp_get_token (pfile)
219 : _cpp_lex_token (pfile))->type != CPP_EOF)
220 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
221 pfile->directive->name);
224 /* Ensure there are no stray tokens other than comments at the end of
225 a directive, and gather the comments. */
226 static const cpp_token **
227 check_eol_return_comments (cpp_reader *pfile)
229 size_t c;
230 size_t capacity = 8;
231 const cpp_token **buf;
233 buf = XNEWVEC (const cpp_token *, capacity);
234 c = 0;
235 if (! SEEN_EOL ())
237 while (1)
239 const cpp_token *tok;
241 tok = _cpp_lex_token (pfile);
242 if (tok->type == CPP_EOF)
243 break;
244 if (tok->type != CPP_COMMENT)
245 cpp_error (pfile, CPP_DL_PEDWARN,
246 "extra tokens at end of #%s directive",
247 pfile->directive->name);
248 else
250 if (c + 1 >= capacity)
252 capacity *= 2;
253 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
255 buf[c] = tok;
256 ++c;
260 buf[c] = NULL;
261 return buf;
264 /* Called when entering a directive, _Pragma or command-line directive. */
265 static void
266 start_directive (cpp_reader *pfile)
268 /* Setup in-directive state. */
269 pfile->state.in_directive = 1;
270 pfile->state.save_comments = 0;
271 pfile->directive_result.type = CPP_PADDING;
273 /* Some handlers need the position of the # for diagnostics. */
274 pfile->directive_line = pfile->line_table->highest_line;
277 /* Called when leaving a directive, _Pragma or command-line directive. */
278 static void
279 end_directive (cpp_reader *pfile, int skip_line)
281 if (pfile->state.in_deferred_pragma)
283 else if (CPP_OPTION (pfile, traditional))
285 /* Revert change of prepare_directive_trad. */
286 pfile->state.prevent_expansion--;
288 if (pfile->directive != &dtable[T_DEFINE])
289 _cpp_remove_overlay (pfile);
291 /* We don't skip for an assembler #. */
292 else if (skip_line)
294 skip_rest_of_line (pfile);
295 if (!pfile->keep_tokens)
297 pfile->cur_run = &pfile->base_run;
298 pfile->cur_token = pfile->base_run.base;
302 /* Restore state. */
303 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
304 pfile->state.in_directive = 0;
305 pfile->state.in_expression = 0;
306 pfile->state.angled_headers = 0;
307 pfile->directive = 0;
310 /* Prepare to handle the directive in pfile->directive. */
311 static void
312 prepare_directive_trad (cpp_reader *pfile)
314 if (pfile->directive != &dtable[T_DEFINE])
316 bool no_expand = (pfile->directive
317 && ! (pfile->directive->flags & EXPAND));
318 bool was_skipping = pfile->state.skipping;
320 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
321 || pfile->directive == &dtable[T_ELIF]);
322 if (pfile->state.in_expression)
323 pfile->state.skipping = false;
325 if (no_expand)
326 pfile->state.prevent_expansion++;
327 _cpp_scan_out_logical_line (pfile, NULL);
328 if (no_expand)
329 pfile->state.prevent_expansion--;
331 pfile->state.skipping = was_skipping;
332 _cpp_overlay_buffer (pfile, pfile->out.base,
333 pfile->out.cur - pfile->out.base);
336 /* Stop ISO C from expanding anything. */
337 pfile->state.prevent_expansion++;
340 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
341 the '#' was indented. */
342 static void
343 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
345 /* Issue -pedantic or deprecated warnings for extensions. We let
346 -pedantic take precedence if both are applicable. */
347 if (! pfile->state.skipping)
349 if (dir->origin == EXTENSION
350 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
351 && CPP_PEDANTIC (pfile))
352 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
353 else if (((dir->flags & DEPRECATED) != 0
354 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
355 && CPP_OPTION (pfile, warn_deprecated))
356 cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension",
357 dir->name);
360 /* Traditionally, a directive is ignored unless its # is in
361 column 1. Therefore in code intended to work with K+R
362 compilers, directives added by C89 must have their #
363 indented, and directives present in traditional C must not.
364 This is true even of directives in skipped conditional
365 blocks. #elif cannot be used at all. */
366 if (CPP_WTRADITIONAL (pfile))
368 if (dir == &dtable[T_ELIF])
369 cpp_error (pfile, CPP_DL_WARNING,
370 "suggest not using #elif in traditional C");
371 else if (indented && dir->origin == KANDR)
372 cpp_error (pfile, CPP_DL_WARNING,
373 "traditional C ignores #%s with the # indented",
374 dir->name);
375 else if (!indented && dir->origin != KANDR)
376 cpp_error (pfile, CPP_DL_WARNING,
377 "suggest hiding #%s from traditional C with an indented #",
378 dir->name);
382 /* Check if we have a known directive. INDENTED is nonzero if the
383 '#' of the directive was indented. This function is in this file
384 to save unnecessarily exporting dtable etc. to lex.c. Returns
385 nonzero if the line of tokens has been handled, zero if we should
386 continue processing the line. */
388 _cpp_handle_directive (cpp_reader *pfile, int indented)
390 const directive *dir = 0;
391 const cpp_token *dname;
392 bool was_parsing_args = pfile->state.parsing_args;
393 bool was_discarding_output = pfile->state.discarding_output;
394 int skip = 1;
396 if (was_discarding_output)
397 pfile->state.prevent_expansion = 0;
399 if (was_parsing_args)
401 if (CPP_OPTION (pfile, pedantic))
402 cpp_error (pfile, CPP_DL_PEDWARN,
403 "embedding a directive within macro arguments is not portable");
404 pfile->state.parsing_args = 0;
405 pfile->state.prevent_expansion = 0;
407 start_directive (pfile);
408 dname = _cpp_lex_token (pfile);
410 if (dname->type == CPP_NAME)
412 if (dname->val.node.node->is_directive)
413 dir = &dtable[dname->val.node.node->directive_index];
415 /* We do not recognize the # followed by a number extension in
416 assembler code. */
417 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
419 dir = &linemarker_dir;
420 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
421 && ! pfile->state.skipping)
422 cpp_error (pfile, CPP_DL_PEDWARN,
423 "style of line directive is a GCC extension");
426 if (dir)
428 /* If we have a directive that is not an opening conditional,
429 invalidate any control macro. */
430 if (! (dir->flags & IF_COND))
431 pfile->mi_valid = false;
433 /* Kluge alert. In order to be sure that code like this
435 #define HASH #
436 HASH define foo bar
438 does not cause '#define foo bar' to get executed when
439 compiled with -save-temps, we recognize directives in
440 -fpreprocessed mode only if the # is in column 1. macro.c
441 puts a space in front of any '#' at the start of a macro.
443 We exclude the -fdirectives-only case because macro expansion
444 has not been performed yet, and block comments can cause spaces
445 to preceed the directive. */
446 if (CPP_OPTION (pfile, preprocessed)
447 && !CPP_OPTION (pfile, directives_only)
448 && (indented || !(dir->flags & IN_I)))
450 skip = 0;
451 dir = 0;
453 else
455 /* In failed conditional groups, all non-conditional
456 directives are ignored. Before doing that, whether
457 skipping or not, we should lex angle-bracketed headers
458 correctly, and maybe output some diagnostics. */
459 pfile->state.angled_headers = dir->flags & INCL;
460 pfile->state.directive_wants_padding = dir->flags & INCL;
461 if (! CPP_OPTION (pfile, preprocessed))
462 directive_diagnostics (pfile, dir, indented);
463 if (pfile->state.skipping && !(dir->flags & COND))
464 dir = 0;
467 else if (dname->type == CPP_EOF)
468 ; /* CPP_EOF is the "null directive". */
469 else
471 /* An unknown directive. Don't complain about it in assembly
472 source: we don't know where the comments are, and # may
473 introduce assembler pseudo-ops. Don't complain about invalid
474 directives in skipped conditional groups (6.10 p4). */
475 if (CPP_OPTION (pfile, lang) == CLK_ASM)
476 skip = 0;
477 else if (!pfile->state.skipping)
478 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
479 cpp_token_as_text (pfile, dname));
482 pfile->directive = dir;
483 if (CPP_OPTION (pfile, traditional))
484 prepare_directive_trad (pfile);
486 if (dir)
487 pfile->directive->handler (pfile);
488 else if (skip == 0)
489 _cpp_backup_tokens (pfile, 1);
491 end_directive (pfile, skip);
492 if (was_parsing_args && !pfile->state.in_deferred_pragma)
494 /* Restore state when within macro args. */
495 pfile->state.parsing_args = 2;
496 pfile->state.prevent_expansion = 1;
498 if (was_discarding_output)
499 pfile->state.prevent_expansion = 1;
500 return skip;
503 /* Directive handler wrapper used by the command line option
504 processor. BUF is \n terminated. */
505 static void
506 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
508 cpp_push_buffer (pfile, (const uchar *) buf, count,
509 /* from_stage3 */ true);
510 start_directive (pfile);
512 /* This is a short-term fix to prevent a leading '#' being
513 interpreted as a directive. */
514 _cpp_clean_line (pfile);
516 pfile->directive = &dtable[dir_no];
517 if (CPP_OPTION (pfile, traditional))
518 prepare_directive_trad (pfile);
519 pfile->directive->handler (pfile);
520 end_directive (pfile, 1);
521 _cpp_pop_buffer (pfile);
524 /* Checks for validity the macro name in #define, #undef, #ifdef and
525 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
526 processing a #define or #undefine directive, and false
527 otherwise. */
528 static cpp_hashnode *
529 lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
531 const cpp_token *token = _cpp_lex_token (pfile);
533 /* The token immediately after #define must be an identifier. That
534 identifier may not be "defined", per C99 6.10.8p4.
535 In C++, it may not be any of the "named operators" either,
536 per C++98 [lex.digraph], [lex.key].
537 Finally, the identifier may not have been poisoned. (In that case
538 the lexer has issued the error message for us.) */
540 if (token->type == CPP_NAME)
542 cpp_hashnode *node = token->val.node.node;
544 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
545 cpp_error (pfile, CPP_DL_ERROR,
546 "\"defined\" cannot be used as a macro name");
547 else if (! (node->flags & NODE_POISONED))
548 return node;
550 else if (token->flags & NAMED_OP)
551 cpp_error (pfile, CPP_DL_ERROR,
552 "\"%s\" cannot be used as a macro name as it is an operator in C++",
553 NODE_NAME (token->val.node.node));
554 else if (token->type == CPP_EOF)
555 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
556 pfile->directive->name);
557 else
558 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
560 return NULL;
563 /* Process a #define directive. Most work is done in macro.c. */
564 static void
565 do_define (cpp_reader *pfile)
567 cpp_hashnode *node = lex_macro_node (pfile, true);
569 if (node)
571 /* If we have been requested to expand comments into macros,
572 then re-enable saving of comments. */
573 pfile->state.save_comments =
574 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
576 if (pfile->cb.before_define)
577 pfile->cb.before_define (pfile);
579 if (_cpp_create_definition (pfile, node))
580 if (pfile->cb.define)
581 pfile->cb.define (pfile, pfile->directive_line, node);
583 node->flags &= ~NODE_USED;
587 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
588 static void
589 do_undef (cpp_reader *pfile)
591 cpp_hashnode *node = lex_macro_node (pfile, true);
593 if (node)
595 if (pfile->cb.before_define)
596 pfile->cb.before_define (pfile);
598 if (pfile->cb.undef)
599 pfile->cb.undef (pfile, pfile->directive_line, node);
601 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
602 identifier is not currently defined as a macro name. */
603 if (node->type == NT_MACRO)
605 if (node->flags & NODE_WARN)
606 cpp_error (pfile, CPP_DL_WARNING,
607 "undefining \"%s\"", NODE_NAME (node));
609 if (CPP_OPTION (pfile, warn_unused_macros))
610 _cpp_warn_if_unused_macro (pfile, node, NULL);
612 _cpp_free_definition (node);
616 check_eol (pfile, false);
619 /* Undefine a single macro/assertion/whatever. */
621 static int
622 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
623 void *data_p ATTRIBUTE_UNUSED)
625 /* Body of _cpp_free_definition inlined here for speed.
626 Macros and assertions no longer have anything to free. */
627 h->type = NT_VOID;
628 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
629 return 1;
632 /* Undefine all macros and assertions. */
634 void
635 cpp_undef_all (cpp_reader *pfile)
637 cpp_forall_identifiers (pfile, undefine_macros, NULL);
641 /* Helper routine used by parse_include. Reinterpret the current line
642 as an h-char-sequence (< ... >); we are looking at the first token
643 after the <. Returns a malloced filename. */
644 static char *
645 glue_header_name (cpp_reader *pfile)
647 const cpp_token *token;
648 char *buffer;
649 size_t len, total_len = 0, capacity = 1024;
651 /* To avoid lexed tokens overwriting our glued name, we can only
652 allocate from the string pool once we've lexed everything. */
653 buffer = XNEWVEC (char, capacity);
654 for (;;)
656 token = get_token_no_padding (pfile);
658 if (token->type == CPP_GREATER)
659 break;
660 if (token->type == CPP_EOF)
662 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
663 break;
666 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
667 if (total_len + len > capacity)
669 capacity = (capacity + len) * 2;
670 buffer = XRESIZEVEC (char, buffer, capacity);
673 if (token->flags & PREV_WHITE)
674 buffer[total_len++] = ' ';
676 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
677 true)
678 - (uchar *) buffer);
681 buffer[total_len] = '\0';
682 return buffer;
685 /* Returns the file name of #include, #include_next, #import and
686 #pragma dependency. The string is malloced and the caller should
687 free it. Returns NULL on error. LOCATION is the source location
688 of the file name. */
690 static const char *
691 parse_include (cpp_reader *pfile, int *pangle_brackets,
692 const cpp_token ***buf, source_location *location)
694 char *fname;
695 const cpp_token *header;
697 /* Allow macro expansion. */
698 header = get_token_no_padding (pfile);
699 *location = header->src_loc;
700 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
702 fname = XNEWVEC (char, header->val.str.len - 1);
703 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
704 fname[header->val.str.len - 2] = '\0';
705 *pangle_brackets = header->type == CPP_HEADER_NAME;
707 else if (header->type == CPP_LESS)
709 fname = glue_header_name (pfile);
710 *pangle_brackets = 1;
712 else
714 const unsigned char *dir;
716 if (pfile->directive == &dtable[T_PRAGMA])
717 dir = UC"pragma dependency";
718 else
719 dir = pfile->directive->name;
720 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
721 dir);
723 return NULL;
726 if (pfile->directive == &dtable[T_PRAGMA])
728 /* This pragma allows extra tokens after the file name. */
730 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
731 check_eol (pfile, true);
732 else
734 /* If we are not discarding comments, then gather them while
735 doing the eol check. */
736 *buf = check_eol_return_comments (pfile);
739 return fname;
742 /* Handle #include, #include_next and #import. */
743 static void
744 do_include_common (cpp_reader *pfile, enum include_type type)
746 const char *fname;
747 int angle_brackets;
748 const cpp_token **buf = NULL;
749 source_location location;
751 /* Re-enable saving of comments if requested, so that the include
752 callback can dump comments which follow #include. */
753 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
755 fname = parse_include (pfile, &angle_brackets, &buf, &location);
756 if (!fname)
758 if (buf)
759 XDELETEVEC (buf);
760 return;
763 if (!*fname)
765 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
766 "empty filename in #%s",
767 pfile->directive->name);
768 XDELETEVEC (fname);
769 if (buf)
770 XDELETEVEC (buf);
771 return;
774 /* Prevent #include recursion. */
775 if (pfile->line_table->depth >= CPP_STACK_MAX)
776 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
777 else
779 /* Get out of macro context, if we are. */
780 skip_rest_of_line (pfile);
782 if (pfile->cb.include)
783 pfile->cb.include (pfile, pfile->directive_line,
784 pfile->directive->name, fname, angle_brackets,
785 buf);
787 _cpp_stack_include (pfile, fname, angle_brackets, type);
790 XDELETEVEC (fname);
791 if (buf)
792 XDELETEVEC (buf);
795 static void
796 do_include (cpp_reader *pfile)
798 do_include_common (pfile, IT_INCLUDE);
801 static void
802 do_import (cpp_reader *pfile)
804 do_include_common (pfile, IT_IMPORT);
807 static void
808 do_include_next (cpp_reader *pfile)
810 enum include_type type = IT_INCLUDE_NEXT;
812 /* If this is the primary source file, warn and use the normal
813 search logic. */
814 if (cpp_in_primary_file (pfile))
816 cpp_error (pfile, CPP_DL_WARNING,
817 "#include_next in primary source file");
818 type = IT_INCLUDE;
820 do_include_common (pfile, type);
823 /* Subroutine of do_linemarker. Read possible flags after file name.
824 LAST is the last flag seen; 0 if this is the first flag. Return the
825 flag if it is valid, 0 at the end of the directive. Otherwise
826 complain. */
827 static unsigned int
828 read_flag (cpp_reader *pfile, unsigned int last)
830 const cpp_token *token = _cpp_lex_token (pfile);
832 if (token->type == CPP_NUMBER && token->val.str.len == 1)
834 unsigned int flag = token->val.str.text[0] - '0';
836 if (flag > last && flag <= 4
837 && (flag != 4 || last == 3)
838 && (flag != 2 || last == 0))
839 return flag;
842 if (token->type != CPP_EOF)
843 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
844 cpp_token_as_text (pfile, token));
845 return 0;
848 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
849 of length LEN, to binary; store it in NUMP, and return false if the
850 number was well-formed, true if not. WRAPPED is set to true if the
851 number did not fit into 'unsigned long'. */
852 static bool
853 strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
855 linenum_type reg = 0;
856 linenum_type reg_prev = 0;
858 uchar c;
859 *wrapped = false;
860 while (len--)
862 c = *str++;
863 if (!ISDIGIT (c))
864 return true;
865 reg *= 10;
866 reg += c - '0';
867 if (reg < reg_prev)
868 *wrapped = true;
869 reg_prev = reg;
871 *nump = reg;
872 return false;
875 /* Interpret #line command.
876 Note that the filename string (if any) is a true string constant
877 (escapes are interpreted), unlike in #line. */
878 static void
879 do_line (cpp_reader *pfile)
881 const struct line_maps *line_table = pfile->line_table;
882 const struct line_map *map = &line_table->maps[line_table->used - 1];
884 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
885 sysp right now. */
887 unsigned char map_sysp = map->sysp;
888 const cpp_token *token;
889 const char *new_file = map->to_file;
890 linenum_type new_lineno;
892 /* C99 raised the minimum limit on #line numbers. */
893 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
894 bool wrapped;
896 /* #line commands expand macros. */
897 token = cpp_get_token (pfile);
898 if (token->type != CPP_NUMBER
899 || strtolinenum (token->val.str.text, token->val.str.len,
900 &new_lineno, &wrapped))
902 if (token->type == CPP_EOF)
903 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
904 else
905 cpp_error (pfile, CPP_DL_ERROR,
906 "\"%s\" after #line is not a positive integer",
907 cpp_token_as_text (pfile, token));
908 return;
911 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
912 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
913 else if (wrapped)
914 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
916 token = cpp_get_token (pfile);
917 if (token->type == CPP_STRING)
919 cpp_string s = { 0, 0 };
920 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
921 &s, CPP_STRING))
922 new_file = (const char *)s.text;
923 check_eol (pfile, true);
925 else if (token->type != CPP_EOF)
927 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
928 cpp_token_as_text (pfile, token));
929 return;
932 skip_rest_of_line (pfile);
933 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
934 map_sysp);
937 /* Interpret the # 44 "file" [flags] notation, which has slightly
938 different syntax and semantics from #line: Flags are allowed,
939 and we never complain about the line number being too big. */
940 static void
941 do_linemarker (cpp_reader *pfile)
943 const struct line_maps *line_table = pfile->line_table;
944 const struct line_map *map = &line_table->maps[line_table->used - 1];
945 const cpp_token *token;
946 const char *new_file = map->to_file;
947 linenum_type new_lineno;
948 unsigned int new_sysp = map->sysp;
949 enum lc_reason reason = LC_RENAME_VERBATIM;
950 int flag;
951 bool wrapped;
953 /* Back up so we can get the number again. Putting this in
954 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
955 some circumstances, which can segfault. */
956 _cpp_backup_tokens (pfile, 1);
958 /* #line commands expand macros. */
959 token = cpp_get_token (pfile);
960 if (token->type != CPP_NUMBER
961 || strtolinenum (token->val.str.text, token->val.str.len,
962 &new_lineno, &wrapped))
964 /* Unlike #line, there does not seem to be a way to get an EOF
965 here. So, it should be safe to always spell the token. */
966 cpp_error (pfile, CPP_DL_ERROR,
967 "\"%s\" after # is not a positive integer",
968 cpp_token_as_text (pfile, token));
969 return;
972 token = cpp_get_token (pfile);
973 if (token->type == CPP_STRING)
975 cpp_string s = { 0, 0 };
976 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
977 1, &s, CPP_STRING))
978 new_file = (const char *)s.text;
980 new_sysp = 0;
981 flag = read_flag (pfile, 0);
982 if (flag == 1)
984 reason = LC_ENTER;
985 /* Fake an include for cpp_included (). */
986 _cpp_fake_include (pfile, new_file);
987 flag = read_flag (pfile, flag);
989 else if (flag == 2)
991 reason = LC_LEAVE;
992 flag = read_flag (pfile, flag);
994 if (flag == 3)
996 new_sysp = 1;
997 flag = read_flag (pfile, flag);
998 if (flag == 4)
999 new_sysp = 2;
1001 pfile->buffer->sysp = new_sysp;
1003 check_eol (pfile, false);
1005 else if (token->type != CPP_EOF)
1007 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
1008 cpp_token_as_text (pfile, token));
1009 return;
1012 skip_rest_of_line (pfile);
1014 /* Compensate for the increment in linemap_add that occurs in
1015 _cpp_do_file_change. We're currently at the start of the line
1016 *following* the #line directive. A separate source_location for this
1017 location makes no sense (until we do the LC_LEAVE), and
1018 complicates LAST_SOURCE_LINE_LOCATION. */
1019 pfile->line_table->highest_location--;
1021 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1024 /* Arrange the file_change callback. pfile->line has changed to
1025 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1026 header, 2 for a system header that needs to be extern "C" protected,
1027 and zero otherwise. */
1028 void
1029 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1030 const char *to_file, linenum_type file_line,
1031 unsigned int sysp)
1033 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1034 to_file, file_line);
1035 if (map != NULL)
1036 linemap_line_start (pfile->line_table, map->to_line, 127);
1038 if (pfile->cb.file_change)
1039 pfile->cb.file_change (pfile, map);
1042 /* Report a warning or error detected by the program we are
1043 processing. Use the directive's tokens in the error message. */
1044 static void
1045 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
1047 const unsigned char *dir_name;
1048 unsigned char *line;
1049 source_location src_loc = pfile->cur_token[-1].src_loc;
1051 if (print_dir)
1052 dir_name = pfile->directive->name;
1053 else
1054 dir_name = NULL;
1055 pfile->state.prevent_expansion++;
1056 line = cpp_output_line_to_string (pfile, dir_name);
1057 pfile->state.prevent_expansion--;
1059 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1060 free (line);
1063 static void
1064 do_error (cpp_reader *pfile)
1066 do_diagnostic (pfile, CPP_DL_ERROR, 1);
1069 static void
1070 do_warning (cpp_reader *pfile)
1072 /* We want #warning diagnostics to be emitted in system headers too. */
1073 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1076 /* Report program identification. */
1077 static void
1078 do_ident (cpp_reader *pfile)
1080 const cpp_token *str = cpp_get_token (pfile);
1082 if (str->type != CPP_STRING)
1083 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1084 pfile->directive->name);
1085 else if (pfile->cb.ident)
1086 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1088 check_eol (pfile, false);
1091 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1092 matching entry, or NULL if none is found. The returned entry could
1093 be the start of a namespace chain, or a pragma. */
1094 static struct pragma_entry *
1095 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1097 while (chain && chain->pragma != pragma)
1098 chain = chain->next;
1100 return chain;
1103 /* Create and insert a blank pragma entry at the beginning of a
1104 singly-linked CHAIN. */
1105 static struct pragma_entry *
1106 new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1108 struct pragma_entry *new_entry;
1110 new_entry = (struct pragma_entry *)
1111 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1113 memset (new_entry, 0, sizeof (struct pragma_entry));
1114 new_entry->next = *chain;
1116 *chain = new_entry;
1117 return new_entry;
1120 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1121 goes in the global namespace. */
1122 static struct pragma_entry *
1123 register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1124 bool allow_name_expansion)
1126 struct pragma_entry **chain = &pfile->pragmas;
1127 struct pragma_entry *entry;
1128 const cpp_hashnode *node;
1130 if (space)
1132 node = cpp_lookup (pfile, UC space, strlen (space));
1133 entry = lookup_pragma_entry (*chain, node);
1134 if (!entry)
1136 entry = new_pragma_entry (pfile, chain);
1137 entry->pragma = node;
1138 entry->is_nspace = true;
1139 entry->allow_expansion = allow_name_expansion;
1141 else if (!entry->is_nspace)
1142 goto clash;
1143 else if (entry->allow_expansion != allow_name_expansion)
1145 cpp_error (pfile, CPP_DL_ICE,
1146 "registering pragmas in namespace \"%s\" with mismatched "
1147 "name expansion", space);
1148 return NULL;
1150 chain = &entry->u.space;
1152 else if (allow_name_expansion)
1154 cpp_error (pfile, CPP_DL_ICE,
1155 "registering pragma \"%s\" with name expansion "
1156 "and no namespace", name);
1157 return NULL;
1160 /* Check for duplicates. */
1161 node = cpp_lookup (pfile, UC name, strlen (name));
1162 entry = lookup_pragma_entry (*chain, node);
1163 if (entry == NULL)
1165 entry = new_pragma_entry (pfile, chain);
1166 entry->pragma = node;
1167 return entry;
1170 if (entry->is_nspace)
1171 clash:
1172 cpp_error (pfile, CPP_DL_ICE,
1173 "registering \"%s\" as both a pragma and a pragma namespace",
1174 NODE_NAME (node));
1175 else if (space)
1176 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1177 space, name);
1178 else
1179 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1181 return NULL;
1184 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1185 static void
1186 register_pragma_internal (cpp_reader *pfile, const char *space,
1187 const char *name, pragma_cb handler)
1189 struct pragma_entry *entry;
1191 entry = register_pragma_1 (pfile, space, name, false);
1192 entry->is_internal = true;
1193 entry->u.handler = handler;
1196 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1197 goes in the global namespace. HANDLER is the handler it will call,
1198 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1199 expansion while parsing pragma NAME. This function is exported
1200 from libcpp. */
1201 void
1202 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1203 pragma_cb handler, bool allow_expansion)
1205 struct pragma_entry *entry;
1207 if (!handler)
1209 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1210 return;
1213 entry = register_pragma_1 (pfile, space, name, false);
1214 if (entry)
1216 entry->allow_expansion = allow_expansion;
1217 entry->u.handler = handler;
1221 /* Similarly, but create mark the pragma for deferred processing.
1222 When found, a CPP_PRAGMA token will be insertted into the stream
1223 with IDENT in the token->u.pragma slot. */
1224 void
1225 cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1226 const char *name, unsigned int ident,
1227 bool allow_expansion, bool allow_name_expansion)
1229 struct pragma_entry *entry;
1231 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1232 if (entry)
1234 entry->is_deferred = true;
1235 entry->allow_expansion = allow_expansion;
1236 entry->u.ident = ident;
1240 /* Register the pragmas the preprocessor itself handles. */
1241 void
1242 _cpp_init_internal_pragmas (cpp_reader *pfile)
1244 /* Pragmas in the global namespace. */
1245 register_pragma_internal (pfile, 0, "once", do_pragma_once);
1247 /* New GCC-specific pragmas should be put in the GCC namespace. */
1248 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1249 register_pragma_internal (pfile, "GCC", "system_header",
1250 do_pragma_system_header);
1251 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1254 /* Return the number of registered pragmas in PE. */
1256 static int
1257 count_registered_pragmas (struct pragma_entry *pe)
1259 int ct = 0;
1260 for (; pe != NULL; pe = pe->next)
1262 if (pe->is_nspace)
1263 ct += count_registered_pragmas (pe->u.space);
1264 ct++;
1266 return ct;
1269 /* Save into SD the names of the registered pragmas referenced by PE,
1270 and return a pointer to the next free space in SD. */
1272 static char **
1273 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1275 for (; pe != NULL; pe = pe->next)
1277 if (pe->is_nspace)
1278 sd = save_registered_pragmas (pe->u.space, sd);
1279 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1280 HT_LEN (&pe->pragma->ident),
1281 HT_LEN (&pe->pragma->ident) + 1);
1283 return sd;
1286 /* Return a newly-allocated array which saves the names of the
1287 registered pragmas. */
1289 char **
1290 _cpp_save_pragma_names (cpp_reader *pfile)
1292 int ct = count_registered_pragmas (pfile->pragmas);
1293 char **result = XNEWVEC (char *, ct);
1294 (void) save_registered_pragmas (pfile->pragmas, result);
1295 return result;
1298 /* Restore from SD the names of the registered pragmas referenced by PE,
1299 and return a pointer to the next unused name in SD. */
1301 static char **
1302 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1303 char **sd)
1305 for (; pe != NULL; pe = pe->next)
1307 if (pe->is_nspace)
1308 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1309 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
1310 free (*sd);
1311 sd++;
1313 return sd;
1316 /* Restore the names of the registered pragmas from SAVED. */
1318 void
1319 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1321 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1322 free (saved);
1325 /* Pragmata handling. We handle some, and pass the rest on to the
1326 front end. C99 defines three pragmas and says that no macro
1327 expansion is to be performed on them; whether or not macro
1328 expansion happens for other pragmas is implementation defined.
1329 This implementation allows for a mix of both, since GCC did not
1330 traditionally macro expand its (few) pragmas, whereas OpenMP
1331 specifies that macro expansion should happen. */
1332 static void
1333 do_pragma (cpp_reader *pfile)
1335 const struct pragma_entry *p = NULL;
1336 const cpp_token *token, *pragma_token = pfile->cur_token;
1337 cpp_token ns_token;
1338 unsigned int count = 1;
1340 pfile->state.prevent_expansion++;
1342 token = cpp_get_token (pfile);
1343 ns_token = *token;
1344 if (token->type == CPP_NAME)
1346 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
1347 if (p && p->is_nspace)
1349 bool allow_name_expansion = p->allow_expansion;
1350 if (allow_name_expansion)
1351 pfile->state.prevent_expansion--;
1352 token = cpp_get_token (pfile);
1353 if (token->type == CPP_NAME)
1354 p = lookup_pragma_entry (p->u.space, token->val.node.node);
1355 else
1356 p = NULL;
1357 if (allow_name_expansion)
1358 pfile->state.prevent_expansion++;
1359 count = 2;
1363 if (p)
1365 if (p->is_deferred)
1367 pfile->directive_result.src_loc = pragma_token->src_loc;
1368 pfile->directive_result.type = CPP_PRAGMA;
1369 pfile->directive_result.flags = pragma_token->flags;
1370 pfile->directive_result.val.pragma = p->u.ident;
1371 pfile->state.in_deferred_pragma = true;
1372 pfile->state.pragma_allow_expansion = p->allow_expansion;
1373 if (!p->allow_expansion)
1374 pfile->state.prevent_expansion++;
1376 else
1378 /* Since the handler below doesn't get the line number, that
1379 it might need for diagnostics, make sure it has the right
1380 numbers in place. */
1381 if (pfile->cb.line_change)
1382 (*pfile->cb.line_change) (pfile, pragma_token, false);
1383 if (p->allow_expansion)
1384 pfile->state.prevent_expansion--;
1385 (*p->u.handler) (pfile);
1386 if (p->allow_expansion)
1387 pfile->state.prevent_expansion++;
1390 else if (pfile->cb.def_pragma)
1392 if (count == 1 || pfile->context->prev == NULL)
1393 _cpp_backup_tokens (pfile, count);
1394 else
1396 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1397 won't allow backing 2 tokens. */
1398 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1399 reads both tokens, we could perhaps free it, but if it doesn't,
1400 we don't know the exact lifespan. */
1401 cpp_token *toks = XNEWVEC (cpp_token, 2);
1402 toks[0] = ns_token;
1403 toks[0].flags |= NO_EXPAND;
1404 toks[1] = *token;
1405 toks[1].flags |= NO_EXPAND;
1406 _cpp_push_token_context (pfile, NULL, toks, 2);
1408 pfile->cb.def_pragma (pfile, pfile->directive_line);
1411 pfile->state.prevent_expansion--;
1414 /* Handle #pragma once. */
1415 static void
1416 do_pragma_once (cpp_reader *pfile)
1418 if (cpp_in_primary_file (pfile))
1419 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1421 check_eol (pfile, false);
1422 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1425 /* Handle #pragma GCC poison, to poison one or more identifiers so
1426 that the lexer produces a hard error for each subsequent usage. */
1427 static void
1428 do_pragma_poison (cpp_reader *pfile)
1430 const cpp_token *tok;
1431 cpp_hashnode *hp;
1433 pfile->state.poisoned_ok = 1;
1434 for (;;)
1436 tok = _cpp_lex_token (pfile);
1437 if (tok->type == CPP_EOF)
1438 break;
1439 if (tok->type != CPP_NAME)
1441 cpp_error (pfile, CPP_DL_ERROR,
1442 "invalid #pragma GCC poison directive");
1443 break;
1446 hp = tok->val.node.node;
1447 if (hp->flags & NODE_POISONED)
1448 continue;
1450 if (hp->type == NT_MACRO)
1451 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1452 NODE_NAME (hp));
1453 _cpp_free_definition (hp);
1454 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1456 pfile->state.poisoned_ok = 0;
1459 /* Mark the current header as a system header. This will suppress
1460 some categories of warnings (notably those from -pedantic). It is
1461 intended for use in system libraries that cannot be implemented in
1462 conforming C, but cannot be certain that their headers appear in a
1463 system include directory. To prevent abuse, it is rejected in the
1464 primary source file. */
1465 static void
1466 do_pragma_system_header (cpp_reader *pfile)
1468 if (cpp_in_primary_file (pfile))
1469 cpp_error (pfile, CPP_DL_WARNING,
1470 "#pragma system_header ignored outside include file");
1471 else
1473 check_eol (pfile, false);
1474 skip_rest_of_line (pfile);
1475 cpp_make_system_header (pfile, 1, 0);
1479 /* Check the modified date of the current include file against a specified
1480 file. Issue a diagnostic, if the specified file is newer. We use this to
1481 determine if a fixed header should be refixed. */
1482 static void
1483 do_pragma_dependency (cpp_reader *pfile)
1485 const char *fname;
1486 int angle_brackets, ordering;
1487 source_location location;
1489 fname = parse_include (pfile, &angle_brackets, NULL, &location);
1490 if (!fname)
1491 return;
1493 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1494 if (ordering < 0)
1495 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1496 else if (ordering > 0)
1498 cpp_error (pfile, CPP_DL_WARNING,
1499 "current file is older than %s", fname);
1500 if (cpp_get_token (pfile)->type != CPP_EOF)
1502 _cpp_backup_tokens (pfile, 1);
1503 do_diagnostic (pfile, CPP_DL_WARNING, 0);
1507 free ((void *) fname);
1510 /* Get a token but skip padding. */
1511 static const cpp_token *
1512 get_token_no_padding (cpp_reader *pfile)
1514 for (;;)
1516 const cpp_token *result = cpp_get_token (pfile);
1517 if (result->type != CPP_PADDING)
1518 return result;
1522 /* Check syntax is "(string-literal)". Returns the string on success,
1523 or NULL on failure. */
1524 static const cpp_token *
1525 get__Pragma_string (cpp_reader *pfile)
1527 const cpp_token *string;
1528 const cpp_token *paren;
1530 paren = get_token_no_padding (pfile);
1531 if (paren->type == CPP_EOF)
1532 _cpp_backup_tokens (pfile, 1);
1533 if (paren->type != CPP_OPEN_PAREN)
1534 return NULL;
1536 string = get_token_no_padding (pfile);
1537 if (string->type == CPP_EOF)
1538 _cpp_backup_tokens (pfile, 1);
1539 if (string->type != CPP_STRING && string->type != CPP_WSTRING
1540 && string->type != CPP_STRING32 && string->type != CPP_STRING16)
1541 return NULL;
1543 paren = get_token_no_padding (pfile);
1544 if (paren->type == CPP_EOF)
1545 _cpp_backup_tokens (pfile, 1);
1546 if (paren->type != CPP_CLOSE_PAREN)
1547 return NULL;
1549 return string;
1552 /* Destringize IN into a temporary buffer, by removing the first \ of
1553 \" and \\ sequences, and process the result as a #pragma directive. */
1554 static void
1555 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1557 const unsigned char *src, *limit;
1558 char *dest, *result;
1559 cpp_context *saved_context;
1560 cpp_token *saved_cur_token;
1561 tokenrun *saved_cur_run;
1562 cpp_token *toks;
1563 int count;
1564 const struct directive *save_directive;
1566 dest = result = (char *) alloca (in->len - 1);
1567 src = in->text + 1 + (in->text[0] == 'L');
1568 limit = in->text + in->len - 1;
1569 while (src < limit)
1571 /* We know there is a character following the backslash. */
1572 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1573 src++;
1574 *dest++ = *src++;
1576 *dest = '\n';
1578 /* Ugh; an awful kludge. We are really not set up to be lexing
1579 tokens when in the middle of a macro expansion. Use a new
1580 context to force cpp_get_token to lex, and so skip_rest_of_line
1581 doesn't go beyond the end of the text. Also, remember the
1582 current lexing position so we can return to it later.
1584 Something like line-at-a-time lexing should remove the need for
1585 this. */
1586 saved_context = pfile->context;
1587 saved_cur_token = pfile->cur_token;
1588 saved_cur_run = pfile->cur_run;
1590 pfile->context = XNEW (cpp_context);
1591 pfile->context->macro = 0;
1592 pfile->context->prev = 0;
1593 pfile->context->next = 0;
1595 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1596 until we've read all of the tokens that we want. */
1597 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1598 /* from_stage3 */ true);
1599 /* ??? Antique Disgusting Hack. What does this do? */
1600 if (pfile->buffer->prev)
1601 pfile->buffer->file = pfile->buffer->prev->file;
1603 start_directive (pfile);
1604 _cpp_clean_line (pfile);
1605 save_directive = pfile->directive;
1606 pfile->directive = &dtable[T_PRAGMA];
1607 do_pragma (pfile);
1608 end_directive (pfile, 1);
1609 pfile->directive = save_directive;
1611 /* We always insert at least one token, the directive result. It'll
1612 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1613 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1615 /* If we're not handling the pragma internally, read all of the tokens from
1616 the string buffer now, while the string buffer is still installed. */
1617 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1618 to me what the true lifespan of the tokens are. It would appear that
1619 the lifespan is the entire parse of the main input stream, in which case
1620 this may not be wrong. */
1621 if (pfile->directive_result.type == CPP_PRAGMA)
1623 int maxcount;
1625 count = 1;
1626 maxcount = 50;
1627 toks = XNEWVEC (cpp_token, maxcount);
1628 toks[0] = pfile->directive_result;
1632 if (count == maxcount)
1634 maxcount = maxcount * 3 / 2;
1635 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1637 toks[count] = *cpp_get_token (pfile);
1638 /* Macros have been already expanded by cpp_get_token
1639 if the pragma allowed expansion. */
1640 toks[count++].flags |= NO_EXPAND;
1642 while (toks[count-1].type != CPP_PRAGMA_EOL);
1644 else
1646 count = 1;
1647 toks = XNEW (cpp_token);
1648 toks[0] = pfile->directive_result;
1650 /* If we handled the entire pragma internally, make sure we get the
1651 line number correct for the next token. */
1652 if (pfile->cb.line_change)
1653 pfile->cb.line_change (pfile, pfile->cur_token, false);
1656 /* Finish inlining run_directive. */
1657 pfile->buffer->file = NULL;
1658 _cpp_pop_buffer (pfile);
1660 /* Reset the old macro state before ... */
1661 XDELETE (pfile->context);
1662 pfile->context = saved_context;
1663 pfile->cur_token = saved_cur_token;
1664 pfile->cur_run = saved_cur_run;
1666 /* ... inserting the new tokens we collected. */
1667 _cpp_push_token_context (pfile, NULL, toks, count);
1670 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1672 _cpp_do__Pragma (cpp_reader *pfile)
1674 const cpp_token *string = get__Pragma_string (pfile);
1675 pfile->directive_result.type = CPP_PADDING;
1677 if (string)
1679 destringize_and_run (pfile, &string->val.str);
1680 return 1;
1682 cpp_error (pfile, CPP_DL_ERROR,
1683 "_Pragma takes a parenthesized string literal");
1684 return 0;
1687 /* Handle #ifdef. */
1688 static void
1689 do_ifdef (cpp_reader *pfile)
1691 int skip = 1;
1693 if (! pfile->state.skipping)
1695 cpp_hashnode *node = lex_macro_node (pfile, false);
1697 if (node)
1699 skip = node->type != NT_MACRO;
1700 _cpp_mark_macro_used (node);
1701 if (!(node->flags & NODE_USED))
1703 node->flags |= NODE_USED;
1704 if (node->type == NT_MACRO)
1706 if (pfile->cb.used_define)
1707 pfile->cb.used_define (pfile, pfile->directive_line, node);
1709 else
1711 if (pfile->cb.used_undef)
1712 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1715 check_eol (pfile, false);
1719 push_conditional (pfile, skip, T_IFDEF, 0);
1722 /* Handle #ifndef. */
1723 static void
1724 do_ifndef (cpp_reader *pfile)
1726 int skip = 1;
1727 cpp_hashnode *node = 0;
1729 if (! pfile->state.skipping)
1731 node = lex_macro_node (pfile, false);
1733 if (node)
1735 skip = node->type == NT_MACRO;
1736 _cpp_mark_macro_used (node);
1737 if (!(node->flags & NODE_USED))
1739 node->flags |= NODE_USED;
1740 if (node->type == NT_MACRO)
1742 if (pfile->cb.used_define)
1743 pfile->cb.used_define (pfile, pfile->directive_line, node);
1745 else
1747 if (pfile->cb.used_undef)
1748 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1751 check_eol (pfile, false);
1755 push_conditional (pfile, skip, T_IFNDEF, node);
1758 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1759 pfile->mi_ind_cmacro so we can handle multiple-include
1760 optimizations. If macro expansion occurs in the expression, we
1761 cannot treat it as a controlling conditional, since the expansion
1762 could change in the future. That is handled by cpp_get_token. */
1763 static void
1764 do_if (cpp_reader *pfile)
1766 int skip = 1;
1768 if (! pfile->state.skipping)
1769 skip = _cpp_parse_expr (pfile, true) == false;
1771 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1774 /* Flip skipping state if appropriate and continue without changing
1775 if_stack; this is so that the error message for missing #endif's
1776 etc. will point to the original #if. */
1777 static void
1778 do_else (cpp_reader *pfile)
1780 cpp_buffer *buffer = pfile->buffer;
1781 struct if_stack *ifs = buffer->if_stack;
1783 if (ifs == NULL)
1784 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1785 else
1787 if (ifs->type == T_ELSE)
1789 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1790 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1791 "the conditional began here");
1793 ifs->type = T_ELSE;
1795 /* Skip any future (erroneous) #elses or #elifs. */
1796 pfile->state.skipping = ifs->skip_elses;
1797 ifs->skip_elses = true;
1799 /* Invalidate any controlling macro. */
1800 ifs->mi_cmacro = 0;
1802 /* Only check EOL if was not originally skipping. */
1803 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1804 check_eol (pfile, false);
1808 /* Handle a #elif directive by not changing if_stack either. See the
1809 comment above do_else. */
1810 static void
1811 do_elif (cpp_reader *pfile)
1813 cpp_buffer *buffer = pfile->buffer;
1814 struct if_stack *ifs = buffer->if_stack;
1816 if (ifs == NULL)
1817 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1818 else
1820 if (ifs->type == T_ELSE)
1822 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1823 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1824 "the conditional began here");
1826 ifs->type = T_ELIF;
1828 if (! ifs->was_skipping)
1830 bool value;
1831 /* The standard mandates that the expression be parsed even
1832 if we are skipping elses at this point -- the lexical
1833 restrictions on #elif only apply to skipped groups, but
1834 this group is not being skipped. Temporarily set
1835 skipping to false to get lexer warnings. */
1836 pfile->state.skipping = 0;
1837 value = _cpp_parse_expr (pfile, false);
1838 if (ifs->skip_elses)
1839 pfile->state.skipping = 1;
1840 else
1842 pfile->state.skipping = ! value;
1843 ifs->skip_elses = value;
1847 /* Invalidate any controlling macro. */
1848 ifs->mi_cmacro = 0;
1852 /* #endif pops the if stack and resets pfile->state.skipping. */
1853 static void
1854 do_endif (cpp_reader *pfile)
1856 cpp_buffer *buffer = pfile->buffer;
1857 struct if_stack *ifs = buffer->if_stack;
1859 if (ifs == NULL)
1860 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1861 else
1863 /* Only check EOL if was not originally skipping. */
1864 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1865 check_eol (pfile, false);
1867 /* If potential control macro, we go back outside again. */
1868 if (ifs->next == 0 && ifs->mi_cmacro)
1870 pfile->mi_valid = true;
1871 pfile->mi_cmacro = ifs->mi_cmacro;
1874 buffer->if_stack = ifs->next;
1875 pfile->state.skipping = ifs->was_skipping;
1876 obstack_free (&pfile->buffer_ob, ifs);
1880 /* Push an if_stack entry for a preprocessor conditional, and set
1881 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1882 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1883 we need to check here that we are at the top of the file. */
1884 static void
1885 push_conditional (cpp_reader *pfile, int skip, int type,
1886 const cpp_hashnode *cmacro)
1888 struct if_stack *ifs;
1889 cpp_buffer *buffer = pfile->buffer;
1891 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1892 ifs->line = pfile->directive_line;
1893 ifs->next = buffer->if_stack;
1894 ifs->skip_elses = pfile->state.skipping || !skip;
1895 ifs->was_skipping = pfile->state.skipping;
1896 ifs->type = type;
1897 /* This condition is effectively a test for top-of-file. */
1898 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1899 ifs->mi_cmacro = cmacro;
1900 else
1901 ifs->mi_cmacro = 0;
1903 pfile->state.skipping = skip;
1904 buffer->if_stack = ifs;
1907 /* Read the tokens of the answer into the macro pool, in a directive
1908 of type TYPE. Only commit the memory if we intend it as permanent
1909 storage, i.e. the #assert case. Returns 0 on success, and sets
1910 ANSWERP to point to the answer. PRED_LOC is the location of the
1911 predicate. */
1912 static int
1913 parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
1914 source_location pred_loc)
1916 const cpp_token *paren;
1917 struct answer *answer;
1918 unsigned int acount;
1920 /* In a conditional, it is legal to not have an open paren. We
1921 should save the following token in this case. */
1922 paren = cpp_get_token (pfile);
1924 /* If not a paren, see if we're OK. */
1925 if (paren->type != CPP_OPEN_PAREN)
1927 /* In a conditional no answer is a test for any answer. It
1928 could be followed by any token. */
1929 if (type == T_IF)
1931 _cpp_backup_tokens (pfile, 1);
1932 return 0;
1935 /* #unassert with no answer is valid - it removes all answers. */
1936 if (type == T_UNASSERT && paren->type == CPP_EOF)
1937 return 0;
1939 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
1940 "missing '(' after predicate");
1941 return 1;
1944 for (acount = 0;; acount++)
1946 size_t room_needed;
1947 const cpp_token *token = cpp_get_token (pfile);
1948 cpp_token *dest;
1950 if (token->type == CPP_CLOSE_PAREN)
1951 break;
1953 if (token->type == CPP_EOF)
1955 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1956 return 1;
1959 /* struct answer includes the space for one token. */
1960 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1962 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1963 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1965 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1966 *dest = *token;
1968 /* Drop whitespace at start, for answer equivalence purposes. */
1969 if (acount == 0)
1970 dest->flags &= ~PREV_WHITE;
1973 if (acount == 0)
1975 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1976 return 1;
1979 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1980 answer->count = acount;
1981 answer->next = NULL;
1982 *answerp = answer;
1984 return 0;
1987 /* Parses an assertion directive of type TYPE, returning a pointer to
1988 the hash node of the predicate, or 0 on error. If an answer was
1989 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1990 static cpp_hashnode *
1991 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1993 cpp_hashnode *result = 0;
1994 const cpp_token *predicate;
1996 /* We don't expand predicates or answers. */
1997 pfile->state.prevent_expansion++;
1999 *answerp = 0;
2000 predicate = cpp_get_token (pfile);
2001 if (predicate->type == CPP_EOF)
2002 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
2003 else if (predicate->type != CPP_NAME)
2004 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2005 "predicate must be an identifier");
2006 else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
2008 unsigned int len = NODE_LEN (predicate->val.node.node);
2009 unsigned char *sym = (unsigned char *) alloca (len + 1);
2011 /* Prefix '#' to get it out of macro namespace. */
2012 sym[0] = '#';
2013 memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
2014 result = cpp_lookup (pfile, sym, len + 1);
2017 pfile->state.prevent_expansion--;
2018 return result;
2021 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2022 or a pointer to NULL if the answer is not in the chain. */
2023 static struct answer **
2024 find_answer (cpp_hashnode *node, const struct answer *candidate)
2026 unsigned int i;
2027 struct answer **result;
2029 for (result = &node->value.answers; *result; result = &(*result)->next)
2031 struct answer *answer = *result;
2033 if (answer->count == candidate->count)
2035 for (i = 0; i < answer->count; i++)
2036 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2037 break;
2039 if (i == answer->count)
2040 break;
2044 return result;
2047 /* Test an assertion within a preprocessor conditional. Returns
2048 nonzero on failure, zero on success. On success, the result of
2049 the test is written into VALUE, otherwise the value 0. */
2051 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2053 struct answer *answer;
2054 cpp_hashnode *node;
2056 node = parse_assertion (pfile, &answer, T_IF);
2058 /* For recovery, an erroneous assertion expression is handled as a
2059 failing assertion. */
2060 *value = 0;
2062 if (node)
2063 *value = (node->type == NT_ASSERTION &&
2064 (answer == 0 || *find_answer (node, answer) != 0));
2065 else if (pfile->cur_token[-1].type == CPP_EOF)
2066 _cpp_backup_tokens (pfile, 1);
2068 /* We don't commit the memory for the answer - it's temporary only. */
2069 return node == 0;
2072 /* Handle #assert. */
2073 static void
2074 do_assert (cpp_reader *pfile)
2076 struct answer *new_answer;
2077 cpp_hashnode *node;
2079 node = parse_assertion (pfile, &new_answer, T_ASSERT);
2080 if (node)
2082 size_t answer_size;
2084 /* Place the new answer in the answer list. First check there
2085 is not a duplicate. */
2086 new_answer->next = 0;
2087 if (node->type == NT_ASSERTION)
2089 if (*find_answer (node, new_answer))
2091 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2092 NODE_NAME (node) + 1);
2093 return;
2095 new_answer->next = node->value.answers;
2098 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2099 * sizeof (cpp_token));
2100 /* Commit or allocate storage for the object. */
2101 if (pfile->hash_table->alloc_subobject)
2103 struct answer *temp_answer = new_answer;
2104 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2105 (answer_size);
2106 memcpy (new_answer, temp_answer, answer_size);
2108 else
2109 BUFF_FRONT (pfile->a_buff) += answer_size;
2111 node->type = NT_ASSERTION;
2112 node->value.answers = new_answer;
2113 check_eol (pfile, false);
2117 /* Handle #unassert. */
2118 static void
2119 do_unassert (cpp_reader *pfile)
2121 cpp_hashnode *node;
2122 struct answer *answer;
2124 node = parse_assertion (pfile, &answer, T_UNASSERT);
2125 /* It isn't an error to #unassert something that isn't asserted. */
2126 if (node && node->type == NT_ASSERTION)
2128 if (answer)
2130 struct answer **p = find_answer (node, answer), *temp;
2132 /* Remove the answer from the list. */
2133 temp = *p;
2134 if (temp)
2135 *p = temp->next;
2137 /* Did we free the last answer? */
2138 if (node->value.answers == 0)
2139 node->type = NT_VOID;
2141 check_eol (pfile, false);
2143 else
2144 _cpp_free_definition (node);
2147 /* We don't commit the memory for the answer - it's temporary only. */
2150 /* These are for -D, -U, -A. */
2152 /* Process the string STR as if it appeared as the body of a #define.
2153 If STR is just an identifier, define it with value 1.
2154 If STR has anything after the identifier, then it should
2155 be identifier=definition. */
2156 void
2157 cpp_define (cpp_reader *pfile, const char *str)
2159 char *buf;
2160 const char *p;
2161 size_t count;
2163 /* Copy the entire option so we can modify it.
2164 Change the first "=" in the string to a space. If there is none,
2165 tack " 1" on the end. */
2167 count = strlen (str);
2168 buf = (char *) alloca (count + 3);
2169 memcpy (buf, str, count);
2171 p = strchr (str, '=');
2172 if (p)
2173 buf[p - str] = ' ';
2174 else
2176 buf[count++] = ' ';
2177 buf[count++] = '1';
2179 buf[count] = '\n';
2181 run_directive (pfile, T_DEFINE, buf, count);
2185 /* Use to build macros to be run through cpp_define() as
2186 described above.
2187 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2189 void
2190 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2192 char *ptr = NULL;
2194 va_list ap;
2195 va_start (ap, fmt);
2196 vasprintf (&ptr, fmt, ap);
2197 va_end (ap);
2199 cpp_define (pfile, ptr);
2200 free (ptr);
2204 /* Slight variant of the above for use by initialize_builtins. */
2205 void
2206 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2208 size_t len = strlen (str);
2209 char *buf = (char *) alloca (len + 1);
2210 memcpy (buf, str, len);
2211 buf[len] = '\n';
2212 run_directive (pfile, T_DEFINE, buf, len);
2215 /* Process MACRO as if it appeared as the body of an #undef. */
2216 void
2217 cpp_undef (cpp_reader *pfile, const char *macro)
2219 size_t len = strlen (macro);
2220 char *buf = (char *) alloca (len + 1);
2221 memcpy (buf, macro, len);
2222 buf[len] = '\n';
2223 run_directive (pfile, T_UNDEF, buf, len);
2226 /* Like lex_macro_node, but read the input from STR. */
2227 static cpp_hashnode *
2228 lex_macro_node_from_str (cpp_reader *pfile, const char *str)
2230 size_t len = strlen (str);
2231 uchar *buf = (uchar *) alloca (len + 1);
2232 cpp_hashnode *node;
2234 memcpy (buf, str, len);
2235 buf[len] = '\n';
2236 cpp_push_buffer (pfile, buf, len, true);
2237 node = lex_macro_node (pfile, true);
2238 _cpp_pop_buffer (pfile);
2240 return node;
2243 /* If STR is a defined macro, return its definition node, else return NULL. */
2244 cpp_macro *
2245 cpp_push_definition (cpp_reader *pfile, const char *str)
2247 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2248 if (node && node->type == NT_MACRO)
2249 return node->value.macro;
2250 else
2251 return NULL;
2254 /* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2255 then the macro should be undefined. */
2256 void
2257 cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2259 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2260 if (node == NULL)
2261 return;
2263 if (pfile->cb.before_define)
2264 pfile->cb.before_define (pfile);
2266 if (node->type == NT_MACRO)
2268 if (pfile->cb.undef)
2269 pfile->cb.undef (pfile, pfile->directive_line, node);
2270 if (CPP_OPTION (pfile, warn_unused_macros))
2271 _cpp_warn_if_unused_macro (pfile, node, NULL);
2273 if (node->type != NT_VOID)
2274 _cpp_free_definition (node);
2276 if (dfn)
2278 node->type = NT_MACRO;
2279 node->value.macro = dfn;
2280 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2281 node->flags |= NODE_WARN;
2283 if (pfile->cb.define)
2284 pfile->cb.define (pfile, pfile->directive_line, node);
2288 /* Process the string STR as if it appeared as the body of a #assert. */
2289 void
2290 cpp_assert (cpp_reader *pfile, const char *str)
2292 handle_assertion (pfile, str, T_ASSERT);
2295 /* Process STR as if it appeared as the body of an #unassert. */
2296 void
2297 cpp_unassert (cpp_reader *pfile, const char *str)
2299 handle_assertion (pfile, str, T_UNASSERT);
2302 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2303 static void
2304 handle_assertion (cpp_reader *pfile, const char *str, int type)
2306 size_t count = strlen (str);
2307 const char *p = strchr (str, '=');
2309 /* Copy the entire option so we can modify it. Change the first
2310 "=" in the string to a '(', and tack a ')' on the end. */
2311 char *buf = (char *) alloca (count + 2);
2313 memcpy (buf, str, count);
2314 if (p)
2316 buf[p - str] = '(';
2317 buf[count++] = ')';
2319 buf[count] = '\n';
2320 str = buf;
2322 run_directive (pfile, type, str, count);
2325 /* The options structure. */
2326 cpp_options *
2327 cpp_get_options (cpp_reader *pfile)
2329 return &pfile->opts;
2332 /* The callbacks structure. */
2333 cpp_callbacks *
2334 cpp_get_callbacks (cpp_reader *pfile)
2336 return &pfile->cb;
2339 /* Copy the given callbacks structure to our own. */
2340 void
2341 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2343 pfile->cb = *cb;
2346 /* The dependencies structure. (Creates one if it hasn't already been.) */
2347 struct deps *
2348 cpp_get_deps (cpp_reader *pfile)
2350 if (!pfile->deps)
2351 pfile->deps = deps_init ();
2352 return pfile->deps;
2355 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2356 doesn't fail. It does not generate a file change call back; that
2357 is the responsibility of the caller. */
2358 cpp_buffer *
2359 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2360 int from_stage3)
2362 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2364 /* Clears, amongst other things, if_stack and mi_cmacro. */
2365 memset (new_buffer, 0, sizeof (cpp_buffer));
2367 new_buffer->next_line = new_buffer->buf = buffer;
2368 new_buffer->rlimit = buffer + len;
2369 new_buffer->from_stage3 = from_stage3;
2370 new_buffer->prev = pfile->buffer;
2371 new_buffer->need_line = true;
2373 pfile->buffer = new_buffer;
2375 return new_buffer;
2378 /* Pops a single buffer, with a file change call-back if appropriate.
2379 Then pushes the next -include file, if any remain. */
2380 void
2381 _cpp_pop_buffer (cpp_reader *pfile)
2383 cpp_buffer *buffer = pfile->buffer;
2384 struct _cpp_file *inc = buffer->file;
2385 struct if_stack *ifs;
2387 /* Walk back up the conditional stack till we reach its level at
2388 entry to this file, issuing error messages. */
2389 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2390 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2391 "unterminated #%s", dtable[ifs->type].name);
2393 /* In case of a missing #endif. */
2394 pfile->state.skipping = 0;
2396 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2397 pfile->buffer = buffer->prev;
2399 free (buffer->notes);
2401 /* Free the buffer object now; we may want to push a new buffer
2402 in _cpp_push_next_include_file. */
2403 obstack_free (&pfile->buffer_ob, buffer);
2405 if (inc)
2407 _cpp_pop_file_buffer (pfile, inc);
2409 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2413 /* Enter all recognized directives in the hash table. */
2414 void
2415 _cpp_init_directives (cpp_reader *pfile)
2417 unsigned int i;
2418 cpp_hashnode *node;
2420 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2422 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2423 node->is_directive = 1;
2424 node->directive_index = i;