2 Copyright (C) 1986, 87, 89, 92-7, 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_TIMES_H
31 #include <sys/times.h>
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
40 #include "gansidecl.h"
42 #ifndef GET_ENVIRONMENT
43 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ENV_VALUE = getenv (ENV_NAME)
46 extern char *update_path ();
54 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
55 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
57 /* Find the largest host integer type and set its size and type.
58 Watch out: on some crazy hosts `long' is shorter than `int'. */
62 # include <inttypes.h>
63 # define HOST_WIDE_INT intmax_t
65 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
66 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
67 # define HOST_WIDE_INT int
69 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
70 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
71 # define HOST_WIDE_INT long
73 # define HOST_WIDE_INT long long
80 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
84 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
87 /* By default, colon separates directories in a path. */
88 #ifndef PATH_SEPARATOR
89 #define PATH_SEPARATOR ':'
92 #ifndef STANDARD_INCLUDE_DIR
93 #define STANDARD_INCLUDE_DIR "/usr/include"
95 #ifndef INCLUDE_LEN_FUDGE
96 #define INCLUDE_LEN_FUDGE 0
99 /* Symbols to predefine. */
101 #ifdef CPP_PREDEFINES
102 static char *predefs
= CPP_PREDEFINES
;
104 static char *predefs
= "";
107 /* We let tm.h override the types used here, to handle trivial differences
108 such as the choice of unsigned int or long unsigned int for size_t.
109 When machines start needing nontrivial differences in the size type,
110 it would be best to do something here to figure out automatically
111 from other information what type to use. */
113 /* The string value for __SIZE_TYPE__. */
116 #define SIZE_TYPE "long unsigned int"
119 /* The string value for __PTRDIFF_TYPE__. */
122 #define PTRDIFF_TYPE "long int"
125 /* The string value for __WCHAR_TYPE__. */
128 #define WCHAR_TYPE "int"
130 #define CPP_WCHAR_TYPE(PFILE) \
131 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
133 /* The string value for __USER_LABEL_PREFIX__ */
135 #ifndef USER_LABEL_PREFIX
136 #define USER_LABEL_PREFIX ""
139 /* The string value for __REGISTER_PREFIX__ */
141 #ifndef REGISTER_PREFIX
142 #define REGISTER_PREFIX ""
145 /* In the definition of a #assert name, this structure forms
146 a list of the individual values asserted.
147 Each value is itself a list of "tokens".
148 These are strings that are compared by name. */
150 struct tokenlist_list
{
151 struct tokenlist_list
*next
;
152 struct arglist
*tokens
;
155 struct assertion_hashnode
{
156 struct assertion_hashnode
*next
; /* double links for easy deletion */
157 struct assertion_hashnode
*prev
;
158 /* also, a back pointer to this node's hash
159 chain is kept, in case the node is the head
160 of the chain and gets deleted. */
161 struct assertion_hashnode
**bucket_hdr
;
162 int length
; /* length of token, for quick comparison */
163 U_CHAR
*name
; /* the actual name */
164 /* List of token-sequences. */
165 struct tokenlist_list
*value
;
168 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
169 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
171 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
172 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
173 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
174 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
175 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
176 (Note that it is false while we're expanding marco *arguments*.) */
177 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
179 /* Move all backslash-newline pairs out of embarrassing places.
180 Exchange all such pairs following BP
181 with any potentially-embarrassing characters that follow them.
182 Potentially-embarrassing characters are / and *
183 (because a backslash-newline inside a comment delimiter
184 would cause it not to be recognized). */
186 #define NEWLINE_FIX \
187 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
189 /* Same, but assume we've already read the potential '\\' into C. */
190 #define NEWLINE_FIX1(C) do { \
191 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
195 struct cpp_pending
*next
;
200 /* Forward declarations. */
204 void cpp_file_line_for_message
PARAMS ((char *, int, int));
205 void cpp_hash_cleanup
PARAMS ((cpp_reader
*));
207 void cpp_print_containing_files
PARAMS ((cpp_reader
*));
209 static void add_import ();
210 static void append_include_chain ();
211 static void make_assertion ();
212 static void path_include ();
213 static void initialize_builtins ();
214 static void initialize_char_syntax ();
215 extern void delete_macro ();
217 static void trigraph_pcp ();
219 static int finclude ();
220 static void validate_else ();
221 static int comp_def_part ();
223 extern void fancy_abort ();
225 static int lookup_import ();
226 static int redundant_include_p ();
227 static int is_system_include ();
228 static struct file_name_map
*read_name_map ();
229 static char *read_filename_string ();
230 static int open_include_file ();
231 static int check_macro_name ();
232 static int compare_defs ();
233 static int compare_token_lists ();
234 static HOST_WIDE_INT
eval_if_expression ();
235 static int change_newlines ();
237 static struct arglist
*read_token_list ();
238 static void free_token_list ();
239 static int safe_read ();
240 static void push_macro_expansion
PARAMS ((cpp_reader
*,
241 U_CHAR
*, int, HASHNODE
*));
242 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
243 extern char *xrealloc ();
244 static char *xcalloc ();
245 static char *savestring ();
247 static void conditional_skip ();
248 static void skip_if_group ();
250 /* Last arg to output_line_command. */
251 enum file_change_code
{same_file
, enter_file
, leave_file
};
253 /* External declarations. */
255 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
257 extern FILE *fdopen ();
258 extern char *version_string
;
259 extern struct tm
*localtime ();
261 /* These functions are declared to return int instead of void since they
262 are going to be placed in a table and some old compilers have trouble with
263 pointers to functions returning void. */
265 static int do_define ();
266 static int do_line ();
267 static int do_include ();
268 static int do_undef ();
269 static int do_error ();
270 static int do_pragma ();
271 static int do_ident ();
273 static int do_xifdef ();
274 static int do_else ();
275 static int do_elif ();
276 static int do_endif ();
277 #ifdef SCCS_DIRECTIVE
278 static int do_sccs ();
280 static int do_once ();
281 static int do_assert ();
282 static int do_unassert ();
283 static int do_warning ();
285 struct file_name_list
287 struct file_name_list
*next
;
289 /* If the following is nonzero, it is a macro name.
290 Don't include the file again if that macro is defined. */
291 U_CHAR
*control_macro
;
292 /* If the following is nonzero, it is a C-language system include
294 int c_system_include_path
;
295 /* Mapping of file names for this directory. */
296 struct file_name_map
*name_map
;
297 /* Non-zero if name_map is valid. */
301 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
302 via the same directory as the file that #included it. */
303 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
305 /* #include "file" looks in source file dir, then stack. */
306 /* #include <file> just looks in the stack. */
307 /* -I directories are added to the end, then the defaults are added. */
309 static struct default_include
{
310 char *fname
; /* The name of the directory. */
311 char *component
; /* The component containing the directory */
312 int cplusplus
; /* Only look here if we're compiling C++. */
313 int cxx_aware
; /* Includes in this directory don't need to
314 be wrapped in extern "C" when compiling
316 } include_defaults_array
[]
317 #ifdef INCLUDE_DEFAULTS
321 /* Pick up GNU C++ specific include files. */
322 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
323 { OLD_GPLUSPLUS_INCLUDE_DIR
, 0, 1, 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 /* For cross-compilation, this dir name is generated
329 automatically in Makefile.in. */
330 { CROSS_INCLUDE_DIR
, "GCC",0, 0 },
331 #ifdef TOOL_INCLUDE_DIR
332 /* This is another place that the target system's headers might be. */
333 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
335 #else /* not CROSS_COMPILE */
336 #ifdef LOCAL_INCLUDE_DIR
337 /* This should be /usr/local/include and should come before
338 the fixincludes-fixed header files. */
339 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
341 #ifdef TOOL_INCLUDE_DIR
342 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
343 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
344 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
346 /* This is the dir for fixincludes. Put it just before
347 the files that we fix. */
348 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
349 /* Some systems have an extra dir of include files. */
350 #ifdef SYSTEM_INCLUDE_DIR
351 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
353 #ifndef STANDARD_INCLUDE_COMPONENT
354 #define STANDARD_INCLUDE_COMPONENT 0
356 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
357 #endif /* not CROSS_COMPILE */
360 #endif /* no INCLUDE_DEFAULTS */
362 /* `struct directive' defines one #-directive, including how to handle it. */
365 int length
; /* Length of name */
366 int (*func
)(); /* Function to handle directive */
367 char *name
; /* Name of directive */
368 enum node_type type
; /* Code which describes which directive. */
369 char command_reads_line
; /* One if rest of line is read by func. */
372 #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT)
374 /* Here is the actual list of #-directives, most-often-used first.
375 The initialize_builtins function assumes #define is the very first. */
377 static struct directive directive_table
[] = {
378 { 6, do_define
, "define", T_DEFINE
},
379 { 5, do_xifdef
, "ifdef", T_IFDEF
, 1},
380 { 6, do_xifdef
, "ifndef", T_IFNDEF
, 1},
381 { 7, do_include
, "include", T_INCLUDE
, 1},
382 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
383 { 6, do_include
, "import", T_IMPORT
, 1},
384 { 5, do_endif
, "endif", T_ENDIF
, 1},
385 { 4, do_else
, "else", T_ELSE
, 1},
386 { 2, do_if
, "if", T_IF
, 1},
387 { 4, do_elif
, "elif", T_ELIF
, 1},
388 { 5, do_undef
, "undef", T_UNDEF
},
389 { 5, do_error
, "error", T_ERROR
},
390 { 7, do_warning
, "warning", T_WARNING
},
391 { 6, do_pragma
, "pragma", T_PRAGMA
},
392 { 4, do_line
, "line", T_LINE
, 1},
393 { 5, do_ident
, "ident", T_IDENT
, 1},
394 #ifdef SCCS_DIRECTIVE
395 { 4, do_sccs
, "sccs", T_SCCS
},
397 { 6, do_assert
, "assert", T_ASSERT
, 1},
398 { 8, do_unassert
, "unassert", T_UNASSERT
, 1},
399 { -1, 0, "", T_UNUSED
},
402 /* table to tell if char can be part of a C identifier. */
403 U_CHAR is_idchar
[256];
404 /* table to tell if char can be first char of a c identifier. */
405 U_CHAR is_idstart
[256];
406 /* table to tell if c is horizontal space. */
407 U_CHAR is_hor_space
[256];
408 /* table to tell if c is horizontal or vertical space. */
409 static U_CHAR is_space
[256];
411 /* Initialize syntactic classifications of characters. */
414 initialize_char_syntax (opts
)
415 struct cpp_options
*opts
;
420 * Set up is_idchar and is_idstart tables. These should be
421 * faster than saying (is_alpha (c) || c == '_'), etc.
422 * Set up these things before calling any routines tthat
425 for (i
= 'a'; i
<= 'z'; i
++) {
426 is_idchar
[i
- 'a' + 'A'] = 1;
428 is_idstart
[i
- 'a' + 'A'] = 1;
431 for (i
= '0'; i
<= '9'; i
++)
435 is_idchar
['$'] = opts
->dollars_in_ident
;
436 is_idstart
['$'] = opts
->dollars_in_ident
;
438 /* horizontal space table */
439 is_hor_space
[' '] = 1;
440 is_hor_space
['\t'] = 1;
441 is_hor_space
['\v'] = 1;
442 is_hor_space
['\f'] = 1;
443 is_hor_space
['\r'] = 1;
454 /* Place into PFILE a quoted string representing the string SRC.
455 Caller must reserve enough space in pfile->token_buffer. */
458 quote_string (pfile
, src
)
464 CPP_PUTC_Q (pfile
, '\"');
466 switch ((c
= *src
++))
470 CPP_PUTC_Q (pfile
, c
);
473 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
474 CPP_ADJUST_WRITTEN (pfile
, 4);
480 CPP_PUTC_Q (pfile
, '\\');
481 CPP_PUTC_Q (pfile
, c
);
485 CPP_PUTC_Q (pfile
, '\"');
486 CPP_NUL_TERMINATE_Q (pfile
);
491 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
494 cpp_grow_buffer (pfile
, n
)
498 long old_written
= CPP_WRITTEN (pfile
);
499 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
500 pfile
->token_buffer
= (U_CHAR
*)
501 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
502 CPP_SET_WRITTEN (pfile
, old_written
);
507 * process a given definition string, for initialization
508 * If STR is just an identifier, define it with value 1.
509 * If STR has anything after the identifier, then it should
510 * be identifier=definition.
514 cpp_define (pfile
, str
)
524 cpp_error (pfile
, "malformed option `-D %s'", str
);
527 while (is_idchar
[*++p
])
531 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
532 strcpy ((char *)buf
, str
);
533 strcat ((char *)buf
, " 1");
537 cpp_error (pfile
, "malformed option `-D %s'", str
);
543 /* Copy the entire option so we can modify it. */
544 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
545 strncpy (buf
, str
, p
- str
);
546 /* Change the = to a space. */
548 /* Scan for any backslash-newline and remove it. */
553 if (*p
== '\\' && p
[1] == '\n')
561 do_define (pfile
, NULL
, buf
, buf
+ strlen (buf
));
564 /* Process the string STR as if it appeared as the body of a #assert.
565 OPTION is the option name for which STR was the argument. */
568 make_assertion (pfile
, option
, str
)
575 /* Copy the entire option so we can modify it. */
576 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
577 strcpy ((char *) buf
, str
);
578 /* Scan for any backslash-newline and remove it. */
582 if (*p
== '\\' && p
[1] == '\n')
591 if (!is_idstart
[*p
]) {
592 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
595 while (is_idchar
[*++p
])
597 while (*p
== ' ' || *p
== '\t') p
++;
598 if (! (*p
== 0 || *p
== '(')) {
599 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
603 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
605 do_assert (pfile
, NULL
, NULL
, NULL
);
606 cpp_pop_buffer (pfile
);
610 /* Append a chain of `struct file_name_list's
611 to the end of the main include chain.
612 FIRST is the beginning of the chain to append, and LAST is the end. */
615 append_include_chain (pfile
, first
, last
)
617 struct file_name_list
*first
, *last
;
619 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
620 struct file_name_list
*dir
;
625 if (opts
->include
== 0)
626 opts
->include
= first
;
628 opts
->last_include
->next
= first
;
630 if (opts
->first_bracket_include
== 0)
631 opts
->first_bracket_include
= first
;
633 for (dir
= first
; ; dir
= dir
->next
) {
634 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
635 if (len
> pfile
->max_include_len
)
636 pfile
->max_include_len
= len
;
642 opts
->last_include
= last
;
645 /* Add output to `deps_buffer' for the -M switch.
646 STRING points to the text to be output.
647 SPACER is ':' for targets, ' ' for dependencies, zero for text
648 to be inserted literally. */
651 deps_output (pfile
, string
, spacer
)
656 int size
= strlen (string
);
661 #ifndef MAX_OUTPUT_COLUMNS
662 #define MAX_OUTPUT_COLUMNS 72
665 && pfile
->deps_column
> 0
666 && (pfile
->deps_column
+ size
) > MAX_OUTPUT_COLUMNS
)
668 deps_output (pfile
, " \\\n ", 0);
669 pfile
->deps_column
= 0;
672 if (pfile
->deps_size
+ size
+ 8 > pfile
->deps_allocated_size
)
674 pfile
->deps_allocated_size
= (pfile
->deps_size
+ size
+ 50) * 2;
675 pfile
->deps_buffer
= (char *) xrealloc (pfile
->deps_buffer
,
676 pfile
->deps_allocated_size
);
678 if (spacer
== ' ' && pfile
->deps_column
> 0)
679 pfile
->deps_buffer
[pfile
->deps_size
++] = ' ';
680 bcopy (string
, &pfile
->deps_buffer
[pfile
->deps_size
], size
);
681 pfile
->deps_size
+= size
;
682 pfile
->deps_column
+= size
;
684 pfile
->deps_buffer
[pfile
->deps_size
++] = ':';
685 pfile
->deps_buffer
[pfile
->deps_size
] = 0;
688 /* Given a colon-separated list of file names PATH,
689 add all the names to the search path for include files. */
692 path_include (pfile
, path
)
704 struct file_name_list
*dirtmp
;
706 /* Find the end of this name. */
707 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
709 /* An empty name in the path stands for the current directory. */
710 name
= (char *) xmalloc (2);
714 /* Otherwise use the directory that is named. */
715 name
= (char *) xmalloc (q
- p
+ 1);
716 bcopy (p
, name
, q
- p
);
720 dirtmp
= (struct file_name_list
*)
721 xmalloc (sizeof (struct file_name_list
));
722 dirtmp
->next
= 0; /* New one goes on the end */
723 dirtmp
->control_macro
= 0;
724 dirtmp
->c_system_include_path
= 0;
725 dirtmp
->fname
= name
;
726 dirtmp
->got_name_map
= 0;
727 append_include_chain (pfile
, dirtmp
, dirtmp
);
729 /* Advance past this name. */
733 /* Skip the colon. */
739 cpp_options_init (opts
)
742 bzero ((char *) opts
, sizeof *opts
);
743 opts
->in_fname
= NULL
;
744 opts
->out_fname
= NULL
;
746 /* Initialize is_idchar to allow $. */
747 opts
->dollars_in_ident
= 1;
748 initialize_char_syntax (opts
);
750 opts
->no_line_commands
= 0;
751 opts
->no_trigraphs
= 1;
752 opts
->put_out_comments
= 0;
753 opts
->print_include_names
= 0;
754 opts
->dump_macros
= dump_none
;
758 opts
->cplusplus_comments
= 0;
765 opts
->pedantic_errors
= 0;
766 opts
->inhibit_warnings
= 0;
767 opts
->warn_comments
= 0;
768 opts
->warn_import
= 1;
769 opts
->warnings_are_errors
= 0;
773 null_underflow (pfile
)
780 null_cleanup (pbuf
, pfile
)
788 macro_cleanup (pbuf
, pfile
)
792 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
793 if (macro
->type
== T_DISABLED
)
794 macro
->type
= T_MACRO
;
795 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
801 file_cleanup (pbuf
, pfile
)
813 /* Assuming we have read '/'.
814 If this is the start of a comment (followed by '*' or '/'),
815 skip to the end of the comment, and return ' '.
816 Return EOF if we reached the end of file before the end of the comment.
817 If not the start of a comment, return '/'. */
820 skip_comment (pfile
, linep
)
825 while (PEEKC() == '\\' && PEEKN(1) == '\n')
840 while (c
== '\\' && PEEKC() == '\n')
844 FORWARD(1), c
= GETC();
846 if (prev_c
== '*' && c
== '/')
848 if (c
== '\n' && linep
)
852 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
859 return ' '; /* Allow // to be terminated by EOF. */
860 while (c
== '\\' && PEEKC() == '\n')
869 /* Don't consider final '\n' to be part of comment. */
879 /* Skip whitespace \-newline and comments. Does not macro-expand. */
882 cpp_skip_hspace (pfile
)
892 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
893 cpp_pedwarn (pfile
, "%s in preprocessing directive",
894 c
== '\f' ? "formfeed" : "vertical tab");
900 c
= skip_comment (pfile
, NULL
);
903 if (c
== EOF
|| c
== '/')
906 else if (c
== '\\' && PEEKN(1) == '\n') {
909 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
910 && is_hor_space
[PEEKN(1)])
916 /* Read the rest of the current line.
917 The line is appended to PFILE's output buffer. */
920 copy_rest_of_line (pfile
)
923 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
940 goto scan_directive_token
;
944 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '/'))
945 goto scan_directive_token
;
949 if (CPP_PEDANTIC (pfile
))
950 cpp_pedwarn (pfile
, "%s in preprocessing directive",
951 c
== '\f' ? "formfeed" : "vertical tab");
957 scan_directive_token
:
959 cpp_get_token (pfile
);
965 CPP_NUL_TERMINATE (pfile
);
969 skip_rest_of_line (pfile
)
972 long old
= CPP_WRITTEN (pfile
);
973 copy_rest_of_line (pfile
);
974 CPP_SET_WRITTEN (pfile
, old
);
977 /* Handle a possible # directive.
978 '#' has already been read. */
981 handle_directive (pfile
)
984 register struct directive
*kt
;
987 U_CHAR
*ident
, *line_end
;
988 long old_written
= CPP_WRITTEN (pfile
);
990 cpp_skip_hspace (pfile
);
993 if (c
>= '0' && c
<= '9')
995 /* Handle # followed by a line number. */
996 if (CPP_PEDANTIC (pfile
))
997 cpp_pedwarn (pfile
, "`#' followed by integer");
998 do_line (pfile
, NULL
);
999 goto done_a_directive
;
1002 /* Now find the directive name. */
1003 CPP_PUTC (pfile
, '#');
1004 parse_name (pfile
, GETC());
1005 ident
= pfile
->token_buffer
+ old_written
+ 1;
1006 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
1007 if (ident_length
== 0 && PEEKC() == '\n')
1009 /* A line of just `#' becomes blank. */
1010 goto done_a_directive
;
1014 if (ident_length
== 0 || !is_idstart
[*ident
]) {
1016 while (is_idchar
[*p
]) {
1017 if (*p
< '0' || *p
> '9')
1021 /* Avoid error for `###' and similar cases unless -pedantic. */
1023 while (*p
== '#' || is_hor_space
[*p
]) p
++;
1025 if (pedantic
&& !lang_asm
)
1026 cpp_warning (pfile
, "invalid preprocessor directive");
1032 cpp_error (pfile
, "invalid preprocessor directive name");
1038 * Decode the keyword and call the appropriate expansion
1039 * routine, after moving the input pointer up to the next line.
1041 for (kt
= directive_table
; ; kt
++) {
1042 if (kt
->length
<= 0)
1043 goto not_a_directive
;
1044 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
))
1048 if (kt
->command_reads_line
)
1052 /* Nonzero means do not delete comments within the directive.
1053 #define needs this when -traditional. */
1054 int comments
= CPP_TRADITIONAL (pfile
) && kt
->type
== T_DEFINE
;
1055 int save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1056 CPP_OPTIONS (pfile
)->put_out_comments
= comments
;
1057 after_ident
= CPP_WRITTEN (pfile
);
1058 copy_rest_of_line (pfile
);
1059 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1062 /* We may want to pass through #define, #pragma, and #include.
1063 Other directives may create output, but we don't want the directive
1064 itself out, so we pop it now. For example conditionals may emit
1065 #failed ... #endfailed stuff. But note that popping the buffer
1066 means the parameters to kt->func may point after pfile->limit
1067 so these parameters are invalid as soon as something gets appended
1068 to the token_buffer. */
1070 line_end
= CPP_PWRITTEN (pfile
);
1071 if (! (kt
->type
== T_DEFINE
1072 || kt
->type
== T_PRAGMA
1073 || (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)
1074 && CPP_OPTIONS (pfile
)->dump_includes
)))
1075 CPP_SET_WRITTEN (pfile
, old_written
);
1077 (*kt
->func
) (pfile
, kt
, pfile
->token_buffer
+ after_ident
, line_end
);
1079 if (kt
->type
== T_DEFINE
)
1081 if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
1083 /* Skip "#define". */
1084 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7;
1086 SKIP_WHITE_SPACE (p
);
1087 while (is_idchar
[*p
]) p
++;
1089 CPP_PUTC (pfile
, '\n');
1091 else if (CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
1092 CPP_SET_WRITTEN (pfile
, old_written
);
1102 /* Pass a directive through to the output file.
1103 BUF points to the contents of the directive, as a contiguous string.
1104 LIMIT points to the first character past the end of the directive.
1105 KEYWORD is the keyword-table entry for the directive. */
1108 pass_thru_directive (buf
, limit
, pfile
, keyword
)
1109 U_CHAR
*buf
, *limit
;
1111 struct directive
*keyword
;
1113 register unsigned keyword_length
= keyword
->length
;
1115 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
1116 CPP_PUTC_Q (pfile
, '#');
1117 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
1118 if (limit
!= buf
&& buf
[0] != ' ')
1119 CPP_PUTC_Q (pfile
, ' ');
1120 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
1122 CPP_PUTS_Q (pfile
, '\n');
1123 /* Count the line we have just made in the output,
1124 to get in sync properly. */
1129 /* The arglist structure is built by do_define to tell
1130 collect_definition where the argument names begin. That
1131 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1132 would contain pointers to the strings x, y, and z.
1133 Collect_definition would then build a DEFINITION node,
1134 with reflist nodes pointing to the places x, y, and z had
1135 appeared. So the arglist is just convenience data passed
1136 between these two routines. It is not kept around after
1137 the current #define has been processed and entered into the
1141 struct arglist
*next
;
1148 /* Read a replacement list for a macro with parameters.
1149 Build the DEFINITION structure.
1150 Reads characters of text starting at BUF until END.
1151 ARGLIST specifies the formal parameters to look for
1152 in the text of the definition; NARGS is the number of args
1153 in that list, or -1 for a macro name that wants no argument list.
1154 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1155 and NAMELEN is its length in characters.
1157 Note that comments, backslash-newlines, and leading white space
1158 have already been deleted from the argument. */
1161 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
1163 U_CHAR
*buf
, *limit
;
1165 struct arglist
*arglist
;
1168 register U_CHAR
*p
, *lastp
, *exp_p
;
1169 struct reflist
*endpat
= NULL
;
1170 /* Pointer to first nonspace after last ## seen. */
1172 /* Pointer to first nonspace after last single-# seen. */
1173 U_CHAR
*stringify
= 0;
1175 int expected_delimiter
= '\0';
1177 /* Scan thru the replacement list, ignoring comments and quoted
1178 strings, picking up on the macro calls. It does a linear search
1179 thru the arg list on every potential symbol. Profiling might say
1180 that something smarter should happen. */
1185 /* Find the beginning of the trailing whitespace. */
1187 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
1189 /* Allocate space for the text in the macro definition.
1190 Leading and trailing whitespace chars need 2 bytes each.
1191 Each other input char may or may not need 1 byte,
1192 so this is an upper bound. The extra 5 are for invented
1193 leading and trailing newline-marker and final null. */
1194 maxsize
= (sizeof (DEFINITION
)
1196 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1200 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
1202 defn
->nargs
= nargs
;
1203 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
1208 /* Add one initial space escape-marker to prevent accidental
1209 token-pasting (often removed by macroexpand). */
1213 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1214 cpp_error (pfile
, "`##' at start of macro definition");
1218 /* Process the main body of the definition. */
1220 int skipped_arg
= 0;
1221 register U_CHAR c
= *p
++;
1225 if (!CPP_TRADITIONAL (pfile
)) {
1229 if (expected_delimiter
!= '\0') {
1230 if (c
== expected_delimiter
)
1231 expected_delimiter
= '\0';
1233 expected_delimiter
= c
;
1237 if (p
< limit
&& expected_delimiter
) {
1238 /* In a string, backslash goes through
1239 and makes next char ordinary. */
1245 /* An '@' in a string or character constant stands for itself,
1246 and does not need to be escaped. */
1247 if (!expected_delimiter
)
1252 /* # is ordinary inside a string. */
1253 if (expected_delimiter
)
1255 if (p
< limit
&& *p
== '#') {
1256 /* ##: concatenate preceding and following tokens. */
1257 /* Take out the first #, discard preceding whitespace. */
1259 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1261 /* Skip the second #. */
1263 /* Discard following whitespace. */
1264 SKIP_WHITE_SPACE (p
);
1267 cpp_error (pfile
, "`##' at end of macro definition");
1268 } else if (nargs
>= 0) {
1269 /* Single #: stringify following argument ref.
1270 Don't leave the # in the expansion. */
1272 SKIP_WHITE_SPACE (p
);
1273 if (p
== limit
|| ! is_idstart
[*p
]
1274 || (*p
== 'L' && p
+ 1 < limit
&& (p
[1] == '\'' || p
[1] == '"')))
1276 "`#' operator is not followed by a macro argument name");
1283 /* In -traditional mode, recognize arguments inside strings and
1284 and character constants, and ignore special properties of #.
1285 Arguments inside strings are considered "stringified", but no
1286 extra quote marks are supplied. */
1290 if (expected_delimiter
!= '\0') {
1291 if (c
== expected_delimiter
)
1292 expected_delimiter
= '\0';
1294 expected_delimiter
= c
;
1298 /* Backslash quotes delimiters and itself, but not macro args. */
1299 if (expected_delimiter
!= 0 && p
< limit
1300 && (*p
== expected_delimiter
|| *p
== '\\')) {
1307 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1310 /* If we find a comment that wasn't removed by handle_directive,
1311 this must be -traditional. So replace the comment with
1315 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1318 /* Mark this as a concatenation-point, as if it had been ##. */
1326 /* Handle the start of a symbol. */
1327 if (is_idchar
[c
] && nargs
> 0) {
1328 U_CHAR
*id_beg
= p
- 1;
1332 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1333 id_len
= p
- id_beg
;
1336 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
1337 register struct arglist
*arg
;
1339 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1340 struct reflist
*tpat
;
1342 if (arg
->name
[0] == c
1343 && arg
->length
== id_len
1344 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1345 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1346 if (CPP_TRADITIONAL (pfile
)) {
1347 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1351 "macro arg `%.*s' would be stringified with -traditional.",
1355 /* If ANSI, don't actually substitute inside a string. */
1356 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1358 /* make a pat node for this arg and append it to the end of
1360 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1362 tpat
->raw_before
= concat
== id_beg
;
1363 tpat
->raw_after
= 0;
1364 tpat
->rest_args
= arg
->rest_args
;
1365 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1366 ? expected_delimiter
!= '\0'
1367 : stringify
== id_beg
);
1370 defn
->pattern
= tpat
;
1372 endpat
->next
= tpat
;
1375 tpat
->argno
= arg
->argno
;
1376 tpat
->nchars
= exp_p
- lastp
;
1378 register U_CHAR
*p1
= p
;
1379 SKIP_WHITE_SPACE (p1
);
1380 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1381 tpat
->raw_after
= 1;
1383 lastp
= exp_p
; /* place to start copying from next time */
1390 /* If this was not a macro arg, copy it into the expansion. */
1391 if (! skipped_arg
) {
1392 register U_CHAR
*lim1
= p
;
1396 if (stringify
== id_beg
)
1398 "`#' operator should be followed by a macro argument name");
1403 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1405 /* If ANSI, put in a "@ " marker to prevent token pasting.
1406 But not if "inside a string" (which in ANSI mode
1407 happens only for -D option). */
1414 defn
->length
= exp_p
- defn
->expansion
;
1416 /* Crash now if we overrun the allocated size. */
1417 if (defn
->length
+ 1 > maxsize
)
1421 /* This isn't worth the time it takes. */
1422 /* give back excess storage */
1423 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1430 * special extension string that can be added to the last macro argument to
1431 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1432 * #define wow(a, b...) process (b, a, b)
1433 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1434 * { wow (one, two); } -> { process (two, one, two); }
1435 * if this "rest_arg" is used with the concat token '##' and if it is not
1436 * supplied then the token attached to with ## will not be outputted. Ex:
1437 * #define wow (a, b...) process (b ## , a, ## b)
1438 * { wow (1, 2); } -> { process (2, 1, 2); }
1439 * { wow (one); } -> { process (one); {
1441 static char rest_extension
[] = "...";
1442 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1444 /* Create a DEFINITION node from a #define directive. Arguments are
1445 as for do_define. */
1448 create_definition (buf
, limit
, pfile
, predefinition
)
1449 U_CHAR
*buf
, *limit
;
1453 U_CHAR
*bp
; /* temp ptr into input buffer */
1454 U_CHAR
*symname
; /* remember where symbol name starts */
1455 int sym_length
; /* and how long it is */
1458 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1460 int arglengths
= 0; /* Accumulate lengths of arg names
1461 plus number of args. */
1463 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1467 while (is_hor_space
[*bp
])
1470 symname
= bp
; /* remember where it starts */
1472 sym_length
= check_macro_name (pfile
, bp
, "macro");
1475 /* Lossage will occur if identifiers or control keywords are broken
1476 across lines using backslash. This is not the right place to take
1480 struct arglist
*arg_ptrs
= NULL
;
1483 bp
++; /* skip '(' */
1484 SKIP_WHITE_SPACE (bp
);
1486 /* Loop over macro argument names. */
1487 while (*bp
!= ')') {
1488 struct arglist
*temp
;
1490 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1492 temp
->next
= arg_ptrs
;
1493 temp
->argno
= argno
++;
1494 temp
->rest_args
= 0;
1498 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1500 if (!is_idstart
[*bp
])
1501 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1503 /* Find the end of the arg name. */
1504 while (is_idchar
[*bp
]) {
1506 /* do we have a "special" rest-args extension here? */
1507 if (limit
- bp
> REST_EXTENSION_LENGTH
1508 && strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1510 temp
->rest_args
= 1;
1514 temp
->length
= bp
- temp
->name
;
1516 bp
+= REST_EXTENSION_LENGTH
;
1517 arglengths
+= temp
->length
+ 2;
1518 SKIP_WHITE_SPACE (bp
);
1519 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1520 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1525 SKIP_WHITE_SPACE (bp
);
1528 cpp_error (pfile
, "unterminated parameter list in `#define'");
1532 struct arglist
*otemp
;
1534 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1535 if (temp
->length
== otemp
->length
1536 && strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1539 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1540 (void) strncpy (name
, temp
->name
, temp
->length
);
1541 name
[temp
->length
] = '\0';
1543 "duplicate argument name `%s' in `#define'", name
);
1549 ++bp
; /* skip paren */
1550 SKIP_WHITE_SPACE (bp
);
1551 /* now everything from bp before limit is the definition. */
1552 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1553 defn
->rest_args
= rest_args
;
1555 /* Now set defn->args.argnames to the result of concatenating
1556 the argument names in reverse order
1557 with comma-space between them. */
1558 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1560 struct arglist
*temp
;
1562 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1563 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1565 if (temp
->next
!= 0) {
1566 defn
->args
.argnames
[i
++] = ',';
1567 defn
->args
.argnames
[i
++] = ' ';
1570 defn
->args
.argnames
[i
] = 0;
1573 /* Simple expansion or empty definition. */
1577 if (is_hor_space
[*bp
]) {
1579 SKIP_WHITE_SPACE (bp
);
1582 case '!': case '"': case '#': case '%': case '&': case '\'':
1583 case ')': case '*': case '+': case ',': case '-': case '.':
1584 case '/': case ':': case ';': case '<': case '=': case '>':
1585 case '?': case '[': case '\\': case ']': case '^': case '{':
1586 case '|': case '}': case '~':
1587 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1588 sym_length
, symname
);
1592 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1593 sym_length
, symname
);
1598 /* now everything from bp before limit is the definition. */
1599 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1600 defn
->args
.argnames
= (U_CHAR
*) "";
1606 /* OP is null if this is a predefinition */
1607 defn
->predefined
= predefinition
;
1609 mdef
.symnam
= symname
;
1610 mdef
.symlen
= sym_length
;
1619 /* Check a purported macro name SYMNAME, and yield its length.
1620 USAGE is the kind of name this is intended for. */
1623 check_macro_name (pfile
, symname
, usage
)
1631 for (p
= symname
; is_idchar
[*p
]; p
++)
1633 sym_length
= p
- symname
;
1635 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
1636 cpp_error (pfile
, "invalid %s name", usage
);
1637 else if (!is_idstart
[*symname
]) {
1638 U_CHAR
*msg
; /* what pain... */
1639 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1640 bcopy (symname
, msg
, sym_length
);
1641 msg
[sym_length
] = 0;
1642 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1644 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1645 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1650 /* Return zero if two DEFINITIONs are isomorphic. */
1653 compare_defs (pfile
, d1
, d2
)
1655 DEFINITION
*d1
, *d2
;
1657 register struct reflist
*a1
, *a2
;
1658 register U_CHAR
*p1
= d1
->expansion
;
1659 register U_CHAR
*p2
= d2
->expansion
;
1662 if (d1
->nargs
!= d2
->nargs
)
1664 if (CPP_PEDANTIC (pfile
)
1665 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1667 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1668 a1
= a1
->next
, a2
= a2
->next
) {
1669 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1670 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1671 || a1
->argno
!= a2
->argno
1672 || a1
->stringify
!= a2
->stringify
1673 || a1
->raw_before
!= a2
->raw_before
1674 || a1
->raw_after
!= a2
->raw_after
)
1682 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1683 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1688 /* Return 1 if two parts of two macro definitions are effectively different.
1689 One of the parts starts at BEG1 and has LEN1 chars;
1690 the other has LEN2 chars at BEG2.
1691 Any sequence of whitespace matches any other sequence of whitespace.
1692 FIRST means these parts are the first of a macro definition;
1693 so ignore leading whitespace entirely.
1694 LAST means these parts are the last of a macro definition;
1695 so ignore trailing whitespace entirely. */
1698 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1700 U_CHAR
*beg1
, *beg2
;
1704 register U_CHAR
*end1
= beg1
+ len1
;
1705 register U_CHAR
*end2
= beg2
+ len2
;
1707 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1708 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1711 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1712 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1714 while (beg1
!= end1
&& beg2
!= end2
) {
1715 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1716 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1717 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1718 } else if (*beg1
== *beg2
) {
1722 return (beg1
!= end1
) || (beg2
!= end2
);
1725 /* Process a #define command.
1726 BUF points to the contents of the #define command, as a contiguous string.
1727 LIMIT points to the first character past the end of the definition.
1728 KEYWORD is the keyword-table entry for #define,
1729 or NULL for a "predefined" macro. */
1732 do_define (pfile
, keyword
, buf
, limit
)
1734 struct directive
*keyword
;
1735 U_CHAR
*buf
, *limit
;
1742 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1743 if (pcp_outfile
&& keyword
)
1744 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1747 mdef
= create_definition (buf
, limit
, pfile
, keyword
== NULL
);
1751 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1753 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1756 /* Redefining a precompiled key is ok. */
1757 if (hp
->type
== T_PCSTRING
)
1759 /* Redefining a macro is ok if the definitions are the same. */
1760 else if (hp
->type
== T_MACRO
)
1761 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
1762 /* Redefining a constant is ok with -D. */
1763 else if (hp
->type
== T_CONST
)
1764 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1765 /* Print the warning if it's not ok. */
1768 U_CHAR
*msg
; /* what pain... */
1770 /* If we are passing through #define and #undef directives, do
1771 that for this re-definition now. */
1772 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1773 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1775 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1777 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1778 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1779 cpp_pedwarn (pfile
, msg
);
1780 if (hp
->type
== T_MACRO
)
1781 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1782 "this is the location of the previous definition");
1784 /* Replace the old definition. */
1786 hp
->value
.defn
= mdef
.defn
;
1790 /* If we are passing through #define and #undef directives, do
1791 that for this new definition now. */
1792 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1793 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1794 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1795 (char *) mdef
.defn
, hashcode
);
1805 /* This structure represents one parsed argument in a macro call.
1806 `raw' points to the argument text as written (`raw_length' is its length).
1807 `expanded' points to the argument's macro-expansion
1808 (its length is `expand_length').
1809 `stringified_length' is the length the argument would have
1811 `use_count' is the number of times this macro arg is substituted
1812 into the macro. If the actual use count exceeds 10,
1813 the value stored is 10. */
1815 /* raw and expanded are relative to ARG_BASE */
1816 #define ARG_BASE ((pfile)->token_buffer)
1819 /* Strings relative to pfile->token_buffer */
1820 long raw
, expanded
, stringified
;
1821 int raw_length
, expand_length
;
1822 int stringified_length
;
1827 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1828 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1829 as the new input buffer.
1830 Return the new buffer, or NULL on failure. */
1833 cpp_push_buffer (pfile
, buffer
, length
)
1838 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1839 if (buf
== pfile
->buffer_stack
)
1841 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1846 bzero ((char *) buf
, sizeof (cpp_buffer
));
1847 CPP_BUFFER (pfile
) = buf
;
1848 buf
->if_stack
= pfile
->if_stack
;
1849 buf
->cleanup
= null_cleanup
;
1850 buf
->underflow
= null_underflow
;
1851 buf
->buf
= buf
->cur
= buffer
;
1852 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1858 cpp_pop_buffer (pfile
)
1861 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1862 (*buf
->cleanup
) (buf
, pfile
);
1863 return ++CPP_BUFFER (pfile
);
1866 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1867 Pop the buffer when done. */
1870 cpp_scan_buffer (pfile
)
1873 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1876 enum cpp_token token
= cpp_get_token (pfile
);
1877 if (token
== CPP_EOF
) /* Should not happen ... */
1879 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1881 cpp_pop_buffer (pfile
);
1888 * Rescan a string (which may have escape marks) into pfile's buffer.
1889 * Place the result in pfile->token_buffer.
1891 * The input is copied before it is scanned, so it is safe to pass
1892 * it something from the token_buffer that will get overwritten
1893 * (because it follows CPP_WRITTEN). This is used by do_include.
1897 cpp_expand_to_buffer (pfile
, buf
, length
)
1902 register cpp_buffer
*ip
;
1906 U_CHAR
*limit
= buf
+ length
;
1909 int odepth
= indepth
;
1915 /* Set up the input on the input stack. */
1917 buf1
= (U_CHAR
*) alloca (length
+ 1);
1919 register U_CHAR
*p1
= buf
;
1920 register U_CHAR
*p2
= buf1
;
1927 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1930 ip
->has_escapes
= 1;
1932 ip
->lineno
= obuf
.lineno
= 1;
1935 /* Scan the input, create the output. */
1936 cpp_scan_buffer (pfile
);
1939 if (indepth
!= odepth
)
1943 CPP_NUL_TERMINATE (pfile
);
1948 adjust_position (buf
, limit
, linep
, colp
)
1958 (*linep
)++, (*colp
) = 1;
1964 /* Move line_base forward, updating lineno and colno. */
1967 update_position (pbuf
)
1968 register cpp_buffer
*pbuf
;
1970 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
1971 unsigned char *new_pos
= pbuf
->cur
;
1972 register struct parse_marker
*mark
;
1973 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
1975 if (pbuf
->buf
+ mark
->position
< new_pos
)
1976 new_pos
= pbuf
->buf
+ mark
->position
;
1978 pbuf
->line_base
+= new_pos
- old_pos
;
1979 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
1983 cpp_buf_line_and_col (pbuf
, linep
, colp
)
1984 register cpp_buffer
*pbuf
;
1992 *linep
= pbuf
->lineno
;
1993 *colp
= pbuf
->colno
;
1994 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
2003 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2006 cpp_file_buffer (pfile
)
2009 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2011 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2012 if (ip
->fname
!= NULL
)
2018 count_newlines (buf
, limit
)
2019 register U_CHAR
*buf
;
2020 register U_CHAR
*limit
;
2022 register long count
= 0;
2033 * write out a #line command, for instance, after an #include file.
2034 * If CONDITIONAL is nonzero, we can omit the #line if it would
2035 * appear to be a no-op, and we can output a few newlines instead
2036 * if we want to increase the line number by a small amount.
2037 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2041 output_line_command (pfile
, conditional
, file_change
)
2044 enum file_change_code file_change
;
2047 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2049 if (ip
->fname
== NULL
)
2052 update_position (ip
);
2054 if (CPP_OPTIONS (pfile
)->no_line_commands
2055 || CPP_OPTIONS (pfile
)->no_output
)
2058 line
= CPP_BUFFER (pfile
)->lineno
;
2059 col
= CPP_BUFFER (pfile
)->colno
;
2060 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2062 if (CPP_OPTIONS (pfile
)->no_line_commands
)
2066 if (line
== pfile
->lineno
)
2069 /* If the inherited line number is a little too small,
2070 output some newlines instead of a #line command. */
2071 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
2072 CPP_RESERVE (pfile
, 20);
2073 while (line
> pfile
->lineno
) {
2074 CPP_PUTC_Q (pfile
, '\n');
2082 /* Don't output a line number of 0 if we can help it. */
2083 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
2084 && *ip
->bufp
== '\n') {
2090 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
2092 #ifdef OUTPUT_LINE_COMMANDS
2093 static char sharp_line
[] = "#line ";
2095 static char sharp_line
[] = "# ";
2097 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
2100 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
2101 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
2103 quote_string (pfile
, ip
->nominal_fname
);
2104 if (file_change
!= same_file
) {
2105 CPP_PUTC_Q (pfile
, ' ');
2106 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
2108 /* Tell cc1 if following text comes from a system header file. */
2109 if (ip
->system_header_p
) {
2110 CPP_PUTC_Q (pfile
, ' ');
2111 CPP_PUTC_Q (pfile
, '3');
2113 #ifndef NO_IMPLICIT_EXTERN_C
2114 /* Tell cc1plus if following text should be treated as C. */
2115 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
2116 CPP_PUTC_Q (pfile
, ' ');
2117 CPP_PUTC_Q (pfile
, '4');
2120 CPP_PUTC_Q (pfile
, '\n');
2121 pfile
->lineno
= line
;
2125 * Parse a macro argument and append the info on PFILE's token_buffer.
2126 * REST_ARGS means to absorb the rest of the args.
2127 * Return nonzero to indicate a syntax error.
2130 static enum cpp_token
2131 macarg (pfile
, rest_args
)
2136 enum cpp_token token
;
2137 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
2138 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
2140 /* Try to parse as much of the argument as exists at this
2141 input stack level. */
2142 pfile
->no_macro_expand
++;
2145 token
= cpp_get_token (pfile
);
2151 /* If we've hit end of file, it's an error (reported by caller).
2152 Ditto if it's the end of cpp_expand_to_buffer text.
2153 If we've hit end of macro, just continue. */
2154 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2165 /* if we've returned to lowest level and
2166 we aren't absorbing all args */
2167 if (paren
== 0 && rest_args
== 0)
2171 /* Remove ',' or ')' from argument buffer. */
2172 CPP_ADJUST_WRITTEN (pfile
, -1);
2179 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
2180 pfile
->no_macro_expand
--;
2185 /* Turn newlines to spaces in the string of length LENGTH at START,
2186 except inside of string constants.
2187 The string is copied into itself with its beginning staying fixed. */
2190 change_newlines (start
, length
)
2194 register U_CHAR
*ibp
;
2195 register U_CHAR
*obp
;
2196 register U_CHAR
*limit
;
2200 limit
= start
+ length
;
2203 while (ibp
< limit
) {
2204 *obp
++ = c
= *ibp
++;
2209 /* Notice and skip strings, so that we don't delete newlines in them. */
2212 while (ibp
< limit
) {
2213 *obp
++ = c
= *ibp
++;
2216 if (c
== '\n' && quotec
== '\'')
2232 if (!pfile
->timebuf
) {
2233 time_t t
= time ((time_t *) 0);
2234 pfile
->timebuf
= localtime (&t
);
2236 return pfile
->timebuf
;
2239 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2240 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2244 * expand things like __FILE__. Place the expansion into the output
2245 * buffer *without* rescanning.
2249 special_symbol (hp
, pfile
)
2256 cpp_buffer
*ip
= NULL
;
2259 int paren
= 0; /* For special `defined' keyword */
2262 if (pcp_outfile
&& pcp_inside_if
2263 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2265 "Predefined macro `%s' used inside `#if' during precompilation",
2269 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2271 if (ip
== CPP_NULL_BUFFER (pfile
))
2273 cpp_error (pfile
, "cccp error: not in any file?!");
2274 return; /* the show must go on */
2276 if (ip
->fname
!= NULL
)
2286 if (hp
->type
== T_BASE_FILE
)
2288 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2289 ip
= CPP_PREV_BUFFER (ip
);
2291 string
= ip
->nominal_fname
;
2295 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2296 quote_string (pfile
, string
);
2300 case T_INCLUDE_LEVEL
:
2302 ip
= CPP_BUFFER (pfile
);
2303 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2304 if (ip
->fname
!= NULL
)
2307 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2308 sprintf (buf
, "%d", true_indepth
- 1);
2312 buf
= (char *) alloca (3 + strlen (version_string
));
2313 sprintf (buf
, "\"%s\"", version_string
);
2316 #ifndef NO_BUILTIN_SIZE_TYPE
2322 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2323 case T_PTRDIFF_TYPE
:
2329 buf
= CPP_WCHAR_TYPE (pfile
);
2332 case T_USER_LABEL_PREFIX_TYPE
:
2333 buf
= USER_LABEL_PREFIX
;
2336 case T_REGISTER_PREFIX_TYPE
:
2337 buf
= REGISTER_PREFIX
;
2341 buf
= (char *) alloca (4 * sizeof (int));
2342 sprintf (buf
, "%d", hp
->value
.ival
);
2343 #ifdef STDC_0_IN_SYSTEM_HEADERS
2344 if (ip
->system_header_p
2345 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
2346 && ! cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", -1, -1))
2350 if (pcp_inside_if
&& pcp_outfile
)
2351 /* Output a precondition for this macro use */
2352 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2358 long line
= ip
->lineno
;
2359 long col
= ip
->colno
;
2360 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2362 buf
= (char *) alloca (10);
2363 sprintf (buf
, "%ld", line
);
2369 buf
= (char *) alloca (20);
2370 timebuf
= timestamp (pfile
);
2371 if (hp
->type
== T_DATE
)
2372 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2373 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2375 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2379 case T_SPEC_DEFINED
:
2380 buf
= " 0 "; /* Assume symbol is not defined */
2381 ip
= CPP_BUFFER (pfile
);
2382 SKIP_WHITE_SPACE (ip
->cur
);
2383 if (*ip
->cur
== '(')
2386 ip
->cur
++; /* Skip over the paren */
2387 SKIP_WHITE_SPACE (ip
->cur
);
2390 if (!is_idstart
[*ip
->cur
])
2392 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
2394 if ((hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1)))
2397 if (pcp_outfile
&& pcp_inside_if
2398 && (hp
->type
== T_CONST
2399 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2400 /* Output a precondition for this macro use. */
2401 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2407 if (pcp_outfile
&& pcp_inside_if
)
2409 /* Output a precondition for this macro use */
2410 U_CHAR
*cp
= ip
->bufp
;
2411 fprintf (pcp_outfile
, "#undef ");
2412 while (is_idchar
[*cp
]) /* Ick! */
2413 fputc (*cp
++, pcp_outfile
);
2414 putc ('\n', pcp_outfile
);
2417 while (is_idchar
[*ip
->cur
])
2419 SKIP_WHITE_SPACE (ip
->cur
);
2422 if (*ip
->cur
!= ')')
2430 cpp_error (pfile
, "`defined' without an identifier");
2434 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2438 CPP_RESERVE (pfile
, len
+ 1);
2439 CPP_PUTS_Q (pfile
, buf
, len
);
2440 CPP_NUL_TERMINATE_Q (pfile
);
2445 /* Write out a #define command for the special named MACRO_NAME
2446 to PFILE's token_buffer. */
2449 dump_special_to_buffer (pfile
, macro_name
)
2453 static char define_directive
[] = "#define ";
2454 int macro_name_length
= strlen (macro_name
);
2455 output_line_command (pfile
, 0, same_file
);
2456 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2457 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2458 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2459 CPP_PUTC_Q (pfile
, ' ');
2460 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2461 CPP_PUTC (pfile
, '\n');
2464 /* Initialize the built-in macros. */
2467 initialize_builtins (pfile
)
2470 install ((U_CHAR
*)"__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2471 install ((U_CHAR
*)"__DATE__", -1, T_DATE
, 0, 0, -1);
2472 install ((U_CHAR
*)"__FILE__", -1, T_FILE
, 0, 0, -1);
2473 install ((U_CHAR
*)"__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2474 install ((U_CHAR
*)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2475 install ((U_CHAR
*)"__VERSION__", -1, T_VERSION
, 0, 0, -1);
2476 #ifndef NO_BUILTIN_SIZE_TYPE
2477 install ((U_CHAR
*)"__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2479 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2480 install ((U_CHAR
*)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2482 install ((U_CHAR
*)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2483 install ((U_CHAR
*)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2484 install ((U_CHAR
*)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2485 install ((U_CHAR
*)"__TIME__", -1, T_TIME
, 0, 0, -1);
2486 if (!CPP_TRADITIONAL (pfile
))
2487 install ((U_CHAR
*)"__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2488 if (CPP_OPTIONS (pfile
)->objc
)
2489 install ((U_CHAR
*)"__OBJC__", -1, T_CONST
, 1, 0, -1);
2490 /* This is supplied using a -D by the compiler driver
2491 so that it is present only when truly compiling with GNU C. */
2492 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2494 if (CPP_OPTIONS (pfile
)->debug_output
)
2496 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2497 dump_special_to_buffer (pfile
, "__VERSION__");
2498 #ifndef NO_BUILTIN_SIZE_TYPE
2499 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2501 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2502 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2504 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2505 dump_special_to_buffer (pfile
, "__DATE__");
2506 dump_special_to_buffer (pfile
, "__TIME__");
2507 if (!CPP_TRADITIONAL (pfile
))
2508 dump_special_to_buffer (pfile
, "__STDC__");
2509 if (CPP_OPTIONS (pfile
)->objc
)
2510 dump_special_to_buffer (pfile
, "__OBJC__");
2514 /* Return 1 iff a token ending in C1 followed directly by a token C2
2515 could cause mis-tokenization. */
2518 unsafe_chars (c1
, c2
)
2524 if (c2
== c1
|| c2
== '=')
2528 case '0': case '1': case '2': case '3': case '4':
2529 case '5': case '6': case '7': case '8': case '9':
2530 case 'e': case 'E': case 'p': case 'P':
2531 if (c2
== '-' || c2
== '+')
2532 return 1; /* could extend a pre-processing number */
2535 if (c2
== '\'' || c2
== '\"')
2536 return 1; /* Could turn into L"xxx" or L'xxx'. */
2540 case 'a': case 'b': case 'c': case 'd': case 'f':
2541 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2542 case 'm': case 'n': case 'o': case 'q': case 'r':
2543 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2545 case 'A': case 'B': case 'C': case 'D': case 'F':
2546 case 'G': case 'H': case 'I': case 'J': case 'K':
2547 case 'M': case 'N': case 'O': case 'Q': case 'R':
2548 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2550 /* We're in the middle of either a name or a pre-processing number. */
2551 return (is_idchar
[c2
] || c2
== '.');
2552 case '<': case '>': case '!': case '%': case '#': case ':':
2553 case '^': case '&': case '|': case '*': case '/': case '=':
2554 return (c2
== c1
|| c2
== '=');
2559 /* Expand a macro call.
2560 HP points to the symbol that is the macro being called.
2561 Put the result of expansion onto the input stack
2562 so that subsequent input by our caller will use it.
2564 If macro wants arguments, caller has already verified that
2565 an argument list follows; arguments come from the input stack. */
2568 macroexpand (pfile
, hp
)
2573 DEFINITION
*defn
= hp
->value
.defn
;
2574 register U_CHAR
*xbuf
;
2575 long start_line
, start_column
;
2577 struct argdata
*args
;
2578 long old_written
= CPP_WRITTEN (pfile
);
2580 int start_line
= instack
[indepth
].lineno
;
2582 int rest_args
, rest_zero
;
2586 CHECK_DEPTH (return;);
2590 /* This macro is being used inside a #if, which means it must be */
2591 /* recorded as a precondition. */
2592 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2593 dump_single_macro (hp
, pcp_outfile
);
2596 pfile
->output_escapes
++;
2597 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2599 nargs
= defn
->nargs
;
2603 enum cpp_token token
;
2605 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2607 for (i
= 0; i
< nargs
; i
++)
2609 args
[i
].raw
= args
[i
].expanded
= 0;
2610 args
[i
].raw_length
= 0;
2611 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2612 args
[i
].use_count
= 0;
2615 /* Parse all the macro args that are supplied. I counts them.
2616 The first NARGS args are stored in ARGS.
2617 The rest are discarded. If rest_args is set then we assume
2618 macarg absorbed the rest of the args. */
2622 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2627 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2629 /* if we are working on last arg which absorbs rest of args... */
2630 if (i
== nargs
- 1 && defn
->rest_args
)
2632 args
[i
].raw
= CPP_WRITTEN (pfile
);
2633 token
= macarg (pfile
, rest_args
);
2634 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2635 args
[i
].newlines
= 0; /* FIXME */
2638 token
= macarg (pfile
, 0);
2639 if (token
== CPP_EOF
|| token
== CPP_POP
)
2641 cpp_error_with_line (pfile
, start_line
, start_column
,
2642 "unterminated macro call");
2646 } while (token
== CPP_COMMA
);
2648 /* If we got one arg but it was just whitespace, call that 0 args. */
2651 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2652 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2653 /* cpp.texi says for foo ( ) we provide one argument.
2654 However, if foo wants just 0 arguments, treat this as 0. */
2656 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2661 /* Don't output an error message if we have already output one for
2662 a parse error above. */
2664 if (nargs
== 0 && i
> 0)
2666 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2670 /* traditional C allows foo() if foo wants one argument. */
2671 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2673 /* the rest args token is allowed to absorb 0 tokens */
2674 else if (i
== nargs
- 1 && defn
->rest_args
)
2677 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2679 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2681 cpp_error (pfile
, "macro `%s' used with only %d args",
2687 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2691 /* If macro wants zero args, we parsed the arglist for checking only.
2692 Read directly from the macro definition. */
2695 xbuf
= defn
->expansion
;
2696 xbuf_len
= defn
->length
;
2700 register U_CHAR
*exp
= defn
->expansion
;
2701 register int offset
; /* offset in expansion,
2702 copied a piece at a time */
2703 register int totlen
; /* total amount of exp buffer filled so far */
2705 register struct reflist
*ap
, *last_ap
;
2707 /* Macro really takes args. Compute the expansion of this call. */
2709 /* Compute length in characters of the macro's expansion.
2710 Also count number of times each arg is used. */
2711 xbuf_len
= defn
->length
;
2712 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2716 register struct argdata
*arg
= &args
[ap
->argno
];
2717 /* Stringify it it hasn't already been */
2718 if (arg
->stringified_length
< 0)
2720 int arglen
= arg
->raw_length
;
2724 /* Initially need_space is -1. Otherwise, 1 means the
2725 previous character was a space, but we suppressed it;
2726 0 means the previous character was a non-space. */
2727 int need_space
= -1;
2729 arg
->stringified
= CPP_WRITTEN (pfile
);
2730 if (!CPP_TRADITIONAL (pfile
))
2731 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2732 for (; i
< arglen
; i
++)
2734 c
= (ARG_BASE
+ arg
->raw
)[i
];
2738 /* Internal sequences of whitespace are replaced by
2739 one space except within an string or char token.*/
2742 if (CPP_WRITTEN (pfile
) > arg
->stringified
2743 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2745 /* "@ " escape markers are removed */
2746 CPP_ADJUST_WRITTEN (pfile
, -1);
2749 if (need_space
== 0)
2753 else if (need_space
> 0)
2754 CPP_PUTC (pfile
, ' ');
2769 else if (c
== '\"' || c
== '\'')
2773 /* Escape these chars */
2774 if (c
== '\"' || (in_string
&& c
== '\\'))
2775 CPP_PUTC (pfile
, '\\');
2777 CPP_PUTC (pfile
, c
);
2780 CPP_RESERVE (pfile
, 4);
2781 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o",
2783 CPP_ADJUST_WRITTEN (pfile
, 4);
2786 if (!CPP_TRADITIONAL (pfile
))
2787 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2788 arg
->stringified_length
2789 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2791 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2793 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2794 /* Add 4 for two newline-space markers to prevent
2795 token concatenation. */
2796 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2799 /* We have an ordinary (expanded) occurrence of the arg.
2800 So compute its expansion, if we have not already. */
2801 if (args
[ap
->argno
].expand_length
< 0)
2803 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2804 cpp_expand_to_buffer (pfile
,
2805 ARG_BASE
+ args
[ap
->argno
].raw
,
2806 args
[ap
->argno
].raw_length
);
2808 args
[ap
->argno
].expand_length
2809 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2812 /* Add 4 for two newline-space markers to prevent
2813 token concatenation. */
2814 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2816 if (args
[ap
->argno
].use_count
< 10)
2817 args
[ap
->argno
].use_count
++;
2820 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2822 /* Generate in XBUF the complete expansion
2823 with arguments substituted in.
2824 TOTLEN is the total size generated so far.
2825 OFFSET is the index in the definition
2826 of where we are copying from. */
2827 offset
= totlen
= 0;
2828 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2829 last_ap
= ap
, ap
= ap
->next
)
2831 register struct argdata
*arg
= &args
[ap
->argno
];
2832 int count_before
= totlen
;
2834 /* Add chars to XBUF. */
2835 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2836 xbuf
[totlen
++] = exp
[offset
];
2838 /* If followed by an empty rest arg with concatenation,
2839 delete the last run of nonwhite chars. */
2840 if (rest_zero
&& totlen
> count_before
2841 && ((ap
->rest_args
&& ap
->raw_before
)
2842 || (last_ap
!= NULL
&& last_ap
->rest_args
2843 && last_ap
->raw_after
)))
2845 /* Delete final whitespace. */
2846 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2849 /* Delete the nonwhites before them. */
2850 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2854 if (ap
->stringify
!= 0)
2856 bcopy (ARG_BASE
+ arg
->stringified
,
2857 xbuf
+ totlen
, arg
->stringified_length
);
2858 totlen
+= arg
->stringified_length
;
2860 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2862 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2863 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2866 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2867 while (p1
!= l1
&& is_idchar
[*p1
])
2868 xbuf
[totlen
++] = *p1
++;
2869 /* Delete any no-reexpansion marker that follows
2870 an identifier at the beginning of the argument
2871 if the argument is concatenated with what precedes it. */
2872 if (p1
[0] == '@' && p1
[1] == '-')
2877 /* Arg is concatenated after: delete trailing whitespace,
2878 whitespace markers, and no-reexpansion markers. */
2881 if (is_space
[l1
[-1]]) l1
--;
2882 else if (l1
[-1] == '-')
2884 U_CHAR
*p2
= l1
- 1;
2885 /* If a `-' is preceded by an odd number of newlines then it
2886 and the last newline are a no-reexpansion marker. */
2887 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
2888 if ((l1
- 1 - p2
) & 1) {
2897 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2902 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2903 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2904 && !CPP_TRADITIONAL(pfile
)
2905 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2907 xbuf
[totlen
++] = '@';
2908 xbuf
[totlen
++] = ' ';
2911 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2912 totlen
+= arg
->expand_length
;
2914 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2915 && !CPP_TRADITIONAL(pfile
)
2916 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2918 xbuf
[totlen
++] = '@';
2919 xbuf
[totlen
++] = ' ';
2922 /* If a macro argument with newlines is used multiple times,
2923 then only expand the newlines once. This avoids creating
2924 output lines which don't correspond to any input line,
2925 which confuses gdb and gcov. */
2926 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2928 /* Don't bother doing change_newlines for subsequent
2932 = change_newlines (expanded
, arg
->expand_length
);
2936 if (totlen
> xbuf_len
)
2940 /* if there is anything left of the definition
2941 after handling the arg list, copy that in too. */
2943 for (i
= offset
; i
< defn
->length
; i
++)
2945 /* if we've reached the end of the macro */
2948 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2949 && last_ap
->raw_after
))
2950 xbuf
[totlen
++] = exp
[i
];
2958 pfile
->output_escapes
--;
2960 /* Now put the expansion on the input stack
2961 so our caller will commence reading from it. */
2962 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
2963 CPP_BUFFER (pfile
)->has_escapes
= 1;
2965 /* Pop the space we've used in the token_buffer for argument expansion. */
2966 CPP_SET_WRITTEN (pfile
, old_written
);
2968 /* Recursive macro use sometimes works traditionally.
2969 #define foo(x,y) bar (x (y,0), y)
2972 if (!CPP_TRADITIONAL (pfile
))
2973 hp
->type
= T_DISABLED
;
2977 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
2979 register U_CHAR
*xbuf
;
2983 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
2986 mbuf
->cleanup
= macro_cleanup
;
2989 /* The first chars of the expansion should be a "@ " added by
2990 collect_expansion. This is to prevent accidental token-pasting
2991 between the text preceding the macro invocation, and the macro
2994 We would like to avoid adding unneeded spaces (for the sake of
2995 tools that use cpp, such as imake). In some common cases we can
2996 tell that it is safe to omit the space.
2998 The character before the macro invocation cannot have been an
2999 idchar (or else it would have been pasted with the idchars of
3000 the macro name). Therefore, if the first non-space character
3001 of the expansion is an idchar, we do not need the extra space
3002 to prevent token pasting.
3004 Also, we don't need the extra space if the first char is '(',
3005 or some other (less common) characters. */
3007 if (xbuf
[0] == '@' && xbuf
[1] == ' '
3008 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
3009 || xbuf
[2] == '\"'))
3013 /* Like cpp_get_token, except that it does not read past end-of-line.
3014 Also, horizontal space is skipped, and macros are popped. */
3016 static enum cpp_token
3017 get_directive_token (pfile
)
3022 long old_written
= CPP_WRITTEN (pfile
);
3023 enum cpp_token token
;
3024 cpp_skip_hspace (pfile
);
3025 if (PEEKC () == '\n')
3027 token
= cpp_get_token (pfile
);
3031 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
3033 /* ... else fall though ... */
3034 case CPP_HSPACE
: case CPP_COMMENT
:
3035 CPP_SET_WRITTEN (pfile
, old_written
);
3043 /* Handle #include and #import.
3044 This function expects to see "fname" or <fname> on the input.
3046 The input is normally in part of the output_buffer following
3047 CPP_WRITTEN, and will get overwritten by output_line_command.
3048 I.e. in input file specification has been popped by handle_directive.
3052 do_include (pfile
, keyword
, unused1
, unused2
)
3054 struct directive
*keyword
;
3055 U_CHAR
*unused1
, *unused2
;
3057 int importing
= (keyword
->type
== T_IMPORT
);
3058 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3059 char *fname
; /* Dynamically allocated fname buffer */
3061 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3062 enum cpp_token token
;
3064 /* Chain of dirs to search */
3065 struct file_name_list
*search_start
= CPP_OPTIONS (pfile
)->include
;
3066 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3067 struct file_name_list
*searchptr
= 0;
3068 long old_written
= CPP_WRITTEN (pfile
);
3072 int f
; /* file number */
3074 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3081 f
= -1; /* JF we iz paranoid! */
3083 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3086 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
3088 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
3091 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
3092 && !CPP_OPTIONS (pfile
)->inhibit_warnings
3093 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
3095 pfile
->import_warning
= 1;
3096 cpp_warning (pfile
, "using `#import' is not recommended");
3097 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3098 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3099 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3100 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3101 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3102 fprintf (stderr
, " ... <real contents of file> ...\n");
3103 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3104 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3105 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3106 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3109 pfile
->parsing_include_directive
++;
3110 token
= get_directive_token (pfile
);
3111 pfile
->parsing_include_directive
--;
3113 if (token
== CPP_STRING
)
3115 /* FIXME - check no trailing garbage */
3116 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
3117 fend
= CPP_PWRITTEN (pfile
) - 1;
3118 if (fbeg
[-1] == '<')
3121 /* If -I-, start with the first -I dir after the -I-. */
3122 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3123 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3125 /* If -I- was specified, don't search current dir, only spec'd ones. */
3126 else if (! CPP_OPTIONS (pfile
)->ignore_srcdir
)
3128 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3129 /* We have "filename". Figure out directory this source
3130 file is coming from and put it on the front of the list. */
3132 for ( ; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3137 if ((nam
= fp
->nominal_fname
) != NULL
)
3139 /* Found a named file. Figure out dir of the file,
3140 and put it in front of the search list. */
3141 dsp
[0].next
= search_start
;
3144 ep
= rindex (nam
, '/');
3146 ep
= rindex (nam
, ']');
3147 if (ep
== NULL
) ep
= rindex (nam
, '>');
3148 if (ep
== NULL
) ep
= rindex (nam
, ':');
3149 if (ep
!= NULL
) ep
++;
3154 dsp
[0].fname
= (char *) alloca (n
+ 1);
3155 strncpy (dsp
[0].fname
, nam
, n
);
3156 dsp
[0].fname
[n
] = '\0';
3157 if (n
+ INCLUDE_LEN_FUDGE
> pfile
->max_include_len
)
3158 pfile
->max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3162 dsp
[0].fname
= 0; /* Current directory */
3164 dsp
[0].got_name_map
= 0;
3171 else if (token
== CPP_NAME
)
3174 * Support '#include xyz' like VAX-C to allow for easy use of all the
3175 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3176 * code from case '<' is repeated here) and generates a warning.
3179 "VAX-C-style include specification found, use '#include <filename.h>' !");
3181 /* If -I-, start with the first -I dir after the -I-. */
3182 if (CPP_OPTIONS (pfile
)->first_bracket_include
)
3183 search_start
= CPP_OPTIONS (pfile
)->first_bracket_include
;
3184 fbeg
= pfile
->token_buffer
+ old_written
;
3185 fend
= CPP_PWRITTEN (pfile
);
3191 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
3192 CPP_SET_WRITTEN (pfile
, old_written
);
3193 skip_rest_of_line (pfile
);
3199 token
= get_directive_token (pfile
);
3200 if (token
!= CPP_VSPACE
)
3202 cpp_error (pfile
, "junk at end of `#include'");
3203 while (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
3204 token
= get_directive_token (pfile
);
3207 /* For #include_next, skip in the search path
3208 past the dir in which the containing file was found. */
3211 cpp_buffer
*fp
= CPP_BUFFER (pfile
);
3212 for (; fp
!= CPP_NULL_BUFFER (pfile
); fp
= CPP_PREV_BUFFER (fp
))
3213 if (fp
->fname
!= NULL
)
3215 /* fp->dir is null if the containing file was specified with
3216 an absolute file name. In that case, don't skip anything. */
3217 if (fp
->dir
== SELF_DIR_DUMMY
)
3218 search_start
= CPP_OPTIONS (pfile
)->include
;
3220 search_start
= fp
->dir
->next
;
3225 CPP_SET_WRITTEN (pfile
, old_written
);
3231 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
3235 /* Allocate this permanently, because it gets stored in the definitions
3237 fname
= (char *) xmalloc (pfile
->max_include_len
+ flen
+ 4);
3238 /* + 2 above for slash and terminating null. */
3239 /* + 2 added for '.h' on VMS (to support '#include filename') */
3241 /* If specified file name is absolute, just open it. */
3244 strncpy (fname
, fbeg
, flen
);
3246 if (redundant_include_p (pfile
, fname
))
3249 f
= lookup_import (pfile
, fname
, NULL_PTR
);
3251 f
= open_include_file (pfile
, fname
, NULL_PTR
);
3253 return 0; /* Already included this file */
3255 /* Search directory path, trying to open the file.
3256 Copy each filename tried into FNAME. */
3258 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3259 if (searchptr
->fname
) {
3260 /* The empty string in a search path is ignored.
3261 This makes it possible to turn off entirely
3262 a standard piece of the list. */
3263 if (searchptr
->fname
[0] == 0)
3265 strcpy (fname
, searchptr
->fname
);
3266 strcat (fname
, "/");
3267 fname
[strlen (fname
) + flen
] = 0;
3271 strncat (fname
, fbeg
, flen
);
3273 /* Change this 1/2 Unix 1/2 VMS file specification into a
3274 full VMS file specification */
3275 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3276 /* Fix up the filename */
3277 hack_vms_include_specification (fname
);
3279 /* This is a normal VMS filespec, so use it unchanged. */
3280 strncpy (fname
, fbeg
, flen
);
3282 /* if it's '#include filename', add the missing .h */
3283 if (index(fname
,'.')==NULL
) {
3284 strcat (fname
, ".h");
3288 /* ??? There are currently 3 separate mechanisms for avoiding processing
3289 of redundant include files: #import, #pragma once, and
3290 redundant_include_p. It would be nice if they were unified. */
3291 if (redundant_include_p (pfile
, fname
))
3294 f
= lookup_import (pfile
, fname
, searchptr
);
3296 f
= open_include_file (pfile
, fname
, searchptr
);
3298 return 0; /* Already included this file */
3300 else if (f
== -1 && errno
== EACCES
)
3301 cpp_warning (pfile
, "Header file %s exists, but is not readable",
3311 /* A file that was not found. */
3312 strncpy (fname
, fbeg
, flen
);
3314 /* If generating dependencies and -MG was specified, we assume missing
3315 files are leaf files, living in the same directory as the source file
3316 or other similar place; these missing files may be generated from
3317 other files and may not exist yet (eg: y.tab.h). */
3319 if (CPP_OPTIONS(pfile
)->print_deps_missing_files
3320 && CPP_PRINT_DEPS (pfile
)
3321 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3323 /* If it was requested as a system header file,
3324 then assume it belongs in the first place to look for such. */
3327 for (searchptr
= search_start
; searchptr
;
3328 searchptr
= searchptr
->next
)
3330 if (searchptr
->fname
)
3334 if (searchptr
->fname
[0] == 0)
3336 p
= (char *) alloca (strlen (searchptr
->fname
)
3337 + strlen (fname
) + 2);
3338 strcpy (p
, searchptr
->fname
);
3341 deps_output (pfile
, p
, ' ');
3348 /* Otherwise, omit the directory, as if the file existed
3349 in the directory with the source. */
3350 deps_output (pfile
, fname
, ' ');
3353 /* If -M was specified, and this header file won't be added to the
3354 dependency list, then don't count this as an error, because we can
3355 still produce correct output. Otherwise, we can't produce correct
3356 output, because there may be dependencies we need inside the missing
3357 file, and we don't know what directory this missing file exists in.*/
3358 else if (CPP_PRINT_DEPS (pfile
)
3359 && (CPP_PRINT_DEPS (pfile
)
3360 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3361 cpp_warning (pfile
, "No include path in which to find %s", fname
);
3362 else if (search_start
)
3363 cpp_error_from_errno (pfile
, fname
);
3365 cpp_error (pfile
, "No include path in which to find %s", fname
);
3368 /* Check to see if this include file is a once-only include file.
3371 struct file_name_list
*ptr
;
3373 for (ptr
= pfile
->dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3374 if (!strcmp (ptr
->fname
, fname
)) {
3376 return 0; /* This file was once'd. */
3380 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3381 if (!strcmp (ptr
->fname
, fname
))
3382 break; /* This file was included before. */
3386 /* This is the first time for this file. */
3387 /* Add it to list of files included. */
3389 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3390 ptr
->control_macro
= 0;
3391 ptr
->c_system_include_path
= 0;
3392 ptr
->next
= pfile
->all_include_files
;
3393 pfile
->all_include_files
= ptr
;
3394 ptr
->fname
= savestring (fname
);
3395 ptr
->got_name_map
= 0;
3397 /* For -M, add this file to the dependencies. */
3398 if (CPP_PRINT_DEPS (pfile
)
3399 > (angle_brackets
|| (pfile
->system_include_depth
> 0)))
3400 deps_output (pfile
, fname
, ' ');
3403 /* Handle -H option. */
3404 if (CPP_OPTIONS(pfile
)->print_include_names
)
3406 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
3407 while ((buf
= CPP_PREV_BUFFER (buf
)) != CPP_NULL_BUFFER (pfile
))
3409 fprintf (stderr
, "%s\n", fname
);
3413 pfile
->system_include_depth
++;
3415 /* Actually process the file. */
3417 /* Record file on "seen" list for #import. */
3418 add_import (pfile
, f
, fname
);
3420 pcftry
= (char *) alloca (strlen (fname
) + 30);
3432 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3434 pcf
= open (pcftry
, O_RDONLY
, 0666);
3440 if (bcmp ((char *) &stat_f
.st_ino
, (char *) &s
.st_ino
,
3442 || stat_f
.st_dev
!= s
.st_dev
)
3444 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3445 /* Don't need it any more. */
3450 /* Don't need it at all. */
3455 } while (pcf
!= -1 && !pcfbuf
);
3459 /* Actually process the file */
3460 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3462 if (finclude (pfile
, f
, fname
, is_system_include (pfile
, fname
),
3463 searchptr
!= dsp
? searchptr
: SELF_DIR_DUMMY
))
3465 output_line_command (pfile
, 0, enter_file
);
3466 pfile
->only_seen_white
= 2;
3470 pfile
->system_include_depth
--;
3475 /* Return nonzero if there is no need to include file NAME
3476 because it has already been included and it contains a conditional
3477 to make a repeated include do nothing. */
3480 redundant_include_p (pfile
, name
)
3484 struct file_name_list
*l
= pfile
->all_include_files
;
3485 for (; l
; l
= l
->next
)
3486 if (! strcmp (name
, l
->fname
)
3488 && cpp_lookup (pfile
, l
->control_macro
, -1, -1))
3493 /* Return nonzero if the given FILENAME is an absolute pathname which
3494 designates a file within one of the known "system" include file
3495 directories. We assume here that if the given FILENAME looks like
3496 it is the name of a file which resides either directly in a "system"
3497 include file directory, or within any subdirectory thereof, then the
3498 given file must be a "system" include file. This function tells us
3499 if we should suppress pedantic errors/warnings for the given FILENAME.
3501 The value is 2 if the file is a C-language system header file
3502 for which C++ should (on most systems) assume `extern "C"'. */
3505 is_system_include (pfile
, filename
)
3507 register char *filename
;
3509 struct file_name_list
*searchptr
;
3511 for (searchptr
= CPP_OPTIONS (pfile
)->first_system_include
; searchptr
;
3512 searchptr
= searchptr
->next
)
3513 if (searchptr
->fname
) {
3514 register char *sys_dir
= searchptr
->fname
;
3515 register unsigned length
= strlen (sys_dir
);
3517 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
3519 if (searchptr
->c_system_include_path
)
3530 * Install a name in the assertion hash table.
3532 * If LEN is >= 0, it is the length of the name.
3533 * Otherwise, compute the length by scanning the entire name.
3535 * If HASH is >= 0, it is the precomputed hash code.
3536 * Otherwise, compute the hash code.
3539 static ASSERTION_HASHNODE
*
3540 assertion_install (pfile
, name
, len
, hash
)
3546 register ASSERTION_HASHNODE
*hp
;
3547 register int i
, bucket
;
3548 register U_CHAR
*p
, *q
;
3550 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
3551 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
3553 hp
->bucket_hdr
= &pfile
->assertion_hashtab
[bucket
];
3554 hp
->next
= pfile
->assertion_hashtab
[bucket
];
3555 pfile
->assertion_hashtab
[bucket
] = hp
;
3557 if (hp
->next
!= NULL
)
3558 hp
->next
->prev
= hp
;
3561 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
3564 for (i
= 0; i
< len
; i
++)
3570 * find the most recent hash node for name name (ending with first
3571 * non-identifier char) installed by install
3573 * If LEN is >= 0, it is the length of the name.
3574 * Otherwise, compute the length by scanning the entire name.
3576 * If HASH is >= 0, it is the precomputed hash code.
3577 * Otherwise, compute the hash code.
3580 static ASSERTION_HASHNODE
*
3581 assertion_lookup (pfile
, name
, len
, hash
)
3587 register ASSERTION_HASHNODE
*bucket
;
3589 bucket
= pfile
->assertion_hashtab
[hash
];
3591 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
3593 bucket
= bucket
->next
;
3599 delete_assertion (hp
)
3600 ASSERTION_HASHNODE
*hp
;
3602 struct tokenlist_list
*tail
;
3603 if (hp
->prev
!= NULL
)
3604 hp
->prev
->next
= hp
->next
;
3605 if (hp
->next
!= NULL
)
3606 hp
->next
->prev
= hp
->prev
;
3608 for (tail
= hp
->value
; tail
; )
3610 struct tokenlist_list
*next
= tail
->next
;
3611 free_token_list (tail
->tokens
);
3616 /* Make sure that the bucket chain header that
3617 the deleted guy was on points to the right thing afterwards. */
3618 if (hp
== *hp
->bucket_hdr
)
3619 *hp
->bucket_hdr
= hp
->next
;
3624 /* Convert a character string literal into a nul-terminated string.
3625 The input string is [IN ... LIMIT).
3626 The result is placed in RESULT. RESULT can be the same as IN.
3627 The value returned in the end of the string written to RESULT,
3628 or NULL on error. */
3631 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3633 register U_CHAR
*result
, *in
, *limit
;
3653 char *bpc
= (char *) in
;
3654 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
);
3655 in
= (U_CHAR
*) bpc
;
3657 *result
++ = (U_CHAR
)c
;
3660 /* else fall through */
3670 * interpret #line command. Remembers previously seen fnames
3671 * in its very own hash table.
3673 #define FNAME_HASHSIZE 37
3676 do_line (pfile
, keyword
)
3678 struct directive
*keyword
;
3680 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3682 long old_written
= CPP_WRITTEN (pfile
);
3683 enum file_change_code file_change
= same_file
;
3684 enum cpp_token token
;
3686 token
= get_directive_token (pfile
);
3688 if (token
!= CPP_NUMBER
3689 || !isdigit(pfile
->token_buffer
[old_written
]))
3691 cpp_error (pfile
, "invalid format `#line' command");
3692 goto bad_line_directive
;
3695 /* The Newline at the end of this line remains to be processed.
3696 To put the next line at the specified line number,
3697 we must store a line number now that is one less. */
3698 new_lineno
= atoi ((char *)(pfile
->token_buffer
+ old_written
)) - 1;
3699 CPP_SET_WRITTEN (pfile
, old_written
);
3701 /* NEW_LINENO is one less than the actual line number here. */
3702 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3703 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3705 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3706 if (PEEKC() && !is_space
[PEEKC()]) {
3707 cpp_error (pfile
, "invalid format `#line' command");
3708 goto bad_line_directive
;
3712 token
= get_directive_token (pfile
);
3714 if (token
== CPP_STRING
) {
3715 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3717 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3718 HASHNODE
*hp
, **hash_bucket
;
3723 /* Turn the file name, which is a character string literal,
3724 into a null-terminated string. Do this in place. */
3725 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3726 if (end_name
== NULL
)
3728 cpp_error (pfile
, "invalid format `#line' command");
3729 goto bad_line_directive
;
3732 fname_length
= end_name
- fname
;
3734 num_start
= CPP_WRITTEN (pfile
);
3735 token
= get_directive_token (pfile
);
3736 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3737 p
= pfile
->token_buffer
+ num_start
;
3738 if (CPP_PEDANTIC (pfile
))
3739 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3741 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3743 cpp_error (pfile
, "invalid format `#line' command");
3744 goto bad_line_directive
;
3747 file_change
= enter_file
;
3749 file_change
= leave_file
;
3751 ip
->system_header_p
= 1;
3752 else /* if (*p == 4) */
3753 ip
->system_header_p
= 2;
3755 CPP_SET_WRITTEN (pfile
, num_start
);
3756 token
= get_directive_token (pfile
);
3757 p
= pfile
->token_buffer
+ num_start
;
3758 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3759 ip
->system_header_p
= *p
== 3 ? 1 : 2;
3760 token
= get_directive_token (pfile
);
3762 if (token
!= CPP_VSPACE
) {
3763 cpp_error (pfile
, "invalid format `#line' command");
3764 goto bad_line_directive
;
3768 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3769 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3770 if (hp
->length
== fname_length
3771 && strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3772 ip
->nominal_fname
= hp
->value
.cpval
;
3776 /* Didn't find it; cons up a new one. */
3777 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3778 hp
->next
= *hash_bucket
;
3781 hp
->length
= fname_length
;
3782 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3783 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3786 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3787 cpp_error (pfile
, "invalid format `#line' command");
3788 goto bad_line_directive
;
3791 ip
->lineno
= new_lineno
;
3793 skip_rest_of_line (pfile
);
3794 CPP_SET_WRITTEN (pfile
, old_written
);
3795 output_line_command (pfile
, 0, file_change
);
3800 * remove the definition of a symbol from the symbol table.
3801 * according to un*x /lib/cpp, it is not an error to undef
3802 * something that has no definitions, so it isn't one here either.
3806 do_undef (pfile
, keyword
, buf
, limit
)
3808 struct directive
*keyword
;
3809 U_CHAR
*buf
, *limit
;
3813 U_CHAR
*orig_buf
= buf
;
3816 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3817 if (pcp_outfile
&& keyword
)
3818 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3821 SKIP_WHITE_SPACE (buf
);
3822 sym_length
= check_macro_name (pfile
, buf
, "macro");
3824 while ((hp
= cpp_lookup (pfile
, buf
, sym_length
, -1)) != NULL
)
3826 /* If we are generating additional info for debugging (with -g) we
3827 need to pass through all effective #undef commands. */
3828 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3829 pass_thru_directive (orig_buf
, limit
, pfile
, keyword
);
3830 if (hp
->type
!= T_MACRO
)
3831 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3835 if (CPP_PEDANTIC (pfile
)) {
3837 SKIP_WHITE_SPACE (buf
);
3839 cpp_pedwarn (pfile
, "garbage after `#undef' directive");
3845 * Report an error detected by the program we are processing.
3846 * Use the text of the line in the error message.
3847 * (We use error because it prints the filename & line#.)
3851 do_error (pfile
, keyword
, buf
, limit
)
3853 struct directive
*keyword
;
3854 U_CHAR
*buf
, *limit
;
3856 int length
= limit
- buf
;
3857 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3858 bcopy (buf
, copy
, length
);
3860 SKIP_WHITE_SPACE (copy
);
3861 cpp_error (pfile
, "#error %s", copy
);
3866 * Report a warning detected by the program we are processing.
3867 * Use the text of the line in the warning message, then continue.
3868 * (We use error because it prints the filename & line#.)
3872 do_warning (pfile
, keyword
, buf
, limit
)
3874 struct directive
*keyword
;
3875 U_CHAR
*buf
, *limit
;
3877 int length
= limit
- buf
;
3878 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
3879 bcopy (buf
, copy
, length
);
3881 SKIP_WHITE_SPACE (copy
);
3882 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3883 if -pedantic-errors is given, #warning should cause an error. */
3884 cpp_pedwarn (pfile
, "#warning %s", copy
);
3888 /* Remember the name of the current file being read from so that we can
3889 avoid ever including it again. */
3895 cpp_buffer
*ip
= NULL
;
3896 struct file_name_list
*new;
3898 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3900 if (ip
== CPP_NULL_BUFFER (pfile
))
3902 if (ip
->fname
!= NULL
)
3907 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3908 new->next
= pfile
->dont_repeat_files
;
3909 pfile
->dont_repeat_files
= new;
3910 new->fname
= savestring (ip
->fname
);
3911 new->control_macro
= 0;
3912 new->got_name_map
= 0;
3913 new->c_system_include_path
= 0;
3918 /* Report program identification. */
3921 do_ident (pfile
, keyword
, buf
, limit
)
3923 struct directive
*keyword
;
3924 U_CHAR
*buf
, *limit
;
3926 /* long old_written = CPP_WRITTEN (pfile);*/
3928 /* Allow #ident in system headers, since that's not user's fault. */
3929 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3930 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3932 /* Leave rest of line to be read by later calls to cpp_get_token. */
3937 /* #pragma and its argument line have already been copied to the output file.
3938 Just check for some recognized pragmas that need validation here. */
3941 do_pragma (pfile
, keyword
, buf
, limit
)
3943 struct directive
*keyword
;
3944 U_CHAR
*buf
, *limit
;
3946 while (*buf
== ' ' || *buf
== '\t')
3948 if (!strncmp (buf
, "once", 4)) {
3949 /* Allow #pragma once in system headers, since that's not the user's
3951 if (!CPP_BUFFER (pfile
)->system_header_p
)
3952 cpp_warning (pfile
, "`#pragma once' is obsolete");
3956 if (!strncmp (buf
, "implementation", 14)) {
3957 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3958 been included yet. */
3959 struct file_name_list
*ptr
;
3960 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
3962 SKIP_WHITE_SPACE (p
);
3963 if (*p
== '\n' || *p
!= '\"')
3967 p
= (U_CHAR
*) index (fname
, '\"');
3968 fname_len
= p
!= NULL
? p
- fname
: strlen (fname
);
3970 for (ptr
= pfile
->all_include_files
; ptr
; ptr
= ptr
->next
) {
3971 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
3972 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
3973 if (inc_fname
&& !strncmp (inc_fname
, fname
, fname_len
))
3975 "`#pragma implementation' for `%s' appears after file is included",
3984 /* This was a fun hack, but #pragma seems to start to be useful.
3985 By failing to recognize it, we pass it through unchanged to cc1. */
3988 * the behavior of the #pragma directive is implementation defined.
3989 * this implementation defines it as follows.
3996 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
3999 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
4001 execl ("/usr/games/hack", "#pragma", 0);
4002 execl ("/usr/games/rogue", "#pragma", 0);
4003 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4004 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4006 fatal ("You are in a maze of twisty compiler features, all different");
4010 #ifdef SCCS_DIRECTIVE
4011 /* Just ignore #sccs, on systems where we define it at all. */
4014 do_sccs (pfile
, keyword
, buf
, limit
)
4016 struct directive
*keyword
;
4017 U_CHAR
*buf
, *limit
;
4019 if (CPP_PEDANTIC (pfile
))
4020 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
4026 * handle #if command by
4027 * 1) inserting special `defined' keyword into the hash table
4028 * that gets turned into 0 or 1 by special_symbol (thus,
4029 * if the luser has a symbol called `defined' already, it won't
4030 * work inside the #if command)
4031 * 2) rescan the input into a temporary output buffer
4032 * 3) pass the output buffer to the yacc parser and collect a value
4033 * 4) clean up the mess left from steps 1 and 2.
4034 * 5) call conditional_skip to skip til the next #endif (etc.),
4035 * or not, depending on the value from step 3.
4039 do_if (pfile
, keyword
, buf
, limit
)
4041 struct directive
*keyword
;
4042 U_CHAR
*buf
, *limit
;
4044 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4045 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
4050 * handle a #elif directive by not changing if_stack either.
4051 * see the comment above do_else.
4055 do_elif (pfile
, keyword
, buf
, limit
)
4057 struct directive
*keyword
;
4058 U_CHAR
*buf
, *limit
;
4060 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4061 cpp_error (pfile
, "`#elif' not within a conditional");
4064 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4065 cpp_error (pfile
, "`#elif' after `#else'");
4067 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4069 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
4070 && strcmp (pfile
->if_stack
->fname
,
4071 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
4072 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4073 fprintf (stderr
, ")\n");
4075 pfile
->if_stack
->type
= T_ELIF
;
4078 if (pfile
->if_stack
->if_succeeded
)
4079 skip_if_group (pfile
, 0);
4081 HOST_WIDE_INT value
= eval_if_expression (pfile
, buf
, limit
- buf
);
4083 skip_if_group (pfile
, 0);
4085 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4086 output_line_command (pfile
, 1, same_file
);
4093 * evaluate a #if expression in BUF, of length LENGTH,
4094 * then parse the result as a C expression and return the value as an int.
4097 static HOST_WIDE_INT
4098 eval_if_expression (pfile
, buf
, length
)
4103 HASHNODE
*save_defined
;
4104 HOST_WIDE_INT value
;
4105 long old_written
= CPP_WRITTEN (pfile
);
4107 save_defined
= install ((U_CHAR
*)"defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
4108 pfile
->pcp_inside_if
= 1;
4110 value
= cpp_parse_expr (pfile
);
4111 pfile
->pcp_inside_if
= 0;
4112 delete_macro (save_defined
); /* clean up special symbol */
4114 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4120 * routine to handle ifdef/ifndef. Try to look up the symbol,
4121 * then do or don't skip to the #endif/#else/#elif depending
4122 * on what directive is actually being processed.
4126 do_xifdef (pfile
, keyword
, unused1
, unused2
)
4128 struct directive
*keyword
;
4129 U_CHAR
*unused1
, *unused2
;
4132 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4135 enum cpp_token token
;
4136 int start_of_file
= 0;
4137 U_CHAR
*control_macro
= 0;
4138 int old_written
= CPP_WRITTEN (pfile
);
4140 /* Detect a #ifndef at start of file (not counting comments). */
4141 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
4142 start_of_file
= pfile
->only_seen_white
== 2;
4144 pfile
->no_macro_expand
++;
4145 token
= get_directive_token (pfile
);
4146 pfile
->no_macro_expand
--;
4148 ident
= pfile
->token_buffer
+ old_written
;
4149 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4150 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
4152 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
4154 skip
= (keyword
->type
== T_IFDEF
);
4155 if (! CPP_TRADITIONAL (pfile
))
4156 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
4158 else if (token
== CPP_NAME
)
4160 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
4161 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
4162 if (start_of_file
&& !skip
)
4164 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
4165 bcopy (ident
, control_macro
, ident_length
+ 1);
4170 skip
= (keyword
->type
== T_IFDEF
);
4171 if (! CPP_TRADITIONAL (pfile
))
4172 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
4175 if (!CPP_TRADITIONAL (pfile
))
4177 cpp_skip_hspace (pfile
);
4179 if (c
!= EOF
&& c
!= '\n')
4180 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
4182 skip_rest_of_line (pfile
);
4186 /* Output a precondition for this macro. */
4187 if (hp
&& hp
->value
.defn
->predefined
)
4188 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4191 fprintf (pcp_outfile
, "#undef ");
4192 while (is_idchar
[*cp
]) /* Ick! */
4193 fputc (*cp
++, pcp_outfile
);
4194 putc ('\n', pcp_outfile
);
4198 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
4202 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4203 If this is a #ifndef starting at the beginning of a file,
4204 CONTROL_MACRO is the macro name tested by the #ifndef.
4205 Otherwise, CONTROL_MACRO is 0. */
4208 conditional_skip (pfile
, skip
, type
, control_macro
)
4211 enum node_type type
;
4212 U_CHAR
*control_macro
;
4214 IF_STACK_FRAME
*temp
;
4216 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4217 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
4219 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
4221 temp
->next
= pfile
->if_stack
;
4222 temp
->control_macro
= control_macro
;
4223 pfile
->if_stack
= temp
;
4225 pfile
->if_stack
->type
= type
;
4228 skip_if_group (pfile
, 0);
4231 ++pfile
->if_stack
->if_succeeded
;
4232 output_line_command (pfile
, 1, same_file
);
4237 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4238 * leaves input ptr at the sharp sign found.
4239 * If ANY is nonzero, return at next directive of any sort.
4243 skip_if_group (pfile
, any
)
4248 struct directive
*kt
;
4249 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
4251 U_CHAR
*beg_of_line
= bp
;
4253 register int ident_length
;
4255 struct parse_marker line_start_mark
;
4257 parse_set_mark (&line_start_mark
, pfile
);
4259 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4260 static char failed
[] = "#failed\n";
4261 CPP_PUTS (pfile
, failed
, sizeof(failed
)-1);
4263 output_line_command (pfile
, 1, same_file
);
4267 if (CPP_OPTIONS (pfile
)->output_conditionals
)
4269 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4270 U_CHAR
*start_line
= pbuf
->buf
+ line_start_mark
.position
;
4271 CPP_PUTS (pfile
, start_line
, pbuf
->cur
- start_line
);
4273 parse_move_mark (&line_start_mark
, pfile
);
4274 if (!CPP_TRADITIONAL (pfile
))
4275 cpp_skip_hspace (pfile
);
4279 int old_written
= CPP_WRITTEN (pfile
);
4280 cpp_skip_hspace (pfile
);
4282 parse_name (pfile
, GETC());
4283 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
4284 ident
= pfile
->token_buffer
+ old_written
;
4285 pfile
->limit
= ident
;
4287 if (ident_length
== 0)
4288 goto not_a_directive
;
4290 /* Handle # followed by a line number. */
4292 /* Avoid error for `###' and similar cases unless -pedantic. */
4295 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
4297 IF_STACK_FRAME
*temp
;
4298 if (ident_length
== kt
->length
4299 && strncmp (ident
, kt
->name
, kt
->length
) == 0)
4301 /* If we are asked to return on next directive, do so now. */
4311 = (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
4312 temp
->next
= pfile
->if_stack
;
4313 pfile
->if_stack
= temp
;
4315 temp
->lineno
= CPP_BUFFER(pfile
)->lineno
;
4317 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
4318 temp
->type
= kt
->type
;
4322 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= save_if_stack
)
4323 validate_else (pfile
,
4324 kt
->type
== T_ELSE
? "#else" : "#endif");
4326 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4329 "`#%s' not within a conditional", kt
->name
);
4332 else if (pfile
->if_stack
== save_if_stack
)
4333 goto done
; /* found what we came for */
4335 if (kt
->type
!= T_ENDIF
)
4337 if (pfile
->if_stack
->type
== T_ELSE
)
4338 cpp_error (pfile
, "`#else' or `#elif' after `#else'");
4339 pfile
->if_stack
->type
= kt
->type
;
4343 temp
= pfile
->if_stack
;
4344 pfile
->if_stack
= temp
->next
;
4351 /* Don't let erroneous code go by. */
4352 if (kt
->length
< 0 && !CPP_OPTIONS (pfile
)->lang_asm
4353 && CPP_PEDANTIC (pfile
))
4354 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
4358 /* We're in the middle of a line. Skip the rest of it. */
4365 case '/': /* possible comment */
4366 c
= skip_comment (pfile
, NULL
);
4373 old
= CPP_WRITTEN (pfile
);
4374 cpp_get_token (pfile
);
4375 CPP_SET_WRITTEN (pfile
, old
);
4378 /* Char after backslash loses its special meaning. */
4379 if (PEEKC() == '\n')
4389 if (CPP_OPTIONS (pfile
)->output_conditionals
) {
4390 static char end_failed
[] = "#endfailed\n";
4391 CPP_PUTS (pfile
, end_failed
, sizeof(end_failed
)-1);
4394 pfile
->only_seen_white
= 1;
4395 parse_goto_mark (&line_start_mark
, pfile
);
4396 parse_clear_mark (&line_start_mark
);
4400 * handle a #else directive. Do this by just continuing processing
4401 * without changing if_stack ; this is so that the error message
4402 * for missing #endif's etc. will point to the original #if. It
4403 * is possible that something different would be better.
4407 do_else (pfile
, keyword
, buf
, limit
)
4409 struct directive
*keyword
;
4410 U_CHAR
*buf
, *limit
;
4412 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
4414 if (CPP_PEDANTIC (pfile
))
4415 validate_else (pfile
, "#else");
4416 skip_rest_of_line (pfile
);
4418 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
4419 cpp_error (pfile
, "`#else' not within a conditional");
4422 /* #ifndef can't have its special treatment for containing the whole file
4423 if it has a #else clause. */
4424 pfile
->if_stack
->control_macro
= 0;
4426 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
4427 cpp_error (pfile
, "`#else' after `#else'");
4428 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
4429 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
4430 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
4431 fprintf (stderr
, ")\n");
4433 pfile
->if_stack
->type
= T_ELSE
;
4436 if (pfile
->if_stack
->if_succeeded
)
4437 skip_if_group (pfile
, 0);
4439 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
4440 output_line_command (pfile
, 1, same_file
);
4446 * unstack after #endif command
4450 do_endif (pfile
, keyword
, buf
, limit
)
4452 struct directive
*keyword
;
4453 U_CHAR
*buf
, *limit
;
4455 if (CPP_PEDANTIC (pfile
))
4456 validate_else (pfile
, "#endif");
4457 skip_rest_of_line (pfile
);
4459 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
4460 cpp_error (pfile
, "unbalanced `#endif'");
4463 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
4464 pfile
->if_stack
= temp
->next
;
4465 if (temp
->control_macro
!= 0)
4467 /* This #endif matched a #ifndef at the start of the file.
4468 See if it is at the end of the file. */
4469 struct parse_marker start_mark
;
4472 parse_set_mark (&start_mark
, pfile
);
4476 cpp_skip_hspace (pfile
);
4481 parse_goto_mark (&start_mark
, pfile
);
4482 parse_clear_mark (&start_mark
);
4486 /* If we get here, this #endif ends a #ifndef
4487 that contains all of the file (aside from whitespace).
4488 Arrange not to include the file again
4489 if the macro that was tested is defined.
4491 Do not do this for the top-level file in a -include or any
4492 file in a -imacros. */
4496 && ! (indepth
== 1 && pfile
->no_record_file
)
4497 && ! (pfile
->no_record_file
&& no_output
))
4500 struct file_name_list
*ifile
= pfile
->all_include_files
;
4502 for ( ; ifile
!= NULL
; ifile
= ifile
->next
)
4504 if (!strcmp (ifile
->fname
, CPP_BUFFER (pfile
)->fname
))
4506 ifile
->control_macro
= temp
->control_macro
;
4514 output_line_command (pfile
, 1, same_file
);
4519 /* When an #else or #endif is found while skipping failed conditional,
4520 if -pedantic was specified, this is called to warn about text after
4521 the command name. P points to the first char after the command name. */
4524 validate_else (pfile
, directive
)
4529 cpp_skip_hspace (pfile
);
4531 if (c
!= EOF
&& c
!= '\n')
4533 "text following `%s' violates ANSI standard", directive
);
4536 /* Get the next token, and add it to the text in pfile->token_buffer.
4537 Return the kind of token we got. */
4540 cpp_get_token (pfile
)
4543 register int c
, c2
, c3
;
4545 long start_line
, start_column
;
4546 enum cpp_token token
;
4547 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4548 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
4554 if (CPP_BUFFER (pfile
)->seen_eof
)
4556 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4563 cpp_buffer
*next_buf
4564 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4565 CPP_BUFFER (pfile
)->seen_eof
= 1;
4566 if (CPP_BUFFER (pfile
)->nominal_fname
4567 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4569 /* We're about to return from an #include file.
4570 Emit #line information now (as part of the CPP_POP) result.
4571 But the #line refers to the file we will pop to. */
4572 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4573 CPP_BUFFER (pfile
) = next_buf
;
4574 pfile
->input_stack_listing_current
= 0;
4575 output_line_command (pfile
, 0, leave_file
);
4576 CPP_BUFFER (pfile
) = cur_buffer
;
4586 struct parse_marker start_mark
;
4588 if (PEEKC () == '=')
4590 if (opts
->put_out_comments
)
4591 parse_set_mark (&start_mark
, pfile
);
4593 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4594 &start_line
, &start_column
);
4595 c
= skip_comment (pfile
, &newlines
);
4596 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4597 parse_clear_mark (&start_mark
);
4602 cpp_error_with_line (pfile
, start_line
, start_column
,
4603 "unterminated comment");
4606 c
= '/'; /* Initial letter of comment. */
4608 /* Comments are equivalent to spaces.
4609 For -traditional, a comment is equivalent to nothing. */
4610 if (opts
->put_out_comments
)
4612 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4613 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4614 int len
= pbuf
->cur
- start
;
4615 CPP_RESERVE(pfile
, 1 + len
);
4616 CPP_PUTC_Q (pfile
, c
);
4617 CPP_PUTS_Q (pfile
, start
, len
);
4618 pfile
->lineno
+= newlines
;
4619 parse_clear_mark (&start_mark
);
4622 else if (CPP_TRADITIONAL (pfile
))
4629 /* This may not work if cpp_get_token is called recursively,
4630 since many places look for horizontal space. */
4633 /* Copy the newlines into the output buffer, in order to
4634 avoid the pain of a #line every time a multiline comment
4636 CPP_RESERVE(pfile
, newlines
);
4637 while (--newlines
>= 0)
4639 CPP_PUTC_Q (pfile
, '\n');
4645 CPP_RESERVE(pfile
, 1);
4646 CPP_PUTC_Q (pfile
, ' ');
4650 if (opts
->for_lint
) {
4653 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4655 if (lintcmd
!= NULL
) {
4656 /* I believe it is always safe to emit this newline: */
4658 bcopy ("#pragma lint ", (char *) obp
, 13);
4660 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4665 bcopy (argbp
, (char *) obp
, arglen
);
4669 /* OK, now bring us back to the state we were in before we entered
4670 this branch. We need #line because the newline for the pragma
4671 could mess things up. */
4672 output_line_command (pfile
, 0, same_file
);
4673 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4681 /* If this is expanding a macro definition, don't recognize
4682 preprocessor directives. */
4685 /* If this is expand_into_temp_buffer, recognize them
4686 only after an actual newline at this level,
4687 not at the beginning of the input level. */
4688 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4694 if (!pfile
->only_seen_white
)
4696 if (handle_directive (pfile
))
4697 return CPP_DIRECTIVE
;
4698 pfile
->only_seen_white
= 0;
4703 /* A single quoted string is treated like a double -- some
4704 programs (e.g., troff) are perverse this way */
4705 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4706 &start_line
, &start_column
);
4707 old_written
= CPP_WRITTEN (pfile
);
4709 CPP_PUTC (pfile
, c
);
4715 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4717 /* try harder: this string crosses a macro expansion
4718 boundary. This can happen naturally if -traditional.
4719 Otherwise, only -D can make a macro with an unmatched
4721 cpp_buffer
*next_buf
4722 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4723 (*CPP_BUFFER (pfile
)->cleanup
)
4724 (CPP_BUFFER (pfile
), pfile
);
4725 CPP_BUFFER (pfile
) = next_buf
;
4728 if (!CPP_TRADITIONAL (pfile
))
4730 cpp_error_with_line (pfile
, start_line
, start_column
,
4731 "unterminated string or character constant");
4732 if (pfile
->multiline_string_line
!= start_line
4733 && pfile
->multiline_string_line
!= 0)
4734 cpp_error_with_line (pfile
,
4735 pfile
->multiline_string_line
, -1,
4736 "possible real start of unterminated constant");
4737 pfile
->multiline_string_line
= 0;
4741 CPP_PUTC (pfile
, cc
);
4745 /* Traditionally, end of line ends a string constant with
4746 no error. So exit the loop and record the new line. */
4747 if (CPP_TRADITIONAL (pfile
))
4751 cpp_error_with_line (pfile
, start_line
, start_column
,
4752 "unterminated character constant");
4755 if (CPP_PEDANTIC (pfile
)
4756 && pfile
->multiline_string_line
== 0)
4758 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4759 "string constant runs past end of line");
4761 if (pfile
->multiline_string_line
== 0)
4762 pfile
->multiline_string_line
= start_line
;
4769 /* Backslash newline is replaced by nothing at all. */
4770 CPP_ADJUST_WRITTEN (pfile
, -1);
4775 /* ANSI stupidly requires that in \\ the second \
4776 is *not* prevented from combining with a newline. */
4779 CPP_PUTC (pfile
, cc
);
4791 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4792 CPP_PWRITTEN (pfile
));
4793 pfile
->only_seen_white
= 0;
4794 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4797 if (!opts
->dollars_in_ident
)
4802 if (opts
->cplusplus
&& PEEKC () == ':')
4811 if (c2
== c
|| c2
== '=')
4821 if (PEEKC () == '=')
4828 if (c2
== '-' && opts
->chill
)
4830 /* Chill style comment */
4831 if (opts
->put_out_comments
)
4832 parse_set_mark (&start_mark
, pfile
);
4833 FORWARD(1); /* Skip second '-'. */
4841 /* Don't consider final '\n' to be part of comment. */
4847 goto return_comment
;
4849 if (c2
== '-' || c2
== '=' || c2
== '>')
4854 if (pfile
->parsing_include_directive
)
4858 CPP_PUTC (pfile
, c
);
4863 if (c
== '\n' || c
== EOF
)
4866 "missing '>' in `#include <FILENAME>'");
4872 /* else fall through */
4881 CPP_RESERVE (pfile
, 4);
4882 CPP_PUTC (pfile
, c
);
4883 CPP_PUTC (pfile
, c2
);
4887 CPP_PUTC_Q (pfile
, GETC ());
4888 CPP_NUL_TERMINATE_Q (pfile
);
4889 pfile
->only_seen_white
= 0;
4893 if (CPP_BUFFER (pfile
)->has_escapes
)
4898 if (pfile
->output_escapes
)
4899 CPP_PUTS (pfile
, "@-", 2);
4900 parse_name (pfile
, GETC ());
4903 else if (is_space
[c
])
4905 CPP_RESERVE (pfile
, 2);
4906 if (pfile
->output_escapes
)
4907 CPP_PUTC_Q (pfile
, '@');
4908 CPP_PUTC_Q (pfile
, c
);
4912 if (pfile
->output_escapes
)
4914 CPP_PUTS (pfile
, "@@", 2);
4924 CPP_RESERVE(pfile
, 2);
4925 CPP_PUTC_Q (pfile
, '.');
4929 /* FIXME - misses the case "..\\\n." */
4930 if (c2
== '.' && PEEKN(1) == '.')
4932 CPP_RESERVE(pfile
, 4);
4933 CPP_PUTC_Q (pfile
, '.');
4934 CPP_PUTC_Q (pfile
, '.');
4935 CPP_PUTC_Q (pfile
, '.');
4937 CPP_NUL_TERMINATE_Q (pfile
);
4938 pfile
->only_seen_white
= 0;
4945 pfile
->only_seen_white
= 0;
4947 CPP_RESERVE(pfile
, 3);
4948 CPP_PUTC_Q (pfile
, c
);
4949 CPP_PUTC_Q (pfile
, GETC ());
4950 CPP_NUL_TERMINATE_Q (pfile
);
4956 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
4958 CPP_PUTC (pfile
, c
);
4964 case '0': case '1': case '2': case '3': case '4':
4965 case '5': case '6': case '7': case '8': case '9':
4970 CPP_RESERVE (pfile
, 2);
4971 CPP_PUTC_Q (pfile
, c
);
4976 if (!is_idchar
[c
] && c
!= '.'
4977 && ((c2
!= 'e' && c2
!= 'E'
4978 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
4979 || (c
!= '+' && c
!= '-')))
4984 CPP_NUL_TERMINATE_Q (pfile
);
4985 pfile
->only_seen_white
= 0;
4987 case 'b': case 'c': case 'd': case 'h': case 'o':
4988 case 'B': case 'C': case 'D': case 'H': case 'O':
4989 if (opts
->chill
&& PEEKC () == '\'')
4991 pfile
->only_seen_white
= 0;
4992 CPP_RESERVE (pfile
, 2);
4993 CPP_PUTC_Q (pfile
, c
);
4994 CPP_PUTC_Q (pfile
, '\'');
5000 goto chill_number_eof
;
5003 if (c
== '\\' && PEEKC() == '\n')
5010 CPP_PUTC (pfile
, c
);
5014 CPP_RESERVE (pfile
, 2);
5015 CPP_PUTC_Q (pfile
, c
);
5016 CPP_NUL_TERMINATE_Q (pfile
);
5023 CPP_NUL_TERMINATE (pfile
);
5030 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5031 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5032 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5033 case 'x': case 'y': case 'z':
5034 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5035 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5036 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5041 unsigned char *ident
;
5042 int before_name_written
= CPP_WRITTEN (pfile
);
5044 parse_name (pfile
, c
);
5045 pfile
->only_seen_white
= 0;
5046 if (pfile
->no_macro_expand
)
5048 ident
= pfile
->token_buffer
+ before_name_written
;
5049 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
5050 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
5053 if (hp
->type
== T_DISABLED
)
5055 if (pfile
->output_escapes
)
5056 { /* Return "@-IDENT", followed by '\0'. */
5058 CPP_RESERVE (pfile
, 3);
5059 ident
= pfile
->token_buffer
+ before_name_written
;
5060 CPP_ADJUST_WRITTEN (pfile
, 2);
5061 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
5068 /* If macro wants an arglist, verify that a '(' follows.
5069 first skip all whitespace, copying it to the output
5070 after the macro name. Then, if there is no '(',
5071 decide this is not a macro call and leave things that way. */
5072 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
5074 struct parse_marker macro_mark
;
5076 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
5078 cpp_buffer
*next_buf
;
5079 cpp_skip_hspace (pfile
);
5080 if (PEEKC () != EOF
)
5082 next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
5083 (*CPP_BUFFER (pfile
)->cleanup
) (CPP_BUFFER (pfile
), pfile
);
5084 CPP_BUFFER (pfile
) = next_buf
;
5086 parse_set_mark (¯o_mark
, pfile
);
5089 cpp_skip_hspace (pfile
);
5091 is_macro_call
= c
== '(';
5097 parse_goto_mark (¯o_mark
, pfile
);
5098 parse_clear_mark (¯o_mark
);
5102 /* This is now known to be a macro call. */
5104 /* it might not actually be a macro. */
5105 if (hp
->type
!= T_MACRO
) {
5106 int xbuf_len
; U_CHAR
*xbuf
;
5107 CPP_SET_WRITTEN (pfile
, before_name_written
);
5108 special_symbol (hp
, pfile
);
5109 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
5110 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
5111 CPP_SET_WRITTEN (pfile
, before_name_written
);
5112 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
5113 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
5117 /* Expand the macro, reading arguments as needed,
5118 and push the expansion on the input stack. */
5119 macroexpand (pfile
, hp
);
5120 CPP_SET_WRITTEN (pfile
, before_name_written
);
5123 /* An extra "@ " is added to the end of a macro expansion
5124 to prevent accidental token pasting. We prefer to avoid
5125 unneeded extra spaces (for the sake of cpp-using tools like
5126 imake). Here we remove the space if it is safe to do so. */
5127 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
5128 && pfile
->buffer
->rlimit
[-2] == '@'
5129 && pfile
->buffer
->rlimit
[-1] == ' ')
5131 int c1
= pfile
->buffer
->rlimit
[-3];
5132 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
5133 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
5134 pfile
->buffer
->rlimit
-= 2;
5139 case ' ': case '\t': case '\v': case '\r':
5142 CPP_PUTC (pfile
, c
);
5144 if (c
== EOF
|| !is_hor_space
[c
])
5158 CPP_PUTC (pfile
, c
);
5159 if (pfile
->only_seen_white
== 0)
5160 pfile
->only_seen_white
= 1;
5162 output_line_command (pfile
, 1, same_file
);
5165 case '(': token
= CPP_LPAREN
; goto char1
;
5166 case ')': token
= CPP_RPAREN
; goto char1
;
5167 case '{': token
= CPP_LBRACE
; goto char1
;
5168 case '}': token
= CPP_RBRACE
; goto char1
;
5169 case ',': token
= CPP_COMMA
; goto char1
;
5170 case ';': token
= CPP_SEMICOLON
; goto char1
;
5176 pfile
->only_seen_white
= 0;
5177 CPP_PUTC (pfile
, c
);
5183 /* Like cpp_get_token, but skip spaces and comments. */
5186 cpp_get_non_space_token (pfile
)
5189 int old_written
= CPP_WRITTEN (pfile
);
5192 enum cpp_token token
= cpp_get_token (pfile
);
5193 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
5194 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
5196 CPP_SET_WRITTEN (pfile
, old_written
);
5200 /* Parse an identifier starting with C. */
5203 parse_name (pfile
, c
)
5204 cpp_reader
*pfile
; int c
;
5210 if (c
== '\\' && PEEKC() == '\n')
5219 if (c
== '$' && CPP_PEDANTIC (pfile
))
5220 cpp_pedwarn ("`$' in identifier");
5222 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
5223 CPP_PUTC_Q (pfile
, c
);
5228 CPP_NUL_TERMINATE_Q (pfile
);
5233 /* Maintain and search list of included files, for #import. */
5235 /* Hash a file name for import_hash_table. */
5243 while (*f
) val
+= *f
++;
5244 return (val
%IMPORT_HASH_SIZE
);
5247 /* Search for file FILENAME in import_hash_table.
5248 Return -2 if found, either a matching name or a matching inode.
5249 Otherwise, open the file and return a file descriptor if successful
5250 or -1 if unsuccessful. */
5253 lookup_import (pfile
, filename
, searchptr
)
5256 struct file_name_list
*searchptr
;
5258 struct import_file
*i
;
5264 hashval
= import_hash (filename
);
5266 /* Attempt to find file in list of already included files */
5267 i
= pfile
->import_hash_table
[hashval
];
5270 if (!strcmp (filename
, i
->name
))
5271 return -2; /* return found */
5274 /* Open it and try a match on inode/dev */
5275 fd
= open_include_file (pfile
, filename
, searchptr
);
5279 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
5280 i
= pfile
->import_hash_table
[h
];
5282 /* Compare the inode and the device.
5283 Supposedly on some systems the inode is not a scalar. */
5284 if (!bcmp ((char *) &i
->inode
, (char *) &sb
.st_ino
, sizeof (sb
.st_ino
))
5285 && i
->dev
== sb
.st_dev
) {
5287 return -2; /* return found */
5292 return fd
; /* Not found, return open file */
5295 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5298 add_import (pfile
, fd
, fname
)
5303 struct import_file
*i
;
5307 hashval
= import_hash (fname
);
5309 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
5310 i
->name
= (char *)xmalloc (strlen (fname
)+1);
5311 strcpy (i
->name
, fname
);
5312 bcopy ((char *) &sb
.st_ino
, (char *) &i
->inode
, sizeof (sb
.st_ino
));
5314 i
->next
= pfile
->import_hash_table
[hashval
];
5315 pfile
->import_hash_table
[hashval
] = i
;
5318 /* The file_name_map structure holds a mapping of file names for a
5319 particular directory. This mapping is read from the file named
5320 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5321 map filenames on a file system with severe filename restrictions,
5322 such as DOS. The format of the file name map file is just a series
5323 of lines with two tokens on each line. The first token is the name
5324 to map, and the second token is the actual name to use. */
5326 struct file_name_map
5328 struct file_name_map
*map_next
;
5333 #define FILE_NAME_MAP_FILE "header.gcc"
5335 /* Read a space delimited string of unlimited length from a stdio
5339 read_filename_string (ch
, f
)
5347 set
= alloc
= xmalloc (len
+ 1);
5351 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5353 if (set
- alloc
== len
)
5356 alloc
= xrealloc (alloc
, len
+ 1);
5357 set
= alloc
+ len
/ 2;
5367 /* This structure holds a linked list of file name maps, one per directory. */
5369 struct file_name_map_list
5371 struct file_name_map_list
*map_list_next
;
5372 char *map_list_name
;
5373 struct file_name_map
*map_list_map
;
5376 /* Read the file name map file for DIRNAME. */
5378 static struct file_name_map
*
5379 read_name_map (pfile
, dirname
)
5383 register struct file_name_map_list
*map_list_ptr
;
5387 for (map_list_ptr
= CPP_OPTIONS (pfile
)->map_list
; map_list_ptr
;
5388 map_list_ptr
= map_list_ptr
->map_list_next
)
5389 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5390 return map_list_ptr
->map_list_map
;
5392 map_list_ptr
= ((struct file_name_map_list
*)
5393 xmalloc (sizeof (struct file_name_map_list
)));
5394 map_list_ptr
->map_list_name
= savestring (dirname
);
5395 map_list_ptr
->map_list_map
= NULL
;
5397 name
= (char *) alloca (strlen (dirname
) + strlen (FILE_NAME_MAP_FILE
) + 2);
5398 strcpy (name
, dirname
);
5401 strcat (name
, FILE_NAME_MAP_FILE
);
5402 f
= fopen (name
, "r");
5404 map_list_ptr
->map_list_map
= NULL
;
5408 int dirlen
= strlen (dirname
);
5410 while ((ch
= getc (f
)) != EOF
)
5413 struct file_name_map
*ptr
;
5417 from
= read_filename_string (ch
, f
);
5418 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5420 to
= read_filename_string (ch
, f
);
5422 ptr
= ((struct file_name_map
*)
5423 xmalloc (sizeof (struct file_name_map
)));
5424 ptr
->map_from
= from
;
5426 /* Make the real filename absolute. */
5431 ptr
->map_to
= xmalloc (dirlen
+ strlen (to
) + 2);
5432 strcpy (ptr
->map_to
, dirname
);
5433 ptr
->map_to
[dirlen
] = '/';
5434 strcpy (ptr
->map_to
+ dirlen
+ 1, to
);
5438 ptr
->map_next
= map_list_ptr
->map_list_map
;
5439 map_list_ptr
->map_list_map
= ptr
;
5441 while ((ch
= getc (f
)) != '\n')
5448 map_list_ptr
->map_list_next
= CPP_OPTIONS (pfile
)->map_list
;
5449 CPP_OPTIONS (pfile
)->map_list
= map_list_ptr
;
5451 return map_list_ptr
->map_list_map
;
5454 /* Try to open include file FILENAME. SEARCHPTR is the directory
5455 being tried from the include file search path. This function maps
5456 filenames on file systems based on information read by
5460 open_include_file (pfile
, filename
, searchptr
)
5463 struct file_name_list
*searchptr
;
5465 if (CPP_OPTIONS (pfile
)->remap
)
5467 register struct file_name_map
*map
;
5468 register char *from
;
5471 if (searchptr
&& ! searchptr
->got_name_map
)
5473 searchptr
->name_map
= read_name_map (pfile
,
5475 ? searchptr
->fname
: ".");
5476 searchptr
->got_name_map
= 1;
5479 /* First check the mapping for the directory we are using. */
5480 if (searchptr
&& searchptr
->name_map
)
5483 if (searchptr
->fname
)
5484 from
+= strlen (searchptr
->fname
) + 1;
5485 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5487 if (! strcmp (map
->map_from
, from
))
5489 /* Found a match. */
5490 return open (map
->map_to
, O_RDONLY
, 0666);
5495 /* Try to find a mapping file for the particular directory we are
5496 looking in. Thus #include <sys/types.h> will look up sys/types.h
5497 in /usr/include/header.gcc and look up types.h in
5498 /usr/include/sys/header.gcc. */
5499 p
= rindex (filename
, '/');
5504 && strlen (searchptr
->fname
) == p
- filename
5505 && ! strncmp (searchptr
->fname
, filename
, p
- filename
))
5507 /* FILENAME is in SEARCHPTR, which we've already checked. */
5508 return open (filename
, O_RDONLY
, 0666);
5518 dir
= (char *) alloca (p
- filename
+ 1);
5519 bcopy (filename
, dir
, p
- filename
);
5520 dir
[p
- filename
] = '\0';
5523 for (map
= read_name_map (pfile
, dir
); map
; map
= map
->map_next
)
5524 if (! strcmp (map
->map_from
, from
))
5525 return open (map
->map_to
, O_RDONLY
, 0666);
5528 return open (filename
, O_RDONLY
, 0666);
5531 /* Process the contents of include file FNAME, already open on descriptor F,
5533 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5534 "system" include directories (as decided by the `is_system_include'
5536 DIRPTR is the link in the dir path through which this file was found,
5537 or 0 if the file name was absolute or via the current directory.
5538 Return 1 on success, 0 on failure.
5540 The caller is responsible for the cpp_push_buffer. */
5543 finclude (pfile
, f
, fname
, system_header_p
, dirptr
)
5547 int system_header_p
;
5548 struct file_name_list
*dirptr
;
5554 cpp_buffer
*fp
; /* For input stack frame */
5556 int missing_newline
= 0;
5559 if (fstat (f
, &st
) < 0)
5561 cpp_perror_with_name (pfile
, fname
);
5563 cpp_pop_buffer (pfile
);
5567 fp
= CPP_BUFFER (pfile
);
5568 fp
->nominal_fname
= fp
->fname
= fname
;
5573 fp
->system_header_p
= system_header_p
;
5576 fp
->cleanup
= file_cleanup
;
5578 if (S_ISREG (st
.st_mode
)) {
5579 st_size
= (size_t) st
.st_size
;
5580 if (st_size
!= st
.st_size
|| st_size
+ 2 < st_size
) {
5581 cpp_error (pfile
, "file `%s' too large", fname
);
5585 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
5586 fp
->alimit
= fp
->buf
+ st_size
+ 2;
5589 /* Read the file contents, knowing that st_size is an upper bound
5590 on the number of bytes we can read. */
5591 length
= safe_read (f
, fp
->buf
, st_size
);
5592 fp
->rlimit
= fp
->buf
+ length
;
5593 if (length
< 0) goto nope
;
5595 else if (S_ISDIR (st
.st_mode
)) {
5596 cpp_error (pfile
, "directory `%s' specified in #include", fname
);
5600 /* Cannot count its file size before reading.
5601 First read the entire file into heap and
5602 copy them into buffer on stack. */
5607 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5610 i
= safe_read (f
, fp
->buf
+ st_size
, bsize
- st_size
);
5612 goto nope
; /* error! */
5614 if (st_size
!= bsize
)
5615 break; /* End of file */
5617 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5623 if ((length
> 0 && fp
->buf
[length
- 1] != '\n')
5624 /* Backslash-newline at end is not good enough. */
5625 || (length
> 1 && fp
->buf
[length
- 2] == '\\')) {
5626 fp
->buf
[length
++] = '\n';
5628 missing_newline
= 1;
5631 fp
->buf
[length
] = '\0';
5632 fp
->rlimit
= fp
->buf
+ length
;
5634 /* Close descriptor now, so nesting does not use lots of descriptors. */
5637 /* Must do this before calling trigraph_pcp, so that the correct file name
5638 will be printed in warning messages. */
5640 pfile
->input_stack_listing_current
= 0;
5650 if (missing_newline
)
5653 if (CPP_PEDANTIC (pfile
) && missing_newline
)
5654 pedwarn ("file does not end in newline");
5657 input_file_stack_tick
++;
5664 cpp_perror_with_name (pfile
, fname
);
5670 /* This is called after options have been processed.
5671 * Check options for consistency, and setup for processing input
5672 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5673 * Return 1 on success, 0 on failure.
5677 cpp_start_read (pfile
, fname
)
5681 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5682 struct cpp_pending
*pend
;
5687 /* The code looks at the defaults through this pointer, rather than through
5688 the constant structure above. This pointer gets changed if an environment
5689 variable specifies other defaults. */
5690 struct default_include
*include_defaults
= include_defaults_array
;
5692 /* Add dirs from CPATH after dirs from -I. */
5693 /* There seems to be confusion about what CPATH should do,
5694 so for the moment it is not documented. */
5695 /* Some people say that CPATH should replace the standard include dirs,
5696 but that seems pointless: it comes before them, so it overrides them
5698 GET_ENVIRONMENT (p
, "CPATH");
5699 if (p
!= 0 && ! opts
->no_standard_includes
)
5700 path_include (pfile
, p
);
5702 /* Now that dollars_in_ident is known, initialize is_idchar. */
5703 initialize_char_syntax (opts
);
5705 /* Do partial setup of input buffer for the sake of generating
5706 early #line directives (when -g is in effect). */
5707 fp
= cpp_push_buffer (pfile
, NULL
, 0);
5710 if (opts
->in_fname
== NULL
)
5711 opts
->in_fname
= "";
5712 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
5715 /* Install __LINE__, etc. Must follow initialize_char_syntax
5716 and option processing. */
5717 initialize_builtins (pfile
);
5719 /* Do standard #defines and assertions
5720 that identify system and machine type. */
5722 if (!opts
->inhibit_predefs
) {
5723 char *p
= (char *) alloca (strlen (predefs
) + 1);
5724 strcpy (p
, predefs
);
5727 while (*p
== ' ' || *p
== '\t')
5729 /* Handle -D options. */
5730 if (p
[0] == '-' && p
[1] == 'D') {
5732 while (*p
&& *p
!= ' ' && *p
!= '\t')
5736 if (opts
->debug_output
)
5737 output_line_command (pfile
, 0, same_file
);
5738 cpp_define (pfile
, q
);
5739 while (*p
== ' ' || *p
== '\t')
5741 } else if (p
[0] == '-' && p
[1] == 'A') {
5742 /* Handle -A options (assertions). */
5751 past_name
= assertion
;
5752 /* Locate end of name. */
5753 while (*past_name
&& *past_name
!= ' '
5754 && *past_name
!= '\t' && *past_name
!= '(')
5756 /* Locate `(' at start of value. */
5758 while (*value
&& (*value
== ' ' || *value
== '\t'))
5760 if (*value
++ != '(')
5762 while (*value
&& (*value
== ' ' || *value
== '\t'))
5765 /* Locate end of value. */
5766 while (*past_value
&& *past_value
!= ' '
5767 && *past_value
!= '\t' && *past_value
!= ')')
5769 termination
= past_value
;
5770 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
5772 if (*termination
++ != ')')
5774 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
5776 /* Temporarily null-terminate the value. */
5777 save_char
= *termination
;
5778 *termination
= '\0';
5779 /* Install the assertion. */
5780 make_assertion (pfile
, "-A", assertion
);
5781 *termination
= (char) save_char
;
5783 while (*p
== ' ' || *p
== '\t')
5791 /* Now handle the command line options. */
5793 /* Do -U's, -D's and -A's in the order they were seen. */
5794 /* First reverse the list. */
5795 opts
->pending
= nreverse_pending (opts
->pending
);
5797 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5799 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
5801 switch (pend
->cmd
[1])
5804 if (opts
->debug_output
)
5805 output_line_command (pfile
, 0, same_file
);
5806 do_undef (pfile
, NULL
, pend
->arg
, pend
->arg
+ strlen (pend
->arg
));
5809 if (opts
->debug_output
)
5810 output_line_command (pfile
, 0, same_file
);
5811 cpp_define (pfile
, pend
->arg
);
5814 make_assertion (pfile
, "-A", pend
->arg
);
5820 opts
->done_initializing
= 1;
5822 { /* Read the appropriate environment variable and if it exists
5823 replace include_defaults with the listed path. */
5825 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
5828 GET_ENVIRONMENT (epath
, "C_INCLUDE_PATH");
5831 GET_ENVIRONMENT (epath
, "CPLUS_INCLUDE_PATH");
5834 GET_ENVIRONMENT (epath
, "OBJC_INCLUDE_PATH");
5837 GET_ENVIRONMENT (epath
, "OBJCPLUS_INCLUDE_PATH");
5840 /* If the environment var for this language is set,
5841 add to the default list of include directories. */
5843 char *nstore
= (char *) alloca (strlen (epath
) + 2);
5845 char *startp
, *endp
;
5847 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
5848 if (*startp
== PATH_SEPARATOR
)
5851 = (struct default_include
*) xmalloc ((num_dirs
5852 * sizeof (struct default_include
))
5853 + sizeof (include_defaults_array
));
5854 startp
= endp
= epath
;
5857 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5858 if ((*endp
== PATH_SEPARATOR
)
5860 strncpy (nstore
, startp
, endp
-startp
);
5862 strcpy (nstore
, ".");
5864 nstore
[endp
-startp
] = '\0';
5866 include_defaults
[num_dirs
].fname
= savestring (nstore
);
5867 include_defaults
[num_dirs
].component
= 0;
5868 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
5869 include_defaults
[num_dirs
].cxx_aware
= 1;
5873 endp
= startp
= endp
+ 1;
5877 /* Put the usual defaults back in at the end. */
5878 bcopy ((char *) include_defaults_array
,
5879 (char *) &include_defaults
[num_dirs
],
5880 sizeof (include_defaults_array
));
5884 append_include_chain (pfile
, opts
->before_system
, opts
->last_before_system
);
5885 opts
->first_system_include
= opts
->before_system
;
5887 /* Unless -fnostdinc,
5888 tack on the standard include file dirs to the specified list */
5889 if (!opts
->no_standard_includes
) {
5890 struct default_include
*p
= include_defaults
;
5891 char *specd_prefix
= opts
->include_prefix
;
5892 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
5893 int default_len
= 0;
5894 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5895 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
5896 default_len
= strlen (default_prefix
) - 7;
5897 default_prefix
[default_len
] = 0;
5899 /* Search "translated" versions of GNU directories.
5900 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5901 if (specd_prefix
!= 0 && default_len
!= 0)
5902 for (p
= include_defaults
; p
->fname
; p
++) {
5903 /* Some standard dirs are only for C++. */
5905 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5906 /* Does this dir start with the prefix? */
5907 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
5908 /* Yes; change prefix and add to search list. */
5909 struct file_name_list
*new
5910 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5911 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
5912 char *str
= (char *) xmalloc (this_len
+ 1);
5913 strcpy (str
, specd_prefix
);
5914 strcat (str
, p
->fname
+ default_len
);
5916 new->control_macro
= 0;
5917 new->c_system_include_path
= !p
->cxx_aware
;
5918 new->got_name_map
= 0;
5919 append_include_chain (pfile
, new, new);
5920 if (opts
->first_system_include
== 0)
5921 opts
->first_system_include
= new;
5925 /* Search ordinary names for GNU include directories. */
5926 for (p
= include_defaults
; p
->fname
; p
++) {
5927 /* Some standard dirs are only for C++. */
5929 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
5930 struct file_name_list
*new
5931 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5932 new->control_macro
= 0;
5933 new->c_system_include_path
= !p
->cxx_aware
;
5934 new->fname
= update_path (p
->fname
, p
->component
);
5935 new->got_name_map
= 0;
5936 append_include_chain (pfile
, new, new);
5937 if (opts
->first_system_include
== 0)
5938 opts
->first_system_include
= new;
5943 /* Tack the after_include chain at the end of the include chain. */
5944 append_include_chain (pfile
, opts
->after_include
, opts
->last_after_include
);
5945 if (opts
->first_system_include
== 0)
5946 opts
->first_system_include
= opts
->after_include
;
5948 /* With -v, print the list of dirs to search. */
5949 if (opts
->verbose
) {
5950 struct file_name_list
*p
;
5951 fprintf (stderr
, "#include \"...\" search starts here:\n");
5952 for (p
= opts
->include
; p
; p
= p
->next
) {
5953 if (p
== opts
->first_bracket_include
)
5954 fprintf (stderr
, "#include <...> search starts here:\n");
5955 fprintf (stderr
, " %s\n", p
->fname
);
5957 fprintf (stderr
, "End of search list.\n");
5960 /* Scan the -imacros files before the main input.
5961 Much like #including them, but with no_output set
5962 so that only their macro definitions matter. */
5964 opts
->no_output
++; pfile
->no_record_file
++;
5965 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5967 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-imacros") == 0)
5969 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5972 cpp_perror_with_name (pfile
, pend
->arg
);
5975 if (!cpp_push_buffer (pfile
, NULL
, 0))
5977 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
5978 cpp_scan_buffer (pfile
);
5981 opts
->no_output
--; pfile
->no_record_file
--;
5983 /* Copy the entire contents of the main input file into
5984 the stacked input buffer previously allocated for it. */
5985 if (fname
== NULL
|| *fname
== 0) {
5988 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
5989 cpp_pfatal_with_name (pfile
, fname
);
5991 /* -MG doesn't select the form of output and must be specified with one of
5992 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5993 inhibit compilation. */
5994 if (opts
->print_deps_missing_files
5995 && (opts
->print_deps
== 0 || !opts
->no_output
))
5997 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
6001 /* Either of two environment variables can specify output of deps.
6002 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6003 where OUTPUT_FILE is the file to write deps info to
6004 and DEPS_TARGET is the target to mention in the deps. */
6006 if (opts
->print_deps
== 0
6007 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6008 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6009 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
6015 spec
= getenv ("SUNPRO_DEPENDENCIES");
6016 opts
->print_deps
= 2;
6019 opts
->print_deps
= 1;
6022 /* Find the space before the DEPS_TARGET, if there is one. */
6023 /* This should use index. (mrs) */
6024 while (*s
!= 0 && *s
!= ' ') s
++;
6027 opts
->deps_target
= s
+ 1;
6028 output_file
= (char *) xmalloc (s
- spec
+ 1);
6029 bcopy (spec
, output_file
, s
- spec
);
6030 output_file
[s
- spec
] = 0;
6034 opts
->deps_target
= 0;
6038 opts
->deps_file
= output_file
;
6039 opts
->print_deps_append
= 1;
6042 /* For -M, print the expected object file name
6043 as the target of this Make-rule. */
6044 if (opts
->print_deps
)
6046 pfile
->deps_allocated_size
= 200;
6047 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
6048 pfile
->deps_buffer
[0] = 0;
6049 pfile
->deps_size
= 0;
6050 pfile
->deps_column
= 0;
6052 if (opts
->deps_target
)
6053 deps_output (pfile
, opts
->deps_target
, ':');
6054 else if (*opts
->in_fname
== 0)
6055 deps_output (pfile
, "-", ':');
6060 static char *known_suffixes
[] = { ".c", ".C", ".s", ".S", ".m",
6061 ".cc", ".cxx", ".cpp", ".cp",
6065 /* Discard all directory prefixes from filename. */
6066 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
6067 #ifdef DIR_SEPARATOR
6068 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
6075 /* Copy remainder to mungable area. */
6076 p
= (char *) alloca (strlen(q
) + 8);
6079 /* Output P, but remove known suffixes. */
6082 /* Point to the filename suffix. */
6083 r
= rindex (p
, '.');
6084 /* Compare against the known suffixes. */
6086 while (known_suffixes
[x
] != 0)
6088 if (strncmp (known_suffixes
[x
], r
, q
- r
) == 0)
6090 /* Make q point to the bit we're going to overwrite
6091 with an object suffix. */
6098 /* Supply our own suffix. */
6105 deps_output (pfile
, p
, ':');
6106 deps_output (pfile
, opts
->in_fname
, ' ');
6111 /* Make sure data ends with a newline. And put a null after it. */
6113 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
6114 /* Backslash-newline at end is not good enough. */
6115 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
6116 fp
->buf
[fp
->length
++] = '\n';
6117 missing_newline
= 1;
6119 fp
->buf
[fp
->length
] = '\0';
6121 /* Unless inhibited, convert trigraphs in the input. */
6127 /* Scan the -include files before the main input.
6128 We push these in reverse order, so that the first one is handled first. */
6130 pfile
->no_record_file
++;
6131 opts
->pending
= nreverse_pending (opts
->pending
);
6132 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
6134 if (pend
->cmd
!= NULL
&& strcmp (pend
->cmd
, "-include") == 0)
6136 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
6139 cpp_perror_with_name (pfile
, pend
->arg
);
6142 if (!cpp_push_buffer (pfile
, NULL
, 0))
6144 finclude (pfile
, fd
, pend
->arg
, 0, NULL_PTR
);
6147 pfile
->no_record_file
--;
6149 /* Free the pending list. */
6150 for (pend
= opts
->pending
; pend
; )
6152 struct cpp_pending
*next
= pend
->next
;
6156 opts
->pending
= NULL
;
6159 /* Scan the input, processing macros and directives. */
6161 rescan (&outbuf
, 0);
6163 if (missing_newline
)
6166 if (CPP_PEDANTIC (pfile
) && missing_newline
)
6167 pedwarn ("file does not end in newline");
6170 if (finclude (pfile
, f
, fname
, 0, NULL_PTR
))
6171 output_line_command (pfile
, 0, same_file
);
6176 cpp_reader_init (pfile
)
6179 bzero ((char *) pfile
, sizeof (cpp_reader
));
6180 pfile
->get_token
= cpp_get_token
;
6182 pfile
->token_buffer_size
= 200;
6183 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
6184 CPP_SET_WRITTEN (pfile
, 0);
6186 pfile
->system_include_depth
= 0;
6187 pfile
->dont_repeat_files
= 0;
6188 pfile
->all_include_files
= 0;
6189 pfile
->max_include_len
= 0;
6190 pfile
->timebuf
= NULL
;
6191 pfile
->only_seen_white
= 1;
6192 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
6195 static struct cpp_pending
*
6196 nreverse_pending (list
)
6197 struct cpp_pending
*list
;
6200 register struct cpp_pending
*prev
= 0, *next
, *pend
;
6201 for (pend
= list
; pend
; pend
= next
)
6211 push_pending (pfile
, cmd
, arg
)
6216 struct cpp_pending
*pend
6217 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
6220 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
6221 CPP_OPTIONS (pfile
)->pending
= pend
;
6224 /* Handle command-line options in (argc, argv).
6225 Can be called multiple times, to handle multiple sets of options.
6226 Returns if an unrecognized option is seen.
6227 Returns number of handled arguments. */
6230 cpp_handle_options (pfile
, argc
, argv
)
6236 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6237 for (i
= 0; i
< argc
; i
++) {
6238 if (argv
[i
][0] != '-') {
6239 if (opts
->out_fname
!= NULL
)
6241 cpp_fatal (pfile
, "Usage: %s [switches] input output", argv
[0]);
6244 else if (opts
->in_fname
!= NULL
)
6245 opts
->out_fname
= argv
[i
];
6247 opts
->in_fname
= argv
[i
];
6249 switch (argv
[i
][1]) {
6252 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
6255 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
6259 if (!strcmp (argv
[i
], "-include")
6260 || !strcmp (argv
[i
], "-imacros")) {
6262 goto missing_filename
;
6264 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
6266 if (!strcmp (argv
[i
], "-iprefix")) {
6268 goto missing_filename
;
6270 opts
->include_prefix
= argv
[++i
];
6272 if (!strcmp (argv
[i
], "-ifoutput")) {
6273 opts
->output_conditionals
= 1;
6275 if (!strcmp (argv
[i
], "-isystem")) {
6276 struct file_name_list
*dirtmp
;
6279 goto missing_filename
;
6281 dirtmp
= (struct file_name_list
*)
6282 xmalloc (sizeof (struct file_name_list
));
6284 dirtmp
->control_macro
= 0;
6285 dirtmp
->c_system_include_path
= 1;
6286 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1]) + 1);
6287 strcpy (dirtmp
->fname
, argv
[++i
]);
6288 dirtmp
->got_name_map
= 0;
6290 if (opts
->before_system
== 0)
6291 opts
->before_system
= dirtmp
;
6293 opts
->last_before_system
->next
= dirtmp
;
6294 opts
->last_before_system
= dirtmp
; /* Tail follows the last one */
6296 /* Add directory to end of path for includes,
6297 with the default prefix at the front of its name. */
6298 if (!strcmp (argv
[i
], "-iwithprefix")) {
6299 struct file_name_list
*dirtmp
;
6302 if (opts
->include_prefix
!= 0)
6303 prefix
= opts
->include_prefix
;
6305 prefix
= savestring (GCC_INCLUDE_DIR
);
6306 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6307 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6308 prefix
[strlen (prefix
) - 7] = 0;
6311 dirtmp
= (struct file_name_list
*)
6312 xmalloc (sizeof (struct file_name_list
));
6313 dirtmp
->next
= 0; /* New one goes on the end */
6314 dirtmp
->control_macro
= 0;
6315 dirtmp
->c_system_include_path
= 0;
6317 goto missing_dirname
;
6319 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6320 + strlen (prefix
) + 1);
6321 strcpy (dirtmp
->fname
, prefix
);
6322 strcat (dirtmp
->fname
, argv
[++i
]);
6323 dirtmp
->got_name_map
= 0;
6325 if (opts
->after_include
== 0)
6326 opts
->after_include
= dirtmp
;
6328 opts
->last_after_include
->next
= dirtmp
;
6329 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6331 /* Add directory to main path for includes,
6332 with the default prefix at the front of its name. */
6333 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
6334 struct file_name_list
*dirtmp
;
6337 if (opts
->include_prefix
!= 0)
6338 prefix
= opts
->include_prefix
;
6340 prefix
= savestring (GCC_INCLUDE_DIR
);
6341 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6342 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
6343 prefix
[strlen (prefix
) - 7] = 0;
6346 dirtmp
= (struct file_name_list
*)
6347 xmalloc (sizeof (struct file_name_list
));
6348 dirtmp
->next
= 0; /* New one goes on the end */
6349 dirtmp
->control_macro
= 0;
6350 dirtmp
->c_system_include_path
= 0;
6352 goto missing_dirname
;
6354 dirtmp
->fname
= (char *) xmalloc (strlen (argv
[i
+1])
6355 + strlen (prefix
) + 1);
6356 strcpy (dirtmp
->fname
, prefix
);
6357 strcat (dirtmp
->fname
, argv
[++i
]);
6358 dirtmp
->got_name_map
= 0;
6360 append_include_chain (pfile
, dirtmp
, dirtmp
);
6362 /* Add directory to end of path for includes. */
6363 if (!strcmp (argv
[i
], "-idirafter")) {
6364 struct file_name_list
*dirtmp
;
6366 dirtmp
= (struct file_name_list
*)
6367 xmalloc (sizeof (struct file_name_list
));
6368 dirtmp
->next
= 0; /* New one goes on the end */
6369 dirtmp
->control_macro
= 0;
6370 dirtmp
->c_system_include_path
= 0;
6372 goto missing_dirname
;
6374 dirtmp
->fname
= argv
[++i
];
6375 dirtmp
->got_name_map
= 0;
6377 if (opts
->after_include
== 0)
6378 opts
->after_include
= dirtmp
;
6380 opts
->last_after_include
->next
= dirtmp
;
6381 opts
->last_after_include
= dirtmp
; /* Tail follows the last one */
6386 if (opts
->out_fname
!= NULL
)
6388 cpp_fatal (pfile
, "Output filename specified twice");
6392 goto missing_filename
;
6393 opts
->out_fname
= argv
[++i
];
6394 if (!strcmp (opts
->out_fname
, "-"))
6395 opts
->out_fname
= "";
6399 if (!strcmp (argv
[i
], "-pedantic"))
6400 CPP_PEDANTIC (pfile
) = 1;
6401 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
6402 CPP_PEDANTIC (pfile
) = 1;
6403 opts
->pedantic_errors
= 1;
6406 else if (!strcmp (argv
[i
], "-pcp")) {
6407 char *pcp_fname
= argv
[++i
];
6408 pcp_outfile
= ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
6409 ? fopen (pcp_fname
, "w")
6410 : fdopen (dup (fileno (stdout
)), "w"));
6411 if (pcp_outfile
== 0)
6412 cpp_pfatal_with_name (pfile
, pcp_fname
);
6419 if (!strcmp (argv
[i
], "-traditional")) {
6420 opts
->traditional
= 1;
6421 } else if (!strcmp (argv
[i
], "-trigraphs")) {
6423 opts
->no_trigraphs
= 0;
6428 if (! strcmp (argv
[i
], "-lang-c"))
6429 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6431 if (! strcmp (argv
[i
], "-lang-c89"))
6432 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->c89
= 1,
6434 if (! strcmp (argv
[i
], "-lang-c++"))
6435 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6437 if (! strcmp (argv
[i
], "-lang-objc"))
6438 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6440 if (! strcmp (argv
[i
], "-lang-objc++"))
6441 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
6443 if (! strcmp (argv
[i
], "-lang-asm"))
6445 if (! strcmp (argv
[i
], "-lint"))
6447 if (! strcmp (argv
[i
], "-lang-chill"))
6448 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
6449 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
6453 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
6457 opts
->inhibit_warnings
= 1;
6461 if (!strcmp (argv
[i
], "-Wtrigraphs"))
6462 opts
->warn_trigraphs
= 1;
6463 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
6464 opts
->warn_trigraphs
= 0;
6465 else if (!strcmp (argv
[i
], "-Wcomment"))
6466 opts
->warn_comments
= 1;
6467 else if (!strcmp (argv
[i
], "-Wno-comment"))
6468 opts
->warn_comments
= 0;
6469 else if (!strcmp (argv
[i
], "-Wcomments"))
6470 opts
->warn_comments
= 1;
6471 else if (!strcmp (argv
[i
], "-Wno-comments"))
6472 opts
->warn_comments
= 0;
6473 else if (!strcmp (argv
[i
], "-Wtraditional"))
6474 opts
->warn_stringify
= 1;
6475 else if (!strcmp (argv
[i
], "-Wno-traditional"))
6476 opts
->warn_stringify
= 0;
6477 else if (!strcmp (argv
[i
], "-Wundef"))
6478 opts
->warn_undef
= 1;
6479 else if (!strcmp (argv
[i
], "-Wno-undef"))
6480 opts
->warn_undef
= 0;
6481 else if (!strcmp (argv
[i
], "-Wimport"))
6482 opts
->warn_import
= 1;
6483 else if (!strcmp (argv
[i
], "-Wno-import"))
6484 opts
->warn_import
= 0;
6485 else if (!strcmp (argv
[i
], "-Werror"))
6486 opts
->warnings_are_errors
= 1;
6487 else if (!strcmp (argv
[i
], "-Wno-error"))
6488 opts
->warnings_are_errors
= 0;
6489 else if (!strcmp (argv
[i
], "-Wall"))
6491 opts
->warn_trigraphs
= 1;
6492 opts
->warn_comments
= 1;
6497 /* The style of the choices here is a bit mixed.
6498 The chosen scheme is a hybrid of keeping all options in one string
6499 and specifying each option in a separate argument:
6500 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6501 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6502 -M[M][G][D file]. This is awkward to handle in specs, and is not
6504 /* ??? -MG must be specified in addition to one of -M or -MM.
6505 This can be relaxed in the future without breaking anything.
6506 The converse isn't true. */
6508 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6509 if (!strcmp (argv
[i
], "-MG"))
6511 opts
->print_deps_missing_files
= 1;
6514 if (!strcmp (argv
[i
], "-M"))
6515 opts
->print_deps
= 2;
6516 else if (!strcmp (argv
[i
], "-MM"))
6517 opts
->print_deps
= 1;
6518 else if (!strcmp (argv
[i
], "-MD"))
6519 opts
->print_deps
= 2;
6520 else if (!strcmp (argv
[i
], "-MMD"))
6521 opts
->print_deps
= 1;
6522 /* For -MD and -MMD options, write deps on file named by next arg. */
6523 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
6526 goto missing_filename
;
6527 opts
->deps_file
= argv
[++i
];
6531 /* For -M and -MM, write deps on standard output
6532 and suppress the usual output. */
6533 opts
->no_output
= 1;
6539 char *p
= argv
[i
] + 2;
6541 while ((c
= *p
++) != 0) {
6542 /* Arg to -d specifies what parts of macros to dump */
6545 opts
->dump_macros
= dump_only
;
6546 opts
->no_output
= 1;
6549 opts
->dump_macros
= dump_names
;
6552 opts
->dump_macros
= dump_definitions
;
6555 opts
->dump_includes
= 1;
6563 if (argv
[i
][2] == '3')
6564 opts
->debug_output
= 1;
6568 fprintf (stderr
, "GNU CPP version %s", version_string
);
6569 #ifdef TARGET_VERSION
6572 fprintf (stderr
, "\n");
6577 opts
->print_include_names
= 1;
6581 if (argv
[i
][2] != 0)
6582 push_pending (pfile
, "-D", argv
[i
] + 2);
6583 else if (i
+ 1 == argc
)
6585 cpp_fatal (pfile
, "Macro name missing after -D option");
6589 i
++, push_pending (pfile
, "-D", argv
[i
]);
6596 if (argv
[i
][2] != 0)
6598 else if (i
+ 1 == argc
)
6600 cpp_fatal (pfile
, "Assertion missing after -A option");
6606 if (!strcmp (p
, "-")) {
6607 struct cpp_pending
**ptr
;
6608 /* -A- eliminates all predefined macros and assertions.
6609 Let's include also any that were specified earlier
6610 on the command line. That way we can get rid of any
6611 that were passed automatically in from GCC. */
6612 opts
->inhibit_predefs
= 1;
6613 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
6615 struct cpp_pending
*pend
= *ptr
;
6616 if (pend
->cmd
&& pend
->cmd
[0] == '-'
6617 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
6626 push_pending (pfile
, "-A", p
);
6631 case 'U': /* JF #undef something */
6632 if (argv
[i
][2] != 0)
6633 push_pending (pfile
, "-U", argv
[i
] + 2);
6634 else if (i
+ 1 == argc
)
6636 cpp_fatal (pfile
, "Macro name missing after -U option", NULL
);
6640 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
6644 opts
->put_out_comments
= 1;
6647 case 'E': /* -E comes from cc -E; ignore it. */
6651 opts
->no_line_commands
= 1;
6654 case '$': /* Don't include $ in identifiers. */
6655 opts
->dollars_in_ident
= 0;
6658 case 'I': /* Add directory to path for includes. */
6660 struct file_name_list
*dirtmp
;
6662 if (! CPP_OPTIONS(pfile
)->ignore_srcdir
6663 && !strcmp (argv
[i
] + 2, "-")) {
6664 CPP_OPTIONS (pfile
)->ignore_srcdir
= 1;
6665 /* Don't use any preceding -I directories for #include <...>. */
6666 CPP_OPTIONS (pfile
)->first_bracket_include
= 0;
6669 dirtmp
= (struct file_name_list
*)
6670 xmalloc (sizeof (struct file_name_list
));
6671 dirtmp
->next
= 0; /* New one goes on the end */
6672 dirtmp
->control_macro
= 0;
6673 dirtmp
->c_system_include_path
= 0;
6674 if (argv
[i
][2] != 0)
6675 dirtmp
->fname
= argv
[i
] + 2;
6676 else if (i
+ 1 == argc
)
6677 goto missing_dirname
;
6679 dirtmp
->fname
= argv
[++i
];
6680 dirtmp
->got_name_map
= 0;
6681 append_include_chain (pfile
, dirtmp
, dirtmp
);
6687 if (!strcmp (argv
[i
], "-nostdinc"))
6688 /* -nostdinc causes no default include directories.
6689 You must specify all include-file directories with -I. */
6690 opts
->no_standard_includes
= 1;
6691 else if (!strcmp (argv
[i
], "-nostdinc++"))
6692 /* -nostdinc++ causes no default C++-specific include directories. */
6693 opts
->no_standard_cplusplus_includes
= 1;
6695 else if (!strcmp (argv
[i
], "-noprecomp"))
6701 if (!strcmp (argv
[i
], "-remap"))
6706 /* Sun compiler passes undocumented switch "-undef".
6707 Let's assume it means to inhibit the predefined symbols. */
6708 opts
->inhibit_predefs
= 1;
6711 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6712 if (opts
->in_fname
== NULL
) {
6713 opts
->in_fname
= "";
6715 } else if (opts
->out_fname
== NULL
) {
6716 opts
->out_fname
= "";
6718 } /* else fall through into error */
6732 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
6734 if (opts
->print_deps
)
6736 /* Stream on which to print the dependency information. */
6739 /* Don't actually write the deps file if compilation has failed. */
6740 if (pfile
->errors
== 0)
6742 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
6743 if (opts
->deps_file
== 0)
6744 deps_stream
= stdout
;
6745 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
6746 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
6747 fputs (pfile
->deps_buffer
, deps_stream
);
6748 putc ('\n', deps_stream
);
6749 if (opts
->deps_file
)
6751 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
6752 cpp_fatal (pfile
, "I/O error on output");
6758 /* Free resources used by PFILE.
6759 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6766 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
6767 cpp_pop_buffer (pfile
);
6769 if (pfile
->token_buffer
)
6771 free (pfile
->token_buffer
);
6772 pfile
->token_buffer
= NULL
;
6775 if (pfile
->deps_buffer
)
6777 free (pfile
->deps_buffer
);
6778 pfile
->deps_buffer
= NULL
;
6779 pfile
->deps_allocated_size
= 0;
6782 while (pfile
->if_stack
)
6784 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
6785 pfile
->if_stack
= temp
->next
;
6789 while (pfile
->dont_repeat_files
)
6791 struct file_name_list
*temp
= pfile
->dont_repeat_files
;
6792 pfile
->dont_repeat_files
= temp
->next
;
6797 while (pfile
->all_include_files
)
6799 struct file_name_list
*temp
= pfile
->all_include_files
;
6800 pfile
->all_include_files
= temp
->next
;
6805 for (i
= IMPORT_HASH_SIZE
; --i
>= 0; )
6807 register struct import_file
*imp
= pfile
->import_hash_table
[i
];
6810 struct import_file
*next
= imp
->next
;
6815 pfile
->import_hash_table
[i
] = 0;
6818 for (i
= ASSERTION_HASHSIZE
; --i
>= 0; )
6820 while (pfile
->assertion_hashtab
[i
])
6821 delete_assertion (pfile
->assertion_hashtab
[i
]);
6824 cpp_hash_cleanup (pfile
);
6828 do_assert (pfile
, keyword
, buf
, limit
)
6830 struct directive
*keyword
;
6831 U_CHAR
*buf
, *limit
;
6833 long symstart
; /* remember where symbol name starts */
6835 int sym_length
; /* and how long it is */
6836 struct arglist
*tokens
= NULL
;
6838 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6839 && !CPP_BUFFER (pfile
)->system_header_p
)
6840 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6842 cpp_skip_hspace (pfile
);
6843 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6844 parse_name (pfile
, GETC());
6845 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6848 cpp_skip_hspace (pfile
);
6849 if (PEEKC() != '(') {
6850 cpp_error (pfile
, "missing token-sequence in `#assert'");
6856 tokens
= read_token_list (pfile
, &error_flag
);
6860 cpp_error (pfile
, "empty token-sequence in `#assert'");
6863 cpp_skip_hspace (pfile
);
6865 if (c
!= EOF
&& c
!= '\n')
6866 cpp_pedwarn (pfile
, "junk at end of `#assert'");
6867 skip_rest_of_line (pfile
);
6870 /* If this name isn't already an assertion name, make it one.
6871 Error if it was already in use in some other way. */
6874 ASSERTION_HASHNODE
*hp
;
6875 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6876 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6877 struct tokenlist_list
*value
6878 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6880 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6882 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
6883 cpp_error (pfile
, "`defined' redefined as assertion");
6884 hp
= assertion_install (pfile
, symname
, sym_length
, hashcode
);
6887 /* Add the spec'd token-sequence to the list of such. */
6888 value
->tokens
= tokens
;
6889 value
->next
= hp
->value
;
6892 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6895 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6896 skip_rest_of_line (pfile
);
6901 do_unassert (pfile
, keyword
, buf
, limit
)
6903 struct directive
*keyword
;
6904 U_CHAR
*buf
, *limit
;
6906 long symstart
; /* remember where symbol name starts */
6907 int sym_length
; /* and how long it is */
6910 struct arglist
*tokens
= NULL
;
6911 int tokens_specified
= 0;
6913 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6914 && !CPP_BUFFER (pfile
)->system_header_p
)
6915 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6917 cpp_skip_hspace (pfile
);
6919 symstart
= CPP_WRITTEN (pfile
); /* remember where it starts */
6920 parse_name (pfile
, GETC());
6921 sym_length
= check_macro_name (pfile
, pfile
->token_buffer
+ symstart
,
6924 cpp_skip_hspace (pfile
);
6925 if (PEEKC() == '(') {
6928 tokens
= read_token_list (pfile
, &error_flag
);
6932 cpp_error (pfile
, "empty token list in `#unassert'");
6936 tokens_specified
= 1;
6939 cpp_skip_hspace (pfile
);
6941 if (c
!= EOF
&& c
!= '\n')
6942 cpp_error (pfile
, "junk at end of `#unassert'");
6943 skip_rest_of_line (pfile
);
6946 ASSERTION_HASHNODE
*hp
;
6947 U_CHAR
*symname
= pfile
->token_buffer
+ symstart
;
6948 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6949 struct tokenlist_list
*tail
, *prev
;
6951 hp
= assertion_lookup (pfile
, symname
, sym_length
, hashcode
);
6955 /* If no token list was specified, then eliminate this assertion
6957 if (! tokens_specified
)
6958 delete_assertion (hp
);
6960 /* If a list of tokens was given, then delete any matching list. */
6965 struct tokenlist_list
*next
= tail
->next
;
6966 if (compare_token_lists (tail
->tokens
, tokens
)) {
6970 hp
->value
= tail
->next
;
6971 free_token_list (tail
->tokens
);
6981 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6984 CPP_SET_WRITTEN (pfile
, symstart
); /* Pop */
6985 skip_rest_of_line (pfile
);
6989 /* Test whether there is an assertion named NAME
6990 and optionally whether it has an asserted token list TOKENS.
6991 NAME is not null terminated; its length is SYM_LENGTH.
6992 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6995 check_assertion (pfile
, name
, sym_length
, tokens_specified
, tokens
)
6999 int tokens_specified
;
7000 struct arglist
*tokens
;
7002 ASSERTION_HASHNODE
*hp
;
7003 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
7005 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
7006 cpp_pedwarn (pfile
, "ANSI C does not allow testing assertions");
7008 hp
= assertion_lookup (pfile
, name
, sym_length
, hashcode
);
7010 /* It is not an assertion; just return false. */
7013 /* If no token list was specified, then value is 1. */
7014 if (! tokens_specified
)
7018 struct tokenlist_list
*tail
;
7022 /* If a list of tokens was given,
7023 then succeed if the assertion records a matching list. */
7026 if (compare_token_lists (tail
->tokens
, tokens
))
7031 /* Fail if the assertion has no matching list. */
7036 /* Compare two lists of tokens for equality including order of tokens. */
7039 compare_token_lists (l1
, l2
)
7040 struct arglist
*l1
, *l2
;
7043 if (l1
->length
!= l2
->length
)
7045 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
7051 /* Succeed if both lists end at the same time. */
7056 reverse_token_list (tokens
)
7057 struct arglist
*tokens
;
7059 register struct arglist
*prev
= 0, *this, *next
;
7060 for (this = tokens
; this; this = next
)
7069 /* Read a space-separated list of tokens ending in a close parenthesis.
7070 Return a list of strings, in the order they were written.
7071 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7073 static struct arglist
*
7074 read_token_list (pfile
, error_flag
)
7078 struct arglist
*token_ptrs
= 0;
7083 FORWARD (1); /* Skip '(' */
7085 /* Loop over the assertion value tokens. */
7088 struct arglist
*temp
;
7089 long name_written
= CPP_WRITTEN (pfile
);
7092 cpp_skip_hspace (pfile
);
7096 /* Find the end of the token. */
7099 CPP_PUTC (pfile
, c
);
7107 CPP_PUTC (pfile
, c
);
7109 else if (c
== '"' || c
== '\'')
7112 cpp_get_token (pfile
);
7118 while (c
!= EOF
&& ! is_space
[c
] && c
!= '(' && c
!= ')'
7119 && c
!= '"' && c
!= '\'')
7121 CPP_PUTC (pfile
, c
);
7124 if (c
!= EOF
) FORWARD(-1);
7127 length
= CPP_WRITTEN (pfile
) - name_written
;
7128 temp
= (struct arglist
*)
7129 xmalloc (sizeof (struct arglist
) + length
+ 1);
7130 temp
->name
= (U_CHAR
*) (temp
+ 1);
7131 bcopy ((char *) (pfile
->token_buffer
+ name_written
),
7132 (char *) temp
->name
, length
);
7133 temp
->name
[length
] = 0;
7134 temp
->next
= token_ptrs
;
7136 temp
->length
= length
;
7138 CPP_ADJUST_WRITTEN (pfile
, -length
); /* pop */
7140 if (c
== EOF
|| c
== '\n')
7143 "unterminated token sequence following `#' operator");
7148 /* We accumulated the names in reverse order.
7149 Now reverse them to get the proper order. */
7150 return reverse_token_list (token_ptrs
);
7154 free_token_list (tokens
)
7155 struct arglist
*tokens
;
7158 struct arglist
*next
= tokens
->next
;
7159 free (tokens
->name
);
7165 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7166 retrying if necessary. If MAX_READ_LEN is defined, read at most
7167 that bytes at a time. Return a negative value if an error occurs,
7168 otherwise return the actual number of bytes read,
7169 which must be LEN unless end-of-file was reached. */
7172 safe_read (desc
, ptr
, len
)
7177 int left
, rcount
, nchars
;
7183 if (rcount
> MAX_READ_LEN
)
7184 rcount
= MAX_READ_LEN
;
7186 nchars
= read (desc
, ptr
, rcount
);
7204 xcalloc (number
, size
)
7205 unsigned number
, size
;
7207 register unsigned total
= number
* size
;
7208 register char *ptr
= (char *) xmalloc (total
);
7217 unsigned size
= strlen (input
);
7218 char *output
= xmalloc (size
+ 1);
7219 strcpy (output
, input
);
7223 /* Initialize PMARK to remember the current position of PFILE. */
7226 parse_set_mark (pmark
, pfile
)
7227 struct parse_marker
*pmark
;
7230 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7231 pmark
->next
= pbuf
->marks
;
7232 pbuf
->marks
= pmark
;
7234 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7237 /* Cleanup PMARK - we no longer need it. */
7240 parse_clear_mark (pmark
)
7241 struct parse_marker
*pmark
;
7243 struct parse_marker
**pp
= &pmark
->buf
->marks
;
7244 for (; ; pp
= &(*pp
)->next
) {
7245 if (*pp
== NULL
) abort ();
7246 if (*pp
== pmark
) break;
7251 /* Backup the current position of PFILE to that saved in PMARK. */
7254 parse_goto_mark (pmark
, pfile
)
7255 struct parse_marker
*pmark
;
7258 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7259 if (pbuf
!= pmark
->buf
)
7260 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
7261 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
7264 /* Reset PMARK to point to the current position of PFILE. (Same
7265 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7268 parse_move_mark (pmark
, pfile
)
7269 struct parse_marker
*pmark
;
7272 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
7273 if (pbuf
!= pmark
->buf
)
7274 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
7275 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
7279 cpp_read_check_assertion (pfile
)
7282 int name_start
= CPP_WRITTEN (pfile
);
7283 int name_length
, name_written
;
7285 FORWARD (1); /* Skip '#' */
7286 cpp_skip_hspace (pfile
);
7287 parse_name (pfile
, GETC ());
7288 name_written
= CPP_WRITTEN (pfile
);
7289 name_length
= name_written
- name_start
;
7290 cpp_skip_hspace (pfile
);
7291 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
7294 struct arglist
*token_ptrs
= read_token_list (pfile
, &error_flag
);
7295 result
= check_assertion (pfile
,
7296 pfile
->token_buffer
+ name_start
, name_length
,
7300 result
= check_assertion (pfile
,
7301 pfile
->token_buffer
+ name_start
, name_length
,
7303 CPP_ADJUST_WRITTEN (pfile
, - name_length
); /* pop */
7308 cpp_print_file_and_line (pfile
)
7311 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7316 cpp_buf_line_and_col (ip
, &line
, &col
);
7317 cpp_file_line_for_message (ip
->nominal_fname
,
7318 line
, pfile
->show_column
? col
: -1);
7323 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
)
7326 char *arg1
, *arg2
, *arg3
;
7328 cpp_print_containing_files (pfile
);
7329 cpp_print_file_and_line (pfile
);
7330 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7333 /* Print error message but don't count it. */
7336 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
)
7339 char *arg1
, *arg2
, *arg3
;
7341 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7344 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7347 cpp_print_containing_files (pfile
);
7348 cpp_print_file_and_line (pfile
);
7349 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7352 /* Print an error message and maybe count it. */
7355 cpp_pedwarn (pfile
, msg
, arg1
, arg2
, arg3
)
7358 char *arg1
, *arg2
, *arg3
;
7360 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7361 cpp_error (pfile
, msg
, arg1
, arg2
, arg3
);
7363 cpp_warning (pfile
, msg
, arg1
, arg2
, arg3
);
7367 cpp_error_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7371 char *arg1
, *arg2
, *arg3
;
7373 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7375 cpp_print_containing_files (pfile
);
7378 cpp_file_line_for_message (ip
->nominal_fname
, line
, column
);
7380 cpp_message (pfile
, 1, msg
, arg1
, arg2
, arg3
);
7384 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7388 char *arg1
, *arg2
, *arg3
;
7392 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
7395 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
7398 cpp_print_containing_files (pfile
);
7400 ip
= cpp_file_buffer (pfile
);
7403 cpp_file_line_for_message (ip
->nominal_fname
, line
, column
);
7405 cpp_message (pfile
, 0, msg
, arg1
, arg2
, arg3
);
7409 cpp_pedwarn_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
)
7413 char *arg1
, *arg2
, *arg3
;
7415 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
7416 cpp_error_with_line (pfile
, column
, line
, msg
, arg1
, arg2
, arg3
);
7418 cpp_warning_with_line (pfile
, line
, column
, msg
, arg1
, arg2
, arg3
);
7421 /* Report a warning (or an error if pedantic_errors)
7422 giving specified file name and line number, not current. */
7425 cpp_pedwarn_with_file_and_line (pfile
, file
, line
, msg
, arg1
, arg2
, arg3
)
7430 char *arg1
, *arg2
, *arg3
;
7432 if (!CPP_OPTIONS (pfile
)->pedantic_errors
7433 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
7436 cpp_file_line_for_message (file
, line
, -1);
7437 cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
,
7438 msg
, arg1
, arg2
, arg3
);
7441 /* This defines "errno" properly for VMS, and gives us EACCES. */
7448 #ifndef HAVE_STRERROR
7449 extern int sys_nerr
;
7450 extern char *sys_errlist
[];
7451 #else /* HAVE_STRERROR */
7455 char *strerror (int,...);
7458 /* my_strerror - return the descriptive text associated with an
7462 my_strerror (errnum
)
7468 #ifndef HAVE_STRERROR
7469 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
7471 result
= strerror (errnum
);
7474 /* VAXCRTL's strerror() takes an optional second argument, which only
7475 matters when the first argument is EVMSERR. However, it's simplest
7476 just to pass it unconditionally. `vaxc$errno' is declared in
7477 <errno.h>, and maintained by the library in parallel with `errno'.
7478 We assume that caller's `errnum' either matches the last setting of
7479 `errno' by the library or else does not have the value `EVMSERR'. */
7481 result
= strerror (errnum
, vaxc$errno
);
7485 result
= "undocumented I/O error";
7490 /* Error including a message from `errno'. */
7493 cpp_error_from_errno (pfile
, name
)
7497 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
7499 cpp_print_containing_files (pfile
);
7502 cpp_file_line_for_message (ip
->nominal_fname
, ip
->lineno
, -1);
7504 cpp_message (pfile
, 1, "%s: %s", name
, my_strerror (errno
));
7508 cpp_perror_with_name (pfile
, name
)
7512 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
7516 * No pre-compiled header file support.
7518 * Possibly different enum token codes for each C/C++ token.
7520 * Should clean up remaining directives to that do_XXX functions
7521 * only take two arguments and all have command_reads_line.
7523 * Find and cleanup remaining uses of static variables,
7525 * Support for trigraphs.
7527 * Support -dM flag (dump_all_macros).
7529 * Support for_lint flag.