1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-95, 1996 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"
47 #ifndef LOCAL_INCLUDE_DIR
48 #define LOCAL_INCLUDE_DIR "/usr/local/include"
53 /* By default, colon separates directories in a path. */
54 #ifndef PATH_SEPARATOR
55 #define PATH_SEPARATOR ':'
58 #include <sys/types.h>
64 /* The following symbols should be autoconfigured:
71 In the mean time, we'll get by with approximations based
72 on existing GCC configuration symbols. */
75 # ifndef HAVE_STDLIB_H
76 # define HAVE_STDLIB_H 1
78 # ifndef HAVE_UNISTD_H
79 # define HAVE_UNISTD_H 1
82 # define STDC_HEADERS 1
84 #endif /* defined (POSIX) */
86 #if defined (POSIX) || (defined (USG) && !defined (VMS))
88 # define HAVE_FCNTL_H 1
95 # if TIME_WITH_SYS_TIME
96 # include <sys/time.h>
100 # include <sys/time.h>
105 # include <sys/resource.h>
123 # define bcmp(a, b, n) memcmp (a, b, n)
126 # define bcopy(s, d, n) memcpy (d, s, n)
129 # define bzero(d, n) memset (d, 0, n)
131 #else /* !STDC_HEADERS */
135 # if !defined (BSTRING) && (defined (USG) || defined (VMS))
138 # define bcmp my_bcmp
151 # endif /* !defined (bcmp) */
154 # define bcopy my_bcopy
164 # endif /* !defined (bcopy) */
167 # define bzero my_bzero
171 register unsigned length
;
176 # endif /* !defined (bzero) */
178 # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
179 #endif /* ! STDC_HEADERS */
181 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6)
182 # define __attribute__(x)
186 # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
187 # define PROTO(ARGS) ARGS
189 # define PROTO(ARGS) ()
193 #if defined (__STDC__) && defined (HAVE_VPRINTF)
195 # define VA_START(va_list, var) va_start (va_list, var)
196 # define PRINTF_ALIST(msg) char *msg, ...
197 # define PRINTF_DCL(msg)
198 # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (printf, m, n)))
200 # include <varargs.h>
201 # define VA_START(va_list, var) va_start (va_list)
202 # define PRINTF_ALIST(msg) msg, va_alist
203 # define PRINTF_DCL(msg) char *msg; va_dcl
204 # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (printf, m, n)))
205 # define vfprintf(file, msg, args) \
207 char *a0 = va_arg(args, char *); \
208 char *a1 = va_arg(args, char *); \
209 char *a2 = va_arg(args, char *); \
210 char *a3 = va_arg(args, char *); \
211 fprintf (file, msg, a0, a1, a2, a3); \
215 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
216 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
217 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
223 /* VMS-specific definitions */
226 #define O_RDONLY 0 /* Open arg for Read/Only */
227 #define O_WRONLY 1 /* Open arg for Write/Only */
228 #define read(fd,buf,size) VMS_read (fd,buf,size)
229 #define write(fd,buf,size) VMS_write (fd,buf,size)
230 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
231 #define fopen(fname,mode) VMS_fopen (fname,mode)
232 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
233 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
234 static int VMS_fstat (), VMS_stat ();
235 static int VMS_read ();
236 static int VMS_write ();
237 static int VMS_open ();
238 static FILE * VMS_fopen ();
239 static FILE * VMS_freopen ();
240 static void hack_vms_include_specification ();
241 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
242 #define INO_T_HASH(a) 0
243 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
245 #define BSTRING /* VMS/GCC supplies the bstring routines */
246 #endif /* __GNUC__ */
255 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
256 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
258 /* Find the largest host integer type and set its size and type.
259 Don't blindly use `long'; on some crazy hosts it is shorter than `int'. */
261 #ifndef HOST_BITS_PER_WIDE_INT
263 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
264 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
265 #define HOST_WIDE_INT long
267 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
268 #define HOST_WIDE_INT int
274 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
278 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
282 #define INO_T_EQ(a, b) ((a) == (b))
286 #define INO_T_HASH(a) (a)
289 /* Define a generic NULL if one hasn't already been defined. */
296 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
297 #define GENERIC_PTR void *
299 #define GENERIC_PTR char *
304 #define NULL_PTR ((GENERIC_PTR)0)
307 #ifndef INCLUDE_LEN_FUDGE
308 #define INCLUDE_LEN_FUDGE 0
311 /* External declarations. */
313 extern char *version_string
;
315 #ifndef HAVE_STRERROR
318 extern const char *const sys_errlist
[];
320 extern char *sys_errlist
[];
322 #else /* HAVE_STRERROR */
326 char *strerror (int,...);
328 HOST_WIDE_INT parse_escape
PROTO((char **, HOST_WIDE_INT
));
329 HOST_WIDE_INT parse_c_expression
PROTO((char *));
335 /* Name under which this program was invoked. */
337 static char *progname
;
339 /* Nonzero means use extra default include directories for C++. */
341 static int cplusplus
;
343 /* Nonzero means handle cplusplus style comments */
345 static int cplusplus_comments
;
347 /* Nonzero means handle #import, for objective C. */
351 /* Nonzero means this is an assembly file, and allow
352 unknown directives, which could be comments. */
356 /* Current maximum length of directory names in the search path
357 for include files. (Altered as we get more of them.) */
359 static int max_include_len
;
361 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
363 static int for_lint
= 0;
365 /* Nonzero means copy comments into the output file. */
367 static int put_out_comments
= 0;
369 /* Nonzero means don't process the ANSI trigraph sequences. */
371 static int no_trigraphs
= 0;
373 /* Nonzero means print the names of included files rather than
374 the preprocessed output. 1 means just the #include "...",
375 2 means #include <...> as well. */
377 static int print_deps
= 0;
379 /* Nonzero if missing .h files in -M output are assumed to be generated
380 files and not errors. */
382 static int print_deps_missing_files
= 0;
384 /* Nonzero means print names of header files (-H). */
386 static int print_include_names
= 0;
388 /* Nonzero means don't output line number information. */
390 static int no_line_directives
;
392 /* Nonzero means output the text in failing conditionals,
393 inside #failed ... #endfailed. */
395 static int output_conditionals
;
397 /* dump_only means inhibit output of the preprocessed text
398 and instead output the definitions of all user-defined
399 macros in a form suitable for use as input to cccp.
400 dump_names means pass #define and the macro name through to output.
401 dump_definitions means pass the whole definition (plus #define) through
404 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
405 dump_macros
= dump_none
;
407 /* Nonzero means pass all #define and #undef directives which we actually
408 process through to the output stream. This feature is used primarily
409 to allow cc1 to record the #defines and #undefs for the sake of
410 debuggers which understand about preprocessor macros, but it may
411 also be useful with -E to figure out how symbols are defined, and
412 where they are defined. */
413 static int debug_output
= 0;
415 /* Nonzero indicates special processing used by the pcp program. The
416 special effects of this mode are:
418 Inhibit all macro expansion, except those inside #if directives.
420 Process #define directives normally, and output their contents
423 Output preconditions to pcp_outfile indicating all the relevant
424 preconditions for use of this file in a later cpp run.
426 static FILE *pcp_outfile
;
428 /* Nonzero means we are inside an IF during a -pcp run. In this mode
429 macro expansion is done, and preconditions are output for all macro
430 uses requiring them. */
431 static int pcp_inside_if
;
433 /* Nonzero means never to include precompiled files.
434 This is 1 since there's no way now to make precompiled files,
435 so it's not worth testing for them. */
436 static int no_precomp
= 1;
438 /* Nonzero means give all the error messages the ANSI standard requires. */
442 /* Nonzero means try to make failure to fit ANSI C an error. */
444 static int pedantic_errors
;
446 /* Nonzero means don't print warning messages. -w. */
448 static int inhibit_warnings
= 0;
450 /* Nonzero means warn if slash-star appears in a slash-star comment,
451 or if newline-backslash appears in a slash-slash comment. */
453 static int warn_comments
;
455 /* Nonzero means warn if a macro argument is (or would be)
456 stringified with -traditional. */
458 static int warn_stringify
;
460 /* Nonzero means warn if there are any trigraphs. */
462 static int warn_trigraphs
;
464 /* Nonzero means warn if #import is used. */
466 static int warn_import
= 1;
468 /* Nonzero means turn warnings into errors. */
470 static int warnings_are_errors
;
472 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
476 /* Nonzero causes output not to be done,
477 but directives such as #define that have side effects
480 static int no_output
;
482 /* Nonzero means this file was included with a -imacros or -include
483 command line and should not be recorded as an include file. */
485 static int no_record_file
;
487 /* Nonzero means that we have finished processing the command line options.
488 This flag is used to decide whether or not to issue certain errors
491 static int done_initializing
= 0;
493 /* Line where a newline was first seen in a string constant. */
495 static int multiline_string_line
= 0;
497 /* I/O buffer structure.
498 The `fname' field is nonzero for source files and #include files
499 and for the dummy text used for -D and -U.
500 It is zero for rescanning results of macro expansion
501 and for expanding macro arguments. */
502 #define INPUT_STACK_MAX 400
503 static struct file_buf
{
505 /* Filename specified with #line directive. */
507 /* Include file description. */
508 struct include_file
*inc
;
509 /* Record where in the search path this file was found.
510 For #include_next. */
511 struct file_name_list
*dir
;
516 /* Macro that this level is the expansion of.
517 Included so that we can reenable the macro
518 at the end of this level. */
519 struct hashnode
*macro
;
520 /* Value of if_stack at start of this file.
521 Used to prohibit unmatched #endif (etc) in an include file. */
522 struct if_stack
*if_stack
;
523 /* Object to be freed at end of input at this level. */
525 /* True if this is a header file included using <FILENAME>. */
526 char system_header_p
;
527 } instack
[INPUT_STACK_MAX
];
529 static int last_error_tick
; /* Incremented each time we print it. */
530 static int input_file_stack_tick
; /* Incremented when the status changes. */
532 /* Current nesting level of input sources.
533 `instack[indepth]' is the level currently being read. */
534 static int indepth
= -1;
535 #define CHECK_DEPTH(code) \
536 if (indepth >= (INPUT_STACK_MAX - 1)) \
538 error_with_line (line_for_error (instack[indepth].lineno), \
539 "macro or `#include' recursion too deep"); \
543 /* Current depth in #include directives that use <...>. */
544 static int system_include_depth
= 0;
546 typedef struct file_buf FILE_BUF
;
548 /* The output buffer. Its LENGTH field is the amount of room allocated
549 for the buffer, not the number of chars actually present. To get
550 that, subtract outbuf.buf from outbuf.bufp. */
552 #define OUTBUF_SIZE 10 /* initial size of output buffer */
553 static FILE_BUF outbuf
;
555 /* Grow output buffer OBUF points at
556 so it can hold at least NEEDED more chars. */
558 #define check_expand(OBUF, NEEDED) \
559 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
560 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
562 struct file_name_list
564 struct file_name_list
*next
;
565 /* If the following is 1, it is a C-language system include
567 int c_system_include_path
;
568 /* Mapping of file names for this directory. */
569 struct file_name_map
*name_map
;
570 /* Non-zero if name_map is valid. */
572 /* The include directory status. */
574 /* The include prefix: "" denotes the working directory,
575 otherwise fname must end in '/'.
576 The actual size is dynamically allocated. */
580 /* #include "file" looks in source file dir, then stack. */
581 /* #include <file> just looks in the stack. */
582 /* -I directories are added to the end, then the defaults are added. */
584 static struct default_include
{
585 char *fname
; /* The name of the directory. */
586 int cplusplus
; /* Only look here if we're compiling C++. */
587 int cxx_aware
; /* Includes in this directory don't need to
588 be wrapped in extern "C" when compiling
590 } include_defaults_array
[]
591 #ifdef INCLUDE_DEFAULTS
595 /* Pick up GNU C++ specific include files. */
596 { GPLUSPLUS_INCLUDE_DIR
, 1, 1 },
598 /* This is the dir for fixincludes. Put it just before
599 the files that we fix. */
600 { GCC_INCLUDE_DIR
, 0, 0 },
601 /* For cross-compilation, this dir name is generated
602 automatically in Makefile.in. */
603 { CROSS_INCLUDE_DIR
, 0, 0 },
604 /* This is another place that the target system's headers might be. */
605 { TOOL_INCLUDE_DIR
, 0, 0 },
606 #else /* not CROSS_COMPILE */
607 /* This should be /usr/local/include and should come before
608 the fixincludes-fixed header files. */
609 { LOCAL_INCLUDE_DIR
, 0, 1 },
610 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
611 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
612 { TOOL_INCLUDE_DIR
, 0, 0 },
613 /* This is the dir for fixincludes. Put it just before
614 the files that we fix. */
615 { GCC_INCLUDE_DIR
, 0, 0 },
616 /* Some systems have an extra dir of include files. */
617 #ifdef SYSTEM_INCLUDE_DIR
618 { SYSTEM_INCLUDE_DIR
, 0, 0 },
620 { STANDARD_INCLUDE_DIR
, 0, 0 },
621 #endif /* not CROSS_COMPILE */
624 #endif /* no INCLUDE_DEFAULTS */
626 /* The code looks at the defaults through this pointer, rather than through
627 the constant structure above. This pointer gets changed if an environment
628 variable specifies other defaults. */
629 static struct default_include
*include_defaults
= include_defaults_array
;
631 static struct file_name_list
*include
= 0; /* First dir to search */
632 /* First dir to search for <file> */
633 /* This is the first element to use for #include <...>.
634 If it is 0, use the entire chain for such includes. */
635 static struct file_name_list
*first_bracket_include
= 0;
636 /* This is the first element in the chain that corresponds to
637 a directory of system header files. */
638 static struct file_name_list
*first_system_include
= 0;
639 static struct file_name_list
*last_include
= 0; /* Last in chain */
641 /* Chain of include directories to put at the end of the other chain. */
642 static struct file_name_list
*after_include
= 0;
643 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
645 /* Chain to put at the start of the system include files. */
646 static struct file_name_list
*before_system
= 0;
647 static struct file_name_list
*last_before_system
= 0; /* Last in chain */
649 /* Directory prefix that should replace `/usr' in the standard
650 include file directories. */
651 static char *include_prefix
;
653 /* Maintain and search list of included files. */
655 struct include_file
{
656 struct include_file
*next
; /* for include_hashtab */
657 struct include_file
*next_ino
; /* for include_ino_hashtab */
659 /* If the following is the empty string, it means #pragma once
660 was seen in this include file, or #import was applied to the file.
661 Otherwise, if it is nonzero, it is a macro name.
662 Don't include the file again if that macro is defined. */
663 U_CHAR
*control_macro
;
664 /* Nonzero if the dependency on this include file has been output. */
669 /* Hash tables of files already included with #include or #import.
670 include_hashtab is by full name; include_ino_hashtab is by inode number. */
672 #define INCLUDE_HASHSIZE 61
673 static struct include_file
*include_hashtab
[INCLUDE_HASHSIZE
];
674 static struct include_file
*include_ino_hashtab
[INCLUDE_HASHSIZE
];
676 /* Global list of strings read in from precompiled files. This list
677 is kept in the order the strings are read in, with new strings being
678 added at the end through stringlist_tailp. We use this list to output
679 the strings at the end of the run.
681 static STRINGDEF
*stringlist
;
682 static STRINGDEF
**stringlist_tailp
= &stringlist
;
685 /* Structure returned by create_definition */
686 typedef struct macrodef MACRODEF
;
689 struct definition
*defn
;
694 enum sharp_token_type
{
695 NO_SHARP_TOKEN
= 0, /* token not present */
697 SHARP_TOKEN
= '#', /* token spelled with # only */
698 WHITE_SHARP_TOKEN
, /* token spelled with # and white space */
700 PERCENT_COLON_TOKEN
= '%', /* token spelled with %: only */
701 WHITE_PERCENT_COLON_TOKEN
/* token spelled with %: and white space */
704 /* Structure allocated for every #define. For a simple replacement
707 nargs = -1, the `pattern' list is null, and the expansion is just
708 the replacement text. Nargs = 0 means a functionlike macro with no args,
710 #define getchar() getc (stdin) .
711 When there are args, the expansion is the replacement text with the
712 args squashed out, and the reflist is a list describing how to
713 build the output from the input: e.g., "3 chars, then the 1st arg,
714 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
715 The chars here come from the expansion. Whatever is left of the
716 expansion after the last arg-occurrence is copied after that arg.
717 Note that the reflist can be arbitrarily long---
718 its length depends on the number of times the arguments appear in
719 the replacement text, not how many args there are. Example:
720 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
722 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
723 where (x, y) means (nchars, argno). */
725 typedef struct definition DEFINITION
;
728 int length
; /* length of expansion string */
729 int predefined
; /* True if the macro was builtin or */
730 /* came from the command line */
732 int line
; /* Line number of definition */
733 char *file
; /* File of definition */
734 char rest_args
; /* Nonzero if last arg. absorbs the rest */
736 struct reflist
*next
;
738 enum sharp_token_type stringify
; /* set if a # operator before arg */
739 enum sharp_token_type raw_before
; /* set if a ## operator before arg */
740 enum sharp_token_type raw_after
; /* set if a ## operator after arg */
742 char rest_args
; /* Nonzero if this arg. absorbs the rest */
743 int nchars
; /* Number of literal chars to copy before
744 this arg occurrence. */
745 int argno
; /* Number of arg to substitute (origin-0) */
748 /* Names of macro args, concatenated in reverse order
749 with comma-space between them.
750 The only use of this is that we warn on redefinition
751 if this differs between the old and new definitions. */
756 /* different kinds of things that can appear in the value field
757 of a hash node. Actually, this may be useless now. */
765 * special extension string that can be added to the last macro argument to
766 * allow it to absorb the "rest" of the arguments when expanded. Ex:
767 * #define wow(a, b...) process (b, a, b)
768 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
769 * { wow (one, two); } -> { process (two, one, two); }
770 * if this "rest_arg" is used with the concat token '##' and if it is not
771 * supplied then the token attached to with ## will not be outputted. Ex:
772 * #define wow (a, b...) process (b ## , a, ## b)
773 * { wow (1, 2); } -> { process (2, 1, 2); }
774 * { wow (one); } -> { process (one); {
776 static char rest_extension
[] = "...";
777 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
779 /* The structure of a node in the hash table. The hash table
780 has entries for all tokens defined by #define directives (type T_MACRO),
781 plus some special tokens like __LINE__ (these each have their own
782 type, and the appropriate code is run when that type of node is seen.
783 It does not contain control words like "#define", which are recognized
784 by a separate piece of code. */
786 /* different flavors of hash nodes --- also used in keyword table */
788 T_DEFINE
= 1, /* the `#define' keyword */
789 T_INCLUDE
, /* the `#include' keyword */
790 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
791 T_IMPORT
, /* the `#import' keyword */
792 T_IFDEF
, /* the `#ifdef' keyword */
793 T_IFNDEF
, /* the `#ifndef' keyword */
794 T_IF
, /* the `#if' keyword */
795 T_ELSE
, /* `#else' */
796 T_PRAGMA
, /* `#pragma' */
797 T_ELIF
, /* `#elif' */
798 T_UNDEF
, /* `#undef' */
799 T_LINE
, /* `#line' */
800 T_ERROR
, /* `#error' */
801 T_WARNING
, /* `#warning' */
802 T_ENDIF
, /* `#endif' */
803 T_SCCS
, /* `#sccs', used on system V. */
804 T_IDENT
, /* `#ident', used on system V. */
805 T_ASSERT
, /* `#assert', taken from system V. */
806 T_UNASSERT
, /* `#unassert', taken from system V. */
807 T_SPECLINE
, /* special symbol `__LINE__' */
808 T_DATE
, /* `__DATE__' */
809 T_FILE
, /* `__FILE__' */
810 T_BASE_FILE
, /* `__BASE_FILE__' */
811 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
812 T_VERSION
, /* `__VERSION__' */
813 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
814 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
815 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
816 T_USER_LABEL_PREFIX_TYPE
, /* `__USER_LABEL_PREFIX__' */
817 T_REGISTER_PREFIX_TYPE
, /* `__REGISTER_PREFIX__' */
818 T_IMMEDIATE_PREFIX_TYPE
, /* `__IMMEDIATE_PREFIX__' */
819 T_TIME
, /* `__TIME__' */
820 T_CONST
, /* Constant value, used by `__STDC__' */
821 T_MACRO
, /* macro defined by `#define' */
822 T_DISABLED
, /* macro temporarily turned off for rescan */
823 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
824 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
825 T_UNUSED
/* Used for something not defined. */
829 struct hashnode
*next
; /* double links for easy deletion */
830 struct hashnode
*prev
;
831 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
832 chain is kept, in case the node is the head
833 of the chain and gets deleted. */
834 enum node_type type
; /* type of special token */
835 int length
; /* length of token, for quick comparison */
836 U_CHAR
*name
; /* the actual name */
837 union hashval value
; /* pointer to expansion, or whatever */
840 typedef struct hashnode HASHNODE
;
842 /* Some definitions for the hash table. The hash function MUST be
843 computed as shown in hashf () below. That is because the rescan
844 loop computes the hash value `on the fly' for most tokens,
845 in order to avoid the overhead of a lot of procedure calls to
846 the hashf () function. Hashf () only exists for the sake of
847 politeness, for use when speed isn't so important. */
849 #define HASHSIZE 1403
850 static HASHNODE
*hashtab
[HASHSIZE
];
851 #define HASHSTEP(old, c) ((old << 2) + c)
852 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
854 /* Symbols to predefine. */
856 #ifdef CPP_PREDEFINES
857 static char *predefs
= CPP_PREDEFINES
;
859 static char *predefs
= "";
862 /* We let tm.h override the types used here, to handle trivial differences
863 such as the choice of unsigned int or long unsigned int for size_t.
864 When machines start needing nontrivial differences in the size type,
865 it would be best to do something here to figure out automatically
866 from other information what type to use. */
868 /* The string value for __SIZE_TYPE__. */
871 #define SIZE_TYPE "long unsigned int"
874 /* The string value for __PTRDIFF_TYPE__. */
877 #define PTRDIFF_TYPE "long int"
880 /* The string value for __WCHAR_TYPE__. */
883 #define WCHAR_TYPE "int"
885 char * wchar_type
= WCHAR_TYPE
;
888 /* The string value for __USER_LABEL_PREFIX__ */
890 #ifndef USER_LABEL_PREFIX
891 #define USER_LABEL_PREFIX ""
894 /* The string value for __REGISTER_PREFIX__ */
896 #ifndef REGISTER_PREFIX
897 #define REGISTER_PREFIX ""
900 /* The string value for __IMMEDIATE_PREFIX__ */
902 #ifndef IMMEDIATE_PREFIX
903 #define IMMEDIATE_PREFIX ""
906 /* In the definition of a #assert name, this structure forms
907 a list of the individual values asserted.
908 Each value is itself a list of "tokens".
909 These are strings that are compared by name. */
911 struct tokenlist_list
{
912 struct tokenlist_list
*next
;
913 struct arglist
*tokens
;
916 struct assertion_hashnode
{
917 struct assertion_hashnode
*next
; /* double links for easy deletion */
918 struct assertion_hashnode
*prev
;
919 /* also, a back pointer to this node's hash
920 chain is kept, in case the node is the head
921 of the chain and gets deleted. */
922 struct assertion_hashnode
**bucket_hdr
;
923 int length
; /* length of token, for quick comparison */
924 U_CHAR
*name
; /* the actual name */
925 /* List of token-sequences. */
926 struct tokenlist_list
*value
;
929 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
931 /* Some definitions for the hash table. The hash function MUST be
932 computed as shown in hashf below. That is because the rescan
933 loop computes the hash value `on the fly' for most tokens,
934 in order to avoid the overhead of a lot of procedure calls to
935 the hashf function. hashf only exists for the sake of
936 politeness, for use when speed isn't so important. */
938 #define ASSERTION_HASHSIZE 37
939 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
941 /* Nonzero means inhibit macroexpansion of what seem to be
942 assertion tests, in rescan. For #if. */
943 static int assertions_flag
;
945 /* `struct directive' defines one #-directive, including how to handle it. */
947 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
950 int length
; /* Length of name */
951 int (*func
) DO_PROTO
; /* Function to handle directive */
952 char *name
; /* Name of directive */
953 enum node_type type
; /* Code which describes which directive. */
954 char angle_brackets
; /* Nonzero => <...> is special. */
955 char traditional_comments
; /* Nonzero: keep comments if -traditional. */
956 char pass_thru
; /* Copy preprocessed directive to output file. */
959 /* These functions are declared to return int instead of void since they
960 are going to be placed in the table and some old compilers have trouble with
961 pointers to functions returning void. */
963 static int do_assert DO_PROTO
;
964 static int do_define DO_PROTO
;
965 static int do_elif DO_PROTO
;
966 static int do_else DO_PROTO
;
967 static int do_endif DO_PROTO
;
968 static int do_error DO_PROTO
;
969 static int do_ident DO_PROTO
;
970 static int do_if DO_PROTO
;
971 static int do_include DO_PROTO
;
972 static int do_line DO_PROTO
;
973 static int do_pragma DO_PROTO
;
974 #ifdef SCCS_DIRECTIVE
975 static int do_sccs DO_PROTO
;
977 static int do_unassert DO_PROTO
;
978 static int do_undef DO_PROTO
;
979 static int do_warning DO_PROTO
;
980 static int do_xifdef DO_PROTO
;
982 /* Here is the actual list of #-directives, most-often-used first. */
984 static struct directive directive_table
[] = {
985 { 6, do_define
, "define", T_DEFINE
, 0, 1},
986 { 2, do_if
, "if", T_IF
},
987 { 5, do_xifdef
, "ifdef", T_IFDEF
},
988 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
989 { 5, do_endif
, "endif", T_ENDIF
},
990 { 4, do_else
, "else", T_ELSE
},
991 { 4, do_elif
, "elif", T_ELIF
},
992 { 4, do_line
, "line", T_LINE
},
993 { 7, do_include
, "include", T_INCLUDE
, 1},
994 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
995 { 6, do_include
, "import", T_IMPORT
, 1},
996 { 5, do_undef
, "undef", T_UNDEF
},
997 { 5, do_error
, "error", T_ERROR
},
998 { 7, do_warning
, "warning", T_WARNING
},
999 #ifdef SCCS_DIRECTIVE
1000 { 4, do_sccs
, "sccs", T_SCCS
},
1002 { 6, do_pragma
, "pragma", T_PRAGMA
, 0, 0, 1},
1003 { 5, do_ident
, "ident", T_IDENT
},
1004 { 6, do_assert
, "assert", T_ASSERT
},
1005 { 8, do_unassert
, "unassert", T_UNASSERT
},
1006 { -1, 0, "", T_UNUSED
},
1009 /* When a directive handler is called,
1010 this points to the # (or the : of the %:) that started the directive. */
1011 U_CHAR
*directive_start
;
1013 /* table to tell if char can be part of a C identifier. */
1014 U_CHAR is_idchar
[256];
1015 /* table to tell if char can be first char of a c identifier. */
1016 U_CHAR is_idstart
[256];
1017 /* table to tell if c is horizontal space. */
1018 U_CHAR is_hor_space
[256];
1019 /* table to tell if c is horizontal or vertical space. */
1020 static U_CHAR is_space
[256];
1021 /* names of some characters */
1022 static char *char_name
[256];
1024 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
1025 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
1027 static int errors
= 0; /* Error counter for exit code */
1029 /* Name of output file, for error messages. */
1030 static char *out_fname
;
1032 /* Zero means dollar signs are punctuation.
1033 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
1034 This must be 0 for correct processing of this ANSI C program:
1036 #define lose(b) foo (b)
1039 static int dollars_in_ident
;
1040 #ifndef DOLLARS_IN_IDENTIFIERS
1041 #define DOLLARS_IN_IDENTIFIERS 1
1045 /* Stack of conditionals currently in progress
1046 (including both successful and failing conditionals). */
1049 struct if_stack
*next
; /* for chaining to the next stack frame */
1050 char *fname
; /* copied from input when frame is made */
1051 int lineno
; /* similarly */
1052 int if_succeeded
; /* true if a leg of this if-group
1053 has been passed through rescan */
1054 U_CHAR
*control_macro
; /* For #ifndef at start of file,
1055 this is the macro name tested. */
1056 enum node_type type
; /* type of last directive seen in this group */
1058 typedef struct if_stack IF_STACK_FRAME
;
1059 static IF_STACK_FRAME
*if_stack
= NULL
;
1061 /* Buffer of -M output. */
1062 static char *deps_buffer
;
1064 /* Number of bytes allocated in above. */
1065 static int deps_allocated_size
;
1067 /* Number of bytes used. */
1068 static int deps_size
;
1070 /* Number of bytes since the last newline. */
1071 static int deps_column
;
1073 /* Nonzero means -I- has been seen,
1074 so don't look for #include "foo" the source-file directory. */
1075 static int ignore_srcdir
;
1077 static int safe_read
PROTO((int, char *, int));
1078 static void safe_write
PROTO((int, char *, int));
1080 int main
PROTO((int, char **));
1082 static void path_include
PROTO((char *));
1084 static U_CHAR
*index0
PROTO((U_CHAR
*, int, size_t));
1086 static void trigraph_pcp
PROTO((FILE_BUF
*));
1088 static void newline_fix
PROTO((U_CHAR
*));
1089 static void name_newline_fix
PROTO((U_CHAR
*));
1091 static char *get_lintcmd
PROTO((U_CHAR
*, U_CHAR
*, U_CHAR
**, int *, int *));
1093 static void rescan
PROTO((FILE_BUF
*, int));
1095 static FILE_BUF expand_to_temp_buffer
PROTO((U_CHAR
*, U_CHAR
*, int, int));
1097 static int handle_directive
PROTO((FILE_BUF
*, FILE_BUF
*));
1099 static struct tm
*timestamp
PROTO((void));
1100 static void special_symbol
PROTO((HASHNODE
*, FILE_BUF
*));
1102 static int is_system_include
PROTO((char *));
1103 static char *base_name
PROTO((char *));
1104 static int absolute_filename
PROTO((char *));
1105 static size_t simplify_filename
PROTO((char *));
1107 static char *read_filename_string
PROTO((int, FILE *));
1108 static struct file_name_map
*read_name_map
PROTO((char *));
1109 static int open_include_file
PROTO((char *, struct file_name_list
*, U_CHAR
*, struct include_file
**));
1110 static char *remap_include_file
PROTO((char *, struct file_name_list
*));
1111 static int lookup_ino_include
PROTO((struct include_file
*));
1113 static void finclude
PROTO((int, struct include_file
*, FILE_BUF
*, int, struct file_name_list
*));
1114 static void record_control_macro
PROTO((struct include_file
*, U_CHAR
*));
1116 static char *check_precompiled
PROTO((int, struct stat
*, char *, char **));
1117 static int check_preconditions
PROTO((char *));
1118 static void pcfinclude
PROTO((U_CHAR
*, U_CHAR
*, U_CHAR
*, FILE_BUF
*));
1119 static void pcstring_used
PROTO((HASHNODE
*));
1120 static void write_output
PROTO((void));
1121 static void pass_thru_directive
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
1123 static MACRODEF create_definition
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
1125 static int check_macro_name
PROTO((U_CHAR
*, char *));
1126 static int compare_defs
PROTO((DEFINITION
*, DEFINITION
*));
1127 static int comp_def_part
PROTO((int, U_CHAR
*, int, U_CHAR
*, int, int));
1129 static DEFINITION
*collect_expansion
PROTO((U_CHAR
*, U_CHAR
*, int, struct arglist
*));
1131 int check_assertion
PROTO((U_CHAR
*, int, int, struct arglist
*));
1132 static int compare_token_lists
PROTO((struct arglist
*, struct arglist
*));
1134 static struct arglist
*read_token_list
PROTO((U_CHAR
**, U_CHAR
*, int *));
1135 static void free_token_list
PROTO((struct arglist
*));
1137 static ASSERTION_HASHNODE
*assertion_install
PROTO((U_CHAR
*, int, int));
1138 static ASSERTION_HASHNODE
*assertion_lookup
PROTO((U_CHAR
*, int, int));
1139 static void delete_assertion
PROTO((ASSERTION_HASHNODE
*));
1141 static void do_once
PROTO((void));
1143 static HOST_WIDE_INT eval_if_expression
PROTO((U_CHAR
*, int));
1144 static void conditional_skip
PROTO((FILE_BUF
*, int, enum node_type
, U_CHAR
*, FILE_BUF
*));
1145 static void skip_if_group
PROTO((FILE_BUF
*, int, FILE_BUF
*));
1146 static void validate_else
PROTO((U_CHAR
*, U_CHAR
*));
1148 static U_CHAR
*skip_to_end_of_comment
PROTO((FILE_BUF
*, int *, int));
1149 static U_CHAR
*skip_quoted_string
PROTO((U_CHAR
*, U_CHAR
*, int, int *, int *, int *));
1150 static char *quote_string
PROTO((char *, char *));
1151 static U_CHAR
*skip_paren_group
PROTO((FILE_BUF
*));
1153 /* Last arg to output_line_directive. */
1154 enum file_change_code
{same_file
, enter_file
, leave_file
};
1155 static void output_line_directive
PROTO((FILE_BUF
*, FILE_BUF
*, int, enum file_change_code
));
1157 static void macroexpand
PROTO((HASHNODE
*, FILE_BUF
*));
1160 static char *macarg
PROTO((struct argdata
*, int));
1162 static U_CHAR
*macarg1
PROTO((U_CHAR
*, U_CHAR
*, int *, int *, int *, int));
1164 static int discard_comments
PROTO((U_CHAR
*, int, int));
1166 static int change_newlines
PROTO((U_CHAR
*, int));
1168 char *my_strerror
PROTO((int));
1169 void error
PRINTF_PROTO_1((char *, ...));
1170 static void verror
PROTO((char *, va_list));
1171 static void error_from_errno
PROTO((char *));
1172 void warning
PRINTF_PROTO_1((char *, ...));
1173 static void vwarning
PROTO((char *, va_list));
1174 static void error_with_line
PRINTF_PROTO_2((int, char *, ...));
1175 static void verror_with_line
PROTO((int, char *, va_list));
1176 static void vwarning_with_line
PROTO((int, char *, va_list));
1177 static void warning_with_line
PRINTF_PROTO_2((int, char *, ...));
1178 void pedwarn
PRINTF_PROTO_1((char *, ...));
1179 void pedwarn_with_line
PRINTF_PROTO_2((int, char *, ...));
1180 static void pedwarn_with_file_and_line
PRINTF_PROTO_3((char *, int, char *, ...));
1182 static void print_containing_files
PROTO((void));
1184 static int line_for_error
PROTO((int));
1185 static int grow_outbuf
PROTO((FILE_BUF
*, int));
1187 static HASHNODE
*install
PROTO((U_CHAR
*, int, enum node_type
, char *, int));
1188 HASHNODE
*lookup
PROTO((U_CHAR
*, int, int));
1189 static void delete_macro
PROTO((HASHNODE
*));
1190 static int hashf
PROTO((U_CHAR
*, int, int));
1192 static void dump_single_macro
PROTO((HASHNODE
*, FILE *));
1193 static void dump_all_macros
PROTO((void));
1194 static void dump_defn_1
PROTO((U_CHAR
*, int, int, FILE *));
1195 static void dump_arg_n
PROTO((DEFINITION
*, int, FILE *));
1197 static void initialize_char_syntax
PROTO((void));
1198 static void initialize_builtins
PROTO((FILE_BUF
*, FILE_BUF
*));
1200 static void make_definition
PROTO((char *, FILE_BUF
*));
1201 static void make_undef
PROTO((char *, FILE_BUF
*));
1203 static void make_assertion
PROTO((char *, char *));
1205 static struct file_name_list
*new_include_prefix
PROTO((struct file_name_list
*, char *, char *));
1206 static void append_include_chain
PROTO((struct file_name_list
*, struct file_name_list
*));
1208 static void deps_output
PROTO((char *, int));
1210 static void fatal
PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn
));
1211 void fancy_abort
PROTO((void)) __attribute__ ((noreturn
));
1212 static void perror_with_name
PROTO((char *));
1213 static void pfatal_with_name
PROTO((char *)) __attribute__ ((noreturn
));
1214 static void pipe_closed
PROTO((int)) __attribute__ ((noreturn
));
1216 static void memory_full
PROTO((void)) __attribute__ ((noreturn
));
1217 GENERIC_PTR xmalloc
PROTO((size_t));
1218 static GENERIC_PTR xrealloc
PROTO((GENERIC_PTR
, size_t));
1219 static GENERIC_PTR xcalloc
PROTO((size_t, size_t));
1220 static char *savestring
PROTO((char *));
1222 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1223 retrying if necessary. Return a negative value if an error occurs,
1224 otherwise return the actual number of bytes read,
1225 which must be LEN unless end-of-file was reached. */
1228 safe_read (desc
, ptr
, len
)
1235 int nchars
= read (desc
, ptr
, left
);
1252 /* Write LEN bytes at PTR to descriptor DESC,
1253 retrying if necessary, and treating any real error as fatal. */
1256 safe_write (desc
, ptr
, len
)
1262 int written
= write (desc
, ptr
, len
);
1269 pfatal_with_name (out_fname
);
1286 char **pend_files
= (char **) xmalloc (argc
* sizeof (char *));
1287 char **pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
1288 char **pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
1289 char **pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
1290 char **pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
1292 /* Record the option used with each element of pend_assertions.
1293 This is preparation for supporting more than one option for making
1295 char **pend_assertion_options
= (char **) xmalloc (argc
* sizeof (char *));
1296 int inhibit_predefs
= 0;
1297 int no_standard_includes
= 0;
1298 int no_standard_cplusplus_includes
= 0;
1299 int missing_newline
= 0;
1301 /* Non-0 means don't output the preprocessed program. */
1302 int inhibit_output
= 0;
1303 /* Non-0 means -v, so print the full set of include dirs. */
1306 /* File name which deps are being written to.
1307 This is 0 if deps are being written to stdout. */
1308 char *deps_file
= 0;
1309 /* Fopen file mode to open deps_file with. */
1310 char *deps_mode
= "a";
1311 /* Stream on which to print the dependency information. */
1312 FILE *deps_stream
= 0;
1313 /* Target-name to write with the dependency information. */
1314 char *deps_target
= 0;
1317 /* Get rid of any avoidable limit on stack size. */
1321 /* Set the stack limit huge so that alloca (particularly stringtab
1322 * in dbxread.c) does not fail. */
1323 getrlimit (RLIMIT_STACK
, &rlim
);
1324 rlim
.rlim_cur
= rlim
.rlim_max
;
1325 setrlimit (RLIMIT_STACK
, &rlim
);
1327 #endif /* RLIMIT_STACK defined */
1330 signal (SIGPIPE
, pipe_closed
);
1333 progname
= base_name (argv
[0]);
1337 /* Remove extension from PROGNAME. */
1339 char *s
= progname
= savestring (progname
);
1341 if ((p
= rindex (s
, ';')) != 0) *p
= '\0'; /* strip version number */
1342 if ((p
= rindex (s
, '.')) != 0 /* strip type iff ".exe" */
1343 && (p
[1] == 'e' || p
[1] == 'E')
1344 && (p
[2] == 'x' || p
[2] == 'X')
1345 && (p
[3] == 'e' || p
[3] == 'E')
1354 /* Initialize is_idchar to allow $. */
1355 dollars_in_ident
= 1;
1356 initialize_char_syntax ();
1357 dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
> 0;
1359 no_line_directives
= 0;
1361 dump_macros
= dump_none
;
1364 cplusplus_comments
= 1;
1366 bzero ((char *) pend_files
, argc
* sizeof (char *));
1367 bzero ((char *) pend_defs
, argc
* sizeof (char *));
1368 bzero ((char *) pend_undefs
, argc
* sizeof (char *));
1369 bzero ((char *) pend_assertions
, argc
* sizeof (char *));
1370 bzero ((char *) pend_includes
, argc
* sizeof (char *));
1372 /* Process switches and find input file name. */
1374 for (i
= 1; i
< argc
; i
++) {
1375 if (argv
[i
][0] != '-') {
1376 if (out_fname
!= NULL
)
1377 fatal ("Usage: %s [switches] input output", argv
[0]);
1378 else if (in_fname
!= NULL
)
1379 out_fname
= argv
[i
];
1383 switch (argv
[i
][1]) {
1386 if (!strcmp (argv
[i
], "-include")) {
1388 fatal ("Filename missing after `-include' option");
1390 simplify_filename (pend_includes
[i
] = argv
[++i
]);
1392 if (!strcmp (argv
[i
], "-imacros")) {
1394 fatal ("Filename missing after `-imacros' option");
1396 simplify_filename (pend_files
[i
] = argv
[++i
]);
1398 if (!strcmp (argv
[i
], "-iprefix")) {
1400 fatal ("Filename missing after `-iprefix' option");
1402 include_prefix
= argv
[++i
];
1404 if (!strcmp (argv
[i
], "-ifoutput")) {
1405 output_conditionals
= 1;
1407 if (!strcmp (argv
[i
], "-isystem")) {
1408 struct file_name_list
*dirtmp
;
1410 if (! (dirtmp
= new_include_prefix (NULL_PTR
, "", argv
[++i
])))
1412 dirtmp
->c_system_include_path
= 1;
1414 if (before_system
== 0)
1415 before_system
= dirtmp
;
1417 last_before_system
->next
= dirtmp
;
1418 last_before_system
= dirtmp
; /* Tail follows the last one */
1420 /* Add directory to end of path for includes,
1421 with the default prefix at the front of its name. */
1422 if (!strcmp (argv
[i
], "-iwithprefix")) {
1423 struct file_name_list
*dirtmp
;
1426 if (include_prefix
!= 0)
1427 prefix
= include_prefix
;
1429 prefix
= savestring (GCC_INCLUDE_DIR
);
1430 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1431 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1432 prefix
[strlen (prefix
) - 7] = 0;
1435 if (! (dirtmp
= new_include_prefix (NULL_PTR
, prefix
, argv
[++i
])))
1438 if (after_include
== 0)
1439 after_include
= dirtmp
;
1441 last_after_include
->next
= dirtmp
;
1442 last_after_include
= dirtmp
; /* Tail follows the last one */
1444 /* Add directory to main path for includes,
1445 with the default prefix at the front of its name. */
1446 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
1447 struct file_name_list
*dirtmp
;
1450 if (include_prefix
!= 0)
1451 prefix
= include_prefix
;
1453 prefix
= savestring (GCC_INCLUDE_DIR
);
1454 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1455 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1456 prefix
[strlen (prefix
) - 7] = 0;
1459 dirtmp
= new_include_prefix (NULL_PTR
, prefix
, argv
[++i
]);
1460 append_include_chain (dirtmp
, dirtmp
);
1462 /* Add directory to end of path for includes. */
1463 if (!strcmp (argv
[i
], "-idirafter")) {
1464 struct file_name_list
*dirtmp
;
1466 if (! (dirtmp
= new_include_prefix (NULL_PTR
, "", argv
[++i
])))
1469 if (after_include
== 0)
1470 after_include
= dirtmp
;
1472 last_after_include
->next
= dirtmp
;
1473 last_after_include
= dirtmp
; /* Tail follows the last one */
1478 if (out_fname
!= NULL
)
1479 fatal ("Output filename specified twice");
1481 fatal ("Filename missing after -o option");
1482 out_fname
= argv
[++i
];
1483 if (!strcmp (out_fname
, "-"))
1488 if (!strcmp (argv
[i
], "-pedantic"))
1490 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1492 pedantic_errors
= 1;
1493 } else if (!strcmp (argv
[i
], "-pcp")) {
1496 fatal ("Filename missing after -pcp option");
1497 pcp_fname
= argv
[++i
];
1499 ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1500 ? fopen (pcp_fname
, "w")
1502 if (pcp_outfile
== 0)
1503 pfatal_with_name (pcp_fname
);
1509 if (!strcmp (argv
[i
], "-traditional")) {
1511 cplusplus_comments
= 0;
1512 if (dollars_in_ident
> 0)
1513 dollars_in_ident
= 1;
1514 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1520 if (! strcmp (argv
[i
], "-lang-c"))
1521 cplusplus
= 0, cplusplus_comments
= 1, objc
= 0;
1522 if (! strcmp (argv
[i
], "-lang-c89"))
1523 cplusplus
= 0, cplusplus_comments
= 0, objc
= 0;
1524 if (! strcmp (argv
[i
], "-lang-c++"))
1525 cplusplus
= 1, cplusplus_comments
= 1, objc
= 0;
1526 if (! strcmp (argv
[i
], "-lang-objc"))
1527 objc
= 1, cplusplus
= 0, cplusplus_comments
= 1;
1528 if (! strcmp (argv
[i
], "-lang-objc++"))
1529 objc
= 1, cplusplus
= 1, cplusplus_comments
= 1;
1530 if (! strcmp (argv
[i
], "-lang-asm"))
1532 if (! strcmp (argv
[i
], "-lint"))
1537 cplusplus
= 1, cplusplus_comments
= 1;
1541 inhibit_warnings
= 1;
1545 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1547 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1549 else if (!strcmp (argv
[i
], "-Wcomment"))
1551 else if (!strcmp (argv
[i
], "-Wno-comment"))
1553 else if (!strcmp (argv
[i
], "-Wcomments"))
1555 else if (!strcmp (argv
[i
], "-Wno-comments"))
1557 else if (!strcmp (argv
[i
], "-Wtraditional"))
1559 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1561 else if (!strcmp (argv
[i
], "-Wimport"))
1563 else if (!strcmp (argv
[i
], "-Wno-import"))
1565 else if (!strcmp (argv
[i
], "-Werror"))
1566 warnings_are_errors
= 1;
1567 else if (!strcmp (argv
[i
], "-Wno-error"))
1568 warnings_are_errors
= 0;
1569 else if (!strcmp (argv
[i
], "-Wall"))
1577 /* The style of the choices here is a bit mixed.
1578 The chosen scheme is a hybrid of keeping all options in one string
1579 and specifying each option in a separate argument:
1580 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1581 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1582 -M[M][G][D file]. This is awkward to handle in specs, and is not
1584 /* ??? -MG must be specified in addition to one of -M or -MM.
1585 This can be relaxed in the future without breaking anything.
1586 The converse isn't true. */
1588 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1589 if (!strcmp (argv
[i
], "-MG"))
1591 print_deps_missing_files
= 1;
1594 if (!strcmp (argv
[i
], "-M"))
1596 else if (!strcmp (argv
[i
], "-MM"))
1598 else if (!strcmp (argv
[i
], "-MD"))
1600 else if (!strcmp (argv
[i
], "-MMD"))
1602 /* For -MD and -MMD options, write deps on file named by next arg. */
1603 if (!strcmp (argv
[i
], "-MD")
1604 || !strcmp (argv
[i
], "-MMD")) {
1606 fatal ("Filename missing after %s option", argv
[i
]);
1608 deps_file
= argv
[i
];
1611 /* For -M and -MM, write deps on standard output
1612 and suppress the usual output. */
1613 deps_stream
= stdout
;
1620 char *p
= argv
[i
] + 2;
1622 while ((c
= *p
++)) {
1623 /* Arg to -d specifies what parts of macros to dump */
1626 dump_macros
= dump_only
;
1630 dump_macros
= dump_names
;
1633 dump_macros
= dump_definitions
;
1641 if (argv
[i
][2] == '3')
1646 fprintf (stderr
, "GNU CPP version %s", version_string
);
1647 #ifdef TARGET_VERSION
1650 fprintf (stderr
, "\n");
1655 print_include_names
= 1;
1659 if (argv
[i
][2] != 0)
1660 pend_defs
[i
] = argv
[i
] + 2;
1661 else if (i
+ 1 == argc
)
1662 fatal ("Macro name missing after -D option");
1664 i
++, pend_defs
[i
] = argv
[i
];
1671 if (argv
[i
][2] != 0)
1673 else if (i
+ 1 == argc
)
1674 fatal ("Assertion missing after -A option");
1678 if (!strcmp (p
, "-")) {
1679 /* -A- eliminates all predefined macros and assertions.
1680 Let's include also any that were specified earlier
1681 on the command line. That way we can get rid of any
1682 that were passed automatically in from GCC. */
1684 inhibit_predefs
= 1;
1685 for (j
= 0; j
< i
; j
++)
1686 pend_defs
[j
] = pend_assertions
[j
] = 0;
1688 pend_assertions
[i
] = p
;
1689 pend_assertion_options
[i
] = "-A";
1694 case 'U': /* JF #undef something */
1695 if (argv
[i
][2] != 0)
1696 pend_undefs
[i
] = argv
[i
] + 2;
1697 else if (i
+ 1 == argc
)
1698 fatal ("Macro name missing after -U option");
1700 pend_undefs
[i
] = argv
[i
+1], i
++;
1704 put_out_comments
= 1;
1707 case 'E': /* -E comes from cc -E; ignore it. */
1711 no_line_directives
= 1;
1714 case '$': /* Don't include $ in identifiers. */
1715 dollars_in_ident
= 0;
1718 case 'I': /* Add directory to path for includes. */
1720 struct file_name_list
*dirtmp
;
1722 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-")) {
1724 /* Don't use any preceding -I directories for #include <...>. */
1725 first_bracket_include
= 0;
1728 dirtmp
= new_include_prefix (last_include
, "",
1729 argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1730 append_include_chain (dirtmp
, dirtmp
);
1736 if (!strcmp (argv
[i
], "-nostdinc"))
1737 /* -nostdinc causes no default include directories.
1738 You must specify all include-file directories with -I. */
1739 no_standard_includes
= 1;
1740 else if (!strcmp (argv
[i
], "-nostdinc++"))
1741 /* -nostdinc++ causes no default C++-specific include directories. */
1742 no_standard_cplusplus_includes
= 1;
1743 else if (!strcmp (argv
[i
], "-noprecomp"))
1748 /* Sun compiler passes undocumented switch "-undef".
1749 Let's assume it means to inhibit the predefined symbols. */
1750 inhibit_predefs
= 1;
1753 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1754 if (in_fname
== NULL
) {
1757 } else if (out_fname
== NULL
) {
1760 } /* else fall through into error */
1763 fatal ("Invalid option `%s'", argv
[i
]);
1768 /* Add dirs from CPATH after dirs from -I. */
1769 /* There seems to be confusion about what CPATH should do,
1770 so for the moment it is not documented. */
1771 /* Some people say that CPATH should replace the standard include dirs,
1772 but that seems pointless: it comes before them, so it overrides them
1774 cp
= getenv ("CPATH");
1775 if (cp
&& ! no_standard_includes
)
1778 /* Now that dollars_in_ident is known, initialize is_idchar. */
1779 initialize_char_syntax ();
1781 /* Initialize output buffer */
1783 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1784 outbuf
.bufp
= outbuf
.buf
;
1785 outbuf
.length
= OUTBUF_SIZE
;
1787 /* Do partial setup of input buffer for the sake of generating
1788 early #line directives (when -g is in effect). */
1790 fp
= &instack
[++indepth
];
1791 if (in_fname
== NULL
)
1793 fp
->nominal_fname
= fp
->fname
= in_fname
;
1796 /* In C++, wchar_t is a distinct basic type, and we can expect
1797 __wchar_t to be defined by cc1plus. */
1799 wchar_type
= "__wchar_t";
1801 /* Install __LINE__, etc. Must follow initialize_char_syntax
1802 and option processing. */
1803 initialize_builtins (fp
, &outbuf
);
1805 /* Do standard #defines and assertions
1806 that identify system and machine type. */
1808 if (!inhibit_predefs
) {
1809 char *p
= (char *) alloca (strlen (predefs
) + 1);
1810 strcpy (p
, predefs
);
1813 while (*p
== ' ' || *p
== '\t')
1815 /* Handle -D options. */
1816 if (p
[0] == '-' && p
[1] == 'D') {
1818 while (*p
&& *p
!= ' ' && *p
!= '\t')
1823 output_line_directive (fp
, &outbuf
, 0, same_file
);
1824 make_definition (q
, &outbuf
);
1825 while (*p
== ' ' || *p
== '\t')
1827 } else if (p
[0] == '-' && p
[1] == 'A') {
1828 /* Handle -A options (assertions). */
1837 past_name
= assertion
;
1838 /* Locate end of name. */
1839 while (*past_name
&& *past_name
!= ' '
1840 && *past_name
!= '\t' && *past_name
!= '(')
1842 /* Locate `(' at start of value. */
1844 while (*value
&& (*value
== ' ' || *value
== '\t'))
1846 if (*value
++ != '(')
1848 while (*value
&& (*value
== ' ' || *value
== '\t'))
1851 /* Locate end of value. */
1852 while (*past_value
&& *past_value
!= ' '
1853 && *past_value
!= '\t' && *past_value
!= ')')
1855 termination
= past_value
;
1856 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
1858 if (*termination
++ != ')')
1860 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
1862 /* Temporarily null-terminate the value. */
1863 save_char
= *termination
;
1864 *termination
= '\0';
1865 /* Install the assertion. */
1866 make_assertion ("-A", assertion
);
1867 *termination
= (char) save_char
;
1869 while (*p
== ' ' || *p
== '\t')
1877 /* Now handle the command line options. */
1879 /* Do -U's, -D's and -A's in the order they were seen. */
1880 for (i
= 1; i
< argc
; i
++) {
1881 if (pend_undefs
[i
]) {
1883 output_line_directive (fp
, &outbuf
, 0, same_file
);
1884 make_undef (pend_undefs
[i
], &outbuf
);
1888 output_line_directive (fp
, &outbuf
, 0, same_file
);
1889 make_definition (pend_defs
[i
], &outbuf
);
1891 if (pend_assertions
[i
])
1892 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1895 done_initializing
= 1;
1897 { /* read the appropriate environment variable and if it exists
1898 replace include_defaults with the listed path. */
1900 switch ((objc
<< 1) + cplusplus
)
1903 epath
= getenv ("C_INCLUDE_PATH");
1906 epath
= getenv ("CPLUS_INCLUDE_PATH");
1909 epath
= getenv ("OBJC_INCLUDE_PATH");
1912 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
1915 /* If the environment var for this language is set,
1916 add to the default list of include directories. */
1919 char *startp
, *endp
;
1921 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1922 if (*startp
== PATH_SEPARATOR
)
1925 = (struct default_include
*) xmalloc ((num_dirs
1926 * sizeof (struct default_include
))
1927 + sizeof (include_defaults_array
));
1928 startp
= endp
= epath
;
1932 if (c
== PATH_SEPARATOR
|| !c
) {
1934 include_defaults
[num_dirs
].fname
1935 = startp
== endp
? "." : savestring (startp
);
1937 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1938 include_defaults
[num_dirs
].cxx_aware
= 1;
1945 /* Put the usual defaults back in at the end. */
1946 bcopy ((char *) include_defaults_array
,
1947 (char *) &include_defaults
[num_dirs
],
1948 sizeof (include_defaults_array
));
1952 append_include_chain (before_system
, last_before_system
);
1953 first_system_include
= before_system
;
1955 /* Unless -fnostdinc,
1956 tack on the standard include file dirs to the specified list */
1957 if (!no_standard_includes
) {
1958 struct default_include
*p
= include_defaults
;
1959 char *specd_prefix
= include_prefix
;
1960 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
1961 int default_len
= 0;
1962 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1963 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1964 default_len
= strlen (default_prefix
) - 7;
1965 default_prefix
[default_len
] = 0;
1967 /* Search "translated" versions of GNU directories.
1968 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1969 if (specd_prefix
!= 0 && default_len
!= 0)
1970 for (p
= include_defaults
; p
->fname
; p
++) {
1971 /* Some standard dirs are only for C++. */
1972 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1973 /* Does this dir start with the prefix? */
1974 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1975 /* Yes; change prefix and add to search list. */
1976 struct file_name_list
*new
1977 = new_include_prefix (NULL_PTR
, specd_prefix
,
1978 p
->fname
+ default_len
);
1980 new->c_system_include_path
= !p
->cxx_aware
;
1981 append_include_chain (new, new);
1982 if (first_system_include
== 0)
1983 first_system_include
= new;
1988 /* Search ordinary names for GNU include directories. */
1989 for (p
= include_defaults
; p
->fname
; p
++) {
1990 /* Some standard dirs are only for C++. */
1991 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1992 struct file_name_list
*new
1993 = new_include_prefix (NULL_PTR
, "", p
->fname
);
1995 new->c_system_include_path
= !p
->cxx_aware
;
1996 append_include_chain (new, new);
1997 if (first_system_include
== 0)
1998 first_system_include
= new;
2004 /* Tack the after_include chain at the end of the include chain. */
2005 append_include_chain (after_include
, last_after_include
);
2006 if (first_system_include
== 0)
2007 first_system_include
= after_include
;
2009 /* With -v, print the list of dirs to search. */
2011 struct file_name_list
*p
;
2012 fprintf (stderr
, "#include \"...\" search starts here:\n");
2013 for (p
= include
; p
; p
= p
->next
) {
2014 if (p
== first_bracket_include
)
2015 fprintf (stderr
, "#include <...> search starts here:\n");
2017 fprintf (stderr
, " .\n");
2018 else if (!strcmp (p
->fname
, "/") || !strcmp (p
->fname
, "//"))
2019 fprintf (stderr
, " %s\n", p
->fname
);
2021 /* Omit trailing '/'. */
2022 fprintf (stderr
, " %.*s\n", (int) strlen (p
->fname
) - 1, p
->fname
);
2024 fprintf (stderr
, "End of search list.\n");
2027 /* -MG doesn't select the form of output and must be specified with one of
2028 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
2029 inhibit compilation. */
2030 if (print_deps_missing_files
&& (print_deps
== 0 || !inhibit_output
))
2031 fatal ("-MG must be specified with one of -M or -MM");
2033 /* Either of two environment variables can specify output of deps.
2034 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
2035 where OUTPUT_FILE is the file to write deps info to
2036 and DEPS_TARGET is the target to mention in the deps. */
2039 && (getenv ("SUNPRO_DEPENDENCIES") != 0
2040 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
2041 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
2046 spec
= getenv ("SUNPRO_DEPENDENCIES");
2053 /* Find the space before the DEPS_TARGET, if there is one. */
2054 /* This should use index. (mrs) */
2055 while (*s
!= 0 && *s
!= ' ') s
++;
2057 deps_target
= s
+ 1;
2058 output_file
= xmalloc (s
- spec
+ 1);
2059 bcopy (spec
, output_file
, s
- spec
);
2060 output_file
[s
- spec
] = 0;
2067 deps_file
= output_file
;
2071 /* For -M, print the expected object file name
2072 as the target of this Make-rule. */
2074 deps_allocated_size
= 200;
2075 deps_buffer
= xmalloc (deps_allocated_size
);
2081 deps_output (deps_target
, ':');
2082 } else if (*in_fname
== 0) {
2083 deps_output ("-", ':');
2088 q
= base_name (in_fname
);
2090 /* Copy remainder to mungable area. */
2091 p
= (char *) alloca (strlen(q
) + 8);
2094 /* Output P, but remove known suffixes. */
2098 && p
[len
- 2] == '.'
2099 && index("cCsSm", p
[len
- 1]))
2102 && p
[len
- 3] == '.'
2103 && p
[len
- 2] == 'c'
2104 && p
[len
- 1] == 'c')
2107 && p
[len
- 4] == '.'
2108 && p
[len
- 3] == 'c'
2109 && p
[len
- 2] == 'x'
2110 && p
[len
- 1] == 'x')
2113 && p
[len
- 4] == '.'
2114 && p
[len
- 3] == 'c'
2115 && p
[len
- 2] == 'p'
2116 && p
[len
- 1] == 'p')
2119 /* Supply our own suffix. */
2126 deps_output (p
, ':');
2127 deps_output (in_fname
, ' ');
2131 /* Scan the -imacros files before the main input.
2132 Much like #including them, but with no_output set
2133 so that only their macro definitions matter. */
2135 no_output
++; no_record_file
++;
2136 for (i
= 1; i
< argc
; i
++)
2137 if (pend_files
[i
]) {
2138 struct include_file
*inc
;
2139 int fd
= open_include_file (pend_files
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2141 perror_with_name (pend_files
[i
]);
2142 return FATAL_EXIT_CODE
;
2144 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2146 no_output
--; no_record_file
--;
2148 /* Copy the entire contents of the main input file into
2149 the stacked input buffer previously allocated for it. */
2151 /* JF check for stdin */
2152 if (in_fname
== NULL
|| *in_fname
== 0) {
2155 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
2158 if (fstat (f
, &st
) != 0)
2159 pfatal_with_name (in_fname
);
2160 fp
->nominal_fname
= fp
->fname
= in_fname
;
2162 fp
->system_header_p
= 0;
2163 /* JF all this is mine about reading pipes and ttys */
2164 if (! S_ISREG (st
.st_mode
)) {
2165 /* Read input from a file that is not a normal disk file.
2166 We cannot preallocate a buffer with the correct size,
2167 so we must read in the file a piece at the time and make it bigger. */
2172 if (S_ISDIR (st
.st_mode
))
2173 fatal ("Input file `%s' is a directory", in_fname
);
2177 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
2179 cnt
= safe_read (f
, (char *) fp
->buf
+ size
, bsize
- size
);
2180 if (cnt
< 0) goto perror
; /* error! */
2182 if (size
!= bsize
) break; /* End of file */
2184 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
2188 /* Read a file whose size we can determine in advance.
2189 For the sake of VMS, st.st_size is just an upper bound. */
2190 fp
->buf
= (U_CHAR
*) xmalloc (st
.st_size
+ 2);
2191 fp
->length
= safe_read (f
, (char *) fp
->buf
, st
.st_size
);
2192 if (fp
->length
< 0) goto perror
;
2195 fp
->if_stack
= if_stack
;
2197 /* Make sure data ends with a newline. And put a null after it. */
2199 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
2200 /* Backslash-newline at end is not good enough. */
2201 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
2202 fp
->buf
[fp
->length
++] = '\n';
2203 missing_newline
= 1;
2205 fp
->buf
[fp
->length
] = '\0';
2207 /* Unless inhibited, convert trigraphs in the input. */
2212 /* Now that we know the input file is valid, open the output. */
2214 if (!out_fname
|| !strcmp (out_fname
, ""))
2215 out_fname
= "stdout";
2216 else if (! freopen (out_fname
, "w", stdout
))
2217 pfatal_with_name (out_fname
);
2219 output_line_directive (fp
, &outbuf
, 0, same_file
);
2221 /* Scan the -include files before the main input. */
2224 for (i
= 1; i
< argc
; i
++)
2225 if (pend_includes
[i
]) {
2226 struct include_file
*inc
;
2227 int fd
= open_include_file (pend_includes
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2229 perror_with_name (pend_includes
[i
]);
2230 return FATAL_EXIT_CODE
;
2232 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2236 /* Scan the input, processing macros and directives. */
2238 rescan (&outbuf
, 0);
2240 if (missing_newline
)
2243 if (pedantic
&& missing_newline
)
2244 pedwarn ("file does not end in newline");
2246 /* Now we have processed the entire input
2247 Write whichever kind of output has been requested. */
2249 if (dump_macros
== dump_only
)
2251 else if (! inhibit_output
) {
2256 /* Don't actually write the deps file if compilation has failed. */
2258 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, deps_mode
)))
2259 pfatal_with_name (deps_file
);
2260 fputs (deps_buffer
, deps_stream
);
2261 putc ('\n', deps_stream
);
2263 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
2264 fatal ("I/O error on output");
2269 if (pcp_outfile
&& pcp_outfile
!= stdout
2270 && (ferror (pcp_outfile
) || fclose (pcp_outfile
) != 0))
2271 fatal ("I/O error on `-pcp' output");
2273 if (ferror (stdout
) || fclose (stdout
) != 0)
2274 fatal ("I/O error on output");
2277 exit (FATAL_EXIT_CODE
);
2278 exit (SUCCESS_EXIT_CODE
);
2281 pfatal_with_name (in_fname
);
2285 /* Given a colon-separated list of file names PATH,
2286 add all the names to the search path for include files. */
2300 struct file_name_list
*dirtmp
;
2302 /* Find the end of this name. */
2303 while ((c
= *q
++) != PATH_SEPARATOR
&& c
)
2307 dirtmp
= new_include_prefix (last_include
, "", p
== q
? "." : p
);
2309 append_include_chain (dirtmp
, dirtmp
);
2311 /* Advance past this name. */
2318 /* Return the address of the first character in S that equals C.
2319 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2320 Return 0 if there is no such character. Assume that C itself is not '\0'.
2321 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2322 but unfortunately memchr isn't autoconfigured yet. */
2330 char *p
= (char *) s
;
2332 char *q
= index (p
, c
);
2334 return (U_CHAR
*) q
;
2336 size_t l
= strlen (p
);
2346 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2347 before main CCCP processing. Name `pcp' is also in honor of the
2348 drugs the trigraph designers must have been on.
2350 Using an extra pass through the buffer takes a little extra time,
2351 but is infinitely less hairy than trying to handle trigraphs inside
2352 strings, etc. everywhere, and also makes sure that trigraphs are
2353 only translated in the top level of processing. */
2359 register U_CHAR c
, *fptr
, *bptr
, *sptr
, *lptr
;
2362 fptr
= bptr
= sptr
= buf
->buf
;
2363 lptr
= fptr
+ buf
->length
;
2364 while ((sptr
= index0 (sptr
, '?', (size_t) (lptr
- sptr
))) != NULL
) {
2401 len
= sptr
- fptr
- 2;
2403 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2404 C, this will be memmove (). */
2405 if (bptr
!= fptr
&& len
> 0)
2406 bcopy ((char *) fptr
, (char *) bptr
, len
);
2412 len
= buf
->length
- (fptr
- buf
->buf
);
2413 if (bptr
!= fptr
&& len
> 0)
2414 bcopy ((char *) fptr
, (char *) bptr
, len
);
2415 buf
->length
-= fptr
- bptr
;
2416 buf
->buf
[buf
->length
] = '\0';
2417 if (warn_trigraphs
&& fptr
!= bptr
)
2418 warning_with_line (0, "%lu trigraph(s) encountered",
2419 (unsigned long) (fptr
- bptr
) / 2);
2422 /* Move all backslash-newline pairs out of embarrassing places.
2423 Exchange all such pairs following BP
2424 with any potentially-embarrassing characters that follow them.
2425 Potentially-embarrassing characters are / and *
2426 (because a backslash-newline inside a comment delimiter
2427 would cause it not to be recognized). */
2433 register U_CHAR
*p
= bp
;
2435 /* First count the backslash-newline pairs here. */
2437 while (p
[0] == '\\' && p
[1] == '\n')
2440 /* What follows the backslash-newlines is not embarrassing. */
2442 if (*p
!= '/' && *p
!= '*')
2445 /* Copy all potentially embarrassing characters
2446 that follow the backslash-newline pairs
2447 down to where the pairs originally started. */
2449 while (*p
== '*' || *p
== '/')
2452 /* Now write the same number of pairs after the embarrassing chars. */
2459 /* Like newline_fix but for use within a directive-name.
2460 Move any backslash-newlines up past any following symbol constituents. */
2463 name_newline_fix (bp
)
2466 register U_CHAR
*p
= bp
;
2468 /* First count the backslash-newline pairs here. */
2469 while (p
[0] == '\\' && p
[1] == '\n')
2472 /* What follows the backslash-newlines is not embarrassing. */
2477 /* Copy all potentially embarrassing characters
2478 that follow the backslash-newline pairs
2479 down to where the pairs originally started. */
2481 while (is_idchar
[*p
])
2484 /* Now write the same number of pairs after the embarrassing chars. */
2491 /* Look for lint commands in comments.
2493 When we come in here, ibp points into a comment. Limit is as one expects.
2494 scan within the comment -- it should start, after lwsp, with a lint command.
2495 If so that command is returned as a (constant) string.
2497 Upon return, any arg will be pointed to with argstart and will be
2498 arglen long. Note that we don't parse that arg since it will just
2499 be printed out again.
2503 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2504 register U_CHAR
*ibp
;
2505 register U_CHAR
*limit
;
2506 U_CHAR
**argstart
; /* point to command arg */
2507 int *arglen
, *cmdlen
; /* how long they are */
2509 HOST_WIDE_INT linsize
;
2510 register U_CHAR
*numptr
; /* temp for arg parsing */
2514 SKIP_WHITE_SPACE (ibp
);
2516 if (ibp
>= limit
) return NULL
;
2518 linsize
= limit
- ibp
;
2520 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2521 if ((linsize
>= 10) && !bcmp (ibp
, "NOTREACHED", 10)) {
2523 return "NOTREACHED";
2525 if ((linsize
>= 8) && !bcmp (ibp
, "ARGSUSED", 8)) {
2529 if ((linsize
>= 11) && !bcmp (ibp
, "LINTLIBRARY", 11)) {
2531 return "LINTLIBRARY";
2533 if ((linsize
>= 7) && !bcmp (ibp
, "VARARGS", 7)) {
2535 ibp
+= 7; linsize
-= 7;
2536 if ((linsize
== 0) || ! isdigit (*ibp
)) return "VARARGS";
2538 /* OK, read a number */
2539 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && isdigit (*numptr
);
2541 *arglen
= numptr
- *argstart
;
2548 * The main loop of the program.
2550 * Read characters from the input stack, transferring them to the
2553 * Macros are expanded and push levels on the input stack.
2554 * At the end of such a level it is popped off and we keep reading.
2555 * At the end of any other kind of level, we return.
2556 * #-directives are handled, except within macros.
2558 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2559 * and insert them when appropriate. This is set while scanning macro
2560 * arguments before substitution. It is zero when scanning for final output.
2561 * There are three types of Newline markers:
2562 * * Newline - follows a macro name that was not expanded
2563 * because it appeared inside an expansion of the same macro.
2564 * This marker prevents future expansion of that identifier.
2565 * When the input is rescanned into the final output, these are deleted.
2566 * These are also deleted by ## concatenation.
2567 * * Newline Space (or Newline and any other whitespace character)
2568 * stands for a place that tokens must be separated or whitespace
2569 * is otherwise desirable, but where the ANSI standard specifies there
2570 * is no whitespace. This marker turns into a Space (or whichever other
2571 * whitespace char appears in the marker) in the final output,
2572 * but it turns into nothing in an argument that is stringified with #.
2573 * Such stringified arguments are the only place where the ANSI standard
2574 * specifies with precision that whitespace may not appear.
2576 * During this function, IP->bufp is kept cached in IBP for speed of access.
2577 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2578 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2579 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2580 * explicitly, and before RECACHE, since RECACHE uses OBP.
2584 rescan (op
, output_marks
)
2588 /* Character being scanned in main loop. */
2591 /* Length of pending accumulated identifier. */
2592 register int ident_length
= 0;
2594 /* Hash code of pending accumulated identifier. */
2595 register int hash
= 0;
2597 /* Current input level (&instack[indepth]). */
2600 /* Pointer for scanning input. */
2601 register U_CHAR
*ibp
;
2603 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2604 register U_CHAR
*limit
;
2606 /* Pointer for storing output. */
2607 register U_CHAR
*obp
;
2609 /* REDO_CHAR is nonzero if we are processing an identifier
2610 after backing up over the terminating character.
2611 Sometimes we process an identifier without backing up over
2612 the terminating character, if the terminating character
2613 is not special. Backing up is done so that the terminating character
2614 will be dispatched on again once the identifier is dealt with. */
2617 /* 1 if within an identifier inside of which a concatenation
2618 marker (Newline -) has been seen. */
2619 int concatenated
= 0;
2621 /* While scanning a comment or a string constant,
2622 this records the line it started on, for error messages. */
2625 /* Record position of last `real' newline. */
2626 U_CHAR
*beg_of_line
;
2628 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2631 do { ip->macro->type = T_MACRO; \
2632 if (ip->free_ptr) free (ip->free_ptr); \
2633 --indepth; } while (0)
2635 /* Reload `rescan's local variables that describe the current
2636 level of the input stack. */
2639 do { ip = &instack[indepth]; \
2641 limit = ip->buf + ip->length; \
2643 check_expand (op, limit - ibp); \
2645 obp = op->bufp; } while (0)
2647 if (no_output
&& instack
[indepth
].fname
!= 0)
2648 skip_if_group (&instack
[indepth
], 1, NULL
);
2655 /* Our caller must always put a null after the end of
2656 the input at each input stack level. */
2666 if (*ibp
== '\n' && !ip
->macro
) {
2667 /* At the top level, always merge lines ending with backslash-newline,
2668 even in middle of identifier. But do not merge lines in a macro,
2669 since backslash might be followed by a newline-space marker. */
2672 --obp
; /* remove backslash from obuf */
2675 /* If ANSI, backslash is just another character outside a string. */
2678 /* Otherwise, backslash suppresses specialness of following char,
2679 so copy it here to prevent the switch from seeing it.
2680 But first get any pending identifier processed. */
2681 if (ident_length
> 0)
2688 if (ident_length
|| ip
->macro
|| traditional
)
2690 while (*ibp
== '\\' && ibp
[1] == '\n') {
2696 /* Treat this %: digraph as if it were #. */
2700 if (assertions_flag
) {
2703 /* Copy #foo (bar lose) without macro expansion. */
2704 obp
[-1] = '#'; /* In case it was '%'. */
2705 SKIP_WHITE_SPACE (ibp
);
2706 while (is_idchar
[*ibp
])
2708 SKIP_WHITE_SPACE (ibp
);
2711 skip_paren_group (ip
);
2712 bcopy ((char *) ibp
, (char *) obp
, ip
->bufp
- ibp
);
2713 obp
+= ip
->bufp
- ibp
;
2719 /* If this is expanding a macro definition, don't recognize
2720 preprocessing directives. */
2723 /* If this is expand_into_temp_buffer,
2724 don't recognize them either. Warn about them
2725 only after an actual newline at this level,
2726 not at the beginning of the input level. */
2728 if (ip
->buf
!= beg_of_line
)
2729 warning ("preprocessing directive not recognized within macro arg");
2736 /* # keyword: a # must be first nonblank char on the line */
2737 if (beg_of_line
== 0)
2742 /* Scan from start of line, skipping whitespace, comments
2743 and backslash-newlines, and see if we reach this #.
2744 If not, this # is not special. */
2746 /* If -traditional, require # to be at beginning of line. */
2749 if (is_hor_space
[*bp
])
2751 else if (*bp
== '\\' && bp
[1] == '\n')
2753 else if (*bp
== '/' && bp
[1] == '*') {
2755 while (!(*bp
== '*' && bp
[1] == '/'))
2759 /* There is no point in trying to deal with C++ // comments here,
2760 because if there is one, then this # must be part of the
2761 comment and we would never reach here. */
2767 while (bp
[1] == '\\' && bp
[2] == '\n')
2771 /* %: appears at start of line; skip past the ':' too. */
2780 /* This # can start a directive. */
2782 --obp
; /* Don't copy the '#' */
2786 if (! handle_directive (ip
, op
)) {
2790 /* Not a known directive: treat it as ordinary text.
2791 IP, OP, IBP, etc. have not been changed. */
2792 if (no_output
&& instack
[indepth
].fname
) {
2793 /* If not generating expanded output,
2794 what we do with ordinary text is skip it.
2795 Discard everything until next # directive. */
2796 skip_if_group (&instack
[indepth
], 1, 0);
2801 *obp
++ = '#'; /* Copy # (even if it was originally %:). */
2802 /* Don't expand an identifier that could be a macro directive.
2803 (Section 3.8.3 of the ANSI C standard) */
2804 SKIP_WHITE_SPACE (ibp
);
2805 if (is_idstart
[*ibp
])
2808 while (is_idchar
[*ibp
])
2816 /* A # directive has been successfully processed. */
2817 /* If not generating expanded output, ignore everything until
2818 next # directive. */
2819 if (no_output
&& instack
[indepth
].fname
)
2820 skip_if_group (&instack
[indepth
], 1, 0);
2826 case '\"': /* skip quoted string */
2828 /* A single quoted string is treated like a double -- some
2829 programs (e.g., troff) are perverse this way */
2834 start_line
= ip
->lineno
;
2836 /* Skip ahead to a matching quote. */
2840 if (ip
->macro
!= 0) {
2841 /* try harder: this string crosses a macro expansion boundary.
2842 This can happen naturally if -traditional.
2843 Otherwise, only -D can make a macro with an unmatched quote. */
2849 error_with_line (line_for_error (start_line
),
2850 "unterminated string or character constant");
2851 error_with_line (multiline_string_line
,
2852 "possible real start of unterminated constant");
2853 multiline_string_line
= 0;
2862 /* Traditionally, end of line ends a string constant with no error.
2863 So exit the loop and record the new line. */
2869 error_with_line (line_for_error (start_line
),
2870 "unterminated character constant");
2873 if (multiline_string_line
== 0) {
2875 pedwarn_with_line (line_for_error (start_line
),
2876 "string constant runs past end of line");
2877 multiline_string_line
= ip
->lineno
- 1;
2885 /* Backslash newline is replaced by nothing at all,
2886 but keep the line counts correct. */
2891 /* ANSI stupidly requires that in \\ the second \
2892 is *not* prevented from combining with a newline. */
2893 while (*ibp
== '\\' && ibp
[1] == '\n') {
2912 if (*ibp
== '\\' && ibp
[1] == '\n')
2916 && !(cplusplus_comments
&& *ibp
== '/'))
2924 /* C++ style comment... */
2925 start_line
= ip
->lineno
;
2927 /* Comments are equivalent to spaces. */
2928 if (! put_out_comments
)
2932 U_CHAR
*before_bp
= ibp
;
2934 while (++ibp
< limit
) {
2936 if (ibp
[-1] != '\\') {
2937 if (put_out_comments
) {
2938 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
2939 obp
+= ibp
- before_bp
;
2944 warning ("multiline `//' comment");
2946 /* Copy the newline into the output buffer, in order to
2947 avoid the pain of a #line every time a multiline comment
2949 if (!put_out_comments
)
2958 /* Ordinary C comment. Skip it, optionally copying it to output. */
2960 start_line
= ip
->lineno
;
2962 ++ibp
; /* Skip the star. */
2964 /* If this cpp is for lint, we peek inside the comments: */
2968 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2970 if (lintcmd
!= NULL
) {
2972 check_expand (op
, cmdlen
+ arglen
+ 14);
2974 /* I believe it is always safe to emit this newline: */
2976 bcopy ("#pragma lint ", (char *) obp
, 13);
2978 bcopy (lintcmd
, (char *) obp
, cmdlen
);
2983 bcopy (argbp
, (char *) obp
, arglen
);
2987 /* OK, now bring us back to the state we were in before we entered
2988 this branch. We need #line because the #pragma's newline always
2989 messes up the line count. */
2991 output_line_directive (ip
, op
, 0, same_file
);
2992 check_expand (op
, limit
- ibp
+ 2);
2998 /* Comments are equivalent to spaces.
2999 Note that we already output the slash; we might not want it.
3000 For -traditional, a comment is equivalent to nothing. */
3001 if (! put_out_comments
) {
3011 U_CHAR
*before_bp
= ibp
;
3016 if (ibp
[-2] == '/' && warn_comments
)
3017 warning ("`/*' within comment");
3018 if (*ibp
== '\\' && ibp
[1] == '\n')
3026 /* Copy the newline into the output buffer, in order to
3027 avoid the pain of a #line every time a multiline comment
3029 if (!put_out_comments
)
3036 error_with_line (line_for_error (start_line
),
3037 "unterminated comment");
3046 if (put_out_comments
) {
3047 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
3048 obp
+= ibp
- before_bp
;
3054 if (!dollars_in_ident
)
3058 case '0': case '1': case '2': case '3': case '4':
3059 case '5': case '6': case '7': case '8': case '9':
3060 /* If digit is not part of identifier, it starts a number,
3061 which means that following letters are not an identifier.
3062 "0x5" does not refer to an identifier "x5".
3063 So copy all alphanumerics that follow without accumulating
3064 as an identifier. Periods also, for sake of "3.e7". */
3066 if (ident_length
== 0) {
3068 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3073 if (!is_idchar
[c
] && c
!= '.') {
3078 /* A sign can be part of a preprocessing number
3079 if it follows an e. */
3080 if (c
== 'e' || c
== 'E') {
3081 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3085 if (*ibp
== '+' || *ibp
== '-') {
3087 /* But traditional C does not let the token go past the sign. */
3098 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3099 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3100 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3101 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3103 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3104 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3105 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3106 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3110 /* Compute step of hash function, to avoid a proc call on every token */
3111 hash
= HASHSTEP (hash
, c
);
3115 if (ip
->fname
== 0 && *ibp
== '-') {
3116 /* Newline - inhibits expansion of preceding token.
3117 If expanding a macro arg, we keep the newline -.
3118 In final output, it is deleted.
3119 We recognize Newline - in macro bodies and macro args. */
3120 if (! concatenated
) {
3125 if (!output_marks
) {
3128 /* If expanding a macro arg, keep the newline -. */
3134 /* If reprocessing a macro expansion, newline is a special marker. */
3135 else if (ip
->macro
!= 0) {
3136 /* Newline White is a "funny space" to separate tokens that are
3137 supposed to be separate but without space between.
3138 Here White means any whitespace character.
3139 Newline - marks a recursive macro use that is not
3140 supposed to be expandable. */
3142 if (is_space
[*ibp
]) {
3143 /* Newline Space does not prevent expansion of preceding token
3144 so expand the preceding token and then come back. */
3145 if (ident_length
> 0)
3148 /* If generating final output, newline space makes a space. */
3149 if (!output_marks
) {
3151 /* And Newline Newline makes a newline, so count it. */
3152 if (obp
[-1] == '\n')
3155 /* If expanding a macro arg, keep the newline space.
3156 If the arg gets stringified, newline space makes nothing. */
3159 } else abort (); /* Newline followed by something random? */
3163 /* If there is a pending identifier, handle it and come back here. */
3164 if (ident_length
> 0)
3169 /* Update the line counts and output a #line if necessary. */
3172 if (ip
->lineno
!= op
->lineno
) {
3174 output_line_directive (ip
, op
, 1, same_file
);
3175 check_expand (op
, limit
- ibp
);
3180 /* Come here either after (1) a null character that is part of the input
3181 or (2) at the end of the input, because there is a null there. */
3184 /* Our input really contains a null character. */
3188 /* At end of a macro-expansion level, pop it and read next level. */
3189 if (ip
->macro
!= 0) {
3192 /* If traditional, and we have an identifier that ends here,
3193 process it now, so we get the right error for recursion. */
3194 if (traditional
&& ident_length
3195 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
3204 /* If we don't have a pending identifier,
3205 return at end of input. */
3206 if (ident_length
== 0) {
3214 /* If we do have a pending identifier, just consider this null
3215 a special character and arrange to dispatch on it again.
3216 The second time, IDENT_LENGTH will be zero so we will return. */
3222 /* Handle the case of a character such as /, ', " or null
3223 seen following an identifier. Back over it so that
3224 after the identifier is processed the special char
3225 will be dispatched on again. */
3235 if (ident_length
> 0) {
3236 register HASHNODE
*hp
;
3238 /* We have just seen an identifier end. If it's a macro, expand it.
3240 IDENT_LENGTH is the length of the identifier
3241 and HASH is its hash code.
3243 The identifier has already been copied to the output,
3244 so if it is a macro we must remove it.
3246 If REDO_CHAR is 0, the char that terminated the identifier
3247 has been skipped in the output and the input.
3248 OBP-IDENT_LENGTH-1 points to the identifier.
3249 If the identifier is a macro, we must back over the terminator.
3251 If REDO_CHAR is 1, the terminating char has already been
3252 backed over. OBP-IDENT_LENGTH points to the identifier. */
3254 if (!pcp_outfile
|| pcp_inside_if
) {
3255 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
3258 if (hp
->length
== ident_length
) {
3259 int obufp_before_macroname
;
3260 int op_lineno_before_macroname
;
3261 register int i
= ident_length
;
3262 register U_CHAR
*p
= hp
->name
;
3263 register U_CHAR
*q
= obp
- i
;
3269 do { /* All this to avoid a strncmp () */
3274 /* We found a use of a macro name.
3275 see if the context shows it is a macro call. */
3277 /* Back up over terminating character if not already done. */
3283 /* Save this as a displacement from the beginning of the output
3284 buffer. We can not save this as a position in the output
3285 buffer, because it may get realloc'ed by RECACHE. */
3286 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
3287 op_lineno_before_macroname
= op
->lineno
;
3289 if (hp
->type
== T_PCSTRING
) {
3290 pcstring_used (hp
); /* Mark the definition of this key
3291 as needed, ensuring that it
3293 break; /* Exit loop, since the key cannot have a
3294 definition any longer. */
3297 /* Record whether the macro is disabled. */
3298 disabled
= hp
->type
== T_DISABLED
;
3300 /* This looks like a macro ref, but if the macro was disabled,
3301 just copy its name and put in a marker if requested. */
3305 /* This error check caught useful cases such as
3306 #define foo(x,y) bar (x (y,0), y)
3309 error ("recursive use of macro `%s'", hp
->name
);
3313 check_expand (op
, limit
- ibp
+ 2);
3320 /* If macro wants an arglist, verify that a '(' follows.
3321 first skip all whitespace, copying it to the output
3322 after the macro name. Then, if there is no '(',
3323 decide this is not a macro call and leave things that way. */
3324 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
3325 && hp
->value
.defn
->nargs
>= 0)
3327 U_CHAR
*old_ibp
= ibp
;
3328 U_CHAR
*old_obp
= obp
;
3329 int old_iln
= ip
->lineno
;
3330 int old_oln
= op
->lineno
;
3333 /* Scan forward over whitespace, copying it to the output. */
3334 if (ibp
== limit
&& ip
->macro
!= 0) {
3339 old_iln
= ip
->lineno
;
3340 old_oln
= op
->lineno
;
3342 /* A comment: copy it unchanged or discard it. */
3343 else if (*ibp
== '/' && ibp
[1] == '*') {
3344 if (put_out_comments
) {
3347 } else if (! traditional
) {
3351 while (ibp
+ 1 != limit
3352 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
3353 /* We need not worry about newline-marks,
3354 since they are never found in comments. */
3356 /* Newline in a file. Count it. */
3360 if (put_out_comments
)
3366 if (put_out_comments
) {
3371 else if (is_space
[*ibp
]) {
3373 if (ibp
[-1] == '\n') {
3374 if (ip
->macro
== 0) {
3375 /* Newline in a file. Count it. */
3378 } else if (!output_marks
) {
3379 /* A newline mark, and we don't want marks
3380 in the output. If it is newline-hyphen,
3381 discard it entirely. Otherwise, it is
3382 newline-whitechar, so keep the whitechar. */
3392 /* A newline mark; copy both chars to the output. */
3400 /* It isn't a macro call.
3401 Put back the space that we just skipped. */
3404 ip
->lineno
= old_iln
;
3405 op
->lineno
= old_oln
;
3406 /* Exit the for loop. */
3411 /* This is now known to be a macro call.
3412 Discard the macro name from the output,
3413 along with any following whitespace just copied,
3414 but preserve newlines if not outputting marks since this
3415 is more likely to do the right thing with line numbers. */
3416 obp
= op
->buf
+ obufp_before_macroname
;
3418 op
->lineno
= op_lineno_before_macroname
;
3420 int newlines
= op
->lineno
- op_lineno_before_macroname
;
3421 while (0 < newlines
--)
3425 /* Prevent accidental token-pasting with a character
3426 before the macro call. */
3427 if (!traditional
&& obp
!= op
->buf
) {
3429 case '!': case '%': case '&': case '*':
3430 case '+': case '-': case '/': case ':':
3431 case '<': case '=': case '>': case '^':
3433 /* If we are expanding a macro arg, make a newline marker
3434 to separate the tokens. If we are making real output,
3435 a plain space will do. */
3442 /* Expand the macro, reading arguments as needed,
3443 and push the expansion on the input stack. */
3446 macroexpand (hp
, op
);
3448 /* Reexamine input stack, since macroexpand has pushed
3449 a new level on it. */
3456 } /* End hash-table-search loop */
3458 ident_length
= hash
= 0; /* Stop collecting identifier */
3461 } /* End if (ident_length > 0) */
3463 } /* End per-char loop */
3465 /* Come here to return -- but first give an error message
3466 if there was an unterminated successful conditional. */
3468 if (if_stack
!= ip
->if_stack
)
3472 switch (if_stack
->type
)
3493 error_with_line (line_for_error (if_stack
->lineno
),
3494 "unterminated `#%s' conditional", str
);
3496 if_stack
= ip
->if_stack
;
3500 * Rescan a string into a temporary buffer and return the result
3501 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3503 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3504 * and insert such markers when appropriate. See `rescan' for details.
3505 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3506 * before substitution; it is 0 for other uses.
3509 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
3510 U_CHAR
*buf
, *limit
;
3511 int output_marks
, assertions
;
3513 register FILE_BUF
*ip
;
3515 int length
= limit
- buf
;
3517 int odepth
= indepth
;
3518 int save_assertions_flag
= assertions_flag
;
3520 assertions_flag
= assertions
;
3525 /* Set up the input on the input stack. */
3527 buf1
= (U_CHAR
*) alloca (length
+ 1);
3529 register U_CHAR
*p1
= buf
;
3530 register U_CHAR
*p2
= buf1
;
3537 /* Set up to receive the output. */
3539 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3540 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3545 CHECK_DEPTH ({return obuf
;});
3549 ip
= &instack
[indepth
];
3551 ip
->nominal_fname
= 0;
3553 ip
->system_header_p
= 0;
3556 ip
->length
= length
;
3557 ip
->buf
= ip
->bufp
= buf1
;
3558 ip
->if_stack
= if_stack
;
3560 ip
->lineno
= obuf
.lineno
= 1;
3562 /* Scan the input, create the output. */
3563 rescan (&obuf
, output_marks
);
3565 /* Pop input stack to original state. */
3568 if (indepth
!= odepth
)
3571 /* Record the output. */
3572 obuf
.length
= obuf
.bufp
- obuf
.buf
;
3574 assertions_flag
= save_assertions_flag
;
3579 * Process a # directive. Expects IP->bufp to point after the '#', as in
3580 * `#define foo bar'. Passes to the directive handler
3581 * (do_define, do_include, etc.): the addresses of the 1st and
3582 * last chars of the directive (starting immediately after the #
3583 * keyword), plus op and the keyword table pointer. If the directive
3584 * contains comments it is copied into a temporary buffer sans comments
3585 * and the temporary buffer is passed to the directive handler instead.
3586 * Likewise for backslash-newlines.
3588 * Returns nonzero if this was a known # directive.
3589 * Otherwise, returns zero, without advancing the input pointer.
3593 handle_directive (ip
, op
)
3596 register U_CHAR
*bp
, *cp
;
3597 register struct directive
*kt
;
3598 register int ident_length
;
3601 /* Nonzero means we must copy the entire directive
3602 to get rid of comments or backslash-newlines. */
3603 int copy_directive
= 0;
3605 U_CHAR
*ident
, *after_ident
;
3609 /* Record where the directive started. do_xifdef needs this. */
3610 directive_start
= bp
- 1;
3612 /* Skip whitespace and \-newline. */
3614 if (is_hor_space
[*bp
]) {
3615 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3616 pedwarn ("%s in preprocessing directive", char_name
[*bp
]);
3618 } else if (*bp
== '/' && (bp
[1] == '*'
3619 || (cplusplus_comments
&& bp
[1] == '/'))) {
3621 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3623 } else if (*bp
== '\\' && bp
[1] == '\n') {
3624 bp
+= 2; ip
->lineno
++;
3628 /* Now find end of directive name.
3629 If we encounter a backslash-newline, exchange it with any following
3630 symbol-constituents so that we end up with a contiguous name. */
3637 if (*cp
== '\\' && cp
[1] == '\n')
3638 name_newline_fix (cp
);
3644 ident_length
= cp
- bp
;
3648 /* A line of just `#' becomes blank. */
3650 if (ident_length
== 0 && *after_ident
== '\n') {
3651 ip
->bufp
= after_ident
;
3655 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3657 while (is_idchar
[*p
]) {
3658 if (*p
< '0' || *p
> '9')
3662 /* Handle # followed by a line number. */
3663 if (p
!= ident
&& !is_idchar
[*p
]) {
3664 static struct directive line_directive_table
[] = {
3665 { 4, do_line
, "line", T_LINE
},
3668 pedwarn ("`#' followed by integer");
3669 after_ident
= ident
;
3670 kt
= line_directive_table
;
3674 /* Avoid error for `###' and similar cases unless -pedantic. */
3676 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3678 if (pedantic
&& !lang_asm
)
3679 warning ("invalid preprocessing directive");
3685 error ("invalid preprocessing directive name");
3691 * Decode the keyword and call the appropriate expansion
3692 * routine, after moving the input pointer up to the next line.
3694 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3695 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3696 register U_CHAR
*buf
;
3697 register U_CHAR
*limit
;
3700 int *already_output
;
3702 /* Nonzero means do not delete comments within the directive.
3703 #define needs this when -traditional. */
3708 limit
= ip
->buf
+ ip
->length
;
3711 keep_comments
= traditional
&& kt
->traditional_comments
;
3712 /* #import is defined only in Objective C, or when on the NeXT. */
3713 if (kt
->type
== T_IMPORT
3714 && !(objc
|| lookup ((U_CHAR
*) "__NeXT__", -1, -1)))
3717 /* Find the end of this directive (first newline not backslashed
3718 and not in a string or comment).
3719 Set COPY_DIRECTIVE if the directive must be copied
3720 (it contains a backslash-newline or a comment). */
3722 buf
= bp
= after_ident
;
3723 while (bp
< limit
) {
3724 register U_CHAR c
= *bp
++;
3732 } else if (traditional
)
3739 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3740 /* Don't bother calling the directive if we already got an error
3741 message due to unterminated string. Skip everything and pretend
3742 we called the directive. */
3745 /* Traditional preprocessing permits unterminated strings. */
3754 /* <...> is special for #include. */
3756 if (!kt
->angle_brackets
)
3758 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3759 if (*bp
== '\\' && bp
[1] == '\n') {
3769 if (*bp
== '\\' && bp
[1] == '\n')
3772 || (cplusplus_comments
&& *bp
== '/')) {
3773 U_CHAR
*obp
= bp
- 1;
3775 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3777 /* No need to copy the directive because of a comment at the end;
3778 just don't include the comment in the directive. */
3779 if (bp
== limit
|| *bp
== '\n') {
3783 /* Don't remove the comments if -traditional. */
3784 if (! keep_comments
)
3793 pedwarn ("%s in preprocessing directive", char_name
[c
]);
3797 --bp
; /* Point to the newline */
3805 resume_p
= ip
->bufp
;
3806 /* BP is the end of the directive.
3807 RESUME_P is the next interesting data after the directive.
3808 A comment may come between. */
3810 /* If a directive should be copied through, and -E was given,
3811 pass it through before removing comments. */
3812 if (!no_output
&& kt
->pass_thru
&& put_out_comments
) {
3815 /* Output directive name. */
3816 check_expand (op
, kt
->length
+ 2);
3817 /* Make sure # is at the start of a line */
3818 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3823 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3824 op
->bufp
+= kt
->length
;
3826 /* Output arguments. */
3828 check_expand (op
, len
);
3829 bcopy (buf
, (char *) op
->bufp
, len
);
3831 /* Take account of any (escaped) newlines just output. */
3833 if (buf
[len
] == '\n')
3836 already_output
= &junk
;
3837 } /* Don't we need a newline or #line? */
3839 if (copy_directive
) {
3840 register U_CHAR
*xp
= buf
;
3841 /* Need to copy entire directive into temp buffer before dispatching */
3843 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for directive plus
3847 /* Copy to the new buffer, deleting comments
3848 and backslash-newlines (and whitespace surrounding the latter). */
3851 register U_CHAR c
= *xp
++;
3856 abort (); /* A bare newline should never part of the line. */
3859 /* <...> is special for #include. */
3861 if (!kt
->angle_brackets
)
3863 while (xp
< bp
&& c
!= '>') {
3865 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
3876 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
3877 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
3879 SKIP_WHITE_SPACE (xp
);
3880 } else if (is_hor_space
[*xp
]) {
3882 SKIP_WHITE_SPACE (xp
);
3884 } else if (traditional
&& xp
< bp
) {
3892 register U_CHAR
*bp1
3893 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
3894 NULL_PTR
, NULL_PTR
, NULL_PTR
);
3908 || (cplusplus_comments
&& *xp
== '/')) {
3910 /* If we already copied the directive through,
3911 already_output != 0 prevents outputting comment now. */
3912 skip_to_end_of_comment (ip
, already_output
, 0);
3914 while (xp
!= ip
->bufp
)
3916 /* Delete or replace the slash. */
3917 else if (traditional
)
3926 /* Null-terminate the copy. */
3932 ip
->bufp
= resume_p
;
3934 /* Some directives should be written out for cc1 to process,
3935 just as if they were not defined. And sometimes we're copying
3936 definitions through. */
3938 if (!no_output
&& already_output
== 0
3940 || (kt
->type
== T_DEFINE
3941 && (dump_macros
== dump_names
3942 || dump_macros
== dump_definitions
)))) {
3945 /* Output directive name. */
3946 check_expand (op
, kt
->length
+ 1);
3948 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
3949 op
->bufp
+= kt
->length
;
3951 if (kt
->pass_thru
|| dump_macros
== dump_definitions
) {
3952 /* Output arguments. */
3954 check_expand (op
, len
);
3955 bcopy (buf
, (char *) op
->bufp
, len
);
3957 } else if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
3960 SKIP_WHITE_SPACE (xp
);
3962 while (is_idchar
[*xp
]) xp
++;
3964 check_expand (op
, len
+ 1);
3966 bcopy (yp
, op
->bufp
, len
);
3969 } /* Don't we need a newline or #line? */
3971 /* Call the appropriate directive handler. buf now points to
3972 either the appropriate place in the input buffer, or to
3973 the temp buffer if it was necessary to make one. cp
3974 points to the first char after the contents of the (possibly
3975 copied) directive, in either case. */
3976 (*kt
->func
) (buf
, cp
, op
, kt
);
3977 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3983 /* It is deliberate that we don't warn about undefined directives.
3984 That is the responsibility of cc1. */
3991 static struct tm
*timebuf
;
3993 time_t t
= time ((time_t *)0);
3994 timebuf
= localtime (&t
);
3999 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4000 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4004 * expand things like __FILE__. Place the expansion into the output
4005 * buffer *without* rescanning.
4009 special_symbol (hp
, op
)
4016 FILE_BUF
*ip
= NULL
;
4019 int paren
= 0; /* For special `defined' keyword */
4021 if (pcp_outfile
&& pcp_inside_if
4022 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4023 error ("Predefined macro `%s' used inside `#if' during precompilation",
4026 for (i
= indepth
; i
>= 0; i
--)
4027 if (instack
[i
].fname
!= NULL
) {
4032 error ("cccp error: not in any file?!");
4033 return; /* the show must go on */
4041 if (hp
->type
== T_FILE
)
4042 string
= ip
->nominal_fname
;
4044 string
= instack
[0].nominal_fname
;
4048 buf
= (char *) alloca (3 + 4 * strlen (string
));
4049 quote_string (buf
, string
);
4057 case T_INCLUDE_LEVEL
:
4059 for (i
= indepth
; i
>= 0; i
--)
4060 if (instack
[i
].fname
!= NULL
)
4063 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
4064 sprintf (buf
, "%d", true_indepth
- 1);
4068 buf
= (char *) alloca (3 + strlen (version_string
));
4069 sprintf (buf
, "\"%s\"", version_string
);
4072 #ifndef NO_BUILTIN_SIZE_TYPE
4078 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4079 case T_PTRDIFF_TYPE
:
4088 case T_USER_LABEL_PREFIX_TYPE
:
4089 buf
= USER_LABEL_PREFIX
;
4092 case T_REGISTER_PREFIX_TYPE
:
4093 buf
= REGISTER_PREFIX
;
4096 case T_IMMEDIATE_PREFIX_TYPE
:
4097 buf
= IMMEDIATE_PREFIX
;
4101 buf
= hp
->value
.cpval
;
4102 if (pcp_inside_if
&& pcp_outfile
)
4103 /* Output a precondition for this macro use */
4104 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4108 buf
= (char *) alloca (10);
4109 sprintf (buf
, "%d", ip
->lineno
);
4114 buf
= (char *) alloca (20);
4115 timebuf
= timestamp ();
4116 if (hp
->type
== T_DATE
)
4117 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4118 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4120 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
4124 case T_SPEC_DEFINED
:
4125 buf
= " 0 "; /* Assume symbol is not defined */
4126 ip
= &instack
[indepth
];
4127 SKIP_WHITE_SPACE (ip
->bufp
);
4128 if (*ip
->bufp
== '(') {
4130 ip
->bufp
++; /* Skip over the paren */
4131 SKIP_WHITE_SPACE (ip
->bufp
);
4134 if (!is_idstart
[*ip
->bufp
])
4136 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4137 if (pcp_outfile
&& pcp_inside_if
4138 && (hp
->type
== T_CONST
4139 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4140 /* Output a precondition for this macro use. */
4141 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4145 if (pcp_outfile
&& pcp_inside_if
) {
4146 /* Output a precondition for this macro use */
4147 U_CHAR
*cp
= ip
->bufp
;
4148 fprintf (pcp_outfile
, "#undef ");
4149 while (is_idchar
[*cp
]) /* Ick! */
4150 fputc (*cp
++, pcp_outfile
);
4151 putc ('\n', pcp_outfile
);
4153 while (is_idchar
[*ip
->bufp
])
4155 SKIP_WHITE_SPACE (ip
->bufp
);
4157 if (*ip
->bufp
!= ')')
4165 error ("`defined' without an identifier");
4169 error ("cccp error: invalid special hash type"); /* time for gdb */
4173 check_expand (op
, len
);
4174 bcopy (buf
, (char *) op
->bufp
, len
);
4181 /* Routines to handle #directives */
4183 /* Handle #include and #import.
4184 This function expects to see "fname" or <fname> on the input. */
4187 do_include (buf
, limit
, op
, keyword
)
4188 U_CHAR
*buf
, *limit
;
4190 struct directive
*keyword
;
4192 U_CHAR
*importing
= keyword
->type
== T_IMPORT
? (U_CHAR
*) "" : (U_CHAR
*) 0;
4193 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4194 static int import_warning
= 0;
4195 char *fname
; /* Dynamically allocated fname buffer */
4198 char *fbeg
, *fend
; /* Beginning and end of fname */
4201 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4202 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4203 struct file_name_list
*searchptr
= 0;
4206 int f
= -3; /* file number */
4207 struct include_file
*inc
= 0;
4209 int retried
= 0; /* Have already tried macro
4210 expanding the include line*/
4211 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4217 if (importing
&& warn_import
&& !inhibit_warnings
4218 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4220 warning ("using `#import' is not recommended");
4221 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
4222 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
4223 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
4224 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
4225 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
4226 fprintf (stderr
, " ... <real contents of file> ...\n");
4227 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
4228 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
4229 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
4230 fprintf (stderr
, "when it is equipped with such a conditional.\n");
4236 SKIP_WHITE_SPACE (fin
);
4237 /* Discard trailing whitespace so we can easily see
4238 if we have parsed all the significant chars we were given. */
4239 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4240 fbeg
= fend
= (char *) alloca (limit
- fin
);
4246 /* Copy the operand text, concatenating the strings. */
4248 while (fin
!= limit
) {
4249 while (fin
!= limit
&& *fin
!= '\"')
4254 /* If not at the end, there had better be another string. */
4255 /* Skip just horiz space, and don't go past limit. */
4256 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4257 if (fin
!= limit
&& *fin
== '\"')
4264 /* We have "filename". Figure out directory this source
4265 file is coming from and put it on the front of the list. */
4267 /* If -I- was specified, don't search current dir, only spec'd ones. */
4268 if (ignore_srcdir
) break;
4270 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4275 if ((nam
= fp
->nominal_fname
) != NULL
) {
4276 /* Found a named file. Figure out dir of the file,
4277 and put it in front of the search list. */
4278 dsp
= ((struct file_name_list
*)
4279 alloca (sizeof (struct file_name_list
) + strlen (nam
)));
4280 strcpy (dsp
->fname
, nam
);
4281 simplify_filename (dsp
->fname
);
4282 nam
= base_name (dsp
->fname
);
4284 /* But for efficiency's sake, do not insert the dir
4285 if it matches the search list's first dir. */
4286 dsp
->next
= search_start
;
4287 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4289 n
= nam
- dsp
->fname
;
4290 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4291 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4293 dsp
[0].got_name_map
= 0;
4301 while (fin
!= limit
&& *fin
!= '>')
4303 if (*fin
== '>' && fin
+ 1 == limit
) {
4305 /* If -I-, start with the first -I dir after the -I-. */
4306 search_start
= first_bracket_include
;
4314 * Support '#include xyz' like VAX-C to allow for easy use of all the
4315 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4316 * code from case '<' is repeated here) and generates a warning.
4317 * (Note: macro expansion of `xyz' takes precedence.)
4319 if (retried
&& isalpha(*(U_CHAR
*)(--fbeg
))) {
4320 while (fin
!= limit
&& (!isspace(*fin
)))
4322 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4325 /* If -I-, start with the first -I dir after the -I-. */
4326 search_start
= first_bracket_include
;
4334 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4337 /* Expand buffer and then remove any newline markers.
4338 We can't just tell expand_to_temp_buffer to omit the markers,
4339 since it would put extra spaces in include file names. */
4342 trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4344 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4346 while (src
!= trybuf
.bufp
) {
4347 switch ((*limit
++ = *src
++)) {
4356 U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4357 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4371 /* For #include_next, skip in the search path
4372 past the dir in which the containing file was found. */
4375 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4376 if (fp
->fname
!= NULL
) {
4377 /* fp->dir is null if the containing file was specified
4378 with an absolute file name. In that case, don't skip anything. */
4380 search_start
= fp
->dir
->next
;
4386 flen
= simplify_filename (fbeg
);
4390 error ("empty file name in `#%s'", keyword
->name
);
4394 /* Allocate this permanently, because it gets stored in the definitions
4396 fname
= xmalloc (max_include_len
+ flen
+ 1);
4397 /* + 1 above for terminating null. */
4399 system_include_depth
+= angle_brackets
;
4401 /* If specified file name is absolute, just open it. */
4403 if (absolute_filename (fbeg
)) {
4404 strcpy (fname
, fbeg
);
4405 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4409 struct bypass_dir
*next
;
4411 struct file_name_list
*searchptr
;
4412 } **bypass_slot
= 0;
4414 /* Search directory path, trying to open the file.
4415 Copy each filename tried into FNAME. */
4417 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4419 if (searchptr
== first_bracket_include
) {
4420 /* Go to bypass directory if we know we've seen this file before. */
4421 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4422 struct bypass_dir
*p
;
4423 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4425 for (p
= *bypass_slot
; p
; p
= p
->next
)
4426 if (!strcmp (fbeg
, p
->fname
)) {
4427 searchptr
= p
->searchptr
;
4433 strcpy (fname
, searchptr
->fname
);
4434 strcat (fname
, fbeg
);
4436 /* Change this 1/2 Unix 1/2 VMS file specification into a
4437 full VMS file specification */
4438 if (searchptr
->fname
[0]) {
4439 /* Fix up the filename */
4440 hack_vms_include_specification (fname
);
4442 /* This is a normal VMS filespec, so use it unchanged. */
4443 strcpy (fname
, fbeg
);
4444 /* if it's '#include filename', add the missing .h */
4445 if (index(fname
,'.')==NULL
) {
4446 strcat (fname
, ".h");
4450 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4452 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4453 /* This is the first time we found this include file,
4454 and we found it after first_bracket_include.
4455 Record its location so that we can bypass to here next time. */
4456 struct bypass_dir
*p
4457 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4458 p
->next
= *bypass_slot
;
4459 p
->fname
= fname
+ strlen (searchptr
->fname
);
4460 p
->searchptr
= searchptr
;
4466 /* Our VMS hacks can produce invalid filespecs, so don't worry
4467 about errors other than EACCES. */
4468 if (errno
== EACCES
)
4471 if (errno
!= ENOENT
)
4481 /* The file was already included. */
4483 /* If generating dependencies and -MG was specified, we assume missing
4484 files are leaf files, living in the same directory as the source file
4485 or other similar place; these missing files may be generated from
4486 other files and may not exist yet (eg: y.tab.h). */
4487 } else if (print_deps_missing_files
4488 && (system_include_depth
!= 0) < print_deps
)
4490 /* If it was requested as a system header file,
4491 then assume it belongs in the first place to look for such. */
4495 char *p
= (char *) alloca (strlen (search_start
->fname
)
4496 + strlen (fbeg
) + 1);
4497 strcpy (p
, search_start
->fname
);
4499 deps_output (p
, ' ');
4504 /* Otherwise, omit the directory, as if the file existed
4505 in the directory with the source. */
4506 deps_output (fbeg
, ' ');
4509 /* If -M was specified, and this header file won't be added to the
4510 dependency list, then don't count this as an error, because we can
4511 still produce correct output. Otherwise, we can't produce correct
4512 output, because there may be dependencies we need inside the missing
4513 file, and we don't know what directory this missing file exists in. */
4514 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4515 warning ("No include path in which to find %s", fbeg
);
4517 error_from_errno (fbeg
);
4519 error ("No include path in which to find %s", fbeg
);
4523 /* Actually process the file. */
4525 pcftry
= (char *) alloca (strlen (fname
) + 30);
4532 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4534 pcf
= open (pcftry
, O_RDONLY
, 0666);
4539 if (fstat (pcf
, &s
) != 0)
4540 pfatal_with_name (pcftry
);
4541 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4542 || inc
->st
.st_dev
!= s
.st_dev
)
4544 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4545 /* Don't need it any more. */
4550 /* Don't need it at all. */
4555 } while (pcf
!= -1 && !pcfbuf
);
4558 /* Actually process the file */
4560 pcfname
= xmalloc (strlen (pcftry
) + 1);
4561 strcpy (pcfname
, pcftry
);
4562 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) pcfbuflimit
,
4563 (U_CHAR
*) fname
, op
);
4566 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4569 system_include_depth
-= angle_brackets
;
4574 /* Return nonzero if the given FILENAME is an absolute pathname which
4575 designates a file within one of the known "system" include file
4576 directories. We assume here that if the given FILENAME looks like
4577 it is the name of a file which resides either directly in a "system"
4578 include file directory, or within any subdirectory thereof, then the
4579 given file must be a "system" include file. This function tells us
4580 if we should suppress pedantic errors/warnings for the given FILENAME.
4582 The value is 2 if the file is a C-language system header file
4583 for which C++ should (on most systems) assume `extern "C"'. */
4586 is_system_include (filename
)
4587 register char *filename
;
4589 struct file_name_list
*searchptr
;
4591 for (searchptr
= first_system_include
; searchptr
;
4592 searchptr
= searchptr
->next
)
4593 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4594 return searchptr
->c_system_include_path
+ 1;
4598 /* Yield the non-directory suffix of a file name. */
4606 #if defined (__MSDOS__) || defined (_WIN32)
4607 if (isalpha (s
[0]) && s
[1] == ':') s
+= 2;
4610 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4611 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4612 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4616 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4617 #ifdef DIR_SEPARATOR
4618 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4623 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4625 absolute_filename (filename
)
4628 #if defined (__MSDOS__) || defined (_WIN32)
4629 if (isalpha (filename
[0]) && filename
[1] == ':') filename
+= 2;
4631 if (filename
[0] == '/') return 1;
4632 #ifdef DIR_SEPARATOR
4633 if (filename
[0] == DIR_SEPARATOR
) return 1;
4638 /* Remove unnecessary characters from FILENAME in place,
4639 to avoid unnecessary filename aliasing.
4640 Return the length of the resulting string.
4642 Do only the simplifications allowed by Posix.
4643 It is OK to miss simplifications on non-Posix hosts,
4644 since this merely leads to suboptimial results. */
4647 simplify_filename (filename
)
4650 register char *from
= filename
;
4651 register char *to
= filename
;
4654 /* Remove redundant initial /s. */
4657 if (*++from
== '/') {
4658 if (*++from
== '/') {
4659 /* 3 or more initial /s are equivalent to 1 /. */
4660 while (*++from
== '/')
4663 /* On some hosts // differs from /; Posix allows this. */
4664 static int slashslash_vs_slash
;
4665 if (slashslash_vs_slash
== 0) {
4667 slashslash_vs_slash
= ((stat ("/", &s1
) == 0 && stat ("//", &s2
) == 0
4668 && INO_T_EQ (s1
.st_ino
, s2
.st_ino
)
4669 && s1
.st_dev
== s2
.st_dev
)
4672 if (slashslash_vs_slash
< 0)
4680 if (from
[0] == '.' && from
[1] == '/')
4683 /* Copy this component and trailing /, if any. */
4684 while ((*to
++ = *from
++) != '/') {
4686 /* Trim . component at end of nonempty name. */
4687 to
-= filename
<= to
- 3 && to
[-3] == '/' && to
[-2] == '.';
4689 /* Trim unnecessary trailing /s. */
4690 while (to0
< --to
&& to
[-1] == '/')
4694 return to
- filename
;
4699 /* Skip /s after a /. */
4700 while (*from
== '/')
4705 /* The file_name_map structure holds a mapping of file names for a
4706 particular directory. This mapping is read from the file named
4707 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4708 map filenames on a file system with severe filename restrictions,
4709 such as DOS. The format of the file name map file is just a series
4710 of lines with two tokens on each line. The first token is the name
4711 to map, and the second token is the actual name to use. */
4713 struct file_name_map
4715 struct file_name_map
*map_next
;
4720 #define FILE_NAME_MAP_FILE "header.gcc"
4722 /* Read a space delimited string of unlimited length from a stdio
4726 read_filename_string (ch
, f
)
4734 set
= alloc
= xmalloc (len
+ 1);
4738 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
4740 if (set
- alloc
== len
)
4743 alloc
= xrealloc (alloc
, len
+ 1);
4744 set
= alloc
+ len
/ 2;
4754 /* Read the file name map file for DIRNAME.
4755 If DIRNAME is empty, read the map file for the working directory;
4756 otherwise DIRNAME must end in '/'. */
4758 static struct file_name_map
*
4759 read_name_map (dirname
)
4762 /* This structure holds a linked list of file name maps, one per
4764 struct file_name_map_list
4766 struct file_name_map_list
*map_list_next
;
4767 char *map_list_name
;
4768 struct file_name_map
*map_list_map
;
4770 static struct file_name_map_list
*map_list
;
4771 register struct file_name_map_list
*map_list_ptr
;
4776 for (map_list_ptr
= map_list
; map_list_ptr
;
4777 map_list_ptr
= map_list_ptr
->map_list_next
)
4778 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
4779 return map_list_ptr
->map_list_map
;
4781 map_list_ptr
= ((struct file_name_map_list
*)
4782 xmalloc (sizeof (struct file_name_map_list
)));
4783 map_list_ptr
->map_list_name
= savestring (dirname
);
4784 map_list_ptr
->map_list_map
= NULL
;
4786 dirlen
= strlen (dirname
);
4787 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
4788 strcpy (name
, dirname
);
4789 strcat (name
, FILE_NAME_MAP_FILE
);
4790 f
= fopen (name
, "r");
4792 map_list_ptr
->map_list_map
= NULL
;
4797 while ((ch
= getc (f
)) != EOF
)
4800 struct file_name_map
*ptr
;
4805 from
= read_filename_string (ch
, f
);
4806 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
4808 to
= read_filename_string (ch
, f
);
4810 simplify_filename (from
);
4811 tolen
= simplify_filename (to
);
4813 ptr
= ((struct file_name_map
*)
4814 xmalloc (sizeof (struct file_name_map
)));
4815 ptr
->map_from
= from
;
4817 /* Make the real filename absolute. */
4818 if (absolute_filename (to
))
4822 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
4823 strcpy (ptr
->map_to
, dirname
);
4824 strcat (ptr
->map_to
, to
);
4828 ptr
->map_next
= map_list_ptr
->map_list_map
;
4829 map_list_ptr
->map_list_map
= ptr
;
4831 while ((ch
= getc (f
)) != '\n')
4838 map_list_ptr
->map_list_next
= map_list
;
4839 map_list
= map_list_ptr
;
4841 return map_list_ptr
->map_list_map
;
4844 /* Try to open include file FILENAME. SEARCHPTR is the directory
4845 being tried from the include file search path.
4846 IMPORTING is "" if we are importing, null otherwise.
4847 Return -2 if found, either a matching name or a matching inode.
4848 Otherwise, open the file and return a file descriptor if successful
4849 or -1 if unsuccessful.
4850 Unless unsuccessful, put a descriptor of the included file into *PINC.
4851 This function maps filenames on file systems based on information read by
4855 open_include_file (filename
, searchptr
, importing
, pinc
)
4857 struct file_name_list
*searchptr
;
4859 struct include_file
**pinc
;
4861 char *fname
= remap_include_file (filename
, searchptr
);
4864 /* Look up FNAME in include_hashtab. */
4865 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
4868 struct include_file
*inc
, *head
= *phead
;
4869 for (inc
= head
; inc
; inc
= inc
->next
)
4870 if (!strcmp (fname
, inc
->fname
))
4874 || ! inc
->control_macro
4875 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
4877 fd
= open (fname
, O_RDONLY
, 0);
4883 /* FNAME was not in include_hashtab; insert a new entry. */
4884 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
4887 inc
->control_macro
= 0;
4888 inc
->deps_output
= 0;
4889 if (fstat (fd
, &inc
->st
) != 0)
4890 pfatal_with_name (fname
);
4893 /* Look for another file with the same inode and device. */
4894 if (lookup_ino_include (inc
)
4895 && inc
->control_macro
4896 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
4902 /* For -M, add this file to the dependencies. */
4903 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
4904 inc
->deps_output
= 1;
4905 deps_output (fname
, ' ');
4908 /* Handle -H option. */
4909 if (print_include_names
)
4910 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
4914 inc
->control_macro
= importing
;
4920 /* Return the remapped name of the the include file FILENAME.
4921 SEARCHPTR is the directory being tried from the include file path. */
4924 remap_include_file (filename
, searchptr
)
4926 struct file_name_list
*searchptr
;
4928 register struct file_name_map
*map
;
4929 register char *from
;
4933 if (! searchptr
->got_name_map
)
4935 searchptr
->name_map
= read_name_map (searchptr
->fname
);
4936 searchptr
->got_name_map
= 1;
4939 /* Check the mapping for the directory we are using. */
4940 from
= filename
+ strlen (searchptr
->fname
);
4941 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
4942 if (! strcmp (map
->map_from
, from
))
4946 from
= base_name (filename
);
4948 if (from
!= filename
|| !searchptr
)
4950 /* Try to find a mapping file for the particular directory we are
4951 looking in. Thus #include <sys/types.h> will look up sys/types.h
4952 in /usr/include/header.gcc and look up types.h in
4953 /usr/include/sys/header.gcc. */
4955 char *dir
= (char *) alloca (from
- filename
+ 1);
4956 bcopy (filename
, dir
, from
- filename
);
4957 dir
[from
- filename
] = '\0';
4959 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
4960 if (! strcmp (map
->map_from
, from
))
4967 /* Insert INC into the include file table, hashed by device and inode number.
4968 If a file with different name but same dev+ino was already in the table,
4969 return 1 and set INC's control macro to the already-known macro. */
4972 lookup_ino_include (inc
)
4973 struct include_file
*inc
;
4975 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
4976 % INCLUDE_HASHSIZE
);
4977 struct include_file
*i
= include_ino_hashtab
[hash
];
4979 include_ino_hashtab
[hash
] = inc
;
4981 for (; i
; i
= i
->next_ino
)
4982 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
4983 && inc
->st
.st_dev
== i
->st
.st_dev
) {
4984 inc
->control_macro
= i
->control_macro
;
4991 /* Process file descriptor F, which corresponds to include file INC,
4993 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
4994 "system" include directories (as decided by the `is_system_include'
4996 DIRPTR is the link in the dir path through which this file was found,
4997 or 0 if the file name was absolute. */
5000 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5002 struct include_file
*inc
;
5004 int system_header_p
;
5005 struct file_name_list
*dirptr
;
5007 char *fname
= inc
->fname
;
5009 FILE_BUF
*fp
; /* For input stack frame */
5010 int missing_newline
= 0;
5012 CHECK_DEPTH (return;);
5014 fp
= &instack
[indepth
+ 1];
5015 bzero ((char *) fp
, sizeof (FILE_BUF
));
5016 fp
->nominal_fname
= fp
->fname
= fname
;
5020 fp
->if_stack
= if_stack
;
5021 fp
->system_header_p
= system_header_p
;
5024 if (S_ISREG (inc
->st
.st_mode
)) {
5025 fp
->buf
= (U_CHAR
*) xmalloc (inc
->st
.st_size
+ 2);
5028 /* Read the file contents, knowing that inc->st.st_size is an upper bound
5029 on the number of bytes we can read. */
5030 fp
->length
= safe_read (f
, (char *) fp
->buf
, inc
->st
.st_size
);
5031 if (fp
->length
< 0) goto nope
;
5033 else if (S_ISDIR (inc
->st
.st_mode
)) {
5034 error ("directory `%s' specified in #include", fname
);
5038 /* Cannot count its file size before reading.
5039 First read the entire file into heap and
5040 copy them into buffer on stack. */
5045 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5048 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5050 goto nope
; /* error! */
5052 if (st_size
!= bsize
)
5053 break; /* End of file */
5055 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5058 fp
->length
= st_size
;
5061 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5062 /* Backslash-newline at end is not good enough. */
5063 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5064 fp
->buf
[fp
->length
++] = '\n';
5065 missing_newline
= 1;
5067 fp
->buf
[fp
->length
] = '\0';
5069 /* Close descriptor now, so nesting does not use lots of descriptors. */
5072 /* Must do this before calling trigraph_pcp, so that the correct file name
5073 will be printed in warning messages. */
5076 input_file_stack_tick
++;
5081 output_line_directive (fp
, op
, 0, enter_file
);
5084 if (missing_newline
)
5087 if (pedantic
&& missing_newline
)
5088 pedwarn ("file does not end in newline");
5091 input_file_stack_tick
++;
5092 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5098 perror_with_name (fname
);
5103 /* Record that inclusion of the include file INC
5104 should be controlled by the macro named MACRO_NAME.
5105 This means that trying to include the file again
5106 will do something if that macro is defined. */
5109 record_control_macro (inc
, macro_name
)
5110 struct include_file
*inc
;
5113 if (!inc
->control_macro
|| inc
->control_macro
[0])
5114 inc
->control_macro
= macro_name
;
5117 /* Load the specified precompiled header into core, and verify its
5118 preconditions. PCF indicates the file descriptor to read, which must
5119 be a regular file. *ST is its file status.
5120 FNAME indicates the file name of the original header.
5121 *LIMIT will be set to an address one past the end of the file.
5122 If the preconditions of the file are not satisfied, the buffer is
5123 freed and we return 0. If the preconditions are satisfied, return
5124 the address of the buffer following the preconditions. The buffer, in
5125 this case, should never be freed because various pieces of it will
5126 be referred to until all precompiled strings are output at the end of
5130 check_precompiled (pcf
, st
, fname
, limit
)
5143 if (S_ISREG (st
->st_mode
))
5145 buf
= xmalloc (st
->st_size
+ 2);
5146 length
= safe_read (pcf
, buf
, st
->st_size
);
5153 if (length
> 0 && buf
[length
-1] != '\n')
5154 buf
[length
++] = '\n';
5157 *limit
= buf
+ length
;
5159 /* File is in core. Check the preconditions. */
5160 if (!check_preconditions (buf
))
5162 for (cp
= buf
; *cp
; cp
++)
5165 fprintf (stderr
, "Using preinclude %s\n", fname
);
5171 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5177 /* PREC (null terminated) points to the preconditions of a
5178 precompiled header. These are a series of #define and #undef
5179 lines which must match the current contents of the hash
5182 check_preconditions (prec
)
5189 lineend
= index (prec
, '\n');
5191 if (*prec
++ != '#') {
5192 error ("Bad format encountered while reading precompiled file");
5195 if (!strncmp (prec
, "define", 6)) {
5199 mdef
= create_definition ((U_CHAR
*) prec
, (U_CHAR
*) lineend
, NULL_PTR
);
5204 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5205 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5206 || (hp
->type
== T_MACRO
5207 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5208 && (mdef
.defn
->length
!= 2
5209 || mdef
.defn
->expansion
[0] != '\n'
5210 || mdef
.defn
->expansion
[1] != ' ')))
5212 } else if (!strncmp (prec
, "undef", 5)) {
5217 while (is_hor_space
[(U_CHAR
) *prec
])
5220 while (is_idchar
[(U_CHAR
) *prec
])
5224 if (lookup ((U_CHAR
*) name
, len
, -1))
5227 error ("Bad format encountered while reading precompiled file");
5232 /* They all passed successfully */
5236 /* Process the main body of a precompiled file. BUF points to the
5237 string section of the file, following the preconditions. LIMIT is one
5238 character past the end. NAME is the name of the file being read
5239 in. OP is the main output buffer */
5241 pcfinclude (buf
, limit
, name
, op
)
5242 U_CHAR
*buf
, *limit
, *name
;
5249 /* First in the file comes 4 bytes indicating the number of strings, */
5250 /* in network byte order. (MSB first). */
5252 nstrings
= (nstrings
<< 8) | *cp
++;
5253 nstrings
= (nstrings
<< 8) | *cp
++;
5254 nstrings
= (nstrings
<< 8) | *cp
++;
5256 /* Looping over each string... */
5257 while (nstrings
--) {
5258 U_CHAR
*string_start
;
5259 U_CHAR
*endofthiskey
;
5263 /* Each string starts with a STRINGDEF structure (str), followed */
5264 /* by the text of the string (string_start) */
5266 /* First skip to a longword boundary */
5267 /* ??? Why a 4-byte boundary? On all machines? */
5268 /* NOTE: This works correctly even if HOST_WIDE_INT
5269 is narrower than a pointer.
5270 Do not try risky measures here to get another type to use!
5271 Do not include stddef.h--it will fail! */
5272 if ((HOST_WIDE_INT
) cp
& 3)
5273 cp
+= 4 - ((HOST_WIDE_INT
) cp
& 3);
5275 /* Now get the string. */
5276 str
= (STRINGDEF
*) (GENERIC_PTR
) cp
;
5277 string_start
= cp
+= sizeof (STRINGDEF
);
5279 for (; *cp
; cp
++) /* skip the string */
5282 /* We need to macro expand the string here to ensure that the
5283 proper definition environment is in place. If it were only
5284 expanded when we find out it is needed, macros necessary for
5285 its proper expansion might have had their definitions changed. */
5286 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5287 /* Lineno is already set in the precompiled file */
5288 str
->contents
= tmpbuf
.buf
;
5289 str
->len
= tmpbuf
.length
;
5291 str
->filename
= name
;
5292 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5295 *stringlist_tailp
= str
;
5296 stringlist_tailp
= &str
->chain
;
5298 /* Next comes a fourbyte number indicating the number of keys */
5299 /* for this string. */
5301 nkeys
= (nkeys
<< 8) | *cp
++;
5302 nkeys
= (nkeys
<< 8) | *cp
++;
5303 nkeys
= (nkeys
<< 8) | *cp
++;
5305 /* If this number is -1, then the string is mandatory. */
5309 /* Otherwise, for each key, */
5310 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5311 KEYDEF
*kp
= (KEYDEF
*) (GENERIC_PTR
) cp
;
5314 /* It starts with a KEYDEF structure */
5315 cp
+= sizeof (KEYDEF
);
5317 /* Find the end of the key. At the end of this for loop we
5318 advance CP to the start of the next key using this variable. */
5319 endofthiskey
= cp
+ strlen ((char *) cp
);
5322 /* Expand the key, and enter it into the hash table. */
5323 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5324 tmpbuf
.bufp
= tmpbuf
.buf
;
5326 while (is_hor_space
[*tmpbuf
.bufp
])
5328 if (!is_idstart
[*tmpbuf
.bufp
]
5329 || tmpbuf
.bufp
== tmpbuf
.buf
+ tmpbuf
.length
) {
5334 hp
= lookup (tmpbuf
.bufp
, -1, -1);
5337 install (tmpbuf
.bufp
, -1, T_PCSTRING
, (char *) kp
, -1);
5339 else if (hp
->type
== T_PCSTRING
) {
5340 kp
->chain
= hp
->value
.keydef
;
5341 hp
->value
.keydef
= kp
;
5347 /* This output_line_directive serves to switch us back to the current
5348 input file in case some of these strings get output (which will
5349 result in line directives for the header file being output). */
5350 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5353 /* Called from rescan when it hits a key for strings. Mark them all */
5354 /* used and clean up. */
5361 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5362 kp
->str
->writeflag
= 1;
5366 /* Write the output, interspersing precompiled strings in their */
5367 /* appropriate places. */
5371 STRINGDEF
*next_string
;
5372 U_CHAR
*cur_buf_loc
;
5373 int line_directive_len
= 80;
5374 char *line_directive
= xmalloc (line_directive_len
);
5377 /* In each run through the loop, either cur_buf_loc == */
5378 /* next_string_loc, in which case we print a series of strings, or */
5379 /* it is less than next_string_loc, in which case we write some of */
5381 cur_buf_loc
= outbuf
.buf
;
5382 next_string
= stringlist
;
5384 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5386 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5387 if (next_string
->writeflag
) {
5388 len
= 4 * strlen ((char *) next_string
->filename
) + 32;
5389 while (len
> line_directive_len
)
5390 line_directive
= xrealloc (line_directive
,
5391 line_directive_len
*= 2);
5392 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5393 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5394 (char *) next_string
->filename
),
5396 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5397 safe_write (fileno (stdout
),
5398 (char *) next_string
->contents
, next_string
->len
);
5400 next_string
= next_string
->chain
;
5404 ? (next_string
->output_mark
5405 - (cur_buf_loc
- outbuf
.buf
))
5406 : outbuf
.bufp
- cur_buf_loc
);
5408 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5412 free (line_directive
);
5415 /* Pass a directive through to the output file.
5416 BUF points to the contents of the directive, as a contiguous string.
5417 LIMIT points to the first character past the end of the directive.
5418 KEYWORD is the keyword-table entry for the directive. */
5421 pass_thru_directive (buf
, limit
, op
, keyword
)
5422 U_CHAR
*buf
, *limit
;
5424 struct directive
*keyword
;
5426 register unsigned keyword_length
= keyword
->length
;
5428 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5430 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5431 op
->bufp
+= keyword_length
;
5432 if (limit
!= buf
&& buf
[0] != ' ')
5434 bcopy ((char *) buf
, (char *) op
->bufp
, limit
- buf
);
5435 op
->bufp
+= (limit
- buf
);
5438 /* Count the line we have just made in the output,
5439 to get in sync properly. */
5444 /* The arglist structure is built by do_define to tell
5445 collect_definition where the argument names begin. That
5446 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5447 would contain pointers to the strings x, y, and z.
5448 Collect_definition would then build a DEFINITION node,
5449 with reflist nodes pointing to the places x, y, and z had
5450 appeared. So the arglist is just convenience data passed
5451 between these two routines. It is not kept around after
5452 the current #define has been processed and entered into the
5456 struct arglist
*next
;
5463 /* Create a DEFINITION node from a #define directive. Arguments are
5464 as for do_define. */
5466 create_definition (buf
, limit
, op
)
5467 U_CHAR
*buf
, *limit
;
5470 U_CHAR
*bp
; /* temp ptr into input buffer */
5471 U_CHAR
*symname
; /* remember where symbol name starts */
5472 int sym_length
; /* and how long it is */
5473 int line
= instack
[indepth
].lineno
;
5474 char *file
= instack
[indepth
].nominal_fname
;
5478 int arglengths
= 0; /* Accumulate lengths of arg names
5479 plus number of args. */
5484 while (is_hor_space
[*bp
])
5487 symname
= bp
; /* remember where it starts */
5488 sym_length
= check_macro_name (bp
, "macro");
5491 /* Lossage will occur if identifiers or control keywords are broken
5492 across lines using backslash. This is not the right place to take
5496 struct arglist
*arg_ptrs
= NULL
;
5499 bp
++; /* skip '(' */
5500 SKIP_WHITE_SPACE (bp
);
5502 /* Loop over macro argument names. */
5503 while (*bp
!= ')') {
5504 struct arglist
*temp
;
5506 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5508 temp
->next
= arg_ptrs
;
5509 temp
->argno
= argno
++;
5510 temp
->rest_args
= 0;
5514 pedwarn ("another parameter follows `%s'",
5517 if (!is_idstart
[*bp
])
5518 pedwarn ("invalid character in macro parameter name");
5520 /* Find the end of the arg name. */
5521 while (is_idchar
[*bp
]) {
5523 /* do we have a "special" rest-args extension here? */
5524 if (limit
- bp
> REST_EXTENSION_LENGTH
&&
5525 bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5527 temp
->rest_args
= 1;
5531 temp
->length
= bp
- temp
->name
;
5533 bp
+= REST_EXTENSION_LENGTH
;
5534 arglengths
+= temp
->length
+ 2;
5535 SKIP_WHITE_SPACE (bp
);
5536 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5537 error ("badly punctuated parameter list in `#define'");
5542 SKIP_WHITE_SPACE (bp
);
5543 /* A comma at this point can only be followed by an identifier. */
5544 if (!is_idstart
[*bp
]) {
5545 error ("badly punctuated parameter list in `#define'");
5550 error ("unterminated parameter list in `#define'");
5554 struct arglist
*otemp
;
5556 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5557 if (temp
->length
== otemp
->length
&&
5558 bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
5559 error ("duplicate argument name `%.*s' in `#define'",
5560 temp
->length
, temp
->name
);
5566 ++bp
; /* skip paren */
5567 SKIP_WHITE_SPACE (bp
);
5568 /* now everything from bp before limit is the definition. */
5569 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5570 defn
->rest_args
= rest_args
;
5572 /* Now set defn->args.argnames to the result of concatenating
5573 the argument names in reverse order
5574 with comma-space between them. */
5575 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5577 struct arglist
*temp
;
5579 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5580 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5582 if (temp
->next
!= 0) {
5583 defn
->args
.argnames
[i
++] = ',';
5584 defn
->args
.argnames
[i
++] = ' ';
5587 defn
->args
.argnames
[i
] = 0;
5590 /* Simple expansion or empty definition. */
5594 if (is_hor_space
[*bp
]) {
5596 SKIP_WHITE_SPACE (bp
);
5599 case '!': case '"': case '#': case '%': case '&': case '\'':
5600 case ')': case '*': case '+': case ',': case '-': case '.':
5601 case '/': case ':': case ';': case '<': case '=': case '>':
5602 case '?': case '[': case '\\': case ']': case '^': case '{':
5603 case '|': case '}': case '~':
5604 warning ("missing white space after `#define %.*s'",
5605 sym_length
, symname
);
5609 pedwarn ("missing white space after `#define %.*s'",
5610 sym_length
, symname
);
5615 /* Now everything from bp before limit is the definition. */
5616 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5617 defn
->args
.argnames
= (U_CHAR
*) "";
5623 /* OP is null if this is a predefinition */
5624 defn
->predefined
= !op
;
5626 mdef
.symnam
= symname
;
5627 mdef
.symlen
= sym_length
;
5636 /* Process a #define directive.
5637 BUF points to the contents of the #define directive, as a contiguous string.
5638 LIMIT points to the first character past the end of the definition.
5639 KEYWORD is the keyword-table entry for #define. */
5642 do_define (buf
, limit
, op
, keyword
)
5643 U_CHAR
*buf
, *limit
;
5645 struct directive
*keyword
;
5650 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5651 if (pcp_outfile
&& op
)
5652 pass_thru_directive (buf
, limit
, op
, keyword
);
5654 mdef
= create_definition (buf
, limit
, op
);
5658 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
5662 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
5664 /* Redefining a precompiled key is ok. */
5665 if (hp
->type
== T_PCSTRING
)
5667 /* Redefining a macro is ok if the definitions are the same. */
5668 else if (hp
->type
== T_MACRO
)
5669 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
5670 /* Redefining a constant is ok with -D. */
5671 else if (hp
->type
== T_CONST
)
5672 ok
= ! done_initializing
;
5673 /* Print the warning if it's not ok. */
5675 /* If we are passing through #define and #undef directives, do
5676 that for this re-definition now. */
5677 if (debug_output
&& op
)
5678 pass_thru_directive (buf
, limit
, op
, keyword
);
5680 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
5681 if (hp
->type
== T_MACRO
)
5682 pedwarn_with_file_and_line (hp
->value
.defn
->file
, hp
->value
.defn
->line
,
5683 "this is the location of the previous definition");
5685 /* Replace the old definition. */
5687 hp
->value
.defn
= mdef
.defn
;
5689 /* If we are passing through #define and #undef directives, do
5690 that for this new definition now. */
5691 if (debug_output
&& op
)
5692 pass_thru_directive (buf
, limit
, op
, keyword
);
5693 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
,
5694 (char *) mdef
.defn
, hashcode
);
5705 /* Check a purported macro name SYMNAME, and yield its length.
5706 USAGE is the kind of name this is intended for. */
5709 check_macro_name (symname
, usage
)
5716 for (p
= symname
; is_idchar
[*p
]; p
++)
5718 sym_length
= p
- symname
;
5719 if (sym_length
== 0)
5720 error ("invalid %s name", usage
);
5721 else if (!is_idstart
[*symname
]
5722 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
5723 error ("invalid %s name `%.*s'", usage
, sym_length
, symname
);
5728 * return zero if two DEFINITIONs are isomorphic
5731 compare_defs (d1
, d2
)
5732 DEFINITION
*d1
, *d2
;
5734 register struct reflist
*a1
, *a2
;
5735 register U_CHAR
*p1
= d1
->expansion
;
5736 register U_CHAR
*p2
= d2
->expansion
;
5739 if (d1
->nargs
!= d2
->nargs
)
5741 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
5743 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
5744 a1
= a1
->next
, a2
= a2
->next
) {
5745 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
5746 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
5747 || a1
->argno
!= a2
->argno
5748 || a1
->stringify
!= a2
->stringify
5749 || a1
->raw_before
!= a2
->raw_before
5750 || a1
->raw_after
!= a2
->raw_after
)
5758 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
5759 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
5764 /* Return 1 if two parts of two macro definitions are effectively different.
5765 One of the parts starts at BEG1 and has LEN1 chars;
5766 the other has LEN2 chars at BEG2.
5767 Any sequence of whitespace matches any other sequence of whitespace.
5768 FIRST means these parts are the first of a macro definition;
5769 so ignore leading whitespace entirely.
5770 LAST means these parts are the last of a macro definition;
5771 so ignore trailing whitespace entirely. */
5774 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
5776 U_CHAR
*beg1
, *beg2
;
5780 register U_CHAR
*end1
= beg1
+ len1
;
5781 register U_CHAR
*end2
= beg2
+ len2
;
5783 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5784 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5787 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
5788 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
5790 while (beg1
!= end1
&& beg2
!= end2
) {
5791 if (is_space
[*beg1
] && is_space
[*beg2
]) {
5792 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5793 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5794 } else if (*beg1
== *beg2
) {
5798 return (beg1
!= end1
) || (beg2
!= end2
);
5801 /* Read a replacement list for a macro with parameters.
5802 Build the DEFINITION structure.
5803 Reads characters of text starting at BUF until END.
5804 ARGLIST specifies the formal parameters to look for
5805 in the text of the definition; NARGS is the number of args
5806 in that list, or -1 for a macro name that wants no argument list.
5807 MACRONAME is the macro name itself (so we can avoid recursive expansion)
5808 and NAMELEN is its length in characters.
5810 Note that comments, backslash-newlines, and leading white space
5811 have already been deleted from the argument. */
5813 /* If there is no trailing whitespace, a Newline Space is added at the end
5814 to prevent concatenation that would be contrary to the standard. */
5817 collect_expansion (buf
, end
, nargs
, arglist
)
5820 struct arglist
*arglist
;
5823 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
5824 struct reflist
*endpat
= NULL
;
5825 /* Pointer to first nonspace after last ## seen. */
5827 /* Pointer to first nonspace after last single-# seen. */
5828 U_CHAR
*stringify
= 0;
5829 /* How those tokens were spelled. */
5830 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
5831 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
5833 int expected_delimiter
= '\0';
5835 /* Scan thru the replacement list, ignoring comments and quoted
5836 strings, picking up on the macro calls. It does a linear search
5837 thru the arg list on every potential symbol. Profiling might say
5838 that something smarter should happen. */
5843 /* Find the beginning of the trailing whitespace. */
5846 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
5848 /* Allocate space for the text in the macro definition.
5849 Each input char may or may not need 1 byte,
5850 so this is an upper bound.
5851 The extra 3 are for invented trailing newline-marker and final null. */
5852 maxsize
= (sizeof (DEFINITION
)
5854 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
5856 defn
->nargs
= nargs
;
5857 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
5862 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
5863 error ("`##' at start of macro definition");
5864 p
+= p
[0] == '#' ? 2 : 4;
5867 /* Process the main body of the definition. */
5869 int skipped_arg
= 0;
5870 register U_CHAR c
= *p
++;
5878 if (expected_delimiter
!= '\0') {
5879 if (c
== expected_delimiter
)
5880 expected_delimiter
= '\0';
5882 expected_delimiter
= c
;
5886 if (p
< limit
&& expected_delimiter
) {
5887 /* In a string, backslash goes through
5888 and makes next char ordinary. */
5894 if (!expected_delimiter
&& *p
== ':') {
5895 /* %: is not a digraph if preceded by an odd number of '<'s. */
5897 while (buf
< p0
&& p0
[-1] == '<')
5900 /* Treat %:%: as ## and %: as #. */
5901 if (p
[1] == '%' && p
[2] == ':') {
5903 goto sharp_sharp_token
;
5914 /* # is ordinary inside a string. */
5915 if (expected_delimiter
)
5919 /* ##: concatenate preceding and following tokens. */
5920 /* Take out the first #, discard preceding whitespace. */
5922 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
5924 /* Skip the second #. */
5926 concat_sharp_token_type
= c
;
5927 if (is_hor_space
[*p
]) {
5928 concat_sharp_token_type
= c
+ 1;
5930 SKIP_WHITE_SPACE (p
);
5934 error ("`##' at end of macro definition");
5935 } else if (nargs
>= 0) {
5936 /* Single #: stringify following argument ref.
5937 Don't leave the # in the expansion. */
5940 stringify_sharp_token_type
= c
;
5941 if (is_hor_space
[*p
]) {
5942 stringify_sharp_token_type
= c
+ 1;
5944 SKIP_WHITE_SPACE (p
);
5946 if (! is_idstart
[*p
] || nargs
== 0)
5947 error ("`#' operator is not followed by a macro argument name");
5954 /* In -traditional mode, recognize arguments inside strings and
5955 and character constants, and ignore special properties of #.
5956 Arguments inside strings are considered "stringified", but no
5957 extra quote marks are supplied. */
5961 if (expected_delimiter
!= '\0') {
5962 if (c
== expected_delimiter
)
5963 expected_delimiter
= '\0';
5965 expected_delimiter
= c
;
5969 /* Backslash quotes delimiters and itself, but not macro args. */
5970 if (expected_delimiter
!= 0 && p
< limit
5971 && (*p
== expected_delimiter
|| *p
== '\\')) {
5978 if (expected_delimiter
!= '\0') /* No comments inside strings. */
5981 /* If we find a comment that wasn't removed by handle_directive,
5982 this must be -traditional. So replace the comment with
5985 while (++p
< limit
) {
5986 if (p
[0] == '*' && p
[1] == '/') {
5992 /* Mark this as a concatenation-point, as if it had been ##. */
6000 /* Handle the start of a symbol. */
6001 if (is_idchar
[c
] && nargs
> 0) {
6002 U_CHAR
*id_beg
= p
- 1;
6006 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6007 id_len
= p
- id_beg
;
6009 if (is_idstart
[c
]) {
6010 register struct arglist
*arg
;
6012 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6013 struct reflist
*tpat
;
6015 if (arg
->name
[0] == c
6016 && arg
->length
== id_len
6017 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6018 enum sharp_token_type tpat_stringify
;
6019 if (expected_delimiter
) {
6020 if (warn_stringify
) {
6022 warning ("macro argument `%.*s' is stringified.",
6025 warning ("macro arg `%.*s' would be stringified with -traditional.",
6029 /* If ANSI, don't actually substitute inside a string. */
6032 tpat_stringify
= SHARP_TOKEN
;
6035 = (stringify
== id_beg
6036 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6038 /* make a pat node for this arg and append it to the end of
6040 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6043 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6044 tpat
->raw_after
= NO_SHARP_TOKEN
;
6045 tpat
->rest_args
= arg
->rest_args
;
6046 tpat
->stringify
= tpat_stringify
;
6049 defn
->pattern
= tpat
;
6051 endpat
->next
= tpat
;
6054 tpat
->argno
= arg
->argno
;
6055 tpat
->nchars
= exp_p
- lastp
;
6057 register U_CHAR
*p1
= p
;
6058 SKIP_WHITE_SPACE (p1
);
6061 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6062 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6064 lastp
= exp_p
; /* place to start copying from next time */
6071 /* If this was not a macro arg, copy it into the expansion. */
6072 if (! skipped_arg
) {
6073 register U_CHAR
*lim1
= p
;
6077 if (stringify
== id_beg
)
6078 error ("`#' operator should be followed by a macro argument name");
6083 if (!traditional
&& expected_delimiter
== 0) {
6084 /* If ANSI, put in a newline-space marker to prevent token pasting.
6085 But not if "inside a string" (which in ANSI mode happens only for
6093 defn
->length
= exp_p
- defn
->expansion
;
6095 /* Crash now if we overrun the allocated size. */
6096 if (defn
->length
+ 1 > maxsize
)
6100 /* This isn't worth the time it takes. */
6101 /* give back excess storage */
6102 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6109 do_assert (buf
, limit
, op
, keyword
)
6110 U_CHAR
*buf
, *limit
;
6112 struct directive
*keyword
;
6114 U_CHAR
*bp
; /* temp ptr into input buffer */
6115 U_CHAR
*symname
; /* remember where symbol name starts */
6116 int sym_length
; /* and how long it is */
6117 struct arglist
*tokens
= NULL
;
6119 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6120 pedwarn ("ANSI C does not allow `#assert'");
6124 while (is_hor_space
[*bp
])
6127 symname
= bp
; /* remember where it starts */
6128 sym_length
= check_macro_name (bp
, "assertion");
6130 /* #define doesn't do this, but we should. */
6131 SKIP_WHITE_SPACE (bp
);
6133 /* Lossage will occur if identifiers or control tokens are broken
6134 across lines using backslash. This is not the right place to take
6138 error ("missing token-sequence in `#assert'");
6145 bp
++; /* skip '(' */
6146 SKIP_WHITE_SPACE (bp
);
6148 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6152 error ("empty token-sequence in `#assert'");
6156 ++bp
; /* skip paren */
6157 SKIP_WHITE_SPACE (bp
);
6160 /* If this name isn't already an assertion name, make it one.
6161 Error if it was already in use in some other way. */
6164 ASSERTION_HASHNODE
*hp
;
6165 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6166 struct tokenlist_list
*value
6167 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6169 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6171 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6172 error ("`defined' redefined as assertion");
6173 hp
= assertion_install (symname
, sym_length
, hashcode
);
6176 /* Add the spec'd token-sequence to the list of such. */
6177 value
->tokens
= tokens
;
6178 value
->next
= hp
->value
;
6186 do_unassert (buf
, limit
, op
, keyword
)
6187 U_CHAR
*buf
, *limit
;
6189 struct directive
*keyword
;
6191 U_CHAR
*bp
; /* temp ptr into input buffer */
6192 U_CHAR
*symname
; /* remember where symbol name starts */
6193 int sym_length
; /* and how long it is */
6195 struct arglist
*tokens
= NULL
;
6196 int tokens_specified
= 0;
6198 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6199 pedwarn ("ANSI C does not allow `#unassert'");
6203 while (is_hor_space
[*bp
])
6206 symname
= bp
; /* remember where it starts */
6207 sym_length
= check_macro_name (bp
, "assertion");
6209 /* #define doesn't do this, but we should. */
6210 SKIP_WHITE_SPACE (bp
);
6212 /* Lossage will occur if identifiers or control tokens are broken
6213 across lines using backslash. This is not the right place to take
6219 bp
++; /* skip '(' */
6220 SKIP_WHITE_SPACE (bp
);
6222 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6226 error ("empty token list in `#unassert'");
6230 tokens_specified
= 1;
6232 ++bp
; /* skip paren */
6233 SKIP_WHITE_SPACE (bp
);
6237 ASSERTION_HASHNODE
*hp
;
6238 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6239 struct tokenlist_list
*tail
, *prev
;
6241 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6245 /* If no token list was specified, then eliminate this assertion
6247 if (! tokens_specified
) {
6248 struct tokenlist_list
*next
;
6249 for (tail
= hp
->value
; tail
; tail
= next
) {
6251 free_token_list (tail
->tokens
);
6254 delete_assertion (hp
);
6256 /* If a list of tokens was given, then delete any matching list. */
6261 struct tokenlist_list
*next
= tail
->next
;
6262 if (compare_token_lists (tail
->tokens
, tokens
)) {
6266 hp
->value
= tail
->next
;
6267 free_token_list (tail
->tokens
);
6280 /* Test whether there is an assertion named NAME
6281 and optionally whether it has an asserted token list TOKENS.
6282 NAME is not null terminated; its length is SYM_LENGTH.
6283 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6286 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6289 int tokens_specified
;
6290 struct arglist
*tokens
;
6292 ASSERTION_HASHNODE
*hp
;
6293 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6295 if (pedantic
&& !instack
[indepth
].system_header_p
)
6296 pedwarn ("ANSI C does not allow testing assertions");
6298 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6300 /* It is not an assertion; just return false. */
6303 /* If no token list was specified, then value is 1. */
6304 if (! tokens_specified
)
6308 struct tokenlist_list
*tail
;
6312 /* If a list of tokens was given,
6313 then succeed if the assertion records a matching list. */
6316 if (compare_token_lists (tail
->tokens
, tokens
))
6321 /* Fail if the assertion has no matching list. */
6326 /* Compare two lists of tokens for equality including order of tokens. */
6329 compare_token_lists (l1
, l2
)
6330 struct arglist
*l1
, *l2
;
6333 if (l1
->length
!= l2
->length
)
6335 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6341 /* Succeed if both lists end at the same time. */
6345 /* Read a space-separated list of tokens ending in a close parenthesis.
6346 Return a list of strings, in the order they were written.
6347 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6348 Parse the text starting at *BPP, and update *BPP.
6349 Don't parse beyond LIMIT. */
6351 static struct arglist
*
6352 read_token_list (bpp
, limit
, error_flag
)
6357 struct arglist
*token_ptrs
= 0;
6363 /* Loop over the assertion value tokens. */
6365 struct arglist
*temp
;
6369 /* Find the end of the token. */
6373 } else if (*bp
== ')') {
6378 } else if (*bp
== '"' || *bp
== '\'')
6379 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6381 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6382 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6385 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6386 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6387 bcopy ((char *) beg
, (char *) temp
->name
, bp
- beg
);
6388 temp
->name
[bp
- beg
] = 0;
6389 temp
->next
= token_ptrs
;
6391 temp
->length
= bp
- beg
;
6393 SKIP_WHITE_SPACE (bp
);
6396 error ("unterminated token sequence in `#assert' or `#unassert'");
6403 /* We accumulated the names in reverse order.
6404 Now reverse them to get the proper order. */
6406 register struct arglist
*prev
= 0, *this, *next
;
6407 for (this = token_ptrs
; this; this = next
) {
6417 free_token_list (tokens
)
6418 struct arglist
*tokens
;
6421 struct arglist
*next
= tokens
->next
;
6422 free (tokens
->name
);
6429 * Install a name in the assertion hash table.
6431 * If LEN is >= 0, it is the length of the name.
6432 * Otherwise, compute the length by scanning the entire name.
6434 * If HASH is >= 0, it is the precomputed hash code.
6435 * Otherwise, compute the hash code.
6437 static ASSERTION_HASHNODE
*
6438 assertion_install (name
, len
, hash
)
6443 register ASSERTION_HASHNODE
*hp
;
6444 register int i
, bucket
;
6445 register U_CHAR
*p
, *q
;
6447 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6448 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6450 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6451 hp
->next
= assertion_hashtab
[bucket
];
6452 assertion_hashtab
[bucket
] = hp
;
6454 if (hp
->next
!= NULL
)
6455 hp
->next
->prev
= hp
;
6458 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6461 for (i
= 0; i
< len
; i
++)
6468 * find the most recent hash node for name name (ending with first
6469 * non-identifier char) installed by install
6471 * If LEN is >= 0, it is the length of the name.
6472 * Otherwise, compute the length by scanning the entire name.
6474 * If HASH is >= 0, it is the precomputed hash code.
6475 * Otherwise, compute the hash code.
6477 static ASSERTION_HASHNODE
*
6478 assertion_lookup (name
, len
, hash
)
6483 register ASSERTION_HASHNODE
*bucket
;
6485 bucket
= assertion_hashtab
[hash
];
6487 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6489 bucket
= bucket
->next
;
6495 delete_assertion (hp
)
6496 ASSERTION_HASHNODE
*hp
;
6499 if (hp
->prev
!= NULL
)
6500 hp
->prev
->next
= hp
->next
;
6501 if (hp
->next
!= NULL
)
6502 hp
->next
->prev
= hp
->prev
;
6504 /* make sure that the bucket chain header that
6505 the deleted guy was on points to the right thing afterwards. */
6506 if (hp
== *hp
->bucket_hdr
)
6507 *hp
->bucket_hdr
= hp
->next
;
6513 * interpret #line directive. Remembers previously seen fnames
6514 * in its very own hash table.
6516 #define FNAME_HASHSIZE 37
6519 do_line (buf
, limit
, op
, keyword
)
6520 U_CHAR
*buf
, *limit
;
6522 struct directive
*keyword
;
6524 register U_CHAR
*bp
;
6525 FILE_BUF
*ip
= &instack
[indepth
];
6528 enum file_change_code file_change
= same_file
;
6530 /* Expand any macros. */
6531 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6533 /* Point to macroexpanded line, which is null-terminated now. */
6535 SKIP_WHITE_SPACE (bp
);
6537 if (!isdigit (*bp
)) {
6538 error ("invalid format `#line' directive");
6542 /* The Newline at the end of this line remains to be processed.
6543 To put the next line at the specified line number,
6544 we must store a line number now that is one less. */
6545 new_lineno
= atoi ((char *) bp
) - 1;
6547 /* NEW_LINENO is one less than the actual line number here. */
6548 if (pedantic
&& new_lineno
< 0)
6549 pedwarn ("line number out of range in `#line' directive");
6551 /* skip over the line number. */
6552 while (isdigit (*bp
))
6555 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6556 if (*bp
&& !is_space
[*bp
]) {
6557 error ("invalid format `#line' directive");
6562 SKIP_WHITE_SPACE (bp
);
6565 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6566 HASHNODE
*hp
, **hash_bucket
;
6572 /* Turn the file name, which is a character string literal,
6573 into a null-terminated string. Do this in place. */
6576 switch ((*p
++ = *bp
++)) {
6578 error ("invalid format `#line' directive");
6583 char *bpc
= (char *) bp
;
6584 HOST_WIDE_INT c
= parse_escape (&bpc
, (HOST_WIDE_INT
) (U_CHAR
) (-1));
6585 bp
= (U_CHAR
*) bpc
;
6598 fname_length
= p
- fname
;
6600 SKIP_WHITE_SPACE (bp
);
6603 pedwarn ("garbage at end of `#line' directive");
6605 file_change
= enter_file
;
6606 else if (*bp
== '2')
6607 file_change
= leave_file
;
6608 else if (*bp
== '3')
6609 ip
->system_header_p
= 1;
6610 else if (*bp
== '4')
6611 ip
->system_header_p
= 2;
6613 error ("invalid format `#line' directive");
6618 SKIP_WHITE_SPACE (bp
);
6620 ip
->system_header_p
= 1;
6622 SKIP_WHITE_SPACE (bp
);
6625 ip
->system_header_p
= 2;
6627 SKIP_WHITE_SPACE (bp
);
6630 error ("invalid format `#line' directive");
6636 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
6637 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
6638 if (hp
->length
== fname_length
&&
6639 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
6640 ip
->nominal_fname
= hp
->value
.cpval
;
6644 /* Didn't find it; cons up a new one. */
6645 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
6646 hp
->next
= *hash_bucket
;
6649 hp
->length
= fname_length
;
6650 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
6651 bcopy (fname
, hp
->value
.cpval
, fname_length
);
6654 error ("invalid format `#line' directive");
6658 ip
->lineno
= new_lineno
;
6659 output_line_directive (ip
, op
, 0, file_change
);
6660 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
6665 * remove the definition of a symbol from the symbol table.
6666 * according to un*x /lib/cpp, it is not an error to undef
6667 * something that has no definitions, so it isn't one here either.
6671 do_undef (buf
, limit
, op
, keyword
)
6672 U_CHAR
*buf
, *limit
;
6674 struct directive
*keyword
;
6678 U_CHAR
*orig_buf
= buf
;
6680 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
6681 if (pcp_outfile
&& op
)
6682 pass_thru_directive (buf
, limit
, op
, keyword
);
6684 SKIP_WHITE_SPACE (buf
);
6685 sym_length
= check_macro_name (buf
, "macro");
6687 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
6688 /* If we are generating additional info for debugging (with -g) we
6689 need to pass through all effective #undef directives. */
6690 if (debug_output
&& op
)
6691 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
6692 if (hp
->type
!= T_MACRO
)
6693 warning ("undefining `%s'", hp
->name
);
6699 SKIP_WHITE_SPACE (buf
);
6701 pedwarn ("garbage after `#undef' directive");
6707 * Report an error detected by the program we are processing.
6708 * Use the text of the line in the error message.
6709 * (We use error because it prints the filename & line#.)
6713 do_error (buf
, limit
, op
, keyword
)
6714 U_CHAR
*buf
, *limit
;
6716 struct directive
*keyword
;
6718 int length
= limit
- buf
;
6719 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
6720 bcopy ((char *) buf
, (char *) copy
, length
);
6722 SKIP_WHITE_SPACE (copy
);
6723 error ("#error %s", copy
);
6728 * Report a warning detected by the program we are processing.
6729 * Use the text of the line in the warning message, then continue.
6730 * (We use error because it prints the filename & line#.)
6734 do_warning (buf
, limit
, op
, keyword
)
6735 U_CHAR
*buf
, *limit
;
6737 struct directive
*keyword
;
6739 int length
= limit
- buf
;
6740 U_CHAR
*copy
= (U_CHAR
*) xmalloc (length
+ 1);
6741 bcopy ((char *) buf
, (char *) copy
, length
);
6743 SKIP_WHITE_SPACE (copy
);
6744 warning ("#warning %s", copy
);
6748 /* Remember the name of the current file being read from so that we can
6749 avoid ever including it again. */
6756 for (i
= indepth
; i
>= 0; i
--)
6757 if (instack
[i
].inc
) {
6758 record_control_macro (instack
[i
].inc
, (U_CHAR
*) "");
6763 /* #ident has already been copied to the output file, so just ignore it. */
6766 do_ident (buf
, limit
, op
, keyword
)
6767 U_CHAR
*buf
, *limit
;
6769 struct directive
*keyword
;
6774 /* Allow #ident in system headers, since that's not user's fault. */
6775 if (pedantic
&& !instack
[indepth
].system_header_p
)
6776 pedwarn ("ANSI C does not allow `#ident'");
6778 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6779 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
6780 bcopy ((char *) trybuf
.buf
, (char *) buf
, trybuf
.bufp
- trybuf
.buf
);
6781 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
6782 len
= (limit
- buf
);
6785 /* Output directive name. */
6786 check_expand (op
, 7);
6787 bcopy ("#ident ", (char *) op
->bufp
, 7);
6790 /* Output the expanded argument line. */
6791 check_expand (op
, len
);
6792 bcopy ((char *) buf
, (char *) op
->bufp
, len
);
6798 /* #pragma and its argument line have already been copied to the output file.
6799 Just check for some recognized pragmas that need validation here. */
6802 do_pragma (buf
, limit
, op
, keyword
)
6803 U_CHAR
*buf
, *limit
;
6805 struct directive
*keyword
;
6807 SKIP_WHITE_SPACE (buf
);
6808 if (!strncmp ((char *) buf
, "once", 4)) {
6809 /* Allow #pragma once in system headers, since that's not the user's
6811 if (!instack
[indepth
].system_header_p
)
6812 warning ("`#pragma once' is obsolete");
6816 if (!strncmp ((char *) buf
, "implementation", 14)) {
6817 /* Be quiet about `#pragma implementation' for a file only if it hasn't
6818 been included yet. */
6821 U_CHAR
*p
= buf
+ 14, *fname
;
6822 SKIP_WHITE_SPACE (p
);
6823 if (*p
== '\n' || *p
!= '\"')
6827 if ((p
= (U_CHAR
*) index ((char *) fname
, '\"')))
6830 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
6831 struct include_file
*inc
;
6832 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
6833 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
6834 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
6844 /* This was a fun hack, but #pragma seems to start to be useful.
6845 By failing to recognize it, we pass it through unchanged to cc1. */
6848 * the behavior of the #pragma directive is implementation defined.
6849 * this implementation defines it as follows.
6856 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
6859 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
6861 execl ("/usr/games/hack", "#pragma", 0);
6862 execl ("/usr/games/rogue", "#pragma", 0);
6863 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
6864 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
6866 fatal ("You are in a maze of twisty compiler features, all different");
6870 #ifdef SCCS_DIRECTIVE
6872 /* Just ignore #sccs, on systems where we define it at all. */
6875 do_sccs (buf
, limit
, op
, keyword
)
6876 U_CHAR
*buf
, *limit
;
6878 struct directive
*keyword
;
6881 pedwarn ("ANSI C does not allow `#sccs'");
6885 #endif /* defined (SCCS_DIRECTIVE) */
6888 * handle #if directive by
6889 * 1) inserting special `defined' keyword into the hash table
6890 * that gets turned into 0 or 1 by special_symbol (thus,
6891 * if the luser has a symbol called `defined' already, it won't
6892 * work inside the #if directive)
6893 * 2) rescan the input into a temporary output buffer
6894 * 3) pass the output buffer to the yacc parser and collect a value
6895 * 4) clean up the mess left from steps 1 and 2.
6896 * 5) call conditional_skip to skip til the next #endif (etc.),
6897 * or not, depending on the value from step 3.
6901 do_if (buf
, limit
, op
, keyword
)
6902 U_CHAR
*buf
, *limit
;
6904 struct directive
*keyword
;
6906 HOST_WIDE_INT value
;
6907 FILE_BUF
*ip
= &instack
[indepth
];
6909 value
= eval_if_expression (buf
, limit
- buf
);
6910 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
6915 * handle a #elif directive by not changing if_stack either.
6916 * see the comment above do_else.
6920 do_elif (buf
, limit
, op
, keyword
)
6921 U_CHAR
*buf
, *limit
;
6923 struct directive
*keyword
;
6925 HOST_WIDE_INT value
;
6926 FILE_BUF
*ip
= &instack
[indepth
];
6928 if (if_stack
== instack
[indepth
].if_stack
) {
6929 error ("`#elif' not within a conditional");
6932 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
6933 error ("`#elif' after `#else'");
6934 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
6935 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
6936 strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
6937 fprintf (stderr
, ", file %s", if_stack
->fname
);
6938 fprintf (stderr
, ")\n");
6940 if_stack
->type
= T_ELIF
;
6943 if (if_stack
->if_succeeded
)
6944 skip_if_group (ip
, 0, op
);
6946 value
= eval_if_expression (buf
, limit
- buf
);
6948 skip_if_group (ip
, 0, op
);
6950 ++if_stack
->if_succeeded
; /* continue processing input */
6951 output_line_directive (ip
, op
, 1, same_file
);
6958 * evaluate a #if expression in BUF, of length LENGTH,
6959 * then parse the result as a C expression and return the value as an int.
6961 static HOST_WIDE_INT
6962 eval_if_expression (buf
, length
)
6967 HASHNODE
*save_defined
;
6968 HOST_WIDE_INT value
;
6970 save_defined
= install ((U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
6973 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
6975 delete_macro (save_defined
); /* clean up special symbol */
6977 temp_obuf
.buf
[temp_obuf
.length
] = '\n';
6978 value
= parse_c_expression ((char *) temp_obuf
.buf
);
6980 free (temp_obuf
.buf
);
6986 * routine to handle ifdef/ifndef. Try to look up the symbol,
6987 * then do or don't skip to the #endif/#else/#elif depending
6988 * on what directive is actually being processed.
6992 do_xifdef (buf
, limit
, op
, keyword
)
6993 U_CHAR
*buf
, *limit
;
6995 struct directive
*keyword
;
6998 FILE_BUF
*ip
= &instack
[indepth
];
7000 int start_of_file
= 0;
7001 U_CHAR
*control_macro
= 0;
7003 /* Detect a #ifndef at start of file (not counting comments). */
7004 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7005 U_CHAR
*p
= ip
->buf
;
7006 while (p
!= directive_start
) {
7010 /* Make no special provision for backslash-newline here; this is
7011 slower if backslash-newlines are present, but it's correct,
7012 and it's not worth it to tune for the rare backslash-newline. */
7014 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7015 /* Skip this comment. */
7017 U_CHAR
*save_bufp
= ip
->bufp
;
7019 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7020 ip
->bufp
= save_bufp
;
7025 /* If we get here, this conditional is the beginning of the file. */
7030 /* Discard leading and trailing whitespace. */
7031 SKIP_WHITE_SPACE (buf
);
7032 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7034 /* Find the end of the identifier at the beginning. */
7035 for (end
= buf
; is_idchar
[*end
]; end
++);
7038 skip
= (keyword
->type
== T_IFDEF
);
7040 pedwarn (end
== limit
? "`#%s' with no argument"
7041 : "`#%s' argument starts with punctuation",
7046 if (! traditional
) {
7047 if (isdigit (buf
[0]))
7048 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7049 else if (end
!= limit
)
7050 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7053 hp
= lookup (buf
, end
-buf
, -1);
7056 /* Output a precondition for this macro. */
7058 (hp
->type
== T_CONST
7059 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7060 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7063 fprintf (pcp_outfile
, "#undef ");
7064 while (is_idchar
[*cp
]) /* Ick! */
7065 fputc (*cp
++, pcp_outfile
);
7066 putc ('\n', pcp_outfile
);
7070 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7071 if (start_of_file
&& !skip
) {
7072 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7073 bcopy ((char *) buf
, (char *) control_macro
, end
- buf
);
7074 control_macro
[end
- buf
] = 0;
7078 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7082 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7083 If this is a #ifndef starting at the beginning of a file,
7084 CONTROL_MACRO is the macro name tested by the #ifndef.
7085 Otherwise, CONTROL_MACRO is 0. */
7088 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7091 enum node_type type
;
7092 U_CHAR
*control_macro
;
7095 IF_STACK_FRAME
*temp
;
7097 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7098 temp
->fname
= ip
->nominal_fname
;
7099 temp
->lineno
= ip
->lineno
;
7100 temp
->next
= if_stack
;
7101 temp
->control_macro
= control_macro
;
7104 if_stack
->type
= type
;
7107 skip_if_group (ip
, 0, op
);
7110 ++if_stack
->if_succeeded
;
7111 output_line_directive (ip
, &outbuf
, 1, same_file
);
7116 * skip to #endif, #else, or #elif. adjust line numbers, etc.
7117 * leaves input ptr at the sharp sign found.
7118 * If ANY is nonzero, return at next directive of any sort.
7121 skip_if_group (ip
, any
, op
)
7126 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7127 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7128 struct directive
*kt
;
7129 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7130 U_CHAR
*beg_of_line
= bp
;
7131 register int ident_length
;
7132 U_CHAR
*ident
, *after_ident
;
7133 /* Save info about where the group starts. */
7134 U_CHAR
*beg_of_group
= bp
;
7135 int beg_lineno
= ip
->lineno
;
7137 if (output_conditionals
&& op
!= 0) {
7138 char *ptr
= "#failed\n";
7139 int len
= strlen (ptr
);
7141 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7146 check_expand (op
, len
);
7147 bcopy (ptr
, (char *) op
->bufp
, len
);
7150 output_line_directive (ip
, op
, 1, 0);
7155 case '/': /* possible comment */
7156 if (*bp
== '\\' && bp
[1] == '\n')
7159 || (cplusplus_comments
&& *bp
== '/')) {
7161 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7166 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7167 NULL_PTR
, NULL_PTR
);
7170 /* Char after backslash loses its special meaning. */
7173 ++ip
->lineno
; /* But do update the line-count. */
7182 if (beg_of_line
== 0 || traditional
)
7185 while (bp
[0] == '\\' && bp
[1] == '\n')
7191 /* # keyword: a # must be first nonblank char on the line */
7192 if (beg_of_line
== 0)
7196 /* Scan from start of line, skipping whitespace, comments
7197 and backslash-newlines, and see if we reach this #.
7198 If not, this # is not special. */
7200 /* If -traditional, require # to be at beginning of line. */
7203 if (is_hor_space
[*bp
])
7205 else if (*bp
== '\\' && bp
[1] == '\n')
7207 else if (*bp
== '/' && bp
[1] == '*') {
7209 while (!(*bp
== '*' && bp
[1] == '/'))
7213 /* There is no point in trying to deal with C++ // comments here,
7214 because if there is one, then this # must be part of the
7215 comment and we would never reach here. */
7219 if (bp
!= ip
->bufp
) {
7220 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7224 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7225 if (ip
->bufp
[0] == '%') {
7226 /* Skip past the ':' again. */
7227 while (*bp
== '\\') {
7234 /* Skip whitespace and \-newline. */
7236 if (is_hor_space
[*bp
])
7238 else if (*bp
== '\\' && bp
[1] == '\n')
7240 else if (*bp
== '/') {
7242 for (bp
+= 2; ; bp
++) {
7245 else if (*bp
== '*') {
7246 if (bp
[-1] == '/' && warn_comments
)
7247 warning ("`/*' within comment");
7253 } else if (bp
[1] == '/' && cplusplus_comments
) {
7254 for (bp
+= 2; ; bp
++) {
7259 warning ("multiline `//' comment");
7271 /* Now find end of directive name.
7272 If we encounter a backslash-newline, exchange it with any following
7273 symbol-constituents so that we end up with a contiguous name. */
7279 if (*bp
== '\\' && bp
[1] == '\n')
7280 name_newline_fix (bp
);
7286 ident_length
= bp
- cp
;
7290 /* A line of just `#' becomes blank. */
7292 if (ident_length
== 0 && *after_ident
== '\n') {
7296 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7298 while (is_idchar
[*p
]) {
7299 if (*p
< '0' || *p
> '9')
7303 /* Handle # followed by a line number. */
7304 if (p
!= ident
&& !is_idchar
[*p
]) {
7306 pedwarn ("`#' followed by integer");
7310 /* Avoid error for `###' and similar cases unless -pedantic. */
7312 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7314 if (pedantic
&& !lang_asm
)
7315 pedwarn ("invalid preprocessing directive");
7320 if (!lang_asm
&& pedantic
)
7321 pedwarn ("invalid preprocessing directive name");
7325 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7326 IF_STACK_FRAME
*temp
;
7327 if (ident_length
== kt
->length
7328 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7329 /* If we are asked to return on next directive, do so now. */
7337 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7338 temp
->next
= if_stack
;
7340 temp
->lineno
= ip
->lineno
;
7341 temp
->fname
= ip
->nominal_fname
;
7342 temp
->type
= kt
->type
;
7346 if (pedantic
&& if_stack
!= save_if_stack
)
7347 validate_else (bp
, endb
);
7349 if (if_stack
== instack
[indepth
].if_stack
) {
7350 error ("`#%s' not within a conditional", kt
->name
);
7353 else if (if_stack
== save_if_stack
)
7354 goto done
; /* found what we came for */
7356 if (kt
->type
!= T_ENDIF
) {
7357 if (if_stack
->type
== T_ELSE
)
7358 error ("`#else' or `#elif' after `#else'");
7359 if_stack
->type
= kt
->type
;
7364 if_stack
= if_stack
->next
;
7374 /* Don't let erroneous code go by. */
7375 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7376 pedwarn ("invalid preprocessing directive name");
7381 /* after this returns, rescan will exit because ip->bufp
7382 now points to the end of the buffer.
7383 rescan is responsible for the error message also. */
7386 if (output_conditionals
&& op
!= 0) {
7387 char *ptr
= "#endfailed\n";
7388 int len
= strlen (ptr
);
7390 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7395 check_expand (op
, beg_of_line
- beg_of_group
);
7396 bcopy ((char *) beg_of_group
, (char *) op
->bufp
,
7397 beg_of_line
- beg_of_group
);
7398 op
->bufp
+= beg_of_line
- beg_of_group
;
7399 op
->lineno
+= ip
->lineno
- beg_lineno
;
7400 check_expand (op
, len
);
7401 bcopy (ptr
, (char *) op
->bufp
, len
);
7408 * handle a #else directive. Do this by just continuing processing
7409 * without changing if_stack ; this is so that the error message
7410 * for missing #endif's etc. will point to the original #if. It
7411 * is possible that something different would be better.
7415 do_else (buf
, limit
, op
, keyword
)
7416 U_CHAR
*buf
, *limit
;
7418 struct directive
*keyword
;
7420 FILE_BUF
*ip
= &instack
[indepth
];
7423 SKIP_WHITE_SPACE (buf
);
7425 pedwarn ("text following `#else' violates ANSI standard");
7428 if (if_stack
== instack
[indepth
].if_stack
) {
7429 error ("`#else' not within a conditional");
7432 /* #ifndef can't have its special treatment for containing the whole file
7433 if it has a #else clause. */
7434 if_stack
->control_macro
= 0;
7436 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7437 error ("`#else' after `#else'");
7438 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7439 if (strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
7440 fprintf (stderr
, ", file %s", if_stack
->fname
);
7441 fprintf (stderr
, ")\n");
7443 if_stack
->type
= T_ELSE
;
7446 if (if_stack
->if_succeeded
)
7447 skip_if_group (ip
, 0, op
);
7449 ++if_stack
->if_succeeded
; /* continue processing input */
7450 output_line_directive (ip
, op
, 1, same_file
);
7456 * unstack after #endif directive
7460 do_endif (buf
, limit
, op
, keyword
)
7461 U_CHAR
*buf
, *limit
;
7463 struct directive
*keyword
;
7466 SKIP_WHITE_SPACE (buf
);
7468 pedwarn ("text following `#endif' violates ANSI standard");
7471 if (if_stack
== instack
[indepth
].if_stack
)
7472 error ("unbalanced `#endif'");
7474 IF_STACK_FRAME
*temp
= if_stack
;
7475 if_stack
= if_stack
->next
;
7476 if (temp
->control_macro
!= 0) {
7477 /* This #endif matched a #ifndef at the start of the file.
7478 See if it is at the end of the file. */
7479 FILE_BUF
*ip
= &instack
[indepth
];
7480 U_CHAR
*p
= ip
->bufp
;
7481 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7487 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7488 /* Skip this comment. */
7490 U_CHAR
*save_bufp
= ip
->bufp
;
7492 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7493 ip
->bufp
= save_bufp
;
7498 /* If we get here, this #endif ends a #ifndef
7499 that contains all of the file (aside from whitespace).
7500 Arrange not to include the file again
7501 if the macro that was tested is defined.
7503 Do not do this for the top-level file in a -include or any
7504 file in a -imacros. */
7506 && ! (indepth
== 1 && no_record_file
)
7507 && ! (no_record_file
&& no_output
))
7508 record_control_macro (ip
->inc
, temp
->control_macro
);
7512 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
7517 /* When an #else or #endif is found while skipping failed conditional,
7518 if -pedantic was specified, this is called to warn about text after
7519 the directive name. P points to the first char after the directive name. */
7522 validate_else (p
, limit
)
7524 register U_CHAR
*limit
;
7526 /* Advance P over whitespace and comments. */
7528 while (*p
== '\\' && p
[1] == '\n')
7530 if (is_hor_space
[*p
])
7532 else if (*p
== '/') {
7533 while (p
[1] == '\\' && p
[2] == '\n')
7536 /* Don't bother warning about unterminated comments
7537 since that will happen later. Just be sure to exit. */
7538 for (p
+= 2; ; p
++) {
7542 while (p
[1] == '\\' && p
[2] == '\n')
7551 else if (cplusplus_comments
&& p
[1] == '/')
7557 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
7560 /* Skip a comment, assuming the input ptr immediately follows the
7561 initial slash-star. Bump *LINE_COUNTER for each newline.
7562 (The canonical line counter is &ip->lineno.)
7563 Don't use this routine (or the next one) if bumping the line
7564 counter is not sufficient to deal with newlines in the string.
7566 If NOWARN is nonzero, don't warn about slash-star inside a comment.
7567 This feature is useful when processing a comment that is going to be
7568 processed or was processed at another point in the preprocessor,
7569 to avoid a duplicate warning. Likewise for unterminated comment errors. */
7572 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
7573 register FILE_BUF
*ip
;
7574 int *line_counter
; /* place to remember newlines, or NULL */
7577 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
7578 register U_CHAR
*bp
= ip
->bufp
;
7579 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
7580 int start_line
= line_counter
? *line_counter
: 0;
7582 /* JF this line_counter stuff is a crock to make sure the
7583 comment is only put out once, no matter how many times
7584 the comment is skipped. It almost works */
7587 *op
->bufp
++ = bp
[-1];
7589 if (cplusplus_comments
&& bp
[-1] == '/') {
7590 for (; bp
< limit
; bp
++) {
7596 if (!nowarn
&& warn_comments
)
7597 warning ("multiline `//' comment");
7607 while (bp
< limit
) {
7612 /* If this is the end of the file, we have an unterminated comment.
7613 Don't swallow the newline. We are guaranteed that there will be a
7614 trailing newline and various pieces assume it's there. */
7621 if (line_counter
!= NULL
)
7627 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
7628 warning ("`/*' within comment");
7629 if (*bp
== '\\' && bp
[1] == '\n')
7642 error_with_line (line_for_error (start_line
), "unterminated comment");
7648 * Skip over a quoted string. BP points to the opening quote.
7649 * Returns a pointer after the closing quote. Don't go past LIMIT.
7650 * START_LINE is the line number of the starting point (but it need
7651 * not be valid if the starting point is inside a macro expansion).
7653 * The input stack state is not changed.
7655 * If COUNT_NEWLINES is nonzero, it points to an int to increment
7656 * for each newline passed.
7658 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
7659 * if we pass a backslash-newline.
7661 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
7664 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
7665 register U_CHAR
*bp
;
7666 register U_CHAR
*limit
;
7668 int *count_newlines
;
7669 int *backslash_newlines_p
;
7672 register U_CHAR c
, match
;
7677 error_with_line (line_for_error (start_line
),
7678 "unterminated string or character constant");
7679 error_with_line (multiline_string_line
,
7680 "possible real start of unterminated constant");
7681 multiline_string_line
= 0;
7688 while (*bp
== '\\' && bp
[1] == '\n') {
7689 if (backslash_newlines_p
)
7690 *backslash_newlines_p
= 1;
7695 if (*bp
== '\n' && count_newlines
) {
7696 if (backslash_newlines_p
)
7697 *backslash_newlines_p
= 1;
7701 } else if (c
== '\n') {
7703 /* Unterminated strings and character constants are 'valid'. */
7704 bp
--; /* Don't consume the newline. */
7709 if (match
== '\'') {
7710 error_with_line (line_for_error (start_line
),
7711 "unterminated string or character constant");
7717 /* If not traditional, then allow newlines inside strings. */
7720 if (multiline_string_line
== 0) {
7722 pedwarn_with_line (line_for_error (start_line
),
7723 "string constant runs past end of line");
7724 multiline_string_line
= start_line
;
7726 } else if (c
== match
)
7732 /* Place into DST a quoted string representing the string SRC.
7733 Return the address of DST's terminating null. */
7735 quote_string (dst
, src
)
7742 switch ((c
= *src
++))
7749 sprintf (dst
, "\\%03o", c
);
7767 /* Skip across a group of balanced parens, starting from IP->bufp.
7768 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
7770 This does not handle newlines, because it's used for the arg of #if,
7771 where there aren't any newlines. Also, backslash-newline can't appear. */
7774 skip_paren_group (ip
)
7775 register FILE_BUF
*ip
;
7777 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
7778 U_CHAR
*p
= ip
->bufp
;
7780 int lines_dummy
= 0;
7782 while (p
!= limit
) {
7792 return ip
->bufp
= p
;
7798 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
7806 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
7808 return ip
->bufp
= p
;
7819 * write out a #line directive, for instance, after an #include file.
7820 * If CONDITIONAL is nonzero, we can omit the #line if it would
7821 * appear to be a no-op, and we can output a few newlines instead
7822 * if we want to increase the line number by a small amount.
7823 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
7827 output_line_directive (ip
, op
, conditional
, file_change
)
7830 enum file_change_code file_change
;
7833 char *line_directive_buf
, *line_end
;
7835 if (no_line_directives
7836 || ip
->fname
== NULL
7838 op
->lineno
= ip
->lineno
;
7843 if (ip
->lineno
== op
->lineno
)
7846 /* If the inherited line number is a little too small,
7847 output some newlines instead of a #line directive. */
7848 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
7849 check_expand (op
, 10);
7850 while (ip
->lineno
> op
->lineno
) {
7858 /* Don't output a line number of 0 if we can help it. */
7859 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
7860 && *ip
->bufp
== '\n') {
7865 line_directive_buf
= (char *) alloca (4 * strlen (ip
->nominal_fname
) + 100);
7866 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
7867 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
7869 if (file_change
!= same_file
) {
7871 *line_end
++ = file_change
== enter_file
? '1' : '2';
7873 /* Tell cc1 if following text comes from a system header file. */
7874 if (ip
->system_header_p
) {
7878 #ifndef NO_IMPLICIT_EXTERN_C
7879 /* Tell cc1plus if following text should be treated as C. */
7880 if (ip
->system_header_p
== 2 && cplusplus
) {
7886 len
= line_end
- line_directive_buf
;
7887 check_expand (op
, len
+ 1);
7888 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7890 bcopy ((char *) line_directive_buf
, (char *) op
->bufp
, len
);
7892 op
->lineno
= ip
->lineno
;
7895 /* This structure represents one parsed argument in a macro call.
7896 `raw' points to the argument text as written (`raw_length' is its length).
7897 `expanded' points to the argument's macro-expansion
7898 (its length is `expand_length').
7899 `stringified_length' is the length the argument would have
7901 `use_count' is the number of times this macro arg is substituted
7902 into the macro. If the actual use count exceeds 10,
7903 the value stored is 10.
7904 `free1' and `free2', if nonzero, point to blocks to be freed
7905 when the macro argument data is no longer needed. */
7908 U_CHAR
*raw
, *expanded
;
7909 int raw_length
, expand_length
;
7910 int stringified_length
;
7911 U_CHAR
*free1
, *free2
;
7916 /* Expand a macro call.
7917 HP points to the symbol that is the macro being called.
7918 Put the result of expansion onto the input stack
7919 so that subsequent input by our caller will use it.
7921 If macro wants arguments, caller has already verified that
7922 an argument list follows; arguments come from the input stack. */
7925 macroexpand (hp
, op
)
7930 DEFINITION
*defn
= hp
->value
.defn
;
7931 register U_CHAR
*xbuf
;
7933 int start_line
= instack
[indepth
].lineno
;
7934 int rest_args
, rest_zero
;
7936 CHECK_DEPTH (return;);
7938 /* it might not actually be a macro. */
7939 if (hp
->type
!= T_MACRO
) {
7940 special_symbol (hp
, op
);
7944 /* This macro is being used inside a #if, which means it must be */
7945 /* recorded as a precondition. */
7946 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
7947 dump_single_macro (hp
, pcp_outfile
);
7949 nargs
= defn
->nargs
;
7953 struct argdata
*args
;
7954 char *parse_error
= 0;
7956 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
7958 for (i
= 0; i
< nargs
; i
++) {
7959 args
[i
].raw
= (U_CHAR
*) "";
7960 args
[i
].expanded
= 0;
7961 args
[i
].raw_length
= args
[i
].expand_length
7962 = args
[i
].stringified_length
= 0;
7963 args
[i
].free1
= args
[i
].free2
= 0;
7964 args
[i
].use_count
= 0;
7967 /* Parse all the macro args that are supplied. I counts them.
7968 The first NARGS args are stored in ARGS.
7969 The rest are discarded.
7970 If rest_args is set then we assume macarg absorbed the rest of the args.
7975 /* Discard the open-parenthesis or comma before the next arg. */
7976 ++instack
[indepth
].bufp
;
7979 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
7980 /* if we are working on last arg which absorbs rest of args... */
7981 if (i
== nargs
- 1 && defn
->rest_args
)
7983 parse_error
= macarg (&args
[i
], rest_args
);
7986 parse_error
= macarg (NULL_PTR
, 0);
7988 error_with_line (line_for_error (start_line
), parse_error
);
7992 } while (*instack
[indepth
].bufp
!= ')');
7994 /* If we got one arg but it was just whitespace, call that 0 args. */
7996 register U_CHAR
*bp
= args
[0].raw
;
7997 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
7998 /* cpp.texi says for foo ( ) we provide one argument.
7999 However, if foo wants just 0 arguments, treat this as 0. */
8001 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8006 /* Don't output an error message if we have already output one for
8007 a parse error above. */
8009 if (nargs
== 0 && i
> 0) {
8011 error ("arguments given to macro `%s'", hp
->name
);
8012 } else if (i
< nargs
) {
8013 /* traditional C allows foo() if foo wants one argument. */
8014 if (nargs
== 1 && i
== 0 && traditional
)
8016 /* the rest args token is allowed to absorb 0 tokens */
8017 else if (i
== nargs
- 1 && defn
->rest_args
)
8019 else if (parse_error
)
8022 error ("macro `%s' used without args", hp
->name
);
8024 error ("macro `%s' used with just one arg", hp
->name
);
8026 error ("macro `%s' used with only %d args", hp
->name
, i
);
8027 } else if (i
> nargs
) {
8029 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8032 /* Swallow the closeparen. */
8033 ++instack
[indepth
].bufp
;
8035 /* If macro wants zero args, we parsed the arglist for checking only.
8036 Read directly from the macro definition. */
8038 xbuf
= defn
->expansion
;
8039 xbuf_len
= defn
->length
;
8041 register U_CHAR
*exp
= defn
->expansion
;
8042 register int offset
; /* offset in expansion,
8043 copied a piece at a time */
8044 register int totlen
; /* total amount of exp buffer filled so far */
8046 register struct reflist
*ap
, *last_ap
;
8048 /* Macro really takes args. Compute the expansion of this call. */
8050 /* Compute length in characters of the macro's expansion.
8051 Also count number of times each arg is used. */
8052 xbuf_len
= defn
->length
;
8053 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8055 xbuf_len
+= args
[ap
->argno
].stringified_length
;
8056 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8057 /* Add 4 for two newline-space markers to prevent
8058 token concatenation. */
8059 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8061 /* We have an ordinary (expanded) occurrence of the arg.
8062 So compute its expansion, if we have not already. */
8063 if (args
[ap
->argno
].expanded
== 0) {
8065 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8066 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8069 args
[ap
->argno
].expanded
= obuf
.buf
;
8070 args
[ap
->argno
].expand_length
= obuf
.length
;
8071 args
[ap
->argno
].free2
= obuf
.buf
;
8074 /* Add 4 for two newline-space markers to prevent
8075 token concatenation. */
8076 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
8078 if (args
[ap
->argno
].use_count
< 10)
8079 args
[ap
->argno
].use_count
++;
8082 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8084 /* Generate in XBUF the complete expansion
8085 with arguments substituted in.
8086 TOTLEN is the total size generated so far.
8087 OFFSET is the index in the definition
8088 of where we are copying from. */
8089 offset
= totlen
= 0;
8090 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8091 last_ap
= ap
, ap
= ap
->next
) {
8092 register struct argdata
*arg
= &args
[ap
->argno
];
8093 int count_before
= totlen
;
8095 /* Add chars to XBUF. */
8096 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8097 xbuf
[totlen
++] = exp
[offset
];
8099 /* If followed by an empty rest arg with concatenation,
8100 delete the last run of nonwhite chars. */
8101 if (rest_zero
&& totlen
> count_before
8102 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8103 || (last_ap
!= NULL
&& last_ap
->rest_args
8104 && last_ap
->raw_after
!= 0))) {
8105 /* Delete final whitespace. */
8106 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8110 /* Delete the nonwhites before them. */
8111 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8116 if (ap
->stringify
!= 0) {
8117 int arglen
= arg
->raw_length
;
8123 && (c
= arg
->raw
[i
], is_space
[c
]))
8126 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8129 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8130 for (; i
< arglen
; i
++) {
8133 /* Special markers Newline Space
8134 generate nothing for a stringified argument. */
8135 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
8140 /* Internal sequences of whitespace are replaced by one space
8141 except within an string or char token. */
8143 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
8145 /* Note that Newline Space does occur within whitespace
8146 sequences; consider it part of the sequence. */
8147 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
8149 else if (c
!= '\n' && is_space
[c
])
8166 } else if (c
== '\"' || c
== '\'')
8170 /* Escape these chars */
8171 if (c
== '\"' || (in_string
&& c
== '\\'))
8172 xbuf
[totlen
++] = '\\';
8176 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
8181 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8182 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8183 U_CHAR
*p1
= arg
->raw
;
8184 U_CHAR
*l1
= p1
+ arg
->raw_length
;
8185 if (ap
->raw_before
!= 0) {
8186 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8187 while (p1
!= l1
&& is_idchar
[*p1
])
8188 xbuf
[totlen
++] = *p1
++;
8189 /* Delete any no-reexpansion marker that follows
8190 an identifier at the beginning of the argument
8191 if the argument is concatenated with what precedes it. */
8192 if (p1
[0] == '\n' && p1
[1] == '-')
8194 } else if (!traditional
) {
8195 /* Ordinary expanded use of the argument.
8196 Put in newline-space markers to prevent token pasting. */
8197 xbuf
[totlen
++] = '\n';
8198 xbuf
[totlen
++] = ' ';
8200 if (ap
->raw_after
!= 0) {
8201 /* Arg is concatenated after: delete trailing whitespace,
8202 whitespace markers, and no-reexpansion markers. */
8204 if (is_space
[l1
[-1]]) l1
--;
8205 else if (l1
[-1] == '-') {
8206 U_CHAR
*p2
= l1
- 1;
8207 /* If a `-' is preceded by an odd number of newlines then it
8208 and the last newline are a no-reexpansion marker. */
8209 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8210 if ((l1
- 1 - p2
) & 1) {
8219 bcopy ((char *) p1
, (char *) (xbuf
+ totlen
), l1
- p1
);
8221 if (!traditional
&& ap
->raw_after
== 0) {
8222 /* Ordinary expanded use of the argument.
8223 Put in newline-space markers to prevent token pasting. */
8224 xbuf
[totlen
++] = '\n';
8225 xbuf
[totlen
++] = ' ';
8228 /* Ordinary expanded use of the argument.
8229 Put in newline-space markers to prevent token pasting. */
8231 xbuf
[totlen
++] = '\n';
8232 xbuf
[totlen
++] = ' ';
8234 bcopy ((char *) arg
->expanded
, (char *) (xbuf
+ totlen
),
8235 arg
->expand_length
);
8236 totlen
+= arg
->expand_length
;
8238 xbuf
[totlen
++] = '\n';
8239 xbuf
[totlen
++] = ' ';
8241 /* If a macro argument with newlines is used multiple times,
8242 then only expand the newlines once. This avoids creating output
8243 lines which don't correspond to any input line, which confuses
8245 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8246 /* Don't bother doing change_newlines for subsequent
8250 = change_newlines (arg
->expanded
, arg
->expand_length
);
8254 if (totlen
> xbuf_len
)
8258 /* if there is anything left of the definition
8259 after handling the arg list, copy that in too. */
8261 for (i
= offset
; i
< defn
->length
; i
++) {
8262 /* if we've reached the end of the macro */
8265 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8266 && last_ap
->raw_after
!= 0))
8267 xbuf
[totlen
++] = exp
[i
];
8273 for (i
= 0; i
< nargs
; i
++) {
8274 if (args
[i
].free1
!= 0)
8275 free (args
[i
].free1
);
8276 if (args
[i
].free2
!= 0)
8277 free (args
[i
].free2
);
8281 xbuf
= defn
->expansion
;
8282 xbuf_len
= defn
->length
;
8285 /* Now put the expansion on the input stack
8286 so our caller will commence reading from it. */
8288 register FILE_BUF
*ip2
;
8290 ip2
= &instack
[++indepth
];
8293 ip2
->nominal_fname
= 0;
8295 /* This may not be exactly correct, but will give much better error
8296 messages for nested macro calls than using a line number of zero. */
8297 ip2
->lineno
= start_line
;
8299 ip2
->length
= xbuf_len
;
8301 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8303 ip2
->if_stack
= if_stack
;
8304 ip2
->system_header_p
= 0;
8306 /* Recursive macro use sometimes works traditionally.
8307 #define foo(x,y) bar (x (y,0), y)
8311 hp
->type
= T_DISABLED
;
8316 * Parse a macro argument and store the info on it into *ARGPTR.
8317 * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8318 * Return nonzero to indicate a syntax error.
8322 macarg (argptr
, rest_args
)
8323 register struct argdata
*argptr
;
8326 FILE_BUF
*ip
= &instack
[indepth
];
8332 /* Try to parse as much of the argument as exists at this
8333 input stack level. */
8334 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
8335 &paren
, &newlines
, &comments
, rest_args
);
8337 /* If we find the end of the argument at this level,
8338 set up *ARGPTR to point at it in the input stack. */
8339 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
8340 && bp
!= ip
->buf
+ ip
->length
) {
8342 argptr
->raw
= ip
->bufp
;
8343 argptr
->raw_length
= bp
- ip
->bufp
;
8344 argptr
->newlines
= newlines
;
8348 /* This input stack level ends before the macro argument does.
8349 We must pop levels and keep parsing.
8350 Therefore, we must allocate a temporary buffer and copy
8351 the macro argument into it. */
8352 int bufsize
= bp
- ip
->bufp
;
8353 int extra
= newlines
;
8354 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8355 int final_start
= 0;
8357 bcopy ((char *) ip
->bufp
, (char *) buffer
, bufsize
);
8359 ip
->lineno
+= newlines
;
8361 while (bp
== ip
->buf
+ ip
->length
) {
8362 if (instack
[indepth
].macro
== 0) {
8363 result
= "unterminated macro call";
8366 ip
->macro
->type
= T_MACRO
;
8368 free (ip
->free_ptr
);
8369 ip
= &instack
[--indepth
];
8372 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
8373 &newlines
, &comments
, rest_args
);
8374 final_start
= bufsize
;
8375 bufsize
+= bp
- ip
->bufp
;
8377 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
8378 bcopy ((char *) ip
->bufp
, (char *) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
8381 ip
->lineno
+= newlines
;
8384 /* Now, if arg is actually wanted, record its raw form,
8385 discarding comments and duplicating newlines in whatever
8386 part of it did not come from a macro expansion.
8387 EXTRA space has been preallocated for duplicating the newlines.
8388 FINAL_START is the index of the start of that part. */
8390 argptr
->raw
= buffer
;
8391 argptr
->raw_length
= bufsize
;
8392 argptr
->free1
= buffer
;
8393 argptr
->newlines
= newlines
;
8394 if ((newlines
|| comments
) && ip
->fname
!= 0)
8397 discard_comments (argptr
->raw
+ final_start
,
8398 argptr
->raw_length
- final_start
,
8400 argptr
->raw
[argptr
->raw_length
] = 0;
8401 if (argptr
->raw_length
> bufsize
+ extra
)
8406 /* If we are not discarding this argument,
8407 macroexpand it and compute its length as stringified.
8408 All this info goes into *ARGPTR. */
8411 register U_CHAR
*buf
, *lim
;
8412 register int totlen
;
8415 lim
= buf
+ argptr
->raw_length
;
8417 while (buf
!= lim
&& is_space
[*buf
])
8419 while (buf
!= lim
&& is_space
[lim
[-1]])
8421 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
8422 while (buf
!= lim
) {
8423 register U_CHAR c
= *buf
++;
8425 /* Internal sequences of whitespace are replaced by one space
8426 in most cases, but not always. So count all the whitespace
8427 in case we need to keep it all. */
8430 SKIP_ALL_WHITE_SPACE (buf
);
8433 if (c
== '\"' || c
== '\\') /* escape these chars */
8435 else if (!isprint (c
))
8438 argptr
->stringified_length
= totlen
;
8443 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8444 counting parens in *DEPTHPTR,
8445 and return if reach LIMIT
8446 or before a `)' that would make *DEPTHPTR negative
8447 or before a comma when *DEPTHPTR is zero.
8448 Single and double quotes are matched and termination
8449 is inhibited within them. Comments also inhibit it.
8450 Value returned is pointer to stopping place.
8452 Increment *NEWLINES each time a newline is passed.
8453 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8454 Set *COMMENTS to 1 if a comment is seen. */
8457 macarg1 (start
, limit
, depthptr
, newlines
, comments
, rest_args
)
8459 register U_CHAR
*limit
;
8460 int *depthptr
, *newlines
, *comments
;
8463 register U_CHAR
*bp
= start
;
8465 while (bp
< limit
) {
8471 if (--(*depthptr
) < 0)
8475 /* Traditionally, backslash makes following char not special. */
8476 if (bp
+ 1 < limit
&& traditional
)
8479 /* But count source lines anyway. */
8488 if (bp
[1] == '\\' && bp
[2] == '\n')
8489 newline_fix (bp
+ 1);
8492 for (bp
+= 2; bp
< limit
; bp
++) {
8495 else if (*bp
== '*') {
8496 if (bp
[-1] == '/' && warn_comments
)
8497 warning ("`/*' within comment");
8498 if (bp
[1] == '\\' && bp
[2] == '\n')
8499 newline_fix (bp
+ 1);
8506 } else if (bp
[1] == '/' && cplusplus_comments
) {
8508 for (bp
+= 2; bp
< limit
; bp
++) {
8514 warning ("multiline `//' comment");
8523 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
8528 while (*bp
== '\\' && bp
[1] == '\n') {
8531 } else if (*bp
== '\n') {
8540 /* if we've returned to lowest level and we aren't absorbing all args */
8541 if ((*depthptr
) == 0 && rest_args
== 0)
8551 /* Discard comments and duplicate newlines
8552 in the string of length LENGTH at START,
8553 except inside of string constants.
8554 The string is copied into itself with its beginning staying fixed.
8556 NEWLINES is the number of newlines that must be duplicated.
8557 We assume that that much extra space is available past the end
8561 discard_comments (start
, length
, newlines
)
8566 register U_CHAR
*ibp
;
8567 register U_CHAR
*obp
;
8568 register U_CHAR
*limit
;
8571 /* If we have newlines to duplicate, copy everything
8572 that many characters up. Then, in the second part,
8573 we will have room to insert the newlines
8575 NEWLINES may actually be too large, because it counts
8576 newlines in string constants, and we don't duplicate those.
8577 But that does no harm. */
8579 ibp
= start
+ length
;
8580 obp
= ibp
+ newlines
;
8582 while (limit
!= ibp
)
8586 ibp
= start
+ newlines
;
8587 limit
= start
+ length
+ newlines
;
8590 while (ibp
< limit
) {
8591 *obp
++ = c
= *ibp
++;
8594 /* Duplicate the newline. */
8606 if (*ibp
== '\\' && ibp
[1] == '\n')
8608 /* Delete any comment. */
8609 if (cplusplus_comments
&& ibp
[0] == '/') {
8610 /* Comments are equivalent to spaces. */
8613 while (ibp
< limit
&& (*ibp
!= '\n' || ibp
[-1] == '\\'))
8617 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
8619 /* Comments are equivalent to spaces.
8620 For -traditional, a comment is equivalent to nothing. */
8626 while (ibp
+ 1 < limit
) {
8628 && ibp
[1] == '\\' && ibp
[2] == '\n')
8629 newline_fix (ibp
+ 1);
8630 if (ibp
[0] == '*' && ibp
[1] == '/')
8639 /* Notice and skip strings, so that we don't
8640 think that comments start inside them,
8641 and so we don't duplicate newlines in them. */
8644 while (ibp
< limit
) {
8645 *obp
++ = c
= *ibp
++;
8648 if (c
== '\n' && quotec
== '\'')
8650 if (c
== '\\' && ibp
< limit
) {
8651 while (*ibp
== '\\' && ibp
[1] == '\n')
8664 /* Turn newlines to spaces in the string of length LENGTH at START,
8665 except inside of string constants.
8666 The string is copied into itself with its beginning staying fixed. */
8669 change_newlines (start
, length
)
8673 register U_CHAR
*ibp
;
8674 register U_CHAR
*obp
;
8675 register U_CHAR
*limit
;
8679 limit
= start
+ length
;
8682 while (ibp
< limit
) {
8683 *obp
++ = c
= *ibp
++;
8686 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
8687 string. Skip past the newline and its duplicate.
8688 Put a space in the output. */
8699 /* Notice and skip strings, so that we don't delete newlines in them. */
8702 while (ibp
< limit
) {
8703 *obp
++ = c
= *ibp
++;
8706 if (c
== '\n' && quotec
== '\'')
8718 * my_strerror - return the descriptive text associated with an `errno' code.
8722 my_strerror (errnum
)
8728 #ifndef HAVE_STRERROR
8729 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
8731 result
= strerror (errnum
);
8734 /* VAXCRTL's strerror() takes an optional second argument, which only
8735 matters when the first argument is EVMSERR. However, it's simplest
8736 just to pass it unconditionally. `vaxc$errno' is declared in
8737 <errno.h>, and maintained by the library in parallel with `errno'.
8738 We assume that caller's `errnum' either matches the last setting of
8739 `errno' by the library or else does not have the value `EVMSERR'. */
8741 result
= strerror (errnum
, vaxc$errno
);
8745 result
= "undocumented I/O error";
8751 * error - print error message and increment count of errors.
8755 error (PRINTF_ALIST (msg
))
8760 VA_START (args
, msg
);
8771 FILE_BUF
*ip
= NULL
;
8773 print_containing_files ();
8775 for (i
= indepth
; i
>= 0; i
--)
8776 if (instack
[i
].fname
!= NULL
) {
8782 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8783 vfprintf (stderr
, msg
, args
);
8784 fprintf (stderr
, "\n");
8788 /* Error including a message from `errno'. */
8791 error_from_errno (name
)
8795 FILE_BUF
*ip
= NULL
;
8797 print_containing_files ();
8799 for (i
= indepth
; i
>= 0; i
--)
8800 if (instack
[i
].fname
!= NULL
) {
8806 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8808 fprintf (stderr
, "%s: %s\n", name
, my_strerror (errno
));
8813 /* Print error message but don't count it. */
8816 warning (PRINTF_ALIST (msg
))
8821 VA_START (args
, msg
);
8822 vwarning (msg
, args
);
8827 vwarning (msg
, args
)
8832 FILE_BUF
*ip
= NULL
;
8834 if (inhibit_warnings
)
8837 if (warnings_are_errors
)
8840 print_containing_files ();
8842 for (i
= indepth
; i
>= 0; i
--)
8843 if (instack
[i
].fname
!= NULL
) {
8849 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8850 fprintf (stderr
, "warning: ");
8851 vfprintf (stderr
, msg
, args
);
8852 fprintf (stderr
, "\n");
8856 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8857 error_with_line (int line
, PRINTF_ALIST (msg
))
8859 error_with_line (line
, PRINTF_ALIST (msg
))
8866 VA_START (args
, msg
);
8867 verror_with_line (line
, msg
, args
);
8872 verror_with_line (line
, msg
, args
)
8878 FILE_BUF
*ip
= NULL
;
8880 print_containing_files ();
8882 for (i
= indepth
; i
>= 0; i
--)
8883 if (instack
[i
].fname
!= NULL
) {
8889 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, line
);
8890 vfprintf (stderr
, msg
, args
);
8891 fprintf (stderr
, "\n");
8896 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8897 warning_with_line (int line
, PRINTF_ALIST (msg
))
8899 warning_with_line (line
, PRINTF_ALIST (msg
))
8906 VA_START (args
, msg
);
8907 vwarning_with_line (line
, msg
, args
);
8912 vwarning_with_line (line
, msg
, args
)
8918 FILE_BUF
*ip
= NULL
;
8920 if (inhibit_warnings
)
8923 if (warnings_are_errors
)
8926 print_containing_files ();
8928 for (i
= indepth
; i
>= 0; i
--)
8929 if (instack
[i
].fname
!= NULL
) {
8935 fprintf (stderr
, line
? "%s:%d: " : "%s: ", ip
->nominal_fname
, line
);
8936 fprintf (stderr
, "warning: ");
8937 vfprintf (stderr
, msg
, args
);
8938 fprintf (stderr
, "\n");
8941 /* print an error message and maybe count it. */
8944 pedwarn (PRINTF_ALIST (msg
))
8949 VA_START (args
, msg
);
8950 if (pedantic_errors
)
8953 vwarning (msg
, args
);
8958 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8959 pedwarn_with_line (int line
, PRINTF_ALIST (msg
))
8961 pedwarn_with_line (line
, PRINTF_ALIST (msg
))
8968 VA_START (args
, msg
);
8969 if (pedantic_errors
)
8970 verror_with_line (line
, msg
, args
);
8972 vwarning_with_line (line
, msg
, args
);
8976 /* Report a warning (or an error if pedantic_errors)
8977 giving specified file name and line number, not current. */
8980 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8981 pedwarn_with_file_and_line (char *file
, int line
, PRINTF_ALIST (msg
))
8983 pedwarn_with_file_and_line (file
, line
, PRINTF_ALIST (msg
))
8991 if (!pedantic_errors
&& inhibit_warnings
)
8994 fprintf (stderr
, "%s:%d: ", file
, line
);
8995 if (pedantic_errors
)
8997 if (!pedantic_errors
)
8998 fprintf (stderr
, "warning: ");
8999 VA_START (args
, msg
);
9000 vfprintf (stderr
, msg
, args
);
9002 fprintf (stderr
, "\n");
9005 /* Print the file names and line numbers of the #include
9006 directives which led to the current file. */
9009 print_containing_files ()
9011 FILE_BUF
*ip
= NULL
;
9015 /* If stack of files hasn't changed since we last printed
9016 this info, don't repeat it. */
9017 if (last_error_tick
== input_file_stack_tick
)
9020 for (i
= indepth
; i
>= 0; i
--)
9021 if (instack
[i
].fname
!= NULL
) {
9026 /* Give up if we don't find a source file. */
9030 /* Find the other, outer source files. */
9031 for (i
--; i
>= 0; i
--)
9032 if (instack
[i
].fname
!= NULL
) {
9036 fprintf (stderr
, "In file included");
9038 fprintf (stderr
, ",\n ");
9041 fprintf (stderr
, " from %s:%d", ip
->nominal_fname
, ip
->lineno
);
9044 fprintf (stderr
, ":\n");
9046 /* Record we have printed the status as of this time. */
9047 last_error_tick
= input_file_stack_tick
;
9050 /* Return the line at which an error occurred.
9051 The error is not necessarily associated with the current spot
9052 in the input stack, so LINE says where. LINE will have been
9053 copied from ip->lineno for the current input level.
9054 If the current level is for a file, we return LINE.
9055 But if the current level is not for a file, LINE is meaningless.
9056 In that case, we return the lineno of the innermost file. */
9059 line_for_error (line
)
9065 for (i
= indepth
; i
>= 0; ) {
9066 if (instack
[i
].fname
!= 0)
9071 line1
= instack
[i
].lineno
;
9079 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9081 * As things stand, nothing is ever placed in the output buffer to be
9082 * removed again except when it's KNOWN to be part of an identifier,
9083 * so flushing and moving down everything left, instead of expanding,
9087 /* You might think void was cleaner for the return type,
9088 but that would get type mismatch in check_expand in strict ANSI. */
9090 grow_outbuf (obuf
, needed
)
9091 register FILE_BUF
*obuf
;
9092 register int needed
;
9097 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9100 /* Make it at least twice as big as it is now. */
9102 /* Make it have at least 150% of the free space we will need. */
9103 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9104 if (minsize
> obuf
->length
)
9105 obuf
->length
= minsize
;
9107 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
9110 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9116 /* Symbol table for macro names and special symbols */
9119 * install a name in the main hash table, even if it is already there.
9120 * name stops with first non alphanumeric, except leading '#'.
9121 * caller must check against redefinition if that is desired.
9122 * delete_macro () removes things installed by install () in fifo order.
9123 * this is important because of the `defined' special symbol used
9124 * in #if, and also if pushdef/popdef directives are ever implemented.
9126 * If LEN is >= 0, it is the length of the name.
9127 * Otherwise, compute the length by scanning the entire name.
9129 * If HASH is >= 0, it is the precomputed hash code.
9130 * Otherwise, compute the hash code.
9133 install (name
, len
, type
, value
, hash
)
9136 enum node_type type
;
9140 register HASHNODE
*hp
;
9141 register int i
, bucket
;
9142 register U_CHAR
*p
, *q
;
9146 while (is_idchar
[*p
])
9152 hash
= hashf (name
, len
, HASHSIZE
);
9154 i
= sizeof (HASHNODE
) + len
+ 1;
9155 hp
= (HASHNODE
*) xmalloc (i
);
9157 hp
->bucket_hdr
= &hashtab
[bucket
];
9158 hp
->next
= hashtab
[bucket
];
9159 hashtab
[bucket
] = hp
;
9161 if (hp
->next
!= NULL
)
9162 hp
->next
->prev
= hp
;
9165 hp
->value
.cpval
= value
;
9166 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9169 for (i
= 0; i
< len
; i
++)
9176 * find the most recent hash node for name name (ending with first
9177 * non-identifier char) installed by install
9179 * If LEN is >= 0, it is the length of the name.
9180 * Otherwise, compute the length by scanning the entire name.
9182 * If HASH is >= 0, it is the precomputed hash code.
9183 * Otherwise, compute the hash code.
9186 lookup (name
, len
, hash
)
9191 register U_CHAR
*bp
;
9192 register HASHNODE
*bucket
;
9195 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
9200 hash
= hashf (name
, len
, HASHSIZE
);
9202 bucket
= hashtab
[hash
];
9204 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
9206 bucket
= bucket
->next
;
9212 * Delete a hash node. Some weirdness to free junk from macros.
9213 * More such weirdness will have to be added if you define more hash
9214 * types that need it.
9217 /* Note that the DEFINITION of a macro is removed from the hash table
9218 but its storage is not freed. This would be a storage leak
9219 except that it is not reasonable to keep undefining and redefining
9220 large numbers of macros many times.
9221 In any case, this is necessary, because a macro can be #undef'd
9222 in the middle of reading the arguments to a call to it.
9223 If #undef freed the DEFINITION, that would crash. */
9230 if (hp
->prev
!= NULL
)
9231 hp
->prev
->next
= hp
->next
;
9232 if (hp
->next
!= NULL
)
9233 hp
->next
->prev
= hp
->prev
;
9235 /* make sure that the bucket chain header that
9236 the deleted guy was on points to the right thing afterwards. */
9237 if (hp
== *hp
->bucket_hdr
)
9238 *hp
->bucket_hdr
= hp
->next
;
9241 if (hp
->type
== T_MACRO
) {
9242 DEFINITION
*d
= hp
->value
.defn
;
9243 struct reflist
*ap
, *nextap
;
9245 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
9256 * return hash function on name. must be compatible with the one
9257 * computed a step at a time, elsewhere
9260 hashf (name
, len
, hashsize
)
9261 register U_CHAR
*name
;
9268 r
= HASHSTEP (r
, *name
++);
9270 return MAKE_POS (r
) % hashsize
;
9274 /* Dump the definition of a single macro HP to OF. */
9276 dump_single_macro (hp
, of
)
9277 register HASHNODE
*hp
;
9280 register DEFINITION
*defn
= hp
->value
.defn
;
9286 /* Print the definition of the macro HP. */
9288 fprintf (of
, "#define %s", hp
->name
);
9290 if (defn
->nargs
>= 0) {
9294 for (i
= 0; i
< defn
->nargs
; i
++) {
9295 dump_arg_n (defn
, i
, of
);
9296 if (i
+ 1 < defn
->nargs
)
9306 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
9307 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
9308 offset
+= ap
->nchars
;
9310 if (ap
->nchars
!= 0)
9312 if (ap
->stringify
) {
9313 switch (ap
->stringify
) {
9314 case SHARP_TOKEN
: fprintf (of
, "#"); break;
9315 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
9316 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
9317 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
9321 if (ap
->raw_before
!= 0) {
9323 switch (ap
->raw_before
) {
9324 case WHITE_SHARP_TOKEN
:
9325 case WHITE_PERCENT_COLON_TOKEN
:
9332 switch (ap
->raw_before
) {
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;
9343 dump_arg_n (defn
, ap
->argno
, of
);
9344 if (!traditional
&& ap
->raw_after
!= 0) {
9345 switch (ap
->raw_after
) {
9346 case SHARP_TOKEN
: fprintf (of
, "##"); break;
9347 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
9348 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
9349 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
9355 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
9359 /* Dump all macro definitions as #defines to stdout. */
9366 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
9367 register HASHNODE
*hp
;
9369 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
9370 if (hp
->type
== T_MACRO
)
9371 dump_single_macro (hp
, stdout
);
9376 /* Output to OF a substring of a macro definition.
9377 BASE is the beginning of the definition.
9378 Output characters START thru LENGTH.
9379 Unless traditional, discard newlines outside of strings, thus
9380 converting funny-space markers to ordinary spaces. */
9383 dump_defn_1 (base
, start
, length
, of
)
9389 U_CHAR
*p
= base
+ start
;
9390 U_CHAR
*limit
= base
+ start
+ length
;
9393 fwrite (p
, sizeof (*p
), length
, of
);
9396 if (*p
== '\"' || *p
=='\'') {
9397 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
9398 NULL_PTR
, NULL_PTR
);
9399 fwrite (p
, sizeof (*p
), p1
- p
, of
);
9410 /* Print the name of argument number ARGNUM of macro definition DEFN
9412 Recall that DEFN->args.argnames contains all the arg names
9413 concatenated in reverse order with comma-space in between. */
9416 dump_arg_n (defn
, argnum
, of
)
9421 register U_CHAR
*p
= defn
->args
.argnames
;
9422 while (argnum
+ 1 < defn
->nargs
) {
9423 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
9427 while (*p
&& *p
!= ',') {
9433 /* Initialize syntactic classifications of characters. */
9436 initialize_char_syntax ()
9441 * Set up is_idchar and is_idstart tables. These should be
9442 * faster than saying (is_alpha (c) || c == '_'), etc.
9443 * Set up these things before calling any routines tthat
9446 for (i
= 'a'; i
<= 'z'; i
++) {
9447 is_idchar
[i
- 'a' + 'A'] = 1;
9449 is_idstart
[i
- 'a' + 'A'] = 1;
9452 for (i
= '0'; i
<= '9'; i
++)
9455 is_idstart
['_'] = 1;
9456 is_idchar
['$'] = dollars_in_ident
;
9457 is_idstart
['$'] = dollars_in_ident
;
9459 /* horizontal space table */
9460 is_hor_space
[' '] = 1;
9461 is_hor_space
['\t'] = 1;
9462 is_hor_space
['\v'] = 1;
9463 is_hor_space
['\f'] = 1;
9464 is_hor_space
['\r'] = 1;
9473 char_name
['\v'] = "vertical tab";
9474 char_name
['\f'] = "formfeed";
9475 char_name
['\r'] = "carriage return";
9478 /* Initialize the built-in macros. */
9481 initialize_builtins (inp
, outp
)
9485 install ((U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
9486 install ((U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
9487 install ((U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
9488 install ((U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
9489 install ((U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
9490 install ((U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
9491 #ifndef NO_BUILTIN_SIZE_TYPE
9492 install ((U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
9494 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9495 install ((U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
9497 install ((U_CHAR
*) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, NULL_PTR
, -1);
9498 install ((U_CHAR
*) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
,
9500 install ((U_CHAR
*) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
,
9502 install ((U_CHAR
*) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE
,
9504 install ((U_CHAR
*) "__TIME__", -1, T_TIME
, NULL_PTR
, -1);
9506 install ((U_CHAR
*) "__STDC__", -1, T_CONST
, "1", -1);
9507 install ((U_CHAR
*) "__STDC_VERSION__", -1, T_CONST
, "199409L", -1);
9510 install ((U_CHAR
*) "__OBJC__", -1, T_CONST
, "1", -1);
9511 /* This is supplied using a -D by the compiler driver
9512 so that it is present only when truly compiling with GNU C. */
9513 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
9514 install ((U_CHAR
*) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST
, "1", -1);
9518 char directive
[2048];
9519 U_CHAR
*udirective
= (U_CHAR
*) directive
;
9520 register struct directive
*dp
= &directive_table
[0];
9521 struct tm
*timebuf
= timestamp ();
9523 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
9524 instack
[0].nominal_fname
);
9525 output_line_directive (inp
, outp
, 0, same_file
);
9526 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9529 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
9530 output_line_directive (inp
, outp
, 0, same_file
);
9531 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9534 #ifndef NO_BUILTIN_SIZE_TYPE
9535 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
9536 output_line_directive (inp
, outp
, 0, same_file
);
9537 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9541 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9542 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
9543 output_line_directive (inp
, outp
, 0, same_file
);
9544 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9548 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
9549 output_line_directive (inp
, outp
, 0, same_file
);
9550 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9553 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
9554 monthnames
[timebuf
->tm_mon
],
9555 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
9556 output_line_directive (inp
, outp
, 0, same_file
);
9557 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9560 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
9561 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
9562 output_line_directive (inp
, outp
, 0, same_file
);
9563 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9568 sprintf (directive
, " __STDC__ 1");
9569 output_line_directive (inp
, outp
, 0, same_file
);
9570 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9575 sprintf (directive
, " __OBJC__ 1");
9576 output_line_directive (inp
, outp
, 0, same_file
);
9577 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9584 * process a given definition string, for initialization
9585 * If STR is just an identifier, define it with value 1.
9586 * If STR has anything after the identifier, then it should
9587 * be identifier=definition.
9591 make_definition (str
, op
)
9596 struct directive
*kt
;
9599 p
= buf
= (U_CHAR
*) str
;
9600 if (!is_idstart
[*p
]) {
9601 error ("malformed option `-D %s'", str
);
9604 while (is_idchar
[*++p
])
9607 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
9610 p
= (U_CHAR
*) str
; /* Error */
9613 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
9614 strcpy ((char *)buf
, str
);
9615 strcat ((char *)buf
, " 1");
9616 } else if (*p
!= '=') {
9617 error ("malformed option `-D %s'", str
);
9621 /* Copy the entire option so we can modify it. */
9622 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
9623 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
9624 /* Change the = to a space. */
9625 buf
[p
- (U_CHAR
*) str
] = ' ';
9626 /* Scan for any backslash-newline and remove it. */
9628 q
= &buf
[p
- (U_CHAR
*) str
];
9630 if (*p
== '\"' || *p
== '\'') {
9631 int unterminated
= 0;
9632 U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
9633 NULL_PTR
, NULL_PTR
, &unterminated
);
9637 if (*p
== '\\' && p
[1] == '\n')
9641 } else if (*p
== '\\' && p
[1] == '\n')
9643 /* Change newline chars into newline-markers. */
9644 else if (*p
== '\n')
9656 ip
= &instack
[++indepth
];
9657 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
9659 ip
->buf
= ip
->bufp
= buf
;
9660 ip
->length
= strlen ((char *) buf
);
9664 ip
->if_stack
= if_stack
;
9665 ip
->system_header_p
= 0;
9667 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
9670 /* Pass NULL instead of OP, since this is a "predefined" macro. */
9671 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
9675 /* JF, this does the work for the -U option */
9678 make_undef (str
, op
)
9683 struct directive
*kt
;
9685 ip
= &instack
[++indepth
];
9686 ip
->nominal_fname
= ip
->fname
= "*undef*";
9688 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
9689 ip
->length
= strlen (str
);
9693 ip
->if_stack
= if_stack
;
9694 ip
->system_header_p
= 0;
9696 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
9699 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
9703 /* Process the string STR as if it appeared as the body of a #assert.
9704 OPTION is the option name for which STR was the argument. */
9707 make_assertion (option
, str
)
9712 struct directive
*kt
;
9713 U_CHAR
*buf
, *p
, *q
;
9715 /* Copy the entire option so we can modify it. */
9716 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
9717 strcpy ((char *) buf
, str
);
9718 /* Scan for any backslash-newline and remove it. */
9721 if (*p
== '\\' && p
[1] == '\n')
9729 if (!is_idstart
[*p
]) {
9730 error ("malformed option `%s %s'", option
, str
);
9733 while (is_idchar
[*++p
])
9735 SKIP_WHITE_SPACE (p
);
9736 if (! (*p
== 0 || *p
== '(')) {
9737 error ("malformed option `%s %s'", option
, str
);
9741 ip
= &instack
[++indepth
];
9742 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
9744 ip
->buf
= ip
->bufp
= buf
;
9745 ip
->length
= strlen ((char *) buf
);
9749 ip
->if_stack
= if_stack
;
9750 ip
->system_header_p
= 0;
9752 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
9755 /* pass NULL as output ptr to do_define since we KNOW it never
9756 does any output.... */
9757 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
9761 /* The previous include prefix, if any, is PREV_FILE_NAME.
9762 Allocate a new include prefix whose name is the
9763 simplified concatenation of PREFIX and NAME,
9764 with a trailing / added if needed.
9765 But return 0 if the include prefix should be ignored,
9766 e.g. because it is a duplicate of PREV_FILE_NAME. */
9768 static struct file_name_list
*
9769 new_include_prefix (prev_file_name
, prefix
, name
)
9770 struct file_name_list
*prev_file_name
;
9775 fatal ("Directory name missing after command line option");
9778 /* Ignore the empty string. */
9781 struct file_name_list
*dir
9782 = ((struct file_name_list
*)
9783 xmalloc (sizeof (struct file_name_list
)
9784 + strlen (prefix
) + strlen (name
) + 1 /* for trailing / */));
9786 strcpy (dir
->fname
, prefix
);
9787 strcat (dir
->fname
, name
);
9788 len
= simplify_filename (dir
->fname
);
9790 /* Convert directory name to a prefix. */
9791 if (dir
->fname
[len
- 1] != '/') {
9792 if (len
== 1 && dir
->fname
[len
- 1] == '.')
9795 dir
->fname
[len
++] = '/';
9796 dir
->fname
[len
] = 0;
9799 /* Ignore a directory whose name matches the previous one. */
9800 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
9801 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
9802 if (!first_bracket_include
)
9803 first_bracket_include
= prev_file_name
;
9809 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
9811 /* Ignore a nonexistent directory. */
9812 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
9813 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
9814 error_from_errno (dir
->fname
);
9819 /* Ignore a directory whose identity matches the previous one. */
9821 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
9822 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
9823 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
9824 if (!first_bracket_include
)
9825 first_bracket_include
= prev_file_name
;
9832 dir
->c_system_include_path
= 0;
9833 dir
->got_name_map
= 0;
9839 /* Append a chain of `struct file_name_list's
9840 to the end of the main include chain.
9841 FIRST is the beginning of the chain to append, and LAST is the end. */
9844 append_include_chain (first
, last
)
9845 struct file_name_list
*first
, *last
;
9847 struct file_name_list
*dir
;
9849 if (!first
|| !last
)
9855 last_include
->next
= first
;
9857 if (first_bracket_include
== 0)
9858 first_bracket_include
= first
;
9860 for (dir
= first
; ; dir
= dir
->next
) {
9861 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
9862 if (len
> max_include_len
)
9863 max_include_len
= len
;
9869 last_include
= last
;
9872 /* Add output to `deps_buffer' for the -M switch.
9873 STRING points to the text to be output.
9874 SPACER is ':' for targets, ' ' for dependencies. */
9877 deps_output (string
, spacer
)
9881 int size
= strlen (string
);
9886 #ifndef MAX_OUTPUT_COLUMNS
9887 #define MAX_OUTPUT_COLUMNS 72
9889 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
9890 && 1 < deps_column
) {
9891 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
9898 if (deps_size
+ size
+ 8 > deps_allocated_size
) {
9899 deps_allocated_size
= (deps_size
+ size
+ 50) * 2;
9900 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
9902 if (spacer
== ' ') {
9903 deps_buffer
[deps_size
++] = ' ';
9906 bcopy (string
, &deps_buffer
[deps_size
], size
);
9908 deps_column
+= size
;
9909 if (spacer
== ':') {
9910 deps_buffer
[deps_size
++] = ':';
9913 deps_buffer
[deps_size
] = 0;
9917 fatal (PRINTF_ALIST (msg
))
9922 fprintf (stderr
, "%s: ", progname
);
9923 VA_START (args
, msg
);
9924 vfprintf (stderr
, msg
, args
);
9926 fprintf (stderr
, "\n");
9927 exit (FATAL_EXIT_CODE
);
9930 /* More 'friendly' abort that prints the line and file.
9931 config.h can #define abort fancy_abort if you like that sort of thing. */
9936 fatal ("Internal gcc abort.");
9940 perror_with_name (name
)
9943 fprintf (stderr
, "%s: ", progname
);
9944 fprintf (stderr
, "%s: %s\n", name
, my_strerror (errno
));
9949 pfatal_with_name (name
)
9952 perror_with_name (name
);
9956 exit (FATAL_EXIT_CODE
);
9960 /* Handler for SIGPIPE. */
9964 /* If this is missing, some compilers complain. */
9967 fatal ("output pipe has been closed");
9973 fatal ("Memory exhausted.");
9981 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (size
);
9988 xrealloc (old
, size
)
9992 register GENERIC_PTR ptr
= (GENERIC_PTR
) realloc (old
, size
);
9999 xcalloc (number
, size
)
10000 size_t number
, size
;
10002 register size_t total
= number
* size
;
10003 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (total
);
10006 bzero (ptr
, total
);
10014 size_t size
= strlen (input
);
10015 char *output
= xmalloc (size
+ 1);
10016 strcpy (output
, input
);
10022 /* Under VMS we need to fix up the "include" specification
10023 filename so that everything following the 1st slash is
10024 changed into its correct VMS file specification. */
10027 hack_vms_include_specification (fname
)
10030 register char *cp
, *cp1
, *cp2
;
10031 int f
, check_filename_before_returning
;
10034 check_filename_before_returning
= 0;
10036 cp
= base_name (fname
);
10039 * Check if we have a vax-c style '#include filename'
10040 * and add the missing .h
10042 if (!index (cp
,'.'))
10045 cp2
= Local
; /* initialize */
10047 /* We are trying to do a number of things here. First of all, we are
10048 trying to hammer the filenames into a standard format, such that later
10049 processing can handle them.
10051 If the file name contains something like [dir.], then it recognizes this
10052 as a root, and strips the ".]". Later processing will add whatever is
10053 needed to get things working properly.
10055 If no device is specified, then the first directory name is taken to be
10056 a device name (or a rooted logical). */
10058 /* See if we found that 1st slash */
10059 if (cp
== 0) return; /* Nothing to do!!! */
10060 if (*cp
!= '/') return; /* Nothing to do!!! */
10061 /* Point to the UNIX filename part (which needs to be fixed!) */
10063 /* If the directory spec is not rooted, we can just copy
10064 the UNIX filename part and we are done */
10065 if (((cp
- fname
) > 1) && ((cp
[-1] == ']') || (cp
[-1] == '>'))) {
10066 if (cp
[-2] != '.') {
10068 * The VMS part ends in a `]', and the preceding character is not a `.'.
10069 * We strip the `]', and then splice the two parts of the name in the
10070 * usual way. Given the default locations for include files in cccp.c,
10071 * we will only use this code if the user specifies alternate locations
10072 * with the /include (-I) switch on the command line. */
10073 cp
-= 1; /* Strip "]" */
10074 cp1
--; /* backspace */
10077 * The VMS part has a ".]" at the end, and this will not do. Later
10078 * processing will add a second directory spec, and this would be a syntax
10079 * error. Thus we strip the ".]", and thus merge the directory specs.
10080 * We also backspace cp1, so that it points to a '/'. This inhibits the
10081 * generation of the 000000 root directory spec (which does not belong here
10084 cp
-= 2; /* Strip ".]" */
10085 cp1
--; }; /* backspace */
10088 /* We drop in here if there is no VMS style directory specification yet.
10089 * If there is no device specification either, we make the first dir a
10090 * device and try that. If we do not do this, then we will be essentially
10091 * searching the users default directory (as if they did a #include "asdf.h").
10093 * Then all we need to do is to push a '[' into the output string. Later
10094 * processing will fill this in, and close the bracket.
10096 if (cp
[-1] != ':') *cp2
++ = ':'; /* dev not in spec. take first dir */
10097 *cp2
++ = '['; /* Open the directory specification */
10100 /* at this point we assume that we have the device spec, and (at least
10101 the opening "[" for a directory specification. We may have directories
10102 specified already */
10104 /* If there are no other slashes then the filename will be
10105 in the "root" directory. Otherwise, we need to add
10106 directory specifications. */
10107 if (index (cp1
, '/') == 0) {
10108 /* Just add "000000]" as the directory string */
10109 strcpy (cp2
, "000000]");
10110 cp2
+= strlen (cp2
);
10111 check_filename_before_returning
= 1; /* we might need to fool with this later */
10113 /* As long as there are still subdirectories to add, do them. */
10114 while (index (cp1
, '/') != 0) {
10115 /* If this token is "." we can ignore it */
10116 if ((cp1
[0] == '.') && (cp1
[1] == '/')) {
10120 /* Add a subdirectory spec. Do not duplicate "." */
10121 if (cp2
[-1] != '.' && cp2
[-1] != '[' && cp2
[-1] != '<')
10123 /* If this is ".." then the spec becomes "-" */
10124 if ((cp1
[0] == '.') && (cp1
[1] == '.') && (cp
[2] == '/')) {
10125 /* Add "-" and skip the ".." */
10130 /* Copy the subdirectory */
10131 while (*cp1
!= '/') *cp2
++= *cp1
++;
10132 cp1
++; /* Skip the "/" */
10134 /* Close the directory specification */
10135 if (cp2
[-1] == '.') /* no trailing periods */
10139 /* Now add the filename */
10140 while (*cp1
) *cp2
++ = *cp1
++;
10142 /* Now append it to the original VMS spec. */
10143 strcpy (cp
, Local
);
10145 /* If we put a [000000] in the filename, try to open it first. If this fails,
10146 remove the [000000], and return that name. This provides flexibility
10147 to the user in that they can use both rooted and non-rooted logical names
10148 to point to the location of the file. */
10150 if (check_filename_before_returning
) {
10151 f
= open (fname
, O_RDONLY
, 0666);
10153 /* The file name is OK as it is, so return it as is. */
10157 /* The filename did not work. Try to remove the [000000] from the name,
10159 cp
= index (fname
, '[');
10160 cp2
= index (fname
, ']') + 1;
10161 strcpy (cp
, cp2
); /* this gets rid of it */
10169 /* These are the read/write replacement routines for
10170 VAX-11 "C". They make read/write behave enough
10171 like their UNIX counterparts that CCCP will work */
10174 read (fd
, buf
, size
)
10179 #undef read /* Get back the REAL read routine */
10181 register int total
= 0;
10183 /* Read until the buffer is exhausted */
10185 /* Limit each read to 32KB */
10186 i
= (size
> (32*1024)) ? (32*1024) : size
;
10187 i
= read (fd
, buf
, i
);
10189 if (i
== 0) return (total
);
10192 /* Account for this read */
10201 write (fd
, buf
, size
)
10206 #undef write /* Get back the REAL write routine */
10210 /* Limit individual writes to 32Kb */
10213 j
= (i
> (32*1024)) ? (32*1024) : i
;
10214 if (write (fd
, buf
, j
) < 0) return (-1);
10215 /* Account for the data written */
10222 /* The following wrapper functions supply additional arguments to the VMS
10223 I/O routines to optimize performance with file handling. The arguments
10225 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
10226 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
10227 "fop=tef"- Truncate unused portions of file when closing file.
10228 "shr=nil"- Disallow file sharing while file is open.
10232 freopen (fname
, type
, oldfile
)
10237 #undef freopen /* Get back the REAL fopen routine */
10238 if (strcmp (type
, "w") == 0)
10239 return freopen (fname
, type
, oldfile
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
10240 return freopen (fname
, type
, oldfile
, "mbc=16");
10244 fopen (fname
, type
)
10248 #undef fopen /* Get back the REAL fopen routine */
10249 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
10250 fixed arguments, which matches ANSI's specification but not VAXCRTL's
10251 pre-ANSI implementation. This hack circumvents the mismatch problem. */
10252 FILE *(*vmslib_fopen
)() = (FILE *(*)()) fopen
;
10255 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
10256 "deq=64", "fop=tef", "shr=nil");
10258 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
10262 open (fname
, flags
, prot
)
10267 #undef open /* Get back the REAL open routine */
10268 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
10271 /* more VMS hackery */
10275 extern unsigned long sys$
parse(), sys$
search();
10277 /* Work around another library bug. If a file is located via a searchlist,
10278 and if the device it's on is not the same device as the one specified
10279 in the first element of that searchlist, then both stat() and fstat()
10280 will fail to return info about it. `errno' will be set to EVMSERR, and
10281 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
10282 We can get around this by fully parsing the filename and then passing
10283 that absolute name to stat().
10285 Without this fix, we can end up failing to find header files, which is
10286 bad enough, but then compounding the problem by reporting the reason for
10287 failure as "normal successful completion." */
10289 #undef fstat /* get back to library version */
10292 VMS_fstat (fd
, statbuf
)
10294 struct stat
*statbuf
;
10296 int result
= fstat (fd
, statbuf
);
10301 char nambuf
[NAM$C_MAXRSS
+1];
10303 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
10304 result
= VMS_stat (nambuf
, statbuf
);
10305 /* No fclose(fp) here; that would close(fd) as well. */
10312 VMS_stat (name
, statbuf
)
10314 struct stat
*statbuf
;
10316 int result
= stat (name
, statbuf
);
10322 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for sys$parse */
10323 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for sys$search */
10326 fab
.fab$l_fna
= (char *) name
;
10327 fab
.fab$b_fns
= (unsigned char) strlen (name
);
10328 fab
.fab$l_nam
= (void *) &nam
;
10330 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
10331 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
10332 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
10333 if (sys$
parse (&fab
) & 1)
10335 if (sys$
search (&fab
) & 1)
10337 res_nam
[nam
.nam$b_rsl
] = '\0';
10338 result
= stat (res_nam
, statbuf
);
10340 /* Clean up searchlist context cached by the system. */
10341 nam
.nam$b_nop
= NAM$M_SYNCHK
;
10342 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
10343 (void) sys$
parse (&fab
);