1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986-2015 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
*);
126 static int parse_answer (cpp_reader
*, struct answer
**, int, source_location
);
127 static cpp_hashnode
*parse_assertion (cpp_reader
*, struct answer
**, int);
128 static struct answer
** find_answer (cpp_hashnode
*, const struct answer
*);
129 static void handle_assertion (cpp_reader
*, const char *, int);
130 static void do_pragma_push_macro (cpp_reader
*);
131 static void do_pragma_pop_macro (cpp_reader
*);
132 static void cpp_pop_definition (cpp_reader
*, struct def_pragma_macro
*);
134 /* This is the table of directive handlers. It is ordered by
135 frequency of occurrence; the numbers at the end are directive
136 counts from all the source code I have lying around (egcs and libc
137 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
138 pcmcia-cs-3.0.9). This is no longer important as directive lookup
139 is now O(1). All extensions other than #warning, #include_next,
140 and #import are deprecated. The name is where the extension
141 appears to have come from. */
143 #define DIRECTIVE_TABLE \
144 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
145 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
146 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
147 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
148 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
149 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
150 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
151 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
152 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
153 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
154 D(error, T_ERROR, STDC89, 0) /* 475 */ \
155 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
156 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
157 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
158 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
159 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
160 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
161 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
162 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
164 /* #sccs is synonymous with #ident. */
165 #define do_sccs do_ident
167 /* Use the table to generate a series of prototypes, an enum for the
168 directive names, and an array of directive handlers. */
170 #define D(name, t, o, f) static void do_##name (cpp_reader *);
174 #define D(n, tag, o, f) tag,
182 #define D(name, t, origin, flags) \
183 { do_##name, (const uchar *) #name, \
184 sizeof #name - 1, origin, flags },
185 static const directive dtable
[] =
190 #undef DIRECTIVE_TABLE
192 /* Wrapper struct directive for linemarkers.
193 The origin is more or less true - the original K+R cpp
194 did use this notation in its preprocessed output. */
195 static const directive linemarker_dir
=
197 do_linemarker
, UC
"#", 1, KANDR
, IN_I
200 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
202 /* Skip any remaining tokens in a directive. */
204 skip_rest_of_line (cpp_reader
*pfile
)
206 /* Discard all stacked contexts. */
207 while (pfile
->context
->prev
)
208 _cpp_pop_context (pfile
);
210 /* Sweep up all tokens remaining on the line. */
212 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
216 /* Helper function for check_oel. */
219 check_eol_1 (cpp_reader
*pfile
, bool expand
, int reason
)
221 if (! SEEN_EOL () && (expand
222 ? cpp_get_token (pfile
)
223 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
224 cpp_pedwarning (pfile
, reason
, "extra tokens at end of #%s directive",
225 pfile
->directive
->name
);
228 /* Variant of check_eol used for Wendif-labels warnings. */
231 check_eol_endif_labels (cpp_reader
*pfile
)
233 check_eol_1 (pfile
, false, CPP_W_ENDIF_LABELS
);
236 /* Ensure there are no stray tokens at the end of a directive. If
237 EXPAND is true, tokens macro-expanding to nothing are allowed. */
240 check_eol (cpp_reader
*pfile
, bool expand
)
242 check_eol_1 (pfile
, expand
, CPP_W_NONE
);
245 /* Ensure there are no stray tokens other than comments at the end of
246 a directive, and gather the comments. */
247 static const cpp_token
**
248 check_eol_return_comments (cpp_reader
*pfile
)
252 const cpp_token
**buf
;
254 buf
= XNEWVEC (const cpp_token
*, capacity
);
260 const cpp_token
*tok
;
262 tok
= _cpp_lex_token (pfile
);
263 if (tok
->type
== CPP_EOF
)
265 if (tok
->type
!= CPP_COMMENT
)
266 cpp_error (pfile
, CPP_DL_PEDWARN
,
267 "extra tokens at end of #%s directive",
268 pfile
->directive
->name
);
271 if (c
+ 1 >= capacity
)
274 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
285 /* Called when entering a directive, _Pragma or command-line directive. */
287 start_directive (cpp_reader
*pfile
)
289 /* Setup in-directive state. */
290 pfile
->state
.in_directive
= 1;
291 pfile
->state
.save_comments
= 0;
292 pfile
->directive_result
.type
= CPP_PADDING
;
294 /* Some handlers need the position of the # for diagnostics. */
295 pfile
->directive_line
= pfile
->line_table
->highest_line
;
298 /* Called when leaving a directive, _Pragma or command-line directive. */
300 end_directive (cpp_reader
*pfile
, int skip_line
)
302 if (CPP_OPTION (pfile
, traditional
))
304 /* Revert change of prepare_directive_trad. */
305 if (!pfile
->state
.in_deferred_pragma
)
306 pfile
->state
.prevent_expansion
--;
308 if (pfile
->directive
!= &dtable
[T_DEFINE
])
309 _cpp_remove_overlay (pfile
);
311 else if (pfile
->state
.in_deferred_pragma
)
313 /* We don't skip for an assembler #. */
316 skip_rest_of_line (pfile
);
317 if (!pfile
->keep_tokens
)
319 pfile
->cur_run
= &pfile
->base_run
;
320 pfile
->cur_token
= pfile
->base_run
.base
;
325 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
326 pfile
->state
.in_directive
= 0;
327 pfile
->state
.in_expression
= 0;
328 pfile
->state
.angled_headers
= 0;
329 pfile
->directive
= 0;
332 /* Prepare to handle the directive in pfile->directive. */
334 prepare_directive_trad (cpp_reader
*pfile
)
336 if (pfile
->directive
!= &dtable
[T_DEFINE
])
338 bool no_expand
= (pfile
->directive
339 && ! (pfile
->directive
->flags
& EXPAND
));
340 bool was_skipping
= pfile
->state
.skipping
;
342 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
343 || pfile
->directive
== &dtable
[T_ELIF
]);
344 if (pfile
->state
.in_expression
)
345 pfile
->state
.skipping
= false;
348 pfile
->state
.prevent_expansion
++;
349 _cpp_scan_out_logical_line (pfile
, NULL
, false);
351 pfile
->state
.prevent_expansion
--;
353 pfile
->state
.skipping
= was_skipping
;
354 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
355 pfile
->out
.cur
- pfile
->out
.base
);
358 /* Stop ISO C from expanding anything. */
359 pfile
->state
.prevent_expansion
++;
362 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
363 the '#' was indented. */
365 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
367 /* Issue -pedantic or deprecated warnings for extensions. We let
368 -pedantic take precedence if both are applicable. */
369 if (! pfile
->state
.skipping
)
371 if (dir
->origin
== EXTENSION
372 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
373 && CPP_PEDANTIC (pfile
))
374 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
375 else if (((dir
->flags
& DEPRECATED
) != 0
376 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
377 && CPP_OPTION (pfile
, cpp_warn_deprecated
))
378 cpp_warning (pfile
, CPP_W_DEPRECATED
,
379 "#%s is a deprecated GCC extension", dir
->name
);
382 /* Traditionally, a directive is ignored unless its # is in
383 column 1. Therefore in code intended to work with K+R
384 compilers, directives added by C89 must have their #
385 indented, and directives present in traditional C must not.
386 This is true even of directives in skipped conditional
387 blocks. #elif cannot be used at all. */
388 if (CPP_WTRADITIONAL (pfile
))
390 if (dir
== &dtable
[T_ELIF
])
391 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
392 "suggest not using #elif in traditional C");
393 else if (indented
&& dir
->origin
== KANDR
)
394 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
395 "traditional C ignores #%s with the # indented",
397 else if (!indented
&& dir
->origin
!= KANDR
)
398 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
399 "suggest hiding #%s from traditional C with an indented #",
404 /* Check if we have a known directive. INDENTED is nonzero if the
405 '#' of the directive was indented. This function is in this file
406 to save unnecessarily exporting dtable etc. to lex.c. Returns
407 nonzero if the line of tokens has been handled, zero if we should
408 continue processing the line. */
410 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
412 const directive
*dir
= 0;
413 const cpp_token
*dname
;
414 bool was_parsing_args
= pfile
->state
.parsing_args
;
415 bool was_discarding_output
= pfile
->state
.discarding_output
;
418 if (was_discarding_output
)
419 pfile
->state
.prevent_expansion
= 0;
421 if (was_parsing_args
)
423 if (CPP_OPTION (pfile
, cpp_pedantic
))
424 cpp_error (pfile
, CPP_DL_PEDWARN
,
425 "embedding a directive within macro arguments is not portable");
426 pfile
->state
.parsing_args
= 0;
427 pfile
->state
.prevent_expansion
= 0;
429 start_directive (pfile
);
430 dname
= _cpp_lex_token (pfile
);
432 if (dname
->type
== CPP_NAME
)
434 if (dname
->val
.node
.node
->is_directive
)
435 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
437 /* We do not recognize the # followed by a number extension in
439 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
441 dir
= &linemarker_dir
;
442 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
443 && ! pfile
->state
.skipping
)
444 cpp_error (pfile
, CPP_DL_PEDWARN
,
445 "style of line directive is a GCC extension");
450 /* If we have a directive that is not an opening conditional,
451 invalidate any control macro. */
452 if (! (dir
->flags
& IF_COND
))
453 pfile
->mi_valid
= false;
455 /* Kluge alert. In order to be sure that code like this
460 does not cause '#define foo bar' to get executed when
461 compiled with -save-temps, we recognize directives in
462 -fpreprocessed mode only if the # is in column 1. macro.c
463 puts a space in front of any '#' at the start of a macro.
465 We exclude the -fdirectives-only case because macro expansion
466 has not been performed yet, and block comments can cause spaces
467 to precede the directive. */
468 if (CPP_OPTION (pfile
, preprocessed
)
469 && !CPP_OPTION (pfile
, directives_only
)
470 && (indented
|| !(dir
->flags
& IN_I
)))
477 /* In failed conditional groups, all non-conditional
478 directives are ignored. Before doing that, whether
479 skipping or not, we should lex angle-bracketed headers
480 correctly, and maybe output some diagnostics. */
481 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
482 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
483 if (! CPP_OPTION (pfile
, preprocessed
))
484 directive_diagnostics (pfile
, dir
, indented
);
485 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
489 else if (dname
->type
== CPP_EOF
)
490 ; /* CPP_EOF is the "null directive". */
493 /* An unknown directive. Don't complain about it in assembly
494 source: we don't know where the comments are, and # may
495 introduce assembler pseudo-ops. Don't complain about invalid
496 directives in skipped conditional groups (6.10 p4). */
497 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
499 else if (!pfile
->state
.skipping
)
500 cpp_error (pfile
, CPP_DL_ERROR
, "invalid preprocessing directive #%s",
501 cpp_token_as_text (pfile
, dname
));
504 pfile
->directive
= dir
;
505 if (CPP_OPTION (pfile
, traditional
))
506 prepare_directive_trad (pfile
);
509 pfile
->directive
->handler (pfile
);
511 _cpp_backup_tokens (pfile
, 1);
513 end_directive (pfile
, skip
);
514 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
516 /* Restore state when within macro args. */
517 pfile
->state
.parsing_args
= 2;
518 pfile
->state
.prevent_expansion
= 1;
520 if (was_discarding_output
)
521 pfile
->state
.prevent_expansion
= 1;
525 /* Directive handler wrapper used by the command line option
526 processor. BUF is \n terminated. */
528 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
530 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
531 /* from_stage3 */ true);
532 start_directive (pfile
);
534 /* This is a short-term fix to prevent a leading '#' being
535 interpreted as a directive. */
536 _cpp_clean_line (pfile
);
538 pfile
->directive
= &dtable
[dir_no
];
539 if (CPP_OPTION (pfile
, traditional
))
540 prepare_directive_trad (pfile
);
541 pfile
->directive
->handler (pfile
);
542 end_directive (pfile
, 1);
543 _cpp_pop_buffer (pfile
);
546 /* Checks for validity the macro name in #define, #undef, #ifdef and
547 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
548 processing a #define or #undefine directive, and false
550 static cpp_hashnode
*
551 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
553 const cpp_token
*token
= _cpp_lex_token (pfile
);
555 /* The token immediately after #define must be an identifier. That
556 identifier may not be "defined", per C99 6.10.8p4.
557 In C++, it may not be any of the "named operators" either,
558 per C++98 [lex.digraph], [lex.key].
559 Finally, the identifier may not have been poisoned. (In that case
560 the lexer has issued the error message for us.) */
562 if (token
->type
== CPP_NAME
)
564 cpp_hashnode
*node
= token
->val
.node
.node
;
566 if (is_def_or_undef
&& node
== pfile
->spec_nodes
.n_defined
)
567 cpp_error (pfile
, CPP_DL_ERROR
,
568 "\"defined\" cannot be used as a macro name");
569 else if (is_def_or_undef
570 && (node
== pfile
->spec_nodes
.n__has_include__
571 || node
== pfile
->spec_nodes
.n__has_include_next__
))
572 cpp_error (pfile
, CPP_DL_ERROR
,
573 "\"__has_include__\" cannot be used as a macro name");
574 else if (! (node
->flags
& NODE_POISONED
))
577 else if (token
->flags
& NAMED_OP
)
578 cpp_error (pfile
, CPP_DL_ERROR
,
579 "\"%s\" cannot be used as a macro name as it is an operator in C++",
580 NODE_NAME (token
->val
.node
.node
));
581 else if (token
->type
== CPP_EOF
)
582 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
583 pfile
->directive
->name
);
585 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
590 /* Process a #define directive. Most work is done in macro.c. */
592 do_define (cpp_reader
*pfile
)
594 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
598 /* If we have been requested to expand comments into macros,
599 then re-enable saving of comments. */
600 pfile
->state
.save_comments
=
601 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
603 if (pfile
->cb
.before_define
)
604 pfile
->cb
.before_define (pfile
);
606 if (_cpp_create_definition (pfile
, node
))
607 if (pfile
->cb
.define
)
608 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
610 node
->flags
&= ~NODE_USED
;
614 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
616 do_undef (cpp_reader
*pfile
)
618 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
622 if (pfile
->cb
.before_define
)
623 pfile
->cb
.before_define (pfile
);
626 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
628 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
629 identifier is not currently defined as a macro name. */
630 if (node
->type
== NT_MACRO
)
632 if (node
->flags
& NODE_WARN
)
633 cpp_error (pfile
, CPP_DL_WARNING
,
634 "undefining \"%s\"", NODE_NAME (node
));
635 else if ((node
->flags
& NODE_BUILTIN
)
636 && CPP_OPTION (pfile
, warn_builtin_macro_redefined
))
637 cpp_warning_with_line (pfile
, CPP_W_BUILTIN_MACRO_REDEFINED
,
638 pfile
->directive_line
, 0,
639 "undefining \"%s\"", NODE_NAME (node
));
641 if (CPP_OPTION (pfile
, warn_unused_macros
))
642 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
644 _cpp_free_definition (node
);
648 check_eol (pfile
, false);
651 /* Undefine a single macro/assertion/whatever. */
654 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
655 void *data_p ATTRIBUTE_UNUSED
)
657 /* Body of _cpp_free_definition inlined here for speed.
658 Macros and assertions no longer have anything to free. */
660 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
|NODE_USED
);
664 /* Undefine all macros and assertions. */
667 cpp_undef_all (cpp_reader
*pfile
)
669 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
673 /* Helper routine used by parse_include. Reinterpret the current line
674 as an h-char-sequence (< ... >); we are looking at the first token
675 after the <. Returns a malloced filename. */
677 glue_header_name (cpp_reader
*pfile
)
679 const cpp_token
*token
;
681 size_t len
, total_len
= 0, capacity
= 1024;
683 /* To avoid lexed tokens overwriting our glued name, we can only
684 allocate from the string pool once we've lexed everything. */
685 buffer
= XNEWVEC (char, capacity
);
688 token
= get_token_no_padding (pfile
);
690 if (token
->type
== CPP_GREATER
)
692 if (token
->type
== CPP_EOF
)
694 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
698 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
699 if (total_len
+ len
> capacity
)
701 capacity
= (capacity
+ len
) * 2;
702 buffer
= XRESIZEVEC (char, buffer
, capacity
);
705 if (token
->flags
& PREV_WHITE
)
706 buffer
[total_len
++] = ' ';
708 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
713 buffer
[total_len
] = '\0';
717 /* Returns the file name of #include, #include_next, #import and
718 #pragma dependency. The string is malloced and the caller should
719 free it. Returns NULL on error. LOCATION is the source location
723 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
724 const cpp_token
***buf
, source_location
*location
)
727 const cpp_token
*header
;
729 /* Allow macro expansion. */
730 header
= get_token_no_padding (pfile
);
731 *location
= header
->src_loc
;
732 if ((header
->type
== CPP_STRING
&& header
->val
.str
.text
[0] != 'R')
733 || header
->type
== CPP_HEADER_NAME
)
735 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
736 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
737 fname
[header
->val
.str
.len
- 2] = '\0';
738 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
740 else if (header
->type
== CPP_LESS
)
742 fname
= glue_header_name (pfile
);
743 *pangle_brackets
= 1;
747 const unsigned char *dir
;
749 if (pfile
->directive
== &dtable
[T_PRAGMA
])
750 dir
= UC
"pragma dependency";
752 dir
= pfile
->directive
->name
;
753 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
759 if (pfile
->directive
== &dtable
[T_PRAGMA
])
761 /* This pragma allows extra tokens after the file name. */
763 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
764 check_eol (pfile
, true);
767 /* If we are not discarding comments, then gather them while
768 doing the eol check. */
769 *buf
= check_eol_return_comments (pfile
);
775 /* Handle #include, #include_next and #import. */
777 do_include_common (cpp_reader
*pfile
, enum include_type type
)
781 const cpp_token
**buf
= NULL
;
782 source_location location
;
784 /* Re-enable saving of comments if requested, so that the include
785 callback can dump comments which follow #include. */
786 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
788 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
798 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
799 "empty filename in #%s",
800 pfile
->directive
->name
);
807 /* Prevent #include recursion. */
808 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
809 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
812 /* Get out of macro context, if we are. */
813 skip_rest_of_line (pfile
);
815 if (pfile
->cb
.include
)
816 pfile
->cb
.include (pfile
, pfile
->directive_line
,
817 pfile
->directive
->name
, fname
, angle_brackets
,
820 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
);
829 do_include (cpp_reader
*pfile
)
831 do_include_common (pfile
, IT_INCLUDE
);
835 do_import (cpp_reader
*pfile
)
837 do_include_common (pfile
, IT_IMPORT
);
841 do_include_next (cpp_reader
*pfile
)
843 enum include_type type
= IT_INCLUDE_NEXT
;
845 /* If this is the primary source file, warn and use the normal
847 if (cpp_in_primary_file (pfile
))
849 cpp_error (pfile
, CPP_DL_WARNING
,
850 "#include_next in primary source file");
853 do_include_common (pfile
, type
);
856 /* Subroutine of do_linemarker. Read possible flags after file name.
857 LAST is the last flag seen; 0 if this is the first flag. Return the
858 flag if it is valid, 0 at the end of the directive. Otherwise
861 read_flag (cpp_reader
*pfile
, unsigned int last
)
863 const cpp_token
*token
= _cpp_lex_token (pfile
);
865 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
867 unsigned int flag
= token
->val
.str
.text
[0] - '0';
869 if (flag
> last
&& flag
<= 4
870 && (flag
!= 4 || last
== 3)
871 && (flag
!= 2 || last
== 0))
875 if (token
->type
!= CPP_EOF
)
876 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
877 cpp_token_as_text (pfile
, token
));
881 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
882 of length LEN, to binary; store it in NUMP, and return false if the
883 number was well-formed, true if not. WRAPPED is set to true if the
884 number did not fit into 'unsigned long'. */
886 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
888 linenum_type reg
= 0;
889 linenum_type reg_prev
= 0;
908 /* Interpret #line command.
909 Note that the filename string (if any) is a true string constant
910 (escapes are interpreted), unlike in #line. */
912 do_line (cpp_reader
*pfile
)
914 struct line_maps
*line_table
= pfile
->line_table
;
915 const struct line_map
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
917 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
920 unsigned char map_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
921 const cpp_token
*token
;
922 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
923 linenum_type new_lineno
;
925 /* C99 raised the minimum limit on #line numbers. */
926 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
929 /* #line commands expand macros. */
930 token
= cpp_get_token (pfile
);
931 if (token
->type
!= CPP_NUMBER
932 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
933 &new_lineno
, &wrapped
))
935 if (token
->type
== CPP_EOF
)
936 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
938 cpp_error (pfile
, CPP_DL_ERROR
,
939 "\"%s\" after #line is not a positive integer",
940 cpp_token_as_text (pfile
, token
));
944 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
945 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
947 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
949 token
= cpp_get_token (pfile
);
950 if (token
->type
== CPP_STRING
)
952 cpp_string s
= { 0, 0 };
953 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
955 new_file
= (const char *)s
.text
;
956 check_eol (pfile
, true);
958 else if (token
->type
!= CPP_EOF
)
960 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
961 cpp_token_as_text (pfile
, token
));
965 skip_rest_of_line (pfile
);
966 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
968 line_table
->seen_line_directive
= true;
971 /* Interpret the # 44 "file" [flags] notation, which has slightly
972 different syntax and semantics from #line: Flags are allowed,
973 and we never complain about the line number being too big. */
975 do_linemarker (cpp_reader
*pfile
)
977 struct line_maps
*line_table
= pfile
->line_table
;
978 const struct line_map
*map
= LINEMAPS_LAST_ORDINARY_MAP (line_table
);
979 const cpp_token
*token
;
980 const char *new_file
= ORDINARY_MAP_FILE_NAME (map
);
981 linenum_type new_lineno
;
982 unsigned int new_sysp
= ORDINARY_MAP_IN_SYSTEM_HEADER_P (map
);
983 enum lc_reason reason
= LC_RENAME_VERBATIM
;
987 /* Back up so we can get the number again. Putting this in
988 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
989 some circumstances, which can segfault. */
990 _cpp_backup_tokens (pfile
, 1);
992 /* #line commands expand macros. */
993 token
= cpp_get_token (pfile
);
994 if (token
->type
!= CPP_NUMBER
995 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
996 &new_lineno
, &wrapped
))
998 /* Unlike #line, there does not seem to be a way to get an EOF
999 here. So, it should be safe to always spell the token. */
1000 cpp_error (pfile
, CPP_DL_ERROR
,
1001 "\"%s\" after # is not a positive integer",
1002 cpp_token_as_text (pfile
, token
));
1006 token
= cpp_get_token (pfile
);
1007 if (token
->type
== CPP_STRING
)
1009 cpp_string s
= { 0, 0 };
1010 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
1012 new_file
= (const char *)s
.text
;
1015 flag
= read_flag (pfile
, 0);
1019 /* Fake an include for cpp_included (). */
1020 _cpp_fake_include (pfile
, new_file
);
1021 flag
= read_flag (pfile
, flag
);
1026 flag
= read_flag (pfile
, flag
);
1031 flag
= read_flag (pfile
, flag
);
1035 pfile
->buffer
->sysp
= new_sysp
;
1037 check_eol (pfile
, false);
1039 else if (token
->type
!= CPP_EOF
)
1041 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1042 cpp_token_as_text (pfile
, token
));
1046 skip_rest_of_line (pfile
);
1048 /* Compensate for the increment in linemap_add that occurs in
1049 _cpp_do_file_change. We're currently at the start of the line
1050 *following* the #line directive. A separate source_location for this
1051 location makes no sense (until we do the LC_LEAVE), and
1052 complicates LAST_SOURCE_LINE_LOCATION. */
1053 pfile
->line_table
->highest_location
--;
1055 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1056 line_table
->seen_line_directive
= true;
1059 /* Arrange the file_change callback. pfile->line has changed to
1060 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1061 header, 2 for a system header that needs to be extern "C" protected,
1062 and zero otherwise. */
1064 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1065 const char *to_file
, linenum_type file_line
,
1068 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1069 to_file
, file_line
);
1071 linemap_line_start (pfile
->line_table
,
1072 ORDINARY_MAP_STARTING_LINE_NUMBER (map
),
1075 if (pfile
->cb
.file_change
)
1076 pfile
->cb
.file_change (pfile
, map
);
1079 /* Report a warning or error detected by the program we are
1080 processing. Use the directive's tokens in the error message. */
1082 do_diagnostic (cpp_reader
*pfile
, int code
, int reason
, int print_dir
)
1084 const unsigned char *dir_name
;
1085 unsigned char *line
;
1086 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1089 dir_name
= pfile
->directive
->name
;
1092 pfile
->state
.prevent_expansion
++;
1093 line
= cpp_output_line_to_string (pfile
, dir_name
);
1094 pfile
->state
.prevent_expansion
--;
1096 if (code
== CPP_DL_WARNING_SYSHDR
&& reason
)
1097 cpp_warning_with_line_syshdr (pfile
, reason
, src_loc
, 0, "%s", line
);
1098 else if (code
== CPP_DL_WARNING
&& reason
)
1099 cpp_warning_with_line (pfile
, reason
, src_loc
, 0, "%s", line
);
1101 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1106 do_error (cpp_reader
*pfile
)
1108 do_diagnostic (pfile
, CPP_DL_ERROR
, 0, 1);
1112 do_warning (cpp_reader
*pfile
)
1114 /* We want #warning diagnostics to be emitted in system headers too. */
1115 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, CPP_W_WARNING_DIRECTIVE
, 1);
1118 /* Report program identification. */
1120 do_ident (cpp_reader
*pfile
)
1122 const cpp_token
*str
= cpp_get_token (pfile
);
1124 if (str
->type
!= CPP_STRING
)
1125 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1126 pfile
->directive
->name
);
1127 else if (pfile
->cb
.ident
)
1128 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1130 check_eol (pfile
, false);
1133 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1134 matching entry, or NULL if none is found. The returned entry could
1135 be the start of a namespace chain, or a pragma. */
1136 static struct pragma_entry
*
1137 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1139 while (chain
&& chain
->pragma
!= pragma
)
1140 chain
= chain
->next
;
1145 /* Create and insert a blank pragma entry at the beginning of a
1146 singly-linked CHAIN. */
1147 static struct pragma_entry
*
1148 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1150 struct pragma_entry
*new_entry
;
1152 new_entry
= (struct pragma_entry
*)
1153 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1155 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1156 new_entry
->next
= *chain
;
1162 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1163 goes in the global namespace. */
1164 static struct pragma_entry
*
1165 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1166 bool allow_name_expansion
)
1168 struct pragma_entry
**chain
= &pfile
->pragmas
;
1169 struct pragma_entry
*entry
;
1170 const cpp_hashnode
*node
;
1174 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1175 entry
= lookup_pragma_entry (*chain
, node
);
1178 entry
= new_pragma_entry (pfile
, chain
);
1179 entry
->pragma
= node
;
1180 entry
->is_nspace
= true;
1181 entry
->allow_expansion
= allow_name_expansion
;
1183 else if (!entry
->is_nspace
)
1185 else if (entry
->allow_expansion
!= allow_name_expansion
)
1187 cpp_error (pfile
, CPP_DL_ICE
,
1188 "registering pragmas in namespace \"%s\" with mismatched "
1189 "name expansion", space
);
1192 chain
= &entry
->u
.space
;
1194 else if (allow_name_expansion
)
1196 cpp_error (pfile
, CPP_DL_ICE
,
1197 "registering pragma \"%s\" with name expansion "
1198 "and no namespace", name
);
1202 /* Check for duplicates. */
1203 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1204 entry
= lookup_pragma_entry (*chain
, node
);
1207 entry
= new_pragma_entry (pfile
, chain
);
1208 entry
->pragma
= node
;
1212 if (entry
->is_nspace
)
1214 cpp_error (pfile
, CPP_DL_ICE
,
1215 "registering \"%s\" as both a pragma and a pragma namespace",
1218 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1221 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1226 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1228 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1229 const char *name
, pragma_cb handler
)
1231 struct pragma_entry
*entry
;
1233 entry
= register_pragma_1 (pfile
, space
, name
, false);
1234 entry
->is_internal
= true;
1235 entry
->u
.handler
= handler
;
1238 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1239 goes in the global namespace. HANDLER is the handler it will call,
1240 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1241 expansion while parsing pragma NAME. This function is exported
1244 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1245 pragma_cb handler
, bool allow_expansion
)
1247 struct pragma_entry
*entry
;
1251 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1255 entry
= register_pragma_1 (pfile
, space
, name
, false);
1258 entry
->allow_expansion
= allow_expansion
;
1259 entry
->u
.handler
= handler
;
1263 /* Similarly, but create mark the pragma for deferred processing.
1264 When found, a CPP_PRAGMA token will be insertted into the stream
1265 with IDENT in the token->u.pragma slot. */
1267 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1268 const char *name
, unsigned int ident
,
1269 bool allow_expansion
, bool allow_name_expansion
)
1271 struct pragma_entry
*entry
;
1273 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1276 entry
->is_deferred
= true;
1277 entry
->allow_expansion
= allow_expansion
;
1278 entry
->u
.ident
= ident
;
1282 /* Register the pragmas the preprocessor itself handles. */
1284 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1286 /* Pragmas in the global namespace. */
1287 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1288 register_pragma_internal (pfile
, 0, "push_macro", do_pragma_push_macro
);
1289 register_pragma_internal (pfile
, 0, "pop_macro", do_pragma_pop_macro
);
1291 /* New GCC-specific pragmas should be put in the GCC namespace. */
1292 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1293 register_pragma_internal (pfile
, "GCC", "system_header",
1294 do_pragma_system_header
);
1295 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1296 register_pragma_internal (pfile
, "GCC", "warning", do_pragma_warning
);
1297 register_pragma_internal (pfile
, "GCC", "error", do_pragma_error
);
1300 /* Return the number of registered pragmas in PE. */
1303 count_registered_pragmas (struct pragma_entry
*pe
)
1306 for (; pe
!= NULL
; pe
= pe
->next
)
1309 ct
+= count_registered_pragmas (pe
->u
.space
);
1315 /* Save into SD the names of the registered pragmas referenced by PE,
1316 and return a pointer to the next free space in SD. */
1319 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1321 for (; pe
!= NULL
; pe
= pe
->next
)
1324 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1325 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1326 HT_LEN (&pe
->pragma
->ident
),
1327 HT_LEN (&pe
->pragma
->ident
) + 1);
1332 /* Return a newly-allocated array which saves the names of the
1333 registered pragmas. */
1336 _cpp_save_pragma_names (cpp_reader
*pfile
)
1338 int ct
= count_registered_pragmas (pfile
->pragmas
);
1339 char **result
= XNEWVEC (char *, ct
);
1340 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1344 /* Restore from SD the names of the registered pragmas referenced by PE,
1345 and return a pointer to the next unused name in SD. */
1348 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1351 for (; pe
!= NULL
; pe
= pe
->next
)
1354 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1355 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1362 /* Restore the names of the registered pragmas from SAVED. */
1365 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1367 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1371 /* Pragmata handling. We handle some, and pass the rest on to the
1372 front end. C99 defines three pragmas and says that no macro
1373 expansion is to be performed on them; whether or not macro
1374 expansion happens for other pragmas is implementation defined.
1375 This implementation allows for a mix of both, since GCC did not
1376 traditionally macro expand its (few) pragmas, whereas OpenMP
1377 specifies that macro expansion should happen. */
1379 do_pragma (cpp_reader
*pfile
)
1381 const struct pragma_entry
*p
= NULL
;
1382 const cpp_token
*token
, *pragma_token
;
1383 source_location pragma_token_virt_loc
= 0;
1385 unsigned int count
= 1;
1387 pfile
->state
.prevent_expansion
++;
1389 pragma_token
= token
= cpp_get_token_with_location (pfile
,
1390 &pragma_token_virt_loc
);
1392 if (token
->type
== CPP_NAME
)
1394 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1395 if (p
&& p
->is_nspace
)
1397 bool allow_name_expansion
= p
->allow_expansion
;
1398 if (allow_name_expansion
)
1399 pfile
->state
.prevent_expansion
--;
1401 token
= cpp_get_token (pfile
);
1402 if (token
->type
== CPP_NAME
)
1403 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1406 if (allow_name_expansion
)
1407 pfile
->state
.prevent_expansion
++;
1416 pfile
->directive_result
.src_loc
= pragma_token_virt_loc
;
1417 pfile
->directive_result
.type
= CPP_PRAGMA
;
1418 pfile
->directive_result
.flags
= pragma_token
->flags
;
1419 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1420 pfile
->state
.in_deferred_pragma
= true;
1421 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1422 if (!p
->allow_expansion
)
1423 pfile
->state
.prevent_expansion
++;
1427 /* Since the handler below doesn't get the line number, that
1428 it might need for diagnostics, make sure it has the right
1429 numbers in place. */
1430 if (pfile
->cb
.line_change
)
1431 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1432 if (p
->allow_expansion
)
1433 pfile
->state
.prevent_expansion
--;
1434 (*p
->u
.handler
) (pfile
);
1435 if (p
->allow_expansion
)
1436 pfile
->state
.prevent_expansion
++;
1439 else if (pfile
->cb
.def_pragma
)
1441 if (count
== 1 || pfile
->context
->prev
== NULL
)
1442 _cpp_backup_tokens (pfile
, count
);
1445 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1446 won't allow backing 2 tokens. */
1447 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1448 reads both tokens, we could perhaps free it, but if it doesn't,
1449 we don't know the exact lifespan. */
1450 cpp_token
*toks
= XNEWVEC (cpp_token
, 2);
1452 toks
[0].flags
|= NO_EXPAND
;
1454 toks
[1].flags
|= NO_EXPAND
;
1455 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1457 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1460 pfile
->state
.prevent_expansion
--;
1463 /* Handle #pragma once. */
1465 do_pragma_once (cpp_reader
*pfile
)
1467 if (cpp_in_primary_file (pfile
))
1468 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1470 check_eol (pfile
, false);
1471 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1474 /* Handle #pragma push_macro(STRING). */
1476 do_pragma_push_macro (cpp_reader
*pfile
)
1480 const uchar
*defn
= NULL
;
1481 char *macroname
, *dest
;
1482 const char *limit
, *src
;
1483 const cpp_token
*txt
;
1484 struct def_pragma_macro
*c
;
1486 txt
= get__Pragma_string (pfile
);
1489 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1490 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1491 "invalid #pragma push_macro directive");
1492 check_eol (pfile
, false);
1493 skip_rest_of_line (pfile
);
1496 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1497 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1498 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1501 /* We know there is a character following the backslash. */
1502 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1507 check_eol (pfile
, false);
1508 skip_rest_of_line (pfile
);
1509 c
= XNEW (struct def_pragma_macro
);
1510 memset (c
, 0, sizeof (struct def_pragma_macro
));
1511 c
->name
= XNEWVAR (char, strlen (macroname
) + 1);
1512 strcpy (c
->name
, macroname
);
1513 c
->next
= pfile
->pushed_macros
;
1514 node
= _cpp_lex_identifier (pfile
, c
->name
);
1515 if (node
->type
== NT_VOID
)
1519 defn
= cpp_macro_definition (pfile
, node
);
1520 defnlen
= ustrlen (defn
);
1521 c
->definition
= XNEWVEC (uchar
, defnlen
+ 2);
1522 c
->definition
[defnlen
] = '\n';
1523 c
->definition
[defnlen
+ 1] = 0;
1524 c
->line
= node
->value
.macro
->line
;
1525 c
->syshdr
= node
->value
.macro
->syshdr
;
1526 c
->used
= node
->value
.macro
->used
;
1527 memcpy (c
->definition
, defn
, defnlen
);
1530 pfile
->pushed_macros
= c
;
1533 /* Handle #pragma pop_macro(STRING). */
1535 do_pragma_pop_macro (cpp_reader
*pfile
)
1537 char *macroname
, *dest
;
1538 const char *limit
, *src
;
1539 const cpp_token
*txt
;
1540 struct def_pragma_macro
*l
= NULL
, *c
= pfile
->pushed_macros
;
1541 txt
= get__Pragma_string (pfile
);
1544 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1545 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1546 "invalid #pragma pop_macro directive");
1547 check_eol (pfile
, false);
1548 skip_rest_of_line (pfile
);
1551 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1552 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1553 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1556 /* We know there is a character following the backslash. */
1557 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1562 check_eol (pfile
, false);
1563 skip_rest_of_line (pfile
);
1567 if (!strcmp (c
->name
, macroname
))
1570 pfile
->pushed_macros
= c
->next
;
1573 cpp_pop_definition (pfile
, c
);
1574 free (c
->definition
);
1584 /* Handle #pragma GCC poison, to poison one or more identifiers so
1585 that the lexer produces a hard error for each subsequent usage. */
1587 do_pragma_poison (cpp_reader
*pfile
)
1589 const cpp_token
*tok
;
1592 pfile
->state
.poisoned_ok
= 1;
1595 tok
= _cpp_lex_token (pfile
);
1596 if (tok
->type
== CPP_EOF
)
1598 if (tok
->type
!= CPP_NAME
)
1600 cpp_error (pfile
, CPP_DL_ERROR
,
1601 "invalid #pragma GCC poison directive");
1605 hp
= tok
->val
.node
.node
;
1606 if (hp
->flags
& NODE_POISONED
)
1609 if (hp
->type
== NT_MACRO
)
1610 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1612 _cpp_free_definition (hp
);
1613 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1615 pfile
->state
.poisoned_ok
= 0;
1618 /* Mark the current header as a system header. This will suppress
1619 some categories of warnings (notably those from -pedantic). It is
1620 intended for use in system libraries that cannot be implemented in
1621 conforming C, but cannot be certain that their headers appear in a
1622 system include directory. To prevent abuse, it is rejected in the
1623 primary source file. */
1625 do_pragma_system_header (cpp_reader
*pfile
)
1627 if (cpp_in_primary_file (pfile
))
1628 cpp_error (pfile
, CPP_DL_WARNING
,
1629 "#pragma system_header ignored outside include file");
1632 check_eol (pfile
, false);
1633 skip_rest_of_line (pfile
);
1634 cpp_make_system_header (pfile
, 1, 0);
1638 /* Check the modified date of the current include file against a specified
1639 file. Issue a diagnostic, if the specified file is newer. We use this to
1640 determine if a fixed header should be refixed. */
1642 do_pragma_dependency (cpp_reader
*pfile
)
1645 int angle_brackets
, ordering
;
1646 source_location location
;
1648 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
1652 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1654 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1655 else if (ordering
> 0)
1657 cpp_error (pfile
, CPP_DL_WARNING
,
1658 "current file is older than %s", fname
);
1659 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1661 _cpp_backup_tokens (pfile
, 1);
1662 do_diagnostic (pfile
, CPP_DL_WARNING
, 0, 0);
1666 free ((void *) fname
);
1669 /* Issue a diagnostic with the message taken from the pragma. If
1670 ERROR is true, the diagnostic is a warning, otherwise, it is an
1673 do_pragma_warning_or_error (cpp_reader
*pfile
, bool error
)
1675 const cpp_token
*tok
= _cpp_lex_token (pfile
);
1677 if (tok
->type
!= CPP_STRING
1678 || !cpp_interpret_string_notranslate (pfile
, &tok
->val
.str
, 1, &str
,
1682 cpp_error (pfile
, CPP_DL_ERROR
, "invalid \"#pragma GCC %s\" directive",
1683 error
? "error" : "warning");
1686 cpp_error (pfile
, error
? CPP_DL_ERROR
: CPP_DL_WARNING
,
1688 free ((void *)str
.text
);
1691 /* Issue a warning diagnostic. */
1693 do_pragma_warning (cpp_reader
*pfile
)
1695 do_pragma_warning_or_error (pfile
, false);
1698 /* Issue an error diagnostic. */
1700 do_pragma_error (cpp_reader
*pfile
)
1702 do_pragma_warning_or_error (pfile
, true);
1705 /* Get a token but skip padding. */
1706 static const cpp_token
*
1707 get_token_no_padding (cpp_reader
*pfile
)
1711 const cpp_token
*result
= cpp_get_token (pfile
);
1712 if (result
->type
!= CPP_PADDING
)
1717 /* Check syntax is "(string-literal)". Returns the string on success,
1718 or NULL on failure. */
1719 static const cpp_token
*
1720 get__Pragma_string (cpp_reader
*pfile
)
1722 const cpp_token
*string
;
1723 const cpp_token
*paren
;
1725 paren
= get_token_no_padding (pfile
);
1726 if (paren
->type
== CPP_EOF
)
1727 _cpp_backup_tokens (pfile
, 1);
1728 if (paren
->type
!= CPP_OPEN_PAREN
)
1731 string
= get_token_no_padding (pfile
);
1732 if (string
->type
== CPP_EOF
)
1733 _cpp_backup_tokens (pfile
, 1);
1734 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1735 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
1736 && string
->type
!= CPP_UTF8STRING
)
1739 paren
= get_token_no_padding (pfile
);
1740 if (paren
->type
== CPP_EOF
)
1741 _cpp_backup_tokens (pfile
, 1);
1742 if (paren
->type
!= CPP_CLOSE_PAREN
)
1748 /* Destringize IN into a temporary buffer, by removing the first \ of
1749 \" and \\ sequences, and process the result as a #pragma directive. */
1751 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
)
1753 const unsigned char *src
, *limit
;
1754 char *dest
, *result
;
1755 cpp_context
*saved_context
;
1756 cpp_token
*saved_cur_token
;
1757 tokenrun
*saved_cur_run
;
1760 const struct directive
*save_directive
;
1762 dest
= result
= (char *) alloca (in
->len
- 1);
1763 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1764 limit
= in
->text
+ in
->len
- 1;
1767 /* We know there is a character following the backslash. */
1768 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1774 /* Ugh; an awful kludge. We are really not set up to be lexing
1775 tokens when in the middle of a macro expansion. Use a new
1776 context to force cpp_get_token to lex, and so skip_rest_of_line
1777 doesn't go beyond the end of the text. Also, remember the
1778 current lexing position so we can return to it later.
1780 Something like line-at-a-time lexing should remove the need for
1782 saved_context
= pfile
->context
;
1783 saved_cur_token
= pfile
->cur_token
;
1784 saved_cur_run
= pfile
->cur_run
;
1786 pfile
->context
= XCNEW (cpp_context
);
1788 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1789 until we've read all of the tokens that we want. */
1790 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1791 /* from_stage3 */ true);
1792 /* ??? Antique Disgusting Hack. What does this do? */
1793 if (pfile
->buffer
->prev
)
1794 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1796 start_directive (pfile
);
1797 _cpp_clean_line (pfile
);
1798 save_directive
= pfile
->directive
;
1799 pfile
->directive
= &dtable
[T_PRAGMA
];
1801 end_directive (pfile
, 1);
1802 pfile
->directive
= save_directive
;
1804 /* We always insert at least one token, the directive result. It'll
1805 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1806 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1808 /* If we're not handling the pragma internally, read all of the tokens from
1809 the string buffer now, while the string buffer is still installed. */
1810 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1811 to me what the true lifespan of the tokens are. It would appear that
1812 the lifespan is the entire parse of the main input stream, in which case
1813 this may not be wrong. */
1814 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1820 toks
= XNEWVEC (cpp_token
, maxcount
);
1821 toks
[0] = pfile
->directive_result
;
1825 if (count
== maxcount
)
1827 maxcount
= maxcount
* 3 / 2;
1828 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1830 toks
[count
] = *cpp_get_token (pfile
);
1831 /* Macros have been already expanded by cpp_get_token
1832 if the pragma allowed expansion. */
1833 toks
[count
++].flags
|= NO_EXPAND
;
1835 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1840 toks
= XNEW (cpp_token
);
1841 toks
[0] = pfile
->directive_result
;
1843 /* If we handled the entire pragma internally, make sure we get the
1844 line number correct for the next token. */
1845 if (pfile
->cb
.line_change
)
1846 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1849 /* Finish inlining run_directive. */
1850 pfile
->buffer
->file
= NULL
;
1851 _cpp_pop_buffer (pfile
);
1853 /* Reset the old macro state before ... */
1854 XDELETE (pfile
->context
);
1855 pfile
->context
= saved_context
;
1856 pfile
->cur_token
= saved_cur_token
;
1857 pfile
->cur_run
= saved_cur_run
;
1859 /* ... inserting the new tokens we collected. */
1860 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1863 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1865 _cpp_do__Pragma (cpp_reader
*pfile
)
1867 const cpp_token
*string
= get__Pragma_string (pfile
);
1868 pfile
->directive_result
.type
= CPP_PADDING
;
1872 destringize_and_run (pfile
, &string
->val
.str
);
1875 cpp_error (pfile
, CPP_DL_ERROR
,
1876 "_Pragma takes a parenthesized string literal");
1880 /* Handle #ifdef. */
1882 do_ifdef (cpp_reader
*pfile
)
1886 if (! pfile
->state
.skipping
)
1888 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
1892 /* Do not treat conditional macros as being defined. This is due to
1893 the powerpc and spu ports using conditional macros for 'vector',
1894 'bool', and 'pixel' to act as conditional keywords. This messes
1895 up tests like #ifndef bool. */
1896 skip
= (node
->type
!= NT_MACRO
1897 || ((node
->flags
& NODE_CONDITIONAL
) != 0));
1898 _cpp_mark_macro_used (node
);
1899 if (!(node
->flags
& NODE_USED
))
1901 node
->flags
|= NODE_USED
;
1902 if (node
->type
== NT_MACRO
)
1904 if ((node
->flags
& NODE_BUILTIN
)
1905 && pfile
->cb
.user_builtin_macro
)
1906 pfile
->cb
.user_builtin_macro (pfile
, node
);
1907 if (pfile
->cb
.used_define
)
1908 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1912 if (pfile
->cb
.used_undef
)
1913 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1917 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1918 check_eol (pfile
, false);
1922 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1925 /* Handle #ifndef. */
1927 do_ifndef (cpp_reader
*pfile
)
1930 cpp_hashnode
*node
= 0;
1932 if (! pfile
->state
.skipping
)
1934 node
= lex_macro_node (pfile
, false);
1938 /* Do not treat conditional macros as being defined. This is due to
1939 the powerpc and spu ports using conditional macros for 'vector',
1940 'bool', and 'pixel' to act as conditional keywords. This messes
1941 up tests like #ifndef bool. */
1942 skip
= (node
->type
== NT_MACRO
1943 && ((node
->flags
& NODE_CONDITIONAL
) == 0));
1944 _cpp_mark_macro_used (node
);
1945 if (!(node
->flags
& NODE_USED
))
1947 node
->flags
|= NODE_USED
;
1948 if (node
->type
== NT_MACRO
)
1950 if ((node
->flags
& NODE_BUILTIN
)
1951 && pfile
->cb
.user_builtin_macro
)
1952 pfile
->cb
.user_builtin_macro (pfile
, node
);
1953 if (pfile
->cb
.used_define
)
1954 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1958 if (pfile
->cb
.used_undef
)
1959 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1963 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1964 check_eol (pfile
, false);
1968 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1971 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1972 pfile->mi_ind_cmacro so we can handle multiple-include
1973 optimizations. If macro expansion occurs in the expression, we
1974 cannot treat it as a controlling conditional, since the expansion
1975 could change in the future. That is handled by cpp_get_token. */
1977 do_if (cpp_reader
*pfile
)
1981 if (! pfile
->state
.skipping
)
1982 skip
= _cpp_parse_expr (pfile
, true) == false;
1984 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1987 /* Flip skipping state if appropriate and continue without changing
1988 if_stack; this is so that the error message for missing #endif's
1989 etc. will point to the original #if. */
1991 do_else (cpp_reader
*pfile
)
1993 cpp_buffer
*buffer
= pfile
->buffer
;
1994 struct if_stack
*ifs
= buffer
->if_stack
;
1997 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
2000 if (ifs
->type
== T_ELSE
)
2002 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
2003 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2004 "the conditional began here");
2008 /* Skip any future (erroneous) #elses or #elifs. */
2009 pfile
->state
.skipping
= ifs
->skip_elses
;
2010 ifs
->skip_elses
= true;
2012 /* Invalidate any controlling macro. */
2015 /* Only check EOL if was not originally skipping. */
2016 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2017 check_eol_endif_labels (pfile
);
2021 /* Handle a #elif directive by not changing if_stack either. See the
2022 comment above do_else. */
2024 do_elif (cpp_reader
*pfile
)
2026 cpp_buffer
*buffer
= pfile
->buffer
;
2027 struct if_stack
*ifs
= buffer
->if_stack
;
2030 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
2033 if (ifs
->type
== T_ELSE
)
2035 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
2036 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2037 "the conditional began here");
2041 /* See DR#412: "Only the first group whose control condition
2042 evaluates to true (nonzero) is processed; any following groups
2043 are skipped and their controlling directives are processed as
2044 if they were in a group that is skipped." */
2045 if (ifs
->skip_elses
)
2046 pfile
->state
.skipping
= 1;
2049 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
, false);
2050 ifs
->skip_elses
= ! pfile
->state
.skipping
;
2053 /* Invalidate any controlling macro. */
2058 /* #endif pops the if stack and resets pfile->state.skipping. */
2060 do_endif (cpp_reader
*pfile
)
2062 cpp_buffer
*buffer
= pfile
->buffer
;
2063 struct if_stack
*ifs
= buffer
->if_stack
;
2066 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
2069 /* Only check EOL if was not originally skipping. */
2070 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
2071 check_eol_endif_labels (pfile
);
2073 /* If potential control macro, we go back outside again. */
2074 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
2076 pfile
->mi_valid
= true;
2077 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
2080 buffer
->if_stack
= ifs
->next
;
2081 pfile
->state
.skipping
= ifs
->was_skipping
;
2082 obstack_free (&pfile
->buffer_ob
, ifs
);
2086 /* Push an if_stack entry for a preprocessor conditional, and set
2087 pfile->state.skipping to SKIP. If TYPE indicates the conditional
2088 is #if or #ifndef, CMACRO is a potentially controlling macro, and
2089 we need to check here that we are at the top of the file. */
2091 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
2092 const cpp_hashnode
*cmacro
)
2094 struct if_stack
*ifs
;
2095 cpp_buffer
*buffer
= pfile
->buffer
;
2097 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
2098 ifs
->line
= pfile
->directive_line
;
2099 ifs
->next
= buffer
->if_stack
;
2100 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
2101 ifs
->was_skipping
= pfile
->state
.skipping
;
2103 /* This condition is effectively a test for top-of-file. */
2104 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
2105 ifs
->mi_cmacro
= cmacro
;
2109 pfile
->state
.skipping
= skip
;
2110 buffer
->if_stack
= ifs
;
2113 /* Read the tokens of the answer into the macro pool, in a directive
2114 of type TYPE. Only commit the memory if we intend it as permanent
2115 storage, i.e. the #assert case. Returns 0 on success, and sets
2116 ANSWERP to point to the answer. PRED_LOC is the location of the
2119 parse_answer (cpp_reader
*pfile
, struct answer
**answerp
, int type
,
2120 source_location pred_loc
)
2122 const cpp_token
*paren
;
2123 struct answer
*answer
;
2124 unsigned int acount
;
2126 /* In a conditional, it is legal to not have an open paren. We
2127 should save the following token in this case. */
2128 paren
= cpp_get_token (pfile
);
2130 /* If not a paren, see if we're OK. */
2131 if (paren
->type
!= CPP_OPEN_PAREN
)
2133 /* In a conditional no answer is a test for any answer. It
2134 could be followed by any token. */
2137 _cpp_backup_tokens (pfile
, 1);
2141 /* #unassert with no answer is valid - it removes all answers. */
2142 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
2145 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
2146 "missing '(' after predicate");
2150 for (acount
= 0;; acount
++)
2153 const cpp_token
*token
= cpp_get_token (pfile
);
2156 if (token
->type
== CPP_CLOSE_PAREN
)
2159 if (token
->type
== CPP_EOF
)
2161 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
2165 /* struct answer includes the space for one token. */
2166 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
2168 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
2169 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
2171 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
2174 /* Drop whitespace at start, for answer equivalence purposes. */
2176 dest
->flags
&= ~PREV_WHITE
;
2181 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
2185 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
2186 answer
->count
= acount
;
2187 answer
->next
= NULL
;
2193 /* Parses an assertion directive of type TYPE, returning a pointer to
2194 the hash node of the predicate, or 0 on error. If an answer was
2195 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
2196 static cpp_hashnode
*
2197 parse_assertion (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
2199 cpp_hashnode
*result
= 0;
2200 const cpp_token
*predicate
;
2202 /* We don't expand predicates or answers. */
2203 pfile
->state
.prevent_expansion
++;
2206 predicate
= cpp_get_token (pfile
);
2207 if (predicate
->type
== CPP_EOF
)
2208 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2209 else if (predicate
->type
!= CPP_NAME
)
2210 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2211 "predicate must be an identifier");
2212 else if (parse_answer (pfile
, answerp
, type
, predicate
->src_loc
) == 0)
2214 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2215 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2217 /* Prefix '#' to get it out of macro namespace. */
2219 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2220 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2223 pfile
->state
.prevent_expansion
--;
2227 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2228 or a pointer to NULL if the answer is not in the chain. */
2229 static struct answer
**
2230 find_answer (cpp_hashnode
*node
, const struct answer
*candidate
)
2233 struct answer
**result
;
2235 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
2237 struct answer
*answer
= *result
;
2239 if (answer
->count
== candidate
->count
)
2241 for (i
= 0; i
< answer
->count
; i
++)
2242 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
2245 if (i
== answer
->count
)
2253 /* Test an assertion within a preprocessor conditional. Returns
2254 nonzero on failure, zero on success. On success, the result of
2255 the test is written into VALUE, otherwise the value 0. */
2257 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2259 struct answer
*answer
;
2262 node
= parse_assertion (pfile
, &answer
, T_IF
);
2264 /* For recovery, an erroneous assertion expression is handled as a
2265 failing assertion. */
2269 *value
= (node
->type
== NT_ASSERTION
&&
2270 (answer
== 0 || *find_answer (node
, answer
) != 0));
2271 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2272 _cpp_backup_tokens (pfile
, 1);
2274 /* We don't commit the memory for the answer - it's temporary only. */
2278 /* Handle #assert. */
2280 do_assert (cpp_reader
*pfile
)
2282 struct answer
*new_answer
;
2285 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
2290 /* Place the new answer in the answer list. First check there
2291 is not a duplicate. */
2292 new_answer
->next
= 0;
2293 if (node
->type
== NT_ASSERTION
)
2295 if (*find_answer (node
, new_answer
))
2297 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2298 NODE_NAME (node
) + 1);
2301 new_answer
->next
= node
->value
.answers
;
2304 answer_size
= sizeof (struct answer
) + ((new_answer
->count
- 1)
2305 * sizeof (cpp_token
));
2306 /* Commit or allocate storage for the object. */
2307 if (pfile
->hash_table
->alloc_subobject
)
2309 struct answer
*temp_answer
= new_answer
;
2310 new_answer
= (struct answer
*) pfile
->hash_table
->alloc_subobject
2312 memcpy (new_answer
, temp_answer
, answer_size
);
2315 BUFF_FRONT (pfile
->a_buff
) += answer_size
;
2317 node
->type
= NT_ASSERTION
;
2318 node
->value
.answers
= new_answer
;
2319 check_eol (pfile
, false);
2323 /* Handle #unassert. */
2325 do_unassert (cpp_reader
*pfile
)
2328 struct answer
*answer
;
2330 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
2331 /* It isn't an error to #unassert something that isn't asserted. */
2332 if (node
&& node
->type
== NT_ASSERTION
)
2336 struct answer
**p
= find_answer (node
, answer
), *temp
;
2338 /* Remove the answer from the list. */
2343 /* Did we free the last answer? */
2344 if (node
->value
.answers
== 0)
2345 node
->type
= NT_VOID
;
2347 check_eol (pfile
, false);
2350 _cpp_free_definition (node
);
2353 /* We don't commit the memory for the answer - it's temporary only. */
2356 /* These are for -D, -U, -A. */
2358 /* Process the string STR as if it appeared as the body of a #define.
2359 If STR is just an identifier, define it with value 1.
2360 If STR has anything after the identifier, then it should
2361 be identifier=definition. */
2363 cpp_define (cpp_reader
*pfile
, const char *str
)
2369 /* Copy the entire option so we can modify it.
2370 Change the first "=" in the string to a space. If there is none,
2371 tack " 1" on the end. */
2373 count
= strlen (str
);
2374 buf
= (char *) alloca (count
+ 3);
2375 memcpy (buf
, str
, count
);
2377 p
= strchr (str
, '=');
2387 run_directive (pfile
, T_DEFINE
, buf
, count
);
2391 /* Use to build macros to be run through cpp_define() as
2393 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2396 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2402 ptr
= xvasprintf (fmt
, ap
);
2405 cpp_define (pfile
, ptr
);
2410 /* Slight variant of the above for use by initialize_builtins. */
2412 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2414 size_t len
= strlen (str
);
2415 char *buf
= (char *) alloca (len
+ 1);
2416 memcpy (buf
, str
, len
);
2418 run_directive (pfile
, T_DEFINE
, buf
, len
);
2421 /* Process MACRO as if it appeared as the body of an #undef. */
2423 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2425 size_t len
= strlen (macro
);
2426 char *buf
= (char *) alloca (len
+ 1);
2427 memcpy (buf
, macro
, len
);
2429 run_directive (pfile
, T_UNDEF
, buf
, len
);
2432 /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
2433 or first element is zero, then the macro should be undefined. */
2435 cpp_pop_definition (cpp_reader
*pfile
, struct def_pragma_macro
*c
)
2437 cpp_hashnode
*node
= _cpp_lex_identifier (pfile
, c
->name
);
2441 if (pfile
->cb
.before_define
)
2442 pfile
->cb
.before_define (pfile
);
2444 if (node
->type
== NT_MACRO
)
2446 if (pfile
->cb
.undef
)
2447 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2448 if (CPP_OPTION (pfile
, warn_unused_macros
))
2449 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2451 if (node
->type
!= NT_VOID
)
2452 _cpp_free_definition (node
);
2459 cpp_hashnode
*h
= NULL
;
2462 namelen
= ustrcspn (c
->definition
, "( \n");
2463 h
= cpp_lookup (pfile
, c
->definition
, namelen
);
2464 dn
= c
->definition
+ namelen
;
2467 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
|NODE_USED
);
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);
2612 /* Enter all recognized directives in the hash table. */
2614 _cpp_init_directives (cpp_reader
*pfile
)
2619 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2621 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2622 node
->is_directive
= 1;
2623 node
->directive_index
= i
;
2627 /* Extract header file from a bracket include. Parsing starts after '<'.
2628 The string is malloced and must be freed by the caller. */
2630 _cpp_bracket_include(cpp_reader
*pfile
)
2632 return glue_header_name (pfile
);