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 unsigned int line
; /* Line 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 directive_diagnostics
86 PARAMS ((cpp_reader
*, const directive
*, int));
87 static void run_directive
PARAMS ((cpp_reader
*, int,
88 const char *, size_t));
89 static int glue_header_name
PARAMS ((cpp_reader
*, cpp_token
*));
90 static int parse_include
PARAMS ((cpp_reader
*, cpp_token
*));
91 static void push_conditional
PARAMS ((cpp_reader
*, int, int,
92 const cpp_hashnode
*));
93 static unsigned int read_flag
PARAMS ((cpp_reader
*, unsigned int));
94 static int strtoul_for_line
PARAMS ((const U_CHAR
*, unsigned int,
96 static void do_diagnostic
PARAMS ((cpp_reader
*, enum error_type
, int));
97 static cpp_hashnode
*lex_macro_node
PARAMS ((cpp_reader
*));
98 static void do_include_common
PARAMS ((cpp_reader
*, enum include_type
));
99 static void do_pragma_once
PARAMS ((cpp_reader
*));
100 static void do_pragma_poison
PARAMS ((cpp_reader
*));
101 static void do_pragma_system_header
PARAMS ((cpp_reader
*));
102 static void do_pragma_dependency
PARAMS ((cpp_reader
*));
103 static int get__Pragma_string
PARAMS ((cpp_reader
*, cpp_token
*));
104 static unsigned char *destringize
PARAMS ((const cpp_string
*,
106 static int parse_answer
PARAMS ((cpp_reader
*, struct answer
**, int));
107 static cpp_hashnode
*parse_assertion
PARAMS ((cpp_reader
*, struct answer
**,
109 static struct answer
** find_answer
PARAMS ((cpp_hashnode
*,
110 const struct answer
*));
111 static void handle_assertion
PARAMS ((cpp_reader
*, const char *, int));
113 /* This is the table of directive handlers. It is ordered by
114 frequency of occurrence; the numbers at the end are directive
115 counts from all the source code I have lying around (egcs and libc
116 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
117 pcmcia-cs-3.0.9). This is no longer important as directive lookup
118 is now O(1). All extensions other than #warning and #include_next
119 are deprecated. The name is where the extension appears to have
122 #define DIRECTIVE_TABLE \
123 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
124 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
125 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
126 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
127 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
128 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
129 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
130 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
131 D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
132 D(elif, T_ELIF, STDC89, COND) /* 610 */ \
133 D(error, T_ERROR, STDC89, 0) /* 475 */ \
134 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
135 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
136 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
137 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
138 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
139 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
140 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
141 SCCS_ENTRY /* 0 SVR4? */
143 /* #sccs is not always recognized. */
144 #ifdef SCCS_DIRECTIVE
145 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
147 # define SCCS_ENTRY /* nothing */
150 /* Use the table to generate a series of prototypes, an enum for the
151 directive names, and an array of directive handlers. */
153 /* The directive-processing functions are declared to return int
154 instead of void, because some old compilers have trouble with
155 pointers to functions returning void. */
157 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
158 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
162 #define D(n, tag, o, f) tag,
170 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
171 #define D(name, t, origin, flags) \
172 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
173 sizeof STRINGX(name) - 1, origin, flags },
174 static const directive dtable
[] =
179 #undef DIRECTIVE_TABLE
181 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
183 /* Skip any remaining tokens in a directive. */
185 skip_rest_of_line (pfile
)
188 /* Discard all stacked contexts. */
189 while (pfile
->context
!= &pfile
->base_context
)
190 _cpp_pop_context (pfile
);
192 /* Sweep up all tokens remaining on the line. */
194 while (_cpp_lex_token (pfile
)->type
!= CPP_EOF
)
198 /* Ensure there are no stray tokens at the end of a directive. */
203 if (! SEEN_EOL () && _cpp_lex_token (pfile
)->type
!= CPP_EOF
)
204 cpp_pedwarn (pfile
, "extra tokens at end of #%s directive",
205 pfile
->directive
->name
);
208 /* Called when entering a directive, _Pragma or command-line directive. */
210 start_directive (pfile
)
213 /* Setup in-directive state. */
214 pfile
->state
.in_directive
= 1;
215 pfile
->state
.save_comments
= 0;
217 /* Some handlers need the position of the # for diagnostics. */
218 pfile
->directive_line
= pfile
->line
;
221 /* Called when leaving a directive, _Pragma or command-line directive. */
223 end_directive (pfile
, skip_line
)
227 /* We don't skip for an assembler #. */
230 skip_rest_of_line (pfile
);
231 if (!pfile
->keep_tokens
)
233 pfile
->cur_run
= &pfile
->base_run
;
234 pfile
->cur_token
= pfile
->base_run
.base
;
239 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
240 pfile
->state
.in_directive
= 0;
241 pfile
->state
.angled_headers
= 0;
242 pfile
->state
.line_extension
= 0;
243 pfile
->directive
= 0;
246 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
247 the '#' was indented. */
250 directive_diagnostics (pfile
, dir
, indented
)
252 const directive
*dir
;
255 if (pfile
->state
.line_extension
)
257 if (CPP_PEDANTIC (pfile
)
258 && ! pfile
->state
.skipping
)
259 cpp_pedwarn (pfile
, "style of line directive is a GCC extension");
263 /* Issue -pedantic warnings for extensions. */
264 if (CPP_PEDANTIC (pfile
)
265 && ! pfile
->state
.skipping
266 && dir
->origin
== EXTENSION
)
267 cpp_pedwarn (pfile
, "#%s is a GCC extension", dir
->name
);
269 /* Traditionally, a directive is ignored unless its # is in
270 column 1. Therefore in code intended to work with K+R
271 compilers, directives added by C89 must have their #
272 indented, and directives present in traditional C must not.
273 This is true even of directives in skipped conditional
275 if (CPP_WTRADITIONAL (pfile
))
277 if (dir
== &dtable
[T_ELIF
])
278 cpp_warning (pfile
, "suggest not using #elif in traditional C");
279 else if (indented
&& dir
->origin
== KANDR
)
281 "traditional C ignores #%s with the # indented",
283 else if (!indented
&& dir
->origin
!= KANDR
)
285 "suggest hiding #%s from traditional C with an indented #",
291 /* Check if we have a known directive. INDENTED is non-zero if the
292 '#' of the directive was indented. This function is in this file
293 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
294 non-zero if the line of tokens has been handled, zero if we should
295 continue processing the line. */
298 _cpp_handle_directive (pfile
, indented
)
302 const directive
*dir
= 0;
303 const cpp_token
*dname
;
306 start_directive (pfile
);
307 dname
= _cpp_lex_token (pfile
);
309 if (dname
->type
== CPP_NAME
)
311 if (dname
->val
.node
->directive_index
)
312 dir
= &dtable
[dname
->val
.node
->directive_index
- 1];
314 /* We do not recognise the # followed by a number extension in
316 else if (dname
->type
== CPP_NUMBER
&& CPP_OPTION (pfile
, lang
) != CLK_ASM
)
318 dir
= &dtable
[T_LINE
];
319 pfile
->state
.line_extension
= 1;
324 /* If we have a directive that is not an opening conditional,
325 invalidate any control macro. */
326 if (! (dir
->flags
& IF_COND
))
327 pfile
->mi_valid
= false;
329 /* Kluge alert. In order to be sure that code like this
334 does not cause '#define foo bar' to get executed when
335 compiled with -save-temps, we recognize directives in
336 -fpreprocessed mode only if the # is in column 1. cppmacro.c
337 puts a space in fron of any '#' at the start of a macro. */
338 if (CPP_OPTION (pfile
, preprocessed
)
339 && (indented
|| !(dir
->flags
& IN_I
)))
346 /* In failed conditional groups, all non-conditional
347 directives are ignored. Before doing that, whether
348 skipping or not, we should lex angle-bracketed headers
349 correctly, and maybe output some diagnostics. */
350 pfile
->state
.angled_headers
= dir
->flags
& INCL
;
351 if (! CPP_OPTION (pfile
, preprocessed
))
352 directive_diagnostics (pfile
, dir
, indented
);
353 if (pfile
->state
.skipping
&& !(dir
->flags
& COND
))
357 else if (dname
->type
== CPP_EOF
)
358 ; /* CPP_EOF is the "null directive". */
361 /* An unknown directive. Don't complain about it in assembly
362 source: we don't know where the comments are, and # may
363 introduce assembler pseudo-ops. Don't complain about invalid
364 directives in skipped conditional groups (6.10 p4). */
365 if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
367 else if (!pfile
->state
.skipping
)
368 cpp_error (pfile
, "invalid preprocessing directive #%s",
369 cpp_token_as_text (pfile
, dname
));
374 pfile
->directive
= dir
;
375 (*pfile
->directive
->handler
) (pfile
);
378 _cpp_backup_tokens (pfile
, 1);
380 end_directive (pfile
, skip
);
384 /* Directive handler wrapper used by the command line option
387 run_directive (pfile
, dir_no
, buf
, count
)
393 cpp_push_buffer (pfile
, (const U_CHAR
*) buf
, count
,
394 /* from_stage3 */ true, 1);
395 start_directive (pfile
);
396 /* We don't want a leading # to be interpreted as a directive. */
397 pfile
->buffer
->saved_flags
= 0;
398 pfile
->directive
= &dtable
[dir_no
];
399 (void) (*pfile
->directive
->handler
) (pfile
);
400 end_directive (pfile
, 1);
401 _cpp_pop_buffer (pfile
);
404 /* Checks for validity the macro name in #define, #undef, #ifdef and
405 #ifndef directives. */
406 static cpp_hashnode
*
407 lex_macro_node (pfile
)
411 const cpp_token
*token
= _cpp_lex_token (pfile
);
413 /* The token immediately after #define must be an identifier. That
414 identifier may not be "defined", per C99 6.10.8p4.
415 In C++, it may not be any of the "named operators" either,
416 per C++98 [lex.digraph], [lex.key].
417 Finally, the identifier may not have been poisoned. (In that case
418 the lexer has issued the error message for us.) */
420 if (token
->type
!= CPP_NAME
)
422 if (token
->type
== CPP_EOF
)
423 cpp_error (pfile
, "no macro name given in #%s directive",
424 pfile
->directive
->name
);
425 else if (token
->flags
& NAMED_OP
)
427 "\"%s\" cannot be used as a macro name as it is an operator in C++",
428 NODE_NAME (token
->val
.node
));
430 cpp_error (pfile
, "macro names must be identifiers");
435 node
= token
->val
.node
;
436 if (node
->flags
& NODE_POISONED
)
439 if (node
== pfile
->spec_nodes
.n_defined
)
441 cpp_error (pfile
, "\"%s\" cannot be used as a macro name",
449 /* Process a #define directive. Most work is done in cppmacro.c. */
454 cpp_hashnode
*node
= lex_macro_node (pfile
);
458 if (_cpp_create_definition (pfile
, node
))
459 if (pfile
->cb
.define
)
460 (*pfile
->cb
.define
) (pfile
, pfile
->directive_line
, node
);
464 /* Handle #undef. Marks the identifier NT_VOID in the hash table. */
469 cpp_hashnode
*node
= lex_macro_node (pfile
);
471 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
472 is not currently defined as a macro name. */
473 if (node
&& node
->type
== NT_MACRO
)
476 (*pfile
->cb
.undef
) (pfile
, pfile
->directive_line
, node
);
478 if (node
->flags
& NODE_WARN
)
479 cpp_warning (pfile
, "undefining \"%s\"", NODE_NAME (node
));
481 _cpp_free_definition (node
);
486 /* Helper routine used by parse_include. Reinterpret the current line
487 as an h-char-sequence (< ... >); we are looking at the first token
488 after the <. Returns zero on success. */
490 glue_header_name (pfile
, header
)
495 unsigned char *buffer
, *token_mem
;
496 size_t len
, total_len
= 0, capacity
= 1024;
498 /* To avoid lexed tokens overwriting our glued name, we can only
499 allocate from the string pool once we've lexed everything. */
501 buffer
= (unsigned char *) xmalloc (capacity
);
504 cpp_get_token (pfile
, &token
);
506 if (token
.type
== CPP_GREATER
|| token
.type
== CPP_EOF
)
509 len
= cpp_token_len (&token
);
510 if (total_len
+ len
> capacity
)
512 capacity
= (capacity
+ len
) * 2;
513 buffer
= (unsigned char *) xrealloc (buffer
, capacity
);
516 if (token
.flags
& PREV_WHITE
)
517 buffer
[total_len
++] = ' ';
519 total_len
= cpp_spell_token (pfile
, &token
, &buffer
[total_len
]) - buffer
;
522 if (token
.type
== CPP_EOF
)
523 cpp_error (pfile
, "missing terminating > character");
526 token_mem
= _cpp_pool_alloc (&pfile
->ident_pool
, total_len
+ 1);
527 memcpy (token_mem
, buffer
, total_len
);
528 token_mem
[total_len
] = '\0';
530 header
->type
= CPP_HEADER_NAME
;
531 header
->flags
&= ~PREV_WHITE
;
532 header
->val
.str
.len
= total_len
;
533 header
->val
.str
.text
= token_mem
;
537 return token
.type
== CPP_EOF
;
540 /* Parse the header name of #include, #include_next, #import and
541 #pragma dependency. Returns zero on success. */
543 parse_include (pfile
, header
)
547 const unsigned char *dir
;
549 if (pfile
->directive
== &dtable
[T_PRAGMA
])
550 dir
= U
"pragma dependency";
552 dir
= pfile
->directive
->name
;
554 /* Allow macro expansion. */
555 cpp_get_token (pfile
, header
);
556 if (header
->type
!= CPP_STRING
&& header
->type
!= CPP_HEADER_NAME
)
558 if (header
->type
!= CPP_LESS
)
560 cpp_error (pfile
, "#%s expects \"FILENAME\" or <FILENAME>", dir
);
563 if (glue_header_name (pfile
, header
))
567 if (header
->val
.str
.len
== 0)
569 cpp_error (pfile
, "empty file name in #%s", dir
);
576 /* Handle #include, #include_next and #import. */
578 do_include_common (pfile
, type
)
580 enum include_type type
;
584 /* For #include_next, if this is the primary source file, warn and
585 use the normal search logic. */
586 if (type
== IT_INCLUDE_NEXT
&& ! pfile
->buffer
->prev
)
588 cpp_warning (pfile
, "#include_next in primary source file");
591 else if (type
== IT_IMPORT
&& CPP_OPTION (pfile
, warn_import
))
593 CPP_OPTION (pfile
, warn_import
) = 0;
595 "#import is obsolete, use an #ifndef wrapper in the header file");
598 if (!parse_include (pfile
, &header
))
600 /* Prevent #include recursion. */
601 if (pfile
->line_maps
.depth
>= CPP_STACK_MAX
)
602 cpp_fatal (pfile
, "#include nested too deeply");
606 /* Get out of macro context, if we are. */
607 skip_rest_of_line (pfile
);
608 if (pfile
->cb
.include
)
609 (*pfile
->cb
.include
) (pfile
, pfile
->directive_line
,
610 pfile
->directive
->name
, &header
);
612 _cpp_execute_include (pfile
, &header
, type
);
621 do_include_common (pfile
, IT_INCLUDE
);
628 do_include_common (pfile
, IT_IMPORT
);
632 do_include_next (pfile
)
635 do_include_common (pfile
, IT_INCLUDE_NEXT
);
638 /* Subroutine of do_line. Read possible flags after file name. LAST
639 is the last flag seen; 0 if this is the first flag. Return the flag
640 if it is valid, 0 at the end of the directive. Otherwise complain. */
643 read_flag (pfile
, last
)
647 const cpp_token
*token
= _cpp_lex_token (pfile
);
649 if (token
->type
== CPP_NUMBER
&& token
->val
.str
.len
== 1)
651 unsigned int flag
= token
->val
.str
.text
[0] - '0';
653 if (flag
> last
&& flag
<= 4
654 && (flag
!= 4 || last
== 3)
655 && (flag
!= 2 || last
== 0))
659 if (token
->type
!= CPP_EOF
)
660 cpp_error (pfile
, "invalid flag \"%s\" in line directive",
661 cpp_token_as_text (pfile
, token
));
665 /* Another subroutine of do_line. Convert a number in STR, of length
666 LEN, to binary; store it in NUMP, and return 0 if the number was
667 well-formed, 1 if not. Temporary, hopefully. */
669 strtoul_for_line (str
, len
, nump
)
674 unsigned long reg
= 0;
688 /* Interpret #line command.
689 Note that the filename string (if any) is treated as if it were an
690 include filename. That means no escape handling. */
697 const char *new_file
= pfile
->map
->to_file
;
698 unsigned long new_lineno
;
699 unsigned int cap
, new_sysp
= pfile
->map
->sysp
;
700 enum lc_reason reason
= LC_RENAME
;
702 /* C99 raised the minimum limit on #line numbers. */
703 cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
705 /* Putting this in _cpp_handle_directive risks two calls to
706 _cpp_backup_tokens in some circumstances, which can segfault. */
707 if (pfile
->state
.line_extension
)
708 _cpp_backup_tokens (pfile
, 1);
710 /* #line commands expand macros. */
711 cpp_get_token (pfile
, &token
);
712 if (token
.type
!= CPP_NUMBER
713 || strtoul_for_line (token
.val
.str
.text
, token
.val
.str
.len
, &new_lineno
))
715 cpp_error (pfile
, "\"%s\" after #line is not a positive integer",
716 cpp_token_as_text (pfile
, &token
));
720 if (CPP_PEDANTIC (pfile
) && ! pfile
->state
.line_extension
721 && (new_lineno
== 0 || new_lineno
> cap
))
722 cpp_pedwarn (pfile
, "line number out of range");
724 cpp_get_token (pfile
, &token
);
725 if (token
.type
== CPP_STRING
)
727 new_file
= (const char *) token
.val
.str
.text
;
729 /* Only accept flags for the # 55 form. */
730 if (pfile
->state
.line_extension
)
735 flag
= read_flag (pfile
, 0);
739 /* Fake an include for cpp_included (). */
740 _cpp_fake_include (pfile
, new_file
);
741 flag
= read_flag (pfile
, flag
);
746 flag
= read_flag (pfile
, flag
);
751 flag
= read_flag (pfile
, flag
);
758 else if (token
.type
!= CPP_EOF
)
760 cpp_error (pfile
, "\"%s\" is not a valid filename",
761 cpp_token_as_text (pfile
, &token
));
765 skip_rest_of_line (pfile
);
766 _cpp_do_file_change (pfile
, reason
, new_file
, new_lineno
, new_sysp
);
769 /* Arrange the file_change callback. pfile->line has changed to
770 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
771 header, 2 for a sytem header that needs to be extern "C" protected,
772 and zero otherwise. */
774 _cpp_do_file_change (pfile
, reason
, to_file
, file_line
, sysp
)
776 enum lc_reason reason
;
778 unsigned int file_line
;
781 pfile
->map
= add_line_map (&pfile
->line_maps
, reason
, sysp
,
782 pfile
->line
, to_file
, file_line
);
784 if (pfile
->cb
.file_change
)
785 (*pfile
->cb
.file_change
) (pfile
, pfile
->map
);
789 * Report a warning or error detected by the program we are
790 * processing. Use the directive's tokens in the error message.
794 do_diagnostic (pfile
, code
, print_dir
)
796 enum error_type code
;
799 if (_cpp_begin_message (pfile
, code
, 0, 0))
802 fprintf (stderr
, "#%s ", pfile
->directive
->name
);
803 pfile
->state
.prevent_expansion
++;
804 cpp_output_line (pfile
, stderr
);
805 pfile
->state
.prevent_expansion
--;
813 do_diagnostic (pfile
, ERROR
, 1);
820 /* We want #warning diagnostics to be emitted in system headers too. */
821 do_diagnostic (pfile
, WARNING_SYSHDR
, 1);
824 /* Report program identification. */
832 cpp_get_token (pfile
, &str
);
833 if (str
.type
!= CPP_STRING
)
834 cpp_error (pfile
, "invalid #ident");
835 else if (pfile
->cb
.ident
)
836 (*pfile
->cb
.ident
) (pfile
, pfile
->directive_line
, &str
.val
.str
);
841 /* Pragmata handling. We handle some of these, and pass the rest on
842 to the front end. C99 defines three pragmas and says that no macro
843 expansion is to be performed on them; whether or not macro
844 expansion happens for other pragmas is implementation defined.
845 This implementation never macro-expands the text after #pragma. */
847 /* Sub-handlers for the pragmas needing treatment here.
848 They return 1 if the token buffer is to be popped, 0 if not. */
849 typedef void (*pragma_cb
) PARAMS ((cpp_reader
*));
852 struct pragma_entry
*next
;
858 struct pragma_entry
*space
;
863 cpp_register_pragma (pfile
, space
, name
, handler
)
869 struct pragma_entry
**x
, *new;
875 struct pragma_entry
*p
= pfile
->pragmas
;
876 len
= strlen (space
);
879 if (p
->isnspace
&& p
->len
== len
&& !memcmp (p
->name
, space
, len
))
886 cpp_ice (pfile
, "unknown #pragma namespace %s", space
);
891 new = (struct pragma_entry
*)
892 _cpp_pool_alloc (&pfile
->macro_pool
, sizeof (struct pragma_entry
));
894 new->len
= strlen (name
);
896 new->u
.handler
= handler
;
903 cpp_register_pragma_space (pfile
, space
)
907 struct pragma_entry
*new;
908 const struct pragma_entry
*p
= pfile
->pragmas
;
909 size_t len
= strlen (space
);
913 if (p
->isnspace
&& p
->len
== len
&& !memcmp (p
->name
, space
, len
))
914 /* Multiple different callers are allowed to register the same
920 new = (struct pragma_entry
*)
921 _cpp_pool_alloc (&pfile
->macro_pool
, sizeof (struct pragma_entry
));
927 new->next
= pfile
->pragmas
;
928 pfile
->pragmas
= new;
932 _cpp_init_internal_pragmas (pfile
)
936 cpp_register_pragma (pfile
, 0, "poison", do_pragma_poison
);
937 cpp_register_pragma (pfile
, 0, "once", do_pragma_once
);
940 cpp_register_pragma_space (pfile
, "GCC");
942 cpp_register_pragma (pfile
, "GCC", "poison", do_pragma_poison
);
943 cpp_register_pragma (pfile
, "GCC", "system_header", do_pragma_system_header
);
944 cpp_register_pragma (pfile
, "GCC", "dependency", do_pragma_dependency
);
951 pragma_cb handler
= NULL
;
952 const struct pragma_entry
*p
;
954 unsigned int count
= 0;
957 pfile
->state
.prevent_expansion
++;
961 cpp_get_token (pfile
, &tok
);
962 if (tok
.type
== CPP_NAME
)
964 const cpp_hashnode
*node
= tok
.val
.node
;
965 size_t len
= NODE_LEN (node
);
969 if (strlen (p
->name
) == len
970 && !memcmp (p
->name
, NODE_NAME (node
), len
))
979 handler
= p
->u
.handler
;
987 /* FIXME. This is an awful kludge to get the front ends to update
988 their notion of line number for diagnostic purposes. The line
989 number should be passed to the handler and they should do it
990 themselves. Stand-alone CPP must ignore us, otherwise it will
991 prefix the directive with spaces, hence the 1. Ugh. */
992 if (pfile
->cb
.line_change
)
993 (*pfile
->cb
.line_change
)(pfile
, &tok
, 1);
997 else if (pfile
->cb
.def_pragma
)
999 _cpp_backup_tokens (pfile
, count
);
1000 (*pfile
->cb
.def_pragma
) (pfile
, pfile
->directive_line
);
1002 pfile
->state
.prevent_expansion
--;
1006 do_pragma_once (pfile
)
1009 cpp_warning (pfile
, "#pragma once is obsolete");
1011 if (pfile
->buffer
->prev
== NULL
)
1012 cpp_warning (pfile
, "#pragma once in main file");
1014 _cpp_never_reread (pfile
->buffer
->inc
);
1020 do_pragma_poison (pfile
)
1023 /* Poison these symbols so that all subsequent usage produces an
1025 const cpp_token
*tok
;
1028 pfile
->state
.poisoned_ok
= 1;
1031 tok
= _cpp_lex_token (pfile
);
1032 if (tok
->type
== CPP_EOF
)
1034 if (tok
->type
!= CPP_NAME
)
1036 cpp_error (pfile
, "invalid #pragma GCC poison directive");
1041 if (hp
->flags
& NODE_POISONED
)
1044 if (hp
->type
== NT_MACRO
)
1045 cpp_warning (pfile
, "poisoning existing macro \"%s\"", NODE_NAME (hp
));
1046 _cpp_free_definition (hp
);
1047 hp
->flags
|= NODE_POISONED
| NODE_DIAGNOSTIC
;
1049 pfile
->state
.poisoned_ok
= 0;
1052 /* Mark the current header as a system header. This will suppress
1053 some categories of warnings (notably those from -pedantic). It is
1054 intended for use in system libraries that cannot be implemented in
1055 conforming C, but cannot be certain that their headers appear in a
1056 system include directory. To prevent abuse, it is rejected in the
1057 primary source file. */
1059 do_pragma_system_header (pfile
)
1062 cpp_buffer
*buffer
= pfile
->buffer
;
1064 if (buffer
->prev
== 0)
1065 cpp_warning (pfile
, "#pragma system_header ignored outside include file");
1069 skip_rest_of_line (pfile
);
1070 cpp_make_system_header (pfile
, 1, 0);
1074 /* Check the modified date of the current include file against a specified
1075 file. Issue a diagnostic, if the specified file is newer. We use this to
1076 determine if a fixed header should be refixed. */
1078 do_pragma_dependency (pfile
)
1081 cpp_token header
, msg
;
1084 if (parse_include (pfile
, &header
))
1087 ordering
= _cpp_compare_file_date (pfile
, &header
);
1089 cpp_warning (pfile
, "cannot find source %s",
1090 cpp_token_as_text (pfile
, &header
));
1091 else if (ordering
> 0)
1093 cpp_warning (pfile
, "current file is older than %s",
1094 cpp_token_as_text (pfile
, &header
));
1095 cpp_get_token (pfile
, &msg
);
1096 if (msg
.type
!= CPP_EOF
)
1098 _cpp_backup_tokens (pfile
, 1);
1099 do_diagnostic (pfile
, WARNING
, 0);
1104 /* Check syntax is "(string-literal)". Returns 0 on success. */
1106 get__Pragma_string (pfile
, string
)
1112 cpp_get_token (pfile
, &paren
);
1113 if (paren
.type
!= CPP_OPEN_PAREN
)
1116 cpp_get_token (pfile
, string
);
1117 if (string
->type
!= CPP_STRING
&& string
->type
!= CPP_WSTRING
)
1120 cpp_get_token (pfile
, &paren
);
1121 return paren
.type
!= CPP_CLOSE_PAREN
;
1124 /* Returns a malloced buffer containing a destringized cpp_string by
1125 removing the first \ of \" and \\ sequences. */
1126 static unsigned char *
1127 destringize (in
, len
)
1128 const cpp_string
*in
;
1131 const unsigned char *src
, *limit
;
1132 unsigned char *dest
, *result
;
1134 dest
= result
= (unsigned char *) xmalloc (in
->len
);
1135 for (src
= in
->text
, limit
= src
+ in
->len
; src
< limit
;)
1137 /* We know there is a character following the backslash. */
1138 if (*src
== '\\' && (src
[1] == '\\' || src
[1] == '"'))
1143 *len
= dest
- result
;
1148 _cpp_do__Pragma (pfile
)
1152 unsigned char *buffer
;
1155 if (get__Pragma_string (pfile
, &string
))
1156 cpp_error (pfile
, "_Pragma takes a parenthesized string literal");
1159 /* Ideally, we'd like
1160 token1 _Pragma ("foo") token2
1167 Getting these correct line markers is a little tricky. */
1169 unsigned int orig_line
= pfile
->line
;
1170 buffer
= destringize (&string
.val
.str
, &len
);
1171 run_directive (pfile
, T_PRAGMA
, (char *) buffer
, len
);
1172 free ((PTR
) buffer
);
1173 pfile
->line
= orig_line
;
1174 pfile
->buffer
->saved_flags
= BOL
;
1178 /* Just ignore #sccs, on systems where we define it at all. */
1179 #ifdef SCCS_DIRECTIVE
1182 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1193 if (! pfile
->state
.skipping
)
1195 const cpp_hashnode
*node
= lex_macro_node (pfile
);
1198 skip
= node
->type
!= NT_MACRO
;
1204 push_conditional (pfile
, skip
, T_IFDEF
, 0);
1212 const cpp_hashnode
*node
= 0;
1214 if (! pfile
->state
.skipping
)
1216 node
= lex_macro_node (pfile
);
1218 skip
= node
->type
== NT_MACRO
;
1224 push_conditional (pfile
, skip
, T_IFNDEF
, node
);
1227 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1228 pfile->mi_ind_cmacro so we can handle multiple-include
1229 optimisations. If macro expansion occurs in the expression, we
1230 cannot treat it as a controlling conditional, since the expansion
1231 could change in the future. That is handled by cpp_get_token. */
1239 if (! pfile
->state
.skipping
)
1240 skip
= _cpp_parse_expr (pfile
) == 0;
1242 push_conditional (pfile
, skip
, T_IF
, pfile
->mi_ind_cmacro
);
1245 /* Flip skipping state if appropriate and continue without changing
1246 if_stack; this is so that the error message for missing #endif's
1247 etc. will point to the original #if. */
1253 cpp_buffer
*buffer
= pfile
->buffer
;
1254 struct if_stack
*ifs
= buffer
->if_stack
;
1257 cpp_error (pfile
, "#else without #if");
1260 if (ifs
->type
== T_ELSE
)
1262 cpp_error (pfile
, "#else after #else");
1263 cpp_error_with_line (pfile
, ifs
->line
, 0,
1264 "the conditional began here");
1268 /* Skip any future (erroneous) #elses or #elifs. */
1269 pfile
->state
.skipping
= ifs
->skip_elses
;
1270 ifs
->skip_elses
= true;
1272 /* Invalidate any controlling macro. */
1275 /* Only check EOL if was not originally skipping. */
1276 if (!ifs
->was_skipping
)
1281 /* handle a #elif directive by not changing if_stack either. see the
1282 comment above do_else. */
1288 cpp_buffer
*buffer
= pfile
->buffer
;
1289 struct if_stack
*ifs
= buffer
->if_stack
;
1292 cpp_error (pfile
, "#elif without #if");
1295 if (ifs
->type
== T_ELSE
)
1297 cpp_error (pfile
, "#elif after #else");
1298 cpp_error_with_line (pfile
, ifs
->line
, 0,
1299 "the conditional began here");
1303 /* Only evaluate this if we aren't skipping elses. During
1304 evaluation, set skipping to false to get lexer warnings. */
1305 if (ifs
->skip_elses
)
1306 pfile
->state
.skipping
= 1;
1309 pfile
->state
.skipping
= 0;
1310 pfile
->state
.skipping
= ! _cpp_parse_expr (pfile
);
1311 ifs
->skip_elses
= ! pfile
->state
.skipping
;
1314 /* Invalidate any controlling macro. */
1319 /* #endif pops the if stack and resets pfile->state.skipping. */
1325 cpp_buffer
*buffer
= pfile
->buffer
;
1326 struct if_stack
*ifs
= buffer
->if_stack
;
1329 cpp_error (pfile
, "#endif without #if");
1332 /* Only check EOL if was not originally skipping. */
1333 if (!ifs
->was_skipping
)
1336 /* If potential control macro, we go back outside again. */
1337 if (ifs
->next
== 0 && ifs
->mi_cmacro
)
1339 pfile
->mi_valid
= true;
1340 pfile
->mi_cmacro
= ifs
->mi_cmacro
;
1343 buffer
->if_stack
= ifs
->next
;
1344 pfile
->state
.skipping
= ifs
->was_skipping
;
1345 obstack_free (&pfile
->buffer_ob
, ifs
);
1349 /* Push an if_stack entry and set pfile->state.skipping accordingly.
1350 If this is a #if or #ifndef, CMACRO is a potentially controlling
1351 macro - we need to check here that we are at the top of the file. */
1354 push_conditional (pfile
, skip
, type
, cmacro
)
1358 const cpp_hashnode
*cmacro
;
1360 struct if_stack
*ifs
;
1361 cpp_buffer
*buffer
= pfile
->buffer
;
1363 ifs
= xobnew (&pfile
->buffer_ob
, struct if_stack
);
1364 ifs
->line
= pfile
->directive_line
;
1365 ifs
->next
= buffer
->if_stack
;
1366 ifs
->skip_elses
= pfile
->state
.skipping
|| !skip
;
1367 ifs
->was_skipping
= pfile
->state
.skipping
;
1369 /* This condition is effectively a test for top-of-file. */
1370 if (pfile
->mi_valid
&& pfile
->mi_cmacro
== 0)
1371 ifs
->mi_cmacro
= cmacro
;
1375 pfile
->state
.skipping
= skip
;
1376 buffer
->if_stack
= ifs
;
1379 /* Read the tokens of the answer into the macro pool. Only commit the
1380 memory if we intend it as permanent storage, i.e. the #assert case.
1381 Returns 0 on success. */
1384 parse_answer (pfile
, answerp
, type
)
1386 struct answer
**answerp
;
1389 cpp_token paren
, *token
;
1390 struct answer
*answer
;
1392 if (POOL_FRONT (&pfile
->macro_pool
) + sizeof (struct answer
) >
1393 POOL_LIMIT (&pfile
->macro_pool
))
1394 _cpp_next_chunk (&pfile
->macro_pool
, sizeof (struct answer
), 0);
1395 answer
= (struct answer
*) POOL_FRONT (&pfile
->macro_pool
);
1398 /* In a conditional, it is legal to not have an open paren. We
1399 should save the following token in this case. */
1400 cpp_get_token (pfile
, &paren
);
1402 /* If not a paren, see if we're OK. */
1403 if (paren
.type
!= CPP_OPEN_PAREN
)
1405 /* In a conditional no answer is a test for any answer. It
1406 could be followed by any token. */
1409 _cpp_backup_tokens (pfile
, 1);
1413 /* #unassert with no answer is valid - it removes all answers. */
1414 if (type
== T_UNASSERT
&& paren
.type
== CPP_EOF
)
1417 cpp_error (pfile
, "missing '(' after predicate");
1423 token
= &answer
->first
[answer
->count
];
1424 /* Check we have room for the token. */
1425 if ((unsigned char *) (token
+ 1) >= POOL_LIMIT (&pfile
->macro_pool
))
1427 _cpp_next_chunk (&pfile
->macro_pool
, sizeof (cpp_token
),
1428 (unsigned char **) &answer
);
1429 token
= &answer
->first
[answer
->count
];
1432 cpp_get_token (pfile
, token
);
1433 if (token
->type
== CPP_CLOSE_PAREN
)
1436 if (token
->type
== CPP_EOF
)
1438 cpp_error (pfile
, "missing ')' to complete answer");
1444 if (answer
->count
== 0)
1446 cpp_error (pfile
, "predicate's answer is empty");
1450 /* Drop whitespace at start. */
1451 answer
->first
->flags
&= ~PREV_WHITE
;
1454 if (type
== T_ASSERT
|| type
== T_UNASSERT
)
1459 /* Parses an assertion, returning a pointer to the hash node of the
1460 predicate, or 0 on error. If an answer was supplied, it is placed
1461 in ANSWERP, otherwise it is set to 0. */
1462 static cpp_hashnode
*
1463 parse_assertion (pfile
, answerp
, type
)
1465 struct answer
**answerp
;
1468 cpp_hashnode
*result
= 0;
1469 cpp_token predicate
;
1471 /* We don't expand predicates or answers. */
1472 pfile
->state
.prevent_expansion
++;
1475 cpp_get_token (pfile
, &predicate
);
1476 if (predicate
.type
== CPP_EOF
)
1477 cpp_error (pfile
, "assertion without predicate");
1478 else if (predicate
.type
!= CPP_NAME
)
1479 cpp_error (pfile
, "predicate must be an identifier");
1480 else if (parse_answer (pfile
, answerp
, type
) == 0)
1482 unsigned int len
= NODE_LEN (predicate
.val
.node
);
1483 unsigned char *sym
= alloca (len
+ 1);
1485 /* Prefix '#' to get it out of macro namespace. */
1487 memcpy (sym
+ 1, NODE_NAME (predicate
.val
.node
), len
);
1488 result
= cpp_lookup (pfile
, sym
, len
+ 1);
1491 pfile
->state
.prevent_expansion
--;
1495 /* Returns a pointer to the pointer to the answer in the answer chain,
1496 or a pointer to NULL if the answer is not in the chain. */
1497 static struct answer
**
1498 find_answer (node
, candidate
)
1500 const struct answer
*candidate
;
1503 struct answer
**result
;
1505 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1507 struct answer
*answer
= *result
;
1509 if (answer
->count
== candidate
->count
)
1511 for (i
= 0; i
< answer
->count
; i
++)
1512 if (! _cpp_equiv_tokens (&answer
->first
[i
], &candidate
->first
[i
]))
1515 if (i
== answer
->count
)
1523 /* Test an assertion within a preprocessor conditional. Returns
1524 non-zero on failure, zero on success. On success, the result of
1525 the test is written into VALUE. */
1527 _cpp_test_assertion (pfile
, value
)
1531 struct answer
*answer
;
1534 node
= parse_assertion (pfile
, &answer
, T_IF
);
1536 *value
= (node
->type
== NT_ASSERTION
&&
1537 (answer
== 0 || *find_answer (node
, answer
) != 0));
1539 /* We don't commit the memory for the answer - it's temporary only. */
1547 struct answer
*new_answer
;
1550 node
= parse_assertion (pfile
, &new_answer
, T_ASSERT
);
1553 /* Place the new answer in the answer list. First check there
1554 is not a duplicate. */
1555 new_answer
->next
= 0;
1556 if (node
->type
== NT_ASSERTION
)
1558 if (*find_answer (node
, new_answer
))
1560 cpp_warning (pfile
, "\"%s\" re-asserted", NODE_NAME (node
) + 1);
1563 new_answer
->next
= node
->value
.answers
;
1565 node
->type
= NT_ASSERTION
;
1566 node
->value
.answers
= new_answer
;
1567 POOL_COMMIT (&pfile
->macro_pool
, (sizeof (struct answer
)
1568 + (new_answer
->count
- 1)
1569 * sizeof (cpp_token
)));
1578 struct answer
*answer
;
1580 node
= parse_assertion (pfile
, &answer
, T_UNASSERT
);
1581 /* It isn't an error to #unassert something that isn't asserted. */
1582 if (node
&& node
->type
== NT_ASSERTION
)
1586 struct answer
**p
= find_answer (node
, answer
), *temp
;
1588 /* Remove the answer from the list. */
1593 /* Did we free the last answer? */
1594 if (node
->value
.answers
== 0)
1595 node
->type
= NT_VOID
;
1598 _cpp_free_definition (node
);
1601 /* We don't commit the memory for the answer - it's temporary only. */
1604 /* These are for -D, -U, -A. */
1606 /* Process the string STR as if it appeared as the body of a #define.
1607 If STR is just an identifier, define it with value 1.
1608 If STR has anything after the identifier, then it should
1609 be identifier=definition. */
1612 cpp_define (pfile
, str
)
1619 /* Copy the entire option so we can modify it.
1620 Change the first "=" in the string to a space. If there is none,
1621 tack " 1" on the end. */
1623 /* Length including the null. */
1624 count
= strlen (str
);
1625 buf
= (char *) alloca (count
+ 2);
1626 memcpy (buf
, str
, count
);
1628 p
= strchr (str
, '=');
1637 run_directive (pfile
, T_DEFINE
, buf
, count
);
1640 /* Slight variant of the above for use by initialize_builtins. */
1642 _cpp_define_builtin (pfile
, str
)
1646 run_directive (pfile
, T_DEFINE
, str
, strlen (str
));
1649 /* Process MACRO as if it appeared as the body of an #undef. */
1651 cpp_undef (pfile
, macro
)
1655 run_directive (pfile
, T_UNDEF
, macro
, strlen (macro
));
1658 /* Process the string STR as if it appeared as the body of a #assert. */
1660 cpp_assert (pfile
, str
)
1664 handle_assertion (pfile
, str
, T_ASSERT
);
1667 /* Process STR as if it appeared as the body of an #unassert. */
1669 cpp_unassert (pfile
, str
)
1673 handle_assertion (pfile
, str
, T_UNASSERT
);
1676 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1678 handle_assertion (pfile
, str
, type
)
1683 size_t count
= strlen (str
);
1684 const char *p
= strchr (str
, '=');
1688 /* Copy the entire option so we can modify it. Change the first
1689 "=" in the string to a '(', and tack a ')' on the end. */
1690 char *buf
= (char *) alloca (count
+ 1);
1692 memcpy (buf
, str
, count
);
1698 run_directive (pfile
, type
, str
, count
);
1701 /* The number of errors for a given reader. */
1706 return pfile
->errors
;
1709 /* The options structure. */
1711 cpp_get_options (pfile
)
1714 return &pfile
->opts
;
1717 /* The callbacks structure. */
1719 cpp_get_callbacks (pfile
)
1725 /* The line map set. */
1726 const struct line_maps
*
1727 cpp_get_line_maps (pfile
)
1730 return &pfile
->line_maps
;
1733 /* Copy the given callbacks structure to our own. */
1735 cpp_set_callbacks (pfile
, cb
)
1742 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1743 doesn't fail. It does not generate a file change call back; that
1744 is the responsibility of the caller. */
1746 cpp_push_buffer (pfile
, buffer
, len
, from_stage3
, return_at_eof
)
1748 const U_CHAR
*buffer
;
1753 cpp_buffer
*new = xobnew (&pfile
->buffer_ob
, cpp_buffer
);
1755 /* Clears, amongst other things, if_stack and mi_cmacro. */
1756 memset (new, 0, sizeof (cpp_buffer
));
1758 new->line_base
= new->buf
= new->cur
= buffer
;
1759 new->rlimit
= buffer
+ len
;
1761 /* No read ahead or extra char initially. */
1762 new->read_ahead
= EOF
;
1763 new->extra_char
= EOF
;
1764 new->from_stage3
= from_stage3
;
1765 new->prev
= pfile
->buffer
;
1766 new->return_at_eof
= return_at_eof
;
1767 new->saved_flags
= BOL
;
1769 pfile
->buffer
= new;
1774 /* If called from do_line, pops a single buffer. Otherwise pops all
1775 buffers until a real file is reached. Generates appropriate
1778 _cpp_pop_buffer (pfile
)
1781 cpp_buffer
*buffer
= pfile
->buffer
;
1782 struct if_stack
*ifs
;
1784 /* Walk back up the conditional stack till we reach its level at
1785 entry to this file, issuing error messages. */
1786 for (ifs
= buffer
->if_stack
; ifs
; ifs
= ifs
->next
)
1787 cpp_error_with_line (pfile
, ifs
->line
, 0,
1788 "unterminated #%s", dtable
[ifs
->type
].name
);
1790 /* In case of a missing #endif. */
1791 pfile
->state
.skipping
= 0;
1793 /* Update the reader's buffer before _cpp_do_file_change. */
1794 pfile
->buffer
= buffer
->prev
;
1797 _cpp_pop_file_buffer (pfile
, buffer
->inc
);
1799 obstack_free (&pfile
->buffer_ob
, buffer
);
1803 _cpp_init_directives (pfile
)
1809 /* Register the directives. */
1810 for (i
= 0; i
< (unsigned int) N_DIRECTIVES
; i
++)
1812 node
= cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
1813 node
->directive_index
= i
+ 1;