1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-96, 1997 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 typedef unsigned char U_CHAR
;
29 #include "../src/config.h"
37 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
38 for the sake of machines with limited C compilers. */
41 #endif /* not EMACS */
43 #ifndef STANDARD_INCLUDE_DIR
44 #define STANDARD_INCLUDE_DIR "/usr/include"
49 /* By default, colon separates directories in a path. */
50 #ifndef PATH_SEPARATOR
51 #define PATH_SEPARATOR ':'
54 /* By default, the suffix for object files is ".o". */
56 #define HAVE_OBJECT_SUFFIX
58 #define OBJECT_SUFFIX ".o"
61 #include <sys/types.h>
67 /* The following symbols should be autoconfigured:
72 In the mean time, we'll get by with approximations based
73 on existing GCC configuration symbols. */
77 # define STDC_HEADERS 1
79 #endif /* defined (POSIX) */
81 #if defined (POSIX) || (defined (USG) && !defined (VMS))
83 # define HAVE_FCNTL_H 1
90 # if TIME_WITH_SYS_TIME
91 # include <sys/time.h>
95 # include <sys/time.h>
100 # include <sys/resource.h>
122 # define bcmp(a, b, n) memcmp (a, b, n)
125 # define bcopy(s, d, n) memcpy (d, s, n)
128 # define bzero(d, n) memset (d, 0, n)
130 #else /* !STDC_HEADERS */
134 # if !defined (BSTRING) && (defined (USG) || defined (VMS))
137 # define bcmp my_bcmp
150 # endif /* !defined (bcmp) */
153 # define bcopy my_bcopy
163 # endif /* !defined (bcopy) */
166 # define bzero my_bzero
170 register unsigned length
;
175 # endif /* !defined (bzero) */
177 # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
178 #endif /* ! STDC_HEADERS */
180 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
181 # define __attribute__(x)
185 # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
186 # define PROTO(ARGS) ARGS
188 # define PROTO(ARGS) ()
192 #if defined (__STDC__) && defined (HAVE_VPRINTF)
194 # define VA_START(va_list, var) va_start (va_list, var)
195 # define PRINTF_ALIST(msg) char *msg, ...
196 # define PRINTF_DCL(msg)
197 # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (__printf__, m, n)))
199 # include <varargs.h>
200 # define VA_START(va_list, var) va_start (va_list)
201 # define PRINTF_ALIST(msg) msg, va_alist
202 # define PRINTF_DCL(msg) char *msg; va_dcl
203 # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (__printf__, m, n)))
204 # define vfprintf(file, msg, args) \
206 char *a0 = va_arg(args, char *); \
207 char *a1 = va_arg(args, char *); \
208 char *a2 = va_arg(args, char *); \
209 char *a3 = va_arg(args, char *); \
210 fprintf (file, msg, a0, a1, a2, a3); \
214 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
215 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
216 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
222 /* VMS-specific definitions */
225 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
226 #define fopen(fname,mode) VMS_fopen (fname,mode)
227 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
228 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
229 static int VMS_fstat (), VMS_stat ();
230 static int VMS_open ();
231 static FILE *VMS_fopen ();
232 static FILE *VMS_freopen ();
233 static void hack_vms_include_specification ();
234 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
235 #define INO_T_HASH(a) 0
236 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
239 /* Windows does not natively support inodes, and neither does MSDOS. */
240 #if (defined (_WIN32) && ! defined (CYGWIN32)) || defined (__MSDOS__)
241 #define INO_T_EQ(a, b) 0
250 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
251 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
253 /* Find the largest host integer type and set its size and type.
254 Watch out: on some crazy hosts `long' is shorter than `int'. */
256 #ifndef HOST_WIDE_INT
258 # include <inttypes.h>
259 # define HOST_WIDE_INT intmax_t
261 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
262 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
263 # define HOST_WIDE_INT int
265 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
266 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
267 # define HOST_WIDE_INT long
269 # define HOST_WIDE_INT long long
276 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
280 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
284 #define INO_T_EQ(a, b) ((a) == (b))
288 #define INO_T_HASH(a) (a)
291 /* Define a generic NULL if one hasn't already been defined. */
298 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
299 #define GENERIC_PTR void *
301 #define GENERIC_PTR char *
306 #define NULL_PTR ((GENERIC_PTR) 0)
309 #ifndef INCLUDE_LEN_FUDGE
310 #define INCLUDE_LEN_FUDGE 0
313 /* External declarations. */
315 extern char *version_string
;
316 extern char *update_path
PROTO((char *, char *));
318 #ifndef HAVE_STRERROR
320 extern char *sys_errlist
[];
321 #else /* HAVE_STRERROR */
325 char *strerror (int,...);
327 HOST_WIDE_INT parse_escape
PROTO((char **, HOST_WIDE_INT
));
328 HOST_WIDE_INT parse_c_expression
PROTO((char *));
334 /* Name under which this program was invoked. */
336 static char *progname
;
338 /* Nonzero means use extra default include directories for C++. */
340 static int cplusplus
;
342 /* Nonzero means handle cplusplus style comments */
344 static int cplusplus_comments
;
346 /* Nonzero means handle #import, for objective C. */
350 /* Nonzero means this is an assembly file, and allow
351 unknown directives, which could be comments. */
355 /* Current maximum length of directory names in the search path
356 for include files. (Altered as we get more of them.) */
358 static int max_include_len
;
360 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
362 static int for_lint
= 0;
364 /* Nonzero means copy comments into the output file. */
366 static int put_out_comments
= 0;
368 /* Nonzero means don't process the ANSI trigraph sequences. */
370 static int no_trigraphs
= 0;
372 /* Nonzero means print the names of included files rather than
373 the preprocessed output. 1 means just the #include "...",
374 2 means #include <...> as well. */
376 static int print_deps
= 0;
378 /* Nonzero if missing .h files in -M output are assumed to be generated
379 files and not errors. */
381 static int print_deps_missing_files
= 0;
383 /* Nonzero means print names of header files (-H). */
385 static int print_include_names
= 0;
387 /* Nonzero means don't output line number information. */
389 static int no_line_directives
;
391 /* Nonzero means output the text in failing conditionals,
392 inside #failed ... #endfailed. */
394 static int output_conditionals
;
396 /* dump_only means inhibit output of the preprocessed text
397 and instead output the definitions of all user-defined
398 macros in a form suitable for use as input to cccp.
399 dump_names means pass #define and the macro name through to output.
400 dump_definitions means pass the whole definition (plus #define) through
403 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
404 dump_macros
= dump_none
;
406 /* Nonzero means pass all #define and #undef directives which we actually
407 process through to the output stream. This feature is used primarily
408 to allow cc1 to record the #defines and #undefs for the sake of
409 debuggers which understand about preprocessor macros, but it may
410 also be useful with -E to figure out how symbols are defined, and
411 where they are defined. */
412 static int debug_output
= 0;
414 /* Nonzero means pass #include lines through to the output,
415 even if they are ifdeffed out. */
416 static int dump_includes
;
418 /* Nonzero indicates special processing used by the pcp program. The
419 special effects of this mode are:
421 Inhibit all macro expansion, except those inside #if directives.
423 Process #define directives normally, and output their contents
426 Output preconditions to pcp_outfile indicating all the relevant
427 preconditions for use of this file in a later cpp run.
429 static FILE *pcp_outfile
;
431 /* Nonzero means we are inside an IF during a -pcp run. In this mode
432 macro expansion is done, and preconditions are output for all macro
433 uses requiring them. */
434 static int pcp_inside_if
;
436 /* Nonzero means never to include precompiled files.
437 This is 1 since there's no way now to make precompiled files,
438 so it's not worth testing for them. */
439 static int no_precomp
= 1;
441 /* Nonzero means give all the error messages the ANSI standard requires. */
445 /* Nonzero means try to make failure to fit ANSI C an error. */
447 static int pedantic_errors
;
449 /* Nonzero means don't print warning messages. -w. */
451 static int inhibit_warnings
= 0;
453 /* Nonzero means warn if slash-star appears in a slash-star comment,
454 or if newline-backslash appears in a slash-slash comment. */
456 static int warn_comments
;
458 /* Nonzero means warn if a macro argument is (or would be)
459 stringified with -traditional. */
461 static int warn_stringify
;
463 /* Nonzero means warn if there are any trigraphs. */
465 static int warn_trigraphs
;
467 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
471 /* Nonzero means warn if #import is used. */
473 static int warn_import
= 1;
475 /* Nonzero means turn warnings into errors. */
477 static int warnings_are_errors
;
479 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
483 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
487 /* Nonzero causes output not to be done,
488 but directives such as #define that have side effects
491 static int no_output
;
493 /* Nonzero means this file was included with a -imacros or -include
494 command line and should not be recorded as an include file. */
496 static int no_record_file
;
498 /* Nonzero means that we have finished processing the command line options.
499 This flag is used to decide whether or not to issue certain errors
502 static int done_initializing
= 0;
504 /* Line where a newline was first seen in a string constant. */
506 static int multiline_string_line
= 0;
508 /* I/O buffer structure.
509 The `fname' field is nonzero for source files and #include files
510 and for the dummy text used for -D and -U.
511 It is zero for rescanning results of macro expansion
512 and for expanding macro arguments. */
513 #define INPUT_STACK_MAX 400
514 static struct file_buf
{
516 /* Filename specified with #line directive. */
518 /* Include file description. */
519 struct include_file
*inc
;
520 /* Record where in the search path this file was found.
521 For #include_next. */
522 struct file_name_list
*dir
;
527 /* Macro that this level is the expansion of.
528 Included so that we can reenable the macro
529 at the end of this level. */
530 struct hashnode
*macro
;
531 /* Value of if_stack at start of this file.
532 Used to prohibit unmatched #endif (etc) in an include file. */
533 struct if_stack
*if_stack
;
534 /* Object to be freed at end of input at this level. */
536 /* True if this is a system header file; see is_system_include. */
537 char system_header_p
;
538 } instack
[INPUT_STACK_MAX
];
540 static int last_error_tick
; /* Incremented each time we print it. */
541 static int input_file_stack_tick
; /* Incremented when the status changes. */
543 /* Current nesting level of input sources.
544 `instack[indepth]' is the level currently being read. */
545 static int indepth
= -1;
546 #define CHECK_DEPTH(code) \
547 if (indepth >= (INPUT_STACK_MAX - 1)) \
549 error_with_line (line_for_error (instack[indepth].lineno), \
550 "macro or `#include' recursion too deep"); \
554 /* Current depth in #include directives that use <...>. */
555 static int system_include_depth
= 0;
557 typedef struct file_buf FILE_BUF
;
559 /* The output buffer. Its LENGTH field is the amount of room allocated
560 for the buffer, not the number of chars actually present. To get
561 that, subtract outbuf.buf from outbuf.bufp. */
563 #define OUTBUF_SIZE 10 /* initial size of output buffer */
564 static FILE_BUF outbuf
;
566 /* Grow output buffer OBUF points at
567 so it can hold at least NEEDED more chars. */
569 #define check_expand(OBUF, NEEDED) \
570 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
571 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
573 struct file_name_list
575 struct file_name_list
*next
;
576 /* If the following is 1, it is a C-language system include
578 int c_system_include_path
;
579 /* Mapping of file names for this directory. */
580 struct file_name_map
*name_map
;
581 /* Non-zero if name_map is valid. */
583 /* The include directory status. */
585 /* The include prefix: "" denotes the working directory,
586 otherwise fname must end in '/'.
587 The actual size is dynamically allocated. */
591 /* #include "file" looks in source file dir, then stack. */
592 /* #include <file> just looks in the stack. */
593 /* -I directories are added to the end, then the defaults are added. */
595 static struct default_include
{
596 char *fname
; /* The name of the directory. */
597 char *component
; /* The component containing the directory */
598 int cplusplus
; /* Only look here if we're compiling C++. */
599 int cxx_aware
; /* Includes in this directory don't need to
600 be wrapped in extern "C" when compiling
602 } include_defaults_array
[]
603 #ifdef INCLUDE_DEFAULTS
607 /* Pick up GNU C++ specific include files. */
608 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
609 { OLD_GPLUSPLUS_INCLUDE_DIR
, 0, 1, 1 },
611 /* This is the dir for fixincludes. Put it just before
612 the files that we fix. */
613 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
614 /* For cross-compilation, this dir name is generated
615 automatically in Makefile.in. */
616 { CROSS_INCLUDE_DIR
, "GCC", 0, 0 },
617 #ifdef TOOL_INCLUDE_DIR
618 /* This is another place that the target system's headers might be. */
619 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0 },
621 #else /* not CROSS_COMPILE */
622 #ifdef LOCAL_INCLUDE_DIR
623 /* This should be /usr/local/include and should come before
624 the fixincludes-fixed header files. */
625 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
627 #ifdef TOOL_INCLUDE_DIR
628 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
629 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
630 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0 },
632 /* This is the dir for fixincludes. Put it just before
633 the files that we fix. */
634 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
635 /* Some systems have an extra dir of include files. */
636 #ifdef SYSTEM_INCLUDE_DIR
637 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
639 #ifndef STANDARD_INCLUDE_COMPONENT
640 #define STANDARD_INCLUDE_COMPONENT 0
642 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
643 #endif /* not CROSS_COMPILE */
646 #endif /* no INCLUDE_DEFAULTS */
648 /* The code looks at the defaults through this pointer, rather than through
649 the constant structure above. This pointer gets changed if an environment
650 variable specifies other defaults. */
651 static struct default_include
*include_defaults
= include_defaults_array
;
653 static struct file_name_list
*include
= 0; /* First dir to search */
654 /* First dir to search for <file> */
655 /* This is the first element to use for #include <...>.
656 If it is 0, use the entire chain for such includes. */
657 static struct file_name_list
*first_bracket_include
= 0;
658 /* This is the first element in the chain that corresponds to
659 a directory of system header files. */
660 static struct file_name_list
*first_system_include
= 0;
661 static struct file_name_list
*last_include
= 0; /* Last in chain */
663 /* Chain of include directories to put at the end of the other chain. */
664 static struct file_name_list
*after_include
= 0;
665 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
667 /* Chain to put at the start of the system include files. */
668 static struct file_name_list
*before_system
= 0;
669 static struct file_name_list
*last_before_system
= 0; /* Last in chain */
671 /* Directory prefix that should replace `/usr' in the standard
672 include file directories. */
673 static char *include_prefix
;
675 /* Maintain and search list of included files. */
677 struct include_file
{
678 struct include_file
*next
; /* for include_hashtab */
679 struct include_file
*next_ino
; /* for include_ino_hashtab */
681 /* If the following is the empty string, it means #pragma once
682 was seen in this include file, or #import was applied to the file.
683 Otherwise, if it is nonzero, it is a macro name.
684 Don't include the file again if that macro is defined. */
685 U_CHAR
*control_macro
;
686 /* Nonzero if the dependency on this include file has been output. */
691 /* Hash tables of files already included with #include or #import.
692 include_hashtab is by full name; include_ino_hashtab is by inode number. */
694 #define INCLUDE_HASHSIZE 61
695 static struct include_file
*include_hashtab
[INCLUDE_HASHSIZE
];
696 static struct include_file
*include_ino_hashtab
[INCLUDE_HASHSIZE
];
698 /* Global list of strings read in from precompiled files. This list
699 is kept in the order the strings are read in, with new strings being
700 added at the end through stringlist_tailp. We use this list to output
701 the strings at the end of the run.
703 static STRINGDEF
*stringlist
;
704 static STRINGDEF
**stringlist_tailp
= &stringlist
;
707 /* Structure returned by create_definition */
708 typedef struct macrodef MACRODEF
;
711 struct definition
*defn
;
716 enum sharp_token_type
{
717 NO_SHARP_TOKEN
= 0, /* token not present */
719 SHARP_TOKEN
= '#', /* token spelled with # only */
720 WHITE_SHARP_TOKEN
, /* token spelled with # and white space */
722 PERCENT_COLON_TOKEN
= '%', /* token spelled with %: only */
723 WHITE_PERCENT_COLON_TOKEN
/* token spelled with %: and white space */
726 /* Structure allocated for every #define. For a simple replacement
729 nargs = -1, the `pattern' list is null, and the expansion is just
730 the replacement text. Nargs = 0 means a functionlike macro with no args,
732 #define getchar() getc (stdin) .
733 When there are args, the expansion is the replacement text with the
734 args squashed out, and the reflist is a list describing how to
735 build the output from the input: e.g., "3 chars, then the 1st arg,
736 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
737 The chars here come from the expansion. Whatever is left of the
738 expansion after the last arg-occurrence is copied after that arg.
739 Note that the reflist can be arbitrarily long---
740 its length depends on the number of times the arguments appear in
741 the replacement text, not how many args there are. Example:
742 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
744 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
745 where (x, y) means (nchars, argno). */
747 typedef struct definition DEFINITION
;
750 int length
; /* length of expansion string */
751 int predefined
; /* True if the macro was builtin or */
752 /* came from the command line */
754 int line
; /* Line number of definition */
755 char *file
; /* File of definition */
756 char rest_args
; /* Nonzero if last arg. absorbs the rest */
758 struct reflist
*next
;
760 enum sharp_token_type stringify
; /* set if a # operator before arg */
761 enum sharp_token_type raw_before
; /* set if a ## operator before arg */
762 enum sharp_token_type raw_after
; /* set if a ## operator after arg */
764 char rest_args
; /* Nonzero if this arg. absorbs the rest */
765 int nchars
; /* Number of literal chars to copy before
766 this arg occurrence. */
767 int argno
; /* Number of arg to substitute (origin-0) */
770 /* Names of macro args, concatenated in reverse order
771 with comma-space between them.
772 The only use of this is that we warn on redefinition
773 if this differs between the old and new definitions. */
778 /* different kinds of things that can appear in the value field
779 of a hash node. Actually, this may be useless now. */
787 * special extension string that can be added to the last macro argument to
788 * allow it to absorb the "rest" of the arguments when expanded. Ex:
789 * #define wow(a, b...) process (b, a, b)
790 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
791 * { wow (one, two); } -> { process (two, one, two); }
792 * if this "rest_arg" is used with the concat token '##' and if it is not
793 * supplied then the token attached to with ## will not be outputted. Ex:
794 * #define wow (a, b...) process (b ## , a, ## b)
795 * { wow (1, 2); } -> { process (2, 1, 2); }
796 * { wow (one); } -> { process (one); {
798 static char rest_extension
[] = "...";
799 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
801 /* The structure of a node in the hash table. The hash table
802 has entries for all tokens defined by #define directives (type T_MACRO),
803 plus some special tokens like __LINE__ (these each have their own
804 type, and the appropriate code is run when that type of node is seen.
805 It does not contain control words like "#define", which are recognized
806 by a separate piece of code. */
808 /* different flavors of hash nodes --- also used in keyword table */
810 T_DEFINE
= 1, /* the `#define' keyword */
811 T_INCLUDE
, /* the `#include' keyword */
812 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
813 T_IMPORT
, /* the `#import' keyword */
814 T_IFDEF
, /* the `#ifdef' keyword */
815 T_IFNDEF
, /* the `#ifndef' keyword */
816 T_IF
, /* the `#if' keyword */
817 T_ELSE
, /* `#else' */
818 T_PRAGMA
, /* `#pragma' */
819 T_ELIF
, /* `#elif' */
820 T_UNDEF
, /* `#undef' */
821 T_LINE
, /* `#line' */
822 T_ERROR
, /* `#error' */
823 T_WARNING
, /* `#warning' */
824 T_ENDIF
, /* `#endif' */
825 T_SCCS
, /* `#sccs', used on system V. */
826 T_IDENT
, /* `#ident', used on system V. */
827 T_ASSERT
, /* `#assert', taken from system V. */
828 T_UNASSERT
, /* `#unassert', taken from system V. */
829 T_SPECLINE
, /* special symbol `__LINE__' */
830 T_DATE
, /* `__DATE__' */
831 T_FILE
, /* `__FILE__' */
832 T_BASE_FILE
, /* `__BASE_FILE__' */
833 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
834 T_VERSION
, /* `__VERSION__' */
835 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
836 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
837 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
838 T_USER_LABEL_PREFIX_TYPE
, /* `__USER_LABEL_PREFIX__' */
839 T_REGISTER_PREFIX_TYPE
, /* `__REGISTER_PREFIX__' */
840 T_IMMEDIATE_PREFIX_TYPE
, /* `__IMMEDIATE_PREFIX__' */
841 T_TIME
, /* `__TIME__' */
842 T_CONST
, /* Constant value, used by `__STDC__' */
843 T_MACRO
, /* macro defined by `#define' */
844 T_DISABLED
, /* macro temporarily turned off for rescan */
845 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
846 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
847 T_UNUSED
/* Used for something not defined. */
851 struct hashnode
*next
; /* double links for easy deletion */
852 struct hashnode
*prev
;
853 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
854 chain is kept, in case the node is the head
855 of the chain and gets deleted. */
856 enum node_type type
; /* type of special token */
857 int length
; /* length of token, for quick comparison */
858 U_CHAR
*name
; /* the actual name */
859 union hashval value
; /* pointer to expansion, or whatever */
862 typedef struct hashnode HASHNODE
;
864 /* Some definitions for the hash table. The hash function MUST be
865 computed as shown in hashf () below. That is because the rescan
866 loop computes the hash value `on the fly' for most tokens,
867 in order to avoid the overhead of a lot of procedure calls to
868 the hashf () function. Hashf () only exists for the sake of
869 politeness, for use when speed isn't so important. */
871 #define HASHSIZE 1403
872 static HASHNODE
*hashtab
[HASHSIZE
];
873 #define HASHSTEP(old, c) ((old << 2) + c)
874 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
876 /* Symbols to predefine. */
878 #ifdef CPP_PREDEFINES
879 static char *predefs
= CPP_PREDEFINES
;
881 static char *predefs
= "";
884 /* We let tm.h override the types used here, to handle trivial differences
885 such as the choice of unsigned int or long unsigned int for size_t.
886 When machines start needing nontrivial differences in the size type,
887 it would be best to do something here to figure out automatically
888 from other information what type to use. */
890 /* The string value for __SIZE_TYPE__. */
893 #define SIZE_TYPE "long unsigned int"
896 /* The string value for __PTRDIFF_TYPE__. */
899 #define PTRDIFF_TYPE "long int"
902 /* The string value for __WCHAR_TYPE__. */
905 #define WCHAR_TYPE "int"
907 char * wchar_type
= WCHAR_TYPE
;
910 /* The string value for __USER_LABEL_PREFIX__ */
912 #ifndef USER_LABEL_PREFIX
913 #define USER_LABEL_PREFIX ""
916 /* The string value for __REGISTER_PREFIX__ */
918 #ifndef REGISTER_PREFIX
919 #define REGISTER_PREFIX ""
922 /* The string value for __IMMEDIATE_PREFIX__ */
924 #ifndef IMMEDIATE_PREFIX
925 #define IMMEDIATE_PREFIX ""
928 /* In the definition of a #assert name, this structure forms
929 a list of the individual values asserted.
930 Each value is itself a list of "tokens".
931 These are strings that are compared by name. */
933 struct tokenlist_list
{
934 struct tokenlist_list
*next
;
935 struct arglist
*tokens
;
938 struct assertion_hashnode
{
939 struct assertion_hashnode
*next
; /* double links for easy deletion */
940 struct assertion_hashnode
*prev
;
941 /* also, a back pointer to this node's hash
942 chain is kept, in case the node is the head
943 of the chain and gets deleted. */
944 struct assertion_hashnode
**bucket_hdr
;
945 int length
; /* length of token, for quick comparison */
946 U_CHAR
*name
; /* the actual name */
947 /* List of token-sequences. */
948 struct tokenlist_list
*value
;
951 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
953 /* Some definitions for the hash table. The hash function MUST be
954 computed as shown in hashf below. That is because the rescan
955 loop computes the hash value `on the fly' for most tokens,
956 in order to avoid the overhead of a lot of procedure calls to
957 the hashf function. hashf only exists for the sake of
958 politeness, for use when speed isn't so important. */
960 #define ASSERTION_HASHSIZE 37
961 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
963 /* Nonzero means inhibit macroexpansion of what seem to be
964 assertion tests, in rescan. For #if. */
965 static int assertions_flag
;
967 /* `struct directive' defines one #-directive, including how to handle it. */
969 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
972 int length
; /* Length of name */
973 int (*func
) DO_PROTO
; /* Function to handle directive */
974 char *name
; /* Name of directive */
975 enum node_type type
; /* Code which describes which directive. */
978 #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT)
980 /* These functions are declared to return int instead of void since they
981 are going to be placed in the table and some old compilers have trouble with
982 pointers to functions returning void. */
984 static int do_assert DO_PROTO
;
985 static int do_define DO_PROTO
;
986 static int do_elif DO_PROTO
;
987 static int do_else DO_PROTO
;
988 static int do_endif DO_PROTO
;
989 static int do_error DO_PROTO
;
990 static int do_ident DO_PROTO
;
991 static int do_if DO_PROTO
;
992 static int do_include DO_PROTO
;
993 static int do_line DO_PROTO
;
994 static int do_pragma DO_PROTO
;
995 #ifdef SCCS_DIRECTIVE
996 static int do_sccs DO_PROTO
;
998 static int do_unassert DO_PROTO
;
999 static int do_undef DO_PROTO
;
1000 static int do_warning DO_PROTO
;
1001 static int do_xifdef DO_PROTO
;
1003 /* Here is the actual list of #-directives, most-often-used first. */
1005 static struct directive directive_table
[] = {
1006 { 6, do_define
, "define", T_DEFINE
},
1007 { 2, do_if
, "if", T_IF
},
1008 { 5, do_xifdef
, "ifdef", T_IFDEF
},
1009 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
1010 { 5, do_endif
, "endif", T_ENDIF
},
1011 { 4, do_else
, "else", T_ELSE
},
1012 { 4, do_elif
, "elif", T_ELIF
},
1013 { 4, do_line
, "line", T_LINE
},
1014 { 7, do_include
, "include", T_INCLUDE
},
1015 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
1016 { 6, do_include
, "import", T_IMPORT
},
1017 { 5, do_undef
, "undef", T_UNDEF
},
1018 { 5, do_error
, "error", T_ERROR
},
1019 { 7, do_warning
, "warning", T_WARNING
},
1020 #ifdef SCCS_DIRECTIVE
1021 { 4, do_sccs
, "sccs", T_SCCS
},
1023 { 6, do_pragma
, "pragma", T_PRAGMA
},
1024 { 5, do_ident
, "ident", T_IDENT
},
1025 { 6, do_assert
, "assert", T_ASSERT
},
1026 { 8, do_unassert
, "unassert", T_UNASSERT
},
1027 { -1, 0, "", T_UNUSED
},
1030 /* When a directive handler is called,
1031 this points to the # (or the : of the %:) that started the directive. */
1032 U_CHAR
*directive_start
;
1034 /* table to tell if char can be part of a C identifier. */
1035 U_CHAR is_idchar
[256];
1036 /* table to tell if char can be first char of a c identifier. */
1037 U_CHAR is_idstart
[256];
1038 /* table to tell if c is horizontal space. */
1039 static U_CHAR is_hor_space
[256];
1040 /* table to tell if c is horizontal or vertical space. */
1041 U_CHAR is_space
[256];
1042 /* names of some characters */
1043 static char *char_name
[256];
1045 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
1046 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
1048 static int errors
= 0; /* Error counter for exit code */
1050 /* Name of output file, for error messages. */
1051 static char *out_fname
;
1054 /* Stack of conditionals currently in progress
1055 (including both successful and failing conditionals). */
1058 struct if_stack
*next
; /* for chaining to the next stack frame */
1059 char *fname
; /* copied from input when frame is made */
1060 int lineno
; /* similarly */
1061 int if_succeeded
; /* true if a leg of this if-group
1062 has been passed through rescan */
1063 U_CHAR
*control_macro
; /* For #ifndef at start of file,
1064 this is the macro name tested. */
1065 enum node_type type
; /* type of last directive seen in this group */
1067 typedef struct if_stack IF_STACK_FRAME
;
1068 static IF_STACK_FRAME
*if_stack
= NULL
;
1070 /* Buffer of -M output. */
1071 static char *deps_buffer
;
1073 /* Number of bytes allocated in above. */
1074 static int deps_allocated_size
;
1076 /* Number of bytes used. */
1077 static int deps_size
;
1079 /* Number of bytes since the last newline. */
1080 static int deps_column
;
1082 /* Nonzero means -I- has been seen,
1083 so don't look for #include "foo" the source-file directory. */
1084 static int ignore_srcdir
;
1086 static int safe_read
PROTO((int, char *, int));
1087 static void safe_write
PROTO((int, char *, int));
1089 int main
PROTO((int, char **));
1091 static void path_include
PROTO((char *));
1093 static U_CHAR
*index0
PROTO((U_CHAR
*, int, size_t));
1095 static void trigraph_pcp
PROTO((FILE_BUF
*));
1097 static void newline_fix
PROTO((U_CHAR
*));
1098 static void name_newline_fix
PROTO((U_CHAR
*));
1100 static char *get_lintcmd
PROTO((U_CHAR
*, U_CHAR
*, U_CHAR
**, int *, int *));
1102 static void rescan
PROTO((FILE_BUF
*, int));
1104 static FILE_BUF expand_to_temp_buffer
PROTO((U_CHAR
*, U_CHAR
*, int, int));
1106 static int handle_directive
PROTO((FILE_BUF
*, FILE_BUF
*));
1108 static struct tm
*timestamp
PROTO((void));
1109 static void special_symbol
PROTO((HASHNODE
*, FILE_BUF
*));
1111 static int is_system_include
PROTO((char *));
1112 static char *base_name
PROTO((char *));
1113 static int absolute_filename
PROTO((char *));
1114 static size_t simplify_filename
PROTO((char *));
1116 static char *read_filename_string
PROTO((int, FILE *));
1117 static struct file_name_map
*read_name_map
PROTO((char *));
1118 static int open_include_file
PROTO((char *, struct file_name_list
*, U_CHAR
*, struct include_file
**));
1119 static char *remap_include_file
PROTO((char *, struct file_name_list
*));
1120 static int lookup_ino_include
PROTO((struct include_file
*));
1122 static void finclude
PROTO((int, struct include_file
*, FILE_BUF
*, int, struct file_name_list
*));
1123 static void record_control_macro
PROTO((struct include_file
*, U_CHAR
*));
1125 static char *check_precompiled
PROTO((int, struct stat
*, char *, char **));
1126 static int check_preconditions
PROTO((char *));
1127 static void pcfinclude
PROTO((U_CHAR
*, U_CHAR
*, U_CHAR
*, FILE_BUF
*));
1128 static void pcstring_used
PROTO((HASHNODE
*));
1129 static void write_output
PROTO((void));
1130 static void pass_thru_directive
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
1132 static MACRODEF create_definition
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
1134 static int check_macro_name
PROTO((U_CHAR
*, char *));
1135 static int compare_defs
PROTO((DEFINITION
*, DEFINITION
*));
1136 static int comp_def_part
PROTO((int, U_CHAR
*, int, U_CHAR
*, int, int));
1138 static DEFINITION
*collect_expansion
PROTO((U_CHAR
*, U_CHAR
*, int, struct arglist
*));
1140 int check_assertion
PROTO((U_CHAR
*, int, int, struct arglist
*));
1141 static int compare_token_lists
PROTO((struct arglist
*, struct arglist
*));
1143 static struct arglist
*read_token_list
PROTO((U_CHAR
**, U_CHAR
*, int *));
1144 static void free_token_list
PROTO((struct arglist
*));
1146 static ASSERTION_HASHNODE
*assertion_install
PROTO((U_CHAR
*, int, int));
1147 static ASSERTION_HASHNODE
*assertion_lookup
PROTO((U_CHAR
*, int, int));
1148 static void delete_assertion
PROTO((ASSERTION_HASHNODE
*));
1150 static void do_once
PROTO((void));
1152 static HOST_WIDE_INT eval_if_expression
PROTO((U_CHAR
*, int));
1153 static void conditional_skip
PROTO((FILE_BUF
*, int, enum node_type
, U_CHAR
*, FILE_BUF
*));
1154 static void skip_if_group
PROTO((FILE_BUF
*, int, FILE_BUF
*));
1155 static void validate_else
PROTO((U_CHAR
*, U_CHAR
*));
1157 static U_CHAR
*skip_to_end_of_comment
PROTO((FILE_BUF
*, int *, int));
1158 static U_CHAR
*skip_quoted_string
PROTO((U_CHAR
*, U_CHAR
*, int, int *, int *, int *));
1159 static char *quote_string
PROTO((char *, char *));
1160 static U_CHAR
*skip_paren_group
PROTO((FILE_BUF
*));
1162 /* Last arg to output_line_directive. */
1163 enum file_change_code
{same_file
, enter_file
, leave_file
};
1164 static void output_line_directive
PROTO((FILE_BUF
*, FILE_BUF
*, int, enum file_change_code
));
1166 static void macroexpand
PROTO((HASHNODE
*, FILE_BUF
*));
1169 static char *macarg
PROTO((struct argdata
*, int));
1171 static U_CHAR
*macarg1
PROTO((U_CHAR
*, U_CHAR
*, int *, int *, int *, int));
1173 static int discard_comments
PROTO((U_CHAR
*, int, int));
1175 static int change_newlines
PROTO((U_CHAR
*, int));
1177 char *my_strerror
PROTO((int));
1178 void error
PRINTF_PROTO_1((char *, ...));
1179 static void verror
PROTO((char *, va_list));
1180 static void error_from_errno
PROTO((char *));
1181 void warning
PRINTF_PROTO_1((char *, ...));
1182 static void vwarning
PROTO((char *, va_list));
1183 static void error_with_line
PRINTF_PROTO_2((int, char *, ...));
1184 static void verror_with_line
PROTO((int, char *, va_list));
1185 static void vwarning_with_line
PROTO((int, char *, va_list));
1186 static void warning_with_line
PRINTF_PROTO_2((int, char *, ...));
1187 void pedwarn
PRINTF_PROTO_1((char *, ...));
1188 void pedwarn_with_line
PRINTF_PROTO_2((int, char *, ...));
1189 static void pedwarn_with_file_and_line
PRINTF_PROTO_3((char *, int, char *, ...));
1191 static void print_containing_files
PROTO((void));
1193 static int line_for_error
PROTO((int));
1194 static int grow_outbuf
PROTO((FILE_BUF
*, int));
1196 static HASHNODE
*install
PROTO((U_CHAR
*, int, enum node_type
, char *, int));
1197 HASHNODE
*lookup
PROTO((U_CHAR
*, int, int));
1198 static void delete_macro
PROTO((HASHNODE
*));
1199 static int hashf
PROTO((U_CHAR
*, int, int));
1201 static void dump_single_macro
PROTO((HASHNODE
*, FILE *));
1202 static void dump_all_macros
PROTO((void));
1203 static void dump_defn_1
PROTO((U_CHAR
*, int, int, FILE *));
1204 static void dump_arg_n
PROTO((DEFINITION
*, int, FILE *));
1206 static void initialize_char_syntax
PROTO((void));
1207 static void initialize_builtins
PROTO((FILE_BUF
*, FILE_BUF
*));
1209 static void make_definition
PROTO((char *, FILE_BUF
*));
1210 static void make_undef
PROTO((char *, FILE_BUF
*));
1212 static void make_assertion
PROTO((char *, char *));
1214 static struct file_name_list
*new_include_prefix
PROTO((struct file_name_list
*, char *, char *, char *));
1215 static void append_include_chain
PROTO((struct file_name_list
*, struct file_name_list
*));
1217 static void deps_output
PROTO((char *, int));
1219 static void fatal
PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn
));
1220 void fancy_abort
PROTO((void)) __attribute__ ((noreturn
));
1221 static void perror_with_name
PROTO((char *));
1222 static void pfatal_with_name
PROTO((char *)) __attribute__ ((noreturn
));
1223 static void pipe_closed
PROTO((int)) __attribute__ ((noreturn
));
1225 static void memory_full
PROTO((void)) __attribute__ ((noreturn
));
1226 GENERIC_PTR xmalloc
PROTO((size_t));
1227 static GENERIC_PTR xrealloc
PROTO((GENERIC_PTR
, size_t));
1228 static GENERIC_PTR xcalloc
PROTO((size_t, size_t));
1229 static char *savestring
PROTO((char *));
1231 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1232 retrying if necessary. If MAX_READ_LEN is defined, read at most
1233 that bytes at a time. Return a negative value if an error occurs,
1234 otherwise return the actual number of bytes read,
1235 which must be LEN unless end-of-file was reached. */
1238 safe_read (desc
, ptr
, len
)
1243 int left
, rcount
, nchars
;
1249 if (rcount
> MAX_READ_LEN
)
1250 rcount
= MAX_READ_LEN
;
1252 nchars
= read (desc
, ptr
, rcount
);
1269 /* Write LEN bytes at PTR to descriptor DESC,
1270 retrying if necessary, and treating any real error as fatal.
1271 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1274 safe_write (desc
, ptr
, len
)
1279 int wcount
, written
;
1283 #ifdef MAX_WRITE_LEN
1284 if (wcount
> MAX_WRITE_LEN
)
1285 wcount
= MAX_WRITE_LEN
;
1287 written
= write (desc
, ptr
, wcount
);
1294 pfatal_with_name (out_fname
);
1311 char **pend_files
= (char **) xmalloc (argc
* sizeof (char *));
1312 char **pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
1313 char **pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
1314 char **pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
1315 char **pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
1317 /* Record the option used with each element of pend_assertions.
1318 This is preparation for supporting more than one option for making
1320 char **pend_assertion_options
= (char **) xmalloc (argc
* sizeof (char *));
1321 int inhibit_predefs
= 0;
1322 int no_standard_includes
= 0;
1323 int no_standard_cplusplus_includes
= 0;
1324 int missing_newline
= 0;
1326 /* Non-0 means don't output the preprocessed program. */
1327 int inhibit_output
= 0;
1328 /* Non-0 means -v, so print the full set of include dirs. */
1331 /* File name which deps are being written to.
1332 This is 0 if deps are being written to stdout. */
1333 char *deps_file
= 0;
1334 /* Fopen file mode to open deps_file with. */
1335 char *deps_mode
= "a";
1336 /* Stream on which to print the dependency information. */
1337 FILE *deps_stream
= 0;
1338 /* Target-name to write with the dependency information. */
1339 char *deps_target
= 0;
1342 /* Get rid of any avoidable limit on stack size. */
1346 /* Set the stack limit huge so that alloca (particularly stringtab
1347 in dbxread.c) does not fail. */
1348 getrlimit (RLIMIT_STACK
, &rlim
);
1349 rlim
.rlim_cur
= rlim
.rlim_max
;
1350 setrlimit (RLIMIT_STACK
, &rlim
);
1352 #endif /* RLIMIT_STACK defined */
1355 signal (SIGPIPE
, pipe_closed
);
1358 progname
= base_name (argv
[0]);
1362 /* Remove extension from PROGNAME. */
1364 char *s
= progname
= savestring (progname
);
1366 if ((p
= rindex (s
, ';')) != 0) *p
= '\0'; /* strip version number */
1367 if ((p
= rindex (s
, '.')) != 0 /* strip type iff ".exe" */
1368 && (p
[1] == 'e' || p
[1] == 'E')
1369 && (p
[2] == 'x' || p
[2] == 'X')
1370 && (p
[3] == 'e' || p
[3] == 'E')
1379 /* Initialize is_idchar. */
1380 initialize_char_syntax ();
1382 no_line_directives
= 0;
1384 dump_macros
= dump_none
;
1387 cplusplus_comments
= 1;
1389 bzero ((char *) pend_files
, argc
* sizeof (char *));
1390 bzero ((char *) pend_defs
, argc
* sizeof (char *));
1391 bzero ((char *) pend_undefs
, argc
* sizeof (char *));
1392 bzero ((char *) pend_assertions
, argc
* sizeof (char *));
1393 bzero ((char *) pend_includes
, argc
* sizeof (char *));
1395 /* Process switches and find input file name. */
1397 for (i
= 1; i
< argc
; i
++) {
1398 if (argv
[i
][0] != '-') {
1399 if (out_fname
!= NULL
)
1400 fatal ("Usage: %s [switches] input output", argv
[0]);
1401 else if (in_fname
!= NULL
)
1402 out_fname
= argv
[i
];
1406 switch (argv
[i
][1]) {
1409 if (!strcmp (argv
[i
], "-include")) {
1411 fatal ("Filename missing after `-include' option");
1413 simplify_filename (pend_includes
[i
] = argv
[++i
]);
1415 if (!strcmp (argv
[i
], "-imacros")) {
1417 fatal ("Filename missing after `-imacros' option");
1419 simplify_filename (pend_files
[i
] = argv
[++i
]);
1421 if (!strcmp (argv
[i
], "-iprefix")) {
1423 fatal ("Filename missing after `-iprefix' option");
1425 include_prefix
= argv
[++i
];
1427 if (!strcmp (argv
[i
], "-ifoutput")) {
1428 output_conditionals
= 1;
1430 if (!strcmp (argv
[i
], "-isystem")) {
1431 struct file_name_list
*dirtmp
;
1433 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1436 dirtmp
->c_system_include_path
= 1;
1438 if (before_system
== 0)
1439 before_system
= dirtmp
;
1441 last_before_system
->next
= dirtmp
;
1442 last_before_system
= dirtmp
; /* Tail follows the last one */
1444 /* Add directory to end of path for includes,
1445 with the default prefix at the front of its name. */
1446 if (!strcmp (argv
[i
], "-iwithprefix")) {
1447 struct file_name_list
*dirtmp
;
1450 if (include_prefix
!= 0)
1451 prefix
= include_prefix
;
1453 prefix
= savestring (GCC_INCLUDE_DIR
);
1454 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1455 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1456 prefix
[strlen (prefix
) - 7] = 0;
1459 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1460 prefix
, argv
[++i
])))
1463 if (after_include
== 0)
1464 after_include
= dirtmp
;
1466 last_after_include
->next
= dirtmp
;
1467 last_after_include
= dirtmp
; /* Tail follows the last one */
1469 /* Add directory to main path for includes,
1470 with the default prefix at the front of its name. */
1471 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
1472 struct file_name_list
*dirtmp
;
1475 if (include_prefix
!= 0)
1476 prefix
= include_prefix
;
1478 prefix
= savestring (GCC_INCLUDE_DIR
);
1479 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1480 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1481 prefix
[strlen (prefix
) - 7] = 0;
1484 dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
, prefix
, argv
[++i
]);
1485 append_include_chain (dirtmp
, dirtmp
);
1487 /* Add directory to end of path for includes. */
1488 if (!strcmp (argv
[i
], "-idirafter")) {
1489 struct file_name_list
*dirtmp
;
1491 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1495 if (after_include
== 0)
1496 after_include
= dirtmp
;
1498 last_after_include
->next
= dirtmp
;
1499 last_after_include
= dirtmp
; /* Tail follows the last one */
1504 if (out_fname
!= NULL
)
1505 fatal ("Output filename specified twice");
1507 fatal ("Filename missing after -o option");
1508 out_fname
= argv
[++i
];
1509 if (!strcmp (out_fname
, "-"))
1514 if (!strcmp (argv
[i
], "-pedantic"))
1516 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1518 pedantic_errors
= 1;
1519 } else if (!strcmp (argv
[i
], "-pcp")) {
1522 fatal ("Filename missing after -pcp option");
1523 pcp_fname
= argv
[++i
];
1525 = ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1526 ? fopen (pcp_fname
, "w")
1528 if (pcp_outfile
== 0)
1529 pfatal_with_name (pcp_fname
);
1535 if (!strcmp (argv
[i
], "-traditional")) {
1537 cplusplus_comments
= 0;
1538 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1544 if (! strcmp (argv
[i
], "-lang-c"))
1545 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, objc
= 0;
1546 if (! strcmp (argv
[i
], "-lang-c89"))
1547 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, objc
= 0;
1548 if (! strcmp (argv
[i
], "-lang-c++"))
1549 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, objc
= 0;
1550 if (! strcmp (argv
[i
], "-lang-objc"))
1551 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, objc
= 1;
1552 if (! strcmp (argv
[i
], "-lang-objc++"))
1553 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, objc
= 1;
1554 if (! strcmp (argv
[i
], "-lang-asm"))
1556 if (! strcmp (argv
[i
], "-lint"))
1561 cplusplus
= 1, cplusplus_comments
= 1;
1565 inhibit_warnings
= 1;
1569 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1571 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1573 else if (!strcmp (argv
[i
], "-Wcomment"))
1575 else if (!strcmp (argv
[i
], "-Wno-comment"))
1577 else if (!strcmp (argv
[i
], "-Wcomments"))
1579 else if (!strcmp (argv
[i
], "-Wno-comments"))
1581 else if (!strcmp (argv
[i
], "-Wtraditional"))
1583 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1585 else if (!strcmp (argv
[i
], "-Wundef"))
1587 else if (!strcmp (argv
[i
], "-Wno-undef"))
1589 else if (!strcmp (argv
[i
], "-Wimport"))
1591 else if (!strcmp (argv
[i
], "-Wno-import"))
1593 else if (!strcmp (argv
[i
], "-Werror"))
1594 warnings_are_errors
= 1;
1595 else if (!strcmp (argv
[i
], "-Wno-error"))
1596 warnings_are_errors
= 0;
1597 else if (!strcmp (argv
[i
], "-Wall"))
1605 /* The style of the choices here is a bit mixed.
1606 The chosen scheme is a hybrid of keeping all options in one string
1607 and specifying each option in a separate argument:
1608 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1609 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1610 -M[M][G][D file]. This is awkward to handle in specs, and is not
1612 /* ??? -MG must be specified in addition to one of -M or -MM.
1613 This can be relaxed in the future without breaking anything.
1614 The converse isn't true. */
1616 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1617 if (!strcmp (argv
[i
], "-MG"))
1619 print_deps_missing_files
= 1;
1622 if (!strcmp (argv
[i
], "-M"))
1624 else if (!strcmp (argv
[i
], "-MM"))
1626 else if (!strcmp (argv
[i
], "-MD"))
1628 else if (!strcmp (argv
[i
], "-MMD"))
1630 /* For -MD and -MMD options, write deps on file named by next arg. */
1631 if (!strcmp (argv
[i
], "-MD")
1632 || !strcmp (argv
[i
], "-MMD")) {
1634 fatal ("Filename missing after %s option", argv
[i
]);
1636 deps_file
= argv
[i
];
1639 /* For -M and -MM, write deps on standard output
1640 and suppress the usual output. */
1641 deps_stream
= stdout
;
1648 char *p
= argv
[i
] + 2;
1650 while ((c
= *p
++)) {
1651 /* Arg to -d specifies what parts of macros to dump */
1654 dump_macros
= dump_only
;
1658 dump_macros
= dump_names
;
1661 dump_macros
= dump_definitions
;
1672 if (argv
[i
][2] == '3')
1677 fprintf (stderr
, "GNU CPP version %s", version_string
);
1678 #ifdef TARGET_VERSION
1681 fprintf (stderr
, "\n");
1686 print_include_names
= 1;
1690 if (argv
[i
][2] != 0)
1691 pend_defs
[i
] = argv
[i
] + 2;
1692 else if (i
+ 1 == argc
)
1693 fatal ("Macro name missing after -D option");
1695 i
++, pend_defs
[i
] = argv
[i
];
1702 if (argv
[i
][2] != 0)
1704 else if (i
+ 1 == argc
)
1705 fatal ("Assertion missing after -A option");
1709 if (!strcmp (p
, "-")) {
1710 /* -A- eliminates all predefined macros and assertions.
1711 Let's include also any that were specified earlier
1712 on the command line. That way we can get rid of any
1713 that were passed automatically in from GCC. */
1715 inhibit_predefs
= 1;
1716 for (j
= 0; j
< i
; j
++)
1717 pend_defs
[j
] = pend_assertions
[j
] = 0;
1719 pend_assertions
[i
] = p
;
1720 pend_assertion_options
[i
] = "-A";
1725 case 'U': /* JF #undef something */
1726 if (argv
[i
][2] != 0)
1727 pend_undefs
[i
] = argv
[i
] + 2;
1728 else if (i
+ 1 == argc
)
1729 fatal ("Macro name missing after -U option");
1731 pend_undefs
[i
] = argv
[i
+1], i
++;
1735 put_out_comments
= 1;
1738 case 'E': /* -E comes from cc -E; ignore it. */
1742 no_line_directives
= 1;
1745 case '$': /* Don't include $ in identifiers. */
1746 is_idchar
['$'] = is_idstart
['$'] = 0;
1749 case 'I': /* Add directory to path for includes. */
1751 struct file_name_list
*dirtmp
;
1753 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-")) {
1755 /* Don't use any preceding -I directories for #include <...>. */
1756 first_bracket_include
= 0;
1759 dirtmp
= new_include_prefix (last_include
, NULL_PTR
, "",
1760 argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1761 append_include_chain (dirtmp
, dirtmp
);
1767 if (!strcmp (argv
[i
], "-nostdinc"))
1768 /* -nostdinc causes no default include directories.
1769 You must specify all include-file directories with -I. */
1770 no_standard_includes
= 1;
1771 else if (!strcmp (argv
[i
], "-nostdinc++"))
1772 /* -nostdinc++ causes no default C++-specific include directories. */
1773 no_standard_cplusplus_includes
= 1;
1774 else if (!strcmp (argv
[i
], "-noprecomp"))
1779 /* Sun compiler passes undocumented switch "-undef".
1780 Let's assume it means to inhibit the predefined symbols. */
1781 inhibit_predefs
= 1;
1784 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1785 if (in_fname
== NULL
) {
1788 } else if (out_fname
== NULL
) {
1791 } /* else fall through into error */
1794 fatal ("Invalid option `%s'", argv
[i
]);
1799 /* Add dirs from CPATH after dirs from -I. */
1800 /* There seems to be confusion about what CPATH should do,
1801 so for the moment it is not documented. */
1802 /* Some people say that CPATH should replace the standard include dirs,
1803 but that seems pointless: it comes before them, so it overrides them
1805 cp
= getenv ("CPATH");
1806 if (cp
&& ! no_standard_includes
)
1809 /* Initialize output buffer */
1811 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1812 outbuf
.bufp
= outbuf
.buf
;
1813 outbuf
.length
= OUTBUF_SIZE
;
1815 /* Do partial setup of input buffer for the sake of generating
1816 early #line directives (when -g is in effect). */
1818 fp
= &instack
[++indepth
];
1819 if (in_fname
== NULL
)
1821 fp
->nominal_fname
= fp
->fname
= in_fname
;
1824 /* In C++, wchar_t is a distinct basic type, and we can expect
1825 __wchar_t to be defined by cc1plus. */
1827 wchar_type
= "__wchar_t";
1829 /* Install __LINE__, etc. Must follow initialize_char_syntax
1830 and option processing. */
1831 initialize_builtins (fp
, &outbuf
);
1833 /* Do standard #defines and assertions
1834 that identify system and machine type. */
1836 if (!inhibit_predefs
) {
1837 char *p
= (char *) alloca (strlen (predefs
) + 1);
1838 strcpy (p
, predefs
);
1841 while (*p
== ' ' || *p
== '\t')
1843 /* Handle -D options. */
1844 if (p
[0] == '-' && p
[1] == 'D') {
1846 while (*p
&& *p
!= ' ' && *p
!= '\t')
1851 output_line_directive (fp
, &outbuf
, 0, same_file
);
1852 make_definition (q
, &outbuf
);
1853 while (*p
== ' ' || *p
== '\t')
1855 } else if (p
[0] == '-' && p
[1] == 'A') {
1856 /* Handle -A options (assertions). */
1865 past_name
= assertion
;
1866 /* Locate end of name. */
1867 while (*past_name
&& *past_name
!= ' '
1868 && *past_name
!= '\t' && *past_name
!= '(')
1870 /* Locate `(' at start of value. */
1872 while (*value
&& (*value
== ' ' || *value
== '\t'))
1874 if (*value
++ != '(')
1876 while (*value
&& (*value
== ' ' || *value
== '\t'))
1879 /* Locate end of value. */
1880 while (*past_value
&& *past_value
!= ' '
1881 && *past_value
!= '\t' && *past_value
!= ')')
1883 termination
= past_value
;
1884 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
1886 if (*termination
++ != ')')
1888 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
1890 /* Temporarily null-terminate the value. */
1891 save_char
= *termination
;
1892 *termination
= '\0';
1893 /* Install the assertion. */
1894 make_assertion ("-A", assertion
);
1895 *termination
= (char) save_char
;
1897 while (*p
== ' ' || *p
== '\t')
1905 /* Now handle the command line options. */
1907 /* Do -U's, -D's and -A's in the order they were seen. */
1908 for (i
= 1; i
< argc
; i
++) {
1909 if (pend_undefs
[i
]) {
1911 output_line_directive (fp
, &outbuf
, 0, same_file
);
1912 make_undef (pend_undefs
[i
], &outbuf
);
1916 output_line_directive (fp
, &outbuf
, 0, same_file
);
1917 make_definition (pend_defs
[i
], &outbuf
);
1919 if (pend_assertions
[i
])
1920 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1923 done_initializing
= 1;
1925 { /* Read the appropriate environment variable and if it exists
1926 replace include_defaults with the listed path. */
1928 switch ((objc
<< 1) + cplusplus
)
1931 epath
= getenv ("C_INCLUDE_PATH");
1934 epath
= getenv ("CPLUS_INCLUDE_PATH");
1937 epath
= getenv ("OBJC_INCLUDE_PATH");
1940 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
1943 /* If the environment var for this language is set,
1944 add to the default list of include directories. */
1947 char *startp
, *endp
;
1949 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1950 if (*startp
== PATH_SEPARATOR
)
1953 = (struct default_include
*) xmalloc ((num_dirs
1954 * sizeof (struct default_include
))
1955 + sizeof (include_defaults_array
));
1956 startp
= endp
= epath
;
1960 if (c
== PATH_SEPARATOR
|| !c
) {
1962 include_defaults
[num_dirs
].fname
1963 = startp
== endp
? "." : savestring (startp
);
1965 include_defaults
[num_dirs
].component
= 0;
1966 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1967 include_defaults
[num_dirs
].cxx_aware
= 1;
1974 /* Put the usual defaults back in at the end. */
1975 bcopy ((char *) include_defaults_array
,
1976 (char *) &include_defaults
[num_dirs
],
1977 sizeof (include_defaults_array
));
1981 append_include_chain (before_system
, last_before_system
);
1982 first_system_include
= before_system
;
1984 /* Unless -fnostdinc,
1985 tack on the standard include file dirs to the specified list */
1986 if (!no_standard_includes
) {
1987 struct default_include
*p
= include_defaults
;
1988 char *specd_prefix
= include_prefix
;
1989 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
1990 int default_len
= 0;
1991 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1992 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1993 default_len
= strlen (default_prefix
) - 7;
1994 default_prefix
[default_len
] = 0;
1996 /* Search "translated" versions of GNU directories.
1997 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1998 if (specd_prefix
!= 0 && default_len
!= 0)
1999 for (p
= include_defaults
; p
->fname
; p
++) {
2000 /* Some standard dirs are only for C++. */
2001 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
2002 /* Does this dir start with the prefix? */
2003 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
2004 /* Yes; change prefix and add to search list. */
2005 struct file_name_list
*new
2006 = new_include_prefix (NULL_PTR
, NULL_PTR
, specd_prefix
,
2007 p
->fname
+ default_len
);
2009 new->c_system_include_path
= !p
->cxx_aware
;
2010 append_include_chain (new, new);
2011 if (first_system_include
== 0)
2012 first_system_include
= new;
2017 /* Search ordinary names for GNU include directories. */
2018 for (p
= include_defaults
; p
->fname
; p
++) {
2019 /* Some standard dirs are only for C++. */
2020 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
2021 struct file_name_list
*new
2022 = new_include_prefix (NULL_PTR
, p
->component
, "", p
->fname
);
2024 new->c_system_include_path
= !p
->cxx_aware
;
2025 append_include_chain (new, new);
2026 if (first_system_include
== 0)
2027 first_system_include
= new;
2033 /* Tack the after_include chain at the end of the include chain. */
2034 append_include_chain (after_include
, last_after_include
);
2035 if (first_system_include
== 0)
2036 first_system_include
= after_include
;
2038 /* With -v, print the list of dirs to search. */
2040 struct file_name_list
*p
;
2041 fprintf (stderr
, "#include \"...\" search starts here:\n");
2042 for (p
= include
; p
; p
= p
->next
) {
2043 if (p
== first_bracket_include
)
2044 fprintf (stderr
, "#include <...> search starts here:\n");
2046 fprintf (stderr
, " .\n");
2047 else if (!strcmp (p
->fname
, "/") || !strcmp (p
->fname
, "//"))
2048 fprintf (stderr
, " %s\n", p
->fname
);
2050 /* Omit trailing '/'. */
2051 fprintf (stderr
, " %.*s\n", (int) strlen (p
->fname
) - 1, p
->fname
);
2053 fprintf (stderr
, "End of search list.\n");
2056 /* -MG doesn't select the form of output and must be specified with one of
2057 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
2058 inhibit compilation. */
2059 if (print_deps_missing_files
&& (print_deps
== 0 || !inhibit_output
))
2060 fatal ("-MG must be specified with one of -M or -MM");
2062 /* Either of two environment variables can specify output of deps.
2063 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
2064 where OUTPUT_FILE is the file to write deps info to
2065 and DEPS_TARGET is the target to mention in the deps. */
2068 && (getenv ("SUNPRO_DEPENDENCIES") != 0
2069 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
2070 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
2075 spec
= getenv ("SUNPRO_DEPENDENCIES");
2082 /* Find the space before the DEPS_TARGET, if there is one. */
2083 /* This should use index. (mrs) */
2084 while (*s
!= 0 && *s
!= ' ') s
++;
2086 deps_target
= s
+ 1;
2087 output_file
= xmalloc (s
- spec
+ 1);
2088 bcopy (spec
, output_file
, s
- spec
);
2089 output_file
[s
- spec
] = 0;
2096 deps_file
= output_file
;
2100 /* For -M, print the expected object file name
2101 as the target of this Make-rule. */
2103 deps_allocated_size
= 200;
2104 deps_buffer
= xmalloc (deps_allocated_size
);
2110 deps_output (deps_target
, ':');
2111 } else if (*in_fname
== 0) {
2112 deps_output ("-", ':');
2117 q
= base_name (in_fname
);
2119 /* Copy remainder to mungable area. */
2120 p
= (char *) alloca (strlen(q
) + 8);
2123 /* Output P, but remove known suffixes. */
2127 && p
[len
- 2] == '.'
2128 && index("cCsSm", p
[len
- 1]))
2131 && p
[len
- 3] == '.'
2132 && p
[len
- 2] == 'c'
2133 && p
[len
- 1] == 'c')
2136 && p
[len
- 4] == '.'
2137 && p
[len
- 3] == 'c'
2138 && p
[len
- 2] == 'x'
2139 && p
[len
- 1] == 'x')
2142 && p
[len
- 4] == '.'
2143 && p
[len
- 3] == 'c'
2144 && p
[len
- 2] == 'p'
2145 && p
[len
- 1] == 'p')
2148 /* Supply our own suffix. */
2149 strcpy (q
, OBJECT_SUFFIX
);
2151 deps_output (p
, ':');
2152 deps_output (in_fname
, ' ');
2156 /* Scan the -imacros files before the main input.
2157 Much like #including them, but with no_output set
2158 so that only their macro definitions matter. */
2160 no_output
++; no_record_file
++;
2161 for (i
= 1; i
< argc
; i
++)
2162 if (pend_files
[i
]) {
2163 struct include_file
*inc
;
2164 int fd
= open_include_file (pend_files
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2166 perror_with_name (pend_files
[i
]);
2167 return FATAL_EXIT_CODE
;
2169 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2171 no_output
--; no_record_file
--;
2173 /* Copy the entire contents of the main input file into
2174 the stacked input buffer previously allocated for it. */
2176 /* JF check for stdin */
2177 if (in_fname
== NULL
|| *in_fname
== 0) {
2180 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
2183 if (fstat (f
, &st
) != 0)
2184 pfatal_with_name (in_fname
);
2185 fp
->nominal_fname
= fp
->fname
= in_fname
;
2187 fp
->system_header_p
= 0;
2188 /* JF all this is mine about reading pipes and ttys */
2189 if (! S_ISREG (st
.st_mode
)) {
2190 /* Read input from a file that is not a normal disk file.
2191 We cannot preallocate a buffer with the correct size,
2192 so we must read in the file a piece at the time and make it bigger. */
2197 if (S_ISDIR (st
.st_mode
))
2198 fatal ("Input file `%s' is a directory", in_fname
);
2202 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
2204 cnt
= safe_read (f
, (char *) fp
->buf
+ size
, bsize
- size
);
2205 if (cnt
< 0) goto perror
; /* error! */
2207 if (size
!= bsize
) break; /* End of file */
2209 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
2213 /* Read a file whose size we can determine in advance.
2214 For the sake of VMS, st.st_size is just an upper bound. */
2215 fp
->buf
= (U_CHAR
*) xmalloc (st
.st_size
+ 2);
2216 fp
->length
= safe_read (f
, (char *) fp
->buf
, st
.st_size
);
2217 if (fp
->length
< 0) goto perror
;
2220 fp
->if_stack
= if_stack
;
2222 /* Make sure data ends with a newline. And put a null after it. */
2224 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
2225 /* Backslash-newline at end is not good enough. */
2226 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
2227 fp
->buf
[fp
->length
++] = '\n';
2228 missing_newline
= 1;
2230 fp
->buf
[fp
->length
] = '\0';
2232 /* Unless inhibited, convert trigraphs in the input. */
2237 /* Now that we know the input file is valid, open the output. */
2239 if (!out_fname
|| !strcmp (out_fname
, ""))
2240 out_fname
= "stdout";
2241 else if (! freopen (out_fname
, "w", stdout
))
2242 pfatal_with_name (out_fname
);
2244 output_line_directive (fp
, &outbuf
, 0, same_file
);
2246 /* Scan the -include files before the main input. */
2249 for (i
= 1; i
< argc
; i
++)
2250 if (pend_includes
[i
]) {
2251 struct include_file
*inc
;
2252 int fd
= open_include_file (pend_includes
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2254 perror_with_name (pend_includes
[i
]);
2255 return FATAL_EXIT_CODE
;
2257 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2261 /* Scan the input, processing macros and directives. */
2263 rescan (&outbuf
, 0);
2265 if (missing_newline
)
2268 if (pedantic
&& missing_newline
)
2269 pedwarn ("file does not end in newline");
2271 /* Now we have processed the entire input
2272 Write whichever kind of output has been requested. */
2274 if (dump_macros
== dump_only
)
2276 else if (! inhibit_output
) {
2281 /* Don't actually write the deps file if compilation has failed. */
2283 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, deps_mode
)))
2284 pfatal_with_name (deps_file
);
2285 fputs (deps_buffer
, deps_stream
);
2286 putc ('\n', deps_stream
);
2288 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
2289 fatal ("I/O error on output");
2294 if (pcp_outfile
&& pcp_outfile
!= stdout
2295 && (ferror (pcp_outfile
) || fclose (pcp_outfile
) != 0))
2296 fatal ("I/O error on `-pcp' output");
2298 if (ferror (stdout
) || fclose (stdout
) != 0)
2299 fatal ("I/O error on output");
2302 exit (FATAL_EXIT_CODE
);
2303 exit (SUCCESS_EXIT_CODE
);
2306 pfatal_with_name (in_fname
);
2310 /* Given a colon-separated list of file names PATH,
2311 add all the names to the search path for include files. */
2325 struct file_name_list
*dirtmp
;
2327 /* Find the end of this name. */
2328 while ((c
= *q
++) != PATH_SEPARATOR
&& c
)
2332 dirtmp
= new_include_prefix (last_include
, NULL_PTR
,
2333 "", p
== q
? "." : p
);
2335 append_include_chain (dirtmp
, dirtmp
);
2337 /* Advance past this name. */
2344 /* Return the address of the first character in S that equals C.
2345 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2346 Return 0 if there is no such character. Assume that C itself is not '\0'.
2347 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2348 but unfortunately memchr isn't autoconfigured yet. */
2356 char *p
= (char *) s
;
2358 char *q
= index (p
, c
);
2360 return (U_CHAR
*) q
;
2362 size_t l
= strlen (p
);
2372 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2373 before main CCCP processing. Name `pcp' is also in honor of the
2374 drugs the trigraph designers must have been on.
2376 Using an extra pass through the buffer takes a little extra time,
2377 but is infinitely less hairy than trying to handle trigraphs inside
2378 strings, etc. everywhere, and also makes sure that trigraphs are
2379 only translated in the top level of processing. */
2385 register U_CHAR c
, *fptr
, *bptr
, *sptr
, *lptr
;
2388 fptr
= bptr
= sptr
= buf
->buf
;
2389 lptr
= fptr
+ buf
->length
;
2390 while ((sptr
= index0 (sptr
, '?', (size_t) (lptr
- sptr
))) != NULL
) {
2427 len
= sptr
- fptr
- 2;
2429 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2430 C, this will be memmove (). */
2431 if (bptr
!= fptr
&& len
> 0)
2432 bcopy ((char *) fptr
, (char *) bptr
, len
);
2438 len
= buf
->length
- (fptr
- buf
->buf
);
2439 if (bptr
!= fptr
&& len
> 0)
2440 bcopy ((char *) fptr
, (char *) bptr
, len
);
2441 buf
->length
-= fptr
- bptr
;
2442 buf
->buf
[buf
->length
] = '\0';
2443 if (warn_trigraphs
&& fptr
!= bptr
)
2444 warning_with_line (0, "%lu trigraph(s) encountered",
2445 (unsigned long) (fptr
- bptr
) / 2);
2448 /* Move all backslash-newline pairs out of embarrassing places.
2449 Exchange all such pairs following BP
2450 with any potentially-embarrassing characters that follow them.
2451 Potentially-embarrassing characters are / and *
2452 (because a backslash-newline inside a comment delimiter
2453 would cause it not to be recognized). */
2459 register U_CHAR
*p
= bp
;
2461 /* First count the backslash-newline pairs here. */
2463 while (p
[0] == '\\' && p
[1] == '\n')
2466 /* What follows the backslash-newlines is not embarrassing. */
2468 if (*p
!= '/' && *p
!= '*')
2471 /* Copy all potentially embarrassing characters
2472 that follow the backslash-newline pairs
2473 down to where the pairs originally started. */
2475 while (*p
== '*' || *p
== '/')
2478 /* Now write the same number of pairs after the embarrassing chars. */
2485 /* Like newline_fix but for use within a directive-name.
2486 Move any backslash-newlines up past any following symbol constituents. */
2489 name_newline_fix (bp
)
2492 register U_CHAR
*p
= bp
;
2494 /* First count the backslash-newline pairs here. */
2495 while (p
[0] == '\\' && p
[1] == '\n')
2498 /* What follows the backslash-newlines is not embarrassing. */
2503 /* Copy all potentially embarrassing characters
2504 that follow the backslash-newline pairs
2505 down to where the pairs originally started. */
2507 while (is_idchar
[*p
])
2510 /* Now write the same number of pairs after the embarrassing chars. */
2517 /* Look for lint commands in comments.
2519 When we come in here, ibp points into a comment. Limit is as one expects.
2520 scan within the comment -- it should start, after lwsp, with a lint command.
2521 If so that command is returned as a (constant) string.
2523 Upon return, any arg will be pointed to with argstart and will be
2524 arglen long. Note that we don't parse that arg since it will just
2525 be printed out again. */
2528 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2529 register U_CHAR
*ibp
;
2530 register U_CHAR
*limit
;
2531 U_CHAR
**argstart
; /* point to command arg */
2532 int *arglen
, *cmdlen
; /* how long they are */
2534 HOST_WIDE_INT linsize
;
2535 register U_CHAR
*numptr
; /* temp for arg parsing */
2539 SKIP_WHITE_SPACE (ibp
);
2541 if (ibp
>= limit
) return NULL
;
2543 linsize
= limit
- ibp
;
2545 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2546 if ((linsize
>= 10) && !bcmp (ibp
, "NOTREACHED", 10)) {
2548 return "NOTREACHED";
2550 if ((linsize
>= 8) && !bcmp (ibp
, "ARGSUSED", 8)) {
2554 if ((linsize
>= 11) && !bcmp (ibp
, "LINTLIBRARY", 11)) {
2556 return "LINTLIBRARY";
2558 if ((linsize
>= 7) && !bcmp (ibp
, "VARARGS", 7)) {
2560 ibp
+= 7; linsize
-= 7;
2561 if ((linsize
== 0) || ! isdigit (*ibp
)) return "VARARGS";
2563 /* OK, read a number */
2564 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && isdigit (*numptr
);
2566 *arglen
= numptr
- *argstart
;
2573 * The main loop of the program.
2575 * Read characters from the input stack, transferring them to the
2578 * Macros are expanded and push levels on the input stack.
2579 * At the end of such a level it is popped off and we keep reading.
2580 * At the end of any other kind of level, we return.
2581 * #-directives are handled, except within macros.
2583 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2584 * and insert them when appropriate. This is set while scanning macro
2585 * arguments before substitution. It is zero when scanning for final output.
2586 * There are three types of Newline markers:
2587 * * Newline - follows a macro name that was not expanded
2588 * because it appeared inside an expansion of the same macro.
2589 * This marker prevents future expansion of that identifier.
2590 * When the input is rescanned into the final output, these are deleted.
2591 * These are also deleted by ## concatenation.
2592 * * Newline Space (or Newline and any other whitespace character)
2593 * stands for a place that tokens must be separated or whitespace
2594 * is otherwise desirable, but where the ANSI standard specifies there
2595 * is no whitespace. This marker turns into a Space (or whichever other
2596 * whitespace char appears in the marker) in the final output,
2597 * but it turns into nothing in an argument that is stringified with #.
2598 * Such stringified arguments are the only place where the ANSI standard
2599 * specifies with precision that whitespace may not appear.
2601 * During this function, IP->bufp is kept cached in IBP for speed of access.
2602 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2603 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2604 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2605 * explicitly, and before RECACHE, since RECACHE uses OBP.
2609 rescan (op
, output_marks
)
2613 /* Character being scanned in main loop. */
2616 /* Length of pending accumulated identifier. */
2617 register int ident_length
= 0;
2619 /* Hash code of pending accumulated identifier. */
2620 register int hash
= 0;
2622 /* Current input level (&instack[indepth]). */
2625 /* Pointer for scanning input. */
2626 register U_CHAR
*ibp
;
2628 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2629 register U_CHAR
*limit
;
2631 /* Pointer for storing output. */
2632 register U_CHAR
*obp
;
2634 /* REDO_CHAR is nonzero if we are processing an identifier
2635 after backing up over the terminating character.
2636 Sometimes we process an identifier without backing up over
2637 the terminating character, if the terminating character
2638 is not special. Backing up is done so that the terminating character
2639 will be dispatched on again once the identifier is dealt with. */
2642 /* 1 if within an identifier inside of which a concatenation
2643 marker (Newline -) has been seen. */
2644 int concatenated
= 0;
2646 /* While scanning a comment or a string constant,
2647 this records the line it started on, for error messages. */
2650 /* Record position of last `real' newline. */
2651 U_CHAR
*beg_of_line
;
2653 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2656 do { ip->macro->type = T_MACRO; \
2657 if (ip->free_ptr) free (ip->free_ptr); \
2658 --indepth; } while (0)
2660 /* Reload `rescan's local variables that describe the current
2661 level of the input stack. */
2664 do { ip = &instack[indepth]; \
2666 limit = ip->buf + ip->length; \
2668 check_expand (op, limit - ibp); \
2670 obp = op->bufp; } while (0)
2672 if (no_output
&& instack
[indepth
].fname
!= 0)
2673 skip_if_group (&instack
[indepth
], 1, NULL
);
2680 /* Our caller must always put a null after the end of
2681 the input at each input stack level. */
2691 if (*ibp
== '\n' && !ip
->macro
) {
2692 /* At the top level, always merge lines ending with backslash-newline,
2693 even in middle of identifier. But do not merge lines in a macro,
2694 since backslash might be followed by a newline-space marker. */
2697 --obp
; /* remove backslash from obuf */
2700 /* If ANSI, backslash is just another character outside a string. */
2703 /* Otherwise, backslash suppresses specialness of following char,
2704 so copy it here to prevent the switch from seeing it.
2705 But first get any pending identifier processed. */
2706 if (ident_length
> 0)
2713 if (ident_length
|| ip
->macro
|| traditional
)
2715 while (*ibp
== '\\' && ibp
[1] == '\n') {
2721 /* Treat this %: digraph as if it were #. */
2725 if (assertions_flag
) {
2728 /* Copy #foo (bar lose) without macro expansion. */
2729 obp
[-1] = '#'; /* In case it was '%'. */
2730 SKIP_WHITE_SPACE (ibp
);
2731 while (is_idchar
[*ibp
])
2733 SKIP_WHITE_SPACE (ibp
);
2736 skip_paren_group (ip
);
2737 bcopy ((char *) ibp
, (char *) obp
, ip
->bufp
- ibp
);
2738 obp
+= ip
->bufp
- ibp
;
2744 /* If this is expanding a macro definition, don't recognize
2745 preprocessing directives. */
2748 /* If this is expand_into_temp_buffer,
2749 don't recognize them either. Warn about them
2750 only after an actual newline at this level,
2751 not at the beginning of the input level. */
2753 if (ip
->buf
!= beg_of_line
)
2754 warning ("preprocessing directive not recognized within macro arg");
2761 /* # keyword: a # must be first nonblank char on the line */
2762 if (beg_of_line
== 0)
2767 /* Scan from start of line, skipping whitespace, comments
2768 and backslash-newlines, and see if we reach this #.
2769 If not, this # is not special. */
2771 /* If -traditional, require # to be at beginning of line. */
2774 if (is_hor_space
[*bp
])
2776 else if (*bp
== '\\' && bp
[1] == '\n')
2778 else if (*bp
== '/' && bp
[1] == '*') {
2780 while (!(*bp
== '*' && bp
[1] == '/'))
2784 /* There is no point in trying to deal with C++ // comments here,
2785 because if there is one, then this # must be part of the
2786 comment and we would never reach here. */
2792 while (bp
[1] == '\\' && bp
[2] == '\n')
2796 /* %: appears at start of line; skip past the ':' too. */
2805 /* This # can start a directive. */
2807 --obp
; /* Don't copy the '#' */
2811 if (! handle_directive (ip
, op
)) {
2815 /* Not a known directive: treat it as ordinary text.
2816 IP, OP, IBP, etc. have not been changed. */
2817 if (no_output
&& instack
[indepth
].fname
) {
2818 /* If not generating expanded output,
2819 what we do with ordinary text is skip it.
2820 Discard everything until next # directive. */
2821 skip_if_group (&instack
[indepth
], 1, 0);
2826 *obp
++ = '#'; /* Copy # (even if it was originally %:). */
2827 /* Don't expand an identifier that could be a macro directive.
2828 (Section 3.8.3 of the ANSI C standard) */
2829 SKIP_WHITE_SPACE (ibp
);
2830 if (is_idstart
[*ibp
])
2833 while (is_idchar
[*ibp
])
2841 /* A # directive has been successfully processed. */
2842 /* If not generating expanded output, ignore everything until
2843 next # directive. */
2844 if (no_output
&& instack
[indepth
].fname
)
2845 skip_if_group (&instack
[indepth
], 1, 0);
2851 case '\"': /* skip quoted string */
2853 /* A single quoted string is treated like a double -- some
2854 programs (e.g., troff) are perverse this way */
2856 /* Handle any pending identifier;
2857 but the L in L'...' or L"..." is not an identifier. */
2859 && ! (ident_length
== 1 && hash
== HASHSTEP (0, 'L')))
2862 start_line
= ip
->lineno
;
2864 /* Skip ahead to a matching quote. */
2868 if (ip
->macro
!= 0) {
2869 /* try harder: this string crosses a macro expansion boundary.
2870 This can happen naturally if -traditional.
2871 Otherwise, only -D can make a macro with an unmatched quote. */
2877 error_with_line (line_for_error (start_line
),
2878 "unterminated string or character constant");
2879 error_with_line (multiline_string_line
,
2880 "possible real start of unterminated constant");
2881 multiline_string_line
= 0;
2890 /* Traditionally, end of line ends a string constant with no error.
2891 So exit the loop and record the new line. */
2897 error_with_line (line_for_error (start_line
),
2898 "unterminated character constant");
2901 if (multiline_string_line
== 0) {
2903 pedwarn_with_line (line_for_error (start_line
),
2904 "string constant runs past end of line");
2905 multiline_string_line
= ip
->lineno
- 1;
2913 /* Backslash newline is replaced by nothing at all,
2914 but keep the line counts correct. */
2919 /* ANSI stupidly requires that in \\ the second \
2920 is *not* prevented from combining with a newline. */
2921 while (*ibp
== '\\' && ibp
[1] == '\n') {
2940 if (*ibp
== '\\' && ibp
[1] == '\n')
2944 && !(cplusplus_comments
&& *ibp
== '/'))
2952 /* C++ style comment... */
2953 start_line
= ip
->lineno
;
2955 /* Comments are equivalent to spaces. */
2956 if (! put_out_comments
)
2960 U_CHAR
*before_bp
= ibp
;
2962 while (++ibp
< limit
) {
2964 if (ibp
[-1] != '\\') {
2965 if (put_out_comments
) {
2966 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
2967 obp
+= ibp
- before_bp
;
2972 warning ("multiline `//' comment");
2974 /* Copy the newline into the output buffer, in order to
2975 avoid the pain of a #line every time a multiline comment
2977 if (!put_out_comments
)
2986 /* Ordinary C comment. Skip it, optionally copying it to output. */
2988 start_line
= ip
->lineno
;
2990 ++ibp
; /* Skip the star. */
2992 /* If this cpp is for lint, we peek inside the comments: */
2996 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2998 if (lintcmd
!= NULL
) {
3000 check_expand (op
, cmdlen
+ arglen
+ 14);
3002 /* I believe it is always safe to emit this newline: */
3004 bcopy ("#pragma lint ", (char *) obp
, 13);
3006 bcopy (lintcmd
, (char *) obp
, cmdlen
);
3011 bcopy (argbp
, (char *) obp
, arglen
);
3015 /* OK, now bring us back to the state we were in before we entered
3016 this branch. We need #line because the #pragma's newline always
3017 messes up the line count. */
3019 output_line_directive (ip
, op
, 0, same_file
);
3020 check_expand (op
, limit
- ibp
+ 2);
3026 /* Comments are equivalent to spaces.
3027 Note that we already output the slash; we might not want it.
3028 For -traditional, a comment is equivalent to nothing. */
3029 if (! put_out_comments
) {
3039 U_CHAR
*before_bp
= ibp
;
3044 if (ibp
[-2] == '/' && warn_comments
)
3045 warning ("`/*' within comment");
3046 if (*ibp
== '\\' && ibp
[1] == '\n')
3054 /* Copy the newline into the output buffer, in order to
3055 avoid the pain of a #line every time a multiline comment
3057 if (!put_out_comments
)
3064 error_with_line (line_for_error (start_line
),
3065 "unterminated comment");
3074 if (put_out_comments
) {
3075 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
3076 obp
+= ibp
- before_bp
;
3082 if (! is_idchar
['$'])
3085 pedwarn ("`$' in identifier");
3088 case '0': case '1': case '2': case '3': case '4':
3089 case '5': case '6': case '7': case '8': case '9':
3090 /* If digit is not part of identifier, it starts a number,
3091 which means that following letters are not an identifier.
3092 "0x5" does not refer to an identifier "x5".
3093 So copy all alphanumerics that follow without accumulating
3094 as an identifier. Periods also, for sake of "3.e7". */
3096 if (ident_length
== 0) {
3098 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3103 if (!is_idchar
[c
] && c
!= '.') {
3108 /* A sign can be part of a preprocessing number
3109 if it follows an `e' or `p'. */
3110 if (c
== 'e' || c
== 'E' || c
== 'p' || c
== 'P') {
3111 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3115 if (*ibp
== '+' || *ibp
== '-') {
3117 /* But traditional C does not let the token go past the sign,
3118 and C89 does not allow `p'. */
3119 if (traditional
|| (c89
&& (c
== 'p' || c
== 'P')))
3129 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3130 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3131 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3132 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3134 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3135 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3136 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3137 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3141 /* Compute step of hash function, to avoid a proc call on every token */
3142 hash
= HASHSTEP (hash
, c
);
3146 if (ip
->fname
== 0 && *ibp
== '-') {
3147 /* Newline - inhibits expansion of preceding token.
3148 If expanding a macro arg, we keep the newline -.
3149 In final output, it is deleted.
3150 We recognize Newline - in macro bodies and macro args. */
3151 if (! concatenated
) {
3156 if (!output_marks
) {
3159 /* If expanding a macro arg, keep the newline -. */
3165 /* If reprocessing a macro expansion, newline is a special marker. */
3166 else if (ip
->macro
!= 0) {
3167 /* Newline White is a "funny space" to separate tokens that are
3168 supposed to be separate but without space between.
3169 Here White means any whitespace character.
3170 Newline - marks a recursive macro use that is not
3171 supposed to be expandable. */
3173 if (is_space
[*ibp
]) {
3174 /* Newline Space does not prevent expansion of preceding token
3175 so expand the preceding token and then come back. */
3176 if (ident_length
> 0)
3179 /* If generating final output, newline space makes a space. */
3180 if (!output_marks
) {
3182 /* And Newline Newline makes a newline, so count it. */
3183 if (obp
[-1] == '\n')
3186 /* If expanding a macro arg, keep the newline space.
3187 If the arg gets stringified, newline space makes nothing. */
3190 } else abort (); /* Newline followed by something random? */
3194 /* If there is a pending identifier, handle it and come back here. */
3195 if (ident_length
> 0)
3200 /* Update the line counts and output a #line if necessary. */
3203 if (ip
->lineno
!= op
->lineno
) {
3205 output_line_directive (ip
, op
, 1, same_file
);
3206 check_expand (op
, limit
- ibp
);
3211 /* Come here either after (1) a null character that is part of the input
3212 or (2) at the end of the input, because there is a null there. */
3215 /* Our input really contains a null character. */
3219 /* At end of a macro-expansion level, pop it and read next level. */
3220 if (ip
->macro
!= 0) {
3223 /* If traditional, and we have an identifier that ends here,
3224 process it now, so we get the right error for recursion. */
3225 if (traditional
&& ident_length
3226 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
3235 /* If we don't have a pending identifier,
3236 return at end of input. */
3237 if (ident_length
== 0) {
3245 /* If we do have a pending identifier, just consider this null
3246 a special character and arrange to dispatch on it again.
3247 The second time, IDENT_LENGTH will be zero so we will return. */
3253 /* Handle the case of a character such as /, ', " or null
3254 seen following an identifier. Back over it so that
3255 after the identifier is processed the special char
3256 will be dispatched on again. */
3266 if (ident_length
> 0) {
3267 register HASHNODE
*hp
;
3269 /* We have just seen an identifier end. If it's a macro, expand it.
3271 IDENT_LENGTH is the length of the identifier
3272 and HASH is its hash code.
3274 The identifier has already been copied to the output,
3275 so if it is a macro we must remove it.
3277 If REDO_CHAR is 0, the char that terminated the identifier
3278 has been skipped in the output and the input.
3279 OBP-IDENT_LENGTH-1 points to the identifier.
3280 If the identifier is a macro, we must back over the terminator.
3282 If REDO_CHAR is 1, the terminating char has already been
3283 backed over. OBP-IDENT_LENGTH points to the identifier. */
3285 if (!pcp_outfile
|| pcp_inside_if
) {
3286 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
3289 if (hp
->length
== ident_length
) {
3290 int obufp_before_macroname
;
3291 int op_lineno_before_macroname
;
3292 register int i
= ident_length
;
3293 register U_CHAR
*p
= hp
->name
;
3294 register U_CHAR
*q
= obp
- i
;
3300 do { /* All this to avoid a strncmp () */
3305 /* We found a use of a macro name.
3306 see if the context shows it is a macro call. */
3308 /* Back up over terminating character if not already done. */
3314 /* Save this as a displacement from the beginning of the output
3315 buffer. We can not save this as a position in the output
3316 buffer, because it may get realloc'ed by RECACHE. */
3317 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
3318 op_lineno_before_macroname
= op
->lineno
;
3320 if (hp
->type
== T_PCSTRING
) {
3321 pcstring_used (hp
); /* Mark the definition of this key
3322 as needed, ensuring that it
3324 break; /* Exit loop, since the key cannot have a
3325 definition any longer. */
3328 /* Record whether the macro is disabled. */
3329 disabled
= hp
->type
== T_DISABLED
;
3331 /* This looks like a macro ref, but if the macro was disabled,
3332 just copy its name and put in a marker if requested. */
3336 /* This error check caught useful cases such as
3337 #define foo(x,y) bar (x (y,0), y)
3340 error ("recursive use of macro `%s'", hp
->name
);
3344 check_expand (op
, limit
- ibp
+ 2);
3351 /* If macro wants an arglist, verify that a '(' follows.
3352 first skip all whitespace, copying it to the output
3353 after the macro name. Then, if there is no '(',
3354 decide this is not a macro call and leave things that way. */
3355 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
3356 && hp
->value
.defn
->nargs
>= 0)
3358 U_CHAR
*old_ibp
= ibp
;
3359 U_CHAR
*old_obp
= obp
;
3360 int old_iln
= ip
->lineno
;
3361 int old_oln
= op
->lineno
;
3364 /* Scan forward over whitespace, copying it to the output. */
3365 if (ibp
== limit
&& ip
->macro
!= 0) {
3370 old_iln
= ip
->lineno
;
3371 old_oln
= op
->lineno
;
3373 /* A comment: copy it unchanged or discard it. */
3374 else if (*ibp
== '/' && ibp
[1] == '*') {
3375 if (put_out_comments
) {
3378 } else if (! traditional
) {
3382 while (ibp
+ 1 != limit
3383 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
3384 /* We need not worry about newline-marks,
3385 since they are never found in comments. */
3387 /* Newline in a file. Count it. */
3391 if (put_out_comments
)
3397 if (put_out_comments
) {
3402 else if (is_space
[*ibp
]) {
3404 if (ibp
[-1] == '\n') {
3405 if (ip
->macro
== 0) {
3406 /* Newline in a file. Count it. */
3409 } else if (!output_marks
) {
3410 /* A newline mark, and we don't want marks
3411 in the output. If it is newline-hyphen,
3412 discard it entirely. Otherwise, it is
3413 newline-whitechar, so keep the whitechar. */
3423 /* A newline mark; copy both chars to the output. */
3431 /* It isn't a macro call.
3432 Put back the space that we just skipped. */
3435 ip
->lineno
= old_iln
;
3436 op
->lineno
= old_oln
;
3437 /* Exit the for loop. */
3442 /* This is now known to be a macro call.
3443 Discard the macro name from the output,
3444 along with any following whitespace just copied,
3445 but preserve newlines if not outputting marks since this
3446 is more likely to do the right thing with line numbers. */
3447 obp
= op
->buf
+ obufp_before_macroname
;
3449 op
->lineno
= op_lineno_before_macroname
;
3451 int newlines
= op
->lineno
- op_lineno_before_macroname
;
3452 while (0 < newlines
--)
3456 /* Prevent accidental token-pasting with a character
3457 before the macro call. */
3458 if (!traditional
&& obp
!= op
->buf
) {
3460 case '!': case '%': case '&': case '*':
3461 case '+': case '-': case '.': case '/':
3462 case ':': case '<': case '=': case '>':
3464 /* If we are expanding a macro arg, make a newline marker
3465 to separate the tokens. If we are making real output,
3466 a plain space will do. */
3473 /* Expand the macro, reading arguments as needed,
3474 and push the expansion on the input stack. */
3477 macroexpand (hp
, op
);
3479 /* Reexamine input stack, since macroexpand has pushed
3480 a new level on it. */
3487 } /* End hash-table-search loop */
3489 ident_length
= hash
= 0; /* Stop collecting identifier */
3492 } /* End if (ident_length > 0) */
3494 } /* End per-char loop */
3496 /* Come here to return -- but first give an error message
3497 if there was an unterminated successful conditional. */
3499 if (if_stack
!= ip
->if_stack
)
3503 switch (if_stack
->type
)
3524 error_with_line (line_for_error (if_stack
->lineno
),
3525 "unterminated `#%s' conditional", str
);
3527 if_stack
= ip
->if_stack
;
3531 * Rescan a string into a temporary buffer and return the result
3532 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3534 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3535 * and insert such markers when appropriate. See `rescan' for details.
3536 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3537 * before substitution; it is 0 for other uses.
3540 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
3541 U_CHAR
*buf
, *limit
;
3542 int output_marks
, assertions
;
3544 register FILE_BUF
*ip
;
3546 int length
= limit
- buf
;
3548 int odepth
= indepth
;
3549 int save_assertions_flag
= assertions_flag
;
3551 assertions_flag
= assertions
;
3556 /* Set up the input on the input stack. */
3558 buf1
= (U_CHAR
*) alloca (length
+ 1);
3560 register U_CHAR
*p1
= buf
;
3561 register U_CHAR
*p2
= buf1
;
3568 /* Set up to receive the output. */
3570 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3571 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3572 obuf
.nominal_fname
= 0;
3579 obuf
.system_header_p
= 0;
3581 CHECK_DEPTH ({return obuf
;});
3585 ip
= &instack
[indepth
];
3587 ip
->nominal_fname
= 0;
3589 ip
->system_header_p
= 0;
3592 ip
->length
= length
;
3593 ip
->buf
= ip
->bufp
= buf1
;
3594 ip
->if_stack
= if_stack
;
3596 ip
->lineno
= obuf
.lineno
= 1;
3598 /* Scan the input, create the output. */
3599 rescan (&obuf
, output_marks
);
3601 /* Pop input stack to original state. */
3604 if (indepth
!= odepth
)
3607 /* Record the output. */
3608 obuf
.length
= obuf
.bufp
- obuf
.buf
;
3610 assertions_flag
= save_assertions_flag
;
3615 * Process a # directive. Expects IP->bufp to point after the '#', as in
3616 * `#define foo bar'. Passes to the directive handler
3617 * (do_define, do_include, etc.): the addresses of the 1st and
3618 * last chars of the directive (starting immediately after the #
3619 * keyword), plus op and the keyword table pointer. If the directive
3620 * contains comments it is copied into a temporary buffer sans comments
3621 * and the temporary buffer is passed to the directive handler instead.
3622 * Likewise for backslash-newlines.
3624 * Returns nonzero if this was a known # directive.
3625 * Otherwise, returns zero, without advancing the input pointer.
3629 handle_directive (ip
, op
)
3632 register U_CHAR
*bp
, *cp
;
3633 register struct directive
*kt
;
3634 register int ident_length
;
3637 /* Nonzero means we must copy the entire directive
3638 to get rid of comments or backslash-newlines. */
3639 int copy_directive
= 0;
3641 U_CHAR
*ident
, *after_ident
;
3645 /* Record where the directive started. do_xifdef needs this. */
3646 directive_start
= bp
- 1;
3648 /* Skip whitespace and \-newline. */
3650 if (is_hor_space
[*bp
]) {
3651 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3652 pedwarn ("%s in preprocessing directive", char_name
[*bp
]);
3654 } else if (*bp
== '/' && (bp
[1] == '*'
3655 || (cplusplus_comments
&& bp
[1] == '/'))) {
3657 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3659 } else if (*bp
== '\\' && bp
[1] == '\n') {
3660 bp
+= 2; ip
->lineno
++;
3664 /* Now find end of directive name.
3665 If we encounter a backslash-newline, exchange it with any following
3666 symbol-constituents so that we end up with a contiguous name. */
3673 if (*cp
== '\\' && cp
[1] == '\n')
3674 name_newline_fix (cp
);
3680 ident_length
= cp
- bp
;
3684 /* A line of just `#' becomes blank. */
3686 if (ident_length
== 0 && *after_ident
== '\n') {
3687 ip
->bufp
= after_ident
;
3691 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3693 while (is_idchar
[*p
]) {
3694 if (*p
< '0' || *p
> '9')
3698 /* Handle # followed by a line number. */
3699 if (p
!= ident
&& !is_idchar
[*p
]) {
3700 static struct directive line_directive_table
[] = {
3701 { 4, do_line
, "line", T_LINE
},
3704 pedwarn ("`#' followed by integer");
3705 after_ident
= ident
;
3706 kt
= line_directive_table
;
3710 /* Avoid error for `###' and similar cases unless -pedantic. */
3712 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3714 if (pedantic
&& !lang_asm
)
3715 warning ("invalid preprocessing directive");
3721 error ("invalid preprocessing directive name");
3727 * Decode the keyword and call the appropriate expansion
3728 * routine, after moving the input pointer up to the next line.
3730 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3731 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3732 register U_CHAR
*buf
;
3733 register U_CHAR
*limit
;
3736 int *already_output
;
3738 /* Nonzero means do not delete comments within the directive.
3739 #define needs this when -traditional. */
3744 limit
= ip
->buf
+ ip
->length
;
3747 keep_comments
= traditional
&& kt
->type
== T_DEFINE
;
3748 /* #import is defined only in Objective C, or when on the NeXT. */
3749 if (kt
->type
== T_IMPORT
3750 && !(objc
|| lookup ((U_CHAR
*) "__NeXT__", -1, -1)))
3753 /* Find the end of this directive (first newline not backslashed
3754 and not in a string or comment).
3755 Set COPY_DIRECTIVE if the directive must be copied
3756 (it contains a backslash-newline or a comment). */
3758 buf
= bp
= after_ident
;
3759 while (bp
< limit
) {
3760 register U_CHAR c
= *bp
++;
3768 } else if (traditional
)
3775 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3776 /* Don't bother calling the directive if we already got an error
3777 message due to unterminated string. Skip everything and pretend
3778 we called the directive. */
3781 /* Traditional preprocessing permits unterminated strings. */
3790 /* <...> is special for #include. */
3792 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3794 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3795 if (*bp
== '\\' && bp
[1] == '\n') {
3805 if (*bp
== '\\' && bp
[1] == '\n')
3808 || (cplusplus_comments
&& *bp
== '/')) {
3809 U_CHAR
*obp
= bp
- 1;
3811 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3813 /* No need to copy the directive because of a comment at the end;
3814 just don't include the comment in the directive. */
3815 if (!put_out_comments
) {
3817 for (p
= bp
; *p
== ' ' || *p
== '\t'; p
++)
3824 /* Don't remove the comments if -traditional. */
3825 if (! keep_comments
)
3834 pedwarn ("%s in preprocessing directive", char_name
[c
]);
3838 --bp
; /* Point to the newline */
3846 resume_p
= ip
->bufp
;
3847 /* BP is the end of the directive.
3848 RESUME_P is the next interesting data after the directive.
3849 A comment may come between. */
3851 /* If a directive should be copied through, and -C was given,
3852 pass it through before removing comments. */
3853 if (!no_output
&& put_out_comments
3854 && (kt
->type
== T_DEFINE
? dump_macros
== dump_definitions
3855 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
3856 : kt
->type
== T_PRAGMA
)) {
3859 /* Output directive name. */
3860 check_expand (op
, kt
->length
+ 2);
3861 /* Make sure # is at the start of a line */
3862 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3867 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3868 op
->bufp
+= kt
->length
;
3870 /* Output arguments. */
3872 check_expand (op
, len
);
3873 bcopy (buf
, (char *) op
->bufp
, len
);
3875 /* Take account of any (escaped) newlines just output. */
3877 if (buf
[len
] == '\n')
3880 already_output
= &junk
;
3881 } /* Don't we need a newline or #line? */
3883 if (copy_directive
) {
3884 register U_CHAR
*xp
= buf
;
3885 /* Need to copy entire directive into temp buffer before dispatching */
3887 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for directive plus
3891 /* Copy to the new buffer, deleting comments
3892 and backslash-newlines (and whitespace surrounding the latter). */
3895 register U_CHAR c
= *xp
++;
3900 abort (); /* A bare newline should never part of the line. */
3903 /* <...> is special for #include. */
3905 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3907 while (xp
< bp
&& c
!= '>') {
3909 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
3920 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
3921 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
3923 SKIP_WHITE_SPACE (xp
);
3924 } else if (is_hor_space
[*xp
]) {
3926 SKIP_WHITE_SPACE (xp
);
3928 } else if (traditional
&& xp
< bp
) {
3936 register U_CHAR
*bp1
3937 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
3938 NULL_PTR
, NULL_PTR
, NULL_PTR
);
3952 || (cplusplus_comments
&& *xp
== '/')) {
3954 /* If we already copied the directive through,
3955 already_output != 0 prevents outputting comment now. */
3956 skip_to_end_of_comment (ip
, already_output
, 0);
3958 while (xp
!= ip
->bufp
)
3960 /* Delete or replace the slash. */
3961 else if (traditional
)
3970 /* Null-terminate the copy. */
3976 ip
->bufp
= resume_p
;
3978 /* Some directives should be written out for cc1 to process,
3979 just as if they were not defined. And sometimes we're copying
3980 directives through. */
3982 if (!no_output
&& already_output
== 0
3983 && (kt
->type
== T_DEFINE
? dump_names
<= dump_macros
3984 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
3985 : kt
->type
== T_PRAGMA
)) {
3988 /* Output directive name. */
3989 check_expand (op
, kt
->length
+ 1);
3991 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
3992 op
->bufp
+= kt
->length
;
3994 if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
3995 /* Output `#define name' only. */
3998 SKIP_WHITE_SPACE (xp
);
4000 while (is_idchar
[*xp
]) xp
++;
4002 check_expand (op
, len
+ 1);
4004 bcopy (yp
, (char *) op
->bufp
, len
);
4006 /* Output entire directive. */
4008 check_expand (op
, len
);
4009 bcopy (buf
, (char *) op
->bufp
, len
);
4012 } /* Don't we need a newline or #line? */
4014 /* Call the appropriate directive handler. buf now points to
4015 either the appropriate place in the input buffer, or to
4016 the temp buffer if it was necessary to make one. cp
4017 points to the first char after the contents of the (possibly
4018 copied) directive, in either case. */
4019 (*kt
->func
) (buf
, cp
, op
, kt
);
4020 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
4026 /* It is deliberate that we don't warn about undefined directives.
4027 That is the responsibility of cc1. */
4034 static struct tm
*timebuf
;
4036 time_t t
= time ((time_t *) 0);
4037 timebuf
= localtime (&t
);
4042 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4043 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4047 * expand things like __FILE__. Place the expansion into the output
4048 * buffer *without* rescanning.
4052 special_symbol (hp
, op
)
4059 FILE_BUF
*ip
= NULL
;
4062 int paren
= 0; /* For special `defined' keyword */
4064 if (pcp_outfile
&& pcp_inside_if
4065 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4066 error ("Predefined macro `%s' used inside `#if' during precompilation",
4069 for (i
= indepth
; i
>= 0; i
--)
4070 if (instack
[i
].fname
!= NULL
) {
4075 error ("cccp error: not in any file?!");
4076 return; /* the show must go on */
4084 if (hp
->type
== T_FILE
)
4085 string
= ip
->nominal_fname
;
4087 string
= instack
[0].nominal_fname
;
4091 buf
= (char *) alloca (3 + 4 * strlen (string
));
4092 quote_string (buf
, string
);
4100 case T_INCLUDE_LEVEL
:
4102 for (i
= indepth
; i
>= 0; i
--)
4103 if (instack
[i
].fname
!= NULL
)
4106 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
4107 sprintf (buf
, "%d", true_indepth
- 1);
4111 buf
= (char *) alloca (3 + strlen (version_string
));
4112 sprintf (buf
, "\"%s\"", version_string
);
4115 #ifndef NO_BUILTIN_SIZE_TYPE
4121 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4122 case T_PTRDIFF_TYPE
:
4131 case T_USER_LABEL_PREFIX_TYPE
:
4132 buf
= USER_LABEL_PREFIX
;
4135 case T_REGISTER_PREFIX_TYPE
:
4136 buf
= REGISTER_PREFIX
;
4139 case T_IMMEDIATE_PREFIX_TYPE
:
4140 buf
= IMMEDIATE_PREFIX
;
4144 buf
= hp
->value
.cpval
;
4145 #ifdef STDC_0_IN_SYSTEM_HEADERS
4146 if (ip
->system_header_p
4147 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
4148 && !lookup ((U_CHAR
*) "__STRICT_ANSI__", -1, -1))
4151 if (pcp_inside_if
&& pcp_outfile
)
4152 /* Output a precondition for this macro use */
4153 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4157 buf
= (char *) alloca (10);
4158 sprintf (buf
, "%d", ip
->lineno
);
4163 buf
= (char *) alloca (20);
4164 timebuf
= timestamp ();
4165 if (hp
->type
== T_DATE
)
4166 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4167 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4169 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
4173 case T_SPEC_DEFINED
:
4174 buf
= " 0 "; /* Assume symbol is not defined */
4175 ip
= &instack
[indepth
];
4176 SKIP_WHITE_SPACE (ip
->bufp
);
4177 if (*ip
->bufp
== '(') {
4179 ip
->bufp
++; /* Skip over the paren */
4180 SKIP_WHITE_SPACE (ip
->bufp
);
4183 if (!is_idstart
[*ip
->bufp
])
4185 if (ip
->bufp
[0] == 'L' && (ip
->bufp
[1] == '\'' || ip
->bufp
[1] == '"'))
4187 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4188 if (pcp_outfile
&& pcp_inside_if
4189 && (hp
->type
== T_CONST
4190 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4191 /* Output a precondition for this macro use. */
4192 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4196 if (pcp_outfile
&& pcp_inside_if
) {
4197 /* Output a precondition for this macro use */
4198 U_CHAR
*cp
= ip
->bufp
;
4199 fprintf (pcp_outfile
, "#undef ");
4200 while (is_idchar
[*cp
]) /* Ick! */
4201 fputc (*cp
++, pcp_outfile
);
4202 putc ('\n', pcp_outfile
);
4204 while (is_idchar
[*ip
->bufp
])
4206 SKIP_WHITE_SPACE (ip
->bufp
);
4208 if (*ip
->bufp
!= ')')
4216 error ("`defined' without an identifier");
4220 error ("cccp error: invalid special hash type"); /* time for gdb */
4224 check_expand (op
, len
);
4225 bcopy (buf
, (char *) op
->bufp
, len
);
4232 /* Routines to handle #directives */
4234 /* Handle #include and #import.
4235 This function expects to see "fname" or <fname> on the input. */
4238 do_include (buf
, limit
, op
, keyword
)
4239 U_CHAR
*buf
, *limit
;
4241 struct directive
*keyword
;
4243 U_CHAR
*importing
= keyword
->type
== T_IMPORT
? (U_CHAR
*) "" : (U_CHAR
*) 0;
4244 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4245 static int import_warning
= 0;
4246 char *fname
; /* Dynamically allocated fname buffer */
4249 char *fbeg
, *fend
; /* Beginning and end of fname */
4252 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4253 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4254 struct file_name_list
*searchptr
= 0;
4257 int f
= -3; /* file number */
4258 struct include_file
*inc
= 0;
4260 int retried
= 0; /* Have already tried macro
4261 expanding the include line*/
4262 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4264 int vaxc_include
= 0; /* 1 for token without punctuation */
4271 if (pedantic
&& !instack
[indepth
].system_header_p
)
4274 pedwarn ("ANSI C does not allow `#import'");
4276 pedwarn ("ANSI C does not allow `#include_next'");
4279 if (importing
&& warn_import
&& !inhibit_warnings
4280 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4282 warning ("using `#import' is not recommended");
4283 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
4284 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
4285 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
4286 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
4287 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
4288 fprintf (stderr
, " ... <real contents of file> ...\n");
4289 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
4290 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
4291 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
4292 fprintf (stderr
, "when it is equipped with such a conditional.\n");
4298 SKIP_WHITE_SPACE (fin
);
4299 /* Discard trailing whitespace so we can easily see
4300 if we have parsed all the significant chars we were given. */
4301 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4302 fbeg
= fend
= (char *) alloca (limit
- fin
);
4308 /* Copy the operand text, concatenating the strings. */
4310 while (fin
!= limit
) {
4311 while (fin
!= limit
&& *fin
!= '\"')
4316 /* If not at the end, there had better be another string. */
4317 /* Skip just horiz space, and don't go past limit. */
4318 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4319 if (fin
!= limit
&& *fin
== '\"')
4326 /* We have "filename". Figure out directory this source
4327 file is coming from and put it on the front of the list. */
4329 /* If -I- was specified, don't search current dir, only spec'd ones. */
4330 if (ignore_srcdir
) break;
4332 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4337 if ((nam
= fp
->nominal_fname
) != NULL
) {
4338 /* Found a named file. Figure out dir of the file,
4339 and put it in front of the search list. */
4340 dsp
= ((struct file_name_list
*)
4341 alloca (sizeof (struct file_name_list
) + strlen (nam
)));
4342 strcpy (dsp
->fname
, nam
);
4343 simplify_filename (dsp
->fname
);
4344 nam
= base_name (dsp
->fname
);
4346 /* But for efficiency's sake, do not insert the dir
4347 if it matches the search list's first dir. */
4348 dsp
->next
= search_start
;
4349 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4351 n
= nam
- dsp
->fname
;
4352 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4353 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4355 dsp
[0].got_name_map
= 0;
4363 while (fin
!= limit
&& *fin
!= '>')
4365 if (*fin
== '>' && fin
+ 1 == limit
) {
4367 /* If -I-, start with the first -I dir after the -I-. */
4368 search_start
= first_bracket_include
;
4376 * Support '#include xyz' like VAX-C to allow for easy use of all the
4377 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4378 * code from case '<' is repeated here) and generates a warning.
4379 * (Note: macro expansion of `xyz' takes precedence.)
4381 if (retried
&& isalpha(*(U_CHAR
*) (--fbeg
))) {
4382 while (fin
!= limit
&& (!isspace(*fin
)))
4384 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4388 /* If -I-, start with the first -I dir after the -I-. */
4389 search_start
= first_bracket_include
;
4397 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4400 /* Expand buffer and then remove any newline markers.
4401 We can't just tell expand_to_temp_buffer to omit the markers,
4402 since it would put extra spaces in include file names. */
4405 trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4407 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4409 while (src
!= trybuf
.bufp
) {
4410 switch ((*limit
++ = *src
++)) {
4419 U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4420 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4434 /* For #include_next, skip in the search path
4435 past the dir in which the containing file was found. */
4438 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4439 if (fp
->fname
!= NULL
) {
4440 /* fp->dir is null if the containing file was specified
4441 with an absolute file name. In that case, don't skip anything. */
4443 search_start
= fp
->dir
->next
;
4449 flen
= simplify_filename (fbeg
);
4453 error ("empty file name in `#%s'", keyword
->name
);
4457 /* Allocate this permanently, because it gets stored in the definitions
4459 fname
= xmalloc (max_include_len
+ flen
+ 1);
4460 /* + 1 above for terminating null. */
4462 system_include_depth
+= angle_brackets
;
4464 /* If specified file name is absolute, just open it. */
4466 if (absolute_filename (fbeg
)) {
4467 strcpy (fname
, fbeg
);
4468 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4472 struct bypass_dir
*next
;
4474 struct file_name_list
*searchptr
;
4475 } **bypass_slot
= 0;
4477 /* Search directory path, trying to open the file.
4478 Copy each filename tried into FNAME. */
4480 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4482 if (searchptr
== first_bracket_include
) {
4483 /* Go to bypass directory if we know we've seen this file before. */
4484 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4485 struct bypass_dir
*p
;
4486 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4488 for (p
= *bypass_slot
; p
; p
= p
->next
)
4489 if (!strcmp (fbeg
, p
->fname
)) {
4490 searchptr
= p
->searchptr
;
4496 strcpy (fname
, searchptr
->fname
);
4497 strcat (fname
, fbeg
);
4499 /* Change this 1/2 Unix 1/2 VMS file specification into a
4500 full VMS file specification */
4501 if (searchptr
->fname
[0]) {
4502 /* Fix up the filename */
4503 hack_vms_include_specification (fname
, vaxc_include
);
4505 /* This is a normal VMS filespec, so use it unchanged. */
4506 strcpy (fname
, fbeg
);
4507 /* if it's '#include filename', add the missing .h */
4508 if (vaxc_include
&& index(fname
,'.')==NULL
) {
4509 strcat (fname
, ".h");
4513 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4515 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4516 /* This is the first time we found this include file,
4517 and we found it after first_bracket_include.
4518 Record its location so that we can bypass to here next time. */
4519 struct bypass_dir
*p
4520 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4521 p
->next
= *bypass_slot
;
4522 p
->fname
= fname
+ strlen (searchptr
->fname
);
4523 p
->searchptr
= searchptr
;
4529 /* Our VMS hacks can produce invalid filespecs, so don't worry
4530 about errors other than EACCES. */
4531 if (errno
== EACCES
)
4534 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
4544 /* The file was already included. */
4546 /* If generating dependencies and -MG was specified, we assume missing
4547 files are leaf files, living in the same directory as the source file
4548 or other similar place; these missing files may be generated from
4549 other files and may not exist yet (eg: y.tab.h). */
4550 } else if (print_deps_missing_files
4551 && (system_include_depth
!= 0) < print_deps
)
4553 /* If it was requested as a system header file,
4554 then assume it belongs in the first place to look for such. */
4558 char *p
= (char *) alloca (strlen (search_start
->fname
)
4559 + strlen (fbeg
) + 1);
4560 strcpy (p
, search_start
->fname
);
4562 deps_output (p
, ' ');
4567 /* Otherwise, omit the directory, as if the file existed
4568 in the directory with the source. */
4569 deps_output (fbeg
, ' ');
4572 /* If -M was specified, and this header file won't be added to the
4573 dependency list, then don't count this as an error, because we can
4574 still produce correct output. Otherwise, we can't produce correct
4575 output, because there may be dependencies we need inside the missing
4576 file, and we don't know what directory this missing file exists in. */
4577 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4578 warning ("No include path in which to find %s", fbeg
);
4580 error_from_errno (fbeg
);
4582 error ("No include path in which to find %s", fbeg
);
4586 /* Actually process the file. */
4588 pcftry
= (char *) alloca (strlen (fname
) + 30);
4595 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4597 pcf
= open (pcftry
, O_RDONLY
, 0666);
4602 if (fstat (pcf
, &s
) != 0)
4603 pfatal_with_name (pcftry
);
4604 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4605 || inc
->st
.st_dev
!= s
.st_dev
)
4607 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4608 /* Don't need it any more. */
4613 /* Don't need it at all. */
4618 } while (pcf
!= -1 && !pcfbuf
);
4621 /* Actually process the file */
4623 pcfname
= xmalloc (strlen (pcftry
) + 1);
4624 strcpy (pcfname
, pcftry
);
4625 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) pcfbuflimit
,
4626 (U_CHAR
*) fname
, op
);
4629 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4632 system_include_depth
-= angle_brackets
;
4637 /* Return nonzero if the given FILENAME is an absolute pathname which
4638 designates a file within one of the known "system" include file
4639 directories. We assume here that if the given FILENAME looks like
4640 it is the name of a file which resides either directly in a "system"
4641 include file directory, or within any subdirectory thereof, then the
4642 given file must be a "system" include file. This function tells us
4643 if we should suppress pedantic errors/warnings for the given FILENAME.
4645 The value is 2 if the file is a C-language system header file
4646 for which C++ should (on most systems) assume `extern "C"'. */
4649 is_system_include (filename
)
4650 register char *filename
;
4652 struct file_name_list
*searchptr
;
4654 for (searchptr
= first_system_include
; searchptr
;
4655 searchptr
= searchptr
->next
)
4656 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4657 return searchptr
->c_system_include_path
+ 1;
4661 /* Yield the non-directory suffix of a file name. */
4669 #if defined (__MSDOS__) || defined (_WIN32)
4670 if (isalpha (s
[0]) && s
[1] == ':') s
+= 2;
4673 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4674 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4675 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4679 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4680 #ifdef DIR_SEPARATOR
4681 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4686 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4689 absolute_filename (filename
)
4692 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN32__))
4693 if (isalpha (filename
[0]) && filename
[1] == ':') filename
+= 2;
4695 #if defined (__CYGWIN32__)
4696 /* At present, any path that begins with a drive spec is absolute. */
4697 if (isalpha (filename
[0]) && filename
[1] == ':') return 1;
4699 if (filename
[0] == '/') return 1;
4700 #ifdef DIR_SEPARATOR
4701 if (filename
[0] == DIR_SEPARATOR
) return 1;
4706 /* Remove unnecessary characters from FILENAME in place,
4707 to avoid unnecessary filename aliasing.
4708 Return the length of the resulting string.
4710 Do only the simplifications allowed by Posix.
4711 It is OK to miss simplifications on non-Posix hosts,
4712 since this merely leads to suboptimial results. */
4715 simplify_filename (filename
)
4718 register char *from
= filename
;
4719 register char *to
= filename
;
4722 /* Remove redundant initial /s. */
4725 if (*++from
== '/') {
4726 if (*++from
== '/') {
4727 /* 3 or more initial /s are equivalent to 1 /. */
4728 while (*++from
== '/')
4731 /* On some hosts // differs from /; Posix allows this. */
4732 static int slashslash_vs_slash
;
4733 if (slashslash_vs_slash
== 0) {
4735 slashslash_vs_slash
= ((stat ("/", &s1
) == 0 && stat ("//", &s2
) == 0
4736 && INO_T_EQ (s1
.st_ino
, s2
.st_ino
)
4737 && s1
.st_dev
== s2
.st_dev
)
4740 if (slashslash_vs_slash
< 0)
4748 if (from
[0] == '.' && from
[1] == '/')
4751 /* Copy this component and trailing /, if any. */
4752 while ((*to
++ = *from
++) != '/') {
4754 /* Trim . component at end of nonempty name. */
4755 to
-= filename
<= to
- 3 && to
[-3] == '/' && to
[-2] == '.';
4757 /* Trim unnecessary trailing /s. */
4758 while (to0
< --to
&& to
[-1] == '/')
4762 return to
- filename
;
4767 /* Skip /s after a /. */
4768 while (*from
== '/')
4773 /* The file_name_map structure holds a mapping of file names for a
4774 particular directory. This mapping is read from the file named
4775 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4776 map filenames on a file system with severe filename restrictions,
4777 such as DOS. The format of the file name map file is just a series
4778 of lines with two tokens on each line. The first token is the name
4779 to map, and the second token is the actual name to use. */
4781 struct file_name_map
4783 struct file_name_map
*map_next
;
4788 #define FILE_NAME_MAP_FILE "header.gcc"
4790 /* Read a space delimited string of unlimited length from a stdio
4794 read_filename_string (ch
, f
)
4802 set
= alloc
= xmalloc (len
+ 1);
4806 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
4808 if (set
- alloc
== len
)
4811 alloc
= xrealloc (alloc
, len
+ 1);
4812 set
= alloc
+ len
/ 2;
4822 /* Read the file name map file for DIRNAME.
4823 If DIRNAME is empty, read the map file for the working directory;
4824 otherwise DIRNAME must end in '/'. */
4826 static struct file_name_map
*
4827 read_name_map (dirname
)
4830 /* This structure holds a linked list of file name maps, one per
4832 struct file_name_map_list
4834 struct file_name_map_list
*map_list_next
;
4835 char *map_list_name
;
4836 struct file_name_map
*map_list_map
;
4838 static struct file_name_map_list
*map_list
;
4839 register struct file_name_map_list
*map_list_ptr
;
4844 for (map_list_ptr
= map_list
; map_list_ptr
;
4845 map_list_ptr
= map_list_ptr
->map_list_next
)
4846 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
4847 return map_list_ptr
->map_list_map
;
4849 map_list_ptr
= ((struct file_name_map_list
*)
4850 xmalloc (sizeof (struct file_name_map_list
)));
4851 map_list_ptr
->map_list_name
= savestring (dirname
);
4852 map_list_ptr
->map_list_map
= NULL
;
4854 dirlen
= strlen (dirname
);
4855 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
4856 strcpy (name
, dirname
);
4857 strcat (name
, FILE_NAME_MAP_FILE
);
4858 f
= fopen (name
, "r");
4860 map_list_ptr
->map_list_map
= NULL
;
4865 while ((ch
= getc (f
)) != EOF
)
4868 struct file_name_map
*ptr
;
4873 from
= read_filename_string (ch
, f
);
4874 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
4876 to
= read_filename_string (ch
, f
);
4878 simplify_filename (from
);
4879 tolen
= simplify_filename (to
);
4881 ptr
= ((struct file_name_map
*)
4882 xmalloc (sizeof (struct file_name_map
)));
4883 ptr
->map_from
= from
;
4885 /* Make the real filename absolute. */
4886 if (absolute_filename (to
))
4890 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
4891 strcpy (ptr
->map_to
, dirname
);
4892 strcat (ptr
->map_to
, to
);
4896 ptr
->map_next
= map_list_ptr
->map_list_map
;
4897 map_list_ptr
->map_list_map
= ptr
;
4899 while ((ch
= getc (f
)) != '\n')
4906 map_list_ptr
->map_list_next
= map_list
;
4907 map_list
= map_list_ptr
;
4909 return map_list_ptr
->map_list_map
;
4912 /* Try to open include file FILENAME. SEARCHPTR is the directory
4913 being tried from the include file search path.
4914 IMPORTING is "" if we are importing, null otherwise.
4915 Return -2 if found, either a matching name or a matching inode.
4916 Otherwise, open the file and return a file descriptor if successful
4917 or -1 if unsuccessful.
4918 Unless unsuccessful, put a descriptor of the included file into *PINC.
4919 This function maps filenames on file systems based on information read by
4923 open_include_file (filename
, searchptr
, importing
, pinc
)
4925 struct file_name_list
*searchptr
;
4927 struct include_file
**pinc
;
4929 char *fname
= remap_include_file (filename
, searchptr
);
4932 /* Look up FNAME in include_hashtab. */
4933 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
4936 struct include_file
*inc
, *head
= *phead
;
4937 for (inc
= head
; inc
; inc
= inc
->next
)
4938 if (!strcmp (fname
, inc
->fname
))
4942 || ! inc
->control_macro
4943 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
4945 fd
= open (fname
, O_RDONLY
, 0);
4951 /* FNAME was not in include_hashtab; insert a new entry. */
4952 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
4955 inc
->control_macro
= 0;
4956 inc
->deps_output
= 0;
4957 if (fstat (fd
, &inc
->st
) != 0)
4958 pfatal_with_name (fname
);
4961 /* Look for another file with the same inode and device. */
4962 if (lookup_ino_include (inc
)
4963 && inc
->control_macro
4964 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
4970 /* For -M, add this file to the dependencies. */
4971 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
4972 inc
->deps_output
= 1;
4973 deps_output (fname
, ' ');
4976 /* Handle -H option. */
4977 if (print_include_names
)
4978 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
4982 inc
->control_macro
= importing
;
4988 /* Return the remapped name of the the include file FILENAME.
4989 SEARCHPTR is the directory being tried from the include file path. */
4992 remap_include_file (filename
, searchptr
)
4994 struct file_name_list
*searchptr
;
4996 register struct file_name_map
*map
;
4997 register char *from
;
5001 if (! searchptr
->got_name_map
)
5003 searchptr
->name_map
= read_name_map (searchptr
->fname
);
5004 searchptr
->got_name_map
= 1;
5007 /* Check the mapping for the directory we are using. */
5008 from
= filename
+ strlen (searchptr
->fname
);
5009 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5010 if (! strcmp (map
->map_from
, from
))
5014 from
= base_name (filename
);
5016 if (from
!= filename
|| !searchptr
)
5018 /* Try to find a mapping file for the particular directory we are
5019 looking in. Thus #include <sys/types.h> will look up sys/types.h
5020 in /usr/include/header.gcc and look up types.h in
5021 /usr/include/sys/header.gcc. */
5023 char *dir
= (char *) alloca (from
- filename
+ 1);
5024 bcopy (filename
, dir
, from
- filename
);
5025 dir
[from
- filename
] = '\0';
5027 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
5028 if (! strcmp (map
->map_from
, from
))
5035 /* Insert INC into the include file table, hashed by device and inode number.
5036 If a file with different name but same dev+ino was already in the table,
5037 return 1 and set INC's control macro to the already-known macro. */
5040 lookup_ino_include (inc
)
5041 struct include_file
*inc
;
5043 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
5044 % INCLUDE_HASHSIZE
);
5045 struct include_file
*i
= include_ino_hashtab
[hash
];
5047 include_ino_hashtab
[hash
] = inc
;
5049 for (; i
; i
= i
->next_ino
)
5050 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
5051 && inc
->st
.st_dev
== i
->st
.st_dev
) {
5052 inc
->control_macro
= i
->control_macro
;
5059 /* Process file descriptor F, which corresponds to include file INC,
5061 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5062 "system" include directories (as decided by the `is_system_include'
5064 DIRPTR is the link in the dir path through which this file was found,
5065 or 0 if the file name was absolute. */
5068 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5070 struct include_file
*inc
;
5072 int system_header_p
;
5073 struct file_name_list
*dirptr
;
5075 char *fname
= inc
->fname
;
5077 FILE_BUF
*fp
; /* For input stack frame */
5078 int missing_newline
= 0;
5080 CHECK_DEPTH (return;);
5082 fp
= &instack
[indepth
+ 1];
5083 bzero ((char *) fp
, sizeof (FILE_BUF
));
5084 fp
->nominal_fname
= fp
->fname
= fname
;
5088 fp
->if_stack
= if_stack
;
5089 fp
->system_header_p
= system_header_p
;
5092 if (S_ISREG (inc
->st
.st_mode
)) {
5093 fp
->buf
= (U_CHAR
*) xmalloc (inc
->st
.st_size
+ 2);
5096 /* Read the file contents, knowing that inc->st.st_size is an upper bound
5097 on the number of bytes we can read. */
5098 fp
->length
= safe_read (f
, (char *) fp
->buf
, inc
->st
.st_size
);
5099 if (fp
->length
< 0) goto nope
;
5101 else if (S_ISDIR (inc
->st
.st_mode
)) {
5102 error ("directory `%s' specified in #include", fname
);
5106 /* Cannot count its file size before reading.
5107 First read the entire file into heap and
5108 copy them into buffer on stack. */
5113 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5116 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5118 goto nope
; /* error! */
5120 if (st_size
!= bsize
)
5121 break; /* End of file */
5123 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5126 fp
->length
= st_size
;
5129 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5130 /* Backslash-newline at end is not good enough. */
5131 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5132 fp
->buf
[fp
->length
++] = '\n';
5133 missing_newline
= 1;
5135 fp
->buf
[fp
->length
] = '\0';
5137 /* Close descriptor now, so nesting does not use lots of descriptors. */
5140 /* Must do this before calling trigraph_pcp, so that the correct file name
5141 will be printed in warning messages. */
5144 input_file_stack_tick
++;
5149 output_line_directive (fp
, op
, 0, enter_file
);
5152 if (missing_newline
)
5155 if (pedantic
&& missing_newline
)
5156 pedwarn ("file does not end in newline");
5159 input_file_stack_tick
++;
5160 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5166 perror_with_name (fname
);
5171 /* Record that inclusion of the include file INC
5172 should be controlled by the macro named MACRO_NAME.
5173 This means that trying to include the file again
5174 will do something if that macro is defined. */
5177 record_control_macro (inc
, macro_name
)
5178 struct include_file
*inc
;
5181 if (!inc
->control_macro
|| inc
->control_macro
[0])
5182 inc
->control_macro
= macro_name
;
5185 /* Load the specified precompiled header into core, and verify its
5186 preconditions. PCF indicates the file descriptor to read, which must
5187 be a regular file. *ST is its file status.
5188 FNAME indicates the file name of the original header.
5189 *LIMIT will be set to an address one past the end of the file.
5190 If the preconditions of the file are not satisfied, the buffer is
5191 freed and we return 0. If the preconditions are satisfied, return
5192 the address of the buffer following the preconditions. The buffer, in
5193 this case, should never be freed because various pieces of it will
5194 be referred to until all precompiled strings are output at the end of
5198 check_precompiled (pcf
, st
, fname
, limit
)
5211 if (S_ISREG (st
->st_mode
))
5213 buf
= xmalloc (st
->st_size
+ 2);
5214 length
= safe_read (pcf
, buf
, st
->st_size
);
5221 if (length
> 0 && buf
[length
-1] != '\n')
5222 buf
[length
++] = '\n';
5225 *limit
= buf
+ length
;
5227 /* File is in core. Check the preconditions. */
5228 if (!check_preconditions (buf
))
5230 for (cp
= buf
; *cp
; cp
++)
5233 fprintf (stderr
, "Using preinclude %s\n", fname
);
5239 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5245 /* PREC (null terminated) points to the preconditions of a
5246 precompiled header. These are a series of #define and #undef
5247 lines which must match the current contents of the hash
5251 check_preconditions (prec
)
5258 lineend
= index (prec
, '\n');
5260 if (*prec
++ != '#') {
5261 error ("Bad format encountered while reading precompiled file");
5264 if (!strncmp (prec
, "define", 6)) {
5268 mdef
= create_definition ((U_CHAR
*) prec
, (U_CHAR
*) lineend
, NULL_PTR
);
5273 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5274 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5275 || (hp
->type
== T_MACRO
5276 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5277 && (mdef
.defn
->length
!= 2
5278 || mdef
.defn
->expansion
[0] != '\n'
5279 || mdef
.defn
->expansion
[1] != ' ')))
5281 } else if (!strncmp (prec
, "undef", 5)) {
5286 while (is_hor_space
[(U_CHAR
) *prec
])
5289 while (is_idchar
[(U_CHAR
) *prec
])
5293 if (lookup ((U_CHAR
*) name
, len
, -1))
5296 error ("Bad format encountered while reading precompiled file");
5301 /* They all passed successfully */
5305 /* Process the main body of a precompiled file. BUF points to the
5306 string section of the file, following the preconditions. LIMIT is one
5307 character past the end. NAME is the name of the file being read
5308 in. OP is the main output buffer. */
5311 pcfinclude (buf
, limit
, name
, op
)
5312 U_CHAR
*buf
, *limit
, *name
;
5319 /* First in the file comes 4 bytes indicating the number of strings, */
5320 /* in network byte order. (MSB first). */
5322 nstrings
= (nstrings
<< 8) | *cp
++;
5323 nstrings
= (nstrings
<< 8) | *cp
++;
5324 nstrings
= (nstrings
<< 8) | *cp
++;
5326 /* Looping over each string... */
5327 while (nstrings
--) {
5328 U_CHAR
*string_start
;
5329 U_CHAR
*endofthiskey
;
5333 /* Each string starts with a STRINGDEF structure (str), followed */
5334 /* by the text of the string (string_start) */
5336 /* First skip to a longword boundary */
5337 /* ??? Why a 4-byte boundary? On all machines? */
5338 /* NOTE: This works correctly even if size_t
5339 is narrower than a pointer.
5340 Do not try risky measures here to get another type to use!
5341 Do not include stddef.h--it will fail! */
5342 if ((size_t) cp
& 3)
5343 cp
+= 4 - ((size_t) cp
& 3);
5345 /* Now get the string. */
5346 str
= (STRINGDEF
*) (GENERIC_PTR
) cp
;
5347 string_start
= cp
+= sizeof (STRINGDEF
);
5349 for (; *cp
; cp
++) /* skip the string */
5352 /* We need to macro expand the string here to ensure that the
5353 proper definition environment is in place. If it were only
5354 expanded when we find out it is needed, macros necessary for
5355 its proper expansion might have had their definitions changed. */
5356 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5357 /* Lineno is already set in the precompiled file */
5358 str
->contents
= tmpbuf
.buf
;
5359 str
->len
= tmpbuf
.length
;
5361 str
->filename
= name
;
5362 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5365 *stringlist_tailp
= str
;
5366 stringlist_tailp
= &str
->chain
;
5368 /* Next comes a fourbyte number indicating the number of keys
5371 nkeys
= (nkeys
<< 8) | *cp
++;
5372 nkeys
= (nkeys
<< 8) | *cp
++;
5373 nkeys
= (nkeys
<< 8) | *cp
++;
5375 /* If this number is -1, then the string is mandatory. */
5379 /* Otherwise, for each key, */
5380 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5381 KEYDEF
*kp
= (KEYDEF
*) (GENERIC_PTR
) cp
;
5384 /* It starts with a KEYDEF structure */
5385 cp
+= sizeof (KEYDEF
);
5387 /* Find the end of the key. At the end of this for loop we
5388 advance CP to the start of the next key using this variable. */
5389 endofthiskey
= cp
+ strlen ((char *) cp
);
5392 /* Expand the key, and enter it into the hash table. */
5393 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5394 tmpbuf
.bufp
= tmpbuf
.buf
;
5396 while (is_hor_space
[*tmpbuf
.bufp
])
5398 if (!is_idstart
[*tmpbuf
.bufp
]
5399 || tmpbuf
.bufp
== tmpbuf
.buf
+ tmpbuf
.length
) {
5404 hp
= lookup (tmpbuf
.bufp
, -1, -1);
5407 install (tmpbuf
.bufp
, -1, T_PCSTRING
, (char *) kp
, -1);
5409 else if (hp
->type
== T_PCSTRING
) {
5410 kp
->chain
= hp
->value
.keydef
;
5411 hp
->value
.keydef
= kp
;
5417 /* This output_line_directive serves to switch us back to the current
5418 input file in case some of these strings get output (which will
5419 result in line directives for the header file being output). */
5420 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5423 /* Called from rescan when it hits a key for strings. Mark them all
5424 used and clean up. */
5432 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5433 kp
->str
->writeflag
= 1;
5437 /* Write the output, interspersing precompiled strings in their
5438 appropriate places. */
5443 STRINGDEF
*next_string
;
5444 U_CHAR
*cur_buf_loc
;
5445 int line_directive_len
= 80;
5446 char *line_directive
= xmalloc (line_directive_len
);
5449 /* In each run through the loop, either cur_buf_loc ==
5450 next_string_loc, in which case we print a series of strings, or
5451 it is less than next_string_loc, in which case we write some of
5453 cur_buf_loc
= outbuf
.buf
;
5454 next_string
= stringlist
;
5456 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5458 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5459 if (next_string
->writeflag
) {
5460 len
= 4 * strlen ((char *) next_string
->filename
) + 32;
5461 while (len
> line_directive_len
)
5462 line_directive
= xrealloc (line_directive
,
5463 line_directive_len
*= 2);
5464 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5465 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5466 (char *) next_string
->filename
),
5468 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5469 safe_write (fileno (stdout
),
5470 (char *) next_string
->contents
, next_string
->len
);
5472 next_string
= next_string
->chain
;
5476 ? (next_string
->output_mark
5477 - (cur_buf_loc
- outbuf
.buf
))
5478 : outbuf
.bufp
- cur_buf_loc
);
5480 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5484 free (line_directive
);
5487 /* Pass a directive through to the output file.
5488 BUF points to the contents of the directive, as a contiguous string.
5489 LIMIT points to the first character past the end of the directive.
5490 KEYWORD is the keyword-table entry for the directive. */
5493 pass_thru_directive (buf
, limit
, op
, keyword
)
5494 U_CHAR
*buf
, *limit
;
5496 struct directive
*keyword
;
5498 register unsigned keyword_length
= keyword
->length
;
5500 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5502 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5503 op
->bufp
+= keyword_length
;
5504 if (limit
!= buf
&& buf
[0] != ' ')
5506 bcopy ((char *) buf
, (char *) op
->bufp
, limit
- buf
);
5507 op
->bufp
+= (limit
- buf
);
5510 /* Count the line we have just made in the output,
5511 to get in sync properly. */
5516 /* The arglist structure is built by do_define to tell
5517 collect_definition where the argument names begin. That
5518 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5519 would contain pointers to the strings x, y, and z.
5520 Collect_definition would then build a DEFINITION node,
5521 with reflist nodes pointing to the places x, y, and z had
5522 appeared. So the arglist is just convenience data passed
5523 between these two routines. It is not kept around after
5524 the current #define has been processed and entered into the
5528 struct arglist
*next
;
5535 /* Create a DEFINITION node from a #define directive. Arguments are
5536 as for do_define. */
5539 create_definition (buf
, limit
, op
)
5540 U_CHAR
*buf
, *limit
;
5543 U_CHAR
*bp
; /* temp ptr into input buffer */
5544 U_CHAR
*symname
; /* remember where symbol name starts */
5545 int sym_length
; /* and how long it is */
5546 int line
= instack
[indepth
].lineno
;
5547 char *file
= instack
[indepth
].nominal_fname
;
5551 int arglengths
= 0; /* Accumulate lengths of arg names
5552 plus number of args. */
5557 while (is_hor_space
[*bp
])
5560 symname
= bp
; /* remember where it starts */
5561 sym_length
= check_macro_name (bp
, "macro");
5564 /* Lossage will occur if identifiers or control keywords are broken
5565 across lines using backslash. This is not the right place to take
5569 struct arglist
*arg_ptrs
= NULL
;
5572 bp
++; /* skip '(' */
5573 SKIP_WHITE_SPACE (bp
);
5575 /* Loop over macro argument names. */
5576 while (*bp
!= ')') {
5577 struct arglist
*temp
;
5579 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5581 temp
->next
= arg_ptrs
;
5582 temp
->argno
= argno
++;
5583 temp
->rest_args
= 0;
5587 pedwarn ("another parameter follows `%s'",
5590 if (!is_idstart
[*bp
])
5591 pedwarn ("invalid character in macro parameter name");
5593 /* Find the end of the arg name. */
5594 while (is_idchar
[*bp
]) {
5596 /* do we have a "special" rest-args extension here? */
5597 if (limit
- bp
> REST_EXTENSION_LENGTH
5598 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5600 temp
->rest_args
= 1;
5604 temp
->length
= bp
- temp
->name
;
5606 bp
+= REST_EXTENSION_LENGTH
;
5607 arglengths
+= temp
->length
+ 2;
5608 SKIP_WHITE_SPACE (bp
);
5609 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5610 error ("badly punctuated parameter list in `#define'");
5615 SKIP_WHITE_SPACE (bp
);
5616 /* A comma at this point can only be followed by an identifier. */
5617 if (!is_idstart
[*bp
]) {
5618 error ("badly punctuated parameter list in `#define'");
5623 error ("unterminated parameter list in `#define'");
5627 struct arglist
*otemp
;
5629 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5630 if (temp
->length
== otemp
->length
5631 && bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
5632 error ("duplicate argument name `%.*s' in `#define'",
5633 temp
->length
, temp
->name
);
5639 ++bp
; /* skip paren */
5640 SKIP_WHITE_SPACE (bp
);
5641 /* now everything from bp before limit is the definition. */
5642 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5643 defn
->rest_args
= rest_args
;
5645 /* Now set defn->args.argnames to the result of concatenating
5646 the argument names in reverse order
5647 with comma-space between them. */
5648 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5650 struct arglist
*temp
;
5652 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5653 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5655 if (temp
->next
!= 0) {
5656 defn
->args
.argnames
[i
++] = ',';
5657 defn
->args
.argnames
[i
++] = ' ';
5660 defn
->args
.argnames
[i
] = 0;
5663 /* Simple expansion or empty definition. */
5667 if (is_hor_space
[*bp
]) {
5669 SKIP_WHITE_SPACE (bp
);
5670 } else if (sym_length
) {
5672 case '!': case '"': case '#': case '%': case '&': case '\'':
5673 case ')': case '*': case '+': case ',': case '-': case '.':
5674 case '/': case ':': case ';': case '<': case '=': case '>':
5675 case '?': case '[': case '\\': case ']': case '^': case '{':
5676 case '|': case '}': case '~':
5677 warning ("missing white space after `#define %.*s'",
5678 sym_length
, symname
);
5682 pedwarn ("missing white space after `#define %.*s'",
5683 sym_length
, symname
);
5688 /* Now everything from bp before limit is the definition. */
5689 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5690 defn
->args
.argnames
= (U_CHAR
*) "";
5696 /* OP is null if this is a predefinition */
5697 defn
->predefined
= !op
;
5699 mdef
.symnam
= symname
;
5700 mdef
.symlen
= sym_length
;
5709 /* Process a #define directive.
5710 BUF points to the contents of the #define directive, as a contiguous string.
5711 LIMIT points to the first character past the end of the definition.
5712 KEYWORD is the keyword-table entry for #define. */
5715 do_define (buf
, limit
, op
, keyword
)
5716 U_CHAR
*buf
, *limit
;
5718 struct directive
*keyword
;
5723 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5724 if (pcp_outfile
&& op
)
5725 pass_thru_directive (buf
, limit
, op
, keyword
);
5727 mdef
= create_definition (buf
, limit
, op
);
5731 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
5735 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
5737 /* Redefining a precompiled key is ok. */
5738 if (hp
->type
== T_PCSTRING
)
5740 /* Redefining a macro is ok if the definitions are the same. */
5741 else if (hp
->type
== T_MACRO
)
5742 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
5743 /* Redefining a constant is ok with -D. */
5744 else if (hp
->type
== T_CONST
)
5745 ok
= ! done_initializing
;
5746 /* Print the warning if it's not ok. */
5748 /* If we are passing through #define and #undef directives, do
5749 that for this re-definition now. */
5750 if (debug_output
&& op
)
5751 pass_thru_directive (buf
, limit
, op
, keyword
);
5753 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
5754 if (hp
->type
== T_MACRO
)
5755 pedwarn_with_file_and_line (hp
->value
.defn
->file
, hp
->value
.defn
->line
,
5756 "this is the location of the previous definition");
5758 /* Replace the old definition. */
5760 hp
->value
.defn
= mdef
.defn
;
5762 /* If we are passing through #define and #undef directives, do
5763 that for this new definition now. */
5764 if (debug_output
&& op
)
5765 pass_thru_directive (buf
, limit
, op
, keyword
);
5766 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
,
5767 (char *) mdef
.defn
, hashcode
);
5778 /* Check a purported macro name SYMNAME, and yield its length.
5779 USAGE is the kind of name this is intended for. */
5782 check_macro_name (symname
, usage
)
5789 for (p
= symname
; is_idchar
[*p
]; p
++)
5791 sym_length
= p
- symname
;
5793 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
5794 error ("invalid %s name", usage
);
5795 else if (!is_idstart
[*symname
]
5796 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
5797 error ("invalid %s name `%.*s'", usage
, sym_length
, symname
);
5801 /* Return zero if two DEFINITIONs are isomorphic. */
5804 compare_defs (d1
, d2
)
5805 DEFINITION
*d1
, *d2
;
5807 register struct reflist
*a1
, *a2
;
5808 register U_CHAR
*p1
= d1
->expansion
;
5809 register U_CHAR
*p2
= d2
->expansion
;
5812 if (d1
->nargs
!= d2
->nargs
)
5815 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
5817 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
5818 a1
= a1
->next
, a2
= a2
->next
) {
5819 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
5820 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
5821 || a1
->argno
!= a2
->argno
5822 || a1
->stringify
!= a2
->stringify
5823 || a1
->raw_before
!= a2
->raw_before
5824 || a1
->raw_after
!= a2
->raw_after
)
5832 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
5833 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
5838 /* Return 1 if two parts of two macro definitions are effectively different.
5839 One of the parts starts at BEG1 and has LEN1 chars;
5840 the other has LEN2 chars at BEG2.
5841 Any sequence of whitespace matches any other sequence of whitespace.
5842 FIRST means these parts are the first of a macro definition;
5843 so ignore leading whitespace entirely.
5844 LAST means these parts are the last of a macro definition;
5845 so ignore trailing whitespace entirely. */
5848 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
5850 U_CHAR
*beg1
, *beg2
;
5854 register U_CHAR
*end1
= beg1
+ len1
;
5855 register U_CHAR
*end2
= beg2
+ len2
;
5857 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5858 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5861 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
5862 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
5864 while (beg1
!= end1
&& beg2
!= end2
) {
5865 if (is_space
[*beg1
] && is_space
[*beg2
]) {
5866 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5867 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5868 } else if (*beg1
== *beg2
) {
5872 return (beg1
!= end1
) || (beg2
!= end2
);
5875 /* Read a replacement list for a macro with parameters.
5876 Build the DEFINITION structure.
5877 Reads characters of text starting at BUF until END.
5878 ARGLIST specifies the formal parameters to look for
5879 in the text of the definition; NARGS is the number of args
5880 in that list, or -1 for a macro name that wants no argument list.
5881 MACRONAME is the macro name itself (so we can avoid recursive expansion)
5882 and NAMELEN is its length in characters.
5884 Note that comments, backslash-newlines, and leading white space
5885 have already been deleted from the argument. */
5887 /* If there is no trailing whitespace, a Newline Space is added at the end
5888 to prevent concatenation that would be contrary to the standard. */
5891 collect_expansion (buf
, end
, nargs
, arglist
)
5894 struct arglist
*arglist
;
5897 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
5898 struct reflist
*endpat
= NULL
;
5899 /* Pointer to first nonspace after last ## seen. */
5901 /* Pointer to first nonspace after last single-# seen. */
5902 U_CHAR
*stringify
= 0;
5903 /* How those tokens were spelled. */
5904 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
5905 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
5907 int expected_delimiter
= '\0';
5909 /* Scan thru the replacement list, ignoring comments and quoted
5910 strings, picking up on the macro calls. It does a linear search
5911 thru the arg list on every potential symbol. Profiling might say
5912 that something smarter should happen. */
5917 /* Find the beginning of the trailing whitespace. */
5920 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
5922 /* Allocate space for the text in the macro definition.
5923 Each input char may or may not need 1 byte,
5924 so this is an upper bound.
5925 The extra 3 are for invented trailing newline-marker and final null. */
5926 maxsize
= (sizeof (DEFINITION
)
5928 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
5930 defn
->nargs
= nargs
;
5931 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
5936 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
5937 error ("`##' at start of macro definition");
5938 p
+= p
[0] == '#' ? 2 : 4;
5941 /* Process the main body of the definition. */
5943 int skipped_arg
= 0;
5944 register U_CHAR c
= *p
++;
5952 if (expected_delimiter
!= '\0') {
5953 if (c
== expected_delimiter
)
5954 expected_delimiter
= '\0';
5956 expected_delimiter
= c
;
5960 if (p
< limit
&& expected_delimiter
) {
5961 /* In a string, backslash goes through
5962 and makes next char ordinary. */
5968 if (!expected_delimiter
&& *p
== ':') {
5969 /* %: is not a digraph if preceded by an odd number of '<'s. */
5971 while (buf
< p0
&& p0
[-1] == '<')
5974 /* Treat %:%: as ## and %: as #. */
5975 if (p
[1] == '%' && p
[2] == ':') {
5977 goto sharp_sharp_token
;
5988 /* # is ordinary inside a string. */
5989 if (expected_delimiter
)
5993 /* ##: concatenate preceding and following tokens. */
5994 /* Take out the first #, discard preceding whitespace. */
5996 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
5998 /* Skip the second #. */
6000 concat_sharp_token_type
= c
;
6001 if (is_hor_space
[*p
]) {
6002 concat_sharp_token_type
= c
+ 1;
6004 SKIP_WHITE_SPACE (p
);
6008 error ("`##' at end of macro definition");
6009 } else if (nargs
>= 0) {
6010 /* Single #: stringify following argument ref.
6011 Don't leave the # in the expansion. */
6014 stringify_sharp_token_type
= c
;
6015 if (is_hor_space
[*p
]) {
6016 stringify_sharp_token_type
= c
+ 1;
6018 SKIP_WHITE_SPACE (p
);
6020 if (! is_idstart
[*p
] || nargs
== 0
6021 || (*p
== 'L' && (p
[1] == '\'' || p
[1] == '"')))
6022 error ("`#' operator is not followed by a macro argument name");
6029 /* In -traditional mode, recognize arguments inside strings and
6030 and character constants, and ignore special properties of #.
6031 Arguments inside strings are considered "stringified", but no
6032 extra quote marks are supplied. */
6036 if (expected_delimiter
!= '\0') {
6037 if (c
== expected_delimiter
)
6038 expected_delimiter
= '\0';
6040 expected_delimiter
= c
;
6044 /* Backslash quotes delimiters and itself, but not macro args. */
6045 if (expected_delimiter
!= 0 && p
< limit
6046 && (*p
== expected_delimiter
|| *p
== '\\')) {
6053 if (expected_delimiter
!= '\0') /* No comments inside strings. */
6056 /* If we find a comment that wasn't removed by handle_directive,
6057 this must be -traditional. So replace the comment with
6060 while (++p
< limit
) {
6061 if (p
[0] == '*' && p
[1] == '/') {
6067 /* Mark this as a concatenation-point, as if it had been ##. */
6075 /* Handle the start of a symbol. */
6076 if (is_idchar
[c
] && nargs
> 0) {
6077 U_CHAR
*id_beg
= p
- 1;
6081 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6082 id_len
= p
- id_beg
;
6085 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
6086 register struct arglist
*arg
;
6088 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6089 struct reflist
*tpat
;
6091 if (arg
->name
[0] == c
6092 && arg
->length
== id_len
6093 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6094 enum sharp_token_type tpat_stringify
;
6095 if (expected_delimiter
) {
6096 if (warn_stringify
) {
6098 warning ("macro argument `%.*s' is stringified.",
6101 warning ("macro arg `%.*s' would be stringified with -traditional.",
6105 /* If ANSI, don't actually substitute inside a string. */
6108 tpat_stringify
= SHARP_TOKEN
;
6111 = (stringify
== id_beg
6112 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6114 /* make a pat node for this arg and append it to the end of
6116 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6119 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6120 tpat
->raw_after
= NO_SHARP_TOKEN
;
6121 tpat
->rest_args
= arg
->rest_args
;
6122 tpat
->stringify
= tpat_stringify
;
6125 defn
->pattern
= tpat
;
6127 endpat
->next
= tpat
;
6130 tpat
->argno
= arg
->argno
;
6131 tpat
->nchars
= exp_p
- lastp
;
6133 register U_CHAR
*p1
= p
;
6134 SKIP_WHITE_SPACE (p1
);
6137 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6138 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6140 lastp
= exp_p
; /* place to start copying from next time */
6147 /* If this was not a macro arg, copy it into the expansion. */
6148 if (! skipped_arg
) {
6149 register U_CHAR
*lim1
= p
;
6153 if (stringify
== id_beg
)
6154 error ("`#' operator should be followed by a macro argument name");
6159 if (!traditional
&& expected_delimiter
== 0) {
6160 /* If ANSI, put in a newline-space marker to prevent token pasting.
6161 But not if "inside a string" (which in ANSI mode happens only for
6169 defn
->length
= exp_p
- defn
->expansion
;
6171 /* Crash now if we overrun the allocated size. */
6172 if (defn
->length
+ 1 > maxsize
)
6176 /* This isn't worth the time it takes. */
6177 /* give back excess storage */
6178 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6185 do_assert (buf
, limit
, op
, keyword
)
6186 U_CHAR
*buf
, *limit
;
6188 struct directive
*keyword
;
6190 U_CHAR
*bp
; /* temp ptr into input buffer */
6191 U_CHAR
*symname
; /* remember where symbol name starts */
6192 int sym_length
; /* and how long it is */
6193 struct arglist
*tokens
= NULL
;
6195 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6196 pedwarn ("ANSI C does not allow `#assert'");
6200 while (is_hor_space
[*bp
])
6203 symname
= bp
; /* remember where it starts */
6204 sym_length
= check_macro_name (bp
, "assertion");
6206 /* #define doesn't do this, but we should. */
6207 SKIP_WHITE_SPACE (bp
);
6209 /* Lossage will occur if identifiers or control tokens are broken
6210 across lines using backslash. This is not the right place to take
6214 error ("missing token-sequence in `#assert'");
6221 bp
++; /* skip '(' */
6222 SKIP_WHITE_SPACE (bp
);
6224 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6228 error ("empty token-sequence in `#assert'");
6232 ++bp
; /* skip paren */
6233 SKIP_WHITE_SPACE (bp
);
6236 /* If this name isn't already an assertion name, make it one.
6237 Error if it was already in use in some other way. */
6240 ASSERTION_HASHNODE
*hp
;
6241 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6242 struct tokenlist_list
*value
6243 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6245 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6247 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6248 error ("`defined' redefined as assertion");
6249 hp
= assertion_install (symname
, sym_length
, hashcode
);
6252 /* Add the spec'd token-sequence to the list of such. */
6253 value
->tokens
= tokens
;
6254 value
->next
= hp
->value
;
6262 do_unassert (buf
, limit
, op
, keyword
)
6263 U_CHAR
*buf
, *limit
;
6265 struct directive
*keyword
;
6267 U_CHAR
*bp
; /* temp ptr into input buffer */
6268 U_CHAR
*symname
; /* remember where symbol name starts */
6269 int sym_length
; /* and how long it is */
6271 struct arglist
*tokens
= NULL
;
6272 int tokens_specified
= 0;
6274 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6275 pedwarn ("ANSI C does not allow `#unassert'");
6279 while (is_hor_space
[*bp
])
6282 symname
= bp
; /* remember where it starts */
6283 sym_length
= check_macro_name (bp
, "assertion");
6285 /* #define doesn't do this, but we should. */
6286 SKIP_WHITE_SPACE (bp
);
6288 /* Lossage will occur if identifiers or control tokens are broken
6289 across lines using backslash. This is not the right place to take
6295 bp
++; /* skip '(' */
6296 SKIP_WHITE_SPACE (bp
);
6298 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6302 error ("empty token list in `#unassert'");
6306 tokens_specified
= 1;
6308 ++bp
; /* skip paren */
6309 SKIP_WHITE_SPACE (bp
);
6313 ASSERTION_HASHNODE
*hp
;
6314 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6315 struct tokenlist_list
*tail
, *prev
;
6317 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6321 /* If no token list was specified, then eliminate this assertion
6323 if (! tokens_specified
) {
6324 struct tokenlist_list
*next
;
6325 for (tail
= hp
->value
; tail
; tail
= next
) {
6327 free_token_list (tail
->tokens
);
6330 delete_assertion (hp
);
6332 /* If a list of tokens was given, then delete any matching list. */
6337 struct tokenlist_list
*next
= tail
->next
;
6338 if (compare_token_lists (tail
->tokens
, tokens
)) {
6342 hp
->value
= tail
->next
;
6343 free_token_list (tail
->tokens
);
6356 /* Test whether there is an assertion named NAME
6357 and optionally whether it has an asserted token list TOKENS.
6358 NAME is not null terminated; its length is SYM_LENGTH.
6359 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6362 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6365 int tokens_specified
;
6366 struct arglist
*tokens
;
6368 ASSERTION_HASHNODE
*hp
;
6369 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6371 if (pedantic
&& !instack
[indepth
].system_header_p
)
6372 pedwarn ("ANSI C does not allow testing assertions");
6374 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6376 /* It is not an assertion; just return false. */
6379 /* If no token list was specified, then value is 1. */
6380 if (! tokens_specified
)
6384 struct tokenlist_list
*tail
;
6388 /* If a list of tokens was given,
6389 then succeed if the assertion records a matching list. */
6392 if (compare_token_lists (tail
->tokens
, tokens
))
6397 /* Fail if the assertion has no matching list. */
6402 /* Compare two lists of tokens for equality including order of tokens. */
6405 compare_token_lists (l1
, l2
)
6406 struct arglist
*l1
, *l2
;
6409 if (l1
->length
!= l2
->length
)
6411 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6417 /* Succeed if both lists end at the same time. */
6421 /* Read a space-separated list of tokens ending in a close parenthesis.
6422 Return a list of strings, in the order they were written.
6423 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6424 Parse the text starting at *BPP, and update *BPP.
6425 Don't parse beyond LIMIT. */
6427 static struct arglist
*
6428 read_token_list (bpp
, limit
, error_flag
)
6433 struct arglist
*token_ptrs
= 0;
6439 /* Loop over the assertion value tokens. */
6441 struct arglist
*temp
;
6445 /* Find the end of the token. */
6449 } else if (*bp
== ')') {
6454 } else if (*bp
== '"' || *bp
== '\'')
6455 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6457 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6458 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6461 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6462 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6463 bcopy ((char *) beg
, (char *) temp
->name
, bp
- beg
);
6464 temp
->name
[bp
- beg
] = 0;
6465 temp
->next
= token_ptrs
;
6467 temp
->length
= bp
- beg
;
6469 SKIP_WHITE_SPACE (bp
);
6472 error ("unterminated token sequence in `#assert' or `#unassert'");
6479 /* We accumulated the names in reverse order.
6480 Now reverse them to get the proper order. */
6482 register struct arglist
*prev
= 0, *this, *next
;
6483 for (this = token_ptrs
; this; this = next
) {
6493 free_token_list (tokens
)
6494 struct arglist
*tokens
;
6497 struct arglist
*next
= tokens
->next
;
6498 free (tokens
->name
);
6504 /* Install a name in the assertion hash table.
6506 If LEN is >= 0, it is the length of the name.
6507 Otherwise, compute the length by scanning the entire name.
6509 If HASH is >= 0, it is the precomputed hash code.
6510 Otherwise, compute the hash code. */
6512 static ASSERTION_HASHNODE
*
6513 assertion_install (name
, len
, hash
)
6518 register ASSERTION_HASHNODE
*hp
;
6519 register int i
, bucket
;
6520 register U_CHAR
*p
, *q
;
6522 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6523 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6525 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6526 hp
->next
= assertion_hashtab
[bucket
];
6527 assertion_hashtab
[bucket
] = hp
;
6529 if (hp
->next
!= NULL
)
6530 hp
->next
->prev
= hp
;
6533 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6536 for (i
= 0; i
< len
; i
++)
6542 /* Find the most recent hash node for name name (ending with first
6543 non-identifier char) installed by install
6545 If LEN is >= 0, it is the length of the name.
6546 Otherwise, compute the length by scanning the entire name.
6548 If HASH is >= 0, it is the precomputed hash code.
6549 Otherwise, compute the hash code. */
6551 static ASSERTION_HASHNODE
*
6552 assertion_lookup (name
, len
, hash
)
6557 register ASSERTION_HASHNODE
*bucket
;
6559 bucket
= assertion_hashtab
[hash
];
6561 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6563 bucket
= bucket
->next
;
6569 delete_assertion (hp
)
6570 ASSERTION_HASHNODE
*hp
;
6573 if (hp
->prev
!= NULL
)
6574 hp
->prev
->next
= hp
->next
;
6575 if (hp
->next
!= NULL
)
6576 hp
->next
->prev
= hp
->prev
;
6578 /* Make sure that the bucket chain header that the deleted guy was
6579 on points to the right thing afterwards. */
6580 if (hp
== *hp
->bucket_hdr
)
6581 *hp
->bucket_hdr
= hp
->next
;
6587 * interpret #line directive. Remembers previously seen fnames
6588 * in its very own hash table.
6590 #define FNAME_HASHSIZE 37
6593 do_line (buf
, limit
, op
, keyword
)
6594 U_CHAR
*buf
, *limit
;
6596 struct directive
*keyword
;
6598 register U_CHAR
*bp
;
6599 FILE_BUF
*ip
= &instack
[indepth
];
6602 enum file_change_code file_change
= same_file
;
6604 /* Expand any macros. */
6605 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6607 /* Point to macroexpanded line, which is null-terminated now. */
6609 SKIP_WHITE_SPACE (bp
);
6611 if (!isdigit (*bp
)) {
6612 error ("invalid format `#line' directive");
6616 /* The Newline at the end of this line remains to be processed.
6617 To put the next line at the specified line number,
6618 we must store a line number now that is one less. */
6619 new_lineno
= atoi ((char *) bp
) - 1;
6621 /* NEW_LINENO is one less than the actual line number here. */
6622 if (pedantic
&& new_lineno
< 0)
6623 pedwarn ("line number out of range in `#line' directive");
6625 /* skip over the line number. */
6626 while (isdigit (*bp
))
6629 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6630 if (*bp
&& !is_space
[*bp
]) {
6631 error ("invalid format `#line' directive");
6636 SKIP_WHITE_SPACE (bp
);
6639 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6640 HASHNODE
*hp
, **hash_bucket
;
6646 /* Turn the file name, which is a character string literal,
6647 into a null-terminated string. Do this in place. */
6650 switch ((*p
++ = *bp
++)) {
6652 error ("invalid format `#line' directive");
6657 char *bpc
= (char *) bp
;
6658 HOST_WIDE_INT c
= parse_escape (&bpc
, (HOST_WIDE_INT
) (U_CHAR
) (-1));
6659 bp
= (U_CHAR
*) bpc
;
6672 fname_length
= p
- fname
;
6674 SKIP_WHITE_SPACE (bp
);
6677 pedwarn ("garbage at end of `#line' directive");
6679 file_change
= enter_file
;
6680 else if (*bp
== '2')
6681 file_change
= leave_file
;
6682 else if (*bp
== '3')
6683 ip
->system_header_p
= 1;
6684 else if (*bp
== '4')
6685 ip
->system_header_p
= 2;
6687 error ("invalid format `#line' directive");
6692 SKIP_WHITE_SPACE (bp
);
6694 ip
->system_header_p
= 1;
6696 SKIP_WHITE_SPACE (bp
);
6699 ip
->system_header_p
= 2;
6701 SKIP_WHITE_SPACE (bp
);
6704 error ("invalid format `#line' directive");
6709 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
6710 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
6711 if (hp
->length
== fname_length
&&
6712 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
6713 ip
->nominal_fname
= hp
->value
.cpval
;
6717 /* Didn't find it; cons up a new one. */
6718 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
6719 hp
->next
= *hash_bucket
;
6722 hp
->length
= fname_length
;
6723 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
6724 bcopy (fname
, hp
->value
.cpval
, fname_length
);
6727 error ("invalid format `#line' directive");
6731 ip
->lineno
= new_lineno
;
6732 output_line_directive (ip
, op
, 0, file_change
);
6733 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
6737 /* Remove the definition of a symbol from the symbol table.
6738 according to un*x /lib/cpp, it is not an error to undef
6739 something that has no definitions, so it isn't one here either. */
6742 do_undef (buf
, limit
, op
, keyword
)
6743 U_CHAR
*buf
, *limit
;
6745 struct directive
*keyword
;
6749 U_CHAR
*orig_buf
= buf
;
6751 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
6752 if (pcp_outfile
&& op
)
6753 pass_thru_directive (buf
, limit
, op
, keyword
);
6755 SKIP_WHITE_SPACE (buf
);
6756 sym_length
= check_macro_name (buf
, "macro");
6758 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
6759 /* If we are generating additional info for debugging (with -g) we
6760 need to pass through all effective #undef directives. */
6761 if (debug_output
&& op
)
6762 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
6763 if (hp
->type
!= T_MACRO
)
6764 warning ("undefining `%s'", hp
->name
);
6770 SKIP_WHITE_SPACE (buf
);
6772 pedwarn ("garbage after `#undef' directive");
6777 /* Report an error detected by the program we are processing.
6778 Use the text of the line in the error message.
6779 (We use error because it prints the filename & line#.) */
6782 do_error (buf
, limit
, op
, keyword
)
6783 U_CHAR
*buf
, *limit
;
6785 struct directive
*keyword
;
6787 int length
= limit
- buf
;
6788 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
6789 bcopy ((char *) buf
, (char *) copy
, length
);
6791 SKIP_WHITE_SPACE (copy
);
6792 error ("#error %s", copy
);
6796 /* Report a warning detected by the program we are processing.
6797 Use the text of the line in the warning message, then continue.
6798 (We use error because it prints the filename & line#.) */
6801 do_warning (buf
, limit
, op
, keyword
)
6802 U_CHAR
*buf
, *limit
;
6804 struct directive
*keyword
;
6806 int length
= limit
- buf
;
6807 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
6808 bcopy ((char *) buf
, (char *) copy
, length
);
6810 SKIP_WHITE_SPACE (copy
);
6811 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
6812 if -pedantic-errors is given, #warning should cause an error. */
6813 pedwarn ("#warning %s", copy
);
6817 /* Remember the name of the current file being read from so that we can
6818 avoid ever including it again. */
6825 for (i
= indepth
; i
>= 0; i
--)
6826 if (instack
[i
].inc
) {
6827 record_control_macro (instack
[i
].inc
, (U_CHAR
*) "");
6832 /* Report program identification. */
6835 do_ident (buf
, limit
, op
, keyword
)
6836 U_CHAR
*buf
, *limit
;
6838 struct directive
*keyword
;
6843 /* Allow #ident in system headers, since that's not user's fault. */
6844 if (pedantic
&& !instack
[indepth
].system_header_p
)
6845 pedwarn ("ANSI C does not allow `#ident'");
6847 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6849 len
= trybuf
.bufp
- buf
;
6851 /* Output expanded directive. */
6852 check_expand (op
, 7 + len
);
6853 bcopy ("#ident ", (char *) op
->bufp
, 7);
6855 bcopy ((char *) buf
, (char *) op
->bufp
, len
);
6862 /* #pragma and its argument line have already been copied to the output file.
6863 Just check for some recognized pragmas that need validation here. */
6866 do_pragma (buf
, limit
, op
, keyword
)
6867 U_CHAR
*buf
, *limit
;
6869 struct directive
*keyword
;
6871 SKIP_WHITE_SPACE (buf
);
6872 if (!strncmp ((char *) buf
, "once", 4)) {
6873 /* Allow #pragma once in system headers, since that's not the user's
6875 if (!instack
[indepth
].system_header_p
)
6876 warning ("`#pragma once' is obsolete");
6880 if (!strncmp ((char *) buf
, "implementation", 14)) {
6881 /* Be quiet about `#pragma implementation' for a file only if it hasn't
6882 been included yet. */
6885 U_CHAR
*p
= buf
+ 14, *fname
;
6886 SKIP_WHITE_SPACE (p
);
6891 if ((p
= (U_CHAR
*) index ((char *) fname
, '\"')))
6894 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
6895 struct include_file
*inc
;
6896 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
6897 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
6898 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
6908 /* This was a fun hack, but #pragma seems to start to be useful.
6909 By failing to recognize it, we pass it through unchanged to cc1. */
6911 /* The behavior of the #pragma directive is implementation defined.
6912 this implementation defines it as follows. */
6918 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
6921 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
6923 execl ("/usr/games/hack", "#pragma", 0);
6924 execl ("/usr/games/rogue", "#pragma", 0);
6925 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
6926 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
6928 fatal ("You are in a maze of twisty compiler features, all different");
6932 #ifdef SCCS_DIRECTIVE
6934 /* Just ignore #sccs, on systems where we define it at all. */
6937 do_sccs (buf
, limit
, op
, keyword
)
6938 U_CHAR
*buf
, *limit
;
6940 struct directive
*keyword
;
6943 pedwarn ("ANSI C does not allow `#sccs'");
6947 #endif /* defined (SCCS_DIRECTIVE) */
6949 /* Handle #if directive by
6950 1) inserting special `defined' keyword into the hash table
6951 that gets turned into 0 or 1 by special_symbol (thus,
6952 if the luser has a symbol called `defined' already, it won't
6953 work inside the #if directive)
6954 2) rescan the input into a temporary output buffer
6955 3) pass the output buffer to the yacc parser and collect a value
6956 4) clean up the mess left from steps 1 and 2.
6957 5) call conditional_skip to skip til the next #endif (etc.),
6958 or not, depending on the value from step 3. */
6961 do_if (buf
, limit
, op
, keyword
)
6962 U_CHAR
*buf
, *limit
;
6964 struct directive
*keyword
;
6966 HOST_WIDE_INT value
;
6967 FILE_BUF
*ip
= &instack
[indepth
];
6969 value
= eval_if_expression (buf
, limit
- buf
);
6970 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
6974 /* Handle a #elif directive by not changing if_stack either.
6975 see the comment above do_else. */
6978 do_elif (buf
, limit
, op
, keyword
)
6979 U_CHAR
*buf
, *limit
;
6981 struct directive
*keyword
;
6983 HOST_WIDE_INT value
;
6984 FILE_BUF
*ip
= &instack
[indepth
];
6986 if (if_stack
== instack
[indepth
].if_stack
) {
6987 error ("`#elif' not within a conditional");
6990 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
6991 error ("`#elif' after `#else'");
6992 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
6993 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
6994 && strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
6995 fprintf (stderr
, ", file %s", if_stack
->fname
);
6996 fprintf (stderr
, ")\n");
6998 if_stack
->type
= T_ELIF
;
7001 if (if_stack
->if_succeeded
)
7002 skip_if_group (ip
, 0, op
);
7004 value
= eval_if_expression (buf
, limit
- buf
);
7006 skip_if_group (ip
, 0, op
);
7008 ++if_stack
->if_succeeded
; /* continue processing input */
7009 output_line_directive (ip
, op
, 1, same_file
);
7015 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7016 result as a C expression and return the value as an int. */
7018 static HOST_WIDE_INT
7019 eval_if_expression (buf
, length
)
7024 HASHNODE
*save_defined
;
7025 HOST_WIDE_INT value
;
7027 save_defined
= install ((U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
7030 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
7032 delete_macro (save_defined
); /* clean up special symbol */
7034 temp_obuf
.buf
[temp_obuf
.length
] = '\n';
7035 value
= parse_c_expression ((char *) temp_obuf
.buf
);
7037 free (temp_obuf
.buf
);
7042 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7043 or don't skip to the #endif/#else/#elif depending on what directive
7044 is actually being processed. */
7047 do_xifdef (buf
, limit
, op
, keyword
)
7048 U_CHAR
*buf
, *limit
;
7050 struct directive
*keyword
;
7053 FILE_BUF
*ip
= &instack
[indepth
];
7055 int start_of_file
= 0;
7056 U_CHAR
*control_macro
= 0;
7058 /* Detect a #ifndef at start of file (not counting comments). */
7059 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7060 U_CHAR
*p
= ip
->buf
;
7061 while (p
!= directive_start
) {
7065 /* Make no special provision for backslash-newline here; this is
7066 slower if backslash-newlines are present, but it's correct,
7067 and it's not worth it to tune for the rare backslash-newline. */
7069 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7070 /* Skip this comment. */
7072 U_CHAR
*save_bufp
= ip
->bufp
;
7074 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7075 ip
->bufp
= save_bufp
;
7080 /* If we get here, this conditional is the beginning of the file. */
7085 /* Discard leading and trailing whitespace. */
7086 SKIP_WHITE_SPACE (buf
);
7087 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7089 /* Find the end of the identifier at the beginning. */
7090 for (end
= buf
; is_idchar
[*end
]; end
++);
7093 skip
= (keyword
->type
== T_IFDEF
);
7095 pedwarn (end
== limit
? "`#%s' with no argument"
7096 : "`#%s' argument starts with punctuation",
7101 if (! traditional
) {
7102 if (isdigit (buf
[0]))
7103 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7104 else if (end
!= limit
)
7105 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7108 hp
= lookup (buf
, end
-buf
, -1);
7111 /* Output a precondition for this macro. */
7113 && (hp
->type
== T_CONST
7114 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7115 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7118 fprintf (pcp_outfile
, "#undef ");
7119 while (is_idchar
[*cp
]) /* Ick! */
7120 fputc (*cp
++, pcp_outfile
);
7121 putc ('\n', pcp_outfile
);
7125 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7126 if (start_of_file
&& !skip
) {
7127 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7128 bcopy ((char *) buf
, (char *) control_macro
, end
- buf
);
7129 control_macro
[end
- buf
] = 0;
7133 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7137 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7138 If this is a #ifndef starting at the beginning of a file,
7139 CONTROL_MACRO is the macro name tested by the #ifndef.
7140 Otherwise, CONTROL_MACRO is 0. */
7143 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7146 enum node_type type
;
7147 U_CHAR
*control_macro
;
7150 IF_STACK_FRAME
*temp
;
7152 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7153 temp
->fname
= ip
->nominal_fname
;
7154 temp
->lineno
= ip
->lineno
;
7155 temp
->next
= if_stack
;
7156 temp
->control_macro
= control_macro
;
7159 if_stack
->type
= type
;
7162 skip_if_group (ip
, 0, op
);
7165 ++if_stack
->if_succeeded
;
7166 output_line_directive (ip
, &outbuf
, 1, same_file
);
7170 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7171 Leaves input ptr at the sharp sign found.
7172 If ANY is nonzero, return at next directive of any sort. */
7175 skip_if_group (ip
, any
, op
)
7180 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7181 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7182 struct directive
*kt
;
7183 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7184 U_CHAR
*beg_of_line
= bp
;
7185 register int ident_length
;
7186 U_CHAR
*ident
, *after_ident
;
7187 /* Save info about where the group starts. */
7188 U_CHAR
*beg_of_group
= bp
;
7189 int beg_lineno
= ip
->lineno
;
7191 if (output_conditionals
&& op
!= 0) {
7192 char *ptr
= "#failed\n";
7193 int len
= strlen (ptr
);
7195 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7200 check_expand (op
, len
);
7201 bcopy (ptr
, (char *) op
->bufp
, len
);
7204 output_line_directive (ip
, op
, 1, 0);
7209 case '/': /* possible comment */
7210 if (*bp
== '\\' && bp
[1] == '\n')
7213 || (cplusplus_comments
&& *bp
== '/')) {
7215 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7220 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7221 NULL_PTR
, NULL_PTR
);
7224 /* Char after backslash loses its special meaning. */
7227 ++ip
->lineno
; /* But do update the line-count. */
7236 if (beg_of_line
== 0 || traditional
)
7239 while (bp
[0] == '\\' && bp
[1] == '\n')
7245 /* # keyword: a # must be first nonblank char on the line */
7246 if (beg_of_line
== 0)
7250 /* Scan from start of line, skipping whitespace, comments
7251 and backslash-newlines, and see if we reach this #.
7252 If not, this # is not special. */
7254 /* If -traditional, require # to be at beginning of line. */
7257 if (is_hor_space
[*bp
])
7259 else if (*bp
== '\\' && bp
[1] == '\n')
7261 else if (*bp
== '/' && bp
[1] == '*') {
7263 while (!(*bp
== '*' && bp
[1] == '/'))
7267 /* There is no point in trying to deal with C++ // comments here,
7268 because if there is one, then this # must be part of the
7269 comment and we would never reach here. */
7273 if (bp
!= ip
->bufp
) {
7274 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7278 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7279 if (ip
->bufp
[0] == '%') {
7280 /* Skip past the ':' again. */
7281 while (*bp
== '\\') {
7288 /* Skip whitespace and \-newline. */
7290 if (is_hor_space
[*bp
])
7292 else if (*bp
== '\\' && bp
[1] == '\n')
7294 else if (*bp
== '/') {
7296 for (bp
+= 2; ; bp
++) {
7299 else if (*bp
== '*') {
7300 if (bp
[-1] == '/' && warn_comments
)
7301 warning ("`/*' within comment");
7307 } else if (bp
[1] == '/' && cplusplus_comments
) {
7308 for (bp
+= 2; ; bp
++) {
7313 warning ("multiline `//' comment");
7325 /* Now find end of directive name.
7326 If we encounter a backslash-newline, exchange it with any following
7327 symbol-constituents so that we end up with a contiguous name. */
7333 if (*bp
== '\\' && bp
[1] == '\n')
7334 name_newline_fix (bp
);
7340 ident_length
= bp
- cp
;
7344 /* A line of just `#' becomes blank. */
7346 if (ident_length
== 0 && *after_ident
== '\n') {
7350 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7352 while (is_idchar
[*p
]) {
7353 if (*p
< '0' || *p
> '9')
7357 /* Handle # followed by a line number. */
7358 if (p
!= ident
&& !is_idchar
[*p
]) {
7360 pedwarn ("`#' followed by integer");
7364 /* Avoid error for `###' and similar cases unless -pedantic. */
7366 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7368 if (pedantic
&& !lang_asm
)
7369 pedwarn ("invalid preprocessing directive");
7374 if (!lang_asm
&& pedantic
)
7375 pedwarn ("invalid preprocessing directive name");
7379 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7380 IF_STACK_FRAME
*temp
;
7381 if (ident_length
== kt
->length
7382 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7383 /* If we are asked to return on next directive, do so now. */
7391 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7392 temp
->next
= if_stack
;
7394 temp
->lineno
= ip
->lineno
;
7395 temp
->fname
= ip
->nominal_fname
;
7396 temp
->type
= kt
->type
;
7400 if (pedantic
&& if_stack
!= save_if_stack
)
7401 validate_else (bp
, endb
);
7403 if (if_stack
== instack
[indepth
].if_stack
) {
7404 error ("`#%s' not within a conditional", kt
->name
);
7407 else if (if_stack
== save_if_stack
)
7408 goto done
; /* found what we came for */
7410 if (kt
->type
!= T_ENDIF
) {
7411 if (if_stack
->type
== T_ELSE
)
7412 error ("`#else' or `#elif' after `#else'");
7413 if_stack
->type
= kt
->type
;
7418 if_stack
= if_stack
->next
;
7428 /* Don't let erroneous code go by. */
7429 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7430 pedwarn ("invalid preprocessing directive name");
7435 /* after this returns, rescan will exit because ip->bufp
7436 now points to the end of the buffer.
7437 rescan is responsible for the error message also. */
7440 if (output_conditionals
&& op
!= 0) {
7441 char *ptr
= "#endfailed\n";
7442 int len
= strlen (ptr
);
7444 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7449 check_expand (op
, beg_of_line
- beg_of_group
);
7450 bcopy ((char *) beg_of_group
, (char *) op
->bufp
,
7451 beg_of_line
- beg_of_group
);
7452 op
->bufp
+= beg_of_line
- beg_of_group
;
7453 op
->lineno
+= ip
->lineno
- beg_lineno
;
7454 check_expand (op
, len
);
7455 bcopy (ptr
, (char *) op
->bufp
, len
);
7461 /* Handle a #else directive. Do this by just continuing processing
7462 without changing if_stack ; this is so that the error message
7463 for missing #endif's etc. will point to the original #if. It
7464 is possible that something different would be better. */
7467 do_else (buf
, limit
, op
, keyword
)
7468 U_CHAR
*buf
, *limit
;
7470 struct directive
*keyword
;
7472 FILE_BUF
*ip
= &instack
[indepth
];
7475 SKIP_WHITE_SPACE (buf
);
7477 pedwarn ("text following `#else' violates ANSI standard");
7480 if (if_stack
== instack
[indepth
].if_stack
) {
7481 error ("`#else' not within a conditional");
7484 /* #ifndef can't have its special treatment for containing the whole file
7485 if it has a #else clause. */
7486 if_stack
->control_macro
= 0;
7488 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7489 error ("`#else' after `#else'");
7490 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7491 if (strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
7492 fprintf (stderr
, ", file %s", if_stack
->fname
);
7493 fprintf (stderr
, ")\n");
7495 if_stack
->type
= T_ELSE
;
7498 if (if_stack
->if_succeeded
)
7499 skip_if_group (ip
, 0, op
);
7501 ++if_stack
->if_succeeded
; /* continue processing input */
7502 output_line_directive (ip
, op
, 1, same_file
);
7507 /* Unstack after #endif directive. */
7510 do_endif (buf
, limit
, op
, keyword
)
7511 U_CHAR
*buf
, *limit
;
7513 struct directive
*keyword
;
7516 SKIP_WHITE_SPACE (buf
);
7518 pedwarn ("text following `#endif' violates ANSI standard");
7521 if (if_stack
== instack
[indepth
].if_stack
)
7522 error ("unbalanced `#endif'");
7524 IF_STACK_FRAME
*temp
= if_stack
;
7525 if_stack
= if_stack
->next
;
7526 if (temp
->control_macro
!= 0) {
7527 /* This #endif matched a #ifndef at the start of the file.
7528 See if it is at the end of the file. */
7529 FILE_BUF
*ip
= &instack
[indepth
];
7530 U_CHAR
*p
= ip
->bufp
;
7531 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7537 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7538 /* Skip this comment. */
7540 U_CHAR
*save_bufp
= ip
->bufp
;
7542 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7543 ip
->bufp
= save_bufp
;
7548 /* If we get here, this #endif ends a #ifndef
7549 that contains all of the file (aside from whitespace).
7550 Arrange not to include the file again
7551 if the macro that was tested is defined.
7553 Do not do this for the top-level file in a -include or any
7554 file in a -imacros. */
7556 && ! (indepth
== 1 && no_record_file
)
7557 && ! (no_record_file
&& no_output
))
7558 record_control_macro (ip
->inc
, temp
->control_macro
);
7562 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
7567 /* When an #else or #endif is found while skipping failed conditional,
7568 if -pedantic was specified, this is called to warn about text after
7569 the directive name. P points to the first char after the directive
7573 validate_else (p
, limit
)
7575 register U_CHAR
*limit
;
7577 /* Advance P over whitespace and comments. */
7579 while (*p
== '\\' && p
[1] == '\n')
7581 if (is_hor_space
[*p
])
7583 else if (*p
== '/') {
7584 while (p
[1] == '\\' && p
[2] == '\n')
7587 /* Don't bother warning about unterminated comments
7588 since that will happen later. Just be sure to exit. */
7589 for (p
+= 2; ; p
++) {
7593 while (p
[1] == '\\' && p
[2] == '\n')
7602 else if (cplusplus_comments
&& p
[1] == '/')
7608 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
7611 /* Skip a comment, assuming the input ptr immediately follows the
7612 initial slash-star. Bump *LINE_COUNTER for each newline.
7613 (The canonical line counter is &ip->lineno.)
7614 Don't use this routine (or the next one) if bumping the line
7615 counter is not sufficient to deal with newlines in the string.
7617 If NOWARN is nonzero, don't warn about slash-star inside a comment.
7618 This feature is useful when processing a comment that is going to
7619 be processed or was processed at another point in the preprocessor,
7620 to avoid a duplicate warning. Likewise for unterminated comment
7624 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
7625 register FILE_BUF
*ip
;
7626 int *line_counter
; /* place to remember newlines, or NULL */
7629 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
7630 register U_CHAR
*bp
= ip
->bufp
;
7631 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
7632 int start_line
= line_counter
? *line_counter
: 0;
7634 /* JF this line_counter stuff is a crock to make sure the
7635 comment is only put out once, no matter how many times
7636 the comment is skipped. It almost works */
7639 *op
->bufp
++ = bp
[-1];
7641 if (cplusplus_comments
&& bp
[-1] == '/') {
7642 for (; bp
< limit
; bp
++) {
7646 if (!nowarn
&& warn_comments
)
7647 warning ("multiline `//' comment");
7659 while (bp
< limit
) {
7664 /* If this is the end of the file, we have an unterminated comment.
7665 Don't swallow the newline. We are guaranteed that there will be a
7666 trailing newline and various pieces assume it's there. */
7673 if (line_counter
!= NULL
)
7679 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
7680 warning ("`/*' within comment");
7681 if (*bp
== '\\' && bp
[1] == '\n')
7694 error_with_line (line_for_error (start_line
), "unterminated comment");
7699 /* Skip over a quoted string. BP points to the opening quote.
7700 Returns a pointer after the closing quote. Don't go past LIMIT.
7701 START_LINE is the line number of the starting point (but it need
7702 not be valid if the starting point is inside a macro expansion).
7704 The input stack state is not changed.
7706 If COUNT_NEWLINES is nonzero, it points to an int to increment
7707 for each newline passed.
7709 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
7710 if we pass a backslash-newline.
7712 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
7715 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
7716 register U_CHAR
*bp
;
7717 register U_CHAR
*limit
;
7719 int *count_newlines
;
7720 int *backslash_newlines_p
;
7723 register U_CHAR c
, match
;
7728 error_with_line (line_for_error (start_line
),
7729 "unterminated string or character constant");
7730 error_with_line (multiline_string_line
,
7731 "possible real start of unterminated constant");
7732 multiline_string_line
= 0;
7739 while (*bp
== '\\' && bp
[1] == '\n') {
7740 if (backslash_newlines_p
)
7741 *backslash_newlines_p
= 1;
7746 if (*bp
== '\n' && count_newlines
) {
7747 if (backslash_newlines_p
)
7748 *backslash_newlines_p
= 1;
7752 } else if (c
== '\n') {
7754 /* Unterminated strings and character constants are 'valid'. */
7755 bp
--; /* Don't consume the newline. */
7760 if (match
== '\'') {
7761 error_with_line (line_for_error (start_line
),
7762 "unterminated string or character constant");
7768 /* If not traditional, then allow newlines inside strings. */
7771 if (multiline_string_line
== 0) {
7773 pedwarn_with_line (line_for_error (start_line
),
7774 "string constant runs past end of line");
7775 multiline_string_line
= start_line
;
7777 } else if (c
== match
)
7783 /* Place into DST a quoted string representing the string SRC.
7784 Return the address of DST's terminating null. */
7787 quote_string (dst
, src
)
7794 switch ((c
= *src
++))
7801 sprintf (dst
, "\\%03o", c
);
7819 /* Skip across a group of balanced parens, starting from IP->bufp.
7820 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
7822 This does not handle newlines, because it's used for the arg of #if,
7823 where there aren't any newlines. Also, backslash-newline can't appear. */
7826 skip_paren_group (ip
)
7827 register FILE_BUF
*ip
;
7829 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
7830 U_CHAR
*p
= ip
->bufp
;
7832 int lines_dummy
= 0;
7834 while (p
!= limit
) {
7844 return ip
->bufp
= p
;
7850 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
7858 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
7860 return ip
->bufp
= p
;
7870 /* Write out a #line directive, for instance, after an #include file.
7871 If CONDITIONAL is nonzero, we can omit the #line if it would
7872 appear to be a no-op, and we can output a few newlines instead
7873 if we want to increase the line number by a small amount.
7874 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
7877 output_line_directive (ip
, op
, conditional
, file_change
)
7880 enum file_change_code file_change
;
7883 char *line_directive_buf
, *line_end
;
7885 if (no_line_directives
7886 || ip
->fname
== NULL
7888 op
->lineno
= ip
->lineno
;
7893 if (ip
->lineno
== op
->lineno
)
7896 /* If the inherited line number is a little too small,
7897 output some newlines instead of a #line directive. */
7898 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
7899 check_expand (op
, 10);
7900 while (ip
->lineno
> op
->lineno
) {
7908 /* Output a positive line number if possible. */
7909 while (ip
->lineno
<= 0 && ip
->bufp
- ip
->buf
< ip
->length
7910 && *ip
->bufp
== '\n') {
7915 line_directive_buf
= (char *) alloca (4 * strlen (ip
->nominal_fname
) + 100);
7916 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
7917 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
7919 if (file_change
!= same_file
) {
7921 *line_end
++ = file_change
== enter_file
? '1' : '2';
7923 /* Tell cc1 if following text comes from a system header file. */
7924 if (ip
->system_header_p
) {
7928 #ifndef NO_IMPLICIT_EXTERN_C
7929 /* Tell cc1plus if following text should be treated as C. */
7930 if (ip
->system_header_p
== 2 && cplusplus
) {
7936 len
= line_end
- line_directive_buf
;
7937 check_expand (op
, len
+ 1);
7938 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7940 bcopy ((char *) line_directive_buf
, (char *) op
->bufp
, len
);
7942 op
->lineno
= ip
->lineno
;
7945 /* This structure represents one parsed argument in a macro call.
7946 `raw' points to the argument text as written (`raw_length' is its length).
7947 `expanded' points to the argument's macro-expansion
7948 (its length is `expand_length').
7949 `stringified_length' is the length the argument would have
7951 `use_count' is the number of times this macro arg is substituted
7952 into the macro. If the actual use count exceeds 10,
7953 the value stored is 10.
7954 `free1' and `free2', if nonzero, point to blocks to be freed
7955 when the macro argument data is no longer needed. */
7958 U_CHAR
*raw
, *expanded
;
7959 int raw_length
, expand_length
;
7960 int stringified_length
;
7961 U_CHAR
*free1
, *free2
;
7966 /* Expand a macro call.
7967 HP points to the symbol that is the macro being called.
7968 Put the result of expansion onto the input stack
7969 so that subsequent input by our caller will use it.
7971 If macro wants arguments, caller has already verified that
7972 an argument list follows; arguments come from the input stack. */
7975 macroexpand (hp
, op
)
7980 DEFINITION
*defn
= hp
->value
.defn
;
7981 register U_CHAR
*xbuf
;
7983 int start_line
= instack
[indepth
].lineno
;
7984 int rest_args
, rest_zero
;
7986 CHECK_DEPTH (return;);
7988 /* it might not actually be a macro. */
7989 if (hp
->type
!= T_MACRO
) {
7990 special_symbol (hp
, op
);
7994 /* This macro is being used inside a #if, which means it must be */
7995 /* recorded as a precondition. */
7996 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
7997 dump_single_macro (hp
, pcp_outfile
);
7999 nargs
= defn
->nargs
;
8003 struct argdata
*args
;
8004 char *parse_error
= 0;
8006 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
8008 for (i
= 0; i
< nargs
; i
++) {
8009 args
[i
].raw
= (U_CHAR
*) "";
8010 args
[i
].expanded
= 0;
8011 args
[i
].raw_length
= args
[i
].expand_length
8012 = args
[i
].stringified_length
= 0;
8013 args
[i
].free1
= args
[i
].free2
= 0;
8014 args
[i
].use_count
= 0;
8017 /* Parse all the macro args that are supplied. I counts them.
8018 The first NARGS args are stored in ARGS.
8019 The rest are discarded.
8020 If rest_args is set then we assume macarg absorbed the rest of the args.
8025 /* Discard the open-parenthesis or comma before the next arg. */
8026 ++instack
[indepth
].bufp
;
8029 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
8030 /* If we are working on last arg which absorbs rest of args... */
8031 if (i
== nargs
- 1 && defn
->rest_args
)
8033 parse_error
= macarg (&args
[i
], rest_args
);
8036 parse_error
= macarg (NULL_PTR
, 0);
8038 error_with_line (line_for_error (start_line
), parse_error
);
8042 } while (*instack
[indepth
].bufp
!= ')');
8044 /* If we got one arg but it was just whitespace, call that 0 args. */
8046 register U_CHAR
*bp
= args
[0].raw
;
8047 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
8048 /* cpp.texi says for foo ( ) we provide one argument.
8049 However, if foo wants just 0 arguments, treat this as 0. */
8051 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8056 /* Don't output an error message if we have already output one for
8057 a parse error above. */
8059 if (nargs
== 0 && i
> 0) {
8061 error ("arguments given to macro `%s'", hp
->name
);
8062 } else if (i
< nargs
) {
8063 /* traditional C allows foo() if foo wants one argument. */
8064 if (nargs
== 1 && i
== 0 && traditional
)
8066 /* the rest args token is allowed to absorb 0 tokens */
8067 else if (i
== nargs
- 1 && defn
->rest_args
)
8069 else if (parse_error
)
8072 error ("macro `%s' used without args", hp
->name
);
8074 error ("macro `%s' used with just one arg", hp
->name
);
8076 error ("macro `%s' used with only %d args", hp
->name
, i
);
8077 } else if (i
> nargs
) {
8079 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8082 /* Swallow the closeparen. */
8083 ++instack
[indepth
].bufp
;
8085 /* If macro wants zero args, we parsed the arglist for checking only.
8086 Read directly from the macro definition. */
8088 xbuf
= defn
->expansion
;
8089 xbuf_len
= defn
->length
;
8091 register U_CHAR
*exp
= defn
->expansion
;
8092 register int offset
; /* offset in expansion,
8093 copied a piece at a time */
8094 register int totlen
; /* total amount of exp buffer filled so far */
8096 register struct reflist
*ap
, *last_ap
;
8098 /* Macro really takes args. Compute the expansion of this call. */
8100 /* Compute length in characters of the macro's expansion.
8101 Also count number of times each arg is used. */
8102 xbuf_len
= defn
->length
;
8103 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8105 xbuf_len
+= args
[ap
->argno
].stringified_length
;
8106 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8107 /* Add 4 for two newline-space markers to prevent
8108 token concatenation. */
8109 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8111 /* We have an ordinary (expanded) occurrence of the arg.
8112 So compute its expansion, if we have not already. */
8113 if (args
[ap
->argno
].expanded
== 0) {
8115 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8116 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8119 args
[ap
->argno
].expanded
= obuf
.buf
;
8120 args
[ap
->argno
].expand_length
= obuf
.length
;
8121 args
[ap
->argno
].free2
= obuf
.buf
;
8124 /* Add 4 for two newline-space markers to prevent
8125 token concatenation. */
8126 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
8128 if (args
[ap
->argno
].use_count
< 10)
8129 args
[ap
->argno
].use_count
++;
8132 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8134 /* Generate in XBUF the complete expansion
8135 with arguments substituted in.
8136 TOTLEN is the total size generated so far.
8137 OFFSET is the index in the definition
8138 of where we are copying from. */
8139 offset
= totlen
= 0;
8140 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8141 last_ap
= ap
, ap
= ap
->next
) {
8142 register struct argdata
*arg
= &args
[ap
->argno
];
8143 int count_before
= totlen
;
8145 /* Add chars to XBUF. */
8146 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8147 xbuf
[totlen
++] = exp
[offset
];
8149 /* If followed by an empty rest arg with concatenation,
8150 delete the last run of nonwhite chars. */
8151 if (rest_zero
&& totlen
> count_before
8152 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8153 || (last_ap
!= NULL
&& last_ap
->rest_args
8154 && last_ap
->raw_after
!= 0))) {
8155 /* Delete final whitespace. */
8156 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8160 /* Delete the nonwhites before them. */
8161 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8166 if (ap
->stringify
!= 0) {
8167 int arglen
= arg
->raw_length
;
8173 && (c
= arg
->raw
[i
], is_space
[c
]))
8176 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8179 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8180 for (; i
< arglen
; i
++) {
8183 /* Special markers Newline Space
8184 generate nothing for a stringified argument. */
8185 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
8190 /* Internal sequences of whitespace are replaced by one space
8191 except within an string or char token. */
8193 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
8195 /* Note that Newline Space does occur within whitespace
8196 sequences; consider it part of the sequence. */
8197 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
8199 else if (c
!= '\n' && is_space
[c
])
8216 } else if (c
== '\"' || c
== '\'')
8220 /* Escape these chars */
8221 if (c
== '\"' || (in_string
&& c
== '\\'))
8222 xbuf
[totlen
++] = '\\';
8226 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
8231 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8232 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8233 U_CHAR
*p1
= arg
->raw
;
8234 U_CHAR
*l1
= p1
+ arg
->raw_length
;
8235 if (ap
->raw_before
!= 0) {
8236 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8237 while (p1
!= l1
&& is_idchar
[*p1
])
8238 xbuf
[totlen
++] = *p1
++;
8239 /* Delete any no-reexpansion marker that follows
8240 an identifier at the beginning of the argument
8241 if the argument is concatenated with what precedes it. */
8242 if (p1
[0] == '\n' && p1
[1] == '-')
8244 } else if (!traditional
) {
8245 /* Ordinary expanded use of the argument.
8246 Put in newline-space markers to prevent token pasting. */
8247 xbuf
[totlen
++] = '\n';
8248 xbuf
[totlen
++] = ' ';
8250 if (ap
->raw_after
!= 0) {
8251 /* Arg is concatenated after: delete trailing whitespace,
8252 whitespace markers, and no-reexpansion markers. */
8254 if (is_space
[l1
[-1]]) l1
--;
8255 else if (l1
[-1] == '-') {
8256 U_CHAR
*p2
= l1
- 1;
8257 /* If a `-' is preceded by an odd number of newlines then it
8258 and the last newline are a no-reexpansion marker. */
8259 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8260 if ((l1
- 1 - p2
) & 1) {
8269 bcopy ((char *) p1
, (char *) (xbuf
+ totlen
), l1
- p1
);
8271 if (!traditional
&& ap
->raw_after
== 0) {
8272 /* Ordinary expanded use of the argument.
8273 Put in newline-space markers to prevent token pasting. */
8274 xbuf
[totlen
++] = '\n';
8275 xbuf
[totlen
++] = ' ';
8278 /* Ordinary expanded use of the argument.
8279 Put in newline-space markers to prevent token pasting. */
8281 xbuf
[totlen
++] = '\n';
8282 xbuf
[totlen
++] = ' ';
8284 bcopy ((char *) arg
->expanded
, (char *) (xbuf
+ totlen
),
8285 arg
->expand_length
);
8286 totlen
+= arg
->expand_length
;
8288 xbuf
[totlen
++] = '\n';
8289 xbuf
[totlen
++] = ' ';
8291 /* If a macro argument with newlines is used multiple times,
8292 then only expand the newlines once. This avoids creating output
8293 lines which don't correspond to any input line, which confuses
8295 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8296 /* Don't bother doing change_newlines for subsequent
8300 = change_newlines (arg
->expanded
, arg
->expand_length
);
8304 if (totlen
> xbuf_len
)
8308 /* If there is anything left of the definition after handling
8309 the arg list, copy that in too. */
8311 for (i
= offset
; i
< defn
->length
; i
++) {
8312 /* if we've reached the end of the macro */
8315 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8316 && last_ap
->raw_after
!= 0))
8317 xbuf
[totlen
++] = exp
[i
];
8323 for (i
= 0; i
< nargs
; i
++) {
8324 if (args
[i
].free1
!= 0)
8325 free (args
[i
].free1
);
8326 if (args
[i
].free2
!= 0)
8327 free (args
[i
].free2
);
8331 xbuf
= defn
->expansion
;
8332 xbuf_len
= defn
->length
;
8335 /* Now put the expansion on the input stack
8336 so our caller will commence reading from it. */
8338 register FILE_BUF
*ip2
;
8340 ip2
= &instack
[++indepth
];
8343 ip2
->nominal_fname
= 0;
8345 /* This may not be exactly correct, but will give much better error
8346 messages for nested macro calls than using a line number of zero. */
8347 ip2
->lineno
= start_line
;
8349 ip2
->length
= xbuf_len
;
8351 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8353 ip2
->if_stack
= if_stack
;
8354 ip2
->system_header_p
= 0;
8356 /* Recursive macro use sometimes works traditionally.
8357 #define foo(x,y) bar (x (y,0), y)
8361 hp
->type
= T_DISABLED
;
8365 /* Parse a macro argument and store the info on it into *ARGPTR.
8366 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8367 Return nonzero to indicate a syntax error. */
8370 macarg (argptr
, rest_args
)
8371 register struct argdata
*argptr
;
8374 FILE_BUF
*ip
= &instack
[indepth
];
8380 /* Try to parse as much of the argument as exists at this
8381 input stack level. */
8382 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
8383 &paren
, &newlines
, &comments
, rest_args
);
8385 /* If we find the end of the argument at this level,
8386 set up *ARGPTR to point at it in the input stack. */
8387 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
8388 && bp
!= ip
->buf
+ ip
->length
) {
8390 argptr
->raw
= ip
->bufp
;
8391 argptr
->raw_length
= bp
- ip
->bufp
;
8392 argptr
->newlines
= newlines
;
8396 /* This input stack level ends before the macro argument does.
8397 We must pop levels and keep parsing.
8398 Therefore, we must allocate a temporary buffer and copy
8399 the macro argument into it. */
8400 int bufsize
= bp
- ip
->bufp
;
8401 int extra
= newlines
;
8402 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8403 int final_start
= 0;
8405 bcopy ((char *) ip
->bufp
, (char *) buffer
, bufsize
);
8407 ip
->lineno
+= newlines
;
8409 while (bp
== ip
->buf
+ ip
->length
) {
8410 if (instack
[indepth
].macro
== 0) {
8411 result
= "unterminated macro call";
8414 ip
->macro
->type
= T_MACRO
;
8416 free (ip
->free_ptr
);
8417 ip
= &instack
[--indepth
];
8420 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
8421 &newlines
, &comments
, rest_args
);
8422 final_start
= bufsize
;
8423 bufsize
+= bp
- ip
->bufp
;
8425 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
8426 bcopy ((char *) ip
->bufp
, (char *) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
8429 ip
->lineno
+= newlines
;
8432 /* Now, if arg is actually wanted, record its raw form,
8433 discarding comments and duplicating newlines in whatever
8434 part of it did not come from a macro expansion.
8435 EXTRA space has been preallocated for duplicating the newlines.
8436 FINAL_START is the index of the start of that part. */
8438 argptr
->raw
= buffer
;
8439 argptr
->raw_length
= bufsize
;
8440 argptr
->free1
= buffer
;
8441 argptr
->newlines
= newlines
;
8442 if ((newlines
|| comments
) && ip
->fname
!= 0)
8445 discard_comments (argptr
->raw
+ final_start
,
8446 argptr
->raw_length
- final_start
,
8448 argptr
->raw
[argptr
->raw_length
] = 0;
8449 if (argptr
->raw_length
> bufsize
+ extra
)
8454 /* If we are not discarding this argument,
8455 macroexpand it and compute its length as stringified.
8456 All this info goes into *ARGPTR. */
8459 register U_CHAR
*buf
, *lim
;
8460 register int totlen
;
8463 lim
= buf
+ argptr
->raw_length
;
8465 while (buf
!= lim
&& is_space
[*buf
])
8467 while (buf
!= lim
&& is_space
[lim
[-1]])
8469 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
8470 while (buf
!= lim
) {
8471 register U_CHAR c
= *buf
++;
8473 /* Internal sequences of whitespace are replaced by one space
8474 in most cases, but not always. So count all the whitespace
8475 in case we need to keep it all. */
8478 SKIP_ALL_WHITE_SPACE (buf
);
8481 if (c
== '\"' || c
== '\\') /* escape these chars */
8483 else if (!isprint (c
))
8486 argptr
->stringified_length
= totlen
;
8491 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8492 counting parens in *DEPTHPTR,
8493 and return if reach LIMIT
8494 or before a `)' that would make *DEPTHPTR negative
8495 or before a comma when *DEPTHPTR is zero.
8496 Single and double quotes are matched and termination
8497 is inhibited within them. Comments also inhibit it.
8498 Value returned is pointer to stopping place.
8500 Increment *NEWLINES each time a newline is passed.
8501 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8502 Set *COMMENTS to 1 if a comment is seen. */
8505 macarg1 (start
, limit
, depthptr
, newlines
, comments
, rest_args
)
8507 register U_CHAR
*limit
;
8508 int *depthptr
, *newlines
, *comments
;
8511 register U_CHAR
*bp
= start
;
8513 while (bp
< limit
) {
8519 if (--(*depthptr
) < 0)
8523 /* Traditionally, backslash makes following char not special. */
8524 if (bp
+ 1 < limit
&& traditional
)
8527 /* But count source lines anyway. */
8536 if (bp
[1] == '\\' && bp
[2] == '\n')
8537 newline_fix (bp
+ 1);
8540 for (bp
+= 2; bp
< limit
; bp
++) {
8543 else if (*bp
== '*') {
8544 if (bp
[-1] == '/' && warn_comments
)
8545 warning ("`/*' within comment");
8546 if (bp
[1] == '\\' && bp
[2] == '\n')
8547 newline_fix (bp
+ 1);
8554 } else if (bp
[1] == '/' && cplusplus_comments
) {
8556 for (bp
+= 2; bp
< limit
; bp
++) {
8562 warning ("multiline `//' comment");
8571 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
8576 while (*bp
== '\\' && bp
[1] == '\n') {
8579 } else if (*bp
== '\n') {
8588 /* if we've returned to lowest level and we aren't absorbing all args */
8589 if ((*depthptr
) == 0 && rest_args
== 0)
8599 /* Discard comments and duplicate newlines
8600 in the string of length LENGTH at START,
8601 except inside of string constants.
8602 The string is copied into itself with its beginning staying fixed.
8604 NEWLINES is the number of newlines that must be duplicated.
8605 We assume that that much extra space is available past the end
8609 discard_comments (start
, length
, newlines
)
8614 register U_CHAR
*ibp
;
8615 register U_CHAR
*obp
;
8616 register U_CHAR
*limit
;
8619 /* If we have newlines to duplicate, copy everything
8620 that many characters up. Then, in the second part,
8621 we will have room to insert the newlines
8623 NEWLINES may actually be too large, because it counts
8624 newlines in string constants, and we don't duplicate those.
8625 But that does no harm. */
8627 ibp
= start
+ length
;
8628 obp
= ibp
+ newlines
;
8630 while (limit
!= ibp
)
8634 ibp
= start
+ newlines
;
8635 limit
= start
+ length
+ newlines
;
8638 while (ibp
< limit
) {
8639 *obp
++ = c
= *ibp
++;
8642 /* Duplicate the newline. */
8654 if (*ibp
== '\\' && ibp
[1] == '\n')
8656 /* Delete any comment. */
8657 if (cplusplus_comments
&& ibp
[0] == '/') {
8658 /* Comments are equivalent to spaces. */
8661 while (ibp
< limit
&& (*ibp
!= '\n' || ibp
[-1] == '\\'))
8665 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
8667 /* Comments are equivalent to spaces.
8668 For -traditional, a comment is equivalent to nothing. */
8674 while (ibp
+ 1 < limit
) {
8676 && ibp
[1] == '\\' && ibp
[2] == '\n')
8677 newline_fix (ibp
+ 1);
8678 if (ibp
[0] == '*' && ibp
[1] == '/')
8687 /* Notice and skip strings, so that we don't
8688 think that comments start inside them,
8689 and so we don't duplicate newlines in them. */
8692 while (ibp
< limit
) {
8693 *obp
++ = c
= *ibp
++;
8696 if (c
== '\n' && quotec
== '\'')
8698 if (c
== '\\' && ibp
< limit
) {
8699 while (*ibp
== '\\' && ibp
[1] == '\n')
8712 /* Turn newlines to spaces in the string of length LENGTH at START,
8713 except inside of string constants.
8714 The string is copied into itself with its beginning staying fixed. */
8717 change_newlines (start
, length
)
8721 register U_CHAR
*ibp
;
8722 register U_CHAR
*obp
;
8723 register U_CHAR
*limit
;
8727 limit
= start
+ length
;
8730 while (ibp
< limit
) {
8731 *obp
++ = c
= *ibp
++;
8734 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
8735 string. Skip past the newline and its duplicate.
8736 Put a space in the output. */
8747 /* Notice and skip strings, so that we don't delete newlines in them. */
8750 while (ibp
< limit
) {
8751 *obp
++ = c
= *ibp
++;
8754 if (c
== '\n' && quotec
== '\'')
8765 /* my_strerror - return the descriptive text associated with an
8769 my_strerror (errnum
)
8775 #ifndef HAVE_STRERROR
8776 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
8778 result
= strerror (errnum
);
8781 /* VAXCRTL's strerror() takes an optional second argument, which only
8782 matters when the first argument is EVMSERR. However, it's simplest
8783 just to pass it unconditionally. `vaxc$errno' is declared in
8784 <errno.h>, and maintained by the library in parallel with `errno'.
8785 We assume that caller's `errnum' either matches the last setting of
8786 `errno' by the library or else does not have the value `EVMSERR'. */
8788 result
= strerror (errnum
, vaxc$errno
);
8792 result
= "undocumented I/O error";
8797 /* error - print error message and increment count of errors. */
8800 error (PRINTF_ALIST (msg
))
8805 VA_START (args
, msg
);
8816 FILE_BUF
*ip
= NULL
;
8818 print_containing_files ();
8820 for (i
= indepth
; i
>= 0; i
--)
8821 if (instack
[i
].fname
!= NULL
) {
8827 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8828 vfprintf (stderr
, msg
, args
);
8829 fprintf (stderr
, "\n");
8833 /* Error including a message from `errno'. */
8836 error_from_errno (name
)
8840 FILE_BUF
*ip
= NULL
;
8842 print_containing_files ();
8844 for (i
= indepth
; i
>= 0; i
--)
8845 if (instack
[i
].fname
!= NULL
) {
8851 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8853 fprintf (stderr
, "%s: %s\n", name
, my_strerror (errno
));
8858 /* Print error message but don't count it. */
8861 warning (PRINTF_ALIST (msg
))
8866 VA_START (args
, msg
);
8867 vwarning (msg
, args
);
8872 vwarning (msg
, args
)
8877 FILE_BUF
*ip
= NULL
;
8879 if (inhibit_warnings
)
8882 if (warnings_are_errors
)
8885 print_containing_files ();
8887 for (i
= indepth
; i
>= 0; i
--)
8888 if (instack
[i
].fname
!= NULL
) {
8894 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
8895 fprintf (stderr
, "warning: ");
8896 vfprintf (stderr
, msg
, args
);
8897 fprintf (stderr
, "\n");
8901 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8902 error_with_line (int line
, PRINTF_ALIST (msg
))
8904 error_with_line (line
, PRINTF_ALIST (msg
))
8911 VA_START (args
, msg
);
8912 verror_with_line (line
, msg
, args
);
8917 verror_with_line (line
, msg
, args
)
8923 FILE_BUF
*ip
= NULL
;
8925 print_containing_files ();
8927 for (i
= indepth
; i
>= 0; i
--)
8928 if (instack
[i
].fname
!= NULL
) {
8934 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, line
);
8935 vfprintf (stderr
, msg
, args
);
8936 fprintf (stderr
, "\n");
8941 #if defined (__STDC__) && defined (HAVE_VPRINTF)
8942 warning_with_line (int line
, PRINTF_ALIST (msg
))
8944 warning_with_line (line
, PRINTF_ALIST (msg
))
8951 VA_START (args
, msg
);
8952 vwarning_with_line (line
, msg
, args
);
8957 vwarning_with_line (line
, msg
, args
)
8963 FILE_BUF
*ip
= NULL
;
8965 if (inhibit_warnings
)
8968 if (warnings_are_errors
)
8971 print_containing_files ();
8973 for (i
= indepth
; i
>= 0; i
--)
8974 if (instack
[i
].fname
!= NULL
) {
8980 fprintf (stderr
, line
? "%s:%d: " : "%s: ", ip
->nominal_fname
, line
);
8981 fprintf (stderr
, "warning: ");
8982 vfprintf (stderr
, msg
, args
);
8983 fprintf (stderr
, "\n");
8986 /* Print an error message and maybe count it. */
8989 pedwarn (PRINTF_ALIST (msg
))
8994 VA_START (args
, msg
);
8995 if (pedantic_errors
)
8998 vwarning (msg
, args
);
9003 #if defined (__STDC__) && defined (HAVE_VPRINTF)
9004 pedwarn_with_line (int line
, PRINTF_ALIST (msg
))
9006 pedwarn_with_line (line
, PRINTF_ALIST (msg
))
9013 VA_START (args
, msg
);
9014 if (pedantic_errors
)
9015 verror_with_line (line
, msg
, args
);
9017 vwarning_with_line (line
, msg
, args
);
9021 /* Report a warning (or an error if pedantic_errors)
9022 giving specified file name and line number, not current. */
9025 #if defined (__STDC__) && defined (HAVE_VPRINTF)
9026 pedwarn_with_file_and_line (char *file
, int line
, PRINTF_ALIST (msg
))
9028 pedwarn_with_file_and_line (file
, line
, PRINTF_ALIST (msg
))
9036 if (!pedantic_errors
&& inhibit_warnings
)
9039 fprintf (stderr
, "%s:%d: ", file
, line
);
9040 if (pedantic_errors
)
9042 if (!pedantic_errors
)
9043 fprintf (stderr
, "warning: ");
9044 VA_START (args
, msg
);
9045 vfprintf (stderr
, msg
, args
);
9047 fprintf (stderr
, "\n");
9050 /* Print the file names and line numbers of the #include
9051 directives which led to the current file. */
9054 print_containing_files ()
9056 FILE_BUF
*ip
= NULL
;
9060 /* If stack of files hasn't changed since we last printed
9061 this info, don't repeat it. */
9062 if (last_error_tick
== input_file_stack_tick
)
9065 for (i
= indepth
; i
>= 0; i
--)
9066 if (instack
[i
].fname
!= NULL
) {
9071 /* Give up if we don't find a source file. */
9075 /* Find the other, outer source files. */
9076 for (i
--; i
>= 0; i
--)
9077 if (instack
[i
].fname
!= NULL
) {
9081 fprintf (stderr
, "In file included");
9083 fprintf (stderr
, ",\n ");
9086 fprintf (stderr
, " from %s:%d", ip
->nominal_fname
, ip
->lineno
);
9089 fprintf (stderr
, ":\n");
9091 /* Record we have printed the status as of this time. */
9092 last_error_tick
= input_file_stack_tick
;
9095 /* Return the line at which an error occurred.
9096 The error is not necessarily associated with the current spot
9097 in the input stack, so LINE says where. LINE will have been
9098 copied from ip->lineno for the current input level.
9099 If the current level is for a file, we return LINE.
9100 But if the current level is not for a file, LINE is meaningless.
9101 In that case, we return the lineno of the innermost file. */
9104 line_for_error (line
)
9110 for (i
= indepth
; i
>= 0; ) {
9111 if (instack
[i
].fname
!= 0)
9116 line1
= instack
[i
].lineno
;
9124 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9126 * As things stand, nothing is ever placed in the output buffer to be
9127 * removed again except when it's KNOWN to be part of an identifier,
9128 * so flushing and moving down everything left, instead of expanding,
9132 /* You might think void was cleaner for the return type,
9133 but that would get type mismatch in check_expand in strict ANSI. */
9136 grow_outbuf (obuf
, needed
)
9137 register FILE_BUF
*obuf
;
9138 register int needed
;
9143 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9146 /* Make it at least twice as big as it is now. */
9148 /* Make it have at least 150% of the free space we will need. */
9149 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9150 if (minsize
> obuf
->length
)
9151 obuf
->length
= minsize
;
9153 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
9156 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9162 /* Symbol table for macro names and special symbols */
9165 * install a name in the main hash table, even if it is already there.
9166 * name stops with first non alphanumeric, except leading '#'.
9167 * caller must check against redefinition if that is desired.
9168 * delete_macro () removes things installed by install () in fifo order.
9169 * this is important because of the `defined' special symbol used
9170 * in #if, and also if pushdef/popdef directives are ever implemented.
9172 * If LEN is >= 0, it is the length of the name.
9173 * Otherwise, compute the length by scanning the entire name.
9175 * If HASH is >= 0, it is the precomputed hash code.
9176 * Otherwise, compute the hash code.
9180 install (name
, len
, type
, value
, hash
)
9183 enum node_type type
;
9187 register HASHNODE
*hp
;
9188 register int i
, bucket
;
9189 register U_CHAR
*p
, *q
;
9193 while (is_idchar
[*p
])
9199 hash
= hashf (name
, len
, HASHSIZE
);
9201 i
= sizeof (HASHNODE
) + len
+ 1;
9202 hp
= (HASHNODE
*) xmalloc (i
);
9204 hp
->bucket_hdr
= &hashtab
[bucket
];
9205 hp
->next
= hashtab
[bucket
];
9206 hashtab
[bucket
] = hp
;
9208 if (hp
->next
!= NULL
)
9209 hp
->next
->prev
= hp
;
9212 hp
->value
.cpval
= value
;
9213 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9216 for (i
= 0; i
< len
; i
++)
9223 * find the most recent hash node for name name (ending with first
9224 * non-identifier char) installed by install
9226 * If LEN is >= 0, it is the length of the name.
9227 * Otherwise, compute the length by scanning the entire name.
9229 * If HASH is >= 0, it is the precomputed hash code.
9230 * Otherwise, compute the hash code.
9234 lookup (name
, len
, hash
)
9239 register U_CHAR
*bp
;
9240 register HASHNODE
*bucket
;
9243 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
9248 hash
= hashf (name
, len
, HASHSIZE
);
9250 bucket
= hashtab
[hash
];
9252 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
9254 bucket
= bucket
->next
;
9260 * Delete a hash node. Some weirdness to free junk from macros.
9261 * More such weirdness will have to be added if you define more hash
9262 * types that need it.
9265 /* Note that the DEFINITION of a macro is removed from the hash table
9266 but its storage is not freed. This would be a storage leak
9267 except that it is not reasonable to keep undefining and redefining
9268 large numbers of macros many times.
9269 In any case, this is necessary, because a macro can be #undef'd
9270 in the middle of reading the arguments to a call to it.
9271 If #undef freed the DEFINITION, that would crash. */
9278 if (hp
->prev
!= NULL
)
9279 hp
->prev
->next
= hp
->next
;
9280 if (hp
->next
!= NULL
)
9281 hp
->next
->prev
= hp
->prev
;
9283 /* Make sure that the bucket chain header that the deleted guy was
9284 on points to the right thing afterwards. */
9285 if (hp
== *hp
->bucket_hdr
)
9286 *hp
->bucket_hdr
= hp
->next
;
9289 if (hp
->type
== T_MACRO
) {
9290 DEFINITION
*d
= hp
->value
.defn
;
9291 struct reflist
*ap
, *nextap
;
9293 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
9304 * return hash function on name. must be compatible with the one
9305 * computed a step at a time, elsewhere
9309 hashf (name
, len
, hashsize
)
9310 register U_CHAR
*name
;
9317 r
= HASHSTEP (r
, *name
++);
9319 return MAKE_POS (r
) % hashsize
;
9323 /* Dump the definition of a single macro HP to OF. */
9326 dump_single_macro (hp
, of
)
9327 register HASHNODE
*hp
;
9330 register DEFINITION
*defn
= hp
->value
.defn
;
9336 /* Print the definition of the macro HP. */
9338 fprintf (of
, "#define %s", hp
->name
);
9340 if (defn
->nargs
>= 0) {
9344 for (i
= 0; i
< defn
->nargs
; i
++) {
9345 dump_arg_n (defn
, i
, of
);
9346 if (i
+ 1 < defn
->nargs
)
9356 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
9357 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
9358 offset
+= ap
->nchars
;
9360 if (ap
->nchars
!= 0)
9362 if (ap
->stringify
) {
9363 switch (ap
->stringify
) {
9364 case SHARP_TOKEN
: fprintf (of
, "#"); break;
9365 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
9366 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
9367 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
9371 if (ap
->raw_before
!= 0) {
9373 switch (ap
->raw_before
) {
9374 case WHITE_SHARP_TOKEN
:
9375 case WHITE_PERCENT_COLON_TOKEN
:
9382 switch (ap
->raw_before
) {
9383 case SHARP_TOKEN
: fprintf (of
, "##"); break;
9384 case WHITE_SHARP_TOKEN
: fprintf (of
, "## "); break;
9385 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
9386 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%: "); break;
9393 dump_arg_n (defn
, ap
->argno
, of
);
9394 if (!traditional
&& ap
->raw_after
!= 0) {
9395 switch (ap
->raw_after
) {
9396 case SHARP_TOKEN
: fprintf (of
, "##"); break;
9397 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
9398 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
9399 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
9405 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
9409 /* Dump all macro definitions as #defines to stdout. */
9416 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
9417 register HASHNODE
*hp
;
9419 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
9420 if (hp
->type
== T_MACRO
)
9421 dump_single_macro (hp
, stdout
);
9426 /* Output to OF a substring of a macro definition.
9427 BASE is the beginning of the definition.
9428 Output characters START thru LENGTH.
9429 Unless traditional, discard newlines outside of strings, thus
9430 converting funny-space markers to ordinary spaces. */
9433 dump_defn_1 (base
, start
, length
, of
)
9439 U_CHAR
*p
= base
+ start
;
9440 U_CHAR
*limit
= base
+ start
+ length
;
9443 fwrite (p
, sizeof (*p
), length
, of
);
9446 if (*p
== '\"' || *p
=='\'') {
9447 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
9448 NULL_PTR
, NULL_PTR
);
9449 fwrite (p
, sizeof (*p
), p1
- p
, of
);
9460 /* Print the name of argument number ARGNUM of macro definition DEFN
9462 Recall that DEFN->args.argnames contains all the arg names
9463 concatenated in reverse order with comma-space in between. */
9466 dump_arg_n (defn
, argnum
, of
)
9471 register U_CHAR
*p
= defn
->args
.argnames
;
9472 while (argnum
+ 1 < defn
->nargs
) {
9473 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
9477 while (*p
&& *p
!= ',') {
9483 /* Initialize syntactic classifications of characters. */
9486 initialize_char_syntax ()
9491 * Set up is_idchar and is_idstart tables. These should be
9492 * faster than saying (is_alpha (c) || c == '_'), etc.
9493 * Set up these things before calling any routines tthat
9496 for (i
= 'a'; i
<= 'z'; i
++) {
9497 is_idchar
[i
- 'a' + 'A'] = 1;
9499 is_idstart
[i
- 'a' + 'A'] = 1;
9502 for (i
= '0'; i
<= '9'; i
++)
9505 is_idstart
['_'] = 1;
9507 is_idstart
['$'] = 1;
9509 /* horizontal space table */
9510 is_hor_space
[' '] = 1;
9511 is_hor_space
['\t'] = 1;
9512 is_hor_space
['\v'] = 1;
9513 is_hor_space
['\f'] = 1;
9514 is_hor_space
['\r'] = 1;
9523 char_name
['\v'] = "vertical tab";
9524 char_name
['\f'] = "formfeed";
9525 char_name
['\r'] = "carriage return";
9528 /* Initialize the built-in macros. */
9531 initialize_builtins (inp
, outp
)
9535 install ((U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
9536 install ((U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
9537 install ((U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
9538 install ((U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
9539 install ((U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
9540 install ((U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
9541 #ifndef NO_BUILTIN_SIZE_TYPE
9542 install ((U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
9544 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9545 install ((U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
9547 install ((U_CHAR
*) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, NULL_PTR
, -1);
9548 install ((U_CHAR
*) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
,
9550 install ((U_CHAR
*) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
,
9552 install ((U_CHAR
*) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE
,
9554 install ((U_CHAR
*) "__TIME__", -1, T_TIME
, NULL_PTR
, -1);
9556 install ((U_CHAR
*) "__STDC__", -1, T_CONST
, "1", -1);
9557 install ((U_CHAR
*) "__STDC_VERSION__", -1, T_CONST
, "199409L", -1);
9560 install ((U_CHAR
*) "__OBJC__", -1, T_CONST
, "1", -1);
9561 /* This is supplied using a -D by the compiler driver
9562 so that it is present only when truly compiling with GNU C. */
9563 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
9564 install ((U_CHAR
*) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST
, "1", -1);
9568 char directive
[2048];
9569 U_CHAR
*udirective
= (U_CHAR
*) directive
;
9570 register struct directive
*dp
= &directive_table
[0];
9571 struct tm
*timebuf
= timestamp ();
9573 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
9574 instack
[0].nominal_fname
);
9575 output_line_directive (inp
, outp
, 0, same_file
);
9576 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9579 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
9580 output_line_directive (inp
, outp
, 0, same_file
);
9581 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9584 #ifndef NO_BUILTIN_SIZE_TYPE
9585 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
9586 output_line_directive (inp
, outp
, 0, same_file
);
9587 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9591 #ifndef NO_BUILTIN_PTRDIFF_TYPE
9592 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
9593 output_line_directive (inp
, outp
, 0, same_file
);
9594 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9598 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
9599 output_line_directive (inp
, outp
, 0, same_file
);
9600 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9603 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
9604 monthnames
[timebuf
->tm_mon
],
9605 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
9606 output_line_directive (inp
, outp
, 0, same_file
);
9607 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9610 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
9611 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
9612 output_line_directive (inp
, outp
, 0, same_file
);
9613 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9618 sprintf (directive
, " __STDC__ 1");
9619 output_line_directive (inp
, outp
, 0, same_file
);
9620 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9625 sprintf (directive
, " __OBJC__ 1");
9626 output_line_directive (inp
, outp
, 0, same_file
);
9627 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
9634 * process a given definition string, for initialization
9635 * If STR is just an identifier, define it with value 1.
9636 * If STR has anything after the identifier, then it should
9637 * be identifier=definition.
9641 make_definition (str
, op
)
9646 struct directive
*kt
;
9649 p
= buf
= (U_CHAR
*) str
;
9650 if (!is_idstart
[*p
]) {
9651 error ("malformed option `-D %s'", str
);
9654 while (is_idchar
[*++p
])
9657 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
9660 p
= (U_CHAR
*) str
; /* Error */
9663 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
9664 strcpy ((char *)buf
, str
);
9665 strcat ((char *)buf
, " 1");
9666 } else if (*p
!= '=') {
9667 error ("malformed option `-D %s'", str
);
9671 /* Copy the entire option so we can modify it. */
9672 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
9673 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
9674 /* Change the = to a space. */
9675 buf
[p
- (U_CHAR
*) str
] = ' ';
9676 /* Scan for any backslash-newline and remove it. */
9678 q
= &buf
[p
- (U_CHAR
*) str
];
9680 if (*p
== '\"' || *p
== '\'') {
9681 int unterminated
= 0;
9682 U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
9683 NULL_PTR
, NULL_PTR
, &unterminated
);
9687 if (*p
== '\\' && p
[1] == '\n')
9691 } else if (*p
== '\\' && p
[1] == '\n')
9693 /* Change newline chars into newline-markers. */
9694 else if (*p
== '\n')
9706 ip
= &instack
[++indepth
];
9707 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
9709 ip
->buf
= ip
->bufp
= buf
;
9710 ip
->length
= strlen ((char *) buf
);
9714 ip
->if_stack
= if_stack
;
9715 ip
->system_header_p
= 0;
9717 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
9720 /* Pass NULL instead of OP, since this is a "predefined" macro. */
9721 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
9725 /* JF, this does the work for the -U option */
9728 make_undef (str
, op
)
9733 struct directive
*kt
;
9735 ip
= &instack
[++indepth
];
9736 ip
->nominal_fname
= ip
->fname
= "*undef*";
9738 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
9739 ip
->length
= strlen (str
);
9743 ip
->if_stack
= if_stack
;
9744 ip
->system_header_p
= 0;
9746 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
9749 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
9753 /* Process the string STR as if it appeared as the body of a #assert.
9754 OPTION is the option name for which STR was the argument. */
9757 make_assertion (option
, str
)
9762 struct directive
*kt
;
9763 U_CHAR
*buf
, *p
, *q
;
9765 /* Copy the entire option so we can modify it. */
9766 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
9767 strcpy ((char *) buf
, str
);
9768 /* Scan for any backslash-newline and remove it. */
9771 if (*p
== '\\' && p
[1] == '\n')
9779 if (!is_idstart
[*p
]) {
9780 error ("malformed option `%s %s'", option
, str
);
9783 while (is_idchar
[*++p
])
9785 SKIP_WHITE_SPACE (p
);
9786 if (! (*p
== 0 || *p
== '(')) {
9787 error ("malformed option `%s %s'", option
, str
);
9791 ip
= &instack
[++indepth
];
9792 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
9794 ip
->buf
= ip
->bufp
= buf
;
9795 ip
->length
= strlen ((char *) buf
);
9799 ip
->if_stack
= if_stack
;
9800 ip
->system_header_p
= 0;
9802 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
9805 /* Pass NULL as output ptr to do_define since we KNOW it never does
9807 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
9811 #ifndef DIR_SEPARATOR
9812 #define DIR_SEPARATOR '/'
9815 /* The previous include prefix, if any, is PREV_FILE_NAME.
9816 Translate any pathnames with COMPONENT.
9817 Allocate a new include prefix whose name is the
9818 simplified concatenation of PREFIX and NAME,
9819 with a trailing / added if needed.
9820 But return 0 if the include prefix should be ignored,
9821 e.g. because it is a duplicate of PREV_FILE_NAME. */
9823 static struct file_name_list
*
9824 new_include_prefix (prev_file_name
, component
, prefix
, name
)
9825 struct file_name_list
*prev_file_name
;
9831 fatal ("Directory name missing after command line option");
9834 /* Ignore the empty string. */
9837 prefix
= update_path (prefix
, component
);
9838 name
= update_path (name
, component
);
9841 struct file_name_list
*dir
9842 = ((struct file_name_list
*)
9843 xmalloc (sizeof (struct file_name_list
)
9844 + strlen (prefix
) + strlen (name
) + 2));
9846 strcpy (dir
->fname
, prefix
);
9847 strcat (dir
->fname
, name
);
9848 len
= simplify_filename (dir
->fname
);
9850 /* Convert directory name to a prefix. */
9851 if (dir
->fname
[len
- 1] != DIR_SEPARATOR
) {
9852 if (len
== 1 && dir
->fname
[len
- 1] == '.')
9855 dir
->fname
[len
++] = DIR_SEPARATOR
;
9856 dir
->fname
[len
] = 0;
9859 /* Ignore a directory whose name matches the previous one. */
9860 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
9861 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
9862 if (!first_bracket_include
)
9863 first_bracket_include
= prev_file_name
;
9869 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
9871 /* Add a trailing "." if there is a filename. This increases the number
9872 of systems that can stat directories. We remove it below. */
9875 dir
->fname
[len
] = '.';
9876 dir
->fname
[len
+ 1] = 0;
9879 /* Ignore a nonexistent directory. */
9880 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
9881 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
9882 error_from_errno (dir
->fname
);
9888 dir
->fname
[len
] = 0;
9890 /* Ignore a directory whose identity matches the previous one. */
9892 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
9893 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
9894 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
9895 if (!first_bracket_include
)
9896 first_bracket_include
= prev_file_name
;
9903 dir
->c_system_include_path
= 0;
9904 dir
->got_name_map
= 0;
9910 /* Append a chain of `struct file_name_list's
9911 to the end of the main include chain.
9912 FIRST is the beginning of the chain to append, and LAST is the end. */
9915 append_include_chain (first
, last
)
9916 struct file_name_list
*first
, *last
;
9918 struct file_name_list
*dir
;
9920 if (!first
|| !last
)
9926 last_include
->next
= first
;
9928 if (first_bracket_include
== 0)
9929 first_bracket_include
= first
;
9931 for (dir
= first
; ; dir
= dir
->next
) {
9932 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
9933 if (len
> max_include_len
)
9934 max_include_len
= len
;
9940 last_include
= last
;
9943 /* Add output to `deps_buffer' for the -M switch.
9944 STRING points to the text to be output.
9945 SPACER is ':' for targets, ' ' for dependencies. */
9948 deps_output (string
, spacer
)
9952 int size
= strlen (string
);
9959 #ifndef MAX_OUTPUT_COLUMNS
9960 #define MAX_OUTPUT_COLUMNS 72
9962 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
9963 && 1 < deps_column
) {
9964 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
9971 if (deps_size
+ 2 * size
+ 8 > deps_allocated_size
) {
9972 deps_allocated_size
= (deps_size
+ 2 * size
+ 50) * 2;
9973 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
9975 if (spacer
== ' ') {
9976 deps_buffer
[deps_size
++] = ' ';
9980 for (i
= 0; i
< size
; ++i
)
9982 if (string
[i
] == '$')
9984 deps_buffer
[deps_size
++] = '$';
9989 p
= strchr ("~[]*?()\\ ", string
[i
]);
9992 deps_buffer
[deps_size
++] = '\\';
9996 deps_buffer
[deps_size
++] = string
[i
];
10000 if (spacer
== ':') {
10001 deps_buffer
[deps_size
++] = ':';
10004 deps_buffer
[deps_size
] = 0;
10008 fatal (PRINTF_ALIST (msg
))
10013 fprintf (stderr
, "%s: ", progname
);
10014 VA_START (args
, msg
);
10015 vfprintf (stderr
, msg
, args
);
10017 fprintf (stderr
, "\n");
10018 exit (FATAL_EXIT_CODE
);
10021 /* More 'friendly' abort that prints the line and file.
10022 config.h can #define abort fancy_abort if you like that sort of thing. */
10027 fatal ("Internal gcc abort.");
10031 perror_with_name (name
)
10034 fprintf (stderr
, "%s: ", progname
);
10035 fprintf (stderr
, "%s: %s\n", name
, my_strerror (errno
));
10040 pfatal_with_name (name
)
10043 perror_with_name (name
);
10047 exit (FATAL_EXIT_CODE
);
10051 /* Handler for SIGPIPE. */
10054 pipe_closed (signo
)
10055 /* If this is missing, some compilers complain. */
10058 fatal ("output pipe has been closed");
10064 fatal ("Memory exhausted.");
10072 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (size
);
10079 xrealloc (old
, size
)
10083 register GENERIC_PTR ptr
= (GENERIC_PTR
) realloc (old
, size
);
10090 xcalloc (number
, size
)
10091 size_t number
, size
;
10093 register size_t total
= number
* size
;
10094 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (total
);
10097 bzero (ptr
, total
);
10105 size_t size
= strlen (input
);
10106 char *output
= xmalloc (size
+ 1);
10107 strcpy (output
, input
);
10113 /* Under VMS we need to fix up the "include" specification filename so
10114 that everything following the 1st slash is changed into its correct
10115 VMS file specification. */
10118 hack_vms_include_specification (fname
, vaxc_include
)
10122 register char *cp
, *cp1
, *cp2
;
10123 int f
, check_filename_before_returning
;
10126 check_filename_before_returning
= 0;
10128 cp
= base_name (fname
);
10131 * Check if we have a vax-c style '#include filename'
10132 * and add the missing .h
10134 if (vaxc_include
&& !index (cp
,'.'))
10137 cp2
= Local
; /* initialize */
10139 /* We are trying to do a number of things here. First of all, we are
10140 trying to hammer the filenames into a standard format, such that later
10141 processing can handle them.
10143 If the file name contains something like [dir.], then it recognizes this
10144 as a root, and strips the ".]". Later processing will add whatever is
10145 needed to get things working properly.
10147 If no device is specified, then the first directory name is taken to be
10148 a device name (or a rooted logical). */
10150 /* See if we found that 1st slash */
10151 if (cp
== 0) return; /* Nothing to do!!! */
10152 if (*cp
!= '/') return; /* Nothing to do!!! */
10153 /* Point to the UNIX filename part (which needs to be fixed!) */
10155 /* If the directory spec is not rooted, we can just copy
10156 the UNIX filename part and we are done */
10157 if (((cp
- fname
) > 1) && ((cp
[-1] == ']') || (cp
[-1] == '>'))) {
10158 if (cp
[-2] != '.') {
10160 * The VMS part ends in a `]', and the preceding character is not a `.'.
10161 * We strip the `]', and then splice the two parts of the name in the
10162 * usual way. Given the default locations for include files in cccp.c,
10163 * we will only use this code if the user specifies alternate locations
10164 * with the /include (-I) switch on the command line. */
10165 cp
-= 1; /* Strip "]" */
10166 cp1
--; /* backspace */
10169 * The VMS part has a ".]" at the end, and this will not do. Later
10170 * processing will add a second directory spec, and this would be a syntax
10171 * error. Thus we strip the ".]", and thus merge the directory specs.
10172 * We also backspace cp1, so that it points to a '/'. This inhibits the
10173 * generation of the 000000 root directory spec (which does not belong here
10176 cp
-= 2; /* Strip ".]" */
10177 cp1
--; }; /* backspace */
10180 /* We drop in here if there is no VMS style directory specification yet.
10181 * If there is no device specification either, we make the first dir a
10182 * device and try that. If we do not do this, then we will be essentially
10183 * searching the users default directory (as if they did a #include "asdf.h").
10185 * Then all we need to do is to push a '[' into the output string. Later
10186 * processing will fill this in, and close the bracket.
10188 if (cp
[-1] != ':') *cp2
++ = ':'; /* dev not in spec. take first dir */
10189 *cp2
++ = '['; /* Open the directory specification */
10192 /* at this point we assume that we have the device spec, and (at least
10193 the opening "[" for a directory specification. We may have directories
10194 specified already */
10196 /* If there are no other slashes then the filename will be
10197 in the "root" directory. Otherwise, we need to add
10198 directory specifications. */
10199 if (index (cp1
, '/') == 0) {
10200 /* Just add "000000]" as the directory string */
10201 strcpy (cp2
, "000000]");
10202 cp2
+= strlen (cp2
);
10203 check_filename_before_returning
= 1; /* we might need to fool with this later */
10205 /* As long as there are still subdirectories to add, do them. */
10206 while (index (cp1
, '/') != 0) {
10207 /* If this token is "." we can ignore it */
10208 if ((cp1
[0] == '.') && (cp1
[1] == '/')) {
10212 /* Add a subdirectory spec. Do not duplicate "." */
10213 if (cp2
[-1] != '.' && cp2
[-1] != '[' && cp2
[-1] != '<')
10215 /* If this is ".." then the spec becomes "-" */
10216 if ((cp1
[0] == '.') && (cp1
[1] == '.') && (cp
[2] == '/')) {
10217 /* Add "-" and skip the ".." */
10222 /* Copy the subdirectory */
10223 while (*cp1
!= '/') *cp2
++= *cp1
++;
10224 cp1
++; /* Skip the "/" */
10226 /* Close the directory specification */
10227 if (cp2
[-1] == '.') /* no trailing periods */
10231 /* Now add the filename */
10232 while (*cp1
) *cp2
++ = *cp1
++;
10234 /* Now append it to the original VMS spec. */
10235 strcpy (cp
, Local
);
10237 /* If we put a [000000] in the filename, try to open it first. If this fails,
10238 remove the [000000], and return that name. This provides flexibility
10239 to the user in that they can use both rooted and non-rooted logical names
10240 to point to the location of the file. */
10242 if (check_filename_before_returning
) {
10243 f
= open (fname
, O_RDONLY
, 0666);
10245 /* The file name is OK as it is, so return it as is. */
10249 /* The filename did not work. Try to remove the [000000] from the name,
10251 cp
= index (fname
, '[');
10252 cp2
= index (fname
, ']') + 1;
10253 strcpy (cp
, cp2
); /* this gets rid of it */
10261 /* The following wrapper functions supply additional arguments to the VMS
10262 I/O routines to optimize performance with file handling. The arguments
10264 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
10265 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
10266 "fop=tef"- Truncate unused portions of file when closing file.
10267 "shr=nil"- Disallow file sharing while file is open. */
10270 VMS_freopen (fname
, type
, oldfile
)
10275 if (strcmp (type
, "w") == 0)
10276 return decc$
freopen (fname
, type
, oldfile
,
10277 "mbc=16", "deq=64", "fop=tef", "shr=nil");
10278 return decc$
freopen (fname
, type
, oldfile
, "mbc=16");
10282 VMS_fopen (fname
, type
)
10286 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
10287 fixed arguments, which matches ANSI's specification but not VAXCRTL's
10288 pre-ANSI implementation. This hack circumvents the mismatch problem. */
10289 FILE *(*vmslib_fopen
)() = (FILE *(*)()) decc$fopen
;
10292 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
10293 "deq=64", "fop=tef", "shr=nil");
10295 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
10299 VMS_open (fname
, flags
, prot
)
10304 return decc$
open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
10307 /* more VMS hackery */
10311 extern unsigned long sys$
parse(), sys$
search();
10313 /* Work around another library bug. If a file is located via a searchlist,
10314 and if the device it's on is not the same device as the one specified
10315 in the first element of that searchlist, then both stat() and fstat()
10316 will fail to return info about it. `errno' will be set to EVMSERR, and
10317 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
10318 We can get around this by fully parsing the filename and then passing
10319 that absolute name to stat().
10321 Without this fix, we can end up failing to find header files, which is
10322 bad enough, but then compounding the problem by reporting the reason for
10323 failure as "normal successful completion." */
10327 VMS_fstat (fd
, statbuf
)
10329 struct stat
*statbuf
;
10331 int result
= decc$
fstat (fd
, statbuf
);
10336 char nambuf
[NAM$C_MAXRSS
+1];
10338 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
10339 result
= VMS_stat (nambuf
, statbuf
);
10340 /* No fclose(fp) here; that would close(fd) as well. */
10347 VMS_stat (name
, statbuf
)
10349 struct stat
*statbuf
;
10351 int result
= stat (name
, statbuf
);
10357 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for sys$parse */
10358 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for sys$search */
10361 fab
.fab$l_fna
= (char *) name
;
10362 fab
.fab$b_fns
= (unsigned char) strlen (name
);
10363 fab
.fab$l_nam
= (void *) &nam
;
10365 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
10366 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
10367 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
10368 if (sys$
parse (&fab
) & 1)
10370 if (sys$
search (&fab
) & 1)
10372 res_nam
[nam
.nam$b_rsl
] = '\0';
10373 result
= stat (res_nam
, statbuf
);
10375 /* Clean up searchlist context cached by the system. */
10376 nam
.nam$b_nop
= NAM$M_SYNCHK
;
10377 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
10378 (void) sys$
parse (&fab
);