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. */
32 # include <sys/mman.h>
35 /* Stack of conditionals currently in progress
36 (including both successful and failing conditionals). */
40 struct if_stack
*next
;
41 unsigned int lineno
; /* line number where condition started */
42 unsigned int colno
; /* and column */
43 int was_skipping
; /* value of pfile->skipping before this if */
44 const cpp_hashnode
*cmacro
; /* macro name for #ifndef around entire file */
45 int type
; /* type of last directive seen in this group */
48 /* Forward declarations. */
50 static void validate_else
PARAMS ((cpp_reader
*, const U_CHAR
*));
51 static int parse_include
PARAMS ((cpp_reader
*, const U_CHAR
*, int,
52 const U_CHAR
**, unsigned int *,
54 static void push_conditional
PARAMS ((cpp_reader
*, int, int,
55 const cpp_hashnode
*));
56 static void pass_thru_directive
PARAMS ((cpp_reader
*));
57 static int read_line_number
PARAMS ((cpp_reader
*, int *));
58 static int strtoul_for_line
PARAMS ((const U_CHAR
*, unsigned int,
61 static const cpp_hashnode
*
62 parse_ifdef
PARAMS ((cpp_reader
*, const U_CHAR
*));
63 static const cpp_hashnode
*
64 detect_if_not_defined
PARAMS ((cpp_reader
*));
66 get_define_node
PARAMS ((cpp_reader
*));
67 static void dump_macro_name
PARAMS ((cpp_reader
*, cpp_hashnode
*));
68 static void unwind_if_stack
PARAMS ((cpp_reader
*, cpp_buffer
*));
71 #define str_match(sym, len, str) \
72 ((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
74 /* This is the table of directive handlers. It is ordered by
75 frequency of occurrence; the numbers at the end are directive
76 counts from all the source code I have lying around (egcs and libc
77 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
80 The entries with a dash and a name after the count are extensions,
81 of which all but #warning and #include_next are deprecated. The name
82 is where the extension appears to have come from. */
84 /* #sccs is not always recognized. */
86 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
88 # define SCCS_ENTRY /* nothing */
91 #define DIRECTIVE_TABLE \
92 D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \
93 D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
94 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
95 D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
96 D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
97 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
98 D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
99 D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \
100 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
101 D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
102 D(error, T_ERROR, STDC89, 0) /* 475 */ \
103 D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \
104 D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
105 D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
106 D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \
107 D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
108 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
109 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
110 SCCS_ENTRY /* 0 SVR2? */
112 /* Use the table to generate a series of prototypes, an enum for the
113 directive names, and an array of directive handlers. */
115 /* The directive-processing functions are declared to return int
116 instead of void, because some old compilers have trouble with
117 pointers to functions returning void. */
119 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
120 #define D(name, t, o, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
124 #define D(n, tag, o, f) tag,
132 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
133 #define D(name, t, origin, flags) \
134 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
135 sizeof STRINGX(name) - 1, origin, flags },
136 static const struct directive dtable
[] =
141 #undef DIRECTIVE_TABLE
143 /* Check if a token's name matches that of a known directive. Put in
144 this file to save exporting dtable and other unneeded information. */
145 const struct directive
*
146 _cpp_check_directive (pfile
, token
, bol
)
148 const cpp_token
*token
;
151 const U_CHAR
*name
= token
->val
.name
.text
;
152 size_t len
= token
->val
.name
.len
;
155 /* If we are rescanning preprocessed input, don't obey any directives
157 if (CPP_OPTION (pfile
, preprocessed
))
160 for (i
= 0; i
< N_DIRECTIVES
; i
++)
161 if (dtable
[i
].length
== len
&& !memcmp (dtable
[i
].name
, name
, len
))
163 /* If we are skipping a failed conditional group, all non-conditional
164 directives are ignored. */
165 if (pfile
->skipping
&& !(dtable
[i
].flags
& COND
))
168 /* In -traditional mode, a directive is ignored unless its #
170 if (!bol
&& dtable
[i
].origin
== KANDR
&& CPP_WTRADITIONAL (pfile
))
171 cpp_warning (pfile
, "traditional C ignores #%s with the # indented",
174 if (!bol
&& CPP_TRADITIONAL (pfile
))
177 /* Issue -pedantic warnings for extended directives. */
178 if (CPP_PEDANTIC (pfile
) && dtable
[i
].origin
== EXTENSION
)
179 cpp_pedwarn (pfile
, "ISO C does not allow #%s", dtable
[i
].name
);
181 /* -Wtraditional gives warnings about directives with inappropriate
183 if (bol
&& dtable
[i
].origin
!= KANDR
&& CPP_WTRADITIONAL (pfile
))
185 "suggest hiding #%s from traditional C with an indented #",
194 const struct directive
*
195 _cpp_check_linemarker (pfile
, token
, bol
)
197 const cpp_token
*token ATTRIBUTE_UNUSED
;
200 /* # followed by a number is equivalent to #line. Do not recognize
201 this form in assembly language source files or skipped
202 conditional groups. Complain about this form if we're being
203 pedantic, but not if this is regurgitated input (preprocessed or
204 fed back in by the C++ frontend). */
205 if (pfile
->skipping
|| CPP_OPTION (pfile
, lang_asm
))
208 if (CPP_PEDANTIC (pfile
) && CPP_BUFFER (pfile
)->inc
209 && ! CPP_OPTION (pfile
, preprocessed
))
210 cpp_pedwarn (pfile
, "# followed by integer");
212 /* In -traditional mode, a directive is ignored unless its #
214 if (!bol
&& CPP_WTRADITIONAL (pfile
))
215 cpp_warning (pfile
, "traditional C ignores #%s with the # indented",
216 dtable
[T_LINE
].name
);
218 if (!bol
&& CPP_TRADITIONAL (pfile
))
221 return &dtable
[T_LINE
];
225 dump_macro_name (pfile
, node
)
229 CPP_PUTS (pfile
, "#define ", sizeof "#define " - 1);
230 CPP_PUTS (pfile
, node
->name
, node
->length
);
233 /* Pass the current directive through to the output file. */
235 pass_thru_directive (pfile
)
238 /* XXX This output may be genuinely needed even when there is no
240 if (! pfile
->printer
)
242 /* Flush first (temporary). */
243 cpp_output_tokens (pfile
, pfile
->printer
, pfile
->token_list
.line
);
244 _cpp_dump_list (pfile
, &pfile
->token_list
, pfile
->first_directive_token
, 1);
247 static cpp_hashnode
*
248 get_define_node (pfile
)
252 const cpp_token
*token
;
256 /* Skip any -C comments. */
257 while ((token
= cpp_get_token (pfile
))->type
== CPP_COMMENT
)
260 if (token
->type
!= CPP_NAME
)
262 cpp_error_with_line (pfile
, pfile
->token_list
.line
, token
->col
,
263 "macro names must be identifiers");
267 /* That identifier is not allowed to be "defined". See predefined
268 macro names (6.10.8.4). */
269 len
= token
->val
.name
.len
;
270 sym
= token
->val
.name
.text
;
271 if (str_match (sym
, len
, "defined"))
273 cpp_error_with_line (pfile
, pfile
->token_list
.line
, token
->col
,
274 "\"defined\" is not a legal macro name");
278 node
= cpp_lookup (pfile
, sym
, len
);
279 /* Check for poisoned identifiers now. */
280 if (node
->type
== T_POISON
)
282 cpp_error (pfile
, "attempt to use poisoned \"%.*s\"", (int) len
, sym
);
289 /* Process a #define command. */
296 if ((node
= get_define_node (pfile
)))
297 if (_cpp_create_definition (pfile
, node
))
299 if (CPP_OPTION (pfile
, debug_output
)
300 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
)
301 _cpp_dump_definition (pfile
, node
);
302 else if (CPP_OPTION (pfile
, dump_macros
) == dump_names
)
303 dump_macro_name (pfile
, node
);
308 /* Remove the definition of a symbol from the symbol table. */
313 cpp_hashnode
*node
= get_define_node (pfile
);
315 if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
316 cpp_pedwarn (pfile
, "junk on line after #undef");
318 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
319 is not currently defined as a macro name. */
320 if (node
&& node
->type
!= T_VOID
)
322 /* If we are generating additional info for debugging (with -g) we
323 need to pass through all effective #undef commands. */
324 if (CPP_OPTION (pfile
, debug_output
)
325 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
326 || CPP_OPTION (pfile
, dump_macros
) == dump_names
)
327 pass_thru_directive (pfile
);
329 if (node
->type
!= T_MACRO
)
330 cpp_warning (pfile
, "undefining \"%s\"", node
->name
);
332 _cpp_free_definition (node
);
340 /* Handle #include and #import. */
343 parse_include (pfile
, dir
, trail
, strp
, lenp
, abp
)
351 const cpp_token
*name
= cpp_get_token (pfile
);
353 if (name
->type
!= CPP_STRING
&& name
->type
!= CPP_HEADER_NAME
)
355 if (name
->type
== CPP_LESS
)
356 name
= _cpp_glue_header_name (pfile
);
359 cpp_error (pfile
, "#%s expects \"FILENAME\" or <FILENAME>", dir
);
363 if (name
->val
.name
.len
== 0)
365 cpp_error (pfile
, "empty file name in #%s", dir
);
369 if (!trail
&& cpp_get_token (pfile
)->type
!= CPP_EOF
)
370 cpp_error (pfile
, "junk at end of #%s", dir
);
372 *lenp
= name
->val
.name
.len
;
373 *strp
= name
->val
.name
.text
;
374 *abp
= (name
->type
== CPP_HEADER_NAME
);
386 if (parse_include (pfile
, dtable
[T_INCLUDE
].name
, 0, &str
, &len
, &ab
))
389 _cpp_execute_include (pfile
, str
, len
, 0, 0, ab
);
390 if (CPP_OPTION (pfile
, dump_includes
))
391 pass_thru_directive (pfile
);
403 if (CPP_OPTION (pfile
, warn_import
)
404 && !CPP_IN_SYSTEM_HEADER (pfile
) && !pfile
->import_warning
)
406 pfile
->import_warning
= 1;
408 "#import is obsolete, use an #ifndef wrapper in the header file");
411 if (parse_include (pfile
, dtable
[T_IMPORT
].name
, 0, &str
, &len
, &ab
))
414 _cpp_execute_include (pfile
, str
, len
, 1, 0, ab
);
415 if (CPP_OPTION (pfile
, dump_includes
))
416 pass_thru_directive (pfile
);
421 do_include_next (pfile
)
426 struct file_name_list
*search_start
= 0;
429 if (parse_include (pfile
, dtable
[T_INCLUDE_NEXT
].name
, 0, &str
, &len
, &ab
))
432 /* For #include_next, skip in the search path past the dir in which
433 the current file was found. If this is the last directory in the
434 search path, don't include anything. If the current file was
435 specified with an absolute path, use the normal search logic. If
436 this is the primary source file, use the normal search logic and
437 generate a warning. */
438 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)))
440 if (CPP_BUFFER (pfile
)->inc
->foundhere
)
442 search_start
= CPP_BUFFER (pfile
)->inc
->foundhere
->next
;
448 cpp_warning (pfile
, "#include_next in primary source file");
450 _cpp_execute_include (pfile
, str
, len
, 0, search_start
, ab
);
451 if (CPP_OPTION (pfile
, dump_includes
))
452 pass_thru_directive (pfile
);
457 /* Subroutine of do_line. Read next token from PFILE without adding it to
458 the output buffer. If it is a number between 1 and 4, store it in *NUM
459 and return 1; otherwise, return 0 and complain if we aren't at the end
463 read_line_number (pfile
, num
)
467 const cpp_token
*tok
= cpp_get_token (pfile
);
468 enum cpp_ttype type
= tok
->type
;
469 const U_CHAR
*p
= tok
->val
.name
.text
;
470 unsigned int len
= tok
->val
.name
.len
;
472 if (type
== CPP_NUMBER
&& len
== 1 && p
[0] >= '1' && p
[0] <= '4')
480 cpp_error (pfile
, "invalid format #line");
485 /* Another subroutine of do_line. Convert a number in STR, of length
486 LEN, to binary; store it in NUMP, and return 0 if the number was
487 legal, 1 if not. Temporary, hopefully. */
489 strtoul_for_line (str
, len
, nump
)
494 unsigned long reg
= 0;
508 /* Interpret #line command.
509 Note that the filename string (if any) is treated as if it were an
510 include filename. That means no escape handling. */
516 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
517 unsigned long new_lineno
, old_lineno
;
518 /* C99 raised the minimum limit on #line numbers. */
519 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
520 int action_number
= 0;
525 const cpp_token
*tok
;
527 tok
= cpp_get_token (pfile
);
529 str
= tok
->val
.name
.text
;
530 len
= tok
->val
.name
.len
;
532 if (type
!= CPP_NUMBER
|| strtoul_for_line (str
, len
, &new_lineno
))
534 cpp_error (pfile
, "token after #line is not a positive integer");
538 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
539 cpp_pedwarn (pfile
, "line number out of range");
541 old_lineno
= ip
->lineno
;
542 ip
->lineno
= new_lineno
;
543 tok
= cpp_get_token (pfile
);
545 str
= tok
->val
.name
.text
;
546 len
= tok
->val
.name
.len
;
550 else if (type
!= CPP_STRING
)
552 cpp_error (pfile
, "second token after #line is not a string");
553 ip
->lineno
= old_lineno
; /* malformed #line should have no effect */
557 fname
= alloca (len
+ 1);
558 memcpy (fname
, str
, len
);
561 if (strcmp (fname
, ip
->nominal_fname
))
563 if (!strcmp (fname
, ip
->inc
->name
))
564 ip
->nominal_fname
= ip
->inc
->name
;
566 ip
->nominal_fname
= _cpp_fake_include (pfile
, fname
);
569 if (read_line_number (pfile
, &action_number
) == 0)
572 if (CPP_PEDANTIC (pfile
))
573 cpp_pedwarn (pfile
, "garbage at end of #line");
575 /* This is somewhat questionable: change the buffer stack
576 depth so that output_line_command thinks we've stacked
578 if (action_number
== 1)
580 pfile
->buffer_stack_depth
++;
581 cpp_make_system_header (pfile
, ip
, 0);
582 read_line_number (pfile
, &action_number
);
584 else if (action_number
== 2)
586 pfile
->buffer_stack_depth
--;
587 cpp_make_system_header (pfile
, ip
, 0);
588 read_line_number (pfile
, &action_number
);
590 if (action_number
== 3)
592 cpp_make_system_header (pfile
, ip
, 1);
593 read_line_number (pfile
, &action_number
);
595 if (action_number
== 4)
597 cpp_make_system_header (pfile
, ip
, 2);
598 read_line_number (pfile
, &action_number
);
607 * Report an error detected by the program we are processing.
608 * Use the text of the line in the error message.
609 * (We use error because it prints the filename & line#.)
616 U_CHAR
*text
, *limit
;
619 _cpp_dump_list (pfile
, &pfile
->token_list
, pfile
->first_directive_token
, 0);
620 limit
= pfile
->limit
;
622 cpp_error (pfile
, "%.*s", (int)(limit
- text
), text
);
628 * Report a warning detected by the program we are processing.
629 * Use the text of the line in the warning message, then continue.
636 U_CHAR
*text
, *limit
;
639 _cpp_dump_list (pfile
, &pfile
->token_list
, pfile
->first_directive_token
, 0);
640 limit
= pfile
->limit
;
642 cpp_warning (pfile
, "%.*s", (int)(limit
- text
), text
);
646 /* Report program identification. */
652 /* Next token should be a string constant. */
653 if (cpp_get_token (pfile
)->type
== CPP_STRING
)
654 /* And then a newline. */
655 if (cpp_get_token (pfile
)->type
== CPP_EOF
)
657 /* Good - ship it. */
658 pass_thru_directive (pfile
);
662 cpp_error (pfile
, "invalid #ident");
666 /* Pragmata handling. We handle some of these, and pass the rest on
667 to the front end. C99 defines three pragmas and says that no macro
668 expansion is to be performed on them; whether or not macro
669 expansion happens for other pragmas is implementation defined.
670 This implementation never macro-expands the text after #pragma.
672 We currently do not support the _Pragma operator. Support for that
673 has to be coordinated with the front end. Proposed implementation:
674 both #pragma blah blah and _Pragma("blah blah") become
675 __builtin_pragma(blah blah) and we teach the parser about that. */
677 /* Sub-handlers for the pragmas needing treatment here.
678 They return 1 if the token buffer is to be popped, 0 if not. */
682 int (*handler
) PARAMS ((cpp_reader
*));
685 static int pragma_dispatch
686 PARAMS ((cpp_reader
*, const struct pragma_entry
*,
687 const U_CHAR
*, size_t));
688 static int do_pragma_once
PARAMS ((cpp_reader
*));
689 static int do_pragma_implementation
PARAMS ((cpp_reader
*));
690 static int do_pragma_poison
PARAMS ((cpp_reader
*));
691 static int do_pragma_system_header
PARAMS ((cpp_reader
*));
692 static int do_pragma_gcc
PARAMS ((cpp_reader
*));
693 static int do_pragma_dependency
PARAMS ((cpp_reader
*));
695 static const struct pragma_entry top_pragmas
[] =
697 {"once", do_pragma_once
},
698 {"implementation", do_pragma_implementation
},
699 {"poison", do_pragma_poison
},
700 {"GCC", do_pragma_gcc
},
704 static const struct pragma_entry gcc_pragmas
[] =
706 {"implementation", do_pragma_implementation
},
707 {"poison", do_pragma_poison
},
708 {"system_header", do_pragma_system_header
},
709 {"dependency", do_pragma_dependency
},
713 static int pragma_dispatch (pfile
, table
, p
, len
)
715 const struct pragma_entry
*table
;
719 for (; table
->name
; table
++)
720 if (strlen (table
->name
) == len
&& !memcmp (p
, table
->name
, len
))
721 return (*table
->handler
) (pfile
);
729 const cpp_token
*tok
;
732 tok
= cpp_get_token (pfile
);
733 if (tok
->type
== CPP_EOF
)
735 else if (tok
->type
!= CPP_NAME
)
737 cpp_error (pfile
, "malformed #pragma directive");
741 pop
= pragma_dispatch (pfile
, top_pragmas
,
742 tok
->val
.name
.text
, tok
->val
.name
.len
);
744 pass_thru_directive (pfile
);
749 do_pragma_gcc (pfile
)
752 const cpp_token
*tok
;
754 tok
= cpp_get_token (pfile
);
755 if (tok
->type
== CPP_EOF
)
757 else if (tok
->type
!= CPP_NAME
)
760 return pragma_dispatch (pfile
, gcc_pragmas
,
761 tok
->val
.name
.text
, tok
->val
.name
.len
);
765 do_pragma_once (pfile
)
768 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
770 /* Allow #pragma once in system headers, since that's not the user's
772 if (!CPP_IN_SYSTEM_HEADER (pfile
))
773 cpp_warning (pfile
, "#pragma once is obsolete");
775 if (CPP_PREV_BUFFER (ip
) == NULL
)
776 cpp_warning (pfile
, "#pragma once outside include file");
778 ip
->inc
->cmacro
= NEVER_REREAD
;
784 do_pragma_implementation (pfile
)
787 /* Be quiet about `#pragma implementation' for a file only if it hasn't
788 been included yet. */
789 const cpp_token
*tok
= cpp_get_token (pfile
);
792 if (tok
->type
== CPP_EOF
)
794 else if (tok
->type
!= CPP_STRING
795 || cpp_get_token (pfile
)->type
!= CPP_EOF
)
797 cpp_error (pfile
, "malformed #pragma implementation");
801 /* Make a NUL-terminated copy of the string. */
802 copy
= alloca (tok
->val
.name
.len
+ 1);
803 memcpy (copy
, tok
->val
.name
.text
, tok
->val
.name
.len
);
804 copy
[tok
->val
.name
.len
] = '\0';
806 if (cpp_included (pfile
, copy
))
808 "#pragma implementation for %s appears after file is included",
814 do_pragma_poison (pfile
)
817 /* Poison these symbols so that all subsequent usage produces an
819 const cpp_token
*tok
;
823 /* As a rule, don't include #pragma poison commands in output,
824 unless the user asks for them. */
825 writeit
= (CPP_OPTION (pfile
, debug_output
)
826 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
827 || CPP_OPTION (pfile
, dump_macros
) == dump_names
);
831 tok
= cpp_get_token (pfile
);
832 if (tok
->type
== CPP_EOF
)
834 if (tok
->type
!= CPP_NAME
)
836 cpp_error (pfile
, "invalid #pragma poison directive");
840 hp
= cpp_lookup (pfile
, tok
->val
.name
.text
, tok
->val
.name
.len
);
841 if (hp
->type
== T_POISON
)
842 ; /* It is allowed to poison the same identifier twice. */
845 if (hp
->type
!= T_VOID
)
846 cpp_warning (pfile
, "poisoning existing macro \"%s\"", hp
->name
);
847 _cpp_free_definition (hp
);
854 /* Mark the current header as a system header. This will suppress
855 some categories of warnings (notably those from -pedantic). It is
856 intended for use in system libraries that cannot be implemented in
857 conforming C, but cannot be certain that their headers appear in a
858 system include directory. To prevent abuse, it is rejected in the
859 primary source file. */
861 do_pragma_system_header (pfile
)
864 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
865 if (CPP_PREV_BUFFER (ip
) == NULL
)
866 cpp_warning (pfile
, "#pragma system_header outside include file");
868 cpp_make_system_header (pfile
, ip
, 1);
873 /* Check the modified date of the current include file against a specified
874 file. Issue a diagnostic, if the specified file is newer. We use this to
875 determine if a fixed header should be refixed. */
877 do_pragma_dependency (pfile
)
885 if (parse_include (pfile
, U
"pragma dependency", 1, &name
, &len
, &ab
))
888 left
= ab
? '<' : '"';
889 right
= ab
? '>' : '"';
891 ordering
= _cpp_compare_file_date (pfile
, name
, len
, ab
);
893 cpp_warning (pfile
, "cannot find source %c%s%c", left
, name
, right
);
894 else if (ordering
> 0)
896 const cpp_token
*msg
= cpp_get_token (pfile
);
898 cpp_warning (pfile
, "current file is older than %c%s%c",
900 if (msg
->type
!= CPP_EOF
)
902 U_CHAR
*text
, *limit
;
905 _cpp_dump_list (pfile
, &pfile
->token_list
, msg
, 0);
906 limit
= pfile
->limit
;
908 cpp_warning (pfile
, "%.*s", (int)(limit
- text
), text
);
914 /* Just ignore #sccs, on systems where we define it at all. */
915 #ifdef SCCS_DIRECTIVE
918 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
924 /* We've found an `#if' directive. If the only thing before it in
925 this file is white space, and if it is of the form
926 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
927 for inclusion of this file. (See redundant_include_p in cppfiles.c
928 for an explanation of controlling macros.) If so, return the
929 hash node for SYMBOL. Otherwise, return NULL. */
931 static const cpp_hashnode
*
932 detect_if_not_defined (pfile
)
935 const cpp_token
*token
;
936 cpp_hashnode
*cmacro
= 0;
938 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
939 token
= pfile
->first_directive_token
+ 2;
941 if (token
->type
!= CPP_NOT
)
945 if (token
->type
!= CPP_NAME
946 || !str_match (token
->val
.name
.text
, token
->val
.name
.len
, "defined"))
950 if (token
->type
== CPP_OPEN_PAREN
)
953 if (token
->type
!= CPP_NAME
)
956 cmacro
= cpp_lookup (pfile
, token
->val
.name
.text
, token
->val
.name
.len
);
958 if (token
[-1].type
== CPP_OPEN_PAREN
)
961 if (token
->type
!= CPP_CLOSE_PAREN
)
966 if (token
->type
!= CPP_EOF
)
972 /* Parse an #ifdef or #ifndef directive. Returns the hash node of the
973 macro being tested, and issues various error messages. */
975 static const cpp_hashnode
*
976 parse_ifdef (pfile
, name
)
983 const cpp_hashnode
*node
= 0;
985 const cpp_token
*token
= cpp_get_token (pfile
);
987 ident
= token
->val
.name
.text
;
988 len
= token
->val
.name
.len
;
990 if (!CPP_TRADITIONAL (pfile
))
993 cpp_pedwarn (pfile
, "#%s with no argument", name
);
994 else if (type
!= CPP_NAME
)
995 cpp_pedwarn (pfile
, "#%s with invalid argument", name
);
996 else if (cpp_get_token (pfile
)->type
!= CPP_EOF
)
997 cpp_pedwarn (pfile
, "garbage at end of #%s", name
);
1000 if (type
== CPP_NAME
)
1001 node
= cpp_lookup (pfile
, ident
, len
);
1002 if (node
&& node
->type
== T_POISON
)
1003 cpp_error (pfile
, "attempt to use poisoned identifier \"%s\"", node
->name
);
1008 /* #ifdef is dead simple. */
1015 const cpp_hashnode
*node
= 0;
1017 if (! pfile
->skipping
)
1019 node
= parse_ifdef (pfile
, dtable
[T_IFDEF
].name
);
1021 def
= (node
->type
!= T_VOID
&& node
->type
!= T_POISON
);
1024 push_conditional (pfile
, !def
, T_IFDEF
, 0);
1028 /* #ifndef is a tad more complex, because we need to check for a
1029 no-reinclusion wrapper. */
1035 int start_of_file
= 0;
1037 const cpp_hashnode
*cmacro
= 0;
1039 if (! pfile
->skipping
)
1041 start_of_file
= (pfile
->token_list
.flags
& BEG_OF_FILE
);
1042 cmacro
= parse_ifdef (pfile
, dtable
[T_IFNDEF
].name
);
1044 def
= cmacro
->type
!= T_VOID
;
1047 push_conditional (pfile
, def
, T_IFNDEF
, start_of_file
? cmacro
: 0);
1051 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1052 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1058 const cpp_hashnode
*cmacro
= 0;
1061 if (! pfile
->skipping
)
1063 cmacro
= detect_if_not_defined (pfile
);
1064 value
= _cpp_parse_expr (pfile
);
1066 push_conditional (pfile
, value
== 0, T_IF
, cmacro
);
1070 /* #else flips pfile->skipping and continues without changing
1071 if_stack; this is so that the error message for missing #endif's
1072 etc. will point to the original #if. */
1078 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1079 validate_else (pfile
, dtable
[T_ELSE
].name
);
1083 cpp_error (pfile
, "#else without #if");
1086 if (ifs
->type
== T_ELSE
)
1088 cpp_error (pfile
, "#else after #else");
1089 cpp_error_with_line (pfile
, ifs
->lineno
, ifs
->colno
,
1090 "the conditional began here");
1093 /* #ifndef can't have its special treatment for containing the whole file
1094 if it has a #else clause. */
1097 if (! ifs
->was_skipping
)
1099 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1100 succeeded, so we mustn't do the else block. */
1101 if (pfile
->skipping
< 2)
1102 pfile
->skipping
= ! pfile
->skipping
;
1108 * handle a #elif directive by not changing if_stack either.
1109 * see the comment above do_else.
1116 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1120 cpp_error (pfile
, "#elif without #if");
1123 if (ifs
->type
== T_ELSE
)
1125 cpp_error (pfile
, "#elif after #else");
1126 cpp_error_with_line (pfile
, ifs
->lineno
, ifs
->colno
,
1127 "the conditional began here");
1131 if (ifs
->was_skipping
)
1132 return 0; /* Don't evaluate a nested #if */
1134 if (pfile
->skipping
!= 1)
1136 pfile
->skipping
= 2; /* one block succeeded, so don't do any others */
1140 pfile
->skipping
= ! _cpp_parse_expr (pfile
);
1144 /* #endif pops the if stack and resets pfile->skipping. */
1150 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1152 validate_else (pfile
, dtable
[T_ENDIF
].name
);
1155 cpp_error (pfile
, "#endif without #if");
1158 CPP_BUFFER (pfile
)->if_stack
= ifs
->next
;
1159 pfile
->skipping
= ifs
->was_skipping
;
1160 pfile
->potential_control_macro
= ifs
->cmacro
;
1161 obstack_free (pfile
->buffer_ob
, ifs
);
1167 /* Push an if_stack entry and set pfile->skipping accordingly.
1168 If this is a #ifndef starting at the beginning of a file,
1169 CMACRO is the macro name tested by the #ifndef. */
1172 push_conditional (pfile
, skip
, type
, cmacro
)
1176 const cpp_hashnode
*cmacro
;
1178 struct if_stack
*ifs
;
1180 ifs
= xobnew (pfile
->buffer_ob
, struct if_stack
);
1181 ifs
->lineno
= _cpp_get_line (pfile
, &ifs
->colno
);
1182 ifs
->next
= CPP_BUFFER (pfile
)->if_stack
;
1183 ifs
->cmacro
= cmacro
;
1184 ifs
->was_skipping
= pfile
->skipping
;
1187 if (!pfile
->skipping
)
1188 pfile
->skipping
= skip
;
1190 CPP_BUFFER (pfile
)->if_stack
= ifs
;
1193 /* Issue -pedantic warning for text which is not a comment following
1194 an #else or #endif. */
1197 validate_else (pfile
, directive
)
1199 const U_CHAR
*directive
;
1201 if (CPP_PEDANTIC (pfile
) && cpp_get_token (pfile
)->type
!= CPP_EOF
)
1202 cpp_pedwarn (pfile
, "ISO C forbids text after #%s", directive
);
1205 /* Called when we reach the end of a file. Walk back up the
1206 conditional stack till we reach its level at entry to this file,
1207 issuing error messages. Then force skipping off. */
1209 unwind_if_stack (pfile
, pbuf
)
1213 struct if_stack
*ifs
, *nifs
;
1215 for (ifs
= pbuf
->if_stack
; ifs
; ifs
= nifs
)
1217 cpp_error_with_line (pfile
, ifs
->lineno
, ifs
->colno
, "unterminated #%s",
1218 dtable
[ifs
->type
].name
);
1220 /* No need to free - they'll all go away with the buffer. */
1222 pfile
->skipping
= 0;
1225 /* Parses an assertion, returning a pointer to the hash node of the
1226 predicate, or 0 on error. If an answer was supplied, it is
1227 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1228 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1229 in a #if statement (we may be in a macro), and we don't want to
1232 _cpp_parse_assertion (pfile
, answerp
)
1234 struct answer
**answerp
;
1236 struct answer
*answer
= 0;
1239 const cpp_token
*token
, *predicate
;
1240 const struct directive
*d
= pfile
->token_list
.directive
;
1241 unsigned int len
= 0;
1243 predicate
= _cpp_get_raw_token (pfile
);
1244 if (predicate
->type
== CPP_EOF
)
1246 cpp_error (pfile
, "assertion without predicate");
1249 else if (predicate
->type
!= CPP_NAME
)
1251 cpp_error (pfile
, "predicate must be an identifier");
1255 token
= _cpp_get_raw_token (pfile
);
1256 if (token
->type
!= CPP_OPEN_PAREN
)
1258 /* #unassert and #if are OK without predicate. */
1259 if (d
== &dtable
[T_UNASSERT
])
1261 if (token
->type
== CPP_EOF
)
1264 else if (d
!= &dtable
[T_ASSERT
])
1266 _cpp_push_token (pfile
, token
);
1269 cpp_error (pfile
, "missing '(' after predicate");
1273 /* Allocate a struct answer, and copy the answer to it. */
1274 answer
= (struct answer
*) xmalloc (sizeof (struct answer
));
1275 list
= &answer
->list
;
1276 _cpp_init_toklist (list
, NO_DUMMY_TOKEN
);
1282 token
= _cpp_get_raw_token (pfile
);
1284 if (token
->type
== CPP_EOF
)
1286 cpp_error (pfile
, "missing ')' to complete answer");
1289 if (token
->type
== CPP_CLOSE_PAREN
)
1292 /* Copy the token. */
1293 _cpp_expand_token_space (list
, 1);
1294 dest
= &list
->tokens
[list
->tokens_used
++];
1297 if (token_spellings
[token
->type
].type
> SPELL_NONE
)
1299 _cpp_expand_name_space (list
, token
->val
.name
.len
);
1300 dest
->val
.name
.text
= list
->namebuf
+ list
->name_used
;
1301 memcpy (list
->namebuf
+ list
->name_used
,
1302 token
->val
.name
.text
, token
->val
.name
.len
);
1303 list
->name_used
+= token
->val
.name
.len
;
1307 if (list
->tokens_used
== 0)
1309 cpp_error (pfile
, "predicate's answer is empty");
1313 /* Drop whitespace at start. */
1314 list
->tokens
[0].flags
&= ~PREV_WHITE
;
1316 if ((d
== &dtable
[T_ASSERT
] || d
== &dtable
[T_UNASSERT
])
1317 && token
[1].type
!= CPP_EOF
)
1319 cpp_error (pfile
, "junk at end of assertion");
1325 len
= predicate
->val
.name
.len
;
1326 sym
= alloca (len
+ 1);
1328 /* Prefix '#' to get it out of macro namespace. */
1330 memcpy (sym
+ 1, predicate
->val
.name
.text
, len
);
1331 return cpp_lookup (pfile
, sym
, len
+ 1);
1334 FREE_ANSWER (answer
);
1338 /* Returns a pointer to the pointer to the answer in the answer chain,
1339 or a pointer to NULL if the answer is not in the chain. */
1341 _cpp_find_answer (node
, candidate
)
1343 const cpp_toklist
*candidate
;
1345 struct answer
**result
;
1347 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1348 if (_cpp_equiv_toklists (&(*result
)->list
, candidate
))
1354 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1355 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1356 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1361 struct answer
*new_answer
;
1364 node
= _cpp_parse_assertion (pfile
, &new_answer
);
1367 new_answer
->next
= 0;
1368 new_answer
->list
.line
= pfile
->token_list
.line
;
1369 new_answer
->list
.file
= pfile
->token_list
.file
;
1371 if (node
->type
== T_ASSERTION
)
1373 if (*_cpp_find_answer (node
, &new_answer
->list
))
1375 new_answer
->next
= node
->value
.answers
;
1377 node
->type
= T_ASSERTION
;
1378 node
->value
.answers
= new_answer
;
1383 cpp_warning (pfile
, "\"%.*s\" re-asserted",
1384 node
->length
- 1, node
->name
+ 1);
1385 FREE_ANSWER (new_answer
);
1394 struct answer
*answer
, *temp
, *next
;
1396 node
= _cpp_parse_assertion (pfile
, &answer
);
1399 /* It isn't an error to #unassert something that isn't asserted. */
1400 if (node
->type
== T_ASSERTION
)
1404 struct answer
**p
= _cpp_find_answer (node
, &answer
->list
);
1412 if (node
->value
.answers
== 0)
1413 node
->type
= T_VOID
;
1417 for (temp
= node
->value
.answers
; temp
; temp
= next
)
1422 node
->type
= T_VOID
;
1427 FREE_ANSWER (answer
);
1432 /* These are for -D, -U, -A. */
1434 /* Process the string STR as if it appeared as the body of a #define.
1435 If STR is just an identifier, define it with value 1.
1436 If STR has anything after the identifier, then it should
1437 be identifier=definition. */
1440 cpp_define (pfile
, str
)
1447 p
= strchr (str
, '=');
1448 /* Copy the entire option so we can modify it.
1449 Change the first "=" in the string to a space. If there is none,
1450 tack " 1" on the end. Then add a newline and a NUL. */
1454 count
= strlen (str
) + 2;
1455 buf
= (char *) alloca (count
);
1456 memcpy (buf
, str
, count
- 2);
1458 buf
[count
- 2] = '\n';
1459 buf
[count
- 1] = '\0';
1463 count
= strlen (str
) + 4;
1464 buf
= (char *) alloca (count
);
1465 memcpy (buf
, str
, count
- 4);
1466 strcpy (&buf
[count
-4], " 1\n");
1469 _cpp_run_directive (pfile
, &dtable
[T_DEFINE
], buf
, count
- 1);
1472 /* Process MACRO as if it appeared as the body of an #undef. */
1474 cpp_undef (pfile
, macro
)
1478 _cpp_run_directive (pfile
, &dtable
[T_UNDEF
], macro
, strlen (macro
));
1481 /* Process the string STR as if it appeared as the body of a #assert. */
1483 cpp_assert (pfile
, str
)
1487 _cpp_run_directive (pfile
, &dtable
[T_ASSERT
], str
, strlen (str
));
1490 /* Process STR as if it appeared as the body of an #unassert. */
1492 cpp_unassert (pfile
, str
)
1496 _cpp_run_directive (pfile
, &dtable
[T_UNASSERT
], str
, strlen (str
));
1499 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1501 cpp_defined (pfile
, id
, len
)
1506 cpp_hashnode
*hp
= cpp_lookup (pfile
, id
, len
);
1507 if (hp
->type
== T_POISON
)
1509 cpp_error (pfile
, "attempt to use poisoned \"%s\"", hp
->name
);
1512 return (hp
->type
!= T_VOID
);
1515 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1516 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1517 as the new input buffer.
1518 Return the new buffer, or NULL on failure. */
1521 cpp_push_buffer (pfile
, buffer
, length
)
1523 const U_CHAR
*buffer
;
1526 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1528 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
1530 cpp_fatal (pfile
, "#include recursion too deep");
1534 new = xobnew (pfile
->buffer_ob
, cpp_buffer
);
1535 memset (new, 0, sizeof (cpp_buffer
));
1537 new->buf
= new->cur
= buffer
;
1538 new->rlimit
= buffer
+ length
;
1541 CPP_BUFFER (pfile
) = new;
1546 cpp_pop_buffer (pfile
)
1549 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1551 unwind_if_stack (pfile
, buf
);
1552 #ifdef HAVE_MMAP_FILE
1554 munmap ((caddr_t
) buf
->buf
, buf
->rlimit
- buf
->buf
);
1558 free ((PTR
) buf
->buf
);
1562 if (pfile
->system_include_depth
)
1563 pfile
->system_include_depth
--;
1564 if (pfile
->include_depth
)
1565 pfile
->include_depth
--;
1566 if (pfile
->potential_control_macro
)
1568 if (buf
->inc
->cmacro
!= NEVER_REREAD
)
1569 buf
->inc
->cmacro
= pfile
->potential_control_macro
;
1570 pfile
->potential_control_macro
= 0;
1572 pfile
->input_stack_listing_current
= 0;
1573 /* If the file will not be included again, then close it. */
1574 if (DO_NOT_REREAD (buf
->inc
))
1576 close (buf
->inc
->fd
);
1581 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
1582 obstack_free (pfile
->buffer_ob
, buf
);
1583 pfile
->buffer_stack_depth
--;
1584 return CPP_BUFFER (pfile
);
1587 #define obstack_chunk_alloc xmalloc
1588 #define obstack_chunk_free free
1590 _cpp_init_stacks (pfile
)
1593 pfile
->buffer_ob
= xnew (struct obstack
);
1594 obstack_init (pfile
->buffer_ob
);
1598 _cpp_cleanup_stacks (pfile
)
1601 obstack_free (pfile
->buffer_ob
, 0);
1602 free (pfile
->buffer_ob
);