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 #include <sys/types.h>
60 /* The following symbols should be autoconfigured:
67 In the mean time, we'll get by with approximations based
68 on existing GCC configuration symbols. */
71 # ifndef HAVE_STDLIB_H
72 # define HAVE_STDLIB_H 1
74 # ifndef HAVE_UNISTD_H
75 # define HAVE_UNISTD_H 1
78 # define STDC_HEADERS 1
80 #endif /* defined (POSIX) */
82 #if defined (POSIX) || (defined (USG) && !defined (VMS))
84 # define HAVE_FCNTL_H 1
91 # if TIME_WITH_SYS_TIME
92 # include <sys/time.h>
96 # include <sys/time.h>
101 # include <sys/resource.h>
119 # define bcmp(a, b, n) memcmp (a, b, n)
122 # define bcopy(s, d, n) memcpy (d, s, n)
125 # define bzero(d, n) memset (d, 0, n)
127 #else /* !STDC_HEADERS */
131 # if !defined (BSTRING) && (defined (USG) || defined (VMS))
134 # define bcmp my_bcmp
147 # endif /* !defined (bcmp) */
150 # define bcopy my_bcopy
160 # endif /* !defined (bcopy) */
163 # define bzero my_bzero
167 register unsigned length;
172 # endif /* !defined (bzero) */
174 # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
175 #endif /* ! STDC_HEADERS */
177 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
178 # define __attribute__(x)
182 # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
183 # define PROTO(ARGS) ARGS
185 # define PROTO(ARGS) ()
189 #if defined (__STDC__) && defined (HAVE_VPRINTF)
191 # define VA_START(va_list, var) va_start (va_list, var)
192 # define PRINTF_ALIST(msg) char *msg, ...
193 # define PRINTF_DCL(msg)
194 # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (__printf__, m, n)))
196 # include <varargs.h>
197 # define VA_START(va_list, var) va_start (va_list)
198 # define PRINTF_ALIST(msg) msg, va_alist
199 # define PRINTF_DCL(msg) char *msg; va_dcl
200 # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (__printf__, m, n)))
201 # define vfprintf(file, msg, args) \
203 char *a0 = va_arg(args, char *); \
204 char *a1 = va_arg(args, char *); \
205 char *a2 = va_arg(args, char *); \
206 char *a3 = va_arg(args, char *); \
207 fprintf (file, msg, a0, a1, a2, a3); \
211 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
212 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
213 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
219 /* VMS-specific definitions */
222 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
223 #define fopen(fname,mode) VMS_fopen (fname,mode)
224 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
225 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
226 static int VMS_fstat (), VMS_stat ();
227 static int VMS_open ();
228 static FILE * VMS_fopen ();
229 static FILE * VMS_freopen ();
230 static void hack_vms_include_specification ();
231 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
232 #define INO_T_HASH(a) 0
233 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
236 /* Windows does not natively support inodes, and neither does MSDOS. */
237 #if (defined (_WIN32) && ! defined (CYGWIN32)) || defined (__MSDOS__)
238 #define INO_T_EQ(a, b) 0
247 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
248 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
250 /* Find the largest host integer type and set its size and type.
251 Don't blindly use `long'; on some crazy hosts it is shorter than `int'. */
253 #ifndef HOST_BITS_PER_WIDE_INT
255 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
256 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
257 #define HOST_WIDE_INT long
259 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
260 #define HOST_WIDE_INT int
266 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
270 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
274 #define INO_T_EQ(a, b) ((a) == (b))
278 #define INO_T_HASH(a) (a)
281 /* Define a generic NULL if one hasn't already been defined. */
288 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
289 #define GENERIC_PTR void *
291 #define GENERIC_PTR char *
296 #define NULL_PTR ((GENERIC_PTR) 0)
299 #ifndef INCLUDE_LEN_FUDGE
300 #define INCLUDE_LEN_FUDGE 0
303 /* External declarations. */
305 extern char *version_string;
307 #ifndef HAVE_STRERROR
309 extern char *sys_errlist[];
310 #else /* HAVE_STRERROR */
314 char *strerror (int,...);
316 HOST_WIDE_INT parse_escape PROTO((char **, HOST_WIDE_INT));
317 HOST_WIDE_INT parse_c_expression PROTO((char *));
323 /* Name under which this program was invoked. */
325 static char *progname;
327 /* Nonzero means use extra default include directories for C++. */
329 static int cplusplus;
331 /* Nonzero means handle cplusplus style comments */
333 static int cplusplus_comments;
335 /* Nonzero means handle #import, for objective C. */
339 /* Nonzero means this is an assembly file, and allow
340 unknown directives, which could be comments. */
344 /* Current maximum length of directory names in the search path
345 for include files. (Altered as we get more of them.) */
347 static int max_include_len;
349 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
351 static int for_lint = 0;
353 /* Nonzero means copy comments into the output file. */
355 static int put_out_comments = 0;
357 /* Nonzero means don't process the ANSI trigraph sequences. */
359 static int no_trigraphs = 0;
361 /* Nonzero means print the names of included files rather than
362 the preprocessed output. 1 means just the #include "...",
363 2 means #include <...> as well. */
365 static int print_deps = 0;
367 /* Nonzero if missing .h files in -M output are assumed to be generated
368 files and not errors. */
370 static int print_deps_missing_files = 0;
372 /* Nonzero means print names of header files (-H). */
374 static int print_include_names = 0;
376 /* Nonzero means don't output line number information. */
378 static int no_line_directives;
380 /* Nonzero means output the text in failing conditionals,
381 inside #failed ... #endfailed. */
383 static int output_conditionals;
385 /* dump_only means inhibit output of the preprocessed text
386 and instead output the definitions of all user-defined
387 macros in a form suitable for use as input to cccp.
388 dump_names means pass #define and the macro name through to output.
389 dump_definitions means pass the whole definition (plus #define) through
392 static enum {dump_none, dump_only, dump_names, dump_definitions}
393 dump_macros = dump_none;
395 /* Nonzero means pass all #define and #undef directives which we actually
396 process through to the output stream. This feature is used primarily
397 to allow cc1 to record the #defines and #undefs for the sake of
398 debuggers which understand about preprocessor macros, but it may
399 also be useful with -E to figure out how symbols are defined, and
400 where they are defined. */
401 static int debug_output = 0;
403 /* Nonzero indicates special processing used by the pcp program. The
404 special effects of this mode are:
406 Inhibit all macro expansion, except those inside #if directives.
408 Process #define directives normally, and output their contents
411 Output preconditions to pcp_outfile indicating all the relevant
412 preconditions for use of this file in a later cpp run.
414 static FILE *pcp_outfile;
416 /* Nonzero means we are inside an IF during a -pcp run. In this mode
417 macro expansion is done, and preconditions are output for all macro
418 uses requiring them. */
419 static int pcp_inside_if;
421 /* Nonzero means never to include precompiled files.
422 This is 1 since there's no way now to make precompiled files,
423 so it's not worth testing for them. */
424 static int no_precomp = 1;
426 /* Nonzero means give all the error messages the ANSI standard requires. */
430 /* Nonzero means try to make failure to fit ANSI C an error. */
432 static int pedantic_errors;
434 /* Nonzero means don't print warning messages. -w. */
436 static int inhibit_warnings = 0;
438 /* Nonzero means warn if slash-star appears in a slash-star comment,
439 or if newline-backslash appears in a slash-slash comment. */
441 static int warn_comments;
443 /* Nonzero means warn if a macro argument is (or would be)
444 stringified with -traditional. */
446 static int warn_stringify;
448 /* Nonzero means warn if there are any trigraphs. */
450 static int warn_trigraphs;
452 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
456 /* Nonzero means warn if #import is used. */
458 static int warn_import = 1;
460 /* Nonzero means turn warnings into errors. */
462 static int warnings_are_errors;
464 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
468 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
472 /* Nonzero causes output not to be done,
473 but directives such as #define that have side effects
476 static int no_output;
478 /* Nonzero means this file was included with a -imacros or -include
479 command line and should not be recorded as an include file. */
481 static int no_record_file;
483 /* Nonzero means that we have finished processing the command line options.
484 This flag is used to decide whether or not to issue certain errors
487 static int done_initializing = 0;
489 /* Line where a newline was first seen in a string constant. */
491 static int multiline_string_line = 0;
493 /* I/O buffer structure.
494 The `fname' field is nonzero for source files and #include files
495 and for the dummy text used for -D and -U.
496 It is zero for rescanning results of macro expansion
497 and for expanding macro arguments. */
498 #define INPUT_STACK_MAX 400
499 static struct file_buf {
501 /* Filename specified with #line directive. */
503 /* Include file description. */
504 struct include_file *inc;
505 /* Record where in the search path this file was found.
506 For #include_next. */
507 struct file_name_list *dir;
512 /* Macro that this level is the expansion of.
513 Included so that we can reenable the macro
514 at the end of this level. */
515 struct hashnode *macro;
516 /* Value of if_stack at start of this file.
517 Used to prohibit unmatched #endif (etc) in an include file. */
518 struct if_stack *if_stack;
519 /* Object to be freed at end of input at this level. */
521 /* True if this is a header file included using <FILENAME>. */
522 char system_header_p;
523 } instack[INPUT_STACK_MAX];
525 static int last_error_tick; /* Incremented each time we print it. */
526 static int input_file_stack_tick; /* Incremented when the status changes. */
528 /* Current nesting level of input sources.
529 `instack[indepth]' is the level currently being read. */
530 static int indepth = -1;
531 #define CHECK_DEPTH(code) \
532 if (indepth >= (INPUT_STACK_MAX - 1)) \
534 error_with_line (line_for_error (instack[indepth].lineno), \
535 "macro or `#include' recursion too deep"); \
539 /* Current depth in #include directives that use <...>. */
540 static int system_include_depth = 0;
542 typedef struct file_buf FILE_BUF;
544 /* The output buffer. Its LENGTH field is the amount of room allocated
545 for the buffer, not the number of chars actually present. To get
546 that, subtract outbuf.buf from outbuf.bufp. */
548 #define OUTBUF_SIZE 10 /* initial size of output buffer */
549 static FILE_BUF outbuf;
551 /* Grow output buffer OBUF points at
552 so it can hold at least NEEDED more chars. */
554 #define check_expand(OBUF, NEEDED) \
555 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
556 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
558 struct file_name_list
560 struct file_name_list *next;
561 /* If the following is 1, it is a C-language system include
563 int c_system_include_path;
564 /* Mapping of file names for this directory. */
565 struct file_name_map *name_map;
566 /* Non-zero if name_map is valid. */
568 /* The include directory status. */
570 /* The include prefix: "" denotes the working directory,
571 otherwise fname must end in '/'.
572 The actual size is dynamically allocated. */
576 /* #include "file" looks in source file dir, then stack. */
577 /* #include <file> just looks in the stack. */
578 /* -I directories are added to the end, then the defaults are added. */
580 static struct default_include {
581 char *fname; /* The name of the directory. */
582 int cplusplus; /* Only look here if we're compiling C++. */
583 int cxx_aware; /* Includes in this directory don't need to
584 be wrapped in extern "C" when compiling
586 } include_defaults_array[]
587 #ifdef INCLUDE_DEFAULTS
591 /* Pick up GNU C++ specific include files. */
592 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
593 { OLD_GPLUSPLUS_INCLUDE_DIR, 1, 1 },
595 /* This is the dir for fixincludes. Put it just before
596 the files that we fix. */
597 { GCC_INCLUDE_DIR, 0, 0 },
598 /* For cross-compilation, this dir name is generated
599 automatically in Makefile.in. */
600 { CROSS_INCLUDE_DIR, 0, 0 },
601 #ifdef TOOL_INCLUDE_DIR
602 /* This is another place that the target system's headers might be. */
603 { TOOL_INCLUDE_DIR, 0, 0 },
605 #else /* not CROSS_COMPILE */
606 #ifdef LOCAL_INCLUDE_DIR
607 /* This should be /usr/local/include and should come before
608 the fixincludes-fixed header files. */
609 { LOCAL_INCLUDE_DIR, 0, 1 },
611 #ifdef TOOL_INCLUDE_DIR
612 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
613 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
614 { TOOL_INCLUDE_DIR, 0, 0 },
616 /* This is the dir for fixincludes. Put it just before
617 the files that we fix. */
618 { GCC_INCLUDE_DIR, 0, 0 },
619 /* Some systems have an extra dir of include files. */
620 #ifdef SYSTEM_INCLUDE_DIR
621 { SYSTEM_INCLUDE_DIR, 0, 0 },
623 { STANDARD_INCLUDE_DIR, 0, 0 },
624 #endif /* not CROSS_COMPILE */
627 #endif /* no INCLUDE_DEFAULTS */
629 /* The code looks at the defaults through this pointer, rather than through
630 the constant structure above. This pointer gets changed if an environment
631 variable specifies other defaults. */
632 static struct default_include *include_defaults = include_defaults_array;
634 static struct file_name_list *include = 0; /* First dir to search */
635 /* First dir to search for <file> */
636 /* This is the first element to use for #include <...>.
637 If it is 0, use the entire chain for such includes. */
638 static struct file_name_list *first_bracket_include = 0;
639 /* This is the first element in the chain that corresponds to
640 a directory of system header files. */
641 static struct file_name_list *first_system_include = 0;
642 static struct file_name_list *last_include = 0; /* Last in chain */
644 /* Chain of include directories to put at the end of the other chain. */
645 static struct file_name_list *after_include = 0;
646 static struct file_name_list *last_after_include = 0; /* Last in chain */
648 /* Chain to put at the start of the system include files. */
649 static struct file_name_list *before_system = 0;
650 static struct file_name_list *last_before_system = 0; /* Last in chain */
652 /* Directory prefix that should replace `/usr' in the standard
653 include file directories. */
654 static char *include_prefix;
656 /* Maintain and search list of included files. */
658 struct include_file {
659 struct include_file *next; /* for include_hashtab */
660 struct include_file *next_ino; /* for include_ino_hashtab */
662 /* If the following is the empty string, it means #pragma once
663 was seen in this include file, or #import was applied to the file.
664 Otherwise, if it is nonzero, it is a macro name.
665 Don't include the file again if that macro is defined. */
666 U_CHAR *control_macro;
667 /* Nonzero if the dependency on this include file has been output. */
672 /* Hash tables of files already included with #include or #import.
673 include_hashtab is by full name; include_ino_hashtab is by inode number. */
675 #define INCLUDE_HASHSIZE 61
676 static struct include_file *include_hashtab[INCLUDE_HASHSIZE];
677 static struct include_file *include_ino_hashtab[INCLUDE_HASHSIZE];
679 /* Global list of strings read in from precompiled files. This list
680 is kept in the order the strings are read in, with new strings being
681 added at the end through stringlist_tailp. We use this list to output
682 the strings at the end of the run.
684 static STRINGDEF *stringlist;
685 static STRINGDEF **stringlist_tailp = &stringlist;
688 /* Structure returned by create_definition */
689 typedef struct macrodef MACRODEF;
692 struct definition *defn;
697 enum sharp_token_type {
698 NO_SHARP_TOKEN = 0, /* token not present */
700 SHARP_TOKEN = '#', /* token spelled with # only */
701 WHITE_SHARP_TOKEN, /* token spelled with # and white space */
703 PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */
704 WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */
707 /* Structure allocated for every #define. For a simple replacement
710 nargs = -1, the `pattern' list is null, and the expansion is just
711 the replacement text. Nargs = 0 means a functionlike macro with no args,
713 #define getchar() getc (stdin) .
714 When there are args, the expansion is the replacement text with the
715 args squashed out, and the reflist is a list describing how to
716 build the output from the input: e.g., "3 chars, then the 1st arg,
717 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
718 The chars here come from the expansion. Whatever is left of the
719 expansion after the last arg-occurrence is copied after that arg.
720 Note that the reflist can be arbitrarily long---
721 its length depends on the number of times the arguments appear in
722 the replacement text, not how many args there are. Example:
723 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
725 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
726 where (x, y) means (nchars, argno). */
728 typedef struct definition DEFINITION;
731 int length; /* length of expansion string */
732 int predefined; /* True if the macro was builtin or */
733 /* came from the command line */
735 int line; /* Line number of definition */
736 char *file; /* File of definition */
737 char rest_args; /* Nonzero if last arg. absorbs the rest */
739 struct reflist *next;
741 enum sharp_token_type stringify; /* set if a # operator before arg */
742 enum sharp_token_type raw_before; /* set if a ## operator before arg */
743 enum sharp_token_type raw_after; /* set if a ## operator after arg */
745 char rest_args; /* Nonzero if this arg. absorbs the rest */
746 int nchars; /* Number of literal chars to copy before
747 this arg occurrence. */
748 int argno; /* Number of arg to substitute (origin-0) */
751 /* Names of macro args, concatenated in reverse order
752 with comma-space between them.
753 The only use of this is that we warn on redefinition
754 if this differs between the old and new definitions. */
759 /* different kinds of things that can appear in the value field
760 of a hash node. Actually, this may be useless now. */
768 * special extension string that can be added to the last macro argument to
769 * allow it to absorb the "rest" of the arguments when expanded. Ex:
770 * #define wow(a, b...) process (b, a, b)
771 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
772 * { wow (one, two); } -> { process (two, one, two); }
773 * if this "rest_arg" is used with the concat token '##' and if it is not
774 * supplied then the token attached to with ## will not be outputted. Ex:
775 * #define wow (a, b...) process (b ## , a, ## b)
776 * { wow (1, 2); } -> { process (2, 1, 2); }
777 * { wow (one); } -> { process (one); {
779 static char rest_extension[] = "...";
780 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
782 /* The structure of a node in the hash table. The hash table
783 has entries for all tokens defined by #define directives (type T_MACRO),
784 plus some special tokens like __LINE__ (these each have their own
785 type, and the appropriate code is run when that type of node is seen.
786 It does not contain control words like "#define", which are recognized
787 by a separate piece of code. */
789 /* different flavors of hash nodes --- also used in keyword table */
791 T_DEFINE = 1, /* the `#define' keyword */
792 T_INCLUDE, /* the `#include' keyword */
793 T_INCLUDE_NEXT, /* the `#include_next' keyword */
794 T_IMPORT, /* the `#import' keyword */
795 T_IFDEF, /* the `#ifdef' keyword */
796 T_IFNDEF, /* the `#ifndef' keyword */
797 T_IF, /* the `#if' keyword */
798 T_ELSE, /* `#else' */
799 T_PRAGMA, /* `#pragma' */
800 T_ELIF, /* `#elif' */
801 T_UNDEF, /* `#undef' */
802 T_LINE, /* `#line' */
803 T_ERROR, /* `#error' */
804 T_WARNING, /* `#warning' */
805 T_ENDIF, /* `#endif' */
806 T_SCCS, /* `#sccs', used on system V. */
807 T_IDENT, /* `#ident', used on system V. */
808 T_ASSERT, /* `#assert', taken from system V. */
809 T_UNASSERT, /* `#unassert', taken from system V. */
810 T_SPECLINE, /* special symbol `__LINE__' */
811 T_DATE, /* `__DATE__' */
812 T_FILE, /* `__FILE__' */
813 T_BASE_FILE, /* `__BASE_FILE__' */
814 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
815 T_VERSION, /* `__VERSION__' */
816 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
817 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
818 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
819 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
820 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
821 T_IMMEDIATE_PREFIX_TYPE, /* `__IMMEDIATE_PREFIX__' */
822 T_TIME, /* `__TIME__' */
823 T_CONST, /* Constant value, used by `__STDC__' */
824 T_MACRO, /* macro defined by `#define' */
825 T_DISABLED, /* macro temporarily turned off for rescan */
826 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
827 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
828 T_UNUSED /* Used for something not defined. */
832 struct hashnode *next; /* double links for easy deletion */
833 struct hashnode *prev;
834 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
835 chain is kept, in case the node is the head
836 of the chain and gets deleted. */
837 enum node_type type; /* type of special token */
838 int length; /* length of token, for quick comparison */
839 U_CHAR *name; /* the actual name */
840 union hashval value; /* pointer to expansion, or whatever */
843 typedef struct hashnode HASHNODE;
845 /* Some definitions for the hash table. The hash function MUST be
846 computed as shown in hashf () below. That is because the rescan
847 loop computes the hash value `on the fly' for most tokens,
848 in order to avoid the overhead of a lot of procedure calls to
849 the hashf () function. Hashf () only exists for the sake of
850 politeness, for use when speed isn't so important. */
852 #define HASHSIZE 1403
853 static HASHNODE *hashtab[HASHSIZE];
854 #define HASHSTEP(old, c) ((old << 2) + c)
855 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
857 /* Symbols to predefine. */
859 #ifdef CPP_PREDEFINES
860 static char *predefs = CPP_PREDEFINES;
862 static char *predefs = "";
865 /* We let tm.h override the types used here, to handle trivial differences
866 such as the choice of unsigned int or long unsigned int for size_t.
867 When machines start needing nontrivial differences in the size type,
868 it would be best to do something here to figure out automatically
869 from other information what type to use. */
871 /* The string value for __SIZE_TYPE__. */
874 #define SIZE_TYPE "long unsigned int"
877 /* The string value for __PTRDIFF_TYPE__. */
880 #define PTRDIFF_TYPE "long int"
883 /* The string value for __WCHAR_TYPE__. */
886 #define WCHAR_TYPE "int"
888 char * wchar_type = WCHAR_TYPE;
891 /* The string value for __USER_LABEL_PREFIX__ */
893 #ifndef USER_LABEL_PREFIX
894 #define USER_LABEL_PREFIX ""
897 /* The string value for __REGISTER_PREFIX__ */
899 #ifndef REGISTER_PREFIX
900 #define REGISTER_PREFIX ""
903 /* The string value for __IMMEDIATE_PREFIX__ */
905 #ifndef IMMEDIATE_PREFIX
906 #define IMMEDIATE_PREFIX ""
909 /* In the definition of a #assert name, this structure forms
910 a list of the individual values asserted.
911 Each value is itself a list of "tokens".
912 These are strings that are compared by name. */
914 struct tokenlist_list {
915 struct tokenlist_list *next;
916 struct arglist *tokens;
919 struct assertion_hashnode {
920 struct assertion_hashnode *next; /* double links for easy deletion */
921 struct assertion_hashnode *prev;
922 /* also, a back pointer to this node's hash
923 chain is kept, in case the node is the head
924 of the chain and gets deleted. */
925 struct assertion_hashnode **bucket_hdr;
926 int length; /* length of token, for quick comparison */
927 U_CHAR *name; /* the actual name */
928 /* List of token-sequences. */
929 struct tokenlist_list *value;
932 typedef struct assertion_hashnode ASSERTION_HASHNODE;
934 /* Some definitions for the hash table. The hash function MUST be
935 computed as shown in hashf below. That is because the rescan
936 loop computes the hash value `on the fly' for most tokens,
937 in order to avoid the overhead of a lot of procedure calls to
938 the hashf function. hashf only exists for the sake of
939 politeness, for use when speed isn't so important. */
941 #define ASSERTION_HASHSIZE 37
942 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
944 /* Nonzero means inhibit macroexpansion of what seem to be
945 assertion tests, in rescan. For #if. */
946 static int assertions_flag;
948 /* `struct directive' defines one #-directive, including how to handle it. */
950 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
953 int length; /* Length of name */
954 int (*func) DO_PROTO; /* Function to handle directive */
955 char *name; /* Name of directive */
956 enum node_type type; /* Code which describes which directive. */
957 char angle_brackets; /* Nonzero => <...> is special. */
958 char traditional_comments; /* Nonzero: keep comments if -traditional. */
959 char pass_thru; /* Copy directive to output:
960 if 1, copy if dumping definitions;
961 if 2, always copy, after preprocessing. */
964 /* These functions are declared to return int instead of void since they
965 are going to be placed in the table and some old compilers have trouble with
966 pointers to functions returning void. */
968 static int do_assert DO_PROTO;
969 static int do_define DO_PROTO;
970 static int do_elif DO_PROTO;
971 static int do_else DO_PROTO;
972 static int do_endif DO_PROTO;
973 static int do_error DO_PROTO;
974 static int do_ident DO_PROTO;
975 static int do_if DO_PROTO;
976 static int do_include DO_PROTO;
977 static int do_line DO_PROTO;
978 static int do_pragma DO_PROTO;
979 #ifdef SCCS_DIRECTIVE
980 static int do_sccs DO_PROTO;
982 static int do_unassert DO_PROTO;
983 static int do_undef DO_PROTO;
984 static int do_warning DO_PROTO;
985 static int do_xifdef DO_PROTO;
987 /* Here is the actual list of #-directives, most-often-used first. */
989 static struct directive directive_table[] = {
990 { 6, do_define, "define", T_DEFINE, 0, 1, 1},
991 { 2, do_if, "if", T_IF},
992 { 5, do_xifdef, "ifdef", T_IFDEF},
993 { 6, do_xifdef, "ifndef", T_IFNDEF},
994 { 5, do_endif, "endif", T_ENDIF},
995 { 4, do_else, "else", T_ELSE},
996 { 4, do_elif, "elif", T_ELIF},
997 { 4, do_line, "line", T_LINE},
998 { 7, do_include, "include", T_INCLUDE, 1},
999 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
1000 { 6, do_include, "import", T_IMPORT, 1},
1001 { 5, do_undef, "undef", T_UNDEF},
1002 { 5, do_error, "error", T_ERROR},
1003 { 7, do_warning, "warning", T_WARNING},
1004 #ifdef SCCS_DIRECTIVE
1005 { 4, do_sccs, "sccs", T_SCCS},
1007 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 2},
1008 { 5, do_ident, "ident", T_IDENT},
1009 { 6, do_assert, "assert", T_ASSERT},
1010 { 8, do_unassert, "unassert", T_UNASSERT},
1011 { -1, 0, "", T_UNUSED},
1014 /* When a directive handler is called,
1015 this points to the # (or the : of the %:) that started the directive. */
1016 U_CHAR *directive_start;
1018 /* table to tell if char can be part of a C identifier. */
1019 U_CHAR is_idchar[256];
1020 /* table to tell if char can be first char of a c identifier. */
1021 U_CHAR is_idstart[256];
1022 /* table to tell if c is horizontal space. */
1023 static U_CHAR is_hor_space[256];
1024 /* table to tell if c is horizontal or vertical space. */
1025 U_CHAR is_space[256];
1026 /* names of some characters */
1027 static char *char_name[256];
1029 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
1030 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
1032 static int errors = 0; /* Error counter for exit code */
1034 /* Name of output file, for error messages. */
1035 static char *out_fname;
1038 /* Stack of conditionals currently in progress
1039 (including both successful and failing conditionals). */
1042 struct if_stack *next; /* for chaining to the next stack frame */
1043 char *fname; /* copied from input when frame is made */
1044 int lineno; /* similarly */
1045 int if_succeeded; /* true if a leg of this if-group
1046 has been passed through rescan */
1047 U_CHAR *control_macro; /* For #ifndef at start of file,
1048 this is the macro name tested. */
1049 enum node_type type; /* type of last directive seen in this group */
1051 typedef struct if_stack IF_STACK_FRAME;
1052 static IF_STACK_FRAME *if_stack = NULL;
1054 /* Buffer of -M output. */
1055 static char *deps_buffer;
1057 /* Number of bytes allocated in above. */
1058 static int deps_allocated_size;
1060 /* Number of bytes used. */
1061 static int deps_size;
1063 /* Number of bytes since the last newline. */
1064 static int deps_column;
1066 /* Nonzero means -I- has been seen,
1067 so don't look for #include "foo" the source-file directory. */
1068 static int ignore_srcdir;
1070 static int safe_read PROTO((int, char *, int));
1071 static void safe_write PROTO((int, char *, int));
1073 int main PROTO((int, char **));
1075 static void path_include PROTO((char *));
1077 static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
1079 static void trigraph_pcp PROTO((FILE_BUF *));
1081 static void newline_fix PROTO((U_CHAR *));
1082 static void name_newline_fix PROTO((U_CHAR *));
1084 static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
1086 static void rescan PROTO((FILE_BUF *, int));
1088 static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
1090 static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
1092 static struct tm *timestamp PROTO((void));
1093 static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
1095 static int is_system_include PROTO((char *));
1096 static char *base_name PROTO((char *));
1097 static int absolute_filename PROTO((char *));
1098 static size_t simplify_filename PROTO((char *));
1100 static char *read_filename_string PROTO((int, FILE *));
1101 static struct file_name_map *read_name_map PROTO((char *));
1102 static int open_include_file PROTO((char *, struct file_name_list *, U_CHAR *, struct include_file **));
1103 static char *remap_include_file PROTO((char *, struct file_name_list *));
1104 static int lookup_ino_include PROTO((struct include_file *));
1106 static void finclude PROTO((int, struct include_file *, FILE_BUF *, int, struct file_name_list *));
1107 static void record_control_macro PROTO((struct include_file *, U_CHAR *));
1109 static char *check_precompiled PROTO((int, struct stat *, char *, char **));
1110 static int check_preconditions PROTO((char *));
1111 static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
1112 static void pcstring_used PROTO((HASHNODE *));
1113 static void write_output PROTO((void));
1114 static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
1116 static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
1118 static int check_macro_name PROTO((U_CHAR *, char *));
1119 static int compare_defs PROTO((DEFINITION *, DEFINITION *));
1120 static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
1122 static DEFINITION *collect_expansion PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
1124 int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
1125 static int compare_token_lists PROTO((struct arglist *, struct arglist *));
1127 static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
1128 static void free_token_list PROTO((struct arglist *));
1130 static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
1131 static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
1132 static void delete_assertion PROTO((ASSERTION_HASHNODE *));
1134 static void do_once PROTO((void));
1136 static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
1137 static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
1138 static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
1139 static void validate_else PROTO((U_CHAR *, U_CHAR *));
1141 static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
1142 static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
1143 static char *quote_string PROTO((char *, char *));
1144 static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
1146 /* Last arg to output_line_directive. */
1147 enum file_change_code {same_file, enter_file, leave_file};
1148 static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
1150 static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
1153 static char *macarg PROTO((struct argdata *, int));
1155 static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, int *, int *, int *, int));
1157 static int discard_comments PROTO((U_CHAR *, int, int));
1159 static int change_newlines PROTO((U_CHAR *, int));
1161 char *my_strerror PROTO((int));
1162 void error PRINTF_PROTO_1((char *, ...));
1163 static void verror PROTO((char *, va_list));
1164 static void error_from_errno PROTO((char *));
1165 void warning PRINTF_PROTO_1((char *, ...));
1166 static void vwarning PROTO((char *, va_list));
1167 static void error_with_line PRINTF_PROTO_2((int, char *, ...));
1168 static void verror_with_line PROTO((int, char *, va_list));
1169 static void vwarning_with_line PROTO((int, char *, va_list));
1170 static void warning_with_line PRINTF_PROTO_2((int, char *, ...));
1171 void pedwarn PRINTF_PROTO_1((char *, ...));
1172 void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
1173 static void pedwarn_with_file_and_line PRINTF_PROTO_3((char *, int, char *, ...));
1175 static void print_containing_files PROTO((void));
1177 static int line_for_error PROTO((int));
1178 static int grow_outbuf PROTO((FILE_BUF *, int));
1180 static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
1181 HASHNODE *lookup PROTO((U_CHAR *, int, int));
1182 static void delete_macro PROTO((HASHNODE *));
1183 static int hashf PROTO((U_CHAR *, int, int));
1185 static void dump_single_macro PROTO((HASHNODE *, FILE *));
1186 static void dump_all_macros PROTO((void));
1187 static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
1188 static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
1190 static void initialize_char_syntax PROTO((void));
1191 static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
1193 static void make_definition PROTO((char *, FILE_BUF *));
1194 static void make_undef PROTO((char *, FILE_BUF *));
1196 static void make_assertion PROTO((char *, char *));
1198 static struct file_name_list *new_include_prefix PROTO((struct file_name_list *, char *, char *));
1199 static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
1201 static void deps_output PROTO((char *, int));
1203 static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
1204 void fancy_abort PROTO((void)) __attribute__ ((noreturn));
1205 static void perror_with_name PROTO((char *));
1206 static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
1207 static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
1209 static void memory_full PROTO((void)) __attribute__ ((noreturn));
1210 GENERIC_PTR xmalloc PROTO((size_t));
1211 static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
1212 static GENERIC_PTR xcalloc PROTO((size_t, size_t));
1213 static char *savestring PROTO((char *));
1215 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1216 retrying if necessary. If MAX_READ_LEN is defined, read at most
1217 that bytes at a time. Return a negative value if an error occurs,
1218 otherwise return the actual number of bytes read,
1219 which must be LEN unless end-of-file was reached. */
1222 safe_read (desc, ptr, len)
1227 int left, rcount, nchars;
1233 if (rcount > MAX_READ_LEN)
1234 rcount = MAX_READ_LEN;
1236 nchars = read (desc, ptr, rcount);
1253 /* Write LEN bytes at PTR to descriptor DESC,
1254 retrying if necessary, and treating any real error as fatal.
1255 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1258 safe_write (desc, ptr, len)
1263 int wcount, written;
1267 #ifdef MAX_WRITE_LEN
1268 if (wcount > MAX_WRITE_LEN)
1269 wcount = MAX_WRITE_LEN;
1271 written = write (desc, ptr, wcount);
1278 pfatal_with_name (out_fname);
1295 char **pend_files = (char **) xmalloc (argc * sizeof (char *));
1296 char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
1297 char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1298 char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1299 char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
1301 /* Record the option used with each element of pend_assertions.
1302 This is preparation for supporting more than one option for making
1304 char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
1305 int inhibit_predefs = 0;
1306 int no_standard_includes = 0;
1307 int no_standard_cplusplus_includes = 0;
1308 int missing_newline = 0;
1310 /* Non-0 means don't output the preprocessed program. */
1311 int inhibit_output = 0;
1312 /* Non-0 means -v, so print the full set of include dirs. */
1315 /* File name which deps are being written to.
1316 This is 0 if deps are being written to stdout. */
1317 char *deps_file = 0;
1318 /* Fopen file mode to open deps_file with. */
1319 char *deps_mode = "a";
1320 /* Stream on which to print the dependency information. */
1321 FILE *deps_stream = 0;
1322 /* Target-name to write with the dependency information. */
1323 char *deps_target = 0;
1326 /* Get rid of any avoidable limit on stack size. */
1330 /* Set the stack limit huge so that alloca (particularly stringtab
1331 in dbxread.c) does not fail. */
1332 getrlimit (RLIMIT_STACK, &rlim);
1333 rlim.rlim_cur = rlim.rlim_max;
1334 setrlimit (RLIMIT_STACK, &rlim);
1336 #endif /* RLIMIT_STACK defined */
1339 signal (SIGPIPE, pipe_closed);
1342 progname = base_name (argv[0]);
1346 /* Remove extension from PROGNAME. */
1348 char *s = progname = savestring (progname);
1350 if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */
1351 if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */
1352 && (p[1] == 'e' || p[1] == 'E')
1353 && (p[2] == 'x' || p[2] == 'X')
1354 && (p[3] == 'e' || p[3] == 'E')
1363 /* Initialize is_idchar. */
1364 initialize_char_syntax ();
1366 no_line_directives = 0;
1368 dump_macros = dump_none;
1371 cplusplus_comments = 1;
1373 bzero ((char *) pend_files, argc * sizeof (char *));
1374 bzero ((char *) pend_defs, argc * sizeof (char *));
1375 bzero ((char *) pend_undefs, argc * sizeof (char *));
1376 bzero ((char *) pend_assertions, argc * sizeof (char *));
1377 bzero ((char *) pend_includes, argc * sizeof (char *));
1379 /* Process switches and find input file name. */
1381 for (i = 1; i < argc; i++) {
1382 if (argv[i][0] != '-') {
1383 if (out_fname != NULL)
1384 fatal ("Usage: %s [switches] input output", argv[0]);
1385 else if (in_fname != NULL)
1386 out_fname = argv[i];
1390 switch (argv[i][1]) {
1393 if (!strcmp (argv[i], "-include")) {
1395 fatal ("Filename missing after `-include' option");
1397 simplify_filename (pend_includes[i] = argv[++i]);
1399 if (!strcmp (argv[i], "-imacros")) {
1401 fatal ("Filename missing after `-imacros' option");
1403 simplify_filename (pend_files[i] = argv[++i]);
1405 if (!strcmp (argv[i], "-iprefix")) {
1407 fatal ("Filename missing after `-iprefix' option");
1409 include_prefix = argv[++i];
1411 if (!strcmp (argv[i], "-ifoutput")) {
1412 output_conditionals = 1;
1414 if (!strcmp (argv[i], "-isystem")) {
1415 struct file_name_list *dirtmp;
1417 if (! (dirtmp = new_include_prefix (NULL_PTR, "", argv[++i])))
1419 dirtmp->c_system_include_path = 1;
1421 if (before_system == 0)
1422 before_system = dirtmp;
1424 last_before_system->next = dirtmp;
1425 last_before_system = dirtmp; /* Tail follows the last one */
1427 /* Add directory to end of path for includes,
1428 with the default prefix at the front of its name. */
1429 if (!strcmp (argv[i], "-iwithprefix")) {
1430 struct file_name_list *dirtmp;
1433 if (include_prefix != 0)
1434 prefix = include_prefix;
1436 prefix = savestring (GCC_INCLUDE_DIR);
1437 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1438 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1439 prefix[strlen (prefix) - 7] = 0;
1442 if (! (dirtmp = new_include_prefix (NULL_PTR, prefix, argv[++i])))
1445 if (after_include == 0)
1446 after_include = dirtmp;
1448 last_after_include->next = dirtmp;
1449 last_after_include = dirtmp; /* Tail follows the last one */
1451 /* Add directory to main path for includes,
1452 with the default prefix at the front of its name. */
1453 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1454 struct file_name_list *dirtmp;
1457 if (include_prefix != 0)
1458 prefix = include_prefix;
1460 prefix = savestring (GCC_INCLUDE_DIR);
1461 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1462 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1463 prefix[strlen (prefix) - 7] = 0;
1466 dirtmp = new_include_prefix (NULL_PTR, prefix, argv[++i]);
1467 append_include_chain (dirtmp, dirtmp);
1469 /* Add directory to end of path for includes. */
1470 if (!strcmp (argv[i], "-idirafter")) {
1471 struct file_name_list *dirtmp;
1473 if (! (dirtmp = new_include_prefix (NULL_PTR, "", argv[++i])))
1476 if (after_include == 0)
1477 after_include = dirtmp;
1479 last_after_include->next = dirtmp;
1480 last_after_include = dirtmp; /* Tail follows the last one */
1485 if (out_fname != NULL)
1486 fatal ("Output filename specified twice");
1488 fatal ("Filename missing after -o option");
1489 out_fname = argv[++i];
1490 if (!strcmp (out_fname, "-"))
1495 if (!strcmp (argv[i], "-pedantic"))
1497 else if (!strcmp (argv[i], "-pedantic-errors")) {
1499 pedantic_errors = 1;
1500 } else if (!strcmp (argv[i], "-pcp")) {
1503 fatal ("Filename missing after -pcp option");
1504 pcp_fname = argv[++i];
1506 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1507 ? fopen (pcp_fname, "w")
1509 if (pcp_outfile == 0)
1510 pfatal_with_name (pcp_fname);
1516 if (!strcmp (argv[i], "-traditional")) {
1518 cplusplus_comments = 0;
1519 } else if (!strcmp (argv[i], "-trigraphs")) {
1525 if (! strcmp (argv[i], "-lang-c"))
1526 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 0;
1527 if (! strcmp (argv[i], "-lang-c89"))
1528 cplusplus = 0, cplusplus_comments = 0, c89 = 1, objc = 0;
1529 if (! strcmp (argv[i], "-lang-c++"))
1530 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 0;
1531 if (! strcmp (argv[i], "-lang-objc"))
1532 cplusplus = 0, cplusplus_comments = 1, c89 = 0, objc = 1;
1533 if (! strcmp (argv[i], "-lang-objc++"))
1534 cplusplus = 1, cplusplus_comments = 1, c89 = 0, objc = 1;
1535 if (! strcmp (argv[i], "-lang-asm"))
1537 if (! strcmp (argv[i], "-lint"))
1542 cplusplus = 1, cplusplus_comments = 1;
1546 inhibit_warnings = 1;
1550 if (!strcmp (argv[i], "-Wtrigraphs"))
1552 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1554 else if (!strcmp (argv[i], "-Wcomment"))
1556 else if (!strcmp (argv[i], "-Wno-comment"))
1558 else if (!strcmp (argv[i], "-Wcomments"))
1560 else if (!strcmp (argv[i], "-Wno-comments"))
1562 else if (!strcmp (argv[i], "-Wtraditional"))
1564 else if (!strcmp (argv[i], "-Wno-traditional"))
1566 else if (!strcmp (argv[i], "-Wundef"))
1568 else if (!strcmp (argv[i], "-Wno-undef"))
1570 else if (!strcmp (argv[i], "-Wimport"))
1572 else if (!strcmp (argv[i], "-Wno-import"))
1574 else if (!strcmp (argv[i], "-Werror"))
1575 warnings_are_errors = 1;
1576 else if (!strcmp (argv[i], "-Wno-error"))
1577 warnings_are_errors = 0;
1578 else if (!strcmp (argv[i], "-Wall"))
1586 /* The style of the choices here is a bit mixed.
1587 The chosen scheme is a hybrid of keeping all options in one string
1588 and specifying each option in a separate argument:
1589 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1590 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1591 -M[M][G][D file]. This is awkward to handle in specs, and is not
1593 /* ??? -MG must be specified in addition to one of -M or -MM.
1594 This can be relaxed in the future without breaking anything.
1595 The converse isn't true. */
1597 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1598 if (!strcmp (argv[i], "-MG"))
1600 print_deps_missing_files = 1;
1603 if (!strcmp (argv[i], "-M"))
1605 else if (!strcmp (argv[i], "-MM"))
1607 else if (!strcmp (argv[i], "-MD"))
1609 else if (!strcmp (argv[i], "-MMD"))
1611 /* For -MD and -MMD options, write deps on file named by next arg. */
1612 if (!strcmp (argv[i], "-MD")
1613 || !strcmp (argv[i], "-MMD")) {
1615 fatal ("Filename missing after %s option", argv[i]);
1617 deps_file = argv[i];
1620 /* For -M and -MM, write deps on standard output
1621 and suppress the usual output. */
1622 deps_stream = stdout;
1629 char *p = argv[i] + 2;
1631 while ((c = *p++)) {
1632 /* Arg to -d specifies what parts of macros to dump */
1635 dump_macros = dump_only;
1639 dump_macros = dump_names;
1642 dump_macros = dump_definitions;
1650 if (argv[i][2] == '3')
1655 fprintf (stderr, "GNU CPP version %s", version_string);
1656 #ifdef TARGET_VERSION
1659 fprintf (stderr, "\n");
1664 print_include_names = 1;
1668 if (argv[i][2] != 0)
1669 pend_defs[i] = argv[i] + 2;
1670 else if (i + 1 == argc)
1671 fatal ("Macro name missing after -D option");
1673 i++, pend_defs[i] = argv[i];
1680 if (argv[i][2] != 0)
1682 else if (i + 1 == argc)
1683 fatal ("Assertion missing after -A option");
1687 if (!strcmp (p, "-")) {
1688 /* -A- eliminates all predefined macros and assertions.
1689 Let's include also any that were specified earlier
1690 on the command line. That way we can get rid of any
1691 that were passed automatically in from GCC. */
1693 inhibit_predefs = 1;
1694 for (j = 0; j < i; j++)
1695 pend_defs[j] = pend_assertions[j] = 0;
1697 pend_assertions[i] = p;
1698 pend_assertion_options[i] = "-A";
1703 case 'U': /* JF #undef something */
1704 if (argv[i][2] != 0)
1705 pend_undefs[i] = argv[i] + 2;
1706 else if (i + 1 == argc)
1707 fatal ("Macro name missing after -U option");
1709 pend_undefs[i] = argv[i+1], i++;
1713 put_out_comments = 1;
1716 case 'E': /* -E comes from cc -E; ignore it. */
1720 no_line_directives = 1;
1723 case '$': /* Don't include $ in identifiers. */
1724 is_idchar['$'] = is_idstart['$'] = 0;
1727 case 'I': /* Add directory to path for includes. */
1729 struct file_name_list *dirtmp;
1731 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
1733 /* Don't use any preceding -I directories for #include <...>. */
1734 first_bracket_include = 0;
1737 dirtmp = new_include_prefix (last_include, "",
1738 argv[i][2] ? argv[i] + 2 : argv[++i]);
1739 append_include_chain (dirtmp, dirtmp);
1745 if (!strcmp (argv[i], "-nostdinc"))
1746 /* -nostdinc causes no default include directories.
1747 You must specify all include-file directories with -I. */
1748 no_standard_includes = 1;
1749 else if (!strcmp (argv[i], "-nostdinc++"))
1750 /* -nostdinc++ causes no default C++-specific include directories. */
1751 no_standard_cplusplus_includes = 1;
1752 else if (!strcmp (argv[i], "-noprecomp"))
1757 /* Sun compiler passes undocumented switch "-undef".
1758 Let's assume it means to inhibit the predefined symbols. */
1759 inhibit_predefs = 1;
1762 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1763 if (in_fname == NULL) {
1766 } else if (out_fname == NULL) {
1769 } /* else fall through into error */
1772 fatal ("Invalid option `%s'", argv[i]);
1777 /* Add dirs from CPATH after dirs from -I. */
1778 /* There seems to be confusion about what CPATH should do,
1779 so for the moment it is not documented. */
1780 /* Some people say that CPATH should replace the standard include dirs,
1781 but that seems pointless: it comes before them, so it overrides them
1783 cp = getenv ("CPATH");
1784 if (cp && ! no_standard_includes)
1787 /* Initialize output buffer */
1789 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1790 outbuf.bufp = outbuf.buf;
1791 outbuf.length = OUTBUF_SIZE;
1793 /* Do partial setup of input buffer for the sake of generating
1794 early #line directives (when -g is in effect). */
1796 fp = &instack[++indepth];
1797 if (in_fname == NULL)
1799 fp->nominal_fname = fp->fname = in_fname;
1802 /* In C++, wchar_t is a distinct basic type, and we can expect
1803 __wchar_t to be defined by cc1plus. */
1805 wchar_type = "__wchar_t";
1807 /* Install __LINE__, etc. Must follow initialize_char_syntax
1808 and option processing. */
1809 initialize_builtins (fp, &outbuf);
1811 /* Do standard #defines and assertions
1812 that identify system and machine type. */
1814 if (!inhibit_predefs) {
1815 char *p = (char *) alloca (strlen (predefs) + 1);
1816 strcpy (p, predefs);
1819 while (*p == ' ' || *p == '\t')
1821 /* Handle -D options. */
1822 if (p[0] == '-' && p[1] == 'D') {
1824 while (*p && *p != ' ' && *p != '\t')
1829 output_line_directive (fp, &outbuf, 0, same_file);
1830 make_definition (q, &outbuf);
1831 while (*p == ' ' || *p == '\t')
1833 } else if (p[0] == '-' && p[1] == 'A') {
1834 /* Handle -A options (assertions). */
1843 past_name = assertion;
1844 /* Locate end of name. */
1845 while (*past_name && *past_name != ' '
1846 && *past_name != '\t' && *past_name != '(')
1848 /* Locate `(' at start of value. */
1850 while (*value && (*value == ' ' || *value == '\t'))
1852 if (*value++ != '(')
1854 while (*value && (*value == ' ' || *value == '\t'))
1857 /* Locate end of value. */
1858 while (*past_value && *past_value != ' '
1859 && *past_value != '\t' && *past_value != ')')
1861 termination = past_value;
1862 while (*termination && (*termination == ' ' || *termination == '\t'))
1864 if (*termination++ != ')')
1866 if (*termination && *termination != ' ' && *termination != '\t')
1868 /* Temporarily null-terminate the value. */
1869 save_char = *termination;
1870 *termination = '\0';
1871 /* Install the assertion. */
1872 make_assertion ("-A", assertion);
1873 *termination = (char) save_char;
1875 while (*p == ' ' || *p == '\t')
1883 /* Now handle the command line options. */
1885 /* Do -U's, -D's and -A's in the order they were seen. */
1886 for (i = 1; i < argc; i++) {
1887 if (pend_undefs[i]) {
1889 output_line_directive (fp, &outbuf, 0, same_file);
1890 make_undef (pend_undefs[i], &outbuf);
1894 output_line_directive (fp, &outbuf, 0, same_file);
1895 make_definition (pend_defs[i], &outbuf);
1897 if (pend_assertions[i])
1898 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1901 done_initializing = 1;
1903 { /* Read the appropriate environment variable and if it exists
1904 replace include_defaults with the listed path. */
1906 switch ((objc << 1) + cplusplus)
1909 epath = getenv ("C_INCLUDE_PATH");
1912 epath = getenv ("CPLUS_INCLUDE_PATH");
1915 epath = getenv ("OBJC_INCLUDE_PATH");
1918 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
1921 /* If the environment var for this language is set,
1922 add to the default list of include directories. */
1925 char *startp, *endp;
1927 for (num_dirs = 1, startp = epath; *startp; startp++)
1928 if (*startp == PATH_SEPARATOR)
1931 = (struct default_include *) xmalloc ((num_dirs
1932 * sizeof (struct default_include))
1933 + sizeof (include_defaults_array));
1934 startp = endp = epath;
1938 if (c == PATH_SEPARATOR || !c) {
1940 include_defaults[num_dirs].fname
1941 = startp == endp ? "." : savestring (startp);
1943 include_defaults[num_dirs].cplusplus = cplusplus;
1944 include_defaults[num_dirs].cxx_aware = 1;
1951 /* Put the usual defaults back in at the end. */
1952 bcopy ((char *) include_defaults_array,
1953 (char *) &include_defaults[num_dirs],
1954 sizeof (include_defaults_array));
1958 append_include_chain (before_system, last_before_system);
1959 first_system_include = before_system;
1961 /* Unless -fnostdinc,
1962 tack on the standard include file dirs to the specified list */
1963 if (!no_standard_includes) {
1964 struct default_include *p = include_defaults;
1965 char *specd_prefix = include_prefix;
1966 char *default_prefix = savestring (GCC_INCLUDE_DIR);
1967 int default_len = 0;
1968 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1969 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1970 default_len = strlen (default_prefix) - 7;
1971 default_prefix[default_len] = 0;
1973 /* Search "translated" versions of GNU directories.
1974 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1975 if (specd_prefix != 0 && default_len != 0)
1976 for (p = include_defaults; p->fname; p++) {
1977 /* Some standard dirs are only for C++. */
1978 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1979 /* Does this dir start with the prefix? */
1980 if (!strncmp (p->fname, default_prefix, default_len)) {
1981 /* Yes; change prefix and add to search list. */
1982 struct file_name_list *new
1983 = new_include_prefix (NULL_PTR, specd_prefix,
1984 p->fname + default_len);
1986 new->c_system_include_path = !p->cxx_aware;
1987 append_include_chain (new, new);
1988 if (first_system_include == 0)
1989 first_system_include = new;
1994 /* Search ordinary names for GNU include directories. */
1995 for (p = include_defaults; p->fname; p++) {
1996 /* Some standard dirs are only for C++. */
1997 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1998 struct file_name_list *new
1999 = new_include_prefix (NULL_PTR, "", p->fname);
2001 new->c_system_include_path = !p->cxx_aware;
2002 append_include_chain (new, new);
2003 if (first_system_include == 0)
2004 first_system_include = new;
2010 /* Tack the after_include chain at the end of the include chain. */
2011 append_include_chain (after_include, last_after_include);
2012 if (first_system_include == 0)
2013 first_system_include = after_include;
2015 /* With -v, print the list of dirs to search. */
2017 struct file_name_list *p;
2018 fprintf (stderr, "#include \"...\" search starts here:\n");
2019 for (p = include; p; p = p->next) {
2020 if (p == first_bracket_include)
2021 fprintf (stderr, "#include <...> search starts here:\n");
2023 fprintf (stderr, " .\n");
2024 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
2025 fprintf (stderr, " %s\n", p->fname);
2027 /* Omit trailing '/'. */
2028 fprintf (stderr, " %.*s\n", (int) strlen (p->fname) - 1, p->fname);
2030 fprintf (stderr, "End of search list.\n");
2033 /* -MG doesn't select the form of output and must be specified with one of
2034 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
2035 inhibit compilation. */
2036 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
2037 fatal ("-MG must be specified with one of -M or -MM");
2039 /* Either of two environment variables can specify output of deps.
2040 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
2041 where OUTPUT_FILE is the file to write deps info to
2042 and DEPS_TARGET is the target to mention in the deps. */
2045 && (getenv ("SUNPRO_DEPENDENCIES") != 0
2046 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
2047 char *spec = getenv ("DEPENDENCIES_OUTPUT");
2052 spec = getenv ("SUNPRO_DEPENDENCIES");
2059 /* Find the space before the DEPS_TARGET, if there is one. */
2060 /* This should use index. (mrs) */
2061 while (*s != 0 && *s != ' ') s++;
2063 deps_target = s + 1;
2064 output_file = xmalloc (s - spec + 1);
2065 bcopy (spec, output_file, s - spec);
2066 output_file[s - spec] = 0;
2073 deps_file = output_file;
2077 /* For -M, print the expected object file name
2078 as the target of this Make-rule. */
2080 deps_allocated_size = 200;
2081 deps_buffer = xmalloc (deps_allocated_size);
2087 deps_output (deps_target, ':');
2088 } else if (*in_fname == 0) {
2089 deps_output ("-", ':');
2094 q = base_name (in_fname);
2096 /* Copy remainder to mungable area. */
2097 p = (char *) alloca (strlen(q) + 8);
2100 /* Output P, but remove known suffixes. */
2104 && p[len - 2] == '.'
2105 && index("cCsSm", p[len - 1]))
2108 && p[len - 3] == '.'
2109 && p[len - 2] == 'c'
2110 && p[len - 1] == 'c')
2113 && p[len - 4] == '.'
2114 && p[len - 3] == 'c'
2115 && p[len - 2] == 'x'
2116 && p[len - 1] == 'x')
2119 && p[len - 4] == '.'
2120 && p[len - 3] == 'c'
2121 && p[len - 2] == 'p'
2122 && p[len - 1] == 'p')
2125 /* Supply our own suffix. */
2132 deps_output (p, ':');
2133 deps_output (in_fname, ' ');
2137 /* Scan the -imacros files before the main input.
2138 Much like #including them, but with no_output set
2139 so that only their macro definitions matter. */
2141 no_output++; no_record_file++;
2142 for (i = 1; i < argc; i++)
2143 if (pend_files[i]) {
2144 struct include_file *inc;
2145 int fd = open_include_file (pend_files[i], NULL_PTR, NULL_PTR, &inc);
2147 perror_with_name (pend_files[i]);
2148 return FATAL_EXIT_CODE;
2150 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2152 no_output--; no_record_file--;
2154 /* Copy the entire contents of the main input file into
2155 the stacked input buffer previously allocated for it. */
2157 /* JF check for stdin */
2158 if (in_fname == NULL || *in_fname == 0) {
2161 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2164 if (fstat (f, &st) != 0)
2165 pfatal_with_name (in_fname);
2166 fp->nominal_fname = fp->fname = in_fname;
2168 fp->system_header_p = 0;
2169 /* JF all this is mine about reading pipes and ttys */
2170 if (! S_ISREG (st.st_mode)) {
2171 /* Read input from a file that is not a normal disk file.
2172 We cannot preallocate a buffer with the correct size,
2173 so we must read in the file a piece at the time and make it bigger. */
2178 if (S_ISDIR (st.st_mode))
2179 fatal ("Input file `%s' is a directory", in_fname);
2183 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2185 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2186 if (cnt < 0) goto perror; /* error! */
2188 if (size != bsize) break; /* End of file */
2190 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2194 /* Read a file whose size we can determine in advance.
2195 For the sake of VMS, st.st_size is just an upper bound. */
2196 fp->buf = (U_CHAR *) xmalloc (st.st_size + 2);
2197 fp->length = safe_read (f, (char *) fp->buf, st.st_size);
2198 if (fp->length < 0) goto perror;
2201 fp->if_stack = if_stack;
2203 /* Make sure data ends with a newline. And put a null after it. */
2205 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2206 /* Backslash-newline at end is not good enough. */
2207 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2208 fp->buf[fp->length++] = '\n';
2209 missing_newline = 1;
2211 fp->buf[fp->length] = '\0';
2213 /* Unless inhibited, convert trigraphs in the input. */
2218 /* Now that we know the input file is valid, open the output. */
2220 if (!out_fname || !strcmp (out_fname, ""))
2221 out_fname = "stdout";
2222 else if (! freopen (out_fname, "w", stdout))
2223 pfatal_with_name (out_fname);
2225 output_line_directive (fp, &outbuf, 0, same_file);
2227 /* Scan the -include files before the main input. */
2230 for (i = 1; i < argc; i++)
2231 if (pend_includes[i]) {
2232 struct include_file *inc;
2233 int fd = open_include_file (pend_includes[i], NULL_PTR, NULL_PTR, &inc);
2235 perror_with_name (pend_includes[i]);
2236 return FATAL_EXIT_CODE;
2238 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2242 /* Scan the input, processing macros and directives. */
2244 rescan (&outbuf, 0);
2246 if (missing_newline)
2249 if (pedantic && missing_newline)
2250 pedwarn ("file does not end in newline");
2252 /* Now we have processed the entire input
2253 Write whichever kind of output has been requested. */
2255 if (dump_macros == dump_only)
2257 else if (! inhibit_output) {
2262 /* Don't actually write the deps file if compilation has failed. */
2264 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2265 pfatal_with_name (deps_file);
2266 fputs (deps_buffer, deps_stream);
2267 putc ('\n', deps_stream);
2269 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2270 fatal ("I/O error on output");
2275 if (pcp_outfile && pcp_outfile != stdout
2276 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2277 fatal ("I/O error on `-pcp' output");
2279 if (ferror (stdout) || fclose (stdout) != 0)
2280 fatal ("I/O error on output");
2283 exit (FATAL_EXIT_CODE);
2284 exit (SUCCESS_EXIT_CODE);
2287 pfatal_with_name (in_fname);
2291 /* Given a colon-separated list of file names PATH,
2292 add all the names to the search path for include files. */
2306 struct file_name_list *dirtmp;
2308 /* Find the end of this name. */
2309 while ((c = *q++) != PATH_SEPARATOR && c)
2313 dirtmp = new_include_prefix (last_include, "", p == q ? "." : p);
2315 append_include_chain (dirtmp, dirtmp);
2317 /* Advance past this name. */
2324 /* Return the address of the first character in S that equals C.
2325 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2326 Return 0 if there is no such character. Assume that C itself is not '\0'.
2327 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2328 but unfortunately memchr isn't autoconfigured yet. */
2336 char *p = (char *) s;
2338 char *q = index (p, c);
2340 return (U_CHAR *) q;
2342 size_t l = strlen (p);
2352 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2353 before main CCCP processing. Name `pcp' is also in honor of the
2354 drugs the trigraph designers must have been on.
2356 Using an extra pass through the buffer takes a little extra time,
2357 but is infinitely less hairy than trying to handle trigraphs inside
2358 strings, etc. everywhere, and also makes sure that trigraphs are
2359 only translated in the top level of processing. */
2365 register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
2368 fptr = bptr = sptr = buf->buf;
2369 lptr = fptr + buf->length;
2370 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2407 len = sptr - fptr - 2;
2409 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2410 C, this will be memmove (). */
2411 if (bptr != fptr && len > 0)
2412 bcopy ((char *) fptr, (char *) bptr, len);
2418 len = buf->length - (fptr - buf->buf);
2419 if (bptr != fptr && len > 0)
2420 bcopy ((char *) fptr, (char *) bptr, len);
2421 buf->length -= fptr - bptr;
2422 buf->buf[buf->length] = '\0';
2423 if (warn_trigraphs && fptr != bptr)
2424 warning_with_line (0, "%lu trigraph(s) encountered",
2425 (unsigned long) (fptr - bptr) / 2);
2428 /* Move all backslash-newline pairs out of embarrassing places.
2429 Exchange all such pairs following BP
2430 with any potentially-embarrassing characters that follow them.
2431 Potentially-embarrassing characters are / and *
2432 (because a backslash-newline inside a comment delimiter
2433 would cause it not to be recognized). */
2439 register U_CHAR *p = bp;
2441 /* First count the backslash-newline pairs here. */
2443 while (p[0] == '\\' && p[1] == '\n')
2446 /* What follows the backslash-newlines is not embarrassing. */
2448 if (*p != '/' && *p != '*')
2451 /* Copy all potentially embarrassing characters
2452 that follow the backslash-newline pairs
2453 down to where the pairs originally started. */
2455 while (*p == '*' || *p == '/')
2458 /* Now write the same number of pairs after the embarrassing chars. */
2465 /* Like newline_fix but for use within a directive-name.
2466 Move any backslash-newlines up past any following symbol constituents. */
2469 name_newline_fix (bp)
2472 register U_CHAR *p = bp;
2474 /* First count the backslash-newline pairs here. */
2475 while (p[0] == '\\' && p[1] == '\n')
2478 /* What follows the backslash-newlines is not embarrassing. */
2483 /* Copy all potentially embarrassing characters
2484 that follow the backslash-newline pairs
2485 down to where the pairs originally started. */
2487 while (is_idchar[*p])
2490 /* Now write the same number of pairs after the embarrassing chars. */
2497 /* Look for lint commands in comments.
2499 When we come in here, ibp points into a comment. Limit is as one expects.
2500 scan within the comment -- it should start, after lwsp, with a lint command.
2501 If so that command is returned as a (constant) string.
2503 Upon return, any arg will be pointed to with argstart and will be
2504 arglen long. Note that we don't parse that arg since it will just
2505 be printed out again. */
2508 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2509 register U_CHAR *ibp;
2510 register U_CHAR *limit;
2511 U_CHAR **argstart; /* point to command arg */
2512 int *arglen, *cmdlen; /* how long they are */
2514 HOST_WIDE_INT linsize;
2515 register U_CHAR *numptr; /* temp for arg parsing */
2519 SKIP_WHITE_SPACE (ibp);
2521 if (ibp >= limit) return NULL;
2523 linsize = limit - ibp;
2525 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2526 if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
2528 return "NOTREACHED";
2530 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2534 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2536 return "LINTLIBRARY";
2538 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2540 ibp += 7; linsize -= 7;
2541 if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
2543 /* OK, read a number */
2544 for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
2546 *arglen = numptr - *argstart;
2553 * The main loop of the program.
2555 * Read characters from the input stack, transferring them to the
2558 * Macros are expanded and push levels on the input stack.
2559 * At the end of such a level it is popped off and we keep reading.
2560 * At the end of any other kind of level, we return.
2561 * #-directives are handled, except within macros.
2563 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2564 * and insert them when appropriate. This is set while scanning macro
2565 * arguments before substitution. It is zero when scanning for final output.
2566 * There are three types of Newline markers:
2567 * * Newline - follows a macro name that was not expanded
2568 * because it appeared inside an expansion of the same macro.
2569 * This marker prevents future expansion of that identifier.
2570 * When the input is rescanned into the final output, these are deleted.
2571 * These are also deleted by ## concatenation.
2572 * * Newline Space (or Newline and any other whitespace character)
2573 * stands for a place that tokens must be separated or whitespace
2574 * is otherwise desirable, but where the ANSI standard specifies there
2575 * is no whitespace. This marker turns into a Space (or whichever other
2576 * whitespace char appears in the marker) in the final output,
2577 * but it turns into nothing in an argument that is stringified with #.
2578 * Such stringified arguments are the only place where the ANSI standard
2579 * specifies with precision that whitespace may not appear.
2581 * During this function, IP->bufp is kept cached in IBP for speed of access.
2582 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2583 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2584 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2585 * explicitly, and before RECACHE, since RECACHE uses OBP.
2589 rescan (op, output_marks)
2593 /* Character being scanned in main loop. */
2596 /* Length of pending accumulated identifier. */
2597 register int ident_length = 0;
2599 /* Hash code of pending accumulated identifier. */
2600 register int hash = 0;
2602 /* Current input level (&instack[indepth]). */
2605 /* Pointer for scanning input. */
2606 register U_CHAR *ibp;
2608 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2609 register U_CHAR *limit;
2611 /* Pointer for storing output. */
2612 register U_CHAR *obp;
2614 /* REDO_CHAR is nonzero if we are processing an identifier
2615 after backing up over the terminating character.
2616 Sometimes we process an identifier without backing up over
2617 the terminating character, if the terminating character
2618 is not special. Backing up is done so that the terminating character
2619 will be dispatched on again once the identifier is dealt with. */
2622 /* 1 if within an identifier inside of which a concatenation
2623 marker (Newline -) has been seen. */
2624 int concatenated = 0;
2626 /* While scanning a comment or a string constant,
2627 this records the line it started on, for error messages. */
2630 /* Record position of last `real' newline. */
2631 U_CHAR *beg_of_line;
2633 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2636 do { ip->macro->type = T_MACRO; \
2637 if (ip->free_ptr) free (ip->free_ptr); \
2638 --indepth; } while (0)
2640 /* Reload `rescan's local variables that describe the current
2641 level of the input stack. */
2644 do { ip = &instack[indepth]; \
2646 limit = ip->buf + ip->length; \
2648 check_expand (op, limit - ibp); \
2650 obp = op->bufp; } while (0)
2652 if (no_output && instack[indepth].fname != 0)
2653 skip_if_group (&instack[indepth], 1, NULL);
2660 /* Our caller must always put a null after the end of
2661 the input at each input stack level. */
2671 if (*ibp == '\n' && !ip->macro) {
2672 /* At the top level, always merge lines ending with backslash-newline,
2673 even in middle of identifier. But do not merge lines in a macro,
2674 since backslash might be followed by a newline-space marker. */
2677 --obp; /* remove backslash from obuf */
2680 /* If ANSI, backslash is just another character outside a string. */
2683 /* Otherwise, backslash suppresses specialness of following char,
2684 so copy it here to prevent the switch from seeing it.
2685 But first get any pending identifier processed. */
2686 if (ident_length > 0)
2693 if (ident_length || ip->macro || traditional)
2695 while (*ibp == '\\' && ibp[1] == '\n') {
2701 /* Treat this %: digraph as if it were #. */
2705 if (assertions_flag) {
2708 /* Copy #foo (bar lose) without macro expansion. */
2709 obp[-1] = '#'; /* In case it was '%'. */
2710 SKIP_WHITE_SPACE (ibp);
2711 while (is_idchar[*ibp])
2713 SKIP_WHITE_SPACE (ibp);
2716 skip_paren_group (ip);
2717 bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
2718 obp += ip->bufp - ibp;
2724 /* If this is expanding a macro definition, don't recognize
2725 preprocessing directives. */
2728 /* If this is expand_into_temp_buffer,
2729 don't recognize them either. Warn about them
2730 only after an actual newline at this level,
2731 not at the beginning of the input level. */
2733 if (ip->buf != beg_of_line)
2734 warning ("preprocessing directive not recognized within macro arg");
2741 /* # keyword: a # must be first nonblank char on the line */
2742 if (beg_of_line == 0)
2747 /* Scan from start of line, skipping whitespace, comments
2748 and backslash-newlines, and see if we reach this #.
2749 If not, this # is not special. */
2751 /* If -traditional, require # to be at beginning of line. */
2754 if (is_hor_space[*bp])
2756 else if (*bp == '\\' && bp[1] == '\n')
2758 else if (*bp == '/' && bp[1] == '*') {
2760 while (!(*bp == '*' && bp[1] == '/'))
2764 /* There is no point in trying to deal with C++ // comments here,
2765 because if there is one, then this # must be part of the
2766 comment and we would never reach here. */
2772 while (bp[1] == '\\' && bp[2] == '\n')
2776 /* %: appears at start of line; skip past the ':' too. */
2785 /* This # can start a directive. */
2787 --obp; /* Don't copy the '#' */
2791 if (! handle_directive (ip, op)) {
2795 /* Not a known directive: treat it as ordinary text.
2796 IP, OP, IBP, etc. have not been changed. */
2797 if (no_output && instack[indepth].fname) {
2798 /* If not generating expanded output,
2799 what we do with ordinary text is skip it.
2800 Discard everything until next # directive. */
2801 skip_if_group (&instack[indepth], 1, 0);
2806 *obp++ = '#'; /* Copy # (even if it was originally %:). */
2807 /* Don't expand an identifier that could be a macro directive.
2808 (Section 3.8.3 of the ANSI C standard) */
2809 SKIP_WHITE_SPACE (ibp);
2810 if (is_idstart[*ibp])
2813 while (is_idchar[*ibp])
2821 /* A # directive has been successfully processed. */
2822 /* If not generating expanded output, ignore everything until
2823 next # directive. */
2824 if (no_output && instack[indepth].fname)
2825 skip_if_group (&instack[indepth], 1, 0);
2831 case '\"': /* skip quoted string */
2833 /* A single quoted string is treated like a double -- some
2834 programs (e.g., troff) are perverse this way */
2836 /* Handle any pending identifier;
2837 but the L in L'...' or L"..." is not an identifier. */
2839 && ! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2842 start_line = ip->lineno;
2844 /* Skip ahead to a matching quote. */
2848 if (ip->macro != 0) {
2849 /* try harder: this string crosses a macro expansion boundary.
2850 This can happen naturally if -traditional.
2851 Otherwise, only -D can make a macro with an unmatched quote. */
2857 error_with_line (line_for_error (start_line),
2858 "unterminated string or character constant");
2859 error_with_line (multiline_string_line,
2860 "possible real start of unterminated constant");
2861 multiline_string_line = 0;
2870 /* Traditionally, end of line ends a string constant with no error.
2871 So exit the loop and record the new line. */
2877 error_with_line (line_for_error (start_line),
2878 "unterminated character constant");
2881 if (multiline_string_line == 0) {
2883 pedwarn_with_line (line_for_error (start_line),
2884 "string constant runs past end of line");
2885 multiline_string_line = ip->lineno - 1;
2893 /* Backslash newline is replaced by nothing at all,
2894 but keep the line counts correct. */
2899 /* ANSI stupidly requires that in \\ the second \
2900 is *not* prevented from combining with a newline. */
2901 while (*ibp == '\\' && ibp[1] == '\n') {
2920 if (*ibp == '\\' && ibp[1] == '\n')
2924 && !(cplusplus_comments && *ibp == '/'))
2932 /* C++ style comment... */
2933 start_line = ip->lineno;
2935 /* Comments are equivalent to spaces. */
2936 if (! put_out_comments)
2940 U_CHAR *before_bp = ibp;
2942 while (++ibp < limit) {
2944 if (ibp[-1] != '\\') {
2945 if (put_out_comments) {
2946 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
2947 obp += ibp - before_bp;
2952 warning ("multiline `//' comment");
2954 /* Copy the newline into the output buffer, in order to
2955 avoid the pain of a #line every time a multiline comment
2957 if (!put_out_comments)
2966 /* Ordinary C comment. Skip it, optionally copying it to output. */
2968 start_line = ip->lineno;
2970 ++ibp; /* Skip the star. */
2972 /* If this cpp is for lint, we peek inside the comments: */
2976 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2978 if (lintcmd != NULL) {
2980 check_expand (op, cmdlen + arglen + 14);
2982 /* I believe it is always safe to emit this newline: */
2984 bcopy ("#pragma lint ", (char *) obp, 13);
2986 bcopy (lintcmd, (char *) obp, cmdlen);
2991 bcopy (argbp, (char *) obp, arglen);
2995 /* OK, now bring us back to the state we were in before we entered
2996 this branch. We need #line because the #pragma's newline always
2997 messes up the line count. */
2999 output_line_directive (ip, op, 0, same_file);
3000 check_expand (op, limit - ibp + 2);
3006 /* Comments are equivalent to spaces.
3007 Note that we already output the slash; we might not want it.
3008 For -traditional, a comment is equivalent to nothing. */
3009 if (! put_out_comments) {
3019 U_CHAR *before_bp = ibp;
3024 if (ibp[-2] == '/' && warn_comments)
3025 warning ("`/*' within comment");
3026 if (*ibp == '\\' && ibp[1] == '\n')
3034 /* Copy the newline into the output buffer, in order to
3035 avoid the pain of a #line every time a multiline comment
3037 if (!put_out_comments)
3044 error_with_line (line_for_error (start_line),
3045 "unterminated comment");
3054 if (put_out_comments) {
3055 bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
3056 obp += ibp - before_bp;
3062 if (! is_idchar['$'])
3065 pedwarn ("`$' in identifier");
3068 case '0': case '1': case '2': case '3': case '4':
3069 case '5': case '6': case '7': case '8': case '9':
3070 /* If digit is not part of identifier, it starts a number,
3071 which means that following letters are not an identifier.
3072 "0x5" does not refer to an identifier "x5".
3073 So copy all alphanumerics that follow without accumulating
3074 as an identifier. Periods also, for sake of "3.e7". */
3076 if (ident_length == 0) {
3078 while (ibp[0] == '\\' && ibp[1] == '\n') {
3083 if (!is_idchar[c] && c != '.') {
3088 /* A sign can be part of a preprocessing number
3089 if it follows an `e' or `p'. */
3090 if (c == 'e' || c == 'E' || c == 'p' || c == 'P') {
3091 while (ibp[0] == '\\' && ibp[1] == '\n') {
3095 if (*ibp == '+' || *ibp == '-') {
3097 /* But traditional C does not let the token go past the sign,
3098 and C89 does not allow `p'. */
3099 if (traditional || (c89 && (c == 'p' || c == 'P')))
3109 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3110 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3111 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3112 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3114 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3115 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3116 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3117 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3121 /* Compute step of hash function, to avoid a proc call on every token */
3122 hash = HASHSTEP (hash, c);
3126 if (ip->fname == 0 && *ibp == '-') {
3127 /* Newline - inhibits expansion of preceding token.
3128 If expanding a macro arg, we keep the newline -.
3129 In final output, it is deleted.
3130 We recognize Newline - in macro bodies and macro args. */
3131 if (! concatenated) {
3136 if (!output_marks) {
3139 /* If expanding a macro arg, keep the newline -. */
3145 /* If reprocessing a macro expansion, newline is a special marker. */
3146 else if (ip->macro != 0) {
3147 /* Newline White is a "funny space" to separate tokens that are
3148 supposed to be separate but without space between.
3149 Here White means any whitespace character.
3150 Newline - marks a recursive macro use that is not
3151 supposed to be expandable. */
3153 if (is_space[*ibp]) {
3154 /* Newline Space does not prevent expansion of preceding token
3155 so expand the preceding token and then come back. */
3156 if (ident_length > 0)
3159 /* If generating final output, newline space makes a space. */
3160 if (!output_marks) {
3162 /* And Newline Newline makes a newline, so count it. */
3163 if (obp[-1] == '\n')
3166 /* If expanding a macro arg, keep the newline space.
3167 If the arg gets stringified, newline space makes nothing. */
3170 } else abort (); /* Newline followed by something random? */
3174 /* If there is a pending identifier, handle it and come back here. */
3175 if (ident_length > 0)
3180 /* Update the line counts and output a #line if necessary. */
3183 if (ip->lineno != op->lineno) {
3185 output_line_directive (ip, op, 1, same_file);
3186 check_expand (op, limit - ibp);
3191 /* Come here either after (1) a null character that is part of the input
3192 or (2) at the end of the input, because there is a null there. */
3195 /* Our input really contains a null character. */
3199 /* At end of a macro-expansion level, pop it and read next level. */
3200 if (ip->macro != 0) {
3203 /* If traditional, and we have an identifier that ends here,
3204 process it now, so we get the right error for recursion. */
3205 if (traditional && ident_length
3206 && ! is_idchar[*instack[indepth - 1].bufp]) {
3215 /* If we don't have a pending identifier,
3216 return at end of input. */
3217 if (ident_length == 0) {
3225 /* If we do have a pending identifier, just consider this null
3226 a special character and arrange to dispatch on it again.
3227 The second time, IDENT_LENGTH will be zero so we will return. */
3233 /* Handle the case of a character such as /, ', " or null
3234 seen following an identifier. Back over it so that
3235 after the identifier is processed the special char
3236 will be dispatched on again. */
3246 if (ident_length > 0) {
3247 register HASHNODE *hp;
3249 /* We have just seen an identifier end. If it's a macro, expand it.
3251 IDENT_LENGTH is the length of the identifier
3252 and HASH is its hash code.
3254 The identifier has already been copied to the output,
3255 so if it is a macro we must remove it.
3257 If REDO_CHAR is 0, the char that terminated the identifier
3258 has been skipped in the output and the input.
3259 OBP-IDENT_LENGTH-1 points to the identifier.
3260 If the identifier is a macro, we must back over the terminator.
3262 If REDO_CHAR is 1, the terminating char has already been
3263 backed over. OBP-IDENT_LENGTH points to the identifier. */
3265 if (!pcp_outfile || pcp_inside_if) {
3266 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3269 if (hp->length == ident_length) {
3270 int obufp_before_macroname;
3271 int op_lineno_before_macroname;
3272 register int i = ident_length;
3273 register U_CHAR *p = hp->name;
3274 register U_CHAR *q = obp - i;
3280 do { /* All this to avoid a strncmp () */
3285 /* We found a use of a macro name.
3286 see if the context shows it is a macro call. */
3288 /* Back up over terminating character if not already done. */
3294 /* Save this as a displacement from the beginning of the output
3295 buffer. We can not save this as a position in the output
3296 buffer, because it may get realloc'ed by RECACHE. */
3297 obufp_before_macroname = (obp - op->buf) - ident_length;
3298 op_lineno_before_macroname = op->lineno;
3300 if (hp->type == T_PCSTRING) {
3301 pcstring_used (hp); /* Mark the definition of this key
3302 as needed, ensuring that it
3304 break; /* Exit loop, since the key cannot have a
3305 definition any longer. */
3308 /* Record whether the macro is disabled. */
3309 disabled = hp->type == T_DISABLED;
3311 /* This looks like a macro ref, but if the macro was disabled,
3312 just copy its name and put in a marker if requested. */
3316 /* This error check caught useful cases such as
3317 #define foo(x,y) bar (x (y,0), y)
3320 error ("recursive use of macro `%s'", hp->name);
3324 check_expand (op, limit - ibp + 2);
3331 /* If macro wants an arglist, verify that a '(' follows.
3332 first skip all whitespace, copying it to the output
3333 after the macro name. Then, if there is no '(',
3334 decide this is not a macro call and leave things that way. */
3335 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3336 && hp->value.defn->nargs >= 0)
3338 U_CHAR *old_ibp = ibp;
3339 U_CHAR *old_obp = obp;
3340 int old_iln = ip->lineno;
3341 int old_oln = op->lineno;
3344 /* Scan forward over whitespace, copying it to the output. */
3345 if (ibp == limit && ip->macro != 0) {
3350 old_iln = ip->lineno;
3351 old_oln = op->lineno;
3353 /* A comment: copy it unchanged or discard it. */
3354 else if (*ibp == '/' && ibp[1] == '*') {
3355 if (put_out_comments) {
3358 } else if (! traditional) {
3362 while (ibp + 1 != limit
3363 && !(ibp[0] == '*' && ibp[1] == '/')) {
3364 /* We need not worry about newline-marks,
3365 since they are never found in comments. */
3367 /* Newline in a file. Count it. */
3371 if (put_out_comments)
3377 if (put_out_comments) {
3382 else if (is_space[*ibp]) {
3384 if (ibp[-1] == '\n') {
3385 if (ip->macro == 0) {
3386 /* Newline in a file. Count it. */
3389 } else if (!output_marks) {
3390 /* A newline mark, and we don't want marks
3391 in the output. If it is newline-hyphen,
3392 discard it entirely. Otherwise, it is
3393 newline-whitechar, so keep the whitechar. */
3403 /* A newline mark; copy both chars to the output. */
3411 /* It isn't a macro call.
3412 Put back the space that we just skipped. */
3415 ip->lineno = old_iln;
3416 op->lineno = old_oln;
3417 /* Exit the for loop. */
3422 /* This is now known to be a macro call.
3423 Discard the macro name from the output,
3424 along with any following whitespace just copied,
3425 but preserve newlines if not outputting marks since this
3426 is more likely to do the right thing with line numbers. */
3427 obp = op->buf + obufp_before_macroname;
3429 op->lineno = op_lineno_before_macroname;
3431 int newlines = op->lineno - op_lineno_before_macroname;
3432 while (0 < newlines--)
3436 /* Prevent accidental token-pasting with a character
3437 before the macro call. */
3438 if (!traditional && obp != op->buf) {
3440 case '!': case '%': case '&': case '*':
3441 case '+': case '-': case '.': case '/':
3442 case ':': case '<': case '=': case '>':
3444 /* If we are expanding a macro arg, make a newline marker
3445 to separate the tokens. If we are making real output,
3446 a plain space will do. */
3453 /* Expand the macro, reading arguments as needed,
3454 and push the expansion on the input stack. */
3457 macroexpand (hp, op);
3459 /* Reexamine input stack, since macroexpand has pushed
3460 a new level on it. */
3467 } /* End hash-table-search loop */
3469 ident_length = hash = 0; /* Stop collecting identifier */
3472 } /* End if (ident_length > 0) */
3474 } /* End per-char loop */
3476 /* Come here to return -- but first give an error message
3477 if there was an unterminated successful conditional. */
3479 if (if_stack != ip->if_stack)
3483 switch (if_stack->type)
3504 error_with_line (line_for_error (if_stack->lineno),
3505 "unterminated `#%s' conditional", str);
3507 if_stack = ip->if_stack;
3511 * Rescan a string into a temporary buffer and return the result
3512 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3514 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3515 * and insert such markers when appropriate. See `rescan' for details.
3516 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3517 * before substitution; it is 0 for other uses.
3520 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3521 U_CHAR *buf, *limit;
3522 int output_marks, assertions;
3524 register FILE_BUF *ip;
3526 int length = limit - buf;
3528 int odepth = indepth;
3529 int save_assertions_flag = assertions_flag;
3531 assertions_flag = assertions;
3536 /* Set up the input on the input stack. */
3538 buf1 = (U_CHAR *) alloca (length + 1);
3540 register U_CHAR *p1 = buf;
3541 register U_CHAR *p2 = buf1;
3548 /* Set up to receive the output. */
3550 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3551 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3552 obuf.nominal_fname = 0;
3559 obuf.system_header_p = 0;
3561 CHECK_DEPTH ({return obuf;});
3565 ip = &instack[indepth];
3567 ip->nominal_fname = 0;
3569 ip->system_header_p = 0;
3572 ip->length = length;
3573 ip->buf = ip->bufp = buf1;
3574 ip->if_stack = if_stack;
3576 ip->lineno = obuf.lineno = 1;
3578 /* Scan the input, create the output. */
3579 rescan (&obuf, output_marks);
3581 /* Pop input stack to original state. */
3584 if (indepth != odepth)
3587 /* Record the output. */
3588 obuf.length = obuf.bufp - obuf.buf;
3590 assertions_flag = save_assertions_flag;
3595 * Process a # directive. Expects IP->bufp to point after the '#', as in
3596 * `#define foo bar'. Passes to the directive handler
3597 * (do_define, do_include, etc.): the addresses of the 1st and
3598 * last chars of the directive (starting immediately after the #
3599 * keyword), plus op and the keyword table pointer. If the directive
3600 * contains comments it is copied into a temporary buffer sans comments
3601 * and the temporary buffer is passed to the directive handler instead.
3602 * Likewise for backslash-newlines.
3604 * Returns nonzero if this was a known # directive.
3605 * Otherwise, returns zero, without advancing the input pointer.
3609 handle_directive (ip, op)
3612 register U_CHAR *bp, *cp;
3613 register struct directive *kt;
3614 register int ident_length;
3617 /* Nonzero means we must copy the entire directive
3618 to get rid of comments or backslash-newlines. */
3619 int copy_directive = 0;
3621 U_CHAR *ident, *after_ident;
3625 /* Record where the directive started. do_xifdef needs this. */
3626 directive_start = bp - 1;
3628 /* Skip whitespace and \-newline. */
3630 if (is_hor_space[*bp]) {
3631 if (*bp != ' ' && *bp != '\t' && pedantic)
3632 pedwarn ("%s in preprocessing directive", char_name[*bp]);
3634 } else if (*bp == '/' && (bp[1] == '*'
3635 || (cplusplus_comments && bp[1] == '/'))) {
3637 skip_to_end_of_comment (ip, &ip->lineno, 0);
3639 } else if (*bp == '\\' && bp[1] == '\n') {
3640 bp += 2; ip->lineno++;
3644 /* Now find end of directive name.
3645 If we encounter a backslash-newline, exchange it with any following
3646 symbol-constituents so that we end up with a contiguous name. */
3653 if (*cp == '\\' && cp[1] == '\n')
3654 name_newline_fix (cp);
3660 ident_length = cp - bp;
3664 /* A line of just `#' becomes blank. */
3666 if (ident_length == 0 && *after_ident == '\n') {
3667 ip->bufp = after_ident;
3671 if (ident_length == 0 || !is_idstart[*ident]) {
3673 while (is_idchar[*p]) {
3674 if (*p < '0' || *p > '9')
3678 /* Handle # followed by a line number. */
3679 if (p != ident && !is_idchar[*p]) {
3680 static struct directive line_directive_table[] = {
3681 { 4, do_line, "line", T_LINE},
3684 pedwarn ("`#' followed by integer");
3685 after_ident = ident;
3686 kt = line_directive_table;
3690 /* Avoid error for `###' and similar cases unless -pedantic. */
3692 while (*p == '#' || is_hor_space[*p]) p++;
3694 if (pedantic && !lang_asm)
3695 warning ("invalid preprocessing directive");
3701 error ("invalid preprocessing directive name");
3707 * Decode the keyword and call the appropriate expansion
3708 * routine, after moving the input pointer up to the next line.
3710 for (kt = directive_table; kt->length > 0; kt++) {
3711 if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
3712 register U_CHAR *buf;
3713 register U_CHAR *limit;
3716 int *already_output;
3718 /* Nonzero means do not delete comments within the directive.
3719 #define needs this when -traditional. */
3724 limit = ip->buf + ip->length;
3727 keep_comments = traditional && kt->traditional_comments;
3728 /* #import is defined only in Objective C, or when on the NeXT. */
3729 if (kt->type == T_IMPORT
3730 && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
3733 /* Find the end of this directive (first newline not backslashed
3734 and not in a string or comment).
3735 Set COPY_DIRECTIVE if the directive must be copied
3736 (it contains a backslash-newline or a comment). */
3738 buf = bp = after_ident;
3739 while (bp < limit) {
3740 register U_CHAR c = *bp++;
3748 } else if (traditional)
3755 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated);
3756 /* Don't bother calling the directive if we already got an error
3757 message due to unterminated string. Skip everything and pretend
3758 we called the directive. */
3761 /* Traditional preprocessing permits unterminated strings. */
3770 /* <...> is special for #include. */
3772 if (!kt->angle_brackets)
3774 while (bp < limit && *bp != '>' && *bp != '\n') {
3775 if (*bp == '\\' && bp[1] == '\n') {
3785 if (*bp == '\\' && bp[1] == '\n')
3788 || (cplusplus_comments && *bp == '/')) {
3789 U_CHAR *obp = bp - 1;
3791 skip_to_end_of_comment (ip, &ip->lineno, 0);
3793 /* No need to copy the directive because of a comment at the end;
3794 just don't include the comment in the directive. */
3795 if (!put_out_comments) {
3797 for (p = bp; *p == ' ' || *p == '\t'; p++)
3804 /* Don't remove the comments if -traditional. */
3805 if (! keep_comments)
3814 pedwarn ("%s in preprocessing directive", char_name[c]);
3818 --bp; /* Point to the newline */
3826 resume_p = ip->bufp;
3827 /* BP is the end of the directive.
3828 RESUME_P is the next interesting data after the directive.
3829 A comment may come between. */
3831 /* If a directive should be copied through, and -E was given,
3832 pass it through before removing comments. */
3833 if (!no_output && put_out_comments
3834 && (dump_macros != dump_definitions) < kt->pass_thru) {
3837 /* Output directive name. */
3838 check_expand (op, kt->length + 2);
3839 /* Make sure # is at the start of a line */
3840 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3845 bcopy (kt->name, op->bufp, kt->length);
3846 op->bufp += kt->length;
3848 /* Output arguments. */
3850 check_expand (op, len);
3851 bcopy (buf, (char *) op->bufp, len);
3853 /* Take account of any (escaped) newlines just output. */
3855 if (buf[len] == '\n')
3858 already_output = &junk;
3859 } /* Don't we need a newline or #line? */
3861 if (copy_directive) {
3862 register U_CHAR *xp = buf;
3863 /* Need to copy entire directive into temp buffer before dispatching */
3865 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus
3869 /* Copy to the new buffer, deleting comments
3870 and backslash-newlines (and whitespace surrounding the latter). */
3873 register U_CHAR c = *xp++;
3878 abort (); /* A bare newline should never part of the line. */
3881 /* <...> is special for #include. */
3883 if (!kt->angle_brackets)
3885 while (xp < bp && c != '>') {
3887 if (c == '\\' && xp < bp && *xp == '\n')
3898 if (cp != buf && is_hor_space[cp[-1]]) {
3899 while (cp - 1 != buf && is_hor_space[cp[-2]])
3901 SKIP_WHITE_SPACE (xp);
3902 } else if (is_hor_space[*xp]) {
3904 SKIP_WHITE_SPACE (xp);
3906 } else if (traditional && xp < bp) {
3914 register U_CHAR *bp1
3915 = skip_quoted_string (xp - 1, bp, ip->lineno,
3916 NULL_PTR, NULL_PTR, NULL_PTR);
3930 || (cplusplus_comments && *xp == '/')) {
3932 /* If we already copied the directive through,
3933 already_output != 0 prevents outputting comment now. */
3934 skip_to_end_of_comment (ip, already_output, 0);
3936 while (xp != ip->bufp)
3938 /* Delete or replace the slash. */
3939 else if (traditional)
3948 /* Null-terminate the copy. */
3954 ip->bufp = resume_p;
3956 /* Some directives should be written out for cc1 to process,
3957 just as if they were not defined. And sometimes we're copying
3958 definitions through. */
3960 if (!no_output && already_output == 0
3961 && (dump_macros < dump_names) < kt->pass_thru) {
3964 /* Output directive name. */
3965 check_expand (op, kt->length + 1);
3967 bcopy (kt->name, (char *) op->bufp, kt->length);
3968 op->bufp += kt->length;
3970 if ((dump_macros != dump_definitions) < kt->pass_thru) {
3971 /* Output arguments. */
3973 check_expand (op, len);
3974 bcopy (buf, (char *) op->bufp, len);
3976 } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
3979 SKIP_WHITE_SPACE (xp);
3981 while (is_idchar[*xp]) xp++;
3983 check_expand (op, len + 1);
3985 bcopy (yp, op->bufp, len);
3988 } /* Don't we need a newline or #line? */
3990 /* Call the appropriate directive handler. buf now points to
3991 either the appropriate place in the input buffer, or to
3992 the temp buffer if it was necessary to make one. cp
3993 points to the first char after the contents of the (possibly
3994 copied) directive, in either case. */
3995 (*kt->func) (buf, cp, op, kt);
3996 check_expand (op, ip->length - (ip->bufp - ip->buf));
4002 /* It is deliberate that we don't warn about undefined directives.
4003 That is the responsibility of cc1. */
4010 static struct tm *timebuf;
4012 time_t t = time ((time_t *) 0);
4013 timebuf = localtime (&t);
4018 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4019 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4023 * expand things like __FILE__. Place the expansion into the output
4024 * buffer *without* rescanning.
4028 special_symbol (hp, op)
4035 FILE_BUF *ip = NULL;
4038 int paren = 0; /* For special `defined' keyword */
4040 if (pcp_outfile && pcp_inside_if
4041 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
4042 error ("Predefined macro `%s' used inside `#if' during precompilation",
4045 for (i = indepth; i >= 0; i--)
4046 if (instack[i].fname != NULL) {
4051 error ("cccp error: not in any file?!");
4052 return; /* the show must go on */
4060 if (hp->type == T_FILE)
4061 string = ip->nominal_fname;
4063 string = instack[0].nominal_fname;
4067 buf = (char *) alloca (3 + 4 * strlen (string));
4068 quote_string (buf, string);
4076 case T_INCLUDE_LEVEL:
4078 for (i = indepth; i >= 0; i--)
4079 if (instack[i].fname != NULL)
4082 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
4083 sprintf (buf, "%d", true_indepth - 1);
4087 buf = (char *) alloca (3 + strlen (version_string));
4088 sprintf (buf, "\"%s\"", version_string);
4091 #ifndef NO_BUILTIN_SIZE_TYPE
4097 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4098 case T_PTRDIFF_TYPE:
4107 case T_USER_LABEL_PREFIX_TYPE:
4108 buf = USER_LABEL_PREFIX;
4111 case T_REGISTER_PREFIX_TYPE:
4112 buf = REGISTER_PREFIX;
4115 case T_IMMEDIATE_PREFIX_TYPE:
4116 buf = IMMEDIATE_PREFIX;
4120 buf = hp->value.cpval;
4121 if (pcp_inside_if && pcp_outfile)
4122 /* Output a precondition for this macro use */
4123 fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
4127 buf = (char *) alloca (10);
4128 sprintf (buf, "%d", ip->lineno);
4133 buf = (char *) alloca (20);
4134 timebuf = timestamp ();
4135 if (hp->type == T_DATE)
4136 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
4137 timebuf->tm_mday, timebuf->tm_year + 1900);
4139 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
4143 case T_SPEC_DEFINED:
4144 buf = " 0 "; /* Assume symbol is not defined */
4145 ip = &instack[indepth];
4146 SKIP_WHITE_SPACE (ip->bufp);
4147 if (*ip->bufp == '(') {
4149 ip->bufp++; /* Skip over the paren */
4150 SKIP_WHITE_SPACE (ip->bufp);
4153 if (!is_idstart[*ip->bufp])
4155 if (ip->bufp[0] == 'L' && (ip->bufp[1] == '\'' || ip->bufp[1] == '"'))
4157 if ((hp = lookup (ip->bufp, -1, -1))) {
4158 if (pcp_outfile && pcp_inside_if
4159 && (hp->type == T_CONST
4160 || (hp->type == T_MACRO && hp->value.defn->predefined)))
4161 /* Output a precondition for this macro use. */
4162 fprintf (pcp_outfile, "#define %s\n", hp->name);
4166 if (pcp_outfile && pcp_inside_if) {
4167 /* Output a precondition for this macro use */
4168 U_CHAR *cp = ip->bufp;
4169 fprintf (pcp_outfile, "#undef ");
4170 while (is_idchar[*cp]) /* Ick! */
4171 fputc (*cp++, pcp_outfile);
4172 putc ('\n', pcp_outfile);
4174 while (is_idchar[*ip->bufp])
4176 SKIP_WHITE_SPACE (ip->bufp);
4178 if (*ip->bufp != ')')
4186 error ("`defined' without an identifier");