1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986-2018 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
28 /* Stack of conditionals currently in progress
29 (including both successful and failing conditionals). */
32 struct if_stack
*next
;
33 source_location line
; /* Line where condition started. */
34 const cpp_hashnode
*mi_cmacro
;/* macro name for #ifndef around entire file */
35 bool skip_elses
; /* Can future #else / #elif be skipped? */
36 bool was_skipping
; /* If were skipping on entry. */
37 int type
; /* Most recent conditional for diagnostics. */
40 /* Contains a registered pragma or pragma namespace. */
41 typedef void (*pragma_cb
) (cpp_reader
*);
44 struct pragma_entry
*next
;
45 const cpp_hashnode
*pragma
; /* Name and length. */
52 struct pragma_entry
*space
;
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. */
64 /* Values for the flags field of struct directive. COND indicates a
65 conditional; IF_COND an opening conditional. INCL means to treat
66 "..." and <...> as q-char and h-char sequences respectively. IN_I
67 means this directive should be handled even if -fpreprocessed is in
68 effect (these are the directives with callback hooks).
70 EXPAND is set on directives that are always macro-expanded. */
72 #define IF_COND (1 << 1)
75 #define EXPAND (1 << 4)
76 #define DEPRECATED (1 << 5)
78 /* Defines one #-directive, including how to handle it. */
79 typedef void (*directive_handler
) (cpp_reader
*);
80 typedef struct directive directive
;
83 directive_handler handler
; /* Function to handle directive. */
84 const uchar
*name
; /* Name of directive. */
85 unsigned short length
; /* Length of name. */
86 unsigned char origin
; /* Origin of directive. */
87 unsigned char flags
; /* Flags describing this directive. */
90 /* Forward declarations. */
92 static void skip_rest_of_line (cpp_reader
*);
93 static void check_eol (cpp_reader
*, bool);
94 static void start_directive (cpp_reader
*);
95 static void prepare_directive_trad (cpp_reader
*);
96 static void end_directive (cpp_reader
*, int);
97 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
98 static void run_directive (cpp_reader
*, int, const char *, size_t);
99 static char *glue_header_name (cpp_reader
*);
100 static const char *parse_include (cpp_reader
*, int *, const cpp_token
***,
102 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
103 static unsigned int read_flag (cpp_reader
*, unsigned int);
104 static bool strtolinenum (const uchar
*, size_t, linenum_type
*, bool *);
105 static void do_diagnostic (cpp_reader
*, int, int, int);
106 static cpp_hashnode
*lex_macro_node (cpp_reader
*, bool);
107 static int undefine_macros (cpp_reader
*, cpp_hashnode
*, void *);
108 static void do_include_common (cpp_reader
*, enum include_type
);
109 static struct pragma_entry
*lookup_pragma_entry (struct pragma_entry
*,
110 const cpp_hashnode
*);
111 static int count_registered_pragmas (struct pragma_entry
*);
112 static char ** save_registered_pragmas (struct pragma_entry
*, char **);
113 static char ** restore_registered_pragmas (cpp_reader
*, struct pragma_entry
*,
115 static void do_pragma_once (cpp_reader
*);
116 static void do_pragma_poison (cpp_reader
*);
117 static void do_pragma_system_header (cpp_reader
*);
118 static void do_pragma_dependency (cpp_reader
*);
119 static void do_pragma_warning_or_error (cpp_reader
*, bool error
);
120 static void do_pragma_warning (cpp_reader
*);
121 static void do_pragma_error (cpp_reader
*);
122 static void do_linemarker (cpp_reader
*);
123 static const cpp_token
*get_token_no_padding (cpp_reader
*);
124 static const cpp_token
*get__Pragma_string (cpp_reader
*);
125 static void destringize_and_run (cpp_reader
*, const cpp_string
*,
127 static int parse_answer (cpp_reader
*, struct answer
**, int, source_location
);
128 static cpp_hashnode
*parse_assertion (cpp_reader
*, struct answer
**, int);
129 static struct answer
** find_answer (cpp_hashnode
*, const struct answer
*);
130 static void handle_assertion (cpp_reader
*, const char *, int);
131 static void do_pragma_push_macro (cpp_reader
*);
132 static void do_pragma_pop_macro (cpp_reader
*);
133 static void cpp_pop_definition (cpp_reader
*, struct def_pragma_macro
*);
135 /* This is the table of directive handlers. It is ordered by
136 frequency of occurrence; the numbers at the end are directive
137 counts from all the source code I have lying around (egcs and libc
138 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
139 pcmcia-cs-3.0.9). This is no longer important as directive lookup
140 is now O(1). All extensions other than #warning, #include_next,
141 and #import are deprecated. The name is where the extension
142 appears to have come from. */
144 #define DIRECTIVE_TABLE \
145 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
146 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
147 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
148 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
149 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
150 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
151 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
152 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
153 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
154 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
155 D(error, T_ERROR, STDC89, 0) /* 475 */ \
156 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
157 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
158 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
159 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
160 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
161 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
162 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
163 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
165 /* #sccs is synonymous with #ident. */
166 #define do_sccs do_ident
168 /* Use the table to generate a series of prototypes, an enum for the
169 directive names, and an array of directive handlers. */
171 #define D(name, t, o, f) static void do_##name (cpp_reader *);
175 #define D(n, tag, o, f) tag,
183 #define D(name, t, origin, flags) \
184 { do_##name, (const uchar *) #name, \
185 sizeof #name - 1, origin, flags },
186 static const directive dtable
[] =
192 /* A NULL-terminated array of directive names for use
193 when suggesting corrections for misspelled directives. */
194 #define D(name, t, origin, flags) #name,
195 static const char * const directive_names
[] = {
201 #undef DIRECTIVE_TABLE
203 /* Wrapper struct directive for linemarkers.
204 The origin is more or less true - the original K+R cpp
205 did use this notation in its preprocessed output. */
206 static const directive linemarker_dir
=
208 do_linemarker
, UC
"#", 1, KANDR
, IN_I
211 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
213 /* Skip any remaining tokens in a directive. */
215 skip_rest_of_line (cpp_reader
*pfile
)
217 /* Discard all stacked contexts. */
218 while (pfile
->context
->prev
)
219 _cpp_pop_context (pfile
);
221 /* Sweep up all tokens remaining on the line. */
223 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
227 /* Helper function for check_oel. */
230 check_eol_1 (cpp_reader
*pfile
, bool expand
, int reason
)
232 if (! SEEN_EOL () && (expand
233 ? cpp_get_token (pfile
)
234 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
235 cpp_pedwarning (pfile
, reason
, "extra tokens at end of #%s directive",
236 pfile
->directive
->name
);
239 /* Variant of check_eol used for Wendif-labels warnings. */
242 check_eol_endif_labels (cpp_reader
*pfile
)
244 check_eol_1 (pfile
, false, CPP_W_ENDIF_LABELS
);
247 /* Ensure there are no stray tokens at the end of a directive. If
248 EXPAND is true, tokens macro-expanding to nothing are allowed. */
251 check_eol (cpp_reader
*pfile
, bool expand
)
253 check_eol_1 (pfile
, expand
, CPP_W_NONE
);
256 /* Ensure there are no stray tokens other than comments at the end of
257 a directive, and gather the comments. */
258 static const cpp_token
**
259 check_eol_return_comments (cpp_reader
*pfile
)
263 const cpp_token
**buf
;
265 buf
= XNEWVEC (const cpp_token
*, capacity
);
271 const cpp_token
*tok
;
273 tok
= _cpp_lex_token (pfile
);
274 if (tok
->type
== CPP_EOF
)
276 if (tok
->type
!= CPP_COMMENT
)
277 cpp_error (pfile
, CPP_DL_PEDWARN
,
278 "extra tokens at end of #%s directive",
279 pfile
->directive
->name
);
282 if (c
+ 1 >= capacity
)
285 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
296 /* Called when entering a directive, _Pragma or command-line directive. */
298 start_directive (cpp_reader
*pfile
)
300 /* Setup in-directive state. */
301 pfile
->state
.in_directive
= 1;
302 pfile
->state
.save_comments
= 0;
303 pfile
->directive_result
.type
= CPP_PADDING
;
305 /* Some handlers need the position of the # for diagnostics. */
306 pfile
->directive_line
= pfile
->line_table
->highest_line
;
309 /* Called when leaving a directive, _Pragma or command-line directive. */
311 end_directive (cpp_reader
*pfile
, int skip_line
)
313 if (CPP_OPTION (pfile
, traditional
))
315 /* Revert change of prepare_directive_trad. */
316 if (!pfile
->state
.in_deferred_pragma
)
317 pfile
->state
.prevent_expansion
--;
319 if (pfile
->directive
!= &dtable
[T_DEFINE
])
320 _cpp_remove_overlay (pfile
);
322 else if (pfile
->state
.in_deferred_pragma
)
324 /* We don't skip for an assembler #. */
327 skip_rest_of_line (pfile
);
328 if (!pfile
->keep_tokens
)
330 pfile
->cur_run
= &pfile
->base_run
;
331 pfile
->cur_token
= pfile
->base_run
.base
;
336 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
337 pfile
->state
.in_directive
= 0;
338 pfile
->state
.in_expression
= 0;
339 pfile
->state
.angled_headers
= 0;
340 pfile
->directive
= 0;
343 /* Prepare to handle the directive in pfile->directive. */
345 prepare_directive_trad (cpp_reader
*pfile
)
347 if (pfile
->directive
!= &dtable
[T_DEFINE
])
349 bool no_expand
= (pfile
->directive
350 && ! (pfile
->directive
->flags
& EXPAND
));
351 bool was_skipping
= pfile
->state
.skipping
;
353 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
354 || pfile
->directive
== &dtable
[T_ELIF
]);
355 if (pfile
->state
.in_expression
)
356 pfile
->state
.skipping
= false;
359 pfile
->state
.prevent_expansion
++;
360 _cpp_scan_out_logical_line (pfile
, NULL
, false);
362 pfile
->state
.prevent_expansion
--;
364 pfile
->state
.skipping
= was_skipping
;
365 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
366 pfile
->out
.cur
- pfile
->out
.base
);
369 /* Stop ISO C from expanding anything. */
370 pfile
->state
.prevent_expansion
++;
373 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
374 the '#' was indented. */
376 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
378 /* Issue -pedantic or deprecated warnings for extensions. We let
379 -pedantic take precedence if both are applicable. */
380 if (! pfile
->state
.skipping
)
382 if (dir
->origin
== EXTENSION
383 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
384 && CPP_PEDANTIC (pfile
))
385 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
386 else if (((dir
->flags
& DEPRECATED
) != 0
387 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
388 && CPP_OPTION (pfile
, cpp_warn_deprecated
))
389 cpp_warning (pfile
, CPP_W_DEPRECATED
,
390 "#%s is a deprecated GCC extension", dir
->name
);
393 /* Traditionally, a directive is ignored unless its # is in
394 column 1. Therefore in code intended to work with K+R
395 compilers, directives added by C89 must have their #
396 indented, and directives present in traditional C must not.
397 This is true even of directives in skipped conditional
398 blocks. #elif cannot be used at all. */
399 if (CPP_WTRADITIONAL (pfile
))
401 if (dir
== &dtable
[T_ELIF
])
402 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
403 "suggest not using #elif in traditional C");
404 else if (indented
&& dir
->origin
== KANDR
)
405 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
406 "traditional C ignores #%s with the # indented",
408 else if (!indented
&& dir
->origin
!= KANDR
)
409 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
410 "suggest hiding #%s from traditional C with an indented #",
415 /* Check if we have a known directive. INDENTED is nonzero if the
416 '#' of the directive was indented. This function is in this file
417 to save unnecessarily exporting dtable etc. to lex.c. Returns
418 nonzero if the line of tokens has been handled, zero if we should
419 continue processing the line. */
421 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
423 const directive
*dir
= 0;
424 const cpp_token
*dname
;
425 bool was_parsing_args
= pfile
->state
.parsing_args
;
426 bool was_discarding_output
= pfile
->state
.discarding_output
;
429 if (was_discarding_output
)
430 pfile
->state
.prevent_expansion
= 0;
432 if (was_parsing_args
)
434 if (CPP_OPTION (pfile
, cpp_pedantic
))
435 cpp_error (pfile
, CPP_DL_PEDWARN
,
436 "embedding a directive within macro arguments is not portable");
437 pfile
->state
.parsing_args
= 0;
438 pfile
->state
.prevent_expansion
= 0;
440 start_directive (pfile
);
441 dname
= _cpp_lex_token (pfile
);
443 if (dname
->type
== CPP_NAME
)
445 if (dname
->val
.node
.node
->is_directive
)
446 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
448 /* We do not recognize the # followed by a number extension in
450 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
452 dir
= &linemarker_dir
;
453 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
454 && ! pfile
->state
.skipping
)
455 cpp_error (pfile
, CPP_DL_PEDWARN
,
456 "style of line directive is a GCC extension");
461 /* If we have a directive that is not an opening conditional,
462 invalidate any control macro. */
463 if (! (dir
->flags
& IF_COND
))
464 pfile
->mi_valid
= false;
466 /* Kluge alert. In order to be sure that code like this
471 does not cause '#define foo bar' to get executed when
472 compiled with -save-temps, we recognize directives in
473 -fpreprocessed mode only if the # is in column 1. macro.c
474 puts a space in front of any '#' at the start of a macro.
476 We exclude the -fdirectives-only case because macro expansion
477 has not been performed yet, and block comments can cause spaces
478 to precede the directive. */
479 if (CPP_OPTION (pfile
, preprocessed
)
480 && !CPP_OPTION (pfile
, directives_only
)
481 && (indented
|| !(dir
->flags
& IN_I
)))
488 /* In failed conditional groups, all non-conditional
489 directives are ignored. Before doing that, whether
490 skipping or not, we should lex angle-bracketed headers
491 correctly, and maybe output some diagnostics. */
492 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
493 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
494 if (! CPP_OPTION (pfile
, preprocessed
))
495 directive_diagnostics (pfile
, dir
, indented
);
496 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
500 else if (dname
->type
== CPP_EOF
)
501 ; /* CPP_EOF is the "null directive". */
504 /* An unknown directive. Don't complain about it in assembly
505 source: we don't know where the comments are, and # may
506 introduce assembler pseudo-ops. Don't complain about invalid
507 directives in skipped conditional groups (6.10 p4). */
508 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
510 else if (!pfile
->state
.skipping
)
512 const char *unrecognized
513 = (const char *)cpp_token_as_text (pfile
, dname
);
514 const char *hint
= NULL
;
516 /* Call back into gcc to get a spelling suggestion. Ideally
517 we'd just use best_match from gcc/spellcheck.h (and filter
518 out the uncommon directives), but that requires moving it
519 to a support library. */
520 if (pfile
->cb
.get_suggestion
)
521 hint
= pfile
->cb
.get_suggestion (pfile
, unrecognized
,
526 rich_location
richloc (pfile
->line_table
, dname
->src_loc
);
527 source_range misspelled_token_range
528 = get_range_from_loc (pfile
->line_table
, dname
->src_loc
);
529 richloc
.add_fixit_replace (misspelled_token_range
, hint
);
530 cpp_error_at (pfile
, CPP_DL_ERROR
, &richloc
,
531 "invalid preprocessing directive #%s;"
532 " did you mean #%s?",
536 cpp_error (pfile
, CPP_DL_ERROR
,
537 "invalid preprocessing directive #%s",
542 pfile
->directive
= dir
;
543 if (CPP_OPTION (pfile
, traditional
))
544 prepare_directive_trad (pfile
);
547 pfile
->directive
->handler (pfile
);
549 _cpp_backup_tokens (pfile
, 1);
551 end_directive (pfile
, skip
);
552 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
554 /* Restore state when within macro args. */
555 pfile
->state
.parsing_args
= 2;
556 pfile
->state
.prevent_expansion
= 1;
558 if (was_discarding_output
)
559 pfile
->state
.prevent_expansion
= 1;
563 /* Directive handler wrapper used by the command line option
564 processor. BUF is \n terminated. */
566 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
568 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
569 /* from_stage3 */ true);
570 start_directive (pfile
);
572 /* This is a short-term fix to prevent a leading '#' being
573 interpreted as a directive. */
574 _cpp_clean_line (pfile
);
576 pfile
->directive
= &dtable
[dir_no
];
577 if (CPP_OPTION (pfile
, traditional
))
578 prepare_directive_trad (pfile
);
579 pfile
->directive
->handler (pfile
);
580 end_directive (pfile
, 1);
581 _cpp_pop_buffer (pfile
);
584 /* Checks for validity the macro name in #define, #undef, #ifdef and
585 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
586 processing a #define or #undefine directive, and false
588 static cpp_hashnode
*
589 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
591 const cpp_token
*token
= _cpp_lex_token (pfile
);
593 /* The token immediately after #define must be an identifier. That
594 identifier may not be "defined", per C99 6.10.8p4.
595 In C++, it may not be any of the "named operators" either,
596 per C++98 [lex.digraph], [lex.key].
597 Finally, the identifier may not have been poisoned. (In that case
598 the lexer has issued the error message for us.) */
600 if (token
->type
== CPP_NAME
)
602 cpp_hashnode
*node
= token
->val
.node
.node
;
604 if (is_def_or_undef
&& node
== pfile
->spec_nodes
.n_defined
)
605 cpp_error (pfile
, CPP_DL_ERROR
,
606 "\"defined\" cannot be used as a macro name");
607 else if (is_def_or_undef
608 && (node
== pfile
->spec_nodes
.n__has_include__
609 || node
== pfile
->spec_nodes
.n__has_include_next__
))
610 cpp_error (pfile
, CPP_DL_ERROR
,
611 "\"__has_include__\" cannot be used as a macro name");
612 else if (! (node
->flags
& NODE_POISONED
))
615 else if (token
->flags
& NAMED_OP
)
616 cpp_error (pfile
, CPP_DL_ERROR
,
617 "\"%s\" cannot be used as a macro name as it is an operator in C++",
618 NODE_NAME (token
->val
.node
.node
));
619 else if (token
->type
== CPP_EOF
)
620 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
621 pfile
->directive
->name
);
623 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
628 /* Process a #define directive. Most work is done in macro.c. */
630 do_define (cpp_reader
*pfile
)
632 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
636 /* If we have been requested to expand comments into macros,
637 then re-enable saving of comments. */
638 pfile
->state
.save_comments
=
639 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
641 if (pfile
->cb
.before_define
)
642 pfile
->cb
.before_define (pfile
);
644 if (_cpp_create_definition (pfile
, node
))
645 if (pfile
->cb
.define
)
646 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
648 node
->flags
&= ~NODE_USED
;
652 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
654 do_undef (cpp_reader
*pfile
)
656 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
660 if (pfile
->cb
.before_define
)
661 pfile
->cb
.before_define (pfile
);
664 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
666 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
667 identifier is not currently defined as a macro name. */
668 if (node
->type
== NT_MACRO
)
670 if (node
->flags
& NODE_WARN
)
671 cpp_error (pfile
, CPP_DL_WARNING
,
672 "undefining \"%s\"", NODE_NAME (node
));
673 else if ((node
->flags
& NODE_BUILTIN
)
674 && CPP_OPTION (pfile
, warn_builtin_macro_redefined
))
675 cpp_warning_with_line (pfile
, CPP_W_BUILTIN_MACRO_REDEFINED
,
676 pfile
->directive_line
, 0,
677 "undefining \"%s\"", NODE_NAME (node
));
679 if (CPP_OPTION (pfile
, warn_unused_macros
))
680 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
682 _cpp_free_definition (node
);
686 check_eol (pfile
, false);
689 /* Undefine a single macro/assertion/whatever. */
692 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
693 void *data_p ATTRIBUTE_UNUSED
)
695 /* Body of _cpp_free_definition inlined here for speed.
696 Macros and assertions no longer have anything to free. */
698 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
|NODE_USED
);
702 /* Undefine all macros and assertions. */
705 cpp_undef_all (cpp_reader
*pfile
)
707 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
711 /* Helper routine used by parse_include. Reinterpret the current line
712 as an h-char-sequence (< ... >); we are looking at the first token
713 after the <. Returns a malloced filename. */
715 glue_header_name (cpp_reader
*pfile
)
717 const cpp_token
*token
;
719 size_t len
, total_len
= 0, capacity
= 1024;
721 /* To avoid lexed tokens overwriting our glued name, we can only
722 allocate from the string pool once we've lexed everything. */
723 buffer
= XNEWVEC (char, capacity
);
726 token
= get_token_no_padding (pfile
);
728 if (token
->type
== CPP_GREATER
)
730 if (token
->type
== CPP_EOF
)
732 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
736 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
737 if (total_len
+ len
> capacity
)
739 capacity
= (capacity
+ len
) * 2;
740 buffer
= XRESIZEVEC (char, buffer
, capacity
);
743 if (token
->flags
& PREV_WHITE
)
744 buffer
[total_len
++] = ' ';
746 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
751 buffer
[total_len
] = '\0';
755 /* Returns the file name of #include, #include_next, #import and
756 #pragma dependency. The string is malloced and the caller should
757 free it. Returns NULL on error. LOCATION is the source location
761 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
762 const cpp_token
***buf
, source_location
*location
)
765 const cpp_token
*header
;
767 /* Allow macro expansion. */
768 header
= get_token_no_padding (pfile
);
769 *location
= header
->src_loc
;
770 if ((header
->type
== CPP_STRING
&& header
->val
.str
.text
[0] != 'R')
771 || header
->type
== CPP_HEADER_NAME
)
773 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
774 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
775 fname
[header
->val
.str
.len
- 2] = '\0';
776 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
778 else if (header
->type
== CPP_LESS
)
780 fname
= glue_header_name (pfile
);
781 *pangle_brackets
= 1;
785 const unsigned char *dir
;
787 if (pfile
->directive
== &dtable
[T_PRAGMA
])
788 dir
= UC
"pragma dependency";
790 dir
= pfile
->directive
->name
;
791 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
797 if (pfile
->directive
== &dtable
[T_PRAGMA
])
799 /* This pragma allows extra tokens after the file name. */
801 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
802 check_eol (pfile
, true);
805 /* If we are not discarding comments, then gather them while
806 doing the eol check. */
807 *buf
= check_eol_return_comments (pfile
);
813 /* Handle #include, #include_next and #import. */
815 do_include_common (cpp_reader
*pfile
, enum include_type type
)
819 const cpp_token
**buf
= NULL
;
820 source_location location
;
822 /* Re-enable saving of comments if requested, so that the include
823 callback can dump comments which follow #include. */
824 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
826 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
836 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
837 "empty filename in #%s",
838 pfile
->directive
->name
);
845 /* Prevent #include recursion. */
846 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
847 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
850 /* Get out of macro context, if we are. */
851 skip_rest_of_line (pfile
);
853 if (pfile
->cb
.include
)
854 pfile
->cb
.include (pfile
, pfile
->directive_line
,
855 pfile
->directive
->name
, fname
, angle_brackets
,
858 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
, location
);
867 do_include (cpp_reader
*pfile
)
869 do_include_common (pfile
, IT_INCLUDE
);
873 do_import (cpp_reader
*pfile
)
875 do_include_common (pfile
, IT_IMPORT
);
879 do_include_next (cpp_reader
*pfile
)
881 enum include_type type
= IT_INCLUDE_NEXT
;
883 /* If this is the primary source file, warn and use the normal
885 if (cpp_in_primary_file (pfile
))
887 cpp_error (pfile
, CPP_DL_WARNING
,
888 "#include_next in primary source file");
891 do_include_common (pfile
, type
);
894 /* Subroutine of do_linemarker. Read possible flags after file name.
895 LAST is the last flag seen; 0 if this is the first flag. Return the
896 flag if it is valid, 0 at the end of the directive. Otherwise
899 read_flag (cpp_reader
*pfile
, unsigned int last
)
901 const cpp_token
*token
= _cpp_lex_token (pfile
);
903 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
905 unsigned int flag
= token
->val
.str
.text
[0] - '0';
907 if (flag
> last
&& flag
<= 4
908 && (flag
!= 4 || last
== 3)
909 && (flag
!= 2 || last
== 0))
913 if (token
->type
!= CPP_EOF
)
914 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
915 cpp_token_as_text (pfile
, token
));
919 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
920 of length LEN, to binary; store it in NUMP, and return false if the
921 number was well-formed, true if not. WRAPPED is set to true if the
922 number did not fit into 'unsigned long'. */
924 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
926 linenum_type reg
= 0;
927 linenum_type reg_prev
= 0;
946 /* Interpret #line command.
947 Note that the filename string (if any) is a true string constant
948 (escapes are interpreted), unlike in #line. */
950 do_line (cpp_reader
*pfile
)
952 struct line_maps
*line_table
= pfile
->line_table
;
953 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
955 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
958 unsigned char map_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
959 const cpp_token
*token
;
960 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
961 linenum_type new_lineno
;
963 /* C99 raised the minimum limit on #line numbers. */
964 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
967 /* #line commands expand macros. */
968 token
= cpp_get_token (pfile
);
969 if (token
->type
!= CPP_NUMBER
970 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
971 &new_lineno
, &wrapped
))
973 if (token
->type
== CPP_EOF
)
974 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
976 cpp_error (pfile
, CPP_DL_ERROR
,
977 "\"%s\" after #line is not a positive integer",
978 cpp_token_as_text (pfile
, token
));
982 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
983 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
985 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
987 token
= cpp_get_token (pfile
);
988 if (token
->type
== CPP_STRING
)
990 cpp_string s
= { 0, 0 };
991 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
993 new_file
= (const char *)s
.text
;
994 check_eol (pfile
, true);
996 else if (token
->type
!= CPP_EOF
)
998 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
999 cpp_token_as_text (pfile
, token
));
1003 skip_rest_of_line (pfile
);
1004 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
1006 line_table
->seen_line_directive
= true;
1009 /* Interpret the # 44 "file" [flags] notation, which has slightly
1010 different syntax and semantics from #line: Flags are allowed,
1011 and we never complain about the line number being too big. */
1013 do_linemarker (cpp_reader
*pfile
)
1015 struct line_maps
*line_table
= pfile
->line_table
;
1016 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1017 const cpp_token
*token
;
1018 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
1019 linenum_type new_lineno
;
1020 unsigned int new_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
1021 enum lc_reason reason
= LC_RENAME_VERBATIM
;
1025 /* Back up so we can get the number again. Putting this in
1026 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
1027 some circumstances, which can segfault. */
1028 _cpp_backup_tokens (pfile
, 1);
1030 /* #line commands expand macros. */
1031 token
= cpp_get_token (pfile
);
1032 if (token
->type
!= CPP_NUMBER
1033 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
1034 &new_lineno
, &wrapped
))
1036 /* Unlike #line, there does not seem to be a way to get an EOF
1037 here. So, it should be safe to always spell the token. */
1038 cpp_error (pfile
, CPP_DL_ERROR
,
1039 "\"%s\" after # is not a positive integer",
1040 cpp_token_as_text (pfile
, token
));
1044 token
= cpp_get_token (pfile
);
1045 if (token
->type
== CPP_STRING
)
1047 cpp_string s
= { 0, 0 };
1048 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
1050 new_file
= (const char *)s
.text
;
1053 flag
= read_flag (pfile
, 0);
1057 /* Fake an include for cpp_included (). */
1058 _cpp_fake_include (pfile
, new_file
);
1059 flag
= read_flag (pfile
, flag
);
1064 flag
= read_flag (pfile
, flag
);
1069 flag
= read_flag (pfile
, flag
);
1073 pfile
->buffer
->sysp
= new_sysp
;
1075 check_eol (pfile
, false);
1077 else if (token
->type
!= CPP_EOF
)
1079 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1080 cpp_token_as_text (pfile
, token
));
1084 skip_rest_of_line (pfile
);
1086 if (reason
== LC_LEAVE
)
1088 /* Reread map since cpp_get_token can invalidate it with a
1090 map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1091 const line_map_ordinary
*from
1092 = linemap_included_from_linemap (line_table
, map
);
1093 if (MAIN_FILE_P (map
)
1095 && filename_cmp (ORDINARY_MAP_FILE_NAME (from
), new_file
) != 0))
1097 cpp_warning (pfile
, CPP_W_NONE
,
1098 "file \"%s\" linemarker ignored due to "
1099 "incorrect nesting", new_file
);
1103 /* Compensate for the increment in linemap_add that occurs in
1104 _cpp_do_file_change. We're currently at the start of the line
1105 *following* the #line directive. A separate source_location for this
1106 location makes no sense (until we do the LC_LEAVE), and
1107 complicates LAST_SOURCE_LINE_LOCATION. */
1108 pfile
->line_table
->highest_location
--;
1110 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1111 line_table
->seen_line_directive
= true;
1114 /* Arrange the file_change callback. pfile->line has changed to
1115 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1116 header, 2 for a system header that needs to be extern "C" protected,
1117 and zero otherwise. */
1119 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1120 const char *to_file
, linenum_type file_line
,
1123 linemap_assert (reason
!= LC_ENTER_MACRO
);
1124 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1125 to_file
, file_line
);
1126 const line_map_ordinary
*ord_map
= NULL
;
1129 ord_map
= linemap_check_ordinary (map
);
1130 linemap_line_start (pfile
->line_table
,
1131 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
),
1135 if (pfile
->cb
.file_change
)
1136 pfile
->cb
.file_change (pfile
, ord_map
);
1139 /* Report a warning or error detected by the program we are
1140 processing. Use the directive's tokens in the error message. */
1142 do_diagnostic (cpp_reader
*pfile
, int code
, int reason
, int print_dir
)
1144 const unsigned char *dir_name
;
1145 unsigned char *line
;
1146 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1149 dir_name
= pfile
->directive
->name
;
1152 pfile
->state
.prevent_expansion
++;
1153 line
= cpp_output_line_to_string (pfile
, dir_name
);
1154 pfile
->state
.prevent_expansion
--;
1156 if (code
== CPP_DL_WARNING_SYSHDR
&& reason
)
1157 cpp_warning_with_line_syshdr (pfile
, reason
, src_loc
, 0, "%s", line
);
1158 else if (code
== CPP_DL_WARNING
&& reason
)
1159 cpp_warning_with_line (pfile
, reason
, src_loc
, 0, "%s", line
);
1161 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1166 do_error (cpp_reader
*pfile
)
1168 do_diagnostic (pfile
, CPP_DL_ERROR
, 0, 1);
1172 do_warning (cpp_reader
*pfile
)
1174 /* We want #warning diagnostics to be emitted in system headers too. */
1175 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, CPP_W_WARNING_DIRECTIVE
, 1);
1178 /* Report program identification. */
1180 do_ident (cpp_reader
*pfile
)
1182 const cpp_token
*str
= cpp_get_token (pfile
);
1184 if (str
->type
!= CPP_STRING
)
1185 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1186 pfile
->directive
->name
);
1187 else if (pfile
->cb
.ident
)
1188 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1190 check_eol (pfile
, false);
1193 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1194 matching entry, or NULL if none is found. The returned entry could
1195 be the start of a namespace chain, or a pragma. */
1196 static struct pragma_entry
*
1197 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1199 while (chain
&& chain
->pragma
!= pragma
)
1200 chain
= chain
->next
;
1205 /* Create and insert a blank pragma entry at the beginning of a
1206 singly-linked CHAIN. */
1207 static struct pragma_entry
*
1208 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1210 struct pragma_entry
*new_entry
;
1212 new_entry
= (struct pragma_entry
*)
1213 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1215 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1216 new_entry
->next
= *chain
;
1222 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1223 goes in the global namespace. */
1224 static struct pragma_entry
*
1225 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1226 bool allow_name_expansion
)
1228 struct pragma_entry
**chain
= &pfile
->pragmas
;
1229 struct pragma_entry
*entry
;
1230 const cpp_hashnode
*node
;
1234 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1235 entry
= lookup_pragma_entry (*chain
, node
);
1238 entry
= new_pragma_entry (pfile
, chain
);
1239 entry
->pragma
= node
;
1240 entry
->is_nspace
= true;
1241 entry
->allow_expansion
= allow_name_expansion
;
1243 else if (!entry
->is_nspace
)
1245 else if (entry
->allow_expansion
!= allow_name_expansion
)
1247 cpp_error (pfile
, CPP_DL_ICE
,
1248 "registering pragmas in namespace \"%s\" with mismatched "
1249 "name expansion", space
);
1252 chain
= &entry
->u
.space
;
1254 else if (allow_name_expansion
)
1256 cpp_error (pfile
, CPP_DL_ICE
,
1257 "registering pragma \"%s\" with name expansion "
1258 "and no namespace", name
);
1262 /* Check for duplicates. */
1263 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1264 entry
= lookup_pragma_entry (*chain
, node
);
1267 entry
= new_pragma_entry (pfile
, chain
);
1268 entry
->pragma
= node
;
1272 if (entry
->is_nspace
)
1274 cpp_error (pfile
, CPP_DL_ICE
,
1275 "registering \"%s\" as both a pragma and a pragma namespace",
1278 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1281 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1286 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1288 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1289 const char *name
, pragma_cb handler
)
1291 struct pragma_entry
*entry
;
1293 entry
= register_pragma_1 (pfile
, space
, name
, false);
1294 entry
->is_internal
= true;
1295 entry
->u
.handler
= handler
;
1298 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1299 goes in the global namespace. HANDLER is the handler it will call,
1300 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1301 expansion while parsing pragma NAME. This function is exported
1304 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1305 pragma_cb handler
, bool allow_expansion
)
1307 struct pragma_entry
*entry
;
1311 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1315 entry
= register_pragma_1 (pfile
, space
, name
, false);
1318 entry
->allow_expansion
= allow_expansion
;
1319 entry
->u
.handler
= handler
;
1323 /* Similarly, but create mark the pragma for deferred processing.
1324 When found, a CPP_PRAGMA token will be insertted into the stream
1325 with IDENT in the token->u.pragma slot. */
1327 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1328 const char *name
, unsigned int ident
,
1329 bool allow_expansion
, bool allow_name_expansion
)
1331 struct pragma_entry
*entry
;
1333 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1336 entry
->is_deferred
= true;
1337 entry
->allow_expansion
= allow_expansion
;
1338 entry
->u
.ident
= ident
;
1342 /* Register the pragmas the preprocessor itself handles. */
1344 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1346 /* Pragmas in the global namespace. */
1347 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1348 register_pragma_internal (pfile
, 0, "push_macro", do_pragma_push_macro
);
1349 register_pragma_internal (pfile
, 0, "pop_macro", do_pragma_pop_macro
);
1351 /* New GCC-specific pragmas should be put in the GCC namespace. */
1352 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1353 register_pragma_internal (pfile
, "GCC", "system_header",
1354 do_pragma_system_header
);
1355 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1356 register_pragma_internal (pfile
, "GCC", "warning", do_pragma_warning
);
1357 register_pragma_internal (pfile
, "GCC", "error", do_pragma_error
);
1360 /* Return the number of registered pragmas in PE. */
1363 count_registered_pragmas (struct pragma_entry
*pe
)
1366 for (; pe
!= NULL
; pe
= pe
->next
)
1369 ct
+= count_registered_pragmas (pe
->u
.space
);
1375 /* Save into SD the names of the registered pragmas referenced by PE,
1376 and return a pointer to the next free space in SD. */
1379 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1381 for (; pe
!= NULL
; pe
= pe
->next
)
1384 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1385 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1386 HT_LEN (&pe
->pragma
->ident
),
1387 HT_LEN (&pe
->pragma
->ident
) + 1);
1392 /* Return a newly-allocated array which saves the names of the
1393 registered pragmas. */
1396 _cpp_save_pragma_names (cpp_reader
*pfile
)
1398 int ct
= count_registered_pragmas (pfile
->pragmas
);
1399 char **result
= XNEWVEC (char *, ct
);
1400 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1404 /* Restore from SD the names of the registered pragmas referenced by PE,
1405 and return a pointer to the next unused name in SD. */
1408 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1411 for (; pe
!= NULL
; pe
= pe
->next
)
1414 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1415 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1422 /* Restore the names of the registered pragmas from SAVED. */
1425 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1427 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1431 /* Pragmata handling. We handle some, and pass the rest on to the
1432 front end. C99 defines three pragmas and says that no macro
1433 expansion is to be performed on them; whether or not macro
1434 expansion happens for other pragmas is implementation defined.
1435 This implementation allows for a mix of both, since GCC did not
1436 traditionally macro expand its (few) pragmas, whereas OpenMP
1437 specifies that macro expansion should happen. */
1439 do_pragma (cpp_reader
*pfile
)
1441 const struct pragma_entry
*p
= NULL
;
1442 const cpp_token
*token
, *pragma_token
;
1443 source_location pragma_token_virt_loc
= 0;
1445 unsigned int count
= 1;
1447 pfile
->state
.prevent_expansion
++;
1449 pragma_token
= token
= cpp_get_token_with_location (pfile
,
1450 &pragma_token_virt_loc
);
1452 if (token
->type
== CPP_NAME
)
1454 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1455 if (p
&& p
->is_nspace
)
1457 bool allow_name_expansion
= p
->allow_expansion
;
1458 if (allow_name_expansion
)
1459 pfile
->state
.prevent_expansion
--;
1461 token
= cpp_get_token (pfile
);
1462 if (token
->type
== CPP_NAME
)
1463 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1466 if (allow_name_expansion
)
1467 pfile
->state
.prevent_expansion
++;
1476 pfile
->directive_result
.src_loc
= pragma_token_virt_loc
;
1477 pfile
->directive_result
.type
= CPP_PRAGMA
;
1478 pfile
->directive_result
.flags
= pragma_token
->flags
;
1479 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1480 pfile
->state
.in_deferred_pragma
= true;
1481 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1482 if (!p
->allow_expansion
)
1483 pfile
->state
.prevent_expansion
++;
1487 /* Since the handler below doesn't get the line number, that
1488 it might need for diagnostics, make sure it has the right
1489 numbers in place. */
1490 if (pfile
->cb
.line_change
)
1491 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1492 if (p
->allow_expansion
)
1493 pfile
->state
.prevent_expansion
--;
1494 (*p
->u
.handler
) (pfile
);
1495 if (p
->allow_expansion
)
1496 pfile
->state
.prevent_expansion
++;
1499 else if (pfile
->cb
.def_pragma
)
1501 if (count
== 1 || pfile
->context
->prev
== NULL
)
1502 _cpp_backup_tokens (pfile
, count
);
1505 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1506 won't allow backing 2 tokens. */
1507 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1508 reads both tokens, we could perhaps free it, but if it doesn't,
1509 we don't know the exact lifespan. */
1510 cpp_token
*toks
= XNEWVEC (cpp_token
, 2);
1512 toks
[0].flags
|= NO_EXPAND
;
1514 toks
[1].flags
|= NO_EXPAND
;
1515 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1517 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1520 pfile
->state
.prevent_expansion
--;
1523 /* Handle #pragma once. */
1525 do_pragma_once (cpp_reader
*pfile
)
1527 if (cpp_in_primary_file (pfile
))
1528 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1530 check_eol (pfile
, false);
1531 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1534 /* Handle #pragma push_macro(STRING). */
1536 do_pragma_push_macro (cpp_reader
*pfile
)
1540 const uchar
*defn
= NULL
;
1541 char *macroname
, *dest
;
1542 const char *limit
, *src
;
1543 const cpp_token
*txt
;
1544 struct def_pragma_macro
*c
;
1546 txt
= get__Pragma_string (pfile
);
1549 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1550 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1551 "invalid #pragma push_macro directive");
1552 check_eol (pfile
, false);
1553 skip_rest_of_line (pfile
);
1556 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1557 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1558 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1561 /* We know there is a character following the backslash. */
1562 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1567 check_eol (pfile
, false);
1568 skip_rest_of_line (pfile
);
1569 c
= XNEW (struct def_pragma_macro
);
1570 memset (c
, 0, sizeof (struct def_pragma_macro
));
1571 c
->name
= XNEWVAR (char, strlen (macroname
) + 1);
1572 strcpy (c
->name
, macroname
);
1573 c
->next
= pfile
->pushed_macros
;
1574 node
= _cpp_lex_identifier (pfile
, c
->name
);
1575 if (node
->type
== NT_VOID
)
1579 defn
= cpp_macro_definition (pfile
, node
);
1580 defnlen
= ustrlen (defn
);
1581 c
->definition
= XNEWVEC (uchar
, defnlen
+ 2);
1582 c
->definition
[defnlen
] = '\n';
1583 c
->definition
[defnlen
+ 1] = 0;
1584 c
->line
= node
->value
.macro
->line
;
1585 c
->syshdr
= node
->value
.macro
->syshdr
;
1586 c
->used
= node
->value
.macro
->used
;
1587 memcpy (c
->definition
, defn
, defnlen
);
1590 pfile
->pushed_macros
= c
;
1593 /* Handle #pragma pop_macro(STRING). */
1595 do_pragma_pop_macro (cpp_reader
*pfile
)
1597 char *macroname
, *dest
;
1598 const char *limit
, *src
;
1599 const cpp_token
*txt
;
1600 struct def_pragma_macro
*l
= NULL
, *c
= pfile
->pushed_macros
;
1601 txt
= get__Pragma_string (pfile
);
1604 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1605 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1606 "invalid #pragma pop_macro directive");
1607 check_eol (pfile
, false);
1608 skip_rest_of_line (pfile
);
1611 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1612 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1613 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1616 /* We know there is a character following the backslash. */
1617 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1622 check_eol (pfile
, false);
1623 skip_rest_of_line (pfile
);
1627 if (!strcmp (c
->name
, macroname
))
1630 pfile
->pushed_macros
= c
->next
;
1633 cpp_pop_definition (pfile
, c
);
1634 free (c
->definition
);
1644 /* Handle #pragma GCC poison, to poison one or more identifiers so
1645 that the lexer produces a hard error for each subsequent usage. */
1647 do_pragma_poison (cpp_reader
*pfile
)
1649 const cpp_token
*tok
;
1652 pfile
->state
.poisoned_ok
= 1;
1655 tok
= _cpp_lex_token (pfile
);
1656 if (tok
->type
== CPP_EOF
)
1658 if (tok
->type
!= CPP_NAME
)
1660 cpp_error (pfile
, CPP_DL_ERROR
,
1661 "invalid #pragma GCC poison directive");
1665 hp
= tok
->val
.node
.node
;
1666 if (hp
->flags
& NODE_POISONED
)
1669 if (hp
->type
== NT_MACRO
)
1670 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1672 _cpp_free_definition (hp
);
1673 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1675 pfile
->state
.poisoned_ok
= 0;
1678 /* Mark the current header as a system header. This will suppress
1679 some categories of warnings (notably those from -pedantic). It is
1680 intended for use in system libraries that cannot be implemented in
1681 conforming C, but cannot be certain that their headers appear in a
1682 system include directory. To prevent abuse, it is rejected in the
1683 primary source file. */
1685 do_pragma_system_header (cpp_reader
*pfile
)
1687 if (cpp_in_primary_file (pfile
))
1688 cpp_error (pfile
, CPP_DL_WARNING
,
1689 "#pragma system_header ignored outside include file");
1692 check_eol (pfile
, false);
1693 skip_rest_of_line (pfile
);
1694 cpp_make_system_header (pfile
, 1, 0);
1698 /* Check the modified date of the current include file against a specified
1699 file. Issue a diagnostic, if the specified file is newer. We use this to
1700 determine if a fixed header should be refixed. */
1702 do_pragma_dependency (cpp_reader
*pfile
)
1705 int angle_brackets
, ordering
;
1706 source_location location
;
1708 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
1712 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1714 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1715 else if (ordering
> 0)
1717 cpp_error (pfile
, CPP_DL_WARNING
,
1718 "current file is older than %s", fname
);
1719 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1721 _cpp_backup_tokens (pfile
, 1);
1722 do_diagnostic (pfile
, CPP_DL_WARNING
, 0, 0);
1726 free ((void *) fname
);
1729 /* Issue a diagnostic with the message taken from the pragma. If
1730 ERROR is true, the diagnostic is a warning, otherwise, it is an
1733 do_pragma_warning_or_error (cpp_reader
*pfile
, bool error
)
1735 const cpp_token
*tok
= _cpp_lex_token (pfile
);
1737 if (tok
->type
!= CPP_STRING
1738 || !cpp_interpret_string_notranslate (pfile
, &tok
->val
.str
, 1, &str
,
1742 cpp_error (pfile
, CPP_DL_ERROR
, "invalid \"#pragma GCC %s\" directive",
1743 error
? "error" : "warning");
1746 cpp_error (pfile
, error
? CPP_DL_ERROR
: CPP_DL_WARNING
,
1748 free ((void *)str
.text
);
1751 /* Issue a warning diagnostic. */
1753 do_pragma_warning (cpp_reader
*pfile
)
1755 do_pragma_warning_or_error (pfile
, false);
1758 /* Issue an error diagnostic. */
1760 do_pragma_error (cpp_reader
*pfile
)
1762 do_pragma_warning_or_error (pfile
, true);
1765 /* Get a token but skip padding. */
1766 static const cpp_token
*
1767 get_token_no_padding (cpp_reader
*pfile
)
1771 const cpp_token
*result
= cpp_get_token (pfile
);
1772 if (result
->type
!= CPP_PADDING
)
1777 /* Check syntax is "(string-literal)". Returns the string on success,
1778 or NULL on failure. */
1779 static const cpp_token
*
1780 get__Pragma_string (cpp_reader
*pfile
)
1782 const cpp_token
*string
;
1783 const cpp_token
*paren
;
1785 paren
= get_token_no_padding (pfile
);
1786 if (paren
->type
== CPP_EOF
)
1787 _cpp_backup_tokens (pfile
, 1);
1788 if (paren
->type
!= CPP_OPEN_PAREN
)
1791 string
= get_token_no_padding (pfile
);
1792 if (string
->type
== CPP_EOF
)
1793 _cpp_backup_tokens (pfile
, 1);
1794 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1795 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
1796 && string
->type
!= CPP_UTF8STRING
)
1799 paren
= get_token_no_padding (pfile
);
1800 if (paren
->type
== CPP_EOF
)
1801 _cpp_backup_tokens (pfile
, 1);
1802 if (paren
->type
!= CPP_CLOSE_PAREN
)
1808 /* Destringize IN into a temporary buffer, by removing the first \ of
1809 \" and \\ sequences, and process the result as a #pragma directive. */
1811 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
,
1812 source_location expansion_loc
)
1814 const unsigned char *src
, *limit
;
1815 char *dest
, *result
;
1816 cpp_context
*saved_context
;
1817 cpp_token
*saved_cur_token
;
1818 tokenrun
*saved_cur_run
;
1821 const struct directive
*save_directive
;
1823 dest
= result
= (char *) alloca (in
->len
- 1);
1824 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1825 limit
= in
->text
+ in
->len
- 1;
1828 /* We know there is a character following the backslash. */
1829 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1835 /* Ugh; an awful kludge. We are really not set up to be lexing
1836 tokens when in the middle of a macro expansion. Use a new
1837 context to force cpp_get_token to lex, and so skip_rest_of_line
1838 doesn't go beyond the end of the text. Also, remember the
1839 current lexing position so we can return to it later.
1841 Something like line-at-a-time lexing should remove the need for
1843 saved_context
= pfile
->context
;
1844 saved_cur_token
= pfile
->cur_token
;
1845 saved_cur_run
= pfile
->cur_run
;
1847 pfile
->context
= XCNEW (cpp_context
);
1849 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1850 until we've read all of the tokens that we want. */
1851 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1852 /* from_stage3 */ true);
1853 /* ??? Antique Disgusting Hack. What does this do? */
1854 if (pfile
->buffer
->prev
)
1855 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1857 start_directive (pfile
);
1858 _cpp_clean_line (pfile
);
1859 save_directive
= pfile
->directive
;
1860 pfile
->directive
= &dtable
[T_PRAGMA
];
1862 end_directive (pfile
, 1);
1863 pfile
->directive
= save_directive
;
1865 /* We always insert at least one token, the directive result. It'll
1866 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1867 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1869 /* If we're not handling the pragma internally, read all of the tokens from
1870 the string buffer now, while the string buffer is still installed. */
1871 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1872 to me what the true lifespan of the tokens are. It would appear that
1873 the lifespan is the entire parse of the main input stream, in which case
1874 this may not be wrong. */
1875 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1881 toks
= XNEWVEC (cpp_token
, maxcount
);
1882 toks
[0] = pfile
->directive_result
;
1886 if (count
== maxcount
)
1888 maxcount
= maxcount
* 3 / 2;
1889 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1891 toks
[count
] = *cpp_get_token (pfile
);
1892 /* _Pragma is a builtin, so we're not within a macro-map, and so
1893 the token locations are set to bogus ordinary locations
1894 near to, but after that of the "_Pragma".
1895 Paper over this by setting them equal to the location of the
1896 _Pragma itself (PR preprocessor/69126). */
1897 toks
[count
].src_loc
= expansion_loc
;
1898 /* Macros have been already expanded by cpp_get_token
1899 if the pragma allowed expansion. */
1900 toks
[count
++].flags
|= NO_EXPAND
;
1902 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1907 toks
= XNEW (cpp_token
);
1908 toks
[0] = pfile
->directive_result
;
1910 /* If we handled the entire pragma internally, make sure we get the
1911 line number correct for the next token. */
1912 if (pfile
->cb
.line_change
)
1913 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1916 /* Finish inlining run_directive. */
1917 pfile
->buffer
->file
= NULL
;
1918 _cpp_pop_buffer (pfile
);
1920 /* Reset the old macro state before ... */
1921 XDELETE (pfile
->context
);
1922 pfile
->context
= saved_context
;
1923 pfile
->cur_token
= saved_cur_token
;
1924 pfile
->cur_run
= saved_cur_run
;
1926 /* ... inserting the new tokens we collected. */
1927 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1930 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1932 _cpp_do__Pragma (cpp_reader
*pfile
, source_location expansion_loc
)
1934 const cpp_token
*string
= get__Pragma_string (pfile
);
1935 pfile
->directive_result
.type
= CPP_PADDING
;
1939 destringize_and_run (pfile
, &string
->val
.str
, expansion_loc
);
1942 cpp_error (pfile
, CPP_DL_ERROR
,
1943 "_Pragma takes a parenthesized string literal");
1947 /* Handle #ifdef. */
1949 do_ifdef (cpp_reader
*pfile
)
1953 if (! pfile
->state
.skipping
)
1955 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
1959 /* Do not treat conditional macros as being defined. This is due to
1960 the powerpc and spu ports using conditional macros for 'vector',
1961 'bool', and 'pixel' to act as conditional keywords. This messes
1962 up tests like #ifndef bool. */
1963 skip
= (node
->type
!= NT_MACRO
1964 || ((node
->flags
& NODE_CONDITIONAL
) != 0));
1965 _cpp_mark_macro_used (node
);
1966 if (!(node
->flags
& NODE_USED
))
1968 node
->flags
|= NODE_USED
;
1969 if (node
->type
== NT_MACRO
)
1971 if ((node
->flags
& NODE_BUILTIN
)
1972 && pfile
->cb
.user_builtin_macro
)
1973 pfile
->cb
.user_builtin_macro (pfile
, node
);
1974 if (pfile
->cb
.used_define
)
1975 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1979 if (pfile
->cb
.used_undef
)
1980 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1984 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1985 check_eol (pfile
, false);
1989 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1992 /* Handle #ifndef. */
1994 do_ifndef (cpp_reader
*pfile
)
1997 cpp_hashnode
*node
= 0;
1999 if (! pfile
->state
.skipping
)
2001 node
= lex_macro_node (pfile
, false);
2005 /* Do not treat conditional macros as being defined. This is due to
2006 the powerpc and spu ports using conditional macros for 'vector',
2007 'bool', and 'pixel' to act as conditional keywords. This messes
2008 up tests like #ifndef bool. */
2009 skip
= (node
->type
== NT_MACRO
2010 && ((node
->flags
& NODE_CONDITIONAL
) == 0));
2011 _cpp_mark_macro_used (node
);
2012 if (!(node
->flags
& NODE_USED
))
2014 node
->flags
|= NODE_USED
;
2015 if (node
->type
== NT_MACRO
)
2017 if ((node
->flags
& NODE_BUILTIN
)
2018 && pfile
->cb
.user_builtin_macro
)
2019 pfile
->cb
.user_builtin_macro (pfile
, node
);
2020 if (pfile
->cb
.used_define
)
2021 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
2025 if (pfile
->cb
.used_undef
)
2026 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
2030 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
2031 check_eol (pfile
, false);
2035 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
2038 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
2039 pfile->mi_ind_cmacro so we can handle multiple-include
2040 optimizations. If macro expansion occurs in the expression, we
2041 cannot treat it as a controlling conditional, since the expansion
2042 could change in the future. That is handled by cpp_get_token. */
2044 do_if (cpp_reader
*pfile
)
2048 if (! pfile
->state
.skipping
)
2049 skip
= _cpp_parse_expr (pfile
, true) == false;
2051 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
2054 /* Flip skipping state if appropriate and continue without changing
2055 if_stack; this is so that the error message for missing #endif's
2056 etc. will point to the original #if. */
2058 do_else (cpp_reader
*pfile
)
2060 cpp_buffer
*buffer
= pfile
->buffer
;
2061 struct if_stack
*ifs
= buffer
->if_stack
;
2064 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
2067 if (ifs
->type
== T_ELSE
)
2069 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
2070 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2071 "the conditional began here");
2075 /* Skip any future (erroneous) #elses or #elifs. */
2076 pfile
->state
.skipping
= ifs
->skip_elses
;
2077 ifs
->skip_elses
= true;
2079 /* Invalidate any controlling macro. */
2082 /* Only check EOL if was not originally skipping. */
2083 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2084 check_eol_endif_labels (pfile
);
2088 /* Handle a #elif directive by not changing if_stack either. See the
2089 comment above do_else. */
2091 do_elif (cpp_reader
*pfile
)
2093 cpp_buffer
*buffer
= pfile
->buffer
;
2094 struct if_stack
*ifs
= buffer
->if_stack
;
2097 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
2100 if (ifs
->type
== T_ELSE
)
2102 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
2103 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2104 "the conditional began here");
2108 /* See DR#412: "Only the first group whose control condition
2109 evaluates to true (nonzero) is processed; any following groups
2110 are skipped and their controlling directives are processed as
2111 if they were in a group that is skipped." */
2112 if (ifs
->skip_elses
)
2113 pfile
->state
.skipping
= 1;
2116 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
, false);
2117 ifs
->skip_elses
= ! pfile
->state
.skipping
;
2120 /* Invalidate any controlling macro. */
2125 /* #endif pops the if stack and resets pfile->state.skipping. */
2127 do_endif (cpp_reader
*pfile
)
2129 cpp_buffer
*buffer
= pfile
->buffer
;
2130 struct if_stack
*ifs
= buffer
->if_stack
;
2133 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
2136 /* Only check EOL if was not originally skipping. */
2137 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2138 check_eol_endif_labels (pfile
);
2140 /* If potential control macro, we go back outside again. */
2141 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
2143 pfile
->mi_valid
= true;
2144 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
2147 buffer
->if_stack
= ifs
->next
;
2148 pfile
->state
.skipping
= ifs
->was_skipping
;
2149 obstack_free (&pfile
->buffer_ob
, ifs
);
2153 /* Push an if_stack entry for a preprocessor conditional, and set
2154 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2155 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2156 we need to check here that we are at the top of the file. */
2158 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
2159 const cpp_hashnode
*cmacro
)
2161 struct if_stack
*ifs
;
2162 cpp_buffer
*buffer
= pfile
->buffer
;
2164 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
2165 ifs
->line
= pfile
->directive_line
;
2166 ifs
->next
= buffer
->if_stack
;
2167 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
2168 ifs
->was_skipping
= pfile
->state
.skipping
;
2170 /* This condition is effectively a test for top-of-file. */
2171 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
2172 ifs
->mi_cmacro
= cmacro
;
2176 pfile
->state
.skipping
= skip
;
2177 buffer
->if_stack
= ifs
;
2180 /* Read the tokens of the answer into the macro pool, in a directive
2181 of type TYPE. Only commit the memory if we intend it as permanent
2182 storage, i.e. the #assert case. Returns 0 on success, and sets
2183 ANSWERP to point to the answer. PRED_LOC is the location of the
2186 parse_answer (cpp_reader
*pfile
, struct answer
**answerp
, int type
,
2187 source_location pred_loc
)
2189 const cpp_token
*paren
;
2190 struct answer
*answer
;
2191 unsigned int acount
;
2193 /* In a conditional, it is legal to not have an open paren. We
2194 should save the following token in this case. */
2195 paren
= cpp_get_token (pfile
);
2197 /* If not a paren, see if we're OK. */
2198 if (paren
->type
!= CPP_OPEN_PAREN
)
2200 /* In a conditional no answer is a test for any answer. It
2201 could be followed by any token. */
2204 _cpp_backup_tokens (pfile
, 1);
2208 /* #unassert with no answer is valid - it removes all answers. */
2209 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
2212 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
2213 "missing '(' after predicate");
2217 for (acount
= 0;; acount
++)
2220 const cpp_token
*token
= cpp_get_token (pfile
);
2223 if (token
->type
== CPP_CLOSE_PAREN
)
2226 if (token
->type
== CPP_EOF
)
2228 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
2232 /* struct answer includes the space for one token. */
2233 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
2235 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
2236 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
2238 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
2241 /* Drop whitespace at start, for answer equivalence purposes. */
2243 dest
->flags
&= ~PREV_WHITE
;
2248 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
2252 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
2253 answer
->count
= acount
;
2254 answer
->next
= NULL
;
2260 /* Parses an assertion directive of type TYPE, returning a pointer to
2261 the hash node of the predicate, or 0 on error. If an answer was
2262 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
2263 static cpp_hashnode
*
2264 parse_assertion (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
2266 cpp_hashnode
*result
= 0;
2267 const cpp_token
*predicate
;
2269 /* We don't expand predicates or answers. */
2270 pfile
->state
.prevent_expansion
++;
2273 predicate
= cpp_get_token (pfile
);
2274 if (predicate
->type
== CPP_EOF
)
2275 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2276 else if (predicate
->type
!= CPP_NAME
)
2277 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2278 "predicate must be an identifier");
2279 else if (parse_answer (pfile
, answerp
, type
, predicate
->src_loc
) == 0)
2281 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2282 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2284 /* Prefix '#' to get it out of macro namespace. */
2286 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2287 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2290 pfile
->state
.prevent_expansion
--;
2294 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2295 or a pointer to NULL if the answer is not in the chain. */
2296 static struct answer
**
2297 find_answer (cpp_hashnode
*node
, const struct answer
*candidate
)
2300 struct answer
**result
;
2302 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
2304 struct answer
*answer
= *result
;
2306 if (answer
->count
== candidate
->count
)
2308 for (i
= 0; i
< answer
->count
; i
++)
2309 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
2312 if (i
== answer
->count
)
2320 /* Test an assertion within a preprocessor conditional. Returns
2321 nonzero on failure, zero on success. On success, the result of
2322 the test is written into VALUE, otherwise the value 0. */
2324 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2326 struct answer
*answer
;
2329 node
= parse_assertion (pfile
, &answer
, T_IF
);
2331 /* For recovery, an erroneous assertion expression is handled as a
2332 failing assertion. */
2336 *value
= (node
->type
== NT_ASSERTION
&&
2337 (answer
== 0 || *find_answer (node
, answer
) != 0));
2338 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2339 _cpp_backup_tokens (pfile
, 1);
2341 /* We don't commit the memory for the answer - it's temporary only. */
2345 /* Handle #assert. */
2347 do_assert (cpp_reader
*pfile
)
2349 struct answer
*new_answer
;
2352 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
2357 /* Place the new answer in the answer list. First check there
2358 is not a duplicate. */
2359 new_answer
->next
= 0;
2360 if (node
->type
== NT_ASSERTION
)
2362 if (*find_answer (node
, new_answer
))
2364 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2365 NODE_NAME (node
) + 1);
2368 new_answer
->next
= node
->value
.answers
;
2371 answer_size
= sizeof (struct answer
) + ((new_answer
->count
- 1)
2372 * sizeof (cpp_token
));
2373 /* Commit or allocate storage for the object. */
2374 if (pfile
->hash_table
->alloc_subobject
)
2376 struct answer
*temp_answer
= new_answer
;
2377 new_answer
= (struct answer
*) pfile
->hash_table
->alloc_subobject
2379 memcpy (new_answer
, temp_answer
, answer_size
);
2382 BUFF_FRONT (pfile
->a_buff
) += answer_size
;
2384 node
->type
= NT_ASSERTION
;
2385 node
->value
.answers
= new_answer
;
2386 check_eol (pfile
, false);
2390 /* Handle #unassert. */
2392 do_unassert (cpp_reader
*pfile
)
2395 struct answer
*answer
;
2397 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
2398 /* It isn't an error to #unassert something that isn't asserted. */
2399 if (node
&& node
->type
== NT_ASSERTION
)
2403 struct answer
**p
= find_answer (node
, answer
), *temp
;
2405 /* Remove the answer from the list. */
2410 /* Did we free the last answer? */
2411 if (node
->value
.answers
== 0)
2412 node
->type
= NT_VOID
;
2414 check_eol (pfile
, false);
2417 _cpp_free_definition (node
);
2420 /* We don't commit the memory for the answer - it's temporary only. */
2423 /* These are for -D, -U, -A. */
2425 /* Process the string STR as if it appeared as the body of a #define.
2426 If STR is just an identifier, define it with value 1.
2427 If STR has anything after the identifier, then it should
2428 be identifier=definition. */
2430 cpp_define (cpp_reader
*pfile
, const char *str
)
2436 /* Copy the entire option so we can modify it.
2437 Change the first "=" in the string to a space. If there is none,
2438 tack " 1" on the end. */
2440 count
= strlen (str
);
2441 buf
= (char *) alloca (count
+ 3);
2442 memcpy (buf
, str
, count
);
2444 p
= strchr (str
, '=');
2454 run_directive (pfile
, T_DEFINE
, buf
, count
);
2458 /* Use to build macros to be run through cpp_define() as
2460 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2463 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2469 ptr
= xvasprintf (fmt
, ap
);
2472 cpp_define (pfile
, ptr
);
2477 /* Slight variant of the above for use by initialize_builtins. */
2479 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2481 size_t len
= strlen (str
);
2482 char *buf
= (char *) alloca (len
+ 1);
2483 memcpy (buf
, str
, len
);
2485 run_directive (pfile
, T_DEFINE
, buf
, len
);
2488 /* Process MACRO as if it appeared as the body of an #undef. */
2490 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2492 size_t len
= strlen (macro
);
2493 char *buf
= (char *) alloca (len
+ 1);
2494 memcpy (buf
, macro
, len
);
2496 run_directive (pfile
, T_UNDEF
, buf
, len
);
2499 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2500 or first element is zero, then the macro should be undefined. */
2502 cpp_pop_definition (cpp_reader
*pfile
, struct def_pragma_macro
*c
)
2504 cpp_hashnode
*node
= _cpp_lex_identifier (pfile
, c
->name
);
2508 if (pfile
->cb
.before_define
)
2509 pfile
->cb
.before_define (pfile
);
2511 if (node
->type
== NT_MACRO
)
2513 if (pfile
->cb
.undef
)
2514 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2515 if (CPP_OPTION (pfile
, warn_unused_macros
))
2516 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2518 if (node
->type
!= NT_VOID
)
2519 _cpp_free_definition (node
);
2526 cpp_hashnode
*h
= NULL
;
2529 namelen
= ustrcspn (c
->definition
, "( \n");
2530 h
= cpp_lookup (pfile
, c
->definition
, namelen
);
2531 dn
= c
->definition
+ namelen
;
2534 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
|NODE_USED
);
2535 nbuf
= cpp_push_buffer (pfile
, dn
, ustrchr (dn
, '\n') - dn
, true);
2538 _cpp_clean_line (pfile
);
2540 if (!_cpp_create_definition (pfile
, h
))
2542 _cpp_pop_buffer (pfile
);
2546 h
->value
.macro
->line
= c
->line
;
2547 h
->value
.macro
->syshdr
= c
->syshdr
;
2548 h
->value
.macro
->used
= c
->used
;
2552 /* Process the string STR as if it appeared as the body of a #assert. */
2554 cpp_assert (cpp_reader
*pfile
, const char *str
)
2556 handle_assertion (pfile
, str
, T_ASSERT
);
2559 /* Process STR as if it appeared as the body of an #unassert. */
2561 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2563 handle_assertion (pfile
, str
, T_UNASSERT
);
2566 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2568 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2570 size_t count
= strlen (str
);
2571 const char *p
= strchr (str
, '=');
2573 /* Copy the entire option so we can modify it. Change the first
2574 "=" in the string to a '(', and tack a ')' on the end. */
2575 char *buf
= (char *) alloca (count
+ 2);
2577 memcpy (buf
, str
, count
);
2586 run_directive (pfile
, type
, str
, count
);
2589 /* The options structure. */
2591 cpp_get_options (cpp_reader
*pfile
)
2593 return &pfile
->opts
;
2596 /* The callbacks structure. */
2598 cpp_get_callbacks (cpp_reader
*pfile
)
2603 /* Copy the given callbacks structure to our own. */
2605 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2610 /* The dependencies structure. (Creates one if it hasn't already been.) */
2612 cpp_get_deps (cpp_reader
*pfile
)
2615 pfile
->deps
= deps_init ();
2619 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2620 doesn't fail. It does not generate a file change call back; that
2621 is the responsibility of the caller. */
2623 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2626 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2628 /* Clears, amongst other things, if_stack and mi_cmacro. */
2629 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2631 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2632 new_buffer
->rlimit
= buffer
+ len
;
2633 new_buffer
->from_stage3
= from_stage3
;
2634 new_buffer
->prev
= pfile
->buffer
;
2635 new_buffer
->need_line
= true;
2637 pfile
->buffer
= new_buffer
;
2642 /* Pops a single buffer, with a file change call-back if appropriate.
2643 Then pushes the next -include file, if any remain. */
2645 _cpp_pop_buffer (cpp_reader
*pfile
)
2647 cpp_buffer
*buffer
= pfile
->buffer
;
2648 struct _cpp_file
*inc
= buffer
->file
;
2649 struct if_stack
*ifs
;
2650 const unsigned char *to_free
;
2652 /* Walk back up the conditional stack till we reach its level at
2653 entry to this file, issuing error messages. */
2654 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2655 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2656 "unterminated #%s", dtable
[ifs
->type
].name
);
2658 /* In case of a missing #endif. */
2659 pfile
->state
.skipping
= 0;
2661 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2662 pfile
->buffer
= buffer
->prev
;
2664 to_free
= buffer
->to_free
;
2665 free (buffer
->notes
);
2667 /* Free the buffer object now; we may want to push a new buffer
2668 in _cpp_push_next_include_file. */
2669 obstack_free (&pfile
->buffer_ob
, buffer
);
2673 _cpp_pop_file_buffer (pfile
, inc
, to_free
);
2675 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2679 /* Enter all recognized directives in the hash table. */
2681 _cpp_init_directives (cpp_reader
*pfile
)
2686 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2688 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2689 node
->is_directive
= 1;
2690 node
->directive_index
= i
;
2694 /* Extract header file from a bracket include. Parsing starts after '<'.
2695 The string is malloced and must be freed by the caller. */
2697 _cpp_bracket_include(cpp_reader
*pfile
)
2699 return glue_header_name (pfile
);