2 Copyright (C) 1986, 87, 89, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
38 #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 PARAMS ((char *, char *));
50 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
51 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
53 /* Find the largest host integer type and set its size and type.
54 Watch out: on some crazy hosts `long' is shorter than `int'. */
58 # include <inttypes.h>
59 # define HOST_WIDE_INT intmax_t
61 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
62 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
63 # define HOST_WIDE_INT int
65 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
66 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
67 # define HOST_WIDE_INT long
69 # define HOST_WIDE_INT long long
76 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
80 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
83 /* By default, colon separates directories in a path. */
84 #ifndef PATH_SEPARATOR
85 #define PATH_SEPARATOR ':'
88 #ifndef STANDARD_INCLUDE_DIR
89 #define STANDARD_INCLUDE_DIR "/usr/include"
91 #ifndef INCLUDE_LEN_FUDGE
92 #define INCLUDE_LEN_FUDGE 0
95 /* Symbols to predefine. */
98 static char *predefs = CPP_PREDEFINES;
100 static char *predefs = "";
103 /* We let tm.h override the types used here, to handle trivial differences
104 such as the choice of unsigned int or long unsigned int for size_t.
105 When machines start needing nontrivial differences in the size type,
106 it would be best to do something here to figure out automatically
107 from other information what type to use. */
109 /* The string value for __SIZE_TYPE__. */
112 #define SIZE_TYPE "long unsigned int"
115 /* The string value for __PTRDIFF_TYPE__. */
118 #define PTRDIFF_TYPE "long int"
121 /* The string value for __WCHAR_TYPE__. */
124 #define WCHAR_TYPE "int"
126 #define CPP_WCHAR_TYPE(PFILE) \
127 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
129 /* The string value for __USER_LABEL_PREFIX__ */
131 #ifndef USER_LABEL_PREFIX
132 #define USER_LABEL_PREFIX ""
135 /* The string value for __REGISTER_PREFIX__ */
137 #ifndef REGISTER_PREFIX
138 #define REGISTER_PREFIX ""
141 /* In the definition of a #assert name, this structure forms
142 a list of the individual values asserted.
143 Each value is itself a list of "tokens".
144 These are strings that are compared by name. */
146 struct tokenlist_list {
147 struct tokenlist_list *next;
148 struct arglist *tokens;
151 struct assertion_hashnode {
152 struct assertion_hashnode *next; /* double links for easy deletion */
153 struct assertion_hashnode *prev;
154 /* also, a back pointer to this node's hash
155 chain is kept, in case the node is the head
156 of the chain and gets deleted. */
157 struct assertion_hashnode **bucket_hdr;
158 int length; /* length of token, for quick comparison */
159 U_CHAR *name; /* the actual name */
160 /* List of token-sequences. */
161 struct tokenlist_list *value;
164 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
165 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
167 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
168 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
169 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
170 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
171 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
172 (Note that it is false while we're expanding marco *arguments*.) */
173 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
175 /* Move all backslash-newline pairs out of embarrassing places.
176 Exchange all such pairs following BP
177 with any potentially-embarrassing characters that follow them.
178 Potentially-embarrassing characters are / and *
179 (because a backslash-newline inside a comment delimiter
180 would cause it not to be recognized). */
182 #define NEWLINE_FIX \
183 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
185 /* Same, but assume we've already read the potential '\\' into C. */
186 #define NEWLINE_FIX1(C) do { \
187 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
191 struct cpp_pending *next;
196 /* Forward declarations. */
199 extern void cpp_hash_cleanup PARAMS ((cpp_reader *));
201 static char *my_strerror PROTO ((int));
202 static void add_import PROTO ((cpp_reader *, int, char *));
203 static void append_include_chain PROTO ((cpp_reader *,
204 struct file_name_list *,
205 struct file_name_list *));
206 static void make_assertion PROTO ((cpp_reader *, char *, U_CHAR *));
207 static void path_include PROTO ((cpp_reader *, char *));
208 static void initialize_builtins PROTO ((cpp_reader *));
209 static void initialize_char_syntax PROTO ((struct cpp_options *));
211 static void trigraph_pcp ();
213 static int finclude PROTO ((cpp_reader *, int, char *,
214 int, struct file_name_list *));
215 static void validate_else PROTO ((cpp_reader *, char *));
216 static int comp_def_part PROTO ((int, U_CHAR *, int, U_CHAR *,
219 extern void fancy_abort ();
221 static int lookup_import PROTO ((cpp_reader *, char *,
222 struct file_name_list *));
223 static int redundant_include_p PROTO ((cpp_reader *, char *));
224 static int is_system_include PROTO ((cpp_reader *, char *));
225 static struct file_name_map *read_name_map PROTO ((cpp_reader *, char *));
226 static char *read_filename_string PROTO ((int, FILE *));
227 static int open_include_file PROTO ((cpp_reader *, char *,
228 struct file_name_list *));
229 static int check_macro_name PROTO ((cpp_reader *, U_CHAR *, char *));
230 static int compare_defs PROTO ((cpp_reader *,
231 DEFINITION *, DEFINITION *));
232 static int compare_token_lists PROTO ((struct arglist *,
234 static HOST_WIDE_INT eval_if_expression PROTO ((cpp_reader *, U_CHAR *, int));
235 static int change_newlines PROTO ((U_CHAR *, int));
236 static struct arglist *read_token_list PROTO ((cpp_reader *, int *));
237 static void free_token_list PROTO ((struct arglist *));
238 static int safe_read PROTO ((int, char *, int));
239 static void push_macro_expansion PARAMS ((cpp_reader *,
240 U_CHAR *, int, HASHNODE *));
241 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
242 extern char *xrealloc ();
243 static char *xcalloc PROTO ((unsigned, unsigned));
244 static char *savestring PROTO ((char *));
246 static void conditional_skip PROTO ((cpp_reader *, int,
247 enum node_type, U_CHAR *));
248 static void skip_if_group PROTO ((cpp_reader *, int));
249 static int parse_name PARAMS ((cpp_reader *, int));
250 static void print_help PROTO ((void));
252 /* Last arg to output_line_command. */
253 enum file_change_code {same_file, enter_file, leave_file};
255 /* External declarations. */
257 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
259 extern char *version_string;
260 extern struct tm *localtime ();
262 struct file_name_list
264 struct file_name_list *next;
266 /* If the following is nonzero, it is a macro name.
267 Don't include the file again if that macro is defined. */
268 U_CHAR *control_macro;
269 /* If the following is nonzero, it is a C-language system include
271 int c_system_include_path;
272 /* Mapping of file names for this directory. */
273 struct file_name_map *name_map;
274 /* Non-zero if name_map is valid. */
278 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
279 via the same directory as the file that #included it. */
280 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
282 /* #include "file" looks in source file dir, then stack. */
283 /* #include <file> just looks in the stack. */
284 /* -I directories are added to the end, then the defaults are added. */
286 static struct default_include {
287 char *fname; /* The name of the directory. */
288 char *component; /* The component containing the directory */
289 int cplusplus; /* Only look here if we're compiling C++. */
290 int cxx_aware; /* Includes in this directory don't need to
291 be wrapped in extern "C" when compiling
293 } include_defaults_array[]
294 #ifdef INCLUDE_DEFAULTS
298 /* Pick up GNU C++ specific include files. */
299 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
300 { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 },
302 /* This is the dir for fixincludes. Put it just before
303 the files that we fix. */
304 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
305 /* For cross-compilation, this dir name is generated
306 automatically in Makefile.in. */
307 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
308 #ifdef TOOL_INCLUDE_DIR
309 /* This is another place that the target system's headers might be. */
310 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
312 #else /* not CROSS_COMPILE */
313 #ifdef LOCAL_INCLUDE_DIR
314 /* This should be /usr/local/include and should come before
315 the fixincludes-fixed header files. */
316 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
318 #ifdef TOOL_INCLUDE_DIR
319 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
320 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
321 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
323 /* This is the dir for fixincludes. Put it just before
324 the files that we fix. */
325 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
326 /* Some systems have an extra dir of include files. */
327 #ifdef SYSTEM_INCLUDE_DIR
328 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
330 #ifndef STANDARD_INCLUDE_COMPONENT
331 #define STANDARD_INCLUDE_COMPONENT 0
333 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
334 #endif /* not CROSS_COMPILE */
337 #endif /* no INCLUDE_DEFAULTS */
339 /* `struct directive' defines one #-directive, including how to handle it. */
342 int length; /* Length of name */
343 int (*func) /* Function to handle directive */
344 PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
345 char *name; /* Name of directive */
346 enum node_type type; /* Code which describes which directive. */
347 char command_reads_line; /* One if rest of line is read by func. */
350 /* These functions are declared to return int instead of void since they
351 are going to be placed in a table and some old compilers have trouble with
352 pointers to functions returning void. */
354 static int do_define PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
355 static int do_line PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
356 static int do_include PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
357 static int do_undef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
358 static int do_error PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
359 static int do_pragma PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
360 static int do_ident PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
361 static int do_if PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
362 static int do_xifdef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
363 static int do_else PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
364 static int do_elif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
365 static int do_endif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
366 #ifdef SCCS_DIRECTIVE
367 static int do_sccs PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
369 static int do_once PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
370 static int do_assert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
371 static int do_unassert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
372 static int do_warning PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
374 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
375 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
377 /* Here is the actual list of #-directives, most-often-used first.
378 The initialize_builtins function assumes #define is the very first. */
380 static struct directive directive_table[] = {
381 { 6, do_define, "define", T_DEFINE},
382 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
383 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
384 { 7, do_include, "include", T_INCLUDE, 1},
385 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
386 { 6, do_include, "import", T_IMPORT, 1},
387 { 5, do_endif, "endif", T_ENDIF, 1},
388 { 4, do_else, "else", T_ELSE, 1},
389 { 2, do_if, "if", T_IF, 1},
390 { 4, do_elif, "elif", T_ELIF, 1},
391 { 5, do_undef, "undef", T_UNDEF},
392 { 5, do_error, "error", T_ERROR},
393 { 7, do_warning, "warning", T_WARNING},
394 { 6, do_pragma, "pragma", T_PRAGMA},
395 { 4, do_line, "line", T_LINE, 1},
396 { 5, do_ident, "ident", T_IDENT, 1},
397 #ifdef SCCS_DIRECTIVE
398 { 4, do_sccs, "sccs", T_SCCS},
400 { 6, do_assert, "assert", T_ASSERT, 1},
401 { 8, do_unassert, "unassert", T_UNASSERT, 1},
402 { -1, 0, "", T_UNUSED},
405 /* table to tell if char can be part of a C identifier. */
406 U_CHAR is_idchar[256];
407 /* table to tell if char can be first char of a c identifier. */
408 U_CHAR is_idstart[256];
409 /* table to tell if c is horizontal space. */
410 U_CHAR is_hor_space[256];
411 /* table to tell if c is horizontal or vertical space. */
412 static U_CHAR is_space[256];
414 /* Initialize syntactic classifications of characters. */
417 initialize_char_syntax (opts)
418 struct cpp_options *opts;
423 * Set up is_idchar and is_idstart tables. These should be
424 * faster than saying (is_alpha (c) || c == '_'), etc.
425 * Set up these things before calling any routines tthat
428 for (i = 'a'; i <= 'z'; i++) {
429 is_idchar[i - 'a' + 'A'] = 1;
431 is_idstart[i - 'a' + 'A'] = 1;
434 for (i = '0'; i <= '9'; i++)
438 is_idchar['$'] = opts->dollars_in_ident;
439 is_idstart['$'] = opts->dollars_in_ident;
441 /* horizontal space table */
442 is_hor_space[' '] = 1;
443 is_hor_space['\t'] = 1;
444 is_hor_space['\v'] = 1;
445 is_hor_space['\f'] = 1;
446 is_hor_space['\r'] = 1;
457 /* Place into PFILE a quoted string representing the string SRC.
458 Caller must reserve enough space in pfile->token_buffer. */
461 quote_string (pfile, src)
467 CPP_PUTC_Q (pfile, '\"');
469 switch ((c = *src++))
473 CPP_PUTC_Q (pfile, c);
476 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
477 CPP_ADJUST_WRITTEN (pfile, 4);
483 CPP_PUTC_Q (pfile, '\\');
484 CPP_PUTC_Q (pfile, c);
488 CPP_PUTC_Q (pfile, '\"');
489 CPP_NUL_TERMINATE_Q (pfile);
494 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
497 cpp_grow_buffer (pfile, n)
501 long old_written = CPP_WRITTEN (pfile);
502 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
503 pfile->token_buffer = (U_CHAR *)
504 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
505 CPP_SET_WRITTEN (pfile, old_written);
510 * process a given definition string, for initialization
511 * If STR is just an identifier, define it with value 1.
512 * If STR has anything after the identifier, then it should
513 * be identifier=definition.
517 cpp_define (pfile, str)
527 cpp_error (pfile, "malformed option `-D %s'", str);
530 while (is_idchar[*++p])
533 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
536 p = (U_CHAR *) str; /* Error */
540 buf = (U_CHAR *) alloca (p - buf + 4);
541 strcpy ((char *)buf, str);
542 strcat ((char *)buf, " 1");
546 cpp_error (pfile, "malformed option `-D %s'", str);
552 /* Copy the entire option so we can modify it. */
553 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
554 strncpy (buf, str, p - str);
555 /* Change the = to a space. */
557 /* Scan for any backslash-newline and remove it. */
562 if (*p == '\\' && p[1] == '\n')
570 do_define (pfile, NULL, buf, buf + strlen (buf));
573 /* Process the string STR as if it appeared as the body of a #assert.
574 OPTION is the option name for which STR was the argument. */
577 make_assertion (pfile, option, str)
584 /* Copy the entire option so we can modify it. */
585 buf = (U_CHAR *) alloca (strlen (str) + 1);
586 strcpy ((char *) buf, str);
587 /* Scan for any backslash-newline and remove it. */
591 if (*p == '\\' && p[1] == '\n')
600 if (!is_idstart[*p]) {
601 cpp_error (pfile, "malformed option `%s %s'", option, str);
604 while (is_idchar[*++p])
606 while (*p == ' ' || *p == '\t') p++;
607 if (! (*p == 0 || *p == '(')) {
608 cpp_error (pfile, "malformed option `%s %s'", option, str);
612 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
614 do_assert (pfile, NULL, NULL, NULL);
615 cpp_pop_buffer (pfile);
619 /* Append a chain of `struct file_name_list's
620 to the end of the main include chain.
621 FIRST is the beginning of the chain to append, and LAST is the end. */
624 append_include_chain (pfile, first, last)
626 struct file_name_list *first, *last;
628 struct cpp_options *opts = CPP_OPTIONS (pfile);
629 struct file_name_list *dir;
634 if (opts->include == 0)
635 opts->include = first;
637 opts->last_include->next = first;
639 if (opts->first_bracket_include == 0)
640 opts->first_bracket_include = first;
642 for (dir = first; ; dir = dir->next) {
643 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
644 if (len > pfile->max_include_len)
645 pfile->max_include_len = len;
651 opts->last_include = last;
654 /* Add output to `deps_buffer' for the -M switch.
655 STRING points to the text to be output.
656 SPACER is ':' for targets, ' ' for dependencies, zero for text
657 to be inserted literally. */
660 deps_output (pfile, string, spacer)
665 int size = strlen (string);
670 #ifndef MAX_OUTPUT_COLUMNS
671 #define MAX_OUTPUT_COLUMNS 72
674 && pfile->deps_column > 0
675 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
677 deps_output (pfile, " \\\n ", 0);
678 pfile->deps_column = 0;
681 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
683 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
684 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
685 pfile->deps_allocated_size);
687 if (spacer == ' ' && pfile->deps_column > 0)
688 pfile->deps_buffer[pfile->deps_size++] = ' ';
689 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
690 pfile->deps_size += size;
691 pfile->deps_column += size;
693 pfile->deps_buffer[pfile->deps_size++] = ':';
694 pfile->deps_buffer[pfile->deps_size] = 0;
697 /* Given a colon-separated list of file names PATH,
698 add all the names to the search path for include files. */
701 path_include (pfile, path)
713 struct file_name_list *dirtmp;
715 /* Find the end of this name. */
716 while (*q != 0 && *q != PATH_SEPARATOR) q++;
718 /* An empty name in the path stands for the current directory. */
719 name = (char *) xmalloc (2);
723 /* Otherwise use the directory that is named. */
724 name = (char *) xmalloc (q - p + 1);
725 bcopy (p, name, q - p);
729 dirtmp = (struct file_name_list *)
730 xmalloc (sizeof (struct file_name_list));
731 dirtmp->next = 0; /* New one goes on the end */
732 dirtmp->control_macro = 0;
733 dirtmp->c_system_include_path = 0;
734 dirtmp->fname = name;
735 dirtmp->got_name_map = 0;
736 append_include_chain (pfile, dirtmp, dirtmp);
738 /* Advance past this name. */
742 /* Skip the colon. */
748 cpp_options_init (opts)
751 bzero ((char *) opts, sizeof *opts);
752 opts->in_fname = NULL;
753 opts->out_fname = NULL;
755 /* Initialize is_idchar to allow $. */
756 opts->dollars_in_ident = 1;
757 initialize_char_syntax (opts);
759 opts->no_line_commands = 0;
760 opts->no_trigraphs = 1;
761 opts->put_out_comments = 0;
762 opts->print_include_names = 0;
763 opts->dump_macros = dump_none;
767 opts->cplusplus_comments = 1;
774 opts->pedantic_errors = 0;
775 opts->inhibit_warnings = 0;
776 opts->warn_comments = 0;
777 opts->warn_import = 1;
778 opts->warnings_are_errors = 0;
782 null_underflow (pfile)
783 cpp_reader *pfile ATTRIBUTE_UNUSED;
789 null_cleanup (pbuf, pfile)
790 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
791 cpp_reader *pfile ATTRIBUTE_UNUSED;
797 macro_cleanup (pbuf, pfile)
799 cpp_reader *pfile ATTRIBUTE_UNUSED;
801 HASHNODE *macro = (HASHNODE *) pbuf->data;
802 if (macro->type == T_DISABLED)
803 macro->type = T_MACRO;
804 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
810 file_cleanup (pbuf, pfile)
812 cpp_reader *pfile ATTRIBUTE_UNUSED;
822 /* Assuming we have read '/'.
823 If this is the start of a comment (followed by '*' or '/'),
824 skip to the end of the comment, and return ' '.
825 Return EOF if we reached the end of file before the end of the comment.
826 If not the start of a comment, return '/'. */
829 skip_comment (pfile, linep)
834 while (PEEKC() == '\\' && PEEKN(1) == '\n')
849 while (c == '\\' && PEEKC() == '\n')
853 FORWARD(1), c = GETC();
855 if (prev_c == '*' && c == '/')
857 if (c == '\n' && linep)
861 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
868 return ' '; /* Allow // to be terminated by EOF. */
869 while (c == '\\' && PEEKC() == '\n')
878 /* Don't consider final '\n' to be part of comment. */
888 /* Skip whitespace \-newline and comments. Does not macro-expand. */
891 cpp_skip_hspace (pfile)
901 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
902 cpp_pedwarn (pfile, "%s in preprocessing directive",
903 c == '\f' ? "formfeed" : "vertical tab");
909 c = skip_comment (pfile, NULL);
912 if (c == EOF || c == '/')
915 else if (c == '\\' && PEEKN(1) == '\n') {
918 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
919 && is_hor_space[PEEKN(1)])
925 /* Read the rest of the current line.
926 The line is appended to PFILE's output buffer. */
929 copy_rest_of_line (pfile)
932 struct cpp_options *opts = CPP_OPTIONS (pfile);
949 goto scan_directive_token;
953 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
954 goto scan_directive_token;
958 if (CPP_PEDANTIC (pfile))
959 cpp_pedwarn (pfile, "%s in preprocessing directive",
960 c == '\f' ? "formfeed" : "vertical tab");
966 scan_directive_token:
968 cpp_get_token (pfile);
974 CPP_NUL_TERMINATE (pfile);
978 skip_rest_of_line (pfile)
981 long old = CPP_WRITTEN (pfile);
982 copy_rest_of_line (pfile);
983 CPP_SET_WRITTEN (pfile, old);
986 /* Handle a possible # directive.
987 '#' has already been read. */
990 handle_directive (pfile)
993 register struct directive *kt;
996 U_CHAR *ident, *line_end;
997 long old_written = CPP_WRITTEN (pfile);
999 cpp_skip_hspace (pfile);
1002 if (c >= '0' && c <= '9')
1004 /* Handle # followed by a line number. */
1005 if (CPP_PEDANTIC (pfile))
1006 cpp_pedwarn (pfile, "`#' followed by integer");
1007 do_line (pfile, NULL, NULL, NULL);
1008 goto done_a_directive;
1011 /* Now find the directive name. */
1012 CPP_PUTC (pfile, '#');
1013 parse_name (pfile, GETC());
1014 ident = pfile->token_buffer + old_written + 1;
1015 ident_length = CPP_PWRITTEN (pfile) - ident;
1016 if (ident_length == 0 && PEEKC() == '\n')
1018 /* A line of just `#' becomes blank. */
1019 goto done_a_directive;
1023 if (ident_length == 0 || !is_idstart[*ident]) {
1025 while (is_idchar[*p]) {
1026 if (*p < '0' || *p > '9')
1030 /* Avoid error for `###' and similar cases unless -pedantic. */
1032 while (*p == '#' || is_hor_space[*p]) p++;
1034 if (pedantic && !lang_asm)
1035 cpp_warning (pfile, "invalid preprocessor directive");
1041 cpp_error (pfile, "invalid preprocessor directive name");
1047 * Decode the keyword and call the appropriate expansion
1048 * routine, after moving the input pointer up to the next line.
1050 for (kt = directive_table; ; kt++) {
1051 if (kt->length <= 0)
1052 goto not_a_directive;
1053 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1057 if (kt->command_reads_line)
1061 /* Nonzero means do not delete comments within the directive.
1062 #define needs this when -traditional. */
1063 int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE;
1064 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1065 CPP_OPTIONS (pfile)->put_out_comments = comments;
1066 after_ident = CPP_WRITTEN (pfile);
1067 copy_rest_of_line (pfile);
1068 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1071 /* We may want to pass through #define, #pragma, and #include.
1072 Other directives may create output, but we don't want the directive
1073 itself out, so we pop it now. For example conditionals may emit
1074 #failed ... #endfailed stuff. But note that popping the buffer
1075 means the parameters to kt->func may point after pfile->limit
1076 so these parameters are invalid as soon as something gets appended
1077 to the token_buffer. */
1079 line_end = CPP_PWRITTEN (pfile);
1080 if (! (kt->type == T_DEFINE
1081 || kt->type == T_PRAGMA
1082 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
1083 && CPP_OPTIONS (pfile)->dump_includes)))
1084 CPP_SET_WRITTEN (pfile, old_written);
1086 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1088 if (kt->type == T_DEFINE)
1090 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
1092 /* Skip "#define". */
1093 U_CHAR *p = pfile->token_buffer + old_written + 7;
1095 SKIP_WHITE_SPACE (p);
1096 while (is_idchar[*p]) p++;
1098 CPP_PUTC (pfile, '\n');
1100 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
1101 CPP_SET_WRITTEN (pfile, old_written);
1111 /* Pass a directive through to the output file.
1112 BUF points to the contents of the directive, as a contiguous string.
1113 LIMIT points to the first character past the end of the directive.
1114 KEYWORD is the keyword-table entry for the directive. */
1117 pass_thru_directive (buf, limit, pfile, keyword)
1118 U_CHAR *buf, *limit;
1120 struct directive *keyword;
1122 register unsigned keyword_length = keyword->length;
1124 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1125 CPP_PUTC_Q (pfile, '#');
1126 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1127 if (limit != buf && buf[0] != ' ')
1128 CPP_PUTC_Q (pfile, ' ');
1129 CPP_PUTS_Q (pfile, buf, limit - buf);
1131 CPP_PUTS_Q (pfile, '\n');
1132 /* Count the line we have just made in the output,
1133 to get in sync properly. */
1138 /* The arglist structure is built by do_define to tell
1139 collect_definition where the argument names begin. That
1140 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1141 would contain pointers to the strings x, y, and z.
1142 Collect_definition would then build a DEFINITION node,
1143 with reflist nodes pointing to the places x, y, and z had
1144 appeared. So the arglist is just convenience data passed
1145 between these two routines. It is not kept around after
1146 the current #define has been processed and entered into the
1150 struct arglist *next;
1157 /* Read a replacement list for a macro with parameters.
1158 Build the DEFINITION structure.
1159 Reads characters of text starting at BUF until END.
1160 ARGLIST specifies the formal parameters to look for
1161 in the text of the definition; NARGS is the number of args
1162 in that list, or -1 for a macro name that wants no argument list.
1163 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1164 and NAMELEN is its length in characters.
1166 Note that comments, backslash-newlines, and leading white space
1167 have already been deleted from the argument. */
1170 collect_expansion (pfile, buf, limit, nargs, arglist)
1172 U_CHAR *buf, *limit;
1174 struct arglist *arglist;
1177 register U_CHAR *p, *lastp, *exp_p;
1178 struct reflist *endpat = NULL;
1179 /* Pointer to first nonspace after last ## seen. */
1181 /* Pointer to first nonspace after last single-# seen. */
1182 U_CHAR *stringify = 0;
1184 int expected_delimiter = '\0';
1186 /* Scan thru the replacement list, ignoring comments and quoted
1187 strings, picking up on the macro calls. It does a linear search
1188 thru the arg list on every potential symbol. Profiling might say
1189 that something smarter should happen. */
1194 /* Find the beginning of the trailing whitespace. */
1196 while (p < limit && is_space[limit[-1]]) limit--;
1198 /* Allocate space for the text in the macro definition.
1199 Leading and trailing whitespace chars need 2 bytes each.
1200 Each other input char may or may not need 1 byte,
1201 so this is an upper bound. The extra 5 are for invented
1202 leading and trailing newline-marker and final null. */
1203 maxsize = (sizeof (DEFINITION)
1205 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1209 defn = (DEFINITION *) xcalloc (1, maxsize);
1211 defn->nargs = nargs;
1212 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1217 /* Add one initial space escape-marker to prevent accidental
1218 token-pasting (often removed by macroexpand). */
1222 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1223 cpp_error (pfile, "`##' at start of macro definition");
1227 /* Process the main body of the definition. */
1229 int skipped_arg = 0;
1230 register U_CHAR c = *p++;
1234 if (!CPP_TRADITIONAL (pfile)) {
1238 if (expected_delimiter != '\0') {
1239 if (c == expected_delimiter)
1240 expected_delimiter = '\0';
1242 expected_delimiter = c;
1246 if (p < limit && expected_delimiter) {
1247 /* In a string, backslash goes through
1248 and makes next char ordinary. */
1254 /* An '@' in a string or character constant stands for itself,
1255 and does not need to be escaped. */
1256 if (!expected_delimiter)
1261 /* # is ordinary inside a string. */
1262 if (expected_delimiter)
1264 if (p < limit && *p == '#') {
1265 /* ##: concatenate preceding and following tokens. */
1266 /* Take out the first #, discard preceding whitespace. */
1268 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1270 /* Skip the second #. */
1272 /* Discard following whitespace. */
1273 SKIP_WHITE_SPACE (p);
1276 cpp_error (pfile, "`##' at end of macro definition");
1277 } else if (nargs >= 0) {
1278 /* Single #: stringify following argument ref.
1279 Don't leave the # in the expansion. */
1281 SKIP_WHITE_SPACE (p);
1282 if (p == limit || ! is_idstart[*p]
1283 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1285 "`#' operator is not followed by a macro argument name");
1292 /* In -traditional mode, recognize arguments inside strings and
1293 character constants, and ignore special properties of #.
1294 Arguments inside strings are considered "stringified", but no
1295 extra quote marks are supplied. */
1299 if (expected_delimiter != '\0') {
1300 if (c == expected_delimiter)
1301 expected_delimiter = '\0';
1303 expected_delimiter = c;
1307 /* Backslash quotes delimiters and itself, but not macro args. */
1308 if (expected_delimiter != 0 && p < limit
1309 && (*p == expected_delimiter || *p == '\\')) {
1316 if (expected_delimiter != '\0') /* No comments inside strings. */
1319 /* If we find a comment that wasn't removed by handle_directive,
1320 this must be -traditional. So replace the comment with
1324 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1327 /* Mark this as a concatenation-point, as if it had been ##. */
1335 /* Handle the start of a symbol. */
1336 if (is_idchar[c] && nargs > 0) {
1337 U_CHAR *id_beg = p - 1;
1341 while (p != limit && is_idchar[*p]) p++;
1342 id_len = p - id_beg;
1345 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1346 register struct arglist *arg;
1348 for (arg = arglist; arg != NULL; arg = arg->next) {
1349 struct reflist *tpat;
1351 if (arg->name[0] == c
1352 && arg->length == id_len
1353 && strncmp (arg->name, id_beg, id_len) == 0) {
1354 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1355 if (CPP_TRADITIONAL (pfile)) {
1356 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1360 "macro arg `%.*s' would be stringified with -traditional.",
1364 /* If ANSI, don't actually substitute inside a string. */
1365 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1367 /* make a pat node for this arg and append it to the end of
1369 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1371 tpat->raw_before = concat == id_beg;
1372 tpat->raw_after = 0;
1373 tpat->rest_args = arg->rest_args;
1374 tpat->stringify = (CPP_TRADITIONAL (pfile)
1375 ? expected_delimiter != '\0'
1376 : stringify == id_beg);
1379 defn->pattern = tpat;
1381 endpat->next = tpat;
1384 tpat->argno = arg->argno;
1385 tpat->nchars = exp_p - lastp;
1387 register U_CHAR *p1 = p;
1388 SKIP_WHITE_SPACE (p1);
1389 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1390 tpat->raw_after = 1;
1392 lastp = exp_p; /* place to start copying from next time */
1399 /* If this was not a macro arg, copy it into the expansion. */
1400 if (! skipped_arg) {
1401 register U_CHAR *lim1 = p;
1405 if (stringify == id_beg)
1407 "`#' operator should be followed by a macro argument name");
1412 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1414 /* If ANSI, put in a "@ " marker to prevent token pasting.
1415 But not if "inside a string" (which in ANSI mode
1416 happens only for -D option). */
1423 defn->length = exp_p - defn->expansion;
1425 /* Crash now if we overrun the allocated size. */
1426 if (defn->length + 1 > maxsize)
1430 /* This isn't worth the time it takes. */
1431 /* give back excess storage */
1432 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1439 * special extension string that can be added to the last macro argument to
1440 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1441 * #define wow(a, b...) process (b, a, b)
1442 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1443 * { wow (one, two); } -> { process (two, one, two); }
1444 * if this "rest_arg" is used with the concat token '##' and if it is not
1445 * supplied then the token attached to with ## will not be outputted. Ex:
1446 * #define wow (a, b...) process (b ## , a, ## b)
1447 * { wow (1, 2); } -> { process (2, 1, 2); }
1448 * { wow (one); } -> { process (one); {
1450 static char rest_extension[] = "...";
1451 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1453 /* Create a DEFINITION node from a #define directive. Arguments are
1454 as for do_define. */
1457 create_definition (buf, limit, pfile, predefinition)
1458 U_CHAR *buf, *limit;
1462 U_CHAR *bp; /* temp ptr into input buffer */
1463 U_CHAR *symname; /* remember where symbol name starts */
1464 int sym_length; /* and how long it is */
1467 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1469 int arglengths = 0; /* Accumulate lengths of arg names
1470 plus number of args. */
1472 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1476 while (is_hor_space[*bp])
1479 symname = bp; /* remember where it starts */
1481 sym_length = check_macro_name (pfile, bp, "macro");
1484 /* Lossage will occur if identifiers or control keywords are broken
1485 across lines using backslash. This is not the right place to take
1489 struct arglist *arg_ptrs = NULL;
1492 bp++; /* skip '(' */
1493 SKIP_WHITE_SPACE (bp);
1495 /* Loop over macro argument names. */
1496 while (*bp != ')') {
1497 struct arglist *temp;
1499 temp = (struct arglist *) alloca (sizeof (struct arglist));
1501 temp->next = arg_ptrs;
1502 temp->argno = argno++;
1503 temp->rest_args = 0;
1507 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1509 if (!is_idstart[*bp])
1510 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1512 /* Find the end of the arg name. */
1513 while (is_idchar[*bp]) {
1515 /* do we have a "special" rest-args extension here? */
1516 if (limit - bp > REST_EXTENSION_LENGTH
1517 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1519 temp->rest_args = 1;
1523 temp->length = bp - temp->name;
1525 bp += REST_EXTENSION_LENGTH;
1526 arglengths += temp->length + 2;
1527 SKIP_WHITE_SPACE (bp);
1528 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1529 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1534 SKIP_WHITE_SPACE (bp);
1537 cpp_error (pfile, "unterminated parameter list in `#define'");
1541 struct arglist *otemp;
1543 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1544 if (temp->length == otemp->length
1545 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1548 name = (U_CHAR *) alloca (temp->length + 1);
1549 (void) strncpy (name, temp->name, temp->length);
1550 name[temp->length] = '\0';
1552 "duplicate argument name `%s' in `#define'", name);
1558 ++bp; /* skip paren */
1559 SKIP_WHITE_SPACE (bp);
1560 /* now everything from bp before limit is the definition. */
1561 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1562 defn->rest_args = rest_args;
1564 /* Now set defn->args.argnames to the result of concatenating
1565 the argument names in reverse order
1566 with comma-space between them. */
1567 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1569 struct arglist *temp;
1571 for (temp = arg_ptrs; temp; temp = temp->next) {
1572 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1574 if (temp->next != 0) {
1575 defn->args.argnames[i++] = ',';
1576 defn->args.argnames[i++] = ' ';
1579 defn->args.argnames[i] = 0;
1582 /* Simple expansion or empty definition. */
1586 if (is_hor_space[*bp]) {
1588 SKIP_WHITE_SPACE (bp);
1591 case '!': case '"': case '#': case '%': case '&': case '\'':
1592 case ')': case '*': case '+': case ',': case '-': case '.':
1593 case '/': case ':': case ';': case '<': case '=': case '>':
1594 case '?': case '[': case '\\': case ']': case '^': case '{':
1595 case '|': case '}': case '~':
1596 cpp_warning (pfile, "missing white space after `#define %.*s'",
1597 sym_length, symname);
1601 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1602 sym_length, symname);
1607 /* now everything from bp before limit is the definition. */
1608 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1609 defn->args.argnames = (U_CHAR *) "";
1615 /* OP is null if this is a predefinition */
1616 defn->predefined = predefinition;
1618 mdef.symnam = symname;
1619 mdef.symlen = sym_length;
1628 /* Check a purported macro name SYMNAME, and yield its length.
1629 USAGE is the kind of name this is intended for. */
1632 check_macro_name (pfile, symname, usage)
1640 for (p = symname; is_idchar[*p]; p++)
1642 sym_length = p - symname;
1644 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1645 cpp_error (pfile, "invalid %s name", usage);
1646 else if (!is_idstart[*symname]) {
1647 U_CHAR *msg; /* what pain... */
1648 msg = (U_CHAR *) alloca (sym_length + 1);
1649 bcopy (symname, msg, sym_length);
1650 msg[sym_length] = 0;
1651 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1653 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1654 cpp_error (pfile, "invalid %s name `defined'", usage);
1659 /* Return zero if two DEFINITIONs are isomorphic. */
1662 compare_defs (pfile, d1, d2)
1664 DEFINITION *d1, *d2;
1666 register struct reflist *a1, *a2;
1667 register U_CHAR *p1 = d1->expansion;
1668 register U_CHAR *p2 = d2->expansion;
1671 if (d1->nargs != d2->nargs)
1673 if (CPP_PEDANTIC (pfile)
1674 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1676 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1677 a1 = a1->next, a2 = a2->next) {
1678 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1679 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1680 || a1->argno != a2->argno
1681 || a1->stringify != a2->stringify
1682 || a1->raw_before != a2->raw_before
1683 || a1->raw_after != a2->raw_after)
1691 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1692 p2, d2->length - (p2 - d2->expansion), 1))
1697 /* Return 1 if two parts of two macro definitions are effectively different.
1698 One of the parts starts at BEG1 and has LEN1 chars;
1699 the other has LEN2 chars at BEG2.
1700 Any sequence of whitespace matches any other sequence of whitespace.
1701 FIRST means these parts are the first of a macro definition;
1702 so ignore leading whitespace entirely.
1703 LAST means these parts are the last of a macro definition;
1704 so ignore trailing whitespace entirely. */
1707 comp_def_part (first, beg1, len1, beg2, len2, last)
1709 U_CHAR *beg1, *beg2;
1713 register U_CHAR *end1 = beg1 + len1;
1714 register U_CHAR *end2 = beg2 + len2;
1716 while (beg1 != end1 && is_space[*beg1]) beg1++;
1717 while (beg2 != end2 && is_space[*beg2]) beg2++;
1720 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1721 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1723 while (beg1 != end1 && beg2 != end2) {
1724 if (is_space[*beg1] && is_space[*beg2]) {
1725 while (beg1 != end1 && is_space[*beg1]) beg1++;
1726 while (beg2 != end2 && is_space[*beg2]) beg2++;
1727 } else if (*beg1 == *beg2) {
1731 return (beg1 != end1) || (beg2 != end2);
1734 /* Process a #define command.
1735 BUF points to the contents of the #define command, as a contiguous string.
1736 LIMIT points to the first character past the end of the definition.
1737 KEYWORD is the keyword-table entry for #define,
1738 or NULL for a "predefined" macro. */
1741 do_define (pfile, keyword, buf, limit)
1743 struct directive *keyword;
1744 U_CHAR *buf, *limit;
1751 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1752 if (pcp_outfile && keyword)
1753 pass_thru_directive (buf, limit, pfile, keyword);
1756 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1760 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1762 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1765 /* Redefining a precompiled key is ok. */
1766 if (hp->type == T_PCSTRING)
1768 /* Redefining a macro is ok if the definitions are the same. */
1769 else if (hp->type == T_MACRO)
1770 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1771 /* Redefining a constant is ok with -D. */
1772 else if (hp->type == T_CONST)
1773 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1774 /* Print the warning if it's not ok. */
1777 U_CHAR *msg; /* what pain... */
1779 /* If we are passing through #define and #undef directives, do
1780 that for this re-definition now. */
1781 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1782 pass_thru_directive (buf, limit, pfile, keyword);
1784 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1786 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1787 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1788 cpp_pedwarn (pfile, msg);
1789 if (hp->type == T_MACRO)
1790 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1791 "this is the location of the previous definition");
1793 /* Replace the old definition. */
1795 hp->value.defn = mdef.defn;
1799 /* If we are passing through #define and #undef directives, do
1800 that for this new definition now. */
1801 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1802 pass_thru_directive (buf, limit, pfile, keyword);
1803 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1804 (char *) mdef.defn, hashcode);
1814 /* This structure represents one parsed argument in a macro call.
1815 `raw' points to the argument text as written (`raw_length' is its length).
1816 `expanded' points to the argument's macro-expansion
1817 (its length is `expand_length').
1818 `stringified_length' is the length the argument would have
1820 `use_count' is the number of times this macro arg is substituted
1821 into the macro. If the actual use count exceeds 10,
1822 the value stored is 10. */
1824 /* raw and expanded are relative to ARG_BASE */
1825 #define ARG_BASE ((pfile)->token_buffer)
1828 /* Strings relative to pfile->token_buffer */
1829 long raw, expanded, stringified;
1830 int raw_length, expand_length;
1831 int stringified_length;
1836 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1837 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1838 as the new input buffer.
1839 Return the new buffer, or NULL on failure. */
1842 cpp_push_buffer (pfile, buffer, length)
1847 register cpp_buffer *buf = CPP_BUFFER (pfile);
1848 if (buf == pfile->buffer_stack)
1850 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1855 bzero ((char *) buf, sizeof (cpp_buffer));
1856 CPP_BUFFER (pfile) = buf;
1857 buf->if_stack = pfile->if_stack;
1858 buf->cleanup = null_cleanup;
1859 buf->underflow = null_underflow;
1860 buf->buf = buf->cur = buffer;
1861 buf->alimit = buf->rlimit = buffer + length;
1867 cpp_pop_buffer (pfile)
1870 cpp_buffer *buf = CPP_BUFFER (pfile);
1871 (*buf->cleanup) (buf, pfile);
1872 return ++CPP_BUFFER (pfile);
1875 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1876 Pop the buffer when done. */
1879 cpp_scan_buffer (pfile)
1882 cpp_buffer *buffer = CPP_BUFFER (pfile);
1885 enum cpp_token token = cpp_get_token (pfile);
1886 if (token == CPP_EOF) /* Should not happen ... */
1888 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1890 cpp_pop_buffer (pfile);
1897 * Rescan a string (which may have escape marks) into pfile's buffer.
1898 * Place the result in pfile->token_buffer.
1900 * The input is copied before it is scanned, so it is safe to pass
1901 * it something from the token_buffer that will get overwritten
1902 * (because it follows CPP_WRITTEN). This is used by do_include.
1906 cpp_expand_to_buffer (pfile, buf, length)
1911 register cpp_buffer *ip;
1915 U_CHAR *limit = buf + length;
1918 int odepth = indepth;
1924 /* Set up the input on the input stack. */
1926 buf1 = (U_CHAR *) alloca (length + 1);
1928 register U_CHAR *p1 = buf;
1929 register U_CHAR *p2 = buf1;
1936 ip = cpp_push_buffer (pfile, buf1, length);
1939 ip->has_escapes = 1;
1941 ip->lineno = obuf.lineno = 1;
1944 /* Scan the input, create the output. */
1945 cpp_scan_buffer (pfile);
1948 if (indepth != odepth)
1952 CPP_NUL_TERMINATE (pfile);
1957 adjust_position (buf, limit, linep, colp)
1967 (*linep)++, (*colp) = 1;
1973 /* Move line_base forward, updating lineno and colno. */
1976 update_position (pbuf)
1977 register cpp_buffer *pbuf;
1979 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
1980 unsigned char *new_pos = pbuf->cur;
1981 register struct parse_marker *mark;
1982 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
1984 if (pbuf->buf + mark->position < new_pos)
1985 new_pos = pbuf->buf + mark->position;
1987 pbuf->line_base += new_pos - old_pos;
1988 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1992 cpp_buf_line_and_col (pbuf, linep, colp)
1993 register cpp_buffer *pbuf;
2001 *linep = pbuf->lineno;
2002 *colp = pbuf->colno;
2003 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2012 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2015 cpp_file_buffer (pfile)
2018 cpp_buffer *ip = CPP_BUFFER (pfile);
2020 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2021 if (ip->fname != NULL)
2027 count_newlines (buf, limit)
2028 register U_CHAR *buf;
2029 register U_CHAR *limit;
2031 register long count = 0;
2042 * write out a #line command, for instance, after an #include file.
2043 * If CONDITIONAL is nonzero, we can omit the #line if it would
2044 * appear to be a no-op, and we can output a few newlines instead
2045 * if we want to increase the line number by a small amount.
2046 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2050 output_line_command (pfile, conditional, file_change)
2053 enum file_change_code file_change;
2056 cpp_buffer *ip = CPP_BUFFER (pfile);
2058 if (ip->fname == NULL)
2061 update_position (ip);
2063 if (CPP_OPTIONS (pfile)->no_line_commands
2064 || CPP_OPTIONS (pfile)->no_output)
2067 line = CPP_BUFFER (pfile)->lineno;
2068 col = CPP_BUFFER (pfile)->colno;
2069 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2071 if (CPP_OPTIONS (pfile)->no_line_commands)
2075 if (line == pfile->lineno)
2078 /* If the inherited line number is a little too small,
2079 output some newlines instead of a #line command. */
2080 if (line > pfile->lineno && line < pfile->lineno + 8) {
2081 CPP_RESERVE (pfile, 20);
2082 while (line > pfile->lineno) {
2083 CPP_PUTC_Q (pfile, '\n');
2091 /* Don't output a line number of 0 if we can help it. */
2092 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2093 && *ip->bufp == '\n') {
2099 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2101 #ifdef OUTPUT_LINE_COMMANDS
2102 static char sharp_line[] = "#line ";
2104 static char sharp_line[] = "# ";
2106 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2109 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
2110 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2112 quote_string (pfile, ip->nominal_fname);
2113 if (file_change != same_file) {
2114 CPP_PUTC_Q (pfile, ' ');
2115 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2117 /* Tell cc1 if following text comes from a system header file. */
2118 if (ip->system_header_p) {
2119 CPP_PUTC_Q (pfile, ' ');
2120 CPP_PUTC_Q (pfile, '3');
2122 #ifndef NO_IMPLICIT_EXTERN_C
2123 /* Tell cc1plus if following text should be treated as C. */
2124 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2125 CPP_PUTC_Q (pfile, ' ');
2126 CPP_PUTC_Q (pfile, '4');
2129 CPP_PUTC_Q (pfile, '\n');
2130 pfile->lineno = line;
2134 * Parse a macro argument and append the info on PFILE's token_buffer.
2135 * REST_ARGS means to absorb the rest of the args.
2136 * Return nonzero to indicate a syntax error.
2139 static enum cpp_token
2140 macarg (pfile, rest_args)
2145 enum cpp_token token;
2146 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2147 CPP_OPTIONS (pfile)->put_out_comments = 0;
2149 /* Try to parse as much of the argument as exists at this
2150 input stack level. */
2151 pfile->no_macro_expand++;
2154 token = cpp_get_token (pfile);
2160 /* If we've hit end of file, it's an error (reported by caller).
2161 Ditto if it's the end of cpp_expand_to_buffer text.
2162 If we've hit end of macro, just continue. */
2163 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2174 /* if we've returned to lowest level and
2175 we aren't absorbing all args */
2176 if (paren == 0 && rest_args == 0)
2180 /* Remove ',' or ')' from argument buffer. */
2181 CPP_ADJUST_WRITTEN (pfile, -1);
2188 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2189 pfile->no_macro_expand--;
2194 /* Turn newlines to spaces in the string of length LENGTH at START,
2195 except inside of string constants.
2196 The string is copied into itself with its beginning staying fixed. */
2199 change_newlines (start, length)
2203 register U_CHAR *ibp;
2204 register U_CHAR *obp;
2205 register U_CHAR *limit;
2209 limit = start + length;
2212 while (ibp < limit) {
2213 *obp++ = c = *ibp++;
2218 /* Notice and skip strings, so that we don't delete newlines in them. */
2221 while (ibp < limit) {
2222 *obp++ = c = *ibp++;
2225 if (c == '\n' && quotec == '\'')
2241 if (!pfile->timebuf) {
2242 time_t t = time ((time_t *) 0);
2243 pfile->timebuf = localtime (&t);
2245 return pfile->timebuf;
2248 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2249 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2253 * expand things like __FILE__. Place the expansion into the output
2254 * buffer *without* rescanning.
2258 special_symbol (hp, pfile)
2265 cpp_buffer *ip = NULL;
2268 int paren = 0; /* For special `defined' keyword */
2271 if (pcp_outfile && pcp_inside_if
2272 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2274 "Predefined macro `%s' used inside `#if' during precompilation",
2278 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2280 if (ip == CPP_NULL_BUFFER (pfile))
2282 cpp_error (pfile, "cccp error: not in any file?!");
2283 return; /* the show must go on */
2285 if (ip->fname != NULL)
2295 if (hp->type == T_BASE_FILE)
2297 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2298 ip = CPP_PREV_BUFFER (ip);
2300 string = ip->nominal_fname;
2304 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2305 quote_string (pfile, string);
2309 case T_INCLUDE_LEVEL:
2311 ip = CPP_BUFFER (pfile);
2312 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2313 if (ip->fname != NULL)
2316 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2317 sprintf (buf, "%d", true_indepth - 1);
2321 buf = (char *) alloca (3 + strlen (version_string));
2322 sprintf (buf, "\"%s\"", version_string);
2325 #ifndef NO_BUILTIN_SIZE_TYPE
2331 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2332 case T_PTRDIFF_TYPE:
2338 buf = CPP_WCHAR_TYPE (pfile);
2341 case T_USER_LABEL_PREFIX_TYPE:
2342 buf = USER_LABEL_PREFIX;
2345 case T_REGISTER_PREFIX_TYPE:
2346 buf = REGISTER_PREFIX;
2350 buf = (char *) alloca (4 * sizeof (int));
2351 sprintf (buf, "%d", hp->value.ival);
2352 #ifdef STDC_0_IN_SYSTEM_HEADERS
2353 if (ip->system_header_p
2354 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2355 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2359 if (pcp_inside_if && pcp_outfile)
2360 /* Output a precondition for this macro use */
2361 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2367 long line = ip->lineno;
2368 long col = ip->colno;
2369 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2371 buf = (char *) alloca (10);
2372 sprintf (buf, "%ld", line);
2378 buf = (char *) alloca (20);
2379 timebuf = timestamp (pfile);
2380 if (hp->type == T_DATE)
2381 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2382 timebuf->tm_mday, timebuf->tm_year + 1900);
2384 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2388 case T_SPEC_DEFINED:
2389 buf = " 0 "; /* Assume symbol is not defined */
2390 ip = CPP_BUFFER (pfile);
2391 SKIP_WHITE_SPACE (ip->cur);
2392 if (*ip->cur == '(')
2395 ip->cur++; /* Skip over the paren */
2396 SKIP_WHITE_SPACE (ip->cur);
2399 if (!is_idstart[*ip->cur])
2401 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2403 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2406 if (pcp_outfile && pcp_inside_if
2407 && (hp->type == T_CONST
2408 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2409 /* Output a precondition for this macro use. */
2410 fprintf (pcp_outfile, "#define %s\n", hp->name);
2416 if (pcp_outfile && pcp_inside_if)
2418 /* Output a precondition for this macro use */
2419 U_CHAR *cp = ip->bufp;
2420 fprintf (pcp_outfile, "#undef ");
2421 while (is_idchar[*cp]) /* Ick! */
2422 fputc (*cp++, pcp_outfile);
2423 putc ('\n', pcp_outfile);
2426 while (is_idchar[*ip->cur])
2428 SKIP_WHITE_SPACE (ip->cur);
2431 if (*ip->cur != ')')
2439 cpp_error (pfile, "`defined' without an identifier");
2443 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2447 CPP_RESERVE (pfile, len + 1);
2448 CPP_PUTS_Q (pfile, buf, len);
2449 CPP_NUL_TERMINATE_Q (pfile);
2454 /* Write out a #define command for the special named MACRO_NAME
2455 to PFILE's token_buffer. */
2458 dump_special_to_buffer (pfile, macro_name)
2462 static char define_directive[] = "#define ";
2463 int macro_name_length = strlen (macro_name);
2464 output_line_command (pfile, 0, same_file);
2465 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2466 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2467 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2468 CPP_PUTC_Q (pfile, ' ');
2469 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2470 CPP_PUTC (pfile, '\n');
2473 /* Initialize the built-in macros. */
2476 initialize_builtins (pfile)
2479 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2480 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2481 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2482 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2483 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2484 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2485 #ifndef NO_BUILTIN_SIZE_TYPE
2486 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2488 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2489 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2491 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2492 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2493 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2494 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2495 if (!CPP_TRADITIONAL (pfile))
2496 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2497 if (CPP_OPTIONS (pfile)->objc)
2498 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2499 /* This is supplied using a -D by the compiler driver
2500 so that it is present only when truly compiling with GNU C. */
2501 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2503 if (CPP_OPTIONS (pfile)->debug_output)
2505 dump_special_to_buffer (pfile, "__BASE_FILE__");
2506 dump_special_to_buffer (pfile, "__VERSION__");
2507 #ifndef NO_BUILTIN_SIZE_TYPE
2508 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2510 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2511 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2513 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2514 dump_special_to_buffer (pfile, "__DATE__");
2515 dump_special_to_buffer (pfile, "__TIME__");
2516 if (!CPP_TRADITIONAL (pfile))
2517 dump_special_to_buffer (pfile, "__STDC__");
2518 if (CPP_OPTIONS (pfile)->objc)
2519 dump_special_to_buffer (pfile, "__OBJC__");
2523 /* Return 1 iff a token ending in C1 followed directly by a token C2
2524 could cause mis-tokenization. */
2527 unsafe_chars (c1, c2)
2533 if (c2 == c1 || c2 == '=')
2537 case '0': case '1': case '2': case '3': case '4':
2538 case '5': case '6': case '7': case '8': case '9':
2539 case 'e': case 'E': case 'p': case 'P':
2540 if (c2 == '-' || c2 == '+')
2541 return 1; /* could extend a pre-processing number */
2544 if (c2 == '\'' || c2 == '\"')
2545 return 1; /* Could turn into L"xxx" or L'xxx'. */
2549 case 'a': case 'b': case 'c': case 'd': case 'f':
2550 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2551 case 'm': case 'n': case 'o': case 'q': case 'r':
2552 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2554 case 'A': case 'B': case 'C': case 'D': case 'F':
2555 case 'G': case 'H': case 'I': case 'J': case 'K':
2556 case 'M': case 'N': case 'O': case 'Q': case 'R':
2557 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2559 /* We're in the middle of either a name or a pre-processing number. */
2560 return (is_idchar[c2] || c2 == '.');
2561 case '<': case '>': case '!': case '%': case '#': case ':':
2562 case '^': case '&': case '|': case '*': case '/': case '=':
2563 return (c2 == c1 || c2 == '=');
2568 /* Expand a macro call.
2569 HP points to the symbol that is the macro being called.
2570 Put the result of expansion onto the input stack
2571 so that subsequent input by our caller will use it.
2573 If macro wants arguments, caller has already verified that
2574 an argument list follows; arguments come from the input stack. */
2577 macroexpand (pfile, hp)
2582 DEFINITION *defn = hp->value.defn;
2583 register U_CHAR *xbuf;
2584 long start_line, start_column;
2586 struct argdata *args;
2587 long old_written = CPP_WRITTEN (pfile);
2589 int start_line = instack[indepth].lineno;
2591 int rest_args, rest_zero;
2595 CHECK_DEPTH (return;);
2599 /* This macro is being used inside a #if, which means it must be */
2600 /* recorded as a precondition. */
2601 if (pcp_inside_if && pcp_outfile && defn->predefined)
2602 dump_single_macro (hp, pcp_outfile);
2605 pfile->output_escapes++;
2606 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2608 nargs = defn->nargs;
2612 enum cpp_token token;
2614 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2616 for (i = 0; i < nargs; i++)
2618 args[i].raw = args[i].expanded = 0;
2619 args[i].raw_length = 0;
2620 args[i].expand_length = args[i].stringified_length = -1;
2621 args[i].use_count = 0;
2624 /* Parse all the macro args that are supplied. I counts them.
2625 The first NARGS args are stored in ARGS.
2626 The rest are discarded. If rest_args is set then we assume
2627 macarg absorbed the rest of the args. */
2631 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2636 if (i < nargs || (nargs == 0 && i == 0))
2638 /* if we are working on last arg which absorbs rest of args... */
2639 if (i == nargs - 1 && defn->rest_args)
2641 args[i].raw = CPP_WRITTEN (pfile);
2642 token = macarg (pfile, rest_args);
2643 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2644 args[i].newlines = 0; /* FIXME */
2647 token = macarg (pfile, 0);
2648 if (token == CPP_EOF || token == CPP_POP)
2650 cpp_error_with_line (pfile, start_line, start_column,
2651 "unterminated macro call");
2655 } while (token == CPP_COMMA);
2657 /* If we got one arg but it was just whitespace, call that 0 args. */
2660 register U_CHAR *bp = ARG_BASE + args[0].raw;
2661 register U_CHAR *lim = bp + args[0].raw_length;
2662 /* cpp.texi says for foo ( ) we provide one argument.
2663 However, if foo wants just 0 arguments, treat this as 0. */
2665 while (bp != lim && is_space[*bp]) bp++;
2670 /* Don't output an error message if we have already output one for
2671 a parse error above. */
2673 if (nargs == 0 && i > 0)
2675 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2679 /* traditional C allows foo() if foo wants one argument. */
2680 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2682 /* the rest args token is allowed to absorb 0 tokens */
2683 else if (i == nargs - 1 && defn->rest_args)
2686 cpp_error (pfile, "macro `%s' used without args", hp->name);
2688 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2690 cpp_error (pfile, "macro `%s' used with only %d args",
2696 "macro `%s' used with too many (%d) args", hp->name, i);
2700 /* If macro wants zero args, we parsed the arglist for checking only.
2701 Read directly from the macro definition. */
2704 xbuf = defn->expansion;
2705 xbuf_len = defn->length;
2709 register U_CHAR *exp = defn->expansion;
2710 register int offset; /* offset in expansion,
2711 copied a piece at a time */
2712 register int totlen; /* total amount of exp buffer filled so far */
2714 register struct reflist *ap, *last_ap;
2716 /* Macro really takes args. Compute the expansion of this call. */
2718 /* Compute length in characters of the macro's expansion.
2719 Also count number of times each arg is used. */
2720 xbuf_len = defn->length;
2721 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2725 register struct argdata *arg = &args[ap->argno];
2726 /* Stringify if it hasn't already been */
2727 if (arg->stringified_length < 0)
2729 int arglen = arg->raw_length;
2733 /* Initially need_space is -1. Otherwise, 1 means the
2734 previous character was a space, but we suppressed it;
2735 0 means the previous character was a non-space. */
2736 int need_space = -1;
2738 arg->stringified = CPP_WRITTEN (pfile);
2739 if (!CPP_TRADITIONAL (pfile))
2740 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2741 for (; i < arglen; i++)
2743 c = (ARG_BASE + arg->raw)[i];
2747 /* Internal sequences of whitespace are replaced by
2748 one space except within an string or char token.*/
2751 if (CPP_WRITTEN (pfile) > arg->stringified
2752 && (CPP_PWRITTEN (pfile))[-1] == '@')
2754 /* "@ " escape markers are removed */
2755 CPP_ADJUST_WRITTEN (pfile, -1);
2758 if (need_space == 0)
2762 else if (need_space > 0)
2763 CPP_PUTC (pfile, ' ');
2778 else if (c == '\"' || c == '\'')
2782 /* Escape these chars */
2783 if (c == '\"' || (in_string && c == '\\'))
2784 CPP_PUTC (pfile, '\\');
2786 CPP_PUTC (pfile, c);
2789 CPP_RESERVE (pfile, 4);
2790 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2792 CPP_ADJUST_WRITTEN (pfile, 4);
2795 if (!CPP_TRADITIONAL (pfile))
2796 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2797 arg->stringified_length
2798 = CPP_WRITTEN (pfile) - arg->stringified;
2800 xbuf_len += args[ap->argno].stringified_length;
2802 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2803 /* Add 4 for two newline-space markers to prevent
2804 token concatenation. */
2805 xbuf_len += args[ap->argno].raw_length + 4;
2808 /* We have an ordinary (expanded) occurrence of the arg.
2809 So compute its expansion, if we have not already. */
2810 if (args[ap->argno].expand_length < 0)
2812 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2813 cpp_expand_to_buffer (pfile,
2814 ARG_BASE + args[ap->argno].raw,
2815 args[ap->argno].raw_length);
2817 args[ap->argno].expand_length
2818 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2821 /* Add 4 for two newline-space markers to prevent
2822 token concatenation. */
2823 xbuf_len += args[ap->argno].expand_length + 4;
2825 if (args[ap->argno].use_count < 10)
2826 args[ap->argno].use_count++;
2829 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2831 /* Generate in XBUF the complete expansion
2832 with arguments substituted in.
2833 TOTLEN is the total size generated so far.
2834 OFFSET is the index in the definition
2835 of where we are copying from. */
2836 offset = totlen = 0;
2837 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2838 last_ap = ap, ap = ap->next)
2840 register struct argdata *arg = &args[ap->argno];
2841 int count_before = totlen;
2843 /* Add chars to XBUF. */
2844 for (i = 0; i < ap->nchars; i++, offset++)
2845 xbuf[totlen++] = exp[offset];
2847 /* If followed by an empty rest arg with concatenation,
2848 delete the last run of nonwhite chars. */
2849 if (rest_zero && totlen > count_before
2850 && ((ap->rest_args && ap->raw_before)
2851 || (last_ap != NULL && last_ap->rest_args
2852 && last_ap->raw_after)))
2854 /* Delete final whitespace. */
2855 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2858 /* Delete the nonwhites before them. */
2859 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2863 if (ap->stringify != 0)
2865 bcopy (ARG_BASE + arg->stringified,
2866 xbuf + totlen, arg->stringified_length);
2867 totlen += arg->stringified_length;
2869 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2871 U_CHAR *p1 = ARG_BASE + arg->raw;
2872 U_CHAR *l1 = p1 + arg->raw_length;
2875 while (p1 != l1 && is_space[*p1]) p1++;
2876 while (p1 != l1 && is_idchar[*p1])
2877 xbuf[totlen++] = *p1++;
2878 /* Delete any no-reexpansion marker that follows
2879 an identifier at the beginning of the argument
2880 if the argument is concatenated with what precedes it. */
2881 if (p1[0] == '@' && p1[1] == '-')
2886 /* Arg is concatenated after: delete trailing whitespace,
2887 whitespace markers, and no-reexpansion markers. */
2890 if (is_space[l1[-1]]) l1--;
2891 else if (l1[-1] == '-')
2893 U_CHAR *p2 = l1 - 1;
2894 /* If a `-' is preceded by an odd number of newlines then it
2895 and the last newline are a no-reexpansion marker. */
2896 while (p2 != p1 && p2[-1] == '\n') p2--;
2897 if ((l1 - 1 - p2) & 1) {
2906 bcopy (p1, xbuf + totlen, l1 - p1);
2911 U_CHAR *expanded = ARG_BASE + arg->expanded;
2912 if (!ap->raw_before && totlen > 0 && arg->expand_length
2913 && !CPP_TRADITIONAL(pfile)
2914 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2916 xbuf[totlen++] = '@';
2917 xbuf[totlen++] = ' ';
2920 bcopy (expanded, xbuf + totlen, arg->expand_length);
2921 totlen += arg->expand_length;
2923 if (!ap->raw_after && totlen > 0 && offset < defn->length
2924 && !CPP_TRADITIONAL(pfile)
2925 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2927 xbuf[totlen++] = '@';
2928 xbuf[totlen++] = ' ';
2931 /* If a macro argument with newlines is used multiple times,
2932 then only expand the newlines once. This avoids creating
2933 output lines which don't correspond to any input line,
2934 which confuses gdb and gcov. */
2935 if (arg->use_count > 1 && arg->newlines > 0)
2937 /* Don't bother doing change_newlines for subsequent
2941 = change_newlines (expanded, arg->expand_length);
2945 if (totlen > xbuf_len)
2949 /* if there is anything left of the definition
2950 after handling the arg list, copy that in too. */
2952 for (i = offset; i < defn->length; i++)
2954 /* if we've reached the end of the macro */
2957 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2958 && last_ap->raw_after))
2959 xbuf[totlen++] = exp[i];
2967 pfile->output_escapes--;
2969 /* Now put the expansion on the input stack
2970 so our caller will commence reading from it. */
2971 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2972 CPP_BUFFER (pfile)->has_escapes = 1;
2974 /* Pop the space we've used in the token_buffer for argument expansion. */
2975 CPP_SET_WRITTEN (pfile, old_written);
2977 /* Recursive macro use sometimes works traditionally.
2978 #define foo(x,y) bar (x (y,0), y)
2981 if (!CPP_TRADITIONAL (pfile))
2982 hp->type = T_DISABLED;
2986 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2988 register U_CHAR *xbuf;
2992 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2995 mbuf->cleanup = macro_cleanup;
2998 /* The first chars of the expansion should be a "@ " added by
2999 collect_expansion. This is to prevent accidental token-pasting
3000 between the text preceding the macro invocation, and the macro
3003 We would like to avoid adding unneeded spaces (for the sake of
3004 tools that use cpp, such as imake). In some common cases we can
3005 tell that it is safe to omit the space.
3007 The character before the macro invocation cannot have been an
3008 idchar (or else it would have been pasted with the idchars of
3009 the macro name). Therefore, if the first non-space character
3010 of the expansion is an idchar, we do not need the extra space
3011 to prevent token pasting.
3013 Also, we don't need the extra space if the first char is '(',
3014 or some other (less common) characters. */
3016 if (xbuf[0] == '@' && xbuf[1] == ' '
3017 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3018 || xbuf[2] == '\"'))
3022 /* Like cpp_get_token, except that it does not read past end-of-line.
3023 Also, horizontal space is skipped, and macros are popped. */
3025 static enum cpp_token
3026 get_directive_token (pfile)
3031 long old_written = CPP_WRITTEN (pfile);
3032 enum cpp_token token;
3033 cpp_skip_hspace (pfile);
3034 if (PEEKC () == '\n')
3036 token = cpp_get_token (pfile);
3040 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3042 /* ... else fall though ... */
3043 case CPP_HSPACE: case CPP_COMMENT:
3044 CPP_SET_WRITTEN (pfile, old_written);
3052 /* Handle #include and #import.
3053 This function expects to see "fname" or <fname> on the input.
3055 The input is normally in part of the output_buffer following
3056 CPP_WRITTEN, and will get overwritten by output_line_command.
3057 I.e. in input file specification has been popped by handle_directive.
3061 do_include (pfile, keyword, unused1, unused2)
3063 struct directive *keyword;
3064 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3066 int importing = (keyword->type == T_IMPORT);
3067 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3068 char *fname; /* Dynamically allocated fname buffer */
3070 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3071 enum cpp_token token;
3073 /* Chain of dirs to search */
3074 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3075 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3076 struct file_name_list *searchptr = 0;
3077 long old_written = CPP_WRITTEN (pfile);
3081 int f; /* file number */
3083 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3090 f= -1; /* JF we iz paranoid! */
3092 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3095 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
3097 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
3100 if (importing && CPP_OPTIONS (pfile)->warn_import
3101 && !CPP_OPTIONS (pfile)->inhibit_warnings
3102 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3104 pfile->import_warning = 1;
3105 cpp_warning (pfile, "using `#import' is not recommended");
3106 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3107 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3108 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3109 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3110 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3111 fprintf (stderr, " ... <real contents of file> ...\n");
3112 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3113 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3114 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3115 fprintf (stderr, "when it is equipped with such a conditional.\n");
3118 pfile->parsing_include_directive++;
3119 token = get_directive_token (pfile);
3120 pfile->parsing_include_directive--;
3122 if (token == CPP_STRING)
3124 /* FIXME - check no trailing garbage */
3125 fbeg = pfile->token_buffer + old_written + 1;
3126 fend = CPP_PWRITTEN (pfile) - 1;
3127 if (fbeg[-1] == '<')
3130 /* If -I-, start with the first -I dir after the -I-. */
3131 if (CPP_OPTIONS (pfile)->first_bracket_include)
3132 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3134 /* If -I- was specified, don't search current dir, only spec'd ones. */
3135 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3137 cpp_buffer *fp = CPP_BUFFER (pfile);
3138 /* We have "filename". Figure out directory this source
3139 file is coming from and put it on the front of the list. */
3141 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3146 if ((nam = fp->nominal_fname) != NULL)
3148 /* Found a named file. Figure out dir of the file,
3149 and put it in front of the search list. */
3150 dsp[0].next = search_start;
3153 ep = rindex (nam, '/');
3155 ep = rindex (nam, ']');
3156 if (ep == NULL) ep = rindex (nam, '>');
3157 if (ep == NULL) ep = rindex (nam, ':');
3158 if (ep != NULL) ep++;
3163 dsp[0].fname = (char *) alloca (n + 1);
3164 strncpy (dsp[0].fname, nam, n);
3165 dsp[0].fname[n] = '\0';
3166 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3167 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3171 dsp[0].fname = 0; /* Current directory */
3173 dsp[0].got_name_map = 0;
3180 else if (token == CPP_NAME)
3183 * Support '#include xyz' like VAX-C to allow for easy use of all the
3184 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3185 * code from case '<' is repeated here) and generates a warning.
3188 "VAX-C-style include specification found, use '#include <filename.h>' !");
3190 /* If -I-, start with the first -I dir after the -I-. */
3191 if (CPP_OPTIONS (pfile)->first_bracket_include)
3192 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3193 fbeg = pfile->token_buffer + old_written;
3194 fend = CPP_PWRITTEN (pfile);
3200 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3201 CPP_SET_WRITTEN (pfile, old_written);
3202 skip_rest_of_line (pfile);
3208 token = get_directive_token (pfile);
3209 if (token != CPP_VSPACE)
3211 cpp_error (pfile, "junk at end of `#include'");
3212 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3213 token = get_directive_token (pfile);
3216 /* For #include_next, skip in the search path
3217 past the dir in which the containing file was found. */
3220 cpp_buffer *fp = CPP_BUFFER (pfile);
3221 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3222 if (fp->fname != NULL)
3224 /* fp->dir is null if the containing file was specified with
3225 an absolute file name. In that case, don't skip anything. */
3226 if (fp->dir == SELF_DIR_DUMMY)
3227 search_start = CPP_OPTIONS (pfile)->include;
3229 search_start = fp->dir->next;
3234 CPP_SET_WRITTEN (pfile, old_written);
3240 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3244 /* Allocate this permanently, because it gets stored in the definitions
3246 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3247 /* + 2 above for slash and terminating null. */
3248 /* + 2 added for '.h' on VMS (to support '#include filename') */
3250 /* If specified file name is absolute, just open it. */
3253 strncpy (fname, fbeg, flen);
3255 if (redundant_include_p (pfile, fname))
3258 f = lookup_import (pfile, fname, NULL_PTR);
3260 f = open_include_file (pfile, fname, NULL_PTR);
3262 return 0; /* Already included this file */
3264 /* Search directory path, trying to open the file.
3265 Copy each filename tried into FNAME. */
3267 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3268 if (searchptr->fname) {
3269 /* The empty string in a search path is ignored.
3270 This makes it possible to turn off entirely
3271 a standard piece of the list. */
3272 if (searchptr->fname[0] == 0)
3274 strcpy (fname, searchptr->fname);
3275 strcat (fname, "/");
3276 fname[strlen (fname) + flen] = 0;
3280 strncat (fname, fbeg, flen);
3282 /* Change this 1/2 Unix 1/2 VMS file specification into a
3283 full VMS file specification */
3284 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3285 /* Fix up the filename */
3286 hack_vms_include_specification (fname);
3288 /* This is a normal VMS filespec, so use it unchanged. */
3289 strncpy (fname, fbeg, flen);
3291 /* if it's '#include filename', add the missing .h */
3292 if (index(fname,'.')==NULL) {
3293 strcat (fname, ".h");
3297 /* ??? There are currently 3 separate mechanisms for avoiding processing
3298 of redundant include files: #import, #pragma once, and
3299 redundant_include_p. It would be nice if they were unified. */
3300 if (redundant_include_p (pfile, fname))
3303 f = lookup_import (pfile, fname, searchptr);
3305 f = open_include_file (pfile, fname, searchptr);
3307 return 0; /* Already included this file */
3309 else if (f == -1 && errno == EACCES)
3310 cpp_warning (pfile, "Header file %s exists, but is not readable",
3320 /* A file that was not found. */
3321 strncpy (fname, fbeg, flen);
3323 /* If generating dependencies and -MG was specified, we assume missing
3324 files are leaf files, living in the same directory as the source file
3325 or other similar place; these missing files may be generated from
3326 other files and may not exist yet (eg: y.tab.h). */
3328 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3329 && CPP_PRINT_DEPS (pfile)
3330 > (angle_brackets || (pfile->system_include_depth > 0)))
3332 /* If it was requested as a system header file,
3333 then assume it belongs in the first place to look for such. */
3336 for (searchptr = search_start; searchptr;
3337 searchptr = searchptr->next)
3339 if (searchptr->fname)
3343 if (searchptr->fname[0] == 0)
3345 p = (char *) alloca (strlen (searchptr->fname)
3346 + strlen (fname) + 2);
3347 strcpy (p, searchptr->fname);
3350 deps_output (pfile, p, ' ');
3357 /* Otherwise, omit the directory, as if the file existed
3358 in the directory with the source. */
3359 deps_output (pfile, fname, ' ');
3362 /* If -M was specified, and this header file won't be added to the
3363 dependency list, then don't count this as an error, because we can
3364 still produce correct output. Otherwise, we can't produce correct
3365 output, because there may be dependencies we need inside the missing
3366 file, and we don't know what directory this missing file exists in.*/
3367 else if (CPP_PRINT_DEPS (pfile)
3368 && (CPP_PRINT_DEPS (pfile)
3369 <= (angle_brackets || (pfile->system_include_depth > 0))))
3370 cpp_warning (pfile, "No include path in which to find %s", fname);
3371 else if (search_start)
3372 cpp_error_from_errno (pfile, fname);
3374 cpp_error (pfile, "No include path in which to find %s", fname);
3377 /* Check to see if this include file is a once-only include file.
3380 struct file_name_list *ptr;
3382 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3383 if (!strcmp (ptr->fname, fname)) {
3385 return 0; /* This file was once'd. */
3389 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3390 if (!strcmp (ptr->fname, fname))
3391 break; /* This file was included before. */
3395 /* This is the first time for this file. */
3396 /* Add it to list of files included. */
3398 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3399 ptr->control_macro = 0;
3400 ptr->c_system_include_path = 0;
3401 ptr->next = pfile->all_include_files;
3402 pfile->all_include_files = ptr;
3403 ptr->fname = savestring (fname);
3404 ptr->got_name_map = 0;
3406 /* For -M, add this file to the dependencies. */
3407 if (CPP_PRINT_DEPS (pfile)
3408 > (angle_brackets || (pfile->system_include_depth > 0)))
3409 deps_output (pfile, fname, ' ');
3412 /* Handle -H option. */
3413 if (CPP_OPTIONS(pfile)->print_include_names)
3415 cpp_buffer *buf = CPP_BUFFER (pfile);
3416 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3418 fprintf (stderr, "%s\n", fname);
3422 pfile->system_include_depth++;
3424 /* Actually process the file. */
3426 /* Record file on "seen" list for #import. */
3427 add_import (pfile, f, fname);
3429 pcftry = (char *) alloca (strlen (fname) + 30);
3441 sprintf (pcftry, "%s%d", fname, pcfnum++);
3443 pcf = open (pcftry, O_RDONLY, 0666);
3449 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3451 || stat_f.st_dev != s.st_dev)
3453 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3454 /* Don't need it any more. */
3459 /* Don't need it at all. */
3464 } while (pcf != -1 && !pcfbuf);
3468 /* Actually process the file */
3469 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3471 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3472 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3474 output_line_command (pfile, 0, enter_file);
3475 pfile->only_seen_white = 2;
3479 pfile->system_include_depth--;
3484 /* Return nonzero if there is no need to include file NAME
3485 because it has already been included and it contains a conditional
3486 to make a repeated include do nothing. */
3489 redundant_include_p (pfile, name)
3493 struct file_name_list *l = pfile->all_include_files;
3494 for (; l; l = l->next)
3495 if (! strcmp (name, l->fname)
3497 && cpp_lookup (pfile, l->control_macro, -1, -1))
3502 /* Return nonzero if the given FILENAME is an absolute pathname which
3503 designates a file within one of the known "system" include file
3504 directories. We assume here that if the given FILENAME looks like
3505 it is the name of a file which resides either directly in a "system"
3506 include file directory, or within any subdirectory thereof, then the
3507 given file must be a "system" include file. This function tells us
3508 if we should suppress pedantic errors/warnings for the given FILENAME.
3510 The value is 2 if the file is a C-language system header file
3511 for which C++ should (on most systems) assume `extern "C"'. */
3514 is_system_include (pfile, filename)
3516 register char *filename;
3518 struct file_name_list *searchptr;
3520 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3521 searchptr = searchptr->next)
3522 if (searchptr->fname) {
3523 register char *sys_dir = searchptr->fname;
3524 register unsigned length = strlen (sys_dir);
3526 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3528 if (searchptr->c_system_include_path)
3539 * Install a name in the assertion hash table.
3541 * If LEN is >= 0, it is the length of the name.
3542 * Otherwise, compute the length by scanning the entire name.
3544 * If HASH is >= 0, it is the precomputed hash code.
3545 * Otherwise, compute the hash code.
3548 static ASSERTION_HASHNODE *
3549 assertion_install (pfile, name, len, hash)
3555 register ASSERTION_HASHNODE *hp;
3556 register int i, bucket;
3557 register U_CHAR *p, *q;
3559 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3560 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3562 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3563 hp->next = pfile->assertion_hashtab[bucket];
3564 pfile->assertion_hashtab[bucket] = hp;
3566 if (hp->next != NULL)
3567 hp->next->prev = hp;
3570 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3573 for (i = 0; i < len; i++)
3579 * find the most recent hash node for name "name" (ending with first
3580 * non-identifier char) installed by install
3582 * If LEN is >= 0, it is the length of the name.
3583 * Otherwise, compute the length by scanning the entire name.
3585 * If HASH is >= 0, it is the precomputed hash code.
3586 * Otherwise, compute the hash code.
3589 static ASSERTION_HASHNODE *
3590 assertion_lookup (pfile, name, len, hash)
3596 register ASSERTION_HASHNODE *bucket;
3598 bucket = pfile->assertion_hashtab[hash];
3600 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3602 bucket = bucket->next;
3608 delete_assertion (hp)
3609 ASSERTION_HASHNODE *hp;
3611 struct tokenlist_list *tail;
3612 if (hp->prev != NULL)
3613 hp->prev->next = hp->next;
3614 if (hp->next != NULL)
3615 hp->next->prev = hp->prev;
3617 for (tail = hp->value; tail; )
3619 struct tokenlist_list *next = tail->next;
3620 free_token_list (tail->tokens);
3625 /* Make sure that the bucket chain header that
3626 the deleted guy was on points to the right thing afterwards. */
3627 if (hp == *hp->bucket_hdr)
3628 *hp->bucket_hdr = hp->next;
3633 /* Convert a character string literal into a nul-terminated string.
3634 The input string is [IN ... LIMIT).
3635 The result is placed in RESULT. RESULT can be the same as IN.
3636 The value returned in the end of the string written to RESULT,
3637 or NULL on error. */
3640 convert_string (pfile, result, in, limit, handle_escapes)
3642 register U_CHAR *result, *in, *limit;
3662 char *bpc = (char *) in;
3663 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3664 in = (U_CHAR *) bpc;
3666 *result++ = (U_CHAR)c;
3669 /* else fall through */
3679 * interpret #line command. Remembers previously seen fnames
3680 * in its very own hash table.
3682 #define FNAME_HASHSIZE 37
3685 do_line (pfile, keyword, unused1, unused2)
3687 struct directive *keyword ATTRIBUTE_UNUSED;
3688 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3690 cpp_buffer *ip = CPP_BUFFER (pfile);
3692 long old_written = CPP_WRITTEN (pfile);
3693 enum file_change_code file_change = same_file;
3694 enum cpp_token token;
3696 token = get_directive_token (pfile);
3698 if (token != CPP_NUMBER
3699 || !ISDIGIT(pfile->token_buffer[old_written]))
3701 cpp_error (pfile, "invalid format `#line' command");
3702 goto bad_line_directive;
3705 /* The Newline at the end of this line remains to be processed.
3706 To put the next line at the specified line number,
3707 we must store a line number now that is one less. */
3708 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3709 CPP_SET_WRITTEN (pfile, old_written);
3711 /* NEW_LINENO is one less than the actual line number here. */
3712 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3713 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3715 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3716 if (PEEKC() && !is_space[PEEKC()]) {
3717 cpp_error (pfile, "invalid format `#line' command");
3718 goto bad_line_directive;
3722 token = get_directive_token (pfile);
3724 if (token == CPP_STRING) {
3725 U_CHAR *fname = pfile->token_buffer + old_written;
3727 static HASHNODE *fname_table[FNAME_HASHSIZE];
3728 HASHNODE *hp, **hash_bucket;
3733 /* Turn the file name, which is a character string literal,
3734 into a null-terminated string. Do this in place. */
3735 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3736 if (end_name == NULL)
3738 cpp_error (pfile, "invalid format `#line' command");
3739 goto bad_line_directive;
3742 fname_length = end_name - fname;
3744 num_start = CPP_WRITTEN (pfile);
3745 token = get_directive_token (pfile);
3746 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3747 p = pfile->token_buffer + num_start;
3748 if (CPP_PEDANTIC (pfile))
3749 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3751 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3753 cpp_error (pfile, "invalid format `#line' command");
3754 goto bad_line_directive;
3757 file_change = enter_file;
3759 file_change = leave_file;
3761 ip->system_header_p = 1;
3762 else /* if (*p == '4') */
3763 ip->system_header_p = 2;
3765 CPP_SET_WRITTEN (pfile, num_start);
3766 token = get_directive_token (pfile);
3767 p = pfile->token_buffer + num_start;
3768 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3769 ip->system_header_p = *p == '3' ? 1 : 2;
3770 token = get_directive_token (pfile);
3772 if (token != CPP_VSPACE) {
3773 cpp_error (pfile, "invalid format `#line' command");
3774 goto bad_line_directive;
3778 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3779 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3780 if (hp->length == fname_length
3781 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3782 ip->nominal_fname = hp->value.cpval;
3786 /* Didn't find it; cons up a new one. */
3787 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3788 hp->next = *hash_bucket;
3791 hp->length = fname_length;
3792 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3793 bcopy (fname, hp->value.cpval, fname_length);
3796 else if (token != CPP_VSPACE && token != CPP_EOF) {
3797 cpp_error (pfile, "invalid format `#line' command");
3798 goto bad_line_directive;
3801 ip->lineno = new_lineno;
3803 skip_rest_of_line (pfile);
3804 CPP_SET_WRITTEN (pfile, old_written);
3805 output_line_command (pfile, 0, file_change);
3810 * remove the definition of a symbol from the symbol table.
3811 * according to un*x /lib/cpp, it is not an error to undef
3812 * something that has no definitions, so it isn't one here either.
3816 do_undef (pfile, keyword, buf, limit)
3818 struct directive *keyword;
3819 U_CHAR *buf, *limit;
3823 U_CHAR *orig_buf = buf;
3826 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3827 if (pcp_outfile && keyword)
3828 pass_thru_directive (buf, limit, pfile, keyword);
3831 SKIP_WHITE_SPACE (buf);
3832 sym_length = check_macro_name (pfile, buf, "macro");
3834 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3836 /* If we are generating additional info for debugging (with -g) we
3837 need to pass through all effective #undef commands. */
3838 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3839 pass_thru_directive (orig_buf, limit, pfile, keyword);
3840 if (hp->type != T_MACRO)
3841 cpp_warning (pfile, "undefining `%s'", hp->name);
3845 if (CPP_PEDANTIC (pfile)) {
3847 SKIP_WHITE_SPACE (buf);
3849 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3855 * Report an error detected by the program we are processing.
3856 * Use the text of the line in the error message.
3857 * (We use error because it prints the filename & line#.)
3861 do_error (pfile, keyword, buf, limit)
3863 struct directive *keyword ATTRIBUTE_UNUSED;
3864 U_CHAR *buf, *limit;
3866 int length = limit - buf;
3867 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3868 bcopy (buf, copy, length);
3870 SKIP_WHITE_SPACE (copy);
3871 cpp_error (pfile, "#error %s", copy);
3876 * Report a warning detected by the program we are processing.
3877 * Use the text of the line in the warning message, then continue.
3878 * (We use error because it prints the filename & line#.)
3882 do_warning (pfile, keyword, buf, limit)
3884 struct directive *keyword ATTRIBUTE_UNUSED;
3885 U_CHAR *buf, *limit;
3887 int length = limit - buf;
3888 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3889 bcopy (buf, copy, length);
3891 SKIP_WHITE_SPACE (copy);
3893 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3894 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
3896 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3897 if -pedantic-errors is given, #warning should cause an error. */
3898 cpp_pedwarn (pfile, "#warning %s", copy);
3902 /* Remember the name of the current file being read from so that we can
3903 avoid ever including it again. */
3906 do_once (pfile, keyword, unused1, unused2)
3908 struct directive *keyword ATTRIBUTE_UNUSED;
3909 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3911 cpp_buffer *ip = NULL;
3912 struct file_name_list *new;
3914 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3916 if (ip == CPP_NULL_BUFFER (pfile))
3918 if (ip->fname != NULL)
3923 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3924 new->next = pfile->dont_repeat_files;
3925 pfile->dont_repeat_files = new;
3926 new->fname = savestring (ip->fname);
3927 new->control_macro = 0;
3928 new->got_name_map = 0;
3929 new->c_system_include_path = 0;
3934 /* Report program identification. */
3937 do_ident (pfile, keyword, buf, limit)
3939 struct directive *keyword ATTRIBUTE_UNUSED;
3940 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
3942 /* long old_written = CPP_WRITTEN (pfile);*/
3944 /* Allow #ident in system headers, since that's not user's fault. */
3945 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3946 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3948 /* Leave rest of line to be read by later calls to cpp_get_token. */
3953 /* #pragma and its argument line have already been copied to the output file.
3954 Just check for some recognized pragmas that need validation here. */
3957 do_pragma (pfile, keyword, buf, limit)
3959 struct directive *keyword ATTRIBUTE_UNUSED;
3960 U_CHAR *buf, *limit ATTRIBUTE_UNUSED;
3962 while (*buf == ' ' || *buf == '\t')
3964 if (!strncmp (buf, "once", 4)) {
3965 /* Allow #pragma once in system headers, since that's not the user's
3967 if (!CPP_BUFFER (pfile)->system_header_p)
3968 cpp_warning (pfile, "`#pragma once' is obsolete");
3969 do_once (pfile, NULL, NULL, NULL);
3972 if (!strncmp (buf, "implementation", 14)) {
3973 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3974 been included yet. */
3975 struct file_name_list *ptr;
3976 U_CHAR *p = buf + 14, *fname, *inc_fname;
3978 SKIP_WHITE_SPACE (p);
3979 if (*p == '\n' || *p != '\"')
3983 p = (U_CHAR *) index (fname, '\"');
3984 fname_len = p != NULL ? p - fname : strlen (fname);
3986 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3987 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
3988 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
3989 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
3991 "`#pragma implementation' for `%s' appears after file is included",
4000 /* This was a fun hack, but #pragma seems to start to be useful.
4001 By failing to recognize it, we pass it through unchanged to cc1. */
4004 * the behavior of the #pragma directive is implementation defined.
4005 * this implementation defines it as follows.
4012 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4015 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4017 execl ("/usr/games/hack", "#pragma", 0);
4018 execl ("/usr/games/rogue", "#pragma", 0);
4019 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4020 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4022 fatal ("You are in a maze of twisty compiler features, all different");
4026 #ifdef SCCS_DIRECTIVE
4027 /* Just ignore #sccs, on systems where we define it at all. */
4030 do_sccs (pfile, keyword, buf, limit)
4032 struct directive *keyword ATTRIBUTE_UNUSED;
4033 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4035 if (CPP_PEDANTIC (pfile))
4036 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4042 * handle #if command by
4043 * 1) inserting special `defined' keyword into the hash table
4044 * that gets turned into 0 or 1 by special_symbol (thus,
4045 * if the luser has a symbol called `defined' already, it won't
4046 * work inside the #if command)
4047 * 2) rescan the input into a temporary output buffer
4048 * 3) pass the output buffer to the yacc parser and collect a value
4049 * 4) clean up the mess left from steps 1 and 2.
4050 * 5) call conditional_skip to skip til the next #endif (etc.),
4051 * or not, depending on the value from step 3.
4055 do_if (pfile, keyword, buf, limit)
4057 struct directive *keyword ATTRIBUTE_UNUSED;
4058 U_CHAR *buf, *limit;
4060 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4061 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4066 * handle a #elif directive by not changing if_stack either.
4067 * see the comment above do_else.
4071 do_elif (pfile, keyword, buf, limit)
4073 struct directive *keyword ATTRIBUTE_UNUSED;
4074 U_CHAR *buf, *limit;
4076 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4077 cpp_error (pfile, "`#elif' not within a conditional");
4080 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4081 cpp_error (pfile, "`#elif' after `#else'");
4083 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4085 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4086 && strcmp (pfile->if_stack->fname,
4087 CPP_BUFFER (pfile)->nominal_fname) != 0)
4088 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4089 fprintf (stderr, ")\n");
4091 pfile->if_stack->type = T_ELIF;
4094 if (pfile->if_stack->if_succeeded)
4095 skip_if_group (pfile, 0);
4097 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4099 skip_if_group (pfile, 0);
4101 ++pfile->if_stack->if_succeeded; /* continue processing input */
4102 output_line_command (pfile, 1, same_file);
4109 * evaluate a #if expression in BUF, of length LENGTH,
4110 * then parse the result as a C expression and return the value as an int.
4113 static HOST_WIDE_INT
4114 eval_if_expression (pfile, buf, length)
4116 U_CHAR *buf ATTRIBUTE_UNUSED;
4117 int length ATTRIBUTE_UNUSED;
4119 HASHNODE *save_defined;
4120 HOST_WIDE_INT value;
4121 long old_written = CPP_WRITTEN (pfile);
4123 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4124 pfile->pcp_inside_if = 1;
4126 value = cpp_parse_expr (pfile);
4127 pfile->pcp_inside_if = 0;
4128 delete_macro (save_defined); /* clean up special symbol */
4130 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4136 * routine to handle ifdef/ifndef. Try to look up the symbol,
4137 * then do or don't skip to the #endif/#else/#elif depending
4138 * on what directive is actually being processed.
4142 do_xifdef (pfile, keyword, unused1, unused2)
4144 struct directive *keyword;
4145 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
4148 cpp_buffer *ip = CPP_BUFFER (pfile);
4151 enum cpp_token token;
4152 int start_of_file = 0;
4153 U_CHAR *control_macro = 0;
4154 int old_written = CPP_WRITTEN (pfile);
4156 /* Detect a #ifndef at start of file (not counting comments). */
4157 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4158 start_of_file = pfile->only_seen_white == 2;
4160 pfile->no_macro_expand++;
4161 token = get_directive_token (pfile);
4162 pfile->no_macro_expand--;
4164 ident = pfile->token_buffer + old_written;
4165 ident_length = CPP_WRITTEN (pfile) - old_written;
4166 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4168 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4170 skip = (keyword->type == T_IFDEF);
4171 if (! CPP_TRADITIONAL (pfile))
4172 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4174 else if (token == CPP_NAME)
4176 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4177 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4178 if (start_of_file && !skip)
4180 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4181 bcopy (ident, control_macro, ident_length + 1);
4186 skip = (keyword->type == T_IFDEF);
4187 if (! CPP_TRADITIONAL (pfile))
4188 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4191 if (!CPP_TRADITIONAL (pfile))
4193 cpp_skip_hspace (pfile);
4195 if (c != EOF && c != '\n')
4196 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4198 skip_rest_of_line (pfile);
4202 /* Output a precondition for this macro. */
4203 if (hp && hp->value.defn->predefined)
4204 fprintf (pcp_outfile, "#define %s\n", hp->name);
4207 fprintf (pcp_outfile, "#undef ");
4208 while (is_idchar[*cp]) /* Ick! */
4209 fputc (*cp++, pcp_outfile);
4210 putc ('\n', pcp_outfile);
4214 conditional_skip (pfile, skip, T_IF, control_macro);
4218 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4219 If this is a #ifndef starting at the beginning of a file,
4220 CONTROL_MACRO is the macro name tested by the #ifndef.
4221 Otherwise, CONTROL_MACRO is 0. */
4224 conditional_skip (pfile, skip, type, control_macro)
4227 enum node_type type;
4228 U_CHAR *control_macro;
4230 IF_STACK_FRAME *temp;
4232 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4233 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4235 temp->lineno = CPP_BUFFER (pfile)->lineno;
4237 temp->next = pfile->if_stack;
4238 temp->control_macro = control_macro;
4239 pfile->if_stack = temp;
4241 pfile->if_stack->type = type;
4244 skip_if_group (pfile, 0);
4247 ++pfile->if_stack->if_succeeded;
4248 output_line_command (pfile, 1, same_file);
4253 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4254 * leaves input ptr at the sharp sign found.
4255 * If ANY is nonzero, return at next directive of any sort.
4259 skip_if_group (pfile, any)
4264 struct directive *kt;
4265 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4267 U_CHAR *beg_of_line = bp;
4269 register int ident_length;
4271 struct parse_marker line_start_mark;
4273 parse_set_mark (&line_start_mark, pfile);
4275 if (CPP_OPTIONS (pfile)->output_conditionals) {
4276 static char failed[] = "#failed\n";
4277 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4279 output_line_command (pfile, 1, same_file);
4283 if (CPP_OPTIONS (pfile)->output_conditionals)
4285 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4286 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4287 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4289 parse_move_mark (&line_start_mark, pfile);
4290 if (!CPP_TRADITIONAL (pfile))
4291 cpp_skip_hspace (pfile);
4295 int old_written = CPP_WRITTEN (pfile);
4296 cpp_skip_hspace (pfile);
4298 parse_name (pfile, GETC());
4299 ident_length = CPP_WRITTEN (pfile) - old_written;
4300 ident = pfile->token_buffer + old_written;
4301 pfile->limit = ident;
4303 if (ident_length == 0)
4304 goto not_a_directive;
4306 /* Handle # followed by a line number. */
4308 /* Avoid error for `###' and similar cases unless -pedantic. */
4311 for (kt = directive_table; kt->length >= 0; kt++)
4313 IF_STACK_FRAME *temp;
4314 if (ident_length == kt->length
4315 && strncmp (ident, kt->name, kt->length) == 0)
4317 /* If we are asked to return on next directive, do so now. */
4327 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4328 temp->next = pfile->if_stack;
4329 pfile->if_stack = temp;
4331 temp->lineno = CPP_BUFFER(pfile)->lineno;
4333 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4334 temp->type = kt->type;
4338 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4339 validate_else (pfile,
4340 kt->type == T_ELSE ? "#else" : "#endif");
4342 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4345 "`#%s' not within a conditional", kt->name);
4348 else if (pfile->if_stack == save_if_stack)
4349 goto done; /* found what we came for */
4351 if (kt->type != T_ENDIF)
4353 if (pfile->if_stack->type == T_ELSE)
4354 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4355 pfile->if_stack->type = kt->type;
4359 temp = pfile->if_stack;
4360 pfile->if_stack = temp->next;
4367 /* Don't let erroneous code go by. */
4368 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4369 && CPP_PEDANTIC (pfile))
4370 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4374 /* We're in the middle of a line. Skip the rest of it. */
4381 case '/': /* possible comment */
4382 c = skip_comment (pfile, NULL);
4389 old = CPP_WRITTEN (pfile);
4390 cpp_get_token (pfile);
4391 CPP_SET_WRITTEN (pfile, old);
4394 /* Char after backslash loses its special meaning. */
4395 if (PEEKC() == '\n')
4405 if (CPP_OPTIONS (pfile)->output_conditionals) {
4406 static char end_failed[] = "#endfailed\n";
4407 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4410 pfile->only_seen_white = 1;
4411 parse_goto_mark (&line_start_mark, pfile);
4412 parse_clear_mark (&line_start_mark);
4416 * handle a #else directive. Do this by just continuing processing
4417 * without changing if_stack ; this is so that the error message
4418 * for missing #endif's etc. will point to the original #if. It
4419 * is possible that something different would be better.
4423 do_else (pfile, keyword, buf, limit)
4425 struct directive *keyword ATTRIBUTE_UNUSED;
4426 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4428 cpp_buffer *ip = CPP_BUFFER (pfile);
4430 if (CPP_PEDANTIC (pfile))
4431 validate_else (pfile, "#else");
4432 skip_rest_of_line (pfile);
4434 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4435 cpp_error (pfile, "`#else' not within a conditional");
4438 /* #ifndef can't have its special treatment for containing the whole file
4439 if it has a #else clause. */
4440 pfile->if_stack->control_macro = 0;
4442 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4443 cpp_error (pfile, "`#else' after `#else'");
4444 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4445 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4446 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4447 fprintf (stderr, ")\n");
4449 pfile->if_stack->type = T_ELSE;
4452 if (pfile->if_stack->if_succeeded)
4453 skip_if_group (pfile, 0);
4455 ++pfile->if_stack->if_succeeded; /* continue processing input */
4456 output_line_command (pfile, 1, same_file);
4462 * unstack after #endif command
4466 do_endif (pfile, keyword, buf, limit)
4468 struct directive *keyword ATTRIBUTE_UNUSED;
4469 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4471 if (CPP_PEDANTIC (pfile))
4472 validate_else (pfile, "#endif");
4473 skip_rest_of_line (pfile);
4475 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4476 cpp_error (pfile, "unbalanced `#endif'");
4479 IF_STACK_FRAME *temp = pfile->if_stack;
4480 pfile->if_stack = temp->next;
4481 if (temp->control_macro != 0)
4483 /* This #endif matched a #ifndef at the start of the file.
4484 See if it is at the end of the file. */
4485 struct parse_marker start_mark;
4488 parse_set_mark (&start_mark, pfile);
4492 cpp_skip_hspace (pfile);
4497 parse_goto_mark (&start_mark, pfile);
4498 parse_clear_mark (&start_mark);
4502 /* If we get here, this #endif ends a #ifndef
4503 that contains all of the file (aside from whitespace).
4504 Arrange not to include the file again
4505 if the macro that was tested is defined.
4507 Do not do this for the top-level file in a -include or any
4508 file in a -imacros. */
4512 && ! (indepth == 1 && pfile->no_record_file)
4513 && ! (pfile->no_record_file && no_output))
4516 struct file_name_list *ifile = pfile->all_include_files;
4518 for ( ; ifile != NULL; ifile = ifile->next)
4520 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4522 ifile->control_macro = temp->control_macro;
4530 output_line_command (pfile, 1, same_file);
4535 /* When an #else or #endif is found while skipping failed conditional,
4536 if -pedantic was specified, this is called to warn about text after
4537 the command name. P points to the first char after the command name. */
4540 validate_else (pfile, directive)
4545 cpp_skip_hspace (pfile);
4547 if (c != EOF && c != '\n')
4549 "text following `%s' violates ANSI standard", directive);
4552 /* Get the next token, and add it to the text in pfile->token_buffer.
4553 Return the kind of token we got. */
4556 cpp_get_token (pfile)
4559 register int c, c2, c3;
4561 long start_line, start_column;
4562 enum cpp_token token;
4563 struct cpp_options *opts = CPP_OPTIONS (pfile);
4564 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4570 if (CPP_BUFFER (pfile)->seen_eof)
4572 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4579 cpp_buffer *next_buf
4580 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4581 CPP_BUFFER (pfile)->seen_eof = 1;
4582 if (CPP_BUFFER (pfile)->nominal_fname
4583 && next_buf != CPP_NULL_BUFFER (pfile))
4585 /* We're about to return from an #include file.
4586 Emit #line information now (as part of the CPP_POP) result.
4587 But the #line refers to the file we will pop to. */
4588 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4589 CPP_BUFFER (pfile) = next_buf;
4590 pfile->input_stack_listing_current = 0;
4591 output_line_command (pfile, 0, leave_file);
4592 CPP_BUFFER (pfile) = cur_buffer;
4602 struct parse_marker start_mark;
4604 if (PEEKC () == '=')
4606 if (opts->put_out_comments)
4607 parse_set_mark (&start_mark, pfile);
4609 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4610 &start_line, &start_column);
4611 c = skip_comment (pfile, &newlines);
4612 if (opts->put_out_comments && (c == '/' || c == EOF))
4613 parse_clear_mark (&start_mark);
4618 cpp_error_with_line (pfile, start_line, start_column,
4619 "unterminated comment");
4622 c = '/'; /* Initial letter of comment. */
4624 /* Comments are equivalent to spaces.
4625 For -traditional, a comment is equivalent to nothing. */
4626 if (opts->put_out_comments)
4628 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4629 U_CHAR *start = pbuf->buf + start_mark.position;
4630 int len = pbuf->cur - start;
4631 CPP_RESERVE(pfile, 1 + len);
4632 CPP_PUTC_Q (pfile, c);
4633 CPP_PUTS_Q (pfile, start, len);
4634 pfile->lineno += newlines;
4635 parse_clear_mark (&start_mark);
4638 else if (CPP_TRADITIONAL (pfile))
4645 /* This may not work if cpp_get_token is called recursively,
4646 since many places look for horizontal space. */
4649 /* Copy the newlines into the output buffer, in order to
4650 avoid the pain of a #line every time a multiline comment
4652 CPP_RESERVE(pfile, newlines);
4653 while (--newlines >= 0)
4655 CPP_PUTC_Q (pfile, '\n');
4661 CPP_RESERVE(pfile, 1);
4662 CPP_PUTC_Q (pfile, ' ');
4666 if (opts->for_lint) {
4669 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4671 if (lintcmd != NULL) {
4672 /* I believe it is always safe to emit this newline: */
4674 bcopy ("#pragma lint ", (char *) obp, 13);
4676 bcopy (lintcmd, (char *) obp, cmdlen);
4681 bcopy (argbp, (char *) obp, arglen);
4685 /* OK, now bring us back to the state we were in before we entered
4686 this branch. We need #line because the newline for the pragma
4687 could mess things up. */
4688 output_line_command (pfile, 0, same_file);
4689 *(obp++) = ' '; /* just in case, if comments are copied thru */
4697 /* If this is expanding a macro definition, don't recognize
4698 preprocessor directives. */
4701 /* If this is expand_into_temp_buffer, recognize them
4702 only after an actual newline at this level,
4703 not at the beginning of the input level. */
4704 if (ip->fname == 0 && beg_of_line == ip->buf)
4710 if (!pfile->only_seen_white)
4712 if (handle_directive (pfile))
4713 return CPP_DIRECTIVE;
4714 pfile->only_seen_white = 0;
4719 /* A single quoted string is treated like a double -- some
4720 programs (e.g., troff) are perverse this way */
4721 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4722 &start_line, &start_column);
4723 old_written = CPP_WRITTEN (pfile);
4725 CPP_PUTC (pfile, c);
4731 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4733 /* try harder: this string crosses a macro expansion
4734 boundary. This can happen naturally if -traditional.
4735 Otherwise, only -D can make a macro with an unmatched
4737 cpp_buffer *next_buf
4738 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4739 (*CPP_BUFFER (pfile)->cleanup)
4740 (CPP_BUFFER (pfile), pfile);
4741 CPP_BUFFER (pfile) = next_buf;
4744 if (!CPP_TRADITIONAL (pfile))
4746 cpp_error_with_line (pfile, start_line, start_column,
4747 "unterminated string or character constant");
4748 if (pfile->multiline_string_line != start_line
4749 && pfile->multiline_string_line != 0)
4750 cpp_error_with_line (pfile,
4751 pfile->multiline_string_line, -1,
4752 "possible real start of unterminated constant");
4753 pfile->multiline_string_line = 0;
4757 CPP_PUTC (pfile, cc);
4761 /* Traditionally, end of line ends a string constant with
4762 no error. So exit the loop and record the new line. */
4763 if (CPP_TRADITIONAL (pfile))
4767 cpp_error_with_line (pfile, start_line, start_column,
4768 "unterminated character constant");
4771 if (CPP_PEDANTIC (pfile)
4772 && pfile->multiline_string_line == 0)
4774 cpp_pedwarn_with_line (pfile, start_line, start_column,
4775 "string constant runs past end of line");
4777 if (pfile->multiline_string_line == 0)
4778 pfile->multiline_string_line = start_line;
4785 /* Backslash newline is replaced by nothing at all. */
4786 CPP_ADJUST_WRITTEN (pfile, -1);
4791 /* ANSI stupidly requires that in \\ the second \
4792 is *not* prevented from combining with a newline. */
4795 CPP_PUTC (pfile, cc);
4807 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4808 CPP_PWRITTEN (pfile));
4809 pfile->only_seen_white = 0;
4810 return c == '\'' ? CPP_CHAR : CPP_STRING;
4813 if (!opts->dollars_in_ident)
4818 if (opts->cplusplus && PEEKC () == ':')
4827 if (c2 == c || c2 == '=')
4837 if (PEEKC () == '=')
4844 if (c2 == '-' && opts->chill)
4846 /* Chill style comment */
4847 if (opts->put_out_comments)
4848 parse_set_mark (&start_mark, pfile);
4849 FORWARD(1); /* Skip second '-'. */
4857 /* Don't consider final '\n' to be part of comment. */
4863 goto return_comment;
4865 if (c2 == '-' || c2 == '=' || c2 == '>')
4870 if (pfile->parsing_include_directive)
4874 CPP_PUTC (pfile, c);
4879 if (c == '\n' || c == EOF)
4882 "missing '>' in `#include <FILENAME>'");
4888 /* else fall through */
4897 CPP_RESERVE (pfile, 4);
4898 CPP_PUTC (pfile, c);
4899 CPP_PUTC (pfile, c2);
4903 CPP_PUTC_Q (pfile, GETC ());
4904 CPP_NUL_TERMINATE_Q (pfile);
4905 pfile->only_seen_white = 0;
4909 if (CPP_BUFFER (pfile)->has_escapes)
4914 if (pfile->output_escapes)
4915 CPP_PUTS (pfile, "@-", 2);
4916 parse_name (pfile, GETC ());
4919 else if (is_space [c])
4921 CPP_RESERVE (pfile, 2);
4922 if (pfile->output_escapes)
4923 CPP_PUTC_Q (pfile, '@');
4924 CPP_PUTC_Q (pfile, c);
4928 if (pfile->output_escapes)
4930 CPP_PUTS (pfile, "@@", 2);
4940 CPP_RESERVE(pfile, 2);
4941 CPP_PUTC_Q (pfile, '.');
4945 /* FIXME - misses the case "..\\\n." */
4946 if (c2 == '.' && PEEKN(1) == '.')
4948 CPP_RESERVE(pfile, 4);
4949 CPP_PUTC_Q (pfile, '.');
4950 CPP_PUTC_Q (pfile, '.');
4951 CPP_PUTC_Q (pfile, '.');
4953 CPP_NUL_TERMINATE_Q (pfile);
4954 pfile->only_seen_white = 0;
4961 pfile->only_seen_white = 0;
4963 CPP_RESERVE(pfile, 3);
4964 CPP_PUTC_Q (pfile, c);
4965 CPP_PUTC_Q (pfile, GETC ());
4966 CPP_NUL_TERMINATE_Q (pfile);
4972 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4974 CPP_PUTC (pfile, c);
4980 case '0': case '1': case '2': case '3': case '4':
4981 case '5': case '6': case '7': case '8': case '9':
4986 CPP_RESERVE (pfile, 2);
4987 CPP_PUTC_Q (pfile, c);
4992 if (!is_idchar[c] && c != '.'
4993 && ((c2 != 'e' && c2 != 'E'
4994 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4995 || (c != '+' && c != '-')))
5000 CPP_NUL_TERMINATE_Q (pfile);
5001 pfile->only_seen_white = 0;
5003 case 'b': case 'c': case 'd': case 'h': case 'o':
5004 case 'B': case 'C': case 'D': case 'H': case 'O':
5005 if (opts->chill && PEEKC () == '\'')
5007 pfile->only_seen_white = 0;
5008 CPP_RESERVE (pfile, 2);
5009 CPP_PUTC_Q (pfile, c);
5010 CPP_PUTC_Q (pfile, '\'');
5016 goto chill_number_eof;
5019 if (c == '\\' && PEEKC() == '\n')
5026 CPP_PUTC (pfile, c);
5030 CPP_RESERVE (pfile, 2);
5031 CPP_PUTC_Q (pfile, c);
5032 CPP_NUL_TERMINATE_Q (pfile);
5039 CPP_NUL_TERMINATE (pfile);
5046 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5047 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5048 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5049 case 'x': case 'y': case 'z':
5050 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5051 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5052 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5057 unsigned char *ident;
5058 int before_name_written = CPP_WRITTEN (pfile);
5060 parse_name (pfile, c);
5061 pfile->only_seen_white = 0;
5062 if (pfile->no_macro_expand)
5064 ident = pfile->token_buffer + before_name_written;
5065 ident_len = CPP_PWRITTEN (pfile) - ident;
5066 hp = cpp_lookup (pfile, ident, ident_len, -1);
5069 if (hp->type == T_DISABLED)
5071 if (pfile->output_escapes)
5072 { /* Return "@-IDENT", followed by '\0'. */
5074 CPP_RESERVE (pfile, 3);
5075 ident = pfile->token_buffer + before_name_written;
5076 CPP_ADJUST_WRITTEN (pfile, 2);
5077 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5084 /* If macro wants an arglist, verify that a '(' follows.
5085 first skip all whitespace, copying it to the output
5086 after the macro name. Then, if there is no '(',
5087 decide this is not a macro call and leave things that way. */
5088 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5090 struct parse_marker macro_mark;
5092 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5094 cpp_buffer *next_buf;
5095 cpp_skip_hspace (pfile);
5096 if (PEEKC () != EOF)
5098 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5099 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5100 CPP_BUFFER (pfile) = next_buf;
5102 parse_set_mark (¯o_mark, pfile);
5105 cpp_skip_hspace (pfile);
5107 is_macro_call = c == '(';
5113 parse_goto_mark (¯o_mark, pfile);
5114 parse_clear_mark (¯o_mark);
5118 /* This is now known to be a macro call. */
5120 /* it might not actually be a macro. */
5121 if (hp->type != T_MACRO) {
5122 int xbuf_len; U_CHAR *xbuf;
5123 CPP_SET_WRITTEN (pfile, before_name_written);
5124 special_symbol (hp, pfile);
5125 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5126 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5127 CPP_SET_WRITTEN (pfile, before_name_written);
5128 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5129 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5133 /* Expand the macro, reading arguments as needed,
5134 and push the expansion on the input stack. */
5135 macroexpand (pfile, hp);
5136 CPP_SET_WRITTEN (pfile, before_name_written);
5139 /* An extra "@ " is added to the end of a macro expansion
5140 to prevent accidental token pasting. We prefer to avoid
5141 unneeded extra spaces (for the sake of cpp-using tools like
5142 imake). Here we remove the space if it is safe to do so. */
5143 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5144 && pfile->buffer->rlimit[-2] == '@'
5145 && pfile->buffer->rlimit[-1] == ' ')
5147 int c1 = pfile->buffer->rlimit[-3];
5148 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5149 if (c2 == EOF || ! unsafe_chars (c1, c2))
5150 pfile->buffer->rlimit -= 2;
5155 case ' ': case '\t': case '\v': case '\r':
5158 CPP_PUTC (pfile, c);
5160 if (c == EOF || !is_hor_space[c])
5174 CPP_PUTC (pfile, c);
5175 if (pfile->only_seen_white == 0)
5176 pfile->only_seen_white = 1;
5178 output_line_command (pfile, 1, same_file);
5181 case '(': token = CPP_LPAREN; goto char1;
5182 case ')': token = CPP_RPAREN; goto char1;
5183 case '{': token = CPP_LBRACE; goto char1;
5184 case '}': token = CPP_RBRACE; goto char1;
5185 case ',': token = CPP_COMMA; goto char1;
5186 case ';': token = CPP_SEMICOLON; goto char1;
5192 pfile->only_seen_white = 0;
5193 CPP_PUTC (pfile, c);
5199 /* Like cpp_get_token, but skip spaces and comments. */
5202 cpp_get_non_space_token (pfile)
5205 int old_written = CPP_WRITTEN (pfile);
5208 enum cpp_token token = cpp_get_token (pfile);
5209 if (token != CPP_COMMENT && token != CPP_POP
5210 && token != CPP_HSPACE && token != CPP_VSPACE)
5212 CPP_SET_WRITTEN (pfile, old_written);
5216 /* Parse an identifier starting with C. */
5219 parse_name (pfile, c)
5220 cpp_reader *pfile; int c;
5226 if (c == '\\' && PEEKC() == '\n')
5235 if (c == '$' && CPP_PEDANTIC (pfile))
5236 cpp_pedwarn (pfile, "`$' in identifier");
5238 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5239 CPP_PUTC_Q (pfile, c);
5244 CPP_NUL_TERMINATE_Q (pfile);
5249 /* Maintain and search list of included files, for #import. */
5251 /* Hash a file name for import_hash_table. */
5259 while (*f) val += *f++;
5260 return (val%IMPORT_HASH_SIZE);
5263 /* Search for file FILENAME in import_hash_table.
5264 Return -2 if found, either a matching name or a matching inode.
5265 Otherwise, open the file and return a file descriptor if successful
5266 or -1 if unsuccessful. */
5269 lookup_import (pfile, filename, searchptr)
5272 struct file_name_list *searchptr;
5274 struct import_file *i;
5280 hashval = import_hash (filename);
5282 /* Attempt to find file in list of already included files */
5283 i = pfile->import_hash_table[hashval];
5286 if (!strcmp (filename, i->name))
5287 return -2; /* return found */
5290 /* Open it and try a match on inode/dev */
5291 fd = open_include_file (pfile, filename, searchptr);
5295 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5296 i = pfile->import_hash_table[h];
5298 /* Compare the inode and the device.
5299 Supposedly on some systems the inode is not a scalar. */
5300 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5301 && i->dev == sb.st_dev) {
5303 return -2; /* return found */
5308 return fd; /* Not found, return open file */
5311 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5314 add_import (pfile, fd, fname)
5319 struct import_file *i;
5323 hashval = import_hash (fname);
5325 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5326 i->name = (char *)xmalloc (strlen (fname)+1);
5327 strcpy (i->name, fname);
5328 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5330 i->next = pfile->import_hash_table[hashval];
5331 pfile->import_hash_table[hashval] = i;
5334 /* The file_name_map structure holds a mapping of file names for a
5335 particular directory. This mapping is read from the file named
5336 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5337 map filenames on a file system with severe filename restrictions,
5338 such as DOS. The format of the file name map file is just a series
5339 of lines with two tokens on each line. The first token is the name
5340 to map, and the second token is the actual name to use. */
5342 struct file_name_map
5344 struct file_name_map *map_next;
5349 #define FILE_NAME_MAP_FILE "header.gcc"
5351 /* Read a space delimited string of unlimited length from a stdio
5355 read_filename_string (ch, f)
5363 set = alloc = xmalloc (len + 1);
5367 while ((ch = getc (f)) != EOF && ! is_space[ch])
5369 if (set - alloc == len)
5372 alloc = xrealloc (alloc, len + 1);
5373 set = alloc + len / 2;
5383 /* This structure holds a linked list of file name maps, one per directory. */
5385 struct file_name_map_list
5387 struct file_name_map_list *map_list_next;
5388 char *map_list_name;
5389 struct file_name_map *map_list_map;
5392 /* Read the file name map file for DIRNAME. */
5394 static struct file_name_map *
5395 read_name_map (pfile, dirname)
5399 register struct file_name_map_list *map_list_ptr;
5403 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5404 map_list_ptr = map_list_ptr->map_list_next)
5405 if (! strcmp (map_list_ptr->map_list_name, dirname))
5406 return map_list_ptr->map_list_map;
5408 map_list_ptr = ((struct file_name_map_list *)
5409 xmalloc (sizeof (struct file_name_map_list)));
5410 map_list_ptr->map_list_name = savestring (dirname);
5411 map_list_ptr->map_list_map = NULL;
5413 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5414 strcpy (name, dirname);
5417 strcat (name, FILE_NAME_MAP_FILE);
5418 f = fopen (name, "r");
5420 map_list_ptr->map_list_map = NULL;
5424 int dirlen = strlen (dirname);
5426 while ((ch = getc (f)) != EOF)
5429 struct file_name_map *ptr;
5433 from = read_filename_string (ch, f);
5434 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5436 to = read_filename_string (ch, f);
5438 ptr = ((struct file_name_map *)
5439 xmalloc (sizeof (struct file_name_map)));
5440 ptr->map_from = from;
5442 /* Make the real filename absolute. */
5447 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5448 strcpy (ptr->map_to, dirname);
5449 ptr->map_to[dirlen] = '/';
5450 strcpy (ptr->map_to + dirlen + 1, to);
5454 ptr->map_next = map_list_ptr->map_list_map;
5455 map_list_ptr->map_list_map = ptr;
5457 while ((ch = getc (f)) != '\n')
5464 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5465 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5467 return map_list_ptr->map_list_map;
5470 /* Try to open include file FILENAME. SEARCHPTR is the directory
5471 being tried from the include file search path. This function maps
5472 filenames on file systems based on information read by
5476 open_include_file (pfile, filename, searchptr)
5479 struct file_name_list *searchptr;
5481 if (CPP_OPTIONS (pfile)->remap)
5483 register struct file_name_map *map;
5484 register char *from;
5487 if (searchptr && ! searchptr->got_name_map)
5489 searchptr->name_map = read_name_map (pfile,
5491 ? searchptr->fname : ".");
5492 searchptr->got_name_map = 1;
5495 /* First check the mapping for the directory we are using. */
5496 if (searchptr && searchptr->name_map)
5499 if (searchptr->fname)
5500 from += strlen (searchptr->fname) + 1;
5501 for (map = searchptr->name_map; map; map = map->map_next)
5503 if (! strcmp (map->map_from, from))
5505 /* Found a match. */
5506 return open (map->map_to, O_RDONLY, 0666);
5511 /* Try to find a mapping file for the particular directory we are
5512 looking in. Thus #include <sys/types.h> will look up sys/types.h
5513 in /usr/include/header.gcc and look up types.h in
5514 /usr/include/sys/header.gcc. */
5515 p = rindex (filename, '/');
5520 && strlen (searchptr->fname) == p - filename
5521 && ! strncmp (searchptr->fname, filename, p - filename))
5523 /* FILENAME is in SEARCHPTR, which we've already checked. */
5524 return open (filename, O_RDONLY, 0666);
5534 dir = (char *) alloca (p - filename + 1);
5535 bcopy (filename, dir, p - filename);
5536 dir[p - filename] = '\0';
5539 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5540 if (! strcmp (map->map_from, from))
5541 return open (map->map_to, O_RDONLY, 0666);
5544 return open (filename, O_RDONLY, 0666);
5547 /* Process the contents of include file FNAME, already open on descriptor F,
5549 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5550 "system" include directories (as decided by the `is_system_include'
5552 DIRPTR is the link in the dir path through which this file was found,
5553 or 0 if the file name was absolute or via the current directory.
5554 Return 1 on success, 0 on failure.
5556 The caller is responsible for the cpp_push_buffer. */
5559 finclude (pfile, f, fname, system_header_p, dirptr)
5563 int system_header_p;
5564 struct file_name_list *dirptr;
5570 cpp_buffer *fp; /* For input stack frame */
5572 int missing_newline = 0;
5575 if (fstat (f, &st) < 0)
5577 cpp_perror_with_name (pfile, fname);
5579 cpp_pop_buffer (pfile);
5583 fp = CPP_BUFFER (pfile);
5584 fp->nominal_fname = fp->fname = fname;
5589 fp->system_header_p = system_header_p;
5592 fp->cleanup = file_cleanup;
5594 if (S_ISREG (st.st_mode)) {
5595 st_size = (size_t) st.st_size;
5596 if (st_size != st.st_size || st_size + 2 < st_size) {
5597 cpp_error (pfile, "file `%s' too large", fname);
5601 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5602 fp->alimit = fp->buf + st_size + 2;
5605 /* Read the file contents, knowing that st_size is an upper bound
5606 on the number of bytes we can read. */
5607 length = safe_read (f, fp->buf, st_size);
5608 fp->rlimit = fp->buf + length;
5609 if (length < 0) goto nope;
5611 else if (S_ISDIR (st.st_mode)) {
5612 cpp_error (pfile, "directory `%s' specified in #include", fname);
5616 /* Cannot count its file size before reading.
5617 First read the entire file into heap and
5618 copy them into buffer on stack. */
5623 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5626 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5628 goto nope; /* error! */
5630 if (st_size != bsize)
5631 break; /* End of file */
5633 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5639 if ((length > 0 && fp->buf[length - 1] != '\n')
5640 /* Backslash-newline at end is not good enough. */
5641 || (length > 1 && fp->buf[length - 2] == '\\')) {
5642 fp->buf[length++] = '\n';
5644 missing_newline = 1;
5647 fp->buf[length] = '\0';
5648 fp->rlimit = fp->buf + length;
5650 /* Close descriptor now, so nesting does not use lots of descriptors. */
5653 /* Must do this before calling trigraph_pcp, so that the correct file name
5654 will be printed in warning messages. */
5656 pfile->input_stack_listing_current = 0;
5666 if (missing_newline)
5669 if (CPP_PEDANTIC (pfile) && missing_newline)
5670 pedwarn ("file does not end in newline");
5673 input_file_stack_tick++;
5680 cpp_perror_with_name (pfile, fname);
5686 /* This is called after options have been processed.
5687 * Check options for consistency, and setup for processing input
5688 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5689 * Return 1 on success, 0 on failure.
5693 cpp_start_read (pfile, fname)
5697 struct cpp_options *opts = CPP_OPTIONS (pfile);
5698 struct cpp_pending *pend;
5703 /* The code looks at the defaults through this pointer, rather than through
5704 the constant structure above. This pointer gets changed if an environment
5705 variable specifies other defaults. */
5706 struct default_include *include_defaults = include_defaults_array;
5708 /* Add dirs from CPATH after dirs from -I. */
5709 /* There seems to be confusion about what CPATH should do,
5710 so for the moment it is not documented. */
5711 /* Some people say that CPATH should replace the standard include dirs,
5712 but that seems pointless: it comes before them, so it overrides them
5714 GET_ENVIRONMENT (p, "CPATH");
5715 if (p != 0 && ! opts->no_standard_includes)
5716 path_include (pfile, p);
5718 /* Now that dollars_in_ident is known, initialize is_idchar. */
5719 initialize_char_syntax (opts);
5721 /* Do partial setup of input buffer for the sake of generating
5722 early #line directives (when -g is in effect). */
5723 fp = cpp_push_buffer (pfile, NULL, 0);
5726 if (opts->in_fname == NULL)
5727 opts->in_fname = "";
5728 fp->nominal_fname = fp->fname = opts->in_fname;
5731 /* Install __LINE__, etc. Must follow initialize_char_syntax
5732 and option processing. */
5733 initialize_builtins (pfile);
5735 /* Do standard #defines and assertions
5736 that identify system and machine type. */
5738 if (!opts->inhibit_predefs) {
5739 char *p = (char *) alloca (strlen (predefs) + 1);
5740 strcpy (p, predefs);
5743 while (*p == ' ' || *p == '\t')
5745 /* Handle -D options. */
5746 if (p[0] == '-' && p[1] == 'D') {
5748 while (*p && *p != ' ' && *p != '\t')
5752 if (opts->debug_output)
5753 output_line_command (pfile, 0, same_file);
5754 cpp_define (pfile, q);
5755 while (*p == ' ' || *p == '\t')
5757 } else if (p[0] == '-' && p[1] == 'A') {
5758 /* Handle -A options (assertions). */
5767 past_name = assertion;
5768 /* Locate end of name. */
5769 while (*past_name && *past_name != ' '
5770 && *past_name != '\t' && *past_name != '(')
5772 /* Locate `(' at start of value. */
5774 while (*value && (*value == ' ' || *value == '\t'))
5776 if (*value++ != '(')
5778 while (*value && (*value == ' ' || *value == '\t'))
5781 /* Locate end of value. */
5782 while (*past_value && *past_value != ' '
5783 && *past_value != '\t' && *past_value != ')')
5785 termination = past_value;
5786 while (*termination && (*termination == ' ' || *termination == '\t'))
5788 if (*termination++ != ')')
5790 if (*termination && *termination != ' ' && *termination != '\t')
5792 /* Temporarily null-terminate the value. */
5793 save_char = *termination;
5794 *termination = '\0';
5795 /* Install the assertion. */
5796 make_assertion (pfile, "-A", assertion);
5797 *termination = (char) save_char;
5799 while (*p == ' ' || *p == '\t')
5807 /* Now handle the command line options. */
5809 /* Do -U's, -D's and -A's in the order they were seen. */
5810 /* First reverse the list. */
5811 opts->pending = nreverse_pending (opts->pending);
5813 for (pend = opts->pending; pend; pend = pend->next)
5815 if (pend->cmd != NULL && pend->cmd[0] == '-')
5817 switch (pend->cmd[1])
5820 if (opts->debug_output)
5821 output_line_command (pfile, 0, same_file);
5822 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5825 if (opts->debug_output)
5826 output_line_command (pfile, 0, same_file);
5827 cpp_define (pfile, pend->arg);
5830 make_assertion (pfile, "-A", pend->arg);
5836 opts->done_initializing = 1;
5838 { /* Read the appropriate environment variable and if it exists
5839 replace include_defaults with the listed path. */
5841 switch ((opts->objc << 1) + opts->cplusplus)
5844 GET_ENVIRONMENT (epath, "C_INCLUDE_PATH");
5847 GET_ENVIRONMENT (epath, "CPLUS_INCLUDE_PATH");
5850 GET_ENVIRONMENT (epath, "OBJC_INCLUDE_PATH");
5853 GET_ENVIRONMENT (epath, "OBJCPLUS_INCLUDE_PATH");
5856 /* If the environment var for this language is set,
5857 add to the default list of include directories. */
5859 char *nstore = (char *) alloca (strlen (epath) + 2);
5861 char *startp, *endp;
5863 for (num_dirs = 1, startp = epath; *startp; startp++)
5864 if (*startp == PATH_SEPARATOR)
5867 = (struct default_include *) xmalloc ((num_dirs
5868 * sizeof (struct default_include))
5869 + sizeof (include_defaults_array));
5870 startp = endp = epath;
5873 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5874 if ((*endp == PATH_SEPARATOR)
5876 strncpy (nstore, startp, endp-startp);
5878 strcpy (nstore, ".");
5880 nstore[endp-startp] = '\0';
5882 include_defaults[num_dirs].fname = savestring (nstore);
5883 include_defaults[num_dirs].component = 0;
5884 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5885 include_defaults[num_dirs].cxx_aware = 1;
5889 endp = startp = endp + 1;
5893 /* Put the usual defaults back in at the end. */
5894 bcopy ((char *) include_defaults_array,
5895 (char *) &include_defaults[num_dirs],
5896 sizeof (include_defaults_array));
5900 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5901 opts->first_system_include = opts->before_system;
5903 /* Unless -fnostdinc,
5904 tack on the standard include file dirs to the specified list */
5905 if (!opts->no_standard_includes) {
5906 struct default_include *p = include_defaults;
5907 char *specd_prefix = opts->include_prefix;
5908 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5909 int default_len = 0;
5910 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5911 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5912 default_len = strlen (default_prefix) - 7;
5913 default_prefix[default_len] = 0;
5915 /* Search "translated" versions of GNU directories.
5916 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5917 if (specd_prefix != 0 && default_len != 0)
5918 for (p = include_defaults; p->fname; p++) {
5919 /* Some standard dirs are only for C++. */
5921 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5922 /* Does this dir start with the prefix? */
5923 if (!strncmp (p->fname, default_prefix, default_len)) {
5924 /* Yes; change prefix and add to search list. */
5925 struct file_name_list *new
5926 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5927 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5928 char *str = (char *) xmalloc (this_len + 1);
5929 strcpy (str, specd_prefix);
5930 strcat (str, p->fname + default_len);
5932 new->control_macro = 0;
5933 new->c_system_include_path = !p->cxx_aware;
5934 new->got_name_map = 0;
5935 append_include_chain (pfile, new, new);
5936 if (opts->first_system_include == 0)
5937 opts->first_system_include = new;
5941 /* Search ordinary names for GNU include directories. */
5942 for (p = include_defaults; p->fname; p++) {
5943 /* Some standard dirs are only for C++. */
5945 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5946 struct file_name_list *new
5947 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5948 new->control_macro = 0;
5949 new->c_system_include_path = !p->cxx_aware;
5950 new->fname = update_path (p->fname, p->component);
5951 new->got_name_map = 0;
5952 append_include_chain (pfile, new, new);
5953 if (opts->first_system_include == 0)
5954 opts->first_system_include = new;
5959 /* Tack the after_include chain at the end of the include chain. */
5960 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5961 if (opts->first_system_include == 0)
5962 opts->first_system_include = opts->after_include;
5964 /* With -v, print the list of dirs to search. */
5965 if (opts->verbose) {
5966 struct file_name_list *p;
5967 fprintf (stderr, "#include \"...\" search starts here:\n");
5968 for (p = opts->include; p; p = p->next) {
5969 if (p == opts->first_bracket_include)
5970 fprintf (stderr, "#include <...> search starts here:\n");
5971 fprintf (stderr, " %s\n", p->fname);
5973 fprintf (stderr, "End of search list.\n");
5976 /* Scan the -imacros files before the main input.
5977 Much like #including them, but with no_output set
5978 so that only their macro definitions matter. */
5980 opts->no_output++; pfile->no_record_file++;
5981 for (pend = opts->pending; pend; pend = pend->next)
5983 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5985 int fd = open (pend->arg, O_RDONLY, 0666);
5988 cpp_perror_with_name (pfile, pend->arg);
5991 if (!cpp_push_buffer (pfile, NULL, 0))
5993 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
5994 cpp_scan_buffer (pfile);
5997 opts->no_output--; pfile->no_record_file--;
5999 /* Copy the entire contents of the main input file into
6000 the stacked input buffer previously allocated for it. */
6001 if (fname == NULL || *fname == 0) {
6004 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6005 cpp_pfatal_with_name (pfile, fname);
6007 /* -MG doesn't select the form of output and must be specified with one of
6008 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6009 inhibit compilation. */
6010 if (opts->print_deps_missing_files
6011 && (opts->print_deps == 0 || !opts->no_output))
6013 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
6017 /* Either of two environment variables can specify output of deps.
6018 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6019 where OUTPUT_FILE is the file to write deps info to
6020 and DEPS_TARGET is the target to mention in the deps. */
6022 if (opts->print_deps == 0
6023 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6024 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6025 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6031 spec = getenv ("SUNPRO_DEPENDENCIES");
6032 opts->print_deps = 2;
6035 opts->print_deps = 1;
6038 /* Find the space before the DEPS_TARGET, if there is one. */
6039 /* This should use index. (mrs) */
6040 while (*s != 0 && *s != ' ') s++;
6043 opts->deps_target = s + 1;
6044 output_file = (char *) xmalloc (s - spec + 1);
6045 bcopy (spec, output_file, s - spec);
6046 output_file[s - spec] = 0;
6050 opts->deps_target = 0;
6054 opts->deps_file = output_file;
6055 opts->print_deps_append = 1;
6058 /* For -M, print the expected object file name
6059 as the target of this Make-rule. */
6060 if (opts->print_deps)
6062 pfile->deps_allocated_size = 200;
6063 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6064 pfile->deps_buffer[0] = 0;
6065 pfile->deps_size = 0;
6066 pfile->deps_column = 0;
6068 if (opts->deps_target)
6069 deps_output (pfile, opts->deps_target, ':');
6070 else if (*opts->in_fname == 0)
6071 deps_output (pfile, "-", ':');
6076 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
6077 ".cc", ".cxx", ".cpp", ".cp",
6081 /* Discard all directory prefixes from filename. */
6082 if ((q = rindex (opts->in_fname, '/')) != NULL
6083 #ifdef DIR_SEPARATOR
6084 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6091 /* Copy remainder to mungable area. */
6092 p = (char *) alloca (strlen(q) + 8);
6095 /* Output P, but remove known suffixes. */
6098 /* Point to the filename suffix. */
6099 r = rindex (p, '.');
6100 /* Compare against the known suffixes. */
6102 while (known_suffixes[x] != 0)
6104 if (strncmp (known_suffixes[x], r, q - r) == 0)
6106 /* Make q point to the bit we're going to overwrite
6107 with an object suffix. */
6114 /* Supply our own suffix. */
6121 deps_output (pfile, p, ':');
6122 deps_output (pfile, opts->in_fname, ' ');
6127 /* Make sure data ends with a newline. And put a null after it. */
6129 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6130 /* Backslash-newline at end is not good enough. */
6131 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6132 fp->buf[fp->length++] = '\n';
6133 missing_newline = 1;
6135 fp->buf[fp->length] = '\0';
6137 /* Unless inhibited, convert trigraphs in the input. */
6143 /* Scan the -include files before the main input.
6144 We push these in reverse order, so that the first one is handled first. */
6146 pfile->no_record_file++;
6147 opts->pending = nreverse_pending (opts->pending);
6148 for (pend = opts->pending; pend; pend = pend->next)
6150 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6152 int fd = open (pend->arg, O_RDONLY, 0666);
6155 cpp_perror_with_name (pfile, pend->arg);
6158 if (!cpp_push_buffer (pfile, NULL, 0))
6160 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6163 pfile->no_record_file--;
6165 /* Free the pending list. */
6166 for (pend = opts->pending; pend; )
6168 struct cpp_pending *next = pend->next;
6172 opts->pending = NULL;
6175 /* Scan the input, processing macros and directives. */
6177 rescan (&outbuf, 0);
6179 if (missing_newline)
6182 if (CPP_PEDANTIC (pfile) && missing_newline)
6183 pedwarn ("file does not end in newline");
6186 if (finclude (pfile, f, fname, 0, NULL_PTR))
6187 output_line_command (pfile, 0, same_file);
6192 cpp_reader_init (pfile)
6195 bzero ((char *) pfile, sizeof (cpp_reader));
6196 pfile->get_token = cpp_get_token;
6198 pfile->token_buffer_size = 200;
6199 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
6200 CPP_SET_WRITTEN (pfile, 0);
6202 pfile->system_include_depth = 0;
6203 pfile->dont_repeat_files = 0;
6204 pfile->all_include_files = 0;
6205 pfile->max_include_len = 0;
6206 pfile->timebuf = NULL;
6207 pfile->only_seen_white = 1;
6208 pfile->buffer = CPP_NULL_BUFFER(pfile);
6211 static struct cpp_pending *
6212 nreverse_pending (list)
6213 struct cpp_pending *list;
6216 register struct cpp_pending *prev = 0, *next, *pend;
6217 for (pend = list; pend; pend = next)
6227 push_pending (pfile, cmd, arg)
6232 struct cpp_pending *pend
6233 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
6236 pend->next = CPP_OPTIONS (pfile)->pending;
6237 CPP_OPTIONS (pfile)->pending = pend;
6244 printf ("Usage: %s [switches] input output\n", progname);
6245 printf ("Switches:\n");
6246 printf (" -include <file> Include the contents of <file> before other files\n");
6247 printf (" -imacros <file> Accept definition of marcos in <file>\n");
6248 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
6249 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
6250 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
6251 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
6252 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
6253 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
6254 printf (" -nostdinc Do not search the system include directories\n");
6255 printf (" -nostdinc++ Do not search the system include directories for C++\n");
6256 printf (" -o <file> Put output into <file>\n");
6257 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
6258 printf (" -traditional Follow K&R pre-processor behaviour\n");
6259 printf (" -trigraphs Support ANSI C trigraphs\n");
6260 printf (" -lang-c Assume that the input sources are in C\n");
6261 printf (" -lang-c89 Assume that the input sources are in C89\n");
6262 printf (" -lang-c++ Assume that the input sources are in C++\n");
6263 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
6264 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
6265 printf (" -lang-asm Assume that the input sources are in assembler\n");
6266 printf (" -lang-chill Assume that the input sources are in Chill\n");
6267 printf (" -+ Allow parsing of C++ style features\n");
6268 printf (" -w Inhibit warning messages\n");
6269 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
6270 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
6271 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
6272 printf (" -Wno-comment{s} Do not warn about comments\n");
6273 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
6274 printf (" a string if -tradtional is specified\n");
6275 printf (" -Wno-traditional Do not warn about stringification\n");
6276 printf (" -Wundef Warn if an undefined macro is used by #if\n");
6277 printf (" -Wno-undef Do not warn about testing udefined macros\n");
6278 printf (" -Wimport Warn about the use of the #import directive\n");
6279 printf (" -Wno-import Do not warn about the use of #import\n");
6280 printf (" -Werror Treat all warnings as errors\n");
6281 printf (" -Wno-error Do not treat warnings as errors\n");
6282 printf (" -Wall Enable all preprocessor warnings\n");
6283 printf (" -M Generate make dependencies\n");
6284 printf (" -MM As -M, but ignore system header files\n");
6285 printf (" -MD As -M, but put output in a .d file\n");
6286 printf (" -MMD As -MD, but ignore system header files\n");
6287 printf (" -MG Treat missing header file as generated files\n");
6288 printf (" -g Include #define and #undef directives in the output\n");
6289 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
6290 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
6291 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
6292 printf (" -U<macro> Undefine <macro> \n");
6293 printf (" -u or -undef Do not predefine any macros\n");
6294 printf (" -v Display the version number\n");
6295 printf (" -H Print the name of header files as they are used\n");
6296 printf (" -C Do not discard comments\n");
6297 printf (" -dM Display a list of macro definitions active at end\n");
6298 printf (" -dD Preserve macro definitions in output\n");
6299 printf (" -dN As -dD except that only the names are preserved\n");
6300 printf (" -dI Include #include directives in the output\n");
6301 printf (" -ifoutput Describe skipped code blocks in output \n");
6302 printf (" -P Do not generate #line directives\n");
6303 printf (" -$ Do not include '$' in identifiers\n");
6304 printf (" -remap Remap file names when including files.\n");
6305 printf (" -h or --help Display this information\n");
6309 /* Handle one command-line option in (argc, argv).
6310 Can be called multiple times, to handle multiple sets of options.
6311 Returns number of strings consumed. */
6313 cpp_handle_option (pfile, argc, argv)
6318 struct cpp_options *opts = CPP_OPTIONS (pfile);
6320 if (argv[i][0] != '-') {
6321 if (opts->out_fname != NULL)
6324 cpp_fatal (pfile, "Too many arguments");
6326 else if (opts->in_fname != NULL)
6327 opts->out_fname = argv[i];
6329 opts->in_fname = argv[i];
6331 switch (argv[i][1]) {
6334 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
6337 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
6341 if (!strcmp (argv[i], "-include")
6342 || !strcmp (argv[i], "-imacros")) {
6344 goto missing_filename;
6346 push_pending (pfile, argv[i], argv[i+1]), i++;
6348 if (!strcmp (argv[i], "-iprefix")) {
6350 goto missing_filename;
6352 opts->include_prefix = argv[++i];
6354 if (!strcmp (argv[i], "-ifoutput")) {
6355 opts->output_conditionals = 1;
6357 if (!strcmp (argv[i], "-isystem")) {
6358 struct file_name_list *dirtmp;
6361 goto missing_filename;
6363 dirtmp = (struct file_name_list *)
6364 xmalloc (sizeof (struct file_name_list));
6366 dirtmp->control_macro = 0;
6367 dirtmp->c_system_include_path = 1;
6368 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6369 strcpy (dirtmp->fname, argv[++i]);
6370 dirtmp->got_name_map = 0;
6372 if (opts->before_system == 0)
6373 opts->before_system = dirtmp;
6375 opts->last_before_system->next = dirtmp;
6376 opts->last_before_system = dirtmp; /* Tail follows the last one */
6378 /* Add directory to end of path for includes,
6379 with the default prefix at the front of its name. */
6380 if (!strcmp (argv[i], "-iwithprefix")) {
6381 struct file_name_list *dirtmp;
6384 if (opts->include_prefix != 0)
6385 prefix = opts->include_prefix;
6387 prefix = savestring (GCC_INCLUDE_DIR);
6388 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6389 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6390 prefix[strlen (prefix) - 7] = 0;
6393 dirtmp = (struct file_name_list *)
6394 xmalloc (sizeof (struct file_name_list));
6395 dirtmp->next = 0; /* New one goes on the end */
6396 dirtmp->control_macro = 0;
6397 dirtmp->c_system_include_path = 0;
6399 goto missing_dirname;
6401 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6402 + strlen (prefix) + 1);
6403 strcpy (dirtmp->fname, prefix);
6404 strcat (dirtmp->fname, argv[++i]);
6405 dirtmp->got_name_map = 0;
6407 if (opts->after_include == 0)
6408 opts->after_include = dirtmp;
6410 opts->last_after_include->next = dirtmp;
6411 opts->last_after_include = dirtmp; /* Tail follows the last one */
6413 /* Add directory to main path for includes,
6414 with the default prefix at the front of its name. */
6415 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6416 struct file_name_list *dirtmp;
6419 if (opts->include_prefix != 0)
6420 prefix = opts->include_prefix;
6422 prefix = savestring (GCC_INCLUDE_DIR);
6423 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6424 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6425 prefix[strlen (prefix) - 7] = 0;
6428 dirtmp = (struct file_name_list *)
6429 xmalloc (sizeof (struct file_name_list));
6430 dirtmp->next = 0; /* New one goes on the end */
6431 dirtmp->control_macro = 0;
6432 dirtmp->c_system_include_path = 0;
6434 goto missing_dirname;
6436 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6437 + strlen (prefix) + 1);
6438 strcpy (dirtmp->fname, prefix);
6439 strcat (dirtmp->fname, argv[++i]);
6440 dirtmp->got_name_map = 0;
6442 append_include_chain (pfile, dirtmp, dirtmp);
6444 /* Add directory to end of path for includes. */
6445 if (!strcmp (argv[i], "-idirafter")) {
6446 struct file_name_list *dirtmp;
6448 dirtmp = (struct file_name_list *)
6449 xmalloc (sizeof (struct file_name_list));
6450 dirtmp->next = 0; /* New one goes on the end */
6451 dirtmp->control_macro = 0;
6452 dirtmp->c_system_include_path = 0;
6454 goto missing_dirname;
6456 dirtmp->fname = argv[++i];
6457 dirtmp->got_name_map = 0;
6459 if (opts->after_include == 0)
6460 opts->after_include = dirtmp;
6462 opts->last_after_include->next = dirtmp;
6463 opts->last_after_include = dirtmp; /* Tail follows the last one */
6468 if (opts->out_fname != NULL)
6470 cpp_fatal (pfile, "Output filename specified twice");
6474 goto missing_filename;
6475 opts->out_fname = argv[++i];
6476 if (!strcmp (opts->out_fname, "-"))
6477 opts->out_fname = "";
6481 if (!strcmp (argv[i], "-pedantic"))
6482 CPP_PEDANTIC (pfile) = 1;
6483 else if (!strcmp (argv[i], "-pedantic-errors")) {
6484 CPP_PEDANTIC (pfile) = 1;
6485 opts->pedantic_errors = 1;
6488 else if (!strcmp (argv[i], "-pcp")) {
6489 char *pcp_fname = argv[++i];
6490 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6491 ? fopen (pcp_fname, "w")
6492 : fdopen (dup (fileno (stdout)), "w"));
6493 if (pcp_outfile == 0)
6494 cpp_pfatal_with_name (pfile, pcp_fname);
6501 if (!strcmp (argv[i], "-traditional")) {
6502 opts->traditional = 1;
6503 opts->cplusplus_comments = 0;
6504 } else if (!strcmp (argv[i], "-trigraphs")) {
6506 opts->no_trigraphs = 0;
6511 if (! strcmp (argv[i], "-lang-c"))
6512 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6514 if (! strcmp (argv[i], "-lang-c89"))
6515 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
6517 if (! strcmp (argv[i], "-lang-c++"))
6518 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6520 if (! strcmp (argv[i], "-lang-objc"))
6521 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6523 if (! strcmp (argv[i], "-lang-objc++"))
6524 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6526 if (! strcmp (argv[i], "-lang-asm"))
6528 if (! strcmp (argv[i], "-lint"))
6530 if (! strcmp (argv[i], "-lang-chill"))
6531 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6532 opts->traditional = 1, opts->no_trigraphs = 1;
6536 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6540 opts->inhibit_warnings = 1;
6544 if (!strcmp (argv[i], "-Wtrigraphs"))
6545 opts->warn_trigraphs = 1;
6546 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6547 opts->warn_trigraphs = 0;
6548 else if (!strcmp (argv[i], "-Wcomment"))
6549 opts->warn_comments = 1;
6550 else if (!strcmp (argv[i], "-Wno-comment"))
6551 opts->warn_comments = 0;
6552 else if (!strcmp (argv[i], "-Wcomments"))
6553 opts->warn_comments = 1;
6554 else if (!strcmp (argv[i], "-Wno-comments"))
6555 opts->warn_comments = 0;
6556 else if (!strcmp (argv[i], "-Wtraditional"))
6557 opts->warn_stringify = 1;
6558 else if (!strcmp (argv[i], "-Wno-traditional"))
6559 opts->warn_stringify = 0;
6560 else if (!strcmp (argv[i], "-Wundef"))
6561 opts->warn_undef = 1;
6562 else if (!strcmp (argv[i], "-Wno-undef"))
6563 opts->warn_undef = 0;
6564 else if (!strcmp (argv[i], "-Wimport"))
6565 opts->warn_import = 1;
6566 else if (!strcmp (argv[i], "-Wno-import"))
6567 opts->warn_import = 0;
6568 else if (!strcmp (argv[i], "-Werror"))
6569 opts->warnings_are_errors = 1;
6570 else if (!strcmp (argv[i], "-Wno-error"))
6571 opts->warnings_are_errors = 0;
6572 else if (!strcmp (argv[i], "-Wall"))
6574 opts->warn_trigraphs = 1;
6575 opts->warn_comments = 1;
6580 /* The style of the choices here is a bit mixed.
6581 The chosen scheme is a hybrid of keeping all options in one string
6582 and specifying each option in a separate argument:
6583 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6584 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6585 -M[M][G][D file]. This is awkward to handle in specs, and is not
6587 /* ??? -MG must be specified in addition to one of -M or -MM.
6588 This can be relaxed in the future without breaking anything.
6589 The converse isn't true. */
6591 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6592 if (!strcmp (argv[i], "-MG"))
6594 opts->print_deps_missing_files = 1;
6597 if (!strcmp (argv[i], "-M"))
6598 opts->print_deps = 2;
6599 else if (!strcmp (argv[i], "-MM"))
6600 opts->print_deps = 1;
6601 else if (!strcmp (argv[i], "-MD"))
6602 opts->print_deps = 2;
6603 else if (!strcmp (argv[i], "-MMD"))
6604 opts->print_deps = 1;
6605 /* For -MD and -MMD options, write deps on file named by next arg. */
6606 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6609 goto missing_filename;
6610 opts->deps_file = argv[++i];
6614 /* For -M and -MM, write deps on standard output
6615 and suppress the usual output. */
6616 opts->no_output = 1;
6622 char *p = argv[i] + 2;
6624 while ((c = *p++) != 0) {
6625 /* Arg to -d specifies what parts of macros to dump */
6628 opts->dump_macros = dump_only;
6629 opts->no_output = 1;
6632 opts->dump_macros = dump_names;
6635 opts->dump_macros = dump_definitions;
6638 opts->dump_includes = 1;
6646 if (argv[i][2] == '3')
6647 opts->debug_output = 1;
6651 if (strcmp (argv[i], "--help") != 0)
6657 fprintf (stderr, "GNU CPP version %s", version_string);
6658 #ifdef TARGET_VERSION
6661 fprintf (stderr, "\n");
6666 opts->print_include_names = 1;
6670 if (argv[i][2] != 0)
6671 push_pending (pfile, "-D", argv[i] + 2);
6672 else if (i + 1 == argc)
6674 cpp_fatal (pfile, "Macro name missing after -D option");
6678 i++, push_pending (pfile, "-D", argv[i]);
6685 if (argv[i][2] != 0)
6687 else if (i + 1 == argc)
6689 cpp_fatal (pfile, "Assertion missing after -A option");
6695 if (!strcmp (p, "-")) {
6696 struct cpp_pending **ptr;
6697 /* -A- eliminates all predefined macros and assertions.
6698 Let's include also any that were specified earlier
6699 on the command line. That way we can get rid of any
6700 that were passed automatically in from GCC. */
6701 opts->inhibit_predefs = 1;
6702 for (ptr = &opts->pending; *ptr != NULL; )
6704 struct cpp_pending *pend = *ptr;
6705 if (pend->cmd && pend->cmd[0] == '-'
6706 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6715 push_pending (pfile, "-A", p);
6720 case 'U': /* JF #undef something */
6721 if (argv[i][2] != 0)
6722 push_pending (pfile, "-U", argv[i] + 2);
6723 else if (i + 1 == argc)
6725 cpp_fatal (pfile, "Macro name missing after -U option");
6729 push_pending (pfile, "-U", argv[i+1]), i++;
6733 opts->put_out_comments = 1;
6736 case 'E': /* -E comes from cc -E; ignore it. */
6740 opts->no_line_commands = 1;
6743 case '$': /* Don't include $ in identifiers. */
6744 opts->dollars_in_ident = 0;
6747 case 'I': /* Add directory to path for includes. */
6749 struct file_name_list *dirtmp;
6751 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6752 && !strcmp (argv[i] + 2, "-")) {
6753 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6754 /* Don't use any preceding -I directories for #include <...>. */
6755 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6758 dirtmp = (struct file_name_list *)
6759 xmalloc (sizeof (struct file_name_list));
6760 dirtmp->next = 0; /* New one goes on the end */
6761 dirtmp->control_macro = 0;
6762 dirtmp->c_system_include_path = 0;
6763 if (argv[i][2] != 0)
6764 dirtmp->fname = argv[i] + 2;
6765 else if (i + 1 == argc)
6766 goto missing_dirname;
6768 dirtmp->fname = argv[++i];
6769 dirtmp->got_name_map = 0;
6770 append_include_chain (pfile, dirtmp, dirtmp);
6776 if (!strcmp (argv[i], "-nostdinc"))
6777 /* -nostdinc causes no default include directories.
6778 You must specify all include-file directories with -I. */
6779 opts->no_standard_includes = 1;
6780 else if (!strcmp (argv[i], "-nostdinc++"))
6781 /* -nostdinc++ causes no default C++-specific include directories. */
6782 opts->no_standard_cplusplus_includes = 1;
6784 else if (!strcmp (argv[i], "-noprecomp"))
6790 if (!strcmp (argv[i], "-remap"))
6795 /* Sun compiler passes undocumented switch "-undef".
6796 Let's assume it means to inhibit the predefined symbols. */
6797 opts->inhibit_predefs = 1;
6800 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6801 if (opts->in_fname == NULL) {
6802 opts->in_fname = "";
6804 } else if (opts->out_fname == NULL) {
6805 opts->out_fname = "";
6807 } /* else fall through into error */
6817 /* Handle command-line options in (argc, argv).
6818 Can be called multiple times, to handle multiple sets of options.
6819 Returns if an unrecognized option is seen.
6820 Returns number of strings consumed. */
6823 cpp_handle_options (pfile, argc, argv)
6829 int strings_processed;
6830 for (i = 0; i < argc; i += strings_processed)
6832 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
6833 if (strings_processed == 0)
6843 struct cpp_options *opts = CPP_OPTIONS (pfile);
6845 if (opts->print_deps)
6847 /* Stream on which to print the dependency information. */
6850 /* Don't actually write the deps file if compilation has failed. */
6851 if (pfile->errors == 0)
6853 char *deps_mode = opts->print_deps_append ? "a" : "w";
6854 if (opts->deps_file == 0)
6855 deps_stream = stdout;
6856 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6857 cpp_pfatal_with_name (pfile, opts->deps_file);
6858 fputs (pfile->deps_buffer, deps_stream);
6859 putc ('\n', deps_stream);
6860 if (opts->deps_file)
6862 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6863 cpp_fatal (pfile, "I/O error on output");
6869 /* Free resources used by PFILE.
6870 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6877 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6878 cpp_pop_buffer (pfile);
6880 if (pfile->token_buffer)
6882 free (pfile->token_buffer);
6883 pfile->token_buffer = NULL;
6886 if (pfile->deps_buffer)
6888 free (pfile->deps_buffer);
6889 pfile->deps_buffer = NULL;
6890 pfile->deps_allocated_size = 0;
6893 while (pfile->if_stack)
6895 IF_STACK_FRAME *temp = pfile->if_stack;
6896 pfile->if_stack = temp->next;
6900 while (pfile->dont_repeat_files)
6902 struct file_name_list *temp = pfile->dont_repeat_files;
6903 pfile->dont_repeat_files = temp->next;
6908 while (pfile->all_include_files)
6910 struct file_name_list *temp = pfile->all_include_files;
6911 pfile->all_include_files = temp->next;
6916 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6918 register struct import_file *imp = pfile->import_hash_table[i];
6921 struct import_file *next = imp->next;
6926 pfile->import_hash_table[i] = 0;
6929 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6931 while (pfile->assertion_hashtab[i])
6932 delete_assertion (pfile->assertion_hashtab[i]);
6935 cpp_hash_cleanup (pfile);
6939 do_assert (pfile, keyword, buf, limit)
6941 struct directive *keyword ATTRIBUTE_UNUSED;
6942 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
6944 long symstart; /* remember where symbol name starts */
6946 int sym_length; /* and how long it is */
6947 struct arglist *tokens = NULL;
6949 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6950 && !CPP_BUFFER (pfile)->system_header_p)
6951 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6953 cpp_skip_hspace (pfile);
6954 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6955 parse_name (pfile, GETC());
6956 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6959 cpp_skip_hspace (pfile);
6960 if (PEEKC() != '(') {
6961 cpp_error (pfile, "missing token-sequence in `#assert'");
6967 tokens = read_token_list (pfile, &error_flag);
6971 cpp_error (pfile, "empty token-sequence in `#assert'");
6974 cpp_skip_hspace (pfile);
6976 if (c != EOF && c != '\n')
6977 cpp_pedwarn (pfile, "junk at end of `#assert'");
6978 skip_rest_of_line (pfile);
6981 /* If this name isn't already an assertion name, make it one.
6982 Error if it was already in use in some other way. */
6985 ASSERTION_HASHNODE *hp;
6986 U_CHAR *symname = pfile->token_buffer + symstart;
6987 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6988 struct tokenlist_list *value
6989 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6991 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6993 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6994 cpp_error (pfile, "`defined' redefined as assertion");
6995 hp = assertion_install (pfile, symname, sym_length, hashcode);
6998 /* Add the spec'd token-sequence to the list of such. */
6999 value->tokens = tokens;
7000 value->next = hp->value;
7003 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7006 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7007 skip_rest_of_line (pfile);
7012 do_unassert (pfile, keyword, buf, limit)
7014 struct directive *keyword ATTRIBUTE_UNUSED;
7015 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
7017 long symstart; /* remember where symbol name starts */
7018 int sym_length; /* and how long it is */
7021 struct arglist *tokens = NULL;
7022 int tokens_specified = 0;
7024 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
7025 && !CPP_BUFFER (pfile)->system_header_p)
7026 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
7028 cpp_skip_hspace (pfile);
7030 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
7031 parse_name (pfile, GETC());
7032 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
7035 cpp_skip_hspace (pfile);
7036 if (PEEKC() == '(') {
7039 tokens = read_token_list (pfile, &error_flag);
7043 cpp_error (pfile, "empty token list in `#unassert'");
7047 tokens_specified = 1;
7050 cpp_skip_hspace (pfile);
7052 if (c != EOF && c != '\n')
7053 cpp_error (pfile, "junk at end of `#unassert'");
7054 skip_rest_of_line (pfile);
7057 ASSERTION_HASHNODE *hp;
7058 U_CHAR *symname = pfile->token_buffer + symstart;
7059 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
7060 struct tokenlist_list *tail, *prev;
7062 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
7066 /* If no token list was specified, then eliminate this assertion
7068 if (! tokens_specified)
7069 delete_assertion (hp);
7071 /* If a list of tokens was given, then delete any matching list. */
7076 struct tokenlist_list *next = tail->next;
7077 if (compare_token_lists (tail->tokens, tokens)) {
7081 hp->value = tail->next;
7082 free_token_list (tail->tokens);
7092 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7095 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7096 skip_rest_of_line (pfile);
7100 /* Test whether there is an assertion named NAME
7101 and optionally whether it has an asserted token list TOKENS.
7102 NAME is not null terminated; its length is SYM_LENGTH.
7103 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7106 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
7110 int tokens_specified;
7111 struct arglist *tokens;
7113 ASSERTION_HASHNODE *hp;
7114 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7116 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7117 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7119 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7121 /* It is not an assertion; just return false. */
7124 /* If no token list was specified, then value is 1. */
7125 if (! tokens_specified)
7129 struct tokenlist_list *tail;
7133 /* If a list of tokens was given,
7134 then succeed if the assertion records a matching list. */
7137 if (compare_token_lists (tail->tokens, tokens))
7142 /* Fail if the assertion has no matching list. */
7147 /* Compare two lists of tokens for equality including order of tokens. */
7150 compare_token_lists (l1, l2)
7151 struct arglist *l1, *l2;
7154 if (l1->length != l2->length)
7156 if (strncmp (l1->name, l2->name, l1->length))
7162 /* Succeed if both lists end at the same time. */
7167 reverse_token_list (tokens)
7168 struct arglist *tokens;
7170 register struct arglist *prev = 0, *this, *next;
7171 for (this = tokens; this; this = next)
7180 /* Read a space-separated list of tokens ending in a close parenthesis.
7181 Return a list of strings, in the order they were written.
7182 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7184 static struct arglist *
7185 read_token_list (pfile, error_flag)
7189 struct arglist *token_ptrs = 0;
7194 FORWARD (1); /* Skip '(' */
7196 /* Loop over the assertion value tokens. */
7199 struct arglist *temp;
7200 long name_written = CPP_WRITTEN (pfile);
7203 cpp_skip_hspace (pfile);
7207 /* Find the end of the token. */
7210 CPP_PUTC (pfile, c);
7218 CPP_PUTC (pfile, c);
7220 else if (c == '"' || c == '\'')
7223 cpp_get_token (pfile);
7229 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7230 && c != '"' && c != '\'')
7232 CPP_PUTC (pfile, c);
7235 if (c != EOF) FORWARD(-1);
7238 length = CPP_WRITTEN (pfile) - name_written;
7239 temp = (struct arglist *)
7240 xmalloc (sizeof (struct arglist) + length + 1);
7241 temp->name = (U_CHAR *) (temp + 1);
7242 bcopy ((char *) (pfile->token_buffer + name_written),
7243 (char *) temp->name, length);
7244 temp->name[length] = 0;
7245 temp->next = token_ptrs;
7247 temp->length = length;
7249 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7251 if (c == EOF || c == '\n')
7254 "unterminated token sequence following `#' operator");
7259 /* We accumulated the names in reverse order.
7260 Now reverse them to get the proper order. */
7261 return reverse_token_list (token_ptrs);
7265 free_token_list (tokens)
7266 struct arglist *tokens;
7269 struct arglist *next = tokens->next;
7270 free (tokens->name);
7276 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7277 retrying if necessary. If MAX_READ_LEN is defined, read at most
7278 that bytes at a time. Return a negative value if an error occurs,
7279 otherwise return the actual number of bytes read,
7280 which must be LEN unless end-of-file was reached. */
7283 safe_read (desc, ptr, len)
7288 int left, rcount, nchars;
7294 if (rcount > MAX_READ_LEN)
7295 rcount = MAX_READ_LEN;
7297 nchars = read (desc, ptr, rcount);
7315 xcalloc (number, size)
7316 unsigned number, size;
7318 register unsigned total = number * size;
7319 register char *ptr = (char *) xmalloc (total);
7328 unsigned size = strlen (input);
7329 char *output = xmalloc (size + 1);
7330 strcpy (output, input);
7334 /* Initialize PMARK to remember the current position of PFILE. */
7337 parse_set_mark (pmark, pfile)
7338 struct parse_marker *pmark;
7341 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7342 pmark->next = pbuf->marks;
7343 pbuf->marks = pmark;
7345 pmark->position = pbuf->cur - pbuf->buf;
7348 /* Cleanup PMARK - we no longer need it. */
7351 parse_clear_mark (pmark)
7352 struct parse_marker *pmark;
7354 struct parse_marker **pp = &pmark->buf->marks;
7355 for (; ; pp = &(*pp)->next) {
7356 if (*pp == NULL) abort ();
7357 if (*pp == pmark) break;
7362 /* Backup the current position of PFILE to that saved in PMARK. */
7365 parse_goto_mark (pmark, pfile)
7366 struct parse_marker *pmark;
7369 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7370 if (pbuf != pmark->buf)
7371 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
7372 pbuf->cur = pbuf->buf + pmark->position;
7375 /* Reset PMARK to point to the current position of PFILE. (Same
7376 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7379 parse_move_mark (pmark, pfile)
7380 struct parse_marker *pmark;
7383 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7384 if (pbuf != pmark->buf)
7385 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
7386 pmark->position = pbuf->cur - pbuf->buf;
7390 cpp_read_check_assertion (pfile)
7393 int name_start = CPP_WRITTEN (pfile);
7394 int name_length, name_written;
7396 FORWARD (1); /* Skip '#' */
7397 cpp_skip_hspace (pfile);
7398 parse_name (pfile, GETC ());
7399 name_written = CPP_WRITTEN (pfile);
7400 name_length = name_written - name_start;
7401 cpp_skip_hspace (pfile);
7402 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7405 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7406 result = check_assertion (pfile,
7407 pfile->token_buffer + name_start, name_length,
7411 result = check_assertion (pfile,
7412 pfile->token_buffer + name_start, name_length,
7414 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7419 cpp_print_file_and_line (pfile)
7422 cpp_buffer *ip = cpp_file_buffer (pfile);
7427 cpp_buf_line_and_col (ip, &line, &col);
7428 cpp_file_line_for_message (pfile, ip->nominal_fname,
7429 line, pfile->show_column ? col : -1);
7434 v_cpp_error (pfile, msg, ap)
7439 cpp_print_containing_files (pfile);
7440 cpp_print_file_and_line (pfile);
7441 v_cpp_message (pfile, 1, msg, ap);
7445 cpp_error VPROTO ((cpp_reader * pfile, const char *msg, ...))
7456 pfile = va_arg (ap, cpp_reader *);
7457 msg = va_arg (ap, const char *);
7460 v_cpp_error (pfile, msg, ap);
7464 /* Print error message but don't count it. */
7467 v_cpp_warning (pfile, msg, ap)
7472 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7475 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7478 cpp_print_containing_files (pfile);
7479 cpp_print_file_and_line (pfile);
7480 v_cpp_message (pfile, 0, msg, ap);
7484 cpp_warning VPROTO ((cpp_reader * pfile, const char *msg, ...))
7495 pfile = va_arg (ap, cpp_reader *);
7496 msg = va_arg (ap, const char *);
7499 v_cpp_warning (pfile, msg, ap);
7503 /* Print an error message and maybe count it. */
7506 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msg, ...))
7517 pfile = va_arg (ap, cpp_reader *);
7518 msg = va_arg (ap, const char *);
7521 if (CPP_OPTIONS (pfile)->pedantic_errors)
7522 v_cpp_error (pfile, msg, ap);
7524 v_cpp_warning (pfile, msg, ap);
7529 v_cpp_error_with_line (pfile, line, column, msg, ap)
7536 cpp_buffer *ip = cpp_file_buffer (pfile);
7538 cpp_print_containing_files (pfile);
7541 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7543 v_cpp_message (pfile, 1, msg, ap);
7547 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
7560 pfile = va_arg (ap, cpp_reader *);
7561 line = va_arg (ap, int);
7562 column = va_arg (ap, int);
7563 msg = va_arg (ap, const char *);
7566 v_cpp_error_with_line(pfile, line, column, msg, ap);
7571 v_cpp_warning_with_line (pfile, line, column, msg, ap)
7580 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7583 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7586 cpp_print_containing_files (pfile);
7588 ip = cpp_file_buffer (pfile);
7591 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7593 v_cpp_message (pfile, 0, msg, ap);
7598 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
7611 pfile = va_arg (ap, cpp_reader *);
7612 line = va_arg (ap, int);
7613 column = va_arg (ap, int);
7614 msg = va_arg (ap, const char *);
7617 v_cpp_warning_with_line (pfile, line, column, msg, ap);
7623 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
7636 pfile = va_arg (ap, cpp_reader *);
7637 line = va_arg (ap, int);
7638 column = va_arg (ap, int);
7639 msg = va_arg (ap, const char *);
7642 if (CPP_OPTIONS (pfile)->pedantic_errors)
7643 v_cpp_error_with_line (pfile, column, line, msg, ap);
7645 v_cpp_warning_with_line (pfile, line, column, msg, ap);
7649 /* Report a warning (or an error if pedantic_errors)
7650 giving specified file name and line number, not current. */
7653 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line, const char *msg, ...))
7666 pfile = va_arg (ap, cpp_reader *);
7667 file = va_arg (ap, char *);
7668 line = va_arg (ap, int);
7669 msg = va_arg (ap, const char *);
7672 if (!CPP_OPTIONS (pfile)->pedantic_errors
7673 && CPP_OPTIONS (pfile)->inhibit_warnings)
7676 cpp_file_line_for_message (pfile, file, line, -1);
7677 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msg, ap);
7681 /* my_strerror - return the descriptive text associated with an
7685 my_strerror (errnum)
7691 #ifndef HAVE_STRERROR
7692 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7694 result = strerror (errnum);
7697 /* VAXCRTL's strerror() takes an optional second argument, which only
7698 matters when the first argument is EVMSERR. However, it's simplest
7699 just to pass it unconditionally. `vaxc$errno' is declared in
7700 <errno.h>, and maintained by the library in parallel with `errno'.
7701 We assume that caller's `errnum' either matches the last setting of
7702 `errno' by the library or else does not have the value `EVMSERR'. */
7704 result = strerror (errnum, vaxc$errno);
7708 result = "undocumented I/O error";
7713 /* Error including a message from `errno'. */
7716 cpp_error_from_errno (pfile, name)
7720 cpp_message_from_errno (pfile, 1, name);
7724 cpp_message_from_errno (pfile, is_error, name)
7730 cpp_buffer *ip = cpp_file_buffer (pfile);
7732 cpp_print_containing_files (pfile);
7735 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7737 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
7741 cpp_perror_with_name (pfile, name)
7745 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7749 * No pre-compiled header file support.
7751 * Possibly different enum token codes for each C/C++ token.
7753 * Should clean up remaining directives to that do_XXX functions
7754 * only take two arguments and all have command_reads_line.
7756 * Find and cleanup remaining uses of static variables,
7758 * Support for trigraphs.
7760 * Support -dM flag (dump_all_macros).
7762 * Support for_lint flag.