1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 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
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. */
31 /* Chained list of answers to an assertion. */
39 /* Stack of conditionals currently in progress
40 (including both successful and failing conditionals). */
44 struct if_stack *next;
45 cpp_lexer_pos pos; /* line and column where condition started */
46 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
47 unsigned char was_skipping; /* Value of pfile->skipping before this if. */
48 int type; /* type of last directive seen in this group */
51 /* Values for the origin field of struct directive. KANDR directives
52 come from traditional (K&R) C. STDC89 directives come from the
53 1989 C standard. EXTENSION directives are extensions. */
58 /* Values for the flags field of struct directive. COND indicates a
59 conditional; IF_COND an opening conditional. INCL means to treat
60 "..." and <...> as q-char and h-char sequences respectively. IN_I
61 means this directive should be handled even if -fpreprocessed is in
62 effect (these are the directives with callback hooks). */
64 #define IF_COND (1 << 1)
68 /* Defines one #-directive, including how to handle it. */
69 typedef void (*directive_handler) PARAMS ((cpp_reader *));
70 typedef struct directive directive;
73 directive_handler handler; /* Function to handle directive. */
74 const U_CHAR *name; /* Name of directive. */
75 unsigned short length; /* Length of name. */
76 unsigned char origin; /* Origin of directive. */
77 unsigned char flags; /* Flags describing this directive. */
80 /* Forward declarations. */
82 static void skip_rest_of_line PARAMS ((cpp_reader *));
83 static void check_eol PARAMS ((cpp_reader *));
84 static void run_directive PARAMS ((cpp_reader *, int,
87 static int glue_header_name PARAMS ((cpp_reader *, cpp_token *));
88 static int parse_include PARAMS ((cpp_reader *, cpp_token *));
89 static void push_conditional PARAMS ((cpp_reader *, int, int,
90 const cpp_hashnode *));
91 static int read_line_number PARAMS ((cpp_reader *, int *));
92 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
94 static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
95 static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
96 static void do_pragma_once PARAMS ((cpp_reader *));
97 static void do_pragma_poison PARAMS ((cpp_reader *));
98 static void do_pragma_system_header PARAMS ((cpp_reader *));
99 static void do_pragma_dependency PARAMS ((cpp_reader *));
100 static int get__Pragma_string PARAMS ((cpp_reader *, cpp_token *));
101 static unsigned char *destringize PARAMS ((const cpp_string *,
103 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
104 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
106 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
107 const struct answer *));
108 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
110 /* This is the table of directive handlers. It is ordered by
111 frequency of occurrence; the numbers at the end are directive
112 counts from all the source code I have lying around (egcs and libc
113 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
114 pcmcia-cs-3.0.9). This is no longer important as directive lookup
115 is now O(1). All extensions other than #warning and #include_next
116 are deprecated. The name is where the extension appears to have
119 #define DIRECTIVE_TABLE \
120 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
121 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
122 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
123 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
124 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
125 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
126 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
127 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
128 D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
129 D(elif, T_ELIF, KANDR, COND) /* 610 */ \
130 D(error, T_ERROR, STDC89, 0) /* 475 */ \
131 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
132 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
133 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
134 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
135 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
136 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
137 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
138 SCCS_ENTRY /* 0 SVR4? */
140 /* #sccs is not always recognized. */
141 #ifdef SCCS_DIRECTIVE
142 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
144 # define SCCS_ENTRY /* nothing */
147 /* Use the table to generate a series of prototypes, an enum for the
148 directive names, and an array of directive handlers. */
150 /* The directive-processing functions are declared to return int
151 instead of void, because some old compilers have trouble with
152 pointers to functions returning void. */
154 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
155 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
159 #define D(n, tag, o, f) tag,
168 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
169 #define D(name, t, origin, flags) \
170 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
171 sizeof STRINGX(name) - 1, origin, flags },
172 static const directive dtable[] =
177 #undef DIRECTIVE_TABLE
179 /* Skip any remaining tokens in a directive. */
181 skip_rest_of_line (pfile)
186 /* Discard all input lookaheads. */
187 while (pfile->la_read)
188 _cpp_release_lookahead (pfile);
190 /* Discard all stacked contexts. */
191 while (pfile->context != &pfile->base_context)
192 _cpp_pop_context (pfile);
194 /* Sweep up all tokens remaining on the line. */
195 pfile->state.prevent_expansion++;
196 while (!pfile->state.next_bol)
197 _cpp_lex_token (pfile, &token);
198 pfile->state.prevent_expansion--;
201 /* Ensure there are no stray tokens at the end of a directive. */
206 if (!pfile->state.next_bol)
210 _cpp_lex_token (pfile, &token);
211 if (token.type != CPP_EOF)
212 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
213 pfile->directive->name);
217 /* Check if a token's name matches that of a known directive. Put in
218 this file to save exporting dtable and other unneeded information. */
220 _cpp_handle_directive (pfile, indented)
224 struct cpp_lookahead *la_saved;
225 cpp_buffer *buffer = pfile->buffer;
226 const directive *dir = 0;
230 /* Setup in-directive state. */
231 pfile->state.in_directive = 1;
232 pfile->state.save_comments = 0;
234 /* Some handlers need the position of the # for diagnostics. */
235 pfile->directive_pos = pfile->lexer_pos;
237 /* Don't save directive tokens for external clients. */
238 la_saved = pfile->la_write;
241 /* Lex the directive name directly. */
242 _cpp_lex_token (pfile, &dname);
244 if (dname.type == CPP_NAME)
246 unsigned int index = dname.val.node->directive_index;
248 dir = &dtable[index - 1];
250 else if (dname.type == CPP_NUMBER)
252 /* # followed by a number is equivalent to #line. Do not
253 recognize this form in assembly language source files or
254 skipped conditional groups. Complain about this form if
255 we're being pedantic, but not if this is regurgitated input
256 (preprocessed or fed back in by the C++ frontend). */
257 if (! pfile->skipping && !CPP_OPTION (pfile, lang_asm))
259 dir = &dtable[T_LINE];
260 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
261 if (CPP_PEDANTIC (pfile) && buffer->inc
262 && ! CPP_OPTION (pfile, preprocessed))
263 cpp_pedwarn (pfile, "# followed by integer");
267 pfile->directive = dir;
270 /* Make sure we lex headers correctly, whether skipping or not. */
271 pfile->state.angled_headers = dir->flags & INCL;
273 /* If we are rescanning preprocessed input, only directives tagged
274 with IN_I are honored, and the warnings below are suppressed. */
275 if (! CPP_OPTION (pfile, preprocessed) || dir->flags & IN_I)
277 /* Traditionally, a directive is ignored unless its # is in
278 column 1. Therefore in code intended to work with K+R
279 compilers, directives added by C89 must have their #
280 indented, and directives present in traditional C must
281 not. This is true even of directives in skipped
282 conditional blocks. */
283 if (CPP_WTRADITIONAL (pfile))
285 if (indented && dir->origin == KANDR)
287 "traditional C ignores #%s with the # indented",
289 else if (!indented && dir->origin != KANDR)
291 "suggest hiding #%s from traditional C with an indented #",
295 /* If we are skipping a failed conditional group, all
296 non-conditional directives are ignored. */
297 if (! pfile->skipping || (dir->flags & COND))
299 /* Issue -pedantic warnings for extensions. */
300 if (CPP_PEDANTIC (pfile) && dir->origin == EXTENSION)
301 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
303 /* If we have a directive that is not an opening
304 conditional, invalidate any control macro. */
305 if (! (dir->flags & IF_COND))
306 pfile->mi_state = MI_FAILED;
308 buffer->was_skipping = pfile->skipping;
310 (*dir->handler) (pfile);
311 pfile->skipping = buffer->was_skipping;
315 else if (dname.type == CPP_EOF)
317 /* The null directive. */
318 if (indented && CPP_WTRADITIONAL (pfile))
319 cpp_warning (pfile, "traditional C ignores #\\n with the # indented");
321 else if (!pfile->skipping)
323 /* An unknown directive. Don't complain about it in assembly
324 source: we don't know where the comments are, and # may
325 introduce assembler pseudo-ops. Don't complain about invalid
326 directives in skipped conditional groups (6.10 p4). */
327 if (CPP_OPTION (pfile, lang_asm))
329 /* Output the # and lookahead token for the assembler. */
331 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
334 cpp_error (pfile, "invalid preprocessing directive #%s",
335 cpp_token_as_text (pfile, &dname));
338 /* Save the lookahead token for assembler. */
340 skip_rest_of_line (pfile);
343 pfile->la_write = la_saved;
344 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
345 pfile->state.in_directive = 0;
346 pfile->state.angled_headers = 0;
347 pfile->directive = 0;
352 /* Directive handler wrapper used by the command line option
355 run_directive (pfile, dir_no, buf, count, name)
362 unsigned int output_line = pfile->lexer_pos.output_line;
364 if (cpp_push_buffer (pfile, (const U_CHAR *) buf, count) != NULL)
366 const struct directive *dir = &dtable[dir_no], *orig_dir;
367 unsigned char orig_in_directive;
370 CPP_BUFFER (pfile)->nominal_fname = name;
372 CPP_BUFFER (pfile)->nominal_fname = _("<command line>");
374 /* A kludge to avoid line markers for _Pragma. */
375 if (dir_no == T_PRAGMA)
376 pfile->lexer_pos.output_line = output_line;
378 /* Save any in-process directive; _Pragma can appear in one. */
379 orig_dir = pfile->directive;
380 orig_in_directive = pfile->state.in_directive;
382 /* For _Pragma, the text is passed through preprocessing stage 3
383 only, i.e. no trigraphs, no escaped newline removal, and no
384 macro expansion. Do the same for command-line directives. */
385 pfile->buffer->from_stage3 = 1;
386 pfile->state.in_directive = 1;
387 pfile->directive = dir;
388 pfile->state.prevent_expansion++;
389 (void) (*dir->handler) (pfile);
390 pfile->state.prevent_expansion--;
391 pfile->directive = orig_dir;
392 pfile->state.in_directive = orig_in_directive;
394 skip_rest_of_line (pfile);
395 if (pfile->buffer->cur != pfile->buffer->rlimit)
396 cpp_error (pfile, "extra text after end of #%s directive",
397 dtable[dir_no].name);
398 cpp_pop_buffer (pfile);
402 /* Checks for validity the macro name in #define, #undef, #ifdef and
403 #ifndef directives. */
404 static cpp_hashnode *
405 lex_macro_node (pfile)
410 /* Lex the macro name directly. */
411 _cpp_lex_token (pfile, &token);
413 /* The token immediately after #define must be an identifier. That
414 identifier is not allowed to be "defined". See predefined macro
415 names (6.10.8.4). In C++, it is not allowed to be any of the
416 <iso646.h> macro names (which are keywords in C++) either. */
418 if (token.type != CPP_NAME)
420 if (token.type == CPP_EOF)
421 cpp_error (pfile, "no macro name given in #%s directive",
422 pfile->directive->name);
423 else if (token.flags & NAMED_OP)
425 "\"%s\" cannot be used as a macro name as it is an operator in C++",
426 token.val.node->name);
428 cpp_error (pfile, "macro names must be identifiers");
432 cpp_hashnode *node = token.val.node;
434 /* In Objective C, some keywords begin with '@', but general
435 identifiers do not, and you're not allowed to #define them. */
436 if (node == pfile->spec_nodes.n_defined || node->name[0] == '@')
437 cpp_error (pfile, "\"%s\" cannot be used as a macro name", node->name);
438 else if (!(node->flags & NODE_POISONED))
445 /* Process a #define directive. Most work is done in cppmacro.c. */
450 cpp_hashnode *node = lex_macro_node (pfile);
454 /* Use the permanent pool for storage. */
455 pfile->string_pool = &pfile->ident_pool;
457 if (_cpp_create_definition (pfile, node))
458 if (pfile->cb.define)
459 (*pfile->cb.define) (pfile, node);
461 /* Revert to the temporary pool. */
462 pfile->string_pool = &pfile->temp_string_pool;
466 /* Handle #undef. Marks the identifier NT_VOID in the hash table. */
471 cpp_hashnode *node = lex_macro_node (pfile);
473 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
474 is not currently defined as a macro name. */
475 if (node && node->type == NT_MACRO)
478 (*pfile->cb.undef) (pfile, node);
480 if (node->flags & NODE_BUILTIN)
481 cpp_warning (pfile, "undefining \"%s\"", node->name);
483 _cpp_free_definition (node);
488 /* Helper routine used by parse_include. Reinterpret the current line
489 as an h-char-sequence (< ... >); we are looking at the first token
490 after the <. Returns zero on success. */
492 glue_header_name (pfile, header)
497 unsigned char *buffer, *token_mem;
498 size_t len, total_len = 0, capacity = 1024;
500 /* To avoid lexed tokens overwriting our glued name, we can only
501 allocate from the string pool once we've lexed everything. */
503 buffer = (unsigned char *) xmalloc (capacity);
506 cpp_get_token (pfile, &token);
508 if (token.type == CPP_GREATER || token.type == CPP_EOF)
511 len = cpp_token_len (&token);
512 if (total_len + len > capacity)
514 capacity = (capacity + len) * 2;
515 buffer = (unsigned char *) realloc (buffer, capacity);
518 if (token.flags & PREV_WHITE)
519 buffer[total_len++] = ' ';
521 total_len = cpp_spell_token (pfile, &token, &buffer[total_len]) - buffer;
524 if (token.type == CPP_EOF)
525 cpp_error (pfile, "missing terminating > character");
528 token_mem = _cpp_pool_alloc (pfile->string_pool, total_len);
529 memcpy (token_mem, buffer, total_len);
531 header->type = CPP_HEADER_NAME;
532 header->flags &= ~PREV_WHITE;
533 header->val.str.len = total_len;
534 header->val.str.text = token_mem;
538 return token.type == CPP_EOF;
541 /* Parse the header name of #include, #include_next, #import and
542 #pragma dependency. Returns zero on success. */
544 parse_include (pfile, header)
548 int is_pragma = pfile->directive == &dtable[T_PRAGMA];
549 const unsigned char *dir;
552 dir = U"pragma dependency";
554 dir = pfile->directive->name;
556 /* Allow macro expansion. */
557 cpp_get_token (pfile, header);
558 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
560 if (header->type != CPP_LESS)
562 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
565 if (glue_header_name (pfile, header))
569 if (header->val.str.len == 0)
571 cpp_error (pfile, "empty file name in #%s", dir);
578 /* Get out of macro context, if we are. */
579 skip_rest_of_line (pfile);
580 if (pfile->cb.include)
581 (*pfile->cb.include) (pfile, dir, header);
593 if (!parse_include (pfile, &header))
594 _cpp_execute_include (pfile, &header, 0, 0);
603 if (!pfile->import_warning && CPP_OPTION (pfile, warn_import))
605 pfile->import_warning = 1;
607 "#import is obsolete, use an #ifndef wrapper in the header file");
610 if (!parse_include (pfile, &header))
611 _cpp_execute_include (pfile, &header, 1, 0);
615 do_include_next (pfile)
619 struct file_name_list *search_start = 0;
621 if (parse_include (pfile, &header))
624 /* For #include_next, skip in the search path past the dir in which
625 the current file was found. If this is the last directory in the
626 search path, don't include anything. If the current file was
627 specified with an absolute path, use the normal search logic. If
628 this is the primary source file, use the normal search logic and
629 generate a warning. */
630 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
632 if (CPP_BUFFER (pfile)->inc->foundhere)
634 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
640 cpp_warning (pfile, "#include_next in primary source file");
642 _cpp_execute_include (pfile, &header, 0, search_start);
645 /* Subroutine of do_line. Read next token from PFILE without adding it to
646 the output buffer. If it is a number between 1 and 4, store it in *NUM
647 and return 1; otherwise, return 0 and complain if we aren't at the end
651 read_line_number (pfile, num)
658 _cpp_lex_token (pfile, &token);
659 if (token.type == CPP_NUMBER && token.val.str.len == 1)
661 val = token.val.str.text[0] - '1';
669 if (token.type != CPP_EOF)
670 cpp_error (pfile, "invalid format #line");
674 /* Another subroutine of do_line. Convert a number in STR, of length
675 LEN, to binary; store it in NUMP, and return 0 if the number was
676 well-formed, 1 if not. Temporary, hopefully. */
678 strtoul_for_line (str, len, nump)
683 unsigned long reg = 0;
697 /* Interpret #line command.
698 Note that the filename string (if any) is treated as if it were an
699 include filename. That means no escape handling. */
705 cpp_buffer *ip = CPP_BUFFER (pfile);
706 unsigned long new_lineno;
707 /* C99 raised the minimum limit on #line numbers. */
708 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
709 int enter = 0, leave = 0, rename = 0;
712 /* #line commands expand macros. */
713 cpp_get_token (pfile, &token);
714 if (token.type != CPP_NUMBER
715 || strtoul_for_line (token.val.str.text, token.val.str.len, &new_lineno))
717 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
718 cpp_token_as_text (pfile, &token));
722 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
723 cpp_pedwarn (pfile, "line number out of range");
725 cpp_get_token (pfile, &token);
727 if (token.type != CPP_EOF)
731 int action_number = 0;
733 if (token.type != CPP_STRING)
735 cpp_error (pfile, "\"%s\" is not a valid filename",
736 cpp_token_as_text (pfile, &token));
740 len = token.val.str.len;
741 fname = alloca (len + 1);
742 memcpy (fname, token.val.str.text, len);
745 if (strcmp (fname, ip->nominal_fname))
748 if (!strcmp (fname, ip->inc->name))
749 ip->nominal_fname = ip->inc->name;
751 ip->nominal_fname = _cpp_fake_include (pfile, fname);
754 if (read_line_number (pfile, &action_number) != 0)
756 if (! CPP_OPTION (pfile, preprocessed) && CPP_PEDANTIC (pfile))
757 cpp_pedwarn (pfile, "extra tokens at end of #line directive");
759 if (action_number == 1)
762 cpp_make_system_header (pfile, ip, 0);
763 read_line_number (pfile, &action_number);
765 else if (action_number == 2)
768 cpp_make_system_header (pfile, ip, 0);
769 read_line_number (pfile, &action_number);
771 if (action_number == 3)
773 cpp_make_system_header (pfile, ip, 1);
774 read_line_number (pfile, &action_number);
776 if (action_number == 4)
778 cpp_make_system_header (pfile, ip, 2);
779 read_line_number (pfile, &action_number);
785 /* Our line number is incremented after the directive is processed. */
786 ip->lineno = new_lineno - 1;
787 pfile->lexer_pos.output_line = ip->lineno;
788 if (enter && pfile->cb.enter_file)
789 (*pfile->cb.enter_file) (pfile);
790 if (leave && pfile->cb.leave_file)
791 (*pfile->cb.leave_file) (pfile);
792 if (rename && pfile->cb.rename_file)
793 (*pfile->cb.rename_file) (pfile);
797 * Report a warning or error detected by the program we are
798 * processing. Use the directive's tokens in the error message.
802 do_diagnostic (pfile, code, print_dir)
804 enum error_type code;
807 if (_cpp_begin_message (pfile, code, NULL, 0))
810 fprintf (stderr, "#%s ", pfile->directive->name);
811 pfile->state.prevent_expansion++;
812 cpp_output_line (pfile, stderr);
813 pfile->state.prevent_expansion--;
821 do_diagnostic (pfile, ERROR, 1);
828 do_diagnostic (pfile, WARNING, 1);
831 /* Report program identification. */
839 cpp_get_token (pfile, &str);
840 if (str.type != CPP_STRING)
841 cpp_error (pfile, "invalid #ident");
842 else if (pfile->cb.ident)
843 (*pfile->cb.ident) (pfile, &str.val.str);
848 /* Pragmata handling. We handle some of these, and pass the rest on
849 to the front end. C99 defines three pragmas and says that no macro
850 expansion is to be performed on them; whether or not macro
851 expansion happens for other pragmas is implementation defined.
852 This implementation never macro-expands the text after #pragma. */
854 /* Sub-handlers for the pragmas needing treatment here.
855 They return 1 if the token buffer is to be popped, 0 if not. */
858 struct pragma_entry *next;
863 void (*handler) PARAMS ((cpp_reader *));
864 struct pragma_entry *space;
869 cpp_register_pragma (pfile, space, name, handler)
873 void (*handler) PARAMS ((cpp_reader *));
875 struct pragma_entry **x, *new;
881 struct pragma_entry *p = pfile->pragmas;
882 len = strlen (space);
885 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
892 cpp_ice (pfile, "unknown #pragma namespace %s", space);
897 new = xnew (struct pragma_entry);
899 new->len = strlen (name);
901 new->u.handler = handler;
908 cpp_register_pragma_space (pfile, space)
912 struct pragma_entry *new;
913 const struct pragma_entry *p = pfile->pragmas;
914 size_t len = strlen (space);
918 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
919 /* Multiple different callers are allowed to register the same
925 new = xnew (struct pragma_entry);
931 new->next = pfile->pragmas;
932 pfile->pragmas = new;
936 _cpp_init_internal_pragmas (pfile)
940 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
941 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
944 cpp_register_pragma_space (pfile, "GCC");
946 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
947 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
948 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
955 const struct pragma_entry *p;
957 const cpp_hashnode *node;
963 pfile->state.prevent_expansion++;
964 cpp_start_lookahead (pfile);
967 cpp_get_token (pfile, &tok);
968 if (tok.type == CPP_NAME)
975 if (strlen (p->name) == len && !memcmp (p->name, name, len))
984 (*p->u.handler) (pfile);
993 cpp_stop_lookahead (pfile, drop);
994 pfile->state.prevent_expansion--;
996 if (!drop && pfile->cb.def_pragma)
997 (*pfile->cb.def_pragma) (pfile);
1001 do_pragma_once (pfile)
1004 cpp_buffer *ip = CPP_BUFFER (pfile);
1006 cpp_warning (pfile, "#pragma once is obsolete");
1008 if (CPP_PREV_BUFFER (ip) == NULL)
1009 cpp_warning (pfile, "#pragma once in main file");
1011 ip->inc->cmacro = NEVER_REREAD;
1017 do_pragma_poison (pfile)
1020 /* Poison these symbols so that all subsequent usage produces an
1025 pfile->state.poisoned_ok = 1;
1028 _cpp_lex_token (pfile, &tok);
1029 if (tok.type == CPP_EOF)
1031 if (tok.type != CPP_NAME)
1033 cpp_error (pfile, "invalid #pragma GCC poison directive");
1038 if (hp->flags & NODE_POISONED)
1041 if (hp->type == NT_MACRO)
1042 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
1043 _cpp_free_definition (hp);
1044 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1046 pfile->state.poisoned_ok = 0;
1048 #if 0 /* Doesn't quite work yet. */
1049 if (tok.type == CPP_EOF && pfile->cb.poison)
1050 (*pfile->cb.poison) (pfile);
1054 /* Mark the current header as a system header. This will suppress
1055 some categories of warnings (notably those from -pedantic). It is
1056 intended for use in system libraries that cannot be implemented in
1057 conforming C, but cannot be certain that their headers appear in a
1058 system include directory. To prevent abuse, it is rejected in the
1059 primary source file. */
1061 do_pragma_system_header (pfile)
1064 cpp_buffer *ip = CPP_BUFFER (pfile);
1065 if (CPP_PREV_BUFFER (ip) == NULL)
1066 cpp_warning (pfile, "#pragma system_header outside include file");
1068 cpp_make_system_header (pfile, ip, 1);
1073 /* Check the modified date of the current include file against a specified
1074 file. Issue a diagnostic, if the specified file is newer. We use this to
1075 determine if a fixed header should be refixed. */
1077 do_pragma_dependency (pfile)
1080 cpp_token header, msg;
1083 if (parse_include (pfile, &header))
1086 ordering = _cpp_compare_file_date (pfile, &header);
1088 cpp_warning (pfile, "cannot find source %s",
1089 cpp_token_as_text (pfile, &header));
1090 else if (ordering > 0)
1092 cpp_warning (pfile, "current file is older than %s",
1093 cpp_token_as_text (pfile, &header));
1094 cpp_start_lookahead (pfile);
1095 cpp_get_token (pfile, &msg);
1096 cpp_stop_lookahead (pfile, msg.type == CPP_EOF);
1097 if (msg.type != CPP_EOF)
1098 do_diagnostic (pfile, WARNING, 0);
1102 /* Check syntax is "(string-literal)". Returns 0 on success. */
1104 get__Pragma_string (pfile, string)
1110 cpp_get_token (pfile, &paren);
1111 if (paren.type != CPP_OPEN_PAREN)
1114 cpp_get_token (pfile, string);
1115 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1118 cpp_get_token (pfile, &paren);
1119 return paren.type != CPP_CLOSE_PAREN;
1122 /* Returns a malloced buffer containing a destringized cpp_string by
1123 removing the first \ of \" and \\ sequences. */
1124 static unsigned char *
1125 destringize (in, len)
1126 const cpp_string *in;
1129 const unsigned char *src, *limit;
1130 unsigned char *dest, *result;
1132 dest = result = (unsigned char *) xmalloc (in->len);
1133 for (src = in->text, limit = src + in->len; src < limit;)
1135 /* We know there is a character following the backslash. */
1136 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1141 *len = dest - result;
1146 _cpp_do__Pragma (pfile)
1150 unsigned char *buffer;
1153 if (get__Pragma_string (pfile, &string))
1155 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1159 buffer = destringize (&string.val.str, &len);
1160 run_directive (pfile, T_PRAGMA, (char *) buffer, len, _("<_Pragma>"));
1161 free ((PTR) buffer);
1164 /* Just ignore #sccs, on systems where we define it at all. */
1165 #ifdef SCCS_DIRECTIVE
1168 cpp_reader *pfile ATTRIBUTE_UNUSED;
1179 if (! pfile->buffer->was_skipping)
1181 const cpp_hashnode *node = lex_macro_node (pfile);
1184 skip = node->type != NT_MACRO;
1187 push_conditional (pfile, skip, T_IFDEF, 0);
1195 const cpp_hashnode *node = 0;
1197 if (! pfile->buffer->was_skipping)
1199 node = lex_macro_node (pfile);
1201 skip = node->type == NT_MACRO;
1204 push_conditional (pfile, skip, T_IFNDEF, node);
1207 /* #if cooperates with parse_defined to handle multiple-include
1208 optimisations. If macro expansions or identifiers appear in the
1209 expression, we cannot treat it as a controlling conditional, since
1210 their values could change in the future. */
1217 const cpp_hashnode *cmacro = 0;
1219 if (! pfile->buffer->was_skipping)
1221 /* Controlling macro of #if ! defined () */
1222 pfile->mi_ind_cmacro = 0;
1223 skip = _cpp_parse_expr (pfile) == 0;
1224 cmacro = pfile->mi_ind_cmacro;
1227 push_conditional (pfile, skip, T_IF, cmacro);
1230 /* Flip skipping state if appropriate and continue without changing
1231 if_stack; this is so that the error message for missing #endif's
1232 etc. will point to the original #if. */
1238 cpp_buffer *buffer = pfile->buffer;
1239 struct if_stack *ifs = buffer->if_stack;
1242 cpp_error (pfile, "#else without #if");
1245 if (ifs->type == T_ELSE)
1247 cpp_error (pfile, "#else after #else");
1248 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1249 "the conditional began here");
1253 /* Buffer->was_skipping is 1 if all conditionals in this chain
1254 have been false, 2 if a conditional has been true. */
1255 if (! ifs->was_skipping && buffer->was_skipping != 2)
1256 buffer->was_skipping = ! buffer->was_skipping;
1258 /* Invalidate any controlling macro. */
1265 /* handle a #elif directive by not changing if_stack either. see the
1266 comment above do_else. */
1272 cpp_buffer *buffer = pfile->buffer;
1273 struct if_stack *ifs = buffer->if_stack;
1276 cpp_error (pfile, "#elif without #if");
1279 if (ifs->type == T_ELSE)
1281 cpp_error (pfile, "#elif after #else");
1282 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1283 "the conditional began here");
1287 /* Don't evaluate #elif if our higher level is skipping. */
1288 if (! ifs->was_skipping)
1290 /* Buffer->was_skipping is 1 if all conditionals in this
1291 chain have been false, 2 if a conditional has been true. */
1292 if (buffer->was_skipping == 1)
1293 buffer->was_skipping = ! _cpp_parse_expr (pfile);
1295 buffer->was_skipping = 2;
1297 /* Invalidate any controlling macro. */
1303 /* #endif pops the if stack and resets pfile->skipping. */
1309 cpp_buffer *buffer = pfile->buffer;
1310 struct if_stack *ifs = buffer->if_stack;
1313 cpp_error (pfile, "#endif without #if");
1316 /* If potential control macro, we go back outside again. */
1317 if (ifs->next == 0 && ifs->mi_cmacro)
1319 pfile->mi_state = MI_OUTSIDE;
1320 pfile->mi_cmacro = ifs->mi_cmacro;
1323 buffer->if_stack = ifs->next;
1324 buffer->was_skipping = ifs->was_skipping;
1325 obstack_free (pfile->buffer_ob, ifs);
1331 /* Push an if_stack entry and set pfile->skipping accordingly.
1332 If this is a #ifndef starting at the beginning of a file,
1333 CMACRO is the macro name tested by the #ifndef. */
1336 push_conditional (pfile, skip, type, cmacro)
1340 const cpp_hashnode *cmacro;
1342 struct if_stack *ifs;
1343 cpp_buffer *buffer = pfile->buffer;
1345 ifs = xobnew (pfile->buffer_ob, struct if_stack);
1346 ifs->pos = pfile->directive_pos;
1347 ifs->next = buffer->if_stack;
1348 ifs->was_skipping = buffer->was_skipping;
1350 if (pfile->mi_state == MI_OUTSIDE && pfile->mi_cmacro == 0)
1351 ifs->mi_cmacro = cmacro;
1355 buffer->was_skipping = skip;
1356 buffer->if_stack = ifs;
1359 /* Read the tokens of the answer into the macro pool. Only commit the
1360 memory if we intend it as permanent storage, i.e. the #assert case.
1361 Returns 0 on success. */
1364 parse_answer (pfile, answerp, type)
1366 struct answer **answerp;
1369 cpp_token paren, *token;
1370 struct answer *answer;
1372 if (POOL_FRONT (&pfile->macro_pool) + sizeof (struct answer) >
1373 POOL_LIMIT (&pfile->macro_pool))
1374 _cpp_next_chunk (&pfile->macro_pool, sizeof (struct answer), 0);
1375 answer = (struct answer *) POOL_FRONT (&pfile->macro_pool);
1378 /* In a conditional, it is legal to not have an open paren. We
1379 should save the following token in this case. */
1381 cpp_start_lookahead (pfile);
1382 cpp_get_token (pfile, &paren);
1384 cpp_stop_lookahead (pfile, paren.type == CPP_OPEN_PAREN);
1386 /* If not a paren, see if we're OK. */
1387 if (paren.type != CPP_OPEN_PAREN)
1389 /* In a conditional no answer is a test for any answer. It
1390 could be followed by any token. */
1394 /* #unassert with no answer is valid - it removes all answers. */
1395 if (type == T_UNASSERT && paren.type == CPP_EOF)
1398 cpp_error (pfile, "missing '(' after predicate");
1404 token = &answer->first[answer->count];
1405 /* Check we have room for the token. */
1406 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1408 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1409 (unsigned char **) &answer);
1410 token = &answer->first[answer->count];
1413 cpp_get_token (pfile, token);
1414 if (token->type == CPP_CLOSE_PAREN)
1417 if (token->type == CPP_EOF)
1419 cpp_error (pfile, "missing ')' to complete answer");
1425 if (answer->count == 0)
1427 cpp_error (pfile, "predicate's answer is empty");
1431 /* Drop whitespace at start. */
1432 answer->first->flags &= ~PREV_WHITE;
1435 if (type == T_ASSERT || type == T_UNASSERT)
1440 /* Parses an assertion, returning a pointer to the hash node of the
1441 predicate, or 0 on error. If an answer was supplied, it is placed
1442 in ANSWERP, otherwise it is set to 0. */
1443 static cpp_hashnode *
1444 parse_assertion (pfile, answerp, type)
1446 struct answer **answerp;
1449 cpp_hashnode *result = 0;
1450 cpp_token predicate;
1452 /* We don't expand predicates or answers. */
1453 pfile->state.prevent_expansion++;
1455 /* Use the permanent pool for storage (for the answers). */
1456 pfile->string_pool = &pfile->ident_pool;
1459 cpp_get_token (pfile, &predicate);
1460 if (predicate.type == CPP_EOF)
1461 cpp_error (pfile, "assertion without predicate");
1462 else if (predicate.type != CPP_NAME)
1463 cpp_error (pfile, "predicate must be an identifier");
1464 else if (parse_answer (pfile, answerp, type) == 0)
1466 unsigned int len = predicate.val.node->length;
1467 unsigned char *sym = alloca (len + 1);
1469 /* Prefix '#' to get it out of macro namespace. */
1471 memcpy (sym + 1, predicate.val.node->name, len);
1472 result = cpp_lookup (pfile, sym, len + 1);
1475 pfile->string_pool = &pfile->temp_string_pool;
1476 pfile->state.prevent_expansion--;
1480 /* Returns a pointer to the pointer to the answer in the answer chain,
1481 or a pointer to NULL if the answer is not in the chain. */
1482 static struct answer **
1483 find_answer (node, candidate)
1485 const struct answer *candidate;
1488 struct answer **result;
1490 for (result = &node->value.answers; *result; result = &(*result)->next)
1492 struct answer *answer = *result;
1494 if (answer->count == candidate->count)
1496 for (i = 0; i < answer->count; i++)
1497 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1500 if (i == answer->count)
1508 /* Test an assertion within a preprocessor conditional. Returns
1509 non-zero on failure, zero on success. On success, the result of
1510 the test is written into VALUE. */
1512 _cpp_test_assertion (pfile, value)
1516 struct answer *answer;
1519 node = parse_assertion (pfile, &answer, T_IF);
1521 *value = (node->type == NT_ASSERTION &&
1522 (answer == 0 || *find_answer (node, answer) != 0));
1524 /* We don't commit the memory for the answer - it's temporary only. */
1532 struct answer *new_answer;
1535 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1538 /* Place the new answer in the answer list. First check there
1539 is not a duplicate. */
1540 new_answer->next = 0;
1541 if (node->type == NT_ASSERTION)
1543 if (*find_answer (node, new_answer))
1545 cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
1548 new_answer->next = node->value.answers;
1550 node->type = NT_ASSERTION;
1551 node->value.answers = new_answer;
1552 POOL_COMMIT (&pfile->macro_pool, (sizeof (struct answer)
1553 + (new_answer->count - 1)
1554 * sizeof (cpp_token)));
1563 struct answer *answer;
1565 node = parse_assertion (pfile, &answer, T_UNASSERT);
1566 /* It isn't an error to #unassert something that isn't asserted. */
1567 if (node && node->type == NT_ASSERTION)
1571 struct answer **p = find_answer (node, answer), *temp;
1573 /* Remove the answer from the list. */
1578 /* Did we free the last answer? */
1579 if (node->value.answers == 0)
1580 node->type = NT_VOID;
1583 _cpp_free_definition (node);
1586 /* We don't commit the memory for the answer - it's temporary only. */
1589 /* These are for -D, -U, -A. */
1591 /* Process the string STR as if it appeared as the body of a #define.
1592 If STR is just an identifier, define it with value 1.
1593 If STR has anything after the identifier, then it should
1594 be identifier=definition. */
1597 cpp_define (pfile, str)
1604 /* Copy the entire option so we can modify it.
1605 Change the first "=" in the string to a space. If there is none,
1606 tack " 1" on the end. */
1608 /* Length including the null. */
1609 count = strlen (str);
1610 buf = (char *) alloca (count + 2);
1611 memcpy (buf, str, count);
1613 p = strchr (str, '=');
1622 run_directive (pfile, T_DEFINE, buf, count, 0);
1625 /* Slight variant of the above for use by initialize_builtins, which (a)
1626 knows how to set up the buffer itself, (b) needs a different "filename"
1629 _cpp_define_builtin (pfile, str)
1633 run_directive (pfile, T_DEFINE, str, strlen (str), _("<builtin>"));
1636 /* Process MACRO as if it appeared as the body of an #undef. */
1638 cpp_undef (pfile, macro)
1642 run_directive (pfile, T_UNDEF, macro, strlen (macro), 0);
1645 /* Process the string STR as if it appeared as the body of a #assert. */
1647 cpp_assert (pfile, str)
1651 handle_assertion (pfile, str, T_ASSERT);
1654 /* Process STR as if it appeared as the body of an #unassert. */
1656 cpp_unassert (pfile, str)
1660 handle_assertion (pfile, str, T_UNASSERT);
1663 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1665 handle_assertion (pfile, str, type)
1670 size_t count = strlen (str);
1671 const char *p = strchr (str, '=');
1675 /* Copy the entire option so we can modify it. Change the first
1676 "=" in the string to a '(', and tack a ')' on the end. */
1677 char *buf = (char *) alloca (count + 1);
1679 memcpy (buf, str, count);
1685 run_directive (pfile, type, str, count, 0);
1688 /* Allocate a new cpp_buffer for PFILE, and push it on the input
1689 buffer stack. If BUFFER != NULL, then use the LENGTH characters in
1690 BUFFER as the new input buffer. Return the new buffer, or NULL on
1694 cpp_push_buffer (pfile, buffer, length)
1696 const U_CHAR *buffer;
1699 cpp_buffer *buf = CPP_BUFFER (pfile);
1701 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1703 cpp_fatal (pfile, "#include nested too deeply");
1707 new = xobnew (pfile->buffer_ob, cpp_buffer);
1708 /* Clears, amongst other things, if_stack and mi_cmacro. */
1709 memset (new, 0, sizeof (cpp_buffer));
1711 pfile->lexer_pos.output_line = 1;
1712 new->line_base = new->buf = new->cur = buffer;
1713 new->rlimit = buffer + length;
1716 new->was_skipping = 0;
1717 /* Preprocessed files don't do trigraph and escaped newline processing. */
1718 new->from_stage3 = CPP_OPTION (pfile, preprocessed);
1719 /* No read ahead or extra char initially. */
1720 new->read_ahead = EOF;
1721 new->extra_char = EOF;
1722 pfile->state.next_bol = 1;
1724 CPP_BUFFER (pfile) = new;
1729 cpp_pop_buffer (pfile)
1732 cpp_buffer *buffer = pfile->buffer;
1733 struct if_stack *ifs = buffer->if_stack;
1736 /* Walk back up the conditional stack till we reach its level at
1737 entry to this file, issuing error messages. */
1738 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1739 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1740 "unterminated #%s", dtable[ifs->type].name);
1742 wfb = (buffer->inc != 0);
1744 _cpp_pop_file_buffer (pfile, buffer);
1746 pfile->buffer = buffer->prev;
1747 obstack_free (pfile->buffer_ob, buffer);
1748 pfile->buffer_stack_depth--;
1750 if (pfile->buffer && wfb && pfile->cb.leave_file)
1751 (*pfile->cb.leave_file) (pfile);
1753 return pfile->buffer;
1756 #define obstack_chunk_alloc xmalloc
1757 #define obstack_chunk_free free
1759 _cpp_init_stacks (pfile)
1765 pfile->buffer_ob = xnew (struct obstack);
1766 obstack_init (pfile->buffer_ob);
1768 /* Register the directives. */
1769 for (i = 1; i < N_DIRECTIVES; i++)
1771 node = cpp_lookup (pfile, dtable[i - 1].name, dtable[i - 1].length);
1772 node->directive_index = i;
1777 _cpp_cleanup_stacks (pfile)
1780 obstack_free (pfile->buffer_ob, 0);
1781 free (pfile->buffer_ob);