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
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. */
29 /* Chained list of answers to an assertion. */
37 /* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
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 *));
53 struct pragma_entry *next;
54 const cpp_hashnode *pragma; /* Name and length. */
58 struct pragma_entry *space;
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. */
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 #define IF_COND (1 << 1)
79 /* Defines one #-directive, including how to handle it. */
80 typedef void (*directive_handler) PARAMS ((cpp_reader *));
81 typedef struct directive directive;
84 directive_handler handler; /* Function to handle directive. */
85 const U_CHAR *name; /* Name of directive. */
86 unsigned short length; /* Length of name. */
87 unsigned char origin; /* Origin of directive. */
88 unsigned char flags; /* Flags describing this directive. */
91 /* Forward declarations. */
93 static void skip_rest_of_line PARAMS ((cpp_reader *));
94 static void check_eol PARAMS ((cpp_reader *));
95 static void start_directive PARAMS ((cpp_reader *));
96 static void end_directive PARAMS ((cpp_reader *, int));
97 static void directive_diagnostics
98 PARAMS ((cpp_reader *, const directive *, int));
99 static void run_directive PARAMS ((cpp_reader *, int,
100 const char *, size_t));
101 static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
102 static const cpp_token *parse_include PARAMS ((cpp_reader *));
103 static void push_conditional PARAMS ((cpp_reader *, int, int,
104 const cpp_hashnode *));
105 static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
106 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
108 static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int));
109 static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
110 static void do_include_common PARAMS ((cpp_reader *, enum include_type));
111 static struct pragma_entry *lookup_pragma_entry
112 PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma));
113 static struct pragma_entry *insert_pragma_entry
114 PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *,
116 static void do_pragma_once PARAMS ((cpp_reader *));
117 static void do_pragma_poison PARAMS ((cpp_reader *));
118 static void do_pragma_system_header PARAMS ((cpp_reader *));
119 static void do_pragma_dependency PARAMS ((cpp_reader *));
120 static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
121 static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
122 static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *));
123 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
124 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
126 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
127 const struct answer *));
128 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
130 /* This is the table of directive handlers. It is ordered by
131 frequency of occurrence; the numbers at the end are directive
132 counts from all the source code I have lying around (egcs and libc
133 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
134 pcmcia-cs-3.0.9). This is no longer important as directive lookup
135 is now O(1). All extensions other than #warning and #include_next
136 are deprecated. The name is where the extension appears to have
139 #define DIRECTIVE_TABLE \
140 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
141 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
142 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
143 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
144 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
145 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
146 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
147 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
148 D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
149 D(elif, T_ELIF, STDC89, COND) /* 610 */ \
150 D(error, T_ERROR, STDC89, 0) /* 475 */ \
151 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
152 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
153 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
154 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
155 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
156 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
157 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
158 SCCS_ENTRY /* 0 SVR4? */
160 /* #sccs is not always recognized. */
161 #ifdef SCCS_DIRECTIVE
162 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
164 # define SCCS_ENTRY /* nothing */
167 /* Use the table to generate a series of prototypes, an enum for the
168 directive names, and an array of directive handlers. */
170 /* The directive-processing functions are declared to return int
171 instead of void, because some old compilers have trouble with
172 pointers to functions returning void. */
174 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
175 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
179 #define D(n, tag, o, f) tag,
187 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
188 #define D(name, t, origin, flags) \
189 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
190 sizeof STRINGX(name) - 1, origin, flags },
191 static const directive dtable[] =
196 #undef DIRECTIVE_TABLE
198 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
200 /* Skip any remaining tokens in a directive. */
202 skip_rest_of_line (pfile)
205 /* Discard all stacked contexts. */
206 while (pfile->context != &pfile->base_context)
207 _cpp_pop_context (pfile);
209 /* Sweep up all tokens remaining on the line. */
211 while (_cpp_lex_token (pfile)->type != CPP_EOF)
215 /* Ensure there are no stray tokens at the end of a directive. */
220 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
221 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
222 pfile->directive->name);
225 /* Called when entering a directive, _Pragma or command-line directive. */
227 start_directive (pfile)
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_line = pfile->line;
238 /* Called when leaving a directive, _Pragma or command-line directive. */
240 end_directive (pfile, skip_line)
244 /* We don't skip for an assembler #. */
247 skip_rest_of_line (pfile);
248 if (!pfile->keep_tokens)
250 pfile->cur_run = &pfile->base_run;
251 pfile->cur_token = pfile->base_run.base;
256 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
257 pfile->state.in_directive = 0;
258 pfile->state.angled_headers = 0;
259 pfile->state.line_extension = 0;
260 pfile->directive = 0;
263 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
264 the '#' was indented. */
266 directive_diagnostics (pfile, dir, indented)
268 const directive *dir;
271 if (pfile->state.line_extension)
273 if (CPP_PEDANTIC (pfile)
274 && ! pfile->state.skipping)
275 cpp_pedwarn (pfile, "style of line directive is a GCC extension");
279 /* Issue -pedantic warnings for extensions. */
280 if (CPP_PEDANTIC (pfile)
281 && ! pfile->state.skipping
282 && dir->origin == EXTENSION)
283 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
285 /* Traditionally, a directive is ignored unless its # is in
286 column 1. Therefore in code intended to work with K+R
287 compilers, directives added by C89 must have their #
288 indented, and directives present in traditional C must not.
289 This is true even of directives in skipped conditional
290 blocks. #elif cannot be used at all. */
291 if (CPP_WTRADITIONAL (pfile))
293 if (dir == &dtable[T_ELIF])
294 cpp_warning (pfile, "suggest not using #elif in traditional C");
295 else if (indented && dir->origin == KANDR)
297 "traditional C ignores #%s with the # indented",
299 else if (!indented && dir->origin != KANDR)
301 "suggest hiding #%s from traditional C with an indented #",
307 /* Check if we have a known directive. INDENTED is non-zero if the
308 '#' of the directive was indented. This function is in this file
309 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
310 non-zero if the line of tokens has been handled, zero if we should
311 continue processing the line. */
313 _cpp_handle_directive (pfile, indented)
317 const directive *dir = 0;
318 const cpp_token *dname;
319 bool was_parsing_args = pfile->state.parsing_args;
322 if (was_parsing_args)
324 if (CPP_OPTION (pfile, pedantic))
326 "embedding a directive within macro arguments is not portable");
327 pfile->state.parsing_args = 0;
328 pfile->state.prevent_expansion = 0;
330 start_directive (pfile);
331 dname = _cpp_lex_token (pfile);
333 if (dname->type == CPP_NAME)
335 if (dname->val.node->directive_index)
336 dir = &dtable[dname->val.node->directive_index - 1];
338 /* We do not recognise the # followed by a number extension in
340 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
342 dir = &dtable[T_LINE];
343 pfile->state.line_extension = 1;
348 /* If we have a directive that is not an opening conditional,
349 invalidate any control macro. */
350 if (! (dir->flags & IF_COND))
351 pfile->mi_valid = false;
353 /* Kluge alert. In order to be sure that code like this
358 does not cause '#define foo bar' to get executed when
359 compiled with -save-temps, we recognize directives in
360 -fpreprocessed mode only if the # is in column 1. cppmacro.c
361 puts a space in front of any '#' at the start of a macro. */
362 if (CPP_OPTION (pfile, preprocessed)
363 && (indented || !(dir->flags & IN_I)))
370 /* In failed conditional groups, all non-conditional
371 directives are ignored. Before doing that, whether
372 skipping or not, we should lex angle-bracketed headers
373 correctly, and maybe output some diagnostics. */
374 pfile->state.angled_headers = dir->flags & INCL;
375 if (! CPP_OPTION (pfile, preprocessed))
376 directive_diagnostics (pfile, dir, indented);
377 if (pfile->state.skipping && !(dir->flags & COND))
381 else if (dname->type == CPP_EOF)
382 ; /* CPP_EOF is the "null directive". */
385 /* An unknown directive. Don't complain about it in assembly
386 source: we don't know where the comments are, and # may
387 introduce assembler pseudo-ops. Don't complain about invalid
388 directives in skipped conditional groups (6.10 p4). */
389 if (CPP_OPTION (pfile, lang) == CLK_ASM)
391 else if (!pfile->state.skipping)
392 cpp_error (pfile, "invalid preprocessing directive #%s",
393 cpp_token_as_text (pfile, dname));
398 pfile->directive = dir;
399 (*pfile->directive->handler) (pfile);
402 _cpp_backup_tokens (pfile, 1);
404 end_directive (pfile, skip);
405 if (was_parsing_args)
407 /* Restore state when within macro args. */
408 pfile->state.parsing_args = 2;
409 pfile->state.prevent_expansion = 1;
410 pfile->buffer->saved_flags |= PREV_WHITE;
415 /* Directive handler wrapper used by the command line option
418 run_directive (pfile, dir_no, buf, count)
424 cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
425 /* from_stage3 */ true, 1);
426 start_directive (pfile);
427 /* We don't want a leading # to be interpreted as a directive. */
428 pfile->buffer->saved_flags = 0;
429 pfile->directive = &dtable[dir_no];
430 (void) (*pfile->directive->handler) (pfile);
431 end_directive (pfile, 1);
432 _cpp_pop_buffer (pfile);
435 /* Checks for validity the macro name in #define, #undef, #ifdef and
436 #ifndef directives. */
437 static cpp_hashnode *
438 lex_macro_node (pfile)
442 const cpp_token *token = _cpp_lex_token (pfile);
444 /* The token immediately after #define must be an identifier. That
445 identifier may not be "defined", per C99 6.10.8p4.
446 In C++, it may not be any of the "named operators" either,
447 per C++98 [lex.digraph], [lex.key].
448 Finally, the identifier may not have been poisoned. (In that case
449 the lexer has issued the error message for us.) */
451 if (token->type != CPP_NAME)
453 if (token->type == CPP_EOF)
454 cpp_error (pfile, "no macro name given in #%s directive",
455 pfile->directive->name);
456 else if (token->flags & NAMED_OP)
458 "\"%s\" cannot be used as a macro name as it is an operator in C++",
459 NODE_NAME (token->val.node));
461 cpp_error (pfile, "macro names must be identifiers");
466 node = token->val.node;
467 if (node->flags & NODE_POISONED)
470 if (node == pfile->spec_nodes.n_defined)
472 cpp_error (pfile, "\"%s\" cannot be used as a macro name",
480 /* Process a #define directive. Most work is done in cppmacro.c. */
485 cpp_hashnode *node = lex_macro_node (pfile);
489 if (_cpp_create_definition (pfile, node))
490 if (pfile->cb.define)
491 (*pfile->cb.define) (pfile, pfile->directive_line, node);
495 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
500 cpp_hashnode *node = lex_macro_node (pfile);
502 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
503 is not currently defined as a macro name. */
504 if (node && node->type == NT_MACRO)
507 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
509 if (node->flags & NODE_WARN)
510 cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
512 _cpp_free_definition (node);
517 /* Helper routine used by parse_include. Reinterpret the current line
518 as an h-char-sequence (< ... >); we are looking at the first token
519 after the <. Returns the header as a token, or NULL on failure. */
520 static const cpp_token *
521 glue_header_name (pfile)
524 cpp_token *header = NULL;
525 const cpp_token *token;
526 unsigned char *buffer;
527 size_t len, total_len = 0, capacity = 1024;
529 /* To avoid lexed tokens overwriting our glued name, we can only
530 allocate from the string pool once we've lexed everything. */
531 buffer = (unsigned char *) xmalloc (capacity);
534 token = cpp_get_token (pfile);
536 if (token->type == CPP_GREATER || token->type == CPP_EOF)
539 len = cpp_token_len (token);
540 if (total_len + len > capacity)
542 capacity = (capacity + len) * 2;
543 buffer = (unsigned char *) xrealloc (buffer, capacity);
546 if (token->flags & PREV_WHITE)
547 buffer[total_len++] = ' ';
549 total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer;
552 if (token->type == CPP_EOF)
553 cpp_error (pfile, "missing terminating > character");
556 unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1);
557 memcpy (token_mem, buffer, total_len);
558 token_mem[total_len] = '\0';
560 header = _cpp_temp_token (pfile);
561 header->type = CPP_HEADER_NAME;
563 header->val.str.len = total_len;
564 header->val.str.text = token_mem;
571 /* Returns the header string of #include, #include_next, #import and
572 #pragma dependency. Returns NULL on error. */
573 static const cpp_token *
574 parse_include (pfile)
577 const unsigned char *dir;
578 const cpp_token *header;
580 if (pfile->directive == &dtable[T_PRAGMA])
581 dir = U"pragma dependency";
583 dir = pfile->directive->name;
585 /* Allow macro expansion. */
586 header = cpp_get_token (pfile);
587 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
589 if (header->type != CPP_LESS)
591 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
595 header = glue_header_name (pfile);
600 if (header->val.str.len == 0)
602 cpp_error (pfile, "empty file name in #%s", dir);
609 /* Handle #include, #include_next and #import. */
611 do_include_common (pfile, type)
613 enum include_type type;
615 const cpp_token *header;
617 /* For #include_next, if this is the primary source file, warn and
618 use the normal search logic. */
619 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
621 cpp_warning (pfile, "#include_next in primary source file");
624 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
626 CPP_OPTION (pfile, warn_import) = 0;
628 "#import is obsolete, use an #ifndef wrapper in the header file");
631 header = parse_include (pfile);
634 /* Prevent #include recursion. */
635 if (pfile->line_maps.depth >= CPP_STACK_MAX)
636 cpp_fatal (pfile, "#include nested too deeply");
640 /* Get out of macro context, if we are. */
641 skip_rest_of_line (pfile);
642 if (pfile->cb.include)
643 (*pfile->cb.include) (pfile, pfile->directive_line,
644 pfile->directive->name, header);
646 _cpp_execute_include (pfile, header, type);
655 do_include_common (pfile, IT_INCLUDE);
662 do_include_common (pfile, IT_IMPORT);
666 do_include_next (pfile)
669 do_include_common (pfile, IT_INCLUDE_NEXT);
672 /* Subroutine of do_line. Read possible flags after file name. LAST
673 is the last flag seen; 0 if this is the first flag. Return the flag
674 if it is valid, 0 at the end of the directive. Otherwise complain. */
676 read_flag (pfile, last)
680 const cpp_token *token = _cpp_lex_token (pfile);
682 if (token->type == CPP_NUMBER && token->val.str.len == 1)
684 unsigned int flag = token->val.str.text[0] - '0';
686 if (flag > last && flag <= 4
687 && (flag != 4 || last == 3)
688 && (flag != 2 || last == 0))
692 if (token->type != CPP_EOF)
693 cpp_error (pfile, "invalid flag \"%s\" in line directive",
694 cpp_token_as_text (pfile, token));
698 /* Another subroutine of do_line. Convert a number in STR, of length
699 LEN, to binary; store it in NUMP, and return 0 if the number was
700 well-formed, 1 if not. Temporary, hopefully. */
702 strtoul_for_line (str, len, nump)
707 unsigned long reg = 0;
721 /* Interpret #line command.
722 Note that the filename string (if any) is treated as if it were an
723 include filename. That means no escape handling. */
728 const cpp_token *token;
729 const char *new_file = pfile->map->to_file;
730 unsigned long new_lineno;
731 unsigned int cap, new_sysp = pfile->map->sysp;
732 enum lc_reason reason = LC_RENAME;
734 /* C99 raised the minimum limit on #line numbers. */
735 cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
737 /* Putting this in _cpp_handle_directive risks two calls to
738 _cpp_backup_tokens in some circumstances, which can segfault. */
739 if (pfile->state.line_extension)
740 _cpp_backup_tokens (pfile, 1);
742 /* #line commands expand macros. */
743 token = cpp_get_token (pfile);
744 if (token->type != CPP_NUMBER
745 || strtoul_for_line (token->val.str.text, token->val.str.len,
748 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
749 cpp_token_as_text (pfile, token));
753 if (CPP_PEDANTIC (pfile) && ! pfile->state.line_extension
754 && (new_lineno == 0 || new_lineno > cap))
755 cpp_pedwarn (pfile, "line number out of range");
757 token = cpp_get_token (pfile);
758 if (token->type == CPP_STRING)
760 new_file = (const char *) token->val.str.text;
762 /* Only accept flags for the # 55 form. */
763 if (pfile->state.line_extension)
768 flag = read_flag (pfile, 0);
772 /* Fake an include for cpp_included (). */
773 _cpp_fake_include (pfile, new_file);
774 flag = read_flag (pfile, flag);
779 flag = read_flag (pfile, flag);
784 flag = read_flag (pfile, flag);
791 else if (token->type != CPP_EOF)
793 cpp_error (pfile, "\"%s\" is not a valid filename",
794 cpp_token_as_text (pfile, token));
798 skip_rest_of_line (pfile);
799 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
802 /* Arrange the file_change callback. pfile->line has changed to
803 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
804 header, 2 for a system header that needs to be extern "C" protected,
805 and zero otherwise. */
807 _cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
809 enum lc_reason reason;
811 unsigned int file_line;
814 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
815 pfile->line, to_file, file_line);
817 if (pfile->cb.file_change)
818 (*pfile->cb.file_change) (pfile, pfile->map);
821 /* Report a warning or error detected by the program we are
822 processing. Use the directive's tokens in the error message. */
824 do_diagnostic (pfile, code, print_dir)
826 enum error_type code;
829 if (_cpp_begin_message (pfile, code, 0, 0))
832 fprintf (stderr, "#%s ", pfile->directive->name);
833 pfile->state.prevent_expansion++;
834 cpp_output_line (pfile, stderr);
835 pfile->state.prevent_expansion--;
843 do_diagnostic (pfile, ERROR, 1);
850 /* We want #warning diagnostics to be emitted in system headers too. */
851 do_diagnostic (pfile, WARNING_SYSHDR, 1);
854 /* Report program identification. */
859 const cpp_token *str = cpp_get_token (pfile);
861 if (str->type != CPP_STRING)
862 cpp_error (pfile, "invalid #ident directive");
863 else if (pfile->cb.ident)
864 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
869 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
870 matching entry, or NULL if none is found. The returned entry could
871 be the start of a namespace chain, or a pragma. */
872 static struct pragma_entry *
873 lookup_pragma_entry (chain, pragma)
874 struct pragma_entry *chain;
875 const cpp_hashnode *pragma;
877 while (chain && chain->pragma != pragma)
883 /* Create and insert a pragma entry for NAME at the beginning of a
884 singly-linked CHAIN. If handler is NULL, it is a namespace,
885 otherwise it is a pragma and its handler. */
886 static struct pragma_entry *
887 insert_pragma_entry (pfile, chain, pragma, handler)
889 struct pragma_entry **chain;
890 const cpp_hashnode *pragma;
893 struct pragma_entry *new;
895 new = (struct pragma_entry *)
896 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
897 new->pragma = pragma;
901 new->u.handler = handler;
914 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
915 goes in the global namespace. HANDLER is the handler it will call,
916 which must be non-NULL. */
918 cpp_register_pragma (pfile, space, name, handler)
924 struct pragma_entry **chain = &pfile->pragmas;
925 struct pragma_entry *entry;
926 const cpp_hashnode *node;
933 node = cpp_lookup (pfile, U space, strlen (space));
934 entry = lookup_pragma_entry (*chain, node);
936 entry = insert_pragma_entry (pfile, chain, node, NULL);
937 else if (!entry->is_nspace)
939 chain = &entry->u.space;
942 /* Check for duplicates. */
943 node = cpp_lookup (pfile, U name, strlen (name));
944 entry = lookup_pragma_entry (*chain, node);
947 if (entry->is_nspace)
950 "registering \"%s\" as both a pragma and a pragma namespace",
953 cpp_ice (pfile, "#pragma %s %s is already registered", space, name);
955 cpp_ice (pfile, "#pragma %s is already registered", name);
958 insert_pragma_entry (pfile, chain, node, handler);
961 /* Register the pragmas the preprocessor itself handles. */
963 _cpp_init_internal_pragmas (pfile)
966 /* Pragmas in the global namespace. */
967 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
968 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
970 /* New GCC-specific pragmas should be put in the GCC namespace. */
971 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
972 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
973 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
976 /* Pragmata handling. We handle some, and pass the rest on to the
977 front end. C99 defines three pragmas and says that no macro
978 expansion is to be performed on them; whether or not macro
979 expansion happens for other pragmas is implementation defined.
980 This implementation never macro-expands the text after #pragma. */
985 const struct pragma_entry *p = NULL;
986 const cpp_token *token;
987 unsigned int count = 1;
989 pfile->state.prevent_expansion++;
991 token = cpp_get_token (pfile);
992 if (token->type == CPP_NAME)
994 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
995 if (p && p->is_nspace)
998 token = cpp_get_token (pfile);
999 if (token->type == CPP_NAME)
1000 p = lookup_pragma_entry (p->u.space, token->val.node);
1006 /* FIXME. This is an awful kludge to get the front ends to update
1007 their notion of line number for diagnostic purposes. The line
1008 number should be passed to the handler and they should do it
1009 themselves. Stand-alone CPP must ignore us, otherwise it will
1010 prefix the directive with spaces, hence the 1. Ugh. */
1011 if (pfile->cb.line_change)
1012 (*pfile->cb.line_change)(pfile, token, 1);
1015 (*p->u.handler) (pfile);
1016 else if (pfile->cb.def_pragma)
1018 _cpp_backup_tokens (pfile, count);
1019 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1022 pfile->state.prevent_expansion--;
1025 /* Handle #pragma once. */
1027 do_pragma_once (pfile)
1030 cpp_warning (pfile, "#pragma once is obsolete");
1032 if (pfile->buffer->prev == NULL)
1033 cpp_warning (pfile, "#pragma once in main file");
1035 _cpp_never_reread (pfile->buffer->inc);
1040 /* Handle #pragma poison, to poison one or more identifiers so that
1041 the lexer produces a hard error for each subsequent usage. */
1043 do_pragma_poison (pfile)
1046 const cpp_token *tok;
1049 pfile->state.poisoned_ok = 1;
1052 tok = _cpp_lex_token (pfile);
1053 if (tok->type == CPP_EOF)
1055 if (tok->type != CPP_NAME)
1057 cpp_error (pfile, "invalid #pragma GCC poison directive");
1062 if (hp->flags & NODE_POISONED)
1065 if (hp->type == NT_MACRO)
1066 cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
1067 _cpp_free_definition (hp);
1068 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1070 pfile->state.poisoned_ok = 0;
1073 /* Mark the current header as a system header. This will suppress
1074 some categories of warnings (notably those from -pedantic). It is
1075 intended for use in system libraries that cannot be implemented in
1076 conforming C, but cannot be certain that their headers appear in a
1077 system include directory. To prevent abuse, it is rejected in the
1078 primary source file. */
1080 do_pragma_system_header (pfile)
1083 cpp_buffer *buffer = pfile->buffer;
1085 if (buffer->prev == 0)
1086 cpp_warning (pfile, "#pragma system_header ignored outside include file");
1090 skip_rest_of_line (pfile);
1091 cpp_make_system_header (pfile, 1, 0);
1095 /* Check the modified date of the current include file against a specified
1096 file. Issue a diagnostic, if the specified file is newer. We use this to
1097 determine if a fixed header should be refixed. */
1099 do_pragma_dependency (pfile)
1102 const cpp_token *header;
1105 header = parse_include (pfile);
1109 ordering = _cpp_compare_file_date (pfile, header);
1111 cpp_warning (pfile, "cannot find source %s",
1112 cpp_token_as_text (pfile, header));
1113 else if (ordering > 0)
1115 cpp_warning (pfile, "current file is older than %s",
1116 cpp_token_as_text (pfile, header));
1117 if (cpp_get_token (pfile)->type != CPP_EOF)
1119 _cpp_backup_tokens (pfile, 1);
1120 do_diagnostic (pfile, WARNING, 0);
1125 /* Get a token but skip padding. */
1126 static const cpp_token *
1127 get_token_no_padding (pfile)
1132 const cpp_token *result = cpp_get_token (pfile);
1133 if (result->type != CPP_PADDING)
1138 /* Check syntax is "(string-literal)". Returns the string on success,
1139 or NULL on failure. */
1140 static const cpp_token *
1141 get__Pragma_string (pfile)
1144 const cpp_token *string;
1146 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1149 string = get_token_no_padding (pfile);
1150 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1153 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1159 /* Destringize IN into a temporary buffer, by removing the first \ of
1160 \" and \\ sequences, and process the result as a #pragma directive. */
1162 destringize_and_run (pfile, in)
1164 const cpp_string *in;
1166 const unsigned char *src, *limit;
1167 char *dest, *result;
1169 dest = result = alloca (in->len + 1);
1170 for (src = in->text, limit = src + in->len; src < limit;)
1172 /* We know there is a character following the backslash. */
1173 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1179 run_directive (pfile, T_PRAGMA, result, dest - result);
1182 /* Handle the _Pragma operator. */
1184 _cpp_do__Pragma (pfile)
1187 const cpp_token *string = get__Pragma_string (pfile);
1190 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1193 /* Ideally, we'd like
1194 token1 _Pragma ("foo") token2
1201 Getting these correct line markers is a little tricky. */
1203 unsigned int orig_line = pfile->line;
1204 destringize_and_run (pfile, &string->val.str);
1205 pfile->line = orig_line;
1206 pfile->buffer->saved_flags = BOL;
1210 /* Just ignore #sccs, on systems where we define it at all. */
1211 #ifdef SCCS_DIRECTIVE
1214 cpp_reader *pfile ATTRIBUTE_UNUSED;
1219 /* Handle #ifdef. */
1226 if (! pfile->state.skipping)
1228 const cpp_hashnode *node = lex_macro_node (pfile);
1231 skip = node->type != NT_MACRO;
1237 push_conditional (pfile, skip, T_IFDEF, 0);
1240 /* Handle #ifndef. */
1246 const cpp_hashnode *node = 0;
1248 if (! pfile->state.skipping)
1250 node = lex_macro_node (pfile);
1252 skip = node->type == NT_MACRO;
1258 push_conditional (pfile, skip, T_IFNDEF, node);
1261 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1262 pfile->mi_ind_cmacro so we can handle multiple-include
1263 optimisations. If macro expansion occurs in the expression, we
1264 cannot treat it as a controlling conditional, since the expansion
1265 could change in the future. That is handled by cpp_get_token. */
1272 if (! pfile->state.skipping)
1273 skip = _cpp_parse_expr (pfile) == 0;
1275 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1278 /* Flip skipping state if appropriate and continue without changing
1279 if_stack; this is so that the error message for missing #endif's
1280 etc. will point to the original #if. */
1285 cpp_buffer *buffer = pfile->buffer;
1286 struct if_stack *ifs = buffer->if_stack;
1289 cpp_error (pfile, "#else without #if");
1292 if (ifs->type == T_ELSE)
1294 cpp_error (pfile, "#else after #else");
1295 cpp_error_with_line (pfile, ifs->line, 0,
1296 "the conditional began here");
1300 /* Skip any future (erroneous) #elses or #elifs. */
1301 pfile->state.skipping = ifs->skip_elses;
1302 ifs->skip_elses = true;
1304 /* Invalidate any controlling macro. */
1307 /* Only check EOL if was not originally skipping. */
1308 if (!ifs->was_skipping)
1313 /* Handle a #elif directive by not changing if_stack either. See the
1314 comment above do_else. */
1319 cpp_buffer *buffer = pfile->buffer;
1320 struct if_stack *ifs = buffer->if_stack;
1323 cpp_error (pfile, "#elif without #if");
1326 if (ifs->type == T_ELSE)
1328 cpp_error (pfile, "#elif after #else");
1329 cpp_error_with_line (pfile, ifs->line, 0,
1330 "the conditional began here");
1334 /* Only evaluate this if we aren't skipping elses. During
1335 evaluation, set skipping to false to get lexer warnings. */
1336 if (ifs->skip_elses)
1337 pfile->state.skipping = 1;
1340 pfile->state.skipping = 0;
1341 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1342 ifs->skip_elses = ! pfile->state.skipping;
1345 /* Invalidate any controlling macro. */
1350 /* #endif pops the if stack and resets pfile->state.skipping. */
1355 cpp_buffer *buffer = pfile->buffer;
1356 struct if_stack *ifs = buffer->if_stack;
1359 cpp_error (pfile, "#endif without #if");
1362 /* Only check EOL if was not originally skipping. */
1363 if (!ifs->was_skipping)
1366 /* If potential control macro, we go back outside again. */
1367 if (ifs->next == 0 && ifs->mi_cmacro)
1369 pfile->mi_valid = true;
1370 pfile->mi_cmacro = ifs->mi_cmacro;
1373 buffer->if_stack = ifs->next;
1374 pfile->state.skipping = ifs->was_skipping;
1375 obstack_free (&pfile->buffer_ob, ifs);
1379 /* Push an if_stack entry for a preprocessor conditional, and set
1380 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1381 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1382 we need to check here that we are at the top of the file. */
1384 push_conditional (pfile, skip, type, cmacro)
1388 const cpp_hashnode *cmacro;
1390 struct if_stack *ifs;
1391 cpp_buffer *buffer = pfile->buffer;
1393 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1394 ifs->line = pfile->directive_line;
1395 ifs->next = buffer->if_stack;
1396 ifs->skip_elses = pfile->state.skipping || !skip;
1397 ifs->was_skipping = pfile->state.skipping;
1399 /* This condition is effectively a test for top-of-file. */
1400 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1401 ifs->mi_cmacro = cmacro;
1405 pfile->state.skipping = skip;
1406 buffer->if_stack = ifs;
1409 /* Read the tokens of the answer into the macro pool, in a directive
1410 of type TYPE. Only commit the memory if we intend it as permanent
1411 storage, i.e. the #assert case. Returns 0 on success, and sets
1412 ANSWERP to point to the answer. */
1414 parse_answer (pfile, answerp, type)
1416 struct answer **answerp;
1419 const cpp_token *paren;
1420 struct answer *answer;
1421 unsigned int acount;
1423 /* In a conditional, it is legal to not have an open paren. We
1424 should save the following token in this case. */
1425 paren = cpp_get_token (pfile);
1427 /* If not a paren, see if we're OK. */
1428 if (paren->type != CPP_OPEN_PAREN)
1430 /* In a conditional no answer is a test for any answer. It
1431 could be followed by any token. */
1434 _cpp_backup_tokens (pfile, 1);
1438 /* #unassert with no answer is valid - it removes all answers. */
1439 if (type == T_UNASSERT && paren->type == CPP_EOF)
1442 cpp_error (pfile, "missing '(' after predicate");
1446 for (acount = 0;; acount++)
1449 const cpp_token *token = cpp_get_token (pfile);
1452 if (token->type == CPP_CLOSE_PAREN)
1455 if (token->type == CPP_EOF)
1457 cpp_error (pfile, "missing ')' to complete answer");
1461 /* struct answer includes the space for one token. */
1462 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1464 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1465 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1467 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1470 /* Drop whitespace at start, for answer equivalence purposes. */
1472 dest->flags &= ~PREV_WHITE;
1477 cpp_error (pfile, "predicate's answer is empty");
1481 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1482 answer->count = acount;
1483 answer->next = NULL;
1489 /* Parses an assertion directive of type TYPE, returning a pointer to
1490 the hash node of the predicate, or 0 on error. If an answer was
1491 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1492 static cpp_hashnode *
1493 parse_assertion (pfile, answerp, type)
1495 struct answer **answerp;
1498 cpp_hashnode *result = 0;
1499 const cpp_token *predicate;
1501 /* We don't expand predicates or answers. */
1502 pfile->state.prevent_expansion++;
1505 predicate = cpp_get_token (pfile);
1506 if (predicate->type == CPP_EOF)
1507 cpp_error (pfile, "assertion without predicate");
1508 else if (predicate->type != CPP_NAME)
1509 cpp_error (pfile, "predicate must be an identifier");
1510 else if (parse_answer (pfile, answerp, type) == 0)
1512 unsigned int len = NODE_LEN (predicate->val.node);
1513 unsigned char *sym = alloca (len + 1);
1515 /* Prefix '#' to get it out of macro namespace. */
1517 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1518 result = cpp_lookup (pfile, sym, len + 1);
1521 pfile->state.prevent_expansion--;
1525 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1526 or a pointer to NULL if the answer is not in the chain. */
1527 static struct answer **
1528 find_answer (node, candidate)
1530 const struct answer *candidate;
1533 struct answer **result;
1535 for (result = &node->value.answers; *result; result = &(*result)->next)
1537 struct answer *answer = *result;
1539 if (answer->count == candidate->count)
1541 for (i = 0; i < answer->count; i++)
1542 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1545 if (i == answer->count)
1553 /* Test an assertion within a preprocessor conditional. Returns
1554 non-zero on failure, zero on success. On success, the result of
1555 the test is written into VALUE. */
1557 _cpp_test_assertion (pfile, value)
1561 struct answer *answer;
1564 node = parse_assertion (pfile, &answer, T_IF);
1566 *value = (node->type == NT_ASSERTION &&
1567 (answer == 0 || *find_answer (node, answer) != 0));
1569 /* We don't commit the memory for the answer - it's temporary only. */
1573 /* Handle #assert. */
1578 struct answer *new_answer;
1581 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1584 /* Place the new answer in the answer list. First check there
1585 is not a duplicate. */
1586 new_answer->next = 0;
1587 if (node->type == NT_ASSERTION)
1589 if (*find_answer (node, new_answer))
1591 cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
1594 new_answer->next = node->value.answers;
1597 node->type = NT_ASSERTION;
1598 node->value.answers = new_answer;
1599 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1600 + (new_answer->count - 1)
1601 * sizeof (cpp_token));
1606 /* Handle #unassert. */
1612 struct answer *answer;
1614 node = parse_assertion (pfile, &answer, T_UNASSERT);
1615 /* It isn't an error to #unassert something that isn't asserted. */
1616 if (node && node->type == NT_ASSERTION)
1620 struct answer **p = find_answer (node, answer), *temp;
1622 /* Remove the answer from the list. */
1627 /* Did we free the last answer? */
1628 if (node->value.answers == 0)
1629 node->type = NT_VOID;
1634 _cpp_free_definition (node);
1637 /* We don't commit the memory for the answer - it's temporary only. */
1640 /* These are for -D, -U, -A. */
1642 /* Process the string STR as if it appeared as the body of a #define.
1643 If STR is just an identifier, define it with value 1.
1644 If STR has anything after the identifier, then it should
1645 be identifier=definition. */
1647 cpp_define (pfile, str)
1654 /* Copy the entire option so we can modify it.
1655 Change the first "=" in the string to a space. If there is none,
1656 tack " 1" on the end. */
1658 count = strlen (str);
1659 buf = (char *) alloca (count + 3);
1660 memcpy (buf, str, count);
1662 p = strchr (str, '=');
1672 run_directive (pfile, T_DEFINE, buf, count);
1675 /* Slight variant of the above for use by initialize_builtins. */
1677 _cpp_define_builtin (pfile, str)
1681 run_directive (pfile, T_DEFINE, str, strlen (str));
1684 /* Process MACRO as if it appeared as the body of an #undef. */
1686 cpp_undef (pfile, macro)
1690 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1693 /* Process the string STR as if it appeared as the body of a #assert. */
1695 cpp_assert (pfile, str)
1699 handle_assertion (pfile, str, T_ASSERT);
1702 /* Process STR as if it appeared as the body of an #unassert. */
1704 cpp_unassert (pfile, str)
1708 handle_assertion (pfile, str, T_UNASSERT);
1711 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1713 handle_assertion (pfile, str, type)
1718 size_t count = strlen (str);
1719 const char *p = strchr (str, '=');
1723 /* Copy the entire option so we can modify it. Change the first
1724 "=" in the string to a '(', and tack a ')' on the end. */
1725 char *buf = (char *) alloca (count + 2);
1727 memcpy (buf, str, count);
1734 run_directive (pfile, type, str, count);
1737 /* The number of errors for a given reader. */
1742 return pfile->errors;
1745 /* The options structure. */
1747 cpp_get_options (pfile)
1750 return &pfile->opts;
1753 /* The callbacks structure. */
1755 cpp_get_callbacks (pfile)
1761 /* The line map set. */
1762 const struct line_maps *
1763 cpp_get_line_maps (pfile)
1766 return &pfile->line_maps;
1769 /* Copy the given callbacks structure to our own. */
1771 cpp_set_callbacks (pfile, cb)
1778 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1779 doesn't fail. It does not generate a file change call back; that
1780 is the responsibility of the caller. */
1782 cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
1784 const U_CHAR *buffer;
1789 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1791 /* Clears, amongst other things, if_stack and mi_cmacro. */
1792 memset (new, 0, sizeof (cpp_buffer));
1794 new->line_base = new->buf = new->cur = buffer;
1795 new->rlimit = buffer + len;
1796 new->from_stage3 = from_stage3;
1797 new->prev = pfile->buffer;
1798 new->return_at_eof = return_at_eof;
1799 new->saved_flags = BOL;
1801 pfile->buffer = new;
1806 /* If called from do_line, pops a single buffer. Otherwise pops all
1807 buffers until a real file is reached. Generates appropriate
1810 _cpp_pop_buffer (pfile)
1813 cpp_buffer *buffer = pfile->buffer;
1814 struct if_stack *ifs;
1815 bool pushed = false;
1817 /* Walk back up the conditional stack till we reach its level at
1818 entry to this file, issuing error messages. */
1819 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1820 cpp_error_with_line (pfile, ifs->line, 0,
1821 "unterminated #%s", dtable[ifs->type].name);
1823 /* In case of a missing #endif. */
1824 pfile->state.skipping = 0;
1826 /* Update the reader's buffer before _cpp_do_file_change. */
1827 pfile->buffer = buffer->prev;
1830 pushed = _cpp_pop_file_buffer (pfile, buffer->inc);
1833 obstack_free (&pfile->buffer_ob, buffer);
1836 /* Enter all recognised directives in the hash table. */
1838 _cpp_init_directives (pfile)
1844 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1846 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1847 node->directive_index = i + 1;