2002-06-13 Akim Demaille <akim@epita.fr>
[official-gcc.git] / gcc / cpplib.c
blobacc71e7120a6ae208336ead67ae146b74a60a1a0
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. If
76 INCL is set, macro expansion is special-cased and EXPAND should not
77 be set. */
78 #define COND (1 << 0)
79 #define IF_COND (1 << 1)
80 #define INCL (1 << 2)
81 #define IN_I (1 << 3)
82 #define EXPAND (1 << 4)
84 /* Defines one #-directive, including how to handle it. */
85 typedef void (*directive_handler) PARAMS ((cpp_reader *));
86 typedef struct directive directive;
87 struct directive
89 directive_handler handler; /* Function to handle directive. */
90 const uchar *name; /* Name of directive. */
91 unsigned short length; /* Length of name. */
92 unsigned char origin; /* Origin of directive. */
93 unsigned char flags; /* Flags describing this directive. */
96 /* Forward declarations. */
98 static void skip_rest_of_line PARAMS ((cpp_reader *));
99 static void check_eol PARAMS ((cpp_reader *));
100 static void start_directive PARAMS ((cpp_reader *));
101 static void prepare_directive_trad PARAMS ((cpp_reader *));
102 static void end_directive PARAMS ((cpp_reader *, int));
103 static void directive_diagnostics
104 PARAMS ((cpp_reader *, const directive *, int));
105 static void run_directive PARAMS ((cpp_reader *, int,
106 const char *, size_t));
107 static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
108 static const cpp_token *parse_include PARAMS ((cpp_reader *));
109 static void push_conditional PARAMS ((cpp_reader *, int, int,
110 const cpp_hashnode *));
111 static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
112 static uchar *dequote_string PARAMS ((cpp_reader *, const uchar *,
113 unsigned int));
114 static int strtoul_for_line PARAMS ((const uchar *, unsigned int,
115 unsigned long *));
116 static void do_diagnostic PARAMS ((cpp_reader *, int, int));
117 static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
118 static void do_include_common PARAMS ((cpp_reader *, enum include_type));
119 static struct pragma_entry *lookup_pragma_entry
120 PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma));
121 static struct pragma_entry *insert_pragma_entry
122 PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *,
123 pragma_cb));
124 static void do_pragma_once PARAMS ((cpp_reader *));
125 static void do_pragma_poison PARAMS ((cpp_reader *));
126 static void do_pragma_system_header PARAMS ((cpp_reader *));
127 static void do_pragma_dependency PARAMS ((cpp_reader *));
128 static void do_linemarker PARAMS ((cpp_reader *));
129 static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
130 static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
131 static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *));
132 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
133 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
134 int));
135 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
136 const struct answer *));
137 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
139 /* This is the table of directive handlers. It is ordered by
140 frequency of occurrence; the numbers at the end are directive
141 counts from all the source code I have lying around (egcs and libc
142 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
143 pcmcia-cs-3.0.9). This is no longer important as directive lookup
144 is now O(1). All extensions other than #warning and #include_next
145 are deprecated. The name is where the extension appears to have
146 come from. */
148 #define DIRECTIVE_TABLE \
149 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
150 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
151 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
152 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
153 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
154 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
155 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
156 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
157 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
158 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
159 D(error, T_ERROR, STDC89, 0) /* 475 */ \
160 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
161 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
162 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
163 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
164 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
165 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
166 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
167 SCCS_ENTRY /* 0 SVR4? */
169 /* #sccs is not always recognized. */
170 #ifdef SCCS_DIRECTIVE
171 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
172 #else
173 # define SCCS_ENTRY /* nothing */
174 #endif
176 /* Use the table to generate a series of prototypes, an enum for the
177 directive names, and an array of directive handlers. */
179 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
180 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
181 DIRECTIVE_TABLE
182 #undef D
184 #define D(n, tag, o, f) tag,
185 enum
187 DIRECTIVE_TABLE
188 N_DIRECTIVES
190 #undef D
192 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
193 #define D(name, t, origin, flags) \
194 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
195 sizeof STRINGX(name) - 1, origin, flags },
196 static const directive dtable[] =
198 DIRECTIVE_TABLE
200 #undef D
201 #undef DIRECTIVE_TABLE
203 /* Wrapper struct directive for linemarkers.
204 The origin is more or less true - the original K+R cpp
205 did use this notation in its preprocessed output. */
206 static const directive linemarker_dir =
208 do_linemarker, U"#", 1, KANDR, IN_I
211 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
213 /* Skip any remaining tokens in a directive. */
214 static void
215 skip_rest_of_line (pfile)
216 cpp_reader *pfile;
218 /* Discard all stacked contexts. */
219 while (pfile->context != &pfile->base_context)
220 _cpp_pop_context (pfile);
222 /* Sweep up all tokens remaining on the line. */
223 if (! SEEN_EOL ())
224 while (_cpp_lex_token (pfile)->type != CPP_EOF)
228 /* Ensure there are no stray tokens at the end of a directive. */
229 static void
230 check_eol (pfile)
231 cpp_reader *pfile;
233 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
234 cpp_error (pfile, DL_PEDWARN, "extra tokens at end of #%s directive",
235 pfile->directive->name);
238 /* Called when entering a directive, _Pragma or command-line directive. */
239 static void
240 start_directive (pfile)
241 cpp_reader *pfile;
243 /* Setup in-directive state. */
244 pfile->state.in_directive = 1;
245 pfile->state.save_comments = 0;
247 /* Some handlers need the position of the # for diagnostics. */
248 pfile->directive_line = pfile->line;
251 /* Called when leaving a directive, _Pragma or command-line directive. */
252 static void
253 end_directive (pfile, skip_line)
254 cpp_reader *pfile;
255 int skip_line;
257 if (CPP_OPTION (pfile, traditional))
259 if (!pfile->directive || pfile->directive == &dtable[T_DEFINE])
260 skip_line = false;
261 else
262 _cpp_remove_overlay (pfile);
265 /* We don't skip for an assembler #. */
266 if (skip_line)
268 skip_rest_of_line (pfile);
269 if (!pfile->keep_tokens)
271 pfile->cur_run = &pfile->base_run;
272 pfile->cur_token = pfile->base_run.base;
276 /* Restore state. */
277 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
278 pfile->state.in_directive = 0;
279 pfile->state.angled_headers = 0;
280 pfile->directive = 0;
283 /* Prepare to handle the directive in pfile->directive. */
284 static void
285 prepare_directive_trad (pfile)
286 cpp_reader *pfile;
288 if (pfile->directive == &dtable[T_DEFINE])
289 CUR (pfile->context) = pfile->buffer->cur;
290 else
292 bool no_expand = ! (pfile->directive->flags & EXPAND);
293 bool was_skipping = pfile->state.skipping;
295 pfile->state.skipping = false;
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);
307 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
308 the '#' was indented. */
309 static void
310 directive_diagnostics (pfile, dir, indented)
311 cpp_reader *pfile;
312 const directive *dir;
313 int indented;
315 /* Issue -pedantic warnings for extensions. */
316 if (CPP_PEDANTIC (pfile)
317 && ! pfile->state.skipping
318 && dir->origin == EXTENSION)
319 cpp_error (pfile, DL_PEDWARN, "#%s is a GCC extension", dir->name);
321 /* Traditionally, a directive is ignored unless its # is in
322 column 1. Therefore in code intended to work with K+R
323 compilers, directives added by C89 must have their #
324 indented, and directives present in traditional C must not.
325 This is true even of directives in skipped conditional
326 blocks. #elif cannot be used at all. */
327 if (CPP_WTRADITIONAL (pfile))
329 if (dir == &dtable[T_ELIF])
330 cpp_error (pfile, DL_WARNING,
331 "suggest not using #elif in traditional C");
332 else if (indented && dir->origin == KANDR)
333 cpp_error (pfile, DL_WARNING,
334 "traditional C ignores #%s with the # indented",
335 dir->name);
336 else if (!indented && dir->origin != KANDR)
337 cpp_error (pfile, DL_WARNING,
338 "suggest hiding #%s from traditional C with an indented #",
339 dir->name);
343 /* Check if we have a known directive. INDENTED is non-zero if the
344 '#' of the directive was indented. This function is in this file
345 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
346 non-zero if the line of tokens has been handled, zero if we should
347 continue processing the line. */
349 _cpp_handle_directive (pfile, indented)
350 cpp_reader *pfile;
351 int indented;
353 const directive *dir = 0;
354 const cpp_token *dname;
355 bool was_parsing_args = pfile->state.parsing_args;
356 int skip = 1;
358 if (was_parsing_args)
360 if (CPP_OPTION (pfile, pedantic))
361 cpp_error (pfile, DL_PEDWARN,
362 "embedding a directive within macro arguments is not portable");
363 pfile->state.parsing_args = 0;
364 pfile->state.prevent_expansion = 0;
366 start_directive (pfile);
367 dname = _cpp_lex_token (pfile);
369 if (dname->type == CPP_NAME)
371 if (dname->val.node->directive_index)
372 dir = &dtable[dname->val.node->directive_index - 1];
374 /* We do not recognise the # followed by a number extension in
375 assembler code. */
376 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
378 dir = &linemarker_dir;
379 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
380 && ! pfile->state.skipping)
381 cpp_error (pfile, DL_PEDWARN,
382 "style of line directive is a GCC extension");
385 if (dir)
387 /* If we have a directive that is not an opening conditional,
388 invalidate any control macro. */
389 if (! (dir->flags & IF_COND))
390 pfile->mi_valid = false;
392 /* Kluge alert. In order to be sure that code like this
394 #define HASH #
395 HASH define foo bar
397 does not cause '#define foo bar' to get executed when
398 compiled with -save-temps, we recognize directives in
399 -fpreprocessed mode only if the # is in column 1. cppmacro.c
400 puts a space in front of any '#' at the start of a macro. */
401 if (CPP_OPTION (pfile, preprocessed)
402 && (indented || !(dir->flags & IN_I)))
404 skip = 0;
405 dir = 0;
407 else
409 /* In failed conditional groups, all non-conditional
410 directives are ignored. Before doing that, whether
411 skipping or not, we should lex angle-bracketed headers
412 correctly, and maybe output some diagnostics. */
413 pfile->state.angled_headers = dir->flags & INCL;
414 if (! CPP_OPTION (pfile, preprocessed))
415 directive_diagnostics (pfile, dir, indented);
416 if (pfile->state.skipping && !(dir->flags & COND))
417 dir = 0;
420 else if (dname->type == CPP_EOF)
421 ; /* CPP_EOF is the "null directive". */
422 else
424 /* An unknown directive. Don't complain about it in assembly
425 source: we don't know where the comments are, and # may
426 introduce assembler pseudo-ops. Don't complain about invalid
427 directives in skipped conditional groups (6.10 p4). */
428 if (CPP_OPTION (pfile, lang) == CLK_ASM)
429 skip = 0;
430 else if (!pfile->state.skipping)
431 cpp_error (pfile, DL_ERROR, "invalid preprocessing directive #%s",
432 cpp_token_as_text (pfile, dname));
435 if (dir)
437 /* If we are processing a `#define' directive and we have been
438 requested to expand comments into macros, then re-enable
439 saving of comments. */
440 if (dir == &dtable[T_DEFINE])
441 pfile->state.save_comments =
442 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
444 pfile->directive = dir;
445 if (CPP_OPTION (pfile, traditional))
446 prepare_directive_trad (pfile);
447 (*pfile->directive->handler) (pfile);
449 else if (skip == 0)
450 _cpp_backup_tokens (pfile, 1);
452 end_directive (pfile, skip);
453 if (was_parsing_args)
455 /* Restore state when within macro args. */
456 pfile->state.parsing_args = 2;
457 pfile->state.prevent_expansion = 1;
458 pfile->buffer->saved_flags |= PREV_WHITE;
460 return skip;
463 /* Directive handler wrapper used by the command line option
464 processor. */
465 static void
466 run_directive (pfile, dir_no, buf, count)
467 cpp_reader *pfile;
468 int dir_no;
469 const char *buf;
470 size_t count;
472 cpp_push_buffer (pfile, (const uchar *) buf, count,
473 /* from_stage3 */ true, 1);
474 start_directive (pfile);
475 /* We don't want a leading # to be interpreted as a directive. */
476 pfile->buffer->saved_flags = 0;
477 pfile->directive = &dtable[dir_no];
478 if (CPP_OPTION (pfile, traditional))
479 prepare_directive_trad (pfile);
480 (void) (*pfile->directive->handler) (pfile);
481 end_directive (pfile, 1);
482 _cpp_pop_buffer (pfile);
485 /* Checks for validity the macro name in #define, #undef, #ifdef and
486 #ifndef directives. */
487 static cpp_hashnode *
488 lex_macro_node (pfile)
489 cpp_reader *pfile;
491 const cpp_token *token = _cpp_lex_token (pfile);
493 /* The token immediately after #define must be an identifier. That
494 identifier may not be "defined", per C99 6.10.8p4.
495 In C++, it may not be any of the "named operators" either,
496 per C++98 [lex.digraph], [lex.key].
497 Finally, the identifier may not have been poisoned. (In that case
498 the lexer has issued the error message for us.)
500 Note that if we're copying comments into macro expansions, we
501 could encounter comment tokens here, so eat them all up first. */
503 if (! CPP_OPTION (pfile, discard_comments_in_macro_exp))
505 while (token->type == CPP_COMMENT)
506 token = _cpp_lex_token (pfile);
509 if (token->type == CPP_NAME)
511 cpp_hashnode *node = token->val.node;
513 if (node == pfile->spec_nodes.n_defined)
514 cpp_error (pfile, DL_ERROR,
515 "\"defined\" cannot be used as a macro name");
516 else if (! (node->flags & NODE_POISONED))
517 return node;
519 else if (token->flags & NAMED_OP)
520 cpp_error (pfile, DL_ERROR,
521 "\"%s\" cannot be used as a macro name as it is an operator in C++",
522 NODE_NAME (token->val.node));
523 else if (token->type == CPP_EOF)
524 cpp_error (pfile, DL_ERROR, "no macro name given in #%s directive",
525 pfile->directive->name);
526 else
527 cpp_error (pfile, DL_ERROR, "macro names must be identifiers");
529 return NULL;
532 /* Process a #define directive. Most work is done in cppmacro.c. */
533 static void
534 do_define (pfile)
535 cpp_reader *pfile;
537 cpp_hashnode *node = lex_macro_node (pfile);
539 if (node)
541 if (_cpp_create_definition (pfile, node))
542 if (pfile->cb.define)
543 (*pfile->cb.define) (pfile, pfile->directive_line, node);
547 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
548 static void
549 do_undef (pfile)
550 cpp_reader *pfile;
552 cpp_hashnode *node = lex_macro_node (pfile);
554 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
555 is not currently defined as a macro name. */
556 if (node && node->type == NT_MACRO)
558 if (pfile->cb.undef)
559 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
561 if (node->flags & NODE_WARN)
562 cpp_error (pfile, DL_WARNING, "undefining \"%s\"", NODE_NAME (node));
564 _cpp_free_definition (node);
566 check_eol (pfile);
569 /* Helper routine used by parse_include. Reinterpret the current line
570 as an h-char-sequence (< ... >); we are looking at the first token
571 after the <. Returns the header as a token, or NULL on failure. */
572 static const cpp_token *
573 glue_header_name (pfile)
574 cpp_reader *pfile;
576 cpp_token *header = NULL;
577 const cpp_token *token;
578 unsigned char *buffer;
579 size_t len, total_len = 0, capacity = 1024;
581 /* To avoid lexed tokens overwriting our glued name, we can only
582 allocate from the string pool once we've lexed everything. */
583 buffer = (unsigned char *) xmalloc (capacity);
584 for (;;)
586 token = cpp_get_token (pfile);
588 if (token->type == CPP_GREATER || token->type == CPP_EOF)
589 break;
591 len = cpp_token_len (token);
592 if (total_len + len > capacity)
594 capacity = (capacity + len) * 2;
595 buffer = (unsigned char *) xrealloc (buffer, capacity);
598 if (token->flags & PREV_WHITE)
599 buffer[total_len++] = ' ';
601 total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer;
604 if (token->type == CPP_EOF)
605 cpp_error (pfile, DL_ERROR, "missing terminating > character");
606 else
608 unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1);
609 memcpy (token_mem, buffer, total_len);
610 token_mem[total_len] = '\0';
612 header = _cpp_temp_token (pfile);
613 header->type = CPP_HEADER_NAME;
614 header->flags = 0;
615 header->val.str.len = total_len;
616 header->val.str.text = token_mem;
619 free ((PTR) buffer);
620 return header;
623 /* Returns the header string of #include, #include_next, #import and
624 #pragma dependency. Returns NULL on error. */
625 static const cpp_token *
626 parse_include (pfile)
627 cpp_reader *pfile;
629 const unsigned char *dir;
630 const cpp_token *header;
632 if (pfile->directive == &dtable[T_PRAGMA])
633 dir = U"pragma dependency";
634 else
635 dir = pfile->directive->name;
637 /* Allow macro expansion. */
638 header = cpp_get_token (pfile);
639 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
641 if (header->type != CPP_LESS)
643 cpp_error (pfile, DL_ERROR,
644 "#%s expects \"FILENAME\" or <FILENAME>", dir);
645 return NULL;
648 header = glue_header_name (pfile);
649 if (header == NULL)
650 return header;
653 if (header->val.str.len == 0)
655 cpp_error (pfile, DL_ERROR, "empty file name in #%s", dir);
656 return NULL;
659 return header;
662 /* Handle #include, #include_next and #import. */
663 static void
664 do_include_common (pfile, type)
665 cpp_reader *pfile;
666 enum include_type type;
668 const cpp_token *header;
670 /* For #include_next, if this is the primary source file, warn and
671 use the normal search logic. */
672 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
674 cpp_error (pfile, DL_WARNING, "#include_next in primary source file");
675 type = IT_INCLUDE;
677 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
679 CPP_OPTION (pfile, warn_import) = 0;
680 cpp_error (pfile, DL_WARNING,
681 "#import is obsolete, use an #ifndef wrapper in the header file");
684 header = parse_include (pfile);
685 if (header)
687 /* Prevent #include recursion. */
688 if (pfile->line_maps.depth >= CPP_STACK_MAX)
689 cpp_error (pfile, DL_ERROR, "#include nested too deeply");
690 else
692 check_eol (pfile);
693 /* Get out of macro context, if we are. */
694 skip_rest_of_line (pfile);
695 if (pfile->cb.include)
696 (*pfile->cb.include) (pfile, pfile->directive_line,
697 pfile->directive->name, header);
699 _cpp_execute_include (pfile, header, type);
704 static void
705 do_include (pfile)
706 cpp_reader *pfile;
708 do_include_common (pfile, IT_INCLUDE);
711 static void
712 do_import (pfile)
713 cpp_reader *pfile;
715 do_include_common (pfile, IT_IMPORT);
718 static void
719 do_include_next (pfile)
720 cpp_reader *pfile;
722 do_include_common (pfile, IT_INCLUDE_NEXT);
725 /* Subroutine of do_linemarker. Read possible flags after file name.
726 LAST is the last flag seen; 0 if this is the first flag. Return the
727 flag if it is valid, 0 at the end of the directive. Otherwise
728 complain. */
729 static unsigned int
730 read_flag (pfile, last)
731 cpp_reader *pfile;
732 unsigned int last;
734 const cpp_token *token = _cpp_lex_token (pfile);
736 if (token->type == CPP_NUMBER && token->val.str.len == 1)
738 unsigned int flag = token->val.str.text[0] - '0';
740 if (flag > last && flag <= 4
741 && (flag != 4 || last == 3)
742 && (flag != 2 || last == 0))
743 return flag;
746 if (token->type != CPP_EOF)
747 cpp_error (pfile, DL_ERROR, "invalid flag \"%s\" in line directive",
748 cpp_token_as_text (pfile, token));
749 return 0;
752 /* Subroutine of do_line and do_linemarker. Returns a version of STR
753 which has a NUL terminator and all escape sequences converted to
754 their equivalents. Temporary, hopefully. */
755 static uchar *
756 dequote_string (pfile, str, len)
757 cpp_reader *pfile;
758 const uchar *str;
759 unsigned int len;
761 uchar *result = _cpp_unaligned_alloc (pfile, len + 1);
762 uchar *dst = result;
763 const uchar *limit = str + len;
764 cppchar_t c;
766 while (str < limit)
768 c = *str++;
769 if (c != '\\')
770 *dst++ = c;
771 else
772 *dst++ = cpp_parse_escape (pfile, &str, limit, 0);
774 *dst++ = '\0';
775 return result;
778 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
779 of length LEN, to binary; store it in NUMP, and return 0 if the
780 number was well-formed, 1 if not. Temporary, hopefully. */
781 static int
782 strtoul_for_line (str, len, nump)
783 const uchar *str;
784 unsigned int len;
785 unsigned long *nump;
787 unsigned long reg = 0;
788 uchar c;
789 while (len--)
791 c = *str++;
792 if (!ISDIGIT (c))
793 return 1;
794 reg *= 10;
795 reg += c - '0';
797 *nump = reg;
798 return 0;
801 /* Interpret #line command.
802 Note that the filename string (if any) is a true string constant
803 (escapes are interpreted), unlike in #line. */
804 static void
805 do_line (pfile)
806 cpp_reader *pfile;
808 const cpp_token *token;
809 const char *new_file = pfile->map->to_file;
810 unsigned long new_lineno;
812 /* C99 raised the minimum limit on #line numbers. */
813 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
815 /* #line commands expand macros. */
816 token = cpp_get_token (pfile);
817 if (token->type != CPP_NUMBER
818 || strtoul_for_line (token->val.str.text, token->val.str.len,
819 &new_lineno))
821 cpp_error (pfile, DL_ERROR,
822 "\"%s\" after #line is not a positive integer",
823 cpp_token_as_text (pfile, token));
824 return;
827 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
828 cpp_error (pfile, DL_PEDWARN, "line number out of range");
830 token = cpp_get_token (pfile);
831 if (token->type == CPP_STRING)
833 new_file = (const char *) dequote_string (pfile, token->val.str.text,
834 token->val.str.len);
835 check_eol (pfile);
837 else if (token->type != CPP_EOF)
839 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
840 cpp_token_as_text (pfile, token));
841 return;
844 skip_rest_of_line (pfile);
845 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
846 pfile->map->sysp);
849 /* Interpret the # 44 "file" [flags] notation, which has slightly
850 different syntax and semantics from #line: Flags are allowed,
851 and we never complain about the line number being too big. */
852 static void
853 do_linemarker (pfile)
854 cpp_reader *pfile;
856 const cpp_token *token;
857 const char *new_file = pfile->map->to_file;
858 unsigned long new_lineno;
859 unsigned int new_sysp = pfile->map->sysp;
860 enum lc_reason reason = LC_RENAME;
861 int flag;
863 /* Back up so we can get the number again. Putting this in
864 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
865 some circumstances, which can segfault. */
866 _cpp_backup_tokens (pfile, 1);
868 /* #line commands expand macros. */
869 token = cpp_get_token (pfile);
870 if (token->type != CPP_NUMBER
871 || strtoul_for_line (token->val.str.text, token->val.str.len,
872 &new_lineno))
874 cpp_error (pfile, DL_ERROR, "\"%s\" after # is not a positive integer",
875 cpp_token_as_text (pfile, token));
876 return;
879 token = cpp_get_token (pfile);
880 if (token->type == CPP_STRING)
882 new_file = (const char *) dequote_string (pfile, token->val.str.text,
883 token->val.str.len);
884 new_sysp = 0;
885 flag = read_flag (pfile, 0);
886 if (flag == 1)
888 reason = LC_ENTER;
889 /* Fake an include for cpp_included (). */
890 _cpp_fake_include (pfile, new_file);
891 flag = read_flag (pfile, flag);
893 else if (flag == 2)
895 reason = LC_LEAVE;
896 flag = read_flag (pfile, flag);
898 if (flag == 3)
900 new_sysp = 1;
901 flag = read_flag (pfile, flag);
902 if (flag == 4)
903 new_sysp = 2;
906 check_eol (pfile);
908 else if (token->type != CPP_EOF)
910 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
911 cpp_token_as_text (pfile, token));
912 return;
915 skip_rest_of_line (pfile);
916 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
919 /* Arrange the file_change callback. pfile->line has changed to
920 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
921 header, 2 for a system header that needs to be extern "C" protected,
922 and zero otherwise. */
923 void
924 _cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
925 cpp_reader *pfile;
926 enum lc_reason reason;
927 const char *to_file;
928 unsigned int file_line;
929 unsigned int sysp;
931 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
932 pfile->line, to_file, file_line);
934 if (pfile->cb.file_change)
935 (*pfile->cb.file_change) (pfile, pfile->map);
938 /* Report a warning or error detected by the program we are
939 processing. Use the directive's tokens in the error message. */
940 static void
941 do_diagnostic (pfile, code, print_dir)
942 cpp_reader *pfile;
943 int code;
944 int print_dir;
946 if (_cpp_begin_message (pfile, code,
947 pfile->cur_token[-1].line,
948 pfile->cur_token[-1].col))
950 if (print_dir)
951 fprintf (stderr, "#%s ", pfile->directive->name);
952 pfile->state.prevent_expansion++;
953 cpp_output_line (pfile, stderr);
954 pfile->state.prevent_expansion--;
958 static void
959 do_error (pfile)
960 cpp_reader *pfile;
962 do_diagnostic (pfile, DL_ERROR, 1);
965 static void
966 do_warning (pfile)
967 cpp_reader *pfile;
969 /* We want #warning diagnostics to be emitted in system headers too. */
970 do_diagnostic (pfile, DL_WARNING_SYSHDR, 1);
973 /* Report program identification. */
974 static void
975 do_ident (pfile)
976 cpp_reader *pfile;
978 const cpp_token *str = cpp_get_token (pfile);
980 if (str->type != CPP_STRING)
981 cpp_error (pfile, DL_ERROR, "invalid #ident directive");
982 else if (pfile->cb.ident)
983 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
985 check_eol (pfile);
988 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
989 matching entry, or NULL if none is found. The returned entry could
990 be the start of a namespace chain, or a pragma. */
991 static struct pragma_entry *
992 lookup_pragma_entry (chain, pragma)
993 struct pragma_entry *chain;
994 const cpp_hashnode *pragma;
996 while (chain && chain->pragma != pragma)
997 chain = chain->next;
999 return chain;
1002 /* Create and insert a pragma entry for NAME at the beginning of a
1003 singly-linked CHAIN. If handler is NULL, it is a namespace,
1004 otherwise it is a pragma and its handler. */
1005 static struct pragma_entry *
1006 insert_pragma_entry (pfile, chain, pragma, handler)
1007 cpp_reader *pfile;
1008 struct pragma_entry **chain;
1009 const cpp_hashnode *pragma;
1010 pragma_cb handler;
1012 struct pragma_entry *new;
1014 new = (struct pragma_entry *)
1015 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1016 new->pragma = pragma;
1017 if (handler)
1019 new->is_nspace = 0;
1020 new->u.handler = handler;
1022 else
1024 new->is_nspace = 1;
1025 new->u.space = NULL;
1028 new->next = *chain;
1029 *chain = new;
1030 return new;
1033 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1034 goes in the global namespace. HANDLER is the handler it will call,
1035 which must be non-NULL. */
1036 void
1037 cpp_register_pragma (pfile, space, name, handler)
1038 cpp_reader *pfile;
1039 const char *space;
1040 const char *name;
1041 pragma_cb handler;
1043 struct pragma_entry **chain = &pfile->pragmas;
1044 struct pragma_entry *entry;
1045 const cpp_hashnode *node;
1047 if (!handler)
1048 abort ();
1050 if (space)
1052 node = cpp_lookup (pfile, U space, strlen (space));
1053 entry = lookup_pragma_entry (*chain, node);
1054 if (!entry)
1055 entry = insert_pragma_entry (pfile, chain, node, NULL);
1056 else if (!entry->is_nspace)
1057 goto clash;
1058 chain = &entry->u.space;
1061 /* Check for duplicates. */
1062 node = cpp_lookup (pfile, U name, strlen (name));
1063 entry = lookup_pragma_entry (*chain, node);
1064 if (entry)
1066 if (entry->is_nspace)
1067 clash:
1068 cpp_error (pfile, DL_ICE,
1069 "registering \"%s\" as both a pragma and a pragma namespace",
1070 NODE_NAME (node));
1071 else if (space)
1072 cpp_error (pfile, DL_ICE, "#pragma %s %s is already registered",
1073 space, name);
1074 else
1075 cpp_error (pfile, DL_ICE, "#pragma %s is already registered", name);
1077 else
1078 insert_pragma_entry (pfile, chain, node, handler);
1081 /* Register the pragmas the preprocessor itself handles. */
1082 void
1083 _cpp_init_internal_pragmas (pfile)
1084 cpp_reader *pfile;
1086 /* Pragmas in the global namespace. */
1087 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1089 /* New GCC-specific pragmas should be put in the GCC namespace. */
1090 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1091 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1092 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
1095 /* Pragmata handling. We handle some, and pass the rest on to the
1096 front end. C99 defines three pragmas and says that no macro
1097 expansion is to be performed on them; whether or not macro
1098 expansion happens for other pragmas is implementation defined.
1099 This implementation never macro-expands the text after #pragma. */
1100 static void
1101 do_pragma (pfile)
1102 cpp_reader *pfile;
1104 const struct pragma_entry *p = NULL;
1105 const cpp_token *token;
1106 unsigned int count = 1;
1108 pfile->state.prevent_expansion++;
1110 token = cpp_get_token (pfile);
1111 if (token->type == CPP_NAME)
1113 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1114 if (p && p->is_nspace)
1116 count = 2;
1117 token = cpp_get_token (pfile);
1118 if (token->type == CPP_NAME)
1119 p = lookup_pragma_entry (p->u.space, token->val.node);
1120 else
1121 p = NULL;
1125 /* FIXME. This is an awful kludge to get the front ends to update
1126 their notion of line number for diagnostic purposes. The line
1127 number should be passed to the handler and they should do it
1128 themselves. Stand-alone CPP must ignore us, otherwise it will
1129 prefix the directive with spaces, hence the 1. Ugh. */
1130 if (pfile->cb.line_change)
1131 (*pfile->cb.line_change)(pfile, token, 1);
1133 if (p)
1134 (*p->u.handler) (pfile);
1135 else if (pfile->cb.def_pragma)
1137 _cpp_backup_tokens (pfile, count);
1138 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1141 pfile->state.prevent_expansion--;
1144 /* Handle #pragma once. */
1145 static void
1146 do_pragma_once (pfile)
1147 cpp_reader *pfile;
1149 cpp_error (pfile, DL_WARNING, "#pragma once is obsolete");
1151 if (pfile->buffer->prev == NULL)
1152 cpp_error (pfile, DL_WARNING, "#pragma once in main file");
1153 else
1154 _cpp_never_reread (pfile->buffer->inc);
1156 check_eol (pfile);
1159 /* Handle #pragma GCC poison, to poison one or more identifiers so
1160 that the lexer produces a hard error for each subsequent usage. */
1161 static void
1162 do_pragma_poison (pfile)
1163 cpp_reader *pfile;
1165 const cpp_token *tok;
1166 cpp_hashnode *hp;
1168 pfile->state.poisoned_ok = 1;
1169 for (;;)
1171 tok = _cpp_lex_token (pfile);
1172 if (tok->type == CPP_EOF)
1173 break;
1174 if (tok->type != CPP_NAME)
1176 cpp_error (pfile, DL_ERROR, "invalid #pragma GCC poison directive");
1177 break;
1180 hp = tok->val.node;
1181 if (hp->flags & NODE_POISONED)
1182 continue;
1184 if (hp->type == NT_MACRO)
1185 cpp_error (pfile, DL_WARNING, "poisoning existing macro \"%s\"",
1186 NODE_NAME (hp));
1187 _cpp_free_definition (hp);
1188 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1190 pfile->state.poisoned_ok = 0;
1193 /* Mark the current header as a system header. This will suppress
1194 some categories of warnings (notably those from -pedantic). It is
1195 intended for use in system libraries that cannot be implemented in
1196 conforming C, but cannot be certain that their headers appear in a
1197 system include directory. To prevent abuse, it is rejected in the
1198 primary source file. */
1199 static void
1200 do_pragma_system_header (pfile)
1201 cpp_reader *pfile;
1203 cpp_buffer *buffer = pfile->buffer;
1205 if (buffer->prev == 0)
1206 cpp_error (pfile, DL_WARNING,
1207 "#pragma system_header ignored outside include file");
1208 else
1210 check_eol (pfile);
1211 skip_rest_of_line (pfile);
1212 cpp_make_system_header (pfile, 1, 0);
1216 /* Check the modified date of the current include file against a specified
1217 file. Issue a diagnostic, if the specified file is newer. We use this to
1218 determine if a fixed header should be refixed. */
1219 static void
1220 do_pragma_dependency (pfile)
1221 cpp_reader *pfile;
1223 const cpp_token *header;
1224 int ordering;
1226 header = parse_include (pfile);
1227 if (!header)
1228 return;
1230 ordering = _cpp_compare_file_date (pfile, header);
1231 if (ordering < 0)
1232 cpp_error (pfile, DL_WARNING, "cannot find source %s",
1233 cpp_token_as_text (pfile, header));
1234 else if (ordering > 0)
1236 cpp_error (pfile, DL_WARNING, "current file is older than %s",
1237 cpp_token_as_text (pfile, header));
1238 if (cpp_get_token (pfile)->type != CPP_EOF)
1240 _cpp_backup_tokens (pfile, 1);
1241 do_diagnostic (pfile, DL_WARNING, 0);
1246 /* Get a token but skip padding. */
1247 static const cpp_token *
1248 get_token_no_padding (pfile)
1249 cpp_reader *pfile;
1251 for (;;)
1253 const cpp_token *result = cpp_get_token (pfile);
1254 if (result->type != CPP_PADDING)
1255 return result;
1259 /* Check syntax is "(string-literal)". Returns the string on success,
1260 or NULL on failure. */
1261 static const cpp_token *
1262 get__Pragma_string (pfile)
1263 cpp_reader *pfile;
1265 const cpp_token *string;
1267 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1268 return NULL;
1270 string = get_token_no_padding (pfile);
1271 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1272 return NULL;
1274 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1275 return NULL;
1277 return string;
1280 /* Destringize IN into a temporary buffer, by removing the first \ of
1281 \" and \\ sequences, and process the result as a #pragma directive. */
1282 static void
1283 destringize_and_run (pfile, in)
1284 cpp_reader *pfile;
1285 const cpp_string *in;
1287 const unsigned char *src, *limit;
1288 char *dest, *result;
1290 dest = result = alloca (in->len + 1);
1291 for (src = in->text, limit = src + in->len; src < limit;)
1293 /* We know there is a character following the backslash. */
1294 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1295 src++;
1296 *dest++ = *src++;
1298 *dest = '\0';
1300 run_directive (pfile, T_PRAGMA, result, dest - result);
1303 /* Handle the _Pragma operator. */
1304 void
1305 _cpp_do__Pragma (pfile)
1306 cpp_reader *pfile;
1308 const cpp_token *string = get__Pragma_string (pfile);
1310 if (!string)
1311 cpp_error (pfile, DL_ERROR,
1312 "_Pragma takes a parenthesized string literal");
1313 else
1315 /* Ideally, we'd like
1316 token1 _Pragma ("foo") token2
1317 to be output as
1318 token1
1319 # 7 "file.c"
1320 #pragma foo
1321 # 7 "file.c"
1322 token2
1323 Getting these correct line markers is a little tricky. */
1325 unsigned int orig_line = pfile->line;
1326 destringize_and_run (pfile, &string->val.str);
1327 pfile->line = orig_line;
1328 pfile->buffer->saved_flags = BOL;
1332 /* Just ignore #sccs, on systems where we define it at all. */
1333 #ifdef SCCS_DIRECTIVE
1334 static void
1335 do_sccs (pfile)
1336 cpp_reader *pfile ATTRIBUTE_UNUSED;
1339 #endif
1341 /* Handle #ifdef. */
1342 static void
1343 do_ifdef (pfile)
1344 cpp_reader *pfile;
1346 int skip = 1;
1348 if (! pfile->state.skipping)
1350 const cpp_hashnode *node = lex_macro_node (pfile);
1352 if (node)
1353 skip = node->type != NT_MACRO;
1355 if (node)
1356 check_eol (pfile);
1359 push_conditional (pfile, skip, T_IFDEF, 0);
1362 /* Handle #ifndef. */
1363 static void
1364 do_ifndef (pfile)
1365 cpp_reader *pfile;
1367 int skip = 1;
1368 const cpp_hashnode *node = 0;
1370 if (! pfile->state.skipping)
1372 node = lex_macro_node (pfile);
1373 if (node)
1374 skip = node->type == NT_MACRO;
1376 if (node)
1377 check_eol (pfile);
1380 push_conditional (pfile, skip, T_IFNDEF, node);
1383 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1384 pfile->mi_ind_cmacro so we can handle multiple-include
1385 optimisations. If macro expansion occurs in the expression, we
1386 cannot treat it as a controlling conditional, since the expansion
1387 could change in the future. That is handled by cpp_get_token. */
1388 static void
1389 do_if (pfile)
1390 cpp_reader *pfile;
1392 int skip = 1;
1394 if (! pfile->state.skipping)
1395 skip = _cpp_parse_expr (pfile) == false;
1397 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1400 /* Flip skipping state if appropriate and continue without changing
1401 if_stack; this is so that the error message for missing #endif's
1402 etc. will point to the original #if. */
1403 static void
1404 do_else (pfile)
1405 cpp_reader *pfile;
1407 cpp_buffer *buffer = pfile->buffer;
1408 struct if_stack *ifs = buffer->if_stack;
1410 if (ifs == NULL)
1411 cpp_error (pfile, DL_ERROR, "#else without #if");
1412 else
1414 if (ifs->type == T_ELSE)
1416 cpp_error (pfile, DL_ERROR, "#else after #else");
1417 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1418 "the conditional began here");
1420 ifs->type = T_ELSE;
1422 /* Skip any future (erroneous) #elses or #elifs. */
1423 pfile->state.skipping = ifs->skip_elses;
1424 ifs->skip_elses = true;
1426 /* Invalidate any controlling macro. */
1427 ifs->mi_cmacro = 0;
1429 /* Only check EOL if was not originally skipping. */
1430 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1431 check_eol (pfile);
1435 /* Handle a #elif directive by not changing if_stack either. See the
1436 comment above do_else. */
1437 static void
1438 do_elif (pfile)
1439 cpp_reader *pfile;
1441 cpp_buffer *buffer = pfile->buffer;
1442 struct if_stack *ifs = buffer->if_stack;
1444 if (ifs == NULL)
1445 cpp_error (pfile, DL_ERROR, "#elif without #if");
1446 else
1448 if (ifs->type == T_ELSE)
1450 cpp_error (pfile, DL_ERROR, "#elif after #else");
1451 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1452 "the conditional began here");
1454 ifs->type = T_ELIF;
1456 /* Only evaluate this if we aren't skipping elses. During
1457 evaluation, set skipping to false to get lexer warnings. */
1458 if (ifs->skip_elses)
1459 pfile->state.skipping = 1;
1460 else
1462 pfile->state.skipping = 0;
1463 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1464 ifs->skip_elses = ! pfile->state.skipping;
1467 /* Invalidate any controlling macro. */
1468 ifs->mi_cmacro = 0;
1472 /* #endif pops the if stack and resets pfile->state.skipping. */
1473 static void
1474 do_endif (pfile)
1475 cpp_reader *pfile;
1477 cpp_buffer *buffer = pfile->buffer;
1478 struct if_stack *ifs = buffer->if_stack;
1480 if (ifs == NULL)
1481 cpp_error (pfile, DL_ERROR, "#endif without #if");
1482 else
1484 /* Only check EOL if was not originally skipping. */
1485 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1486 check_eol (pfile);
1488 /* If potential control macro, we go back outside again. */
1489 if (ifs->next == 0 && ifs->mi_cmacro)
1491 pfile->mi_valid = true;
1492 pfile->mi_cmacro = ifs->mi_cmacro;
1495 buffer->if_stack = ifs->next;
1496 pfile->state.skipping = ifs->was_skipping;
1497 obstack_free (&pfile->buffer_ob, ifs);
1501 /* Push an if_stack entry for a preprocessor conditional, and set
1502 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1503 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1504 we need to check here that we are at the top of the file. */
1505 static void
1506 push_conditional (pfile, skip, type, cmacro)
1507 cpp_reader *pfile;
1508 int skip;
1509 int type;
1510 const cpp_hashnode *cmacro;
1512 struct if_stack *ifs;
1513 cpp_buffer *buffer = pfile->buffer;
1515 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1516 ifs->line = pfile->directive_line;
1517 ifs->next = buffer->if_stack;
1518 ifs->skip_elses = pfile->state.skipping || !skip;
1519 ifs->was_skipping = pfile->state.skipping;
1520 ifs->type = type;
1521 /* This condition is effectively a test for top-of-file. */
1522 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1523 ifs->mi_cmacro = cmacro;
1524 else
1525 ifs->mi_cmacro = 0;
1527 pfile->state.skipping = skip;
1528 buffer->if_stack = ifs;
1531 /* Read the tokens of the answer into the macro pool, in a directive
1532 of type TYPE. Only commit the memory if we intend it as permanent
1533 storage, i.e. the #assert case. Returns 0 on success, and sets
1534 ANSWERP to point to the answer. */
1535 static int
1536 parse_answer (pfile, answerp, type)
1537 cpp_reader *pfile;
1538 struct answer **answerp;
1539 int type;
1541 const cpp_token *paren;
1542 struct answer *answer;
1543 unsigned int acount;
1545 /* In a conditional, it is legal to not have an open paren. We
1546 should save the following token in this case. */
1547 paren = cpp_get_token (pfile);
1549 /* If not a paren, see if we're OK. */
1550 if (paren->type != CPP_OPEN_PAREN)
1552 /* In a conditional no answer is a test for any answer. It
1553 could be followed by any token. */
1554 if (type == T_IF)
1556 _cpp_backup_tokens (pfile, 1);
1557 return 0;
1560 /* #unassert with no answer is valid - it removes all answers. */
1561 if (type == T_UNASSERT && paren->type == CPP_EOF)
1562 return 0;
1564 cpp_error (pfile, DL_ERROR, "missing '(' after predicate");
1565 return 1;
1568 for (acount = 0;; acount++)
1570 size_t room_needed;
1571 const cpp_token *token = cpp_get_token (pfile);
1572 cpp_token *dest;
1574 if (token->type == CPP_CLOSE_PAREN)
1575 break;
1577 if (token->type == CPP_EOF)
1579 cpp_error (pfile, DL_ERROR, "missing ')' to complete answer");
1580 return 1;
1583 /* struct answer includes the space for one token. */
1584 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1586 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1587 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1589 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1590 *dest = *token;
1592 /* Drop whitespace at start, for answer equivalence purposes. */
1593 if (acount == 0)
1594 dest->flags &= ~PREV_WHITE;
1597 if (acount == 0)
1599 cpp_error (pfile, DL_ERROR, "predicate's answer is empty");
1600 return 1;
1603 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1604 answer->count = acount;
1605 answer->next = NULL;
1606 *answerp = answer;
1608 return 0;
1611 /* Parses an assertion directive of type TYPE, returning a pointer to
1612 the hash node of the predicate, or 0 on error. If an answer was
1613 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1614 static cpp_hashnode *
1615 parse_assertion (pfile, answerp, type)
1616 cpp_reader *pfile;
1617 struct answer **answerp;
1618 int type;
1620 cpp_hashnode *result = 0;
1621 const cpp_token *predicate;
1623 /* We don't expand predicates or answers. */
1624 pfile->state.prevent_expansion++;
1626 *answerp = 0;
1627 predicate = cpp_get_token (pfile);
1628 if (predicate->type == CPP_EOF)
1629 cpp_error (pfile, DL_ERROR, "assertion without predicate");
1630 else if (predicate->type != CPP_NAME)
1631 cpp_error (pfile, DL_ERROR, "predicate must be an identifier");
1632 else if (parse_answer (pfile, answerp, type) == 0)
1634 unsigned int len = NODE_LEN (predicate->val.node);
1635 unsigned char *sym = alloca (len + 1);
1637 /* Prefix '#' to get it out of macro namespace. */
1638 sym[0] = '#';
1639 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1640 result = cpp_lookup (pfile, sym, len + 1);
1643 pfile->state.prevent_expansion--;
1644 return result;
1647 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1648 or a pointer to NULL if the answer is not in the chain. */
1649 static struct answer **
1650 find_answer (node, candidate)
1651 cpp_hashnode *node;
1652 const struct answer *candidate;
1654 unsigned int i;
1655 struct answer **result;
1657 for (result = &node->value.answers; *result; result = &(*result)->next)
1659 struct answer *answer = *result;
1661 if (answer->count == candidate->count)
1663 for (i = 0; i < answer->count; i++)
1664 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1665 break;
1667 if (i == answer->count)
1668 break;
1672 return result;
1675 /* Test an assertion within a preprocessor conditional. Returns
1676 non-zero on failure, zero on success. On success, the result of
1677 the test is written into VALUE. */
1679 _cpp_test_assertion (pfile, value)
1680 cpp_reader *pfile;
1681 unsigned int *value;
1683 struct answer *answer;
1684 cpp_hashnode *node;
1686 node = parse_assertion (pfile, &answer, T_IF);
1687 if (node)
1688 *value = (node->type == NT_ASSERTION &&
1689 (answer == 0 || *find_answer (node, answer) != 0));
1690 else if (pfile->cur_token[-1].type == CPP_EOF)
1691 _cpp_backup_tokens (pfile, 1);
1693 /* We don't commit the memory for the answer - it's temporary only. */
1694 return node == 0;
1697 /* Handle #assert. */
1698 static void
1699 do_assert (pfile)
1700 cpp_reader *pfile;
1702 struct answer *new_answer;
1703 cpp_hashnode *node;
1705 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1706 if (node)
1708 /* Place the new answer in the answer list. First check there
1709 is not a duplicate. */
1710 new_answer->next = 0;
1711 if (node->type == NT_ASSERTION)
1713 if (*find_answer (node, new_answer))
1715 cpp_error (pfile, DL_WARNING, "\"%s\" re-asserted",
1716 NODE_NAME (node) + 1);
1717 return;
1719 new_answer->next = node->value.answers;
1722 node->type = NT_ASSERTION;
1723 node->value.answers = new_answer;
1724 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1725 + (new_answer->count - 1)
1726 * sizeof (cpp_token));
1727 check_eol (pfile);
1731 /* Handle #unassert. */
1732 static void
1733 do_unassert (pfile)
1734 cpp_reader *pfile;
1736 cpp_hashnode *node;
1737 struct answer *answer;
1739 node = parse_assertion (pfile, &answer, T_UNASSERT);
1740 /* It isn't an error to #unassert something that isn't asserted. */
1741 if (node && node->type == NT_ASSERTION)
1743 if (answer)
1745 struct answer **p = find_answer (node, answer), *temp;
1747 /* Remove the answer from the list. */
1748 temp = *p;
1749 if (temp)
1750 *p = temp->next;
1752 /* Did we free the last answer? */
1753 if (node->value.answers == 0)
1754 node->type = NT_VOID;
1756 check_eol (pfile);
1758 else
1759 _cpp_free_definition (node);
1762 /* We don't commit the memory for the answer - it's temporary only. */
1765 /* These are for -D, -U, -A. */
1767 /* Process the string STR as if it appeared as the body of a #define.
1768 If STR is just an identifier, define it with value 1.
1769 If STR has anything after the identifier, then it should
1770 be identifier=definition. */
1771 void
1772 cpp_define (pfile, str)
1773 cpp_reader *pfile;
1774 const char *str;
1776 char *buf, *p;
1777 size_t count;
1779 /* Copy the entire option so we can modify it.
1780 Change the first "=" in the string to a space. If there is none,
1781 tack " 1" on the end. */
1783 count = strlen (str);
1784 buf = (char *) alloca (count + 3);
1785 memcpy (buf, str, count);
1787 p = strchr (str, '=');
1788 if (p)
1789 buf[p - str] = ' ';
1790 else
1792 buf[count++] = ' ';
1793 buf[count++] = '1';
1795 buf[count] = '\0';
1797 run_directive (pfile, T_DEFINE, buf, count);
1800 /* Slight variant of the above for use by initialize_builtins. */
1801 void
1802 _cpp_define_builtin (pfile, str)
1803 cpp_reader *pfile;
1804 const char *str;
1806 run_directive (pfile, T_DEFINE, str, strlen (str));
1809 /* Process MACRO as if it appeared as the body of an #undef. */
1810 void
1811 cpp_undef (pfile, macro)
1812 cpp_reader *pfile;
1813 const char *macro;
1815 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1818 /* Process the string STR as if it appeared as the body of a #assert. */
1819 void
1820 cpp_assert (pfile, str)
1821 cpp_reader *pfile;
1822 const char *str;
1824 handle_assertion (pfile, str, T_ASSERT);
1827 /* Process STR as if it appeared as the body of an #unassert. */
1828 void
1829 cpp_unassert (pfile, str)
1830 cpp_reader *pfile;
1831 const char *str;
1833 handle_assertion (pfile, str, T_UNASSERT);
1836 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1837 static void
1838 handle_assertion (pfile, str, type)
1839 cpp_reader *pfile;
1840 const char *str;
1841 int type;
1843 size_t count = strlen (str);
1844 const char *p = strchr (str, '=');
1846 if (p)
1848 /* Copy the entire option so we can modify it. Change the first
1849 "=" in the string to a '(', and tack a ')' on the end. */
1850 char *buf = (char *) alloca (count + 2);
1852 memcpy (buf, str, count);
1853 buf[p - str] = '(';
1854 buf[count++] = ')';
1855 buf[count] = '\0';
1856 str = buf;
1859 run_directive (pfile, type, str, count);
1862 /* The number of errors for a given reader. */
1863 unsigned int
1864 cpp_errors (pfile)
1865 cpp_reader *pfile;
1867 return pfile->errors;
1870 /* The options structure. */
1871 cpp_options *
1872 cpp_get_options (pfile)
1873 cpp_reader *pfile;
1875 return &pfile->opts;
1878 /* The callbacks structure. */
1879 cpp_callbacks *
1880 cpp_get_callbacks (pfile)
1881 cpp_reader *pfile;
1883 return &pfile->cb;
1886 /* The line map set. */
1887 const struct line_maps *
1888 cpp_get_line_maps (pfile)
1889 cpp_reader *pfile;
1891 return &pfile->line_maps;
1894 /* Copy the given callbacks structure to our own. */
1895 void
1896 cpp_set_callbacks (pfile, cb)
1897 cpp_reader *pfile;
1898 cpp_callbacks *cb;
1900 pfile->cb = *cb;
1903 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1904 doesn't fail. It does not generate a file change call back; that
1905 is the responsibility of the caller. */
1906 cpp_buffer *
1907 cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
1908 cpp_reader *pfile;
1909 const uchar *buffer;
1910 size_t len;
1911 int from_stage3;
1912 int return_at_eof;
1914 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1916 /* Clears, amongst other things, if_stack and mi_cmacro. */
1917 memset (new, 0, sizeof (cpp_buffer));
1919 new->line_base = new->buf = new->cur = buffer;
1920 new->rlimit = buffer + len;
1921 new->from_stage3 = from_stage3 || CPP_OPTION (pfile, traditional);
1922 new->prev = pfile->buffer;
1923 new->return_at_eof = return_at_eof;
1924 new->saved_flags = BOL;
1926 pfile->buffer = new;
1928 if (CPP_OPTION (pfile, traditional))
1929 _cpp_set_trad_context (pfile);
1931 return new;
1934 /* Pops a single buffer, with a file change call-back if appropriate.
1935 Then pushes the next -include file, if any remain. */
1936 void
1937 _cpp_pop_buffer (pfile)
1938 cpp_reader *pfile;
1940 cpp_buffer *buffer = pfile->buffer;
1941 struct include_file *inc = buffer->inc;
1942 struct if_stack *ifs;
1944 /* Walk back up the conditional stack till we reach its level at
1945 entry to this file, issuing error messages. */
1946 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1947 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1948 "unterminated #%s", dtable[ifs->type].name);
1950 /* In case of a missing #endif. */
1951 pfile->state.skipping = 0;
1953 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
1954 pfile->buffer = buffer->prev;
1956 /* Free the buffer object now; we may want to push a new buffer
1957 in _cpp_push_next_include_file. */
1958 obstack_free (&pfile->buffer_ob, buffer);
1960 if (inc)
1962 _cpp_pop_file_buffer (pfile, inc);
1964 /* Don't generate a callback for popping the main file. */
1965 if (pfile->buffer)
1967 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
1969 /* If this is the main file, there may be some -include
1970 files left to push. */
1971 if (!pfile->buffer->prev)
1972 _cpp_maybe_push_include_file (pfile);
1976 if (pfile->buffer && CPP_OPTION (pfile, traditional))
1977 _cpp_set_trad_context (pfile);
1980 /* Enter all recognised directives in the hash table. */
1981 void
1982 _cpp_init_directives (pfile)
1983 cpp_reader *pfile;
1985 unsigned int i;
1986 cpp_hashnode *node;
1988 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1990 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1991 node->directive_index = i + 1;