1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-96, 1997 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.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 typedef unsigned char U_CHAR;
29 #include "../src/config.h"
37 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
38 for the sake of machines with limited C compilers. */
41 #endif /* not EMACS */
43 #ifndef STANDARD_INCLUDE_DIR
44 #define STANDARD_INCLUDE_DIR "/usr/include"
49 /* By default, colon separates directories in a path. */
50 #ifndef PATH_SEPARATOR
51 #define PATH_SEPARATOR ':'
54 /* By default, the suffix for object files is ".o". */
56 #define HAVE_OBJECT_SUFFIX
58 #define OBJECT_SUFFIX ".o"
61 #include <sys/types.h>
67 /* The following symbols should be autoconfigured:
72 In the mean time, we'll get by with approximations based
73 on existing GCC configuration symbols. */
77 # define STDC_HEADERS 1
79 #endif /* defined (POSIX) */
81 #if defined (POSIX) || (defined (USG) && !defined (VMS))
83 # define HAVE_FCNTL_H 1
90 # if TIME_WITH_SYS_TIME
91 # include <sys/time.h>
95 # include <sys/time.h>
100 # include <sys/resource.h>
122 # define bcmp(a, b, n) memcmp (a, b, n)
125 # define bcopy(s, d, n) memcpy (d, s, n)
128 # define bzero(d, n) memset (d, 0, n)
130 #else /* !STDC_HEADERS */
134 # if !defined (BSTRING) && (defined (USG) || defined (VMS))
137 # define bcmp my_bcmp
150 # endif /* !defined (bcmp) */
153 # define bcopy my_bcopy
163 # endif /* !defined (bcopy) */
166 # define bzero my_bzero
170 register unsigned length;
175 # endif /* !defined (bzero) */
177 # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
178 #endif /* ! STDC_HEADERS */
180 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
181 # define __attribute__(x)
185 # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
186 # define PROTO(ARGS) ARGS
188 # define PROTO(ARGS) ()
192 #if defined (__STDC__) && defined (HAVE_VPRINTF)
194 # define VA_START(va_list, var) va_start (va_list, var)
195 # define PRINTF_ALIST(msg) char *msg, ...
196 # define PRINTF_DCL(msg)
197 # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (__printf__, m, n)))
199 # include <varargs.h>
200 # define VA_START(va_list, var) va_start (va_list)
201 # define PRINTF_ALIST(msg) msg, va_alist
202 # define PRINTF_DCL(msg) char *msg; va_dcl
203 # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (__printf__, m, n)))
204 # define vfprintf(file, msg, args) \
206 char *a0 = va_arg(args, char *); \
207 char *a1 = va_arg(args, char *); \
208 char *a2 = va_arg(args, char *); \
209 char *a3 = va_arg(args, char *); \
210 fprintf (file, msg, a0, a1, a2, a3); \
214 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
215 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
216 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
222 /* VMS-specific definitions */
225 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
226 #define fopen(fname,mode) VMS_fopen (fname,mode)
227 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
228 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
229 static int VMS_fstat (), VMS_stat ();
230 static int VMS_open ();
231 static FILE *VMS_fopen ();
232 static FILE *VMS_freopen ();
233 static void hack_vms_include_specification ();
234 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
235 #define INO_T_HASH(a) 0
236 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
239 /* Windows does not natively support inodes, and neither does MSDOS. */
240 #if (defined (_WIN32) && ! defined (CYGWIN32)) || defined (__MSDOS__)
241 #define INO_T_EQ(a, b) 0
250 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
251 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
253 /* Find the largest host integer type and set its size and type.
254 Watch out: on some crazy hosts `long' is shorter than `int'. */
256 #ifndef HOST_WIDE_INT
258 # include <inttypes.h>
259 # define HOST_WIDE_INT intmax_t
261 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
262 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
263 # define HOST_WIDE_INT int
265 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
266 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
267 # define HOST_WIDE_INT long
269 # define HOST_WIDE_INT long long
276 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
280 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
284 #define INO_T_EQ(a, b) ((a) == (b))
288 #define INO_T_HASH(a) (a)
291 /* Define a generic NULL if one hasn't already been defined. */
298 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
299 #define GENERIC_PTR void *
301 #define GENERIC_PTR char *
306 #define NULL_PTR ((GENERIC_PTR) 0)
309 #ifndef INCLUDE_LEN_FUDGE
310 #define INCLUDE_LEN_FUDGE 0
313 /* External declarations. */
315 extern char *version_string;
316 extern char *update_path PROTO((char *, char *));
318 #ifndef HAVE_STRERROR
320 extern char *sys_errlist[];
321 #else /* HAVE_STRERROR */
325 char *strerror (int,...);
327 HOST_WIDE_INT parse_escape PROTO((char **, HOST_WIDE_INT));
328 HOST_WIDE_INT parse_c_expression PROTO((char *));
334 /* Name under which this program was invoked. */
336 static char *progname;
338 /* Nonzero means use extra default include directories for C++. */
340 static int cplusplus;
342 /* Nonzero means handle cplusplus style comments */
344 static int cplusplus_comments;
346 /* Nonzero means handle #import, for objective C. */
350 /* Nonzero means this is an assembly file, and allow
351 unknown directives, which could be comments. */
355 /* Current maximum length of directory names in the search path
356 for include files. (Altered as we get more of them.) */
358 static int max_include_len;
360 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
362 static int for_lint = 0;
364 /* Nonzero means copy comments into the output file. */
366 static int put_out_comments = 0;
368 /* Nonzero means don't process the ANSI trigraph sequences. */
370 static int no_trigraphs = 0;
372 /* Nonzero means print the names of included files rather than
373 the preprocessed output. 1 means just the #include "...",
374 2 means #include <...> as well. */
376 static int print_deps = 0;
378 /* Nonzero if missing .h files in -M output are assumed to be generated
379 files and not errors. */
381 static int print_deps_missing_files = 0;
383 /* Nonzero means print names of header files (-H). */
385 static int print_include_names = 0;
387 /* Nonzero means don't output line number information. */
389 static int no_line_directives;
391 /* Nonzero means output the text in failing conditionals,
392 inside #failed ... #endfailed. */
394 static int output_conditionals;
396 /* dump_only means inhibit output of the preprocessed text
397 and instead output the definitions of all user-defined
398 macros in a form suitable for use as input to cccp.
399 dump_names means pass #define and the macro name through to output.
400 dump_definitions means pass the whole definition (plus #define) through
403 static enum {dump_none, dump_only, dump_names, dump_definitions}
404 dump_macros = dump_none;
406 /* Nonzero means pass all #define and #undef directives which we actually
407 process through to the output stream. This feature is used primarily
408 to allow cc1 to record the #defines and #undefs for the sake of
409 debuggers which understand about preprocessor macros, but it may
410 also be useful with -E to figure out how symbols are defined, and
411 where they are defined. */
412 static int debug_output = 0;
414 /* Nonzero means pass #include lines through to the output,
415 even if they are ifdeffed out. */
416 static int dump_includes;
418 /* Nonzero indicates special processing used by the pcp program. The
419 special effects of this mode are:
421 Inhibit all macro expansion, except those inside #if directives.
423 Process #define directives normally, and output their contents
426 Output preconditions to pcp_outfile indicating all the relevant
427 preconditions for use of this file in a later cpp run.
429 static FILE *pcp_outfile;
431 /* Nonzero means we are inside an IF during a -pcp run. In this mode
432 macro expansion is done, and preconditions are output for all macro
433 uses requiring them. */
434 static int pcp_inside_if;
436 /* Nonzero means never to include precompiled files.
437 This is 1 since there's no way now to make precompiled files,
438 so it's not worth testing for them. */
439 static int no_precomp = 1;
441 /* Nonzero means give all the error messages the ANSI standard requires. */
445 /* Nonzero means try to make failure to fit ANSI C an error. */
447 static int pedantic_errors;
449 /* Nonzero means don't print warning messages. -w. */
451 static int inhibit_warnings = 0;
453 /* Nonzero means warn if slash-star appears in a slash-star comment,
454 or if newline-backslash appears in a slash-slash comment. */
456 static int warn_comments;
458 /* Nonzero means warn if a macro argument is (or would be)
459 stringified with -traditional. */
461 static int warn_stringify;
463 /* Nonzero means warn if there are any trigraphs. */
465 static int warn_trigraphs;
467 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
471 /* Nonzero means warn if #import is used. */
473 static int warn_import = 1;
475 /* Nonzero means turn warnings into errors. */
477 static int warnings_are_errors;
479 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
483 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
487 /* Nonzero causes output not to be done,
488 but directives such as #define that have side effects
491 static int no_output;
493 /* Nonzero means this file was included with a -imacros or -include
494 command line and should not be recorded as an include file. */
496 static int no_record_file;
498 /* Nonzero means that we have finished processing the command line options.
499 This flag is used to decide whether or not to issue certain errors
502 static int done_initializing = 0;
504 /* Line where a newline was first seen in a string constant. */
506 static int multiline_string_line = 0;
508 /* I/O buffer structure.
509 The `fname' field is nonzero for source files and #include files
510 and for the dummy text used for -D and -U.
511 It is zero for rescanning results of macro expansion
512 and for expanding macro arguments. */
513 #define INPUT_STACK_MAX 400
514 static struct file_buf {
516 /* Filename specified with #line directive. */
518 /* Include file description. */
519 struct include_file *inc;
520 /* Record where in the search path this file was found.
521 For #include_next. */
522 struct file_name_list *dir;
527 /* Macro that this level is the expansion of.
528 Included so that we can reenable the macro
529 at the end of this level. */
530 struct hashnode *macro;
531 /* Value of if_stack at start of this file.
532 Used to prohibit unmatched #endif (etc) in an include file. */
533 struct if_stack *if_stack;
534 /* Object to be freed at end of input at this level. */
536 /* True if this is a system header file; see is_system_include. */
537 char system_header_p;
538 } instack[INPUT_STACK_MAX];
540 static int last_error_tick; /* Incremented each time we print it. */
541 static int input_file_stack_tick; /* Incremented when the status changes. */
543 /* Current nesting level of input sources.
544 `instack[indepth]' is the level currently being read. */
545 static int indepth = -1;
546 #define CHECK_DEPTH(code) \
547 if (indepth >= (INPUT_STACK_MAX - 1)) \
549 error_with_line (line_for_error (instack[indepth].lineno), \
550 "macro or `#include' recursion too deep"); \
554 /* Current depth in #include directives that use <...>. */
555 static int system_include_depth = 0;
557 typedef struct file_buf FILE_BUF;
559 /* The output buffer. Its LENGTH field is the amount of room allocated
560 for the buffer, not the number of chars actually present. To get
561 that, subtract outbuf.buf from outbuf.bufp. */
563 #define OUTBUF_SIZE 10 /* initial size of output buffer */
564 static FILE_BUF outbuf;
566 /* Grow output buffer OBUF points at
567 so it can hold at least NEEDED more chars. */
569 #define check_expand(OBUF, NEEDED) \
570 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
571 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
573 struct file_name_list
575 struct file_name_list *next;
576 /* If the following is 1, it is a C-language system include
578 int c_system_include_path;
579 /* Mapping of file names for this directory. */
580 struct file_name_map *name_map;
581 /* Non-zero if name_map is valid. */
583 /* The include directory status. */
585 /* The include prefix: "" denotes the working directory,
586 otherwise fname must end in '/'.
587 The actual size is dynamically allocated. */
591 /* #include "file" looks in source file dir, then stack. */
592 /* #include <file> just looks in the stack. */
593 /* -I directories are added to the end, then the defaults are added. */
595 static struct default_include {
596 char *fname; /* The name of the directory. */
597 char *component; /* The component containing the directory */
598 int cplusplus; /* Only look here if we're compiling C++. */
599 int cxx_aware; /* Includes in this directory don't need to
600 be wrapped in extern "C" when compiling
602 } include_defaults_array[]
603 #ifdef INCLUDE_DEFAULTS
607 /* Pick up GNU C++ specific include files. */
608 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
609 { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 },
611 /* This is the dir for fixincludes. Put it just before
612 the files that we fix. */
613 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
614 /* For cross-compilation, this dir name is generated
615 automatically in Makefile.in. */
616 { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
617 #ifdef TOOL_INCLUDE_DIR
618 /* This is another place that the target system's headers might be. */
619 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
621 #else /* not CROSS_COMPILE */
622 #ifdef LOCAL_INCLUDE_DIR
623 /* This should be /usr/local/include and should come before
624 the fixincludes-fixed header files. */
625 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
627 #ifdef TOOL_INCLUDE_DIR
628 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
629 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
630 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 },
632 /* This is the dir for fixincludes. Put it just before
633 the files that we fix. */
634 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
635 /* Some systems have an extra dir of include files. */
636 #ifdef SYSTEM_INCLUDE_DIR
637 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
639 #ifndef STANDARD_INCLUDE_COMPONENT
640 #define STANDARD_INCLUDE_COMPONENT 0
642 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
643 #endif /* not CROSS_COMPILE */
646 #endif /* no INCLUDE_DEFAULTS */
648 /* The code looks at the defaults through this pointer, rather than through
649 the constant structure above. This pointer gets changed if an environment
650 variable specifies other defaults. */
651 static struct default_include *include_defaults = include_defaults_array;
653 static struct file_name_list *include = 0; /* First dir to search */
654 /* First dir to search for <file> */
655 /* This is the first element to use for #include <...>.
656 If it is 0, use the entire chain for such includes. */
657 static struct file_name_list *first_bracket_include = 0;
658 /* This is the first element in the chain that corresponds to
659 a directory of system header files. */
660 static struct file_name_list *first_system_include = 0;
661 static struct file_name_list *last_include = 0; /* Last in chain */
663 /* Chain of include directories to put at the end of the other chain. */
664 static struct file_name_list *after_include = 0;
665 static struct file_name_list *last_after_include = 0; /* Last in chain */
667 /* Chain to put at the start of the system include files. */
668 static struct file_name_list *before_system = 0;
669 static struct file_name_list *last_before_system = 0; /* Last in chain */
671 /* Directory prefix that should replace `/usr' in the standard
672 include file directories. */
673 static char *include_prefix;
675 /* Maintain and search list of included files. */
677 struct include_file {
678 struct include_file *next; /* for include_hashtab */
679 struct include_file *next_ino; /* for include_ino_hashtab */
681 /* If the following is the empty string, it means #pragma once
682 was seen in this include file, or #import was applied to the file.
683 Otherwise, if it is nonzero, it is a macro name.
684 Don't include the file again if that macro is defined. */
685 U_CHAR *control_macro;
686 /* Nonzero if the dependency on this include file has been output. */
691 /* Hash tables of files already included with #include or #import.
692 include_hashtab is by full name; include_ino_hashtab is by inode number. */
694 #define INCLUDE_HASHSIZE 61
695 static struct include_file *include_hashtab[INCLUDE_HASHSIZE];
696 static struct include_file *include_ino_hashtab[INCLUDE_HASHSIZE];
698 /* Global list of strings read in from precompiled files. This list
699 is kept in the order the strings are read in, with new strings being
700 added at the end through stringlist_tailp. We use this list to output
701 the strings at the end of the run.
703 static STRINGDEF *stringlist;
704 static STRINGDEF **stringlist_tailp = &stringlist;
707 /* Structure returned by create_definition */
708 typedef struct macrodef MACRODEF;
711 struct definition *defn;
716 enum sharp_token_type {
717 NO_SHARP_TOKEN = 0, /* token not present */
719 SHARP_TOKEN = '#', /* token spelled with # only */
720 WHITE_SHARP_TOKEN, /* token spelled with # and white space */
722 PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */
723 WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */
726 /* Structure allocated for every #define. For a simple replacement
729 nargs = -1, the `pattern' list is null, and the expansion is just
730 the replacement text. Nargs = 0 means a functionlike macro with no args,
732 #define getchar() getc (stdin) .
733 When there are args, the expansion is the replacement text with the
734 args squashed out, and the reflist is a list describing how to
735 build the output from the input: e.g., "3 chars, then the 1st arg,
736 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
737 The chars here come from the expansion. Whatever is left of the
738 expansion after the last arg-occurrence is copied after that arg.
739 Note that the reflist can be arbitrarily long---
740 its length depends on the number of times the arguments appear in
741 the replacement text, not how many args there are. Example:
742 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
744 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
745 where (x, y) means (nchars, argno). */
747 typedef struct definition DEFINITION;
750 int length; /* length of expansion string */
751 int predefined; /* True if the macro was builtin or */
752 /* came from the command line */
754 int line; /* Line number of definition */
755 char *file; /* File of definition */
756 char rest_args; /* Nonzero if last arg. absorbs the rest */
758 struct reflist *next;
760 enum sharp_token_type stringify; /* set if a # operator before arg */
761 enum sharp_token_type raw_before; /* set if a ## operator before arg */
762 enum sharp_token_type raw_after; /* set if a ## operator after arg */
764 char rest_args; /* Nonzero if this arg. absorbs the rest */
765 int nchars; /* Number of literal chars to copy before
766 this arg occurrence. */
767 int argno; /* Number of arg to substitute (origin-0) */
770 /* Names of macro args, concatenated in reverse order
771 with comma-space between them.
772 The only use of this is that we warn on redefinition
773 if this differs between the old and new definitions. */
778 /* different kinds of things that can appear in the value field
779 of a hash node. Actually, this may be useless now. */
787 * special extension string that can be added to the last macro argument to
788 * allow it to absorb the "rest" of the arguments when expanded. Ex:
789 * #define wow(a, b...) process (b, a, b)
790 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
791 * { wow (one, two); } -> { process (two, one, two); }
792 * if this "rest_arg" is used with the concat token '##' and if it is not
793 * supplied then the token attached to with ## will not be outputted. Ex:
794 * #define wow (a, b...) process (b ## , a, ## b)
795 * { wow (1, 2); } -> { process (2, 1, 2); }
796 * { wow (one); } -> { process (one); {
798 static char rest_extension[] = "...";
799 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
801 /* The structure of a node in the hash table. The hash table
802 has entries for all tokens defined by #define directives (type T_MACRO),
803 plus some special tokens like __LINE__ (these each have their own
804 type, and the appropriate code is run when that type of node is seen.
805 It does not contain control words like "#define", which are recognized
806 by a separate piece of code. */
808 /* different flavors of hash nodes --- also used in keyword table */
810 T_DEFINE = 1, /* the `#define' keyword */
811 T_INCLUDE, /* the `#include' keyword */
812 T_INCLUDE_NEXT, /* the `#include_next' keyword */
813 T_IMPORT, /* the `#import' keyword */
814 T_IFDEF, /* the `#ifdef' keyword */
815 T_IFNDEF, /* the `#ifndef' keyword */
816 T_IF, /* the `#if' keyword */
817 T_ELSE, /* `#else' */
818 T_PRAGMA, /* `#pragma' */
819 T_ELIF, /* `#elif' */
820 T_UNDEF, /* `#undef' */
821 T_LINE, /* `#line' */
822 T_ERROR, /* `#error' */
823 T_WARNING, /* `#warning' */
824 T_ENDIF, /* `#endif' */
825 T_SCCS, /* `#sccs', used on system V. */
826 T_IDENT, /* `#ident', used on system V. */
827 T_ASSERT, /* `#assert', taken from system V. */
828 T_UNASSERT, /* `#unassert', taken from system V. */
829 T_SPECLINE, /* special symbol `__LINE__' */
830 T_DATE, /* `__DATE__' */
831 T_FILE, /* `__FILE__' */
832 T_BASE_FILE, /* `__BASE_FILE__' */
833 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
834 T_VERSION, /* `__VERSION__' */
835 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
836 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
837 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
838 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
839 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
840 T_IMMEDIATE_PREFIX_TYPE, /* `__IMMEDIATE_PREFIX__' */
841 T_TIME, /* `__TIME__' */
842 T_CONST, /* Constant value, used by `__STDC__' */
843 T_MACRO, /* macro defined by `#define' */
844 T_DISABLED, /* macro temporarily turned off for rescan */
845 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
846 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
847 T_UNUSED /* Used for something not defined. */
851 struct hashnode *next; /* double links for easy deletion */
852 struct hashnode *prev;
853 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
854 chain is kept, in case the node is the head
855 of the chain and gets deleted. */
856 enum node_type type; /* type of special token */
857 int length; /* length of token, for quick comparison */
858 U_CHAR *name; /* the actual name */
859 union hashval value; /* pointer to expansion, or whatever */
862 typedef struct hashnode HASHNODE;
864 /* Some definitions for the hash table. The hash function MUST be
865 computed as shown in hashf () below. That is because the rescan
866 loop computes the hash value `on the fly' for most tokens,
867 in order to avoid the overhead of a lot of procedure calls to
868 the hashf () function. Hashf () only exists for the sake of
869 politeness, for use when speed isn't so important. */
871 #define HASHSIZE 1403
872 static HASHNODE *hashtab[HASHSIZE];
873 #define HASHSTEP(old, c) ((old << 2) + c)
874 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
876 /* Symbols to predefine. */
878 #ifdef CPP_PREDEFINES
879 static char *predefs = CPP_PREDEFINES;
881 static char *predefs = "";
884 /* We let tm.h override the types used here, to handle trivial differences
885 such as the choice of unsigned int or long unsigned int for size_t.
886 When machines start needing nontrivial differences in the size type,
887 it would be best to do something here to figure out automatically
888 from other information what type to use. */
890 /* The string value for __SIZE_TYPE__. */
893 #define SIZE_TYPE "long unsigned int"
896 /* The string value for __PTRDIFF_TYPE__. */
899 #define PTRDIFF_TYPE "long int"
902 /* The string value for __WCHAR_TYPE__. */
905 #define WCHAR_TYPE "int"
907 char * wchar_type = WCHAR_TYPE;
910 /* The string value for __USER_LABEL_PREFIX__ */
912 #ifndef USER_LABEL_PREFIX
913 #define USER_LABEL_PREFIX ""
916 /* The string value for __REGISTER_PREFIX__ */
918 #ifndef REGISTER_PREFIX
919 #define REGISTER_PREFIX ""
922 /* The string value for __IMMEDIATE_PREFIX__ */
924 #ifndef IMMEDIATE_PREFIX
925 #define IMMEDIATE_PREFIX ""
928 /* In the definition of a #assert name, this structure forms
929 a list of the individual values asserted.
930 Each value is itself a list of "tokens".
931 These are strings that are compared by name. */
933 struct tokenlist_list {
934 struct tokenlist_list *next;
935 struct arglist *tokens;
938 struct assertion_hashnode {
939 struct assertion_hashnode *next; /* double links for easy deletion */
940 struct assertion_hashnode *prev;
941 /* also, a back pointer to this node's hash
942 chain is kept, in case the node is the head
943 of the chain and gets deleted. */
944 struct assertion_hashnode **bucket_hdr;
945 int length; /* length of token, for quick comparison */
946 U_CHAR *name; /* the actual name */
947 /* List of token-sequences. */
948 struct tokenlist_list *value;
951 typedef struct assertion_hashnode ASSERTION_HASHNODE;
953 /* Some definitions for the hash table. The hash function MUST be
954 computed as shown in hashf below. That is because the rescan
955 loop computes the hash value `on the fly' for most tokens,
956 in order to avoid the overhead of a lot of procedure calls to
957 the hashf function. hashf only exists for the sake of
958 politeness, for use when speed isn't so important. */
960 #define ASSERTION_HASHSIZE 37
961 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
963 /* Nonzero means inhibit macroexpansion of what seem to be
964 assertion tests, in rescan. For #if. */
965 static int assertions_flag;
967 /* `struct directive' defines one #-directive, including how to handle it. */
969 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
972 int length; /* Length of name */
973 int (*func) DO_PROTO; /* Function to handle directive */
974 char *name; /* Name of directive */
975 enum node_type type; /* Code which describes which directive. */
978 #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT)
980 /* These functions are declared to return int instead of void since they
981 are going to be placed in the table and some old compilers have trouble with
982 pointers to functions returning void. */
984 static int do_assert DO_PROTO;
985 static int do_define DO_PROTO;
986 static int do_elif DO_PROTO;
987 static int do_else DO_PROTO;
988 static int do_endif DO_PROTO;
989 static int do_error DO_PROTO;
990 static int do_ident DO_PROTO;
991 static int do_if DO_PROTO;
992 static int do_include DO_PROTO;
993 static int do_line DO_PROTO;
994 static int do_pragma DO_PROTO;
995 #ifdef SCCS_DIRECTIVE
996 static int do_sccs DO_PROTO;
998 static int do_unassert DO_PROTO;
999 static int do_undef DO_PROTO;
1000 static int do_warning DO_PROTO;
1001 static int do_xifdef DO_PROTO;
1003 /* Here is the actual list of #-directives, most-often-used first. */
1005 static struct directive directive_table[] = {
1006 { 6, do_define, "define", T_DEFINE},
1007 { 2, do_if, "if", T_IF},
1008 { 5, do_xifdef, "ifdef", T_IFDEF},
1009 { 6, do_xifdef, "ifndef", T_IFNDEF},
1010 { 5, do_endif, "endif", T_ENDIF},
1011 { 4, do_else, "else", T_ELSE},
1012 { 4, do_elif, "elif", T_ELIF},
1013 { 4, do_line, "line", T_LINE},
1014 { 7, do_include, "include", T_INCLUDE},
1015 { 12, do_include, "include_next", T_INCLUDE_NEXT},
1016 { 6, do_include, "import", T_IMPORT},
1017 { 5, do_undef, "undef", T_UNDEF},
1018 { 5, do_error, "error", T_ERROR},
1019 { 7, do_warning, "warning", T_WARNING},
1020 #ifdef SCCS_DIRECTIVE
1021 { 4, do_sccs, "sccs", T_SCCS},
1023 { 6, do_pragma, "pragma", T_PRAGMA},
1024 { 5, do_ident, "ident", T_IDENT},
1025 { 6, do_assert, "assert", T_ASSERT},
1026 { 8, do_unassert, "unassert", T_UNASSERT},
1027 { -1, 0, "", T_UNUSED},
1030 /* When a directive handler is called,
1031 this points to the # (or the : of the %:) that started the directive. */
1032 U_CHAR *directive_start;
1034 /* table to tell if char can be part of a C identifier. */
1035 U_CHAR is_idchar[256];
1036 /* table to tell if char can be first char of a c identifier. */
1037 U_CHAR is_idstart[256];
1038 /* table to tell if c is horizontal space. */
1039 static U_CHAR is_hor_space[256];
1040 /* table to tell if c is horizontal or vertical space. */
1041 U_CHAR is_space[256];
1042 /* names of some characters */
1043 static char *char_name[256];
1045 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
1046 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
1048 static int errors = 0; /* Error counter for exit code */
1050 /* Name of output file, for error messages. */
1051 static char *out_fname;
1054 /* Stack of conditionals currently in progress
1055 (including both successful and failing conditionals). */
1058 struct if_stack *next; /* for chaining to the next stack frame */
1059 char *fname; /* copied from input when frame is made */
1060 int lineno; /* similarly */
1061 int if_succeeded; /* true if a leg of this if-group
1062 has been passed through rescan */
1063 U_CHAR *control_macro; /* For #ifndef at start of file,
1064 this is the macro name tested. */
1065 enum node_type type; /* type of last directive seen in this group */
1067 typedef struct if_stack IF_STACK_FRAME;
1068 static IF_STACK_FRAME *if_stack = NULL;
1070 /* Buffer of -M output. */
1071 static char *deps_buffer;
1073 /* Number of bytes allocated in above. */
1074 static int deps_allocated_size;
1076 /* Number of bytes used. */
1077 static int deps_size;
1079 /* Number of bytes since the last newline. */
1080 static int deps_column;
1082 /* Nonzero means -I- has been seen,
1083 so don't look for #include "foo" the source-file directory. */
1084 static int ignore_srcdir;
1086 static int safe_read PROTO((int, char *, int));
1087 static void safe_write PROTO((int, char *, int));
1089 int main PROTO((int, char **));
1091 static void path_include PROTO((char *));
1093 static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
1095 static void trigraph_pcp PROTO((FILE_BUF *));
1097 static void newline_fix PROTO((U_CHAR *));
1098 static void name_newline_fix PROTO((U_CHAR *));
1100 static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
1102 static void rescan PROTO((FILE_BUF *, int));
1104 static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
1106 static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
1108 static struct tm *timestamp PROTO((void));
1109 static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
1111 static int is_system_include PROTO((char *));
1112 static char *base_name PROTO((char *));
1113 static int absolute_filename PROTO((char *));
1114 static size_t simplify_filename PROTO((char *));
1116 static char *read_filename_string PROTO((int, FILE *));
1117 static struct file_name_map *read_name_map PROTO((char *));
1118 static int open_include_file PROTO((char *, struct file_name_list *, U_CHAR *, struct include_file **));
1119 static char *remap_include_file PROTO((char *, struct file_name_list *));
1120 static int lookup_ino_include PROTO((struct include_file *));
1122 static void finclude PROTO((int, struct include_file *, FILE_BUF *, int, struct file_name_list *));
1123 static void record_control_macro PROTO((struct include_file *, U_CHAR *));
1125 static char *check_precompiled PROTO((int, struct stat *, char *, char **));
1126 static int check_preconditions PROTO((char *));
1127 static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
1128 static void pcstring_used PROTO((HASHNODE *));
1129 static void write_output PROTO((void));
1130 static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
1132 static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
1134 static int check_macro_name PROTO((U_CHAR *, char *));
1135 static int compare_defs PROTO((DEFINITION *, DEFINITION *));
1136 static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
1138 static DEFINITION *collect_expansion PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
1140 int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
1141 static int compare_token_lists PROTO((struct arglist *, struct arglist *));
1143 static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
1144 static void free_token_list PROTO((struct arglist *));
1146 static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
1147 static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
1148 static void delete_assertion PROTO((ASSERTION_HASHNODE *));
1150 static void do_once PROTO((void));
1152 static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
1153 static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
1154 static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
1155 static void validate_else PROTO((U_CHAR *, U_CHAR *));
1157 static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
1158 static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
1159 static char *quote_string PROTO((char *, char *));
1160 static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
1162 /* Last arg to output_line_directive. */
1163 enum file_change_code {same_file, enter_file, leave_file};
1164 static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
1166 static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
1169 static char *macarg PROTO((struct argdata *, int));
1171 static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, int *, int *, int *, int));
1173 static int discard_comments PROTO((U_CHAR *, int, int));
1175 static int change_newlines PROTO((U_CHAR *, int));
1177 char *my_strerror PROTO((int));
1178 void error PRINTF_PROTO_1((char *, ...));
1179 static void verror PROTO((char *, va_list));
1180 static void error_from_errno PROTO((char *));
1181 void warning PRINTF_PROTO_1((char *, ...));
1182 static void vwarning PROTO((char *, va_list));
1183 static void error_with_line PRINTF_PROTO_2((int, char *, ...));
1184 static void verror_with_line PROTO((int, char *, va_list));
1185 static void vwarning_with_line PROTO((int, char *, va_list));
1186 static void warning_with_line PRINTF_PROTO_2((int, char *, ...));
1187 void pedwarn PRINTF_PROTO_1((char *, ...));
1188 void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
1189 static void pedwarn_with_file_and_line PRINTF_PROTO_3((char *, int, char *, ...));
1191 static void print_containing_files PROTO((void));
1193 static int line_for_error PROTO((int));
1194 static int grow_outbuf PROTO((FILE_BUF *, int));
1196 static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
1197 HASHNODE *lookup PROTO((U_CHAR *, int, int));
1198 static void delete_macro PROTO((HASHNODE *));
1199 static int hashf PROTO((U_CHAR *, int, int));
1201 static void dump_single_macro PROTO((HASHNODE *, FILE *));
1202 static void dump_all_macros PROTO((void));
1203 static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
1204 static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
1206 static void initialize_char_syntax PROTO((void));
1207 static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
1209 static void make_definition PROTO((char *, FILE_BUF *));
1210 static void make_undef PROTO((char *, FILE_BUF *));
1212 static void make_assertion PROTO((char *, char *));
1214 static struct file_name_list *new_include_prefix PROTO((struct file_name_list *, char *, char *, char *));
1215 static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
1217 static void deps_output PROTO((char *, int));
1219 static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
1220 void fancy_abort PROTO((void)) __attribute__ ((noreturn));
1221 static void perror_with_name PROTO((char *));
1222 static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
1223 static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
1225 static void memory_full PROTO((void)) __attribute__ ((noreturn));
1226 GENERIC_PTR xmalloc PROTO((size_t));
1227 static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
1228 static GENERIC_PTR xcalloc PROTO((size_t, size_t));
1229 static char *savestring PROTO((char *));
1231 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1232 retrying if necessary. If MAX_READ_LEN is defined, read at most
1233 that bytes at a time. Return a negative value if an error occurs,
1234 otherwise return the actual number of bytes read,
1235 which must be LEN unless end-of-file was reached. */
1238 safe_read (desc, ptr, len)
1243 int left, rcount, nchars;
1249 if (rcount > MAX_READ_LEN)
1250 rcount = MAX_READ_LEN;
1252 nchars = read (desc, ptr, rcount);
1269 /* Write LEN bytes at PTR to descriptor DESC,
1270 retrying if necessary, and treating any real error as fatal.
1271 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1274 safe_write (desc, ptr, len)
1279 int wcount, written;
1283 #ifdef MAX_WRITE_LEN
1284 if (wcount > MAX_WRITE_LEN)
1285 wcount = MAX_WRITE_LEN;
1287 written = write (desc, ptr, wcount);
1294 pfatal_with_name (out_fname);
1311 char **pend_files = (char **) xmalloc (argc * sizeof (char *));
1312 char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
1313 char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1314 char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1315 char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
1317 /* Record the option used with each element of pend_assertions.
1318 This is preparation for supporting more than one option for making
1320 char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
1321 int inhibit_predefs = 0;
1322 int no_standard_includes = 0;
1323 int no_standard_cplusplus_includes = 0;
1324 int missing_newline = 0;
1326 /* Non-0 means don't output the preprocessed program. */
1327 int inhibit_output = 0;
1328 /* Non-0 means -v, so print the full set of include dirs. */
1331 /* File name which deps are being written to.
1332 This is 0 if deps are being written to stdout. */
1333 char *deps_file = 0;
1334 /* Fopen file mode to open deps_file with. */
1335 char *deps_mode = "a";
1336 /* Stream on which to print the dependency information. */
1337 FILE *deps_stream = 0;
1338 /* Target-name to write with the dependency information. */
1339 char *deps_target = 0;
1342 /* Get rid of any avoidable limit on stack size. */
1346 /* Set the stack limit huge so that alloca (particularly stringtab
1347 in dbxread.c) does not fail. */
1348 getrlimit (RLIMIT_STACK, &rlim);
1349 rlim.rlim_cur = rlim.rlim_max;
1350 setrlimit (RLIMIT_STACK, &rlim);
1352 #endif /* RLIMIT_STACK defined */
1355 signal (SIGPIPE, pipe_closed);
1358 progname = base_name (argv[0]);
1362 /* Remove extension from PROGNAME. */
1364 char *s = progname = savestring (progname);
1366 if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */
1367 if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */
1368 && (p[1] == 'e' || p[1] == 'E')
1369 && (p[2] == 'x' || p[2] == 'X')
1370 && (p[3] == 'e' || p[3] == 'E')
1379 /* Initialize is_idchar. */
1380 initialize_char_syntax ();
1382 no_line_directives = 0;
1384 dump_macros = dump_none;
1387 cplusplus_comments = 1;
1389 bzero ((char *) pend_files, argc * sizeof (char *));
1390 bzero ((char *) pend_defs, argc * sizeof (char *));
1391 bzero ((char *) pend_undefs, argc * sizeof (char *));
1392 bzero ((char *) pend_assertions, argc * sizeof (char *));
1393 bzero ((char *) pend_includes, argc * sizeof (char *));
1395 /* Process switches and find input file name. */
1397 for (i = 1; i < argc; i++) {
1398 if (argv[i][0] != '-') {
1399 if (out_fname != NULL)
1400 fatal ("Usage: %s [switches] input output", argv[0]);
1401 else if (in_fname != NULL)
1402 out_fname = argv[i];
1406 switch (argv[i][1]) {
1409 if (!strcmp (argv[i], "-include")) {
1411 fatal ("Filename missing after `-include' option");
1413 simplify_filename (pend_includes[i] = argv[++i]);
1415 if (!strcmp (argv[i], "-imacros")) {
1417 fatal ("Filename missing after `-imacros' option");
1419 simplify_filename (pend_files[i] = argv[++i]);
1421 if (!strcmp (argv[i], "-iprefix")) {
1423 fatal ("Filename missing after `-iprefix' option");
1425 include_prefix = argv[++i];
1427 if (!strcmp (argv[i], "-ifoutput")) {
1428 output_conditionals = 1;
1430 if (!strcmp (argv[i], "-isystem")) {
1431 struct file_name_list *dirtmp;
1433 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1436 dirtmp->c_system_include_path = 1;
1438 if (before_system == 0)
1439 before_system = dirtmp;
1441 last_before_system->next = dirtmp;
1442 last_before_system = dirtmp; /* Tail follows the last one */
1444 /* Add directory to end of path for includes,
1445 with the default prefix at the front of its name. */
1446 if (!strcmp (argv[i], "-iwithprefix")) {
1447 struct file_name_list *dirtmp;
1450 if (include_prefix != 0)
1451 prefix = include_prefix;
1453 prefix = savestring (GCC_INCLUDE_DIR);
1454 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1455 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1456 prefix[strlen (prefix) - 7] = 0;
1459 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1460 prefix, argv[++i])))
1463 if (after_include == 0)
1464 after_include = dirtmp;
1466 last_after_include->next = dirtmp;
1467 last_after_include = dirtmp; /* Tail follows the last one */
1469 /* Add directory to main path for includes,
1470 with the default prefix at the front of its name. */
1471 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1472 struct file_name_list *dirtmp;
1475 if (include_prefix != 0)
1476 prefix = include_prefix;
1478 prefix = savestring (GCC_INCLUDE_DIR);
1479 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1480 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1481 prefix[strlen (prefix) - 7] = 0;
1484 dirtmp = new_include_prefix (NULL_PTR, NULL_PTR, prefix, argv[++i]);
1485 append_include_chain (dirtmp, dirtmp);
1487 /* Add directory to end of path for includes. */
1488 if (!strcmp (argv[i], "-idirafter")) {
1489 struct file_name_list *dirtmp;
1491 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1495 if (after_include == 0)
1496 after_include = dirtmp;
1498 last_after_include->next = dirtmp;
1499 last_after_include = dirtmp; /* Tail follows the last one */
1504 if (out_fname != NULL)
1505 fatal ("Output filename specified twice");
1507 fatal ("Filename missing after -o option");
1508 out_fname = argv[++i];
1509 if (!strcmp (out_fname, "-"))
1514 if (!strcmp (argv[i], "-pedantic"))
1516 else if (!strcmp (argv[i], "-pedantic-errors")) {
1518 pedantic_errors = 1;
1519 } else if (!strcmp (argv[i], "-pcp")) {
1522 fatal ("Filename missing after -pcp option");
1523 pcp_fname = argv[++i];
1525 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1526 ? fopen (pcp_fname, "w")
1528 if (pcp_outfile == 0)
1529 pfatal_with_name (pcp_fname);
1535 if (!strcmp (argv[i], "-traditional")) {
1537 cplusplus_comments = 0;
1538 } else if (!strcmp (argv[i], "-trigraphs")) {
1544 if (! strcmp (argv[i], "-lang-c"))
1545 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 0;
1546 if (! strcmp (argv[i], "-lang-c89"))
1547 cplusplus = 0, cplusplus_comments = 0, c89 = 1, objc = 0;
1548 if (! strcmp (argv[i], "-lang-c++"))
1549 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 0;
1550 if (! strcmp (argv[i], "-lang-objc"))
1551 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 1;
1552 if (! strcmp (argv[i], "-lang-objc++"))
1553 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 1;
1554 if (! strcmp (argv[i], "-lang-asm"))
1556 if (! strcmp (argv[i], "-lint"))
1561 cplusplus = 1, cplusplus_comments = 1;
1565 inhibit_warnings = 1;
1569 if (!strcmp (argv[i], "-Wtrigraphs"))
1571 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1573 else if (!strcmp (argv[i], "-Wcomment"))
1575 else if (!strcmp (argv[i], "-Wno-comment"))
1577 else if (!strcmp (argv[i], "-Wcomments"))
1579 else if (!strcmp (argv[i], "-Wno-comments"))
1581 else if (!strcmp (argv[i], "-Wtraditional"))
1583 else if (!strcmp (argv[i], "-Wno-traditional"))
1585 else if (!strcmp (argv[i], "-Wundef"))
1587 else if (!strcmp (argv[i], "-Wno-undef"))
1589 else if (!strcmp (argv[i], "-Wimport"))
1591 else if (!strcmp (argv[i], "-Wno-import"))
1593 else if (!strcmp (argv[i], "-Werror"))
1594 warnings_are_errors = 1;
1595 else if (!strcmp (argv[i], "-Wno-error"))
1596 warnings_are_errors = 0;
1597 else if (!strcmp (argv[i], "-Wall"))
1605 /* The style of the choices here is a bit mixed.
1606 The chosen scheme is a hybrid of keeping all options in one string
1607 and specifying each option in a separate argument:
1608 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1609 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1610 -M[M][G][D file]. This is awkward to handle in specs, and is not
1612 /* ??? -MG must be specified in addition to one of -M or -MM.
1613 This can be relaxed in the future without breaking anything.
1614 The converse isn't true. */
1616 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1617 if (!strcmp (argv[i], "-MG"))
1619 print_deps_missing_files = 1;
1622 if (!strcmp (argv[i], "-M"))
1624 else if (!strcmp (argv[i], "-MM"))
1626 else if (!strcmp (argv[i], "-MD"))
1628 else if (!strcmp (argv[i], "-MMD"))
1630 /* For -MD and -MMD options, write deps on file named by next arg. */
1631 if (!strcmp (argv[i], "-MD")
1632 || !strcmp (argv[i], "-MMD")) {
1634 fatal ("Filename missing after %s option", argv[i]);
1636 deps_file = argv[i];
1639 /* For -M and -MM, write deps on standard output
1640 and suppress the usual output. */
1641 deps_stream = stdout;
1648 char *p = argv[i] + 2;
1650 while ((c = *p++)) {
1651 /* Arg to -d specifies what parts of macros to dump */
1654 dump_macros = dump_only;
1658 dump_macros = dump_names;
1661 dump_macros = dump_definitions;
1672 if (argv[i][2] == '3')
1677 fprintf (stderr, "GNU CPP version %s", version_string);
1678 #ifdef TARGET_VERSION
1681 fprintf (stderr, "\n");
1686 print_include_names = 1;
1690 if (argv[i][2] != 0)
1691 pend_defs[i] = argv[i] + 2;
1692 else if (i + 1 == argc)
1693 fatal ("Macro name missing after -D option");
1695 i++, pend_defs[i] = argv[i];
1702 if (argv[i][2] != 0)
1704 else if (i + 1 == argc)
1705 fatal ("Assertion missing after -A option");
1709 if (!strcmp (p, "-")) {
1710 /* -A- eliminates all predefined macros and assertions.
1711 Let's include also any that were specified earlier
1712 on the command line. That way we can get rid of any
1713 that were passed automatically in from GCC. */
1715 inhibit_predefs = 1;
1716 for (j = 0; j < i; j++)
1717 pend_defs[j] = pend_assertions[j] = 0;
1719 pend_assertions[i] = p;
1720 pend_assertion_options[i] = "-A";
1725 case 'U': /* JF #undef something */
1726 if (argv[i][2] != 0)
1727 pend_undefs[i] = argv[i] + 2;
1728 else if (i + 1 == argc)
1729 fatal ("Macro name missing after -U option");
1731 pend_undefs[i] = argv[i+1], i++;
1735 put_out_comments = 1;
1738 case 'E': /* -E comes from cc -E; ignore it. */
1742 no_line_directives = 1;
1745 case '$': /* Don't include $ in identifiers. */
1746 is_idchar['$'] = is_idstart['$'] = 0;
1749 case 'I': /* Add directory to path for includes. */
1751 struct file_name_list *dirtmp;
1753 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
1755 /* Don't use any preceding -I directories for #include <...>. */
1756 first_bracket_include = 0;
1759 dirtmp = new_include_prefix (last_include, NULL_PTR, "",
1760 argv[i][2] ? argv[i] + 2 : argv[++i]);
1761 append_include_chain (dirtmp, dirtmp);
1767 if (!strcmp (argv[i], "-nostdinc"))
1768 /* -nostdinc causes no default include directories.
1769 You must specify all include-file directories with -I. */
1770 no_standard_includes = 1;
1771 else if (!strcmp (argv[i], "-nostdinc++"))
1772 /* -nostdinc++ causes no default C++-specific include directories. */
1773 no_standard_cplusplus_includes = 1;
1774 else if (!strcmp (argv[i], "-noprecomp"))
1779 /* Sun compiler passes undocumented switch "-undef".
1780 Let's assume it means to inhibit the predefined symbols. */
1781 inhibit_predefs = 1;
1784 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1785 if (in_fname == NULL) {
1788 } else if (out_fname == NULL) {
1791 } /* else fall through into error */
1794 fatal ("Invalid option `%s'", argv[i]);
1799 /* Add dirs from CPATH after dirs from -I. */
1800 /* There seems to be confusion about what CPATH should do,
1801 so for the moment it is not documented. */
1802 /* Some people say that CPATH should replace the standard include dirs,
1803 but that seems pointless: it comes before them, so it overrides them
1805 cp = getenv ("CPATH");
1806 if (cp && ! no_standard_includes)
1809 /* Initialize output buffer */
1811 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1812 outbuf.bufp = outbuf.buf;
1813 outbuf.length = OUTBUF_SIZE;
1815 /* Do partial setup of input buffer for the sake of generating
1816 early #line directives (when -g is in effect). */
1818 fp = &instack[++indepth];
1819 if (in_fname == NULL)
1821 fp->nominal_fname = fp->fname = in_fname;
1824 /* In C++, wchar_t is a distinct basic type, and we can expect
1825 __wchar_t to be defined by cc1plus. */
1827 wchar_type = "__wchar_t";
1829 /* Install __LINE__, etc. Must follow initialize_char_syntax
1830 and option processing. */
1831 initialize_builtins (fp, &outbuf);
1833 /* Do standard #defines and assertions
1834 that identify system and machine type. */
1836 if (!inhibit_predefs) {
1837 char *p = (char *) alloca (strlen (predefs) + 1);
1838 strcpy (p, predefs);
1841 while (*p == ' ' || *p == '\t')
1843 /* Handle -D options. */
1844 if (p[0] == '-' && p[1] == 'D') {
1846 while (*p && *p != ' ' && *p != '\t')
1851 output_line_directive (fp, &outbuf, 0, same_file);
1852 make_definition (q, &outbuf);
1853 while (*p == ' ' || *p == '\t')
1855 } else if (p[0] == '-' && p[1] == 'A') {
1856 /* Handle -A options (assertions). */
1865 past_name = assertion;
1866 /* Locate end of name. */
1867 while (*past_name && *past_name != ' '
1868 && *past_name != '\t' && *past_name != '(')
1870 /* Locate `(' at start of value. */
1872 while (*value && (*value == ' ' || *value == '\t'))
1874 if (*value++ != '(')
1876 while (*value && (*value == ' ' || *value == '\t'))
1879 /* Locate end of value. */
1880 while (*past_value && *past_value != ' '
1881 && *past_value != '\t' && *past_value != ')')
1883 termination = past_value;
1884 while (*termination && (*termination == ' ' || *termination == '\t'))
1886 if (*termination++ != ')')
1888 if (*termination && *termination != ' ' && *termination != '\t')
1890 /* Temporarily null-terminate the value. */
1891 save_char = *termination;
1892 *termination = '\0';
1893 /* Install the assertion. */
1894 make_assertion ("-A", assertion);
1895 *termination = (char) save_char;
1897 while (*p == ' ' || *p == '\t')
1905 /* Now handle the command line options. */
1907 /* Do -U's, -D's and -A's in the order they were seen. */
1908 for (i = 1; i < argc; i++) {
1909 if (pend_undefs[i]) {
1911 output_line_directive (fp, &outbuf, 0, same_file);
1912 make_undef (pend_undefs[i], &outbuf);
1916 output_line_directive (fp, &outbuf, 0, same_file);
1917 make_definition (pend_defs[i], &outbuf);
1919 if (pend_assertions[i])
1920 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1923 done_initializing = 1;
1925 { /* Read the appropriate environment variable and if it exists
1926 replace include_defaults with the listed path. */
1928 switch ((objc << 1) + cplusplus)
1931 epath = getenv ("C_INCLUDE_PATH");
1934 epath = getenv ("CPLUS_INCLUDE_PATH");
1937 epath = getenv ("OBJC_INCLUDE_PATH");
1940 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
1943 /* If the environment var for this language is set,
1944 add to the default list of include directories. */
1947 char *startp, *endp;
1949 for (num_dirs = 1, startp = epath; *startp; startp++)
1950 if (*startp == PATH_SEPARATOR)
1953 = (struct default_include *) xmalloc ((num_dirs
1954 * sizeof (struct default_include))
1955 + sizeof (include_defaults_array));
1956 startp = endp = epath;
1960 if (c == PATH_SEPARATOR || !c) {
1962 include_defaults[num_dirs].fname
1963 = startp == endp ? "." : savestring (startp);
1965 include_defaults[num_dirs].component = 0;
1966 include_defaults[num_dirs].cplusplus = cplusplus;
1967 include_defaults[num_dirs].cxx_aware = 1;
1974 /* Put the usual defaults back in at the end. */
1975 bcopy ((char *) include_defaults_array,
1976 (char *) &include_defaults[num_dirs],
1977 sizeof (include_defaults_array));
1981 append_include_chain (before_system, last_before_system);
1982 first_system_include = before_system;
1984 /* Unless -fnostdinc,
1985 tack on the standard include file dirs to the specified list */
1986 if (!no_standard_includes) {
1987 struct default_include *p = include_defaults;
1988 char *specd_prefix = include_prefix;
1989 char *default_prefix = savestring (GCC_INCLUDE_DIR);
1990 int default_len = 0;
1991 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1992 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1993 default_len = strlen (default_prefix) - 7;
1994 default_prefix[default_len] = 0;
1996 /* Search "translated" versions of GNU directories.
1997 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1998 if (specd_prefix != 0 && default_len != 0)
1999 for (p = include_defaults; p->fname; p++) {
2000 /* Some standard dirs are only for C++. */
2001 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
2002 /* Does this dir start with the prefix? */
2003 if (!strncmp (p->fname, default_prefix, default_len)) {
2004 /* Yes; change prefix and add to search list. */
2005 struct file_name_list *new
2006 = new_include_prefix (NULL_PTR, NULL_PTR, specd_prefix,
2007 p->fname + default_len);
2009 new->c_system_include_path = !p->cxx_aware;
2010 append_include_chain (new, new);
2011 if (first_system_include == 0)
2012 first_system_include = new;
2017 /* Search ordinary names for GNU include directories. */
2018 for (p = include_defaults; p->fname; p++) {
2019 /* Some standard dirs are only for C++. */
2020 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
2021 struct file_name_list *new
2022 = new_include_prefix (NULL_PTR, p->component, "", p->fname);
2024 new->c_system_include_path = !p->cxx_aware;
2025 append_include_chain (new, new);
2026 if (first_system_include == 0)
2027 first_system_include = new;
2033 /* Tack the after_include chain at the end of the include chain. */
2034 append_include_chain (after_include, last_after_include);
2035 if (first_system_include == 0)
2036 first_system_include = after_include;
2038 /* With -v, print the list of dirs to search. */
2040 struct file_name_list *p;
2041 fprintf (stderr, "#include \"...\" search starts here:\n");
2042 for (p = include; p; p = p->next) {
2043 if (p == first_bracket_include)
2044 fprintf (stderr, "#include <...> search starts here:\n");
2046 fprintf (stderr, " .\n");
2047 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
2048 fprintf (stderr, " %s\n", p->fname);
2050 /* Omit trailing '/'. */
2051 fprintf (stderr, " %.*s\n", (int) strlen (p->fname) - 1, p->fname);
2053 fprintf (stderr, "End of search list.\n");
2056 /* -MG doesn't select the form of output and must be specified with one of
2057 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
2058 inhibit compilation. */
2059 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
2060 fatal ("-MG must be specified with one of -M or -MM");
2062 /* Either of two environment variables can specify output of deps.
2063 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
2064 where OUTPUT_FILE is the file to write deps info to
2065 and DEPS_TARGET is the target to mention in the deps. */
2068 && (getenv ("SUNPRO_DEPENDENCIES") != 0
2069 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
2070 char *spec = getenv ("DEPENDENCIES_OUTPUT");
2075 spec = getenv ("SUNPRO_DEPENDENCIES");
2082 /* Find the space before the DEPS_TARGET, if there is one. */
2083 /* This should use index. (mrs) */
2084 while (*s != 0 && *s != ' ') s++;
2086 deps_target = s + 1;
2087 output_file = xmalloc (s - spec + 1);
2088 bcopy (spec, output_file, s - spec);
2089 output_file[s - spec] = 0;
2096 deps_file = output_file;
2100 /* For -M, print the expected object file name
2101 as the target of this Make-rule. */
2103 deps_allocated_size = 200;
2104 deps_buffer = xmalloc (deps_allocated_size);
2110 deps_output (deps_target, ':');
2111 } else if (*in_fname == 0) {
2112 deps_output ("-", ':');
2117 q = base_name (in_fname);
2119 /* Copy remainder to mungable area. */
2120 p = (char *) alloca (strlen(q) + 8);
2123 /* Output P, but remove known suffixes. */
2127 && p[len - 2] == '.'
2128 && index("cCsSm", p[len - 1]))
2131 && p[len - 3] == '.'
2132 && p[len - 2] == 'c'
2133 && p[len - 1] == 'c')
2136 && p[len - 4] == '.'
2137 && p[len - 3] == 'c'
2138 && p[len - 2] == 'x'
2139 && p[len - 1] == 'x')
2142 && p[len - 4] == '.'
2143 && p[len - 3] == 'c'
2144 && p[len - 2] == 'p'
2145 && p[len - 1] == 'p')
2148 /* Supply our own suffix. */
2149 strcpy (q, OBJECT_SUFFIX);
2151 deps_output (p, ':');
2152 deps_output (in_fname, ' ');
2156 /* Scan the -imacros files before the main input.
2157 Much like #including them, but with no_output set
2158 so that only their macro definitions matter. */
2160 no_output++; no_record_file++;
2161 for (i = 1; i < argc; i++)
2162 if (pend_files[i]) {
2163 struct include_file *inc;
2164 int fd = open_include_file (pend_files[i], NULL_PTR, NULL_PTR, &inc);
2166 perror_with_name (pend_files[i]);
2167 return FATAL_EXIT_CODE;
2169 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2171 no_output--; no_record_file--;
2173 /* Copy the entire contents of the main input file into
2174 the stacked input buffer previously allocated for it. */
2176 /* JF check for stdin */
2177 if (in_fname == NULL || *in_fname == 0) {
2180 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2183 if (fstat (f, &st) != 0)
2184 pfatal_with_name (in_fname);
2185 fp->nominal_fname = fp->fname = in_fname;
2187 fp->system_header_p = 0;
2188 /* JF all this is mine about reading pipes and ttys */
2189 if (! S_ISREG (st.st_mode)) {
2190 /* Read input from a file that is not a normal disk file.
2191 We cannot preallocate a buffer with the correct size,
2192 so we must read in the file a piece at the time and make it bigger. */
2197 if (S_ISDIR (st.st_mode))
2198 fatal ("Input file `%s' is a directory", in_fname);
2202 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2204 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2205 if (cnt < 0) goto perror; /* error! */
2207 if (size != bsize) break; /* End of file */
2209 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2213 /* Read a file whose size we can determine in advance.
2214 For the sake of VMS, st.st_size is just an upper bound. */
2215 fp->buf = (U_CHAR *) xmalloc (st.st_size + 2);
2216 fp->length = safe_read (f, (char *) fp->buf, st.st_size);
2217 if (fp->length < 0) goto perror;
2220 fp->if_stack = if_stack;
2222 /* Make sure data ends with a newline. And put a null after it. */
2224 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2225 /* Backslash-newline at end is not good enough. */
2226 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2227 fp->buf[fp->length++] = '\n';
2228 missing_newline = 1;
2230 fp->buf[fp->length] = '\0';
2232 /* Unless inhibited, convert trigraphs in the input. */
2237 /* Now that we know the input file is valid, open the output. */
2239 if (!out_fname || !strcmp (out_fname, ""))
2240 out_fname = "stdout";
2241 else if (! freopen (out_fname, "w", stdout))
2242 pfatal_with_name (out_fname);
2244 output_line_directive (fp, &outbuf, 0, same_file);
2246 /* Scan the -include files before the main input. */
2249 for (i = 1; i < argc; i++)
2250 if (pend_includes[i]) {
2251 struct include_file *inc;
2252 int fd = open_include_file (pend_includes[i], NULL_PTR, NULL_PTR, &inc);
2254 perror_with_name (pend_includes[i]);
2255 return FATAL_EXIT_CODE;
2257 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2261 /* Scan the input, processing macros and directives. */
2263 rescan (&outbuf, 0);
2265 if (missing_newline)
2268 if (pedantic && missing_newline)
2269 pedwarn ("file does not end in newline");
2271 /* Now we have processed the entire input
2272 Write whichever kind of output has been requested. */
2274 if (dump_macros == dump_only)
2276 else if (! inhibit_output) {
2281 /* Don't actually write the deps file if compilation has failed. */
2283 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2284 pfatal_with_name (deps_file);
2285 fputs (deps_buffer, deps_stream);
2286 putc ('\n', deps_stream);
2288 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2289 fatal ("I/O error on output");
2294 if (pcp_outfile && pcp_outfile != stdout
2295 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2296 fatal ("I/O error on `-pcp' output");
2298 if (ferror (stdout) || fclose (stdout) != 0)
2299 fatal ("I/O error on output");
2302 exit (FATAL_EXIT_CODE);
2303 exit (SUCCESS_EXIT_CODE);
2306 pfatal_with_name (in_fname);
2310 /* Given a colon-separated list of file names PATH,
2311 add all the names to the search path for include files. */
2325 struct file_name_list *dirtmp;
2327 /* Find the end of this name. */
2328 while ((c = *q++) != PATH_SEPARATOR && c)
2332 dirtmp = new_include_prefix (last_include, NULL_PTR,
2333 "", p == q ? "." : p);
2335 append_include_chain (dirtmp, dirtmp);
2337 /* Advance past this name. */
2344 /* Return the address of the first character in S that equals C.
2345 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2346 Return 0 if there is no such character. Assume that C itself is not '\0'.
2347 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2348 but unfortunately memchr isn't autoconfigured yet. */
2356 char *p = (char *) s;
2358 char *q = index (p, c);
2360 return (U_CHAR *) q;
2362 size_t l = strlen (p);
2372 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2373 before main CCCP processing. Name `pcp' is also in honor of the
2374 drugs the trigraph designers must have been on.
2376 Using an extra pass through the buffer takes a little extra time,
2377 but is infinitely less hairy than trying to handle trigraphs inside
2378 strings, etc. everywhere, and also makes sure that trigraphs are
2379 only translated in the top level of processing. */
2385 register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
2388 fptr = bptr = sptr = buf->buf;
2389 lptr = fptr + buf->length;
2390 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2427 len = sptr - fptr - 2;
2429 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2430 C, this will be memmove (). */
2431 if (bptr != fptr && len > 0)
2432 bcopy ((char *) fptr, (char *) bptr, len);
2438 len = buf->length - (fptr - buf->buf);
2439 if (bptr != fptr && len > 0)
2440 bcopy ((char *) fptr, (char *) bptr, len);
2441 buf->length -= fptr - bptr;
2442 buf->buf[buf->length] = '\0';
2443 if (warn_trigraphs && fptr != bptr)
2444 warning_with_line (0, "%lu trigraph(s) encountered",
2445 (unsigned long) (fptr - bptr) / 2);
2448 /* Move all backslash-newline pairs out of embarrassing places.
2449 Exchange all such pairs following BP
2450 with any potentially-embarrassing characters that follow them.
2451 Potentially-embarrassing characters are / and *
2452 (because a backslash-newline inside a comment delimiter
2453 would cause it not to be recognized). */
2459 register U_CHAR *p = bp;
2461 /* First count the backslash-newline pairs here. */
2463 while (p[0] == '\\' && p[1] == '\n')
2466 /* What follows the backslash-newlines is not embarrassing. */
2468 if (*p != '/' && *p != '*')
2471 /* Copy all potentially embarrassing characters
2472 that follow the backslash-newline pairs
2473 down to where the pairs originally started. */
2475 while (*p == '*' || *p == '/')
2478 /* Now write the same number of pairs after the embarrassing chars. */
2485 /* Like newline_fix but for use within a directive-name.
2486 Move any backslash-newlines up past any following symbol constituents. */
2489 name_newline_fix (bp)
2492 register U_CHAR *p = bp;
2494 /* First count the backslash-newline pairs here. */
2495 while (p[0] == '\\' && p[1] == '\n')
2498 /* What follows the backslash-newlines is not embarrassing. */
2503 /* Copy all potentially embarrassing characters
2504 that follow the backslash-newline pairs
2505 down to where the pairs originally started. */
2507 while (is_idchar[*p])
2510 /* Now write the same number of pairs after the embarrassing chars. */
2517 /* Look for lint commands in comments.
2519 When we come in here, ibp points into a comment. Limit is as one expects.
2520 scan within the comment -- it should start, after lwsp, with a lint command.
2521 If so that command is returned as a (constant) string.
2523 Upon return, any arg will be pointed to with argstart and will be
2524 arglen long. Note that we don't parse that arg since it will just
2525 be printed out again. */
2528 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2529 register U_CHAR *ibp;
2530 register U_CHAR *limit;
2531 U_CHAR **argstart; /* point to command arg */
2532 int *arglen, *cmdlen; /* how long they are */
2534 HOST_WIDE_INT linsize;
2535 register U_CHAR *numptr; /* temp for arg parsing */
2539 SKIP_WHITE_SPACE (ibp);
2541 if (ibp >= limit) return NULL;
2543 linsize = limit - ibp;
2545 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2546 if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
2548 return "NOTREACHED";
2550 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2554 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2556 return "LINTLIBRARY";
2558 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2560 ibp += 7; linsize -= 7;
2561 if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
2563 /* OK, read a number */
2564 for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
2566 *arglen = numptr - *argstart;
2573 * The main loop of the program.
2575 * Read characters from the input stack, transferring them to the
2578 * Macros are expanded and push levels on the input stack.
2579 * At the end of such a level it is popped off and we keep reading.
2580 * At the end of any other kind of level, we return.
2581 * #-directives are handled, except within macros.
2583 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2584 * and insert them when appropriate. This is set while scanning macro
2585 * arguments before substitution. It is zero when scanning for final output.
2586 * There are three types of Newline markers:
2587 * * Newline - follows a macro name that was not expanded
2588 * because it appeared inside an expansion of the same macro.
2589 * This marker prevents future expansion of that identifier.
2590 * When the input is rescanned into the final output, these are deleted.
2591 * These are also deleted by ## concatenation.
2592 * * Newline Space (or Newline and any other whitespace character)
2593 * stands for a place that tokens must be separated or whitespace
2594 * is otherwise desirable, but where the ANSI standard specifies there
2595 * is no whitespace. This marker turns into a Space (or whichever other
2596 * whitespace char appears in the marker) in the final output,
2597 * but it turns into nothing in an argument that is stringified with #.
2598 * Such stringified arguments are the only place where the ANSI standard
2599 * specifies with precision that whitespace may not appear.
2601 * During this function, IP->bufp is kept cached in IBP for speed of access.
2602 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2603 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2604 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2605 * explicitly, and before RECACHE, since RECACHE uses OBP.
2609 rescan (op, output_marks)
2613 /* Character being scanned in main loop. */
2616 /* Length of pending accumulated identifier. */
2617 register int ident_length = 0;
2619 /* Hash code of pending accumulated identifier. */
2620 register int hash = 0;
2622 /* Current input level (&instack[indepth]). */
2625 /* Pointer for scanning input. */
2626 register U_CHAR *ibp;
2628 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2629 register U_CHAR *limit;
2631 /* Pointer for storing output. */
2632 register U_CHAR *obp;
2634 /* REDO_CHAR is nonzero if we are processing an identifier
2635 after backing up over the terminating character.
2636 Sometimes we process an identifier without backing up over
2637 the terminating character, if the terminating character
2638 is not special. Backing up is done so that the terminating character
2639 will be dispatched on again once the identifier is dealt with. */
2642 /* 1 if within an identifier inside of which a concatenation
2643 marker (Newline -) has been seen. */
2644 int concatenated = 0;
2646 /* While scanning a comment or a string constant,
2647 this records the line it started on, for error messages. */
2650 /* Record position of last `real' newline. */
2651 U_CHAR *beg_of_line;
2653 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2656 do { ip->macro->type = T_MACRO; \
2657 if (ip->free_ptr) free (ip->free_ptr); \
2658 --indepth; } while (0)
2660 /* Reload `rescan's local variables that describe the current
2661 level of the input stack. */
2664 do { ip = &instack[indepth]; \
2666 limit = ip->buf + ip->length; \
2668 check_expand (op, limit - ibp); \
2670 obp = op->bufp; } while (0)
2672 if (no_output && instack[indepth].fname != 0)
2673 skip_if_group (&instack[indepth], 1, NULL);
2680 /* Our caller must always put a null after the end of
2681 the input at each input stack level. */
2691 if (*ibp == '\n' && !ip->macro) {
2692 /* At the top level, always merge lines ending with backslash-newline,
2693 even in middle of identifier. But do not merge lines in a macro,
2694 since backslash might be followed by a newline-space marker. */
2697 --obp; /* remove backslash from obuf */
2700 /* If ANSI, backslash is just another character outside a string. */
2703 /* Otherwise, backslash suppresses specialness of following char,
2704 so copy it here to prevent the switch from seeing it.
2705 But first get any pending identifier processed. */
2706 if (ident_length > 0)
2713 if (ident_length || ip->macro || traditional)
2715 while (*ibp == '\\' && ibp[1] == '\n') {
2721 /* Treat this %: digraph as if it were #. */
2725 if (assertions_flag) {
2728 /* Copy #foo (bar lose) without macro expansion. */
2729 obp[-1] = '#'; /* In case it was '%'. */
2730 SKIP_WHITE_SPACE (ibp);
2731 while (is_idchar[*ibp])
2733 SKIP_WHITE_SPACE (ibp);
2736 skip_paren_group (ip);
2737 bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
2738 obp += ip->bufp - ibp;
2744 /* If this is expanding a macro definition, don't recognize
2745 preprocessing directives. */
2748 /* If this is expand_into_temp_buffer,
2749 don't recognize them either. Warn about them
2750 only after an actual newline at this level,
2751 not at the beginning of the input level. */
2753 if (ip->buf != beg_of_line)
2754 warning ("preprocessing directive not recognized within macro arg");
2761 /* # keyword: a # must be first nonblank char on the line */
2762 if (beg_of_line == 0)
2767 /* Scan from start of line, skipping whitespace, comments
2768 and backslash-newlines, and see if we reach this #.
2769 If not, this # is not special. */
2771 /* If -traditional, require # to be at beginning of line. */
2774 if (is_hor_space[*bp])
2776 else if (*bp == '\\' && bp[1] == '\n')
2778 else if (*bp == '/' && bp[1] == '*') {
2780 while (!(*bp == '*' && bp[1] == '/'))
2784 /* There is no point in trying to deal with C++ // comments here,
2785 because if there is one, then this # must be part of the
2786 comment and we would never reach here. */
2792 while (bp[1] == '\\' && bp[2] == '\n')
2796 /* %: appears at start of line; skip past the ':' too. */
2805 /* This # can start a directive. */
2807 --obp; /* Don't copy the '#' */
2811 if (! handle_directive (ip, op)) {
2815 /* Not a known directive: treat it as ordinary text.
2816 IP, OP, IBP, etc. have not been changed. */
2817 if (no_output && instack[indepth].fname) {
2818 /* If not generating expanded output,
2819 what we do with ordinary text is skip it.
2820 Discard everything until next # directive. */
2821 skip_if_group (&instack[indepth], 1, 0);
2826 *obp++ = '#'; /* Copy # (even if it was originally %:). */
2827 /* Don't expand an identifier that could be a macro directive.
2828 (Section 3.8.3 of the ANSI C standard) */
2829 SKIP_WHITE_SPACE (ibp);
2830 if (is_idstart[*ibp])
2833 while (is_idchar[*ibp])
2841 /* A # directive has been successfully processed. */
2842 /* If not generating expanded output, ignore everything until
2843 next # directive. */
2844 if (no_output && instack[indepth].fname)
2845 skip_if_group (&instack[indepth], 1, 0);
2851 case '\"': /* skip quoted string */
2853 /* A single quoted string is treated like a double -- some
2854 programs (e.g., troff) are perverse this way */
2856 /* Handle any pending identifier;
2857 but the L in L'...' or L"..." is not an identifier. */
2859 && ! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2862 start_line = ip->lineno;
2864 /* Skip ahead to a matching quote. */
2868 if (ip->macro != 0) {
2869 /* try harder: this string crosses a macro expansion boundary.
2870 This can happen naturally if -traditional.
2871 Otherwise, only -D can make a macro with an unmatched quote. */
2877 error_with_line (line_for_error (start_line),
2878 "unterminated string or character constant");
2879 error_with_line (multiline_string_line,
2880 "possible real start of unterminated constant");
2881 multiline_string_line = 0;
2890 /* Traditionally, end of line ends a string constant with no error.
2891 So exit the loop and record the new line. */
2897 error_with_line (line_for_error (start_line),
2898 "unterminated character constant");
2901 if (multiline_string_line == 0) {
2903 pedwarn_with_line (line_for_error (start_line),
2904 "string constant runs past end of line");
2905 multiline_string_line = ip->lineno - 1;
2913 /* Backslash newline is replaced by nothing at all,
2914 but keep the line counts correct. */
2919 /* ANSI stupidly requires that in \\ the second \
2920 is *not* prevented from combining with a newline. */
2921 while (*ibp == '\\' && ibp[1] == '\n') {
2940 if (*ibp == '\\' && ibp[1] == '\n')
2944 && !(cplusplus_comments && *ibp == '/'))
2952 /* C++ style comment... */
2953 start_line = ip->lineno;
2955 /* Comments are equivalent to spaces. */
2956 if (! put_out_comments)
2960 U_CHAR *before_bp = ibp;
2962 while (++ibp < limit) {
2964 if (ibp[-1] != '\\') {
2965 if (put_out_comments) {
2966 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
2967 obp += ibp - before_bp;
2972 warning ("multiline `//' comment");
2974 /* Copy the newline into the output buffer, in order to
2975 avoid the pain of a #line every time a multiline comment
2977 if (!put_out_comments)
2986 /* Ordinary C comment. Skip it, optionally copying it to output. */
2988 start_line = ip->lineno;
2990 ++ibp; /* Skip the star. */
2992 /* If this cpp is for lint, we peek inside the comments: */
2996 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2998 if (lintcmd != NULL) {
3000 check_expand (op, cmdlen + arglen + 14);
3002 /* I believe it is always safe to emit this newline: */
3004 bcopy ("#pragma lint ", (char *) obp, 13);
3006 bcopy (lintcmd, (char *) obp, cmdlen);
3011 bcopy (argbp, (char *) obp, arglen);
3015 /* OK, now bring us back to the state we were in before we entered
3016 this branch. We need #line because the #pragma's newline always
3017 messes up the line count. */
3019 output_line_directive (ip, op, 0, same_file);
3020 check_expand (op, limit - ibp + 2);
3026 /* Comments are equivalent to spaces.
3027 Note that we already output the slash; we might not want it.
3028 For -traditional, a comment is equivalent to nothing. */
3029 if (! put_out_comments) {
3039 U_CHAR *before_bp = ibp;
3044 if (ibp[-2] == '/' && warn_comments)
3045 warning ("`/*' within comment");
3046 if (*ibp == '\\' && ibp[1] == '\n')
3054 /* Copy the newline into the output buffer, in order to
3055 avoid the pain of a #line every time a multiline comment
3057 if (!put_out_comments)
3064 error_with_line (line_for_error (start_line),
3065 "unterminated comment");
3074 if (put_out_comments) {
3075 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
3076 obp += ibp - before_bp;
3082 if (! is_idchar['$'])
3085 pedwarn ("`$' in identifier");
3088 case '0': case '1': case '2': case '3': case '4':
3089 case '5': case '6': case '7': case '8': case '9':
3090 /* If digit is not part of identifier, it starts a number,
3091 which means that following letters are not an identifier.
3092 "0x5" does not refer to an identifier "x5".
3093 So copy all alphanumerics that follow without accumulating
3094 as an identifier. Periods also, for sake of "3.e7". */
3096 if (ident_length == 0) {
3098 while (ibp[0] == '\\' && ibp[1] == '\n') {
3103 if (!is_idchar[c] && c != '.') {
3108 /* A sign can be part of a preprocessing number
3109 if it follows an `e' or `p'. */
3110 if (c == 'e' || c == 'E' || c == 'p' || c == 'P') {
3111 while (ibp[0] == '\\' && ibp[1] == '\n') {
3115 if (*ibp == '+' || *ibp == '-') {
3117 /* But traditional C does not let the token go past the sign,
3118 and C89 does not allow `p'. */
3119 if (traditional || (c89 && (c == 'p' || c == 'P')))
3129 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3130 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3131 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3132 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3134 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3135 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3136 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3137 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3141 /* Compute step of hash function, to avoid a proc call on every token */
3142 hash = HASHSTEP (hash, c);
3146 if (ip->fname == 0 && *ibp == '-') {
3147 /* Newline - inhibits expansion of preceding token.
3148 If expanding a macro arg, we keep the newline -.
3149 In final output, it is deleted.
3150 We recognize Newline - in macro bodies and macro args. */
3151 if (! concatenated) {
3156 if (!output_marks) {
3159 /* If expanding a macro arg, keep the newline -. */
3165 /* If reprocessing a macro expansion, newline is a special marker. */
3166 else if (ip->macro != 0) {
3167 /* Newline White is a "funny space" to separate tokens that are
3168 supposed to be separate but without space between.
3169 Here White means any whitespace character.
3170 Newline - marks a recursive macro use that is not
3171 supposed to be expandable. */
3173 if (is_space[*ibp]) {
3174 /* Newline Space does not prevent expansion of preceding token
3175 so expand the preceding token and then come back. */
3176 if (ident_length > 0)
3179 /* If generating final output, newline space makes a space. */
3180 if (!output_marks) {
3182 /* And Newline Newline makes a newline, so count it. */
3183 if (obp[-1] == '\n')
3186 /* If expanding a macro arg, keep the newline space.
3187 If the arg gets stringified, newline space makes nothing. */
3190 } else abort (); /* Newline followed by something random? */
3194 /* If there is a pending identifier, handle it and come back here. */
3195 if (ident_length > 0)
3200 /* Update the line counts and output a #line if necessary. */
3203 if (ip->lineno != op->lineno) {
3205 output_line_directive (ip, op, 1, same_file);
3206 check_expand (op, limit - ibp);
3211 /* Come here either after (1) a null character that is part of the input
3212 or (2) at the end of the input, because there is a null there. */
3215 /* Our input really contains a null character. */
3219 /* At end of a macro-expansion level, pop it and read next level. */
3220 if (ip->macro != 0) {
3223 /* If traditional, and we have an identifier that ends here,
3224 process it now, so we get the right error for recursion. */
3225 if (traditional && ident_length
3226 && ! is_idchar[*instack[indepth - 1].bufp]) {
3235 /* If we don't have a pending identifier,
3236 return at end of input. */
3237 if (ident_length == 0) {
3245 /* If we do have a pending identifier, just consider this null
3246 a special character and arrange to dispatch on it again.
3247 The second time, IDENT_LENGTH will be zero so we will return. */
3253 /* Handle the case of a character such as /, ', " or null
3254 seen following an identifier. Back over it so that
3255 after the identifier is processed the special char
3256 will be dispatched on again. */
3266 if (ident_length > 0) {
3267 register HASHNODE *hp;
3269 /* We have just seen an identifier end. If it's a macro, expand it.
3271 IDENT_LENGTH is the length of the identifier
3272 and HASH is its hash code.
3274 The identifier has already been copied to the output,
3275 so if it is a macro we must remove it.
3277 If REDO_CHAR is 0, the char that terminated the identifier
3278 has been skipped in the output and the input.
3279 OBP-IDENT_LENGTH-1 points to the identifier.
3280 If the identifier is a macro, we must back over the terminator.
3282 If REDO_CHAR is 1, the terminating char has already been
3283 backed over. OBP-IDENT_LENGTH points to the identifier. */
3285 if (!pcp_outfile || pcp_inside_if) {
3286 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3289 if (hp->length == ident_length) {
3290 int obufp_before_macroname;
3291 int op_lineno_before_macroname;
3292 register int i = ident_length;
3293 register U_CHAR *p = hp->name;
3294 register U_CHAR *q = obp - i;
3300 do { /* All this to avoid a strncmp () */
3305 /* We found a use of a macro name.
3306 see if the context shows it is a macro call. */
3308 /* Back up over terminating character if not already done. */
3314 /* Save this as a displacement from the beginning of the output
3315 buffer. We can not save this as a position in the output
3316 buffer, because it may get realloc'ed by RECACHE. */
3317 obufp_before_macroname = (obp - op->buf) - ident_length;
3318 op_lineno_before_macroname = op->lineno;
3320 if (hp->type == T_PCSTRING) {
3321 pcstring_used (hp); /* Mark the definition of this key
3322 as needed, ensuring that it
3324 break; /* Exit loop, since the key cannot have a
3325 definition any longer. */
3328 /* Record whether the macro is disabled. */
3329 disabled = hp->type == T_DISABLED;
3331 /* This looks like a macro ref, but if the macro was disabled,
3332 just copy its name and put in a marker if requested. */
3336 /* This error check caught useful cases such as
3337 #define foo(x,y) bar (x (y,0), y)
3340 error ("recursive use of macro `%s'", hp->name);
3344 check_expand (op, limit - ibp + 2);
3351 /* If macro wants an arglist, verify that a '(' follows.
3352 first skip all whitespace, copying it to the output
3353 after the macro name. Then, if there is no '(',
3354 decide this is not a macro call and leave things that way. */
3355 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3356 && hp->value.defn->nargs >= 0)
3358 U_CHAR *old_ibp = ibp;
3359 U_CHAR *old_obp = obp;
3360 int old_iln = ip->lineno;
3361 int old_oln = op->lineno;
3364 /* Scan forward over whitespace, copying it to the output. */
3365 if (ibp == limit && ip->macro != 0) {
3370 old_iln = ip->lineno;
3371 old_oln = op->lineno;
3373 /* A comment: copy it unchanged or discard it. */
3374 else if (*ibp == '/' && ibp[1] == '*') {
3375 if (put_out_comments) {
3378 } else if (! traditional) {
3382 while (ibp + 1 != limit
3383 && !(ibp[0] == '*' && ibp[1] == '/')) {
3384 /* We need not worry about newline-marks,
3385 since they are never found in comments. */
3387 /* Newline in a file. Count it. */
3391 if (put_out_comments)
3397 if (put_out_comments) {
3402 else if (is_space[*ibp]) {
3404 if (ibp[-1] == '\n') {
3405 if (ip->macro == 0) {
3406 /* Newline in a file. Count it. */
3409 } else if (!output_marks) {
3410 /* A newline mark, and we don't want marks
3411 in the output. If it is newline-hyphen,
3412 discard it entirely. Otherwise, it is
3413 newline-whitechar, so keep the whitechar. */
3423 /* A newline mark; copy both chars to the output. */
3431 /* It isn't a macro call.
3432 Put back the space that we just skipped. */
3435 ip->lineno = old_iln;
3436 op->lineno = old_oln;
3437 /* Exit the for loop. */
3442 /* This is now known to be a macro call.
3443 Discard the macro name from the output,
3444 along with any following whitespace just copied,
3445 but preserve newlines if not outputting marks since this
3446 is more likely to do the right thing with line numbers. */
3447 obp = op->buf + obufp_before_macroname;
3449 op->lineno = op_lineno_before_macroname;
3451 int newlines = op->lineno - op_lineno_before_macroname;
3452 while (0 < newlines--)
3456 /* Prevent accidental token-pasting with a character
3457 before the macro call. */
3458 if (!traditional && obp != op->buf) {
3460 case '!': case '%': case '&': case '*':
3461 case '+': case '-': case '.': case '/':
3462 case ':': case '<': case '=': case '>':
3464 /* If we are expanding a macro arg, make a newline marker
3465 to separate the tokens. If we are making real output,
3466 a plain space will do. */
3473 /* Expand the macro, reading arguments as needed,
3474 and push the expansion on the input stack. */
3477 macroexpand (hp, op);
3479 /* Reexamine input stack, since macroexpand has pushed
3480 a new level on it. */
3487 } /* End hash-table-search loop */
3489 ident_length = hash = 0; /* Stop collecting identifier */
3492 } /* End if (ident_length > 0) */
3494 } /* End per-char loop */
3496 /* Come here to return -- but first give an error message
3497 if there was an unterminated successful conditional. */
3499 if (if_stack != ip->if_stack)
3503 switch (if_stack->type)
3524 error_with_line (line_for_error (if_stack->lineno),
3525 "unterminated `#%s' conditional", str);
3527 if_stack = ip->if_stack;
3531 * Rescan a string into a temporary buffer and return the result
3532 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3534 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3535 * and insert such markers when appropriate. See `rescan' for details.
3536 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3537 * before substitution; it is 0 for other uses.
3540 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3541 U_CHAR *buf, *limit;
3542 int output_marks, assertions;
3544 register FILE_BUF *ip;
3546 int length = limit - buf;
3548 int odepth = indepth;
3549 int save_assertions_flag = assertions_flag;