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
);
3556 CHECK_DEPTH ({return obuf
;});
3560 ip
= &instack
[indepth
];
3562 ip
->nominal_fname
= 0;
3564 ip
->system_header_p
= 0;
3567 ip
->length
= length
;
3568 ip
->buf
= ip
->bufp
= buf1
;
3569 ip
->if_stack
= if_stack
;
3571 ip
->lineno
= obuf
.lineno
= 1;
3573 /* Scan the input, create the output. */
3574 rescan (&obuf
, output_marks
);
3576 /* Pop input stack to original state. */
3579 if (indepth
!= odepth
)
3582 /* Record the output. */
3583 obuf
.length
= obuf
.bufp
- obuf
.buf
;
3585 assertions_flag
= save_assertions_flag
;
3590 * Process a # directive. Expects IP->bufp to point after the '#', as in
3591 * `#define foo bar'. Passes to the directive handler
3592 * (do_define, do_include, etc.): the addresses of the 1st and
3593 * last chars of the directive (starting immediately after the #
3594 * keyword), plus op and the keyword table pointer. If the directive
3595 * contains comments it is copied into a temporary buffer sans comments
3596 * and the temporary buffer is passed to the directive handler instead.
3597 * Likewise for backslash-newlines.
3599 * Returns nonzero if this was a known # directive.
3600 * Otherwise, returns zero, without advancing the input pointer.
3604 handle_directive (ip
, op
)
3607 register U_CHAR
*bp
, *cp
;
3608 register struct directive
*kt
;
3609 register int ident_length
;
3612 /* Nonzero means we must copy the entire directive
3613 to get rid of comments or backslash-newlines. */
3614 int copy_directive
= 0;
3616 U_CHAR
*ident
, *after_ident
;
3620 /* Record where the directive started. do_xifdef needs this. */
3621 directive_start
= bp
- 1;
3623 /* Skip whitespace and \-newline. */
3625 if (is_hor_space
[*bp
]) {
3626 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3627 pedwarn ("%s in preprocessing directive", char_name
[*bp
]);
3629 } else if (*bp
== '/' && (bp
[1] == '*'
3630 || (cplusplus_comments
&& bp
[1] == '/'))) {
3632 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3634 } else if (*bp
== '\\' && bp
[1] == '\n') {
3635 bp
+= 2; ip
->lineno
++;
3639 /* Now find end of directive name.
3640 If we encounter a backslash-newline, exchange it with any following
3641 symbol-constituents so that we end up with a contiguous name. */
3648 if (*cp
== '\\' && cp
[1] == '\n')
3649 name_newline_fix (cp
);
3655 ident_length
= cp
- bp
;
3659 /* A line of just `#' becomes blank. */
3661 if (ident_length
== 0 && *after_ident
== '\n') {
3662 ip
->bufp
= after_ident
;
3666 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3668 while (is_idchar
[*p
]) {
3669 if (*p
< '0' || *p
> '9')
3673 /* Handle # followed by a line number. */
3674 if (p
!= ident
&& !is_idchar
[*p
]) {
3675 static struct directive line_directive_table
[] = {
3676 { 4, do_line
, "line", T_LINE
},
3679 pedwarn ("`#' followed by integer");
3680 after_ident
= ident
;
3681 kt
= line_directive_table
;
3685 /* Avoid error for `###' and similar cases unless -pedantic. */
3687 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3689 if (pedantic
&& !lang_asm
)
3690 warning ("invalid preprocessing directive");
3696 error ("invalid preprocessing directive name");
3702 * Decode the keyword and call the appropriate expansion
3703 * routine, after moving the input pointer up to the next line.
3705 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3706 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3707 register U_CHAR
*buf
;
3708 register U_CHAR
*limit
;
3711 int *already_output
;
3713 /* Nonzero means do not delete comments within the directive.
3714 #define needs this when -traditional. */
3719 limit
= ip
->buf
+ ip
->length
;
3722 keep_comments
= traditional
&& kt
->traditional_comments
;
3723 /* #import is defined only in Objective C, or when on the NeXT. */
3724 if (kt
->type
== T_IMPORT
3725 && !(objc
|| lookup ((U_CHAR
*) "__NeXT__", -1, -1)))
3728 /* Find the end of this directive (first newline not backslashed
3729 and not in a string or comment).
3730 Set COPY_DIRECTIVE if the directive must be copied
3731 (it contains a backslash-newline or a comment). */
3733 buf
= bp
= after_ident
;
3734 while (bp
< limit
) {
3735 register U_CHAR c
= *bp
++;
3743 } else if (traditional
)
3750 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3751 /* Don't bother calling the directive if we already got an error
3752 message due to unterminated string. Skip everything and pretend
3753 we called the directive. */
3756 /* Traditional preprocessing permits unterminated strings. */
3765 /* <...> is special for #include. */
3767 if (!kt
->angle_brackets
)
3769 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3770 if (*bp
== '\\' && bp
[1] == '\n') {
3780 if (*bp
== '\\' && bp
[1] == '\n')
3783 || (cplusplus_comments
&& *bp
== '/')) {
3784 U_CHAR
*obp
= bp
- 1;
3786 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3788 /* No need to copy the directive because of a comment at the end;
3789 just don't include the comment in the directive. */
3790 if (!put_out_comments
) {
3792 for (p
= bp
; *p
== ' ' || *p
== '\t'; p
++)
3799 /* Don't remove the comments if -traditional. */
3800 if (! keep_comments
)
3809 pedwarn ("%s in preprocessing directive", char_name
[c
]);
3813 --bp
; /* Point to the newline */
3821 resume_p
= ip
->bufp
;
3822 /* BP is the end of the directive.
3823 RESUME_P is the next interesting data after the directive.
3824 A comment may come between. */
3826 /* If a directive should be copied through, and -E was given,
3827 pass it through before removing comments. */
3828 if (!no_output
&& put_out_comments
3829 && (dump_macros
!= dump_definitions
) < kt
->pass_thru
) {
3832 /* Output directive name. */
3833 check_expand (op
, kt
->length
+ 2);
3834 /* Make sure # is at the start of a line */
3835 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3840 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3841 op
->bufp
+= kt
->length
;
3843 /* Output arguments. */
3845 check_expand (op
, len
);
3846 bcopy (buf
, (char *) op
->bufp
, len
);
3848 /* Take account of any (escaped) newlines just output. */
3850 if (buf
[len
] == '\n')
3853 already_output
= &junk
;
3854 } /* Don't we need a newline or #line? */
3856 if (copy_directive
) {
3857 register U_CHAR
*xp
= buf
;
3858 /* Need to copy entire directive into temp buffer before dispatching */
3860 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for directive plus
3864 /* Copy to the new buffer, deleting comments
3865 and backslash-newlines (and whitespace surrounding the latter). */
3868 register U_CHAR c
= *xp
++;
3873 abort (); /* A bare newline should never part of the line. */
3876 /* <...> is special for #include. */
3878 if (!kt
->angle_brackets
)
3880 while (xp
< bp
&& c
!= '>') {
3882 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
3893 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
3894 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
3896 SKIP_WHITE_SPACE (xp
);
3897 } else if (is_hor_space
[*xp
]) {
3899 SKIP_WHITE_SPACE (xp
);
3901 } else if (traditional
&& xp
< bp
) {
3909 register U_CHAR
*bp1
3910 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
3911 NULL_PTR
, NULL_PTR
, NULL_PTR
);
3925 || (cplusplus_comments
&& *xp
== '/')) {
3927 /* If we already copied the directive through,
3928 already_output != 0 prevents outputting comment now. */
3929 skip_to_end_of_comment (ip
, already_output
, 0);
3931 while (xp
!= ip
->bufp
)
3933 /* Delete or replace the slash. */
3934 else if (traditional
)
3943 /* Null-terminate the copy. */
3949 ip
->bufp
= resume_p
;
3951 /* Some directives should be written out for cc1 to process,
3952 just as if they were not defined. And sometimes we're copying
3953 definitions through. */
3955 if (!no_output
&& already_output
== 0
3956 && (dump_macros
< dump_names
) < kt
->pass_thru
) {
3959 /* Output directive name. */
3960 check_expand (op
, kt
->length
+ 1);
3962 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
3963 op
->bufp
+= kt
->length
;
3965 if ((dump_macros
!= dump_definitions
) < kt
->pass_thru
) {
3966 /* Output arguments. */
3968 check_expand (op
, len
);
3969 bcopy (buf
, (char *) op
->bufp
, len
);
3971 } else if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
3974 SKIP_WHITE_SPACE (xp
);
3976 while (is_idchar
[*xp
]) xp
++;
3978 check_expand (op
, len
+ 1);
3980 bcopy (yp
, op
->bufp
, len
);
3983 } /* Don't we need a newline or #line? */
3985 /* Call the appropriate directive handler. buf now points to
3986 either the appropriate place in the input buffer, or to
3987 the temp buffer if it was necessary to make one. cp
3988 points to the first char after the contents of the (possibly
3989 copied) directive, in either case. */
3990 (*kt
->func
) (buf
, cp
, op
, kt
);
3991 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3997 /* It is deliberate that we don't warn about undefined directives.
3998 That is the responsibility of cc1. */
4005 static struct tm
*timebuf
;
4007 time_t t
= time ((time_t *) 0);
4008 timebuf
= localtime (&t
);
4013 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4014 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4018 * expand things like __FILE__. Place the expansion into the output
4019 * buffer *without* rescanning.
4023 special_symbol (hp
, op
)
4030 FILE_BUF
*ip
= NULL
;
4033 int paren
= 0; /* For special `defined' keyword */
4035 if (pcp_outfile
&& pcp_inside_if
4036 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4037 error ("Predefined macro `%s' used inside `#if' during precompilation",
4040 for (i
= indepth
; i
>= 0; i
--)
4041 if (instack
[i
].fname
!= NULL
) {
4046 error ("cccp error: not in any file?!");
4047 return; /* the show must go on */
4055 if (hp
->type
== T_FILE
)
4056 string
= ip
->nominal_fname
;
4058 string
= instack
[0].nominal_fname
;
4062 buf
= (char *) alloca (3 + 4 * strlen (string
));
4063 quote_string (buf
, string
);
4071 case T_INCLUDE_LEVEL
:
4073 for (i
= indepth
; i
>= 0; i
--)
4074 if (instack
[i
].fname
!= NULL
)
4077 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
4078 sprintf (buf
, "%d", true_indepth
- 1);
4082 buf
= (char *) alloca (3 + strlen (version_string
));
4083 sprintf (buf
, "\"%s\"", version_string
);
4086 #ifndef NO_BUILTIN_SIZE_TYPE
4092 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4093 case T_PTRDIFF_TYPE
:
4102 case T_USER_LABEL_PREFIX_TYPE
:
4103 buf
= USER_LABEL_PREFIX
;
4106 case T_REGISTER_PREFIX_TYPE
:
4107 buf
= REGISTER_PREFIX
;
4110 case T_IMMEDIATE_PREFIX_TYPE
:
4111 buf
= IMMEDIATE_PREFIX
;
4115 buf
= hp
->value
.cpval
;
4116 if (pcp_inside_if
&& pcp_outfile
)
4117 /* Output a precondition for this macro use */
4118 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4122 buf
= (char *) alloca (10);
4123 sprintf (buf
, "%d", ip
->lineno
);
4128 buf
= (char *) alloca (20);
4129 timebuf
= timestamp ();
4130 if (hp
->type
== T_DATE
)
4131 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4132 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4134 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
4138 case T_SPEC_DEFINED
:
4139 buf
= " 0 "; /* Assume symbol is not defined */
4140 ip
= &instack
[indepth
];
4141 SKIP_WHITE_SPACE (ip
->bufp
);
4142 if (*ip
->bufp
== '(') {
4144 ip
->bufp
++; /* Skip over the paren */
4145 SKIP_WHITE_SPACE (ip
->bufp
);
4148 if (!is_idstart
[*ip
->bufp
])
4150 if (ip
->bufp
[0] == 'L' && (ip
->bufp
[1] == '\'' || ip
->bufp
[1] == '"'))
4152 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4153 if (pcp_outfile
&& pcp_inside_if
4154 && (hp
->type
== T_CONST
4155 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4156 /* Output a precondition for this macro use. */
4157 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4161 if (pcp_outfile
&& pcp_inside_if
) {
4162 /* Output a precondition for this macro use */
4163 U_CHAR
*cp
= ip
->bufp
;
4164 fprintf (pcp_outfile
, "#undef ");
4165 while (is_idchar
[*cp
]) /* Ick! */
4166 fputc (*cp
++, pcp_outfile
);
4167 putc ('\n', pcp_outfile
);
4169 while (is_idchar
[*ip
->bufp
])
4171 SKIP_WHITE_SPACE (ip
->bufp
);
4173 if (*ip
->bufp
!= ')')
4181 error ("`defined' without an identifier");
4185 error ("cccp error: invalid special hash type"); /* time for gdb */
4189 check_expand (op
, len
);
4190 bcopy (buf
, (char *) op
->bufp
, len
);
4197 /* Routines to handle #directives */
4199 /* Handle #include and #import.
4200 This function expects to see "fname" or <fname> on the input. */
4203 do_include (buf
, limit
, op
, keyword
)
4204 U_CHAR
*buf
, *limit
;
4206 struct directive
*keyword
;
4208 U_CHAR
*importing
= keyword
->type
== T_IMPORT
? (U_CHAR
*) "" : (U_CHAR
*) 0;
4209 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4210 static int import_warning
= 0;
4211 char *fname
; /* Dynamically allocated fname buffer */
4214 char *fbeg
, *fend
; /* Beginning and end of fname */
4217 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4218 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4219 struct file_name_list
*searchptr
= 0;
4222 int f
= -3; /* file number */
4223 struct include_file
*inc
= 0;
4225 int retried
= 0; /* Have already tried macro
4226 expanding the include line*/
4227 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4229 int vaxc_include
= 0; /* 1 for token without punctuation */
4236 if (pedantic
&& !instack
[indepth
].system_header_p
)
4239 pedwarn ("ANSI C does not allow `#import'");
4241 pedwarn ("ANSI C does not allow `#include_next'");
4244 if (importing
&& warn_import
&& !inhibit_warnings
4245 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4247 warning ("using `#import' is not recommended");
4248 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
4249 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
4250 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
4251 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
4252 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
4253 fprintf (stderr
, " ... <real contents of file> ...\n");
4254 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
4255 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
4256 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
4257 fprintf (stderr
, "when it is equipped with such a conditional.\n");
4263 SKIP_WHITE_SPACE (fin
);
4264 /* Discard trailing whitespace so we can easily see
4265 if we have parsed all the significant chars we were given. */
4266 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4267 fbeg
= fend
= (char *) alloca (limit
- fin
);
4273 /* Copy the operand text, concatenating the strings. */
4275 while (fin
!= limit
) {
4276 while (fin
!= limit
&& *fin
!= '\"')
4281 /* If not at the end, there had better be another string. */
4282 /* Skip just horiz space, and don't go past limit. */
4283 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4284 if (fin
!= limit
&& *fin
== '\"')
4291 /* We have "filename". Figure out directory this source
4292 file is coming from and put it on the front of the list. */
4294 /* If -I- was specified, don't search current dir, only spec'd ones. */
4295 if (ignore_srcdir
) break;
4297 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4302 if ((nam
= fp
->nominal_fname
) != NULL
) {
4303 /* Found a named file. Figure out dir of the file,
4304 and put it in front of the search list. */
4305 dsp
= ((struct file_name_list
*)
4306 alloca (sizeof (struct file_name_list
) + strlen (nam
)));
4307 strcpy (dsp
->fname
, nam
);
4308 simplify_filename (dsp
->fname
);
4309 nam
= base_name (dsp
->fname
);
4311 /* But for efficiency's sake, do not insert the dir
4312 if it matches the search list's first dir. */
4313 dsp
->next
= search_start
;
4314 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4316 n
= nam
- dsp
->fname
;
4317 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4318 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4320 dsp
[0].got_name_map
= 0;
4328 while (fin
!= limit
&& *fin
!= '>')
4330 if (*fin
== '>' && fin
+ 1 == limit
) {
4332 /* If -I-, start with the first -I dir after the -I-. */
4333 search_start
= first_bracket_include
;
4341 * Support '#include xyz' like VAX-C to allow for easy use of all the
4342 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4343 * code from case '<' is repeated here) and generates a warning.
4344 * (Note: macro expansion of `xyz' takes precedence.)
4346 if (retried
&& isalpha(*(U_CHAR
*) (--fbeg
))) {
4347 while (fin
!= limit
&& (!isspace(*fin
)))
4349 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4353 /* If -I-, start with the first -I dir after the -I-. */
4354 search_start
= first_bracket_include
;
4362 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4365 /* Expand buffer and then remove any newline markers.
4366 We can't just tell expand_to_temp_buffer to omit the markers,
4367 since it would put extra spaces in include file names. */
4370 trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4372 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4374 while (src
!= trybuf
.bufp
) {
4375 switch ((*limit
++ = *src
++)) {
4384 U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4385 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4399 /* For #include_next, skip in the search path
4400 past the dir in which the containing file was found. */
4403 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4404 if (fp
->fname
!= NULL
) {
4405 /* fp->dir is null if the containing file was specified
4406 with an absolute file name. In that case, don't skip anything. */
4408 search_start
= fp
->dir
->next
;
4414 flen
= simplify_filename (fbeg
);
4418 error ("empty file name in `#%s'", keyword
->name
);
4422 /* Allocate this permanently, because it gets stored in the definitions
4424 fname
= xmalloc (max_include_len
+ flen
+ 1);
4425 /* + 1 above for terminating null. */
4427 system_include_depth
+= angle_brackets
;
4429 /* If specified file name is absolute, just open it. */
4431 if (absolute_filename (fbeg
)) {
4432 strcpy (fname
, fbeg
);
4433 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4437 struct bypass_dir
*next
;
4439 struct file_name_list
*searchptr
;
4440 } **bypass_slot
= 0;
4442 /* Search directory path, trying to open the file.
4443 Copy each filename tried into FNAME. */
4445 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4447 if (searchptr
== first_bracket_include
) {
4448 /* Go to bypass directory if we know we've seen this file before. */
4449 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4450 struct bypass_dir
*p
;
4451 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4453 for (p
= *bypass_slot
; p
; p
= p
->next
)
4454 if (!strcmp (fbeg
, p
->fname
)) {
4455 searchptr
= p
->searchptr
;
4461 strcpy (fname
, searchptr
->fname
);
4462 strcat (fname
, fbeg
);
4464 /* Change this 1/2 Unix 1/2 VMS file specification into a
4465 full VMS file specification */
4466 if (searchptr
->fname
[0]) {
4467 /* Fix up the filename */
4468 hack_vms_include_specification (fname
, vaxc_include
);
4470 /* This is a normal VMS filespec, so use it unchanged. */
4471 strcpy (fname
, fbeg
);
4472 /* if it's '#include filename', add the missing .h */
4473 if (vaxc_include
&& index(fname
,'.')==NULL
) {
4474 strcat (fname
, ".h");
4478 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4480 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4481 /* This is the first time we found this include file,
4482 and we found it after first_bracket_include.
4483 Record its location so that we can bypass to here next time. */
4484 struct bypass_dir
*p
4485 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4486 p
->next
= *bypass_slot
;
4487 p
->fname
= fname
+ strlen (searchptr
->fname
);
4488 p
->searchptr
= searchptr
;
4494 /* Our VMS hacks can produce invalid filespecs, so don't worry
4495 about errors other than EACCES. */
4496 if (errno
== EACCES
)
4499 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
4509 /* The file was already included. */
4511 /* If generating dependencies and -MG was specified, we assume missing
4512 files are leaf files, living in the same directory as the source file
4513 or other similar place; these missing files may be generated from
4514 other files and may not exist yet (eg: y.tab.h). */
4515 } else if (print_deps_missing_files
4516 && (system_include_depth
!= 0) < print_deps
)
4518 /* If it was requested as a system header file,
4519 then assume it belongs in the first place to look for such. */
4523 char *p
= (char *) alloca (strlen (search_start
->fname
)
4524 + strlen (fbeg
) + 1);
4525 strcpy (p
, search_start
->fname
);
4527 deps_output (p
, ' ');
4532 /* Otherwise, omit the directory, as if the file existed
4533 in the directory with the source. */
4534 deps_output (fbeg
, ' ');
4537 /* If -M was specified, and this header file won't be added to the
4538 dependency list, then don't count this as an error, because we can
4539 still produce correct output. Otherwise, we can't produce correct
4540 output, because there may be dependencies we need inside the missing
4541 file, and we don't know what directory this missing file exists in. */
4542 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4543 warning ("No include path in which to find %s", fbeg
);
4545 error_from_errno (fbeg
);
4547 error ("No include path in which to find %s", fbeg
);
4551 /* Actually process the file. */
4553 pcftry
= (char *) alloca (strlen (fname
) + 30);
4560 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4562 pcf
= open (pcftry
, O_RDONLY
, 0666);
4567 if (fstat (pcf
, &s
) != 0)
4568 pfatal_with_name (pcftry
);
4569 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4570 || inc
->st
.st_dev
!= s
.st_dev
)
4572 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4573 /* Don't need it any more. */
4578 /* Don't need it at all. */
4583 } while (pcf
!= -1 && !pcfbuf
);
4586 /* Actually process the file */
4588 pcfname
= xmalloc (strlen (pcftry
) + 1);
4589 strcpy (pcfname
, pcftry
);
4590 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) pcfbuflimit
,
4591 (U_CHAR
*) fname
, op
);
4594 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4597 system_include_depth
-= angle_brackets
;
4602 /* Return nonzero if the given FILENAME is an absolute pathname which
4603 designates a file within one of the known "system" include file
4604 directories. We assume here that if the given FILENAME looks like
4605 it is the name of a file which resides either directly in a "system"
4606 include file directory, or within any subdirectory thereof, then the
4607 given file must be a "system" include file. This function tells us
4608 if we should suppress pedantic errors/warnings for the given FILENAME.
4610 The value is 2 if the file is a C-language system header file
4611 for which C++ should (on most systems) assume `extern "C"'. */
4614 is_system_include (filename
)
4615 register char *filename
;
4617 struct file_name_list
*searchptr
;
4619 for (searchptr
= first_system_include
; searchptr
;
4620 searchptr
= searchptr
->next
)
4621 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4622 return searchptr
->c_system_include_path
+ 1;
4626 /* Yield the non-directory suffix of a file name. */
4634 #if defined (__MSDOS__) || defined (_WIN32)
4635 if (isalpha (s
[0]) && s
[1] == ':') s
+= 2;
4638 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4639 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4640 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4644 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4645 #ifdef DIR_SEPARATOR
4646 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4651 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4654 absolute_filename (filename
)
4657 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN32__))
4658 if (isalpha (filename
[0]) && filename
[1] == ':') filename
+= 2;
4660 #if defined (__CYGWIN32__)
4661 /* At present, any path that begins with a drive spec is absolute. */
4662 if (isalpha (filename
[0]) && filename
[1] == ':') return 1;
4664 if (filename
[0] == '/') return 1;
4665 #ifdef DIR_SEPARATOR
4666 if (filename
[0] == DIR_SEPARATOR
) return 1;
4671 /* Remove unnecessary characters from FILENAME in place,
4672 to avoid unnecessary filename aliasing.
4673 Return the length of the resulting string.
4675 Do only the simplifications allowed by Posix.
4676 It is OK to miss simplifications on non-Posix hosts,
4677 since this merely leads to suboptimial results. */
4680 simplify_filename (filename
)
4683 register char *from
= filename
;
4684 register char *to
= filename
;
4687 /* Remove redundant initial /s. */
4690 if (*++from
== '/') {
4691 if (*++from
== '/') {
4692 /* 3 or more initial /s are equivalent to 1 /. */
4693 while (*++from
== '/')
4696 /* On some hosts // differs from /; Posix allows this. */
4697 static int slashslash_vs_slash
;
4698 if (slashslash_vs_slash
== 0) {
4700 slashslash_vs_slash
= ((stat ("/", &s1
) == 0 && stat ("//", &s2
) == 0
4701 && INO_T_EQ (s1
.st_ino
, s2
.st_ino
)
4702 && s1
.st_dev
== s2
.st_dev
)
4705 if (slashslash_vs_slash
< 0)
4713 if (from
[0] == '.' && from
[1] == '/')
4716 /* Copy this component and trailing /, if any. */
4717 while ((*to
++ = *from
++) != '/') {
4719 /* Trim . component at end of nonempty name. */
4720 to
-= filename
<= to
- 3 && to
[-3] == '/' && to
[-2] == '.';
4722 /* Trim unnecessary trailing /s. */
4723 while (to0
< --to
&& to
[-1] == '/')
4727 return to
- filename
;
4732 /* Skip /s after a /. */
4733 while (*from
== '/')
4738 /* The file_name_map structure holds a mapping of file names for a
4739 particular directory. This mapping is read from the file named
4740 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4741 map filenames on a file system with severe filename restrictions,
4742 such as DOS. The format of the file name map file is just a series
4743 of lines with two tokens on each line. The first token is the name
4744 to map, and the second token is the actual name to use. */
4746 struct file_name_map
4748 struct file_name_map
*map_next
;
4753 #define FILE_NAME_MAP_FILE "header.gcc"
4755 /* Read a space delimited string of unlimited length from a stdio
4759 read_filename_string (ch
, f
)
4767 set
= alloc
= xmalloc (len
+ 1);
4771 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
4773 if (set
- alloc
== len
)
4776 alloc
= xrealloc (alloc
, len
+ 1);
4777 set
= alloc
+ len
/ 2;
4787 /* Read the file name map file for DIRNAME.
4788 If DIRNAME is empty, read the map file for the working directory;
4789 otherwise DIRNAME must end in '/'. */
4791 static struct file_name_map
*
4792 read_name_map (dirname
)
4795 /* This structure holds a linked list of file name maps, one per
4797 struct file_name_map_list
4799 struct file_name_map_list
*map_list_next
;
4800 char *map_list_name
;
4801 struct file_name_map
*map_list_map
;
4803 static struct file_name_map_list
*map_list
;
4804 register struct file_name_map_list
*map_list_ptr
;
4809 for (map_list_ptr
= map_list
; map_list_ptr
;
4810 map_list_ptr
= map_list_ptr
->map_list_next
)
4811 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
4812 return map_list_ptr
->map_list_map
;
4814 map_list_ptr
= ((struct file_name_map_list
*)
4815 xmalloc (sizeof (struct file_name_map_list
)));
4816 map_list_ptr
->map_list_name
= savestring (dirname
);
4817 map_list_ptr
->map_list_map
= NULL
;
4819 dirlen
= strlen (dirname
);
4820 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
4821 strcpy (name
, dirname
);
4822 strcat (name
, FILE_NAME_MAP_FILE
);
4823 f
= fopen (name
, "r");
4825 map_list_ptr
->map_list_map
= NULL
;
4830 while ((ch
= getc (f
)) != EOF
)
4833 struct file_name_map
*ptr
;
4838 from
= read_filename_string (ch
, f
);
4839 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
4841 to
= read_filename_string (ch
, f
);
4843 simplify_filename (from
);
4844 tolen
= simplify_filename (to
);
4846 ptr
= ((struct file_name_map
*)
4847 xmalloc (sizeof (struct file_name_map
)));
4848 ptr
->map_from
= from
;
4850 /* Make the real filename absolute. */
4851 if (absolute_filename (to
))
4855 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
4856 strcpy (ptr
->map_to
, dirname
);
4857 strcat (ptr
->map_to
, to
);
4861 ptr
->map_next
= map_list_ptr
->map_list_map
;
4862 map_list_ptr
->map_list_map
= ptr
;
4864 while ((ch
= getc (f
)) != '\n')
4871 map_list_ptr
->map_list_next
= map_list
;
4872 map_list
= map_list_ptr
;
4874 return map_list_ptr
->map_list_map
;
4877 /* Try to open include file FILENAME. SEARCHPTR is the directory
4878 being tried from the include file search path.
4879 IMPORTING is "" if we are importing, null otherwise.
4880 Return -2 if found, either a matching name or a matching inode.
4881 Otherwise, open the file and return a file descriptor if successful
4882 or -1 if unsuccessful.
4883 Unless unsuccessful, put a descriptor of the included file into *PINC.
4884 This function maps filenames on file systems based on information read by
4888 open_include_file (filename
, searchptr
, importing
, pinc
)
4890 struct file_name_list
*searchptr
;
4892 struct include_file
**pinc
;
4894 char *fname
= remap_include_file (filename
, searchptr
);
4897 /* Look up FNAME in include_hashtab. */
4898 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
4901 struct include_file
*inc
, *head
= *phead
;
4902 for (inc
= head
; inc
; inc
= inc
->next
)
4903 if (!strcmp (fname
, inc
->fname
))
4907 || ! inc
->control_macro
4908 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
4910 fd
= open (fname
, O_RDONLY
, 0);
4916 /* FNAME was not in include_hashtab; insert a new entry. */
4917 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
4920 inc
->control_macro
= 0;
4921 inc
->deps_output
= 0;
4922 if (fstat (fd
, &inc
->st
) != 0)
4923 pfatal_with_name (fname
);
4926 /* Look for another file with the same inode and device. */
4927 if (lookup_ino_include (inc
)
4928 && inc
->control_macro
4929 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
4935 /* For -M, add this file to the dependencies. */
4936 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
4937 inc
->deps_output
= 1;
4938 deps_output (fname
, ' ');
4941 /* Handle -H option. */
4942 if (print_include_names
)
4943 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
4947 inc
->control_macro
= importing
;
4953 /* Return the remapped name of the the include file FILENAME.
4954 SEARCHPTR is the directory being tried from the include file path. */
4957 remap_include_file (filename
, searchptr
)
4959 struct file_name_list
*searchptr
;
4961 register struct file_name_map
*map
;
4962 register char *from
;
4966 if (! searchptr
->got_name_map
)
4968 searchptr
->name_map
= read_name_map (searchptr
->fname
);
4969 searchptr
->got_name_map
= 1;
4972 /* Check the mapping for the directory we are using. */
4973 from
= filename
+ strlen (searchptr
->fname
);
4974 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
4975 if (! strcmp (map
->map_from
, from
))
4979 from
= base_name (filename
);
4981 if (from
!= filename
|| !searchptr
)
4983 /* Try to find a mapping file for the particular directory we are
4984 looking in. Thus #include <sys/types.h> will look up sys/types.h
4985 in /usr/include/header.gcc and look up types.h in
4986 /usr/include/sys/header.gcc. */
4988 char *dir
= (char *) alloca (from
- filename
+ 1);
4989 bcopy (filename
, dir
, from
- filename
);
4990 dir
[from
- filename
] = '\0';
4992 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
4993 if (! strcmp (map
->map_from
, from
))
5000 /* Insert INC into the include file table, hashed by device and inode number.
5001 If a file with different name but same dev+ino was already in the table,
5002 return 1 and set INC's control macro to the already-known macro. */
5005 lookup_ino_include (inc
)
5006 struct include_file
*inc
;
5008 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
5009 % INCLUDE_HASHSIZE
);
5010 struct include_file
*i
= include_ino_hashtab
[hash
];
5012 include_ino_hashtab
[hash
] = inc
;
5014 for (; i
; i
= i
->next_ino
)
5015 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
5016 && inc
->st
.st_dev
== i
->st
.st_dev
) {
5017 inc
->control_macro
= i
->control_macro
;
5024 /* Process file descriptor F, which corresponds to include file INC,
5026 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5027 "system" include directories (as decided by the `is_system_include'
5029 DIRPTR is the link in the dir path through which this file was found,
5030 or 0 if the file name was absolute. */
5033 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5035 struct include_file
*inc
;
5037 int system_header_p
;
5038 struct file_name_list
*dirptr
;
5040 char *fname
= inc
->fname
;
5042 FILE_BUF
*fp
; /* For input stack frame */
5043 int missing_newline
= 0;
5045 CHECK_DEPTH (return;);
5047 fp
= &instack
[indepth
+ 1];
5048 bzero ((char *) fp
, sizeof (FILE_BUF
));
5049 fp
->nominal_fname
= fp
->fname
= fname
;
5053 fp
->if_stack
= if_stack
;
5054 fp
->system_header_p
= system_header_p
;
5057 if (S_ISREG (inc
->st
.st_mode
)) {
5058 fp
->buf
= (U_CHAR
*) xmalloc (inc
->st
.st_size
+ 2);
5061 /* Read the file contents, knowing that inc->st.st_size is an upper bound
5062 on the number of bytes we can read. */
5063 fp
->length
= safe_read (f
, (char *) fp
->buf
, inc
->st
.st_size
);
5064 if (fp
->length
< 0) goto nope
;
5066 else if (S_ISDIR (inc
->st
.st_mode
)) {
5067 error ("directory `%s' specified in #include", fname
);
5071 /* Cannot count its file size before reading.
5072 First read the entire file into heap and
5073 copy them into buffer on stack. */
5078 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5081 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5083 goto nope
; /* error! */
5085 if (st_size
!= bsize
)
5086 break; /* End of file */
5088 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5091 fp
->length
= st_size
;
5094 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5095 /* Backslash-newline at end is not good enough. */
5096 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5097 fp
->buf
[fp
->length
++] = '\n';
5098 missing_newline
= 1;
5100 fp
->buf
[fp
->length
] = '\0';
5102 /* Close descriptor now, so nesting does not use lots of descriptors. */
5105 /* Must do this before calling trigraph_pcp, so that the correct file name
5106 will be printed in warning messages. */
5109 input_file_stack_tick
++;
5114 output_line_directive (fp
, op
, 0, enter_file
);
5117 if (missing_newline
)
5120 if (pedantic
&& missing_newline
)
5121 pedwarn ("file does not end in newline");
5124 input_file_stack_tick
++;
5125 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5131 perror_with_name (fname
);
5136 /* Record that inclusion of the include file INC
5137 should be controlled by the macro named MACRO_NAME.
5138 This means that trying to include the file again
5139 will do something if that macro is defined. */
5142 record_control_macro (inc
, macro_name
)
5143 struct include_file
*inc
;
5146 if (!inc
->control_macro
|| inc
->control_macro
[0])
5147 inc
->control_macro
= macro_name
;
5150 /* Load the specified precompiled header into core, and verify its
5151 preconditions. PCF indicates the file descriptor to read, which must
5152 be a regular file. *ST is its file status.
5153 FNAME indicates the file name of the original header.
5154 *LIMIT will be set to an address one past the end of the file.
5155 If the preconditions of the file are not satisfied, the buffer is
5156 freed and we return 0. If the preconditions are satisfied, return
5157 the address of the buffer following the preconditions. The buffer, in
5158 this case, should never be freed because various pieces of it will
5159 be referred to until all precompiled strings are output at the end of
5163 check_precompiled (pcf
, st
, fname
, limit
)
5176 if (S_ISREG (st
->st_mode
))
5178 buf
= xmalloc (st
->st_size
+ 2);
5179 length
= safe_read (pcf
, buf
, st
->st_size
);
5186 if (length
> 0 && buf
[length
-1] != '\n')
5187 buf
[length
++] = '\n';
5190 *limit
= buf
+ length
;
5192 /* File is in core. Check the preconditions. */
5193 if (!check_preconditions (buf
))
5195 for (cp
= buf
; *cp
; cp
++)
5198 fprintf (stderr
, "Using preinclude %s\n", fname
);
5204 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5210 /* PREC (null terminated) points to the preconditions of a
5211 precompiled header. These are a series of #define and #undef
5212 lines which must match the current contents of the hash
5216 check_preconditions (prec
)
5223 lineend
= index (prec
, '\n');
5225 if (*prec
++ != '#') {
5226 error ("Bad format encountered while reading precompiled file");
5229 if (!strncmp (prec
, "define", 6)) {
5233 mdef
= create_definition ((U_CHAR
*) prec
, (U_CHAR
*) lineend
, NULL_PTR
);
5238 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5239 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5240 || (hp
->type
== T_MACRO
5241 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5242 && (mdef
.defn
->length
!= 2
5243 || mdef
.defn
->expansion
[0] != '\n'
5244 || mdef
.defn
->expansion
[1] != ' ')))
5246 } else if (!strncmp (prec
, "undef", 5)) {
5251 while (is_hor_space
[(U_CHAR
) *prec
])
5254 while (is_idchar
[(U_CHAR
) *prec
])
5258 if (lookup ((U_CHAR
*) name
, len
, -1))
5261 error ("Bad format encountered while reading precompiled file");
5266 /* They all passed successfully */
5270 /* Process the main body of a precompiled file. BUF points to the
5271 string section of the file, following the preconditions. LIMIT is one
5272 character past the end. NAME is the name of the file being read
5273 in. OP is the main output buffer. */
5276 pcfinclude (buf
, limit
, name
, op
)
5277 U_CHAR
*buf
, *limit
, *name
;
5284 /* First in the file comes 4 bytes indicating the number of strings, */
5285 /* in network byte order. (MSB first). */
5287 nstrings
= (nstrings
<< 8) | *cp
++;
5288 nstrings
= (nstrings
<< 8) | *cp
++;
5289 nstrings
= (nstrings
<< 8) | *cp
++;
5291 /* Looping over each string... */
5292 while (nstrings
--) {
5293 U_CHAR
*string_start
;
5294 U_CHAR
*endofthiskey
;
5298 /* Each string starts with a STRINGDEF structure (str), followed */
5299 /* by the text of the string (string_start) */
5301 /* First skip to a longword boundary */
5302 /* ??? Why a 4-byte boundary? On all machines? */
5303 /* NOTE: This works correctly even if HOST_WIDE_INT
5304 is narrower than a pointer.
5305 Do not try risky measures here to get another type to use!
5306 Do not include stddef.h--it will fail! */
5307 if ((HOST_WIDE_INT
) cp
& 3)
5308 cp
+= 4 - ((HOST_WIDE_INT
) cp
& 3);
5310 /* Now get the string. */
5311 str
= (STRINGDEF
*) (GENERIC_PTR
) cp
;
5312 string_start
= cp
+= sizeof (STRINGDEF
);
5314 for (; *cp
; cp
++) /* skip the string */
5317 /* We need to macro expand the string here to ensure that the
5318 proper definition environment is in place. If it were only
5319 expanded when we find out it is needed, macros necessary for
5320 its proper expansion might have had their definitions changed. */
5321 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5322 /* Lineno is already set in the precompiled file */
5323 str
->contents
= tmpbuf
.buf
;
5324 str
->len
= tmpbuf
.length
;
5326 str
->filename
= name
;
5327 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5330 *stringlist_tailp
= str
;
5331 stringlist_tailp
= &str
->chain
;
5333 /* Next comes a fourbyte number indicating the number of keys
5336 nkeys
= (nkeys
<< 8) | *cp
++;
5337 nkeys
= (nkeys
<< 8) | *cp
++;
5338 nkeys
= (nkeys
<< 8) | *cp
++;
5340 /* If this number is -1, then the string is mandatory. */
5344 /* Otherwise, for each key, */
5345 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5346 KEYDEF
*kp
= (KEYDEF
*) (GENERIC_PTR
) cp
;
5349 /* It starts with a KEYDEF structure */
5350 cp
+= sizeof (KEYDEF
);
5352 /* Find the end of the key. At the end of this for loop we
5353 advance CP to the start of the next key using this variable. */
5354 endofthiskey
= cp
+ strlen ((char *) cp
);
5357 /* Expand the key, and enter it into the hash table. */
5358 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5359 tmpbuf
.bufp
= tmpbuf
.buf
;
5361 while (is_hor_space
[*tmpbuf
.bufp
])
5363 if (!is_idstart
[*tmpbuf
.bufp
]
5364 || tmpbuf
.bufp
== tmpbuf
.buf
+ tmpbuf
.length
) {
5369 hp
= lookup (tmpbuf
.bufp
, -1, -1);
5372 install (tmpbuf
.bufp
, -1, T_PCSTRING
, (char *) kp
, -1);
5374 else if (hp
->type
== T_PCSTRING
) {
5375 kp
->chain
= hp
->value
.keydef
;
5376 hp
->value
.keydef
= kp
;
5382 /* This output_line_directive serves to switch us back to the current
5383 input file in case some of these strings get output (which will
5384 result in line directives for the header file being output). */
5385 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5388 /* Called from rescan when it hits a key for strings. Mark them all
5389 used and clean up. */
5397 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5398 kp
->str
->writeflag
= 1;
5402 /* Write the output, interspersing precompiled strings in their
5403 appropriate places. */
5408 STRINGDEF
*next_string
;
5409 U_CHAR
*cur_buf_loc
;
5410 int line_directive_len
= 80;
5411 char *line_directive
= xmalloc (line_directive_len
);
5414 /* In each run through the loop, either cur_buf_loc ==
5415 next_string_loc, in which case we print a series of strings, or
5416 it is less than next_string_loc, in which case we write some of
5418 cur_buf_loc
= outbuf
.buf
;
5419 next_string
= stringlist
;
5421 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5423 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5424 if (next_string
->writeflag
) {
5425 len
= 4 * strlen ((char *) next_string
->filename
) + 32;
5426 while (len
> line_directive_len
)
5427 line_directive
= xrealloc (line_directive
,
5428 line_directive_len
*= 2);
5429 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5430 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5431 (char *) next_string
->filename
),
5433 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5434 safe_write (fileno (stdout
),
5435 (char *) next_string
->contents
, next_string
->len
);
5437 next_string
= next_string
->chain
;
5441 ? (next_string
->output_mark
5442 - (cur_buf_loc
- outbuf
.buf
))
5443 : outbuf
.bufp
- cur_buf_loc
);
5445 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5449 free (line_directive
);
5452 /* Pass a directive through to the output file.
5453 BUF points to the contents of the directive, as a contiguous string.
5454 LIMIT points to the first character past the end of the directive.
5455 KEYWORD is the keyword-table entry for the directive. */
5458 pass_thru_directive (buf
, limit
, op
, keyword
)
5459 U_CHAR
*buf
, *limit
;
5461 struct directive
*keyword
;
5463 register unsigned keyword_length
= keyword
->length
;
5465 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5467 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5468 op
->bufp
+= keyword_length
;
5469 if (limit
!= buf
&& buf
[0] != ' ')
5471 bcopy ((char *) buf
, (char *) op
->bufp
, limit
- buf
);
5472 op
->bufp
+= (limit
- buf
);
5475 /* Count the line we have just made in the output,
5476 to get in sync properly. */
5481 /* The arglist structure is built by do_define to tell
5482 collect_definition where the argument names begin. That
5483 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5484 would contain pointers to the strings x, y, and z.
5485 Collect_definition would then build a DEFINITION node,
5486 with reflist nodes pointing to the places x, y, and z had
5487 appeared. So the arglist is just convenience data passed
5488 between these two routines. It is not kept around after
5489 the current #define has been processed and entered into the
5493 struct arglist
*next
;
5500 /* Create a DEFINITION node from a #define directive. Arguments are
5501 as for do_define. */
5504 create_definition (buf
, limit
, op
)
5505 U_CHAR
*buf
, *limit
;
5508 U_CHAR
*bp
; /* temp ptr into input buffer */
5509 U_CHAR
*symname
; /* remember where symbol name starts */
5510 int sym_length
; /* and how long it is */
5511 int line
= instack
[indepth
].lineno
;
5512 char *file
= instack
[indepth
].nominal_fname
;
5516 int arglengths
= 0; /* Accumulate lengths of arg names
5517 plus number of args. */
5522 while (is_hor_space
[*bp
])
5525 symname
= bp
; /* remember where it starts */
5526 sym_length
= check_macro_name (bp
, "macro");
5529 /* Lossage will occur if identifiers or control keywords are broken
5530 across lines using backslash. This is not the right place to take
5534 struct arglist
*arg_ptrs
= NULL
;
5537 bp
++; /* skip '(' */
5538 SKIP_WHITE_SPACE (bp
);
5540 /* Loop over macro argument names. */
5541 while (*bp
!= ')') {
5542 struct arglist
*temp
;
5544 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5546 temp
->next
= arg_ptrs
;
5547 temp
->argno
= argno
++;
5548 temp
->rest_args
= 0;
5552 pedwarn ("another parameter follows `%s'",
5555 if (!is_idstart
[*bp
])
5556 pedwarn ("invalid character in macro parameter name");
5558 /* Find the end of the arg name. */
5559 while (is_idchar
[*bp
]) {
5561 /* do we have a "special" rest-args extension here? */
5562 if (limit
- bp
> REST_EXTENSION_LENGTH
5563 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5565 temp
->rest_args
= 1;
5569 temp
->length
= bp
- temp
->name
;
5571 bp
+= REST_EXTENSION_LENGTH
;
5572 arglengths
+= temp
->length
+ 2;
5573 SKIP_WHITE_SPACE (bp
);
5574 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5575 error ("badly punctuated parameter list in `#define'");
5580 SKIP_WHITE_SPACE (bp
);
5581 /* A comma at this point can only be followed by an identifier. */
5582 if (!is_idstart
[*bp
]) {
5583 error ("badly punctuated parameter list in `#define'");
5588 error ("unterminated parameter list in `#define'");
5592 struct arglist
*otemp
;
5594 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5595 if (temp
->length
== otemp
->length
5596 && bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
5597 error ("duplicate argument name `%.*s' in `#define'",
5598 temp
->length
, temp
->name
);
5604 ++bp
; /* skip paren */
5605 SKIP_WHITE_SPACE (bp
);
5606 /* now everything from bp before limit is the definition. */
5607 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5608 defn
->rest_args
= rest_args
;
5610 /* Now set defn->args.argnames to the result of concatenating
5611 the argument names in reverse order
5612 with comma-space between them. */
5613 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5615 struct arglist
*temp
;
5617 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5618 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5620 if (temp
->next
!= 0) {
5621 defn
->args
.argnames
[i
++] = ',';
5622 defn
->args
.argnames
[i
++] = ' ';
5625 defn
->args
.argnames
[i
] = 0;
5628 /* Simple expansion or empty definition. */
5632 if (is_hor_space
[*bp
]) {
5634 SKIP_WHITE_SPACE (bp
);
5635 } else if (sym_length
) {
5637 case '!': case '"': case '#': case '%': case '&': case '\'':
5638 case ')': case '*': case '+': case ',': case '-': case '.':
5639 case '/': case ':': case ';': case '<': case '=': case '>':
5640 case '?': case '[': case '\\': case ']': case '^': case '{':
5641 case '|': case '}': case '~':
5642 warning ("missing white space after `#define %.*s'",
5643 sym_length
, symname
);
5647 pedwarn ("missing white space after `#define %.*s'",
5648 sym_length
, symname
);
5653 /* Now everything from bp before limit is the definition. */
5654 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5655 defn
->args
.argnames
= (U_CHAR
*) "";
5661 /* OP is null if this is a predefinition */
5662 defn
->predefined
= !op
;
5664 mdef
.symnam
= symname
;
5665 mdef
.symlen
= sym_length
;
5674 /* Process a #define directive.
5675 BUF points to the contents of the #define directive, as a contiguous string.
5676 LIMIT points to the first character past the end of the definition.
5677 KEYWORD is the keyword-table entry for #define. */
5680 do_define (buf
, limit
, op
, keyword
)
5681 U_CHAR
*buf
, *limit
;
5683 struct directive
*keyword
;
5688 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5689 if (pcp_outfile
&& op
)
5690 pass_thru_directive (buf
, limit
, op
, keyword
);
5692 mdef
= create_definition (buf
, limit
, op
);
5696 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
5700 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
5702 /* Redefining a precompiled key is ok. */
5703 if (hp
->type
== T_PCSTRING
)
5705 /* Redefining a macro is ok if the definitions are the same. */
5706 else if (hp
->type
== T_MACRO
)
5707 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
5708 /* Redefining a constant is ok with -D. */
5709 else if (hp
->type
== T_CONST
)
5710 ok
= ! done_initializing
;
5711 /* Print the warning if it's not ok. */
5713 /* If we are passing through #define and #undef directives, do
5714 that for this re-definition now. */
5715 if (debug_output
&& op
)
5716 pass_thru_directive (buf
, limit
, op
, keyword
);
5718 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
5719 if (hp
->type
== T_MACRO
)
5720 pedwarn_with_file_and_line (hp
->value
.defn
->file
, hp
->value
.defn
->line
,
5721 "this is the location of the previous definition");
5723 /* Replace the old definition. */
5725 hp
->value
.defn
= mdef
.defn
;
5727 /* If we are passing through #define and #undef directives, do
5728 that for this new definition now. */
5729 if (debug_output
&& op
)
5730 pass_thru_directive (buf
, limit
, op
, keyword
);
5731 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
,
5732 (char *) mdef
.defn
, hashcode
);
5743 /* Check a purported macro name SYMNAME, and yield its length.
5744 USAGE is the kind of name this is intended for. */
5747 check_macro_name (symname
, usage
)
5754 for (p
= symname
; is_idchar
[*p
]; p
++)
5756 sym_length
= p
- symname
;
5758 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
5759 error ("invalid %s name", usage
);
5760 else if (!is_idstart
[*symname
]
5761 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
5762 error ("invalid %s name `%.*s'", usage
, sym_length
, symname
);
5766 /* Return zero if two DEFINITIONs are isomorphic. */
5769 compare_defs (d1
, d2
)
5770 DEFINITION
*d1
, *d2
;
5772 register struct reflist
*a1
, *a2
;
5773 register U_CHAR
*p1
= d1
->expansion
;
5774 register U_CHAR
*p2
= d2
->expansion
;
5777 if (d1
->nargs
!= d2
->nargs
)
5779 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
5781 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
5782 a1
= a1
->next
, a2
= a2
->next
) {
5783 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
5784 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
5785 || a1
->argno
!= a2
->argno
5786 || a1
->stringify
!= a2
->stringify
5787 || a1
->raw_before
!= a2
->raw_before
5788 || a1
->raw_after
!= a2
->raw_after
)
5796 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
5797 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
5802 /* Return 1 if two parts of two macro definitions are effectively different.
5803 One of the parts starts at BEG1 and has LEN1 chars;
5804 the other has LEN2 chars at BEG2.
5805 Any sequence of whitespace matches any other sequence of whitespace.
5806 FIRST means these parts are the first of a macro definition;
5807 so ignore leading whitespace entirely.
5808 LAST means these parts are the last of a macro definition;
5809 so ignore trailing whitespace entirely. */
5812 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
5814 U_CHAR
*beg1
, *beg2
;
5818 register U_CHAR
*end1
= beg1
+ len1
;
5819 register U_CHAR
*end2
= beg2
+ len2
;
5821 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5822 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5825 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
5826 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
5828 while (beg1
!= end1
&& beg2
!= end2
) {
5829 if (is_space
[*beg1
] && is_space
[*beg2
]) {
5830 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5831 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5832 } else if (*beg1
== *beg2
) {
5836 return (beg1
!= end1
) || (beg2
!= end2
);
5839 /* Read a replacement list for a macro with parameters.
5840 Build the DEFINITION structure.
5841 Reads characters of text starting at BUF until END.
5842 ARGLIST specifies the formal parameters to look for
5843 in the text of the definition; NARGS is the number of args
5844 in that list, or -1 for a macro name that wants no argument list.
5845 MACRONAME is the macro name itself (so we can avoid recursive expansion)
5846 and NAMELEN is its length in characters.
5848 Note that comments, backslash-newlines, and leading white space
5849 have already been deleted from the argument. */
5851 /* If there is no trailing whitespace, a Newline Space is added at the end
5852 to prevent concatenation that would be contrary to the standard. */
5855 collect_expansion (buf
, end
, nargs
, arglist
)
5858 struct arglist
*arglist
;
5861 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
5862 struct reflist
*endpat
= NULL
;
5863 /* Pointer to first nonspace after last ## seen. */
5865 /* Pointer to first nonspace after last single-# seen. */
5866 U_CHAR
*stringify
= 0;
5867 /* How those tokens were spelled. */
5868 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
5869 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
5871 int expected_delimiter
= '\0';
5873 /* Scan thru the replacement list, ignoring comments and quoted
5874 strings, picking up on the macro calls. It does a linear search
5875 thru the arg list on every potential symbol. Profiling might say
5876 that something smarter should happen. */
5881 /* Find the beginning of the trailing whitespace. */
5884 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
5886 /* Allocate space for the text in the macro definition.
5887 Each input char may or may not need 1 byte,
5888 so this is an upper bound.
5889 The extra 3 are for invented trailing newline-marker and final null. */
5890 maxsize
= (sizeof (DEFINITION
)
5892 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
5894 defn
->nargs
= nargs
;
5895 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
5900 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
5901 error ("`##' at start of macro definition");
5902 p
+= p
[0] == '#' ? 2 : 4;
5905 /* Process the main body of the definition. */
5907 int skipped_arg
= 0;
5908 register U_CHAR c
= *p
++;
5916 if (expected_delimiter
!= '\0') {
5917 if (c
== expected_delimiter
)
5918 expected_delimiter
= '\0';
5920 expected_delimiter
= c
;
5924 if (p
< limit
&& expected_delimiter
) {
5925 /* In a string, backslash goes through
5926 and makes next char ordinary. */
5932 if (!expected_delimiter
&& *p
== ':') {
5933 /* %: is not a digraph if preceded by an odd number of '<'s. */
5935 while (buf
< p0
&& p0
[-1] == '<')
5938 /* Treat %:%: as ## and %: as #. */
5939 if (p
[1] == '%' && p
[2] == ':') {
5941 goto sharp_sharp_token
;
5952 /* # is ordinary inside a string. */
5953 if (expected_delimiter
)
5957 /* ##: concatenate preceding and following tokens. */
5958 /* Take out the first #, discard preceding whitespace. */
5960 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
5962 /* Skip the second #. */
5964 concat_sharp_token_type
= c
;
5965 if (is_hor_space
[*p
]) {
5966 concat_sharp_token_type
= c
+ 1;
5968 SKIP_WHITE_SPACE (p
);
5972 error ("`##' at end of macro definition");
5973 } else if (nargs
>= 0) {
5974 /* Single #: stringify following argument ref.
5975 Don't leave the # in the expansion. */
5978 stringify_sharp_token_type
= c
;
5979 if (is_hor_space
[*p
]) {
5980 stringify_sharp_token_type
= c
+ 1;
5982 SKIP_WHITE_SPACE (p
);
5984 if (! is_idstart
[*p
] || nargs
== 0
5985 || (*p
== 'L' && (p
[1] == '\'' || p
[1] == '"')))
5986 error ("`#' operator is not followed by a macro argument name");
5993 /* In -traditional mode, recognize arguments inside strings and
5994 and character constants, and ignore special properties of #.
5995 Arguments inside strings are considered "stringified", but no
5996 extra quote marks are supplied. */
6000 if (expected_delimiter
!= '\0') {
6001 if (c
== expected_delimiter
)
6002 expected_delimiter
= '\0';
6004 expected_delimiter
= c
;
6008 /* Backslash quotes delimiters and itself, but not macro args. */
6009 if (expected_delimiter
!= 0 && p
< limit
6010 && (*p
== expected_delimiter
|| *p
== '\\')) {
6017 if (expected_delimiter
!= '\0') /* No comments inside strings. */
6020 /* If we find a comment that wasn't removed by handle_directive,
6021 this must be -traditional. So replace the comment with
6024 while (++p
< limit
) {
6025 if (p
[0] == '*' && p
[1] == '/') {
6031 /* Mark this as a concatenation-point, as if it had been ##. */
6039 /* Handle the start of a symbol. */
6040 if (is_idchar
[c
] && nargs
> 0) {
6041 U_CHAR
*id_beg
= p
- 1;
6045 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6046 id_len
= p
- id_beg
;
6049 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
6050 register struct arglist
*arg
;
6052 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6053 struct reflist
*tpat
;
6055 if (arg
->name
[0] == c
6056 && arg
->length
== id_len
6057 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6058 enum sharp_token_type tpat_stringify
;
6059 if (expected_delimiter
) {
6060 if (warn_stringify
) {
6062 warning ("macro argument `%.*s' is stringified.",
6065 warning ("macro arg `%.*s' would be stringified with -traditional.",
6069 /* If ANSI, don't actually substitute inside a string. */
6072 tpat_stringify
= SHARP_TOKEN
;
6075 = (stringify
== id_beg
6076 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6078 /* make a pat node for this arg and append it to the end of
6080 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6083 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6084 tpat
->raw_after
= NO_SHARP_TOKEN
;
6085 tpat
->rest_args
= arg
->rest_args
;
6086 tpat
->stringify
= tpat_stringify
;
6089 defn
->pattern
= tpat
;
6091 endpat
->next
= tpat
;
6094 tpat
->argno
= arg
->argno
;
6095 tpat
->nchars
= exp_p
- lastp
;
6097 register U_CHAR
*p1
= p
;
6098 SKIP_WHITE_SPACE (p1
);
6101 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6102 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6104 lastp
= exp_p
; /* place to start copying from next time */
6111 /* If this was not a macro arg, copy it into the expansion. */
6112 if (! skipped_arg
) {
6113 register U_CHAR
*lim1
= p
;
6117 if (stringify
== id_beg
)
6118 error ("`#' operator should be followed by a macro argument name");
6123 if (!traditional
&& expected_delimiter
== 0) {
6124 /* If ANSI, put in a newline-space marker to prevent token pasting.
6125 But not if "inside a string" (which in ANSI mode happens only for
6133 defn
->length
= exp_p
- defn
->expansion
;
6135 /* Crash now if we overrun the allocated size. */
6136 if (defn
->length
+ 1 > maxsize
)
6140 /* This isn't worth the time it takes. */
6141 /* give back excess storage */
6142 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6149 do_assert (buf
, limit
, op
, keyword
)
6150 U_CHAR
*buf
, *limit
;
6152 struct directive
*keyword
;
6154 U_CHAR
*bp
; /* temp ptr into input buffer */
6155 U_CHAR
*symname
; /* remember where symbol name starts */
6156 int sym_length
; /* and how long it is */
6157 struct arglist
*tokens
= NULL
;
6159 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6160 pedwarn ("ANSI C does not allow `#assert'");
6164 while (is_hor_space
[*bp
])
6167 symname
= bp
; /* remember where it starts */
6168 sym_length
= check_macro_name (bp
, "assertion");
6170 /* #define doesn't do this, but we should. */
6171 SKIP_WHITE_SPACE (bp
);
6173 /* Lossage will occur if identifiers or control tokens are broken
6174 across lines using backslash. This is not the right place to take
6178 error ("missing token-sequence in `#assert'");
6185 bp
++; /* skip '(' */
6186 SKIP_WHITE_SPACE (bp
);
6188 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6192 error ("empty token-sequence in `#assert'");
6196 ++bp
; /* skip paren */
6197 SKIP_WHITE_SPACE (bp
);
6200 /* If this name isn't already an assertion name, make it one.
6201 Error if it was already in use in some other way. */
6204 ASSERTION_HASHNODE
*hp
;
6205 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6206 struct tokenlist_list
*value
6207 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6209 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6211 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6212 error ("`defined' redefined as assertion");
6213 hp
= assertion_install (symname
, sym_length
, hashcode
);
6216 /* Add the spec'd token-sequence to the list of such. */
6217 value
->tokens
= tokens
;
6218 value
->next
= hp
->value
;
6226 do_unassert (buf
, limit
, op
, keyword
)
6227 U_CHAR
*buf
, *limit
;
6229 struct directive
*keyword
;
6231 U_CHAR
*bp
; /* temp ptr into input buffer */
6232 U_CHAR
*symname
; /* remember where symbol name starts */
6233 int sym_length
; /* and how long it is */
6235 struct arglist
*tokens
= NULL
;
6236 int tokens_specified
= 0;
6238 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6239 pedwarn ("ANSI C does not allow `#unassert'");
6243 while (is_hor_space
[*bp
])
6246 symname
= bp
; /* remember where it starts */
6247 sym_length
= check_macro_name (bp
, "assertion");
6249 /* #define doesn't do this, but we should. */
6250 SKIP_WHITE_SPACE (bp
);
6252 /* Lossage will occur if identifiers or control tokens are broken
6253 across lines using backslash. This is not the right place to take
6259 bp
++; /* skip '(' */
6260 SKIP_WHITE_SPACE (bp
);
6262 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6266 error ("empty token list in `#unassert'");
6270 tokens_specified
= 1;
6272 ++bp
; /* skip paren */
6273 SKIP_WHITE_SPACE (bp
);
6277 ASSERTION_HASHNODE
*hp
;
6278 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6279 struct tokenlist_list
*tail
, *prev
;
6281 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6285 /* If no token list was specified, then eliminate this assertion
6287 if (! tokens_specified
) {
6288 struct tokenlist_list
*next
;
6289 for (tail
= hp
->value
; tail
; tail
= next
) {
6291 free_token_list (tail
->tokens
);
6294 delete_assertion (hp
);
6296 /* If a list of tokens was given, then delete any matching list. */
6301 struct tokenlist_list
*next
= tail
->next
;
6302 if (compare_token_lists (tail
->tokens
, tokens
)) {
6306 hp
->value
= tail
->next
;
6307 free_token_list (tail
->tokens
);
6320 /* Test whether there is an assertion named NAME
6321 and optionally whether it has an asserted token list TOKENS.
6322 NAME is not null terminated; its length is SYM_LENGTH.
6323 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6326 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6329 int tokens_specified
;
6330 struct arglist
*tokens
;
6332 ASSERTION_HASHNODE
*hp
;
6333 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6335 if (pedantic
&& !instack
[indepth
].system_header_p
)
6336 pedwarn ("ANSI C does not allow testing assertions");
6338 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6340 /* It is not an assertion; just return false. */
6343 /* If no token list was specified, then value is 1. */
6344 if (! tokens_specified
)
6348 struct tokenlist_list
*tail
;
6352 /* If a list of tokens was given,
6353 then succeed if the assertion records a matching list. */
6356 if (compare_token_lists (tail
->tokens
, tokens
))
6361 /* Fail if the assertion has no matching list. */
6366 /* Compare two lists of tokens for equality including order of tokens. */
6369 compare_token_lists (l1
, l2
)
6370 struct arglist
*l1
, *l2
;
6373 if (l1
->length
!= l2
->length
)
6375 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6381 /* Succeed if both lists end at the same time. */
6385 /* Read a space-separated list of tokens ending in a close parenthesis.
6386 Return a list of strings, in the order they were written.
6387 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6388 Parse the text starting at *BPP, and update *BPP.
6389 Don't parse beyond LIMIT. */
6391 static struct arglist
*
6392 read_token_list (bpp
, limit
, error_flag
)
6397 struct arglist
*token_ptrs
= 0;
6403 /* Loop over the assertion value tokens. */
6405 struct arglist
*temp
;
6409 /* Find the end of the token. */
6413 } else if (*bp
== ')') {
6418 } else if (*bp
== '"' || *bp
== '\'')
6419 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6421 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6422 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6425 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6426 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6427 bcopy ((char *) beg
, (char *) temp
->name
, bp
- beg
);
6428 temp
->name
[bp
- beg
] = 0;
6429 temp
->next
= token_ptrs
;
6431 temp
->length
= bp
- beg
;
6433 SKIP_WHITE_SPACE (bp
);
6436 error ("unterminated token sequence in `#assert' or `#unassert'");
6443 /* We accumulated the names in reverse order.
6444 Now reverse them to get the proper order. */
6446 register struct arglist
*prev
= 0, *this, *next
;
6447 for (this = token_ptrs
; this; this = next
) {
6457 free_token_list (tokens
)
6458 struct arglist
*tokens
;
6461 struct arglist
*next
= tokens
->next
;
6462 free (tokens
->name
);
6468 /* Install a name in the assertion hash table.
6470 If LEN is >= 0, it is the length of the name.
6471 Otherwise, compute the length by scanning the entire name.
6473 If HASH is >= 0, it is the precomputed hash code.
6474 Otherwise, compute the hash code. */
6476 static ASSERTION_HASHNODE
*
6477 assertion_install (name
, len
, hash
)
6482 register ASSERTION_HASHNODE
*hp
;
6483 register int i
, bucket
;
6484 register U_CHAR
*p
, *q
;
6486 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6487 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6489 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6490 hp
->next
= assertion_hashtab
[bucket
];
6491 assertion_hashtab
[bucket
] = hp
;
6493 if (hp
->next
!= NULL
)
6494 hp
->next
->prev
= hp
;
6497 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6500 for (i
= 0; i
< len
; i
++)
6506 /* Find the most recent hash node for name name (ending with first
6507 non-identifier char) installed by install
6509 If LEN is >= 0, it is the length of the name.
6510 Otherwise, compute the length by scanning the entire name.
6512 If HASH is >= 0, it is the precomputed hash code.
6513 Otherwise, compute the hash code. */
6515 static ASSERTION_HASHNODE
*
6516 assertion_lookup (name
, len
, hash
)
6521 register ASSERTION_HASHNODE
*bucket
;
6523 bucket
= assertion_hashtab
[hash
];
6525 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6527 bucket
= bucket
->next
;
6533 delete_assertion (hp
)
6534 ASSERTION_HASHNODE
*hp
;
6537 if (hp
->prev
!= NULL
)
6538 hp
->prev
->next
= hp
->next
;
6539 if (hp
->next
!= NULL
)
6540 hp
->next
->prev
= hp
->prev
;
6542 /* Make sure that the bucket chain header that the deleted guy was
6543 on points to the right thing afterwards. */
6544 if (hp
== *hp
->bucket_hdr
)
6545 *hp
->bucket_hdr
= hp
->next
;
6551 * interpret #line directive. Remembers previously seen fnames
6552 * in its very own hash table.
6554 #define FNAME_HASHSIZE 37
6557 do_line (buf
, limit
, op
, keyword
)
6558 U_CHAR
*buf
, *limit
;
6560 struct directive
*keyword
;
6562 register U_CHAR
*bp
;
6563 FILE_BUF
*ip
= &instack
[indepth
];
6566 enum file_change_code file_change
= same_file
;
6568 /* Expand any macros. */
6569 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6571 /* Point to macroexpanded line, which is null-terminated now. */
6573 SKIP_WHITE_SPACE (bp
);
6575 if (!isdigit (*bp
)) {
6576 error ("invalid format `#line' directive");
6580 /* The Newline at the end of this line remains to be processed.
6581 To put the next line at the specified line number,
6582 we must store a line number now that is one less. */
6583 new_lineno
= atoi ((char *) bp
) - 1;
6585 /* NEW_LINENO is one less than the actual line number here. */
6586 if (pedantic
&& new_lineno
< 0)
6587 pedwarn ("line number out of range in `#line' directive");
6589 /* skip over the line number. */
6590 while (isdigit (*bp
))
6593 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6594 if (*bp
&& !is_space
[*bp
]) {
6595 error ("invalid format `#line' directive");
6600 SKIP_WHITE_SPACE (bp
);
6603 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6604 HASHNODE
*hp
, **hash_bucket
;
6610 /* Turn the file name, which is a character string literal,
6611 into a null-terminated string. Do this in place. */
6614 switch ((*p
++ = *bp
++)) {
6616 error ("invalid format `#line' directive");
6621 char *bpc
= (char *) bp
;
6622 HOST_WIDE_INT c
= parse_escape (&bpc
, (HOST_WIDE_INT
) (U_CHAR
) (-1));
6623 bp
= (U_CHAR
*) bpc
;
6636 fname_length
= p
- fname
;
6638 SKIP_WHITE_SPACE (bp
);
6641 pedwarn ("garbage at end of `#line' directive");
6643 file_change
= enter_file
;
6644 else if (*bp
== '2')
6645 file_change
= leave_file
;
6646 else if (*bp
== '3')
6647 ip
->system_header_p
= 1;
6648 else if (*bp
== '4')
6649 ip
->system_header_p
= 2;
6651 error ("invalid format `#line' directive");
6656 SKIP_WHITE_SPACE (bp
);
6658 ip
->system_header_p
= 1;
6660 SKIP_WHITE_SPACE (bp
);
6663 ip
->system_header_p
= 2;
6665 SKIP_WHITE_SPACE (bp
);
6668 error ("invalid format `#line' directive");
6673 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
6674 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
6675 if (hp
->length
== fname_length
&&
6676 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
6677 ip
->nominal_fname
= hp
->value
.cpval
;
6681 /* Didn't find it; cons up a new one. */
6682 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
6683 hp
->next
= *hash_bucket
;
6686 hp
->length
= fname_length
;
6687 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
6688 bcopy (fname
, hp
->value
.cpval
, fname_length
);
6691 error ("invalid format `#line' directive");
6695 ip
->lineno
= new_lineno
;
6696 output_line_directive (ip
, op
, 0, file_change
);
6697 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
6701 /* Remove the definition of a symbol from the symbol table.
6702 according to un*x /lib/cpp, it is not an error to undef
6703 something that has no definitions, so it isn't one here either. */
6706 do_undef (buf
, limit
, op
, keyword
)
6707 U_CHAR
*buf
, *limit
;
6709 struct directive
*keyword
;
6713 U_CHAR
*orig_buf
= buf
;
6715 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
6716 if (pcp_outfile
&& op
)
6717 pass_thru_directive (buf
, limit
, op
, keyword
);
6719 SKIP_WHITE_SPACE (buf
);
6720 sym_length
= check_macro_name (buf
, "macro");
6722 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
6723 /* If we are generating additional info for debugging (with -g) we
6724 need to pass through all effective #undef directives. */
6725 if (debug_output
&& op
)
6726 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
6727 if (hp
->type
!= T_MACRO
)
6728 warning ("undefining `%s'", hp
->name
);
6734 SKIP_WHITE_SPACE (buf
);
6736 pedwarn ("garbage after `#undef' directive");
6741 /* Report an error detected by the program we are processing.
6742 Use the text of the line in the error message.
6743 (We use error because it prints the filename & line#.) */
6746 do_error (buf
, limit
, op
, keyword
)
6747 U_CHAR
*buf
, *limit
;
6749 struct directive
*keyword
;
6751 int length
= limit
- buf
;
6752 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
6753 bcopy ((char *) buf
, (char *) copy
, length
);
6755 SKIP_WHITE_SPACE (copy
);
6756 error ("#error %s", copy
);
6760 /* Report a warning detected by the program we are processing.
6761 Use the text of the line in the warning message, then continue.
6762 (We use error because it prints the filename & line#.) */
6765 do_warning (buf
, limit
, op
, keyword
)
6766 U_CHAR
*buf
, *limit
;
6768 struct directive
*keyword
;
6770 int length
= limit
- buf
;
6771 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
6772 bcopy ((char *) buf
, (char *) copy
, length
);
6774 SKIP_WHITE_SPACE (copy
);
6775 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
6776 if -pedantic-errors is given, #warning should cause an error. */
6777 pedwarn ("#warning %s", copy
);
6781 /* Remember the name of the current file being read from so that we can
6782 avoid ever including it again. */
6789 for (i
= indepth
; i
>= 0; i
--)
6790 if (instack
[i
].inc
) {
6791 record_control_macro (instack
[i
].inc
, (U_CHAR
*) "");
6796 /* #ident has already been copied to the output file, so just ignore it. */
6799 do_ident (buf
, limit
, op
, keyword
)
6800 U_CHAR
*buf
, *limit
;
6802 struct directive
*keyword
;
6807 /* Allow #ident in system headers, since that's not user's fault. */
6808 if (pedantic
&& !instack
[indepth
].system_header_p
)
6809 pedwarn ("ANSI C does not allow `#ident'");
6811 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6812 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
6813 bcopy ((char *) trybuf
.buf
, (char *) buf
, trybuf
.bufp
- trybuf
.buf
);
6814 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
6815 len
= (limit
- buf
);
6818 /* Output directive name. */
6819 check_expand (op
, 7);
6820 bcopy ("#ident ", (char *) op
->bufp
, 7);
6823 /* Output the expanded argument line. */
6824 check_expand (op
, len
);
6825 bcopy ((char *) buf
, (char *) op
->bufp
, len
);
6831 /* #pragma and its argument line have already been copied to the output file.
6832 Just check for some recognized pragmas that need validation here. */
6835 do_pragma (buf
, limit
, op
, keyword
)
6836 U_CHAR
*buf
, *limit
;
6838 struct directive
*keyword
;
6840 SKIP_WHITE_SPACE (buf
);
6841 if (!strncmp ((char *) buf
, "once", 4)) {
6842 /* Allow #pragma once in system headers, since that's not the user's
6844 if (!instack
[indepth
].system_header_p
)
6845 warning ("`#pragma once' is obsolete");
6849 if (!strncmp ((char *) buf
, "implementation", 14)) {
6850 /* Be quiet about `#pragma implementation' for a file only if it hasn't
6851 been included yet. */
6854 U_CHAR
*p
= buf
+ 14, *fname
;
6855 SKIP_WHITE_SPACE (p
);
6856 if (*p
== '\n' || *p
!= '\"')
6860 if ((p
= (U_CHAR
*) index ((char *) fname
, '\"')))
6863 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
6864 struct include_file
*inc
;
6865 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
6866 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
6867 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
6877 /* This was a fun hack, but #pragma seems to start to be useful.
6878 By failing to recognize it, we pass it through unchanged to cc1. */
6880 /* The behavior of the #pragma directive is implementation defined.
6881 this implementation defines it as follows. */
6887 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
6890 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
6892 execl ("/usr/games/hack", "#pragma", 0);
6893 execl ("/usr/games/rogue", "#pragma", 0);
6894 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
6895 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
6897 fatal ("You are in a maze of twisty compiler features, all different");
6901 #ifdef SCCS_DIRECTIVE
6903 /* Just ignore #sccs, on systems where we define it at all. */
6906 do_sccs (buf
, limit
, op
, keyword
)
6907 U_CHAR
*buf
, *limit
;
6909 struct directive
*keyword
;
6912 pedwarn ("ANSI C does not allow `#sccs'");
6916 #endif /* defined (SCCS_DIRECTIVE) */
6918 /* Handle #if directive by
6919 1) inserting special `defined' keyword into the hash table
6920 that gets turned into 0 or 1 by special_symbol (thus,
6921 if the luser has a symbol called `defined' already, it won't
6922 work inside the #if directive)
6923 2) rescan the input into a temporary output buffer
6924 3) pass the output buffer to the yacc parser and collect a value
6925 4) clean up the mess left from steps 1 and 2.
6926 5) call conditional_skip to skip til the next #endif (etc.),
6927 or not, depending on the value from step 3. */
6930 do_if (buf
, limit
, op
, keyword
)
6931 U_CHAR
*buf
, *limit
;
6933 struct directive
*keyword
;
6935 HOST_WIDE_INT value
;
6936 FILE_BUF
*ip
= &instack
[indepth
];
6938 value
= eval_if_expression (buf
, limit
- buf
);
6939 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
6943 /* Handle a #elif directive by not changing if_stack either.
6944 see the comment above do_else. */
6947 do_elif (buf
, limit
, op
, keyword
)
6948 U_CHAR
*buf
, *limit
;
6950 struct directive
*keyword
;
6952 HOST_WIDE_INT value
;
6953 FILE_BUF
*ip
= &instack
[indepth
];
6955 if (if_stack
== instack
[indepth
].if_stack
) {
6956 error ("`#elif' not within a conditional");
6959 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
6960 error ("`#elif' after `#else'");
6961 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
6962 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
6963 && strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
6964 fprintf (stderr
, ", file %s", if_stack
->fname
);
6965 fprintf (stderr
, ")\n");
6967 if_stack
->type
= T_ELIF
;
6970 if (if_stack
->if_succeeded
)
6971 skip_if_group (ip
, 0, op
);
6973 value
= eval_if_expression (buf
, limit
- buf
);
6975 skip_if_group (ip
, 0, op
);
6977 ++if_stack
->if_succeeded
; /* continue processing input */
6978 output_line_directive (ip
, op
, 1, same_file
);
6984 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
6985 result as a C expression and return the value as an int. */
6987 static HOST_WIDE_INT
6988 eval_if_expression (buf
, length
)
6993 HASHNODE
*save_defined
;
6994 HOST_WIDE_INT value
;
6996 save_defined
= install ((U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
6999 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
7001 delete_macro (save_defined
); /* clean up special symbol */
7003 temp_obuf
.buf
[temp_obuf
.length
] = '\n';
7004 value
= parse_c_expression ((char *) temp_obuf
.buf
);
7006 free (temp_obuf
.buf
);
7011 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7012 or don't skip to the #endif/#else/#elif depending on what directive
7013 is actually being processed. */
7016 do_xifdef (buf
, limit
, op
, keyword
)
7017 U_CHAR
*buf
, *limit
;
7019 struct directive
*keyword
;
7022 FILE_BUF
*ip
= &instack
[indepth
];
7024 int start_of_file
= 0;
7025 U_CHAR
*control_macro
= 0;
7027 /* Detect a #ifndef at start of file (not counting comments). */
7028 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7029 U_CHAR
*p
= ip
->buf
;
7030 while (p
!= directive_start
) {
7034 /* Make no special provision for backslash-newline here; this is
7035 slower if backslash-newlines are present, but it's correct,
7036 and it's not worth it to tune for the rare backslash-newline. */
7038 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7039 /* Skip this comment. */
7041 U_CHAR
*save_bufp
= ip
->bufp
;
7043 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7044 ip
->bufp
= save_bufp
;
7049 /* If we get here, this conditional is the beginning of the file. */
7054 /* Discard leading and trailing whitespace. */
7055 SKIP_WHITE_SPACE (buf
);
7056 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7058 /* Find the end of the identifier at the beginning. */
7059 for (end
= buf
; is_idchar
[*end
]; end
++);
7062 skip
= (keyword
->type
== T_IFDEF
);
7064 pedwarn (end
== limit
? "`#%s' with no argument"
7065 : "`#%s' argument starts with punctuation",
7070 if (! traditional
) {
7071 if (isdigit (buf
[0]))
7072 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7073 else if (end
!= limit
)
7074 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7077 hp
= lookup (buf
, end
-buf
, -1);
7080 /* Output a precondition for this macro. */
7082 && (hp
->type
== T_CONST
7083 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7084 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7087 fprintf (pcp_outfile
, "#undef ");
7088 while (is_idchar
[*cp
]) /* Ick! */
7089 fputc (*cp
++, pcp_outfile
);
7090 putc ('\n', pcp_outfile
);
7094 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7095 if (start_of_file
&& !skip
) {
7096 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7097 bcopy ((char *) buf
, (char *) control_macro
, end
- buf
);
7098 control_macro
[end
- buf
] = 0;
7102 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7106 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7107 If this is a #ifndef starting at the beginning of a file,
7108 CONTROL_MACRO is the macro name tested by the #ifndef.
7109 Otherwise, CONTROL_MACRO is 0. */
7112 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7115 enum node_type type
;
7116 U_CHAR
*control_macro
;
7119 IF_STACK_FRAME
*temp
;
7121 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7122 temp
->fname
= ip
->nominal_fname
;
7123 temp
->lineno
= ip
->lineno
;
7124 temp
->next
= if_stack
;
7125 temp
->control_macro
= control_macro
;
7128 if_stack
->type
= type
;
7131 skip_if_group (ip
, 0, op
);
7134 ++if_stack
->if_succeeded
;
7135 output_line_directive (ip
, &outbuf
, 1, same_file
);
7139 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7140 Leaves input ptr at the sharp sign found.
7141 If ANY is nonzero, return at next directive of any sort. */
7144 skip_if_group (ip
, any
, op
)
7149 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7150 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7151 struct directive
*kt
;
7152 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7153 U_CHAR
*beg_of_line
= bp
;
7154 register int ident_length
;
7155 U_CHAR
*ident
, *after_ident
;
7156 /* Save info about where the group starts. */
7157 U_CHAR
*beg_of_group
= bp
;
7158 int beg_lineno
= ip
->lineno
;
7160 if (output_conditionals
&& op
!= 0) {
7161 char *ptr
= "#failed\n";
7162 int len
= strlen (ptr
);
7164 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7169 check_expand (op
, len
);
7170 bcopy (ptr
, (char *) op
->bufp
, len
);
7173 output_line_directive (ip
, op
, 1, 0);
7178 case '/': /* possible comment */
7179 if (*bp
== '\\' && bp
[1] == '\n')
7182 || (cplusplus_comments
&& *bp
== '/')) {
7184 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7189 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7190 NULL_PTR
, NULL_PTR
);
7193 /* Char after backslash loses its special meaning. */
7196 ++ip
->lineno
; /* But do update the line-count. */
7205 if (beg_of_line
== 0 || traditional
)
7208 while (bp
[0] == '\\' && bp
[1] == '\n')
7214 /* # keyword: a # must be first nonblank char on the line */
7215 if (beg_of_line
== 0)
7219 /* Scan from start of line, skipping whitespace, comments
7220 and backslash-newlines, and see if we reach this #.
7221 If not, this # is not special. */
7223 /* If -traditional, require # to be at beginning of line. */
7226 if (is_hor_space
[*bp
])
7228 else if (*bp
== '\\' && bp
[1] == '\n')
7230 else if (*bp
== '/' && bp
[1] == '*') {
7232 while (!(*bp
== '*' && bp
[1] == '/'))
7236 /* There is no point in trying to deal with C++ // comments here,
7237 because if there is one, then this # must be part of the
7238 comment and we would never reach here. */
7242 if (bp
!= ip
->bufp
) {
7243 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7247 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7248 if (ip
->bufp
[0] == '%') {
7249 /* Skip past the ':' again. */
7250 while (*bp
== '\\') {
7257 /* Skip whitespace and \-newline. */
7259 if (is_hor_space
[*bp
])
7261 else if (*bp
== '\\' && bp
[1] == '\n')
7263 else if (*bp
== '/') {
7265 for (bp
+= 2; ; bp
++) {
7268 else if (*bp
== '*') {
7269 if (bp
[-1] == '/' && warn_comments
)
7270 warning ("`/*' within comment");
7276 } else if (bp
[1] == '/' && cplusplus_comments
) {
7277 for (bp
+= 2; ; bp
++) {
7282 warning ("multiline `//' comment");
7294 /* Now find end of directive name.
7295 If we encounter a backslash-newline, exchange it with any following
7296 symbol-constituents so that we end up with a contiguous name. */
7302 if (*bp
== '\\' && bp
[1] == '\n')
7303 name_newline_fix (bp
);
7309 ident_length
= bp
- cp
;
7313 /* A line of just `#' becomes blank. */
7315 if (ident_length
== 0 && *after_ident
== '\n') {
7319 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7321 while (is_idchar
[*p
]) {
7322 if (*p
< '0' || *p
> '9')
7326 /* Handle # followed by a line number. */
7327 if (p
!= ident
&& !is_idchar
[*p
]) {
7329 pedwarn ("`#' followed by integer");
7333 /* Avoid error for `###' and similar cases unless -pedantic. */
7335 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7337 if (pedantic
&& !lang_asm
)
7338 pedwarn ("invalid preprocessing directive");
7343 if (!lang_asm
&& pedantic
)
7344 pedwarn ("invalid preprocessing directive name");
7348 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7349 IF_STACK_FRAME
*temp
;
7350 if (ident_length
== kt
->length
7351 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7352 /* If we are asked to return on next directive, do so now. */
7360 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7361 temp
->next
= if_stack
;
7363 temp
->lineno
= ip
->lineno
;
7364 temp
->fname
= ip
->nominal_fname
;
7365 temp
->type
= kt
->type
;
7369 if (pedantic
&& if_stack
!= save_if_stack
)
7370 validate_else (bp
, endb
);
7372 if (if_stack
== instack
[indepth
].if_stack
) {
7373 error ("`#%s' not within a conditional", kt
->name
);
7376 else if (if_stack
== save_if_stack
)
7377 goto done
; /* found what we came for */
7379 if (kt
->type
!= T_ENDIF
) {
7380 if (if_stack
->type
== T_ELSE
)
7381 error ("`#else' or `#elif' after `#else'");
7382 if_stack
->type
= kt
->type
;
7387 if_stack
= if_stack
->next
;
7397 /* Don't let erroneous code go by. */
7398 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7399 pedwarn ("invalid preprocessing directive name");
7404 /* after this returns, rescan will exit because ip->bufp
7405 now points to the end of the buffer.
7406 rescan is responsible for the error message also. */
7409 if (output_conditionals
&& op
!= 0) {
7410 char *ptr
= "#endfailed\n";
7411 int len
= strlen (ptr
);
7413 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7418 check_expand (op
, beg_of_line
- beg_of_group
);
7419 bcopy ((char *) beg_of_group
, (char *) op
->bufp
,
7420 beg_of_line
- beg_of_group
);
7421 op
->bufp
+= beg_of_line
- beg_of_group
;
7422 op
->lineno
+= ip
->lineno
- beg_lineno
;
7423 check_expand (op
, len
);
7424 bcopy (ptr
, (char *) op
->bufp
, len
);
7430 /* Handle a #else directive. Do this by just continuing processing
7431 without changing if_stack ; this is so that the error message
7432 for missing #endif's etc. will point to the original #if. It
7433 is possible that something different would be better. */
7436 do_else (buf
, limit
, op
, keyword
)
7437 U_CHAR
*buf
, *limit
;
7439 struct directive
*keyword
;
7441 FILE_BUF
*ip
= &instack
[indepth
];
7444 SKIP_WHITE_SPACE (buf
);
7446 pedwarn ("text following `#else' violates ANSI standard");
7449 if (if_stack
== instack
[indepth
].if_stack
) {
7450 error ("`#else' not within a conditional");
7453 /* #ifndef can't have its special treatment for containing the whole file
7454 if it has a #else clause. */
7455 if_stack
->control_macro
= 0;
7457 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7458 error ("`#else' after `#else'");
7459 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7460 if (strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
7461 fprintf (stderr
, ", file %s", if_stack
->fname
);
7462 fprintf (stderr
, ")\n");
7464 if_stack
->type
= T_ELSE
;
7467 if (if_stack
->if_succeeded
)
7468 skip_if_group (ip
, 0, op
);
7470 ++if_stack
->if_succeeded
; /* continue processing input */
7471 output_line_directive (ip
, op
, 1, same_file
);
7476 /* Unstack after #endif directive. */
7479 do_endif (buf
, limit
, op
, keyword
)
7480 U_CHAR
*buf
, *limit
;
7482 struct directive
*keyword
;
7485 SKIP_WHITE_SPACE (buf
);
7487 pedwarn ("text following `#endif' violates ANSI standard");
7490 if (if_stack
== instack
[indepth
].if_stack
)
7491 error ("unbalanced `#endif'");
7493 IF_STACK_FRAME
*temp
= if_stack
;
7494 if_stack
= if_stack
->next
;
7495 if (temp
->control_macro
!= 0) {
7496 /* This #endif matched a #ifndef at the start of the file.
7497 See if it is at the end of the file. */
7498 FILE_BUF
*ip
= &instack
[indepth
];
7499 U_CHAR
*p
= ip
->bufp
;
7500 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7506 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7507 /* Skip this comment. */
7509 U_CHAR
*save_bufp
= ip
->bufp
;
7511 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7512 ip
->bufp
= save_bufp
;
7517 /* If we get here, this #endif ends a #ifndef
7518 that contains all of the file (aside from whitespace).
7519 Arrange not to include the file again
7520 if the macro that was tested is defined.
7522 Do not do this for the top-level file in a -include or any
7523 file in a -imacros. */
7525 && ! (indepth
== 1 && no_record_file
)
7526 && ! (no_record_file
&& no_output
))
7527 record_control_macro (ip
->inc
, temp
->control_macro
);
7531 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
7536 /* When an #else or #endif is found while skipping failed conditional,
7537 if -pedantic was specified, this is called to warn about text after
7538 the directive name. P points to the first char after the directive
7542 validate_else (p
, limit
)
7544 register U_CHAR
*limit
;
7546 /* Advance P over whitespace and comments. */
7548 while (*p
== '\\' && p
[1] == '\n')
7550 if (is_hor_space
[*p
])
7552 else if (*p
== '/') {
7553 while (p
[1] == '\\' && p
[2] == '\n')
7556 /* Don't bother warning about unterminated comments
7557 since that will happen later. Just be sure to exit. */
7558 for (p
+= 2; ; p
++) {
7562 while (p
[1] == '\\' && p
[2] == '\n')
7571 else if (cplusplus_comments
&& p
[1] == '/')
7577 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
7580 /* Skip a comment, assuming the input ptr immediately follows the
7581 initial slash-star. Bump *LINE_COUNTER for each newline.
7582 (The canonical line counter is &ip->lineno.)
7583 Don't use this routine (or the next one) if bumping the line
7584 counter is not sufficient to deal with newlines in the string.
7586 If NOWARN is nonzero, don't warn about slash-star inside a comment.
7587 This feature is useful when processing a comment that is going to
7588 be processed or was processed at another point in the preprocessor,
7589 to avoid a duplicate warning. Likewise for unterminated comment
7593 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
7594 register FILE_BUF
*ip
;
7595 int *line_counter
; /* place to remember newlines, or NULL */
7598 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
7599 register U_CHAR
*bp
= ip
->bufp
;
7600 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
7601 int start_line
= line_counter
? *line_counter
: 0;
7603 /* JF this line_counter stuff is a crock to make sure the
7604 comment is only put out once, no matter how many times
7605 the comment is skipped. It almost works */
7608 *op
->bufp
++ = bp
[-1];
7610 if (cplusplus_comments
&& bp
[-1] == '/') {
7611 for (; bp
< limit
; bp
++) {
7615 if (!nowarn
&& warn_comments
)
7616 warning ("multiline `//' comment");
7628 while (bp
< limit
) {
7633 /* If this is the end of the file, we have an unterminated comment.
7634 Don't swallow the newline. We are guaranteed that there will be a
7635 trailing newline and various pieces assume it's there. */
7642 if (line_counter
!= NULL
)
7648 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
7649 warning ("`/*' within comment");
7650 if (*bp
== '\\' && bp
[1] == '\n')
7663 error_with_line (line_for_error (start_line
), "unterminated comment");
7668 /* Skip over a quoted string. BP points to the opening quote.
7669 Returns a pointer after the closing quote. Don't go past LIMIT.
7670 START_LINE is the line number of the starting point (but it need
7671 not be valid if the starting point is inside a macro expansion).
7673 The input stack state is not changed.
7675 If COUNT_NEWLINES is nonzero, it points to an int to increment
7676 for each newline passed.
7678 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
7679 if we pass a backslash-newline.
7681 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
7684 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
7685 register U_CHAR
*bp
;
7686 register U_CHAR
*limit
;
7688 int *count_newlines
;
7689 int *backslash_newlines_p
;
7692 register U_CHAR c
, match
;
7697 error_with_line (line_for_error (start_line
),
7698 "unterminated string or character constant");
7699 error_with_line (multiline_string_line
,
7700 "possible real start of unterminated constant");
7701 multiline_string_line
= 0;
7708 while (*bp
== '\\' && bp
[1] == '\n') {
7709 if (backslash_newlines_p
)
7710 *backslash_newlines_p
= 1;
7715 if (*bp
== '\n' && count_newlines
) {
7716 if (backslash_newlines_p
)
7717 *backslash_newlines_p
= 1;
7721 } else if (c
== '\n') {
7723 /* Unterminated strings and character constants are 'valid'. */
7724 bp
--; /* Don't consume the newline. */
7729 if (match
== '\'') {
7730 error_with_line (line_for_error (start_line
),
7731 "unterminated string or character constant");
7737 /* If not traditional, then allow newlines inside strings. */
7740 if (multiline_string_line
== 0) {
7742 pedwarn_with_line (line_for_error (start_line
),
7743 "string constant runs past end of line");
7744 multiline_string_line
= start_line
;
7746 } else if (c
== match
)
7752 /* Place into DST a quoted string representing the string SRC.
7753 Return the address of DST's terminating null. */
7756 quote_string (dst
, src
)
7763 switch ((c
= *src
++))
7770 sprintf (dst
, "\\%03o", c
);
7788 /* Skip across a group of balanced parens, starting from IP->bufp.
7789 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
7791 This does not handle newlines, because it's used for the arg of #if,
7792 where there aren't any newlines. Also, backslash-newline can't appear. */
7795 skip_paren_group (ip
)
7796 register FILE_BUF
*ip
;
7798 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
7799 U_CHAR
*p
= ip
->bufp
;
7801 int lines_dummy
= 0;
7803 while (p
!= limit
) {
7813 return ip
->bufp
= p
;
7819 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
7827 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
7829 return ip
->bufp
= p
;
7839 /* Write out a #line directive, for instance, after an #include file.
7840 If CONDITIONAL is nonzero, we can omit the #line if it would
7841 appear to be a no-op, and we can output a few newlines instead
7842 if we want to increase the line number by a small amount.
7843 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
7846 output_line_directive (ip
, op
, conditional
, file_change
)
7849 enum file_change_code file_change
;
7852 char *line_directive_buf
, *line_end
;
7854 if (no_line_directives
7855 || ip
->fname
== NULL
7857 op
->lineno
= ip
->lineno
;
7862 if (ip
->lineno
== op
->lineno
)
7865 /* If the inherited line number is a little too small,
7866 output some newlines instead of a #line directive. */
7867 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
7868 check_expand (op
, 10);
7869 while (ip
->lineno
> op
->lineno
) {
7877 /* Output a positive line number if possible. */
7878 while (ip
->lineno
<= 0 && ip
->bufp
- ip
->buf
< ip
->length
7879 && *ip
->bufp
== '\n') {
7884 line_directive_buf
= (char *) alloca (4 * strlen (ip
->nominal_fname
) + 100);
7885 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
7886 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
7888 if (file_change
!= same_file
) {
7890 *line_end
++ = file_change
== enter_file
? '1' : '2';
7892 /* Tell cc1 if following text comes from a system header file. */
7893 if (ip
->system_header_p
) {
7897 #ifndef NO_IMPLICIT_EXTERN_C
7898 /* Tell cc1plus if following text should be treated as C. */
7899 if (ip
->system_header_p
== 2 && cplusplus
) {
7905 len
= line_end
- line_directive_buf
;
7906 check_expand (op
, len
+ 1);
7907 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7909 bcopy ((char *) line_directive_buf
, (char *) op
->bufp
, len
);
7911 op
->lineno
= ip
->lineno
;
7914 /* This structure represents one parsed argument in a macro call.
7915 `raw' points to the argument text as written (`raw_length' is its length).
7916 `expanded' points to the argument's macro-expansion
7917 (its length is `expand_length').
7918 `stringified_length' is the length the argument would have
7920 `use_count' is the number of times this macro arg is substituted
7921 into the macro. If the actual use count exceeds 10,
7922 the value stored is 10.
7923 `free1' and `free2', if nonzero, point to blocks to be freed
7924 when the macro argument data is no longer needed. */
7927 U_CHAR
*raw
, *expanded
;
7928 int raw_length
, expand_length
;
7929 int stringified_length
;
7930 U_CHAR
*free1
, *free2
;
7935 /* Expand a macro call.
7936 HP points to the symbol that is the macro being called.
7937 Put the result of expansion onto the input stack
7938 so that subsequent input by our caller will use it.
7940 If macro wants arguments, caller has already verified that
7941 an argument list follows; arguments come from the input stack. */
7944 macroexpand (hp
, op
)
7949 DEFINITION
*defn
= hp
->value
.defn
;
7950 register U_CHAR
*xbuf
;
7952 int start_line
= instack
[indepth
].lineno
;
7953 int rest_args
, rest_zero
;
7955 CHECK_DEPTH (return;);
7957 /* it might not actually be a macro. */
7958 if (hp
->type
!= T_MACRO
) {
7959 special_symbol (hp
, op
);
7963 /* This macro is being used inside a #if, which means it must be */
7964 /* recorded as a precondition. */
7965 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
7966 dump_single_macro (hp
, pcp_outfile
);
7968 nargs
= defn
->nargs
;
7972 struct argdata
*args
;
7973 char *parse_error
= 0;
7975 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
7977 for (i
= 0; i
< nargs
; i
++) {
7978 args
[i
].raw
= (U_CHAR
*) "";
7979 args
[i
].expanded
= 0;
7980 args
[i
].raw_length
= args
[i
].expand_length
7981 = args
[i
].stringified_length
= 0;
7982 args
[i
].free1
= args
[i
].free2
= 0;
7983 args
[i
].use_count
= 0;
7986 /* Parse all the macro args that are supplied. I counts them.
7987 The first NARGS args are stored in ARGS.
7988 The rest are discarded.
7989 If rest_args is set then we assume macarg absorbed the rest of the args.
7994 /* Discard the open-parenthesis or comma before the next arg. */
7995 ++instack
[indepth
].bufp
;
7998 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
7999 /* If we are working on last arg which absorbs rest of args... */
8000 if (i
== nargs
- 1 && defn
->rest_args
)
8002 parse_error
= macarg (&args
[i
], rest_args
);
8005 parse_error
= macarg (NULL_PTR
, 0);
8007 error_with_line (line_for_error (start_line
), parse_error
);
8011 } while (*instack
[indepth
].bufp
!= ')');
8013 /* If we got one arg but it was just whitespace, call that 0 args. */
8015 register U_CHAR
*bp
= args
[0].raw
;
8016 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
8017 /* cpp.texi says for foo ( ) we provide one argument.
8018 However, if foo wants just 0 arguments, treat this as 0. */
8020 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8025 /* Don't output an error message if we have already output one for
8026 a parse error above. */
8028 if (nargs
== 0 && i
> 0) {
8030 error ("arguments given to macro `%s'", hp
->name
);
8031 } else if (i
< nargs
) {
8032 /* traditional C allows foo() if foo wants one argument. */
8033 if (nargs
== 1 && i
== 0 && traditional
)
8035 /* the rest args token is allowed to absorb 0 tokens */
8036 else if (i
== nargs
- 1 && defn
->rest_args
)
8038 else if (parse_error
)
8041 error ("macro `%s' used without args", hp
->name
);
8043 error ("macro `%s' used with just one arg", hp
->name
);
8045 error ("macro `%s' used with only %d args", hp
->name
, i
);
8046 } else if (i
> nargs
) {
8048 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8051 /* Swallow the closeparen. */
8052 ++instack
[indepth
].bufp
;
8054 /* If macro wants zero args, we parsed the arglist for checking only.
8055 Read directly from the macro definition. */
8057 xbuf
= defn
->expansion
;
8058 xbuf_len
= defn
->length
;
8060 register U_CHAR
*exp
= defn
->expansion
;
8061 register int offset
; /* offset in expansion,
8062 copied a piece at a time */
8063 register int totlen
; /* total amount of exp buffer filled so far */
8065 register struct reflist
*ap
, *last_ap
;
8067 /* Macro really takes args. Compute the expansion of this call. */
8069 /* Compute length in characters of the macro's expansion.
8070 Also count number of times each arg is used. */
8071 xbuf_len
= defn
->length
;
8072 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8074 xbuf_len
+= args
[ap
->argno
].stringified_length
;
8075 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8076 /* Add 4 for two newline-space markers to prevent
8077 token concatenation. */
8078 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8080 /* We have an ordinary (expanded) occurrence of the arg.
8081 So compute its expansion, if we have not already. */
8082 if (args
[ap
->argno
].expanded
== 0) {
8084 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8085 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8088 args
[ap
->argno
].expanded
= obuf
.buf
;
8089 args
[ap
->argno
].expand_length
= obuf
.length
;
8090 args
[ap
->argno
].free2
= obuf
.buf
;
8093 /* Add 4 for two newline-space markers to prevent
8094 token concatenation. */
8095 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
8097 if (args
[ap
->argno
].use_count
< 10)
8098 args
[ap
->argno
].use_count
++;
8101 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8103 /* Generate in XBUF the complete expansion
8104 with arguments substituted in.
8105 TOTLEN is the total size generated so far.
8106 OFFSET is the index in the definition
8107 of where we are copying from. */
8108 offset
= totlen
= 0;
8109 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8110 last_ap
= ap
, ap
= ap
->next
) {
8111 register struct argdata
*arg
= &args
[ap
->argno
];
8112 int count_before
= totlen
;
8114 /* Add chars to XBUF. */
8115 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8116 xbuf
[totlen
++] = exp
[offset
];
8118 /* If followed by an empty rest arg with concatenation,
8119 delete the last run of nonwhite chars. */
8120 if (rest_zero
&& totlen
> count_before
8121 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8122 || (last_ap
!= NULL
&& last_ap
->rest_args
8123 && last_ap
->raw_after
!= 0))) {
8124 /* Delete final whitespace. */
8125 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8129 /* Delete the nonwhites before them. */
8130 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8135 if (ap
->stringify
!= 0) {
8136 int arglen
= arg
->raw_length
;
8142 && (c
= arg
->raw
[i
], is_space
[c
]))
8145 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8148 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8149 for (; i
< arglen
; i
++) {
8152 /* Special markers Newline Space
8153 generate nothing for a stringified argument. */
8154 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
8159 /* Internal sequences of whitespace are replaced by one space
8160 except within an string or char token. */
8162 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
8164 /* Note that Newline Space does occur within whitespace
8165 sequences; consider it part of the sequence. */
8166 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
8168 else if (c
!= '\n' && is_space
[c
])
8185 } else if (c
== '\"' || c
== '\'')
8189 /* Escape these chars */
8190 if (c
== '\"' || (in_string
&& c
== '\\'))
8191 xbuf
[totlen
++] = '\\';
8195 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
8200 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8201 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8202 U_CHAR
*p1
= arg
->raw
;
8203 U_CHAR
*l1
= p1
+ arg
->raw_length
;
8204 if (ap
->raw_before
!= 0) {
8205 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8206 while (p1
!= l1
&& is_idchar
[*p1
])
8207 xbuf
[totlen
++] = *p1
++;
8208 /* Delete any no-reexpansion marker that follows
8209 an identifier at the beginning of the argument
8210 if the argument is concatenated with what precedes it. */
8211 if (p1
[0] == '\n' && p1
[1] == '-')
8213 } else if (!traditional
) {
8214 /* Ordinary expanded use of the argument.
8215 Put in newline-space markers to prevent token pasting. */
8216 xbuf
[totlen
++] = '\n';
8217 xbuf
[totlen
++] = ' ';
8219 if (ap
->raw_after
!= 0) {
8220 /* Arg is concatenated after: delete trailing whitespace,
8221 whitespace markers, and no-reexpansion markers. */
8223 if (is_space
[l1
[-1]]) l1
--;
8224 else if (l1
[-1] == '-') {
8225 U_CHAR
*p2
= l1
- 1;
8226 /* If a `-' is preceded by an odd number of newlines then it
8227 and the last newline are a no-reexpansion marker. */
8228 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8229 if ((l1
- 1 - p2
) & 1) {
8238 bcopy ((char *) p1
, (char *) (xbuf
+ totlen
), l1
- p1
);
8240 if (!traditional
&& ap
->raw_after
== 0) {
8241 /* Ordinary expanded use of the argument.
8242 Put in newline-space markers to prevent token pasting. */
8243 xbuf
[totlen
++] = '\n';
8244 xbuf
[totlen
++] = ' ';
8247 /* Ordinary expanded use of the argument.
8248 Put in newline-space markers to prevent token pasting. */
8250 xbuf
[totlen
++] = '\n';
8251 xbuf
[totlen
++] = ' ';
8253 bcopy ((char *) arg
->expanded
, (char *) (xbuf
+ totlen
),
8254 arg
->expand_length
);
8255 totlen
+= arg
->expand_length
;
8257 xbuf
[totlen
++] = '\n';
8258 xbuf
[totlen
++] = ' ';
8260 /* If a macro argument with newlines is used multiple times,
8261 then only expand the newlines once. This avoids creating output
8262 lines which don't correspond to any input line, which confuses
8264 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8265 /* Don't bother doing change_newlines for subsequent
8269 = change_newlines (arg
->expanded
, arg
->expand_length
);
8273 if (totlen
> xbuf_len
)
8277 /* If there is anything left of the definition after handling
8278 the arg list, copy that in too. */
8280 for (i
= offset
; i
< defn
->length
; i
++) {
8281 /* if we've reached the end of the macro */
8284 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8285 && last_ap
->raw_after
!= 0))
8286 xbuf
[totlen
++] = exp
[i
];
8292 for (i
= 0; i
< nargs
; i
++) {
8293 if (args
[i
].free1
!= 0)
8294 free (args
[i
].free1
);
8295 if (args
[i
].free2
!= 0)
8296 free (args
[i
].free2
);
8300 xbuf
= defn
->expansion
;
8301 xbuf_len
= defn
->length
;
8304 /* Now put the expansion on the input stack
8305 so our caller will commence reading from it. */
8307 register FILE_BUF
*ip2
;
8309 ip2
= &instack
[++indepth
];
8312 ip2
->nominal_fname
= 0;
8314 /* This may not be exactly correct, but will give much better error
8315 messages for nested macro calls than using a line number of zero. */
8316 ip2
->lineno
= start_line
;
8318 ip2
->length
= xbuf_len
;
8320 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8322 ip2
->if_stack
= if_stack
;
8323 ip2
->system_header_p
= 0;
8325 /* Recursive macro use sometimes works traditionally.
8326 #define foo(x,y) bar (x (y,0), y)
8330 hp
->type
= T_DISABLED
;
8334 /* Parse a macro argument and store the info on it into *ARGPTR.
8335 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8336 Return nonzero to indicate a syntax error. */
8339 macarg (argptr
, rest_args
)
8340 register struct argdata
*argptr
;
8343 FILE_BUF
*ip
= &instack
[indepth
];
8349 /* Try to parse as much of the argument as exists at this
8350 input stack level. */
8351 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
8352 &paren
, &newlines
, &comments
, rest_args
);
8354 /* If we find the end of the argument at this level,
8355 set up *ARGPTR to point at it in the input stack. */
8356 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
8357 && bp
!= ip
->buf
+ ip
->length
) {
8359 argptr
->raw
= ip
->bufp
;
8360 argptr
->raw_length
= bp
- ip
->bufp
;
8361 argptr
->newlines
= newlines
;
8365 /* This input stack level ends before the macro argument does.
8366 We must pop levels and keep parsing.
8367 Therefore, we must allocate a temporary buffer and copy
8368 the macro argument into it. */
8369 int bufsize
= bp
- ip
->bufp
;
8370 int extra
= newlines
;
8371 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8372 int final_start
= 0;
8374 bcopy ((char *) ip
->bufp
, (char *) buffer
, bufsize
);
8376 ip
->lineno
+= newlines
;
8378 while (bp
== ip
->buf
+ ip
->length
) {
8379 if (instack
[indepth
].macro
== 0) {
8380 result
= "unterminated macro call";
8383 ip
->macro
->type
= T_MACRO
;
8385 free (ip
->free_ptr
);
8386 ip
= &instack
[--indepth
];
8389 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
8390 &newlines
, &comments
, rest_args
);
8391 final_start
= bufsize
;
8392 bufsize
+= bp
- ip
->bufp
;
8394 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
8395 bcopy ((char *) ip
->bufp
, (char *) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
8398 ip
->lineno
+= newlines
;
8401 /* Now, if arg is actually wanted, record its raw form,
8402 discarding comments and duplicating newlines in whatever
8403 part of it did not come from a macro expansion.
8404 EXTRA space has been preallocated for duplicating the newlines.
8405 FINAL_START is the index of the start of that part. */
8407 argptr
->raw
= buffer
;
8408 argptr
->raw_length
= bufsize
;
8409 argptr
->free1
= buffer
;
8410 argptr
->newlines
= newlines
;
8411 if ((newlines
|| comments
) && ip
->fname
!= 0)
8414 discard_comments (argptr
->raw
+ final_start
,
8415 argptr
->raw_length
- final_start
,
8417 argptr
->raw
[argptr
->raw_length
] = 0;
8418 if (argptr
->raw_length
> bufsize
+ extra
)
8423 /* If we are not discarding this argument,
8424 macroexpand it and compute its length as stringified.
8425 All this info goes into *ARGPTR. */
8428 register U_CHAR
*buf
, *lim
;
8429 register int totlen
;
8432 lim
= buf
+ argptr
->raw_length
;
8434 while (buf
!= lim
&& is_space
[*buf
])
8436 while (buf
!= lim
&& is_space
[lim
[-1]])
8438 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
8439 while (buf
!= lim
) {
8440 register U_CHAR c
= *buf
++;
8442 /* Internal sequences of whitespace are replaced by one space
8443 in most cases, but not always. So count all the whitespace
8444 in case we need to keep it all. */
8447 SKIP_ALL_WHITE_SPACE (buf
);
8450 if (c
== '\"' || c
== '\\') /* escape these chars */
8452 else if (!isprint (c
))
8455 argptr
->stringified_length
= totlen
;
8460 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8461 counting parens in *DEPTHPTR,
8462 and return if reach LIMIT
8463 or before a `)' that would make *DEPTHPTR negative
8464 or before a comma when *DEPTHPTR is zero.
8465 Single and double quotes are matched and termination
8466 is inhibited within them. Comments also inhibit it.
8467 Value returned is pointer to stopping place.
8469 Increment *NEWLINES each time a newline is passed.
8470 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8471 Set *COMMENTS to 1 if a comment is seen. */
8474 macarg1 (start
, limit
, depthptr
, newlines
, comments
, rest_args
)
8476 register U_CHAR
*limit
;
8477 int *depthptr
, *newlines
, *comments
;
8480 register U_CHAR
*bp
= start
;
8482 while (bp
< limit
) {
8488 if (--(*depthptr
) < 0)
8492 /* Traditionally, backslash makes following char not special. */
8493 if (bp
+ 1 < limit
&& traditional
)
8496 /* But count source lines anyway. */
8505 if (bp
[1] == '\\' && bp
[2] == '\n')
8506 newline_fix (bp
+ 1);
8509 for (bp
+= 2; bp
< limit
; bp
++) {
8512 else if (*bp
== '*') {
8513 if (bp
[-1] == '/' && warn_comments
)
8514 warning ("`/*' within comment");
8515 if (bp
[1] == '\\' && bp
[2] == '\n')
8516 newline_fix (bp
+ 1);
8523 } else if (bp
[1] == '/' && cplusplus_comments
) {
8525 for (bp
+= 2; bp
< limit
; bp
++) {
8531 warning ("multiline `//' comment");
8540 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
8545 while (*bp
== '\\' && bp
[1] == '\n') {
8548 } else if (*bp
== '\n') {
8557 /* if we've returned to lowest level and we aren't absorbing all args */
8558 if ((*depthptr
) == 0 && rest_args
== 0)
8568 /* Discard comments and duplicate newlines
8569 in the string of length LENGTH at START,
8570 except inside of string constants.
8571 The string is copied into itself with its beginning staying fixed.
8573 NEWLINES is the number of newlines that must be duplicated.
8574 We assume that that much extra space is available past the end
8578 discard_comments (start
, length
, newlines
)
8583 register U_CHAR
*ibp
;
8584 register U_CHAR
*obp
;
8585 register U_CHAR
*limit
;
8588 /* If we have newlines to duplicate, copy everything
8589 that many characters up. Then, in the second part,
8590 we will have room to insert the newlines
8592 NEWLINES may actually be too large, because it counts
8593 newlines in string constants, and we don't duplicate those.
8594 But that does no harm. */
8596 ibp
= start
+ length
;
8597 obp
= ibp
+ newlines
;
8599 while (limit
!= ibp
)
8603 ibp
= start
+ newlines
;
8604 limit
= start
+ length
+ newlines
;
8607 while (ibp
< limit
) {
8608 *obp
++ = c
= *ibp
++;
8611 /* Duplicate the newline. */
8623 if (*ibp
== '\\' && ibp
[1] == '\n')
8625 /* Delete any comment. */
8626 if (cplusplus_comments
&& ibp
[0] == '/') {
8627 /* Comments are equivalent to spaces. */
8630 while (ibp
< limit
&& (*ibp
!= '\n' || ibp
[-1] == '\\'))
8634 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
8636 /* Comments are equivalent to spaces.
8637 For -traditional, a comment is equivalent to nothing. */
8643 while (ibp
+ 1 < limit
) {
8645 && ibp
[1] == '\\' && ibp
[2] == '\n')
8646 newline_fix (ibp
+ 1);
8647 if (ibp
[0] == '*' && ibp
[1] == '/')
8656 /* Notice and skip strings, so that we don't
8657 think that comments start inside them,
8658 and so we don't duplicate newlines in them. */
8661 while (ibp
< limit
) {
8662 *obp
++ = c
= *ibp
++;
8665 if (c
== '\n' && quotec
== '\'')
8667 if (c
== '\\' && ibp
< limit
) {
8668 while (*ibp
== '\\' && ibp
[1] == '\n')
8681 /* Turn newlines to spaces in the string of length LENGTH at START,
8682 except inside of string constants.
8683 The string is copied into itself with its beginning staying fixed. */
8686 change_newlines (start
, length
)
8690 register U_CHAR
*ibp
;
8691 register U_CHAR
*obp
;
8692 register U_CHAR
*limit
;
8696 limit
= start
+ length
;
8699 while (ibp
< limit
) {
8700 *obp
++ = c
= *ibp
++;
8703 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
8704 string. Skip past the newline and its duplicate.
8705 Put a space in the output. */
8716 /* Notice and skip strings, so that we don't delete newlines in them. */
8719 while (ibp
< limit
) {
8720 *obp
++ = c
= *ibp
++;
8723 if (c
== '\n' && quotec
== '\'')
8734 /* my_strerror - return the descriptive text associated with an
8738 my_strerror (errnum
)
8744 #ifndef HAVE_STRERROR
8745 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
8747 result
= strerror (errnum
);
8750 /* VAXCRTL's strerror() takes an optional second argument, which only
8751 matters when the first argument is EVMSERR. However, it's simplest
8752 just to pass it unconditionally. `vaxc$errno' is declared in
8753 <errno.h>, and maintained by the library in parallel with `errno'.
8754 We assume that caller's `errnum' either matches the last setting of
8755 `errno' by the library or else does not have the value `EVMSERR'. */
8757 result
= strerror (errnum
, vaxc$errno
);
8761 result
= "undocumented I/O error";
8766 /* error - print error message and increment count of errors. */
8769 error (PRINTF_ALIST (msg
))
8774 VA_START (args
, msg
);
8785 FILE_BUF
*ip
= NULL
;
8787 print_containing_files ();
8789 for (i
= indepth
; i
>= 0; i
--)
8790 if (instack
[i
].fname
!= NULL
) {
8796 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8797 vfprintf (stderr
, msg
, args
);
8798 fprintf (stderr
, "\n");
8802 /* Error including a message from `errno'. */
8805 error_from_errno (name
)
8809 FILE_BUF
*ip
= NULL
;
8811 print_containing_files ();
8813 for (i
= indepth
; i
>= 0; i
--)
8814 if (instack
[i
].fname
!= NULL
) {
8820 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8822 fprintf (stderr
, "%s: %s\n", name
, my_strerror (errno
));
8827 /* Print error message but don't count it. */
8830 warning (PRINTF_ALIST (msg
))
8835 VA_START (args
, msg
);
8836 vwarning (msg
, args
);
8841 vwarning (msg
, args
)
8846 FILE_BUF
*ip
= NULL
;
8848 if (inhibit_warnings
)
8851 if (warnings_are_errors
)
8854 print_containing_files ();
8856 for (i
= indepth
; i
>= 0; i
--)
8857 if (instack
[i
].fname
!= NULL
) {
8863 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8864 fprintf (stderr
, "warning: ");
8865 vfprintf (stderr
, msg
, args
);
8866 fprintf (stderr
, "\n");
8870 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8871 error_with_line (int line
, PRINTF_ALIST (msg
))
8873 error_with_line (line
, PRINTF_ALIST (msg
))
8880 VA_START (args
, msg
);
8881 verror_with_line (line
, msg
, args
);
8886 verror_with_line (line
, msg
, args
)
8892 FILE_BUF
*ip
= NULL
;
8894 print_containing_files ();
8896 for (i
= indepth
; i
>= 0; i
--)
8897 if (instack
[i
].fname
!= NULL
) {
8903 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, line
);
8904 vfprintf (stderr
, msg
, args
);
8905 fprintf (stderr
, "\n");
8910 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8911 warning_with_line (int line
, PRINTF_ALIST (msg
))
8913 warning_with_line (line
, PRINTF_ALIST (msg
))
8920 VA_START (args
, msg
);
8921 vwarning_with_line (line
, msg
, args
);
8926 vwarning_with_line (line
, msg
, args
)
8932 FILE_BUF
*ip
= NULL
;
8934 if (inhibit_warnings
)
8937 if (warnings_are_errors
)
8940 print_containing_files ();
8942 for (i
= indepth
; i
>= 0; i
--)
8943 if (instack
[i
].fname
!= NULL
) {
8949 fprintf (stderr
, line
? "%s:%d: " : "%s: ", ip
->nominal_fname
, line
);
8950 fprintf (stderr
, "warning: ");
8951 vfprintf (stderr
, msg
, args
);
8952 fprintf (stderr
, "\n");
8955 /* Print an error message and maybe count it. */
8958 pedwarn (PRINTF_ALIST (msg
))
8963 VA_START (args
, msg
);
8964 if (pedantic_errors
)
8967 vwarning (msg
, args
);
8972 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8973 pedwarn_with_line (int line
, PRINTF_ALIST (msg
))
8975 pedwarn_with_line (line
, PRINTF_ALIST (msg
))
8982 VA_START (args
, msg
);
8983 if (pedantic_errors
)
8984 verror_with_line (line
, msg
, args
);
8986 vwarning_with_line (line
, msg
, args
);
8990 /* Report a warning (or an error if pedantic_errors)
8991 giving specified file name and line number, not current. */
8994 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8995 pedwarn_with_file_and_line (char *file
, int line
, PRINTF_ALIST (msg
))
8997 pedwarn_with_file_and_line (file
, line
, PRINTF_ALIST (msg
))
9005 if (!pedantic_errors
&& inhibit_warnings
)
9008 fprintf (stderr
, "%s:%d: ", file
, line
);
9009 if (pedantic_errors
)
9011 if (!pedantic_errors
)
9012 fprintf (stderr
, "warning: ");
9013 VA_START (args
, msg
);
9014 vfprintf (stderr
, msg
, args
);
9016 fprintf (stderr
, "\n");
9019 /* Print the file names and line numbers of the #include
9020 directives which led to the current file. */
9023 print_containing_files ()
9025 FILE_BUF
*ip
= NULL
;
9029 /* If stack of files hasn't changed since we last printed
9030 this info, don't repeat it. */
9031 if (last_error_tick
== input_file_stack_tick
)
9034 for (i
= indepth
; i
>= 0; i
--)
9035 if (instack
[i
].fname
!= NULL
) {
9040 /* Give up if we don't find a source file. */
9044 /* Find the other, outer source files. */
9045 for (i
--; i
>= 0; i
--)
9046 if (instack
[i
].fname
!= NULL
) {
9050 fprintf (stderr
, "In file included");
9052 fprintf (stderr
, ",\n ");
9055 fprintf (stderr
, " from %s:%d", ip
->nominal_fname
, ip
->lineno
);
9058 fprintf (stderr
, ":\n");
9060 /* Record we have printed the status as of this time. */
9061 last_error_tick
= input_file_stack_tick
;
9064 /* Return the line at which an error occurred.
9065 The error is not necessarily associated with the current spot
9066 in the input stack, so LINE says where. LINE will have been
9067 copied from ip->lineno for the current input level.
9068 If the current level is for a file, we return LINE.
9069 But if the current level is not for a file, LINE is meaningless.
9070 In that case, we return the lineno of the innermost file. */
9073 line_for_error (line
)
9079 for (i
= indepth
; i
>= 0; ) {
9080 if (instack
[i
].fname
!= 0)
9085 line1
= instack
[i
].lineno
;
9093 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9095 * As things stand, nothing is ever placed in the output buffer to be
9096 * removed again except when it's KNOWN to be part of an identifier,
9097 * so flushing and moving down everything left, instead of expanding,
9101 /* You might think void was cleaner for the return type,
9102 but that would get type mismatch in check_expand in strict ANSI. */
9105 grow_outbuf (obuf
, needed
)
9106 register FILE_BUF
*obuf
;
9107 register int needed
;
9112 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9115 /* Make it at least twice as big as it is now. */
9117 /* Make it have at least 150% of the free space we will need. */
9118 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9119 if (minsize
> obuf
->length
)
9120 obuf
->length
= minsize
;
9122 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
9125 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9131 /* Symbol table for macro names and special symbols */
9134 * install a name in the main hash table, even if it is already there.
9135 * name stops with first non alphanumeric, except leading '#'.
9136 * caller must check against redefinition if that is desired.
9137 * delete_macro () removes things installed by install () in fifo order.
9138 * this is important because of the `defined' special symbol used
9139 * in #if, and also if pushdef/popdef directives are ever implemented.
9141 * If LEN is >= 0, it is the length of the name.
9142 * Otherwise, compute the length by scanning the entire name.
9144 * If HASH is >= 0, it is the precomputed hash code.
9145 * Otherwise, compute the hash code.
9149 install (name
, len
, type
, value
, hash
)
9152 enum node_type type
;
9156 register HASHNODE
*hp
;
9157 register int i
, bucket
;
9158 register U_CHAR
*p
, *q
;
9162 while (is_idchar
[*p
])
9168 hash
= hashf (name
, len
, HASHSIZE
);
9170 i
= sizeof (HASHNODE
) + len
+ 1;
9171 hp
= (HASHNODE
*) xmalloc (i
);
9173 hp
->bucket_hdr
= &hashtab
[bucket
];
9174 hp
->next
= hashtab
[bucket
];
9175 hashtab
[bucket
] = hp
;
9177 if (hp
->next
!= NULL
)
9178 hp
->next
->prev
= hp
;
9181 hp
->value
.cpval
= value
;
9182 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9185 for (i
= 0; i
< len
; i
++)
9192 * find the most recent hash node for name name (ending with first
9193 * non-identifier char) installed by install
9195 * If LEN is >= 0, it is the length of the name.
9196 * Otherwise, compute the length by scanning the entire name.
9198 * If HASH is >= 0, it is the precomputed hash code.
9199 * Otherwise, compute the hash code.
9203 lookup (name
, len
, hash
)
9208 register U_CHAR
*bp
;
9209 register HASHNODE
*bucket
;
9212 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
9217 hash
= hashf (name
, len
, HASHSIZE
);
9219 bucket
= hashtab
[hash
];
9221 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
9223 bucket
= bucket
->next
;
9229 * Delete a hash node. Some weirdness to free junk from macros.
9230 * More such weirdness will have to be added if you define more hash
9231 * types that need it.
9234 /* Note that the DEFINITION of a macro is removed from the hash table
9235 but its storage is not freed. This would be a storage leak
9236 except that it is not reasonable to keep undefining and redefining
9237 large numbers of macros many times.
9238 In any case, this is necessary, because a macro can be #undef'd
9239 in the middle of reading the arguments to a call to it.
9240 If #undef freed the DEFINITION, that would crash. */
9247 if (hp
->prev
!= NULL
)
9248 hp
->prev
->next
= hp
->next
;
9249 if (hp
->next
!= NULL
)
9250 hp
->next
->prev
= hp
->prev
;
9252 /* Make sure that the bucket chain header that the deleted guy was
9253 on points to the right thing afterwards. */
9254 if (hp
== *hp
->bucket_hdr
)
9255 *hp
->bucket_hdr
= hp
->next
;
9258 if (hp
->type
== T_MACRO
) {
9259 DEFINITION
*d
= hp
->value
.defn
;
9260 struct reflist
*ap
, *nextap
;
9262 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
9273 * return hash function on name. must be compatible with the one
9274 * computed a step at a time, elsewhere
9278 hashf (name
, len
, hashsize
)
9279 register U_CHAR
*name
;
9286 r
= HASHSTEP (r
, *name
++);
9288 return MAKE_POS (r
) % hashsize
;
9292 /* Dump the definition of a single macro HP to OF. */
9295 dump_single_macro (hp
, of
)
9296 register HASHNODE
*hp
;
9299 register DEFINITION
*defn
= hp
->value
.defn
;
9305 /* Print the definition of the macro HP. */
9307 fprintf (of
, "#define %s", hp
->name
);
9309 if (defn
->nargs
>= 0) {
9313 for (i
= 0; i
< defn
->nargs
; i
++) {
9314 dump_arg_n (defn
, i
, of
);
9315 if (i
+ 1 < defn
->nargs
)
9325 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
9326 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
9327 offset
+= ap
->nchars
;
9329 if (ap
->nchars
!= 0)
9331 if (ap
->stringify
) {
9332 switch (ap
->stringify
) {
9333 case SHARP_TOKEN
: fprintf (of
, "#"); break;
9334 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
9335 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
9336 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
9340 if (ap
->raw_before
!= 0) {
9342 switch (ap
->raw_before
) {
9343 case WHITE_SHARP_TOKEN
:
9344 case WHITE_PERCENT_COLON_TOKEN
:
9351 switch (ap
->raw_before
) {
9352 case SHARP_TOKEN
: fprintf (of
, "##"); break;
9353 case WHITE_SHARP_TOKEN
: fprintf (of
, "## "); break;
9354 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
9355 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%: "); break;
9362 dump_arg_n (defn
, ap
->argno
, of
);
9363 if (!traditional
&& ap
->raw_after
!= 0) {
9364 switch (ap
->raw_after
) {
9365 case SHARP_TOKEN
: fprintf (of
, "##"); break;
9366 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
9367 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
9368 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
9374 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
9378 /* Dump all macro definitions as #defines to stdout. */
9385 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
9386 register HASHNODE
*hp
;
9388 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
9389 if (hp
->type
== T_MACRO
)
9390 dump_single_macro (hp
, stdout
);
9395 /* Output to OF a substring of a macro definition.
9396 BASE is the beginning of the definition.
9397 Output characters START thru LENGTH.
9398 Unless traditional, discard newlines outside of strings, thus
9399 converting funny-space markers to ordinary spaces. */
9402 dump_defn_1 (base
, start
, length
, of
)
9408 U_CHAR
*p
= base
+ start
;
9409 U_CHAR
*limit
= base
+ start
+ length
;
9412 fwrite (p
, sizeof (*p
), length
, of
);
9415 if (*p
== '\"' || *p
=='\'') {
9416 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
9417 NULL_PTR
, NULL_PTR
);
9418 fwrite (p
, sizeof (*p
), p1
- p
, of
);
9429 /* Print the name of argument number ARGNUM of macro definition DEFN
9431 Recall that DEFN->args.argnames contains all the arg names
9432 concatenated in reverse order with comma-space in between. */
9435 dump_arg_n (defn
, argnum
, of
)
9440 register U_CHAR
*p
= defn
->args
.argnames
;
9441 while (argnum
+ 1 < defn
->nargs
) {
9442 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
9446 while (*p
&& *p
!= ',') {
9452 /* Initialize syntactic classifications of characters. */
9455 initialize_char_syntax ()
9460 * Set up is_idchar and is_idstart tables. These should be
9461 * faster than saying (is_alpha (c) || c == '_'), etc.
9462 * Set up these things before calling any routines tthat
9465 for (i
= 'a'; i
<= 'z'; i
++) {
9466 is_idchar
[i
- 'a' + 'A'] = 1;
9468 is_idstart
[i
- 'a' + 'A'] = 1;
9471 for (i
= '0'; i
<= '9'; i
++)
9474 is_idstart
['_'] = 1;
9476 is_idstart
['$'] = 1;
9478 /* horizontal space table */
9479 is_hor_space
[' '] = 1;
9480 is_hor_space
['\t'] = 1;
9481 is_hor_space
['\v'] = 1;
9482 is_hor_space
['\f'] = 1;
9483 is_hor_space
['\r'] = 1;
9492 char_name
['\v'] = "vertical tab";
9493 char_name
['\f'] = "formfeed";
9494 char_name
['\r'] = "carriage return";
9497 /* Initialize the built-in macros. */
9500 initialize_builtins (inp
, outp
)
9504 install ((U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
9505 install ((U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
9506 install ((U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
9507 install ((U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
9508 install ((U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
9509 install ((U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
9510 #ifndef NO_BUILTIN_SIZE_TYPE
9511 install ((U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
9513 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9514 install ((U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
9516 install ((U_CHAR
*) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, NULL_PTR
, -1);
9517 install ((U_CHAR
*) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
,
9519 install ((U_CHAR
*) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
,
9521 install ((U_CHAR
*) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE
,
9523 install ((U_CHAR
*) "__TIME__", -1, T_TIME
, NULL_PTR
, -1);
9525 install ((U_CHAR
*) "__STDC__", -1, T_CONST
, "1", -1);
9526 install ((U_CHAR
*) "__STDC_VERSION__", -1, T_CONST
, "199409L", -1);
9529 install ((U_CHAR
*) "__OBJC__", -1, T_CONST
, "1", -1);
9530 /* This is supplied using a -D by the compiler driver
9531 so that it is present only when truly compiling with GNU C. */
9532 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
9533 install ((U_CHAR
*) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST
, "1", -1);
9537 char directive
[2048];
9538 U_CHAR
*udirective
= (U_CHAR
*) directive
;
9539 register struct directive
*dp
= &directive_table
[0];
9540 struct tm
*timebuf
= timestamp ();
9542 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
9543 instack
[0].nominal_fname
);
9544 output_line_directive (inp
, outp
, 0, same_file
);
9545 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9548 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
9549 output_line_directive (inp
, outp
, 0, same_file
);
9550 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9553 #ifndef NO_BUILTIN_SIZE_TYPE
9554 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
9555 output_line_directive (inp
, outp
, 0, same_file
);
9556 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9560 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9561 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
9562 output_line_directive (inp
, outp
, 0, same_file
);
9563 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9567 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
9568 output_line_directive (inp
, outp
, 0, same_file
);
9569 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9572 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
9573 monthnames
[timebuf
->tm_mon
],
9574 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
9575 output_line_directive (inp
, outp
, 0, same_file
);
9576 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9579 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
9580 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
9581 output_line_directive (inp
, outp
, 0, same_file
);
9582 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9587 sprintf (directive
, " __STDC__ 1");
9588 output_line_directive (inp
, outp
, 0, same_file
);
9589 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9594 sprintf (directive
, " __OBJC__ 1");
9595 output_line_directive (inp
, outp
, 0, same_file
);
9596 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9603 * process a given definition string, for initialization
9604 * If STR is just an identifier, define it with value 1.
9605 * If STR has anything after the identifier, then it should
9606 * be identifier=definition.
9610 make_definition (str
, op
)
9615 struct directive
*kt
;
9618 p
= buf
= (U_CHAR
*) str
;
9619 if (!is_idstart
[*p
]) {
9620 error ("malformed option `-D %s'", str
);
9623 while (is_idchar
[*++p
])
9626 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
9629 p
= (U_CHAR
*) str
; /* Error */
9632 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
9633 strcpy ((char *)buf
, str
);
9634 strcat ((char *)buf
, " 1");
9635 } else if (*p
!= '=') {
9636 error ("malformed option `-D %s'", str
);
9640 /* Copy the entire option so we can modify it. */
9641 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
9642 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
9643 /* Change the = to a space. */
9644 buf
[p
- (U_CHAR
*) str
] = ' ';
9645 /* Scan for any backslash-newline and remove it. */
9647 q
= &buf
[p
- (U_CHAR
*) str
];
9649 if (*p
== '\"' || *p
== '\'') {
9650 int unterminated
= 0;
9651 U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
9652 NULL_PTR
, NULL_PTR
, &unterminated
);
9656 if (*p
== '\\' && p
[1] == '\n')
9660 } else if (*p
== '\\' && p
[1] == '\n')
9662 /* Change newline chars into newline-markers. */
9663 else if (*p
== '\n')
9675 ip
= &instack
[++indepth
];
9676 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
9678 ip
->buf
= ip
->bufp
= buf
;
9679 ip
->length
= strlen ((char *) buf
);
9683 ip
->if_stack
= if_stack
;
9684 ip
->system_header_p
= 0;
9686 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
9689 /* Pass NULL instead of OP, since this is a "predefined" macro. */
9690 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
9694 /* JF, this does the work for the -U option */
9697 make_undef (str
, op
)
9702 struct directive
*kt
;
9704 ip
= &instack
[++indepth
];
9705 ip
->nominal_fname
= ip
->fname
= "*undef*";
9707 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
9708 ip
->length
= strlen (str
);
9712 ip
->if_stack
= if_stack
;
9713 ip
->system_header_p
= 0;
9715 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
9718 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
9722 /* Process the string STR as if it appeared as the body of a #assert.
9723 OPTION is the option name for which STR was the argument. */
9726 make_assertion (option
, str
)
9731 struct directive
*kt
;
9732 U_CHAR
*buf
, *p
, *q
;
9734 /* Copy the entire option so we can modify it. */
9735 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
9736 strcpy ((char *) buf
, str
);
9737 /* Scan for any backslash-newline and remove it. */
9740 if (*p
== '\\' && p
[1] == '\n')
9748 if (!is_idstart
[*p
]) {
9749 error ("malformed option `%s %s'", option
, str
);
9752 while (is_idchar
[*++p
])
9754 SKIP_WHITE_SPACE (p
);
9755 if (! (*p
== 0 || *p
== '(')) {
9756 error ("malformed option `%s %s'", option
, str
);
9760 ip
= &instack
[++indepth
];
9761 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
9763 ip
->buf
= ip
->bufp
= buf
;
9764 ip
->length
= strlen ((char *) buf
);
9768 ip
->if_stack
= if_stack
;
9769 ip
->system_header_p
= 0;
9771 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
9774 /* Pass NULL as output ptr to do_define since we KNOW it never does
9776 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
9780 /* The previous include prefix, if any, is PREV_FILE_NAME.
9781 Allocate a new include prefix whose name is the
9782 simplified concatenation of PREFIX and NAME,
9783 with a trailing / added if needed.
9784 But return 0 if the include prefix should be ignored,
9785 e.g. because it is a duplicate of PREV_FILE_NAME. */
9787 static struct file_name_list
*
9788 new_include_prefix (prev_file_name
, prefix
, name
)
9789 struct file_name_list
*prev_file_name
;
9794 fatal ("Directory name missing after command line option");
9797 /* Ignore the empty string. */
9800 struct file_name_list
*dir
9801 = ((struct file_name_list
*)
9802 xmalloc (sizeof (struct file_name_list
)
9803 + strlen (prefix
) + strlen (name
) + 1 /* for trailing / */));
9805 strcpy (dir
->fname
, prefix
);
9806 strcat (dir
->fname
, name
);
9807 len
= simplify_filename (dir
->fname
);
9809 /* Convert directory name to a prefix. */
9810 if (dir
->fname
[len
- 1] != '/') {
9811 if (len
== 1 && dir
->fname
[len
- 1] == '.')
9814 dir
->fname
[len
++] = '/';
9815 dir
->fname
[len
] = 0;
9818 /* Ignore a directory whose name matches the previous one. */
9819 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
9820 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
9821 if (!first_bracket_include
)
9822 first_bracket_include
= prev_file_name
;
9828 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
9830 /* Ignore a nonexistent directory. */
9831 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
9832 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
9833 error_from_errno (dir
->fname
);
9838 /* Ignore a directory whose identity matches the previous one. */
9840 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
9841 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
9842 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
9843 if (!first_bracket_include
)
9844 first_bracket_include
= prev_file_name
;
9851 dir
->c_system_include_path
= 0;
9852 dir
->got_name_map
= 0;
9858 /* Append a chain of `struct file_name_list's
9859 to the end of the main include chain.
9860 FIRST is the beginning of the chain to append, and LAST is the end. */
9863 append_include_chain (first
, last
)
9864 struct file_name_list
*first
, *last
;
9866 struct file_name_list
*dir
;
9868 if (!first
|| !last
)
9874 last_include
->next
= first
;
9876 if (first_bracket_include
== 0)
9877 first_bracket_include
= first
;
9879 for (dir
= first
; ; dir
= dir
->next
) {
9880 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
9881 if (len
> max_include_len
)
9882 max_include_len
= len
;
9888 last_include
= last
;
9891 /* Add output to `deps_buffer' for the -M switch.
9892 STRING points to the text to be output.
9893 SPACER is ':' for targets, ' ' for dependencies. */
9896 deps_output (string
, spacer
)
9900 int size
= strlen (string
);
9905 #ifndef MAX_OUTPUT_COLUMNS
9906 #define MAX_OUTPUT_COLUMNS 72
9908 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
9909 && 1 < deps_column
) {
9910 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
9917 if (deps_size
+ size
+ 8 > deps_allocated_size
) {
9918 deps_allocated_size
= (deps_size
+ size
+ 50) * 2;
9919 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
9921 if (spacer
== ' ') {
9922 deps_buffer
[deps_size
++] = ' ';
9925 bcopy (string
, &deps_buffer
[deps_size
], size
);
9927 deps_column
+= size
;
9928 if (spacer
== ':') {
9929 deps_buffer
[deps_size
++] = ':';
9932 deps_buffer
[deps_size
] = 0;
9936 fatal (PRINTF_ALIST (msg
))
9941 fprintf (stderr
, "%s: ", progname
);
9942 VA_START (args
, msg
);
9943 vfprintf (stderr
, msg
, args
);
9945 fprintf (stderr
, "\n");
9946 exit (FATAL_EXIT_CODE
);
9949 /* More 'friendly' abort that prints the line and file.
9950 config.h can #define abort fancy_abort if you like that sort of thing. */
9955 fatal ("Internal gcc abort.");
9959 perror_with_name (name
)
9962 fprintf (stderr
, "%s: ", progname
);
9963 fprintf (stderr
, "%s: %s\n", name
, my_strerror (errno
));
9968 pfatal_with_name (name
)
9971 perror_with_name (name
);
9975 exit (FATAL_EXIT_CODE
);
9979 /* Handler for SIGPIPE. */
9983 /* If this is missing, some compilers complain. */
9986 fatal ("output pipe has been closed");
9992 fatal ("Memory exhausted.");
10000 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (size
);
10007 xrealloc (old
, size
)
10011 register GENERIC_PTR ptr
= (GENERIC_PTR
) realloc (old
, size
);
10018 xcalloc (number
, size
)
10019 size_t number
, size
;
10021 register size_t total
= number
* size
;
10022 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (total
);
10025 bzero (ptr
, total
);
10033 size_t size
= strlen (input
);
10034 char *output
= xmalloc (size
+ 1);
10035 strcpy (output
, input
);
10041 /* Under VMS we need to fix up the "include" specification filename so
10042 that everything following the 1st slash is changed into its correct
10043 VMS file specification. */
10046 hack_vms_include_specification (fname
, vaxc_include
)
10050 register char *cp
, *cp1
, *cp2
;
10051 int f
, check_filename_before_returning
;
10054 check_filename_before_returning
= 0;
10056 cp
= base_name (fname
);
10059 * Check if we have a vax-c style '#include filename'
10060 * and add the missing .h
10062 if (vaxc_include
&& !index (cp
,'.'))
10065 cp2
= Local
; /* initialize */
10067 /* We are trying to do a number of things here. First of all, we are
10068 trying to hammer the filenames into a standard format, such that later
10069 processing can handle them.
10071 If the file name contains something like [dir.], then it recognizes this
10072 as a root, and strips the ".]". Later processing will add whatever is
10073 needed to get things working properly.
10075 If no device is specified, then the first directory name is taken to be
10076 a device name (or a rooted logical). */
10078 /* See if we found that 1st slash */
10079 if (cp
== 0) return; /* Nothing to do!!! */
10080 if (*cp
!= '/') return; /* Nothing to do!!! */
10081 /* Point to the UNIX filename part (which needs to be fixed!) */
10083 /* If the directory spec is not rooted, we can just copy
10084 the UNIX filename part and we are done */
10085 if (((cp
- fname
) > 1) && ((cp
[-1] == ']') || (cp
[-1] == '>'))) {
10086 if (cp
[-2] != '.') {
10088 * The VMS part ends in a `]', and the preceding character is not a `.'.
10089 * We strip the `]', and then splice the two parts of the name in the
10090 * usual way. Given the default locations for include files in cccp.c,
10091 * we will only use this code if the user specifies alternate locations
10092 * with the /include (-I) switch on the command line. */
10093 cp
-= 1; /* Strip "]" */
10094 cp1
--; /* backspace */
10097 * The VMS part has a ".]" at the end, and this will not do. Later
10098 * processing will add a second directory spec, and this would be a syntax
10099 * error. Thus we strip the ".]", and thus merge the directory specs.
10100 * We also backspace cp1, so that it points to a '/'. This inhibits the
10101 * generation of the 000000 root directory spec (which does not belong here
10104 cp
-= 2; /* Strip ".]" */
10105 cp1
--; }; /* backspace */
10108 /* We drop in here if there is no VMS style directory specification yet.
10109 * If there is no device specification either, we make the first dir a
10110 * device and try that. If we do not do this, then we will be essentially
10111 * searching the users default directory (as if they did a #include "asdf.h").
10113 * Then all we need to do is to push a '[' into the output string. Later
10114 * processing will fill this in, and close the bracket.
10116 if (cp
[-1] != ':') *cp2
++ = ':'; /* dev not in spec. take first dir */
10117 *cp2
++ = '['; /* Open the directory specification */
10120 /* at this point we assume that we have the device spec, and (at least
10121 the opening "[" for a directory specification. We may have directories
10122 specified already */
10124 /* If there are no other slashes then the filename will be
10125 in the "root" directory. Otherwise, we need to add
10126 directory specifications. */
10127 if (index (cp1
, '/') == 0) {
10128 /* Just add "000000]" as the directory string */
10129 strcpy (cp2
, "000000]");
10130 cp2
+= strlen (cp2
);
10131 check_filename_before_returning
= 1; /* we might need to fool with this later */
10133 /* As long as there are still subdirectories to add, do them. */
10134 while (index (cp1
, '/') != 0) {
10135 /* If this token is "." we can ignore it */
10136 if ((cp1
[0] == '.') && (cp1
[1] == '/')) {
10140 /* Add a subdirectory spec. Do not duplicate "." */
10141 if (cp2
[-1] != '.' && cp2
[-1] != '[' && cp2
[-1] != '<')
10143 /* If this is ".." then the spec becomes "-" */
10144 if ((cp1
[0] == '.') && (cp1
[1] == '.') && (cp
[2] == '/')) {
10145 /* Add "-" and skip the ".." */
10150 /* Copy the subdirectory */
10151 while (*cp1
!= '/') *cp2
++= *cp1
++;
10152 cp1
++; /* Skip the "/" */
10154 /* Close the directory specification */
10155 if (cp2
[-1] == '.') /* no trailing periods */
10159 /* Now add the filename */
10160 while (*cp1
) *cp2
++ = *cp1
++;
10162 /* Now append it to the original VMS spec. */
10163 strcpy (cp
, Local
);
10165 /* If we put a [000000] in the filename, try to open it first. If this fails,
10166 remove the [000000], and return that name. This provides flexibility
10167 to the user in that they can use both rooted and non-rooted logical names
10168 to point to the location of the file. */
10170 if (check_filename_before_returning
) {
10171 f
= open (fname
, O_RDONLY
, 0666);
10173 /* The file name is OK as it is, so return it as is. */
10177 /* The filename did not work. Try to remove the [000000] from the name,
10179 cp
= index (fname
, '[');
10180 cp2
= index (fname
, ']') + 1;
10181 strcpy (cp
, cp2
); /* this gets rid of it */
10189 /* The following wrapper functions supply additional arguments to the VMS
10190 I/O routines to optimize performance with file handling. The arguments
10192 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
10193 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
10194 "fop=tef"- Truncate unused portions of file when closing file.
10195 "shr=nil"- Disallow file sharing while file is open. */
10198 freopen (fname
, type
, oldfile
)
10203 #undef freopen /* Get back the REAL fopen routine */
10204 if (strcmp (type
, "w") == 0)
10205 return freopen (fname
, type
, oldfile
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
10206 return freopen (fname
, type
, oldfile
, "mbc=16");
10210 fopen (fname
, type
)
10214 #undef fopen /* Get back the REAL fopen routine */
10215 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
10216 fixed arguments, which matches ANSI's specification but not VAXCRTL's
10217 pre-ANSI implementation. This hack circumvents the mismatch problem. */
10218 FILE *(*vmslib_fopen
)() = (FILE *(*)()) fopen
;
10221 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
10222 "deq=64", "fop=tef", "shr=nil");
10224 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
10228 open (fname
, flags
, prot
)
10233 #undef open /* Get back the REAL open routine */
10234 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
10237 /* more VMS hackery */
10241 extern unsigned long sys$
parse(), sys$
search();
10243 /* Work around another library bug. If a file is located via a searchlist,
10244 and if the device it's on is not the same device as the one specified
10245 in the first element of that searchlist, then both stat() and fstat()
10246 will fail to return info about it. `errno' will be set to EVMSERR, and
10247 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
10248 We can get around this by fully parsing the filename and then passing
10249 that absolute name to stat().
10251 Without this fix, we can end up failing to find header files, which is
10252 bad enough, but then compounding the problem by reporting the reason for
10253 failure as "normal successful completion." */
10255 #undef fstat /* get back to library version */
10258 VMS_fstat (fd
, statbuf
)
10260 struct stat
*statbuf
;
10262 int result
= fstat (fd
, statbuf
);
10267 char nambuf
[NAM$C_MAXRSS
+1];
10269 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
10270 result
= VMS_stat (nambuf
, statbuf
);
10271 /* No fclose(fp) here; that would close(fd) as well. */
10278 VMS_stat (name
, statbuf
)
10280 struct stat
*statbuf
;
10282 int result
= stat (name
, statbuf
);
10288 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for sys$parse */
10289 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for sys$search */
10292 fab
.fab$l_fna
= (char *) name
;
10293 fab
.fab$b_fns
= (unsigned char) strlen (name
);
10294 fab
.fab$l_nam
= (void *) &nam
;
10296 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
10297 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
10298 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
10299 if (sys$
parse (&fab
) & 1)
10301 if (sys$
search (&fab
) & 1)
10303 res_nam
[nam
.nam$b_rsl
] = '\0';
10304 result
= stat (res_nam
, statbuf
);
10306 /* Clean up searchlist context cached by the system. */
10307 nam
.nam$b_nop
= NAM$M_SYNCHK
;
10308 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
10309 (void) sys$
parse (&fab
);