1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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). */
42 struct if_stack *next;
43 cpp_lexer_pos pos; /* line and column where condition started */
44 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
45 bool skip_elses; /* Can future #else / #elif be skipped? */
46 bool was_skipping; /* If were skipping on entry. */
47 int type; /* Most recent conditional, for diagnostics. */
50 /* Values for the origin field of struct directive. KANDR directives
51 come from traditional (K&R) C. STDC89 directives come from the
52 1989 C standard. EXTENSION directives are extensions. */
57 /* Values for the flags field of struct directive. COND indicates a
58 conditional; IF_COND an opening conditional. INCL means to treat
59 "..." and <...> as q-char and h-char sequences respectively. IN_I
60 means this directive should be handled even if -fpreprocessed is in
61 effect (these are the directives with callback hooks). */
63 #define IF_COND (1 << 1)
67 /* Defines one #-directive, including how to handle it. */
68 typedef void (*directive_handler) PARAMS ((cpp_reader *));
69 typedef struct directive directive;
72 directive_handler handler; /* Function to handle directive. */
73 const U_CHAR *name; /* Name of directive. */
74 unsigned short length; /* Length of name. */
75 unsigned char origin; /* Origin of directive. */
76 unsigned char flags; /* Flags describing this directive. */
79 /* Forward declarations. */
81 static void skip_rest_of_line PARAMS ((cpp_reader *));
82 static void check_eol PARAMS ((cpp_reader *));
83 static void start_directive PARAMS ((cpp_reader *));
84 static void end_directive PARAMS ((cpp_reader *, int));
85 static void run_directive PARAMS ((cpp_reader *, int,
86 const char *, size_t));
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 unsigned int read_flag PARAMS ((cpp_reader *, unsigned 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_include_common PARAMS ((cpp_reader *, enum include_type));
97 static void do_pragma_once PARAMS ((cpp_reader *));
98 static void do_pragma_poison PARAMS ((cpp_reader *));
99 static void do_pragma_system_header PARAMS ((cpp_reader *));
100 static void do_pragma_dependency PARAMS ((cpp_reader *));
101 static int get__Pragma_string PARAMS ((cpp_reader *, cpp_token *));
102 static unsigned char *destringize PARAMS ((const cpp_string *,
104 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
105 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
107 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
108 const struct answer *));
109 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
111 /* This is the table of directive handlers. It is ordered by
112 frequency of occurrence; the numbers at the end are directive
113 counts from all the source code I have lying around (egcs and libc
114 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
115 pcmcia-cs-3.0.9). This is no longer important as directive lookup
116 is now O(1). All extensions other than #warning and #include_next
117 are deprecated. The name is where the extension appears to have
120 #define DIRECTIVE_TABLE \
121 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
122 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
123 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
124 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
125 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
126 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
127 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
128 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
129 D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
130 D(elif, T_ELIF, STDC89, COND) /* 610 */ \
131 D(error, T_ERROR, STDC89, 0) /* 475 */ \
132 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
133 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
134 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
135 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
136 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
137 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
138 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
139 SCCS_ENTRY /* 0 SVR4? */
141 /* #sccs is not always recognized. */
142 #ifdef SCCS_DIRECTIVE
143 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
145 # define SCCS_ENTRY /* nothing */
148 /* Use the table to generate a series of prototypes, an enum for the
149 directive names, and an array of directive handlers. */
151 /* The directive-processing functions are declared to return int
152 instead of void, because some old compilers have trouble with
153 pointers to functions returning void. */
155 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
156 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
160 #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 #define SEEN_EOL() (pfile->lexer_pos.output_line > pfile->line)
181 /* Skip any remaining tokens in a directive. */
183 skip_rest_of_line (pfile)
188 /* Discard all input lookaheads. */
189 while (pfile->la_read)
190 _cpp_release_lookahead (pfile);
192 /* Discard all stacked contexts. */
193 while (pfile->context != &pfile->base_context)
194 _cpp_pop_context (pfile);
196 /* Sweep up all tokens remaining on the line. */
197 while (! SEEN_EOL ())
198 _cpp_lex_token (pfile, &token);
201 /* Ensure there are no stray tokens at the end of a directive. */
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 /* Called when entering a directive, _Pragma or command-line directive. */
219 start_directive (pfile)
222 /* Setup in-directive state. */
223 pfile->state.in_directive = 1;
224 pfile->state.save_comments = 0;
226 /* Some handlers need the position of the # for diagnostics. */
227 pfile->directive_pos = pfile->lexer_pos;
228 pfile->directive_pos.line = pfile->line;
229 pfile->directive_line = pfile->line;
231 /* Don't save directive tokens for external clients. */
232 pfile->la_saved = pfile->la_write;
236 /* Called when leaving a directive, _Pragma or command-line directive. */
238 end_directive (pfile, skip_line)
242 /* We don't skip for an assembler #. */
245 skip_rest_of_line (pfile);
246 /* "Accept" the newline now. */
251 pfile->la_write = pfile->la_saved;
252 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
253 pfile->state.in_directive = 0;
254 pfile->state.angled_headers = 0;
255 pfile->state.line_extension = 0;
256 pfile->directive = 0;
259 /* Check if a token's name matches that of a known directive. Put in
260 this file to save exporting dtable and other unneeded information. */
262 _cpp_handle_directive (pfile, indented)
266 const directive *dir = 0;
270 start_directive (pfile);
272 /* Lex the directive name directly. */
273 _cpp_lex_token (pfile, &dname);
275 if (dname.type == CPP_NAME)
277 unsigned int index = dname.val.node->directive_index;
279 dir = &dtable[index - 1];
281 else if (dname.type == CPP_NUMBER)
283 /* # followed by a number is equivalent to #line. Do not
284 recognize this form in assembly language source files or
285 skipped conditional groups. Complain about this form if
286 we're being pedantic, but not if this is regurgitated input
287 (preprocessed or fed back in by the C++ frontend). */
288 if (! pfile->state.skipping && CPP_OPTION (pfile, lang) != CLK_ASM)
290 dir = &dtable[T_LINE];
291 pfile->state.line_extension = 1;
292 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
293 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed))
294 cpp_pedwarn (pfile, "# followed by integer");
298 pfile->directive = dir;
301 /* Make sure we lex headers correctly, whether skipping or not. */
302 pfile->state.angled_headers = dir->flags & INCL;
304 /* If we are rescanning preprocessed input, only directives tagged
305 with IN_I are honored, and the warnings below are suppressed. */
306 if (CPP_OPTION (pfile, preprocessed))
308 /* Kluge alert. In order to be sure that code like this
311 does not cause '#define foo bar' to get executed when
312 compiled with -save-temps, we recognize directives in
313 -fpreprocessed mode only if the # is in column 1 and the
314 directive name starts in column 2. This output can only
315 be generated by the directive callbacks in cppmain.c (see
316 also the special case in scan_buffer). */
317 if (dir->flags & IN_I && !indented && !(dname.flags & PREV_WHITE))
318 (*dir->handler) (pfile);
319 /* That check misses '# 123' linemarkers. Let them through too. */
320 else if (dname.type == CPP_NUMBER)
321 (*dir->handler) (pfile);
324 /* We don't want to process this directive. Put back the
325 tokens so caller will see them (and issue an error,
327 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
333 /* Traditionally, a directive is ignored unless its # is in
334 column 1. Therefore in code intended to work with K+R
335 compilers, directives added by C89 must have their #
336 indented, and directives present in traditional C must
337 not. This is true even of directives in skipped
338 conditional blocks. */
339 if (CPP_WTRADITIONAL (pfile))
341 if (dir == &dtable[T_ELIF])
343 "suggest not using #elif in traditional C");
344 else if (indented && dir->origin == KANDR)
346 "traditional C ignores #%s with the # indented",
348 else if (!indented && dir->origin != KANDR)
350 "suggest hiding #%s from traditional C with an indented #",
354 /* If we are skipping a failed conditional group, all
355 non-conditional directives are ignored. */
356 if (! pfile->state.skipping || (dir->flags & COND))
358 /* Issue -pedantic warnings for extensions. */
359 if (CPP_PEDANTIC (pfile) && dir->origin == EXTENSION)
360 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
362 /* If we have a directive that is not an opening
363 conditional, invalidate any control macro. */
364 if (! (dir->flags & IF_COND))
365 pfile->mi_valid = false;
367 (*dir->handler) (pfile);
371 else if (dname.type != CPP_EOF && ! pfile->state.skipping)
373 /* An unknown directive. Don't complain about it in assembly
374 source: we don't know where the comments are, and # may
375 introduce assembler pseudo-ops. Don't complain about invalid
376 directives in skipped conditional groups (6.10 p4). */
377 if (CPP_OPTION (pfile, lang) == CLK_ASM)
379 /* Output the # and lookahead token for the assembler. */
380 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
384 cpp_error (pfile, "invalid preprocessing directive #%s",
385 cpp_token_as_text (pfile, &dname));
388 if (pfile->state.in_directive)
389 end_directive (pfile, skip);
393 /* Directive handler wrapper used by the command line option
396 run_directive (pfile, dir_no, buf, count)
402 cpp_push_buffer (pfile, (const U_CHAR *) buf, count,
403 /* from_stage3 */ true, 1);
404 start_directive (pfile);
405 pfile->state.prevent_expansion++;
406 pfile->directive = &dtable[dir_no];
407 (void) (*pfile->directive->handler) (pfile);
408 pfile->state.prevent_expansion--;
410 end_directive (pfile, 1);
411 _cpp_pop_buffer (pfile);
414 /* Checks for validity the macro name in #define, #undef, #ifdef and
415 #ifndef directives. */
416 static cpp_hashnode *
417 lex_macro_node (pfile)
423 /* Lex the macro name directly. */
424 _cpp_lex_token (pfile, &token);
426 /* The token immediately after #define must be an identifier. That
427 identifier may not be "defined", per C99 6.10.8p4.
428 In C++, it may not be any of the "named operators" either,
429 per C++98 [lex.digraph], [lex.key].
430 Finally, the identifier may not have been poisoned. (In that case
431 the lexer has issued the error message for us.) */
433 if (token.type != CPP_NAME)
435 if (token.type == CPP_EOF)
436 cpp_error (pfile, "no macro name given in #%s directive",
437 pfile->directive->name);
438 else if (token.flags & NAMED_OP)
440 "\"%s\" cannot be used as a macro name as it is an operator in C++",
441 NODE_NAME (token.val.node));
443 cpp_error (pfile, "macro names must be identifiers");
448 node = token.val.node;
449 if (node->flags & NODE_POISONED)
452 if (node == pfile->spec_nodes.n_defined)
454 cpp_error (pfile, "\"%s\" cannot be used as a macro name",
462 /* Process a #define directive. Most work is done in cppmacro.c. */
467 cpp_hashnode *node = lex_macro_node (pfile);
471 if (_cpp_create_definition (pfile, node))
472 if (pfile->cb.define)
473 (*pfile->cb.define) (pfile, pfile->directive_line, node);
477 /* Handle #undef. Marks the identifier NT_VOID in the hash table. */
482 cpp_hashnode *node = lex_macro_node (pfile);
484 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
485 is not currently defined as a macro name. */
486 if (node && node->type == NT_MACRO)
489 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
491 if (node->flags & NODE_WARN)
492 cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node));
494 _cpp_free_definition (node);
499 /* Helper routine used by parse_include. Reinterpret the current line
500 as an h-char-sequence (< ... >); we are looking at the first token
501 after the <. Returns zero on success. */
503 glue_header_name (pfile, header)
508 unsigned char *buffer, *token_mem;
509 size_t len, total_len = 0, capacity = 1024;
511 /* To avoid lexed tokens overwriting our glued name, we can only
512 allocate from the string pool once we've lexed everything. */
514 buffer = (unsigned char *) xmalloc (capacity);
517 cpp_get_token (pfile, &token);
519 if (token.type == CPP_GREATER || token.type == CPP_EOF)
522 len = cpp_token_len (&token);
523 if (total_len + len > capacity)
525 capacity = (capacity + len) * 2;
526 buffer = (unsigned char *) xrealloc (buffer, capacity);
529 if (token.flags & PREV_WHITE)
530 buffer[total_len++] = ' ';
532 total_len = cpp_spell_token (pfile, &token, &buffer[total_len]) - buffer;
535 if (token.type == CPP_EOF)
536 cpp_error (pfile, "missing terminating > character");
539 token_mem = _cpp_pool_alloc (&pfile->ident_pool, total_len + 1);
540 memcpy (token_mem, buffer, total_len);
541 token_mem[total_len] = '\0';
543 header->type = CPP_HEADER_NAME;
544 header->flags &= ~PREV_WHITE;
545 header->val.str.len = total_len;
546 header->val.str.text = token_mem;
550 return token.type == CPP_EOF;
553 /* Parse the header name of #include, #include_next, #import and
554 #pragma dependency. Returns zero on success. */
556 parse_include (pfile, header)
560 const unsigned char *dir;
562 if (pfile->directive == &dtable[T_PRAGMA])
563 dir = U"pragma dependency";
565 dir = pfile->directive->name;
567 /* Allow macro expansion. */
568 cpp_get_token (pfile, header);
569 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
571 if (header->type != CPP_LESS)
573 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
576 if (glue_header_name (pfile, header))
580 if (header->val.str.len == 0)
582 cpp_error (pfile, "empty file name in #%s", dir);
589 /* Handle #include, #include_next and #import. */
591 do_include_common (pfile, type)
593 enum include_type type;
597 /* For #include_next, if this is the primary source file, warn and
598 use the normal search logic. */
599 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
601 cpp_warning (pfile, "#include_next in primary source file");
604 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
606 CPP_OPTION (pfile, warn_import) = 0;
608 "#import is obsolete, use an #ifndef wrapper in the header file");
611 if (!parse_include (pfile, &header))
613 /* Prevent #include recursion. */
614 if (pfile->line_maps.depth >= CPP_STACK_MAX)
615 cpp_fatal (pfile, "#include nested too deeply");
619 /* Get out of macro context, if we are. */
620 end_directive (pfile, 1);
621 if (pfile->cb.include)
622 (*pfile->cb.include) (pfile, pfile->directive_line,
623 pfile->directive->name, &header);
625 _cpp_execute_include (pfile, &header, type);
634 do_include_common (pfile, IT_INCLUDE);
641 do_include_common (pfile, IT_IMPORT);
645 do_include_next (pfile)
648 do_include_common (pfile, IT_INCLUDE_NEXT);
651 /* Subroutine of do_line. Read possible flags after file name. LAST
652 is the last flag seen; 0 if this is the first flag. Return the flag
653 if it is valid, 0 at the end of the directive. Otherwise complain. */
656 read_flag (pfile, last)
662 _cpp_lex_token (pfile, &token);
663 if (token.type == CPP_NUMBER && token.val.str.len == 1)
665 unsigned int flag = token.val.str.text[0] - '0';
667 if (flag > last && flag <= 4
668 && (flag != 4 || last == 3)
669 && (flag != 2 || last == 0))
673 if (token.type != CPP_EOF)
674 cpp_error (pfile, "invalid flag \"%s\" in line directive",
675 cpp_token_as_text (pfile, &token));
679 /* Another subroutine of do_line. Convert a number in STR, of length
680 LEN, to binary; store it in NUMP, and return 0 if the number was
681 well-formed, 1 if not. Temporary, hopefully. */
683 strtoul_for_line (str, len, nump)
688 unsigned long reg = 0;
702 /* Interpret #line command.
703 Note that the filename string (if any) is treated as if it were an
704 include filename. That means no escape handling. */
711 const char *new_file = pfile->map->to_file;
712 unsigned long new_lineno;
713 unsigned int cap, new_sysp = pfile->map->sysp;
714 enum lc_reason reason = LC_RENAME;
716 /* C99 raised the minimum limit on #line numbers. */
717 cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
719 /* #line commands expand macros. */
720 cpp_get_token (pfile, &token);
721 if (token.type != CPP_NUMBER
722 || strtoul_for_line (token.val.str.text, token.val.str.len, &new_lineno))
724 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
725 cpp_token_as_text (pfile, &token));
729 if (CPP_PEDANTIC (pfile) && ! pfile->state.line_extension
730 && (new_lineno == 0 || new_lineno > cap))
731 cpp_pedwarn (pfile, "line number out of range");
733 cpp_get_token (pfile, &token);
734 if (token.type == CPP_STRING)
736 new_file = (const char *) token.val.str.text;
738 /* Only accept flags for the # 55 form. */
739 if (pfile->state.line_extension)
744 flag = read_flag (pfile, 0);
748 /* Fake an include for cpp_included (). */
749 _cpp_fake_include (pfile, new_file);
750 flag = read_flag (pfile, flag);
755 flag = read_flag (pfile, flag);
760 flag = read_flag (pfile, flag);
767 else if (token.type != CPP_EOF)
769 cpp_error (pfile, "\"%s\" is not a valid filename",
770 cpp_token_as_text (pfile, &token));
774 end_directive (pfile, 1);
775 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
778 /* Arrange the file_change callback. pfile->line has changed to
779 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
780 header, 2 for a sytem header that needs to be extern "C" protected,
781 and zero otherwise. */
783 _cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
785 enum lc_reason reason;
787 unsigned int file_line;
790 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
791 pfile->line, to_file, file_line);
793 if (pfile->cb.file_change)
794 (*pfile->cb.file_change) (pfile, pfile->map);
798 * Report a warning or error detected by the program we are
799 * processing. Use the directive's tokens in the error message.
803 do_diagnostic (pfile, code, print_dir)
805 enum error_type code;
808 if (_cpp_begin_message (pfile, code, 0))
811 fprintf (stderr, "#%s ", pfile->directive->name);
812 pfile->state.prevent_expansion++;
813 cpp_output_line (pfile, stderr);
814 pfile->state.prevent_expansion--;
822 do_diagnostic (pfile, ERROR, 1);
829 /* We want #warning diagnostics to be emitted in system headers too. */
830 do_diagnostic (pfile, WARNING_SYSHDR, 1);
833 /* Report program identification. */
841 cpp_get_token (pfile, &str);
842 if (str.type != CPP_STRING)
843 cpp_error (pfile, "invalid #ident");
844 else if (pfile->cb.ident)
845 (*pfile->cb.ident) (pfile, pfile->directive_line, &str.val.str);
850 /* Pragmata handling. We handle some of these, and pass the rest on
851 to the front end. C99 defines three pragmas and says that no macro
852 expansion is to be performed on them; whether or not macro
853 expansion happens for other pragmas is implementation defined.
854 This implementation never macro-expands the text after #pragma. */
856 /* Sub-handlers for the pragmas needing treatment here.
857 They return 1 if the token buffer is to be popped, 0 if not. */
858 typedef void (*pragma_cb) PARAMS ((cpp_reader *));
861 struct pragma_entry *next;
867 struct pragma_entry *space;
872 cpp_register_pragma (pfile, space, name, handler)
878 struct pragma_entry **x, *new;
884 struct pragma_entry *p = pfile->pragmas;
885 len = strlen (space);
888 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
895 cpp_ice (pfile, "unknown #pragma namespace %s", space);
900 new = (struct pragma_entry *)
901 _cpp_pool_alloc (&pfile->macro_pool, sizeof (struct pragma_entry));
903 new->len = strlen (name);
905 new->u.handler = handler;
912 cpp_register_pragma_space (pfile, space)
916 struct pragma_entry *new;
917 const struct pragma_entry *p = pfile->pragmas;
918 size_t len = strlen (space);
922 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
923 /* Multiple different callers are allowed to register the same
929 new = (struct pragma_entry *)
930 _cpp_pool_alloc (&pfile->macro_pool, sizeof (struct pragma_entry));
936 new->next = pfile->pragmas;
937 pfile->pragmas = new;
941 _cpp_init_internal_pragmas (pfile)
945 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
946 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
949 cpp_register_pragma_space (pfile, "GCC");
951 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
952 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
953 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
960 pragma_cb handler = NULL;
961 const struct pragma_entry *p;
965 pfile->state.prevent_expansion++;
966 cpp_start_lookahead (pfile);
969 cpp_get_token (pfile, &tok);
970 if (tok.type == CPP_NAME)
972 const cpp_hashnode *node = tok.val.node;
973 size_t len = NODE_LEN (node);
977 if (strlen (p->name) == len
978 && !memcmp (p->name, NODE_NAME (node), len))
987 handler = p->u.handler;
995 cpp_stop_lookahead (pfile, handler != NULL);
996 pfile->state.prevent_expansion--;
1000 else if (pfile->cb.def_pragma)
1001 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1005 do_pragma_once (pfile)
1008 cpp_warning (pfile, "#pragma once is obsolete");
1010 if (pfile->buffer->prev == NULL)
1011 cpp_warning (pfile, "#pragma once in main file");
1013 _cpp_never_reread (pfile->buffer->inc);
1019 do_pragma_poison (pfile)
1022 /* Poison these symbols so that all subsequent usage produces an
1027 pfile->state.poisoned_ok = 1;
1030 _cpp_lex_token (pfile, &tok);
1031 if (tok.type == CPP_EOF)
1033 if (tok.type != CPP_NAME)
1035 cpp_error (pfile, "invalid #pragma GCC poison directive");
1040 if (hp->flags & NODE_POISONED)
1043 if (hp->type == NT_MACRO)
1044 cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp));
1045 _cpp_free_definition (hp);
1046 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1048 pfile->state.poisoned_ok = 0;
1051 /* Mark the current header as a system header. This will suppress
1052 some categories of warnings (notably those from -pedantic). It is
1053 intended for use in system libraries that cannot be implemented in
1054 conforming C, but cannot be certain that their headers appear in a
1055 system include directory. To prevent abuse, it is rejected in the
1056 primary source file. */
1058 do_pragma_system_header (pfile)
1061 cpp_buffer *buffer = pfile->buffer;
1063 if (buffer->prev == 0)
1064 cpp_warning (pfile, "#pragma system_header ignored outside include file");
1068 end_directive (pfile, 1);
1069 cpp_make_system_header (pfile, 1, 0);
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;
1152 cpp_lexer_pos orig_pos;
1154 orig_pos = pfile->lexer_pos;
1155 if (get__Pragma_string (pfile, &string))
1156 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);
1161 free ((PTR) buffer);
1162 pfile->lexer_pos = orig_pos;
1163 pfile->line = pfile->lexer_pos.line;
1167 /* Just ignore #sccs, on systems where we define it at all. */
1168 #ifdef SCCS_DIRECTIVE
1171 cpp_reader *pfile ATTRIBUTE_UNUSED;
1182 if (! pfile->state.skipping)
1184 const cpp_hashnode *node = lex_macro_node (pfile);
1187 skip = node->type != NT_MACRO;
1193 push_conditional (pfile, skip, T_IFDEF, 0);
1201 const cpp_hashnode *node = 0;
1203 if (! pfile->state.skipping)
1205 node = lex_macro_node (pfile);
1207 skip = node->type == NT_MACRO;
1213 push_conditional (pfile, skip, T_IFNDEF, node);
1216 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1217 pfile->mi_ind_cmacro so we can handle multiple-include
1218 optimisations. If macro expansion occurs in the expression, we
1219 cannot treat it as a controlling conditional, since the expansion
1220 could change in the future. That is handled by cpp_get_token. */
1228 if (! pfile->state.skipping)
1229 skip = _cpp_parse_expr (pfile) == 0;
1231 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1234 /* Flip skipping state if appropriate and continue without changing
1235 if_stack; this is so that the error message for missing #endif's
1236 etc. will point to the original #if. */
1242 cpp_buffer *buffer = pfile->buffer;
1243 struct if_stack *ifs = buffer->if_stack;
1246 cpp_error (pfile, "#else without #if");
1249 if (ifs->type == T_ELSE)
1251 cpp_error (pfile, "#else after #else");
1252 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1253 "the conditional began here");
1257 /* Skip any future (erroneous) #elses or #elifs. */
1258 pfile->state.skipping = ifs->skip_elses;
1259 ifs->skip_elses = true;
1261 /* Invalidate any controlling macro. */
1264 /* Only check EOL if was not originally skipping. */
1265 if (!ifs->was_skipping)
1270 /* handle a #elif directive by not changing if_stack either. see the
1271 comment above do_else. */
1277 cpp_buffer *buffer = pfile->buffer;
1278 struct if_stack *ifs = buffer->if_stack;
1281 cpp_error (pfile, "#elif without #if");
1284 if (ifs->type == T_ELSE)
1286 cpp_error (pfile, "#elif after #else");
1287 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1288 "the conditional began here");
1292 /* Only evaluate this if we aren't skipping elses. During
1293 evaluation, set skipping to false to get lexer warnings. */
1294 if (ifs->skip_elses)
1295 pfile->state.skipping = 1;
1298 pfile->state.skipping = 0;
1299 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1300 ifs->skip_elses = ! pfile->state.skipping;
1303 /* Invalidate any controlling macro. */
1308 /* #endif pops the if stack and resets pfile->state.skipping. */
1314 cpp_buffer *buffer = pfile->buffer;
1315 struct if_stack *ifs = buffer->if_stack;
1318 cpp_error (pfile, "#endif without #if");
1321 /* Only check EOL if was not originally skipping. */
1322 if (!ifs->was_skipping)
1325 /* If potential control macro, we go back outside again. */
1326 if (ifs->next == 0 && ifs->mi_cmacro)
1328 pfile->mi_valid = true;
1329 pfile->mi_cmacro = ifs->mi_cmacro;
1332 buffer->if_stack = ifs->next;
1333 pfile->state.skipping = ifs->was_skipping;
1334 obstack_free (&pfile->buffer_ob, ifs);
1338 /* Push an if_stack entry and set pfile->state.skipping accordingly.
1339 If this is a #if or #ifndef, CMACRO is a potentially controlling
1340 macro - we need to check here that we are at the top of the file. */
1343 push_conditional (pfile, skip, type, cmacro)
1347 const cpp_hashnode *cmacro;
1349 struct if_stack *ifs;
1350 cpp_buffer *buffer = pfile->buffer;
1352 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1353 ifs->pos = pfile->directive_pos;
1354 ifs->next = buffer->if_stack;
1355 ifs->skip_elses = pfile->state.skipping || !skip;
1356 ifs->was_skipping = pfile->state.skipping;
1358 /* This condition is effectively a test for top-of-file. */
1359 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1360 ifs->mi_cmacro = cmacro;
1364 pfile->state.skipping = skip;
1365 buffer->if_stack = ifs;
1368 /* Read the tokens of the answer into the macro pool. Only commit the
1369 memory if we intend it as permanent storage, i.e. the #assert case.
1370 Returns 0 on success. */
1373 parse_answer (pfile, answerp, type)
1375 struct answer **answerp;
1378 cpp_token paren, *token;
1379 struct answer *answer;
1381 if (POOL_FRONT (&pfile->macro_pool) + sizeof (struct answer) >
1382 POOL_LIMIT (&pfile->macro_pool))
1383 _cpp_next_chunk (&pfile->macro_pool, sizeof (struct answer), 0);
1384 answer = (struct answer *) POOL_FRONT (&pfile->macro_pool);
1387 /* In a conditional, it is legal to not have an open paren. We
1388 should save the following token in this case. */
1390 cpp_start_lookahead (pfile);
1391 cpp_get_token (pfile, &paren);
1393 cpp_stop_lookahead (pfile, paren.type == CPP_OPEN_PAREN);
1395 /* If not a paren, see if we're OK. */
1396 if (paren.type != CPP_OPEN_PAREN)
1398 /* In a conditional no answer is a test for any answer. It
1399 could be followed by any token. */
1403 /* #unassert with no answer is valid - it removes all answers. */
1404 if (type == T_UNASSERT && paren.type == CPP_EOF)
1407 cpp_error (pfile, "missing '(' after predicate");
1413 token = &answer->first[answer->count];
1414 /* Check we have room for the token. */
1415 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1417 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1418 (unsigned char **) &answer);
1419 token = &answer->first[answer->count];
1422 cpp_get_token (pfile, token);
1423 if (token->type == CPP_CLOSE_PAREN)
1426 if (token->type == CPP_EOF)
1428 cpp_error (pfile, "missing ')' to complete answer");
1434 if (answer->count == 0)
1436 cpp_error (pfile, "predicate's answer is empty");
1440 /* Drop whitespace at start. */
1441 answer->first->flags &= ~PREV_WHITE;
1444 if (type == T_ASSERT || type == T_UNASSERT)
1449 /* Parses an assertion, returning a pointer to the hash node of the
1450 predicate, or 0 on error. If an answer was supplied, it is placed
1451 in ANSWERP, otherwise it is set to 0. */
1452 static cpp_hashnode *
1453 parse_assertion (pfile, answerp, type)
1455 struct answer **answerp;
1458 cpp_hashnode *result = 0;
1459 cpp_token predicate;
1461 /* We don't expand predicates or answers. */
1462 pfile->state.prevent_expansion++;
1465 cpp_get_token (pfile, &predicate);
1466 if (predicate.type == CPP_EOF)
1467 cpp_error (pfile, "assertion without predicate");
1468 else if (predicate.type != CPP_NAME)
1469 cpp_error (pfile, "predicate must be an identifier");
1470 else if (parse_answer (pfile, answerp, type) == 0)
1472 unsigned int len = NODE_LEN (predicate.val.node);
1473 unsigned char *sym = alloca (len + 1);
1475 /* Prefix '#' to get it out of macro namespace. */
1477 memcpy (sym + 1, NODE_NAME (predicate.val.node), len);
1478 result = cpp_lookup (pfile, sym, len + 1);
1481 pfile->state.prevent_expansion--;
1485 /* Returns a pointer to the pointer to the answer in the answer chain,
1486 or a pointer to NULL if the answer is not in the chain. */
1487 static struct answer **
1488 find_answer (node, candidate)
1490 const struct answer *candidate;
1493 struct answer **result;
1495 for (result = &node->value.answers; *result; result = &(*result)->next)
1497 struct answer *answer = *result;
1499 if (answer->count == candidate->count)
1501 for (i = 0; i < answer->count; i++)
1502 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1505 if (i == answer->count)
1513 /* Test an assertion within a preprocessor conditional. Returns
1514 non-zero on failure, zero on success. On success, the result of
1515 the test is written into VALUE. */
1517 _cpp_test_assertion (pfile, value)
1521 struct answer *answer;
1524 node = parse_assertion (pfile, &answer, T_IF);
1526 *value = (node->type == NT_ASSERTION &&
1527 (answer == 0 || *find_answer (node, answer) != 0));
1529 /* We don't commit the memory for the answer - it's temporary only. */
1537 struct answer *new_answer;
1540 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1543 /* Place the new answer in the answer list. First check there
1544 is not a duplicate. */
1545 new_answer->next = 0;
1546 if (node->type == NT_ASSERTION)
1548 if (*find_answer (node, new_answer))
1550 cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1);
1553 new_answer->next = node->value.answers;
1555 node->type = NT_ASSERTION;
1556 node->value.answers = new_answer;
1557 POOL_COMMIT (&pfile->macro_pool, (sizeof (struct answer)
1558 + (new_answer->count - 1)
1559 * sizeof (cpp_token)));
1568 struct answer *answer;
1570 node = parse_assertion (pfile, &answer, T_UNASSERT);
1571 /* It isn't an error to #unassert something that isn't asserted. */
1572 if (node && node->type == NT_ASSERTION)
1576 struct answer **p = find_answer (node, answer), *temp;
1578 /* Remove the answer from the list. */
1583 /* Did we free the last answer? */
1584 if (node->value.answers == 0)
1585 node->type = NT_VOID;
1588 _cpp_free_definition (node);
1591 /* We don't commit the memory for the answer - it's temporary only. */
1594 /* These are for -D, -U, -A. */
1596 /* Process the string STR as if it appeared as the body of a #define.
1597 If STR is just an identifier, define it with value 1.
1598 If STR has anything after the identifier, then it should
1599 be identifier=definition. */
1602 cpp_define (pfile, str)
1609 /* Copy the entire option so we can modify it.
1610 Change the first "=" in the string to a space. If there is none,
1611 tack " 1" on the end. */
1613 /* Length including the null. */
1614 count = strlen (str);
1615 buf = (char *) alloca (count + 2);
1616 memcpy (buf, str, count);
1618 p = strchr (str, '=');
1627 run_directive (pfile, T_DEFINE, buf, count);
1630 /* Slight variant of the above for use by initialize_builtins. */
1632 _cpp_define_builtin (pfile, str)
1636 run_directive (pfile, T_DEFINE, str, strlen (str));
1639 /* Process MACRO as if it appeared as the body of an #undef. */
1641 cpp_undef (pfile, macro)
1645 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1648 /* Process the string STR as if it appeared as the body of a #assert. */
1650 cpp_assert (pfile, str)
1654 handle_assertion (pfile, str, T_ASSERT);
1657 /* Process STR as if it appeared as the body of an #unassert. */
1659 cpp_unassert (pfile, str)
1663 handle_assertion (pfile, str, T_UNASSERT);
1666 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1668 handle_assertion (pfile, str, type)
1673 size_t count = strlen (str);
1674 const char *p = strchr (str, '=');
1678 /* Copy the entire option so we can modify it. Change the first
1679 "=" in the string to a '(', and tack a ')' on the end. */
1680 char *buf = (char *) alloca (count + 1);
1682 memcpy (buf, str, count);
1688 run_directive (pfile, type, str, count);
1691 /* The number of errors for a given reader. */
1696 return pfile->errors;
1699 /* The options structure. */
1701 cpp_get_options (pfile)
1704 return &pfile->opts;
1707 /* The callbacks structure. */
1709 cpp_get_callbacks (pfile)
1715 /* The line map set. */
1716 const struct line_maps *
1717 cpp_get_line_maps (pfile)
1720 return &pfile->line_maps;
1723 /* Copy the given callbacks structure to our own. */
1725 cpp_set_callbacks (pfile, cb)
1732 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1733 doesn't fail. It does not generate a file change call back; that
1734 is the responsibility of the caller. */
1736 cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
1738 const U_CHAR *buffer;
1743 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1745 /* Clears, amongst other things, if_stack and mi_cmacro. */
1746 memset (new, 0, sizeof (cpp_buffer));
1748 new->line_base = new->buf = new->cur = buffer;
1749 new->rlimit = buffer + len;
1751 /* No read ahead or extra char initially. */
1752 new->read_ahead = EOF;
1753 new->extra_char = EOF;
1754 new->from_stage3 = from_stage3;
1755 new->prev = pfile->buffer;
1756 new->return_at_eof = return_at_eof;
1758 pfile->buffer = new;
1763 /* If called from do_line, pops a single buffer. Otherwise pops all
1764 buffers until a real file is reached. Generates appropriate
1767 _cpp_pop_buffer (pfile)
1770 cpp_buffer *buffer = pfile->buffer;
1771 struct if_stack *ifs;
1773 /* Walk back up the conditional stack till we reach its level at
1774 entry to this file, issuing error messages. */
1775 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1776 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1777 "unterminated #%s", dtable[ifs->type].name);
1779 /* The output line can fall out of sync if we missed the final
1780 newline from the previous buffer, for example because of an
1781 unterminated comment. Similarly, skipping needs to be cleared in
1782 case of a missing #endif. */
1783 pfile->lexer_pos.output_line = pfile->line;
1784 pfile->state.skipping = 0;
1786 /* Update the reader's buffer before _cpp_do_file_change. */
1787 pfile->buffer = buffer->prev;
1790 _cpp_pop_file_buffer (pfile, buffer->inc);
1792 obstack_free (&pfile->buffer_ob, buffer);
1796 _cpp_init_directives (pfile)
1802 /* Register the directives. */
1803 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1805 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1806 node->directive_index = i + 1;