2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 /* `struct directive' defines one #-directive, including how to handle it. */
34 directive_handler func
; /* Function to handle directive. */
35 const U_CHAR
*name
; /* Name of directive. */
36 unsigned short length
; /* Length of name. */
37 unsigned short flags
; /* Flags describing this directive. */
40 /* Stack of conditionals currently in progress
41 (including both successful and failing conditionals). */
45 struct if_stack
*next
;
46 int lineno
; /* line number where condition started */
47 int was_skipping
; /* value of pfile->skipping before this if */
48 const cpp_hashnode
*cmacro
; /* macro name for #ifndef around entire file */
49 int type
; /* type of last directive seen in this group */
52 /* Forward declarations. */
54 static void validate_else
PARAMS ((cpp_reader
*, const U_CHAR
*));
55 static unsigned int parse_include
PARAMS ((cpp_reader
*, const U_CHAR
*));
56 static void push_conditional
PARAMS ((cpp_reader
*, int, int,
57 const cpp_hashnode
*));
58 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t,
60 static int read_line_number
PARAMS ((cpp_reader
*, int *));
61 static const cpp_hashnode
*parse_ifdef
PARAMS ((cpp_reader
*, const U_CHAR
*));
62 static const cpp_hashnode
*detect_if_not_defined
PARAMS ((cpp_reader
*));
64 /* Values for the flags field of the table below. KANDR and COND
65 directives come from traditional (K&R) C. The difference is, if we
66 care about it while skipping a failed conditional block, its origin
67 is COND. STDC89 directives come from the 1989 C standard.
68 EXTENSION directives are extensions, with origins noted below. */
76 #define ORIGIN(f) ((f) & ORIGIN_MASK)
77 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
79 /* This is the table of directive handlers. It is ordered by
80 frequency of occurrence; the numbers at the end are directive
81 counts from all the source code I have lying around (egcs and libc
82 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
85 The entries with a dash and a name after the count are extensions,
86 of which all but #warning and #include_next are deprecated. The name
87 is where the extension appears to have come from. */
89 /* #sccs is not always recognized. */
91 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
93 # define SCCS_ENTRY /* nothing */
96 #define DIRECTIVE_TABLE \
97 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
98 D(include, T_INCLUDE, KANDR | SYNTAX_INCLUDE) /* 52262 */ \
99 D(endif, T_ENDIF, COND) /* 45855 */ \
100 D(ifdef, T_IFDEF, COND) /* 22000 */ \
101 D(if, T_IF, COND) /* 18162 */ \
102 D(else, T_ELSE, COND) /* 9863 */ \
103 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
104 D(undef, T_UNDEF, KANDR) /* 4837 */ \
105 D(line, T_LINE, KANDR) /* 2465 */ \
106 D(elif, T_ELIF, COND) /* 610 */ \
107 D(error, T_ERROR, STDC89) /* 475 */ \
108 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
109 D(warning, T_WARNING, EXTENSION) /* 22 GNU */ \
110 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /* 19 GNU */ \
111 D(ident, T_IDENT, EXTENSION) /* 11 SVR4 */ \
112 D(import, T_IMPORT, EXTENSION | SYNTAX_INCLUDE) /* 0 ObjC */ \
113 D(assert, T_ASSERT, EXTENSION) /* 0 SVR4 */ \
114 D(unassert, T_UNASSERT, EXTENSION) /* 0 SVR4 */ \
117 /* Use the table to generate a series of prototypes, an enum for the
118 directive names, and an array of directive handlers. */
120 /* The directive-processing functions are declared to return int
121 instead of void, because some old compilers have trouble with
122 pointers to functions returning void. */
124 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
125 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
129 #define D(n, tag, f) tag,
137 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
138 #define D(name, t, flags) \
139 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
140 sizeof STRINGX(name) - 1, flags },
141 static const struct directive dtable
[] =
146 #undef DIRECTIVE_TABLE
148 /* Check if a token's name matches that of a known directive. Put in
149 this file to save exporting dtable and other unneeded information. */
151 _cpp_check_directive (list
, token
)
155 const U_CHAR
*name
= token
->val
.name
.text
;
156 size_t len
= token
->val
.name
.len
;
160 list
->flags
&= ~SYNTAX_INCLUDE
;
162 for (i
= 0; i
< N_DIRECTIVES
; i
++)
163 if (dtable
[i
].length
== len
&& !ustrncmp (dtable
[i
].name
, name
, len
))
166 if (dtable
[i
].flags
& SYNTAX_INCLUDE
)
167 list
->flags
|= SYNTAX_INCLUDE
;
172 /* Handle a possible # directive.
173 '#' has already been read. */
176 _cpp_handle_directive (pfile
)
183 long old_written
= CPP_WRITTEN (pfile
);
186 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
188 cpp_ice (pfile
, "handle_directive called on macro buffer");
192 /* -traditional directives are recognized only with the # in column 1. */
193 hash_at_bol
= CPP_IN_COLUMN_1 (pfile
);
195 /* Scan the next token, then pretend we didn't. */
196 CPP_SET_MARK (pfile
);
197 pfile
->no_macro_expand
++;
198 tok
= _cpp_get_directive_token (pfile
);
199 pfile
->no_macro_expand
--;
201 ident
= pfile
->token_buffer
+ old_written
;
202 len
= CPP_PWRITTEN (pfile
) - ident
;
203 CPP_SET_WRITTEN (pfile
, old_written
);
204 CPP_GOTO_MARK (pfile
);
206 /* # followed by a number is equivalent to #line. Do not recognize
207 this form in assembly language source files or skipped
208 conditional groups. Complain about this form if we're being
209 pedantic, but not if this is regurgitated input (preprocessed or
210 fed back in by the C++ frontend). */
211 if (tok
== CPP_NUMBER
)
213 if (pfile
->skipping
|| CPP_OPTION (pfile
, lang_asm
))
216 if (CPP_PEDANTIC (pfile
)
217 && CPP_BUFFER (pfile
)->ihash
218 && ! CPP_OPTION (pfile
, preprocessed
))
219 cpp_pedwarn (pfile
, "# followed by integer");
221 goto process_directive
;
224 /* If we are rescanning preprocessed input, don't obey any directives
226 else if (CPP_OPTION (pfile
, preprocessed
))
229 /* A line of just # becomes blank. */
230 else if (tok
== CPP_VSPACE
)
233 /* A NAME token might in fact be a directive! */
234 else if (tok
== CPP_NAME
)
236 for (i
= 0; i
< N_DIRECTIVES
; i
++)
238 if (dtable
[i
].length
== len
239 && !ustrncmp (dtable
[i
].name
, ident
, len
))
242 /* Don't complain about invalid directives in assembly source,
243 we don't know where the comments are, and # may introduce
244 assembler pseudo-ops. Don't complain about invalid directives
245 in skipped conditional groups (6.10 p4). */
246 if (!pfile
->skipping
&& !CPP_OPTION (pfile
, lang_asm
))
247 cpp_error (pfile
, "invalid preprocessing directive #%.*s",
251 /* And anything else means the # wasn't a directive marker. */
257 /* If we are skipping a failed conditional group, all non-conditional
258 directives are ignored. */
259 if (pfile
->skipping
&& ORIGIN (dtable
[i
].flags
) != COND
)
262 /* In -traditional mode, a directive is ignored unless its # is in
264 if (CPP_TRADITIONAL (pfile
) && !hash_at_bol
)
266 if (CPP_WTRADITIONAL (pfile
))
267 cpp_warning (pfile
, "ignoring #%s because of its indented #",
272 /* no_directives is set when we are parsing macro arguments. Directives
273 in macro arguments are undefined behavior (C99 6.10.3.11); this
274 implementation chooses to make them hard errors. */
275 if (pfile
->no_directives
)
277 cpp_error (pfile
, "#%s may not be used inside a macro argument",
279 _cpp_skip_rest_of_line (pfile
);
283 /* Issue -pedantic warnings for extended directives. */
284 if (CPP_PEDANTIC (pfile
) && ORIGIN (dtable
[i
].flags
) == EXTENSION
)
285 cpp_pedwarn (pfile
, "ISO C does not allow #%s", dtable
[i
].name
);
287 /* -Wtraditional gives warnings about directives with inappropriate
289 if (CPP_WTRADITIONAL (pfile
))
291 if (!hash_at_bol
&& TRAD_DIRECT_P (dtable
[i
].flags
))
292 cpp_warning (pfile
, "traditional C ignores #%s with the # indented",
294 else if (hash_at_bol
&& ! TRAD_DIRECT_P (dtable
[i
].flags
))
296 "suggest hiding #%s from traditional C with an indented #",
300 /* Unfortunately, it's necessary to scan the directive name again,
301 now we know we're going to consume it. FIXME. */
303 pfile
->no_macro_expand
++;
304 _cpp_get_directive_token (pfile
);
305 pfile
->no_macro_expand
--;
306 CPP_SET_WRITTEN (pfile
, old_written
);
309 (void) (*dtable
[i
].func
) (pfile
);
313 /* Pass a directive through to the output file.
314 BUF points to the contents of the directive, as a contiguous string.
315 LEN is the length of the string pointed to by BUF.
316 KEYWORD is the keyword-table entry for the directive. */
319 pass_thru_directive (buf
, len
, pfile
, keyword
)
325 const struct directive
*kt
= &dtable
[keyword
];
326 register unsigned klen
= kt
->length
;
328 CPP_RESERVE (pfile
, 1 + klen
+ len
);
329 CPP_PUTC_Q (pfile
, '#');
330 CPP_PUTS_Q (pfile
, kt
->name
, klen
);
331 if (len
!= 0 && buf
[0] != ' ')
332 CPP_PUTC_Q (pfile
, ' ');
333 CPP_PUTS_Q (pfile
, buf
, len
);
336 /* Process a #define command. */
345 cpp_toklist
*list
= &pfile
->directbuf
;
347 pfile
->no_macro_expand
++;
348 CPP_OPTION (pfile
, discard_comments
)++;
350 _cpp_scan_until (pfile
, list
, CPP_VSPACE
);
352 /* First token on the line must be a NAME. There may not be any
353 tokens in the list (if we had #define all by itself on a line). */
354 if (list
->tokens_used
== 0
355 || TOK_TYPE (list
, 0) != CPP_NAME
)
357 cpp_error_with_line (pfile
, list
->line
, TOK_COL (list
, 0),
358 "#define must be followed by an identifier");
362 sym
= TOK_NAME (list
, 0);
363 len
= TOK_LEN (list
, 0);
365 /* That NAME is not allowed to be "defined". (Not clear if the
366 standard requires this.) */
367 if (len
== 7 && !ustrncmp (sym
, U
"defined", 7))
369 cpp_error_with_line (pfile
, list
->line
, TOK_COL (list
, 0),
370 "\"defined\" is not a legal macro name");
374 node
= cpp_lookup (pfile
, sym
, len
);
375 /* Check for poisoned identifiers now. All other checks
376 are done in cpphash.c. */
377 if (node
->type
== T_POISON
)
379 cpp_error (pfile
, "redefining poisoned `%.*s'", len
, sym
);
383 if (_cpp_create_definition (pfile
, list
, node
) == 0)
386 if (CPP_OPTION (pfile
, debug_output
)
387 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
)
388 _cpp_dump_definition (pfile
, node
);
389 else if (CPP_OPTION (pfile
, dump_macros
) == dump_names
)
390 pass_thru_directive (sym
, len
, pfile
, T_DEFINE
);
393 pfile
->no_macro_expand
--;
394 CPP_OPTION (pfile
, discard_comments
)--;
398 /* Handle #include and #import. */
401 parse_include (pfile
, name
)
405 long old_written
= CPP_WRITTEN (pfile
);
406 enum cpp_ttype token
;
409 pfile
->parsing_include_directive
++;
410 token
= _cpp_get_directive_token (pfile
);
411 pfile
->parsing_include_directive
--;
413 len
= CPP_WRITTEN (pfile
) - old_written
;
415 if (token
!= CPP_STRING
)
417 cpp_error (pfile
, "#%s expects \"FILENAME\" or <FILENAME>", name
);
418 CPP_SET_WRITTEN (pfile
, old_written
);
419 _cpp_skip_rest_of_line (pfile
);
423 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
425 cpp_error (pfile
, "junk at end of #%s", name
);
426 _cpp_skip_rest_of_line (pfile
);
429 CPP_SET_WRITTEN (pfile
, old_written
);
432 cpp_error (pfile
, "empty file name in #%s", name
);
444 len
= parse_include (pfile
, dtable
[T_INCLUDE
].name
);
447 token
= (U_CHAR
*) alloca (len
+ 1);
448 memcpy (token
, CPP_PWRITTEN (pfile
), len
);
451 if (CPP_OPTION (pfile
, dump_includes
))
452 pass_thru_directive (token
, len
, pfile
, T_INCLUDE
);
454 _cpp_execute_include (pfile
, token
, len
, 0, 0);
465 if (CPP_OPTION (pfile
, warn_import
)
466 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
468 pfile
->import_warning
= 1;
470 "#import is obsolete, use an #ifndef wrapper in the header file");
473 len
= parse_include (pfile
, dtable
[T_IMPORT
].name
);
476 token
= (U_CHAR
*) alloca (len
+ 1);
477 memcpy (token
, CPP_PWRITTEN (pfile
), len
);
480 if (CPP_OPTION (pfile
, dump_includes
))
481 pass_thru_directive (token
, len
, pfile
, T_IMPORT
);
483 _cpp_execute_include (pfile
, token
, len
, 1, 0);
488 do_include_next (pfile
)
493 struct file_name_list
*search_start
= 0;
495 len
= parse_include (pfile
, dtable
[T_INCLUDE_NEXT
].name
);
498 token
= (U_CHAR
*) alloca (len
+ 1);
499 memcpy (token
, CPP_PWRITTEN (pfile
), len
);
502 if (CPP_OPTION (pfile
, dump_includes
))
503 pass_thru_directive (token
, len
, pfile
, T_INCLUDE_NEXT
);
505 /* For #include_next, skip in the search path past the dir in which the
506 containing file was found. Treat files specified using an absolute path
507 as if there are no more directories to search. Treat the primary source
508 file like any other included source, but generate a warning. */
509 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)))
511 if (CPP_BUFFER (pfile
)->ihash
->foundhere
!= ABSOLUTE_PATH
)
512 search_start
= CPP_BUFFER (pfile
)->ihash
->foundhere
->next
;
515 cpp_warning (pfile
, "#include_next in primary source file");
517 _cpp_execute_include (pfile
, token
, len
, 0, search_start
);
521 /* Subroutine of do_line. Read next token from PFILE without adding it to
522 the output buffer. If it is a number between 1 and 4, store it in *NUM
523 and return 1; otherwise, return 0 and complain if we aren't at the end
527 read_line_number (pfile
, num
)
531 long save_written
= CPP_WRITTEN (pfile
);
533 enum cpp_ttype token
= _cpp_get_directive_token (pfile
);
534 p
= pfile
->token_buffer
+ save_written
;
536 if (token
== CPP_NUMBER
&& p
+ 1 == CPP_PWRITTEN (pfile
)
537 && p
[0] >= '1' && p
[0] <= '4')
540 CPP_SET_WRITTEN (pfile
, save_written
);
545 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
546 cpp_error (pfile
, "invalid format #line");
547 CPP_SET_WRITTEN (pfile
, save_written
);
552 /* Interpret #line command.
553 Note that the filename string (if any) is treated as if it were an
554 include filename. That means no escape handling. */
560 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
561 unsigned int new_lineno
;
562 long old_written
= CPP_WRITTEN (pfile
);
563 enum cpp_ttype token
;
566 token
= _cpp_get_directive_token (pfile
);
568 if (token
!= CPP_NUMBER
)
570 cpp_error (pfile
, "token after #line is not an integer");
571 goto bad_line_directive
;
574 CPP_PUTC (pfile
, '\0'); /* not terminated for us */
575 new_lineno
= strtoul ((const char *) (pfile
->token_buffer
+ old_written
),
579 cpp_error (pfile
, "token after #line is not an integer");
580 goto bad_line_directive
;
582 CPP_SET_WRITTEN (pfile
, old_written
);
584 if (CPP_PEDANTIC (pfile
) && (new_lineno
<= 0 || new_lineno
> 32767))
585 cpp_pedwarn (pfile
, "line number out of range in #line");
587 token
= _cpp_get_directive_token (pfile
);
589 if (token
== CPP_STRING
)
591 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
592 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
593 int action_number
= 0;
595 if (read_line_number (pfile
, &action_number
))
597 if (CPP_PEDANTIC (pfile
))
598 cpp_pedwarn (pfile
, "garbage at end of #line");
600 /* This is somewhat questionable: change the buffer stack
601 depth so that output_line_command thinks we've stacked
603 if (action_number
== 1)
605 pfile
->buffer_stack_depth
++;
606 ip
->system_header_p
= 0;
607 read_line_number (pfile
, &action_number
);
609 else if (action_number
== 2)
611 pfile
->buffer_stack_depth
--;
612 ip
->system_header_p
= 0;
613 read_line_number (pfile
, &action_number
);
615 if (action_number
== 3)
617 ip
->system_header_p
= 1;
618 read_line_number (pfile
, &action_number
);
620 if (action_number
== 4)
622 ip
->system_header_p
= 2;
623 read_line_number (pfile
, &action_number
);
629 if (strcmp ((const char *)fname
, ip
->nominal_fname
))
631 if (!strcmp ((const char *)fname
, ip
->ihash
->name
))
632 ip
->nominal_fname
= ip
->ihash
->name
;
634 ip
->nominal_fname
= _cpp_fake_ihash (pfile
, (const char *)fname
);
637 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
639 cpp_error (pfile
, "second token after #line is not a string");
640 goto bad_line_directive
;
643 /* The Newline at the end of this line remains to be processed.
644 To put the next line at the specified line number,
645 we must store a line number now that is one less. */
646 ip
->lineno
= new_lineno
- 1;
647 CPP_SET_WRITTEN (pfile
, old_written
);
651 _cpp_skip_rest_of_line (pfile
);
652 CPP_SET_WRITTEN (pfile
, old_written
);
656 /* Remove the definition of a symbol from the symbol table.
657 According to the C standard, it is not an error to undef
658 something that has no definitions. */
666 long here
= CPP_WRITTEN (pfile
);
667 enum cpp_ttype token
;
669 pfile
->no_macro_expand
++;
670 token
= _cpp_get_directive_token (pfile
);
671 pfile
->no_macro_expand
--;
673 if (token
!= CPP_NAME
)
675 cpp_error (pfile
, "token after #undef is not an identifier");
676 _cpp_skip_rest_of_line (pfile
);
679 len
= CPP_WRITTEN (pfile
) - here
;
681 token
= _cpp_get_directive_token (pfile
);
682 if (token
!= CPP_VSPACE
)
684 cpp_pedwarn (pfile
, "junk on line after #undef");
685 _cpp_skip_rest_of_line (pfile
);
688 name
= pfile
->token_buffer
+ here
;
689 CPP_SET_WRITTEN (pfile
, here
);
691 hp
= cpp_lookup (pfile
, name
, len
);
692 if (hp
->type
== T_VOID
)
693 ; /* Not defined in the first place - do nothing. */
694 else if (hp
->type
== T_POISON
)
695 cpp_error (pfile
, "cannot undefine poisoned \"%s\"", hp
->name
);
698 /* If we are generating additional info for debugging (with -g) we
699 need to pass through all effective #undef commands. */
700 if (CPP_OPTION (pfile
, debug_output
))
701 pass_thru_directive (hp
->name
, len
, pfile
, T_UNDEF
);
703 if (hp
->type
!= T_MACRO
&& hp
->type
!= T_FMACRO
704 && hp
->type
!= T_EMPTY
&& hp
->type
!= T_IDENTITY
)
705 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
707 _cpp_free_definition (hp
);
715 * Report an error detected by the program we are processing.
716 * Use the text of the line in the error message.
717 * (We use error because it prints the filename & line#.)
724 const U_CHAR
*text
, *limit
;
726 _cpp_skip_hspace (pfile
);
727 text
= CPP_BUFFER (pfile
)->cur
;
728 _cpp_skip_rest_of_line (pfile
);
729 limit
= CPP_BUFFER (pfile
)->cur
;
731 cpp_error (pfile
, "#error %.*s", (int)(limit
- text
), text
);
736 * Report a warning detected by the program we are processing.
737 * Use the text of the line in the warning message, then continue.
744 const U_CHAR
*text
, *limit
;
746 _cpp_skip_hspace (pfile
);
747 text
= CPP_BUFFER (pfile
)->cur
;
748 _cpp_skip_rest_of_line (pfile
);
749 limit
= CPP_BUFFER (pfile
)->cur
;
751 cpp_warning (pfile
, "#warning %.*s", (int)(limit
- text
), text
);
755 /* Report program identification. */
761 long old_written
= CPP_WRITTEN (pfile
);
763 CPP_PUTS (pfile
, "#ident ", 7);
765 /* Next token should be a string constant. */
766 if (_cpp_get_directive_token (pfile
) == CPP_STRING
)
767 /* And then a newline. */
768 if (_cpp_get_directive_token (pfile
) == CPP_VSPACE
)
769 /* Good - ship it. */
772 cpp_error (pfile
, "invalid #ident");
773 _cpp_skip_rest_of_line (pfile
);
774 CPP_SET_WRITTEN (pfile
, old_written
); /* discard directive */
779 /* Pragmata handling. We handle some of these, and pass the rest on
780 to the front end. C99 defines three pragmas and says that no macro
781 expansion is to be performed on them; whether or not macro
782 expansion happens for other pragmas is implementation defined.
783 This implementation never macro-expands the text after #pragma.
785 We currently do not support the _Pragma operator. Support for that
786 has to be coordinated with the front end. Proposed implementation:
787 both #pragma blah blah and _Pragma("blah blah") become
788 __builtin_pragma(blah blah) and we teach the parser about that. */
790 /* Sub-handlers for the pragmas needing treatment here.
791 They return 1 if the token buffer is to be popped, 0 if not. */
792 static int do_pragma_once
PARAMS ((cpp_reader
*));
793 static int do_pragma_implementation
PARAMS ((cpp_reader
*));
794 static int do_pragma_poison
PARAMS ((cpp_reader
*));
795 static int do_pragma_system_header
PARAMS ((cpp_reader
*));
796 static int do_pragma_default
PARAMS ((cpp_reader
*));
805 enum cpp_ttype token
;
807 here
= CPP_WRITTEN (pfile
);
808 CPP_PUTS (pfile
, "#pragma ", 8);
810 key
= CPP_WRITTEN (pfile
);
811 pfile
->no_macro_expand
++;
812 token
= _cpp_get_directive_token (pfile
);
813 if (token
!= CPP_NAME
)
815 if (token
== CPP_VSPACE
)
821 buf
= pfile
->token_buffer
+ key
;
822 CPP_PUTC (pfile
, ' ');
824 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
826 pop
= do_pragma_once (pfile
);
827 else if (tokis ("implementation"))
828 pop
= do_pragma_implementation (pfile
);
829 else if (tokis ("poison"))
830 pop
= do_pragma_poison (pfile
);
831 else if (tokis ("system_header"))
832 pop
= do_pragma_system_header (pfile
);
834 pop
= do_pragma_default (pfile
);
837 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
841 CPP_SET_WRITTEN (pfile
, here
);
842 pfile
->no_macro_expand
--;
846 cpp_error (pfile
, "malformed #pragma directive");
847 _cpp_skip_rest_of_line (pfile
);
849 CPP_SET_WRITTEN (pfile
, here
);
850 pfile
->no_macro_expand
--;
855 do_pragma_default (pfile
)
858 while (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
859 CPP_PUTC (pfile
, ' ');
864 do_pragma_once (pfile
)
867 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
869 /* Allow #pragma once in system headers, since that's not the user's
871 if (!ip
->system_header_p
)
872 cpp_warning (pfile
, "#pragma once is obsolete");
874 if (CPP_PREV_BUFFER (ip
) == NULL
)
875 cpp_warning (pfile
, "#pragma once outside include file");
877 ip
->ihash
->cmacro
= NEVER_REINCLUDE
;
883 do_pragma_implementation (pfile
)
886 /* Be quiet about `#pragma implementation' for a file only if it hasn't
887 been included yet. */
888 enum cpp_ttype token
;
889 long written
= CPP_WRITTEN (pfile
);
894 token
= _cpp_get_directive_token (pfile
);
895 if (token
== CPP_VSPACE
)
897 else if (token
!= CPP_STRING
)
899 cpp_error (pfile
, "malformed #pragma implementation");
903 /* Trim the leading and trailing quote marks from the string. */
904 name
= pfile
->token_buffer
+ written
+ 1;
905 len
= CPP_PWRITTEN (pfile
) - name
;
907 memcpy (copy
, name
, len
- 1);
908 copy
[len
- 1] = '\0';
910 if (cpp_included (pfile
, copy
))
912 "#pragma implementation for %s appears after file is included",
918 do_pragma_poison (pfile
)
921 /* Poison these symbols so that all subsequent usage produces an
927 enum cpp_ttype token
;
930 /* As a rule, don't include #pragma poison commands in output,
931 unless the user asks for them. */
932 writeit
= (CPP_OPTION (pfile
, debug_output
)
933 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
934 || CPP_OPTION (pfile
, dump_macros
) == dump_names
);
938 written
= CPP_WRITTEN (pfile
);
939 token
= _cpp_get_directive_token (pfile
);
940 if (token
== CPP_VSPACE
)
942 if (token
!= CPP_NAME
)
944 cpp_error (pfile
, "invalid #pragma poison directive");
945 _cpp_skip_rest_of_line (pfile
);
949 p
= pfile
->token_buffer
+ written
;
950 len
= CPP_PWRITTEN (pfile
) - p
;
951 hp
= cpp_lookup (pfile
, p
, len
);
952 if (hp
->type
== T_POISON
)
953 ; /* It is allowed to poison the same identifier twice. */
956 if (hp
->type
!= T_VOID
)
957 cpp_warning (pfile
, "poisoning existing macro `%s'", hp
->name
);
958 _cpp_free_definition (hp
);
962 CPP_PUTC (pfile
, ' ');
967 /* Mark the current header as a system header. This will suppress
968 some categories of warnings (notably those from -pedantic). It is
969 intended for use in system libraries that cannot be implemented in
970 conforming C, but cannot be certain that their headers appear in a
971 system include directory. To prevent abuse, it is rejected in the
972 primary source file. */
974 do_pragma_system_header (pfile
)
977 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
978 if (CPP_PREV_BUFFER (ip
) == NULL
)
979 cpp_warning (pfile
, "#pragma system_header outside include file");
981 ip
->system_header_p
= 1;
986 /* Just ignore #sccs, on systems where we define it at all. */
987 #ifdef SCCS_DIRECTIVE
992 _cpp_skip_rest_of_line (pfile
);
997 /* We've found an `#if' directive. If the only thing before it in
998 this file is white space, and if it is of the form
999 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1000 for inclusion of this file. (See redundant_include_p in cppfiles.c
1001 for an explanation of controlling macros.) If so, return the
1002 hash node for SYMBOL. Otherwise, return NULL. */
1004 static const cpp_hashnode
*
1005 detect_if_not_defined (pfile
)
1008 const cpp_hashnode
*cmacro
= 0;
1009 enum cpp_ttype token
;
1010 unsigned int base_offset
;
1011 unsigned int token_offset
;
1012 unsigned int need_rparen
= 0;
1013 unsigned int token_len
;
1015 if (pfile
->skipping
|| pfile
->only_seen_white
!= 2)
1018 /* Save state required for restore. */
1019 pfile
->no_macro_expand
++;
1020 CPP_SET_MARK (pfile
);
1021 base_offset
= CPP_WRITTEN (pfile
);
1024 if (_cpp_get_directive_token (pfile
) != CPP_OTHER
1025 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1026 || CPP_PWRITTEN (pfile
)[-1] != '!')
1029 /* ...then `defined', */
1030 token_offset
= CPP_WRITTEN (pfile
);
1031 token
= _cpp_get_directive_token (pfile
);
1032 if (token
!= CPP_NAME
)
1034 if (ustrncmp (pfile
->token_buffer
+ token_offset
, U
"defined", 7))
1037 /* ...then an optional '(' and the name, */
1038 token_offset
= CPP_WRITTEN (pfile
);
1039 token
= _cpp_get_directive_token (pfile
);
1040 if (token
== CPP_OPEN_PAREN
)
1042 token_offset
= CPP_WRITTEN (pfile
);
1044 token
= _cpp_get_directive_token (pfile
);
1046 if (token
!= CPP_NAME
)
1049 token_len
= CPP_WRITTEN (pfile
) - token_offset
;
1051 /* ...then the ')', if necessary, */
1052 if (need_rparen
&& _cpp_get_directive_token (pfile
) != CPP_CLOSE_PAREN
)
1055 /* ...and make sure there's nothing else on the line. */
1056 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1059 /* We have a legitimate controlling macro for this header. */
1060 cmacro
= cpp_lookup (pfile
, pfile
->token_buffer
+ token_offset
, token_len
);
1063 CPP_SET_WRITTEN (pfile
, base_offset
);
1064 pfile
->no_macro_expand
--;
1065 CPP_GOTO_MARK (pfile
);
1070 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1071 not defined; the macro tested is left in the token buffer (but
1074 static const cpp_hashnode
*
1075 parse_ifdef (pfile
, name
)
1081 enum cpp_ttype token
;
1082 long old_written
= CPP_WRITTEN (pfile
);
1083 const cpp_hashnode
*node
= 0;
1085 pfile
->no_macro_expand
++;
1086 token
= _cpp_get_directive_token (pfile
);
1087 pfile
->no_macro_expand
--;
1089 ident
= pfile
->token_buffer
+ old_written
;
1090 len
= CPP_WRITTEN (pfile
) - old_written
;
1092 if (token
== CPP_VSPACE
)
1094 if (! CPP_TRADITIONAL (pfile
))
1095 cpp_pedwarn (pfile
, "#%s with no argument", name
);
1098 else if (token
== CPP_NAME
)
1100 node
= cpp_lookup (pfile
, ident
, len
);
1104 if (! CPP_TRADITIONAL (pfile
))
1105 cpp_error (pfile
, "#%s with invalid argument", name
);
1108 if (!CPP_TRADITIONAL (pfile
))
1110 if (_cpp_get_directive_token (pfile
) == CPP_VSPACE
)
1113 cpp_pedwarn (pfile
, "garbage at end of #%s", name
);
1115 _cpp_skip_rest_of_line (pfile
);
1118 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1122 /* #ifdef is dead simple. */
1129 const cpp_hashnode
*node
= parse_ifdef (pfile
, dtable
[T_IFDEF
].name
);
1132 if (node
->type
== T_POISON
)
1133 cpp_error (pfile
, "attempt to use poisoned `%s'", node
->name
);
1135 def
= (node
->type
!= T_VOID
);
1137 push_conditional (pfile
, !def
, T_IFDEF
, 0);
1141 /* #ifndef is a tad more complex, because we need to check for a
1142 no-reinclusion wrapper. */
1150 const cpp_hashnode
*cmacro
;
1152 start_of_file
= pfile
->only_seen_white
== 2;
1153 cmacro
= parse_ifdef (pfile
, dtable
[T_IFNDEF
].name
);
1156 if (cmacro
->type
== T_POISON
)
1157 cpp_error (pfile
, "attempt to use poisoned `%s'", cmacro
->name
);
1159 def
= (cmacro
->type
!= T_VOID
);
1161 push_conditional (pfile
, def
, T_IFNDEF
,
1162 start_of_file
? cmacro
: 0);
1166 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1167 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1173 const cpp_hashnode
*cmacro
= 0;
1176 if (! pfile
->skipping
)
1178 cmacro
= detect_if_not_defined (pfile
);
1179 value
= _cpp_parse_expr (pfile
);
1181 push_conditional (pfile
, value
== 0, T_IF
, cmacro
);
1185 /* #else flips pfile->skipping and continues without changing
1186 if_stack; this is so that the error message for missing #endif's
1187 etc. will point to the original #if. */
1193 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1195 validate_else (pfile
, dtable
[T_ELSE
].name
);
1199 cpp_error (pfile
, "#else without #if");
1202 if (ifs
->type
== T_ELSE
)
1204 cpp_error (pfile
, "#else after #else");
1205 cpp_error_with_line (pfile
, ifs
->lineno
, 1, "the conditional began here");
1208 /* #ifndef can't have its special treatment for containing the whole file
1209 if it has a #else clause. */
1213 if (! ifs
->was_skipping
)
1215 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1216 succeeded, so we mustn't do the else block. */
1217 if (pfile
->skipping
< 2)
1218 pfile
->skipping
= ! pfile
->skipping
;
1224 * handle a #elif directive by not changing if_stack either.
1225 * see the comment above do_else.
1232 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1236 cpp_error (pfile
, "#elif without #if");
1239 if (ifs
->type
== T_ELSE
)
1241 cpp_error (pfile
, "#elif after #else");
1242 cpp_error_with_line (pfile
, ifs
->lineno
, 1, "the conditional began here");
1246 if (ifs
->was_skipping
)
1247 _cpp_skip_rest_of_line (pfile
);
1248 else if (pfile
->skipping
!= 1)
1250 _cpp_skip_rest_of_line (pfile
);
1251 pfile
->skipping
= 2; /* one block succeeded, so don't do any others */
1254 pfile
->skipping
= ! _cpp_parse_expr (pfile
);
1260 /* #endif pops the if stack and resets pfile->skipping. */
1266 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1268 validate_else (pfile
, dtable
[T_ENDIF
].name
);
1271 cpp_error (pfile
, "#endif without #if");
1274 CPP_BUFFER (pfile
)->if_stack
= ifs
->next
;
1275 pfile
->skipping
= ifs
->was_skipping
;
1276 pfile
->potential_control_macro
= ifs
->cmacro
;
1282 /* Push an if_stack entry and set pfile->skipping accordingly.
1283 If this is a #ifndef starting at the beginning of a file,
1284 CMACRO is the macro name tested by the #ifndef. */
1287 push_conditional (pfile
, skip
, type
, cmacro
)
1291 const cpp_hashnode
*cmacro
;
1293 struct if_stack
*ifs
;
1295 ifs
= (struct if_stack
*) xmalloc (sizeof (struct if_stack
));
1296 ifs
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1297 ifs
->next
= CPP_BUFFER (pfile
)->if_stack
;
1298 ifs
->cmacro
= cmacro
;
1299 ifs
->was_skipping
= pfile
->skipping
;
1302 if (!pfile
->skipping
)
1303 pfile
->skipping
= skip
;
1305 CPP_BUFFER (pfile
)->if_stack
= ifs
;
1308 /* Issue -pedantic warning for text which is not a comment following
1309 an #else or #endif. */
1312 validate_else (pfile
, directive
)
1314 const U_CHAR
*directive
;
1316 if (CPP_PEDANTIC (pfile
))
1318 long old_written
= CPP_WRITTEN (pfile
);
1319 pfile
->no_macro_expand
++;
1320 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1321 cpp_pedwarn (pfile
, "ISO C forbids text after #%s", directive
);
1322 CPP_SET_WRITTEN (pfile
, old_written
);
1323 pfile
->no_macro_expand
--;
1325 _cpp_skip_rest_of_line (pfile
);
1328 /* Called when we reach the end of a macro buffer. Walk back up the
1329 conditional stack till we reach its level at entry to this file,
1330 issuing error messages. Then force skipping off. */
1332 _cpp_unwind_if_stack (pfile
, pbuf
)
1336 struct if_stack
*ifs
, *nifs
;
1338 for (ifs
= pbuf
->if_stack
; ifs
; ifs
= nifs
)
1340 cpp_error_with_line (pfile
, ifs
->lineno
, 1, "unterminated #%s",
1341 dtable
[ifs
->type
].name
);
1345 pfile
->skipping
= 0;
1348 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1349 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1350 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1359 struct predicate
*pred
= 0;
1360 enum cpp_ttype type
;
1362 old_written
= CPP_WRITTEN (pfile
);
1363 pfile
->no_macro_expand
++;
1365 CPP_PUTC (pfile
, '#'); /* force token out of macro namespace */
1366 type
= _cpp_get_directive_token (pfile
);
1367 if (type
== CPP_VSPACE
)
1368 ERROR ("#assert without predicate");
1369 else if (type
!= CPP_NAME
)
1370 ERROR ("assertion predicate is not an identifier");
1372 sym
= pfile
->token_buffer
+ old_written
;
1373 len
= CPP_WRITTEN (pfile
) - old_written
;
1374 hp
= cpp_lookup (pfile
, sym
, len
);
1376 if (_cpp_get_directive_token (pfile
) != CPP_OPEN_PAREN
)
1377 ERROR ("missing token-sequence in #assert");
1379 pred
= (struct predicate
*) xmalloc (sizeof (struct predicate
));
1380 _cpp_init_toklist (&pred
->answer
, NO_DUMMY_TOKEN
);
1382 if (_cpp_scan_until (pfile
, &pred
->answer
, CPP_CLOSE_PAREN
)
1384 ERROR ("missing close paren in #assert");
1386 if (_cpp_get_directive_token (pfile
) != CPP_CLOSE_PAREN
)
1387 ICE ("impossible token, expecting ) in do_assert");
1389 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1390 ERROR ("junk at end of #assert");
1392 if (hp
->type
== T_ASSERTION
)
1394 /* Check for reassertion. */
1395 const struct predicate
*old
;
1397 for (old
= hp
->value
.pred
; old
; old
= old
->next
)
1398 if (_cpp_equiv_toklists (&pred
->answer
, &old
->answer
))
1399 /* We used to warn about this, but SVR4 cc doesn't, so let's
1400 match that (also consistent with #define). goto error will
1403 pred
->next
= hp
->value
.pred
;
1407 hp
->type
= T_ASSERTION
;
1411 _cpp_squeeze_toklist (&pred
->answer
);
1412 hp
->value
.pred
= pred
;
1413 pfile
->no_macro_expand
--;
1414 CPP_SET_WRITTEN (pfile
, old_written
);
1418 _cpp_skip_rest_of_line (pfile
);
1419 pfile
->no_macro_expand
--;
1420 CPP_SET_WRITTEN (pfile
, old_written
);
1423 _cpp_free_toklist (&pred
->answer
);
1438 enum cpp_ttype type
;
1441 old_written
= CPP_WRITTEN (pfile
);
1442 pfile
->no_macro_expand
++;
1444 CPP_PUTC (pfile
, '#'); /* force token out of macro namespace */
1445 if (_cpp_get_directive_token (pfile
) != CPP_NAME
)
1446 ERROR ("#unassert must be followed by an identifier");
1448 sym
= pfile
->token_buffer
+ old_written
;
1449 len
= CPP_WRITTEN (pfile
) - old_written
;
1450 hp
= cpp_lookup (pfile
, sym
, len
);
1452 type
= _cpp_get_directive_token (pfile
);
1453 if (type
== CPP_OPEN_PAREN
)
1456 _cpp_init_toklist (&ans
, NO_DUMMY_TOKEN
);
1458 if (_cpp_scan_until (pfile
, &ans
, CPP_CLOSE_PAREN
)
1460 ERROR ("missing close paren in #unassert");
1462 if (_cpp_get_directive_token (pfile
) != CPP_CLOSE_PAREN
)
1463 ICE ("impossible token, expecting ) in do_unassert");
1465 type
= _cpp_get_directive_token (pfile
);
1468 if (type
!= CPP_VSPACE
)
1469 ERROR ("junk at end of #unassert");
1471 if (hp
->type
!= T_ASSERTION
)
1472 /* Not an error to #unassert something that isn't asserted.
1473 goto error to clean up. */
1478 /* Find this specific answer and remove it. */
1479 struct predicate
*o
, *p
;
1481 for (p
= NULL
, o
= hp
->value
.pred
; o
; p
= o
, o
= o
->next
)
1482 if (_cpp_equiv_toklists (&ans
, &o
->answer
))
1487 hp
->value
.pred
= o
->next
;
1489 _cpp_free_toklist (&o
->answer
);
1496 struct predicate
*o
, *p
;
1497 for (o
= hp
->value
.pred
; o
; o
= p
)
1500 _cpp_free_toklist ((cpp_toklist
*) &o
->answer
);
1503 hp
->value
.pred
= NULL
;
1506 if (hp
->value
.pred
== NULL
)
1507 hp
->type
= T_VOID
; /* Last answer for this predicate deleted. */
1510 _cpp_skip_rest_of_line (pfile
);
1511 pfile
->no_macro_expand
--;
1512 CPP_SET_WRITTEN (pfile
, old_written
);
1514 _cpp_free_toklist (&ans
);
1518 /* These are for -D, -U, -A. */
1520 /* Process the string STR as if it appeared as the body of a #define.
1521 If STR is just an identifier, define it with value 1.
1522 If STR has anything after the identifier, then it should
1523 be identifier=definition. */
1526 cpp_define (pfile
, str
)
1533 p
= strchr (str
, '=');
1534 /* Copy the entire option so we can modify it.
1535 Change the first "=" in the string to a space. If there is none,
1536 tack " 1" on the end. Then add a newline and a NUL. */
1540 count
= strlen (str
) + 2;
1541 buf
= (char *) alloca (count
);
1542 memcpy (buf
, str
, count
- 2);
1544 buf
[count
- 2] = '\n';
1545 buf
[count
- 1] = '\0';
1549 count
= strlen (str
) + 4;
1550 buf
= (char *) alloca (count
);
1551 memcpy (buf
, str
, count
- 4);
1552 strcpy (&buf
[count
-4], " 1\n");
1555 if (cpp_push_buffer (pfile
, (U_CHAR
*)buf
, count
- 1) != NULL
)
1558 cpp_pop_buffer (pfile
);
1562 /* Process MACRO as if it appeared as the body of an #undef. */
1564 cpp_undef (pfile
, macro
)
1568 /* Copy the string so we can append a newline. */
1569 size_t len
= strlen (macro
);
1570 char *buf
= (char *) alloca (len
+ 2);
1571 memcpy (buf
, macro
, len
);
1573 buf
[len
+ 1] = '\0';
1574 if (cpp_push_buffer (pfile
, (U_CHAR
*)buf
, len
+ 1) != NULL
)
1577 cpp_pop_buffer (pfile
);
1581 /* Process the string STR as if it appeared as the body of a #assert. */
1583 cpp_assert (pfile
, str
)
1587 if (cpp_push_buffer (pfile
, (const U_CHAR
*)str
, strlen (str
)) != NULL
)
1590 cpp_pop_buffer (pfile
);
1594 /* Process STR as if it appeared as the body of an #unassert. */
1596 cpp_unassert (pfile
, str
)
1600 if (cpp_push_buffer (pfile
, (const U_CHAR
*)str
, strlen (str
)) != NULL
)
1602 do_unassert (pfile
);
1603 cpp_pop_buffer (pfile
);
1607 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1609 cpp_defined (pfile
, id
, len
)
1614 cpp_hashnode
*hp
= cpp_lookup (pfile
, id
, len
);
1615 if (hp
->type
== T_POISON
)
1617 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1620 return (hp
->type
!= T_VOID
);