1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding! */
24 typedef unsigned char U_CHAR
;
28 #include "../src/config.h"
36 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
37 for the sake of machines with limited C compilers. */
40 #endif /* not EMACS */
42 #ifndef STANDARD_INCLUDE_DIR
43 #define STANDARD_INCLUDE_DIR "/usr/include"
46 #ifndef LOCAL_INCLUDE_DIR
47 #define LOCAL_INCLUDE_DIR "/usr/local/include"
50 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
52 #define PTR_INT_TYPE ptrdiff_t
54 #define PTR_INT_TYPE long
64 /* By default, colon separates directories in a path. */
65 #ifndef PATH_SEPARATOR
66 #define PATH_SEPARATOR ':'
69 /* In case config.h defines these. */
74 #include <sys/types.h>
81 #include <sys/time.h> /* for __DATE__ and __TIME__ */
82 #include <sys/resource.h>
89 /* VMS-specific definitions */
92 #include <errno.h> /* This defines "errno" properly */
93 #include <perror.h> /* This defines sys_errlist/sys_nerr properly */
95 #define O_RDONLY 0 /* Open arg for Read/Only */
96 #define O_WRONLY 1 /* Open arg for Write/Only */
97 #define read(fd,buf,size) VMS_read(fd,buf,size)
98 #define write(fd,buf,size) VMS_write(fd,buf,size)
99 #define open(fname,mode,prot) VMS_open(fname,mode,prot)
100 #define fopen(fname,mode) VMS_fopen(fname,mode)
101 #define freopen(fname,mode,ofile) VMS_freopen(fname,mode,ofile)
102 #define strncat(dst,src,cnt) VMS_strncat(dst,src,cnt)
103 static char * VMS_strncat ();
104 static int VMS_read ();
105 static int VMS_write ();
106 static int VMS_open ();
107 static FILE * VMS_fopen ();
108 static FILE * VMS_freopen ();
109 static void hack_vms_include_specification ();
110 typedef struct { unsigned :16, :16, :16; } vms_ino_t
;
111 #define ino_t vms_ino_t
112 #define INCLUDE_LEN_FUDGE 10 /* leave room for VMS syntax conversion */
114 #define BSTRING /* VMS/GCC supplies the bstring routines */
115 #endif /* __GNUC__ */
118 extern char *index ();
119 extern char *rindex ();
127 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
128 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
130 /* Find the largest host integer type and set its size and type. */
132 #ifndef HOST_BITS_PER_WIDE_INT
134 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
135 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
136 #define HOST_WIDE_INT long
138 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
139 #define HOST_WIDE_INT int
145 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
148 /* Define a generic NULL if one hasn't already been defined. */
155 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
156 #define GENERIC_PTR void *
158 #define GENERIC_PTR char *
163 #define NULL_PTR ((GENERIC_PTR)0)
166 #ifndef INCLUDE_LEN_FUDGE
167 #define INCLUDE_LEN_FUDGE 0
170 /* Forward declarations. */
176 /* External declarations. */
178 extern char *getenv ();
179 extern FILE *fdopen ();
180 extern char *version_string
;
181 extern struct tm
*localtime ();
183 extern char *sys_errlist
[];
189 /* Forward declarations. */
196 #if defined(USG) || defined(VMS)
204 /* These functions are declared to return int instead of void since they
205 are going to be placed in a table and some old compilers have trouble with
206 pointers to functions returning void. */
208 static int do_define ();
209 static int do_line ();
210 static int do_include ();
211 static int do_undef ();
212 static int do_error ();
213 static int do_pragma ();
214 static int do_ident ();
216 static int do_xifdef ();
217 static int do_else ();
218 static int do_elif ();
219 static int do_endif ();
220 static int do_sccs ();
221 static int do_once ();
222 static int do_assert ();
223 static int do_unassert ();
224 static int do_warning ();
226 static void add_import ();
227 static void append_include_chain ();
228 static void deps_output ();
229 static void make_undef ();
230 static void make_definition ();
231 static void make_assertion ();
232 static void path_include ();
233 static void initialize_builtins ();
234 static void initialize_char_syntax ();
235 static void dump_arg_n ();
236 static void dump_defn_1 ();
237 static void delete_macro ();
238 static void trigraph_pcp ();
239 static void rescan ();
240 static void finclude ();
241 static void validate_else ();
242 static int comp_def_part ();
243 static void error_from_errno ();
244 static void error_with_line ();
246 static void pedwarn_with_file_and_line ();
247 static void fatal ();
249 static void pfatal_with_name ();
250 static void perror_with_name ();
251 static void print_containing_files ();
252 static int lookup_import ();
253 static int redundant_include_p ();
254 static is_system_include ();
255 static int check_preconditions ();
256 static void pcfinclude ();
257 static void pcstring_used ();
258 static void write_output ();
259 static int check_macro_name ();
260 static int compare_defs ();
261 static int compare_token_lists ();
262 static int eval_if_expression ();
263 static int discard_comments ();
264 static int delete_newlines ();
265 static int line_for_error ();
267 static int file_size_and_mode ();
269 static struct arglist
*read_token_list ();
270 static void free_token_list ();
272 static struct hashnode
*install ();
273 struct hashnode
*lookup ();
275 static struct assertion_hashnode
*assertion_install ();
276 static struct assertion_hashnode
*assertion_lookup ();
278 static char *xrealloc ();
279 static char *xcalloc ();
280 static char *savestring ();
282 static void delete_assertion ();
283 static void macroexpand ();
284 static void dump_all_macros ();
285 static void conditional_skip ();
286 static void skip_if_group ();
287 static void output_line_command ();
289 /* Last arg to output_line_command. */
290 enum file_change_code
{same_file
, enter_file
, leave_file
};
292 static int grow_outbuf ();
293 static int handle_directive ();
294 static void memory_full ();
296 static U_CHAR
*macarg1 ();
297 static char *macarg ();
299 static U_CHAR
*skip_to_end_of_comment ();
300 static U_CHAR
*skip_quoted_string ();
301 static U_CHAR
*skip_paren_group ();
303 static char *check_precompiled ();
304 /* static struct macrodef create_definition (); [moved below] */
305 static void dump_single_macro ();
307 #ifndef FAILURE_EXIT_CODE
308 #define FAILURE_EXIT_CODE 33 /* gnu cc command understands this */
311 #ifndef SUCCESS_EXIT_CODE
312 #define SUCCESS_EXIT_CODE 0 /* 0 means success on Unix. */
315 /* Name under which this program was invoked. */
317 static char *progname
;
319 /* Nonzero means use extra default include directories for C++. */
321 static int cplusplus
;
323 /* Nonzero means handle cplusplus style comments */
325 static int cplusplus_comments
;
327 /* Nonzero means handle #import, for objective C. */
331 /* Nonzero means this is an assembly file, and allow
332 unknown directives, which could be comments. */
336 /* Current maximum length of directory names in the search path
337 for include files. (Altered as we get more of them.) */
339 static int max_include_len
;
341 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
345 /* Nonzero means copy comments into the output file. */
347 static int put_out_comments
= 0;
349 /* Nonzero means don't process the ANSI trigraph sequences. */
351 static int no_trigraphs
= 0;
353 /* Nonzero means print the names of included files rather than
354 the preprocessed output. 1 means just the #include "...",
355 2 means #include <...> as well. */
357 static int print_deps
= 0;
359 /* Nonzero means print names of header files (-H). */
361 static int print_include_names
= 0;
363 /* Nonzero means don't output line number information. */
365 static int no_line_commands
;
367 /* dump_only means inhibit output of the preprocessed text
368 and instead output the definitions of all user-defined
369 macros in a form suitable for use as input to cccp.
370 dump_names means pass #define and the macro name through to output.
371 dump_definitions means pass the whole definition (plus #define) through
374 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
375 dump_macros
= dump_none
;
377 /* Nonzero means pass all #define and #undef directives which we actually
378 process through to the output stream. This feature is used primarily
379 to allow cc1 to record the #defines and #undefs for the sake of
380 debuggers which understand about preprocessor macros, but it may
381 also be useful with -E to figure out how symbols are defined, and
382 where they are defined. */
383 static int debug_output
= 0;
385 /* Nonzero indicates special processing used by the pcp program. The
386 special effects of this mode are:
388 Inhibit all macro expansion, except those inside #if directives.
390 Process #define directives normally, and output their contents
393 Output preconditions to pcp_outfile indicating all the relevant
394 preconditions for use of this file in a later cpp run.
396 static FILE *pcp_outfile
;
398 /* Nonzero means we are inside an IF during a -pcp run. In this mode
399 macro expansion is done, and preconditions are output for all macro
400 uses requiring them. */
401 static int pcp_inside_if
;
403 /* Nonzero means never to include precompiled files. */
404 static int no_precomp
;
406 /* Nonzero means give all the error messages the ANSI standard requires. */
410 /* Nonzero means try to make failure to fit ANSI C an error. */
412 static int pedantic_errors
;
414 /* Nonzero means don't print warning messages. -w. */
416 static int inhibit_warnings
= 0;
418 /* Nonzero means warn if slash-star appears in a comment. */
420 static int warn_comments
;
422 /* Nonzero means warn if a macro argument is (or would be)
423 stringified with -traditional. */
425 static int warn_stringify
;
427 /* Nonzero means warn if there are any trigraphs. */
429 static int warn_trigraphs
;
431 /* Nonzero means warn if #import is used. */
433 static int warn_import
= 1;
435 /* Nonzero means turn warnings into errors. */
437 static int warnings_are_errors
;
439 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
443 /* Nonzero causes output not to be done,
444 but directives such as #define that have side effects
447 static int no_output
;
449 /* Nonzero means that we have finished processing the command line options.
450 This flag is used to decide whether or not to issue certain errors
453 static int done_initializing
= 0;
455 /* I/O buffer structure.
456 The `fname' field is nonzero for source files and #include files
457 and for the dummy text used for -D and -U.
458 It is zero for rescanning results of macro expansion
459 and for expanding macro arguments. */
460 #define INPUT_STACK_MAX 200
461 static struct file_buf
{
463 /* Filename specified with #line command. */
465 /* Record where in the search path this file was found.
466 For #include_next. */
467 struct file_name_list
*dir
;
472 /* Macro that this level is the expansion of.
473 Included so that we can reenable the macro
474 at the end of this level. */
475 struct hashnode
*macro
;
476 /* Value of if_stack at start of this file.
477 Used to prohibit unmatched #endif (etc) in an include file. */
478 struct if_stack
*if_stack
;
479 /* Object to be freed at end of input at this level. */
481 /* True if this is a header file included using <FILENAME>. */
482 char system_header_p
;
483 } instack
[INPUT_STACK_MAX
];
485 static int last_error_tick
; /* Incremented each time we print it. */
486 static int input_file_stack_tick
; /* Incremented when the status changes. */
488 /* Current nesting level of input sources.
489 `instack[indepth]' is the level currently being read. */
490 static int indepth
= -1;
491 #define CHECK_DEPTH(code) \
492 if (indepth >= (INPUT_STACK_MAX - 1)) \
494 error_with_line (line_for_error (instack[indepth].lineno), \
495 "macro or `#include' recursion too deep"); \
499 /* Current depth in #include directives that use <...>. */
500 static int system_include_depth
= 0;
502 typedef struct file_buf FILE_BUF
;
504 /* The output buffer. Its LENGTH field is the amount of room allocated
505 for the buffer, not the number of chars actually present. To get
506 that, subtract outbuf.buf from outbuf.bufp. */
508 #define OUTBUF_SIZE 10 /* initial size of output buffer */
509 static FILE_BUF outbuf
;
511 /* Grow output buffer OBUF points at
512 so it can hold at least NEEDED more chars. */
514 #define check_expand(OBUF, NEEDED) \
515 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
516 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
518 struct file_name_list
520 struct file_name_list
*next
;
522 /* If the following is nonzero, it is a macro name.
523 Don't include the file again if that macro is defined. */
524 U_CHAR
*control_macro
;
527 /* #include "file" looks in source file dir, then stack. */
528 /* #include <file> just looks in the stack. */
529 /* -I directories are added to the end, then the defaults are added. */
530 static struct default_include
{ char *fname
; int cplusplus
; } include_defaults_array
[]
531 #ifdef INCLUDE_DEFAULTS
535 /* Pick up GNU C++ specific include files. */
536 { GPLUSPLUS_INCLUDE_DIR
, 1},
537 { GCC_INCLUDE_DIR
, 0},
539 /* For cross-compilation, this dir name is generated
540 automatically in Makefile.in. */
541 { CROSS_INCLUDE_DIR
, 0 },
542 #else /* not CROSS_COMPILE */
543 { LOCAL_INCLUDE_DIR
, 0},
544 /* Some systems have an extra dir of include files. */
545 #ifdef SYSTEM_INCLUDE_DIR
546 { SYSTEM_INCLUDE_DIR
, 0},
548 { STANDARD_INCLUDE_DIR
, 0},
549 #endif /* not CROSS_COMPILE */
552 #endif /* no INCLUDE_DEFAULTS */
554 /* The code looks at the defaults through this pointer, rather than through
555 the constant structure above. This pointer gets changed if an environment
556 variable specifies other defaults. */
557 static struct default_include
*include_defaults
= include_defaults_array
;
559 static struct file_name_list
*include
= 0; /* First dir to search */
560 /* First dir to search for <file> */
561 /* This is the first element to use for #include <...>.
562 If it is 0, use the entire chain for such includes. */
563 static struct file_name_list
*first_bracket_include
= 0;
564 /* This is the first element in the chain that corresponds to
565 a directory of system header files. */
566 static struct file_name_list
*first_system_include
= 0;
567 static struct file_name_list
*last_include
= 0; /* Last in chain */
569 /* Chain of include directories to put at the end of the other chain. */
570 static struct file_name_list
*after_include
= 0;
571 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
573 /* List of included files that contained #pragma once. */
574 static struct file_name_list
*dont_repeat_files
= 0;
576 /* List of other included files.
577 If ->control_macro if nonzero, the file had a #ifndef
578 around the entire contents, and ->control_macro gives the macro name. */
579 static struct file_name_list
*all_include_files
= 0;
581 /* Directory prefix that should replace `/usr' in the standard
582 include file directories. */
583 static char *include_prefix
;
585 /* Global list of strings read in from precompiled files. This list
586 is kept in the order the strings are read in, with new strings being
587 added at the end through stringlist_tailp. We use this list to output
588 the strings at the end of the run.
590 static STRINGDEF
*stringlist
;
591 static STRINGDEF
**stringlist_tailp
= &stringlist
;
594 /* Structure returned by create_definition */
595 typedef struct macrodef MACRODEF
;
598 struct definition
*defn
;
603 static struct macrodef
create_definition ();
606 /* Structure allocated for every #define. For a simple replacement
609 nargs = -1, the `pattern' list is null, and the expansion is just
610 the replacement text. Nargs = 0 means a functionlike macro with no args,
612 #define getchar() getc (stdin) .
613 When there are args, the expansion is the replacement text with the
614 args squashed out, and the reflist is a list describing how to
615 build the output from the input: e.g., "3 chars, then the 1st arg,
616 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
617 The chars here come from the expansion. Whatever is left of the
618 expansion after the last arg-occurrence is copied after that arg.
619 Note that the reflist can be arbitrarily long---
620 its length depends on the number of times the arguments appear in
621 the replacement text, not how many args there are. Example:
622 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
624 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
625 where (x, y) means (nchars, argno). */
627 typedef struct definition DEFINITION
;
630 int length
; /* length of expansion string */
631 int predefined
; /* True if the macro was builtin or */
632 /* came from the command line */
634 int line
; /* Line number of definition */
635 char *file
; /* File of definition */
636 char rest_args
; /* Nonzero if last arg. absorbs the rest */
638 struct reflist
*next
;
639 char stringify
; /* nonzero if this arg was preceded by a
641 char raw_before
; /* Nonzero if a ## operator before arg. */
642 char raw_after
; /* Nonzero if a ## operator after arg. */
643 char rest_args
; /* Nonzero if this arg. absorbs the rest */
644 int nchars
; /* Number of literal chars to copy before
645 this arg occurrence. */
646 int argno
; /* Number of arg to substitute (origin-0) */
649 /* Names of macro args, concatenated in reverse order
650 with comma-space between them.
651 The only use of this is that we warn on redefinition
652 if this differs between the old and new definitions. */
657 /* different kinds of things that can appear in the value field
658 of a hash node. Actually, this may be useless now. */
667 * special extension string that can be added to the last macro argument to
668 * allow it to absorb the "rest" of the arguments when expanded. Ex:
669 * #define wow(a, b...) process(b, a, b)
670 * { wow(1, 2, 3); } -> { process( 2, 3, 1, 2, 3); }
671 * { wow(one, two); } -> { process( two, one, two); }
672 * if this "rest_arg" is used with the concat token '##' and if it is not
673 * supplied then the token attached to with ## will not be outputted. Ex:
674 * #define wow(a, b...) process(b ## , a, ## b)
675 * { wow(1, 2); } -> { process( 2, 1,2); }
676 * { wow(one); } -> { process( one); {
678 static char rest_extension
[] = "...";
679 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
681 /* The structure of a node in the hash table. The hash table
682 has entries for all tokens defined by #define commands (type T_MACRO),
683 plus some special tokens like __LINE__ (these each have their own
684 type, and the appropriate code is run when that type of node is seen.
685 It does not contain control words like "#define", which are recognized
686 by a separate piece of code. */
688 /* different flavors of hash nodes --- also used in keyword table */
690 T_DEFINE
= 1, /* the `#define' keyword */
691 T_INCLUDE
, /* the `#include' keyword */
692 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
693 T_IMPORT
, /* the `#import' keyword */
694 T_IFDEF
, /* the `#ifdef' keyword */
695 T_IFNDEF
, /* the `#ifndef' keyword */
696 T_IF
, /* the `#if' keyword */
697 T_ELSE
, /* `#else' */
698 T_PRAGMA
, /* `#pragma' */
699 T_ELIF
, /* `#elif' */
700 T_UNDEF
, /* `#undef' */
701 T_LINE
, /* `#line' */
702 T_ERROR
, /* `#error' */
703 T_WARNING
, /* `#warning' */
704 T_ENDIF
, /* `#endif' */
705 T_SCCS
, /* `#sccs', used on system V. */
706 T_IDENT
, /* `#ident', used on system V. */
707 T_ASSERT
, /* `#assert', taken from system V. */
708 T_UNASSERT
, /* `#unassert', taken from system V. */
709 T_SPECLINE
, /* special symbol `__LINE__' */
710 T_DATE
, /* `__DATE__' */
711 T_FILE
, /* `__FILE__' */
712 T_BASE_FILE
, /* `__BASE_FILE__' */
713 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
714 T_VERSION
, /* `__VERSION__' */
715 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
716 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
717 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
718 T_TIME
, /* `__TIME__' */
719 T_CONST
, /* Constant value, used by `__STDC__' */
720 T_MACRO
, /* macro defined by `#define' */
721 T_DISABLED
, /* macro temporarily turned off for rescan */
722 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
723 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
724 T_UNUSED
/* Used for something not defined. */
728 struct hashnode
*next
; /* double links for easy deletion */
729 struct hashnode
*prev
;
730 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
731 chain is kept, in case the node is the head
732 of the chain and gets deleted. */
733 enum node_type type
; /* type of special token */
734 int length
; /* length of token, for quick comparison */
735 U_CHAR
*name
; /* the actual name */
736 union hashval value
; /* pointer to expansion, or whatever */
739 typedef struct hashnode HASHNODE
;
741 /* Some definitions for the hash table. The hash function MUST be
742 computed as shown in hashf () below. That is because the rescan
743 loop computes the hash value `on the fly' for most tokens,
744 in order to avoid the overhead of a lot of procedure calls to
745 the hashf () function. Hashf () only exists for the sake of
746 politeness, for use when speed isn't so important. */
748 #define HASHSIZE 1403
749 static HASHNODE
*hashtab
[HASHSIZE
];
750 #define HASHSTEP(old, c) ((old << 2) + c)
751 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
753 /* Symbols to predefine. */
755 #ifdef CPP_PREDEFINES
756 static char *predefs
= CPP_PREDEFINES
;
758 static char *predefs
= "";
761 /* We let tm.h override the types used here, to handle trivial differences
762 such as the choice of unsigned int or long unsigned int for size_t.
763 When machines start needing nontrivial differences in the size type,
764 it would be best to do something here to figure out automatically
765 from other information what type to use. */
767 /* The string value for __size_type__. */
770 #define SIZE_TYPE "long unsigned int"
773 /* The string value for __ptrdiff_type__. */
776 #define PTRDIFF_TYPE "long int"
779 /* The string value for __wchar_type__. */
782 #define WCHAR_TYPE "int"
785 /* In the definition of a #assert name, this structure forms
786 a list of the individual values asserted.
787 Each value is itself a list of "tokens".
788 These are strings that are compared by name. */
790 struct tokenlist_list
{
791 struct tokenlist_list
*next
;
792 struct arglist
*tokens
;
795 struct assertion_hashnode
{
796 struct assertion_hashnode
*next
; /* double links for easy deletion */
797 struct assertion_hashnode
*prev
;
798 /* also, a back pointer to this node's hash
799 chain is kept, in case the node is the head
800 of the chain and gets deleted. */
801 struct assertion_hashnode
**bucket_hdr
;
802 int length
; /* length of token, for quick comparison */
803 U_CHAR
*name
; /* the actual name */
804 /* List of token-sequences. */
805 struct tokenlist_list
*value
;
808 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
810 /* Some definitions for the hash table. The hash function MUST be
811 computed as shown in hashf below. That is because the rescan
812 loop computes the hash value `on the fly' for most tokens,
813 in order to avoid the overhead of a lot of procedure calls to
814 the hashf function. hashf only exists for the sake of
815 politeness, for use when speed isn't so important. */
817 #define ASSERTION_HASHSIZE 37
818 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
820 /* Nonzero means inhibit macroexpansion of what seem to be
821 assertion tests, in rescan. For #if. */
822 static int assertions_flag
;
824 /* `struct directive' defines one #-directive, including how to handle it. */
827 int length
; /* Length of name */
828 int (*func
)(); /* Function to handle directive */
829 char *name
; /* Name of directive */
830 enum node_type type
; /* Code which describes which directive. */
831 char angle_brackets
; /* Nonzero => <...> is special. */
832 char traditional_comments
; /* Nonzero: keep comments if -traditional. */
833 char pass_thru
; /* Copy preprocessed directive to output file. */
836 /* Here is the actual list of #-directives, most-often-used first. */
838 static struct directive directive_table
[] = {
839 { 6, do_define
, "define", T_DEFINE
, 0, 1},
840 { 2, do_if
, "if", T_IF
},
841 { 5, do_xifdef
, "ifdef", T_IFDEF
},
842 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
843 { 5, do_endif
, "endif", T_ENDIF
},
844 { 4, do_else
, "else", T_ELSE
},
845 { 4, do_elif
, "elif", T_ELIF
},
846 { 4, do_line
, "line", T_LINE
},
847 { 7, do_include
, "include", T_INCLUDE
, 1},
848 { 12, do_include
, "include_next", T_INCLUDE_NEXT
, 1},
849 { 6, do_include
, "import", T_IMPORT
, 1},
850 { 5, do_undef
, "undef", T_UNDEF
},
851 { 5, do_error
, "error", T_ERROR
},
852 { 7, do_warning
, "warning", T_WARNING
},
853 #ifdef SCCS_DIRECTIVE
854 { 4, do_sccs
, "sccs", T_SCCS
},
856 { 6, do_pragma
, "pragma", T_PRAGMA
, 0, 0, 1},
857 { 5, do_ident
, "ident", T_IDENT
, 0, 0, 1},
858 { 6, do_assert
, "assert", T_ASSERT
},
859 { 8, do_unassert
, "unassert", T_UNASSERT
},
860 { -1, 0, "", T_UNUSED
},
863 /* When a directive handler is called,
864 this points to the # that started the directive. */
865 U_CHAR
*directive_start
;
867 /* table to tell if char can be part of a C identifier. */
868 U_CHAR is_idchar
[256];
869 /* table to tell if char can be first char of a c identifier. */
870 U_CHAR is_idstart
[256];
871 /* table to tell if c is horizontal space. */
872 U_CHAR is_hor_space
[256];
873 /* table to tell if c is horizontal or vertical space. */
874 static U_CHAR is_space
[256];
876 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
877 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
879 static int errors
= 0; /* Error counter for exit code */
881 /* Zero means dollar signs are punctuation.
882 -$ stores 0; -traditional may store 1. Default is 1 for VMS, 0 otherwise.
883 This must be 0 for correct processing of this ANSI C program:
885 #define lose(b) foo(b)
888 static int dollars_in_ident
;
889 #ifndef DOLLARS_IN_IDENTIFIERS
890 #define DOLLARS_IN_IDENTIFIERS 1
893 static FILE_BUF
expand_to_temp_buffer ();
895 static DEFINITION
*collect_expansion ();
897 /* Stack of conditionals currently in progress
898 (including both successful and failing conditionals). */
901 struct if_stack
*next
; /* for chaining to the next stack frame */
902 char *fname
; /* copied from input when frame is made */
903 int lineno
; /* similarly */
904 int if_succeeded
; /* true if a leg of this if-group
905 has been passed through rescan */
906 U_CHAR
*control_macro
; /* For #ifndef at start of file,
907 this is the macro name tested. */
908 enum node_type type
; /* type of last directive seen in this group */
910 typedef struct if_stack IF_STACK_FRAME
;
911 static IF_STACK_FRAME
*if_stack
= NULL
;
913 /* Buffer of -M output. */
914 static char *deps_buffer
;
916 /* Number of bytes allocated in above. */
917 static int deps_allocated_size
;
919 /* Number of bytes used. */
920 static int deps_size
;
922 /* Number of bytes since the last newline. */
923 static int deps_column
;
925 /* Nonzero means -I- has been seen,
926 so don't look for #include "foo" the source-file directory. */
927 static int ignore_srcdir
;
936 char *in_fname
, *out_fname
;
940 char **pend_files
= (char **) xmalloc (argc
* sizeof (char *));
941 char **pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
942 char **pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
943 char **pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
944 char **pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
946 /* Record the option used with each element of pend_assertions.
947 This is preparation for supporting more than one option for making
949 char **pend_assertion_options
= (char **) xmalloc (argc
* sizeof (char *));
950 int inhibit_predefs
= 0;
951 int no_standard_includes
= 0;
952 int no_standard_cplusplus_includes
= 0;
953 int missing_newline
= 0;
955 /* Non-0 means don't output the preprocessed program. */
956 int inhibit_output
= 0;
958 /* File name which deps are being written to.
959 This is 0 if deps are being written to stdout. */
961 /* Stream on which to print the dependency information. */
962 FILE *deps_stream
= 0;
963 /* Target-name to write with the dependency information. */
964 char *deps_target
= 0;
967 /* Get rid of any avoidable limit on stack size. */
971 /* Set the stack limit huge so that alloca (particularly stringtab
972 * in dbxread.c) does not fail. */
973 getrlimit (RLIMIT_STACK
, &rlim
);
974 rlim
.rlim_cur
= rlim
.rlim_max
;
975 setrlimit (RLIMIT_STACK
, &rlim
);
977 #endif /* RLIMIT_STACK defined */
982 /* Remove directories from PROGNAME. */
985 progname
= savestring (argv
[0]);
987 if (!(s
= rindex (progname
, ']')))
988 s
= rindex (progname
, ':');
990 strcpy (progname
, s
+1);
991 if (s
= rindex (progname
, '.'))
999 /* Initialize is_idchar to allow $. */
1000 dollars_in_ident
= 1;
1001 initialize_char_syntax ();
1002 dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
> 0;
1004 no_line_commands
= 0;
1006 dump_macros
= dump_none
;
1009 cplusplus_comments
= 0;
1011 bzero (pend_files
, argc
* sizeof (char *));
1012 bzero (pend_defs
, argc
* sizeof (char *));
1013 bzero (pend_undefs
, argc
* sizeof (char *));
1014 bzero (pend_assertions
, argc
* sizeof (char *));
1015 bzero (pend_includes
, argc
* sizeof (char *));
1017 /* Process switches and find input file name. */
1019 for (i
= 1; i
< argc
; i
++) {
1020 if (argv
[i
][0] != '-') {
1021 if (out_fname
!= NULL
)
1022 fatal ("Usage: %s [switches] input output", argv
[0]);
1023 else if (in_fname
!= NULL
)
1024 out_fname
= argv
[i
];
1028 switch (argv
[i
][1]) {
1031 if (!strcmp (argv
[i
], "-include")) {
1033 fatal ("Filename missing after -include option");
1035 pend_includes
[i
] = argv
[i
+1], i
++;
1037 if (!strcmp (argv
[i
], "-imacros")) {
1039 fatal ("Filename missing after -imacros option");
1041 pend_files
[i
] = argv
[i
+1], i
++;
1043 if (!strcmp (argv
[i
], "-iprefix")) {
1045 fatal ("Filename missing after -iprefix option");
1047 include_prefix
= argv
[++i
];
1049 /* Add directory to end of path for includes. */
1050 if (!strcmp (argv
[i
], "-idirafter")) {
1051 struct file_name_list
*dirtmp
;
1053 dirtmp
= (struct file_name_list
*)
1054 xmalloc (sizeof (struct file_name_list
));
1055 dirtmp
->next
= 0; /* New one goes on the end */
1056 dirtmp
->control_macro
= 0;
1058 fatal ("Directory name missing after -idirafter option");
1060 dirtmp
->fname
= argv
[++i
];
1062 if (after_include
== 0)
1063 after_include
= dirtmp
;
1065 last_after_include
->next
= dirtmp
;
1066 last_after_include
= dirtmp
; /* Tail follows the last one */
1071 if (out_fname
!= NULL
)
1072 fatal ("Output filename specified twice");
1074 fatal ("Filename missing after -o option");
1075 out_fname
= argv
[++i
];
1076 if (!strcmp (out_fname
, "-"))
1081 if (!strcmp (argv
[i
], "-pedantic"))
1083 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1085 pedantic_errors
= 1;
1086 } else if (!strcmp (argv
[i
], "-pcp")) {
1087 char *pcp_fname
= argv
[++i
];
1089 ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1090 ? fopen (pcp_fname
, "w")
1091 : fdopen (dup (fileno (stdout
)), "w"));
1092 if (pcp_outfile
== 0)
1093 pfatal_with_name (pcp_fname
);
1099 if (!strcmp (argv
[i
], "-traditional")) {
1101 if (dollars_in_ident
> 0)
1102 dollars_in_ident
= 1;
1103 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1109 if (! strcmp (argv
[i
], "-lang-c"))
1110 cplusplus
= 0, cplusplus_comments
= 0, objc
= 0;
1111 if (! strcmp (argv
[i
], "-lang-c++"))
1112 cplusplus
= 1, cplusplus_comments
= 1, objc
= 0;
1113 if (! strcmp (argv
[i
], "-lang-objc"))
1114 objc
= 1, cplusplus
= 0, cplusplus_comments
= 1;
1115 if (! strcmp (argv
[i
], "-lang-objc++"))
1116 objc
= 1, cplusplus
= 1, cplusplus_comments
= 1;
1117 if (! strcmp (argv
[i
], "-lang-asm"))
1119 if (! strcmp (argv
[i
], "-lint"))
1124 cplusplus
= 1, cplusplus_comments
= 1;
1128 inhibit_warnings
= 1;
1132 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1134 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1136 else if (!strcmp (argv
[i
], "-Wcomment"))
1138 else if (!strcmp (argv
[i
], "-Wno-comment"))
1140 else if (!strcmp (argv
[i
], "-Wcomments"))
1142 else if (!strcmp (argv
[i
], "-Wno-comments"))
1144 else if (!strcmp (argv
[i
], "-Wtraditional"))
1146 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1148 else if (!strcmp (argv
[i
], "-Wimport"))
1150 else if (!strcmp (argv
[i
], "-Wno-import"))
1152 else if (!strcmp (argv
[i
], "-Werror"))
1153 warnings_are_errors
= 1;
1154 else if (!strcmp (argv
[i
], "-Wno-error"))
1155 warnings_are_errors
= 0;
1156 else if (!strcmp (argv
[i
], "-Wall"))
1164 if (!strcmp (argv
[i
], "-M"))
1166 else if (!strcmp (argv
[i
], "-MM"))
1168 else if (!strcmp (argv
[i
], "-MD"))
1170 else if (!strcmp (argv
[i
], "-MMD"))
1172 /* For -MD and -MMD options, write deps on file named by next arg. */
1173 if (!strcmp (argv
[i
], "-MD")
1174 || !strcmp (argv
[i
], "-MMD")) {
1176 deps_file
= argv
[i
];
1178 /* For -M and -MM, write deps on standard output
1179 and suppress the usual output. */
1180 deps_stream
= stdout
;
1187 char *p
= argv
[i
] + 2;
1190 /* Arg to -d specifies what parts of macros to dump */
1193 dump_macros
= dump_only
;
1197 dump_macros
= dump_names
;
1200 dump_macros
= dump_definitions
;
1208 if (argv
[i
][2] == '3')
1213 fprintf (stderr
, "GNU CPP version %s", version_string
);
1214 #ifdef TARGET_VERSION
1217 fprintf (stderr
, "\n");
1221 print_include_names
= 1;
1228 if (argv
[i
][2] != 0)
1230 else if (i
+ 1 == argc
)
1231 fatal ("Macro name missing after -D option");
1243 if (argv
[i
][2] != 0)
1245 else if (i
+ 1 == argc
)
1246 fatal ("Assertion missing after -A option");
1250 if (!strcmp (p
, "-")) {
1251 /* -A- eliminates all predefined macros and assertions.
1252 Let's include also any that were specified earlier
1253 on the command line. That way we can get rid of any
1254 that were passed automatically in from GCC. */
1256 inhibit_predefs
= 1;
1257 for (j
= 0; j
< i
; j
++)
1258 pend_defs
[j
] = pend_assertions
[j
] = 0;
1260 pend_assertions
[i
] = p
;
1261 pend_assertion_options
[i
] = "-A";
1266 case 'U': /* JF #undef something */
1267 if (argv
[i
][2] != 0)
1268 pend_undefs
[i
] = argv
[i
] + 2;
1269 else if (i
+ 1 == argc
)
1270 fatal ("Macro name missing after -U option");
1272 pend_undefs
[i
] = argv
[i
+1], i
++;
1276 put_out_comments
= 1;
1279 case 'E': /* -E comes from cc -E; ignore it. */
1283 no_line_commands
= 1;
1286 case '$': /* Don't include $ in identifiers. */
1287 dollars_in_ident
= 0;
1290 case 'I': /* Add directory to path for includes. */
1292 struct file_name_list
*dirtmp
;
1294 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-")) {
1296 /* Don't use any preceding -I directories for #include <...>. */
1297 first_bracket_include
= 0;
1300 dirtmp
= (struct file_name_list
*)
1301 xmalloc (sizeof (struct file_name_list
));
1302 dirtmp
->next
= 0; /* New one goes on the end */
1303 dirtmp
->control_macro
= 0;
1304 if (argv
[i
][2] != 0)
1305 dirtmp
->fname
= argv
[i
] + 2;
1306 else if (i
+ 1 == argc
)
1307 fatal ("Directory name missing after -I option");
1309 dirtmp
->fname
= argv
[++i
];
1310 append_include_chain (dirtmp
, dirtmp
);
1316 if (!strcmp (argv
[i
], "-nostdinc"))
1317 /* -nostdinc causes no default include directories.
1318 You must specify all include-file directories with -I. */
1319 no_standard_includes
= 1;
1320 else if (!strcmp (argv
[i
], "-nostdinc++"))
1321 /* -nostdinc++ causes no default C++-specific include directories. */
1322 no_standard_cplusplus_includes
= 1;
1323 else if (!strcmp (argv
[i
], "-noprecomp"))
1328 /* Sun compiler passes undocumented switch "-undef".
1329 Let's assume it means to inhibit the predefined symbols. */
1330 inhibit_predefs
= 1;
1333 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1334 if (in_fname
== NULL
) {
1337 } else if (out_fname
== NULL
) {
1340 } /* else fall through into error */
1343 fatal ("Invalid option `%s'", argv
[i
]);
1348 /* Add dirs from CPATH after dirs from -I. */
1349 /* There seems to be confusion about what CPATH should do,
1350 so for the moment it is not documented. */
1351 /* Some people say that CPATH should replace the standard include dirs,
1352 but that seems pointless: it comes before them, so it overrides them
1354 p
= (char *) getenv ("CPATH");
1355 if (p
!= 0 && ! no_standard_includes
)
1358 /* Now that dollars_in_ident is known, initialize is_idchar. */
1359 initialize_char_syntax ();
1361 /* Initialize output buffer */
1363 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1364 outbuf
.bufp
= outbuf
.buf
;
1365 outbuf
.length
= OUTBUF_SIZE
;
1367 /* Do partial setup of input buffer for the sake of generating
1368 early #line directives (when -g is in effect). */
1370 fp
= &instack
[++indepth
];
1371 if (in_fname
== NULL
)
1373 fp
->nominal_fname
= fp
->fname
= in_fname
;
1376 /* Install __LINE__, etc. Must follow initialize_char_syntax
1377 and option processing. */
1378 initialize_builtins (fp
, &outbuf
);
1380 /* Do standard #defines and assertions
1381 that identify system and machine type. */
1383 if (!inhibit_predefs
) {
1384 char *p
= (char *) alloca (strlen (predefs
) + 1);
1385 strcpy (p
, predefs
);
1388 while (*p
== ' ' || *p
== '\t')
1390 /* Handle -D options. */
1391 if (p
[0] == '-' && p
[1] == 'D') {
1393 while (*p
&& *p
!= ' ' && *p
!= '\t')
1398 output_line_command (fp
, &outbuf
, 0, same_file
);
1399 make_definition (q
, &outbuf
);
1400 while (*p
== ' ' || *p
== '\t')
1402 } else if (p
[0] == '-' && p
[1] == 'A') {
1403 /* Handle -A options (assertions). */
1412 past_name
= assertion
;
1413 /* Locate end of name. */
1414 while (*past_name
&& *past_name
!= ' '
1415 && *past_name
!= '\t' && *past_name
!= '(')
1417 /* Locate `(' at start of value. */
1419 while (*value
&& (*value
== ' ' || *value
== '\t'))
1421 if (*value
++ != '(')
1423 while (*value
&& (*value
== ' ' || *value
== '\t'))
1426 /* Locate end of value. */
1427 while (*past_value
&& *past_value
!= ' '
1428 && *past_value
!= '\t' && *past_value
!= ')')
1430 termination
= past_value
;
1431 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
1433 if (*termination
++ != ')')
1435 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
1437 /* Temporarily null-terminate the value. */
1438 save_char
= *termination
;
1439 *termination
= '\0';
1440 /* Install the assertion. */
1441 make_assertion ("-A", assertion
);
1442 *termination
= (char) save_char
;
1444 while (*p
== ' ' || *p
== '\t')
1452 /* Now handle the command line options. */
1454 /* Do -U's, -D's and -A's in the order they were seen. */
1455 for (i
= 1; i
< argc
; i
++) {
1456 if (pend_undefs
[i
]) {
1458 output_line_command (fp
, &outbuf
, 0, same_file
);
1459 make_undef (pend_undefs
[i
], &outbuf
);
1463 output_line_command (fp
, &outbuf
, 0, same_file
);
1464 make_definition (pend_defs
[i
], &outbuf
);
1466 if (pend_assertions
[i
])
1467 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1470 done_initializing
= 1;
1472 { /* read the appropriate environment variable and if it exists
1473 replace include_defaults with the listed path. */
1475 switch ((objc
<< 1) + cplusplus
)
1478 epath
= getenv ("C_INCLUDE_PATH");
1481 epath
= getenv ("CPLUS_INCLUDE_PATH");
1484 epath
= getenv ("OBJC_INCLUDE_PATH");
1487 epath
= getenv ("OBJCPLUS_INCLUDE_PATH");
1490 /* If the environment var for this language is set,
1491 add to the default list of include directories. */
1493 char *nstore
= (char *) alloca (strlen (epath
) + 2);
1495 char *startp
, *endp
;
1497 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1498 if (*startp
== PATH_SEPARATOR
)
1501 = (struct default_include
*) xmalloc ((num_dirs
1502 * sizeof (struct default_include
))
1503 + sizeof (include_defaults_array
));
1504 startp
= endp
= epath
;
1507 /* Handle cases like c:/usr/lib:d:/gcc/lib */
1508 if ((*endp
== PATH_SEPARATOR
1509 #if 0 /* Obsolete, now that we use semicolons as the path separator. */
1511 && (endp
-startp
!= 1 || !isalpha (*startp
))
1516 strncpy (nstore
, startp
, endp
-startp
);
1518 strcpy (nstore
, ".");
1520 nstore
[endp
-startp
] = '\0';
1522 include_defaults
[num_dirs
].fname
= savestring (nstore
);
1523 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1527 endp
= startp
= endp
+ 1;
1531 /* Put the usual defaults back in at the end. */
1532 bcopy (include_defaults_array
, &include_defaults
[num_dirs
],
1533 sizeof (include_defaults_array
));
1537 first_system_include
= 0;
1538 /* Unless -fnostdinc,
1539 tack on the standard include file dirs to the specified list */
1540 if (!no_standard_includes
) {
1541 struct default_include
*p
= include_defaults
;
1542 char *specd_prefix
= include_prefix
;
1543 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
1544 int default_len
= 0;
1545 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1546 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1547 default_len
= strlen (default_prefix
) - 7;
1548 default_prefix
[default_len
] = 0;
1550 /* Search "translated" versions of GNU directories.
1551 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1552 if (specd_prefix
!= 0 && default_len
!= 0)
1553 for (p
= include_defaults
; p
->fname
; p
++) {
1554 /* Some standard dirs are only for C++. */
1555 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1556 /* Does this dir start with the prefix? */
1557 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1558 /* Yes; change prefix and add to search list. */
1559 struct file_name_list
*new
1560 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
1561 int this_len
= strlen (specd_prefix
) + strlen (p
->fname
) - default_len
;
1562 char *str
= (char *) xmalloc (this_len
+ 1);
1563 strcpy (str
, specd_prefix
);
1564 strcat (str
, p
->fname
+ default_len
);
1566 new->control_macro
= 0;
1567 append_include_chain (new, new);
1568 if (first_system_include
== 0)
1569 first_system_include
= new;
1573 /* Search ordinary names for GNU include directories. */
1574 for (p
= include_defaults
; p
->fname
; p
++) {
1575 /* Some standard dirs are only for C++. */
1576 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1577 struct file_name_list
*new
1578 = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
1579 new->control_macro
= 0;
1580 new->fname
= p
->fname
;
1581 append_include_chain (new, new);
1582 if (first_system_include
== 0)
1583 first_system_include
= new;
1588 /* Tack the after_include chain at the end of the include chain. */
1589 append_include_chain (after_include
, last_after_include
);
1590 if (first_system_include
== 0)
1591 first_system_include
= after_include
;
1593 /* Scan the -imacros files before the main input.
1594 Much like #including them, but with no_output set
1595 so that only their macro definitions matter. */
1598 for (i
= 1; i
< argc
; i
++)
1599 if (pend_files
[i
]) {
1600 int fd
= open (pend_files
[i
], O_RDONLY
, 0666);
1602 perror_with_name (pend_files
[i
]);
1603 return FAILURE_EXIT_CODE
;
1605 finclude (fd
, pend_files
[i
], &outbuf
, 0, NULL_PTR
);
1609 /* Copy the entire contents of the main input file into
1610 the stacked input buffer previously allocated for it. */
1612 /* JF check for stdin */
1613 if (in_fname
== NULL
|| *in_fname
== 0) {
1616 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
1619 /* Either of two environment variables can specify output of deps.
1620 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1621 where OUTPUT_FILE is the file to write deps info to
1622 and DEPS_TARGET is the target to mention in the deps. */
1625 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1626 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1627 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
1632 spec
= getenv ("SUNPRO_DEPENDENCIES");
1639 /* Find the space before the DEPS_TARGET, if there is one. */
1640 /* This should use index. (mrs) */
1641 while (*s
!= 0 && *s
!= ' ') s
++;
1643 deps_target
= s
+ 1;
1644 output_file
= (char *) xmalloc (s
- spec
+ 1);
1645 bcopy (spec
, output_file
, s
- spec
);
1646 output_file
[s
- spec
] = 0;
1653 deps_file
= output_file
;
1656 /* For -M, print the expected object file name
1657 as the target of this Make-rule. */
1659 deps_allocated_size
= 200;
1660 deps_buffer
= (char *) xmalloc (deps_allocated_size
);
1666 deps_output (deps_target
, 0);
1667 deps_output (":", 0);
1668 } else if (*in_fname
== 0)
1669 deps_output ("-: ", 0);
1674 /* Discard all directory prefixes from P. */
1680 /* Output P, but remove known suffixes. */
1682 if (p
[len
- 2] == '.' && p
[len
- 1] == 'c')
1683 deps_output (p
, len
- 2);
1684 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'C')
1685 deps_output (p
, len
- 2);
1686 else if (p
[len
- 3] == '.'
1687 && p
[len
- 2] == 'c'
1688 && p
[len
- 1] == 'c')
1689 deps_output (p
, len
- 3);
1690 else if (p
[len
- 2] == '.' && p
[len
- 1] == 's')
1691 deps_output (p
, len
- 2);
1692 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'S')
1693 deps_output (p
, len
- 2);
1694 else if (p
[len
- 2] == '.' && p
[len
- 1] == 'm')
1695 deps_output (p
, len
- 2);
1698 /* Supply our own suffix. */
1700 deps_output (".o : ", 0);
1702 deps_output (".obj : ", 0);
1704 deps_output (in_fname
, 0);
1705 deps_output (" ", 0);
1709 file_size_and_mode (f
, &st_mode
, &st_size
);
1710 fp
->nominal_fname
= fp
->fname
= in_fname
;
1712 fp
->system_header_p
= 0;
1713 /* JF all this is mine about reading pipes and ttys */
1714 if (! S_ISREG (st_mode
)) {
1715 /* Read input from a file that is not a normal disk file.
1716 We cannot preallocate a buffer with the correct size,
1717 so we must read in the file a piece at the time and make it bigger. */
1725 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
1728 cnt
= read (f
, bufp
, bsize
- size
);
1729 if (cnt
< 0) goto perror
; /* error! */
1730 if (cnt
== 0) break; /* End of file */
1733 if (bsize
== size
) { /* Buffer is full! */
1735 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
1736 bufp
= fp
->buf
+ size
; /* May have moved */
1741 /* Read a file whose size we can determine in advance.
1742 For the sake of VMS, st_size is just an upper bound. */
1745 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
1747 while (st_size
> 0) {
1748 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
1758 fp
->if_stack
= if_stack
;
1760 /* Make sure data ends with a newline. And put a null after it. */
1762 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
1763 /* Backslash-newline at end is not good enough. */
1764 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
1765 fp
->buf
[fp
->length
++] = '\n';
1766 missing_newline
= 1;
1768 fp
->buf
[fp
->length
] = '\0';
1770 /* Unless inhibited, convert trigraphs in the input. */
1775 /* Now that we know the input file is valid, open the output. */
1777 if (!out_fname
|| !strcmp (out_fname
, ""))
1778 out_fname
= "stdout";
1779 else if (! freopen (out_fname
, "w", stdout
))
1780 pfatal_with_name (out_fname
);
1782 output_line_command (fp
, &outbuf
, 0, same_file
);
1784 /* Scan the -include files before the main input. */
1786 for (i
= 1; i
< argc
; i
++)
1787 if (pend_includes
[i
]) {
1788 int fd
= open (pend_includes
[i
], O_RDONLY
, 0666);
1790 perror_with_name (pend_includes
[i
]);
1791 return FAILURE_EXIT_CODE
;
1793 finclude (fd
, pend_includes
[i
], &outbuf
, 0, NULL_PTR
);
1796 /* Scan the input, processing macros and directives. */
1798 rescan (&outbuf
, 0);
1800 if (pedantic
&& missing_newline
)
1801 pedwarn ("file does not end in newline");
1803 /* Now we have processed the entire input
1804 Write whichever kind of output has been requested. */
1806 if (dump_macros
== dump_only
)
1808 else if (! inhibit_output
) {
1813 /* Don't actually write the deps file if compilation has failed. */
1815 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, "a")))
1816 pfatal_with_name (deps_file
);
1817 fputs (deps_buffer
, deps_stream
);
1818 putc ('\n', deps_stream
);
1820 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
1821 fatal ("I/O error on output");
1826 if (ferror (stdout
) || fclose (stdout
) != 0)
1827 fatal ("I/O error on output");
1830 exit (FAILURE_EXIT_CODE
);
1831 exit (SUCCESS_EXIT_CODE
);
1834 pfatal_with_name (in_fname
);
1838 /* Given a colon-separated list of file names PATH,
1839 add all the names to the search path for include files. */
1853 struct file_name_list
*dirtmp
;
1855 /* Find the end of this name. */
1856 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
1858 /* An empty name in the path stands for the current directory. */
1859 name
= (char *) xmalloc (2);
1863 /* Otherwise use the directory that is named. */
1864 name
= (char *) xmalloc (q
- p
+ 1);
1865 bcopy (p
, name
, q
- p
);
1869 dirtmp
= (struct file_name_list
*)
1870 xmalloc (sizeof (struct file_name_list
));
1871 dirtmp
->next
= 0; /* New one goes on the end */
1872 dirtmp
->control_macro
= 0;
1873 dirtmp
->fname
= name
;
1874 append_include_chain (dirtmp
, dirtmp
);
1876 /* Advance past this name. */
1880 /* Skip the colon. */
1885 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
1886 before main CCCP processing. Name `pcp' is also in honor of the
1887 drugs the trigraph designers must have been on.
1889 Using an extra pass through the buffer takes a little extra time,
1890 but is infinitely less hairy than trying to handle trigraphs inside
1891 strings, etc. everywhere, and also makes sure that trigraphs are
1892 only translated in the top level of processing. */
1898 register U_CHAR c
, *fptr
, *bptr
, *sptr
;
1901 fptr
= bptr
= sptr
= buf
->buf
;
1902 while ((sptr
= (U_CHAR
*) index (sptr
, '?')) != NULL
) {
1939 len
= sptr
- fptr
- 2;
1940 if (bptr
!= fptr
&& len
> 0)
1941 bcopy (fptr
, bptr
, len
); /* BSD doc says bcopy () works right
1942 for overlapping strings. In ANSI
1943 C, this will be memmove (). */
1948 len
= buf
->length
- (fptr
- buf
->buf
);
1949 if (bptr
!= fptr
&& len
> 0)
1950 bcopy (fptr
, bptr
, len
);
1951 buf
->length
-= fptr
- bptr
;
1952 buf
->buf
[buf
->length
] = '\0';
1953 if (warn_trigraphs
&& fptr
!= bptr
)
1954 warning ("%d trigraph(s) encountered", (fptr
- bptr
) / 2);
1957 /* Move all backslash-newline pairs out of embarrassing places.
1958 Exchange all such pairs following BP
1959 with any potentially-embarrassing characters that follow them.
1960 Potentially-embarrassing characters are / and *
1961 (because a backslash-newline inside a comment delimiter
1962 would cause it not to be recognized). */
1968 register U_CHAR
*p
= bp
;
1969 register int count
= 0;
1971 /* First count the backslash-newline pairs here. */
1977 else if (p
[1] == '\r' && p
[2] == '\n')
1985 /* What follows the backslash-newlines is not embarrassing. */
1987 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
1990 /* Copy all potentially embarrassing characters
1991 that follow the backslash-newline pairs
1992 down to where the pairs originally started. */
1994 while (*p
== '*' || *p
== '/')
1997 /* Now write the same number of pairs after the embarrassing chars. */
1998 while (count
-- > 0) {
2004 /* Like newline_fix but for use within a directive-name.
2005 Move any backslash-newlines up past any following symbol constituents. */
2008 name_newline_fix (bp
)
2011 register U_CHAR
*p
= bp
;
2012 register int count
= 0;
2014 /* First count the backslash-newline pairs here. */
2019 else if (p
[1] == '\r' && p
[2] == '\n')
2027 /* What follows the backslash-newlines is not embarrassing. */
2029 if (count
== 0 || !is_idchar
[*p
])
2032 /* Copy all potentially embarrassing characters
2033 that follow the backslash-newline pairs
2034 down to where the pairs originally started. */
2036 while (is_idchar
[*p
])
2039 /* Now write the same number of pairs after the embarrassing chars. */
2040 while (count
-- > 0) {
2046 /* Look for lint commands in comments.
2048 When we come in here, ibp points into a comment. Limit is as one expects.
2049 scan within the comment -- it should start, after lwsp, with a lint command.
2050 If so that command is returned as a (constant) string.
2052 Upon return, any arg will be pointed to with argstart and will be
2053 arglen long. Note that we don't parse that arg since it will just
2054 be printed out again.
2058 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2059 register U_CHAR
*ibp
;
2060 register U_CHAR
*limit
;
2061 U_CHAR
**argstart
; /* point to command arg */
2062 int *arglen
, *cmdlen
; /* how long they are */
2065 register U_CHAR
*numptr
; /* temp for arg parsing */
2069 SKIP_WHITE_SPACE (ibp
);
2071 if (ibp
>= limit
) return NULL
;
2073 linsize
= limit
- ibp
;
2075 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2076 if ((linsize
>= 10) && !strncmp (ibp
, "NOTREACHED", 10)) {
2078 return "NOTREACHED";
2080 if ((linsize
>= 8) && !strncmp (ibp
, "ARGSUSED", 8)) {
2084 if ((linsize
>= 11) && !strncmp (ibp
, "LINTLIBRARY", 11)) {
2086 return "LINTLIBRARY";
2088 if ((linsize
>= 7) && !strncmp (ibp
, "VARARGS", 7)) {
2090 ibp
+= 7; linsize
-= 7;
2091 if ((linsize
== 0) || ! isdigit (*ibp
)) return "VARARGS";
2093 /* OK, read a number */
2094 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && isdigit (*numptr
);
2096 *arglen
= numptr
- *argstart
;
2103 * The main loop of the program.
2105 * Read characters from the input stack, transferring them to the
2108 * Macros are expanded and push levels on the input stack.
2109 * At the end of such a level it is popped off and we keep reading.
2110 * At the end of any other kind of level, we return.
2111 * #-directives are handled, except within macros.
2113 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2114 * and insert them when appropriate. This is set while scanning macro
2115 * arguments before substitution. It is zero when scanning for final output.
2116 * There are three types of Newline markers:
2117 * * Newline - follows a macro name that was not expanded
2118 * because it appeared inside an expansion of the same macro.
2119 * This marker prevents future expansion of that identifier.
2120 * When the input is rescanned into the final output, these are deleted.
2121 * These are also deleted by ## concatenation.
2122 * * Newline Space (or Newline and any other whitespace character)
2123 * stands for a place that tokens must be separated or whitespace
2124 * is otherwise desirable, but where the ANSI standard specifies there
2125 * is no whitespace. This marker turns into a Space (or whichever other
2126 * whitespace char appears in the marker) in the final output,
2127 * but it turns into nothing in an argument that is stringified with #.
2128 * Such stringified arguments are the only place where the ANSI standard
2129 * specifies with precision that whitespace may not appear.
2131 * During this function, IP->bufp is kept cached in IBP for speed of access.
2132 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2133 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2134 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2135 * explicitly, and before RECACHE, since RECACHE uses OBP.
2139 rescan (op
, output_marks
)
2143 /* Character being scanned in main loop. */
2146 /* Length of pending accumulated identifier. */
2147 register int ident_length
= 0;
2149 /* Hash code of pending accumulated identifier. */
2150 register int hash
= 0;
2152 /* Current input level (&instack[indepth]). */
2155 /* Pointer for scanning input. */
2156 register U_CHAR
*ibp
;
2158 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2159 register U_CHAR
*limit
;
2161 /* Pointer for storing output. */
2162 register U_CHAR
*obp
;
2164 /* REDO_CHAR is nonzero if we are processing an identifier
2165 after backing up over the terminating character.
2166 Sometimes we process an identifier without backing up over
2167 the terminating character, if the terminating character
2168 is not special. Backing up is done so that the terminating character
2169 will be dispatched on again once the identifier is dealt with. */
2172 /* 1 if within an identifier inside of which a concatenation
2173 marker (Newline -) has been seen. */
2174 int concatenated
= 0;
2176 /* While scanning a comment or a string constant,
2177 this records the line it started on, for error messages. */
2180 /* Line where a newline was first seen in a string constant. */
2181 int multiline_string_line
= 0;
2183 /* Record position of last `real' newline. */
2184 U_CHAR
*beg_of_line
;
2186 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2189 do { ip->macro->type = T_MACRO; \
2190 if (ip->free_ptr) free (ip->free_ptr); \
2191 --indepth; } while (0)
2193 /* Reload `rescan's local variables that describe the current
2194 level of the input stack. */
2197 do { ip = &instack[indepth]; \
2199 limit = ip->buf + ip->length; \
2201 check_expand (op, limit - ibp); \
2203 obp = op->bufp; } while (0)
2205 if (no_output
&& instack
[indepth
].fname
!= 0)
2206 skip_if_group (&instack
[indepth
], 1);
2213 /* Our caller must always put a null after the end of
2214 the input at each input stack level. */
2227 /* Always merge lines ending with backslash-newline,
2228 even in middle of identifier. */
2231 --obp
; /* remove backslash from obuf */
2234 /* Otherwise, backslash suppresses specialness of following char,
2235 so copy it here to prevent the switch from seeing it.
2236 But first get any pending identifier processed. */
2237 if (ident_length
> 0)
2243 if (assertions_flag
) {
2244 /* Copy #foo (bar lose) without macro expansion. */
2245 SKIP_WHITE_SPACE (ibp
);
2246 while (is_idchar
[*ibp
])
2248 SKIP_WHITE_SPACE (ibp
);
2251 skip_paren_group (ip
);
2252 bcopy (ibp
, obp
, ip
->bufp
- ibp
);
2253 obp
+= ip
->bufp
- ibp
;
2258 /* If this is expanding a macro definition, don't recognize
2259 preprocessor directives. */
2262 /* If this is expand_into_temp_buffer, recognize them
2263 only after an actual newline at this level,
2264 not at the beginning of the input level. */
2265 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
2271 /* # keyword: a # must be first nonblank char on the line */
2272 if (beg_of_line
== 0)
2277 /* Scan from start of line, skipping whitespace, comments
2278 and backslash-newlines, and see if we reach this #.
2279 If not, this # is not special. */
2281 /* If -traditional, require # to be at beginning of line. */
2284 if (is_hor_space
[*bp
])
2286 else if (*bp
== '\\' && bp
[1] == '\n')
2288 else if (*bp
== '/' && bp
[1] == '*') {
2290 while (!(*bp
== '*' && bp
[1] == '/'))
2294 else if (cplusplus_comments
&& *bp
== '/' && bp
[1] == '/') {
2296 while (*bp
++ != '\n') ;
2304 /* This # can start a directive. */
2306 --obp
; /* Don't copy the '#' */
2310 if (! handle_directive (ip
, op
)) {
2314 /* Not a known directive: treat it as ordinary text.
2315 IP, OP, IBP, etc. have not been changed. */
2316 if (no_output
&& instack
[indepth
].fname
) {
2317 /* If not generating expanded output,
2318 what we do with ordinary text is skip it.
2319 Discard everything until next # directive. */
2320 skip_if_group (&instack
[indepth
], 1);
2325 ++obp
; /* Copy the '#' after all */
2331 /* A # directive has been successfully processed. */
2332 /* If not generating expanded output, ignore everything until
2333 next # directive. */
2334 if (no_output
&& instack
[indepth
].fname
)
2335 skip_if_group (&instack
[indepth
], 1);
2341 case '\"': /* skip quoted string */
2343 /* A single quoted string is treated like a double -- some
2344 programs (e.g., troff) are perverse this way */
2349 start_line
= ip
->lineno
;
2351 /* Skip ahead to a matching quote. */
2355 if (ip
->macro
!= 0) {
2356 /* try harder: this string crosses a macro expansion boundary.
2357 This can happen naturally if -traditional.
2358 Otherwise, only -D can make a macro with an unmatched quote. */
2364 error_with_line (line_for_error (start_line
),
2365 "unterminated string or character constant");
2366 error_with_line (multiline_string_line
,
2367 "possible real start of unterminated constant");
2368 multiline_string_line
= 0;
2377 /* Traditionally, end of line ends a string constant with no error.
2378 So exit the loop and record the new line. */
2383 if (pedantic
|| c
== '\'') {
2384 error_with_line (line_for_error (start_line
),
2385 "unterminated string or character constant");
2388 if (multiline_string_line
== 0)
2389 multiline_string_line
= ip
->lineno
- 1;
2396 /* Backslash newline is replaced by nothing at all,
2397 but keep the line counts correct. */
2402 /* ANSI stupidly requires that in \\ the second \
2403 is *not* prevented from combining with a newline. */
2404 while (*ibp
== '\\' && ibp
[1] == '\n') {
2423 if (*ibp
== '\\' && ibp
[1] == '\n')
2427 && !(cplusplus_comments
&& *ibp
== '/'))
2435 /* C++ style comment... */
2436 start_line
= ip
->lineno
;
2438 --ibp
; /* Back over the slash */
2441 /* Comments are equivalent to spaces. */
2442 if (! put_out_comments
)
2445 /* must fake up a comment here */
2450 U_CHAR
*before_bp
= ibp
+2;
2452 while (ibp
< limit
) {
2453 if (*ibp
++ == '\n') {
2455 if (put_out_comments
) {
2456 bcopy (before_bp
, obp
, ibp
- before_bp
);
2457 obp
+= ibp
- before_bp
;
2466 /* Ordinary C comment. Skip it, optionally copying it to output. */
2468 start_line
= ip
->lineno
;
2470 ++ibp
; /* Skip the star. */
2472 /* If this cpp is for lint, we peek inside the comments: */
2476 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2478 if (lintcmd
!= NULL
) {
2479 /* I believe it is always safe to emit this newline: */
2481 bcopy ("#pragma lint ", obp
, 13);
2483 bcopy (lintcmd
, obp
, cmdlen
);
2488 bcopy (argbp
, obp
, arglen
);
2492 /* OK, now bring us back to the state we were in before we entered
2493 this branch. We need #line b/c the newline for the pragma
2494 could fuck things up. */
2495 output_line_command (ip
, op
, 0, same_file
);
2496 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2501 /* Comments are equivalent to spaces.
2502 Note that we already output the slash; we might not want it.
2503 For -traditional, a comment is equivalent to nothing. */
2504 if (! put_out_comments
) {
2514 U_CHAR
*before_bp
= ibp
;
2516 while (ibp
< limit
) {
2519 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
2520 warning("`/*' within comment");
2523 if (*ibp
== '\\' && ibp
[1] == '\n')
2525 if (ibp
>= limit
|| *ibp
== '/')
2530 /* Copy the newline into the output buffer, in order to
2531 avoid the pain of a #line every time a multiline comment
2533 if (!put_out_comments
)
2541 error_with_line (line_for_error (start_line
),
2542 "unterminated comment");
2545 if (put_out_comments
) {
2546 bcopy (before_bp
, obp
, ibp
- before_bp
);
2547 obp
+= ibp
- before_bp
;
2554 if (!dollars_in_ident
)
2558 case '0': case '1': case '2': case '3': case '4':
2559 case '5': case '6': case '7': case '8': case '9':
2560 /* If digit is not part of identifier, it starts a number,
2561 which means that following letters are not an identifier.
2562 "0x5" does not refer to an identifier "x5".
2563 So copy all alphanumerics that follow without accumulating
2564 as an identifier. Periods also, for sake of "3.e7". */
2566 if (ident_length
== 0) {
2567 while (ibp
< limit
) {
2568 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
2573 /* ".." terminates a preprocessing number. This is useless for C
2574 code but useful for preprocessing other things. */
2575 if (!isalnum (c
) && (c
!= '.' || *ibp
== '.') && c
!= '_') {
2580 /* A sign can be part of a preprocessing number
2581 if it follows an e. */
2582 if (c
== 'e' || c
== 'E') {
2583 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
2587 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
2589 /* But traditional C does not let the token go past the sign. */
2600 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2601 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2602 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2603 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2605 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2606 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
2607 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2608 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2612 /* Compute step of hash function, to avoid a proc call on every token */
2613 hash
= HASHSTEP (hash
, c
);
2617 /* If reprocessing a macro expansion, newline is a special marker. */
2618 if (ip
->macro
!= 0) {
2619 /* Newline White is a "funny space" to separate tokens that are
2620 supposed to be separate but without space between.
2621 Here White means any whitespace character.
2622 Newline - marks a recursive macro use that is not
2623 supposed to be expandable. */
2626 /* Newline - inhibits expansion of preceding token.
2627 If expanding a macro arg, we keep the newline -.
2628 In final output, it is deleted. */
2629 if (! concatenated
) {
2634 if (!output_marks
) {
2637 /* If expanding a macro arg, keep the newline -. */
2640 } else if (is_space
[*ibp
]) {
2641 /* Newline Space does not prevent expansion of preceding token
2642 so expand the preceding token and then come back. */
2643 if (ident_length
> 0)
2646 /* If generating final output, newline space makes a space. */
2647 if (!output_marks
) {
2649 /* And Newline Newline makes a newline, so count it. */
2650 if (obp
[-1] == '\n')
2653 /* If expanding a macro arg, keep the newline space.
2654 If the arg gets stringified, newline space makes nothing. */
2657 } else abort (); /* Newline followed by something random? */
2661 /* If there is a pending identifier, handle it and come back here. */
2662 if (ident_length
> 0)
2667 /* Update the line counts and output a #line if necessary. */
2670 if (ip
->lineno
!= op
->lineno
) {
2672 output_line_command (ip
, op
, 1, same_file
);
2673 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2678 /* Come here either after (1) a null character that is part of the input
2679 or (2) at the end of the input, because there is a null there. */
2682 /* Our input really contains a null character. */
2685 /* At end of a macro-expansion level, pop it and read next level. */
2686 if (ip
->macro
!= 0) {
2689 /* If traditional, and we have an identifier that ends here,
2690 process it now, so we get the right error for recursion. */
2691 if (traditional
&& ident_length
2692 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
2701 /* If we don't have a pending identifier,
2702 return at end of input. */
2703 if (ident_length
== 0) {
2711 /* If we do have a pending identifier, just consider this null
2712 a special character and arrange to dispatch on it again.
2713 The second time, IDENT_LENGTH will be zero so we will return. */
2719 /* Handle the case of a character such as /, ', " or null
2720 seen following an identifier. Back over it so that
2721 after the identifier is processed the special char
2722 will be dispatched on again. */
2732 if (ident_length
> 0) {
2733 register HASHNODE
*hp
;
2735 /* We have just seen an identifier end. If it's a macro, expand it.
2737 IDENT_LENGTH is the length of the identifier
2738 and HASH is its hash code.
2740 The identifier has already been copied to the output,
2741 so if it is a macro we must remove it.
2743 If REDO_CHAR is 0, the char that terminated the identifier
2744 has been skipped in the output and the input.
2745 OBP-IDENT_LENGTH-1 points to the identifier.
2746 If the identifier is a macro, we must back over the terminator.
2748 If REDO_CHAR is 1, the terminating char has already been
2749 backed over. OBP-IDENT_LENGTH points to the identifier. */
2751 if (!pcp_outfile
|| pcp_inside_if
) {
2753 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
2756 if (hp
->length
== ident_length
) {
2757 int obufp_before_macroname
;
2758 int op_lineno_before_macroname
;
2759 register int i
= ident_length
;
2760 register U_CHAR
*p
= hp
->name
;
2761 register U_CHAR
*q
= obp
- i
;
2767 do { /* All this to avoid a strncmp () */
2772 /* We found a use of a macro name.
2773 see if the context shows it is a macro call. */
2775 /* Back up over terminating character if not already done. */
2781 /* Save this as a displacement from the beginning of the output
2782 buffer. We can not save this as a position in the output
2783 buffer, because it may get realloc'ed by RECACHE. */
2784 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
2785 op_lineno_before_macroname
= op
->lineno
;
2787 if (hp
->type
== T_PCSTRING
) {
2788 pcstring_used (hp
); /* Mark the definition of this key
2789 as needed, ensuring that it
2791 break; /* Exit loop, since the key cannot have a
2792 definition any longer. */
2795 /* Record whether the macro is disabled. */
2796 disabled
= hp
->type
== T_DISABLED
;
2798 /* This looks like a macro ref, but if the macro was disabled,
2799 just copy its name and put in a marker if requested. */
2803 /* This error check caught useful cases such as
2804 #define foo(x,y) bar(x(y,0), y)
2807 error ("recursive use of macro `%s'", hp
->name
);
2811 check_expand (op
, limit
- ibp
+ 2);
2818 /* If macro wants an arglist, verify that a '(' follows.
2819 first skip all whitespace, copying it to the output
2820 after the macro name. Then, if there is no '(',
2821 decide this is not a macro call and leave things that way. */
2822 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
2823 && hp
->value
.defn
->nargs
>= 0)
2825 U_CHAR
*old_ibp
= ibp
;
2826 U_CHAR
*old_obp
= obp
;
2827 int old_iln
= ip
->lineno
;
2828 int old_oln
= op
->lineno
;
2831 /* Scan forward over whitespace, copying it to the output. */
2832 if (ibp
== limit
&& ip
->macro
!= 0) {
2837 old_iln
= ip
->lineno
;
2838 old_oln
= op
->lineno
;
2840 /* A comment: copy it unchanged or discard it. */
2841 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
2842 if (put_out_comments
) {
2845 } else if (! traditional
) {
2849 while (ibp
+ 1 != limit
2850 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
2851 /* We need not worry about newline-marks,
2852 since they are never found in comments. */
2854 /* Newline in a file. Count it. */
2858 if (put_out_comments
)
2864 if (put_out_comments
) {
2869 else if (is_space
[*ibp
]) {
2871 if (ibp
[-1] == '\n') {
2872 if (ip
->macro
== 0) {
2873 /* Newline in a file. Count it. */
2876 } else if (!output_marks
) {
2877 /* A newline mark, and we don't want marks
2878 in the output. If it is newline-hyphen,
2879 discard it entirely. Otherwise, it is
2880 newline-whitechar, so keep the whitechar. */
2890 /* A newline mark; copy both chars to the output. */
2898 /* It isn't a macro call.
2899 Put back the space that we just skipped. */
2902 ip
->lineno
= old_iln
;
2903 op
->lineno
= old_oln
;
2904 /* Exit the for loop. */
2909 /* This is now known to be a macro call.
2910 Discard the macro name from the output,
2911 along with any following whitespace just copied. */
2912 obp
= op
->buf
+ obufp_before_macroname
;
2913 op
->lineno
= op_lineno_before_macroname
;
2915 /* Expand the macro, reading arguments as needed,
2916 and push the expansion on the input stack. */
2919 macroexpand (hp
, op
);
2921 /* Reexamine input stack, since macroexpand has pushed
2922 a new level on it. */
2929 } /* End hash-table-search loop */
2931 ident_length
= hash
= 0; /* Stop collecting identifier */
2934 } /* End if (ident_length > 0) */
2936 } /* End per-char loop */
2938 /* Come here to return -- but first give an error message
2939 if there was an unterminated successful conditional. */
2941 if (if_stack
!= ip
->if_stack
) {
2943 switch (if_stack
->type
) {
2960 error_with_line (line_for_error (if_stack
->lineno
),
2961 "unterminated `#%s' conditional", str
);
2963 if_stack
= ip
->if_stack
;
2967 * Rescan a string into a temporary buffer and return the result
2968 * as a FILE_BUF. Note this function returns a struct, not a pointer.
2970 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
2971 * and insert such markers when appropriate. See `rescan' for details.
2972 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
2973 * before substitution; it is 0 for other uses.
2976 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
2977 U_CHAR
*buf
, *limit
;
2978 int output_marks
, assertions
;
2980 register FILE_BUF
*ip
;
2982 int length
= limit
- buf
;
2984 int odepth
= indepth
;
2985 int save_assertions_flag
= assertions_flag
;
2987 assertions_flag
= assertions
;
2992 /* Set up the input on the input stack. */
2994 buf1
= (U_CHAR
*) alloca (length
+ 1);
2996 register U_CHAR
*p1
= buf
;
2997 register U_CHAR
*p2
= buf1
;
3004 /* Set up to receive the output. */
3006 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3007 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3012 CHECK_DEPTH ({return obuf
;});
3016 ip
= &instack
[indepth
];
3018 ip
->nominal_fname
= 0;
3019 ip
->system_header_p
= 0;
3022 ip
->length
= length
;
3023 ip
->buf
= ip
->bufp
= buf1
;
3024 ip
->if_stack
= if_stack
;
3026 ip
->lineno
= obuf
.lineno
= 1;
3028 /* Scan the input, create the output. */
3029 rescan (&obuf
, output_marks
);
3031 /* Pop input stack to original state. */
3034 if (indepth
!= odepth
)
3037 /* Record the output. */
3038 obuf
.length
= obuf
.bufp
- obuf
.buf
;
3040 assertions_flag
= save_assertions_flag
;
3045 * Process a # directive. Expects IP->bufp to point after the '#', as in
3046 * `#define foo bar'. Passes to the command handler
3047 * (do_define, do_include, etc.): the addresses of the 1st and
3048 * last chars of the command (starting immediately after the #
3049 * keyword), plus op and the keyword table pointer. If the command
3050 * contains comments it is copied into a temporary buffer sans comments
3051 * and the temporary buffer is passed to the command handler instead.
3052 * Likewise for backslash-newlines.
3054 * Returns nonzero if this was a known # directive.
3055 * Otherwise, returns zero, without advancing the input pointer.
3059 handle_directive (ip
, op
)
3062 register U_CHAR
*bp
, *cp
;
3063 register struct directive
*kt
;
3064 register int ident_length
;
3067 /* Nonzero means we must copy the entire command
3068 to get rid of comments or backslash-newlines. */
3069 int copy_command
= 0;
3071 U_CHAR
*ident
, *after_ident
;
3075 /* Record where the directive started. do_xifdef needs this. */
3076 directive_start
= bp
- 1;
3078 /* Skip whitespace and \-newline. */
3080 if (is_hor_space
[*bp
]) {
3081 if ((*bp
== '\f' || *bp
== '\v') && pedantic
)
3082 pedwarn ("%s in preprocessing directive",
3083 *bp
== '\f' ? "formfeed" : "vertical tab");
3085 } else if (*bp
== '/' && bp
[1] == '*') {
3087 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3089 } else if (*bp
== '\\' && bp
[1] == '\n') {
3090 bp
+= 2; ip
->lineno
++;
3094 /* Now find end of directive name.
3095 If we encounter a backslash-newline, exchange it with any following
3096 symbol-constituents so that we end up with a contiguous name. */
3103 if (*cp
== '\\' && cp
[1] == '\n')
3104 name_newline_fix (cp
);
3110 ident_length
= cp
- bp
;
3114 /* A line of just `#' becomes blank. */
3116 if (ident_length
== 0 && *after_ident
== '\n') {
3117 ip
->bufp
= after_ident
;
3121 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3123 while (is_idchar
[*p
]) {
3124 if (*p
< '0' || *p
> '9')
3128 /* Handle # followed by a line number. */
3129 if (p
!= ident
&& !is_idchar
[*p
]) {
3130 static struct directive line_directive_table
[] = {
3131 { 4, do_line
, "line", T_LINE
},
3134 pedwarn ("`#' followed by integer");
3135 after_ident
= ident
;
3136 kt
= line_directive_table
;
3140 /* Avoid error for `###' and similar cases unless -pedantic. */
3142 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3144 if (pedantic
&& !lang_asm
)
3145 warning ("invalid preprocessor directive");
3151 error ("invalid preprocessor directive name");
3157 * Decode the keyword and call the appropriate expansion
3158 * routine, after moving the input pointer up to the next line.
3160 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3161 if (kt
->length
== ident_length
&& !strncmp (kt
->name
, ident
, ident_length
)) {
3162 register U_CHAR
*buf
;
3163 register U_CHAR
*limit
;
3166 int *already_output
= 0;
3168 /* Nonzero means do not delete comments within the directive.
3169 #define needs this when -traditional. */
3174 limit
= ip
->buf
+ ip
->length
;
3176 keep_comments
= traditional
&& kt
->traditional_comments
;
3177 /* #import is defined only in Objective C, or when on the NeXT. */
3178 if (kt
->type
== T_IMPORT
&& !(objc
|| lookup ("__NeXT__", -1, -1)))
3181 /* Find the end of this command (first newline not backslashed
3182 and not in a string or comment).
3183 Set COPY_COMMAND if the command must be copied
3184 (it contains a backslash-newline or a comment). */
3186 buf
= bp
= after_ident
;
3187 while (bp
< limit
) {
3188 register U_CHAR c
= *bp
++;
3202 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
3203 /* Don't bother calling the directive if we already got an error
3204 message due to unterminated string. Skip everything and pretend
3205 we called the directive. */
3208 /* Traditional preprocessing permits unterminated strings. */
3217 /* <...> is special for #include. */
3219 if (!kt
->angle_brackets
)
3221 while (*bp
&& *bp
!= '>') bp
++;
3225 if (*bp
== '\\' && bp
[1] == '\n')
3228 || (cplusplus_comments
&& *bp
== '/')) {
3229 U_CHAR
*obp
= bp
- 1;
3231 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3233 /* No need to copy the command because of a comment at the end;
3234 just don't include the comment in the directive. */
3235 if (bp
== limit
|| *bp
== '\n') {
3239 /* Don't remove the comments if -traditional. */
3240 if (! keep_comments
)
3248 pedwarn ("%s in preprocessing directive",
3249 c
== '\f' ? "formfeed" : "vertical tab");
3253 --bp
; /* Point to the newline */
3261 resume_p
= ip
->bufp
;
3262 /* BP is the end of the directive.
3263 RESUME_P is the next interesting data after the directive.
3264 A comment may come between. */
3266 /* If a directive should be copied through, and -E was given,
3267 pass it through before removing comments. */
3268 if (!no_output
&& kt
->pass_thru
&& put_out_comments
) {
3271 /* Output directive name. */
3272 check_expand (op
, kt
->length
+ 2);
3273 /* Make sure # is at the start of a line */
3274 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3279 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3280 op
->bufp
+= kt
->length
;
3282 /* Output arguments. */
3284 check_expand (op
, len
);
3285 bcopy (buf
, op
->bufp
, len
);
3287 /* Take account of any (escaped) newlines just output. */
3289 if (buf
[len
] == '\n')
3292 already_output
= &junk
;
3293 } /* Don't we need a newline or #line? */
3296 register U_CHAR
*xp
= buf
;
3297 /* Need to copy entire command into temp buffer before dispatching */
3299 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
3303 /* Copy to the new buffer, deleting comments
3304 and backslash-newlines (and whitespace surrounding the latter). */
3307 register U_CHAR c
= *xp
++;
3312 abort (); /* A bare newline should never part of the line. */
3315 /* <...> is special for #include. */
3317 if (!kt
->angle_brackets
)
3319 while (xp
< bp
&& c
!= '>') {
3321 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
3332 if (cp
!= buf
&& is_space
[cp
[-1]]) {
3333 while (cp
!= buf
&& is_space
[cp
[-1]]) cp
--;
3335 SKIP_WHITE_SPACE (xp
);
3336 } else if (is_space
[*xp
]) {
3338 SKIP_WHITE_SPACE (xp
);
3348 register U_CHAR
*bp1
3349 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
3350 NULL_PTR
, NULL_PTR
, NULL_PTR
);
3364 || (cplusplus_comments
&& *xp
== '/')) {
3366 /* If we already copied the command through,
3367 already_output != 0 prevents outputting comment now. */
3368 skip_to_end_of_comment (ip
, already_output
, 0);
3370 while (xp
!= ip
->bufp
)
3372 /* Delete or replace the slash. */
3373 else if (traditional
)
3382 /* Null-terminate the copy. */
3388 ip
->bufp
= resume_p
;
3390 /* Some directives should be written out for cc1 to process,
3391 just as if they were not defined. And sometimes we're copying
3392 definitions through. */
3394 if (!no_output
&& already_output
== 0
3396 || (kt
->type
== T_DEFINE
3397 && (dump_macros
== dump_names
3398 || dump_macros
== dump_definitions
)))) {
3401 /* Output directive name. */
3402 check_expand (op
, kt
->length
+ 1);
3404 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3405 op
->bufp
+= kt
->length
;
3407 if (kt
->pass_thru
|| dump_macros
== dump_definitions
) {
3408 /* Output arguments. */
3410 check_expand (op
, len
);
3411 bcopy (buf
, op
->bufp
, len
);
3413 } else if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
3416 SKIP_WHITE_SPACE (xp
);
3418 while (is_idchar
[*xp
]) xp
++;
3420 check_expand (op
, len
+ 1);
3422 bcopy (yp
, op
->bufp
, len
);
3425 } /* Don't we need a newline or #line? */
3427 /* Call the appropriate command handler. buf now points to
3428 either the appropriate place in the input buffer, or to
3429 the temp buffer if it was necessary to make one. cp
3430 points to the first char after the contents of the (possibly
3431 copied) command, in either case. */
3432 (*kt
->func
) (buf
, cp
, op
, kt
);
3433 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3439 /* It is deliberate that we don't warn about undefined directives.
3440 That is the responsibility of cc1. */
3447 static struct tm
*timebuf
;
3449 time_t t
= time (0);
3450 timebuf
= localtime (&t
);
3455 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3456 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3460 * expand things like __FILE__. Place the expansion into the output
3461 * buffer *without* rescanning.
3465 special_symbol (hp
, op
)
3472 FILE_BUF
*ip
= NULL
;
3475 int paren
= 0; /* For special `defined' keyword */
3477 if (pcp_outfile
&& pcp_inside_if
3478 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
3479 error ("Predefined macro `%s' used inside `#if' during precompilation",
3482 for (i
= indepth
; i
>= 0; i
--)
3483 if (instack
[i
].fname
!= NULL
) {
3488 error ("cccp error: not in any file?!");
3489 return; /* the show must go on */
3497 if (hp
->type
== T_FILE
)
3498 string
= ip
->nominal_fname
;
3500 string
= instack
[0].nominal_fname
;
3504 buf
= (char *) alloca (3 + strlen (string
));
3505 sprintf (buf
, "\"%s\"", string
);
3513 case T_INCLUDE_LEVEL
:
3515 for (i
= indepth
; i
>= 0; i
--)
3516 if (instack
[i
].fname
!= NULL
)
3519 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
3520 sprintf (buf
, "%d", true_indepth
- 1);
3524 buf
= (char *) alloca (3 + strlen (version_string
));
3525 sprintf (buf
, "\"%s\"", version_string
);
3529 buf
= (char *) alloca (3 + strlen (SIZE_TYPE
));
3530 sprintf (buf
, "%s", SIZE_TYPE
);
3533 case T_PTRDIFF_TYPE
:
3534 buf
= (char *) alloca (3 + strlen (PTRDIFF_TYPE
));
3535 sprintf (buf
, "%s", PTRDIFF_TYPE
);
3539 buf
= (char *) alloca (3 + strlen (WCHAR_TYPE
));
3540 sprintf (buf
, "%s", WCHAR_TYPE
);
3544 buf
= (char *) alloca (4 * sizeof (int));
3545 sprintf (buf
, "%d", hp
->value
.ival
);
3546 if (pcp_inside_if
&& pcp_outfile
)
3547 /* Output a precondition for this macro use */
3548 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
3552 buf
= (char *) alloca (10);
3553 sprintf (buf
, "%d", ip
->lineno
);
3558 buf
= (char *) alloca (20);
3559 timebuf
= timestamp ();
3560 if (hp
->type
== T_DATE
)
3561 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
3562 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
3564 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
3568 case T_SPEC_DEFINED
:
3569 buf
= " 0 "; /* Assume symbol is not defined */
3570 ip
= &instack
[indepth
];
3571 SKIP_WHITE_SPACE (ip
->bufp
);
3572 if (*ip
->bufp
== '(') {
3574 ip
->bufp
++; /* Skip over the paren */
3575 SKIP_WHITE_SPACE (ip
->bufp
);
3578 if (!is_idstart
[*ip
->bufp
])
3580 if (hp
= lookup (ip
->bufp
, -1, -1)) {
3581 if (pcp_outfile
&& pcp_inside_if
3582 && hp
->value
.defn
->predefined
)
3583 /* Output a precondition for this macro use. */
3584 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
3588 if (pcp_outfile
&& pcp_inside_if
) {
3589 /* Output a precondition for this macro use */
3590 U_CHAR
*cp
= ip
->bufp
;
3591 fprintf (pcp_outfile
, "#undef ");
3592 while (is_idchar
[*cp
]) /* Ick! */
3593 fputc (*cp
++, pcp_outfile
);
3594 putc ('\n', pcp_outfile
);
3596 while (is_idchar
[*ip
->bufp
])
3598 SKIP_WHITE_SPACE (ip
->bufp
);
3600 if (*ip
->bufp
!= ')')
3608 error ("`defined' without an identifier");
3612 error ("cccp error: invalid special hash type"); /* time for gdb */
3616 check_expand (op
, len
);
3617 bcopy (buf
, op
->bufp
, len
);
3624 /* Routines to handle #directives */
3626 /* Handle #include and #import.
3627 This function expects to see "fname" or <fname> on the input. */
3630 do_include (buf
, limit
, op
, keyword
)
3631 U_CHAR
*buf
, *limit
;
3633 struct directive
*keyword
;
3635 int importing
= (keyword
->type
== T_IMPORT
);
3636 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
3637 static int import_warning
= 0;
3638 char *fname
; /* Dynamically allocated fname buffer */
3641 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
3643 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
3644 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
3645 struct file_name_list
*searchptr
= 0;
3648 int f
; /* file number */
3650 int retried
= 0; /* Have already tried macro
3651 expanding the include line*/
3652 FILE_BUF trybuf
; /* It got expanded into here */
3653 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
3658 f
= -1; /* JF we iz paranoid! */
3660 if (importing
&& warn_import
&& !inhibit_warnings
3661 && !instack
[indepth
].system_header_p
&& !import_warning
) {
3663 warning ("using `#import' is not recommended");
3664 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
3665 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
3666 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
3667 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
3668 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
3669 fprintf (stderr
, " ... <real contents of file> ...\n");
3670 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3671 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
3672 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
3673 fprintf (stderr
, "when it is equipped with such a conditional.\n");
3679 SKIP_WHITE_SPACE (fbeg
);
3680 /* Discard trailing whitespace so we can easily see
3681 if we have parsed all the significant chars we were given. */
3682 while (limit
!= fbeg
&& is_hor_space
[limit
[-1]]) limit
--;
3688 /* Copy the operand text, concatenating the strings. */
3691 fbeg
= (U_CHAR
*) alloca (limit
- fbeg
+ 1);
3693 while (fin
!= limit
) {
3694 while (fin
!= limit
&& *fin
!= '\"')
3699 /* If not at the end, there had better be another string. */
3700 /* Skip just horiz space, and don't go past limit. */
3701 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
3702 if (fin
!= limit
&& *fin
== '\"')
3710 /* We have "filename". Figure out directory this source
3711 file is coming from and put it on the front of the list. */
3713 /* If -I- was specified, don't search current dir, only spec'd ones. */
3714 if (ignore_srcdir
) break;
3716 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
3721 if ((nam
= fp
->nominal_fname
) != NULL
) {
3722 /* Found a named file. Figure out dir of the file,
3723 and put it in front of the search list. */
3724 dsp
[0].next
= search_start
;
3727 ep
= rindex (nam
, '/');
3729 ep
= rindex (nam
, ']');
3730 if (ep
== NULL
) ep
= rindex (nam
, '>');
3731 if (ep
== NULL
) ep
= rindex (nam
, ':');
3732 if (ep
!= NULL
) ep
++;
3736 dsp
[0].fname
= (char *) alloca (n
+ 1);
3737 strncpy (dsp
[0].fname
, nam
, n
);
3738 dsp
[0].fname
[n
] = '\0';
3739 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
3740 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
3742 dsp
[0].fname
= 0; /* Current directory */
3752 while (fend
!= limit
&& *fend
!= '>') fend
++;
3753 if (*fend
== '>' && fend
+ 1 == limit
) {
3755 /* If -I-, start with the first -I dir after the -I-. */
3756 if (first_bracket_include
)
3757 search_start
= first_bracket_include
;
3766 error ("`#import' expects \"fname\" or <fname>");
3768 error ("`#include' expects \"fname\" or <fname>");
3771 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
3772 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
3773 bcopy (trybuf
.buf
, buf
, trybuf
.bufp
- trybuf
.buf
);
3774 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
3781 /* For #include_next, skip in the search path
3782 past the dir in which the containing file was found. */
3785 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
3786 if (fp
->fname
!= NULL
) {
3787 /* fp->dir is null if the containing file was specified
3788 with an absolute file name. In that case, don't skip anything. */
3790 search_start
= fp
->dir
->next
;
3796 /* Allocate this permanently, because it gets stored in the definitions
3798 fname
= (char *) xmalloc (max_include_len
+ flen
+ 2);
3799 /* + 2 above for slash and terminating null. */
3801 /* If specified file name is absolute, just open it. */
3804 strncpy (fname
, fbeg
, flen
);
3806 if (redundant_include_p (fname
))
3809 f
= lookup_import (fname
);
3811 f
= open (fname
, O_RDONLY
, 0666);
3813 return 0; /* Already included this file */
3815 /* Search directory path, trying to open the file.
3816 Copy each filename tried into FNAME. */
3818 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3819 if (searchptr
->fname
) {
3820 /* The empty string in a search path is ignored.
3821 This makes it possible to turn off entirely
3822 a standard piece of the list. */
3823 if (searchptr
->fname
[0] == 0)
3825 strcpy (fname
, searchptr
->fname
);
3826 strcat (fname
, "/");
3827 fname
[strlen (fname
) + flen
] = 0;
3831 strncat (fname
, fbeg
, flen
);
3833 /* Change this 1/2 Unix 1/2 VMS file specification into a
3834 full VMS file specification */
3835 if (searchptr
->fname
&& (searchptr
->fname
[0] != 0)) {
3836 /* Fix up the filename */
3837 hack_vms_include_specification (fname
);
3839 /* This is a normal VMS filespec, so use it unchanged. */
3840 strncpy (fname
, fbeg
, flen
);
3845 f
= lookup_import (fname
);
3847 f
= open (fname
, O_RDONLY
, 0666);
3849 return 0; /* Already included this file */
3850 if (redundant_include_p (fname
)) {
3860 /* A file that was not found. */
3862 strncpy (fname
, fbeg
, flen
);
3865 error_from_errno (fname
);
3867 error ("No include path in which to find %s", fname
);
3869 /* For -M, add this file to the dependencies. */
3870 if (print_deps
> (angle_brackets
|| (system_include_depth
> 0))) {
3871 /* Break the line before this. */
3872 deps_output ("", 0);
3874 /* If it was requested as a system header file,
3875 then assume it belongs in the first place to look for such. */
3876 if (angle_brackets
) {
3877 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
3878 if (searchptr
->fname
) {
3879 if (searchptr
->fname
[0] == 0)
3881 deps_output (searchptr
->fname
, 0);
3882 deps_output ("/", 0);
3887 /* Otherwise, omit the directory, as if the file existed
3888 in the directory with the source. */
3889 deps_output (fbeg
, flen
);
3890 deps_output (" ", 0);
3895 /* Check to see if this include file is a once-only include file.
3898 struct file_name_list
* ptr
;
3900 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
3901 if (!strcmp (ptr
->fname
, fname
)) {
3903 return 0; /* This file was once'd. */
3907 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
3908 if (!strcmp (ptr
->fname
, fname
))
3909 break; /* This file was included before. */
3913 /* This is the first time for this file. */
3914 /* Add it to list of files included. */
3916 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
3917 ptr
->control_macro
= 0;
3918 ptr
->next
= all_include_files
;
3919 all_include_files
= ptr
;
3920 ptr
->fname
= savestring (fname
);
3922 /* For -M, add this file to the dependencies. */
3923 if (print_deps
> (angle_brackets
|| (system_include_depth
> 0))) {
3924 deps_output ("", 0);
3925 deps_output (fname
, 0);
3926 deps_output (" ", 0);
3930 /* Handle -H option. */
3931 if (print_include_names
)
3932 fprintf (stderr
, "%s\n", fname
);
3935 system_include_depth
++;
3937 /* Actually process the file. */
3938 add_import (f
, fname
); /* Record file on "seen" list for #import. */
3940 pcftry
= (char *) alloca (strlen (fname
) + 30);
3948 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
3950 pcf
= open (pcftry
, O_RDONLY
, 0666);
3956 if (bcmp (&stat_f
.st_ino
, &s
.st_ino
, sizeof (s
.st_ino
))
3957 || stat_f
.st_dev
!= s
.st_dev
)
3959 pcfbuf
= check_precompiled (pcf
, fname
, &pcfbuflimit
);
3960 /* Don't need it any more. */
3965 /* Don't need it at all. */
3970 } while (pcf
!= -1 && !pcfbuf
);
3972 /* Actually process the file */
3974 pcfname
= xmalloc (strlen (pcftry
) + 1);
3975 strcpy (pcfname
, pcftry
);
3976 pcfinclude (pcfbuf
, pcfbuflimit
, fname
, op
);
3979 finclude (f
, fname
, op
, is_system_include (fname
), searchptr
);
3982 system_include_depth
--;
3987 /* Return nonzero if there is no need to include file NAME
3988 because it has already been included and it contains a conditional
3989 to make a repeated include do nothing. */
3992 redundant_include_p (name
)
3995 struct file_name_list
*l
= all_include_files
;
3996 for (; l
; l
= l
->next
)
3997 if (! strcmp (name
, l
->fname
)
3999 && lookup (l
->control_macro
, -1, -1))
4004 /* Return nonzero if the given FILENAME is an absolute pathname which
4005 designates a file within one of the known "system" include file
4006 directories. We assume here that if the given FILENAME looks like
4007 it is the name of a file which resides either directly in a "system"
4008 include file directory, or within any subdirectory thereof, then the
4009 given file must be a "system" include file. This function tells us
4010 if we should suppress pedantic errors/warnings for the given FILENAME. */
4013 is_system_include (filename
)
4014 register char *filename
;
4016 struct file_name_list
*searchptr
;
4018 for (searchptr
= first_system_include
; searchptr
;
4019 searchptr
= searchptr
->next
)
4020 if (searchptr
->fname
) {
4021 register char *sys_dir
= searchptr
->fname
;
4022 register unsigned length
= strlen (sys_dir
);
4024 if (! strncmp (sys_dir
, filename
, length
) && filename
[length
] == '/')
4030 /* Process the contents of include file FNAME, already open on descriptor F,
4032 SYSTEM_HEADER_P is 1 if this file was specified using <...>.
4033 DIRPTR is the link in the dir path through which this file was found,
4034 or 0 if the file name was absolute. */
4037 finclude (f
, fname
, op
, system_header_p
, dirptr
)
4041 int system_header_p
;
4042 struct file_name_list
*dirptr
;
4047 FILE_BUF
*fp
; /* For input stack frame */
4048 int missing_newline
= 0;
4050 CHECK_DEPTH (return;);
4052 if (file_size_and_mode (f
, &st_mode
, &st_size
) < 0)
4054 perror_with_name (fname
);
4059 fp
= &instack
[indepth
+ 1];
4060 bzero (fp
, sizeof (FILE_BUF
));
4061 fp
->nominal_fname
= fp
->fname
= fname
;
4064 fp
->if_stack
= if_stack
;
4065 fp
->system_header_p
= system_header_p
;
4068 if (S_ISREG (st_mode
)) {
4069 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
4072 /* Read the file contents, knowing that st_size is an upper bound
4073 on the number of bytes we can read. */
4074 while (st_size
> 0) {
4075 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
4085 /* Cannot count its file size before reading.
4086 First read the entire file into heap and
4087 copy them into buffer on stack. */
4094 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
4095 fp
->buf
= basep
; /* So it will get freed, on error. */
4099 i
= read (f
, bufp
, bsize
- st_size
);
4101 goto nope
; /* error! */
4103 break; /* End of file */
4106 if (bsize
== st_size
) { /* Buffer is full! */
4108 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
4110 bufp
= basep
+ st_size
; /* May have moved */
4114 fp
->length
= st_size
;
4117 /* Close descriptor now, so nesting does not use lots of descriptors. */
4120 /* Must do this before calling trigraph_pcp, so that the correct file name
4121 will be printed in warning messages. */
4124 input_file_stack_tick
++;
4129 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
4130 /* Backslash-newline at end is not good enough. */
4131 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
4132 fp
->buf
[fp
->length
++] = '\n';
4133 missing_newline
= 1;
4135 fp
->buf
[fp
->length
] = '\0';
4137 output_line_command (fp
, op
, 0, enter_file
);
4140 if (pedantic
&& missing_newline
)
4141 pedwarn ("file does not end in newline");
4144 input_file_stack_tick
++;
4145 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
4151 perror_with_name (fname
);
4156 /* Record that inclusion of the file named FILE
4157 should be controlled by the macro named MACRO_NAME.
4158 This means that trying to include the file again
4159 will do something if that macro is defined. */
4162 record_control_macro (file
, macro_name
)
4166 struct file_name_list
*new;
4168 for (new = all_include_files
; new; new = new->next
) {
4169 if (!strcmp (new->fname
, file
)) {
4170 new->control_macro
= macro_name
;
4175 /* If the file is not in all_include_files, something's wrong. */
4179 /* Maintain and search list of included files, for #import. */
4181 #define IMPORT_HASH_SIZE 31
4183 struct import_file
{
4187 struct import_file
*next
;
4190 /* Hash table of files already included with #include or #import. */
4192 static struct import_file
*import_hash_table
[IMPORT_HASH_SIZE
];
4194 /* Hash a file name for import_hash_table. */
4202 while (*f
) val
+= *f
++;
4203 return (val
%IMPORT_HASH_SIZE
);
4206 /* Search for file FILENAME in import_hash_table.
4207 Return -2 if found, either a matching name or a matching inode.
4208 Otherwise, open the file and return a file descriptor if successful
4209 or -1 if unsuccessful. */
4212 lookup_import (filename
)
4215 struct import_file
*i
;
4221 hashval
= import_hash (filename
);
4223 /* Attempt to find file in list of already included files */
4224 i
= import_hash_table
[hashval
];
4227 if (!strcmp (filename
, i
->name
))
4228 return -2; /* return found */
4231 /* Open it and try a match on inode/dev */
4232 fd
= open (filename
, O_RDONLY
, 0666);
4236 for (h
= 0; h
< IMPORT_HASH_SIZE
; h
++) {
4237 i
= import_hash_table
[h
];
4239 /* Compare the inode and the device.
4240 Supposedly on some systems the inode is not a scalar. */
4241 if (!bcmp (&i
->inode
, &sb
.st_ino
, sizeof (sb
.st_ino
))
4242 && i
->dev
== sb
.st_dev
) {
4244 return -2; /* return found */
4249 return fd
; /* Not found, return open file */
4252 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
4255 add_import (fd
, fname
)
4259 struct import_file
*i
;
4263 hashval
= import_hash (fname
);
4265 i
= (struct import_file
*)xmalloc (sizeof (struct import_file
));
4266 i
->name
= (char *)xmalloc (strlen (fname
)+1);
4267 strcpy (i
->name
, fname
);
4268 bcopy (&sb
.st_ino
, &i
->inode
, sizeof (sb
.st_ino
));
4270 i
->next
= import_hash_table
[hashval
];
4271 import_hash_table
[hashval
] = i
;
4274 /* Load the specified precompiled header into core, and verify its
4275 preconditions. PCF indicates the file descriptor to read, which must
4276 be a regular file. FNAME indicates the file name of the original
4277 header. *LIMIT will be set to an address one past the end of the file.
4278 If the preconditions of the file are not satisfied, the buffer is
4279 freed and we return 0. If the preconditions are satisfied, return
4280 the address of the buffer following the preconditions. The buffer, in
4281 this case, should never be freed because various pieces of it will
4282 be referred to until all precompiled strings are output at the end of
4286 check_precompiled (pcf
, fname
, limit
)
4302 if (file_size_and_mode (pcf
, &st_mode
, &st_size
) < 0)
4305 if (S_ISREG (st_mode
))
4307 buf
= xmalloc (st_size
+ 2);
4310 i
= read (pcf
, buf
+ length
, st_size
);
4322 if (length
> 0 && buf
[length
-1] != '\n')
4323 buf
[length
++] = '\n';
4326 *limit
= buf
+ length
;
4328 /* File is in core. Check the preconditions. */
4329 if (!check_preconditions (buf
))
4331 for (cp
= buf
; *cp
; cp
++)
4334 fprintf (stderr
, "Using preinclude %s\n", fname
);
4340 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
4346 /* PREC (null terminated) points to the preconditions of a
4347 precompiled header. These are a series of #define and #undef
4348 lines which must match the current contents of the hash
4351 check_preconditions (prec
)
4358 lineend
= (char *) index (prec
, '\n');
4360 if (*prec
++ != '#') {
4361 error ("Bad format encountered while reading precompiled file");
4364 if (!strncmp (prec
, "define", 6)) {
4368 mdef
= create_definition (prec
, lineend
, NULL_PTR
);
4373 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
4374 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
4375 || (hp
->type
== T_MACRO
4376 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
4377 && (mdef
.defn
->length
!= 2
4378 || mdef
.defn
->expansion
[0] != '\n'
4379 || mdef
.defn
->expansion
[1] != ' ')))
4381 } else if (!strncmp (prec
, "undef", 5)) {
4386 while (is_hor_space
[*prec
])
4389 while (is_idchar
[*prec
])
4393 if (lookup (name
, len
, -1))
4396 error ("Bad format encountered while reading precompiled file");
4401 /* They all passed successfully */
4405 /* Process the main body of a precompiled file. BUF points to the
4406 string section of the file, following the preconditions. LIMIT is one
4407 character past the end. NAME is the name of the file being read
4408 in. OP is the main output buffer */
4410 pcfinclude (buf
, limit
, name
, op
)
4411 U_CHAR
*buf
, *limit
, *name
;
4418 /* First in the file comes 4 bytes indicating the number of strings, */
4419 /* in network byte order. (MSB first). */
4421 nstrings
= (nstrings
<< 8) | *cp
++;
4422 nstrings
= (nstrings
<< 8) | *cp
++;
4423 nstrings
= (nstrings
<< 8) | *cp
++;
4425 /* Looping over each string... */
4426 while (nstrings
--) {
4427 U_CHAR
*string_start
;
4428 U_CHAR
*endofthiskey
;
4432 /* Each string starts with a STRINGDEF structure (str), followed */
4433 /* by the text of the string (string_start) */
4435 /* First skip to a longword boundary */
4436 /* ??? Why a 4-byte boundary? On all machines? */
4437 /* NOTE: This works correctly even if HOST_WIDE_INT
4438 is narrower than a pointer.
4439 Do not try risky measures here to get another type to use!
4440 Do not include gstddef.h or stddef.h--either one will fail! */
4441 if ((HOST_WIDE_INT
) cp
& 3)
4442 cp
+= 4 - ((HOST_WIDE_INT
) cp
& 3);
4444 /* Now get the string. */
4445 str
= (STRINGDEF
*) cp
;
4446 string_start
= cp
+= sizeof (STRINGDEF
);
4448 for (; *cp
; cp
++) /* skip the string */
4451 /* We need to macro expand the string here to ensure that the
4452 proper definition environment is in place. If it were only
4453 expanded when we find out it is needed, macros necessary for
4454 its proper expansion might have had their definitions changed. */
4455 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
4456 /* Lineno is already set in the precompiled file */
4457 str
->contents
= tmpbuf
.buf
;
4458 str
->len
= tmpbuf
.length
;
4460 str
->filename
= name
;
4461 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
4464 *stringlist_tailp
= str
;
4465 stringlist_tailp
= &str
->chain
;
4467 /* Next comes a fourbyte number indicating the number of keys */
4468 /* for this string. */
4470 nkeys
= (nkeys
<< 8) | *cp
++;
4471 nkeys
= (nkeys
<< 8) | *cp
++;
4472 nkeys
= (nkeys
<< 8) | *cp
++;
4474 /* If this number is -1, then the string is mandatory. */
4478 /* Otherwise, for each key, */
4479 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
4480 KEYDEF
*kp
= (KEYDEF
*) cp
;
4483 /* It starts with a KEYDEF structure */
4484 cp
+= sizeof (KEYDEF
);
4486 /* Find the end of the key. At the end of this for loop we
4487 advance CP to the start of the next key using this variable. */
4488 endofthiskey
= cp
+ strlen (cp
);
4491 /* Expand the key, and enter it into the hash table. */
4492 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
4493 tmpbuf
.bufp
= tmpbuf
.buf
;
4495 while (is_hor_space
[*tmpbuf
.bufp
])
4497 if (!is_idstart
[*tmpbuf
.bufp
]
4498 || tmpbuf
.bufp
== tmpbuf
.buf
+ tmpbuf
.length
) {
4503 hp
= lookup (tmpbuf
.bufp
, -1, -1);
4506 install (tmpbuf
.bufp
, -1, T_PCSTRING
, 0, (char *) kp
, -1);
4508 else if (hp
->type
== T_PCSTRING
) {
4509 kp
->chain
= hp
->value
.keydef
;
4510 hp
->value
.keydef
= kp
;
4516 /* This output_line_command serves to switch us back to the current
4517 input file in case some of these strings get output (which will
4518 result in line commands for the header file being output). */
4519 output_line_command (&instack
[indepth
], op
, 0, enter_file
);
4522 /* Called from rescan when it hits a key for strings. Mark them all */
4523 /* used and clean up. */
4530 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
4531 kp
->str
->writeflag
= 1;
4535 /* Write the output, interspersing precompiled strings in their */
4536 /* appropriate places. */
4540 STRINGDEF
*next_string
;
4541 U_CHAR
*cur_buf_loc
;
4542 int line_command_len
= 80;
4543 char *line_command
= xmalloc (line_command_len
);
4546 /* In each run through the loop, either cur_buf_loc == */
4547 /* next_string_loc, in which case we print a series of strings, or */
4548 /* it is less than next_string_loc, in which case we write some of */
4550 cur_buf_loc
= outbuf
.buf
;
4551 next_string
= stringlist
;
4553 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
4555 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
4556 if (next_string
->writeflag
) {
4557 len
= strlen (next_string
->filename
);
4558 if (len
> line_command_len
)
4559 line_command
= xrealloc (line_command
,
4560 line_command_len
*= 2);
4561 sprintf (line_command
, "\n# %d \"%s\"\n",
4562 next_string
->lineno
, next_string
->filename
);
4563 write (fileno (stdout
), line_command
,
4564 strlen (line_command
));
4565 write (fileno (stdout
),
4566 next_string
->contents
, next_string
->len
);
4568 next_string
= next_string
->chain
;
4572 ? (next_string
->output_mark
4573 - (cur_buf_loc
- outbuf
.buf
))
4574 : outbuf
.bufp
- cur_buf_loc
);
4576 write (fileno (stdout
), cur_buf_loc
, len
);
4582 /* Pass a directive through to the output file.
4583 BUF points to the contents of the directive, as a contiguous string.
4584 LIMIT points to the first character past the end of the directive.
4585 KEYWORD is the keyword-table entry for the directive. */
4588 pass_thru_directive (buf
, limit
, op
, keyword
)
4589 U_CHAR
*buf
, *limit
;
4591 struct directive
*keyword
;
4593 register unsigned keyword_length
= keyword
->length
;
4595 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
4597 bcopy (keyword
->name
, op
->bufp
, keyword_length
);
4598 op
->bufp
+= keyword_length
;
4599 if (limit
!= buf
&& buf
[0] != ' ')
4601 bcopy (buf
, op
->bufp
, limit
- buf
);
4602 op
->bufp
+= (limit
- buf
);
4605 /* Count the line we have just made in the output,
4606 to get in sync properly. */
4611 /* The arglist structure is built by do_define to tell
4612 collect_definition where the argument names begin. That
4613 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
4614 would contain pointers to the strings x, y, and z.
4615 Collect_definition would then build a DEFINITION node,
4616 with reflist nodes pointing to the places x, y, and z had
4617 appeared. So the arglist is just convenience data passed
4618 between these two routines. It is not kept around after
4619 the current #define has been processed and entered into the
4623 struct arglist
*next
;
4630 /* Create a DEFINITION node from a #define directive. Arguments are
4631 as for do_define. */
4633 create_definition (buf
, limit
, op
)
4634 U_CHAR
*buf
, *limit
;
4637 U_CHAR
*bp
; /* temp ptr into input buffer */
4638 U_CHAR
*symname
; /* remember where symbol name starts */
4639 int sym_length
; /* and how long it is */
4640 int line
= instack
[indepth
].lineno
;
4641 char *file
= instack
[indepth
].nominal_fname
;
4645 int arglengths
= 0; /* Accumulate lengths of arg names
4646 plus number of args. */
4651 while (is_hor_space
[*bp
])
4654 symname
= bp
; /* remember where it starts */
4655 sym_length
= check_macro_name (bp
, "macro");
4658 /* Lossage will occur if identifiers or control keywords are broken
4659 across lines using backslash. This is not the right place to take
4663 struct arglist
*arg_ptrs
= NULL
;
4666 bp
++; /* skip '(' */
4667 SKIP_WHITE_SPACE (bp
);
4669 /* Loop over macro argument names. */
4670 while (*bp
!= ')') {
4671 struct arglist
*temp
;
4673 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
4675 temp
->next
= arg_ptrs
;
4676 temp
->argno
= argno
++;
4677 temp
->rest_args
= 0;
4681 pedwarn ("another parameter follows `%s'",
4684 if (!is_idstart
[*bp
])
4685 pedwarn ("invalid character in macro parameter name");
4687 /* Find the end of the arg name. */
4688 while (is_idchar
[*bp
]) {
4690 /* do we have a "special" rest-args extension here? */
4691 if (limit
- bp
> REST_EXTENSION_LENGTH
&&
4692 strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
4694 temp
->rest_args
= 1;
4698 temp
->length
= bp
- temp
->name
;
4700 bp
+= REST_EXTENSION_LENGTH
;
4701 arglengths
+= temp
->length
+ 2;
4702 SKIP_WHITE_SPACE (bp
);
4703 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
4704 error ("badly punctuated parameter list in `#define'");
4709 SKIP_WHITE_SPACE (bp
);
4712 error ("unterminated parameter list in `#define'");
4716 struct arglist
*otemp
;
4718 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
4719 if (temp
->length
== otemp
->length
&&
4720 strncmp(temp
->name
, otemp
->name
, temp
->length
) == 0) {
4723 name
= (U_CHAR
*) alloca(temp
->length
+ 1);
4724 (void) strncpy(name
, temp
->name
, temp
->length
);
4725 name
[temp
->length
] = '\0';
4726 error ("duplicate argument name `%s' in `#define'", name
);
4732 ++bp
; /* skip paren */
4733 /* Skip exactly one space or tab if any. */
4734 if (bp
< limit
&& (*bp
== ' ' || *bp
== '\t')) ++bp
;
4735 /* now everything from bp before limit is the definition. */
4736 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
4737 defn
->rest_args
= rest_args
;
4739 /* Now set defn->args.argnames to the result of concatenating
4740 the argument names in reverse order
4741 with comma-space between them. */
4742 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
4744 struct arglist
*temp
;
4746 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
4747 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
4749 if (temp
->next
!= 0) {
4750 defn
->args
.argnames
[i
++] = ',';
4751 defn
->args
.argnames
[i
++] = ' ';
4754 defn
->args
.argnames
[i
] = 0;
4757 /* simple expansion or empty definition; gobble it */
4758 if (is_hor_space
[*bp
])
4759 ++bp
; /* skip exactly one blank/tab char */
4760 /* now everything from bp before limit is the definition. */
4761 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
4762 defn
->args
.argnames
= (U_CHAR
*) "";
4768 /* OP is null if this is a predefinition */
4769 defn
->predefined
= !op
;
4771 mdef
.symnam
= symname
;
4772 mdef
.symlen
= sym_length
;
4781 /* Process a #define command.
4782 BUF points to the contents of the #define command, as a contiguous string.
4783 LIMIT points to the first character past the end of the definition.
4784 KEYWORD is the keyword-table entry for #define. */
4787 do_define (buf
, limit
, op
, keyword
)
4788 U_CHAR
*buf
, *limit
;
4790 struct directive
*keyword
;
4795 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
4796 if (pcp_outfile
&& op
)
4797 pass_thru_directive (buf
, limit
, op
, keyword
);
4799 mdef
= create_definition (buf
, limit
, op
);
4803 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
4807 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
4809 /* Redefining a precompiled key is ok. */
4810 if (hp
->type
== T_PCSTRING
)
4812 /* Redefining a macro is ok if the definitions are the same. */
4813 else if (hp
->type
== T_MACRO
)
4814 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
4815 /* Redefining a constant is ok with -D. */
4816 else if (hp
->type
== T_CONST
)
4817 ok
= ! done_initializing
;
4818 /* Print the warning if it's not ok. */
4820 U_CHAR
*msg
; /* what pain... */
4822 /* If we are passing through #define and #undef directives, do
4823 that for this re-definition now. */
4824 if (debug_output
&& op
)
4825 pass_thru_directive (buf
, limit
, op
, keyword
);
4827 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
4829 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
4830 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
4832 if (hp
->type
== T_MACRO
)
4833 pedwarn_with_file_and_line (hp
->value
.defn
->file
, hp
->value
.defn
->line
,
4834 "this is the location of the previous definition");
4836 /* Replace the old definition. */
4838 hp
->value
.defn
= mdef
.defn
;
4840 /* If we are passing through #define and #undef directives, do
4841 that for this new definition now. */
4842 if (debug_output
&& op
)
4843 pass_thru_directive (buf
, limit
, op
, keyword
);
4844 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
4845 (char *) mdef
.defn
, hashcode
);
4856 /* Check a purported macro name SYMNAME, and yield its length.
4857 USAGE is the kind of name this is intended for. */
4860 check_macro_name (symname
, usage
)
4867 for (p
= symname
; is_idchar
[*p
]; p
++)
4869 sym_length
= p
- symname
;
4870 if (sym_length
== 0)
4871 error ("invalid %s name", usage
);
4872 else if (!is_idstart
[*symname
]) {
4873 U_CHAR
*msg
; /* what pain... */
4874 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
4875 bcopy (symname
, msg
, sym_length
);
4876 msg
[sym_length
] = 0;
4877 error ("invalid %s name `%s'", usage
, msg
);
4879 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
4880 error ("invalid %s name `defined'", usage
);
4886 * return zero if two DEFINITIONs are isomorphic
4889 compare_defs (d1
, d2
)
4890 DEFINITION
*d1
, *d2
;
4892 register struct reflist
*a1
, *a2
;
4893 register U_CHAR
*p1
= d1
->expansion
;
4894 register U_CHAR
*p2
= d2
->expansion
;
4897 if (d1
->nargs
!= d2
->nargs
)
4899 if (strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
4901 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
4902 a1
= a1
->next
, a2
= a2
->next
) {
4903 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
4904 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
4905 || a1
->argno
!= a2
->argno
4906 || a1
->stringify
!= a2
->stringify
4907 || a1
->raw_before
!= a2
->raw_before
4908 || a1
->raw_after
!= a2
->raw_after
)
4916 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
4917 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
4922 /* Return 1 if two parts of two macro definitions are effectively different.
4923 One of the parts starts at BEG1 and has LEN1 chars;
4924 the other has LEN2 chars at BEG2.
4925 Any sequence of whitespace matches any other sequence of whitespace.
4926 FIRST means these parts are the first of a macro definition;
4927 so ignore leading whitespace entirely.
4928 LAST means these parts are the last of a macro definition;
4929 so ignore trailing whitespace entirely. */
4932 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
4934 U_CHAR
*beg1
, *beg2
;
4938 register U_CHAR
*end1
= beg1
+ len1
;
4939 register U_CHAR
*end2
= beg2
+ len2
;
4941 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
4942 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
4945 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
4946 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
4948 while (beg1
!= end1
&& beg2
!= end2
) {
4949 if (is_space
[*beg1
] && is_space
[*beg2
]) {
4950 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
4951 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
4952 } else if (*beg1
== *beg2
) {
4956 return (beg1
!= end1
) || (beg2
!= end2
);
4959 /* Read a replacement list for a macro with parameters.
4960 Build the DEFINITION structure.
4961 Reads characters of text starting at BUF until END.
4962 ARGLIST specifies the formal parameters to look for
4963 in the text of the definition; NARGS is the number of args
4964 in that list, or -1 for a macro name that wants no argument list.
4965 MACRONAME is the macro name itself (so we can avoid recursive expansion)
4966 and NAMELEN is its length in characters.
4968 Note that comments and backslash-newlines have already been deleted
4969 from the argument. */
4971 /* Leading and trailing Space, Tab, etc. are converted to markers
4972 Newline Space, Newline Tab, etc.
4973 Newline Space makes a space in the final output
4974 but is discarded if stringified. (Newline Tab is similar but
4975 makes a Tab instead.)
4977 If there is no trailing whitespace, a Newline Space is added at the end
4978 to prevent concatenation that would be contrary to the standard. */
4981 collect_expansion (buf
, end
, nargs
, arglist
)
4984 struct arglist
*arglist
;
4987 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
4988 struct reflist
*endpat
= NULL
;
4989 /* Pointer to first nonspace after last ## seen. */
4991 /* Pointer to first nonspace after last single-# seen. */
4992 U_CHAR
*stringify
= 0;
4994 int expected_delimiter
= '\0';
4996 /* Scan thru the replacement list, ignoring comments and quoted
4997 strings, picking up on the macro calls. It does a linear search
4998 thru the arg list on every potential symbol. Profiling might say
4999 that something smarter should happen. */
5004 /* Find the beginning of the trailing whitespace. */
5005 /* Find end of leading whitespace. */
5008 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
5009 while (p
< limit
&& is_space
[*p
]) p
++;
5011 /* Allocate space for the text in the macro definition.
5012 Leading and trailing whitespace chars need 2 bytes each.
5013 Each other input char may or may not need 1 byte,
5014 so this is an upper bound.
5015 The extra 2 are for invented trailing newline-marker and final null. */
5016 maxsize
= (sizeof (DEFINITION
)
5017 + 2 * (end
- limit
) + 2 * (p
- buf
)
5019 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
5021 defn
->nargs
= nargs
;
5022 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
5027 /* Convert leading whitespace to Newline-markers. */
5028 while (p
< limit
&& is_space
[*p
]) {
5033 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
5034 error ("`##' at start of macro definition");
5038 /* Process the main body of the definition. */
5040 int skipped_arg
= 0;
5041 register U_CHAR c
= *p
++;
5049 if (expected_delimiter
!= '\0') {
5050 if (c
== expected_delimiter
)
5051 expected_delimiter
= '\0';
5053 expected_delimiter
= c
;
5056 /* Special hack: if a \# is written in the #define
5057 include a # in the definition. This is useless for C code
5058 but useful for preprocessing other things. */
5061 /* \# quotes a # even outside of strings. */
5062 if (p
< limit
&& *p
== '#' && !expected_delimiter
) {
5065 } else if (p
< limit
&& expected_delimiter
) {
5066 /* In a string, backslash goes through
5067 and makes next char ordinary. */
5073 /* # is ordinary inside a string. */
5074 if (expected_delimiter
)
5076 if (p
< limit
&& *p
== '#') {
5077 /* ##: concatenate preceding and following tokens. */
5078 /* Take out the first #, discard preceding whitespace. */
5080 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
5082 /* Skip the second #. */
5084 /* Discard following whitespace. */
5085 SKIP_WHITE_SPACE (p
);
5088 error ("`##' at end of macro definition");
5090 /* Single #: stringify following argument ref.
5091 Don't leave the # in the expansion. */
5093 SKIP_WHITE_SPACE (p
);
5094 if (p
== limit
|| ! is_idstart
[*p
] || nargs
<= 0)
5095 error ("`#' operator is not followed by a macro argument name");
5102 /* In -traditional mode, recognize arguments inside strings and
5103 and character constants, and ignore special properties of #.
5104 Arguments inside strings are considered "stringified", but no
5105 extra quote marks are supplied. */
5109 if (expected_delimiter
!= '\0') {
5110 if (c
== expected_delimiter
)
5111 expected_delimiter
= '\0';
5113 expected_delimiter
= c
;
5117 /* Backslash quotes delimiters and itself, but not macro args. */
5118 if (expected_delimiter
!= 0 && p
< limit
5119 && (*p
== expected_delimiter
|| *p
== '\\')) {
5126 if (expected_delimiter
!= '\0') /* No comments inside strings. */
5129 /* If we find a comment that wasn't removed by handle_directive,
5130 this must be -traditional. So replace the comment with
5134 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
5137 /* Mark this as a concatenation-point, as if it had been ##. */
5145 /* Handle the start of a symbol. */
5146 if (is_idchar
[c
] && nargs
> 0) {
5147 U_CHAR
*id_beg
= p
- 1;
5151 while (p
!= limit
&& is_idchar
[*p
]) p
++;
5152 id_len
= p
- id_beg
;
5154 if (is_idstart
[c
]) {
5155 register struct arglist
*arg
;
5157 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
5158 struct reflist
*tpat
;
5160 if (arg
->name
[0] == c
5161 && arg
->length
== id_len
5162 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
5163 if (expected_delimiter
&& warn_stringify
) {
5165 warning ("macro argument `%.*s' is stringified.",
5168 warning ("macro arg `%.*s' would be stringified with -traditional.",
5172 /* If ANSI, don't actually substitute inside a string. */
5173 if (!traditional
&& expected_delimiter
)
5175 /* make a pat node for this arg and append it to the end of
5177 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
5179 tpat
->raw_before
= concat
== id_beg
;
5180 tpat
->raw_after
= 0;
5181 tpat
->rest_args
= arg
->rest_args
;
5182 tpat
->stringify
= (traditional
? expected_delimiter
!= '\0'
5183 : stringify
== id_beg
);
5186 defn
->pattern
= tpat
;
5188 endpat
->next
= tpat
;
5191 tpat
->argno
= arg
->argno
;
5192 tpat
->nchars
= exp_p
- lastp
;
5194 register U_CHAR
*p1
= p
;
5195 SKIP_WHITE_SPACE (p1
);
5196 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
5197 tpat
->raw_after
= 1;
5199 lastp
= exp_p
; /* place to start copying from next time */
5206 /* If this was not a macro arg, copy it into the expansion. */
5207 if (! skipped_arg
) {
5208 register U_CHAR
*lim1
= p
;
5212 if (stringify
== id_beg
)
5213 error ("`#' operator should be followed by a macro argument name");
5219 /* Convert trailing whitespace to Newline-markers. */
5220 while (limit
< end
&& is_space
[*limit
]) {
5222 *exp_p
++ = *limit
++;
5224 } else if (!traditional
&& expected_delimiter
== 0) {
5225 /* There is no trailing whitespace, so invent some in ANSI mode.
5226 But not if "inside a string" (which in ANSI mode
5227 happens only for -D option). */
5234 defn
->length
= exp_p
- defn
->expansion
;
5236 /* Crash now if we overrun the allocated size. */
5237 if (defn
->length
+ 1 > maxsize
)
5241 /* This isn't worth the time it takes. */
5242 /* give back excess storage */
5243 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
5250 do_assert (buf
, limit
, op
, keyword
)
5251 U_CHAR
*buf
, *limit
;
5253 struct directive
*keyword
;
5255 U_CHAR
*bp
; /* temp ptr into input buffer */
5256 U_CHAR
*symname
; /* remember where symbol name starts */
5257 int sym_length
; /* and how long it is */
5258 struct arglist
*tokens
= NULL
;
5260 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
5261 pedwarn ("ANSI C does not allow `#assert'");
5265 while (is_hor_space
[*bp
])
5268 symname
= bp
; /* remember where it starts */
5269 sym_length
= check_macro_name (bp
, "assertion");
5271 /* #define doesn't do this, but we should. */
5272 SKIP_WHITE_SPACE (bp
);
5274 /* Lossage will occur if identifiers or control tokens are broken
5275 across lines using backslash. This is not the right place to take
5279 error ("missing token-sequence in `#assert'");
5286 bp
++; /* skip '(' */
5287 SKIP_WHITE_SPACE (bp
);
5289 tokens
= read_token_list (&bp
, limit
, &error_flag
);
5293 error ("empty token-sequence in `#assert'");
5297 ++bp
; /* skip paren */
5298 SKIP_WHITE_SPACE (bp
);
5301 /* If this name isn't already an assertion name, make it one.
5302 Error if it was already in use in some other way. */
5305 ASSERTION_HASHNODE
*hp
;
5306 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
5307 struct tokenlist_list
*value
5308 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
5310 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
5312 if (sym_length
== 7 && ! strncmp (symname
, "defined", sym_length
))
5313 error ("`defined' redefined as assertion");
5314 hp
= assertion_install (symname
, sym_length
, hashcode
);
5317 /* Add the spec'd token-sequence to the list of such. */
5318 value
->tokens
= tokens
;
5319 value
->next
= hp
->value
;
5327 do_unassert (buf
, limit
, op
, keyword
)
5328 U_CHAR
*buf
, *limit
;
5330 struct directive
*keyword
;
5332 U_CHAR
*bp
; /* temp ptr into input buffer */
5333 U_CHAR
*symname
; /* remember where symbol name starts */
5334 int sym_length
; /* and how long it is */
5336 struct arglist
*tokens
= NULL
;
5337 int tokens_specified
= 0;
5339 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
5340 pedwarn ("ANSI C does not allow `#unassert'");
5344 while (is_hor_space
[*bp
])
5347 symname
= bp
; /* remember where it starts */
5348 sym_length
= check_macro_name (bp
, "assertion");
5350 /* #define doesn't do this, but we should. */
5351 SKIP_WHITE_SPACE (bp
);
5353 /* Lossage will occur if identifiers or control tokens are broken
5354 across lines using backslash. This is not the right place to take
5360 bp
++; /* skip '(' */
5361 SKIP_WHITE_SPACE (bp
);
5363 tokens
= read_token_list (&bp
, limit
, &error_flag
);
5367 error ("empty token list in `#unassert'");
5371 tokens_specified
= 1;
5373 ++bp
; /* skip paren */
5374 SKIP_WHITE_SPACE (bp
);
5378 ASSERTION_HASHNODE
*hp
;
5379 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
5380 struct tokenlist_list
*tail
, *prev
;
5382 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
5386 /* If no token list was specified, then eliminate this assertion
5388 if (! tokens_specified
) {
5389 struct tokenlist_list
*next
;
5390 for (tail
= hp
->value
; tail
; tail
= next
) {
5392 free_token_list (tail
->tokens
);
5395 delete_assertion (hp
);
5397 /* If a list of tokens was given, then delete any matching list. */
5402 struct tokenlist_list
*next
= tail
->next
;
5403 if (compare_token_lists (tail
->tokens
, tokens
)) {
5407 hp
->value
= tail
->next
;
5408 free_token_list (tail
->tokens
);
5421 /* Test whether there is an assertion named NAME
5422 and optionally whether it has an asserted token list TOKENS.
5423 NAME is not null terminated; its length is SYM_LENGTH.
5424 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
5427 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
5430 int tokens_specified
;
5431 struct arglist
*tokens
;
5433 ASSERTION_HASHNODE
*hp
;
5434 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
5436 if (pedantic
&& !instack
[indepth
].system_header_p
)
5437 pedwarn ("ANSI C does not allow testing assertions");
5439 hp
= assertion_lookup (name
, sym_length
, hashcode
);
5441 /* It is not an assertion; just return false. */
5444 /* If no token list was specified, then value is 1. */
5445 if (! tokens_specified
)
5449 struct tokenlist_list
*tail
;
5453 /* If a list of tokens was given,
5454 then succeed if the assertion records a matching list. */
5457 if (compare_token_lists (tail
->tokens
, tokens
))
5462 /* Fail if the assertion has no matching list. */
5467 /* Compare two lists of tokens for equality including order of tokens. */
5470 compare_token_lists (l1
, l2
)
5471 struct arglist
*l1
, *l2
;
5474 if (l1
->length
!= l2
->length
)
5476 if (strncmp (l1
->name
, l2
->name
, l1
->length
))
5482 /* Succeed if both lists end at the same time. */
5486 /* Read a space-separated list of tokens ending in a close parenthesis.
5487 Return a list of strings, in the order they were written.
5488 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
5489 Parse the text starting at *BPP, and update *BPP.
5490 Don't parse beyond LIMIT. */
5492 static struct arglist
*
5493 read_token_list (bpp
, limit
, error_flag
)
5498 struct arglist
*token_ptrs
= 0;
5504 /* Loop over the assertion value tokens. */
5506 struct arglist
*temp
;
5510 /* Find the end of the token. */
5514 } else if (*bp
== ')') {
5519 } else if (*bp
== '"' || *bp
== '\'')
5520 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
5522 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
5523 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
5526 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
5527 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
5528 bcopy (beg
, temp
->name
, bp
- beg
);
5529 temp
->name
[bp
- beg
] = 0;
5530 temp
->next
= token_ptrs
;
5532 temp
->length
= bp
- beg
;
5534 SKIP_WHITE_SPACE (bp
);
5537 error ("unterminated token sequence in `#assert' or `#unassert'");
5544 /* We accumulated the names in reverse order.
5545 Now reverse them to get the proper order. */
5547 register struct arglist
*prev
= 0, *this, *next
;
5548 for (this = token_ptrs
; this; this = next
) {
5558 free_token_list (tokens
)
5559 struct arglist
*tokens
;
5562 struct arglist
*next
= tokens
->next
;
5563 free (tokens
->name
);
5570 * Install a name in the assertion hash table.
5572 * If LEN is >= 0, it is the length of the name.
5573 * Otherwise, compute the length by scanning the entire name.
5575 * If HASH is >= 0, it is the precomputed hash code.
5576 * Otherwise, compute the hash code.
5578 static ASSERTION_HASHNODE
*
5579 assertion_install (name
, len
, hash
)
5584 register ASSERTION_HASHNODE
*hp
;
5585 register int i
, bucket
;
5586 register U_CHAR
*p
, *q
;
5588 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
5589 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
5591 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
5592 hp
->next
= assertion_hashtab
[bucket
];
5593 assertion_hashtab
[bucket
] = hp
;
5595 if (hp
->next
!= NULL
)
5596 hp
->next
->prev
= hp
;
5599 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
5602 for (i
= 0; i
< len
; i
++)
5609 * find the most recent hash node for name name (ending with first
5610 * non-identifier char) installed by install
5612 * If LEN is >= 0, it is the length of the name.
5613 * Otherwise, compute the length by scanning the entire name.
5615 * If HASH is >= 0, it is the precomputed hash code.
5616 * Otherwise, compute the hash code.
5618 static ASSERTION_HASHNODE
*
5619 assertion_lookup (name
, len
, hash
)
5624 register U_CHAR
*bp
;
5625 register ASSERTION_HASHNODE
*bucket
;
5627 bucket
= assertion_hashtab
[hash
];
5629 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
5631 bucket
= bucket
->next
;
5637 delete_assertion (hp
)
5638 ASSERTION_HASHNODE
*hp
;
5641 if (hp
->prev
!= NULL
)
5642 hp
->prev
->next
= hp
->next
;
5643 if (hp
->next
!= NULL
)
5644 hp
->next
->prev
= hp
->prev
;
5646 /* make sure that the bucket chain header that
5647 the deleted guy was on points to the right thing afterwards. */
5648 if (hp
== *hp
->bucket_hdr
)
5649 *hp
->bucket_hdr
= hp
->next
;
5655 * interpret #line command. Remembers previously seen fnames
5656 * in its very own hash table.
5658 #define FNAME_HASHSIZE 37
5661 do_line (buf
, limit
, op
, keyword
)
5662 U_CHAR
*buf
, *limit
;
5664 struct directive
*keyword
;
5666 register U_CHAR
*bp
;
5667 FILE_BUF
*ip
= &instack
[indepth
];
5670 enum file_change_code file_change
= same_file
;
5672 /* Expand any macros. */
5673 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
5675 /* Point to macroexpanded line, which is null-terminated now. */
5677 SKIP_WHITE_SPACE (bp
);
5679 if (!isdigit (*bp
)) {
5680 error ("invalid format `#line' command");
5684 /* The Newline at the end of this line remains to be processed.
5685 To put the next line at the specified line number,
5686 we must store a line number now that is one less. */
5687 new_lineno
= atoi (bp
) - 1;
5689 /* skip over the line number. */
5690 while (isdigit (*bp
))
5693 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
5694 if (*bp
&& !is_space
[*bp
]) {
5695 error ("invalid format `#line' command");
5700 SKIP_WHITE_SPACE (bp
);
5703 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
5704 HASHNODE
*hp
, **hash_bucket
;
5710 while (*bp
&& *bp
!= '\"')
5713 error ("invalid format `#line' command");
5717 fname_length
= bp
- fname
;
5720 SKIP_WHITE_SPACE (bp
);
5723 file_change
= enter_file
;
5724 else if (*bp
== '2')
5725 file_change
= leave_file
;
5726 else if (*bp
== '3')
5727 ip
->system_header_p
= 1;
5729 error ("invalid format `#line' command");
5734 SKIP_WHITE_SPACE (bp
);
5736 ip
->system_header_p
= 1;
5738 SKIP_WHITE_SPACE (bp
);
5741 error ("invalid format `#line' command");
5747 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
5748 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
5749 if (hp
->length
== fname_length
&&
5750 strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
5751 ip
->nominal_fname
= hp
->value
.cpval
;
5755 /* Didn't find it; cons up a new one. */
5756 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
5757 hp
->next
= *hash_bucket
;
5760 hp
->length
= fname_length
;
5761 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
5762 bcopy (fname
, hp
->value
.cpval
, fname_length
);
5765 error ("invalid format `#line' command");
5769 ip
->lineno
= new_lineno
;
5770 output_line_command (ip
, op
, 0, file_change
);
5771 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
5776 * remove the definition of a symbol from the symbol table.
5777 * according to un*x /lib/cpp, it is not an error to undef
5778 * something that has no definitions, so it isn't one here either.
5782 do_undef (buf
, limit
, op
, keyword
)
5783 U_CHAR
*buf
, *limit
;
5785 struct directive
*keyword
;
5789 U_CHAR
*orig_buf
= buf
;
5791 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
5792 if (pcp_outfile
&& op
)
5793 pass_thru_directive (buf
, limit
, op
, keyword
);
5795 SKIP_WHITE_SPACE (buf
);
5796 sym_length
= check_macro_name (buf
, "macro");
5798 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
5799 /* If we are generating additional info for debugging (with -g) we
5800 need to pass through all effective #undef commands. */
5801 if (debug_output
&& op
)
5802 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
5803 if (hp
->type
!= T_MACRO
)
5804 warning ("undefining `%s'", hp
->name
);
5810 SKIP_WHITE_SPACE (buf
);
5812 pedwarn ("garbage after `#undef' directive");
5818 * Report a fatal error detected by the program we are processing.
5819 * Use the text of the line in the error message, then terminate.
5820 * (We use error() because it prints the filename & line#.)
5824 do_error (buf
, limit
, op
, keyword
)
5825 U_CHAR
*buf
, *limit
;
5827 struct directive
*keyword
;
5829 int length
= limit
- buf
;
5830 char *copy
= (char *) xmalloc (length
+ 1);
5831 bcopy (buf
, copy
, length
);
5833 SKIP_WHITE_SPACE (copy
);
5834 error ("#error %s", copy
);
5835 exit (FAILURE_EXIT_CODE
);
5841 * Report a warning detected by the program we are processing.
5842 * Use the text of the line in the warning message, then continue.
5843 * (We use error() because it prints the filename & line#.)
5847 do_warning (buf
, limit
, op
, keyword
)
5848 U_CHAR
*buf
, *limit
;
5850 struct directive
*keyword
;
5852 int length
= limit
- buf
;
5853 char *copy
= (char *) xmalloc (length
+ 1);
5854 bcopy (buf
, copy
, length
);
5856 SKIP_WHITE_SPACE (copy
);
5857 warning ("#warning %s", copy
);
5861 /* Remember the name of the current file being read from so that we can
5862 avoid ever including it again. */
5868 FILE_BUF
*ip
= NULL
;
5870 for (i
= indepth
; i
>= 0; i
--)
5871 if (instack
[i
].fname
!= NULL
) {
5877 struct file_name_list
*new;
5879 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
5880 new->next
= dont_repeat_files
;
5881 dont_repeat_files
= new;
5882 new->fname
= savestring (ip
->fname
);
5883 new->control_macro
= 0;
5888 /* #ident has already been copied to the output file, so just ignore it. */
5891 do_ident (buf
, limit
)
5892 U_CHAR
*buf
, *limit
;
5894 /* Allow #ident in system headers, since that's not user's fault. */
5895 if (pedantic
&& !instack
[indepth
].system_header_p
)
5896 pedwarn ("ANSI C does not allow `#ident'");
5900 /* #pragma and its argument line have already been copied to the output file.
5901 Just check for some recognized pragmas that need validation here. */
5904 do_pragma (buf
, limit
)
5905 U_CHAR
*buf
, *limit
;
5907 while (*buf
== ' ' || *buf
== '\t')
5909 if (!strncmp (buf
, "once", 4)) {
5910 /* Allow #pragma once in system headers, since that's not the user's
5912 if (!instack
[indepth
].system_header_p
)
5913 warning ("`#pragma once' is obsolete");
5917 if (!strncmp (buf
, "implementation", 14)) {
5918 /* Be quiet about `#pragma implementation' for a file only if it hasn't
5919 been included yet. */
5920 struct file_name_list
*ptr
;
5921 U_CHAR
*p
= buf
+ 14, *fname
, *inc_fname
;
5922 SKIP_WHITE_SPACE (p
);
5923 if (*p
== '\n' || *p
!= '\"')
5927 if (p
= (U_CHAR
*) strchr (fname
, '\"'))
5930 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
5931 inc_fname
= (U_CHAR
*) rindex (ptr
->fname
, '/');
5932 inc_fname
= inc_fname
? inc_fname
+ 1 : (U_CHAR
*) ptr
->fname
;
5933 if (inc_fname
&& !strcmp (inc_fname
, fname
))
5934 warning ("`#pragma implementation' for `%s' appears after file is included",
5943 /* This was a fun hack, but #pragma seems to start to be useful.
5944 By failing to recognize it, we pass it through unchanged to cc1. */
5947 * the behavior of the #pragma directive is implementation defined.
5948 * this implementation defines it as follows.
5955 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
5958 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
5960 execl ("/usr/games/hack", "#pragma", 0);
5961 execl ("/usr/games/rogue", "#pragma", 0);
5962 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
5963 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
5965 fatal ("You are in a maze of twisty compiler features, all different");
5969 /* Just ignore #sccs, on systems where we define it at all. */
5975 pedwarn ("ANSI C does not allow `#sccs'");
5980 * handle #if command by
5981 * 1) inserting special `defined' keyword into the hash table
5982 * that gets turned into 0 or 1 by special_symbol (thus,
5983 * if the luser has a symbol called `defined' already, it won't
5984 * work inside the #if command)
5985 * 2) rescan the input into a temporary output buffer
5986 * 3) pass the output buffer to the yacc parser and collect a value
5987 * 4) clean up the mess left from steps 1 and 2.
5988 * 5) call conditional_skip to skip til the next #endif (etc.),
5989 * or not, depending on the value from step 3.
5993 do_if (buf
, limit
, op
, keyword
)
5994 U_CHAR
*buf
, *limit
;
5996 struct directive
*keyword
;
5999 FILE_BUF
*ip
= &instack
[indepth
];
6001 value
= eval_if_expression (buf
, limit
- buf
);
6002 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
);
6007 * handle a #elif directive by not changing if_stack either.
6008 * see the comment above do_else.
6012 do_elif (buf
, limit
, op
, keyword
)
6013 U_CHAR
*buf
, *limit
;
6015 struct directive
*keyword
;
6018 FILE_BUF
*ip
= &instack
[indepth
];
6020 if (if_stack
== instack
[indepth
].if_stack
) {
6021 error ("`#elif' not within a conditional");
6024 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
6025 error ("`#elif' after `#else'");
6026 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
6027 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
6028 strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
6029 fprintf (stderr
, ", file %s", if_stack
->fname
);
6030 fprintf (stderr
, ")\n");
6032 if_stack
->type
= T_ELIF
;
6035 if (if_stack
->if_succeeded
)
6036 skip_if_group (ip
, 0);
6038 value
= eval_if_expression (buf
, limit
- buf
);
6040 skip_if_group (ip
, 0);
6042 ++if_stack
->if_succeeded
; /* continue processing input */
6043 output_line_command (ip
, op
, 1, same_file
);
6050 * evaluate a #if expression in BUF, of length LENGTH,
6051 * then parse the result as a C expression and return the value as an int.
6054 eval_if_expression (buf
, length
)
6059 HASHNODE
*save_defined
;
6062 save_defined
= install ("defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
6064 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
6066 delete_macro (save_defined
); /* clean up special symbol */
6068 value
= parse_c_expression (temp_obuf
.buf
);
6070 free (temp_obuf
.buf
);
6076 * routine to handle ifdef/ifndef. Try to look up the symbol,
6077 * then do or don't skip to the #endif/#else/#elif depending
6078 * on what directive is actually being processed.
6082 do_xifdef (buf
, limit
, op
, keyword
)
6083 U_CHAR
*buf
, *limit
;
6085 struct directive
*keyword
;
6088 FILE_BUF
*ip
= &instack
[indepth
];
6090 int start_of_file
= 0;
6091 U_CHAR
*control_macro
= 0;
6093 /* Detect a #ifndef at start of file (not counting comments). */
6094 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
6095 U_CHAR
*p
= ip
->buf
;
6096 while (p
!= directive_start
) {
6100 else if (c
== '/' && p
!= ip
->bufp
&& *p
== '*') {
6101 /* Skip this comment. */
6103 U_CHAR
*save_bufp
= ip
->bufp
;
6105 p
= skip_to_end_of_comment (ip
, &junk
, 1);
6106 ip
->bufp
= save_bufp
;
6111 /* If we get here, this conditional is the beginning of the file. */
6116 /* Discard leading and trailing whitespace. */
6117 SKIP_WHITE_SPACE (buf
);
6118 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
6120 /* Find the end of the identifier at the beginning. */
6121 for (end
= buf
; is_idchar
[*end
]; end
++);
6124 skip
= (keyword
->type
== T_IFDEF
);
6126 pedwarn (end
== limit
? "`#%s' with no argument"
6127 : "`#%s' argument starts with punctuation",
6132 if (pedantic
&& buf
[0] >= '0' && buf
[0] <= '9')
6133 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
6134 else if (end
!= limit
&& !traditional
)
6135 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
6137 hp
= lookup (buf
, end
-buf
, -1);
6140 /* Output a precondition for this macro. */
6141 if (hp
&& hp
->value
.defn
->predefined
)
6142 fprintf(pcp_outfile
, "#define %s\n", hp
->name
);
6145 fprintf(pcp_outfile
, "#undef ");
6146 while (is_idchar
[*cp
]) /* Ick! */
6147 fputc (*cp
++, pcp_outfile
);
6148 putc ('\n', pcp_outfile
);
6152 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
6153 if (start_of_file
&& !skip
) {
6154 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
6155 bcopy (buf
, control_macro
, end
- buf
);
6156 control_macro
[end
- buf
] = 0;
6160 conditional_skip (ip
, skip
, T_IF
, control_macro
);
6164 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
6165 If this is a #ifndef starting at the beginning of a file,
6166 CONTROL_MACRO is the macro name tested by the #ifndef.
6167 Otherwise, CONTROL_MACRO is 0. */
6170 conditional_skip (ip
, skip
, type
, control_macro
)
6173 enum node_type type
;
6174 U_CHAR
*control_macro
;
6176 IF_STACK_FRAME
*temp
;
6178 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
6179 temp
->fname
= ip
->nominal_fname
;
6180 temp
->lineno
= ip
->lineno
;
6181 temp
->next
= if_stack
;
6182 temp
->control_macro
= control_macro
;
6185 if_stack
->type
= type
;
6188 skip_if_group (ip
, 0);
6191 ++if_stack
->if_succeeded
;
6192 output_line_command (ip
, &outbuf
, 1, same_file
);
6197 * skip to #endif, #else, or #elif. adjust line numbers, etc.
6198 * leaves input ptr at the sharp sign found.
6199 * If ANY is nonzero, return at next directive of any sort.
6202 skip_if_group (ip
, any
)
6206 register U_CHAR
*bp
= ip
->bufp
, *cp
;
6207 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
6208 struct directive
*kt
;
6209 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
6210 U_CHAR
*beg_of_line
= bp
;
6211 register int ident_length
;
6212 U_CHAR
*ident
, *after_ident
;
6216 case '/': /* possible comment */
6217 if (*bp
== '\\' && bp
[1] == '\n')
6220 || (cplusplus_comments
&& *bp
== '/')) {
6222 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
6227 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
6228 NULL_PTR
, NULL_PTR
);
6231 /* Char after backslash loses its special meaning. */
6234 ++ip
->lineno
; /* But do update the line-count. */
6245 /* # keyword: a # must be first nonblank char on the line */
6246 if (beg_of_line
== 0)
6248 /* Scan from start of line, skipping whitespace, comments
6249 and backslash-newlines, and see if we reach this #.
6250 If not, this # is not special. */
6253 if (is_hor_space
[*bp
])
6255 else if (*bp
== '\\' && bp
[1] == '\n')
6257 else if (*bp
== '/' && bp
[1] == '*') {
6259 while (!(*bp
== '*' && bp
[1] == '/'))
6262 } else if (cplusplus_comments
&& *bp
== '/' && bp
[1] == '/') {
6264 while (*bp
++ != '\n') ;
6268 if (bp
!= ip
->bufp
) {
6269 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
6273 bp
= ip
->bufp
+ 1; /* Point after the '#' */
6275 /* Skip whitespace and \-newline. */
6277 if (is_hor_space
[*bp
])
6279 else if (*bp
== '\\' && bp
[1] == '\n')
6281 else if (*bp
== '/' && bp
[1] == '*') {
6283 while (!(*bp
== '*' && bp
[1] == '/')) {
6289 } else if (cplusplus_comments
&& *bp
== '/' && bp
[1] == '/') {
6291 while (*bp
++ != '\n') ;
6298 /* Now find end of directive name.
6299 If we encounter a backslash-newline, exchange it with any following
6300 symbol-constituents so that we end up with a contiguous name. */
6306 if (*bp
== '\\' && bp
[1] == '\n')
6307 name_newline_fix (bp
);
6313 ident_length
= bp
- cp
;
6317 /* A line of just `#' becomes blank. */
6319 if (ident_length
== 0 && *after_ident
== '\n') {
6323 if (ident_length
== 0 || !is_idstart
[*ident
]) {
6325 while (is_idchar
[*p
]) {
6326 if (*p
< '0' || *p
> '9')
6330 /* Handle # followed by a line number. */
6331 if (p
!= ident
&& !is_idchar
[*p
]) {
6333 pedwarn ("`#' followed by integer");
6337 /* Avoid error for `###' and similar cases unless -pedantic. */
6339 while (*p
== '#' || is_hor_space
[*p
]) p
++;
6341 if (pedantic
&& !lang_asm
)
6342 pedwarn ("invalid preprocessor directive");
6347 if (!lang_asm
&& pedantic
)
6348 pedwarn ("invalid preprocessor directive name");
6352 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
6353 IF_STACK_FRAME
*temp
;
6354 if (ident_length
== kt
->length
6355 && strncmp (cp
, kt
->name
, kt
->length
) == 0) {
6356 /* If we are asked to return on next directive, do so now. */
6364 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
6365 temp
->next
= if_stack
;
6367 temp
->lineno
= ip
->lineno
;
6368 temp
->fname
= ip
->nominal_fname
;
6369 temp
->type
= kt
->type
;
6373 if (pedantic
&& if_stack
!= save_if_stack
)
6376 if (if_stack
== instack
[indepth
].if_stack
) {
6377 error ("`#%s' not within a conditional", kt
->name
);
6380 else if (if_stack
== save_if_stack
)
6381 return; /* found what we came for */
6383 if (kt
->type
!= T_ENDIF
) {
6384 if (if_stack
->type
== T_ELSE
)
6385 error ("`#else' or `#elif' after `#else'");
6386 if_stack
->type
= kt
->type
;
6391 if_stack
= if_stack
->next
;
6398 /* Don't let erroneous code go by. */
6399 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
6400 pedwarn ("invalid preprocessor directive name");
6404 /* after this returns, rescan will exit because ip->bufp
6405 now points to the end of the buffer.
6406 rescan is responsible for the error message also. */
6410 * handle a #else directive. Do this by just continuing processing
6411 * without changing if_stack ; this is so that the error message
6412 * for missing #endif's etc. will point to the original #if. It
6413 * is possible that something different would be better.
6417 do_else (buf
, limit
, op
, keyword
)
6418 U_CHAR
*buf
, *limit
;
6420 struct directive
*keyword
;
6422 FILE_BUF
*ip
= &instack
[indepth
];
6425 SKIP_WHITE_SPACE (buf
);
6427 pedwarn ("text following `#else' violates ANSI standard");
6430 if (if_stack
== instack
[indepth
].if_stack
) {
6431 error ("`#else' not within a conditional");
6434 /* #ifndef can't have its special treatment for containing the whole file
6435 if it has a #else clause. */
6436 if_stack
->control_macro
= 0;
6438 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
6439 error ("`#else' after `#else'");
6440 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
6441 if (strcmp (if_stack
->fname
, ip
->nominal_fname
) != 0)
6442 fprintf (stderr
, ", file %s", if_stack
->fname
);
6443 fprintf (stderr
, ")\n");
6445 if_stack
->type
= T_ELSE
;
6448 if (if_stack
->if_succeeded
)
6449 skip_if_group (ip
, 0);
6451 ++if_stack
->if_succeeded
; /* continue processing input */
6452 output_line_command (ip
, op
, 1, same_file
);
6458 * unstack after #endif command
6462 do_endif (buf
, limit
, op
, keyword
)
6463 U_CHAR
*buf
, *limit
;
6465 struct directive
*keyword
;
6468 SKIP_WHITE_SPACE (buf
);
6470 pedwarn ("text following `#endif' violates ANSI standard");
6473 if (if_stack
== instack
[indepth
].if_stack
)
6474 error ("unbalanced `#endif'");
6476 IF_STACK_FRAME
*temp
= if_stack
;
6477 if_stack
= if_stack
->next
;
6478 if (temp
->control_macro
!= 0) {
6479 /* This #endif matched a #ifndef at the start of the file.
6480 See if it is at the end of the file. */
6481 FILE_BUF
*ip
= &instack
[indepth
];
6482 U_CHAR
*p
= ip
->bufp
;
6483 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
6493 if (p
!= ep
&& *p
== '*') {
6494 /* Skip this comment. */
6496 U_CHAR
*save_bufp
= ip
->bufp
;
6498 p
= skip_to_end_of_comment (ip
, &junk
, 1);
6499 ip
->bufp
= save_bufp
;
6506 /* If we get here, this #endif ends a #ifndef
6507 that contains all of the file (aside from whitespace).
6508 Arrange not to include the file again
6509 if the macro that was tested is defined. */
6511 record_control_macro (ip
->fname
, temp
->control_macro
);
6515 output_line_command (&instack
[indepth
], op
, 1, same_file
);
6520 /* When an #else or #endif is found while skipping failed conditional,
6521 if -pedantic was specified, this is called to warn about text after
6522 the command name. P points to the first char after the command name. */
6528 /* Advance P over whitespace and comments. */
6530 if (*p
== '\\' && p
[1] == '\n')
6532 if (is_hor_space
[*p
])
6534 else if (*p
== '/') {
6535 if (p
[1] == '\\' && p
[2] == '\n')
6536 newline_fix (p
+ 1);
6539 /* Don't bother warning about unterminated comments
6540 since that will happen later. Just be sure to exit. */
6542 if (p
[1] == '\\' && p
[2] == '\n')
6543 newline_fix (p
+ 1);
6544 if (*p
== '*' && p
[1] == '/') {
6551 else if (cplusplus_comments
&& p
[1] == '/') {
6553 while (*p
&& *p
++ != '\n') ;
6557 if (*p
&& *p
!= '\n')
6558 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
6561 /* Skip a comment, assuming the input ptr immediately follows the
6562 initial slash-star. Bump *LINE_COUNTER for each newline.
6563 (The canonical line counter is &ip->lineno.)
6564 Don't use this routine (or the next one) if bumping the line
6565 counter is not sufficient to deal with newlines in the string.
6567 If NOWARN is nonzero, don't warn about slash-star inside a comment.
6568 This feature is useful when processing a comment that is going to be
6569 processed or was processed at another point in the preprocessor,
6570 to avoid a duplicate warning. */
6572 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
6573 register FILE_BUF
*ip
;
6574 int *line_counter
; /* place to remember newlines, or NULL */
6577 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
6578 register U_CHAR
*bp
= ip
->bufp
;
6579 FILE_BUF
*op
= &outbuf
; /* JF */
6580 int output
= put_out_comments
&& !line_counter
;
6582 /* JF this line_counter stuff is a crock to make sure the
6583 comment is only put out once, no matter how many times
6584 the comment is skipped. It almost works */
6589 if (cplusplus_comments
&& bp
[-1] == '/') {
6592 if ((*op
->bufp
++ = *bp
++) == '\n') {
6600 while (bp
< limit
) {
6601 if (*bp
++ == '\n') {
6610 while (bp
< limit
) {
6615 if (warn_comments
&& !nowarn
&& bp
< limit
&& *bp
== '*')
6616 warning ("`/*' within comment");
6619 if (line_counter
!= NULL
)
6625 if (*bp
== '\\' && bp
[1] == '\n')
6641 * Skip over a quoted string. BP points to the opening quote.
6642 * Returns a pointer after the closing quote. Don't go past LIMIT.
6643 * START_LINE is the line number of the starting point (but it need
6644 * not be valid if the starting point is inside a macro expansion).
6646 * The input stack state is not changed.
6648 * If COUNT_NEWLINES is nonzero, it points to an int to increment
6649 * for each newline passed.
6651 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
6652 * if we pass a backslash-newline.
6654 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
6657 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
6658 register U_CHAR
*bp
;
6659 register U_CHAR
*limit
;
6661 int *count_newlines
;
6662 int *backslash_newlines_p
;
6665 register U_CHAR c
, match
;
6670 error_with_line (line_for_error (start_line
),
6671 "unterminated string or character constant");
6678 while (*bp
== '\\' && bp
[1] == '\n') {
6679 if (backslash_newlines_p
)
6680 *backslash_newlines_p
= 1;
6685 if (*bp
== '\n' && count_newlines
) {
6686 if (backslash_newlines_p
)
6687 *backslash_newlines_p
= 1;
6691 } else if (c
== '\n') {
6693 /* Unterminated strings and character constants are 'legal'. */
6694 bp
--; /* Don't consume the newline. */
6699 if (match
== '\'') {
6700 error_with_line (line_for_error (start_line
),
6701 "unterminated character constant");
6707 if (traditional
) { /* Unterminated strings are 'legal'. */
6712 /* If not traditional, then allow newlines inside strings. */
6715 } else if (c
== match
)
6721 /* Skip across a group of balanced parens, starting from IP->bufp.
6722 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
6724 This does not handle newlines, because it's used for the arg of #if,
6725 where there aren't any newlines. Also, backslash-newline can't appear. */
6728 skip_paren_group (ip
)
6729 register FILE_BUF
*ip
;
6731 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
6732 U_CHAR
*p
= ip
->bufp
;
6734 int lines_dummy
= 0;
6736 while (p
!= limit
) {
6746 return ip
->bufp
= p
;
6752 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
6760 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6762 return ip
->bufp
= p
;
6773 * write out a #line command, for instance, after an #include file.
6774 * If CONDITIONAL is nonzero, we can omit the #line if it would
6775 * appear to be a no-op, and we can output a few newlines instead
6776 * if we want to increase the line number by a small amount.
6777 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
6781 output_line_command (ip
, op
, conditional
, file_change
)
6784 enum file_change_code file_change
;
6787 char line_cmd_buf
[500];
6789 if (no_line_commands
6790 || ip
->fname
== NULL
6792 op
->lineno
= ip
->lineno
;
6797 if (ip
->lineno
== op
->lineno
)
6800 /* If the inherited line number is a little too small,
6801 output some newlines instead of a #line command. */
6802 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
6803 check_expand (op
, 10);
6804 while (ip
->lineno
> op
->lineno
) {
6812 /* Don't output a line number of 0 if we can help it. */
6813 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
6814 && *ip
->bufp
== '\n') {
6819 #ifdef OUTPUT_LINE_COMMANDS
6820 sprintf (line_cmd_buf
, "#line %d \"%s\"", ip
->lineno
, ip
->nominal_fname
);
6822 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->nominal_fname
);
6824 if (file_change
!= same_file
)
6825 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
6826 /* Tell cc1 if following text comes from a system header file. */
6827 if (ip
->system_header_p
)
6828 strcat (line_cmd_buf
, " 3");
6829 len
= strlen (line_cmd_buf
);
6830 line_cmd_buf
[len
++] = '\n';
6831 check_expand (op
, len
+ 1);
6832 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
6834 bcopy (line_cmd_buf
, op
->bufp
, len
);
6836 op
->lineno
= ip
->lineno
;
6839 /* This structure represents one parsed argument in a macro call.
6840 `raw' points to the argument text as written (`raw_length' is its length).
6841 `expanded' points to the argument's macro-expansion
6842 (its length is `expand_length').
6843 `stringified_length' is the length the argument would have
6845 `use_count' is the number of times this macro arg is substituted
6846 into the macro. If the actual use count exceeds 10,
6847 the value stored is 10.
6848 `free1' and `free2', if nonzero, point to blocks to be freed
6849 when the macro argument data is no longer needed. */
6852 U_CHAR
*raw
, *expanded
;
6853 int raw_length
, expand_length
;
6854 int stringified_length
;
6855 U_CHAR
*free1
, *free2
;
6861 /* Expand a macro call.
6862 HP points to the symbol that is the macro being called.
6863 Put the result of expansion onto the input stack
6864 so that subsequent input by our caller will use it.
6866 If macro wants arguments, caller has already verified that
6867 an argument list follows; arguments come from the input stack. */
6870 macroexpand (hp
, op
)
6875 DEFINITION
*defn
= hp
->value
.defn
;
6876 register U_CHAR
*xbuf
;
6878 int start_line
= instack
[indepth
].lineno
;
6879 int rest_args
, rest_zero
;
6881 CHECK_DEPTH (return;);
6883 /* it might not actually be a macro. */
6884 if (hp
->type
!= T_MACRO
) {
6885 special_symbol (hp
, op
);
6889 /* This macro is being used inside a #if, which means it must be */
6890 /* recorded as a precondition. */
6891 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
6892 dump_single_macro (hp
, pcp_outfile
);
6894 nargs
= defn
->nargs
;
6898 struct argdata
*args
;
6899 char *parse_error
= 0;
6901 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
6903 for (i
= 0; i
< nargs
; i
++) {
6904 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
6905 args
[i
].raw_length
= args
[i
].expand_length
6906 = args
[i
].stringified_length
= 0;
6907 args
[i
].free1
= args
[i
].free2
= 0;
6908 args
[i
].use_count
= 0;
6911 /* Parse all the macro args that are supplied. I counts them.
6912 The first NARGS args are stored in ARGS.
6913 The rest are discarded.
6914 If rest_args is set then we assume macarg absorbed the rest of the args.
6919 /* Discard the open-parenthesis or comma before the next arg. */
6920 ++instack
[indepth
].bufp
;
6923 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
6924 /* if we are working on last arg which absorbs rest of args... */
6925 if (i
== nargs
- 1 && defn
->rest_args
)
6927 parse_error
= macarg (&args
[i
], rest_args
);
6930 parse_error
= macarg (NULL_PTR
, 0);
6932 error_with_line (line_for_error (start_line
), parse_error
);
6936 } while (*instack
[indepth
].bufp
!= ')');
6938 /* If we got one arg but it was just whitespace, call that 0 args. */
6940 register U_CHAR
*bp
= args
[0].raw
;
6941 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
6942 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
6947 /* Don't output an error message if we have already output one for
6948 a parse error above. */
6950 if (nargs
== 0 && i
> 0) {
6952 error ("arguments given to macro `%s'", hp
->name
);
6953 } else if (i
< nargs
) {
6954 /* traditional C allows foo() if foo wants one argument. */
6955 if (nargs
== 1 && i
== 0 && traditional
)
6957 /* the rest args token is allowed to absorb 0 tokens */
6958 else if (i
== nargs
- 1 && defn
->rest_args
)
6960 else if (parse_error
)
6963 error ("macro `%s' used without args", hp
->name
);
6965 error ("macro `%s' used with just one arg", hp
->name
);
6967 error ("macro `%s' used with only %d args", hp
->name
, i
);
6968 } else if (i
> nargs
) {
6970 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
6973 /* Swallow the closeparen. */
6974 ++instack
[indepth
].bufp
;
6976 /* If macro wants zero args, we parsed the arglist for checking only.
6977 Read directly from the macro definition. */
6979 xbuf
= defn
->expansion
;
6980 xbuf_len
= defn
->length
;
6982 register U_CHAR
*exp
= defn
->expansion
;
6983 register int offset
; /* offset in expansion,
6984 copied a piece at a time */
6985 register int totlen
; /* total amount of exp buffer filled so far */
6987 register struct reflist
*ap
, *last_ap
;
6989 /* Macro really takes args. Compute the expansion of this call. */
6991 /* Compute length in characters of the macro's expansion.
6992 Also count number of times each arg is used. */
6993 xbuf_len
= defn
->length
;
6994 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
6996 xbuf_len
+= args
[ap
->argno
].stringified_length
;
6997 else if (ap
->raw_before
|| ap
->raw_after
|| traditional
)
6998 xbuf_len
+= args
[ap
->argno
].raw_length
;
7000 xbuf_len
+= args
[ap
->argno
].expand_length
;
7002 if (args
[ap
->argno
].use_count
< 10)
7003 args
[ap
->argno
].use_count
++;
7006 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
7008 /* Generate in XBUF the complete expansion
7009 with arguments substituted in.
7010 TOTLEN is the total size generated so far.
7011 OFFSET is the index in the definition
7012 of where we are copying from. */
7013 offset
= totlen
= 0;
7014 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
7015 last_ap
= ap
, ap
= ap
->next
) {
7016 register struct argdata
*arg
= &args
[ap
->argno
];
7018 /* add chars to XBUF unless rest_args was zero with concatenation */
7019 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
7020 if (! (rest_zero
&& ((ap
->rest_args
&& ap
->raw_before
)
7021 || (last_ap
!= NULL
&& last_ap
->rest_args
7022 && last_ap
->raw_after
))))
7023 xbuf
[totlen
++] = exp
[offset
];
7025 if (ap
->stringify
!= 0) {
7026 int arglen
= arg
->raw_length
;
7032 && (c
= arg
->raw
[i
], is_space
[c
]))
7035 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
7038 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
7039 for (; i
< arglen
; i
++) {
7042 /* Special markers Newline Space
7043 generate nothing for a stringified argument. */
7044 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
7049 /* Internal sequences of whitespace are replaced by one space
7050 except within an string or char token. */
7052 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
])) {
7054 /* Note that Newline Space does occur within whitespace
7055 sequences; consider it part of the sequence. */
7056 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
7058 else if (c
!= '\n' && is_space
[c
])
7075 } else if (c
== '\"' || c
== '\'')
7079 /* Escape these chars */
7080 if (c
== '\"' || (in_string
&& c
== '\\'))
7081 xbuf
[totlen
++] = '\\';
7085 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
7090 xbuf
[totlen
++] = '\"'; /* insert ending quote */
7091 } else if (ap
->raw_before
|| ap
->raw_after
|| traditional
) {
7092 U_CHAR
*p1
= arg
->raw
;
7093 U_CHAR
*l1
= p1
+ arg
->raw_length
;
7094 if (ap
->raw_before
) {
7095 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
7096 while (p1
!= l1
&& is_idchar
[*p1
])
7097 xbuf
[totlen
++] = *p1
++;
7098 /* Delete any no-reexpansion marker that follows
7099 an identifier at the beginning of the argument
7100 if the argument is concatenated with what precedes it. */
7101 if (p1
[0] == '\n' && p1
[1] == '-')
7104 if (ap
->raw_after
) {
7105 /* Arg is concatenated after: delete trailing whitespace,
7106 whitespace markers, and no-reexpansion markers. */
7108 if (is_space
[l1
[-1]]) l1
--;
7109 else if (l1
[-1] == '-') {
7110 U_CHAR
*p2
= l1
- 1;
7111 /* If a `-' is preceded by an odd number of newlines then it
7112 and the last newline are a no-reexpansion marker. */
7113 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
7114 if ((l1
- 1 - p2
) & 1) {
7122 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
7125 bcopy (arg
->expanded
, xbuf
+ totlen
, arg
->expand_length
);
7126 totlen
+= arg
->expand_length
;
7127 /* If a macro argument with newlines is used multiple times,
7128 then only expand the newlines once. This avoids creating output
7129 lines which don't correspond to any input line, which confuses
7131 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
7132 /* Don't bother doing delete_newlines for subsequent
7136 = delete_newlines (arg
->expanded
, arg
->expand_length
);
7140 if (totlen
> xbuf_len
)
7144 /* if there is anything left of the definition
7145 after handling the arg list, copy that in too. */
7147 for (i
= offset
; i
< defn
->length
; i
++) {
7148 /* if we've reached the end of the macro */
7151 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
7152 && last_ap
->raw_after
))
7153 xbuf
[totlen
++] = exp
[i
];
7159 for (i
= 0; i
< nargs
; i
++) {
7160 if (args
[i
].free1
!= 0)
7161 free (args
[i
].free1
);
7162 if (args
[i
].free2
!= 0)
7163 free (args
[i
].free2
);
7167 xbuf
= defn
->expansion
;
7168 xbuf_len
= defn
->length
;
7171 /* Now put the expansion on the input stack
7172 so our caller will commence reading from it. */
7174 register FILE_BUF
*ip2
;
7176 ip2
= &instack
[++indepth
];
7179 ip2
->nominal_fname
= 0;
7182 ip2
->length
= xbuf_len
;
7184 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
7186 ip2
->if_stack
= if_stack
;
7187 ip2
->system_header_p
= 0;
7189 /* Recursive macro use sometimes works traditionally.
7190 #define foo(x,y) bar(x(y,0), y)
7194 hp
->type
= T_DISABLED
;
7199 * Parse a macro argument and store the info on it into *ARGPTR.
7200 * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
7201 * Return nonzero to indicate a syntax error.
7205 macarg (argptr
, rest_args
)
7206 register struct argdata
*argptr
;
7209 FILE_BUF
*ip
= &instack
[indepth
];
7214 /* Try to parse as much of the argument as exists at this
7215 input stack level. */
7216 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
7217 &paren
, &newlines
, &comments
, rest_args
);
7219 /* If we find the end of the argument at this level,
7220 set up *ARGPTR to point at it in the input stack. */
7221 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
7222 && bp
!= ip
->buf
+ ip
->length
) {
7224 argptr
->raw
= ip
->bufp
;
7225 argptr
->raw_length
= bp
- ip
->bufp
;
7226 argptr
->newlines
= newlines
;
7230 /* This input stack level ends before the macro argument does.
7231 We must pop levels and keep parsing.
7232 Therefore, we must allocate a temporary buffer and copy
7233 the macro argument into it. */
7234 int bufsize
= bp
- ip
->bufp
;
7235 int extra
= newlines
;
7236 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
7237 int final_start
= 0;
7239 bcopy (ip
->bufp
, buffer
, bufsize
);
7241 ip
->lineno
+= newlines
;
7243 while (bp
== ip
->buf
+ ip
->length
) {
7244 if (instack
[indepth
].macro
== 0) {
7246 return "unterminated macro call";
7248 ip
->macro
->type
= T_MACRO
;
7250 free (ip
->free_ptr
);
7251 ip
= &instack
[--indepth
];
7254 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
7255 &newlines
, &comments
, rest_args
);
7256 final_start
= bufsize
;
7257 bufsize
+= bp
- ip
->bufp
;
7259 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
7260 bcopy (ip
->bufp
, buffer
+ bufsize
- (bp
- ip
->bufp
), bp
- ip
->bufp
);
7262 ip
->lineno
+= newlines
;
7265 /* Now, if arg is actually wanted, record its raw form,
7266 discarding comments and duplicating newlines in whatever
7267 part of it did not come from a macro expansion.
7268 EXTRA space has been preallocated for duplicating the newlines.
7269 FINAL_START is the index of the start of that part. */
7271 argptr
->raw
= buffer
;
7272 argptr
->raw_length
= bufsize
;
7273 argptr
->free1
= buffer
;
7274 argptr
->newlines
= newlines
;
7275 argptr
->comments
= comments
;
7276 if ((newlines
|| comments
) && ip
->fname
!= 0)
7279 discard_comments (argptr
->raw
+ final_start
,
7280 argptr
->raw_length
- final_start
,
7282 argptr
->raw
[argptr
->raw_length
] = 0;
7283 if (argptr
->raw_length
> bufsize
+ extra
)
7288 /* If we are not discarding this argument,
7289 macroexpand it and compute its length as stringified.
7290 All this info goes into *ARGPTR. */
7294 register U_CHAR
*buf
, *lim
;
7295 register int totlen
;
7297 obuf
= expand_to_temp_buffer (argptr
->raw
,
7298 argptr
->raw
+ argptr
->raw_length
,
7301 argptr
->expanded
= obuf
.buf
;
7302 argptr
->expand_length
= obuf
.length
;
7303 argptr
->free2
= obuf
.buf
;
7306 lim
= buf
+ argptr
->raw_length
;
7308 while (buf
!= lim
&& is_space
[*buf
])
7310 while (buf
!= lim
&& is_space
[lim
[-1]])
7312 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
7313 while (buf
!= lim
) {
7314 register U_CHAR c
= *buf
++;
7316 /* Internal sequences of whitespace are replaced by one space
7317 in most cases, but not always. So count all the whitespace
7318 in case we need to keep it all. */
7321 SKIP_ALL_WHITE_SPACE (buf
);
7324 if (c
== '\"' || c
== '\\') /* escape these chars */
7326 else if (!isprint (c
))
7329 argptr
->stringified_length
= totlen
;
7334 /* Scan text from START (inclusive) up to LIMIT (exclusive),
7335 counting parens in *DEPTHPTR,
7336 and return if reach LIMIT
7337 or before a `)' that would make *DEPTHPTR negative
7338 or before a comma when *DEPTHPTR is zero.
7339 Single and double quotes are matched and termination
7340 is inhibited within them. Comments also inhibit it.
7341 Value returned is pointer to stopping place.
7343 Increment *NEWLINES each time a newline is passed.
7344 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
7345 Set *COMMENTS to 1 if a comment is seen. */
7348 macarg1 (start
, limit
, depthptr
, newlines
, comments
, rest_args
)
7350 register U_CHAR
*limit
;
7351 int *depthptr
, *newlines
, *comments
;
7354 register U_CHAR
*bp
= start
;
7356 while (bp
< limit
) {
7362 if (--(*depthptr
) < 0)
7366 /* Traditionally, backslash makes following char not special. */
7367 if (bp
+ 1 < limit
&& traditional
)
7370 /* But count source lines anyway. */
7379 if (bp
[1] == '\\' && bp
[2] == '\n')
7380 newline_fix (bp
+ 1);
7381 if (cplusplus_comments
&& bp
[1] == '/') {
7384 while (bp
< limit
&& *bp
++ != '\n') ;
7388 if (bp
[1] != '*' || bp
+ 1 >= limit
)
7392 while (bp
+ 1 < limit
) {
7394 && bp
[1] == '\\' && bp
[2] == '\n')
7395 newline_fix (bp
+ 1);
7396 if (bp
[0] == '*' && bp
[1] == '/')
7398 if (*bp
== '\n') ++*newlines
;
7406 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
7411 while (*bp
== '\\' && bp
[1] == '\n') {
7414 } else if (*bp
== '\n') {
7423 /* if we've returned to lowest level and we aren't absorbing all args */
7424 if ((*depthptr
) == 0 && rest_args
== 0)
7434 /* Discard comments and duplicate newlines
7435 in the string of length LENGTH at START,
7436 except inside of string constants.
7437 The string is copied into itself with its beginning staying fixed.
7439 NEWLINES is the number of newlines that must be duplicated.
7440 We assume that that much extra space is available past the end
7444 discard_comments (start
, length
, newlines
)
7449 register U_CHAR
*ibp
;
7450 register U_CHAR
*obp
;
7451 register U_CHAR
*limit
;
7454 /* If we have newlines to duplicate, copy everything
7455 that many characters up. Then, in the second part,
7456 we will have room to insert the newlines
7458 NEWLINES may actually be too large, because it counts
7459 newlines in string constants, and we don't duplicate those.
7460 But that does no harm. */
7462 ibp
= start
+ length
;
7463 obp
= ibp
+ newlines
;
7465 while (limit
!= ibp
)
7469 ibp
= start
+ newlines
;
7470 limit
= start
+ length
+ newlines
;
7473 while (ibp
< limit
) {
7474 *obp
++ = c
= *ibp
++;
7477 /* Duplicate the newline. */
7489 if (*ibp
== '\\' && ibp
[1] == '\n')
7491 /* Delete any comment. */
7492 if (cplusplus_comments
&& ibp
[0] == '/') {
7495 while (ibp
< limit
&& *ibp
++ != '\n') ;
7498 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
7502 while (ibp
+ 1 < limit
) {
7504 && ibp
[1] == '\\' && ibp
[2] == '\n')
7505 newline_fix (ibp
+ 1);
7506 if (ibp
[0] == '*' && ibp
[1] == '/')
7515 /* Notice and skip strings, so that we don't
7516 think that comments start inside them,
7517 and so we don't duplicate newlines in them. */
7520 while (ibp
< limit
) {
7521 *obp
++ = c
= *ibp
++;
7524 if (c
== '\n' && quotec
== '\'')
7526 if (c
== '\\' && ibp
< limit
) {
7527 while (*ibp
== '\\' && ibp
[1] == '\n')
7540 /* Delete newlines in the string of length LENGTH at START, except inside
7541 of string constants. The string is copied into itself with its beginning
7545 delete_newlines (start
, length
)
7549 register U_CHAR
*ibp
;
7550 register U_CHAR
*obp
;
7551 register U_CHAR
*limit
;
7555 limit
= start
+ length
;
7558 while (ibp
< limit
) {
7559 *obp
++ = c
= *ibp
++;
7562 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
7563 output. Skip past the newline and its duplicate. */
7573 /* Notice and skip strings, so that we don't delete newlines in them. */
7576 while (ibp
< limit
) {
7577 *obp
++ = c
= *ibp
++;
7580 if (c
== '\n' && quotec
== '\'')
7592 * error - print error message and increment count of errors.
7596 error (msg
, arg1
, arg2
, arg3
)
7598 char *arg1
, *arg2
, *arg3
;
7601 FILE_BUF
*ip
= NULL
;
7603 print_containing_files ();
7605 for (i
= indepth
; i
>= 0; i
--)
7606 if (instack
[i
].fname
!= NULL
) {
7612 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7613 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7614 fprintf (stderr
, "\n");
7618 /* Error including a message from `errno'. */
7621 error_from_errno (name
)
7625 FILE_BUF
*ip
= NULL
;
7627 print_containing_files ();
7629 for (i
= indepth
; i
>= 0; i
--)
7630 if (instack
[i
].fname
!= NULL
) {
7636 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7638 if (errno
< sys_nerr
)
7639 fprintf (stderr
, "%s: %s\n", name
, sys_errlist
[errno
]);
7641 fprintf (stderr
, "%s: undocumented I/O error\n", name
);
7646 /* Print error message but don't count it. */
7649 warning (msg
, arg1
, arg2
, arg3
)
7651 char *arg1
, *arg2
, *arg3
;
7654 FILE_BUF
*ip
= NULL
;
7656 if (inhibit_warnings
)
7659 if (warnings_are_errors
)
7662 print_containing_files ();
7664 for (i
= indepth
; i
>= 0; i
--)
7665 if (instack
[i
].fname
!= NULL
) {
7671 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, ip
->lineno
);
7672 fprintf (stderr
, "warning: ");
7673 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7674 fprintf (stderr
, "\n");
7678 error_with_line (line
, msg
, arg1
, arg2
, arg3
)
7681 char *arg1
, *arg2
, *arg3
;
7684 FILE_BUF
*ip
= NULL
;
7686 print_containing_files ();
7688 for (i
= indepth
; i
>= 0; i
--)
7689 if (instack
[i
].fname
!= NULL
) {
7695 fprintf (stderr
, "%s:%d: ", ip
->nominal_fname
, line
);
7696 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7697 fprintf (stderr
, "\n");
7701 /* print an error message and maybe count it. */
7704 pedwarn (msg
, arg1
, arg2
, arg3
)
7706 char *arg1
, *arg2
, *arg3
;
7708 if (pedantic_errors
)
7709 error (msg
, arg1
, arg2
, arg3
);
7711 warning (msg
, arg1
, arg2
, arg3
);
7714 /* Report a warning (or an error if pedantic_errors)
7715 giving specified file name and line number, not current. */
7718 pedwarn_with_file_and_line (file
, line
, msg
, arg1
, arg2
, arg3
)
7722 char *arg1
, *arg2
, *arg3
;
7725 if (!pedantic_errors
&& inhibit_warnings
)
7728 fprintf (stderr
, "%s:%d: ", file
, line
);
7729 if (pedantic_errors
|| warnings_are_errors
)
7731 if (!pedantic_errors
)
7732 fprintf (stderr
, "warning: ");
7733 fprintf (stderr
, msg
, arg1
, arg2
, arg3
);
7734 fprintf (stderr
, "\n");
7737 /* Print the file names and line numbers of the #include
7738 commands which led to the current file. */
7741 print_containing_files ()
7743 FILE_BUF
*ip
= NULL
;
7747 /* If stack of files hasn't changed since we last printed
7748 this info, don't repeat it. */
7749 if (last_error_tick
== input_file_stack_tick
)
7752 for (i
= indepth
; i
>= 0; i
--)
7753 if (instack
[i
].fname
!= NULL
) {
7758 /* Give up if we don't find a source file. */
7762 /* Find the other, outer source files. */
7763 for (i
--; i
>= 0; i
--)
7764 if (instack
[i
].fname
!= NULL
) {
7768 fprintf (stderr
, "In file included");
7770 fprintf (stderr
, ",");
7773 fprintf (stderr
, " from %s:%d", ip
->nominal_fname
, ip
->lineno
);
7776 fprintf (stderr
, ":\n");
7778 /* Record we have printed the status as of this time. */
7779 last_error_tick
= input_file_stack_tick
;
7782 /* Return the line at which an error occurred.
7783 The error is not necessarily associated with the current spot
7784 in the input stack, so LINE says where. LINE will have been
7785 copied from ip->lineno for the current input level.
7786 If the current level is for a file, we return LINE.
7787 But if the current level is not for a file, LINE is meaningless.
7788 In that case, we return the lineno of the innermost file. */
7791 line_for_error (line
)
7797 for (i
= indepth
; i
>= 0; ) {
7798 if (instack
[i
].fname
!= 0)
7803 line1
= instack
[i
].lineno
;
7811 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
7813 * As things stand, nothing is ever placed in the output buffer to be
7814 * removed again except when it's KNOWN to be part of an identifier,
7815 * so flushing and moving down everything left, instead of expanding,
7819 /* You might think void was cleaner for the return type,
7820 but that would get type mismatch in check_expand in strict ANSI. */
7822 grow_outbuf (obuf
, needed
)
7823 register FILE_BUF
*obuf
;
7824 register int needed
;
7829 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
7832 /* Make it at least twice as big as it is now. */
7834 /* Make it have at least 150% of the free space we will need. */
7835 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
7836 if (minsize
> obuf
->length
)
7837 obuf
->length
= minsize
;
7839 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
7842 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
7848 /* Symbol table for macro names and special symbols */
7851 * install a name in the main hash table, even if it is already there.
7852 * name stops with first non alphanumeric, except leading '#'.
7853 * caller must check against redefinition if that is desired.
7854 * delete_macro () removes things installed by install () in fifo order.
7855 * this is important because of the `defined' special symbol used
7856 * in #if, and also if pushdef/popdef directives are ever implemented.
7858 * If LEN is >= 0, it is the length of the name.
7859 * Otherwise, compute the length by scanning the entire name.
7861 * If HASH is >= 0, it is the precomputed hash code.
7862 * Otherwise, compute the hash code.
7865 install (name
, len
, type
, ivalue
, value
, hash
)
7868 enum node_type type
;
7873 register HASHNODE
*hp
;
7874 register int i
, bucket
;
7875 register U_CHAR
*p
, *q
;
7879 while (is_idchar
[*p
])
7885 hash
= hashf (name
, len
, HASHSIZE
);
7887 i
= sizeof (HASHNODE
) + len
+ 1;
7888 hp
= (HASHNODE
*) xmalloc (i
);
7890 hp
->bucket_hdr
= &hashtab
[bucket
];
7891 hp
->next
= hashtab
[bucket
];
7892 hashtab
[bucket
] = hp
;
7894 if (hp
->next
!= NULL
)
7895 hp
->next
->prev
= hp
;
7898 if (hp
->type
== T_CONST
)
7899 hp
->value
.ival
= ivalue
;
7901 hp
->value
.cpval
= value
;
7902 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
7905 for (i
= 0; i
< len
; i
++)
7912 * find the most recent hash node for name name (ending with first
7913 * non-identifier char) installed by install
7915 * If LEN is >= 0, it is the length of the name.
7916 * Otherwise, compute the length by scanning the entire name.
7918 * If HASH is >= 0, it is the precomputed hash code.
7919 * Otherwise, compute the hash code.
7922 lookup (name
, len
, hash
)
7927 register U_CHAR
*bp
;
7928 register HASHNODE
*bucket
;
7931 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
7936 hash
= hashf (name
, len
, HASHSIZE
);
7938 bucket
= hashtab
[hash
];
7940 if (bucket
->length
== len
&& strncmp (bucket
->name
, name
, len
) == 0)
7942 bucket
= bucket
->next
;
7948 * Delete a hash node. Some weirdness to free junk from macros.
7949 * More such weirdness will have to be added if you define more hash
7950 * types that need it.
7953 /* Note that the DEFINITION of a macro is removed from the hash table
7954 but its storage is not freed. This would be a storage leak
7955 except that it is not reasonable to keep undefining and redefining
7956 large numbers of macros many times.
7957 In any case, this is necessary, because a macro can be #undef'd
7958 in the middle of reading the arguments to a call to it.
7959 If #undef freed the DEFINITION, that would crash. */
7966 if (hp
->prev
!= NULL
)
7967 hp
->prev
->next
= hp
->next
;
7968 if (hp
->next
!= NULL
)
7969 hp
->next
->prev
= hp
->prev
;
7971 /* make sure that the bucket chain header that
7972 the deleted guy was on points to the right thing afterwards. */
7973 if (hp
== *hp
->bucket_hdr
)
7974 *hp
->bucket_hdr
= hp
->next
;
7977 if (hp
->type
== T_MACRO
) {
7978 DEFINITION
*d
= hp
->value
.defn
;
7979 struct reflist
*ap
, *nextap
;
7981 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
7992 * return hash function on name. must be compatible with the one
7993 * computed a step at a time, elsewhere
7996 hashf (name
, len
, hashsize
)
7997 register U_CHAR
*name
;
8004 r
= HASHSTEP (r
, *name
++);
8006 return MAKE_POS (r
) % hashsize
;
8010 /* Dump the definition of a single macro HP to OF. */
8012 dump_single_macro (hp
, of
)
8013 register HASHNODE
*hp
;
8016 register DEFINITION
*defn
= hp
->value
.defn
;
8022 /* Print the definition of the macro HP. */
8024 fprintf (of
, "#define %s", hp
->name
);
8026 if (defn
->nargs
>= 0) {
8030 for (i
= 0; i
< defn
->nargs
; i
++) {
8031 dump_arg_n (defn
, i
, of
);
8032 if (i
+ 1 < defn
->nargs
)
8042 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8043 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
8044 if (ap
->nchars
!= 0)
8046 offset
+= ap
->nchars
;
8049 if (ap
->raw_before
&& !concat
)
8050 fprintf (of
, " ## ");
8052 dump_arg_n (defn
, ap
->argno
, of
);
8053 if (ap
->raw_after
) {
8054 fprintf (of
, " ## ");
8058 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
8062 /* Dump all macro definitions as #defines to stdout. */
8069 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
8070 register HASHNODE
*hp
;
8072 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
8073 if (hp
->type
== T_MACRO
)
8074 dump_single_macro (hp
, stdout
);
8079 /* Output to OF a substring of a macro definition.
8080 BASE is the beginning of the definition.
8081 Output characters START thru LENGTH.
8082 Discard newlines outside of strings, thus
8083 converting funny-space markers to ordinary spaces. */
8086 dump_defn_1 (base
, start
, length
, of
)
8092 U_CHAR
*p
= base
+ start
;
8093 U_CHAR
*limit
= base
+ start
+ length
;
8098 else if (*p
== '\"' || *p
=='\'') {
8099 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
8100 NULL_PTR
, NULL_PTR
);
8101 fwrite (p
, p1
- p
, 1, of
);
8108 /* Print the name of argument number ARGNUM of macro definition DEFN
8110 Recall that DEFN->args.argnames contains all the arg names
8111 concatenated in reverse order with comma-space in between. */
8114 dump_arg_n (defn
, argnum
, of
)
8119 register U_CHAR
*p
= defn
->args
.argnames
;
8120 while (argnum
+ 1 < defn
->nargs
) {
8121 p
= (U_CHAR
*) index (p
, ' ') + 1;
8125 while (*p
&& *p
!= ',') {
8131 /* Initialize syntactic classifications of characters. */
8134 initialize_char_syntax ()
8139 * Set up is_idchar and is_idstart tables. These should be
8140 * faster than saying (is_alpha (c) || c == '_'), etc.
8141 * Set up these things before calling any routines tthat
8144 for (i
= 'a'; i
<= 'z'; i
++) {
8145 is_idchar
[i
- 'a' + 'A'] = 1;
8147 is_idstart
[i
- 'a' + 'A'] = 1;
8150 for (i
= '0'; i
<= '9'; i
++)
8153 is_idstart
['_'] = 1;
8154 is_idchar
['$'] = dollars_in_ident
;
8155 is_idstart
['$'] = dollars_in_ident
;
8157 /* horizontal space table */
8158 is_hor_space
[' '] = 1;
8159 is_hor_space
['\t'] = 1;
8160 is_hor_space
['\v'] = 1;
8161 is_hor_space
['\f'] = 1;
8162 is_hor_space
['\r'] = 1;
8172 /* Initialize the built-in macros. */
8175 initialize_builtins (inp
, outp
)
8179 install ("__LINE__", -1, T_SPECLINE
, 0, 0, -1);
8180 install ("__DATE__", -1, T_DATE
, 0, 0, -1);
8181 install ("__FILE__", -1, T_FILE
, 0, 0, -1);
8182 install ("__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
8183 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
8184 install ("__VERSION__", -1, T_VERSION
, 0, 0, -1);
8185 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
8186 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
8187 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
8188 install ("__TIME__", -1, T_TIME
, 0, 0, -1);
8190 install ("__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
8192 install ("__OBJC__", -1, T_CONST
, 1, 0, -1);
8193 /* This is supplied using a -D by the compiler driver
8194 so that it is present only when truly compiling with GNU C. */
8195 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
8199 char directive
[2048];
8200 register struct directive
*dp
= &directive_table
[0];
8201 struct tm
*timebuf
= timestamp ();
8203 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
8204 instack
[0].nominal_fname
);
8205 output_line_command (inp
, outp
, 0, same_file
);
8206 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8208 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
8209 output_line_command (inp
, outp
, 0, same_file
);
8210 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8212 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
8213 output_line_command (inp
, outp
, 0, same_file
);
8214 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8216 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
8217 output_line_command (inp
, outp
, 0, same_file
);
8218 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8220 sprintf (directive
, " __WCHAR_TYPE__ %s\n", WCHAR_TYPE
);
8221 output_line_command (inp
, outp
, 0, same_file
);
8222 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8224 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
8225 monthnames
[timebuf
->tm_mon
],
8226 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
8227 output_line_command (inp
, outp
, 0, same_file
);
8228 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8230 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
8231 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
8232 output_line_command (inp
, outp
, 0, same_file
);
8233 pass_thru_directive (directive
, &directive
[strlen (directive
)], outp
, dp
);
8237 sprintf (directive
, " __STDC__ 1");
8238 output_line_command (inp
, outp
, 0, same_file
);
8239 pass_thru_directive (directive
, &directive
[strlen (directive
)],
8244 sprintf (directive
, " __OBJC__ 1");
8245 output_line_command (inp
, outp
, 0, same_file
);
8246 pass_thru_directive (directive
, &directive
[strlen (directive
)],
8253 * process a given definition string, for initialization
8254 * If STR is just an identifier, define it with value 1.
8255 * If STR has anything after the identifier, then it should
8256 * be identifier=definition.
8260 make_definition (str
, op
)
8265 struct directive
*kt
;
8270 if (!is_idstart
[*p
]) {
8271 error ("malformed option `-D %s'", str
);
8274 while (is_idchar
[*++p
])
8277 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
8278 strcpy ((char *)buf
, str
);
8279 strcat ((char *)buf
, " 1");
8280 } else if (*p
!= '=') {
8281 error ("malformed option `-D %s'", str
);
8285 /* Copy the entire option so we can modify it. */
8286 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
8287 strncpy (buf
, str
, p
- str
);
8288 /* Change the = to a space. */
8290 /* Scan for any backslash-newline and remove it. */
8294 if (*p
== '\\' && p
[1] == '\n')
8296 /* Change newline chars into newline-markers. */
8297 else if (*p
== '\n')
8309 ip
= &instack
[++indepth
];
8310 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
8312 ip
->buf
= ip
->bufp
= buf
;
8313 ip
->length
= strlen (buf
);
8317 ip
->if_stack
= if_stack
;
8318 ip
->system_header_p
= 0;
8320 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
8323 do_define (buf
, buf
+ strlen (buf
) , op
, kt
);
8327 /* JF, this does the work for the -U option */
8330 make_undef (str
, op
)
8335 struct directive
*kt
;
8337 ip
= &instack
[++indepth
];
8338 ip
->nominal_fname
= ip
->fname
= "*undef*";
8340 ip
->buf
= ip
->bufp
= str
;
8341 ip
->length
= strlen (str
);
8345 ip
->if_stack
= if_stack
;
8346 ip
->system_header_p
= 0;
8348 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
8351 do_undef (str
, str
+ strlen (str
), op
, kt
);
8355 /* Process the string STR as if it appeared as the body of a #assert.
8356 OPTION is the option name for which STR was the argument. */
8359 make_assertion (option
, str
)
8364 struct directive
*kt
;
8365 U_CHAR
*buf
, *p
, *q
;
8367 /* Copy the entire option so we can modify it. */
8368 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
8369 strcpy ((char *) buf
, str
);
8370 /* Scan for any backslash-newline and remove it. */
8373 if (*p
== '\\' && p
[1] == '\n')
8381 if (!is_idstart
[*p
]) {
8382 error ("malformed option `%s %s'", option
, str
);
8385 while (is_idchar
[*++p
])
8387 while (*p
== ' ' || *p
== '\t') p
++;
8388 if (! (*p
== 0 || *p
== '(')) {
8389 error ("malformed option `%s %s'", option
, str
);
8393 ip
= &instack
[++indepth
];
8394 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
8396 ip
->buf
= ip
->bufp
= buf
;
8397 ip
->length
= strlen (buf
);
8401 ip
->if_stack
= if_stack
;
8402 ip
->system_header_p
= 0;
8404 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
8407 /* pass NULL as output ptr to do_define since we KNOW it never
8408 does any output.... */
8409 do_assert (buf
, buf
+ strlen (buf
) , NULL_PTR
, kt
);
8413 /* Append a chain of `struct file_name_list's
8414 to the end of the main include chain.
8415 FIRST is the beginning of the chain to append, and LAST is the end. */
8418 append_include_chain (first
, last
)
8419 struct file_name_list
*first
, *last
;
8421 struct file_name_list
*dir
;
8423 if (!first
|| !last
)
8429 last_include
->next
= first
;
8431 if (first_bracket_include
== 0)
8432 first_bracket_include
= first
;
8434 for (dir
= first
; ; dir
= dir
->next
) {
8435 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
8436 if (len
> max_include_len
)
8437 max_include_len
= len
;
8443 last_include
= last
;
8446 /* Add output to `deps_buffer' for the -M switch.
8447 STRING points to the text to be output.
8448 SIZE is the number of bytes, or 0 meaning output until a null.
8449 Outputting the empty string breaks the line if it is long enough. */
8452 deps_output (string
, size
)
8457 size
= strlen (string
);
8459 #ifndef MAX_OUTPUT_COLUMNS
8460 #define MAX_OUTPUT_COLUMNS 75
8462 if (size
== 0 && deps_column
!= 0
8463 && size
+ deps_column
> MAX_OUTPUT_COLUMNS
) {
8464 deps_output ("\\\n ", 0);
8468 if (deps_size
+ size
+ 1 > deps_allocated_size
) {
8469 deps_allocated_size
= deps_size
+ size
+ 50;
8470 deps_allocated_size
*= 2;
8471 deps_buffer
= (char *) xrealloc (deps_buffer
, deps_allocated_size
);
8473 bcopy (string
, &deps_buffer
[deps_size
], size
);
8475 deps_column
+= size
;
8476 deps_buffer
[deps_size
] = 0;
8479 #if defined(USG) || defined(VMS)
8485 register unsigned length
;
8487 while (length
-- > 0)
8492 bcopy (b1
, b2
, length
)
8495 register unsigned length
;
8497 while (length
-- > 0)
8502 bcmp (b1
, b2
, length
) /* This could be a macro! */
8505 register unsigned length
;
8507 while (length
-- > 0)
8513 #endif /* not BSTRING */
8514 #endif /* USG or VMS */
8521 fprintf (stderr
, "%s: ", progname
);
8522 fprintf (stderr
, str
, arg
);
8523 fprintf (stderr
, "\n");
8524 exit (FAILURE_EXIT_CODE
);
8527 /* More 'friendly' abort that prints the line and file.
8528 config.h can #define abort fancy_abort if you like that sort of thing. */
8533 fatal ("Internal gcc abort.");
8537 perror_with_name (name
)
8540 fprintf (stderr
, "%s: ", progname
);
8541 if (errno
< sys_nerr
)
8542 fprintf (stderr
, "%s: %s\n", name
, sys_errlist
[errno
]);
8544 fprintf (stderr
, "%s: undocumented I/O error\n", name
);
8549 pfatal_with_name (name
)
8552 perror_with_name (name
);
8556 exit (FAILURE_EXIT_CODE
);
8564 fatal ("Memory exhausted.");
8572 register char *ptr
= (char *) malloc (size
);
8573 if (ptr
!= 0) return (ptr
);
8580 xrealloc (old
, size
)
8584 register char *ptr
= (char *) realloc (old
, size
);
8585 if (ptr
!= 0) return (ptr
);
8592 xcalloc (number
, size
)
8593 unsigned number
, size
;
8595 register unsigned total
= number
* size
;
8596 register char *ptr
= (char *) malloc (total
);
8601 /* It's not too long, so loop, zeroing by longs.
8602 It must be safe because malloc values are always well aligned. */
8603 register long *zp
= (long *) ptr
;
8604 register long *zl
= (long *) (ptr
+ total
- 4);
8605 register int i
= total
- 4;
8624 unsigned size
= strlen (input
);
8625 char *output
= xmalloc (size
+ 1);
8626 strcpy (output
, input
);
8630 /* Get the file-mode and data size of the file open on FD
8631 and store them in *MODE_POINTER and *SIZE_POINTER. */
8634 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
8637 long int *size_pointer
;
8641 if (fstat (fd
, &sbuf
) < 0) return (-1);
8642 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
8643 if (size_pointer
) *size_pointer
= sbuf
.st_size
;
8649 /* Under VMS we need to fix up the "include" specification
8650 filename so that everything following the 1st slash is
8651 changed into its correct VMS file specification. */
8654 hack_vms_include_specification (fname
)
8657 register char *cp
, *cp1
, *cp2
;
8658 int f
, check_filename_before_returning
, no_prefix_seen
;
8661 check_filename_before_returning
= 0;
8664 /* Ignore leading "./"s */
8665 while (fname
[0] == '.' && fname
[1] == '/') {
8666 strcpy (fname
, fname
+2);
8667 no_prefix_seen
= 1; /* mark this for later */
8669 /* Look for the boundary between the VMS and UNIX filespecs */
8670 cp
= rindex (fname
, ']'); /* Look for end of dirspec. */
8671 if (cp
== 0) cp
= rindex (fname
, '>'); /* ... Ditto */
8672 if (cp
== 0) cp
= rindex (fname
, ':'); /* Look for end of devspec. */
8676 cp
= index (fname
, '/'); /* Look for the "/" */
8679 cp2
= Local
; /* initialize */
8681 /* We are trying to do a number of things here. First of all, we are
8682 trying to hammer the filenames into a standard format, such that later
8683 processing can handle them.
8685 If the file name contains something like [dir.], then it recognizes this
8686 as a root, and strips the ".]". Later processing will add whatever is
8687 needed to get things working properly.
8689 If no device is specified, then the first directory name is taken to be
8690 a device name (or a rooted logical). */
8692 /* See if we found that 1st slash */
8693 if (cp
== 0) return; /* Nothing to do!!! */
8694 if (*cp
!= '/') return; /* Nothing to do!!! */
8695 /* Point to the UNIX filename part (which needs to be fixed!) */
8697 /* If the directory spec is not rooted, we can just copy
8698 the UNIX filename part and we are done */
8699 if (((cp
- fname
) > 1) && ((cp
[-1] == ']') || (cp
[-1] == '>'))) {
8700 if (cp
[-2] != '.') {
8702 * The VMS part ends in a `]', and the preceding character is not a `.'.
8703 * We strip the `]', and then splice the two parts of the name in the
8704 * usual way. Given the default locations for include files in cccp.c,
8705 * we will only use this code if the user specifies alternate locations
8706 * with the /include (-I) switch on the command line. */
8707 cp
-= 1; /* Strip "]" */
8708 cp1
--; /* backspace */
8711 * The VMS part has a ".]" at the end, and this will not do. Later
8712 * processing will add a second directory spec, and this would be a syntax
8713 * error. Thus we strip the ".]", and thus merge the directory specs.
8714 * We also backspace cp1, so that it points to a '/'. This inhibits the
8715 * generation of the 000000 root directory spec (which does not belong here
8718 cp
-= 2; /* Strip ".]" */
8719 cp1
--; }; /* backspace */
8722 /* We drop in here if there is no VMS style directory specification yet.
8723 * If there is no device specification either, we make the first dir a
8724 * device and try that. If we do not do this, then we will be essentially
8725 * searching the users default directory (as if they did a #include "asdf.h").
8727 * Then all we need to do is to push a '[' into the output string. Later
8728 * processing will fill this in, and close the bracket.
8730 if(cp
[-1] != ':') *cp2
++ = ':'; /* dev not in spec. take first dir */
8731 *cp2
++ = '['; /* Open the directory specification */
8734 /* at this point we assume that we have the device spec, and (at least
8735 the opening "[" for a directory specification. We may have directories
8736 specified already */
8738 /* If there are no other slashes then the filename will be
8739 in the "root" directory. Otherwise, we need to add
8740 directory specifications. */
8741 if (index (cp1
, '/') == 0) {
8742 /* Just add "000000]" as the directory string */
8743 strcpy (cp2
, "000000]");
8744 cp2
+= strlen (cp2
);
8745 check_filename_before_returning
= 1; /* we might need to fool with this later */
8747 /* As long as there are still subdirectories to add, do them. */
8748 while (index (cp1
, '/') != 0) {
8749 /* If this token is "." we can ignore it */
8750 if ((cp1
[0] == '.') && (cp1
[1] == '/')) {
8754 /* Add a subdirectory spec. Do not duplicate "." */
8755 if (cp2
[-1] != '.' && cp2
[-1] != '[' && cp2
[-1] != '<')
8757 /* If this is ".." then the spec becomes "-" */
8758 if ((cp1
[0] == '.') && (cp1
[1] == '.') && (cp
[2] == '/')) {
8759 /* Add "-" and skip the ".." */
8764 /* Copy the subdirectory */
8765 while (*cp1
!= '/') *cp2
++= *cp1
++;
8766 cp1
++; /* Skip the "/" */
8768 /* Close the directory specification */
8769 if(cp2
[-1] == '.') /* no trailing periods */
8773 /* Now add the filename */
8774 while (*cp1
) *cp2
++ = *cp1
++;
8776 /* Now append it to the original VMS spec. */
8779 /* If we put a [000000] in the filename, try to open it first. If this fails,
8780 remove the [000000], and return that name. This provides flexibility
8781 to the user in that they can use both rooted and non-rooted logical names
8782 to point to the location of the file. */
8784 if (check_filename_before_returning
&& no_prefix_seen
) {
8785 f
= open (fname
, O_RDONLY
, 0666);
8787 /* The file name is OK as it is, so return it as is. */
8791 /* The filename did not work. Try to remove the [000000] from the name,
8793 cp
= index (fname
, '[');
8794 cp2
= index (fname
, ']') + 1;
8795 strcpy (cp
, cp2
); /* this gets rid of it */
8803 /* These are the read/write replacement routines for
8804 VAX-11 "C". They make read/write behave enough
8805 like their UNIX counterparts that CCCP will work */
8808 read (fd
, buf
, size
)
8813 #undef read /* Get back the REAL read routine */
8815 register int total
= 0;
8817 /* Read until the buffer is exhausted */
8819 /* Limit each read to 32KB */
8820 i
= (size
> (32*1024)) ? (32*1024) : size
;
8821 i
= read (fd
, buf
, i
);
8823 if (i
== 0) return (total
);
8826 /* Account for this read */
8835 write (fd
, buf
, size
)
8840 #undef write /* Get back the REAL write routine */
8844 /* Limit individual writes to 32Kb */
8847 j
= (i
> (32*1024)) ? (32*1024) : i
;
8848 if (write (fd
, buf
, j
) < 0) return (-1);
8849 /* Account for the data written */
8856 /* The following wrapper functions supply additional arguments to the VMS
8857 I/O routines to optimize performance with file handling. The arguments
8859 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
8860 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
8861 "fop=tef"- Truncate unused portions of file when closing file.
8862 "shr=nil"- Disallow file sharing while file is open.
8866 freopen (fname
, type
, oldfile
)
8871 #undef freopen /* Get back the REAL fopen routine */
8872 if (strcmp (type
, "w") == 0)
8873 return freopen (fname
, type
, oldfile
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8874 return freopen (fname
, type
, oldfile
, "mbc=16");
8882 #undef fopen /* Get back the REAL fopen routine */
8883 if (strcmp (type
, "w") == 0)
8884 return fopen (fname
, type
, "mbc=16", "deq=64", "fop=tef", "shr=nil");
8885 return fopen (fname
, type
, "mbc=16");
8889 open (fname
, flags
, prot
)
8894 #undef open /* Get back the REAL open routine */
8895 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
8898 /* Avoid run-time library bug, where copying M out of N+M characters with
8899 N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
8900 gcc-cpp exercises this particular bug. */
8903 strncat (dst
, src
, cnt
)
8908 register char *d
= dst
, *s
= (char *) src
;
8909 register int n
= cnt
; /* convert to _signed_ type */
8911 while (*d
) d
++; /* advance to end */
8913 if (!(*d
++ = *s
++)) break;
8914 if (n
< 0) *d
= '\0';