2 Copyright (C) 1986, 87, 89, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #ifdef HAVE_SYS_RESOURCE_H
31 # include <sys/resource.h>
36 #include "gansidecl.h"
39 extern char *update_path ();
43 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
44 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
46 /* Find the largest host integer type and set its size and type.
47 Watch out: on some crazy hosts `long' is shorter than `int'. */
51 # include <inttypes.h>
52 # define HOST_WIDE_INT intmax_t
54 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
55 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
56 # define HOST_WIDE_INT int
58 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
59 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
60 # define HOST_WIDE_INT long
62 # define HOST_WIDE_INT long long
68 /* By default, colon separates directories in a path. */
69 #ifndef PATH_SEPARATOR
70 #define PATH_SEPARATOR ':'
73 #ifndef STANDARD_INCLUDE_DIR
74 #define STANDARD_INCLUDE_DIR "/usr/include"
76 #ifndef INCLUDE_LEN_FUDGE
77 #define INCLUDE_LEN_FUDGE 0
80 /* Symbols to predefine. */
83 static char *predefs
= CPP_PREDEFINES
;
85 static char *predefs
= "";
88 /* We let tm.h override the types used here, to handle trivial differences
89 such as the choice of unsigned int or long unsigned int for size_t.
90 When machines start needing nontrivial differences in the size type,
91 it would be best to do something here to figure out automatically
92 from other information what type to use. */
94 /* The string value for __SIZE_TYPE__. */
97 #define SIZE_TYPE "long unsigned int"
100 /* The string value for __PTRDIFF_TYPE__. */
103 #define PTRDIFF_TYPE "long int"
106 /* The string value for __WCHAR_TYPE__. */
109 #define WCHAR_TYPE "int"
111 #define CPP_WCHAR_TYPE(PFILE) \
112 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
114 /* The string value for __USER_LABEL_PREFIX__ */
116 #ifndef USER_LABEL_PREFIX
117 #define USER_LABEL_PREFIX ""
120 /* The string value for __REGISTER_PREFIX__ */
122 #ifndef REGISTER_PREFIX
123 #define REGISTER_PREFIX ""
126 /* In the definition of a #assert name, this structure forms
127 a list of the individual values asserted.
128 Each value is itself a list of "tokens".
129 These are strings that are compared by name. */
131 struct tokenlist_list
{
132 struct tokenlist_list
*next
;
133 struct arglist
*tokens
;
136 struct assertion_hashnode
{
137 struct assertion_hashnode
*next
; /* double links for easy deletion */
138 struct assertion_hashnode
*prev
;
139 /* also, a back pointer to this node's hash
140 chain is kept, in case the node is the head
141 of the chain and gets deleted. */
142 struct assertion_hashnode
**bucket_hdr
;
143 int length
; /* length of token, for quick comparison */
144 U_CHAR
*name
; /* the actual name */
145 /* List of token-sequences. */
146 struct tokenlist_list
*value
;
149 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
150 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
152 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
153 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
154 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
155 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
156 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
157 (Note that it is false while we're expanding marco *arguments*.) */
158 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
160 /* Move all backslash-newline pairs out of embarrassing places.
161 Exchange all such pairs following BP
162 with any potentially-embarrassing characters that follow them.
163 Potentially-embarrassing characters are / and *
164 (because a backslash-newline inside a comment delimiter
165 would cause it not to be recognized). */
167 #define NEWLINE_FIX \
168 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
170 /* Same, but assume we've already read the potential '\\' into C. */
171 #define NEWLINE_FIX1(C) do { \
172 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
176 struct cpp_pending
*next
;
181 /* Forward declarations. */
185 void cpp_file_line_for_message
PARAMS ((cpp_reader
*, char *, int, int));
186 void cpp_hash_cleanup
PARAMS ((cpp_reader
*));
188 void cpp_print_containing_files
PARAMS ((cpp_reader
*));
190 static void add_import ();
191 static void append_include_chain ();
192 static void make_assertion ();
193 static void path_include ();
194 static void initialize_builtins ();
195 static void initialize_char_syntax ();
196 extern void delete_macro ();
198 static void trigraph_pcp ();
200 static int finclude ();
201 static void validate_else ();
202 static int comp_def_part ();
204 extern void fancy_abort ();
206 static int lookup_import ();
207 static int redundant_include_p ();
208 static is_system_include ();
209 static struct file_name_map
*read_name_map ();
210 static char *read_filename_string ();
211 static int open_include_file ();
212 static int check_macro_name ();
213 static int compare_defs ();
214 static int compare_token_lists ();
215 static HOST_WIDE_INT
eval_if_expression ();
216 static int change_newlines ();
218 static struct arglist
*read_token_list ();
219 static void free_token_list ();
220 static int safe_read ();
221 static void push_macro_expansion
PARAMS ((cpp_reader
*,
222 U_CHAR
*, int, HASHNODE
*));
223 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
224 extern char *xrealloc ();
225 static char *xcalloc ();
226 static char *savestring ();
228 static void conditional_skip ();
229 static void skip_if_group ();
231 /* Last arg to output_line_command. */
232 enum file_change_code
{same_file
, enter_file
, leave_file
};
234 /* External declarations. */
236 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
238 extern FILE *fdopen ();
239 extern char *version_string
;
240 extern struct tm
*localtime ();
242 /* These functions are declared to return int instead of void since they
243 are going to be placed in a table and some old compilers have trouble with
244 pointers to functions returning void. */
246 static int do_define ();
247 static int do_line ();
248 static int do_include ();
249 static int do_undef ();
250 static int do_error ();
251 static int do_pragma ();
252 static int do_ident ();
254 static int do_xifdef ();
255 static int do_else ();
256 static int do_elif ();
257 static int do_endif ();
258 static int do_sccs ();
259 static int do_once ();
260 static int do_assert ();
261 static int do_unassert ();
262 static int do_warning ();
264 struct file_name_list
266 struct file_name_list
*next
;
268 /* If the following is nonzero, it is a macro name.
269 Don't include the file again if that macro is defined. */
270 U_CHAR
*control_macro
;
271 /* If the following is nonzero, it is a C-language system include
273 int c_system_include_path
;
274 /* Mapping of file names for this directory. */
275 struct file_name_map
*name_map
;
276 /* Non-zero if name_map is valid. */
280 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
281 via the same directory as the file that #included it. */
282 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
284 /* #include "file" looks in source file dir, then stack. */
285 /* #include <file> just looks in the stack. */
286 /* -I directories are added to the end, then the defaults are added. */
288 static struct default_include
{
289 char *fname
; /* The name of the directory. */
290 char *component
; /* The component containing the directory */
291 int cplusplus
; /* Only look here if we're compiling C++. */
292 int cxx_aware
; /* Includes in this directory don't need to
293 be wrapped in extern "C" when compiling
295 } include_defaults_array
[]
296 #ifdef INCLUDE_DEFAULTS
300 /* Pick up GNU C++ specific include files. */
301 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
302 { OLD_GPLUSPLUS_INCLUDE_DIR
, 0, 1, 1 },
304 /* This is the dir for fixincludes. Put it just before
305 the files that we fix. */
306 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
307 /* For cross-compilation, this dir name is generated
308 automatically in Makefile.in. */
309 { CROSS_INCLUDE_DIR
, "GCC",0, 0 },
310 #ifdef TOOL_INCLUDE_DIR
311 /* This is another place that the target system's headers might be. */
312 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
314 #else /* not CROSS_COMPILE */
315 #ifdef LOCAL_INCLUDE_DIR
316 /* This should be /usr/local/include and should come before
317 the fixincludes-fixed header files. */
318 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
320 #ifdef TOOL_INCLUDE_DIR
321 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
322 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
323 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
325 /* This is the dir for fixincludes. Put it just before
326 the files that we fix. */
327 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
328 /* Some systems have an extra dir of include files. */
329 #ifdef SYSTEM_INCLUDE_DIR
330 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
332 #ifndef STANDARD_INCLUDE_COMPONENT
333 #define STANDARD_INCLUDE_COMPONENT 0
335 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
336 #endif /* not CROSS_COMPILE */
339 #endif /* no INCLUDE_DEFAULTS */
341 /* `struct directive' defines one #-directive, including how to handle it. */
344 int length
; /* Length of name */
345 int (*func
)(); /* Function to handle directive */
346 char *name
; /* Name of directive */
347 enum node_type type
; /* Code which describes which directive. */
348 char command_reads_line
; /* One if rest of line is read by func. */
351 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
352 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
354 /* Here is the actual list of #-directives, most-often-used first.
355 The initialize_builtins function assumes #define is the very first. */
357 static struct directive directive_table
[] = {
358 { 6, do_define
, "define", T_DEFINE
},
359 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
360 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
361 { 7, do_include
, "include", T_INCLUDE
, 1},
362 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
363 { 6, do_include
, "import", T_IMPORT
, 1},
364 { 5, do_endif
, "endif", T_ENDIF
, 1},
365 { 4, do_else
, "else", T_ELSE
, 1},
366 { 2, do_if
, "if", T_IF
, 1},
367 { 4, do_elif
, "elif", T_ELIF
, 1},
368 { 5, do_undef
, "undef", T_UNDEF
},
369 { 5, do_error
, "error", T_ERROR
},
370 { 7, do_warning
, "warning", T_WARNING
},
371 { 6, do_pragma
, "pragma", T_PRAGMA
},
372 { 4, do_line
, "line", T_LINE
, 1},
373 { 5, do_ident
, "ident", T_IDENT
, 1},
374 #ifdef SCCS_DIRECTIVE
375 { 4, do_sccs
, "sccs", T_SCCS
},
377 { 6, do_assert
, "assert", T_ASSERT
, 1},
378 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
379 { -1, 0, "", T_UNUSED
},
382 /* table to tell if char can be part of a C identifier. */
383 U_CHAR is_idchar
[256];
384 /* table to tell if char can be first char of a c identifier. */
385 U_CHAR is_idstart
[256];
386 /* table to tell if c is horizontal space. */
387 U_CHAR is_hor_space
[256];
388 /* table to tell if c is horizontal or vertical space. */
389 static U_CHAR is_space
[256];
391 /* Initialize syntactic classifications of characters. */
394 initialize_char_syntax (opts
)
395 struct cpp_options
*opts
;
400 * Set up is_idchar and is_idstart tables. These should be
401 * faster than saying (is_alpha (c) || c == '_'), etc.
402 * Set up these things before calling any routines tthat
405 for (i
= 'a'; i
<= 'z'; i
++) {
406 is_idchar
[i
- 'a' + 'A'] = 1;
408 is_idstart
[i
- 'a' + 'A'] = 1;
411 for (i
= '0'; i
<= '9'; i
++)
415 is_idchar
['$'] = opts
->dollars_in_ident
;
416 is_idstart
['$'] = opts
->dollars_in_ident
;
418 /* horizontal space table */
419 is_hor_space
[' '] = 1;
420 is_hor_space
['\t'] = 1;
421 is_hor_space
['\v'] = 1;
422 is_hor_space
['\f'] = 1;
423 is_hor_space
['\r'] = 1;
434 /* Place into PFILE a quoted string representing the string SRC.
435 Caller must reserve enough space in pfile->token_buffer. */
438 quote_string (pfile
, src
)
444 CPP_PUTC_Q (pfile
, '\"');
446 switch ((c
= *src
++))
450 CPP_PUTC_Q (pfile
, c
);
453 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
454 CPP_ADJUST_WRITTEN (pfile
, 4);
460 CPP_PUTC_Q (pfile
, '\\');
461 CPP_PUTC_Q (pfile
, c
);
465 CPP_PUTC_Q (pfile
, '\"');
466 CPP_NUL_TERMINATE_Q (pfile
);
471 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
474 cpp_grow_buffer (pfile
, n
)
478 long old_written
= CPP_WRITTEN (pfile
);
479 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
480 pfile
->token_buffer
= (U_CHAR
*)
481 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
482 CPP_SET_WRITTEN (pfile
, old_written
);
487 * process a given definition string, for initialization
488 * If STR is just an identifier, define it with value 1.
489 * If STR has anything after the identifier, then it should
490 * be identifier=definition.
494 cpp_define (pfile
, str
)
504 cpp_error (pfile
, "malformed option `-D %s'", str
);
507 while (is_idchar
[*++p
])
511 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
512 strcpy ((char *)buf
, str
);
513 strcat ((char *)buf
, " 1");
517 cpp_error (pfile
, "malformed option `-D %s'", str
);
523 /* Copy the entire option so we can modify it. */
524 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
525 strncpy (buf
, str
, p
- str
);
526 /* Change the = to a space. */
528 /* Scan for any backslash-newline and remove it. */
533 if (*p
== '\\' && p
[1] == '\n')
541 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
544 /* Process the string STR as if it appeared as the body of a #assert.
545 OPTION is the option name for which STR was the argument. */
548 make_assertion (pfile
, option
, str
)
553 struct directive
*kt
;
556 /* Copy the entire option so we can modify it. */
557 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
558 strcpy ((char *) buf
, str
);
559 /* Scan for any backslash-newline and remove it. */
563 if (*p
== '\\' && p
[1] == '\n')
572 if (!is_idstart
[*p
]) {
573 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
576 while (is_idchar
[*++p
])
578 while (*p
== ' ' || *p
== '\t') p
++;
579 if (! (*p
== 0 || *p
== '(')) {
580 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
584 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
586 do_assert (pfile
, NULL
, NULL
, NULL
);
587 cpp_pop_buffer (pfile
);
591 /* Append a chain of `struct file_name_list's
592 to the end of the main include chain.
593 FIRST is the beginning of the chain to append, and LAST is the end. */
596 append_include_chain (pfile
, first
, last
)
598 struct file_name_list
*first
, *last
;
600 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
601 struct file_name_list
*dir
;
606 if (opts
->include
== 0)
607 opts
->include
= first
;
609 opts
->last_include
->next
= first
;
611 if (opts
->first_bracket_include
== 0)
612 opts
->first_bracket_include
= first
;
614 for (dir
= first
; ; dir
= dir
->next
) {
615 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
616 if (len
> pfile
->max_include_len
)
617 pfile
->max_include_len
= len
;
623 opts
->last_include
= last
;
626 /* Add output to `deps_buffer' for the -M switch.
627 STRING points to the text to be output.
628 SPACER is ':' for targets, ' ' for dependencies, zero for text
629 to be inserted literally. */
632 deps_output (pfile
, string
, spacer
)
637 int size
= strlen (string
);
642 #ifndef MAX_OUTPUT_COLUMNS
643 #define MAX_OUTPUT_COLUMNS 72
646 && pfile
->deps_column
> 0
647 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
649 deps_output (pfile
, " \\\n ", 0);
650 pfile
->deps_column
= 0;
653 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
655 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
656 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
657 pfile
->deps_allocated_size
);
659 if (spacer
== ' ' && pfile
->deps_column
> 0)
660 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
661 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
662 pfile
->deps_size
+= size
;
663 pfile
->deps_column
+= size
;
665 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
666 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
669 /* Given a colon-separated list of file names PATH,
670 add all the names to the search path for include files. */
673 path_include (pfile
, path
)
685 struct file_name_list
*dirtmp
;
687 /* Find the end of this name. */
688 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
690 /* An empty name in the path stands for the current directory. */
691 name
= (char *) xmalloc (2);
695 /* Otherwise use the directory that is named. */
696 name
= (char *) xmalloc (q
- p
+ 1);
697 bcopy (p
, name
, q
- p
);
701 dirtmp
= (struct file_name_list
*)
702 xmalloc (sizeof (struct file_name_list
));
703 dirtmp
->next
= 0; /* New one goes on the end */
704 dirtmp
->control_macro
= 0;
705 dirtmp
->c_system_include_path
= 0;
706 dirtmp
->fname
= name
;
707 dirtmp
->got_name_map
= 0;
708 append_include_chain (pfile
, dirtmp
, dirtmp
);
710 /* Advance past this name. */
714 /* Skip the colon. */
720 cpp_options_init (opts
)
723 bzero ((char *) opts
, sizeof *opts
);
724 opts
->in_fname
= NULL
;
725 opts
->out_fname
= NULL
;
727 /* Initialize is_idchar to allow $. */
728 opts
->dollars_in_ident
= 1;
729 initialize_char_syntax (opts
);
731 opts
->no_line_commands
= 0;
732 opts
->no_trigraphs
= 1;
733 opts
->put_out_comments
= 0;
734 opts
->print_include_names
= 0;
735 opts
->dump_macros
= dump_none
;
739 opts
->cplusplus_comments
= 0;
746 opts
->pedantic_errors
= 0;
747 opts
->inhibit_warnings
= 0;
748 opts
->warn_comments
= 0;
749 opts
->warn_import
= 1;
750 opts
->warnings_are_errors
= 0;
754 null_underflow (pfile
)
761 null_cleanup (pbuf
, pfile
)
769 macro_cleanup (pbuf
, pfile
)
773 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
774 if (macro
->type
== T_DISABLED
)
775 macro
->type
= T_MACRO
;
776 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
782 file_cleanup (pbuf
, pfile
)
794 /* Assuming we have read '/'.
795 If this is the start of a comment (followed by '*' or '/'),
796 skip to the end of the comment, and return ' '.
797 Return EOF if we reached the end of file before the end of the comment.
798 If not the start of a comment, return '/'. */
801 skip_comment (pfile
, linep
)
806 while (PEEKC() == '\\' && PEEKN(1) == '\n')
821 while (c
== '\\' && PEEKC() == '\n')
825 FORWARD(1), c
= GETC();
827 if (prev_c
== '*' && c
== '/')
829 if (c
== '\n' && linep
)
833 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
840 return ' '; /* Allow // to be terminated by EOF. */
841 while (c
== '\\' && PEEKC() == '\n')
850 /* Don't consider final '\n' to be part of comment. */
860 /* Skip whitespace \-newline and comments. Does not macro-expand. */
863 cpp_skip_hspace (pfile
)
873 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
874 cpp_pedwarn (pfile
, "%s in preprocessing directive",
875 c
== '\f' ? "formfeed" : "vertical tab");
881 c
= skip_comment (pfile
, NULL
);
884 if (c
== EOF
|| c
== '/')
887 else if (c
== '\\' && PEEKN(1) == '\n') {
890 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
891 && is_hor_space
[PEEKN(1)])
897 /* Read the rest of the current line.
898 The line is appended to PFILE's output buffer. */
901 copy_rest_of_line (pfile
)
904 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
921 goto scan_directive_token
;
925 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '/'))
926 goto scan_directive_token
;
930 if (CPP_PEDANTIC (pfile
))
931 cpp_pedwarn (pfile
, "%s in preprocessing directive",
932 c
== '\f' ? "formfeed" : "vertical tab");
938 scan_directive_token
:
940 cpp_get_token (pfile
);
946 CPP_NUL_TERMINATE (pfile
);
950 skip_rest_of_line (pfile
)
953 long old
= CPP_WRITTEN (pfile
);
954 copy_rest_of_line (pfile
);
955 CPP_SET_WRITTEN (pfile
, old
);
958 /* Handle a possible # directive.
959 '#' has already been read. */
962 handle_directive (pfile
)
965 register struct directive
*kt
;
968 U_CHAR
*ident
, *line_end
;
969 long old_written
= CPP_WRITTEN (pfile
);
971 cpp_skip_hspace (pfile
);
974 if (c
>= '0' && c
<= '9')
976 /* Handle # followed by a line number. */
977 if (CPP_PEDANTIC (pfile
))
978 cpp_pedwarn (pfile
, "`#' followed by integer");
979 do_line (pfile
, NULL
);
980 goto done_a_directive
;
983 /* Now find the directive name. */
984 CPP_PUTC (pfile
, '#');
985 parse_name (pfile
, GETC());
986 ident
= pfile
->token_buffer
+ old_written
+ 1;
987 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
988 if (ident_length
== 0 && PEEKC() == '\n')
990 /* A line of just `#' becomes blank. */
991 goto done_a_directive
;
995 if (ident_length
== 0 || !is_idstart
[*ident
]) {
997 while (is_idchar
[*p
]) {
998 if (*p
< '0' || *p
> '9')
1002 /* Avoid error for `###' and similar cases unless -pedantic. */
1004 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1006 if (pedantic
&& !lang_asm
)
1007 cpp_warning (pfile
, "invalid preprocessor directive");
1013 cpp_error (pfile
, "invalid preprocessor directive name");
1019 * Decode the keyword and call the appropriate expansion
1020 * routine, after moving the input pointer up to the next line.
1022 for (kt
= directive_table
; ; kt
++) {
1023 if (kt
->length
<= 0)
1024 goto not_a_directive
;
1025 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1029 if (kt
->command_reads_line
)
1033 /* Nonzero means do not delete comments within the directive.
1034 #define needs this when -traditional. */
1035 int comments
= CPP_TRADITIONAL (pfile
) && kt
->type
== T_DEFINE
;
1036 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1037 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1038 after_ident
= CPP_WRITTEN (pfile
);
1039 copy_rest_of_line (pfile
);
1040 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1043 /* We may want to pass through #define, #undef, #pragma, and #include.
1044 Other directives may create output, but we don't want the directive
1045 itself out, so we pop it now. For example conditionals may emit
1046 #failed ... #endfailed stuff. But note that popping the buffer
1047 means the parameters to kt->func may point after pfile->limit
1048 so these parameters are invalid as soon as something gets appended
1049 to the token_buffer. */
1051 line_end
= CPP_PWRITTEN (pfile
);
1052 if (! (kt
->type
== T_DEFINE
1053 || (kt
->type
== T_UNDEF
1054 && CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
1055 || kt
->type
== T_PRAGMA
1056 || (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)
1057 && CPP_OPTIONS (pfile
)->dump_includes
)))
1058 CPP_SET_WRITTEN (pfile
, old_written
);
1060 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1062 if (kt
->type
== T_DEFINE
)
1064 if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1066 /* Skip "#define". */
1067 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7;
1069 SKIP_WHITE_SPACE (p
);
1070 while (is_idchar
[*p
]) p
++;
1072 CPP_PUTC (pfile
, '\n');
1074 else if (CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
1075 CPP_SET_WRITTEN (pfile
, old_written
);
1085 /* Pass a directive through to the output file.
1086 BUF points to the contents of the directive, as a contiguous string.
1087 LIMIT points to the first character past the end of the directive.
1088 KEYWORD is the keyword-table entry for the directive. */
1091 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1092 U_CHAR
*buf
, *limit
;
1094 struct directive
*keyword
;
1096 register unsigned keyword_length
= keyword
->length
;
1098 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1099 CPP_PUTC_Q (pfile
, '#');
1100 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1101 if (limit
!= buf
&& buf
[0] != ' ')
1102 CPP_PUTC_Q (pfile
, ' ');
1103 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1105 CPP_PUTS_Q (pfile
, '\n');
1106 /* Count the line we have just made in the output,
1107 to get in sync properly. */
1112 /* The arglist structure is built by do_define to tell
1113 collect_definition where the argument names begin. That
1114 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1115 would contain pointers to the strings x, y, and z.
1116 Collect_definition would then build a DEFINITION node,
1117 with reflist nodes pointing to the places x, y, and z had
1118 appeared. So the arglist is just convenience data passed
1119 between these two routines. It is not kept around after
1120 the current #define has been processed and entered into the
1124 struct arglist
*next
;
1131 /* Read a replacement list for a macro with parameters.
1132 Build the DEFINITION structure.
1133 Reads characters of text starting at BUF until END.
1134 ARGLIST specifies the formal parameters to look for
1135 in the text of the definition; NARGS is the number of args
1136 in that list, or -1 for a macro name that wants no argument list.
1137 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1138 and NAMELEN is its length in characters.
1140 Note that comments, backslash-newlines, and leading white space
1141 have already been deleted from the argument. */
1144 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1146 U_CHAR
*buf
, *limit
;
1148 struct arglist
*arglist
;
1151 register U_CHAR
*p
, *lastp
, *exp_p
;
1152 struct reflist
*endpat
= NULL
;
1153 /* Pointer to first nonspace after last ## seen. */
1155 /* Pointer to first nonspace after last single-# seen. */
1156 U_CHAR
*stringify
= 0;
1158 int expected_delimiter
= '\0';
1160 /* Scan thru the replacement list, ignoring comments and quoted
1161 strings, picking up on the macro calls. It does a linear search
1162 thru the arg list on every potential symbol. Profiling might say
1163 that something smarter should happen. */
1168 /* Find the beginning of the trailing whitespace. */
1170 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1172 /* Allocate space for the text in the macro definition.
1173 Leading and trailing whitespace chars need 2 bytes each.
1174 Each other input char may or may not need 1 byte,
1175 so this is an upper bound. The extra 5 are for invented
1176 leading and trailing newline-marker and final null. */
1177 maxsize
= (sizeof (DEFINITION
)
1179 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1183 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1185 defn
->nargs
= nargs
;
1186 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1191 /* Add one initial space escape-marker to prevent accidental
1192 token-pasting (often removed by macroexpand). */
1196 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1197 cpp_error (pfile
, "`##' at start of macro definition");
1201 /* Process the main body of the definition. */
1203 int skipped_arg
= 0;
1204 register U_CHAR c
= *p
++;
1208 if (!CPP_TRADITIONAL (pfile
)) {
1212 if (expected_delimiter
!= '\0') {
1213 if (c
== expected_delimiter
)
1214 expected_delimiter
= '\0';
1216 expected_delimiter
= c
;
1220 if (p
< limit
&& expected_delimiter
) {
1221 /* In a string, backslash goes through
1222 and makes next char ordinary. */
1228 /* An '@' in a string or character constant stands for itself,
1229 and does not need to be escaped. */
1230 if (!expected_delimiter
)
1235 /* # is ordinary inside a string. */
1236 if (expected_delimiter
)
1238 if (p
< limit
&& *p
== '#') {
1239 /* ##: concatenate preceding and following tokens. */
1240 /* Take out the first #, discard preceding whitespace. */
1242 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1244 /* Skip the second #. */
1246 /* Discard following whitespace. */
1247 SKIP_WHITE_SPACE (p
);
1250 cpp_error (pfile
, "`##' at end of macro definition");
1251 } else if (nargs
>= 0) {
1252 /* Single #: stringify following argument ref.
1253 Don't leave the # in the expansion. */
1255 SKIP_WHITE_SPACE (p
);
1256 if (p
== limit
|| ! is_idstart
[*p
]
1257 || (*p
== 'L' && p
+ 1 < limit
&& (p
[1] == '\'' || p
[1] == '"')))
1259 "`#' operator is not followed by a macro argument name");
1266 /* In -traditional mode, recognize arguments inside strings and
1267 and character constants, and ignore special properties of #.
1268 Arguments inside strings are considered "stringified", but no
1269 extra quote marks are supplied. */
1273 if (expected_delimiter
!= '\0') {
1274 if (c
== expected_delimiter
)
1275 expected_delimiter
= '\0';
1277 expected_delimiter
= c
;
1281 /* Backslash quotes delimiters and itself, but not macro args. */
1282 if (expected_delimiter
!= 0 && p
< limit
1283 && (*p
== expected_delimiter
|| *p
== '\\')) {
1290 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1293 /* If we find a comment that wasn't removed by handle_directive,
1294 this must be -traditional. So replace the comment with
1298 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1301 /* Mark this as a concatenation-point, as if it had been ##. */
1309 /* Handle the start of a symbol. */
1310 if (is_idchar
[c
] && nargs
> 0) {
1311 U_CHAR
*id_beg
= p
- 1;
1315 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1316 id_len
= p
- id_beg
;
1319 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
1320 register struct arglist
*arg
;
1322 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1323 struct reflist
*tpat
;
1325 if (arg
->name
[0] == c
1326 && arg
->length
== id_len
1327 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1328 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1329 if (CPP_TRADITIONAL (pfile
)) {
1330 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1334 "macro arg `%.*s' would be stringified with -traditional.",
1338 /* If ANSI, don't actually substitute inside a string. */
1339 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1341 /* make a pat node for this arg and append it to the end of
1343 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1345 tpat
->raw_before
= concat
== id_beg
;
1346 tpat
->raw_after
= 0;
1347 tpat
->rest_args
= arg
->rest_args
;
1348 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1349 ? expected_delimiter
!= '\0'
1350 : stringify
== id_beg
);
1353 defn
->pattern
= tpat
;
1355 endpat
->next
= tpat
;
1358 tpat
->argno
= arg
->argno
;
1359 tpat
->nchars
= exp_p
- lastp
;
1361 register U_CHAR
*p1
= p
;
1362 SKIP_WHITE_SPACE (p1
);
1363 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1364 tpat
->raw_after
= 1;
1366 lastp
= exp_p
; /* place to start copying from next time */
1373 /* If this was not a macro arg, copy it into the expansion. */
1374 if (! skipped_arg
) {
1375 register U_CHAR
*lim1
= p
;
1379 if (stringify
== id_beg
)
1381 "`#' operator should be followed by a macro argument name");
1386 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1388 /* If ANSI, put in a "@ " marker to prevent token pasting.
1389 But not if "inside a string" (which in ANSI mode
1390 happens only for -D option). */
1397 defn
->length
= exp_p
- defn
->expansion
;
1399 /* Crash now if we overrun the allocated size. */
1400 if (defn
->length
+ 1 > maxsize
)
1404 /* This isn't worth the time it takes. */
1405 /* give back excess storage */
1406 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1413 * special extension string that can be added to the last macro argument to
1414 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1415 * #define wow(a, b...) process (b, a, b)
1416 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1417 * { wow (one, two); } -> { process (two, one, two); }
1418 * if this "rest_arg" is used with the concat token '##' and if it is not
1419 * supplied then the token attached to with ## will not be outputted. Ex:
1420 * #define wow (a, b...) process (b ## , a, ## b)
1421 * { wow (1, 2); } -> { process (2, 1, 2); }
1422 * { wow (one); } -> { process (one); {
1424 static char rest_extension
[] = "...";
1425 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1427 /* Create a DEFINITION node from a #define directive. Arguments are
1428 as for do_define. */
1431 create_definition (buf
, limit
, pfile
, predefinition
)
1432 U_CHAR
*buf
, *limit
;
1436 U_CHAR
*bp
; /* temp ptr into input buffer */
1437 U_CHAR
*symname
; /* remember where symbol name starts */
1438 int sym_length
; /* and how long it is */
1441 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1443 int arglengths
= 0; /* Accumulate lengths of arg names
1444 plus number of args. */
1446 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1450 while (is_hor_space
[*bp
])
1453 symname
= bp
; /* remember where it starts */
1455 sym_length
= check_macro_name (pfile
, bp
, 0);
1458 /* Lossage will occur if identifiers or control keywords are broken
1459 across lines using backslash. This is not the right place to take
1463 struct arglist
*arg_ptrs
= NULL
;
1466 bp
++; /* skip '(' */
1467 SKIP_WHITE_SPACE (bp
);
1469 /* Loop over macro argument names. */
1470 while (*bp
!= ')') {
1471 struct arglist
*temp
;
1473 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1475 temp
->next
= arg_ptrs
;
1476 temp
->argno
= argno
++;
1477 temp
->rest_args
= 0;
1481 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1483 if (!is_idstart
[*bp
])
1484 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1486 /* Find the end of the arg name. */
1487 while (is_idchar
[*bp
]) {
1489 /* do we have a "special" rest-args extension here? */
1490 if (limit
- bp
> REST_EXTENSION_LENGTH
1491 && strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1493 temp
->rest_args
= 1;
1497 temp
->length
= bp
- temp
->name
;
1499 bp
+= REST_EXTENSION_LENGTH
;
1500 arglengths
+= temp
->length
+ 2;
1501 SKIP_WHITE_SPACE (bp
);
1502 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1503 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1508 SKIP_WHITE_SPACE (bp
);
1511 cpp_error (pfile
, "unterminated parameter list in `#define'");
1515 struct arglist
*otemp
;
1517 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1518 if (temp
->length
== otemp
->length
1519 && strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1522 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1523 (void) strncpy (name
, temp
->name
, temp
->length
);
1524 name
[temp
->length
] = '\0';
1526 "duplicate argument name `%s' in `#define'", name
);
1532 ++bp
; /* skip paren */
1533 SKIP_WHITE_SPACE (bp
);
1534 /* now everything from bp before limit is the definition. */
1535 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1536 defn
->rest_args
= rest_args
;
1538 /* Now set defn->args.argnames to the result of concatenating
1539 the argument names in reverse order
1540 with comma-space between them. */
1541 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1543 struct arglist
*temp
;
1545 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1546 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1548 if (temp
->next
!= 0) {
1549 defn
->args
.argnames
[i
++] = ',';
1550 defn
->args
.argnames
[i
++] = ' ';
1553 defn
->args
.argnames
[i
] = 0;
1556 /* Simple expansion or empty definition. */
1560 if (is_hor_space
[*bp
]) {
1562 SKIP_WHITE_SPACE (bp
);
1565 case '!': case '"': case '#': case '%': case '&': case '\'':
1566 case ')': case '*': case '+': case ',': case '-': case '.':
1567 case '/': case ':': case ';': case '<': case '=': case '>':
1568 case '?': case '[': case '\\': case ']': case '^': case '{':
1569 case '|': case '}': case '~':
1570 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1571 sym_length
, symname
);
1575 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1576 sym_length
, symname
);
1581 /* now everything from bp before limit is the definition. */
1582 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1583 defn
->args
.argnames
= (U_CHAR
*) "";
1589 /* OP is null if this is a predefinition */
1590 defn
->predefined
= predefinition
;
1592 mdef
.symnam
= symname
;
1593 mdef
.symlen
= sym_length
;
1602 /* Check a purported macro name SYMNAME, and yield its length.
1603 ASSERTION is nonzero if this is really for an assertion name. */
1606 check_macro_name (pfile
, symname
, assertion
)
1614 for (p
= symname
; is_idchar
[*p
]; p
++)
1616 sym_length
= p
- symname
;
1618 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
1620 assertion
? "invalid assertion name" : "invalid macro name");
1621 else if (!is_idstart
[*symname
]
1622 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
1623 U_CHAR
*msg
; /* what pain... */
1624 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1625 bcopy (symname
, msg
, sym_length
);
1626 msg
[sym_length
] = 0;
1629 ? "invalid assertion name `%s'"
1630 : "invalid macro name `%s'"),
1636 /* Return zero if two DEFINITIONs are isomorphic. */
1639 compare_defs (pfile
, d1
, d2
)
1641 DEFINITION
*d1
, *d2
;
1643 register struct reflist
*a1
, *a2
;
1644 register U_CHAR
*p1
= d1
->expansion
;
1645 register U_CHAR
*p2
= d2
->expansion
;
1648 if (d1
->nargs
!= d2
->nargs
)
1650 if (CPP_PEDANTIC (pfile
)
1651 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1653 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1654 a1
= a1
->next
, a2
= a2
->next
) {
1655 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1656 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1657 || a1
->argno
!= a2
->argno
1658 || a1
->stringify
!= a2
->stringify
1659 || a1
->raw_before
!= a2
->raw_before
1660 || a1
->raw_after
!= a2
->raw_after
)
1668 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1669 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1674 /* Return 1 if two parts of two macro definitions are effectively different.
1675 One of the parts starts at BEG1 and has LEN1 chars;
1676 the other has LEN2 chars at BEG2.
1677 Any sequence of whitespace matches any other sequence of whitespace.
1678 FIRST means these parts are the first of a macro definition;
1679 so ignore leading whitespace entirely.
1680 LAST means these parts are the last of a macro definition;
1681 so ignore trailing whitespace entirely. */
1684 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1686 U_CHAR
*beg1
, *beg2
;
1690 register U_CHAR
*end1
= beg1
+ len1
;
1691 register U_CHAR
*end2
= beg2
+ len2
;
1693 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1694 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1697 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1698 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1700 while (beg1
!= end1
&& beg2
!= end2
) {
1701 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1702 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1703 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1704 } else if (*beg1
== *beg2
) {
1708 return (beg1
!= end1
) || (beg2
!= end2
);
1711 /* Process a #define command.
1712 BUF points to the contents of the #define command, as a contiguous string.
1713 LIMIT points to the first character past the end of the definition.
1714 KEYWORD is the keyword-table entry for #define,
1715 or NULL for a "predefined" macro. */
1718 do_define (pfile
, keyword
, buf
, limit
)
1720 struct directive
*keyword
;
1721 U_CHAR
*buf
, *limit
;
1728 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1729 if (pcp_outfile
&& keyword
)
1730 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1733 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1737 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1739 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1742 /* Redefining a precompiled key is ok. */
1743 if (hp
->type
== T_PCSTRING
)
1745 /* Redefining a macro is ok if the definitions are the same. */
1746 else if (hp
->type
== T_MACRO
)
1747 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
1748 /* Redefining a constant is ok with -D. */
1749 else if (hp
->type
== T_CONST
)
1750 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1751 /* Print the warning if it's not ok. */
1754 U_CHAR
*msg
; /* what pain... */
1756 /* If we are passing through #define and #undef directives, do
1757 that for this re-definition now. */
1758 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1759 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1761 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
1762 if (hp
->type
== T_MACRO
)
1763 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1764 "this is the location of the previous definition");
1766 /* Replace the old definition. */
1768 hp
->value
.defn
= mdef
.defn
;
1772 /* If we are passing through #define and #undef directives, do
1773 that for this new definition now. */
1774 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1775 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1776 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1777 (char *) mdef
.defn
, hashcode
);
1787 /* This structure represents one parsed argument in a macro call.
1788 `raw' points to the argument text as written (`raw_length' is its length).
1789 `expanded' points to the argument's macro-expansion
1790 (its length is `expand_length').
1791 `stringified_length' is the length the argument would have
1793 `use_count' is the number of times this macro arg is substituted
1794 into the macro. If the actual use count exceeds 10,
1795 the value stored is 10. */
1797 /* raw and expanded are relative to ARG_BASE */
1798 #define ARG_BASE ((pfile)->token_buffer)
1801 /* Strings relative to pfile->token_buffer */
1802 long raw
, expanded
, stringified
;
1803 int raw_length
, expand_length
;
1804 int stringified_length
;
1809 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1810 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1811 as the new input buffer.
1812 Return the new buffer, or NULL on failure. */
1815 cpp_push_buffer (pfile
, buffer
, length
)
1820 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1821 if (buf
== pfile
->buffer_stack
)
1823 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1828 bzero ((char *) buf
, sizeof (cpp_buffer
));
1829 CPP_BUFFER (pfile
) = buf
;
1830 buf
->if_stack
= pfile
->if_stack
;
1831 buf
->cleanup
= null_cleanup
;
1832 buf
->underflow
= null_underflow
;
1833 buf
->buf
= buf
->cur
= buffer
;
1834 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1840 cpp_pop_buffer (pfile
)
1843 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1844 (*buf
->cleanup
) (buf
, pfile
);
1845 return ++CPP_BUFFER (pfile
);
1848 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1849 Pop the buffer when done. */
1852 cpp_scan_buffer (pfile
)
1855 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1858 enum cpp_token token
= cpp_get_token (pfile
);
1859 if (token
== CPP_EOF
) /* Should not happen ... */
1861 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1863 cpp_pop_buffer (pfile
);
1870 * Rescan a string (which may have escape marks) into pfile's buffer.
1871 * Place the result in pfile->token_buffer.
1873 * The input is copied before it is scanned, so it is safe to pass
1874 * it something from the token_buffer that will get overwritten
1875 * (because it follows CPP_WRITTEN). This is used by do_include.
1879 cpp_expand_to_buffer (pfile
, buf
, length
)
1884 register cpp_buffer
*ip
;
1886 U_CHAR
*limit
= buf
+ length
;
1889 int odepth
= indepth
;
1895 /* Set up the input on the input stack. */
1897 buf1
= (U_CHAR
*) alloca (length
+ 1);
1899 register U_CHAR
*p1
= buf
;
1900 register U_CHAR
*p2
= buf1
;
1907 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1910 ip
->has_escapes
= 1;
1912 ip
->lineno
= obuf
.lineno
= 1;
1915 /* Scan the input, create the output. */
1916 cpp_scan_buffer (pfile
);
1919 if (indepth
!= odepth
)
1923 CPP_NUL_TERMINATE (pfile
);
1928 adjust_position (buf
, limit
, linep
, colp
)
1938 (*linep
)++, (*colp
) = 1;
1944 /* Move line_base forward, updating lineno and colno. */
1947 update_position (pbuf
)
1948 register cpp_buffer
*pbuf
;
1950 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
1951 unsigned char *new_pos
= pbuf
->cur
;
1952 register struct parse_marker
*mark
;
1953 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
1955 if (pbuf
->buf
+ mark
->position
< new_pos
)
1956 new_pos
= pbuf
->buf
+ mark
->position
;
1958 pbuf
->line_base
+= new_pos
- old_pos
;
1959 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
1963 cpp_buf_line_and_col (pbuf
, linep
, colp
)
1964 register cpp_buffer
*pbuf
;
1972 *linep
= pbuf
->lineno
;
1973 *colp
= pbuf
->colno
;
1974 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
1983 /* Return the cpp_buffer that corresponds to a file (not a macro). */
1986 cpp_file_buffer (pfile
)
1989 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1991 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
1992 if (ip
->fname
!= NULL
)
1998 count_newlines (buf
, limit
)
1999 register U_CHAR
*buf
;
2000 register U_CHAR
*limit
;
2002 register long count
= 0;
2013 * write out a #line command, for instance, after an #include file.
2014 * If CONDITIONAL is nonzero, we can omit the #line if it would
2015 * appear to be a no-op, and we can output a few newlines instead
2016 * if we want to increase the line number by a small amount.
2017 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2021 output_line_command (pfile
, conditional
, file_change
)
2024 enum file_change_code file_change
;
2027 char *line_cmd_buf
, *line_end
;
2029 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2031 if (ip
->fname
== NULL
)
2034 update_position (ip
);
2036 if (CPP_OPTIONS (pfile
)->no_line_commands
2037 || CPP_OPTIONS (pfile
)->no_output
)
2040 line
= CPP_BUFFER (pfile
)->lineno
;
2041 col
= CPP_BUFFER (pfile
)->colno
;
2042 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2044 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2048 if (line
== pfile
->lineno
)
2051 /* If the inherited line number is a little too small,
2052 output some newlines instead of a #line command. */
2053 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2054 CPP_RESERVE (pfile
, 20);
2055 while (line
> pfile
->lineno
) {
2056 CPP_PUTC_Q (pfile
, '\n');
2064 /* Don't output a line number of 0 if we can help it. */
2065 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2066 && *ip
->bufp
== '\n') {
2072 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2074 #ifdef OUTPUT_LINE_COMMANDS
2075 static char sharp_line
[] = "#line ";
2077 static char sharp_line
[] = "# ";
2079 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2082 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
2083 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2085 quote_string (pfile
, ip
->nominal_fname
);
2086 if (file_change
!= same_file
) {
2087 CPP_PUTC_Q (pfile
, ' ');
2088 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2090 /* Tell cc1 if following text comes from a system header file. */
2091 if (ip
->system_header_p
) {
2092 CPP_PUTC_Q (pfile
, ' ');
2093 CPP_PUTC_Q (pfile
, '3');
2095 #ifndef NO_IMPLICIT_EXTERN_C
2096 /* Tell cc1plus if following text should be treated as C. */
2097 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2098 CPP_PUTC_Q (pfile
, ' ');
2099 CPP_PUTC_Q (pfile
, '4');
2102 CPP_PUTC_Q (pfile
, '\n');
2103 pfile
->lineno
= line
;
2107 * Parse a macro argument and append the info on PFILE's token_buffer.
2108 * REST_ARGS means to absorb the rest of the args.
2109 * Return nonzero to indicate a syntax error.
2112 static enum cpp_token
2113 macarg (pfile
, rest_args
)
2118 enum cpp_token token
;
2119 long arg_start
= CPP_WRITTEN (pfile
);
2120 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2121 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2123 /* Try to parse as much of the argument as exists at this
2124 input stack level. */
2125 pfile
->no_macro_expand
++;
2128 token
= cpp_get_token (pfile
);
2134 /* If we've hit end of file, it's an error (reported by caller).
2135 Ditto if it's the end of cpp_expand_to_buffer text.
2136 If we've hit end of macro, just continue. */
2137 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2148 /* if we've returned to lowest level and
2149 we aren't absorbing all args */
2150 if (paren
== 0 && rest_args
== 0)
2154 /* Remove ',' or ')' from argument buffer. */
2155 CPP_ADJUST_WRITTEN (pfile
, -1);
2162 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2163 pfile
->no_macro_expand
--;
2168 /* Turn newlines to spaces in the string of length LENGTH at START,
2169 except inside of string constants.
2170 The string is copied into itself with its beginning staying fixed. */
2173 change_newlines (start
, length
)
2177 register U_CHAR
*ibp
;
2178 register U_CHAR
*obp
;
2179 register U_CHAR
*limit
;
2183 limit
= start
+ length
;
2186 while (ibp
< limit
) {
2187 *obp
++ = c
= *ibp
++;
2192 /* Notice and skip strings, so that we don't delete newlines in them. */
2195 while (ibp
< limit
) {
2196 *obp
++ = c
= *ibp
++;
2199 if (c
== '\n' && quotec
== '\'')
2215 if (!pfile
->timebuf
) {
2216 time_t t
= time ((time_t *) 0);
2217 pfile
->timebuf
= localtime (&t
);
2219 return pfile
->timebuf
;
2222 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2223 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2227 * expand things like __FILE__. Place the expansion into the output
2228 * buffer *without* rescanning.
2232 special_symbol (hp
, pfile
)
2239 cpp_buffer
*ip
= NULL
;
2242 int paren
= 0; /* For special `defined' keyword */
2245 if (pcp_outfile
&& pcp_inside_if
2246 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2248 "Predefined macro `%s' used inside `#if' during precompilation",
2252 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2254 if (ip
== CPP_NULL_BUFFER (pfile
))
2256 cpp_error (pfile
, "cccp error: not in any file?!");
2257 return; /* the show must go on */
2259 if (ip
->fname
!= NULL
)
2269 if (hp
->type
== T_BASE_FILE
)
2271 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2272 ip
= CPP_PREV_BUFFER (ip
);
2274 string
= ip
->nominal_fname
;
2278 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2279 quote_string (pfile
, string
);
2283 case T_INCLUDE_LEVEL
:
2285 ip
= CPP_BUFFER (pfile
);
2286 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2287 if (ip
->fname
!= NULL
)
2290 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2291 sprintf (buf
, "%d", true_indepth
- 1);
2295 buf
= (char *) alloca (3 + strlen (version_string
));
2296 sprintf (buf
, "\"%s\"", version_string
);
2299 #ifndef NO_BUILTIN_SIZE_TYPE
2305 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2306 case T_PTRDIFF_TYPE
:
2312 buf
= CPP_WCHAR_TYPE (pfile
);
2315 case T_USER_LABEL_PREFIX_TYPE
:
2316 buf
= USER_LABEL_PREFIX
;
2319 case T_REGISTER_PREFIX_TYPE
:
2320 buf
= REGISTER_PREFIX
;
2324 buf
= (char *) alloca (4 * sizeof (int));
2325 sprintf (buf
, "%d", hp
->value
.ival
);
2326 #ifdef STDC_0_IN_SYSTEM_HEADERS
2327 if (ip
->system_header_p
2328 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
2329 && ! cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", -1, -1))
2333 if (pcp_inside_if
&& pcp_outfile
)
2334 /* Output a precondition for this macro use */
2335 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2341 long line
= ip
->lineno
;
2342 long col
= ip
->colno
;
2343 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2345 buf
= (char *) alloca (10);
2346 sprintf (buf
, "%ld", line
);
2352 buf
= (char *) alloca (20);
2353 timebuf
= timestamp (pfile
);
2354 if (hp
->type
== T_DATE
)
2355 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2356 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2358 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2362 case T_SPEC_DEFINED
:
2363 buf
= " 0 "; /* Assume symbol is not defined */
2364 ip
= CPP_BUFFER (pfile
);
2365 SKIP_WHITE_SPACE (ip
->cur
);
2366 if (*ip
->cur
== '(')
2369 ip
->cur
++; /* Skip over the paren */
2370 SKIP_WHITE_SPACE (ip
->cur
);
2373 if (!is_idstart
[*ip
->cur
])
2375 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
2377 if (hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1))
2380 if (pcp_outfile
&& pcp_inside_if
2381 && (hp
->type
== T_CONST
2382 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2383 /* Output a precondition for this macro use. */
2384 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2390 if (pcp_outfile
&& pcp_inside_if
)
2392 /* Output a precondition for this macro use */
2393 U_CHAR
*cp
= ip
->bufp
;
2394 fprintf (pcp_outfile
, "#undef ");
2395 while (is_idchar
[*cp
]) /* Ick! */
2396 fputc (*cp
++, pcp_outfile
);
2397 putc ('\n', pcp_outfile
);
2400 while (is_idchar
[*ip
->cur
])
2402 SKIP_WHITE_SPACE (ip
->cur
);
2405 if (*ip
->cur
!= ')')
2413 cpp_error (pfile
, "`defined' without an identifier");
2417 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2421 CPP_RESERVE (pfile
, len
+ 1);
2422 CPP_PUTS_Q (pfile
, buf
, len
);
2423 CPP_NUL_TERMINATE_Q (pfile
);
2428 /* Write out a #define command for the special named MACRO_NAME
2429 to PFILE's token_buffer. */
2432 dump_special_to_buffer (pfile
, macro_name
)
2436 static char define_directive
[] = "#define ";
2437 int macro_name_length
= strlen (macro_name
);
2438 output_line_command (pfile
, 0, same_file
);
2439 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2440 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2441 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2442 CPP_PUTC_Q (pfile
, ' ');
2443 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2444 CPP_PUTC (pfile
, '\n');
2447 /* Initialize the built-in macros. */
2450 initialize_builtins (pfile
)
2453 install ((U_CHAR
*)"__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2454 install ((U_CHAR
*)"__DATE__", -1, T_DATE
, 0, 0, -1);
2455 install ((U_CHAR
*)"__FILE__", -1, T_FILE
, 0, 0, -1);
2456 install ((U_CHAR
*)"__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2457 install ((U_CHAR
*)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2458 install ((U_CHAR
*)"__VERSION__", -1, T_VERSION
, 0, 0, -1);
2459 #ifndef NO_BUILTIN_SIZE_TYPE
2460 install ((U_CHAR
*)"__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2462 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2463 install ((U_CHAR
*)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2465 install ((U_CHAR
*)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2466 install ((U_CHAR
*)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2467 install ((U_CHAR
*)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2468 install ((U_CHAR
*)"__TIME__", -1, T_TIME
, 0, 0, -1);
2469 if (!CPP_TRADITIONAL (pfile
))
2470 install ((U_CHAR
*)"__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2471 if (CPP_OPTIONS (pfile
)->objc
)
2472 install ((U_CHAR
*)"__OBJC__", -1, T_CONST
, 1, 0, -1);
2473 /* This is supplied using a -D by the compiler driver
2474 so that it is present only when truly compiling with GNU C. */
2475 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2477 if (CPP_OPTIONS (pfile
)->debug_output
)
2479 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2480 dump_special_to_buffer (pfile
, "__VERSION__");
2481 #ifndef NO_BUILTIN_SIZE_TYPE
2482 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2484 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2485 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2487 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2488 dump_special_to_buffer (pfile
, "__DATE__");
2489 dump_special_to_buffer (pfile
, "__TIME__");
2490 if (!CPP_TRADITIONAL (pfile
))
2491 dump_special_to_buffer (pfile
, "__STDC__");
2492 if (CPP_OPTIONS (pfile
)->objc
)
2493 dump_special_to_buffer (pfile
, "__OBJC__");
2497 /* Return 1 iff a token ending in C1 followed directly by a token C2
2498 could cause mis-tokenization. */
2501 unsafe_chars (c1
, c2
)
2507 if (c2
== c1
|| c2
== '=')
2511 case '0': case '1': case '2': case '3': case '4':
2512 case '5': case '6': case '7': case '8': case '9':
2513 case 'e': case 'E': case 'p': case 'P':
2514 if (c2
== '-' || c2
== '+')
2515 return 1; /* could extend a pre-processing number */
2518 if (c2
== '\'' || c2
== '\"')
2519 return 1; /* Could turn into L"xxx" or L'xxx'. */
2523 case 'a': case 'b': case 'c': case 'd': case 'f':
2524 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2525 case 'm': case 'n': case 'o': case 'q': case 'r':
2526 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2528 case 'A': case 'B': case 'C': case 'D': case 'F':
2529 case 'G': case 'H': case 'I': case 'J': case 'K':
2530 case 'M': case 'N': case 'O': case 'Q': case 'R':
2531 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2533 /* We're in the middle of either a name or a pre-processing number. */
2534 return (is_idchar
[c2
] || c2
== '.');
2535 case '<': case '>': case '!': case '%': case '#': case ':':
2536 case '^': case '&': case '|': case '*': case '/': case '=':
2537 return (c2
== c1
|| c2
== '=');
2542 /* Expand a macro call.
2543 HP points to the symbol that is the macro being called.
2544 Put the result of expansion onto the input stack
2545 so that subsequent input by our caller will use it.
2547 If macro wants arguments, caller has already verified that
2548 an argument list follows; arguments come from the input stack. */
2551 macroexpand (pfile
, hp
)
2556 DEFINITION
*defn
= hp
->value
.defn
;
2557 register U_CHAR
*xbuf
;
2558 long start_line
, start_column
;
2560 struct argdata
*args
;
2561 long old_written
= CPP_WRITTEN (pfile
);
2563 int start_line
= instack
[indepth
].lineno
;
2565 int rest_args
, rest_zero
;
2569 CHECK_DEPTH (return;);
2573 /* This macro is being used inside a #if, which means it must be */
2574 /* recorded as a precondition. */
2575 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2576 dump_single_macro (hp
, pcp_outfile
);
2579 pfile
->output_escapes
++;
2580 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2582 nargs
= defn
->nargs
;
2586 enum cpp_token token
;
2588 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2590 for (i
= 0; i
< nargs
; i
++)
2592 args
[i
].raw
= args
[i
].expanded
= 0;
2593 args
[i
].raw_length
= 0;
2594 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2595 args
[i
].use_count
= 0;
2598 /* Parse all the macro args that are supplied. I counts them.
2599 The first NARGS args are stored in ARGS.
2600 The rest are discarded. If rest_args is set then we assume
2601 macarg absorbed the rest of the args. */
2605 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2610 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2612 /* if we are working on last arg which absorbs rest of args... */
2613 if (i
== nargs
- 1 && defn
->rest_args
)
2615 args
[i
].raw
= CPP_WRITTEN (pfile
);
2616 token
= macarg (pfile
, rest_args
);
2617 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2618 args
[i
].newlines
= 0; /* FIXME */
2621 token
= macarg (pfile
, 0);
2622 if (token
== CPP_EOF
|| token
== CPP_POP
)
2624 cpp_error_with_line (pfile
, start_line
, start_column
,
2625 "unterminated macro call");
2629 } while (token
== CPP_COMMA
);
2631 /* If we got one arg but it was just whitespace, call that 0 args. */
2634 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2635 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2636 /* cpp.texi says for foo ( ) we provide one argument.
2637 However, if foo wants just 0 arguments, treat this as 0. */
2639 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2644 /* Don't output an error message if we have already output one for
2645 a parse error above. */
2647 if (nargs
== 0 && i
> 0)
2649 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2653 /* traditional C allows foo() if foo wants one argument. */
2654 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2656 /* the rest args token is allowed to absorb 0 tokens */
2657 else if (i
== nargs
- 1 && defn
->rest_args
)
2660 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2662 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2664 cpp_error (pfile
, "macro `%s' used with only %d args",
2670 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2674 /* If macro wants zero args, we parsed the arglist for checking only.
2675 Read directly from the macro definition. */
2678 xbuf
= defn
->expansion
;
2679 xbuf_len
= defn
->length
;
2683 register U_CHAR
*exp
= defn
->expansion
;
2684 register int offset
; /* offset in expansion,
2685 copied a piece at a time */
2686 register int totlen
; /* total amount of exp buffer filled so far */
2688 register struct reflist
*ap
, *last_ap
;
2690 /* Macro really takes args. Compute the expansion of this call. */
2692 /* Compute length in characters of the macro's expansion.
2693 Also count number of times each arg is used. */
2694 xbuf_len
= defn
->length
;
2695 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2699 register struct argdata
*arg
= &args
[ap
->argno
];
2700 /* Stringify it it hasn't already been */
2701 if (arg
->stringified_length
< 0)
2703 int arglen
= arg
->raw_length
;
2707 /* Initially need_space is -1. Otherwise, 1 means the
2708 previous character was a space, but we suppressed it;
2709 0 means the previous character was a non-space. */
2710 int need_space
= -1;
2712 arg
->stringified
= CPP_WRITTEN (pfile
);
2713 if (!CPP_TRADITIONAL (pfile
))
2714 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2715 for (; i
< arglen
; i
++)
2717 c
= (ARG_BASE
+ arg
->raw
)[i
];
2721 /* Internal sequences of whitespace are replaced by
2722 one space except within an string or char token.*/
2725 if (CPP_WRITTEN (pfile
) > arg
->stringified
2726 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2728 /* "@ " escape markers are removed */
2729 CPP_ADJUST_WRITTEN (pfile
, -1);
2732 if (need_space
== 0)
2736 else if (need_space
> 0)
2737 CPP_PUTC (pfile
, ' ');
2752 else if (c
== '\"' || c
== '\'')
2756 /* Escape these chars */
2757 if (c
== '\"' || (in_string
&& c
== '\\'))
2758 CPP_PUTC (pfile
, '\\');
2760 CPP_PUTC (pfile
, c
);
2763 CPP_RESERVE (pfile
, 4);
2764 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o",
2766 CPP_ADJUST_WRITTEN (pfile
, 4);
2769 if (!CPP_TRADITIONAL (pfile
))
2770 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2771 arg
->stringified_length
2772 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2774 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2776 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2777 /* Add 4 for two newline-space markers to prevent
2778 token concatenation. */
2779 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2782 /* We have an ordinary (expanded) occurrence of the arg.
2783 So compute its expansion, if we have not already. */
2784 if (args
[ap
->argno
].expand_length
< 0)
2786 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2787 cpp_expand_to_buffer (pfile
,
2788 ARG_BASE
+ args
[ap
->argno
].raw
,
2789 args
[ap
->argno
].raw_length
);
2791 args
[ap
->argno
].expand_length
2792 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2795 /* Add 4 for two newline-space markers to prevent
2796 token concatenation. */
2797 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2799 if (args
[ap
->argno
].use_count
< 10)
2800 args
[ap
->argno
].use_count
++;
2803 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2805 /* Generate in XBUF the complete expansion
2806 with arguments substituted in.
2807 TOTLEN is the total size generated so far.
2808 OFFSET is the index in the definition
2809 of where we are copying from. */
2810 offset
= totlen
= 0;
2811 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2812 last_ap
= ap
, ap
= ap
->next
)
2814 register struct argdata
*arg
= &args
[ap
->argno
];
2815 int count_before
= totlen
;
2817 /* Add chars to XBUF. */
2818 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2819 xbuf
[totlen
++] = exp
[offset
];
2821 /* If followed by an empty rest arg with concatenation,
2822 delete the last run of nonwhite chars. */
2823 if (rest_zero
&& totlen
> count_before
2824 && ((ap
->rest_args
&& ap
->raw_before
)
2825 || (last_ap
!= NULL
&& last_ap
->rest_args
2826 && last_ap
->raw_after
)))
2828 /* Delete final whitespace. */
2829 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2832 /* Delete the nonwhites before them. */
2833 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2837 if (ap
->stringify
!= 0)
2839 bcopy (ARG_BASE
+ arg
->stringified
,
2840 xbuf
+ totlen
, arg
->stringified_length
);
2841 totlen
+= arg
->stringified_length
;
2843 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2845 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2846 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2849 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2850 while (p1
!= l1
&& is_idchar
[*p1
])
2851 xbuf
[totlen
++] = *p1
++;
2852 /* Delete any no-reexpansion marker that follows
2853 an identifier at the beginning of the argument
2854 if the argument is concatenated with what precedes it. */
2855 if (p1
[0] == '@' && p1
[1] == '-')
2860 /* Arg is concatenated after: delete trailing whitespace,
2861 whitespace markers, and no-reexpansion markers. */
2864 if (is_space
[l1
[-1]]) l1
--;
2865 else if (l1
[-1] == '-')
2867 U_CHAR
*p2
= l1
- 1;
2868 /* If a `-' is preceded by an odd number of newlines then it
2869 and the last newline are a no-reexpansion marker. */
2870 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2871 if ((l1
- 1 - p2
) & 1) {
2880 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2885 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2886 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2887 && !CPP_TRADITIONAL(pfile
)
2888 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2890 xbuf
[totlen
++] = '@';
2891 xbuf
[totlen
++] = ' ';
2894 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2895 totlen
+= arg
->expand_length
;
2897 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2898 && !CPP_TRADITIONAL(pfile
)
2899 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2901 xbuf
[totlen
++] = '@';
2902 xbuf
[totlen
++] = ' ';
2905 /* If a macro argument with newlines is used multiple times,
2906 then only expand the newlines once. This avoids creating
2907 output lines which don't correspond to any input line,
2908 which confuses gdb and gcov. */
2909 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2911 /* Don't bother doing change_newlines for subsequent
2915 = change_newlines (expanded
, arg
->expand_length
);
2919 if (totlen
> xbuf_len
)
2923 /* if there is anything left of the definition
2924 after handling the arg list, copy that in too. */
2926 for (i
= offset
; i
< defn
->length
; i
++)
2928 /* if we've reached the end of the macro */
2931 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2932 && last_ap
->raw_after
))
2933 xbuf
[totlen
++] = exp
[i
];
2941 pfile
->output_escapes
--;
2943 /* Now put the expansion on the input stack
2944 so our caller will commence reading from it. */
2945 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
2946 CPP_BUFFER (pfile
)->has_escapes
= 1;
2948 /* Pop the space we've used in the token_buffer for argument expansion. */
2949 CPP_SET_WRITTEN (pfile
, old_written
);
2951 /* Recursive macro use sometimes works traditionally.
2952 #define foo(x,y) bar (x (y,0), y)
2955 if (!CPP_TRADITIONAL (pfile
))
2956 hp
->type
= T_DISABLED
;
2960 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
2962 register U_CHAR
*xbuf
;
2966 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
2969 mbuf
->cleanup
= macro_cleanup
;
2972 /* The first chars of the expansion should be a "@ " added by
2973 collect_expansion. This is to prevent accidental token-pasting
2974 between the text preceding the macro invocation, and the macro
2977 We would like to avoid adding unneeded spaces (for the sake of
2978 tools that use cpp, such as imake). In some common cases we can
2979 tell that it is safe to omit the space.
2981 The character before the macro invocation cannot have been an
2982 idchar (or else it would have been pasted with the idchars of
2983 the macro name). Therefore, if the first non-space character
2984 of the expansion is an idchar, we do not need the extra space
2985 to prevent token pasting.
2987 Also, we don't need the extra space if the first char is '(',
2988 or some other (less common) characters. */
2990 if (xbuf
[0] == '@' && xbuf
[1] == ' '
2991 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
2992 || xbuf
[2] == '\"'))
2996 /* Like cpp_get_token, except that it does not read past end-of-line.
2997 Also, horizontal space is skipped, and macros are popped. */
2999 static enum cpp_token
3000 get_directive_token (pfile
)
3005 long old_written
= CPP_WRITTEN (pfile
);
3006 enum cpp_token token
;
3007 cpp_skip_hspace (pfile
);
3008 if (PEEKC () == '\n')
3010 token
= cpp_get_token (pfile
);
3014 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3016 /* ... else fall though ... */
3017 case CPP_HSPACE
: case CPP_COMMENT
:
3018 CPP_SET_WRITTEN (pfile
, old_written
);
3026 /* Handle #include and #import.
3027 This function expects to see "fname" or <fname> on the input.
3029 The input is normally in part of the output_buffer following
3030 CPP_WRITTEN, and will get overwritten by output_line_command.
3031 I.e. in input file specification has been popped by handle_directive.
3035 do_include (pfile
, keyword
, unused1
, unused2
)
3037 struct directive
*keyword
;
3038 U_CHAR
*unused1
, *unused2
;
3040 int importing
= (keyword
->type
== T_IMPORT
);
3041 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3042 char *fname
; /* Dynamically allocated fname buffer */
3045 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3046 enum cpp_token token
;
3048 /* Chain of dirs to search */
3049 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3050 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3051 struct file_name_list
*searchptr
= 0;
3052 long old_written
= CPP_WRITTEN (pfile
);
3056 int f
; /* file number */
3058 int retried
= 0; /* Have already tried macro
3059 expanding the include line */
3060 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3065 f
= -1; /* JF we iz paranoid! */
3067 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3070 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
3072 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
3075 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3076 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3077 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3079 pfile
->import_warning
= 1;
3080 cpp_warning (pfile
, "using `#import' is not recommended");
3081 cpp_notice ("The fact that a certain header file need not be processed more than once\n\
3082 should be indicated in the header file, not where it is used.\n\
3083 The best way to do this is with a conditional of this form:\n\
3085 #ifndef _FOO_H_INCLUDED\n\
3086 #define _FOO_H_INCLUDED\n\
3087 ... <real contents of file> ...\n\
3088 #endif /* Not _FOO_H_INCLUDED */\n\
3090 Then users can use `#include' any number of times.\n\
3091 GNU C automatically avoids processing the file more than once\n\
3092 when it is equipped with such a conditional.\n");
3095 pfile
->parsing_include_directive
++;
3096 token
= get_directive_token (pfile
);
3097 pfile
->parsing_include_directive
--;
3099 if (token
== CPP_STRING
)
3101 /* FIXME - check no trailing garbage */
3102 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3103 fend
= CPP_PWRITTEN (pfile
) - 1;
3104 if (fbeg
[-1] == '<')
3107 /* If -I-, start with the first -I dir after the -I-. */
3108 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3109 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3111 /* If -I- was specified, don't search current dir, only spec'd ones. */
3112 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3114 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3115 /* We have "filename". Figure out directory this source
3116 file is coming from and put it on the front of the list. */
3118 for ( ; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3123 if ((nam
= fp
->nominal_fname
) != NULL
)
3125 /* Found a named file. Figure out dir of the file,
3126 and put it in front of the search list. */
3127 dsp
[0].next
= search_start
;
3130 ep
= rindex (nam
, '/');
3132 ep
= rindex (nam
, ']');
3133 if (ep
== NULL
) ep
= rindex (nam
, '>');
3134 if (ep
== NULL
) ep
= rindex (nam
, ':');
3135 if (ep
!= NULL
) ep
++;
3140 dsp
[0].fname
= (char *) alloca (n
+ 1);
3141 strncpy (dsp
[0].fname
, nam
, n
);
3142 dsp
[0].fname
[n
] = '\0';
3143 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3144 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3148 dsp
[0].fname
= 0; /* Current directory */
3150 dsp
[0].got_name_map
= 0;
3157 else if (token
== CPP_NAME
)
3160 * Support '#include xyz' like VAX-C to allow for easy use of all the
3161 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3162 * code from case '<' is repeated here) and generates a warning.
3165 "VAX-C-style include specification found, use '#include <filename.h>' !");
3167 /* If -I-, start with the first -I dir after the -I-. */
3168 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3169 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3170 fbeg
= pfile
->token_buffer
+ old_written
;
3171 fend
= CPP_PWRITTEN (pfile
);
3177 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3178 CPP_SET_WRITTEN (pfile
, old_written
);
3179 skip_rest_of_line (pfile
);
3185 token
= get_directive_token (pfile
);
3186 if (token
!= CPP_VSPACE
)
3188 cpp_error (pfile
, "junk at end of `#include'");
3189 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3190 token
= get_directive_token (pfile
);
3193 /* For #include_next, skip in the search path
3194 past the dir in which the containing file was found. */
3197 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3198 for (; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3199 if (fp
->fname
!= NULL
)
3201 /* fp->dir is null if the containing file was specified with
3202 an absolute file name. In that case, don't skip anything. */
3203 if (fp
->dir
== SELF_DIR_DUMMY
)
3204 search_start
= CPP_OPTIONS (pfile
)->include
;
3206 search_start
= fp
->dir
->next
;
3211 CPP_SET_WRITTEN (pfile
, old_written
);
3217 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3221 /* Allocate this permanently, because it gets stored in the definitions
3223 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3224 /* + 2 above for slash and terminating null. */
3225 /* + 2 added for '.h' on VMS (to support '#include filename') */
3227 /* If specified file name is absolute, just open it. */
3230 strncpy (fname
, fbeg
, flen
);
3232 if (redundant_include_p (pfile
, fname
))
3235 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3237 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3239 return 0; /* Already included this file */
3241 /* Search directory path, trying to open the file.
3242 Copy each filename tried into FNAME. */
3244 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3245 if (searchptr
->fname
) {
3246 /* The empty string in a search path is ignored.
3247 This makes it possible to turn off entirely
3248 a standard piece of the list. */
3249 if (searchptr
->fname
[0] == 0)
3251 strcpy (fname
, searchptr
->fname
);
3252 strcat (fname
, "/");
3253 fname
[strlen (fname
) + flen
] = 0;
3257 strncat (fname
, fbeg
, flen
);
3259 /* Change this 1/2 Unix 1/2 VMS file specification into a
3260 full VMS file specification */
3261 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3262 /* Fix up the filename */
3263 hack_vms_include_specification (fname
);
3265 /* This is a normal VMS filespec, so use it unchanged. */
3266 strncpy (fname
, fbeg
, flen
);
3268 /* if it's '#include filename', add the missing .h */
3269 if (index(fname
,'.')==NULL
) {
3270 strcat (fname
, ".h");
3274 /* ??? There are currently 3 separate mechanisms for avoiding processing
3275 of redundant include files: #import, #pragma once, and
3276 redundant_include_p. It would be nice if they were unified. */
3277 if (redundant_include_p (pfile
, fname
))
3280 f
= lookup_import (pfile
, fname
, searchptr
);
3282 f
= open_include_file (pfile
, fname
, searchptr
);
3284 return 0; /* Already included this file */
3286 else if (f
== -1 && errno
== EACCES
)
3287 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3297 /* A file that was not found. */
3298 strncpy (fname
, fbeg
, flen
);
3300 /* If generating dependencies and -MG was specified, we assume missing
3301 files are leaf files, living in the same directory as the source file
3302 or other similar place; these missing files may be generated from
3303 other files and may not exist yet (eg: y.tab.h). */
3305 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3306 && CPP_PRINT_DEPS (pfile
)
3307 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3309 /* If it was requested as a system header file,
3310 then assume it belongs in the first place to look for such. */
3313 for (searchptr
= search_start
; searchptr
;
3314 searchptr
= searchptr
->next
)
3316 if (searchptr
->fname
)
3320 if (searchptr
->fname
[0] == 0)
3322 p
= (char *) alloca (strlen (searchptr
->fname
)
3323 + strlen (fname
) + 2);
3324 strcpy (p
, searchptr
->fname
);
3327 deps_output (pfile
, p
, ' ');
3334 /* Otherwise, omit the directory, as if the file existed
3335 in the directory with the source. */
3336 deps_output (pfile
, fname
, ' ');
3339 /* If -M was specified, and this header file won't be added to the
3340 dependency list, then don't count this as an error, because we can
3341 still produce correct output. Otherwise, we can't produce correct
3342 output, because there may be dependencies we need inside the missing
3343 file, and we don't know what directory this missing file exists in.*/
3344 else if (CPP_PRINT_DEPS (pfile
)
3345 && (CPP_PRINT_DEPS (pfile
)
3346 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3347 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3348 else if (search_start
)
3349 cpp_error_from_errno (pfile
, fname
);
3351 cpp_error (pfile
, "No include path in which to find %s", fname
);
3354 /* Check to see if this include file is a once-only include file.
3357 struct file_name_list
*ptr
;
3359 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3360 if (!strcmp (ptr
->fname
, fname
)) {
3362 return 0; /* This file was once'd. */
3366 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3367 if (!strcmp (ptr
->fname
, fname
))
3368 break; /* This file was included before. */
3372 /* This is the first time for this file. */
3373 /* Add it to list of files included. */
3375 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3376 ptr
->control_macro
= 0;
3377 ptr
->c_system_include_path
= 0;
3378 ptr
->next
= pfile
->all_include_files
;
3379 pfile
->all_include_files
= ptr
;
3380 ptr
->fname
= savestring (fname
);
3381 ptr
->got_name_map
= 0;
3383 /* For -M, add this file to the dependencies. */
3384 if (CPP_PRINT_DEPS (pfile
)
3385 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3386 deps_output (pfile
, fname
, ' ');
3389 /* Handle -H option. */
3390 if (CPP_OPTIONS(pfile
)->print_include_names
)
3392 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3393 while ((buf
= CPP_PREV_BUFFER (buf
)) != CPP_NULL_BUFFER (pfile
))
3395 fprintf (stderr
, "%s\n", fname
);
3399 pfile
->system_include_depth
++;
3401 /* Actually process the file. */
3403 /* Record file on "seen" list for #import. */
3404 add_import (pfile
, f
, fname
);
3406 pcftry
= (char *) alloca (strlen (fname
) + 30);
3418 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3420 pcf
= open (pcftry
, O_RDONLY
, 0666);
3426 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3428 || stat_f
.st_dev
!= s
.st_dev
)
3430 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3431 /* Don't need it any more. */
3436 /* Don't need it at all. */
3441 } while (pcf
!= -1 && !pcfbuf
);
3445 /* Actually process the file */
3446 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3448 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3449 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3451 output_line_command (pfile
, 0, enter_file
);
3452 pfile
->only_seen_white
= 2;
3456 pfile
->system_include_depth
--;
3461 /* Return nonzero if there is no need to include file NAME
3462 because it has already been included and it contains a conditional
3463 to make a repeated include do nothing. */
3466 redundant_include_p (pfile
, name
)
3470 struct file_name_list
*l
= pfile
->all_include_files
;
3471 for (; l
; l
= l
->next
)
3472 if (! strcmp (name
, l
->fname
)
3474 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3479 /* Return nonzero if the given FILENAME is an absolute pathname which
3480 designates a file within one of the known "system" include file
3481 directories. We assume here that if the given FILENAME looks like
3482 it is the name of a file which resides either directly in a "system"
3483 include file directory, or within any subdirectory thereof, then the
3484 given file must be a "system" include file. This function tells us
3485 if we should suppress pedantic errors/warnings for the given FILENAME.
3487 The value is 2 if the file is a C-language system header file
3488 for which C++ should (on most systems) assume `extern "C"'. */
3491 is_system_include (pfile
, filename
)
3493 register char *filename
;
3495 struct file_name_list
*searchptr
;
3497 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3498 searchptr
= searchptr
->next
)
3499 if (searchptr
->fname
) {
3500 register char *sys_dir
= searchptr
->fname
;
3501 register unsigned length
= strlen (sys_dir
);
3503 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3505 if (searchptr
->c_system_include_path
)
3516 * Install a name in the assertion hash table.
3518 * If LEN is >= 0, it is the length of the name.
3519 * Otherwise, compute the length by scanning the entire name.
3521 * If HASH is >= 0, it is the precomputed hash code.
3522 * Otherwise, compute the hash code.
3525 static ASSERTION_HASHNODE
*
3526 assertion_install (pfile
, name
, len
, hash
)
3532 register ASSERTION_HASHNODE
*hp
;
3533 register int i
, bucket
;
3534 register U_CHAR
*p
, *q
;
3536 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3537 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3539 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3540 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3541 pfile
->assertion_hashtab
[bucket
] = hp
;
3543 if (hp
->next
!= NULL
)
3544 hp
->next
->prev
= hp
;
3547 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3550 for (i
= 0; i
< len
; i
++)
3556 * find the most recent hash node for name name (ending with first
3557 * non-identifier char) installed by install
3559 * If LEN is >= 0, it is the length of the name.
3560 * Otherwise, compute the length by scanning the entire name.
3562 * If HASH is >= 0, it is the precomputed hash code.
3563 * Otherwise, compute the hash code.
3566 static ASSERTION_HASHNODE
*
3567 assertion_lookup (pfile
, name
, len
, hash
)
3573 register ASSERTION_HASHNODE
*bucket
;
3575 bucket
= pfile
->assertion_hashtab
[hash
];
3577 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3579 bucket
= bucket
->next
;
3585 delete_assertion (hp
)
3586 ASSERTION_HASHNODE
*hp
;
3588 struct tokenlist_list
*tail
;
3589 if (hp
->prev
!= NULL
)
3590 hp
->prev
->next
= hp
->next
;
3591 if (hp
->next
!= NULL
)
3592 hp
->next
->prev
= hp
->prev
;
3594 for (tail
= hp
->value
; tail
; )
3596 struct tokenlist_list
*next
= tail
->next
;
3597 free_token_list (tail
->tokens
);
3602 /* Make sure that the bucket chain header that
3603 the deleted guy was on points to the right thing afterwards. */
3604 if (hp
== *hp
->bucket_hdr
)
3605 *hp
->bucket_hdr
= hp
->next
;
3610 /* Convert a character string literal into a nul-terminated string.
3611 The input string is [IN ... LIMIT).
3612 The result is placed in RESULT. RESULT can be the same as IN.
3613 The value returned in the end of the string written to RESULT,
3614 or NULL on error. */
3617 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3619 register U_CHAR
*result
, *in
, *limit
;
3639 char *bpc
= (char *) in
;
3640 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3641 in
= (U_CHAR
*) bpc
;
3643 *result
++ = (U_CHAR
)c
;
3646 /* else fall through */
3656 * interpret #line command. Remembers previously seen fnames
3657 * in its very own hash table.
3659 #define FNAME_HASHSIZE 37
3662 do_line (pfile
, keyword
)
3664 struct directive
*keyword
;
3666 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3668 long old_written
= CPP_WRITTEN (pfile
);
3669 enum file_change_code file_change
= same_file
;
3670 enum cpp_token token
;
3673 token
= get_directive_token (pfile
);
3675 if (token
!= CPP_NUMBER
3676 || !isdigit(pfile
->token_buffer
[old_written
]))
3678 cpp_error (pfile
, "invalid format `#line' command");
3679 goto bad_line_directive
;
3682 /* The Newline at the end of this line remains to be processed.
3683 To put the next line at the specified line number,
3684 we must store a line number now that is one less. */
3685 new_lineno
= atoi ((char *)(pfile
->token_buffer
+ old_written
)) - 1;
3686 CPP_SET_WRITTEN (pfile
, old_written
);
3688 /* NEW_LINENO is one less than the actual line number here. */
3689 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3690 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3692 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3693 if (PEEKC() && !is_space
[PEEKC()]) {
3694 cpp_error (pfile
, "invalid format `#line' command");
3695 goto bad_line_directive
;
3699 token
= get_directive_token (pfile
);
3701 if (token
== CPP_STRING
) {
3702 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3704 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3705 HASHNODE
*hp
, **hash_bucket
;
3710 /* Turn the file name, which is a character string literal,
3711 into a null-terminated string. Do this in place. */
3712 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3713 if (end_name
== NULL
)
3715 cpp_error (pfile
, "invalid format `#line' command");
3716 goto bad_line_directive
;
3719 fname_length
= end_name
- fname
;
3721 num_start
= CPP_WRITTEN (pfile
);
3722 token
= get_directive_token (pfile
);
3723 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3724 p
= pfile
->token_buffer
+ num_start
;
3725 if (CPP_PEDANTIC (pfile
))
3726 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3728 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3730 cpp_error (pfile
, "invalid format `#line' command");
3731 goto bad_line_directive
;
3734 file_change
= enter_file
;
3736 file_change
= leave_file
;
3738 ip
->system_header_p
= 1;
3739 else /* if (*p == 4) */
3740 ip
->system_header_p
= 2;
3742 CPP_SET_WRITTEN (pfile
, num_start
);
3743 token
= get_directive_token (pfile
);
3744 p
= pfile
->token_buffer
+ num_start
;
3745 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3746 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3747 token
= get_directive_token (pfile
);
3749 if (token
!= CPP_VSPACE
) {
3750 cpp_error (pfile
, "invalid format `#line' command");
3751 goto bad_line_directive
;
3755 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3756 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3757 if (hp
->length
== fname_length
3758 && strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3759 ip
->nominal_fname
= hp
->value
.cpval
;
3763 /* Didn't find it; cons up a new one. */
3764 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3765 hp
->next
= *hash_bucket
;
3768 hp
->length
= fname_length
;
3769 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3770 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3773 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3774 cpp_error (pfile
, "invalid format `#line' command");
3775 goto bad_line_directive
;
3778 ip
->lineno
= new_lineno
;
3780 skip_rest_of_line (pfile
);
3781 CPP_SET_WRITTEN (pfile
, old_written
);
3782 output_line_command (pfile
, 0, file_change
);
3787 * remove the definition of a symbol from the symbol table.
3788 * according to un*x /lib/cpp, it is not an error to undef
3789 * something that has no definitions, so it isn't one here either.
3793 do_undef (pfile
, keyword
, buf
, limit
)
3795 struct directive
*keyword
;
3796 U_CHAR
*buf
, *limit
;
3800 U_CHAR
*orig_buf
= buf
;
3803 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3804 if (pcp_outfile
&& keyword
)
3805 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3808 SKIP_WHITE_SPACE (buf
);
3809 sym_length
= check_macro_name (pfile
, buf
, 0);
3811 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3813 /* If we are generating additional info for debugging (with -g) we
3814 need to pass through all effective #undef commands. */
3815 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3816 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3817 if (hp
->type
!= T_MACRO
)
3818 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3822 if (CPP_PEDANTIC (pfile
)) {
3824 SKIP_WHITE_SPACE (buf
);
3826 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3832 * Report an error detected by the program we are processing.
3833 * Use the text of the line in the error message.
3834 * (We use error because it prints the filename & line#.)
3838 do_error (pfile
, keyword
, buf
, limit
)
3840 struct directive
*keyword
;
3841 U_CHAR
*buf
, *limit
;
3843 int length
= limit
- buf
;
3844 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3845 bcopy (buf
, copy
, length
);
3847 SKIP_WHITE_SPACE (copy
);
3848 cpp_error (pfile
, "#error %s", copy
);
3853 * Report a warning detected by the program we are processing.
3854 * Use the text of the line in the warning message, then continue.
3855 * (We use error because it prints the filename & line#.)
3859 do_warning (pfile
, keyword
, buf
, limit
)
3861 struct directive
*keyword
;
3862 U_CHAR
*buf
, *limit
;
3864 int length
= limit
- buf
;
3865 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3866 bcopy (buf
, copy
, length
);
3868 SKIP_WHITE_SPACE (copy
);
3870 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3871 cpp_pedwarn ("ANSI C does not allow `#warning'");
3873 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3874 if -pedantic-errors is given, #warning should cause an error. */
3875 cpp_pedwarn (pfile
, "#warning %s", copy
);
3879 /* Remember the name of the current file being read from so that we can
3880 avoid ever including it again. */
3886 cpp_buffer
*ip
= NULL
;
3887 struct file_name_list
*new;
3889 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3891 if (ip
== CPP_NULL_BUFFER (pfile
))
3893 if (ip
->fname
!= NULL
)
3898 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3899 new->next
= pfile
->dont_repeat_files
;
3900 pfile
->dont_repeat_files
= new;
3901 new->fname
= savestring (ip
->fname
);
3902 new->control_macro
= 0;
3903 new->got_name_map
= 0;
3904 new->c_system_include_path
= 0;
3909 /* Report program identification. */
3912 do_ident (pfile
, keyword
, buf
, limit
)
3914 struct directive
*keyword
;
3915 U_CHAR
*buf
, *limit
;
3917 /* long old_written = CPP_WRITTEN (pfile);*/
3920 /* Allow #ident in system headers, since that's not user's fault. */
3921 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3922 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3924 /* Leave rest of line to be read by later calls to cpp_get_token. */
3929 /* #pragma and its argument line have already been copied to the output file.
3930 Just check for some recognized pragmas that need validation here. */
3933 do_pragma (pfile
, keyword
, buf
, limit
)
3935 struct directive
*keyword
;
3936 U_CHAR
*buf
, *limit
;
3938 while (*buf
== ' ' || *buf
== '\t')
3940 if (!strncmp (buf
, "once", 4)) {
3941 /* Allow #pragma once in system headers, since that's not the user's
3943 if (!CPP_BUFFER (pfile
)->system_header_p
)
3944 cpp_warning (pfile
, "`#pragma once' is obsolete");
3948 if (!strncmp (buf
, "implementation", 14)) {
3949 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3950 been included yet. */
3951 struct file_name_list
*ptr
;
3952 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
3954 SKIP_WHITE_SPACE (p
);
3955 if (*p
== '\n' || *p
!= '\"')
3959 p
= (U_CHAR
*) index (fname
, '\"');
3960 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
3962 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3963 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
3964 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
3965 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
3967 "`#pragma implementation' for `%s' appears after file is included",
3976 /* This was a fun hack, but #pragma seems to start to be useful.
3977 By failing to recognize it, we pass it through unchanged to cc1. */
3980 * the behavior of the #pragma directive is implementation defined.
3981 * this implementation defines it as follows.
3988 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
3991 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
3993 execl ("/usr/games/hack", "#pragma", 0);
3994 execl ("/usr/games/rogue", "#pragma", 0);
3995 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
3996 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
3998 fatal ("You are in a maze of twisty compiler features, all different");
4002 /* Just ignore #sccs, on systems where we define it at all. */
4005 do_sccs (pfile
, keyword
, buf
, limit
)
4007 struct directive
*keyword
;
4008 U_CHAR
*buf
, *limit
;
4010 if (CPP_PEDANTIC (pfile
))
4011 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4016 * handle #if command by
4017 * 1) inserting special `defined' keyword into the hash table
4018 * that gets turned into 0 or 1 by special_symbol (thus,
4019 * if the luser has a symbol called `defined' already, it won't
4020 * work inside the #if command)
4021 * 2) rescan the input into a temporary output buffer
4022 * 3) pass the output buffer to the yacc parser and collect a value
4023 * 4) clean up the mess left from steps 1 and 2.
4024 * 5) call conditional_skip to skip til the next #endif (etc.),
4025 * or not, depending on the value from step 3.
4029 do_if (pfile
, keyword
, buf
, limit
)
4031 struct directive
*keyword
;
4032 U_CHAR
*buf
, *limit
;
4034 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4035 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4040 * handle a #elif directive by not changing if_stack either.
4041 * see the comment above do_else.
4045 do_elif (pfile
, keyword
, buf
, limit
)
4047 struct directive
*keyword
;
4048 U_CHAR
*buf
, *limit
;
4050 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4051 cpp_error (pfile
, "`#elif' not within a conditional");
4054 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4055 cpp_error (pfile
, "`#elif' after `#else'");
4057 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4059 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4060 && strcmp (pfile
->if_stack
->fname
,
4061 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4062 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4063 fprintf (stderr
, ")\n");
4065 pfile
->if_stack
->type
= T_ELIF
;
4068 if (pfile
->if_stack
->if_succeeded
)
4069 skip_if_group (pfile
, 0);
4071 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4073 skip_if_group (pfile
, 0);
4075 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4076 output_line_command (pfile
, 1, same_file
);
4083 * evaluate a #if expression in BUF, of length LENGTH,
4084 * then parse the result as a C expression and return the value as an int.
4087 static HOST_WIDE_INT
4088 eval_if_expression (pfile
, buf
, length
)
4093 HASHNODE
*save_defined
;
4094 HOST_WIDE_INT value
;
4095 long old_written
= CPP_WRITTEN (pfile
);
4097 save_defined
= install ((U_CHAR
*)"defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4098 pfile
->pcp_inside_if
= 1;
4100 value
= cpp_parse_expr (pfile
);
4101 pfile
->pcp_inside_if
= 0;
4102 delete_macro (save_defined
); /* clean up special symbol */
4104 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4110 * routine to handle ifdef/ifndef. Try to look up the symbol,
4111 * then do or don't skip to the #endif/#else/#elif depending
4112 * on what directive is actually being processed.
4116 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4118 struct directive
*keyword
;
4119 U_CHAR
*unused1
, *unused2
;
4122 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4125 enum cpp_token token
;
4126 int start_of_file
= 0;
4127 U_CHAR
*control_macro
= 0;
4128 int old_written
= CPP_WRITTEN (pfile
);
4130 /* Detect a #ifndef at start of file (not counting comments). */
4131 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4132 start_of_file
= pfile
->only_seen_white
== 2;
4134 pfile
->no_macro_expand
++;
4135 token
= get_directive_token (pfile
);
4136 pfile
->no_macro_expand
--;
4138 ident
= pfile
->token_buffer
+ old_written
;
4139 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4140 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4142 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4144 skip
= (keyword
->type
== T_IFDEF
);
4145 if (! CPP_TRADITIONAL (pfile
))
4146 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4148 else if (token
== CPP_NAME
)
4150 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4151 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4152 if (start_of_file
&& !skip
)
4154 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4155 bcopy (ident
, control_macro
, ident_length
+ 1);
4160 skip
= (keyword
->type
== T_IFDEF
);
4161 if (! CPP_TRADITIONAL (pfile
))
4162 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4165 if (!CPP_TRADITIONAL (pfile
))
4167 cpp_skip_hspace (pfile
);
4169 if (c
!= EOF
&& c
!= '\n')
4170 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4172 skip_rest_of_line (pfile
);
4176 /* Output a precondition for this macro. */
4177 if (hp
&& hp
->value
.defn
->predefined
)
4178 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4181 fprintf (pcp_outfile
, "#undef ");
4182 while (is_idchar
[*cp
]) /* Ick! */
4183 fputc (*cp
++, pcp_outfile
);
4184 putc ('\n', pcp_outfile
);
4188 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4192 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4193 If this is a #ifndef starting at the beginning of a file,
4194 CONTROL_MACRO is the macro name tested by the #ifndef.
4195 Otherwise, CONTROL_MACRO is 0. */
4198 conditional_skip (pfile
, skip
, type
, control_macro
)
4201 enum node_type type
;
4202 U_CHAR
*control_macro
;
4204 IF_STACK_FRAME
*temp
;
4206 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4207 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4209 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4211 temp
->next
= pfile
->if_stack
;
4212 temp
->control_macro
= control_macro
;
4213 pfile
->if_stack
= temp
;
4215 pfile
->if_stack
->type
= type
;
4218 skip_if_group (pfile
, 0);
4221 ++pfile
->if_stack
->if_succeeded
;
4222 output_line_command (pfile
, 1, same_file
);
4227 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4228 * leaves input ptr at the sharp sign found.
4229 * If ANY is nonzero, return at next directive of any sort.
4233 skip_if_group (pfile
, any
)
4238 int at_beg_of_line
= 1;
4239 struct directive
*kt
;
4240 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4242 U_CHAR
*beg_of_line
= bp
;
4244 register int ident_length
;
4245 U_CHAR
*ident
, *after_ident
;
4246 struct parse_marker line_start_mark
;
4248 parse_set_mark (&line_start_mark
, pfile
);
4250 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4251 static char failed
[] = "#failed\n";
4252 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4254 output_line_command (pfile
, 1, same_file
);
4258 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4260 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4261 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4262 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4264 parse_move_mark (&line_start_mark
, pfile
);
4265 if (!CPP_TRADITIONAL (pfile
))
4266 cpp_skip_hspace (pfile
);
4270 int old_written
= CPP_WRITTEN (pfile
);
4271 cpp_skip_hspace (pfile
);
4273 parse_name (pfile
, GETC());
4274 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4275 ident
= pfile
->token_buffer
+ old_written
;
4276 pfile
->limit
= ident
;
4278 if (ident_length
== 0)
4279 goto not_a_directive
;
4281 /* Handle # followed by a line number. */
4283 /* Avoid error for `###' and similar cases unless -pedantic. */
4286 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4288 IF_STACK_FRAME
*temp
;
4289 if (ident_length
== kt
->length
4290 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4292 /* If we are asked to return on next directive, do so now. */
4302 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4303 temp
->next
= pfile
->if_stack
;
4304 pfile
->if_stack
= temp
;
4306 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4308 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4309 temp
->type
= kt
->type
;
4313 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4314 validate_else (pfile
,
4315 kt
->type
== T_ELSE
? "#else" : "#endif");
4317 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4320 "`#%s' not within a conditional", kt
->name
);
4323 else if (pfile
->if_stack
== save_if_stack
)
4324 goto done
; /* found what we came for */
4326 if (kt
->type
!= T_ENDIF
)
4328 if (pfile
->if_stack
->type
== T_ELSE
)
4329 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4330 pfile
->if_stack
->type
= kt
->type
;
4334 temp
= pfile
->if_stack
;
4335 pfile
->if_stack
= temp
->next
;
4342 /* Don't let erroneous code go by. */
4343 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4344 && CPP_PEDANTIC (pfile
))
4345 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4349 /* We're in the middle of a line. Skip the rest of it. */
4356 case '/': /* possible comment */
4357 c
= skip_comment (pfile
, NULL
);
4364 old
= CPP_WRITTEN (pfile
);
4365 cpp_get_token (pfile
);
4366 CPP_SET_WRITTEN (pfile
, old
);
4369 /* Char after backslash loses its special meaning. */
4370 if (PEEKC() == '\n')
4380 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4381 static char end_failed
[] = "#endfailed\n";
4382 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4385 pfile
->only_seen_white
= 1;
4386 parse_goto_mark (&line_start_mark
, pfile
);
4387 parse_clear_mark (&line_start_mark
);
4391 * handle a #else directive. Do this by just continuing processing
4392 * without changing if_stack ; this is so that the error message
4393 * for missing #endif's etc. will point to the original #if. It
4394 * is possible that something different would be better.
4398 do_else (pfile
, keyword
, buf
, limit
)
4400 struct directive
*keyword
;
4401 U_CHAR
*buf
, *limit
;
4403 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4405 if (CPP_PEDANTIC (pfile
))
4406 validate_else (pfile
, "#else");
4407 skip_rest_of_line (pfile
);
4409 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4410 cpp_error (pfile
, "`#else' not within a conditional");
4413 /* #ifndef can't have its special treatment for containing the whole file
4414 if it has a #else clause. */
4415 pfile
->if_stack
->control_macro
= 0;
4417 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4418 cpp_error (pfile
, "`#else' after `#else'");
4419 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4420 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4421 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4422 fprintf (stderr
, ")\n");
4424 pfile
->if_stack
->type
= T_ELSE
;
4427 if (pfile
->if_stack
->if_succeeded
)
4428 skip_if_group (pfile
, 0);
4430 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4431 output_line_command (pfile
, 1, same_file
);
4437 * unstack after #endif command
4441 do_endif (pfile
, keyword
, buf
, limit
)
4443 struct directive
*keyword
;
4444 U_CHAR
*buf
, *limit
;
4446 if (CPP_PEDANTIC (pfile
))
4447 validate_else (pfile
, "#endif");
4448 skip_rest_of_line (pfile
);
4450 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4451 cpp_error (pfile
, "unbalanced `#endif'");
4454 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4455 pfile
->if_stack
= temp
->next
;
4456 if (temp
->control_macro
!= 0)
4458 /* This #endif matched a #ifndef at the start of the file.
4459 See if it is at the end of the file. */
4460 struct parse_marker start_mark
;
4463 parse_set_mark (&start_mark
, pfile
);
4467 cpp_skip_hspace (pfile
);
4472 parse_goto_mark (&start_mark
, pfile
);
4473 parse_clear_mark (&start_mark
);
4477 /* If we get here, this #endif ends a #ifndef
4478 that contains all of the file (aside from whitespace).
4479 Arrange not to include the file again
4480 if the macro that was tested is defined.
4482 Do not do this for the top-level file in a -include or any
4483 file in a -imacros. */
4487 && ! (indepth
== 1 && pfile
->no_record_file
)
4488 && ! (pfile
->no_record_file
&& no_output
))
4491 struct file_name_list
*ifile
= pfile
->all_include_files
;
4493 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4495 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4497 ifile
->control_macro
= temp
->control_macro
;
4505 output_line_command (pfile
, 1, same_file
);
4510 /* When an #else or #endif is found while skipping failed conditional,
4511 if -pedantic was specified, this is called to warn about text after
4512 the command name. P points to the first char after the command name. */
4515 validate_else (pfile
, directive
)
4520 cpp_skip_hspace (pfile
);
4522 if (c
!= EOF
&& c
!= '\n')
4524 "text following `%s' violates ANSI standard", directive
);
4527 /* Get the next token, and add it to the text in pfile->token_buffer.
4528 Return the kind of token we got. */
4531 cpp_get_token (pfile
)
4534 register int c
, c2
, c3
;
4536 long start_line
, start_column
;
4537 enum cpp_token token
;
4538 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4539 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4545 if (CPP_BUFFER (pfile
)->seen_eof
)
4547 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4554 cpp_buffer
*next_buf
4555 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4556 CPP_BUFFER (pfile
)->seen_eof
= 1;
4557 if (CPP_BUFFER (pfile
)->nominal_fname
4558 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4560 /* We're about to return from an #include file.
4561 Emit #line information now (as part of the CPP_POP) result.
4562 But the #line refers to the file we will pop to. */
4563 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4564 CPP_BUFFER (pfile
) = next_buf
;
4565 pfile
->input_stack_listing_current
= 0;
4566 output_line_command (pfile
, 0, leave_file
);
4567 CPP_BUFFER (pfile
) = cur_buffer
;
4577 struct parse_marker start_mark
;
4579 if (PEEKC () == '=')
4581 if (opts
->put_out_comments
)
4582 parse_set_mark (&start_mark
, pfile
);
4584 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4585 &start_line
, &start_column
);
4586 c
= skip_comment (pfile
, &newlines
);
4587 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4588 parse_clear_mark (&start_mark
);
4593 cpp_error_with_line (pfile
, start_line
, start_column
,
4594 "unterminated comment");
4597 c
= '/'; /* Initial letter of comment. */
4599 /* Comments are equivalent to spaces.
4600 For -traditional, a comment is equivalent to nothing. */
4601 if (opts
->put_out_comments
)
4603 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4605 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4606 int len
= pbuf
->cur
- start
;
4607 CPP_RESERVE(pfile
, 1 + len
);
4608 CPP_PUTC_Q (pfile
, c
);
4609 CPP_PUTS_Q (pfile
, start
, len
);
4610 pfile
->lineno
+= newlines
;
4611 parse_clear_mark (&start_mark
);
4614 else if (CPP_TRADITIONAL (pfile
))
4621 /* This may not work if cpp_get_token is called recursively,
4622 since many places look for horizontal space. */
4625 /* Copy the newlines into the output buffer, in order to
4626 avoid the pain of a #line every time a multiline comment
4628 CPP_RESERVE(pfile
, newlines
);
4629 while (--newlines
>= 0)
4631 CPP_PUTC_Q (pfile
, '\n');
4637 CPP_RESERVE(pfile
, 1);
4638 CPP_PUTC_Q (pfile
, ' ');
4642 if (opts
->for_lint
) {
4645 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4647 if (lintcmd
!= NULL
) {
4648 /* I believe it is always safe to emit this newline: */
4650 bcopy ("#pragma lint ", (char *) obp
, 13);
4652 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4657 bcopy (argbp
, (char *) obp
, arglen
);
4661 /* OK, now bring us back to the state we were in before we entered
4662 this branch. We need #line because the newline for the pragma
4663 could mess things up. */
4664 output_line_command (pfile
, 0, same_file
);
4665 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4673 /* If this is expanding a macro definition, don't recognize
4674 preprocessor directives. */
4677 /* If this is expand_into_temp_buffer, recognize them
4678 only after an actual newline at this level,
4679 not at the beginning of the input level. */
4680 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4686 if (!pfile
->only_seen_white
)
4688 if (handle_directive (pfile
))
4689 return CPP_DIRECTIVE
;
4690 pfile
->only_seen_white
= 0;
4695 /* A single quoted string is treated like a double -- some
4696 programs (e.g., troff) are perverse this way */
4697 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4698 &start_line
, &start_column
);
4699 old_written
= CPP_WRITTEN (pfile
);
4701 CPP_PUTC (pfile
, c
);
4707 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4709 /* try harder: this string crosses a macro expansion
4710 boundary. This can happen naturally if -traditional.
4711 Otherwise, only -D can make a macro with an unmatched
4713 cpp_buffer
*next_buf
4714 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4715 (*CPP_BUFFER (pfile
)->cleanup
)
4716 (CPP_BUFFER (pfile
), pfile
);
4717 CPP_BUFFER (pfile
) = next_buf
;
4720 if (!CPP_TRADITIONAL (pfile
))
4722 cpp_error_with_line (pfile
, start_line
, start_column
,
4723 "unterminated string or character constant");
4724 if (pfile
->multiline_string_line
!= start_line
4725 && pfile
->multiline_string_line
!= 0)
4726 cpp_error_with_line (pfile
,
4727 pfile
->multiline_string_line
, -1,
4728 "possible real start of unterminated constant");
4729 pfile
->multiline_string_line
= 0;
4733 CPP_PUTC (pfile
, cc
);
4737 /* Traditionally, end of line ends a string constant with
4738 no error. So exit the loop and record the new line. */
4739 if (CPP_TRADITIONAL (pfile
))
4743 cpp_error_with_line (pfile
, start_line
, start_column
,
4744 "unterminated character constant");
4747 if (CPP_PEDANTIC (pfile
)
4748 && pfile
->multiline_string_line
== 0)
4750 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4751 "string constant runs past end of line");
4753 if (pfile
->multiline_string_line
== 0)
4754 pfile
->multiline_string_line
= start_line
;
4761 /* Backslash newline is replaced by nothing at all. */
4762 CPP_ADJUST_WRITTEN (pfile
, -1);
4767 /* ANSI stupidly requires that in \\ the second \
4768 is *not* prevented from combining with a newline. */
4771 CPP_PUTC (pfile
, cc
);
4783 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4784 CPP_PWRITTEN (pfile
));
4785 pfile
->only_seen_white
= 0;
4786 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4789 if (!opts
->dollars_in_ident
)
4794 if (opts
->cplusplus
&& PEEKC () == ':')
4803 if (c2
== c
|| c2
== '=')
4813 if (PEEKC () == '=')
4820 if (c2
== '-' && opts
->chill
)
4822 /* Chill style comment */
4823 if (opts
->put_out_comments
)
4824 parse_set_mark (&start_mark
, pfile
);
4825 FORWARD(1); /* Skip second '-'. */
4833 /* Don't consider final '\n' to be part of comment. */
4839 goto return_comment
;
4841 if (c2
== '-' || c2
== '=' || c2
== '>')
4846 if (pfile
->parsing_include_directive
)
4850 CPP_PUTC (pfile
, c
);
4855 if (c
== '\n' || c
== EOF
)
4858 "missing '>' in `#include <FILENAME>'");
4864 /* else fall through */
4873 CPP_RESERVE (pfile
, 4);
4874 CPP_PUTC (pfile
, c
);
4875 CPP_PUTC (pfile
, c2
);
4879 CPP_PUTC_Q (pfile
, GETC ());
4880 CPP_NUL_TERMINATE_Q (pfile
);
4881 pfile
->only_seen_white
= 0;
4885 if (CPP_BUFFER (pfile
)->has_escapes
)
4890 if (pfile
->output_escapes
)
4891 CPP_PUTS (pfile
, "@-", 2);
4892 parse_name (pfile
, GETC ());
4895 else if (is_space
[c
])
4897 CPP_RESERVE (pfile
, 2);
4898 if (pfile
->output_escapes
)
4899 CPP_PUTC_Q (pfile
, '@');
4900 CPP_PUTC_Q (pfile
, c
);
4904 if (pfile
->output_escapes
)
4906 CPP_PUTS (pfile
, "@@", 2);
4916 CPP_RESERVE(pfile
, 2);
4917 CPP_PUTC_Q (pfile
, '.');
4921 /* FIXME - misses the case "..\\\n." */
4922 if (c2
== '.' && PEEKN(1) == '.')
4924 CPP_RESERVE(pfile
, 4);
4925 CPP_PUTC_Q (pfile
, '.');
4926 CPP_PUTC_Q (pfile
, '.');
4927 CPP_PUTC_Q (pfile
, '.');
4929 CPP_NUL_TERMINATE_Q (pfile
);
4930 pfile
->only_seen_white
= 0;
4937 pfile
->only_seen_white
= 0;
4939 CPP_RESERVE(pfile
, 3);
4940 CPP_PUTC_Q (pfile
, c
);
4941 CPP_PUTC_Q (pfile
, GETC ());
4942 CPP_NUL_TERMINATE_Q (pfile
);
4948 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
4950 CPP_PUTC (pfile
, c
);
4956 case '0': case '1': case '2': case '3': case '4':
4957 case '5': case '6': case '7': case '8': case '9':
4962 CPP_RESERVE (pfile
, 2);
4963 CPP_PUTC_Q (pfile
, c
);
4968 if (!is_idchar
[c
] && c
!= '.'
4969 && ((c2
!= 'e' && c2
!= 'E'
4970 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
4971 || (c
!= '+' && c
!= '-')))
4976 CPP_NUL_TERMINATE_Q (pfile
);
4977 pfile
->only_seen_white
= 0;
4979 case 'b': case 'c': case 'd': case 'h': case 'o':
4980 case 'B': case 'C': case 'D': case 'H': case 'O':
4981 if (opts
->chill
&& PEEKC () == '\'')
4983 pfile
->only_seen_white
= 0;
4984 CPP_RESERVE (pfile
, 2);
4985 CPP_PUTC_Q (pfile
, c
);
4986 CPP_PUTC_Q (pfile
, '\'');
4992 goto chill_number_eof
;
4995 if (c
== '\\' && PEEKC() == '\n')
5002 CPP_PUTC (pfile
, c
);
5006 CPP_RESERVE (pfile
, 2);
5007 CPP_PUTC_Q (pfile
, c
);
5008 CPP_NUL_TERMINATE_Q (pfile
);
5015 CPP_NUL_TERMINATE (pfile
);
5022 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5023 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5024 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5025 case 'x': case 'y': case 'z':
5026 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5027 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5028 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5033 unsigned char *ident
;
5034 int before_name_written
= CPP_WRITTEN (pfile
);
5036 parse_name (pfile
, c
);
5037 pfile
->only_seen_white
= 0;
5038 if (pfile
->no_macro_expand
)
5040 ident
= pfile
->token_buffer
+ before_name_written
;
5041 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5042 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5045 if (hp
->type
== T_DISABLED
)
5047 if (pfile
->output_escapes
)
5048 { /* Return "@-IDENT", followed by '\0'. */
5050 CPP_RESERVE (pfile
, 3);
5051 ident
= pfile
->token_buffer
+ before_name_written
;
5052 CPP_ADJUST_WRITTEN (pfile
, 2);
5053 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5060 /* If macro wants an arglist, verify that a '(' follows.
5061 first skip all whitespace, copying it to the output
5062 after the macro name. Then, if there is no '(',
5063 decide this is not a macro call and leave things that way. */
5064 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5066 struct parse_marker macro_mark
;
5068 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5070 cpp_buffer
*next_buf
;
5071 cpp_skip_hspace (pfile
);
5072 if (PEEKC () != EOF
)
5074 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5075 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5076 CPP_BUFFER (pfile
) = next_buf
;
5078 parse_set_mark (¯o_mark
, pfile
);
5081 cpp_skip_hspace (pfile
);
5083 is_macro_call
= c
== '(';
5089 parse_goto_mark (¯o_mark
, pfile
);
5090 parse_clear_mark (¯o_mark
);
5094 /* This is now known to be a macro call. */
5096 /* it might not actually be a macro. */
5097 if (hp
->type
!= T_MACRO
) {
5098 int xbuf_len
; U_CHAR
*xbuf
;
5099 CPP_SET_WRITTEN (pfile
, before_name_written
);
5100 special_symbol (hp
, pfile
);
5101 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5102 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5103 CPP_SET_WRITTEN (pfile
, before_name_written
);
5104 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5105 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5109 /* Expand the macro, reading arguments as needed,
5110 and push the expansion on the input stack. */
5111 macroexpand (pfile
, hp
);
5112 CPP_SET_WRITTEN (pfile
, before_name_written
);
5115 /* An extra "@ " is added to the end of a macro expansion
5116 to prevent accidental token pasting. We prefer to avoid
5117 unneeded extra spaces (for the sake of cpp-using tools like
5118 imake). Here we remove the space if it is safe to do so. */
5119 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5120 && pfile
->buffer
->rlimit
[-2] == '@'
5121 && pfile
->buffer
->rlimit
[-1] == ' ')
5123 int c1
= pfile
->buffer
->rlimit
[-3];
5124 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5125 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5126 pfile
->buffer
->rlimit
-= 2;
5131 case ' ': case '\t': case '\v': case '\r':
5134 CPP_PUTC (pfile
, c
);
5136 if (c
== EOF
|| !is_hor_space
[c
])
5150 CPP_PUTC (pfile
, c
);
5151 if (pfile
->only_seen_white
== 0)
5152 pfile
->only_seen_white
= 1;
5154 output_line_command (pfile
, 1, same_file
);
5157 case '(': token
= CPP_LPAREN
; goto char1
;
5158 case ')': token
= CPP_RPAREN
; goto char1
;
5159 case '{': token
= CPP_LBRACE
; goto char1
;
5160 case '}': token
= CPP_RBRACE
; goto char1
;
5161 case ',': token
= CPP_COMMA
; goto char1
;
5162 case ';': token
= CPP_SEMICOLON
; goto char1
;
5168 pfile
->only_seen_white
= 0;
5169 CPP_PUTC (pfile
, c
);
5175 /* Like cpp_get_token, but skip spaces and comments. */
5178 cpp_get_non_space_token (pfile
)
5181 int old_written
= CPP_WRITTEN (pfile
);
5184 enum cpp_token token
= cpp_get_token (pfile
);
5185 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5186 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5188 CPP_SET_WRITTEN (pfile
, old_written
);
5192 /* Parse an identifier starting with C. */
5195 parse_name (pfile
, c
)
5196 cpp_reader
*pfile
; int c
;
5202 if (c
== '\\' && PEEKC() == '\n')
5211 if (c
== '$' && CPP_PEDANTIC (pfile
))
5212 cpp_pedwarn ("`$' in identifier");
5214 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5215 CPP_PUTC_Q (pfile
, c
);
5220 CPP_NUL_TERMINATE_Q (pfile
);
5225 /* Maintain and search list of included files, for #import. */
5227 /* Hash a file name for import_hash_table. */
5235 while (*f
) val
+= *f
++;
5236 return (val
%IMPORT_HASH_SIZE
);
5239 /* Search for file FILENAME in import_hash_table.
5240 Return -2 if found, either a matching name or a matching inode.
5241 Otherwise, open the file and return a file descriptor if successful
5242 or -1 if unsuccessful. */
5245 lookup_import (pfile
, filename
, searchptr
)
5248 struct file_name_list
*searchptr
;
5250 struct import_file
*i
;
5256 hashval
= import_hash (filename
);
5258 /* Attempt to find file in list of already included files */
5259 i
= pfile
->import_hash_table
[hashval
];
5262 if (!strcmp (filename
, i
->name
))
5263 return -2; /* return found */
5266 /* Open it and try a match on inode/dev */
5267 fd
= open_include_file (pfile
, filename
, searchptr
);
5271 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5272 i
= pfile
->import_hash_table
[h
];
5274 /* Compare the inode and the device.
5275 Supposedly on some systems the inode is not a scalar. */
5276 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5277 && i
->dev
== sb
.st_dev
) {
5279 return -2; /* return found */
5284 return fd
; /* Not found, return open file */
5287 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5290 add_import (pfile
, fd
, fname
)
5295 struct import_file
*i
;
5299 hashval
= import_hash (fname
);
5301 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5302 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5303 strcpy (i
->name
, fname
);
5304 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5306 i
->next
= pfile
->import_hash_table
[hashval
];
5307 pfile
->import_hash_table
[hashval
] = i
;
5310 /* The file_name_map structure holds a mapping of file names for a
5311 particular directory. This mapping is read from the file named
5312 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5313 map filenames on a file system with severe filename restrictions,
5314 such as DOS. The format of the file name map file is just a series
5315 of lines with two tokens on each line. The first token is the name
5316 to map, and the second token is the actual name to use. */
5318 struct file_name_map
5320 struct file_name_map
*map_next
;
5325 #define FILE_NAME_MAP_FILE "header.gcc"
5327 /* Read a space delimited string of unlimited length from a stdio
5331 read_filename_string (ch
, f
)
5339 set
= alloc
= xmalloc (len
+ 1);
5343 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5345 if (set
- alloc
== len
)
5348 alloc
= xrealloc (alloc
, len
+ 1);
5349 set
= alloc
+ len
/ 2;
5359 /* This structure holds a linked list of file name maps, one per directory. */
5361 struct file_name_map_list
5363 struct file_name_map_list
*map_list_next
;
5364 char *map_list_name
;
5365 struct file_name_map
*map_list_map
;
5368 /* Read the file name map file for DIRNAME. */
5370 static struct file_name_map
*
5371 read_name_map (pfile
, dirname
)
5375 register struct file_name_map_list
*map_list_ptr
;
5379 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5380 map_list_ptr
= map_list_ptr
->map_list_next
)
5381 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5382 return map_list_ptr
->map_list_map
;
5384 map_list_ptr
= ((struct file_name_map_list
*)
5385 xmalloc (sizeof (struct file_name_map_list
)));
5386 map_list_ptr
->map_list_name
= savestring (dirname
);
5387 map_list_ptr
->map_list_map
= NULL
;
5389 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5390 strcpy (name
, dirname
);
5393 strcat (name
, FILE_NAME_MAP_FILE
);
5394 f
= fopen (name
, "r");
5396 map_list_ptr
->map_list_map
= NULL
;
5400 int dirlen
= strlen (dirname
);
5402 while ((ch
= getc (f
)) != EOF
)
5405 struct file_name_map
*ptr
;
5409 from
= read_filename_string (ch
, f
);
5410 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5412 to
= read_filename_string (ch
, f
);
5414 ptr
= ((struct file_name_map
*)
5415 xmalloc (sizeof (struct file_name_map
)));
5416 ptr
->map_from
= from
;
5418 /* Make the real filename absolute. */
5423 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5424 strcpy (ptr
->map_to
, dirname
);
5425 ptr
->map_to
[dirlen
] = '/';
5426 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5430 ptr
->map_next
= map_list_ptr
->map_list_map
;
5431 map_list_ptr
->map_list_map
= ptr
;
5433 while ((ch
= getc (f
)) != '\n')
5440 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5441 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5443 return map_list_ptr
->map_list_map
;
5446 /* Try to open include file FILENAME. SEARCHPTR is the directory
5447 being tried from the include file search path. This function maps
5448 filenames on file systems based on information read by
5452 open_include_file (pfile
, filename
, searchptr
)
5455 struct file_name_list
*searchptr
;
5457 if (CPP_OPTIONS (pfile
)->remap
)
5459 register struct file_name_map
*map
;
5460 register char *from
;
5463 if (searchptr
&& ! searchptr
->got_name_map
)
5465 searchptr
->name_map
= read_name_map (pfile
,
5467 ? searchptr
->fname
: ".");
5468 searchptr
->got_name_map
= 1;
5471 /* First check the mapping for the directory we are using. */
5472 if (searchptr
&& searchptr
->name_map
)
5475 if (searchptr
->fname
)
5476 from
+= strlen (searchptr
->fname
) + 1;
5477 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5479 if (! strcmp (map
->map_from
, from
))
5481 /* Found a match. */
5482 return open (map
->map_to
, O_RDONLY
, 0666);
5487 /* Try to find a mapping file for the particular directory we are
5488 looking in. Thus #include <sys/types.h> will look up sys/types.h
5489 in /usr/include/header.gcc and look up types.h in
5490 /usr/include/sys/header.gcc. */
5491 p
= rindex (filename
, '/');
5496 && strlen (searchptr
->fname
) == p
- filename
5497 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5499 /* FILENAME is in SEARCHPTR, which we've already checked. */
5500 return open (filename
, O_RDONLY
, 0666);
5510 dir
= (char *) alloca (p
- filename
+ 1);
5511 bcopy (filename
, dir
, p
- filename
);
5512 dir
[p
- filename
] = '\0';
5515 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5516 if (! strcmp (map
->map_from
, from
))
5517 return open (map
->map_to
, O_RDONLY
, 0666);
5520 return open (filename
, O_RDONLY
, 0666);
5523 /* Process the contents of include file FNAME, already open on descriptor F,
5525 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5526 "system" include directories (as decided by the `is_system_include'
5528 DIRPTR is the link in the dir path through which this file was found,
5529 or 0 if the file name was absolute or via the current directory.
5530 Return 1 on success, 0 on failure.
5532 The caller is responsible for the cpp_push_buffer. */
5535 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5539 int system_header_p
;
5540 struct file_name_list
*dirptr
;
5546 cpp_buffer
*fp
; /* For input stack frame */
5547 int missing_newline
= 0;
5549 if (fstat (f
, &st
) < 0)
5551 cpp_perror_with_name (pfile
, fname
);
5553 cpp_pop_buffer (pfile
);
5557 fp
= CPP_BUFFER (pfile
);
5558 fp
->nominal_fname
= fp
->fname
= fname
;
5563 fp
->system_header_p
= system_header_p
;
5566 fp
->cleanup
= file_cleanup
;
5568 if (S_ISREG (st
.st_mode
)) {
5569 st_size
= (size_t) st
.st_size
;
5570 if (st_size
!= st
.st_size
|| st_size
+ 2 < st_size
) {
5571 cpp_error (pfile
, "file `%s' too large", fname
);
5575 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5576 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5579 /* Read the file contents, knowing that st_size is an upper bound
5580 on the number of bytes we can read. */
5581 length
= safe_read (f
, fp
->buf
, st_size
);
5582 fp
->rlimit
= fp
->buf
+ length
;
5583 if (length
< 0) goto nope
;
5585 else if (S_ISDIR (st
.st_mode
)) {
5586 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5590 /* Cannot count its file size before reading.
5591 First read the entire file into heap and
5592 copy them into buffer on stack. */
5597 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5600 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5602 goto nope
; /* error! */
5604 if (st_size
!= bsize
)
5605 break; /* End of file */
5607 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5613 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5614 /* Backslash-newline at end is not good enough. */
5615 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5616 fp
->buf
[length
++] = '\n';
5618 missing_newline
= 1;
5621 fp
->buf
[length
] = '\0';
5622 fp
->rlimit
= fp
->buf
+ length
;
5624 /* Close descriptor now, so nesting does not use lots of descriptors. */
5627 /* Must do this before calling trigraph_pcp, so that the correct file name
5628 will be printed in warning messages. */
5630 pfile
->input_stack_listing_current
= 0;
5640 if (missing_newline
)
5643 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5644 pedwarn ("file does not end in newline");
5647 input_file_stack_tick
++;
5654 cpp_perror_with_name (pfile
, fname
);
5660 /* This is called after options have been processed.
5661 * Check options for consistency, and setup for processing input
5662 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5663 * Return 1 on success, 0 on failure.
5667 cpp_start_read (pfile
, fname
)
5671 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5672 struct cpp_pending
*pend
;
5677 /* The code looks at the defaults through this pointer, rather than through
5678 the constant structure above. This pointer gets changed if an environment
5679 variable specifies other defaults. */
5680 struct default_include
*include_defaults
= include_defaults_array
;
5682 /* Add dirs from CPATH after dirs from -I. */
5683 /* There seems to be confusion about what CPATH should do,
5684 so for the moment it is not documented. */
5685 /* Some people say that CPATH should replace the standard include dirs,
5686 but that seems pointless: it comes before them, so it overrides them
5688 p
= (char *) getenv ("CPATH");
5689 if (p
!= 0 && ! opts
->no_standard_includes
)
5690 path_include (pfile
, p
);
5692 /* Now that dollars_in_ident is known, initialize is_idchar. */
5693 initialize_char_syntax (opts
);
5695 /* Do partial setup of input buffer for the sake of generating
5696 early #line directives (when -g is in effect). */
5697 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5700 if (opts
->in_fname
== NULL
)
5701 opts
->in_fname
= "";
5702 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5705 /* Install __LINE__, etc. Must follow initialize_char_syntax
5706 and option processing. */
5707 initialize_builtins (pfile
);
5709 /* Do standard #defines and assertions
5710 that identify system and machine type. */
5712 if (!opts
->inhibit_predefs
) {
5713 char *p
= (char *) alloca (strlen (predefs
) + 1);
5714 strcpy (p
, predefs
);
5717 while (*p
== ' ' || *p
== '\t')
5719 /* Handle -D options. */
5720 if (p
[0] == '-' && p
[1] == 'D') {
5722 while (*p
&& *p
!= ' ' && *p
!= '\t')
5726 if (opts
->debug_output
)
5727 output_line_command (pfile
, 0, same_file
);
5728 cpp_define (pfile
, q
);
5729 while (*p
== ' ' || *p
== '\t')
5731 } else if (p
[0] == '-' && p
[1] == 'A') {
5732 /* Handle -A options (assertions). */
5741 past_name
= assertion
;
5742 /* Locate end of name. */
5743 while (*past_name
&& *past_name
!= ' '
5744 && *past_name
!= '\t' && *past_name
!= '(')
5746 /* Locate `(' at start of value. */
5748 while (*value
&& (*value
== ' ' || *value
== '\t'))
5750 if (*value
++ != '(')
5752 while (*value
&& (*value
== ' ' || *value
== '\t'))
5755 /* Locate end of value. */
5756 while (*past_value
&& *past_value
!= ' '
5757 && *past_value
!= '\t' && *past_value
!= ')')
5759 termination
= past_value
;
5760 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5762 if (*termination
++ != ')')
5764 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5766 /* Temporarily null-terminate the value. */
5767 save_char
= *termination
;
5768 *termination
= '\0';
5769 /* Install the assertion. */
5770 make_assertion (pfile
, "-A", assertion
);
5771 *termination
= (char) save_char
;
5773 while (*p
== ' ' || *p
== '\t')
5781 /* Now handle the command line options. */
5783 /* Do -U's, -D's and -A's in the order they were seen. */
5784 /* First reverse the list. */
5785 opts
->pending
= nreverse_pending (opts
->pending
);
5787 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5789 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5791 switch (pend
->cmd
[1])
5794 if (opts
->debug_output
)
5795 output_line_command (pfile
, 0, same_file
);
5796 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5799 if (opts
->debug_output
)
5800 output_line_command (pfile
, 0, same_file
);
5801 cpp_define (pfile
, pend
->arg
);
5804 make_assertion (pfile
, "-A", pend
->arg
);
5810 opts
->done_initializing
= 1;
5812 { /* Read the appropriate environment variable and if it exists
5813 replace include_defaults with the listed path. */
5815 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5818 epath
= getenv ("C_INCLUDE_PATH");
5821 epath
= getenv ("CPLUS_INCLUDE_PATH");
5824 epath
= getenv ("OBJC_INCLUDE_PATH");
5827 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
5830 /* If the environment var for this language is set,
5831 add to the default list of include directories. */
5833 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5835 char *startp
, *endp
;
5837 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5838 if (*startp
== PATH_SEPARATOR
)
5841 = (struct default_include
*) xmalloc ((num_dirs
5842 * sizeof (struct default_include
))
5843 + sizeof (include_defaults_array
));
5844 startp
= endp
= epath
;
5847 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5848 if ((*endp
== PATH_SEPARATOR
)
5850 strncpy (nstore
, startp
, endp
-startp
);
5852 strcpy (nstore
, ".");
5854 nstore
[endp
-startp
] = '\0';
5856 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5857 include_defaults
[num_dirs
].component
= 0;
5858 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5859 include_defaults
[num_dirs
].cxx_aware
= 1;
5863 endp
= startp
= endp
+ 1;
5867 /* Put the usual defaults back in at the end. */
5868 bcopy ((char *) include_defaults_array
,
5869 (char *) &include_defaults
[num_dirs
],
5870 sizeof (include_defaults_array
));
5874 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5875 opts
->first_system_include
= opts
->before_system
;
5877 /* Unless -fnostdinc,
5878 tack on the standard include file dirs to the specified list */
5879 if (!opts
->no_standard_includes
) {
5880 struct default_include
*p
= include_defaults
;
5881 char *specd_prefix
= opts
->include_prefix
;
5882 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5883 int default_len
= 0;
5884 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5885 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5886 default_len
= strlen (default_prefix
) - 7;
5887 default_prefix
[default_len
] = 0;
5889 /* Search "translated" versions of GNU directories.
5890 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5891 if (specd_prefix
!= 0 && default_len
!= 0)
5892 for (p
= include_defaults
; p
->fname
; p
++) {
5893 /* Some standard dirs are only for C++. */
5895 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5896 /* Does this dir start with the prefix? */
5897 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5898 /* Yes; change prefix and add to search list. */
5899 struct file_name_list
*new
5900 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5901 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5902 char *str
= (char *) xmalloc (this_len
+ 1);
5903 strcpy (str
, specd_prefix
);
5904 strcat (str
, p
->fname
+ default_len
);
5906 new->control_macro
= 0;
5907 new->c_system_include_path
= !p
->cxx_aware
;
5908 new->got_name_map
= 0;
5909 append_include_chain (pfile
, new, new);
5910 if (opts
->first_system_include
== 0)
5911 opts
->first_system_include
= new;
5915 /* Search ordinary names for GNU include directories. */
5916 for (p
= include_defaults
; p
->fname
; p
++) {
5917 /* Some standard dirs are only for C++. */
5919 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5920 struct file_name_list
*new
5921 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5922 new->control_macro
= 0;
5923 new->c_system_include_path
= !p
->cxx_aware
;
5924 new->fname
= update_path (p
->fname
, p
->component
);
5925 new->got_name_map
= 0;
5926 append_include_chain (pfile
, new, new);
5927 if (opts
->first_system_include
== 0)
5928 opts
->first_system_include
= new;
5933 /* Tack the after_include chain at the end of the include chain. */
5934 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5935 if (opts
->first_system_include
== 0)
5936 opts
->first_system_include
= opts
->after_include
;
5938 /* With -v, print the list of dirs to search. */
5939 if (opts
->verbose
) {
5940 struct file_name_list
*p
;
5941 cpp_notice ("#include \"...\" search starts here:\n");
5942 for (p
= opts
->include
; p
; p
= p
->next
) {
5943 if (p
== opts
->first_bracket_include
)
5944 cpp_notice ("#include <...> search starts here:\n");
5945 fprintf (stderr
, " %s\n", p
->fname
);
5947 cpp_notice ("End of search list.\n");
5950 /* Scan the -imacros files before the main input.
5951 Much like #including them, but with no_output set
5952 so that only their macro definitions matter. */
5954 opts
->no_output
++; pfile
->no_record_file
++;
5955 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5957 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
5959 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5962 cpp_perror_with_name (pfile
, pend
->arg
);
5965 if (!cpp_push_buffer (pfile
, NULL
, 0))
5967 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
5968 cpp_scan_buffer (pfile
);
5971 opts
->no_output
--; pfile
->no_record_file
--;
5973 /* Copy the entire contents of the main input file into
5974 the stacked input buffer previously allocated for it. */
5975 if (fname
== NULL
|| *fname
== 0) {
5978 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
5979 cpp_pfatal_with_name (pfile
, fname
);
5981 /* -MG doesn't select the form of output and must be specified with one of
5982 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5983 inhibit compilation. */
5984 if (opts
->print_deps_missing_files
5985 && (opts
->print_deps
== 0 || !opts
->no_output
))
5987 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
5991 /* Either of two environment variables can specify output of deps.
5992 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
5993 where OUTPUT_FILE is the file to write deps info to
5994 and DEPS_TARGET is the target to mention in the deps. */
5996 if (opts
->print_deps
== 0
5997 && (getenv ("SUNPRO_DEPENDENCIES") != 0
5998 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
5999 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6005 spec
= getenv ("SUNPRO_DEPENDENCIES");
6006 opts
->print_deps
= 2;
6009 opts
->print_deps
= 1;
6012 /* Find the space before the DEPS_TARGET, if there is one. */
6013 /* This should use index. (mrs) */
6014 while (*s
!= 0 && *s
!= ' ') s
++;
6017 opts
->deps_target
= s
+ 1;
6018 output_file
= (char *) xmalloc (s
- spec
+ 1);
6019 bcopy (spec
, output_file
, s
- spec
);
6020 output_file
[s
- spec
] = 0;
6024 opts
->deps_target
= 0;
6028 opts
->deps_file
= output_file
;
6029 opts
->print_deps_append
= 1;
6032 /* For -M, print the expected object file name
6033 as the target of this Make-rule. */
6034 if (opts
->print_deps
)
6036 pfile
->deps_allocated_size
= 200;
6037 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6038 pfile
->deps_buffer
[0] = 0;
6039 pfile
->deps_size
= 0;
6040 pfile
->deps_column
= 0;
6042 if (opts
->deps_target
)
6043 deps_output (pfile
, opts
->deps_target
, ':');
6044 else if (*opts
->in_fname
== 0)
6045 deps_output (pfile
, "-", ':');
6050 static char *known_suffixes
[] = { ".c", ".C", ".s", ".S", ".m",
6051 ".cc", ".cxx", ".cpp", ".cp",
6055 /* Discard all directory prefixes from filename. */
6056 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6057 #ifdef DIR_SEPARATOR
6058 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6065 /* Copy remainder to mungable area. */
6066 p
= (char *) alloca (strlen(q
) + 8);
6069 /* Output P, but remove known suffixes. */
6072 /* Point to the filename suffix. */
6073 r
= rindex (p
, '.');
6074 /* Compare against the known suffixes. */
6076 while (known_suffixes
[x
] != 0)
6078 if (strncmp (known_suffixes
[x
], r
, q
- r
) == 0)
6080 /* Make q point to the bit we're going to overwrite
6081 with an object suffix. */
6088 /* Supply our own suffix. */
6095 deps_output (pfile
, p
, ':');
6096 deps_output (pfile
, opts
->in_fname
, ' ');
6101 /* Make sure data ends with a newline. And put a null after it. */
6103 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6104 /* Backslash-newline at end is not good enough. */
6105 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6106 fp
->buf
[fp
->length
++] = '\n';
6107 missing_newline
= 1;
6109 fp
->buf
[fp
->length
] = '\0';
6111 /* Unless inhibited, convert trigraphs in the input. */
6117 /* Scan the -include files before the main input.
6118 We push these in reverse order, so that the first one is handled first. */
6120 pfile
->no_record_file
++;
6121 opts
->pending
= nreverse_pending (opts
->pending
);
6122 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6124 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6126 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6129 cpp_perror_with_name (pfile
, pend
->arg
);
6132 if (!cpp_push_buffer (pfile
, NULL
, 0))
6134 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6137 pfile
->no_record_file
--;
6139 /* Free the pending list. */
6140 for (pend
= opts
->pending
; pend
; )
6142 struct cpp_pending
*next
= pend
->next
;
6146 opts
->pending
= NULL
;
6149 /* Scan the input, processing macros and directives. */
6151 rescan (&outbuf
, 0);
6153 if (missing_newline
)
6156 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6157 pedwarn ("file does not end in newline");
6160 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6161 output_line_command (pfile
, 0, same_file
);
6166 cpp_reader_init (pfile
)
6169 bzero ((char *) pfile
, sizeof (cpp_reader
));
6170 pfile
->get_token
= cpp_get_token
;
6172 pfile
->token_buffer_size
= 200;
6173 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
6174 CPP_SET_WRITTEN (pfile
, 0);
6176 pfile
->system_include_depth
= 0;
6177 pfile
->dont_repeat_files
= 0;
6178 pfile
->all_include_files
= 0;
6179 pfile
->max_include_len
= 0;
6180 pfile
->timebuf
= NULL
;
6181 pfile
->only_seen_white
= 1;
6182 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6185 static struct cpp_pending
*
6186 nreverse_pending (list
)
6187 struct cpp_pending
*list
;
6190 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6191 for (pend
= list
; pend
; pend
= next
)
6201 push_pending (pfile
, cmd
, arg
)
6206 struct cpp_pending
*pend
6207 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
6210 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6211 CPP_OPTIONS (pfile
)->pending
= pend
;
6214 /* Handle command-line options in (argc, argv).
6215 Can be called multiple times, to handle multiple sets of options.
6216 Returns if an unrecognized option is seen.
6217 Returns number of handled arguments. */
6220 cpp_handle_options (pfile
, argc
, argv
)
6226 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6227 for (i
= 0; i
< argc
; i
++) {
6228 if (argv
[i
][0] != '-') {
6229 if (opts
->out_fname
!= NULL
)
6231 cpp_fatal (pfile
, "Usage: %s [switches] input output", argv
[0]);
6234 else if (opts
->in_fname
!= NULL
)
6235 opts
->out_fname
= argv
[i
];
6237 opts
->in_fname
= argv
[i
];
6239 switch (argv
[i
][1]) {
6242 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
6245 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
6249 if (!strcmp (argv
[i
], "-include")
6250 || !strcmp (argv
[i
], "-imacros")) {
6252 goto missing_filename
;
6254 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6256 if (!strcmp (argv
[i
], "-iprefix")) {
6258 goto missing_filename
;
6260 opts
->include_prefix
= argv
[++i
];
6262 if (!strcmp (argv
[i
], "-ifoutput")) {
6263 opts
->output_conditionals
= 1;
6265 if (!strcmp (argv
[i
], "-isystem")) {
6266 struct file_name_list
*dirtmp
;
6269 goto missing_filename
;
6271 dirtmp
= (struct file_name_list
*)
6272 xmalloc (sizeof (struct file_name_list
));
6274 dirtmp
->control_macro
= 0;
6275 dirtmp
->c_system_include_path
= 1;
6276 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6277 strcpy (dirtmp
->fname
, argv
[++i
]);
6278 dirtmp
->got_name_map
= 0;
6280 if (opts
->before_system
== 0)
6281 opts
->before_system
= dirtmp
;
6283 opts
->last_before_system
->next
= dirtmp
;
6284 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6286 /* Add directory to end of path for includes,
6287 with the default prefix at the front of its name. */
6288 if (!strcmp (argv
[i
], "-iwithprefix")) {
6289 struct file_name_list
*dirtmp
;
6292 if (opts
->include_prefix
!= 0)
6293 prefix
= opts
->include_prefix
;
6295 prefix
= savestring (GCC_INCLUDE_DIR
);
6296 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6297 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6298 prefix
[strlen (prefix
) - 7] = 0;
6301 dirtmp
= (struct file_name_list
*)
6302 xmalloc (sizeof (struct file_name_list
));
6303 dirtmp
->next
= 0; /* New one goes on the end */
6304 dirtmp
->control_macro
= 0;
6305 dirtmp
->c_system_include_path
= 0;
6307 goto missing_dirname
;
6309 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6310 + strlen (prefix
) + 1);
6311 strcpy (dirtmp
->fname
, prefix
);
6312 strcat (dirtmp
->fname
, argv
[++i
]);
6313 dirtmp
->got_name_map
= 0;
6315 if (opts
->after_include
== 0)
6316 opts
->after_include
= dirtmp
;
6318 opts
->last_after_include
->next
= dirtmp
;
6319 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6321 /* Add directory to main path for includes,
6322 with the default prefix at the front of its name. */
6323 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6324 struct file_name_list
*dirtmp
;
6327 if (opts
->include_prefix
!= 0)
6328 prefix
= opts
->include_prefix
;
6330 prefix
= savestring (GCC_INCLUDE_DIR
);
6331 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6332 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6333 prefix
[strlen (prefix
) - 7] = 0;
6336 dirtmp
= (struct file_name_list
*)
6337 xmalloc (sizeof (struct file_name_list
));
6338 dirtmp
->next
= 0; /* New one goes on the end */
6339 dirtmp
->control_macro
= 0;
6340 dirtmp
->c_system_include_path
= 0;
6342 goto missing_dirname
;
6344 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6345 + strlen (prefix
) + 1);
6346 strcpy (dirtmp
->fname
, prefix
);
6347 strcat (dirtmp
->fname
, argv
[++i
]);
6348 dirtmp
->got_name_map
= 0;
6350 append_include_chain (pfile
, dirtmp
, dirtmp
);
6352 /* Add directory to end of path for includes. */
6353 if (!strcmp (argv
[i
], "-idirafter")) {
6354 struct file_name_list
*dirtmp
;
6356 dirtmp
= (struct file_name_list
*)
6357 xmalloc (sizeof (struct file_name_list
));
6358 dirtmp
->next
= 0; /* New one goes on the end */
6359 dirtmp
->control_macro
= 0;
6360 dirtmp
->c_system_include_path
= 0;
6362 goto missing_dirname
;
6364 dirtmp
->fname
= argv
[++i
];
6365 dirtmp
->got_name_map
= 0;
6367 if (opts
->after_include
== 0)
6368 opts
->after_include
= dirtmp
;
6370 opts
->last_after_include
->next
= dirtmp
;
6371 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6376 if (opts
->out_fname
!= NULL
)
6378 cpp_fatal (pfile
, "Output filename specified twice");
6382 goto missing_filename
;
6383 opts
->out_fname
= argv
[++i
];
6384 if (!strcmp (opts
->out_fname
, "-"))
6385 opts
->out_fname
= "";
6389 if (!strcmp (argv
[i
], "-pedantic"))
6390 CPP_PEDANTIC (pfile
) = 1;
6391 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6392 CPP_PEDANTIC (pfile
) = 1;
6393 opts
->pedantic_errors
= 1;
6396 else if (!strcmp (argv
[i
], "-pcp")) {
6397 char *pcp_fname
= argv
[++i
];
6398 pcp_outfile
= ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6399 ? fopen (pcp_fname
, "w")
6400 : fdopen (dup (fileno (stdout
)), "w"));
6401 if (pcp_outfile
== 0)
6402 cpp_pfatal_with_name (pfile
, pcp_fname
);
6409 if (!strcmp (argv
[i
], "-traditional")) {
6410 opts
->traditional
= 1;
6411 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6413 opts
->no_trigraphs
= 0;
6418 if (! strcmp (argv
[i
], "-lang-c"))
6419 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6421 if (! strcmp (argv
[i
], "-lang-c89"))
6422 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->c89
= 1,
6424 if (! strcmp (argv
[i
], "-lang-c++"))
6425 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6427 if (! strcmp (argv
[i
], "-lang-objc"))
6428 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6430 if (! strcmp (argv
[i
], "-lang-objc++"))
6431 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6433 if (! strcmp (argv
[i
], "-lang-asm"))
6435 if (! strcmp (argv
[i
], "-lint"))
6437 if (! strcmp (argv
[i
], "-lang-chill"))
6438 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6439 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6443 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6447 opts
->inhibit_warnings
= 1;
6451 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6452 opts
->warn_trigraphs
= 1;
6453 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6454 opts
->warn_trigraphs
= 0;
6455 else if (!strcmp (argv
[i
], "-Wcomment"))
6456 opts
->warn_comments
= 1;
6457 else if (!strcmp (argv
[i
], "-Wno-comment"))
6458 opts
->warn_comments
= 0;
6459 else if (!strcmp (argv
[i
], "-Wcomments"))
6460 opts
->warn_comments
= 1;
6461 else if (!strcmp (argv
[i
], "-Wno-comments"))
6462 opts
->warn_comments
= 0;
6463 else if (!strcmp (argv
[i
], "-Wtraditional"))
6464 opts
->warn_stringify
= 1;
6465 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6466 opts
->warn_stringify
= 0;
6467 else if (!strcmp (argv
[i
], "-Wundef"))
6468 opts
->warn_undef
= 1;
6469 else if (!strcmp (argv
[i
], "-Wno-undef"))
6470 opts
->warn_undef
= 0;
6471 else if (!strcmp (argv
[i
], "-Wimport"))
6472 opts
->warn_import
= 1;
6473 else if (!strcmp (argv
[i
], "-Wno-import"))
6474 opts
->warn_import
= 0;
6475 else if (!strcmp (argv
[i
], "-Werror"))
6476 opts
->warnings_are_errors
= 1;
6477 else if (!strcmp (argv
[i
], "-Wno-error"))
6478 opts
->warnings_are_errors
= 0;
6479 else if (!strcmp (argv
[i
], "-Wall"))
6481 opts
->warn_trigraphs
= 1;
6482 opts
->warn_comments
= 1;
6487 /* The style of the choices here is a bit mixed.
6488 The chosen scheme is a hybrid of keeping all options in one string
6489 and specifying each option in a separate argument:
6490 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6491 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6492 -M[M][G][D file]. This is awkward to handle in specs, and is not
6494 /* ??? -MG must be specified in addition to one of -M or -MM.
6495 This can be relaxed in the future without breaking anything.
6496 The converse isn't true. */
6498 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6499 if (!strcmp (argv
[i
], "-MG"))
6501 opts
->print_deps_missing_files
= 1;
6504 if (!strcmp (argv
[i
], "-M"))
6505 opts
->print_deps
= 2;
6506 else if (!strcmp (argv
[i
], "-MM"))
6507 opts
->print_deps
= 1;
6508 else if (!strcmp (argv
[i
], "-MD"))
6509 opts
->print_deps
= 2;
6510 else if (!strcmp (argv
[i
], "-MMD"))
6511 opts
->print_deps
= 1;
6512 /* For -MD and -MMD options, write deps on file named by next arg. */
6513 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6516 goto missing_filename
;
6517 opts
->deps_file
= argv
[++i
];
6521 /* For -M and -MM, write deps on standard output
6522 and suppress the usual output. */
6523 opts
->no_output
= 1;
6529 char *p
= argv
[i
] + 2;
6531 while ((c
= *p
++) != 0) {
6532 /* Arg to -d specifies what parts of macros to dump */
6535 opts
->dump_macros
= dump_only
;
6536 opts
->no_output
= 1;
6539 opts
->dump_macros
= dump_names
;
6542 opts
->dump_macros
= dump_definitions
;
6545 opts
->dump_includes
= 1;
6553 if (argv
[i
][2] == '3')
6554 opts
->debug_output
= 1;
6558 cpp_notice ("GNU CPP version %s", version_string
);
6559 #ifdef TARGET_VERSION
6562 fprintf (stderr
, "\n");
6567 opts
->print_include_names
= 1;
6571 if (argv
[i
][2] != 0)
6572 push_pending (pfile
, "-D", argv
[i
] + 2);
6573 else if (i
+ 1 == argc
)
6575 cpp_fatal (pfile
, "Macro name missing after -D option");
6579 i
++, push_pending (pfile
, "-D", argv
[i
]);
6586 if (argv
[i
][2] != 0)
6588 else if (i
+ 1 == argc
)
6590 cpp_fatal (pfile
, "Assertion missing after -A option");
6596 if (!strcmp (p
, "-")) {
6597 struct cpp_pending
**ptr
;
6598 /* -A- eliminates all predefined macros and assertions.
6599 Let's include also any that were specified earlier
6600 on the command line. That way we can get rid of any
6601 that were passed automatically in from GCC. */
6603 opts
->inhibit_predefs
= 1;
6604 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6606 struct cpp_pending
*pend
= *ptr
;
6607 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6608 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6617 push_pending (pfile
, "-A", p
);
6622 case 'U': /* JF #undef something */
6623 if (argv
[i
][2] != 0)
6624 push_pending (pfile
, "-U", argv
[i
] + 2);
6625 else if (i
+ 1 == argc
)
6627 cpp_fatal (pfile
, "Macro name missing after -U option");
6631 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6635 opts
->put_out_comments
= 1;
6638 case 'E': /* -E comes from cc -E; ignore it. */
6642 opts
->no_line_commands
= 1;
6645 case '$': /* Don't include $ in identifiers. */
6646 opts
->dollars_in_ident
= 0;
6649 case 'I': /* Add directory to path for includes. */
6651 struct file_name_list
*dirtmp
;
6653 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6654 && !strcmp (argv
[i
] + 2, "-")) {
6655 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6656 /* Don't use any preceding -I directories for #include <...>. */
6657 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6660 dirtmp
= (struct file_name_list
*)
6661 xmalloc (sizeof (struct file_name_list
));
6662 dirtmp
->next
= 0; /* New one goes on the end */
6663 dirtmp
->control_macro
= 0;
6664 dirtmp
->c_system_include_path
= 0;
6665 if (argv
[i
][2] != 0)
6666 dirtmp
->fname
= argv
[i
] + 2;
6667 else if (i
+ 1 == argc
)
6668 goto missing_dirname
;
6670 dirtmp
->fname
= argv
[++i
];
6671 dirtmp
->got_name_map
= 0;
6672 append_include_chain (pfile
, dirtmp
, dirtmp
);
6678 if (!strcmp (argv
[i
], "-nostdinc"))
6679 /* -nostdinc causes no default include directories.
6680 You must specify all include-file directories with -I. */
6681 opts
->no_standard_includes
= 1;
6682 else if (!strcmp (argv
[i
], "-nostdinc++"))
6683 /* -nostdinc++ causes no default C++-specific include directories. */
6684 opts
->no_standard_cplusplus_includes
= 1;
6686 else if (!strcmp (argv
[i
], "-noprecomp"))
6692 if (!strcmp (argv
[i
], "-remap"))
6697 /* Sun compiler passes undocumented switch "-undef".
6698 Let's assume it means to inhibit the predefined symbols. */
6699 opts
->inhibit_predefs
= 1;
6702 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6703 if (opts
->in_fname
== NULL
) {
6704 opts
->in_fname
= "";
6706 } else if (opts
->out_fname
== NULL
) {
6707 opts
->out_fname
= "";
6709 } /* else fall through into error */
6723 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6725 if (opts
->print_deps
)
6727 /* Stream on which to print the dependency information. */
6730 /* Don't actually write the deps file if compilation has failed. */
6731 if (pfile
->errors
== 0)
6733 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6734 if (opts
->deps_file
== 0)
6735 deps_stream
= stdout
;
6736 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6737 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6738 fputs (pfile
->deps_buffer
, deps_stream
);
6739 putc ('\n', deps_stream
);
6740 if (opts
->deps_file
)
6742 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6743 cpp_fatal (pfile
, "I/O error on output");
6749 /* Free resources used by PFILE.
6750 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6757 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6758 cpp_pop_buffer (pfile
);
6760 if (pfile
->token_buffer
)
6762 free (pfile
->token_buffer
);
6763 pfile
->token_buffer
= NULL
;
6766 if (pfile
->deps_buffer
)
6768 free (pfile
->deps_buffer
);
6769 pfile
->deps_buffer
= NULL
;
6770 pfile
->deps_allocated_size
= 0;
6773 while (pfile
->if_stack
)
6775 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6776 pfile
->if_stack
= temp
->next
;
6780 while (pfile
->dont_repeat_files
)
6782 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6783 pfile
->dont_repeat_files
= temp
->next
;
6788 while (pfile
->all_include_files
)
6790 struct file_name_list
*temp
= pfile
->all_include_files
;
6791 pfile
->all_include_files
= temp
->next
;
6796 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6798 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6801 struct import_file
*next
= imp
->next
;
6806 pfile
->import_hash_table
[i
] = 0;
6809 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6811 while (pfile
->assertion_hashtab
[i
])
6812 delete_assertion (pfile
->assertion_hashtab
[i
]);
6815 cpp_hash_cleanup (pfile
);
6819 do_assert (pfile
, keyword
, buf
, limit
)
6821 struct directive
*keyword
;
6822 U_CHAR
*buf
, *limit
;
6824 long symstart
; /* remember where symbol name starts */
6826 int sym_length
; /* and how long it is */
6827 struct arglist
*tokens
= NULL
;
6829 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6830 && !CPP_BUFFER (pfile
)->system_header_p
)
6831 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6833 cpp_skip_hspace (pfile
);
6834 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6835 parse_name (pfile
, GETC());
6836 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
, 1);
6838 cpp_skip_hspace (pfile
);
6839 if (PEEKC() != '(') {
6840 cpp_error (pfile
, "missing token-sequence in `#assert'");
6846 tokens
= read_token_list (pfile
, &error_flag
);
6850 cpp_error (pfile
, "empty token-sequence in `#assert'");
6853 cpp_skip_hspace (pfile
);
6855 if (c
!= EOF
&& c
!= '\n')
6856 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6857 skip_rest_of_line (pfile
);
6860 /* If this name isn't already an assertion name, make it one.
6861 Error if it was already in use in some other way. */
6864 ASSERTION_HASHNODE
*hp
;
6865 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6866 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6867 struct tokenlist_list
*value
6868 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6870 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6872 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6873 cpp_error (pfile
, "`defined' redefined as assertion");
6874 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6877 /* Add the spec'd token-sequence to the list of such. */
6878 value
->tokens
= tokens
;
6879 value
->next
= hp
->value
;
6882 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6885 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6886 skip_rest_of_line (pfile
);
6891 do_unassert (pfile
, keyword
, buf
, limit
)
6893 struct directive
*keyword
;
6894 U_CHAR
*buf
, *limit
;
6896 long symstart
; /* remember where symbol name starts */
6897 int sym_length
; /* and how long it is */
6900 struct arglist
*tokens
= NULL
;
6901 int tokens_specified
= 0;
6903 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6904 && !CPP_BUFFER (pfile
)->system_header_p
)
6905 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6907 cpp_skip_hspace (pfile
);
6909 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6910 parse_name (pfile
, GETC());
6911 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
, 1);
6913 cpp_skip_hspace (pfile
);
6914 if (PEEKC() == '(') {
6917 tokens
= read_token_list (pfile
, &error_flag
);
6921 cpp_error (pfile
, "empty token list in `#unassert'");
6925 tokens_specified
= 1;
6928 cpp_skip_hspace (pfile
);
6930 if (c
!= EOF
&& c
!= '\n')
6931 cpp_error (pfile
, "junk at end of `#unassert'");
6932 skip_rest_of_line (pfile
);
6935 ASSERTION_HASHNODE
*hp
;
6936 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6937 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6938 struct tokenlist_list
*tail
, *prev
;
6940 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6944 /* If no token list was specified, then eliminate this assertion
6946 if (! tokens_specified
)
6947 delete_assertion (hp
);
6949 /* If a list of tokens was given, then delete any matching list. */
6954 struct tokenlist_list
*next
= tail
->next
;
6955 if (compare_token_lists (tail
->tokens
, tokens
)) {
6959 hp
->value
= tail
->next
;
6960 free_token_list (tail
->tokens
);
6970 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6973 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6974 skip_rest_of_line (pfile
);
6978 /* Test whether there is an assertion named NAME
6979 and optionally whether it has an asserted token list TOKENS.
6980 NAME is not null terminated; its length is SYM_LENGTH.
6981 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6984 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
6988 int tokens_specified
;
6989 struct arglist
*tokens
;
6991 ASSERTION_HASHNODE
*hp
;
6992 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6994 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
6995 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
6997 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
6999 /* It is not an assertion; just return false. */
7002 /* If no token list was specified, then value is 1. */
7003 if (! tokens_specified
)
7007 struct tokenlist_list
*tail
;
7011 /* If a list of tokens was given,
7012 then succeed if the assertion records a matching list. */
7015 if (compare_token_lists (tail
->tokens
, tokens
))
7020 /* Fail if the assertion has no matching list. */
7025 /* Compare two lists of tokens for equality including order of tokens. */
7028 compare_token_lists (l1
, l2
)
7029 struct arglist
*l1
, *l2
;
7032 if (l1
->length
!= l2
->length
)
7034 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7040 /* Succeed if both lists end at the same time. */
7045 reverse_token_list (tokens
)
7046 struct arglist
*tokens
;
7048 register struct arglist
*prev
= 0, *this, *next
;
7049 for (this = tokens
; this; this = next
)
7058 /* Read a space-separated list of tokens ending in a close parenthesis.
7059 Return a list of strings, in the order they were written.
7060 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7062 static struct arglist
*
7063 read_token_list (pfile
, error_flag
)
7067 struct arglist
*token_ptrs
= 0;
7072 FORWARD (1); /* Skip '(' */
7074 /* Loop over the assertion value tokens. */
7077 struct arglist
*temp
;
7078 long name_written
= CPP_WRITTEN (pfile
);
7079 int eofp
= 0; int c
;
7081 cpp_skip_hspace (pfile
);
7085 /* Find the end of the token. */
7088 CPP_PUTC (pfile
, c
);
7096 CPP_PUTC (pfile
, c
);
7098 else if (c
== '"' || c
== '\'')
7101 cpp_get_token (pfile
);
7107 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7108 && c
!= '"' && c
!= '\'')
7110 CPP_PUTC (pfile
, c
);
7113 if (c
!= EOF
) FORWARD(-1);
7116 length
= CPP_WRITTEN (pfile
) - name_written
;
7117 temp
= (struct arglist
*)
7118 xmalloc (sizeof (struct arglist
) + length
+ 1);
7119 temp
->name
= (U_CHAR
*) (temp
+ 1);
7120 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7121 (char *) temp
->name
, length
);
7122 temp
->name
[length
] = 0;
7123 temp
->next
= token_ptrs
;
7125 temp
->length
= length
;
7127 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7129 if (c
== EOF
|| c
== '\n')
7132 "unterminated token sequence following `#' operator");
7137 /* We accumulated the names in reverse order.
7138 Now reverse them to get the proper order. */
7139 return reverse_token_list (token_ptrs
);
7143 free_token_list (tokens
)
7144 struct arglist
*tokens
;
7147 struct arglist
*next
= tokens
->next
;
7148 free (tokens
->name
);
7154 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7155 retrying if necessary. If MAX_READ_LEN is defined, read at most
7156 that bytes at a time. Return a negative value if an error occurs,
7157 otherwise return the actual number of bytes read,
7158 which must be LEN unless end-of-file was reached. */
7161 safe_read (desc
, ptr
, len
)
7166 int left
, rcount
, nchars
;
7172 if (rcount
> MAX_READ_LEN
)
7173 rcount
= MAX_READ_LEN
;
7175 nchars
= read (desc
, ptr
, rcount
);
7193 xcalloc (number
, size
)
7194 unsigned number
, size
;
7196 register unsigned total
= number
* size
;
7197 register char *ptr
= (char *) xmalloc (total
);
7206 unsigned size
= strlen (input
);
7207 char *output
= xmalloc (size
+ 1);
7208 strcpy (output
, input
);
7212 /* Initialize PMARK to remember the current position of PFILE. */
7215 parse_set_mark (pmark
, pfile
)
7216 struct parse_marker
*pmark
;
7219 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7220 pmark
->next
= pbuf
->marks
;
7221 pbuf
->marks
= pmark
;
7223 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7226 /* Cleanup PMARK - we no longer need it. */
7229 parse_clear_mark (pmark
)
7230 struct parse_marker
*pmark
;
7232 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7233 for (; ; pp
= &(*pp
)->next
) {
7234 if (*pp
== NULL
) abort ();
7235 if (*pp
== pmark
) break;
7240 /* Backup the current position of PFILE to that saved in PMARK. */
7243 parse_goto_mark (pmark
, pfile
)
7244 struct parse_marker
*pmark
;
7247 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7248 if (pbuf
!= pmark
->buf
)
7249 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
7250 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7253 /* Reset PMARK to point to the current position of PFILE. (Same
7254 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7257 parse_move_mark (pmark
, pfile
)
7258 struct parse_marker
*pmark
;
7261 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7262 if (pbuf
!= pmark
->buf
)
7263 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
7264 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7268 cpp_read_check_assertion (pfile
)
7271 int name_start
= CPP_WRITTEN (pfile
);
7272 int name_length
, name_written
;
7274 FORWARD (1); /* Skip '#' */
7275 cpp_skip_hspace (pfile
);
7276 parse_name (pfile
, GETC ());
7277 name_written
= CPP_WRITTEN (pfile
);
7278 name_length
= name_written
- name_start
;
7279 cpp_skip_hspace (pfile
);
7280 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7283 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7284 result
= check_assertion (pfile
,
7285 pfile
->token_buffer
+ name_start
, name_length
,
7289 result
= check_assertion (pfile
,
7290 pfile
->token_buffer
+ name_start
, name_length
,
7292 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7297 cpp_print_file_and_line (pfile
)
7300 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7305 cpp_buf_line_and_col (ip
, &line
, &col
);
7306 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
7307 line
, pfile
->show_column
? col
: -1);
7312 cpp_error (pfile
, msgid
, arg1
, arg2
, arg3
)
7315 char *arg1
, *arg2
, *arg3
;
7317 cpp_print_containing_files (pfile
);
7318 cpp_print_file_and_line (pfile
);
7319 cpp_message (pfile
, 1, _(msgid
), arg1
, arg2
, arg3
);
7322 /* Print error message but don't count it. */
7325 cpp_warning (pfile
, msgid
, arg1
, arg2
, arg3
)
7328 char *arg1
, *arg2
, *arg3
;
7330 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7333 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7336 cpp_print_containing_files (pfile
);
7337 cpp_print_file_and_line (pfile
);
7338 cpp_message (pfile
, 0, _(msgid
), arg1
, arg2
, arg3
);
7341 /* Print an error message. */
7344 cpp_notice (msgid
, arg1
, arg2
, arg3
)
7346 char *arg1
, *arg2
, *arg3
;
7348 cpp_message ((cpp_reader
*) 0, -1, _(msgid
), arg1
, arg2
, arg3
);
7351 /* Print an error message and maybe count it. */
7354 cpp_pedwarn (pfile
, msgid
, arg1
, arg2
, arg3
)
7357 char *arg1
, *arg2
, *arg3
;
7359 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7360 cpp_error (pfile
, msgid
, arg1
, arg2
, arg3
);
7362 cpp_warning (pfile
, msgid
, arg1
, arg2
, arg3
);
7366 cpp_error_with_line (pfile
, line
, column
, msgid
, arg1
, arg2
, arg3
)
7370 char *arg1
, *arg2
, *arg3
;
7373 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7375 cpp_print_containing_files (pfile
);
7378 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7380 cpp_message (pfile
, 1, _(msgid
), arg1
, arg2
, arg3
);
7384 cpp_warning_with_line (pfile
, line
, column
, msgid
, arg1
, arg2
, arg3
)
7388 char *arg1
, *arg2
, *arg3
;
7393 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7396 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7399 cpp_print_containing_files (pfile
);
7401 ip
= cpp_file_buffer (pfile
);
7404 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
7406 cpp_message (pfile
, 0, _(msgid
), arg1
, arg2
, arg3
);
7410 cpp_pedwarn_with_line (pfile
, line
, column
, msgid
, arg1
, arg2
, arg3
)
7414 char *arg1
, *arg2
, *arg3
;
7416 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7417 cpp_error_with_line (pfile
, column
, line
, msgid
, arg1
, arg2
, arg3
);
7419 cpp_warning_with_line (pfile
, line
, column
, msgid
, arg1
, arg2
, arg3
);
7422 /* Report a warning (or an error if pedantic_errors)
7423 giving specified file name and line number, not current. */
7426 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msgid
, arg1
, arg2
, arg3
)
7431 char *arg1
, *arg2
, *arg3
;
7433 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7434 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7437 cpp_file_line_for_message (pfile
, file
, line
, -1);
7438 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7439 _(msgid
), arg1
, arg2
, arg3
);
7442 /* This defines "errno" properly for VMS, and gives us EACCES. */
7449 #ifndef HAVE_STRERROR
7450 extern int sys_nerr
;
7451 extern char *sys_errlist
[];
7452 #else /* HAVE_STRERROR */
7456 char *strerror (int,...);
7459 /* my_strerror - return the descriptive text associated with an
7463 my_strerror (errnum
)
7469 #ifndef HAVE_STRERROR
7470 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7472 result
= strerror (errnum
);
7475 /* VAXCRTL's strerror() takes an optional second argument, which only
7476 matters when the first argument is EVMSERR. However, it's simplest
7477 just to pass it unconditionally. `vaxc$errno' is declared in
7478 <errno.h>, and maintained by the library in parallel with `errno'.
7479 We assume that caller's `errnum' either matches the last setting of
7480 `errno' by the library or else does not have the value `EVMSERR'. */
7482 result
= strerror (errnum
, vaxc$errno
);
7486 result
= "errno = ?";
7491 /* Error including a message from `errno'. */
7494 cpp_error_from_errno (pfile
, name
)
7500 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7502 cpp_print_containing_files (pfile
);
7505 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
7507 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (e
));
7511 cpp_perror_with_name (pfile
, name
)
7515 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7519 * No pre-compiled header file support.
7521 * Possibly different enum token codes for each C/C++ token.
7523 * Should clean up remaining directives to that do_XXX functions
7524 * only take two arguments and all have command_reads_line.
7526 * Find and cleanup remaining uses of static variables,
7528 * Support for trigraphs.
7530 * Support -dM flag (dump_all_macros).
7532 * Support for_lint flag.