]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcpp/directives.c
2010-09-29 Kai Tietz <kai.tietz@onevision.com>
[thirdparty/gcc.git] / libcpp / directives.c
1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007, 2008, 2009, 2010 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
8
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.
13
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.
18
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/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "internal.h"
27 #include "mkdeps.h"
28 #include "obstack.h"
29
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
32 struct if_stack
33 {
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. */
40 };
41
42 /* Contains a registered pragma or pragma namespace. */
43 typedef void (*pragma_cb) (cpp_reader *);
44 struct pragma_entry
45 {
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;
57 };
58
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
65
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).
71
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)
79
80 /* Defines one #-directive, including how to handle it. */
81 typedef void (*directive_handler) (cpp_reader *);
82 typedef struct directive directive;
83 struct directive
84 {
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. */
90 };
91
92 /* Forward declarations. */
93
94 static void skip_rest_of_line (cpp_reader *);
95 static void check_eol (cpp_reader *, bool);
96 static void start_directive (cpp_reader *);
97 static void prepare_directive_trad (cpp_reader *);
98 static void end_directive (cpp_reader *, int);
99 static void directive_diagnostics (cpp_reader *, const directive *, int);
100 static void run_directive (cpp_reader *, int, const char *, size_t);
101 static char *glue_header_name (cpp_reader *);
102 static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
103 source_location *);
104 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
105 static unsigned int read_flag (cpp_reader *, unsigned int);
106 static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
107 static void do_diagnostic (cpp_reader *, int, int, int);
108 static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
109 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
110 static void do_include_common (cpp_reader *, enum include_type);
111 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
112 const cpp_hashnode *);
113 static int count_registered_pragmas (struct pragma_entry *);
114 static char ** save_registered_pragmas (struct pragma_entry *, char **);
115 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
116 char **);
117 static void do_pragma_once (cpp_reader *);
118 static void do_pragma_poison (cpp_reader *);
119 static void do_pragma_system_header (cpp_reader *);
120 static void do_pragma_dependency (cpp_reader *);
121 static void do_linemarker (cpp_reader *);
122 static const cpp_token *get_token_no_padding (cpp_reader *);
123 static const cpp_token *get__Pragma_string (cpp_reader *);
124 static void destringize_and_run (cpp_reader *, const cpp_string *);
125 static int parse_answer (cpp_reader *, struct answer **, int, source_location);
126 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
127 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
128 static void handle_assertion (cpp_reader *, const char *, int);
129 static void do_pragma_push_macro (cpp_reader *);
130 static void do_pragma_pop_macro (cpp_reader *);
131 static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *);
132
133 /* This is the table of directive handlers. It is ordered by
134 frequency of occurrence; the numbers at the end are directive
135 counts from all the source code I have lying around (egcs and libc
136 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
137 pcmcia-cs-3.0.9). This is no longer important as directive lookup
138 is now O(1). All extensions other than #warning, #include_next,
139 and #import are deprecated. The name is where the extension
140 appears to have come from. */
141
142 #define DIRECTIVE_TABLE \
143 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
144 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
145 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
146 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
147 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
148 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
149 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
150 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
151 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
152 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
153 D(error, T_ERROR, STDC89, 0) /* 475 */ \
154 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
155 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
156 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
157 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
158 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
159 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
160 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
161 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
162
163 /* #sccs is synonymous with #ident. */
164 #define do_sccs do_ident
165
166 /* Use the table to generate a series of prototypes, an enum for the
167 directive names, and an array of directive handlers. */
168
169 #define D(name, t, o, f) static void do_##name (cpp_reader *);
170 DIRECTIVE_TABLE
171 #undef D
172
173 #define D(n, tag, o, f) tag,
174 enum
175 {
176 DIRECTIVE_TABLE
177 N_DIRECTIVES
178 };
179 #undef D
180
181 #define D(name, t, origin, flags) \
182 { do_##name, (const uchar *) #name, \
183 sizeof #name - 1, origin, flags },
184 static const directive dtable[] =
185 {
186 DIRECTIVE_TABLE
187 };
188 #undef D
189 #undef DIRECTIVE_TABLE
190
191 /* Wrapper struct directive for linemarkers.
192 The origin is more or less true - the original K+R cpp
193 did use this notation in its preprocessed output. */
194 static const directive linemarker_dir =
195 {
196 do_linemarker, UC"#", 1, KANDR, IN_I
197 };
198
199 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
200
201 /* Skip any remaining tokens in a directive. */
202 static void
203 skip_rest_of_line (cpp_reader *pfile)
204 {
205 /* Discard all stacked contexts. */
206 while (pfile->context->prev)
207 _cpp_pop_context (pfile);
208
209 /* Sweep up all tokens remaining on the line. */
210 if (! SEEN_EOL ())
211 while (_cpp_lex_token (pfile)->type != CPP_EOF)
212 ;
213 }
214
215 /* Ensure there are no stray tokens at the end of a directive. If
216 EXPAND is true, tokens macro-expanding to nothing are allowed. */
217 static void
218 check_eol (cpp_reader *pfile, bool expand)
219 {
220 if (! SEEN_EOL () && (expand
221 ? cpp_get_token (pfile)
222 : _cpp_lex_token (pfile))->type != CPP_EOF)
223 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
224 pfile->directive->name);
225 }
226
227 /* Ensure there are no stray tokens other than comments at the end of
228 a directive, and gather the comments. */
229 static const cpp_token **
230 check_eol_return_comments (cpp_reader *pfile)
231 {
232 size_t c;
233 size_t capacity = 8;
234 const cpp_token **buf;
235
236 buf = XNEWVEC (const cpp_token *, capacity);
237 c = 0;
238 if (! SEEN_EOL ())
239 {
240 while (1)
241 {
242 const cpp_token *tok;
243
244 tok = _cpp_lex_token (pfile);
245 if (tok->type == CPP_EOF)
246 break;
247 if (tok->type != CPP_COMMENT)
248 cpp_error (pfile, CPP_DL_PEDWARN,
249 "extra tokens at end of #%s directive",
250 pfile->directive->name);
251 else
252 {
253 if (c + 1 >= capacity)
254 {
255 capacity *= 2;
256 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
257 }
258 buf[c] = tok;
259 ++c;
260 }
261 }
262 }
263 buf[c] = NULL;
264 return buf;
265 }
266
267 /* Called when entering a directive, _Pragma or command-line directive. */
268 static void
269 start_directive (cpp_reader *pfile)
270 {
271 /* Setup in-directive state. */
272 pfile->state.in_directive = 1;
273 pfile->state.save_comments = 0;
274 pfile->directive_result.type = CPP_PADDING;
275
276 /* Some handlers need the position of the # for diagnostics. */
277 pfile->directive_line = pfile->line_table->highest_line;
278 }
279
280 /* Called when leaving a directive, _Pragma or command-line directive. */
281 static void
282 end_directive (cpp_reader *pfile, int skip_line)
283 {
284 if (pfile->state.in_deferred_pragma)
285 ;
286 else if (CPP_OPTION (pfile, traditional))
287 {
288 /* Revert change of prepare_directive_trad. */
289 pfile->state.prevent_expansion--;
290
291 if (pfile->directive != &dtable[T_DEFINE])
292 _cpp_remove_overlay (pfile);
293 }
294 /* We don't skip for an assembler #. */
295 else if (skip_line)
296 {
297 skip_rest_of_line (pfile);
298 if (!pfile->keep_tokens)
299 {
300 pfile->cur_run = &pfile->base_run;
301 pfile->cur_token = pfile->base_run.base;
302 }
303 }
304
305 /* Restore state. */
306 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
307 pfile->state.in_directive = 0;
308 pfile->state.in_expression = 0;
309 pfile->state.angled_headers = 0;
310 pfile->directive = 0;
311 }
312
313 /* Prepare to handle the directive in pfile->directive. */
314 static void
315 prepare_directive_trad (cpp_reader *pfile)
316 {
317 if (pfile->directive != &dtable[T_DEFINE])
318 {
319 bool no_expand = (pfile->directive
320 && ! (pfile->directive->flags & EXPAND));
321 bool was_skipping = pfile->state.skipping;
322
323 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
324 || pfile->directive == &dtable[T_ELIF]);
325 if (pfile->state.in_expression)
326 pfile->state.skipping = false;
327
328 if (no_expand)
329 pfile->state.prevent_expansion++;
330 _cpp_scan_out_logical_line (pfile, NULL);
331 if (no_expand)
332 pfile->state.prevent_expansion--;
333
334 pfile->state.skipping = was_skipping;
335 _cpp_overlay_buffer (pfile, pfile->out.base,
336 pfile->out.cur - pfile->out.base);
337 }
338
339 /* Stop ISO C from expanding anything. */
340 pfile->state.prevent_expansion++;
341 }
342
343 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
344 the '#' was indented. */
345 static void
346 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
347 {
348 /* Issue -pedantic or deprecated warnings for extensions. We let
349 -pedantic take precedence if both are applicable. */
350 if (! pfile->state.skipping)
351 {
352 if (dir->origin == EXTENSION
353 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
354 && CPP_PEDANTIC (pfile))
355 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
356 else if (((dir->flags & DEPRECATED) != 0
357 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
358 && CPP_OPTION (pfile, cpp_warn_deprecated))
359 cpp_warning (pfile, CPP_W_DEPRECATED,
360 "#%s is a deprecated GCC extension", dir->name);
361 }
362
363 /* Traditionally, a directive is ignored unless its # is in
364 column 1. Therefore in code intended to work with K+R
365 compilers, directives added by C89 must have their #
366 indented, and directives present in traditional C must not.
367 This is true even of directives in skipped conditional
368 blocks. #elif cannot be used at all. */
369 if (CPP_WTRADITIONAL (pfile))
370 {
371 if (dir == &dtable[T_ELIF])
372 cpp_warning (pfile, CPP_W_TRADITIONAL,
373 "suggest not using #elif in traditional C");
374 else if (indented && dir->origin == KANDR)
375 cpp_warning (pfile, CPP_W_TRADITIONAL,
376 "traditional C ignores #%s with the # indented",
377 dir->name);
378 else if (!indented && dir->origin != KANDR)
379 cpp_warning (pfile, CPP_W_TRADITIONAL,
380 "suggest hiding #%s from traditional C with an indented #",
381 dir->name);
382 }
383 }
384
385 /* Check if we have a known directive. INDENTED is nonzero if the
386 '#' of the directive was indented. This function is in this file
387 to save unnecessarily exporting dtable etc. to lex.c. Returns
388 nonzero if the line of tokens has been handled, zero if we should
389 continue processing the line. */
390 int
391 _cpp_handle_directive (cpp_reader *pfile, int indented)
392 {
393 const directive *dir = 0;
394 const cpp_token *dname;
395 bool was_parsing_args = pfile->state.parsing_args;
396 bool was_discarding_output = pfile->state.discarding_output;
397 int skip = 1;
398
399 if (was_discarding_output)
400 pfile->state.prevent_expansion = 0;
401
402 if (was_parsing_args)
403 {
404 if (CPP_OPTION (pfile, cpp_pedantic))
405 cpp_error (pfile, CPP_DL_PEDWARN,
406 "embedding a directive within macro arguments is not portable");
407 pfile->state.parsing_args = 0;
408 pfile->state.prevent_expansion = 0;
409 }
410 start_directive (pfile);
411 dname = _cpp_lex_token (pfile);
412
413 if (dname->type == CPP_NAME)
414 {
415 if (dname->val.node.node->is_directive)
416 dir = &dtable[dname->val.node.node->directive_index];
417 }
418 /* We do not recognize the # followed by a number extension in
419 assembler code. */
420 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
421 {
422 dir = &linemarker_dir;
423 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
424 && ! pfile->state.skipping)
425 cpp_error (pfile, CPP_DL_PEDWARN,
426 "style of line directive is a GCC extension");
427 }
428
429 if (dir)
430 {
431 /* If we have a directive that is not an opening conditional,
432 invalidate any control macro. */
433 if (! (dir->flags & IF_COND))
434 pfile->mi_valid = false;
435
436 /* Kluge alert. In order to be sure that code like this
437
438 #define HASH #
439 HASH define foo bar
440
441 does not cause '#define foo bar' to get executed when
442 compiled with -save-temps, we recognize directives in
443 -fpreprocessed mode only if the # is in column 1. macro.c
444 puts a space in front of any '#' at the start of a macro.
445
446 We exclude the -fdirectives-only case because macro expansion
447 has not been performed yet, and block comments can cause spaces
448 to preceed the directive. */
449 if (CPP_OPTION (pfile, preprocessed)
450 && !CPP_OPTION (pfile, directives_only)
451 && (indented || !(dir->flags & IN_I)))
452 {
453 skip = 0;
454 dir = 0;
455 }
456 else
457 {
458 /* In failed conditional groups, all non-conditional
459 directives are ignored. Before doing that, whether
460 skipping or not, we should lex angle-bracketed headers
461 correctly, and maybe output some diagnostics. */
462 pfile->state.angled_headers = dir->flags & INCL;
463 pfile->state.directive_wants_padding = dir->flags & INCL;
464 if (! CPP_OPTION (pfile, preprocessed))
465 directive_diagnostics (pfile, dir, indented);
466 if (pfile->state.skipping && !(dir->flags & COND))
467 dir = 0;
468 }
469 }
470 else if (dname->type == CPP_EOF)
471 ; /* CPP_EOF is the "null directive". */
472 else
473 {
474 /* An unknown directive. Don't complain about it in assembly
475 source: we don't know where the comments are, and # may
476 introduce assembler pseudo-ops. Don't complain about invalid
477 directives in skipped conditional groups (6.10 p4). */
478 if (CPP_OPTION (pfile, lang) == CLK_ASM)
479 skip = 0;
480 else if (!pfile->state.skipping)
481 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
482 cpp_token_as_text (pfile, dname));
483 }
484
485 pfile->directive = dir;
486 if (CPP_OPTION (pfile, traditional))
487 prepare_directive_trad (pfile);
488
489 if (dir)
490 pfile->directive->handler (pfile);
491 else if (skip == 0)
492 _cpp_backup_tokens (pfile, 1);
493
494 end_directive (pfile, skip);
495 if (was_parsing_args && !pfile->state.in_deferred_pragma)
496 {
497 /* Restore state when within macro args. */
498 pfile->state.parsing_args = 2;
499 pfile->state.prevent_expansion = 1;
500 }
501 if (was_discarding_output)
502 pfile->state.prevent_expansion = 1;
503 return skip;
504 }
505
506 /* Directive handler wrapper used by the command line option
507 processor. BUF is \n terminated. */
508 static void
509 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
510 {
511 cpp_push_buffer (pfile, (const uchar *) buf, count,
512 /* from_stage3 */ true);
513 start_directive (pfile);
514
515 /* This is a short-term fix to prevent a leading '#' being
516 interpreted as a directive. */
517 _cpp_clean_line (pfile);
518
519 pfile->directive = &dtable[dir_no];
520 if (CPP_OPTION (pfile, traditional))
521 prepare_directive_trad (pfile);
522 pfile->directive->handler (pfile);
523 end_directive (pfile, 1);
524 _cpp_pop_buffer (pfile);
525 }
526
527 /* Checks for validity the macro name in #define, #undef, #ifdef and
528 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
529 processing a #define or #undefine directive, and false
530 otherwise. */
531 static cpp_hashnode *
532 lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
533 {
534 const cpp_token *token = _cpp_lex_token (pfile);
535
536 /* The token immediately after #define must be an identifier. That
537 identifier may not be "defined", per C99 6.10.8p4.
538 In C++, it may not be any of the "named operators" either,
539 per C++98 [lex.digraph], [lex.key].
540 Finally, the identifier may not have been poisoned. (In that case
541 the lexer has issued the error message for us.) */
542
543 if (token->type == CPP_NAME)
544 {
545 cpp_hashnode *node = token->val.node.node;
546
547 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
548 cpp_error (pfile, CPP_DL_ERROR,
549 "\"defined\" cannot be used as a macro name");
550 else if (! (node->flags & NODE_POISONED))
551 return node;
552 }
553 else if (token->flags & NAMED_OP)
554 cpp_error (pfile, CPP_DL_ERROR,
555 "\"%s\" cannot be used as a macro name as it is an operator in C++",
556 NODE_NAME (token->val.node.node));
557 else if (token->type == CPP_EOF)
558 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
559 pfile->directive->name);
560 else
561 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
562
563 return NULL;
564 }
565
566 /* Process a #define directive. Most work is done in macro.c. */
567 static void
568 do_define (cpp_reader *pfile)
569 {
570 cpp_hashnode *node = lex_macro_node (pfile, true);
571
572 if (node)
573 {
574 /* If we have been requested to expand comments into macros,
575 then re-enable saving of comments. */
576 pfile->state.save_comments =
577 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
578
579 if (pfile->cb.before_define)
580 pfile->cb.before_define (pfile);
581
582 if (_cpp_create_definition (pfile, node))
583 if (pfile->cb.define)
584 pfile->cb.define (pfile, pfile->directive_line, node);
585
586 node->flags &= ~NODE_USED;
587 }
588 }
589
590 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
591 static void
592 do_undef (cpp_reader *pfile)
593 {
594 cpp_hashnode *node = lex_macro_node (pfile, true);
595
596 if (node)
597 {
598 if (pfile->cb.before_define)
599 pfile->cb.before_define (pfile);
600
601 if (pfile->cb.undef)
602 pfile->cb.undef (pfile, pfile->directive_line, node);
603
604 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
605 identifier is not currently defined as a macro name. */
606 if (node->type == NT_MACRO)
607 {
608 if (node->flags & NODE_WARN)
609 cpp_error (pfile, CPP_DL_WARNING,
610 "undefining \"%s\"", NODE_NAME (node));
611
612 if (CPP_OPTION (pfile, warn_unused_macros))
613 _cpp_warn_if_unused_macro (pfile, node, NULL);
614
615 _cpp_free_definition (node);
616 }
617 }
618
619 check_eol (pfile, false);
620 }
621
622 /* Undefine a single macro/assertion/whatever. */
623
624 static int
625 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
626 void *data_p ATTRIBUTE_UNUSED)
627 {
628 /* Body of _cpp_free_definition inlined here for speed.
629 Macros and assertions no longer have anything to free. */
630 h->type = NT_VOID;
631 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
632 return 1;
633 }
634
635 /* Undefine all macros and assertions. */
636
637 void
638 cpp_undef_all (cpp_reader *pfile)
639 {
640 cpp_forall_identifiers (pfile, undefine_macros, NULL);
641 }
642
643
644 /* Helper routine used by parse_include. Reinterpret the current line
645 as an h-char-sequence (< ... >); we are looking at the first token
646 after the <. Returns a malloced filename. */
647 static char *
648 glue_header_name (cpp_reader *pfile)
649 {
650 const cpp_token *token;
651 char *buffer;
652 size_t len, total_len = 0, capacity = 1024;
653
654 /* To avoid lexed tokens overwriting our glued name, we can only
655 allocate from the string pool once we've lexed everything. */
656 buffer = XNEWVEC (char, capacity);
657 for (;;)
658 {
659 token = get_token_no_padding (pfile);
660
661 if (token->type == CPP_GREATER)
662 break;
663 if (token->type == CPP_EOF)
664 {
665 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
666 break;
667 }
668
669 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
670 if (total_len + len > capacity)
671 {
672 capacity = (capacity + len) * 2;
673 buffer = XRESIZEVEC (char, buffer, capacity);
674 }
675
676 if (token->flags & PREV_WHITE)
677 buffer[total_len++] = ' ';
678
679 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
680 true)
681 - (uchar *) buffer);
682 }
683
684 buffer[total_len] = '\0';
685 return buffer;
686 }
687
688 /* Returns the file name of #include, #include_next, #import and
689 #pragma dependency. The string is malloced and the caller should
690 free it. Returns NULL on error. LOCATION is the source location
691 of the file name. */
692
693 static const char *
694 parse_include (cpp_reader *pfile, int *pangle_brackets,
695 const cpp_token ***buf, source_location *location)
696 {
697 char *fname;
698 const cpp_token *header;
699
700 /* Allow macro expansion. */
701 header = get_token_no_padding (pfile);
702 *location = header->src_loc;
703 if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
704 || header->type == CPP_HEADER_NAME)
705 {
706 fname = XNEWVEC (char, header->val.str.len - 1);
707 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
708 fname[header->val.str.len - 2] = '\0';
709 *pangle_brackets = header->type == CPP_HEADER_NAME;
710 }
711 else if (header->type == CPP_LESS)
712 {
713 fname = glue_header_name (pfile);
714 *pangle_brackets = 1;
715 }
716 else
717 {
718 const unsigned char *dir;
719
720 if (pfile->directive == &dtable[T_PRAGMA])
721 dir = UC"pragma dependency";
722 else
723 dir = pfile->directive->name;
724 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
725 dir);
726
727 return NULL;
728 }
729
730 if (pfile->directive == &dtable[T_PRAGMA])
731 {
732 /* This pragma allows extra tokens after the file name. */
733 }
734 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
735 check_eol (pfile, true);
736 else
737 {
738 /* If we are not discarding comments, then gather them while
739 doing the eol check. */
740 *buf = check_eol_return_comments (pfile);
741 }
742
743 return fname;
744 }
745
746 /* Handle #include, #include_next and #import. */
747 static void
748 do_include_common (cpp_reader *pfile, enum include_type type)
749 {
750 const char *fname;
751 int angle_brackets;
752 const cpp_token **buf = NULL;
753 source_location location;
754
755 /* Re-enable saving of comments if requested, so that the include
756 callback can dump comments which follow #include. */
757 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
758
759 fname = parse_include (pfile, &angle_brackets, &buf, &location);
760 if (!fname)
761 {
762 if (buf)
763 XDELETEVEC (buf);
764 return;
765 }
766
767 if (!*fname)
768 {
769 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
770 "empty filename in #%s",
771 pfile->directive->name);
772 XDELETEVEC (fname);
773 if (buf)
774 XDELETEVEC (buf);
775 return;
776 }
777
778 /* Prevent #include recursion. */
779 if (pfile->line_table->depth >= CPP_STACK_MAX)
780 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
781 else
782 {
783 /* Get out of macro context, if we are. */
784 skip_rest_of_line (pfile);
785
786 if (pfile->cb.include)
787 pfile->cb.include (pfile, pfile->directive_line,
788 pfile->directive->name, fname, angle_brackets,
789 buf);
790
791 _cpp_stack_include (pfile, fname, angle_brackets, type);
792 }
793
794 XDELETEVEC (fname);
795 if (buf)
796 XDELETEVEC (buf);
797 }
798
799 static void
800 do_include (cpp_reader *pfile)
801 {
802 do_include_common (pfile, IT_INCLUDE);
803 }
804
805 static void
806 do_import (cpp_reader *pfile)
807 {
808 do_include_common (pfile, IT_IMPORT);
809 }
810
811 static void
812 do_include_next (cpp_reader *pfile)
813 {
814 enum include_type type = IT_INCLUDE_NEXT;
815
816 /* If this is the primary source file, warn and use the normal
817 search logic. */
818 if (cpp_in_primary_file (pfile))
819 {
820 cpp_error (pfile, CPP_DL_WARNING,
821 "#include_next in primary source file");
822 type = IT_INCLUDE;
823 }
824 do_include_common (pfile, type);
825 }
826
827 /* Subroutine of do_linemarker. Read possible flags after file name.
828 LAST is the last flag seen; 0 if this is the first flag. Return the
829 flag if it is valid, 0 at the end of the directive. Otherwise
830 complain. */
831 static unsigned int
832 read_flag (cpp_reader *pfile, unsigned int last)
833 {
834 const cpp_token *token = _cpp_lex_token (pfile);
835
836 if (token->type == CPP_NUMBER && token->val.str.len == 1)
837 {
838 unsigned int flag = token->val.str.text[0] - '0';
839
840 if (flag > last && flag <= 4
841 && (flag != 4 || last == 3)
842 && (flag != 2 || last == 0))
843 return flag;
844 }
845
846 if (token->type != CPP_EOF)
847 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
848 cpp_token_as_text (pfile, token));
849 return 0;
850 }
851
852 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
853 of length LEN, to binary; store it in NUMP, and return false if the
854 number was well-formed, true if not. WRAPPED is set to true if the
855 number did not fit into 'unsigned long'. */
856 static bool
857 strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
858 {
859 linenum_type reg = 0;
860 linenum_type reg_prev = 0;
861
862 uchar c;
863 *wrapped = false;
864 while (len--)
865 {
866 c = *str++;
867 if (!ISDIGIT (c))
868 return true;
869 reg *= 10;
870 reg += c - '0';
871 if (reg < reg_prev)
872 *wrapped = true;
873 reg_prev = reg;
874 }
875 *nump = reg;
876 return false;
877 }
878
879 /* Interpret #line command.
880 Note that the filename string (if any) is a true string constant
881 (escapes are interpreted), unlike in #line. */
882 static void
883 do_line (cpp_reader *pfile)
884 {
885 const struct line_maps *line_table = pfile->line_table;
886 const struct line_map *map = &line_table->maps[line_table->used - 1];
887
888 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
889 sysp right now. */
890
891 unsigned char map_sysp = map->sysp;
892 const cpp_token *token;
893 const char *new_file = map->to_file;
894 linenum_type new_lineno;
895
896 /* C99 raised the minimum limit on #line numbers. */
897 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
898 bool wrapped;
899
900 /* #line commands expand macros. */
901 token = cpp_get_token (pfile);
902 if (token->type != CPP_NUMBER
903 || strtolinenum (token->val.str.text, token->val.str.len,
904 &new_lineno, &wrapped))
905 {
906 if (token->type == CPP_EOF)
907 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
908 else
909 cpp_error (pfile, CPP_DL_ERROR,
910 "\"%s\" after #line is not a positive integer",
911 cpp_token_as_text (pfile, token));
912 return;
913 }
914
915 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
916 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
917 else if (wrapped)
918 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
919
920 token = cpp_get_token (pfile);
921 if (token->type == CPP_STRING)
922 {
923 cpp_string s = { 0, 0 };
924 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
925 &s, CPP_STRING))
926 new_file = (const char *)s.text;
927 check_eol (pfile, true);
928 }
929 else if (token->type != CPP_EOF)
930 {
931 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
932 cpp_token_as_text (pfile, token));
933 return;
934 }
935
936 skip_rest_of_line (pfile);
937 _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
938 map_sysp);
939 }
940
941 /* Interpret the # 44 "file" [flags] notation, which has slightly
942 different syntax and semantics from #line: Flags are allowed,
943 and we never complain about the line number being too big. */
944 static void
945 do_linemarker (cpp_reader *pfile)
946 {
947 const struct line_maps *line_table = pfile->line_table;
948 const struct line_map *map = &line_table->maps[line_table->used - 1];
949 const cpp_token *token;
950 const char *new_file = map->to_file;
951 linenum_type new_lineno;
952 unsigned int new_sysp = map->sysp;
953 enum lc_reason reason = LC_RENAME_VERBATIM;
954 int flag;
955 bool wrapped;
956
957 /* Back up so we can get the number again. Putting this in
958 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
959 some circumstances, which can segfault. */
960 _cpp_backup_tokens (pfile, 1);
961
962 /* #line commands expand macros. */
963 token = cpp_get_token (pfile);
964 if (token->type != CPP_NUMBER
965 || strtolinenum (token->val.str.text, token->val.str.len,
966 &new_lineno, &wrapped))
967 {
968 /* Unlike #line, there does not seem to be a way to get an EOF
969 here. So, it should be safe to always spell the token. */
970 cpp_error (pfile, CPP_DL_ERROR,
971 "\"%s\" after # is not a positive integer",
972 cpp_token_as_text (pfile, token));
973 return;
974 }
975
976 token = cpp_get_token (pfile);
977 if (token->type == CPP_STRING)
978 {
979 cpp_string s = { 0, 0 };
980 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
981 1, &s, CPP_STRING))
982 new_file = (const char *)s.text;
983
984 new_sysp = 0;
985 flag = read_flag (pfile, 0);
986 if (flag == 1)
987 {
988 reason = LC_ENTER;
989 /* Fake an include for cpp_included (). */
990 _cpp_fake_include (pfile, new_file);
991 flag = read_flag (pfile, flag);
992 }
993 else if (flag == 2)
994 {
995 reason = LC_LEAVE;
996 flag = read_flag (pfile, flag);
997 }
998 if (flag == 3)
999 {
1000 new_sysp = 1;
1001 flag = read_flag (pfile, flag);
1002 if (flag == 4)
1003 new_sysp = 2;
1004 }
1005 pfile->buffer->sysp = new_sysp;
1006
1007 check_eol (pfile, false);
1008 }
1009 else if (token->type != CPP_EOF)
1010 {
1011 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
1012 cpp_token_as_text (pfile, token));
1013 return;
1014 }
1015
1016 skip_rest_of_line (pfile);
1017
1018 /* Compensate for the increment in linemap_add that occurs in
1019 _cpp_do_file_change. We're currently at the start of the line
1020 *following* the #line directive. A separate source_location for this
1021 location makes no sense (until we do the LC_LEAVE), and
1022 complicates LAST_SOURCE_LINE_LOCATION. */
1023 pfile->line_table->highest_location--;
1024
1025 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1026 }
1027
1028 /* Arrange the file_change callback. pfile->line has changed to
1029 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1030 header, 2 for a system header that needs to be extern "C" protected,
1031 and zero otherwise. */
1032 void
1033 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1034 const char *to_file, linenum_type file_line,
1035 unsigned int sysp)
1036 {
1037 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1038 to_file, file_line);
1039 if (map != NULL)
1040 linemap_line_start (pfile->line_table, map->to_line, 127);
1041
1042 if (pfile->cb.file_change)
1043 pfile->cb.file_change (pfile, map);
1044 }
1045
1046 /* Report a warning or error detected by the program we are
1047 processing. Use the directive's tokens in the error message. */
1048 static void
1049 do_diagnostic (cpp_reader *pfile, int code, int reason, int print_dir)
1050 {
1051 const unsigned char *dir_name;
1052 unsigned char *line;
1053 source_location src_loc = pfile->cur_token[-1].src_loc;
1054
1055 if (print_dir)
1056 dir_name = pfile->directive->name;
1057 else
1058 dir_name = NULL;
1059 pfile->state.prevent_expansion++;
1060 line = cpp_output_line_to_string (pfile, dir_name);
1061 pfile->state.prevent_expansion--;
1062
1063 if (code == CPP_DL_WARNING_SYSHDR && reason)
1064 cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line);
1065 else if (code == CPP_DL_WARNING && reason)
1066 cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line);
1067 else
1068 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1069 free (line);
1070 }
1071
1072 static void
1073 do_error (cpp_reader *pfile)
1074 {
1075 do_diagnostic (pfile, CPP_DL_ERROR, 0, 1);
1076 }
1077
1078 static void
1079 do_warning (cpp_reader *pfile)
1080 {
1081 /* We want #warning diagnostics to be emitted in system headers too. */
1082 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1);
1083 }
1084
1085 /* Report program identification. */
1086 static void
1087 do_ident (cpp_reader *pfile)
1088 {
1089 const cpp_token *str = cpp_get_token (pfile);
1090
1091 if (str->type != CPP_STRING)
1092 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1093 pfile->directive->name);
1094 else if (pfile->cb.ident)
1095 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1096
1097 check_eol (pfile, false);
1098 }
1099
1100 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1101 matching entry, or NULL if none is found. The returned entry could
1102 be the start of a namespace chain, or a pragma. */
1103 static struct pragma_entry *
1104 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1105 {
1106 while (chain && chain->pragma != pragma)
1107 chain = chain->next;
1108
1109 return chain;
1110 }
1111
1112 /* Create and insert a blank pragma entry at the beginning of a
1113 singly-linked CHAIN. */
1114 static struct pragma_entry *
1115 new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1116 {
1117 struct pragma_entry *new_entry;
1118
1119 new_entry = (struct pragma_entry *)
1120 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1121
1122 memset (new_entry, 0, sizeof (struct pragma_entry));
1123 new_entry->next = *chain;
1124
1125 *chain = new_entry;
1126 return new_entry;
1127 }
1128
1129 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1130 goes in the global namespace. */
1131 static struct pragma_entry *
1132 register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1133 bool allow_name_expansion)
1134 {
1135 struct pragma_entry **chain = &pfile->pragmas;
1136 struct pragma_entry *entry;
1137 const cpp_hashnode *node;
1138
1139 if (space)
1140 {
1141 node = cpp_lookup (pfile, UC space, strlen (space));
1142 entry = lookup_pragma_entry (*chain, node);
1143 if (!entry)
1144 {
1145 entry = new_pragma_entry (pfile, chain);
1146 entry->pragma = node;
1147 entry->is_nspace = true;
1148 entry->allow_expansion = allow_name_expansion;
1149 }
1150 else if (!entry->is_nspace)
1151 goto clash;
1152 else if (entry->allow_expansion != allow_name_expansion)
1153 {
1154 cpp_error (pfile, CPP_DL_ICE,
1155 "registering pragmas in namespace \"%s\" with mismatched "
1156 "name expansion", space);
1157 return NULL;
1158 }
1159 chain = &entry->u.space;
1160 }
1161 else if (allow_name_expansion)
1162 {
1163 cpp_error (pfile, CPP_DL_ICE,
1164 "registering pragma \"%s\" with name expansion "
1165 "and no namespace", name);
1166 return NULL;
1167 }
1168
1169 /* Check for duplicates. */
1170 node = cpp_lookup (pfile, UC name, strlen (name));
1171 entry = lookup_pragma_entry (*chain, node);
1172 if (entry == NULL)
1173 {
1174 entry = new_pragma_entry (pfile, chain);
1175 entry->pragma = node;
1176 return entry;
1177 }
1178
1179 if (entry->is_nspace)
1180 clash:
1181 cpp_error (pfile, CPP_DL_ICE,
1182 "registering \"%s\" as both a pragma and a pragma namespace",
1183 NODE_NAME (node));
1184 else if (space)
1185 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1186 space, name);
1187 else
1188 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1189
1190 return NULL;
1191 }
1192
1193 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1194 static void
1195 register_pragma_internal (cpp_reader *pfile, const char *space,
1196 const char *name, pragma_cb handler)
1197 {
1198 struct pragma_entry *entry;
1199
1200 entry = register_pragma_1 (pfile, space, name, false);
1201 entry->is_internal = true;
1202 entry->u.handler = handler;
1203 }
1204
1205 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1206 goes in the global namespace. HANDLER is the handler it will call,
1207 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1208 expansion while parsing pragma NAME. This function is exported
1209 from libcpp. */
1210 void
1211 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1212 pragma_cb handler, bool allow_expansion)
1213 {
1214 struct pragma_entry *entry;
1215
1216 if (!handler)
1217 {
1218 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1219 return;
1220 }
1221
1222 entry = register_pragma_1 (pfile, space, name, false);
1223 if (entry)
1224 {
1225 entry->allow_expansion = allow_expansion;
1226 entry->u.handler = handler;
1227 }
1228 }
1229
1230 /* Similarly, but create mark the pragma for deferred processing.
1231 When found, a CPP_PRAGMA token will be insertted into the stream
1232 with IDENT in the token->u.pragma slot. */
1233 void
1234 cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1235 const char *name, unsigned int ident,
1236 bool allow_expansion, bool allow_name_expansion)
1237 {
1238 struct pragma_entry *entry;
1239
1240 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1241 if (entry)
1242 {
1243 entry->is_deferred = true;
1244 entry->allow_expansion = allow_expansion;
1245 entry->u.ident = ident;
1246 }
1247 }
1248
1249 /* Register the pragmas the preprocessor itself handles. */
1250 void
1251 _cpp_init_internal_pragmas (cpp_reader *pfile)
1252 {
1253 /* Pragmas in the global namespace. */
1254 register_pragma_internal (pfile, 0, "once", do_pragma_once);
1255 register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
1256 register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
1257
1258 /* New GCC-specific pragmas should be put in the GCC namespace. */
1259 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1260 register_pragma_internal (pfile, "GCC", "system_header",
1261 do_pragma_system_header);
1262 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1263 }
1264
1265 /* Return the number of registered pragmas in PE. */
1266
1267 static int
1268 count_registered_pragmas (struct pragma_entry *pe)
1269 {
1270 int ct = 0;
1271 for (; pe != NULL; pe = pe->next)
1272 {
1273 if (pe->is_nspace)
1274 ct += count_registered_pragmas (pe->u.space);
1275 ct++;
1276 }
1277 return ct;
1278 }
1279
1280 /* Save into SD the names of the registered pragmas referenced by PE,
1281 and return a pointer to the next free space in SD. */
1282
1283 static char **
1284 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1285 {
1286 for (; pe != NULL; pe = pe->next)
1287 {
1288 if (pe->is_nspace)
1289 sd = save_registered_pragmas (pe->u.space, sd);
1290 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1291 HT_LEN (&pe->pragma->ident),
1292 HT_LEN (&pe->pragma->ident) + 1);
1293 }
1294 return sd;
1295 }
1296
1297 /* Return a newly-allocated array which saves the names of the
1298 registered pragmas. */
1299
1300 char **
1301 _cpp_save_pragma_names (cpp_reader *pfile)
1302 {
1303 int ct = count_registered_pragmas (pfile->pragmas);
1304 char **result = XNEWVEC (char *, ct);
1305 (void) save_registered_pragmas (pfile->pragmas, result);
1306 return result;
1307 }
1308
1309 /* Restore from SD the names of the registered pragmas referenced by PE,
1310 and return a pointer to the next unused name in SD. */
1311
1312 static char **
1313 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1314 char **sd)
1315 {
1316 for (; pe != NULL; pe = pe->next)
1317 {
1318 if (pe->is_nspace)
1319 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1320 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
1321 free (*sd);
1322 sd++;
1323 }
1324 return sd;
1325 }
1326
1327 /* Restore the names of the registered pragmas from SAVED. */
1328
1329 void
1330 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1331 {
1332 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1333 free (saved);
1334 }
1335
1336 /* Pragmata handling. We handle some, and pass the rest on to the
1337 front end. C99 defines three pragmas and says that no macro
1338 expansion is to be performed on them; whether or not macro
1339 expansion happens for other pragmas is implementation defined.
1340 This implementation allows for a mix of both, since GCC did not
1341 traditionally macro expand its (few) pragmas, whereas OpenMP
1342 specifies that macro expansion should happen. */
1343 static void
1344 do_pragma (cpp_reader *pfile)
1345 {
1346 const struct pragma_entry *p = NULL;
1347 const cpp_token *token, *pragma_token = pfile->cur_token;
1348 cpp_token ns_token;
1349 unsigned int count = 1;
1350
1351 pfile->state.prevent_expansion++;
1352
1353 token = cpp_get_token (pfile);
1354 ns_token = *token;
1355 if (token->type == CPP_NAME)
1356 {
1357 p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
1358 if (p && p->is_nspace)
1359 {
1360 bool allow_name_expansion = p->allow_expansion;
1361 if (allow_name_expansion)
1362 pfile->state.prevent_expansion--;
1363 token = cpp_get_token (pfile);
1364 if (token->type == CPP_NAME)
1365 p = lookup_pragma_entry (p->u.space, token->val.node.node);
1366 else
1367 p = NULL;
1368 if (allow_name_expansion)
1369 pfile->state.prevent_expansion++;
1370 count = 2;
1371 }
1372 }
1373
1374 if (p)
1375 {
1376 if (p->is_deferred)
1377 {
1378 pfile->directive_result.src_loc = pragma_token->src_loc;
1379 pfile->directive_result.type = CPP_PRAGMA;
1380 pfile->directive_result.flags = pragma_token->flags;
1381 pfile->directive_result.val.pragma = p->u.ident;
1382 pfile->state.in_deferred_pragma = true;
1383 pfile->state.pragma_allow_expansion = p->allow_expansion;
1384 if (!p->allow_expansion)
1385 pfile->state.prevent_expansion++;
1386 }
1387 else
1388 {
1389 /* Since the handler below doesn't get the line number, that
1390 it might need for diagnostics, make sure it has the right
1391 numbers in place. */
1392 if (pfile->cb.line_change)
1393 (*pfile->cb.line_change) (pfile, pragma_token, false);
1394 if (p->allow_expansion)
1395 pfile->state.prevent_expansion--;
1396 (*p->u.handler) (pfile);
1397 if (p->allow_expansion)
1398 pfile->state.prevent_expansion++;
1399 }
1400 }
1401 else if (pfile->cb.def_pragma)
1402 {
1403 if (count == 1 || pfile->context->prev == NULL)
1404 _cpp_backup_tokens (pfile, count);
1405 else
1406 {
1407 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1408 won't allow backing 2 tokens. */
1409 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1410 reads both tokens, we could perhaps free it, but if it doesn't,
1411 we don't know the exact lifespan. */
1412 cpp_token *toks = XNEWVEC (cpp_token, 2);
1413 toks[0] = ns_token;
1414 toks[0].flags |= NO_EXPAND;
1415 toks[1] = *token;
1416 toks[1].flags |= NO_EXPAND;
1417 _cpp_push_token_context (pfile, NULL, toks, 2);
1418 }
1419 pfile->cb.def_pragma (pfile, pfile->directive_line);
1420 }
1421
1422 pfile->state.prevent_expansion--;
1423 }
1424
1425 /* Handle #pragma once. */
1426 static void
1427 do_pragma_once (cpp_reader *pfile)
1428 {
1429 if (cpp_in_primary_file (pfile))
1430 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1431
1432 check_eol (pfile, false);
1433 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1434 }
1435
1436 /* Handle #pragma push_macro(STRING). */
1437 static void
1438 do_pragma_push_macro (cpp_reader *pfile)
1439 {
1440 cpp_hashnode *node;
1441 size_t defnlen;
1442 const uchar *defn = NULL;
1443 char *macroname, *dest;
1444 const char *limit, *src;
1445 const cpp_token *txt;
1446 struct def_pragma_macro *c;
1447
1448 txt = get__Pragma_string (pfile);
1449 if (!txt)
1450 {
1451 source_location src_loc = pfile->cur_token[-1].src_loc;
1452 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1453 "invalid #pragma push_macro directive");
1454 check_eol (pfile, false);
1455 skip_rest_of_line (pfile);
1456 return;
1457 }
1458 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1459 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1460 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1461 while (src < limit)
1462 {
1463 /* We know there is a character following the backslash. */
1464 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1465 src++;
1466 *dest++ = *src++;
1467 }
1468 *dest = 0;
1469 check_eol (pfile, false);
1470 skip_rest_of_line (pfile);
1471 c = XNEW (struct def_pragma_macro);
1472 memset (c, 0, sizeof (struct def_pragma_macro));
1473 c->name = XNEWVAR (char, strlen (macroname) + 1);
1474 strcpy (c->name, macroname);
1475 c->next = pfile->pushed_macros;
1476 node = _cpp_lex_identifier (pfile, c->name);
1477 if (node->type == NT_VOID)
1478 c->is_undef = 1;
1479 else
1480 {
1481 defn = cpp_macro_definition (pfile, node);
1482 defnlen = ustrlen (defn);
1483 c->definition = XNEWVEC (uchar, defnlen + 2);
1484 c->definition[defnlen] = '\n';
1485 c->definition[defnlen + 1] = 0;
1486 c->line = node->value.macro->line;
1487 c->syshdr = node->value.macro->syshdr;
1488 c->used = node->value.macro->used;
1489 memcpy (c->definition, defn, defnlen);
1490 }
1491
1492 pfile->pushed_macros = c;
1493 }
1494
1495 /* Handle #pragma pop_macro(STRING). */
1496 static void
1497 do_pragma_pop_macro (cpp_reader *pfile)
1498 {
1499 char *macroname, *dest;
1500 const char *limit, *src;
1501 const cpp_token *txt;
1502 struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
1503 txt = get__Pragma_string (pfile);
1504 if (!txt)
1505 {
1506 source_location src_loc = pfile->cur_token[-1].src_loc;
1507 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1508 "invalid #pragma pop_macro directive");
1509 check_eol (pfile, false);
1510 skip_rest_of_line (pfile);
1511 return;
1512 }
1513 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1514 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1515 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1516 while (src < limit)
1517 {
1518 /* We know there is a character following the backslash. */
1519 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1520 src++;
1521 *dest++ = *src++;
1522 }
1523 *dest = 0;
1524 check_eol (pfile, false);
1525 skip_rest_of_line (pfile);
1526
1527 while (c != NULL)
1528 {
1529 if (!strcmp (c->name, macroname))
1530 {
1531 if (!l)
1532 pfile->pushed_macros = c->next;
1533 else
1534 l->next = c->next;
1535 cpp_pop_definition (pfile, c);
1536 free (c->definition);
1537 free (c->name);
1538 free (c);
1539 break;
1540 }
1541 l = c;
1542 c = c->next;
1543 }
1544 }
1545
1546 /* Handle #pragma GCC poison, to poison one or more identifiers so
1547 that the lexer produces a hard error for each subsequent usage. */
1548 static void
1549 do_pragma_poison (cpp_reader *pfile)
1550 {
1551 const cpp_token *tok;
1552 cpp_hashnode *hp;
1553
1554 pfile->state.poisoned_ok = 1;
1555 for (;;)
1556 {
1557 tok = _cpp_lex_token (pfile);
1558 if (tok->type == CPP_EOF)
1559 break;
1560 if (tok->type != CPP_NAME)
1561 {
1562 cpp_error (pfile, CPP_DL_ERROR,
1563 "invalid #pragma GCC poison directive");
1564 break;
1565 }
1566
1567 hp = tok->val.node.node;
1568 if (hp->flags & NODE_POISONED)
1569 continue;
1570
1571 if (hp->type == NT_MACRO)
1572 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1573 NODE_NAME (hp));
1574 _cpp_free_definition (hp);
1575 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1576 }
1577 pfile->state.poisoned_ok = 0;
1578 }
1579
1580 /* Mark the current header as a system header. This will suppress
1581 some categories of warnings (notably those from -pedantic). It is
1582 intended for use in system libraries that cannot be implemented in
1583 conforming C, but cannot be certain that their headers appear in a
1584 system include directory. To prevent abuse, it is rejected in the
1585 primary source file. */
1586 static void
1587 do_pragma_system_header (cpp_reader *pfile)
1588 {
1589 if (cpp_in_primary_file (pfile))
1590 cpp_error (pfile, CPP_DL_WARNING,
1591 "#pragma system_header ignored outside include file");
1592 else
1593 {
1594 check_eol (pfile, false);
1595 skip_rest_of_line (pfile);
1596 cpp_make_system_header (pfile, 1, 0);
1597 }
1598 }
1599
1600 /* Check the modified date of the current include file against a specified
1601 file. Issue a diagnostic, if the specified file is newer. We use this to
1602 determine if a fixed header should be refixed. */
1603 static void
1604 do_pragma_dependency (cpp_reader *pfile)
1605 {
1606 const char *fname;
1607 int angle_brackets, ordering;
1608 source_location location;
1609
1610 fname = parse_include (pfile, &angle_brackets, NULL, &location);
1611 if (!fname)
1612 return;
1613
1614 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1615 if (ordering < 0)
1616 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1617 else if (ordering > 0)
1618 {
1619 cpp_error (pfile, CPP_DL_WARNING,
1620 "current file is older than %s", fname);
1621 if (cpp_get_token (pfile)->type != CPP_EOF)
1622 {
1623 _cpp_backup_tokens (pfile, 1);
1624 do_diagnostic (pfile, CPP_DL_WARNING, 0, 0);
1625 }
1626 }
1627
1628 free ((void *) fname);
1629 }
1630
1631 /* Get a token but skip padding. */
1632 static const cpp_token *
1633 get_token_no_padding (cpp_reader *pfile)
1634 {
1635 for (;;)
1636 {
1637 const cpp_token *result = cpp_get_token (pfile);
1638 if (result->type != CPP_PADDING)
1639 return result;
1640 }
1641 }
1642
1643 /* Check syntax is "(string-literal)". Returns the string on success,
1644 or NULL on failure. */
1645 static const cpp_token *
1646 get__Pragma_string (cpp_reader *pfile)
1647 {
1648 const cpp_token *string;
1649 const cpp_token *paren;
1650
1651 paren = get_token_no_padding (pfile);
1652 if (paren->type == CPP_EOF)
1653 _cpp_backup_tokens (pfile, 1);
1654 if (paren->type != CPP_OPEN_PAREN)
1655 return NULL;
1656
1657 string = get_token_no_padding (pfile);
1658 if (string->type == CPP_EOF)
1659 _cpp_backup_tokens (pfile, 1);
1660 if (string->type != CPP_STRING && string->type != CPP_WSTRING
1661 && string->type != CPP_STRING32 && string->type != CPP_STRING16
1662 && string->type != CPP_UTF8STRING)
1663 return NULL;
1664
1665 paren = get_token_no_padding (pfile);
1666 if (paren->type == CPP_EOF)
1667 _cpp_backup_tokens (pfile, 1);
1668 if (paren->type != CPP_CLOSE_PAREN)
1669 return NULL;
1670
1671 return string;
1672 }
1673
1674 /* Destringize IN into a temporary buffer, by removing the first \ of
1675 \" and \\ sequences, and process the result as a #pragma directive. */
1676 static void
1677 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1678 {
1679 const unsigned char *src, *limit;
1680 char *dest, *result;
1681 cpp_context *saved_context;
1682 cpp_token *saved_cur_token;
1683 tokenrun *saved_cur_run;
1684 cpp_token *toks;
1685 int count;
1686 const struct directive *save_directive;
1687
1688 dest = result = (char *) alloca (in->len - 1);
1689 src = in->text + 1 + (in->text[0] == 'L');
1690 limit = in->text + in->len - 1;
1691 while (src < limit)
1692 {
1693 /* We know there is a character following the backslash. */
1694 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1695 src++;
1696 *dest++ = *src++;
1697 }
1698 *dest = '\n';
1699
1700 /* Ugh; an awful kludge. We are really not set up to be lexing
1701 tokens when in the middle of a macro expansion. Use a new
1702 context to force cpp_get_token to lex, and so skip_rest_of_line
1703 doesn't go beyond the end of the text. Also, remember the
1704 current lexing position so we can return to it later.
1705
1706 Something like line-at-a-time lexing should remove the need for
1707 this. */
1708 saved_context = pfile->context;
1709 saved_cur_token = pfile->cur_token;
1710 saved_cur_run = pfile->cur_run;
1711
1712 pfile->context = XNEW (cpp_context);
1713 pfile->context->macro = 0;
1714 pfile->context->prev = 0;
1715 pfile->context->next = 0;
1716
1717 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1718 until we've read all of the tokens that we want. */
1719 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1720 /* from_stage3 */ true);
1721 /* ??? Antique Disgusting Hack. What does this do? */
1722 if (pfile->buffer->prev)
1723 pfile->buffer->file = pfile->buffer->prev->file;
1724
1725 start_directive (pfile);
1726 _cpp_clean_line (pfile);
1727 save_directive = pfile->directive;
1728 pfile->directive = &dtable[T_PRAGMA];
1729 do_pragma (pfile);
1730 end_directive (pfile, 1);
1731 pfile->directive = save_directive;
1732
1733 /* We always insert at least one token, the directive result. It'll
1734 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1735 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1736
1737 /* If we're not handling the pragma internally, read all of the tokens from
1738 the string buffer now, while the string buffer is still installed. */
1739 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1740 to me what the true lifespan of the tokens are. It would appear that
1741 the lifespan is the entire parse of the main input stream, in which case
1742 this may not be wrong. */
1743 if (pfile->directive_result.type == CPP_PRAGMA)
1744 {
1745 int maxcount;
1746
1747 count = 1;
1748 maxcount = 50;
1749 toks = XNEWVEC (cpp_token, maxcount);
1750 toks[0] = pfile->directive_result;
1751
1752 do
1753 {
1754 if (count == maxcount)
1755 {
1756 maxcount = maxcount * 3 / 2;
1757 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1758 }
1759 toks[count] = *cpp_get_token (pfile);
1760 /* Macros have been already expanded by cpp_get_token
1761 if the pragma allowed expansion. */
1762 toks[count++].flags |= NO_EXPAND;
1763 }
1764 while (toks[count-1].type != CPP_PRAGMA_EOL);
1765 }
1766 else
1767 {
1768 count = 1;
1769 toks = XNEW (cpp_token);
1770 toks[0] = pfile->directive_result;
1771
1772 /* If we handled the entire pragma internally, make sure we get the
1773 line number correct for the next token. */
1774 if (pfile->cb.line_change)
1775 pfile->cb.line_change (pfile, pfile->cur_token, false);
1776 }
1777
1778 /* Finish inlining run_directive. */
1779 pfile->buffer->file = NULL;
1780 _cpp_pop_buffer (pfile);
1781
1782 /* Reset the old macro state before ... */
1783 XDELETE (pfile->context);
1784 pfile->context = saved_context;
1785 pfile->cur_token = saved_cur_token;
1786 pfile->cur_run = saved_cur_run;
1787
1788 /* ... inserting the new tokens we collected. */
1789 _cpp_push_token_context (pfile, NULL, toks, count);
1790 }
1791
1792 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1793 int
1794 _cpp_do__Pragma (cpp_reader *pfile)
1795 {
1796 const cpp_token *string = get__Pragma_string (pfile);
1797 pfile->directive_result.type = CPP_PADDING;
1798
1799 if (string)
1800 {
1801 destringize_and_run (pfile, &string->val.str);
1802 return 1;
1803 }
1804 cpp_error (pfile, CPP_DL_ERROR,
1805 "_Pragma takes a parenthesized string literal");
1806 return 0;
1807 }
1808
1809 /* Handle #ifdef. */
1810 static void
1811 do_ifdef (cpp_reader *pfile)
1812 {
1813 int skip = 1;
1814
1815 if (! pfile->state.skipping)
1816 {
1817 cpp_hashnode *node = lex_macro_node (pfile, false);
1818
1819 if (node)
1820 {
1821 skip = node->type != NT_MACRO;
1822 _cpp_mark_macro_used (node);
1823 if (!(node->flags & NODE_USED))
1824 {
1825 node->flags |= NODE_USED;
1826 if (node->type == NT_MACRO)
1827 {
1828 if ((node->flags & NODE_BUILTIN)
1829 && pfile->cb.user_builtin_macro)
1830 pfile->cb.user_builtin_macro (pfile, node);
1831 if (pfile->cb.used_define)
1832 pfile->cb.used_define (pfile, pfile->directive_line, node);
1833 }
1834 else
1835 {
1836 if (pfile->cb.used_undef)
1837 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1838 }
1839 }
1840 if (pfile->cb.used)
1841 pfile->cb.used (pfile, pfile->directive_line, node);
1842 check_eol (pfile, false);
1843 }
1844 }
1845
1846 push_conditional (pfile, skip, T_IFDEF, 0);
1847 }
1848
1849 /* Handle #ifndef. */
1850 static void
1851 do_ifndef (cpp_reader *pfile)
1852 {
1853 int skip = 1;
1854 cpp_hashnode *node = 0;
1855
1856 if (! pfile->state.skipping)
1857 {
1858 node = lex_macro_node (pfile, false);
1859
1860 if (node)
1861 {
1862 skip = node->type == NT_MACRO;
1863 _cpp_mark_macro_used (node);
1864 if (!(node->flags & NODE_USED))
1865 {
1866 node->flags |= NODE_USED;
1867 if (node->type == NT_MACRO)
1868 {
1869 if ((node->flags & NODE_BUILTIN)
1870 && pfile->cb.user_builtin_macro)
1871 pfile->cb.user_builtin_macro (pfile, node);
1872 if (pfile->cb.used_define)
1873 pfile->cb.used_define (pfile, pfile->directive_line, node);
1874 }
1875 else
1876 {
1877 if (pfile->cb.used_undef)
1878 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1879 }
1880 }
1881 if (pfile->cb.used)
1882 pfile->cb.used (pfile, pfile->directive_line, node);
1883 check_eol (pfile, false);
1884 }
1885 }
1886
1887 push_conditional (pfile, skip, T_IFNDEF, node);
1888 }
1889
1890 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1891 pfile->mi_ind_cmacro so we can handle multiple-include
1892 optimizations. If macro expansion occurs in the expression, we
1893 cannot treat it as a controlling conditional, since the expansion
1894 could change in the future. That is handled by cpp_get_token. */
1895 static void
1896 do_if (cpp_reader *pfile)
1897 {
1898 int skip = 1;
1899
1900 if (! pfile->state.skipping)
1901 skip = _cpp_parse_expr (pfile, true) == false;
1902
1903 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1904 }
1905
1906 /* Flip skipping state if appropriate and continue without changing
1907 if_stack; this is so that the error message for missing #endif's
1908 etc. will point to the original #if. */
1909 static void
1910 do_else (cpp_reader *pfile)
1911 {
1912 cpp_buffer *buffer = pfile->buffer;
1913 struct if_stack *ifs = buffer->if_stack;
1914
1915 if (ifs == NULL)
1916 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1917 else
1918 {
1919 if (ifs->type == T_ELSE)
1920 {
1921 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1922 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1923 "the conditional began here");
1924 }
1925 ifs->type = T_ELSE;
1926
1927 /* Skip any future (erroneous) #elses or #elifs. */
1928 pfile->state.skipping = ifs->skip_elses;
1929 ifs->skip_elses = true;
1930
1931 /* Invalidate any controlling macro. */
1932 ifs->mi_cmacro = 0;
1933
1934 /* Only check EOL if was not originally skipping. */
1935 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1936 check_eol (pfile, false);
1937 }
1938 }
1939
1940 /* Handle a #elif directive by not changing if_stack either. See the
1941 comment above do_else. */
1942 static void
1943 do_elif (cpp_reader *pfile)
1944 {
1945 cpp_buffer *buffer = pfile->buffer;
1946 struct if_stack *ifs = buffer->if_stack;
1947
1948 if (ifs == NULL)
1949 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1950 else
1951 {
1952 if (ifs->type == T_ELSE)
1953 {
1954 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1955 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1956 "the conditional began here");
1957 }
1958 ifs->type = T_ELIF;
1959
1960 if (! ifs->was_skipping)
1961 {
1962 bool value;
1963 /* The standard mandates that the expression be parsed even
1964 if we are skipping elses at this point -- the lexical
1965 restrictions on #elif only apply to skipped groups, but
1966 this group is not being skipped. Temporarily set
1967 skipping to false to get lexer warnings. */
1968 pfile->state.skipping = 0;
1969 value = _cpp_parse_expr (pfile, false);
1970 if (ifs->skip_elses)
1971 pfile->state.skipping = 1;
1972 else
1973 {
1974 pfile->state.skipping = ! value;
1975 ifs->skip_elses = value;
1976 }
1977 }
1978
1979 /* Invalidate any controlling macro. */
1980 ifs->mi_cmacro = 0;
1981 }
1982 }
1983
1984 /* #endif pops the if stack and resets pfile->state.skipping. */
1985 static void
1986 do_endif (cpp_reader *pfile)
1987 {
1988 cpp_buffer *buffer = pfile->buffer;
1989 struct if_stack *ifs = buffer->if_stack;
1990
1991 if (ifs == NULL)
1992 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1993 else
1994 {
1995 /* Only check EOL if was not originally skipping. */
1996 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1997 check_eol (pfile, false);
1998
1999 /* If potential control macro, we go back outside again. */
2000 if (ifs->next == 0 && ifs->mi_cmacro)
2001 {
2002 pfile->mi_valid = true;
2003 pfile->mi_cmacro = ifs->mi_cmacro;
2004 }
2005
2006 buffer->if_stack = ifs->next;
2007 pfile->state.skipping = ifs->was_skipping;
2008 obstack_free (&pfile->buffer_ob, ifs);
2009 }
2010 }
2011
2012 /* Push an if_stack entry for a preprocessor conditional, and set
2013 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2014 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2015 we need to check here that we are at the top of the file. */
2016 static void
2017 push_conditional (cpp_reader *pfile, int skip, int type,
2018 const cpp_hashnode *cmacro)
2019 {
2020 struct if_stack *ifs;
2021 cpp_buffer *buffer = pfile->buffer;
2022
2023 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
2024 ifs->line = pfile->directive_line;
2025 ifs->next = buffer->if_stack;
2026 ifs->skip_elses = pfile->state.skipping || !skip;
2027 ifs->was_skipping = pfile->state.skipping;
2028 ifs->type = type;
2029 /* This condition is effectively a test for top-of-file. */
2030 if (pfile->mi_valid && pfile->mi_cmacro == 0)
2031 ifs->mi_cmacro = cmacro;
2032 else
2033 ifs->mi_cmacro = 0;
2034
2035 pfile->state.skipping = skip;
2036 buffer->if_stack = ifs;
2037 }
2038
2039 /* Read the tokens of the answer into the macro pool, in a directive
2040 of type TYPE. Only commit the memory if we intend it as permanent
2041 storage, i.e. the #assert case. Returns 0 on success, and sets
2042 ANSWERP to point to the answer. PRED_LOC is the location of the
2043 predicate. */
2044 static int
2045 parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
2046 source_location pred_loc)
2047 {
2048 const cpp_token *paren;
2049 struct answer *answer;
2050 unsigned int acount;
2051
2052 /* In a conditional, it is legal to not have an open paren. We
2053 should save the following token in this case. */
2054 paren = cpp_get_token (pfile);
2055
2056 /* If not a paren, see if we're OK. */
2057 if (paren->type != CPP_OPEN_PAREN)
2058 {
2059 /* In a conditional no answer is a test for any answer. It
2060 could be followed by any token. */
2061 if (type == T_IF)
2062 {
2063 _cpp_backup_tokens (pfile, 1);
2064 return 0;
2065 }
2066
2067 /* #unassert with no answer is valid - it removes all answers. */
2068 if (type == T_UNASSERT && paren->type == CPP_EOF)
2069 return 0;
2070
2071 cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
2072 "missing '(' after predicate");
2073 return 1;
2074 }
2075
2076 for (acount = 0;; acount++)
2077 {
2078 size_t room_needed;
2079 const cpp_token *token = cpp_get_token (pfile);
2080 cpp_token *dest;
2081
2082 if (token->type == CPP_CLOSE_PAREN)
2083 break;
2084
2085 if (token->type == CPP_EOF)
2086 {
2087 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
2088 return 1;
2089 }
2090
2091 /* struct answer includes the space for one token. */
2092 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
2093
2094 if (BUFF_ROOM (pfile->a_buff) < room_needed)
2095 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
2096
2097 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
2098 *dest = *token;
2099
2100 /* Drop whitespace at start, for answer equivalence purposes. */
2101 if (acount == 0)
2102 dest->flags &= ~PREV_WHITE;
2103 }
2104
2105 if (acount == 0)
2106 {
2107 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
2108 return 1;
2109 }
2110
2111 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
2112 answer->count = acount;
2113 answer->next = NULL;
2114 *answerp = answer;
2115
2116 return 0;
2117 }
2118
2119 /* Parses an assertion directive of type TYPE, returning a pointer to
2120 the hash node of the predicate, or 0 on error. If an answer was
2121 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
2122 static cpp_hashnode *
2123 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
2124 {
2125 cpp_hashnode *result = 0;
2126 const cpp_token *predicate;
2127
2128 /* We don't expand predicates or answers. */
2129 pfile->state.prevent_expansion++;
2130
2131 *answerp = 0;
2132 predicate = cpp_get_token (pfile);
2133 if (predicate->type == CPP_EOF)
2134 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
2135 else if (predicate->type != CPP_NAME)
2136 cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
2137 "predicate must be an identifier");
2138 else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
2139 {
2140 unsigned int len = NODE_LEN (predicate->val.node.node);
2141 unsigned char *sym = (unsigned char *) alloca (len + 1);
2142
2143 /* Prefix '#' to get it out of macro namespace. */
2144 sym[0] = '#';
2145 memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
2146 result = cpp_lookup (pfile, sym, len + 1);
2147 }
2148
2149 pfile->state.prevent_expansion--;
2150 return result;
2151 }
2152
2153 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2154 or a pointer to NULL if the answer is not in the chain. */
2155 static struct answer **
2156 find_answer (cpp_hashnode *node, const struct answer *candidate)
2157 {
2158 unsigned int i;
2159 struct answer **result;
2160
2161 for (result = &node->value.answers; *result; result = &(*result)->next)
2162 {
2163 struct answer *answer = *result;
2164
2165 if (answer->count == candidate->count)
2166 {
2167 for (i = 0; i < answer->count; i++)
2168 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2169 break;
2170
2171 if (i == answer->count)
2172 break;
2173 }
2174 }
2175
2176 return result;
2177 }
2178
2179 /* Test an assertion within a preprocessor conditional. Returns
2180 nonzero on failure, zero on success. On success, the result of
2181 the test is written into VALUE, otherwise the value 0. */
2182 int
2183 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2184 {
2185 struct answer *answer;
2186 cpp_hashnode *node;
2187
2188 node = parse_assertion (pfile, &answer, T_IF);
2189
2190 /* For recovery, an erroneous assertion expression is handled as a
2191 failing assertion. */
2192 *value = 0;
2193
2194 if (node)
2195 *value = (node->type == NT_ASSERTION &&
2196 (answer == 0 || *find_answer (node, answer) != 0));
2197 else if (pfile->cur_token[-1].type == CPP_EOF)
2198 _cpp_backup_tokens (pfile, 1);
2199
2200 /* We don't commit the memory for the answer - it's temporary only. */
2201 return node == 0;
2202 }
2203
2204 /* Handle #assert. */
2205 static void
2206 do_assert (cpp_reader *pfile)
2207 {
2208 struct answer *new_answer;
2209 cpp_hashnode *node;
2210
2211 node = parse_assertion (pfile, &new_answer, T_ASSERT);
2212 if (node)
2213 {
2214 size_t answer_size;
2215
2216 /* Place the new answer in the answer list. First check there
2217 is not a duplicate. */
2218 new_answer->next = 0;
2219 if (node->type == NT_ASSERTION)
2220 {
2221 if (*find_answer (node, new_answer))
2222 {
2223 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2224 NODE_NAME (node) + 1);
2225 return;
2226 }
2227 new_answer->next = node->value.answers;
2228 }
2229
2230 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2231 * sizeof (cpp_token));
2232 /* Commit or allocate storage for the object. */
2233 if (pfile->hash_table->alloc_subobject)
2234 {
2235 struct answer *temp_answer = new_answer;
2236 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2237 (answer_size);
2238 memcpy (new_answer, temp_answer, answer_size);
2239 }
2240 else
2241 BUFF_FRONT (pfile->a_buff) += answer_size;
2242
2243 node->type = NT_ASSERTION;
2244 node->value.answers = new_answer;
2245 check_eol (pfile, false);
2246 }
2247 }
2248
2249 /* Handle #unassert. */
2250 static void
2251 do_unassert (cpp_reader *pfile)
2252 {
2253 cpp_hashnode *node;
2254 struct answer *answer;
2255
2256 node = parse_assertion (pfile, &answer, T_UNASSERT);
2257 /* It isn't an error to #unassert something that isn't asserted. */
2258 if (node && node->type == NT_ASSERTION)
2259 {
2260 if (answer)
2261 {
2262 struct answer **p = find_answer (node, answer), *temp;
2263
2264 /* Remove the answer from the list. */
2265 temp = *p;
2266 if (temp)
2267 *p = temp->next;
2268
2269 /* Did we free the last answer? */
2270 if (node->value.answers == 0)
2271 node->type = NT_VOID;
2272
2273 check_eol (pfile, false);
2274 }
2275 else
2276 _cpp_free_definition (node);
2277 }
2278
2279 /* We don't commit the memory for the answer - it's temporary only. */
2280 }
2281
2282 /* These are for -D, -U, -A. */
2283
2284 /* Process the string STR as if it appeared as the body of a #define.
2285 If STR is just an identifier, define it with value 1.
2286 If STR has anything after the identifier, then it should
2287 be identifier=definition. */
2288 void
2289 cpp_define (cpp_reader *pfile, const char *str)
2290 {
2291 char *buf;
2292 const char *p;
2293 size_t count;
2294
2295 /* Copy the entire option so we can modify it.
2296 Change the first "=" in the string to a space. If there is none,
2297 tack " 1" on the end. */
2298
2299 count = strlen (str);
2300 buf = (char *) alloca (count + 3);
2301 memcpy (buf, str, count);
2302
2303 p = strchr (str, '=');
2304 if (p)
2305 buf[p - str] = ' ';
2306 else
2307 {
2308 buf[count++] = ' ';
2309 buf[count++] = '1';
2310 }
2311 buf[count] = '\n';
2312
2313 run_directive (pfile, T_DEFINE, buf, count);
2314 }
2315
2316
2317 /* Use to build macros to be run through cpp_define() as
2318 described above.
2319 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2320
2321 void
2322 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2323 {
2324 char *ptr = NULL;
2325
2326 va_list ap;
2327 va_start (ap, fmt);
2328 vasprintf (&ptr, fmt, ap);
2329 va_end (ap);
2330
2331 cpp_define (pfile, ptr);
2332 free (ptr);
2333 }
2334
2335
2336 /* Slight variant of the above for use by initialize_builtins. */
2337 void
2338 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2339 {
2340 size_t len = strlen (str);
2341 char *buf = (char *) alloca (len + 1);
2342 memcpy (buf, str, len);
2343 buf[len] = '\n';
2344 run_directive (pfile, T_DEFINE, buf, len);
2345 }
2346
2347 /* Process MACRO as if it appeared as the body of an #undef. */
2348 void
2349 cpp_undef (cpp_reader *pfile, const char *macro)
2350 {
2351 size_t len = strlen (macro);
2352 char *buf = (char *) alloca (len + 1);
2353 memcpy (buf, macro, len);
2354 buf[len] = '\n';
2355 run_directive (pfile, T_UNDEF, buf, len);
2356 }
2357
2358 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2359 or first element is zero, then the macro should be undefined. */
2360 static void
2361 cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
2362 {
2363 cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
2364 if (node == NULL)
2365 return;
2366
2367 if (pfile->cb.before_define)
2368 pfile->cb.before_define (pfile);
2369
2370 if (node->type == NT_MACRO)
2371 {
2372 if (pfile->cb.undef)
2373 pfile->cb.undef (pfile, pfile->directive_line, node);
2374 if (CPP_OPTION (pfile, warn_unused_macros))
2375 _cpp_warn_if_unused_macro (pfile, node, NULL);
2376 }
2377 if (node->type != NT_VOID)
2378 _cpp_free_definition (node);
2379
2380 if (c->is_undef)
2381 return;
2382 {
2383 size_t namelen;
2384 const uchar *dn;
2385 cpp_hashnode *h = NULL;
2386 cpp_buffer *nbuf;
2387
2388 namelen = ustrcspn (c->definition, "( \n");
2389 h = cpp_lookup (pfile, c->definition, namelen);
2390 dn = c->definition + namelen;
2391
2392 h->type = NT_VOID;
2393 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
2394 nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true);
2395 if (nbuf != NULL)
2396 {
2397 _cpp_clean_line (pfile);
2398 nbuf->sysp = 1;
2399 if (!_cpp_create_definition (pfile, h))
2400 abort ();
2401 _cpp_pop_buffer (pfile);
2402 }
2403 else
2404 abort ();
2405 h->value.macro->line = c->line;
2406 h->value.macro->syshdr = c->syshdr;
2407 h->value.macro->used = c->used;
2408 }
2409 }
2410
2411 /* Process the string STR as if it appeared as the body of a #assert. */
2412 void
2413 cpp_assert (cpp_reader *pfile, const char *str)
2414 {
2415 handle_assertion (pfile, str, T_ASSERT);
2416 }
2417
2418 /* Process STR as if it appeared as the body of an #unassert. */
2419 void
2420 cpp_unassert (cpp_reader *pfile, const char *str)
2421 {
2422 handle_assertion (pfile, str, T_UNASSERT);
2423 }
2424
2425 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2426 static void
2427 handle_assertion (cpp_reader *pfile, const char *str, int type)
2428 {
2429 size_t count = strlen (str);
2430 const char *p = strchr (str, '=');
2431
2432 /* Copy the entire option so we can modify it. Change the first
2433 "=" in the string to a '(', and tack a ')' on the end. */
2434 char *buf = (char *) alloca (count + 2);
2435
2436 memcpy (buf, str, count);
2437 if (p)
2438 {
2439 buf[p - str] = '(';
2440 buf[count++] = ')';
2441 }
2442 buf[count] = '\n';
2443 str = buf;
2444
2445 run_directive (pfile, type, str, count);
2446 }
2447
2448 /* The options structure. */
2449 cpp_options *
2450 cpp_get_options (cpp_reader *pfile)
2451 {
2452 return &pfile->opts;
2453 }
2454
2455 /* The callbacks structure. */
2456 cpp_callbacks *
2457 cpp_get_callbacks (cpp_reader *pfile)
2458 {
2459 return &pfile->cb;
2460 }
2461
2462 /* Copy the given callbacks structure to our own. */
2463 void
2464 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2465 {
2466 pfile->cb = *cb;
2467 }
2468
2469 /* The dependencies structure. (Creates one if it hasn't already been.) */
2470 struct deps *
2471 cpp_get_deps (cpp_reader *pfile)
2472 {
2473 if (!pfile->deps)
2474 pfile->deps = deps_init ();
2475 return pfile->deps;
2476 }
2477
2478 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2479 doesn't fail. It does not generate a file change call back; that
2480 is the responsibility of the caller. */
2481 cpp_buffer *
2482 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2483 int from_stage3)
2484 {
2485 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2486
2487 /* Clears, amongst other things, if_stack and mi_cmacro. */
2488 memset (new_buffer, 0, sizeof (cpp_buffer));
2489
2490 new_buffer->next_line = new_buffer->buf = buffer;
2491 new_buffer->rlimit = buffer + len;
2492 new_buffer->from_stage3 = from_stage3;
2493 new_buffer->prev = pfile->buffer;
2494 new_buffer->need_line = true;
2495
2496 pfile->buffer = new_buffer;
2497
2498 return new_buffer;
2499 }
2500
2501 /* Pops a single buffer, with a file change call-back if appropriate.
2502 Then pushes the next -include file, if any remain. */
2503 void
2504 _cpp_pop_buffer (cpp_reader *pfile)
2505 {
2506 cpp_buffer *buffer = pfile->buffer;
2507 struct _cpp_file *inc = buffer->file;
2508 struct if_stack *ifs;
2509
2510 /* Walk back up the conditional stack till we reach its level at
2511 entry to this file, issuing error messages. */
2512 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2513 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2514 "unterminated #%s", dtable[ifs->type].name);
2515
2516 /* In case of a missing #endif. */
2517 pfile->state.skipping = 0;
2518
2519 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2520 pfile->buffer = buffer->prev;
2521
2522 free (buffer->notes);
2523
2524 /* Free the buffer object now; we may want to push a new buffer
2525 in _cpp_push_next_include_file. */
2526 obstack_free (&pfile->buffer_ob, buffer);
2527
2528 if (inc)
2529 {
2530 _cpp_pop_file_buffer (pfile, inc);
2531
2532 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2533 }
2534 }
2535
2536 /* Enter all recognized directives in the hash table. */
2537 void
2538 _cpp_init_directives (cpp_reader *pfile)
2539 {
2540 unsigned int i;
2541 cpp_hashnode *node;
2542
2543 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2544 {
2545 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2546 node->is_directive = 1;
2547 node->directive_index = i;
2548 }
2549 }