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);
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);
130 /* This is the table of directive handlers. It is ordered by
131 frequency of occurrence; the numbers at the end are directive
132 counts from all the source code I have lying around (egcs and libc
133 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
134 pcmcia-cs-3.0.9). This is no longer important as directive lookup
135 is now O(1). All extensions other than #warning, #include_next,
136 and #import are deprecated. The name is where the extension
137 appears to have come from. */
139 #define DIRECTIVE_TABLE \
140 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
141 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
142 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
143 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
144 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
145 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
146 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
147 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
148 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
149 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
150 D(error, T_ERROR, STDC89, 0) /* 475 */ \
151 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
152 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
153 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
154 D(ident, T_IDENT, EXTENSION, IN_I | DEPRECATED) /* 11 */ \
155 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
156 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
157 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
158 D(sccs, T_SCCS, EXTENSION, IN_I | DEPRECATED) /* 0 SVR4? */
160 /* #sccs is synonymous with #ident. */
161 #define do_sccs do_ident
163 /* Use the table to generate a series of prototypes, an enum for the
164 directive names, and an array of directive handlers. */
166 #define D(name, t, o, f) static void do_##name (cpp_reader *);
170 #define D(n, tag, o, f) tag,
178 #define D(name, t, origin, flags) \
179 { do_##name, (const uchar *) #name, \
180 sizeof #name - 1, origin, flags },
181 static const directive dtable
[] =
186 #undef DIRECTIVE_TABLE
188 /* Wrapper struct directive for linemarkers.
189 The origin is more or less true - the original K+R cpp
190 did use this notation in its preprocessed output. */
191 static const directive linemarker_dir
=
193 do_linemarker
, UC
"#", 1, KANDR
, IN_I
196 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
198 /* Skip any remaining tokens in a directive. */
200 skip_rest_of_line (cpp_reader
*pfile
)
202 /* Discard all stacked contexts. */
203 while (pfile
->context
->prev
)
204 _cpp_pop_context (pfile
);
206 /* Sweep up all tokens remaining on the line. */
208 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
212 /* Ensure there are no stray tokens at the end of a directive. If
213 EXPAND is true, tokens macro-expanding to nothing are allowed. */
215 check_eol (cpp_reader
*pfile
, bool expand
)
217 if (! SEEN_EOL () && (expand
218 ? cpp_get_token (pfile
)
219 : _cpp_lex_token (pfile
))->type
!= CPP_EOF
)
220 cpp_error (pfile
, CPP_DL_PEDWARN
, "extra tokens at end of #%s directive",
221 pfile
->directive
->name
);
224 /* Ensure there are no stray tokens other than comments at the end of
225 a directive, and gather the comments. */
226 static const cpp_token
**
227 check_eol_return_comments (cpp_reader
*pfile
)
231 const cpp_token
**buf
;
233 buf
= XNEWVEC (const cpp_token
*, capacity
);
239 const cpp_token
*tok
;
241 tok
= _cpp_lex_token (pfile
);
242 if (tok
->type
== CPP_EOF
)
244 if (tok
->type
!= CPP_COMMENT
)
245 cpp_error (pfile
, CPP_DL_PEDWARN
,
246 "extra tokens at end of #%s directive",
247 pfile
->directive
->name
);
250 if (c
+ 1 >= capacity
)
253 buf
= XRESIZEVEC (const cpp_token
*, buf
, capacity
);
264 /* Called when entering a directive, _Pragma or command-line directive. */
266 start_directive (cpp_reader
*pfile
)
268 /* Setup in-directive state. */
269 pfile
->state
.in_directive
= 1;
270 pfile
->state
.save_comments
= 0;
271 pfile
->directive_result
.type
= CPP_PADDING
;
273 /* Some handlers need the position of the # for diagnostics. */
274 pfile
->directive_line
= pfile
->line_table
->highest_line
;
277 /* Called when leaving a directive, _Pragma or command-line directive. */
279 end_directive (cpp_reader
*pfile
, int skip_line
)
281 if (pfile
->state
.in_deferred_pragma
)
283 else if (CPP_OPTION (pfile
, traditional
))
285 /* Revert change of prepare_directive_trad. */
286 pfile
->state
.prevent_expansion
--;
288 if (pfile
->directive
!= &dtable
[T_DEFINE
])
289 _cpp_remove_overlay (pfile
);
291 /* We don't skip for an assembler #. */
294 skip_rest_of_line (pfile
);
295 if (!pfile
->keep_tokens
)
297 pfile
->cur_run
= &pfile
->base_run
;
298 pfile
->cur_token
= pfile
->base_run
.base
;
303 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
304 pfile
->state
.in_directive
= 0;
305 pfile
->state
.in_expression
= 0;
306 pfile
->state
.angled_headers
= 0;
307 pfile
->directive
= 0;
310 /* Prepare to handle the directive in pfile->directive. */
312 prepare_directive_trad (cpp_reader
*pfile
)
314 if (pfile
->directive
!= &dtable
[T_DEFINE
])
316 bool no_expand
= (pfile
->directive
317 && ! (pfile
->directive
->flags
& EXPAND
));
318 bool was_skipping
= pfile
->state
.skipping
;
320 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
321 || pfile
->directive
== &dtable
[T_ELIF
]);
322 if (pfile
->state
.in_expression
)
323 pfile
->state
.skipping
= false;
326 pfile
->state
.prevent_expansion
++;
327 _cpp_scan_out_logical_line (pfile
, NULL
);
329 pfile
->state
.prevent_expansion
--;
331 pfile
->state
.skipping
= was_skipping
;
332 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
333 pfile
->out
.cur
- pfile
->out
.base
);
336 /* Stop ISO C from expanding anything. */
337 pfile
->state
.prevent_expansion
++;
340 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
341 the '#' was indented. */
343 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
345 /* Issue -pedantic or deprecated warnings for extensions. We let
346 -pedantic take precedence if both are applicable. */
347 if (! pfile
->state
.skipping
)
349 if (dir
->origin
== EXTENSION
350 && !(dir
== &dtable
[T_IMPORT
] && CPP_OPTION (pfile
, objc
))
351 && CPP_PEDANTIC (pfile
))
352 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
353 else if (((dir
->flags
& DEPRECATED
) != 0
354 || (dir
== &dtable
[T_IMPORT
] && !CPP_OPTION (pfile
, objc
)))
355 && CPP_OPTION (pfile
, warn_deprecated
))
356 cpp_error (pfile
, CPP_DL_WARNING
, "#%s is a deprecated GCC extension",
360 /* Traditionally, a directive is ignored unless its # is in
361 column 1. Therefore in code intended to work with K+R
362 compilers, directives added by C89 must have their #
363 indented, and directives present in traditional C must not.
364 This is true even of directives in skipped conditional
365 blocks. #elif cannot be used at all. */
366 if (CPP_WTRADITIONAL (pfile
))
368 if (dir
== &dtable
[T_ELIF
])
369 cpp_error (pfile
, CPP_DL_WARNING
,
370 "suggest not using #elif in traditional C");
371 else if (indented
&& dir
->origin
== KANDR
)
372 cpp_error (pfile
, CPP_DL_WARNING
,
373 "traditional C ignores #%s with the # indented",
375 else if (!indented
&& dir
->origin
!= KANDR
)
376 cpp_error (pfile
, CPP_DL_WARNING
,
377 "suggest hiding #%s from traditional C with an indented #",
382 /* Check if we have a known directive. INDENTED is nonzero if the
383 '#' of the directive was indented. This function is in this file
384 to save unnecessarily exporting dtable etc. to lex.c. Returns
385 nonzero if the line of tokens has been handled, zero if we should
386 continue processing the line. */
388 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
390 const directive
*dir
= 0;
391 const cpp_token
*dname
;
392 bool was_parsing_args
= pfile
->state
.parsing_args
;
393 bool was_discarding_output
= pfile
->state
.discarding_output
;
396 if (was_discarding_output
)
397 pfile
->state
.prevent_expansion
= 0;
399 if (was_parsing_args
)
401 if (CPP_OPTION (pfile
, pedantic
))
402 cpp_error (pfile
, CPP_DL_PEDWARN
,
403 "embedding a directive within macro arguments is not portable");
404 pfile
->state
.parsing_args
= 0;
405 pfile
->state
.prevent_expansion
= 0;
407 start_directive (pfile
);
408 dname
= _cpp_lex_token (pfile
);
410 if (dname
->type
== CPP_NAME
)
412 if (dname
->val
.node
.node
->is_directive
)
413 dir
= &dtable
[dname
->val
.node
.node
->directive_index
];
415 /* We do not recognize the # followed by a number extension in
417 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
419 dir
= &linemarker_dir
;
420 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
421 && ! pfile
->state
.skipping
)
422 cpp_error (pfile
, CPP_DL_PEDWARN
,
423 "style of line directive is a GCC extension");
428 /* If we have a directive that is not an opening conditional,
429 invalidate any control macro. */
430 if (! (dir
->flags
& IF_COND
))
431 pfile
->mi_valid
= false;
433 /* Kluge alert. In order to be sure that code like this
438 does not cause '#define foo bar' to get executed when
439 compiled with -save-temps, we recognize directives in
440 -fpreprocessed mode only if the # is in column 1. macro.c
441 puts a space in front of any '#' at the start of a macro.
443 We exclude the -fdirectives-only case because macro expansion
444 has not been performed yet, and block comments can cause spaces
445 to preceed the directive. */
446 if (CPP_OPTION (pfile
, preprocessed
)
447 && !CPP_OPTION (pfile
, directives_only
)
448 && (indented
|| !(dir
->flags
& IN_I
)))
455 /* In failed conditional groups, all non-conditional
456 directives are ignored. Before doing that, whether
457 skipping or not, we should lex angle-bracketed headers
458 correctly, and maybe output some diagnostics. */
459 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
460 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
461 if (! CPP_OPTION (pfile
, preprocessed
))
462 directive_diagnostics (pfile
, dir
, indented
);
463 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
467 else if (dname
->type
== CPP_EOF
)
468 ; /* CPP_EOF is the "null directive". */
471 /* An unknown directive. Don't complain about it in assembly
472 source: we don't know where the comments are, and # may
473 introduce assembler pseudo-ops. Don't complain about invalid
474 directives in skipped conditional groups (6.10 p4). */
475 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
477 else if (!pfile
->state
.skipping
)
478 cpp_error (pfile
, CPP_DL_ERROR
, "invalid preprocessing directive #%s",
479 cpp_token_as_text (pfile
, dname
));
482 pfile
->directive
= dir
;
483 if (CPP_OPTION (pfile
, traditional
))
484 prepare_directive_trad (pfile
);
487 pfile
->directive
->handler (pfile
);
489 _cpp_backup_tokens (pfile
, 1);
491 end_directive (pfile
, skip
);
492 if (was_parsing_args
&& !pfile
->state
.in_deferred_pragma
)
494 /* Restore state when within macro args. */
495 pfile
->state
.parsing_args
= 2;
496 pfile
->state
.prevent_expansion
= 1;
498 if (was_discarding_output
)
499 pfile
->state
.prevent_expansion
= 1;
503 /* Directive handler wrapper used by the command line option
504 processor. BUF is \n terminated. */
506 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
508 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
509 /* from_stage3 */ true);
510 start_directive (pfile
);
512 /* This is a short-term fix to prevent a leading '#' being
513 interpreted as a directive. */
514 _cpp_clean_line (pfile
);
516 pfile
->directive
= &dtable
[dir_no
];
517 if (CPP_OPTION (pfile
, traditional
))
518 prepare_directive_trad (pfile
);
519 pfile
->directive
->handler (pfile
);
520 end_directive (pfile
, 1);
521 _cpp_pop_buffer (pfile
);
524 /* Checks for validity the macro name in #define, #undef, #ifdef and
525 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
526 processing a #define or #undefine directive, and false
528 static cpp_hashnode
*
529 lex_macro_node (cpp_reader
*pfile
, bool is_def_or_undef
)
531 const cpp_token
*token
= _cpp_lex_token (pfile
);
533 /* The token immediately after #define must be an identifier. That
534 identifier may not be "defined", per C99 6.10.8p4.
535 In C++, it may not be any of the "named operators" either,
536 per C++98 [lex.digraph], [lex.key].
537 Finally, the identifier may not have been poisoned. (In that case
538 the lexer has issued the error message for us.) */
540 if (token
->type
== CPP_NAME
)
542 cpp_hashnode
*node
= token
->val
.node
.node
;
544 if (is_def_or_undef
&& node
== pfile
->spec_nodes
.n_defined
)
545 cpp_error (pfile
, CPP_DL_ERROR
,
546 "\"defined\" cannot be used as a macro name");
547 else if (! (node
->flags
& NODE_POISONED
))
550 else if (token
->flags
& NAMED_OP
)
551 cpp_error (pfile
, CPP_DL_ERROR
,
552 "\"%s\" cannot be used as a macro name as it is an operator in C++",
553 NODE_NAME (token
->val
.node
.node
));
554 else if (token
->type
== CPP_EOF
)
555 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
556 pfile
->directive
->name
);
558 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
563 /* Process a #define directive. Most work is done in macro.c. */
565 do_define (cpp_reader
*pfile
)
567 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
571 /* If we have been requested to expand comments into macros,
572 then re-enable saving of comments. */
573 pfile
->state
.save_comments
=
574 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
576 if (pfile
->cb
.before_define
)
577 pfile
->cb
.before_define (pfile
);
579 if (_cpp_create_definition (pfile
, node
))
580 if (pfile
->cb
.define
)
581 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
583 node
->flags
&= ~NODE_USED
;
587 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
589 do_undef (cpp_reader
*pfile
)
591 cpp_hashnode
*node
= lex_macro_node (pfile
, true);
595 if (pfile
->cb
.before_define
)
596 pfile
->cb
.before_define (pfile
);
599 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
601 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
602 identifier is not currently defined as a macro name. */
603 if (node
->type
== NT_MACRO
)
605 if (node
->flags
& NODE_WARN
)
606 cpp_error (pfile
, CPP_DL_WARNING
,
607 "undefining \"%s\"", NODE_NAME (node
));
609 if (CPP_OPTION (pfile
, warn_unused_macros
))
610 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
612 _cpp_free_definition (node
);
616 check_eol (pfile
, false);
619 /* Undefine a single macro/assertion/whatever. */
622 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
623 void *data_p ATTRIBUTE_UNUSED
)
625 /* Body of _cpp_free_definition inlined here for speed.
626 Macros and assertions no longer have anything to free. */
628 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
|NODE_USED
);
632 /* Undefine all macros and assertions. */
635 cpp_undef_all (cpp_reader
*pfile
)
637 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
641 /* Helper routine used by parse_include. Reinterpret the current line
642 as an h-char-sequence (< ... >); we are looking at the first token
643 after the <. Returns a malloced filename. */
645 glue_header_name (cpp_reader
*pfile
)
647 const cpp_token
*token
;
649 size_t len
, total_len
= 0, capacity
= 1024;
651 /* To avoid lexed tokens overwriting our glued name, we can only
652 allocate from the string pool once we've lexed everything. */
653 buffer
= XNEWVEC (char, capacity
);
656 token
= get_token_no_padding (pfile
);
658 if (token
->type
== CPP_GREATER
)
660 if (token
->type
== CPP_EOF
)
662 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
666 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
667 if (total_len
+ len
> capacity
)
669 capacity
= (capacity
+ len
) * 2;
670 buffer
= XRESIZEVEC (char, buffer
, capacity
);
673 if (token
->flags
& PREV_WHITE
)
674 buffer
[total_len
++] = ' ';
676 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
681 buffer
[total_len
] = '\0';
685 /* Returns the file name of #include, #include_next, #import and
686 #pragma dependency. The string is malloced and the caller should
687 free it. Returns NULL on error. LOCATION is the source location
691 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
692 const cpp_token
***buf
, source_location
*location
)
695 const cpp_token
*header
;
697 /* Allow macro expansion. */
698 header
= get_token_no_padding (pfile
);
699 *location
= header
->src_loc
;
700 if (header
->type
== CPP_STRING
|| header
->type
== CPP_HEADER_NAME
)
702 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
703 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
704 fname
[header
->val
.str
.len
- 2] = '\0';
705 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
707 else if (header
->type
== CPP_LESS
)
709 fname
= glue_header_name (pfile
);
710 *pangle_brackets
= 1;
714 const unsigned char *dir
;
716 if (pfile
->directive
== &dtable
[T_PRAGMA
])
717 dir
= UC
"pragma dependency";
719 dir
= pfile
->directive
->name
;
720 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
726 if (pfile
->directive
== &dtable
[T_PRAGMA
])
728 /* This pragma allows extra tokens after the file name. */
730 else if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
731 check_eol (pfile
, true);
734 /* If we are not discarding comments, then gather them while
735 doing the eol check. */
736 *buf
= check_eol_return_comments (pfile
);
742 /* Handle #include, #include_next and #import. */
744 do_include_common (cpp_reader
*pfile
, enum include_type type
)
748 const cpp_token
**buf
= NULL
;
749 source_location location
;
751 /* Re-enable saving of comments if requested, so that the include
752 callback can dump comments which follow #include. */
753 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
755 fname
= parse_include (pfile
, &angle_brackets
, &buf
, &location
);
765 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
766 "empty filename in #%s",
767 pfile
->directive
->name
);
774 /* Prevent #include recursion. */
775 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
776 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
779 /* Get out of macro context, if we are. */
780 skip_rest_of_line (pfile
);
782 if (pfile
->cb
.include
)
783 pfile
->cb
.include (pfile
, pfile
->directive_line
,
784 pfile
->directive
->name
, fname
, angle_brackets
,
787 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
);
796 do_include (cpp_reader
*pfile
)
798 do_include_common (pfile
, IT_INCLUDE
);
802 do_import (cpp_reader
*pfile
)
804 do_include_common (pfile
, IT_IMPORT
);
808 do_include_next (cpp_reader
*pfile
)
810 enum include_type type
= IT_INCLUDE_NEXT
;
812 /* If this is the primary source file, warn and use the normal
814 if (cpp_in_primary_file (pfile
))
816 cpp_error (pfile
, CPP_DL_WARNING
,
817 "#include_next in primary source file");
820 do_include_common (pfile
, type
);
823 /* Subroutine of do_linemarker. Read possible flags after file name.
824 LAST is the last flag seen; 0 if this is the first flag. Return the
825 flag if it is valid, 0 at the end of the directive. Otherwise
828 read_flag (cpp_reader
*pfile
, unsigned int last
)
830 const cpp_token
*token
= _cpp_lex_token (pfile
);
832 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
834 unsigned int flag
= token
->val
.str
.text
[0] - '0';
836 if (flag
> last
&& flag
<= 4
837 && (flag
!= 4 || last
== 3)
838 && (flag
!= 2 || last
== 0))
842 if (token
->type
!= CPP_EOF
)
843 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
844 cpp_token_as_text (pfile
, token
));
848 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
849 of length LEN, to binary; store it in NUMP, and return false if the
850 number was well-formed, true if not. WRAPPED is set to true if the
851 number did not fit into 'unsigned long'. */
853 strtolinenum (const uchar
*str
, size_t len
, linenum_type
*nump
, bool *wrapped
)
855 linenum_type reg
= 0;
856 linenum_type reg_prev
= 0;
875 /* Interpret #line command.
876 Note that the filename string (if any) is a true string constant
877 (escapes are interpreted), unlike in #line. */
879 do_line (cpp_reader
*pfile
)
881 const struct line_maps
*line_table
= pfile
->line_table
;
882 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
884 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
887 unsigned char map_sysp
= map
->sysp
;
888 const cpp_token
*token
;
889 const char *new_file
= map
->to_file
;
890 linenum_type new_lineno
;
892 /* C99 raised the minimum limit on #line numbers. */
893 linenum_type cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
896 /* #line commands expand macros. */
897 token
= cpp_get_token (pfile
);
898 if (token
->type
!= CPP_NUMBER
899 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
900 &new_lineno
, &wrapped
))
902 if (token
->type
== CPP_EOF
)
903 cpp_error (pfile
, CPP_DL_ERROR
, "unexpected end of file after #line");
905 cpp_error (pfile
, CPP_DL_ERROR
,
906 "\"%s\" after #line is not a positive integer",
907 cpp_token_as_text (pfile
, token
));
911 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
|| wrapped
))
912 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
914 cpp_error (pfile
, CPP_DL_WARNING
, "line number out of range");
916 token
= cpp_get_token (pfile
);
917 if (token
->type
== CPP_STRING
)
919 cpp_string s
= { 0, 0 };
920 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
922 new_file
= (const char *)s
.text
;
923 check_eol (pfile
, true);
925 else if (token
->type
!= CPP_EOF
)
927 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
928 cpp_token_as_text (pfile
, token
));
932 skip_rest_of_line (pfile
);
933 _cpp_do_file_change (pfile
, LC_RENAME_VERBATIM
, new_file
, new_lineno
,
937 /* Interpret the # 44 "file" [flags] notation, which has slightly
938 different syntax and semantics from #line: Flags are allowed,
939 and we never complain about the line number being too big. */
941 do_linemarker (cpp_reader
*pfile
)
943 const struct line_maps
*line_table
= pfile
->line_table
;
944 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
945 const cpp_token
*token
;
946 const char *new_file
= map
->to_file
;
947 linenum_type new_lineno
;
948 unsigned int new_sysp
= map
->sysp
;
949 enum lc_reason reason
= LC_RENAME_VERBATIM
;
953 /* Back up so we can get the number again. Putting this in
954 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
955 some circumstances, which can segfault. */
956 _cpp_backup_tokens (pfile
, 1);
958 /* #line commands expand macros. */
959 token
= cpp_get_token (pfile
);
960 if (token
->type
!= CPP_NUMBER
961 || strtolinenum (token
->val
.str
.text
, token
->val
.str
.len
,
962 &new_lineno
, &wrapped
))
964 /* Unlike #line, there does not seem to be a way to get an EOF
965 here. So, it should be safe to always spell the token. */
966 cpp_error (pfile
, CPP_DL_ERROR
,
967 "\"%s\" after # is not a positive integer",
968 cpp_token_as_text (pfile
, token
));
972 token
= cpp_get_token (pfile
);
973 if (token
->type
== CPP_STRING
)
975 cpp_string s
= { 0, 0 };
976 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
978 new_file
= (const char *)s
.text
;
981 flag
= read_flag (pfile
, 0);
985 /* Fake an include for cpp_included (). */
986 _cpp_fake_include (pfile
, new_file
);
987 flag
= read_flag (pfile
, flag
);
992 flag
= read_flag (pfile
, flag
);
997 flag
= read_flag (pfile
, flag
);
1001 pfile
->buffer
->sysp
= new_sysp
;
1003 check_eol (pfile
, false);
1005 else if (token
->type
!= CPP_EOF
)
1007 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
1008 cpp_token_as_text (pfile
, token
));
1012 skip_rest_of_line (pfile
);
1014 /* Compensate for the increment in linemap_add that occurs in
1015 _cpp_do_file_change. We're currently at the start of the line
1016 *following* the #line directive. A separate source_location for this
1017 location makes no sense (until we do the LC_LEAVE), and
1018 complicates LAST_SOURCE_LINE_LOCATION. */
1019 pfile
->line_table
->highest_location
--;
1021 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
1024 /* Arrange the file_change callback. pfile->line has changed to
1025 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1026 header, 2 for a system header that needs to be extern "C" protected,
1027 and zero otherwise. */
1029 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
1030 const char *to_file
, linenum_type file_line
,
1033 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
1034 to_file
, file_line
);
1036 linemap_line_start (pfile
->line_table
, map
->to_line
, 127);
1038 if (pfile
->cb
.file_change
)
1039 pfile
->cb
.file_change (pfile
, map
);
1042 /* Report a warning or error detected by the program we are
1043 processing. Use the directive's tokens in the error message. */
1045 do_diagnostic (cpp_reader
*pfile
, int code
, int print_dir
)
1047 const unsigned char *dir_name
;
1048 unsigned char *line
;
1049 source_location src_loc
= pfile
->cur_token
[-1].src_loc
;
1052 dir_name
= pfile
->directive
->name
;
1055 pfile
->state
.prevent_expansion
++;
1056 line
= cpp_output_line_to_string (pfile
, dir_name
);
1057 pfile
->state
.prevent_expansion
--;
1059 cpp_error_with_line (pfile
, code
, src_loc
, 0, "%s", line
);
1064 do_error (cpp_reader
*pfile
)
1066 do_diagnostic (pfile
, CPP_DL_ERROR
, 1);
1070 do_warning (cpp_reader
*pfile
)
1072 /* We want #warning diagnostics to be emitted in system headers too. */
1073 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, 1);
1076 /* Report program identification. */
1078 do_ident (cpp_reader
*pfile
)
1080 const cpp_token
*str
= cpp_get_token (pfile
);
1082 if (str
->type
!= CPP_STRING
)
1083 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1084 pfile
->directive
->name
);
1085 else if (pfile
->cb
.ident
)
1086 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1088 check_eol (pfile
, false);
1091 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1092 matching entry, or NULL if none is found. The returned entry could
1093 be the start of a namespace chain, or a pragma. */
1094 static struct pragma_entry
*
1095 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1097 while (chain
&& chain
->pragma
!= pragma
)
1098 chain
= chain
->next
;
1103 /* Create and insert a blank pragma entry at the beginning of a
1104 singly-linked CHAIN. */
1105 static struct pragma_entry
*
1106 new_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
)
1108 struct pragma_entry
*new_entry
;
1110 new_entry
= (struct pragma_entry
*)
1111 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1113 memset (new_entry
, 0, sizeof (struct pragma_entry
));
1114 new_entry
->next
= *chain
;
1120 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1121 goes in the global namespace. */
1122 static struct pragma_entry
*
1123 register_pragma_1 (cpp_reader
*pfile
, const char *space
, const char *name
,
1124 bool allow_name_expansion
)
1126 struct pragma_entry
**chain
= &pfile
->pragmas
;
1127 struct pragma_entry
*entry
;
1128 const cpp_hashnode
*node
;
1132 node
= cpp_lookup (pfile
, UC space
, strlen (space
));
1133 entry
= lookup_pragma_entry (*chain
, node
);
1136 entry
= new_pragma_entry (pfile
, chain
);
1137 entry
->pragma
= node
;
1138 entry
->is_nspace
= true;
1139 entry
->allow_expansion
= allow_name_expansion
;
1141 else if (!entry
->is_nspace
)
1143 else if (entry
->allow_expansion
!= allow_name_expansion
)
1145 cpp_error (pfile
, CPP_DL_ICE
,
1146 "registering pragmas in namespace \"%s\" with mismatched "
1147 "name expansion", space
);
1150 chain
= &entry
->u
.space
;
1152 else if (allow_name_expansion
)
1154 cpp_error (pfile
, CPP_DL_ICE
,
1155 "registering pragma \"%s\" with name expansion "
1156 "and no namespace", name
);
1160 /* Check for duplicates. */
1161 node
= cpp_lookup (pfile
, UC name
, strlen (name
));
1162 entry
= lookup_pragma_entry (*chain
, node
);
1165 entry
= new_pragma_entry (pfile
, chain
);
1166 entry
->pragma
= node
;
1170 if (entry
->is_nspace
)
1172 cpp_error (pfile
, CPP_DL_ICE
,
1173 "registering \"%s\" as both a pragma and a pragma namespace",
1176 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1179 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1184 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1186 register_pragma_internal (cpp_reader
*pfile
, const char *space
,
1187 const char *name
, pragma_cb handler
)
1189 struct pragma_entry
*entry
;
1191 entry
= register_pragma_1 (pfile
, space
, name
, false);
1192 entry
->is_internal
= true;
1193 entry
->u
.handler
= handler
;
1196 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1197 goes in the global namespace. HANDLER is the handler it will call,
1198 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1199 expansion while parsing pragma NAME. This function is exported
1202 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1203 pragma_cb handler
, bool allow_expansion
)
1205 struct pragma_entry
*entry
;
1209 cpp_error (pfile
, CPP_DL_ICE
, "registering pragma with NULL handler");
1213 entry
= register_pragma_1 (pfile
, space
, name
, false);
1216 entry
->allow_expansion
= allow_expansion
;
1217 entry
->u
.handler
= handler
;
1221 /* Similarly, but create mark the pragma for deferred processing.
1222 When found, a CPP_PRAGMA token will be insertted into the stream
1223 with IDENT in the token->u.pragma slot. */
1225 cpp_register_deferred_pragma (cpp_reader
*pfile
, const char *space
,
1226 const char *name
, unsigned int ident
,
1227 bool allow_expansion
, bool allow_name_expansion
)
1229 struct pragma_entry
*entry
;
1231 entry
= register_pragma_1 (pfile
, space
, name
, allow_name_expansion
);
1234 entry
->is_deferred
= true;
1235 entry
->allow_expansion
= allow_expansion
;
1236 entry
->u
.ident
= ident
;
1240 /* Register the pragmas the preprocessor itself handles. */
1242 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1244 /* Pragmas in the global namespace. */
1245 register_pragma_internal (pfile
, 0, "once", do_pragma_once
);
1247 /* New GCC-specific pragmas should be put in the GCC namespace. */
1248 register_pragma_internal (pfile
, "GCC", "poison", do_pragma_poison
);
1249 register_pragma_internal (pfile
, "GCC", "system_header",
1250 do_pragma_system_header
);
1251 register_pragma_internal (pfile
, "GCC", "dependency", do_pragma_dependency
);
1254 /* Return the number of registered pragmas in PE. */
1257 count_registered_pragmas (struct pragma_entry
*pe
)
1260 for (; pe
!= NULL
; pe
= pe
->next
)
1263 ct
+= count_registered_pragmas (pe
->u
.space
);
1269 /* Save into SD the names of the registered pragmas referenced by PE,
1270 and return a pointer to the next free space in SD. */
1273 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1275 for (; pe
!= NULL
; pe
= pe
->next
)
1278 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1279 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1280 HT_LEN (&pe
->pragma
->ident
),
1281 HT_LEN (&pe
->pragma
->ident
) + 1);
1286 /* Return a newly-allocated array which saves the names of the
1287 registered pragmas. */
1290 _cpp_save_pragma_names (cpp_reader
*pfile
)
1292 int ct
= count_registered_pragmas (pfile
->pragmas
);
1293 char **result
= XNEWVEC (char *, ct
);
1294 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1298 /* Restore from SD the names of the registered pragmas referenced by PE,
1299 and return a pointer to the next unused name in SD. */
1302 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1305 for (; pe
!= NULL
; pe
= pe
->next
)
1308 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1309 pe
->pragma
= cpp_lookup (pfile
, UC
*sd
, strlen (*sd
));
1316 /* Restore the names of the registered pragmas from SAVED. */
1319 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1321 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1325 /* Pragmata handling. We handle some, and pass the rest on to the
1326 front end. C99 defines three pragmas and says that no macro
1327 expansion is to be performed on them; whether or not macro
1328 expansion happens for other pragmas is implementation defined.
1329 This implementation allows for a mix of both, since GCC did not
1330 traditionally macro expand its (few) pragmas, whereas OpenMP
1331 specifies that macro expansion should happen. */
1333 do_pragma (cpp_reader
*pfile
)
1335 const struct pragma_entry
*p
= NULL
;
1336 const cpp_token
*token
, *pragma_token
= pfile
->cur_token
;
1338 unsigned int count
= 1;
1340 pfile
->state
.prevent_expansion
++;
1342 token
= cpp_get_token (pfile
);
1344 if (token
->type
== CPP_NAME
)
1346 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
.node
);
1347 if (p
&& p
->is_nspace
)
1349 bool allow_name_expansion
= p
->allow_expansion
;
1350 if (allow_name_expansion
)
1351 pfile
->state
.prevent_expansion
--;
1352 token
= cpp_get_token (pfile
);
1353 if (token
->type
== CPP_NAME
)
1354 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
.node
);
1357 if (allow_name_expansion
)
1358 pfile
->state
.prevent_expansion
++;
1367 pfile
->directive_result
.src_loc
= pragma_token
->src_loc
;
1368 pfile
->directive_result
.type
= CPP_PRAGMA
;
1369 pfile
->directive_result
.flags
= pragma_token
->flags
;
1370 pfile
->directive_result
.val
.pragma
= p
->u
.ident
;
1371 pfile
->state
.in_deferred_pragma
= true;
1372 pfile
->state
.pragma_allow_expansion
= p
->allow_expansion
;
1373 if (!p
->allow_expansion
)
1374 pfile
->state
.prevent_expansion
++;
1378 /* Since the handler below doesn't get the line number, that
1379 it might need for diagnostics, make sure it has the right
1380 numbers in place. */
1381 if (pfile
->cb
.line_change
)
1382 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1383 if (p
->allow_expansion
)
1384 pfile
->state
.prevent_expansion
--;
1385 (*p
->u
.handler
) (pfile
);
1386 if (p
->allow_expansion
)
1387 pfile
->state
.prevent_expansion
++;
1390 else if (pfile
->cb
.def_pragma
)
1392 if (count
== 1 || pfile
->context
->prev
== NULL
)
1393 _cpp_backup_tokens (pfile
, count
);
1396 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1397 won't allow backing 2 tokens. */
1398 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1399 reads both tokens, we could perhaps free it, but if it doesn't,
1400 we don't know the exact lifespan. */
1401 cpp_token
*toks
= XNEWVEC (cpp_token
, 2);
1403 toks
[0].flags
|= NO_EXPAND
;
1405 toks
[1].flags
|= NO_EXPAND
;
1406 _cpp_push_token_context (pfile
, NULL
, toks
, 2);
1408 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1411 pfile
->state
.prevent_expansion
--;
1414 /* Handle #pragma once. */
1416 do_pragma_once (cpp_reader
*pfile
)
1418 if (cpp_in_primary_file (pfile
))
1419 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1421 check_eol (pfile
, false);
1422 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1425 /* Handle #pragma GCC poison, to poison one or more identifiers so
1426 that the lexer produces a hard error for each subsequent usage. */
1428 do_pragma_poison (cpp_reader
*pfile
)
1430 const cpp_token
*tok
;
1433 pfile
->state
.poisoned_ok
= 1;
1436 tok
= _cpp_lex_token (pfile
);
1437 if (tok
->type
== CPP_EOF
)
1439 if (tok
->type
!= CPP_NAME
)
1441 cpp_error (pfile
, CPP_DL_ERROR
,
1442 "invalid #pragma GCC poison directive");
1446 hp
= tok
->val
.node
.node
;
1447 if (hp
->flags
& NODE_POISONED
)
1450 if (hp
->type
== NT_MACRO
)
1451 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1453 _cpp_free_definition (hp
);
1454 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1456 pfile
->state
.poisoned_ok
= 0;
1459 /* Mark the current header as a system header. This will suppress
1460 some categories of warnings (notably those from -pedantic). It is
1461 intended for use in system libraries that cannot be implemented in
1462 conforming C, but cannot be certain that their headers appear in a
1463 system include directory. To prevent abuse, it is rejected in the
1464 primary source file. */
1466 do_pragma_system_header (cpp_reader
*pfile
)
1468 if (cpp_in_primary_file (pfile
))
1469 cpp_error (pfile
, CPP_DL_WARNING
,
1470 "#pragma system_header ignored outside include file");
1473 check_eol (pfile
, false);
1474 skip_rest_of_line (pfile
);
1475 cpp_make_system_header (pfile
, 1, 0);
1479 /* Check the modified date of the current include file against a specified
1480 file. Issue a diagnostic, if the specified file is newer. We use this to
1481 determine if a fixed header should be refixed. */
1483 do_pragma_dependency (cpp_reader
*pfile
)
1486 int angle_brackets
, ordering
;
1487 source_location location
;
1489 fname
= parse_include (pfile
, &angle_brackets
, NULL
, &location
);
1493 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1495 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1496 else if (ordering
> 0)
1498 cpp_error (pfile
, CPP_DL_WARNING
,
1499 "current file is older than %s", fname
);
1500 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1502 _cpp_backup_tokens (pfile
, 1);
1503 do_diagnostic (pfile
, CPP_DL_WARNING
, 0);
1507 free ((void *) fname
);
1510 /* Get a token but skip padding. */
1511 static const cpp_token
*
1512 get_token_no_padding (cpp_reader
*pfile
)
1516 const cpp_token
*result
= cpp_get_token (pfile
);
1517 if (result
->type
!= CPP_PADDING
)
1522 /* Check syntax is "(string-literal)". Returns the string on success,
1523 or NULL on failure. */
1524 static const cpp_token
*
1525 get__Pragma_string (cpp_reader
*pfile
)
1527 const cpp_token
*string
;
1528 const cpp_token
*paren
;
1530 paren
= get_token_no_padding (pfile
);
1531 if (paren
->type
== CPP_EOF
)
1532 _cpp_backup_tokens (pfile
, 1);
1533 if (paren
->type
!= CPP_OPEN_PAREN
)
1536 string
= get_token_no_padding (pfile
);
1537 if (string
->type
== CPP_EOF
)
1538 _cpp_backup_tokens (pfile
, 1);
1539 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
1540 && string
->type
!= CPP_STRING32
&& string
->type
!= CPP_STRING16
)
1543 paren
= get_token_no_padding (pfile
);
1544 if (paren
->type
== CPP_EOF
)
1545 _cpp_backup_tokens (pfile
, 1);
1546 if (paren
->type
!= CPP_CLOSE_PAREN
)
1552 /* Destringize IN into a temporary buffer, by removing the first \ of
1553 \" and \\ sequences, and process the result as a #pragma directive. */
1555 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
)
1557 const unsigned char *src
, *limit
;
1558 char *dest
, *result
;
1559 cpp_context
*saved_context
;
1560 cpp_token
*saved_cur_token
;
1561 tokenrun
*saved_cur_run
;
1564 const struct directive
*save_directive
;
1566 dest
= result
= (char *) alloca (in
->len
- 1);
1567 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1568 limit
= in
->text
+ in
->len
- 1;
1571 /* We know there is a character following the backslash. */
1572 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1578 /* Ugh; an awful kludge. We are really not set up to be lexing
1579 tokens when in the middle of a macro expansion. Use a new
1580 context to force cpp_get_token to lex, and so skip_rest_of_line
1581 doesn't go beyond the end of the text. Also, remember the
1582 current lexing position so we can return to it later.
1584 Something like line-at-a-time lexing should remove the need for
1586 saved_context
= pfile
->context
;
1587 saved_cur_token
= pfile
->cur_token
;
1588 saved_cur_run
= pfile
->cur_run
;
1590 pfile
->context
= XNEW (cpp_context
);
1591 pfile
->context
->macro
= 0;
1592 pfile
->context
->prev
= 0;
1593 pfile
->context
->next
= 0;
1595 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1596 until we've read all of the tokens that we want. */
1597 cpp_push_buffer (pfile
, (const uchar
*) result
, dest
- result
,
1598 /* from_stage3 */ true);
1599 /* ??? Antique Disgusting Hack. What does this do? */
1600 if (pfile
->buffer
->prev
)
1601 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
1603 start_directive (pfile
);
1604 _cpp_clean_line (pfile
);
1605 save_directive
= pfile
->directive
;
1606 pfile
->directive
= &dtable
[T_PRAGMA
];
1608 end_directive (pfile
, 1);
1609 pfile
->directive
= save_directive
;
1611 /* We always insert at least one token, the directive result. It'll
1612 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1613 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1615 /* If we're not handling the pragma internally, read all of the tokens from
1616 the string buffer now, while the string buffer is still installed. */
1617 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1618 to me what the true lifespan of the tokens are. It would appear that
1619 the lifespan is the entire parse of the main input stream, in which case
1620 this may not be wrong. */
1621 if (pfile
->directive_result
.type
== CPP_PRAGMA
)
1627 toks
= XNEWVEC (cpp_token
, maxcount
);
1628 toks
[0] = pfile
->directive_result
;
1632 if (count
== maxcount
)
1634 maxcount
= maxcount
* 3 / 2;
1635 toks
= XRESIZEVEC (cpp_token
, toks
, maxcount
);
1637 toks
[count
] = *cpp_get_token (pfile
);
1638 /* Macros have been already expanded by cpp_get_token
1639 if the pragma allowed expansion. */
1640 toks
[count
++].flags
|= NO_EXPAND
;
1642 while (toks
[count
-1].type
!= CPP_PRAGMA_EOL
);
1647 toks
= XNEW (cpp_token
);
1648 toks
[0] = pfile
->directive_result
;
1650 /* If we handled the entire pragma internally, make sure we get the
1651 line number correct for the next token. */
1652 if (pfile
->cb
.line_change
)
1653 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1656 /* Finish inlining run_directive. */
1657 pfile
->buffer
->file
= NULL
;
1658 _cpp_pop_buffer (pfile
);
1660 /* Reset the old macro state before ... */
1661 XDELETE (pfile
->context
);
1662 pfile
->context
= saved_context
;
1663 pfile
->cur_token
= saved_cur_token
;
1664 pfile
->cur_run
= saved_cur_run
;
1666 /* ... inserting the new tokens we collected. */
1667 _cpp_push_token_context (pfile
, NULL
, toks
, count
);
1670 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1672 _cpp_do__Pragma (cpp_reader
*pfile
)
1674 const cpp_token
*string
= get__Pragma_string (pfile
);
1675 pfile
->directive_result
.type
= CPP_PADDING
;
1679 destringize_and_run (pfile
, &string
->val
.str
);
1682 cpp_error (pfile
, CPP_DL_ERROR
,
1683 "_Pragma takes a parenthesized string literal");
1687 /* Handle #ifdef. */
1689 do_ifdef (cpp_reader
*pfile
)
1693 if (! pfile
->state
.skipping
)
1695 cpp_hashnode
*node
= lex_macro_node (pfile
, false);
1699 skip
= node
->type
!= NT_MACRO
;
1700 _cpp_mark_macro_used (node
);
1701 if (!(node
->flags
& NODE_USED
))
1703 node
->flags
|= NODE_USED
;
1704 if (node
->type
== NT_MACRO
)
1706 if (pfile
->cb
.used_define
)
1707 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1711 if (pfile
->cb
.used_undef
)
1712 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1715 check_eol (pfile
, false);
1719 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1722 /* Handle #ifndef. */
1724 do_ifndef (cpp_reader
*pfile
)
1727 cpp_hashnode
*node
= 0;
1729 if (! pfile
->state
.skipping
)
1731 node
= lex_macro_node (pfile
, false);
1735 skip
= node
->type
== NT_MACRO
;
1736 _cpp_mark_macro_used (node
);
1737 if (!(node
->flags
& NODE_USED
))
1739 node
->flags
|= NODE_USED
;
1740 if (node
->type
== NT_MACRO
)
1742 if (pfile
->cb
.used_define
)
1743 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
1747 if (pfile
->cb
.used_undef
)
1748 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
1751 check_eol (pfile
, false);
1755 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1758 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1759 pfile->mi_ind_cmacro so we can handle multiple-include
1760 optimizations. If macro expansion occurs in the expression, we
1761 cannot treat it as a controlling conditional, since the expansion
1762 could change in the future. That is handled by cpp_get_token. */
1764 do_if (cpp_reader
*pfile
)
1768 if (! pfile
->state
.skipping
)
1769 skip
= _cpp_parse_expr (pfile
, true) == false;
1771 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1774 /* Flip skipping state if appropriate and continue without changing
1775 if_stack; this is so that the error message for missing #endif's
1776 etc. will point to the original #if. */
1778 do_else (cpp_reader
*pfile
)
1780 cpp_buffer
*buffer
= pfile
->buffer
;
1781 struct if_stack
*ifs
= buffer
->if_stack
;
1784 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
1787 if (ifs
->type
== T_ELSE
)
1789 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
1790 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1791 "the conditional began here");
1795 /* Skip any future (erroneous) #elses or #elifs. */
1796 pfile
->state
.skipping
= ifs
->skip_elses
;
1797 ifs
->skip_elses
= true;
1799 /* Invalidate any controlling macro. */
1802 /* Only check EOL if was not originally skipping. */
1803 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1804 check_eol (pfile
, false);
1808 /* Handle a #elif directive by not changing if_stack either. See the
1809 comment above do_else. */
1811 do_elif (cpp_reader
*pfile
)
1813 cpp_buffer
*buffer
= pfile
->buffer
;
1814 struct if_stack
*ifs
= buffer
->if_stack
;
1817 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
1820 if (ifs
->type
== T_ELSE
)
1822 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
1823 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1824 "the conditional began here");
1828 if (! ifs
->was_skipping
)
1831 /* The standard mandates that the expression be parsed even
1832 if we are skipping elses at this point -- the lexical
1833 restrictions on #elif only apply to skipped groups, but
1834 this group is not being skipped. Temporarily set
1835 skipping to false to get lexer warnings. */
1836 pfile
->state
.skipping
= 0;
1837 value
= _cpp_parse_expr (pfile
, false);
1838 if (ifs
->skip_elses
)
1839 pfile
->state
.skipping
= 1;
1842 pfile
->state
.skipping
= ! value
;
1843 ifs
->skip_elses
= value
;
1847 /* Invalidate any controlling macro. */
1852 /* #endif pops the if stack and resets pfile->state.skipping. */
1854 do_endif (cpp_reader
*pfile
)
1856 cpp_buffer
*buffer
= pfile
->buffer
;
1857 struct if_stack
*ifs
= buffer
->if_stack
;
1860 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
1863 /* Only check EOL if was not originally skipping. */
1864 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1865 check_eol (pfile
, false);
1867 /* If potential control macro, we go back outside again. */
1868 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1870 pfile
->mi_valid
= true;
1871 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1874 buffer
->if_stack
= ifs
->next
;
1875 pfile
->state
.skipping
= ifs
->was_skipping
;
1876 obstack_free (&pfile
->buffer_ob
, ifs
);
1880 /* Push an if_stack entry for a preprocessor conditional, and set
1881 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1882 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1883 we need to check here that we are at the top of the file. */
1885 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
1886 const cpp_hashnode
*cmacro
)
1888 struct if_stack
*ifs
;
1889 cpp_buffer
*buffer
= pfile
->buffer
;
1891 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
1892 ifs
->line
= pfile
->directive_line
;
1893 ifs
->next
= buffer
->if_stack
;
1894 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1895 ifs
->was_skipping
= pfile
->state
.skipping
;
1897 /* This condition is effectively a test for top-of-file. */
1898 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1899 ifs
->mi_cmacro
= cmacro
;
1903 pfile
->state
.skipping
= skip
;
1904 buffer
->if_stack
= ifs
;
1907 /* Read the tokens of the answer into the macro pool, in a directive
1908 of type TYPE. Only commit the memory if we intend it as permanent
1909 storage, i.e. the #assert case. Returns 0 on success, and sets
1910 ANSWERP to point to the answer. PRED_LOC is the location of the
1913 parse_answer (cpp_reader
*pfile
, struct answer
**answerp
, int type
,
1914 source_location pred_loc
)
1916 const cpp_token
*paren
;
1917 struct answer
*answer
;
1918 unsigned int acount
;
1920 /* In a conditional, it is legal to not have an open paren. We
1921 should save the following token in this case. */
1922 paren
= cpp_get_token (pfile
);
1924 /* If not a paren, see if we're OK. */
1925 if (paren
->type
!= CPP_OPEN_PAREN
)
1927 /* In a conditional no answer is a test for any answer. It
1928 could be followed by any token. */
1931 _cpp_backup_tokens (pfile
, 1);
1935 /* #unassert with no answer is valid - it removes all answers. */
1936 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1939 cpp_error_with_line (pfile
, CPP_DL_ERROR
, pred_loc
, 0,
1940 "missing '(' after predicate");
1944 for (acount
= 0;; acount
++)
1947 const cpp_token
*token
= cpp_get_token (pfile
);
1950 if (token
->type
== CPP_CLOSE_PAREN
)
1953 if (token
->type
== CPP_EOF
)
1955 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
1959 /* struct answer includes the space for one token. */
1960 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1962 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1963 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1965 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1968 /* Drop whitespace at start, for answer equivalence purposes. */
1970 dest
->flags
&= ~PREV_WHITE
;
1975 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
1979 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1980 answer
->count
= acount
;
1981 answer
->next
= NULL
;
1987 /* Parses an assertion directive of type TYPE, returning a pointer to
1988 the hash node of the predicate, or 0 on error. If an answer was
1989 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1990 static cpp_hashnode
*
1991 parse_assertion (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
1993 cpp_hashnode
*result
= 0;
1994 const cpp_token
*predicate
;
1996 /* We don't expand predicates or answers. */
1997 pfile
->state
.prevent_expansion
++;
2000 predicate
= cpp_get_token (pfile
);
2001 if (predicate
->type
== CPP_EOF
)
2002 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
2003 else if (predicate
->type
!= CPP_NAME
)
2004 cpp_error_with_line (pfile
, CPP_DL_ERROR
, predicate
->src_loc
, 0,
2005 "predicate must be an identifier");
2006 else if (parse_answer (pfile
, answerp
, type
, predicate
->src_loc
) == 0)
2008 unsigned int len
= NODE_LEN (predicate
->val
.node
.node
);
2009 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
2011 /* Prefix '#' to get it out of macro namespace. */
2013 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
.node
), len
);
2014 result
= cpp_lookup (pfile
, sym
, len
+ 1);
2017 pfile
->state
.prevent_expansion
--;
2021 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2022 or a pointer to NULL if the answer is not in the chain. */
2023 static struct answer
**
2024 find_answer (cpp_hashnode
*node
, const struct answer
*candidate
)
2027 struct answer
**result
;
2029 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
2031 struct answer
*answer
= *result
;
2033 if (answer
->count
== candidate
->count
)
2035 for (i
= 0; i
< answer
->count
; i
++)
2036 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
2039 if (i
== answer
->count
)
2047 /* Test an assertion within a preprocessor conditional. Returns
2048 nonzero on failure, zero on success. On success, the result of
2049 the test is written into VALUE, otherwise the value 0. */
2051 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
2053 struct answer
*answer
;
2056 node
= parse_assertion (pfile
, &answer
, T_IF
);
2058 /* For recovery, an erroneous assertion expression is handled as a
2059 failing assertion. */
2063 *value
= (node
->type
== NT_ASSERTION
&&
2064 (answer
== 0 || *find_answer (node
, answer
) != 0));
2065 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
2066 _cpp_backup_tokens (pfile
, 1);
2068 /* We don't commit the memory for the answer - it's temporary only. */
2072 /* Handle #assert. */
2074 do_assert (cpp_reader
*pfile
)
2076 struct answer
*new_answer
;
2079 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
2084 /* Place the new answer in the answer list. First check there
2085 is not a duplicate. */
2086 new_answer
->next
= 0;
2087 if (node
->type
== NT_ASSERTION
)
2089 if (*find_answer (node
, new_answer
))
2091 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
2092 NODE_NAME (node
) + 1);
2095 new_answer
->next
= node
->value
.answers
;
2098 answer_size
= sizeof (struct answer
) + ((new_answer
->count
- 1)
2099 * sizeof (cpp_token
));
2100 /* Commit or allocate storage for the object. */
2101 if (pfile
->hash_table
->alloc_subobject
)
2103 struct answer
*temp_answer
= new_answer
;
2104 new_answer
= (struct answer
*) pfile
->hash_table
->alloc_subobject
2106 memcpy (new_answer
, temp_answer
, answer_size
);
2109 BUFF_FRONT (pfile
->a_buff
) += answer_size
;
2111 node
->type
= NT_ASSERTION
;
2112 node
->value
.answers
= new_answer
;
2113 check_eol (pfile
, false);
2117 /* Handle #unassert. */
2119 do_unassert (cpp_reader
*pfile
)
2122 struct answer
*answer
;
2124 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
2125 /* It isn't an error to #unassert something that isn't asserted. */
2126 if (node
&& node
->type
== NT_ASSERTION
)
2130 struct answer
**p
= find_answer (node
, answer
), *temp
;
2132 /* Remove the answer from the list. */
2137 /* Did we free the last answer? */
2138 if (node
->value
.answers
== 0)
2139 node
->type
= NT_VOID
;
2141 check_eol (pfile
, false);
2144 _cpp_free_definition (node
);
2147 /* We don't commit the memory for the answer - it's temporary only. */
2150 /* These are for -D, -U, -A. */
2152 /* Process the string STR as if it appeared as the body of a #define.
2153 If STR is just an identifier, define it with value 1.
2154 If STR has anything after the identifier, then it should
2155 be identifier=definition. */
2157 cpp_define (cpp_reader
*pfile
, const char *str
)
2162 /* Copy the entire option so we can modify it.
2163 Change the first "=" in the string to a space. If there is none,
2164 tack " 1" on the end. */
2166 count
= strlen (str
);
2167 buf
= (char *) alloca (count
+ 3);
2168 memcpy (buf
, str
, count
);
2170 p
= strchr (str
, '=');
2180 run_directive (pfile
, T_DEFINE
, buf
, count
);
2184 /* Use to build macros to be run through cpp_define() as
2186 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2189 cpp_define_formatted (cpp_reader
*pfile
, const char *fmt
, ...)
2195 vasprintf (&ptr
, fmt
, ap
);
2198 cpp_define (pfile
, ptr
);
2203 /* Slight variant of the above for use by initialize_builtins. */
2205 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2207 size_t len
= strlen (str
);
2208 char *buf
= (char *) alloca (len
+ 1);
2209 memcpy (buf
, str
, len
);
2211 run_directive (pfile
, T_DEFINE
, buf
, len
);
2214 /* Process MACRO as if it appeared as the body of an #undef. */
2216 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2218 size_t len
= strlen (macro
);
2219 char *buf
= (char *) alloca (len
+ 1);
2220 memcpy (buf
, macro
, len
);
2222 run_directive (pfile
, T_UNDEF
, buf
, len
);
2225 /* Like lex_macro_node, but read the input from STR. */
2226 static cpp_hashnode
*
2227 lex_macro_node_from_str (cpp_reader
*pfile
, const char *str
)
2229 size_t len
= strlen (str
);
2230 uchar
*buf
= (uchar
*) alloca (len
+ 1);
2233 memcpy (buf
, str
, len
);
2235 cpp_push_buffer (pfile
, buf
, len
, true);
2236 node
= lex_macro_node (pfile
, true);
2237 _cpp_pop_buffer (pfile
);
2242 /* If STR is a defined macro, return its definition node, else return NULL. */
2244 cpp_push_definition (cpp_reader
*pfile
, const char *str
)
2246 cpp_hashnode
*node
= lex_macro_node_from_str (pfile
, str
);
2247 if (node
&& node
->type
== NT_MACRO
)
2248 return node
->value
.macro
;
2253 /* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2254 then the macro should be undefined. */
2256 cpp_pop_definition (cpp_reader
*pfile
, const char *str
, cpp_macro
*dfn
)
2258 cpp_hashnode
*node
= lex_macro_node_from_str (pfile
, str
);
2262 if (pfile
->cb
.before_define
)
2263 pfile
->cb
.before_define (pfile
);
2265 if (node
->type
== NT_MACRO
)
2267 if (pfile
->cb
.undef
)
2268 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
2269 if (CPP_OPTION (pfile
, warn_unused_macros
))
2270 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
2272 if (node
->type
!= NT_VOID
)
2273 _cpp_free_definition (node
);
2277 node
->type
= NT_MACRO
;
2278 node
->value
.macro
= dfn
;
2279 if (! ustrncmp (NODE_NAME (node
), DSC ("__STDC_")))
2280 node
->flags
|= NODE_WARN
;
2282 if (pfile
->cb
.define
)
2283 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
2287 /* Process the string STR as if it appeared as the body of a #assert. */
2289 cpp_assert (cpp_reader
*pfile
, const char *str
)
2291 handle_assertion (pfile
, str
, T_ASSERT
);
2294 /* Process STR as if it appeared as the body of an #unassert. */
2296 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2298 handle_assertion (pfile
, str
, T_UNASSERT
);
2301 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2303 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2305 size_t count
= strlen (str
);
2306 const char *p
= strchr (str
, '=');
2308 /* Copy the entire option so we can modify it. Change the first
2309 "=" in the string to a '(', and tack a ')' on the end. */
2310 char *buf
= (char *) alloca (count
+ 2);
2312 memcpy (buf
, str
, count
);
2321 run_directive (pfile
, type
, str
, count
);
2324 /* The options structure. */
2326 cpp_get_options (cpp_reader
*pfile
)
2328 return &pfile
->opts
;
2331 /* The callbacks structure. */
2333 cpp_get_callbacks (cpp_reader
*pfile
)
2338 /* Copy the given callbacks structure to our own. */
2340 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2345 /* The dependencies structure. (Creates one if it hasn't already been.) */
2347 cpp_get_deps (cpp_reader
*pfile
)
2350 pfile
->deps
= deps_init ();
2354 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2355 doesn't fail. It does not generate a file change call back; that
2356 is the responsibility of the caller. */
2358 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2361 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2363 /* Clears, amongst other things, if_stack and mi_cmacro. */
2364 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2366 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2367 new_buffer
->rlimit
= buffer
+ len
;
2368 new_buffer
->from_stage3
= from_stage3
;
2369 new_buffer
->prev
= pfile
->buffer
;
2370 new_buffer
->need_line
= true;
2372 pfile
->buffer
= new_buffer
;
2377 /* Pops a single buffer, with a file change call-back if appropriate.
2378 Then pushes the next -include file, if any remain. */
2380 _cpp_pop_buffer (cpp_reader
*pfile
)
2382 cpp_buffer
*buffer
= pfile
->buffer
;
2383 struct _cpp_file
*inc
= buffer
->file
;
2384 struct if_stack
*ifs
;
2386 /* Walk back up the conditional stack till we reach its level at
2387 entry to this file, issuing error messages. */
2388 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2389 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2390 "unterminated #%s", dtable
[ifs
->type
].name
);
2392 /* In case of a missing #endif. */
2393 pfile
->state
.skipping
= 0;
2395 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2396 pfile
->buffer
= buffer
->prev
;
2398 free (buffer
->notes
);
2400 /* Free the buffer object now; we may want to push a new buffer
2401 in _cpp_push_next_include_file. */
2402 obstack_free (&pfile
->buffer_ob
, buffer
);
2406 _cpp_pop_file_buffer (pfile
, inc
);
2408 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2412 /* Enter all recognized directives in the hash table. */
2414 _cpp_init_directives (cpp_reader
*pfile
)
2419 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2421 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2422 node
->is_directive
= 1;
2423 node
->directive_index
= i
;