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, 2006
4 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 2, 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; if not, write to the Free Software
21 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
34 struct if_stack
*next
;
35 unsigned int 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. */
53 struct pragma_entry
*space
;
57 /* Values for the origin field of struct directive. KANDR directives
58 come from traditional (K&R) C. STDC89 directives come from the
59 1989 C standard. EXTENSION directives are extensions. */
64 /* Values for the flags field of struct directive. COND indicates a
65 conditional; IF_COND an opening conditional. INCL means to treat
66 "..." and <...> as q-char and h-char sequences respectively. IN_I
67 means this directive should be handled even if -fpreprocessed is in
68 effect (these are the directives with callback hooks).
70 EXPAND is set on directives that are always macro-expanded. */
72 #define IF_COND (1 << 1)
75 #define EXPAND (1 << 4)
77 /* Defines one #-directive, including how to handle it. */
78 typedef void (*directive_handler
) (cpp_reader
*);
79 typedef struct directive directive
;
82 directive_handler handler
; /* Function to handle directive. */
83 const uchar
*name
; /* Name of directive. */
84 unsigned short length
; /* Length of name. */
85 unsigned char origin
; /* Origin of directive. */
86 unsigned char flags
; /* Flags describing this directive. */
89 /* Forward declarations. */
91 static void skip_rest_of_line (cpp_reader
*);
92 static void check_eol (cpp_reader
*);
93 static void start_directive (cpp_reader
*);
94 static void prepare_directive_trad (cpp_reader
*);
95 static void end_directive (cpp_reader
*, int);
96 static void directive_diagnostics (cpp_reader
*, const directive
*, int);
97 static void run_directive (cpp_reader
*, int, const char *, size_t);
98 static char *glue_header_name (cpp_reader
*);
99 static const char *parse_include (cpp_reader
*, int *, const cpp_token
***);
100 static void push_conditional (cpp_reader
*, int, int, const cpp_hashnode
*);
101 static unsigned int read_flag (cpp_reader
*, unsigned int);
102 static int strtoul_for_line (const uchar
*, unsigned int, unsigned long *);
103 static void do_diagnostic (cpp_reader
*, int, int);
104 static cpp_hashnode
*lex_macro_node (cpp_reader
*);
105 static int undefine_macros (cpp_reader
*, cpp_hashnode
*, void *);
106 static void do_include_common (cpp_reader
*, enum include_type
);
107 static struct pragma_entry
*lookup_pragma_entry (struct pragma_entry
*,
108 const cpp_hashnode
*);
109 static struct pragma_entry
*insert_pragma_entry (cpp_reader
*,
110 struct pragma_entry
**,
111 const cpp_hashnode
*,
114 static void register_pragma (cpp_reader
*, const char *, const char *,
115 pragma_cb
, bool, bool);
116 static int count_registered_pragmas (struct pragma_entry
*);
117 static char ** save_registered_pragmas (struct pragma_entry
*, char **);
118 static char ** restore_registered_pragmas (cpp_reader
*, struct pragma_entry
*,
120 static void do_pragma_once (cpp_reader
*);
121 static void do_pragma_poison (cpp_reader
*);
122 static void do_pragma_system_header (cpp_reader
*);
123 static void do_pragma_dependency (cpp_reader
*);
124 static void do_linemarker (cpp_reader
*);
125 static const cpp_token
*get_token_no_padding (cpp_reader
*);
126 static const cpp_token
*get__Pragma_string (cpp_reader
*);
127 static void destringize_and_run (cpp_reader
*, const cpp_string
*);
128 static int parse_answer (cpp_reader
*, struct answer
**, int);
129 static cpp_hashnode
*parse_assertion (cpp_reader
*, struct answer
**, int);
130 static struct answer
** find_answer (cpp_hashnode
*, const struct answer
*);
131 static void handle_assertion (cpp_reader
*, const char *, int);
133 /* This is the table of directive handlers. It is ordered by
134 frequency of occurrence; the numbers at the end are directive
135 counts from all the source code I have lying around (egcs and libc
136 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
137 pcmcia-cs-3.0.9). This is no longer important as directive lookup
138 is now O(1). All extensions other than #warning and #include_next
139 are deprecated. The name is where the extension appears to have
142 #define DIRECTIVE_TABLE \
143 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
144 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
145 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
146 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
147 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
148 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
149 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
150 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
151 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
152 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
153 D(error, T_ERROR, STDC89, 0) /* 475 */ \
154 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
155 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
156 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
157 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
158 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
159 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
160 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
161 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
163 /* #sccs is synonymous with #ident. */
164 #define do_sccs do_ident
166 /* Use the table to generate a series of prototypes, an enum for the
167 directive names, and an array of directive handlers. */
169 #define D(name, t, o, f) static void do_##name (cpp_reader *);
173 #define D(n, tag, o, f) tag,
181 #define D(name, t, origin, flags) \
182 { do_##name, (const uchar *) #name, \
183 sizeof #name - 1, origin, flags },
184 static const directive dtable
[] =
189 #undef DIRECTIVE_TABLE
191 /* Wrapper struct directive for linemarkers.
192 The origin is more or less true - the original K+R cpp
193 did use this notation in its preprocessed output. */
194 static const directive linemarker_dir
=
196 do_linemarker
, U
"#", 1, KANDR
, IN_I
199 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
201 /* Skip any remaining tokens in a directive. */
203 skip_rest_of_line (cpp_reader
*pfile
)
205 /* Discard all stacked contexts. */
206 while (pfile
->context
->prev
)
207 _cpp_pop_context (pfile
);
209 /* Sweep up all tokens remaining on the line. */
211 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
215 /* Ensure there are no stray tokens at the end of a directive. */
217 check_eol (cpp_reader
*pfile
)
219 if (! SEEN_EOL () && _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 (CPP_OPTION (pfile
, traditional
))
283 /* Revert change of prepare_directive_trad. */
284 pfile
->state
.prevent_expansion
--;
286 if (pfile
->directive
!= &dtable
[T_DEFINE
])
287 _cpp_remove_overlay (pfile
);
289 /* We don't skip for an assembler #. */
292 skip_rest_of_line (pfile
);
293 if (!pfile
->keep_tokens
)
295 pfile
->cur_run
= &pfile
->base_run
;
296 pfile
->cur_token
= pfile
->base_run
.base
;
301 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
302 pfile
->state
.in_directive
= 0;
303 pfile
->state
.in_expression
= 0;
304 pfile
->state
.angled_headers
= 0;
305 pfile
->directive
= 0;
308 /* Prepare to handle the directive in pfile->directive. */
310 prepare_directive_trad (cpp_reader
*pfile
)
312 if (pfile
->directive
!= &dtable
[T_DEFINE
])
314 bool no_expand
= (pfile
->directive
315 && ! (pfile
->directive
->flags
& EXPAND
));
316 bool was_skipping
= pfile
->state
.skipping
;
318 pfile
->state
.in_expression
= (pfile
->directive
== &dtable
[T_IF
]
319 || pfile
->directive
== &dtable
[T_ELIF
]);
320 if (pfile
->state
.in_expression
)
321 pfile
->state
.skipping
= false;
324 pfile
->state
.prevent_expansion
++;
325 _cpp_scan_out_logical_line (pfile
, NULL
);
327 pfile
->state
.prevent_expansion
--;
329 pfile
->state
.skipping
= was_skipping
;
330 _cpp_overlay_buffer (pfile
, pfile
->out
.base
,
331 pfile
->out
.cur
- pfile
->out
.base
);
334 /* Stop ISO C from expanding anything. */
335 pfile
->state
.prevent_expansion
++;
338 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
339 the '#' was indented. */
341 directive_diagnostics (cpp_reader
*pfile
, const directive
*dir
, int indented
)
343 /* Issue -pedantic warnings for extensions. */
344 if (CPP_PEDANTIC (pfile
)
345 && ! pfile
->state
.skipping
346 && dir
->origin
== EXTENSION
)
347 cpp_error (pfile
, CPP_DL_PEDWARN
, "#%s is a GCC extension", dir
->name
);
349 /* Traditionally, a directive is ignored unless its # is in
350 column 1. Therefore in code intended to work with K+R
351 compilers, directives added by C89 must have their #
352 indented, and directives present in traditional C must not.
353 This is true even of directives in skipped conditional
354 blocks. #elif cannot be used at all. */
355 if (CPP_WTRADITIONAL (pfile
))
357 if (dir
== &dtable
[T_ELIF
])
358 cpp_error (pfile
, CPP_DL_WARNING
,
359 "suggest not using #elif in traditional C");
360 else if (indented
&& dir
->origin
== KANDR
)
361 cpp_error (pfile
, CPP_DL_WARNING
,
362 "traditional C ignores #%s with the # indented",
364 else if (!indented
&& dir
->origin
!= KANDR
)
365 cpp_error (pfile
, CPP_DL_WARNING
,
366 "suggest hiding #%s from traditional C with an indented #",
371 /* Check if we have a known directive. INDENTED is nonzero if the
372 '#' of the directive was indented. This function is in this file
373 to save unnecessarily exporting dtable etc. to lex.c. Returns
374 nonzero if the line of tokens has been handled, zero if we should
375 continue processing the line. */
377 _cpp_handle_directive (cpp_reader
*pfile
, int indented
)
379 const directive
*dir
= 0;
380 const cpp_token
*dname
;
381 bool was_parsing_args
= pfile
->state
.parsing_args
;
382 bool was_discarding_output
= pfile
->state
.discarding_output
;
385 if (was_discarding_output
)
386 pfile
->state
.prevent_expansion
= 0;
388 if (was_parsing_args
)
390 if (CPP_OPTION (pfile
, pedantic
))
391 cpp_error (pfile
, CPP_DL_PEDWARN
,
392 "embedding a directive within macro arguments is not portable");
393 pfile
->state
.parsing_args
= 0;
394 pfile
->state
.prevent_expansion
= 0;
396 start_directive (pfile
);
397 dname
= _cpp_lex_token (pfile
);
399 if (dname
->type
== CPP_NAME
)
401 if (dname
->val
.node
->is_directive
)
402 dir
= &dtable
[dname
->val
.node
->directive_index
];
404 /* We do not recognize the # followed by a number extension in
406 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
408 dir
= &linemarker_dir
;
409 if (CPP_PEDANTIC (pfile
) && ! CPP_OPTION (pfile
, preprocessed
)
410 && ! pfile
->state
.skipping
)
411 cpp_error (pfile
, CPP_DL_PEDWARN
,
412 "style of line directive is a GCC extension");
417 /* If we have a directive that is not an opening conditional,
418 invalidate any control macro. */
419 if (! (dir
->flags
& IF_COND
))
420 pfile
->mi_valid
= false;
422 /* Kluge alert. In order to be sure that code like this
427 does not cause '#define foo bar' to get executed when
428 compiled with -save-temps, we recognize directives in
429 -fpreprocessed mode only if the # is in column 1. macro.c
430 puts a space in front of any '#' at the start of a macro. */
431 if (CPP_OPTION (pfile
, preprocessed
)
432 && (indented
|| !(dir
->flags
& IN_I
)))
439 /* In failed conditional groups, all non-conditional
440 directives are ignored. Before doing that, whether
441 skipping or not, we should lex angle-bracketed headers
442 correctly, and maybe output some diagnostics. */
443 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
444 pfile
->state
.directive_wants_padding
= dir
->flags
& INCL
;
445 if (! CPP_OPTION (pfile
, preprocessed
))
446 directive_diagnostics (pfile
, dir
, indented
);
447 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
451 else if (dname
->type
== CPP_EOF
)
452 ; /* CPP_EOF is the "null directive". */
455 /* An unknown directive. Don't complain about it in assembly
456 source: we don't know where the comments are, and # may
457 introduce assembler pseudo-ops. Don't complain about invalid
458 directives in skipped conditional groups (6.10 p4). */
459 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
461 else if (!pfile
->state
.skipping
)
462 cpp_error (pfile
, CPP_DL_ERROR
, "invalid preprocessing directive #%s",
463 cpp_token_as_text (pfile
, dname
));
466 pfile
->directive
= dir
;
467 if (CPP_OPTION (pfile
, traditional
))
468 prepare_directive_trad (pfile
);
471 pfile
->directive
->handler (pfile
);
473 _cpp_backup_tokens (pfile
, 1);
475 end_directive (pfile
, skip
);
476 if (was_parsing_args
)
478 /* Restore state when within macro args. */
479 pfile
->state
.parsing_args
= 2;
480 pfile
->state
.prevent_expansion
= 1;
482 if (was_discarding_output
)
483 pfile
->state
.prevent_expansion
= 1;
487 /* Directive handler wrapper used by the command line option
488 processor. BUF is \n terminated. */
490 run_directive (cpp_reader
*pfile
, int dir_no
, const char *buf
, size_t count
)
492 cpp_push_buffer (pfile
, (const uchar
*) buf
, count
,
493 /* from_stage3 */ true);
494 /* Disgusting hack. */
495 if (dir_no
== T_PRAGMA
&& pfile
->buffer
->prev
)
496 pfile
->buffer
->file
= pfile
->buffer
->prev
->file
;
497 start_directive (pfile
);
499 /* This is a short-term fix to prevent a leading '#' being
500 interpreted as a directive. */
501 _cpp_clean_line (pfile
);
503 pfile
->directive
= &dtable
[dir_no
];
504 if (CPP_OPTION (pfile
, traditional
))
505 prepare_directive_trad (pfile
);
506 pfile
->directive
->handler (pfile
);
507 end_directive (pfile
, 1);
508 if (dir_no
== T_PRAGMA
)
509 pfile
->buffer
->file
= NULL
;
510 _cpp_pop_buffer (pfile
);
513 /* Checks for validity the macro name in #define, #undef, #ifdef and
514 #ifndef directives. */
515 static cpp_hashnode
*
516 lex_macro_node (cpp_reader
*pfile
)
518 const cpp_token
*token
= _cpp_lex_token (pfile
);
520 /* The token immediately after #define must be an identifier. That
521 identifier may not be "defined", per C99 6.10.8p4.
522 In C++, it may not be any of the "named operators" either,
523 per C++98 [lex.digraph], [lex.key].
524 Finally, the identifier may not have been poisoned. (In that case
525 the lexer has issued the error message for us.) */
527 if (token
->type
== CPP_NAME
)
529 cpp_hashnode
*node
= token
->val
.node
;
531 if (node
== pfile
->spec_nodes
.n_defined
)
532 cpp_error (pfile
, CPP_DL_ERROR
,
533 "\"defined\" cannot be used as a macro name");
534 else if (! (node
->flags
& NODE_POISONED
))
537 else if (token
->flags
& NAMED_OP
)
538 cpp_error (pfile
, CPP_DL_ERROR
,
539 "\"%s\" cannot be used as a macro name as it is an operator in C++",
540 NODE_NAME (token
->val
.node
));
541 else if (token
->type
== CPP_EOF
)
542 cpp_error (pfile
, CPP_DL_ERROR
, "no macro name given in #%s directive",
543 pfile
->directive
->name
);
545 cpp_error (pfile
, CPP_DL_ERROR
, "macro names must be identifiers");
550 /* Process a #define directive. Most work is done in macro.c. */
552 do_define (cpp_reader
*pfile
)
554 cpp_hashnode
*node
= lex_macro_node (pfile
);
558 /* If we have been requested to expand comments into macros,
559 then re-enable saving of comments. */
560 pfile
->state
.save_comments
=
561 ! CPP_OPTION (pfile
, discard_comments_in_macro_exp
);
563 if (_cpp_create_definition (pfile
, node
))
564 if (pfile
->cb
.define
)
565 pfile
->cb
.define (pfile
, pfile
->directive_line
, node
);
569 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
571 do_undef (cpp_reader
*pfile
)
573 cpp_hashnode
*node
= lex_macro_node (pfile
);
578 pfile
->cb
.undef (pfile
, pfile
->directive_line
, node
);
580 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
581 identifier is not currently defined as a macro name. */
582 if (node
->type
== NT_MACRO
)
584 if (node
->flags
& NODE_WARN
)
585 cpp_error (pfile
, CPP_DL_WARNING
,
586 "undefining \"%s\"", NODE_NAME (node
));
588 if (CPP_OPTION (pfile
, warn_unused_macros
))
589 _cpp_warn_if_unused_macro (pfile
, node
, NULL
);
591 _cpp_free_definition (node
);
598 /* Undefine a single macro/assertion/whatever. */
601 undefine_macros (cpp_reader
*pfile ATTRIBUTE_UNUSED
, cpp_hashnode
*h
,
602 void *data_p ATTRIBUTE_UNUSED
)
604 /* Body of _cpp_free_definition inlined here for speed.
605 Macros and assertions no longer have anything to free. */
607 h
->flags
&= ~(NODE_POISONED
|NODE_BUILTIN
|NODE_DISABLED
);
611 /* Undefine all macros and assertions. */
614 cpp_undef_all (cpp_reader
*pfile
)
616 cpp_forall_identifiers (pfile
, undefine_macros
, NULL
);
620 /* Helper routine used by parse_include. Reinterpret the current line
621 as an h-char-sequence (< ... >); we are looking at the first token
622 after the <. Returns a malloced filename. */
624 glue_header_name (cpp_reader
*pfile
)
626 const cpp_token
*token
;
628 size_t len
, total_len
= 0, capacity
= 1024;
630 /* To avoid lexed tokens overwriting our glued name, we can only
631 allocate from the string pool once we've lexed everything. */
632 buffer
= XNEWVEC (char, capacity
);
635 token
= get_token_no_padding (pfile
);
637 if (token
->type
== CPP_GREATER
)
639 if (token
->type
== CPP_EOF
)
641 cpp_error (pfile
, CPP_DL_ERROR
, "missing terminating > character");
645 len
= cpp_token_len (token
) + 2; /* Leading space, terminating \0. */
646 if (total_len
+ len
> capacity
)
648 capacity
= (capacity
+ len
) * 2;
649 buffer
= XRESIZEVEC (char, buffer
, capacity
);
652 if (token
->flags
& PREV_WHITE
)
653 buffer
[total_len
++] = ' ';
655 total_len
= (cpp_spell_token (pfile
, token
, (uchar
*) &buffer
[total_len
],
660 buffer
[total_len
] = '\0';
664 /* Returns the file name of #include, #include_next, #import and
665 #pragma dependency. The string is malloced and the caller should
666 free it. Returns NULL on error. */
668 parse_include (cpp_reader
*pfile
, int *pangle_brackets
,
669 const cpp_token
***buf
)
672 const cpp_token
*header
;
674 /* Allow macro expansion. */
675 header
= get_token_no_padding (pfile
);
676 if (header
->type
== CPP_STRING
|| header
->type
== CPP_HEADER_NAME
)
678 fname
= XNEWVEC (char, header
->val
.str
.len
- 1);
679 memcpy (fname
, header
->val
.str
.text
+ 1, header
->val
.str
.len
- 2);
680 fname
[header
->val
.str
.len
- 2] = '\0';
681 *pangle_brackets
= header
->type
== CPP_HEADER_NAME
;
683 else if (header
->type
== CPP_LESS
)
685 fname
= glue_header_name (pfile
);
686 *pangle_brackets
= 1;
690 const unsigned char *dir
;
692 if (pfile
->directive
== &dtable
[T_PRAGMA
])
693 dir
= U
"pragma dependency";
695 dir
= pfile
->directive
->name
;
696 cpp_error (pfile
, CPP_DL_ERROR
, "#%s expects \"FILENAME\" or <FILENAME>",
702 if (buf
== NULL
|| CPP_OPTION (pfile
, discard_comments
))
706 /* If we are not discarding comments, then gather them while
707 doing the eol check. */
708 *buf
= check_eol_return_comments (pfile
);
714 /* Handle #include, #include_next and #import. */
716 do_include_common (cpp_reader
*pfile
, enum include_type type
)
720 const cpp_token
**buf
= NULL
;
722 /* Re-enable saving of comments if requested, so that the include
723 callback can dump comments which follow #include. */
724 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
726 fname
= parse_include (pfile
, &angle_brackets
, &buf
);
736 cpp_error (pfile
, CPP_DL_ERROR
, "empty filename in #%s",
737 pfile
->directive
->name
);
744 /* Prevent #include recursion. */
745 if (pfile
->line_table
->depth
>= CPP_STACK_MAX
)
746 cpp_error (pfile
, CPP_DL_ERROR
, "#include nested too deeply");
749 /* Get out of macro context, if we are. */
750 skip_rest_of_line (pfile
);
752 if (pfile
->cb
.include
)
753 pfile
->cb
.include (pfile
, pfile
->directive_line
,
754 pfile
->directive
->name
, fname
, angle_brackets
,
757 _cpp_stack_include (pfile
, fname
, angle_brackets
, type
);
766 do_include (cpp_reader
*pfile
)
768 do_include_common (pfile
, IT_INCLUDE
);
772 do_import (cpp_reader
*pfile
)
774 do_include_common (pfile
, IT_IMPORT
);
778 do_include_next (cpp_reader
*pfile
)
780 enum include_type type
= IT_INCLUDE_NEXT
;
782 /* If this is the primary source file, warn and use the normal
784 if (! pfile
->buffer
->prev
)
786 cpp_error (pfile
, CPP_DL_WARNING
,
787 "#include_next in primary source file");
790 do_include_common (pfile
, type
);
793 /* Subroutine of do_linemarker. Read possible flags after file name.
794 LAST is the last flag seen; 0 if this is the first flag. Return the
795 flag if it is valid, 0 at the end of the directive. Otherwise
798 read_flag (cpp_reader
*pfile
, unsigned int last
)
800 const cpp_token
*token
= _cpp_lex_token (pfile
);
802 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
804 unsigned int flag
= token
->val
.str
.text
[0] - '0';
806 if (flag
> last
&& flag
<= 4
807 && (flag
!= 4 || last
== 3)
808 && (flag
!= 2 || last
== 0))
812 if (token
->type
!= CPP_EOF
)
813 cpp_error (pfile
, CPP_DL_ERROR
, "invalid flag \"%s\" in line directive",
814 cpp_token_as_text (pfile
, token
));
818 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
819 of length LEN, to binary; store it in NUMP, and return 0 if the
820 number was well-formed, 1 if not. Temporary, hopefully. */
822 strtoul_for_line (const uchar
*str
, unsigned int len
, long unsigned int *nump
)
824 unsigned long reg
= 0;
838 /* Interpret #line command.
839 Note that the filename string (if any) is a true string constant
840 (escapes are interpreted), unlike in #line. */
842 do_line (cpp_reader
*pfile
)
844 const struct line_maps
*line_table
= pfile
->line_table
;
845 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
847 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
850 unsigned char map_sysp
= map
->sysp
;
851 const cpp_token
*token
;
852 const char *new_file
= map
->to_file
;
853 unsigned long new_lineno
;
855 /* C99 raised the minimum limit on #line numbers. */
856 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
858 /* #line commands expand macros. */
859 token
= cpp_get_token (pfile
);
860 if (token
->type
!= CPP_NUMBER
861 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
864 cpp_error (pfile
, CPP_DL_ERROR
,
865 "\"%s\" after #line is not a positive integer",
866 cpp_token_as_text (pfile
, token
));
870 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
871 cpp_error (pfile
, CPP_DL_PEDWARN
, "line number out of range");
873 token
= cpp_get_token (pfile
);
874 if (token
->type
== CPP_STRING
)
876 cpp_string s
= { 0, 0 };
877 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
, 1,
879 new_file
= (const char *)s
.text
;
882 else if (token
->type
!= CPP_EOF
)
884 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
885 cpp_token_as_text (pfile
, token
));
889 skip_rest_of_line (pfile
);
890 _cpp_do_file_change (pfile
, LC_RENAME
, new_file
, new_lineno
,
894 /* Interpret the # 44 "file" [flags] notation, which has slightly
895 different syntax and semantics from #line: Flags are allowed,
896 and we never complain about the line number being too big. */
898 do_linemarker (cpp_reader
*pfile
)
900 const struct line_maps
*line_table
= pfile
->line_table
;
901 const struct line_map
*map
= &line_table
->maps
[line_table
->used
- 1];
902 const cpp_token
*token
;
903 const char *new_file
= map
->to_file
;
904 unsigned long new_lineno
;
905 unsigned int new_sysp
= map
->sysp
;
906 enum lc_reason reason
= LC_RENAME
;
909 /* Back up so we can get the number again. Putting this in
910 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
911 some circumstances, which can segfault. */
912 _cpp_backup_tokens (pfile
, 1);
914 /* #line commands expand macros. */
915 token
= cpp_get_token (pfile
);
916 if (token
->type
!= CPP_NUMBER
917 || strtoul_for_line (token
->val
.str
.text
, token
->val
.str
.len
,
920 cpp_error (pfile
, CPP_DL_ERROR
,
921 "\"%s\" after # is not a positive integer",
922 cpp_token_as_text (pfile
, token
));
926 token
= cpp_get_token (pfile
);
927 if (token
->type
== CPP_STRING
)
929 cpp_string s
= { 0, 0 };
930 if (cpp_interpret_string_notranslate (pfile
, &token
->val
.str
,
932 new_file
= (const char *)s
.text
;
935 flag
= read_flag (pfile
, 0);
939 /* Fake an include for cpp_included (). */
940 _cpp_fake_include (pfile
, new_file
);
941 flag
= read_flag (pfile
, flag
);
946 flag
= read_flag (pfile
, flag
);
951 flag
= read_flag (pfile
, flag
);
955 pfile
->buffer
->sysp
= new_sysp
;
959 else if (token
->type
!= CPP_EOF
)
961 cpp_error (pfile
, CPP_DL_ERROR
, "\"%s\" is not a valid filename",
962 cpp_token_as_text (pfile
, token
));
966 skip_rest_of_line (pfile
);
967 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
970 /* Arrange the file_change callback. pfile->line has changed to
971 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
972 header, 2 for a system header that needs to be extern "C" protected,
973 and zero otherwise. */
975 _cpp_do_file_change (cpp_reader
*pfile
, enum lc_reason reason
,
976 const char *to_file
, unsigned int file_line
,
979 const struct line_map
*map
= linemap_add (pfile
->line_table
, reason
, sysp
,
982 linemap_line_start (pfile
->line_table
, map
->to_line
, 127);
984 if (pfile
->cb
.file_change
)
985 pfile
->cb
.file_change (pfile
, map
);
988 /* Report a warning or error detected by the program we are
989 processing. Use the directive's tokens in the error message. */
991 do_diagnostic (cpp_reader
*pfile
, int code
, int print_dir
)
993 if (_cpp_begin_message (pfile
, code
, pfile
->cur_token
[-1].src_loc
, 0))
996 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
997 pfile
->state
.prevent_expansion
++;
998 cpp_output_line (pfile
, stderr
);
999 pfile
->state
.prevent_expansion
--;
1004 do_error (cpp_reader
*pfile
)
1006 do_diagnostic (pfile
, CPP_DL_ERROR
, 1);
1010 do_warning (cpp_reader
*pfile
)
1012 /* We want #warning diagnostics to be emitted in system headers too. */
1013 do_diagnostic (pfile
, CPP_DL_WARNING_SYSHDR
, 1);
1016 /* Report program identification. */
1018 do_ident (cpp_reader
*pfile
)
1020 const cpp_token
*str
= cpp_get_token (pfile
);
1022 if (str
->type
!= CPP_STRING
)
1023 cpp_error (pfile
, CPP_DL_ERROR
, "invalid #%s directive",
1024 pfile
->directive
->name
);
1025 else if (pfile
->cb
.ident
)
1026 pfile
->cb
.ident (pfile
, pfile
->directive_line
, &str
->val
.str
);
1031 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1032 matching entry, or NULL if none is found. The returned entry could
1033 be the start of a namespace chain, or a pragma. */
1034 static struct pragma_entry
*
1035 lookup_pragma_entry (struct pragma_entry
*chain
, const cpp_hashnode
*pragma
)
1037 while (chain
&& chain
->pragma
!= pragma
)
1038 chain
= chain
->next
;
1043 /* Create and insert a pragma entry for NAME at the beginning of a
1044 singly-linked CHAIN. If handler is NULL, it is a namespace,
1045 otherwise it is a pragma and its handler. If INTERNAL is true
1046 this pragma is being inserted by libcpp itself. */
1047 static struct pragma_entry
*
1048 insert_pragma_entry (cpp_reader
*pfile
, struct pragma_entry
**chain
,
1049 const cpp_hashnode
*pragma
, pragma_cb handler
,
1050 bool allow_expansion
, bool internal
)
1052 struct pragma_entry
*new_entry
;
1054 new_entry
= (struct pragma_entry
*)
1055 _cpp_aligned_alloc (pfile
, sizeof (struct pragma_entry
));
1056 new_entry
->pragma
= pragma
;
1059 new_entry
->is_nspace
= 0;
1060 new_entry
->u
.handler
= handler
;
1064 new_entry
->is_nspace
= 1;
1065 new_entry
->u
.space
= NULL
;
1068 new_entry
->allow_expansion
= allow_expansion
;
1069 new_entry
->is_internal
= internal
;
1070 new_entry
->next
= *chain
;
1075 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1076 goes in the global namespace. HANDLER is the handler it will call,
1077 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1078 expansion while parsing pragma NAME. INTERNAL is true if this is a
1079 pragma registered by cpplib itself, false if it is registered via
1080 cpp_register_pragma */
1082 register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1083 pragma_cb handler
, bool allow_expansion
, bool internal
)
1085 struct pragma_entry
**chain
= &pfile
->pragmas
;
1086 struct pragma_entry
*entry
;
1087 const cpp_hashnode
*node
;
1094 node
= cpp_lookup (pfile
, U space
, strlen (space
));
1095 entry
= lookup_pragma_entry (*chain
, node
);
1097 entry
= insert_pragma_entry (pfile
, chain
, node
, NULL
,
1098 allow_expansion
, internal
);
1099 else if (!entry
->is_nspace
)
1101 chain
= &entry
->u
.space
;
1104 /* Check for duplicates. */
1105 node
= cpp_lookup (pfile
, U name
, strlen (name
));
1106 entry
= lookup_pragma_entry (*chain
, node
);
1109 if (entry
->is_nspace
)
1111 cpp_error (pfile
, CPP_DL_ICE
,
1112 "registering \"%s\" as both a pragma and a pragma namespace",
1115 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s %s is already registered",
1118 cpp_error (pfile
, CPP_DL_ICE
, "#pragma %s is already registered", name
);
1121 insert_pragma_entry (pfile
, chain
, node
, handler
, allow_expansion
,
1125 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1126 goes in the global namespace. HANDLER is the handler it will call,
1127 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1128 expansion while parsing pragma NAME. This function is exported
1131 cpp_register_pragma (cpp_reader
*pfile
, const char *space
, const char *name
,
1132 pragma_cb handler
, bool allow_expansion
)
1134 register_pragma (pfile
, space
, name
, handler
, allow_expansion
, false);
1137 /* Register the pragmas the preprocessor itself handles. */
1139 _cpp_init_internal_pragmas (cpp_reader
*pfile
)
1141 /* Pragmas in the global namespace. */
1142 register_pragma (pfile
, 0, "once", do_pragma_once
, false, true);
1144 /* New GCC-specific pragmas should be put in the GCC namespace. */
1145 register_pragma (pfile
, "GCC", "poison", do_pragma_poison
, false, true);
1146 register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
,
1148 register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
,
1152 /* Return the number of registered pragmas in PE. */
1155 count_registered_pragmas (struct pragma_entry
*pe
)
1158 for (; pe
!= NULL
; pe
= pe
->next
)
1161 ct
+= count_registered_pragmas (pe
->u
.space
);
1167 /* Save into SD the names of the registered pragmas referenced by PE,
1168 and return a pointer to the next free space in SD. */
1171 save_registered_pragmas (struct pragma_entry
*pe
, char **sd
)
1173 for (; pe
!= NULL
; pe
= pe
->next
)
1176 sd
= save_registered_pragmas (pe
->u
.space
, sd
);
1177 *sd
++ = (char *) xmemdup (HT_STR (&pe
->pragma
->ident
),
1178 HT_LEN (&pe
->pragma
->ident
),
1179 HT_LEN (&pe
->pragma
->ident
) + 1);
1184 /* Return a newly-allocated array which saves the names of the
1185 registered pragmas. */
1188 _cpp_save_pragma_names (cpp_reader
*pfile
)
1190 int ct
= count_registered_pragmas (pfile
->pragmas
);
1191 char **result
= XNEWVEC (char *, ct
);
1192 (void) save_registered_pragmas (pfile
->pragmas
, result
);
1196 /* Restore from SD the names of the registered pragmas referenced by PE,
1197 and return a pointer to the next unused name in SD. */
1200 restore_registered_pragmas (cpp_reader
*pfile
, struct pragma_entry
*pe
,
1203 for (; pe
!= NULL
; pe
= pe
->next
)
1206 sd
= restore_registered_pragmas (pfile
, pe
->u
.space
, sd
);
1207 pe
->pragma
= cpp_lookup (pfile
, U
*sd
, strlen (*sd
));
1214 /* Restore the names of the registered pragmas from SAVED. */
1217 _cpp_restore_pragma_names (cpp_reader
*pfile
, char **saved
)
1219 (void) restore_registered_pragmas (pfile
, pfile
->pragmas
, saved
);
1223 /* Pragmata handling. We handle some, and pass the rest on to the
1224 front end. C99 defines three pragmas and says that no macro
1225 expansion is to be performed on them; whether or not macro
1226 expansion happens for other pragmas is implementation defined.
1227 This implementation never macro-expands the text after #pragma.
1229 The library user has the option of deferring execution of
1230 #pragmas not handled by cpplib, in which case they are converted
1231 to CPP_PRAGMA tokens and inserted into the output stream. */
1233 do_pragma (cpp_reader
*pfile
)
1235 const struct pragma_entry
*p
= NULL
;
1236 const cpp_token
*token
, *pragma_token
= pfile
->cur_token
;
1237 unsigned int count
= 1;
1239 /* Save the current position so that defer_pragmas mode can
1240 copy the entire current line to a string. It will not work
1241 to use _cpp_backup_tokens as that does not reverse buffer->cur. */
1242 const uchar
*line_start
= CPP_BUFFER (pfile
)->cur
;
1244 pfile
->state
.prevent_expansion
++;
1246 token
= cpp_get_token (pfile
);
1247 if (token
->type
== CPP_NAME
)
1249 p
= lookup_pragma_entry (pfile
->pragmas
, token
->val
.node
);
1250 if (p
&& p
->is_nspace
)
1253 token
= cpp_get_token (pfile
);
1254 if (token
->type
== CPP_NAME
)
1255 p
= lookup_pragma_entry (p
->u
.space
, token
->val
.node
);
1263 if (p
->is_internal
|| !CPP_OPTION (pfile
, defer_pragmas
))
1265 /* Since the handler below doesn't get the line number, that it
1266 might need for diagnostics, make sure it has the right
1267 numbers in place. */
1268 if (pfile
->cb
.line_change
)
1269 (*pfile
->cb
.line_change
) (pfile
, pragma_token
, false);
1270 /* Never expand macros if handling a deferred pragma, since
1271 the macro definitions now applicable may be different
1272 from those at the point the pragma appeared. */
1273 if (p
->allow_expansion
&& !pfile
->state
.in_deferred_pragma
)
1274 pfile
->state
.prevent_expansion
--;
1275 (*p
->u
.handler
) (pfile
);
1276 if (p
->allow_expansion
&& !pfile
->state
.in_deferred_pragma
)
1277 pfile
->state
.prevent_expansion
++;
1281 /* Squirrel away the pragma text. Pragmas are
1282 newline-terminated. */
1283 const uchar
*line_end
;
1288 for (line_end
= line_start
; (c
= *line_end
) != '\n'; line_end
++)
1289 if (c
== '"' || c
== '\'')
1291 /* Skip over string literal. */
1295 if (cc
== '\\' && line_end
[1] != '\n')
1297 else if (cc
== '\n')
1307 if (line_end
[1] == '*')
1309 /* Skip over C block comment, unless it is multi-line.
1310 When encountering multi-line block comment, terminate
1311 the pragma token right before that block comment. */
1312 const uchar
*le
= line_end
+ 2;
1314 if (*le
++ == '*' && *le
== '/')
1322 else if (line_end
[1] == '/'
1323 && (CPP_OPTION (pfile
, cplusplus_comments
)
1324 || cpp_in_system_header (pfile
)))
1327 while (*line_end
!= '\n')
1333 body
.len
= (line_end
- line_start
) + 1;
1334 s
= _cpp_unaligned_alloc (pfile
, body
.len
+ 1);
1335 memcpy (s
, line_start
, body
.len
- 1);
1336 s
[body
.len
- 1] = '\n';
1340 /* Create a CPP_PRAGMA token. */
1341 ptok
= &pfile
->directive_result
;
1342 ptok
->src_loc
= pragma_token
->src_loc
;
1343 ptok
->type
= CPP_PRAGMA
;
1344 ptok
->flags
= pragma_token
->flags
| NO_EXPAND
;
1345 ptok
->val
.str
= body
;
1348 else if (pfile
->cb
.def_pragma
)
1350 _cpp_backup_tokens (pfile
, count
);
1351 pfile
->cb
.def_pragma (pfile
, pfile
->directive_line
);
1354 pfile
->state
.prevent_expansion
--;
1357 /* Handle #pragma once. */
1359 do_pragma_once (cpp_reader
*pfile
)
1361 if (pfile
->buffer
->prev
== NULL
)
1362 cpp_error (pfile
, CPP_DL_WARNING
, "#pragma once in main file");
1365 _cpp_mark_file_once_only (pfile
, pfile
->buffer
->file
);
1368 /* Handle #pragma GCC poison, to poison one or more identifiers so
1369 that the lexer produces a hard error for each subsequent usage. */
1371 do_pragma_poison (cpp_reader
*pfile
)
1373 const cpp_token
*tok
;
1376 pfile
->state
.poisoned_ok
= 1;
1379 tok
= _cpp_lex_token (pfile
);
1380 if (tok
->type
== CPP_EOF
)
1382 if (tok
->type
!= CPP_NAME
)
1384 cpp_error (pfile
, CPP_DL_ERROR
,
1385 "invalid #pragma GCC poison directive");
1390 if (hp
->flags
& NODE_POISONED
)
1393 if (hp
->type
== NT_MACRO
)
1394 cpp_error (pfile
, CPP_DL_WARNING
, "poisoning existing macro \"%s\"",
1396 _cpp_free_definition (hp
);
1397 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1399 pfile
->state
.poisoned_ok
= 0;
1402 /* Mark the current header as a system header. This will suppress
1403 some categories of warnings (notably those from -pedantic). It is
1404 intended for use in system libraries that cannot be implemented in
1405 conforming C, but cannot be certain that their headers appear in a
1406 system include directory. To prevent abuse, it is rejected in the
1407 primary source file. */
1409 do_pragma_system_header (cpp_reader
*pfile
)
1411 cpp_buffer
*buffer
= pfile
->buffer
;
1413 if (buffer
->prev
== 0)
1414 cpp_error (pfile
, CPP_DL_WARNING
,
1415 "#pragma system_header ignored outside include file");
1419 skip_rest_of_line (pfile
);
1420 cpp_make_system_header (pfile
, 1, 0);
1424 /* Check the modified date of the current include file against a specified
1425 file. Issue a diagnostic, if the specified file is newer. We use this to
1426 determine if a fixed header should be refixed. */
1428 do_pragma_dependency (cpp_reader
*pfile
)
1431 int angle_brackets
, ordering
;
1433 fname
= parse_include (pfile
, &angle_brackets
, NULL
);
1437 ordering
= _cpp_compare_file_date (pfile
, fname
, angle_brackets
);
1439 cpp_error (pfile
, CPP_DL_WARNING
, "cannot find source file %s", fname
);
1440 else if (ordering
> 0)
1442 cpp_error (pfile
, CPP_DL_WARNING
,
1443 "current file is older than %s", fname
);
1444 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
1446 _cpp_backup_tokens (pfile
, 1);
1447 do_diagnostic (pfile
, CPP_DL_WARNING
, 0);
1451 free ((void *) fname
);
1454 /* Get a token but skip padding. */
1455 static const cpp_token
*
1456 get_token_no_padding (cpp_reader
*pfile
)
1460 const cpp_token
*result
= cpp_get_token (pfile
);
1461 if (result
->type
!= CPP_PADDING
)
1466 /* Check syntax is "(string-literal)". Returns the string on success,
1467 or NULL on failure. */
1468 static const cpp_token
*
1469 get__Pragma_string (cpp_reader
*pfile
)
1471 const cpp_token
*string
;
1473 if (get_token_no_padding (pfile
)->type
!= CPP_OPEN_PAREN
)
1476 string
= get_token_no_padding (pfile
);
1477 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1480 if (get_token_no_padding (pfile
)->type
!= CPP_CLOSE_PAREN
)
1486 /* Destringize IN into a temporary buffer, by removing the first \ of
1487 \" and \\ sequences, and process the result as a #pragma directive. */
1489 destringize_and_run (cpp_reader
*pfile
, const cpp_string
*in
)
1491 const unsigned char *src
, *limit
;
1492 char *dest
, *result
;
1494 dest
= result
= (char *) alloca (in
->len
- 1);
1495 src
= in
->text
+ 1 + (in
->text
[0] == 'L');
1496 limit
= in
->text
+ in
->len
- 1;
1499 /* We know there is a character following the backslash. */
1500 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1506 /* Ugh; an awful kludge. We are really not set up to be lexing
1507 tokens when in the middle of a macro expansion. Use a new
1508 context to force cpp_get_token to lex, and so skip_rest_of_line
1509 doesn't go beyond the end of the text. Also, remember the
1510 current lexing position so we can return to it later.
1512 Something like line-at-a-time lexing should remove the need for
1515 cpp_context
*saved_context
= pfile
->context
;
1516 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1517 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1519 pfile
->context
= XNEW (cpp_context
);
1520 pfile
->context
->macro
= 0;
1521 pfile
->context
->prev
= 0;
1522 run_directive (pfile
, T_PRAGMA
, result
, dest
- result
);
1523 XDELETE (pfile
->context
);
1524 pfile
->context
= saved_context
;
1525 pfile
->cur_token
= saved_cur_token
;
1526 pfile
->cur_run
= saved_cur_run
;
1529 /* See above comment. For the moment, we'd like
1531 token1 _Pragma ("foo") token2
1541 Getting the line markers is a little tricky. */
1542 if (pfile
->cb
.line_change
)
1543 pfile
->cb
.line_change (pfile
, pfile
->cur_token
, false);
1546 /* Handle the _Pragma operator. */
1548 _cpp_do__Pragma (cpp_reader
*pfile
)
1550 const cpp_token
*string
= get__Pragma_string (pfile
);
1551 pfile
->directive_result
.type
= CPP_PADDING
;
1554 destringize_and_run (pfile
, &string
->val
.str
);
1556 cpp_error (pfile
, CPP_DL_ERROR
,
1557 "_Pragma takes a parenthesized string literal");
1560 /* Handle a pragma that the front end deferred until now. */
1562 cpp_handle_deferred_pragma (cpp_reader
*pfile
, const cpp_string
*s
)
1564 cpp_context
*saved_context
= pfile
->context
;
1565 cpp_token
*saved_cur_token
= pfile
->cur_token
;
1566 tokenrun
*saved_cur_run
= pfile
->cur_run
;
1567 bool saved_defer_pragmas
= CPP_OPTION (pfile
, defer_pragmas
);
1568 void (*saved_line_change
) (cpp_reader
*, const cpp_token
*, int)
1569 = pfile
->cb
.line_change
;
1571 pfile
->context
= XNEW (cpp_context
);
1572 pfile
->context
->macro
= 0;
1573 pfile
->context
->prev
= 0;
1574 pfile
->cb
.line_change
= NULL
;
1575 pfile
->state
.in_deferred_pragma
= true;
1576 CPP_OPTION (pfile
, defer_pragmas
) = false;
1578 run_directive (pfile
, T_PRAGMA
, (const char *)s
->text
, s
->len
);
1580 XDELETE (pfile
->context
);
1581 pfile
->context
= saved_context
;
1582 pfile
->cur_token
= saved_cur_token
;
1583 pfile
->cur_run
= saved_cur_run
;
1584 pfile
->cb
.line_change
= saved_line_change
;
1585 pfile
->state
.in_deferred_pragma
= false;
1586 CPP_OPTION (pfile
, defer_pragmas
) = saved_defer_pragmas
;
1589 /* Handle #ifdef. */
1591 do_ifdef (cpp_reader
*pfile
)
1595 if (! pfile
->state
.skipping
)
1597 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1601 skip
= node
->type
!= NT_MACRO
;
1602 _cpp_mark_macro_used (node
);
1607 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1610 /* Handle #ifndef. */
1612 do_ifndef (cpp_reader
*pfile
)
1615 const cpp_hashnode
*node
= 0;
1617 if (! pfile
->state
.skipping
)
1619 node
= lex_macro_node (pfile
);
1623 skip
= node
->type
== NT_MACRO
;
1624 _cpp_mark_macro_used (node
);
1629 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1632 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1633 pfile->mi_ind_cmacro so we can handle multiple-include
1634 optimizations. If macro expansion occurs in the expression, we
1635 cannot treat it as a controlling conditional, since the expansion
1636 could change in the future. That is handled by cpp_get_token. */
1638 do_if (cpp_reader
*pfile
)
1642 if (! pfile
->state
.skipping
)
1643 skip
= _cpp_parse_expr (pfile
) == false;
1645 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1648 /* Flip skipping state if appropriate and continue without changing
1649 if_stack; this is so that the error message for missing #endif's
1650 etc. will point to the original #if. */
1652 do_else (cpp_reader
*pfile
)
1654 cpp_buffer
*buffer
= pfile
->buffer
;
1655 struct if_stack
*ifs
= buffer
->if_stack
;
1658 cpp_error (pfile
, CPP_DL_ERROR
, "#else without #if");
1661 if (ifs
->type
== T_ELSE
)
1663 cpp_error (pfile
, CPP_DL_ERROR
, "#else after #else");
1664 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1665 "the conditional began here");
1669 /* Skip any future (erroneous) #elses or #elifs. */
1670 pfile
->state
.skipping
= ifs
->skip_elses
;
1671 ifs
->skip_elses
= true;
1673 /* Invalidate any controlling macro. */
1676 /* Only check EOL if was not originally skipping. */
1677 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1682 /* Handle a #elif directive by not changing if_stack either. See the
1683 comment above do_else. */
1685 do_elif (cpp_reader
*pfile
)
1687 cpp_buffer
*buffer
= pfile
->buffer
;
1688 struct if_stack
*ifs
= buffer
->if_stack
;
1691 cpp_error (pfile
, CPP_DL_ERROR
, "#elif without #if");
1694 if (ifs
->type
== T_ELSE
)
1696 cpp_error (pfile
, CPP_DL_ERROR
, "#elif after #else");
1697 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
1698 "the conditional began here");
1702 /* Only evaluate this if we aren't skipping elses. During
1703 evaluation, set skipping to false to get lexer warnings. */
1704 if (ifs
->skip_elses
)
1705 pfile
->state
.skipping
= 1;
1708 pfile
->state
.skipping
= 0;
1709 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1710 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1713 /* Invalidate any controlling macro. */
1718 /* #endif pops the if stack and resets pfile->state.skipping. */
1720 do_endif (cpp_reader
*pfile
)
1722 cpp_buffer
*buffer
= pfile
->buffer
;
1723 struct if_stack
*ifs
= buffer
->if_stack
;
1726 cpp_error (pfile
, CPP_DL_ERROR
, "#endif without #if");
1729 /* Only check EOL if was not originally skipping. */
1730 if (!ifs
->was_skipping
&& CPP_OPTION (pfile
, warn_endif_labels
))
1733 /* If potential control macro, we go back outside again. */
1734 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1736 pfile
->mi_valid
= true;
1737 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1740 buffer
->if_stack
= ifs
->next
;
1741 pfile
->state
.skipping
= ifs
->was_skipping
;
1742 obstack_free (&pfile
->buffer_ob
, ifs
);
1746 /* Push an if_stack entry for a preprocessor conditional, and set
1747 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1748 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1749 we need to check here that we are at the top of the file. */
1751 push_conditional (cpp_reader
*pfile
, int skip
, int type
,
1752 const cpp_hashnode
*cmacro
)
1754 struct if_stack
*ifs
;
1755 cpp_buffer
*buffer
= pfile
->buffer
;
1757 ifs
= XOBNEW (&pfile
->buffer_ob
, struct if_stack
);
1758 ifs
->line
= pfile
->directive_line
;
1759 ifs
->next
= buffer
->if_stack
;
1760 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1761 ifs
->was_skipping
= pfile
->state
.skipping
;
1763 /* This condition is effectively a test for top-of-file. */
1764 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1765 ifs
->mi_cmacro
= cmacro
;
1769 pfile
->state
.skipping
= skip
;
1770 buffer
->if_stack
= ifs
;
1773 /* Read the tokens of the answer into the macro pool, in a directive
1774 of type TYPE. Only commit the memory if we intend it as permanent
1775 storage, i.e. the #assert case. Returns 0 on success, and sets
1776 ANSWERP to point to the answer. */
1778 parse_answer (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
1780 const cpp_token
*paren
;
1781 struct answer
*answer
;
1782 unsigned int acount
;
1784 /* In a conditional, it is legal to not have an open paren. We
1785 should save the following token in this case. */
1786 paren
= cpp_get_token (pfile
);
1788 /* If not a paren, see if we're OK. */
1789 if (paren
->type
!= CPP_OPEN_PAREN
)
1791 /* In a conditional no answer is a test for any answer. It
1792 could be followed by any token. */
1795 _cpp_backup_tokens (pfile
, 1);
1799 /* #unassert with no answer is valid - it removes all answers. */
1800 if (type
== T_UNASSERT
&& paren
->type
== CPP_EOF
)
1803 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' after predicate");
1807 for (acount
= 0;; acount
++)
1810 const cpp_token
*token
= cpp_get_token (pfile
);
1813 if (token
->type
== CPP_CLOSE_PAREN
)
1816 if (token
->type
== CPP_EOF
)
1818 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' to complete answer");
1822 /* struct answer includes the space for one token. */
1823 room_needed
= (sizeof (struct answer
) + acount
* sizeof (cpp_token
));
1825 if (BUFF_ROOM (pfile
->a_buff
) < room_needed
)
1826 _cpp_extend_buff (pfile
, &pfile
->a_buff
, sizeof (struct answer
));
1828 dest
= &((struct answer
*) BUFF_FRONT (pfile
->a_buff
))->first
[acount
];
1831 /* Drop whitespace at start, for answer equivalence purposes. */
1833 dest
->flags
&= ~PREV_WHITE
;
1838 cpp_error (pfile
, CPP_DL_ERROR
, "predicate's answer is empty");
1842 answer
= (struct answer
*) BUFF_FRONT (pfile
->a_buff
);
1843 answer
->count
= acount
;
1844 answer
->next
= NULL
;
1850 /* Parses an assertion directive of type TYPE, returning a pointer to
1851 the hash node of the predicate, or 0 on error. If an answer was
1852 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1853 static cpp_hashnode
*
1854 parse_assertion (cpp_reader
*pfile
, struct answer
**answerp
, int type
)
1856 cpp_hashnode
*result
= 0;
1857 const cpp_token
*predicate
;
1859 /* We don't expand predicates or answers. */
1860 pfile
->state
.prevent_expansion
++;
1863 predicate
= cpp_get_token (pfile
);
1864 if (predicate
->type
== CPP_EOF
)
1865 cpp_error (pfile
, CPP_DL_ERROR
, "assertion without predicate");
1866 else if (predicate
->type
!= CPP_NAME
)
1867 cpp_error (pfile
, CPP_DL_ERROR
, "predicate must be an identifier");
1868 else if (parse_answer (pfile
, answerp
, type
) == 0)
1870 unsigned int len
= NODE_LEN (predicate
->val
.node
);
1871 unsigned char *sym
= (unsigned char *) alloca (len
+ 1);
1873 /* Prefix '#' to get it out of macro namespace. */
1875 memcpy (sym
+ 1, NODE_NAME (predicate
->val
.node
), len
);
1876 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1879 pfile
->state
.prevent_expansion
--;
1883 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1884 or a pointer to NULL if the answer is not in the chain. */
1885 static struct answer
**
1886 find_answer (cpp_hashnode
*node
, const struct answer
*candidate
)
1889 struct answer
**result
;
1891 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1893 struct answer
*answer
= *result
;
1895 if (answer
->count
== candidate
->count
)
1897 for (i
= 0; i
< answer
->count
; i
++)
1898 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1901 if (i
== answer
->count
)
1909 /* Test an assertion within a preprocessor conditional. Returns
1910 nonzero on failure, zero on success. On success, the result of
1911 the test is written into VALUE, otherwise the value 0. */
1913 _cpp_test_assertion (cpp_reader
*pfile
, unsigned int *value
)
1915 struct answer
*answer
;
1918 node
= parse_assertion (pfile
, &answer
, T_IF
);
1920 /* For recovery, an erroneous assertion expression is handled as a
1921 failing assertion. */
1925 *value
= (node
->type
== NT_ASSERTION
&&
1926 (answer
== 0 || *find_answer (node
, answer
) != 0));
1927 else if (pfile
->cur_token
[-1].type
== CPP_EOF
)
1928 _cpp_backup_tokens (pfile
, 1);
1930 /* We don't commit the memory for the answer - it's temporary only. */
1934 /* Handle #assert. */
1936 do_assert (cpp_reader
*pfile
)
1938 struct answer
*new_answer
;
1941 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1946 /* Place the new answer in the answer list. First check there
1947 is not a duplicate. */
1948 new_answer
->next
= 0;
1949 if (node
->type
== NT_ASSERTION
)
1951 if (*find_answer (node
, new_answer
))
1953 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" re-asserted",
1954 NODE_NAME (node
) + 1);
1957 new_answer
->next
= node
->value
.answers
;
1960 answer_size
= sizeof (struct answer
) + ((new_answer
->count
- 1)
1961 * sizeof (cpp_token
));
1962 /* Commit or allocate storage for the object. */
1963 if (pfile
->hash_table
->alloc_subobject
)
1965 struct answer
*temp_answer
= new_answer
;
1966 new_answer
= (struct answer
*) pfile
->hash_table
->alloc_subobject
1968 memcpy (new_answer
, temp_answer
, answer_size
);
1971 BUFF_FRONT (pfile
->a_buff
) += answer_size
;
1973 node
->type
= NT_ASSERTION
;
1974 node
->value
.answers
= new_answer
;
1979 /* Handle #unassert. */
1981 do_unassert (cpp_reader
*pfile
)
1984 struct answer
*answer
;
1986 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1987 /* It isn't an error to #unassert something that isn't asserted. */
1988 if (node
&& node
->type
== NT_ASSERTION
)
1992 struct answer
**p
= find_answer (node
, answer
), *temp
;
1994 /* Remove the answer from the list. */
1999 /* Did we free the last answer? */
2000 if (node
->value
.answers
== 0)
2001 node
->type
= NT_VOID
;
2006 _cpp_free_definition (node
);
2009 /* We don't commit the memory for the answer - it's temporary only. */
2012 /* These are for -D, -U, -A. */
2014 /* Process the string STR as if it appeared as the body of a #define.
2015 If STR is just an identifier, define it with value 1.
2016 If STR has anything after the identifier, then it should
2017 be identifier=definition. */
2019 cpp_define (cpp_reader
*pfile
, const char *str
)
2024 /* Copy the entire option so we can modify it.
2025 Change the first "=" in the string to a space. If there is none,
2026 tack " 1" on the end. */
2028 count
= strlen (str
);
2029 buf
= (char *) alloca (count
+ 3);
2030 memcpy (buf
, str
, count
);
2032 p
= strchr (str
, '=');
2042 run_directive (pfile
, T_DEFINE
, buf
, count
);
2045 /* Slight variant of the above for use by initialize_builtins. */
2047 _cpp_define_builtin (cpp_reader
*pfile
, const char *str
)
2049 size_t len
= strlen (str
);
2050 char *buf
= (char *) alloca (len
+ 1);
2051 memcpy (buf
, str
, len
);
2053 run_directive (pfile
, T_DEFINE
, buf
, len
);
2056 /* Process MACRO as if it appeared as the body of an #undef. */
2058 cpp_undef (cpp_reader
*pfile
, const char *macro
)
2060 size_t len
= strlen (macro
);
2061 char *buf
= (char *) alloca (len
+ 1);
2062 memcpy (buf
, macro
, len
);
2064 run_directive (pfile
, T_UNDEF
, buf
, len
);
2067 /* Process the string STR as if it appeared as the body of a #assert. */
2069 cpp_assert (cpp_reader
*pfile
, const char *str
)
2071 handle_assertion (pfile
, str
, T_ASSERT
);
2074 /* Process STR as if it appeared as the body of an #unassert. */
2076 cpp_unassert (cpp_reader
*pfile
, const char *str
)
2078 handle_assertion (pfile
, str
, T_UNASSERT
);
2081 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2083 handle_assertion (cpp_reader
*pfile
, const char *str
, int type
)
2085 size_t count
= strlen (str
);
2086 const char *p
= strchr (str
, '=');
2088 /* Copy the entire option so we can modify it. Change the first
2089 "=" in the string to a '(', and tack a ')' on the end. */
2090 char *buf
= (char *) alloca (count
+ 2);
2092 memcpy (buf
, str
, count
);
2101 run_directive (pfile
, type
, str
, count
);
2104 /* The number of errors for a given reader. */
2106 cpp_errors (cpp_reader
*pfile
)
2108 return pfile
->errors
;
2111 /* The options structure. */
2113 cpp_get_options (cpp_reader
*pfile
)
2115 return &pfile
->opts
;
2118 /* The callbacks structure. */
2120 cpp_get_callbacks (cpp_reader
*pfile
)
2125 /* Copy the given callbacks structure to our own. */
2127 cpp_set_callbacks (cpp_reader
*pfile
, cpp_callbacks
*cb
)
2132 /* The dependencies structure. (Creates one if it hasn't already been.) */
2134 cpp_get_deps (cpp_reader
*pfile
)
2137 pfile
->deps
= deps_init ();
2141 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2142 doesn't fail. It does not generate a file change call back; that
2143 is the responsibility of the caller. */
2145 cpp_push_buffer (cpp_reader
*pfile
, const uchar
*buffer
, size_t len
,
2148 cpp_buffer
*new_buffer
= XOBNEW (&pfile
->buffer_ob
, cpp_buffer
);
2150 /* Clears, amongst other things, if_stack and mi_cmacro. */
2151 memset (new_buffer
, 0, sizeof (cpp_buffer
));
2153 new_buffer
->next_line
= new_buffer
->buf
= buffer
;
2154 new_buffer
->rlimit
= buffer
+ len
;
2155 new_buffer
->from_stage3
= from_stage3
;
2156 new_buffer
->prev
= pfile
->buffer
;
2157 new_buffer
->need_line
= true;
2159 pfile
->buffer
= new_buffer
;
2164 /* Pops a single buffer, with a file change call-back if appropriate.
2165 Then pushes the next -include file, if any remain. */
2167 _cpp_pop_buffer (cpp_reader
*pfile
)
2169 cpp_buffer
*buffer
= pfile
->buffer
;
2170 struct _cpp_file
*inc
= buffer
->file
;
2171 struct if_stack
*ifs
;
2173 /* Walk back up the conditional stack till we reach its level at
2174 entry to this file, issuing error messages. */
2175 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
2176 cpp_error_with_line (pfile
, CPP_DL_ERROR
, ifs
->line
, 0,
2177 "unterminated #%s", dtable
[ifs
->type
].name
);
2179 /* In case of a missing #endif. */
2180 pfile
->state
.skipping
= 0;
2182 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2183 pfile
->buffer
= buffer
->prev
;
2185 free (buffer
->notes
);
2187 /* Free the buffer object now; we may want to push a new buffer
2188 in _cpp_push_next_include_file. */
2189 obstack_free (&pfile
->buffer_ob
, buffer
);
2193 _cpp_pop_file_buffer (pfile
, inc
);
2195 _cpp_do_file_change (pfile
, LC_LEAVE
, 0, 0, 0);
2199 /* Enter all recognized directives in the hash table. */
2201 _cpp_init_directives (cpp_reader
*pfile
)
2206 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
2208 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
2209 node
->is_directive
= 1;
2210 node
->directive_index
= i
;