Daily bump.
[official-gcc.git] / gcc / cpplib.c
blobf61f7e68eadf5cd6d67885b5b6ba296d463325fa
1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "obstack.h"
29 /* Chained list of answers to an assertion. */
30 struct answer
32 struct answer *next;
33 unsigned int count;
34 cpp_token first[1];
37 /* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
39 struct if_stack
41 struct if_stack *next;
42 unsigned int line; /* Line where condition started. */
43 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
44 bool skip_elses; /* Can future #else / #elif be skipped? */
45 bool was_skipping; /* If were skipping on entry. */
46 int type; /* Most recent conditional, for diagnostics. */
49 /* Contains a registered pragma or pragma namespace. */
50 typedef void (*pragma_cb) PARAMS ((cpp_reader *));
51 struct pragma_entry
53 struct pragma_entry *next;
54 const cpp_hashnode *pragma; /* Name and length. */
55 int is_nspace;
56 union {
57 pragma_cb handler;
58 struct pragma_entry *space;
59 } u;
62 /* Values for the origin field of struct directive. KANDR directives
63 come from traditional (K&R) C. STDC89 directives come from the
64 1989 C standard. EXTENSION directives are extensions. */
65 #define KANDR 0
66 #define STDC89 1
67 #define EXTENSION 2
69 /* Values for the flags field of struct directive. COND indicates a
70 conditional; IF_COND an opening conditional. INCL means to treat
71 "..." and <...> as q-char and h-char sequences respectively. IN_I
72 means this directive should be handled even if -fpreprocessed is in
73 effect (these are the directives with callback hooks).
75 EXPAND is set on directives that are always macro-expanded. */
76 #define COND (1 << 0)
77 #define IF_COND (1 << 1)
78 #define INCL (1 << 2)
79 #define IN_I (1 << 3)
80 #define EXPAND (1 << 4)
82 /* Defines one #-directive, including how to handle it. */
83 typedef void (*directive_handler) PARAMS ((cpp_reader *));
84 typedef struct directive directive;
85 struct directive
87 directive_handler handler; /* Function to handle directive. */
88 const uchar *name; /* Name of directive. */
89 unsigned short length; /* Length of name. */
90 unsigned char origin; /* Origin of directive. */
91 unsigned char flags; /* Flags describing this directive. */
94 /* Forward declarations. */
96 static void skip_rest_of_line PARAMS ((cpp_reader *));
97 static void check_eol PARAMS ((cpp_reader *));
98 static void start_directive PARAMS ((cpp_reader *));
99 static void prepare_directive_trad PARAMS ((cpp_reader *));
100 static void end_directive PARAMS ((cpp_reader *, int));
101 static void directive_diagnostics
102 PARAMS ((cpp_reader *, const directive *, int));
103 static void run_directive PARAMS ((cpp_reader *, int,
104 const char *, size_t));
105 static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
106 static const cpp_token *parse_include PARAMS ((cpp_reader *));
107 static void push_conditional PARAMS ((cpp_reader *, int, int,
108 const cpp_hashnode *));
109 static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
110 static uchar *dequote_string PARAMS ((cpp_reader *, const uchar *,
111 unsigned int));
112 static int strtoul_for_line PARAMS ((const uchar *, unsigned int,
113 unsigned long *));
114 static void do_diagnostic PARAMS ((cpp_reader *, int, int));
115 static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
116 static void do_include_common PARAMS ((cpp_reader *, enum include_type));
117 static struct pragma_entry *lookup_pragma_entry
118 PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma));
119 static struct pragma_entry *insert_pragma_entry
120 PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *,
121 pragma_cb));
122 static void do_pragma_once PARAMS ((cpp_reader *));
123 static void do_pragma_poison PARAMS ((cpp_reader *));
124 static void do_pragma_system_header PARAMS ((cpp_reader *));
125 static void do_pragma_dependency PARAMS ((cpp_reader *));
126 static void do_linemarker PARAMS ((cpp_reader *));
127 static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
128 static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
129 static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *));
130 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
131 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
132 int));
133 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
134 const struct answer *));
135 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
137 /* This is the table of directive handlers. It is ordered by
138 frequency of occurrence; the numbers at the end are directive
139 counts from all the source code I have lying around (egcs and libc
140 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
141 pcmcia-cs-3.0.9). This is no longer important as directive lookup
142 is now O(1). All extensions other than #warning and #include_next
143 are deprecated. The name is where the extension appears to have
144 come from. */
146 #define DIRECTIVE_TABLE \
147 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
148 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
149 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
150 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
151 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
152 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
153 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
154 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
155 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
156 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
157 D(error, T_ERROR, STDC89, 0) /* 475 */ \
158 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
159 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
160 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
161 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
162 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
163 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
164 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
165 SCCS_ENTRY /* 0 SVR4? */
167 /* #sccs is not always recognized. */
168 #ifdef SCCS_DIRECTIVE
169 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
170 #else
171 # define SCCS_ENTRY /* nothing */
172 #endif
174 /* Use the table to generate a series of prototypes, an enum for the
175 directive names, and an array of directive handlers. */
177 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
178 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
179 DIRECTIVE_TABLE
180 #undef D
182 #define D(n, tag, o, f) tag,
183 enum
185 DIRECTIVE_TABLE
186 N_DIRECTIVES
188 #undef D
190 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
191 #define D(name, t, origin, flags) \
192 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
193 sizeof STRINGX(name) - 1, origin, flags },
194 static const directive dtable[] =
196 DIRECTIVE_TABLE
198 #undef D
199 #undef DIRECTIVE_TABLE
201 /* Wrapper struct directive for linemarkers.
202 The origin is more or less true - the original K+R cpp
203 did use this notation in its preprocessed output. */
204 static const directive linemarker_dir =
206 do_linemarker, U"#", 1, KANDR, IN_I
209 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
211 /* Skip any remaining tokens in a directive. */
212 static void
213 skip_rest_of_line (pfile)
214 cpp_reader *pfile;
216 /* Discard all stacked contexts. */
217 while (pfile->context != &pfile->base_context)
218 _cpp_pop_context (pfile);
220 /* Sweep up all tokens remaining on the line. */
221 if (! SEEN_EOL ())
222 while (_cpp_lex_token (pfile)->type != CPP_EOF)
226 /* Ensure there are no stray tokens at the end of a directive. */
227 static void
228 check_eol (pfile)
229 cpp_reader *pfile;
231 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
232 cpp_error (pfile, DL_PEDWARN, "extra tokens at end of #%s directive",
233 pfile->directive->name);
236 /* Called when entering a directive, _Pragma or command-line directive. */
237 static void
238 start_directive (pfile)
239 cpp_reader *pfile;
241 /* Setup in-directive state. */
242 pfile->state.in_directive = 1;
243 pfile->state.save_comments = 0;
245 /* Some handlers need the position of the # for diagnostics. */
246 pfile->directive_line = pfile->line;
249 /* Called when leaving a directive, _Pragma or command-line directive. */
250 static void
251 end_directive (pfile, skip_line)
252 cpp_reader *pfile;
253 int skip_line;
255 if (CPP_OPTION (pfile, traditional))
257 /* Revert change of prepare_directive_trad. */
258 pfile->state.prevent_expansion--;
260 if (pfile->directive != &dtable[T_DEFINE])
261 _cpp_remove_overlay (pfile);
263 /* We don't skip for an assembler #. */
264 else if (skip_line)
266 skip_rest_of_line (pfile);
267 if (!pfile->keep_tokens)
269 pfile->cur_run = &pfile->base_run;
270 pfile->cur_token = pfile->base_run.base;
274 /* Restore state. */
275 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
276 pfile->state.in_directive = 0;
277 pfile->state.in_expression = 0;
278 pfile->state.angled_headers = 0;
279 pfile->directive = 0;
282 /* Prepare to handle the directive in pfile->directive. */
283 static void
284 prepare_directive_trad (pfile)
285 cpp_reader *pfile;
287 if (pfile->directive != &dtable[T_DEFINE])
289 bool no_expand = (pfile->directive
290 && ! (pfile->directive->flags & EXPAND));
291 bool was_skipping = pfile->state.skipping;
293 pfile->state.skipping = false;
294 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
295 || pfile->directive == &dtable[T_ELIF]);
296 if (no_expand)
297 pfile->state.prevent_expansion++;
298 _cpp_read_logical_line_trad (pfile);
299 if (no_expand)
300 pfile->state.prevent_expansion--;
301 pfile->state.skipping = was_skipping;
302 _cpp_overlay_buffer (pfile, pfile->out.base,
303 pfile->out.cur - pfile->out.base);
306 /* Stop ISO C from expanding anything. */
307 pfile->state.prevent_expansion++;
310 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
311 the '#' was indented. */
312 static void
313 directive_diagnostics (pfile, dir, indented)
314 cpp_reader *pfile;
315 const directive *dir;
316 int indented;
318 /* Issue -pedantic warnings for extensions. */
319 if (CPP_PEDANTIC (pfile)
320 && ! pfile->state.skipping
321 && dir->origin == EXTENSION)
322 cpp_error (pfile, DL_PEDWARN, "#%s is a GCC extension", dir->name);
324 /* Traditionally, a directive is ignored unless its # is in
325 column 1. Therefore in code intended to work with K+R
326 compilers, directives added by C89 must have their #
327 indented, and directives present in traditional C must not.
328 This is true even of directives in skipped conditional
329 blocks. #elif cannot be used at all. */
330 if (CPP_WTRADITIONAL (pfile))
332 if (dir == &dtable[T_ELIF])
333 cpp_error (pfile, DL_WARNING,
334 "suggest not using #elif in traditional C");
335 else if (indented && dir->origin == KANDR)
336 cpp_error (pfile, DL_WARNING,
337 "traditional C ignores #%s with the # indented",
338 dir->name);
339 else if (!indented && dir->origin != KANDR)
340 cpp_error (pfile, DL_WARNING,
341 "suggest hiding #%s from traditional C with an indented #",
342 dir->name);
346 /* Check if we have a known directive. INDENTED is non-zero if the
347 '#' of the directive was indented. This function is in this file
348 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
349 non-zero if the line of tokens has been handled, zero if we should
350 continue processing the line. */
352 _cpp_handle_directive (pfile, indented)
353 cpp_reader *pfile;
354 int indented;
356 const directive *dir = 0;
357 const cpp_token *dname;
358 bool was_parsing_args = pfile->state.parsing_args;
359 int skip = 1;
361 if (was_parsing_args)
363 if (CPP_OPTION (pfile, pedantic))
364 cpp_error (pfile, DL_PEDWARN,
365 "embedding a directive within macro arguments is not portable");
366 pfile->state.parsing_args = 0;
367 pfile->state.prevent_expansion = 0;
369 start_directive (pfile);
370 dname = _cpp_lex_token (pfile);
372 if (dname->type == CPP_NAME)
374 if (dname->val.node->directive_index)
375 dir = &dtable[dname->val.node->directive_index - 1];
377 /* We do not recognise the # followed by a number extension in
378 assembler code. */
379 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
381 dir = &linemarker_dir;
382 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
383 && ! pfile->state.skipping)
384 cpp_error (pfile, DL_PEDWARN,
385 "style of line directive is a GCC extension");
388 if (dir)
390 /* If we have a directive that is not an opening conditional,
391 invalidate any control macro. */
392 if (! (dir->flags & IF_COND))
393 pfile->mi_valid = false;
395 /* Kluge alert. In order to be sure that code like this
397 #define HASH #
398 HASH define foo bar
400 does not cause '#define foo bar' to get executed when
401 compiled with -save-temps, we recognize directives in
402 -fpreprocessed mode only if the # is in column 1. cppmacro.c
403 puts a space in front of any '#' at the start of a macro. */
404 if (CPP_OPTION (pfile, preprocessed)
405 && (indented || !(dir->flags & IN_I)))
407 skip = 0;
408 dir = 0;
410 else
412 /* In failed conditional groups, all non-conditional
413 directives are ignored. Before doing that, whether
414 skipping or not, we should lex angle-bracketed headers
415 correctly, and maybe output some diagnostics. */
416 pfile->state.angled_headers = dir->flags & INCL;
417 if (! CPP_OPTION (pfile, preprocessed))
418 directive_diagnostics (pfile, dir, indented);
419 if (pfile->state.skipping && !(dir->flags & COND))
420 dir = 0;
423 else if (dname->type == CPP_EOF)
424 ; /* CPP_EOF is the "null directive". */
425 else
427 /* An unknown directive. Don't complain about it in assembly
428 source: we don't know where the comments are, and # may
429 introduce assembler pseudo-ops. Don't complain about invalid
430 directives in skipped conditional groups (6.10 p4). */
431 if (CPP_OPTION (pfile, lang) == CLK_ASM)
432 skip = 0;
433 else if (!pfile->state.skipping)
434 cpp_error (pfile, DL_ERROR, "invalid preprocessing directive #%s",
435 cpp_token_as_text (pfile, dname));
438 pfile->directive = dir;
439 if (CPP_OPTION (pfile, traditional))
440 prepare_directive_trad (pfile);
442 if (dir)
443 (*pfile->directive->handler) (pfile);
444 else if (skip == 0)
445 _cpp_backup_tokens (pfile, 1);
447 end_directive (pfile, skip);
448 if (was_parsing_args)
450 /* Restore state when within macro args. */
451 pfile->state.parsing_args = 2;
452 pfile->state.prevent_expansion = 1;
453 pfile->buffer->saved_flags |= PREV_WHITE;
455 return skip;
458 /* Directive handler wrapper used by the command line option
459 processor. */
460 static void
461 run_directive (pfile, dir_no, buf, count)
462 cpp_reader *pfile;
463 int dir_no;
464 const char *buf;
465 size_t count;
467 cpp_push_buffer (pfile, (const uchar *) buf, count,
468 /* from_stage3 */ true, 1);
469 start_directive (pfile);
470 /* We don't want a leading # to be interpreted as a directive. */
471 pfile->buffer->saved_flags = 0;
472 pfile->directive = &dtable[dir_no];
473 if (CPP_OPTION (pfile, traditional))
474 prepare_directive_trad (pfile);
475 (void) (*pfile->directive->handler) (pfile);
476 end_directive (pfile, 1);
477 _cpp_pop_buffer (pfile);
480 /* Checks for validity the macro name in #define, #undef, #ifdef and
481 #ifndef directives. */
482 static cpp_hashnode *
483 lex_macro_node (pfile)
484 cpp_reader *pfile;
486 const cpp_token *token = _cpp_lex_token (pfile);
488 /* The token immediately after #define must be an identifier. That
489 identifier may not be "defined", per C99 6.10.8p4.
490 In C++, it may not be any of the "named operators" either,
491 per C++98 [lex.digraph], [lex.key].
492 Finally, the identifier may not have been poisoned. (In that case
493 the lexer has issued the error message for us.) */
495 if (token->type == CPP_NAME)
497 cpp_hashnode *node = token->val.node;
499 if (node == pfile->spec_nodes.n_defined)
500 cpp_error (pfile, DL_ERROR,
501 "\"defined\" cannot be used as a macro name");
502 else if (! (node->flags & NODE_POISONED))
503 return node;
505 else if (token->flags & NAMED_OP)
506 cpp_error (pfile, DL_ERROR,
507 "\"%s\" cannot be used as a macro name as it is an operator in C++",
508 NODE_NAME (token->val.node));
509 else if (token->type == CPP_EOF)
510 cpp_error (pfile, DL_ERROR, "no macro name given in #%s directive",
511 pfile->directive->name);
512 else
513 cpp_error (pfile, DL_ERROR, "macro names must be identifiers");
515 return NULL;
518 /* Process a #define directive. Most work is done in cppmacro.c. */
519 static void
520 do_define (pfile)
521 cpp_reader *pfile;
523 cpp_hashnode *node = lex_macro_node (pfile);
525 if (node)
527 /* If we have been requested to expand comments into macros,
528 then re-enable saving of comments. */
529 pfile->state.save_comments =
530 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
532 if (_cpp_create_definition (pfile, node))
533 if (pfile->cb.define)
534 (*pfile->cb.define) (pfile, pfile->directive_line, node);
538 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
539 static void
540 do_undef (pfile)
541 cpp_reader *pfile;
543 cpp_hashnode *node = lex_macro_node (pfile);
545 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
546 is not currently defined as a macro name. */
547 if (node && node->type == NT_MACRO)
549 if (pfile->cb.undef)
550 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
552 if (node->flags & NODE_WARN)
553 cpp_error (pfile, DL_WARNING, "undefining \"%s\"", NODE_NAME (node));
555 _cpp_free_definition (node);
557 check_eol (pfile);
560 /* Helper routine used by parse_include. Reinterpret the current line
561 as an h-char-sequence (< ... >); we are looking at the first token
562 after the <. Returns the header as a token, or NULL on failure. */
563 static const cpp_token *
564 glue_header_name (pfile)
565 cpp_reader *pfile;
567 cpp_token *header = NULL;
568 const cpp_token *token;
569 unsigned char *buffer;
570 size_t len, total_len = 0, capacity = 1024;
572 /* To avoid lexed tokens overwriting our glued name, we can only
573 allocate from the string pool once we've lexed everything. */
574 buffer = (unsigned char *) xmalloc (capacity);
575 for (;;)
577 token = cpp_get_token (pfile);
579 if (token->type == CPP_GREATER || token->type == CPP_EOF)
580 break;
582 len = cpp_token_len (token);
583 if (total_len + len > capacity)
585 capacity = (capacity + len) * 2;
586 buffer = (unsigned char *) xrealloc (buffer, capacity);
589 if (token->flags & PREV_WHITE)
590 buffer[total_len++] = ' ';
592 total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer;
595 if (token->type == CPP_EOF)
596 cpp_error (pfile, DL_ERROR, "missing terminating > character");
597 else
599 unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1);
600 memcpy (token_mem, buffer, total_len);
601 token_mem[total_len] = '\0';
603 header = _cpp_temp_token (pfile);
604 header->type = CPP_HEADER_NAME;
605 header->flags = 0;
606 header->val.str.len = total_len;
607 header->val.str.text = token_mem;
610 free ((PTR) buffer);
611 return header;
614 /* Returns the header string of #include, #include_next, #import and
615 #pragma dependency. Returns NULL on error. */
616 static const cpp_token *
617 parse_include (pfile)
618 cpp_reader *pfile;
620 const unsigned char *dir;
621 const cpp_token *header;
623 if (pfile->directive == &dtable[T_PRAGMA])
624 dir = U"pragma dependency";
625 else
626 dir = pfile->directive->name;
628 /* Allow macro expansion. */
629 header = cpp_get_token (pfile);
630 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
632 if (header->type != CPP_LESS)
634 cpp_error (pfile, DL_ERROR,
635 "#%s expects \"FILENAME\" or <FILENAME>", dir);
636 return NULL;
639 header = glue_header_name (pfile);
640 if (header == NULL)
641 return header;
644 if (header->val.str.len == 0)
646 cpp_error (pfile, DL_ERROR, "empty file name in #%s", dir);
647 return NULL;
650 return header;
653 /* Handle #include, #include_next and #import. */
654 static void
655 do_include_common (pfile, type)
656 cpp_reader *pfile;
657 enum include_type type;
659 const cpp_token *header;
661 /* For #include_next, if this is the primary source file, warn and
662 use the normal search logic. */
663 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
665 cpp_error (pfile, DL_WARNING, "#include_next in primary source file");
666 type = IT_INCLUDE;
668 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
670 CPP_OPTION (pfile, warn_import) = 0;
671 cpp_error (pfile, DL_WARNING,
672 "#import is obsolete, use an #ifndef wrapper in the header file");
675 header = parse_include (pfile);
676 if (header)
678 /* Prevent #include recursion. */
679 if (pfile->line_maps.depth >= CPP_STACK_MAX)
680 cpp_error (pfile, DL_ERROR, "#include nested too deeply");
681 else
683 check_eol (pfile);
684 /* Get out of macro context, if we are. */
685 skip_rest_of_line (pfile);
686 if (pfile->cb.include)
687 (*pfile->cb.include) (pfile, pfile->directive_line,
688 pfile->directive->name, header);
689 _cpp_execute_include (pfile, header, type);
694 static void
695 do_include (pfile)
696 cpp_reader *pfile;
698 do_include_common (pfile, IT_INCLUDE);
701 static void
702 do_import (pfile)
703 cpp_reader *pfile;
705 do_include_common (pfile, IT_IMPORT);
708 static void
709 do_include_next (pfile)
710 cpp_reader *pfile;
712 do_include_common (pfile, IT_INCLUDE_NEXT);
715 /* Subroutine of do_linemarker. Read possible flags after file name.
716 LAST is the last flag seen; 0 if this is the first flag. Return the
717 flag if it is valid, 0 at the end of the directive. Otherwise
718 complain. */
719 static unsigned int
720 read_flag (pfile, last)
721 cpp_reader *pfile;
722 unsigned int last;
724 const cpp_token *token = _cpp_lex_token (pfile);
726 if (token->type == CPP_NUMBER && token->val.str.len == 1)
728 unsigned int flag = token->val.str.text[0] - '0';
730 if (flag > last && flag <= 4
731 && (flag != 4 || last == 3)
732 && (flag != 2 || last == 0))
733 return flag;
736 if (token->type != CPP_EOF)
737 cpp_error (pfile, DL_ERROR, "invalid flag \"%s\" in line directive",
738 cpp_token_as_text (pfile, token));
739 return 0;
742 /* Subroutine of do_line and do_linemarker. Returns a version of STR
743 which has a NUL terminator and all escape sequences converted to
744 their equivalents. Temporary, hopefully. */
745 static uchar *
746 dequote_string (pfile, str, len)
747 cpp_reader *pfile;
748 const uchar *str;
749 unsigned int len;
751 uchar *result = _cpp_unaligned_alloc (pfile, len + 1);
752 uchar *dst = result;
753 const uchar *limit = str + len;
754 cppchar_t c;
756 while (str < limit)
758 c = *str++;
759 if (c != '\\')
760 *dst++ = c;
761 else
762 *dst++ = cpp_parse_escape (pfile, &str, limit, 0);
764 *dst++ = '\0';
765 return result;
768 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
769 of length LEN, to binary; store it in NUMP, and return 0 if the
770 number was well-formed, 1 if not. Temporary, hopefully. */
771 static int
772 strtoul_for_line (str, len, nump)
773 const uchar *str;
774 unsigned int len;
775 unsigned long *nump;
777 unsigned long reg = 0;
778 uchar c;
779 while (len--)
781 c = *str++;
782 if (!ISDIGIT (c))
783 return 1;
784 reg *= 10;
785 reg += c - '0';
787 *nump = reg;
788 return 0;
791 /* Interpret #line command.
792 Note that the filename string (if any) is a true string constant
793 (escapes are interpreted), unlike in #line. */
794 static void
795 do_line (pfile)
796 cpp_reader *pfile;
798 const cpp_token *token;
799 const char *new_file = pfile->map->to_file;
800 unsigned long new_lineno;
802 /* C99 raised the minimum limit on #line numbers. */
803 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
805 /* #line commands expand macros. */
806 token = cpp_get_token (pfile);
807 if (token->type != CPP_NUMBER
808 || strtoul_for_line (token->val.str.text, token->val.str.len,
809 &new_lineno))
811 cpp_error (pfile, DL_ERROR,
812 "\"%s\" after #line is not a positive integer",
813 cpp_token_as_text (pfile, token));
814 return;
817 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
818 cpp_error (pfile, DL_PEDWARN, "line number out of range");
820 token = cpp_get_token (pfile);
821 if (token->type == CPP_STRING)
823 new_file = (const char *) dequote_string (pfile, token->val.str.text,
824 token->val.str.len);
825 check_eol (pfile);
827 else if (token->type != CPP_EOF)
829 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
830 cpp_token_as_text (pfile, token));
831 return;
834 skip_rest_of_line (pfile);
835 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
836 pfile->map->sysp);
839 /* Interpret the # 44 "file" [flags] notation, which has slightly
840 different syntax and semantics from #line: Flags are allowed,
841 and we never complain about the line number being too big. */
842 static void
843 do_linemarker (pfile)
844 cpp_reader *pfile;
846 const cpp_token *token;
847 const char *new_file = pfile->map->to_file;
848 unsigned long new_lineno;
849 unsigned int new_sysp = pfile->map->sysp;
850 enum lc_reason reason = LC_RENAME;
851 int flag;
853 /* Back up so we can get the number again. Putting this in
854 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
855 some circumstances, which can segfault. */
856 _cpp_backup_tokens (pfile, 1);
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,
862 &new_lineno))
864 cpp_error (pfile, DL_ERROR, "\"%s\" after # is not a positive integer",
865 cpp_token_as_text (pfile, token));
866 return;
869 token = cpp_get_token (pfile);
870 if (token->type == CPP_STRING)
872 new_file = (const char *) dequote_string (pfile, token->val.str.text,
873 token->val.str.len);
874 new_sysp = 0;
875 flag = read_flag (pfile, 0);
876 if (flag == 1)
878 reason = LC_ENTER;
879 /* Fake an include for cpp_included (). */
880 _cpp_fake_include (pfile, new_file);
881 flag = read_flag (pfile, flag);
883 else if (flag == 2)
885 reason = LC_LEAVE;
886 flag = read_flag (pfile, flag);
888 if (flag == 3)
890 new_sysp = 1;
891 flag = read_flag (pfile, flag);
892 if (flag == 4)
893 new_sysp = 2;
896 check_eol (pfile);
898 else if (token->type != CPP_EOF)
900 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
901 cpp_token_as_text (pfile, token));
902 return;
905 skip_rest_of_line (pfile);
906 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
909 /* Arrange the file_change callback. pfile->line has changed to
910 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
911 header, 2 for a system header that needs to be extern "C" protected,
912 and zero otherwise. */
913 void
914 _cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
915 cpp_reader *pfile;
916 enum lc_reason reason;
917 const char *to_file;
918 unsigned int file_line;
919 unsigned int sysp;
921 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
922 pfile->line, to_file, file_line);
924 if (pfile->cb.file_change)
925 (*pfile->cb.file_change) (pfile, pfile->map);
928 /* Report a warning or error detected by the program we are
929 processing. Use the directive's tokens in the error message. */
930 static void
931 do_diagnostic (pfile, code, print_dir)
932 cpp_reader *pfile;
933 int code;
934 int print_dir;
936 if (_cpp_begin_message (pfile, code,
937 pfile->cur_token[-1].line,
938 pfile->cur_token[-1].col))
940 if (print_dir)
941 fprintf (stderr, "#%s ", pfile->directive->name);
942 pfile->state.prevent_expansion++;
943 cpp_output_line (pfile, stderr);
944 pfile->state.prevent_expansion--;
948 static void
949 do_error (pfile)
950 cpp_reader *pfile;
952 do_diagnostic (pfile, DL_ERROR, 1);
955 static void
956 do_warning (pfile)
957 cpp_reader *pfile;
959 /* We want #warning diagnostics to be emitted in system headers too. */
960 do_diagnostic (pfile, DL_WARNING_SYSHDR, 1);
963 /* Report program identification. */
964 static void
965 do_ident (pfile)
966 cpp_reader *pfile;
968 const cpp_token *str = cpp_get_token (pfile);
970 if (str->type != CPP_STRING)
971 cpp_error (pfile, DL_ERROR, "invalid #ident directive");
972 else if (pfile->cb.ident)
973 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
975 check_eol (pfile);
978 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
979 matching entry, or NULL if none is found. The returned entry could
980 be the start of a namespace chain, or a pragma. */
981 static struct pragma_entry *
982 lookup_pragma_entry (chain, pragma)
983 struct pragma_entry *chain;
984 const cpp_hashnode *pragma;
986 while (chain && chain->pragma != pragma)
987 chain = chain->next;
989 return chain;
992 /* Create and insert a pragma entry for NAME at the beginning of a
993 singly-linked CHAIN. If handler is NULL, it is a namespace,
994 otherwise it is a pragma and its handler. */
995 static struct pragma_entry *
996 insert_pragma_entry (pfile, chain, pragma, handler)
997 cpp_reader *pfile;
998 struct pragma_entry **chain;
999 const cpp_hashnode *pragma;
1000 pragma_cb handler;
1002 struct pragma_entry *new;
1004 new = (struct pragma_entry *)
1005 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1006 new->pragma = pragma;
1007 if (handler)
1009 new->is_nspace = 0;
1010 new->u.handler = handler;
1012 else
1014 new->is_nspace = 1;
1015 new->u.space = NULL;
1018 new->next = *chain;
1019 *chain = new;
1020 return new;
1023 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1024 goes in the global namespace. HANDLER is the handler it will call,
1025 which must be non-NULL. */
1026 void
1027 cpp_register_pragma (pfile, space, name, handler)
1028 cpp_reader *pfile;
1029 const char *space;
1030 const char *name;
1031 pragma_cb handler;
1033 struct pragma_entry **chain = &pfile->pragmas;
1034 struct pragma_entry *entry;
1035 const cpp_hashnode *node;
1037 if (!handler)
1038 abort ();
1040 if (space)
1042 node = cpp_lookup (pfile, U space, strlen (space));
1043 entry = lookup_pragma_entry (*chain, node);
1044 if (!entry)
1045 entry = insert_pragma_entry (pfile, chain, node, NULL);
1046 else if (!entry->is_nspace)
1047 goto clash;
1048 chain = &entry->u.space;
1051 /* Check for duplicates. */
1052 node = cpp_lookup (pfile, U name, strlen (name));
1053 entry = lookup_pragma_entry (*chain, node);
1054 if (entry)
1056 if (entry->is_nspace)
1057 clash:
1058 cpp_error (pfile, DL_ICE,
1059 "registering \"%s\" as both a pragma and a pragma namespace",
1060 NODE_NAME (node));
1061 else if (space)
1062 cpp_error (pfile, DL_ICE, "#pragma %s %s is already registered",
1063 space, name);
1064 else
1065 cpp_error (pfile, DL_ICE, "#pragma %s is already registered", name);
1067 else
1068 insert_pragma_entry (pfile, chain, node, handler);
1071 /* Register the pragmas the preprocessor itself handles. */
1072 void
1073 _cpp_init_internal_pragmas (pfile)
1074 cpp_reader *pfile;
1076 /* Pragmas in the global namespace. */
1077 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1079 /* New GCC-specific pragmas should be put in the GCC namespace. */
1080 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1081 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1082 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
1085 /* Pragmata handling. We handle some, and pass the rest on to the
1086 front end. C99 defines three pragmas and says that no macro
1087 expansion is to be performed on them; whether or not macro
1088 expansion happens for other pragmas is implementation defined.
1089 This implementation never macro-expands the text after #pragma. */
1090 static void
1091 do_pragma (pfile)
1092 cpp_reader *pfile;
1094 const struct pragma_entry *p = NULL;
1095 const cpp_token *token;
1096 unsigned int count = 1;
1098 pfile->state.prevent_expansion++;
1100 token = cpp_get_token (pfile);
1101 if (token->type == CPP_NAME)
1103 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1104 if (p && p->is_nspace)
1106 count = 2;
1107 token = cpp_get_token (pfile);
1108 if (token->type == CPP_NAME)
1109 p = lookup_pragma_entry (p->u.space, token->val.node);
1110 else
1111 p = NULL;
1115 /* FIXME. This is an awful kludge to get the front ends to update
1116 their notion of line number for diagnostic purposes. The line
1117 number should be passed to the handler and they should do it
1118 themselves. Stand-alone CPP must ignore us, otherwise it will
1119 prefix the directive with spaces, hence the 1. Ugh. */
1120 if (pfile->cb.line_change)
1121 (*pfile->cb.line_change)(pfile, token, 1);
1123 if (p)
1124 (*p->u.handler) (pfile);
1125 else if (pfile->cb.def_pragma)
1127 _cpp_backup_tokens (pfile, count);
1128 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1131 pfile->state.prevent_expansion--;
1134 /* Handle #pragma once. */
1135 static void
1136 do_pragma_once (pfile)
1137 cpp_reader *pfile;
1139 cpp_error (pfile, DL_WARNING, "#pragma once is obsolete");
1141 if (pfile->buffer->prev == NULL)
1142 cpp_error (pfile, DL_WARNING, "#pragma once in main file");
1143 else
1144 _cpp_never_reread (pfile->buffer->inc);
1146 check_eol (pfile);
1149 /* Handle #pragma GCC poison, to poison one or more identifiers so
1150 that the lexer produces a hard error for each subsequent usage. */
1151 static void
1152 do_pragma_poison (pfile)
1153 cpp_reader *pfile;
1155 const cpp_token *tok;
1156 cpp_hashnode *hp;
1158 pfile->state.poisoned_ok = 1;
1159 for (;;)
1161 tok = _cpp_lex_token (pfile);
1162 if (tok->type == CPP_EOF)
1163 break;
1164 if (tok->type != CPP_NAME)
1166 cpp_error (pfile, DL_ERROR, "invalid #pragma GCC poison directive");
1167 break;
1170 hp = tok->val.node;
1171 if (hp->flags & NODE_POISONED)
1172 continue;
1174 if (hp->type == NT_MACRO)
1175 cpp_error (pfile, DL_WARNING, "poisoning existing macro \"%s\"",
1176 NODE_NAME (hp));
1177 _cpp_free_definition (hp);
1178 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1180 pfile->state.poisoned_ok = 0;
1183 /* Mark the current header as a system header. This will suppress
1184 some categories of warnings (notably those from -pedantic). It is
1185 intended for use in system libraries that cannot be implemented in
1186 conforming C, but cannot be certain that their headers appear in a
1187 system include directory. To prevent abuse, it is rejected in the
1188 primary source file. */
1189 static void
1190 do_pragma_system_header (pfile)
1191 cpp_reader *pfile;
1193 cpp_buffer *buffer = pfile->buffer;
1195 if (buffer->prev == 0)
1196 cpp_error (pfile, DL_WARNING,
1197 "#pragma system_header ignored outside include file");
1198 else
1200 check_eol (pfile);
1201 skip_rest_of_line (pfile);
1202 cpp_make_system_header (pfile, 1, 0);
1206 /* Check the modified date of the current include file against a specified
1207 file. Issue a diagnostic, if the specified file is newer. We use this to
1208 determine if a fixed header should be refixed. */
1209 static void
1210 do_pragma_dependency (pfile)
1211 cpp_reader *pfile;
1213 const cpp_token *header;
1214 int ordering;
1216 header = parse_include (pfile);
1217 if (!header)
1218 return;
1220 ordering = _cpp_compare_file_date (pfile, header);
1221 if (ordering < 0)
1222 cpp_error (pfile, DL_WARNING, "cannot find source %s",
1223 cpp_token_as_text (pfile, header));
1224 else if (ordering > 0)
1226 cpp_error (pfile, DL_WARNING, "current file is older than %s",
1227 cpp_token_as_text (pfile, header));
1228 if (cpp_get_token (pfile)->type != CPP_EOF)
1230 _cpp_backup_tokens (pfile, 1);
1231 do_diagnostic (pfile, DL_WARNING, 0);
1236 /* Get a token but skip padding. */
1237 static const cpp_token *
1238 get_token_no_padding (pfile)
1239 cpp_reader *pfile;
1241 for (;;)
1243 const cpp_token *result = cpp_get_token (pfile);
1244 if (result->type != CPP_PADDING)
1245 return result;
1249 /* Check syntax is "(string-literal)". Returns the string on success,
1250 or NULL on failure. */
1251 static const cpp_token *
1252 get__Pragma_string (pfile)
1253 cpp_reader *pfile;
1255 const cpp_token *string;
1257 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1258 return NULL;
1260 string = get_token_no_padding (pfile);
1261 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1262 return NULL;
1264 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1265 return NULL;
1267 return string;
1270 /* Destringize IN into a temporary buffer, by removing the first \ of
1271 \" and \\ sequences, and process the result as a #pragma directive. */
1272 static void
1273 destringize_and_run (pfile, in)
1274 cpp_reader *pfile;
1275 const cpp_string *in;
1277 const unsigned char *src, *limit;
1278 char *dest, *result;
1280 dest = result = alloca (in->len + 1);
1281 for (src = in->text, limit = src + in->len; src < limit;)
1283 /* We know there is a character following the backslash. */
1284 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1285 src++;
1286 *dest++ = *src++;
1288 *dest = '\0';
1290 run_directive (pfile, T_PRAGMA, result, dest - result);
1293 /* Handle the _Pragma operator. */
1294 void
1295 _cpp_do__Pragma (pfile)
1296 cpp_reader *pfile;
1298 const cpp_token *string = get__Pragma_string (pfile);
1300 if (!string)
1301 cpp_error (pfile, DL_ERROR,
1302 "_Pragma takes a parenthesized string literal");
1303 else
1305 /* Ideally, we'd like
1306 token1 _Pragma ("foo") token2
1307 to be output as
1308 token1
1309 # 7 "file.c"
1310 #pragma foo
1311 # 7 "file.c"
1312 token2
1313 Getting these correct line markers is a little tricky. */
1315 unsigned int orig_line = pfile->line;
1316 destringize_and_run (pfile, &string->val.str);
1317 pfile->line = orig_line;
1318 pfile->buffer->saved_flags = BOL;
1322 /* Just ignore #sccs, on systems where we define it at all. */
1323 #ifdef SCCS_DIRECTIVE
1324 static void
1325 do_sccs (pfile)
1326 cpp_reader *pfile ATTRIBUTE_UNUSED;
1329 #endif
1331 /* Handle #ifdef. */
1332 static void
1333 do_ifdef (pfile)
1334 cpp_reader *pfile;
1336 int skip = 1;
1338 if (! pfile->state.skipping)
1340 const cpp_hashnode *node = lex_macro_node (pfile);
1342 if (node)
1343 skip = node->type != NT_MACRO;
1345 if (node)
1346 check_eol (pfile);
1349 push_conditional (pfile, skip, T_IFDEF, 0);
1352 /* Handle #ifndef. */
1353 static void
1354 do_ifndef (pfile)
1355 cpp_reader *pfile;
1357 int skip = 1;
1358 const cpp_hashnode *node = 0;
1360 if (! pfile->state.skipping)
1362 node = lex_macro_node (pfile);
1363 if (node)
1364 skip = node->type == NT_MACRO;
1366 if (node)
1367 check_eol (pfile);
1370 push_conditional (pfile, skip, T_IFNDEF, node);
1373 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1374 pfile->mi_ind_cmacro so we can handle multiple-include
1375 optimisations. If macro expansion occurs in the expression, we
1376 cannot treat it as a controlling conditional, since the expansion
1377 could change in the future. That is handled by cpp_get_token. */
1378 static void
1379 do_if (pfile)
1380 cpp_reader *pfile;
1382 int skip = 1;
1384 if (! pfile->state.skipping)
1385 skip = _cpp_parse_expr (pfile) == false;
1387 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1390 /* Flip skipping state if appropriate and continue without changing
1391 if_stack; this is so that the error message for missing #endif's
1392 etc. will point to the original #if. */
1393 static void
1394 do_else (pfile)
1395 cpp_reader *pfile;
1397 cpp_buffer *buffer = pfile->buffer;
1398 struct if_stack *ifs = buffer->if_stack;
1400 if (ifs == NULL)
1401 cpp_error (pfile, DL_ERROR, "#else without #if");
1402 else
1404 if (ifs->type == T_ELSE)
1406 cpp_error (pfile, DL_ERROR, "#else after #else");
1407 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1408 "the conditional began here");
1410 ifs->type = T_ELSE;
1412 /* Skip any future (erroneous) #elses or #elifs. */
1413 pfile->state.skipping = ifs->skip_elses;
1414 ifs->skip_elses = true;
1416 /* Invalidate any controlling macro. */
1417 ifs->mi_cmacro = 0;
1419 /* Only check EOL if was not originally skipping. */
1420 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1421 check_eol (pfile);
1425 /* Handle a #elif directive by not changing if_stack either. See the
1426 comment above do_else. */
1427 static void
1428 do_elif (pfile)
1429 cpp_reader *pfile;
1431 cpp_buffer *buffer = pfile->buffer;
1432 struct if_stack *ifs = buffer->if_stack;
1434 if (ifs == NULL)
1435 cpp_error (pfile, DL_ERROR, "#elif without #if");
1436 else
1438 if (ifs->type == T_ELSE)
1440 cpp_error (pfile, DL_ERROR, "#elif after #else");
1441 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1442 "the conditional began here");
1444 ifs->type = T_ELIF;
1446 /* Only evaluate this if we aren't skipping elses. During
1447 evaluation, set skipping to false to get lexer warnings. */
1448 if (ifs->skip_elses)
1449 pfile->state.skipping = 1;
1450 else
1452 pfile->state.skipping = 0;
1453 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1454 ifs->skip_elses = ! pfile->state.skipping;
1457 /* Invalidate any controlling macro. */
1458 ifs->mi_cmacro = 0;
1462 /* #endif pops the if stack and resets pfile->state.skipping. */
1463 static void
1464 do_endif (pfile)
1465 cpp_reader *pfile;
1467 cpp_buffer *buffer = pfile->buffer;
1468 struct if_stack *ifs = buffer->if_stack;
1470 if (ifs == NULL)
1471 cpp_error (pfile, DL_ERROR, "#endif without #if");
1472 else
1474 /* Only check EOL if was not originally skipping. */
1475 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1476 check_eol (pfile);
1478 /* If potential control macro, we go back outside again. */
1479 if (ifs->next == 0 && ifs->mi_cmacro)
1481 pfile->mi_valid = true;
1482 pfile->mi_cmacro = ifs->mi_cmacro;
1485 buffer->if_stack = ifs->next;
1486 pfile->state.skipping = ifs->was_skipping;
1487 obstack_free (&pfile->buffer_ob, ifs);
1491 /* Push an if_stack entry for a preprocessor conditional, and set
1492 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1493 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1494 we need to check here that we are at the top of the file. */
1495 static void
1496 push_conditional (pfile, skip, type, cmacro)
1497 cpp_reader *pfile;
1498 int skip;
1499 int type;
1500 const cpp_hashnode *cmacro;
1502 struct if_stack *ifs;
1503 cpp_buffer *buffer = pfile->buffer;
1505 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1506 ifs->line = pfile->directive_line;
1507 ifs->next = buffer->if_stack;
1508 ifs->skip_elses = pfile->state.skipping || !skip;
1509 ifs->was_skipping = pfile->state.skipping;
1510 ifs->type = type;
1511 /* This condition is effectively a test for top-of-file. */
1512 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1513 ifs->mi_cmacro = cmacro;
1514 else
1515 ifs->mi_cmacro = 0;
1517 pfile->state.skipping = skip;
1518 buffer->if_stack = ifs;
1521 /* Read the tokens of the answer into the macro pool, in a directive
1522 of type TYPE. Only commit the memory if we intend it as permanent
1523 storage, i.e. the #assert case. Returns 0 on success, and sets
1524 ANSWERP to point to the answer. */
1525 static int
1526 parse_answer (pfile, answerp, type)
1527 cpp_reader *pfile;
1528 struct answer **answerp;
1529 int type;
1531 const cpp_token *paren;
1532 struct answer *answer;
1533 unsigned int acount;
1535 /* In a conditional, it is legal to not have an open paren. We
1536 should save the following token in this case. */
1537 paren = cpp_get_token (pfile);
1539 /* If not a paren, see if we're OK. */
1540 if (paren->type != CPP_OPEN_PAREN)
1542 /* In a conditional no answer is a test for any answer. It
1543 could be followed by any token. */
1544 if (type == T_IF)
1546 _cpp_backup_tokens (pfile, 1);
1547 return 0;
1550 /* #unassert with no answer is valid - it removes all answers. */
1551 if (type == T_UNASSERT && paren->type == CPP_EOF)
1552 return 0;
1554 cpp_error (pfile, DL_ERROR, "missing '(' after predicate");
1555 return 1;
1558 for (acount = 0;; acount++)
1560 size_t room_needed;
1561 const cpp_token *token = cpp_get_token (pfile);
1562 cpp_token *dest;
1564 if (token->type == CPP_CLOSE_PAREN)
1565 break;
1567 if (token->type == CPP_EOF)
1569 cpp_error (pfile, DL_ERROR, "missing ')' to complete answer");
1570 return 1;
1573 /* struct answer includes the space for one token. */
1574 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1576 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1577 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1579 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1580 *dest = *token;
1582 /* Drop whitespace at start, for answer equivalence purposes. */
1583 if (acount == 0)
1584 dest->flags &= ~PREV_WHITE;
1587 if (acount == 0)
1589 cpp_error (pfile, DL_ERROR, "predicate's answer is empty");
1590 return 1;
1593 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1594 answer->count = acount;
1595 answer->next = NULL;
1596 *answerp = answer;
1598 return 0;
1601 /* Parses an assertion directive of type TYPE, returning a pointer to
1602 the hash node of the predicate, or 0 on error. If an answer was
1603 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1604 static cpp_hashnode *
1605 parse_assertion (pfile, answerp, type)
1606 cpp_reader *pfile;
1607 struct answer **answerp;
1608 int type;
1610 cpp_hashnode *result = 0;
1611 const cpp_token *predicate;
1613 /* We don't expand predicates or answers. */
1614 pfile->state.prevent_expansion++;
1616 *answerp = 0;
1617 predicate = cpp_get_token (pfile);
1618 if (predicate->type == CPP_EOF)
1619 cpp_error (pfile, DL_ERROR, "assertion without predicate");
1620 else if (predicate->type != CPP_NAME)
1621 cpp_error (pfile, DL_ERROR, "predicate must be an identifier");
1622 else if (parse_answer (pfile, answerp, type) == 0)
1624 unsigned int len = NODE_LEN (predicate->val.node);
1625 unsigned char *sym = alloca (len + 1);
1627 /* Prefix '#' to get it out of macro namespace. */
1628 sym[0] = '#';
1629 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1630 result = cpp_lookup (pfile, sym, len + 1);
1633 pfile->state.prevent_expansion--;
1634 return result;
1637 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1638 or a pointer to NULL if the answer is not in the chain. */
1639 static struct answer **
1640 find_answer (node, candidate)
1641 cpp_hashnode *node;
1642 const struct answer *candidate;
1644 unsigned int i;
1645 struct answer **result;
1647 for (result = &node->value.answers; *result; result = &(*result)->next)
1649 struct answer *answer = *result;
1651 if (answer->count == candidate->count)
1653 for (i = 0; i < answer->count; i++)
1654 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1655 break;
1657 if (i == answer->count)
1658 break;
1662 return result;
1665 /* Test an assertion within a preprocessor conditional. Returns
1666 non-zero on failure, zero on success. On success, the result of
1667 the test is written into VALUE. */
1669 _cpp_test_assertion (pfile, value)
1670 cpp_reader *pfile;
1671 unsigned int *value;
1673 struct answer *answer;
1674 cpp_hashnode *node;
1676 node = parse_assertion (pfile, &answer, T_IF);
1677 if (node)
1678 *value = (node->type == NT_ASSERTION &&
1679 (answer == 0 || *find_answer (node, answer) != 0));
1680 else if (pfile->cur_token[-1].type == CPP_EOF)
1681 _cpp_backup_tokens (pfile, 1);
1683 /* We don't commit the memory for the answer - it's temporary only. */
1684 return node == 0;
1687 /* Handle #assert. */
1688 static void
1689 do_assert (pfile)
1690 cpp_reader *pfile;
1692 struct answer *new_answer;
1693 cpp_hashnode *node;
1695 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1696 if (node)
1698 /* Place the new answer in the answer list. First check there
1699 is not a duplicate. */
1700 new_answer->next = 0;
1701 if (node->type == NT_ASSERTION)
1703 if (*find_answer (node, new_answer))
1705 cpp_error (pfile, DL_WARNING, "\"%s\" re-asserted",
1706 NODE_NAME (node) + 1);
1707 return;
1709 new_answer->next = node->value.answers;
1712 node->type = NT_ASSERTION;
1713 node->value.answers = new_answer;
1714 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1715 + (new_answer->count - 1)
1716 * sizeof (cpp_token));
1717 check_eol (pfile);
1721 /* Handle #unassert. */
1722 static void
1723 do_unassert (pfile)
1724 cpp_reader *pfile;
1726 cpp_hashnode *node;
1727 struct answer *answer;
1729 node = parse_assertion (pfile, &answer, T_UNASSERT);
1730 /* It isn't an error to #unassert something that isn't asserted. */
1731 if (node && node->type == NT_ASSERTION)
1733 if (answer)
1735 struct answer **p = find_answer (node, answer), *temp;
1737 /* Remove the answer from the list. */
1738 temp = *p;
1739 if (temp)
1740 *p = temp->next;
1742 /* Did we free the last answer? */
1743 if (node->value.answers == 0)
1744 node->type = NT_VOID;
1746 check_eol (pfile);
1748 else
1749 _cpp_free_definition (node);
1752 /* We don't commit the memory for the answer - it's temporary only. */
1755 /* These are for -D, -U, -A. */
1757 /* Process the string STR as if it appeared as the body of a #define.
1758 If STR is just an identifier, define it with value 1.
1759 If STR has anything after the identifier, then it should
1760 be identifier=definition. */
1761 void
1762 cpp_define (pfile, str)
1763 cpp_reader *pfile;
1764 const char *str;
1766 char *buf, *p;
1767 size_t count;
1769 /* Copy the entire option so we can modify it.
1770 Change the first "=" in the string to a space. If there is none,
1771 tack " 1" on the end. */
1773 count = strlen (str);
1774 buf = (char *) alloca (count + 3);
1775 memcpy (buf, str, count);
1777 p = strchr (str, '=');
1778 if (p)
1779 buf[p - str] = ' ';
1780 else
1782 buf[count++] = ' ';
1783 buf[count++] = '1';
1785 buf[count] = '\0';
1787 run_directive (pfile, T_DEFINE, buf, count);
1790 /* Slight variant of the above for use by initialize_builtins. */
1791 void
1792 _cpp_define_builtin (pfile, str)
1793 cpp_reader *pfile;
1794 const char *str;
1796 run_directive (pfile, T_DEFINE, str, strlen (str));
1799 /* Process MACRO as if it appeared as the body of an #undef. */
1800 void
1801 cpp_undef (pfile, macro)
1802 cpp_reader *pfile;
1803 const char *macro;
1805 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1808 /* Process the string STR as if it appeared as the body of a #assert. */
1809 void
1810 cpp_assert (pfile, str)
1811 cpp_reader *pfile;
1812 const char *str;
1814 handle_assertion (pfile, str, T_ASSERT);
1817 /* Process STR as if it appeared as the body of an #unassert. */
1818 void
1819 cpp_unassert (pfile, str)
1820 cpp_reader *pfile;
1821 const char *str;
1823 handle_assertion (pfile, str, T_UNASSERT);
1826 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1827 static void
1828 handle_assertion (pfile, str, type)
1829 cpp_reader *pfile;
1830 const char *str;
1831 int type;
1833 size_t count = strlen (str);
1834 const char *p = strchr (str, '=');
1836 if (p)
1838 /* Copy the entire option so we can modify it. Change the first
1839 "=" in the string to a '(', and tack a ')' on the end. */
1840 char *buf = (char *) alloca (count + 2);
1842 memcpy (buf, str, count);
1843 buf[p - str] = '(';
1844 buf[count++] = ')';
1845 buf[count] = '\0';
1846 str = buf;
1849 run_directive (pfile, type, str, count);
1852 /* The number of errors for a given reader. */
1853 unsigned int
1854 cpp_errors (pfile)
1855 cpp_reader *pfile;
1857 return pfile->errors;
1860 /* The options structure. */
1861 cpp_options *
1862 cpp_get_options (pfile)
1863 cpp_reader *pfile;
1865 return &pfile->opts;
1868 /* The callbacks structure. */
1869 cpp_callbacks *
1870 cpp_get_callbacks (pfile)
1871 cpp_reader *pfile;
1873 return &pfile->cb;
1876 /* The line map set. */
1877 const struct line_maps *
1878 cpp_get_line_maps (pfile)
1879 cpp_reader *pfile;
1881 return &pfile->line_maps;
1884 /* Copy the given callbacks structure to our own. */
1885 void
1886 cpp_set_callbacks (pfile, cb)
1887 cpp_reader *pfile;
1888 cpp_callbacks *cb;
1890 pfile->cb = *cb;
1893 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1894 doesn't fail. It does not generate a file change call back; that
1895 is the responsibility of the caller. */
1896 cpp_buffer *
1897 cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
1898 cpp_reader *pfile;
1899 const uchar *buffer;
1900 size_t len;
1901 int from_stage3;
1902 int return_at_eof;
1904 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1906 /* Clears, amongst other things, if_stack and mi_cmacro. */
1907 memset (new, 0, sizeof (cpp_buffer));
1909 new->line_base = new->buf = new->cur = buffer;
1910 new->rlimit = buffer + len;
1911 new->from_stage3 = from_stage3 || CPP_OPTION (pfile, traditional);
1912 new->prev = pfile->buffer;
1913 new->return_at_eof = return_at_eof;
1914 new->saved_flags = BOL;
1916 pfile->buffer = new;
1918 return new;
1921 /* Pops a single buffer, with a file change call-back if appropriate.
1922 Then pushes the next -include file, if any remain. */
1923 void
1924 _cpp_pop_buffer (pfile)
1925 cpp_reader *pfile;
1927 cpp_buffer *buffer = pfile->buffer;
1928 struct include_file *inc = buffer->inc;
1929 struct if_stack *ifs;
1931 /* Walk back up the conditional stack till we reach its level at
1932 entry to this file, issuing error messages. */
1933 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1934 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1935 "unterminated #%s", dtable[ifs->type].name);
1937 /* In case of a missing #endif. */
1938 pfile->state.skipping = 0;
1940 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
1941 pfile->buffer = buffer->prev;
1943 /* Free the buffer object now; we may want to push a new buffer
1944 in _cpp_push_next_include_file. */
1945 obstack_free (&pfile->buffer_ob, buffer);
1947 if (inc)
1949 _cpp_pop_file_buffer (pfile, inc);
1951 /* Don't generate a callback for popping the main file. */
1952 if (pfile->buffer)
1954 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
1956 /* If this is the main file, there may be some -include
1957 files left to push. */
1958 if (!pfile->buffer->prev)
1959 _cpp_maybe_push_include_file (pfile);
1964 /* Enter all recognised directives in the hash table. */
1965 void
1966 _cpp_init_directives (pfile)
1967 cpp_reader *pfile;
1969 unsigned int i;
1970 cpp_hashnode *node;
1972 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1974 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1975 node->directive_index = i + 1;