1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-97, 1998 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #define PRINTF_PROTO(ARGS, m, n) PVPROTO (ARGS) ATTRIBUTE_PRINTF(m, n)
25 #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
26 #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
27 #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
28 #define PRINTF_PROTO_4(ARGS) PRINTF_PROTO(ARGS, 4, 5)
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
38 typedef unsigned char U_CHAR
;
42 #ifdef MULTIBYTE_CHARS
45 #endif /* MULTIBYTE_CHARS */
47 #ifndef GET_ENV_PATH_LIST
48 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
51 #ifndef STANDARD_INCLUDE_DIR
52 # define STANDARD_INCLUDE_DIR "/usr/include"
55 /* By default, colon separates directories in a path. */
56 #ifndef PATH_SEPARATOR
57 # define PATH_SEPARATOR ':'
60 /* By default, the suffix for object files is ".o". */
62 # define HAVE_OBJECT_SUFFIX
64 # define OBJECT_SUFFIX ".o"
67 /* VMS-specific definitions */
72 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
73 #define fopen(fname,mode) VMS_fopen (fname,mode)
74 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
75 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
76 static int VMS_fstat (), VMS_stat ();
77 static int VMS_open ();
78 static FILE *VMS_fopen ();
79 static FILE *VMS_freopen ();
80 static int hack_vms_include_specification ();
81 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
82 #define INO_T_HASH(a) 0
83 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
86 /* Windows does not natively support inodes, and neither does MSDOS. */
87 #if (defined (_WIN32) && ! defined (__CYGWIN__)) || defined (__MSDOS__)
88 #define INO_T_EQ(a, b) 0
93 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
94 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
96 /* Find the largest host integer type and set its size and type.
97 Watch out: on some crazy hosts `long' is shorter than `int'. */
101 # include <inttypes.h>
102 # define HOST_WIDE_INT intmax_t
104 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
105 # define HOST_WIDE_INT int
107 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
108 # define HOST_WIDE_INT long
110 # define HOST_WIDE_INT long long
117 #define INO_T_EQ(a, b) ((a) == (b))
121 #define INO_T_HASH(a) (a)
124 #ifndef INCLUDE_LEN_FUDGE
125 #define INCLUDE_LEN_FUDGE 0
128 /* External declarations. */
130 extern char *version_string
;
131 extern char *update_path
PROTO((char *, char *));
132 HOST_WIDE_INT parse_escape
PROTO((char **, HOST_WIDE_INT
));
133 HOST_WIDE_INT parse_c_expression
PROTO((char *, int));
135 /* Name under which this program was invoked. */
137 static char *progname
;
139 /* Nonzero means use extra default include directories for C++. */
141 static int cplusplus
;
143 /* Nonzero means handle cplusplus style comments */
145 static int cplusplus_comments
;
147 /* Nonzero means handle #import, for objective C. */
151 /* Nonzero means this is an assembly file, and allow
152 unknown directives, which could be comments. */
156 /* Current maximum length of directory names in the search path
157 for include files. (Altered as we get more of them.) */
159 static int max_include_len
;
161 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
163 static int for_lint
= 0;
165 /* Nonzero means copy comments into the output file. */
167 static int put_out_comments
= 0;
169 /* Nonzero means don't process the ANSI trigraph sequences. */
171 static int no_trigraphs
= 0;
173 /* Nonzero means print the names of included files rather than
174 the preprocessed output. 1 means just the #include "...",
175 2 means #include <...> as well. */
177 static int print_deps
= 0;
179 /* Nonzero if missing .h files in -M output are assumed to be generated
180 files and not errors. */
182 static int print_deps_missing_files
= 0;
184 /* Nonzero means print names of header files (-H). */
186 static int print_include_names
= 0;
188 /* Nonzero means don't output line number information. */
190 static int no_line_directives
;
192 /* Nonzero means output the text in failing conditionals,
193 inside #failed ... #endfailed. */
195 static int output_conditionals
;
197 /* dump_only means inhibit output of the preprocessed text
198 and instead output the definitions of all user-defined
199 macros in a form suitable for use as input to cccp.
200 dump_names means pass #define and the macro name through to output.
201 dump_definitions means pass the whole definition (plus #define) through
204 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
205 dump_macros
= dump_none
;
207 /* Nonzero means pass all #define and #undef directives which we actually
208 process through to the output stream. This feature is used primarily
209 to allow cc1 to record the #defines and #undefs for the sake of
210 debuggers which understand about preprocessor macros, but it may
211 also be useful with -E to figure out how symbols are defined, and
212 where they are defined. */
213 static int debug_output
= 0;
215 /* Nonzero means pass #include lines through to the output,
216 even if they are ifdefed out. */
217 static int dump_includes
;
219 /* Nonzero indicates special processing used by the pcp program. The
220 special effects of this mode are:
222 Inhibit all macro expansion, except those inside #if directives.
224 Process #define directives normally, and output their contents
227 Output preconditions to pcp_outfile indicating all the relevant
228 preconditions for use of this file in a later cpp run.
230 static FILE *pcp_outfile
;
232 /* Nonzero means we are inside an IF during a -pcp run. In this mode
233 macro expansion is done, and preconditions are output for all macro
234 uses requiring them. */
235 static int pcp_inside_if
;
237 /* Nonzero means never to include precompiled files.
238 This is 1 since there's no way now to make precompiled files,
239 so it's not worth testing for them. */
240 static int no_precomp
= 1;
242 /* Nonzero means give all the error messages the ANSI standard requires. */
246 /* Nonzero means try to make failure to fit ANSI C an error. */
248 static int pedantic_errors
;
250 /* Nonzero means don't print warning messages. -w. */
252 static int inhibit_warnings
= 0;
254 /* Nonzero means warn if slash-star appears in a slash-star comment,
255 or if newline-backslash appears in a slash-slash comment. */
257 static int warn_comments
;
259 /* Nonzero means warn if a macro argument is (or would be)
260 stringified with -traditional. */
262 static int warn_stringify
;
264 /* Nonzero means warn if there are any trigraphs. */
266 static int warn_trigraphs
;
268 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
270 static int warn_undef
;
272 /* Nonzero means warn if #import is used. */
274 static int warn_import
= 1;
276 /* Nonzero means turn warnings into errors. */
278 static int warnings_are_errors
;
280 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
284 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
288 /* Nonzero for the 199x C Standard. */
292 /* Nonzero causes output not to be done,
293 but directives such as #define that have side effects
296 static int no_output
;
298 /* Nonzero means we should look for header.gcc files that remap file names. */
301 /* Nonzero means this file was included with a -imacros or -include
302 command line and should not be recorded as an include file. */
304 static int no_record_file
;
306 /* Nonzero means that we have finished processing the command line options.
307 This flag is used to decide whether or not to issue certain errors
310 static int done_initializing
= 0;
312 /* Line where a newline was first seen in a string constant. */
314 static int multiline_string_line
= 0;
316 /* I/O buffer structure.
317 The `fname' field is nonzero for source files and #include files
318 and for the dummy text used for -D and -U.
319 It is zero for rescanning results of macro expansion
320 and for expanding macro arguments. */
321 #define INPUT_STACK_MAX 400
322 static struct file_buf
{
324 /* Filename specified with #line directive. */
326 /* The length of nominal_fname, which may contain embedded NULs. */
327 size_t nominal_fname_len
;
328 /* Include file description. */
329 struct include_file
*inc
;
330 /* Record where in the search path this file was found.
331 For #include_next. */
332 struct file_name_list
*dir
;
337 /* Macro that this level is the expansion of.
338 Included so that we can reenable the macro
339 at the end of this level. */
340 struct hashnode
*macro
;
341 /* Value of if_stack at start of this file.
342 Used to prohibit unmatched #endif (etc) in an include file. */
343 struct if_stack
*if_stack
;
344 /* Object to be freed at end of input at this level. */
346 /* True if this is a system header file; see is_system_include. */
347 char system_header_p
;
348 } instack
[INPUT_STACK_MAX
];
350 static int last_error_tick
; /* Incremented each time we print it. */
351 static int input_file_stack_tick
; /* Incremented when the status changes. */
353 /* Current nesting level of input sources.
354 `instack[indepth]' is the level currently being read. */
355 static int indepth
= -1;
356 #define CHECK_DEPTH(code) \
357 if (indepth >= (INPUT_STACK_MAX - 1)) \
359 error_with_line (line_for_error (instack[indepth].lineno), \
360 "macro or `#include' recursion too deep"); \
364 /* Current depth in #include directives that use <...>. */
365 static int system_include_depth
= 0;
367 typedef struct file_buf FILE_BUF
;
369 /* The output buffer. Its LENGTH field is the amount of room allocated
370 for the buffer, not the number of chars actually present. To get
371 that, subtract outbuf.buf from outbuf.bufp. */
373 #define OUTBUF_SIZE 10 /* initial size of output buffer */
374 static FILE_BUF outbuf
;
376 /* Grow output buffer OBUF points at
377 so it can hold at least NEEDED more chars. */
379 #define check_expand(OBUF, NEEDED) \
380 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
381 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
383 struct file_name_list
385 struct file_name_list
*next
;
386 /* If the following is 1, it is a C-language system include
388 int c_system_include_path
;
389 /* Mapping of file names for this directory. */
390 struct file_name_map
*name_map
;
391 /* Non-zero if name_map is valid. */
393 /* The include directory status. */
395 /* The include prefix: "" denotes the working directory,
396 otherwise fname must end in '/'.
397 The actual size is dynamically allocated. */
401 /* #include "file" looks in source file dir, then stack. */
402 /* #include <file> just looks in the stack. */
403 /* -I directories are added to the end, then the defaults are added. */
405 static struct default_include
{
406 char *fname
; /* The name of the directory. */
407 char *component
; /* The component containing the directory */
408 int cplusplus
; /* Only look here if we're compiling C++. */
409 int cxx_aware
; /* Includes in this directory don't need to
410 be wrapped in extern "C" when compiling
412 } include_defaults_array
[]
413 #ifdef INCLUDE_DEFAULTS
417 /* Pick up GNU C++ specific include files. */
418 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
420 /* This is the dir for fixincludes. Put it just before
421 the files that we fix. */
422 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
423 /* For cross-compilation, this dir name is generated
424 automatically in Makefile.in. */
425 { CROSS_INCLUDE_DIR
, "GCC", 0, 0 },
426 #ifdef TOOL_INCLUDE_DIR
427 /* This is another place that the target system's headers might be. */
428 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0 },
430 #else /* not CROSS_COMPILE */
431 #ifdef LOCAL_INCLUDE_DIR
432 /* This should be /usr/local/include and should come before
433 the fixincludes-fixed header files. */
434 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
436 #ifdef TOOL_INCLUDE_DIR
437 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
438 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
439 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0 },
441 /* This is the dir for fixincludes. Put it just before
442 the files that we fix. */
443 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
444 /* Some systems have an extra dir of include files. */
445 #ifdef SYSTEM_INCLUDE_DIR
446 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
448 #ifndef STANDARD_INCLUDE_COMPONENT
449 #define STANDARD_INCLUDE_COMPONENT 0
451 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
452 #endif /* not CROSS_COMPILE */
455 #endif /* no INCLUDE_DEFAULTS */
457 /* The code looks at the defaults through this pointer, rather than through
458 the constant structure above. This pointer gets changed if an environment
459 variable specifies other defaults. */
460 static struct default_include
*include_defaults
= include_defaults_array
;
462 static struct file_name_list
*include
= 0; /* First dir to search */
463 /* First dir to search for <file> */
464 /* This is the first element to use for #include <...>.
465 If it is 0, use the entire chain for such includes. */
466 static struct file_name_list
*first_bracket_include
= 0;
467 /* This is the first element in the chain that corresponds to
468 a directory of system header files. */
469 static struct file_name_list
*first_system_include
= 0;
470 static struct file_name_list
*last_include
= 0; /* Last in chain */
472 /* Chain of include directories to put at the end of the other chain. */
473 static struct file_name_list
*after_include
= 0;
474 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
476 /* Chain to put at the start of the system include files. */
477 static struct file_name_list
*before_system
= 0;
478 static struct file_name_list
*last_before_system
= 0; /* Last in chain */
480 /* Directory prefix that should replace `/usr' in the standard
481 include file directories. */
482 static char *include_prefix
;
484 /* Maintain and search list of included files. */
486 struct include_file
{
487 struct include_file
*next
; /* for include_hashtab */
488 struct include_file
*next_ino
; /* for include_ino_hashtab */
490 /* If the following is the empty string, it means #pragma once
491 was seen in this include file, or #import was applied to the file.
492 Otherwise, if it is nonzero, it is a macro name.
493 Don't include the file again if that macro is defined. */
494 U_CHAR
*control_macro
;
495 /* Nonzero if the dependency on this include file has been output. */
500 /* Hash tables of files already included with #include or #import.
501 include_hashtab is by full name; include_ino_hashtab is by inode number. */
503 #define INCLUDE_HASHSIZE 61
504 static struct include_file
*include_hashtab
[INCLUDE_HASHSIZE
];
505 static struct include_file
*include_ino_hashtab
[INCLUDE_HASHSIZE
];
507 /* Global list of strings read in from precompiled files. This list
508 is kept in the order the strings are read in, with new strings being
509 added at the end through stringlist_tailp. We use this list to output
510 the strings at the end of the run.
512 static STRINGDEF
*stringlist
;
513 static STRINGDEF
**stringlist_tailp
= &stringlist
;
516 /* Structure returned by create_definition */
517 typedef struct macrodef MACRODEF
;
520 struct definition
*defn
;
525 enum sharp_token_type
{
526 NO_SHARP_TOKEN
= 0, /* token not present */
528 SHARP_TOKEN
= '#', /* token spelled with # only */
529 WHITE_SHARP_TOKEN
, /* token spelled with # and white space */
531 PERCENT_COLON_TOKEN
= '%', /* token spelled with %: only */
532 WHITE_PERCENT_COLON_TOKEN
/* token spelled with %: and white space */
535 /* Structure allocated for every #define. For a simple replacement
538 nargs = -1, the `pattern' list is null, and the expansion is just
539 the replacement text. Nargs = 0 means a functionlike macro with no args,
541 #define getchar() getc (stdin) .
542 When there are args, the expansion is the replacement text with the
543 args squashed out, and the reflist is a list describing how to
544 build the output from the input: e.g., "3 chars, then the 1st arg,
545 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
546 The chars here come from the expansion. Whatever is left of the
547 expansion after the last arg-occurrence is copied after that arg.
548 Note that the reflist can be arbitrarily long---
549 its length depends on the number of times the arguments appear in
550 the replacement text, not how many args there are. Example:
551 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
553 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
554 where (x, y) means (nchars, argno). */
556 typedef struct definition DEFINITION
;
559 int length
; /* length of expansion string */
560 int predefined
; /* True if the macro was builtin or */
561 /* came from the command line */
563 int line
; /* Line number of definition */
564 char *file
; /* File of definition */
565 size_t file_len
; /* Length of file (which can contain NULs) */
566 char rest_args
; /* Nonzero if last arg. absorbs the rest */
568 struct reflist
*next
;
570 enum sharp_token_type stringify
; /* set if a # operator before arg */
571 enum sharp_token_type raw_before
; /* set if a ## operator before arg */
572 enum sharp_token_type raw_after
; /* set if a ## operator after arg */
574 char rest_args
; /* Nonzero if this arg. absorbs the rest */
575 int nchars
; /* Number of literal chars to copy before
576 this arg occurrence. */
577 int argno
; /* Number of arg to substitute (origin-0) */
580 /* Names of macro args, concatenated in reverse order
581 with comma-space between them.
582 The only use of this is that we warn on redefinition
583 if this differs between the old and new definitions. */
588 /* different kinds of things that can appear in the value field
589 of a hash node. Actually, this may be useless now. */
597 * special extension string that can be added to the last macro argument to
598 * allow it to absorb the "rest" of the arguments when expanded. Ex:
599 * #define wow(a, b...) process (b, a, b)
600 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
601 * { wow (one, two); } -> { process (two, one, two); }
602 * if this "rest_arg" is used with the concat token '##' and if it is not
603 * supplied then the token attached to with ## will not be outputted. Ex:
604 * #define wow (a, b...) process (b ## , a, ## b)
605 * { wow (1, 2); } -> { process (2, 1, 2); }
606 * { wow (one); } -> { process (one); {
608 static char rest_extension
[] = "...";
609 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
611 /* This is the implicit parameter name when using variable number of
612 parameters for macros using the ISO C 9x extension. */
613 static char va_args_name
[] = "__VA_ARGS__";
614 #define VA_ARGS_NAME_LENGTH (sizeof (va_args_name) - 1)
616 /* The structure of a node in the hash table. The hash table
617 has entries for all tokens defined by #define directives (type T_MACRO),
618 plus some special tokens like __LINE__ (these each have their own
619 type, and the appropriate code is run when that type of node is seen.
620 It does not contain control words like "#define", which are recognized
621 by a separate piece of code. */
623 /* different flavors of hash nodes --- also used in keyword table */
625 T_DEFINE
= 1, /* the `#define' keyword */
626 T_INCLUDE
, /* the `#include' keyword */
627 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
628 T_IMPORT
, /* the `#import' keyword */
629 T_IFDEF
, /* the `#ifdef' keyword */
630 T_IFNDEF
, /* the `#ifndef' keyword */
631 T_IF
, /* the `#if' keyword */
632 T_ELSE
, /* `#else' */
633 T_PRAGMA
, /* `#pragma' */
634 T_ELIF
, /* `#elif' */
635 T_UNDEF
, /* `#undef' */
636 T_LINE
, /* `#line' */
637 T_ERROR
, /* `#error' */
638 T_WARNING
, /* `#warning' */
639 T_ENDIF
, /* `#endif' */
640 T_SCCS
, /* `#sccs', used on system V. */
641 T_IDENT
, /* `#ident', used on system V. */
642 T_ASSERT
, /* `#assert', taken from system V. */
643 T_UNASSERT
, /* `#unassert', taken from system V. */
644 T_SPECLINE
, /* special symbol `__LINE__' */
645 T_DATE
, /* `__DATE__' */
646 T_FILE
, /* `__FILE__' */
647 T_BASE_FILE
, /* `__BASE_FILE__' */
648 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
649 T_VERSION
, /* `__VERSION__' */
650 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
651 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
652 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
653 T_USER_LABEL_PREFIX_TYPE
, /* `__USER_LABEL_PREFIX__' */
654 T_REGISTER_PREFIX_TYPE
, /* `__REGISTER_PREFIX__' */
655 T_IMMEDIATE_PREFIX_TYPE
, /* `__IMMEDIATE_PREFIX__' */
656 T_TIME
, /* `__TIME__' */
657 T_CONST
, /* Constant value, used by `__STDC__' */
658 T_MACRO
, /* macro defined by `#define' */
659 T_DISABLED
, /* macro temporarily turned off for rescan */
660 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
661 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
662 T_UNUSED
/* Used for something not defined. */
666 struct hashnode
*next
; /* double links for easy deletion */
667 struct hashnode
*prev
;
668 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
669 chain is kept, in case the node is the head
670 of the chain and gets deleted. */
671 enum node_type type
; /* type of special token */
672 int length
; /* length of token, for quick comparison */
673 U_CHAR
*name
; /* the actual name */
674 union hashval value
; /* pointer to expansion, or whatever */
677 typedef struct hashnode HASHNODE
;
679 /* Some definitions for the hash table. The hash function MUST be
680 computed as shown in hashf () below. That is because the rescan
681 loop computes the hash value `on the fly' for most tokens,
682 in order to avoid the overhead of a lot of procedure calls to
683 the hashf () function. Hashf () only exists for the sake of
684 politeness, for use when speed isn't so important. */
686 #define HASHSIZE 1403
687 static HASHNODE
*hashtab
[HASHSIZE
];
688 #define HASHSTEP(old, c) ((old << 2) + c)
689 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
691 /* Symbols to predefine. */
693 #ifdef CPP_PREDEFINES
694 static char *predefs
= CPP_PREDEFINES
;
696 static char *predefs
= "";
699 /* We let tm.h override the types used here, to handle trivial differences
700 such as the choice of unsigned int or long unsigned int for size_t.
701 When machines start needing nontrivial differences in the size type,
702 it would be best to do something here to figure out automatically
703 from other information what type to use. */
705 /* The string value for __SIZE_TYPE__. */
708 #define SIZE_TYPE "long unsigned int"
711 /* The string value for __PTRDIFF_TYPE__. */
714 #define PTRDIFF_TYPE "long int"
717 /* The string value for __WCHAR_TYPE__. */
720 #define WCHAR_TYPE "int"
722 char * wchar_type
= WCHAR_TYPE
;
725 /* The string value for __USER_LABEL_PREFIX__ */
727 #ifndef USER_LABEL_PREFIX
728 #define USER_LABEL_PREFIX ""
730 char * user_label_prefix
= USER_LABEL_PREFIX
;
731 #undef USER_LABEL_PREFIX
733 /* The string value for __REGISTER_PREFIX__ */
735 #ifndef REGISTER_PREFIX
736 #define REGISTER_PREFIX ""
739 /* The string value for __IMMEDIATE_PREFIX__ */
741 #ifndef IMMEDIATE_PREFIX
742 #define IMMEDIATE_PREFIX ""
745 /* In the definition of a #assert name, this structure forms
746 a list of the individual values asserted.
747 Each value is itself a list of "tokens".
748 These are strings that are compared by name. */
750 struct tokenlist_list
{
751 struct tokenlist_list
*next
;
752 struct arglist
*tokens
;
755 struct assertion_hashnode
{
756 struct assertion_hashnode
*next
; /* double links for easy deletion */
757 struct assertion_hashnode
*prev
;
758 /* also, a back pointer to this node's hash
759 chain is kept, in case the node is the head
760 of the chain and gets deleted. */
761 struct assertion_hashnode
**bucket_hdr
;
762 int length
; /* length of token, for quick comparison */
763 U_CHAR
*name
; /* the actual name */
764 /* List of token-sequences. */
765 struct tokenlist_list
*value
;
768 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
770 /* Some definitions for the hash table. The hash function MUST be
771 computed as shown in hashf below. That is because the rescan
772 loop computes the hash value `on the fly' for most tokens,
773 in order to avoid the overhead of a lot of procedure calls to
774 the hashf function. hashf only exists for the sake of
775 politeness, for use when speed isn't so important. */
777 #define ASSERTION_HASHSIZE 37
778 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
780 /* Nonzero means inhibit macroexpansion of what seem to be
781 assertion tests, in rescan. For #if. */
782 static int assertions_flag
;
784 /* `struct directive' defines one #-directive, including how to handle it. */
786 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
789 int length
; /* Length of name */
790 int (*func
) DO_PROTO
; /* Function to handle directive */
791 char *name
; /* Name of directive */
792 enum node_type type
; /* Code which describes which directive. */
795 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
796 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
798 /* These functions are declared to return int instead of void since they
799 are going to be placed in the table and some old compilers have trouble with
800 pointers to functions returning void. */
802 static int do_assert DO_PROTO
;
803 static int do_define DO_PROTO
;
804 static int do_elif DO_PROTO
;
805 static int do_else DO_PROTO
;
806 static int do_endif DO_PROTO
;
807 static int do_error DO_PROTO
;
808 static int do_ident DO_PROTO
;
809 static int do_if DO_PROTO
;
810 static int do_include DO_PROTO
;
811 static int do_line DO_PROTO
;
812 static int do_pragma DO_PROTO
;
813 #ifdef SCCS_DIRECTIVE
814 static int do_sccs DO_PROTO
;
816 static int do_unassert DO_PROTO
;
817 static int do_undef DO_PROTO
;
818 static int do_warning DO_PROTO
;
819 static int do_xifdef DO_PROTO
;
821 /* Here is the actual list of #-directives, most-often-used first. */
823 static struct directive directive_table
[] = {
824 { 6, do_define
, "define", T_DEFINE
},
825 { 2, do_if
, "if", T_IF
},
826 { 5, do_xifdef
, "ifdef", T_IFDEF
},
827 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
828 { 5, do_endif
, "endif", T_ENDIF
},
829 { 4, do_else
, "else", T_ELSE
},
830 { 4, do_elif
, "elif", T_ELIF
},
831 { 4, do_line
, "line", T_LINE
},
832 { 7, do_include
, "include", T_INCLUDE
},
833 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
834 { 6, do_include
, "import", T_IMPORT
},
835 { 5, do_undef
, "undef", T_UNDEF
},
836 { 5, do_error
, "error", T_ERROR
},
837 { 7, do_warning
, "warning", T_WARNING
},
838 #ifdef SCCS_DIRECTIVE
839 { 4, do_sccs
, "sccs", T_SCCS
},
841 { 6, do_pragma
, "pragma", T_PRAGMA
},
842 { 5, do_ident
, "ident", T_IDENT
},
843 { 6, do_assert
, "assert", T_ASSERT
},
844 { 8, do_unassert
, "unassert", T_UNASSERT
},
845 { -1, 0, "", T_UNUSED
},
848 /* When a directive handler is called,
849 this points to the # (or the : of the %:) that started the directive. */
850 U_CHAR
*directive_start
;
852 /* table to tell if char can be part of a C identifier. */
853 U_CHAR is_idchar
[256];
854 /* table to tell if char can be first char of a c identifier. */
855 U_CHAR is_idstart
[256];
856 /* table to tell if c is horizontal space. */
857 static U_CHAR is_hor_space
[256];
858 /* table to tell if c is horizontal or vertical space. */
859 U_CHAR is_space
[256];
860 /* names of some characters */
861 static char *char_name
[256];
863 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
864 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
866 static int errors
= 0; /* Error counter for exit code */
868 /* Name of output file, for error messages. */
869 static char *out_fname
;
871 /* Nonzero to ignore \ in string constants. Use to treat #line 1 "A:\file.h
872 as a non-form feed. If you want it to be a form feed, you must use
874 static int ignore_escape_flag
= 1;
876 /* Stack of conditionals currently in progress
877 (including both successful and failing conditionals). */
880 struct if_stack
*next
; /* for chaining to the next stack frame */
881 char *fname
; /* copied from input when frame is made */
882 size_t fname_len
; /* similarly */
883 int lineno
; /* similarly */
884 int if_succeeded
; /* true if a leg of this if-group
885 has been passed through rescan */
886 U_CHAR
*control_macro
; /* For #ifndef at start of file,
887 this is the macro name tested. */
888 enum node_type type
; /* type of last directive seen in this group */
890 typedef struct if_stack IF_STACK_FRAME
;
891 static IF_STACK_FRAME
*if_stack
= NULL
;
893 /* Buffer of -M output. */
894 static char *deps_buffer
;
896 /* Number of bytes allocated in above. */
897 static int deps_allocated_size
;
899 /* Number of bytes used. */
900 static int deps_size
;
902 /* Number of bytes since the last newline. */
903 static int deps_column
;
905 /* Nonzero means -I- has been seen,
906 so don't look for #include "foo" the source-file directory. */
907 static int ignore_srcdir
;
909 static int safe_read
PROTO((int, char *, int));
910 static void safe_write
PROTO((int, char *, int));
911 static void eprint_string
PROTO((char *, size_t));
913 int main
PROTO((int, char **));
915 static void path_include
PROTO((char *));
917 static U_CHAR
*index0
PROTO((U_CHAR
*, int, size_t));
919 static void trigraph_pcp
PROTO((FILE_BUF
*));
921 static void newline_fix
PROTO((U_CHAR
*));
922 static void name_newline_fix
PROTO((U_CHAR
*));
924 static char *get_lintcmd
PROTO((U_CHAR
*, U_CHAR
*, U_CHAR
**, int *, int *));
926 static void rescan
PROTO((FILE_BUF
*, int));
928 static FILE_BUF expand_to_temp_buffer
PROTO((U_CHAR
*, U_CHAR
*, int, int));
930 static int handle_directive
PROTO((FILE_BUF
*, FILE_BUF
*));
932 static struct tm
*timestamp
PROTO((void));
933 static void special_symbol
PROTO((HASHNODE
*, FILE_BUF
*));
935 static int is_system_include
PROTO((char *));
936 static char *base_name
PROTO((char *));
937 static int absolute_filename
PROTO((char *));
938 static size_t simplify_filename
PROTO((char *));
940 static char *read_filename_string
PROTO((int, FILE *));
941 static struct file_name_map
*read_name_map
PROTO((char *));
942 static int open_include_file
PROTO((char *, struct file_name_list
*, U_CHAR
*, struct include_file
**));
943 static char *remap_include_file
PROTO((char *, struct file_name_list
*));
944 static int lookup_ino_include
PROTO((struct include_file
*));
946 static void finclude
PROTO((int, struct include_file
*, FILE_BUF
*, int, struct file_name_list
*));
947 static void record_control_macro
PROTO((struct include_file
*, U_CHAR
*));
949 static char *check_precompiled
PROTO((int, struct stat
*, char *, char **));
950 static int check_preconditions
PROTO((char *));
951 static void pcfinclude
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
952 static void pcstring_used
PROTO((HASHNODE
*));
953 static void write_output
PROTO((void));
954 static void pass_thru_directive
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*, struct directive
*));
956 static MACRODEF create_definition
PROTO((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
958 static int check_macro_name
PROTO((U_CHAR
*, char *));
959 static int compare_defs
PROTO((DEFINITION
*, DEFINITION
*));
960 static int comp_def_part
PROTO((int, U_CHAR
*, int, U_CHAR
*, int, int));
962 static DEFINITION
*collect_expansion
PROTO((U_CHAR
*, U_CHAR
*, int, struct arglist
*));
964 int check_assertion
PROTO((U_CHAR
*, int, int, struct arglist
*));
965 static int compare_token_lists
PROTO((struct arglist
*, struct arglist
*));
967 static struct arglist
*read_token_list
PROTO((U_CHAR
**, U_CHAR
*, int *));
968 static void free_token_list
PROTO((struct arglist
*));
970 static ASSERTION_HASHNODE
*assertion_install
PROTO((U_CHAR
*, int, int));
971 static ASSERTION_HASHNODE
*assertion_lookup
PROTO((U_CHAR
*, int, int));
972 static void delete_assertion
PROTO((ASSERTION_HASHNODE
*));
974 static void do_once
PROTO((void));
976 static HOST_WIDE_INT eval_if_expression
PROTO((U_CHAR
*, int));
977 static void conditional_skip
PROTO((FILE_BUF
*, int, enum node_type
, U_CHAR
*, FILE_BUF
*));
978 static void skip_if_group
PROTO((FILE_BUF
*, int, FILE_BUF
*));
979 static void validate_else
PROTO((U_CHAR
*, U_CHAR
*));
981 static U_CHAR
*skip_to_end_of_comment
PROTO((FILE_BUF
*, int *, int));
982 static U_CHAR
*skip_quoted_string
PROTO((U_CHAR
*, U_CHAR
*, int, int *, int *, int *));
983 static char *quote_string
PROTO((char *, char *, size_t));
984 static U_CHAR
*skip_paren_group
PROTO((FILE_BUF
*));
986 /* Last arg to output_line_directive. */
987 enum file_change_code
{same_file
, enter_file
, leave_file
};
988 static void output_line_directive
PROTO((FILE_BUF
*, FILE_BUF
*, int, enum file_change_code
));
990 static void macroexpand
PROTO((HASHNODE
*, FILE_BUF
*));
993 static char *macarg
PROTO((struct argdata
*, int));
995 static U_CHAR
*macarg1
PROTO((U_CHAR
*, U_CHAR
*, struct hashnode
*, int *, int *, int *, int));
997 static int discard_comments
PROTO((U_CHAR
*, int, int));
999 static int change_newlines
PROTO((U_CHAR
*, int));
1001 static char *my_strerror
PROTO((int));
1002 void error
PRINTF_PROTO_1((char *, ...));
1003 static void verror
PROTO((char *, va_list));
1004 static void error_from_errno
PROTO((char *));
1005 void warning
PRINTF_PROTO_1((char *, ...));
1006 static void vwarning
PROTO((char *, va_list));
1007 static void error_with_line
PRINTF_PROTO_2((int, char *, ...));
1008 static void verror_with_line
PROTO((int, char *, va_list));
1009 static void vwarning_with_line
PROTO((int, char *, va_list));
1010 static void warning_with_line
PRINTF_PROTO_2((int, char *, ...));
1011 void pedwarn
PRINTF_PROTO_1((char *, ...));
1012 void pedwarn_with_line
PRINTF_PROTO_2((int, char *, ...));
1013 static void pedwarn_with_file_and_line
PRINTF_PROTO_4((char *, size_t, int, char *, ...));
1015 static void print_containing_files
PROTO((void));
1017 static int line_for_error
PROTO((int));
1018 static int grow_outbuf
PROTO((FILE_BUF
*, int));
1020 static HASHNODE
*install
PROTO((U_CHAR
*, int, enum node_type
, char *, int));
1021 HASHNODE
*lookup
PROTO((U_CHAR
*, int, int));
1022 static void delete_macro
PROTO((HASHNODE
*));
1023 static int hashf
PROTO((U_CHAR
*, int, int));
1025 static void dump_single_macro
PROTO((HASHNODE
*, FILE *));
1026 static void dump_all_macros
PROTO((void));
1027 static void dump_defn_1
PROTO((U_CHAR
*, int, int, FILE *));
1028 static void dump_arg_n
PROTO((DEFINITION
*, int, FILE *));
1030 static void initialize_char_syntax
PROTO((void));
1031 static void initialize_builtins
PROTO((FILE_BUF
*, FILE_BUF
*));
1033 static void make_definition
PROTO((char *));
1034 static void make_undef
PROTO((char *, FILE_BUF
*));
1036 static void make_assertion
PROTO((char *, char *));
1038 static struct file_name_list
*new_include_prefix
PROTO((struct file_name_list
*, char *, char *, char *));
1039 static void append_include_chain
PROTO((struct file_name_list
*, struct file_name_list
*));
1041 static int quote_string_for_make
PROTO((char *, char *));
1042 static void deps_output
PROTO((char *, int));
1044 static void fatal
PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn
));
1045 void fancy_abort
PROTO((void)) __attribute__ ((noreturn
));
1046 static void perror_with_name
PROTO((char *));
1047 static void pfatal_with_name
PROTO((char *)) __attribute__ ((noreturn
));
1048 static void pipe_closed
PROTO((int)) __attribute__ ((noreturn
));
1050 static void memory_full
PROTO((void)) __attribute__ ((noreturn
));
1051 static char *savestring
PROTO((char *));
1052 static void print_help
PROTO((void));
1054 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1055 retrying if necessary. If MAX_READ_LEN is defined, read at most
1056 that bytes at a time. Return a negative value if an error occurs,
1057 otherwise return the actual number of bytes read,
1058 which must be LEN unless end-of-file was reached. */
1061 safe_read (desc
, ptr
, len
)
1066 int left
, rcount
, nchars
;
1072 if (rcount
> MAX_READ_LEN
)
1073 rcount
= MAX_READ_LEN
;
1075 nchars
= read (desc
, ptr
, rcount
);
1092 /* Write LEN bytes at PTR to descriptor DESC,
1093 retrying if necessary, and treating any real error as fatal.
1094 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1097 safe_write (desc
, ptr
, len
)
1102 int wcount
, written
;
1106 #ifdef MAX_WRITE_LEN
1107 if (wcount
> MAX_WRITE_LEN
)
1108 wcount
= MAX_WRITE_LEN
;
1110 written
= write (desc
, ptr
, wcount
);
1117 pfatal_with_name (out_fname
);
1124 /* Print a string to stderr, with extra handling in case it contains
1125 embedded NUL characters. Any present are written as is.
1127 Using fwrite for this purpose produces undesireable results on VMS
1128 when stderr happens to be a record oriented file, such as a batch log
1129 file, rather than a stream oriented one. */
1132 eprint_string (string
, length
)
1136 size_t segment_length
;
1139 fprintf(stderr
, "%s", string
);
1140 length
-= (segment_length
= strlen(string
));
1143 fputc('\0', stderr
);
1145 /* Advance past the portion which has already been printed. */
1146 string
+= segment_length
+ 1;
1148 } while (length
> 0);
1155 printf ("Usage: %s [switches] input output\n", progname
);
1156 printf ("Switches:\n");
1157 printf (" -include <file> Include the contents of <file> before other files\n");
1158 printf (" -imacros <file> Accept definition of marcos in <file>\n");
1159 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1160 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1161 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1162 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1163 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1164 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1165 printf (" -nostdinc Do not search the system include directories\n");
1166 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1167 printf (" -o <file> Put output into <file>\n");
1168 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1169 printf (" -traditional Follow K&R pre-processor behaviour\n");
1170 printf (" -trigraphs Support ANSI C trigraphs\n");
1171 printf (" -lang-c Assume that the input sources are in C\n");
1172 printf (" -lang-c89 Assume that the input is C89; depricated\n");
1173 printf (" -lang-c++ Assume that the input sources are in C++\n");
1174 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1175 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1176 printf (" -lang-asm Assume that the input sources are in assembler\n");
1177 printf (" -lang-chill Assume that the input sources are in Chill\n");
1178 printf (" -std=<std name> Specify the conformance standard; one of:\n");
1179 printf (" gnu89, gnu9x, c89, c9x, iso9899:1990,\n");
1180 printf (" iso9899:199409, iso9899:199x\n");
1181 printf (" -+ Allow parsing of C++ style features\n");
1182 printf (" -w Inhibit warning messages\n");
1183 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1184 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1185 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1186 printf (" -Wno-comment{s} Do not warn about comments\n");
1187 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1188 printf (" a string if -tradtional is specified\n");
1189 printf (" -Wno-traditional Do not warn about stringification\n");
1190 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1191 printf (" -Wno-undef Do not warn about testing udefined macros\n");
1192 printf (" -Wimport Warn about the use of the #import directive\n");
1193 printf (" -Wno-import Do not warn about the use of #import\n");
1194 printf (" -Werror Treat all warnings as errors\n");
1195 printf (" -Wno-error Do not treat warnings as errors\n");
1196 printf (" -Wall Enable all preprocessor warnings\n");
1197 printf (" -M Generate make dependencies\n");
1198 printf (" -MM As -M, but ignore system header files\n");
1199 printf (" -MD As -M, but put output in a .d file\n");
1200 printf (" -MMD As -MD, but ignore system header files\n");
1201 printf (" -MG Treat missing header file as generated files\n");
1202 printf (" -g Include #define and #undef directives in the output\n");
1203 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1204 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1205 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1206 printf (" -U<macro> Undefine <macro> \n");
1207 printf (" -u or -undef Do not predefine any macros\n");
1208 printf (" -v Display the version number\n");
1209 printf (" -H Print the name of header files as they are used\n");
1210 printf (" -C Do not discard comments\n");
1211 printf (" -dM Display a list of macro definitions active at end\n");
1212 printf (" -dD Preserve macro definitions in output\n");
1213 printf (" -dN As -dD except that only the names are preserved\n");
1214 printf (" -dI Include #include directives in the output\n");
1215 printf (" -ifoutput Describe skipped code blocks in output \n");
1216 printf (" -P Do not generate #line directives\n");
1217 printf (" -$ Do not include '$' in identifiers\n");
1218 printf (" -remap Remap file names when including files.\n");
1219 printf (" -h or --help Display this information\n");
1232 char **pend_files
= (char **) xmalloc (argc
* sizeof (char *));
1233 char **pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
1234 char **pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
1235 char **pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
1236 char **pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
1238 /* Record the option used with each element of pend_assertions.
1239 This is preparation for supporting more than one option for making
1241 char **pend_assertion_options
= (char **) xmalloc (argc
* sizeof (char *));
1242 int inhibit_predefs
= 0;
1243 int no_standard_includes
= 0;
1244 int no_standard_cplusplus_includes
= 0;
1245 int missing_newline
= 0;
1247 /* Non-0 means don't output the preprocessed program. */
1248 int inhibit_output
= 0;
1249 /* Non-0 means -v, so print the full set of include dirs. */
1252 /* File name which deps are being written to.
1253 This is 0 if deps are being written to stdout. */
1254 char *deps_file
= 0;
1255 /* Fopen file mode to open deps_file with. */
1256 char *deps_mode
= "a";
1257 /* Stream on which to print the dependency information. */
1258 FILE *deps_stream
= 0;
1259 /* Target-name to write with the dependency information. */
1260 char *deps_target
= 0;
1262 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1263 /* Get rid of any avoidable limit on stack size. */
1267 /* Set the stack limit huge so that alloca (particularly stringtab
1268 in dbxread.c) does not fail. */
1269 getrlimit (RLIMIT_STACK
, &rlim
);
1270 rlim
.rlim_cur
= rlim
.rlim_max
;
1271 setrlimit (RLIMIT_STACK
, &rlim
);
1276 signal (SIGPIPE
, pipe_closed
);
1279 progname
= base_name (argv
[0]);
1283 /* Remove extension from PROGNAME. */
1285 char *s
= progname
= savestring (progname
);
1287 if ((p
= rindex (s
, ';')) != 0) *p
= '\0'; /* strip version number */
1288 if ((p
= rindex (s
, '.')) != 0 /* strip type iff ".exe" */
1289 && (p
[1] == 'e' || p
[1] == 'E')
1290 && (p
[2] == 'x' || p
[2] == 'X')
1291 && (p
[3] == 'e' || p
[3] == 'E')
1300 /* Initialize is_idchar. */
1301 initialize_char_syntax ();
1303 no_line_directives
= 0;
1305 dump_macros
= dump_none
;
1308 cplusplus_comments
= 1;
1310 bzero ((char *) pend_files
, argc
* sizeof (char *));
1311 bzero ((char *) pend_defs
, argc
* sizeof (char *));
1312 bzero ((char *) pend_undefs
, argc
* sizeof (char *));
1313 bzero ((char *) pend_assertions
, argc
* sizeof (char *));
1314 bzero ((char *) pend_includes
, argc
* sizeof (char *));
1316 #ifdef MULTIBYTE_CHARS
1317 /* Change to the native locale for multibyte conversions. */
1318 setlocale (LC_CTYPE
, "");
1319 literal_codeset
= getenv ("LANG");
1322 /* Process switches and find input file name. */
1324 for (i
= 1; i
< argc
; i
++) {
1325 if (argv
[i
][0] != '-') {
1326 if (out_fname
!= NULL
)
1329 fatal ("Too many arguments");
1331 else if (in_fname
!= NULL
)
1332 out_fname
= argv
[i
];
1336 switch (argv
[i
][1]) {
1339 if (!strcmp (argv
[i
], "-include")) {
1343 fatal ("Filename missing after `-include' option");
1345 simplify_filename (pend_includes
[temp
] = argv
[++i
]);
1347 if (!strcmp (argv
[i
], "-imacros")) {
1351 fatal ("Filename missing after `-imacros' option");
1353 simplify_filename (pend_files
[temp
] = argv
[++i
]);
1355 if (!strcmp (argv
[i
], "-iprefix")) {
1357 fatal ("Filename missing after `-iprefix' option");
1359 include_prefix
= argv
[++i
];
1361 if (!strcmp (argv
[i
], "-ifoutput")) {
1362 output_conditionals
= 1;
1364 if (!strcmp (argv
[i
], "-isystem")) {
1365 struct file_name_list
*dirtmp
;
1367 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1370 dirtmp
->c_system_include_path
= 1;
1372 if (before_system
== 0)
1373 before_system
= dirtmp
;
1375 last_before_system
->next
= dirtmp
;
1376 last_before_system
= dirtmp
; /* Tail follows the last one */
1378 /* Add directory to end of path for includes,
1379 with the default prefix at the front of its name. */
1380 if (!strcmp (argv
[i
], "-iwithprefix")) {
1381 struct file_name_list
*dirtmp
;
1384 if (include_prefix
!= 0)
1385 prefix
= include_prefix
;
1387 prefix
= savestring (GCC_INCLUDE_DIR
);
1388 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1389 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1390 prefix
[strlen (prefix
) - 7] = 0;
1393 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1394 prefix
, argv
[++i
])))
1397 if (after_include
== 0)
1398 after_include
= dirtmp
;
1400 last_after_include
->next
= dirtmp
;
1401 last_after_include
= dirtmp
; /* Tail follows the last one */
1403 /* Add directory to main path for includes,
1404 with the default prefix at the front of its name. */
1405 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
1406 struct file_name_list
*dirtmp
;
1409 if (include_prefix
!= 0)
1410 prefix
= include_prefix
;
1412 prefix
= savestring (GCC_INCLUDE_DIR
);
1413 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1414 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1415 prefix
[strlen (prefix
) - 7] = 0;
1418 dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
, prefix
, argv
[++i
]);
1419 append_include_chain (dirtmp
, dirtmp
);
1421 /* Add directory to end of path for includes. */
1422 if (!strcmp (argv
[i
], "-idirafter")) {
1423 struct file_name_list
*dirtmp
;
1425 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1429 if (after_include
== 0)
1430 after_include
= dirtmp
;
1432 last_after_include
->next
= dirtmp
;
1433 last_after_include
= dirtmp
; /* Tail follows the last one */
1438 if (out_fname
!= NULL
)
1439 fatal ("Output filename specified twice");
1441 fatal ("Filename missing after -o option");
1442 out_fname
= argv
[++i
];
1443 if (!strcmp (out_fname
, "-"))
1448 if (!strcmp (argv
[i
], "-pedantic"))
1450 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1452 pedantic_errors
= 1;
1453 } else if (!strcmp (argv
[i
], "-pcp")) {
1456 fatal ("Filename missing after -pcp option");
1457 pcp_fname
= argv
[++i
];
1459 = ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1460 ? fopen (pcp_fname
, "w")
1462 if (pcp_outfile
== 0)
1463 pfatal_with_name (pcp_fname
);
1469 if (!strcmp (argv
[i
], "-traditional")) {
1471 cplusplus_comments
= 0;
1472 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1478 if (! strcmp (argv
[i
], "-lang-c"))
1479 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c9x
= 1, objc
= 0;
1480 else if (! strcmp (argv
[i
], "-lang-c89"))
1481 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c9x
= 0, objc
= 0;
1482 else if (! strcmp (argv
[i
], "-lang-c++"))
1483 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c9x
= 0, objc
= 0;
1484 else if (! strcmp (argv
[i
], "-lang-objc"))
1485 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c9x
= 0, objc
= 1;
1486 else if (! strcmp (argv
[i
], "-lang-objc++"))
1487 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c9x
= 0, objc
= 1;
1488 else if (! strcmp (argv
[i
], "-lang-asm"))
1490 else if (! strcmp (argv
[i
], "-lint"))
1495 cplusplus
= 1, cplusplus_comments
= 1;
1499 if (!strcmp (argv
[i
], "-std=iso9899:1990")
1500 || !strcmp (argv
[i
], "-std=iso9899:199409")
1501 || !strcmp (argv
[i
], "-std=c89")
1502 || !strcmp (argv
[i
], "-std=gnu89"))
1503 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c9x
= 0, objc
= 0;
1504 else if (!strcmp (argv
[i
], "-std=iso9899:199x")
1505 || !strcmp (argv
[i
], "-std=c9x")
1506 || !strcmp (argv
[i
], "-std=gnu9x"))
1507 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c9x
= 1, objc
= 0;
1511 inhibit_warnings
= 1;
1515 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1517 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1519 else if (!strcmp (argv
[i
], "-Wcomment"))
1521 else if (!strcmp (argv
[i
], "-Wno-comment"))
1523 else if (!strcmp (argv
[i
], "-Wcomments"))
1525 else if (!strcmp (argv
[i
], "-Wno-comments"))
1527 else if (!strcmp (argv
[i
], "-Wtraditional"))
1529 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1531 else if (!strcmp (argv
[i
], "-Wundef"))
1533 else if (!strcmp (argv
[i
], "-Wno-undef"))
1535 else if (!strcmp (argv
[i
], "-Wimport"))
1537 else if (!strcmp (argv
[i
], "-Wno-import"))
1539 else if (!strcmp (argv
[i
], "-Werror"))
1540 warnings_are_errors
= 1;
1541 else if (!strcmp (argv
[i
], "-Wno-error"))
1542 warnings_are_errors
= 0;
1543 else if (!strcmp (argv
[i
], "-Wall"))
1551 if (!strcmp (argv
[i
], "-fleading-underscore"))
1552 user_label_prefix
= "_";
1553 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
1554 user_label_prefix
= "";
1558 /* The style of the choices here is a bit mixed.
1559 The chosen scheme is a hybrid of keeping all options in one string
1560 and specifying each option in a separate argument:
1561 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1562 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1563 -M[M][G][D file]. This is awkward to handle in specs, and is not
1565 /* ??? -MG must be specified in addition to one of -M or -MM.
1566 This can be relaxed in the future without breaking anything.
1567 The converse isn't true. */
1569 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1570 if (!strcmp (argv
[i
], "-MG"))
1572 print_deps_missing_files
= 1;
1575 if (!strcmp (argv
[i
], "-M"))
1577 else if (!strcmp (argv
[i
], "-MM"))
1579 else if (!strcmp (argv
[i
], "-MD"))
1581 else if (!strcmp (argv
[i
], "-MMD"))
1583 /* For -MD and -MMD options, write deps on file named by next arg. */
1584 if (!strcmp (argv
[i
], "-MD")
1585 || !strcmp (argv
[i
], "-MMD")) {
1587 fatal ("Filename missing after %s option", argv
[i
]);
1589 deps_file
= argv
[i
];
1592 /* For -M and -MM, write deps on standard output
1593 and suppress the usual output. */
1594 deps_stream
= stdout
;
1601 char *p
= argv
[i
] + 2;
1603 while ((c
= *p
++)) {
1604 /* Arg to -d specifies what parts of macros to dump */
1607 dump_macros
= dump_only
;
1611 dump_macros
= dump_names
;
1614 dump_macros
= dump_definitions
;
1625 if (argv
[i
][2] == '3')
1630 if (strcmp (argv
[i
], "--help") != 0)
1637 fprintf (stderr
, "GNU CPP version %s", version_string
);
1638 #ifdef TARGET_VERSION
1641 fprintf (stderr
, "\n");
1646 print_include_names
= 1;
1650 if (argv
[i
][2] != 0)
1651 pend_defs
[i
] = argv
[i
] + 2;
1652 else if (i
+ 1 == argc
)
1653 fatal ("Macro name missing after -D option");
1655 i
++, pend_defs
[i
] = argv
[i
];
1662 if (argv
[i
][2] != 0)
1664 else if (i
+ 1 == argc
)
1665 fatal ("Assertion missing after -A option");
1669 if (!strcmp (p
, "-")) {
1670 /* -A- eliminates all predefined macros and assertions.
1671 Let's include also any that were specified earlier
1672 on the command line. That way we can get rid of any
1673 that were passed automatically in from GCC. */
1675 inhibit_predefs
= 1;
1676 for (j
= 0; j
< i
; j
++)
1677 pend_defs
[j
] = pend_assertions
[j
] = 0;
1679 pend_assertions
[i
] = p
;
1680 pend_assertion_options
[i
] = "-A";
1685 case 'U': /* JF #undef something */
1686 if (argv
[i
][2] != 0)
1687 pend_undefs
[i
] = argv
[i
] + 2;
1688 else if (i
+ 1 == argc
)
1689 fatal ("Macro name missing after -U option");
1691 pend_undefs
[i
] = argv
[i
+1], i
++;
1695 put_out_comments
= 1;
1698 case 'E': /* -E comes from cc -E; ignore it. */
1702 no_line_directives
= 1;
1705 case '$': /* Don't include $ in identifiers. */
1706 is_idchar
['$'] = is_idstart
['$'] = 0;
1709 case 'I': /* Add directory to path for includes. */
1711 struct file_name_list
*dirtmp
;
1713 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-")) {
1715 /* Don't use any preceding -I directories for #include <...>. */
1716 first_bracket_include
= 0;
1719 dirtmp
= new_include_prefix (last_include
, NULL_PTR
, "",
1720 argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1721 append_include_chain (dirtmp
, dirtmp
);
1727 if (!strcmp (argv
[i
], "-nostdinc"))
1728 /* -nostdinc causes no default include directories.
1729 You must specify all include-file directories with -I. */
1730 no_standard_includes
= 1;
1731 else if (!strcmp (argv
[i
], "-nostdinc++"))
1732 /* -nostdinc++ causes no default C++-specific include directories. */
1733 no_standard_cplusplus_includes
= 1;
1734 else if (!strcmp (argv
[i
], "-noprecomp"))
1739 if (!strcmp (argv
[i
], "-remap"))
1744 /* Sun compiler passes undocumented switch "-undef".
1745 Let's assume it means to inhibit the predefined symbols. */
1746 inhibit_predefs
= 1;
1749 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1750 if (in_fname
== NULL
) {
1753 } else if (out_fname
== NULL
) {
1756 } /* else fall through into error */
1759 fatal ("Invalid option `%s'", argv
[i
]);
1764 /* Add dirs from CPATH after dirs from -I. */
1765 /* There seems to be confusion about what CPATH should do,
1766 so for the moment it is not documented. */
1767 /* Some people say that CPATH should replace the standard include dirs,
1768 but that seems pointless: it comes before them, so it overrides them
1770 GET_ENV_PATH_LIST (cp
, "CPATH");
1771 if (cp
&& ! no_standard_includes
)
1774 /* Initialize output buffer */
1776 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1777 outbuf
.bufp
= outbuf
.buf
;
1778 outbuf
.length
= OUTBUF_SIZE
;
1780 /* Do partial setup of input buffer for the sake of generating
1781 early #line directives (when -g is in effect). */
1783 fp
= &instack
[++indepth
];
1784 if (in_fname
== NULL
)
1786 fp
->nominal_fname
= fp
->fname
= in_fname
;
1787 fp
->nominal_fname_len
= strlen (in_fname
);
1790 /* In C++, wchar_t is a distinct basic type, and we can expect
1791 __wchar_t to be defined by cc1plus. */
1793 wchar_type
= "__wchar_t";
1795 /* Install __LINE__, etc. Must follow initialize_char_syntax
1796 and option processing. */
1797 initialize_builtins (fp
, &outbuf
);
1799 /* Do standard #defines and assertions
1800 that identify system and machine type. */
1802 if (!inhibit_predefs
) {
1803 char *p
= (char *) alloca (strlen (predefs
) + 1);
1806 struct dsc$descriptor_s lcl_name
;
1808 unsigned short length
; /* input length */
1809 unsigned short code
; /* item code */
1810 unsigned long dptr
; /* data ptr */
1811 unsigned long lptr
; /* output length ptr */
1814 unsigned long syi_length
;
1817 struct item_list items
[] = {
1818 { 16, SYI$_VERSION
, 0, 0 },
1822 items
[0].dptr
= (unsigned long)syi_data
;
1823 items
[0].lptr
= (unsigned long)(&syi_length
);
1825 if (SYS$
GETSYIW (0, 0, 0, items
, NULL
, NULL
, NULL
, NULL
) == SS$_NORMAL
)
1827 unsigned long vms_version_value
;
1831 vms_version_value
= 0;
1835 if (ISDIGIT (*vers
))
1837 vms_version_value
= (*vers
- '0') * 10000000;
1843 if (ISDIGIT (*vers
))
1845 vms_version_value
+= (*vers
- '0') * 100000;
1849 if (vms_version_value
> 0)
1853 sprintf (versbuf
, "__VMS_VER=%08ld", vms_version_value
);
1855 output_line_directive (fp
, &outbuf
, 0, same_file
);
1856 make_definition (versbuf
);
1861 strcpy (p
, predefs
);
1864 while (*p
== ' ' || *p
== '\t')
1866 /* Handle -D options. */
1867 if (p
[0] == '-' && p
[1] == 'D') {
1869 while (*p
&& *p
!= ' ' && *p
!= '\t')
1874 output_line_directive (fp
, &outbuf
, 0, same_file
);
1875 make_definition (q
);
1876 while (*p
== ' ' || *p
== '\t')
1878 } else if (p
[0] == '-' && p
[1] == 'A') {
1879 /* Handle -A options (assertions). */
1888 past_name
= assertion
;
1889 /* Locate end of name. */
1890 while (*past_name
&& *past_name
!= ' '
1891 && *past_name
!= '\t' && *past_name
!= '(')
1893 /* Locate `(' at start of value. */
1895 while (*value
&& (*value
== ' ' || *value
== '\t'))
1897 if (*value
++ != '(')
1899 while (*value
&& (*value
== ' ' || *value
== '\t'))
1902 /* Locate end of value. */
1903 while (*past_value
&& *past_value
!= ' '
1904 && *past_value
!= '\t' && *past_value
!= ')')
1906 termination
= past_value
;
1907 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
1909 if (*termination
++ != ')')
1911 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
1913 /* Temporarily null-terminate the value. */
1914 save_char
= *termination
;
1915 *termination
= '\0';
1916 /* Install the assertion. */
1917 make_assertion ("-A", assertion
);
1918 *termination
= (char) save_char
;
1920 while (*p
== ' ' || *p
== '\t')
1928 /* Now handle the command line options. */
1930 /* Do -U's, -D's and -A's in the order they were seen. */
1931 for (i
= 1; i
< argc
; i
++) {
1932 if (pend_undefs
[i
]) {
1934 output_line_directive (fp
, &outbuf
, 0, same_file
);
1935 make_undef (pend_undefs
[i
], &outbuf
);
1939 output_line_directive (fp
, &outbuf
, 0, same_file
);
1940 make_definition (pend_defs
[i
]);
1942 if (pend_assertions
[i
])
1943 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1946 done_initializing
= 1;
1948 { /* Read the appropriate environment variable and if it exists
1949 replace include_defaults with the listed path. */
1951 switch ((objc
<< 1) + cplusplus
)
1954 GET_ENV_PATH_LIST (epath
, "C_INCLUDE_PATH");
1957 GET_ENV_PATH_LIST (epath
, "CPLUS_INCLUDE_PATH");
1960 GET_ENV_PATH_LIST (epath
, "OBJC_INCLUDE_PATH");
1963 GET_ENV_PATH_LIST (epath
, "OBJCPLUS_INCLUDE_PATH");
1966 /* If the environment var for this language is set,
1967 add to the default list of include directories. */
1970 char *startp
, *endp
;
1972 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1973 if (*startp
== PATH_SEPARATOR
)
1976 = (struct default_include
*) xmalloc ((num_dirs
1977 * sizeof (struct default_include
))
1978 + sizeof (include_defaults_array
));
1979 startp
= endp
= epath
;
1983 if (c
== PATH_SEPARATOR
|| !c
) {
1985 include_defaults
[num_dirs
].fname
1986 = startp
== endp
? "." : savestring (startp
);
1988 include_defaults
[num_dirs
].component
= 0;
1989 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1990 include_defaults
[num_dirs
].cxx_aware
= 1;
1997 /* Put the usual defaults back in at the end. */
1998 bcopy ((char *) include_defaults_array
,
1999 (char *) &include_defaults
[num_dirs
],
2000 sizeof (include_defaults_array
));
2004 append_include_chain (before_system
, last_before_system
);
2005 first_system_include
= before_system
;
2007 /* Unless -fnostdinc,
2008 tack on the standard include file dirs to the specified list */
2009 if (!no_standard_includes
) {
2010 struct default_include
*p
= include_defaults
;
2011 char *specd_prefix
= include_prefix
;
2012 char *default_prefix
= savestring (GCC_INCLUDE_DIR
);
2013 int default_len
= 0;
2014 /* Remove the `include' from /usr/local/lib/gcc.../include. */
2015 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
2016 default_len
= strlen (default_prefix
) - 7;
2017 default_prefix
[default_len
] = 0;
2019 /* Search "translated" versions of GNU directories.
2020 These have /usr/local/lib/gcc... replaced by specd_prefix. */
2021 if (specd_prefix
!= 0 && default_len
!= 0)
2022 for (p
= include_defaults
; p
->fname
; p
++) {
2023 /* Some standard dirs are only for C++. */
2024 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
2025 /* Does this dir start with the prefix? */
2026 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
2027 /* Yes; change prefix and add to search list. */
2028 struct file_name_list
*new
2029 = new_include_prefix (NULL_PTR
, NULL_PTR
, specd_prefix
,
2030 p
->fname
+ default_len
);
2032 new->c_system_include_path
= !p
->cxx_aware
;
2033 append_include_chain (new, new);
2034 if (first_system_include
== 0)
2035 first_system_include
= new;
2040 /* Search ordinary names for GNU include directories. */
2041 for (p
= include_defaults
; p
->fname
; p
++) {
2042 /* Some standard dirs are only for C++. */
2043 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
2044 struct file_name_list
*new
2045 = new_include_prefix (NULL_PTR
, p
->component
, "", p
->fname
);
2047 new->c_system_include_path
= !p
->cxx_aware
;
2048 append_include_chain (new, new);
2049 if (first_system_include
== 0)
2050 first_system_include
= new;
2056 /* Tack the after_include chain at the end of the include chain. */
2057 append_include_chain (after_include
, last_after_include
);
2058 if (first_system_include
== 0)
2059 first_system_include
= after_include
;
2061 /* With -v, print the list of dirs to search. */
2063 struct file_name_list
*p
;
2064 fprintf (stderr
, "#include \"...\" search starts here:\n");
2065 for (p
= include
; p
; p
= p
->next
) {
2066 if (p
== first_bracket_include
)
2067 fprintf (stderr
, "#include <...> search starts here:\n");
2069 fprintf (stderr
, " .\n");
2070 else if (!strcmp (p
->fname
, "/") || !strcmp (p
->fname
, "//"))
2071 fprintf (stderr
, " %s\n", p
->fname
);
2073 /* Omit trailing '/'. */
2074 fprintf (stderr
, " %.*s\n", (int) strlen (p
->fname
) - 1, p
->fname
);
2076 fprintf (stderr
, "End of search list.\n");
2079 /* -MG doesn't select the form of output and must be specified with one of
2080 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
2081 inhibit compilation. */
2082 if (print_deps_missing_files
&& (print_deps
== 0 || !inhibit_output
))
2083 fatal ("-MG must be specified with one of -M or -MM");
2085 /* Either of two environment variables can specify output of deps.
2086 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
2087 where OUTPUT_FILE is the file to write deps info to
2088 and DEPS_TARGET is the target to mention in the deps. */
2091 && (getenv ("SUNPRO_DEPENDENCIES") != 0
2092 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
2093 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
2098 spec
= getenv ("SUNPRO_DEPENDENCIES");
2105 /* Find the space before the DEPS_TARGET, if there is one. */
2106 /* This should use index. (mrs) */
2107 while (*s
!= 0 && *s
!= ' ') s
++;
2109 deps_target
= s
+ 1;
2110 output_file
= xmalloc (s
- spec
+ 1);
2111 bcopy (spec
, output_file
, s
- spec
);
2112 output_file
[s
- spec
] = 0;
2119 deps_file
= output_file
;
2123 /* For -M, print the expected object file name
2124 as the target of this Make-rule. */
2126 deps_allocated_size
= 200;
2127 deps_buffer
= xmalloc (deps_allocated_size
);
2133 deps_output (deps_target
, ':');
2134 } else if (*in_fname
== 0) {
2135 deps_output ("-", ':');
2140 q
= base_name (in_fname
);
2142 /* Copy remainder to mungable area. */
2143 p
= (char *) alloca (strlen(q
) + 8);
2146 /* Output P, but remove known suffixes. */
2150 && p
[len
- 2] == '.'
2151 && index("cCsSm", p
[len
- 1]))
2154 && p
[len
- 3] == '.'
2155 && p
[len
- 2] == 'c'
2156 && p
[len
- 1] == 'c')
2159 && p
[len
- 4] == '.'
2160 && p
[len
- 3] == 'c'
2161 && p
[len
- 2] == 'x'
2162 && p
[len
- 1] == 'x')
2165 && p
[len
- 4] == '.'
2166 && p
[len
- 3] == 'c'
2167 && p
[len
- 2] == 'p'
2168 && p
[len
- 1] == 'p')
2171 /* Supply our own suffix. */
2172 strcpy (q
, OBJECT_SUFFIX
);
2174 deps_output (p
, ':');
2175 deps_output (in_fname
, ' ');
2179 /* Scan the -imacros files before the main input.
2180 Much like #including them, but with no_output set
2181 so that only their macro definitions matter. */
2183 no_output
++; no_record_file
++;
2184 for (i
= 1; i
< argc
; i
++)
2185 if (pend_files
[i
]) {
2186 struct include_file
*inc
;
2187 int fd
= open_include_file (pend_files
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2189 perror_with_name (pend_files
[i
]);
2190 return FATAL_EXIT_CODE
;
2192 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2194 no_output
--; no_record_file
--;
2196 /* Copy the entire contents of the main input file into
2197 the stacked input buffer previously allocated for it. */
2199 /* JF check for stdin */
2200 if (in_fname
== NULL
|| *in_fname
== 0) {
2203 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
2206 if (fstat (f
, &st
) != 0)
2207 pfatal_with_name (in_fname
);
2208 fp
->nominal_fname
= fp
->fname
= in_fname
;
2209 fp
->nominal_fname_len
= strlen (in_fname
);
2211 fp
->system_header_p
= 0;
2212 /* JF all this is mine about reading pipes and ttys */
2213 if (! S_ISREG (st
.st_mode
)) {
2214 /* Read input from a file that is not a normal disk file.
2215 We cannot preallocate a buffer with the correct size,
2216 so we must read in the file a piece at the time and make it bigger. */
2221 if (S_ISDIR (st
.st_mode
))
2222 fatal ("Input file `%s' is a directory", in_fname
);
2226 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
2228 cnt
= safe_read (f
, (char *) fp
->buf
+ size
, bsize
- size
);
2229 if (cnt
< 0) goto perror
; /* error! */
2231 if (size
!= bsize
) break; /* End of file */
2233 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
2237 /* Read a file whose size we can determine in advance.
2238 For the sake of VMS, st.st_size is just an upper bound. */
2239 size_t s
= (size_t) st
.st_size
;
2240 if (s
!= st
.st_size
|| s
+ 2 < s
)
2242 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
2243 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
2244 if (fp
->length
< 0) goto perror
;
2247 fp
->if_stack
= if_stack
;
2249 /* Make sure data ends with a newline. And put a null after it. */
2251 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
2252 /* Backslash-newline at end is not good enough. */
2253 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
2254 fp
->buf
[fp
->length
++] = '\n';
2255 missing_newline
= 1;
2257 fp
->buf
[fp
->length
] = '\0';
2259 /* Unless inhibited, convert trigraphs in the input. */
2264 /* Now that we know the input file is valid, open the output. */
2266 if (!out_fname
|| !strcmp (out_fname
, ""))
2267 out_fname
= "stdout";
2268 else if (! freopen (out_fname
, "w", stdout
))
2269 pfatal_with_name (out_fname
);
2271 output_line_directive (fp
, &outbuf
, 0, same_file
);
2273 /* Scan the -include files before the main input. */
2276 for (i
= 1; i
< argc
; i
++)
2277 if (pend_includes
[i
]) {
2278 struct include_file
*inc
;
2279 int fd
= open_include_file (pend_includes
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2281 perror_with_name (pend_includes
[i
]);
2282 return FATAL_EXIT_CODE
;
2284 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2288 /* Scan the input, processing macros and directives. */
2290 rescan (&outbuf
, 0);
2292 if (missing_newline
)
2295 if (pedantic
&& missing_newline
)
2296 pedwarn ("file does not end in newline");
2298 /* Now we have processed the entire input
2299 Write whichever kind of output has been requested. */
2301 if (dump_macros
== dump_only
)
2303 else if (! inhibit_output
) {
2308 /* Don't actually write the deps file if compilation has failed. */
2310 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, deps_mode
)))
2311 pfatal_with_name (deps_file
);
2312 fputs (deps_buffer
, deps_stream
);
2313 putc ('\n', deps_stream
);
2315 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
2316 fatal ("I/O error on output");
2321 if (pcp_outfile
&& pcp_outfile
!= stdout
2322 && (ferror (pcp_outfile
) || fclose (pcp_outfile
) != 0))
2323 fatal ("I/O error on `-pcp' output");
2325 if (ferror (stdout
) || fclose (stdout
) != 0)
2326 fatal ("I/O error on output");
2329 exit (FATAL_EXIT_CODE
);
2330 exit (SUCCESS_EXIT_CODE
);
2333 pfatal_with_name (in_fname
);
2337 /* Given a colon-separated list of file names PATH,
2338 add all the names to the search path for include files. */
2352 struct file_name_list
*dirtmp
;
2354 /* Find the end of this name. */
2355 while ((c
= *q
++) != PATH_SEPARATOR
&& c
)
2359 dirtmp
= new_include_prefix (last_include
, NULL_PTR
,
2360 "", p
== q
? "." : p
);
2362 append_include_chain (dirtmp
, dirtmp
);
2364 /* Advance past this name. */
2371 /* Return the address of the first character in S that equals C.
2372 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2373 Return 0 if there is no such character. Assume that C itself is not '\0'.
2374 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2375 but unfortunately memchr isn't autoconfigured yet. */
2383 char *p
= (char *) s
;
2385 char *q
= index (p
, c
);
2387 return (U_CHAR
*) q
;
2389 size_t l
= strlen (p
);
2399 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2400 before main CCCP processing. Name `pcp' is also in honor of the
2401 drugs the trigraph designers must have been on.
2403 Using an extra pass through the buffer takes a little extra time,
2404 but is infinitely less hairy than trying to handle trigraphs inside
2405 strings, etc. everywhere, and also makes sure that trigraphs are
2406 only translated in the top level of processing. */
2412 register U_CHAR c
, *fptr
, *bptr
, *sptr
, *lptr
;
2415 fptr
= bptr
= sptr
= buf
->buf
;
2416 lptr
= fptr
+ buf
->length
;
2417 while ((sptr
= index0 (sptr
, '?', (size_t) (lptr
- sptr
))) != NULL
) {
2454 len
= sptr
- fptr
- 2;
2456 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2457 C, this will be memmove (). */
2458 if (bptr
!= fptr
&& len
> 0)
2459 bcopy ((char *) fptr
, (char *) bptr
, len
);
2465 len
= buf
->length
- (fptr
- buf
->buf
);
2466 if (bptr
!= fptr
&& len
> 0)
2467 bcopy ((char *) fptr
, (char *) bptr
, len
);
2468 buf
->length
-= fptr
- bptr
;
2469 buf
->buf
[buf
->length
] = '\0';
2470 if (warn_trigraphs
&& fptr
!= bptr
)
2471 warning_with_line (0, "%lu trigraph(s) encountered",
2472 (unsigned long) (fptr
- bptr
) / 2);
2475 /* Move all backslash-newline pairs out of embarrassing places.
2476 Exchange all such pairs following BP
2477 with any potentially-embarrassing characters that follow them.
2478 Potentially-embarrassing characters are / and *
2479 (because a backslash-newline inside a comment delimiter
2480 would cause it not to be recognized). */
2486 register U_CHAR
*p
= bp
;
2488 /* First count the backslash-newline pairs here. */
2490 while (p
[0] == '\\' && p
[1] == '\n')
2493 /* What follows the backslash-newlines is not embarrassing. */
2495 if (*p
!= '/' && *p
!= '*')
2498 /* Copy all potentially embarrassing characters
2499 that follow the backslash-newline pairs
2500 down to where the pairs originally started. */
2502 while (*p
== '*' || *p
== '/')
2505 /* Now write the same number of pairs after the embarrassing chars. */
2512 /* Like newline_fix but for use within a directive-name.
2513 Move any backslash-newlines up past any following symbol constituents. */
2516 name_newline_fix (bp
)
2519 register U_CHAR
*p
= bp
;
2521 /* First count the backslash-newline pairs here. */
2522 while (p
[0] == '\\' && p
[1] == '\n')
2525 /* What follows the backslash-newlines is not embarrassing. */
2530 /* Copy all potentially embarrassing characters
2531 that follow the backslash-newline pairs
2532 down to where the pairs originally started. */
2534 while (is_idchar
[*p
])
2537 /* Now write the same number of pairs after the embarrassing chars. */
2544 /* Look for lint commands in comments.
2546 When we come in here, ibp points into a comment. Limit is as one expects.
2547 scan within the comment -- it should start, after lwsp, with a lint command.
2548 If so that command is returned as a (constant) string.
2550 Upon return, any arg will be pointed to with argstart and will be
2551 arglen long. Note that we don't parse that arg since it will just
2552 be printed out again. */
2555 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2556 register U_CHAR
*ibp
;
2557 register U_CHAR
*limit
;
2558 U_CHAR
**argstart
; /* point to command arg */
2559 int *arglen
, *cmdlen
; /* how long they are */
2561 HOST_WIDE_INT linsize
;
2562 register U_CHAR
*numptr
; /* temp for arg parsing */
2566 SKIP_WHITE_SPACE (ibp
);
2568 if (ibp
>= limit
) return NULL
;
2570 linsize
= limit
- ibp
;
2572 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2573 if ((linsize
>= 10) && !bcmp (ibp
, "NOTREACHED", 10)) {
2575 return "NOTREACHED";
2577 if ((linsize
>= 8) && !bcmp (ibp
, "ARGSUSED", 8)) {
2581 if ((linsize
>= 11) && !bcmp (ibp
, "LINTLIBRARY", 11)) {
2583 return "LINTLIBRARY";
2585 if ((linsize
>= 7) && !bcmp (ibp
, "VARARGS", 7)) {
2587 ibp
+= 7; linsize
-= 7;
2588 if ((linsize
== 0) || ! ISDIGIT (*ibp
)) return "VARARGS";
2590 /* OK, read a number */
2591 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && ISDIGIT (*numptr
);
2593 *arglen
= numptr
- *argstart
;
2600 * The main loop of the program.
2602 * Read characters from the input stack, transferring them to the
2605 * Macros are expanded and push levels on the input stack.
2606 * At the end of such a level it is popped off and we keep reading.
2607 * At the end of any other kind of level, we return.
2608 * #-directives are handled, except within macros.
2610 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2611 * and insert them when appropriate. This is set while scanning macro
2612 * arguments before substitution. It is zero when scanning for final output.
2613 * There are three types of Newline markers:
2614 * * Newline - follows a macro name that was not expanded
2615 * because it appeared inside an expansion of the same macro.
2616 * This marker prevents future expansion of that identifier.
2617 * When the input is rescanned into the final output, these are deleted.
2618 * These are also deleted by ## concatenation.
2619 * * Newline Space (or Newline and any other whitespace character)
2620 * stands for a place that tokens must be separated or whitespace
2621 * is otherwise desirable, but where the ANSI standard specifies there
2622 * is no whitespace. This marker turns into a Space (or whichever other
2623 * whitespace char appears in the marker) in the final output,
2624 * but it turns into nothing in an argument that is stringified with #.
2625 * Such stringified arguments are the only place where the ANSI standard
2626 * specifies with precision that whitespace may not appear.
2628 * During this function, IP->bufp is kept cached in IBP for speed of access.
2629 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2630 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2631 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2632 * explicitly, and before RECACHE, since RECACHE uses OBP.
2636 rescan (op
, output_marks
)
2640 /* Character being scanned in main loop. */
2643 /* Length of pending accumulated identifier. */
2644 register int ident_length
= 0;
2646 /* Hash code of pending accumulated identifier. */
2647 register int hash
= 0;
2649 /* Current input level (&instack[indepth]). */
2652 /* Pointer for scanning input. */
2653 register U_CHAR
*ibp
;
2655 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2656 register U_CHAR
*limit
;
2658 /* Pointer for storing output. */
2659 register U_CHAR
*obp
;
2661 /* REDO_CHAR is nonzero if we are processing an identifier
2662 after backing up over the terminating character.
2663 Sometimes we process an identifier without backing up over
2664 the terminating character, if the terminating character
2665 is not special. Backing up is done so that the terminating character
2666 will be dispatched on again once the identifier is dealt with. */
2669 /* 1 if within an identifier inside of which a concatenation
2670 marker (Newline -) has been seen. */
2671 int concatenated
= 0;
2673 /* While scanning a comment or a string constant,
2674 this records the line it started on, for error messages. */
2677 /* Record position of last `real' newline. */
2678 U_CHAR
*beg_of_line
;
2680 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2683 do { ip->macro->type = T_MACRO; \
2684 if (ip->free_ptr) free (ip->free_ptr); \
2685 --indepth; } while (0)
2687 /* Reload `rescan's local variables that describe the current
2688 level of the input stack. */
2691 do { ip = &instack[indepth]; \
2693 limit = ip->buf + ip->length; \
2695 check_expand (op, limit - ibp); \
2697 obp = op->bufp; } while (0)
2699 if (no_output
&& instack
[indepth
].fname
!= 0)
2700 skip_if_group (&instack
[indepth
], 1, NULL
);
2707 /* Our caller must always put a null after the end of
2708 the input at each input stack level. */
2718 if (*ibp
== '\n' && !ip
->macro
) {
2719 /* At the top level, always merge lines ending with backslash-newline,
2720 even in middle of identifier. But do not merge lines in a macro,
2721 since backslash might be followed by a newline-space marker. */
2724 --obp
; /* remove backslash from obuf */
2727 /* If ANSI, backslash is just another character outside a string. */
2730 /* Otherwise, backslash suppresses specialness of following char,
2731 so copy it here to prevent the switch from seeing it.
2732 But first get any pending identifier processed. */
2733 if (ident_length
> 0)
2740 if (ident_length
|| ip
->macro
|| traditional
)
2742 while (*ibp
== '\\' && ibp
[1] == '\n') {
2748 /* Treat this %: digraph as if it were #. */
2752 if (assertions_flag
) {
2755 /* Copy #foo (bar lose) without macro expansion. */
2756 obp
[-1] = '#'; /* In case it was '%'. */
2757 SKIP_WHITE_SPACE (ibp
);
2758 while (is_idchar
[*ibp
])
2760 SKIP_WHITE_SPACE (ibp
);
2763 skip_paren_group (ip
);
2764 bcopy ((char *) ibp
, (char *) obp
, ip
->bufp
- ibp
);
2765 obp
+= ip
->bufp
- ibp
;
2771 /* If this is expanding a macro definition, don't recognize
2772 preprocessing directives. */
2775 /* If this is expand_into_temp_buffer,
2776 don't recognize them either. Warn about them
2777 only after an actual newline at this level,
2778 not at the beginning of the input level. */
2780 if (ip
->buf
!= beg_of_line
)
2781 warning ("preprocessing directive not recognized within macro arg");
2788 /* # keyword: a # must be first nonblank char on the line */
2789 if (beg_of_line
== 0)
2794 /* Scan from start of line, skipping whitespace, comments
2795 and backslash-newlines, and see if we reach this #.
2796 If not, this # is not special. */
2798 /* If -traditional, require # to be at beginning of line. */
2801 if (is_hor_space
[*bp
])
2803 else if (*bp
== '\\' && bp
[1] == '\n')
2805 else if (*bp
== '/' && bp
[1] == '*') {
2819 #ifdef MULTIBYTE_CHARS
2821 length
= local_mblen (bp
, limit
- bp
);
2829 /* There is no point in trying to deal with C++ // comments here,
2830 because if there is one, then this # must be part of the
2831 comment and we would never reach here. */
2837 while (bp
[1] == '\\' && bp
[2] == '\n')
2841 /* %: appears at start of line; skip past the ':' too. */
2850 /* This # can start a directive. */
2852 --obp
; /* Don't copy the '#' */
2856 if (! handle_directive (ip
, op
)) {
2860 /* Not a known directive: treat it as ordinary text.
2861 IP, OP, IBP, etc. have not been changed. */
2862 if (no_output
&& instack
[indepth
].fname
) {
2863 /* If not generating expanded output,
2864 what we do with ordinary text is skip it.
2865 Discard everything until next # directive. */
2866 skip_if_group (&instack
[indepth
], 1, 0);
2871 *obp
++ = '#'; /* Copy # (even if it was originally %:). */
2872 /* Don't expand an identifier that could be a macro directive.
2873 (Section 3.8.3 of the ANSI C standard) */
2874 SKIP_WHITE_SPACE (ibp
);
2875 if (is_idstart
[*ibp
])
2878 while (is_idchar
[*ibp
])
2886 /* A # directive has been successfully processed. */
2887 /* If not generating expanded output, ignore everything until
2888 next # directive. */
2889 if (no_output
&& instack
[indepth
].fname
)
2890 skip_if_group (&instack
[indepth
], 1, 0);
2896 case '\"': /* skip quoted string */
2898 /* A single quoted string is treated like a double -- some
2899 programs (e.g., troff) are perverse this way */
2901 /* Handle any pending identifier;
2902 but the L in L'...' or L"..." is not an identifier. */
2904 if (! (ident_length
== 1 && hash
== HASHSTEP (0, 'L')))
2906 ident_length
= hash
= 0;
2909 start_line
= ip
->lineno
;
2911 /* Skip ahead to a matching quote. */
2915 if (ip
->macro
!= 0) {
2916 /* try harder: this string crosses a macro expansion boundary.
2917 This can happen naturally if -traditional.
2918 Otherwise, only -D can make a macro with an unmatched quote. */
2924 error_with_line (line_for_error (start_line
),
2925 "unterminated string or character constant");
2926 if (multiline_string_line
) {
2927 error_with_line (multiline_string_line
,
2928 "possible real start of unterminated constant");
2929 multiline_string_line
= 0;
2939 /* Traditionally, end of line ends a string constant with no error.
2940 So exit the loop and record the new line. */
2946 error_with_line (line_for_error (start_line
),
2947 "unterminated character constant");
2950 if (multiline_string_line
== 0) {
2952 pedwarn_with_line (line_for_error (start_line
),
2953 "string constant runs past end of line");
2954 multiline_string_line
= ip
->lineno
- 1;
2960 /* Backslash newline is replaced by nothing at all, but
2961 keep the line counts correct. But if we are reading
2962 from a macro, keep the backslash newline, since backslash
2963 newlines have already been processed. */
2971 /* ANSI stupidly requires that in \\ the second \
2972 is *not* prevented from combining with a newline. */
2974 while (*ibp
== '\\' && ibp
[1] == '\n') {
2988 #ifdef MULTIBYTE_CHARS
2993 length
= local_mblen (ibp
, limit
- ibp
);
2997 bcopy (ibp
, obp
, length
);
3014 if (*ibp
== '\\' && ibp
[1] == '\n')
3017 && !(cplusplus_comments
&& *ibp
== '/'))
3023 /* C++ style comment... */
3024 start_line
= ip
->lineno
;
3026 /* Comments are equivalent to spaces. */
3027 if (! put_out_comments
)
3031 U_CHAR
*before_bp
= ibp
;
3033 while (++ibp
< limit
) {
3036 if (put_out_comments
) {
3037 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
3038 obp
+= ibp
- before_bp
;
3044 if (ibp
+ 1 < limit
&& ibp
[1] == '\n')
3047 warning ("multiline `//' comment");
3049 /* Copy the newline into the output buffer, in order to
3050 avoid the pain of a #line every time a multiline comment
3052 if (!put_out_comments
)
3060 #ifdef MULTIBYTE_CHARS
3062 length
= local_mblen (ibp
, limit
- ibp
);
3064 ibp
+= (length
- 1);
3072 /* Ordinary C comment. Skip it, optionally copying it to output. */
3074 start_line
= ip
->lineno
;
3076 ++ibp
; /* Skip the star. */
3078 /* If this cpp is for lint, we peek inside the comments: */
3082 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
3084 if (lintcmd
!= NULL
) {
3086 check_expand (op
, cmdlen
+ arglen
+ 14);
3088 /* I believe it is always safe to emit this newline: */
3090 bcopy ("#pragma lint ", (char *) obp
, 13);
3092 bcopy (lintcmd
, (char *) obp
, cmdlen
);
3097 bcopy (argbp
, (char *) obp
, arglen
);
3101 /* OK, now bring us back to the state we were in before we entered
3102 this branch. We need #line because the #pragma's newline always
3103 messes up the line count. */
3105 output_line_directive (ip
, op
, 0, same_file
);
3106 check_expand (op
, limit
- ibp
+ 2);
3112 /* Comments are equivalent to spaces.
3113 Note that we already output the slash; we might not want it.
3114 For -traditional, a comment is equivalent to nothing. */
3115 if (! put_out_comments
) {
3125 U_CHAR
*before_bp
= ibp
;
3130 if (ibp
[-2] == '/' && warn_comments
)
3131 warning ("`/*' within comment");
3132 if (*ibp
== '\\' && ibp
[1] == '\n')
3140 /* Copy the newline into the output buffer, in order to
3141 avoid the pain of a #line every time a multiline comment
3143 if (!put_out_comments
)
3150 error_with_line (line_for_error (start_line
),
3151 "unterminated comment");
3155 #ifdef MULTIBYTE_CHARS
3159 length
= local_mblen (ibp
, limit
- ibp
);
3161 ibp
+= (length
- 1);
3170 if (put_out_comments
) {
3171 bcopy ((char *) before_bp
, (char *) obp
, ibp
- before_bp
);
3172 obp
+= ibp
- before_bp
;
3178 if (! is_idchar
['$'])
3181 pedwarn ("`$' in identifier");
3184 case '0': case '1': case '2': case '3': case '4':
3185 case '5': case '6': case '7': case '8': case '9':
3186 /* If digit is not part of identifier, it starts a number,
3187 which means that following letters are not an identifier.
3188 "0x5" does not refer to an identifier "x5".
3189 So copy all alphanumerics that follow without accumulating
3190 as an identifier. Periods also, for sake of "3.e7". */
3192 if (ident_length
== 0) {
3195 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3201 if (!is_idchar
[c
] && c
!= '.') {
3206 /* A sign can be part of a preprocessing number
3207 if it follows an `e' or `p'. */
3208 if (c
== 'e' || c
== 'E' || c
== 'p' || c
== 'P') {
3210 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3215 if (*ibp
== '+' || *ibp
== '-') {
3217 /* But traditional C does not let the token go past the sign,
3218 and C89 does not allow `p'. */
3219 if (traditional
|| (c89
&& (c
== 'p' || c
== 'P')))
3229 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3230 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3231 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3232 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3234 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3235 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3236 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3237 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3241 /* Compute step of hash function, to avoid a proc call on every token */
3242 hash
= HASHSTEP (hash
, c
);
3246 if (ip
->fname
== 0 && *ibp
== '-') {
3247 /* Newline - inhibits expansion of preceding token.
3248 If expanding a macro arg, we keep the newline -.
3249 In final output, it is deleted.
3250 We recognize Newline - in macro bodies and macro args. */
3251 if (! concatenated
) {
3256 if (!output_marks
) {
3259 /* If expanding a macro arg, keep the newline -. */
3265 /* If reprocessing a macro expansion, newline is a special marker. */
3266 else if (ip
->macro
!= 0) {
3267 /* Newline White is a "funny space" to separate tokens that are
3268 supposed to be separate but without space between.
3269 Here White means any whitespace character.
3270 Newline - marks a recursive macro use that is not
3271 supposed to be expandable. */
3273 if (is_space
[*ibp
]) {
3274 /* Newline Space does not prevent expansion of preceding token
3275 so expand the preceding token and then come back. */
3276 if (ident_length
> 0)
3279 /* If generating final output, newline space makes a space. */
3280 if (!output_marks
) {
3282 /* And Newline Newline makes a newline, so count it. */
3283 if (obp
[-1] == '\n')
3286 /* If expanding a macro arg, keep the newline space.
3287 If the arg gets stringified, newline space makes nothing. */
3290 } else abort (); /* Newline followed by something random? */
3294 /* If there is a pending identifier, handle it and come back here. */
3295 if (ident_length
> 0)
3300 /* Update the line counts and output a #line if necessary. */
3303 if (ip
->lineno
!= op
->lineno
) {
3305 output_line_directive (ip
, op
, 1, same_file
);
3306 check_expand (op
, limit
- ibp
);
3311 /* Come here either after (1) a null character that is part of the input
3312 or (2) at the end of the input, because there is a null there. */
3315 /* Our input really contains a null character. */
3319 /* At end of a macro-expansion level, pop it and read next level. */
3320 if (ip
->macro
!= 0) {
3323 /* If traditional, and we have an identifier that ends here,
3324 process it now, so we get the right error for recursion. */
3325 if (traditional
&& ident_length
3326 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
3335 /* If we don't have a pending identifier,
3336 return at end of input. */
3337 if (ident_length
== 0) {
3345 /* If we do have a pending identifier, just consider this null
3346 a special character and arrange to dispatch on it again.
3347 The second time, IDENT_LENGTH will be zero so we will return. */
3353 /* Handle the case of a character such as /, ', " or null
3354 seen following an identifier. Back over it so that
3355 after the identifier is processed the special char
3356 will be dispatched on again. */
3366 if (ident_length
> 0) {
3367 register HASHNODE
*hp
;
3369 /* We have just seen an identifier end. If it's a macro, expand it.
3371 IDENT_LENGTH is the length of the identifier
3372 and HASH is its hash code.
3374 The identifier has already been copied to the output,
3375 so if it is a macro we must remove it.
3377 If REDO_CHAR is 0, the char that terminated the identifier
3378 has been skipped in the output and the input.
3379 OBP-IDENT_LENGTH-1 points to the identifier.
3380 If the identifier is a macro, we must back over the terminator.
3382 If REDO_CHAR is 1, the terminating char has already been
3383 backed over. OBP-IDENT_LENGTH points to the identifier. */
3385 if (!pcp_outfile
|| pcp_inside_if
) {
3386 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
3389 if (hp
->length
== ident_length
) {
3390 int obufp_before_macroname
;
3391 int op_lineno_before_macroname
;
3392 register int i
= ident_length
;
3393 register U_CHAR
*p
= hp
->name
;
3394 register U_CHAR
*q
= obp
- i
;
3400 do { /* All this to avoid a strncmp () */
3405 /* We found a use of a macro name.
3406 see if the context shows it is a macro call. */
3408 /* Back up over terminating character if not already done. */
3414 /* Save this as a displacement from the beginning of the output
3415 buffer. We can not save this as a position in the output
3416 buffer, because it may get realloc'ed by RECACHE. */
3417 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
3418 op_lineno_before_macroname
= op
->lineno
;
3420 if (hp
->type
== T_PCSTRING
) {
3421 pcstring_used (hp
); /* Mark the definition of this key
3422 as needed, ensuring that it
3424 break; /* Exit loop, since the key cannot have a
3425 definition any longer. */
3428 /* Record whether the macro is disabled. */
3429 disabled
= hp
->type
== T_DISABLED
;
3431 /* This looks like a macro ref, but if the macro was disabled,
3432 just copy its name and put in a marker if requested. */
3436 /* This error check caught useful cases such as
3437 #define foo(x,y) bar (x (y,0), y)
3440 error ("recursive use of macro `%s'", hp
->name
);
3444 check_expand (op
, limit
- ibp
+ 2);
3451 /* If macro wants an arglist, verify that a '(' follows.
3452 first skip all whitespace, copying it to the output
3453 after the macro name. Then, if there is no '(',
3454 decide this is not a macro call and leave things that way. */
3455 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
3456 && hp
->value
.defn
->nargs
>= 0)
3458 U_CHAR
*old_ibp
= ibp
;
3459 U_CHAR
*old_obp
= obp
;
3460 int old_iln
= ip
->lineno
;
3461 int old_oln
= op
->lineno
;
3464 /* Scan forward over whitespace, copying it to the output. */
3465 if (ibp
== limit
&& ip
->macro
!= 0) {
3470 old_iln
= ip
->lineno
;
3471 old_oln
= op
->lineno
;
3473 else if (is_space
[*ibp
]) {
3475 if (ibp
[-1] == '\n') {
3476 if (ip
->macro
== 0) {
3477 /* Newline in a file. Count it. */
3480 } else if (!output_marks
) {
3481 /* A newline mark, and we don't want marks
3482 in the output. If it is newline-hyphen,
3483 discard it entirely. Otherwise, it is
3484 newline-whitechar, so keep the whitechar. */
3494 /* A newline mark; copy both chars to the output. */
3501 else if (*ibp
== '/') {
3502 /* If a comment, copy it unchanged or discard it. */
3503 if (ibp
[1] == '\\' && ibp
[2] == '\n')
3504 newline_fix (ibp
+ 1);
3505 if (ibp
[1] == '*') {
3506 if (put_out_comments
) {
3509 } else if (! traditional
) {
3512 for (ibp
+= 2; ibp
< limit
; ibp
++) {
3513 /* We need not worry about newline-marks,
3514 since they are never found in comments. */
3515 if (ibp
[0] == '*') {
3516 if (ibp
[1] == '\\' && ibp
[2] == '\n')
3517 newline_fix (ibp
+ 1);
3518 if (ibp
[1] == '/') {
3520 if (put_out_comments
) {
3527 else if (*ibp
== '\n') {
3528 /* Newline in a file. Count it. */
3534 #ifdef MULTIBYTE_CHARS
3536 length
= local_mblen (ibp
, limit
- ibp
);
3539 if (put_out_comments
)
3541 bcopy (ibp
, obp
, length
- 1);
3544 ibp
+= (length
- 1);
3548 if (put_out_comments
)
3551 } else if (ibp
[1] == '/' && cplusplus_comments
) {
3552 if (put_out_comments
) {
3555 } else if (! traditional
) {
3558 for (ibp
+= 2; ; ibp
++)
3562 if (*ibp
== '\\' && ibp
[1] == '\n')
3564 if (put_out_comments
)
3569 #ifdef MULTIBYTE_CHARS
3571 length
= local_mblen (ibp
, limit
- ibp
);
3574 if (put_out_comments
)
3576 bcopy (ibp
, obp
, length
- 1);
3579 ibp
+= (length
- 1);
3583 if (put_out_comments
)
3589 else if (ibp
[0] == '\\' && ibp
[1] == '\n') {
3596 /* It isn't a macro call.
3597 Put back the space that we just skipped. */
3600 ip
->lineno
= old_iln
;
3601 op
->lineno
= old_oln
;
3602 /* Exit the for loop. */
3607 /* This is now known to be a macro call.
3608 Discard the macro name from the output,
3609 along with any following whitespace just copied,
3610 but preserve newlines if not outputting marks since this
3611 is more likely to do the right thing with line numbers. */
3612 obp
= op
->buf
+ obufp_before_macroname
;
3614 op
->lineno
= op_lineno_before_macroname
;
3616 int newlines
= op
->lineno
- op_lineno_before_macroname
;
3617 while (0 < newlines
--)
3621 /* Prevent accidental token-pasting with a character
3622 before the macro call. */
3623 if (!traditional
&& obp
!= op
->buf
) {
3625 case '!': case '%': case '&': case '*':
3626 case '+': case '-': case '.': case '/':
3627 case ':': case '<': case '=': case '>':
3629 /* If we are expanding a macro arg, make a newline marker
3630 to separate the tokens. If we are making real output,
3631 a plain space will do. */
3638 /* Expand the macro, reading arguments as needed,
3639 and push the expansion on the input stack. */
3642 macroexpand (hp
, op
);
3644 /* Reexamine input stack, since macroexpand has pushed
3645 a new level on it. */
3652 } /* End hash-table-search loop */
3654 ident_length
= hash
= 0; /* Stop collecting identifier */
3657 } /* End if (ident_length > 0) */
3659 } /* End per-char loop */
3661 /* Come here to return -- but first give an error message
3662 if there was an unterminated successful conditional. */
3664 if (if_stack
!= ip
->if_stack
)
3668 switch (if_stack
->type
)
3689 error_with_line (line_for_error (if_stack
->lineno
),
3690 "unterminated `#%s' conditional", str
);
3692 if_stack
= ip
->if_stack
;
3696 * Rescan a string into a temporary buffer and return the result
3697 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3699 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3700 * and insert such markers when appropriate. See `rescan' for details.
3701 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3702 * before substitution; it is 0 for other uses.
3705 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
3706 U_CHAR
*buf
, *limit
;
3707 int output_marks
, assertions
;
3709 register FILE_BUF
*ip
;
3711 int length
= limit
- buf
;
3713 int odepth
= indepth
;
3714 int save_assertions_flag
= assertions_flag
;
3716 assertions_flag
= assertions
;
3721 /* Set up the input on the input stack. */
3723 buf1
= (U_CHAR
*) alloca (length
+ 1);
3725 register U_CHAR
*p1
= buf
;
3726 register U_CHAR
*p2
= buf1
;
3733 /* Set up to receive the output. */
3735 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3736 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3737 obuf
.nominal_fname
= 0;
3744 obuf
.system_header_p
= 0;
3746 CHECK_DEPTH ({return obuf
;});
3750 ip
= &instack
[indepth
];
3752 ip
->nominal_fname
= 0;
3753 ip
->nominal_fname_len
= 0;
3755 ip
->system_header_p
= 0;
3758 ip
->length
= length
;
3759 ip
->buf
= ip
->bufp
= buf1
;
3760 ip
->if_stack
= if_stack
;
3762 ip
->lineno
= obuf
.lineno
= 1;
3764 /* Scan the input, create the output. */
3765 rescan (&obuf
, output_marks
);
3767 /* Pop input stack to original state. */
3770 if (indepth
!= odepth
)
3773 /* Record the output. */
3774 obuf
.length
= obuf
.bufp
- obuf
.buf
;
3776 assertions_flag
= save_assertions_flag
;
3781 * Process a # directive. Expects IP->bufp to point after the '#', as in
3782 * `#define foo bar'. Passes to the directive handler
3783 * (do_define, do_include, etc.): the addresses of the 1st and
3784 * last chars of the directive (starting immediately after the #
3785 * keyword), plus op and the keyword table pointer. If the directive
3786 * contains comments it is copied into a temporary buffer sans comments
3787 * and the temporary buffer is passed to the directive handler instead.
3788 * Likewise for backslash-newlines.
3790 * Returns nonzero if this was a known # directive.
3791 * Otherwise, returns zero, without advancing the input pointer.
3795 handle_directive (ip
, op
)
3798 register U_CHAR
*bp
, *cp
;
3799 register struct directive
*kt
;
3800 register int ident_length
;
3803 /* Nonzero means we must copy the entire directive
3804 to get rid of comments or backslash-newlines. */
3805 int copy_directive
= 0;
3807 U_CHAR
*ident
, *after_ident
;
3811 /* Record where the directive started. do_xifdef needs this. */
3812 directive_start
= bp
- 1;
3814 ignore_escape_flag
= 1;
3816 /* Skip whitespace and \-newline. */
3818 if (is_hor_space
[*bp
]) {
3819 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3820 pedwarn ("%s in preprocessing directive", char_name
[*bp
]);
3822 } else if (*bp
== '/') {
3823 if (bp
[1] == '\\' && bp
[2] == '\n')
3824 newline_fix (bp
+ 1);
3825 if (! (bp
[1] == '*' || (cplusplus_comments
&& bp
[1] == '/')))
3828 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3830 } else if (*bp
== '\\' && bp
[1] == '\n') {
3831 bp
+= 2; ip
->lineno
++;
3835 /* Now find end of directive name.
3836 If we encounter a backslash-newline, exchange it with any following
3837 symbol-constituents so that we end up with a contiguous name. */
3844 if (*cp
== '\\' && cp
[1] == '\n')
3845 name_newline_fix (cp
);
3851 ident_length
= cp
- bp
;
3855 /* A line of just `#' becomes blank. */
3857 if (ident_length
== 0 && *after_ident
== '\n') {
3858 ip
->bufp
= after_ident
;
3862 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3864 while (is_idchar
[*p
]) {
3865 if (*p
< '0' || *p
> '9')
3869 /* Handle # followed by a line number. */
3870 if (p
!= ident
&& !is_idchar
[*p
]) {
3871 static struct directive line_directive_table
[] = {
3872 { 4, do_line
, "line", T_LINE
},
3875 pedwarn ("`#' followed by integer");
3876 after_ident
= ident
;
3877 kt
= line_directive_table
;
3878 ignore_escape_flag
= 0;
3882 /* Avoid error for `###' and similar cases unless -pedantic. */
3884 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3886 if (pedantic
&& !lang_asm
)
3887 warning ("invalid preprocessing directive");
3893 error ("invalid preprocessing directive name");
3899 * Decode the keyword and call the appropriate expansion
3900 * routine, after moving the input pointer up to the next line.
3902 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3903 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3904 register U_CHAR
*buf
;
3905 register U_CHAR
*limit
;
3908 int *already_output
;
3910 /* Nonzero means do not delete comments within the directive.
3911 #define needs this when -traditional. */
3916 limit
= ip
->buf
+ ip
->length
;
3919 keep_comments
= traditional
&& kt
->type
== T_DEFINE
;
3920 /* #import is defined only in Objective C, or when on the NeXT. */
3921 if (kt
->type
== T_IMPORT
3922 && !(objc
|| lookup ((U_CHAR
*) "__NeXT__", -1, -1)))
3925 /* Find the end of this directive (first newline not backslashed
3926 and not in a string or comment).
3927 Set COPY_DIRECTIVE if the directive must be copied
3928 (it contains a backslash-newline or a comment). */
3930 buf
= bp
= after_ident
;
3931 while (bp
< limit
) {
3932 register U_CHAR c
= *bp
++;
3940 } else if (traditional
)
3946 /* "..." is special for #include. */
3947 if (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)) {
3948 while (bp
< limit
&& *bp
!= '\n') {
3953 if (*bp
== '\\' && bp
[1] == '\n') {
3964 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3965 /* Don't bother calling the directive if we already got an error
3966 message due to unterminated string. Skip everything and pretend
3967 we called the directive. */
3970 /* Traditional preprocessing permits unterminated strings. */
3979 /* <...> is special for #include. */
3981 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3983 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3984 if (*bp
== '\\' && bp
[1] == '\n') {
3994 if (*bp
== '\\' && bp
[1] == '\n')
3997 || (cplusplus_comments
&& *bp
== '/')) {
3998 U_CHAR
*obp
= bp
- 1;
4000 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
4002 /* No need to copy the directive because of a comment at the end;
4003 just don't include the comment in the directive. */
4004 if (!put_out_comments
) {
4006 for (p
= bp
; *p
== ' ' || *p
== '\t'; p
++)
4013 /* Don't remove the comments if -traditional. */
4014 if (! keep_comments
)
4023 pedwarn ("%s in preprocessing directive", char_name
[c
]);
4027 --bp
; /* Point to the newline */
4035 resume_p
= ip
->bufp
;
4036 /* BP is the end of the directive.
4037 RESUME_P is the next interesting data after the directive.
4038 A comment may come between. */
4040 /* If a directive should be copied through, and -C was given,
4041 pass it through before removing comments. */
4042 if (!no_output
&& put_out_comments
4043 && (kt
->type
== T_DEFINE
? dump_macros
== dump_definitions
4044 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
4045 : kt
->type
== T_PRAGMA
)) {
4048 /* Output directive name. */
4049 check_expand (op
, kt
->length
+ 2);
4050 /* Make sure # is at the start of a line */
4051 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
4056 bcopy (kt
->name
, op
->bufp
, kt
->length
);
4057 op
->bufp
+= kt
->length
;
4059 /* Output arguments. */
4061 check_expand (op
, len
);
4062 bcopy (buf
, (char *) op
->bufp
, len
);
4064 /* Take account of any (escaped) newlines just output. */
4066 if (buf
[len
] == '\n')
4069 already_output
= &junk
;
4070 } /* Don't we need a newline or #line? */
4072 if (copy_directive
) {
4073 register U_CHAR
*xp
= buf
;
4074 /* Need to copy entire directive into temp buffer before dispatching */
4076 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for directive plus
4080 /* Copy to the new buffer, deleting comments
4081 and backslash-newlines (and whitespace surrounding the latter). */
4084 register U_CHAR c
= *xp
++;
4089 abort (); /* A bare newline should never part of the line. */
4092 /* <...> is special for #include. */
4094 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
4096 while (xp
< bp
&& c
!= '>') {
4098 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
4109 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
4110 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
4112 SKIP_WHITE_SPACE (xp
);
4113 } else if (is_hor_space
[*xp
]) {
4115 SKIP_WHITE_SPACE (xp
);
4117 } else if (traditional
&& xp
< bp
) {
4125 register U_CHAR
*bp1
4126 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
4127 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4135 || (cplusplus_comments
&& *xp
== '/')) {
4137 /* If we already copied the directive through,
4138 already_output != 0 prevents outputting comment now. */
4139 skip_to_end_of_comment (ip
, already_output
, 0);
4141 while (xp
!= ip
->bufp
)
4143 /* Delete or replace the slash. */
4144 else if (traditional
)
4153 /* Null-terminate the copy. */
4159 ip
->bufp
= resume_p
;
4161 /* Some directives should be written out for cc1 to process,
4162 just as if they were not defined. And sometimes we're copying
4163 directives through. */
4165 if (!no_output
&& already_output
== 0
4166 && (kt
->type
== T_DEFINE
? (int) dump_names
<= (int) dump_macros
4167 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
4168 : kt
->type
== T_PRAGMA
)) {
4171 /* Output directive name. */
4172 check_expand (op
, kt
->length
+ 1);
4174 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
4175 op
->bufp
+= kt
->length
;
4177 if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
4178 /* Output `#define name' only. */
4181 SKIP_WHITE_SPACE (xp
);
4183 while (is_idchar
[*xp
]) xp
++;
4185 check_expand (op
, len
+ 1);
4187 bcopy (yp
, (char *) op
->bufp
, len
);
4189 /* Output entire directive. */
4191 check_expand (op
, len
);
4192 bcopy (buf
, (char *) op
->bufp
, len
);
4195 } /* Don't we need a newline or #line? */
4197 /* Call the appropriate directive handler. buf now points to
4198 either the appropriate place in the input buffer, or to
4199 the temp buffer if it was necessary to make one. cp
4200 points to the first char after the contents of the (possibly
4201 copied) directive, in either case. */
4202 (*kt
->func
) (buf
, cp
, op
, kt
);
4203 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
4209 /* It is deliberate that we don't warn about undefined directives.
4210 That is the responsibility of cc1. */
4217 static struct tm
*timebuf
;
4219 time_t t
= time ((time_t *) 0);
4220 timebuf
= localtime (&t
);
4225 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4226 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4230 * expand things like __FILE__. Place the expansion into the output
4231 * buffer *without* rescanning.
4235 special_symbol (hp
, op
)
4242 FILE_BUF
*ip
= NULL
;
4245 int paren
= 0; /* For special `defined' keyword */
4247 if (pcp_outfile
&& pcp_inside_if
4248 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4249 error ("Predefined macro `%s' used inside `#if' during precompilation",
4252 for (i
= indepth
; i
>= 0; i
--)
4253 if (instack
[i
].fname
!= NULL
) {
4258 error ("cccp error: not in any file?!");
4259 return; /* the show must go on */
4266 FILE_BUF
*p
= hp
->type
== T_FILE
? ip
: &instack
[0];
4267 char *string
= p
->nominal_fname
;
4271 size_t string_len
= p
->nominal_fname_len
;
4272 buf
= (char *) alloca (3 + 4 * string_len
);
4273 quote_string (buf
, string
, string_len
);
4281 case T_INCLUDE_LEVEL
:
4283 for (i
= indepth
; i
>= 0; i
--)
4284 if (instack
[i
].fname
!= NULL
)
4287 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
4288 sprintf (buf
, "%d", true_indepth
- 1);
4292 buf
= (char *) alloca (3 + strlen (version_string
));
4293 sprintf (buf
, "\"%s\"", version_string
);
4296 #ifndef NO_BUILTIN_SIZE_TYPE
4302 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4303 case T_PTRDIFF_TYPE
:
4312 case T_USER_LABEL_PREFIX_TYPE
:
4313 buf
= user_label_prefix
;
4316 case T_REGISTER_PREFIX_TYPE
:
4317 buf
= REGISTER_PREFIX
;
4320 case T_IMMEDIATE_PREFIX_TYPE
:
4321 buf
= IMMEDIATE_PREFIX
;
4325 buf
= hp
->value
.cpval
;
4326 #ifdef STDC_0_IN_SYSTEM_HEADERS
4327 if (ip
->system_header_p
4328 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
4329 && !lookup ((U_CHAR
*) "__STRICT_ANSI__", -1, -1))
4332 if (pcp_inside_if
&& pcp_outfile
)
4333 /* Output a precondition for this macro use */
4334 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4338 buf
= (char *) alloca (10);
4339 sprintf (buf
, "%d", ip
->lineno
);
4344 buf
= (char *) alloca (20);
4345 timebuf
= timestamp ();
4346 if (hp
->type
== T_DATE
)
4347 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4348 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4350 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
4354 case T_SPEC_DEFINED
:
4355 buf
= " 0 "; /* Assume symbol is not defined */
4356 ip
= &instack
[indepth
];
4357 SKIP_WHITE_SPACE (ip
->bufp
);
4358 if (*ip
->bufp
== '(') {
4360 ip
->bufp
++; /* Skip over the paren */
4361 SKIP_WHITE_SPACE (ip
->bufp
);
4364 if (!is_idstart
[*ip
->bufp
])
4366 if (ip
->bufp
[0] == 'L' && (ip
->bufp
[1] == '\'' || ip
->bufp
[1] == '"'))
4368 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4369 if (pcp_outfile
&& pcp_inside_if
4370 && (hp
->type
== T_CONST
4371 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4372 /* Output a precondition for this macro use. */
4373 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4377 if (pcp_outfile
&& pcp_inside_if
) {
4378 /* Output a precondition for this macro use */
4379 U_CHAR
*cp
= ip
->bufp
;
4380 fprintf (pcp_outfile
, "#undef ");
4381 while (is_idchar
[*cp
]) /* Ick! */
4382 fputc (*cp
++, pcp_outfile
);
4383 putc ('\n', pcp_outfile
);
4385 while (is_idchar
[*ip
->bufp
])
4387 SKIP_WHITE_SPACE (ip
->bufp
);
4389 if (*ip
->bufp
!= ')')
4397 error ("`defined' without an identifier");
4401 error ("cccp error: invalid special hash type"); /* time for gdb */
4405 check_expand (op
, len
);
4406 bcopy (buf
, (char *) op
->bufp
, len
);
4413 /* Routines to handle #directives */
4415 /* Handle #include and #import.
4416 This function expects to see "fname" or <fname> on the input. */
4419 do_include (buf
, limit
, op
, keyword
)
4420 U_CHAR
*buf
, *limit
;
4422 struct directive
*keyword
;
4424 U_CHAR
*importing
= keyword
->type
== T_IMPORT
? (U_CHAR
*) "" : (U_CHAR
*) 0;
4425 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4426 static int import_warning
= 0;
4427 char *fname
; /* Dynamically allocated fname buffer */
4430 char *fbeg
, *fend
; /* Beginning and end of fname */
4433 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4434 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4435 struct file_name_list
*searchptr
= 0;
4438 int f
= -3; /* file number */
4439 struct include_file
*inc
= 0;
4441 int retried
= 0; /* Have already tried macro
4442 expanding the include line*/
4443 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4445 int vaxc_include
= 0; /* 1 for token without punctuation */
4452 if (pedantic
&& !instack
[indepth
].system_header_p
)
4455 pedwarn ("ANSI C does not allow `#import'");
4457 pedwarn ("ANSI C does not allow `#include_next'");
4460 if (importing
&& warn_import
&& !inhibit_warnings
4461 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4463 warning ("using `#import' is not recommended");
4464 fprintf (stderr
, "The fact that a certain header file need not be processed more than once\n");
4465 fprintf (stderr
, "should be indicated in the header file, not where it is used.\n");
4466 fprintf (stderr
, "The best way to do this is with a conditional of this form:\n\n");
4467 fprintf (stderr
, " #ifndef _FOO_H_INCLUDED\n");
4468 fprintf (stderr
, " #define _FOO_H_INCLUDED\n");
4469 fprintf (stderr
, " ... <real contents of file> ...\n");
4470 fprintf (stderr
, " #endif /* Not _FOO_H_INCLUDED */\n\n");
4471 fprintf (stderr
, "Then users can use `#include' any number of times.\n");
4472 fprintf (stderr
, "GNU C automatically avoids processing the file more than once\n");
4473 fprintf (stderr
, "when it is equipped with such a conditional.\n");
4479 SKIP_WHITE_SPACE (fin
);
4480 /* Discard trailing whitespace so we can easily see
4481 if we have parsed all the significant chars we were given. */
4482 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4483 fbeg
= fend
= (char *) alloca (limit
- fin
);
4489 /* Copy the operand text, concatenating the strings. */
4494 goto invalid_include_file_name
;
4502 /* If not at the end, there had better be another string. */
4503 /* Skip just horiz space, and don't go past limit. */
4504 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4505 if (fin
!= limit
&& *fin
== '\"')
4512 /* We have "filename". Figure out directory this source
4513 file is coming from and put it on the front of the list. */
4515 /* If -I- was specified, don't search current dir, only spec'd ones. */
4516 if (ignore_srcdir
) break;
4518 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4523 if ((nam
= fp
->nominal_fname
) != NULL
) {
4524 /* Found a named file. Figure out dir of the file,
4525 and put it in front of the search list. */
4526 dsp
= ((struct file_name_list
*)
4527 alloca (sizeof (struct file_name_list
)
4528 + fp
->nominal_fname_len
));
4529 strcpy (dsp
->fname
, nam
);
4530 simplify_filename (dsp
->fname
);
4531 nam
= base_name (dsp
->fname
);
4534 /* for hack_vms_include_specification(), a local
4535 dir specification must start with "./" on VMS. */
4536 if (nam
== dsp
->fname
)
4543 /* But for efficiency's sake, do not insert the dir
4544 if it matches the search list's first dir. */
4545 dsp
->next
= search_start
;
4546 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4548 n
= nam
- dsp
->fname
;
4549 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4550 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4552 dsp
[0].got_name_map
= 0;
4560 while (fin
!= limit
&& *fin
!= '>')
4562 if (*fin
== '>' && fin
+ 1 == limit
) {
4564 /* If -I-, start with the first -I dir after the -I-. */
4565 search_start
= first_bracket_include
;
4573 * Support '#include xyz' like VAX-C to allow for easy use of all the
4574 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4575 * code from case '<' is repeated here) and generates a warning.
4576 * (Note: macro expansion of `xyz' takes precedence.)
4578 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4579 the pre-decrement outside of the macro. */
4580 if (retried
&& (--fin
, ISALPHA(*(U_CHAR
*) (fin
)))) {
4581 while (fin
!= limit
&& (!ISSPACE(*fin
)))
4583 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4587 /* If -I-, start with the first -I dir after the -I-. */
4588 search_start
= first_bracket_include
;
4596 /* Expand buffer and then remove any newline markers.
4597 We can't just tell expand_to_temp_buffer to omit the markers,
4598 since it would put extra spaces in include file names. */
4601 int errors_before_expansion
= errors
;
4602 trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4603 if (errors
!= errors_before_expansion
) {
4605 goto invalid_include_file_name
;
4608 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4610 while (src
!= trybuf
.bufp
) {
4611 switch ((*limit
++ = *src
++)) {
4620 U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4621 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4634 invalid_include_file_name
:
4635 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4639 /* For #include_next, skip in the search path
4640 past the dir in which the containing file was found. */
4643 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4644 if (fp
->fname
!= NULL
) {
4645 /* fp->dir is null if the containing file was specified
4646 with an absolute file name. In that case, don't skip anything. */
4648 search_start
= fp
->dir
->next
;
4654 flen
= simplify_filename (fbeg
);
4658 error ("empty file name in `#%s'", keyword
->name
);
4662 /* Allocate this permanently, because it gets stored in the definitions
4664 fname
= xmalloc (max_include_len
+ flen
+ 1);
4665 /* + 1 above for terminating null. */
4667 system_include_depth
+= angle_brackets
;
4669 /* If specified file name is absolute, just open it. */
4671 if (absolute_filename (fbeg
)) {
4672 strcpy (fname
, fbeg
);
4673 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4677 struct bypass_dir
*next
;
4679 struct file_name_list
*searchptr
;
4680 } **bypass_slot
= 0;
4682 /* Search directory path, trying to open the file.
4683 Copy each filename tried into FNAME. */
4685 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4687 if (searchptr
== first_bracket_include
) {
4688 /* Go to bypass directory if we know we've seen this file before. */
4689 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4690 struct bypass_dir
*p
;
4691 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4693 for (p
= *bypass_slot
; p
; p
= p
->next
)
4694 if (!strcmp (fbeg
, p
->fname
)) {
4695 searchptr
= p
->searchptr
;
4702 /* Change this 1/2 Unix 1/2 VMS file specification into a
4703 full VMS file specification */
4704 if (searchptr
->fname
[0])
4706 strcpy (fname
, searchptr
->fname
);
4707 if (fname
[strlen (fname
) - 1] == ':')
4710 slashp
= strchr (fbeg
, '/');
4712 /* start at root-dir of logical device if no path given. */
4714 strcat (fname
, "[000000]");
4716 strcat (fname
, fbeg
);
4718 /* Fix up the filename */
4719 hack_vms_include_specification (fname
, vaxc_include
);
4723 /* This is a normal VMS filespec, so use it unchanged. */
4724 strcpy (fname
, fbeg
);
4725 /* if it's '#include filename', add the missing .h */
4726 if (vaxc_include
&& index(fname
,'.')==NULL
)
4727 strcat (fname
, ".h");
4730 strcpy (fname
, searchptr
->fname
);
4731 strcat (fname
, fbeg
);
4733 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4735 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4736 /* This is the first time we found this include file,
4737 and we found it after first_bracket_include.
4738 Record its location so that we can bypass to here next time. */
4739 struct bypass_dir
*p
4740 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4741 p
->next
= *bypass_slot
;
4742 p
->fname
= fname
+ strlen (searchptr
->fname
);
4743 p
->searchptr
= searchptr
;
4749 /* Our VMS hacks can produce invalid filespecs, so don't worry
4750 about errors other than EACCES. */
4751 if (errno
== EACCES
)
4754 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
4764 /* The file was already included. */
4766 /* If generating dependencies and -MG was specified, we assume missing
4767 files are leaf files, living in the same directory as the source file
4768 or other similar place; these missing files may be generated from
4769 other files and may not exist yet (eg: y.tab.h). */
4770 } else if (print_deps_missing_files
4771 && (system_include_depth
!= 0) < print_deps
)
4773 /* If it was requested as a system header file,
4774 then assume it belongs in the first place to look for such. */
4778 char *p
= (char *) alloca (strlen (search_start
->fname
)
4779 + strlen (fbeg
) + 1);
4780 strcpy (p
, search_start
->fname
);
4782 deps_output (p
, ' ');
4787 /* Otherwise, omit the directory, as if the file existed
4788 in the directory with the source. */
4789 deps_output (fbeg
, ' ');
4792 /* If -M was specified, and this header file won't be added to the
4793 dependency list, then don't count this as an error, because we can
4794 still produce correct output. Otherwise, we can't produce correct
4795 output, because there may be dependencies we need inside the missing
4796 file, and we don't know what directory this missing file exists in. */
4797 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4798 warning ("No include path in which to find %s", fbeg
);
4800 error_from_errno (fbeg
);
4802 error ("No include path in which to find %s", fbeg
);
4806 /* Actually process the file. */
4808 pcftry
= (char *) alloca (strlen (fname
) + 30);
4815 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4817 pcf
= open (pcftry
, O_RDONLY
, 0666);
4822 if (fstat (pcf
, &s
) != 0)
4823 pfatal_with_name (pcftry
);
4824 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4825 || inc
->st
.st_dev
!= s
.st_dev
)
4827 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4828 /* Don't need it any more. */
4833 /* Don't need it at all. */
4838 } while (pcf
!= -1 && !pcfbuf
);
4841 /* Actually process the file */
4843 pcfname
= xmalloc (strlen (pcftry
) + 1);
4844 strcpy (pcfname
, pcftry
);
4845 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) fname
, op
);
4848 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4851 system_include_depth
-= angle_brackets
;
4856 /* Return nonzero if the given FILENAME is an absolute pathname which
4857 designates a file within one of the known "system" include file
4858 directories. We assume here that if the given FILENAME looks like
4859 it is the name of a file which resides either directly in a "system"
4860 include file directory, or within any subdirectory thereof, then the
4861 given file must be a "system" include file. This function tells us
4862 if we should suppress pedantic errors/warnings for the given FILENAME.
4864 The value is 2 if the file is a C-language system header file
4865 for which C++ should (on most systems) assume `extern "C"'. */
4868 is_system_include (filename
)
4869 register char *filename
;
4871 struct file_name_list
*searchptr
;
4873 for (searchptr
= first_system_include
; searchptr
;
4874 searchptr
= searchptr
->next
)
4875 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4876 return searchptr
->c_system_include_path
+ 1;
4880 /* Yield the non-directory suffix of a file name. */
4888 #if defined (__MSDOS__) || defined (_WIN32)
4889 if (ISALPHA (s
[0]) && s
[1] == ':') s
+= 2;
4892 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4893 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4894 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4898 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4899 #ifdef DIR_SEPARATOR
4900 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4905 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4908 absolute_filename (filename
)
4911 #if defined (__MSDOS__) || (defined (_WIN32) && !defined (__CYGWIN__))
4912 if (ISALPHA (filename
[0]) && filename
[1] == ':') filename
+= 2;
4914 #if defined (__CYGWIN__)
4915 /* At present, any path that begins with a drive spec is absolute. */
4916 if (ISALPHA (filename
[0]) && filename
[1] == ':') return 1;
4919 if (index (filename
, ':') != 0) return 1;
4921 if (filename
[0] == '/') return 1;
4922 #ifdef DIR_SEPARATOR
4923 if (filename
[0] == DIR_SEPARATOR
) return 1;
4928 /* Remove unnecessary characters from FILENAME in place,
4929 to avoid unnecessary filename aliasing.
4930 Return the length of the resulting string.
4932 Do only the simplifications allowed by Posix.
4933 It is OK to miss simplifications on non-Posix hosts,
4934 since this merely leads to suboptimal results. */
4937 simplify_filename (filename
)
4940 register char *from
= filename
;
4941 register char *to
= filename
;
4944 /* Remove redundant initial /s. */
4947 if (*++from
== '/') {
4948 if (*++from
== '/') {
4949 /* 3 or more initial /s are equivalent to 1 /. */
4950 while (*++from
== '/')
4953 /* On some hosts // differs from /; Posix allows this. */
4954 static int slashslash_vs_slash
;
4955 if (slashslash_vs_slash
== 0) {
4957 slashslash_vs_slash
= ((stat ("/", &s1
) == 0 && stat ("//", &s2
) == 0
4958 && INO_T_EQ (s1
.st_ino
, s2
.st_ino
)
4959 && s1
.st_dev
== s2
.st_dev
)
4962 if (slashslash_vs_slash
< 0)
4971 if (from
[0] == '.' && from
[1] == '/')
4976 /* Copy this component and trailing /, if any. */
4977 while ((*to
++ = *from
++) != '/') {
4979 /* Trim . component at end of nonempty name. */
4980 to
-= filename
<= to
- 3 && to
[-3] == '/' && to
[-2] == '.';
4982 /* Trim unnecessary trailing /s. */
4983 while (to0
< --to
&& to
[-1] == '/')
4987 return to
- filename
;
4992 /* Skip /s after a /. */
4993 while (*from
== '/')
4998 /* The file_name_map structure holds a mapping of file names for a
4999 particular directory. This mapping is read from the file named
5000 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5001 map filenames on a file system with severe filename restrictions,
5002 such as DOS. The format of the file name map file is just a series
5003 of lines with two tokens on each line. The first token is the name
5004 to map, and the second token is the actual name to use. */
5006 struct file_name_map
5008 struct file_name_map
*map_next
;
5013 #define FILE_NAME_MAP_FILE "header.gcc"
5015 /* Read a space delimited string of unlimited length from a stdio
5019 read_filename_string (ch
, f
)
5027 set
= alloc
= xmalloc (len
+ 1);
5031 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5033 if (set
- alloc
== len
)
5036 alloc
= xrealloc (alloc
, len
+ 1);
5037 set
= alloc
+ len
/ 2;
5047 /* Read the file name map file for DIRNAME.
5048 If DIRNAME is empty, read the map file for the working directory;
5049 otherwise DIRNAME must end in '/'. */
5051 static struct file_name_map
*
5052 read_name_map (dirname
)
5055 /* This structure holds a linked list of file name maps, one per
5057 struct file_name_map_list
5059 struct file_name_map_list
*map_list_next
;
5060 char *map_list_name
;
5061 struct file_name_map
*map_list_map
;
5063 static struct file_name_map_list
*map_list
;
5064 register struct file_name_map_list
*map_list_ptr
;
5069 for (map_list_ptr
= map_list
; map_list_ptr
;
5070 map_list_ptr
= map_list_ptr
->map_list_next
)
5071 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5072 return map_list_ptr
->map_list_map
;
5074 map_list_ptr
= ((struct file_name_map_list
*)
5075 xmalloc (sizeof (struct file_name_map_list
)));
5076 map_list_ptr
->map_list_name
= savestring (dirname
);
5077 map_list_ptr
->map_list_map
= NULL
;
5079 dirlen
= strlen (dirname
);
5080 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
5081 strcpy (name
, dirname
);
5082 strcat (name
, FILE_NAME_MAP_FILE
);
5083 f
= fopen (name
, "r");
5085 map_list_ptr
->map_list_map
= NULL
;
5090 while ((ch
= getc (f
)) != EOF
)
5093 struct file_name_map
*ptr
;
5098 from
= read_filename_string (ch
, f
);
5099 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5101 to
= read_filename_string (ch
, f
);
5103 simplify_filename (from
);
5104 tolen
= simplify_filename (to
);
5106 ptr
= ((struct file_name_map
*)
5107 xmalloc (sizeof (struct file_name_map
)));
5108 ptr
->map_from
= from
;
5110 /* Make the real filename absolute. */
5111 if (absolute_filename (to
))
5115 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
5116 strcpy (ptr
->map_to
, dirname
);
5117 strcat (ptr
->map_to
, to
);
5121 ptr
->map_next
= map_list_ptr
->map_list_map
;
5122 map_list_ptr
->map_list_map
= ptr
;
5124 while ((ch
= getc (f
)) != '\n')
5131 map_list_ptr
->map_list_next
= map_list
;
5132 map_list
= map_list_ptr
;
5134 return map_list_ptr
->map_list_map
;
5137 /* Try to open include file FILENAME. SEARCHPTR is the directory
5138 being tried from the include file search path.
5139 IMPORTING is "" if we are importing, null otherwise.
5140 Return -2 if found, either a matching name or a matching inode.
5141 Otherwise, open the file and return a file descriptor if successful
5142 or -1 if unsuccessful.
5143 Unless unsuccessful, put a descriptor of the included file into *PINC.
5144 This function maps filenames on file systems based on information read by
5148 open_include_file (filename
, searchptr
, importing
, pinc
)
5150 struct file_name_list
*searchptr
;
5152 struct include_file
**pinc
;
5154 char *fname
= remap
? remap_include_file (filename
, searchptr
) : filename
;
5157 /* Look up FNAME in include_hashtab. */
5158 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
5161 struct include_file
*inc
, *head
= *phead
;
5162 for (inc
= head
; inc
; inc
= inc
->next
)
5163 if (!strcmp (fname
, inc
->fname
))
5167 || ! inc
->control_macro
5168 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
5170 fd
= open (fname
, O_RDONLY
, 0);
5175 /* if #include <dir/file> fails, try again with hacked spec. */
5176 if (!hack_vms_include_specification (fname
, 0))
5178 fd
= open (fname
, O_RDONLY
, 0);
5185 /* FNAME was not in include_hashtab; insert a new entry. */
5186 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
5189 inc
->control_macro
= 0;
5190 inc
->deps_output
= 0;
5191 if (fstat (fd
, &inc
->st
) != 0)
5192 pfatal_with_name (fname
);
5195 /* Look for another file with the same inode and device. */
5196 if (lookup_ino_include (inc
)
5197 && inc
->control_macro
5198 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
5204 /* For -M, add this file to the dependencies. */
5205 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
5206 inc
->deps_output
= 1;
5207 deps_output (fname
, ' ');
5210 /* Handle -H option. */
5211 if (print_include_names
)
5212 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
5216 inc
->control_macro
= importing
;
5222 /* Return the remapped name of the include file FILENAME.
5223 SEARCHPTR is the directory being tried from the include file path. */
5226 remap_include_file (filename
, searchptr
)
5228 struct file_name_list
*searchptr
;
5230 register struct file_name_map
*map
;
5231 register char *from
;
5235 if (! searchptr
->got_name_map
)
5237 searchptr
->name_map
= read_name_map (searchptr
->fname
);
5238 searchptr
->got_name_map
= 1;
5241 /* Check the mapping for the directory we are using. */
5242 from
= filename
+ strlen (searchptr
->fname
);
5243 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5244 if (! strcmp (map
->map_from
, from
))
5248 from
= base_name (filename
);
5250 if (from
!= filename
|| !searchptr
)
5252 /* Try to find a mapping file for the particular directory we are
5253 looking in. Thus #include <sys/types.h> will look up sys/types.h
5254 in /usr/include/header.gcc and look up types.h in
5255 /usr/include/sys/header.gcc. */
5257 char *dir
= (char *) alloca (from
- filename
+ 1);
5258 bcopy (filename
, dir
, from
- filename
);
5259 dir
[from
- filename
] = '\0';
5261 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
5262 if (! strcmp (map
->map_from
, from
))
5269 /* Insert INC into the include file table, hashed by device and inode number.
5270 If a file with different name but same dev+ino was already in the table,
5271 return 1 and set INC's control macro to the already-known macro. */
5274 lookup_ino_include (inc
)
5275 struct include_file
*inc
;
5277 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
5278 % INCLUDE_HASHSIZE
);
5279 struct include_file
*i
= include_ino_hashtab
[hash
];
5281 include_ino_hashtab
[hash
] = inc
;
5283 for (; i
; i
= i
->next_ino
)
5284 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
5285 && inc
->st
.st_dev
== i
->st
.st_dev
) {
5286 inc
->control_macro
= i
->control_macro
;
5293 /* Process file descriptor F, which corresponds to include file INC,
5295 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5296 "system" include directories (as decided by the `is_system_include'
5298 DIRPTR is the link in the dir path through which this file was found,
5299 or 0 if the file name was absolute. */
5302 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5304 struct include_file
*inc
;
5306 int system_header_p
;
5307 struct file_name_list
*dirptr
;
5309 char *fname
= inc
->fname
;
5311 FILE_BUF
*fp
; /* For input stack frame */
5312 int missing_newline
= 0;
5314 CHECK_DEPTH (return;);
5316 fp
= &instack
[indepth
+ 1];
5317 bzero ((char *) fp
, sizeof (FILE_BUF
));
5318 fp
->nominal_fname
= fp
->fname
= fname
;
5319 fp
->nominal_fname_len
= strlen (fname
);
5323 fp
->if_stack
= if_stack
;
5324 fp
->system_header_p
= system_header_p
;
5327 if (S_ISREG (inc
->st
.st_mode
)) {
5328 size_t s
= (size_t) inc
->st
.st_size
;
5329 if (s
!= inc
->st
.st_size
|| s
+ 2 < s
)
5331 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
5334 /* Read the file contents, knowing that s is an upper bound
5335 on the number of bytes we can read. */
5336 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
5337 if (fp
->length
< 0) goto nope
;
5339 else if (S_ISDIR (inc
->st
.st_mode
)) {
5340 error ("directory `%s' specified in #include", fname
);
5344 /* Cannot count its file size before reading.
5345 First read the entire file into heap and
5346 copy them into buffer on stack. */
5351 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5354 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5356 goto nope
; /* error! */
5358 if (st_size
!= bsize
)
5359 break; /* End of file */
5361 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5364 fp
->length
= st_size
;
5367 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5368 /* Backslash-newline at end is not good enough. */
5369 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5370 fp
->buf
[fp
->length
++] = '\n';
5371 missing_newline
= 1;
5373 fp
->buf
[fp
->length
] = '\0';
5375 /* Close descriptor now, so nesting does not use lots of descriptors. */
5378 /* Must do this before calling trigraph_pcp, so that the correct file name
5379 will be printed in warning messages. */
5382 input_file_stack_tick
++;
5387 output_line_directive (fp
, op
, 0, enter_file
);
5390 if (missing_newline
)
5393 if (pedantic
&& missing_newline
)
5394 pedwarn ("file does not end in newline");
5397 input_file_stack_tick
++;
5398 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5404 perror_with_name (fname
);
5409 /* Record that inclusion of the include file INC
5410 should be controlled by the macro named MACRO_NAME.
5411 This means that trying to include the file again
5412 will do something if that macro is defined. */
5415 record_control_macro (inc
, macro_name
)
5416 struct include_file
*inc
;
5419 if (!inc
->control_macro
|| inc
->control_macro
[0])
5420 inc
->control_macro
= macro_name
;
5423 /* Load the specified precompiled header into core, and verify its
5424 preconditions. PCF indicates the file descriptor to read, which must
5425 be a regular file. *ST is its file status.
5426 FNAME indicates the file name of the original header.
5427 *LIMIT will be set to an address one past the end of the file.
5428 If the preconditions of the file are not satisfied, the buffer is
5429 freed and we return 0. If the preconditions are satisfied, return
5430 the address of the buffer following the preconditions. The buffer, in
5431 this case, should never be freed because various pieces of it will
5432 be referred to until all precompiled strings are output at the end of
5436 check_precompiled (pcf
, st
, fname
, limit
)
5439 char *fname ATTRIBUTE_UNUSED
;
5449 if (S_ISREG (st
->st_mode
))
5451 size_t s
= (size_t) st
->st_size
;
5452 if (s
!= st
->st_size
|| s
+ 2 < s
)
5454 buf
= xmalloc (s
+ 2);
5455 length
= safe_read (pcf
, buf
, s
);
5462 if (length
> 0 && buf
[length
-1] != '\n')
5463 buf
[length
++] = '\n';
5466 *limit
= buf
+ length
;
5468 /* File is in core. Check the preconditions. */
5469 if (!check_preconditions (buf
))
5471 for (cp
= buf
; *cp
; cp
++)
5474 fprintf (stderr
, "Using preinclude %s\n", fname
);
5480 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5486 /* PREC (null terminated) points to the preconditions of a
5487 precompiled header. These are a series of #define and #undef
5488 lines which must match the current contents of the hash
5492 check_preconditions (prec
)
5499 lineend
= index (prec
, '\n');
5501 if (*prec
++ != '#') {
5502 error ("Bad format encountered while reading precompiled file");
5505 if (!strncmp (prec
, "define", 6)) {
5509 mdef
= create_definition ((U_CHAR
*) prec
, (U_CHAR
*) lineend
, NULL_PTR
);
5514 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5515 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5516 || (hp
->type
== T_MACRO
5517 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5518 && (mdef
.defn
->length
!= 2
5519 || mdef
.defn
->expansion
[0] != '\n'
5520 || mdef
.defn
->expansion
[1] != ' ')))
5522 } else if (!strncmp (prec
, "undef", 5)) {
5527 while (is_hor_space
[(U_CHAR
) *prec
])
5530 while (is_idchar
[(U_CHAR
) *prec
])
5534 if (lookup ((U_CHAR
*) name
, len
, -1))
5537 error ("Bad format encountered while reading precompiled file");
5542 /* They all passed successfully */
5546 /* Process the main body of a precompiled file. BUF points to the
5547 string section of the file, following the preconditions. LIMIT is one
5548 character past the end. NAME is the name of the file being read
5549 in. OP is the main output buffer. */
5552 pcfinclude (buf
, name
, op
)
5560 /* First in the file comes 4 bytes indicating the number of strings, */
5561 /* in network byte order. (MSB first). */
5563 nstrings
= (nstrings
<< 8) | *cp
++;
5564 nstrings
= (nstrings
<< 8) | *cp
++;
5565 nstrings
= (nstrings
<< 8) | *cp
++;
5567 /* Looping over each string... */
5568 while (nstrings
--) {
5569 U_CHAR
*string_start
;
5570 U_CHAR
*endofthiskey
;
5574 /* Each string starts with a STRINGDEF structure (str), followed */
5575 /* by the text of the string (string_start) */
5577 /* First skip to a longword boundary */
5578 /* ??? Why a 4-byte boundary? On all machines? */
5579 /* NOTE: This works correctly even if size_t
5580 is narrower than a pointer.
5581 Do not try risky measures here to get another type to use!
5582 Do not include stddef.h--it will fail! */
5583 if ((size_t) cp
& 3)
5584 cp
+= 4 - ((size_t) cp
& 3);
5586 /* Now get the string. */
5587 str
= (STRINGDEF
*) (GENERIC_PTR
) cp
;
5588 string_start
= cp
+= sizeof (STRINGDEF
);
5590 for (; *cp
; cp
++) /* skip the string */
5593 /* We need to macro expand the string here to ensure that the
5594 proper definition environment is in place. If it were only
5595 expanded when we find out it is needed, macros necessary for
5596 its proper expansion might have had their definitions changed. */
5597 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5598 /* Lineno is already set in the precompiled file */
5599 str
->contents
= tmpbuf
.buf
;
5600 str
->len
= tmpbuf
.length
;
5602 str
->filename
= name
;
5603 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5606 *stringlist_tailp
= str
;
5607 stringlist_tailp
= &str
->chain
;
5609 /* Next comes a fourbyte number indicating the number of keys
5612 nkeys
= (nkeys
<< 8) | *cp
++;
5613 nkeys
= (nkeys
<< 8) | *cp
++;
5614 nkeys
= (nkeys
<< 8) | *cp
++;
5616 /* If this number is -1, then the string is mandatory. */
5620 /* Otherwise, for each key, */
5621 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5622 KEYDEF
*kp
= (KEYDEF
*) (GENERIC_PTR
) cp
;
5625 /* It starts with a KEYDEF structure */
5626 cp
+= sizeof (KEYDEF
);
5628 /* Find the end of the key. At the end of this for loop we
5629 advance CP to the start of the next key using this variable. */
5630 endofthiskey
= cp
+ strlen ((char *) cp
);
5633 /* Expand the key, and enter it into the hash table. */
5634 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5635 tmpbuf
.bufp
= tmpbuf
.buf
;
5637 while (is_hor_space
[*tmpbuf
.bufp
])
5639 if (!is_idstart
[*tmpbuf
.bufp
]
5640 || tmpbuf
.bufp
== tmpbuf
.buf
+ tmpbuf
.length
) {
5645 hp
= lookup (tmpbuf
.bufp
, -1, -1);
5648 install (tmpbuf
.bufp
, -1, T_PCSTRING
, (char *) kp
, -1);
5650 else if (hp
->type
== T_PCSTRING
) {
5651 kp
->chain
= hp
->value
.keydef
;
5652 hp
->value
.keydef
= kp
;
5658 /* This output_line_directive serves to switch us back to the current
5659 input file in case some of these strings get output (which will
5660 result in line directives for the header file being output). */
5661 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5664 /* Called from rescan when it hits a key for strings. Mark them all
5665 used and clean up. */
5673 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5674 kp
->str
->writeflag
= 1;
5678 /* Write the output, interspersing precompiled strings in their
5679 appropriate places. */
5684 STRINGDEF
*next_string
;
5685 U_CHAR
*cur_buf_loc
;
5686 int line_directive_len
= 80;
5687 char *line_directive
= xmalloc (line_directive_len
);
5690 /* In each run through the loop, either cur_buf_loc ==
5691 next_string_loc, in which case we print a series of strings, or
5692 it is less than next_string_loc, in which case we write some of
5694 cur_buf_loc
= outbuf
.buf
;
5695 next_string
= stringlist
;
5697 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5699 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5700 if (next_string
->writeflag
) {
5701 len
= 4 * strlen ((char *) next_string
->filename
) + 32;
5702 while (len
> line_directive_len
)
5703 line_directive
= xrealloc (line_directive
,
5704 line_directive_len
*= 2);
5705 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5706 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5707 (char *) next_string
->filename
,
5708 strlen ((char *) next_string
->filename
)),
5710 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5711 safe_write (fileno (stdout
),
5712 (char *) next_string
->contents
, next_string
->len
);
5714 next_string
= next_string
->chain
;
5718 ? (next_string
->output_mark
5719 - (cur_buf_loc
- outbuf
.buf
))
5720 : outbuf
.bufp
- cur_buf_loc
);
5722 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5726 free (line_directive
);
5729 /* Pass a directive through to the output file.
5730 BUF points to the contents of the directive, as a contiguous string.
5731 LIMIT points to the first character past the end of the directive.
5732 KEYWORD is the keyword-table entry for the directive. */
5735 pass_thru_directive (buf
, limit
, op
, keyword
)
5736 U_CHAR
*buf
, *limit
;
5738 struct directive
*keyword
;
5740 register unsigned keyword_length
= keyword
->length
;
5742 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5744 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5745 op
->bufp
+= keyword_length
;
5746 if (limit
!= buf
&& buf
[0] != ' ')
5748 bcopy ((char *) buf
, (char *) op
->bufp
, limit
- buf
);
5749 op
->bufp
+= (limit
- buf
);
5752 /* Count the line we have just made in the output,
5753 to get in sync properly. */
5758 /* The arglist structure is built by do_define to tell
5759 collect_definition where the argument names begin. That
5760 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5761 would contain pointers to the strings x, y, and z.
5762 Collect_definition would then build a DEFINITION node,
5763 with reflist nodes pointing to the places x, y, and z had
5764 appeared. So the arglist is just convenience data passed
5765 between these two routines. It is not kept around after
5766 the current #define has been processed and entered into the
5770 struct arglist
*next
;
5777 /* Create a DEFINITION node from a #define directive. Arguments are
5778 as for do_define. */
5781 create_definition (buf
, limit
, op
)
5782 U_CHAR
*buf
, *limit
;
5785 U_CHAR
*bp
; /* temp ptr into input buffer */
5786 U_CHAR
*symname
; /* remember where symbol name starts */
5787 int sym_length
; /* and how long it is */
5788 int line
= instack
[indepth
].lineno
;
5789 char *file
= instack
[indepth
].nominal_fname
;
5790 size_t file_len
= instack
[indepth
].nominal_fname_len
;
5794 int arglengths
= 0; /* Accumulate lengths of arg names
5795 plus number of args. */
5800 while (is_hor_space
[*bp
])
5803 symname
= bp
; /* remember where it starts */
5804 sym_length
= check_macro_name (bp
, "macro");
5807 /* Lossage will occur if identifiers or control keywords are broken
5808 across lines using backslash. This is not the right place to take
5812 struct arglist
*arg_ptrs
= NULL
;
5815 bp
++; /* skip '(' */
5816 SKIP_WHITE_SPACE (bp
);
5818 /* Loop over macro argument names. */
5819 while (*bp
!= ')') {
5820 struct arglist
*temp
;
5822 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5824 temp
->next
= arg_ptrs
;
5825 temp
->argno
= argno
++;
5826 temp
->rest_args
= 0;
5830 pedwarn ("another parameter follows `%s'",
5833 if (!is_idstart
[*bp
])
5835 if (c9x
&& limit
- bp
> (long) REST_EXTENSION_LENGTH
5836 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0)
5838 /* This is the ISO C 9x way to write macros with variable
5839 number of arguments. */
5841 temp
->rest_args
= 1;
5844 pedwarn ("invalid character in macro parameter name");
5847 /* Find the end of the arg name. */
5848 while (is_idchar
[*bp
]) {
5850 /* do we have a "special" rest-args extension here? */
5851 if (limit
- bp
> (long) REST_EXTENSION_LENGTH
5852 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5853 if (pedantic
&& !instack
[indepth
].system_header_p
)
5854 pedwarn ("ANSI C does not allow macro with variable arguments");
5856 temp
->rest_args
= 1;
5860 if (bp
== temp
->name
&& rest_args
== 1)
5862 /* This is the ISO C 9x style. */
5863 temp
->name
= va_args_name
;
5864 temp
->length
= VA_ARGS_NAME_LENGTH
;
5867 temp
->length
= bp
- temp
->name
;
5869 bp
+= REST_EXTENSION_LENGTH
;
5870 arglengths
+= temp
->length
+ 2;
5871 SKIP_WHITE_SPACE (bp
);
5872 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5873 error ("badly punctuated parameter list in `#define'");
5878 SKIP_WHITE_SPACE (bp
);
5879 /* A comma at this point can only be followed by an identifier. */
5880 if (!is_idstart
[*bp
]
5881 && (c9x
&& limit
- bp
<= (long) REST_EXTENSION_LENGTH
5882 || bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) != 0)) {
5883 error ("badly punctuated parameter list in `#define'");
5888 error ("unterminated parameter list in `#define'");
5892 struct arglist
*otemp
;
5894 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5895 if (temp
->length
== otemp
->length
5896 && bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0)
5898 error ("duplicate argument name `%.*s' in `#define'",
5899 temp
->length
, temp
->name
);
5902 if (rest_args
== 0 && temp
->length
== VA_ARGS_NAME_LENGTH
5903 && bcmp (temp
->name
, va_args_name
, VA_ARGS_NAME_LENGTH
) == 0)
5906 reserved name `%s' used as argument name in `#define'", va_args_name
);
5912 ++bp
; /* skip paren */
5913 SKIP_WHITE_SPACE (bp
);
5914 /* now everything from bp before limit is the definition. */
5915 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5916 defn
->rest_args
= rest_args
;
5918 /* Now set defn->args.argnames to the result of concatenating
5919 the argument names in reverse order
5920 with comma-space between them. */
5921 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5923 struct arglist
*temp
;
5925 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5926 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5928 if (temp
->next
!= 0) {
5929 defn
->args
.argnames
[i
++] = ',';
5930 defn
->args
.argnames
[i
++] = ' ';
5933 defn
->args
.argnames
[i
] = 0;
5936 /* Simple expansion or empty definition. */
5940 if (is_hor_space
[*bp
]) {
5942 SKIP_WHITE_SPACE (bp
);
5943 } else if (sym_length
) {
5945 case '!': case '"': case '#': case '%': case '&': case '\'':
5946 case ')': case '*': case '+': case ',': case '-': case '.':
5947 case '/': case ':': case ';': case '<': case '=': case '>':
5948 case '?': case '[': case '\\': case ']': case '^': case '{':
5949 case '|': case '}': case '~':
5950 warning ("missing white space after `#define %.*s'",
5951 sym_length
, symname
);
5955 pedwarn ("missing white space after `#define %.*s'",
5956 sym_length
, symname
);
5961 /* Now everything from bp before limit is the definition. */
5962 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5963 defn
->args
.argnames
= (U_CHAR
*) "";
5968 defn
->file_len
= file_len
;
5970 /* OP is null if this is a predefinition */
5971 defn
->predefined
= !op
;
5973 mdef
.symnam
= symname
;
5974 mdef
.symlen
= sym_length
;
5983 /* Process a #define directive.
5984 BUF points to the contents of the #define directive, as a contiguous string.
5985 LIMIT points to the first character past the end of the definition.
5986 KEYWORD is the keyword-table entry for #define. */
5989 do_define (buf
, limit
, op
, keyword
)
5990 U_CHAR
*buf
, *limit
;
5992 struct directive
*keyword
;
5997 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5998 if (pcp_outfile
&& op
)
5999 pass_thru_directive (buf
, limit
, op
, keyword
);
6001 mdef
= create_definition (buf
, limit
, op
);
6005 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
6009 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
6011 /* Redefining a precompiled key is ok. */
6012 if (hp
->type
== T_PCSTRING
)
6014 /* Redefining a macro is ok if the definitions are the same. */
6015 else if (hp
->type
== T_MACRO
)
6016 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
6017 /* Redefining a constant is ok with -D. */
6018 else if (hp
->type
== T_CONST
)
6019 ok
= ! done_initializing
;
6020 /* Print the warning if it's not ok. */
6022 /* If we are passing through #define and #undef directives, do
6023 that for this re-definition now. */
6024 if (debug_output
&& op
)
6025 pass_thru_directive (buf
, limit
, op
, keyword
);
6027 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
6028 if (hp
->type
== T_MACRO
)
6029 pedwarn_with_file_and_line (hp
->value
.defn
->file
,
6030 hp
->value
.defn
->file_len
,
6031 hp
->value
.defn
->line
,
6032 "this is the location of the previous definition");
6034 /* Replace the old definition. */
6036 hp
->value
.defn
= mdef
.defn
;
6038 /* If we are passing through #define and #undef directives, do
6039 that for this new definition now. */
6040 if (debug_output
&& op
)
6041 pass_thru_directive (buf
, limit
, op
, keyword
);
6042 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
,
6043 (char *) mdef
.defn
, hashcode
);
6054 /* Check a purported macro name SYMNAME, and yield its length.
6055 USAGE is the kind of name this is intended for. */
6058 check_macro_name (symname
, usage
)
6065 for (p
= symname
; is_idchar
[*p
]; p
++)
6067 sym_length
= p
- symname
;
6069 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
6070 error ("invalid %s name", usage
);
6071 else if (!is_idstart
[*symname
]
6072 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
6073 error ("invalid %s name `%.*s'", usage
, sym_length
, symname
);
6077 /* Return zero if two DEFINITIONs are isomorphic. */
6080 compare_defs (d1
, d2
)
6081 DEFINITION
*d1
, *d2
;
6083 register struct reflist
*a1
, *a2
;
6084 register U_CHAR
*p1
= d1
->expansion
;
6085 register U_CHAR
*p2
= d2
->expansion
;
6088 if (d1
->nargs
!= d2
->nargs
)
6091 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
6093 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
6094 a1
= a1
->next
, a2
= a2
->next
) {
6095 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
6096 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
6097 || a1
->argno
!= a2
->argno
6098 || a1
->stringify
!= a2
->stringify
6099 || a1
->raw_before
!= a2
->raw_before
6100 || a1
->raw_after
!= a2
->raw_after
)
6108 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
6109 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
6114 /* Return 1 if two parts of two macro definitions are effectively different.
6115 One of the parts starts at BEG1 and has LEN1 chars;
6116 the other has LEN2 chars at BEG2.
6117 Any sequence of whitespace matches any other sequence of whitespace.
6118 FIRST means these parts are the first of a macro definition;
6119 so ignore leading whitespace entirely.
6120 LAST means these parts are the last of a macro definition;
6121 so ignore trailing whitespace entirely. */
6124 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
6126 U_CHAR
*beg1
, *beg2
;
6130 register U_CHAR
*end1
= beg1
+ len1
;
6131 register U_CHAR
*end2
= beg2
+ len2
;
6133 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6134 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6137 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
6138 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
6140 while (beg1
!= end1
&& beg2
!= end2
) {
6141 if (is_space
[*beg1
] && is_space
[*beg2
]) {
6142 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6143 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6144 } else if (*beg1
== *beg2
) {
6148 return (beg1
!= end1
) || (beg2
!= end2
);
6151 /* Read a replacement list for a macro with parameters.
6152 Build the DEFINITION structure.
6153 Reads characters of text starting at BUF until END.
6154 ARGLIST specifies the formal parameters to look for
6155 in the text of the definition; NARGS is the number of args
6156 in that list, or -1 for a macro name that wants no argument list.
6157 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6158 and NAMELEN is its length in characters.
6160 Note that comments, backslash-newlines, and leading white space
6161 have already been deleted from the argument. */
6163 /* If there is no trailing whitespace, a Newline Space is added at the end
6164 to prevent concatenation that would be contrary to the standard. */
6167 collect_expansion (buf
, end
, nargs
, arglist
)
6170 struct arglist
*arglist
;
6173 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
6174 struct reflist
*endpat
= NULL
;
6175 /* Pointer to first nonspace after last ## seen. */
6177 /* Pointer to first nonspace after last single-# seen. */
6178 U_CHAR
*stringify
= 0;
6179 /* How those tokens were spelled. */
6180 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
6181 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
6183 int expected_delimiter
= '\0';
6185 /* Scan thru the replacement list, ignoring comments and quoted
6186 strings, picking up on the macro calls. It does a linear search
6187 thru the arg list on every potential symbol. Profiling might say
6188 that something smarter should happen. */
6193 /* Find the beginning of the trailing whitespace. */
6196 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
6198 /* Allocate space for the text in the macro definition.
6199 Each input char may or may not need 1 byte,
6200 so this is an upper bound.
6201 The extra 3 are for invented trailing newline-marker and final null. */
6202 maxsize
= (sizeof (DEFINITION
)
6204 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
6206 defn
->nargs
= nargs
;
6207 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
6212 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
6213 error ("`##' at start of macro definition");
6214 p
+= p
[0] == '#' ? 2 : 4;
6217 /* Process the main body of the definition. */
6219 int skipped_arg
= 0;
6220 register U_CHAR c
= *p
++;
6228 if (expected_delimiter
!= '\0') {
6229 if (c
== expected_delimiter
)
6230 expected_delimiter
= '\0';
6232 expected_delimiter
= c
;
6236 if (p
< limit
&& expected_delimiter
) {
6237 /* In a string, backslash goes through
6238 and makes next char ordinary. */
6244 if (!expected_delimiter
&& *p
== ':') {
6245 /* %: is not a digraph if preceded by an odd number of '<'s. */
6247 while (buf
< p0
&& p0
[-1] == '<')
6250 /* Treat %:%: as ## and %: as #. */
6251 if (p
[1] == '%' && p
[2] == ':') {
6253 goto sharp_sharp_token
;
6264 /* # is ordinary inside a string. */
6265 if (expected_delimiter
)
6269 /* ##: concatenate preceding and following tokens. */
6270 /* Take out the first #, discard preceding whitespace. */
6272 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
6274 /* Skip the second #. */
6276 concat_sharp_token_type
= c
;
6277 if (is_hor_space
[*p
]) {
6278 concat_sharp_token_type
= c
+ 1;
6280 SKIP_WHITE_SPACE (p
);
6284 error ("`##' at end of macro definition");
6285 } else if (nargs
>= 0) {
6286 /* Single #: stringify following argument ref.
6287 Don't leave the # in the expansion. */
6290 stringify_sharp_token_type
= c
;
6291 if (is_hor_space
[*p
]) {
6292 stringify_sharp_token_type
= c
+ 1;
6294 SKIP_WHITE_SPACE (p
);
6296 if (! is_idstart
[*p
] || nargs
== 0
6297 || (*p
== 'L' && (p
[1] == '\'' || p
[1] == '"')))
6298 error ("`#' operator is not followed by a macro argument name");
6305 /* In -traditional mode, recognize arguments inside strings and
6306 character constants, and ignore special properties of #.
6307 Arguments inside strings are considered "stringified", but no
6308 extra quote marks are supplied. */
6312 if (expected_delimiter
!= '\0') {
6313 if (c
== expected_delimiter
)
6314 expected_delimiter
= '\0';
6316 expected_delimiter
= c
;
6320 /* Backslash quotes delimiters and itself, but not macro args. */
6321 if (expected_delimiter
!= 0 && p
< limit
6322 && (*p
== expected_delimiter
|| *p
== '\\')) {
6329 if (expected_delimiter
!= '\0') /* No comments inside strings. */
6332 /* If we find a comment that wasn't removed by handle_directive,
6333 this must be -traditional. So replace the comment with
6336 while (++p
< limit
) {
6337 if (p
[0] == '*' && p
[1] == '/') {
6343 /* Mark this as a concatenation-point, as if it had been ##. */
6351 #ifdef MULTIBYTE_CHARS
6352 /* Handle multibyte characters inside string and character literals. */
6353 if (expected_delimiter
!= '\0')
6357 length
= local_mblen (p
, limit
- p
);
6361 bcopy (p
, exp_p
, length
);
6370 /* Handle the start of a symbol. */
6371 if (is_idchar
[c
] && nargs
> 0) {
6372 U_CHAR
*id_beg
= p
- 1;
6376 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6377 id_len
= p
- id_beg
;
6380 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
6381 register struct arglist
*arg
;
6383 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6384 struct reflist
*tpat
;
6386 if (arg
->name
[0] == c
6387 && arg
->length
== id_len
6388 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6389 enum sharp_token_type tpat_stringify
;
6390 if (expected_delimiter
) {
6391 if (warn_stringify
) {
6393 warning ("macro argument `%.*s' is stringified.",
6396 warning ("macro arg `%.*s' would be stringified with -traditional.",
6400 /* If ANSI, don't actually substitute inside a string. */
6403 tpat_stringify
= SHARP_TOKEN
;
6406 = (stringify
== id_beg
6407 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6409 /* make a pat node for this arg and append it to the end of
6411 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6414 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6415 tpat
->raw_after
= NO_SHARP_TOKEN
;
6416 tpat
->rest_args
= arg
->rest_args
;
6417 tpat
->stringify
= tpat_stringify
;
6420 defn
->pattern
= tpat
;
6422 endpat
->next
= tpat
;
6425 tpat
->argno
= arg
->argno
;
6426 tpat
->nchars
= exp_p
- lastp
;
6428 register U_CHAR
*p1
= p
;
6429 SKIP_WHITE_SPACE (p1
);
6432 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6433 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6435 lastp
= exp_p
; /* place to start copying from next time */
6442 /* If this was not a macro arg, copy it into the expansion. */
6443 if (! skipped_arg
) {
6444 register U_CHAR
*lim1
= p
;
6448 if (stringify
== id_beg
)
6449 error ("`#' operator should be followed by a macro argument name");
6454 if (!traditional
&& expected_delimiter
== 0) {
6455 /* If ANSI, put in a newline-space marker to prevent token pasting.
6456 But not if "inside a string" (which in ANSI mode happens only for
6464 defn
->length
= exp_p
- defn
->expansion
;
6466 /* Crash now if we overrun the allocated size. */
6467 if (defn
->length
+ 1 > maxsize
)
6471 /* This isn't worth the time it takes. */
6472 /* give back excess storage */
6473 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6480 do_assert (buf
, limit
, op
, keyword
)
6481 U_CHAR
*buf
, *limit
;
6482 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6483 struct directive
*keyword ATTRIBUTE_UNUSED
;
6485 U_CHAR
*bp
; /* temp ptr into input buffer */
6486 U_CHAR
*symname
; /* remember where symbol name starts */
6487 int sym_length
; /* and how long it is */
6488 struct arglist
*tokens
= NULL
;
6490 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6491 pedwarn ("ANSI C does not allow `#assert'");
6495 while (is_hor_space
[*bp
])
6498 symname
= bp
; /* remember where it starts */
6499 sym_length
= check_macro_name (bp
, "assertion");
6501 /* #define doesn't do this, but we should. */
6502 SKIP_WHITE_SPACE (bp
);
6504 /* Lossage will occur if identifiers or control tokens are broken
6505 across lines using backslash. This is not the right place to take
6509 error ("missing token-sequence in `#assert'");
6516 bp
++; /* skip '(' */
6517 SKIP_WHITE_SPACE (bp
);
6519 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6523 error ("empty token-sequence in `#assert'");
6527 ++bp
; /* skip paren */
6528 SKIP_WHITE_SPACE (bp
);
6531 /* If this name isn't already an assertion name, make it one.
6532 Error if it was already in use in some other way. */
6535 ASSERTION_HASHNODE
*hp
;
6536 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6537 struct tokenlist_list
*value
6538 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6540 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6542 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6543 error ("`defined' redefined as assertion");
6544 hp
= assertion_install (symname
, sym_length
, hashcode
);
6547 /* Add the spec'd token-sequence to the list of such. */
6548 value
->tokens
= tokens
;
6549 value
->next
= hp
->value
;
6557 do_unassert (buf
, limit
, op
, keyword
)
6558 U_CHAR
*buf
, *limit
;
6559 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6560 struct directive
*keyword ATTRIBUTE_UNUSED
;
6562 U_CHAR
*bp
; /* temp ptr into input buffer */
6563 U_CHAR
*symname
; /* remember where symbol name starts */
6564 int sym_length
; /* and how long it is */
6566 struct arglist
*tokens
= NULL
;
6567 int tokens_specified
= 0;
6569 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6570 pedwarn ("ANSI C does not allow `#unassert'");
6574 while (is_hor_space
[*bp
])
6577 symname
= bp
; /* remember where it starts */
6578 sym_length
= check_macro_name (bp
, "assertion");
6580 /* #define doesn't do this, but we should. */
6581 SKIP_WHITE_SPACE (bp
);
6583 /* Lossage will occur if identifiers or control tokens are broken
6584 across lines using backslash. This is not the right place to take
6590 bp
++; /* skip '(' */
6591 SKIP_WHITE_SPACE (bp
);
6593 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6597 error ("empty token list in `#unassert'");
6601 tokens_specified
= 1;
6603 ++bp
; /* skip paren */
6604 SKIP_WHITE_SPACE (bp
);
6608 ASSERTION_HASHNODE
*hp
;
6609 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6610 struct tokenlist_list
*tail
, *prev
;
6612 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6616 /* If no token list was specified, then eliminate this assertion
6618 if (! tokens_specified
) {
6619 struct tokenlist_list
*next
;
6620 for (tail
= hp
->value
; tail
; tail
= next
) {
6622 free_token_list (tail
->tokens
);
6625 delete_assertion (hp
);
6627 /* If a list of tokens was given, then delete any matching list. */
6632 struct tokenlist_list
*next
= tail
->next
;
6633 if (compare_token_lists (tail
->tokens
, tokens
)) {
6637 hp
->value
= tail
->next
;
6638 free_token_list (tail
->tokens
);
6651 /* Test whether there is an assertion named NAME
6652 and optionally whether it has an asserted token list TOKENS.
6653 NAME is not null terminated; its length is SYM_LENGTH.
6654 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6657 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6660 int tokens_specified
;
6661 struct arglist
*tokens
;
6663 ASSERTION_HASHNODE
*hp
;
6664 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6666 if (pedantic
&& !instack
[indepth
].system_header_p
)
6667 pedwarn ("ANSI C does not allow testing assertions");
6669 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6671 /* It is not an assertion; just return false. */
6674 /* If no token list was specified, then value is 1. */
6675 if (! tokens_specified
)
6679 struct tokenlist_list
*tail
;
6683 /* If a list of tokens was given,
6684 then succeed if the assertion records a matching list. */
6687 if (compare_token_lists (tail
->tokens
, tokens
))
6692 /* Fail if the assertion has no matching list. */
6697 /* Compare two lists of tokens for equality including order of tokens. */
6700 compare_token_lists (l1
, l2
)
6701 struct arglist
*l1
, *l2
;
6704 if (l1
->length
!= l2
->length
)
6706 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6712 /* Succeed if both lists end at the same time. */
6716 /* Read a space-separated list of tokens ending in a close parenthesis.
6717 Return a list of strings, in the order they were written.
6718 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6719 Parse the text starting at *BPP, and update *BPP.
6720 Don't parse beyond LIMIT. */
6722 static struct arglist
*
6723 read_token_list (bpp
, limit
, error_flag
)
6728 struct arglist
*token_ptrs
= 0;
6734 /* Loop over the assertion value tokens. */
6736 struct arglist
*temp
;
6740 /* Find the end of the token. */
6744 } else if (*bp
== ')') {
6749 } else if (*bp
== '"' || *bp
== '\'')
6750 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6752 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6753 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6756 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6757 temp
->name
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6758 bcopy ((char *) beg
, (char *) temp
->name
, bp
- beg
);
6759 temp
->name
[bp
- beg
] = 0;
6760 temp
->next
= token_ptrs
;
6762 temp
->length
= bp
- beg
;
6764 SKIP_WHITE_SPACE (bp
);
6767 error ("unterminated token sequence in `#assert' or `#unassert'");
6774 /* We accumulated the names in reverse order.
6775 Now reverse them to get the proper order. */
6777 register struct arglist
*prev
= 0, *this, *next
;
6778 for (this = token_ptrs
; this; this = next
) {
6788 free_token_list (tokens
)
6789 struct arglist
*tokens
;
6792 struct arglist
*next
= tokens
->next
;
6793 free (tokens
->name
);
6799 /* Install a name in the assertion hash table.
6801 If LEN is >= 0, it is the length of the name.
6802 Otherwise, compute the length by scanning the entire name.
6804 If HASH is >= 0, it is the precomputed hash code.
6805 Otherwise, compute the hash code. */
6807 static ASSERTION_HASHNODE
*
6808 assertion_install (name
, len
, hash
)
6813 register ASSERTION_HASHNODE
*hp
;
6814 register int i
, bucket
;
6815 register U_CHAR
*p
, *q
;
6817 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6818 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6820 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6821 hp
->next
= assertion_hashtab
[bucket
];
6822 assertion_hashtab
[bucket
] = hp
;
6824 if (hp
->next
!= NULL
)
6825 hp
->next
->prev
= hp
;
6828 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6831 for (i
= 0; i
< len
; i
++)
6837 /* Find the most recent hash node for name "name" (ending with first
6838 non-identifier char) installed by install
6840 If LEN is >= 0, it is the length of the name.
6841 Otherwise, compute the length by scanning the entire name.
6843 If HASH is >= 0, it is the precomputed hash code.
6844 Otherwise, compute the hash code. */
6846 static ASSERTION_HASHNODE
*
6847 assertion_lookup (name
, len
, hash
)
6852 register ASSERTION_HASHNODE
*bucket
;
6854 bucket
= assertion_hashtab
[hash
];
6856 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6858 bucket
= bucket
->next
;
6864 delete_assertion (hp
)
6865 ASSERTION_HASHNODE
*hp
;
6868 if (hp
->prev
!= NULL
)
6869 hp
->prev
->next
= hp
->next
;
6870 if (hp
->next
!= NULL
)
6871 hp
->next
->prev
= hp
->prev
;
6873 /* Make sure that the bucket chain header that the deleted guy was
6874 on points to the right thing afterwards. */
6875 if (hp
== *hp
->bucket_hdr
)
6876 *hp
->bucket_hdr
= hp
->next
;
6882 * interpret #line directive. Remembers previously seen fnames
6883 * in its very own hash table.
6885 #define FNAME_HASHSIZE 37
6888 do_line (buf
, limit
, op
, keyword
)
6889 U_CHAR
*buf
, *limit
;
6891 struct directive
*keyword ATTRIBUTE_UNUSED
;
6893 register U_CHAR
*bp
;
6894 FILE_BUF
*ip
= &instack
[indepth
];
6897 enum file_change_code file_change
= same_file
;
6899 /* Expand any macros. */
6900 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6902 /* Point to macroexpanded line, which is null-terminated now. */
6904 SKIP_WHITE_SPACE (bp
);
6906 if (!ISDIGIT (*bp
)) {
6907 error ("invalid format `#line' directive");
6911 /* The Newline at the end of this line remains to be processed.
6912 To put the next line at the specified line number,
6913 we must store a line number now that is one less. */
6914 new_lineno
= atoi ((char *) bp
) - 1;
6916 /* NEW_LINENO is one less than the actual line number here. */
6917 if (pedantic
&& new_lineno
< 0)
6918 pedwarn ("line number out of range in `#line' directive");
6920 /* skip over the line number. */
6921 while (ISDIGIT (*bp
))
6924 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6925 if (*bp
&& !is_space
[*bp
]) {
6926 error ("invalid format `#line' directive");
6931 SKIP_WHITE_SPACE (bp
);
6934 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6935 HASHNODE
*hp
, **hash_bucket
;
6941 /* Turn the file name, which is a character string literal,
6942 into a null-terminated string. Do this in place. */
6945 switch ((*p
++ = *bp
++)) {
6947 error ("invalid format `#line' directive");
6951 if (! ignore_escape_flag
)
6953 char *bpc
= (char *) bp
;
6954 HOST_WIDE_INT c
= parse_escape (&bpc
, (HOST_WIDE_INT
) (U_CHAR
) (-1));
6955 bp
= (U_CHAR
*) bpc
;
6968 fname_length
= p
- fname
;
6970 SKIP_WHITE_SPACE (bp
);
6973 pedwarn ("garbage at end of `#line' directive");
6975 file_change
= enter_file
;
6976 else if (*bp
== '2')
6977 file_change
= leave_file
;
6978 else if (*bp
== '3')
6979 ip
->system_header_p
= 1;
6980 else if (*bp
== '4')
6981 ip
->system_header_p
= 2;
6983 error ("invalid format `#line' directive");
6988 SKIP_WHITE_SPACE (bp
);
6990 ip
->system_header_p
= 1;
6992 SKIP_WHITE_SPACE (bp
);
6995 ip
->system_header_p
= 2;
6997 SKIP_WHITE_SPACE (bp
);
7000 error ("invalid format `#line' directive");
7005 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
7006 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
7007 if (hp
->length
== fname_length
&&
7008 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
7009 ip
->nominal_fname
= hp
->value
.cpval
;
7010 ip
->nominal_fname_len
= fname_length
;
7014 /* Didn't find it; cons up a new one. */
7015 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
7016 hp
->next
= *hash_bucket
;
7019 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
7020 ip
->nominal_fname_len
= hp
->length
= fname_length
;
7021 bcopy (fname
, hp
->value
.cpval
, fname_length
+ 1);
7024 error ("invalid format `#line' directive");
7028 ip
->lineno
= new_lineno
;
7029 output_line_directive (ip
, op
, 0, file_change
);
7030 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
7034 /* Remove the definition of a symbol from the symbol table.
7035 according to un*x /lib/cpp, it is not an error to undef
7036 something that has no definitions, so it isn't one here either. */
7039 do_undef (buf
, limit
, op
, keyword
)
7040 U_CHAR
*buf
, *limit
;
7042 struct directive
*keyword
;
7046 U_CHAR
*orig_buf
= buf
;
7048 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7049 if (pcp_outfile
&& op
)
7050 pass_thru_directive (buf
, limit
, op
, keyword
);
7052 SKIP_WHITE_SPACE (buf
);
7053 sym_length
= check_macro_name (buf
, "macro");
7055 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
7056 /* If we are generating additional info for debugging (with -g) we
7057 need to pass through all effective #undef directives. */
7058 if (debug_output
&& op
)
7059 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
7060 if (hp
->type
!= T_MACRO
)
7061 warning ("undefining `%s'", hp
->name
);
7067 SKIP_WHITE_SPACE (buf
);
7069 pedwarn ("garbage after `#undef' directive");
7074 /* Report an error detected by the program we are processing.
7075 Use the text of the line in the error message.
7076 (We use error because it prints the filename & line#.) */
7079 do_error (buf
, limit
, op
, keyword
)
7080 U_CHAR
*buf
, *limit
;
7081 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7082 struct directive
*keyword ATTRIBUTE_UNUSED
;
7084 int length
= limit
- buf
;
7085 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
7086 bcopy ((char *) buf
, (char *) copy
, length
);
7088 SKIP_WHITE_SPACE (copy
);
7089 error ("#error %s", copy
);
7093 /* Report a warning detected by the program we are processing.
7094 Use the text of the line in the warning message, then continue.
7095 (We use error because it prints the filename & line#.) */
7098 do_warning (buf
, limit
, op
, keyword
)
7099 U_CHAR
*buf
, *limit
;
7100 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7101 struct directive
*keyword ATTRIBUTE_UNUSED
;
7103 int length
= limit
- buf
;
7104 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
7105 bcopy ((char *) buf
, (char *) copy
, length
);
7107 SKIP_WHITE_SPACE (copy
);
7109 if (pedantic
&& !instack
[indepth
].system_header_p
)
7110 pedwarn ("ANSI C does not allow `#warning'");
7112 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
7113 if -pedantic-errors is given, #warning should cause an error. */
7114 pedwarn ("#warning %s", copy
);
7118 /* Remember the name of the current file being read from so that we can
7119 avoid ever including it again. */
7126 for (i
= indepth
; i
>= 0; i
--)
7127 if (instack
[i
].inc
) {
7128 record_control_macro (instack
[i
].inc
, (U_CHAR
*) "");
7133 /* Report program identification. */
7136 do_ident (buf
, limit
, op
, keyword
)
7137 U_CHAR
*buf
, *limit
;
7139 struct directive
*keyword ATTRIBUTE_UNUSED
;
7144 /* Allow #ident in system headers, since that's not user's fault. */
7145 if (pedantic
&& !instack
[indepth
].system_header_p
)
7146 pedwarn ("ANSI C does not allow `#ident'");
7148 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
7150 len
= trybuf
.bufp
- buf
;
7152 /* Output expanded directive. */
7153 check_expand (op
, 7 + len
);
7154 bcopy ("#ident ", (char *) op
->bufp
, 7);
7156 bcopy ((char *) buf
, (char *) op
->bufp
, len
);
7163 /* #pragma and its argument line have already been copied to the output file.
7164 Just check for some recognized pragmas that need validation here. */
7167 do_pragma (buf
, limit
, op
, keyword
)
7168 U_CHAR
*buf
, *limit ATTRIBUTE_UNUSED
;
7169 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7170 struct directive
*keyword ATTRIBUTE_UNUSED
;
7172 SKIP_WHITE_SPACE (buf
);
7173 if (!strncmp ((char *) buf
, "once", 4)) {
7174 /* Allow #pragma once in system headers, since that's not the user's
7176 if (!instack
[indepth
].system_header_p
)
7177 warning ("`#pragma once' is obsolete");
7181 if (!strncmp ((char *) buf
, "implementation", 14)) {
7182 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7183 been included yet. */
7186 U_CHAR
*p
= buf
+ 14, *fname
;
7187 SKIP_WHITE_SPACE (p
);
7192 if ((p
= (U_CHAR
*) index ((char *) fname
, '\"')))
7195 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
7196 struct include_file
*inc
;
7197 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
7198 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
7199 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
7209 /* This was a fun hack, but #pragma seems to start to be useful.
7210 By failing to recognize it, we pass it through unchanged to cc1. */
7212 /* The behavior of the #pragma directive is implementation defined.
7213 this implementation defines it as follows. */
7219 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
7222 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
7224 execl ("/usr/games/hack", "#pragma", 0);
7225 execl ("/usr/games/rogue", "#pragma", 0);
7226 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7227 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7229 fatal ("You are in a maze of twisty compiler features, all different");
7233 #ifdef SCCS_DIRECTIVE
7235 /* Just ignore #sccs, on systems where we define it at all. */
7238 do_sccs (buf
, limit
, op
, keyword
)
7239 U_CHAR
*buf ATTRIBUTE_UNUSED
, *limit ATTRIBUTE_UNUSED
;
7240 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7241 struct directive
*keyword ATTRIBUTE_UNUSED
;
7244 pedwarn ("ANSI C does not allow `#sccs'");
7248 #endif /* defined (SCCS_DIRECTIVE) */
7250 /* Handle #if directive by
7251 1) inserting special `defined' keyword into the hash table
7252 that gets turned into 0 or 1 by special_symbol (thus,
7253 if the luser has a symbol called `defined' already, it won't
7254 work inside the #if directive)
7255 2) rescan the input into a temporary output buffer
7256 3) pass the output buffer to the yacc parser and collect a value
7257 4) clean up the mess left from steps 1 and 2.
7258 5) call conditional_skip to skip til the next #endif (etc.),
7259 or not, depending on the value from step 3. */
7262 do_if (buf
, limit
, op
, keyword
)
7263 U_CHAR
*buf
, *limit
;
7265 struct directive
*keyword ATTRIBUTE_UNUSED
;
7267 HOST_WIDE_INT value
;
7268 FILE_BUF
*ip
= &instack
[indepth
];
7270 value
= eval_if_expression (buf
, limit
- buf
);
7271 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
7275 /* Handle a #elif directive by not changing if_stack either.
7276 see the comment above do_else. */
7279 do_elif (buf
, limit
, op
, keyword
)
7280 U_CHAR
*buf
, *limit
;
7282 struct directive
*keyword ATTRIBUTE_UNUSED
;
7284 HOST_WIDE_INT value
;
7285 FILE_BUF
*ip
= &instack
[indepth
];
7287 if (if_stack
== instack
[indepth
].if_stack
) {
7288 error ("`#elif' not within a conditional");
7291 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7292 error ("`#elif' after `#else'");
7293 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7294 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7295 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7296 if_stack
->fname_len
))) {
7297 fprintf (stderr
, ", file ");
7298 eprint_string (if_stack
->fname
, if_stack
->fname_len
);
7300 fprintf (stderr
, ")\n");
7302 if_stack
->type
= T_ELIF
;
7305 if (if_stack
->if_succeeded
)
7306 skip_if_group (ip
, 0, op
);
7308 value
= eval_if_expression (buf
, limit
- buf
);
7310 skip_if_group (ip
, 0, op
);
7312 ++if_stack
->if_succeeded
; /* continue processing input */
7313 output_line_directive (ip
, op
, 1, same_file
);
7319 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7320 result as a C expression and return the value as an int. */
7322 static HOST_WIDE_INT
7323 eval_if_expression (buf
, length
)
7328 HASHNODE
*save_defined
;
7329 HOST_WIDE_INT value
;
7331 save_defined
= install ((U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
7334 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
7336 delete_macro (save_defined
); /* clean up special symbol */
7338 temp_obuf
.buf
[temp_obuf
.length
] = '\n';
7339 value
= parse_c_expression ((char *) temp_obuf
.buf
,
7340 warn_undef
&& !instack
[indepth
].system_header_p
);
7342 free (temp_obuf
.buf
);
7347 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7348 or don't skip to the #endif/#else/#elif depending on what directive
7349 is actually being processed. */
7352 do_xifdef (buf
, limit
, op
, keyword
)
7353 U_CHAR
*buf
, *limit
;
7355 struct directive
*keyword
;
7358 FILE_BUF
*ip
= &instack
[indepth
];
7360 int start_of_file
= 0;
7361 U_CHAR
*control_macro
= 0;
7363 /* Detect a #ifndef at start of file (not counting comments). */
7364 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7365 U_CHAR
*p
= ip
->buf
;
7366 while (p
!= directive_start
) {
7370 /* Make no special provision for backslash-newline here; this is
7371 slower if backslash-newlines are present, but it's correct,
7372 and it's not worth it to tune for the rare backslash-newline. */
7374 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7375 /* Skip this comment. */
7377 U_CHAR
*save_bufp
= ip
->bufp
;
7379 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7380 ip
->bufp
= save_bufp
;
7385 /* If we get here, this conditional is the beginning of the file. */
7390 /* Discard leading and trailing whitespace. */
7391 SKIP_WHITE_SPACE (buf
);
7392 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7394 /* Find the end of the identifier at the beginning. */
7395 for (end
= buf
; is_idchar
[*end
]; end
++);
7398 skip
= (keyword
->type
== T_IFDEF
);
7400 pedwarn (end
== limit
? "`#%s' with no argument"
7401 : "`#%s' argument starts with punctuation",
7406 if (! traditional
) {
7407 if (ISDIGIT (buf
[0]))
7408 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7409 else if (end
!= limit
)
7410 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7413 hp
= lookup (buf
, end
-buf
, -1);
7416 /* Output a precondition for this macro. */
7418 && (hp
->type
== T_CONST
7419 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7420 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7423 fprintf (pcp_outfile
, "#undef ");
7424 while (is_idchar
[*cp
]) /* Ick! */
7425 fputc (*cp
++, pcp_outfile
);
7426 putc ('\n', pcp_outfile
);
7430 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7431 if (start_of_file
&& !skip
) {
7432 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7433 bcopy ((char *) buf
, (char *) control_macro
, end
- buf
);
7434 control_macro
[end
- buf
] = 0;
7438 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7442 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7443 If this is a #ifndef starting at the beginning of a file,
7444 CONTROL_MACRO is the macro name tested by the #ifndef.
7445 Otherwise, CONTROL_MACRO is 0. */
7448 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7451 enum node_type type
;
7452 U_CHAR
*control_macro
;
7455 IF_STACK_FRAME
*temp
;
7457 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7458 temp
->fname
= ip
->nominal_fname
;
7459 temp
->fname_len
= ip
->nominal_fname_len
;
7460 temp
->lineno
= ip
->lineno
;
7461 temp
->next
= if_stack
;
7462 temp
->control_macro
= control_macro
;
7465 if_stack
->type
= type
;
7468 skip_if_group (ip
, 0, op
);
7471 ++if_stack
->if_succeeded
;
7472 output_line_directive (ip
, &outbuf
, 1, same_file
);
7476 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7477 Leaves input ptr at the sharp sign found.
7478 If ANY is nonzero, return at next directive of any sort. */
7481 skip_if_group (ip
, any
, op
)
7486 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7487 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7488 struct directive
*kt
;
7489 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7490 U_CHAR
*beg_of_line
= bp
;
7491 register int ident_length
;
7492 U_CHAR
*ident
, *after_ident
;
7493 /* Save info about where the group starts. */
7494 U_CHAR
*beg_of_group
= bp
;
7495 int beg_lineno
= ip
->lineno
;
7496 int skipping_include_directive
= 0;
7498 if (output_conditionals
&& op
!= 0) {
7499 char *ptr
= "#failed\n";
7500 int len
= strlen (ptr
);
7502 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7507 check_expand (op
, len
);
7508 bcopy (ptr
, (char *) op
->bufp
, len
);
7511 output_line_directive (ip
, op
, 1, 0);
7516 case '/': /* possible comment */
7517 if (*bp
== '\\' && bp
[1] == '\n')
7520 || (cplusplus_comments
&& *bp
== '/')) {
7522 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7526 if (skipping_include_directive
) {
7527 while (bp
< endb
&& *bp
!= '>' && *bp
!= '\n') {
7528 if (*bp
== '\\' && bp
[1] == '\n') {
7537 if (skipping_include_directive
) {
7538 while (bp
< endb
&& *bp
!= '\n') {
7543 if (*bp
== '\\' && bp
[1] == '\n') {
7553 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7554 NULL_PTR
, NULL_PTR
);
7557 /* Char after backslash loses its special meaning in some cases. */
7561 } else if (traditional
&& bp
< endb
)
7567 skipping_include_directive
= 0;
7570 if (beg_of_line
== 0 || traditional
)
7573 while (bp
[0] == '\\' && bp
[1] == '\n')
7579 /* # keyword: a # must be first nonblank char on the line */
7580 if (beg_of_line
== 0)
7584 /* Scan from start of line, skipping whitespace, comments
7585 and backslash-newlines, and see if we reach this #.
7586 If not, this # is not special. */
7588 /* If -traditional, require # to be at beginning of line. */
7591 if (is_hor_space
[*bp
])
7593 else if (*bp
== '\\' && bp
[1] == '\n')
7595 else if (*bp
== '/' && bp
[1] == '*') {
7609 #ifdef MULTIBYTE_CHARS
7611 length
= local_mblen (bp
, endb
- bp
);
7619 /* There is no point in trying to deal with C++ // comments here,
7620 because if there is one, then this # must be part of the
7621 comment and we would never reach here. */
7625 if (bp
!= ip
->bufp
) {
7626 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7630 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7631 if (ip
->bufp
[0] == '%') {
7632 /* Skip past the ':' again. */
7633 while (*bp
== '\\') {
7640 /* Skip whitespace and \-newline. */
7642 if (is_hor_space
[*bp
])
7644 else if (*bp
== '\\' && bp
[1] == '\n')
7646 else if (*bp
== '/') {
7647 if (bp
[1] == '\\' && bp
[2] == '\n')
7648 newline_fix (bp
+ 1);
7650 for (bp
+= 2; ; bp
++) {
7653 else if (*bp
== '*') {
7654 if (bp
[-1] == '/' && warn_comments
)
7655 warning ("`/*' within comment");
7656 if (bp
[1] == '\\' && bp
[2] == '\n')
7657 newline_fix (bp
+ 1);
7663 #ifdef MULTIBYTE_CHARS
7665 length
= local_mblen (bp
, endb
- bp
);
7672 } else if (bp
[1] == '/' && cplusplus_comments
) {
7673 for (bp
+= 2; ; bp
++) {
7676 if (*bp
== '\\' && bp
[1] == '\n')
7679 warning ("multiline `//' comment");
7685 #ifdef MULTIBYTE_CHARS
7687 length
= local_mblen (bp
, endb
- bp
);
7701 /* Now find end of directive name.
7702 If we encounter a backslash-newline, exchange it with any following
7703 symbol-constituents so that we end up with a contiguous name. */
7709 if (*bp
== '\\' && bp
[1] == '\n')
7710 name_newline_fix (bp
);
7716 ident_length
= bp
- cp
;
7720 /* A line of just `#' becomes blank. */
7722 if (ident_length
== 0 && *after_ident
== '\n') {
7726 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7728 while (is_idchar
[*p
]) {
7729 if (*p
< '0' || *p
> '9')
7733 /* Handle # followed by a line number. */
7734 if (p
!= ident
&& !is_idchar
[*p
]) {
7736 pedwarn ("`#' followed by integer");
7740 /* Avoid error for `###' and similar cases unless -pedantic. */
7742 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7744 if (pedantic
&& !lang_asm
)
7745 pedwarn ("invalid preprocessing directive");
7750 if (!lang_asm
&& pedantic
)
7751 pedwarn ("invalid preprocessing directive name");
7755 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7756 IF_STACK_FRAME
*temp
;
7757 if (ident_length
== kt
->length
7758 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7759 /* If we are asked to return on next directive, do so now. */
7767 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7768 temp
->next
= if_stack
;
7770 temp
->lineno
= ip
->lineno
;
7771 temp
->fname
= ip
->nominal_fname
;
7772 temp
->fname_len
= ip
->nominal_fname_len
;
7773 temp
->type
= kt
->type
;
7777 if (pedantic
&& if_stack
!= save_if_stack
)
7778 validate_else (bp
, endb
);
7780 if (if_stack
== instack
[indepth
].if_stack
) {
7781 error ("`#%s' not within a conditional", kt
->name
);
7784 else if (if_stack
== save_if_stack
)
7785 goto done
; /* found what we came for */
7787 if (kt
->type
!= T_ENDIF
) {
7788 if (if_stack
->type
== T_ELSE
)
7789 error ("`#else' or `#elif' after `#else'");
7790 if_stack
->type
= kt
->type
;
7795 if_stack
= if_stack
->next
;
7800 case T_INCLUDE_NEXT
:
7802 skipping_include_directive
= 1;
7811 /* Don't let erroneous code go by. */
7812 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7813 pedwarn ("invalid preprocessing directive name");
7818 /* after this returns, rescan will exit because ip->bufp
7819 now points to the end of the buffer.
7820 rescan is responsible for the error message also. */
7823 if (output_conditionals
&& op
!= 0) {
7824 char *ptr
= "#endfailed\n";
7825 int len
= strlen (ptr
);
7827 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7832 check_expand (op
, beg_of_line
- beg_of_group
);
7833 bcopy ((char *) beg_of_group
, (char *) op
->bufp
,
7834 beg_of_line
- beg_of_group
);
7835 op
->bufp
+= beg_of_line
- beg_of_group
;
7836 op
->lineno
+= ip
->lineno
- beg_lineno
;
7837 check_expand (op
, len
);
7838 bcopy (ptr
, (char *) op
->bufp
, len
);
7844 /* Handle a #else directive. Do this by just continuing processing
7845 without changing if_stack ; this is so that the error message
7846 for missing #endif's etc. will point to the original #if. It
7847 is possible that something different would be better. */
7850 do_else (buf
, limit
, op
, keyword
)
7851 U_CHAR
*buf
, *limit
;
7853 struct directive
*keyword ATTRIBUTE_UNUSED
;
7855 FILE_BUF
*ip
= &instack
[indepth
];
7858 SKIP_WHITE_SPACE (buf
);
7860 pedwarn ("text following `#else' violates ANSI standard");
7863 if (if_stack
== instack
[indepth
].if_stack
) {
7864 error ("`#else' not within a conditional");
7867 /* #ifndef can't have its special treatment for containing the whole file
7868 if it has a #else clause. */
7869 if_stack
->control_macro
= 0;
7871 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7872 error ("`#else' after `#else'");
7873 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7874 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7875 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7876 if_stack
->fname_len
))) {
7877 fprintf (stderr
, ", file ");
7878 eprint_string (if_stack
->fname
, if_stack
->fname_len
);
7880 fprintf (stderr
, ")\n");
7882 if_stack
->type
= T_ELSE
;
7885 if (if_stack
->if_succeeded
)
7886 skip_if_group (ip
, 0, op
);
7888 ++if_stack
->if_succeeded
; /* continue processing input */
7889 output_line_directive (ip
, op
, 1, same_file
);
7894 /* Unstack after #endif directive. */
7897 do_endif (buf
, limit
, op
, keyword
)
7898 U_CHAR
*buf
, *limit
;
7900 struct directive
*keyword ATTRIBUTE_UNUSED
;
7903 SKIP_WHITE_SPACE (buf
);
7905 pedwarn ("text following `#endif' violates ANSI standard");
7908 if (if_stack
== instack
[indepth
].if_stack
)
7909 error ("unbalanced `#endif'");
7911 IF_STACK_FRAME
*temp
= if_stack
;
7912 if_stack
= if_stack
->next
;
7913 if (temp
->control_macro
!= 0) {
7914 /* This #endif matched a #ifndef at the start of the file.
7915 See if it is at the end of the file. */
7916 FILE_BUF
*ip
= &instack
[indepth
];
7917 U_CHAR
*p
= ip
->bufp
;
7918 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7924 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7925 /* Skip this comment. */
7927 U_CHAR
*save_bufp
= ip
->bufp
;
7929 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7930 ip
->bufp
= save_bufp
;
7935 /* If we get here, this #endif ends a #ifndef
7936 that contains all of the file (aside from whitespace).
7937 Arrange not to include the file again
7938 if the macro that was tested is defined.
7940 Do not do this for the top-level file in a -include or any
7941 file in a -imacros. */
7943 && ! (indepth
== 1 && no_record_file
)
7944 && ! (no_record_file
&& no_output
))
7945 record_control_macro (ip
->inc
, temp
->control_macro
);
7949 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
7954 /* When an #else or #endif is found while skipping failed conditional,
7955 if -pedantic was specified, this is called to warn about text after
7956 the directive name. P points to the first char after the directive
7960 validate_else (p
, limit
)
7962 register U_CHAR
*limit
;
7964 /* Advance P over whitespace and comments. */
7966 while (*p
== '\\' && p
[1] == '\n')
7968 if (is_hor_space
[*p
])
7970 else if (*p
== '/') {
7971 while (p
[1] == '\\' && p
[2] == '\n')
7974 /* Don't bother warning about unterminated comments
7975 since that will happen later. Just be sure to exit. */
7976 for (p
+= 2; ; p
++) {
7980 while (p
[1] == '\\' && p
[2] == '\n')
7989 #ifdef MULTIBYTE_CHARS
7991 length
= local_mblen (p
, limit
- p
);
7998 else if (cplusplus_comments
&& p
[1] == '/')
8004 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8007 /* Skip a comment, assuming the input ptr immediately follows the
8008 initial slash-star. Bump *LINE_COUNTER for each newline.
8009 (The canonical line counter is &ip->lineno.)
8010 Don't use this routine (or the next one) if bumping the line
8011 counter is not sufficient to deal with newlines in the string.
8013 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8014 This feature is useful when processing a comment that is going to
8015 be processed or was processed at another point in the preprocessor,
8016 to avoid a duplicate warning. Likewise for unterminated comment
8020 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
8021 register FILE_BUF
*ip
;
8022 int *line_counter
; /* place to remember newlines, or NULL */
8025 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8026 register U_CHAR
*bp
= ip
->bufp
;
8027 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
8028 int start_line
= line_counter
? *line_counter
: 0;
8030 /* JF this line_counter stuff is a crock to make sure the
8031 comment is only put out once, no matter how many times
8032 the comment is skipped. It almost works */
8035 *op
->bufp
++ = bp
[-1];
8037 if (cplusplus_comments
&& bp
[-1] == '/') {
8038 for (; bp
< limit
; bp
++) {
8041 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
8043 if (!nowarn
&& warn_comments
)
8044 warning ("multiline `//' comment");
8056 #ifdef MULTIBYTE_CHARS
8058 length
= local_mblen (bp
, limit
- bp
);
8063 bcopy (bp
, op
->bufp
, length
- 1);
8064 op
->bufp
+= (length
- 1);
8076 while (bp
< limit
) {
8081 /* If this is the end of the file, we have an unterminated comment.
8082 Don't swallow the newline. We are guaranteed that there will be a
8083 trailing newline and various pieces assume it's there. */
8090 if (line_counter
!= NULL
)
8096 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
8097 warning ("`/*' within comment");
8098 if (*bp
== '\\' && bp
[1] == '\n')
8107 #ifdef MULTIBYTE_CHARS
8112 length
= local_mblen (bp
, limit
- bp
);
8118 bcopy (bp
, op
->bufp
, length
);
8128 error_with_line (line_for_error (start_line
), "unterminated comment");
8133 /* Skip over a quoted string. BP points to the opening quote.
8134 Returns a pointer after the closing quote. Don't go past LIMIT.
8135 START_LINE is the line number of the starting point (but it need
8136 not be valid if the starting point is inside a macro expansion).
8138 The input stack state is not changed.
8140 If COUNT_NEWLINES is nonzero, it points to an int to increment
8141 for each newline passed.
8143 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8144 if we pass a backslash-newline.
8146 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8149 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
8150 register U_CHAR
*bp
;
8151 register U_CHAR
*limit
;
8153 int *count_newlines
;
8154 int *backslash_newlines_p
;
8157 register U_CHAR c
, match
;
8162 error_with_line (line_for_error (start_line
),
8163 "unterminated string or character constant");
8164 error_with_line (multiline_string_line
,
8165 "possible real start of unterminated constant");
8166 multiline_string_line
= 0;
8173 while (*bp
== '\\' && bp
[1] == '\n') {
8174 if (backslash_newlines_p
)
8175 *backslash_newlines_p
= 1;
8181 if (backslash_newlines_p
)
8182 *backslash_newlines_p
= 1;
8187 } else if (c
== '\n') {
8189 /* Unterminated strings and character constants are 'valid'. */
8190 bp
--; /* Don't consume the newline. */
8195 if (match
== '\'') {
8196 error_with_line (line_for_error (start_line
),
8197 "unterminated string or character constant");
8203 /* If not traditional, then allow newlines inside strings. */
8206 if (multiline_string_line
== 0) {
8208 pedwarn_with_line (line_for_error (start_line
),
8209 "string constant runs past end of line");
8210 multiline_string_line
= start_line
;
8212 } else if (c
== match
)
8214 #ifdef MULTIBYTE_CHARS
8218 length
= local_mblen (bp
, limit
- bp
);
8228 /* Place into DST a quoted string representing the string SRC.
8229 SRCLEN is the length of SRC; SRC may contain null bytes.
8230 Return the address of DST's terminating null. */
8233 quote_string (dst
, src
, srclen
)
8238 char *srclim
= src
+ srclen
;
8241 while (src
!= srclim
)
8242 switch ((c
= *src
++))
8249 sprintf (dst
, "\\%03o", c
);
8266 /* Skip across a group of balanced parens, starting from IP->bufp.
8267 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8269 This does not handle newlines, because it's used for the arg of #if,
8270 where there aren't any newlines. Also, backslash-newline can't appear. */
8273 skip_paren_group (ip
)
8274 register FILE_BUF
*ip
;
8276 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8277 U_CHAR
*p
= ip
->bufp
;
8279 int lines_dummy
= 0;
8281 while (p
!= limit
) {
8291 return ip
->bufp
= p
;
8297 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
8305 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
8307 return ip
->bufp
= p
;
8317 /* Write out a #line directive, for instance, after an #include file.
8318 If CONDITIONAL is nonzero, we can omit the #line if it would
8319 appear to be a no-op, and we can output a few newlines instead
8320 if we want to increase the line number by a small amount.
8321 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8324 output_line_directive (ip
, op
, conditional
, file_change
)
8327 enum file_change_code file_change
;
8330 char *line_directive_buf
, *line_end
;
8332 if (no_line_directives
8333 || ip
->fname
== NULL
8335 op
->lineno
= ip
->lineno
;
8340 if (ip
->lineno
== op
->lineno
)
8343 /* If the inherited line number is a little too small,
8344 output some newlines instead of a #line directive. */
8345 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
8346 check_expand (op
, 10);
8347 while (ip
->lineno
> op
->lineno
) {
8355 /* Output a positive line number if possible. */
8356 while (ip
->lineno
<= 0 && ip
->bufp
- ip
->buf
< ip
->length
8357 && *ip
->bufp
== '\n') {
8362 line_directive_buf
= (char *) alloca (4 * ip
->nominal_fname_len
+ 100);
8363 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
8364 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
8365 ip
->nominal_fname
, ip
->nominal_fname_len
);
8366 if (file_change
!= same_file
) {
8368 *line_end
++ = file_change
== enter_file
? '1' : '2';
8370 /* Tell cc1 if following text comes from a system header file. */
8371 if (ip
->system_header_p
) {
8375 #ifndef NO_IMPLICIT_EXTERN_C
8376 /* Tell cc1plus if following text should be treated as C. */
8377 if (ip
->system_header_p
== 2 && cplusplus
) {
8383 len
= line_end
- line_directive_buf
;
8384 check_expand (op
, len
+ 1);
8385 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
8387 bcopy ((char *) line_directive_buf
, (char *) op
->bufp
, len
);
8389 op
->lineno
= ip
->lineno
;
8392 /* This structure represents one parsed argument in a macro call.
8393 `raw' points to the argument text as written (`raw_length' is its length).
8394 `expanded' points to the argument's macro-expansion
8395 (its length is `expand_length').
8396 `stringified_length' is the length the argument would have
8398 `use_count' is the number of times this macro arg is substituted
8399 into the macro. If the actual use count exceeds 10,
8400 the value stored is 10.
8401 `free1' and `free2', if nonzero, point to blocks to be freed
8402 when the macro argument data is no longer needed. */
8405 U_CHAR
*raw
, *expanded
;
8406 int raw_length
, expand_length
;
8407 int stringified_length
;
8408 U_CHAR
*free1
, *free2
;
8413 /* Expand a macro call.
8414 HP points to the symbol that is the macro being called.
8415 Put the result of expansion onto the input stack
8416 so that subsequent input by our caller will use it.
8418 If macro wants arguments, caller has already verified that
8419 an argument list follows; arguments come from the input stack. */
8422 macroexpand (hp
, op
)
8427 DEFINITION
*defn
= hp
->value
.defn
;
8428 register U_CHAR
*xbuf
;
8430 int start_line
= instack
[indepth
].lineno
;
8431 int rest_args
, rest_zero
;
8433 CHECK_DEPTH (return;);
8435 /* it might not actually be a macro. */
8436 if (hp
->type
!= T_MACRO
) {
8437 special_symbol (hp
, op
);
8441 /* This macro is being used inside a #if, which means it must be */
8442 /* recorded as a precondition. */
8443 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
8444 dump_single_macro (hp
, pcp_outfile
);
8446 nargs
= defn
->nargs
;
8450 struct argdata
*args
;
8451 char *parse_error
= 0;
8453 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
8455 for (i
= 0; i
< nargs
; i
++) {
8456 args
[i
].raw
= (U_CHAR
*) "";
8457 args
[i
].expanded
= 0;
8458 args
[i
].raw_length
= args
[i
].expand_length
8459 = args
[i
].stringified_length
= 0;
8460 args
[i
].free1
= args
[i
].free2
= 0;
8461 args
[i
].use_count
= 0;
8464 /* Parse all the macro args that are supplied. I counts them.
8465 The first NARGS args are stored in ARGS.
8466 The rest are discarded.
8467 If rest_args is set then we assume macarg absorbed the rest of the args.
8472 /* Discard the open-parenthesis or comma before the next arg. */
8473 ++instack
[indepth
].bufp
;
8476 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
8477 /* If we are working on last arg which absorbs rest of args... */
8478 if (i
== nargs
- 1 && defn
->rest_args
)
8480 parse_error
= macarg (&args
[i
], rest_args
);
8483 parse_error
= macarg (NULL_PTR
, 0);
8485 error_with_line (line_for_error (start_line
), parse_error
);
8489 } while (*instack
[indepth
].bufp
!= ')');
8491 /* If we got one arg but it was just whitespace, call that 0 args. */
8493 register U_CHAR
*bp
= args
[0].raw
;
8494 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
8495 /* cpp.texi says for foo ( ) we provide one argument.
8496 However, if foo wants just 0 arguments, treat this as 0. */
8498 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8503 /* Don't output an error message if we have already output one for
8504 a parse error above. */
8506 if (nargs
== 0 && i
> 0) {
8508 error ("arguments given to macro `%s'", hp
->name
);
8509 } else if (i
< nargs
) {
8510 /* traditional C allows foo() if foo wants one argument. */
8511 if (nargs
== 1 && i
== 0 && traditional
)
8513 /* the rest args token is allowed to absorb 0 tokens */
8514 else if (i
== nargs
- 1 && defn
->rest_args
)
8516 else if (parse_error
)
8519 error ("macro `%s' used without args", hp
->name
);
8521 error ("macro `%s' used with just one arg", hp
->name
);
8523 error ("macro `%s' used with only %d args", hp
->name
, i
);
8524 } else if (i
> nargs
) {
8526 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8529 /* Swallow the closeparen. */
8530 ++instack
[indepth
].bufp
;
8532 /* If macro wants zero args, we parsed the arglist for checking only.
8533 Read directly from the macro definition. */
8535 xbuf
= defn
->expansion
;
8536 xbuf_len
= defn
->length
;
8538 register U_CHAR
*exp
= defn
->expansion
;
8539 register int offset
; /* offset in expansion,
8540 copied a piece at a time */
8541 register int totlen
; /* total amount of exp buffer filled so far */
8543 register struct reflist
*ap
, *last_ap
;
8545 /* Macro really takes args. Compute the expansion of this call. */
8547 /* Compute length in characters of the macro's expansion.
8548 Also count number of times each arg is used. */
8549 xbuf_len
= defn
->length
;
8550 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8552 xbuf_len
+= args
[ap
->argno
].stringified_length
;
8553 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8554 /* Add 4 for two newline-space markers to prevent
8555 token concatenation. */
8556 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8558 /* We have an ordinary (expanded) occurrence of the arg.
8559 So compute its expansion, if we have not already. */
8560 if (args
[ap
->argno
].expanded
== 0) {
8562 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8563 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8566 args
[ap
->argno
].expanded
= obuf
.buf
;
8567 args
[ap
->argno
].expand_length
= obuf
.length
;
8568 args
[ap
->argno
].free2
= obuf
.buf
;
8571 /* Add 4 for two newline-space markers to prevent
8572 token concatenation. */
8573 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
8575 if (args
[ap
->argno
].use_count
< 10)
8576 args
[ap
->argno
].use_count
++;
8579 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8581 /* Generate in XBUF the complete expansion
8582 with arguments substituted in.
8583 TOTLEN is the total size generated so far.
8584 OFFSET is the index in the definition
8585 of where we are copying from. */
8586 offset
= totlen
= 0;
8587 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8588 last_ap
= ap
, ap
= ap
->next
) {
8589 register struct argdata
*arg
= &args
[ap
->argno
];
8590 int count_before
= totlen
;
8592 /* Add chars to XBUF. */
8593 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8594 xbuf
[totlen
++] = exp
[offset
];
8596 /* If followed by an empty rest arg with concatenation,
8597 delete the last run of nonwhite chars. */
8598 if (rest_zero
&& totlen
> count_before
8599 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8600 || (last_ap
!= NULL
&& last_ap
->rest_args
8601 && last_ap
->raw_after
!= 0))) {
8602 /* Delete final whitespace. */
8603 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8607 /* Delete the nonwhites before them. */
8608 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8613 if (ap
->stringify
!= 0) {
8614 int arglen
= arg
->raw_length
;
8620 && (c
= arg
->raw
[i
], is_space
[c
]))
8623 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8626 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8627 for (; i
< arglen
; i
++) {
8631 /* Special markers Newline Space
8632 generate nothing for a stringified argument. */
8633 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
8638 /* Internal sequences of whitespace are replaced by one space
8639 except within an string or char token. */
8640 if (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space
[c
]) {
8642 /* Note that Newline Space does occur within whitespace
8643 sequences; consider it part of the sequence. */
8644 if (c
== '\n' && is_space
[arg
->raw
[i
+1]])
8646 else if (c
!= '\n' && is_space
[c
])
8661 else if (in_string
) {
8666 #ifdef MULTIBYTE_CHARS
8668 length
= local_mblen (arg
->raw
+ i
, arglen
- i
);
8671 bcopy (arg
->raw
+ i
, xbuf
+ totlen
, length
);
8678 } else if (c
== '\"' || c
== '\'')
8682 /* Escape these chars */
8683 if (c
== '\"' || (in_string
&& c
== '\\'))
8684 xbuf
[totlen
++] = '\\';
8685 /* We used to output e.g. \008 for control characters here,
8686 but this doesn't conform to the C Standard.
8687 Just output the characters as-is. */
8691 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8692 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8693 U_CHAR
*p1
= arg
->raw
;
8694 U_CHAR
*l1
= p1
+ arg
->raw_length
;
8695 if (ap
->raw_before
!= 0) {
8696 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8697 while (p1
!= l1
&& is_idchar
[*p1
])
8698 xbuf
[totlen
++] = *p1
++;
8699 /* Delete any no-reexpansion marker that follows
8700 an identifier at the beginning of the argument
8701 if the argument is concatenated with what precedes it. */
8702 if (p1
[0] == '\n' && p1
[1] == '-')
8704 } else if (!traditional
) {
8705 /* Ordinary expanded use of the argument.
8706 Put in newline-space markers to prevent token pasting. */
8707 xbuf
[totlen
++] = '\n';
8708 xbuf
[totlen
++] = ' ';
8710 if (ap
->raw_after
!= 0) {
8711 /* Arg is concatenated after: delete trailing whitespace,
8712 whitespace markers, and no-reexpansion markers. */
8714 if (is_space
[l1
[-1]]) l1
--;
8715 else if (l1
[-1] == '-') {
8716 U_CHAR
*p2
= l1
- 1;
8717 /* If a `-' is preceded by an odd number of newlines then it
8718 and the last newline are a no-reexpansion marker. */
8719 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8720 if ((l1
- 1 - p2
) & 1) {
8729 bcopy ((char *) p1
, (char *) (xbuf
+ totlen
), l1
- p1
);
8731 if (!traditional
&& ap
->raw_after
== 0) {
8732 /* Ordinary expanded use of the argument.
8733 Put in newline-space markers to prevent token pasting. */
8734 xbuf
[totlen
++] = '\n';
8735 xbuf
[totlen
++] = ' ';
8738 /* Ordinary expanded use of the argument.
8739 Put in newline-space markers to prevent token pasting. */
8741 xbuf
[totlen
++] = '\n';
8742 xbuf
[totlen
++] = ' ';
8744 bcopy ((char *) arg
->expanded
, (char *) (xbuf
+ totlen
),
8745 arg
->expand_length
);
8746 totlen
+= arg
->expand_length
;
8748 xbuf
[totlen
++] = '\n';
8749 xbuf
[totlen
++] = ' ';
8751 /* If a macro argument with newlines is used multiple times,
8752 then only expand the newlines once. This avoids creating output
8753 lines which don't correspond to any input line, which confuses
8755 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8756 /* Don't bother doing change_newlines for subsequent
8760 = change_newlines (arg
->expanded
, arg
->expand_length
);
8764 if (totlen
> xbuf_len
)
8768 /* If there is anything left of the definition after handling
8769 the arg list, copy that in too. */
8771 for (i
= offset
; i
< defn
->length
; i
++) {
8772 /* if we've reached the end of the macro */
8775 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8776 && last_ap
->raw_after
!= 0))
8777 xbuf
[totlen
++] = exp
[i
];
8783 for (i
= 0; i
< nargs
; i
++) {
8784 if (args
[i
].free1
!= 0)
8785 free (args
[i
].free1
);
8786 if (args
[i
].free2
!= 0)
8787 free (args
[i
].free2
);
8791 xbuf
= defn
->expansion
;
8792 xbuf_len
= defn
->length
;
8795 /* Now put the expansion on the input stack
8796 so our caller will commence reading from it. */
8798 register FILE_BUF
*ip2
;
8800 ip2
= &instack
[++indepth
];
8803 ip2
->nominal_fname
= 0;
8804 ip2
->nominal_fname_len
= 0;
8806 /* This may not be exactly correct, but will give much better error
8807 messages for nested macro calls than using a line number of zero. */
8808 ip2
->lineno
= start_line
;
8810 ip2
->length
= xbuf_len
;
8812 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8814 ip2
->if_stack
= if_stack
;
8815 ip2
->system_header_p
= 0;
8817 /* Recursive macro use sometimes works traditionally.
8818 #define foo(x,y) bar (x (y,0), y)
8822 hp
->type
= T_DISABLED
;
8826 /* Parse a macro argument and store the info on it into *ARGPTR.
8827 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8828 Return nonzero to indicate a syntax error. */
8831 macarg (argptr
, rest_args
)
8832 register struct argdata
*argptr
;
8835 FILE_BUF
*ip
= &instack
[indepth
];
8841 /* Try to parse as much of the argument as exists at this
8842 input stack level. */
8843 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
,
8844 &paren
, &newlines
, &comments
, rest_args
);
8846 /* If we find the end of the argument at this level,
8847 set up *ARGPTR to point at it in the input stack. */
8848 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
8849 && bp
!= ip
->buf
+ ip
->length
) {
8851 argptr
->raw
= ip
->bufp
;
8852 argptr
->raw_length
= bp
- ip
->bufp
;
8853 argptr
->newlines
= newlines
;
8857 /* This input stack level ends before the macro argument does.
8858 We must pop levels and keep parsing.
8859 Therefore, we must allocate a temporary buffer and copy
8860 the macro argument into it. */
8861 int bufsize
= bp
- ip
->bufp
;
8862 int extra
= newlines
;
8863 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8864 int final_start
= 0;
8866 bcopy ((char *) ip
->bufp
, (char *) buffer
, bufsize
);
8868 ip
->lineno
+= newlines
;
8870 while (bp
== ip
->buf
+ ip
->length
) {
8871 if (instack
[indepth
].macro
== 0) {
8872 result
= "unterminated macro call";
8875 ip
->macro
->type
= T_MACRO
;
8877 free (ip
->free_ptr
);
8878 ip
= &instack
[--indepth
];
8881 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
, &paren
,
8882 &newlines
, &comments
, rest_args
);
8883 final_start
= bufsize
;
8884 bufsize
+= bp
- ip
->bufp
;
8886 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
8887 bcopy ((char *) ip
->bufp
, (char *) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
8890 ip
->lineno
+= newlines
;
8893 /* Now, if arg is actually wanted, record its raw form,
8894 discarding comments and duplicating newlines in whatever
8895 part of it did not come from a macro expansion.
8896 EXTRA space has been preallocated for duplicating the newlines.
8897 FINAL_START is the index of the start of that part. */
8899 argptr
->raw
= buffer
;
8900 argptr
->raw_length
= bufsize
;
8901 argptr
->free1
= buffer
;
8902 argptr
->newlines
= newlines
;
8903 if ((newlines
|| comments
) && ip
->fname
!= 0)
8906 discard_comments (argptr
->raw
+ final_start
,
8907 argptr
->raw_length
- final_start
,
8909 argptr
->raw
[argptr
->raw_length
] = 0;
8910 if (argptr
->raw_length
> bufsize
+ extra
)
8915 /* If we are not discarding this argument,
8916 macroexpand it and compute its length as stringified.
8917 All this info goes into *ARGPTR. */
8920 register U_CHAR
*buf
, *lim
;
8921 register int totlen
;
8924 lim
= buf
+ argptr
->raw_length
;
8926 while (buf
!= lim
&& is_space
[*buf
])
8928 while (buf
!= lim
&& is_space
[lim
[-1]])
8930 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
8931 while (buf
!= lim
) {
8932 register U_CHAR c
= *buf
++;
8934 /* Internal sequences of whitespace are replaced by one space
8935 in most cases, but not always. So count all the whitespace
8936 in case we need to keep it all. */
8939 SKIP_ALL_WHITE_SPACE (buf
);
8942 if (c
== '\"' || c
== '\\') /* escape these chars */
8945 argptr
->stringified_length
= totlen
;
8950 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8951 taken from the expansion of MACRO,
8952 counting parens in *DEPTHPTR,
8953 and return if reach LIMIT
8954 or before a `)' that would make *DEPTHPTR negative
8955 or before a comma when *DEPTHPTR is zero.
8956 Single and double quotes are matched and termination
8957 is inhibited within them. Comments also inhibit it.
8958 Value returned is pointer to stopping place.
8960 Increment *NEWLINES each time a newline is passed.
8961 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
8962 Set *COMMENTS to 1 if a comment is seen. */
8965 macarg1 (start
, limit
, macro
, depthptr
, newlines
, comments
, rest_args
)
8967 register U_CHAR
*limit
;
8968 struct hashnode
*macro
;
8969 int *depthptr
, *newlines
, *comments
;
8972 register U_CHAR
*bp
= start
;
8974 while (bp
< limit
) {
8980 if (--(*depthptr
) < 0)
8984 /* Traditionally, backslash makes following char not special. */
8985 if (traditional
&& bp
+ 1 < limit
&& bp
[1] != '\n')
8994 if (bp
[1] == '\\' && bp
[2] == '\n')
8995 newline_fix (bp
+ 1);
8998 for (bp
+= 2; bp
< limit
; bp
++) {
9001 else if (*bp
== '*') {
9002 if (bp
[-1] == '/' && warn_comments
)
9003 warning ("`/*' within comment");
9004 if (bp
[1] == '\\' && bp
[2] == '\n')
9005 newline_fix (bp
+ 1);
9013 #ifdef MULTIBYTE_CHARS
9015 length
= local_mblen (bp
, limit
- bp
);
9021 } else if (bp
[1] == '/' && cplusplus_comments
) {
9023 for (bp
+= 2; bp
< limit
; bp
++) {
9028 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
9032 warning ("multiline `//' comment");
9037 #ifdef MULTIBYTE_CHARS
9039 length
= local_mblen (bp
, limit
- bp
);
9051 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
9057 while (*bp
== '\\' && bp
[1] == '\n') {
9062 } else if (*bp
== '\n') {
9069 #ifdef MULTIBYTE_CHARS
9071 length
= local_mblen (bp
, limit
- bp
);
9080 /* if we've returned to lowest level and we aren't absorbing all args */
9081 if ((*depthptr
) == 0 && rest_args
== 0)
9091 /* Discard comments and duplicate newlines
9092 in the string of length LENGTH at START,
9093 except inside of string constants.
9094 The string is copied into itself with its beginning staying fixed.
9096 NEWLINES is the number of newlines that must be duplicated.
9097 We assume that that much extra space is available past the end
9101 discard_comments (start
, length
, newlines
)
9106 register U_CHAR
*ibp
;
9107 register U_CHAR
*obp
;
9108 register U_CHAR
*limit
;
9111 /* If we have newlines to duplicate, copy everything
9112 that many characters up. Then, in the second part,
9113 we will have room to insert the newlines
9115 NEWLINES may actually be too large, because it counts
9116 newlines in string constants, and we don't duplicate those.
9117 But that does no harm. */
9119 ibp
= start
+ length
;
9120 obp
= ibp
+ newlines
;
9122 while (limit
!= ibp
)
9126 ibp
= start
+ newlines
;
9127 limit
= start
+ length
+ newlines
;
9130 while (ibp
< limit
) {
9131 *obp
++ = c
= *ibp
++;
9134 /* Duplicate the newline. */
9146 if (*ibp
== '\\' && ibp
[1] == '\n')
9148 /* Delete any comment. */
9149 if (cplusplus_comments
&& ibp
[0] == '/') {
9150 /* Comments are equivalent to spaces. */
9157 if (*ibp
== '\\' && ibp
+ 1 < limit
&& ibp
[1] == '\n')
9161 #ifdef MULTIBYTE_CHARS
9162 int length
= local_mblen (ibp
, limit
- ibp
);
9164 ibp
+= (length
- 1);
9171 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
9173 /* Comments are equivalent to spaces.
9174 For -traditional, a comment is equivalent to nothing. */
9179 while (++ibp
< limit
) {
9180 if (ibp
[0] == '*') {
9181 if (ibp
[1] == '\\' && ibp
[2] == '\n')
9182 newline_fix (ibp
+ 1);
9183 if (ibp
[1] == '/') {
9190 #ifdef MULTIBYTE_CHARS
9191 int length
= local_mblen (ibp
, limit
- ibp
);
9193 ibp
+= (length
- 1);
9201 /* Notice and skip strings, so that we don't
9202 think that comments start inside them,
9203 and so we don't duplicate newlines in them. */
9206 while (ibp
< limit
) {
9207 *obp
++ = c
= *ibp
++;
9215 else if (c
== '\\') {
9216 if (ibp
< limit
&& *ibp
== '\n') {
9220 while (*ibp
== '\\' && ibp
[1] == '\n')
9228 #ifdef MULTIBYTE_CHARS
9231 length
= local_mblen (ibp
, limit
- ibp
);
9235 bcopy (ibp
, obp
, length
);
9252 /* Turn newlines to spaces in the string of length LENGTH at START,
9253 except inside of string constants.
9254 The string is copied into itself with its beginning staying fixed. */
9257 change_newlines (start
, length
)
9261 register U_CHAR
*ibp
;
9262 register U_CHAR
*obp
;
9263 register U_CHAR
*limit
;
9267 limit
= start
+ length
;
9270 while (ibp
< limit
) {
9271 *obp
++ = c
= *ibp
++;
9274 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9275 string. Skip past the newline and its duplicate.
9276 Put a space in the output. */
9287 /* Notice and skip strings, so that we don't delete newlines in them. */
9290 while (ibp
< limit
) {
9291 *obp
++ = c
= *ibp
++;
9294 else if (c
== '\\' && ibp
< limit
&& *ibp
== '\n')
9303 #ifdef MULTIBYTE_CHARS
9306 length
= local_mblen (ibp
, limit
- ibp
);
9310 bcopy (ibp
, obp
, length
);
9327 /* my_strerror - return the descriptive text associated with an
9331 my_strerror (errnum
)
9337 #ifndef HAVE_STRERROR
9338 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
9340 result
= strerror (errnum
);
9343 /* VAXCRTL's strerror() takes an optional second argument, which only
9344 matters when the first argument is EVMSERR. However, it's simplest
9345 just to pass it unconditionally. `vaxc$errno' is declared in
9346 <errno.h>, and maintained by the library in parallel with `errno'.
9347 We assume that caller's `errnum' either matches the last setting of
9348 `errno' by the library or else does not have the value `EVMSERR'. */
9350 result
= strerror (errnum
, vaxc$errno
);
9354 result
= "undocumented I/O error";
9359 /* error - print error message and increment count of errors. */
9362 error
VPROTO ((char * msg
, ...))
9364 #ifndef ANSI_PROTOTYPES
9369 VA_START (args
, msg
);
9371 #ifndef ANSI_PROTOTYPES
9372 msg
= va_arg (args
, char *);
9385 FILE_BUF
*ip
= NULL
;
9387 print_containing_files ();
9389 for (i
= indepth
; i
>= 0; i
--)
9390 if (instack
[i
].fname
!= NULL
) {
9396 eprint_string (ip
->nominal_fname
, ip
->nominal_fname_len
);
9397 fprintf (stderr
, ":%d: ", ip
->lineno
);
9399 vfprintf (stderr
, msg
, args
);
9400 fprintf (stderr
, "\n");
9404 /* Error including a message from `errno'. */
9407 error_from_errno (name
)
9412 FILE_BUF
*ip
= NULL
;
9414 print_containing_files ();
9416 for (i
= indepth
; i
>= 0; i
--)
9417 if (instack
[i
].fname
!= NULL
) {
9423 eprint_string (ip
->nominal_fname
, ip
->nominal_fname_len
);
9424 fprintf (stderr
, ":%d: ", ip
->lineno
);
9427 fprintf (stderr
, "%s: %s\n", name
, my_strerror (e
));
9432 /* Print error message but don't count it. */
9435 warning
VPROTO ((char * msg
, ...))
9437 #ifndef ANSI_PROTOTYPES
9442 VA_START (args
, msg
);
9444 #ifndef ANSI_PROTOTYPES
9445 msg
= va_arg (args
, char *);
9448 vwarning (msg
, args
);
9453 vwarning (msg
, args
)
9458 FILE_BUF
*ip
= NULL
;
9460 if (inhibit_warnings
)
9463 if (warnings_are_errors
)
9466 print_containing_files ();
9468 for (i
= indepth
; i
>= 0; i
--)
9469 if (instack
[i
].fname
!= NULL
) {
9475 eprint_string (ip
->nominal_fname
, ip
->nominal_fname_len
);
9476 fprintf (stderr
, ":%d: ", ip
->lineno
);
9478 fprintf (stderr
, "warning: ");
9479 vfprintf (stderr
, msg
, args
);
9480 fprintf (stderr
, "\n");
9484 error_with_line
VPROTO ((int line
, char * msg
, ...))
9486 #ifndef ANSI_PROTOTYPES
9492 VA_START (args
, msg
);
9494 #ifndef ANSI_PROTOTYPES
9495 line
= va_arg (args
, int);
9496 msg
= va_arg (args
, char *);
9499 verror_with_line (line
, msg
, args
);
9504 verror_with_line (line
, msg
, args
)
9510 FILE_BUF
*ip
= NULL
;
9512 print_containing_files ();
9514 for (i
= indepth
; i
>= 0; i
--)
9515 if (instack
[i
].fname
!= NULL
) {
9521 eprint_string (ip
->nominal_fname
, ip
->nominal_fname_len
);
9522 fprintf (stderr
, ":%d: ", line
);
9524 vfprintf (stderr
, msg
, args
);
9525 fprintf (stderr
, "\n");
9530 warning_with_line
VPROTO ((int line
, char * msg
, ...))
9532 #ifndef ANSI_PROTOTYPES
9538 VA_START (args
, msg
);
9540 #ifndef ANSI_PROTOTYPES
9541 line
= va_arg (args
, int);
9542 msg
= va_arg (args
, char *);
9545 vwarning_with_line (line
, msg
, args
);
9550 vwarning_with_line (line
, msg
, args
)
9556 FILE_BUF
*ip
= NULL
;
9558 if (inhibit_warnings
)
9561 if (warnings_are_errors
)
9564 print_containing_files ();
9566 for (i
= indepth
; i
>= 0; i
--)
9567 if (instack
[i
].fname
!= NULL
) {
9573 eprint_string (ip
->nominal_fname
, ip
->nominal_fname_len
);
9574 fprintf (stderr
, line
? ":%d: " : ": ", line
);
9576 fprintf (stderr
, "warning: ");
9577 vfprintf (stderr
, msg
, args
);
9578 fprintf (stderr
, "\n");
9581 /* Print an error message and maybe count it. */
9584 pedwarn
VPROTO ((char * msg
, ...))
9586 #ifndef ANSI_PROTOTYPES
9591 VA_START (args
, msg
);
9593 #ifndef ANSI_PROTOTYPES
9594 msg
= va_arg (args
, char *);
9597 if (pedantic_errors
)
9600 vwarning (msg
, args
);
9605 pedwarn_with_line
VPROTO ((int line
, char * msg
, ...))
9607 #ifndef ANSI_PROTOTYPES
9613 VA_START (args
, msg
);
9615 #ifndef ANSI_PROTOTYPES
9616 line
= va_arg (args
, int);
9617 msg
= va_arg (args
, char *);
9620 if (pedantic_errors
)
9621 verror_with_line (line
, msg
, args
);
9623 vwarning_with_line (line
, msg
, args
);
9627 /* Report a warning (or an error if pedantic_errors)
9628 giving specified file name and line number, not current. */
9631 pedwarn_with_file_and_line
VPROTO ((char *file
, size_t file_len
, int line
,
9634 #ifndef ANSI_PROTOTYPES
9642 if (!pedantic_errors
&& inhibit_warnings
)
9645 VA_START (args
, msg
);
9647 #ifndef ANSI_PROTOTYPES
9648 file
= va_arg (args
, char *);
9649 file_len
= va_arg (args
, size_t);
9650 line
= va_arg (args
, int);
9651 msg
= va_arg (args
, char *);
9655 eprint_string (file
, file_len
);
9656 fprintf (stderr
, ":%d: ", line
);
9658 if (pedantic_errors
)
9660 if (!pedantic_errors
)
9661 fprintf (stderr
, "warning: ");
9663 vfprintf (stderr
, msg
, args
);
9665 fprintf (stderr
, "\n");
9668 /* Print the file names and line numbers of the #include
9669 directives which led to the current file. */
9672 print_containing_files ()
9674 FILE_BUF
*ip
= NULL
;
9678 /* If stack of files hasn't changed since we last printed
9679 this info, don't repeat it. */
9680 if (last_error_tick
== input_file_stack_tick
)
9683 for (i
= indepth
; i
>= 0; i
--)
9684 if (instack
[i
].fname
!= NULL
) {
9689 /* Give up if we don't find a source file. */
9693 /* Find the other, outer source files. */
9694 for (i
--; i
>= 0; i
--)
9695 if (instack
[i
].fname
!= NULL
) {
9699 fprintf (stderr
, "In file included");
9701 fprintf (stderr
, ",\n ");
9704 fprintf (stderr
, " from ");
9705 eprint_string (ip
->nominal_fname
, ip
->nominal_fname_len
);
9706 fprintf (stderr
, ":%d", ip
->lineno
);
9709 fprintf (stderr
, ":\n");
9711 /* Record we have printed the status as of this time. */
9712 last_error_tick
= input_file_stack_tick
;
9715 /* Return the line at which an error occurred.
9716 The error is not necessarily associated with the current spot
9717 in the input stack, so LINE says where. LINE will have been
9718 copied from ip->lineno for the current input level.
9719 If the current level is for a file, we return LINE.
9720 But if the current level is not for a file, LINE is meaningless.
9721 In that case, we return the lineno of the innermost file. */
9724 line_for_error (line
)
9730 for (i
= indepth
; i
>= 0; ) {
9731 if (instack
[i
].fname
!= 0)
9736 line1
= instack
[i
].lineno
;
9744 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9746 * As things stand, nothing is ever placed in the output buffer to be
9747 * removed again except when it's KNOWN to be part of an identifier,
9748 * so flushing and moving down everything left, instead of expanding,
9752 /* You might think void was cleaner for the return type,
9753 but that would get type mismatch in check_expand in strict ANSI. */
9756 grow_outbuf (obuf
, needed
)
9757 register FILE_BUF
*obuf
;
9758 register int needed
;
9763 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9766 /* Make it at least twice as big as it is now. */
9768 /* Make it have at least 150% of the free space we will need. */
9769 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9770 if (minsize
> obuf
->length
)
9771 obuf
->length
= minsize
;
9773 if ((p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
)) == NULL
)
9776 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9782 /* Symbol table for macro names and special symbols */
9785 * install a name in the main hash table, even if it is already there.
9786 * name stops with first non alphanumeric, except leading '#'.
9787 * caller must check against redefinition if that is desired.
9788 * delete_macro () removes things installed by install () in fifo order.
9789 * this is important because of the `defined' special symbol used
9790 * in #if, and also if pushdef/popdef directives are ever implemented.
9792 * If LEN is >= 0, it is the length of the name.
9793 * Otherwise, compute the length by scanning the entire name.
9795 * If HASH is >= 0, it is the precomputed hash code.
9796 * Otherwise, compute the hash code.
9800 install (name
, len
, type
, value
, hash
)
9803 enum node_type type
;
9807 register HASHNODE
*hp
;
9808 register int i
, bucket
;
9809 register U_CHAR
*p
, *q
;
9813 while (is_idchar
[*p
])
9819 hash
= hashf (name
, len
, HASHSIZE
);
9821 i
= sizeof (HASHNODE
) + len
+ 1;
9822 hp
= (HASHNODE
*) xmalloc (i
);
9824 hp
->bucket_hdr
= &hashtab
[bucket
];
9825 hp
->next
= hashtab
[bucket
];
9826 hashtab
[bucket
] = hp
;
9828 if (hp
->next
!= NULL
)
9829 hp
->next
->prev
= hp
;
9832 hp
->value
.cpval
= value
;
9833 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9836 for (i
= 0; i
< len
; i
++)
9843 * find the most recent hash node for name "name" (ending with first
9844 * non-identifier char) installed by install
9846 * If LEN is >= 0, it is the length of the name.
9847 * Otherwise, compute the length by scanning the entire name.
9849 * If HASH is >= 0, it is the precomputed hash code.
9850 * Otherwise, compute the hash code.
9854 lookup (name
, len
, hash
)
9859 register U_CHAR
*bp
;
9860 register HASHNODE
*bucket
;
9863 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
9868 hash
= hashf (name
, len
, HASHSIZE
);
9870 bucket
= hashtab
[hash
];
9872 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
9874 bucket
= bucket
->next
;
9880 * Delete a hash node. Some weirdness to free junk from macros.
9881 * More such weirdness will have to be added if you define more hash
9882 * types that need it.
9885 /* Note that the DEFINITION of a macro is removed from the hash table
9886 but its storage is not freed. This would be a storage leak
9887 except that it is not reasonable to keep undefining and redefining
9888 large numbers of macros many times.
9889 In any case, this is necessary, because a macro can be #undef'd
9890 in the middle of reading the arguments to a call to it.
9891 If #undef freed the DEFINITION, that would crash. */
9898 if (hp
->prev
!= NULL
)
9899 hp
->prev
->next
= hp
->next
;
9900 if (hp
->next
!= NULL
)
9901 hp
->next
->prev
= hp
->prev
;
9903 /* Make sure that the bucket chain header that the deleted guy was
9904 on points to the right thing afterwards. */
9905 if (hp
== *hp
->bucket_hdr
)
9906 *hp
->bucket_hdr
= hp
->next
;
9909 if (hp
->type
== T_MACRO
) {
9910 DEFINITION
*d
= hp
->value
.defn
;
9911 struct reflist
*ap
, *nextap
;
9913 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
9924 * return hash function on name. must be compatible with the one
9925 * computed a step at a time, elsewhere
9929 hashf (name
, len
, hashsize
)
9930 register U_CHAR
*name
;
9937 r
= HASHSTEP (r
, *name
++);
9939 return MAKE_POS (r
) % hashsize
;
9943 /* Dump the definition of a single macro HP to OF. */
9946 dump_single_macro (hp
, of
)
9947 register HASHNODE
*hp
;
9950 register DEFINITION
*defn
= hp
->value
.defn
;
9956 /* Print the definition of the macro HP. */
9958 fprintf (of
, "#define %s", hp
->name
);
9960 if (defn
->nargs
>= 0) {
9964 for (i
= 0; i
< defn
->nargs
; i
++) {
9965 dump_arg_n (defn
, i
, of
);
9966 if (i
+ 1 < defn
->nargs
)
9976 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
9977 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
9978 offset
+= ap
->nchars
;
9980 if (ap
->nchars
!= 0)
9982 if (ap
->stringify
) {
9983 switch (ap
->stringify
) {
9984 case SHARP_TOKEN
: fprintf (of
, "#"); break;
9985 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
9986 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
9987 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
9991 if (ap
->raw_before
!= 0) {
9993 switch (ap
->raw_before
) {
9994 case WHITE_SHARP_TOKEN
:
9995 case WHITE_PERCENT_COLON_TOKEN
:
10002 switch (ap
->raw_before
) {
10003 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10004 case WHITE_SHARP_TOKEN
: fprintf (of
, "## "); break;
10005 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10006 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%: "); break;
10013 dump_arg_n (defn
, ap
->argno
, of
);
10014 if (!traditional
&& ap
->raw_after
!= 0) {
10015 switch (ap
->raw_after
) {
10016 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10017 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
10018 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10019 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
10025 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
10026 fprintf (of
, "\n");
10029 /* Dump all macro definitions as #defines to stdout. */
10036 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
10037 register HASHNODE
*hp
;
10039 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
10040 if (hp
->type
== T_MACRO
)
10041 dump_single_macro (hp
, stdout
);
10046 /* Output to OF a substring of a macro definition.
10047 BASE is the beginning of the definition.
10048 Output characters START thru LENGTH.
10049 Unless traditional, discard newlines outside of strings, thus
10050 converting funny-space markers to ordinary spaces. */
10053 dump_defn_1 (base
, start
, length
, of
)
10059 U_CHAR
*p
= base
+ start
;
10060 U_CHAR
*limit
= base
+ start
+ length
;
10063 fwrite (p
, sizeof (*p
), length
, of
);
10065 while (p
< limit
) {
10066 if (*p
== '\"' || *p
=='\'') {
10067 U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
10068 NULL_PTR
, NULL_PTR
);
10069 fwrite (p
, sizeof (*p
), p1
- p
, of
);
10080 /* Print the name of argument number ARGNUM of macro definition DEFN
10082 Recall that DEFN->args.argnames contains all the arg names
10083 concatenated in reverse order with comma-space in between. */
10086 dump_arg_n (defn
, argnum
, of
)
10091 register U_CHAR
*p
= defn
->args
.argnames
;
10092 while (argnum
+ 1 < defn
->nargs
) {
10093 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
10097 while (*p
&& *p
!= ',') {
10103 /* Initialize syntactic classifications of characters. */
10106 initialize_char_syntax ()
10111 * Set up is_idchar and is_idstart tables. These should be
10112 * faster than saying (is_alpha (c) || c == '_'), etc.
10113 * Set up these things before calling any routines tthat
10116 for (i
= 'a'; i
<= 'z'; i
++) {
10117 is_idchar
[i
- 'a' + 'A'] = 1;
10119 is_idstart
[i
- 'a' + 'A'] = 1;
10122 for (i
= '0'; i
<= '9'; i
++)
10124 is_idchar
['_'] = 1;
10125 is_idstart
['_'] = 1;
10126 is_idchar
['$'] = 1;
10127 is_idstart
['$'] = 1;
10129 /* horizontal space table */
10130 is_hor_space
[' '] = 1;
10131 is_hor_space
['\t'] = 1;
10132 is_hor_space
['\v'] = 1;
10133 is_hor_space
['\f'] = 1;
10134 is_hor_space
['\r'] = 1;
10137 is_space
['\t'] = 1;
10138 is_space
['\v'] = 1;
10139 is_space
['\f'] = 1;
10140 is_space
['\n'] = 1;
10141 is_space
['\r'] = 1;
10143 char_name
['\v'] = "vertical tab";
10144 char_name
['\f'] = "formfeed";
10145 char_name
['\r'] = "carriage return";
10148 /* Initialize the built-in macros. */
10151 initialize_builtins (inp
, outp
)
10155 install ((U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
10156 install ((U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
10157 install ((U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
10158 install ((U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
10159 install ((U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
10160 install ((U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
10161 #ifndef NO_BUILTIN_SIZE_TYPE
10162 install ((U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
10164 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10165 install ((U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
10167 install ((U_CHAR
*) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, NULL_PTR
, -1);
10168 install ((U_CHAR
*) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
,
10170 install ((U_CHAR
*) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
,
10172 install ((U_CHAR
*) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE
,
10174 install ((U_CHAR
*) "__TIME__", -1, T_TIME
, NULL_PTR
, -1);
10175 if (!traditional
) {
10176 install ((U_CHAR
*) "__STDC__", -1, T_CONST
, "1", -1);
10177 install ((U_CHAR
*) "__STDC_VERSION__", -1, T_CONST
, "199409L", -1);
10180 install ((U_CHAR
*) "__OBJC__", -1, T_CONST
, "1", -1);
10181 /* This is supplied using a -D by the compiler driver
10182 so that it is present only when truly compiling with GNU C. */
10183 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10184 install ((U_CHAR
*) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST
, "1", -1);
10188 char directive
[2048];
10189 U_CHAR
*udirective
= (U_CHAR
*) directive
;
10190 register struct directive
*dp
= &directive_table
[0];
10191 struct tm
*timebuf
= timestamp ();
10193 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
10194 instack
[0].nominal_fname
);
10195 output_line_directive (inp
, outp
, 0, same_file
);
10196 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10199 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
10200 output_line_directive (inp
, outp
, 0, same_file
);
10201 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10204 #ifndef NO_BUILTIN_SIZE_TYPE
10205 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
10206 output_line_directive (inp
, outp
, 0, same_file
);
10207 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10211 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10212 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
10213 output_line_directive (inp
, outp
, 0, same_file
);
10214 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10218 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
10219 output_line_directive (inp
, outp
, 0, same_file
);
10220 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10223 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
10224 monthnames
[timebuf
->tm_mon
],
10225 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
10226 output_line_directive (inp
, outp
, 0, same_file
);
10227 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10230 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
10231 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
10232 output_line_directive (inp
, outp
, 0, same_file
);
10233 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10238 sprintf (directive
, " __STDC__ 1");
10239 output_line_directive (inp
, outp
, 0, same_file
);
10240 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10245 sprintf (directive
, " __OBJC__ 1");
10246 output_line_directive (inp
, outp
, 0, same_file
);
10247 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10254 * process a given definition string, for initialization
10255 * If STR is just an identifier, define it with value 1.
10256 * If STR has anything after the identifier, then it should
10257 * be identifier=definition.
10261 make_definition (str
)
10265 struct directive
*kt
;
10268 p
= buf
= (U_CHAR
*) str
;
10269 if (!is_idstart
[*p
]) {
10270 error ("malformed option `-D %s'", str
);
10273 while (is_idchar
[*++p
])
10276 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
10279 p
= (U_CHAR
*) str
; /* Error */
10282 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
10283 strcpy ((char *)buf
, str
);
10284 strcat ((char *)buf
, " 1");
10285 } else if (*p
!= '=') {
10286 error ("malformed option `-D %s'", str
);
10290 /* Copy the entire option so we can modify it. */
10291 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
10292 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
10293 /* Change the = to a space. */
10294 buf
[p
- (U_CHAR
*) str
] = ' ';
10295 /* Scan for any backslash-newline and remove it. */
10297 q
= &buf
[p
- (U_CHAR
*) str
];
10299 if (*p
== '\"' || *p
== '\'') {
10300 int unterminated
= 0;
10301 U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
10302 NULL_PTR
, NULL_PTR
, &unterminated
);
10307 } else if (*p
== '\\' && p
[1] == '\n')
10309 /* Change newline chars into newline-markers. */
10310 else if (*p
== '\n')
10322 ip
= &instack
[++indepth
];
10323 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10324 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10326 ip
->buf
= ip
->bufp
= buf
;
10327 ip
->length
= strlen ((char *) buf
);
10331 ip
->if_stack
= if_stack
;
10332 ip
->system_header_p
= 0;
10334 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
10337 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10338 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
10342 /* JF, this does the work for the -U option */
10345 make_undef (str
, op
)
10350 struct directive
*kt
;
10352 ip
= &instack
[++indepth
];
10353 ip
->nominal_fname
= ip
->fname
= "*undef*";
10354 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10356 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
10357 ip
->length
= strlen (str
);
10361 ip
->if_stack
= if_stack
;
10362 ip
->system_header_p
= 0;
10364 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
10367 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
10371 /* Process the string STR as if it appeared as the body of a #assert.
10372 OPTION is the option name for which STR was the argument. */
10375 make_assertion (option
, str
)
10380 struct directive
*kt
;
10381 U_CHAR
*buf
, *p
, *q
;
10383 /* Copy the entire option so we can modify it. */
10384 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
10385 strcpy ((char *) buf
, str
);
10386 /* Scan for any backslash-newline and remove it. */
10389 if (*p
== '\\' && p
[1] == '\n')
10397 if (!is_idstart
[*p
]) {
10398 error ("malformed option `%s %s'", option
, str
);
10401 while (is_idchar
[*++p
])
10403 SKIP_WHITE_SPACE (p
);
10404 if (! (*p
== 0 || *p
== '(')) {
10405 error ("malformed option `%s %s'", option
, str
);
10409 ip
= &instack
[++indepth
];
10410 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10411 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10413 ip
->buf
= ip
->bufp
= buf
;
10414 ip
->length
= strlen ((char *) buf
);
10418 ip
->if_stack
= if_stack
;
10419 ip
->system_header_p
= 0;
10421 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
10424 /* Pass NULL as output ptr to do_define since we KNOW it never does
10426 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
10430 #ifndef DIR_SEPARATOR
10431 #define DIR_SEPARATOR '/'
10434 /* The previous include prefix, if any, is PREV_FILE_NAME.
10435 Translate any pathnames with COMPONENT.
10436 Allocate a new include prefix whose name is the
10437 simplified concatenation of PREFIX and NAME,
10438 with a trailing / added if needed.
10439 But return 0 if the include prefix should be ignored,
10440 e.g. because it is a duplicate of PREV_FILE_NAME. */
10442 static struct file_name_list
*
10443 new_include_prefix (prev_file_name
, component
, prefix
, name
)
10444 struct file_name_list
*prev_file_name
;
10450 fatal ("Directory name missing after command line option");
10453 /* Ignore the empty string. */
10456 prefix
= update_path (prefix
, component
);
10457 name
= update_path (name
, component
);
10460 struct file_name_list
*dir
10461 = ((struct file_name_list
*)
10462 xmalloc (sizeof (struct file_name_list
)
10463 + strlen (prefix
) + strlen (name
) + 2));
10465 strcpy (dir
->fname
, prefix
);
10466 strcat (dir
->fname
, name
);
10467 len
= simplify_filename (dir
->fname
);
10469 /* Convert directory name to a prefix. */
10470 if (len
&& dir
->fname
[len
- 1] != DIR_SEPARATOR
) {
10471 if (len
== 1 && dir
->fname
[len
- 1] == '.')
10475 /* must be '/', hack_vms_include_specification triggers on it. */
10476 dir
->fname
[len
++] = '/';
10478 dir
->fname
[len
++] = DIR_SEPARATOR
;
10480 dir
->fname
[len
] = 0;
10483 /* Ignore a directory whose name matches the previous one. */
10484 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
10485 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10486 if (!first_bracket_include
)
10487 first_bracket_include
= prev_file_name
;
10493 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10495 /* Add a trailing "." if there is a filename. This increases the number
10496 of systems that can stat directories. We remove it below. */
10499 dir
->fname
[len
] = '.';
10500 dir
->fname
[len
+ 1] = 0;
10503 /* Ignore a nonexistent directory. */
10504 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
10505 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
10506 error_from_errno (dir
->fname
);
10512 dir
->fname
[len
] = 0;
10514 /* Ignore a directory whose identity matches the previous one. */
10516 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
10517 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
10518 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10519 if (!first_bracket_include
)
10520 first_bracket_include
= prev_file_name
;
10527 dir
->c_system_include_path
= 0;
10528 dir
->got_name_map
= 0;
10534 /* Append a chain of `struct file_name_list's
10535 to the end of the main include chain.
10536 FIRST is the beginning of the chain to append, and LAST is the end. */
10539 append_include_chain (first
, last
)
10540 struct file_name_list
*first
, *last
;
10542 struct file_name_list
*dir
;
10544 if (!first
|| !last
)
10550 last_include
->next
= first
;
10552 if (first_bracket_include
== 0)
10553 first_bracket_include
= first
;
10555 for (dir
= first
; ; dir
= dir
->next
) {
10556 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
10557 if (len
> max_include_len
)
10558 max_include_len
= len
;
10564 last_include
= last
;
10567 /* Place into DST a representation of the file named SRC that is suitable
10568 for `make'. Do not null-terminate DST. Return its length. */
10570 quote_string_for_make (dst
, src
)
10585 /* GNU make uses a weird quoting scheme for white space.
10586 A space or tab preceded by 2N+1 backslashes represents
10587 N backslashes followed by space; a space or tab
10588 preceded by 2N backslashes represents N backslashes at
10589 the end of a file name; and backslashes in other
10590 contexts should not be doubled. */
10592 for (q
= p
- 1; src
< q
&& q
[-1] == '\\'; q
--)
10604 goto ordinary_char
;
10610 /* Fall through. This can mishandle things like "$(" but
10611 there's no easy fix. */
10614 /* This can mishandle characters in the string "\0\n%*?[\\~";
10615 exactly which chars are mishandled depends on the `make' version.
10616 We know of no portable solution for this;
10617 even GNU make 3.76.1 doesn't solve the problem entirely.
10618 (Also, '\0' is mishandled due to our calling conventions.) */
10628 /* Add output to `deps_buffer' for the -M switch.
10629 STRING points to the text to be output.
10630 SPACER is ':' for targets, ' ' for dependencies. */
10633 deps_output (string
, spacer
)
10637 int size
= quote_string_for_make ((char *) 0, string
);
10642 #ifndef MAX_OUTPUT_COLUMNS
10643 #define MAX_OUTPUT_COLUMNS 72
10645 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
10646 && 1 < deps_column
) {
10647 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
10654 if (deps_size
+ 2 * size
+ 8 > deps_allocated_size
) {
10655 deps_allocated_size
= (deps_size
+ 2 * size
+ 50) * 2;
10656 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
10658 if (spacer
== ' ') {
10659 deps_buffer
[deps_size
++] = ' ';
10662 quote_string_for_make (&deps_buffer
[deps_size
], string
);
10664 deps_column
+= size
;
10665 if (spacer
== ':') {
10666 deps_buffer
[deps_size
++] = ':';
10669 deps_buffer
[deps_size
] = 0;
10673 fatal
VPROTO ((char * msg
, ...))
10675 #ifndef ANSI_PROTOTYPES
10680 fprintf (stderr
, "%s: ", progname
);
10681 VA_START (args
, msg
);
10683 #ifndef ANSI_PROTOTYPES
10684 msg
= va_arg (args
, char *);
10687 vfprintf (stderr
, msg
, args
);
10689 fprintf (stderr
, "\n");
10690 exit (FATAL_EXIT_CODE
);
10693 /* More 'friendly' abort that prints the line and file.
10694 config.h can #define abort fancy_abort if you like that sort of thing. */
10699 fatal ("Internal gcc abort.");
10703 perror_with_name (name
)
10706 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, my_strerror (errno
));
10711 pfatal_with_name (name
)
10714 perror_with_name (name
);
10718 exit (FATAL_EXIT_CODE
);
10722 /* Handler for SIGPIPE. */
10725 pipe_closed (signo
)
10726 /* If this is missing, some compilers complain. */
10727 int signo ATTRIBUTE_UNUSED
;
10729 fatal ("output pipe has been closed");
10735 fatal ("Memory exhausted.");
10742 register PTR ptr
= (PTR
) malloc (size
);
10749 xrealloc (old
, size
)
10753 register PTR ptr
= (PTR
) realloc (old
, size
);
10760 xcalloc (number
, size
)
10761 size_t number
, size
;
10763 register size_t total
= number
* size
;
10764 register PTR ptr
= (PTR
) malloc (total
);
10767 bzero (ptr
, total
);
10775 size_t size
= strlen (input
);
10776 char *output
= xmalloc (size
+ 1);
10777 strcpy (output
, input
);
10783 /* Under VMS we need to fix up the "include" specification filename.
10785 Rules for possible conversions
10787 fullname tried paths
10790 ./dir/name [.dir]name
10792 /name [000000]name, name
10793 dir/name dir:[000000]name, dir:name, dir/name
10794 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10795 path:/name path:[000000]name, path:name
10796 path:/dir/name path:[000000.dir]name, path:[dir]name
10797 path:dir/name path:[dir]name
10798 [path]:[dir]name [path.dir]name
10799 path/[dir]name [path.dir]name
10801 The path:/name input is constructed when expanding <> includes.
10803 return 1 if name was changed, 0 else. */
10806 hack_vms_include_specification (fullname
, vaxc_include
)
10810 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
10811 int f
, check_filename_before_returning
, must_revert
;
10814 check_filename_before_returning
= 0;
10816 /* See if we can find a 1st slash. If not, there's no path information. */
10817 first_slash
= index (fullname
, '/');
10818 if (first_slash
== 0)
10819 return 0; /* Nothing to do!!! */
10821 /* construct device spec if none given. */
10823 if (index (fullname
, ':') == 0)
10826 /* If fullname has a slash, take it as device spec. */
10828 if (first_slash
== fullname
)
10830 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
10832 *first_slash
= ':'; /* make device spec */
10833 for (basename
= fullname
; *basename
!= 0; basename
++)
10834 *basename
= *(basename
+1); /* remove leading slash */
10836 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
10837 && (first_slash
[-1] != ':')
10838 && (first_slash
[-1] != ']')) /* or a vms path */
10840 *first_slash
= ':';
10842 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
10843 && (first_slash
[-1] == '.'))
10847 /* Get part after first ':' (basename[-1] == ':')
10848 or last '/' (basename[-1] == '/'). */
10850 basename
= base_name (fullname
);
10853 * Check if we have a vax-c style '#include filename'
10854 * and add the missing .h
10857 if (vaxc_include
&& !index (basename
,'.'))
10858 strcat (basename
, ".h");
10860 local_ptr
= Local
; /* initialize */
10862 /* We are trying to do a number of things here. First of all, we are
10863 trying to hammer the filenames into a standard format, such that later
10864 processing can handle them.
10866 If the file name contains something like [dir.], then it recognizes this
10867 as a root, and strips the ".]". Later processing will add whatever is
10868 needed to get things working properly.
10870 If no device is specified, then the first directory name is taken to be
10871 a device name (or a rooted logical). */
10873 /* Point to the UNIX filename part (which needs to be fixed!)
10874 but skip vms path information.
10875 [basename != fullname since first_slash != 0]. */
10877 if ((basename
[-1] == ':') /* vms path spec. */
10878 || (basename
[-1] == ']')
10879 || (basename
[-1] == '>'))
10880 unixname
= basename
;
10882 unixname
= fullname
;
10884 if (*unixname
== '/')
10887 /* If the directory spec is not rooted, we can just copy
10888 the UNIX filename part and we are done. */
10890 if (((basename
- fullname
) > 1)
10891 && ( (basename
[-1] == ']')
10892 || (basename
[-1] == '>')))
10894 if (basename
[-2] != '.')
10897 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10898 -> PATH]:/name (basename = '/name', unixname = 'name')
10899 We strip the `]', and then splice the two parts of the name in the
10900 usual way. Given the default locations for include files in cccp.c,
10901 we will only use this code if the user specifies alternate locations
10902 with the /include (-I) switch on the command line. */
10904 basename
-= 1; /* Strip "]" */
10905 unixname
--; /* backspace */
10910 /* The VMS part has a ".]" at the end, and this will not do. Later
10911 processing will add a second directory spec, and this would be a syntax
10912 error. Thus we strip the ".]", and thus merge the directory specs.
10913 We also backspace unixname, so that it points to a '/'. This inhibits the
10914 generation of the 000000 root directory spec (which does not belong here
10917 basename
-= 2; /* Strip ".]" */
10918 unixname
--; /* backspace */
10926 /* We drop in here if there is no VMS style directory specification yet.
10927 If there is no device specification either, we make the first dir a
10928 device and try that. If we do not do this, then we will be essentially
10929 searching the users default directory (as if they did a #include "asdf.h").
10931 Then all we need to do is to push a '[' into the output string. Later
10932 processing will fill this in, and close the bracket. */
10934 if ((unixname
!= fullname
) /* vms path spec found. */
10935 && (basename
[-1] != ':'))
10936 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
10938 *local_ptr
++ = '['; /* Open the directory specification */
10941 if (unixname
== fullname
) /* no vms dir spec. */
10944 if ((first_slash
!= 0) /* unix dir spec. */
10945 && (*unixname
!= '/') /* not beginning with '/' */
10946 && (*unixname
!= '.')) /* or './' or '../' */
10947 *local_ptr
++ = '.'; /* dir is local ! */
10950 /* at this point we assume that we have the device spec, and (at least
10951 the opening "[" for a directory specification. We may have directories
10954 If there are no other slashes then the filename will be
10955 in the "root" directory. Otherwise, we need to add
10956 directory specifications. */
10958 if (index (unixname
, '/') == 0)
10960 /* if no directories specified yet and none are following. */
10961 if (local_ptr
[-1] == '[')
10963 /* Just add "000000]" as the directory string */
10964 strcpy (local_ptr
, "000000]");
10965 local_ptr
+= strlen (local_ptr
);
10966 check_filename_before_returning
= 1; /* we might need to fool with this later */
10972 /* As long as there are still subdirectories to add, do them. */
10973 while (index (unixname
, '/') != 0)
10975 /* If this token is "." we can ignore it
10976 if it's not at the beginning of a path. */
10977 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
10979 /* remove it at beginning of path. */
10980 if ( ((unixname
== fullname
) /* no device spec */
10981 && (fullname
+2 != basename
)) /* starts with ./ */
10983 || ((basename
[-1] == ':') /* device spec */
10984 && (unixname
-1 == basename
))) /* and ./ afterwards */
10985 *local_ptr
++ = '.'; /* make '[.' start of path. */
10990 /* Add a subdirectory spec. Do not duplicate "." */
10991 if ( local_ptr
[-1] != '.'
10992 && local_ptr
[-1] != '['
10993 && local_ptr
[-1] != '<')
10994 *local_ptr
++ = '.';
10996 /* If this is ".." then the spec becomes "-" */
10997 if ( (unixname
[0] == '.')
10998 && (unixname
[1] == '.')
10999 && (unixname
[2] == '/'))
11001 /* Add "-" and skip the ".." */
11002 if ((local_ptr
[-1] == '.')
11003 && (local_ptr
[-2] == '['))
11004 local_ptr
--; /* prevent [.- */
11005 *local_ptr
++ = '-';
11010 /* Copy the subdirectory */
11011 while (*unixname
!= '/')
11012 *local_ptr
++= *unixname
++;
11014 unixname
++; /* Skip the "/" */
11017 /* Close the directory specification */
11018 if (local_ptr
[-1] == '.') /* no trailing periods */
11021 if (local_ptr
[-1] == '[') /* no dir needed */
11024 *local_ptr
++ = ']';
11027 /* Now add the filename. */
11030 *local_ptr
++ = *unixname
++;
11033 /* Now append it to the original VMS spec. */
11035 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
11037 /* If we put a [000000] in the filename, try to open it first. If this fails,
11038 remove the [000000], and return that name. This provides flexibility
11039 to the user in that they can use both rooted and non-rooted logical names
11040 to point to the location of the file. */
11042 if (check_filename_before_returning
)
11044 f
= open (fullname
, O_RDONLY
, 0666);
11047 /* The file name is OK as it is, so return it as is. */
11052 /* The filename did not work. Try to remove the [000000] from the name,
11055 basename
= index (fullname
, '[');
11056 local_ptr
= index (fullname
, ']') + 1;
11057 strcpy (basename
, local_ptr
); /* this gets rid of it */
11067 /* The following wrapper functions supply additional arguments to the VMS
11068 I/O routines to optimize performance with file handling. The arguments
11070 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11071 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11072 "fop=tef"- Truncate unused portions of file when closing file.
11073 "shr=nil"- Disallow file sharing while file is open. */
11076 VMS_freopen (fname
, type
, oldfile
)
11081 #undef freopen /* Get back the real freopen routine. */
11082 if (strcmp (type
, "w") == 0)
11083 return freopen (fname
, type
, oldfile
,
11084 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11085 return freopen (fname
, type
, oldfile
, "mbc=16");
11089 VMS_fopen (fname
, type
)
11093 #undef fopen /* Get back the real fopen routine. */
11094 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11095 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11096 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11097 FILE *(*vmslib_fopen
)() = (FILE *(*)()) fopen
;
11100 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
11101 "deq=64", "fop=tef", "shr=nil");
11103 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
11107 VMS_open (fname
, flags
, prot
)
11112 #undef open /* Get back the real open routine. */
11113 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
11116 /* more VMS hackery */
11120 extern unsigned long SYS$
PARSE(), SYS$
SEARCH();
11122 /* Work around another library bug. If a file is located via a searchlist,
11123 and if the device it's on is not the same device as the one specified
11124 in the first element of that searchlist, then both stat() and fstat()
11125 will fail to return info about it. `errno' will be set to EVMSERR, and
11126 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11127 We can get around this by fully parsing the filename and then passing
11128 that absolute name to stat().
11130 Without this fix, we can end up failing to find header files, which is
11131 bad enough, but then compounding the problem by reporting the reason for
11132 failure as "normal successful completion." */
11134 #undef fstat /* Get back to the library version. */
11137 VMS_fstat (fd
, statbuf
)
11139 struct stat
*statbuf
;
11141 int result
= fstat (fd
, statbuf
);
11146 char nambuf
[NAM$C_MAXRSS
+1];
11148 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
11149 result
= VMS_stat (nambuf
, statbuf
);
11150 /* No fclose(fp) here; that would close(fd) as well. */
11157 VMS_stat (name
, statbuf
)
11159 struct stat
*statbuf
;
11161 int result
= stat (name
, statbuf
);
11167 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for SYS$PARSE */
11168 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for SYS$SEARCH */
11171 fab
.fab$l_fna
= (char *) name
;
11172 fab
.fab$b_fns
= (unsigned char) strlen (name
);
11173 fab
.fab$l_nam
= (void *) &nam
;
11175 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
11176 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
11177 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
11178 if (SYS$
PARSE (&fab
) & 1)
11180 if (SYS$
SEARCH (&fab
) & 1)
11182 res_nam
[nam
.nam$b_rsl
] = '\0';
11183 result
= stat (res_nam
, statbuf
);
11185 /* Clean up searchlist context cached by the system. */
11186 nam
.nam$b_nop
= NAM$M_SYNCHK
;
11187 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
11188 (void) SYS$
PARSE (&fab
);