1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-97, 1998 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. */
23 #include "gansidecl.h"
26 #ifdef HAVE_SYS_RESOURCE_H
27 # include <sys/resource.h>
30 #ifdef MULTIBYTE_CHARS
33 #endif /* MULTIBYTE_CHARS */
35 typedef unsigned char U_CHAR
;
40 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
41 # define __attribute__(x)
44 #ifndef STANDARD_INCLUDE_DIR
45 # define STANDARD_INCLUDE_DIR "/usr/include"
48 /* By default, colon separates directories in a path. */
49 #ifndef PATH_SEPARATOR
50 # define PATH_SEPARATOR ':'
53 /* By default, the suffix for object files is ".o". */
55 # define HAVE_OBJECT_SUFFIX
57 # define OBJECT_SUFFIX ".o"
60 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
61 # define PRINTF_ALIST(msg) char *msg, ...
62 # define PRINTF_DCL(msg)
63 # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (__printf__, m, n)))
65 # define PRINTF_ALIST(msg) msg, va_alist
66 # define PRINTF_DCL(msg) char *msg; va_dcl
67 # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (__printf__, m, n)))
68 # define vfprintf(file, msg, args) \
70 char *a0 = va_arg(args, char *); \
71 char *a1 = va_arg(args, char *); \
72 char *a2 = va_arg(args, char *); \
73 char *a3 = va_arg(args, char *); \
74 fprintf (file, msg, a0, a1, a2, a3); \
78 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
79 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
80 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
81 #define PRINTF_PROTO_4(ARGS) PRINTF_PROTO(ARGS, 4, 5)
83 /* VMS-specific definitions */
86 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
87 #define fopen(fname,mode) VMS_fopen (fname,mode)
88 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
89 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
90 #define fwrite(ptr,size,nitems,stream) VMS_fwrite (ptr,size,nitems,stream)
91 static int VMS_fstat (), VMS_stat ();
92 static int VMS_open ();
93 static FILE *VMS_fopen ();
94 static FILE *VMS_freopen ();
95 static size_t VMS_fwrite ();
96 static void hack_vms_include_specification ();
97 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
98 #define INO_T_HASH(a) 0
99 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
102 /* Windows does not natively support inodes, and neither does MSDOS. */
103 #if (defined (_WIN32) && ! defined (CYGWIN32)) || defined (__MSDOS__)
104 #define INO_T_EQ(a, b) 0
113 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
114 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
116 /* Find the largest host integer type and set its size and type.
117 Watch out: on some crazy hosts `long' is shorter than `int'. */
119 #ifndef HOST_WIDE_INT
121 # include <inttypes.h>
122 # define HOST_WIDE_INT intmax_t
124 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
125 # define HOST_WIDE_INT int
127 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
128 # define HOST_WIDE_INT long
130 # define HOST_WIDE_INT long long
137 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
141 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
145 #define INO_T_EQ(a, b) ((a) == (b))
149 #define INO_T_HASH(a) (a)
152 #ifndef INCLUDE_LEN_FUDGE
153 #define INCLUDE_LEN_FUDGE 0
156 /* External declarations. */
158 extern char *version_string
;
159 extern char *update_path
PROTO((char *, char *));
161 #ifndef HAVE_STRERROR
163 extern char *sys_errlist
[];
164 #else /* HAVE_STRERROR */
165 #ifdef NEED_DECLARATION_STRERROR
170 char *strerror (int,...);
172 HOST_WIDE_INT parse_escape
PROTO((char **, HOST_WIDE_INT
));
173 HOST_WIDE_INT parse_c_expression
PROTO((char *, int));
179 /* Name under which this program was invoked. */
181 static char *progname
;
183 /* Nonzero means use extra default include directories for C++. */
185 static int cplusplus
;
187 /* Nonzero means handle cplusplus style comments */
189 static int cplusplus_comments
;
191 /* Nonzero means handle #import, for objective C. */
195 /* Nonzero means this is an assembly file, and allow
196 unknown directives, which could be comments. */
200 /* Current maximum length of directory names in the search path
201 for include files. (Altered as we get more of them.) */
203 static int max_include_len
;
205 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
207 static int for_lint
= 0;
209 /* Nonzero means copy comments into the output file. */
211 static int put_out_comments
= 0;
213 /* Nonzero means don't process the ANSI trigraph sequences. */
215 static int no_trigraphs
= 0;
217 /* Nonzero means print the names of included files rather than
218 the preprocessed output. 1 means just the #include "...",
219 2 means #include <...> as well. */
221 static int print_deps
= 0;
223 /* Nonzero if missing .h files in -M output are assumed to be generated
224 files and not errors. */
226 static int print_deps_missing_files
= 0;
228 /* Nonzero means print names of header files (-H). */
230 static int print_include_names
= 0;
232 /* Nonzero means don't output line number information. */
234 static int no_line_directives
;
236 /* Nonzero means output the text in failing conditionals,
237 inside #failed ... #endfailed. */
239 static int output_conditionals
;
241 /* dump_only means inhibit output of the preprocessed text
242 and instead output the definitions of all user-defined
243 macros in a form suitable for use as input to cccp.
244 dump_names means pass #define and the macro name through to output.
245 dump_definitions means pass the whole definition (plus #define) through
248 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
249 dump_macros
= dump_none
;
251 /* Nonzero means pass all #define and #undef directives which we actually
252 process through to the output stream. This feature is used primarily
253 to allow cc1 to record the #defines and #undefs for the sake of
254 debuggers which understand about preprocessor macros, but it may
255 also be useful with -E to figure out how symbols are defined, and
256 where they are defined. */
257 static int debug_output
= 0;
259 /* Nonzero means pass #include lines through to the output,
260 even if they are ifdefed out. */
261 static int dump_includes
;
263 /* Nonzero indicates special processing used by the pcp program. The
264 special effects of this mode are:
266 Inhibit all macro expansion, except those inside #if directives.
268 Process #define directives normally, and output their contents
271 Output preconditions to pcp_outfile indicating all the relevant
272 preconditions for use of this file in a later cpp run.
274 static FILE *pcp_outfile
;
276 /* Nonzero means we are inside an IF during a -pcp run. In this mode
277 macro expansion is done, and preconditions are output for all macro
278 uses requiring them. */
279 static int pcp_inside_if
;
281 /* Nonzero means never to include precompiled files.
282 This is 1 since there's no way now to make precompiled files,
283 so it's not worth testing for them. */
284 static int no_precomp
= 1;
286 /* Nonzero means give all the error messages the ANSI standard requires. */
290 /* Nonzero means try to make failure to fit ANSI C an error. */
292 static int pedantic_errors
;
294 /* Nonzero means don't print warning messages. -w. */
296 static int inhibit_warnings
= 0;
298 /* Nonzero means warn if slash-star appears in a slash-star comment,
299 or if newline-backslash appears in a slash-slash comment. */
301 static int warn_comments
;
303 /* Nonzero means warn if a macro argument is (or would be)
304 stringified with -traditional. */
306 static int warn_stringify
;
308 /* Nonzero means warn if there are any trigraphs. */
310 static int warn_trigraphs
;
312 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
314 static int warn_undef
;
316 /* Nonzero means warn if we find white space where it doesn't belong. */
318 static int warn_white_space
;
320 /* Nonzero means warn if #import is used. */
322 static int warn_import
= 1;
324 /* Nonzero means turn warnings into errors. */
326 static int warnings_are_errors
;
328 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
332 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
336 /* Nonzero causes output not to be done,
337 but directives such as #define that have side effects
340 static int no_output
;
342 /* Nonzero means we should look for header.gcc files that remap file names. */
345 /* Nonzero means this file was included with a -imacros or -include
346 command line and should not be recorded as an include file. */
348 static int no_record_file
;
350 /* Nonzero means that we have finished processing the command line options.
351 This flag is used to decide whether or not to issue certain errors
354 static int done_initializing
= 0;
356 /* Line where a newline was first seen in a string constant. */
358 static int multiline_string_line
= 0;
360 /* I/O buffer structure.
361 The `fname' field is nonzero for source files and #include files
362 and for the dummy text used for -D and -U.
363 It is zero for rescanning results of macro expansion
364 and for expanding macro arguments. */
365 #define INPUT_STACK_MAX 400
366 static struct file_buf
{
368 /* Filename specified with #line directive. */
370 /* The length of nominal_fname, which may contain embedded NULs. */
371 size_t nominal_fname_len
;
372 /* Include file description. */
373 struct include_file
*inc
;
374 /* Record where in the search path this file was found.
375 For #include_next. */
376 struct file_name_list
*dir
;
381 /* Macro that this level is the expansion of.
382 Included so that we can reenable the macro
383 at the end of this level. */
384 struct hashnode
*macro
;
385 /* Value of if_stack at start of this file.
386 Used to prohibit unmatched #endif (etc) in an include file. */
387 struct if_stack
*if_stack
;
388 /* Object to be freed at end of input at this level. */
390 /* True if this is a system header file; see is_system_include. */
391 char system_header_p
;
392 } instack
[INPUT_STACK_MAX
];
394 static int last_error_tick
; /* Incremented each time we print it. */
395 static int input_file_stack_tick
; /* Incremented when the status changes. */
397 /* Current nesting level of input sources.
398 `instack[indepth]' is the level currently being read. */
399 static int indepth
= -1;
400 #define CHECK_DEPTH(code) \
401 if (indepth >= (INPUT_STACK_MAX - 1)) \
403 error_with_line (line_for_error (instack[indepth].lineno), \
404 "macro or `#include' recursion too deep"); \
408 /* Current depth in #include directives that use <...>. */
409 static int system_include_depth
= 0;
411 typedef struct file_buf FILE_BUF
;
413 /* The output buffer. Its LENGTH field is the amount of room allocated
414 for the buffer, not the number of chars actually present. To get
415 that, subtract outbuf.buf from outbuf.bufp. */
417 #define OUTBUF_SIZE 10 /* initial size of output buffer */
418 static FILE_BUF outbuf
;
420 /* Grow output buffer OBUF points at
421 so it can hold at least NEEDED more chars. */
423 #define check_expand(OBUF, NEEDED) \
424 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
425 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
427 struct file_name_list
429 struct file_name_list
*next
;
430 /* If the following is 1, it is a C-language system include
432 int c_system_include_path
;
433 /* Mapping of file names for this directory. */
434 struct file_name_map
*name_map
;
435 /* Non-zero if name_map is valid. */
437 /* The include directory status. */
439 /* The include prefix: "" denotes the working directory,
440 otherwise fname must end in '/'.
441 The actual size is dynamically allocated. */
445 /* #include "file" looks in source file dir, then stack. */
446 /* #include <file> just looks in the stack. */
447 /* -I directories are added to the end, then the defaults are added. */
449 static struct default_include
{
450 char *fname
; /* The name of the directory. */
451 char *component
; /* The component containing the directory */
452 int cplusplus
; /* Only look here if we're compiling C++. */
453 int cxx_aware
; /* Includes in this directory don't need to
454 be wrapped in extern "C" when compiling
456 } include_defaults_array
[]
457 #ifdef INCLUDE_DEFAULTS
461 /* Pick up GNU C++ specific include files. */
462 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
463 { OLD_GPLUSPLUS_INCLUDE_DIR
, 0, 1, 1 },
465 /* This is the dir for fixincludes. Put it just before
466 the files that we fix. */
467 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
468 /* For cross-compilation, this dir name is generated
469 automatically in Makefile.in. */
470 { CROSS_INCLUDE_DIR
, "GCC", 0, 0 },
471 #ifdef TOOL_INCLUDE_DIR
472 /* This is another place that the target system's headers might be. */
473 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0 },
475 #else /* not CROSS_COMPILE */
476 #ifdef LOCAL_INCLUDE_DIR
477 /* This should be /usr/local/include and should come before
478 the fixincludes-fixed header files. */
479 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
481 #ifdef TOOL_INCLUDE_DIR
482 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
483 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
484 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0 },
486 /* This is the dir for fixincludes. Put it just before
487 the files that we fix. */
488 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
489 /* Some systems have an extra dir of include files. */
490 #ifdef SYSTEM_INCLUDE_DIR
491 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
493 #ifndef STANDARD_INCLUDE_COMPONENT
494 #define STANDARD_INCLUDE_COMPONENT 0
496 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
497 #endif /* not CROSS_COMPILE */
500 #endif /* no INCLUDE_DEFAULTS */
502 /* The code looks at the defaults through this pointer, rather than through
503 the constant structure above. This pointer gets changed if an environment
504 variable specifies other defaults. */
505 static struct default_include
*include_defaults
= include_defaults_array
;
507 static struct file_name_list
*include
= 0; /* First dir to search */
508 /* First dir to search for <file> */
509 /* This is the first element to use for #include <...>.
510 If it is 0, use the entire chain for such includes. */
511 static struct file_name_list
*first_bracket_include
= 0;
512 /* This is the first element in the chain that corresponds to
513 a directory of system header files. */
514 static struct file_name_list
*first_system_include
= 0;
515 static struct file_name_list
*last_include
= 0; /* Last in chain */
517 /* Chain of include directories to put at the end of the other chain. */
518 static struct file_name_list
*after_include
= 0;
519 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
521 /* Chain to put at the start of the system include files. */
522 static struct file_name_list
*before_system
= 0;
523 static struct file_name_list
*last_before_system
= 0; /* Last in chain */
525 /* Directory prefix that should replace `/usr' in the standard
526 include file directories. */
527 static char *include_prefix
;
529 /* Maintain and search list of included files. */
531 struct include_file
{
532 struct include_file
*next
; /* for include_hashtab */
533 struct include_file
*next_ino
; /* for include_ino_hashtab */
535 /* If the following is the empty string, it means #pragma once
536 was seen in this include file, or #import was applied to the file.
537 Otherwise, if it is nonzero, it is a macro name.
538 Don't include the file again if that macro is defined. */
539 U_CHAR
*control_macro
;
540 /* Nonzero if the dependency on this include file has been output. */
545 /* Hash tables of files already included with #include or #import.
546 include_hashtab is by full name; include_ino_hashtab is by inode number. */
548 #define INCLUDE_HASHSIZE 61
549 static struct include_file
*include_hashtab
[INCLUDE_HASHSIZE
];
550 static struct include_file
*include_ino_hashtab
[INCLUDE_HASHSIZE
];
552 /* Global list of strings read in from precompiled files. This list
553 is kept in the order the strings are read in, with new strings being
554 added at the end through stringlist_tailp. We use this list to output
555 the strings at the end of the run.
557 static STRINGDEF
*stringlist
;
558 static STRINGDEF
**stringlist_tailp
= &stringlist
;
561 /* Structure returned by create_definition */
562 typedef struct macrodef MACRODEF
;
565 struct definition
*defn
;
570 enum sharp_token_type
{
571 NO_SHARP_TOKEN
= 0, /* token not present */
573 SHARP_TOKEN
= '#', /* token spelled with # only */
574 WHITE_SHARP_TOKEN
, /* token spelled with # and white space */
576 PERCENT_COLON_TOKEN
= '%', /* token spelled with %: only */
577 WHITE_PERCENT_COLON_TOKEN
/* token spelled with %: and white space */
580 /* Structure allocated for every #define. For a simple replacement
583 nargs = -1, the `pattern' list is null, and the expansion is just
584 the replacement text. Nargs = 0 means a functionlike macro with no args,
586 #define getchar() getc (stdin) .
587 When there are args, the expansion is the replacement text with the
588 args squashed out, and the reflist is a list describing how to
589 build the output from the input: e.g., "3 chars, then the 1st arg,
590 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
591 The chars here come from the expansion. Whatever is left of the
592 expansion after the last arg-occurrence is copied after that arg.
593 Note that the reflist can be arbitrarily long---
594 its length depends on the number of times the arguments appear in
595 the replacement text, not how many args there are. Example:
596 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
598 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
599 where (x, y) means (nchars, argno). */
601 typedef struct definition DEFINITION
;
604 int length
; /* length of expansion string */
605 int predefined
; /* True if the macro was builtin or */
606 /* came from the command line */
608 int line
; /* Line number of definition */
609 char *file
; /* File of definition */
610 size_t file_len
; /* Length of file (which can contain NULs) */
611 char rest_args
; /* Nonzero if last arg. absorbs the rest */
613 struct reflist
*next
;
615 enum sharp_token_type stringify
; /* set if a # operator before arg */
616 enum sharp_token_type raw_before
; /* set if a ## operator before arg */
617 enum sharp_token_type raw_after
; /* set if a ## operator after arg */
619 char rest_args
; /* Nonzero if this arg. absorbs the rest */
620 int nchars
; /* Number of literal chars to copy before
621 this arg occurrence. */
622 int argno
; /* Number of arg to substitute (origin-0) */
625 /* Names of macro args, concatenated in reverse order
626 with comma-space between them.
627 The only use of this is that we warn on redefinition
628 if this differs between the old and new definitions. */
633 /* different kinds of things that can appear in the value field
634 of a hash node. Actually, this may be useless now. */
642 * special extension string that can be added to the last macro argument to
643 * allow it to absorb the "rest" of the arguments when expanded. Ex:
644 * #define wow(a, b...) process (b, a, b)
645 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
646 * { wow (one, two); } -> { process (two, one, two); }
647 * if this "rest_arg" is used with the concat token '##' and if it is not
648 * supplied then the token attached to with ## will not be outputted. Ex:
649 * #define wow (a, b...) process (b ## , a, ## b)
650 * { wow (1, 2); } -> { process (2, 1, 2); }
651 * { wow (one); } -> { process (one); {
653 static char rest_extension
[] = "...";
654 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
656 /* The structure of a node in the hash table. The hash table
657 has entries for all tokens defined by #define directives (type T_MACRO),
658 plus some special tokens like __LINE__ (these each have their own
659 type, and the appropriate code is run when that type of node is seen.
660 It does not contain control words like "#define", which are recognized
661 by a separate piece of code. */
663 /* different flavors of hash nodes --- also used in keyword table */
665 T_DEFINE
= 1, /* the `#define' keyword */
666 T_INCLUDE
, /* the `#include' keyword */
667 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
668 T_IMPORT
, /* the `#import' keyword */
669 T_IFDEF
, /* the `#ifdef' keyword */
670 T_IFNDEF
, /* the `#ifndef' keyword */
671 T_IF
, /* the `#if' keyword */
672 T_ELSE
, /* `#else' */
673 T_PRAGMA
, /* `#pragma' */
674 T_ELIF
, /* `#elif' */
675 T_UNDEF
, /* `#undef' */
676 T_LINE
, /* `#line' */
677 T_ERROR
, /* `#error' */
678 T_WARNING
, /* `#warning' */
679 T_ENDIF
, /* `#endif' */
680 T_SCCS
, /* `#sccs', used on system V. */
681 T_IDENT
, /* `#ident', used on system V. */
682 T_ASSERT
, /* `#assert', taken from system V. */
683 T_UNASSERT
, /* `#unassert', taken from system V. */
684 T_SPECLINE
, /* special symbol `__LINE__' */
685 T_DATE
, /* `__DATE__' */
686 T_FILE
, /* `__FILE__' */
687 T_BASE_FILE
, /* `__BASE_FILE__' */
688 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
689 T_VERSION
, /* `__VERSION__' */
690 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
691 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
692 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
693 T_USER_LABEL_PREFIX_TYPE
, /* `__USER_LABEL_PREFIX__' */
694 T_REGISTER_PREFIX_TYPE
, /* `__REGISTER_PREFIX__' */
695 T_IMMEDIATE_PREFIX_TYPE
, /* `__IMMEDIATE_PREFIX__' */
696 T_TIME
, /* `__TIME__' */
697 T_CONST
, /* Constant value, used by `__STDC__' */
698 T_MACRO
, /* macro defined by `#define' */
699 T_DISABLED
, /* macro temporarily turned off for rescan */
700 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
701 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
702 T_UNUSED
/* Used for something not defined. */
706 struct hashnode
*next
; /* double links for easy deletion */
707 struct hashnode
*prev
;
708 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
709 chain is kept, in case the node is the head
710 of the chain and gets deleted. */
711 enum node_type type
; /* type of special token */
712 int length
; /* length of token, for quick comparison */
713 U_CHAR
*name
; /* the actual name */
714 union hashval value
; /* pointer to expansion, or whatever */
717 typedef struct hashnode HASHNODE
;
719 /* Some definitions for the hash table. The hash function MUST be
720 computed as shown in hashf () below. That is because the rescan
721 loop computes the hash value `on the fly' for most tokens,
722 in order to avoid the overhead of a lot of procedure calls to
723 the hashf () function. Hashf () only exists for the sake of
724 politeness, for use when speed isn't so important. */
726 #define HASHSIZE 1403
727 static HASHNODE
*hashtab
[HASHSIZE
];
728 #define HASHSTEP(old, c) ((old << 2) + c)
729 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
731 /* Symbols to predefine. */
733 #ifdef CPP_PREDEFINES
734 static char *predefs
= CPP_PREDEFINES
;
736 static char *predefs
= "";
739 /* We let tm.h override the types used here, to handle trivial differences
740 such as the choice of unsigned int or long unsigned int for size_t.
741 When machines start needing nontrivial differences in the size type,
742 it would be best to do something here to figure out automatically
743 from other information what type to use. */
745 /* The string value for __SIZE_TYPE__. */
748 #define SIZE_TYPE "long unsigned int"
751 /* The string value for __PTRDIFF_TYPE__. */
754 #define PTRDIFF_TYPE "long int"
757 /* The string value for __WCHAR_TYPE__. */
760 #define WCHAR_TYPE "int"
762 char * wchar_type
= WCHAR_TYPE
;
765 /* The string value for __USER_LABEL_PREFIX__ */
767 #ifndef USER_LABEL_PREFIX
768 #define USER_LABEL_PREFIX ""
771 /* The string value for __REGISTER_PREFIX__ */
773 #ifndef REGISTER_PREFIX
774 #define REGISTER_PREFIX ""
777 /* The string value for __IMMEDIATE_PREFIX__ */
779 #ifndef IMMEDIATE_PREFIX
780 #define IMMEDIATE_PREFIX ""
783 /* In the definition of a #assert name, this structure forms
784 a list of the individual values asserted.
785 Each value is itself a list of "tokens".
786 These are strings that are compared by name. */
788 struct tokenlist_list
{
789 struct tokenlist_list
*next
;
790 struct arglist
*tokens
;
793 struct assertion_hashnode
{
794 struct assertion_hashnode
*next
; /* double links for easy deletion */
795 struct assertion_hashnode
*prev
;
796 /* also, a back pointer to this node's hash
797 chain is kept, in case the node is the head
798 of the chain and gets deleted. */
799 struct assertion_hashnode
**bucket_hdr
;
800 int length
; /* length of token, for quick comparison */
801 U_CHAR
*name
; /* the actual name */
802 /* List of token-sequences. */
803 struct tokenlist_list
*value
;
806 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
808 /* Some definitions for the hash table. The hash function MUST be
809 computed as shown in hashf below. That is because the rescan
810 loop computes the hash value `on the fly' for most tokens,
811 in order to avoid the overhead of a lot of procedure calls to
812 the hashf function. hashf only exists for the sake of
813 politeness, for use when speed isn't so important. */
815 #define ASSERTION_HASHSIZE 37
816 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
818 /* Nonzero means inhibit macroexpansion of what seem to be
819 assertion tests, in rescan. For #if. */
820 static int assertions_flag
;
822 /* `struct directive' defines one #-directive, including how to handle it. */
824 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
827 int length
; /* Length of name */
828 int (*func
) DO_PROTO
; /* Function to handle directive */
829 char *name
; /* Name of directive */
830 enum node_type type
; /* Code which describes which directive. */
833 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
834 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
836 /* These functions are declared to return int instead of void since they
837 are going to be placed in the table and some old compilers have trouble with
838 pointers to functions returning void. */
840 static int do_assert DO_PROTO
;
841 static int do_define DO_PROTO
;
842 static int do_elif DO_PROTO
;
843 static int do_else DO_PROTO
;
844 static int do_endif DO_PROTO
;
845 static int do_error DO_PROTO
;
846 static int do_ident DO_PROTO
;
847 static int do_if DO_PROTO
;
848 static int do_include DO_PROTO
;
849 static int do_line DO_PROTO
;
850 static int do_pragma DO_PROTO
;
851 #ifdef SCCS_DIRECTIVE
852 static int do_sccs DO_PROTO
;
854 static int do_unassert DO_PROTO
;
855 static int do_undef DO_PROTO
;
856 static int do_xifdef DO_PROTO
;
858 /* Here is the actual list of #-directives, most-often-used first. */
860 static struct directive directive_table
[] = {
861 { 6, do_define
, "define", T_DEFINE
},
862 { 2, do_if
, "if", T_IF
},
863 { 5, do_xifdef
, "ifdef", T_IFDEF
},
864 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
865 { 5, do_endif
, "endif", T_ENDIF
},
866 { 4, do_else
, "else", T_ELSE
},
867 { 4, do_elif
, "elif", T_ELIF
},
868 { 4, do_line
, "line", T_LINE
},
869 { 7, do_include
, "include", T_INCLUDE
},
870 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
871 { 6, do_include
, "import", T_IMPORT
},
872 { 5, do_undef
, "undef", T_UNDEF
},
873 { 5, do_error
, "error", T_ERROR
},
874 { 7, do_error
, "warning", T_WARNING
},
875 #ifdef SCCS_DIRECTIVE
876 { 4, do_sccs
, "sccs", T_SCCS
},
878 { 6, do_pragma
, "pragma", T_PRAGMA
},
879 { 5, do_ident
, "ident", T_IDENT
},
880 { 6, do_assert
, "assert", T_ASSERT
},
881 { 8, do_unassert
, "unassert", T_UNASSERT
},
882 { -1, 0, "", T_UNUSED
},
885 /* When a directive handler is called,
886 this points to the # (or the : of the %:) that started the directive. */
887 U_CHAR
*directive_start
;
889 /* table to tell if char can be part of a C identifier. */
890 U_CHAR is_idchar
[256];
891 /* table to tell if char can be first char of a c identifier. */
892 U_CHAR is_idstart
[256];
893 /* table to tell if c is horizontal space. */
894 static U_CHAR is_hor_space
[256];
895 /* table to tell if c is horizontal or vertical space. */
896 U_CHAR is_space
[256];
898 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
899 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
901 static int errors
= 0; /* Error counter for exit code */
903 /* Name of output file, for error messages. */
904 static char *out_fname
;
907 /* Stack of conditionals currently in progress
908 (including both successful and failing conditionals). */
911 struct if_stack
*next
; /* for chaining to the next stack frame */
912 char *fname
; /* copied from input when frame is made */
913 size_t fname_len
; /* similarly */
914 int lineno
; /* similarly */
915 int if_succeeded
; /* true if a leg of this if-group
916 has been passed through rescan */
917 U_CHAR
*control_macro
; /* For #ifndef at start of file,
918 this is the macro name tested. */
919 enum node_type type
; /* type of last directive seen in this group */
921 typedef struct if_stack IF_STACK_FRAME
;
922 static IF_STACK_FRAME
*if_stack
= NULL
;
924 /* Buffer of -M output. */
925 static char *deps_buffer
;
927 /* Number of bytes allocated in above. */
928 static int deps_allocated_size
;
930 /* Number of bytes used. */
931 static int deps_size
;
933 /* Number of bytes since the last newline. */
934 static int deps_column
;
936 /* Nonzero means -I- has been seen,
937 so don't look for #include "foo" the source-file directory. */
938 static int ignore_srcdir
;
940 static int safe_read
PROTO((int, char *, int));
941 static void safe_write
PROTO((int, char *, int));
943 int main
PROTO((int, char **));
945 static void path_include
PROTO((char *));
947 static U_CHAR
*index0
PROTO((U_CHAR
*, int, size_t));
949 static void trigraph_pcp
PROTO((FILE_BUF
*));
950 static void check_white_space
PROTO((FILE_BUF
*));
952 static void newline_fix
PROTO((U_CHAR
*));
953 static void name_newline_fix
PROTO((U_CHAR
*));
955 static char *get_lintcmd
PROTO((U_CHAR
*, U_CHAR
*, U_CHAR
**, int *, int *));
957 static void rescan
PROTO((FILE_BUF
*, int));
959 static FILE_BUF expand_to_temp_buffer
PROTO((U_CHAR
*, U_CHAR
*, int, int));
961 static int handle_directive
PROTO((FILE_BUF
*, FILE_BUF
*));
963 static struct tm
*timestamp
PROTO((void));
964 static void special_symbol
PROTO((HASHNODE
*, FILE_BUF
*));
966 static int is_system_include
PROTO((char *));
967 static char *base_name
PROTO((char *));
968 static int absolute_filename
PROTO((char *));
969 static size_t simplify_filename
PROTO((char *));
971 static char *read_filename_string
PROTO((int, FILE *));
972 static struct file_name_map
*read_name_map
PROTO((char *));
973 static int open_include_file
PROTO((char *, struct file_name_list
*, U_CHAR
*, struct include_file
**));
974 static char *remap_include_file
PROTO((char *, struct file_name_list
*));
975 static int lookup_ino_include
PROTO((struct include_file
*));
977 static void finclude
PROTO((int, struct include_file
*, FILE_BUF
*, int, struct file_name_list
*));
978 static void record_control_macro
PROTO((struct include_file
*, U_CHAR
*));
980 static char *check_precompiled
PROTO((int, struct stat
*, char *, char **));
981 static int check_preconditions
PROTO((char *));
982 static void pcfinclude
PROTO((U_CHAR
*, U_CHAR
*, U_CHAR
*, FILE_BUF
*));
983 static void pcstring_used
PROTO((HASHNODE
*));
984 static void write_output
PROTO((void));
985 static void pass_thru_directive
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
987 static MACRODEF create_definition
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
989 static int check_macro_name
PROTO((U_CHAR
*, int));
990 static int compare_defs
PROTO((DEFINITION
*, DEFINITION
*));
991 static int comp_def_part
PROTO((int, U_CHAR
*, int, U_CHAR
*, int, int));
993 static DEFINITION
*collect_expansion
PROTO((U_CHAR
*, U_CHAR
*, int, struct arglist
*));
995 int check_assertion
PROTO((U_CHAR
*, int, int, struct arglist
*));
996 static int compare_token_lists
PROTO((struct arglist
*, struct arglist
*));
998 static struct arglist
*read_token_list
PROTO((U_CHAR
**, U_CHAR
*, int *));
999 static void free_token_list
PROTO((struct arglist
*));
1001 static ASSERTION_HASHNODE
*assertion_install
PROTO((U_CHAR
*, int, int));
1002 static ASSERTION_HASHNODE
*assertion_lookup
PROTO((U_CHAR
*, int, int));
1003 static void delete_assertion
PROTO((ASSERTION_HASHNODE
*));
1005 static void do_once
PROTO((void));
1007 static HOST_WIDE_INT eval_if_expression
PROTO((U_CHAR
*, int));
1008 static void conditional_skip
PROTO((FILE_BUF
*, int, enum node_type
, U_CHAR
*, FILE_BUF
*));
1009 static void skip_if_group
PROTO((FILE_BUF
*, int, FILE_BUF
*));
1010 static void validate_else
PROTO((U_CHAR
*, U_CHAR
*));
1012 static U_CHAR
*skip_to_end_of_comment
PROTO((FILE_BUF
*, int *, int));
1013 static U_CHAR
*skip_quoted_string
PROTO((U_CHAR
*, U_CHAR
*, int, int *, int *, int *));
1014 static char *quote_string
PROTO((char *, char *, size_t));
1015 static U_CHAR
*skip_paren_group
PROTO((FILE_BUF
*));
1017 /* Last arg to output_line_directive. */
1018 enum file_change_code
{same_file
, enter_file
, leave_file
};
1019 static void output_line_directive
PROTO((FILE_BUF
*, FILE_BUF
*, int, enum file_change_code
));
1021 static void macroexpand
PROTO((HASHNODE
*, FILE_BUF
*));
1024 static int macarg
PROTO((struct argdata
*, int));
1026 static U_CHAR
*macarg1
PROTO((U_CHAR
*, U_CHAR
*, struct hashnode
*, int *, int *, int *, int));
1028 static int discard_comments
PROTO((U_CHAR
*, int, int));
1030 static void change_newlines
PROTO((struct argdata
*));
1032 char *my_strerror
PROTO((int));
1033 static void notice
PRINTF_PROTO_1((char *, ...));
1034 static void vnotice
PROTO((char *, va_list));
1035 void error
PRINTF_PROTO_1((char *, ...));
1036 void verror
PROTO((char *, va_list));
1037 static void error_from_errno
PROTO((char *));
1038 void warning
PRINTF_PROTO_1((char *, ...));
1039 static void vwarning
PROTO((char *, va_list));
1040 static void error_with_line
PRINTF_PROTO_2((int, char *, ...));
1041 static void verror_with_line
PROTO((int, char *, va_list));
1042 static void vwarning_with_line
PROTO((int, char *, va_list));
1043 static void warning_with_line
PRINTF_PROTO_2((int, char *, ...));
1044 void pedwarn
PRINTF_PROTO_1((char *, ...));
1045 void pedwarn_with_line
PRINTF_PROTO_2((int, char *, ...));
1046 static void pedwarn_with_file_and_line
PRINTF_PROTO_4((char *, size_t, int, char *, ...));
1047 static void pedwarn_strange_white_space
PROTO((int));
1049 static void print_containing_files
PROTO((void));
1051 static int line_for_error
PROTO((int));
1052 static int grow_outbuf
PROTO((FILE_BUF
*, int));
1054 static HASHNODE
*install
PROTO((U_CHAR
*, int, enum node_type
, char *, int));
1055 HASHNODE
*lookup
PROTO((U_CHAR
*, int, int));
1056 static void delete_macro
PROTO((HASHNODE
*));
1057 static int hashf
PROTO((U_CHAR
*, int, int));
1059 static void dump_single_macro
PROTO((HASHNODE
*, FILE *));
1060 static void dump_all_macros
PROTO((void));
1061 static void dump_defn_1
PROTO((U_CHAR
*, int, int, FILE *));
1062 static void dump_arg_n
PROTO((DEFINITION
*, int, FILE *));
1064 static void initialize_char_syntax
PROTO((void));
1065 static void initialize_builtins
PROTO((FILE_BUF
*, FILE_BUF
*));
1067 static void make_definition
PROTO((char *, FILE_BUF
*));
1068 static void make_undef
PROTO((char *, FILE_BUF
*));
1070 static void make_assertion
PROTO((char *, char *));
1072 static struct file_name_list
*new_include_prefix
PROTO((struct file_name_list
*, char *, char *, char *));
1073 static void append_include_chain
PROTO((struct file_name_list
*, struct file_name_list
*));
1075 static int quote_string_for_make
PROTO((char *, char *));
1076 static void deps_output
PROTO((char *, int));
1078 void fatal
PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn
));
1079 void fancy_abort
PROTO((void)) __attribute__ ((noreturn
));
1080 static void perror_with_name
PROTO((char *));
1081 static void pfatal_with_name
PROTO((char *)) __attribute__ ((noreturn
));
1082 static void pipe_closed
PROTO((int)) __attribute__ ((noreturn
));
1084 static void memory_full
PROTO((void)) __attribute__ ((noreturn
));
1085 GENERIC_PTR xmalloc
PROTO((size_t));
1086 static GENERIC_PTR xrealloc
PROTO((GENERIC_PTR
, size_t));
1087 static GENERIC_PTR xcalloc
PROTO((size_t, size_t));
1088 static char *savestring
PROTO((char *));
1090 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1091 retrying if necessary. If MAX_READ_LEN is defined, read at most
1092 that bytes at a time. Return a negative value if an error occurs,
1093 otherwise return the actual number of bytes read,
1094 which must be LEN unless end-of-file was reached. */
1097 safe_read (desc
, ptr
, len
)
1102 int left
, rcount
, nchars
;
1108 if (rcount
> MAX_READ_LEN
)
1109 rcount
= MAX_READ_LEN
;
1111 nchars
= read (desc
, ptr
, rcount
);
1128 /* Write LEN bytes at PTR to descriptor DESC,
1129 retrying if necessary, and treating any real error as fatal.
1130 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1133 safe_write (desc
, ptr
, len
)
1138 int wcount
, written
;
1142 #ifdef MAX_WRITE_LEN
1143 if (wcount
> MAX_WRITE_LEN
)
1144 wcount
= MAX_WRITE_LEN
;
1146 written
= write (desc
, ptr
, wcount
);
1153 pfatal_with_name (out_fname
);
1174 char **pend_assertions
;
1175 char **pend_includes
;
1177 /* Record the option used with each element of pend_assertions.
1178 This is preparation for supporting more than one option for making
1180 char **pend_assertion_options
;
1181 int inhibit_predefs
= 0;
1182 int no_standard_includes
= 0;
1183 int no_standard_cplusplus_includes
= 0;
1184 int missing_newline
= 0;
1186 /* Non-0 means don't output the preprocessed program. */
1187 int inhibit_output
= 0;
1188 /* Non-0 means -v, so print the full set of include dirs. */
1191 /* File name which deps are being written to.
1192 This is 0 if deps are being written to stdout. */
1193 char *deps_file
= 0;
1194 /* Fopen file mode to open deps_file with. */
1195 char *deps_mode
= "a";
1196 /* Stream on which to print the dependency information. */
1197 FILE *deps_stream
= 0;
1198 /* Target-name to write with the dependency information. */
1199 char *deps_target
= 0;
1201 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1202 /* Get rid of any avoidable limit on stack size. */
1206 /* Set the stack limit huge so that alloca (particularly stringtab
1207 in dbxread.c) does not fail. */
1208 getrlimit (RLIMIT_STACK
, &rlim
);
1209 rlim
.rlim_cur
= rlim
.rlim_max
;
1210 setrlimit (RLIMIT_STACK
, &rlim
);
1215 signal (SIGPIPE
, pipe_closed
);
1218 setlocale (LC_MESSAGES
, "");
1219 bindtextdomain (PACKAGE
, localedir
);
1220 textdomain (PACKAGE
);
1222 progname
= base_name (argv
[0]);
1226 /* Remove extension from PROGNAME. */
1228 char *s
= progname
= savestring (progname
);
1230 if ((p
= rindex (s
, ';')) != 0) *p
= '\0'; /* strip version number */
1231 if ((p
= rindex (s
, '.')) != 0 /* strip type iff ".exe" */
1232 && (p
[1] == 'e' || p
[1] == 'E')
1233 && (p
[2] == 'x' || p
[2] == 'X')
1234 && (p
[3] == 'e' || p
[3] == 'E')
1240 /* Do not invoke xmalloc before this point, since locale and
1241 progname need to be set first, in case a diagnostic is issued. */
1243 pend_files
= (char **) xmalloc (argc
* sizeof (char *));
1244 pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
1245 pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
1246 pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
1247 pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
1248 pend_assertion_options
= (char **) xmalloc (argc
* sizeof (char *));
1253 /* Initialize is_idchar. */
1254 initialize_char_syntax ();
1256 no_line_directives
= 0;
1258 dump_macros
= dump_none
;
1261 cplusplus_comments
= 1;
1263 bzero ((char *) pend_files
, argc
* sizeof (char *));
1264 bzero ((char *) pend_defs
, argc
* sizeof (char *));
1265 bzero ((char *) pend_undefs
, argc
* sizeof (char *));
1266 bzero ((char *) pend_assertions
, argc
* sizeof (char *));
1267 bzero ((char *) pend_includes
, argc
* sizeof (char *));
1269 #ifdef MULTIBYTE_CHARS
1270 /* Change to the native locale for multibyte conversions. */
1271 setlocale (LC_CTYPE
, "");
1272 literal_codeset
= getenv ("LANG");
1275 /* Process switches and find input file name. */
1277 for (i
= 1; i
< argc
; i
++) {
1278 if (argv
[i
][0] != '-') {
1279 if (out_fname
!= NULL
)
1280 fatal ("Usage: %s [switches] input output", argv
[0]);
1281 else if (in_fname
!= NULL
)
1282 out_fname
= argv
[i
];
1286 switch (argv
[i
][1]) {
1289 if (!strcmp (argv
[i
], "-include")) {
1291 fatal ("Filename missing after `-include' option");
1293 simplify_filename (pend_includes
[i
] = argv
[i
]);
1297 if (!strcmp (argv
[i
], "-imacros")) {
1299 fatal ("Filename missing after `-imacros' option");
1301 simplify_filename (pend_files
[i
] = argv
[i
]);
1305 if (!strcmp (argv
[i
], "-iprefix")) {
1307 fatal ("Filename missing after `-iprefix' option");
1309 include_prefix
= argv
[++i
];
1311 if (!strcmp (argv
[i
], "-ifoutput")) {
1312 output_conditionals
= 1;
1314 if (!strcmp (argv
[i
], "-isystem")) {
1315 struct file_name_list
*dirtmp
;
1317 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1320 dirtmp
->c_system_include_path
= 1;
1322 if (before_system
== 0)
1323 before_system
= dirtmp
;
1325 last_before_system
->next
= dirtmp
;
1326 last_before_system
= dirtmp
; /* Tail follows the last one */
1328 /* Add directory to end of path for includes,
1329 with the default prefix at the front of its name. */
1330 if (!strcmp (argv
[i
], "-iwithprefix")) {
1331 struct file_name_list
*dirtmp
;
1334 if (include_prefix
!= 0)
1335 prefix
= include_prefix
;
1337 prefix
= savestring (GCC_INCLUDE_DIR
);
1338 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1339 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1340 prefix
[strlen (prefix
) - 7] = 0;
1343 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1344 prefix
, argv
[++i
])))
1347 if (after_include
== 0)
1348 after_include
= dirtmp
;
1350 last_after_include
->next
= dirtmp
;
1351 last_after_include
= dirtmp
; /* Tail follows the last one */
1353 /* Add directory to main path for includes,
1354 with the default prefix at the front of its name. */
1355 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
1356 struct file_name_list
*dirtmp
;
1359 if (include_prefix
!= 0)
1360 prefix
= include_prefix
;
1362 prefix
= savestring (GCC_INCLUDE_DIR
);
1363 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1364 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1365 prefix
[strlen (prefix
) - 7] = 0;
1368 dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
, prefix
, argv
[++i
]);
1369 append_include_chain (dirtmp
, dirtmp
);
1371 /* Add directory to end of path for includes. */
1372 if (!strcmp (argv
[i
], "-idirafter")) {
1373 struct file_name_list
*dirtmp
;
1375 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1379 if (after_include
== 0)
1380 after_include
= dirtmp
;
1382 last_after_include
->next
= dirtmp
;
1383 last_after_include
= dirtmp
; /* Tail follows the last one */
1388 if (out_fname
!= NULL
)
1389 fatal ("Output filename specified twice");
1391 fatal ("Filename missing after -o option");
1392 out_fname
= argv
[++i
];
1393 if (!strcmp (out_fname
, "-"))
1398 if (!strcmp (argv
[i
], "-pedantic"))
1400 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1402 pedantic_errors
= 1;
1403 } else if (!strcmp (argv
[i
], "-pcp")) {
1406 fatal ("Filename missing after -pcp option");
1407 pcp_fname
= argv
[++i
];
1409 = ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1410 ? fopen (pcp_fname
, "w")
1412 if (pcp_outfile
== 0)
1413 pfatal_with_name (pcp_fname
);
1419 if (!strcmp (argv
[i
], "-traditional")) {
1421 cplusplus_comments
= 0;
1422 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1428 if (! strcmp (argv
[i
], "-lang-c"))
1429 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, objc
= 0;
1430 if (! strcmp (argv
[i
], "-lang-c89"))
1431 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, objc
= 0;
1432 if (! strcmp (argv
[i
], "-lang-c++"))
1433 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, objc
= 0;
1434 if (! strcmp (argv
[i
], "-lang-objc"))
1435 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, objc
= 1;
1436 if (! strcmp (argv
[i
], "-lang-objc++"))
1437 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, objc
= 1;
1438 if (! strcmp (argv
[i
], "-lang-asm"))
1440 if (! strcmp (argv
[i
], "-lint"))
1445 cplusplus
= 1, cplusplus_comments
= 1;
1449 inhibit_warnings
= 1;
1453 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1455 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1457 else if (!strcmp (argv
[i
], "-Wcomment"))
1459 else if (!strcmp (argv
[i
], "-Wno-comment"))
1461 else if (!strcmp (argv
[i
], "-Wcomments"))
1463 else if (!strcmp (argv
[i
], "-Wno-comments"))
1465 else if (!strcmp (argv
[i
], "-Wtraditional"))
1467 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1469 else if (!strcmp (argv
[i
], "-Wwhite-space"))
1470 warn_white_space
= 1;
1471 else if (!strcmp (argv
[i
], "-Wno-white-space"))
1472 warn_white_space
= 0;
1473 else if (!strcmp (argv
[i
], "-Wundef"))
1475 else if (!strcmp (argv
[i
], "-Wno-undef"))
1477 else if (!strcmp (argv
[i
], "-Wimport"))
1479 else if (!strcmp (argv
[i
], "-Wno-import"))
1481 else if (!strcmp (argv
[i
], "-Werror"))
1482 warnings_are_errors
= 1;
1483 else if (!strcmp (argv
[i
], "-Wno-error"))
1484 warnings_are_errors
= 0;
1485 else if (!strcmp (argv
[i
], "-Wall"))
1489 warn_white_space
= 1;
1494 /* The style of the choices here is a bit mixed.
1495 The chosen scheme is a hybrid of keeping all options in one string
1496 and specifying each option in a separate argument:
1497 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1498 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1499 -M[M][G][D file]. This is awkward to handle in specs, and is not
1501 /* ??? -MG must be specified in addition to one of -M or -MM.
1502 This can be relaxed in the future without breaking anything.
1503 The converse isn't true. */
1505 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1506 if (!strcmp (argv
[i
], "-MG"))
1508 print_deps_missing_files
= 1;
1511 if (!strcmp (argv
[i
], "-M"))
1513 else if (!strcmp (argv
[i
], "-MM"))
1515 else if (!strcmp (argv
[i
], "-MD"))
1517 else if (!strcmp (argv
[i
], "-MMD"))
1519 /* For -MD and -MMD options, write deps on file named by next arg. */
1520 if (!strcmp (argv
[i
], "-MD")
1521 || !strcmp (argv
[i
], "-MMD")) {
1523 fatal ("Filename missing after %s option", argv
[i
]);
1525 deps_file
= argv
[i
];
1528 /* For -M and -MM, write deps on standard output
1529 and suppress the usual output. */
1530 deps_stream
= stdout
;
1537 char *p
= argv
[i
] + 2;
1539 while ((c
= *p
++)) {
1540 /* Arg to -d specifies what parts of macros to dump */
1543 dump_macros
= dump_only
;
1547 dump_macros
= dump_names
;
1550 dump_macros
= dump_definitions
;
1561 if (argv
[i
][2] == '3')
1566 notice ("GNU CPP version %s", version_string
);
1567 #ifdef TARGET_VERSION
1570 fprintf (stderr
, "\n");
1575 print_include_names
= 1;
1579 if (argv
[i
][2] != 0)
1580 pend_defs
[i
] = argv
[i
] + 2;
1581 else if (i
+ 1 == argc
)
1582 fatal ("Macro name missing after -D option");
1584 i
++, pend_defs
[i
] = argv
[i
];
1591 if (argv
[i
][2] != 0)
1593 else if (i
+ 1 == argc
)
1594 fatal ("Assertion missing after -A option");
1598 if (!strcmp (p
, "-")) {
1599 /* -A- eliminates all predefined macros and assertions.
1600 Let's include also any that were specified earlier
1601 on the command line. That way we can get rid of any
1602 that were passed automatically in from GCC. */
1604 inhibit_predefs
= 1;
1605 for (j
= 0; j
< i
; j
++)
1606 pend_defs
[j
] = pend_assertions
[j
] = 0;
1608 pend_assertions
[i
] = p
;
1609 pend_assertion_options
[i
] = "-A";
1614 case 'U': /* JF #undef something */
1615 if (argv
[i
][2] != 0)
1616 pend_undefs
[i
] = argv
[i
] + 2;
1617 else if (i
+ 1 == argc
)
1618 fatal ("Macro name missing after -U option");
1620 pend_undefs
[i
] = argv
[i
+1], i
++;
1624 put_out_comments
= 1;
1627 case 'E': /* -E comes from cc -E; ignore it. */
1631 no_line_directives
= 1;
1634 case '$': /* Don't include $ in identifiers. */
1635 is_idchar
['$'] = is_idstart
['$'] = 0;
1638 case 'I': /* Add directory to path for includes. */
1640 struct file_name_list
*dirtmp
;
1641 char *dir
= argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
];
1643 if (! ignore_srcdir
&& !strcmp (dir
, "-")) {
1645 /* Don't use any preceding -I directories for #include <...>. */
1646 first_bracket_include
= 0;
1649 dirtmp
= new_include_prefix (last_include
, NULL_PTR
, "", dir
);
1650 append_include_chain (dirtmp
, dirtmp
);
1656 if (!strcmp (argv
[i
], "-nostdinc"))
1657 /* -nostdinc causes no default include directories.
1658 You must specify all include-file directories with -I. */
1659 no_standard_includes
= 1;
1660 else if (!strcmp (argv
[i
], "-nostdinc++"))
1661 /* -nostdinc++ causes no default C++-specific include directories. */
1662 no_standard_cplusplus_includes
= 1;
1663 else if (!strcmp (argv
[i
], "-noprecomp"))
1668 if (!strcmp (argv
[i
], "-remap"))
1673 /* Sun compiler passes undocumented switch "-undef".
1674 Let's assume it means to inhibit the predefined symbols. */
1675 inhibit_predefs
= 1;
1678 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1679 if (in_fname
== NULL
) {
1682 } else if (out_fname
== NULL
) {
1685 } /* else fall through into error */
1688 fatal ("Invalid option `%s'", argv
[i
]);
1693 /* Add dirs from CPATH after dirs from -I. */
1694 /* There seems to be confusion about what CPATH should do,
1695 so for the moment it is not documented. */
1696 /* Some people say that CPATH should replace the standard include dirs,
1697 but that seems pointless: it comes before them, so it overrides them
1699 cp
= getenv ("CPATH");
1700 if (cp
&& ! no_standard_includes
)
1703 /* Initialize output buffer */
1705 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1706 outbuf
.bufp
= outbuf
.buf
;
1707 outbuf
.length
= OUTBUF_SIZE
;
1709 /* Do partial setup of input buffer for the sake of generating
1710 early #line directives (when -g is in effect). */
1712 fp
= &instack
[++indepth
];
1713 if (in_fname
== NULL
)
1715 fp
->nominal_fname
= fp
->fname
= in_fname
;
1716 fp
->nominal_fname_len
= strlen (in_fname
);
1719 /* In C++, wchar_t is a distinct basic type, and we can expect
1720 __wchar_t to be defined by cc1plus. */
1722 wchar_type
= "__wchar_t";
1724 /* Install __LINE__, etc. Must follow initialize_char_syntax
1725 and option processing. */
1726 initialize_builtins (fp
, &outbuf
);
1728 /* Do standard #defines and assertions
1729 that identify system and machine type. */
1731 if (!inhibit_predefs
) {
1732 char *p
= (char *) alloca (strlen (predefs
) + 1);
1733 strcpy (p
, predefs
);
1736 while (*p
== ' ' || *p
== '\t')
1738 /* Handle -D options. */
1739 if (p
[0] == '-' && p
[1] == 'D') {
1741 while (*p
&& *p
!= ' ' && *p
!= '\t')
1746 output_line_directive (fp
, &outbuf
, 0, same_file
);
1747 make_definition (q
, &outbuf
);
1748 while (*p
== ' ' || *p
== '\t')
1750 } else if (p
[0] == '-' && p
[1] == 'A') {
1751 /* Handle -A options (assertions). */
1760 past_name
= assertion
;
1761 /* Locate end of name. */
1762 while (*past_name
&& *past_name
!= ' '
1763 && *past_name
!= '\t' && *past_name
!= '(')
1765 /* Locate `(' at start of value. */
1767 while (*value
&& (*value
== ' ' || *value
== '\t'))
1769 if (*value
++ != '(')
1771 while (*value
&& (*value
== ' ' || *value
== '\t'))
1774 /* Locate end of value. */
1775 while (*past_value
&& *past_value
!= ' '
1776 && *past_value
!= '\t' && *past_value
!= ')')
1778 termination
= past_value
;
1779 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
1781 if (*termination
++ != ')')
1783 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
1785 /* Temporarily null-terminate the value. */
1786 save_char
= *termination
;
1787 *termination
= '\0';
1788 /* Install the assertion. */
1789 make_assertion ("-A", assertion
);
1790 *termination
= (char) save_char
;
1792 while (*p
== ' ' || *p
== '\t')
1800 /* Now handle the command line options. */
1802 /* Do -U's, -D's and -A's in the order they were seen. */
1803 for (i
= 1; i
< argc
; i
++) {
1804 if (pend_undefs
[i
]) {
1806 output_line_directive (fp
, &outbuf
, 0, same_file
);
1807 make_undef (pend_undefs
[i
], &outbuf
);
1811 output_line_directive (fp
, &outbuf
, 0, same_file
);
1812 make_definition (pend_defs
[i
], &outbuf
);
1814 if (pend_assertions
[i
])
1815 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1818 done_initializing
= 1;
1820 { /* Read the appropriate environment variable and if it exists
1821 replace include_defaults with the listed path. */
1823 switch ((objc
<< 1) + cplusplus
)
1826 epath
= getenv ("C_INCLUDE_PATH");
1829 epath
= getenv ("CPLUS_INCLUDE_PATH");
1832 epath
= getenv ("OBJC_INCLUDE_PATH");
1835 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
1838 /* If the environment var for this language is set,
1839 add to the default list of include directories. */
1842 char *startp
, *endp
;
1844 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1845 if (*startp
== PATH_SEPARATOR
)
1848 = (struct default_include
*) xmalloc ((num_dirs
1849 * sizeof (struct default_include
))
1850 + sizeof (include_defaults_array
));
1851 startp
= endp
= epath
;
1855 if (c
== PATH_SEPARATOR
|| !c
) {
1857 include_defaults
[num_dirs
].fname
1858 = startp
== endp
? "." : savestring (startp
);
1860 include_defaults
[num_dirs
].component
= 0;
1861 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1862 include_defaults
[num_dirs
].cxx_aware
= 1;
1869 /* Put the usual defaults back in at the end. */
1870 bcopy ((char *) include_defaults_array
,
1871 (char *) &include_defaults
[num_dirs
],
1872 sizeof (include_defaults_array
));
1876 append_include_chain (before_system
, last_before_system
);
1877 first_system_include
= before_system
;
1879 /* Unless -fnostdinc,
1880 tack on the standard include file dirs to the specified list */
1881 if (!no_standard_includes
) {
1882 struct default_include
*p
= include_defaults
;
1883 char *specd_prefix
= include_prefix
;
1884 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
1885 int default_len
= 0;
1886 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1887 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1888 default_len
= strlen (default_prefix
) - 7;
1889 default_prefix
[default_len
] = 0;
1891 /* Search "translated" versions of GNU directories.
1892 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1893 if (specd_prefix
!= 0 && default_len
!= 0)
1894 for (p
= include_defaults
; p
->fname
; p
++) {
1895 /* Some standard dirs are only for C++. */
1896 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1897 /* Does this dir start with the prefix? */
1898 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1899 /* Yes; change prefix and add to search list. */
1900 struct file_name_list
*new
1901 = new_include_prefix (NULL_PTR
, NULL_PTR
, specd_prefix
,
1902 p
->fname
+ default_len
);
1904 new->c_system_include_path
= !p
->cxx_aware
;
1905 append_include_chain (new, new);
1906 if (first_system_include
== 0)
1907 first_system_include
= new;
1912 /* Search ordinary names for GNU include directories. */
1913 for (p
= include_defaults
; p
->fname
; p
++) {
1914 /* Some standard dirs are only for C++. */
1915 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1916 struct file_name_list
*new
1917 = new_include_prefix (NULL_PTR
, p
->component
, "", p
->fname
);
1919 new->c_system_include_path
= !p
->cxx_aware
;
1920 append_include_chain (new, new);
1921 if (first_system_include
== 0)
1922 first_system_include
= new;
1928 /* Tack the after_include chain at the end of the include chain. */
1929 append_include_chain (after_include
, last_after_include
);
1930 if (first_system_include
== 0)
1931 first_system_include
= after_include
;
1933 /* With -v, print the list of dirs to search. */
1935 struct file_name_list
*p
;
1936 notice ("#include \"...\" search starts here:\n");
1937 for (p
= include
; p
; p
= p
->next
) {
1938 if (p
== first_bracket_include
)
1939 notice ("#include <...> search starts here:\n");
1941 fprintf (stderr
, " .\n");
1942 else if (!strcmp (p
->fname
, "/") || !strcmp (p
->fname
, "//"))
1943 fprintf (stderr
, " %s\n", p
->fname
);
1945 /* Omit trailing '/'. */
1946 fprintf (stderr
, " %.*s\n", (int) strlen (p
->fname
) - 1, p
->fname
);
1948 notice ("End of search list.\n");
1951 /* -MG doesn't select the form of output and must be specified with one of
1952 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1953 inhibit compilation. */
1954 if (print_deps_missing_files
&& (print_deps
== 0 || !inhibit_output
))
1955 fatal ("-MG must be specified with one of -M or -MM");
1957 /* Either of two environment variables can specify output of deps.
1958 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1959 where OUTPUT_FILE is the file to write deps info to
1960 and DEPS_TARGET is the target to mention in the deps. */
1963 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1964 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1965 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
1970 spec
= getenv ("SUNPRO_DEPENDENCIES");
1976 /* Find the space before the DEPS_TARGET, if there is one. */
1977 s
= index (spec
, ' ');
1979 deps_target
= s
+ 1;
1980 output_file
= xmalloc (s
- spec
+ 1);
1981 bcopy (spec
, output_file
, s
- spec
);
1982 output_file
[s
- spec
] = 0;
1988 deps_file
= output_file
;
1992 /* For -M, print the expected object file name
1993 as the target of this Make-rule. */
1995 deps_allocated_size
= 200;
1996 deps_buffer
= xmalloc (deps_allocated_size
);
2002 deps_output (deps_target
, ':');
2003 } else if (*in_fname
== 0) {
2004 deps_output ("-", ':');
2009 q
= base_name (in_fname
);
2011 /* Copy remainder to mungable area. */
2012 p
= (char *) alloca (strlen(q
) + 8);
2015 /* Output P, but remove known suffixes. */
2019 && p
[len
- 2] == '.'
2020 && index("cCsSm", p
[len
- 1]))
2023 && p
[len
- 3] == '.'
2024 && p
[len
- 2] == 'c'
2025 && p
[len
- 1] == 'c')
2028 && p
[len
- 4] == '.'
2029 && p
[len
- 3] == 'c'
2030 && p
[len
- 2] == 'x'
2031 && p
[len
- 1] == 'x')
2034 && p
[len
- 4] == '.'
2035 && p
[len
- 3] == 'c'
2036 && p
[len
- 2] == 'p'
2037 && p
[len
- 1] == 'p')
2040 /* Supply our own suffix. */
2041 strcpy (q
, OBJECT_SUFFIX
);
2043 deps_output (p
, ':');
2046 deps_output (in_fname
, ' ');
2049 /* Scan the -imacros files before the main input.
2050 Much like #including them, but with no_output set
2051 so that only their macro definitions matter. */
2053 no_output
++; no_record_file
++;
2054 for (i
= 1; i
< argc
; i
++)
2055 if (pend_files
[i
]) {
2056 struct include_file
*inc
;
2057 int fd
= open_include_file (pend_files
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2059 perror_with_name (pend_files
[i
]);
2060 return FATAL_EXIT_CODE
;
2062 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2064 no_output
--; no_record_file
--;
2066 /* Copy the entire contents of the main input file into
2067 the stacked input buffer previously allocated for it. */
2069 /* JF check for stdin */
2070 if (in_fname
== NULL
|| *in_fname
== 0) {
2073 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
2076 if (fstat (f
, &st
) != 0)
2077 pfatal_with_name (in_fname
);
2078 fp
->nominal_fname
= fp
->fname
= in_fname
;
2079 fp
->nominal_fname_len
= strlen (in_fname
);
2081 fp
->system_header_p
= 0;
2082 /* JF all this is mine about reading pipes and ttys */
2083 if (! S_ISREG (st
.st_mode
)) {
2084 /* Read input from a file that is not a normal disk file.
2085 We cannot preallocate a buffer with the correct size,
2086 so we must read in the file a piece at the time and make it bigger. */
2091 if (S_ISDIR (st
.st_mode
))
2092 fatal ("Input file `%s' is a directory", in_fname
);
2096 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
2098 cnt
= safe_read (f
, (char *) fp
->buf
+ size
, bsize
- size
);
2099 if (cnt
< 0) goto perror
; /* error! */
2101 if (size
!= bsize
) break; /* End of file */
2103 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
2107 /* Read a file whose size we can determine in advance.
2108 For the sake of VMS, st.st_size is just an upper bound. */
2109 size_t s
= (size_t) st
.st_size
;
2110 if (s
!= st
.st_size
|| s
+ 2 < s
)
2112 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
2113 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
2114 if (fp
->length
< 0) goto perror
;
2117 fp
->if_stack
= if_stack
;
2119 /* Make sure data ends with a newline. And put a null after it. */
2121 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
2122 /* Backslash-newline at end is not good enough. */
2123 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
2124 fp
->buf
[fp
->length
++] = '\n';
2125 missing_newline
= 1;
2127 fp
->buf
[fp
->length
] = '\0';
2129 /* Unless inhibited, convert trigraphs in the input. */
2134 if (warn_white_space
)
2135 check_white_space (fp
);
2137 /* Now that we know the input file is valid, open the output. */
2139 if (!out_fname
|| !strcmp (out_fname
, ""))
2140 out_fname
= "stdout";
2141 else if (! freopen (out_fname
, "w", stdout
))
2142 pfatal_with_name (out_fname
);
2144 output_line_directive (fp
, &outbuf
, 0, same_file
);
2146 /* Scan the -include files before the main input. */
2149 for (i
= 1; i
< argc
; i
++)
2150 if (pend_includes
[i
]) {
2151 struct include_file
*inc
;
2152 int fd
= open_include_file (pend_includes
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2154 perror_with_name (pend_includes
[i
]);
2155 return FATAL_EXIT_CODE
;
2157 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2161 /* Scan the input, processing macros and directives. */
2163 rescan (&outbuf
, 0);
2165 if (missing_newline
)
2168 if (pedantic
&& missing_newline
)
2169 pedwarn ("file does not end in newline");
2171 /* Now we have processed the entire input
2172 Write whichever kind of output has been requested. */
2174 if (dump_macros
== dump_only
)
2176 else if (! inhibit_output
) {
2181 /* Don't actually write the deps file if compilation has failed. */
2183 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, deps_mode
)))
2184 pfatal_with_name (deps_file
);
2185 fputs (deps_buffer
, deps_stream
);
2186 putc ('\n', deps_stream
);
2188 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
2189 fatal ("I/O error on output");
2194 if (pcp_outfile
&& pcp_outfile
!= stdout
2195 && (ferror (pcp_outfile
) || fclose (pcp_outfile
) != 0))
2196 fatal ("I/O error on `-pcp' output");
2198 if (ferror (stdout
) || fclose (stdout
) != 0)
2199 fatal ("I/O error on output");
2202 exit (FATAL_EXIT_CODE
);
2203 exit (SUCCESS_EXIT_CODE
);
2206 pfatal_with_name (in_fname
);
2210 /* Given a colon-separated list of file names PATH,
2211 add all the names to the search path for include files. */
2225 struct file_name_list
*dirtmp
;
2227 /* Find the end of this name. */
2228 while ((c
= *q
++) != PATH_SEPARATOR
&& c
)
2232 dirtmp
= new_include_prefix (last_include
, NULL_PTR
,
2233 "", p
== q
? "." : p
);
2235 append_include_chain (dirtmp
, dirtmp
);
2237 /* Advance past this name. */
2244 /* Return the address of the first character in S that equals C.
2245 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2246 Return 0 if there is no such character. Assume that C itself is not '\0'.
2247 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2248 but unfortunately memchr isn't autoconfigured yet. */
2256 char *p
= (char *) s
;
2258 char *q
= index (p
, c
);
2260 return (U_CHAR
*) q
;
2262 size_t l
= strlen (p
);
2272 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2273 before main CCCP processing. Name `pcp' is also in honor of the
2274 drugs the trigraph designers must have been on.
2276 Using an extra pass through the buffer takes a little extra time,
2277 but is infinitely less hairy than trying to handle trigraphs inside
2278 strings, etc. everywhere, and also makes sure that trigraphs are
2279 only translated in the top level of processing. */
2285 register U_CHAR c
, *fptr
, *bptr
, *sptr
, *lptr
;
2288 fptr
= bptr
= sptr
= buf
->buf
;
2289 lptr
= fptr
+ buf
->length
;
2290 while ((sptr
= index0 (sptr
, '?', (size_t) (lptr
- sptr
))) != NULL
) {
2327 len
= sptr
- fptr
- 2;
2329 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2330 C, this will be memmove (). */
2331 if (bptr
!= fptr
&& len
> 0)
2332 bcopy ((char *) fptr
, (char *) bptr
, len
);
2338 len
= buf
->length
- (fptr
- buf
->buf
);
2339 if (bptr
!= fptr
&& len
> 0)
2340 bcopy ((char *) fptr
, (char *) bptr
, len
);
2341 buf
->length
-= fptr
- bptr
;
2342 buf
->buf
[buf
->length
] = '\0';
2343 if (warn_trigraphs
&& fptr
!= bptr
)
2344 warning_with_line (0, "%lu trigraph(s) encountered",
2345 (unsigned long) (fptr
- bptr
) / 2);
2348 /* Warn about white space between backslash and end of line. */
2351 check_white_space (buf
)
2354 register U_CHAR
*sptr
= buf
->buf
;
2355 register U_CHAR
*lptr
= sptr
+ buf
->length
;
2356 register U_CHAR
*nptr
;
2359 nptr
= sptr
= buf
->buf
;
2360 lptr
= sptr
+ buf
->length
;
2362 (nptr
= index0 (nptr
, '\n', (size_t) (lptr
- nptr
))) != NULL
;
2364 register U_CHAR
*p
= nptr
;
2366 for (p
= nptr
; sptr
< p
; p
--) {
2367 if (! is_hor_space
[p
[-1]]) {
2368 if (p
[-1] == '\\' && p
!= nptr
)
2369 warning_with_line (line
,
2370 "`\\' followed by white space at end of line");
2377 /* Move all backslash-newline pairs out of embarrassing places.
2378 Exchange all such pairs following BP
2379 with any potentially-embarrassing characters that follow them.
2380 Potentially-embarrassing characters are / and *
2381 (because a backslash-newline inside a comment delimiter
2382 would cause it not to be recognized).
2383 We assume that *BP == '\\'. */
2389 register U_CHAR
*p
= bp
;
2391 /* First count the backslash-newline pairs here. */
2396 } while (*p
== '\\');
2398 if (*p
!= '/' && *p
!= '*')
2399 /* What follows the backslash-newlines is not embarrassing. */
2402 /* Copy all potentially embarrassing characters
2403 that follow the backslash-newline pairs
2404 down to where the pairs originally started. */
2407 while (*p
== '*' || *p
== '/');
2409 /* Now write the same number of pairs after the embarrassing chars. */
2416 /* Like newline_fix but for use within a directive-name.
2417 Move any backslash-newlines up past any following symbol constituents. */
2420 name_newline_fix (bp
)
2423 register U_CHAR
*p
= bp
;
2425 /* First count the backslash-newline pairs here. */
2430 } while (*p
== '\\');
2433 /* What follows the backslash-newlines is not embarrassing. */
2436 /* Copy all potentially embarrassing characters
2437 that follow the backslash-newline pairs
2438 down to where the pairs originally started. */
2441 while (is_idchar
[*p
]);
2443 /* Now write the same number of pairs after the embarrassing chars. */
2450 /* Look for lint commands in comments.
2452 When we come in here, ibp points into a comment. Limit is as one expects.
2453 scan within the comment -- it should start, after lwsp, with a lint command.
2454 If so that command is returned as a (constant) string.
2456 Upon return, any arg will be pointed to with argstart and will be
2457 arglen long. Note that we don't parse that arg since it will just
2458 be printed out again. */
2461 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2462 register U_CHAR
*ibp
;
2463 register U_CHAR
*limit
;
2464 U_CHAR
**argstart
; /* point to command arg */
2465 int *arglen
, *cmdlen
; /* how long they are */
2467 HOST_WIDE_INT linsize
;
2468 register U_CHAR
*numptr
; /* temp for arg parsing */
2472 SKIP_WHITE_SPACE (ibp
);
2474 if (ibp
>= limit
) return NULL
;
2476 linsize
= limit
- ibp
;
2478 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2479 if ((linsize
>= 10) && !bcmp (ibp
, "NOTREACHED", 10)) {
2481 return "NOTREACHED";
2483 if ((linsize
>= 8) && !bcmp (ibp
, "ARGSUSED", 8)) {
2487 if ((linsize
>= 11) && !bcmp (ibp
, "LINTLIBRARY", 11)) {
2489 return "LINTLIBRARY";
2491 if ((linsize
>= 7) && !bcmp (ibp
, "VARARGS", 7)) {
2493 ibp
+= 7; linsize
-= 7;
2494 if ((linsize
== 0) || ! isdigit (*ibp
)) return "VARARGS";
2496 /* OK, read a number */
2497 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && isdigit (*numptr
);
2499 *arglen
= numptr
- *argstart
;
2506 * The main loop of the program.
2508 * Read characters from the input stack, transferring them to the
2511 * Macros are expanded and push levels on the input stack.
2512 * At the end of such a level it is popped off and we keep reading.
2513 * At the end of any other kind of level, we return.
2514 * #-directives are handled, except within macros.
2516 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2517 * and insert them when appropriate. This is set while scanning macro
2518 * arguments before substitution. It is zero when scanning for final output.
2519 * There are two types of Newline markers:
2520 * * Newline - follows a macro name that was not expanded
2521 * because it appeared inside an expansion of the same macro.
2522 * This marker prevents future expansion of that identifier.
2523 * When the input is rescanned into the final output, these are deleted.
2524 * These are also deleted by ## concatenation.
2525 * * Newline Space (or Newline and any other whitespace character)
2526 * stands for a place that tokens must be separated or whitespace
2527 * is otherwise desirable, but where the ANSI standard specifies there
2528 * is no whitespace. This marker turns into a Space (or whichever other
2529 * whitespace char appears in the marker) in the final output,
2530 * but it turns into nothing in an argument that is stringified with #.
2531 * Such stringified arguments are the only place where the ANSI standard
2532 * specifies with precision that whitespace may not appear.
2534 * During this function, IP->bufp is kept cached in IBP for speed of access.
2535 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2536 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2537 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2538 * explicitly, and before RECACHE, since RECACHE uses OBP.
2542 rescan (op
, output_marks
)
2546 /* Character being scanned in main loop. */
2549 /* Length of pending accumulated identifier. */
2550 register int ident_length
= 0;
2552 /* Hash code of pending accumulated identifier. */
2553 register int hash
= 0;
2555 /* Current input level (&instack[indepth]). */
2558 /* Pointer for scanning input. */
2559 register U_CHAR
*ibp
;
2561 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2562 register U_CHAR
*limit
;
2564 /* Pointer for storing output. */
2565 register U_CHAR
*obp
;
2567 /* REDO_CHAR is nonzero if we are processing an identifier
2568 after backing up over the terminating character.
2569 Sometimes we process an identifier without backing up over
2570 the terminating character, if the terminating character
2571 is not special. Backing up is done so that the terminating character
2572 will be dispatched on again once the identifier is dealt with. */
2575 /* 1 if within an identifier inside of which a concatenation
2576 marker (Newline -) has been seen. */
2577 int concatenated
= 0;
2579 /* While scanning a comment or a string constant,
2580 this records the line it started on, for error messages. */
2583 /* Record position of last `real' newline. */
2584 U_CHAR
*beg_of_line
;
2586 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2589 do { ip->macro->type = T_MACRO; \
2590 if (ip->free_ptr) free (ip->free_ptr); \
2591 --indepth; } while (0)
2593 /* Reload `rescan's local variables that describe the current
2594 level of the input stack. */
2597 do { ip = &instack[indepth]; \
2599 limit = ip->buf + ip->length; \
2601 check_expand (op, limit - ibp); \
2603 obp = op->bufp; } while (0)
2605 if (no_output
&& instack
[indepth
].fname
!= 0)
2606 skip_if_group (&instack
[indepth
], 1, NULL
);
2613 /* Our caller must always put a null after the end of
2614 the input at each input stack level. */
2624 if (*ibp
== '\n' && !ip
->macro
) {
2625 /* At the top level, always merge lines ending with backslash-newline,
2626 even in middle of identifier. But do not merge lines in a macro,
2627 since backslash might be followed by a newline-space marker. */
2630 --obp
; /* remove backslash from obuf */
2633 /* If ANSI, backslash is just another character outside a string. */
2636 /* Otherwise, backslash suppresses specialness of following char,
2637 so copy it here to prevent the switch from seeing it.
2638 But first get any pending identifier processed. */
2639 if (ident_length
> 0)
2646 if (ident_length
|| ip
->macro
|| traditional
)
2648 while (*ibp
== '\\' && ibp
[1] == '\n') {
2654 /* Treat this %: digraph as if it were #. */
2658 if (assertions_flag
) {
2661 /* Copy #foo (bar lose) without macro expansion. */
2662 obp
[-1] = '#'; /* In case it was '%'. */
2663 SKIP_WHITE_SPACE (ibp
);
2664 while (is_idchar
[*ibp
])
2666 SKIP_WHITE_SPACE (ibp
);
2669 skip_paren_group (ip
);
2670 bcopy ((char *) ibp
, (char *) obp
, ip
->bufp
- ibp
);
2671 obp
+= ip
->bufp
- ibp
;
2677 /* If this is expanding a macro definition, don't recognize
2678 preprocessing directives. */
2681 /* If this is expand_into_temp_buffer,
2682 don't recognize them either. Warn about them
2683 only after an actual newline at this level,
2684 not at the beginning of the input level. */
2686 if (ip
->buf
!= beg_of_line
)
2687 warning ("preprocessing directive not recognized within macro arg");
2694 /* # keyword: a # must be first nonblank char on the line */
2695 if (beg_of_line
== 0)
2700 /* Scan from start of line, skipping whitespace, comments
2701 and backslash-newlines, and see if we reach this #.
2702 If not, this # is not special. */
2704 /* If -traditional, require # to be at beginning of line. */
2707 if (is_hor_space
[*bp
])
2709 else if (*bp
== '\\' && bp
[1] == '\n')
2711 else if (*bp
== '/' && bp
[1] == '*') {
2725 #ifdef MULTIBYTE_CHARS
2727 length
= local_mblen (bp
, limit
- bp
);
2735 /* There is no point in trying to deal with C++ // comments here,
2736 because if there is one, then this # must be part of the
2737 comment and we would never reach here. */
2743 while (bp
[1] == '\\' && bp
[2] == '\n')
2747 /* %: appears at start of line; skip past the ':' too. */
2756 /* This # can start a directive. */
2758 --obp
; /* Don't copy the '#' */
2762 if (! handle_directive (ip
, op
)) {
2766 /* Not a known directive: treat it as ordinary text.
2767 IP, OP, IBP, etc. have not been changed. */
2768 if (no_output
&& instack
[indepth
].fname
) {
2769 /* If not generating expanded output,
2770 what we do with ordinary text is skip it.
2771 Discard everything until next # directive. */
2772 skip_if_group (&instack
[indepth
], 1, 0);
2777 *obp
++ = '#'; /* Copy # (even if it was originally %:). */
2778 /* Don't expand an identifier that could be a macro directive.
2779 (Section 3.8.3 of the ANSI C standard) */
2780 SKIP_WHITE_SPACE (ibp
);
2781 if (is_idstart
[*ibp
])
2784 while (is_idchar
[*ibp
])
2792 /* A # directive has been successfully processed. */
2793 /* If not generating expanded output, ignore everything until
2794 next # directive. */
2795 if (no_output
&& instack
[indepth
].fname
)
2796 skip_if_group (&instack
[indepth
], 1, 0);
2802 case '\"': /* skip quoted string */
2804 /* A single quoted string is treated like a double -- some
2805 programs (e.g., troff) are perverse this way */
2807 /* Handle any pending identifier;
2808 but the L in L'...' or L"..." is not an identifier. */
2810 if (! (ident_length
== 1 && hash
== HASHSTEP (0, 'L')))
2812 ident_length
= hash
= 0;
2815 start_line
= ip
->lineno
;
2817 /* Skip ahead to a matching quote. */
2821 if (ip
->macro
!= 0) {
2822 /* try harder: this string crosses a macro expansion boundary.
2823 This can happen naturally if -traditional.
2824 Otherwise, only -D can make a macro with an unmatched quote. */
2830 error_with_line (line_for_error (start_line
),
2831 "unterminated string or character constant");
2832 if (multiline_string_line
) {
2833 error_with_line (multiline_string_line
,
2834 "possible real start of unterminated constant");
2835 multiline_string_line
= 0;
2843 if (warn_white_space
&& ip
->fname
&& is_hor_space
[ibp
[-2]])
2844 warning ("white space at end of line in string");
2847 /* Traditionally, end of line ends a string constant with no error.
2848 So exit the loop and record the new line. */
2854 error_with_line (line_for_error (start_line
),
2855 "unterminated character constant");
2858 if (multiline_string_line
== 0) {
2860 pedwarn_with_line (line_for_error (start_line
),
2861 "string constant runs past end of line");
2862 multiline_string_line
= ip
->lineno
- 1;
2868 /* Backslash newline is replaced by nothing at all, but
2869 keep the line counts correct. But if we are reading
2870 from a macro, keep the backslash newline, since backslash
2871 newlines have already been processed. */
2880 /* ANSI stupidly requires that in \\ the second \
2881 is *not* prevented from combining with a newline. */
2883 while (*ibp
== '\\' && ibp
[1] == '\n') {
2899 #ifdef MULTIBYTE_CHARS
2904 length
= local_mblen (ibp
, limit
- ibp
);
2908 bcopy (ibp
, obp
, length
);
2928 && !(cplusplus_comments
&& *ibp
== '/'))
2934 /* C++ style comment... */
2935 start_line
= ip
->lineno
;
2937 /* Comments are equivalent to spaces. */
2938 if (! put_out_comments
)
2942 U_CHAR
*before_bp
= ibp
;
2944 while (++ibp
< limit
) {
2946 if (ibp
[-1] != '\\') {
2947 if (put_out_comments
) {
2948 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
2949 obp
+= ibp
- before_bp
;
2954 warning ("multiline `//' comment");
2956 /* Copy the newline into the output buffer, in order to
2957 avoid the pain of a #line every time a multiline comment
2959 if (!put_out_comments
)
2965 #ifdef MULTIBYTE_CHARS
2967 length
= local_mblen (ibp
, limit
- ibp
);
2969 ibp
+= (length
- 1);
2977 /* Ordinary C comment. Skip it, optionally copying it to output. */
2979 start_line
= ip
->lineno
;
2981 ++ibp
; /* Skip the star. */
2983 /* If this cpp is for lint, we peek inside the comments: */
2987 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2989 if (lintcmd
!= NULL
) {
2991 check_expand (op
, cmdlen
+ arglen
+ 14);
2993 /* I believe it is always safe to emit this newline: */
2995 bcopy ("#pragma lint ", (char *) obp
, 13);
2997 bcopy (lintcmd
, (char *) obp
, cmdlen
);
3002 bcopy (argbp
, (char *) obp
, arglen
);
3006 /* OK, now bring us back to the state we were in before we entered
3007 this branch. We need #line because the #pragma's newline always
3008 messes up the line count. */
3010 output_line_directive (ip
, op
, 0, same_file
);
3011 check_expand (op
, limit
- ibp
+ 2);
3017 /* Comments are equivalent to spaces.
3018 Note that we already output the slash; we might not want it.
3019 For -traditional, a comment is equivalent to nothing. */
3020 if (! put_out_comments
) {
3030 U_CHAR
*before_bp
= ibp
;
3035 if (ibp
[-2] == '/' && warn_comments
)
3036 warning ("`/*' within comment");
3045 /* Copy the newline into the output buffer, in order to
3046 avoid the pain of a #line every time a multiline comment
3048 if (!put_out_comments
)
3055 error_with_line (line_for_error (start_line
),
3056 "unterminated comment");
3060 #ifdef MULTIBYTE_CHARS
3064 length
= local_mblen (ibp
, limit
- ibp
);
3066 ibp
+= (length
- 1);
3075 if (put_out_comments
) {
3076 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
3077 obp
+= ibp
- before_bp
;
3083 if (! is_idchar
['$'])
3086 pedwarn ("`$' in identifier");
3089 case '0': case '1': case '2': case '3': case '4':
3090 case '5': case '6': case '7': case '8': case '9':
3091 /* If digit is not part of identifier, it starts a number,
3092 which means that following letters are not an identifier.
3093 "0x5" does not refer to an identifier "x5".
3094 So copy all alphanumerics that follow without accumulating
3095 as an identifier. Periods also, for sake of "3.e7". */
3097 if (ident_length
== 0) {
3100 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3106 if (!is_idchar
[c
] && c
!= '.') {
3111 /* A sign can be part of a preprocessing number
3112 if it follows an `e' or `p'. */
3113 if (c
== 'e' || c
== 'E' || c
== 'p' || c
== 'P') {
3115 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3120 if (*ibp
== '+' || *ibp
== '-') {
3122 /* But traditional C does not let the token go past the sign,
3123 and C89 does not allow `p'. */
3124 if (traditional
|| (c89
&& (c
== 'p' || c
== 'P')))
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':
3139 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3140 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3141 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3142 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3146 /* Compute step of hash function, to avoid a proc call on every token */
3147 hash
= HASHSTEP (hash
, c
);
3151 if (ip
->fname
== 0 && *ibp
== '-') {
3152 /* Newline - inhibits expansion of preceding token.
3153 If expanding a macro arg, we keep the newline -.
3154 In final output, it is deleted.
3155 We recognize Newline - in macro bodies and macro args. */
3156 if (! concatenated
) {
3161 if (!output_marks
) {
3164 /* If expanding a macro arg, keep the newline -. */
3170 /* If reprocessing a macro expansion, newline is a special marker. */
3171 else if (ip
->macro
!= 0) {
3172 /* Newline White is a "funny space" to separate tokens that are
3173 supposed to be separate but without space between.
3174 Here White means any whitespace character.
3175 Newline - marks a recursive macro use that is not
3176 supposed to be expandable. */
3178 if (is_space
[*ibp
]) {
3179 /* Newline Space does not prevent expansion of preceding token
3180 so expand the preceding token and then come back. */
3181 if (ident_length
> 0)
3184 /* If generating final output, newline space makes a space. */
3185 if (!output_marks
) {
3187 /* And Newline Newline makes a newline, so count it. */
3188 if (obp
[-1] == '\n')
3191 /* If expanding a macro arg, keep the newline space.
3192 If the arg gets stringified, newline space makes nothing. */
3195 } else abort (); /* Newline followed by something random? */
3199 /* If there is a pending identifier, handle it and come back here. */
3200 if (ident_length
> 0)
3205 /* Update the line counts and output a #line if necessary. */
3208 if (ip
->lineno
!= op
->lineno
) {
3210 output_line_directive (ip
, op
, 1, same_file
);
3211 check_expand (op
, limit
- ibp
);
3216 /* Come here either after (1) a null character that is part of the input
3217 or (2) at the end of the input, because there is a null there. */
3220 /* Our input really contains a null character. */
3224 /* At end of a macro-expansion level, pop it and read next level. */
3225 if (ip
->macro
!= 0) {
3228 /* If traditional, and we have an identifier that ends here,
3229 process it now, so we get the right error for recursion. */
3230 if (traditional
&& ident_length
3231 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
3240 /* If we don't have a pending identifier,
3241 return at end of input. */
3242 if (ident_length
== 0) {
3250 /* If we do have a pending identifier, just consider this null
3251 a special character and arrange to dispatch on it again.
3252 The second time, IDENT_LENGTH will be zero so we will return. */
3258 /* Handle the case of a character such as /, ', " or null
3259 seen following an identifier. Back over it so that
3260 after the identifier is processed the special char
3261 will be dispatched on again. */
3271 if (ident_length
> 0) {
3272 register HASHNODE
*hp
;
3274 /* We have just seen an identifier end. If it's a macro, expand it.
3276 IDENT_LENGTH is the length of the identifier
3277 and HASH is its hash code.
3279 The identifier has already been copied to the output,
3280 so if it is a macro we must remove it.
3282 If REDO_CHAR is 0, the char that terminated the identifier
3283 has been skipped in the output and the input.
3284 OBP-IDENT_LENGTH-1 points to the identifier.
3285 If the identifier is a macro, we must back over the terminator.
3287 If REDO_CHAR is 1, the terminating char has already been
3288 backed over. OBP-IDENT_LENGTH points to the identifier. */
3290 if (!pcp_outfile
|| pcp_inside_if
) {
3291 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
3294 if (hp
->length
== ident_length
) {
3295 int obufp_before_macroname
;
3296 int op_lineno_before_macroname
;
3297 register int i
= ident_length
;
3298 register U_CHAR
*p
= hp
->name
;
3299 register U_CHAR
*q
= obp
- i
;
3305 do { /* All this to avoid a strncmp () */
3310 /* We found a use of a macro name.
3311 see if the context shows it is a macro call. */
3313 /* Back up over terminating character if not already done. */
3319 /* Save this as a displacement from the beginning of the output
3320 buffer. We can not save this as a position in the output
3321 buffer, because it may get realloc'ed by RECACHE. */
3322 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
3323 op_lineno_before_macroname
= op
->lineno
;
3325 if (hp
->type
== T_PCSTRING
) {
3326 pcstring_used (hp
); /* Mark the definition of this key
3327 as needed, ensuring that it
3329 break; /* Exit loop, since the key cannot have a
3330 definition any longer. */
3333 /* Record whether the macro is disabled. */
3334 disabled
= hp
->type
== T_DISABLED
;
3336 /* This looks like a macro ref, but if the macro was disabled,
3337 just copy its name and put in a marker if requested. */
3341 /* This error check caught useful cases such as
3342 #define foo(x,y) bar (x (y,0), y)
3345 error ("recursive use of macro `%s'", hp
->name
);
3349 check_expand (op
, limit
- ibp
+ 2);
3356 /* If macro wants an arglist, verify that a '(' follows.
3357 first skip all whitespace, copying it to the output
3358 after the macro name. Then, if there is no '(',
3359 decide this is not a macro call and leave things that way. */
3360 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
3361 && hp
->value
.defn
->nargs
>= 0)
3363 U_CHAR
*old_ibp
= ibp
;
3364 U_CHAR
*old_obp
= obp
;
3365 int old_iln
= ip
->lineno
;
3366 int old_oln
= op
->lineno
;
3369 /* Scan forward over whitespace, copying it to the output. */
3370 if (ibp
== limit
&& ip
->macro
!= 0) {
3375 old_iln
= ip
->lineno
;
3376 old_oln
= op
->lineno
;
3378 else if (is_space
[*ibp
]) {
3380 if (ibp
[-1] == '\n') {
3381 if (ip
->macro
== 0) {
3382 /* Newline in a file. Count it. */
3385 } else if (!output_marks
) {
3386 /* A newline mark, and we don't want marks
3387 in the output. If it is newline-hyphen,
3388 discard it entirely. Otherwise, it is
3389 newline-whitechar, so keep the whitechar. */
3399 /* A newline mark; copy both chars to the output. */
3406 else if (*ibp
== '/') {
3407 /* If a comment, copy it unchanged or discard it. */
3409 newline_fix (ibp
+ 1);
3410 if (ibp
[1] == '*') {
3411 if (put_out_comments
) {
3414 } else if (! traditional
) {
3417 for (ibp
+= 2; ibp
< limit
; ibp
++) {
3418 /* We need not worry about newline-marks,
3419 since they are never found in comments. */
3420 if (ibp
[0] == '*') {
3422 newline_fix (ibp
+ 1);
3423 if (ibp
[1] == '/') {
3425 if (put_out_comments
) {
3432 else if (*ibp
== '\n') {
3433 /* Newline in a file. Count it. */
3439 #ifdef MULTIBYTE_CHARS
3441 length
= local_mblen (ibp
, limit
- ibp
);
3444 if (put_out_comments
)
3446 bcopy (ibp
, obp
, length
- 1);
3449 ibp
+= (length
- 1);
3453 if (put_out_comments
)
3456 } else if (ibp
[1] == '/' && cplusplus_comments
) {
3457 if (put_out_comments
) {
3460 } else if (! traditional
) {
3463 for (ibp
+= 2; ; ibp
++)
3467 if (ibp
[-1] != '\\')
3472 #ifdef MULTIBYTE_CHARS
3474 length
= local_mblen (ibp
, limit
- ibp
);
3477 if (put_out_comments
)
3479 bcopy (ibp
, obp
, length
- 1);
3482 ibp
+= (length
- 1);
3486 if (put_out_comments
)
3492 else if (ibp
[0] == '\\' && ibp
[1] == '\n') {
3499 /* It isn't a macro call.
3500 Put back the space that we just skipped. */
3503 ip
->lineno
= old_iln
;
3504 op
->lineno
= old_oln
;
3505 /* Exit the for loop. */
3510 /* This is now known to be a macro call.
3511 Discard the macro name from the output,
3512 along with any following whitespace just copied,
3513 but preserve newlines if not outputting marks since this
3514 is more likely to do the right thing with line numbers. */
3515 obp
= op
->buf
+ obufp_before_macroname
;
3517 op
->lineno
= op_lineno_before_macroname
;
3519 int newlines
= op
->lineno
- op_lineno_before_macroname
;
3520 while (0 < newlines
--)
3524 /* Prevent accidental token-pasting with a character
3525 before the macro call. */
3526 if (!traditional
&& obp
!= op
->buf
) {
3528 case '!': case '%': case '&': case '*':
3529 case '+': case '-': case '.': case '/':
3530 case ':': case '<': case '=': case '>':
3532 /* If we are expanding a macro arg, make a newline marker
3533 to separate the tokens. If we are making real output,
3534 a plain space will do. */
3541 /* Expand the macro, reading arguments as needed,
3542 and push the expansion on the input stack. */
3545 macroexpand (hp
, op
);
3547 /* Reexamine input stack, since macroexpand has pushed
3548 a new level on it. */
3555 } /* End hash-table-search loop */
3557 ident_length
= hash
= 0; /* Stop collecting identifier */
3560 } /* End if (ident_length > 0) */
3562 } /* End per-char loop */
3564 /* Come here to return -- but first give an error message
3565 if there was an unterminated successful conditional. */
3567 if (if_stack
!= ip
->if_stack
)
3571 switch (if_stack
->type
)
3592 error_with_line (line_for_error (if_stack
->lineno
),
3593 "unterminated `#%s' conditional", str
);
3595 if_stack
= ip
->if_stack
;
3599 * Rescan a string into a temporary buffer and return the result
3600 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3602 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3603 * and insert such markers when appropriate. See `rescan' for details.
3604 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3605 * before substitution; it is 0 for other uses.
3608 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
3609 U_CHAR
*buf
, *limit
;
3610 int output_marks
, assertions
;
3612 register FILE_BUF
*ip
;
3614 int length
= limit
- buf
;
3616 int odepth
= indepth
;
3617 int save_assertions_flag
= assertions_flag
;
3619 assertions_flag
= assertions
;
3624 /* Set up the input on the input stack. */
3626 buf1
= (U_CHAR
*) alloca (length
+ 1);
3628 register U_CHAR
*p1
= buf
;
3629 register U_CHAR
*p2
= buf1
;
3636 /* Set up to receive the output. */
3638 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3639 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3644 CHECK_DEPTH ({return obuf
;});
3648 ip
= &instack
[indepth
];
3650 ip
->nominal_fname
= 0;
3651 ip
->nominal_fname_len
= 0;
3653 ip
->system_header_p
= 0;
3656 ip
->length
= length
;
3657 ip
->buf
= ip
->bufp
= buf1
;
3658 ip
->if_stack
= if_stack
;
3660 ip
->lineno
= obuf
.lineno
= 1;
3662 /* Scan the input, create the output. */
3663 rescan (&obuf
, output_marks
);
3665 /* Pop input stack to original state. */
3668 if (indepth
!= odepth
)
3671 assertions_flag
= save_assertions_flag
;
3676 * Process a # directive. Expects IP->bufp to point after the '#', as in
3677 * `#define foo bar'. Passes to the directive handler
3678 * (do_define, do_include, etc.): the addresses of the 1st and
3679 * last chars of the directive (starting immediately after the #
3680 * keyword), plus op and the keyword table pointer. If the directive
3681 * contains comments it is copied into a temporary buffer sans comments
3682 * and the temporary buffer is passed to the directive handler instead.
3683 * Likewise for backslash-newlines.
3685 * Returns nonzero if this was a known # directive.
3686 * Otherwise, returns zero, without advancing the input pointer.
3690 handle_directive (ip
, op
)
3693 register U_CHAR
*bp
, *cp
;
3694 register struct directive
*kt
;
3695 register int ident_length
;
3698 /* Nonzero means we must copy the entire directive
3699 to get rid of comments or backslash-newlines. */
3700 int copy_directive
= 0;
3702 U_CHAR
*ident
, *after_ident
;
3706 /* Record where the directive started. do_xifdef needs this. */
3707 directive_start
= bp
- 1;
3709 /* Skip whitespace and \-newline. */
3711 if (is_hor_space
[*bp
]) {
3712 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3713 pedwarn_strange_white_space (*bp
);
3715 } else if (*bp
== '/') {
3717 newline_fix (bp
+ 1);
3718 if (! (bp
[1] == '*' || (cplusplus_comments
&& bp
[1] == '/')))
3721 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3723 } else if (*bp
== '\\' && bp
[1] == '\n') {
3724 bp
+= 2; ip
->lineno
++;
3728 /* Now find end of directive name.
3729 If we encounter a backslash-newline, exchange it with any following
3730 symbol-constituents so that we end up with a contiguous name. */
3738 name_newline_fix (cp
);
3744 ident_length
= cp
- bp
;
3748 /* A line of just `#' becomes blank. */
3750 if (ident_length
== 0 && *after_ident
== '\n') {
3751 ip
->bufp
= after_ident
;
3755 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3757 while (is_idchar
[*p
]) {
3758 if (*p
< '0' || *p
> '9')
3762 /* Handle # followed by a line number. */
3763 if (p
!= ident
&& !is_idchar
[*p
]) {
3764 static struct directive line_directive_table
[] = {
3765 { 4, do_line
, "line", T_LINE
},
3768 pedwarn ("`#' followed by integer");
3769 after_ident
= ident
;
3770 kt
= line_directive_table
;
3774 /* Avoid error for `###' and similar cases unless -pedantic. */
3776 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3778 if (pedantic
&& !lang_asm
)
3779 warning ("invalid preprocessing directive");
3785 error ("invalid preprocessing directive name");
3791 * Decode the keyword and call the appropriate expansion
3792 * routine, after moving the input pointer up to the next line.
3794 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3795 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3796 register U_CHAR
*buf
;
3797 register U_CHAR
*limit
;
3800 int *already_output
;
3802 /* Nonzero means do not delete comments within the directive.
3803 #define needs this when -traditional. */
3808 limit
= ip
->buf
+ ip
->length
;
3811 keep_comments
= traditional
&& kt
->type
== T_DEFINE
;
3812 /* #import is defined only in Objective C, or when on the NeXT. */
3813 if (kt
->type
== T_IMPORT
3814 && !(objc
|| lookup ((U_CHAR
*) "__NeXT__", -1, -1)))
3817 /* Find the end of this directive (first newline not backslashed
3818 and not in a string or comment).
3819 Set COPY_DIRECTIVE if the directive must be copied
3820 (it contains a backslash-newline or a comment). */
3822 buf
= bp
= after_ident
;
3823 while (bp
< limit
) {
3824 register U_CHAR c
= *bp
++;
3831 } else if (traditional
&& bp
< limit
)
3836 /* "..." is special for #include. */
3837 if (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)) {
3838 while (bp
< limit
&& *bp
!= '\n') {
3843 if (*bp
== '\\' && bp
[1] == '\n') {
3854 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3855 /* Don't bother calling the directive if we already got an error
3856 message due to unterminated string. Skip everything and pretend
3857 we called the directive. */
3860 /* Traditional preprocessing permits unterminated strings. */
3869 /* <...> is special for #include. */
3871 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3873 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3874 if (*bp
== '\\' && bp
[1] == '\n') {
3887 || (cplusplus_comments
&& *bp
== '/')) {
3888 U_CHAR
*obp
= bp
- 1;
3890 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3892 /* No need to copy the directive because of a comment at the end;
3893 just don't include the comment in the directive. */
3894 if (!put_out_comments
) {
3896 for (p
= bp
; *p
== ' ' || *p
== '\t'; p
++)
3903 /* Don't remove the comments if -traditional. */
3904 if (! keep_comments
)
3913 pedwarn_strange_white_space (c
);
3917 --bp
; /* Point to the newline */
3925 resume_p
= ip
->bufp
;
3926 /* BP is the end of the directive.
3927 RESUME_P is the next interesting data after the directive.
3928 A comment may come between. */
3930 /* If a directive should be copied through, and -C was given,
3931 pass it through before removing comments. */
3932 if (!no_output
&& put_out_comments
3933 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
3934 ? dump_macros
== dump_definitions
3935 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
3936 : kt
->type
== T_PRAGMA
)) {
3939 /* Output directive name. */
3940 check_expand (op
, kt
->length
+ 2);
3941 /* Make sure # is at the start of a line */
3942 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3947 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3948 op
->bufp
+= kt
->length
;
3950 /* Output arguments. */
3952 check_expand (op
, len
);
3953 bcopy (buf
, (char *) op
->bufp
, len
);
3955 /* Take account of any (escaped) newlines just output. */
3957 if (buf
[len
] == '\n')
3960 already_output
= &junk
;
3961 } /* Don't we need a newline or #line? */
3963 if (copy_directive
) {
3964 register U_CHAR
*xp
= buf
;
3965 /* Need to copy entire directive into temp buffer before dispatching */
3967 /* room for directive plus some slop */
3968 cp
= (U_CHAR
*) alloca (2 * (bp
- buf
) + 5);
3971 /* Copy to the new buffer, deleting comments
3972 and backslash-newlines (and whitespace surrounding the latter
3973 if outside of char and string constants). */
3976 register U_CHAR c
= *xp
++;
3981 abort (); /* A bare newline should never part of the line. */
3984 /* <...> is special for #include. */
3986 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3988 while (xp
< bp
&& c
!= '>') {
3990 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
4001 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
4002 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
4004 SKIP_WHITE_SPACE (xp
);
4005 } else if (is_hor_space
[*xp
]) {
4007 SKIP_WHITE_SPACE (xp
);
4009 } else if (traditional
&& xp
< bp
) {
4017 register U_CHAR
*bp1
4018 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
4019 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4021 if ((*cp
++ = *xp
++) == '\n') {
4035 || (cplusplus_comments
&& *xp
== '/')) {
4037 /* If we already copied the directive through,
4038 already_output != 0 prevents outputting comment now. */
4039 skip_to_end_of_comment (ip
, already_output
, 0);
4041 while (xp
!= ip
->bufp
)
4043 /* Delete or replace the slash. */
4044 else if (traditional
)
4053 /* Null-terminate the copy. */
4059 ip
->bufp
= resume_p
;
4061 /* Some directives should be written out for cc1 to process,
4062 just as if they were not defined. And sometimes we're copying
4063 directives through. */
4065 if (!no_output
&& already_output
== 0
4066 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
4067 ? (int) dump_names
<= (int) dump_macros
4068 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
4069 : kt
->type
== T_PRAGMA
)) {
4072 /* Output directive name. */
4073 check_expand (op
, kt
->length
+ 1);
4075 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
4076 op
->bufp
+= kt
->length
;
4078 if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
4079 /* Output `#define name' only. */
4082 SKIP_WHITE_SPACE (xp
);
4084 while (is_idchar
[*xp
]) xp
++;
4086 check_expand (op
, len
+ 1);
4088 bcopy (yp
, (char *) op
->bufp
, len
);
4090 /* Output entire directive. */
4092 check_expand (op
, len
);
4093 bcopy (buf
, (char *) op
->bufp
, len
);
4098 /* Call the appropriate directive handler. buf now points to
4099 either the appropriate place in the input buffer, or to
4100 the temp buffer if it was necessary to make one. cp
4101 points to the first char after the contents of the (possibly
4102 copied) directive, in either case. */
4103 (*kt
->func
) (buf
, cp
, op
, kt
);
4104 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
4110 /* It is deliberate that we don't warn about undefined directives.
4111 That is the responsibility of cc1. */
4118 static struct tm tmbuf
;
4119 if (! tmbuf
.tm_mday
) {
4120 time_t t
= time ((time_t *) 0);
4121 struct tm
*tm
= localtime (&t
);
4125 /* Use 0000-01-01 00:00:00 if local time is not available. */
4126 tmbuf
.tm_year
= -1900;
4133 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4134 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4138 * expand things like __FILE__. Place the expansion into the output
4139 * buffer *without* rescanning.
4143 special_symbol (hp
, op
)
4150 FILE_BUF
*ip
= NULL
;
4153 int paren
= 0; /* For special `defined' keyword */
4155 if (pcp_outfile
&& pcp_inside_if
4156 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4157 error ("Predefined macro `%s' used inside `#if' during precompilation",
4160 for (i
= indepth
; i
>= 0; i
--)
4161 if (instack
[i
].fname
!= NULL
) {
4166 error ("cccp error: not in any file?!");
4167 return; /* the show must go on */
4174 FILE_BUF
*p
= hp
->type
== T_FILE
? ip
: &instack
[0];
4175 char *string
= p
->nominal_fname
;
4179 size_t string_len
= p
->nominal_fname_len
;
4180 buf
= (char *) alloca (3 + 4 * string_len
);
4181 quote_string (buf
, string
, string_len
);
4189 case T_INCLUDE_LEVEL
:
4191 for (i
= indepth
; i
>= 0; i
--)
4192 if (instack
[i
].fname
!= NULL
)
4195 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
4196 sprintf (buf
, "%d", true_indepth
- 1);
4200 buf
= (char *) alloca (3 + strlen (version_string
));
4201 sprintf (buf
, "\"%s\"", version_string
);
4204 #ifndef NO_BUILTIN_SIZE_TYPE
4210 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4211 case T_PTRDIFF_TYPE
:
4220 case T_USER_LABEL_PREFIX_TYPE
:
4221 buf
= USER_LABEL_PREFIX
;
4224 case T_REGISTER_PREFIX_TYPE
:
4225 buf
= REGISTER_PREFIX
;
4228 case T_IMMEDIATE_PREFIX_TYPE
:
4229 buf
= IMMEDIATE_PREFIX
;
4233 buf
= hp
->value
.cpval
;
4234 #ifdef STDC_0_IN_SYSTEM_HEADERS
4235 if (ip
->system_header_p
4236 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
4237 && !lookup ((U_CHAR
*) "__STRICT_ANSI__", -1, -1))
4240 if (pcp_inside_if
&& pcp_outfile
)
4241 /* Output a precondition for this macro use */
4242 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4246 buf
= (char *) alloca (10);
4247 sprintf (buf
, "%d", ip
->lineno
);
4252 buf
= (char *) alloca (20);
4253 timebuf
= timestamp ();
4254 if (hp
->type
== T_DATE
)
4255 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4256 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4258 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
4262 case T_SPEC_DEFINED
:
4263 buf
= " 0 "; /* Assume symbol is not defined */
4264 ip
= &instack
[indepth
];
4265 SKIP_WHITE_SPACE (ip
->bufp
);
4266 if (*ip
->bufp
== '(') {
4268 ip
->bufp
++; /* Skip over the paren */
4269 SKIP_WHITE_SPACE (ip
->bufp
);
4272 if (!is_idstart
[*ip
->bufp
])
4274 if (ip
->bufp
[0] == 'L' && (ip
->bufp
[1] == '\'' || ip
->bufp
[1] == '"'))
4276 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4277 if (pcp_outfile
&& pcp_inside_if
4278 && (hp
->type
== T_CONST
4279 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4280 /* Output a precondition for this macro use. */
4281 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4285 if (pcp_outfile
&& pcp_inside_if
) {
4286 /* Output a precondition for this macro use */
4287 U_CHAR
*cp
= ip
->bufp
;
4288 fprintf (pcp_outfile
, "#undef ");
4289 while (is_idchar
[*cp
]) /* Ick! */
4290 fputc (*cp
++, pcp_outfile
);
4291 putc ('\n', pcp_outfile
);
4293 while (is_idchar
[*ip
->bufp
])
4295 SKIP_WHITE_SPACE (ip
->bufp
);
4297 if (*ip
->bufp
!= ')')
4305 error ("`defined' without an identifier");
4309 error ("cccp error: invalid special hash type"); /* time for gdb */
4313 check_expand (op
, len
);
4314 bcopy (buf
, (char *) op
->bufp
, len
);
4321 /* Routines to handle #directives */
4323 /* Handle #include and #import.
4324 This function expects to see "fname" or <fname> on the input. */
4327 do_include (buf
, limit
, op
, keyword
)
4328 U_CHAR
*buf
, *limit
;
4330 struct directive
*keyword
;
4332 U_CHAR
*importing
= keyword
->type
== T_IMPORT
? (U_CHAR
*) "" : (U_CHAR
*) 0;
4333 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4334 static int import_warning
= 0;
4335 char *fname
; /* Dynamically allocated fname buffer */
4338 char *fbeg
, *fend
; /* Beginning and end of fname */
4341 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4342 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4343 struct file_name_list
*searchptr
= 0;
4346 int f
= -3; /* file number */
4347 struct include_file
*inc
= 0;
4349 int retried
= 0; /* Have already tried macro
4350 expanding the include line*/
4351 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4353 int vaxc_include
= 0; /* 1 for token without punctuation */
4360 if (pedantic
&& !instack
[indepth
].system_header_p
)
4363 pedwarn ("ANSI C does not allow `#import'");
4365 pedwarn ("ANSI C does not allow `#include_next'");
4368 if (importing
&& warn_import
&& !inhibit_warnings
4369 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4371 warning ("using `#import' is not recommended");
4372 notice ("The fact that a certain header file need not be processed more than once\n\
4373 should be indicated in the header file, not where it is used.\n\
4374 The best way to do this is with a conditional of this form:\n\
4376 #ifndef _FOO_H_INCLUDED\n\
4377 #define _FOO_H_INCLUDED\n\
4378 ... <real contents of file> ...\n\
4379 #endif /* Not _FOO_H_INCLUDED */\n\
4381 Then users can use `#include' any number of times.\n\
4382 GNU C automatically avoids processing the file more than once\n\
4383 when it is equipped with such a conditional.\n");
4389 SKIP_WHITE_SPACE (fin
);
4390 /* Discard trailing whitespace so we can easily see
4391 if we have parsed all the significant chars we were given. */
4392 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4393 fbeg
= fend
= (char *) alloca (limit
- fin
);
4399 /* Copy the operand text, concatenating the strings. */
4404 goto invalid_include_file_name
;
4412 /* If not at the end, there had better be another string. */
4413 /* Skip just horiz space, and don't go past limit. */
4414 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4415 if (fin
!= limit
&& *fin
== '\"')
4422 /* We have "filename". Figure out directory this source
4423 file is coming from and put it on the front of the list. */
4425 /* If -I- was specified, don't search current dir, only spec'd ones. */
4426 if (ignore_srcdir
) break;
4428 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4433 if ((nam
= fp
->nominal_fname
) != NULL
) {
4434 /* Found a named file. Figure out dir of the file,
4435 and put it in front of the search list. */
4436 dsp
= ((struct file_name_list
*)
4437 alloca (sizeof (struct file_name_list
)
4438 + fp
->nominal_fname_len
));
4439 strcpy (dsp
->fname
, nam
);
4440 simplify_filename (dsp
->fname
);
4441 nam
= base_name (dsp
->fname
);
4443 /* But for efficiency's sake, do not insert the dir
4444 if it matches the search list's first dir. */
4445 dsp
->next
= search_start
;
4446 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4448 n
= nam
- dsp
->fname
;
4449 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4450 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4452 dsp
[0].got_name_map
= 0;
4460 while (fin
!= limit
&& *fin
!= '>')
4462 if (*fin
== '>' && fin
+ 1 == limit
) {
4464 /* If -I-, start with the first -I dir after the -I-. */
4465 search_start
= first_bracket_include
;
4473 * Support '#include xyz' like VAX-C to allow for easy use of all the
4474 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4475 * code from case '<' is repeated here) and generates a warning.
4476 * (Note: macro expansion of `xyz' takes precedence.)
4478 if (retried
&& isalpha(*(U_CHAR
*) (--fbeg
))) {
4479 while (fin
!= limit
&& (!isspace(*fin
)))
4481 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4485 /* If -I-, start with the first -I dir after the -I-. */
4486 search_start
= first_bracket_include
;
4494 /* Expand buffer and then remove any newline markers.
4495 We can't just tell expand_to_temp_buffer to omit the markers,
4496 since it would put extra spaces in include file names. */
4499 int errors_before_expansion
= errors
;
4500 trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4501 if (errors
!= errors_before_expansion
) {
4503 goto invalid_include_file_name
;
4506 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4508 while (src
!= trybuf
.bufp
) {
4509 switch ((*limit
++ = *src
++)) {
4518 U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4519 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4532 invalid_include_file_name
:
4533 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4537 /* For #include_next, skip in the search path
4538 past the dir in which the containing file was found. */
4541 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4542 if (fp
->fname
!= NULL
) {
4543 /* fp->dir is null if the containing file was specified
4544 with an absolute file name. In that case, don't skip anything. */
4546 search_start
= fp
->dir
->next
;
4552 flen
= simplify_filename (fbeg
);
4556 error ("empty file name in `#%s'", keyword
->name
);
4560 /* Allocate this permanently, because it gets stored in the definitions
4562 fname
= xmalloc (max_include_len
+ flen
+ 1);
4563 /* + 1 above for terminating null. */
4565 system_include_depth
+= angle_brackets
;
4567 /* If specified file name is absolute, just open it. */
4569 if (absolute_filename (fbeg
)) {
4570 strcpy (fname
, fbeg
);
4571 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4575 struct bypass_dir
*next
;
4577 struct file_name_list
*searchptr
;
4578 } **bypass_slot
= 0;
4580 /* Search directory path, trying to open the file.
4581 Copy each filename tried into FNAME. */
4583 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4585 if (searchptr
== first_bracket_include
) {
4586 /* Go to bypass directory if we know we've seen this file before. */
4587 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4588 struct bypass_dir
*p
;
4589 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4591 for (p
= *bypass_slot
; p
; p
= p
->next
)
4592 if (!strcmp (fbeg
, p
->fname
)) {
4593 searchptr
= p
->searchptr
;
4599 strcpy (fname
, searchptr
->fname
);
4600 strcat (fname
, fbeg
);
4602 /* Change this 1/2 Unix 1/2 VMS file specification into a
4603 full VMS file specification */
4604 if (searchptr
->fname
[0]) {
4605 /* Fix up the filename */
4606 hack_vms_include_specification (fname
, vaxc_include
);
4608 /* This is a normal VMS filespec, so use it unchanged. */
4609 strcpy (fname
, fbeg
);
4610 /* if it's '#include filename', add the missing .h */
4611 if (vaxc_include
&& index(fname
,'.')==NULL
) {
4612 strcat (fname
, ".h");
4616 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4618 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4619 /* This is the first time we found this include file,
4620 and we found it after first_bracket_include.
4621 Record its location so that we can bypass to here next time. */
4622 struct bypass_dir
*p
4623 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4624 p
->next
= *bypass_slot
;
4625 p
->fname
= fname
+ strlen (searchptr
->fname
);
4626 p
->searchptr
= searchptr
;
4632 /* Our VMS hacks can produce invalid filespecs, so don't worry
4633 about errors other than EACCES. */
4634 if (errno
== EACCES
)
4637 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
4647 /* The file was already included. */
4649 /* If generating dependencies and -MG was specified, we assume missing
4650 files are leaf files, living in the same directory as the source file
4651 or other similar place; these missing files may be generated from
4652 other files and may not exist yet (eg: y.tab.h). */
4653 } else if (print_deps_missing_files
4654 && (system_include_depth
!= 0) < print_deps
)
4656 /* If it was requested as a system header file,
4657 then assume it belongs in the first place to look for such. */
4661 char *p
= (char *) alloca (strlen (search_start
->fname
)
4662 + strlen (fbeg
) + 1);
4663 strcpy (p
, search_start
->fname
);
4665 deps_output (p
, ' ');
4670 /* Otherwise, omit the directory, as if the file existed
4671 in the directory with the source. */
4672 deps_output (fbeg
, ' ');
4675 /* If -M was specified, and this header file won't be added to the
4676 dependency list, then don't count this as an error, because we can
4677 still produce correct output. Otherwise, we can't produce correct
4678 output, because there may be dependencies we need inside the missing
4679 file, and we don't know what directory this missing file exists in. */
4680 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4681 warning ("No include path in which to find %s", fbeg
);
4683 error_from_errno (fbeg
);
4685 error ("No include path in which to find %s", fbeg
);
4689 /* Actually process the file. */
4691 pcftry
= (char *) alloca (strlen (fname
) + 30);
4698 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4700 pcf
= open (pcftry
, O_RDONLY
, 0666);
4705 if (fstat (pcf
, &s
) != 0)
4706 pfatal_with_name (pcftry
);
4707 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4708 || inc
->st
.st_dev
!= s
.st_dev
)
4710 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4711 /* Don't need it any more. */
4716 /* Don't need it at all. */
4721 } while (pcf
!= -1 && !pcfbuf
);
4724 /* Actually process the file */
4726 pcfname
= xmalloc (strlen (pcftry
) + 1);
4727 strcpy (pcfname
, pcftry
);
4728 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) pcfbuflimit
,
4729 (U_CHAR
*) fname
, op
);
4732 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4735 system_include_depth
-= angle_brackets
;
4740 /* Return nonzero if the given FILENAME is an absolute pathname which
4741 designates a file within one of the known "system" include file
4742 directories. We assume here that if the given FILENAME looks like
4743 it is the name of a file which resides either directly in a "system"
4744 include file directory, or within any subdirectory thereof, then the
4745 given file must be a "system" include file. This function tells us
4746 if we should suppress pedantic errors/warnings for the given FILENAME.
4748 The value is 2 if the file is a C-language system header file
4749 for which C++ should (on most systems) assume `extern "C"'. */
4752 is_system_include (filename
)
4753 register char *filename
;
4755 struct file_name_list
*searchptr
;
4757 for (searchptr
= first_system_include
; searchptr
;
4758 searchptr
= searchptr
->next
)
4759 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4760 return searchptr
->c_system_include_path
+ 1;
4764 /* Yield the non-directory suffix of a file name. */
4772 #if defined (__MSDOS__) || defined (_WIN32)
4773 if (isalpha (s
[0]) && s
[1] == ':') s
+= 2;
4776 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4777 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4778 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4782 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4783 #ifdef DIR_SEPARATOR
4784 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4789 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4792 absolute_filename (filename
)
4795 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN32__))
4796 if (isalpha (filename
[0]) && filename
[1] == ':') filename
+= 2;
4798 #if defined (__CYGWIN32__)
4799 /* At present, any path that begins with a drive spec is absolute. */
4800 if (isalpha (filename
[0]) && filename
[1] == ':') return 1;
4802 if (filename
[0] == '/') return 1;
4803 #ifdef DIR_SEPARATOR
4804 if (filename
[0] == DIR_SEPARATOR
) return 1;
4809 /* Remove unnecessary characters from FILENAME in place,
4810 to avoid unnecessary filename aliasing.
4811 Return the length of the resulting string.
4813 Do only the simplifications allowed by Posix.
4814 It is OK to miss simplifications on non-Posix hosts,
4815 since this merely leads to suboptimal results. */
4818 simplify_filename (filename
)
4821 register char *from
= filename
;
4822 register char *to
= filename
;
4825 /* Remove redundant initial /s. */
4828 if (*++from
== '/') {
4829 if (*++from
== '/') {
4830 /* 3 or more initial /s are equivalent to 1 /. */
4831 while (*++from
== '/')
4834 /* On some hosts // differs from /; Posix allows this. */
4835 static int slashslash_vs_slash
;
4836 if (slashslash_vs_slash
== 0) {
4838 slashslash_vs_slash
= ((stat ("/", &s1
) == 0 && stat ("//", &s2
) == 0
4839 && INO_T_EQ (s1
.st_ino
, s2
.st_ino
)
4840 && s1
.st_dev
== s2
.st_dev
)
4843 if (slashslash_vs_slash
< 0)
4851 if (from
[0] == '.' && from
[1] == '/')
4854 /* Copy this component and trailing /, if any. */
4855 while ((*to
++ = *from
++) != '/') {
4857 /* Trim . component at end of nonempty name. */
4858 to
-= filename
<= to
- 3 && to
[-3] == '/' && to
[-2] == '.';
4860 /* Trim unnecessary trailing /s. */
4861 while (to0
< --to
&& to
[-1] == '/')
4865 return to
- filename
;
4870 /* Skip /s after a /. */
4871 while (*from
== '/')
4876 /* The file_name_map structure holds a mapping of file names for a
4877 particular directory. This mapping is read from the file named
4878 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4879 map filenames on a file system with severe filename restrictions,
4880 such as DOS. The format of the file name map file is just a series
4881 of lines with two tokens on each line. The first token is the name
4882 to map, and the second token is the actual name to use. */
4884 struct file_name_map
4886 struct file_name_map
*map_next
;
4891 #define FILE_NAME_MAP_FILE "header.gcc"
4893 /* Read a space delimited string of unlimited length from a stdio
4897 read_filename_string (ch
, f
)
4905 set
= alloc
= xmalloc (len
+ 1);
4909 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
4911 if (set
- alloc
== len
)
4914 alloc
= xrealloc (alloc
, len
+ 1);
4915 set
= alloc
+ len
/ 2;
4925 /* Read the file name map file for DIRNAME.
4926 If DIRNAME is empty, read the map file for the working directory;
4927 otherwise DIRNAME must end in '/'. */
4929 static struct file_name_map
*
4930 read_name_map (dirname
)
4933 /* This structure holds a linked list of file name maps, one per
4935 struct file_name_map_list
4937 struct file_name_map_list
*map_list_next
;
4938 char *map_list_name
;
4939 struct file_name_map
*map_list_map
;
4941 static struct file_name_map_list
*map_list
;
4942 register struct file_name_map_list
*map_list_ptr
;
4947 for (map_list_ptr
= map_list
; map_list_ptr
;
4948 map_list_ptr
= map_list_ptr
->map_list_next
)
4949 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
4950 return map_list_ptr
->map_list_map
;
4952 map_list_ptr
= ((struct file_name_map_list
*)
4953 xmalloc (sizeof (struct file_name_map_list
)));
4954 map_list_ptr
->map_list_name
= savestring (dirname
);
4955 map_list_ptr
->map_list_map
= NULL
;
4957 dirlen
= strlen (dirname
);
4958 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
4959 strcpy (name
, dirname
);
4960 strcat (name
, FILE_NAME_MAP_FILE
);
4961 f
= fopen (name
, "r");
4963 map_list_ptr
->map_list_map
= NULL
;
4968 while ((ch
= getc (f
)) != EOF
)
4971 struct file_name_map
*ptr
;
4976 from
= read_filename_string (ch
, f
);
4977 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
4979 to
= read_filename_string (ch
, f
);
4981 simplify_filename (from
);
4982 tolen
= simplify_filename (to
);
4984 ptr
= ((struct file_name_map
*)
4985 xmalloc (sizeof (struct file_name_map
)));
4986 ptr
->map_from
= from
;
4988 /* Make the real filename absolute. */
4989 if (absolute_filename (to
))
4993 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
4994 strcpy (ptr
->map_to
, dirname
);
4995 strcat (ptr
->map_to
, to
);
4999 ptr
->map_next
= map_list_ptr
->map_list_map
;
5000 map_list_ptr
->map_list_map
= ptr
;
5002 while ((ch
= getc (f
)) != '\n')
5009 map_list_ptr
->map_list_next
= map_list
;
5010 map_list
= map_list_ptr
;
5012 return map_list_ptr
->map_list_map
;
5015 /* Try to open include file FILENAME. SEARCHPTR is the directory
5016 being tried from the include file search path.
5017 IMPORTING is "" if we are importing, null otherwise.
5018 Return -2 if found, either a matching name or a matching inode.
5019 Otherwise, open the file and return a file descriptor if successful
5020 or -1 if unsuccessful.
5021 Unless unsuccessful, put a descriptor of the included file into *PINC.
5022 This function maps filenames on file systems based on information read by
5026 open_include_file (filename
, searchptr
, importing
, pinc
)
5028 struct file_name_list
*searchptr
;
5030 struct include_file
**pinc
;
5032 char *fname
= remap
? remap_include_file (filename
, searchptr
) : filename
;
5035 /* Look up FNAME in include_hashtab. */
5036 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
5039 struct include_file
*inc
, *head
= *phead
;
5040 for (inc
= head
; inc
; inc
= inc
->next
)
5041 if (!strcmp (fname
, inc
->fname
))
5045 || ! inc
->control_macro
5046 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
5048 fd
= open (fname
, O_RDONLY
, 0);
5054 /* FNAME was not in include_hashtab; insert a new entry. */
5055 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
5058 inc
->control_macro
= 0;
5059 inc
->deps_output
= 0;
5060 if (fstat (fd
, &inc
->st
) != 0)
5061 pfatal_with_name (fname
);
5064 /* Look for another file with the same inode and device. */
5065 if (lookup_ino_include (inc
)
5066 && inc
->control_macro
5067 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
5073 /* For -M, add this file to the dependencies. */
5074 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
5075 inc
->deps_output
= 1;
5076 deps_output (fname
, ' ');
5079 /* Handle -H option. */
5080 if (print_include_names
)
5081 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
5085 inc
->control_macro
= importing
;
5091 /* Return the remapped name of the the include file FILENAME.
5092 SEARCHPTR is the directory being tried from the include file path. */
5095 remap_include_file (filename
, searchptr
)
5097 struct file_name_list
*searchptr
;
5099 register struct file_name_map
*map
;
5100 register char *from
;
5104 if (! searchptr
->got_name_map
)
5106 searchptr
->name_map
= read_name_map (searchptr
->fname
);
5107 searchptr
->got_name_map
= 1;
5110 /* Check the mapping for the directory we are using. */
5111 from
= filename
+ strlen (searchptr
->fname
);
5112 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5113 if (! strcmp (map
->map_from
, from
))
5117 from
= base_name (filename
);
5119 if (from
!= filename
|| !searchptr
)
5121 /* Try to find a mapping file for the particular directory we are
5122 looking in. Thus #include <sys/types.h> will look up sys/types.h
5123 in /usr/include/header.gcc and look up types.h in
5124 /usr/include/sys/header.gcc. */
5126 char *dir
= (char *) alloca (from
- filename
+ 1);
5127 bcopy (filename
, dir
, from
- filename
);
5128 dir
[from
- filename
] = '\0';
5130 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
5131 if (! strcmp (map
->map_from
, from
))
5138 /* Insert INC into the include file table, hashed by device and inode number.
5139 If a file with different name but same dev+ino was already in the table,
5140 return 1 and set INC's control macro to the already-known macro. */
5143 lookup_ino_include (inc
)
5144 struct include_file
*inc
;
5146 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
5147 % INCLUDE_HASHSIZE
);
5148 struct include_file
*i
= include_ino_hashtab
[hash
];
5150 include_ino_hashtab
[hash
] = inc
;
5152 for (; i
; i
= i
->next_ino
)
5153 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
5154 && inc
->st
.st_dev
== i
->st
.st_dev
) {
5155 inc
->control_macro
= i
->control_macro
;
5162 /* Process file descriptor F, which corresponds to include file INC,
5164 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5165 "system" include directories (as decided by the `is_system_include'
5167 DIRPTR is the link in the dir path through which this file was found,
5168 or 0 if the file name was absolute. */
5171 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5173 struct include_file
*inc
;
5175 int system_header_p
;
5176 struct file_name_list
*dirptr
;
5178 char *fname
= inc
->fname
;
5180 FILE_BUF
*fp
; /* For input stack frame */
5181 int missing_newline
= 0;
5183 CHECK_DEPTH (return;);
5185 fp
= &instack
[indepth
+ 1];
5186 bzero ((char *) fp
, sizeof (FILE_BUF
));
5187 fp
->nominal_fname
= fp
->fname
= fname
;
5188 fp
->nominal_fname_len
= strlen (fname
);
5192 fp
->if_stack
= if_stack
;
5193 fp
->system_header_p
= system_header_p
;
5196 if (S_ISREG (inc
->st
.st_mode
)) {
5197 size_t s
= (size_t) inc
->st
.st_size
;
5198 if (s
!= inc
->st
.st_size
|| s
+ 2 < s
)
5200 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
5203 /* Read the file contents, knowing that s is an upper bound
5204 on the number of bytes we can read. */
5205 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
5206 if (fp
->length
< 0) goto nope
;
5208 else if (S_ISDIR (inc
->st
.st_mode
)) {
5209 error ("directory `%s' specified in #include", fname
);
5213 /* Cannot count its file size before reading.
5214 First read the entire file into heap and
5215 copy them into buffer on stack. */
5220 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5223 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5225 goto nope
; /* error! */
5227 if (st_size
!= bsize
)
5228 break; /* End of file */
5230 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5233 fp
->length
= st_size
;
5236 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5237 /* Backslash-newline at end is not good enough. */
5238 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5239 fp
->buf
[fp
->length
++] = '\n';
5240 missing_newline
= 1;
5242 fp
->buf
[fp
->length
] = '\0';
5244 /* Close descriptor now, so nesting does not use lots of descriptors. */
5247 /* Must do this before calling trigraph_pcp, so that the correct file name
5248 will be printed in warning messages. */
5251 input_file_stack_tick
++;
5256 if (warn_white_space
)
5257 check_white_space (fp
);
5259 output_line_directive (fp
, op
, 0, enter_file
);
5262 if (missing_newline
)
5265 if (pedantic
&& missing_newline
)
5266 pedwarn ("file does not end in newline");
5269 input_file_stack_tick
++;
5270 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5276 perror_with_name (fname
);
5281 /* Record that inclusion of the include file INC
5282 should be controlled by the macro named MACRO_NAME.
5283 This means that trying to include the file again
5284 will do something if that macro is defined. */
5287 record_control_macro (inc
, macro_name
)
5288 struct include_file
*inc
;
5291 if (!inc
->control_macro
|| inc
->control_macro
[0])
5292 inc
->control_macro
= macro_name
;
5295 /* Load the specified precompiled header into core, and verify its
5296 preconditions. PCF indicates the file descriptor to read, which must
5297 be a regular file. *ST is its file status.
5298 FNAME indicates the file name of the original header.
5299 *LIMIT will be set to an address one past the end of the file.
5300 If the preconditions of the file are not satisfied, the buffer is
5301 freed and we return 0. If the preconditions are satisfied, return
5302 the address of the buffer following the preconditions. The buffer, in
5303 this case, should never be freed because various pieces of it will
5304 be referred to until all precompiled strings are output at the end of
5308 check_precompiled (pcf
, st
, fname
, limit
)
5321 if (S_ISREG (st
->st_mode
))
5323 size_t s
= (size_t) st
->st_size
;
5324 if (s
!= st
->st_size
|| s
+ 2 < s
)
5326 buf
= xmalloc (s
+ 2);
5327 length
= safe_read (pcf
, buf
, s
);
5334 if (length
> 0 && buf
[length
-1] != '\n')
5335 buf
[length
++] = '\n';
5338 *limit
= buf
+ length
;
5340 /* File is in core. Check the preconditions. */
5341 if (!check_preconditions (buf
))
5343 for (cp
= buf
; *cp
; cp
++)
5346 fprintf (stderr
, "Using preinclude %s\n", fname
);
5352 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5358 /* PREC (null terminated) points to the preconditions of a
5359 precompiled header. These are a series of #define and #undef
5360 lines which must match the current contents of the hash
5364 check_preconditions (prec
)
5371 lineend
= index (prec
, '\n');
5373 if (*prec
++ != '#') {
5374 error ("Bad format encountered while reading precompiled file");
5377 if (!strncmp (prec
, "define", 6)) {
5381 mdef
= create_definition ((U_CHAR
*) prec
, (U_CHAR
*) lineend
, NULL_PTR
);
5386 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5387 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5388 || (hp
->type
== T_MACRO
5389 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5390 && (mdef
.defn
->length
!= 2
5391 || mdef
.defn
->expansion
[0] != '\n'
5392 || mdef
.defn
->expansion
[1] != ' ')))
5394 } else if (!strncmp (prec
, "undef", 5)) {
5399 while (is_hor_space
[(U_CHAR
) *prec
])
5402 while (is_idchar
[(U_CHAR
) *prec
])
5406 if (lookup ((U_CHAR
*) name
, len
, -1))
5409 error ("Bad format encountered while reading precompiled file");
5414 /* They all passed successfully */
5418 /* Process the main body of a precompiled file. BUF points to the
5419 string section of the file, following the preconditions. LIMIT is one
5420 character past the end. NAME is the name of the file being read
5421 in. OP is the main output buffer. */
5424 pcfinclude (buf
, limit
, name
, op
)
5425 U_CHAR
*buf
, *limit
, *name
;
5432 /* First in the file comes 4 bytes indicating the number of strings, */
5433 /* in network byte order. (MSB first). */
5435 nstrings
= (nstrings
<< 8) | *cp
++;
5436 nstrings
= (nstrings
<< 8) | *cp
++;
5437 nstrings
= (nstrings
<< 8) | *cp
++;
5439 /* Looping over each string... */
5440 while (nstrings
--) {
5441 U_CHAR
*string_start
;
5442 U_CHAR
*endofthiskey
;
5446 /* Each string starts with a STRINGDEF structure (str), followed */
5447 /* by the text of the string (string_start) */
5449 /* First skip to a longword boundary */
5450 /* ??? Why a 4-byte boundary? On all machines? */
5451 /* NOTE: This works correctly even if size_t
5452 is narrower than a pointer.
5453 Do not try risky measures here to get another type to use!
5454 Do not include stddef.h--it will fail! */
5455 if ((size_t) cp
& 3)
5456 cp
+= 4 - ((size_t) cp
& 3);
5458 /* Now get the string. */
5459 str
= (STRINGDEF
*) (GENERIC_PTR
) cp
;
5460 string_start
= cp
+= sizeof (STRINGDEF
);
5462 for (; *cp
; cp
++) /* skip the string */
5465 /* We need to macro expand the string here to ensure that the
5466 proper definition environment is in place. If it were only
5467 expanded when we find out it is needed, macros necessary for
5468 its proper expansion might have had their definitions changed. */
5469 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5470 /* Lineno is already set in the precompiled file */
5471 str
->contents
= tmpbuf
.buf
;
5472 str
->len
= tmpbuf
.bufp
- tmpbuf
.buf
;
5474 str
->filename
= name
;
5475 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5478 *stringlist_tailp
= str
;
5479 stringlist_tailp
= &str
->chain
;
5481 /* Next comes a fourbyte number indicating the number of keys
5484 nkeys
= (nkeys
<< 8) | *cp
++;
5485 nkeys
= (nkeys
<< 8) | *cp
++;
5486 nkeys
= (nkeys
<< 8) | *cp
++;
5488 /* If this number is -1, then the string is mandatory. */
5492 /* Otherwise, for each key, */
5493 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5494 KEYDEF
*kp
= (KEYDEF
*) (GENERIC_PTR
) cp
;
5498 /* It starts with a KEYDEF structure */
5499 cp
+= sizeof (KEYDEF
);
5501 /* Find the end of the key. At the end of this for loop we
5502 advance CP to the start of the next key using this variable. */
5503 endofthiskey
= cp
+ strlen ((char *) cp
);
5506 /* Expand the key, and enter it into the hash table. */
5507 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5510 while (is_hor_space
[*bp
])
5512 if (!is_idstart
[*bp
] || bp
== tmpbuf
.bufp
) {
5517 hp
= lookup (bp
, -1, -1);
5520 install (bp
, -1, T_PCSTRING
, (char *) kp
, -1);
5522 else if (hp
->type
== T_PCSTRING
) {
5523 kp
->chain
= hp
->value
.keydef
;
5524 hp
->value
.keydef
= kp
;
5530 /* This output_line_directive serves to switch us back to the current
5531 input file in case some of these strings get output (which will
5532 result in line directives for the header file being output). */
5533 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5536 /* Called from rescan when it hits a key for strings. Mark them all
5537 used and clean up. */
5545 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5546 kp
->str
->writeflag
= 1;
5550 /* Write the output, interspersing precompiled strings in their
5551 appropriate places. */
5556 STRINGDEF
*next_string
;
5557 U_CHAR
*cur_buf_loc
;
5558 int line_directive_len
= 80;
5559 char *line_directive
= xmalloc (line_directive_len
);
5562 /* In each run through the loop, either cur_buf_loc ==
5563 next_string_loc, in which case we print a series of strings, or
5564 it is less than next_string_loc, in which case we write some of
5566 cur_buf_loc
= outbuf
.buf
;
5567 next_string
= stringlist
;
5569 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5571 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5572 if (next_string
->writeflag
) {
5573 len
= 4 * strlen ((char *) next_string
->filename
) + 32;
5574 while (len
> line_directive_len
)
5575 line_directive
= xrealloc (line_directive
,
5576 line_directive_len
*= 2);
5577 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5578 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5579 (char *) next_string
->filename
,
5580 strlen ((char *) next_string
->filename
)),
5582 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5583 safe_write (fileno (stdout
),
5584 (char *) next_string
->contents
, next_string
->len
);
5586 next_string
= next_string
->chain
;
5590 ? (next_string
->output_mark
5591 - (cur_buf_loc
- outbuf
.buf
))
5592 : outbuf
.bufp
- cur_buf_loc
);
5594 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5598 free (line_directive
);
5601 /* Pass a directive through to the output file.
5602 BUF points to the contents of the directive, as a contiguous string.
5603 LIMIT points to the first character past the end of the directive.
5604 KEYWORD is the keyword-table entry for the directive. */
5607 pass_thru_directive (buf
, limit
, op
, keyword
)
5608 U_CHAR
*buf
, *limit
;
5610 struct directive
*keyword
;
5612 register unsigned keyword_length
= keyword
->length
;
5614 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5616 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5617 op
->bufp
+= keyword_length
;
5618 if (limit
!= buf
&& buf
[0] != ' ')
5620 bcopy ((char *) buf
, (char *) op
->bufp
, limit
- buf
);
5621 op
->bufp
+= (limit
- buf
);
5624 /* Count the line we have just made in the output,
5625 to get in sync properly. */
5630 /* The arglist structure is built by do_define to tell
5631 collect_definition where the argument names begin. That
5632 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5633 would contain pointers to the strings x, y, and z.
5634 Collect_definition would then build a DEFINITION node,
5635 with reflist nodes pointing to the places x, y, and z had
5636 appeared. So the arglist is just convenience data passed
5637 between these two routines. It is not kept around after
5638 the current #define has been processed and entered into the
5642 struct arglist
*next
;
5649 /* Create a DEFINITION node from a #define directive. Arguments are
5650 as for do_define. */
5653 create_definition (buf
, limit
, op
)
5654 U_CHAR
*buf
, *limit
;
5657 U_CHAR
*bp
; /* temp ptr into input buffer */
5658 U_CHAR
*symname
; /* remember where symbol name starts */
5659 int sym_length
; /* and how long it is */
5660 int line
= instack
[indepth
].lineno
;
5661 char *file
= instack
[indepth
].nominal_fname
;
5662 size_t file_len
= instack
[indepth
].nominal_fname_len
;
5666 int arglengths
= 0; /* Accumulate lengths of arg names
5667 plus number of args. */
5672 while (is_hor_space
[*bp
])
5675 symname
= bp
; /* remember where it starts */
5676 sym_length
= check_macro_name (bp
, 0);
5679 /* Lossage will occur if identifiers or control keywords are broken
5680 across lines using backslash. This is not the right place to take
5684 struct arglist
*arg_ptrs
= NULL
;
5687 bp
++; /* skip '(' */
5688 SKIP_WHITE_SPACE (bp
);
5690 /* Loop over macro argument names. */
5691 while (*bp
!= ')') {
5692 struct arglist
*temp
;
5694 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5696 temp
->next
= arg_ptrs
;
5697 temp
->argno
= argno
++;
5698 temp
->rest_args
= 0;
5702 pedwarn ("another parameter follows `%s'",
5705 if (!is_idstart
[*bp
])
5706 pedwarn ("invalid character in macro parameter name");
5708 /* Find the end of the arg name. */
5709 while (is_idchar
[*bp
]) {
5711 /* do we have a "special" rest-args extension here? */
5712 if (limit
- bp
> REST_EXTENSION_LENGTH
5713 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5715 temp
->rest_args
= 1;
5719 temp
->length
= bp
- temp
->name
;
5721 bp
+= REST_EXTENSION_LENGTH
;
5722 arglengths
+= temp
->length
+ 2;
5723 SKIP_WHITE_SPACE (bp
);
5724 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5725 error ("badly punctuated parameter list in `#define'");
5730 SKIP_WHITE_SPACE (bp
);
5731 /* A comma at this point can only be followed by an identifier. */
5732 if (!is_idstart
[*bp
]) {
5733 error ("badly punctuated parameter list in `#define'");
5738 error ("unterminated parameter list in `#define'");
5742 struct arglist
*otemp
;
5744 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5745 if (temp
->length
== otemp
->length
5746 && bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
5747 error ("duplicate argument name `%.*s' in `#define'",
5748 temp
->length
, temp
->name
);
5754 ++bp
; /* skip paren */
5755 SKIP_WHITE_SPACE (bp
);
5756 /* now everything from bp before limit is the definition. */
5757 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5758 defn
->rest_args
= rest_args
;
5760 /* Now set defn->args.argnames to the result of concatenating
5761 the argument names in reverse order
5762 with comma-space between them. */
5763 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5765 struct arglist
*temp
;
5767 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5768 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5770 if (temp
->next
!= 0) {
5771 defn
->args
.argnames
[i
++] = ',';
5772 defn
->args
.argnames
[i
++] = ' ';
5775 defn
->args
.argnames
[i
] = 0;
5778 /* Simple expansion or empty definition. */
5782 if (is_hor_space
[*bp
]) {
5784 SKIP_WHITE_SPACE (bp
);
5785 } else if (sym_length
) {
5787 case '!': case '"': case '#': case '%': case '&': case '\'':
5788 case ')': case '*': case '+': case ',': case '-': case '.':
5789 case '/': case ':': case ';': case '<': case '=': case '>':
5790 case '?': case '[': case '\\': case ']': case '^': case '{':
5791 case '|': case '}': case '~':
5792 warning ("missing white space after `#define %.*s'",
5793 sym_length
, symname
);
5797 pedwarn ("missing white space after `#define %.*s'",
5798 sym_length
, symname
);
5803 /* Now everything from bp before limit is the definition. */
5804 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5805 defn
->args
.argnames
= (U_CHAR
*) "";
5810 defn
->file_len
= file_len
;
5812 /* OP is null if this is a predefinition */
5813 defn
->predefined
= !op
;
5815 mdef
.symnam
= symname
;
5816 mdef
.symlen
= sym_length
;
5825 /* Process a #define directive.
5826 BUF points to the contents of the #define directive, as a contiguous string.
5827 LIMIT points to the first character past the end of the definition.
5828 KEYWORD is the keyword-table entry for #define. */
5831 do_define (buf
, limit
, op
, keyword
)
5832 U_CHAR
*buf
, *limit
;
5834 struct directive
*keyword
;
5839 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5840 if (pcp_outfile
&& op
)
5841 pass_thru_directive (buf
, limit
, op
, keyword
);
5843 mdef
= create_definition (buf
, limit
, op
);
5847 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
5851 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
5853 /* Redefining a precompiled key is ok. */
5854 if (hp
->type
== T_PCSTRING
)
5856 /* Redefining a macro is ok if the definitions are the same. */
5857 else if (hp
->type
== T_MACRO
)
5858 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
5859 /* Redefining a constant is ok with -D. */
5860 else if (hp
->type
== T_CONST
)
5861 ok
= ! done_initializing
;
5862 /* Print the warning if it's not ok. */
5864 /* If we are passing through #define and #undef directives, do
5865 that for this re-definition now. */
5866 if (debug_output
&& op
)
5867 pass_thru_directive (buf
, limit
, op
, keyword
);
5869 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
5870 if (hp
->type
== T_MACRO
)
5871 pedwarn_with_file_and_line (hp
->value
.defn
->file
,
5872 hp
->value
.defn
->file_len
,
5873 hp
->value
.defn
->line
,
5874 "this is the location of the previous definition");
5876 /* Replace the old definition. */
5878 hp
->value
.defn
= mdef
.defn
;
5880 /* If we are passing through #define and #undef directives, do
5881 that for this new definition now. */
5882 if (debug_output
&& op
)
5883 pass_thru_directive (buf
, limit
, op
, keyword
);
5884 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
,
5885 (char *) mdef
.defn
, hashcode
);
5896 /* Check a purported macro name SYMNAME, and yield its length.
5897 ASSERTION is nonzero if this is really for an assertion name. */
5900 check_macro_name (symname
, assertion
)
5907 for (p
= symname
; is_idchar
[*p
]; p
++)
5909 sym_length
= p
- symname
;
5911 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
5912 error (assertion
? "invalid assertion name" : "invalid macro name");
5913 else if (!is_idstart
[*symname
]
5914 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
5916 ? "invalid assertion name `%.*s'"
5917 : "invalid macro name `%.*s'"),
5918 sym_length
, symname
);
5922 /* Return zero if two DEFINITIONs are isomorphic. */
5925 compare_defs (d1
, d2
)
5926 DEFINITION
*d1
, *d2
;
5928 register struct reflist
*a1
, *a2
;
5929 register U_CHAR
*p1
= d1
->expansion
;
5930 register U_CHAR
*p2
= d2
->expansion
;
5933 if (d1
->nargs
!= d2
->nargs
)
5936 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
5938 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
5939 a1
= a1
->next
, a2
= a2
->next
) {
5940 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
5941 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
5942 || a1
->argno
!= a2
->argno
5943 || a1
->stringify
!= a2
->stringify
5944 || a1
->raw_before
!= a2
->raw_before
5945 || a1
->raw_after
!= a2
->raw_after
)
5953 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
5954 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
5959 /* Return 1 if two parts of two macro definitions are effectively different.
5960 One of the parts starts at BEG1 and has LEN1 chars;
5961 the other has LEN2 chars at BEG2.
5962 Any sequence of whitespace matches any other sequence of whitespace.
5963 FIRST means these parts are the first of a macro definition;
5964 so ignore leading whitespace entirely.
5965 LAST means these parts are the last of a macro definition;
5966 so ignore trailing whitespace entirely. */
5969 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
5971 U_CHAR
*beg1
, *beg2
;
5975 register U_CHAR
*end1
= beg1
+ len1
;
5976 register U_CHAR
*end2
= beg2
+ len2
;
5978 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5979 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5982 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
5983 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
5985 while (beg1
!= end1
&& beg2
!= end2
) {
5986 if (is_space
[*beg1
] && is_space
[*beg2
]) {
5987 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
5988 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
5989 } else if (*beg1
== *beg2
) {
5993 return (beg1
!= end1
) || (beg2
!= end2
);
5996 /* Read a replacement list for a macro with parameters.
5997 Build the DEFINITION structure.
5998 Reads characters of text starting at BUF until END.
5999 ARGLIST specifies the formal parameters to look for
6000 in the text of the definition; NARGS is the number of args
6001 in that list, or -1 for a macro name that wants no argument list.
6002 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6003 and NAMELEN is its length in characters.
6005 Note that comments, backslash-newlines, and leading white space
6006 have already been deleted from the argument. */
6008 /* If there is no trailing whitespace, a Newline Space is added at the end
6009 to prevent concatenation that would be contrary to the standard. */
6012 collect_expansion (buf
, end
, nargs
, arglist
)
6015 struct arglist
*arglist
;
6018 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
6019 struct reflist
*endpat
= NULL
;
6020 /* Pointer to first nonspace after last ## seen. */
6022 /* Pointer to first nonspace after last single-# seen. */
6023 U_CHAR
*stringify
= 0;
6024 /* How those tokens were spelled. */
6025 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
6026 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
6028 int expected_delimiter
= '\0';
6030 /* Scan thru the replacement list, ignoring comments and quoted
6031 strings, picking up on the macro calls. It does a linear search
6032 thru the arg list on every potential symbol. Profiling might say
6033 that something smarter should happen. */
6038 /* Find the beginning of the trailing whitespace. */
6041 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
6043 /* Allocate space for the text in the macro definition.
6044 Each input char may or may not need 1 byte,
6045 so this is an upper bound.
6046 The extra 3 are for invented trailing newline-marker and final null. */
6047 maxsize
= (sizeof (DEFINITION
)
6049 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
6051 defn
->nargs
= nargs
;
6052 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
6057 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
6058 error ("`##' at start of macro definition");
6059 p
+= p
[0] == '#' ? 2 : 4;
6062 /* Process the main body of the definition. */
6064 int skipped_arg
= 0;
6065 register U_CHAR c
= *p
++;
6073 if (expected_delimiter
!= '\0') {
6074 if (c
== expected_delimiter
)
6075 expected_delimiter
= '\0';
6077 expected_delimiter
= c
;
6081 if (expected_delimiter
) {
6082 /* In a string, backslash goes through
6083 and makes next char ordinary. */
6089 if (!expected_delimiter
&& *p
== ':') {
6090 /* %: is not a digraph if preceded by an odd number of '<'s. */
6092 while (buf
< p0
&& p0
[-1] == '<')
6095 /* Treat %:%: as ## and %: as #. */
6096 if (p
[1] == '%' && p
[2] == ':') {
6098 goto sharp_sharp_token
;
6109 /* # is ordinary inside a string. */
6110 if (expected_delimiter
)
6114 /* ##: concatenate preceding and following tokens. */
6115 /* Take out the first #, discard preceding whitespace. */
6117 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
6119 /* Skip the second #. */
6121 concat_sharp_token_type
= c
;
6122 if (is_hor_space
[*p
]) {
6123 concat_sharp_token_type
= c
+ 1;
6125 SKIP_WHITE_SPACE (p
);
6129 error ("`##' at end of macro definition");
6130 } else if (nargs
>= 0) {
6131 /* Single #: stringify following argument ref.
6132 Don't leave the # in the expansion. */
6135 stringify_sharp_token_type
= c
;
6136 if (is_hor_space
[*p
]) {
6137 stringify_sharp_token_type
= c
+ 1;
6139 SKIP_WHITE_SPACE (p
);
6141 if (! is_idstart
[*p
] || nargs
== 0
6142 || (*p
== 'L' && (p
[1] == '\'' || p
[1] == '"')))
6143 error ("`#' operator is not followed by a macro argument name");
6150 /* In -traditional mode, recognize arguments inside strings and
6151 and character constants, and ignore special properties of #.
6152 Arguments inside strings are considered "stringified", but no
6153 extra quote marks are supplied. */
6157 if (expected_delimiter
!= '\0') {
6158 if (c
== expected_delimiter
)
6159 expected_delimiter
= '\0';
6161 expected_delimiter
= c
;
6165 /* Backslash quotes delimiters and itself, but not macro args. */
6166 if (expected_delimiter
!= 0 && p
< limit
6167 && (*p
== expected_delimiter
|| *p
== '\\')) {
6174 if (expected_delimiter
!= '\0') /* No comments inside strings. */
6177 /* If we find a comment that wasn't removed by handle_directive,
6178 this must be -traditional. So replace the comment with
6181 while (++p
< limit
) {
6182 if (p
[0] == '*' && p
[1] == '/') {
6188 /* Mark this as a concatenation-point, as if it had been ##. */
6196 #ifdef MULTIBYTE_CHARS
6197 /* Handle multibyte characters inside string and character literals. */
6198 if (expected_delimiter
!= '\0')
6202 length
= local_mblen (p
, limit
- p
);
6206 bcopy (p
, exp_p
, length
);
6215 /* Handle the start of a symbol. */
6216 if (is_idchar
[c
] && nargs
> 0) {
6217 U_CHAR
*id_beg
= p
- 1;
6221 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6222 id_len
= p
- id_beg
;
6225 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
6226 register struct arglist
*arg
;
6228 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6229 struct reflist
*tpat
;
6231 if (arg
->name
[0] == c
6232 && arg
->length
== id_len
6233 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6234 enum sharp_token_type tpat_stringify
;
6235 if (expected_delimiter
) {
6236 if (warn_stringify
) {
6238 warning ("macro argument `%.*s' is stringified.",
6241 warning ("macro arg `%.*s' would be stringified with -traditional.",
6245 /* If ANSI, don't actually substitute inside a string. */
6248 tpat_stringify
= SHARP_TOKEN
;
6251 = (stringify
== id_beg
6252 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6254 /* make a pat node for this arg and append it to the end of
6256 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6259 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6260 tpat
->raw_after
= NO_SHARP_TOKEN
;
6261 tpat
->rest_args
= arg
->rest_args
;
6262 tpat
->stringify
= tpat_stringify
;
6265 defn
->pattern
= tpat
;
6267 endpat
->next
= tpat
;
6270 tpat
->argno
= arg
->argno
;
6271 tpat
->nchars
= exp_p
- lastp
;
6273 register U_CHAR
*p1
= p
;
6274 SKIP_WHITE_SPACE (p1
);
6277 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6278 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6280 lastp
= exp_p
; /* place to start copying from next time */
6287 /* If this was not a macro arg, copy it into the expansion. */
6288 if (! skipped_arg
) {
6289 register U_CHAR
*lim1
= p
;
6293 if (stringify
== id_beg
)
6294 error ("`#' operator should be followed by a macro argument name");
6299 if (!traditional
&& expected_delimiter
== 0) {
6300 /* If ANSI, put in a newline-space marker to prevent token pasting.
6301 But not if "inside a string" (which in ANSI mode happens only for
6309 defn
->length
= exp_p
- defn
->expansion
;
6311 /* Crash now if we overrun the allocated size. */
6312 if (defn
->length
+ 1 > maxsize
)
6316 /* This isn't worth the time it takes. */
6317 /* give back excess storage */
6318 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6325 do_assert (buf
, limit
, op
, keyword
)
6326 U_CHAR
*buf
, *limit
;
6328 struct directive
*keyword
;
6330 U_CHAR
*bp
; /* temp ptr into input buffer */
6331 U_CHAR
*symname
; /* remember where symbol name starts */
6332 int sym_length
; /* and how long it is */
6333 struct arglist
*tokens
= NULL
;
6335 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6336 pedwarn ("ANSI C does not allow `#assert'");
6340 while (is_hor_space
[*bp
])
6343 symname
= bp
; /* remember where it starts */
6344 sym_length
= check_macro_name (bp
, 1);
6346 /* #define doesn't do this, but we should. */
6347 SKIP_WHITE_SPACE (bp
);
6349 /* Lossage will occur if identifiers or control tokens are broken
6350 across lines using backslash. This is not the right place to take
6354 error ("missing token-sequence in `#assert'");
6361 bp
++; /* skip '(' */
6362 SKIP_WHITE_SPACE (bp
);
6364 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6368 error ("empty token-sequence in `#assert'");
6372 ++bp
; /* skip paren */
6373 SKIP_WHITE_SPACE (bp
);
6376 /* If this name isn't already an assertion name, make it one.
6377 Error if it was already in use in some other way. */
6380 ASSERTION_HASHNODE
*hp
;
6381 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6382 struct tokenlist_list
*value
6383 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6385 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6387 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6388 error ("`defined' redefined as assertion");
6389 hp
= assertion_install (symname
, sym_length
, hashcode
);
6392 /* Add the spec'd token-sequence to the list of such. */
6393 value
->tokens
= tokens
;
6394 value
->next
= hp
->value
;
6402 do_unassert (buf
, limit
, op
, keyword
)
6403 U_CHAR
*buf
, *limit
;
6405 struct directive
*keyword
;
6407 U_CHAR
*bp
; /* temp ptr into input buffer */
6408 U_CHAR
*symname
; /* remember where symbol name starts */
6409 int sym_length
; /* and how long it is */
6411 struct arglist
*tokens
= NULL
;
6412 int tokens_specified
= 0;
6414 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6415 pedwarn ("ANSI C does not allow `#unassert'");
6419 while (is_hor_space
[*bp
])
6422 symname
= bp
; /* remember where it starts */
6423 sym_length
= check_macro_name (bp
, 1);
6425 /* #define doesn't do this, but we should. */
6426 SKIP_WHITE_SPACE (bp
);
6428 /* Lossage will occur if identifiers or control tokens are broken
6429 across lines using backslash. This is not the right place to take
6435 bp
++; /* skip '(' */
6436 SKIP_WHITE_SPACE (bp
);
6438 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6442 error ("empty token list in `#unassert'");
6446 tokens_specified
= 1;
6448 ++bp
; /* skip paren */
6449 SKIP_WHITE_SPACE (bp
);
6453 ASSERTION_HASHNODE
*hp
;
6454 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6455 struct tokenlist_list
*tail
, *prev
;
6457 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6461 /* If no token list was specified, then eliminate this assertion
6463 if (! tokens_specified
) {
6464 struct tokenlist_list
*next
;
6465 for (tail
= hp
->value
; tail
; tail
= next
) {
6467 free_token_list (tail
->tokens
);
6470 delete_assertion (hp
);
6472 /* If a list of tokens was given, then delete any matching list. */
6477 struct tokenlist_list
*next
= tail
->next
;
6478 if (compare_token_lists (tail
->tokens
, tokens
)) {
6482 hp
->value
= tail
->next
;
6483 free_token_list (tail
->tokens
);
6496 /* Test whether there is an assertion named NAME
6497 and optionally whether it has an asserted token list TOKENS.
6498 NAME is not null terminated; its length is SYM_LENGTH.
6499 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6502 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6505 int tokens_specified
;
6506 struct arglist
*tokens
;
6508 ASSERTION_HASHNODE
*hp
;
6509 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6511 if (pedantic
&& !instack
[indepth
].system_header_p
)
6512 pedwarn ("ANSI C does not allow testing assertions");
6514 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6516 /* It is not an assertion; just return false. */
6519 /* If no token list was specified, then value is 1. */
6520 if (! tokens_specified
)
6524 struct tokenlist_list
*tail
;
6528 /* If a list of tokens was given,
6529 then succeed if the assertion records a matching list. */
6532 if (compare_token_lists (tail
->tokens
, tokens
))
6537 /* Fail if the assertion has no matching list. */
6542 /* Compare two lists of tokens for equality including order of tokens. */
6545 compare_token_lists (l1
, l2
)
6546 struct arglist
*l1
, *l2
;
6549 if (l1
->length
!= l2
->length
)
6551 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6557 /* Succeed if both lists end at the same time. */
6561 /* Read a space-separated list of tokens ending in a close parenthesis.
6562 Return a list of strings, in the order they were written.
6563 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6564 Parse the text starting at *BPP, and update *BPP.
6565 Don't parse beyond LIMIT. */
6567 static struct arglist
*
6568 read_token_list (bpp
, limit
, error_flag
)
6573 struct arglist
*token_ptrs
= 0;
6579 /* Loop over the assertion value tokens. */
6581 struct arglist
*temp
;
6585 /* Find the end of the token. */
6589 } else if (*bp
== ')') {
6594 } else if (*bp
== '"' || *bp
== '\'')
6595 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6597 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6598 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6601 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6602 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6603 bcopy ((char *) beg
, (char *) temp
->name
, bp
- beg
);
6604 temp
->name
[bp
- beg
] = 0;
6605 temp
->next
= token_ptrs
;
6607 temp
->length
= bp
- beg
;
6609 SKIP_WHITE_SPACE (bp
);
6612 error ("unterminated token sequence in `#assert' or `#unassert'");
6619 /* We accumulated the names in reverse order.
6620 Now reverse them to get the proper order. */
6622 register struct arglist
*prev
= 0, *this, *next
;
6623 for (this = token_ptrs
; this; this = next
) {
6633 free_token_list (tokens
)
6634 struct arglist
*tokens
;
6637 struct arglist
*next
= tokens
->next
;
6638 free (tokens
->name
);
6644 /* Install a name in the assertion hash table.
6646 If LEN is >= 0, it is the length of the name.
6647 Otherwise, compute the length by scanning the entire name.
6649 If HASH is >= 0, it is the precomputed hash code.
6650 Otherwise, compute the hash code. */
6652 static ASSERTION_HASHNODE
*
6653 assertion_install (name
, len
, hash
)
6658 register ASSERTION_HASHNODE
*hp
;
6659 register int i
, bucket
;
6660 register U_CHAR
*p
, *q
;
6662 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6663 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6665 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6666 hp
->next
= assertion_hashtab
[bucket
];
6667 assertion_hashtab
[bucket
] = hp
;
6669 if (hp
->next
!= NULL
)
6670 hp
->next
->prev
= hp
;
6673 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6676 for (i
= 0; i
< len
; i
++)
6682 /* Find the most recent hash node for name name (ending with first
6683 non-identifier char) installed by install
6685 If LEN is >= 0, it is the length of the name.
6686 Otherwise, compute the length by scanning the entire name.
6688 If HASH is >= 0, it is the precomputed hash code.
6689 Otherwise, compute the hash code. */
6691 static ASSERTION_HASHNODE
*
6692 assertion_lookup (name
, len
, hash
)
6697 register ASSERTION_HASHNODE
*bucket
;
6699 bucket
= assertion_hashtab
[hash
];
6701 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6703 bucket
= bucket
->next
;
6709 delete_assertion (hp
)
6710 ASSERTION_HASHNODE
*hp
;
6713 if (hp
->prev
!= NULL
)
6714 hp
->prev
->next
= hp
->next
;
6715 if (hp
->next
!= NULL
)
6716 hp
->next
->prev
= hp
->prev
;
6718 /* Make sure that the bucket chain header that the deleted guy was
6719 on points to the right thing afterwards. */
6720 if (hp
== *hp
->bucket_hdr
)
6721 *hp
->bucket_hdr
= hp
->next
;
6727 * interpret #line directive. Remembers previously seen fnames
6728 * in its very own hash table.
6730 #define FNAME_HASHSIZE 37
6733 do_line (buf
, limit
, op
, keyword
)
6734 U_CHAR
*buf
, *limit
;
6736 struct directive
*keyword
;
6738 register U_CHAR
*bp
;
6739 FILE_BUF
*ip
= &instack
[indepth
];
6742 enum file_change_code file_change
= same_file
;
6744 /* Expand any macros. */
6745 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6747 /* Point to macroexpanded line, which is null-terminated now. */
6750 SKIP_WHITE_SPACE (bp
);
6752 if (!isdigit (*bp
)) {
6753 error ("invalid format `#line' directive");
6757 /* The Newline at the end of this line remains to be processed.
6758 To put the next line at the specified line number,
6759 we must store a line number now that is one less. */
6760 new_lineno
= atoi ((char *) bp
) - 1;
6762 /* NEW_LINENO is one less than the actual line number here. */
6763 if (pedantic
&& new_lineno
< 0)
6764 pedwarn ("line number out of range in `#line' directive");
6766 /* skip over the line number. */
6767 while (isdigit (*bp
))
6770 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6771 if (*bp
&& !is_space
[*bp
]) {
6772 error ("invalid format `#line' directive");
6777 SKIP_WHITE_SPACE (bp
);
6780 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6781 HASHNODE
*hp
, **hash_bucket
;
6787 /* Turn the file name, which is a character string literal,
6788 into a null-terminated string. Do this in place. */
6791 switch ((*p
++ = *bp
++)) {
6794 char *bpc
= (char *) bp
;
6795 HOST_WIDE_INT c
= parse_escape (&bpc
, (HOST_WIDE_INT
) (U_CHAR
) (-1));
6796 bp
= (U_CHAR
*) bpc
;
6809 fname_length
= p
- fname
;
6811 SKIP_WHITE_SPACE (bp
);
6814 pedwarn ("garbage at end of `#line' directive");
6816 file_change
= enter_file
;
6817 else if (*bp
== '2')
6818 file_change
= leave_file
;
6819 else if (*bp
== '3')
6820 ip
->system_header_p
= 1;
6821 else if (*bp
== '4')
6822 ip
->system_header_p
= 2;
6824 error ("invalid format `#line' directive");
6829 SKIP_WHITE_SPACE (bp
);
6831 ip
->system_header_p
= 1;
6833 SKIP_WHITE_SPACE (bp
);
6836 ip
->system_header_p
= 2;
6838 SKIP_WHITE_SPACE (bp
);
6841 error ("invalid format `#line' directive");
6846 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
6847 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
6848 if (hp
->length
== fname_length
&&
6849 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
6850 ip
->nominal_fname
= hp
->value
.cpval
;
6851 ip
->nominal_fname_len
= fname_length
;
6855 /* Didn't find it; cons up a new one. */
6856 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
6857 hp
->next
= *hash_bucket
;
6860 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
6861 ip
->nominal_fname_len
= hp
->length
= fname_length
;
6862 bcopy (fname
, hp
->value
.cpval
, fname_length
+ 1);
6865 error ("invalid format `#line' directive");
6869 ip
->lineno
= new_lineno
;
6870 output_line_directive (ip
, op
, 0, file_change
);
6871 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
6875 /* Remove the definition of a symbol from the symbol table.
6876 according to un*x /lib/cpp, it is not an error to undef
6877 something that has no definitions, so it isn't one here either. */
6880 do_undef (buf
, limit
, op
, keyword
)
6881 U_CHAR
*buf
, *limit
;
6883 struct directive
*keyword
;
6887 U_CHAR
*orig_buf
= buf
;
6889 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
6890 if (pcp_outfile
&& op
)
6891 pass_thru_directive (buf
, limit
, op
, keyword
);
6893 SKIP_WHITE_SPACE (buf
);
6894 sym_length
= check_macro_name (buf
, 0);
6896 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
6897 /* If we are generating additional info for debugging (with -g) we
6898 need to pass through all effective #undef directives. */
6899 if (debug_output
&& op
)
6900 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
6901 if (hp
->type
!= T_MACRO
)
6902 warning ("undefining `%s'", hp
->name
);
6908 SKIP_WHITE_SPACE (buf
);
6910 pedwarn ("garbage after `#undef' directive");
6915 /* Report an error detected by the program we are processing.
6916 Use the text of the line in the error message. */
6919 do_error (buf
, limit
, op
, keyword
)
6920 U_CHAR
*buf
, *limit
;
6922 struct directive
*keyword
;
6924 int length
= limit
- buf
;
6925 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
6926 bcopy ((char *) buf
, (char *) copy
, length
);
6928 SKIP_WHITE_SPACE (copy
);
6930 switch (keyword
->type
) {
6932 error ("#error %s", copy
);
6936 if (pedantic
&& !instack
[indepth
].system_header_p
)
6937 pedwarn ("ANSI C does not allow `#warning'");
6938 warning ("#warning %s", copy
);
6948 /* Remember the name of the current file being read from so that we can
6949 avoid ever including it again. */
6956 for (i
= indepth
; i
>= 0; i
--)
6957 if (instack
[i
].inc
) {
6958 record_control_macro (instack
[i
].inc
, (U_CHAR
*) "");
6963 /* Report program identification. */
6966 do_ident (buf
, limit
, op
, keyword
)
6967 U_CHAR
*buf
, *limit
;
6969 struct directive
*keyword
;
6974 /* Allow #ident in system headers, since that's not user's fault. */
6975 if (pedantic
&& !instack
[indepth
].system_header_p
)
6976 pedwarn ("ANSI C does not allow `#ident'");
6978 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6980 len
= trybuf
.bufp
- buf
;
6982 /* Output expanded directive. */
6983 check_expand (op
, 7 + len
);
6984 bcopy ("#ident ", (char *) op
->bufp
, 7);
6986 bcopy ((char *) buf
, (char *) op
->bufp
, len
);
6993 /* #pragma and its argument line have already been copied to the output file.
6994 Just check for some recognized pragmas that need validation here. */
6997 do_pragma (buf
, limit
, op
, keyword
)
6998 U_CHAR
*buf
, *limit
;
7000 struct directive
*keyword
;
7002 SKIP_WHITE_SPACE (buf
);
7003 if (!strncmp ((char *) buf
, "once", 4)) {
7004 /* Allow #pragma once in system headers, since that's not the user's
7006 if (!instack
[indepth
].system_header_p
)
7007 warning ("`#pragma once' is obsolete");
7011 if (!strncmp ((char *) buf
, "implementation", 14)) {
7012 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7013 been included yet. */
7016 U_CHAR
*p
= buf
+ 14, *f
, *fname
;
7017 SKIP_WHITE_SPACE (p
);
7022 p
= skip_quoted_string (p
, limit
, 0, NULL_PTR
, NULL_PTR
, NULL_PTR
);
7026 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
7027 struct include_file
*inc
;
7028 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
7029 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
7030 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
7040 /* This was a fun hack, but #pragma seems to start to be useful.
7041 By failing to recognize it, we pass it through unchanged to cc1. */
7043 /* The behavior of the #pragma directive is implementation defined.
7044 this implementation defines it as follows. */
7050 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
7053 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
7055 execl ("/usr/games/hack", "#pragma", 0);
7056 execl ("/usr/games/rogue", "#pragma", 0);
7057 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7058 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7060 fatal ("You are in a maze of twisty compiler features, all different");
7064 #ifdef SCCS_DIRECTIVE
7066 /* Just ignore #sccs, on systems where we define it at all. */
7069 do_sccs (buf
, limit
, op
, keyword
)
7070 U_CHAR
*buf
, *limit
;
7072 struct directive
*keyword
;
7075 pedwarn ("ANSI C does not allow `#sccs'");
7079 #endif /* defined (SCCS_DIRECTIVE) */
7081 /* Handle #if directive by
7082 1) inserting special `defined' keyword into the hash table
7083 that gets turned into 0 or 1 by special_symbol (thus,
7084 if the luser has a symbol called `defined' already, it won't
7085 work inside the #if directive)
7086 2) rescan the input into a temporary output buffer
7087 3) pass the output buffer to the yacc parser and collect a value
7088 4) clean up the mess left from steps 1 and 2.
7089 5) call conditional_skip to skip til the next #endif (etc.),
7090 or not, depending on the value from step 3. */
7093 do_if (buf
, limit
, op
, keyword
)
7094 U_CHAR
*buf
, *limit
;
7096 struct directive
*keyword
;
7098 HOST_WIDE_INT value
;
7099 FILE_BUF
*ip
= &instack
[indepth
];
7101 value
= eval_if_expression (buf
, limit
- buf
);
7102 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
7106 /* Handle a #elif directive by not changing if_stack either.
7107 see the comment above do_else. */
7110 do_elif (buf
, limit
, op
, keyword
)
7111 U_CHAR
*buf
, *limit
;
7113 struct directive
*keyword
;
7115 HOST_WIDE_INT value
;
7116 FILE_BUF
*ip
= &instack
[indepth
];
7118 if (if_stack
== instack
[indepth
].if_stack
) {
7119 error ("`#elif' not within a conditional");
7122 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7123 error ("`#elif' after `#else'");
7124 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7125 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7126 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7127 if_stack
->fname_len
))) {
7128 fprintf (stderr
, ", file ");
7129 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7130 if_stack
->fname_len
, stderr
);
7132 fprintf (stderr
, ")\n");
7134 if_stack
->type
= T_ELIF
;
7137 if (if_stack
->if_succeeded
)
7138 skip_if_group (ip
, 0, op
);
7140 value
= eval_if_expression (buf
, limit
- buf
);
7142 skip_if_group (ip
, 0, op
);
7144 ++if_stack
->if_succeeded
; /* continue processing input */
7145 output_line_directive (ip
, op
, 1, same_file
);
7151 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7152 result as a C expression and return the value as an int. */
7154 static HOST_WIDE_INT
7155 eval_if_expression (buf
, length
)
7160 HASHNODE
*save_defined
;
7161 HOST_WIDE_INT value
;
7163 save_defined
= install ((U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
7166 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
7168 delete_macro (save_defined
); /* clean up special symbol */
7170 *temp_obuf
.bufp
= '\n';
7171 value
= parse_c_expression ((char *) temp_obuf
.buf
,
7172 warn_undef
&& !instack
[indepth
].system_header_p
);
7174 free (temp_obuf
.buf
);
7179 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7180 or don't skip to the #endif/#else/#elif depending on what directive
7181 is actually being processed. */
7184 do_xifdef (buf
, limit
, op
, keyword
)
7185 U_CHAR
*buf
, *limit
;
7187 struct directive
*keyword
;
7190 FILE_BUF
*ip
= &instack
[indepth
];
7192 int start_of_file
= 0;
7193 U_CHAR
*control_macro
= 0;
7195 /* Detect a #ifndef at start of file (not counting comments). */
7196 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7197 U_CHAR
*p
= ip
->buf
;
7198 while (p
!= directive_start
) {
7202 /* Make no special provision for backslash-newline here; this is
7203 slower if backslash-newlines are present, but it's correct,
7204 and it's not worth it to tune for the rare backslash-newline. */
7206 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7207 /* Skip this comment. */
7209 U_CHAR
*save_bufp
= ip
->bufp
;
7211 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7212 ip
->bufp
= save_bufp
;
7217 /* If we get here, this conditional is the beginning of the file. */
7222 /* Discard leading and trailing whitespace. */
7223 SKIP_WHITE_SPACE (buf
);
7224 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7226 /* Find the end of the identifier at the beginning. */
7227 for (end
= buf
; is_idchar
[*end
]; end
++);
7230 skip
= (keyword
->type
== T_IFDEF
);
7232 pedwarn (end
== limit
? "`#%s' with no argument"
7233 : "`#%s' argument starts with punctuation",
7238 if (! traditional
) {
7239 if (isdigit (buf
[0]))
7240 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7241 else if (end
!= limit
)
7242 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7245 hp
= lookup (buf
, end
-buf
, -1);
7248 /* Output a precondition for this macro. */
7250 && (hp
->type
== T_CONST
7251 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7252 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7255 fprintf (pcp_outfile
, "#undef ");
7256 while (is_idchar
[*cp
]) /* Ick! */
7257 fputc (*cp
++, pcp_outfile
);
7258 putc ('\n', pcp_outfile
);
7262 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7263 if (start_of_file
&& !skip
) {
7264 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7265 bcopy ((char *) buf
, (char *) control_macro
, end
- buf
);
7266 control_macro
[end
- buf
] = 0;
7270 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7274 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7275 If this is a #ifndef starting at the beginning of a file,
7276 CONTROL_MACRO is the macro name tested by the #ifndef.
7277 Otherwise, CONTROL_MACRO is 0. */
7280 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7283 enum node_type type
;
7284 U_CHAR
*control_macro
;
7287 IF_STACK_FRAME
*temp
;
7289 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7290 temp
->fname
= ip
->nominal_fname
;
7291 temp
->fname_len
= ip
->nominal_fname_len
;
7292 temp
->lineno
= ip
->lineno
;
7293 temp
->next
= if_stack
;
7294 temp
->control_macro
= control_macro
;
7297 if_stack
->type
= type
;
7300 skip_if_group (ip
, 0, op
);
7303 ++if_stack
->if_succeeded
;
7304 output_line_directive (ip
, &outbuf
, 1, same_file
);
7308 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7309 Leaves input ptr at the sharp sign found.
7310 If ANY is nonzero, return at next directive of any sort. */
7313 skip_if_group (ip
, any
, op
)
7318 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7319 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7320 struct directive
*kt
;
7321 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7322 U_CHAR
*beg_of_line
= bp
;
7323 register int ident_length
;
7324 U_CHAR
*ident
, *after_ident
;
7325 /* Save info about where the group starts. */
7326 U_CHAR
*beg_of_group
= bp
;
7327 int beg_lineno
= ip
->lineno
;
7328 int skipping_include_directive
= 0;
7330 if (output_conditionals
&& op
!= 0) {
7331 char *ptr
= "#failed\n";
7332 int len
= strlen (ptr
);
7334 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7339 check_expand (op
, len
);
7340 bcopy (ptr
, (char *) op
->bufp
, len
);
7343 output_line_directive (ip
, op
, 1, 0);
7348 case '/': /* possible comment */
7352 || (cplusplus_comments
&& *bp
== '/')) {
7354 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7358 if (skipping_include_directive
) {
7359 while (bp
< endb
&& *bp
!= '>' && *bp
!= '\n') {
7360 if (*bp
== '\\' && bp
[1] == '\n') {
7369 if (skipping_include_directive
) {
7370 while (bp
< endb
&& *bp
!= '\n') {
7375 if (*bp
== '\\' && bp
[1] == '\n') {
7385 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7386 NULL_PTR
, NULL_PTR
);
7389 /* Char after backslash loses its special meaning in some cases. */
7393 } else if (traditional
&& bp
< endb
)
7399 skipping_include_directive
= 0;
7402 if (beg_of_line
== 0 || traditional
)
7405 while (bp
[0] == '\\' && bp
[1] == '\n')
7411 /* # keyword: a # must be first nonblank char on the line */
7412 if (beg_of_line
== 0)
7416 /* Scan from start of line, skipping whitespace, comments
7417 and backslash-newlines, and see if we reach this #.
7418 If not, this # is not special. */
7420 /* If -traditional, require # to be at beginning of line. */
7423 if (is_hor_space
[*bp
])
7425 else if (*bp
== '\\' && bp
[1] == '\n')
7427 else if (*bp
== '/' && bp
[1] == '*') {
7441 #ifdef MULTIBYTE_CHARS
7443 length
= local_mblen (bp
, endb
- bp
);
7451 /* There is no point in trying to deal with C++ // comments here,
7452 because if there is one, then this # must be part of the
7453 comment and we would never reach here. */
7457 if (bp
!= ip
->bufp
) {
7458 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7462 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7463 if (ip
->bufp
[0] == '%') {
7464 /* Skip past the ':' again. */
7465 while (*bp
== '\\') {
7472 /* Skip whitespace and \-newline. */
7474 if (is_hor_space
[*bp
])
7476 else if (*bp
== '\\' && bp
[1] == '\n')
7478 else if (*bp
== '/') {
7480 newline_fix (bp
+ 1);
7482 for (bp
+= 2; ; bp
++) {
7485 else if (*bp
== '*') {
7486 if (bp
[-1] == '/' && warn_comments
)
7487 warning ("`/*' within comment");
7489 newline_fix (bp
+ 1);
7495 #ifdef MULTIBYTE_CHARS
7497 length
= local_mblen (bp
, endb
- bp
);
7504 } else if (bp
[1] == '/' && cplusplus_comments
) {
7505 for (bp
+= 2; ; bp
++) {
7510 warning ("multiline `//' comment");
7515 #ifdef MULTIBYTE_CHARS
7517 length
= local_mblen (bp
, endb
- bp
);
7531 /* Now find end of directive name.
7532 If we encounter a backslash-newline, exchange it with any following
7533 symbol-constituents so that we end up with a contiguous name. */
7540 name_newline_fix (bp
);
7546 ident_length
= bp
- cp
;
7550 /* A line of just `#' becomes blank. */
7552 if (ident_length
== 0 && *after_ident
== '\n') {
7556 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7558 while (is_idchar
[*p
]) {
7559 if (*p
< '0' || *p
> '9')
7563 /* Handle # followed by a line number. */
7564 if (p
!= ident
&& !is_idchar
[*p
]) {
7566 pedwarn ("`#' followed by integer");
7570 /* Avoid error for `###' and similar cases unless -pedantic. */
7572 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7574 if (pedantic
&& !lang_asm
)
7575 pedwarn ("invalid preprocessing directive");
7580 if (!lang_asm
&& pedantic
)
7581 pedwarn ("invalid preprocessing directive name");
7585 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7586 IF_STACK_FRAME
*temp
;
7587 if (ident_length
== kt
->length
7588 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7589 /* If we are asked to return on next directive, do so now. */
7597 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7598 temp
->next
= if_stack
;
7600 temp
->lineno
= ip
->lineno
;
7601 temp
->fname
= ip
->nominal_fname
;
7602 temp
->fname_len
= ip
->nominal_fname_len
;
7603 temp
->type
= kt
->type
;
7607 if (pedantic
&& if_stack
!= save_if_stack
)
7608 validate_else (bp
, endb
);
7610 if (if_stack
== instack
[indepth
].if_stack
) {
7611 error ("`#%s' not within a conditional", kt
->name
);
7614 else if (if_stack
== save_if_stack
)
7615 goto done
; /* found what we came for */
7617 if (kt
->type
!= T_ENDIF
) {
7618 if (if_stack
->type
== T_ELSE
)
7619 error ("`#else' or `#elif' after `#else'");
7620 if_stack
->type
= kt
->type
;
7625 if_stack
= if_stack
->next
;
7630 case T_INCLUDE_NEXT
:
7632 skipping_include_directive
= 1;
7641 /* Don't let erroneous code go by. */
7642 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7643 pedwarn ("invalid preprocessing directive name");
7648 /* after this returns, rescan will exit because ip->bufp
7649 now points to the end of the buffer.
7650 rescan is responsible for the error message also. */
7653 if (output_conditionals
&& op
!= 0) {
7654 char *ptr
= "#endfailed\n";
7655 int len
= strlen (ptr
);
7657 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7662 check_expand (op
, beg_of_line
- beg_of_group
);
7663 bcopy ((char *) beg_of_group
, (char *) op
->bufp
,
7664 beg_of_line
- beg_of_group
);
7665 op
->bufp
+= beg_of_line
- beg_of_group
;
7666 op
->lineno
+= ip
->lineno
- beg_lineno
;
7667 check_expand (op
, len
);
7668 bcopy (ptr
, (char *) op
->bufp
, len
);
7674 /* Handle a #else directive. Do this by just continuing processing
7675 without changing if_stack ; this is so that the error message
7676 for missing #endif's etc. will point to the original #if. It
7677 is possible that something different would be better. */
7680 do_else (buf
, limit
, op
, keyword
)
7681 U_CHAR
*buf
, *limit
;
7683 struct directive
*keyword
;
7685 FILE_BUF
*ip
= &instack
[indepth
];
7688 SKIP_WHITE_SPACE (buf
);
7690 pedwarn ("text following `#else' violates ANSI standard");
7693 if (if_stack
== instack
[indepth
].if_stack
) {
7694 error ("`#else' not within a conditional");
7697 /* #ifndef can't have its special treatment for containing the whole file
7698 if it has a #else clause. */
7699 if_stack
->control_macro
= 0;
7701 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7702 error ("`#else' after `#else'");
7703 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7704 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7705 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7706 if_stack
->fname_len
))) {
7707 fprintf (stderr
, ", file ");
7708 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7709 if_stack
->fname_len
, stderr
);
7711 fprintf (stderr
, ")\n");
7713 if_stack
->type
= T_ELSE
;
7716 if (if_stack
->if_succeeded
)
7717 skip_if_group (ip
, 0, op
);
7719 ++if_stack
->if_succeeded
; /* continue processing input */
7720 output_line_directive (ip
, op
, 1, same_file
);
7725 /* Unstack after #endif directive. */
7728 do_endif (buf
, limit
, op
, keyword
)
7729 U_CHAR
*buf
, *limit
;
7731 struct directive
*keyword
;
7734 SKIP_WHITE_SPACE (buf
);
7736 pedwarn ("text following `#endif' violates ANSI standard");
7739 if (if_stack
== instack
[indepth
].if_stack
)
7740 error ("unbalanced `#endif'");
7742 IF_STACK_FRAME
*temp
= if_stack
;
7743 if_stack
= if_stack
->next
;
7744 if (temp
->control_macro
!= 0) {
7745 /* This #endif matched a #ifndef at the start of the file.
7746 See if it is at the end of the file. */
7747 FILE_BUF
*ip
= &instack
[indepth
];
7748 U_CHAR
*p
= ip
->bufp
;
7749 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7755 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7756 /* Skip this comment. */
7758 U_CHAR
*save_bufp
= ip
->bufp
;
7760 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7761 ip
->bufp
= save_bufp
;
7766 /* If we get here, this #endif ends a #ifndef
7767 that contains all of the file (aside from whitespace).
7768 Arrange not to include the file again
7769 if the macro that was tested is defined.
7771 Do not do this for the top-level file in a -include or any
7772 file in a -imacros. */
7774 && ! (indepth
== 1 && no_record_file
)
7775 && ! (no_record_file
&& no_output
))
7776 record_control_macro (ip
->inc
, temp
->control_macro
);
7780 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
7785 /* When an #else or #endif is found while skipping failed conditional,
7786 if -pedantic was specified, this is called to warn about text after
7787 the directive name. P points to the first char after the directive
7791 validate_else (p
, limit
)
7793 register U_CHAR
*limit
;
7795 /* Advance P over whitespace and comments. */
7797 while (*p
== '\\' && p
[1] == '\n')
7799 if (is_hor_space
[*p
])
7801 else if (*p
== '/') {
7802 while (p
[1] == '\\' && p
[2] == '\n')
7805 /* Don't bother warning about unterminated comments
7806 since that will happen later. Just be sure to exit. */
7807 for (p
+= 2; ; p
++) {
7811 while (p
[1] == '\\' && p
[2] == '\n')
7820 #ifdef MULTIBYTE_CHARS
7822 length
= local_mblen (p
, limit
- p
);
7829 else if (cplusplus_comments
&& p
[1] == '/')
7835 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
7838 /* Skip a comment, assuming the input ptr immediately follows the
7839 initial slash-star. Bump *LINE_COUNTER for each newline.
7840 (The canonical line counter is &ip->lineno.)
7841 Don't use this routine (or the next one) if bumping the line
7842 counter is not sufficient to deal with newlines in the string.
7844 If NOWARN is nonzero, don't warn about slash-star inside a comment.
7845 This feature is useful when processing a comment that is going to
7846 be processed or was processed at another point in the preprocessor,
7847 to avoid a duplicate warning. Likewise for unterminated comment
7851 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
7852 register FILE_BUF
*ip
;
7853 int *line_counter
; /* place to remember newlines, or NULL */
7856 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
7857 register U_CHAR
*bp
= ip
->bufp
;
7858 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
7859 int start_line
= line_counter
? *line_counter
: 0;
7861 /* JF this line_counter stuff is a crock to make sure the
7862 comment is only put out once, no matter how many times
7863 the comment is skipped. It almost works */
7866 *op
->bufp
++ = bp
[-1];
7868 if (cplusplus_comments
&& bp
[-1] == '/') {
7869 for (; bp
< limit
; bp
++) {
7873 if (!nowarn
&& warn_comments
)
7874 warning ("multiline `//' comment");
7882 #ifdef MULTIBYTE_CHARS
7884 length
= local_mblen (bp
, limit
- bp
);
7889 bcopy (bp
, op
->bufp
, length
- 1);
7890 op
->bufp
+= (length
- 1);
7902 while (bp
< limit
) {
7907 /* If this is the end of the file, we have an unterminated comment.
7908 Don't swallow the newline. We are guaranteed that there will be a
7909 trailing newline and various pieces assume it's there. */
7916 if (line_counter
!= NULL
)
7922 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
7923 warning ("`/*' within comment");
7933 #ifdef MULTIBYTE_CHARS
7938 length
= local_mblen (bp
, limit
- bp
);
7944 bcopy (bp
, op
->bufp
, length
);
7954 error_with_line (line_for_error (start_line
), "unterminated comment");
7959 /* Skip over a quoted string. BP points to the opening quote.
7960 Returns a pointer after the closing quote. Don't go past LIMIT.
7961 START_LINE is the line number of the starting point (but it need
7962 not be valid if the starting point is inside a macro expansion).
7964 The input stack state is not changed.
7966 If COUNT_NEWLINES is nonzero, it points to an int to increment
7967 for each newline passed; also, warn about any white space
7968 just before line end.
7970 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
7971 if we pass a backslash-newline.
7973 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
7976 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
7977 register U_CHAR
*bp
;
7978 register U_CHAR
*limit
;
7980 int *count_newlines
;
7981 int *backslash_newlines_p
;
7984 register U_CHAR c
, match
;
7989 error_with_line (line_for_error (start_line
),
7990 "unterminated string or character constant");
7991 error_with_line (multiline_string_line
,
7992 "possible real start of unterminated constant");
7993 multiline_string_line
= 0;
8000 while (*bp
== '\\' && bp
[1] == '\n') {
8001 if (backslash_newlines_p
)
8002 *backslash_newlines_p
= 1;
8008 if (backslash_newlines_p
)
8009 *backslash_newlines_p
= 1;
8014 } else if (c
== '\n') {
8016 /* Unterminated strings and character constants are 'valid'. */
8017 bp
--; /* Don't consume the newline. */
8022 if (match
== '\'') {
8023 error_with_line (line_for_error (start_line
),
8024 "unterminated character constant");
8030 /* If not traditional, then allow newlines inside strings. */
8031 if (count_newlines
) {
8032 if (warn_white_space
&& is_hor_space
[bp
[-2]])
8033 warning ("white space at end of line in string");
8036 if (multiline_string_line
== 0) {
8038 pedwarn_with_line (line_for_error (start_line
),
8039 "string constant runs past end of line");
8040 multiline_string_line
= start_line
;
8042 } else if (c
== match
)
8044 #ifdef MULTIBYTE_CHARS
8048 length
= local_mblen (bp
, limit
- bp
);
8058 /* Place into DST a quoted string representing the string SRC.
8059 SRCLEN is the length of SRC; SRC may contain null bytes.
8060 Return the address of DST's terminating null. */
8063 quote_string (dst
, src
, srclen
)
8068 char *srclim
= src
+ srclen
;
8071 while (src
!= srclim
)
8072 switch ((c
= *src
++))
8079 sprintf (dst
, "\\%03o", c
);
8096 /* Skip across a group of balanced parens, starting from IP->bufp.
8097 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8099 This does not handle newlines, because it's used for the arg of #if,
8100 where there aren't any newlines. Also, backslash-newline can't appear. */
8103 skip_paren_group (ip
)
8104 register FILE_BUF
*ip
;
8106 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8107 U_CHAR
*p
= ip
->bufp
;
8109 int lines_dummy
= 0;
8111 while (p
!= limit
) {
8121 return ip
->bufp
= p
;
8127 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
8135 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
8137 return ip
->bufp
= p
;
8147 /* Write out a #line directive, for instance, after an #include file.
8148 If CONDITIONAL is nonzero, we can omit the #line if it would
8149 appear to be a no-op, and we can output a few newlines instead
8150 if we want to increase the line number by a small amount.
8151 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8154 output_line_directive (ip
, op
, conditional
, file_change
)
8157 enum file_change_code file_change
;
8160 char *line_directive_buf
, *line_end
;
8162 if (no_line_directives
8163 || ip
->fname
== NULL
8165 op
->lineno
= ip
->lineno
;
8170 if (ip
->lineno
== op
->lineno
)
8173 /* If the inherited line number is a little too small,
8174 output some newlines instead of a #line directive. */
8175 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
8176 check_expand (op
, 10);
8177 while (ip
->lineno
> op
->lineno
) {
8185 /* Output a positive line number if possible. */
8186 while (ip
->lineno
<= 0 && ip
->bufp
- ip
->buf
< ip
->length
8187 && *ip
->bufp
== '\n') {
8192 line_directive_buf
= (char *) alloca (4 * ip
->nominal_fname_len
+ 100);
8193 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
8194 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
8195 ip
->nominal_fname
, ip
->nominal_fname_len
);
8196 if (file_change
!= same_file
) {
8198 *line_end
++ = file_change
== enter_file
? '1' : '2';
8200 /* Tell cc1 if following text comes from a system header file. */
8201 if (ip
->system_header_p
) {
8205 #ifndef NO_IMPLICIT_EXTERN_C
8206 /* Tell cc1plus if following text should be treated as C. */
8207 if (ip
->system_header_p
== 2 && cplusplus
) {
8213 len
= line_end
- line_directive_buf
;
8214 check_expand (op
, len
+ 1);
8215 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
8217 bcopy ((char *) line_directive_buf
, (char *) op
->bufp
, len
);
8219 op
->lineno
= ip
->lineno
;
8222 /* This structure represents one parsed argument in a macro call.
8223 `raw' points to the argument text as written (`raw_length' is its length).
8224 `expanded' points to the argument's macro-expansion
8225 (its length is `expand_length', and its allocated size is `expand_size').
8226 `stringified_length_bound' is an upper bound on the length
8227 the argument would have if stringified.
8228 `use_count' is the number of times this macro arg is substituted
8229 into the macro. If the actual use count exceeds 10,
8230 the value stored is 10.
8231 `free1' and `free2', if nonzero, point to blocks to be freed
8232 when the macro argument data is no longer needed. */
8235 U_CHAR
*raw
, *expanded
;
8236 int raw_length
, expand_length
, expand_size
;
8237 int stringified_length_bound
;
8238 U_CHAR
*free1
, *free2
;
8243 /* Expand a macro call.
8244 HP points to the symbol that is the macro being called.
8245 Put the result of expansion onto the input stack
8246 so that subsequent input by our caller will use it.
8248 If macro wants arguments, caller has already verified that
8249 an argument list follows; arguments come from the input stack. */
8252 macroexpand (hp
, op
)
8257 DEFINITION
*defn
= hp
->value
.defn
;
8258 register U_CHAR
*xbuf
;
8260 int start_line
= instack
[indepth
].lineno
;
8261 int rest_args
, rest_zero
;
8263 CHECK_DEPTH (return;);
8265 /* it might not actually be a macro. */
8266 if (hp
->type
!= T_MACRO
) {
8267 special_symbol (hp
, op
);
8271 /* This macro is being used inside a #if, which means it must be */
8272 /* recorded as a precondition. */
8273 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
8274 dump_single_macro (hp
, pcp_outfile
);
8276 nargs
= defn
->nargs
;
8280 struct argdata
*args
;
8281 int parse_error
= 0;
8283 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
8285 for (i
= 0; i
< nargs
; i
++) {
8286 args
[i
].raw
= (U_CHAR
*) "";
8287 args
[i
].expanded
= 0;
8288 args
[i
].raw_length
= args
[i
].expand_length
= args
[i
].expand_size
8289 = args
[i
].stringified_length_bound
= 0;
8290 args
[i
].free1
= args
[i
].free2
= 0;
8291 args
[i
].use_count
= 0;
8294 /* Parse all the macro args that are supplied. I counts them.
8295 The first NARGS args are stored in ARGS.
8296 The rest are discarded.
8297 If rest_args is set then we assume macarg absorbed the rest of the args.
8302 /* Discard the open-parenthesis or comma before the next arg. */
8303 ++instack
[indepth
].bufp
;
8306 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
8307 /* If we are working on last arg which absorbs rest of args... */
8308 if (i
== nargs
- 1 && defn
->rest_args
)
8310 parse_error
= macarg (&args
[i
], rest_args
);
8313 parse_error
= macarg (NULL_PTR
, 0);
8315 error_with_line (line_for_error (start_line
),
8316 "unterminated macro call");
8320 } while (*instack
[indepth
].bufp
!= ')');
8322 /* If we got one arg but it was just whitespace, call that 0 args. */
8324 register U_CHAR
*bp
= args
[0].raw
;
8325 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
8326 /* cpp.texi says for foo ( ) we provide one argument.
8327 However, if foo wants just 0 arguments, treat this as 0. */
8329 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8334 /* Don't output an error message if we have already output one for
8335 a parse error above. */
8337 if (nargs
== 0 && i
> 0) {
8339 error ("arguments given to macro `%s'", hp
->name
);
8340 } else if (i
< nargs
) {
8341 /* traditional C allows foo() if foo wants one argument. */
8342 if (nargs
== 1 && i
== 0 && traditional
)
8344 /* the rest args token is allowed to absorb 0 tokens */
8345 else if (i
== nargs
- 1 && defn
->rest_args
)
8347 else if (parse_error
)
8350 error ("macro `%s' used without args", hp
->name
);
8352 error ("macro `%s' used with just one arg", hp
->name
);
8354 error ("macro `%s' used with only %d args", hp
->name
, i
);
8355 } else if (i
> nargs
) {
8357 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8360 /* Swallow the closeparen. */
8361 ++instack
[indepth
].bufp
;
8363 /* If macro wants zero args, we parsed the arglist for checking only.
8364 Read directly from the macro definition. */
8366 xbuf
= defn
->expansion
;
8367 xbuf_len
= defn
->length
;
8369 register U_CHAR
*exp
= defn
->expansion
;
8370 register int offset
; /* offset in expansion,
8371 copied a piece at a time */
8372 register int totlen
; /* total amount of exp buffer filled so far */
8374 register struct reflist
*ap
, *last_ap
;
8376 /* Macro really takes args. Compute the expansion of this call. */
8378 /* Compute length in characters of the macro's expansion.
8379 Also count number of times each arg is used. */
8380 xbuf_len
= defn
->length
;
8381 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8383 xbuf_len
+= args
[ap
->argno
].stringified_length_bound
;
8384 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8385 /* Add 4 for two newline-space markers to prevent
8386 token concatenation. */
8387 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8389 /* We have an ordinary (expanded) occurrence of the arg.
8390 So compute its expansion, if we have not already. */
8391 if (args
[ap
->argno
].expanded
== 0) {
8393 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8394 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8397 args
[ap
->argno
].expanded
= obuf
.buf
;
8398 args
[ap
->argno
].expand_length
= obuf
.bufp
- obuf
.buf
;
8399 args
[ap
->argno
].expand_size
= obuf
.length
;
8400 args
[ap
->argno
].free2
= obuf
.buf
;
8402 xbuf_len
+= args
[ap
->argno
].expand_length
;
8404 /* If the arg appears more than once, its later occurrences
8405 may have newline turned into backslash-'n', which is a
8406 factor of 2 expansion. */
8407 xbuf_len
+= 2 * args
[ap
->argno
].expand_length
;
8409 /* Add 4 for two newline-space markers to prevent
8410 token concatenation. */
8413 if (args
[ap
->argno
].use_count
< 10)
8414 args
[ap
->argno
].use_count
++;
8417 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8419 /* Generate in XBUF the complete expansion
8420 with arguments substituted in.
8421 TOTLEN is the total size generated so far.
8422 OFFSET is the index in the definition
8423 of where we are copying from. */
8424 offset
= totlen
= 0;
8425 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8426 last_ap
= ap
, ap
= ap
->next
) {
8427 register struct argdata
*arg
= &args
[ap
->argno
];
8428 int count_before
= totlen
;
8430 /* Add chars to XBUF. */
8431 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8432 xbuf
[totlen
++] = exp
[offset
];
8434 /* If followed by an empty rest arg with concatenation,
8435 delete the last run of nonwhite chars. */
8436 if (rest_zero
&& totlen
> count_before
8437 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8438 || (last_ap
!= NULL
&& last_ap
->rest_args
8439 && last_ap
->raw_after
!= 0))) {
8440 /* Delete final whitespace. */
8441 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8445 /* Delete the nonwhites before them. */
8446 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8451 if (ap
->stringify
!= 0) {
8452 int arglen
= arg
->raw_length
;
8458 && (c
= arg
->raw
[i
], is_space
[c
]))
8461 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8464 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8465 for (; i
< arglen
; i
++) {
8469 /* Generate nothing for backslash-newline in a string. */
8470 if (c
== '\\' && arg
->raw
[i
+ 1] == '\n') {
8476 generate nothing for a stringified argument. */
8482 /* Internal sequences of whitespace are replaced by one space
8483 except within a string or char token. */
8486 while (is_space
[(c
= arg
->raw
[i
])])
8487 /* Newline markers can occur within a whitespace sequence;
8488 consider them part of the sequence. */
8489 i
+= (c
== '\n') + 1;
8500 else if (in_string
) {
8505 #ifdef MULTIBYTE_CHARS
8507 length
= local_mblen (arg
->raw
+ i
, arglen
- i
);
8510 bcopy (arg
->raw
+ i
, xbuf
+ totlen
, length
);
8517 } else if (c
== '\"' || c
== '\'')
8521 /* Escape double-quote, and backslashes in strings.
8522 Newlines in strings are best escaped as \n, since
8523 otherwise backslash-backslash-newline-newline is
8524 mishandled. The C Standard doesn't allow newlines in
8525 strings, so we can escape newlines as we please. */
8529 || (c
== '\n' ? (c
= 'n', 1) : 0))))
8530 xbuf
[totlen
++] = '\\';
8531 /* We used to output e.g. \008 for control characters here,
8532 but this doesn't conform to the C Standard.
8533 Just output the characters as-is. */
8537 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8538 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8539 U_CHAR
*p1
= arg
->raw
;
8540 U_CHAR
*l1
= p1
+ arg
->raw_length
;
8541 if (ap
->raw_before
!= 0) {
8542 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8543 while (p1
!= l1
&& is_idchar
[*p1
])
8544 xbuf
[totlen
++] = *p1
++;
8545 /* Delete any no-reexpansion marker that follows
8546 an identifier at the beginning of the argument
8547 if the argument is concatenated with what precedes it. */
8548 if (p1
[0] == '\n' && p1
[1] == '-')
8550 } else if (!traditional
) {
8551 /* Ordinary expanded use of the argument.
8552 Put in newline-space markers to prevent token pasting. */
8553 xbuf
[totlen
++] = '\n';
8554 xbuf
[totlen
++] = ' ';
8556 if (ap
->raw_after
!= 0) {
8557 /* Arg is concatenated after: delete trailing whitespace,
8558 whitespace markers, and no-reexpansion markers. */
8560 if (is_space
[l1
[-1]]) l1
--;
8561 else if (l1
[-1] == '-') {
8562 U_CHAR
*p2
= l1
- 1;
8563 /* If a `-' is preceded by an odd number of newlines then it
8564 and the last newline are a no-reexpansion marker. */
8565 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8566 if ((l1
- 1 - p2
) & 1) {
8575 bcopy ((char *) p1
, (char *) (xbuf
+ totlen
), l1
- p1
);
8577 if (!traditional
&& ap
->raw_after
== 0) {
8578 /* Ordinary expanded use of the argument.
8579 Put in newline-space markers to prevent token pasting. */
8580 xbuf
[totlen
++] = '\n';
8581 xbuf
[totlen
++] = ' ';
8584 /* Ordinary expanded use of the argument.
8585 Put in newline-space markers to prevent token pasting. */
8587 xbuf
[totlen
++] = '\n';
8588 xbuf
[totlen
++] = ' ';
8590 bcopy ((char *) arg
->expanded
, (char *) (xbuf
+ totlen
),
8591 arg
->expand_length
);
8592 totlen
+= arg
->expand_length
;
8594 xbuf
[totlen
++] = '\n';
8595 xbuf
[totlen
++] = ' ';
8597 /* If a macro argument with newlines is used multiple times,
8598 then only expand the newlines once. This avoids creating output
8599 lines which don't correspond to any input line, which confuses
8601 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8602 /* Don't bother doing change_newlines for subsequent
8605 change_newlines (arg
);
8609 if (totlen
> xbuf_len
)
8613 /* If there is anything left of the definition after handling
8614 the arg list, copy that in too. */
8616 for (i
= offset
; i
< defn
->length
; i
++) {
8617 /* if we've reached the end of the macro */
8620 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8621 && last_ap
->raw_after
!= 0))
8622 xbuf
[totlen
++] = exp
[i
];
8628 for (i
= 0; i
< nargs
; i
++) {
8629 if (args
[i
].free1
!= 0)
8630 free (args
[i
].free1
);
8631 if (args
[i
].free2
!= 0)
8632 free (args
[i
].free2
);
8636 xbuf
= defn
->expansion
;
8637 xbuf_len
= defn
->length
;
8640 /* Now put the expansion on the input stack
8641 so our caller will commence reading from it. */
8643 register FILE_BUF
*ip2
;
8645 ip2
= &instack
[++indepth
];
8648 ip2
->nominal_fname
= 0;
8649 ip2
->nominal_fname_len
= 0;
8651 /* This may not be exactly correct, but will give much better error
8652 messages for nested macro calls than using a line number of zero. */
8653 ip2
->lineno
= start_line
;
8655 ip2
->length
= xbuf_len
;
8657 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8659 ip2
->if_stack
= if_stack
;
8660 ip2
->system_header_p
= 0;
8662 /* Recursive macro use sometimes works traditionally.
8663 #define foo(x,y) bar (x (y,0), y)
8667 hp
->type
= T_DISABLED
;
8671 /* Parse a macro argument and store the info on it into *ARGPTR.
8672 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8673 Return nonzero to indicate a syntax error. */
8676 macarg (argptr
, rest_args
)
8677 register struct argdata
*argptr
;
8680 FILE_BUF
*ip
= &instack
[indepth
];
8682 int lineno0
= ip
->lineno
;
8686 /* Try to parse as much of the argument as exists at this
8687 input stack level. */
8688 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
,
8689 &paren
, &ip
->lineno
, &comments
, rest_args
);
8691 /* If we find the end of the argument at this level,
8692 set up *ARGPTR to point at it in the input stack. */
8693 if (!(ip
->fname
!= 0 && (ip
->lineno
!= lineno0
|| comments
!= 0))
8694 && bp
!= ip
->buf
+ ip
->length
) {
8696 argptr
->raw
= ip
->bufp
;
8697 argptr
->raw_length
= bp
- ip
->bufp
;
8698 argptr
->newlines
= ip
->lineno
- lineno0
;
8702 /* This input stack level ends before the macro argument does.
8703 We must pop levels and keep parsing.
8704 Therefore, we must allocate a temporary buffer and copy
8705 the macro argument into it. */
8706 int bufsize
= bp
- ip
->bufp
;
8707 int extra
= ip
->lineno
- lineno0
;
8708 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8709 int final_start
= 0;
8711 bcopy ((char *) ip
->bufp
, (char *) buffer
, bufsize
);
8714 while (bp
== ip
->buf
+ ip
->length
) {
8715 if (instack
[indepth
].macro
== 0) {
8719 ip
->macro
->type
= T_MACRO
;
8721 free (ip
->free_ptr
);
8722 ip
= &instack
[--indepth
];
8723 lineno0
= ip
->lineno
;
8725 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
, &paren
,
8726 &ip
->lineno
, &comments
, rest_args
);
8727 final_start
= bufsize
;
8728 bufsize
+= bp
- ip
->bufp
;
8729 extra
+= ip
->lineno
- lineno0
;
8730 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
8731 bcopy ((char *) ip
->bufp
, (char *) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
8736 /* Now, if arg is actually wanted, record its raw form,
8737 discarding comments and duplicating newlines in whatever
8738 part of it did not come from a macro expansion.
8739 EXTRA space has been preallocated for duplicating the newlines.
8740 FINAL_START is the index of the start of that part. */
8742 argptr
->raw
= buffer
;
8743 argptr
->raw_length
= bufsize
;
8744 argptr
->free1
= buffer
;
8745 argptr
->newlines
= ip
->lineno
- lineno0
;
8746 if ((argptr
->newlines
|| comments
) && ip
->fname
!= 0)
8749 discard_comments (argptr
->raw
+ final_start
,
8750 argptr
->raw_length
- final_start
,
8752 argptr
->raw
[argptr
->raw_length
] = 0;
8753 if (argptr
->raw_length
> bufsize
+ extra
)
8758 /* If we are not discarding this argument,
8759 macroexpand it and compute its length as stringified.
8760 All this info goes into *ARGPTR. */
8763 register U_CHAR
*buf
, *lim
;
8764 register int totlen
;
8767 lim
= buf
+ argptr
->raw_length
;
8769 while (buf
!= lim
&& is_space
[*buf
])
8771 while (buf
!= lim
&& is_space
[lim
[-1]])
8773 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
8774 while (buf
!= lim
) {
8775 register U_CHAR c
= *buf
++;
8777 /* Internal sequences of whitespace are replaced by one space
8778 in most cases, but not always. So count all the whitespace
8779 in case we need to keep it all. */
8782 SKIP_ALL_WHITE_SPACE (buf
);
8785 if (c
== '\"' || c
== '\\' || c
== '\n') /* escape these chars */
8788 argptr
->stringified_length_bound
= totlen
;
8793 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8794 taken from the expansion of MACRO,
8795 counting parens in *DEPTHPTR,
8796 and return if reach LIMIT
8797 or before a `)' that would make *DEPTHPTR negative
8798 or before a comma when *DEPTHPTR is zero.
8799 Single and double quotes are matched and termination
8800 is inhibited within them. Comments also inhibit it.
8801 Value returned is pointer to stopping place.
8803 Increment *NEWLINES each time a newline is passed.
8804 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8805 Set *COMMENTS to 1 if a comment is seen. */
8808 macarg1 (start
, limit
, macro
, depthptr
, newlines
, comments
, rest_args
)
8810 register U_CHAR
*limit
;
8811 struct hashnode
*macro
;
8812 int *depthptr
, *newlines
, *comments
;
8815 register U_CHAR
*bp
= start
;
8817 while (bp
< limit
) {
8823 if (--(*depthptr
) < 0)
8827 /* Traditionally, backslash makes following char not special. */
8828 if (traditional
&& bp
+ 1 < limit
&& bp
[1] != '\n')
8838 newline_fix (bp
+ 1);
8841 for (bp
+= 2; bp
< limit
; bp
++) {
8844 else if (*bp
== '*') {
8845 if (bp
[-1] == '/' && warn_comments
)
8846 warning ("`/*' within comment");
8848 newline_fix (bp
+ 1);
8856 #ifdef MULTIBYTE_CHARS
8858 length
= local_mblen (bp
, limit
- bp
);
8864 } else if (bp
[1] == '/' && cplusplus_comments
) {
8866 for (bp
+= 2; bp
< limit
; bp
++) {
8872 warning ("multiline `//' comment");
8876 #ifdef MULTIBYTE_CHARS
8878 length
= local_mblen (bp
, limit
- bp
);
8890 for (quotec
= *bp
++; bp
< limit
&& *bp
!= quotec
; bp
++) {
8895 while (*bp
== '\\' && bp
[1] == '\n') {
8899 } else if (*bp
== '\n') {
8900 if (warn_white_space
&& is_hor_space
[bp
[-1]] && ! macro
)
8901 warning ("white space at end of line in string");
8908 #ifdef MULTIBYTE_CHARS
8910 length
= local_mblen (bp
, limit
- bp
);
8919 /* if we've returned to lowest level and we aren't absorbing all args */
8920 if ((*depthptr
) == 0 && rest_args
== 0)
8930 /* Discard comments and duplicate newlines
8931 in the string of length LENGTH at START,
8932 except inside of string constants.
8933 The string is copied into itself with its beginning staying fixed.
8935 NEWLINES is the number of newlines that must be duplicated.
8936 We assume that that much extra space is available past the end
8940 discard_comments (start
, length
, newlines
)
8945 register U_CHAR
*ibp
;
8946 register U_CHAR
*obp
;
8947 register U_CHAR
*limit
;
8950 /* If we have newlines to duplicate, copy everything
8951 that many characters up. Then, in the second part,
8952 we will have room to insert the newlines
8954 NEWLINES may actually be too large, because it counts
8955 newlines in string constants, and we don't duplicate those.
8956 But that does no harm. */
8958 ibp
= start
+ length
;
8959 obp
= ibp
+ newlines
;
8961 while (limit
!= ibp
)
8965 ibp
= start
+ newlines
;
8966 limit
= start
+ length
+ newlines
;
8969 while (ibp
< limit
) {
8970 *obp
++ = c
= *ibp
++;
8973 /* Duplicate the newline. */
8987 /* Delete any comment. */
8988 if (cplusplus_comments
&& ibp
[0] == '/') {
8989 /* Comments are equivalent to spaces. */
8996 if (ibp
[-1] != '\\')
9001 #ifdef MULTIBYTE_CHARS
9002 int length
= local_mblen (ibp
, limit
- ibp
);
9004 ibp
+= (length
- 1);
9011 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
9013 /* Comments are equivalent to spaces.
9014 For -traditional, a comment is equivalent to nothing. */
9019 while (++ibp
< limit
) {
9020 if (ibp
[0] == '*') {
9022 newline_fix (ibp
+ 1);
9023 if (ibp
[1] == '/') {
9030 #ifdef MULTIBYTE_CHARS
9031 int length
= local_mblen (ibp
, limit
- ibp
);
9033 ibp
+= (length
- 1);
9041 /* Notice and skip strings, so that we don't
9042 think that comments start inside them,
9043 and so we don't duplicate newlines in them. */
9046 while (ibp
< limit
) {
9047 *obp
++ = c
= *ibp
++;
9055 else if (c
== '\\') {
9056 if (ibp
< limit
&& *ibp
== '\n') {
9060 while (*ibp
== '\\' && ibp
[1] == '\n')
9068 #ifdef MULTIBYTE_CHARS
9071 length
= local_mblen (ibp
, limit
- ibp
);
9075 bcopy (ibp
, obp
, length
);
9092 /* Turn newlines to spaces in the macro argument ARG.
9093 Remove backslash-newline from string constants,
9094 and turn other newlines in string constants to backslash-'n'. */
9097 change_newlines (arg
)
9098 struct argdata
*arg
;
9100 U_CHAR
*start
= arg
->expanded
;
9101 int length
= arg
->expand_length
;
9102 register U_CHAR
*ibp
;
9103 register U_CHAR
*obp
;
9104 register U_CHAR
*limit
;
9107 limit
= start
+ length
;
9110 while (ibp
< limit
) {
9111 switch ((*obp
++ = *ibp
++)) {
9113 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9114 string. Skip past the newline and its duplicate.
9115 Put a space in the output. */
9126 /* Notice and skip strings, to handle their newlines properly. */
9128 U_CHAR
*ibp1
= skip_quoted_string (ibp
- 1, limit
, 0,
9129 NULL_PTR
, NULL_PTR
, NULL_PTR
);
9130 while (ibp
!= ibp1
) {
9131 switch ((*obp
++ = *ibp
++)) {
9133 /* Replace backslash-newline with nothing. */
9141 /* Replace non-backslashed newline with backslash-'n',
9142 replacing the arg buffer with a new one if this hasn't
9143 been done already. */
9145 if (start
== arg
->expanded
) {
9146 int olength
= obp
- arg
->expanded
;
9148 arg
->expand_size
= 2 * arg
->expand_length
;
9149 newbuf
= (U_CHAR
*) xmalloc (arg
->expand_size
);
9150 arg
->free2
= arg
->expanded
= newbuf
;
9151 obp
= newbuf
+ olength
;
9152 bcopy ((char *) start
, (char *) newbuf
, olength
);
9159 #ifdef MULTIBYTE_CHARS
9164 length
= local_mblen (ibp
, ibp1
- ibp
);
9168 bcopy (ibp
, obp
, length
);
9183 arg
->expand_length
= obp
- arg
->expanded
;
9185 if (start
!= arg
->expanded
)
9189 /* my_strerror - return the descriptive text associated with an
9193 my_strerror (errnum
)
9199 #ifndef HAVE_STRERROR
9200 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
9202 result
= strerror (errnum
);
9205 /* VAXCRTL's strerror() takes an optional second argument, which only
9206 matters when the first argument is EVMSERR. However, it's simplest
9207 just to pass it unconditionally. `vaxc$errno' is declared in
9208 <errno.h>, and maintained by the library in parallel with `errno'.
9209 We assume that caller's `errnum' either matches the last setting of
9210 `errno' by the library or else does not have the value `EVMSERR'. */
9212 result
= strerror (errnum
, vaxc$errno
);
9216 result
= "errno = ?";
9221 /* notice - output message to stderr */
9224 notice (PRINTF_ALIST (msgid
))
9229 VA_START (args
, msgid
);
9230 vnotice (msgid
, args
);
9235 vnotice (msgid
, args
)
9239 vfprintf (stderr
, _(msgid
), args
);
9242 /* error - print error message and increment count of errors. */
9245 error (PRINTF_ALIST (msgid
))
9250 VA_START (args
, msgid
);
9251 verror (msgid
, args
);
9256 verror (msgid
, args
)
9261 FILE_BUF
*ip
= NULL
;
9263 print_containing_files ();
9265 for (i
= indepth
; i
>= 0; i
--)
9266 if (instack
[i
].fname
!= NULL
) {
9272 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9273 ip
->nominal_fname_len
, stderr
);
9274 fprintf (stderr
, ":%d: ", ip
->lineno
);
9276 vnotice (msgid
, args
);
9277 fprintf (stderr
, "\n");
9281 /* Error including a message from `errno'. */
9284 error_from_errno (name
)
9289 FILE_BUF
*ip
= NULL
;
9291 print_containing_files ();
9293 for (i
= indepth
; i
>= 0; i
--)
9294 if (instack
[i
].fname
!= NULL
) {
9300 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9301 ip
->nominal_fname_len
, stderr
);
9302 fprintf (stderr
, ":%d: ", ip
->lineno
);
9305 fprintf (stderr
, "%s: %s\n", name
, my_strerror (e
));
9310 /* Print error message but don't count it. */
9313 warning (PRINTF_ALIST (msgid
))
9318 VA_START (args
, msgid
);
9319 vwarning (msgid
, args
);
9324 vwarning (msgid
, args
)
9329 FILE_BUF
*ip
= NULL
;
9331 if (inhibit_warnings
)
9334 if (warnings_are_errors
)
9337 print_containing_files ();
9339 for (i
= indepth
; i
>= 0; i
--)
9340 if (instack
[i
].fname
!= NULL
) {
9346 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9347 ip
->nominal_fname_len
, stderr
);
9348 fprintf (stderr
, ":%d: ", ip
->lineno
);
9350 notice ("warning: ");
9351 vnotice (msgid
, args
);
9352 fprintf (stderr
, "\n");
9356 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9357 error_with_line (int line
, PRINTF_ALIST (msgid
))
9359 error_with_line (line
, PRINTF_ALIST (msgid
))
9366 VA_START (args
, msgid
);
9367 verror_with_line (line
, msgid
, args
);
9372 verror_with_line (line
, msgid
, args
)
9378 FILE_BUF
*ip
= NULL
;
9380 print_containing_files ();
9382 for (i
= indepth
; i
>= 0; i
--)
9383 if (instack
[i
].fname
!= NULL
) {
9389 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9390 ip
->nominal_fname_len
, stderr
);
9391 fprintf (stderr
, ":%d: ", line
);
9393 vnotice (msgid
, args
);
9394 fprintf (stderr
, "\n");
9399 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9400 warning_with_line (int line
, PRINTF_ALIST (msgid
))
9402 warning_with_line (line
, PRINTF_ALIST (msgid
))
9409 VA_START (args
, msgid
);
9410 vwarning_with_line (line
, msgid
, args
);
9415 vwarning_with_line (line
, msgid
, args
)
9421 FILE_BUF
*ip
= NULL
;
9423 if (inhibit_warnings
)
9426 if (warnings_are_errors
)
9429 print_containing_files ();
9431 for (i
= indepth
; i
>= 0; i
--)
9432 if (instack
[i
].fname
!= NULL
) {
9438 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9439 ip
->nominal_fname_len
, stderr
);
9440 fprintf (stderr
, line
? ":%d: " : ": ", line
);
9442 notice ("warning: ");
9443 vnotice (msgid
, args
);
9444 fprintf (stderr
, "\n");
9447 /* Print an error message and maybe count it. */
9450 pedwarn (PRINTF_ALIST (msgid
))
9455 VA_START (args
, msgid
);
9456 if (pedantic_errors
)
9457 verror (msgid
, args
);
9459 vwarning (msgid
, args
);
9464 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9465 pedwarn_with_line (int line
, PRINTF_ALIST (msgid
))
9467 pedwarn_with_line (line
, PRINTF_ALIST (msgid
))
9474 VA_START (args
, msgid
);
9475 if (pedantic_errors
)
9476 verror_with_line (line
, msgid
, args
);
9478 vwarning_with_line (line
, msgid
, args
);
9482 /* Report a warning (or an error if pedantic_errors)
9483 giving specified file name and line number, not current. */
9486 #if defined (ANSI_PROTOTYPES) && defined (HAVE_VPRINTF)
9487 pedwarn_with_file_and_line (char *file
, size_t file_len
, int line
,
9488 PRINTF_ALIST (msgid
))
9490 pedwarn_with_file_and_line (file
, file_len
, line
, PRINTF_ALIST (msgid
))
9499 if (!pedantic_errors
&& inhibit_warnings
)
9502 fwrite (file
, sizeof file
[0], file_len
, stderr
);
9503 fprintf (stderr
, ":%d: ", line
);
9505 if (pedantic_errors
)
9507 if (!pedantic_errors
)
9508 notice ("warning: ");
9509 VA_START (args
, msgid
);
9510 vnotice (msgid
, args
);
9512 fprintf (stderr
, "\n");
9516 pedwarn_strange_white_space (ch
)
9521 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9522 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9523 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9528 /* Print the file names and line numbers of the #include
9529 directives which led to the current file. */
9532 print_containing_files ()
9534 FILE_BUF
*ip
= NULL
;
9538 /* If stack of files hasn't changed since we last printed
9539 this info, don't repeat it. */
9540 if (last_error_tick
== input_file_stack_tick
)
9543 for (i
= indepth
; i
>= 0; i
--)
9544 if (instack
[i
].fname
!= NULL
) {
9549 /* Give up if we don't find a source file. */
9553 /* Find the other, outer source files. */
9554 for (i
--; i
>= 0; i
--)
9555 if (instack
[i
].fname
!= NULL
) {
9559 notice ( "In file included from ");
9561 notice (",\n from ");
9564 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9565 ip
->nominal_fname_len
, stderr
);
9566 fprintf (stderr
, ":%d", ip
->lineno
);
9569 fprintf (stderr
, ":\n");
9571 /* Record we have printed the status as of this time. */
9572 last_error_tick
= input_file_stack_tick
;
9575 /* Return the line at which an error occurred.
9576 The error is not necessarily associated with the current spot
9577 in the input stack, so LINE says where. LINE will have been
9578 copied from ip->lineno for the current input level.
9579 If the current level is for a file, we return LINE.
9580 But if the current level is not for a file, LINE is meaningless.
9581 In that case, we return the lineno of the innermost file. */
9584 line_for_error (line
)
9590 for (i
= indepth
; i
>= 0; ) {
9591 if (instack
[i
].fname
!= 0)
9596 line1
= instack
[i
].lineno
;
9604 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9606 * As things stand, nothing is ever placed in the output buffer to be
9607 * removed again except when it's KNOWN to be part of an identifier,
9608 * so flushing and moving down everything left, instead of expanding,
9612 /* You might think void was cleaner for the return type,
9613 but that would get type mismatch in check_expand in strict ANSI. */
9616 grow_outbuf (obuf
, needed
)
9617 register FILE_BUF
*obuf
;
9618 register int needed
;
9623 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9626 /* Make it at least twice as big as it is now. */
9628 /* Make it have at least 150% of the free space we will need. */
9629 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9630 if (minsize
> obuf
->length
)
9631 obuf
->length
= minsize
;
9633 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
9636 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9642 /* Symbol table for macro names and special symbols */
9645 * install a name in the main hash table, even if it is already there.
9646 * name stops with first non alphanumeric, except leading '#'.
9647 * caller must check against redefinition if that is desired.
9648 * delete_macro () removes things installed by install () in fifo order.
9649 * this is important because of the `defined' special symbol used
9650 * in #if, and also if pushdef/popdef directives are ever implemented.
9652 * If LEN is >= 0, it is the length of the name.
9653 * Otherwise, compute the length by scanning the entire name.
9655 * If HASH is >= 0, it is the precomputed hash code.
9656 * Otherwise, compute the hash code.
9660 install (name
, len
, type
, value
, hash
)
9663 enum node_type type
;
9667 register HASHNODE
*hp
;
9668 register int i
, bucket
;
9669 register U_CHAR
*p
, *q
;
9673 while (is_idchar
[*p
])
9679 hash
= hashf (name
, len
, HASHSIZE
);
9681 i
= sizeof (HASHNODE
) + len
+ 1;
9682 hp
= (HASHNODE
*) xmalloc (i
);
9684 hp
->bucket_hdr
= &hashtab
[bucket
];
9685 hp
->next
= hashtab
[bucket
];
9686 hashtab
[bucket
] = hp
;
9688 if (hp
->next
!= NULL
)
9689 hp
->next
->prev
= hp
;
9692 hp
->value
.cpval
= value
;
9693 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9696 for (i
= 0; i
< len
; i
++)
9703 * find the most recent hash node for name name (ending with first
9704 * non-identifier char) installed by install
9706 * If LEN is >= 0, it is the length of the name.
9707 * Otherwise, compute the length by scanning the entire name.
9709 * If HASH is >= 0, it is the precomputed hash code.
9710 * Otherwise, compute the hash code.
9714 lookup (name
, len
, hash
)
9719 register U_CHAR
*bp
;
9720 register HASHNODE
*bucket
;
9723 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
9728 hash
= hashf (name
, len
, HASHSIZE
);
9730 bucket
= hashtab
[hash
];
9732 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
9734 bucket
= bucket
->next
;
9740 * Delete a hash node. Some weirdness to free junk from macros.
9741 * More such weirdness will have to be added if you define more hash
9742 * types that need it.
9745 /* Note that the DEFINITION of a macro is removed from the hash table
9746 but its storage is not freed. This would be a storage leak
9747 except that it is not reasonable to keep undefining and redefining
9748 large numbers of macros many times.
9749 In any case, this is necessary, because a macro can be #undef'd
9750 in the middle of reading the arguments to a call to it.
9751 If #undef freed the DEFINITION, that would crash. */
9758 if (hp
->prev
!= NULL
)
9759 hp
->prev
->next
= hp
->next
;
9760 if (hp
->next
!= NULL
)
9761 hp
->next
->prev
= hp
->prev
;
9763 /* Make sure that the bucket chain header that the deleted guy was
9764 on points to the right thing afterwards. */
9765 if (hp
== *hp
->bucket_hdr
)
9766 *hp
->bucket_hdr
= hp
->next
;
9769 if (hp
->type
== T_MACRO
) {
9770 DEFINITION
*d
= hp
->value
.defn
;
9771 struct reflist
*ap
, *nextap
;
9773 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
9784 * return hash function on name. must be compatible with the one
9785 * computed a step at a time, elsewhere
9789 hashf (name
, len
, hashsize
)
9790 register U_CHAR
*name
;
9797 r
= HASHSTEP (r
, *name
++);
9799 return MAKE_POS (r
) % hashsize
;
9803 /* Dump the definition of a single macro HP to OF. */
9806 dump_single_macro (hp
, of
)
9807 register HASHNODE
*hp
;
9810 register DEFINITION
*defn
= hp
->value
.defn
;
9816 /* Print the definition of the macro HP. */
9818 fprintf (of
, "#define %s", hp
->name
);
9820 if (defn
->nargs
>= 0) {
9824 for (i
= 0; i
< defn
->nargs
; i
++) {
9825 dump_arg_n (defn
, i
, of
);
9826 if (i
+ 1 < defn
->nargs
)
9836 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
9837 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
9838 offset
+= ap
->nchars
;
9840 if (ap
->nchars
!= 0)
9842 if (ap
->stringify
) {
9843 switch (ap
->stringify
) {
9844 case SHARP_TOKEN
: fprintf (of
, "#"); break;
9845 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
9846 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
9847 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
9851 if (ap
->raw_before
!= 0) {
9853 switch (ap
->raw_before
) {
9854 case WHITE_SHARP_TOKEN
:
9855 case WHITE_PERCENT_COLON_TOKEN
:
9862 switch (ap
->raw_before
) {
9863 case SHARP_TOKEN
: fprintf (of
, "##"); break;
9864 case WHITE_SHARP_TOKEN
: fprintf (of
, "## "); break;
9865 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
9866 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%: "); break;
9873 dump_arg_n (defn
, ap
->argno
, of
);
9874 if (!traditional
&& ap
->raw_after
!= 0) {
9875 switch (ap
->raw_after
) {
9876 case SHARP_TOKEN
: fprintf (of
, "##"); break;
9877 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
9878 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
9879 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
9885 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
9889 /* Dump all macro definitions as #defines to stdout. */
9896 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
9897 register HASHNODE
*hp
;
9899 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
9900 if (hp
->type
== T_MACRO
)
9901 dump_single_macro (hp
, stdout
);
9906 /* Output to OF a substring of a macro definition.
9907 BASE is the beginning of the definition.
9908 Output characters START thru LENGTH.
9909 Unless traditional, discard newlines outside of strings, thus
9910 converting funny-space markers to ordinary spaces. */
9913 dump_defn_1 (base
, start
, length
, of
)
9919 U_CHAR
*p
= base
+ start
;
9920 U_CHAR
*limit
= base
+ start
+ length
;
9923 fwrite (p
, sizeof (*p
), length
, of
);
9926 if (*p
== '\"' || *p
=='\'') {
9927 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
9928 NULL_PTR
, NULL_PTR
);
9929 fwrite (p
, sizeof (*p
), p1
- p
, of
);
9940 /* Print the name of argument number ARGNUM of macro definition DEFN
9942 Recall that DEFN->args.argnames contains all the arg names
9943 concatenated in reverse order with comma-space in between. */
9946 dump_arg_n (defn
, argnum
, of
)
9951 register U_CHAR
*p
= defn
->args
.argnames
;
9952 while (argnum
+ 1 < defn
->nargs
) {
9953 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
9957 while (*p
&& *p
!= ',') {
9963 /* Initialize syntactic classifications of characters. */
9966 initialize_char_syntax ()
9971 * Set up is_idchar and is_idstart tables. These should be
9972 * faster than saying (is_alpha (c) || c == '_'), etc.
9973 * Set up these things before calling any routines tthat
9976 for (i
= 'a'; i
<= 'z'; i
++) {
9977 is_idchar
[i
- 'a' + 'A'] = 1;
9979 is_idstart
[i
- 'a' + 'A'] = 1;
9982 for (i
= '0'; i
<= '9'; i
++)
9985 is_idstart
['_'] = 1;
9987 is_idstart
['$'] = 1;
9989 /* horizontal space table */
9990 is_hor_space
[' '] = 1;
9991 is_hor_space
['\t'] = 1;
9992 is_hor_space
['\v'] = 1;
9993 is_hor_space
['\f'] = 1;
9994 is_hor_space
['\r'] = 1;
10000 is_space
['\n'] = 1;
10001 is_space
['\r'] = 1;
10004 /* Initialize the built-in macros. */
10007 initialize_builtins (inp
, outp
)
10011 install ((U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
10012 install ((U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
10013 install ((U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
10014 install ((U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
10015 install ((U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
10016 install ((U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
10017 #ifndef NO_BUILTIN_SIZE_TYPE
10018 install ((U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
10020 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10021 install ((U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
10023 install ((U_CHAR
*) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, NULL_PTR
, -1);
10024 install ((U_CHAR
*) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
,
10026 install ((U_CHAR
*) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
,
10028 install ((U_CHAR
*) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE
,
10030 install ((U_CHAR
*) "__TIME__", -1, T_TIME
, NULL_PTR
, -1);
10031 if (!traditional
) {
10032 install ((U_CHAR
*) "__STDC__", -1, T_CONST
, "1", -1);
10033 install ((U_CHAR
*) "__STDC_VERSION__", -1, T_CONST
, "199409L", -1);
10036 install ((U_CHAR
*) "__OBJC__", -1, T_CONST
, "1", -1);
10037 /* This is supplied using a -D by the compiler driver
10038 so that it is present only when truly compiling with GNU C. */
10039 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10040 install ((U_CHAR
*) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST
, "1", -1);
10044 char directive
[2048];
10045 U_CHAR
*udirective
= (U_CHAR
*) directive
;
10046 register struct directive
*dp
= &directive_table
[0];
10047 struct tm
*timebuf
= timestamp ();
10049 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
10050 instack
[0].nominal_fname
);
10051 output_line_directive (inp
, outp
, 0, same_file
);
10052 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10055 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
10056 output_line_directive (inp
, outp
, 0, same_file
);
10057 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10060 #ifndef NO_BUILTIN_SIZE_TYPE
10061 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
10062 output_line_directive (inp
, outp
, 0, same_file
);
10063 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10067 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10068 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
10069 output_line_directive (inp
, outp
, 0, same_file
);
10070 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10074 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
10075 output_line_directive (inp
, outp
, 0, same_file
);
10076 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10079 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
10080 monthnames
[timebuf
->tm_mon
],
10081 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
10082 output_line_directive (inp
, outp
, 0, same_file
);
10083 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10086 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
10087 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
10088 output_line_directive (inp
, outp
, 0, same_file
);
10089 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10094 sprintf (directive
, " __STDC__ 1");
10095 output_line_directive (inp
, outp
, 0, same_file
);
10096 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10101 sprintf (directive
, " __OBJC__ 1");
10102 output_line_directive (inp
, outp
, 0, same_file
);
10103 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10110 * process a given definition string, for initialization
10111 * If STR is just an identifier, define it with value 1.
10112 * If STR has anything after the identifier, then it should
10113 * be identifier=definition.
10117 make_definition (str
, op
)
10122 struct directive
*kt
;
10125 p
= buf
= (U_CHAR
*) str
;
10126 if (!is_idstart
[*p
]) {
10127 error ("malformed option `-D %s'", str
);
10130 while (is_idchar
[*++p
])
10133 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
10136 p
= (U_CHAR
*) str
; /* Error */
10139 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
10140 strcpy ((char *)buf
, str
);
10141 strcat ((char *)buf
, " 1");
10142 } else if (*p
!= '=') {
10143 error ("malformed option `-D %s'", str
);
10147 /* Copy the entire option so we can modify it. */
10148 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
10149 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
10150 /* Change the = to a space. */
10151 buf
[p
- (U_CHAR
*) str
] = ' ';
10152 /* Scan for any backslash-newline and remove it. */
10154 q
= &buf
[p
- (U_CHAR
*) str
];
10156 if (*p
== '\"' || *p
== '\'') {
10157 int unterminated
= 0;
10158 U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
10159 NULL_PTR
, NULL_PTR
, &unterminated
);
10163 if (*p
== '\\' && p
[1] == '\n')
10165 else if (*p
== '\n')
10174 } else if (*p
== '\\' && p
[1] == '\n')
10176 /* Change newline chars into newline-markers. */
10177 else if (*p
== '\n')
10189 ip
= &instack
[++indepth
];
10190 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10191 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10193 ip
->buf
= ip
->bufp
= buf
;
10194 ip
->length
= strlen ((char *) buf
);
10198 ip
->if_stack
= if_stack
;
10199 ip
->system_header_p
= 0;
10201 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
10204 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10205 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
10209 /* JF, this does the work for the -U option */
10212 make_undef (str
, op
)
10217 struct directive
*kt
;
10219 ip
= &instack
[++indepth
];
10220 ip
->nominal_fname
= ip
->fname
= "*undef*";
10221 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10223 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
10224 ip
->length
= strlen (str
);
10228 ip
->if_stack
= if_stack
;
10229 ip
->system_header_p
= 0;
10231 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
10234 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
10238 /* Process the string STR as if it appeared as the body of a #assert.
10239 OPTION is the option name for which STR was the argument. */
10242 make_assertion (option
, str
)
10247 struct directive
*kt
;
10248 U_CHAR
*buf
, *p
, *q
;
10250 /* Copy the entire option so we can modify it. */
10251 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
10252 strcpy ((char *) buf
, str
);
10253 /* Scan for any backslash-newline and remove it. */
10256 if (*p
== '\\' && p
[1] == '\n')
10264 if (!is_idstart
[*p
]) {
10265 error ("malformed option `%s %s'", option
, str
);
10268 while (is_idchar
[*++p
])
10270 SKIP_WHITE_SPACE (p
);
10271 if (! (*p
== 0 || *p
== '(')) {
10272 error ("malformed option `%s %s'", option
, str
);
10276 ip
= &instack
[++indepth
];
10277 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10278 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10280 ip
->buf
= ip
->bufp
= buf
;
10281 ip
->length
= strlen ((char *) buf
);
10285 ip
->if_stack
= if_stack
;
10286 ip
->system_header_p
= 0;
10288 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
10291 /* Pass NULL as output ptr to do_define since we KNOW it never does
10293 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
10297 #ifndef DIR_SEPARATOR
10298 #define DIR_SEPARATOR '/'
10301 /* The previous include prefix, if any, is PREV_FILE_NAME.
10302 Translate any pathnames with COMPONENT.
10303 Allocate a new include prefix whose name is the
10304 simplified concatenation of PREFIX and NAME,
10305 with a trailing / added if needed.
10306 But return 0 if the include prefix should be ignored,
10307 e.g. because it is a duplicate of PREV_FILE_NAME. */
10309 static struct file_name_list
*
10310 new_include_prefix (prev_file_name
, component
, prefix
, name
)
10311 struct file_name_list
*prev_file_name
;
10317 fatal ("Directory name missing after command line option");
10320 /* Ignore the empty string. */
10323 prefix
= update_path (prefix
, component
);
10324 name
= update_path (name
, component
);
10327 struct file_name_list
*dir
10328 = ((struct file_name_list
*)
10329 xmalloc (sizeof (struct file_name_list
)
10330 + strlen (prefix
) + strlen (name
) + 2));
10332 strcpy (dir
->fname
, prefix
);
10333 strcat (dir
->fname
, name
);
10334 len
= simplify_filename (dir
->fname
);
10336 /* Convert directory name to a prefix. */
10337 if (dir
->fname
[len
- 1] != DIR_SEPARATOR
) {
10338 if (len
== 1 && dir
->fname
[len
- 1] == '.')
10341 dir
->fname
[len
++] = DIR_SEPARATOR
;
10342 dir
->fname
[len
] = 0;
10345 /* Ignore a directory whose name matches the previous one. */
10346 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
10347 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10348 if (!first_bracket_include
)
10349 first_bracket_include
= prev_file_name
;
10355 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10357 /* Add a trailing "." if there is a filename. This increases the number
10358 of systems that can stat directories. We remove it below. */
10361 dir
->fname
[len
] = '.';
10362 dir
->fname
[len
+ 1] = 0;
10365 /* Ignore a nonexistent directory. */
10366 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
10367 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
10368 error_from_errno (dir
->fname
);
10374 dir
->fname
[len
] = 0;
10376 /* Ignore a directory whose identity matches the previous one. */
10378 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
10379 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
10380 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10381 if (!first_bracket_include
)
10382 first_bracket_include
= prev_file_name
;
10389 dir
->c_system_include_path
= 0;
10390 dir
->got_name_map
= 0;
10396 /* Append a chain of `struct file_name_list's
10397 to the end of the main include chain.
10398 FIRST is the beginning of the chain to append, and LAST is the end. */
10401 append_include_chain (first
, last
)
10402 struct file_name_list
*first
, *last
;
10404 struct file_name_list
*dir
;
10406 if (!first
|| !last
)
10412 last_include
->next
= first
;
10414 if (first_bracket_include
== 0)
10415 first_bracket_include
= first
;
10417 for (dir
= first
; ; dir
= dir
->next
) {
10418 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
10419 if (len
> max_include_len
)
10420 max_include_len
= len
;
10426 last_include
= last
;
10429 /* Place into DST a representation of the file named SRC that is suitable
10430 for `make'. Do not null-terminate DST. Return its length. */
10432 quote_string_for_make (dst
, src
)
10447 /* GNU make uses a weird quoting scheme for white space.
10448 A space or tab preceded by 2N+1 backslashes represents
10449 N backslashes followed by space; a space or tab
10450 preceded by 2N backslashes represents N backslashes at
10451 the end of a file name; and backslashes in other
10452 contexts should not be doubled. */
10454 for (q
= p
- 1; src
< q
&& q
[-1] == '\\'; q
--)
10466 goto ordinary_char
;
10472 /* Fall through. This can mishandle things like "$(" but
10473 there's no easy fix. */
10476 /* This can mishandle characters in the string "\0\n%*?[\\~";
10477 exactly which chars are mishandled depends on the `make' version.
10478 We know of no portable solution for this;
10479 even GNU make 3.76.1 doesn't solve the problem entirely.
10480 (Also, '\0' is mishandled due to our calling conventions.) */
10490 /* Add output to `deps_buffer' for the -M switch.
10491 STRING points to the text to be output.
10492 SPACER is ':' for targets, ' ' for dependencies. */
10495 deps_output (string
, spacer
)
10499 int size
= quote_string_for_make ((char *) 0, string
);
10504 #ifndef MAX_OUTPUT_COLUMNS
10505 #define MAX_OUTPUT_COLUMNS 72
10507 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
10508 && 1 < deps_column
) {
10509 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
10516 if (deps_size
+ size
+ 8 > deps_allocated_size
) {
10517 deps_allocated_size
= (deps_size
+ size
+ 50) * 2;
10518 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
10520 if (spacer
== ' ') {
10521 deps_buffer
[deps_size
++] = ' ';
10524 quote_string_for_make (&deps_buffer
[deps_size
], string
);
10526 deps_column
+= size
;
10527 if (spacer
== ':') {
10528 deps_buffer
[deps_size
++] = ':';
10531 deps_buffer
[deps_size
] = 0;
10535 fatal (PRINTF_ALIST (msgid
))
10540 fprintf (stderr
, "%s: ", progname
);
10541 VA_START (args
, msgid
);
10542 vnotice (msgid
, args
);
10544 fprintf (stderr
, "\n");
10545 exit (FATAL_EXIT_CODE
);
10548 /* More 'friendly' abort that prints the line and file.
10549 config.h can #define abort fancy_abort if you like that sort of thing. */
10554 fatal ("Internal gcc abort.");
10558 perror_with_name (name
)
10561 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, my_strerror (errno
));
10566 pfatal_with_name (name
)
10569 perror_with_name (name
);
10573 exit (FATAL_EXIT_CODE
);
10577 /* Handler for SIGPIPE. */
10580 pipe_closed (signo
)
10581 /* If this is missing, some compilers complain. */
10584 fatal ("output pipe has been closed");
10590 fatal ("Memory exhausted.");
10598 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (size
);
10605 xrealloc (old
, size
)
10609 register GENERIC_PTR ptr
= (GENERIC_PTR
) realloc (old
, size
);
10616 xcalloc (number
, size
)
10617 size_t number
, size
;
10619 register size_t total
= number
* size
;
10620 register GENERIC_PTR ptr
= (GENERIC_PTR
) malloc (total
);
10623 bzero (ptr
, total
);
10631 size_t size
= strlen (input
);
10632 char *output
= xmalloc (size
+ 1);
10633 strcpy (output
, input
);
10639 /* Under VMS we need to fix up the "include" specification filename so
10640 that everything following the 1st slash is changed into its correct
10641 VMS file specification. */
10644 hack_vms_include_specification (fname
, vaxc_include
)
10648 register char *cp
, *cp1
, *cp2
;
10649 int f
, check_filename_before_returning
;
10652 check_filename_before_returning
= 0;
10654 cp
= base_name (fname
);
10657 * Check if we have a vax-c style '#include filename'
10658 * and add the missing .h
10660 if (vaxc_include
&& !index (cp
,'.'))
10663 cp2
= Local
; /* initialize */
10665 /* We are trying to do a number of things here. First of all, we are
10666 trying to hammer the filenames into a standard format, such that later
10667 processing can handle them.
10669 If the file name contains something like [dir.], then it recognizes this
10670 as a root, and strips the ".]". Later processing will add whatever is
10671 needed to get things working properly.
10673 If no device is specified, then the first directory name is taken to be
10674 a device name (or a rooted logical). */
10676 /* See if we found that 1st slash */
10677 if (cp
== 0) return; /* Nothing to do!!! */
10678 if (*cp
!= '/') return; /* Nothing to do!!! */
10679 /* Point to the UNIX filename part (which needs to be fixed!) */
10681 /* If the directory spec is not rooted, we can just copy
10682 the UNIX filename part and we are done */
10683 if (((cp
- fname
) > 1) && ((cp
[-1] == ']') || (cp
[-1] == '>'))) {
10684 if (cp
[-2] != '.') {
10686 * The VMS part ends in a `]', and the preceding character is not a `.'.
10687 * We strip the `]', and then splice the two parts of the name in the
10688 * usual way. Given the default locations for include files in cccp.c,
10689 * we will only use this code if the user specifies alternate locations
10690 * with the /include (-I) switch on the command line. */
10691 cp
-= 1; /* Strip "]" */
10692 cp1
--; /* backspace */
10695 * The VMS part has a ".]" at the end, and this will not do. Later
10696 * processing will add a second directory spec, and this would be a syntax
10697 * error. Thus we strip the ".]", and thus merge the directory specs.
10698 * We also backspace cp1, so that it points to a '/'. This inhibits the
10699 * generation of the 000000 root directory spec (which does not belong here
10702 cp
-= 2; /* Strip ".]" */
10703 cp1
--; }; /* backspace */
10706 /* We drop in here if there is no VMS style directory specification yet.
10707 * If there is no device specification either, we make the first dir a
10708 * device and try that. If we do not do this, then we will be essentially
10709 * searching the users default directory (as if they did a #include "asdf.h").
10711 * Then all we need to do is to push a '[' into the output string. Later
10712 * processing will fill this in, and close the bracket.
10714 if (cp
[-1] != ':') *cp2
++ = ':'; /* dev not in spec. take first dir */
10715 *cp2
++ = '['; /* Open the directory specification */
10718 /* at this point we assume that we have the device spec, and (at least
10719 the opening "[" for a directory specification. We may have directories
10720 specified already */
10722 /* If there are no other slashes then the filename will be
10723 in the "root" directory. Otherwise, we need to add
10724 directory specifications. */
10725 if (index (cp1
, '/') == 0) {
10726 /* Just add "000000]" as the directory string */
10727 strcpy (cp2
, "000000]");
10728 cp2
+= strlen (cp2
);
10729 check_filename_before_returning
= 1; /* we might need to fool with this later */
10731 /* As long as there are still subdirectories to add, do them. */
10732 while (index (cp1
, '/') != 0) {
10733 /* If this token is "." we can ignore it */
10734 if ((cp1
[0] == '.') && (cp1
[1] == '/')) {
10738 /* Add a subdirectory spec. Do not duplicate "." */
10739 if (cp2
[-1] != '.' && cp2
[-1] != '[' && cp2
[-1] != '<')
10741 /* If this is ".." then the spec becomes "-" */
10742 if ((cp1
[0] == '.') && (cp1
[1] == '.') && (cp
[2] == '/')) {
10743 /* Add "-" and skip the ".." */
10748 /* Copy the subdirectory */
10749 while (*cp1
!= '/') *cp2
++= *cp1
++;
10750 cp1
++; /* Skip the "/" */
10752 /* Close the directory specification */
10753 if (cp2
[-1] == '.') /* no trailing periods */
10757 /* Now add the filename */
10758 while (*cp1
) *cp2
++ = *cp1
++;
10760 /* Now append it to the original VMS spec. */
10761 strcpy (cp
, Local
);
10763 /* If we put a [000000] in the filename, try to open it first. If this fails,
10764 remove the [000000], and return that name. This provides flexibility
10765 to the user in that they can use both rooted and non-rooted logical names
10766 to point to the location of the file. */
10768 if (check_filename_before_returning
) {
10769 f
= open (fname
, O_RDONLY
, 0666);
10771 /* The file name is OK as it is, so return it as is. */
10775 /* The filename did not work. Try to remove the [000000] from the name,
10777 cp
= index (fname
, '[');
10778 cp2
= index (fname
, ']') + 1;
10779 strcpy (cp
, cp2
); /* this gets rid of it */
10787 /* The following wrapper functions supply additional arguments to the VMS
10788 I/O routines to optimize performance with file handling. The arguments
10790 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
10791 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
10792 "fop=tef"- Truncate unused portions of file when closing file.
10793 "shr=nil"- Disallow file sharing while file is open. */
10796 VMS_freopen (fname
, type
, oldfile
)
10801 #undef freopen /* Get back the real freopen routine. */
10802 if (strcmp (type
, "w") == 0)
10803 return freopen (fname
, type
, oldfile
,
10804 "mbc=16", "deq=64", "fop=tef", "shr=nil");
10805 return freopen (fname
, type
, oldfile
, "mbc=16");
10809 VMS_fopen (fname
, type
)
10813 #undef fopen /* Get back the real fopen routine. */
10814 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
10815 fixed arguments, which matches ANSI's specification but not VAXCRTL's
10816 pre-ANSI implementation. This hack circumvents the mismatch problem. */
10817 FILE *(*vmslib_fopen
)() = (FILE *(*)()) fopen
;
10820 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
10821 "deq=64", "fop=tef", "shr=nil");
10823 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
10827 VMS_open (fname
, flags
, prot
)
10832 #undef open /* Get back the real open routine. */
10833 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
10836 /* more VMS hackery */
10840 extern unsigned long sys$
parse(), sys$
search();
10842 /* Work around another library bug. If a file is located via a searchlist,
10843 and if the device it's on is not the same device as the one specified
10844 in the first element of that searchlist, then both stat() and fstat()
10845 will fail to return info about it. `errno' will be set to EVMSERR, and
10846 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
10847 We can get around this by fully parsing the filename and then passing
10848 that absolute name to stat().
10850 Without this fix, we can end up failing to find header files, which is
10851 bad enough, but then compounding the problem by reporting the reason for
10852 failure as "normal successful completion." */
10854 #undef fstat /* Get back to the library version. */
10857 VMS_fstat (fd
, statbuf
)
10859 struct stat
*statbuf
;
10861 int result
= fstat (fd
, statbuf
);
10866 char nambuf
[NAM$C_MAXRSS
+1];
10868 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
10869 result
= VMS_stat (nambuf
, statbuf
);
10870 /* No fclose(fp) here; that would close(fd) as well. */
10877 VMS_stat (name
, statbuf
)
10879 struct stat
*statbuf
;
10881 int result
= stat (name
, statbuf
);
10887 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for sys$parse */
10888 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for sys$search */
10891 fab
.fab$l_fna
= (char *) name
;
10892 fab
.fab$b_fns
= (unsigned char) strlen (name
);
10893 fab
.fab$l_nam
= (void *) &nam
;
10895 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
10896 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
10897 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
10898 if (sys$
parse (&fab
) & 1)
10900 if (sys$
search (&fab
) & 1)
10902 res_nam
[nam
.nam$b_rsl
] = '\0';
10903 result
= stat (res_nam
, statbuf
);
10905 /* Clean up searchlist context cached by the system. */
10906 nam
.nam$b_nop
= NAM$M_SYNCHK
;
10907 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
10908 (void) sys$
parse (&fab
);
10916 VMS_fwrite (ptr
, size
, nitems
, stream
)
10922 /* VMS fwrite has undesirable results
10923 if STREAM happens to be a record oriented file.
10924 Work around this problem by writing each character individually. */
10925 char const *p
= ptr
;
10926 size_t bytes
= size
* nitems
;
10927 char *lim
= p
+ bytes
;
10930 if (putc (*p
++, stream
) == EOF
)