1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #define PRINTF_PROTO(ARGS, m, n) PVPROTO (ARGS) ATTRIBUTE_PRINTF(m, n)
25 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
26 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
27 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
28 #define PRINTF_PROTO_4(ARGS) PRINTF_PROTO(ARGS, 4, 5)
33 #ifdef HAVE_SYS_RESOURCE_H
34 # include <sys/resource.h>
37 typedef unsigned char U_CHAR;
42 #ifdef MULTIBYTE_CHARS
45 #endif /* MULTIBYTE_CHARS */
47 #ifndef GET_ENV_PATH_LIST
48 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
51 #ifndef STANDARD_INCLUDE_DIR
52 # define STANDARD_INCLUDE_DIR "/usr/include"
55 /* By default, colon separates directories in a path. */
56 #ifndef PATH_SEPARATOR
57 # define PATH_SEPARATOR ':'
60 /* By default, the suffix for object files is ".o". */
62 # define HAVE_OBJECT_SUFFIX
64 # define OBJECT_SUFFIX ".o"
67 /* VMS-specific definitions */
72 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
73 #define fopen(fname,mode) VMS_fopen (fname,mode)
74 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
75 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
76 static int VMS_fstat (), VMS_stat ();
77 static int VMS_open ();
78 static FILE *VMS_fopen ();
79 static FILE *VMS_freopen ();
80 static int hack_vms_include_specification ();
81 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
82 #define INO_T_HASH(a) 0
83 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
86 /* Windows does not natively support inodes, and neither does MSDOS. */
87 #if (defined (_WIN32) && ! defined (__CYGWIN__)) || defined (__MSDOS__)
88 #define INO_T_EQ(a, b) 0
91 /* Find the largest host integer type and set its size and type.
92 Watch out: on some crazy hosts `long' is shorter than `int'. */
96 # include <inttypes.h>
97 # define HOST_WIDE_INT intmax_t
99 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
100 # define HOST_WIDE_INT int
102 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
103 # define HOST_WIDE_INT long
105 # define HOST_WIDE_INT long long
112 #define INO_T_EQ(a, b) ((a) == (b))
116 #define INO_T_HASH(a) (a)
119 #ifndef INCLUDE_LEN_FUDGE
120 #define INCLUDE_LEN_FUDGE 0
123 /* External declarations. */
125 extern char *version_string;
126 HOST_WIDE_INT parse_escape PROTO((char **, HOST_WIDE_INT));
127 HOST_WIDE_INT parse_c_expression PROTO((char *, int));
129 /* Name under which this program was invoked. */
131 static char *progname;
133 /* Nonzero means use extra default include directories for C++. */
135 static int cplusplus;
137 /* Nonzero means handle cplusplus style comments */
139 static int cplusplus_comments;
141 /* Nonzero means handle #import, for objective C. */
145 /* Nonzero means this is an assembly file, and allow
146 unknown directives, which could be comments. */
150 /* Current maximum length of directory names in the search path
151 for include files. (Altered as we get more of them.) */
153 static int max_include_len;
155 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
157 static int for_lint = 0;
159 /* Nonzero means copy comments into the output file. */
161 static int put_out_comments = 0;
163 /* Nonzero means don't process the ANSI trigraph sequences. */
165 static int no_trigraphs = 0;
167 /* Nonzero means print the names of included files rather than
168 the preprocessed output. 1 means just the #include "...",
169 2 means #include <...> as well. */
171 static int print_deps = 0;
173 /* Nonzero if missing .h files in -M output are assumed to be generated
174 files and not errors. */
176 static int print_deps_missing_files = 0;
178 /* Nonzero means print names of header files (-H). */
180 static int print_include_names = 0;
182 /* Nonzero means don't output line number information. */
184 static int no_line_directives;
186 /* Nonzero means output the text in failing conditionals,
187 inside #failed ... #endfailed. */
189 static int output_conditionals;
191 /* dump_only means inhibit output of the preprocessed text
192 and instead output the definitions of all user-defined
193 macros in a form suitable for use as input to cccp.
194 dump_names means pass #define and the macro name through to output.
195 dump_definitions means pass the whole definition (plus #define) through
198 static enum {dump_none, dump_only, dump_names, dump_definitions}
199 dump_macros = dump_none;
201 /* Nonzero means pass all #define and #undef directives which we actually
202 process through to the output stream. This feature is used primarily
203 to allow cc1 to record the #defines and #undefs for the sake of
204 debuggers which understand about preprocessor macros, but it may
205 also be useful with -E to figure out how symbols are defined, and
206 where they are defined. */
207 static int debug_output = 0;
209 /* Nonzero means pass #include lines through to the output,
210 even if they are ifdefed out. */
211 static int dump_includes;
213 /* Nonzero indicates special processing used by the pcp program. The
214 special effects of this mode are:
216 Inhibit all macro expansion, except those inside #if directives.
218 Process #define directives normally, and output their contents
221 Output preconditions to pcp_outfile indicating all the relevant
222 preconditions for use of this file in a later cpp run.
224 static FILE *pcp_outfile;
226 /* Nonzero means we are inside an IF during a -pcp run. In this mode
227 macro expansion is done, and preconditions are output for all macro
228 uses requiring them. */
229 static int pcp_inside_if;
231 /* Nonzero means never to include precompiled files.
232 This is 1 since there's no way now to make precompiled files,
233 so it's not worth testing for them. */
234 static int no_precomp = 1;
236 /* Nonzero means give all the error messages the ANSI standard requires. */
240 /* Nonzero means try to make failure to fit ANSI C an error. */
242 static int pedantic_errors;
244 /* Nonzero means don't print warning messages. -w. */
246 static int inhibit_warnings = 0;
248 /* Nonzero means warn if slash-star appears in a slash-star comment,
249 or if newline-backslash appears in a slash-slash comment. */
251 static int warn_comments;
253 /* Nonzero means warn if a macro argument is (or would be)
254 stringified with -traditional. */
256 static int warn_stringify;
258 /* Nonzero means warn if there are any trigraphs. */
260 static int warn_trigraphs;
262 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
264 static int warn_undef;
266 /* Nonzero means warn if #import is used. */
268 static int warn_import = 1;
270 /* Nonzero means turn warnings into errors. */
272 static int warnings_are_errors;
274 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
278 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
282 /* Nonzero for the 199x C Standard. */
286 /* Nonzero causes output not to be done,
287 but directives such as #define that have side effects
290 static int no_output;
292 /* Nonzero means we should look for header.gcc files that remap file names. */
295 /* Nonzero means this file was included with a -imacros or -include
296 command line and should not be recorded as an include file. */
298 static int no_record_file;
300 /* Nonzero means that we have finished processing the command line options.
301 This flag is used to decide whether or not to issue certain errors
304 static int done_initializing = 0;
306 /* Line where a newline was first seen in a string constant. */
308 static int multiline_string_line = 0;
310 /* I/O buffer structure.
311 The `fname' field is nonzero for source files and #include files
312 and for the dummy text used for -D and -U.
313 It is zero for rescanning results of macro expansion
314 and for expanding macro arguments. */
315 #define INPUT_STACK_MAX 400
316 static struct file_buf {
318 /* Filename specified with #line directive. */
320 /* The length of nominal_fname, which may contain embedded NULs. */
321 size_t nominal_fname_len;
322 /* Include file description. */
323 struct include_file *inc;
324 /* Record where in the search path this file was found.
325 For #include_next. */
326 struct file_name_list *dir;
331 /* Macro that this level is the expansion of.
332 Included so that we can reenable the macro
333 at the end of this level. */
334 struct hashnode *macro;
335 /* Value of if_stack at start of this file.
336 Used to prohibit unmatched #endif (etc) in an include file. */
337 struct if_stack *if_stack;
338 /* Object to be freed at end of input at this level. */
340 /* True if this is a system header file; see is_system_include. */
341 char system_header_p;
342 } instack[INPUT_STACK_MAX];
344 static int last_error_tick; /* Incremented each time we print it. */
345 static int input_file_stack_tick; /* Incremented when the status changes. */
347 /* Current nesting level of input sources.
348 `instack[indepth]' is the level currently being read. */
349 static int indepth = -1;
350 #define CHECK_DEPTH(code) \
351 if (indepth >= (INPUT_STACK_MAX - 1)) \
353 error_with_line (line_for_error (instack[indepth].lineno), \
354 "macro or `#include' recursion too deep"); \
358 /* Current depth in #include directives that use <...>. */
359 static int system_include_depth = 0;
361 typedef struct file_buf FILE_BUF;
363 /* The output buffer. Its LENGTH field is the amount of room allocated
364 for the buffer, not the number of chars actually present. To get
365 that, subtract outbuf.buf from outbuf.bufp. */
367 #define OUTBUF_SIZE 10 /* initial size of output buffer */
368 static FILE_BUF outbuf;
370 /* Grow output buffer OBUF points at
371 so it can hold at least NEEDED more chars. */
373 #define check_expand(OBUF, NEEDED) \
374 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
375 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
377 struct file_name_list
379 struct file_name_list *next;
380 /* If the following is 1, it is a C-language system include
382 int c_system_include_path;
383 /* Mapping of file names for this directory. */
384 struct file_name_map *name_map;
385 /* Non-zero if name_map is valid. */
387 /* The include directory status. */
389 /* The include prefix: "" denotes the working directory,
390 otherwise fname must end in '/'.
391 The actual size is dynamically allocated. */
395 /* #include "file" looks in source file dir, then stack. */
396 /* #include <file> just looks in the stack. */
397 /* -I directories are added to the end, then the defaults are added. */
399 static struct default_include {
400 char *fname; /* The name of the directory. */
401 char *component; /* The component containing the directory */
402 int cplusplus; /* Only look here if we're compiling C++. */
403 int cxx_aware; /* Includes in this directory don't need to
404 be wrapped in extern "C" when compiling
406 } include_defaults_array[]
407 #ifdef INCLUDE_DEFAULTS
411 /* Pick up GNU C++ specific include files. */
412 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
414 /* This is the dir for fixincludes. Put it just before
415 the files that we fix. */
416 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
417 /* For cross-compilation, this dir name is generated
418 automatically in Makefile.in. */
419 { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
420 #ifdef TOOL_INCLUDE_DIR
421 /* This is another place that the target system's headers might be. */
422 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
424 #else /* not CROSS_COMPILE */
425 #ifdef LOCAL_INCLUDE_DIR
426 /* This should be /usr/local/include and should come before
427 the fixincludes-fixed header files. */
428 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
430 #ifdef TOOL_INCLUDE_DIR
431 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
432 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
433 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
435 /* This is the dir for fixincludes. Put it just before
436 the files that we fix. */
437 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
438 /* Some systems have an extra dir of include files. */
439 #ifdef SYSTEM_INCLUDE_DIR
440 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
442 #ifndef STANDARD_INCLUDE_COMPONENT
443 #define STANDARD_INCLUDE_COMPONENT 0
445 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
446 #endif /* not CROSS_COMPILE */
449 #endif /* no INCLUDE_DEFAULTS */
451 /* The code looks at the defaults through this pointer, rather than through
452 the constant structure above. This pointer gets changed if an environment
453 variable specifies other defaults. */
454 static struct default_include *include_defaults = include_defaults_array;
456 static struct file_name_list *include = 0; /* First dir to search */
457 /* First dir to search for <file> */
458 /* This is the first element to use for #include <...>.
459 If it is 0, use the entire chain for such includes. */
460 static struct file_name_list *first_bracket_include = 0;
461 /* This is the first element in the chain that corresponds to
462 a directory of system header files. */
463 static struct file_name_list *first_system_include = 0;
464 static struct file_name_list *last_include = 0; /* Last in chain */
466 /* Chain of include directories to put at the end of the other chain. */
467 static struct file_name_list *after_include = 0;
468 static struct file_name_list *last_after_include = 0; /* Last in chain */
470 /* Chain to put at the start of the system include files. */
471 static struct file_name_list *before_system = 0;
472 static struct file_name_list *last_before_system = 0; /* Last in chain */
474 /* Directory prefix that should replace `/usr' in the standard
475 include file directories. */
476 static char *include_prefix;
478 /* Maintain and search list of included files. */
480 struct include_file {
481 struct include_file *next; /* for include_hashtab */
482 struct include_file *next_ino; /* for include_ino_hashtab */
484 /* If the following is the empty string, it means #pragma once
485 was seen in this include file, or #import was applied to the file.
486 Otherwise, if it is nonzero, it is a macro name.
487 Don't include the file again if that macro is defined. */
488 U_CHAR *control_macro;
489 /* Nonzero if the dependency on this include file has been output. */
494 /* Hash tables of files already included with #include or #import.
495 include_hashtab is by full name; include_ino_hashtab is by inode number. */
497 #define INCLUDE_HASHSIZE 61
498 static struct include_file *include_hashtab[INCLUDE_HASHSIZE];
499 static struct include_file *include_ino_hashtab[INCLUDE_HASHSIZE];
501 /* Global list of strings read in from precompiled files. This list
502 is kept in the order the strings are read in, with new strings being
503 added at the end through stringlist_tailp. We use this list to output
504 the strings at the end of the run.
506 static STRINGDEF *stringlist;
507 static STRINGDEF **stringlist_tailp = &stringlist;
510 /* Structure returned by create_definition */
511 typedef struct macrodef MACRODEF;
514 struct definition *defn;
519 enum sharp_token_type {
520 NO_SHARP_TOKEN = 0, /* token not present */
522 SHARP_TOKEN = '#', /* token spelled with # only */
523 WHITE_SHARP_TOKEN, /* token spelled with # and white space */
525 PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */
526 WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */
529 /* Structure allocated for every #define. For a simple replacement
532 nargs = -1, the `pattern' list is null, and the expansion is just
533 the replacement text. Nargs = 0 means a functionlike macro with no args,
535 #define getchar() getc (stdin) .
536 When there are args, the expansion is the replacement text with the
537 args squashed out, and the reflist is a list describing how to
538 build the output from the input: e.g., "3 chars, then the 1st arg,
539 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
540 The chars here come from the expansion. Whatever is left of the
541 expansion after the last arg-occurrence is copied after that arg.
542 Note that the reflist can be arbitrarily long---
543 its length depends on the number of times the arguments appear in
544 the replacement text, not how many args there are. Example:
545 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
547 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
548 where (x, y) means (nchars, argno). */
550 typedef struct definition DEFINITION;
553 int length; /* length of expansion string */
554 int predefined; /* True if the macro was builtin or */
555 /* came from the command line */
557 int line; /* Line number of definition */
558 char *file; /* File of definition */
559 size_t file_len; /* Length of file (which can contain NULs) */
560 char rest_args; /* Nonzero if last arg. absorbs the rest */
562 struct reflist *next;
564 enum sharp_token_type stringify; /* set if a # operator before arg */
565 enum sharp_token_type raw_before; /* set if a ## operator before arg */
566 enum sharp_token_type raw_after; /* set if a ## operator after arg */
568 char rest_args; /* Nonzero if this arg. absorbs the rest */
569 int nchars; /* Number of literal chars to copy before
570 this arg occurrence. */
571 int argno; /* Number of arg to substitute (origin-0) */
574 /* Names of macro args, concatenated in reverse order
575 with comma-space between them.
576 The only use of this is that we warn on redefinition
577 if this differs between the old and new definitions. */
582 /* different kinds of things that can appear in the value field
583 of a hash node. Actually, this may be useless now. */
591 * special extension string that can be added to the last macro argument to
592 * allow it to absorb the "rest" of the arguments when expanded. Ex:
593 * #define wow(a, b...) process (b, a, b)
594 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
595 * { wow (one, two); } -> { process (two, one, two); }
596 * if this "rest_arg" is used with the concat token '##' and if it is not
597 * supplied then the token attached to with ## will not be outputted. Ex:
598 * #define wow (a, b...) process (b ## , a, ## b)
599 * { wow (1, 2); } -> { process (2, 1, 2); }
600 * { wow (one); } -> { process (one); {
602 static char rest_extension[] = "...";
603 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
605 /* This is the implicit parameter name when using variable number of
606 parameters for macros using the ISO C 9x extension. */
607 static char va_args_name[] = "__VA_ARGS__";
608 #define VA_ARGS_NAME_LENGTH (sizeof (va_args_name) - 1)
610 /* The structure of a node in the hash table. The hash table
611 has entries for all tokens defined by #define directives (type T_MACRO),
612 plus some special tokens like __LINE__ (these each have their own
613 type, and the appropriate code is run when that type of node is seen.
614 It does not contain control words like "#define", which are recognized
615 by a separate piece of code. */
617 /* different flavors of hash nodes --- also used in keyword table */
619 T_DEFINE = 1, /* the `#define' keyword */
620 T_INCLUDE, /* the `#include' keyword */
621 T_INCLUDE_NEXT, /* the `#include_next' keyword */
622 T_IMPORT, /* the `#import' keyword */
623 T_IFDEF, /* the `#ifdef' keyword */
624 T_IFNDEF, /* the `#ifndef' keyword */
625 T_IF, /* the `#if' keyword */
626 T_ELSE, /* `#else' */
627 T_PRAGMA, /* `#pragma' */
628 T_ELIF, /* `#elif' */
629 T_UNDEF, /* `#undef' */
630 T_LINE, /* `#line' */
631 T_ERROR, /* `#error' */
632 T_WARNING, /* `#warning' */
633 T_ENDIF, /* `#endif' */
634 T_SCCS, /* `#sccs', used on system V. */
635 T_IDENT, /* `#ident', used on system V. */
636 T_ASSERT, /* `#assert', taken from system V. */
637 T_UNASSERT, /* `#unassert', taken from system V. */
638 T_SPECLINE, /* special symbol `__LINE__' */
639 T_DATE, /* `__DATE__' */
640 T_FILE, /* `__FILE__' */
641 T_BASE_FILE, /* `__BASE_FILE__' */
642 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
643 T_VERSION, /* `__VERSION__' */
644 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
645 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
646 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
647 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
648 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
649 T_IMMEDIATE_PREFIX_TYPE, /* `__IMMEDIATE_PREFIX__' */
650 T_TIME, /* `__TIME__' */
651 T_CONST, /* Constant value, used by `__STDC__' */
652 T_MACRO, /* macro defined by `#define' */
653 T_DISABLED, /* macro temporarily turned off for rescan */
654 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
655 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
656 T_UNUSED /* Used for something not defined. */
660 struct hashnode *next; /* double links for easy deletion */
661 struct hashnode *prev;
662 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
663 chain is kept, in case the node is the head
664 of the chain and gets deleted. */
665 enum node_type type; /* type of special token */
666 int length; /* length of token, for quick comparison */
667 U_CHAR *name; /* the actual name */
668 union hashval value; /* pointer to expansion, or whatever */
671 typedef struct hashnode HASHNODE;
673 /* Some definitions for the hash table. The hash function MUST be
674 computed as shown in hashf () below. That is because the rescan
675 loop computes the hash value `on the fly' for most tokens,
676 in order to avoid the overhead of a lot of procedure calls to
677 the hashf () function. Hashf () only exists for the sake of
678 politeness, for use when speed isn't so important. */
680 #define HASHSIZE 1403
681 static HASHNODE *hashtab[HASHSIZE];
682 #define HASHSTEP(old, c) ((old << 2) + c)
683 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
685 /* Symbols to predefine. */
687 #ifdef CPP_PREDEFINES
688 static char *predefs = CPP_PREDEFINES;
690 static char *predefs = "";
693 /* We let tm.h override the types used here, to handle trivial differences
694 such as the choice of unsigned int or long unsigned int for size_t.
695 When machines start needing nontrivial differences in the size type,
696 it would be best to do something here to figure out automatically
697 from other information what type to use. */
699 /* The string value for __SIZE_TYPE__. */
702 #define SIZE_TYPE "long unsigned int"
705 /* The string value for __PTRDIFF_TYPE__. */
708 #define PTRDIFF_TYPE "long int"
711 /* The string value for __WCHAR_TYPE__. */
714 #define WCHAR_TYPE "int"
716 char * wchar_type = WCHAR_TYPE;
719 /* The string value for __USER_LABEL_PREFIX__ */
721 #ifndef USER_LABEL_PREFIX
722 #define USER_LABEL_PREFIX ""
724 char * user_label_prefix = USER_LABEL_PREFIX;
725 #undef USER_LABEL_PREFIX
727 /* The string value for __REGISTER_PREFIX__ */
729 #ifndef REGISTER_PREFIX
730 #define REGISTER_PREFIX ""
733 /* The string value for __IMMEDIATE_PREFIX__ */
735 #ifndef IMMEDIATE_PREFIX
736 #define IMMEDIATE_PREFIX ""
739 /* In the definition of a #assert name, this structure forms
740 a list of the individual values asserted.
741 Each value is itself a list of "tokens".
742 These are strings that are compared by name. */
744 struct tokenlist_list {
745 struct tokenlist_list *next;
746 struct arglist *tokens;
749 struct assertion_hashnode {
750 struct assertion_hashnode *next; /* double links for easy deletion */
751 struct assertion_hashnode *prev;
752 /* also, a back pointer to this node's hash
753 chain is kept, in case the node is the head
754 of the chain and gets deleted. */
755 struct assertion_hashnode **bucket_hdr;
756 int length; /* length of token, for quick comparison */
757 U_CHAR *name; /* the actual name */
758 /* List of token-sequences. */
759 struct tokenlist_list *value;
762 typedef struct assertion_hashnode ASSERTION_HASHNODE;
764 /* Some definitions for the hash table. The hash function MUST be
765 computed as shown in hashf below. That is because the rescan
766 loop computes the hash value `on the fly' for most tokens,
767 in order to avoid the overhead of a lot of procedure calls to
768 the hashf function. hashf only exists for the sake of
769 politeness, for use when speed isn't so important. */
771 #define ASSERTION_HASHSIZE 37
772 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
774 /* Nonzero means inhibit macroexpansion of what seem to be
775 assertion tests, in rescan. For #if. */
776 static int assertions_flag;
778 /* `struct directive' defines one #-directive, including how to handle it. */
780 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
783 int length; /* Length of name */
784 int (*func) DO_PROTO; /* Function to handle directive */
785 char *name; /* Name of directive */
786 enum node_type type; /* Code which describes which directive. */
789 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
790 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
792 /* These functions are declared to return int instead of void since they
793 are going to be placed in the table and some old compilers have trouble with
794 pointers to functions returning void. */
796 static int do_assert DO_PROTO;
797 static int do_define DO_PROTO;
798 static int do_elif DO_PROTO;
799 static int do_else DO_PROTO;
800 static int do_endif DO_PROTO;
801 static int do_error DO_PROTO;
802 static int do_ident DO_PROTO;
803 static int do_if DO_PROTO;
804 static int do_include DO_PROTO;
805 static int do_line DO_PROTO;
806 static int do_pragma DO_PROTO;
807 #ifdef SCCS_DIRECTIVE
808 static int do_sccs DO_PROTO;
810 static int do_unassert DO_PROTO;
811 static int do_undef DO_PROTO;
812 static int do_warning DO_PROTO;
813 static int do_xifdef DO_PROTO;
815 /* Here is the actual list of #-directives, most-often-used first. */
817 static struct directive directive_table[] = {
818 { 6, do_define, "define", T_DEFINE},
819 { 2, do_if, "if", T_IF},
820 { 5, do_xifdef, "ifdef", T_IFDEF},
821 { 6, do_xifdef, "ifndef", T_IFNDEF},
822 { 5, do_endif, "endif", T_ENDIF},
823 { 4, do_else, "else", T_ELSE},
824 { 4, do_elif, "elif", T_ELIF},
825 { 4, do_line, "line", T_LINE},
826 { 7, do_include, "include", T_INCLUDE},
827 { 12, do_include, "include_next", T_INCLUDE_NEXT},
828 { 6, do_include, "import", T_IMPORT},
829 { 5, do_undef, "undef", T_UNDEF},
830 { 5, do_error, "error", T_ERROR},
831 { 7, do_warning, "warning", T_WARNING},
832 #ifdef SCCS_DIRECTIVE
833 { 4, do_sccs, "sccs", T_SCCS},
835 { 6, do_pragma, "pragma", T_PRAGMA},
836 { 5, do_ident, "ident", T_IDENT},
837 { 6, do_assert, "assert", T_ASSERT},
838 { 8, do_unassert, "unassert", T_UNASSERT},
839 { -1, 0, "", T_UNUSED},
842 /* When a directive handler is called,
843 this points to the # (or the : of the %:) that started the directive. */
844 U_CHAR *directive_start;
846 /* table to tell if char can be part of a C identifier. */
847 U_CHAR is_idchar[256];
848 /* table to tell if char can be first char of a c identifier. */
849 U_CHAR is_idstart[256];
850 /* table to tell if c is horizontal space. */
851 static U_CHAR is_hor_space[256];
852 /* table to tell if c is horizontal or vertical space. */
853 U_CHAR is_space[256];
854 /* names of some characters */
855 static char *char_name[256];
857 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
858 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
860 static int errors = 0; /* Error counter for exit code */
862 /* Name of output file, for error messages. */
863 static char *out_fname;
865 /* Nonzero to ignore \ in string constants. Use to treat #line 1 "A:\file.h
866 as a non-form feed. If you want it to be a form feed, you must use
868 static int ignore_escape_flag = 1;
870 /* Stack of conditionals currently in progress
871 (including both successful and failing conditionals). */
874 struct if_stack *next; /* for chaining to the next stack frame */
875 char *fname; /* copied from input when frame is made */
876 size_t fname_len; /* similarly */
877 int lineno; /* similarly */
878 int if_succeeded; /* true if a leg of this if-group
879 has been passed through rescan */
880 U_CHAR *control_macro; /* For #ifndef at start of file,
881 this is the macro name tested. */
882 enum node_type type; /* type of last directive seen in this group */
884 typedef struct if_stack IF_STACK_FRAME;
885 static IF_STACK_FRAME *if_stack = NULL;
887 /* Buffer of -M output. */
888 static char *deps_buffer;
890 /* Number of bytes allocated in above. */
891 static int deps_allocated_size;
893 /* Number of bytes used. */
894 static int deps_size;
896 /* Number of bytes since the last newline. */
897 static int deps_column;
899 /* Nonzero means -I- has been seen,
900 so don't look for #include "foo" the source-file directory. */
901 static int ignore_srcdir;
903 static int safe_read PROTO((int, char *, int));
904 static void safe_write PROTO((int, char *, int));
905 static void eprint_string PROTO((char *, size_t));
907 int main PROTO((int, char **));
909 static void path_include PROTO((char *));
911 static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
913 static void trigraph_pcp PROTO((FILE_BUF *));
915 static void newline_fix PROTO((U_CHAR *));
916 static void name_newline_fix PROTO((U_CHAR *));
918 static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
920 static void rescan PROTO((FILE_BUF *, int));
922 static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
924 static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
926 static struct tm *timestamp PROTO((void));
927 static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
929 static int is_system_include PROTO((char *));
930 static char *base_name PROTO((char *));
931 static int absolute_filename PROTO((char *));
932 static size_t simplify_filename PROTO((char *));
934 static char *read_filename_string PROTO((int, FILE *));
935 static struct file_name_map *read_name_map PROTO((char *));
936 static int open_include_file PROTO((char *, struct file_name_list *, U_CHAR *, struct include_file **));
937 static char *remap_include_file PROTO((char *, struct file_name_list *));
938 static int lookup_ino_include PROTO((struct include_file *));
940 static void finclude PROTO((int, struct include_file *, FILE_BUF *, int, struct file_name_list *));
941 static void record_control_macro PROTO((struct include_file *, U_CHAR *));
943 static char *check_precompiled PROTO((int, struct stat *, char *, char **));
944 static int check_preconditions PROTO((char *));
945 static void pcfinclude PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
946 static void pcstring_used PROTO((HASHNODE *));
947 static void write_output PROTO((void));
948 static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
950 static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
952 static int check_macro_name PROTO((U_CHAR *, char *));
953 static int compare_defs PROTO((DEFINITION *, DEFINITION *));
954 static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
956 static DEFINITION *collect_expansion PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
958 int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
959 static int compare_token_lists PROTO((struct arglist *, struct arglist *));
961 static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
962 static void free_token_list PROTO((struct arglist *));
964 static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
965 static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
966 static void delete_assertion PROTO((ASSERTION_HASHNODE *));
968 static void do_once PROTO((void));
970 static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
971 static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
972 static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
973 static void validate_else PROTO((U_CHAR *, U_CHAR *));
975 static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
976 static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
977 static char *quote_string PROTO((char *, char *, size_t));
978 static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
980 /* Last arg to output_line_directive. */
981 enum file_change_code {same_file, enter_file, leave_file};
982 static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
984 static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
987 static char *macarg PROTO((struct argdata *, int));
989 static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, struct hashnode *, int *, int *, int *, int));
991 static int discard_comments PROTO((U_CHAR *, int, int));
993 static int change_newlines PROTO((U_CHAR *, int));
995 static char *my_strerror PROTO((int));
996 void error PRINTF_PROTO_1((char *, ...));
997 static void verror PROTO((char *, va_list));
998 static void error_from_errno PROTO((char *));
999 void warning PRINTF_PROTO_1((char *, ...));
1000 static void vwarning PROTO((char *, va_list));
1001 static void error_with_line PRINTF_PROTO_2((int, char *, ...));
1002 static void verror_with_line PROTO((int, char *, va_list));
1003 static void vwarning_with_line PROTO((int, char *, va_list));
1004 static void warning_with_line PRINTF_PROTO_2((int, char *, ...));
1005 void pedwarn PRINTF_PROTO_1((char *, ...));
1006 void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
1007 static void pedwarn_with_file_and_line PRINTF_PROTO_4((char *, size_t, int, char *, ...));
1009 static void print_containing_files PROTO((void));
1011 static int line_for_error PROTO((int));
1012 static int grow_outbuf PROTO((FILE_BUF *, int));
1014 static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
1015 HASHNODE *lookup PROTO((U_CHAR *, int, int));
1016 static void delete_macro PROTO((HASHNODE *));
1017 static int hashf PROTO((U_CHAR *, int, int));
1019 static void dump_single_macro PROTO((HASHNODE *, FILE *));
1020 static void dump_all_macros PROTO((void));
1021 static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
1022 static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
1024 static void initialize_char_syntax PROTO((void));
1025 static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
1027 static void make_definition PROTO((char *));
1028 static void make_undef PROTO((char *, FILE_BUF *));
1030 static void make_assertion PROTO((char *, char *));
1032 static struct file_name_list *new_include_prefix PROTO((struct file_name_list *, const char *, const char *, const char *));
1033 static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
1035 static int quote_string_for_make PROTO((char *, char *));
1036 static void deps_output PROTO((char *, int));
1038 static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
1039 void fancy_abort PROTO((void)) __attribute__ ((noreturn));
1040 static void perror_with_name PROTO((char *));
1041 static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
1042 static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
1044 static void memory_full PROTO((void)) __attribute__ ((noreturn));
1045 static char *savestring PROTO((char *));
1046 static void print_help PROTO((void));
1048 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1049 retrying if necessary. If MAX_READ_LEN is defined, read at most
1050 that bytes at a time. Return a negative value if an error occurs,
1051 otherwise return the actual number of bytes read,
1052 which must be LEN unless end-of-file was reached. */
1055 safe_read (desc, ptr, len)
1060 int left, rcount, nchars;
1066 if (rcount > MAX_READ_LEN)
1067 rcount = MAX_READ_LEN;
1069 nchars = read (desc, ptr, rcount);
1086 /* Write LEN bytes at PTR to descriptor DESC,
1087 retrying if necessary, and treating any real error as fatal.
1088 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1091 safe_write (desc, ptr, len)
1096 int wcount, written;
1100 #ifdef MAX_WRITE_LEN
1101 if (wcount > MAX_WRITE_LEN)
1102 wcount = MAX_WRITE_LEN;
1104 written = write (desc, ptr, wcount);
1111 pfatal_with_name (out_fname);
1118 /* Print a string to stderr, with extra handling in case it contains
1119 embedded NUL characters. Any present are written as is.
1121 Using fwrite for this purpose produces undesireable results on VMS
1122 when stderr happens to be a record oriented file, such as a batch log
1123 file, rather than a stream oriented one. */
1126 eprint_string (string, length)
1130 size_t segment_length;
1133 fprintf(stderr, "%s", string);
1134 length -= (segment_length = strlen(string));
1137 fputc('\0', stderr);
1139 /* Advance past the portion which has already been printed. */
1140 string += segment_length + 1;
1142 } while (length > 0);
1149 printf ("Usage: %s [switches] input output\n", progname);
1150 printf ("Switches:\n");
1151 printf (" -include <file> Include the contents of <file> before other files\n");
1152 printf (" -imacros <file> Accept definition of marcos in <file>\n");
1153 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1154 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1155 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1156 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1157 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1158 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1159 printf (" -nostdinc Do not search the system include directories\n");
1160 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1161 printf (" -o <file> Put output into <file>\n");
1162 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1163 printf (" -traditional Follow K&R pre-processor behaviour\n");
1164 printf (" -trigraphs Support ANSI C trigraphs\n");
1165 printf (" -lang-c Assume that the input sources are in C\n");
1166 printf (" -lang-c89 Assume that the input is C89; depricated\n");
1167 printf (" -lang-c++ Assume that the input sources are in C++\n");
1168 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1169 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1170 printf (" -lang-asm Assume that the input sources are in assembler\n");
1171 printf (" -lang-chill Assume that the input sources are in Chill\n");
1172 printf (" -std=<std name> Specify the conformance standard; one of:\n");
1173 printf (" gnu89, gnu9x, c89, c9x, iso9899:1990,\n");
1174 printf (" iso9899:199409, iso9899:199x\n");
1175 printf (" -+ Allow parsing of C++ style features\n");
1176 printf (" -w Inhibit warning messages\n");
1177 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1178 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1179 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1180 printf (" -Wno-comment{s} Do not warn about comments\n");
1181 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1182 printf (" a string if -tradtional is specified\n");
1183 printf (" -Wno-traditional Do not warn about stringification\n");
1184 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1185 printf (" -Wno-undef Do not warn about testing udefined macros\n");
1186 printf (" -Wimport Warn about the use of the #import directive\n");
1187 printf (" -Wno-import Do not warn about the use of #import\n");
1188 printf (" -Werror Treat all warnings as errors\n");
1189 printf (" -Wno-error Do not treat warnings as errors\n");
1190 printf (" -Wall Enable all preprocessor warnings\n");
1191 printf (" -M Generate make dependencies\n");
1192 printf (" -MM As -M, but ignore system header files\n");
1193 printf (" -MD As -M, but put output in a .d file\n");
1194 printf (" -MMD As -MD, but ignore system header files\n");
1195 printf (" -MG Treat missing header file as generated files\n");
1196 printf (" -g Include #define and #undef directives in the output\n");
1197 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1198 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1199 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1200 printf (" -U<macro> Undefine <macro> \n");
1201 printf (" -u or -undef Do not predefine any macros\n");
1202 printf (" -v Display the version number\n");
1203 printf (" -H Print the name of header files as they are used\n");
1204 printf (" -C Do not discard comments\n");
1205 printf (" -dM Display a list of macro definitions active at end\n");
1206 printf (" -dD Preserve macro definitions in output\n");
1207 printf (" -dN As -dD except that only the names are preserved\n");
1208 printf (" -dI Include #include directives in the output\n");
1209 printf (" -ifoutput Describe skipped code blocks in output \n");
1210 printf (" -P Do not generate #line directives\n");
1211 printf (" -$ Do not include '$' in identifiers\n");
1212 printf (" -remap Remap file names when including files.\n");
1213 printf (" -h or --help Display this information\n");
1226 char **pend_files = (char **) xmalloc (argc * sizeof (char *));
1227 char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
1228 char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1229 char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1230 char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
1232 /* Record the option used with each element of pend_assertions.
1233 This is preparation for supporting more than one option for making
1235 char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
1236 int inhibit_predefs = 0;
1237 int no_standard_includes = 0;
1238 int no_standard_cplusplus_includes = 0;
1239 int missing_newline = 0;
1241 /* Non-0 means don't output the preprocessed program. */
1242 int inhibit_output = 0;
1243 /* Non-0 means -v, so print the full set of include dirs. */
1246 /* File name which deps are being written to.
1247 This is 0 if deps are being written to stdout. */
1248 char *deps_file = 0;
1249 /* Fopen file mode to open deps_file with. */
1250 char *deps_mode = "a";
1251 /* Stream on which to print the dependency information. */
1252 FILE *deps_stream = 0;
1253 /* Target-name to write with the dependency information. */
1254 char *deps_target = 0;
1256 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1257 /* Get rid of any avoidable limit on stack size. */
1261 /* Set the stack limit huge so that alloca (particularly stringtab
1262 in dbxread.c) does not fail. */
1263 getrlimit (RLIMIT_STACK, &rlim);
1264 rlim.rlim_cur = rlim.rlim_max;
1265 setrlimit (RLIMIT_STACK, &rlim);
1270 signal (SIGPIPE, pipe_closed);
1273 progname = base_name (argv[0]);
1277 /* Remove extension from PROGNAME. */
1279 char *s = progname = savestring (progname);
1281 if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */
1282 if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */
1283 && (p[1] == 'e' || p[1] == 'E')
1284 && (p[2] == 'x' || p[2] == 'X')
1285 && (p[3] == 'e' || p[3] == 'E')
1294 /* Initialize is_idchar. */
1295 initialize_char_syntax ();
1297 no_line_directives = 0;
1299 dump_macros = dump_none;
1302 cplusplus_comments = 1;
1304 bzero ((char *) pend_files, argc * sizeof (char *));
1305 bzero ((char *) pend_defs, argc * sizeof (char *));
1306 bzero ((char *) pend_undefs, argc * sizeof (char *));
1307 bzero ((char *) pend_assertions, argc * sizeof (char *));
1308 bzero ((char *) pend_includes, argc * sizeof (char *));
1310 #ifdef MULTIBYTE_CHARS
1311 /* Change to the native locale for multibyte conversions. */
1312 setlocale (LC_CTYPE, "");
1313 literal_codeset = getenv ("LANG");
1316 /* Process switches and find input file name. */
1318 for (i = 1; i < argc; i++) {
1319 if (argv[i][0] != '-') {
1320 if (out_fname != NULL)
1323 fatal ("Too many arguments");
1325 else if (in_fname != NULL)
1326 out_fname = argv[i];
1330 switch (argv[i][1]) {
1333 if (!strcmp (argv[i], "-include")) {
1337 fatal ("Filename missing after `-include' option");
1339 simplify_filename (pend_includes[temp] = argv[++i]);
1341 if (!strcmp (argv[i], "-imacros")) {
1345 fatal ("Filename missing after `-imacros' option");
1347 simplify_filename (pend_files[temp] = argv[++i]);
1349 if (!strcmp (argv[i], "-iprefix")) {
1351 fatal ("Filename missing after `-iprefix' option");
1353 include_prefix = argv[++i];
1355 if (!strcmp (argv[i], "-ifoutput")) {
1356 output_conditionals = 1;
1358 if (!strcmp (argv[i], "-isystem")) {
1359 struct file_name_list *dirtmp;
1361 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1364 dirtmp->c_system_include_path = 1;
1366 if (before_system == 0)
1367 before_system = dirtmp;
1369 last_before_system->next = dirtmp;
1370 last_before_system = dirtmp; /* Tail follows the last one */
1372 /* Add directory to end of path for includes,
1373 with the default prefix at the front of its name. */
1374 if (!strcmp (argv[i], "-iwithprefix")) {
1375 struct file_name_list *dirtmp;
1378 if (include_prefix != 0)
1379 prefix = include_prefix;
1381 prefix = savestring (GCC_INCLUDE_DIR);
1382 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1383 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1384 prefix[strlen (prefix) - 7] = 0;
1387 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1388 prefix, argv[++i])))
1391 if (after_include == 0)
1392 after_include = dirtmp;
1394 last_after_include->next = dirtmp;
1395 last_after_include = dirtmp; /* Tail follows the last one */
1397 /* Add directory to main path for includes,
1398 with the default prefix at the front of its name. */
1399 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1400 struct file_name_list *dirtmp;
1403 if (include_prefix != 0)
1404 prefix = include_prefix;
1406 prefix = savestring (GCC_INCLUDE_DIR);
1407 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1408 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1409 prefix[strlen (prefix) - 7] = 0;
1412 dirtmp = new_include_prefix (NULL_PTR, NULL_PTR, prefix, argv[++i]);
1413 append_include_chain (dirtmp, dirtmp);
1415 /* Add directory to end of path for includes. */
1416 if (!strcmp (argv[i], "-idirafter")) {
1417 struct file_name_list *dirtmp;
1419 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1423 if (after_include == 0)
1424 after_include = dirtmp;
1426 last_after_include->next = dirtmp;
1427 last_after_include = dirtmp; /* Tail follows the last one */
1432 if (out_fname != NULL)
1433 fatal ("Output filename specified twice");
1435 fatal ("Filename missing after -o option");
1436 out_fname = argv[++i];
1437 if (!strcmp (out_fname, "-"))
1442 if (!strcmp (argv[i], "-pedantic"))
1444 else if (!strcmp (argv[i], "-pedantic-errors")) {
1446 pedantic_errors = 1;
1447 } else if (!strcmp (argv[i], "-pcp")) {
1450 fatal ("Filename missing after -pcp option");
1451 pcp_fname = argv[++i];
1453 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1454 ? fopen (pcp_fname, "w")
1456 if (pcp_outfile == 0)
1457 pfatal_with_name (pcp_fname);
1463 if (!strcmp (argv[i], "-traditional")) {
1465 cplusplus_comments = 0;
1466 } else if (!strcmp (argv[i], "-trigraphs")) {
1472 if (! strcmp (argv[i], "-lang-c"))
1473 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c9x = 1, objc = 0;
1474 else if (! strcmp (argv[i], "-lang-c89"))
1475 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c9x = 0, objc = 0;
1476 else if (! strcmp (argv[i], "-lang-c++"))
1477 cplusplus = 1, cplusplus_comments = 1, c89 = 0, c9x = 0, objc = 0;
1478 else if (! strcmp (argv[i], "-lang-objc"))
1479 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c9x = 0, objc = 1;
1480 else if (! strcmp (argv[i], "-lang-objc++"))
1481 cplusplus = 1, cplusplus_comments = 1, c89 = 0, c9x = 0, objc = 1;
1482 else if (! strcmp (argv[i], "-lang-asm"))
1484 else if (! strcmp (argv[i], "-lint"))
1489 cplusplus = 1, cplusplus_comments = 1;
1493 if (!strcmp (argv[i], "-std=iso9899:1990")
1494 || !strcmp (argv[i], "-std=iso9899:199409")
1495 || !strcmp (argv[i], "-std=c89")
1496 || !strcmp (argv[i], "-std=gnu89"))
1497 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c9x = 0, objc = 0;
1498 else if (!strcmp (argv[i], "-std=iso9899:199x")
1499 || !strcmp (argv[i], "-std=c9x")
1500 || !strcmp (argv[i], "-std=gnu9x"))
1501 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c9x = 1, objc = 0;
1505 inhibit_warnings = 1;
1509 if (!strcmp (argv[i], "-Wtrigraphs"))
1511 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1513 else if (!strcmp (argv[i], "-Wcomment"))
1515 else if (!strcmp (argv[i], "-Wno-comment"))
1517 else if (!strcmp (argv[i], "-Wcomments"))
1519 else if (!strcmp (argv[i], "-Wno-comments"))
1521 else if (!strcmp (argv[i], "-Wtraditional"))
1523 else if (!strcmp (argv[i], "-Wno-traditional"))
1525 else if (!strcmp (argv[i], "-Wundef"))
1527 else if (!strcmp (argv[i], "-Wno-undef"))
1529 else if (!strcmp (argv[i], "-Wimport"))
1531 else if (!strcmp (argv[i], "-Wno-import"))
1533 else if (!strcmp (argv[i], "-Werror"))
1534 warnings_are_errors = 1;
1535 else if (!strcmp (argv[i], "-Wno-error"))
1536 warnings_are_errors = 0;
1537 else if (!strcmp (argv[i], "-Wall"))
1545 if (!strcmp (argv[i], "-fleading-underscore"))
1546 user_label_prefix = "_";
1547 else if (!strcmp (argv[i], "-fno-leading-underscore"))
1548 user_label_prefix = "";
1552 /* The style of the choices here is a bit mixed.
1553 The chosen scheme is a hybrid of keeping all options in one string
1554 and specifying each option in a separate argument:
1555 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1556 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1557 -M[M][G][D file]. This is awkward to handle in specs, and is not
1559 /* ??? -MG must be specified in addition to one of -M or -MM.
1560 This can be relaxed in the future without breaking anything.
1561 The converse isn't true. */
1563 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1564 if (!strcmp (argv[i], "-MG"))
1566 print_deps_missing_files = 1;
1569 if (!strcmp (argv[i], "-M"))
1571 else if (!strcmp (argv[i], "-MM"))
1573 else if (!strcmp (argv[i], "-MD"))
1575 else if (!strcmp (argv[i], "-MMD"))
1577 /* For -MD and -MMD options, write deps on file named by next arg. */
1578 if (!strcmp (argv[i], "-MD")
1579 || !strcmp (argv[i], "-MMD")) {
1581 fatal ("Filename missing after %s option", argv[i]);
1583 deps_file = argv[i];
1586 /* For -M and -MM, write deps on standard output
1587 and suppress the usual output. */
1588 deps_stream = stdout;
1595 char *p = argv[i] + 2;
1597 while ((c = *p++)) {
1598 /* Arg to -d specifies what parts of macros to dump */
1601 dump_macros = dump_only;
1605 dump_macros = dump_names;
1608 dump_macros = dump_definitions;
1619 if (argv[i][2] == '3')
1624 if (strcmp (argv[i], "--help") != 0)
1631 fprintf (stderr, "GNU CPP version %s", version_string);
1632 #ifdef TARGET_VERSION
1635 fprintf (stderr, "\n");
1640 print_include_names = 1;
1644 if (argv[i][2] != 0)
1645 pend_defs[i] = argv[i] + 2;
1646 else if (i + 1 == argc)
1647 fatal ("Macro name missing after -D option");
1649 i++, pend_defs[i] = argv[i];
1656 if (argv[i][2] != 0)
1658 else if (i + 1 == argc)
1659 fatal ("Assertion missing after -A option");
1663 if (!strcmp (p, "-")) {
1664 /* -A- eliminates all predefined macros and assertions.
1665 Let's include also any that were specified earlier
1666 on the command line. That way we can get rid of any
1667 that were passed automatically in from GCC. */
1669 inhibit_predefs = 1;
1670 for (j = 0; j < i; j++)
1671 pend_defs[j] = pend_assertions[j] = 0;
1673 pend_assertions[i] = p;
1674 pend_assertion_options[i] = "-A";
1679 case 'U': /* JF #undef something */
1680 if (argv[i][2] != 0)
1681 pend_undefs[i] = argv[i] + 2;
1682 else if (i + 1 == argc)
1683 fatal ("Macro name missing after -U option");
1685 pend_undefs[i] = argv[i+1], i++;
1689 put_out_comments = 1;
1692 case 'E': /* -E comes from cc -E; ignore it. */
1696 no_line_directives = 1;
1699 case '$': /* Don't include $ in identifiers. */
1700 is_idchar['$'] = is_idstart['$'] = 0;
1703 case 'I': /* Add directory to path for includes. */
1705 struct file_name_list *dirtmp;
1707 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
1709 /* Don't use any preceding -I directories for #include <...>. */
1710 first_bracket_include = 0;
1713 dirtmp = new_include_prefix (last_include, NULL_PTR, "",
1714 argv[i][2] ? argv[i] + 2 : argv[++i]);
1715 append_include_chain (dirtmp, dirtmp);
1721 if (!strcmp (argv[i], "-nostdinc"))
1722 /* -nostdinc causes no default include directories.
1723 You must specify all include-file directories with -I. */
1724 no_standard_includes = 1;
1725 else if (!strcmp (argv[i], "-nostdinc++"))
1726 /* -nostdinc++ causes no default C++-specific include directories. */
1727 no_standard_cplusplus_includes = 1;
1728 else if (!strcmp (argv[i], "-noprecomp"))
1733 if (!strcmp (argv[i], "-remap"))
1738 /* Sun compiler passes undocumented switch "-undef".
1739 Let's assume it means to inhibit the predefined symbols. */
1740 inhibit_predefs = 1;
1743 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1744 if (in_fname == NULL) {
1747 } else if (out_fname == NULL) {
1750 } /* else fall through into error */
1753 fatal ("Invalid option `%s'", argv[i]);
1758 /* Add dirs from CPATH after dirs from -I. */
1759 /* There seems to be confusion about what CPATH should do,
1760 so for the moment it is not documented. */
1761 /* Some people say that CPATH should replace the standard include dirs,
1762 but that seems pointless: it comes before them, so it overrides them
1764 GET_ENV_PATH_LIST (cp, "CPATH");
1765 if (cp && ! no_standard_includes)
1768 /* Initialize output buffer */
1770 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1771 outbuf.bufp = outbuf.buf;
1772 outbuf.length = OUTBUF_SIZE;
1774 /* Do partial setup of input buffer for the sake of generating
1775 early #line directives (when -g is in effect). */
1777 fp = &instack[++indepth];
1778 if (in_fname == NULL)
1780 fp->nominal_fname = fp->fname = in_fname;
1781 fp->nominal_fname_len = strlen (in_fname);
1784 /* In C++, wchar_t is a distinct basic type, and we can expect
1785 __wchar_t to be defined by cc1plus. */
1787 wchar_type = "__wchar_t";
1789 /* Install __LINE__, etc. Must follow initialize_char_syntax
1790 and option processing. */
1791 initialize_builtins (fp, &outbuf);
1793 /* Do standard #defines and assertions
1794 that identify system and machine type. */
1796 if (!inhibit_predefs) {
1797 char *p = (char *) alloca (strlen (predefs) + 1);
1800 struct dsc$descriptor_s lcl_name;
1802 unsigned short length; /* input length */
1803 unsigned short code; /* item code */
1804 unsigned long dptr; /* data ptr */
1805 unsigned long lptr; /* output length ptr */
1808 unsigned long syi_length;
1811 struct item_list items[] = {
1812 { 16, SYI$_VERSION, 0, 0 },
1816 items[0].dptr = (unsigned long)syi_data;
1817 items[0].lptr = (unsigned long)(&syi_length);
1819 if (SYS$GETSYIW (0, 0, 0, items, NULL, NULL, NULL, NULL) == SS$_NORMAL)
1821 unsigned long vms_version_value;
1825 vms_version_value = 0;
1829 if (ISDIGIT (*vers))
1831 vms_version_value = (*vers - '0') * 10000000;
1837 if (ISDIGIT (*vers))
1839 vms_version_value += (*vers - '0') * 100000;
1843 if (vms_version_value > 0)
1847 sprintf (versbuf, "__VMS_VER=%08ld", vms_version_value);
1849 output_line_directive (fp, &outbuf, 0, same_file);
1850 make_definition (versbuf);
1855 strcpy (p, predefs);
1858 while (*p == ' ' || *p == '\t')
1860 /* Handle -D options. */
1861 if (p[0] == '-' && p[1] == 'D') {
1863 while (*p && *p != ' ' && *p != '\t')
1868 output_line_directive (fp, &outbuf, 0, same_file);
1869 make_definition (q);
1870 while (*p == ' ' || *p == '\t')
1872 } else if (p[0] == '-' && p[1] == 'A') {
1873 /* Handle -A options (assertions). */
1882 past_name = assertion;
1883 /* Locate end of name. */
1884 while (*past_name && *past_name != ' '
1885 && *past_name != '\t' && *past_name != '(')
1887 /* Locate `(' at start of value. */
1889 while (*value && (*value == ' ' || *value == '\t'))
1891 if (*value++ != '(')
1893 while (*value && (*value == ' ' || *value == '\t'))
1896 /* Locate end of value. */
1897 while (*past_value && *past_value != ' '
1898 && *past_value != '\t' && *past_value != ')')
1900 termination = past_value;
1901 while (*termination && (*termination == ' ' || *termination == '\t'))
1903 if (*termination++ != ')')
1905 if (*termination && *termination != ' ' && *termination != '\t')
1907 /* Temporarily null-terminate the value. */
1908 save_char = *termination;
1909 *termination = '\0';
1910 /* Install the assertion. */
1911 make_assertion ("-A", assertion);
1912 *termination = (char) save_char;
1914 while (*p == ' ' || *p == '\t')
1922 /* Now handle the command line options. */
1924 /* Do -U's, -D's and -A's in the order they were seen. */
1925 for (i = 1; i < argc; i++) {
1926 if (pend_undefs[i]) {
1928 output_line_directive (fp, &outbuf, 0, same_file);
1929 make_undef (pend_undefs[i], &outbuf);
1933 output_line_directive (fp, &outbuf, 0, same_file);
1934 make_definition (pend_defs[i]);
1936 if (pend_assertions[i])
1937 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1940 done_initializing = 1;
1942 { /* Read the appropriate environment variable and if it exists
1943 replace include_defaults with the listed path. */
1945 switch ((objc << 1) + cplusplus)
1948 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
1951 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
1954 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
1957 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
1960 /* If the environment var for this language is set,
1961 add to the default list of include directories. */
1964 char *startp, *endp;
1966 for (num_dirs = 1, startp = epath; *startp; startp++)
1967 if (*startp == PATH_SEPARATOR)
1970 = (struct default_include *) xmalloc ((num_dirs
1971 * sizeof (struct default_include))
1972 + sizeof (include_defaults_array));
1973 startp = endp = epath;
1977 if (c == PATH_SEPARATOR || !c) {
1979 include_defaults[num_dirs].fname
1980 = startp == endp ? "." : savestring (startp);
1982 include_defaults[num_dirs].component = 0;
1983 include_defaults[num_dirs].cplusplus = cplusplus;
1984 include_defaults[num_dirs].cxx_aware = 1;
1991 /* Put the usual defaults back in at the end. */
1992 bcopy ((char *) include_defaults_array,
1993 (char *) &include_defaults[num_dirs],
1994 sizeof (include_defaults_array));
1998 append_include_chain (before_system, last_before_system);
1999 first_system_include = before_system;
2001 /* Unless -fnostdinc,
2002 tack on the standard include file dirs to the specified list */
2003 if (!no_standard_includes) {
2004 struct default_include *p = include_defaults;
2005 char *specd_prefix = include_prefix;
2006 char *default_prefix = savestring (GCC_INCLUDE_DIR);
2007 int default_len = 0;
2008 /* Remove the `include' from /usr/local/lib/gcc.../include. */
2009 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
2010 default_len = strlen (default_prefix) - 7;
2011 default_prefix[default_len] = 0;
2013 /* Search "translated" versions of GNU directories.
2014 These have /usr/local/lib/gcc... replaced by specd_prefix. */
2015 if (specd_prefix != 0 && default_len != 0)
2016 for (p = include_defaults; p->fname; p++) {
2017 /* Some standard dirs are only for C++. */
2018 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
2019 /* Does this dir start with the prefix? */
2020 if (!strncmp (p->fname, default_prefix, default_len)) {
2021 /* Yes; change prefix and add to search list. */
2022 struct file_name_list *new
2023 = new_include_prefix (NULL_PTR, NULL_PTR, specd_prefix,
2024 p->fname + default_len);
2026 new->c_system_include_path = !p->cxx_aware;
2027 append_include_chain (new, new);
2028 if (first_system_include == 0)
2029 first_system_include = new;
2034 /* Search ordinary names for GNU include directories. */
2035 for (p = include_defaults; p->fname; p++) {
2036 /* Some standard dirs are only for C++. */
2037 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
2038 struct file_name_list *new
2039 = new_include_prefix (NULL_PTR, p->component, "", p->fname);
2041 new->c_system_include_path = !p->cxx_aware;
2042 append_include_chain (new, new);
2043 if (first_system_include == 0)
2044 first_system_include = new;
2050 /* Tack the after_include chain at the end of the include chain. */
2051 append_include_chain (after_include, last_after_include);
2052 if (first_system_include == 0)
2053 first_system_include = after_include;
2055 /* With -v, print the list of dirs to search. */
2057 struct file_name_list *p;
2058 fprintf (stderr, "#include \"...\" search starts here:\n");
2059 for (p = include; p; p = p->next) {
2060 if (p == first_bracket_include)
2061 fprintf (stderr, "#include <...> search starts here:\n");
2063 fprintf (stderr, " .\n");
2064 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
2065 fprintf (stderr, " %s\n", p->fname);
2067 /* Omit trailing '/'. */
2068 fprintf (stderr, " %.*s\n", (int) strlen (p->fname) - 1, p->fname);
2070 fprintf (stderr, "End of search list.\n");
2073 /* -MG doesn't select the form of output and must be specified with one of
2074 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
2075 inhibit compilation. */
2076 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
2077 fatal ("-MG must be specified with one of -M or -MM");
2079 /* Either of two environment variables can specify output of deps.
2080 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
2081 where OUTPUT_FILE is the file to write deps info to
2082 and DEPS_TARGET is the target to mention in the deps. */
2085 && (getenv ("SUNPRO_DEPENDENCIES") != 0
2086 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
2087 char *spec = getenv ("DEPENDENCIES_OUTPUT");
2092 spec = getenv ("SUNPRO_DEPENDENCIES");
2099 /* Find the space before the DEPS_TARGET, if there is one. */
2100 /* This should use index. (mrs) */
2101 while (*s != 0 && *s != ' ') s++;
2103 deps_target = s + 1;
2104 output_file = xmalloc (s - spec + 1);
2105 bcopy (spec, output_file, s - spec);
2106 output_file[s - spec] = 0;
2113 deps_file = output_file;
2117 /* For -M, print the expected object file name
2118 as the target of this Make-rule. */
2120 deps_allocated_size = 200;
2121 deps_buffer = xmalloc (deps_allocated_size);
2127 deps_output (deps_target, ':');
2128 } else if (*in_fname == 0) {
2129 deps_output ("-", ':');
2134 q = base_name (in_fname);
2136 /* Copy remainder to mungable area. */
2137 p = (char *) alloca (strlen(q) + 8);
2140 /* Output P, but remove known suffixes. */
2144 && p[len - 2] == '.'
2145 && index("cCsSm", p[len - 1]))
2148 && p[len - 3] == '.'
2149 && p[len - 2] == 'c'
2150 && p[len - 1] == 'c')
2153 && p[len - 4] == '.'
2154 && p[len - 3] == 'c'
2155 && p[len - 2] == 'x'
2156 && p[len - 1] == 'x')
2159 && p[len - 4] == '.'
2160 && p[len - 3] == 'c'
2161 && p[len - 2] == 'p'
2162 && p[len - 1] == 'p')
2165 /* Supply our own suffix. */
2166 strcpy (q, OBJECT_SUFFIX);
2168 deps_output (p, ':');
2169 deps_output (in_fname, ' ');
2173 /* Scan the -imacros files before the main input.
2174 Much like #including them, but with no_output set
2175 so that only their macro definitions matter. */
2177 no_output++; no_record_file++;
2178 for (i = 1; i < argc; i++)
2179 if (pend_files[i]) {
2180 struct include_file *inc;
2181 int fd = open_include_file (pend_files[i], NULL_PTR, NULL_PTR, &inc);
2183 perror_with_name (pend_files[i]);
2184 return FATAL_EXIT_CODE;
2186 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2188 no_output--; no_record_file--;
2190 /* Copy the entire contents of the main input file into
2191 the stacked input buffer previously allocated for it. */
2193 /* JF check for stdin */
2194 if (in_fname == NULL || *in_fname == 0) {
2197 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2200 if (fstat (f, &st) != 0)
2201 pfatal_with_name (in_fname);
2202 fp->nominal_fname = fp->fname = in_fname;
2203 fp->nominal_fname_len = strlen (in_fname);
2205 fp->system_header_p = 0;
2206 /* JF all this is mine about reading pipes and ttys */
2207 if (! S_ISREG (st.st_mode)) {
2208 /* Read input from a file that is not a normal disk file.
2209 We cannot preallocate a buffer with the correct size,
2210 so we must read in the file a piece at the time and make it bigger. */
2215 if (S_ISDIR (st.st_mode))
2216 fatal ("Input file `%s' is a directory", in_fname);
2220 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2222 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2223 if (cnt < 0) goto perror; /* error! */
2225 if (size != bsize) break; /* End of file */
2227 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2231 /* Read a file whose size we can determine in advance.
2232 For the sake of VMS, st.st_size is just an upper bound. */
2233 size_t s = (size_t) st.st_size;
2234 if (s != st.st_size || s + 2 < s)
2236 fp->buf = (U_CHAR *) xmalloc (s + 2);
2237 fp->length = safe_read (f, (char *) fp->buf, s);
2238 if (fp->length < 0) goto perror;
2241 fp->if_stack = if_stack;
2243 /* Make sure data ends with a newline. And put a null after it. */
2245 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2246 /* Backslash-newline at end is not good enough. */
2247 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2248 fp->buf[fp->length++] = '\n';
2249 missing_newline = 1;
2251 fp->buf[fp->length] = '\0';
2253 /* Unless inhibited, convert trigraphs in the input. */
2258 /* Now that we know the input file is valid, open the output. */
2260 if (!out_fname || !strcmp (out_fname, ""))
2261 out_fname = "stdout";
2262 else if (! freopen (out_fname, "w", stdout))
2263 pfatal_with_name (out_fname);
2265 output_line_directive (fp, &outbuf, 0, same_file);
2267 /* Scan the -include files before the main input. */
2270 for (i = 1; i < argc; i++)
2271 if (pend_includes[i]) {
2272 struct include_file *inc;
2273 int fd = open_include_file (pend_includes[i], NULL_PTR, NULL_PTR, &inc);
2275 perror_with_name (pend_includes[i]);
2276 return FATAL_EXIT_CODE;
2278 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2282 /* Scan the input, processing macros and directives. */
2284 rescan (&outbuf, 0);
2286 if (missing_newline)
2289 if (pedantic && missing_newline)
2290 pedwarn ("file does not end in newline");
2292 /* Now we have processed the entire input
2293 Write whichever kind of output has been requested. */
2295 if (dump_macros == dump_only)
2297 else if (! inhibit_output) {
2302 /* Don't actually write the deps file if compilation has failed. */
2304 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2305 pfatal_with_name (deps_file);
2306 fputs (deps_buffer, deps_stream);
2307 putc ('\n', deps_stream);
2309 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2310 fatal ("I/O error on output");
2315 if (pcp_outfile && pcp_outfile != stdout
2316 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2317 fatal ("I/O error on `-pcp' output");
2319 if (ferror (stdout) || fclose (stdout) != 0)
2320 fatal ("I/O error on output");
2323 exit (FATAL_EXIT_CODE);
2324 exit (SUCCESS_EXIT_CODE);
2327 pfatal_with_name (in_fname);
2331 /* Given a colon-separated list of file names PATH,
2332 add all the names to the search path for include files. */
2346 struct file_name_list *dirtmp;
2348 /* Find the end of this name. */
2349 while ((c = *q++) != PATH_SEPARATOR && c)
2353 dirtmp = new_include_prefix (last_include, NULL_PTR,
2354 "", p == q ? "." : p);
2356 append_include_chain (dirtmp, dirtmp);
2358 /* Advance past this name. */
2365 /* Return the address of the first character in S that equals C.
2366 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2367 Return 0 if there is no such character. Assume that C itself is not '\0'.
2368 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2369 but unfortunately memchr isn't autoconfigured yet. */
2377 char *p = (char *) s;
2379 char *q = index (p, c);
2381 return (U_CHAR *) q;
2383 size_t l = strlen (p);
2393 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2394 before main CCCP processing. Name `pcp' is also in honor of the
2395 drugs the trigraph designers must have been on.
2397 Using an extra pass through the buffer takes a little extra time,
2398 but is infinitely less hairy than trying to handle trigraphs inside
2399 strings, etc. everywhere, and also makes sure that trigraphs are
2400 only translated in the top level of processing. */
2406 register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
2409 fptr = bptr = sptr = buf->buf;
2410 lptr = fptr + buf->length;
2411 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2448 len = sptr - fptr - 2;
2450 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2451 C, this will be memmove (). */
2452 if (bptr != fptr && len > 0)
2453 bcopy ((char *) fptr, (char *) bptr, len);
2459 len = buf->length - (fptr - buf->buf);
2460 if (bptr != fptr && len > 0)
2461 bcopy ((char *) fptr, (char *) bptr, len);
2462 buf->length -= fptr - bptr;
2463 buf->buf[buf->length] = '\0';
2464 if (warn_trigraphs && fptr != bptr)
2465 warning_with_line (0, "%lu trigraph(s) encountered",
2466 (unsigned long) (fptr - bptr) / 2);
2469 /* Move all backslash-newline pairs out of embarrassing places.
2470 Exchange all such pairs following BP
2471 with any potentially-embarrassing characters that follow them.
2472 Potentially-embarrassing characters are / and *
2473 (because a backslash-newline inside a comment delimiter
2474 would cause it not to be recognized). */
2480 register U_CHAR *p = bp;
2482 /* First count the backslash-newline pairs here. */
2484 while (p[0] == '\\' && p[1] == '\n')
2487 /* What follows the backslash-newlines is not embarrassing. */
2489 if (*p != '/' && *p != '*')
2492 /* Copy all potentially embarrassing characters
2493 that follow the backslash-newline pairs
2494 down to where the pairs originally started. */
2496 while (*p == '*' || *p == '/')
2499 /* Now write the same number of pairs after the embarrassing chars. */
2506 /* Like newline_fix but for use within a directive-name.
2507 Move any backslash-newlines up past any following symbol constituents. */
2510 name_newline_fix (bp)
2513 register U_CHAR *p = bp;
2515 /* First count the backslash-newline pairs here. */
2516 while (p[0] == '\\' && p[1] == '\n')
2519 /* What follows the backslash-newlines is not embarrassing. */
2524 /* Copy all potentially embarrassing characters
2525 that follow the backslash-newline pairs
2526 down to where the pairs originally started. */
2528 while (is_idchar[*p])
2531 /* Now write the same number of pairs after the embarrassing chars. */
2538 /* Look for lint commands in comments.
2540 When we come in here, ibp points into a comment. Limit is as one expects.
2541 scan within the comment -- it should start, after lwsp, with a lint command.
2542 If so that command is returned as a (constant) string.
2544 Upon return, any arg will be pointed to with argstart and will be
2545 arglen long. Note that we don't parse that arg since it will just
2546 be printed out again. */
2549 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2550 register U_CHAR *ibp;
2551 register U_CHAR *limit;
2552 U_CHAR **argstart; /* point to command arg */
2553 int *arglen, *cmdlen; /* how long they are */
2555 HOST_WIDE_INT linsize;
2556 register U_CHAR *numptr; /* temp for arg parsing */
2560 SKIP_WHITE_SPACE (ibp);
2562 if (ibp >= limit) return NULL;
2564 linsize = limit - ibp;
2566 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2567 if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
2569 return "NOTREACHED";
2571 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2575 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2577 return "LINTLIBRARY";
2579 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2581 ibp += 7; linsize -= 7;
2582 if ((linsize == 0) || ! ISDIGIT (*ibp)) return "VARARGS";
2584 /* OK, read a number */
2585 for (numptr = *argstart = ibp; (numptr < limit) && ISDIGIT (*numptr);
2587 *arglen = numptr - *argstart;
2594 * The main loop of the program.
2596 * Read characters from the input stack, transferring them to the
2599 * Macros are expanded and push levels on the input stack.
2600 * At the end of such a level it is popped off and we keep reading.
2601 * At the end of any other kind of level, we return.
2602 * #-directives are handled, except within macros.
2604 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2605 * and insert them when appropriate. This is set while scanning macro
2606 * arguments before substitution. It is zero when scanning for final output.
2607 * There are three types of Newline markers:
2608 * * Newline - follows a macro name that was not expanded
2609 * because it appeared inside an expansion of the same macro.
2610 * This marker prevents future expansion of that identifier.
2611 * When the input is rescanned into the final output, these are deleted.
2612 * These are also deleted by ## concatenation.
2613 * * Newline Space (or Newline and any other whitespace character)
2614 * stands for a place that tokens must be separated or whitespace
2615 * is otherwise desirable, but where the ANSI standard specifies there
2616 * is no whitespace. This marker turns into a Space (or whichever other
2617 * whitespace char appears in the marker) in the final output,
2618 * but it turns into nothing in an argument that is stringified with #.
2619 * Such stringified arguments are the only place where the ANSI standard
2620 * specifies with precision that whitespace may not appear.
2622 * During this function, IP->bufp is kept cached in IBP for speed of access.
2623 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2624 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2625 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2626 * explicitly, and before RECACHE, since RECACHE uses OBP.
2630 rescan (op, output_marks)
2634 /* Character being scanned in main loop. */
2637 /* Length of pending accumulated identifier. */
2638 register int ident_length = 0;
2640 /* Hash code of pending accumulated identifier. */
2641 register int hash = 0;
2643 /* Current input level (&instack[indepth]). */
2646 /* Pointer for scanning input. */
2647 register U_CHAR *ibp;
2649 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2650 register U_CHAR *limit;
2652 /* Pointer for storing output. */
2653 register U_CHAR *obp;
2655 /* REDO_CHAR is nonzero if we are processing an identifier
2656 after backing up over the terminating character.
2657 Sometimes we process an identifier without backing up over
2658 the terminating character, if the terminating character
2659 is not special. Backing up is done so that the terminating character
2660 will be dispatched on again once the identifier is dealt with. */
2663 /* 1 if within an identifier inside of which a concatenation
2664 marker (Newline -) has been seen. */
2665 int concatenated = 0;
2667 /* While scanning a comment or a string constant,
2668 this records the line it started on, for error messages. */
2671 /* Record position of last `real' newline. */
2672 U_CHAR *beg_of_line;
2674 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2677 do { ip->macro->type = T_MACRO; \
2678 if (ip->free_ptr) free (ip->free_ptr); \
2679 --indepth; } while (0)
2681 /* Reload `rescan's local variables that describe the current
2682 level of the input stack. */
2685 do { ip = &instack[indepth]; \
2687 limit = ip->buf + ip->length; \
2689 check_expand (op, limit - ibp); \
2691 obp = op->bufp; } while (0)
2693 if (no_output && instack[indepth].fname != 0)
2694 skip_if_group (&instack[indepth], 1, NULL);
2701 /* Our caller must always put a null after the end of
2702 the input at each input stack level. */
2712 if (*ibp == '\n' && !ip->macro) {
2713 /* At the top level, always merge lines ending with backslash-newline,
2714 even in middle of identifier. But do not merge lines in a macro,
2715 since backslash might be followed by a newline-space marker. */
2718 --obp; /* remove backslash from obuf */
2721 /* If ANSI, backslash is just another character outside a string. */
2724 /* Otherwise, backslash suppresses specialness of following char,
2725 so copy it here to prevent the switch from seeing it.
2726 But first get any pending identifier processed. */
2727 if (ident_length > 0)
2734 if (ident_length || ip->macro || traditional)
2736 while (*ibp == '\\' && ibp[1] == '\n') {
2742 /* Treat this %: digraph as if it were #. */
2746 if (assertions_flag) {
2749 /* Copy #foo (bar lose) without macro expansion. */
2750 obp[-1] = '#'; /* In case it was '%'. */
2751 SKIP_WHITE_SPACE (ibp);
2752 while (is_idchar[*ibp])
2754 SKIP_WHITE_SPACE (ibp);
2757 skip_paren_group (ip);
2758 bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
2759 obp += ip->bufp - ibp;
2765 /* If this is expanding a macro definition, don't recognize
2766 preprocessing directives. */
2769 /* If this is expand_into_temp_buffer,
2770 don't recognize them either. Warn about them
2771 only after an actual newline at this level,
2772 not at the beginning of the input level. */
2774 if (ip->buf != beg_of_line)
2775 warning ("preprocessing directive not recognized within macro arg");
2782 /* # keyword: a # must be first nonblank char on the line */
2783 if (beg_of_line == 0)
2788 /* Scan from start of line, skipping whitespace, comments
2789 and backslash-newlines, and see if we reach this #.
2790 If not, this # is not special. */
2792 /* If -traditional, require # to be at beginning of line. */
2795 if (is_hor_space[*bp])
2797 else if (*bp == '\\' && bp[1] == '\n')
2799 else if (*bp == '/' && bp[1] == '*') {
2813 #ifdef MULTIBYTE_CHARS
2815 length = local_mblen (bp, limit - bp);
2823 /* There is no point in trying to deal with C++ // comments here,
2824 because if there is one, then this # must be part of the
2825 comment and we would never reach here. */
2831 while (bp[1] == '\\' && bp[2] == '\n')
2835 /* %: appears at start of line; skip past the ':' too. */
2844 /* This # can start a directive. */
2846 --obp; /* Don't copy the '#' */
2850 if (! handle_directive (ip, op)) {
2854 /* Not a known directive: treat it as ordinary text.
2855 IP, OP, IBP, etc. have not been changed. */
2856 if (no_output && instack[indepth].fname) {
2857 /* If not generating expanded output,
2858 what we do with ordinary text is skip it.
2859 Discard everything until next # directive. */
2860 skip_if_group (&instack[indepth], 1, 0);
2865 *obp++ = '#'; /* Copy # (even if it was originally %:). */
2866 /* Don't expand an identifier that could be a macro directive.
2867 (Section 3.8.3 of the ANSI C standard) */
2868 SKIP_WHITE_SPACE (ibp);
2869 if (is_idstart[*ibp])
2872 while (is_idchar[*ibp])
2880 /* A # directive has been successfully processed. */
2881 /* If not generating expanded output, ignore everything until
2882 next # directive. */
2883 if (no_output && instack[indepth].fname)
2884 skip_if_group (&instack[indepth], 1, 0);
2890 case '\"': /* skip quoted string */
2892 /* A single quoted string is treated like a double -- some
2893 programs (e.g., troff) are perverse this way */
2895 /* Handle any pending identifier;
2896 but the L in L'...' or L"..." is not an identifier. */
2898 if (! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2900 ident_length = hash = 0;
2903 start_line = ip->lineno;
2905 /* Skip ahead to a matching quote. */
2909 if (ip->macro != 0) {
2910 /* try harder: this string crosses a macro expansion boundary.
2911 This can happen naturally if -traditional.
2912 Otherwise, only -D can make a macro with an unmatched quote. */
2918 error_with_line (line_for_error (start_line),
2919 "unterminated string or character constant");
2920 if (multiline_string_line) {
2921 error_with_line (multiline_string_line,
2922 "possible real start of unterminated constant");
2923 multiline_string_line = 0;
2933 /* Traditionally, end of line ends a string constant with no error.
2934 So exit the loop and record the new line. */
2940 error_with_line (line_for_error (start_line),
2941 "unterminated character constant");
2944 if (multiline_string_line == 0) {
2946 pedwarn_with_line (line_for_error (start_line),
2947 "string constant runs past end of line");
2948 multiline_string_line = ip->lineno - 1;
2954 /* Backslash newline is replaced by nothing at all, but
2955 keep the line counts correct. But if we are reading
2956 from a macro, keep the backslash newline, since backslash
2957 newlines have already been processed. */
2965 /* ANSI stupidly requires that in \\ the second \
2966 is *not* prevented from combining with a newline. */
2968 while (*ibp == '\\' && ibp[1] == '\n') {
2982 #ifdef MULTIBYTE_CHARS
2987 length = local_mblen (ibp, limit - ibp);
2991 bcopy (ibp, obp, length);
3008 if (*ibp == '\\' && ibp[1] == '\n')
3011 && !(cplusplus_comments && *ibp == '/'))
3017 /* C++ style comment... */
3018 start_line = ip->lineno;
3020 /* Comments are equivalent to spaces. */
3021 if (! put_out_comments)
3025 U_CHAR *before_bp = ibp;
3027 while (++ibp < limit) {
3030 if (put_out_comments) {
3031 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
3032 obp += ibp - before_bp;
3038 if (ibp + 1 < limit && ibp[1] == '\n')
3041 warning ("multiline `//' comment");
3043 /* Copy the newline into the output buffer, in order to
3044 avoid the pain of a #line every time a multiline comment
3046 if (!put_out_comments)
3054 #ifdef MULTIBYTE_CHARS
3056 length = local_mblen (ibp, limit - ibp);
3058 ibp += (length - 1);
3066 /* Ordinary C comment. Skip it, optionally copying it to output. */
3068 start_line = ip->lineno;
3070 ++ibp; /* Skip the star. */
3072 /* If this cpp is for lint, we peek inside the comments: */
3076 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
3078 if (lintcmd != NULL) {
3080 check_expand (op, cmdlen + arglen + 14);
3082 /* I believe it is always safe to emit this newline: */
3084 bcopy ("#pragma lint ", (char *) obp, 13);
3086 bcopy (lintcmd, (char *) obp, cmdlen);
3091 bcopy (argbp, (char *) obp, arglen);
3095 /* OK, now bring us back to the state we were in before we entered
3096 this branch. We need #line because the #pragma's newline always
3097 messes up the line count. */
3099 output_line_directive (ip, op, 0, same_file);
3100 check_expand (op, limit - ibp + 2);
3106 /* Comments are equivalent to spaces.
3107 Note that we already output the slash; we might not want it.
3108 For -traditional, a comment is equivalent to nothing. */
3109 if (! put_out_comments) {
3119 U_CHAR *before_bp = ibp;
3124 if (ibp[-2] == '/' && warn_comments)
3125 warning ("`/*' within comment");
3126 if (*ibp == '\\' && ibp[1] == '\n')
3134 /* Copy the newline into the output buffer, in order to
3135 avoid the pain of a #line every time a multiline comment
3137 if (!put_out_comments)
3144 error_with_line (line_for_error (start_line),
3145 "unterminated comment");
3149 #ifdef MULTIBYTE_CHARS
3153 length = local_mblen (ibp, limit - ibp);
3155 ibp += (length - 1);
3164 if (put_out_comments) {
3165 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
3166 obp += ibp - before_bp;
3172 if (! is_idchar['$'])
3175 pedwarn ("`$' in identifier");
3178 case '0': case '1': case '2': case '3': case '4':
3179 case '5': case '6': case '7': case '8': case '9':
3180 /* If digit is not part of identifier, it starts a number,
3181 which means that following letters are not an identifier.
3182 "0x5" does not refer to an identifier "x5".
3183 So copy all alphanumerics that follow without accumulating
3184 as an identifier. Periods also, for sake of "3.e7". */
3186 if (ident_length == 0) {
3189 while (ibp[0] == '\\' && ibp[1] == '\n') {
3195 if (!is_idchar[c] && c != '.') {
3200 /* A sign can be part of a preprocessing number
3201 if it follows an `e' or `p'. */
3202 if (c == 'e' || c == 'E' || c == 'p' || c == 'P') {
3204 while (ibp[0] == '\\' && ibp[1] == '\n') {
3209 if (*ibp == '+' || *ibp == '-') {
3211 /* But traditional C does not let the token go past the sign,
3212 and C89 does not allow `p'. */
3213 if (traditional || (c89 && (c == 'p' || c == 'P')))
3223 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3224 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3225 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3226 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3228 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3229 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3230 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3231 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3235 /* Compute step of hash function, to avoid a proc call on every token */
3236 hash = HASHSTEP (hash, c);
3240 if (ip->fname == 0 && *ibp == '-') {
3241 /* Newline - inhibits expansion of preceding token.
3242 If expanding a macro arg, we keep the newline -.
3243 In final output, it is deleted.
3244 We recognize Newline - in macro bodies and macro args. */
3245 if (! concatenated) {
3250 if (!output_marks) {
3253 /* If expanding a macro arg, keep the newline -. */
3259 /* If reprocessing a macro expansion, newline is a special marker. */
3260 else if (ip->macro != 0) {
3261 /* Newline White is a "funny space" to separate tokens that are
3262 supposed to be separate but without space between.
3263 Here White means any whitespace character.
3264 Newline - marks a recursive macro use that is not
3265 supposed to be expandable. */
3267 if (is_space[*ibp]) {
3268 /* Newline Space does not prevent expansion of preceding token
3269 so expand the preceding token and then come back. */
3270 if (ident_length > 0)
3273 /* If generating final output, newline space makes a space. */
3274 if (!output_marks) {
3276 /* And Newline Newline makes a newline, so count it. */
3277 if (obp[-1] == '\n')
3280 /* If expanding a macro arg, keep the newline space.
3281 If the arg gets stringified, newline space makes nothing. */
3284 } else abort (); /* Newline followed by something random? */
3288 /* If there is a pending identifier, handle it and come back here. */
3289 if (ident_length > 0)
3294 /* Update the line counts and output a #line if necessary. */
3297 if (ip->lineno != op->lineno) {
3299 output_line_directive (ip, op, 1, same_file);
3300 check_expand (op, limit - ibp);
3305 /* Come here either after (1) a null character that is part of the input
3306 or (2) at the end of the input, because there is a null there. */
3309 /* Our input really contains a null character. */
3313 /* At end of a macro-expansion level, pop it and read next level. */
3314 if (ip->macro != 0) {
3317 /* If traditional, and we have an identifier that ends here,
3318 process it now, so we get the right error for recursion. */
3319 if (traditional && ident_length
3320 && ! is_idchar[*instack[indepth - 1].bufp]) {
3329 /* If we don't have a pending identifier,
3330 return at end of input. */
3331 if (ident_length == 0) {
3339 /* If we do have a pending identifier, just consider this null
3340 a special character and arrange to dispatch on it again.
3341 The second time, IDENT_LENGTH will be zero so we will return. */
3347 /* Handle the case of a character such as /, ', " or null
3348 seen following an identifier. Back over it so that
3349 after the identifier is processed the special char
3350 will be dispatched on again. */
3360 if (ident_length > 0) {
3361 register HASHNODE *hp;
3363 /* We have just seen an identifier end. If it's a macro, expand it.
3365 IDENT_LENGTH is the length of the identifier
3366 and HASH is its hash code.
3368 The identifier has already been copied to the output,
3369 so if it is a macro we must remove it.
3371 If REDO_CHAR is 0, the char that terminated the identifier
3372 has been skipped in the output and the input.
3373 OBP-IDENT_LENGTH-1 points to the identifier.
3374 If the identifier is a macro, we must back over the terminator.
3376 If REDO_CHAR is 1, the terminating char has already been
3377 backed over. OBP-IDENT_LENGTH points to the identifier. */
3379 if (!pcp_outfile || pcp_inside_if) {
3380 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3383 if (hp->length == ident_length) {
3384 int obufp_before_macroname;
3385 int op_lineno_before_macroname;
3386 register int i = ident_length;
3387 register U_CHAR *p = hp->name;
3388 register U_CHAR *q = obp - i;
3394 do { /* All this to avoid a strncmp () */
3399 /* We found a use of a macro name.
3400 see if the context shows it is a macro call. */
3402 /* Back up over terminating character if not already done. */
3408 /* Save this as a displacement from the beginning of the output
3409 buffer. We can not save this as a position in the output
3410 buffer, because it may get realloc'ed by RECACHE. */
3411 obufp_before_macroname = (obp - op->buf) - ident_length;
3412 op_lineno_before_macroname = op->lineno;
3414 if (hp->type == T_PCSTRING) {
3415 pcstring_used (hp); /* Mark the definition of this key
3416 as needed, ensuring that it
3418 break; /* Exit loop, since the key cannot have a
3419 definition any longer. */
3422 /* Record whether the macro is disabled. */
3423 disabled = hp->type == T_DISABLED;
3425 /* This looks like a macro ref, but if the macro was disabled,
3426 just copy its name and put in a marker if requested. */
3430 /* This error check caught useful cases such as
3431 #define foo(x,y) bar (x (y,0), y)
3434 error ("recursive use of macro `%s'", hp->name);
3438 check_expand (op, limit - ibp + 2);
3445 /* If macro wants an arglist, verify that a '(' follows.
3446 first skip all whitespace, copying it to the output
3447 after the macro name. Then, if there is no '(',
3448 decide this is not a macro call and leave things that way. */
3449 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3450 && hp->value.defn->nargs >= 0)
3452 U_CHAR *old_ibp = ibp;
3453 U_CHAR *old_obp = obp;
3454 int old_iln = ip->lineno;
3455 int old_oln = op->lineno;
3458 /* Scan forward over whitespace, copying it to the output. */
3459 if (ibp == limit && ip->macro != 0) {
3464 old_iln = ip->lineno;
3465 old_oln = op->lineno;
3467 else if (is_space[*ibp]) {
3469 if (ibp[-1] == '\n') {
3470 if (ip->macro == 0) {
3471 /* Newline in a file. Count it. */
3474 } else if (!output_marks) {
3475 /* A newline mark, and we don't want marks
3476 in the output. If it is newline-hyphen,
3477 discard it entirely. Otherwise, it is
3478 newline-whitechar, so keep the whitechar. */
3488 /* A newline mark; copy both chars to the output. */
3495 else if (*ibp == '/') {
3496 /* If a comment, copy it unchanged or discard it. */
3497 if (ibp[1] == '\\' && ibp[2] == '\n')
3498 newline_fix (ibp + 1);
3499 if (ibp[1] == '*') {
3500 if (put_out_comments) {
3503 } else if (! traditional) {
3506 for (ibp += 2; ibp < limit; ibp++) {
3507 /* We need not worry about newline-marks,
3508 since they are never found in comments. */
3509 if (ibp[0] == '*') {
3510 if (ibp[1] == '\\' && ibp[2] == '\n')
3511 newline_fix (ibp + 1);
3512 if (ibp[1] == '/') {
3514 if (put_out_comments) {
3521 else if (*ibp == '\n') {
3522 /* Newline in a file. Count it. */
3528 #ifdef MULTIBYTE_CHARS
3530 length = local_mblen (ibp, limit - ibp);
3533 if (put_out_comments)
3535 bcopy (ibp, obp, length - 1);
3538 ibp += (length - 1);
3542 if (put_out_comments)
3545 } else if (ibp[1] == '/' && cplusplus_comments) {
3546 if (put_out_comments) {
3549 } else if (! traditional) {
3552 for (ibp += 2; ; ibp++)
3556 if (*ibp == '\\' && ibp[1] == '\n')
3558 if (put_out_comments)
3563 #ifdef MULTIBYTE_CHARS
3565 length = local_mblen (ibp, limit - ibp);
3568 if (put_out_comments)
3570 bcopy (ibp, obp, length - 1);
3573 ibp += (length - 1);
3577 if (put_out_comments)
3583 else if (ibp[0] == '\\' && ibp[1] == '\n') {
3590 /* It isn't a macro call.
3591 Put back the space that we just skipped. */
3594 ip->lineno = old_iln;
3595 op->lineno = old_oln;
3596 /* Exit the for loop. */
3601 /* This is now known to be a macro call.
3602 Discard the macro name from the output,
3603 along with any following whitespace just copied,
3604 but preserve newlines if not outputting marks since this
3605 is more likely to do the right thing with line numbers. */
3606 obp = op->buf + obufp_before_macroname;
3608 op->lineno = op_lineno_before_macroname;
3610 int newlines = op->lineno - op_lineno_before_macroname;
3611 while (0 < newlines--)
3615 /* Prevent accidental token-pasting with a character
3616 before the macro call. */
3617 if (!traditional && obp != op->buf) {
3619 case '!': case '%': case '&': case '*':
3620 case '+': case '-': case '.': case '/':
3621 case ':': case '<': case '=': case '>':
3623 /* If we are expanding a macro arg, make a newline marker
3624 to separate the tokens. If we are making real output,
3625 a plain space will do. */
3632 /* Expand the macro, reading arguments as needed,
3633 and push the expansion on the input stack. */
3636 macroexpand (hp, op);
3638 /* Reexamine input stack, since macroexpand has pushed
3639 a new level on it. */
3646 } /* End hash-table-search loop */
3648 ident_length = hash = 0; /* Stop collecting identifier */
3651 } /* End if (ident_length > 0) */
3653 } /* End per-char loop */
3655 /* Come here to return -- but first give an error message
3656 if there was an unterminated successful conditional. */
3658 if (if_stack != ip->if_stack)
3662 switch (if_stack->type)
3683 error_with_line (line_for_error (if_stack->lineno),
3684 "unterminated `#%s' conditional", str);
3686 if_stack = ip->if_stack;
3690 * Rescan a string into a temporary buffer and return the result
3691 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3693 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3694 * and insert such markers when appropriate. See `rescan' for details.
3695 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3696 * before substitution; it is 0 for other uses.
3699 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3700 U_CHAR *buf, *limit;
3701 int output_marks, assertions;
3703 register FILE_BUF *ip;
3705 int length = limit - buf;
3707 int odepth = indepth;
3708 int save_assertions_flag = assertions_flag;
3710 assertions_flag = assertions;
3715 /* Set up the input on the input stack. */
3717 buf1 = (U_CHAR *) alloca (length + 1);
3719 register U_CHAR *p1 = buf;
3720 register U_CHAR *p2 = buf1;
3727 /* Set up to receive the output. */
3729 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3730 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3731 obuf.nominal_fname = 0;
3738 obuf.system_header_p = 0;
3740 CHECK_DEPTH ({return obuf;});
3744 ip = &instack[indepth];
3746 ip->nominal_fname = 0;
3747 ip->nominal_fname_len = 0;
3749 ip->system_header_p = 0;
3752 ip->length = length;
3753 ip->buf = ip->bufp = buf1;
3754 ip->if_stack = if_stack;
3756 ip->lineno = obuf.lineno = 1;
3758 /* Scan the input, create the output. */
3759 rescan (&obuf, output_marks);
3761 /* Pop input stack to original state. */
3764 if (indepth != odepth)
3767 /* Record the output. */
3768 obuf.length = obuf.bufp - obuf.buf;
3770 assertions_flag = save_assertions_flag;
3775 * Process a # directive. Expects IP->bufp to point after the '#', as in
3776 * `#define foo bar'. Passes to the directive handler
3777 * (do_define, do_include, etc.): the addresses of the 1st and
3778 * last chars of the directive (starting immediately after the #
3779 * keyword), plus op and the keyword table pointer. If the directive
3780 * contains comments it is copied into a temporary buffer sans comments
3781 * and the temporary buffer is passed to the directive handler instead.
3782 * Likewise for backslash-newlines.
3784 * Returns nonzero if this was a known # directive.
3785 * Otherwise, returns zero, without advancing the input pointer.
3789 handle_directive (ip, op)
3792 register U_CHAR *bp, *cp;
3793 register struct directive *kt;
3794 register int ident_length;
3797 /* Nonzero means we must copy the entire directive
3798 to get rid of comments or backslash-newlines. */
3799 int copy_directive = 0;
3801 U_CHAR *ident, *after_ident;
3805 /* Record where the directive started. do_xifdef needs this. */
3806 directive_start = bp - 1;
3808 ignore_escape_flag = 1;
3810 /* Skip whitespace and \-newline. */
3812 if (is_hor_space[*bp]) {
3813 if (*bp != ' ' && *bp != '\t' && pedantic)
3814 pedwarn ("%s in preprocessing directive", char_name[*bp]);
3816 } else if (*bp == '/') {
3817 if (bp[1] == '\\' && bp[2] == '\n')
3818 newline_fix (bp + 1);
3819 if (! (bp[1] == '*' || (cplusplus_comments && bp[1] == '/')))
3822 skip_to_end_of_comment (ip, &ip->lineno, 0);
3824 } else if (*bp == '\\' && bp[1] == '\n') {
3825 bp += 2; ip->lineno++;
3829 /* Now find end of directive name.
3830 If we encounter a backslash-newline, exchange it with any following
3831 symbol-constituents so that we end up with a contiguous name. */
3838 if (*cp == '\\' && cp[1] == '\n')
3839 name_newline_fix (cp);
3845 ident_length = cp - bp;
3849 /* A line of just `#' becomes blank. */
3851 if (ident_length == 0 && *after_ident == '\n') {
3852 ip->bufp = after_ident;
3856 if (ident_length == 0 || !is_idstart[*ident]) {
3858 while (is_idchar[*p]) {
3859 if (*p < '0' || *p > '9')
3863 /* Handle # followed by a line number. */
3864 if (p != ident && !is_idchar[*p]) {
3865 static struct directive line_directive_table[] = {
3866 { 4, do_line, "line", T_LINE},
3869 pedwarn ("`#' followed by integer");
3870 after_ident = ident;
3871 kt = line_directive_table;
3872 ignore_escape_flag = 0;
3876 /* Avoid error for `###' and similar cases unless -pedantic. */
3878 while (*p == '#' || is_hor_space[*p]) p++;
3880 if (pedantic && !lang_asm)
3881 warning ("invalid preprocessing directive");
3887 error ("invalid preprocessing directive name");
3893 * Decode the keyword and call the appropriate expansion
3894 * routine, after moving the input pointer up to the next line.
3896 for (kt = directive_table; kt->length > 0; kt++) {
3897 if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
3898 register U_CHAR *buf;
3899 register U_CHAR *limit;
3902 int *already_output;
3904 /* Nonzero means do not delete comments within the directive.
3905 #define needs this when -traditional. */
3910 limit = ip->buf + ip->length;
3913 keep_comments = traditional && kt->type == T_DEFINE;
3914 /* #import is defined only in Objective C, or when on the NeXT. */
3915 if (kt->type == T_IMPORT
3916 && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
3919 /* Find the end of this directive (first newline not backslashed
3920 and not in a string or comment).
3921 Set COPY_DIRECTIVE if the directive must be copied
3922 (it contains a backslash-newline or a comment). */
3924 buf = bp = after_ident;
3925 while (bp < limit) {
3926 register U_CHAR c = *bp++;
3934 } else if (traditional)
3940 /* "..." is special for #include. */
3941 if (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)) {
3942 while (bp < limit && *bp != '\n') {
3947 if (*bp == '\\' && bp[1] == '\n') {
3958 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated);
3959 /* Don't bother calling the directive if we already got an error
3960 message due to unterminated string. Skip everything and pretend
3961 we called the directive. */
3964 /* Traditional preprocessing permits unterminated strings. */
3973 /* <...> is special for #include. */
3975 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3977 while (bp < limit && *bp != '>' && *bp != '\n') {
3978 if (*bp == '\\' && bp[1] == '\n') {
3988 if (*bp == '\\' && bp[1] == '\n')
3991 || (cplusplus_comments && *bp == '/')) {
3992 U_CHAR *obp = bp - 1;
3994 skip_to_end_of_comment (ip, &ip->lineno, 0);
3996 /* No need to copy the directive because of a comment at the end;
3997 just don't include the comment in the directive. */
3998 if (!put_out_comments) {
4000 for (p = bp; *p == ' ' || *p == '\t'; p++)
4007 /* Don't remove the comments if -traditional. */
4008 if (! keep_comments)
4017 pedwarn ("%s in preprocessing directive", char_name[c]);
4021 --bp; /* Point to the newline */
4029 resume_p = ip->bufp;
4030 /* BP is the end of the directive.
4031 RESUME_P is the next interesting data after the directive.
4032 A comment may come between. */
4034 /* If a directive should be copied through, and -C was given,
4035 pass it through before removing comments. */
4036 if (!no_output && put_out_comments
4037 && (kt->type == T_DEFINE ? dump_macros == dump_definitions
4038 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4039 : kt->type == T_PRAGMA)) {
4042 /* Output directive name. */
4043 check_expand (op, kt->length + 2);
4044 /* Make sure # is at the start of a line */
4045 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
4050 bcopy (kt->name, op->bufp, kt->length);
4051 op->bufp += kt->length;
4053 /* Output arguments. */
4055 check_expand (op, len);
4056 bcopy (buf, (char *) op->bufp, len);
4058 /* Take account of any (escaped) newlines just output. */
4060 if (buf[len] == '\n')
4063 already_output = &junk;
4064 } /* Don't we need a newline or #line? */
4066 if (copy_directive) {
4067 register U_CHAR *xp = buf;
4068 /* Need to copy entire directive into temp buffer before dispatching */
4070 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus
4074 /* Copy to the new buffer, deleting comments
4075 and backslash-newlines (and whitespace surrounding the latter). */
4078 register U_CHAR c = *xp++;
4083 abort (); /* A bare newline should never part of the line. */
4086 /* <...> is special for #include. */
4088 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
4090 while (xp < bp && c != '>') {
4092 if (c == '\\' && xp < bp && *xp == '\n')
4103 if (cp != buf && is_hor_space[cp[-1]]) {
4104 while (cp - 1 != buf && is_hor_space[cp[-2]])
4106 SKIP_WHITE_SPACE (xp);
4107 } else if (is_hor_space[*xp]) {
4109 SKIP_WHITE_SPACE (xp);
4111 } else if (traditional && xp < bp) {
4119 register U_CHAR *bp1
4120 = skip_quoted_string (xp - 1, bp, ip->lineno,
4121 NULL_PTR, NULL_PTR, NULL_PTR);
4129 || (cplusplus_comments && *xp == '/')) {
4131 /* If we already copied the directive through,
4132 already_output != 0 prevents outputting comment now. */
4133 skip_to_end_of_comment (ip, already_output, 0);
4135 while (xp != ip->bufp)
4137 /* Delete or replace the slash. */
4138 else if (traditional)
4147 /* Null-terminate the copy. */
4153 ip->bufp = resume_p;
4155 /* Some directives should be written out for cc1 to process,
4156 just as if they were not defined. And sometimes we're copying
4157 directives through. */
4159 if (!no_output && already_output == 0
4160 && (kt->type == T_DEFINE ? (int) dump_names <= (int) dump_macros
4161 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4162 : kt->type == T_PRAGMA)) {
4165 /* Output directive name. */
4166 check_expand (op, kt->length + 1);
4168 bcopy (kt->name, (char *) op->bufp, kt->length);
4169 op->bufp += kt->length;
4171 if (kt->type == T_DEFINE && dump_macros == dump_names) {
4172 /* Output `#define name' only. */
4175 SKIP_WHITE_SPACE (xp);
4177 while (is_idchar[*xp]) xp++;
4179 check_expand (op, len + 1);
4181 bcopy (yp, (char *) op->bufp, len);
4183 /* Output entire directive. */
4185 check_expand (op, len);
4186 bcopy (buf, (char *) op->bufp, len);
4189 } /* Don't we need a newline or #line? */
4191 /* Call the appropriate directive handler. buf now points to
4192 either the appropriate place in the input buffer, or to
4193 the temp buffer if it was necessary to make one. cp
4194 points to the first char after the contents of the (possibly
4195 copied) directive, in either case. */
4196 (*kt->func) (buf, cp, op, kt);
4197 check_expand (op, ip->length - (ip->bufp - ip->buf));
4203 /* It is deliberate that we don't warn about undefined directives.
4204 That is the responsibility of cc1. */
4211 static struct tm *timebuf;
4213 time_t t = time ((time_t *) 0);
4214 timebuf = localtime (&t);
4219 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4220 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4224 * expand things like __FILE__. Place the expansion into the output
4225 * buffer *without* rescanning.
4229 special_symbol (hp, op)
4236 FILE_BUF *ip = NULL;
4239 int paren = 0; /* For special `defined' keyword */
4241 if (pcp_outfile && pcp_inside_if
4242 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
4243 error ("Predefined macro `%s' used inside `#if' during precompilation",
4246 for (i = indepth; i >= 0; i--)
4247 if (instack[i].fname != NULL) {
4252 error ("cccp error: not in any file?!");
4253 return; /* the show must go on */
4260 FILE_BUF *p = hp->type == T_FILE ? ip : &instack[0];
4261 char *string = p->nominal_fname;
4265 size_t string_len = p->nominal_fname_len;
4266 buf = (char *) alloca (3 + 4 * string_len);
4267 quote_string (buf, string, string_len);
4275 case T_INCLUDE_LEVEL:
4277 for (i = indepth; i >= 0; i--)
4278 if (instack[i].fname != NULL)
4281 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
4282 sprintf (buf, "%d", true_indepth - 1);
4286 buf = (char *) alloca (3 + strlen (version_string));
4287 sprintf (buf, "\"%s\"", version_string);
4290 #ifndef NO_BUILTIN_SIZE_TYPE
4296 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4297 case T_PTRDIFF_TYPE:
4306 case T_USER_LABEL_PREFIX_TYPE:
4307 buf = user_label_prefix;
4310 case T_REGISTER_PREFIX_TYPE:
4311 buf = REGISTER_PREFIX;
4314 case T_IMMEDIATE_PREFIX_TYPE:
4315 buf = IMMEDIATE_PREFIX;
4319 buf = hp->value.cpval;
4320 #ifdef STDC_0_IN_SYSTEM_HEADERS
4321 if (ip->system_header_p
4322 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
4323 && !lookup ((U_CHAR *) "__STRICT_ANSI__", -1, -1))
4326 if (pcp_inside_if && pcp_outfile)
4327 /* Output a precondition for this macro use */
4328 fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
4332 buf = (char *) alloca (10);
4333 sprintf (buf, "%d", ip->lineno);
4338 buf = (char *) alloca (20);
4339 timebuf = timestamp ();
4340 if (hp->type == T_DATE)
4341 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
4342 timebuf->tm_mday, timebuf->tm_year + 1900);
4344 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
4348 case T_SPEC_DEFINED:
4349 buf = " 0 "; /* Assume symbol is not defined */
4350 ip = &instack[indepth];
4351 SKIP_WHITE_SPACE (ip->bufp);
4352 if (*ip->bufp == '(') {
4354 ip->bufp++; /* Skip over the paren */
4355 SKIP_WHITE_SPACE (ip->bufp);
4358 if (!is_idstart[*ip->bufp])
4360 if (ip->bufp[0] == 'L' && (ip->bufp[1] == '\'' || ip->bufp[1] == '"'))
4362 if ((hp = lookup (ip->bufp, -1, -1))) {
4363 if (pcp_outfile && pcp_inside_if
4364 && (hp->type == T_CONST
4365 || (hp->type == T_MACRO && hp->value.defn->predefined)))
4366 /* Output a precondition for this macro use. */
4367 fprintf (pcp_outfile, "#define %s\n", hp->name);
4371 if (pcp_outfile && pcp_inside_if) {
4372 /* Output a precondition for this macro use */
4373 U_CHAR *cp = ip->bufp;
4374 fprintf (pcp_outfile, "#undef ");
4375 while (is_idchar[*cp]) /* Ick! */
4376 fputc (*cp++, pcp_outfile);
4377 putc ('\n', pcp_outfile);
4379 while (is_idchar[*ip->bufp])
4381 SKIP_WHITE_SPACE (ip->bufp);
4383 if (*ip->bufp != ')')
4391 error ("`defined' without an identifier");
4395 error ("cccp error: invalid special hash type"); /* time for gdb */
4399 check_expand (op, len);
4400 bcopy (buf, (char *) op->bufp, len);
4407 /* Routines to handle #directives */
4409 /* Handle #include and #import.
4410 This function expects to see "fname" or <fname> on the input. */
4413 do_include (buf, limit, op, keyword)
4414 U_CHAR *buf, *limit;
4416 struct directive *keyword;
4418 U_CHAR *importing = keyword->type == T_IMPORT ? (U_CHAR *) "" : (U_CHAR *) 0;
4419 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4420 static int import_warning = 0;
4421 char *fname; /* Dynamically allocated fname buffer */
4424 char *fbeg, *fend; /* Beginning and end of fname */
4427 struct file_name_list *search_start = include; /* Chain of dirs to search */
4428 struct file_name_list *dsp; /* First in chain, if #include "..." */
4429 struct file_name_list *searchptr = 0;
4432 int f = -3; /* file number */
4433 struct include_file *inc = 0;
4435 int retried = 0; /* Have already tried macro
4436 expanding the include line*/
4437 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4439 int vaxc_include = 0; /* 1 for token without punctuation */
4446 if (pedantic && !instack[indepth].system_header_p)
4449 pedwarn ("ANSI C does not allow `#import'");
4451 pedwarn ("ANSI C does not allow `#include_next'");
4454 if (importing && warn_import && !inhibit_warnings
4455 && !instack[indepth].system_header_p && !import_warning) {
4457 warning ("using `#import' is not recommended");
4458 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
4459 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
4460 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
4461 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
4462 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
4463 fprintf (stderr, " ... <real contents of file> ...\n");
4464 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
4465 fprintf (stderr, "Then users can use `#include' any number of times.\n");
4466 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
4467 fprintf (stderr, "when it is equipped with such a conditional.\n");
4473 SKIP_WHITE_SPACE (fin);
4474 /* Discard trailing whitespace so we can easily see
4475 if we have parsed all the significant chars we were given. */
4476 while (limit != fin && is_hor_space[limit[-1]]) limit--;
4477 fbeg = fend = (char *) alloca (limit - fin);
4483 /* Copy the operand text, concatenating the strings. */
4488 goto invalid_include_file_name;
4496 /* If not at the end, there had better be another string. */
4497 /* Skip just horiz space, and don't go past limit. */
4498 while (fin != limit && is_hor_space[*fin]) fin++;
4499 if (fin != limit && *fin == '\"')
4506 /* We have "filename". Figure out directory this source
4507 file is coming from and put it on the front of the list. */
4509 /* If -I- was specified, don't search current dir, only spec'd ones. */
4510 if (ignore_srcdir) break;
4512 for (fp = &instack[indepth]; fp >= instack; fp--)
4517 if ((nam = fp->nominal_fname) != NULL) {
4518 /* Found a named file. Figure out dir of the file,
4519 and put it in front of the search list. */
4520 dsp = ((struct file_name_list *)
4521 alloca (sizeof (struct file_name_list)
4522 + fp->nominal_fname_len));
4523 strcpy (dsp->fname, nam);
4524 simplify_filename (dsp->fname);
4525 nam = base_name (dsp->fname);
4528 /* for hack_vms_include_specification(), a local
4529 dir specification must start with "./" on VMS. */
4530 if (nam == dsp->fname)
4537 /* But for efficiency's sake, do not insert the dir
4538 if it matches the search list's first dir. */
4539 dsp->next = search_start;
4540 if (!search_start || strcmp (dsp->fname, search_start->fname)) {
4542 n = nam - dsp->fname;
4543 if (n + INCLUDE_LEN_FUDGE > max_include_len)
4544 max_include_len = n + INCLUDE_LEN_FUDGE;
4546 dsp[0].got_name_map = 0;
4554 while (fin != limit && *fin != '>')
4556 if (*fin == '>' && fin + 1 == limit) {
4558 /* If -I-, start with the first -I dir after the -I-. */
4559 search_start = first_bracket_include;
4567 * Support '#include xyz' like VAX-C to allow for easy use of all the
4568 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4569 * code from case '<' is repeated here) and generates a warning.
4570 * (Note: macro expansion of `xyz' takes precedence.)
4572 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4573 the pre-decrement outside of the macro. */
4574 if (retried && (--fin, ISALPHA(*(U_CHAR *) (fin)))) {
4575 while (fin != limit && (!ISSPACE(*fin)))
4577 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4581 /* If -I-, start with the first -I dir after the -I-. */
4582 search_start = first_bracket_include;
4590 /* Expand buffer and then remove any newline markers.
4591 We can't just tell expand_to_temp_buffer to omit the markers,
4592 since it would put extra spaces in include file names. */
4595 int errors_before_expansion = errors;
4596 trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
4597 if (errors != errors_before_expansion) {
4599 goto invalid_include_file_name;
4602 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
4604 while (src != trybuf.bufp) {
4605 switch ((*limit++ = *src++)) {
4614 U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
4615 NULL_PTR, NULL_PTR, NULL_PTR);
4628 invalid_include_file_name:
4629 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
4633 /* For #include_next, skip in the search path
4634 past the dir in which the containing file was found. */
4637 for (fp = &instack[indepth]; fp >= instack; fp--)
4638 if (fp->fname != NULL) {
4639 /* fp->dir is null if the containing file was specified
4640 with an absolute file name. In that case, don't skip anything. */
4642 search_start = fp->dir->next;
4648 flen = simplify_filename (fbeg);
4652 error ("empty file name in `#%s'", keyword->name);
4656 /* Allocate this permanently, because it gets stored in the definitions
4658 fname = xmalloc (max_include_len + flen + 1);
4659 /* + 1 above for terminating null. */
4661 system_include_depth += angle_brackets;
4663 /* If specified file name is absolute, just open it. */
4665 if (absolute_filename (fbeg)) {
4666 strcpy (fname, fbeg);
4667 f = open_include_file (fname, NULL_PTR, importing, &inc);
4671 struct bypass_dir *next;
4673 struct file_name_list *searchptr;
4674 } **bypass_slot = 0;
4676 /* Search directory path, trying to open the file.
4677 Copy each filename tried into FNAME. */
4679 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
4681 if (searchptr == first_bracket_include) {
4682 /* Go to bypass directory if we know we've seen this file before. */
4683 static struct bypass_dir *bypass_hashtab[INCLUDE_HASHSIZE];
4684 struct bypass_dir *p;
4685 bypass_slot = &bypass_hashtab[hashf ((U_CHAR *) fbeg, flen,
4687 for (p = *bypass_slot; p; p = p->next)
4688 if (!strcmp (fbeg, p->fname)) {
4689 searchptr = p->searchptr;
4696 /* Change this 1/2 Unix 1/2 VMS file specification into a
4697 full VMS file specification */
4698 if (searchptr->fname[0])
4700 strcpy (fname, searchptr->fname);
4701 if (fname[strlen (fname) - 1] == ':')
4704 slashp = strchr (fbeg, '/');
4706 /* start at root-dir of logical device if no path given. */
4708 strcat (fname, "[000000]");
4710 strcat (fname, fbeg);
4712 /* Fix up the filename */
4713 hack_vms_include_specification (fname, vaxc_include);
4717 /* This is a normal VMS filespec, so use it unchanged. */
4718 strcpy (fname, fbeg);
4719 /* if it's '#include filename', add the missing .h */
4720 if (vaxc_include && index(fname,'.')==NULL)
4721 strcat (fname, ".h");
4724 strcpy (fname, searchptr->fname);
4725 strcat (fname, fbeg);
4727 f = open_include_file (fname, searchptr, importing, &inc);
4729 if (bypass_slot && searchptr != first_bracket_include) {
4730 /* This is the first time we found this include file,
4731 and we found it after first_bracket_include.
4732 Record its location so that we can bypass to here next time. */
4733 struct bypass_dir *p
4734 = (struct bypass_dir *) xmalloc (sizeof (struct bypass_dir));
4735 p->next = *bypass_slot;
4736 p->fname = fname + strlen (searchptr->fname);
4737 p->searchptr = searchptr;
4743 /* Our VMS hacks can produce invalid filespecs, so don't worry
4744 about errors other than EACCES. */
4745 if (errno == EACCES)
4748 if (errno != ENOENT && errno != ENOTDIR)
4758 /* The file was already included. */
4760 /* If generating dependencies and -MG was specified, we assume missing
4761 files are leaf files, living in the same directory as the source file
4762 or other similar place; these missing files may be generated from
4763 other files and may not exist yet (eg: y.tab.h). */
4764 } else if (print_deps_missing_files
4765 && (system_include_depth != 0) < print_deps)
4767 /* If it was requested as a system header file,
4768 then assume it belongs in the first place to look for such. */
4772 char *p = (char *) alloca (strlen (search_start->fname)
4773 + strlen (fbeg) + 1);
4774 strcpy (p, search_start->fname);
4776 deps_output (p, ' ');
4781 /* Otherwise, omit the directory, as if the file existed
4782 in the directory with the source. */
4783 deps_output (fbeg, ' ');
4786 /* If -M was specified, and this header file won't be added to the
4787 dependency list, then don't count this as an error, because we can
4788 still produce correct output. Otherwise, we can't produce correct
4789 output, because there may be dependencies we need inside the missing
4790 file, and we don't know what directory this missing file exists in. */
4791 else if (0 < print_deps && print_deps <= (system_include_depth != 0))
4792 warning ("No include path in which to find %s", fbeg);
4794 error_from_errno (fbeg);
4796 error ("No include path in which to find %s", fbeg);
4800 /* Actually process the file. */
4802 pcftry = (char *) alloca (strlen (fname) + 30);
4809 sprintf (pcftry, "%s%d", fname, pcfnum++);
4811 pcf = open (pcftry, O_RDONLY, 0666);
4816 if (fstat (pcf, &s) != 0)
4817 pfatal_with_name (pcftry);
4818 if (! INO_T_EQ (inc->st.st_ino, s.st_ino)
4819 || inc->st.st_dev != s.st_dev)
4821 pcfbuf = check_precompiled (pcf, &s, fname, &pcfbuflimit);
4822 /* Don't need it any more. */
4827 /* Don't need it at all. */
4832 } while (pcf != -1 && !pcfbuf);
4835 /* Actually process the file */
4837 pcfname = xmalloc (strlen (pcftry) + 1);
4838 strcpy (pcfname, pcftry);
4839 pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) fname, op);
4842 finclude (f, inc, op, is_system_include (fname), searchptr);
4845 system_include_depth -= angle_brackets;
4850 /* Return nonzero if the given FILENAME is an absolute pathname which
4851 designates a file within one of the known "system" include file
4852 directories. We assume here that if the given FILENAME looks like
4853 it is the name of a file which resides either directly in a "system"
4854 include file directory, or within any subdirectory thereof, then the
4855 given file must be a "system" include file. This function tells us
4856 if we should suppress pedantic errors/warnings for the given FILENAME.
4858 The value is 2 if the file is a C-language system header file
4859 for which C++ should (on most systems) assume `extern "C"'. */
4862 is_system_include (filename)
4863 register char *filename;
4865 struct file_name_list *searchptr;
4867 for (searchptr = first_system_include; searchptr;
4868 searchptr = searchptr->next)
4869 if (! strncmp (searchptr->fname, filename, strlen (searchptr->fname)))
4870 return searchptr->c_system_include_path + 1;
4874 /* Yield the non-directory suffix of a file name. */
4882 #if defined (__MSDOS__) || defined (_WIN32)
4883 if (ISALPHA (s[0]) && s[1] == ':') s += 2;
4886 if ((p = rindex (s, ':'))) s = p + 1; /* Skip device. */
4887 if ((p = rindex (s, ']'))) s = p + 1; /* Skip directory. */
4888 if ((p = rindex (s, '>'))) s = p + 1; /* Skip alternate (int'n'l) dir. */
4892 if ((p = rindex (s, '/'))) s = p + 1;
4893 #ifdef DIR_SEPARATOR
4894 if ((p = rindex (s, DIR_SEPARATOR))) s = p + 1;
4899 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4902 absolute_filename (filename)
4905 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN__))
4906 if (ISALPHA (filename[0]) && filename[1] == ':') filename += 2;
4908 #if defined (__CYGWIN__)
4909 /* At present, any path that begins with a drive spec is absolute. */
4910 if (ISALPHA (filename[0]) && filename[1] == ':') return 1;
4913 if (index (filename, ':') != 0) return 1;
4915 if (filename[0] == '/') return 1;
4916 #ifdef DIR_SEPARATOR
4917 if (filename[0] == DIR_SEPARATOR) return 1;
4922 /* Remove unnecessary characters from FILENAME in place,
4923 to avoid unnecessary filename aliasing.
4924 Return the length of the resulting string.
4926 Do only the simplifications allowed by Posix.
4927 It is OK to miss simplifications on non-Posix hosts,
4928 since this merely leads to suboptimal results. */
4931 simplify_filename (filename)
4934 register char *from = filename;
4935 register char *to = filename;
4938 /* Remove redundant initial /s. */
4941 if (*++from == '/') {
4942 if (*++from == '/') {
4943 /* 3 or more initial /s are equivalent to 1 /. */
4944 while (*++from == '/')
4947 /* On some hosts // differs from /; Posix allows this. */
4948 static int slashslash_vs_slash;
4949 if (slashslash_vs_slash == 0) {
4951 slashslash_vs_slash = ((stat ("/", &s1) == 0 && stat ("//", &s2) == 0
4952 && INO_T_EQ (s1.st_ino, s2.st_ino)
4953 && s1.st_dev == s2.st_dev)
4956 if (slashslash_vs_slash < 0)
4965 if (from[0] == '.' && from[1] == '/')
4970 /* Copy this component and trailing /, if any. */
4971 while ((*to++ = *from++) != '/') {
4973 /* Trim . component at end of nonempty name. */
4974 to -= filename <= to - 3 && to[-3] == '/' && to[-2] == '.';
4976 /* Trim unnecessary trailing /s. */
4977 while (to0 < --to && to[-1] == '/')
4981 return to - filename;
4986 /* Skip /s after a /. */
4987 while (*from == '/')
4992 /* The file_name_map structure holds a mapping of file names for a
4993 particular directory. This mapping is read from the file named
4994 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4995 map filenames on a file system with severe filename restrictions,
4996 such as DOS. The format of the file name map file is just a series
4997 of lines with two tokens on each line. The first token is the name
4998 to map, and the second token is the actual name to use. */
5000 struct file_name_map
5002 struct file_name_map *map_next;
5007 #define FILE_NAME_MAP_FILE "header.gcc"
5009 /* Read a space delimited string of unlimited length from a stdio
5013 read_filename_string (ch, f)
5021 set = alloc = xmalloc (len + 1);
5025 while ((ch = getc (f)) != EOF && ! is_space[ch])
5027 if (set - alloc == len)
5030 alloc = xrealloc (alloc, len + 1);
5031 set = alloc + len / 2;
5041 /* Read the file name map file for DIRNAME.
5042 If DIRNAME is empty, read the map file for the working directory;
5043 otherwise DIRNAME must end in '/'. */
5045 static struct file_name_map *
5046 read_name_map (dirname)
5049 /* This structure holds a linked list of file name maps, one per
5051 struct file_name_map_list
5053 struct file_name_map_list *map_list_next;
5054 char *map_list_name;
5055 struct file_name_map *map_list_map;
5057 static struct file_name_map_list *map_list;
5058 register struct file_name_map_list *map_list_ptr;
5063 for (map_list_ptr = map_list; map_list_ptr;
5064 map_list_ptr = map_list_ptr->map_list_next)
5065 if (! strcmp (map_list_ptr->map_list_name, dirname))
5066 return map_list_ptr->map_list_map;
5068 map_list_ptr = ((struct file_name_map_list *)
5069 xmalloc (sizeof (struct file_name_map_list)));
5070 map_list_ptr->map_list_name = savestring (dirname);
5071 map_list_ptr->map_list_map = NULL;
5073 dirlen = strlen (dirname);
5074 name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 1);
5075 strcpy (name, dirname);
5076 strcat (name, FILE_NAME_MAP_FILE);
5077 f = fopen (name, "r");
5079 map_list_ptr->map_list_map = NULL;
5084 while ((ch = getc (f)) != EOF)
5087 struct file_name_map *ptr;
5092 from = read_filename_string (ch, f);
5093 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5095 to = read_filename_string (ch, f);
5097 simplify_filename (from);
5098 tolen = simplify_filename (to);
5100 ptr = ((struct file_name_map *)
5101 xmalloc (sizeof (struct file_name_map)));
5102 ptr->map_from = from;
5104 /* Make the real filename absolute. */
5105 if (absolute_filename (to))
5109 ptr->map_to = xmalloc (dirlen + tolen + 1);
5110 strcpy (ptr->map_to, dirname);
5111 strcat (ptr->map_to, to);
5115 ptr->map_next = map_list_ptr->map_list_map;
5116 map_list_ptr->map_list_map = ptr;
5118 while ((ch = getc (f)) != '\n')
5125 map_list_ptr->map_list_next = map_list;
5126 map_list = map_list_ptr;
5128 return map_list_ptr->map_list_map;
5131 /* Try to open include file FILENAME. SEARCHPTR is the directory
5132 being tried from the include file search path.
5133 IMPORTING is "" if we are importing, null otherwise.
5134 Return -2 if found, either a matching name or a matching inode.
5135 Otherwise, open the file and return a file descriptor if successful
5136 or -1 if unsuccessful.
5137 Unless unsuccessful, put a descriptor of the included file into *PINC.
5138 This function maps filenames on file systems based on information read by
5142 open_include_file (filename, searchptr, importing, pinc)
5144 struct file_name_list *searchptr;
5146 struct include_file **pinc;
5148 char *fname = remap ? remap_include_file (filename, searchptr) : filename;
5151 /* Look up FNAME in include_hashtab. */
5152 struct include_file **phead = &include_hashtab[hashf ((U_CHAR *) fname,
5155 struct include_file *inc, *head = *phead;
5156 for (inc = head; inc; inc = inc->next)
5157 if (!strcmp (fname, inc->fname))
5161 || ! inc->control_macro
5162 || (inc->control_macro[0] && ! lookup (inc->control_macro, -1, -1))) {
5164 fd = open (fname, O_RDONLY, 0);
5169 /* if #include <dir/file> fails, try again with hacked spec. */
5170 if (!hack_vms_include_specification (fname, 0))
5172 fd = open (fname, O_RDONLY, 0);
5179 /* FNAME was not in include_hashtab; insert a new entry. */
5180 inc = (struct include_file *) xmalloc (sizeof (struct include_file));
5183 inc->control_macro = 0;
5184 inc->deps_output = 0;
5185 if (fstat (fd, &inc->st) != 0)
5186 pfatal_with_name (fname);
5189 /* Look for another file with the same inode and device. */
5190 if (lookup_ino_include (inc)
5191 && inc->control_macro
5192 && (!inc->control_macro[0] || lookup (inc->control_macro, -1, -1))) {
5198 /* For -M, add this file to the dependencies. */
5199 if (! inc->deps_output && (system_include_depth != 0) < print_deps) {
5200 inc->deps_output = 1;
5201 deps_output (fname, ' ');
5204 /* Handle -H option. */
5205 if (print_include_names)
5206 fprintf (stderr, "%*s%s\n", indepth, "", fname);
5210 inc->control_macro = importing;
5216 /* Return the remapped name of the include file FILENAME.
5217 SEARCHPTR is the directory being tried from the include file path. */
5220 remap_include_file (filename, searchptr)
5222 struct file_name_list *searchptr;
5224 register struct file_name_map *map;
5225 register char *from;
5229 if (! searchptr->got_name_map)
5231 searchptr->name_map = read_name_map (searchptr->fname);
5232 searchptr->got_name_map = 1;
5235 /* Check the mapping for the directory we are using. */
5236 from = filename + strlen (searchptr->fname);
5237 for (map = searchptr->name_map; map; map = map->map_next)
5238 if (! strcmp (map->map_from, from))
5242 from = base_name (filename);
5244 if (from != filename || !searchptr)
5246 /* Try to find a mapping file for the particular directory we are
5247 looking in. Thus #include <sys/types.h> will look up sys/types.h
5248 in /usr/include/header.gcc and look up types.h in
5249 /usr/include/sys/header.gcc. */
5251 char *dir = (char *) alloca (from - filename + 1);
5252 bcopy (filename, dir, from - filename);
5253 dir[from - filename] = '\0';
5255 for (map = read_name_map (dir); map; map = map->map_next)
5256 if (! strcmp (map->map_from, from))
5263 /* Insert INC into the include file table, hashed by device and inode number.
5264 If a file with different name but same dev+ino was already in the table,
5265 return 1 and set INC's control macro to the already-known macro. */
5268 lookup_ino_include (inc)
5269 struct include_file *inc;
5271 int hash = ((unsigned) (inc->st.st_dev + INO_T_HASH (inc->st.st_ino))
5272 % INCLUDE_HASHSIZE);
5273 struct include_file *i = include_ino_hashtab[hash];
5275 include_ino_hashtab[hash] = inc;
5277 for (; i; i = i->next_ino)
5278 if (INO_T_EQ (inc->st.st_ino, i->st.st_ino)
5279 && inc->st.st_dev == i->st.st_dev) {
5280 inc->control_macro = i->control_macro;
5287 /* Process file descriptor F, which corresponds to include file INC,
5289 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5290 "system" include directories (as decided by the `is_system_include'
5292 DIRPTR is the link in the dir path through which this file was found,
5293 or 0 if the file name was absolute. */
5296 finclude (f, inc, op, system_header_p, dirptr)
5298 struct include_file *inc;
5300 int system_header_p;
5301 struct file_name_list *dirptr;
5303 char *fname = inc->fname;
5305 FILE_BUF *fp; /* For input stack frame */
5306 int missing_newline = 0;
5308 CHECK_DEPTH (return;);
5310 fp = &instack[indepth + 1];
5311 bzero ((char *) fp, sizeof (FILE_BUF));
5312 fp->nominal_fname = fp->fname = fname;
5313 fp->nominal_fname_len = strlen (fname);
5317 fp->if_stack = if_stack;
5318 fp->system_header_p = system_header_p;
5321 if (S_ISREG (inc->st.st_mode)) {
5322 size_t s = (size_t) inc->st.st_size;
5323 if (s != inc->st.st_size || s + 2 < s)
5325 fp->buf = (U_CHAR *) xmalloc (s + 2);
5328 /* Read the file contents, knowing that s is an upper bound
5329 on the number of bytes we can read. */
5330 fp->length = safe_read (f, (char *) fp->buf, s);
5331 if (fp->length < 0) goto nope;
5333 else if (S_ISDIR (inc->st.st_mode)) {
5334 error ("directory `%s' specified in #include", fname);
5338 /* Cannot count its file size before reading.
5339 First read the entire file into heap and
5340 copy them into buffer on stack. */
5345 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5348 i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
5350 goto nope; /* error! */
5352 if (st_size != bsize)
5353 break; /* End of file */
5355 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5358 fp->length = st_size;
5361 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5362 /* Backslash-newline at end is not good enough. */
5363 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5364 fp->buf[fp->length++] = '\n';
5365 missing_newline = 1;
5367 fp->buf[fp->length] = '\0';
5369 /* Close descriptor now, so nesting does not use lots of descriptors. */
5372 /* Must do this before calling trigraph_pcp, so that the correct file name
5373 will be printed in warning messages. */
5376 input_file_stack_tick++;
5381 output_line_directive (fp, op, 0, enter_file);
5384 if (missing_newline)
5387 if (pedantic && missing_newline)
5388 pedwarn ("file does not end in newline");
5391 input_file_stack_tick++;
5392 output_line_directive (&instack[indepth], op, 0, leave_file);
5398 perror_with_name (fname);
5403 /* Record that inclusion of the include file INC
5404 should be controlled by the macro named MACRO_NAME.
5405 This means that trying to include the file again
5406 will do something if that macro is defined. */
5409 record_control_macro (inc, macro_name)
5410 struct include_file *inc;
5413 if (!inc->control_macro || inc->control_macro[0])
5414 inc->control_macro = macro_name;
5417 /* Load the specified precompiled header into core, and verify its
5418 preconditions. PCF indicates the file descriptor to read, which must
5419 be a regular file. *ST is its file status.
5420 FNAME indicates the file name of the original header.
5421 *LIMIT will be set to an address one past the end of the file.
5422 If the preconditions of the file are not satisfied, the buffer is
5423 freed and we return 0. If the preconditions are satisfied, return
5424 the address of the buffer following the preconditions. The buffer, in
5425 this case, should never be freed because various pieces of it will
5426 be referred to until all precompiled strings are output at the end of
5430 check_precompiled (pcf, st, fname, limit)
5433 char *fname ATTRIBUTE_UNUSED;
5443 if (S_ISREG (st->st_mode))
5445 size_t s = (size_t) st->st_size;
5446 if (s != st->st_size || s + 2 < s)
5448 buf = xmalloc (s + 2);
5449 length = safe_read (pcf, buf, s);
5456 if (length > 0 && buf[length-1] != '\n')
5457 buf[length++] = '\n';
5460 *limit = buf + length;
5462 /* File is in core. Check the preconditions. */
5463 if (!check_preconditions (buf))
5465 for (cp = buf; *cp; cp++)
5468 fprintf (stderr, "Using preinclude %s\n", fname);
5474 fprintf (stderr, "Cannot use preinclude %s\n", fname);
5480 /* PREC (null terminated) points to the preconditions of a
5481 precompiled header. These are a series of #define and #undef
5482 lines which must match the current contents of the hash
5486 check_preconditions (prec)
5493 lineend = index (prec, '\n');
5495 if (*prec++ != '#') {
5496 error ("Bad format encountered while reading precompiled file");
5499 if (!strncmp (prec, "define", 6)) {
5503 mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR);
5508 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
5509 || (hp->type != T_MACRO && hp->type != T_CONST)
5510 || (hp->type == T_MACRO
5511 && !compare_defs (mdef.defn, hp->value.defn)
5512 && (mdef.defn->length != 2
5513 || mdef.defn->expansion[0] != '\n'
5514 || mdef.defn->expansion[1] != ' ')))
5516 } else if (!strncmp (prec, "undef", 5)) {
5521 while (is_hor_space[(U_CHAR) *prec])
5524 while (is_idchar[(U_CHAR) *prec])
5528 if (lookup ((U_CHAR *) name, len, -1))
5531 error ("Bad format encountered while reading precompiled file");
5536 /* They all passed successfully */
5540 /* Process the main body of a precompiled file. BUF points to the
5541 string section of the file, following the preconditions. LIMIT is one
5542 character past the end. NAME is the name of the file being read
5543 in. OP is the main output buffer. */
5546 pcfinclude (buf, name, op)
5554 /* First in the file comes 4 bytes indicating the number of strings, */
5555 /* in network byte order. (MSB first). */
5557 nstrings = (nstrings << 8) | *cp++;
5558 nstrings = (nstrings << 8) | *cp++;
5559 nstrings = (nstrings << 8) | *cp++;
5561 /* Looping over each string... */
5562 while (nstrings--) {
5563 U_CHAR *string_start;
5564 U_CHAR *endofthiskey;
5568 /* Each string starts with a STRINGDEF structure (str), followed */
5569 /* by the text of the string (string_start) */
5571 /* First skip to a longword boundary */
5572 /* ??? Why a 4-byte boundary? On all machines? */
5573 /* NOTE: This works correctly even if size_t
5574 is narrower than a pointer.
5575 Do not try risky measures here to get another type to use!
5576 Do not include stddef.h--it will fail! */
5577 if ((size_t) cp & 3)
5578 cp += 4 - ((size_t) cp & 3);
5580 /* Now get the string. */
5581 str = (STRINGDEF *) (GENERIC_PTR) cp;
5582 string_start = cp += sizeof (STRINGDEF);
5584 for (; *cp; cp++) /* skip the string */
5587 /* We need to macro expand the string here to ensure that the
5588 proper definition environment is in place. If it were only
5589 expanded when we find out it is needed, macros necessary for
5590 its proper expansion might have had their definitions changed. */
5591 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
5592 /* Lineno is already set in the precompiled file */
5593 str->contents = tmpbuf.buf;
5594 str->len = tmpbuf.length;
5596 str->filename = name;
5597 str->output_mark = outbuf.bufp - outbuf.buf;
5600 *stringlist_tailp = str;
5601 stringlist_tailp = &str->chain;
5603 /* Next comes a fourbyte number indicating the number of keys
5606 nkeys = (nkeys << 8) | *cp++;
5607 nkeys = (nkeys << 8) | *cp++;
5608 nkeys = (nkeys << 8) | *cp++;
5610 /* If this number is -1, then the string is mandatory. */
5614 /* Otherwise, for each key, */
5615 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
5616 KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp;
5619 /* It starts with a KEYDEF structure */
5620 cp += sizeof (KEYDEF);
5622 /* Find the end of the key. At the end of this for loop we
5623 advance CP to the start of the next key using this variable. */
5624 endofthiskey = cp + strlen ((char *) cp);
5627 /* Expand the key, and enter it into the hash table. */
5628 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
5629 tmpbuf.bufp = tmpbuf.buf;
5631 while (is_hor_space[*tmpbuf.bufp])
5633 if (!is_idstart[*tmpbuf.bufp]
5634 || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
5639 hp = lookup (tmpbuf.bufp, -1, -1);
5642 install (tmpbuf.bufp, -1, T_PCSTRING, (char *) kp, -1);
5644 else if (hp->type == T_PCSTRING) {
5645 kp->chain = hp->value.keydef;
5646 hp->value.keydef = kp;
5652 /* This output_line_directive serves to switch us back to the current
5653 input file in case some of these strings get output (which will
5654 result in line directives for the header file being output). */
5655 output_line_directive (&instack[indepth], op, 0, enter_file);
5658 /* Called from rescan when it hits a key for strings. Mark them all
5659 used and clean up. */
5667 for (kp = hp->value.keydef; kp; kp = kp->chain)
5668 kp->str->writeflag = 1;
5672 /* Write the output, interspersing precompiled strings in their
5673 appropriate places. */
5678 STRINGDEF *next_string;
5679 U_CHAR *cur_buf_loc;
5680 int line_directive_len = 80;
5681 char *line_directive = xmalloc (line_directive_len);
5684 /* In each run through the loop, either cur_buf_loc ==
5685 next_string_loc, in which case we print a series of strings, or
5686 it is less than next_string_loc, in which case we write some of
5688 cur_buf_loc = outbuf.buf;
5689 next_string = stringlist;
5691 while (cur_buf_loc < outbuf.bufp || next_string) {
5693 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
5694 if (next_string->writeflag) {
5695 len = 4 * strlen ((char *) next_string->filename) + 32;
5696 while (len > line_directive_len)
5697 line_directive = xrealloc (line_directive,
5698 line_directive_len *= 2);
5699 sprintf (line_directive, "\n# %d ", next_string->lineno);
5700 strcpy (quote_string (line_directive + strlen (line_directive),
5701 (char *) next_string->filename,
5702 strlen ((char *) next_string->filename)),
5704 safe_write (fileno (stdout), line_directive, strlen (line_directive));
5705 safe_write (fileno (stdout),
5706 (char *) next_string->contents, next_string->len);
5708 next_string = next_string->chain;
5712 ? (next_string->output_mark
5713 - (cur_buf_loc - outbuf.buf))
5714 : outbuf.bufp - cur_buf_loc);
5716 safe_write (fileno (stdout), (char *) cur_buf_loc, len);
5720 free (line_directive);
5723 /* Pass a directive through to the output file.
5724 BUF points to the contents of the directive, as a contiguous string.
5725 LIMIT points to the first character past the end of the directive.
5726 KEYWORD is the keyword-table entry for the directive. */
5729 pass_thru_directive (buf, limit, op, keyword)
5730 U_CHAR *buf, *limit;
5732 struct directive *keyword;
5734 register unsigned keyword_length = keyword->length;
5736 check_expand (op, 1 + keyword_length + (limit - buf));
5738 bcopy (keyword->name, (char *) op->bufp, keyword_length);
5739 op->bufp += keyword_length;
5740 if (limit != buf && buf[0] != ' ')
5742 bcopy ((char *) buf, (char *) op->bufp, limit - buf);
5743 op->bufp += (limit - buf);
5746 /* Count the line we have just made in the output,
5747 to get in sync properly. */
5752 /* The arglist structure is built by do_define to tell
5753 collect_definition where the argument names begin. That
5754 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5755 would contain pointers to the strings x, y, and z.
5756 Collect_definition would then build a DEFINITION node,
5757 with reflist nodes pointing to the places x, y, and z had
5758 appeared. So the arglist is just convenience data passed
5759 between these two routines. It is not kept around after
5760 the current #define has been processed and entered into the
5764 struct arglist *next;
5771 /* Create a DEFINITION node from a #define directive. Arguments are
5772 as for do_define. */
5775 create_definition (buf, limit, op)
5776 U_CHAR *buf, *limit;
5779 U_CHAR *bp; /* temp ptr into input buffer */
5780 U_CHAR *symname; /* remember where symbol name starts */
5781 int sym_length; /* and how long it is */
5782 int line = instack[indepth].lineno;
5783 char *file = instack[indepth].nominal_fname;
5784 size_t file_len = instack[indepth].nominal_fname_len;
5788 int arglengths = 0; /* Accumulate lengths of arg names
5789 plus number of args. */
5794 while (is_hor_space[*bp])
5797 symname = bp; /* remember where it starts */
5798 sym_length = check_macro_name (bp, "macro");
5801 /* Lossage will occur if identifiers or control keywords are broken
5802 across lines using backslash. This is not the right place to take
5806 struct arglist *arg_ptrs = NULL;
5809 bp++; /* skip '(' */
5810 SKIP_WHITE_SPACE (bp);
5812 /* Loop over macro argument names. */
5813 while (*bp != ')') {
5814 struct arglist *temp;
5816 temp = (struct arglist *) alloca (sizeof (struct arglist));
5818 temp->next = arg_ptrs;
5819 temp->argno = argno++;
5820 temp->rest_args = 0;
5824 pedwarn ("another parameter follows `%s'",
5827 if (!is_idstart[*bp])
5829 if (c9x && limit - bp > (long) REST_EXTENSION_LENGTH
5830 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
5832 /* This is the ISO C 9x way to write macros with variable
5833 number of arguments. */
5835 temp->rest_args = 1;
5838 pedwarn ("invalid character in macro parameter name");
5841 /* Find the end of the arg name. */
5842 while (is_idchar[*bp]) {
5844 /* do we have a "special" rest-args extension here? */
5845 if (limit - bp > (long) REST_EXTENSION_LENGTH
5846 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
5847 if (pedantic && !instack[indepth].system_header_p)
5848 pedwarn ("ANSI C does not allow macro with variable arguments");
5850 temp->rest_args = 1;
5854 if (bp == temp->name && rest_args == 1)
5856 /* This is the ISO C 9x style. */
5857 temp->name = va_args_name;
5858 temp->length = VA_ARGS_NAME_LENGTH;
5861 temp->length = bp - temp->name;
5863 bp += REST_EXTENSION_LENGTH;
5864 arglengths += temp->length + 2;
5865 SKIP_WHITE_SPACE (bp);
5866 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
5867 error ("badly punctuated parameter list in `#define'");
5872 SKIP_WHITE_SPACE (bp);
5873 /* A comma at this point can only be followed by an identifier. */
5874 if (!is_idstart[*bp]
5875 && !(c9x && limit - bp > (long) REST_EXTENSION_LENGTH
5876 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)) {
5877 error ("badly punctuated parameter list in `#define'");
5882 error ("unterminated parameter list in `#define'");
5886 struct arglist *otemp;
5888 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
5889 if (temp->length == otemp->length
5890 && bcmp (temp->name, otemp->name, temp->length) == 0)
5892 error ("duplicate argument name `%.*s' in `#define'",
5893 temp->length, temp->name);
5896 if (rest_args == 0 && temp->length == VA_ARGS_NAME_LENGTH
5897 && bcmp (temp->name, va_args_name, VA_ARGS_NAME_LENGTH) == 0)
5900 reserved name `%s' used as argument name in `#define'", va_args_name);
5906 ++bp; /* skip paren */
5907 SKIP_WHITE_SPACE (bp);
5908 /* now everything from bp before limit is the definition. */
5909 defn = collect_expansion (bp, limit, argno, arg_ptrs);
5910 defn->rest_args = rest_args;
5912 /* Now set defn->args.argnames to the result of concatenating
5913 the argument names in reverse order
5914 with comma-space between them. */
5915 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
5917 struct arglist *temp;
5919 for (temp = arg_ptrs; temp; temp = temp->next) {
5920 bcopy (temp->name, &defn->args.argnames[i], temp->length);
5922 if (temp->next != 0) {
5923 defn->args.argnames[i++] = ',';
5924 defn->args.argnames[i++] = ' ';
5927 defn->args.argnames[i] = 0;
5930 /* Simple expansion or empty definition. */
5934 if (is_hor_space[*bp]) {
5936 SKIP_WHITE_SPACE (bp);
5937 } else if (sym_length) {
5939 case '!': case '"': case '#': case '%': case '&': case '\'':
5940 case ')': case '*': case '+': case ',': case '-': case '.':
5941 case '/': case ':': case ';': case '<': case '=': case '>':
5942 case '?': case '[': case '\\': case ']': case '^': case '{':
5943 case '|': case '}': case '~':
5944 warning ("missing white space after `#define %.*s'",
5945 sym_length, symname);
5949 pedwarn ("missing white space after `#define %.*s'",
5950 sym_length, symname);
5955 /* Now everything from bp before limit is the definition. */
5956 defn = collect_expansion (bp, limit, -1, NULL_PTR);
5957 defn->args.argnames = (U_CHAR *) "";
5962 defn->file_len = file_len;
5964 /* OP is null if this is a predefinition */
5965 defn->predefined = !op;
5967 mdef.symnam = symname;
5968 mdef.symlen = sym_length;
5977 /* Process a #define directive.
5978 BUF points to the contents of the #define directive, as a contiguous string.
5979 LIMIT points to the first character past the end of the definition.
5980 KEYWORD is the keyword-table entry for #define. */
5983 do_define (buf, limit, op, keyword)
5984 U_CHAR *buf, *limit;
5986 struct directive *keyword;
5991 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5992 if (pcp_outfile && op)
5993 pass_thru_directive (buf, limit, op, keyword);
5995 mdef = create_definition (buf, limit, op);
5999 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
6003 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
6005 /* Redefining a precompiled key is ok. */
6006 if (hp->type == T_PCSTRING)
6008 /* Redefining a macro is ok if the definitions are the same. */
6009 else if (hp->type == T_MACRO)
6010 ok = ! compare_defs (mdef.defn, hp->value.defn);
6011 /* Redefining a constant is ok with -D. */
6012 else if (hp->type == T_CONST)
6013 ok = ! done_initializing;
6014 /* Print the warning if it's not ok. */
6016 /* If we are passing through #define and #undef directives, do
6017 that for this re-definition now. */
6018 if (debug_output && op)
6019 pass_thru_directive (buf, limit, op, keyword);
6021 pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
6022 if (hp->type == T_MACRO)
6023 pedwarn_with_file_and_line (hp->value.defn->file,
6024 hp->value.defn->file_len,
6025 hp->value.defn->line,
6026 "this is the location of the previous definition");
6028 /* Replace the old definition. */
6030 hp->value.defn = mdef.defn;
6032 /* If we are passing through #define and #undef directives, do
6033 that for this new definition now. */
6034 if (debug_output && op)
6035 pass_thru_directive (buf, limit, op, keyword);
6036 install (mdef.symnam, mdef.symlen, T_MACRO,
6037 (char *) mdef.defn, hashcode);
6048 /* Check a purported macro name SYMNAME, and yield its length.
6049 USAGE is the kind of name this is intended for. */
6052 check_macro_name (symname, usage)
6059 for (p = symname; is_idchar[*p]; p++)
6061 sym_length = p - symname;
6063 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
6064 error ("invalid %s name", usage);
6065 else if (!is_idstart[*symname]
6066 || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
6067 error ("invalid %s name `%.*s'", usage, sym_length, symname);
6071 /* Return zero if two DEFINITIONs are isomorphic. */
6074 compare_defs (d1, d2)
6075 DEFINITION *d1, *d2;
6077 register struct reflist *a1, *a2;
6078 register U_CHAR *p1 = d1->expansion;
6079 register U_CHAR *p2 = d2->expansion;
6082 if (d1->nargs != d2->nargs)
6085 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
6087 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
6088 a1 = a1->next, a2 = a2->next) {
6089 if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
6090 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
6091 || a1->argno != a2->argno
6092 || a1->stringify != a2->stringify
6093 || a1->raw_before != a2->raw_before
6094 || a1->raw_after != a2->raw_after)
6102 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
6103 p2, d2->length - (p2 - d2->expansion), 1))
6108 /* Return 1 if two parts of two macro definitions are effectively different.
6109 One of the parts starts at BEG1 and has LEN1 chars;
6110 the other has LEN2 chars at BEG2.
6111 Any sequence of whitespace matches any other sequence of whitespace.
6112 FIRST means these parts are the first of a macro definition;
6113 so ignore leading whitespace entirely.
6114 LAST means these parts are the last of a macro definition;
6115 so ignore trailing whitespace entirely. */
6118 comp_def_part (first, beg1, len1, beg2, len2, last)
6120 U_CHAR *beg1, *beg2;
6124 register U_CHAR *end1 = beg1 + len1;
6125 register U_CHAR *end2 = beg2 + len2;
6127 while (beg1 != end1 && is_space[*beg1]) beg1++;
6128 while (beg2 != end2 && is_space[*beg2]) beg2++;
6131 while (beg1 != end1 && is_space[end1[-1]]) end1--;
6132 while (beg2 != end2 && is_space[end2[-1]]) end2--;
6134 while (beg1 != end1 && beg2 != end2) {
6135 if (is_space[*beg1] && is_space[*beg2]) {
6136 while (beg1 != end1 && is_space[*beg1]) beg1++;
6137 while (beg2 != end2 && is_space[*beg2]) beg2++;
6138 } else if (*beg1 == *beg2) {
6142 return (beg1 != end1) || (beg2 != end2);
6145 /* Read a replacement list for a macro with parameters.
6146 Build the DEFINITION structure.
6147 Reads characters of text starting at BUF until END.
6148 ARGLIST specifies the formal parameters to look for
6149 in the text of the definition; NARGS is the number of args
6150 in that list, or -1 for a macro name that wants no argument list.
6151 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6152 and NAMELEN is its length in characters.
6154 Note that comments, backslash-newlines, and leading white space
6155 have already been deleted from the argument. */
6157 /* If there is no trailing whitespace, a Newline Space is added at the end
6158 to prevent concatenation that would be contrary to the standard. */
6161 collect_expansion (buf, end, nargs, arglist)
6164 struct arglist *arglist;
6167 register U_CHAR *p, *limit, *lastp, *exp_p;
6168 struct reflist *endpat = NULL;
6169 /* Pointer to first nonspace after last ## seen. */
6171 /* Pointer to first nonspace after last single-# seen. */
6172 U_CHAR *stringify = 0;
6173 /* How those tokens were spelled. */
6174 enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
6175 enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
6177 int expected_delimiter = '\0';
6179 /* Scan thru the replacement list, ignoring comments and quoted
6180 strings, picking up on the macro calls. It does a linear search
6181 thru the arg list on every potential symbol. Profiling might say
6182 that something smarter should happen. */
6187 /* Find the beginning of the trailing whitespace. */
6190 while (p < limit && is_space[limit[-1]]) limit--;
6192 /* Allocate space for the text in the macro definition.
6193 Each input char may or may not need 1 byte,
6194 so this is an upper bound.
6195 The extra 3 are for invented trailing newline-marker and final null. */
6196 maxsize = (sizeof (DEFINITION)
6198 defn = (DEFINITION *) xcalloc (1, maxsize);
6200 defn->nargs = nargs;
6201 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
6206 : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
6207 error ("`##' at start of macro definition");
6208 p += p[0] == '#' ? 2 : 4;
6211 /* Process the main body of the definition. */
6213 int skipped_arg = 0;
6214 register U_CHAR c = *p++;
6222 if (expected_delimiter != '\0') {
6223 if (c == expected_delimiter)
6224 expected_delimiter = '\0';
6226 expected_delimiter = c;
6230 if (p < limit && expected_delimiter) {
6231 /* In a string, backslash goes through
6232 and makes next char ordinary. */
6238 if (!expected_delimiter && *p == ':') {
6239 /* %: is not a digraph if preceded by an odd number of '<'s. */
6241 while (buf < p0 && p0[-1] == '<')
6244 /* Treat %:%: as ## and %: as #. */
6245 if (p[1] == '%' && p[2] == ':') {
6247 goto sharp_sharp_token;
6258 /* # is ordinary inside a string. */
6259 if (expected_delimiter)
6263 /* ##: concatenate preceding and following tokens. */
6264 /* Take out the first #, discard preceding whitespace. */
6266 while (exp_p > lastp && is_hor_space[exp_p[-1]])
6268 /* Skip the second #. */
6270 concat_sharp_token_type = c;
6271 if (is_hor_space[*p]) {
6272 concat_sharp_token_type = c + 1;
6274 SKIP_WHITE_SPACE (p);
6278 error ("`##' at end of macro definition");
6279 } else if (nargs >= 0) {
6280 /* Single #: stringify following argument ref.
6281 Don't leave the # in the expansion. */
6284 stringify_sharp_token_type = c;
6285 if (is_hor_space[*p]) {
6286 stringify_sharp_token_type = c + 1;
6288 SKIP_WHITE_SPACE (p);
6290 if (! is_idstart[*p] || nargs == 0
6291 || (*p == 'L' && (p[1] == '\'' || p[1] == '"')))
6292 error ("`#' operator is not followed by a macro argument name");
6299 /* In -traditional mode, recognize arguments inside strings and
6300 character constants, and ignore special properties of #.
6301 Arguments inside strings are considered "stringified", but no
6302 extra quote marks are supplied. */
6306 if (expected_delimiter != '\0') {
6307 if (c == expected_delimiter)
6308 expected_delimiter = '\0';
6310 expected_delimiter = c;
6314 /* Backslash quotes delimiters and itself, but not macro args. */
6315 if (expected_delimiter != 0 && p < limit
6316 && (*p == expected_delimiter || *p == '\\')) {
6323 if (expected_delimiter != '\0') /* No comments inside strings. */
6326 /* If we find a comment that wasn't removed by handle_directive,
6327 this must be -traditional. So replace the comment with
6330 while (++p < limit) {
6331 if (p[0] == '*' && p[1] == '/') {
6337 /* Mark this as a concatenation-point, as if it had been ##. */
6345 #ifdef MULTIBYTE_CHARS
6346 /* Handle multibyte characters inside string and character literals. */
6347 if (expected_delimiter != '\0')
6351 length = local_mblen (p, limit - p);
6355 bcopy (p, exp_p, length);
6364 /* Handle the start of a symbol. */
6365 if (is_idchar[c] && nargs > 0) {
6366 U_CHAR *id_beg = p - 1;
6370 while (p != limit && is_idchar[*p]) p++;
6371 id_len = p - id_beg;
6374 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
6375 register struct arglist *arg;
6377 for (arg = arglist; arg != NULL; arg = arg->next) {
6378 struct reflist *tpat;
6380 if (arg->name[0] == c
6381 && arg->length == id_len
6382 && bcmp (arg->name, id_beg, id_len) == 0) {
6383 enum sharp_token_type tpat_stringify;
6384 if (expected_delimiter) {
6385 if (warn_stringify) {
6387 warning ("macro argument `%.*s' is stringified.",
6390 warning ("macro arg `%.*s' would be stringified with -traditional.",
6394 /* If ANSI, don't actually substitute inside a string. */
6397 tpat_stringify = SHARP_TOKEN;
6400 = (stringify == id_beg
6401 ? stringify_sharp_token_type : NO_SHARP_TOKEN);
6403 /* make a pat node for this arg and append it to the end of
6405 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
6408 = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
6409 tpat->raw_after = NO_SHARP_TOKEN;
6410 tpat->rest_args = arg->rest_args;
6411 tpat->stringify = tpat_stringify;
6414 defn->pattern = tpat;
6416 endpat->next = tpat;
6419 tpat->argno = arg->argno;
6420 tpat->nchars = exp_p - lastp;
6422 register U_CHAR *p1 = p;
6423 SKIP_WHITE_SPACE (p1);
6426 : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
6427 tpat->raw_after = p1[0] + (p != p1);
6429 lastp = exp_p; /* place to start copying from next time */
6436 /* If this was not a macro arg, copy it into the expansion. */
6437 if (! skipped_arg) {
6438 register U_CHAR *lim1 = p;
6442 if (stringify == id_beg)
6443 error ("`#' operator should be followed by a macro argument name");
6448 if (!traditional && expected_delimiter == 0) {
6449 /* If ANSI, put in a newline-space marker to prevent token pasting.
6450 But not if "inside a string" (which in ANSI mode happens only for
6458 defn->length = exp_p - defn->expansion;
6460 /* Crash now if we overrun the allocated size. */
6461 if (defn->length + 1 > maxsize)
6465 /* This isn't worth the time it takes. */
6466 /* give back excess storage */
6467 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
6474 do_assert (buf, limit, op, keyword)
6475 U_CHAR *buf, *limit;
6476 FILE_BUF *op ATTRIBUTE_UNUSED;
6477 struct directive *keyword ATTRIBUTE_UNUSED;
6479 U_CHAR *bp; /* temp ptr into input buffer */
6480 U_CHAR *symname; /* remember where symbol name starts */
6481 int sym_length; /* and how long it is */
6482 struct arglist *tokens = NULL;
6484 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6485 pedwarn ("ANSI C does not allow `#assert'");
6489 while (is_hor_space[*bp])
6492 symname = bp; /* remember where it starts */
6493 sym_length = check_macro_name (bp, "assertion");
6495 /* #define doesn't do this, but we should. */
6496 SKIP_WHITE_SPACE (bp);
6498 /* Lossage will occur if identifiers or control tokens are broken
6499 across lines using backslash. This is not the right place to take
6503 error ("missing token-sequence in `#assert'");
6510 bp++; /* skip '(' */
6511 SKIP_WHITE_SPACE (bp);
6513 tokens = read_token_list (&bp, limit, &error_flag);
6517 error ("empty token-sequence in `#assert'");
6521 ++bp; /* skip paren */
6522 SKIP_WHITE_SPACE (bp);
6525 /* If this name isn't already an assertion name, make it one.
6526 Error if it was already in use in some other way. */
6529 ASSERTION_HASHNODE *hp;
6530 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6531 struct tokenlist_list *value
6532 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6534 hp = assertion_lookup (symname, sym_length, hashcode);
6536 if (sym_length == 7 && ! bcmp (symname, "defined", 7))
6537 error ("`defined' redefined as assertion");
6538 hp = assertion_install (symname, sym_length, hashcode);
6541 /* Add the spec'd token-sequence to the list of such. */
6542 value->tokens = tokens;
6543 value->next = hp->value;
6551 do_unassert (buf, limit, op, keyword)
6552 U_CHAR *buf, *limit;
6553 FILE_BUF *op ATTRIBUTE_UNUSED;
6554 struct directive *keyword ATTRIBUTE_UNUSED;
6556 U_CHAR *bp; /* temp ptr into input buffer */
6557 U_CHAR *symname; /* remember where symbol name starts */
6558 int sym_length; /* and how long it is */
6560 struct arglist *tokens = NULL;
6561 int tokens_specified = 0;
6563 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6564 pedwarn ("ANSI C does not allow `#unassert'");
6568 while (is_hor_space[*bp])
6571 symname = bp; /* remember where it starts */
6572 sym_length = check_macro_name (bp, "assertion");
6574 /* #define doesn't do this, but we should. */
6575 SKIP_WHITE_SPACE (bp);
6577 /* Lossage will occur if identifiers or control tokens are broken
6578 across lines using backslash. This is not the right place to take
6584 bp++; /* skip '(' */
6585 SKIP_WHITE_SPACE (bp);
6587 tokens = read_token_list (&bp, limit, &error_flag);
6591 error ("empty token list in `#unassert'");
6595 tokens_specified = 1;
6597 ++bp; /* skip paren */
6598 SKIP_WHITE_SPACE (bp);
6602 ASSERTION_HASHNODE *hp;
6603 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6604 struct tokenlist_list *tail, *prev;
6606 hp = assertion_lookup (symname, sym_length, hashcode);
6610 /* If no token list was specified, then eliminate this assertion
6612 if (! tokens_specified) {
6613 struct tokenlist_list *next;
6614 for (tail = hp->value; tail; tail = next) {
6616 free_token_list (tail->tokens);
6619 delete_assertion (hp);
6621 /* If a list of tokens was given, then delete any matching list. */
6626 struct tokenlist_list *next = tail->next;
6627 if (compare_token_lists (tail->tokens, tokens)) {
6631 hp->value = tail->next;
6632 free_token_list (tail->tokens);
6645 /* Test whether there is an assertion named NAME
6646 and optionally whether it has an asserted token list TOKENS.
6647 NAME is not null terminated; its length is SYM_LENGTH.
6648 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6651 check_assertion (name, sym_length, tokens_specified, tokens)
6654 int tokens_specified;
6655 struct arglist *tokens;
6657 ASSERTION_HASHNODE *hp;
6658 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6660 if (pedantic && !instack[indepth].system_header_p)
6661 pedwarn ("ANSI C does not allow testing assertions");
6663 hp = assertion_lookup (name, sym_length, hashcode);
6665 /* It is not an assertion; just return false. */
6668 /* If no token list was specified, then value is 1. */
6669 if (! tokens_specified)
6673 struct tokenlist_list *tail;
6677 /* If a list of tokens was given,
6678 then succeed if the assertion records a matching list. */
6681 if (compare_token_lists (tail->tokens, tokens))
6686 /* Fail if the assertion has no matching list. */
6691 /* Compare two lists of tokens for equality including order of tokens. */
6694 compare_token_lists (l1, l2)
6695 struct arglist *l1, *l2;
6698 if (l1->length != l2->length)
6700 if (bcmp (l1->name, l2->name, l1->length))
6706 /* Succeed if both lists end at the same time. */
6710 /* Read a space-separated list of tokens ending in a close parenthesis.
6711 Return a list of strings, in the order they were written.
6712 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6713 Parse the text starting at *BPP, and update *BPP.
6714 Don't parse beyond LIMIT. */
6716 static struct arglist *
6717 read_token_list (bpp, limit, error_flag)
6722 struct arglist *token_ptrs = 0;
6728 /* Loop over the assertion value tokens. */
6730 struct arglist *temp;
6734 /* Find the end of the token. */
6738 } else if (*bp == ')') {
6743 } else if (*bp == '"' || *bp == '\'')
6744 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6746 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
6747 && *bp != '"' && *bp != '\'' && bp != limit)
6750 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
6751 temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
6752 bcopy ((char *) beg, (char *) temp->name, bp - beg);
6753 temp->name[bp - beg] = 0;
6754 temp->next = token_ptrs;
6756 temp->length = bp - beg;
6758 SKIP_WHITE_SPACE (bp);
6761 error ("unterminated token sequence in `#assert' or `#unassert'");
6768 /* We accumulated the names in reverse order.
6769 Now reverse them to get the proper order. */
6771 register struct arglist *prev = 0, *this, *next;
6772 for (this = token_ptrs; this; this = next) {
6782 free_token_list (tokens)
6783 struct arglist *tokens;
6786 struct arglist *next = tokens->next;
6787 free (tokens->name);
6793 /* Install a name in the assertion hash table.
6795 If LEN is >= 0, it is the length of the name.
6796 Otherwise, compute the length by scanning the entire name.
6798 If HASH is >= 0, it is the precomputed hash code.
6799 Otherwise, compute the hash code. */
6801 static ASSERTION_HASHNODE *
6802 assertion_install (name, len, hash)
6807 register ASSERTION_HASHNODE *hp;
6808 register int i, bucket;
6809 register U_CHAR *p, *q;
6811 i = sizeof (ASSERTION_HASHNODE) + len + 1;
6812 hp = (ASSERTION_HASHNODE *) xmalloc (i);
6814 hp->bucket_hdr = &assertion_hashtab[bucket];
6815 hp->next = assertion_hashtab[bucket];
6816 assertion_hashtab[bucket] = hp;
6818 if (hp->next != NULL)
6819 hp->next->prev = hp;
6822 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
6825 for (i = 0; i < len; i++)
6831 /* Find the most recent hash node for name "name" (ending with first
6832 non-identifier char) installed by install
6834 If LEN is >= 0, it is the length of the name.
6835 Otherwise, compute the length by scanning the entire name.
6837 If HASH is >= 0, it is the precomputed hash code.
6838 Otherwise, compute the hash code. */
6840 static ASSERTION_HASHNODE *
6841 assertion_lookup (name, len, hash)
6846 register ASSERTION_HASHNODE *bucket;
6848 bucket = assertion_hashtab[hash];
6850 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
6852 bucket = bucket->next;
6858 delete_assertion (hp)
6859 ASSERTION_HASHNODE *hp;
6862 if (hp->prev != NULL)
6863 hp->prev->next = hp->next;
6864 if (hp->next != NULL)
6865 hp->next->prev = hp->prev;
6867 /* Make sure that the bucket chain header that the deleted guy was
6868 on points to the right thing afterwards. */
6869 if (hp == *hp->bucket_hdr)
6870 *hp->bucket_hdr = hp->next;
6876 * interpret #line directive. Remembers previously seen fnames
6877 * in its very own hash table.
6879 #define FNAME_HASHSIZE 37
6882 do_line (buf, limit, op, keyword)
6883 U_CHAR *buf, *limit;
6885 struct directive *keyword ATTRIBUTE_UNUSED;
6887 register U_CHAR *bp;
6888 FILE_BUF *ip = &instack[indepth];
6891 enum file_change_code file_change = same_file;
6893 /* Expand any macros. */
6894 tem = expand_to_temp_buffer (buf, limit, 0, 0);
6896 /* Point to macroexpanded line, which is null-terminated now. */
6898 SKIP_WHITE_SPACE (bp);
6900 if (!ISDIGIT (*bp)) {
6901 error ("invalid format `#line' directive");
6905 /* The Newline at the end of this line remains to be processed.
6906 To put the next line at the specified line number,
6907 we must store a line number now that is one less. */
6908 new_lineno = atoi ((char *) bp) - 1;
6910 /* NEW_LINENO is one less than the actual line number here. */
6911 if (pedantic && new_lineno < 0)
6912 pedwarn ("line number out of range in `#line' directive");
6914 /* skip over the line number. */
6915 while (ISDIGIT (*bp))
6918 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6919 if (*bp && !is_space[*bp]) {
6920 error ("invalid format `#line' directive");
6925 SKIP_WHITE_SPACE (bp);
6928 static HASHNODE *fname_table[FNAME_HASHSIZE];
6929 HASHNODE *hp, **hash_bucket;
6935 /* Turn the file name, which is a character string literal,
6936 into a null-terminated string. Do this in place. */
6939 switch ((*p++ = *bp++)) {
6941 error ("invalid format `#line' directive");
6945 if (! ignore_escape_flag)
6947 char *bpc = (char *) bp;
6948 HOST_WIDE_INT c = parse_escape (&bpc, (HOST_WIDE_INT) (U_CHAR) (-1));
6949 bp = (U_CHAR *) bpc;
6962 fname_length = p - fname;
6964 SKIP_WHITE_SPACE (bp);
6967 pedwarn ("garbage at end of `#line' directive");
6969 file_change = enter_file;
6970 else if (*bp == '2')
6971 file_change = leave_file;
6972 else if (*bp == '3')
6973 ip->system_header_p = 1;
6974 else if (*bp == '4')
6975 ip->system_header_p = 2;
6977 error ("invalid format `#line' directive");
6982 SKIP_WHITE_SPACE (bp);
6984 ip->system_header_p = 1;
6986 SKIP_WHITE_SPACE (bp);
6989 ip->system_header_p = 2;
6991 SKIP_WHITE_SPACE (bp);
6994 error ("invalid format `#line' directive");
6999 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
7000 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
7001 if (hp->length == fname_length &&
7002 bcmp (hp->value.cpval, fname, fname_length) == 0) {
7003 ip->nominal_fname = hp->value.cpval;
7004 ip->nominal_fname_len = fname_length;
7008 /* Didn't find it; cons up a new one. */
7009 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
7010 hp->next = *hash_bucket;
7013 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
7014 ip->nominal_fname_len = hp->length = fname_length;
7015 bcopy (fname, hp->value.cpval, fname_length + 1);
7018 error ("invalid format `#line' directive");
7022 ip->lineno = new_lineno;
7023 output_line_directive (ip, op, 0, file_change);
7024 check_expand (op, ip->length - (ip->bufp - ip->buf));
7028 /* Remove the definition of a symbol from the symbol table.
7029 according to un*x /lib/cpp, it is not an error to undef
7030 something that has no definitions, so it isn't one here either. */
7033 do_undef (buf, limit, op, keyword)
7034 U_CHAR *buf, *limit;
7036 struct directive *keyword;
7040 U_CHAR *orig_buf = buf;
7042 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7043 if (pcp_outfile && op)
7044 pass_thru_directive (buf, limit, op, keyword);
7046 SKIP_WHITE_SPACE (buf);
7047 sym_length = check_macro_name (buf, "macro");
7049 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
7050 /* If we are generating additional info for debugging (with -g) we
7051 need to pass through all effective #undef directives. */
7052 if (debug_output && op)
7053 pass_thru_directive (orig_buf, limit, op, keyword);
7054 if (hp->type != T_MACRO)
7055 warning ("undefining `%s'", hp->name);
7061 SKIP_WHITE_SPACE (buf);
7063 pedwarn ("garbage after `#undef' directive");
7068 /* Report an error detected by the program we are processing.
7069 Use the text of the line in the error message.
7070 (We use error because it prints the filename & line#.) */
7073 do_error (buf, limit, op, keyword)
7074 U_CHAR *buf, *limit;
7075 FILE_BUF *op ATTRIBUTE_UNUSED;
7076 struct directive *keyword ATTRIBUTE_UNUSED;
7078 int length = limit - buf;
7079 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
7080 bcopy ((char *) buf, (char *) copy, length);
7082 SKIP_WHITE_SPACE (copy);
7083 error ("#error %s", copy);
7087 /* Report a warning detected by the program we are processing.
7088 Use the text of the line in the warning message, then continue.
7089 (We use error because it prints the filename & line#.) */
7092 do_warning (buf, limit, op, keyword)
7093 U_CHAR *buf, *limit;
7094 FILE_BUF *op ATTRIBUTE_UNUSED;
7095 struct directive *keyword ATTRIBUTE_UNUSED;
7097 int length = limit - buf;
7098 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
7099 bcopy ((char *) buf, (char *) copy, length);
7101 SKIP_WHITE_SPACE (copy);
7103 if (pedantic && !instack[indepth].system_header_p)
7104 pedwarn ("ANSI C does not allow `#warning'");
7106 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
7107 if -pedantic-errors is given, #warning should cause an error. */
7108 pedwarn ("#warning %s", copy);
7112 /* Remember the name of the current file being read from so that we can
7113 avoid ever including it again. */
7120 for (i = indepth; i >= 0; i--)
7121 if (instack[i].inc) {
7122 record_control_macro (instack[i].inc, (U_CHAR *) "");
7127 /* Report program identification. */
7130 do_ident (buf, limit, op, keyword)
7131 U_CHAR *buf, *limit;
7133 struct directive *keyword ATTRIBUTE_UNUSED;
7138 /* Allow #ident in system headers, since that's not user's fault. */
7139 if (pedantic && !instack[indepth].system_header_p)
7140 pedwarn ("ANSI C does not allow `#ident'");
7142 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
7144 len = trybuf.bufp - buf;
7146 /* Output expanded directive. */
7147 check_expand (op, 7 + len);
7148 bcopy ("#ident ", (char *) op->bufp, 7);
7150 bcopy ((char *) buf, (char *) op->bufp, len);
7157 /* #pragma and its argument line have already been copied to the output file.
7158 Just check for some recognized pragmas that need validation here. */
7161 do_pragma (buf, limit, op, keyword)
7162 U_CHAR *buf, *limit ATTRIBUTE_UNUSED;
7163 FILE_BUF *op ATTRIBUTE_UNUSED;
7164 struct directive *keyword ATTRIBUTE_UNUSED;
7166 SKIP_WHITE_SPACE (buf);
7167 if (!strncmp ((char *) buf, "once", 4)) {
7168 /* Allow #pragma once in system headers, since that's not the user's
7170 if (!instack[indepth].system_header_p)
7171 warning ("`#pragma once' is obsolete");
7175 if (!strncmp ((char *) buf, "implementation", 14)) {
7176 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7177 been included yet. */
7180 U_CHAR *p = buf + 14, *fname;
7181 SKIP_WHITE_SPACE (p);
7186 if ((p = (U_CHAR *) index ((char *) fname, '\"')))
7189 for (h = 0; h < INCLUDE_HASHSIZE; h++) {
7190 struct include_file *inc;
7191 for (inc = include_hashtab[h]; inc; inc = inc->next) {
7192 if (!strcmp (base_name (inc->fname), (char *) fname)) {
7193 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname);
7203 /* This was a fun hack, but #pragma seems to start to be useful.
7204 By failing to recognize it, we pass it through unchanged to cc1. */
7206 /* The behavior of the #pragma directive is implementation defined.
7207 this implementation defines it as follows. */
7213 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
7216 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
7218 execl ("/usr/games/hack", "#pragma", 0);
7219 execl ("/usr/games/rogue", "#pragma", 0);
7220 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7221 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7223 fatal ("You are in a maze of twisty compiler features, all different");
7227 #ifdef SCCS_DIRECTIVE
7229 /* Just ignore #sccs, on systems where we define it at all. */
7232 do_sccs (buf, limit, op, keyword)
7233 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
7234 FILE_BUF *op ATTRIBUTE_UNUSED;
7235 struct directive *keyword ATTRIBUTE_UNUSED;
7238 pedwarn ("ANSI C does not allow `#sccs'");
7242 #endif /* defined (SCCS_DIRECTIVE) */
7244 /* Handle #if directive by
7245 1) inserting special `defined' keyword into the hash table
7246 that gets turned into 0 or 1 by special_symbol (thus,
7247 if the luser has a symbol called `defined' already, it won't
7248 work inside the #if directive)
7249 2) rescan the input into a temporary output buffer
7250 3) pass the output buffer to the yacc parser and collect a value
7251 4) clean up the mess left from steps 1 and 2.
7252 5) call conditional_skip to skip til the next #endif (etc.),
7253 or not, depending on the value from step 3. */
7256 do_if (buf, limit, op, keyword)
7257 U_CHAR *buf, *limit;
7259 struct directive *keyword ATTRIBUTE_UNUSED;
7261 HOST_WIDE_INT value;
7262 FILE_BUF *ip = &instack[indepth];
7264 value = eval_if_expression (buf, limit - buf);
7265 conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
7269 /* Handle a #elif directive by not changing if_stack either.
7270 see the comment above do_else. */
7273 do_elif (buf, limit, op, keyword)
7274 U_CHAR *buf, *limit;
7276 struct directive *keyword ATTRIBUTE_UNUSED;
7278 HOST_WIDE_INT value;
7279 FILE_BUF *ip = &instack[indepth];
7281 if (if_stack == instack[indepth].if_stack) {
7282 error ("`#elif' not within a conditional");
7285 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7286 error ("`#elif' after `#else'");
7287 fprintf (stderr, " (matches line %d", if_stack->lineno);
7288 if (! (if_stack->fname_len == ip->nominal_fname_len
7289 && !bcmp (if_stack->fname, ip->nominal_fname,
7290 if_stack->fname_len))) {
7291 fprintf (stderr, ", file ");
7292 eprint_string (if_stack->fname, if_stack->fname_len);
7294 fprintf (stderr, ")\n");
7296 if_stack->type = T_ELIF;
7299 if (if_stack->if_succeeded)
7300 skip_if_group (ip, 0, op);
7302 value = eval_if_expression (buf, limit - buf);
7304 skip_if_group (ip, 0, op);
7306 ++if_stack->if_succeeded; /* continue processing input */
7307 output_line_directive (ip, op, 1, same_file);
7313 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7314 result as a C expression and return the value as an int. */
7316 static HOST_WIDE_INT
7317 eval_if_expression (buf, length)
7322 HASHNODE *save_defined;
7323 HOST_WIDE_INT value;
7325 save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED,
7328 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
7330 delete_macro (save_defined); /* clean up special symbol */
7332 temp_obuf.buf[temp_obuf.length] = '\n';
7333 value = parse_c_expression ((char *) temp_obuf.buf,
7334 warn_undef && !instack[indepth].system_header_p);
7336 free (temp_obuf.buf);
7341 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7342 or don't skip to the #endif/#else/#elif depending on what directive
7343 is actually being processed. */
7346 do_xifdef (buf, limit, op, keyword)
7347 U_CHAR *buf, *limit;
7349 struct directive *keyword;
7352 FILE_BUF *ip = &instack[indepth];
7354 int start_of_file = 0;
7355 U_CHAR *control_macro = 0;
7357 /* Detect a #ifndef at start of file (not counting comments). */
7358 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
7359 U_CHAR *p = ip->buf;
7360 while (p != directive_start) {
7364 /* Make no special provision for backslash-newline here; this is
7365 slower if backslash-newlines are present, but it's correct,
7366 and it's not worth it to tune for the rare backslash-newline. */
7368 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7369 /* Skip this comment. */
7371 U_CHAR *save_bufp = ip->bufp;
7373 p = skip_to_end_of_comment (ip, &junk, 1);
7374 ip->bufp = save_bufp;
7379 /* If we get here, this conditional is the beginning of the file. */
7384 /* Discard leading and trailing whitespace. */
7385 SKIP_WHITE_SPACE (buf);
7386 while (limit != buf && is_hor_space[limit[-1]]) limit--;
7388 /* Find the end of the identifier at the beginning. */
7389 for (end = buf; is_idchar[*end]; end++);
7392 skip = (keyword->type == T_IFDEF);
7394 pedwarn (end == limit ? "`#%s' with no argument"
7395 : "`#%s' argument starts with punctuation",
7400 if (! traditional) {
7401 if (ISDIGIT (buf[0]))
7402 pedwarn ("`#%s' argument starts with a digit", keyword->name);
7403 else if (end != limit)
7404 pedwarn ("garbage at end of `#%s' argument", keyword->name);
7407 hp = lookup (buf, end-buf, -1);
7410 /* Output a precondition for this macro. */
7412 && (hp->type == T_CONST
7413 || (hp->type == T_MACRO && hp->value.defn->predefined)))
7414 fprintf (pcp_outfile, "#define %s\n", hp->name);
7417 fprintf (pcp_outfile, "#undef ");
7418 while (is_idchar[*cp]) /* Ick! */
7419 fputc (*cp++, pcp_outfile);
7420 putc ('\n', pcp_outfile);
7424 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
7425 if (start_of_file && !skip) {
7426 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
7427 bcopy ((char *) buf, (char *) control_macro, end - buf);
7428 control_macro[end - buf] = 0;
7432 conditional_skip (ip, skip, T_IF, control_macro, op);
7436 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7437 If this is a #ifndef starting at the beginning of a file,
7438 CONTROL_MACRO is the macro name tested by the #ifndef.
7439 Otherwise, CONTROL_MACRO is 0. */
7442 conditional_skip (ip, skip, type, control_macro, op)
7445 enum node_type type;
7446 U_CHAR *control_macro;
7449 IF_STACK_FRAME *temp;
7451 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7452 temp->fname = ip->nominal_fname;
7453 temp->fname_len = ip->nominal_fname_len;
7454 temp->lineno = ip->lineno;
7455 temp->next = if_stack;
7456 temp->control_macro = control_macro;
7459 if_stack->type = type;
7462 skip_if_group (ip, 0, op);
7465 ++if_stack->if_succeeded;
7466 output_line_directive (ip, &outbuf, 1, same_file);
7470 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7471 Leaves input ptr at the sharp sign found.
7472 If ANY is nonzero, return at next directive of any sort. */
7475 skip_if_group (ip, any, op)
7480 register U_CHAR *bp = ip->bufp, *cp;
7481 register U_CHAR *endb = ip->buf + ip->length;
7482 struct directive *kt;
7483 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
7484 U_CHAR *beg_of_line = bp;
7485 register int ident_length;
7486 U_CHAR *ident, *after_ident;
7487 /* Save info about where the group starts. */
7488 U_CHAR *beg_of_group = bp;
7489 int beg_lineno = ip->lineno;
7490 int skipping_include_directive = 0;
7492 if (output_conditionals && op != 0) {
7493 char *ptr = "#failed\n";
7494 int len = strlen (ptr);
7496 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7501 check_expand (op, len);
7502 bcopy (ptr, (char *) op->bufp, len);
7505 output_line_directive (ip, op, 1, 0);
7510 case '/': /* possible comment */
7511 if (*bp == '\\' && bp[1] == '\n')
7514 || (cplusplus_comments && *bp == '/')) {
7516 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
7520 if (skipping_include_directive) {
7521 while (bp < endb && *bp != '>' && *bp != '\n') {
7522 if (*bp == '\\' && bp[1] == '\n') {
7531 if (skipping_include_directive) {
7532 while (bp < endb && *bp != '\n') {
7537 if (*bp == '\\' && bp[1] == '\n') {
7547 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
7548 NULL_PTR, NULL_PTR);
7551 /* Char after backslash loses its special meaning in some cases. */
7555 } else if (traditional && bp < endb)
7561 skipping_include_directive = 0;
7564 if (beg_of_line == 0 || traditional)
7567 while (bp[0] == '\\' && bp[1] == '\n')
7573 /* # keyword: a # must be first nonblank char on the line */
7574 if (beg_of_line == 0)
7578 /* Scan from start of line, skipping whitespace, comments
7579 and backslash-newlines, and see if we reach this #.
7580 If not, this # is not special. */
7582 /* If -traditional, require # to be at beginning of line. */
7585 if (is_hor_space[*bp])
7587 else if (*bp == '\\' && bp[1] == '\n')
7589 else if (*bp == '/' && bp[1] == '*') {
7603 #ifdef MULTIBYTE_CHARS
7605 length = local_mblen (bp, endb - bp);
7613 /* There is no point in trying to deal with C++ // comments here,
7614 because if there is one, then this # must be part of the
7615 comment and we would never reach here. */
7619 if (bp != ip->bufp) {
7620 bp = ip->bufp + 1; /* Reset bp to after the #. */
7624 bp = ip->bufp + 1; /* Point after the '#' */
7625 if (ip->bufp[0] == '%') {
7626 /* Skip past the ':' again. */
7627 while (*bp == '\\') {
7634 /* Skip whitespace and \-newline. */
7636 if (is_hor_space[*bp])
7638 else if (*bp == '\\' && bp[1] == '\n')
7640 else if (*bp == '/') {
7641 if (bp[1] == '\\' && bp[2] == '\n')
7642 newline_fix (bp + 1);
7644 for (bp += 2; ; bp++) {
7647 else if (*bp == '*') {
7648 if (bp[-1] == '/' && warn_comments)
7649 warning ("`/*' within comment");
7650 if (bp[1] == '\\' && bp[2] == '\n')
7651 newline_fix (bp + 1);
7657 #ifdef MULTIBYTE_CHARS
7659 length = local_mblen (bp, endb - bp);
7666 } else if (bp[1] == '/' && cplusplus_comments) {
7667 for (bp += 2; ; bp++) {
7670 if (*bp == '\\' && bp[1] == '\n')
7673 warning ("multiline `//' comment");
7679 #ifdef MULTIBYTE_CHARS
7681 length = local_mblen (bp, endb - bp);
7695 /* Now find end of directive name.
7696 If we encounter a backslash-newline, exchange it with any following
7697 symbol-constituents so that we end up with a contiguous name. */
7703 if (*bp == '\\' && bp[1] == '\n')
7704 name_newline_fix (bp);
7710 ident_length = bp - cp;
7714 /* A line of just `#' becomes blank. */
7716 if (ident_length == 0 && *after_ident == '\n') {
7720 if (ident_length == 0 || !is_idstart[*ident]) {
7722 while (is_idchar[*p]) {
7723 if (*p < '0' || *p > '9')
7727 /* Handle # followed by a line number. */
7728 if (p != ident && !is_idchar[*p]) {
7730 pedwarn ("`#' followed by integer");
7734 /* Avoid error for `###' and similar cases unless -pedantic. */
7736 while (*p == '#' || is_hor_space[*p]) p++;
7738 if (pedantic && !lang_asm)
7739 pedwarn ("invalid preprocessing directive");
7744 if (!lang_asm && pedantic)
7745 pedwarn ("invalid preprocessing directive name");
7749 for (kt = directive_table; kt->length >= 0; kt++) {
7750 IF_STACK_FRAME *temp;
7751 if (ident_length == kt->length
7752 && bcmp (cp, kt->name, kt->length) == 0) {
7753 /* If we are asked to return on next directive, do so now. */
7761 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7762 temp->next = if_stack;
7764 temp->lineno = ip->lineno;
7765 temp->fname = ip->nominal_fname;
7766 temp->fname_len = ip->nominal_fname_len;
7767 temp->type = kt->type;
7771 if (pedantic && if_stack != save_if_stack)
7772 validate_else (bp, endb);
7774 if (if_stack == instack[indepth].if_stack) {
7775 error ("`#%s' not within a conditional", kt->name);
7778 else if (if_stack == save_if_stack)
7779 goto done; /* found what we came for */
7781 if (kt->type != T_ENDIF) {
7782 if (if_stack->type == T_ELSE)
7783 error ("`#else' or `#elif' after `#else'");
7784 if_stack->type = kt->type;
7789 if_stack = if_stack->next;
7794 case T_INCLUDE_NEXT:
7796 skipping_include_directive = 1;
7805 /* Don't let erroneous code go by. */
7806 if (kt->length < 0 && !lang_asm && pedantic)
7807 pedwarn ("invalid preprocessing directive name");
7812 /* after this returns, rescan will exit because ip->bufp
7813 now points to the end of the buffer.
7814 rescan is responsible for the error message also. */
7817 if (output_conditionals && op != 0) {
7818 char *ptr = "#endfailed\n";
7819 int len = strlen (ptr);
7821 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7826 check_expand (op, beg_of_line - beg_of_group);
7827 bcopy ((char *) beg_of_group, (char *) op->bufp,
7828 beg_of_line - beg_of_group);
7829 op->bufp += beg_of_line - beg_of_group;
7830 op->lineno += ip->lineno - beg_lineno;
7831 check_expand (op, len);
7832 bcopy (ptr, (char *) op->bufp, len);
7838 /* Handle a #else directive. Do this by just continuing processing
7839 without changing if_stack ; this is so that the error message
7840 for missing #endif's etc. will point to the original #if. It
7841 is possible that something different would be better. */
7844 do_else (buf, limit, op, keyword)
7845 U_CHAR *buf, *limit;
7847 struct directive *keyword ATTRIBUTE_UNUSED;
7849 FILE_BUF *ip = &instack[indepth];
7852 SKIP_WHITE_SPACE (buf);
7854 pedwarn ("text following `#else' violates ANSI standard");
7857 if (if_stack == instack[indepth].if_stack) {
7858 error ("`#else' not within a conditional");
7861 /* #ifndef can't have its special treatment for containing the whole file
7862 if it has a #else clause. */
7863 if_stack->control_macro = 0;
7865 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7866 error ("`#else' after `#else'");
7867 fprintf (stderr, " (matches line %d", if_stack->lineno);
7868 if (! (if_stack->fname_len == ip->nominal_fname_len
7869 && !bcmp (if_stack->fname, ip->nominal_fname,
7870 if_stack->fname_len))) {
7871 fprintf (stderr, ", file ");
7872 eprint_string (if_stack->fname, if_stack->fname_len);
7874 fprintf (stderr, ")\n");
7876 if_stack->type = T_ELSE;
7879 if (if_stack->if_succeeded)
7880 skip_if_group (ip, 0, op);
7882 ++if_stack->if_succeeded; /* continue processing input */
7883 output_line_directive (ip, op, 1, same_file);
7888 /* Unstack after #endif directive. */
7891 do_endif (buf, limit, op, keyword)
7892 U_CHAR *buf, *limit;
7894 struct directive *keyword ATTRIBUTE_UNUSED;
7897 SKIP_WHITE_SPACE (buf);
7899 pedwarn ("text following `#endif' violates ANSI standard");
7902 if (if_stack == instack[indepth].if_stack)
7903 error ("unbalanced `#endif'");
7905 IF_STACK_FRAME *temp = if_stack;
7906 if_stack = if_stack->next;
7907 if (temp->control_macro != 0) {
7908 /* This #endif matched a #ifndef at the start of the file.
7909 See if it is at the end of the file. */
7910 FILE_BUF *ip = &instack[indepth];
7911 U_CHAR *p = ip->bufp;
7912 U_CHAR *ep = ip->buf + ip->length;
7918 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7919 /* Skip this comment. */
7921 U_CHAR *save_bufp = ip->bufp;
7923 p = skip_to_end_of_comment (ip, &junk, 1);
7924 ip->bufp = save_bufp;
7929 /* If we get here, this #endif ends a #ifndef
7930 that contains all of the file (aside from whitespace).
7931 Arrange not to include the file again
7932 if the macro that was tested is defined.
7934 Do not do this for the top-level file in a -include or any
7935 file in a -imacros. */
7937 && ! (indepth == 1 && no_record_file)
7938 && ! (no_record_file && no_output))
7939 record_control_macro (ip->inc, temp->control_macro);
7943 output_line_directive (&instack[indepth], op, 1, same_file);
7948 /* When an #else or #endif is found while skipping failed conditional,
7949 if -pedantic was specified, this is called to warn about text after
7950 the directive name. P points to the first char after the directive
7954 validate_else (p, limit)
7956 register U_CHAR *limit;
7958 /* Advance P over whitespace and comments. */
7960 while (*p == '\\' && p[1] == '\n')
7962 if (is_hor_space[*p])
7964 else if (*p == '/') {
7965 while (p[1] == '\\' && p[2] == '\n')
7968 /* Don't bother warning about unterminated comments
7969 since that will happen later. Just be sure to exit. */
7970 for (p += 2; ; p++) {
7974 while (p[1] == '\\' && p[2] == '\n')
7983 #ifdef MULTIBYTE_CHARS
7985 length = local_mblen (p, limit - p);
7992 else if (cplusplus_comments && p[1] == '/')
7998 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8001 /* Skip a comment, assuming the input ptr immediately follows the
8002 initial slash-star. Bump *LINE_COUNTER for each newline.
8003 (The canonical line counter is &ip->lineno.)
8004 Don't use this routine (or the next one) if bumping the line
8005 counter is not sufficient to deal with newlines in the string.
8007 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8008 This feature is useful when processing a comment that is going to
8009 be processed or was processed at another point in the preprocessor,
8010 to avoid a duplicate warning. Likewise for unterminated comment
8014 skip_to_end_of_comment (ip, line_counter, nowarn)
8015 register FILE_BUF *ip;
8016 int *line_counter; /* place to remember newlines, or NULL */
8019 register U_CHAR *limit = ip->buf + ip->length;
8020 register U_CHAR *bp = ip->bufp;
8021 FILE_BUF *op = put_out_comments && !line_counter ? &outbuf : (FILE_BUF *) 0;
8022 int start_line = line_counter ? *line_counter : 0;
8024 /* JF this line_counter stuff is a crock to make sure the
8025 comment is only put out once, no matter how many times
8026 the comment is skipped. It almost works */
8029 *op->bufp++ = bp[-1];
8031 if (cplusplus_comments && bp[-1] == '/') {
8032 for (; bp < limit; bp++) {
8035 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
8037 if (!nowarn && warn_comments)
8038 warning ("multiline `//' comment");
8050 #ifdef MULTIBYTE_CHARS
8052 length = local_mblen (bp, limit - bp);
8057 bcopy (bp, op->bufp, length - 1);
8058 op->bufp += (length - 1);
8070 while (bp < limit) {
8075 /* If this is the end of the file, we have an unterminated comment.
8076 Don't swallow the newline. We are guaranteed that there will be a
8077 trailing newline and various pieces assume it's there. */
8084 if (line_counter != NULL)
8090 if (bp[-2] == '/' && !nowarn && warn_comments)
8091 warning ("`/*' within comment");
8092 if (*bp == '\\' && bp[1] == '\n')
8101 #ifdef MULTIBYTE_CHARS
8106 length = local_mblen (bp, limit - bp);
8112 bcopy (bp, op->bufp, length);
8122 error_with_line (line_for_error (start_line), "unterminated comment");
8127 /* Skip over a quoted string. BP points to the opening quote.
8128 Returns a pointer after the closing quote. Don't go past LIMIT.
8129 START_LINE is the line number of the starting point (but it need
8130 not be valid if the starting point is inside a macro expansion).
8132 The input stack state is not changed.
8134 If COUNT_NEWLINES is nonzero, it points to an int to increment
8135 for each newline passed.
8137 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8138 if we pass a backslash-newline.
8140 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8143 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
8144 register U_CHAR *bp;
8145 register U_CHAR *limit;
8147 int *count_newlines;
8148 int *backslash_newlines_p;
8151 register U_CHAR c, match;
8156 error_with_line (line_for_error (start_line),
8157 "unterminated string or character constant");
8158 error_with_line (multiline_string_line,
8159 "possible real start of unterminated constant");
8160 multiline_string_line = 0;
8167 while (*bp == '\\' && bp[1] == '\n') {
8168 if (backslash_newlines_p)
8169 *backslash_newlines_p = 1;
8175 if (backslash_newlines_p)
8176 *backslash_newlines_p = 1;
8181 } else if (c == '\n') {
8183 /* Unterminated strings and character constants are 'valid'. */
8184 bp--; /* Don't consume the newline. */
8189 if (match == '\'') {
8190 error_with_line (line_for_error (start_line),
8191 "unterminated string or character constant");
8197 /* If not traditional, then allow newlines inside strings. */
8200 if (multiline_string_line == 0) {
8202 pedwarn_with_line (line_for_error (start_line),
8203 "string constant runs past end of line");
8204 multiline_string_line = start_line;
8206 } else if (c == match)
8208 #ifdef MULTIBYTE_CHARS
8212 length = local_mblen (bp, limit - bp);
8222 /* Place into DST a quoted string representing the string SRC.
8223 SRCLEN is the length of SRC; SRC may contain null bytes.
8224 Return the address of DST's terminating null. */
8227 quote_string (dst, src, srclen)
8232 char *srclim = src + srclen;
8235 while (src != srclim)
8236 switch ((c = *src++))
8243 sprintf (dst, "\\%03o", c);
8260 /* Skip across a group of balanced parens, starting from IP->bufp.
8261 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8263 This does not handle newlines, because it's used for the arg of #if,
8264 where there aren't any newlines. Also, backslash-newline can't appear. */
8267 skip_paren_group (ip)
8268 register FILE_BUF *ip;
8270 U_CHAR *limit = ip->buf + ip->length;
8271 U_CHAR *p = ip->bufp;
8273 int lines_dummy = 0;
8275 while (p != limit) {
8285 return ip->bufp = p;
8291 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
8299 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
8301 return ip->bufp = p;
8311 /* Write out a #line directive, for instance, after an #include file.
8312 If CONDITIONAL is nonzero, we can omit the #line if it would
8313 appear to be a no-op, and we can output a few newlines instead
8314 if we want to increase the line number by a small amount.
8315 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8318 output_line_directive (ip, op, conditional, file_change)
8321 enum file_change_code file_change;
8324 char *line_directive_buf, *line_end;
8326 if (no_line_directives
8327 || ip->fname == NULL
8329 op->lineno = ip->lineno;
8334 if (ip->lineno == op->lineno)
8337 /* If the inherited line number is a little too small,
8338 output some newlines instead of a #line directive. */
8339 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
8340 check_expand (op, 10);
8341 while (ip->lineno > op->lineno) {
8349 /* Output a positive line number if possible. */
8350 while (ip->lineno <= 0 && ip->bufp - ip->buf < ip->length
8351 && *ip->bufp == '\n') {
8356 line_directive_buf = (char *) alloca (4 * ip->nominal_fname_len + 100);
8357 sprintf (line_directive_buf, "# %d ", ip->lineno);
8358 line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
8359 ip->nominal_fname, ip->nominal_fname_len);
8360 if (file_change != same_file) {
8362 *line_end++ = file_change == enter_file ? '1' : '2';
8364 /* Tell cc1 if following text comes from a system header file. */
8365 if (ip->system_header_p) {
8369 #ifndef NO_IMPLICIT_EXTERN_C
8370 /* Tell cc1plus if following text should be treated as C. */
8371 if (ip->system_header_p == 2 && cplusplus) {
8377 len = line_end - line_directive_buf;
8378 check_expand (op, len + 1);
8379 if (op->bufp > op->buf && op->bufp[-1] != '\n')
8381 bcopy ((char *) line_directive_buf, (char *) op->bufp, len);
8383 op->lineno = ip->lineno;
8386 /* This structure represents one parsed argument in a macro call.
8387 `raw' points to the argument text as written (`raw_length' is its length).
8388 `expanded' points to the argument's macro-expansion
8389 (its length is `expand_length').
8390 `stringified_length' is the length the argument would have
8392 `use_count' is the number of times this macro arg is substituted
8393 into the macro. If the actual use count exceeds 10,
8394 the value stored is 10.
8395 `free1' and `free2', if nonzero, point to blocks to be freed
8396 when the macro argument data is no longer needed. */
8399 U_CHAR *raw, *expanded;
8400 int raw_length, expand_length;
8401 int stringified_length;
8402 U_CHAR *free1, *free2;
8407 /* Expand a macro call.
8408 HP points to the symbol that is the macro being called.
8409 Put the result of expansion onto the input stack
8410 so that subsequent input by our caller will use it.
8412 If macro wants arguments, caller has already verified that
8413 an argument list follows; arguments come from the input stack. */
8416 macroexpand (hp, op)
8421 DEFINITION *defn = hp->value.defn;
8422 register U_CHAR *xbuf;
8424 int start_line = instack[indepth].lineno;
8425 int rest_args, rest_zero;
8427 CHECK_DEPTH (return;);
8429 /* it might not actually be a macro. */
8430 if (hp->type != T_MACRO) {
8431 special_symbol (hp, op);
8435 /* This macro is being used inside a #if, which means it must be */
8436 /* recorded as a precondition. */
8437 if (pcp_inside_if && pcp_outfile && defn->predefined)
8438 dump_single_macro (hp, pcp_outfile);
8440 nargs = defn->nargs;
8444 struct argdata *args;
8445 char *parse_error = 0;
8447 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
8449 for (i = 0; i < nargs; i++) {
8450 args[i].raw = (U_CHAR *) "";
8451 args[i].expanded = 0;
8452 args[i].raw_length = args[i].expand_length
8453 = args[i].stringified_length = 0;
8454 args[i].free1 = args[i].free2 = 0;
8455 args[i].use_count = 0;
8458 /* Parse all the macro args that are supplied. I counts them.
8459 The first NARGS args are stored in ARGS.
8460 The rest are discarded.
8461 If rest_args is set then we assume macarg absorbed the rest of the args.
8466 /* Discard the open-parenthesis or comma before the next arg. */
8467 ++instack[indepth].bufp;
8470 if (i < nargs || (nargs == 0 && i == 0)) {
8471 /* If we are working on last arg which absorbs rest of args... */
8472 if (i == nargs - 1 && defn->rest_args)
8474 parse_error = macarg (&args[i], rest_args);
8477 parse_error = macarg (NULL_PTR, 0);
8479 error_with_line (line_for_error (start_line), parse_error);
8483 } while (*instack[indepth].bufp != ')');
8485 /* If we got one arg but it was just whitespace, call that 0 args. */
8487 register U_CHAR *bp = args[0].raw;
8488 register U_CHAR *lim = bp + args[0].raw_length;
8489 /* cpp.texi says for foo ( ) we provide one argument.
8490 However, if foo wants just 0 arguments, treat this as 0. */
8492 while (bp != lim && is_space[*bp]) bp++;
8497 /* Don't output an error message if we have already output one for
8498 a parse error above. */
8500 if (nargs == 0 && i > 0) {
8502 error ("arguments given to macro `%s'", hp->name);
8503 } else if (i < nargs) {
8504 /* traditional C allows foo() if foo wants one argument. */
8505 if (nargs == 1 && i == 0 && traditional)
8507 /* the rest args token is allowed to absorb 0 tokens */
8508 else if (i == nargs - 1 && defn->rest_args)
8510 else if (parse_error)
8513 error ("macro `%s' used without args", hp->name);
8515 error ("macro `%s' used with just one arg", hp->name);
8517 error ("macro `%s' used with only %d args", hp->name, i);
8518 } else if (i > nargs) {
8520 error ("macro `%s' used with too many (%d) args", hp->name, i);
8523 /* Swallow the closeparen. */
8524 ++instack[indepth].bufp;
8526 /* If macro wants zero args, we parsed the arglist for checking only.
8527 Read directly from the macro definition. */
8529 xbuf = defn->expansion;
8530 xbuf_len = defn->length;
8532 register U_CHAR *exp = defn->expansion;
8533 register int offset; /* offset in expansion,
8534 copied a piece at a time */
8535 register int totlen; /* total amount of exp buffer filled so far */
8537 register struct reflist *ap, *last_ap;
8539 /* Macro really takes args. Compute the expansion of this call. */
8541 /* Compute length in characters of the macro's expansion.
8542 Also count number of times each arg is used. */
8543 xbuf_len = defn->length;
8544 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
8546 xbuf_len += args[ap->argno].stringified_length;
8547 else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
8548 /* Add 4 for two newline-space markers to prevent
8549 token concatenation. */
8550 xbuf_len += args[ap->argno].raw_length + 4;
8552 /* We have an ordinary (expanded) occurrence of the arg.
8553 So compute its expansion, if we have not already. */
8554 if (args[ap->argno].expanded == 0) {
8556 obuf = expand_to_temp_buffer (args[ap->argno].raw,
8557 args[ap->argno].raw + args[ap->argno].raw_length,
8560 args[ap->argno].expanded = obuf.buf;
8561 args[ap->argno].expand_length = obuf.length;
8562 args[ap->argno].free2 = obuf.buf;
8565 /* Add 4 for two newline-space markers to prevent
8566 token concatenation. */
8567 xbuf_len += args[ap->argno].expand_length + 4;
8569 if (args[ap->argno].use_count < 10)
8570 args[ap->argno].use_count++;
8573 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
8575 /* Generate in XBUF the complete expansion
8576 with arguments substituted in.
8577 TOTLEN is the total size generated so far.
8578 OFFSET is the index in the definition
8579 of where we are copying from. */
8580 offset = totlen = 0;
8581 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
8582 last_ap = ap, ap = ap->next) {
8583 register struct argdata *arg = &args[ap->argno];
8584 int count_before = totlen;
8586 /* Add chars to XBUF. */
8587 for (i = 0; i < ap->nchars; i++, offset++)
8588 xbuf[totlen++] = exp[offset];
8590 /* If followed by an empty rest arg with concatenation,
8591 delete the last run of nonwhite chars. */
8592 if (rest_zero && totlen > count_before
8593 && ((ap->rest_args && ap->raw_before != 0)
8594 || (last_ap != NULL && last_ap->rest_args
8595 && last_ap->raw_after != 0))) {
8596 /* Delete final whitespace. */
8597 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
8601 /* Delete the nonwhites before them. */
8602 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
8607 if (ap->stringify != 0) {
8608 int arglen = arg->raw_length;
8614 && (c = arg->raw[i], is_space[c]))
8617 && (c = arg->raw[arglen - 1], is_space[c]))
8620 xbuf[totlen++] = '\"'; /* insert beginning quote */
8621 for (; i < arglen; i++) {
8625 /* Special markers Newline Space
8626 generate nothing for a stringified argument. */
8627 if (c == '\n' && arg->raw[i+1] != '\n') {
8632 /* Internal sequences of whitespace are replaced by one space
8633 except within an string or char token. */
8634 if (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c]) {
8636 /* Note that Newline Space does occur within whitespace
8637 sequences; consider it part of the sequence. */
8638 if (c == '\n' && is_space[arg->raw[i+1]])
8640 else if (c != '\n' && is_space[c])
8655 else if (in_string) {
8660 #ifdef MULTIBYTE_CHARS
8662 length = local_mblen (arg->raw + i, arglen - i);
8665 bcopy (arg->raw + i, xbuf + totlen, length);
8672 } else if (c == '\"' || c == '\'')
8676 /* Escape these chars */
8677 if (c == '\"' || (in_string && c == '\\'))
8678 xbuf[totlen++] = '\\';
8679 /* We used to output e.g. \008 for control characters here,
8680 but this doesn't conform to the C Standard.
8681 Just output the characters as-is. */
8685 xbuf[totlen++] = '\"'; /* insert ending quote */
8686 } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
8687 U_CHAR *p1 = arg->raw;
8688 U_CHAR *l1 = p1 + arg->raw_length;
8689 if (ap->raw_before != 0) {
8690 while (p1 != l1 && is_space[*p1]) p1++;
8691 while (p1 != l1 && is_idchar[*p1])
8692 xbuf[totlen++] = *p1++;
8693 /* Delete any no-reexpansion marker that follows
8694 an identifier at the beginning of the argument
8695 if the argument is concatenated with what precedes it. */
8696 if (p1[0] == '\n' && p1[1] == '-')
8698 } else if (!traditional) {
8699 /* Ordinary expanded use of the argument.
8700 Put in newline-space markers to prevent token pasting. */
8701 xbuf[totlen++] = '\n';
8702 xbuf[totlen++] = ' ';
8704 if (ap->raw_after != 0) {
8705 /* Arg is concatenated after: delete trailing whitespace,
8706 whitespace markers, and no-reexpansion markers. */
8708 if (is_space[l1[-1]]) l1--;
8709 else if (l1[-1] == '-') {
8710 U_CHAR *p2 = l1 - 1;
8711 /* If a `-' is preceded by an odd number of newlines then it
8712 and the last newline are a no-reexpansion marker. */
8713 while (p2 != p1 && p2[-1] == '\n') p2--;
8714 if ((l1 - 1 - p2) & 1) {
8723 bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
8725 if (!traditional && ap->raw_after == 0) {
8726 /* Ordinary expanded use of the argument.
8727 Put in newline-space markers to prevent token pasting. */
8728 xbuf[totlen++] = '\n';
8729 xbuf[totlen++] = ' ';
8732 /* Ordinary expanded use of the argument.
8733 Put in newline-space markers to prevent token pasting. */
8735 xbuf[totlen++] = '\n';
8736 xbuf[totlen++] = ' ';
8738 bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
8739 arg->expand_length);
8740 totlen += arg->expand_length;
8742 xbuf[totlen++] = '\n';
8743 xbuf[totlen++] = ' ';
8745 /* If a macro argument with newlines is used multiple times,
8746 then only expand the newlines once. This avoids creating output
8747 lines which don't correspond to any input line, which confuses
8749 if (arg->use_count > 1 && arg->newlines > 0) {
8750 /* Don't bother doing change_newlines for subsequent
8754 = change_newlines (arg->expanded, arg->expand_length);
8758 if (totlen > xbuf_len)
8762 /* If there is anything left of the definition after handling
8763 the arg list, copy that in too. */
8765 for (i = offset; i < defn->length; i++) {
8766 /* if we've reached the end of the macro */
8769 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
8770 && last_ap->raw_after != 0))
8771 xbuf[totlen++] = exp[i];
8777 for (i = 0; i < nargs; i++) {
8778 if (args[i].free1 != 0)
8779 free (args[i].free1);
8780 if (args[i].free2 != 0)
8781 free (args[i].free2);
8785 xbuf = defn->expansion;
8786 xbuf_len = defn->length;
8789 /* Now put the expansion on the input stack
8790 so our caller will commence reading from it. */
8792 register FILE_BUF *ip2;
8794 ip2 = &instack[++indepth];
8797 ip2->nominal_fname = 0;
8798 ip2->nominal_fname_len = 0;
8800 /* This may not be exactly correct, but will give much better error
8801 messages for nested macro calls than using a line number of zero. */
8802 ip2->lineno = start_line;
8804 ip2->length = xbuf_len;
8806 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
8808 ip2->if_stack = if_stack;
8809 ip2->system_header_p = 0;
8811 /* Recursive macro use sometimes works traditionally.
8812 #define foo(x,y) bar (x (y,0), y)
8816 hp->type = T_DISABLED;
8820 /* Parse a macro argument and store the info on it into *ARGPTR.
8821 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8822 Return nonzero to indicate a syntax error. */
8825 macarg (argptr, rest_args)
8826 register struct argdata *argptr;
8829 FILE_BUF *ip = &instack[indepth];
8835 /* Try to parse as much of the argument as exists at this
8836 input stack level. */
8837 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro,
8838 &paren, &newlines, &comments, rest_args);
8840 /* If we find the end of the argument at this level,
8841 set up *ARGPTR to point at it in the input stack. */
8842 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
8843 && bp != ip->buf + ip->length) {
8845 argptr->raw = ip->bufp;
8846 argptr->raw_length = bp - ip->bufp;
8847 argptr->newlines = newlines;
8851 /* This input stack level ends before the macro argument does.
8852 We must pop levels and keep parsing.
8853 Therefore, we must allocate a temporary buffer and copy
8854 the macro argument into it. */
8855 int bufsize = bp - ip->bufp;
8856 int extra = newlines;
8857 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
8858 int final_start = 0;
8860 bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
8862 ip->lineno += newlines;
8864 while (bp == ip->buf + ip->length) {
8865 if (instack[indepth].macro == 0) {
8866 result = "unterminated macro call";
8869 ip->macro->type = T_MACRO;
8871 free (ip->free_ptr);
8872 ip = &instack[--indepth];
8875 bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro, &paren,
8876 &newlines, &comments, rest_args);
8877 final_start = bufsize;
8878 bufsize += bp - ip->bufp;
8880 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
8881 bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
8884 ip->lineno += newlines;
8887 /* Now, if arg is actually wanted, record its raw form,
8888 discarding comments and duplicating newlines in whatever
8889 part of it did not come from a macro expansion.
8890 EXTRA space has been preallocated for duplicating the newlines.
8891 FINAL_START is the index of the start of that part. */
8893 argptr->raw = buffer;
8894 argptr->raw_length = bufsize;
8895 argptr->free1 = buffer;
8896 argptr->newlines = newlines;
8897 if ((newlines || comments) && ip->fname != 0)
8900 discard_comments (argptr->raw + final_start,
8901 argptr->raw_length - final_start,
8903 argptr->raw[argptr->raw_length] = 0;
8904 if (argptr->raw_length > bufsize + extra)
8909 /* If we are not discarding this argument,
8910 macroexpand it and compute its length as stringified.
8911 All this info goes into *ARGPTR. */
8914 register U_CHAR *buf, *lim;
8915 register int totlen;
8918 lim = buf + argptr->raw_length;
8920 while (buf != lim && is_space[*buf])
8922 while (buf != lim && is_space[lim[-1]])
8924 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
8925 while (buf != lim) {
8926 register U_CHAR c = *buf++;
8928 /* Internal sequences of whitespace are replaced by one space
8929 in most cases, but not always. So count all the whitespace
8930 in case we need to keep it all. */
8933 SKIP_ALL_WHITE_SPACE (buf);
8936 if (c == '\"' || c == '\\') /* escape these chars */
8939 argptr->stringified_length = totlen;
8944 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8945 taken from the expansion of MACRO,
8946 counting parens in *DEPTHPTR,
8947 and return if reach LIMIT
8948 or before a `)' that would make *DEPTHPTR negative
8949 or before a comma when *DEPTHPTR is zero.
8950 Single and double quotes are matched and termination
8951 is inhibited within them. Comments also inhibit it.
8952 Value returned is pointer to stopping place.
8954 Increment *NEWLINES each time a newline is passed.
8955 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8956 Set *COMMENTS to 1 if a comment is seen. */
8959 macarg1 (start, limit, macro, depthptr, newlines, comments, rest_args)
8961 register U_CHAR *limit;
8962 struct hashnode *macro;
8963 int *depthptr, *newlines, *comments;
8966 register U_CHAR *bp = start;
8968 while (bp < limit) {
8974 if (--(*depthptr) < 0)
8978 /* Traditionally, backslash makes following char not special. */
8979 if (traditional && bp + 1 < limit && bp[1] != '\n')
8988 if (bp[1] == '\\' && bp[2] == '\n')
8989 newline_fix (bp + 1);
8992 for (bp += 2; bp < limit; bp++) {
8995 else if (*bp == '*') {
8996 if (bp[-1] == '/' && warn_comments)
8997 warning ("`/*' within comment");
8998 if (bp[1] == '\\' && bp[2] == '\n')
8999 newline_fix (bp + 1);
9007 #ifdef MULTIBYTE_CHARS
9009 length = local_mblen (bp, limit - bp);
9015 } else if (bp[1] == '/' && cplusplus_comments) {
9017 for (bp += 2; bp < limit; bp++) {
9022 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
9026 warning ("multiline `//' comment");
9031 #ifdef MULTIBYTE_CHARS
9033 length = local_mblen (bp, limit - bp);
9045 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
9051 while (*bp == '\\' && bp[1] == '\n') {
9056 } else if (*bp == '\n') {
9063 #ifdef MULTIBYTE_CHARS
9065 length = local_mblen (bp, limit - bp);
9074 /* if we've returned to lowest level and we aren't absorbing all args */
9075 if ((*depthptr) == 0 && rest_args == 0)
9085 /* Discard comments and duplicate newlines
9086 in the string of length LENGTH at START,
9087 except inside of string constants.
9088 The string is copied into itself with its beginning staying fixed.
9090 NEWLINES is the number of newlines that must be duplicated.
9091 We assume that that much extra space is available past the end
9095 discard_comments (start, length, newlines)
9100 register U_CHAR *ibp;
9101 register U_CHAR *obp;
9102 register U_CHAR *limit;
9105 /* If we have newlines to duplicate, copy everything
9106 that many characters up. Then, in the second part,
9107 we will have room to insert the newlines
9109 NEWLINES may actually be too large, because it counts
9110 newlines in string constants, and we don't duplicate those.
9111 But that does no harm. */
9113 ibp = start + length;
9114 obp = ibp + newlines;
9116 while (limit != ibp)
9120 ibp = start + newlines;
9121 limit = start + length + newlines;
9124 while (ibp < limit) {
9125 *obp++ = c = *ibp++;
9128 /* Duplicate the newline. */
9140 if (*ibp == '\\' && ibp[1] == '\n')
9142 /* Delete any comment. */
9143 if (cplusplus_comments && ibp[0] == '/') {
9144 /* Comments are equivalent to spaces. */
9151 if (*ibp == '\\' && ibp + 1 < limit && ibp[1] == '\n')
9155 #ifdef MULTIBYTE_CHARS
9156 int length = local_mblen (ibp, limit - ibp);
9158 ibp += (length - 1);
9165 if (ibp[0] != '*' || ibp + 1 >= limit)
9167 /* Comments are equivalent to spaces.
9168 For -traditional, a comment is equivalent to nothing. */
9173 while (++ibp < limit) {
9174 if (ibp[0] == '*') {
9175 if (ibp[1] == '\\' && ibp[2] == '\n')
9176 newline_fix (ibp + 1);
9177 if (ibp[1] == '/') {
9184 #ifdef MULTIBYTE_CHARS
9185 int length = local_mblen (ibp, limit - ibp);
9187 ibp += (length - 1);
9195 /* Notice and skip strings, so that we don't
9196 think that comments start inside them,
9197 and so we don't duplicate newlines in them. */
9200 while (ibp < limit) {
9201 *obp++ = c = *ibp++;
9209 else if (c == '\\') {
9210 if (ibp < limit && *ibp == '\n') {
9214 while (*ibp == '\\' && ibp[1] == '\n')
9222 #ifdef MULTIBYTE_CHARS
9225 length = local_mblen (ibp, limit - ibp);
9229 bcopy (ibp, obp, length);
9246 /* Turn newlines to spaces in the string of length LENGTH at START,
9247 except inside of string constants.
9248 The string is copied into itself with its beginning staying fixed. */
9251 change_newlines (start, length)
9255 register U_CHAR *ibp;
9256 register U_CHAR *obp;
9257 register U_CHAR *limit;
9261 limit = start + length;
9264 while (ibp < limit) {
9265 *obp++ = c = *ibp++;
9268 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9269 string. Skip past the newline and its duplicate.
9270 Put a space in the output. */
9281 /* Notice and skip strings, so that we don't delete newlines in them. */
9284 while (ibp < limit) {
9285 *obp++ = c = *ibp++;
9288 else if (c == '\\' && ibp < limit && *ibp == '\n')
9297 #ifdef MULTIBYTE_CHARS
9300 length = local_mblen (ibp, limit - ibp);
9304 bcopy (ibp, obp, length);
9321 /* my_strerror - return the descriptive text associated with an
9325 my_strerror (errnum)
9331 #ifndef HAVE_STRERROR
9332 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
9334 result = strerror (errnum);
9337 /* VAXCRTL's strerror() takes an optional second argument, which only
9338 matters when the first argument is EVMSERR. However, it's simplest
9339 just to pass it unconditionally. `vaxc$errno' is declared in
9340 <errno.h>, and maintained by the library in parallel with `errno'.
9341 We assume that caller's `errnum' either matches the last setting of
9342 `errno' by the library or else does not have the value `EVMSERR'. */
9344 result = strerror (errnum, vaxc$errno);
9348 result = "undocumented I/O error";
9353 /* error - print error message and increment count of errors. */
9356 error VPROTO ((char * msg, ...))
9358 #ifndef ANSI_PROTOTYPES
9363 VA_START (args, msg);
9365 #ifndef ANSI_PROTOTYPES
9366 msg = va_arg (args, char *);
9379 FILE_BUF *ip = NULL;
9381 print_containing_files ();
9383 for (i = indepth; i >= 0; i--)
9384 if (instack[i].fname != NULL) {
9390 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9391 fprintf (stderr, ":%d: ", ip->lineno);
9393 vfprintf (stderr, msg, args);
9394 fprintf (stderr, "\n");
9398 /* Error including a message from `errno'. */
9401 error_from_errno (name)
9406 FILE_BUF *ip = NULL;
9408 print_containing_files ();
9410 for (i = indepth; i >= 0; i--)
9411 if (instack[i].fname != NULL) {
9417 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9418 fprintf (stderr, ":%d: ", ip->lineno);
9421 fprintf (stderr, "%s: %s\n", name, my_strerror (e));
9426 /* Print error message but don't count it. */
9429 warning VPROTO ((char * msg, ...))
9431 #ifndef ANSI_PROTOTYPES
9436 VA_START (args, msg);
9438 #ifndef ANSI_PROTOTYPES
9439 msg = va_arg (args, char *);
9442 vwarning (msg, args);
9447 vwarning (msg, args)
9452 FILE_BUF *ip = NULL;
9454 if (inhibit_warnings)
9457 if (warnings_are_errors)
9460 print_containing_files ();
9462 for (i = indepth; i >= 0; i--)
9463 if (instack[i].fname != NULL) {
9469 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9470 fprintf (stderr, ":%d: ", ip->lineno);
9472 fprintf (stderr, "warning: ");
9473 vfprintf (stderr, msg, args);
9474 fprintf (stderr, "\n");
9478 error_with_line VPROTO ((int line, char * msg, ...))
9480 #ifndef ANSI_PROTOTYPES
9486 VA_START (args, msg);
9488 #ifndef ANSI_PROTOTYPES
9489 line = va_arg (args, int);
9490 msg = va_arg (args, char *);
9493 verror_with_line (line, msg, args);
9498 verror_with_line (line, msg, args)
9504 FILE_BUF *ip = NULL;
9506 print_containing_files ();
9508 for (i = indepth; i >= 0; i--)
9509 if (instack[i].fname != NULL) {
9515 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9516 fprintf (stderr, ":%d: ", line);
9518 vfprintf (stderr, msg, args);
9519 fprintf (stderr, "\n");
9524 warning_with_line VPROTO ((int line, char * msg, ...))
9526 #ifndef ANSI_PROTOTYPES
9532 VA_START (args, msg);
9534 #ifndef ANSI_PROTOTYPES
9535 line = va_arg (args, int);
9536 msg = va_arg (args, char *);
9539 vwarning_with_line (line, msg, args);
9544 vwarning_with_line (line, msg, args)
9550 FILE_BUF *ip = NULL;
9552 if (inhibit_warnings)
9555 if (warnings_are_errors)
9558 print_containing_files ();
9560 for (i = indepth; i >= 0; i--)
9561 if (instack[i].fname != NULL) {
9567 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9568 fprintf (stderr, line ? ":%d: " : ": ", line);
9570 fprintf (stderr, "warning: ");
9571 vfprintf (stderr, msg, args);
9572 fprintf (stderr, "\n");
9575 /* Print an error message and maybe count it. */
9578 pedwarn VPROTO ((char * msg, ...))
9580 #ifndef ANSI_PROTOTYPES
9585 VA_START (args, msg);
9587 #ifndef ANSI_PROTOTYPES
9588 msg = va_arg (args, char *);
9591 if (pedantic_errors)
9594 vwarning (msg, args);
9599 pedwarn_with_line VPROTO ((int line, char * msg, ...))
9601 #ifndef ANSI_PROTOTYPES
9607 VA_START (args, msg);
9609 #ifndef ANSI_PROTOTYPES
9610 line = va_arg (args, int);
9611 msg = va_arg (args, char *);
9614 if (pedantic_errors)
9615 verror_with_line (line, msg, args);
9617 vwarning_with_line (line, msg, args);
9621 /* Report a warning (or an error if pedantic_errors)
9622 giving specified file name and line number, not current. */
9625 pedwarn_with_file_and_line VPROTO ((char *file, size_t file_len, int line,
9628 #ifndef ANSI_PROTOTYPES
9636 if (!pedantic_errors && inhibit_warnings)
9639 VA_START (args, msg);
9641 #ifndef ANSI_PROTOTYPES
9642 file = va_arg (args, char *);
9643 file_len = va_arg (args, size_t);
9644 line = va_arg (args, int);
9645 msg = va_arg (args, char *);
9649 eprint_string (file, file_len);
9650 fprintf (stderr, ":%d: ", line);
9652 if (pedantic_errors)
9654 if (!pedantic_errors)
9655 fprintf (stderr, "warning: ");
9657 vfprintf (stderr, msg, args);
9659 fprintf (stderr, "\n");
9662 /* Print the file names and line numbers of the #include
9663 directives which led to the current file. */
9666 print_containing_files ()
9668 FILE_BUF *ip = NULL;
9672 /* If stack of files hasn't changed since we last printed
9673 this info, don't repeat it. */
9674 if (last_error_tick == input_file_stack_tick)
9677 for (i = indepth; i >= 0; i--)
9678 if (instack[i].fname != NULL) {
9683 /* Give up if we don't find a source file. */
9687 /* Find the other, outer source files. */
9688 for (i--; i >= 0; i--)
9689 if (instack[i].fname != NULL) {
9693 fprintf (stderr, "In file included");
9695 fprintf (stderr, ",\n ");
9698 fprintf (stderr, " from ");
9699 eprint_string (ip->nominal_fname, ip->nominal_fname_len);
9700 fprintf (stderr, ":%d", ip->lineno);
9703 fprintf (stderr, ":\n");
9705 /* Record we have printed the status as of this time. */
9706 last_error_tick = input_file_stack_tick;
9709 /* Return the line at which an error occurred.
9710 The error is not necessarily associated with the current spot
9711 in the input stack, so LINE says where. LINE will have been
9712 copied from ip->lineno for the current input level.
9713 If the current level is for a file, we return LINE.
9714 But if the current level is not for a file, LINE is meaningless.
9715 In that case, we return the lineno of the innermost file. */
9718 line_for_error (line)
9724 for (i = indepth; i >= 0; ) {
9725 if (instack[i].fname != 0)
9730 line1 = instack[i].lineno;
9738 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9740 * As things stand, nothing is ever placed in the output buffer to be
9741 * removed again except when it's KNOWN to be part of an identifier,
9742 * so flushing and moving down everything left, instead of expanding,
9746 /* You might think void was cleaner for the return type,
9747 but that would get type mismatch in check_expand in strict ANSI. */
9750 grow_outbuf (obuf, needed)
9751 register FILE_BUF *obuf;
9752 register int needed;
9757 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
9760 /* Make it at least twice as big as it is now. */
9762 /* Make it have at least 150% of the free space we will need. */
9763 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
9764 if (minsize > obuf->length)
9765 obuf->length = minsize;
9767 if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
9770 obuf->bufp = p + (obuf->bufp - obuf->buf);
9776 /* Symbol table for macro names and special symbols */
9779 * install a name in the main hash table, even if it is already there.
9780 * name stops with first non alphanumeric, except leading '#'.
9781 * caller must check against redefinition if that is desired.
9782 * delete_macro () removes things installed by install () in fifo order.
9783 * this is important because of the `defined' special symbol used
9784 * in #if, and also if pushdef/popdef directives are ever implemented.
9786 * If LEN is >= 0, it is the length of the name.
9787 * Otherwise, compute the length by scanning the entire name.
9789 * If HASH is >= 0, it is the precomputed hash code.
9790 * Otherwise, compute the hash code.
9794 install (name, len, type, value, hash)
9797 enum node_type type;
9801 register HASHNODE *hp;
9802 register int i, bucket;
9803 register U_CHAR *p, *q;
9807 while (is_idchar[*p])
9813 hash = hashf (name, len, HASHSIZE);
9815 i = sizeof (HASHNODE) + len + 1;
9816 hp = (HASHNODE *) xmalloc (i);
9818 hp->bucket_hdr = &hashtab[bucket];
9819 hp->next = hashtab[bucket];
9820 hashtab[bucket] = hp;
9822 if (hp->next != NULL)
9823 hp->next->prev = hp;
9826 hp->value.cpval = value;
9827 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
9830 for (i = 0; i < len; i++)
9837 * find the most recent hash node for name "name" (ending with first
9838 * non-identifier char) installed by install
9840 * If LEN is >= 0, it is the length of the name.
9841 * Otherwise, compute the length by scanning the entire name.
9843 * If HASH is >= 0, it is the precomputed hash code.
9844 * Otherwise, compute the hash code.
9848 lookup (name, len, hash)
9853 register U_CHAR *bp;
9854 register HASHNODE *bucket;
9857 for (bp = name; is_idchar[*bp]; bp++) ;
9862 hash = hashf (name, len, HASHSIZE);
9864 bucket = hashtab[hash];
9866 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
9868 bucket = bucket->next;
9874 * Delete a hash node. Some weirdness to free junk from macros.
9875 * More such weirdness will have to be added if you define more hash
9876 * types that need it.
9879 /* Note that the DEFINITION of a macro is removed from the hash table
9880 but its storage is not freed. This would be a storage leak
9881 except that it is not reasonable to keep undefining and redefining
9882 large numbers of macros many times.
9883 In any case, this is necessary, because a macro can be #undef'd
9884 in the middle of reading the arguments to a call to it.
9885 If #undef freed the DEFINITION, that would crash. */
9892 if (hp->prev != NULL)
9893 hp->prev->next = hp->next;
9894 if (hp->next != NULL)
9895 hp->next->prev = hp->prev;
9897 /* Make sure that the bucket chain header that the deleted guy was
9898 on points to the right thing afterwards. */
9899 if (hp == *hp->bucket_hdr)
9900 *hp->bucket_hdr = hp->next;
9903 if (hp->type == T_MACRO) {
9904 DEFINITION *d = hp->value.defn;
9905 struct reflist *ap, *nextap;
9907 for (ap = d->pattern; ap != NULL; ap = nextap) {
9918 * return hash function on name. must be compatible with the one
9919 * computed a step at a time, elsewhere
9923 hashf (name, len, hashsize)
9924 register U_CHAR *name;
9931 r = HASHSTEP (r, *name++);
9933 return MAKE_POS (r) % hashsize;
9937 /* Dump the definition of a single macro HP to OF. */
9940 dump_single_macro (hp, of)
9941 register HASHNODE *hp;
9944 register DEFINITION *defn = hp->value.defn;
9950 /* Print the definition of the macro HP. */
9952 fprintf (of, "#define %s", hp->name);
9954 if (defn->nargs >= 0) {
9958 for (i = 0; i < defn->nargs; i++) {
9959 dump_arg_n (defn, i, of);
9960 if (i + 1 < defn->nargs)
9970 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
9971 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
9972 offset += ap->nchars;
9974 if (ap->nchars != 0)
9976 if (ap->stringify) {
9977 switch (ap->stringify) {
9978 case SHARP_TOKEN: fprintf (of, "#"); break;
9979 case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
9980 case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
9981 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
9985 if (ap->raw_before != 0) {
9987 switch (ap->raw_before) {
9988 case WHITE_SHARP_TOKEN:
9989 case WHITE_PERCENT_COLON_TOKEN:
9996 switch (ap->raw_before) {
9997 case SHARP_TOKEN: fprintf (of, "##"); break;
9998 case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
9999 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10000 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
10007 dump_arg_n (defn, ap->argno, of);
10008 if (!traditional && ap->raw_after != 0) {
10009 switch (ap->raw_after) {
10010 case SHARP_TOKEN: fprintf (of, "##"); break;
10011 case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
10012 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10013 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
10019 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
10020 fprintf (of, "\n");
10023 /* Dump all macro definitions as #defines to stdout. */
10030 for (bucket = 0; bucket < HASHSIZE; bucket++) {
10031 register HASHNODE *hp;
10033 for (hp = hashtab[bucket]; hp; hp= hp->next) {
10034 if (hp->type == T_MACRO)
10035 dump_single_macro (hp, stdout);
10040 /* Output to OF a substring of a macro definition.
10041 BASE is the beginning of the definition.
10042 Output characters START thru LENGTH.
10043 Unless traditional, discard newlines outside of strings, thus
10044 converting funny-space markers to ordinary spaces. */
10047 dump_defn_1 (base, start, length, of)
10053 U_CHAR *p = base + start;
10054 U_CHAR *limit = base + start + length;
10057 fwrite (p, sizeof (*p), length, of);
10059 while (p < limit) {
10060 if (*p == '\"' || *p =='\'') {
10061 U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
10062 NULL_PTR, NULL_PTR);
10063 fwrite (p, sizeof (*p), p1 - p, of);
10074 /* Print the name of argument number ARGNUM of macro definition DEFN
10076 Recall that DEFN->args.argnames contains all the arg names
10077 concatenated in reverse order with comma-space in between. */
10080 dump_arg_n (defn, argnum, of)
10085 register U_CHAR *p = defn->args.argnames;
10086 while (argnum + 1 < defn->nargs) {
10087 p = (U_CHAR *) index ((char *) p, ' ') + 1;
10091 while (*p && *p != ',') {
10097 /* Initialize syntactic classifications of characters. */
10100 initialize_char_syntax ()
10105 * Set up is_idchar and is_idstart tables. These should be
10106 * faster than saying (is_alpha (c) || c == '_'), etc.
10107 * Set up these things before calling any routines tthat
10110 for (i = 'a'; i <= 'z'; i++) {
10111 is_idchar[i - 'a' + 'A'] = 1;
10113 is_idstart[i - 'a' + 'A'] = 1;
10116 for (i = '0'; i <= '9'; i++)
10118 is_idchar['_'] = 1;
10119 is_idstart['_'] = 1;
10120 is_idchar['$'] = 1;
10121 is_idstart['$'] = 1;
10123 /* horizontal space table */
10124 is_hor_space[' '] = 1;
10125 is_hor_space['\t'] = 1;
10126 is_hor_space['\v'] = 1;
10127 is_hor_space['\f'] = 1;
10128 is_hor_space['\r'] = 1;
10131 is_space['\t'] = 1;
10132 is_space['\v'] = 1;
10133 is_space['\f'] = 1;
10134 is_space['\n'] = 1;
10135 is_space['\r'] = 1;
10137 char_name['\v'] = "vertical tab";
10138 char_name['\f'] = "formfeed";
10139 char_name['\r'] = "carriage return";
10142 /* Initialize the built-in macros. */
10145 initialize_builtins (inp, outp)
10149 install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
10150 install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
10151 install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
10152 install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
10153 install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
10154 install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
10155 #ifndef NO_BUILTIN_SIZE_TYPE
10156 install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
10158 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10159 install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
10161 install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
10162 install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
10164 install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
10166 install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
10168 install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
10169 if (!traditional) {
10170 install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
10171 install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
10174 install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1);
10175 /* This is supplied using a -D by the compiler driver
10176 so that it is present only when truly compiling with GNU C. */
10177 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10178 install ((U_CHAR *) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST, "1", -1);
10182 char directive[2048];
10183 U_CHAR *udirective = (U_CHAR *) directive;
10184 register struct directive *dp = &directive_table[0];
10185 struct tm *timebuf = timestamp ();
10187 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
10188 instack[0].nominal_fname);
10189 output_line_directive (inp, outp, 0, same_file);
10190 pass_thru_directive (udirective, &udirective[strlen (directive)],
10193 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
10194 output_line_directive (inp, outp, 0, same_file);
10195 pass_thru_directive (udirective, &udirective[strlen (directive)],
10198 #ifndef NO_BUILTIN_SIZE_TYPE
10199 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
10200 output_line_directive (inp, outp, 0, same_file);
10201 pass_thru_directive (udirective, &udirective[strlen (directive)],
10205 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10206 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
10207 output_line_directive (inp, outp, 0, same_file);
10208 pass_thru_directive (udirective, &udirective[strlen (directive)],
10212 sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
10213 output_line_directive (inp, outp, 0, same_file);
10214 pass_thru_directive (udirective, &udirective[strlen (directive)],
10217 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
10218 monthnames[timebuf->tm_mon],
10219 timebuf->tm_mday, timebuf->tm_year + 1900);
10220 output_line_directive (inp, outp, 0, same_file);
10221 pass_thru_directive (udirective, &udirective[strlen (directive)],
10224 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
10225 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
10226 output_line_directive (inp, outp, 0, same_file);
10227 pass_thru_directive (udirective, &udirective[strlen (directive)],
10232 sprintf (directive, " __STDC__ 1");
10233 output_line_directive (inp, outp, 0, same_file);
10234 pass_thru_directive (udirective, &udirective[strlen (directive)],
10239 sprintf (directive, " __OBJC__ 1");
10240 output_line_directive (inp, outp, 0, same_file);
10241 pass_thru_directive (udirective, &udirective[strlen (directive)],
10248 * process a given definition string, for initialization
10249 * If STR is just an identifier, define it with value 1.
10250 * If STR has anything after the identifier, then it should
10251 * be identifier=definition.
10255 make_definition (str)
10259 struct directive *kt;
10262 p = buf = (U_CHAR *) str;
10263 if (!is_idstart[*p]) {
10264 error ("malformed option `-D %s'", str);
10267 while (is_idchar[*++p])
10270 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
10273 p = (U_CHAR *) str; /* Error */
10276 buf = (U_CHAR *) alloca (p - buf + 4);
10277 strcpy ((char *)buf, str);
10278 strcat ((char *)buf, " 1");
10279 } else if (*p != '=') {
10280 error ("malformed option `-D %s'", str);
10284 /* Copy the entire option so we can modify it. */
10285 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
10286 strncpy ((char *) buf, str, p - (U_CHAR *) str);
10287 /* Change the = to a space. */
10288 buf[p - (U_CHAR *) str] = ' ';
10289 /* Scan for any backslash-newline and remove it. */
10291 q = &buf[p - (U_CHAR *) str];
10293 if (*p == '\"' || *p == '\'') {
10294 int unterminated = 0;
10295 U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
10296 NULL_PTR, NULL_PTR, &unterminated);
10301 } else if (*p == '\\' && p[1] == '\n')
10303 /* Change newline chars into newline-markers. */
10304 else if (*p == '\n')
10316 ip = &instack[++indepth];
10317 ip->nominal_fname = ip->fname = "*Initialization*";
10318 ip->nominal_fname_len = strlen (ip->nominal_fname);
10320 ip->buf = ip->bufp = buf;
10321 ip->length = strlen ((char *) buf);
10325 ip->if_stack = if_stack;
10326 ip->system_header_p = 0;
10328 for (kt = directive_table; kt->type != T_DEFINE; kt++)
10331 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10332 do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
10336 /* JF, this does the work for the -U option */
10339 make_undef (str, op)
10344 struct directive *kt;
10346 ip = &instack[++indepth];
10347 ip->nominal_fname = ip->fname = "*undef*";
10348 ip->nominal_fname_len = strlen (ip->nominal_fname);
10350 ip->buf = ip->bufp = (U_CHAR *) str;
10351 ip->length = strlen (str);
10355 ip->if_stack = if_stack;
10356 ip->system_header_p = 0;
10358 for (kt = directive_table; kt->type != T_UNDEF; kt++)
10361 do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
10365 /* Process the string STR as if it appeared as the body of a #assert.
10366 OPTION is the option name for which STR was the argument. */
10369 make_assertion (option, str)
10374 struct directive *kt;
10375 U_CHAR *buf, *p, *q;
10377 /* Copy the entire option so we can modify it. */
10378 buf = (U_CHAR *) alloca (strlen (str) + 1);
10379 strcpy ((char *) buf, str);
10380 /* Scan for any backslash-newline and remove it. */
10383 if (*p == '\\' && p[1] == '\n')
10391 if (!is_idstart[*p]) {
10392 error ("malformed option `%s %s'", option, str);
10395 while (is_idchar[*++p])
10397 SKIP_WHITE_SPACE (p);
10398 if (! (*p == 0 || *p == '(')) {
10399 error ("malformed option `%s %s'", option, str);
10403 ip = &instack[++indepth];
10404 ip->nominal_fname = ip->fname = "*Initialization*";
10405 ip->nominal_fname_len = strlen (ip->nominal_fname);
10407 ip->buf = ip->bufp = buf;
10408 ip->length = strlen ((char *) buf);
10412 ip->if_stack = if_stack;
10413 ip->system_header_p = 0;
10415 for (kt = directive_table; kt->type != T_ASSERT; kt++)
10418 /* Pass NULL as output ptr to do_define since we KNOW it never does
10420 do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
10424 #ifndef DIR_SEPARATOR
10425 #define DIR_SEPARATOR '/'
10428 /* The previous include prefix, if any, is PREV_FILE_NAME.
10429 Translate any pathnames with COMPONENT.
10430 Allocate a new include prefix whose name is the
10431 simplified concatenation of PREFIX and NAME,
10432 with a trailing / added if needed.
10433 But return 0 if the include prefix should be ignored,
10434 e.g. because it is a duplicate of PREV_FILE_NAME. */
10436 static struct file_name_list *
10437 new_include_prefix (prev_file_name, component, prefix, name)
10438 struct file_name_list *prev_file_name;
10439 const char *component;
10440 const char *prefix;
10444 fatal ("Directory name missing after command line option");
10447 /* Ignore the empty string. */
10450 prefix = update_path (prefix, component);
10451 name = update_path (name, component);
10454 struct file_name_list *dir
10455 = ((struct file_name_list *)
10456 xmalloc (sizeof (struct file_name_list)
10457 + strlen (prefix) + strlen (name) + 2));
10459 strcpy (dir->fname, prefix);
10460 strcat (dir->fname, name);
10461 len = simplify_filename (dir->fname);
10463 /* Convert directory name to a prefix. */
10464 if (len && dir->fname[len - 1] != DIR_SEPARATOR) {
10465 if (len == 1 && dir->fname[len - 1] == '.')
10469 /* must be '/', hack_vms_include_specification triggers on it. */
10470 dir->fname[len++] = '/';
10472 dir->fname[len++] = DIR_SEPARATOR;
10474 dir->fname[len] = 0;
10477 /* Ignore a directory whose name matches the previous one. */
10478 if (prev_file_name && !strcmp (prev_file_name->fname, dir->fname)) {
10479 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10480 if (!first_bracket_include)
10481 first_bracket_include = prev_file_name;
10487 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10489 /* Add a trailing "." if there is a filename. This increases the number
10490 of systems that can stat directories. We remove it below. */
10493 dir->fname[len] = '.';
10494 dir->fname[len + 1] = 0;
10497 /* Ignore a nonexistent directory. */
10498 if (stat (len ? dir->fname : ".", &dir->st) != 0) {
10499 if (errno != ENOENT && errno != ENOTDIR)
10500 error_from_errno (dir->fname);
10506 dir->fname[len] = 0;
10508 /* Ignore a directory whose identity matches the previous one. */
10510 && INO_T_EQ (prev_file_name->st.st_ino, dir->st.st_ino)
10511 && prev_file_name->st.st_dev == dir->st.st_dev) {
10512 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10513 if (!first_bracket_include)
10514 first_bracket_include = prev_file_name;
10521 dir->c_system_include_path = 0;
10522 dir->got_name_map = 0;
10528 /* Append a chain of `struct file_name_list's
10529 to the end of the main include chain.
10530 FIRST is the beginning of the chain to append, and LAST is the end. */
10533 append_include_chain (first, last)
10534 struct file_name_list *first, *last;
10536 struct file_name_list *dir;
10538 if (!first || !last)
10544 last_include->next = first;
10546 if (first_bracket_include == 0)
10547 first_bracket_include = first;
10549 for (dir = first; ; dir = dir->next) {
10550 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
10551 if (len > max_include_len)
10552 max_include_len = len;
10558 last_include = last;
10561 /* Place into DST a representation of the file named SRC that is suitable
10562 for `make'. Do not null-terminate DST. Return its length. */
10564 quote_string_for_make (dst, src)
10579 /* GNU make uses a weird quoting scheme for white space.
10580 A space or tab preceded by 2N+1 backslashes represents
10581 N backslashes followed by space; a space or tab
10582 preceded by 2N backslashes represents N backslashes at
10583 the end of a file name; and backslashes in other
10584 contexts should not be doubled. */
10586 for (q = p - 1; src < q && q[-1] == '\\'; q--)
10598 goto ordinary_char;
10604 /* Fall through. This can mishandle things like "$(" but
10605 there's no easy fix. */
10608 /* This can mishandle characters in the string "\0\n%*?[\\~";
10609 exactly which chars are mishandled depends on the `make' version.
10610 We know of no portable solution for this;
10611 even GNU make 3.76.1 doesn't solve the problem entirely.
10612 (Also, '\0' is mishandled due to our calling conventions.) */
10622 /* Add output to `deps_buffer' for the -M switch.
10623 STRING points to the text to be output.
10624 SPACER is ':' for targets, ' ' for dependencies. */
10627 deps_output (string, spacer)
10631 int size = quote_string_for_make ((char *) 0, string);
10636 #ifndef MAX_OUTPUT_COLUMNS
10637 #define MAX_OUTPUT_COLUMNS 72
10639 if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
10640 && 1 < deps_column) {
10641 bcopy (" \\\n ", &deps_buffer[deps_size], 4);
10648 if (deps_size + 2 * size + 8 > deps_allocated_size) {
10649 deps_allocated_size = (deps_size + 2 * size + 50) * 2;
10650 deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
10652 if (spacer == ' ') {
10653 deps_buffer[deps_size++] = ' ';
10656 quote_string_for_make (&deps_buffer[deps_size], string);
10658 deps_column += size;
10659 if (spacer == ':') {
10660 deps_buffer[deps_size++] = ':';
10663 deps_buffer[deps_size] = 0;
10667 fatal VPROTO ((char * msg, ...))
10669 #ifndef ANSI_PROTOTYPES
10674 fprintf (stderr, "%s: ", progname);
10675 VA_START (args, msg);
10677 #ifndef ANSI_PROTOTYPES
10678 msg = va_arg (args, char *);
10681 vfprintf (stderr, msg, args);
10683 fprintf (stderr, "\n");
10684 exit (FATAL_EXIT_CODE);
10687 /* More 'friendly' abort that prints the line and file.
10688 config.h can #define abort fancy_abort if you like that sort of thing. */
10693 fatal ("Internal gcc abort.");
10697 perror_with_name (name)
10700 fprintf (stderr, "%s: %s: %s\n", progname, name, my_strerror (errno));
10705 pfatal_with_name (name)
10708 perror_with_name (name);
10712 exit (FATAL_EXIT_CODE);
10716 /* Handler for SIGPIPE. */
10719 pipe_closed (signo)
10720 /* If this is missing, some compilers complain. */
10721 int signo ATTRIBUTE_UNUSED;
10723 fatal ("output pipe has been closed");
10729 fatal ("Memory exhausted.");
10736 register PTR ptr = (PTR) malloc (size);
10743 xrealloc (old, size)
10747 register PTR ptr = (PTR) realloc (old, size);
10754 xcalloc (number, size)
10755 size_t number, size;
10757 register size_t total = number * size;
10758 register PTR ptr = (PTR) malloc (total);
10761 bzero (ptr, total);
10769 size_t size = strlen (input);
10770 char *output = xmalloc (size + 1);
10771 strcpy (output, input);
10777 /* Under VMS we need to fix up the "include" specification filename.
10779 Rules for possible conversions
10781 fullname tried paths
10784 ./dir/name [.dir]name
10786 /name [000000]name, name
10787 dir/name dir:[000000]name, dir:name, dir/name
10788 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10789 path:/name path:[000000]name, path:name
10790 path:/dir/name path:[000000.dir]name, path:[dir]name
10791 path:dir/name path:[dir]name
10792 [path]:[dir]name [path.dir]name
10793 path/[dir]name [path.dir]name
10795 The path:/name input is constructed when expanding <> includes.
10797 return 1 if name was changed, 0 else. */
10800 hack_vms_include_specification (fullname, vaxc_include)
10804 register char *basename, *unixname, *local_ptr, *first_slash;
10805 int f, check_filename_before_returning, must_revert;
10808 check_filename_before_returning = 0;
10810 /* See if we can find a 1st slash. If not, there's no path information. */
10811 first_slash = index (fullname, '/');
10812 if (first_slash == 0)
10813 return 0; /* Nothing to do!!! */
10815 /* construct device spec if none given. */
10817 if (index (fullname, ':') == 0)
10820 /* If fullname has a slash, take it as device spec. */
10822 if (first_slash == fullname)
10824 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
10826 *first_slash = ':'; /* make device spec */
10827 for (basename = fullname; *basename != 0; basename++)
10828 *basename = *(basename+1); /* remove leading slash */
10830 else if ((first_slash[-1] != '.') /* keep ':/', './' */
10831 && (first_slash[-1] != ':')
10832 && (first_slash[-1] != ']')) /* or a vms path */
10834 *first_slash = ':';
10836 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
10837 && (first_slash[-1] == '.'))
10841 /* Get part after first ':' (basename[-1] == ':')
10842 or last '/' (basename[-1] == '/'). */
10844 basename = base_name (fullname);
10847 * Check if we have a vax-c style '#include filename'
10848 * and add the missing .h
10851 if (vaxc_include && !index (basename,'.'))
10852 strcat (basename, ".h");
10854 local_ptr = Local; /* initialize */
10856 /* We are trying to do a number of things here. First of all, we are
10857 trying to hammer the filenames into a standard format, such that later
10858 processing can handle them.
10860 If the file name contains something like [dir.], then it recognizes this
10861 as a root, and strips the ".]". Later processing will add whatever is
10862 needed to get things working properly.
10864 If no device is specified, then the first directory name is taken to be
10865 a device name (or a rooted logical). */
10867 /* Point to the UNIX filename part (which needs to be fixed!)
10868 but skip vms path information.
10869 [basename != fullname since first_slash != 0]. */
10871 if ((basename[-1] == ':') /* vms path spec. */
10872 || (basename[-1] == ']')
10873 || (basename[-1] == '>'))
10874 unixname = basename;
10876 unixname = fullname;
10878 if (*unixname == '/')
10881 /* If the directory spec is not rooted, we can just copy
10882 the UNIX filename part and we are done. */
10884 if (((basename - fullname) > 1)
10885 && ( (basename[-1] == ']')
10886 || (basename[-1] == '>')))
10888 if (basename[-2] != '.')
10891 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10892 -> PATH]:/name (basename = '/name', unixname = 'name')
10893 We strip the `]', and then splice the two parts of the name in the
10894 usual way. Given the default locations for include files in cccp.c,
10895 we will only use this code if the user specifies alternate locations
10896 with the /include (-I) switch on the command line. */
10898 basename -= 1; /* Strip "]" */
10899 unixname--; /* backspace */
10904 /* The VMS part has a ".]" at the end, and this will not do. Later
10905 processing will add a second directory spec, and this would be a syntax
10906 error. Thus we strip the ".]", and thus merge the directory specs.
10907 We also backspace unixname, so that it points to a '/'. This inhibits the
10908 generation of the 000000 root directory spec (which does not belong here
10911 basename -= 2; /* Strip ".]" */
10912 unixname--; /* backspace */
10920 /* We drop in here if there is no VMS style directory specification yet.
10921 If there is no device specification either, we make the first dir a
10922 device and try that. If we do not do this, then we will be essentially
10923 searching the users default directory (as if they did a #include "asdf.h").
10925 Then all we need to do is to push a '[' into the output string. Later
10926 processing will fill this in, and close the bracket. */
10928 if ((unixname != fullname) /* vms path spec found. */
10929 && (basename[-1] != ':'))
10930 *local_ptr++ = ':'; /* dev not in spec. take first dir */
10932 *local_ptr++ = '['; /* Open the directory specification */
10935 if (unixname == fullname) /* no vms dir spec. */
10938 if ((first_slash != 0) /* unix dir spec. */
10939 && (*unixname != '/') /* not beginning with '/' */
10940 && (*unixname != '.')) /* or './' or '../' */
10941 *local_ptr++ = '.'; /* dir is local ! */
10944 /* at this point we assume that we have the device spec, and (at least
10945 the opening "[" for a directory specification. We may have directories
10948 If there are no other slashes then the filename will be
10949 in the "root" directory. Otherwise, we need to add
10950 directory specifications. */
10952 if (index (unixname, '/') == 0)
10954 /* if no directories specified yet and none are following. */
10955 if (local_ptr[-1] == '[')
10957 /* Just add "000000]" as the directory string */
10958 strcpy (local_ptr, "000000]");
10959 local_ptr += strlen (local_ptr);
10960 check_filename_before_returning = 1; /* we might need to fool with this later */
10966 /* As long as there are still subdirectories to add, do them. */
10967 while (index (unixname, '/') != 0)
10969 /* If this token is "." we can ignore it
10970 if it's not at the beginning of a path. */
10971 if ((unixname[0] == '.') && (unixname[1] == '/'))
10973 /* remove it at beginning of path. */
10974 if ( ((unixname == fullname) /* no device spec */
10975 && (fullname+2 != basename)) /* starts with ./ */
10977 || ((basename[-1] == ':') /* device spec */
10978 && (unixname-1 == basename))) /* and ./ afterwards */
10979 *local_ptr++ = '.'; /* make '[.' start of path. */
10984 /* Add a subdirectory spec. Do not duplicate "." */
10985 if ( local_ptr[-1] != '.'
10986 && local_ptr[-1] != '['
10987 && local_ptr[-1] != '<')
10988 *local_ptr++ = '.';
10990 /* If this is ".." then the spec becomes "-" */
10991 if ( (unixname[0] == '.')
10992 && (unixname[1] == '.')
10993 && (unixname[2] == '/'))
10995 /* Add "-" and skip the ".." */
10996 if ((local_ptr[-1] == '.')
10997 && (local_ptr[-2] == '['))
10998 local_ptr--; /* prevent [.- */
10999 *local_ptr++ = '-';
11004 /* Copy the subdirectory */
11005 while (*unixname != '/')
11006 *local_ptr++= *unixname++;
11008 unixname++; /* Skip the "/" */
11011 /* Close the directory specification */
11012 if (local_ptr[-1] == '.') /* no trailing periods */
11015 if (local_ptr[-1] == '[') /* no dir needed */
11018 *local_ptr++ = ']';
11021 /* Now add the filename. */
11024 *local_ptr++ = *unixname++;
11027 /* Now append it to the original VMS spec. */
11029 strcpy ((must_revert==1)?fullname:basename, Local);
11031 /* If we put a [000000] in the filename, try to open it first. If this fails,
11032 remove the [000000], and return that name. This provides flexibility
11033 to the user in that they can use both rooted and non-rooted logical names
11034 to point to the location of the file. */
11036 if (check_filename_before_returning)
11038 f = open (fullname, O_RDONLY, 0666);
11041 /* The file name is OK as it is, so return it as is. */
11046 /* The filename did not work. Try to remove the [000000] from the name,
11049 basename = index (fullname, '[');
11050 local_ptr = index (fullname, ']') + 1;
11051 strcpy (basename, local_ptr); /* this gets rid of it */
11061 /* The following wrapper functions supply additional arguments to the VMS
11062 I/O routines to optimize performance with file handling. The arguments
11064 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11065 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11066 "fop=tef"- Truncate unused portions of file when closing file.
11067 "shr=nil"- Disallow file sharing while file is open. */
11070 VMS_freopen (fname, type, oldfile)
11075 #undef freopen /* Get back the real freopen routine. */
11076 if (strcmp (type, "w") == 0)
11077 return freopen (fname, type, oldfile,
11078 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11079 return freopen (fname, type, oldfile, "mbc=16");
11083 VMS_fopen (fname, type)
11087 #undef fopen /* Get back the real fopen routine. */
11088 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11089 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11090 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11091 FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
11094 return (*vmslib_fopen) (fname, type, "mbc=32",
11095 "deq=64", "fop=tef", "shr=nil");
11097 return (*vmslib_fopen) (fname, type, "mbc=32");
11101 VMS_open (fname, flags, prot)
11106 #undef open /* Get back the real open routine. */
11107 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
11110 /* more VMS hackery */
11114 extern unsigned long SYS$PARSE(), SYS$SEARCH();
11116 /* Work around another library bug. If a file is located via a searchlist,
11117 and if the device it's on is not the same device as the one specified
11118 in the first element of that searchlist, then both stat() and fstat()
11119 will fail to return info about it. `errno' will be set to EVMSERR, and
11120 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11121 We can get around this by fully parsing the filename and then passing
11122 that absolute name to stat().
11124 Without this fix, we can end up failing to find header files, which is
11125 bad enough, but then compounding the problem by reporting the reason for
11126 failure as "normal successful completion." */
11128 #undef fstat /* Get back to the library version. */
11131 VMS_fstat (fd, statbuf)
11133 struct stat *statbuf;
11135 int result = fstat (fd, statbuf);
11140 char nambuf[NAM$C_MAXRSS+1];
11142 if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
11143 result = VMS_stat (nambuf, statbuf);
11144 /* No fclose(fp) here; that would close(fd) as well. */
11151 VMS_stat (name, statbuf)
11153 struct stat *statbuf;
11155 int result = stat (name, statbuf);
11161 char exp_nam[NAM$C_MAXRSS+1], /* expanded name buffer for SYS$PARSE */
11162 res_nam[NAM$C_MAXRSS+1]; /* resultant name buffer for SYS$SEARCH */
11165 fab.fab$l_fna = (char *) name;
11166 fab.fab$b_fns = (unsigned char) strlen (name);
11167 fab.fab$l_nam = (void *) &nam;
11169 nam.nam$l_esa = exp_nam, nam.nam$b_ess = sizeof exp_nam - 1;
11170 nam.nam$l_rsa = res_nam, nam.nam$b_rss = sizeof res_nam - 1;
11171 nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
11172 if (SYS$PARSE (&fab) & 1)
11174 if (SYS$SEARCH (&fab) & 1)
11176 res_nam[nam.nam$b_rsl] = '\0';
11177 result = stat (res_nam, statbuf);
11179 /* Clean up searchlist context cached by the system. */
11180 nam.nam$b_nop = NAM$M_SYNCHK;
11181 fab.fab$l_fna = 0, fab.fab$b_fns = 0;
11182 (void) SYS$PARSE (&fab);