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