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