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
;
153 /* If we are rescanning preprocessed input, don't obey any directives
155 if (CPP_OPTION (pfile
, preprocessed
))
158 for (i
= 0; i
< N_DIRECTIVES
; i
++)
159 if (pfile
->spec_nodes
->dirs
[i
] == token
->val
.node
)
161 /* If we are skipping a failed conditional group, all non-conditional
162 directives are ignored. */
163 if (pfile
->skipping
&& !(dtable
[i
].flags
& COND
))
166 /* In -traditional mode, a directive is ignored unless its #
168 if (!bol
&& dtable
[i
].origin
== KANDR
&& CPP_WTRADITIONAL (pfile
))
169 cpp_warning (pfile
, "traditional C ignores #%s with the # indented",
172 if (!bol
&& CPP_TRADITIONAL (pfile
))
175 /* Issue -pedantic warnings for extended directives. */
176 if (CPP_PEDANTIC (pfile
) && dtable
[i
].origin
== EXTENSION
)
177 cpp_pedwarn (pfile
, "ISO C does not allow #%s", dtable
[i
].name
);
179 /* -Wtraditional gives warnings about directives with inappropriate
181 if (bol
&& dtable
[i
].origin
!= KANDR
&& CPP_WTRADITIONAL (pfile
))
183 "suggest hiding #%s from traditional C with an indented #",
192 const struct directive
*
193 _cpp_check_linemarker (pfile
, token
, bol
)
195 const cpp_token
*token ATTRIBUTE_UNUSED
;
198 /* # followed by a number is equivalent to #line. Do not recognize
199 this form in assembly language source files or skipped
200 conditional groups. Complain about this form if we're being
201 pedantic, but not if this is regurgitated input (preprocessed or
202 fed back in by the C++ frontend). */
203 if (pfile
->skipping
|| CPP_OPTION (pfile
, lang_asm
))
206 if (CPP_PEDANTIC (pfile
) && CPP_BUFFER (pfile
)->inc
207 && ! CPP_OPTION (pfile
, preprocessed
))
208 cpp_pedwarn (pfile
, "# followed by integer");
210 /* In -traditional mode, a directive is ignored unless its #
212 if (!bol
&& CPP_WTRADITIONAL (pfile
))
213 cpp_warning (pfile
, "traditional C ignores #%s with the # indented",
214 dtable
[T_LINE
].name
);
216 if (!bol
&& CPP_TRADITIONAL (pfile
))
219 return &dtable
[T_LINE
];
223 dump_macro_name (pfile
, node
)
227 CPP_PUTS (pfile
, "#define ", sizeof "#define " - 1);
228 CPP_PUTS (pfile
, node
->name
, node
->length
);
231 /* Pass the current directive through to the output file. */
233 pass_thru_directive (pfile
)
236 /* XXX This output may be genuinely needed even when there is no
238 if (! pfile
->printer
)
240 /* Flush first (temporary). */
241 cpp_output_tokens (pfile
, pfile
->printer
, pfile
->token_list
.line
);
242 _cpp_dump_list (pfile
, &pfile
->token_list
, pfile
->first_directive_token
, 1);
245 static cpp_hashnode
*
246 get_define_node (pfile
)
250 const cpp_token
*token
;
252 /* Skip any -C comments. */
253 while ((token
= _cpp_get_token (pfile
))->type
== CPP_COMMENT
)
256 if (token
->type
!= CPP_NAME
)
258 cpp_error_with_line (pfile
, token
->line
, token
->col
,
259 "macro names must be identifiers");
263 /* That identifier is not allowed to be "defined". See predefined
264 macro names (6.10.8.4). */
265 node
= token
->val
.node
;
267 if (node
== pfile
->spec_nodes
->n_defined
)
269 cpp_error_with_line (pfile
, pfile
->token_list
.line
, token
->col
,
270 "\"defined\" is not a legal macro name");
274 /* Check for poisoned identifiers now. */
275 if (node
->type
== T_POISON
)
277 cpp_error (pfile
, "attempt to use poisoned \"%s\"", node
->name
);
284 /* Process a #define command. */
291 if ((node
= get_define_node (pfile
)))
292 if (_cpp_create_definition (pfile
, node
))
294 if (CPP_OPTION (pfile
, debug_output
)
295 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
)
296 _cpp_dump_definition (pfile
, node
);
297 else if (CPP_OPTION (pfile
, dump_macros
) == dump_names
)
298 dump_macro_name (pfile
, node
);
303 /* Remove the definition of a symbol from the symbol table. */
308 cpp_hashnode
*node
= get_define_node (pfile
);
310 if (_cpp_get_token (pfile
)->type
!= CPP_EOF
)
311 cpp_pedwarn (pfile
, "junk on line after #undef");
313 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
314 is not currently defined as a macro name. */
315 if (node
&& node
->type
!= T_VOID
)
317 /* If we are generating additional info for debugging (with -g) we
318 need to pass through all effective #undef commands. */
319 if (CPP_OPTION (pfile
, debug_output
)
320 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
321 || CPP_OPTION (pfile
, dump_macros
) == dump_names
)
322 pass_thru_directive (pfile
);
324 if (node
->type
!= T_MACRO
)
325 cpp_warning (pfile
, "undefining \"%s\"", node
->name
);
327 _cpp_free_definition (node
);
335 /* Handle #include and #import. */
338 parse_include (pfile
, dir
, trail
, strp
, lenp
, abp
)
346 const cpp_token
*name
= _cpp_get_token (pfile
);
348 if (name
->type
!= CPP_STRING
&& name
->type
!= CPP_HEADER_NAME
)
350 if (name
->type
== CPP_LESS
)
351 name
= _cpp_glue_header_name (pfile
);
354 cpp_error (pfile
, "#%s expects \"FILENAME\" or <FILENAME>", dir
);
358 if (name
->val
.str
.len
== 0)
360 cpp_error (pfile
, "empty file name in #%s", dir
);
364 if (!trail
&& _cpp_get_token (pfile
)->type
!= CPP_EOF
)
365 cpp_error (pfile
, "junk at end of #%s", dir
);
367 *lenp
= name
->val
.str
.len
;
368 *strp
= name
->val
.str
.text
;
369 *abp
= (name
->type
== CPP_HEADER_NAME
);
381 if (parse_include (pfile
, dtable
[T_INCLUDE
].name
, 0, &str
, &len
, &ab
))
384 _cpp_execute_include (pfile
, str
, len
, 0, 0, ab
);
385 if (CPP_OPTION (pfile
, dump_includes
))
386 pass_thru_directive (pfile
);
398 if (CPP_OPTION (pfile
, warn_import
)
399 && !CPP_IN_SYSTEM_HEADER (pfile
) && !pfile
->import_warning
)
401 pfile
->import_warning
= 1;
403 "#import is obsolete, use an #ifndef wrapper in the header file");
406 if (parse_include (pfile
, dtable
[T_IMPORT
].name
, 0, &str
, &len
, &ab
))
409 _cpp_execute_include (pfile
, str
, len
, 1, 0, ab
);
410 if (CPP_OPTION (pfile
, dump_includes
))
411 pass_thru_directive (pfile
);
416 do_include_next (pfile
)
421 struct file_name_list
*search_start
= 0;
424 if (parse_include (pfile
, dtable
[T_INCLUDE_NEXT
].name
, 0, &str
, &len
, &ab
))
427 /* For #include_next, skip in the search path past the dir in which
428 the current file was found. If this is the last directory in the
429 search path, don't include anything. If the current file was
430 specified with an absolute path, use the normal search logic. If
431 this is the primary source file, use the normal search logic and
432 generate a warning. */
433 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)))
435 if (CPP_BUFFER (pfile
)->inc
->foundhere
)
437 search_start
= CPP_BUFFER (pfile
)->inc
->foundhere
->next
;
443 cpp_warning (pfile
, "#include_next in primary source file");
445 _cpp_execute_include (pfile
, str
, len
, 0, search_start
, ab
);
446 if (CPP_OPTION (pfile
, dump_includes
))
447 pass_thru_directive (pfile
);
452 /* Subroutine of do_line. Read next token from PFILE without adding it to
453 the output buffer. If it is a number between 1 and 4, store it in *NUM
454 and return 1; otherwise, return 0 and complain if we aren't at the end
458 read_line_number (pfile
, num
)
462 const cpp_token
*tok
= _cpp_get_token (pfile
);
463 enum cpp_ttype type
= tok
->type
;
464 const U_CHAR
*p
= tok
->val
.str
.text
;
465 unsigned int len
= tok
->val
.str
.len
;
467 if (type
== CPP_NUMBER
&& len
== 1 && p
[0] >= '1' && p
[0] <= '4')
475 cpp_error (pfile
, "invalid format #line");
480 /* Another subroutine of do_line. Convert a number in STR, of length
481 LEN, to binary; store it in NUMP, and return 0 if the number was
482 legal, 1 if not. Temporary, hopefully. */
484 strtoul_for_line (str
, len
, nump
)
489 unsigned long reg
= 0;
503 /* Interpret #line command.
504 Note that the filename string (if any) is treated as if it were an
505 include filename. That means no escape handling. */
511 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
512 unsigned long new_lineno
, old_lineno
;
513 /* C99 raised the minimum limit on #line numbers. */
514 unsigned int cap
= CPP_OPTION (pfile
, c99
) ? 2147483647 : 32767;
515 int action_number
= 0;
520 const cpp_token
*tok
;
522 tok
= _cpp_get_token (pfile
);
524 str
= tok
->val
.str
.text
;
525 len
= tok
->val
.str
.len
;
527 if (type
!= CPP_NUMBER
|| strtoul_for_line (str
, len
, &new_lineno
))
529 cpp_error (pfile
, "token after #line is not a positive integer");
533 if (CPP_PEDANTIC (pfile
) && (new_lineno
== 0 || new_lineno
> cap
))
534 cpp_pedwarn (pfile
, "line number out of range");
536 old_lineno
= ip
->lineno
;
537 ip
->lineno
= new_lineno
;
538 tok
= _cpp_get_token (pfile
);
540 str
= tok
->val
.str
.text
;
541 len
= tok
->val
.str
.len
;
545 else if (type
!= CPP_STRING
)
547 cpp_error (pfile
, "second token after #line is not a string");
548 ip
->lineno
= old_lineno
; /* malformed #line should have no effect */
552 fname
= alloca (len
+ 1);
553 memcpy (fname
, str
, len
);
556 if (strcmp (fname
, ip
->nominal_fname
))
558 if (!strcmp (fname
, ip
->inc
->name
))
559 ip
->nominal_fname
= ip
->inc
->name
;
561 ip
->nominal_fname
= _cpp_fake_include (pfile
, fname
);
564 if (read_line_number (pfile
, &action_number
) == 0)
567 if (CPP_PEDANTIC (pfile
))
568 cpp_pedwarn (pfile
, "garbage at end of #line");
570 /* This is somewhat questionable: change the buffer stack
571 depth so that output_line_command thinks we've stacked
573 if (action_number
== 1)
575 pfile
->buffer_stack_depth
++;
576 cpp_make_system_header (pfile
, ip
, 0);
577 read_line_number (pfile
, &action_number
);
579 else if (action_number
== 2)
581 pfile
->buffer_stack_depth
--;
582 cpp_make_system_header (pfile
, ip
, 0);
583 read_line_number (pfile
, &action_number
);
585 if (action_number
== 3)
587 cpp_make_system_header (pfile
, ip
, 1);
588 read_line_number (pfile
, &action_number
);
590 if (action_number
== 4)
592 cpp_make_system_header (pfile
, ip
, 2);
593 read_line_number (pfile
, &action_number
);
602 * Report an error detected by the program we are processing.
603 * Use the text of the line in the error message.
604 * (We use error because it prints the filename & line#.)
611 U_CHAR
*text
, *limit
;
614 _cpp_dump_list (pfile
, &pfile
->token_list
, pfile
->first_directive_token
, 0);
615 limit
= pfile
->limit
;
617 cpp_error (pfile
, "%.*s", (int)(limit
- text
), text
);
623 * Report a warning detected by the program we are processing.
624 * Use the text of the line in the warning message, then continue.
631 U_CHAR
*text
, *limit
;
634 _cpp_dump_list (pfile
, &pfile
->token_list
, pfile
->first_directive_token
, 0);
635 limit
= pfile
->limit
;
637 cpp_warning (pfile
, "%.*s", (int)(limit
- text
), text
);
641 /* Report program identification. */
647 /* Next token should be a string constant. */
648 if (_cpp_get_token (pfile
)->type
== CPP_STRING
)
649 /* And then a newline. */
650 if (_cpp_get_token (pfile
)->type
== CPP_EOF
)
652 /* Good - ship it. */
653 pass_thru_directive (pfile
);
657 cpp_error (pfile
, "invalid #ident");
661 /* Pragmata handling. We handle some of these, and pass the rest on
662 to the front end. C99 defines three pragmas and says that no macro
663 expansion is to be performed on them; whether or not macro
664 expansion happens for other pragmas is implementation defined.
665 This implementation never macro-expands the text after #pragma.
667 We currently do not support the _Pragma operator. Support for that
668 has to be coordinated with the front end. Proposed implementation:
669 both #pragma blah blah and _Pragma("blah blah") become
670 __builtin_pragma(blah blah) and we teach the parser about that. */
672 /* Sub-handlers for the pragmas needing treatment here.
673 They return 1 if the token buffer is to be popped, 0 if not. */
677 int (*handler
) PARAMS ((cpp_reader
*));
680 static int pragma_dispatch
681 PARAMS ((cpp_reader
*, const struct pragma_entry
*, const cpp_hashnode
*));
682 static int do_pragma_once
PARAMS ((cpp_reader
*));
683 static int do_pragma_implementation
PARAMS ((cpp_reader
*));
684 static int do_pragma_poison
PARAMS ((cpp_reader
*));
685 static int do_pragma_system_header
PARAMS ((cpp_reader
*));
686 static int do_pragma_gcc
PARAMS ((cpp_reader
*));
687 static int do_pragma_dependency
PARAMS ((cpp_reader
*));
689 static const struct pragma_entry top_pragmas
[] =
691 {"once", do_pragma_once
},
692 {"implementation", do_pragma_implementation
},
693 {"poison", do_pragma_poison
},
694 {"GCC", do_pragma_gcc
},
698 static const struct pragma_entry gcc_pragmas
[] =
700 {"implementation", do_pragma_implementation
},
701 {"poison", do_pragma_poison
},
702 {"system_header", do_pragma_system_header
},
703 {"dependency", do_pragma_dependency
},
707 static int pragma_dispatch (pfile
, table
, node
)
709 const struct pragma_entry
*table
;
710 const cpp_hashnode
*node
;
712 const U_CHAR
*p
= node
->name
;
713 size_t len
= node
->length
;
715 for (; table
->name
; table
++)
716 if (strlen (table
->name
) == len
&& !memcmp (p
, table
->name
, len
))
717 return (*table
->handler
) (pfile
);
725 const cpp_token
*tok
;
728 tok
= _cpp_get_token (pfile
);
729 if (tok
->type
== CPP_EOF
)
731 else if (tok
->type
!= CPP_NAME
)
733 cpp_error (pfile
, "malformed #pragma directive");
737 pop
= pragma_dispatch (pfile
, top_pragmas
, tok
->val
.node
);
739 pass_thru_directive (pfile
);
744 do_pragma_gcc (pfile
)
747 const cpp_token
*tok
;
749 tok
= _cpp_get_token (pfile
);
750 if (tok
->type
== CPP_EOF
)
752 else if (tok
->type
!= CPP_NAME
)
755 return pragma_dispatch (pfile
, gcc_pragmas
, tok
->val
.node
);
759 do_pragma_once (pfile
)
762 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
764 /* Allow #pragma once in system headers, since that's not the user's
766 if (!CPP_IN_SYSTEM_HEADER (pfile
))
767 cpp_warning (pfile
, "#pragma once is obsolete");
769 if (CPP_PREV_BUFFER (ip
) == NULL
)
770 cpp_warning (pfile
, "#pragma once outside include file");
772 ip
->inc
->cmacro
= NEVER_REREAD
;
778 do_pragma_implementation (pfile
)
781 /* Be quiet about `#pragma implementation' for a file only if it hasn't
782 been included yet. */
783 const cpp_token
*tok
= _cpp_get_token (pfile
);
786 if (tok
->type
== CPP_EOF
)
788 else if (tok
->type
!= CPP_STRING
789 || _cpp_get_token (pfile
)->type
!= CPP_EOF
)
791 cpp_error (pfile
, "malformed #pragma implementation");
795 /* Make a NUL-terminated copy of the string. */
796 copy
= alloca (tok
->val
.str
.len
+ 1);
797 memcpy (copy
, tok
->val
.str
.text
, tok
->val
.str
.len
);
798 copy
[tok
->val
.str
.len
] = '\0';
800 if (cpp_included (pfile
, copy
))
802 "#pragma implementation for %s appears after file is included",
808 do_pragma_poison (pfile
)
811 /* Poison these symbols so that all subsequent usage produces an
813 const cpp_token
*tok
;
817 /* As a rule, don't include #pragma poison commands in output,
818 unless the user asks for them. */
819 writeit
= (CPP_OPTION (pfile
, debug_output
)
820 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
821 || CPP_OPTION (pfile
, dump_macros
) == dump_names
);
825 tok
= _cpp_get_token (pfile
);
826 if (tok
->type
== CPP_EOF
)
828 if (tok
->type
!= CPP_NAME
)
830 cpp_error (pfile
, "invalid #pragma poison directive");
835 if (hp
->type
== T_POISON
)
836 ; /* It is allowed to poison the same identifier twice. */
839 if (hp
->type
!= T_VOID
)
840 cpp_warning (pfile
, "poisoning existing macro \"%s\"", hp
->name
);
841 _cpp_free_definition (hp
);
848 /* Mark the current header as a system header. This will suppress
849 some categories of warnings (notably those from -pedantic). It is
850 intended for use in system libraries that cannot be implemented in
851 conforming C, but cannot be certain that their headers appear in a
852 system include directory. To prevent abuse, it is rejected in the
853 primary source file. */
855 do_pragma_system_header (pfile
)
858 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
859 if (CPP_PREV_BUFFER (ip
) == NULL
)
860 cpp_warning (pfile
, "#pragma system_header outside include file");
862 cpp_make_system_header (pfile
, ip
, 1);
867 /* Check the modified date of the current include file against a specified
868 file. Issue a diagnostic, if the specified file is newer. We use this to
869 determine if a fixed header should be refixed. */
871 do_pragma_dependency (pfile
)
879 if (parse_include (pfile
, U
"pragma dependency", 1, &name
, &len
, &ab
))
882 left
= ab
? '<' : '"';
883 right
= ab
? '>' : '"';
885 ordering
= _cpp_compare_file_date (pfile
, name
, len
, ab
);
887 cpp_warning (pfile
, "cannot find source %c%s%c", left
, name
, right
);
888 else if (ordering
> 0)
890 const cpp_token
*msg
= _cpp_get_token (pfile
);
892 cpp_warning (pfile
, "current file is older than %c%s%c",
894 if (msg
->type
!= CPP_EOF
)
896 U_CHAR
*text
, *limit
;
899 _cpp_dump_list (pfile
, &pfile
->token_list
, msg
, 0);
900 limit
= pfile
->limit
;
902 cpp_warning (pfile
, "%.*s", (int)(limit
- text
), text
);
908 /* Just ignore #sccs, on systems where we define it at all. */
909 #ifdef SCCS_DIRECTIVE
912 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
918 /* We've found an `#if' directive. If the only thing before it in
919 this file is white space, and if it is of the form
920 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
921 for inclusion of this file. (See redundant_include_p in cppfiles.c
922 for an explanation of controlling macros.) If so, return the
923 hash node for SYMBOL. Otherwise, return NULL. */
925 static const cpp_hashnode
*
926 detect_if_not_defined (pfile
)
929 const cpp_token
*token
;
930 cpp_hashnode
*cmacro
= 0;
932 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
933 token
= pfile
->first_directive_token
+ 2;
935 if (token
->type
!= CPP_NOT
)
939 if (token
->type
!= CPP_NAME
940 || token
->val
.node
!= pfile
->spec_nodes
->n_defined
)
944 if (token
->type
== CPP_OPEN_PAREN
)
947 if (token
->type
!= CPP_NAME
)
950 cmacro
= token
->val
.node
;
952 if (token
[-1].type
== CPP_OPEN_PAREN
)
955 if (token
->type
!= CPP_CLOSE_PAREN
)
960 if (token
->type
!= CPP_EOF
)
966 /* Parse an #ifdef or #ifndef directive. Returns the hash node of the
967 macro being tested, and issues various error messages. */
969 static const cpp_hashnode
*
970 parse_ifdef (pfile
, name
)
975 const cpp_hashnode
*node
= 0;
977 const cpp_token
*token
= _cpp_get_token (pfile
);
980 if (!CPP_TRADITIONAL (pfile
))
983 cpp_pedwarn (pfile
, "#%s with no argument", name
);
984 else if (type
!= CPP_NAME
)
985 cpp_pedwarn (pfile
, "#%s with invalid argument", name
);
986 else if (_cpp_get_token (pfile
)->type
!= CPP_EOF
)
987 cpp_pedwarn (pfile
, "garbage at end of #%s", name
);
990 if (type
== CPP_NAME
)
991 node
= token
->val
.node
;
992 if (node
&& node
->type
== T_POISON
)
994 cpp_error (pfile
, "attempt to use poisoned identifier \"%s\"",
1002 /* #ifdef is dead simple. */
1008 const cpp_hashnode
*node
= 0;
1010 if (! pfile
->skipping
)
1011 node
= parse_ifdef (pfile
, dtable
[T_IFDEF
].name
);
1013 push_conditional (pfile
, !(node
&& node
->type
!= T_VOID
), T_IFDEF
, 0);
1017 /* #ifndef is a tad more complex, because we need to check for a
1018 no-reinclusion wrapper. */
1024 int start_of_file
= 0;
1025 const cpp_hashnode
*node
= 0;
1027 if (! pfile
->skipping
)
1029 start_of_file
= (pfile
->token_list
.flags
& BEG_OF_FILE
);
1030 node
= parse_ifdef (pfile
, dtable
[T_IFNDEF
].name
);
1033 push_conditional (pfile
, node
&& node
->type
!= T_VOID
,
1034 T_IFNDEF
, start_of_file
? node
: 0);
1038 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1039 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1045 const cpp_hashnode
*cmacro
= 0;
1048 if (! pfile
->skipping
)
1050 if (pfile
->token_list
.flags
& BEG_OF_FILE
)
1051 cmacro
= detect_if_not_defined (pfile
);
1052 value
= _cpp_parse_expr (pfile
);
1054 push_conditional (pfile
, value
== 0, T_IF
, cmacro
);
1058 /* #else flips pfile->skipping and continues without changing
1059 if_stack; this is so that the error message for missing #endif's
1060 etc. will point to the original #if. */
1066 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1067 validate_else (pfile
, dtable
[T_ELSE
].name
);
1071 cpp_error (pfile
, "#else without #if");
1074 if (ifs
->type
== T_ELSE
)
1076 cpp_error (pfile
, "#else after #else");
1077 cpp_error_with_line (pfile
, ifs
->lineno
, ifs
->colno
,
1078 "the conditional began here");
1081 /* #ifndef can't have its special treatment for containing the whole file
1082 if it has a #else clause. */
1085 if (! ifs
->was_skipping
)
1087 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1088 succeeded, so we mustn't do the else block. */
1089 if (pfile
->skipping
< 2)
1090 pfile
->skipping
= ! pfile
->skipping
;
1096 * handle a #elif directive by not changing if_stack either.
1097 * see the comment above do_else.
1104 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1108 cpp_error (pfile
, "#elif without #if");
1111 if (ifs
->type
== T_ELSE
)
1113 cpp_error (pfile
, "#elif after #else");
1114 cpp_error_with_line (pfile
, ifs
->lineno
, ifs
->colno
,
1115 "the conditional began here");
1119 if (ifs
->was_skipping
)
1120 return 0; /* Don't evaluate a nested #if */
1122 if (pfile
->skipping
!= 1)
1124 pfile
->skipping
= 2; /* one block succeeded, so don't do any others */
1128 pfile
->skipping
= ! _cpp_parse_expr (pfile
);
1132 /* #endif pops the if stack and resets pfile->skipping. */
1138 struct if_stack
*ifs
= CPP_BUFFER (pfile
)->if_stack
;
1140 validate_else (pfile
, dtable
[T_ENDIF
].name
);
1143 cpp_error (pfile
, "#endif without #if");
1146 CPP_BUFFER (pfile
)->if_stack
= ifs
->next
;
1147 pfile
->skipping
= ifs
->was_skipping
;
1148 pfile
->potential_control_macro
= ifs
->cmacro
;
1149 obstack_free (pfile
->buffer_ob
, ifs
);
1155 /* Push an if_stack entry and set pfile->skipping accordingly.
1156 If this is a #ifndef starting at the beginning of a file,
1157 CMACRO is the macro name tested by the #ifndef. */
1160 push_conditional (pfile
, skip
, type
, cmacro
)
1164 const cpp_hashnode
*cmacro
;
1166 struct if_stack
*ifs
;
1168 ifs
= xobnew (pfile
->buffer_ob
, struct if_stack
);
1169 ifs
->lineno
= _cpp_get_line (pfile
, &ifs
->colno
);
1170 ifs
->next
= CPP_BUFFER (pfile
)->if_stack
;
1171 ifs
->cmacro
= cmacro
;
1172 ifs
->was_skipping
= pfile
->skipping
;
1175 if (!pfile
->skipping
)
1176 pfile
->skipping
= skip
;
1178 CPP_BUFFER (pfile
)->if_stack
= ifs
;
1181 /* Issue -pedantic warning for text which is not a comment following
1182 an #else or #endif. */
1185 validate_else (pfile
, directive
)
1187 const U_CHAR
*directive
;
1189 if (CPP_PEDANTIC (pfile
) && _cpp_get_token (pfile
)->type
!= CPP_EOF
)
1190 cpp_pedwarn (pfile
, "ISO C forbids text after #%s", directive
);
1193 /* Called when we reach the end of a file. Walk back up the
1194 conditional stack till we reach its level at entry to this file,
1195 issuing error messages. Then force skipping off. */
1197 unwind_if_stack (pfile
, pbuf
)
1201 struct if_stack
*ifs
, *nifs
;
1203 for (ifs
= pbuf
->if_stack
; ifs
; ifs
= nifs
)
1205 cpp_error_with_line (pfile
, ifs
->lineno
, ifs
->colno
, "unterminated #%s",
1206 dtable
[ifs
->type
].name
);
1208 /* No need to free - they'll all go away with the buffer. */
1210 pfile
->skipping
= 0;
1213 /* Parses an assertion, returning a pointer to the hash node of the
1214 predicate, or 0 on error. If an answer was supplied, it is
1215 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1216 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1217 in a #if statement (we may be in a macro), and we don't want to
1220 _cpp_parse_assertion (pfile
, answerp
)
1222 struct answer
**answerp
;
1224 struct answer
*answer
= 0;
1227 const cpp_token
*token
, *predicate
;
1228 const struct directive
*d
= pfile
->token_list
.directive
;
1229 unsigned int len
= 0;
1231 predicate
= _cpp_get_raw_token (pfile
);
1232 if (predicate
->type
== CPP_EOF
)
1234 cpp_error (pfile
, "assertion without predicate");
1237 else if (predicate
->type
!= CPP_NAME
)
1239 cpp_error (pfile
, "predicate must be an identifier");
1243 token
= _cpp_get_raw_token (pfile
);
1244 if (token
->type
!= CPP_OPEN_PAREN
)
1246 /* #unassert and #if are OK without predicate. */
1247 if (d
== &dtable
[T_UNASSERT
])
1249 if (token
->type
== CPP_EOF
)
1252 else if (d
!= &dtable
[T_ASSERT
])
1254 _cpp_push_token (pfile
, token
);
1257 cpp_error (pfile
, "missing '(' after predicate");
1261 /* Allocate a struct answer, and copy the answer to it. */
1262 answer
= (struct answer
*) xmalloc (sizeof (struct answer
));
1263 list
= &answer
->list
;
1264 _cpp_init_toklist (list
, NO_DUMMY_TOKEN
);
1270 token
= _cpp_get_raw_token (pfile
);
1272 if (token
->type
== CPP_EOF
)
1274 cpp_error (pfile
, "missing ')' to complete answer");
1277 if (token
->type
== CPP_CLOSE_PAREN
)
1280 /* Copy the token. */
1281 _cpp_expand_token_space (list
, 1);
1282 dest
= &list
->tokens
[list
->tokens_used
++];
1285 if (token_spellings
[token
->type
].type
== SPELL_STRING
)
1287 _cpp_expand_name_space (list
, token
->val
.str
.len
);
1288 dest
->val
.str
.text
= list
->namebuf
+ list
->name_used
;
1289 memcpy (list
->namebuf
+ list
->name_used
,
1290 token
->val
.str
.text
, token
->val
.str
.len
);
1291 list
->name_used
+= token
->val
.str
.len
;
1295 if (list
->tokens_used
== 0)
1297 cpp_error (pfile
, "predicate's answer is empty");
1301 /* Drop whitespace at start. */
1302 list
->tokens
[0].flags
&= ~PREV_WHITE
;
1304 if ((d
== &dtable
[T_ASSERT
] || d
== &dtable
[T_UNASSERT
])
1305 && token
[1].type
!= CPP_EOF
)
1307 cpp_error (pfile
, "junk at end of assertion");
1313 len
= predicate
->val
.node
->length
;
1314 sym
= alloca (len
+ 1);
1316 /* Prefix '#' to get it out of macro namespace. */
1318 memcpy (sym
+ 1, predicate
->val
.node
->name
, len
);
1319 return cpp_lookup (pfile
, sym
, len
+ 1);
1322 FREE_ANSWER (answer
);
1326 /* Returns a pointer to the pointer to the answer in the answer chain,
1327 or a pointer to NULL if the answer is not in the chain. */
1329 _cpp_find_answer (node
, candidate
)
1331 const cpp_toklist
*candidate
;
1333 struct answer
**result
;
1335 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
1336 if (_cpp_equiv_toklists (&(*result
)->list
, candidate
))
1342 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1343 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1344 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1349 struct answer
*new_answer
;
1352 node
= _cpp_parse_assertion (pfile
, &new_answer
);
1355 new_answer
->next
= 0;
1356 new_answer
->list
.line
= pfile
->token_list
.line
;
1357 new_answer
->list
.file
= pfile
->token_list
.file
;
1359 if (node
->type
== T_ASSERTION
)
1361 if (*_cpp_find_answer (node
, &new_answer
->list
))
1363 new_answer
->next
= node
->value
.answers
;
1365 node
->type
= T_ASSERTION
;
1366 node
->value
.answers
= new_answer
;
1371 cpp_warning (pfile
, "\"%.*s\" re-asserted",
1372 node
->length
- 1, node
->name
+ 1);
1373 FREE_ANSWER (new_answer
);
1382 struct answer
*answer
, *temp
, *next
;
1384 node
= _cpp_parse_assertion (pfile
, &answer
);
1387 /* It isn't an error to #unassert something that isn't asserted. */
1388 if (node
->type
== T_ASSERTION
)
1392 struct answer
**p
= _cpp_find_answer (node
, &answer
->list
);
1400 if (node
->value
.answers
== 0)
1401 node
->type
= T_VOID
;
1405 for (temp
= node
->value
.answers
; temp
; temp
= next
)
1410 node
->type
= T_VOID
;
1415 FREE_ANSWER (answer
);
1420 /* These are for -D, -U, -A. */
1422 /* Process the string STR as if it appeared as the body of a #define.
1423 If STR is just an identifier, define it with value 1.
1424 If STR has anything after the identifier, then it should
1425 be identifier=definition. */
1428 cpp_define (pfile
, str
)
1435 p
= strchr (str
, '=');
1436 /* Copy the entire option so we can modify it.
1437 Change the first "=" in the string to a space. If there is none,
1438 tack " 1" on the end. Then add a newline and a NUL. */
1442 count
= strlen (str
) + 2;
1443 buf
= (char *) alloca (count
);
1444 memcpy (buf
, str
, count
- 2);
1446 buf
[count
- 2] = '\n';
1447 buf
[count
- 1] = '\0';
1451 count
= strlen (str
) + 4;
1452 buf
= (char *) alloca (count
);
1453 memcpy (buf
, str
, count
- 4);
1454 strcpy (&buf
[count
-4], " 1\n");
1457 _cpp_run_directive (pfile
, &dtable
[T_DEFINE
], buf
, count
- 1);
1460 /* Process MACRO as if it appeared as the body of an #undef. */
1462 cpp_undef (pfile
, macro
)
1466 _cpp_run_directive (pfile
, &dtable
[T_UNDEF
], macro
, strlen (macro
));
1469 /* Process the string STR as if it appeared as the body of a #assert. */
1471 cpp_assert (pfile
, str
)
1475 _cpp_run_directive (pfile
, &dtable
[T_ASSERT
], str
, strlen (str
));
1478 /* Process STR as if it appeared as the body of an #unassert. */
1480 cpp_unassert (pfile
, str
)
1484 _cpp_run_directive (pfile
, &dtable
[T_UNASSERT
], str
, strlen (str
));
1487 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1489 cpp_defined (pfile
, id
, len
)
1494 cpp_hashnode
*hp
= cpp_lookup (pfile
, id
, len
);
1495 if (hp
->type
== T_POISON
)
1497 cpp_error (pfile
, "attempt to use poisoned \"%s\"", hp
->name
);
1500 return (hp
->type
!= T_VOID
);
1503 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1504 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1505 as the new input buffer.
1506 Return the new buffer, or NULL on failure. */
1509 cpp_push_buffer (pfile
, buffer
, length
)
1511 const U_CHAR
*buffer
;
1514 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1516 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
1518 cpp_fatal (pfile
, "#include nested too deep");
1521 if (pfile
->cur_context
> 0)
1523 cpp_ice (pfile
, "buffer pushed with contexts stacked");
1524 _cpp_skip_rest_of_line (pfile
);
1527 new = xobnew (pfile
->buffer_ob
, cpp_buffer
);
1528 memset (new, 0, sizeof (cpp_buffer
));
1530 new->buf
= new->cur
= buffer
;
1531 new->rlimit
= buffer
+ length
;
1534 CPP_BUFFER (pfile
) = new;
1539 cpp_pop_buffer (pfile
)
1542 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1544 unwind_if_stack (pfile
, buf
);
1545 #ifdef HAVE_MMAP_FILE
1547 munmap ((caddr_t
) buf
->buf
, buf
->rlimit
- buf
->buf
);
1551 free ((PTR
) buf
->buf
);
1555 if (pfile
->system_include_depth
)
1556 pfile
->system_include_depth
--;
1557 if (pfile
->include_depth
)
1558 pfile
->include_depth
--;
1559 if (pfile
->potential_control_macro
)
1561 if (buf
->inc
->cmacro
!= NEVER_REREAD
)
1562 buf
->inc
->cmacro
= pfile
->potential_control_macro
;
1563 pfile
->potential_control_macro
= 0;
1565 pfile
->input_stack_listing_current
= 0;
1566 /* If the file will not be included again, then close it. */
1567 if (DO_NOT_REREAD (buf
->inc
))
1569 close (buf
->inc
->fd
);
1574 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
1575 obstack_free (pfile
->buffer_ob
, buf
);
1576 pfile
->buffer_stack_depth
--;
1577 return CPP_BUFFER (pfile
);
1580 #define obstack_chunk_alloc xmalloc
1581 #define obstack_chunk_free free
1582 #define DSC(x) U x, sizeof x - 1
1584 _cpp_init_stacks (pfile
)
1588 struct spec_nodes
*s
;
1590 pfile
->buffer_ob
= xnew (struct obstack
);
1591 obstack_init (pfile
->buffer_ob
);
1593 /* Perhaps not the ideal place to put this. */
1594 pfile
->spec_nodes
= s
= xnew (struct spec_nodes
);
1595 s
->n_L
= cpp_lookup (pfile
, DSC("L"));
1596 s
->n_defined
= cpp_lookup (pfile
, DSC("defined"));
1597 s
->n__STRICT_ANSI__
= cpp_lookup (pfile
, DSC("__STRICT_ANSI__"));
1598 s
->n__CHAR_UNSIGNED__
= cpp_lookup (pfile
, DSC("__CHAR_UNSIGNED__"));
1599 s
->n__VA_ARGS__
= cpp_lookup (pfile
, DSC("__VA_ARGS__"));
1600 for (i
= 0; i
< N_DIRECTIVES
; i
++)
1601 s
->dirs
[i
] = cpp_lookup (pfile
, dtable
[i
].name
, dtable
[i
].length
);
1605 _cpp_cleanup_stacks (pfile
)
1608 obstack_free (pfile
->buffer_ob
, 0);
1609 free (pfile
->buffer_ob
);