1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2007, 2008, 2009 Free Software Foundation, Inc.
5 Contributed by Per Bothner, 1994-95.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
34 struct if_stack
*next
;
35 linenum_type line
; /* Line where condition started. */
36 const cpp_hashnode
*mi_cmacro
;/* macro name for #ifndef around entire file */
37 bool skip_elses
; /* Can future #else / #elif be skipped? */
38 bool was_skipping
; /* If were skipping on entry. */
39 int type
; /* Most recent conditional for diagnostics. */
42 /* Contains a registered pragma or pragma namespace. */
43 typedef void (*pragma_cb
) (cpp_reader
*);
46 struct pragma_entry
*next
;
47 const cpp_hashnode
*pragma
; /* Name and length. */
54 struct pragma_entry
*space
;
59 /* Values for the origin field of struct directive. KANDR directives
60 come from traditional (K&R) C. STDC89 directives come from the
61 1989 C standard. EXTENSION directives are extensions. */
66 /* Values for the flags field of struct directive. COND indicates a
67 conditional; IF_COND an opening conditional. INCL means to treat
68 "..." and <...> as q-char and h-char sequences respectively. IN_I
69 means this directive should be handled even if -fpreprocessed is in
70 effect (these are the directives with callback hooks).
72 EXPAND is set on directives that are always macro-expanded. */
74 #define IF_COND (1 << 1)
77 #define EXPAND (1 << 4)
78 #define DEPRECATED (1 << 5)
80 /* Defines one #-directive, including how to handle it. */
81 typedef void (*directive_handler
) (cpp_reader
*);
82 typedef struct directive directive
;
85 directive_handler handler
; /* Function to handle directive. */
86 const uchar
*name
; /* Name of directive. */
87 unsigned short length
; /* Length of name. */
88 unsigned char origin
; /* Origin of directive. */
89 unsigned char flags
; /* Flags describing this directive. */
92 /* Forward declarations. */
94 static void skip_rest_of_line (cpp_reader
*);
95 static void check_eol (cpp_reader
*, bool);
96 static void start_directive (cpp_reader
*);
97 static void prepare_directive_trad (cpp_reader
*);
98 static void end_directive (cpp_reader
*, int);
99 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
100 static void run_directive (cpp_reader
*, int, const char *, size_t);
101 static char *glue_header_name (cpp_reader
*);
102 static const char *parse_include (cpp_reader
*, int *, const cpp_token
***,
104 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
105 static unsigned int read_flag (cpp_reader
*, unsigned int);
106 static bool strtolinenum (const uchar
*, size_t, linenum_type
*, bool *);
107 static void do_diagnostic (cpp_reader
*, int, int, int);
108 static cpp_hashnode
*lex_macro_node (cpp_reader
*, bool);
109 static int undefine_macros (cpp_reader
*, cpp_hashnode
*, void *);
110 static void do_include_common (cpp_reader
*, enum include_type
);
111 static struct pragma_entry
*lookup_pragma_entry (struct pragma_entry
*,
112 const cpp_hashnode
*);
113 static int count_registered_pragmas (struct pragma_entry
*);
114 static char ** save_registered_pragmas (struct pragma_entry
*, char **);
115 static char ** restore_registered_pragmas (cpp_reader
*, struct pragma_entry
*,
117 static void do_pragma_once (cpp_reader
*);
118 static void do_pragma_poison (cpp_reader
*);
119 static void do_pragma_system_header (cpp_reader
*);
120 static void do_pragma_dependency (cpp_reader
*);
121 static void do_linemarker (cpp_reader
*);
122 static const cpp_token
*get_token_no_padding (cpp_reader
*);
123 static const cpp_token
*get__Pragma_string (cpp_reader
*);
124 static void destringize_and_run (cpp_reader
*, const cpp_string
*);
125 static int parse_answer (cpp_reader
*, struct answer
**, int, source_location
);
126 static cpp_hashnode
*parse_assertion (cpp_reader
*, struct answer
**, int);
127 static struct answer
** find_answer (cpp_hashnode
*, const struct answer
*);
128 static void handle_assertion (cpp_reader
*, const char *, int);
129 static void do_pragma_push_macro (cpp_reader
*);
130 static void do_pragma_pop_macro (cpp_reader
*);
132 /* This is the table of directive handlers. It is ordered by
133 frequency of occurrence; the numbers at the end are directive
134 counts from all the source code I have lying around (egcs and libc
135 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
136 pcmcia-cs-3.0.9). This is no longer important as directive lookup
137 is now O(1). All extensions other than #warning, #include_next,
138 and #import are deprecated. The name is where the extension
139 appears to have come from. */
141 #define DIRECTIVE_TABLE \
142 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
143 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
144 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
145 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
146 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
147 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
148 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
149 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
150 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
151 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
152 D(error, T_ERROR, STDC89, 0) /* 475 */ \
153 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
154 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
155 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
156 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
157 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
158 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
159 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
160 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
162 /* #sccs is synonymous with #ident. */
163 #define do_sccs do_ident
165 /* Use the table to generate a series of prototypes, an enum for the
166 directive names, and an array of directive handlers. */
168 #define D(name, t, o, f) static void do_##name (cpp_reader *);
172 #define D(n, tag, o, f) tag,
180 #define D(name, t, origin, flags) \
181 { do_##name, (const uchar *) #name, \
182 sizeof #name - 1, origin, flags },
183 static const directive dtable
[] =
188 #undef DIRECTIVE_TABLE
190 /* Wrapper struct directive for linemarkers.
191 The origin is more or less true - the original K+R cpp
192 did use this notation in its preprocessed output. */
193 static const directive linemarker_dir
=
195 do_linemarker
, UC
"#", 1, KANDR
, IN_I
198 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
200 /* Skip any remaining tokens in a directive. */
202 skip_rest_of_line (cpp_reader
*pfile
)
204 /* Discard all stacked contexts. */
205 while (pfile
->context
->prev
)
206 _cpp_pop_context (pfile
);
208 /* Sweep up all tokens remaining on the line. */
210 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
214 /* Ensure there are no stray tokens at the end of a directive. If
215 EXPAND is true, tokens macro-expanding to nothing are allowed. */
217 check_eol (cpp_reader
*pfile
, bool expand
)
219 if (! SEEN_EOL () && (expand
220 ? cpp_get_token (pfile
)
221 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
222 cpp_error (pfile
, CPP_DL_PEDWARN
, "extra tokens at end of #%s directive",
223 pfile
->directive
->name
);
226 /* Ensure there are no stray tokens other than comments at the end of
227 a directive, and gather the comments. */
228 static const cpp_token
**
229 check_eol_return_comments (cpp_reader
*pfile
)
233 const cpp_token
**buf
;
235 buf
= XNEWVEC (const cpp_token
*, capacity
);
241 const cpp_token
*tok
;
243 tok
= _cpp_lex_token (pfile
);
244 if (tok
->type
== CPP_EOF
)
246 if (tok
->type
!= CPP_COMMENT
)
247 cpp_error (pfile
, CPP_DL_PEDWARN
,
248 "extra tokens at end of #%s directive",
249 pfile
->directive
->name
);
252 if (c
+ 1 >= capacity
)
255 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
266 /* Called when entering a directive, _Pragma or command-line directive. */
268 start_directive (cpp_reader
*pfile
)
270 /* Setup in-directive state. */
271 pfile
->state
.in_directive
= 1;
272 pfile
->state
.save_comments
= 0;
273 pfile
->directive_result
.type
= CPP_PADDING
;
275 /* Some handlers need the position of the # for diagnostics. */
276 pfile
->directive_line
= pfile
->line_table
->highest_line
;
279 /* Called when leaving a directive, _Pragma or command-line directive. */
281 end_directive (cpp_reader
*pfile
, int skip_line
)
283 if (pfile
->state
.in_deferred_pragma
)
285 else if (CPP_OPTION (pfile
, traditional
))
287 /* Revert change of prepare_directive_trad. */
288 pfile
->state
.prevent_expansion
--;
290 if (pfile
->directive
!= &dtable
[T_DEFINE
])
291 _cpp_remove_overlay (pfile
);
293 /* We don't skip for an assembler #. */
296 skip_rest_of_line (pfile
);
297 if (!pfile
->keep_tokens
)
299 pfile
->cur_run
= &pfile
->base_run
;
300 pfile
->cur_token
= pfile
->base_run
.base
;
305 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
306 pfile
->state
.in_directive
= 0;
307 pfile
->state
.in_expression
= 0;
308 pfile
->state
.angled_headers
= 0;
309 pfile
->directive
= 0;
312 /* Prepare to handle the directive in pfile->directive. */
314 prepare_directive_trad (cpp_reader
*pfile
)
316 if (pfile
->directive
!= &dtable
[T_DEFINE
])
318 bool no_expand
= (pfile
->directive
319 && ! (pfile
->directive
->flags
& EXPAND
));
320 bool was_skipping
= pfile
->state
.skipping
;
322 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
323 || pfile
->directive
== &dtable
[T_ELIF
]);
324 if (pfile
->state
.in_expression
)
325 pfile
->state
.skipping
= false;
328 pfile
->state
.prevent_expansion
++;
329 _cpp_scan_out_logical_line (pfile
, NULL
);
331 pfile
->state
.prevent_expansion
--;
333 pfile
->state
.skipping
= was_skipping
;
334 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
335 pfile
->out
.cur
- pfile
->out
.base
);
338 /* Stop ISO C from expanding anything. */
339 pfile
->state
.prevent_expansion
++;
342 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
343 the '#' was indented. */
345 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
347 /* Issue -pedantic or deprecated warnings for extensions. We let
348 -pedantic take precedence if both are applicable. */
349 if (! pfile
->state
.skipping
)
351 if (dir
->origin
== EXTENSION
352 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
353 && CPP_PEDANTIC (pfile
))
354 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
355 else if (((dir
->flags
& DEPRECATED
) != 0
356 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
357 && CPP_OPTION (pfile
, warn_deprecated
))
358 cpp_warning (pfile
, CPP_W_DEPRECATED
,
359 "#%s is a deprecated GCC extension", dir
->name
);
362 /* Traditionally, a directive is ignored unless its # is in
363 column 1. Therefore in code intended to work with K+R
364 compilers, directives added by C89 must have their #
365 indented, and directives present in traditional C must not.
366 This is true even of directives in skipped conditional
367 blocks. #elif cannot be used at all. */
368 if (CPP_WTRADITIONAL (pfile
))
370 if (dir
== &dtable
[T_ELIF
])
371 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
372 "suggest not using #elif in traditional C");
373 else if (indented
&& dir
->origin
== KANDR
)
374 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
375 "traditional C ignores #%s with the # indented",
377 else if (!indented
&& dir
->origin
!= KANDR
)
378 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
379 "suggest hiding #%s from traditional C with an indented #",
384 /* Check if we have a known directive. INDENTED is nonzero if the
385 '#' of the directive was indented. This function is in this file
386 to save unnecessarily exporting dtable etc. to lex.c. Returns
387 nonzero if the line of tokens has been handled, zero if we should
388 continue processing the line. */
390 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
392 const directive
*dir
= 0;
393 const cpp_token
*dname
;
394 bool was_parsing_args
= pfile
->state
.parsing_args
;
395 bool was_discarding_output
= pfile
->state
.discarding_output
;
398 if (was_discarding_output
)
399 pfile
->state
.prevent_expansion
= 0;
401 if (was_parsing_args
)
403 if (CPP_OPTION (pfile
, pedantic
))
404 cpp_error (pfile
, CPP_DL_PEDWARN
,
405 "embedding a directive within macro arguments is not portable");
406 pfile
->state
.parsing_args
= 0;
407 pfile
->state
.prevent_expansion
= 0;
409 start_directive (pfile
);
410 dname
= _cpp_lex_token (pfile
);
412 if (dname
->type
== CPP_NAME
)
414 if (dname
->val
.node
.node
->is_directive
)
415 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
417 /* We do not recognize the # followed by a number extension in
419 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
421 dir
= &linemarker_dir
;
422 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
423 && ! pfile
->state
.skipping
)
424 cpp_error (pfile
, CPP_DL_PEDWARN
,
425 "style of line directive is a GCC extension");
430 /* If we have a directive that is not an opening conditional,
431 invalidate any control macro. */
432 if (! (dir
->flags
& IF_COND
))
433 pfile
->mi_valid
= false;
435 /* Kluge alert. In order to be sure that code like this
440 does not cause '#define foo bar' to get executed when
441 compiled with -save-temps, we recognize directives in
442 -fpreprocessed mode only if the # is in column 1. macro.c
443 puts a space in front of any '#' at the start of a macro.
445 We exclude the -fdirectives-only case because macro expansion
446 has not been performed yet, and block comments can cause spaces
447 to preceed the directive. */
448 if (CPP_OPTION (pfile
, preprocessed
)
449 && !CPP_OPTION (pfile
, directives_only
)
450 && (indented
|| !(dir
->flags
& IN_I
)))
457 /* In failed conditional groups, all non-conditional
458 directives are ignored. Before doing that, whether
459 skipping or not, we should lex angle-bracketed headers
460 correctly, and maybe output some diagnostics. */
461 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
462 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
463 if (! CPP_OPTION (pfile
, preprocessed
))
464 directive_diagnostics (pfile
, dir
, indented
);
465 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
469 else if (dname
->type
== CPP_EOF
)
470 ; /* CPP_EOF is the "null directive". */
473 /* An unknown directive. Don't complain about it in assembly
474 source: we don't know where the comments are, and # may
475 introduce assembler pseudo-ops. Don't complain about invalid
476 directives in skipped conditional groups (6.10 p4). */
477 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
479 else if (!pfile
->state
.skipping
)
480 cpp_error (pfile
, CPP_DL_ERROR
, "invalid preprocessing directive #%s",
481 cpp_token_as_text (pfile
, dname
));
484 pfile
->directive
= dir
;
485 if (CPP_OPTION (pfile
, traditional
))
486 prepare_directive_trad (pfile
);
489 pfile
->directive
->handler (pfile
);
491 _cpp_backup_tokens (pfile
, 1);
493 end_directive (pfile
, skip
);
494 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
496 /* Restore state when within macro args. */
497 pfile
->state
.parsing_args
= 2;
498 pfile
->state
.prevent_expansion
= 1;
500 if (was_discarding_output
)
501 pfile
->state
.prevent_expansion
= 1;
505 /* Directive handler wrapper used by the command line option
506 processor. BUF is \n terminated. */
508 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
510 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
511 /* from_stage3 */ true);
512 start_directive (pfile
);
514 /* This is a short-term fix to prevent a leading '#' being
515 interpreted as a directive. */
516 _cpp_clean_line (pfile
);
518 pfile
->directive
= &dtable
[dir_no
];
519 if (CPP_OPTION (pfile
, traditional
))
520 prepare_directive_trad (pfile
);
521 pfile
->directive
->handler (pfile
);
522 end_directive (pfile
, 1);
523 _cpp_pop_buffer (pfile
);
526 /* Checks for validity the macro name in #define, #undef, #ifdef and
527 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
528 processing a #define or #undefine directive, and false
530 static cpp_hashnode
*
531 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
533 const cpp_token
*token
= _cpp_lex_token (pfile
);
535 /* The token immediately after #define must be an identifier. That
536 identifier may not be "defined", per C99 6.10.8p4.
537 In C++, it may not be any of the "named operators" either,
538 per C++98 [lex.digraph], [lex.key].
539 Finally, the identifier may not have been poisoned. (In that case
540 the lexer has issued the error message for us.) */
542 if (token
->type
== CPP_NAME
)
544 cpp_hashnode
*node
= token
->val
.node
.node
;
546 if (is_def_or_undef
&& node
== pfile
->spec_nodes
.n_defined
)
547 cpp_error (pfile
, CPP_DL_ERROR
,
548 "\"defined\" cannot be used as a macro name");
549 else if (! (node
->flags
& NODE_POISONED
))
552 else if (token
->flags
& NAMED_OP
)
553 cpp_error (pfile
, CPP_DL_ERROR
,
554 "\"%s\" cannot be used as a macro name as it is an operator in C++",
555 NODE_NAME (token
->val
.node
.node
));
556 else if (token
->type
== CPP_EOF
)
557 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
558 pfile
->directive
->name
);
560 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
565 /* Process a #define directive. Most work is done in macro.c. */
567 do_define (cpp_reader
*pfile
)
569 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
573 /* If we have been requested to expand comments into macros,
574 then re-enable saving of comments. */
575 pfile
->state
.save_comments
=
576 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
578 if (pfile
->cb
.before_define
)
579 pfile
->cb
.before_define (pfile
);
581 if (_cpp_create_definition (pfile
, node
))
582 if (pfile
->cb
.define
)
583 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
585 node
->flags
&= ~NODE_USED
;
589 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
591 do_undef (cpp_reader
*pfile
)
593 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
597 if (pfile
->cb
.before_define
)
598 pfile
->cb
.before_define (pfile
);
601 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
603 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
604 identifier is not currently defined as a macro name. */
605 if (node
->type
== NT_MACRO
)
607 if (node
->flags
& NODE_WARN
)
608 cpp_error (pfile
, CPP_DL_WARNING
,
609 "undefining \"%s\"", NODE_NAME (node
));
611 if (CPP_OPTION (pfile
, warn_unused_macros
))
612 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
614 _cpp_free_definition (node
);
618 check_eol (pfile
, false);
621 /* Undefine a single macro/assertion/whatever. */
624 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
625 void *data_p ATTRIBUTE_UNUSED
)
627 /* Body of _cpp_free_definition inlined here for speed.
628 Macros and assertions no longer have anything to free. */
630 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
|NODE_USED
);
634 /* Undefine all macros and assertions. */
637 cpp_undef_all (cpp_reader
*pfile
)
639 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
643 /* Helper routine used by parse_include. Reinterpret the current line
644 as an h-char-sequence (< ... >); we are looking at the first token
645 after the <. Returns a malloced filename. */
647 glue_header_name (cpp_reader
*pfile
)
649 const cpp_token
*token
;
651 size_t len
, total_len
= 0, capacity
= 1024;
653 /* To avoid lexed tokens overwriting our glued name, we can only
654 allocate from the string pool once we've lexed everything. */
655 buffer
= XNEWVEC (char, capacity
);
658 token
= get_token_no_padding (pfile
);
660 if (token
->type
== CPP_GREATER
)
662 if (token
->type
== CPP_EOF
)
664 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
668 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
669 if (total_len
+ len
> capacity
)
671 capacity
= (capacity
+ len
) * 2;
672 buffer
= XRESIZEVEC (char, buffer
, capacity
);
675 if (token
->flags
& PREV_WHITE
)
676 buffer
[total_len
++] = ' ';
678 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
683 buffer
[total_len
] = '\0';
687 /* Returns the file name of #include, #include_next, #import and
688 #pragma dependency. The string is malloced and the caller should
689 free it. Returns NULL on error. LOCATION is the source location
693 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
694 const cpp_token
***buf
, source_location
*location
)
697 const cpp_token
*header
;
699 /* Allow macro expansion. */
700 header
= get_token_no_padding (pfile
);
701 *location
= header
->src_loc
;
702 if ((header
->type
== CPP_STRING
&& header
->val
.str
.text
[0] != 'R')
703 || header
->type
== CPP_HEADER_NAME
)
705 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
706 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
707 fname
[header
->val
.str
.len
- 2] = '\0';
708 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
710 else if (header
->type
== CPP_LESS
)
712 fname
= glue_header_name (pfile
);
713 *pangle_brackets
= 1;
717 const unsigned char *dir
;
719 if (pfile
->directive
== &dtable
[T_PRAGMA
])
720 dir
= UC
"pragma dependency";
722 dir
= pfile
->directive
->name
;
723 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
729 if (pfile
->directive
== &dtable
[T_PRAGMA
])
731 /* This pragma allows extra tokens after the file name. */
733 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
734 check_eol (pfile
, true);
737 /* If we are not discarding comments, then gather them while
738 doing the eol check. */
739 *buf
= check_eol_return_comments (pfile
);
745 /* Handle #include, #include_next and #import. */
747 do_include_common (cpp_reader
*pfile
, enum include_type type
)
751 const cpp_token
**buf
= NULL
;
752 source_location location
;
754 /* Re-enable saving of comments if requested, so that the include
755 callback can dump comments which follow #include. */
756 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
758 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
768 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
769 "empty filename in #%s",
770 pfile
->directive
->name
);
777 /* Prevent #include recursion. */
778 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
779 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
782 /* Get out of macro context, if we are. */
783 skip_rest_of_line (pfile
);
785 if (pfile
->cb
.include
)
786 pfile
->cb
.include (pfile
, pfile
->directive_line
,
787 pfile
->directive
->name
, fname
, angle_brackets
,
790 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
);
799 do_include (cpp_reader
*pfile
)
801 do_include_common (pfile
, IT_INCLUDE
);
805 do_import (cpp_reader
*pfile
)
807 do_include_common (pfile
, IT_IMPORT
);
811 do_include_next (cpp_reader
*pfile
)
813 enum include_type type
= IT_INCLUDE_NEXT
;
815 /* If this is the primary source file, warn and use the normal
817 if (cpp_in_primary_file (pfile
))
819 cpp_error (pfile
, CPP_DL_WARNING
,
820 "#include_next in primary source file");
823 do_include_common (pfile
, type
);
826 /* Subroutine of do_linemarker. Read possible flags after file name.
827 LAST is the last flag seen; 0 if this is the first flag. Return the
828 flag if it is valid, 0 at the end of the directive. Otherwise
831 read_flag (cpp_reader
*pfile
, unsigned int last
)
833 const cpp_token
*token
= _cpp_lex_token (pfile
);
835 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
837 unsigned int flag
= token
->val
.str
.text
[0] - '0';
839 if (flag
> last
&& flag
<= 4
840 && (flag
!= 4 || last
== 3)
841 && (flag
!= 2 || last
== 0))
845 if (token
->type
!= CPP_EOF
)
846 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
847 cpp_token_as_text (pfile
, token
));
851 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
852 of length LEN, to binary; store it in NUMP, and return false if the
853 number was well-formed, true if not. WRAPPED is set to true if the
854 number did not fit into 'unsigned long'. */
856 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
858 linenum_type reg
= 0;
859 linenum_type reg_prev
= 0;
878 /* Interpret #line command.
879 Note that the filename string (if any) is a true string constant
880 (escapes are interpreted), unlike in #line. */
882 do_line (cpp_reader
*pfile
)
884 const struct line_maps
*line_table
= pfile
->line_table
;
885 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
887 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
890 unsigned char map_sysp
= map
->sysp
;
891 const cpp_token
*token
;
892 const char *new_file
= map
->to_file
;
893 linenum_type new_lineno
;
895 /* C99 raised the minimum limit on #line numbers. */
896 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
899 /* #line commands expand macros. */
900 token
= cpp_get_token (pfile
);
901 if (token
->type
!= CPP_NUMBER
902 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
903 &new_lineno
, &wrapped
))
905 if (token
->type
== CPP_EOF
)
906 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
908 cpp_error (pfile
, CPP_DL_ERROR
,
909 "\"%s\" after #line is not a positive integer",
910 cpp_token_as_text (pfile
, token
));
914 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
915 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
917 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
919 token
= cpp_get_token (pfile
);
920 if (token
->type
== CPP_STRING
)
922 cpp_string s
= { 0, 0 };
923 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
925 new_file
= (const char *)s
.text
;
926 check_eol (pfile
, true);
928 else if (token
->type
!= CPP_EOF
)
930 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
931 cpp_token_as_text (pfile
, token
));
935 skip_rest_of_line (pfile
);
936 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
940 /* Interpret the # 44 "file" [flags] notation, which has slightly
941 different syntax and semantics from #line: Flags are allowed,
942 and we never complain about the line number being too big. */
944 do_linemarker (cpp_reader
*pfile
)
946 const struct line_maps
*line_table
= pfile
->line_table
;
947 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
948 const cpp_token
*token
;
949 const char *new_file
= map
->to_file
;
950 linenum_type new_lineno
;
951 unsigned int new_sysp
= map
->sysp
;
952 enum lc_reason reason
= LC_RENAME_VERBATIM
;
956 /* Back up so we can get the number again. Putting this in
957 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
958 some circumstances, which can segfault. */
959 _cpp_backup_tokens (pfile
, 1);
961 /* #line commands expand macros. */
962 token
= cpp_get_token (pfile
);
963 if (token
->type
!= CPP_NUMBER
964 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
965 &new_lineno
, &wrapped
))
967 /* Unlike #line, there does not seem to be a way to get an EOF
968 here. So, it should be safe to always spell the token. */
969 cpp_error (pfile
, CPP_DL_ERROR
,
970 "\"%s\" after # is not a positive integer",
971 cpp_token_as_text (pfile
, token
));
975 token
= cpp_get_token (pfile
);
976 if (token
->type
== CPP_STRING
)
978 cpp_string s
= { 0, 0 };
979 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
981 new_file
= (const char *)s
.text
;
984 flag
= read_flag (pfile
, 0);
988 /* Fake an include for cpp_included (). */
989 _cpp_fake_include (pfile
, new_file
);
990 flag
= read_flag (pfile
, flag
);
995 flag
= read_flag (pfile
, flag
);
1000 flag
= read_flag (pfile
, flag
);
1004 pfile
->buffer
->sysp
= new_sysp
;
1006 check_eol (pfile
, false);
1008 else if (token
->type
!= CPP_EOF
)
1010 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1011 cpp_token_as_text (pfile
, token
));
1015 skip_rest_of_line (pfile
);
1017 /* Compensate for the increment in linemap_add that occurs in
1018 _cpp_do_file_change. We're currently at the start of the line
1019 *following* the #line directive. A separate source_location for this
1020 location makes no sense (until we do the LC_LEAVE), and
1021 complicates LAST_SOURCE_LINE_LOCATION. */
1022 pfile
->line_table
->highest_location
--;
1024 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1027 /* Arrange the file_change callback. pfile->line has changed to
1028 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1029 header, 2 for a system header that needs to be extern "C" protected,
1030 and zero otherwise. */
1032 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1033 const char *to_file
, linenum_type file_line
,
1036 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1037 to_file
, file_line
);
1039 linemap_line_start (pfile
->line_table
, map
->to_line
, 127);
1041 if (pfile
->cb
.file_change
)
1042 pfile
->cb
.file_change (pfile
, map
);
1045 /* Report a warning or error detected by the program we are
1046 processing. Use the directive's tokens in the error message. */
1048 do_diagnostic (cpp_reader
*pfile
, int code
, int reason
, int print_dir
)
1050 const unsigned char *dir_name
;
1051 unsigned char *line
;
1052 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1055 dir_name
= pfile
->directive
->name
;
1058 pfile
->state
.prevent_expansion
++;
1059 line
= cpp_output_line_to_string (pfile
, dir_name
);
1060 pfile
->state
.prevent_expansion
--;
1062 if (code
== CPP_DL_WARNING_SYSHDR
&& reason
)
1063 cpp_warning_with_line_syshdr (pfile
, reason
, src_loc
, 0, "%s", line
);
1064 else if (code
== CPP_DL_WARNING
&& reason
)
1065 cpp_warning_with_line (pfile
, reason
, src_loc
, 0, "%s", line
);
1067 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1072 do_error (cpp_reader
*pfile
)
1074 do_diagnostic (pfile
, CPP_DL_ERROR
, 0, 1);
1078 do_warning (cpp_reader
*pfile
)
1080 /* We want #warning diagnostics to be emitted in system headers too. */
1081 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, CPP_W_WARNING_DIRECTIVE
, 1);
1084 /* Report program identification. */
1086 do_ident (cpp_reader
*pfile
)
1088 const cpp_token
*str
= cpp_get_token (pfile
);
1090 if (str
->type
!= CPP_STRING
)
1091 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1092 pfile
->directive
->name
);
1093 else if (pfile
->cb
.ident
)
1094 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1096 check_eol (pfile
, false);
1099 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1100 matching entry, or NULL if none is found. The returned entry could
1101 be the start of a namespace chain, or a pragma. */
1102 static struct pragma_entry
*
1103 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1105 while (chain
&& chain
->pragma
!= pragma
)
1106 chain
= chain
->next
;
1111 /* Create and insert a blank pragma entry at the beginning of a
1112 singly-linked CHAIN. */
1113 static struct pragma_entry
*
1114 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1116 struct pragma_entry
*new_entry
;
1118 new_entry
= (struct pragma_entry
*)
1119 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1121 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1122 new_entry
->next
= *chain
;
1128 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1129 goes in the global namespace. */
1130 static struct pragma_entry
*
1131 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1132 bool allow_name_expansion
)
1134 struct pragma_entry
**chain
= &pfile
->pragmas
;
1135 struct pragma_entry
*entry
;
1136 const cpp_hashnode
*node
;
1140 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1141 entry
= lookup_pragma_entry (*chain
, node
);
1144 entry
= new_pragma_entry (pfile
, chain
);
1145 entry
->pragma
= node
;
1146 entry
->is_nspace
= true;
1147 entry
->allow_expansion
= allow_name_expansion
;
1149 else if (!entry
->is_nspace
)
1151 else if (entry
->allow_expansion
!= allow_name_expansion
)
1153 cpp_error (pfile
, CPP_DL_ICE
,
1154 "registering pragmas in namespace \"%s\" with mismatched "
1155 "name expansion", space
);
1158 chain
= &entry
->u
.space
;
1160 else if (allow_name_expansion
)
1162 cpp_error (pfile
, CPP_DL_ICE
,
1163 "registering pragma \"%s\" with name expansion "
1164 "and no namespace", name
);
1168 /* Check for duplicates. */
1169 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1170 entry
= lookup_pragma_entry (*chain
, node
);
1173 entry
= new_pragma_entry (pfile
, chain
);
1174 entry
->pragma
= node
;
1178 if (entry
->is_nspace
)
1180 cpp_error (pfile
, CPP_DL_ICE
,
1181 "registering \"%s\" as both a pragma and a pragma namespace",
1184 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1187 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1192 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1194 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1195 const char *name
, pragma_cb handler
)
1197 struct pragma_entry
*entry
;
1199 entry
= register_pragma_1 (pfile
, space
, name
, false);
1200 entry
->is_internal
= true;
1201 entry
->u
.handler
= handler
;
1204 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1205 goes in the global namespace. HANDLER is the handler it will call,
1206 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1207 expansion while parsing pragma NAME. This function is exported
1210 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1211 pragma_cb handler
, bool allow_expansion
)
1213 struct pragma_entry
*entry
;
1217 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1221 entry
= register_pragma_1 (pfile
, space
, name
, false);
1224 entry
->allow_expansion
= allow_expansion
;
1225 entry
->u
.handler
= handler
;
1229 /* Similarly, but create mark the pragma for deferred processing.
1230 When found, a CPP_PRAGMA token will be insertted into the stream
1231 with IDENT in the token->u.pragma slot. */
1233 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1234 const char *name
, unsigned int ident
,
1235 bool allow_expansion
, bool allow_name_expansion
)
1237 struct pragma_entry
*entry
;
1239 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1242 entry
->is_deferred
= true;
1243 entry
->allow_expansion
= allow_expansion
;
1244 entry
->u
.ident
= ident
;
1248 /* Register the pragmas the preprocessor itself handles. */
1250 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1252 /* Pragmas in the global namespace. */
1253 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1254 register_pragma_internal (pfile
, 0, "push_macro", do_pragma_push_macro
);
1255 register_pragma_internal (pfile
, 0, "pop_macro", do_pragma_pop_macro
);
1257 /* New GCC-specific pragmas should be put in the GCC namespace. */
1258 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1259 register_pragma_internal (pfile
, "GCC", "system_header",
1260 do_pragma_system_header
);
1261 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1264 /* Return the number of registered pragmas in PE. */
1267 count_registered_pragmas (struct pragma_entry
*pe
)
1270 for (; pe
!= NULL
; pe
= pe
->next
)
1273 ct
+= count_registered_pragmas (pe
->u
.space
);
1279 /* Save into SD the names of the registered pragmas referenced by PE,
1280 and return a pointer to the next free space in SD. */
1283 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1285 for (; pe
!= NULL
; pe
= pe
->next
)
1288 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1289 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1290 HT_LEN (&pe
->pragma
->ident
),
1291 HT_LEN (&pe
->pragma
->ident
) + 1);
1296 /* Return a newly-allocated array which saves the names of the
1297 registered pragmas. */
1300 _cpp_save_pragma_names (cpp_reader
*pfile
)
1302 int ct
= count_registered_pragmas (pfile
->pragmas
);
1303 char **result
= XNEWVEC (char *, ct
);
1304 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1308 /* Restore from SD the names of the registered pragmas referenced by PE,
1309 and return a pointer to the next unused name in SD. */
1312 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1315 for (; pe
!= NULL
; pe
= pe
->next
)
1318 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1319 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1326 /* Restore the names of the registered pragmas from SAVED. */
1329 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1331 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1335 /* Pragmata handling. We handle some, and pass the rest on to the
1336 front end. C99 defines three pragmas and says that no macro
1337 expansion is to be performed on them; whether or not macro
1338 expansion happens for other pragmas is implementation defined.
1339 This implementation allows for a mix of both, since GCC did not
1340 traditionally macro expand its (few) pragmas, whereas OpenMP
1341 specifies that macro expansion should happen. */
1343 do_pragma (cpp_reader
*pfile
)
1345 const struct pragma_entry
*p
= NULL
;
1346 const cpp_token
*token
, *pragma_token
= pfile
->cur_token
;
1348 unsigned int count
= 1;
1350 pfile
->state
.prevent_expansion
++;
1352 token
= cpp_get_token (pfile
);
1354 if (token
->type
== CPP_NAME
)
1356 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1357 if (p
&& p
->is_nspace
)
1359 bool allow_name_expansion
= p
->allow_expansion
;
1360 if (allow_name_expansion
)
1361 pfile
->state
.prevent_expansion
--;
1362 token
= cpp_get_token (pfile
);
1363 if (token
->type
== CPP_NAME
)
1364 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1367 if (allow_name_expansion
)
1368 pfile
->state
.prevent_expansion
++;
1377 pfile
->directive_result
.src_loc
= pragma_token
->src_loc
;
1378 pfile
->directive_result
.type
= CPP_PRAGMA
;
1379 pfile
->directive_result
.flags
= pragma_token
->flags
;
1380 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1381 pfile
->state
.in_deferred_pragma
= true;
1382 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1383 if (!p
->allow_expansion
)
1384 pfile
->state
.prevent_expansion
++;
1388 /* Since the handler below doesn't get the line number, that
1389 it might need for diagnostics, make sure it has the right
1390 numbers in place. */
1391 if (pfile
->cb
.line_change
)
1392 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1393 if (p
->allow_expansion
)
1394 pfile
->state
.prevent_expansion
--;
1395 (*p
->u
.handler
) (pfile
);
1396 if (p
->allow_expansion
)
1397 pfile
->state
.prevent_expansion
++;
1400 else if (pfile
->cb
.def_pragma
)
1402 if (count
== 1 || pfile
->context
->prev
== NULL
)
1403 _cpp_backup_tokens (pfile
, count
);
1406 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1407 won't allow backing 2 tokens. */
1408 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1409 reads both tokens, we could perhaps free it, but if it doesn't,
1410 we don't know the exact lifespan. */
1411 cpp_token
*toks
= XNEWVEC (cpp_token
, 2);
1413 toks
[0].flags
|= NO_EXPAND
;
1415 toks
[1].flags
|= NO_EXPAND
;
1416 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1418 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1421 pfile
->state
.prevent_expansion
--;
1424 /* Handle #pragma once. */
1426 do_pragma_once (cpp_reader
*pfile
)
1428 if (cpp_in_primary_file (pfile
))
1429 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1431 check_eol (pfile
, false);
1432 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1435 /* Handle #pragma push_macro(STRING). */
1437 do_pragma_push_macro (cpp_reader
*pfile
)
1439 char *macroname
, *dest
;
1440 const char *limit
, *src
;
1441 const cpp_token
*txt
;
1442 struct def_pragma_macro
*c
;
1444 txt
= get__Pragma_string (pfile
);
1447 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1448 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1449 "invalid #pragma push_macro directive");
1450 check_eol (pfile
, false);
1451 skip_rest_of_line (pfile
);
1454 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1455 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1456 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1459 /* We know there is a character following the backslash. */
1460 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1465 check_eol (pfile
, false);
1466 skip_rest_of_line (pfile
);
1467 c
= XNEW (struct def_pragma_macro
);
1468 c
->name
= XNEWVAR (char, strlen (macroname
) + 1);
1469 strcpy (c
->name
, macroname
);
1470 c
->next
= pfile
->pushed_macros
;
1471 c
->value
= cpp_push_definition (pfile
, c
->name
);
1472 pfile
->pushed_macros
= c
;
1475 /* Handle #pragma pop_macro(STRING). */
1477 do_pragma_pop_macro (cpp_reader
*pfile
)
1479 char *macroname
, *dest
;
1480 const char *limit
, *src
;
1481 const cpp_token
*txt
;
1482 struct def_pragma_macro
*l
= NULL
, *c
= pfile
->pushed_macros
;
1483 txt
= get__Pragma_string (pfile
);
1486 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1487 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
1488 "invalid #pragma pop_macro directive");
1489 check_eol (pfile
, false);
1490 skip_rest_of_line (pfile
);
1493 dest
= macroname
= (char *) alloca (txt
->val
.str
.len
+ 2);
1494 src
= (const char *) (txt
->val
.str
.text
+ 1 + (txt
->val
.str
.text
[0] == 'L'));
1495 limit
= (const char *) (txt
->val
.str
.text
+ txt
->val
.str
.len
- 1);
1498 /* We know there is a character following the backslash. */
1499 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1504 check_eol (pfile
, false);
1505 skip_rest_of_line (pfile
);
1509 if (!strcmp (c
->name
, macroname
))
1512 pfile
->pushed_macros
= c
->next
;
1515 cpp_pop_definition (pfile
, c
->name
, c
->value
);
1525 /* Handle #pragma GCC poison, to poison one or more identifiers so
1526 that the lexer produces a hard error for each subsequent usage. */
1528 do_pragma_poison (cpp_reader
*pfile
)
1530 const cpp_token
*tok
;
1533 pfile
->state
.poisoned_ok
= 1;
1536 tok
= _cpp_lex_token (pfile
);
1537 if (tok
->type
== CPP_EOF
)
1539 if (tok
->type
!= CPP_NAME
)
1541 cpp_error (pfile
, CPP_DL_ERROR
,
1542 "invalid #pragma GCC poison directive");
1546 hp
= tok
->val
.node
.node
;
1547 if (hp
->flags
& NODE_POISONED
)
1550 if (hp
->type
== NT_MACRO
)
1551 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1553 _cpp_free_definition (hp
);
1554 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1556 pfile
->state
.poisoned_ok
= 0;
1559 /* Mark the current header as a system header. This will suppress
1560 some categories of warnings (notably those from -pedantic). It is
1561 intended for use in system libraries that cannot be implemented in
1562 conforming C, but cannot be certain that their headers appear in a
1563 system include directory. To prevent abuse, it is rejected in the
1564 primary source file. */
1566 do_pragma_system_header (cpp_reader
*pfile
)
1568 if (cpp_in_primary_file (pfile
))
1569 cpp_error (pfile
, CPP_DL_WARNING
,
1570 "#pragma system_header ignored outside include file");
1573 check_eol (pfile
, false);
1574 skip_rest_of_line (pfile
);
1575 cpp_make_system_header (pfile
, 1, 0);
1579 /* Check the modified date of the current include file against a specified
1580 file. Issue a diagnostic, if the specified file is newer. We use this to
1581 determine if a fixed header should be refixed. */
1583 do_pragma_dependency (cpp_reader
*pfile
)
1586 int angle_brackets
, ordering
;
1587 source_location location
;
1589 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
1593 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1595 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1596 else if (ordering
> 0)
1598 cpp_error (pfile
, CPP_DL_WARNING
,
1599 "current file is older than %s", fname
);
1600 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1602 _cpp_backup_tokens (pfile
, 1);
1603 do_diagnostic (pfile
, CPP_DL_WARNING
, 0, 0);
1607 free ((void *) fname
);
1610 /* Get a token but skip padding. */
1611 static const cpp_token
*
1612 get_token_no_padding (cpp_reader
*pfile
)
1616 const cpp_token
*result
= cpp_get_token (pfile
);
1617 if (result
->type
!= CPP_PADDING
)
1622 /* Check syntax is "(string-literal)". Returns the string on success,
1623 or NULL on failure. */
1624 static const cpp_token
*
1625 get__Pragma_string (cpp_reader
*pfile
)
1627 const cpp_token
*string
;
1628 const cpp_token
*paren
;
1630 paren
= get_token_no_padding (pfile
);
1631 if (paren
->type
== CPP_EOF
)
1632 _cpp_backup_tokens (pfile
, 1);
1633 if (paren
->type
!= CPP_OPEN_PAREN
)
1636 string
= get_token_no_padding (pfile
);
1637 if (string
->type
== CPP_EOF
)
1638 _cpp_backup_tokens (pfile
, 1);
1639 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1640 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
1641 && string
->type
!= CPP_UTF8STRING
)
1644 paren
= get_token_no_padding (pfile
);
1645 if (paren
->type
== CPP_EOF
)
1646 _cpp_backup_tokens (pfile
, 1);
1647 if (paren
->type
!= CPP_CLOSE_PAREN
)
1653 /* Destringize IN into a temporary buffer, by removing the first \ of
1654 \" and \\ sequences, and process the result as a #pragma directive. */
1656 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
)
1658 const unsigned char *src
, *limit
;
1659 char *dest
, *result
;
1660 cpp_context
*saved_context
;
1661 cpp_token
*saved_cur_token
;
1662 tokenrun
*saved_cur_run
;
1665 const struct directive
*save_directive
;
1667 dest
= result
= (char *) alloca (in
->len
- 1);
1668 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1669 limit
= in
->text
+ in
->len
- 1;
1672 /* We know there is a character following the backslash. */
1673 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1679 /* Ugh; an awful kludge. We are really not set up to be lexing
1680 tokens when in the middle of a macro expansion. Use a new
1681 context to force cpp_get_token to lex, and so skip_rest_of_line
1682 doesn't go beyond the end of the text. Also, remember the
1683 current lexing position so we can return to it later.
1685 Something like line-at-a-time lexing should remove the need for
1687 saved_context
= pfile
->context
;
1688 saved_cur_token
= pfile
->cur_token
;
1689 saved_cur_run
= pfile
->cur_run
;
1691 pfile
->context
= XNEW (cpp_context
);
1692 pfile
->context
->macro
= 0;
1693 pfile
->context
->prev
= 0;
1694 pfile
->context
->next
= 0;
1696 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1697 until we've read all of the tokens that we want. */
1698 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1699 /* from_stage3 */ true);
1700 /* ??? Antique Disgusting Hack. What does this do? */
1701 if (pfile
->buffer
->prev
)
1702 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1704 start_directive (pfile
);
1705 _cpp_clean_line (pfile
);
1706 save_directive
= pfile
->directive
;
1707 pfile
->directive
= &dtable
[T_PRAGMA
];
1709 end_directive (pfile
, 1);
1710 pfile
->directive
= save_directive
;
1712 /* We always insert at least one token, the directive result. It'll
1713 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1714 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1716 /* If we're not handling the pragma internally, read all of the tokens from
1717 the string buffer now, while the string buffer is still installed. */
1718 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1719 to me what the true lifespan of the tokens are. It would appear that
1720 the lifespan is the entire parse of the main input stream, in which case
1721 this may not be wrong. */
1722 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1728 toks
= XNEWVEC (cpp_token
, maxcount
);
1729 toks
[0] = pfile
->directive_result
;
1733 if (count
== maxcount
)
1735 maxcount
= maxcount
* 3 / 2;
1736 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1738 toks
[count
] = *cpp_get_token (pfile
);
1739 /* Macros have been already expanded by cpp_get_token
1740 if the pragma allowed expansion. */
1741 toks
[count
++].flags
|= NO_EXPAND
;
1743 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1748 toks
= XNEW (cpp_token
);
1749 toks
[0] = pfile
->directive_result
;
1751 /* If we handled the entire pragma internally, make sure we get the
1752 line number correct for the next token. */
1753 if (pfile
->cb
.line_change
)
1754 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1757 /* Finish inlining run_directive. */
1758 pfile
->buffer
->file
= NULL
;
1759 _cpp_pop_buffer (pfile
);
1761 /* Reset the old macro state before ... */
1762 XDELETE (pfile
->context
);
1763 pfile
->context
= saved_context
;
1764 pfile
->cur_token
= saved_cur_token
;
1765 pfile
->cur_run
= saved_cur_run
;
1767 /* ... inserting the new tokens we collected. */
1768 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1771 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1773 _cpp_do__Pragma (cpp_reader
*pfile
)
1775 const cpp_token
*string
= get__Pragma_string (pfile
);
1776 pfile
->directive_result
.type
= CPP_PADDING
;
1780 destringize_and_run (pfile
, &string
->val
.str
);
1783 cpp_error (pfile
, CPP_DL_ERROR
,
1784 "_Pragma takes a parenthesized string literal");
1788 /* Handle #ifdef. */
1790 do_ifdef (cpp_reader
*pfile
)
1794 if (! pfile
->state
.skipping
)
1796 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
1800 skip
= node
->type
!= NT_MACRO
;
1801 _cpp_mark_macro_used (node
);
1802 if (!(node
->flags
& NODE_USED
))
1804 node
->flags
|= NODE_USED
;
1805 if (node
->type
== NT_MACRO
)
1807 if (pfile
->cb
.used_define
)
1808 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1812 if (pfile
->cb
.used_undef
)
1813 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1817 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1818 check_eol (pfile
, false);
1822 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1825 /* Handle #ifndef. */
1827 do_ifndef (cpp_reader
*pfile
)
1830 cpp_hashnode
*node
= 0;
1832 if (! pfile
->state
.skipping
)
1834 node
= lex_macro_node (pfile
, false);
1838 skip
= node
->type
== NT_MACRO
;
1839 _cpp_mark_macro_used (node
);
1840 if (!(node
->flags
& NODE_USED
))
1842 node
->flags
|= NODE_USED
;
1843 if (node
->type
== NT_MACRO
)
1845 if (pfile
->cb
.used_define
)
1846 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1850 if (pfile
->cb
.used_undef
)
1851 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1855 pfile
->cb
.used (pfile
, pfile
->directive_line
, node
);
1856 check_eol (pfile
, false);
1860 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1863 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1864 pfile->mi_ind_cmacro so we can handle multiple-include
1865 optimizations. If macro expansion occurs in the expression, we
1866 cannot treat it as a controlling conditional, since the expansion
1867 could change in the future. That is handled by cpp_get_token. */
1869 do_if (cpp_reader
*pfile
)
1873 if (! pfile
->state
.skipping
)
1874 skip
= _cpp_parse_expr (pfile
, true) == false;
1876 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1879 /* Flip skipping state if appropriate and continue without changing
1880 if_stack; this is so that the error message for missing #endif's
1881 etc. will point to the original #if. */
1883 do_else (cpp_reader
*pfile
)
1885 cpp_buffer
*buffer
= pfile
->buffer
;
1886 struct if_stack
*ifs
= buffer
->if_stack
;
1889 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
1892 if (ifs
->type
== T_ELSE
)
1894 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
1895 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1896 "the conditional began here");
1900 /* Skip any future (erroneous) #elses or #elifs. */
1901 pfile
->state
.skipping
= ifs
->skip_elses
;
1902 ifs
->skip_elses
= true;
1904 /* Invalidate any controlling macro. */
1907 /* Only check EOL if was not originally skipping. */
1908 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1909 check_eol (pfile
, false);
1913 /* Handle a #elif directive by not changing if_stack either. See the
1914 comment above do_else. */
1916 do_elif (cpp_reader
*pfile
)
1918 cpp_buffer
*buffer
= pfile
->buffer
;
1919 struct if_stack
*ifs
= buffer
->if_stack
;
1922 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
1925 if (ifs
->type
== T_ELSE
)
1927 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
1928 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1929 "the conditional began here");
1933 if (! ifs
->was_skipping
)
1936 /* The standard mandates that the expression be parsed even
1937 if we are skipping elses at this point -- the lexical
1938 restrictions on #elif only apply to skipped groups, but
1939 this group is not being skipped. Temporarily set
1940 skipping to false to get lexer warnings. */
1941 pfile
->state
.skipping
= 0;
1942 value
= _cpp_parse_expr (pfile
, false);
1943 if (ifs
->skip_elses
)
1944 pfile
->state
.skipping
= 1;
1947 pfile
->state
.skipping
= ! value
;
1948 ifs
->skip_elses
= value
;
1952 /* Invalidate any controlling macro. */
1957 /* #endif pops the if stack and resets pfile->state.skipping. */
1959 do_endif (cpp_reader
*pfile
)
1961 cpp_buffer
*buffer
= pfile
->buffer
;
1962 struct if_stack
*ifs
= buffer
->if_stack
;
1965 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
1968 /* Only check EOL if was not originally skipping. */
1969 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1970 check_eol (pfile
, false);
1972 /* If potential control macro, we go back outside again. */
1973 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1975 pfile
->mi_valid
= true;
1976 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1979 buffer
->if_stack
= ifs
->next
;
1980 pfile
->state
.skipping
= ifs
->was_skipping
;
1981 obstack_free (&pfile
->buffer_ob
, ifs
);
1985 /* Push an if_stack entry for a preprocessor conditional, and set
1986 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1987 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1988 we need to check here that we are at the top of the file. */
1990 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
1991 const cpp_hashnode
*cmacro
)
1993 struct if_stack
*ifs
;
1994 cpp_buffer
*buffer
= pfile
->buffer
;
1996 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
1997 ifs
->line
= pfile
->directive_line
;
1998 ifs
->next
= buffer
->if_stack
;
1999 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
2000 ifs
->was_skipping
= pfile
->state
.skipping
;
2002 /* This condition is effectively a test for top-of-file. */
2003 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
2004 ifs
->mi_cmacro
= cmacro
;
2008 pfile
->state
.skipping
= skip
;
2009 buffer
->if_stack
= ifs
;
2012 /* Read the tokens of the answer into the macro pool, in a directive
2013 of type TYPE. Only commit the memory if we intend it as permanent
2014 storage, i.e. the #assert case. Returns 0 on success, and sets
2015 ANSWERP to point to the answer. PRED_LOC is the location of the
2018 parse_answer (cpp_reader
*pfile
, struct answer
**answerp
, int type
,
2019 source_location pred_loc
)
2021 const cpp_token
*paren
;
2022 struct answer
*answer
;
2023 unsigned int acount
;
2025 /* In a conditional, it is legal to not have an open paren. We
2026 should save the following token in this case. */
2027 paren
= cpp_get_token (pfile
);
2029 /* If not a paren, see if we're OK. */
2030 if (paren
->type
!= CPP_OPEN_PAREN
)
2032 /* In a conditional no answer is a test for any answer. It
2033 could be followed by any token. */
2036 _cpp_backup_tokens (pfile
, 1);
2040 /* #unassert with no answer is valid - it removes all answers. */
2041 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
2044 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
2045 "missing '(' after predicate");
2049 for (acount
= 0;; acount
++)
2052 const cpp_token
*token
= cpp_get_token (pfile
);
2055 if (token
->type
== CPP_CLOSE_PAREN
)
2058 if (token
->type
== CPP_EOF
)
2060 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
2064 /* struct answer includes the space for one token. */
2065 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
2067 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
2068 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
2070 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
2073 /* Drop whitespace at start, for answer equivalence purposes. */
2075 dest
->flags
&= ~PREV_WHITE
;
2080 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
2084 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
2085 answer
->count
= acount
;
2086 answer
->next
= NULL
;
2092 /* Parses an assertion directive of type TYPE, returning a pointer to
2093 the hash node of the predicate, or 0 on error. If an answer was
2094 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
2095 static cpp_hashnode
*
2096 parse_assertion (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
2098 cpp_hashnode
*result
= 0;
2099 const cpp_token
*predicate
;
2101 /* We don't expand predicates or answers. */
2102 pfile
->state
.prevent_expansion
++;
2105 predicate
= cpp_get_token (pfile
);
2106 if (predicate
->type
== CPP_EOF
)
2107 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2108 else if (predicate
->type
!= CPP_NAME
)
2109 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2110 "predicate must be an identifier");
2111 else if (parse_answer (pfile
, answerp
, type
, predicate
->src_loc
) == 0)
2113 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2114 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2116 /* Prefix '#' to get it out of macro namespace. */
2118 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2119 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2122 pfile
->state
.prevent_expansion
--;
2126 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2127 or a pointer to NULL if the answer is not in the chain. */
2128 static struct answer
**
2129 find_answer (cpp_hashnode
*node
, const struct answer
*candidate
)
2132 struct answer
**result
;
2134 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
2136 struct answer
*answer
= *result
;
2138 if (answer
->count
== candidate
->count
)
2140 for (i
= 0; i
< answer
->count
; i
++)
2141 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
2144 if (i
== answer
->count
)
2152 /* Test an assertion within a preprocessor conditional. Returns
2153 nonzero on failure, zero on success. On success, the result of
2154 the test is written into VALUE, otherwise the value 0. */
2156 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2158 struct answer
*answer
;
2161 node
= parse_assertion (pfile
, &answer
, T_IF
);
2163 /* For recovery, an erroneous assertion expression is handled as a
2164 failing assertion. */
2168 *value
= (node
->type
== NT_ASSERTION
&&
2169 (answer
== 0 || *find_answer (node
, answer
) != 0));
2170 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2171 _cpp_backup_tokens (pfile
, 1);
2173 /* We don't commit the memory for the answer - it's temporary only. */
2177 /* Handle #assert. */
2179 do_assert (cpp_reader
*pfile
)
2181 struct answer
*new_answer
;
2184 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
2189 /* Place the new answer in the answer list. First check there
2190 is not a duplicate. */
2191 new_answer
->next
= 0;
2192 if (node
->type
== NT_ASSERTION
)
2194 if (*find_answer (node
, new_answer
))
2196 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2197 NODE_NAME (node
) + 1);
2200 new_answer
->next
= node
->value
.answers
;
2203 answer_size
= sizeof (struct answer
) + ((new_answer
->count
- 1)
2204 * sizeof (cpp_token
));
2205 /* Commit or allocate storage for the object. */
2206 if (pfile
->hash_table
->alloc_subobject
)
2208 struct answer
*temp_answer
= new_answer
;
2209 new_answer
= (struct answer
*) pfile
->hash_table
->alloc_subobject
2211 memcpy (new_answer
, temp_answer
, answer_size
);
2214 BUFF_FRONT (pfile
->a_buff
) += answer_size
;
2216 node
->type
= NT_ASSERTION
;
2217 node
->value
.answers
= new_answer
;
2218 check_eol (pfile
, false);
2222 /* Handle #unassert. */
2224 do_unassert (cpp_reader
*pfile
)
2227 struct answer
*answer
;
2229 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
2230 /* It isn't an error to #unassert something that isn't asserted. */
2231 if (node
&& node
->type
== NT_ASSERTION
)
2235 struct answer
**p
= find_answer (node
, answer
), *temp
;
2237 /* Remove the answer from the list. */
2242 /* Did we free the last answer? */
2243 if (node
->value
.answers
== 0)
2244 node
->type
= NT_VOID
;
2246 check_eol (pfile
, false);
2249 _cpp_free_definition (node
);
2252 /* We don't commit the memory for the answer - it's temporary only. */
2255 /* These are for -D, -U, -A. */
2257 /* Process the string STR as if it appeared as the body of a #define.
2258 If STR is just an identifier, define it with value 1.
2259 If STR has anything after the identifier, then it should
2260 be identifier=definition. */
2262 cpp_define (cpp_reader
*pfile
, const char *str
)
2268 /* Copy the entire option so we can modify it.
2269 Change the first "=" in the string to a space. If there is none,
2270 tack " 1" on the end. */
2272 count
= strlen (str
);
2273 buf
= (char *) alloca (count
+ 3);
2274 memcpy (buf
, str
, count
);
2276 p
= strchr (str
, '=');
2286 run_directive (pfile
, T_DEFINE
, buf
, count
);
2290 /* Use to build macros to be run through cpp_define() as
2292 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2295 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2301 vasprintf (&ptr
, fmt
, ap
);
2304 cpp_define (pfile
, ptr
);
2309 /* Slight variant of the above for use by initialize_builtins. */
2311 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2313 size_t len
= strlen (str
);
2314 char *buf
= (char *) alloca (len
+ 1);
2315 memcpy (buf
, str
, len
);
2317 run_directive (pfile
, T_DEFINE
, buf
, len
);
2320 /* Process MACRO as if it appeared as the body of an #undef. */
2322 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2324 size_t len
= strlen (macro
);
2325 char *buf
= (char *) alloca (len
+ 1);
2326 memcpy (buf
, macro
, len
);
2328 run_directive (pfile
, T_UNDEF
, buf
, len
);
2331 /* If STR is a defined macro, return its definition node, else return NULL. */
2333 cpp_push_definition (cpp_reader
*pfile
, const char *str
)
2335 cpp_hashnode
*node
= _cpp_lex_identifier (pfile
, str
);
2336 if (node
&& node
->type
== NT_MACRO
)
2337 return node
->value
.macro
;
2342 /* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2343 then the macro should be undefined. */
2345 cpp_pop_definition (cpp_reader
*pfile
, const char *str
, cpp_macro
*dfn
)
2347 cpp_hashnode
*node
= _cpp_lex_identifier (pfile
, str
);
2351 if (pfile
->cb
.before_define
)
2352 pfile
->cb
.before_define (pfile
);
2354 if (node
->type
== NT_MACRO
)
2356 if (pfile
->cb
.undef
)
2357 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2358 if (CPP_OPTION (pfile
, warn_unused_macros
))
2359 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2361 if (node
->type
!= NT_VOID
)
2362 _cpp_free_definition (node
);
2366 node
->type
= NT_MACRO
;
2367 node
->value
.macro
= dfn
;
2368 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_")))
2369 node
->flags
|= NODE_WARN
;
2371 if (pfile
->cb
.define
)
2372 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
2376 /* Process the string STR as if it appeared as the body of a #assert. */
2378 cpp_assert (cpp_reader
*pfile
, const char *str
)
2380 handle_assertion (pfile
, str
, T_ASSERT
);
2383 /* Process STR as if it appeared as the body of an #unassert. */
2385 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2387 handle_assertion (pfile
, str
, T_UNASSERT
);
2390 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2392 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2394 size_t count
= strlen (str
);
2395 const char *p
= strchr (str
, '=');
2397 /* Copy the entire option so we can modify it. Change the first
2398 "=" in the string to a '(', and tack a ')' on the end. */
2399 char *buf
= (char *) alloca (count
+ 2);
2401 memcpy (buf
, str
, count
);
2410 run_directive (pfile
, type
, str
, count
);
2413 /* The options structure. */
2415 cpp_get_options (cpp_reader
*pfile
)
2417 return &pfile
->opts
;
2420 /* The callbacks structure. */
2422 cpp_get_callbacks (cpp_reader
*pfile
)
2427 /* Copy the given callbacks structure to our own. */
2429 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2434 /* The dependencies structure. (Creates one if it hasn't already been.) */
2436 cpp_get_deps (cpp_reader
*pfile
)
2439 pfile
->deps
= deps_init ();
2443 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2444 doesn't fail. It does not generate a file change call back; that
2445 is the responsibility of the caller. */
2447 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2450 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2452 /* Clears, amongst other things, if_stack and mi_cmacro. */
2453 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2455 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2456 new_buffer
->rlimit
= buffer
+ len
;
2457 new_buffer
->from_stage3
= from_stage3
;
2458 new_buffer
->prev
= pfile
->buffer
;
2459 new_buffer
->need_line
= true;
2461 pfile
->buffer
= new_buffer
;
2466 /* Pops a single buffer, with a file change call-back if appropriate.
2467 Then pushes the next -include file, if any remain. */
2469 _cpp_pop_buffer (cpp_reader
*pfile
)
2471 cpp_buffer
*buffer
= pfile
->buffer
;
2472 struct _cpp_file
*inc
= buffer
->file
;
2473 struct if_stack
*ifs
;
2475 /* Walk back up the conditional stack till we reach its level at
2476 entry to this file, issuing error messages. */
2477 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2478 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2479 "unterminated #%s", dtable
[ifs
->type
].name
);
2481 /* In case of a missing #endif. */
2482 pfile
->state
.skipping
= 0;
2484 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2485 pfile
->buffer
= buffer
->prev
;
2487 free (buffer
->notes
);
2489 /* Free the buffer object now; we may want to push a new buffer
2490 in _cpp_push_next_include_file. */
2491 obstack_free (&pfile
->buffer_ob
, buffer
);
2495 _cpp_pop_file_buffer (pfile
, inc
);
2497 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2501 /* Enter all recognized directives in the hash table. */
2503 _cpp_init_directives (cpp_reader
*pfile
)
2508 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2510 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2511 node
->is_directive
= 1;
2512 node
->directive_index
= i
;