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 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. */
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
*, 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
*,
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
*,
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
*);
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. */
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? */
161 /* #sccs is synonymous with #ident. */
162 #define do_sccs do_ident
164 /* Use the table to generate a series of prototypes, an enum for the
165 directive names, and an array of directive handlers. */
167 #define D(name, t, o, f) static void do_##name (cpp_reader *);
171 #define D(n, tag, o, f) tag,
179 #define D(name, t, origin, flags) \
180 { do_##name, (const uchar *) #name, \
181 sizeof #name - 1, origin, flags },
182 static const directive dtable
[] =
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
[] = {
197 #undef DIRECTIVE_TABLE
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
=
204 do_linemarker
, UC
"#", 1, KANDR
, IN_I
207 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
209 /* Skip any remaining tokens in a directive. */
211 skip_rest_of_line (cpp_reader
*pfile
)
213 /* Discard all stacked contexts. */
214 while (pfile
->context
->prev
)
215 _cpp_pop_context (pfile
);
217 /* Sweep up all tokens remaining on the line. */
219 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
223 /* Helper function for check_oel. */
226 check_eol_1 (cpp_reader
*pfile
, bool expand
, enum cpp_warning_reason reason
)
228 if (! SEEN_EOL () && (expand
229 ? cpp_get_token (pfile
)
230 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
231 cpp_pedwarning (pfile
, reason
, "extra tokens at end of #%s directive",
232 pfile
->directive
->name
);
235 /* Variant of check_eol used for Wendif-labels warnings. */
238 check_eol_endif_labels (cpp_reader
*pfile
)
240 check_eol_1 (pfile
, false, CPP_W_ENDIF_LABELS
);
243 /* Ensure there are no stray tokens at the end of a directive. If
244 EXPAND is true, tokens macro-expanding to nothing are allowed. */
247 check_eol (cpp_reader
*pfile
, bool expand
)
249 check_eol_1 (pfile
, expand
, CPP_W_NONE
);
252 /* Ensure there are no stray tokens other than comments at the end of
253 a directive, and gather the comments. */
254 static const cpp_token
**
255 check_eol_return_comments (cpp_reader
*pfile
)
259 const cpp_token
**buf
;
261 buf
= XNEWVEC (const cpp_token
*, capacity
);
267 const cpp_token
*tok
;
269 tok
= _cpp_lex_token (pfile
);
270 if (tok
->type
== CPP_EOF
)
272 if (tok
->type
!= CPP_COMMENT
)
273 cpp_error (pfile
, CPP_DL_PEDWARN
,
274 "extra tokens at end of #%s directive",
275 pfile
->directive
->name
);
278 if (c
+ 1 >= capacity
)
281 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
292 /* Called when entering a directive, _Pragma or command-line directive. */
294 start_directive (cpp_reader
*pfile
)
296 /* Setup in-directive state. */
297 pfile
->state
.in_directive
= 1;
298 pfile
->state
.save_comments
= 0;
299 pfile
->directive_result
.type
= CPP_PADDING
;
301 /* Some handlers need the position of the # for diagnostics. */
302 pfile
->directive_line
= pfile
->line_table
->highest_line
;
305 /* Called when leaving a directive, _Pragma or command-line directive. */
307 end_directive (cpp_reader
*pfile
, int skip_line
)
309 if (CPP_OPTION (pfile
, traditional
))
311 /* Revert change of prepare_directive_trad. */
312 if (!pfile
->state
.in_deferred_pragma
)
313 pfile
->state
.prevent_expansion
--;
315 if (pfile
->directive
!= &dtable
[T_DEFINE
])
316 _cpp_remove_overlay (pfile
);
318 else if (pfile
->state
.in_deferred_pragma
)
320 /* We don't skip for an assembler #. */
323 skip_rest_of_line (pfile
);
324 if (!pfile
->keep_tokens
)
326 pfile
->cur_run
= &pfile
->base_run
;
327 pfile
->cur_token
= pfile
->base_run
.base
;
332 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
333 pfile
->state
.in_directive
= 0;
334 pfile
->state
.in_expression
= 0;
335 pfile
->state
.angled_headers
= 0;
336 pfile
->directive
= 0;
339 /* Prepare to handle the directive in pfile->directive. */
341 prepare_directive_trad (cpp_reader
*pfile
)
343 if (pfile
->directive
!= &dtable
[T_DEFINE
])
345 bool no_expand
= (pfile
->directive
346 && ! (pfile
->directive
->flags
& EXPAND
));
347 bool was_skipping
= pfile
->state
.skipping
;
349 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
350 || pfile
->directive
== &dtable
[T_ELIF
]);
351 if (pfile
->state
.in_expression
)
352 pfile
->state
.skipping
= false;
355 pfile
->state
.prevent_expansion
++;
356 _cpp_scan_out_logical_line (pfile
, NULL
, false);
358 pfile
->state
.prevent_expansion
--;
360 pfile
->state
.skipping
= was_skipping
;
361 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
362 pfile
->out
.cur
- pfile
->out
.base
);
365 /* Stop ISO C from expanding anything. */
366 pfile
->state
.prevent_expansion
++;
369 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
370 the '#' was indented. */
372 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
374 /* Issue -pedantic or deprecated warnings for extensions. We let
375 -pedantic take precedence if both are applicable. */
376 if (! pfile
->state
.skipping
)
378 if (dir
->origin
== EXTENSION
379 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
380 && CPP_PEDANTIC (pfile
))
381 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
382 else if (((dir
->flags
& DEPRECATED
) != 0
383 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
384 && CPP_OPTION (pfile
, cpp_warn_deprecated
))
385 cpp_warning (pfile
, CPP_W_DEPRECATED
,
386 "#%s is a deprecated GCC extension", dir
->name
);
389 /* Traditionally, a directive is ignored unless its # is in
390 column 1. Therefore in code intended to work with K+R
391 compilers, directives added by C89 must have their #
392 indented, and directives present in traditional C must not.
393 This is true even of directives in skipped conditional
394 blocks. #elif cannot be used at all. */
395 if (CPP_WTRADITIONAL (pfile
))
397 if (dir
== &dtable
[T_ELIF
])
398 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
399 "suggest not using #elif in traditional C");
400 else if (indented
&& dir
->origin
== KANDR
)
401 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
402 "traditional C ignores #%s with the # indented",
404 else if (!indented
&& dir
->origin
!= KANDR
)
405 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
406 "suggest hiding #%s from traditional C with an indented #",
411 /* Check if we have a known directive. INDENTED is nonzero if the
412 '#' of the directive was indented. This function is in this file
413 to save unnecessarily exporting dtable etc. to lex.c. Returns
414 nonzero if the line of tokens has been handled, zero if we should
415 continue processing the line. */
417 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
419 const directive
*dir
= 0;
420 const cpp_token
*dname
;
421 bool was_parsing_args
= pfile
->state
.parsing_args
;
422 bool was_discarding_output
= pfile
->state
.discarding_output
;
425 if (was_discarding_output
)
426 pfile
->state
.prevent_expansion
= 0;
428 if (was_parsing_args
)
430 if (CPP_OPTION (pfile
, cpp_pedantic
))
431 cpp_error (pfile
, CPP_DL_PEDWARN
,
432 "embedding a directive within macro arguments is not portable");
433 pfile
->state
.parsing_args
= 0;
434 pfile
->state
.prevent_expansion
= 0;
436 start_directive (pfile
);
437 dname
= _cpp_lex_token (pfile
);
439 if (dname
->type
== CPP_NAME
)
441 if (dname
->val
.node
.node
->is_directive
)
442 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
444 /* We do not recognize the # followed by a number extension in
446 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
448 dir
= &linemarker_dir
;
449 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
450 && ! pfile
->state
.skipping
)
451 cpp_error (pfile
, CPP_DL_PEDWARN
,
452 "style of line directive is a GCC extension");
457 /* If we have a directive that is not an opening conditional,
458 invalidate any control macro. */
459 if (! (dir
->flags
& IF_COND
))
460 pfile
->mi_valid
= false;
462 /* Kluge alert. In order to be sure that code like this
467 does not cause '#define foo bar' to get executed when
468 compiled with -save-temps, we recognize directives in
469 -fpreprocessed mode only if the # is in column 1. macro.c
470 puts a space in front of any '#' at the start of a macro.
472 We exclude the -fdirectives-only case because macro expansion
473 has not been performed yet, and block comments can cause spaces
474 to precede the directive. */
475 if (CPP_OPTION (pfile
, preprocessed
)
476 && !CPP_OPTION (pfile
, directives_only
)
477 && (indented
|| !(dir
->flags
& IN_I
)))
484 /* In failed conditional groups, all non-conditional
485 directives are ignored. Before doing that, whether
486 skipping or not, we should lex angle-bracketed headers
487 correctly, and maybe output some diagnostics. */
488 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
489 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
490 if (! CPP_OPTION (pfile
, preprocessed
))
491 directive_diagnostics (pfile
, dir
, indented
);
492 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
496 else if (dname
->type
== CPP_EOF
)
497 ; /* CPP_EOF is the "null directive". */
500 /* An unknown directive. Don't complain about it in assembly
501 source: we don't know where the comments are, and # may
502 introduce assembler pseudo-ops. Don't complain about invalid
503 directives in skipped conditional groups (6.10 p4). */
504 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
506 else if (!pfile
->state
.skipping
)
508 const char *unrecognized
509 = (const char *)cpp_token_as_text (pfile
, dname
);
510 const char *hint
= NULL
;
512 /* Call back into gcc to get a spelling suggestion. Ideally
513 we'd just use best_match from gcc/spellcheck.h (and filter
514 out the uncommon directives), but that requires moving it
515 to a support library. */
516 if (pfile
->cb
.get_suggestion
)
517 hint
= pfile
->cb
.get_suggestion (pfile
, unrecognized
,
522 rich_location
richloc (pfile
->line_table
, dname
->src_loc
);
523 source_range misspelled_token_range
524 = get_range_from_loc (pfile
->line_table
, dname
->src_loc
);
525 richloc
.add_fixit_replace (misspelled_token_range
, hint
);
526 cpp_error_at (pfile
, CPP_DL_ERROR
, &richloc
,
527 "invalid preprocessing directive #%s;"
528 " did you mean #%s?",
532 cpp_error (pfile
, CPP_DL_ERROR
,
533 "invalid preprocessing directive #%s",
538 pfile
->directive
= dir
;
539 if (CPP_OPTION (pfile
, traditional
))
540 prepare_directive_trad (pfile
);
543 pfile
->directive
->handler (pfile
);
545 _cpp_backup_tokens (pfile
, 1);
547 end_directive (pfile
, skip
);
548 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
550 /* Restore state when within macro args. */
551 pfile
->state
.parsing_args
= 2;
552 pfile
->state
.prevent_expansion
= 1;
554 if (was_discarding_output
)
555 pfile
->state
.prevent_expansion
= 1;
559 /* Directive handler wrapper used by the command line option
560 processor. BUF is \n terminated. */
562 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
564 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
565 /* from_stage3 */ true);
566 start_directive (pfile
);
568 /* This is a short-term fix to prevent a leading '#' being
569 interpreted as a directive. */
570 _cpp_clean_line (pfile
);
572 pfile
->directive
= &dtable
[dir_no
];
573 if (CPP_OPTION (pfile
, traditional
))
574 prepare_directive_trad (pfile
);
575 pfile
->directive
->handler (pfile
);
576 end_directive (pfile
, 1);
577 _cpp_pop_buffer (pfile
);
580 /* Checks for validity the macro name in #define, #undef, #ifdef and
581 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
582 processing a #define or #undefine directive, and false
584 static cpp_hashnode
*
585 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
587 const cpp_token
*token
= _cpp_lex_token (pfile
);
589 /* The token immediately after #define must be an identifier. That
590 identifier may not be "defined", per C99 6.10.8p4.
591 In C++, it may not be any of the "named operators" either,
592 per C++98 [lex.digraph], [lex.key].
593 Finally, the identifier may not have been poisoned. (In that case
594 the lexer has issued the error message for us.) */
596 if (token
->type
== CPP_NAME
)
598 cpp_hashnode
*node
= token
->val
.node
.node
;
600 if (is_def_or_undef
&& node
== pfile
->spec_nodes
.n_defined
)
601 cpp_error (pfile
, CPP_DL_ERROR
,
602 "\"defined\" cannot be used as a macro name");
603 else if (is_def_or_undef
604 && (node
== pfile
->spec_nodes
.n__has_include__
605 || node
== pfile
->spec_nodes
.n__has_include_next__
))
606 cpp_error (pfile
, CPP_DL_ERROR
,
607 "\"__has_include__\" cannot be used as a macro name");
608 else if (! (node
->flags
& NODE_POISONED
))
611 else if (token
->flags
& NAMED_OP
)
612 cpp_error (pfile
, CPP_DL_ERROR
,
613 "\"%s\" cannot be used as a macro name as it is an operator in C++",
614 NODE_NAME (token
->val
.node
.node
));
615 else if (token
->type
== CPP_EOF
)
616 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
617 pfile
->directive
->name
);
619 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
624 /* Process a #define directive. Most work is done in macro.c. */
626 do_define (cpp_reader
*pfile
)
628 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
632 /* If we have been requested to expand comments into macros,
633 then re-enable saving of comments. */
634 pfile
->state
.save_comments
=
635 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
637 if (pfile
->cb
.before_define
)
638 pfile
->cb
.before_define (pfile
);
640 if (_cpp_create_definition (pfile
, node
))
641 if (pfile
->cb
.define
)
642 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
644 node
->flags
&= ~NODE_USED
;
648 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
650 do_undef (cpp_reader
*pfile
)
652 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
656 if (pfile
->cb
.before_define
)
657 pfile
->cb
.before_define (pfile
);
660 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
662 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
663 identifier is not currently defined as a macro name. */
664 if (cpp_macro_p (node
))
666 if (node
->flags
& NODE_WARN
)
667 cpp_error (pfile
, CPP_DL_WARNING
,
668 "undefining \"%s\"", NODE_NAME (node
));
669 else if (cpp_builtin_macro_p (node
)
670 && CPP_OPTION (pfile
, warn_builtin_macro_redefined
))
671 cpp_warning_with_line (pfile
, CPP_W_BUILTIN_MACRO_REDEFINED
,
672 pfile
->directive_line
, 0,
673 "undefining \"%s\"", NODE_NAME (node
));
675 if (CPP_OPTION (pfile
, warn_unused_macros
))
676 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
678 _cpp_free_definition (node
);
682 check_eol (pfile
, false);
685 /* Undefine a single macro/assertion/whatever. */
688 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
689 void *data_p ATTRIBUTE_UNUSED
)
691 /* Body of _cpp_free_definition inlined here for speed.
692 Macros and assertions no longer have anything to free. */
694 h
->value
.answers
= NULL
;
695 h
->flags
&= ~(NODE_POISONED
|NODE_DISABLED
|NODE_USED
);
699 /* Undefine all macros and assertions. */
702 cpp_undef_all (cpp_reader
*pfile
)
704 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
708 /* Helper routine used by parse_include. Reinterpret the current line
709 as an h-char-sequence (< ... >); we are looking at the first token
710 after the <. Returns a malloced filename. */
712 glue_header_name (cpp_reader
*pfile
)
714 const cpp_token
*token
;
716 size_t len
, total_len
= 0, capacity
= 1024;
718 /* To avoid lexed tokens overwriting our glued name, we can only
719 allocate from the string pool once we've lexed everything. */
720 buffer
= XNEWVEC (char, capacity
);
723 token
= get_token_no_padding (pfile
);
725 if (token
->type
== CPP_GREATER
)
727 if (token
->type
== CPP_EOF
)
729 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
733 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
734 if (total_len
+ len
> capacity
)
736 capacity
= (capacity
+ len
) * 2;
737 buffer
= XRESIZEVEC (char, buffer
, capacity
);
740 if (token
->flags
& PREV_WHITE
)
741 buffer
[total_len
++] = ' ';
743 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
748 buffer
[total_len
] = '\0';
752 /* Returns the file name of #include, #include_next, #import and
753 #pragma dependency. The string is malloced and the caller should
754 free it. Returns NULL on error. LOCATION is the source location
758 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
759 const cpp_token
***buf
, location_t
*location
)
762 const cpp_token
*header
;
764 /* Allow macro expansion. */
765 header
= get_token_no_padding (pfile
);
766 *location
= header
->src_loc
;
767 if ((header
->type
== CPP_STRING
&& header
->val
.str
.text
[0] != 'R')
768 || header
->type
== CPP_HEADER_NAME
)
770 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
771 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
772 fname
[header
->val
.str
.len
- 2] = '\0';
773 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
775 else if (header
->type
== CPP_LESS
)
777 fname
= glue_header_name (pfile
);
778 *pangle_brackets
= 1;
782 const unsigned char *dir
;
784 if (pfile
->directive
== &dtable
[T_PRAGMA
])
785 dir
= UC
"pragma dependency";
787 dir
= pfile
->directive
->name
;
788 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
794 if (pfile
->directive
== &dtable
[T_PRAGMA
])
796 /* This pragma allows extra tokens after the file name. */
798 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
799 check_eol (pfile
, true);
802 /* If we are not discarding comments, then gather them while
803 doing the eol check. */
804 *buf
= check_eol_return_comments (pfile
);
810 /* Handle #include, #include_next and #import. */
812 do_include_common (cpp_reader
*pfile
, enum include_type type
)
816 const cpp_token
**buf
= NULL
;
819 /* Re-enable saving of comments if requested, so that the include
820 callback can dump comments which follow #include. */
821 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
823 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
829 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
830 "empty filename in #%s",
831 pfile
->directive
->name
);
835 /* Prevent #include recursion. */
836 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
837 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
840 /* Get out of macro context, if we are. */
841 skip_rest_of_line (pfile
);
843 if (pfile
->cb
.include
)
844 pfile
->cb
.include (pfile
, pfile
->directive_line
,
845 pfile
->directive
->name
, fname
, angle_brackets
,
848 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
, location
);
858 do_include (cpp_reader
*pfile
)
860 do_include_common (pfile
, IT_INCLUDE
);
864 do_import (cpp_reader
*pfile
)
866 do_include_common (pfile
, IT_IMPORT
);
870 do_include_next (cpp_reader
*pfile
)
872 enum include_type type
= IT_INCLUDE_NEXT
;
874 /* If this is the primary source file, warn and use the normal
876 if (cpp_in_primary_file (pfile
))
878 cpp_error (pfile
, CPP_DL_WARNING
,
879 "#include_next in primary source file");
882 do_include_common (pfile
, type
);
885 /* Subroutine of do_linemarker. Read possible flags after file name.
886 LAST is the last flag seen; 0 if this is the first flag. Return the
887 flag if it is valid, 0 at the end of the directive. Otherwise
890 read_flag (cpp_reader
*pfile
, unsigned int last
)
892 const cpp_token
*token
= _cpp_lex_token (pfile
);
894 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
896 unsigned int flag
= token
->val
.str
.text
[0] - '0';
898 if (flag
> last
&& flag
<= 4
899 && (flag
!= 4 || last
== 3)
900 && (flag
!= 2 || last
== 0))
904 if (token
->type
!= CPP_EOF
)
905 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
906 cpp_token_as_text (pfile
, token
));
910 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
911 of length LEN, to binary; store it in NUMP, and return false if the
912 number was well-formed, true if not. WRAPPED is set to true if the
913 number did not fit into 'unsigned long'. */
915 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
917 linenum_type reg
= 0;
918 linenum_type reg_prev
= 0;
937 /* Interpret #line command.
938 Note that the filename string (if any) is a true string constant
939 (escapes are interpreted), unlike in #line. */
941 do_line (cpp_reader
*pfile
)
943 struct line_maps
*line_table
= pfile
->line_table
;
944 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
946 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
949 unsigned char map_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
950 const cpp_token
*token
;
951 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
952 linenum_type new_lineno
;
954 /* C99 raised the minimum limit on #line numbers. */
955 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
958 /* #line commands expand macros. */
959 token
= cpp_get_token (pfile
);
960 if (token
->type
!= CPP_NUMBER
961 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
962 &new_lineno
, &wrapped
))
964 if (token
->type
== CPP_EOF
)
965 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
967 cpp_error (pfile
, CPP_DL_ERROR
,
968 "\"%s\" after #line is not a positive integer",
969 cpp_token_as_text (pfile
, token
));
973 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
974 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
976 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
978 token
= cpp_get_token (pfile
);
979 if (token
->type
== CPP_STRING
)
981 cpp_string s
= { 0, 0 };
982 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
984 new_file
= (const char *)s
.text
;
985 check_eol (pfile
, true);
987 else if (token
->type
!= CPP_EOF
)
989 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
990 cpp_token_as_text (pfile
, token
));
994 skip_rest_of_line (pfile
);
995 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
997 line_table
->seen_line_directive
= true;
1000 /* Interpret the # 44 "file" [flags] notation, which has slightly
1001 different syntax and semantics from #line: Flags are allowed,
1002 and we never complain about the line number being too big. */
1004 do_linemarker (cpp_reader
*pfile
)
1006 struct line_maps
*line_table
= pfile
->line_table
;
1007 const line_map_ordinary
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1008 const cpp_token
*token
;
1009 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
1010 linenum_type new_lineno
;
1011 unsigned int new_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
1012 enum lc_reason reason
= LC_RENAME_VERBATIM
;
1016 /* Back up so we can get the number again. Putting this in
1017 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
1018 some circumstances, which can segfault. */
1019 _cpp_backup_tokens (pfile
, 1);
1021 /* #line commands expand macros. */
1022 token
= cpp_get_token (pfile
);
1023 if (token
->type
!= CPP_NUMBER
1024 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
1025 &new_lineno
, &wrapped
))
1027 /* Unlike #line, there does not seem to be a way to get an EOF
1028 here. So, it should be safe to always spell the token. */
1029 cpp_error (pfile
, CPP_DL_ERROR
,
1030 "\"%s\" after # is not a positive integer",
1031 cpp_token_as_text (pfile
, token
));
1035 token
= cpp_get_token (pfile
);
1036 if (token
->type
== CPP_STRING
)
1038 cpp_string s
= { 0, 0 };
1039 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
1041 new_file
= (const char *)s
.text
;
1044 flag
= read_flag (pfile
, 0);
1048 /* Fake an include for cpp_included (). */
1049 _cpp_fake_include (pfile
, new_file
);
1050 flag
= read_flag (pfile
, flag
);
1055 flag
= read_flag (pfile
, flag
);
1060 flag
= read_flag (pfile
, flag
);
1064 pfile
->buffer
->sysp
= new_sysp
;
1066 check_eol (pfile
, false);
1068 else if (token
->type
!= CPP_EOF
)
1070 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1071 cpp_token_as_text (pfile
, token
));
1075 skip_rest_of_line (pfile
);
1077 if (reason
== LC_LEAVE
)
1079 /* Reread map since cpp_get_token can invalidate it with a
1081 map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
1082 const line_map_ordinary
*from
1083 = linemap_included_from_linemap (line_table
, map
);
1084 if (MAIN_FILE_P (map
)
1086 && filename_cmp (ORDINARY_MAP_FILE_NAME (from
), new_file
) != 0))
1088 cpp_warning (pfile
, CPP_W_NONE
,
1089 "file \"%s\" linemarker ignored due to "
1090 "incorrect nesting", new_file
);
1094 /* Compensate for the increment in linemap_add that occurs in
1095 _cpp_do_file_change. We're currently at the start of the line
1096 *following* the #line directive. A separate location_t for this
1097 location makes no sense (until we do the LC_LEAVE), and
1098 complicates LAST_SOURCE_LINE_LOCATION. */
1099 pfile
->line_table
->highest_location
--;
1101 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1102 line_table
->seen_line_directive
= true;
1105 /* Arrange the file_change callback. pfile->line has changed to
1106 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1107 header, 2 for a system header that needs to be extern "C" protected,
1108 and zero otherwise. */
1110 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1111 const char *to_file
, linenum_type file_line
,
1114 linemap_assert (reason
!= LC_ENTER_MACRO
);
1115 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1116 to_file
, file_line
);
1117 const line_map_ordinary
*ord_map
= NULL
;
1120 ord_map
= linemap_check_ordinary (map
);
1121 linemap_line_start (pfile
->line_table
,
1122 ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map
),
1126 if (pfile
->cb
.file_change
)
1127 pfile
->cb
.file_change (pfile
, ord_map
);
1130 /* Report a warning or error detected by the program we are
1131 processing. Use the directive's tokens in the error message. */
1133 do_diagnostic (cpp_reader
*pfile
, enum cpp_diagnostic_level code
,
1134 enum cpp_warning_reason reason
, int print_dir
)
1136 const unsigned char *dir_name
;
1137 unsigned char *line
;
1138 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1141 dir_name
= pfile
->directive
->name
;
1144 pfile
->state
.prevent_expansion
++;
1145 line
= cpp_output_line_to_string (pfile
, dir_name
);
1146 pfile
->state
.prevent_expansion
--;
1148 if (code
== CPP_DL_WARNING_SYSHDR
&& reason
)
1149 cpp_warning_with_line_syshdr (pfile
, reason
, src_loc
, 0, "%s", line
);
1150 else if (code
== CPP_DL_WARNING
&& reason
)
1151 cpp_warning_with_line (pfile
, reason
, src_loc
, 0, "%s", line
);
1153 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1158 do_error (cpp_reader
*pfile
)
1160 do_diagnostic (pfile
, CPP_DL_ERROR
, CPP_W_NONE
, 1);
1164 do_warning (cpp_reader
*pfile
)
1166 /* We want #warning diagnostics to be emitted in system headers too. */
1167 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, CPP_W_WARNING_DIRECTIVE
, 1);
1170 /* Report program identification. */
1172 do_ident (cpp_reader
*pfile
)
1174 const cpp_token
*str
= cpp_get_token (pfile
);
1176 if (str
->type
!= CPP_STRING
)
1177 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1178 pfile
->directive
->name
);
1179 else if (pfile
->cb
.ident
)
1180 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1182 check_eol (pfile
, false);
1185 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1186 matching entry, or NULL if none is found. The returned entry could
1187 be the start of a namespace chain, or a pragma. */
1188 static struct pragma_entry
*
1189 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1191 while (chain
&& chain
->pragma
!= pragma
)
1192 chain
= chain
->next
;
1197 /* Create and insert a blank pragma entry at the beginning of a
1198 singly-linked CHAIN. */
1199 static struct pragma_entry
*
1200 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1202 struct pragma_entry
*new_entry
;
1204 new_entry
= (struct pragma_entry
*)
1205 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1207 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1208 new_entry
->next
= *chain
;
1214 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1215 goes in the global namespace. */
1216 static struct pragma_entry
*
1217 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1218 bool allow_name_expansion
)
1220 struct pragma_entry
**chain
= &pfile
->pragmas
;
1221 struct pragma_entry
*entry
;
1222 const cpp_hashnode
*node
;
1226 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1227 entry
= lookup_pragma_entry (*chain
, node
);
1230 entry
= new_pragma_entry (pfile
, chain
);
1231 entry
->pragma
= node
;
1232 entry
->is_nspace
= true;
1233 entry
->allow_expansion
= allow_name_expansion
;
1235 else if (!entry
->is_nspace
)
1237 else if (entry
->allow_expansion
!= allow_name_expansion
)
1239 cpp_error (pfile
, CPP_DL_ICE
,
1240 "registering pragmas in namespace \"%s\" with mismatched "
1241 "name expansion", space
);
1244 chain
= &entry
->u
.space
;
1246 else if (allow_name_expansion
)
1248 cpp_error (pfile
, CPP_DL_ICE
,
1249 "registering pragma \"%s\" with name expansion "
1250 "and no namespace", name
);
1254 /* Check for duplicates. */
1255 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1256 entry
= lookup_pragma_entry (*chain
, node
);
1259 entry
= new_pragma_entry (pfile
, chain
);
1260 entry
->pragma
= node
;
1264 if (entry
->is_nspace
)
1266 cpp_error (pfile
, CPP_DL_ICE
,
1267 "registering \"%s\" as both a pragma and a pragma namespace",
1270 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1273 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1278 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1280 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1281 const char *name
, pragma_cb handler
)
1283 struct pragma_entry
*entry
;
1285 entry
= register_pragma_1 (pfile
, space
, name
, false);
1286 entry
->is_internal
= true;
1287 entry
->u
.handler
= handler
;
1290 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1291 goes in the global namespace. HANDLER is the handler it will call,
1292 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1293 expansion while parsing pragma NAME. This function is exported
1296 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1297 pragma_cb handler
, bool allow_expansion
)
1299 struct pragma_entry
*entry
;
1303 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1307 entry
= register_pragma_1 (pfile
, space
, name
, false);
1310 entry
->allow_expansion
= allow_expansion
;
1311 entry
->u
.handler
= handler
;
1315 /* Similarly, but create mark the pragma for deferred processing.
1316 When found, a CPP_PRAGMA token will be insertted into the stream
1317 with IDENT in the token->u.pragma slot. */
1319 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1320 const char *name
, unsigned int ident
,
1321 bool allow_expansion
, bool allow_name_expansion
)
1323 struct pragma_entry
*entry
;
1325 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1328 entry
->is_deferred
= true;
1329 entry
->allow_expansion
= allow_expansion
;
1330 entry
->u
.ident
= ident
;
1334 /* Register the pragmas the preprocessor itself handles. */
1336 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1338 /* Pragmas in the global namespace. */
1339 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1340 register_pragma_internal (pfile
, 0, "push_macro", do_pragma_push_macro
);
1341 register_pragma_internal (pfile
, 0, "pop_macro", do_pragma_pop_macro
);
1343 /* New GCC-specific pragmas should be put in the GCC namespace. */
1344 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1345 register_pragma_internal (pfile
, "GCC", "system_header",
1346 do_pragma_system_header
);
1347 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1348 register_pragma_internal (pfile
, "GCC", "warning", do_pragma_warning
);
1349 register_pragma_internal (pfile
, "GCC", "error", do_pragma_error
);
1352 /* Return the number of registered pragmas in PE. */
1355 count_registered_pragmas (struct pragma_entry
*pe
)
1358 for (; pe
!= NULL
; pe
= pe
->next
)
1361 ct
+= count_registered_pragmas (pe
->u
.space
);
1367 /* Save into SD the names of the registered pragmas referenced by PE,
1368 and return a pointer to the next free space in SD. */
1371 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1373 for (; pe
!= NULL
; pe
= pe
->next
)
1376 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1377 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1378 HT_LEN (&pe
->pragma
->ident
),
1379 HT_LEN (&pe
->pragma
->ident
) + 1);
1384 /* Return a newly-allocated array which saves the names of the
1385 registered pragmas. */
1388 _cpp_save_pragma_names (cpp_reader
*pfile
)
1390 int ct
= count_registered_pragmas (pfile
->pragmas
);
1391 char **result
= XNEWVEC (char *, ct
);
1392 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1396 /* Restore from SD the names of the registered pragmas referenced by PE,
1397 and return a pointer to the next unused name in SD. */
1400 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1403 for (; pe
!= NULL
; pe
= pe
->next
)
1406 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1407 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1414 /* Restore the names of the registered pragmas from SAVED. */
1417 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1419 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1423 /* Pragmata handling. We handle some, and pass the rest on to the
1424 front end. C99 defines three pragmas and says that no macro
1425 expansion is to be performed on them; whether or not macro
1426 expansion happens for other pragmas is implementation defined.
1427 This implementation allows for a mix of both, since GCC did not
1428 traditionally macro expand its (few) pragmas, whereas OpenMP
1429 specifies that macro expansion should happen. */
1431 do_pragma (cpp_reader
*pfile
)
1433 const struct pragma_entry
*p
= NULL
;
1434 const cpp_token
*token
, *pragma_token
;
1435 location_t pragma_token_virt_loc
= 0;
1437 unsigned int count
= 1;
1439 pfile
->state
.prevent_expansion
++;
1441 pragma_token
= token
= cpp_get_token_with_location (pfile
,
1442 &pragma_token_virt_loc
);
1444 if (token
->type
== CPP_NAME
)
1446 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1447 if (p
&& p
->is_nspace
)
1449 bool allow_name_expansion
= p
->allow_expansion
;
1450 if (allow_name_expansion
)
1451 pfile
->state
.prevent_expansion
--;
1453 token
= cpp_get_token (pfile
);
1454 if (token
->type
== CPP_NAME
)
1455 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1458 if (allow_name_expansion
)
1459 pfile
->state
.prevent_expansion
++;
1468 pfile
->directive_result
.src_loc
= pragma_token_virt_loc
;
1469 pfile
->directive_result
.type
= CPP_PRAGMA
;
1470 pfile
->directive_result
.flags
= pragma_token
->flags
;
1471 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1472 pfile
->state
.in_deferred_pragma
= true;
1473 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1474 if (!p
->allow_expansion
)
1475 pfile
->state
.prevent_expansion
++;
1479 /* Since the handler below doesn't get the line number, that
1480 it might need for diagnostics, make sure it has the right
1481 numbers in place. */
1482 if (pfile
->cb
.line_change
)
1483 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1484 if (p
->allow_expansion
)
1485 pfile
->state
.prevent_expansion
--;
1486 (*p
->u
.handler
) (pfile
);
1487 if (p
->allow_expansion
)
1488 pfile
->state
.prevent_expansion
++;
1491 else if (pfile
->cb
.def_pragma
)
1493 if (count
== 1 || pfile
->context
->prev
== NULL
)
1494 _cpp_backup_tokens (pfile
, count
);
1497 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1498 won't allow backing 2 tokens. */
1499 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1500 reads both tokens, we could perhaps free it, but if it doesn't,
1501 we don't know the exact lifespan. */
1502 cpp_token
*toks
= XNEWVEC (cpp_token
, 2);
1504 toks
[0].flags
|= NO_EXPAND
;
1506 toks
[1].flags
|= NO_EXPAND
;
1507 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1509 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1512 pfile
->state
.prevent_expansion
--;
1515 /* Handle #pragma once. */
1517 do_pragma_once (cpp_reader
*pfile
)
1519 if (cpp_in_primary_file (pfile
))
1520 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1522 check_eol (pfile
, false);
1523 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1526 /* Handle #pragma push_macro(STRING). */
1528 do_pragma_push_macro (cpp_reader
*pfile
)
1532 const uchar
*defn
= NULL
;
1533 char *macroname
, *dest
;
1534 const char *limit
, *src
;
1535 const cpp_token
*txt
;
1536 struct def_pragma_macro
*c
;
1538 txt
= get__Pragma_string (pfile
);
1541 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1542 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1543 "invalid #pragma push_macro directive");
1544 check_eol (pfile
, false);
1545 skip_rest_of_line (pfile
);
1548 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1549 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1550 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1553 /* We know there is a character following the backslash. */
1554 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1559 check_eol (pfile
, false);
1560 skip_rest_of_line (pfile
);
1561 c
= XNEW (struct def_pragma_macro
);
1562 memset (c
, 0, sizeof (struct def_pragma_macro
));
1563 c
->name
= XNEWVAR (char, strlen (macroname
) + 1);
1564 strcpy (c
->name
, macroname
);
1565 c
->next
= pfile
->pushed_macros
;
1566 node
= _cpp_lex_identifier (pfile
, c
->name
);
1567 if (node
->type
== NT_VOID
)
1571 defn
= cpp_macro_definition (pfile
, node
);
1572 defnlen
= ustrlen (defn
);
1573 c
->definition
= XNEWVEC (uchar
, defnlen
+ 2);
1574 c
->definition
[defnlen
] = '\n';
1575 c
->definition
[defnlen
+ 1] = 0;
1576 c
->line
= node
->value
.macro
->line
;
1577 c
->syshdr
= node
->value
.macro
->syshdr
;
1578 c
->used
= node
->value
.macro
->used
;
1579 memcpy (c
->definition
, defn
, defnlen
);
1582 pfile
->pushed_macros
= c
;
1585 /* Handle #pragma pop_macro(STRING). */
1587 do_pragma_pop_macro (cpp_reader
*pfile
)
1589 char *macroname
, *dest
;
1590 const char *limit
, *src
;
1591 const cpp_token
*txt
;
1592 struct def_pragma_macro
*l
= NULL
, *c
= pfile
->pushed_macros
;
1593 txt
= get__Pragma_string (pfile
);
1596 location_t src_loc
= pfile
->cur_token
[-1].src_loc
;
1597 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1598 "invalid #pragma pop_macro directive");
1599 check_eol (pfile
, false);
1600 skip_rest_of_line (pfile
);
1603 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1604 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1605 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1608 /* We know there is a character following the backslash. */
1609 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1614 check_eol (pfile
, false);
1615 skip_rest_of_line (pfile
);
1619 if (!strcmp (c
->name
, macroname
))
1622 pfile
->pushed_macros
= c
->next
;
1625 cpp_pop_definition (pfile
, c
);
1626 free (c
->definition
);
1636 /* Handle #pragma GCC poison, to poison one or more identifiers so
1637 that the lexer produces a hard error for each subsequent usage. */
1639 do_pragma_poison (cpp_reader
*pfile
)
1641 const cpp_token
*tok
;
1644 pfile
->state
.poisoned_ok
= 1;
1647 tok
= _cpp_lex_token (pfile
);
1648 if (tok
->type
== CPP_EOF
)
1650 if (tok
->type
!= CPP_NAME
)
1652 cpp_error (pfile
, CPP_DL_ERROR
,
1653 "invalid #pragma GCC poison directive");
1657 hp
= tok
->val
.node
.node
;
1658 if (hp
->flags
& NODE_POISONED
)
1661 if (cpp_macro_p (hp
))
1662 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1664 _cpp_free_definition (hp
);
1665 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1667 pfile
->state
.poisoned_ok
= 0;
1670 /* Mark the current header as a system header. This will suppress
1671 some categories of warnings (notably those from -pedantic). It is
1672 intended for use in system libraries that cannot be implemented in
1673 conforming C, but cannot be certain that their headers appear in a
1674 system include directory. To prevent abuse, it is rejected in the
1675 primary source file. */
1677 do_pragma_system_header (cpp_reader
*pfile
)
1679 if (cpp_in_primary_file (pfile
))
1680 cpp_error (pfile
, CPP_DL_WARNING
,
1681 "#pragma system_header ignored outside include file");
1684 check_eol (pfile
, false);
1685 skip_rest_of_line (pfile
);
1686 cpp_make_system_header (pfile
, 1, 0);
1690 /* Check the modified date of the current include file against a specified
1691 file. Issue a diagnostic, if the specified file is newer. We use this to
1692 determine if a fixed header should be refixed. */
1694 do_pragma_dependency (cpp_reader
*pfile
)
1697 int angle_brackets
, ordering
;
1698 location_t location
;
1700 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
1704 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1706 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1707 else if (ordering
> 0)
1709 cpp_error (pfile
, CPP_DL_WARNING
,
1710 "current file is older than %s", fname
);
1711 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1713 _cpp_backup_tokens (pfile
, 1);
1714 do_diagnostic (pfile
, CPP_DL_WARNING
, CPP_W_NONE
, 0);
1718 free ((void *) fname
);
1721 /* Issue a diagnostic with the message taken from the pragma. If
1722 ERROR is true, the diagnostic is a warning, otherwise, it is an
1725 do_pragma_warning_or_error (cpp_reader
*pfile
, bool error
)
1727 const cpp_token
*tok
= _cpp_lex_token (pfile
);
1729 if (tok
->type
!= CPP_STRING
1730 || !cpp_interpret_string_notranslate (pfile
, &tok
->val
.str
, 1, &str
,
1734 cpp_error (pfile
, CPP_DL_ERROR
, "invalid \"#pragma GCC %s\" directive",
1735 error
? "error" : "warning");
1738 cpp_error (pfile
, error
? CPP_DL_ERROR
: CPP_DL_WARNING
,
1740 free ((void *)str
.text
);
1743 /* Issue a warning diagnostic. */
1745 do_pragma_warning (cpp_reader
*pfile
)
1747 do_pragma_warning_or_error (pfile
, false);
1750 /* Issue an error diagnostic. */
1752 do_pragma_error (cpp_reader
*pfile
)
1754 do_pragma_warning_or_error (pfile
, true);
1757 /* Get a token but skip padding. */
1758 static const cpp_token
*
1759 get_token_no_padding (cpp_reader
*pfile
)
1763 const cpp_token
*result
= cpp_get_token (pfile
);
1764 if (result
->type
!= CPP_PADDING
)
1769 /* Check syntax is "(string-literal)". Returns the string on success,
1770 or NULL on failure. */
1771 static const cpp_token
*
1772 get__Pragma_string (cpp_reader
*pfile
)
1774 const cpp_token
*string
;
1775 const cpp_token
*paren
;
1777 paren
= get_token_no_padding (pfile
);
1778 if (paren
->type
== CPP_EOF
)
1779 _cpp_backup_tokens (pfile
, 1);
1780 if (paren
->type
!= CPP_OPEN_PAREN
)
1783 string
= get_token_no_padding (pfile
);
1784 if (string
->type
== CPP_EOF
)
1785 _cpp_backup_tokens (pfile
, 1);
1786 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1787 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
1788 && string
->type
!= CPP_UTF8STRING
)
1791 paren
= get_token_no_padding (pfile
);
1792 if (paren
->type
== CPP_EOF
)
1793 _cpp_backup_tokens (pfile
, 1);
1794 if (paren
->type
!= CPP_CLOSE_PAREN
)
1800 /* Destringize IN into a temporary buffer, by removing the first \ of
1801 \" and \\ sequences, and process the result as a #pragma directive. */
1803 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
,
1804 location_t expansion_loc
)
1806 const unsigned char *src
, *limit
;
1807 char *dest
, *result
;
1808 cpp_context
*saved_context
;
1809 cpp_token
*saved_cur_token
;
1810 tokenrun
*saved_cur_run
;
1813 const struct directive
*save_directive
;
1815 dest
= result
= (char *) alloca (in
->len
- 1);
1816 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1817 limit
= in
->text
+ in
->len
- 1;
1820 /* We know there is a character following the backslash. */
1821 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1827 /* Ugh; an awful kludge. We are really not set up to be lexing
1828 tokens when in the middle of a macro expansion. Use a new
1829 context to force cpp_get_token to lex, and so skip_rest_of_line
1830 doesn't go beyond the end of the text. Also, remember the
1831 current lexing position so we can return to it later.
1833 Something like line-at-a-time lexing should remove the need for
1835 saved_context
= pfile
->context
;
1836 saved_cur_token
= pfile
->cur_token
;
1837 saved_cur_run
= pfile
->cur_run
;
1839 pfile
->context
= XCNEW (cpp_context
);
1841 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1842 until we've read all of the tokens that we want. */
1843 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1844 /* from_stage3 */ true);
1845 /* ??? Antique Disgusting Hack. What does this do? */
1846 if (pfile
->buffer
->prev
)
1847 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1849 start_directive (pfile
);
1850 _cpp_clean_line (pfile
);
1851 save_directive
= pfile
->directive
;
1852 pfile
->directive
= &dtable
[T_PRAGMA
];
1854 end_directive (pfile
, 1);
1855 pfile
->directive
= save_directive
;
1857 /* We always insert at least one token, the directive result. It'll
1858 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1859 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1861 /* If we're not handling the pragma internally, read all of the tokens from
1862 the string buffer now, while the string buffer is still installed. */
1863 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1864 to me what the true lifespan of the tokens are. It would appear that
1865 the lifespan is the entire parse of the main input stream, in which case
1866 this may not be wrong. */
1867 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1873 toks
= XNEWVEC (cpp_token
, maxcount
);
1874 toks
[0] = pfile
->directive_result
;
1878 if (count
== maxcount
)
1880 maxcount
= maxcount
* 3 / 2;
1881 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1883 toks
[count
] = *cpp_get_token (pfile
);
1884 /* _Pragma is a builtin, so we're not within a macro-map, and so
1885 the token locations are set to bogus ordinary locations
1886 near to, but after that of the "_Pragma".
1887 Paper over this by setting them equal to the location of the
1888 _Pragma itself (PR preprocessor/69126). */
1889 toks
[count
].src_loc
= expansion_loc
;
1890 /* Macros have been already expanded by cpp_get_token
1891 if the pragma allowed expansion. */
1892 toks
[count
++].flags
|= NO_EXPAND
;
1894 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1899 toks
= XNEW (cpp_token
);
1900 toks
[0] = pfile
->directive_result
;
1902 /* If we handled the entire pragma internally, make sure we get the
1903 line number correct for the next token. */
1904 if (pfile
->cb
.line_change
)
1905 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1908 /* Finish inlining run_directive. */
1909 pfile
->buffer
->file
= NULL
;
1910 _cpp_pop_buffer (pfile
);
1912 /* Reset the old macro state before ... */
1913 XDELETE (pfile
->context
);
1914 pfile
->context
= saved_context
;
1915 pfile
->cur_token
= saved_cur_token
;
1916 pfile
->cur_run
= saved_cur_run
;
1918 /* ... inserting the new tokens we collected. */
1919 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1922 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1924 _cpp_do__Pragma (cpp_reader
*pfile
, location_t expansion_loc
)
1926 const cpp_token
*string
= get__Pragma_string (pfile
);
1927 pfile
->directive_result
.type
= CPP_PADDING
;
1931 destringize_and_run (pfile
, &string
->val
.str
, expansion_loc
);
1934 cpp_error (pfile
, CPP_DL_ERROR
,
1935 "_Pragma takes a parenthesized string literal");
1939 /* Handle #ifdef. */
1941 do_ifdef (cpp_reader
*pfile
)
1945 if (! pfile
->state
.skipping
)
1947 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
1951 /* Do not treat conditional macros as being defined. This is due to
1952 the powerpc and spu ports using conditional macros for 'vector',
1953 'bool', and 'pixel' to act as conditional keywords. This messes
1954 up tests like #ifndef bool. */
1955 skip
= !cpp_macro_p (node
) || (node
->flags
& NODE_CONDITIONAL
);
1956 _cpp_mark_macro_used (node
);
1957 _cpp_maybe_notify_macro_use (pfile
, node
);
1959 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1960 check_eol (pfile
, false);
1964 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1967 /* Handle #ifndef. */
1969 do_ifndef (cpp_reader
*pfile
)
1972 cpp_hashnode
*node
= 0;
1974 if (! pfile
->state
.skipping
)
1976 node
= lex_macro_node (pfile
, false);
1980 /* Do not treat conditional macros as being defined. This is due to
1981 the powerpc and spu ports using conditional macros for 'vector',
1982 'bool', and 'pixel' to act as conditional keywords. This messes
1983 up tests like #ifndef bool. */
1984 skip
= (cpp_macro_p (node
)
1985 && !(node
->flags
& NODE_CONDITIONAL
));
1986 _cpp_mark_macro_used (node
);
1987 _cpp_maybe_notify_macro_use (pfile
, node
);
1989 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1990 check_eol (pfile
, false);
1994 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1997 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1998 pfile->mi_ind_cmacro so we can handle multiple-include
1999 optimizations. If macro expansion occurs in the expression, we
2000 cannot treat it as a controlling conditional, since the expansion
2001 could change in the future. That is handled by cpp_get_token. */
2003 do_if (cpp_reader
*pfile
)
2007 if (! pfile
->state
.skipping
)
2008 skip
= _cpp_parse_expr (pfile
, true) == false;
2010 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
2013 /* Flip skipping state if appropriate and continue without changing
2014 if_stack; this is so that the error message for missing #endif's
2015 etc. will point to the original #if. */
2017 do_else (cpp_reader
*pfile
)
2019 cpp_buffer
*buffer
= pfile
->buffer
;
2020 struct if_stack
*ifs
= buffer
->if_stack
;
2023 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
2026 if (ifs
->type
== T_ELSE
)
2028 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
2029 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2030 "the conditional began here");
2034 /* Skip any future (erroneous) #elses or #elifs. */
2035 pfile
->state
.skipping
= ifs
->skip_elses
;
2036 ifs
->skip_elses
= true;
2038 /* Invalidate any controlling macro. */
2041 /* Only check EOL if was not originally skipping. */
2042 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2043 check_eol_endif_labels (pfile
);
2047 /* Handle a #elif directive by not changing if_stack either. See the
2048 comment above do_else. */
2050 do_elif (cpp_reader
*pfile
)
2052 cpp_buffer
*buffer
= pfile
->buffer
;
2053 struct if_stack
*ifs
= buffer
->if_stack
;
2056 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
2059 if (ifs
->type
== T_ELSE
)
2061 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
2062 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2063 "the conditional began here");
2067 /* See DR#412: "Only the first group whose control condition
2068 evaluates to true (nonzero) is processed; any following groups
2069 are skipped and their controlling directives are processed as
2070 if they were in a group that is skipped." */
2071 if (ifs
->skip_elses
)
2072 pfile
->state
.skipping
= 1;
2075 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
, false);
2076 ifs
->skip_elses
= ! pfile
->state
.skipping
;
2079 /* Invalidate any controlling macro. */
2084 /* #endif pops the if stack and resets pfile->state.skipping. */
2086 do_endif (cpp_reader
*pfile
)
2088 cpp_buffer
*buffer
= pfile
->buffer
;
2089 struct if_stack
*ifs
= buffer
->if_stack
;
2092 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
2095 /* Only check EOL if was not originally skipping. */
2096 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2097 check_eol_endif_labels (pfile
);
2099 /* If potential control macro, we go back outside again. */
2100 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
2102 pfile
->mi_valid
= true;
2103 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
2106 buffer
->if_stack
= ifs
->next
;
2107 pfile
->state
.skipping
= ifs
->was_skipping
;
2108 obstack_free (&pfile
->buffer_ob
, ifs
);
2112 /* Push an if_stack entry for a preprocessor conditional, and set
2113 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2114 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2115 we need to check here that we are at the top of the file. */
2117 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
2118 const cpp_hashnode
*cmacro
)
2120 struct if_stack
*ifs
;
2121 cpp_buffer
*buffer
= pfile
->buffer
;
2123 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
2124 ifs
->line
= pfile
->directive_line
;
2125 ifs
->next
= buffer
->if_stack
;
2126 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
2127 ifs
->was_skipping
= pfile
->state
.skipping
;
2129 /* This condition is effectively a test for top-of-file. */
2130 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
2131 ifs
->mi_cmacro
= cmacro
;
2135 pfile
->state
.skipping
= skip
;
2136 buffer
->if_stack
= ifs
;
2139 /* Read the tokens of the answer into the macro pool, in a directive
2140 of type TYPE. Only commit the memory if we intend it as permanent
2141 storage, i.e. the #assert case. Returns 0 on success, and sets
2142 ANSWERP to point to the answer. PRED_LOC is the location of the
2145 parse_answer (cpp_reader
*pfile
, int type
, location_t pred_loc
,
2146 cpp_macro
**answer_ptr
)
2148 /* In a conditional, it is legal to not have an open paren. We
2149 should save the following token in this case. */
2150 const cpp_token
*paren
= cpp_get_token (pfile
);
2152 /* If not a paren, see if we're OK. */
2153 if (paren
->type
!= CPP_OPEN_PAREN
)
2155 /* In a conditional no answer is a test for any answer. It
2156 could be followed by any token. */
2159 _cpp_backup_tokens (pfile
, 1);
2163 /* #unassert with no answer is valid - it removes all answers. */
2164 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
2167 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
2168 "missing '(' after predicate");
2172 cpp_macro
*answer
= _cpp_new_macro (pfile
, cmk_assert
,
2173 _cpp_reserve_room (pfile
, 0,
2174 sizeof (cpp_macro
)));
2175 answer
->parm
.next
= NULL
;
2179 const cpp_token
*token
= cpp_get_token (pfile
);
2181 if (token
->type
== CPP_CLOSE_PAREN
)
2184 if (token
->type
== CPP_EOF
)
2186 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
2190 answer
= (cpp_macro
*)_cpp_reserve_room
2191 (pfile
, sizeof (cpp_macro
) + count
* sizeof (cpp_token
),
2192 sizeof (cpp_token
));
2193 answer
->exp
.tokens
[count
++] = *token
;
2198 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
2202 /* Drop whitespace at start, for answer equivalence purposes. */
2203 answer
->exp
.tokens
[0].flags
&= ~PREV_WHITE
;
2205 answer
->count
= count
;
2206 *answer_ptr
= answer
;
2211 /* Parses an assertion directive of type TYPE, returning a pointer to
2212 the hash node of the predicate, or 0 on error. The node is
2213 guaranteed to be disjoint from the macro namespace, so can only
2214 have type 'NT_VOID'. If an answer was supplied, it is placed in
2215 *ANSWER_PTR, which is otherwise set to 0. */
2216 static cpp_hashnode
*
2217 parse_assertion (cpp_reader
*pfile
, int type
, cpp_macro
**answer_ptr
)
2219 cpp_hashnode
*result
= 0;
2221 /* We don't expand predicates or answers. */
2222 pfile
->state
.prevent_expansion
++;
2226 const cpp_token
*predicate
= cpp_get_token (pfile
);
2227 if (predicate
->type
== CPP_EOF
)
2228 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2229 else if (predicate
->type
!= CPP_NAME
)
2230 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2231 "predicate must be an identifier");
2232 else if (parse_answer (pfile
, type
, predicate
->src_loc
, answer_ptr
))
2234 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2235 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2237 /* Prefix '#' to get it out of macro namespace. */
2239 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2240 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2243 pfile
->state
.prevent_expansion
--;
2248 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2249 or a pointer to NULL if the answer is not in the chain. */
2251 find_answer (cpp_hashnode
*node
, const cpp_macro
*candidate
)
2254 cpp_macro
**result
= NULL
;
2256 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->parm
.next
)
2258 cpp_macro
*answer
= *result
;
2260 if (answer
->count
== candidate
->count
)
2262 for (i
= 0; i
< answer
->count
; i
++)
2263 if (!_cpp_equiv_tokens (&answer
->exp
.tokens
[i
],
2264 &candidate
->exp
.tokens
[i
]))
2267 if (i
== answer
->count
)
2275 /* Test an assertion within a preprocessor conditional. Returns
2276 nonzero on failure, zero on success. On success, the result of
2277 the test is written into VALUE, otherwise the value 0. */
2279 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2282 cpp_hashnode
*node
= parse_assertion (pfile
, T_IF
, &answer
);
2284 /* For recovery, an erroneous assertion expression is handled as a
2285 failing assertion. */
2290 if (node
->value
.answers
)
2291 *value
= !answer
|| *find_answer (node
, answer
);
2293 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2294 _cpp_backup_tokens (pfile
, 1);
2296 /* We don't commit the memory for the answer - it's temporary only. */
2300 /* Handle #assert. */
2302 do_assert (cpp_reader
*pfile
)
2305 cpp_hashnode
*node
= parse_assertion (pfile
, T_ASSERT
, &answer
);
2309 /* Place the new answer in the answer list. First check there
2310 is not a duplicate. */
2311 if (*find_answer (node
, answer
))
2313 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2314 NODE_NAME (node
) + 1);
2318 /* Commit or allocate storage for the answer. */
2319 answer
= (cpp_macro
*)_cpp_commit_buff
2320 (pfile
, sizeof (cpp_macro
) - sizeof (cpp_token
)
2321 + sizeof (cpp_token
) * answer
->count
);
2323 /* Chain into the list. */
2324 answer
->parm
.next
= node
->value
.answers
;
2325 node
->value
.answers
= answer
;
2327 check_eol (pfile
, false);
2331 /* Handle #unassert. */
2333 do_unassert (cpp_reader
*pfile
)
2336 cpp_hashnode
*node
= parse_assertion (pfile
, T_UNASSERT
, &answer
);
2338 /* It isn't an error to #unassert something that isn't asserted. */
2343 cpp_macro
**p
= find_answer (node
, answer
);
2345 /* Remove the assert from the list. */
2346 if (cpp_macro
*temp
= *p
)
2347 *p
= temp
->parm
.next
;
2349 check_eol (pfile
, false);
2352 _cpp_free_definition (node
);
2355 /* We don't commit the memory for the answer - it's temporary only. */
2358 /* These are for -D, -U, -A. */
2360 /* Process the string STR as if it appeared as the body of a #define.
2361 If STR is just an identifier, define it with value 1.
2362 If STR has anything after the identifier, then it should
2363 be identifier=definition. */
2365 cpp_define (cpp_reader
*pfile
, const char *str
)
2371 /* Copy the entire option so we can modify it.
2372 Change the first "=" in the string to a space. If there is none,
2373 tack " 1" on the end. */
2375 count
= strlen (str
);
2376 buf
= (char *) alloca (count
+ 3);
2377 memcpy (buf
, str
, count
);
2379 p
= strchr (str
, '=');
2389 run_directive (pfile
, T_DEFINE
, buf
, count
);
2393 /* Use to build macros to be run through cpp_define() as
2395 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2398 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2404 ptr
= xvasprintf (fmt
, ap
);
2407 cpp_define (pfile
, ptr
);
2412 /* Slight variant of the above for use by initialize_builtins. */
2414 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2416 size_t len
= strlen (str
);
2417 char *buf
= (char *) alloca (len
+ 1);
2418 memcpy (buf
, str
, len
);
2420 run_directive (pfile
, T_DEFINE
, buf
, len
);
2423 /* Process MACRO as if it appeared as the body of an #undef. */
2425 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2427 size_t len
= strlen (macro
);
2428 char *buf
= (char *) alloca (len
+ 1);
2429 memcpy (buf
, macro
, len
);
2431 run_directive (pfile
, T_UNDEF
, buf
, len
);
2434 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2435 or first element is zero, then the macro should be undefined. */
2437 cpp_pop_definition (cpp_reader
*pfile
, struct def_pragma_macro
*c
)
2439 cpp_hashnode
*node
= _cpp_lex_identifier (pfile
, c
->name
);
2443 if (pfile
->cb
.before_define
)
2444 pfile
->cb
.before_define (pfile
);
2446 if (cpp_macro_p (node
))
2448 if (pfile
->cb
.undef
)
2449 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2450 if (CPP_OPTION (pfile
, warn_unused_macros
))
2451 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2452 _cpp_free_definition (node
);
2461 cpp_hashnode
*h
= NULL
;
2464 namelen
= ustrcspn (c
->definition
, "( \n");
2465 h
= cpp_lookup (pfile
, c
->definition
, namelen
);
2466 dn
= c
->definition
+ namelen
;
2468 nbuf
= cpp_push_buffer (pfile
, dn
, ustrchr (dn
, '\n') - dn
, true);
2471 _cpp_clean_line (pfile
);
2473 if (!_cpp_create_definition (pfile
, h
))
2475 _cpp_pop_buffer (pfile
);
2479 h
->value
.macro
->line
= c
->line
;
2480 h
->value
.macro
->syshdr
= c
->syshdr
;
2481 h
->value
.macro
->used
= c
->used
;
2485 /* Process the string STR as if it appeared as the body of a #assert. */
2487 cpp_assert (cpp_reader
*pfile
, const char *str
)
2489 handle_assertion (pfile
, str
, T_ASSERT
);
2492 /* Process STR as if it appeared as the body of an #unassert. */
2494 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2496 handle_assertion (pfile
, str
, T_UNASSERT
);
2499 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2501 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2503 size_t count
= strlen (str
);
2504 const char *p
= strchr (str
, '=');
2506 /* Copy the entire option so we can modify it. Change the first
2507 "=" in the string to a '(', and tack a ')' on the end. */
2508 char *buf
= (char *) alloca (count
+ 2);
2510 memcpy (buf
, str
, count
);
2519 run_directive (pfile
, type
, str
, count
);
2522 /* The options structure. */
2524 cpp_get_options (cpp_reader
*pfile
)
2526 return &pfile
->opts
;
2529 /* The callbacks structure. */
2531 cpp_get_callbacks (cpp_reader
*pfile
)
2536 /* Copy the given callbacks structure to our own. */
2538 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2543 /* The dependencies structure. (Creates one if it hasn't already been.) */
2545 cpp_get_deps (cpp_reader
*pfile
)
2548 pfile
->deps
= deps_init ();
2552 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2553 doesn't fail. It does not generate a file change call back; that
2554 is the responsibility of the caller. */
2556 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2559 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2561 /* Clears, amongst other things, if_stack and mi_cmacro. */
2562 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2564 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2565 new_buffer
->rlimit
= buffer
+ len
;
2566 new_buffer
->from_stage3
= from_stage3
;
2567 new_buffer
->prev
= pfile
->buffer
;
2568 new_buffer
->need_line
= true;
2570 pfile
->buffer
= new_buffer
;
2575 /* Pops a single buffer, with a file change call-back if appropriate.
2576 Then pushes the next -include file, if any remain. */
2578 _cpp_pop_buffer (cpp_reader
*pfile
)
2580 cpp_buffer
*buffer
= pfile
->buffer
;
2581 struct _cpp_file
*inc
= buffer
->file
;
2582 struct if_stack
*ifs
;
2583 const unsigned char *to_free
;
2585 /* Walk back up the conditional stack till we reach its level at
2586 entry to this file, issuing error messages. */
2587 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2588 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2589 "unterminated #%s", dtable
[ifs
->type
].name
);
2591 /* In case of a missing #endif. */
2592 pfile
->state
.skipping
= 0;
2594 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2595 pfile
->buffer
= buffer
->prev
;
2597 to_free
= buffer
->to_free
;
2598 free (buffer
->notes
);
2600 /* Free the buffer object now; we may want to push a new buffer
2601 in _cpp_push_next_include_file. */
2602 obstack_free (&pfile
->buffer_ob
, buffer
);
2606 _cpp_pop_file_buffer (pfile
, inc
, to_free
);
2608 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2611 free ((void *)to_free
);
2614 /* Enter all recognized directives in the hash table. */
2616 _cpp_init_directives (cpp_reader
*pfile
)
2621 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2623 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2624 node
->is_directive
= 1;
2625 node
->directive_index
= i
;
2629 /* Extract header file from a bracket include. Parsing starts after '<'.
2630 The string is malloced and must be freed by the caller. */
2632 _cpp_bracket_include(cpp_reader
*pfile
)
2634 return glue_header_name (pfile
);