1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000 Free Software Foundation, Inc.
4 Written by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #ifdef HAVE_SYS_RESOURCE_H
28 # include <sys/resource.h>
31 typedef unsigned char U_CHAR
;
38 #ifdef MULTIBYTE_CHARS
41 #endif /* MULTIBYTE_CHARS */
43 #ifndef GET_ENV_PATH_LIST
44 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
47 #ifndef STANDARD_INCLUDE_DIR
48 # define STANDARD_INCLUDE_DIR "/usr/include"
51 /* By default, the suffix for object files is ".o". */
53 # define HAVE_OBJECT_SUFFIX
55 # define OBJECT_SUFFIX ".o"
58 /* VMS-specific definitions */
63 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
64 #define fopen(fname,mode) VMS_fopen (fname,mode)
65 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
66 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
67 #define fwrite(ptr,size,nitems,stream) VMS_fwrite (ptr,size,nitems,stream)
68 static int VMS_fstat (), VMS_stat ();
69 static int VMS_open ();
70 static FILE *VMS_fopen ();
71 static FILE *VMS_freopen ();
72 static size_t VMS_fwrite ();
73 static void hack_vms_include_specification ();
74 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
75 #define INO_T_HASH(a) 0
76 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
79 /* Windows does not natively support inodes, and neither does MSDOS.
80 Cygwin's emulation can generate non-unique inodes, so don't use it. */
81 #if (defined (_WIN32) && ! defined (_UWIN)) \
82 || defined (__MSDOS__)
83 #define INO_T_EQ(a, b) 0
87 #define INO_T_EQ(a, b) ((a) == (b))
91 #define INO_T_HASH(a) (a)
94 #ifndef INCLUDE_LEN_FUDGE
95 #define INCLUDE_LEN_FUDGE 0
98 /* External declarations. */
100 HOST_WIDEST_INT parse_escape
PARAMS ((char **, HOST_WIDEST_INT
));
101 HOST_WIDEST_INT parse_c_expression
PARAMS ((char *, int));
103 /* Name under which this program was invoked. */
105 static const char *progname
;
107 /* Nonzero means use extra default include directories for C++. */
109 static int cplusplus
;
111 /* Nonzero means handle cplusplus style comments */
113 static int cplusplus_comments
;
115 /* Nonzero means handle #import, for objective C. */
119 /* Nonzero means this is an assembly file, and allow
120 unknown directives, which could be comments. */
124 /* Current maximum length of directory names in the search path
125 for include files. (Altered as we get more of them.) */
127 static int max_include_len
;
129 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
131 static int for_lint
= 0;
133 /* Nonzero means copy comments into the output file. */
135 static int put_out_comments
= 0;
137 /* Nonzero means don't process the ANSI trigraph sequences. */
139 static int no_trigraphs
= 0;
141 /* Nonzero means print the names of included files rather than
142 the preprocessed output. 1 means just the #include "...",
143 2 means #include <...> as well. */
145 static int print_deps
= 0;
147 /* Nonzero if missing .h files in -M output are assumed to be generated
148 files and not errors. */
150 static int print_deps_missing_files
= 0;
152 /* Nonzero means print names of header files (-H). */
154 static int print_include_names
= 0;
156 /* Nonzero means don't output line number information. */
158 static int no_line_directives
;
160 /* Nonzero means output the text in failing conditionals,
161 inside #failed ... #endfailed. */
163 static int output_conditionals
;
165 /* dump_only means inhibit output of the preprocessed text
166 and instead output the definitions of all user-defined
167 macros in a form suitable for use as input to cccp.
168 dump_names means pass #define and the macro name through to output.
169 dump_definitions means pass the whole definition (plus #define) through
172 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
173 dump_macros
= dump_none
;
175 /* Nonzero means pass all #define and #undef directives which we actually
176 process through to the output stream. This feature is used primarily
177 to allow cc1 to record the #defines and #undefs for the sake of
178 debuggers which understand about preprocessor macros, but it may
179 also be useful with -E to figure out how symbols are defined, and
180 where they are defined. */
181 static int debug_output
= 0;
183 /* Nonzero means pass #include lines through to the output,
184 even if they are ifdefed out. */
185 static int dump_includes
;
187 /* Nonzero indicates special processing used by the pcp program. The
188 special effects of this mode are:
190 Inhibit all macro expansion, except those inside #if directives.
192 Process #define directives normally, and output their contents
195 Output preconditions to pcp_outfile indicating all the relevant
196 preconditions for use of this file in a later cpp run.
198 static FILE *pcp_outfile
;
200 /* Nonzero means we are inside an IF during a -pcp run. In this mode
201 macro expansion is done, and preconditions are output for all macro
202 uses requiring them. */
203 static int pcp_inside_if
;
205 /* Nonzero means never to include precompiled files.
206 This is 1 since there's no way now to make precompiled files,
207 so it's not worth testing for them. */
208 static int no_precomp
= 1;
210 /* Nonzero means give all the error messages the ANSI standard requires. */
214 /* Nonzero means try to make failure to fit ANSI C an error. */
216 static int pedantic_errors
;
218 /* Nonzero means don't print warning messages. -w. */
220 static int inhibit_warnings
= 0;
222 /* Nonzero means warn if slash-star appears in a slash-star comment,
223 or if newline-backslash appears in a slash-slash comment. */
225 static int warn_comments
;
227 /* Nonzero means warn if a macro argument is (or would be)
228 stringified with -traditional. */
230 static int warn_stringify
;
232 /* Nonzero means warn if there are any trigraphs. */
234 static int warn_trigraphs
;
236 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
238 static int warn_undef
;
240 /* Nonzero means warn if we find white space where it doesn't belong. */
242 static int warn_white_space
;
244 /* Nonzero means warn if #import is used. */
246 static int warn_import
= 1;
248 /* Nonzero means turn warnings into errors. */
250 static int warnings_are_errors
;
252 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
256 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
260 /* Nonzero for the 1999 C Standard. */
264 /* Nonzero causes output not to be done,
265 but directives such as #define that have side effects
268 static int no_output
;
270 /* Nonzero means we should look for header.gcc files that remap file names. */
273 /* Nonzero means this file was included with a -imacros or -include
274 command line and should not be recorded as an include file. */
276 static int no_record_file
;
278 /* Nonzero means that we have finished processing the command line options.
279 This flag is used to decide whether or not to issue certain errors
282 static int done_initializing
= 0;
284 /* Line where a newline was first seen in a string constant. */
286 static int multiline_string_line
= 0;
288 /* I/O buffer structure.
289 The `fname' field is nonzero for source files and #include files
290 and for the dummy text used for -D and -U.
291 It is zero for rescanning results of macro expansion
292 and for expanding macro arguments. */
293 #define INPUT_STACK_MAX 400
294 static struct file_buf
{
296 /* Filename specified with #line directive. */
297 const char *nominal_fname
;
298 /* The length of nominal_fname, which may contain embedded NULs. */
299 size_t nominal_fname_len
;
300 /* Include file description. */
301 struct include_file
*inc
;
302 /* Record where in the search path this file was found.
303 For #include_next. */
304 struct file_name_list
*dir
;
309 /* Macro that this level is the expansion of.
310 Included so that we can reenable the macro
311 at the end of this level. */
312 struct hashnode
*macro
;
313 /* Value of if_stack at start of this file.
314 Used to prohibit unmatched #endif (etc) in an include file. */
315 struct if_stack
*if_stack
;
316 /* Object to be freed at end of input at this level. */
318 /* True if this is a system header file; see is_system_include. */
319 char system_header_p
;
320 } instack
[INPUT_STACK_MAX
];
322 static int last_error_tick
; /* Incremented each time we print it. */
323 static int input_file_stack_tick
; /* Incremented when the status changes. */
325 /* Current nesting level of input sources.
326 `instack[indepth]' is the level currently being read. */
327 static int indepth
= -1;
328 #define CHECK_DEPTH(code) \
329 if (indepth >= (INPUT_STACK_MAX - 1)) \
331 error_with_line (line_for_error (instack[indepth].lineno), \
332 "macro or `#include' recursion too deep"); \
336 /* Current depth in #include directives that use <...>. */
337 static int system_include_depth
= 0;
339 typedef struct file_buf FILE_BUF
;
341 /* The output buffer. Its LENGTH field is the amount of room allocated
342 for the buffer, not the number of chars actually present. To get
343 that, subtract outbuf.buf from outbuf.bufp. */
345 #define OUTBUF_SIZE 10 /* initial size of output buffer */
346 static FILE_BUF outbuf
;
348 /* Grow output buffer OBUF points at
349 so it can hold at least NEEDED more chars. */
351 #define check_expand(OBUF, NEEDED) \
352 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
353 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
355 struct file_name_list
357 struct file_name_list
*next
;
358 /* If the following is 1, it is a C-language system include
360 int c_system_include_path
;
361 /* Mapping of file names for this directory. */
362 struct file_name_map
*name_map
;
363 /* Non-zero if name_map is valid. */
365 /* The include directory status. */
367 /* The include prefix: "" denotes the working directory,
368 otherwise fname must end in '/'.
369 The actual size is dynamically allocated. */
373 /* #include "file" looks in source file dir, then stack. */
374 /* #include <file> just looks in the stack. */
375 /* -I directories are added to the end, then the defaults are added. */
377 static struct default_include
{
378 const char *fname
; /* The name of the directory. */
379 const char *component
; /* The component containing the directory */
380 int cplusplus
; /* Only look here if we're compiling C++. */
381 int cxx_aware
; /* Includes in this directory don't need to
382 be wrapped in extern "C" when compiling
384 int included
; /* Set if the directory is acceptable. */
385 } include_defaults_array
[]
386 #ifdef INCLUDE_DEFAULTS
390 /* Pick up GNU C++ specific include files. */
391 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1, 0 },
393 /* This is the dir for fixincludes. Put it just before
394 the files that we fix. */
395 { GCC_INCLUDE_DIR
, "GCC", 0, 0, 0 },
396 /* For cross-compilation, this dir name is generated
397 automatically in Makefile.in. */
398 { CROSS_INCLUDE_DIR
, "GCC", 0, 0, 0 },
399 #ifdef TOOL_INCLUDE_DIR
400 /* This is another place that the target system's headers might be. */
401 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0, 0 },
403 #else /* not CROSS_COMPILE */
404 #ifdef LOCAL_INCLUDE_DIR
405 /* This should be /usr/local/include and should come before
406 the fixincludes-fixed header files. */
407 { LOCAL_INCLUDE_DIR
, 0, 0, 1, 0 },
409 #ifdef TOOL_INCLUDE_DIR
410 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
411 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
412 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0, 0 },
414 /* This is the dir for fixincludes. Put it just before
415 the files that we fix. */
416 { GCC_INCLUDE_DIR
, "GCC", 0, 0, 0 },
417 /* Some systems have an extra dir of include files. */
418 #ifdef SYSTEM_INCLUDE_DIR
419 { SYSTEM_INCLUDE_DIR
, 0, 0, 0, 0 },
421 #ifndef STANDARD_INCLUDE_COMPONENT
422 #define STANDARD_INCLUDE_COMPONENT 0
424 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0, 0 },
425 #endif /* not CROSS_COMPILE */
428 #endif /* no INCLUDE_DEFAULTS */
430 /* The code looks at the defaults through this pointer, rather than through
431 the constant structure above. This pointer gets changed if an environment
432 variable specifies other defaults. */
433 static struct default_include
*include_defaults
= include_defaults_array
;
435 static struct file_name_list
*include
= 0; /* First dir to search */
436 /* First dir to search for <file> */
437 /* This is the first element to use for #include <...>.
438 If it is 0, use the entire chain for such includes. */
439 static struct file_name_list
*first_bracket_include
= 0;
440 /* This is the first element in the chain that corresponds to
441 a directory of system header files. */
442 static struct file_name_list
*first_system_include
= 0;
443 static struct file_name_list
*last_include
= 0; /* Last in chain */
445 /* Chain of include directories to put at the end of the other chain. */
446 static struct file_name_list
*after_include
= 0;
447 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
449 /* Chain to put at the start of the system include files. */
450 static struct file_name_list
*before_system
= 0;
451 static struct file_name_list
*last_before_system
= 0; /* Last in chain */
453 /* Directory prefix that should replace `/usr' in the standard
454 include file directories. */
455 static char *include_prefix
;
457 /* Maintain and search list of included files. */
459 struct include_file
{
460 struct include_file
*next
; /* for include_hashtab */
461 struct include_file
*next_ino
; /* for include_ino_hashtab */
463 /* If the following is the empty string, it means #pragma once
464 was seen in this include file, or #import was applied to the file.
465 Otherwise, if it is nonzero, it is a macro name.
466 Don't include the file again if that macro is defined. */
467 const U_CHAR
*control_macro
;
468 /* Nonzero if the dependency on this include file has been output. */
473 /* Hash tables of files already included with #include or #import.
474 include_hashtab is by full name; include_ino_hashtab is by inode number. */
476 #define INCLUDE_HASHSIZE 61
477 static struct include_file
*include_hashtab
[INCLUDE_HASHSIZE
];
478 static struct include_file
*include_ino_hashtab
[INCLUDE_HASHSIZE
];
480 /* Global list of strings read in from precompiled files. This list
481 is kept in the order the strings are read in, with new strings being
482 added at the end through stringlist_tailp. We use this list to output
483 the strings at the end of the run.
485 static STRINGDEF
*stringlist
;
486 static STRINGDEF
**stringlist_tailp
= &stringlist
;
489 /* Structure returned by create_definition */
490 typedef struct macrodef MACRODEF
;
493 struct definition
*defn
;
494 const U_CHAR
*symnam
;
498 enum sharp_token_type
{
499 NO_SHARP_TOKEN
= 0, /* token not present */
501 SHARP_TOKEN
= '#', /* token spelled with # only */
502 WHITE_SHARP_TOKEN
, /* token spelled with # and white space */
504 PERCENT_COLON_TOKEN
= '%', /* token spelled with %: only */
505 WHITE_PERCENT_COLON_TOKEN
/* token spelled with %: and white space */
508 /* Structure allocated for every #define. For a simple replacement
511 nargs = -1, the `pattern' list is null, and the expansion is just
512 the replacement text. Nargs = 0 means a functionlike macro with no args,
514 #define getchar() getc (stdin) .
515 When there are args, the expansion is the replacement text with the
516 args squashed out, and the reflist is a list describing how to
517 build the output from the input: e.g., "3 chars, then the 1st arg,
518 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
519 The chars here come from the expansion. Whatever is left of the
520 expansion after the last arg-occurrence is copied after that arg.
521 Note that the reflist can be arbitrarily long---
522 its length depends on the number of times the arguments appear in
523 the replacement text, not how many args there are. Example:
524 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
526 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
527 where (x, y) means (nchars, argno). */
529 typedef struct definition DEFINITION
;
532 int length
; /* length of expansion string */
533 int predefined
; /* True if the macro was builtin or */
534 /* came from the command line */
536 int line
; /* Line number of definition */
537 const char *file
; /* File of definition */
538 size_t file_len
; /* Length of file (which can contain NULs) */
539 char rest_args
; /* Nonzero if last arg. absorbs the rest */
541 struct reflist
*next
;
543 enum sharp_token_type stringify
; /* set if a # operator before arg */
544 enum sharp_token_type raw_before
; /* set if a ## operator before arg */
545 enum sharp_token_type raw_after
; /* set if a ## operator after arg */
547 char rest_args
; /* Nonzero if this arg. absorbs the rest */
548 int nchars
; /* Number of literal chars to copy before
549 this arg occurrence. */
550 int argno
; /* Number of arg to substitute (origin-0) */
553 /* Names of macro args, concatenated in reverse order
554 with comma-space between them.
555 The only use of this is that we warn on redefinition
556 if this differs between the old and new definitions. */
561 /* different kinds of things that can appear in the value field
562 of a hash node. Actually, this may be useless now. */
570 * special extension string that can be added to the last macro argument to
571 * allow it to absorb the "rest" of the arguments when expanded. Ex:
572 * #define wow(a, b...) process (b, a, b)
573 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
574 * { wow (one, two); } -> { process (two, one, two); }
575 * if this "rest_arg" is used with the concat token '##' and if it is not
576 * supplied then the token attached to with ## will not be outputted. Ex:
577 * #define wow (a, b...) process (b ## , a, ## b)
578 * { wow (1, 2); } -> { process (2, 1, 2); }
579 * { wow (one); } -> { process (one); {
581 static char rest_extension
[] = "...";
582 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
584 /* This is the implicit parameter name when using variable number of
585 parameters for macros using the ISO C 99 extension. */
586 static char va_args_name
[] = "__VA_ARGS__";
587 #define VA_ARGS_NAME_LENGTH (sizeof (va_args_name) - 1)
589 /* The structure of a node in the hash table. The hash table
590 has entries for all tokens defined by #define directives (type T_MACRO),
591 plus some special tokens like __LINE__ (these each have their own
592 type, and the appropriate code is run when that type of node is seen.
593 It does not contain control words like "#define", which are recognized
594 by a separate piece of code. */
596 /* different flavors of hash nodes --- also used in keyword table */
598 T_DEFINE
= 1, /* the `#define' keyword */
599 T_INCLUDE
, /* the `#include' keyword */
600 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
601 T_IMPORT
, /* the `#import' keyword */
602 T_IFDEF
, /* the `#ifdef' keyword */
603 T_IFNDEF
, /* the `#ifndef' keyword */
604 T_IF
, /* the `#if' keyword */
605 T_ELSE
, /* `#else' */
606 T_PRAGMA
, /* `#pragma' */
607 T_ELIF
, /* `#elif' */
608 T_UNDEF
, /* `#undef' */
609 T_LINE
, /* `#line' */
610 T_ERROR
, /* `#error' */
611 T_WARNING
, /* `#warning' */
612 T_ENDIF
, /* `#endif' */
613 T_SCCS
, /* `#sccs', used on system V. */
614 T_IDENT
, /* `#ident', used on system V. */
615 T_ASSERT
, /* `#assert', taken from system V. */
616 T_UNASSERT
, /* `#unassert', taken from system V. */
617 T_SPECLINE
, /* special symbol `__LINE__' */
618 T_DATE
, /* `__DATE__' */
619 T_FILE
, /* `__FILE__' */
620 T_BASE_FILE
, /* `__BASE_FILE__' */
621 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
622 T_VERSION
, /* `__VERSION__' */
623 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
624 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
625 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
626 T_USER_LABEL_PREFIX_TYPE
, /* `__USER_LABEL_PREFIX__' */
627 T_REGISTER_PREFIX_TYPE
, /* `__REGISTER_PREFIX__' */
628 T_IMMEDIATE_PREFIX_TYPE
, /* `__IMMEDIATE_PREFIX__' */
629 T_TIME
, /* `__TIME__' */
630 T_CONST
, /* Constant value, used by `__STDC__' */
631 T_MACRO
, /* macro defined by `#define' */
632 T_DISABLED
, /* macro temporarily turned off for rescan */
633 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
634 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
635 T_POISON
, /* defined with `#pragma poison' */
636 T_UNUSED
/* Used for something not defined. */
640 struct hashnode
*next
; /* double links for easy deletion */
641 struct hashnode
*prev
;
642 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
643 chain is kept, in case the node is the head
644 of the chain and gets deleted. */
645 enum node_type type
; /* type of special token */
646 int length
; /* length of token, for quick comparison */
647 U_CHAR
*name
; /* the actual name */
648 union hashval value
; /* pointer to expansion, or whatever */
651 typedef struct hashnode HASHNODE
;
653 /* Some definitions for the hash table. The hash function MUST be
654 computed as shown in hashf () below. That is because the rescan
655 loop computes the hash value `on the fly' for most tokens,
656 in order to avoid the overhead of a lot of procedure calls to
657 the hashf () function. Hashf () only exists for the sake of
658 politeness, for use when speed isn't so important. */
660 #define HASHSIZE 1403
661 static HASHNODE
*hashtab
[HASHSIZE
];
662 #define HASHSTEP(old, c) ((old << 2) + c)
663 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
666 /* We let tm.h override the types used here, to handle trivial differences
667 such as the choice of unsigned int or long unsigned int for size_t.
668 When machines start needing nontrivial differences in the size type,
669 it would be best to do something here to figure out automatically
670 from other information what type to use. */
672 /* The string value for __SIZE_TYPE__. */
675 #define SIZE_TYPE "long unsigned int"
678 /* The string value for __PTRDIFF_TYPE__. */
681 #define PTRDIFF_TYPE "long int"
684 /* The string value for __WCHAR_TYPE__. */
687 #define WCHAR_TYPE "int"
689 static const char * wchar_type
= WCHAR_TYPE
;
692 /* The string value for __USER_LABEL_PREFIX__ */
694 #ifndef USER_LABEL_PREFIX
695 #define USER_LABEL_PREFIX ""
697 static const char * user_label_prefix
= USER_LABEL_PREFIX
;
698 #undef USER_LABEL_PREFIX
700 /* The string value for __REGISTER_PREFIX__ */
702 #ifndef REGISTER_PREFIX
703 #define REGISTER_PREFIX ""
706 /* The string value for __IMMEDIATE_PREFIX__ */
708 #ifndef IMMEDIATE_PREFIX
709 #define IMMEDIATE_PREFIX ""
712 /* In the definition of a #assert name, this structure forms
713 a list of the individual values asserted.
714 Each value is itself a list of "tokens".
715 These are strings that are compared by name. */
717 struct tokenlist_list
{
718 struct tokenlist_list
*next
;
719 struct arglist
*tokens
;
722 struct assertion_hashnode
{
723 struct assertion_hashnode
*next
; /* double links for easy deletion */
724 struct assertion_hashnode
*prev
;
725 /* also, a back pointer to this node's hash
726 chain is kept, in case the node is the head
727 of the chain and gets deleted. */
728 struct assertion_hashnode
**bucket_hdr
;
729 int length
; /* length of token, for quick comparison */
730 U_CHAR
*name
; /* the actual name */
731 /* List of token-sequences. */
732 struct tokenlist_list
*value
;
735 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
737 /* Some definitions for the hash table. The hash function MUST be
738 computed as shown in hashf below. That is because the rescan
739 loop computes the hash value `on the fly' for most tokens,
740 in order to avoid the overhead of a lot of procedure calls to
741 the hashf function. hashf only exists for the sake of
742 politeness, for use when speed isn't so important. */
744 #define ASSERTION_HASHSIZE 37
745 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
747 /* Nonzero means inhibit macroexpansion of what seem to be
748 assertion tests, in rescan. For #if. */
749 static int assertions_flag
;
751 /* `struct directive' defines one #-directive, including how to handle it. */
753 #define DO_PROTO PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
756 int length
; /* Length of name */
757 int (*func
) DO_PROTO
; /* Function to handle directive */
758 const char *name
; /* Name of directive */
759 enum node_type type
; /* Code which describes which directive. */
762 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
763 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
765 /* These functions are declared to return int instead of void since they
766 are going to be placed in the table and some old compilers have trouble with
767 pointers to functions returning void. */
769 static int do_assert DO_PROTO
;
770 static int do_define DO_PROTO
;
771 static int do_elif DO_PROTO
;
772 static int do_else DO_PROTO
;
773 static int do_endif DO_PROTO
;
774 static int do_error DO_PROTO
;
775 static int do_ident DO_PROTO
;
776 static int do_if DO_PROTO
;
777 static int do_include DO_PROTO
;
778 static int do_line DO_PROTO
;
779 static int do_pragma DO_PROTO
;
780 #ifdef SCCS_DIRECTIVE
781 static int do_sccs DO_PROTO
;
783 static int do_unassert DO_PROTO
;
784 static int do_undef DO_PROTO
;
785 static int do_xifdef DO_PROTO
;
787 /* Here is the actual list of #-directives, most-often-used first. */
789 static struct directive directive_table
[] = {
790 { 6, do_define
, "define", T_DEFINE
},
791 { 2, do_if
, "if", T_IF
},
792 { 5, do_xifdef
, "ifdef", T_IFDEF
},
793 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
794 { 5, do_endif
, "endif", T_ENDIF
},
795 { 4, do_else
, "else", T_ELSE
},
796 { 4, do_elif
, "elif", T_ELIF
},
797 { 4, do_line
, "line", T_LINE
},
798 { 7, do_include
, "include", T_INCLUDE
},
799 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
800 { 6, do_include
, "import", T_IMPORT
},
801 { 5, do_undef
, "undef", T_UNDEF
},
802 { 5, do_error
, "error", T_ERROR
},
803 { 7, do_error
, "warning", T_WARNING
},
804 #ifdef SCCS_DIRECTIVE
805 { 4, do_sccs
, "sccs", T_SCCS
},
807 { 6, do_pragma
, "pragma", T_PRAGMA
},
808 { 5, do_ident
, "ident", T_IDENT
},
809 { 6, do_assert
, "assert", T_ASSERT
},
810 { 8, do_unassert
, "unassert", T_UNASSERT
},
811 { -1, 0, "", T_UNUSED
},
814 /* When a directive handler is called,
815 this points to the # (or the : of the %:) that started the directive. */
816 U_CHAR
*directive_start
;
818 /* table to tell if char can be part of a C identifier. */
819 U_CHAR is_idchar
[256];
820 /* table to tell if char can be first char of a c identifier. */
821 U_CHAR is_idstart
[256];
822 /* table to tell if c is horizontal space. */
823 static U_CHAR is_hor_space
[256];
824 /* table to tell if c is horizontal or vertical space. */
825 U_CHAR is_space
[256];
827 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
828 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
830 static int errors
= 0; /* Error counter for exit code */
832 /* Name of output file, for error messages. */
833 static const char *out_fname
;
835 /* Nonzero to ignore \ in string constants. Use to treat #line 1 "A:\file.h
836 as a non-form feed. If you want it to be a form feed, you must use
838 static int ignore_escape_flag
= 1;
840 /* Stack of conditionals currently in progress
841 (including both successful and failing conditionals). */
844 struct if_stack
*next
; /* for chaining to the next stack frame */
845 const char *fname
; /* copied from input when frame is made */
846 size_t fname_len
; /* similarly */
847 int lineno
; /* similarly */
848 int if_succeeded
; /* true if a leg of this if-group
849 has been passed through rescan */
850 const U_CHAR
*control_macro
; /* For #ifndef at start of file,
851 this is the macro name tested. */
852 enum node_type type
; /* type of last directive seen in this group */
854 typedef struct if_stack IF_STACK_FRAME
;
855 static IF_STACK_FRAME
*if_stack
= NULL
;
857 /* Buffer of -M output. */
858 static char *deps_buffer
;
860 /* Number of bytes allocated in above. */
861 static int deps_allocated_size
;
863 /* Number of bytes used. */
864 static int deps_size
;
866 /* Number of bytes since the last newline. */
867 static int deps_column
;
869 /* Nonzero means -I- has been seen,
870 so don't look for #include "foo" the source-file directory. */
871 static int ignore_srcdir
;
873 static int safe_read
PARAMS ((int, char *, int));
874 static void safe_write
PARAMS ((int, const char *, int));
876 int main
PARAMS ((int, char **));
878 static void path_include
PARAMS ((char *));
880 static const U_CHAR
*index0
PARAMS ((const U_CHAR
*, int, size_t));
882 static void trigraph_pcp
PARAMS ((FILE_BUF
*));
883 static void check_white_space
PARAMS ((FILE_BUF
*));
885 static void newline_fix
PARAMS ((U_CHAR
*));
886 static void name_newline_fix
PARAMS ((U_CHAR
*));
888 static const char *get_lintcmd
PARAMS ((const U_CHAR
*, const U_CHAR
*,
889 const U_CHAR
**, int *, int *));
891 static void rescan
PARAMS ((FILE_BUF
*, int));
893 static FILE_BUF expand_to_temp_buffer
PARAMS ((const U_CHAR
*, const U_CHAR
*,
896 static int handle_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*));
898 static struct tm
*timestamp
PARAMS ((void));
899 static void special_symbol
PARAMS ((HASHNODE
*, FILE_BUF
*));
901 static int is_system_include
PARAMS ((const char *));
902 static char *base_name
PARAMS ((const char *));
903 static int absolute_filename
PARAMS ((const char *));
904 static size_t simplify_filename
PARAMS ((char *));
906 static char *read_filename_string
PARAMS ((int, FILE *));
907 static struct file_name_map
*read_name_map
PARAMS ((const char *));
908 static int open_include_file
PARAMS ((char *, struct file_name_list
*,
909 const U_CHAR
*, struct include_file
**));
910 static char *remap_include_file
PARAMS ((char *, struct file_name_list
*));
911 static int lookup_ino_include
PARAMS ((struct include_file
*));
913 static void finclude
PARAMS ((int, struct include_file
*, FILE_BUF
*, int,
914 struct file_name_list
*));
915 static void record_control_macro
PARAMS ((struct include_file
*,
918 static char *check_precompiled
PARAMS ((int, struct stat
*, const char *,
920 static int check_preconditions
PARAMS ((const char *));
921 static void pcfinclude
PARAMS ((U_CHAR
*, const U_CHAR
*, FILE_BUF
*));
922 static void pcstring_used
PARAMS ((HASHNODE
*));
923 static void write_output
PARAMS ((void));
924 static void pass_thru_directive
PARAMS ((const U_CHAR
*, const U_CHAR
*,
925 FILE_BUF
*, struct directive
*));
927 static MACRODEF create_definition
PARAMS ((const U_CHAR
*, const U_CHAR
*,
930 static int check_macro_name
PARAMS ((const U_CHAR
*, int));
931 static int compare_defs
PARAMS ((DEFINITION
*, DEFINITION
*));
932 static int comp_def_part
PARAMS ((int, const U_CHAR
*, int, const U_CHAR
*,
935 static DEFINITION
*collect_expansion
PARAMS ((const U_CHAR
*, const U_CHAR
*,
936 int, struct arglist
*));
938 int check_assertion
PARAMS ((const U_CHAR
*, int, int, struct arglist
*));
939 static int compare_token_lists
PARAMS ((struct arglist
*, struct arglist
*));
941 static struct arglist
*read_token_list
PARAMS ((const U_CHAR
**,
942 const U_CHAR
*, int *));
943 static void free_token_list
PARAMS ((struct arglist
*));
945 static ASSERTION_HASHNODE
*assertion_install
PARAMS ((const U_CHAR
*, int, int));
946 static ASSERTION_HASHNODE
*assertion_lookup
PARAMS ((const U_CHAR
*, int, int));
947 static void delete_assertion
PARAMS ((ASSERTION_HASHNODE
*));
949 static void do_once
PARAMS ((void));
951 static HOST_WIDEST_INT eval_if_expression
PARAMS ((const U_CHAR
*, int));
952 static void conditional_skip
PARAMS ((FILE_BUF
*, int, enum node_type
,
953 const U_CHAR
*, FILE_BUF
*));
954 static void skip_if_group
PARAMS ((FILE_BUF
*, int, FILE_BUF
*));
955 static void validate_else
PARAMS ((const U_CHAR
*, const U_CHAR
*));
957 static U_CHAR
*skip_to_end_of_comment
PARAMS ((FILE_BUF
*, int *, int));
958 static U_CHAR
*skip_quoted_string
PARAMS ((const U_CHAR
*, const U_CHAR
*,
959 int, int *, int *, int *));
960 static char *quote_string
PARAMS ((char *, const char *, size_t));
961 static U_CHAR
*skip_paren_group
PARAMS ((FILE_BUF
*));
963 /* Last arg to output_line_directive. */
964 enum file_change_code
{same_file
, enter_file
, leave_file
};
965 static void output_line_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*, int, enum file_change_code
));
967 static void macroexpand
PARAMS ((HASHNODE
*, FILE_BUF
*));
970 static int macarg
PARAMS ((struct argdata
*, int));
972 static U_CHAR
*macarg1
PARAMS ((U_CHAR
*, const U_CHAR
*, struct hashnode
*, int *, int *, int *, int));
974 static int discard_comments
PARAMS ((U_CHAR
*, int, int));
976 static void change_newlines
PARAMS ((struct argdata
*));
978 static void notice
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
979 static void vnotice
PARAMS ((const char *, va_list));
980 void error
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
981 void verror
PARAMS ((const char *, va_list));
982 static void error_from_errno
PARAMS ((const char *));
983 void warning
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
984 static void vwarning
PARAMS ((const char *, va_list));
985 static void error_with_line
PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
986 static void verror_with_line
PARAMS ((int, const char *, va_list));
987 static void vwarning_with_line
PARAMS ((int, const char *, va_list));
988 static void warning_with_line
PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
989 void pedwarn
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
990 void pedwarn_with_line
PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
991 static void pedwarn_with_file_and_line
PARAMS ((const char *, size_t, int, const char *, ...)) ATTRIBUTE_PRINTF_4
;
992 static void pedwarn_strange_white_space
PARAMS ((int));
994 static void print_containing_files
PARAMS ((void));
996 static int line_for_error
PARAMS ((int));
997 static int grow_outbuf
PARAMS ((FILE_BUF
*, int));
999 static HASHNODE
*install
PARAMS ((const U_CHAR
*, int, enum node_type
,
1000 const char *, int));
1001 HASHNODE
*lookup
PARAMS ((const U_CHAR
*, int, int));
1002 static void delete_macro
PARAMS ((HASHNODE
*));
1003 static int hashf
PARAMS ((const U_CHAR
*, int, int));
1005 static void dump_single_macro
PARAMS ((HASHNODE
*, FILE *));
1006 static void dump_all_macros
PARAMS ((void));
1007 static void dump_defn_1
PARAMS ((const U_CHAR
*, int, int, FILE *));
1008 static void dump_arg_n
PARAMS ((DEFINITION
*, int, FILE *));
1010 static void initialize_char_syntax
PARAMS ((void));
1011 static void initialize_builtins
PARAMS ((FILE_BUF
*, FILE_BUF
*));
1013 static void make_definition
PARAMS ((char *));
1014 static void make_undef
PARAMS ((char *, FILE_BUF
*));
1016 static void make_assertion
PARAMS ((const char *, const char *));
1018 static struct file_name_list
*new_include_prefix
PARAMS ((struct file_name_list
*, const char *, const char *, const char *));
1019 static void append_include_chain
PARAMS ((struct file_name_list
*, struct file_name_list
*));
1021 static int quote_string_for_make
PARAMS ((char *, const char *));
1022 static void deps_output
PARAMS ((const char *, int));
1024 void fatal
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN
;
1025 void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
1026 static void perror_with_name
PARAMS ((const char *));
1027 static void pfatal_with_name
PARAMS ((const char *)) ATTRIBUTE_NORETURN
;
1028 static void pipe_closed
PARAMS ((int)) ATTRIBUTE_NORETURN
;
1030 static void memory_full
PARAMS ((void)) ATTRIBUTE_NORETURN
;
1031 static void print_help
PARAMS ((void));
1033 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1034 retrying if necessary. If MAX_READ_LEN is defined, read at most
1035 that bytes at a time. Return a negative value if an error occurs,
1036 otherwise return the actual number of bytes read,
1037 which must be LEN unless end-of-file was reached. */
1040 safe_read (desc
, ptr
, len
)
1045 int left
, rcount
, nchars
;
1051 if (rcount
> MAX_READ_LEN
)
1052 rcount
= MAX_READ_LEN
;
1054 nchars
= read (desc
, ptr
, rcount
);
1071 /* Write LEN bytes at PTR to descriptor DESC,
1072 retrying if necessary, and treating any real error as fatal.
1073 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1076 safe_write (desc
, ptr
, len
)
1081 int wcount
, written
;
1085 #ifdef MAX_WRITE_LEN
1086 if (wcount
> MAX_WRITE_LEN
)
1087 wcount
= MAX_WRITE_LEN
;
1089 written
= write (desc
, ptr
, wcount
);
1096 pfatal_with_name (out_fname
);
1107 printf ("Usage: %s [switches] input output\n", progname
);
1108 printf ("Switches:\n");
1109 printf (" -include <file> Include the contents of <file> before other files\n");
1110 printf (" -imacros <file> Accept definition of macros in <file>\n");
1111 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1112 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1113 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1114 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1115 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1116 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1117 printf (" -nostdinc Do not search the system include directories\n");
1118 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1119 printf (" -o <file> Put output into <file>\n");
1120 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1121 printf (" -traditional Follow K&R pre-processor behaviour\n");
1122 printf (" -trigraphs Support ANSI C trigraphs\n");
1123 printf (" -lang-c Assume that the input sources are in C\n");
1124 printf (" -lang-c89 Assume that the input is C89; depricated\n");
1125 printf (" -lang-c++ Assume that the input sources are in C++\n");
1126 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1127 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1128 printf (" -lang-asm Assume that the input sources are in assembler\n");
1129 printf (" -lang-fortran Assume that the input sources are in Fortran\n");
1130 printf (" -lang-chill Assume that the input sources are in Chill\n");
1131 printf (" -std=<std name> Specify the conformance standard; one of:\n");
1132 printf (" gnu89, gnu99, c89, c99, iso9899:1990,\n");
1133 printf (" iso9899:199409, iso9899:1999\n");
1134 printf (" -+ Allow parsing of C++ style features\n");
1135 printf (" -w Inhibit warning messages\n");
1136 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1137 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1138 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1139 printf (" -Wno-comment{s} Do not warn about comments\n");
1140 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1141 printf (" a string if -traditional is specified\n");
1142 printf (" -Wno-traditional Do not warn about stringification\n");
1143 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1144 printf (" -Wno-undef Do not warn about testing undefined macros\n");
1145 printf (" -Wimport Warn about the use of the #import directive\n");
1146 printf (" -Wno-import Do not warn about the use of #import\n");
1147 printf (" -Werror Treat all warnings as errors\n");
1148 printf (" -Wno-error Do not treat warnings as errors\n");
1149 printf (" -Wall Enable all preprocessor warnings\n");
1150 printf (" -M Generate make dependencies\n");
1151 printf (" -MM As -M, but ignore system header files\n");
1152 printf (" -MD As -M, but put output in a .d file\n");
1153 printf (" -MMD As -MD, but ignore system header files\n");
1154 printf (" -MG Treat missing header file as generated files\n");
1155 printf (" -g Include #define and #undef directives in the output\n");
1156 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1157 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1158 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1159 printf (" -U<macro> Undefine <macro> \n");
1160 printf (" -u or -undef Do not predefine any macros\n");
1161 printf (" -v Display the version number\n");
1162 printf (" -H Print the name of header files as they are used\n");
1163 printf (" -C Do not discard comments\n");
1164 printf (" -dM Display a list of macro definitions active at end\n");
1165 printf (" -dD Preserve macro definitions in output\n");
1166 printf (" -dN As -dD except that only the names are preserved\n");
1167 printf (" -dI Include #include directives in the output\n");
1168 printf (" -ifoutput Describe skipped code blocks in output \n");
1169 printf (" -P Do not generate #line directives\n");
1170 printf (" -$ Do not include '$' in identifiers\n");
1171 printf (" -remap Remap file names when including files.\n");
1172 printf (" -h or --help Display this information\n");
1181 const char *in_fname
;
1189 char **pend_assertions
;
1190 char **pend_includes
;
1192 /* Record the option used with each element of pend_assertions.
1193 This is preparation for supporting more than one option for making
1195 const char **pend_assertion_options
;
1196 int no_standard_includes
= 0;
1197 int no_standard_cplusplus_includes
= 0;
1198 int missing_newline
= 0;
1200 /* Non-0 means don't output the preprocessed program. */
1201 int inhibit_output
= 0;
1202 /* Non-0 means -v, so print the full set of include dirs. */
1205 /* File name which deps are being written to.
1206 This is 0 if deps are being written to stdout. */
1207 char *deps_file
= 0;
1208 /* Fopen file mode to open deps_file with. */
1209 const char *deps_mode
= "a";
1210 /* Stream on which to print the dependency information. */
1211 FILE *deps_stream
= 0;
1212 /* Target-name to write with the dependency information. */
1213 char *deps_target
= 0;
1215 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1216 /* Get rid of any avoidable limit on stack size. */
1220 /* Set the stack limit huge so that alloca (particularly stringtab
1221 in dbxread.c) does not fail. */
1222 getrlimit (RLIMIT_STACK
, &rlim
);
1223 rlim
.rlim_cur
= rlim
.rlim_max
;
1224 setrlimit (RLIMIT_STACK
, &rlim
);
1229 signal (SIGPIPE
, pipe_closed
);
1232 #ifdef HAVE_LC_MESSAGES
1233 setlocale (LC_MESSAGES
, "");
1235 (void) bindtextdomain (PACKAGE
, localedir
);
1236 (void) textdomain (PACKAGE
);
1238 progname
= base_name (argv
[0]);
1242 /* Remove extension from PROGNAME. */
1244 char *s
= xstrdup (progname
);
1247 if ((p
= rindex (s
, ';')) != 0) *p
= '\0'; /* strip version number */
1248 if ((p
= rindex (s
, '.')) != 0 /* strip type iff ".exe" */
1249 && (p
[1] == 'e' || p
[1] == 'E')
1250 && (p
[2] == 'x' || p
[2] == 'X')
1251 && (p
[3] == 'e' || p
[3] == 'E')
1257 /* Do not invoke xmalloc before this point, since locale and
1258 progname need to be set first, in case a diagnostic is issued. */
1260 pend_files
= (char **) xmalloc (argc
* sizeof (char *));
1261 pend_defs
= (char **) xmalloc ((2 * argc
) * sizeof (char *));
1262 pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
1263 pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
1264 pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
1265 pend_assertion_options
= (const char **) xmalloc (argc
* sizeof (char *));
1270 /* Initialize is_idchar. */
1271 initialize_char_syntax ();
1273 no_line_directives
= 0;
1275 dump_macros
= dump_none
;
1278 cplusplus_comments
= 1;
1280 bzero ((char *) pend_files
, argc
* sizeof (char *));
1281 bzero ((char *) pend_defs
, (2 * argc
) * sizeof (char *));
1282 bzero ((char *) pend_undefs
, argc
* sizeof (char *));
1283 bzero ((char *) pend_assertions
, argc
* sizeof (char *));
1284 bzero ((char *) pend_includes
, argc
* sizeof (char *));
1286 #ifdef MULTIBYTE_CHARS
1287 /* Change to the native locale for multibyte conversions. */
1288 setlocale (LC_CTYPE
, "");
1289 literal_codeset
= getenv ("LANG");
1292 /* Process switches and find input file name. */
1294 for (i
= 1; i
< argc
; i
++) {
1295 if (argv
[i
][0] != '-') {
1296 if (out_fname
!= NULL
)
1299 fatal ("Too many arguments");
1301 else if (in_fname
!= NULL
)
1302 out_fname
= argv
[i
];
1306 switch (argv
[i
][1]) {
1309 if (!strcmp (argv
[i
], "-include")) {
1311 fatal ("Filename missing after `-include' option");
1314 simplify_filename (pend_includes
[i
] = argv
[i
]);
1317 if (!strcmp (argv
[i
], "-imacros")) {
1319 fatal ("Filename missing after `-imacros' option");
1322 simplify_filename (pend_files
[i
] = argv
[i
]);
1325 if (!strcmp (argv
[i
], "-iprefix")) {
1327 fatal ("Filename missing after `-iprefix' option");
1329 include_prefix
= argv
[++i
];
1331 if (!strcmp (argv
[i
], "-ifoutput")) {
1332 output_conditionals
= 1;
1334 if (!strcmp (argv
[i
], "-isystem")) {
1335 struct file_name_list
*dirtmp
;
1337 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1340 dirtmp
->c_system_include_path
= 1;
1342 if (before_system
== 0)
1343 before_system
= dirtmp
;
1345 last_before_system
->next
= dirtmp
;
1346 last_before_system
= dirtmp
; /* Tail follows the last one */
1348 /* Add directory to end of path for includes,
1349 with the default prefix at the front of its name. */
1350 if (!strcmp (argv
[i
], "-iwithprefix")) {
1351 struct file_name_list
*dirtmp
;
1354 if (include_prefix
!= 0)
1355 prefix
= include_prefix
;
1357 prefix
= xstrdup (GCC_INCLUDE_DIR
);
1358 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1359 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1360 prefix
[strlen (prefix
) - 7] = 0;
1363 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1364 prefix
, argv
[++i
])))
1367 if (after_include
== 0)
1368 after_include
= dirtmp
;
1370 last_after_include
->next
= dirtmp
;
1371 last_after_include
= dirtmp
; /* Tail follows the last one */
1373 /* Add directory to main path for includes,
1374 with the default prefix at the front of its name. */
1375 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
1376 struct file_name_list
*dirtmp
;
1379 if (include_prefix
!= 0)
1380 prefix
= include_prefix
;
1382 prefix
= xstrdup (GCC_INCLUDE_DIR
);
1383 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1384 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1385 prefix
[strlen (prefix
) - 7] = 0;
1388 dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
, prefix
, argv
[++i
]);
1389 append_include_chain (dirtmp
, dirtmp
);
1391 /* Add directory to end of path for includes. */
1392 if (!strcmp (argv
[i
], "-idirafter")) {
1393 struct file_name_list
*dirtmp
;
1395 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1399 if (after_include
== 0)
1400 after_include
= dirtmp
;
1402 last_after_include
->next
= dirtmp
;
1403 last_after_include
= dirtmp
; /* Tail follows the last one */
1408 if (out_fname
!= NULL
)
1409 fatal ("Output filename specified twice");
1411 fatal ("Filename missing after -o option");
1412 out_fname
= argv
[++i
];
1413 if (!strcmp (out_fname
, "-"))
1418 if (!strcmp (argv
[i
], "-pedantic"))
1420 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1422 pedantic_errors
= 1;
1423 } else if (!strcmp (argv
[i
], "-pcp")) {
1426 fatal ("Filename missing after -pcp option");
1427 pcp_fname
= argv
[++i
];
1429 = ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1430 ? fopen (pcp_fname
, "w")
1432 if (pcp_outfile
== 0)
1433 pfatal_with_name (pcp_fname
);
1439 if (!strcmp (argv
[i
], "-traditional")) {
1441 cplusplus_comments
= 0;
1442 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1448 if (! strcmp (argv
[i
], "-lang-c"))
1449 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 1, objc
= 0;
1450 else if (! strcmp (argv
[i
], "-lang-c89"))
1452 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1454 pend_defs
[2*i
] = "__STRICT_ANSI__";
1456 else if (! strcmp (argv
[i
], "-lang-c++"))
1457 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c99
= 0, objc
= 0;
1458 else if (! strcmp (argv
[i
], "-lang-objc"))
1459 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 0, objc
= 1;
1460 else if (! strcmp (argv
[i
], "-lang-objc++"))
1461 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c99
= 0, objc
= 1;
1462 else if (! strcmp (argv
[i
], "-lang-asm"))
1464 else if (! strcmp (argv
[i
], "-lang-fortran"))
1465 /* Doesn't actually do anything. */ ;
1466 else if (! strcmp (argv
[i
], "-lint"))
1471 cplusplus
= 1, cplusplus_comments
= 1;
1475 if (!strcmp (argv
[i
], "-std=gnu89"))
1477 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1479 else if (!strcmp (argv
[i
], "-std=gnu9x")
1480 || !strcmp (argv
[i
], "-std=gnu99"))
1482 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 1, objc
= 0;
1483 pend_defs
[2*i
+1] = "__STDC_VERSION__=199901L";
1485 else if (!strcmp (argv
[i
], "-std=iso9899:1990")
1486 || !strcmp (argv
[i
], "-std=c89"))
1488 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1490 pend_defs
[2*i
] = "__STRICT_ANSI__";
1492 else if (!strcmp (argv
[i
], "-std=iso9899:199409"))
1494 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1496 pend_defs
[2*i
] = "__STRICT_ANSI__";
1497 pend_defs
[2*i
+1] = "__STDC_VERSION__=199409L";
1499 else if (!strcmp (argv
[i
], "-std=iso9899:199x")
1500 || !strcmp (argv
[i
], "-std=iso9899:1999")
1501 || !strcmp (argv
[i
], "-std=c9x")
1502 || !strcmp (argv
[i
], "-std=c99"))
1504 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 1, objc
= 0;
1506 pend_defs
[2*i
] = "__STRICT_ANSI__";
1507 pend_defs
[2*i
+1] = "__STDC_VERSION__=199901L";
1512 inhibit_warnings
= 1;
1516 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1518 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1520 else if (!strcmp (argv
[i
], "-Wcomment"))
1522 else if (!strcmp (argv
[i
], "-Wno-comment"))
1524 else if (!strcmp (argv
[i
], "-Wcomments"))
1526 else if (!strcmp (argv
[i
], "-Wno-comments"))
1528 else if (!strcmp (argv
[i
], "-Wtraditional"))
1530 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1532 else if (!strcmp (argv
[i
], "-Wwhite-space"))
1533 warn_white_space
= 1;
1534 else if (!strcmp (argv
[i
], "-Wno-white-space"))
1535 warn_white_space
= 0;
1536 else if (!strcmp (argv
[i
], "-Wundef"))
1538 else if (!strcmp (argv
[i
], "-Wno-undef"))
1540 else if (!strcmp (argv
[i
], "-Wimport"))
1542 else if (!strcmp (argv
[i
], "-Wno-import"))
1544 else if (!strcmp (argv
[i
], "-Werror"))
1545 warnings_are_errors
= 1;
1546 else if (!strcmp (argv
[i
], "-Wno-error"))
1547 warnings_are_errors
= 0;
1548 else if (!strcmp (argv
[i
], "-Wall"))
1552 warn_white_space
= 1;
1557 if (!strcmp (argv
[i
], "-fleading-underscore"))
1558 user_label_prefix
= "_";
1559 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
1560 user_label_prefix
= "";
1564 /* The style of the choices here is a bit mixed.
1565 The chosen scheme is a hybrid of keeping all options in one string
1566 and specifying each option in a separate argument:
1567 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1568 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1569 -M[M][G][D file]. This is awkward to handle in specs, and is not
1571 /* ??? -MG must be specified in addition to one of -M or -MM.
1572 This can be relaxed in the future without breaking anything.
1573 The converse isn't true. */
1575 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1576 if (!strcmp (argv
[i
], "-MG"))
1578 print_deps_missing_files
= 1;
1581 if (!strcmp (argv
[i
], "-M"))
1583 else if (!strcmp (argv
[i
], "-MM"))
1585 else if (!strcmp (argv
[i
], "-MD"))
1587 else if (!strcmp (argv
[i
], "-MMD"))
1589 /* For -MD and -MMD options, write deps on file named by next arg. */
1590 if (!strcmp (argv
[i
], "-MD")
1591 || !strcmp (argv
[i
], "-MMD")) {
1593 fatal ("Filename missing after %s option", argv
[i
]);
1595 deps_file
= argv
[i
];
1598 /* For -M and -MM, write deps on standard output
1599 and suppress the usual output. */
1600 deps_stream
= stdout
;
1607 char *p
= argv
[i
] + 2;
1609 while ((c
= *p
++)) {
1610 /* Arg to -d specifies what parts of macros to dump */
1613 dump_macros
= dump_only
;
1617 dump_macros
= dump_names
;
1620 dump_macros
= dump_definitions
;
1631 if (argv
[i
][2] == '3')
1636 if (strcmp (argv
[i
], "--help") != 0)
1643 notice ("GNU CPP version %s", version_string
);
1644 #ifdef TARGET_VERSION
1647 fprintf (stderr
, "\n");
1652 print_include_names
= 1;
1656 if (argv
[i
][2] != 0)
1657 pend_defs
[2*i
] = argv
[i
] + 2;
1658 else if (i
+ 1 == argc
)
1659 fatal ("Macro name missing after -D option");
1661 i
++, pend_defs
[2*i
] = argv
[i
];
1668 if (argv
[i
][2] != 0)
1670 else if (i
+ 1 == argc
)
1671 fatal ("Assertion missing after -A option");
1675 if (!strcmp (p
, "-")) {
1676 /* -A- eliminates all predefined macros and assertions.
1677 Let's include also any that were specified earlier
1678 on the command line. That way we can get rid of any
1679 that were passed automatically in from GCC. */
1681 for (j
= 0; j
< i
; j
++)
1682 pend_defs
[2*j
] = pend_assertions
[j
] = 0;
1684 pend_assertions
[i
] = p
;
1685 pend_assertion_options
[i
] = "-A";
1690 case 'U': /* JF #undef something */
1691 if (argv
[i
][2] != 0)
1692 pend_undefs
[i
] = argv
[i
] + 2;
1693 else if (i
+ 1 == argc
)
1694 fatal ("Macro name missing after -U option");
1696 pend_undefs
[i
] = argv
[i
+1], i
++;
1700 put_out_comments
= 1;
1703 case 'E': /* -E comes from cc -E; ignore it. */
1707 no_line_directives
= 1;
1710 case '$': /* Don't include $ in identifiers. */
1711 is_idchar
['$'] = is_idstart
['$'] = 0;
1714 case 'I': /* Add directory to path for includes. */
1716 struct file_name_list
*dirtmp
;
1717 char *dir
= argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
];
1719 if (! ignore_srcdir
&& dir
&& !strcmp (dir
, "-")) {
1721 /* Don't use any preceding -I directories for #include <...>. */
1722 first_bracket_include
= 0;
1725 dirtmp
= new_include_prefix (last_include
, NULL_PTR
, "", dir
);
1726 append_include_chain (dirtmp
, dirtmp
);
1732 if (!strcmp (argv
[i
], "-nostdinc"))
1733 /* -nostdinc causes no default include directories.
1734 You must specify all include-file directories with -I. */
1735 no_standard_includes
= 1;
1736 else if (!strcmp (argv
[i
], "-nostdinc++"))
1737 /* -nostdinc++ causes no default C++-specific include directories. */
1738 no_standard_cplusplus_includes
= 1;
1739 else if (!strcmp (argv
[i
], "-noprecomp"))
1744 if (!strcmp (argv
[i
], "-remap"))
1748 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1749 if (in_fname
== NULL
) {
1752 } else if (out_fname
== NULL
) {
1755 } /* else fall through into error */
1758 fatal ("Invalid option `%s'", argv
[i
]);
1763 /* Add dirs from CPATH after dirs from -I. */
1764 /* There seems to be confusion about what CPATH should do,
1765 so for the moment it is not documented. */
1766 /* Some people say that CPATH should replace the standard include dirs,
1767 but that seems pointless: it comes before them, so it overrides them
1769 GET_ENV_PATH_LIST (cp
, "CPATH");
1770 if (cp
&& ! no_standard_includes
)
1773 /* Initialize output buffer */
1775 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1776 outbuf
.bufp
= outbuf
.buf
;
1777 outbuf
.length
= OUTBUF_SIZE
;
1779 /* Do partial setup of input buffer for the sake of generating
1780 early #line directives (when -g is in effect). */
1782 fp
= &instack
[++indepth
];
1783 if (in_fname
== NULL
)
1785 fp
->nominal_fname
= fp
->fname
= in_fname
;
1786 fp
->nominal_fname_len
= strlen (in_fname
);
1789 /* Install __LINE__, etc. Must follow initialize_char_syntax
1790 and option processing. */
1791 initialize_builtins (fp
, &outbuf
);
1793 /* Now handle the command line options. */
1795 /* Do -U's, -D's and -A's in the order they were seen. */
1796 for (i
= 1; i
< argc
; i
++) {
1797 if (pend_undefs
[i
]) {
1799 output_line_directive (fp
, &outbuf
, 0, same_file
);
1800 make_undef (pend_undefs
[i
], &outbuf
);
1802 if (pend_defs
[2*i
]) {
1804 output_line_directive (fp
, &outbuf
, 0, same_file
);
1805 make_definition (pend_defs
[2*i
]);
1807 if (pend_defs
[2*i
+1]) {
1809 output_line_directive (fp
, &outbuf
, 0, same_file
);
1810 make_definition (pend_defs
[2*i
+1]);
1812 if (pend_assertions
[i
])
1813 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1816 done_initializing
= 1;
1818 { /* Read the appropriate environment variable and if it exists
1819 replace include_defaults with the listed path. */
1821 switch ((objc
<< 1) + cplusplus
)
1824 GET_ENV_PATH_LIST (epath
, "C_INCLUDE_PATH");
1827 GET_ENV_PATH_LIST (epath
, "CPLUS_INCLUDE_PATH");
1830 GET_ENV_PATH_LIST (epath
, "OBJC_INCLUDE_PATH");
1833 GET_ENV_PATH_LIST (epath
, "OBJCPLUS_INCLUDE_PATH");
1836 /* If the environment var for this language is set,
1837 add to the default list of include directories. */
1840 char *startp
, *endp
;
1842 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1843 if (*startp
== PATH_SEPARATOR
)
1846 = (struct default_include
*) xmalloc ((num_dirs
1847 * sizeof (struct default_include
))
1848 + sizeof (include_defaults_array
));
1849 startp
= endp
= epath
;
1853 if (c
== PATH_SEPARATOR
|| !c
) {
1855 include_defaults
[num_dirs
].fname
1856 = startp
== endp
? "." : xstrdup (startp
);
1858 include_defaults
[num_dirs
].component
= 0;
1859 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1860 include_defaults
[num_dirs
].cxx_aware
= 1;
1867 /* Put the usual defaults back in at the end. */
1868 bcopy ((const PTR
) include_defaults_array
,
1869 (PTR
) &include_defaults
[num_dirs
],
1870 sizeof (include_defaults_array
));
1874 append_include_chain (before_system
, last_before_system
);
1875 first_system_include
= before_system
;
1877 /* Unless -fnostdinc,
1878 tack on the standard include file dirs to the specified list */
1879 if (!no_standard_includes
) {
1880 struct default_include
*p
= include_defaults
;
1881 char *specd_prefix
= include_prefix
;
1882 char *default_prefix
= xstrdup (GCC_INCLUDE_DIR
);
1883 int default_len
= 0;
1884 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1885 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1886 default_len
= strlen (default_prefix
) - 7;
1887 default_prefix
[default_len
] = 0;
1889 /* Search "translated" versions of GNU directories.
1890 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1891 if (specd_prefix
!= 0 && default_len
!= 0)
1892 for (p
= include_defaults
; p
->fname
; p
++) {
1893 /* Some standard dirs are only for C++. */
1894 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1895 /* Does this dir start with the prefix? */
1896 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1897 /* Yes; change prefix and add to search list. */
1898 struct file_name_list
*new
1899 = new_include_prefix (NULL_PTR
, NULL_PTR
, specd_prefix
,
1900 p
->fname
+ default_len
);
1902 new->c_system_include_path
= !p
->cxx_aware
;
1903 append_include_chain (new, new);
1904 if (first_system_include
== 0)
1905 first_system_include
= new;
1911 /* Search ordinary names for GNU include directories. */
1912 for (p
= include_defaults
; p
->fname
; p
++) {
1913 /* Some standard dirs are only for C++. */
1914 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1915 struct file_name_list
*new
1916 = new_include_prefix (NULL_PTR
, p
->component
, "", p
->fname
);
1918 new->c_system_include_path
= !p
->cxx_aware
;
1919 append_include_chain (new, new);
1920 if (first_system_include
== 0)
1921 first_system_include
= new;
1928 /* Tack the after_include chain at the end of the include chain. */
1929 append_include_chain (after_include
, last_after_include
);
1930 if (first_system_include
== 0)
1931 first_system_include
= after_include
;
1933 /* With -v, print the list of dirs to search. */
1935 struct file_name_list
*p
;
1936 notice ("#include \"...\" search starts here:\n");
1937 for (p
= include
; p
; p
= p
->next
) {
1938 if (p
== first_bracket_include
)
1939 notice ("#include <...> search starts here:\n");
1941 fprintf (stderr
, " .\n");
1942 else if (!strcmp (p
->fname
, "/") || !strcmp (p
->fname
, "//"))
1943 fprintf (stderr
, " %s\n", p
->fname
);
1945 /* Omit trailing '/'. */
1946 fprintf (stderr
, " %.*s\n", (int) strlen (p
->fname
) - 1, p
->fname
);
1948 notice ("End of search list.\n");
1950 struct default_include
* d
;
1951 notice ("The following default directories have been omitted from the search path:\n");
1952 for (d
= include_defaults
; d
->fname
; d
++)
1954 fprintf (stderr
, " %s\n", d
->fname
);
1955 notice ("End of omitted list.\n");
1959 /* -MG doesn't select the form of output and must be specified with one of
1960 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1961 inhibit compilation. */
1962 if (print_deps_missing_files
&& (print_deps
== 0 || !inhibit_output
))
1963 fatal ("-MG must be specified with one of -M or -MM");
1965 /* Either of two environment variables can specify output of deps.
1966 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1967 where OUTPUT_FILE is the file to write deps info to
1968 and DEPS_TARGET is the target to mention in the deps. */
1971 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1972 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1973 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
1978 spec
= getenv ("SUNPRO_DEPENDENCIES");
1984 /* Find the space before the DEPS_TARGET, if there is one. */
1985 s
= index (spec
, ' ');
1987 deps_target
= s
+ 1;
1988 output_file
= xmalloc (s
- spec
+ 1);
1989 bcopy (spec
, output_file
, s
- spec
);
1990 output_file
[s
- spec
] = 0;
1996 deps_file
= output_file
;
2000 /* For -M, print the expected object file name
2001 as the target of this Make-rule. */
2003 deps_allocated_size
= 200;
2004 deps_buffer
= xmalloc (deps_allocated_size
);
2010 deps_output (deps_target
, ':');
2011 } else if (*in_fname
== 0) {
2012 deps_output ("-", ':');
2017 q
= base_name (in_fname
);
2019 /* Copy remainder to mungable area. */
2020 p
= (char *) alloca (strlen(q
) + 8);
2023 /* Output P, but remove known suffixes. */
2027 && p
[len
- 2] == '.'
2028 && index("cCsSm", p
[len
- 1]))
2031 && p
[len
- 3] == '.'
2032 && p
[len
- 2] == 'c'
2033 && p
[len
- 1] == 'c')
2036 && p
[len
- 4] == '.'
2037 && p
[len
- 3] == 'c'
2038 && p
[len
- 2] == 'x'
2039 && p
[len
- 1] == 'x')
2042 && p
[len
- 4] == '.'
2043 && p
[len
- 3] == 'c'
2044 && p
[len
- 2] == 'p'
2045 && p
[len
- 1] == 'p')
2048 /* Supply our own suffix. */
2049 strcpy (q
, OBJECT_SUFFIX
);
2051 deps_output (p
, ':');
2054 deps_output (in_fname
, ' ');
2057 /* Scan the -imacros files before the main input.
2058 Much like #including them, but with no_output set
2059 so that only their macro definitions matter. */
2061 no_output
++; no_record_file
++;
2062 for (i
= 1; i
< argc
; i
++)
2063 if (pend_files
[i
]) {
2064 struct include_file
*inc
;
2065 int fd
= open_include_file (pend_files
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2067 perror_with_name (pend_files
[i
]);
2068 return FATAL_EXIT_CODE
;
2070 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2072 no_output
--; no_record_file
--;
2074 /* Copy the entire contents of the main input file into
2075 the stacked input buffer previously allocated for it. */
2077 /* JF check for stdin */
2078 if (in_fname
== NULL
|| *in_fname
== 0) {
2081 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
2084 if (fstat (f
, &st
) != 0)
2085 pfatal_with_name (in_fname
);
2086 fp
->nominal_fname
= fp
->fname
= in_fname
;
2087 fp
->nominal_fname_len
= strlen (in_fname
);
2089 fp
->system_header_p
= 0;
2090 /* JF all this is mine about reading pipes and ttys */
2091 if (! S_ISREG (st
.st_mode
)) {
2092 /* Read input from a file that is not a normal disk file.
2093 We cannot preallocate a buffer with the correct size,
2094 so we must read in the file a piece at the time and make it bigger. */
2099 if (S_ISDIR (st
.st_mode
))
2100 fatal ("Input file `%s' is a directory", in_fname
);
2104 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
2106 cnt
= safe_read (f
, (char *) fp
->buf
+ size
, bsize
- size
);
2107 if (cnt
< 0) goto perror
; /* error! */
2109 if (size
!= bsize
) break; /* End of file */
2111 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
2115 /* Read a file whose size we can determine in advance.
2116 For the sake of VMS, st.st_size is just an upper bound. */
2117 size_t s
= (size_t) st
.st_size
;
2118 if (s
!= st
.st_size
|| s
+ 2 < s
)
2120 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
2121 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
2122 if (fp
->length
< 0) goto perror
;
2125 fp
->if_stack
= if_stack
;
2127 /* Make sure data ends with a newline. And put a null after it. */
2129 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
2130 /* Backslash-newline at end is not good enough. */
2131 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
2132 fp
->buf
[fp
->length
++] = '\n';
2133 missing_newline
= 1;
2135 fp
->buf
[fp
->length
] = '\0';
2137 /* Unless inhibited, convert trigraphs in the input. */
2142 if (warn_white_space
)
2143 check_white_space (fp
);
2145 /* Now that we know the input file is valid, open the output. */
2147 if (!out_fname
|| !strcmp (out_fname
, ""))
2148 out_fname
= "stdout";
2149 else if (! freopen (out_fname
, "w", stdout
))
2150 pfatal_with_name (out_fname
);
2152 output_line_directive (fp
, &outbuf
, 0, same_file
);
2154 /* Scan the -include files before the main input. */
2157 for (i
= 1; i
< argc
; i
++)
2158 if (pend_includes
[i
]) {
2159 struct include_file
*inc
;
2160 int fd
= open_include_file (pend_includes
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2162 perror_with_name (pend_includes
[i
]);
2163 return FATAL_EXIT_CODE
;
2165 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2169 /* Scan the input, processing macros and directives. */
2171 rescan (&outbuf
, 0);
2173 if (missing_newline
)
2176 if (pedantic
&& missing_newline
)
2177 pedwarn ("file does not end in newline");
2179 /* Now we have processed the entire input
2180 Write whichever kind of output has been requested. */
2182 if (dump_macros
== dump_only
)
2184 else if (! inhibit_output
) {
2189 /* Don't actually write the deps file if compilation has failed. */
2191 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, deps_mode
)))
2192 pfatal_with_name (deps_file
);
2193 fputs (deps_buffer
, deps_stream
);
2194 putc ('\n', deps_stream
);
2196 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
2197 fatal ("I/O error on output");
2202 if (pcp_outfile
&& pcp_outfile
!= stdout
2203 && (ferror (pcp_outfile
) || fclose (pcp_outfile
) != 0))
2204 fatal ("I/O error on `-pcp' output");
2206 if (ferror (stdout
) || fclose (stdout
) != 0)
2207 fatal ("I/O error on output");
2210 exit (FATAL_EXIT_CODE
);
2211 exit (SUCCESS_EXIT_CODE
);
2214 pfatal_with_name (in_fname
);
2218 /* Given a colon-separated list of file names PATH,
2219 add all the names to the search path for include files. */
2233 struct file_name_list
*dirtmp
;
2235 /* Find the end of this name. */
2236 while ((c
= *q
++) != PATH_SEPARATOR
&& c
)
2240 dirtmp
= new_include_prefix (last_include
, NULL_PTR
,
2241 "", p
== q
? "." : p
);
2243 append_include_chain (dirtmp
, dirtmp
);
2245 /* Advance past this name. */
2252 /* Return the address of the first character in S that equals C.
2253 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2254 Return 0 if there is no such character. Assume that C itself is not '\0'.
2255 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2256 but unfortunately memchr isn't autoconfigured yet. */
2258 static const U_CHAR
*
2264 const char *p
= (const char *) s
;
2266 const char *q
= index (p
, c
);
2268 return (const U_CHAR
*) q
;
2270 size_t l
= strlen (p
);
2280 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2281 before main CCCP processing. Name `pcp' is also in honor of the
2282 drugs the trigraph designers must have been on.
2284 Using an extra pass through the buffer takes a little extra time,
2285 but is infinitely less hairy than trying to handle trigraphs inside
2286 strings, etc. everywhere, and also makes sure that trigraphs are
2287 only translated in the top level of processing. */
2293 register U_CHAR c
, *bptr
;
2294 register const U_CHAR
*fptr
, *sptr
, *lptr
;
2297 fptr
= sptr
= bptr
= buf
->buf
;
2298 lptr
= fptr
+ buf
->length
;
2299 while ((sptr
= index0 (sptr
, '?', (size_t) (lptr
- sptr
))) != NULL
) {
2336 len
= sptr
- fptr
- 2;
2338 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2339 C, this will be memmove (). */
2340 if (bptr
!= fptr
&& len
> 0)
2341 bcopy ((const PTR
) fptr
, (PTR
) bptr
, len
);
2347 len
= buf
->length
- (fptr
- buf
->buf
);
2348 if (bptr
!= fptr
&& len
> 0)
2349 bcopy ((const PTR
) fptr
, (PTR
) bptr
, len
);
2350 buf
->length
-= fptr
- bptr
;
2351 buf
->buf
[buf
->length
] = '\0';
2352 if (warn_trigraphs
&& fptr
!= bptr
)
2353 warning_with_line (0, "%lu trigraph(s) encountered",
2354 (unsigned long) (fptr
- bptr
) / 2);
2357 /* Warn about white space between backslash and end of line. */
2360 check_white_space (buf
)
2363 register const U_CHAR
*sptr
= buf
->buf
;
2364 register const U_CHAR
*lptr
= sptr
+ buf
->length
;
2365 register const U_CHAR
*nptr
;
2368 nptr
= sptr
= buf
->buf
;
2369 lptr
= sptr
+ buf
->length
;
2371 (nptr
= index0 (nptr
, '\n', (size_t) (lptr
- nptr
))) != NULL
;
2373 register const U_CHAR
*p
= nptr
;
2375 for (p
= nptr
; sptr
< p
; p
--) {
2376 if (! is_hor_space
[p
[-1]]) {
2377 if (p
[-1] == '\\' && p
!= nptr
)
2378 warning_with_line (line
,
2379 "`\\' followed by white space at end of line");
2386 /* Move all backslash-newline pairs out of embarrassing places.
2387 Exchange all such pairs following BP
2388 with any potentially-embarrassing characters that follow them.
2389 Potentially-embarrassing characters are / and *
2390 (because a backslash-newline inside a comment delimiter
2391 would cause it not to be recognized).
2392 We assume that *BP == '\\'. */
2398 register U_CHAR
*p
= bp
;
2400 /* First count the backslash-newline pairs here. */
2405 } while (*p
== '\\');
2407 /* What follows the backslash-newlines is not embarrassing. */
2409 if (*p
!= '/' && *p
!= '*')
2410 /* What follows the backslash-newlines is not embarrassing. */
2413 /* Copy all potentially embarrassing characters
2414 that follow the backslash-newline pairs
2415 down to where the pairs originally started. */
2418 while (*p
== '*' || *p
== '/');
2420 /* Now write the same number of pairs after the embarrassing chars. */
2427 /* Like newline_fix but for use within a directive-name.
2428 Move any backslash-newlines up past any following symbol constituents. */
2431 name_newline_fix (bp
)
2434 register U_CHAR
*p
= bp
;
2436 /* First count the backslash-newline pairs here. */
2441 } while (*p
== '\\');
2443 /* What follows the backslash-newlines is not embarrassing. */
2446 /* What follows the backslash-newlines is not embarrassing. */
2449 /* Copy all potentially embarrassing characters
2450 that follow the backslash-newline pairs
2451 down to where the pairs originally started. */
2454 while (is_idchar
[*p
]);
2456 /* Now write the same number of pairs after the embarrassing chars. */
2463 /* Look for lint commands in comments.
2465 When we come in here, ibp points into a comment. Limit is as one expects.
2466 scan within the comment -- it should start, after lwsp, with a lint command.
2467 If so that command is returned as a (constant) string.
2469 Upon return, any arg will be pointed to with argstart and will be
2470 arglen long. Note that we don't parse that arg since it will just
2471 be printed out again. */
2474 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2475 register const U_CHAR
*ibp
;
2476 register const U_CHAR
*limit
;
2477 const U_CHAR
**argstart
; /* point to command arg */
2478 int *arglen
, *cmdlen
; /* how long they are */
2480 HOST_WIDEST_INT linsize
;
2481 register const U_CHAR
*numptr
; /* temp for arg parsing */
2485 SKIP_WHITE_SPACE (ibp
);
2487 if (ibp
>= limit
) return NULL
;
2489 linsize
= limit
- ibp
;
2491 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2492 if ((linsize
>= 10) && !bcmp (ibp
, "NOTREACHED", 10)) {
2494 return "NOTREACHED";
2496 if ((linsize
>= 8) && !bcmp (ibp
, "ARGSUSED", 8)) {
2500 if ((linsize
>= 11) && !bcmp (ibp
, "LINTLIBRARY", 11)) {
2502 return "LINTLIBRARY";
2504 if ((linsize
>= 7) && !bcmp (ibp
, "VARARGS", 7)) {
2506 ibp
+= 7; linsize
-= 7;
2507 if ((linsize
== 0) || ! ISDIGIT (*ibp
)) return "VARARGS";
2509 /* OK, read a number */
2510 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && ISDIGIT (*numptr
);
2512 *arglen
= numptr
- *argstart
;
2519 * The main loop of the program.
2521 * Read characters from the input stack, transferring them to the
2524 * Macros are expanded and push levels on the input stack.
2525 * At the end of such a level it is popped off and we keep reading.
2526 * At the end of any other kind of level, we return.
2527 * #-directives are handled, except within macros.
2529 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2530 * and insert them when appropriate. This is set while scanning macro
2531 * arguments before substitution. It is zero when scanning for final output.
2532 * There are two types of Newline markers:
2533 * * Newline - follows a macro name that was not expanded
2534 * because it appeared inside an expansion of the same macro.
2535 * This marker prevents future expansion of that identifier.
2536 * When the input is rescanned into the final output, these are deleted.
2537 * These are also deleted by ## concatenation.
2538 * * Newline Space (or Newline and any other whitespace character)
2539 * stands for a place that tokens must be separated or whitespace
2540 * is otherwise desirable, but where the ANSI standard specifies there
2541 * is no whitespace. This marker turns into a Space (or whichever other
2542 * whitespace char appears in the marker) in the final output,
2543 * but it turns into nothing in an argument that is stringified with #.
2544 * Such stringified arguments are the only place where the ANSI standard
2545 * specifies with precision that whitespace may not appear.
2547 * During this function, IP->bufp is kept cached in IBP for speed of access.
2548 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2549 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2550 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2551 * explicitly, and before RECACHE, since RECACHE uses OBP.
2555 rescan (op
, output_marks
)
2559 /* Character being scanned in main loop. */
2562 /* Length of pending accumulated identifier. */
2563 register int ident_length
= 0;
2565 /* Hash code of pending accumulated identifier. */
2566 register int hash
= 0;
2568 /* Current input level (&instack[indepth]). */
2571 /* Pointer for scanning input. */
2572 register U_CHAR
*ibp
;
2574 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2575 register U_CHAR
*limit
;
2577 /* Pointer for storing output. */
2578 register U_CHAR
*obp
;
2580 /* REDO_CHAR is nonzero if we are processing an identifier
2581 after backing up over the terminating character.
2582 Sometimes we process an identifier without backing up over
2583 the terminating character, if the terminating character
2584 is not special. Backing up is done so that the terminating character
2585 will be dispatched on again once the identifier is dealt with. */
2588 /* 1 if within an identifier inside of which a concatenation
2589 marker (Newline -) has been seen. */
2590 int concatenated
= 0;
2592 /* While scanning a comment or a string constant,
2593 this records the line it started on, for error messages. */
2596 /* Record position of last `real' newline. */
2597 U_CHAR
*beg_of_line
;
2599 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2602 do { ip->macro->type = T_MACRO; \
2603 if (ip->free_ptr) free (ip->free_ptr); \
2604 --indepth; } while (0)
2606 /* Reload `rescan's local variables that describe the current
2607 level of the input stack. */
2610 do { ip = &instack[indepth]; \
2612 limit = ip->buf + ip->length; \
2614 check_expand (op, limit - ibp); \
2616 obp = op->bufp; } while (0)
2618 if (no_output
&& instack
[indepth
].fname
!= 0)
2619 skip_if_group (&instack
[indepth
], 1, NULL
);
2626 /* Our caller must always put a null after the end of
2627 the input at each input stack level. */
2637 if (*ibp
== '\n' && !ip
->macro
) {
2638 /* At the top level, always merge lines ending with backslash-newline,
2639 even in middle of identifier. But do not merge lines in a macro,
2640 since backslash might be followed by a newline-space marker. */
2643 --obp
; /* remove backslash from obuf */
2646 /* If ANSI, backslash is just another character outside a string. */
2649 /* Otherwise, backslash suppresses specialness of following char,
2650 so copy it here to prevent the switch from seeing it.
2651 But first get any pending identifier processed. */
2652 if (ident_length
> 0)
2659 if (ident_length
|| ip
->macro
|| traditional
)
2661 while (*ibp
== '\\' && ibp
[1] == '\n') {
2667 /* Treat this %: digraph as if it were #. */
2671 if (assertions_flag
) {
2674 /* Copy #foo (bar lose) without macro expansion. */
2675 obp
[-1] = '#'; /* In case it was '%'. */
2676 SKIP_WHITE_SPACE (ibp
);
2677 while (is_idchar
[*ibp
])
2679 SKIP_WHITE_SPACE (ibp
);
2682 skip_paren_group (ip
);
2683 bcopy ((const PTR
) ibp
, (PTR
) obp
, ip
->bufp
- ibp
);
2684 obp
+= ip
->bufp
- ibp
;
2690 /* If this is expanding a macro definition, don't recognize
2691 preprocessing directives. */
2694 /* If this is expand_into_temp_buffer,
2695 don't recognize them either. Warn about them
2696 only after an actual newline at this level,
2697 not at the beginning of the input level. */
2699 if (ip
->buf
!= beg_of_line
)
2700 warning ("preprocessing directive not recognized within macro arg");
2707 /* # keyword: a # must be first nonblank char on the line */
2708 if (beg_of_line
== 0)
2713 /* Scan from start of line, skipping whitespace, comments
2714 and backslash-newlines, and see if we reach this #.
2715 If not, this # is not special. */
2717 /* If -traditional, require # to be at beginning of line. */
2720 if (is_hor_space
[*bp
])
2722 else if (*bp
== '\\' && bp
[1] == '\n')
2724 else if (*bp
== '/' && bp
[1] == '*') {
2738 #ifdef MULTIBYTE_CHARS
2740 length
= local_mblen (bp
, limit
- bp
);
2748 /* There is no point in trying to deal with C++ // comments here,
2749 because if there is one, then this # must be part of the
2750 comment and we would never reach here. */
2756 while (bp
[1] == '\\' && bp
[2] == '\n')
2760 /* %: appears at start of line; skip past the ':' too. */
2769 /* This # can start a directive. */
2771 --obp
; /* Don't copy the '#' */
2775 if (! handle_directive (ip
, op
)) {
2779 /* Not a known directive: treat it as ordinary text.
2780 IP, OP, IBP, etc. have not been changed. */
2781 if (no_output
&& instack
[indepth
].fname
) {
2782 /* If not generating expanded output,
2783 what we do with ordinary text is skip it.
2784 Discard everything until next # directive. */
2785 skip_if_group (&instack
[indepth
], 1, 0);
2790 *obp
++ = '#'; /* Copy # (even if it was originally %:). */
2791 /* Don't expand an identifier that could be a macro directive.
2792 (Section 3.8.3 of the ANSI C standard) */
2793 SKIP_WHITE_SPACE (ibp
);
2794 if (is_idstart
[*ibp
])
2797 while (is_idchar
[*ibp
])
2805 /* A # directive has been successfully processed. */
2806 /* If not generating expanded output, ignore everything until
2807 next # directive. */
2808 if (no_output
&& instack
[indepth
].fname
)
2809 skip_if_group (&instack
[indepth
], 1, 0);
2815 case '\"': /* skip quoted string */
2817 /* A single quoted string is treated like a double -- some
2818 programs (e.g., troff) are perverse this way */
2820 /* Handle any pending identifier;
2821 but the L in L'...' or L"..." is not an identifier. */
2823 if (! (ident_length
== 1 && hash
== HASHSTEP (0, 'L')))
2825 ident_length
= hash
= 0;
2828 start_line
= ip
->lineno
;
2830 /* Skip ahead to a matching quote. */
2834 if (ip
->macro
!= 0) {
2835 /* try harder: this string crosses a macro expansion boundary.
2836 This can happen naturally if -traditional.
2837 Otherwise, only -D can make a macro with an unmatched quote. */
2843 error_with_line (line_for_error (start_line
),
2844 "unterminated string or character constant");
2845 if (multiline_string_line
) {
2846 error_with_line (multiline_string_line
,
2847 "possible real start of unterminated constant");
2848 multiline_string_line
= 0;
2856 if (warn_white_space
&& ip
->fname
&& is_hor_space
[ibp
[-2]])
2857 warning ("white space at end of line in string");
2860 /* Traditionally, end of line ends a string constant with no error.
2861 So exit the loop and record the new line. */
2867 error_with_line (line_for_error (start_line
),
2868 "unterminated character constant");
2871 if (multiline_string_line
== 0) {
2873 pedwarn_with_line (line_for_error (start_line
),
2874 "string constant runs past end of line");
2875 multiline_string_line
= ip
->lineno
- 1;
2881 /* Backslash newline is replaced by nothing at all, but
2882 keep the line counts correct. But if we are reading
2883 from a macro, keep the backslash newline, since backslash
2884 newlines have already been processed. */
2893 /* ANSI stupidly requires that in \\ the second \
2894 is *not* prevented from combining with a newline. */
2896 while (*ibp
== '\\' && ibp
[1] == '\n') {
2912 #ifdef MULTIBYTE_CHARS
2917 length
= local_mblen (ibp
, limit
- ibp
);
2921 bcopy (ibp
, obp
, length
);
2941 && !(cplusplus_comments
&& *ibp
== '/'))
2947 /* C++ style comment... */
2948 start_line
= ip
->lineno
;
2950 /* Comments are equivalent to spaces. */
2951 if (! put_out_comments
)
2955 U_CHAR
*before_bp
= ibp
;
2957 while (++ibp
< limit
) {
2960 if (put_out_comments
) {
2961 bcopy ((const PTR
) before_bp
, (PTR
) obp
, ibp
- before_bp
);
2962 obp
+= ibp
- before_bp
;
2968 if (ibp
+ 1 < limit
&& ibp
[1] == '\n')
2971 warning ("multiline `//' comment");
2973 /* Copy the newline into the output buffer, in order to
2974 avoid the pain of a #line every time a multiline comment
2976 if (!put_out_comments
)
2984 #ifdef MULTIBYTE_CHARS
2986 length
= local_mblen (ibp
, limit
- ibp
);
2988 ibp
+= (length
- 1);
2996 /* Ordinary C comment. Skip it, optionally copying it to output. */
2998 start_line
= ip
->lineno
;
3000 ++ibp
; /* Skip the star. */
3002 /* If this cpp is for lint, we peek inside the comments: */
3004 const U_CHAR
*argbp
;
3006 const char *lintcmd
=
3007 get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
3009 if (lintcmd
!= NULL
) {
3011 check_expand (op
, cmdlen
+ arglen
+ 14);
3013 /* I believe it is always safe to emit this newline: */
3015 bcopy ("#pragma lint ", (char *) obp
, 13);
3017 bcopy (lintcmd
, (char *) obp
, cmdlen
);
3022 bcopy (argbp
, (char *) obp
, arglen
);
3026 /* OK, now bring us back to the state we were in before we entered
3027 this branch. We need #line because the #pragma's newline always
3028 messes up the line count. */
3030 output_line_directive (ip
, op
, 0, same_file
);
3031 check_expand (op
, limit
- ibp
+ 2);
3037 /* Comments are equivalent to spaces.
3038 Note that we already output the slash; we might not want it.
3039 For -traditional, a comment is equivalent to nothing. */
3040 if (! put_out_comments
) {
3050 U_CHAR
*before_bp
= ibp
;
3055 if (ibp
[-2] == '/' && warn_comments
)
3056 warning ("`/*' within comment");
3065 /* Copy the newline into the output buffer, in order to
3066 avoid the pain of a #line every time a multiline comment
3068 if (!put_out_comments
)
3075 error_with_line (line_for_error (start_line
),
3076 "unterminated comment");
3080 #ifdef MULTIBYTE_CHARS
3084 length
= local_mblen (ibp
, limit
- ibp
);
3086 ibp
+= (length
- 1);
3095 if (put_out_comments
) {
3096 bcopy ((const PTR
) before_bp
, (PTR
) obp
, ibp
- before_bp
);
3097 obp
+= ibp
- before_bp
;
3103 if (! is_idchar
['$'])
3106 pedwarn ("`$' in identifier");
3109 case '0': case '1': case '2': case '3': case '4':
3110 case '5': case '6': case '7': case '8': case '9':
3111 /* If digit is not part of identifier, it starts a number,
3112 which means that following letters are not an identifier.
3113 "0x5" does not refer to an identifier "x5".
3114 So copy all alphanumerics that follow without accumulating
3115 as an identifier. Periods also, for sake of "3.e7". */
3117 if (ident_length
== 0) {
3120 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3126 if (!is_idchar
[c
] && c
!= '.') {
3131 /* A sign can be part of a preprocessing number
3132 if it follows an `e' or `p'. */
3133 if (c
== 'e' || c
== 'E' || c
== 'p' || c
== 'P') {
3135 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3140 if (*ibp
== '+' || *ibp
== '-') {
3142 /* But traditional C does not let the token go past the sign,
3143 and C89 does not allow `p'. */
3144 if (traditional
|| (c89
&& (c
== 'p' || c
== 'P')))
3154 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3155 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3156 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3157 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3159 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3160 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3161 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3162 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3166 /* Compute step of hash function, to avoid a proc call on every token */
3167 hash
= HASHSTEP (hash
, c
);
3171 if (ip
->fname
== 0 && *ibp
== '-') {
3172 /* Newline - inhibits expansion of preceding token.
3173 If expanding a macro arg, we keep the newline -.
3174 In final output, it is deleted.
3175 We recognize Newline - in macro bodies and macro args. */
3176 if (! concatenated
) {
3181 if (!output_marks
) {
3184 /* If expanding a macro arg, keep the newline -. */
3190 /* If reprocessing a macro expansion, newline is a special marker. */
3191 else if (ip
->macro
!= 0) {
3192 /* Newline White is a "funny space" to separate tokens that are
3193 supposed to be separate but without space between.
3194 Here White means any whitespace character.
3195 Newline - marks a recursive macro use that is not
3196 supposed to be expandable. */
3198 if (is_space
[*ibp
]) {
3199 /* Newline Space does not prevent expansion of preceding token
3200 so expand the preceding token and then come back. */
3201 if (ident_length
> 0)
3204 /* If generating final output, newline space makes a space. */
3205 if (!output_marks
) {
3207 /* And Newline Newline makes a newline, so count it. */
3208 if (obp
[-1] == '\n')
3211 /* If expanding a macro arg, keep the newline space.
3212 If the arg gets stringified, newline space makes nothing. */
3215 } else abort (); /* Newline followed by something random? */
3219 /* If there is a pending identifier, handle it and come back here. */
3220 if (ident_length
> 0)
3225 /* Update the line counts and output a #line if necessary. */
3228 if (ip
->lineno
!= op
->lineno
) {
3230 output_line_directive (ip
, op
, 1, same_file
);
3231 check_expand (op
, limit
- ibp
);
3236 /* Come here either after (1) a null character that is part of the input
3237 or (2) at the end of the input, because there is a null there. */
3240 /* Our input really contains a null character. */
3244 /* At end of a macro-expansion level, pop it and read next level. */
3245 if (ip
->macro
!= 0) {
3248 /* If traditional, and we have an identifier that ends here,
3249 process it now, so we get the right error for recursion. */
3250 if (traditional
&& ident_length
3251 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
3260 /* If we don't have a pending identifier,
3261 return at end of input. */
3262 if (ident_length
== 0) {
3270 /* If we do have a pending identifier, just consider this null
3271 a special character and arrange to dispatch on it again.
3272 The second time, IDENT_LENGTH will be zero so we will return. */
3278 /* Handle the case of a character such as /, ', " or null
3279 seen following an identifier. Back over it so that
3280 after the identifier is processed the special char
3281 will be dispatched on again. */
3291 if (ident_length
> 0) {
3292 register HASHNODE
*hp
;
3294 /* We have just seen an identifier end. If it's a macro, expand it.
3296 IDENT_LENGTH is the length of the identifier
3297 and HASH is its hash code.
3299 The identifier has already been copied to the output,
3300 so if it is a macro we must remove it.
3302 If REDO_CHAR is 0, the char that terminated the identifier
3303 has been skipped in the output and the input.
3304 OBP-IDENT_LENGTH-1 points to the identifier.
3305 If the identifier is a macro, we must back over the terminator.
3307 If REDO_CHAR is 1, the terminating char has already been
3308 backed over. OBP-IDENT_LENGTH points to the identifier. */
3310 if (!pcp_outfile
|| pcp_inside_if
) {
3311 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
3314 if (hp
->length
== ident_length
) {
3315 int obufp_before_macroname
;
3316 int op_lineno_before_macroname
;
3317 register int i
= ident_length
;
3318 register U_CHAR
*p
= hp
->name
;
3319 register U_CHAR
*q
= obp
- i
;
3325 do { /* All this to avoid a strncmp () */
3330 /* We found a use of a macro name.
3331 see if the context shows it is a macro call. */
3333 /* Back up over terminating character if not already done. */
3339 /* Save this as a displacement from the beginning of the output
3340 buffer. We can not save this as a position in the output
3341 buffer, because it may get realloc'ed by RECACHE. */
3342 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
3343 op_lineno_before_macroname
= op
->lineno
;
3345 if (hp
->type
== T_PCSTRING
) {
3346 pcstring_used (hp
); /* Mark the definition of this key
3347 as needed, ensuring that it
3349 break; /* Exit loop, since the key cannot have a
3350 definition any longer. */
3353 /* Record whether the macro is disabled. */
3354 disabled
= hp
->type
== T_DISABLED
;
3356 /* This looks like a macro ref, but if the macro was disabled,
3357 just copy its name and put in a marker if requested. */
3361 /* This error check caught useful cases such as
3362 #define foo(x,y) bar (x (y,0), y)
3365 error ("recursive use of macro `%s'", hp
->name
);
3370 check_expand (op
, limit
- ibp
+ 2);
3378 /* If macro wants an arglist, verify that a '(' follows.
3379 first skip all whitespace, copying it to the output
3380 after the macro name. Then, if there is no '(',
3381 decide this is not a macro call and leave things that way. */
3382 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
3383 && hp
->value
.defn
->nargs
>= 0)
3385 U_CHAR
*old_ibp
= ibp
;
3386 U_CHAR
*old_obp
= obp
;
3387 int old_iln
= ip
->lineno
;
3388 int old_oln
= op
->lineno
;
3391 /* Scan forward over whitespace, copying it to the output. */
3392 if (ibp
== limit
&& ip
->macro
!= 0) {
3397 old_iln
= ip
->lineno
;
3398 old_oln
= op
->lineno
;
3400 else if (is_space
[*ibp
]) {
3402 if (ibp
[-1] == '\n') {
3403 if (ip
->macro
== 0) {
3404 /* Newline in a file. Count it. */
3407 } else if (!output_marks
) {
3408 /* A newline mark, and we don't want marks
3409 in the output. If it is newline-hyphen,
3410 discard it entirely. Otherwise, it is
3411 newline-whitechar, so keep the whitechar. */
3421 /* A newline mark; copy both chars to the output. */
3428 else if (*ibp
== '/') {
3429 /* If a comment, copy it unchanged or discard it. */
3431 newline_fix (ibp
+ 1);
3432 if (ibp
[1] == '*') {
3433 if (put_out_comments
) {
3436 } else if (! traditional
) {
3439 for (ibp
+= 2; ibp
< limit
; ibp
++) {
3440 /* We need not worry about newline-marks,
3441 since they are never found in comments. */
3442 if (ibp
[0] == '*') {
3444 newline_fix (ibp
+ 1);
3445 if (ibp
[1] == '/') {
3447 if (put_out_comments
) {
3454 else if (*ibp
== '\n') {
3455 /* Newline in a file. Count it. */
3461 #ifdef MULTIBYTE_CHARS
3463 length
= local_mblen (ibp
, limit
- ibp
);
3466 if (put_out_comments
)
3468 bcopy (ibp
, obp
, length
- 1);
3471 ibp
+= (length
- 1);
3475 if (put_out_comments
)
3478 } else if (ibp
[1] == '/' && cplusplus_comments
) {
3479 if (put_out_comments
) {
3482 } else if (! traditional
) {
3485 for (ibp
+= 2; ; ibp
++)
3489 if (*ibp
== '\\' && ibp
[1] == '\n')
3491 if (put_out_comments
)
3496 #ifdef MULTIBYTE_CHARS
3498 length
= local_mblen (ibp
, limit
- ibp
);
3501 if (put_out_comments
)
3503 bcopy (ibp
, obp
, length
- 1);
3506 ibp
+= (length
- 1);
3510 if (put_out_comments
)
3516 else if (ibp
[0] == '\\' && ibp
[1] == '\n') {
3523 /* It isn't a macro call.
3524 Put back the space that we just skipped. */
3527 ip
->lineno
= old_iln
;
3528 op
->lineno
= old_oln
;
3529 /* Exit the for loop. */
3534 /* This is now known to be a macro call.
3535 Discard the macro name from the output,
3536 along with any following whitespace just copied,
3537 but preserve newlines if not outputting marks since this
3538 is more likely to do the right thing with line numbers. */
3539 obp
= op
->buf
+ obufp_before_macroname
;
3541 op
->lineno
= op_lineno_before_macroname
;
3543 int newlines
= op
->lineno
- op_lineno_before_macroname
;
3544 while (0 < newlines
--)
3548 /* Prevent accidental token-pasting with a character
3549 before the macro call. */
3550 if (!traditional
&& obp
!= op
->buf
) {
3552 case '!': case '%': case '&': case '*':
3553 case '+': case '-': case '.': case '/':
3554 case ':': case '<': case '=': case '>':
3556 /* If we are expanding a macro arg, make a newline marker
3557 to separate the tokens. If we are making real output,
3558 a plain space will do. */
3565 /* Expand the macro, reading arguments as needed,
3566 and push the expansion on the input stack. */
3569 macroexpand (hp
, op
);
3571 /* Reexamine input stack, since macroexpand has pushed
3572 a new level on it. */
3579 } /* End hash-table-search loop */
3581 ident_length
= hash
= 0; /* Stop collecting identifier */
3584 } /* End if (ident_length > 0) */
3586 } /* End per-char loop */
3588 /* Come here to return -- but first give an error message
3589 if there was an unterminated successful conditional. */
3591 if (if_stack
!= ip
->if_stack
)
3595 switch (if_stack
->type
)
3616 error_with_line (line_for_error (if_stack
->lineno
),
3617 "unterminated `#%s' conditional", str
);
3619 if_stack
= ip
->if_stack
;
3623 * Rescan a string into a temporary buffer and return the result
3624 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3626 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3627 * and insert such markers when appropriate. See `rescan' for details.
3628 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3629 * before substitution; it is 0 for other uses.
3632 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
3634 const U_CHAR
*limit
;
3635 int output_marks
, assertions
;
3637 register FILE_BUF
*ip
;
3639 int length
= limit
- buf
;
3641 int odepth
= indepth
;
3642 int save_assertions_flag
= assertions_flag
;
3644 assertions_flag
= assertions
;
3649 /* Set up the input on the input stack. */
3651 buf1
= (U_CHAR
*) alloca (length
+ 1);
3653 register const U_CHAR
*p1
= buf
;
3654 register U_CHAR
*p2
= buf1
;
3661 /* Set up to receive the output. */
3663 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3664 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3665 obuf
.nominal_fname
= 0;
3672 obuf
.system_header_p
= 0;
3674 CHECK_DEPTH ({return obuf
;});
3678 ip
= &instack
[indepth
];
3680 ip
->nominal_fname
= 0;
3681 ip
->nominal_fname_len
= 0;
3683 ip
->system_header_p
= 0;
3686 ip
->length
= length
;
3687 ip
->buf
= ip
->bufp
= buf1
;
3688 ip
->if_stack
= if_stack
;
3690 ip
->lineno
= obuf
.lineno
= 1;
3692 /* Scan the input, create the output. */
3693 rescan (&obuf
, output_marks
);
3695 /* Pop input stack to original state. */
3698 if (indepth
!= odepth
)
3701 assertions_flag
= save_assertions_flag
;
3706 * Process a # directive. Expects IP->bufp to point after the '#', as in
3707 * `#define foo bar'. Passes to the directive handler
3708 * (do_define, do_include, etc.): the addresses of the 1st and
3709 * last chars of the directive (starting immediately after the #
3710 * keyword), plus op and the keyword table pointer. If the directive
3711 * contains comments it is copied into a temporary buffer sans comments
3712 * and the temporary buffer is passed to the directive handler instead.
3713 * Likewise for backslash-newlines.
3715 * Returns nonzero if this was a known # directive.
3716 * Otherwise, returns zero, without advancing the input pointer.
3720 handle_directive (ip
, op
)
3723 register U_CHAR
*bp
, *cp
;
3724 register struct directive
*kt
;
3725 register int ident_length
;
3728 /* Nonzero means we must copy the entire directive
3729 to get rid of comments or backslash-newlines. */
3730 int copy_directive
= 0;
3732 U_CHAR
*ident
, *after_ident
;
3736 /* Record where the directive started. do_xifdef needs this. */
3737 directive_start
= bp
- 1;
3739 ignore_escape_flag
= 1;
3741 /* Skip whitespace and \-newline. */
3743 if (is_hor_space
[*bp
]) {
3744 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3745 pedwarn_strange_white_space (*bp
);
3747 } else if (*bp
== '/') {
3749 newline_fix (bp
+ 1);
3750 if (! (bp
[1] == '*' || (cplusplus_comments
&& bp
[1] == '/')))
3753 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3755 } else if (*bp
== '\\' && bp
[1] == '\n') {
3756 bp
+= 2; ip
->lineno
++;
3760 /* Now find end of directive name.
3761 If we encounter a backslash-newline, exchange it with any following
3762 symbol-constituents so that we end up with a contiguous name. */
3770 name_newline_fix (cp
);
3776 ident_length
= cp
- bp
;
3780 /* A line of just `#' becomes blank. */
3782 if (ident_length
== 0 && *after_ident
== '\n') {
3783 ip
->bufp
= after_ident
;
3787 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3789 while (is_idchar
[*p
]) {
3790 if (*p
< '0' || *p
> '9')
3794 /* Handle # followed by a line number. */
3795 if (p
!= ident
&& !is_idchar
[*p
]) {
3796 static struct directive line_directive_table
[] = {
3797 { 4, do_line
, "line", T_LINE
},
3800 pedwarn ("`#' followed by integer");
3801 after_ident
= ident
;
3802 kt
= line_directive_table
;
3803 ignore_escape_flag
= 0;
3807 /* Avoid error for `###' and similar cases unless -pedantic. */
3809 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3811 if (pedantic
&& !lang_asm
)
3812 warning ("invalid preprocessing directive");
3818 error ("invalid preprocessing directive name");
3824 * Decode the keyword and call the appropriate expansion
3825 * routine, after moving the input pointer up to the next line.
3827 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3828 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3829 register U_CHAR
*buf
;
3830 register U_CHAR
*limit
;
3833 int *already_output
;
3835 /* Nonzero means do not delete comments within the directive.
3836 #define needs this when -traditional. */
3841 limit
= ip
->buf
+ ip
->length
;
3844 keep_comments
= traditional
&& kt
->type
== T_DEFINE
;
3845 /* #import is defined only in Objective C, or when on the NeXT. */
3846 if (kt
->type
== T_IMPORT
3847 && !(objc
|| lookup ((const U_CHAR
*) "__NeXT__", -1, -1)))
3850 /* Find the end of this directive (first newline not backslashed
3851 and not in a string or comment).
3852 Set COPY_DIRECTIVE if the directive must be copied
3853 (it contains a backslash-newline or a comment). */
3855 buf
= bp
= after_ident
;
3856 while (bp
< limit
) {
3857 register U_CHAR c
= *bp
++;
3864 } else if (traditional
&& bp
< limit
)
3869 /* "..." is special for #include. */
3870 if (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)) {
3871 while (bp
< limit
&& *bp
!= '\n') {
3876 if (*bp
== '\\' && bp
[1] == '\n') {
3887 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3888 /* Don't bother calling the directive if we already got an error
3889 message due to unterminated string. Skip everything and pretend
3890 we called the directive. */
3893 /* Traditional preprocessing permits unterminated strings. */
3902 /* <...> is special for #include. */
3904 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3906 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3907 if (*bp
== '\\' && bp
[1] == '\n') {
3920 || (cplusplus_comments
&& *bp
== '/')) {
3921 U_CHAR
*obp
= bp
- 1;
3923 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3925 /* No need to copy the directive because of a comment at the end;
3926 just don't include the comment in the directive. */
3927 if (!put_out_comments
) {
3929 for (p
= bp
; *p
== ' ' || *p
== '\t'; p
++)
3936 /* Don't remove the comments if -traditional. */
3937 if (! keep_comments
)
3946 pedwarn_strange_white_space (c
);
3950 --bp
; /* Point to the newline */
3958 resume_p
= ip
->bufp
;
3959 /* BP is the end of the directive.
3960 RESUME_P is the next interesting data after the directive.
3961 A comment may come between. */
3963 /* If a directive should be copied through, and -C was given,
3964 pass it through before removing comments. */
3965 if (!no_output
&& put_out_comments
3966 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
3967 ? dump_macros
== dump_definitions
3968 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
3969 : kt
->type
== T_PRAGMA
)) {
3972 /* Output directive name. */
3973 check_expand (op
, kt
->length
+ 2);
3974 /* Make sure # is at the start of a line */
3975 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3980 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3981 op
->bufp
+= kt
->length
;
3983 /* Output arguments. */
3985 check_expand (op
, len
);
3986 bcopy (buf
, (char *) op
->bufp
, len
);
3988 /* Take account of any (escaped) newlines just output. */
3990 if (buf
[len
] == '\n')
3993 already_output
= &junk
;
3994 } /* Don't we need a newline or #line? */
3996 if (copy_directive
) {
3997 register U_CHAR
*xp
= buf
;
3998 /* Need to copy entire directive into temp buffer before dispatching */
4000 /* room for directive plus some slop */
4001 cp
= (U_CHAR
*) alloca (2 * (bp
- buf
) + 5);
4004 /* Copy to the new buffer, deleting comments
4005 and backslash-newlines (and whitespace surrounding the latter
4006 if outside of char and string constants). */
4009 register U_CHAR c
= *xp
++;
4014 abort (); /* A bare newline should never part of the line. */
4017 /* <...> is special for #include. */
4019 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
4021 while (xp
< bp
&& c
!= '>') {
4023 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
4034 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
4035 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
4037 SKIP_WHITE_SPACE (xp
);
4038 } else if (is_hor_space
[*xp
]) {
4040 SKIP_WHITE_SPACE (xp
);
4042 } else if (traditional
&& xp
< bp
) {
4050 int backslash_newlines_p
= 0;
4052 register const U_CHAR
*bp1
4053 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
4054 NULL_PTR
, &backslash_newlines_p
,
4056 if (backslash_newlines_p
)
4059 /* With something like:
4064 we should still remove the backslash-newline
4065 pair as part of phase two. */
4066 if (xp
[0] == '\\' && xp
[1] == '\n')
4072 /* This is the same as the loop above, but taking
4073 advantage of the fact that we know there are no
4074 backslash-newline pairs. */
4082 || (cplusplus_comments
&& *xp
== '/')) {
4084 /* If we already copied the directive through,
4085 already_output != 0 prevents outputting comment now. */
4086 skip_to_end_of_comment (ip
, already_output
, 0);
4088 while (xp
!= ip
->bufp
)
4090 /* Delete or replace the slash. */
4091 else if (traditional
)
4100 /* Null-terminate the copy. */
4106 ip
->bufp
= resume_p
;
4108 /* Some directives should be written out for cc1 to process,
4109 just as if they were not defined. And sometimes we're copying
4110 directives through. */
4112 if (!no_output
&& already_output
== 0
4113 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
4114 ? (int) dump_names
<= (int) dump_macros
4115 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
4116 : kt
->type
== T_PRAGMA
)) {
4119 /* Output directive name. */
4120 check_expand (op
, kt
->length
+ 1);
4122 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
4123 op
->bufp
+= kt
->length
;
4125 if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
4126 /* Output `#define name' only. */
4129 SKIP_WHITE_SPACE (xp
);
4131 while (is_idchar
[*xp
]) xp
++;
4133 check_expand (op
, len
+ 1);
4135 bcopy (yp
, (char *) op
->bufp
, len
);
4137 /* Output entire directive. */
4139 check_expand (op
, len
);
4140 bcopy (buf
, (char *) op
->bufp
, len
);
4145 /* Call the appropriate directive handler. buf now points to
4146 either the appropriate place in the input buffer, or to
4147 the temp buffer if it was necessary to make one. cp
4148 points to the first char after the contents of the (possibly
4149 copied) directive, in either case. */
4150 (*kt
->func
) (buf
, cp
, op
, kt
);
4151 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
4157 /* It is deliberate that we don't warn about undefined directives.
4158 That is the responsibility of cc1. */
4165 static struct tm tmbuf
;
4166 if (! tmbuf
.tm_mday
) {
4167 time_t t
= time ((time_t *) 0);
4168 struct tm
*tm
= localtime (&t
);
4172 /* Use 0000-01-01 00:00:00 if local time is not available. */
4173 tmbuf
.tm_year
= -1900;
4180 static const char * const monthnames
[] = {
4181 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4182 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4186 * expand things like __FILE__. Place the expansion into the output
4187 * buffer *without* rescanning.
4191 special_symbol (hp
, op
)
4198 FILE_BUF
*ip
= NULL
;
4201 int paren
= 0; /* For special `defined' keyword */
4203 if (pcp_outfile
&& pcp_inside_if
4204 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4205 error ("Predefined macro `%s' used inside `#if' during precompilation",
4208 for (i
= indepth
; i
>= 0; i
--)
4209 if (instack
[i
].fname
!= NULL
) {
4214 error ("cccp error: not in any file?!");
4215 return; /* the show must go on */
4222 FILE_BUF
*p
= hp
->type
== T_FILE
? ip
: &instack
[0];
4223 const char *string
= p
->nominal_fname
;
4227 size_t string_len
= p
->nominal_fname_len
;
4228 char *newbuf
= (char *) alloca (3 + 4 * string_len
);
4229 quote_string (newbuf
, string
, string_len
);
4238 case T_INCLUDE_LEVEL
:
4240 /* Eight bytes ought to be more than enough */
4241 char *newbuf
= (char *) alloca (8);
4243 for (i
= indepth
; i
>= 0; i
--)
4244 if (instack
[i
].fname
!= NULL
)
4246 sprintf (newbuf
, "%d", true_indepth
- 1);
4253 char *newbuf
= (char *) alloca (3 + strlen (version_string
));
4254 sprintf (newbuf
, "\"%s\"", version_string
);
4259 #ifndef NO_BUILTIN_SIZE_TYPE
4265 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4266 case T_PTRDIFF_TYPE
:
4271 #ifndef NO_BUILTIN_WCHAR_TYPE
4277 case T_USER_LABEL_PREFIX_TYPE
:
4278 buf
= user_label_prefix
;
4281 case T_REGISTER_PREFIX_TYPE
:
4282 buf
= REGISTER_PREFIX
;
4285 case T_IMMEDIATE_PREFIX_TYPE
:
4286 buf
= IMMEDIATE_PREFIX
;
4290 buf
= hp
->value
.cpval
;
4291 #ifdef STDC_0_IN_SYSTEM_HEADERS
4292 if (ip
->system_header_p
4293 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
4294 && !lookup ((const U_CHAR
*) "__STRICT_ANSI__", -1, -1))
4297 if (pcp_inside_if
&& pcp_outfile
)
4298 /* Output a precondition for this macro use */
4299 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4304 char *newbuf
= (char *) alloca (10);
4305 sprintf (newbuf
, "%d", ip
->lineno
);
4313 char *newbuf
= (char *) alloca (20);
4314 timebuf
= timestamp ();
4315 if (hp
->type
== T_DATE
)
4316 sprintf (newbuf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4317 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4319 sprintf (newbuf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
,
4320 timebuf
->tm_min
, timebuf
->tm_sec
);
4325 case T_SPEC_DEFINED
:
4326 buf
= " 0 "; /* Assume symbol is not defined */
4327 ip
= &instack
[indepth
];
4328 SKIP_WHITE_SPACE (ip
->bufp
);
4329 if (*ip
->bufp
== '(') {
4331 ip
->bufp
++; /* Skip over the paren */
4332 SKIP_WHITE_SPACE (ip
->bufp
);
4335 if (!is_idstart
[*ip
->bufp
])
4337 if (ip
->bufp
[0] == 'L' && (ip
->bufp
[1] == '\'' || ip
->bufp
[1] == '"'))
4339 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4340 if (pcp_outfile
&& pcp_inside_if
4341 && (hp
->type
== T_CONST
4342 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4343 /* Output a precondition for this macro use. */
4344 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4345 if (hp
->type
== T_POISON
) {
4346 error("attempt to use poisoned `%s'.", hp
->name
);
4353 if (pcp_outfile
&& pcp_inside_if
) {
4354 /* Output a precondition for this macro use */
4355 U_CHAR
*cp
= ip
->bufp
;
4356 fprintf (pcp_outfile
, "#undef ");
4357 while (is_idchar
[*cp
]) /* Ick! */
4358 fputc (*cp
++, pcp_outfile
);
4359 putc ('\n', pcp_outfile
);
4361 while (is_idchar
[*ip
->bufp
])
4363 SKIP_WHITE_SPACE (ip
->bufp
);
4365 if (*ip
->bufp
!= ')')
4372 error("attempt to use poisoned `%s'.", hp
->name
);
4373 buf
= " 0 "; /* Consider poisoned symbol to not be defined */
4378 error ("`defined' without an identifier");
4382 error ("cccp error: invalid special hash type"); /* time for gdb */
4386 check_expand (op
, len
);
4387 bcopy (buf
, (char *) op
->bufp
, len
);
4394 /* Routines to handle #directives */
4396 /* Handle #include and #import.
4397 This function expects to see "fname" or <fname> on the input. */
4400 do_include (buf
, limit
, op
, keyword
)
4401 U_CHAR
*buf
, *limit
;
4403 struct directive
*keyword
;
4405 const U_CHAR
*importing
=
4406 keyword
->type
== T_IMPORT
? (const U_CHAR
*) "" : (const U_CHAR
*) 0;
4407 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4408 static int import_warning
= 0;
4409 char *fname
; /* Dynamically allocated fname buffer */
4412 char *fbeg
, *fend
; /* Beginning and end of fname */
4415 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4416 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4417 struct file_name_list
*searchptr
= 0;
4420 int f
= -3; /* file number */
4421 struct include_file
*inc
= 0;
4423 int retried
= 0; /* Have already tried macro
4424 expanding the include line*/
4425 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4427 int vaxc_include
= 0; /* 1 for token without punctuation */
4431 const char *pcfbuflimit
;
4434 if (pedantic
&& !instack
[indepth
].system_header_p
)
4437 pedwarn ("ANSI C does not allow `#import'");
4439 pedwarn ("ANSI C does not allow `#include_next'");
4442 if (importing
&& warn_import
&& !inhibit_warnings
4443 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4445 warning ("using `#import' is not recommended");
4446 notice ("The fact that a certain header file need not be processed more than once\n\
4447 should be indicated in the header file, not where it is used.\n\
4448 The best way to do this is with a conditional of this form:\n\
4450 #ifndef _FOO_H_INCLUDED\n\
4451 #define _FOO_H_INCLUDED\n\
4452 ... <real contents of file> ...\n\
4453 #endif /* Not _FOO_H_INCLUDED */\n\
4455 Then users can use `#include' any number of times.\n\
4456 GNU C automatically avoids processing the file more than once\n\
4457 when it is equipped with such a conditional.\n");
4463 SKIP_WHITE_SPACE (fin
);
4464 /* Discard trailing whitespace so we can easily see
4465 if we have parsed all the significant chars we were given. */
4466 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4467 fbeg
= fend
= (char *) alloca (limit
- fin
);
4473 /* Copy the operand text, concatenating the strings. */
4478 goto invalid_include_file_name
;
4486 /* If not at the end, there had better be another string. */
4487 /* Skip just horiz space, and don't go past limit. */
4488 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4489 if (fin
!= limit
&& *fin
== '\"')
4496 /* We have "filename". Figure out directory this source
4497 file is coming from and put it on the front of the list. */
4499 /* If -I- was specified, don't search current dir, only spec'd ones. */
4500 if (ignore_srcdir
) break;
4502 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4506 if ((fp
->nominal_fname
) != NULL
) {
4508 /* Found a named file. Figure out dir of the file,
4509 and put it in front of the search list. */
4510 dsp
= ((struct file_name_list
*)
4511 alloca (sizeof (struct file_name_list
)
4512 + fp
->nominal_fname_len
));
4513 strcpy (dsp
->fname
, fp
->nominal_fname
);
4514 simplify_filename (dsp
->fname
);
4515 nam
= base_name (dsp
->fname
);
4518 /* for hack_vms_include_specification(), a local
4519 dir specification must start with "./" on VMS. */
4520 if (nam
== dsp
->fname
)
4527 /* But for efficiency's sake, do not insert the dir
4528 if it matches the search list's first dir. */
4529 dsp
->next
= search_start
;
4530 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4532 n
= nam
- dsp
->fname
;
4533 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4534 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4536 dsp
[0].got_name_map
= 0;
4544 while (fin
!= limit
&& *fin
!= '>')
4546 if (*fin
== '>' && fin
+ 1 == limit
) {
4548 /* If -I-, start with the first -I dir after the -I-. */
4549 search_start
= first_bracket_include
;
4557 * Support '#include xyz' like VAX-C to allow for easy use of all the
4558 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4559 * code from case '<' is repeated here) and generates a warning.
4560 * (Note: macro expansion of `xyz' takes precedence.)
4562 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4563 the pre-decrement outside of the macro. */
4564 if (retried
&& (--fin
, ISALPHA(*(U_CHAR
*) (fin
)))) {
4565 while (fin
!= limit
&& (!ISSPACE(*fin
)))
4567 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4571 /* If -I-, start with the first -I dir after the -I-. */
4572 search_start
= first_bracket_include
;
4580 /* Expand buffer and then remove any newline markers.
4581 We can't just tell expand_to_temp_buffer to omit the markers,
4582 since it would put extra spaces in include file names. */
4584 int errors_before_expansion
= errors
;
4587 trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4588 if (errors
!= errors_before_expansion
) {
4590 goto invalid_include_file_name
;
4593 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4595 while (src
!= trybuf
.bufp
) {
4596 switch ((*limit
++ = *src
++)) {
4605 const U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4606 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4619 invalid_include_file_name
:
4620 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4624 /* For #include_next, skip in the search path
4625 past the dir in which the containing file was found. */
4628 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4629 if (fp
->fname
!= NULL
) {
4630 /* fp->dir is null if the containing file was specified
4631 with an absolute file name. In that case, don't skip anything. */
4633 search_start
= fp
->dir
->next
;
4639 flen
= simplify_filename (fbeg
);
4643 error ("empty file name in `#%s'", keyword
->name
);
4647 /* Allocate this permanently, because it gets stored in the definitions
4649 fname
= xmalloc (max_include_len
+ flen
+ 1);
4650 /* + 1 above for terminating null. */
4652 system_include_depth
+= angle_brackets
;
4654 /* If specified file name is absolute, just open it. */
4656 if (absolute_filename (fbeg
)) {
4657 strcpy (fname
, fbeg
);
4658 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4662 struct bypass_dir
*next
;
4664 struct file_name_list
*searchptr
;
4665 } **bypass_slot
= 0;
4667 /* Search directory path, trying to open the file.
4668 Copy each filename tried into FNAME. */
4670 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4672 if (searchptr
== first_bracket_include
) {
4673 /* Go to bypass directory if we know we've seen this file before. */
4674 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4675 struct bypass_dir
*p
;
4676 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4678 for (p
= *bypass_slot
; p
; p
= p
->next
)
4679 if (!strcmp (fbeg
, p
->fname
)) {
4680 searchptr
= p
->searchptr
;
4687 /* Change this 1/2 Unix 1/2 VMS file specification into a
4688 full VMS file specification */
4689 if (searchptr
->fname
[0])
4691 strcpy (fname
, searchptr
->fname
);
4692 if (fname
[strlen (fname
) - 1] == ':')
4695 slashp
= strchr (fbeg
, '/');
4697 /* start at root-dir of logical device if no path given. */
4699 strcat (fname
, "[000000]");
4701 strcat (fname
, fbeg
);
4703 /* Fix up the filename */
4704 hack_vms_include_specification (fname
, vaxc_include
);
4708 /* This is a normal VMS filespec, so use it unchanged. */
4709 strcpy (fname
, fbeg
);
4710 /* if it's '#include filename', add the missing .h */
4711 if (vaxc_include
&& index(fname
,'.')==NULL
)
4712 strcat (fname
, ".h");
4715 strcpy (fname
, searchptr
->fname
);
4716 strcat (fname
, fbeg
);
4718 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4720 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4721 /* This is the first time we found this include file,
4722 and we found it after first_bracket_include.
4723 Record its location so that we can bypass to here next time. */
4724 struct bypass_dir
*p
4725 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4726 p
->next
= *bypass_slot
;
4727 p
->fname
= fname
+ strlen (searchptr
->fname
);
4728 p
->searchptr
= searchptr
;
4734 /* Our VMS hacks can produce invalid filespecs, so don't worry
4735 about errors other than EACCES. */
4736 if (errno
== EACCES
)
4739 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
4749 /* The file was already included. */
4751 /* If generating dependencies and -MG was specified, we assume missing
4752 files are leaf files, living in the same directory as the source file
4753 or other similar place; these missing files may be generated from
4754 other files and may not exist yet (eg: y.tab.h). */
4755 } else if (print_deps_missing_files
4756 && (system_include_depth
!= 0) < print_deps
)
4758 /* If it was requested as a system header file,
4759 then assume it belongs in the first place to look for such. */
4763 char *p
= (char *) alloca (strlen (search_start
->fname
)
4764 + strlen (fbeg
) + 1);
4765 strcpy (p
, search_start
->fname
);
4767 deps_output (p
, ' ');
4772 /* Otherwise, omit the directory, as if the file existed
4773 in the directory with the source. */
4774 deps_output (fbeg
, ' ');
4777 /* If -M was specified, and this header file won't be added to the
4778 dependency list, then don't count this as an error, because we can
4779 still produce correct output. Otherwise, we can't produce correct
4780 output, because there may be dependencies we need inside the missing
4781 file, and we don't know what directory this missing file exists in. */
4782 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4783 warning ("No include path in which to find %s", fbeg
);
4785 error_from_errno (fbeg
);
4787 error ("No include path in which to find %s", fbeg
);
4791 /* Actually process the file. */
4793 pcftry
= (char *) alloca (strlen (fname
) + 30);
4800 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4802 pcf
= open (pcftry
, O_RDONLY
, 0666);
4807 if (fstat (pcf
, &s
) != 0)
4808 pfatal_with_name (pcftry
);
4809 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4810 || inc
->st
.st_dev
!= s
.st_dev
)
4812 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4813 /* Don't need it any more. */
4818 /* Don't need it at all. */
4823 } while (pcf
!= -1 && !pcfbuf
);
4826 /* Actually process the file */
4828 pcfname
= xstrdup (pcftry
);
4829 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) fname
, op
);
4832 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4835 system_include_depth
-= angle_brackets
;
4840 /* Return nonzero if the given FILENAME is an absolute pathname which
4841 designates a file within one of the known "system" include file
4842 directories. We assume here that if the given FILENAME looks like
4843 it is the name of a file which resides either directly in a "system"
4844 include file directory, or within any subdirectory thereof, then the
4845 given file must be a "system" include file. This function tells us
4846 if we should suppress pedantic errors/warnings for the given FILENAME.
4848 The value is 2 if the file is a C-language system header file
4849 for which C++ should (on most systems) assume `extern "C"'. */
4852 is_system_include (filename
)
4853 register const char *filename
;
4855 struct file_name_list
*searchptr
;
4857 for (searchptr
= first_system_include
; searchptr
;
4858 searchptr
= searchptr
->next
)
4859 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4860 return searchptr
->c_system_include_path
+ 1;
4864 /* Yield the non-directory suffix of a file name. */
4870 const char *s
= fname
;
4872 #if defined (__MSDOS__) || defined (_WIN32)
4873 if (ISALPHA (s
[0]) && s
[1] == ':') s
+= 2;
4876 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4877 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4878 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4882 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4883 #ifdef DIR_SEPARATOR
4884 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4889 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4892 absolute_filename (filename
)
4893 const char *filename
;
4895 #if defined (__MSDOS__) \
4896 || (defined (_WIN32) && !defined (__CYGWIN__) && !defined (_UWIN))
4897 if (ISALPHA (filename
[0]) && filename
[1] == ':') filename
+= 2;
4899 #if defined (__CYGWIN__)
4900 /* At present, any path that begins with a drive spec is absolute. */
4901 if (ISALPHA (filename
[0]) && filename
[1] == ':') return 1;
4904 if (index (filename
, ':') != 0) return 1;
4906 if (filename
[0] == '/') return 1;
4907 #ifdef DIR_SEPARATOR
4908 if (filename
[0] == DIR_SEPARATOR
) return 1;
4913 /* Returns whether or not a given character is a directory separator.
4914 Used by simplify_filename. */
4915 static inline int is_dir_separator
PARAMS ((int));
4919 is_dir_separator(ch
)
4922 return (ch
== DIR_SEPARATOR
)
4923 #if defined (DIR_SEPARATOR_2)
4924 || (ch
== DIR_SEPARATOR_2
)
4929 /* Remove unnecessary characters from FILENAME in place,
4930 to avoid unnecessary filename aliasing.
4931 Return the length of the resulting string.
4933 Do only the simplifications allowed by Posix.
4934 It is OK to miss simplifications on non-Posix hosts,
4935 since this merely leads to suboptimal results. */
4938 simplify_filename (filename
)
4941 register char *from
= filename
;
4942 register char *to
= filename
;
4945 /* Remove redundant initial /s. */
4946 if (is_dir_separator (*from
))
4948 *to
++ = DIR_SEPARATOR
;
4949 if (is_dir_separator (*++from
))
4951 if (is_dir_separator (*++from
))
4953 /* 3 or more initial /s are equivalent to 1 /. */
4954 while (is_dir_separator (*++from
))
4959 /* On some hosts // differs from /; Posix allows this. */
4960 *to
++ = DIR_SEPARATOR
;
4970 if (from
[0] == '.' && from
[1] == '/')
4975 /* Copy this component and trailing DIR_SEPARATOR, if any. */
4976 while (!is_dir_separator (*to
++ = *from
++))
4980 /* Trim . component at end of nonempty name. */
4981 to
-= filename
<= to
- 3 && to
[-3] == DIR_SEPARATOR
&& to
[-2] == '.';
4983 /* Trim unnecessary trailing /s. */
4984 while (to0
< --to
&& to
[-1] == DIR_SEPARATOR
)
4988 return to
- filename
;
4991 #if defined(DIR_SEPARATOR_2)
4992 /* Simplify to one directory separator. */
4993 to
[-1] = DIR_SEPARATOR
;
4997 /* Skip /s after a /. */
4998 while (is_dir_separator (*from
))
5003 /* The file_name_map structure holds a mapping of file names for a
5004 particular directory. This mapping is read from the file named
5005 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5006 map filenames on a file system with severe filename restrictions,
5007 such as DOS. The format of the file name map file is just a series
5008 of lines with two tokens on each line. The first token is the name
5009 to map, and the second token is the actual name to use. */
5011 struct file_name_map
5013 struct file_name_map
*map_next
;
5018 #define FILE_NAME_MAP_FILE "header.gcc"
5020 /* Read a space delimited string of unlimited length from a stdio
5024 read_filename_string (ch
, f
)
5032 set
= alloc
= xmalloc (len
+ 1);
5036 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5038 if (set
- alloc
== len
)
5041 alloc
= xrealloc (alloc
, len
+ 1);
5042 set
= alloc
+ len
/ 2;
5052 /* Read the file name map file for DIRNAME.
5053 If DIRNAME is empty, read the map file for the working directory;
5054 otherwise DIRNAME must end in '/'. */
5056 static struct file_name_map
*
5057 read_name_map (dirname
)
5058 const char *dirname
;
5060 /* This structure holds a linked list of file name maps, one per
5062 struct file_name_map_list
5064 struct file_name_map_list
*map_list_next
;
5065 char *map_list_name
;
5066 struct file_name_map
*map_list_map
;
5068 static struct file_name_map_list
*map_list
;
5069 register struct file_name_map_list
*map_list_ptr
;
5074 for (map_list_ptr
= map_list
; map_list_ptr
;
5075 map_list_ptr
= map_list_ptr
->map_list_next
)
5076 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5077 return map_list_ptr
->map_list_map
;
5079 map_list_ptr
= ((struct file_name_map_list
*)
5080 xmalloc (sizeof (struct file_name_map_list
)));
5081 map_list_ptr
->map_list_name
= xstrdup (dirname
);
5082 map_list_ptr
->map_list_map
= NULL
;
5084 dirlen
= strlen (dirname
);
5085 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
5086 strcpy (name
, dirname
);
5087 strcat (name
, FILE_NAME_MAP_FILE
);
5088 f
= fopen (name
, "r");
5090 map_list_ptr
->map_list_map
= NULL
;
5095 while ((ch
= getc (f
)) != EOF
)
5098 struct file_name_map
*ptr
;
5103 from
= read_filename_string (ch
, f
);
5104 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5106 to
= read_filename_string (ch
, f
);
5108 simplify_filename (from
);
5109 tolen
= simplify_filename (to
);
5111 ptr
= ((struct file_name_map
*)
5112 xmalloc (sizeof (struct file_name_map
)));
5113 ptr
->map_from
= from
;
5115 /* Make the real filename absolute. */
5116 if (absolute_filename (to
))
5120 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
5121 strcpy (ptr
->map_to
, dirname
);
5122 strcat (ptr
->map_to
, to
);
5126 ptr
->map_next
= map_list_ptr
->map_list_map
;
5127 map_list_ptr
->map_list_map
= ptr
;
5129 while ((ch
= getc (f
)) != '\n')
5136 map_list_ptr
->map_list_next
= map_list
;
5137 map_list
= map_list_ptr
;
5139 return map_list_ptr
->map_list_map
;
5142 /* Try to open include file FILENAME. SEARCHPTR is the directory
5143 being tried from the include file search path.
5144 IMPORTING is "" if we are importing, null otherwise.
5145 Return -2 if found, either a matching name or a matching inode.
5146 Otherwise, open the file and return a file descriptor if successful
5147 or -1 if unsuccessful.
5148 Unless unsuccessful, put a descriptor of the included file into *PINC.
5149 This function maps filenames on file systems based on information read by
5153 open_include_file (filename
, searchptr
, importing
, pinc
)
5155 struct file_name_list
*searchptr
;
5156 const U_CHAR
*importing
;
5157 struct include_file
**pinc
;
5159 char *fname
= remap
? remap_include_file (filename
, searchptr
) : filename
;
5162 /* Look up FNAME in include_hashtab. */
5163 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
5166 struct include_file
*inc
, *head
= *phead
;
5167 for (inc
= head
; inc
; inc
= inc
->next
)
5168 if (!strcmp (fname
, inc
->fname
))
5172 || ! inc
->control_macro
5173 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
5175 fd
= open (fname
, O_RDONLY
, 0);
5180 /* if #include <dir/file> fails, try again with hacked spec. */
5181 if (!hack_vms_include_specification (fname
, 0))
5183 fd
= open (fname
, O_RDONLY
, 0);
5190 /* FNAME was not in include_hashtab; insert a new entry. */
5191 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
5194 inc
->control_macro
= 0;
5195 inc
->deps_output
= 0;
5196 if (fstat (fd
, &inc
->st
) != 0)
5197 pfatal_with_name (fname
);
5200 /* Look for another file with the same inode and device. */
5201 if (lookup_ino_include (inc
)
5202 && inc
->control_macro
5203 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
5209 /* For -M, add this file to the dependencies. */
5210 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
5211 inc
->deps_output
= 1;
5212 deps_output (fname
, ' ');
5215 /* Handle -H option. */
5216 if (print_include_names
)
5217 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
5221 inc
->control_macro
= importing
;
5227 /* Return the remapped name of the include file FILENAME.
5228 SEARCHPTR is the directory being tried from the include file path. */
5231 remap_include_file (filename
, searchptr
)
5233 struct file_name_list
*searchptr
;
5235 register struct file_name_map
*map
;
5236 register const char *from
;
5240 if (! searchptr
->got_name_map
)
5242 searchptr
->name_map
= read_name_map (searchptr
->fname
);
5243 searchptr
->got_name_map
= 1;
5246 /* Check the mapping for the directory we are using. */
5247 from
= filename
+ strlen (searchptr
->fname
);
5248 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5249 if (! strcmp (map
->map_from
, from
))
5253 from
= base_name (filename
);
5255 if (from
!= filename
|| !searchptr
)
5257 /* Try to find a mapping file for the particular directory we are
5258 looking in. Thus #include <sys/types.h> will look up sys/types.h
5259 in /usr/include/header.gcc and look up types.h in
5260 /usr/include/sys/header.gcc. */
5262 char *dir
= (char *) alloca (from
- filename
+ 1);
5263 bcopy (filename
, dir
, from
- filename
);
5264 dir
[from
- filename
] = '\0';
5266 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
5267 if (! strcmp (map
->map_from
, from
))
5274 /* Insert INC into the include file table, hashed by device and inode number.
5275 If a file with different name but same dev+ino was already in the table,
5276 return 1 and set INC's control macro to the already-known macro. */
5279 lookup_ino_include (inc
)
5280 struct include_file
*inc
;
5282 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
5283 % INCLUDE_HASHSIZE
);
5284 struct include_file
*i
= include_ino_hashtab
[hash
];
5286 include_ino_hashtab
[hash
] = inc
;
5288 for (; i
; i
= i
->next_ino
)
5289 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
5290 && inc
->st
.st_dev
== i
->st
.st_dev
) {
5291 inc
->control_macro
= i
->control_macro
;
5298 /* Process file descriptor F, which corresponds to include file INC,
5300 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5301 "system" include directories (as decided by the `is_system_include'
5303 DIRPTR is the link in the dir path through which this file was found,
5304 or 0 if the file name was absolute. */
5307 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5309 struct include_file
*inc
;
5311 int system_header_p
;
5312 struct file_name_list
*dirptr
;
5314 char *fname
= inc
->fname
;
5316 FILE_BUF
*fp
; /* For input stack frame */
5317 int missing_newline
= 0;
5319 CHECK_DEPTH (return;);
5321 fp
= &instack
[indepth
+ 1];
5322 bzero ((char *) fp
, sizeof (FILE_BUF
));
5323 fp
->nominal_fname
= fp
->fname
= fname
;
5324 fp
->nominal_fname_len
= strlen (fname
);
5328 fp
->if_stack
= if_stack
;
5329 fp
->system_header_p
= system_header_p
;
5332 if (S_ISREG (inc
->st
.st_mode
)) {
5333 size_t s
= (size_t) inc
->st
.st_size
;
5334 if (s
!= inc
->st
.st_size
|| s
+ 2 < s
)
5336 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
5339 /* Read the file contents, knowing that s is an upper bound
5340 on the number of bytes we can read. */
5341 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
5342 if (fp
->length
< 0) goto nope
;
5344 else if (S_ISDIR (inc
->st
.st_mode
)) {
5345 error ("directory `%s' specified in #include", fname
);
5349 /* Cannot count its file size before reading.
5350 First read the entire file into heap and
5351 copy them into buffer on stack. */
5356 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5359 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5361 goto nope
; /* error! */
5363 if (st_size
!= bsize
)
5364 break; /* End of file */
5366 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5369 fp
->length
= st_size
;
5372 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5373 /* Backslash-newline at end is not good enough. */
5374 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5375 fp
->buf
[fp
->length
++] = '\n';
5376 missing_newline
= 1;
5378 fp
->buf
[fp
->length
] = '\0';
5380 /* Close descriptor now, so nesting does not use lots of descriptors. */
5383 /* Must do this before calling trigraph_pcp, so that the correct file name
5384 will be printed in warning messages. */
5387 input_file_stack_tick
++;
5392 if (warn_white_space
)
5393 check_white_space (fp
);
5395 output_line_directive (fp
, op
, 0, enter_file
);
5398 if (missing_newline
)
5401 if (pedantic
&& missing_newline
)
5402 pedwarn ("file does not end in newline");
5405 input_file_stack_tick
++;
5406 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5412 perror_with_name (fname
);
5417 /* Record that inclusion of the include file INC
5418 should be controlled by the macro named MACRO_NAME.
5419 This means that trying to include the file again
5420 will do something if that macro is defined. */
5423 record_control_macro (inc
, macro_name
)
5424 struct include_file
*inc
;
5425 const U_CHAR
*macro_name
;
5427 if (!inc
->control_macro
|| inc
->control_macro
[0])
5428 inc
->control_macro
= macro_name
;
5431 /* Load the specified precompiled header into core, and verify its
5432 preconditions. PCF indicates the file descriptor to read, which must
5433 be a regular file. *ST is its file status.
5434 FNAME indicates the file name of the original header.
5435 *LIMIT will be set to an address one past the end of the file.
5436 If the preconditions of the file are not satisfied, the buffer is
5437 freed and we return 0. If the preconditions are satisfied, return
5438 the address of the buffer following the preconditions. The buffer, in
5439 this case, should never be freed because various pieces of it will
5440 be referred to until all precompiled strings are output at the end of
5444 check_precompiled (pcf
, st
, fname
, limit
)
5447 const char *fname ATTRIBUTE_UNUSED
;
5457 if (S_ISREG (st
->st_mode
))
5459 size_t s
= (size_t) st
->st_size
;
5460 if (s
!= st
->st_size
|| s
+ 2 < s
)
5462 buf
= xmalloc (s
+ 2);
5463 length
= safe_read (pcf
, buf
, s
);
5470 if (length
> 0 && buf
[length
-1] != '\n')
5471 buf
[length
++] = '\n';
5474 *limit
= buf
+ length
;
5476 /* File is in core. Check the preconditions. */
5477 if (!check_preconditions (buf
))
5479 for (cp
= buf
; *cp
; cp
++)
5482 fprintf (stderr
, "Using preinclude %s\n", fname
);
5488 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5494 /* PREC (null terminated) points to the preconditions of a
5495 precompiled header. These are a series of #define and #undef
5496 lines which must match the current contents of the hash
5500 check_preconditions (prec
)
5504 const char *lineend
;
5507 lineend
= index (prec
, '\n');
5509 if (*prec
++ != '#') {
5510 error ("Bad format encountered while reading precompiled file");
5513 if (!strncmp (prec
, "define", 6)) {
5517 mdef
= create_definition ((const U_CHAR
*) prec
,
5518 (const U_CHAR
*) lineend
, NULL_PTR
);
5523 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5524 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5525 || (hp
->type
== T_MACRO
5526 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5527 && (mdef
.defn
->length
!= 2
5528 || mdef
.defn
->expansion
[0] != '\n'
5529 || mdef
.defn
->expansion
[1] != ' ')))
5531 } else if (!strncmp (prec
, "undef", 5)) {
5536 while (is_hor_space
[(U_CHAR
) *prec
])
5539 while (is_idchar
[(U_CHAR
) *prec
])
5543 if (lookup ((const U_CHAR
*) name
, len
, -1))
5546 error ("Bad format encountered while reading precompiled file");
5551 /* They all passed successfully */
5555 /* Process the main body of a precompiled file. BUF points to the
5556 string section of the file, following the preconditions. LIMIT is one
5557 character past the end. NAME is the name of the file being read
5558 in. OP is the main output buffer. */
5561 pcfinclude (buf
, name
, op
)
5570 /* First in the file comes 4 bytes indicating the number of strings, */
5571 /* in network byte order. (MSB first). */
5573 nstrings
= (nstrings
<< 8) | *cp
++;
5574 nstrings
= (nstrings
<< 8) | *cp
++;
5575 nstrings
= (nstrings
<< 8) | *cp
++;
5577 /* Looping over each string... */
5578 while (nstrings
--) {
5579 U_CHAR
*string_start
;
5580 U_CHAR
*endofthiskey
;
5584 /* Each string starts with a STRINGDEF structure (str), followed */
5585 /* by the text of the string (string_start) */
5587 /* First skip to a longword boundary */
5588 /* ??? Why a 4-byte boundary? On all machines? */
5589 /* NOTE: This works correctly even if size_t
5590 is narrower than a pointer.
5591 Do not try risky measures here to get another type to use!
5592 Do not include stddef.h--it will fail! */
5593 if ((size_t) cp
& 3)
5594 cp
+= 4 - ((size_t) cp
& 3);
5596 /* Now get the string. */
5597 str
= (STRINGDEF
*) (PTR
) cp
;
5598 string_start
= cp
+= sizeof (STRINGDEF
);
5600 for (; *cp
; cp
++) /* skip the string */
5603 /* We need to macro expand the string here to ensure that the
5604 proper definition environment is in place. If it were only
5605 expanded when we find out it is needed, macros necessary for
5606 its proper expansion might have had their definitions changed. */
5607 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5608 /* Lineno is already set in the precompiled file */
5609 str
->contents
= tmpbuf
.buf
;
5610 str
->len
= tmpbuf
.bufp
- tmpbuf
.buf
;
5612 str
->filename
= name
;
5613 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5616 *stringlist_tailp
= str
;
5617 stringlist_tailp
= &str
->chain
;
5619 /* Next comes a fourbyte number indicating the number of keys
5622 nkeys
= (nkeys
<< 8) | *cp
++;
5623 nkeys
= (nkeys
<< 8) | *cp
++;
5624 nkeys
= (nkeys
<< 8) | *cp
++;
5626 /* If this number is -1, then the string is mandatory. */
5630 /* Otherwise, for each key, */
5631 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5632 KEYDEF
*kp
= (KEYDEF
*) (PTR
) cp
;
5636 /* It starts with a KEYDEF structure */
5637 cp
+= sizeof (KEYDEF
);
5639 /* Find the end of the key. At the end of this for loop we
5640 advance CP to the start of the next key using this variable. */
5641 endofthiskey
= cp
+ strlen ((char *) cp
);
5644 /* Expand the key, and enter it into the hash table. */
5645 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5648 while (is_hor_space
[*bp
])
5650 if (!is_idstart
[*bp
] || bp
== tmpbuf
.bufp
) {
5655 hp
= lookup (bp
, -1, -1);
5658 install (bp
, -1, T_PCSTRING
, (char *) kp
, -1);
5660 else if (hp
->type
== T_PCSTRING
) {
5661 kp
->chain
= hp
->value
.keydef
;
5662 hp
->value
.keydef
= kp
;
5668 /* This output_line_directive serves to switch us back to the current
5669 input file in case some of these strings get output (which will
5670 result in line directives for the header file being output). */
5671 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5674 /* Called from rescan when it hits a key for strings. Mark them all
5675 used and clean up. */
5683 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5684 kp
->str
->writeflag
= 1;
5688 /* Write the output, interspersing precompiled strings in their
5689 appropriate places. */
5694 STRINGDEF
*next_string
;
5695 U_CHAR
*cur_buf_loc
;
5696 int line_directive_len
= 80;
5697 char *line_directive
= xmalloc (line_directive_len
);
5700 /* In each run through the loop, either cur_buf_loc ==
5701 next_string_loc, in which case we print a series of strings, or
5702 it is less than next_string_loc, in which case we write some of
5704 cur_buf_loc
= outbuf
.buf
;
5705 next_string
= stringlist
;
5707 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5709 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5710 if (next_string
->writeflag
) {
5711 len
= 4 * strlen ((const char *) next_string
->filename
) + 32;
5712 while (len
> line_directive_len
)
5713 line_directive
= xrealloc (line_directive
,
5714 line_directive_len
*= 2);
5715 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5716 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5717 (const char *) next_string
->filename
,
5718 strlen ((const char *) next_string
->filename
)),
5720 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5721 safe_write (fileno (stdout
),
5722 (char *) next_string
->contents
, next_string
->len
);
5724 next_string
= next_string
->chain
;
5728 ? (next_string
->output_mark
5729 - (cur_buf_loc
- outbuf
.buf
))
5730 : outbuf
.bufp
- cur_buf_loc
);
5732 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5736 free (line_directive
);
5739 /* Pass a directive through to the output file.
5740 BUF points to the contents of the directive, as a contiguous string.
5741 LIMIT points to the first character past the end of the directive.
5742 KEYWORD is the keyword-table entry for the directive. */
5745 pass_thru_directive (buf
, limit
, op
, keyword
)
5747 const U_CHAR
*limit
;
5749 struct directive
*keyword
;
5751 register int keyword_length
= keyword
->length
;
5753 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5755 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5756 op
->bufp
+= keyword_length
;
5757 if (limit
!= buf
&& buf
[0] != ' ')
5759 bcopy ((const PTR
) buf
, (PTR
) op
->bufp
, limit
- buf
);
5760 op
->bufp
+= (limit
- buf
);
5763 /* Count the line we have just made in the output,
5764 to get in sync properly. */
5769 /* The arglist structure is built by do_define to tell
5770 collect_definition where the argument names begin. That
5771 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5772 would contain pointers to the strings x, y, and z.
5773 Collect_definition would then build a DEFINITION node,
5774 with reflist nodes pointing to the places x, y, and z had
5775 appeared. So the arglist is just convenience data passed
5776 between these two routines. It is not kept around after
5777 the current #define has been processed and entered into the
5781 struct arglist
*next
;
5788 /* Create a DEFINITION node from a #define directive. Arguments are
5789 as for do_define. */
5792 create_definition (buf
, limit
, op
)
5793 const U_CHAR
*buf
, *limit
;
5796 const U_CHAR
*bp
; /* temp ptr into input buffer */
5797 const U_CHAR
*symname
; /* remember where symbol name starts */
5798 int sym_length
; /* and how long it is */
5799 int line
= instack
[indepth
].lineno
;
5800 const char *file
= instack
[indepth
].nominal_fname
;
5801 size_t file_len
= instack
[indepth
].nominal_fname_len
;
5805 int arglengths
= 0; /* Accumulate lengths of arg names
5806 plus number of args. */
5811 while (is_hor_space
[*bp
])
5814 symname
= bp
; /* remember where it starts */
5815 sym_length
= check_macro_name (bp
, 0);
5818 /* Lossage will occur if identifiers or control keywords are broken
5819 across lines using backslash. This is not the right place to take
5823 struct arglist
*arg_ptrs
= NULL
;
5826 bp
++; /* skip '(' */
5827 SKIP_WHITE_SPACE (bp
);
5829 /* Loop over macro argument names. */
5830 while (*bp
!= ')') {
5831 struct arglist
*temp
;
5833 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5835 temp
->next
= arg_ptrs
;
5836 temp
->argno
= argno
++;
5837 temp
->rest_args
= 0;
5841 pedwarn ("another parameter follows `%s'",
5844 if (!is_idstart
[*bp
])
5846 if (c99
&& limit
- bp
> (long) REST_EXTENSION_LENGTH
5847 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0)
5849 /* This is the ISO C 99 way to write macros with variable
5850 number of arguments. */
5852 temp
->rest_args
= 1;
5855 pedwarn ("invalid character in macro parameter name");
5858 /* Find the end of the arg name. */
5859 while (is_idchar
[*bp
]) {
5861 /* do we have a "special" rest-args extension here? */
5862 if (limit
- bp
> (long) REST_EXTENSION_LENGTH
5863 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5864 if (pedantic
&& !instack
[indepth
].system_header_p
)
5865 pedwarn ("ANSI C does not allow macro with variable arguments");
5867 temp
->rest_args
= 1;
5871 if (bp
== temp
->name
&& rest_args
== 1)
5873 /* This is the ISO C 99 style. */
5874 temp
->name
= (U_CHAR
*) va_args_name
;
5875 temp
->length
= VA_ARGS_NAME_LENGTH
;
5878 temp
->length
= bp
- temp
->name
;
5880 bp
+= REST_EXTENSION_LENGTH
;
5881 arglengths
+= temp
->length
+ 2;
5882 SKIP_WHITE_SPACE (bp
);
5883 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5884 error ("badly punctuated parameter list in `#define'");
5889 SKIP_WHITE_SPACE (bp
);
5890 /* A comma at this point can only be followed by an identifier. */
5891 if (!is_idstart
[*bp
]
5892 && !(c99
&& limit
- bp
> (long) REST_EXTENSION_LENGTH
5893 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0)) {
5894 error ("badly punctuated parameter list in `#define'");
5899 error ("unterminated parameter list in `#define'");
5903 struct arglist
*otemp
;
5905 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5906 if (temp
->length
== otemp
->length
5907 && bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0)
5909 error ("duplicate argument name `%.*s' in `#define'",
5910 temp
->length
, temp
->name
);
5913 if (rest_args
== 0 && temp
->length
== VA_ARGS_NAME_LENGTH
5914 && bcmp (temp
->name
, va_args_name
, VA_ARGS_NAME_LENGTH
) == 0)
5917 reserved name `%s' used as argument name in `#define'", va_args_name
);
5923 ++bp
; /* skip paren */
5924 SKIP_WHITE_SPACE (bp
);
5925 /* now everything from bp before limit is the definition. */
5926 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5927 defn
->rest_args
= rest_args
;
5929 /* Now set defn->args.argnames to the result of concatenating
5930 the argument names in reverse order
5931 with comma-space between them. */
5932 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5934 struct arglist
*temp
;
5936 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5937 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5939 if (temp
->next
!= 0) {
5940 defn
->args
.argnames
[i
++] = ',';
5941 defn
->args
.argnames
[i
++] = ' ';
5944 defn
->args
.argnames
[i
] = 0;
5947 /* Simple expansion or empty definition. */
5951 if (is_hor_space
[*bp
]) {
5953 SKIP_WHITE_SPACE (bp
);
5954 } else if (sym_length
) {
5956 case '!': case '"': case '#': case '%': case '&': case '\'':
5957 case ')': case '*': case '+': case ',': case '-': case '.':
5958 case '/': case ':': case ';': case '<': case '=': case '>':
5959 case '?': case '[': case '\\': case ']': case '^': case '{':
5960 case '|': case '}': case '~':
5961 warning ("missing white space after `#define %.*s'",
5962 sym_length
, symname
);
5966 pedwarn ("missing white space after `#define %.*s'",
5967 sym_length
, symname
);
5972 /* Now everything from bp before limit is the definition. */
5973 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5974 defn
->args
.argnames
= (U_CHAR
*) "";
5979 defn
->file_len
= file_len
;
5981 /* OP is null if this is a predefinition */
5982 defn
->predefined
= !op
;
5984 mdef
.symnam
= symname
;
5985 mdef
.symlen
= sym_length
;
5994 /* Process a #define directive.
5995 BUF points to the contents of the #define directive, as a contiguous string.
5996 LIMIT points to the first character past the end of the definition.
5997 KEYWORD is the keyword-table entry for #define. */
6000 do_define (buf
, limit
, op
, keyword
)
6001 U_CHAR
*buf
, *limit
;
6003 struct directive
*keyword
;
6007 enum node_type newtype
= keyword
->type
== T_DEFINE
? T_MACRO
: T_POISON
;
6009 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
6010 if (pcp_outfile
&& op
)
6011 pass_thru_directive (buf
, limit
, op
, keyword
);
6013 mdef
= create_definition (buf
, limit
, op
);
6017 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
6021 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
6023 /* Redefining a precompiled key is ok. */
6024 if (hp
->type
== T_PCSTRING
)
6026 /* Redefining a poisoned identifier is even worse than `not ok'. */
6027 else if (hp
->type
== T_POISON
)
6029 /* Poisoning anything else is not ok.
6030 The poison should always come first. */
6031 else if (newtype
== T_POISON
)
6033 /* Redefining a macro is ok if the definitions are the same. */
6034 else if (hp
->type
== T_MACRO
)
6035 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
6036 /* Redefining a constant is ok with -D. */
6037 else if (hp
->type
== T_CONST
)
6038 ok
= ! done_initializing
;
6040 /* Print the warning or error if it's not ok. */
6043 /* If we are passing through #define and #undef directives, do
6044 that for this re-definition now. */
6045 if (debug_output
&& op
)
6046 pass_thru_directive (buf
, limit
, op
, keyword
);
6048 if (hp
->type
== T_POISON
)
6049 error ("redefining poisoned `%.*s'", mdef
.symlen
, mdef
.symnam
);
6051 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
6052 if (hp
->type
== T_MACRO
)
6053 pedwarn_with_file_and_line (hp
->value
.defn
->file
,
6054 hp
->value
.defn
->file_len
,
6055 hp
->value
.defn
->line
,
6056 "this is the location of the previous definition");
6058 if (hp
->type
!= T_POISON
)
6060 /* Replace the old definition. */
6062 hp
->value
.defn
= mdef
.defn
;
6065 /* If we are passing through #define and #undef directives, do
6066 that for this new definition now. */
6067 if (debug_output
&& op
)
6068 pass_thru_directive (buf
, limit
, op
, keyword
);
6069 install (mdef
.symnam
, mdef
.symlen
, newtype
,
6070 (char *) mdef
.defn
, hashcode
);
6081 /* Check a purported macro name SYMNAME, and yield its length.
6082 ASSERTION is nonzero if this is really for an assertion name. */
6085 check_macro_name (symname
, assertion
)
6086 const U_CHAR
*symname
;
6092 for (p
= symname
; is_idchar
[*p
]; p
++)
6094 sym_length
= p
- symname
;
6096 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
6099 error ("invalid assertion name");
6101 error ("invalid macro name");
6103 else if (!is_idstart
[*symname
]
6104 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
6107 error ("invalid assertion name `%.*s'", sym_length
, symname
);
6109 error ("invalid macro name `%.*s'", sym_length
, symname
);
6114 /* Return zero if two DEFINITIONs are isomorphic. */
6117 compare_defs (d1
, d2
)
6118 DEFINITION
*d1
, *d2
;
6120 register struct reflist
*a1
, *a2
;
6121 register U_CHAR
*p1
= d1
->expansion
;
6122 register U_CHAR
*p2
= d2
->expansion
;
6125 if (d1
->nargs
!= d2
->nargs
)
6128 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
6130 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
6131 a1
= a1
->next
, a2
= a2
->next
) {
6132 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
6133 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
6134 || a1
->argno
!= a2
->argno
6135 || a1
->stringify
!= a2
->stringify
6136 || a1
->raw_before
!= a2
->raw_before
6137 || a1
->raw_after
!= a2
->raw_after
)
6145 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
6146 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
6151 /* Return 1 if two parts of two macro definitions are effectively different.
6152 One of the parts starts at BEG1 and has LEN1 chars;
6153 the other has LEN2 chars at BEG2.
6154 Any sequence of whitespace matches any other sequence of whitespace.
6155 FIRST means these parts are the first of a macro definition;
6156 so ignore leading whitespace entirely.
6157 LAST means these parts are the last of a macro definition;
6158 so ignore trailing whitespace entirely. */
6161 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
6163 const U_CHAR
*beg1
, *beg2
;
6167 register const U_CHAR
*end1
= beg1
+ len1
;
6168 register const U_CHAR
*end2
= beg2
+ len2
;
6170 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6171 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6174 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
6175 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
6177 while (beg1
!= end1
&& beg2
!= end2
) {
6178 if (is_space
[*beg1
] && is_space
[*beg2
]) {
6179 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6180 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6181 } else if (*beg1
== *beg2
) {
6185 return (beg1
!= end1
) || (beg2
!= end2
);
6188 /* Read a replacement list for a macro with parameters.
6189 Build the DEFINITION structure.
6190 Reads characters of text starting at BUF until END.
6191 ARGLIST specifies the formal parameters to look for
6192 in the text of the definition; NARGS is the number of args
6193 in that list, or -1 for a macro name that wants no argument list.
6194 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6195 and NAMELEN is its length in characters.
6197 Note that comments, backslash-newlines, and leading white space
6198 have already been deleted from the argument. */
6200 /* If there is no trailing whitespace, a Newline Space is added at the end
6201 to prevent concatenation that would be contrary to the standard. */
6204 collect_expansion (buf
, end
, nargs
, arglist
)
6208 struct arglist
*arglist
;
6211 register const U_CHAR
*p
;
6212 register const U_CHAR
*limit
;
6213 register U_CHAR
*lastp
, *exp_p
;
6214 struct reflist
*endpat
= NULL
;
6215 /* Pointer to first nonspace after last ## seen. */
6216 const U_CHAR
*concat
= 0;
6217 /* Pointer to first nonspace after last single-# seen. */
6218 const U_CHAR
*stringify
= 0;
6219 /* How those tokens were spelled. */
6220 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
6221 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
6223 int expected_delimiter
= '\0';
6225 /* Scan thru the replacement list, ignoring comments and quoted
6226 strings, picking up on the macro calls. It does a linear search
6227 thru the arg list on every potential symbol. Profiling might say
6228 that something smarter should happen. */
6233 /* Find the beginning of the trailing whitespace. */
6236 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
6238 /* Allocate space for the text in the macro definition.
6239 Each input char may or may not need 1 byte,
6240 so this is an upper bound.
6241 The extra 3 are for invented trailing newline-marker and final null. */
6242 maxsize
= (sizeof (DEFINITION
)
6244 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
6246 defn
->nargs
= nargs
;
6247 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
6252 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
6253 error ("`##' at start of macro definition");
6254 p
+= p
[0] == '#' ? 2 : 4;
6257 /* Process the main body of the definition. */
6259 int skipped_arg
= 0;
6260 register U_CHAR c
= *p
++;
6268 if (expected_delimiter
!= '\0') {
6269 if (c
== expected_delimiter
)
6270 expected_delimiter
= '\0';
6272 expected_delimiter
= c
;
6276 if (expected_delimiter
) {
6277 /* In a string, backslash goes through
6278 and makes next char ordinary. */
6284 if (!expected_delimiter
&& *p
== ':') {
6285 /* %: is not a digraph if preceded by an odd number of '<'s. */
6286 const U_CHAR
*p0
= p
- 1;
6287 while (buf
< p0
&& p0
[-1] == '<')
6290 /* Treat %:%: as ## and %: as #. */
6291 if (p
[1] == '%' && p
[2] == ':') {
6293 goto sharp_sharp_token
;
6304 /* # is ordinary inside a string. */
6305 if (expected_delimiter
)
6309 /* ##: concatenate preceding and following tokens. */
6310 /* Take out the first #, discard preceding whitespace. */
6312 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
6314 /* Skip the second #. */
6316 concat_sharp_token_type
= c
;
6317 if (is_hor_space
[*p
]) {
6318 concat_sharp_token_type
= c
+ 1;
6320 SKIP_WHITE_SPACE (p
);
6324 error ("`##' at end of macro definition");
6325 } else if (nargs
>= 0) {
6326 /* Single #: stringify following argument ref.
6327 Don't leave the # in the expansion. */
6330 stringify_sharp_token_type
= c
;
6331 if (is_hor_space
[*p
]) {
6332 stringify_sharp_token_type
= c
+ 1;
6334 SKIP_WHITE_SPACE (p
);
6336 if (! is_idstart
[*p
] || nargs
== 0
6337 || (*p
== 'L' && (p
[1] == '\'' || p
[1] == '"')))
6338 error ("`#' operator is not followed by a macro argument name");
6345 /* In -traditional mode, recognize arguments inside strings and
6346 character constants, and ignore special properties of #.
6347 Arguments inside strings are considered "stringified", but no
6348 extra quote marks are supplied. */
6352 if (expected_delimiter
!= '\0') {
6353 if (c
== expected_delimiter
)
6354 expected_delimiter
= '\0';
6356 expected_delimiter
= c
;
6360 /* Backslash quotes delimiters and itself, but not macro args. */
6361 if (expected_delimiter
!= 0 && p
< limit
6362 && (*p
== expected_delimiter
|| *p
== '\\')) {
6369 if (expected_delimiter
!= '\0') /* No comments inside strings. */
6372 /* If we find a comment that wasn't removed by handle_directive,
6373 this must be -traditional. So replace the comment with
6376 while (++p
< limit
) {
6377 if (p
[0] == '*' && p
[1] == '/') {
6383 /* Mark this as a concatenation-point, as if it had been ##. */
6391 #ifdef MULTIBYTE_CHARS
6392 /* Handle multibyte characters inside string and character literals. */
6393 if (expected_delimiter
!= '\0')
6397 length
= local_mblen (p
, limit
- p
);
6401 bcopy (p
, exp_p
, length
);
6410 /* Handle the start of a symbol. */
6411 if (is_idchar
[c
] && nargs
> 0) {
6412 const U_CHAR
*id_beg
= p
- 1;
6416 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6417 id_len
= p
- id_beg
;
6420 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
6421 register struct arglist
*arg
;
6423 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6424 struct reflist
*tpat
;
6426 if (arg
->name
[0] == c
6427 && arg
->length
== id_len
6428 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6429 enum sharp_token_type tpat_stringify
;
6430 if (expected_delimiter
) {
6431 if (warn_stringify
) {
6433 warning ("macro argument `%.*s' is stringified.",
6436 warning ("macro arg `%.*s' would be stringified with -traditional.",
6440 /* If ANSI, don't actually substitute inside a string. */
6443 tpat_stringify
= SHARP_TOKEN
;
6446 = (stringify
== id_beg
6447 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6449 /* make a pat node for this arg and append it to the end of
6451 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6454 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6455 tpat
->raw_after
= NO_SHARP_TOKEN
;
6456 tpat
->rest_args
= arg
->rest_args
;
6457 tpat
->stringify
= tpat_stringify
;
6460 defn
->pattern
= tpat
;
6462 endpat
->next
= tpat
;
6465 tpat
->argno
= arg
->argno
;
6466 tpat
->nchars
= exp_p
- lastp
;
6468 register const U_CHAR
*p1
= p
;
6469 SKIP_WHITE_SPACE (p1
);
6472 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6473 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6475 lastp
= exp_p
; /* place to start copying from next time */
6482 /* If this was not a macro arg, copy it into the expansion. */
6483 if (! skipped_arg
) {
6484 register const U_CHAR
*lim1
= p
;
6488 if (stringify
== id_beg
)
6489 error ("`#' operator should be followed by a macro argument name");
6494 if (!traditional
&& expected_delimiter
== 0) {
6495 /* If ANSI, put in a newline-space marker to prevent token pasting.
6496 But not if "inside a string" (which in ANSI mode happens only for
6504 defn
->length
= exp_p
- defn
->expansion
;
6506 /* Crash now if we overrun the allocated size. */
6507 if (defn
->length
+ 1 > maxsize
)
6511 /* This isn't worth the time it takes. */
6512 /* give back excess storage */
6513 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6520 do_assert (buf
, limit
, op
, keyword
)
6521 U_CHAR
*buf
, *limit
;
6522 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6523 struct directive
*keyword ATTRIBUTE_UNUSED
;
6525 const U_CHAR
*bp
; /* temp ptr into input buffer */
6526 const U_CHAR
*symname
; /* remember where symbol name starts */
6527 int sym_length
; /* and how long it is */
6528 struct arglist
*tokens
= NULL
;
6530 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6531 pedwarn ("ANSI C does not allow `#assert'");
6535 while (is_hor_space
[*bp
])
6538 symname
= bp
; /* remember where it starts */
6539 sym_length
= check_macro_name (bp
, 1);
6541 /* #define doesn't do this, but we should. */
6542 SKIP_WHITE_SPACE (bp
);
6544 /* Lossage will occur if identifiers or control tokens are broken
6545 across lines using backslash. This is not the right place to take
6549 error ("missing token-sequence in `#assert'");
6556 bp
++; /* skip '(' */
6557 SKIP_WHITE_SPACE (bp
);
6559 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6563 error ("empty token-sequence in `#assert'");
6567 ++bp
; /* skip paren */
6568 SKIP_WHITE_SPACE (bp
);
6571 /* If this name isn't already an assertion name, make it one.
6572 Error if it was already in use in some other way. */
6575 ASSERTION_HASHNODE
*hp
;
6576 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6577 struct tokenlist_list
*value
6578 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6580 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6582 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6583 error ("`defined' redefined as assertion");
6584 hp
= assertion_install (symname
, sym_length
, hashcode
);
6587 /* Add the spec'd token-sequence to the list of such. */
6588 value
->tokens
= tokens
;
6589 value
->next
= hp
->value
;
6597 do_unassert (buf
, limit
, op
, keyword
)
6598 U_CHAR
*buf
, *limit
;
6599 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6600 struct directive
*keyword ATTRIBUTE_UNUSED
;
6602 const U_CHAR
*bp
; /* temp ptr into input buffer */
6603 const U_CHAR
*symname
; /* remember where symbol name starts */
6604 int sym_length
; /* and how long it is */
6606 struct arglist
*tokens
= NULL
;
6607 int tokens_specified
= 0;
6609 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6610 pedwarn ("ANSI C does not allow `#unassert'");
6614 while (is_hor_space
[*bp
])
6617 symname
= bp
; /* remember where it starts */
6618 sym_length
= check_macro_name (bp
, 1);
6620 /* #define doesn't do this, but we should. */
6621 SKIP_WHITE_SPACE (bp
);
6623 /* Lossage will occur if identifiers or control tokens are broken
6624 across lines using backslash. This is not the right place to take
6630 bp
++; /* skip '(' */
6631 SKIP_WHITE_SPACE (bp
);
6633 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6637 error ("empty token list in `#unassert'");
6641 tokens_specified
= 1;
6643 ++bp
; /* skip paren */
6644 SKIP_WHITE_SPACE (bp
);
6648 ASSERTION_HASHNODE
*hp
;
6649 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6650 struct tokenlist_list
*tail
, *prev
;
6652 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6656 /* If no token list was specified, then eliminate this assertion
6658 if (! tokens_specified
) {
6659 struct tokenlist_list
*next
;
6660 for (tail
= hp
->value
; tail
; tail
= next
) {
6662 free_token_list (tail
->tokens
);
6665 delete_assertion (hp
);
6667 /* If a list of tokens was given, then delete any matching list. */
6672 struct tokenlist_list
*next
= tail
->next
;
6673 if (compare_token_lists (tail
->tokens
, tokens
)) {
6677 hp
->value
= tail
->next
;
6678 free_token_list (tail
->tokens
);
6691 /* Test whether there is an assertion named NAME
6692 and optionally whether it has an asserted token list TOKENS.
6693 NAME is not null terminated; its length is SYM_LENGTH.
6694 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6697 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6700 int tokens_specified
;
6701 struct arglist
*tokens
;
6703 ASSERTION_HASHNODE
*hp
;
6704 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6706 if (pedantic
&& !instack
[indepth
].system_header_p
)
6707 pedwarn ("ANSI C does not allow testing assertions");
6709 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6711 /* It is not an assertion; just return false. */
6714 /* If no token list was specified, then value is 1. */
6715 if (! tokens_specified
)
6719 struct tokenlist_list
*tail
;
6723 /* If a list of tokens was given,
6724 then succeed if the assertion records a matching list. */
6727 if (compare_token_lists (tail
->tokens
, tokens
))
6732 /* Fail if the assertion has no matching list. */
6737 /* Compare two lists of tokens for equality including order of tokens. */
6740 compare_token_lists (l1
, l2
)
6741 struct arglist
*l1
, *l2
;
6744 if (l1
->length
!= l2
->length
)
6746 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6752 /* Succeed if both lists end at the same time. */
6756 /* Read a space-separated list of tokens ending in a close parenthesis.
6757 Return a list of strings, in the order they were written.
6758 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6759 Parse the text starting at *BPP, and update *BPP.
6760 Don't parse beyond LIMIT. */
6762 static struct arglist
*
6763 read_token_list (bpp
, limit
, error_flag
)
6765 const U_CHAR
*limit
;
6768 struct arglist
*token_ptrs
= 0;
6769 const U_CHAR
*bp
= *bpp
;
6774 /* Loop over the assertion value tokens. */
6776 struct arglist
*temp
;
6779 const U_CHAR
*beg
= bp
;
6781 /* Find the end of the token. */
6785 } else if (*bp
== ')') {
6790 } else if (*bp
== '"' || *bp
== '\'')
6791 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6793 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6794 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6797 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6798 temp2
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6799 bcopy ((const PTR
) beg
, (PTR
) temp2
, bp
- beg
);
6800 temp2
[bp
- beg
] = 0;
6802 temp
->next
= token_ptrs
;
6804 temp
->length
= bp
- beg
;
6806 SKIP_WHITE_SPACE (bp
);
6809 error ("unterminated token sequence in `#assert' or `#unassert'");
6816 /* We accumulated the names in reverse order.
6817 Now reverse them to get the proper order. */
6819 register struct arglist
*prev
= 0, *this, *next
;
6820 for (this = token_ptrs
; this; this = next
) {
6830 free_token_list (tokens
)
6831 struct arglist
*tokens
;
6834 struct arglist
*next
= tokens
->next
;
6835 free ((PTR
) tokens
->name
);
6841 /* Install a name in the assertion hash table.
6843 If LEN is >= 0, it is the length of the name.
6844 Otherwise, compute the length by scanning the entire name.
6846 If HASH is >= 0, it is the precomputed hash code.
6847 Otherwise, compute the hash code. */
6849 static ASSERTION_HASHNODE
*
6850 assertion_install (name
, len
, hash
)
6855 register ASSERTION_HASHNODE
*hp
;
6856 register int i
, bucket
;
6858 register const U_CHAR
*q
;
6860 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6861 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6863 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6864 hp
->next
= assertion_hashtab
[bucket
];
6865 assertion_hashtab
[bucket
] = hp
;
6867 if (hp
->next
!= NULL
)
6868 hp
->next
->prev
= hp
;
6871 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6874 for (i
= 0; i
< len
; i
++)
6880 /* Find the most recent hash node for name "name" (ending with first
6881 non-identifier char) installed by install
6883 If LEN is >= 0, it is the length of the name.
6884 Otherwise, compute the length by scanning the entire name.
6886 If HASH is >= 0, it is the precomputed hash code.
6887 Otherwise, compute the hash code. */
6889 static ASSERTION_HASHNODE
*
6890 assertion_lookup (name
, len
, hash
)
6895 register ASSERTION_HASHNODE
*bucket
;
6897 bucket
= assertion_hashtab
[hash
];
6899 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6901 bucket
= bucket
->next
;
6907 delete_assertion (hp
)
6908 ASSERTION_HASHNODE
*hp
;
6911 if (hp
->prev
!= NULL
)
6912 hp
->prev
->next
= hp
->next
;
6913 if (hp
->next
!= NULL
)
6914 hp
->next
->prev
= hp
->prev
;
6916 /* Make sure that the bucket chain header that the deleted guy was
6917 on points to the right thing afterwards. */
6918 if (hp
== *hp
->bucket_hdr
)
6919 *hp
->bucket_hdr
= hp
->next
;
6925 * interpret #line directive. Remembers previously seen fnames
6926 * in its very own hash table.
6928 #define FNAME_HASHSIZE 37
6931 do_line (buf
, limit
, op
, keyword
)
6932 U_CHAR
*buf
, *limit
;
6934 struct directive
*keyword ATTRIBUTE_UNUSED
;
6936 register U_CHAR
*bp
;
6937 FILE_BUF
*ip
= &instack
[indepth
];
6940 enum file_change_code file_change
= same_file
;
6942 /* Expand any macros. */
6943 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6945 /* Point to macroexpanded line, which is null-terminated now. */
6948 SKIP_WHITE_SPACE (bp
);
6950 if (!ISDIGIT (*bp
)) {
6951 error ("invalid format `#line' directive");
6955 /* The Newline at the end of this line remains to be processed.
6956 To put the next line at the specified line number,
6957 we must store a line number now that is one less. */
6958 new_lineno
= atoi ((char *) bp
) - 1;
6960 /* NEW_LINENO is one less than the actual line number here. */
6961 if (pedantic
&& new_lineno
< 0)
6962 pedwarn ("line number out of range in `#line' directive");
6964 /* skip over the line number. */
6965 while (ISDIGIT (*bp
))
6968 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6969 if (*bp
&& !is_space
[*bp
]) {
6970 error ("invalid format `#line' directive");
6975 SKIP_WHITE_SPACE (bp
);
6978 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6979 HASHNODE
*hp
, **hash_bucket
;
6985 /* Turn the file name, which is a character string literal,
6986 into a null-terminated string. Do this in place. */
6989 switch ((*p
++ = *bp
++)) {
6991 if (! ignore_escape_flag
)
6993 char *bpc
= (char *) bp
;
6994 HOST_WIDEST_INT c
= parse_escape (&bpc
, (HOST_WIDEST_INT
) (U_CHAR
) (-1));
6995 bp
= (U_CHAR
*) bpc
;
7008 fname_length
= p
- fname
;
7010 SKIP_WHITE_SPACE (bp
);
7013 pedwarn ("garbage at end of `#line' directive");
7015 file_change
= enter_file
;
7016 else if (*bp
== '2')
7017 file_change
= leave_file
;
7018 else if (*bp
== '3')
7019 ip
->system_header_p
= 1;
7020 else if (*bp
== '4')
7021 ip
->system_header_p
= 2;
7023 error ("invalid format `#line' directive");
7028 SKIP_WHITE_SPACE (bp
);
7030 ip
->system_header_p
= 1;
7032 SKIP_WHITE_SPACE (bp
);
7035 ip
->system_header_p
= 2;
7037 SKIP_WHITE_SPACE (bp
);
7040 error ("invalid format `#line' directive");
7045 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
7046 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
7047 if (hp
->length
== fname_length
&&
7048 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
7049 ip
->nominal_fname
= hp
->value
.cpval
;
7050 ip
->nominal_fname_len
= fname_length
;
7054 /* Didn't find it; cons up a new one. */
7055 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
7056 hp
->next
= *hash_bucket
;
7059 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
7060 ip
->nominal_fname_len
= hp
->length
= fname_length
;
7061 bcopy (fname
, ((char *) hp
) + sizeof (HASHNODE
), fname_length
+ 1);
7064 error ("invalid format `#line' directive");
7068 ip
->lineno
= new_lineno
;
7069 output_line_directive (ip
, op
, 0, file_change
);
7070 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
7074 /* Remove the definition of a symbol from the symbol table.
7075 according to un*x /lib/cpp, it is not an error to undef
7076 something that has no definitions, so it isn't one here either. */
7079 do_undef (buf
, limit
, op
, keyword
)
7080 U_CHAR
*buf
, *limit
;
7082 struct directive
*keyword
;
7086 U_CHAR
*orig_buf
= buf
;
7088 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7089 if (pcp_outfile
&& op
)
7090 pass_thru_directive (buf
, limit
, op
, keyword
);
7092 SKIP_WHITE_SPACE (buf
);
7093 sym_length
= check_macro_name (buf
, 0);
7095 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
7096 /* If we are generating additional info for debugging (with -g) we
7097 need to pass through all effective #undef directives. */
7098 if (debug_output
&& op
)
7099 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
7100 if (hp
->type
== T_POISON
)
7101 error ("cannot undefine poisoned `%s'", hp
->name
);
7103 if (hp
->type
!= T_MACRO
)
7104 warning ("undefining `%s'", hp
->name
);
7111 SKIP_WHITE_SPACE (buf
);
7113 pedwarn ("garbage after `#undef' directive");
7119 /* Report an error detected by the program we are processing.
7120 Use the text of the line in the error message. */
7123 do_error (buf
, limit
, op
, keyword
)
7124 U_CHAR
*buf
, *limit
;
7125 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7126 struct directive
*keyword
;
7128 int length
= limit
- buf
;
7129 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
7130 bcopy ((const PTR
) buf
, (PTR
) copy
, length
);
7132 SKIP_WHITE_SPACE (copy
);
7134 switch (keyword
->type
) {
7136 error ("#error %s", copy
);
7140 if (pedantic
&& !instack
[indepth
].system_header_p
)
7141 pedwarn ("ANSI C does not allow `#warning'");
7142 warning ("#warning %s", copy
);
7151 /* Remember the name of the current file being read from so that we can
7152 avoid ever including it again. */
7159 for (i
= indepth
; i
>= 0; i
--)
7160 if (instack
[i
].inc
) {
7161 record_control_macro (instack
[i
].inc
, (const U_CHAR
*) "");
7166 /* Report program identification. */
7169 do_ident (buf
, limit
, op
, keyword
)
7170 U_CHAR
*buf
, *limit
;
7172 struct directive
*keyword ATTRIBUTE_UNUSED
;
7177 /* Allow #ident in system headers, since that's not user's fault. */
7178 if (pedantic
&& !instack
[indepth
].system_header_p
)
7179 pedwarn ("ANSI C does not allow `#ident'");
7181 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
7183 len
= trybuf
.bufp
- buf
;
7185 /* Output expanded directive. */
7186 check_expand (op
, 7 + len
);
7187 bcopy ("#ident ", (char *) op
->bufp
, 7);
7189 bcopy ((const PTR
) buf
, (PTR
) op
->bufp
, len
);
7196 /* #pragma and its argument line have already been copied to the output file.
7197 Just check for some recognized pragmas that need validation here. */
7200 do_pragma (buf
, limit
, op
, keyword
)
7201 U_CHAR
*buf
, *limit
;
7203 struct directive
*keyword
;
7205 SKIP_WHITE_SPACE (buf
);
7206 if (!strncmp ((char *) buf
, "once", 4)) {
7207 /* Allow #pragma once in system headers, since that's not the user's
7209 if (!instack
[indepth
].system_header_p
)
7210 warning ("`#pragma once' is obsolete");
7214 if (!strncmp ((char *) buf
, "poison", 6)) {
7215 /* Poison these symbols so that all subsequent usage produces an
7217 U_CHAR
*p
= buf
+ 6;
7219 SKIP_WHITE_SPACE (p
);
7224 while (end
< limit
&& is_idchar
[*end
])
7226 if (end
< limit
&& !is_space
[*end
])
7228 error ("invalid #pragma poison");
7231 do_define(p
, end
, op
, keyword
);
7233 SKIP_WHITE_SPACE (p
);
7237 if (!strncmp ((char *) buf
, "implementation", 14)) {
7238 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7239 been included yet. */
7242 U_CHAR
*p
= buf
+ 14, *fname
;
7243 SKIP_WHITE_SPACE (p
);
7248 p
= skip_quoted_string (p
, limit
, 0, NULL_PTR
, NULL_PTR
, NULL_PTR
);
7252 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
7253 struct include_file
*inc
;
7254 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
7255 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
7256 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
7266 /* This was a fun hack, but #pragma seems to start to be useful.
7267 By failing to recognize it, we pass it through unchanged to cc1. */
7269 /* The behavior of the #pragma directive is implementation defined.
7270 this implementation defines it as follows. */
7276 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
7279 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
7281 execl ("/usr/games/hack", "#pragma", 0);
7282 execl ("/usr/games/rogue", "#pragma", 0);
7283 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7284 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7286 fatal ("You are in a maze of twisty compiler features, all different");
7290 #ifdef SCCS_DIRECTIVE
7292 /* Just ignore #sccs, on systems where we define it at all. */
7295 do_sccs (buf
, limit
, op
, keyword
)
7296 U_CHAR
*buf ATTRIBUTE_UNUSED
;
7297 U_CHAR
*limit ATTRIBUTE_UNUSED
;
7298 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7299 struct directive
*keyword ATTRIBUTE_UNUSED
;
7302 pedwarn ("ANSI C does not allow `#sccs'");
7306 #endif /* defined (SCCS_DIRECTIVE) */
7308 /* Handle #if directive by
7309 1) inserting special `defined' keyword into the hash table
7310 that gets turned into 0 or 1 by special_symbol (thus,
7311 if the luser has a symbol called `defined' already, it won't
7312 work inside the #if directive)
7313 2) rescan the input into a temporary output buffer
7314 3) pass the output buffer to the yacc parser and collect a value
7315 4) clean up the mess left from steps 1 and 2.
7316 5) call conditional_skip to skip til the next #endif (etc.),
7317 or not, depending on the value from step 3. */
7320 do_if (buf
, limit
, op
, keyword
)
7321 U_CHAR
*buf
, *limit
;
7323 struct directive
*keyword ATTRIBUTE_UNUSED
;
7325 HOST_WIDEST_INT value
;
7326 FILE_BUF
*ip
= &instack
[indepth
];
7328 value
= eval_if_expression (buf
, limit
- buf
);
7329 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
7333 /* Handle a #elif directive by not changing if_stack either.
7334 see the comment above do_else. */
7337 do_elif (buf
, limit
, op
, keyword
)
7338 U_CHAR
*buf
, *limit
;
7340 struct directive
*keyword ATTRIBUTE_UNUSED
;
7342 HOST_WIDEST_INT value
;
7343 FILE_BUF
*ip
= &instack
[indepth
];
7345 if (if_stack
== instack
[indepth
].if_stack
) {
7346 error ("`#elif' not within a conditional");
7349 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7350 error ("`#elif' after `#else'");
7351 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7352 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7353 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7354 if_stack
->fname_len
))) {
7355 fprintf (stderr
, ", file ");
7356 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7357 if_stack
->fname_len
, stderr
);
7359 fprintf (stderr
, ")\n");
7361 if_stack
->type
= T_ELIF
;
7364 if (if_stack
->if_succeeded
)
7365 skip_if_group (ip
, 0, op
);
7367 value
= eval_if_expression (buf
, limit
- buf
);
7369 skip_if_group (ip
, 0, op
);
7371 ++if_stack
->if_succeeded
; /* continue processing input */
7372 output_line_directive (ip
, op
, 1, same_file
);
7378 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7379 result as a C expression and return the value as an int. */
7381 static HOST_WIDEST_INT
7382 eval_if_expression (buf
, length
)
7387 HASHNODE
*save_defined
;
7388 HOST_WIDEST_INT value
;
7390 save_defined
= install ((const U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
7393 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
7395 delete_macro (save_defined
); /* clean up special symbol */
7397 *temp_obuf
.bufp
= '\n';
7398 value
= parse_c_expression ((char *) temp_obuf
.buf
,
7399 warn_undef
&& !instack
[indepth
].system_header_p
);
7401 free (temp_obuf
.buf
);
7406 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7407 or don't skip to the #endif/#else/#elif depending on what directive
7408 is actually being processed. */
7411 do_xifdef (buf
, limit
, op
, keyword
)
7412 U_CHAR
*buf
, *limit
;
7414 struct directive
*keyword
;
7417 FILE_BUF
*ip
= &instack
[indepth
];
7419 int start_of_file
= 0;
7420 U_CHAR
*control_macro
= 0;
7422 /* Detect a #ifndef at start of file (not counting comments). */
7423 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7424 U_CHAR
*p
= ip
->buf
;
7425 while (p
!= directive_start
) {
7429 /* Make no special provision for backslash-newline here; this is
7430 slower if backslash-newlines are present, but it's correct,
7431 and it's not worth it to tune for the rare backslash-newline. */
7433 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7434 /* Skip this comment. */
7436 U_CHAR
*save_bufp
= ip
->bufp
;
7438 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7439 ip
->bufp
= save_bufp
;
7444 /* If we get here, this conditional is the beginning of the file. */
7449 /* Discard leading and trailing whitespace. */
7450 SKIP_WHITE_SPACE (buf
);
7451 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7453 /* Find the end of the identifier at the beginning. */
7454 for (end
= buf
; is_idchar
[*end
]; end
++);
7457 skip
= (keyword
->type
== T_IFDEF
);
7461 pedwarn ("`#%s' with no argument", keyword
->name
);
7463 pedwarn ("`#%s' argument starts with punctuation", keyword
->name
);
7468 if (! traditional
) {
7469 if (ISDIGIT (buf
[0]))
7470 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7471 else if (end
!= limit
)
7472 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7475 hp
= lookup (buf
, end
-buf
, -1);
7478 /* Output a precondition for this macro. */
7480 && (hp
->type
== T_CONST
7481 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7482 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7485 fprintf (pcp_outfile
, "#undef ");
7486 while (is_idchar
[*cp
]) /* Ick! */
7487 fputc (*cp
++, pcp_outfile
);
7488 putc ('\n', pcp_outfile
);
7492 if ((hp
!= NULL
) && (hp
->type
== T_POISON
)) {
7493 error("attempt to use poisoned `%s'.", hp
->name
);
7496 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7497 if (start_of_file
&& !skip
) {
7498 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7499 bcopy ((const PTR
) buf
, (PTR
) control_macro
, end
- buf
);
7500 control_macro
[end
- buf
] = 0;
7504 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7508 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7509 If this is a #ifndef starting at the beginning of a file,
7510 CONTROL_MACRO is the macro name tested by the #ifndef.
7511 Otherwise, CONTROL_MACRO is 0. */
7514 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7517 enum node_type type
;
7518 const U_CHAR
*control_macro
;
7521 IF_STACK_FRAME
*temp
;
7523 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7524 temp
->fname
= ip
->nominal_fname
;
7525 temp
->fname_len
= ip
->nominal_fname_len
;
7526 temp
->lineno
= ip
->lineno
;
7527 temp
->next
= if_stack
;
7528 temp
->control_macro
= control_macro
;
7531 if_stack
->type
= type
;
7534 skip_if_group (ip
, 0, op
);
7537 ++if_stack
->if_succeeded
;
7538 output_line_directive (ip
, &outbuf
, 1, same_file
);
7542 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7543 Leaves input ptr at the sharp sign found.
7544 If ANY is nonzero, return at next directive of any sort. */
7547 skip_if_group (ip
, any
, op
)
7552 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7553 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7554 struct directive
*kt
;
7555 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7556 U_CHAR
*beg_of_line
= bp
;
7557 register int ident_length
;
7558 U_CHAR
*ident
, *after_ident
;
7559 /* Save info about where the group starts. */
7560 U_CHAR
*beg_of_group
= bp
;
7561 int beg_lineno
= ip
->lineno
;
7562 int skipping_include_directive
= 0;
7564 if (output_conditionals
&& op
!= 0) {
7565 static const char * const ptr
= "#failed\n";
7566 int len
= strlen (ptr
);
7568 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7573 check_expand (op
, len
);
7574 bcopy (ptr
, (char *) op
->bufp
, len
);
7577 output_line_directive (ip
, op
, 1, 0);
7582 case '/': /* possible comment */
7586 || (cplusplus_comments
&& *bp
== '/')) {
7588 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7592 if (skipping_include_directive
) {
7593 while (bp
< endb
&& *bp
!= '>' && *bp
!= '\n') {
7594 if (*bp
== '\\' && bp
[1] == '\n') {
7603 if (skipping_include_directive
) {
7604 while (bp
< endb
&& *bp
!= '\n') {
7609 if (*bp
== '\\' && bp
[1] == '\n') {
7619 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7620 NULL_PTR
, NULL_PTR
);
7623 /* Char after backslash loses its special meaning in some cases. */
7627 } else if (traditional
&& bp
< endb
)
7633 skipping_include_directive
= 0;
7636 if (beg_of_line
== 0 || traditional
)
7639 while (bp
[0] == '\\' && bp
[1] == '\n')
7645 /* # keyword: a # must be first nonblank char on the line */
7646 if (beg_of_line
== 0)
7650 /* Scan from start of line, skipping whitespace, comments
7651 and backslash-newlines, and see if we reach this #.
7652 If not, this # is not special. */
7654 /* If -traditional, require # to be at beginning of line. */
7657 if (is_hor_space
[*bp
])
7659 else if (*bp
== '\\' && bp
[1] == '\n')
7661 else if (*bp
== '/' && bp
[1] == '*') {
7675 #ifdef MULTIBYTE_CHARS
7677 length
= local_mblen (bp
, endb
- bp
);
7685 /* There is no point in trying to deal with C++ // comments here,
7686 because if there is one, then this # must be part of the
7687 comment and we would never reach here. */
7691 if (bp
!= ip
->bufp
) {
7692 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7696 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7697 if (ip
->bufp
[0] == '%') {
7698 /* Skip past the ':' again. */
7699 while (*bp
== '\\') {
7706 /* Skip whitespace and \-newline. */
7708 if (is_hor_space
[*bp
])
7710 else if (*bp
== '\\' && bp
[1] == '\n')
7712 else if (*bp
== '/') {
7714 newline_fix (bp
+ 1);
7716 for (bp
+= 2; ; bp
++) {
7719 else if (*bp
== '*') {
7720 if (bp
[-1] == '/' && warn_comments
)
7721 warning ("`/*' within comment");
7723 newline_fix (bp
+ 1);
7729 #ifdef MULTIBYTE_CHARS
7731 length
= local_mblen (bp
, endb
- bp
);
7738 } else if (bp
[1] == '/' && cplusplus_comments
) {
7739 for (bp
+= 2; ; bp
++) {
7742 if (*bp
== '\\' && bp
[1] == '\n')
7745 warning ("multiline `//' comment");
7751 #ifdef MULTIBYTE_CHARS
7753 length
= local_mblen (bp
, endb
- bp
);
7767 /* Now find end of directive name.
7768 If we encounter a backslash-newline, exchange it with any following
7769 symbol-constituents so that we end up with a contiguous name. */
7776 name_newline_fix (bp
);
7782 ident_length
= bp
- cp
;
7786 /* A line of just `#' becomes blank. */
7788 if (ident_length
== 0 && *after_ident
== '\n') {
7792 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7794 while (is_idchar
[*p
]) {
7795 if (*p
< '0' || *p
> '9')
7799 /* Handle # followed by a line number. */
7800 if (p
!= ident
&& !is_idchar
[*p
]) {
7802 pedwarn ("`#' followed by integer");
7806 /* Avoid error for `###' and similar cases unless -pedantic. */
7808 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7810 if (pedantic
&& !lang_asm
)
7811 pedwarn ("invalid preprocessing directive");
7816 if (!lang_asm
&& pedantic
)
7817 pedwarn ("invalid preprocessing directive name");
7821 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7822 IF_STACK_FRAME
*temp
;
7823 if (ident_length
== kt
->length
7824 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7825 /* If we are asked to return on next directive, do so now. */
7833 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7834 temp
->next
= if_stack
;
7836 temp
->lineno
= ip
->lineno
;
7837 temp
->fname
= ip
->nominal_fname
;
7838 temp
->fname_len
= ip
->nominal_fname_len
;
7839 temp
->type
= kt
->type
;
7843 if (pedantic
&& if_stack
!= save_if_stack
)
7844 validate_else (bp
, endb
);
7846 if (if_stack
== instack
[indepth
].if_stack
) {
7847 error ("`#%s' not within a conditional", kt
->name
);
7850 else if (if_stack
== save_if_stack
)
7851 goto done
; /* found what we came for */
7853 if (kt
->type
!= T_ENDIF
) {
7854 if (if_stack
->type
== T_ELSE
)
7855 error ("`#else' or `#elif' after `#else'");
7856 if_stack
->type
= kt
->type
;
7861 if_stack
= if_stack
->next
;
7866 case T_INCLUDE_NEXT
:
7868 skipping_include_directive
= 1;
7877 /* Don't let erroneous code go by. */
7878 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7879 pedwarn ("invalid preprocessing directive name");
7884 /* after this returns, rescan will exit because ip->bufp
7885 now points to the end of the buffer.
7886 rescan is responsible for the error message also. */
7889 if (output_conditionals
&& op
!= 0) {
7890 static const char * const ptr
= "#endfailed\n";
7891 int len
= strlen (ptr
);
7893 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7898 check_expand (op
, beg_of_line
- beg_of_group
);
7899 bcopy ((const PTR
) beg_of_group
, (PTR
) op
->bufp
,
7900 beg_of_line
- beg_of_group
);
7901 op
->bufp
+= beg_of_line
- beg_of_group
;
7902 op
->lineno
+= ip
->lineno
- beg_lineno
;
7903 check_expand (op
, len
);
7904 bcopy (ptr
, (char *) op
->bufp
, len
);
7910 /* Handle a #else directive. Do this by just continuing processing
7911 without changing if_stack ; this is so that the error message
7912 for missing #endif's etc. will point to the original #if. It
7913 is possible that something different would be better. */
7916 do_else (buf
, limit
, op
, keyword
)
7917 U_CHAR
*buf
, *limit
;
7919 struct directive
*keyword ATTRIBUTE_UNUSED
;
7921 FILE_BUF
*ip
= &instack
[indepth
];
7924 SKIP_WHITE_SPACE (buf
);
7926 pedwarn ("text following `#else' violates ANSI standard");
7929 if (if_stack
== instack
[indepth
].if_stack
) {
7930 error ("`#else' not within a conditional");
7933 /* #ifndef can't have its special treatment for containing the whole file
7934 if it has a #else clause. */
7935 if_stack
->control_macro
= 0;
7937 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7938 error ("`#else' after `#else'");
7939 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7940 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7941 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7942 if_stack
->fname_len
))) {
7943 fprintf (stderr
, ", file ");
7944 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7945 if_stack
->fname_len
, stderr
);
7947 fprintf (stderr
, ")\n");
7949 if_stack
->type
= T_ELSE
;
7952 if (if_stack
->if_succeeded
)
7953 skip_if_group (ip
, 0, op
);
7955 ++if_stack
->if_succeeded
; /* continue processing input */
7956 output_line_directive (ip
, op
, 1, same_file
);
7961 /* Unstack after #endif directive. */
7964 do_endif (buf
, limit
, op
, keyword
)
7965 U_CHAR
*buf
, *limit
;
7967 struct directive
*keyword ATTRIBUTE_UNUSED
;
7970 SKIP_WHITE_SPACE (buf
);
7972 pedwarn ("text following `#endif' violates ANSI standard");
7975 if (if_stack
== instack
[indepth
].if_stack
)
7976 error ("unbalanced `#endif'");
7978 IF_STACK_FRAME
*temp
= if_stack
;
7979 if_stack
= if_stack
->next
;
7980 if (temp
->control_macro
!= 0) {
7981 /* This #endif matched a #ifndef at the start of the file.
7982 See if it is at the end of the file. */
7983 FILE_BUF
*ip
= &instack
[indepth
];
7984 U_CHAR
*p
= ip
->bufp
;
7985 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7991 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7992 /* Skip this comment. */
7994 U_CHAR
*save_bufp
= ip
->bufp
;
7996 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7997 ip
->bufp
= save_bufp
;
8002 /* If we get here, this #endif ends a #ifndef
8003 that contains all of the file (aside from whitespace).
8004 Arrange not to include the file again
8005 if the macro that was tested is defined.
8007 Do not do this for the top-level file in a -include or any
8008 file in a -imacros. */
8010 && ! (indepth
== 1 && no_record_file
)
8011 && ! (no_record_file
&& no_output
))
8012 record_control_macro (ip
->inc
, temp
->control_macro
);
8016 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
8021 /* When an #else or #endif is found while skipping failed conditional,
8022 if -pedantic was specified, this is called to warn about text after
8023 the directive name. P points to the first char after the directive
8027 validate_else (p
, limit
)
8028 register const U_CHAR
*p
;
8029 register const U_CHAR
*limit
;
8031 /* Advance P over whitespace and comments. */
8033 while (*p
== '\\' && p
[1] == '\n')
8035 if (is_hor_space
[*p
])
8037 else if (*p
== '/') {
8038 while (p
[1] == '\\' && p
[2] == '\n')
8041 /* Don't bother warning about unterminated comments
8042 since that will happen later. Just be sure to exit. */
8043 for (p
+= 2; ; p
++) {
8047 while (p
[1] == '\\' && p
[2] == '\n')
8056 #ifdef MULTIBYTE_CHARS
8058 length
= local_mblen (p
, limit
- p
);
8065 else if (cplusplus_comments
&& p
[1] == '/')
8071 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8074 /* Skip a comment, assuming the input ptr immediately follows the
8075 initial slash-star. Bump *LINE_COUNTER for each newline.
8076 (The canonical line counter is &ip->lineno.)
8077 Don't use this routine (or the next one) if bumping the line
8078 counter is not sufficient to deal with newlines in the string.
8080 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8081 This feature is useful when processing a comment that is going to
8082 be processed or was processed at another point in the preprocessor,
8083 to avoid a duplicate warning. Likewise for unterminated comment
8087 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
8088 register FILE_BUF
*ip
;
8089 int *line_counter
; /* place to remember newlines, or NULL */
8092 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8093 register U_CHAR
*bp
= ip
->bufp
;
8094 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
8095 int start_line
= line_counter
? *line_counter
: 0;
8097 /* JF this line_counter stuff is a crock to make sure the
8098 comment is only put out once, no matter how many times
8099 the comment is skipped. It almost works */
8102 *op
->bufp
++ = bp
[-1];
8104 if (cplusplus_comments
&& bp
[-1] == '/') {
8105 for (; bp
< limit
; bp
++) {
8108 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
8110 if (!nowarn
&& warn_comments
)
8111 warning ("multiline `//' comment");
8123 #ifdef MULTIBYTE_CHARS
8125 length
= local_mblen (bp
, limit
- bp
);
8130 bcopy (bp
, op
->bufp
, length
- 1);
8131 op
->bufp
+= (length
- 1);
8143 while (bp
< limit
) {
8148 /* If this is the end of the file, we have an unterminated comment.
8149 Don't swallow the newline. We are guaranteed that there will be a
8150 trailing newline and various pieces assume it's there. */
8157 if (line_counter
!= NULL
)
8163 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
8164 warning ("`/*' within comment");
8174 #ifdef MULTIBYTE_CHARS
8179 length
= local_mblen (bp
, limit
- bp
);
8185 bcopy (bp
, op
->bufp
, length
);
8195 error_with_line (line_for_error (start_line
), "unterminated comment");
8200 /* Skip over a quoted string. BP points to the opening quote.
8201 Returns a pointer after the closing quote. Don't go past LIMIT.
8202 START_LINE is the line number of the starting point (but it need
8203 not be valid if the starting point is inside a macro expansion).
8205 The input stack state is not changed.
8207 If COUNT_NEWLINES is nonzero, it points to an int to increment
8208 for each newline passed; also, warn about any white space
8209 just before line end.
8211 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8212 if we pass a backslash-newline.
8214 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8217 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
8218 register const U_CHAR
*bp
;
8219 register const U_CHAR
*limit
;
8221 int *count_newlines
;
8222 int *backslash_newlines_p
;
8225 register U_CHAR c
, match
;
8230 error_with_line (line_for_error (start_line
),
8231 "unterminated string or character constant");
8232 error_with_line (multiline_string_line
,
8233 "possible real start of unterminated constant");
8234 multiline_string_line
= 0;
8241 while (*bp
== '\\' && bp
[1] == '\n') {
8242 if (backslash_newlines_p
)
8243 *backslash_newlines_p
= 1;
8249 if (backslash_newlines_p
)
8250 *backslash_newlines_p
= 1;
8255 } else if (c
== '\n') {
8257 /* Unterminated strings and character constants are 'valid'. */
8258 bp
--; /* Don't consume the newline. */
8263 if (match
== '\'') {
8264 error_with_line (line_for_error (start_line
),
8265 "unterminated character constant");
8271 /* If not traditional, then allow newlines inside strings. */
8272 if (count_newlines
) {
8273 if (warn_white_space
&& is_hor_space
[bp
[-2]])
8274 warning ("white space at end of line in string");
8277 if (multiline_string_line
== 0) {
8279 pedwarn_with_line (line_for_error (start_line
),
8280 "string constant runs past end of line");
8281 multiline_string_line
= start_line
;
8283 } else if (c
== match
)
8285 #ifdef MULTIBYTE_CHARS
8289 length
= local_mblen (bp
, limit
- bp
);
8296 return (U_CHAR
*) bp
;
8299 /* Place into DST a quoted string representing the string SRC.
8300 SRCLEN is the length of SRC; SRC may contain null bytes.
8301 Return the address of DST's terminating null. */
8304 quote_string (dst
, src
, srclen
)
8310 const char *srclim
= src
+ srclen
;
8313 while (src
!= srclim
)
8314 switch ((c
= *src
++))
8321 sprintf (dst
, "\\%03o", c
);
8338 /* Skip across a group of balanced parens, starting from IP->bufp.
8339 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8341 This does not handle newlines, because it's used for the arg of #if,
8342 where there aren't any newlines. Also, backslash-newline can't appear. */
8345 skip_paren_group (ip
)
8346 register FILE_BUF
*ip
;
8348 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8349 U_CHAR
*p
= ip
->bufp
;
8351 int lines_dummy
= 0;
8353 while (p
!= limit
) {
8363 return ip
->bufp
= p
;
8369 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
8377 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
8379 return ip
->bufp
= p
;
8389 /* Write out a #line directive, for instance, after an #include file.
8390 If CONDITIONAL is nonzero, we can omit the #line if it would
8391 appear to be a no-op, and we can output a few newlines instead
8392 if we want to increase the line number by a small amount.
8393 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8396 output_line_directive (ip
, op
, conditional
, file_change
)
8399 enum file_change_code file_change
;
8402 char *line_directive_buf
, *line_end
;
8404 if (no_line_directives
8405 || ip
->fname
== NULL
8407 op
->lineno
= ip
->lineno
;
8412 if (ip
->lineno
== op
->lineno
)
8415 /* If the inherited line number is a little too small,
8416 output some newlines instead of a #line directive. */
8417 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
8418 check_expand (op
, 10);
8419 while (ip
->lineno
> op
->lineno
) {
8427 /* Output a positive line number if possible. */
8428 while (ip
->lineno
<= 0 && ip
->bufp
- ip
->buf
< ip
->length
8429 && *ip
->bufp
== '\n') {
8434 line_directive_buf
= (char *) alloca (4 * ip
->nominal_fname_len
+ 100);
8435 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
8436 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
8437 ip
->nominal_fname
, ip
->nominal_fname_len
);
8438 if (file_change
!= same_file
) {
8440 *line_end
++ = file_change
== enter_file
? '1' : '2';
8442 /* Tell cc1 if following text comes from a system header file. */
8443 if (ip
->system_header_p
) {
8447 #ifndef NO_IMPLICIT_EXTERN_C
8448 /* Tell cc1plus if following text should be treated as C. */
8449 if (ip
->system_header_p
== 2 && cplusplus
) {
8455 len
= line_end
- line_directive_buf
;
8456 check_expand (op
, len
+ 1);
8457 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
8459 bcopy ((const PTR
) line_directive_buf
, (PTR
) op
->bufp
, len
);
8461 op
->lineno
= ip
->lineno
;
8464 /* This structure represents one parsed argument in a macro call.
8465 `raw' points to the argument text as written (`raw_length' is its length).
8466 `expanded' points to the argument's macro-expansion
8467 (its length is `expand_length', and its allocated size is `expand_size').
8468 `stringified_length_bound' is an upper bound on the length
8469 the argument would have if stringified.
8470 `use_count' is the number of times this macro arg is substituted
8471 into the macro. If the actual use count exceeds 10,
8472 the value stored is 10.
8473 `free1' and `free2', if nonzero, point to blocks to be freed
8474 when the macro argument data is no longer needed.
8475 `free_ptr', if nonzero, points to a value of instack[i].free_ptr
8476 where the raw field points somewhere into this string. The purpose
8477 of this is to hold onto instack[i].buf for macro arguments, even
8478 when the element has been popped off the input stack.
8482 U_CHAR
*raw
, *expanded
;
8483 int raw_length
, expand_length
, expand_size
;
8484 int stringified_length_bound
;
8485 U_CHAR
*free1
, *free2
;
8491 /* Expand a macro call.
8492 HP points to the symbol that is the macro being called.
8493 Put the result of expansion onto the input stack
8494 so that subsequent input by our caller will use it.
8496 If macro wants arguments, caller has already verified that
8497 an argument list follows; arguments come from the input stack. */
8500 macroexpand (hp
, op
)
8505 DEFINITION
*defn
= hp
->value
.defn
;
8506 register U_CHAR
*xbuf
;
8508 int start_line
= instack
[indepth
].lineno
;
8509 int rest_args
, rest_zero
;
8511 CHECK_DEPTH (return;);
8513 /* it might not actually be a macro. */
8514 if (hp
->type
!= T_MACRO
) {
8515 special_symbol (hp
, op
);
8519 /* This macro is being used inside a #if, which means it must be */
8520 /* recorded as a precondition. */
8521 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
8522 dump_single_macro (hp
, pcp_outfile
);
8524 nargs
= defn
->nargs
;
8528 struct argdata
*args
;
8529 int parse_error
= 0;
8531 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
8533 for (i
= 0; i
< nargs
; i
++) {
8534 args
[i
].raw
= (U_CHAR
*) "";
8535 args
[i
].expanded
= 0;
8536 args
[i
].raw_length
= args
[i
].expand_length
= args
[i
].expand_size
8537 = args
[i
].stringified_length_bound
= 0;
8538 args
[i
].free1
= args
[i
].free2
= 0;
8539 args
[i
].free_ptr
= 0;
8540 args
[i
].use_count
= 0;
8543 /* Parse all the macro args that are supplied. I counts them.
8544 The first NARGS args are stored in ARGS.
8545 The rest are discarded.
8546 If rest_args is set then we assume macarg absorbed the rest of the args.
8551 /* Discard the open-parenthesis or comma before the next arg. */
8552 ++instack
[indepth
].bufp
;
8555 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
8556 /* If we are working on last arg which absorbs rest of args... */
8557 if (i
== nargs
- 1 && defn
->rest_args
)
8559 parse_error
= macarg (&args
[i
], rest_args
);
8562 parse_error
= macarg (NULL_PTR
, 0);
8564 error_with_line (line_for_error (start_line
),
8565 "unterminated macro call");
8569 } while (*instack
[indepth
].bufp
!= ')');
8571 /* If we got one arg but it was just whitespace, call that 0 args. */
8573 register const U_CHAR
*bp
= args
[0].raw
;
8574 register const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
8575 /* cpp.texi says for foo ( ) we provide one argument.
8576 However, if foo wants just 0 arguments, treat this as 0. */
8578 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8583 /* Don't output an error message if we have already output one for
8584 a parse error above. */
8586 if (nargs
== 0 && i
> 0) {
8588 error ("arguments given to macro `%s'", hp
->name
);
8589 } else if (i
< nargs
) {
8590 /* traditional C allows foo() if foo wants one argument. */
8591 if (nargs
== 1 && i
== 0 && traditional
)
8593 /* the rest args token is allowed to absorb 0 tokens */
8594 else if (i
== nargs
- 1 && defn
->rest_args
)
8596 else if (parse_error
)
8599 error ("macro `%s' used without args", hp
->name
);
8601 error ("macro `%s' used with just one arg", hp
->name
);
8603 error ("macro `%s' used with only %d args", hp
->name
, i
);
8604 } else if (i
> nargs
) {
8606 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8609 /* Swallow the closeparen. */
8610 ++instack
[indepth
].bufp
;
8612 /* If macro wants zero args, we parsed the arglist for checking only.
8613 Read directly from the macro definition. */
8615 xbuf
= defn
->expansion
;
8616 xbuf_len
= defn
->length
;
8618 register U_CHAR
*exp
= defn
->expansion
;
8619 register int offset
; /* offset in expansion,
8620 copied a piece at a time */
8621 register int totlen
; /* total amount of exp buffer filled so far */
8623 register struct reflist
*ap
, *last_ap
;
8625 /* Macro really takes args. Compute the expansion of this call. */
8627 /* Compute length in characters of the macro's expansion.
8628 Also count number of times each arg is used. */
8629 xbuf_len
= defn
->length
;
8630 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8631 if (ap
->stringify
&& args
[ap
->argno
].stringified_length_bound
== 0)
8632 /* macarg is not called for omitted arguments, as a result
8633 stringified_length_bound will be zero. We need to make
8634 enough space for "". */
8636 else if (ap
->stringify
)
8637 xbuf_len
+= args
[ap
->argno
].stringified_length_bound
;
8638 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8639 /* Add 4 for two newline-space markers to prevent
8640 token concatenation. */
8641 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8643 /* We have an ordinary (expanded) occurrence of the arg.
8644 So compute its expansion, if we have not already. */
8645 if (args
[ap
->argno
].expanded
== 0) {
8647 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8648 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8651 args
[ap
->argno
].expanded
= obuf
.buf
;
8652 args
[ap
->argno
].expand_length
= obuf
.bufp
- obuf
.buf
;
8653 args
[ap
->argno
].expand_size
= obuf
.length
;
8654 args
[ap
->argno
].free2
= obuf
.buf
;
8656 xbuf_len
+= args
[ap
->argno
].expand_length
;
8658 /* If the arg appears more than once, its later occurrences
8659 may have newline turned into backslash-'n', which is a
8660 factor of 2 expansion. */
8661 xbuf_len
+= 2 * args
[ap
->argno
].expand_length
;
8663 /* Add 4 for two newline-space markers to prevent
8664 token concatenation. */
8667 if (args
[ap
->argno
].use_count
< 10)
8668 args
[ap
->argno
].use_count
++;
8671 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8673 /* Generate in XBUF the complete expansion
8674 with arguments substituted in.
8675 TOTLEN is the total size generated so far.
8676 OFFSET is the index in the definition
8677 of where we are copying from. */
8678 offset
= totlen
= 0;
8679 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8680 last_ap
= ap
, ap
= ap
->next
) {
8681 register struct argdata
*arg
= &args
[ap
->argno
];
8682 int count_before
= totlen
;
8684 /* Add chars to XBUF. */
8685 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8686 xbuf
[totlen
++] = exp
[offset
];
8688 /* If followed by an empty rest arg with concatenation,
8689 delete the last run of nonwhite chars. */
8690 if (rest_zero
&& totlen
> count_before
8691 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8692 || (last_ap
!= NULL
&& last_ap
->rest_args
8693 && last_ap
->raw_after
!= 0))) {
8694 /* Delete final whitespace. */
8695 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8699 /* Delete the nonwhites before them. */
8700 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8705 if (ap
->stringify
!= 0) {
8706 int arglen
= arg
->raw_length
;
8712 && (c
= arg
->raw
[i
], is_space
[c
]))
8715 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8718 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8719 for (; i
< arglen
; i
++) {
8723 /* Generate nothing for backslash-newline in a string. */
8724 if (c
== '\\' && arg
->raw
[i
+ 1] == '\n') {
8730 generate nothing for a stringified argument. */
8736 /* Internal sequences of whitespace are replaced by one space
8737 except within a string or char token. */
8740 while (is_space
[(c
= arg
->raw
[i
])])
8741 /* Newline markers can occur within a whitespace sequence;
8742 consider them part of the sequence. */
8743 i
+= (c
== '\n') + 1;
8754 else if (in_string
) {
8759 #ifdef MULTIBYTE_CHARS
8761 length
= local_mblen (arg
->raw
+ i
, arglen
- i
);
8764 bcopy (arg
->raw
+ i
, xbuf
+ totlen
, length
);
8771 } else if (c
== '\"' || c
== '\'')
8775 /* Escape double-quote, and backslashes in strings.
8776 Newlines in strings are best escaped as \n, since
8777 otherwise backslash-backslash-newline-newline is
8778 mishandled. The C Standard doesn't allow newlines in
8779 strings, so we can escape newlines as we please. */
8783 || (c
== '\n' ? (c
= 'n', 1) : 0))))
8784 xbuf
[totlen
++] = '\\';
8785 /* We used to output e.g. \008 for control characters here,
8786 but this doesn't conform to the C Standard.
8787 Just output the characters as-is. */
8791 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8792 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8793 const U_CHAR
*p1
= arg
->raw
;
8794 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
8795 if (ap
->raw_before
!= 0) {
8796 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8797 while (p1
!= l1
&& is_idchar
[*p1
])
8798 xbuf
[totlen
++] = *p1
++;
8799 /* Delete any no-reexpansion marker that follows
8800 an identifier at the beginning of the argument
8801 if the argument is concatenated with what precedes it. */
8802 if (p1
[0] == '\n' && p1
[1] == '-')
8804 } else if (!traditional
) {
8805 /* Ordinary expanded use of the argument.
8806 Put in newline-space markers to prevent token pasting. */
8807 xbuf
[totlen
++] = '\n';
8808 xbuf
[totlen
++] = ' ';
8810 if (ap
->raw_after
!= 0) {
8811 /* Arg is concatenated after: delete trailing whitespace,
8812 whitespace markers, and no-reexpansion markers. */
8814 if (is_space
[l1
[-1]]) l1
--;
8815 else if (l1
[-1] == '-') {
8816 const U_CHAR
*p2
= l1
- 1;
8817 /* If a `-' is preceded by an odd number of newlines then it
8818 and the last newline are a no-reexpansion marker. */
8819 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8820 if ((l1
- 1 - p2
) & 1) {
8829 bcopy ((const PTR
) p1
, (PTR
) (xbuf
+ totlen
), l1
- p1
);
8831 if (!traditional
&& ap
->raw_after
== 0) {
8832 /* Ordinary expanded use of the argument.
8833 Put in newline-space markers to prevent token pasting. */
8834 xbuf
[totlen
++] = '\n';
8835 xbuf
[totlen
++] = ' ';
8838 /* Ordinary expanded use of the argument.
8839 Put in newline-space markers to prevent token pasting. */
8841 xbuf
[totlen
++] = '\n';
8842 xbuf
[totlen
++] = ' ';
8844 bcopy ((const PTR
) arg
->expanded
, (PTR
) (xbuf
+ totlen
),
8845 arg
->expand_length
);
8846 totlen
+= arg
->expand_length
;
8848 xbuf
[totlen
++] = '\n';
8849 xbuf
[totlen
++] = ' ';
8851 /* If a macro argument with newlines is used multiple times,
8852 then only expand the newlines once. This avoids creating output
8853 lines which don't correspond to any input line, which confuses
8855 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8856 /* Don't bother doing change_newlines for subsequent
8859 change_newlines (arg
);
8863 if (totlen
> xbuf_len
)
8867 /* If there is anything left of the definition after handling
8868 the arg list, copy that in too. */
8870 for (i
= offset
; i
< defn
->length
; i
++) {
8871 /* if we've reached the end of the macro */
8874 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8875 && last_ap
->raw_after
!= 0))
8876 xbuf
[totlen
++] = exp
[i
];
8882 for (i
= 0; i
< nargs
; i
++) {
8883 if (args
[i
].free_ptr
!= 0) {
8884 U_CHAR
*buf
= args
[i
].free_ptr
;
8886 for (d
= indepth
; d
>= 0; --d
) {
8887 if (instack
[d
].buf
== buf
) {
8888 instack
[d
].free_ptr
= buf
; /* Give ownership back to instack */
8892 free (buf
); /* buf is not on the stack; must have been popped */
8895 if (args
[i
].free1
!= 0)
8896 free (args
[i
].free1
);
8897 if (args
[i
].free2
!= 0)
8898 free (args
[i
].free2
);
8902 xbuf
= defn
->expansion
;
8903 xbuf_len
= defn
->length
;
8906 /* Now put the expansion on the input stack
8907 so our caller will commence reading from it. */
8909 register FILE_BUF
*ip2
;
8911 ip2
= &instack
[++indepth
];
8914 ip2
->nominal_fname
= 0;
8915 ip2
->nominal_fname_len
= 0;
8917 /* This may not be exactly correct, but will give much better error
8918 messages for nested macro calls than using a line number of zero. */
8919 ip2
->lineno
= start_line
;
8921 ip2
->length
= xbuf_len
;
8923 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8925 ip2
->if_stack
= if_stack
;
8926 ip2
->system_header_p
= 0;
8928 /* Recursive macro use sometimes works traditionally.
8929 #define foo(x,y) bar (x (y,0), y)
8933 hp
->type
= T_DISABLED
;
8937 /* Parse a macro argument and store the info on it into *ARGPTR.
8938 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8939 Return nonzero to indicate a syntax error. */
8942 macarg (argptr
, rest_args
)
8943 register struct argdata
*argptr
;
8946 FILE_BUF
*ip
= &instack
[indepth
];
8948 int lineno0
= ip
->lineno
;
8952 /* Try to parse as much of the argument as exists at this
8953 input stack level. */
8954 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
,
8955 &paren
, &ip
->lineno
, &comments
, rest_args
);
8957 /* If we find the end of the argument at this level,
8958 set up *ARGPTR to point at it in the input stack. */
8959 if (!(ip
->fname
!= 0 && (ip
->lineno
!= lineno0
|| comments
!= 0))
8960 && bp
!= ip
->buf
+ ip
->length
) {
8962 argptr
->raw
= ip
->bufp
;
8963 argptr
->raw_length
= bp
- ip
->bufp
;
8964 argptr
->newlines
= ip
->lineno
- lineno0
;
8965 /* The next two statements transfer ownership of the the buffer
8966 from ip to argptr. Note that the second statement ensures that
8967 a given free_ptr is owned by at most one macro argument. */
8968 argptr
->free_ptr
= ip
->free_ptr
;
8973 /* This input stack level ends before the macro argument does.
8974 We must pop levels and keep parsing.
8975 Therefore, we must allocate a temporary buffer and copy
8976 the macro argument into it. */
8977 int bufsize
= bp
- ip
->bufp
;
8978 int extra
= ip
->lineno
- lineno0
;
8979 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8980 int final_start
= 0;
8982 bcopy ((const PTR
) ip
->bufp
, (PTR
) buffer
, bufsize
);
8985 while (bp
== ip
->buf
+ ip
->length
) {
8986 if (instack
[indepth
].macro
== 0) {
8990 ip
->macro
->type
= T_MACRO
;
8992 free (ip
->free_ptr
);
8993 ip
= &instack
[--indepth
];
8994 lineno0
= ip
->lineno
;
8996 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
, &paren
,
8997 &ip
->lineno
, &comments
, rest_args
);
8998 final_start
= bufsize
;
8999 bufsize
+= bp
- ip
->bufp
;
9000 extra
+= ip
->lineno
- lineno0
;
9001 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
9002 bcopy ((const PTR
) ip
->bufp
, (PTR
) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
9007 /* Now, if arg is actually wanted, record its raw form,
9008 discarding comments and duplicating newlines in whatever
9009 part of it did not come from a macro expansion.
9010 EXTRA space has been preallocated for duplicating the newlines.
9011 FINAL_START is the index of the start of that part. */
9013 argptr
->raw
= buffer
;
9014 argptr
->raw_length
= bufsize
;
9015 argptr
->free1
= buffer
;
9016 argptr
->newlines
= ip
->lineno
- lineno0
;
9017 if ((argptr
->newlines
|| comments
) && ip
->fname
!= 0)
9020 discard_comments (argptr
->raw
+ final_start
,
9021 argptr
->raw_length
- final_start
,
9023 argptr
->raw
[argptr
->raw_length
] = 0;
9024 if (argptr
->raw_length
> bufsize
+ extra
)
9029 /* If we are not discarding this argument,
9030 macroexpand it and compute its length as stringified.
9031 All this info goes into *ARGPTR. */
9034 register const U_CHAR
*buf
, *lim
;
9035 register int totlen
;
9038 lim
= buf
+ argptr
->raw_length
;
9040 while (buf
!= lim
&& is_space
[*buf
])
9042 while (buf
!= lim
&& is_space
[lim
[-1]])
9044 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
9045 while (buf
!= lim
) {
9046 register U_CHAR c
= *buf
++;
9048 /* Internal sequences of whitespace are replaced by one space
9049 in most cases, but not always. So count all the whitespace
9050 in case we need to keep it all. */
9053 SKIP_ALL_WHITE_SPACE (buf
);
9056 if (c
== '\"' || c
== '\\' || c
== '\n') /* escape these chars */
9059 argptr
->stringified_length_bound
= totlen
;
9064 /* Scan text from START (inclusive) up to LIMIT (exclusive),
9065 taken from the expansion of MACRO,
9066 counting parens in *DEPTHPTR,
9067 and return if reach LIMIT
9068 or before a `)' that would make *DEPTHPTR negative
9069 or before a comma when *DEPTHPTR is zero.
9070 Single and double quotes are matched and termination
9071 is inhibited within them. Comments also inhibit it.
9072 Value returned is pointer to stopping place.
9074 Increment *NEWLINES each time a newline is passed.
9075 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
9076 Set *COMMENTS to 1 if a comment is seen. */
9079 macarg1 (start
, limit
, macro
, depthptr
, newlines
, comments
, rest_args
)
9081 register const U_CHAR
*limit
;
9082 struct hashnode
*macro
;
9083 int *depthptr
, *newlines
, *comments
;
9086 register U_CHAR
*bp
= start
;
9088 while (bp
< limit
) {
9094 if (--(*depthptr
) < 0)
9098 /* Traditionally, backslash makes following char not special. */
9099 if (traditional
&& bp
+ 1 < limit
&& bp
[1] != '\n')
9109 newline_fix (bp
+ 1);
9112 for (bp
+= 2; bp
< limit
; bp
++) {
9115 else if (*bp
== '*') {
9116 if (bp
[-1] == '/' && warn_comments
)
9117 warning ("`/*' within comment");
9119 newline_fix (bp
+ 1);
9127 #ifdef MULTIBYTE_CHARS
9129 length
= local_mblen (bp
, limit
- bp
);
9135 } else if (bp
[1] == '/' && cplusplus_comments
) {
9137 for (bp
+= 2; bp
< limit
; bp
++) {
9142 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
9146 warning ("multiline `//' comment");
9151 #ifdef MULTIBYTE_CHARS
9153 length
= local_mblen (bp
, limit
- bp
);
9165 for (quotec
= *bp
++; bp
< limit
&& *bp
!= quotec
; bp
++) {
9170 while (*bp
== '\\' && bp
[1] == '\n') {
9174 } else if (*bp
== '\n') {
9175 if (warn_white_space
&& is_hor_space
[bp
[-1]] && ! macro
)
9176 warning ("white space at end of line in string");
9183 #ifdef MULTIBYTE_CHARS
9185 length
= local_mblen (bp
, limit
- bp
);
9194 /* if we've returned to lowest level and we aren't absorbing all args */
9195 if ((*depthptr
) == 0 && rest_args
== 0)
9205 /* Discard comments and duplicate newlines
9206 in the string of length LENGTH at START,
9207 except inside of string constants.
9208 The string is copied into itself with its beginning staying fixed.
9210 NEWLINES is the number of newlines that must be duplicated.
9211 We assume that that much extra space is available past the end
9215 discard_comments (start
, length
, newlines
)
9220 register U_CHAR
*ibp
;
9221 register U_CHAR
*obp
;
9222 register U_CHAR
*limit
;
9225 /* If we have newlines to duplicate, copy everything
9226 that many characters up. Then, in the second part,
9227 we will have room to insert the newlines
9229 NEWLINES may actually be too large, because it counts
9230 newlines in string constants, and we don't duplicate those.
9231 But that does no harm. */
9233 ibp
= start
+ length
;
9234 obp
= ibp
+ newlines
;
9236 while (limit
!= ibp
)
9240 ibp
= start
+ newlines
;
9241 limit
= start
+ length
+ newlines
;
9244 while (ibp
< limit
) {
9245 *obp
++ = c
= *ibp
++;
9248 /* Duplicate the newline. */
9262 /* Delete any comment. */
9263 if (cplusplus_comments
&& ibp
[0] == '/') {
9264 /* Comments are equivalent to spaces. */
9271 if (*ibp
== '\\' && ibp
+ 1 < limit
&& ibp
[1] == '\n')
9275 #ifdef MULTIBYTE_CHARS
9276 int length
= local_mblen (ibp
, limit
- ibp
);
9278 ibp
+= (length
- 1);
9285 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
9287 /* Comments are equivalent to spaces.
9288 For -traditional, a comment is equivalent to nothing. */
9293 while (++ibp
< limit
) {
9294 if (ibp
[0] == '*') {
9296 newline_fix (ibp
+ 1);
9297 if (ibp
[1] == '/') {
9304 #ifdef MULTIBYTE_CHARS
9305 int length
= local_mblen (ibp
, limit
- ibp
);
9307 ibp
+= (length
- 1);
9315 /* Notice and skip strings, so that we don't
9316 think that comments start inside them,
9317 and so we don't duplicate newlines in them. */
9320 while (ibp
< limit
) {
9321 *obp
++ = c
= *ibp
++;
9329 else if (c
== '\\') {
9330 if (ibp
< limit
&& *ibp
== '\n') {
9334 while (*ibp
== '\\' && ibp
[1] == '\n')
9342 #ifdef MULTIBYTE_CHARS
9345 length
= local_mblen (ibp
, limit
- ibp
);
9349 bcopy (ibp
, obp
, length
);
9366 /* Turn newlines to spaces in the macro argument ARG.
9367 Remove backslash-newline from string constants,
9368 and turn other newlines in string constants to backslash-'n'. */
9371 change_newlines (arg
)
9372 struct argdata
*arg
;
9374 U_CHAR
*start
= arg
->expanded
;
9375 int length
= arg
->expand_length
;
9376 register U_CHAR
*ibp
;
9377 register U_CHAR
*obp
;
9378 register const U_CHAR
*limit
;
9382 limit
= start
+ length
;
9385 while (ibp
< limit
) {
9386 *obp
++ = c
= *ibp
++;
9389 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9390 string. Skip past the newline and its duplicate.
9391 Put a space in the output. */
9402 /* Notice and skip strings, so that we don't delete newlines in them. */
9405 while (ibp
< limit
) {
9406 *obp
++ = c
= *ibp
++;
9409 else if (c
== '\\' && ibp
< limit
&& *ibp
== '\n')
9418 #ifdef MULTIBYTE_CHARS
9421 length
= local_mblen (ibp
, limit
- ibp
);
9425 bcopy (ibp
, obp
, length
);
9439 arg
->expand_length
= obp
- arg
->expanded
;
9441 if (start
!= arg
->expanded
)
9445 /* notice - output message to stderr */
9448 notice
VPARAMS ((const char * msgid
, ...))
9450 #ifndef ANSI_PROTOTYPES
9455 VA_START (args
, msgid
);
9457 #ifndef ANSI_PROTOTYPES
9458 msgid
= va_arg (args
, const char *);
9461 vnotice (msgid
, args
);
9466 vnotice (msgid
, args
)
9470 vfprintf (stderr
, _(msgid
), args
);
9473 /* error - print error message and increment count of errors. */
9476 error
VPARAMS ((const char * msgid
, ...))
9478 #ifndef ANSI_PROTOTYPES
9483 VA_START (args
, msgid
);
9485 #ifndef ANSI_PROTOTYPES
9486 msgid
= va_arg (args
, const char *);
9489 verror (msgid
, args
);
9494 verror (msgid
, args
)
9499 FILE_BUF
*ip
= NULL
;
9501 print_containing_files ();
9503 for (i
= indepth
; i
>= 0; i
--)
9504 if (instack
[i
].fname
!= NULL
) {
9510 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9511 ip
->nominal_fname_len
, stderr
);
9512 fprintf (stderr
, ":%d: ", ip
->lineno
);
9514 vnotice (msgid
, args
);
9515 fprintf (stderr
, "\n");
9519 /* Error including a message from `errno'. */
9522 error_from_errno (name
)
9527 FILE_BUF
*ip
= NULL
;
9529 print_containing_files ();
9531 for (i
= indepth
; i
>= 0; i
--)
9532 if (instack
[i
].fname
!= NULL
) {
9538 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9539 ip
->nominal_fname_len
, stderr
);
9540 fprintf (stderr
, ":%d: ", ip
->lineno
);
9543 fprintf (stderr
, "%s: %s\n", name
, xstrerror (e
));
9548 /* Print error message but don't count it. */
9551 warning
VPARAMS ((const char * msgid
, ...))
9553 #ifndef ANSI_PROTOTYPES
9558 VA_START (args
, msgid
);
9560 #ifndef ANSI_PROTOTYPES
9561 msgid
= va_arg (args
, const char *);
9564 vwarning (msgid
, args
);
9569 vwarning (msgid
, args
)
9574 FILE_BUF
*ip
= NULL
;
9576 if (inhibit_warnings
)
9579 if (warnings_are_errors
)
9582 print_containing_files ();
9584 for (i
= indepth
; i
>= 0; i
--)
9585 if (instack
[i
].fname
!= NULL
) {
9591 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9592 ip
->nominal_fname_len
, stderr
);
9593 fprintf (stderr
, ":%d: ", ip
->lineno
);
9595 notice ("warning: ");
9596 vnotice (msgid
, args
);
9597 fprintf (stderr
, "\n");
9601 error_with_line
VPARAMS ((int line
, const char * msgid
, ...))
9603 #ifndef ANSI_PROTOTYPES
9609 VA_START (args
, msgid
);
9611 #ifndef ANSI_PROTOTYPES
9612 line
= va_arg (args
, int);
9613 msgid
= va_arg (args
, const char *);
9616 verror_with_line (line
, msgid
, args
);
9622 verror_with_line (line
, msgid
, args
)
9628 FILE_BUF
*ip
= NULL
;
9630 print_containing_files ();
9632 for (i
= indepth
; i
>= 0; i
--)
9633 if (instack
[i
].fname
!= NULL
) {
9639 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9640 ip
->nominal_fname_len
, stderr
);
9641 fprintf (stderr
, ":%d: ", line
);
9643 vnotice (msgid
, args
);
9644 fprintf (stderr
, "\n");
9649 warning_with_line
VPARAMS ((int line
, const char * msgid
, ...))
9651 #ifndef ANSI_PROTOTYPES
9657 VA_START (args
, msgid
);
9659 #ifndef ANSI_PROTOTYPES
9660 line
= va_arg (args
, int);
9661 msgid
= va_arg (args
, const char *);
9664 vwarning_with_line (line
, msgid
, args
);
9669 vwarning_with_line (line
, msgid
, args
)
9675 FILE_BUF
*ip
= NULL
;
9677 if (inhibit_warnings
)
9680 if (warnings_are_errors
)
9683 print_containing_files ();
9685 for (i
= indepth
; i
>= 0; i
--)
9686 if (instack
[i
].fname
!= NULL
) {
9692 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9693 ip
->nominal_fname_len
, stderr
);
9695 fprintf (stderr
, ":%d: ", line
);
9697 fputs (": ", stderr
);
9699 notice ("warning: ");
9700 vnotice (msgid
, args
);
9701 fprintf (stderr
, "\n");
9704 /* Print an error message and maybe count it. */
9707 pedwarn
VPARAMS ((const char * msgid
, ...))
9709 #ifndef ANSI_PROTOTYPES
9714 VA_START (args
, msgid
);
9716 #ifndef ANSI_PROTOTYPES
9717 msgid
= va_arg (args
, const char *);
9720 if (pedantic_errors
)
9721 verror (msgid
, args
);
9723 vwarning (msgid
, args
);
9728 pedwarn_with_line
VPARAMS ((int line
, const char * msgid
, ...))
9730 #ifndef ANSI_PROTOTYPES
9736 VA_START (args
, msgid
);
9738 #ifndef ANSI_PROTOTYPES
9739 line
= va_arg (args
, int);
9740 msgid
= va_arg (args
, const char *);
9743 if (pedantic_errors
)
9744 verror_with_line (line
, msgid
, args
);
9746 vwarning_with_line (line
, msgid
, args
);
9750 /* Report a warning (or an error if pedantic_errors)
9751 giving specified file name and line number, not current. */
9754 pedwarn_with_file_and_line
VPARAMS ((const char *file
, size_t file_len
,
9755 int line
, const char * msgid
, ...))
9757 #ifndef ANSI_PROTOTYPES
9765 if (!pedantic_errors
&& inhibit_warnings
)
9768 VA_START (args
, msgid
);
9770 #ifndef ANSI_PROTOTYPES
9771 file
= va_arg (args
, const char *);
9772 file_len
= va_arg (args
, size_t);
9773 line
= va_arg (args
, int);
9774 msgid
= va_arg (args
, const char *);
9778 fwrite (file
, sizeof file
[0], file_len
, stderr
);
9779 fprintf (stderr
, ":%d: ", line
);
9781 if (pedantic_errors
)
9783 if (!pedantic_errors
)
9784 notice ("warning: ");
9785 vnotice (msgid
, args
);
9787 fprintf (stderr
, "\n");
9791 pedwarn_strange_white_space (ch
)
9796 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9797 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9798 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9803 /* Print the file names and line numbers of the #include
9804 directives which led to the current file. */
9807 print_containing_files ()
9809 FILE_BUF
*ip
= NULL
;
9813 /* If stack of files hasn't changed since we last printed
9814 this info, don't repeat it. */
9815 if (last_error_tick
== input_file_stack_tick
)
9818 for (i
= indepth
; i
>= 0; i
--)
9819 if (instack
[i
].fname
!= NULL
) {
9824 /* Give up if we don't find a source file. */
9828 /* Find the other, outer source files. */
9829 for (i
--; i
>= 0; i
--)
9830 if (instack
[i
].fname
!= NULL
) {
9834 notice ( "In file included from ");
9836 notice (",\n from ");
9839 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9840 ip
->nominal_fname_len
, stderr
);
9841 fprintf (stderr
, ":%d", ip
->lineno
);
9844 fprintf (stderr
, ":\n");
9846 /* Record we have printed the status as of this time. */
9847 last_error_tick
= input_file_stack_tick
;
9850 /* Return the line at which an error occurred.
9851 The error is not necessarily associated with the current spot
9852 in the input stack, so LINE says where. LINE will have been
9853 copied from ip->lineno for the current input level.
9854 If the current level is for a file, we return LINE.
9855 But if the current level is not for a file, LINE is meaningless.
9856 In that case, we return the lineno of the innermost file. */
9859 line_for_error (line
)
9865 for (i
= indepth
; i
>= 0; ) {
9866 if (instack
[i
].fname
!= 0)
9871 line1
= instack
[i
].lineno
;
9879 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9881 * As things stand, nothing is ever placed in the output buffer to be
9882 * removed again except when it's KNOWN to be part of an identifier,
9883 * so flushing and moving down everything left, instead of expanding,
9887 /* You might think void was cleaner for the return type,
9888 but that would get type mismatch in check_expand in strict ANSI. */
9891 grow_outbuf (obuf
, needed
)
9892 register FILE_BUF
*obuf
;
9893 register int needed
;
9898 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9901 /* Make it at least twice as big as it is now. */
9903 /* Make it have at least 150% of the free space we will need. */
9904 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9905 if (minsize
> obuf
->length
)
9906 obuf
->length
= minsize
;
9908 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
9910 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9916 /* Symbol table for macro names and special symbols */
9919 * install a name in the main hash table, even if it is already there.
9920 * name stops with first non alphanumeric, except leading '#'.
9921 * caller must check against redefinition if that is desired.
9922 * delete_macro () removes things installed by install () in fifo order.
9923 * this is important because of the `defined' special symbol used
9924 * in #if, and also if pushdef/popdef directives are ever implemented.
9926 * If LEN is >= 0, it is the length of the name.
9927 * Otherwise, compute the length by scanning the entire name.
9929 * If HASH is >= 0, it is the precomputed hash code.
9930 * Otherwise, compute the hash code.
9934 install (name
, len
, type
, value
, hash
)
9937 enum node_type type
;
9941 register HASHNODE
*hp
;
9942 register int i
, bucket
;
9944 register const U_CHAR
*q
;
9948 while (is_idchar
[*q
])
9954 hash
= hashf (name
, len
, HASHSIZE
);
9956 i
= sizeof (HASHNODE
) + len
+ 1;
9957 hp
= (HASHNODE
*) xmalloc (i
);
9959 hp
->bucket_hdr
= &hashtab
[bucket
];
9960 hp
->next
= hashtab
[bucket
];
9961 hashtab
[bucket
] = hp
;
9963 if (hp
->next
!= NULL
)
9964 hp
->next
->prev
= hp
;
9967 hp
->value
.cpval
= value
;
9968 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9971 for (i
= 0; i
< len
; i
++)
9978 * find the most recent hash node for name "name" (ending with first
9979 * non-identifier char) installed by install
9981 * If LEN is >= 0, it is the length of the name.
9982 * Otherwise, compute the length by scanning the entire name.
9984 * If HASH is >= 0, it is the precomputed hash code.
9985 * Otherwise, compute the hash code.
9989 lookup (name
, len
, hash
)
9994 register const U_CHAR
*bp
;
9995 register HASHNODE
*bucket
;
9998 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
10003 hash
= hashf (name
, len
, HASHSIZE
);
10005 bucket
= hashtab
[hash
];
10007 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
10009 bucket
= bucket
->next
;
10015 * Delete a hash node. Some weirdness to free junk from macros.
10016 * More such weirdness will have to be added if you define more hash
10017 * types that need it.
10020 /* Note that the DEFINITION of a macro is removed from the hash table
10021 but its storage is not freed. This would be a storage leak
10022 except that it is not reasonable to keep undefining and redefining
10023 large numbers of macros many times.
10024 In any case, this is necessary, because a macro can be #undef'd
10025 in the middle of reading the arguments to a call to it.
10026 If #undef freed the DEFINITION, that would crash. */
10033 if (hp
->prev
!= NULL
)
10034 hp
->prev
->next
= hp
->next
;
10035 if (hp
->next
!= NULL
)
10036 hp
->next
->prev
= hp
->prev
;
10038 /* Make sure that the bucket chain header that the deleted guy was
10039 on points to the right thing afterwards. */
10040 if (hp
== *hp
->bucket_hdr
)
10041 *hp
->bucket_hdr
= hp
->next
;
10044 if (hp
->type
== T_MACRO
) {
10045 DEFINITION
*d
= hp
->value
.defn
;
10046 struct reflist
*ap
, *nextap
;
10048 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
10059 * return hash function on name. must be compatible with the one
10060 * computed a step at a time, elsewhere
10064 hashf (name
, len
, hashsize
)
10065 register const U_CHAR
*name
;
10069 register int r
= 0;
10072 r
= HASHSTEP (r
, *name
++);
10074 return MAKE_POS (r
) % hashsize
;
10078 /* Dump the definition of a single macro HP to OF. */
10081 dump_single_macro (hp
, of
)
10082 register HASHNODE
*hp
;
10085 register DEFINITION
*defn
= hp
->value
.defn
;
10086 struct reflist
*ap
;
10091 /* Print the definition of the macro HP. */
10093 fprintf (of
, "#define %s", hp
->name
);
10095 if (defn
->nargs
>= 0) {
10099 for (i
= 0; i
< defn
->nargs
; i
++) {
10100 dump_arg_n (defn
, i
, of
);
10101 if (i
+ 1 < defn
->nargs
)
10102 fprintf (of
, ", ");
10111 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
10112 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
10113 offset
+= ap
->nchars
;
10114 if (!traditional
) {
10115 if (ap
->nchars
!= 0)
10117 if (ap
->stringify
) {
10118 switch (ap
->stringify
) {
10119 case SHARP_TOKEN
: fprintf (of
, "#"); break;
10120 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
10121 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
10122 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
10126 if (ap
->raw_before
!= 0) {
10128 switch (ap
->raw_before
) {
10129 case WHITE_SHARP_TOKEN
:
10130 case WHITE_PERCENT_COLON_TOKEN
:
10137 switch (ap
->raw_before
) {
10138 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10139 case WHITE_SHARP_TOKEN
: fprintf (of
, "## "); break;
10140 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10141 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%: "); break;
10148 dump_arg_n (defn
, ap
->argno
, of
);
10149 if (!traditional
&& ap
->raw_after
!= 0) {
10150 switch (ap
->raw_after
) {
10151 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10152 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
10153 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10154 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
10160 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
10161 fprintf (of
, "\n");
10164 /* Dump all macro definitions as #defines to stdout. */
10171 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
10172 register HASHNODE
*hp
;
10174 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
10175 if (hp
->type
== T_MACRO
)
10176 dump_single_macro (hp
, stdout
);
10181 /* Output to OF a substring of a macro definition.
10182 BASE is the beginning of the definition.
10183 Output characters START thru LENGTH.
10184 Unless traditional, discard newlines outside of strings, thus
10185 converting funny-space markers to ordinary spaces. */
10188 dump_defn_1 (base
, start
, length
, of
)
10189 const U_CHAR
*base
;
10194 const U_CHAR
*p
= base
+ start
;
10195 const U_CHAR
*limit
= base
+ start
+ length
;
10198 fwrite (p
, sizeof (*p
), length
, of
);
10200 while (p
< limit
) {
10201 if (*p
== '\"' || *p
=='\'') {
10202 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
10203 NULL_PTR
, NULL_PTR
);
10204 fwrite (p
, sizeof (*p
), p1
- p
, of
);
10215 /* Print the name of argument number ARGNUM of macro definition DEFN
10217 Recall that DEFN->args.argnames contains all the arg names
10218 concatenated in reverse order with comma-space in between. */
10221 dump_arg_n (defn
, argnum
, of
)
10226 register U_CHAR
*p
= defn
->args
.argnames
;
10227 while (argnum
+ 1 < defn
->nargs
) {
10228 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
10232 while (*p
&& *p
!= ',') {
10238 /* Initialize syntactic classifications of characters. */
10241 initialize_char_syntax ()
10246 * Set up is_idchar and is_idstart tables. These should be
10247 * faster than saying (is_alpha (c) || c == '_'), etc.
10248 * Set up these things before calling any routines tthat
10251 for (i
= 'a'; i
<= 'z'; i
++) {
10252 is_idchar
[TOUPPER(i
)] = 1;
10254 is_idstart
[TOUPPER(i
)] = 1;
10257 for (i
= '0'; i
<= '9'; i
++)
10259 is_idchar
['_'] = 1;
10260 is_idstart
['_'] = 1;
10261 is_idchar
['$'] = 1;
10262 is_idstart
['$'] = 1;
10264 /* horizontal space table */
10265 is_hor_space
[' '] = 1;
10266 is_hor_space
['\t'] = 1;
10267 is_hor_space
['\v'] = 1;
10268 is_hor_space
['\f'] = 1;
10269 is_hor_space
['\r'] = 1;
10272 is_space
['\t'] = 1;
10273 is_space
['\v'] = 1;
10274 is_space
['\f'] = 1;
10275 is_space
['\n'] = 1;
10276 is_space
['\r'] = 1;
10279 /* Initialize the built-in macros. */
10282 initialize_builtins (inp
, outp
)
10286 install ((const U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
10287 install ((const U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
10288 install ((const U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
10289 install ((const U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
10290 install ((const U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
10291 install ((const U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
10292 #ifndef NO_BUILTIN_SIZE_TYPE
10293 install ((const U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
10295 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10296 install ((const U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
10298 #ifndef NO_BUILTIN_WCHAR_TYPE
10299 install ((const U_CHAR
*) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, NULL_PTR
, -1);
10301 install ((const U_CHAR
*) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
,
10303 install ((const U_CHAR
*) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
,
10305 install ((const U_CHAR
*) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE
,
10307 install ((const U_CHAR
*) "__TIME__", -1, T_TIME
, NULL_PTR
, -1);
10308 if (!traditional
) {
10309 install ((const U_CHAR
*) "__STDC__", -1, T_CONST
, "1", -1);
10310 install ((const U_CHAR
*) "__STDC_VERSION__", -1, T_CONST
, "199409L", -1);
10312 /* This is supplied using a -D by the compiler driver
10313 so that it is present only when truly compiling with GNU C. */
10314 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10315 install ((const U_CHAR
*) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST
, "1", -1);
10319 char directive
[2048];
10320 U_CHAR
*udirective
= (U_CHAR
*) directive
;
10321 register struct directive
*dp
= &directive_table
[0];
10322 struct tm
*timebuf
= timestamp ();
10324 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
10325 instack
[0].nominal_fname
);
10326 output_line_directive (inp
, outp
, 0, same_file
);
10327 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10330 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
10331 output_line_directive (inp
, outp
, 0, same_file
);
10332 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10335 #ifndef NO_BUILTIN_SIZE_TYPE
10336 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
10337 output_line_directive (inp
, outp
, 0, same_file
);
10338 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10342 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10343 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
10344 output_line_directive (inp
, outp
, 0, same_file
);
10345 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10349 #ifndef NO_BUILTIN_WCHAR_TYPE
10350 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
10351 output_line_directive (inp
, outp
, 0, same_file
);
10352 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10356 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
10357 monthnames
[timebuf
->tm_mon
],
10358 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
10359 output_line_directive (inp
, outp
, 0, same_file
);
10360 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10363 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
10364 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
10365 output_line_directive (inp
, outp
, 0, same_file
);
10366 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10371 sprintf (directive
, " __STDC__ 1");
10372 output_line_directive (inp
, outp
, 0, same_file
);
10373 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10378 sprintf (directive
, " __OBJC__ 1");
10379 output_line_directive (inp
, outp
, 0, same_file
);
10380 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10387 * process a given definition string, for initialization
10388 * If STR is just an identifier, define it with value 1.
10389 * If STR has anything after the identifier, then it should
10390 * be identifier=definition.
10394 make_definition (str
)
10398 struct directive
*kt
;
10401 p
= buf
= (U_CHAR
*) str
;
10402 if (!is_idstart
[*p
]) {
10403 error ("malformed option `-D %s'", str
);
10406 while (is_idchar
[*++p
])
10409 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
10412 p
= (U_CHAR
*) str
; /* Error */
10415 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
10416 strcpy ((char *)buf
, str
);
10417 strcat ((char *)buf
, " 1");
10418 } else if (*p
!= '=') {
10419 error ("malformed option `-D %s'", str
);
10423 /* Copy the entire option so we can modify it. */
10424 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
10425 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
10426 /* Change the = to a space. */
10427 buf
[p
- (U_CHAR
*) str
] = ' ';
10428 /* Scan for any backslash-newline and remove it. */
10430 q
= &buf
[p
- (U_CHAR
*) str
];
10432 if (*p
== '\"' || *p
== '\'') {
10433 int unterminated
= 0;
10434 const U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
10435 NULL_PTR
, NULL_PTR
, &unterminated
);
10439 if (*p
== '\\' && p
[1] == '\n')
10441 else if (*p
== '\n')
10450 } else if (*p
== '\\' && p
[1] == '\n')
10452 /* Change newline chars into newline-markers. */
10453 else if (*p
== '\n')
10465 ip
= &instack
[++indepth
];
10466 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10467 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10469 ip
->buf
= ip
->bufp
= buf
;
10470 ip
->length
= strlen ((char *) buf
);
10474 ip
->if_stack
= if_stack
;
10475 ip
->system_header_p
= 0;
10477 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
10480 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10481 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
10485 /* JF, this does the work for the -U option */
10488 make_undef (str
, op
)
10493 struct directive
*kt
;
10495 ip
= &instack
[++indepth
];
10496 ip
->nominal_fname
= ip
->fname
= "*undef*";
10497 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10499 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
10500 ip
->length
= strlen (str
);
10504 ip
->if_stack
= if_stack
;
10505 ip
->system_header_p
= 0;
10507 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
10510 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
10514 /* Process the string STR as if it appeared as the body of a #assert.
10515 OPTION is the option name for which STR was the argument. */
10518 make_assertion (option
, str
)
10519 const char *option
;
10523 struct directive
*kt
;
10524 U_CHAR
*buf
, *p
, *q
;
10526 /* Copy the entire option so we can modify it. */
10527 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
10528 strcpy ((char *) buf
, str
);
10529 /* Scan for any backslash-newline and remove it. */
10532 if (*p
== '\\' && p
[1] == '\n')
10540 if (!is_idstart
[*p
]) {
10541 error ("malformed option `%s %s'", option
, str
);
10544 while (is_idchar
[*++p
])
10546 SKIP_WHITE_SPACE (p
);
10547 if (! (*p
== 0 || *p
== '(')) {
10548 error ("malformed option `%s %s'", option
, str
);
10552 ip
= &instack
[++indepth
];
10553 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10554 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10556 ip
->buf
= ip
->bufp
= buf
;
10557 ip
->length
= strlen ((char *) buf
);
10561 ip
->if_stack
= if_stack
;
10562 ip
->system_header_p
= 0;
10564 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
10567 /* Pass NULL as output ptr to do_define since we KNOW it never does
10569 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
10573 /* The previous include prefix, if any, is PREV_FILE_NAME.
10574 Translate any pathnames with COMPONENT.
10575 Allocate a new include prefix whose name is the
10576 simplified concatenation of PREFIX and NAME,
10577 with a trailing / added if needed.
10578 But return 0 if the include prefix should be ignored,
10579 e.g. because it is a duplicate of PREV_FILE_NAME. */
10581 static struct file_name_list
*
10582 new_include_prefix (prev_file_name
, component
, prefix
, name
)
10583 struct file_name_list
*prev_file_name
;
10584 const char *component
;
10585 const char *prefix
;
10589 fatal ("Directory name missing after command line option");
10592 /* Ignore the empty string. */
10595 prefix
= update_path (prefix
, component
);
10596 name
= update_path (name
, component
);
10599 struct file_name_list
*dir
10600 = ((struct file_name_list
*)
10601 xmalloc (sizeof (struct file_name_list
)
10602 + strlen (prefix
) + strlen (name
) + 2));
10604 strcpy (dir
->fname
, prefix
);
10605 strcat (dir
->fname
, name
);
10606 len
= simplify_filename (dir
->fname
);
10608 /* Convert directory name to a prefix. */
10609 if (len
&& dir
->fname
[len
- 1] != DIR_SEPARATOR
) {
10610 if (len
== 1 && dir
->fname
[len
- 1] == '.')
10614 /* must be '/', hack_vms_include_specification triggers on it. */
10615 dir
->fname
[len
++] = '/';
10617 dir
->fname
[len
++] = DIR_SEPARATOR
;
10619 dir
->fname
[len
] = 0;
10622 /* Ignore a directory whose name matches the previous one. */
10623 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
10624 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10625 if (!first_bracket_include
)
10626 first_bracket_include
= prev_file_name
;
10632 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10634 /* Add a trailing "." if there is a filename. This increases the number
10635 of systems that can stat directories. We remove it below. */
10638 dir
->fname
[len
] = '.';
10639 dir
->fname
[len
+ 1] = 0;
10642 /* Ignore a nonexistent directory. */
10643 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
10644 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
10645 error_from_errno (dir
->fname
);
10651 dir
->fname
[len
] = 0;
10653 /* Ignore a directory whose identity matches the previous one. */
10655 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
10656 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
10657 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10658 if (!first_bracket_include
)
10659 first_bracket_include
= prev_file_name
;
10666 dir
->c_system_include_path
= 0;
10667 dir
->got_name_map
= 0;
10673 /* Append a chain of `struct file_name_list's
10674 to the end of the main include chain.
10675 FIRST is the beginning of the chain to append, and LAST is the end. */
10678 append_include_chain (first
, last
)
10679 struct file_name_list
*first
, *last
;
10681 struct file_name_list
*dir
;
10683 if (!first
|| !last
)
10689 last_include
->next
= first
;
10691 if (first_bracket_include
== 0)
10692 first_bracket_include
= first
;
10694 for (dir
= first
; ; dir
= dir
->next
) {
10695 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
10696 if (len
> max_include_len
)
10697 max_include_len
= len
;
10703 last_include
= last
;
10706 /* Place into DST a representation of the file named SRC that is suitable
10707 for `make'. Do not null-terminate DST. Return its length. */
10709 quote_string_for_make (dst
, src
)
10713 const char *p
= src
;
10724 /* GNU make uses a weird quoting scheme for white space.
10725 A space or tab preceded by 2N+1 backslashes represents
10726 N backslashes followed by space; a space or tab
10727 preceded by 2N backslashes represents N backslashes at
10728 the end of a file name; and backslashes in other
10729 contexts should not be doubled. */
10731 for (q
= p
- 1; src
< q
&& q
[-1] == '\\'; q
--)
10743 goto ordinary_char
;
10749 /* Fall through. This can mishandle things like "$(" but
10750 there's no easy fix. */
10753 /* This can mishandle characters in the string "\0\n%*?[\\~";
10754 exactly which chars are mishandled depends on the `make' version.
10755 We know of no portable solution for this;
10756 even GNU make 3.76.1 doesn't solve the problem entirely.
10757 (Also, '\0' is mishandled due to our calling conventions.) */
10767 /* Add output to `deps_buffer' for the -M switch.
10768 STRING points to the text to be output.
10769 SPACER is ':' for targets, ' ' for dependencies. */
10772 deps_output (string
, spacer
)
10773 const char *string
;
10776 int size
= quote_string_for_make ((char *) 0, string
);
10781 #ifndef MAX_OUTPUT_COLUMNS
10782 #define MAX_OUTPUT_COLUMNS 72
10784 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
10785 && 1 < deps_column
) {
10786 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
10793 if (deps_size
+ 2 * size
+ 8 > deps_allocated_size
) {
10794 deps_allocated_size
= (deps_size
+ 2 * size
+ 50) * 2;
10795 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
10797 if (spacer
== ' ') {
10798 deps_buffer
[deps_size
++] = ' ';
10801 quote_string_for_make (&deps_buffer
[deps_size
], string
);
10803 deps_column
+= size
;
10804 if (spacer
== ':') {
10805 deps_buffer
[deps_size
++] = ':';
10808 deps_buffer
[deps_size
] = 0;
10812 fatal
VPARAMS ((const char * msgid
, ...))
10814 #ifndef ANSI_PROTOTYPES
10815 const char * msgid
;
10819 fprintf (stderr
, "%s: ", progname
);
10820 VA_START (args
, msgid
);
10822 #ifndef ANSI_PROTOTYPES
10823 msgid
= va_arg (args
, const char *);
10825 vnotice (msgid
, args
);
10827 fprintf (stderr
, "\n");
10828 exit (FATAL_EXIT_CODE
);
10831 /* More 'friendly' abort that prints the line and file.
10832 config.h can #define abort fancy_abort if you like that sort of thing. */
10837 fatal ("Internal gcc abort.");
10841 perror_with_name (name
)
10844 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, xstrerror (errno
));
10849 pfatal_with_name (name
)
10852 perror_with_name (name
);
10856 exit (FATAL_EXIT_CODE
);
10860 /* Handler for SIGPIPE. */
10863 pipe_closed (signo
)
10864 /* If this is missing, some compilers complain. */
10865 int signo ATTRIBUTE_UNUSED
;
10867 fatal ("output pipe has been closed");
10873 fatal ("Memory exhausted.");
10878 /* Under VMS we need to fix up the "include" specification filename.
10880 Rules for possible conversions
10882 fullname tried paths
10885 ./dir/name [.dir]name
10887 /name [000000]name, name
10888 dir/name dir:[000000]name, dir:name, dir/name
10889 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10890 path:/name path:[000000]name, path:name
10891 path:/dir/name path:[000000.dir]name, path:[dir]name
10892 path:dir/name path:[dir]name
10893 [path]:[dir]name [path.dir]name
10894 path/[dir]name [path.dir]name
10896 The path:/name input is constructed when expanding <> includes.
10898 return 1 if name was changed, 0 else. */
10901 hack_vms_include_specification (fullname
, vaxc_include
)
10905 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
10906 int f
, check_filename_before_returning
, must_revert
;
10909 check_filename_before_returning
= 0;
10911 /* See if we can find a 1st slash. If not, there's no path information. */
10912 first_slash
= index (fullname
, '/');
10913 if (first_slash
== 0)
10914 return 0; /* Nothing to do!!! */
10916 /* construct device spec if none given. */
10918 if (index (fullname
, ':') == 0)
10921 /* If fullname has a slash, take it as device spec. */
10923 if (first_slash
== fullname
)
10925 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
10927 *first_slash
= ':'; /* make device spec */
10928 for (basename
= fullname
; *basename
!= 0; basename
++)
10929 *basename
= *(basename
+1); /* remove leading slash */
10931 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
10932 && (first_slash
[-1] != ':')
10933 && (first_slash
[-1] != ']')) /* or a vms path */
10935 *first_slash
= ':';
10937 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
10938 && (first_slash
[-1] == '.'))
10942 /* Get part after first ':' (basename[-1] == ':')
10943 or last '/' (basename[-1] == '/'). */
10945 basename
= base_name (fullname
);
10948 * Check if we have a vax-c style '#include filename'
10949 * and add the missing .h
10952 if (vaxc_include
&& !index (basename
,'.'))
10953 strcat (basename
, ".h");
10955 local_ptr
= Local
; /* initialize */
10957 /* We are trying to do a number of things here. First of all, we are
10958 trying to hammer the filenames into a standard format, such that later
10959 processing can handle them.
10961 If the file name contains something like [dir.], then it recognizes this
10962 as a root, and strips the ".]". Later processing will add whatever is
10963 needed to get things working properly.
10965 If no device is specified, then the first directory name is taken to be
10966 a device name (or a rooted logical). */
10968 /* Point to the UNIX filename part (which needs to be fixed!)
10969 but skip vms path information.
10970 [basename != fullname since first_slash != 0]. */
10972 if ((basename
[-1] == ':') /* vms path spec. */
10973 || (basename
[-1] == ']')
10974 || (basename
[-1] == '>'))
10975 unixname
= basename
;
10977 unixname
= fullname
;
10979 if (*unixname
== '/')
10982 /* If the directory spec is not rooted, we can just copy
10983 the UNIX filename part and we are done. */
10985 if (((basename
- fullname
) > 1)
10986 && ( (basename
[-1] == ']')
10987 || (basename
[-1] == '>')))
10989 if (basename
[-2] != '.')
10992 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10993 -> PATH]:/name (basename = '/name', unixname = 'name')
10994 We strip the `]', and then splice the two parts of the name in the
10995 usual way. Given the default locations for include files in cccp.c,
10996 we will only use this code if the user specifies alternate locations
10997 with the /include (-I) switch on the command line. */
10999 basename
-= 1; /* Strip "]" */
11000 unixname
--; /* backspace */
11005 /* The VMS part has a ".]" at the end, and this will not do. Later
11006 processing will add a second directory spec, and this would be a syntax
11007 error. Thus we strip the ".]", and thus merge the directory specs.
11008 We also backspace unixname, so that it points to a '/'. This inhibits the
11009 generation of the 000000 root directory spec (which does not belong here
11012 basename
-= 2; /* Strip ".]" */
11013 unixname
--; /* backspace */
11021 /* We drop in here if there is no VMS style directory specification yet.
11022 If there is no device specification either, we make the first dir a
11023 device and try that. If we do not do this, then we will be essentially
11024 searching the users default directory (as if they did a #include "asdf.h").
11026 Then all we need to do is to push a '[' into the output string. Later
11027 processing will fill this in, and close the bracket. */
11029 if ((unixname
!= fullname
) /* vms path spec found. */
11030 && (basename
[-1] != ':'))
11031 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
11033 *local_ptr
++ = '['; /* Open the directory specification */
11036 if (unixname
== fullname
) /* no vms dir spec. */
11039 if ((first_slash
!= 0) /* unix dir spec. */
11040 && (*unixname
!= '/') /* not beginning with '/' */
11041 && (*unixname
!= '.')) /* or './' or '../' */
11042 *local_ptr
++ = '.'; /* dir is local ! */
11045 /* at this point we assume that we have the device spec, and (at least
11046 the opening "[" for a directory specification. We may have directories
11049 If there are no other slashes then the filename will be
11050 in the "root" directory. Otherwise, we need to add
11051 directory specifications. */
11053 if (index (unixname
, '/') == 0)
11055 /* if no directories specified yet and none are following. */
11056 if (local_ptr
[-1] == '[')
11058 /* Just add "000000]" as the directory string */
11059 strcpy (local_ptr
, "000000]");
11060 local_ptr
+= strlen (local_ptr
);
11061 check_filename_before_returning
= 1; /* we might need to fool with this later */
11067 /* As long as there are still subdirectories to add, do them. */
11068 while (index (unixname
, '/') != 0)
11070 /* If this token is "." we can ignore it
11071 if it's not at the beginning of a path. */
11072 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
11074 /* remove it at beginning of path. */
11075 if ( ((unixname
== fullname
) /* no device spec */
11076 && (fullname
+2 != basename
)) /* starts with ./ */
11078 || ((basename
[-1] == ':') /* device spec */
11079 && (unixname
-1 == basename
))) /* and ./ afterwards */
11080 *local_ptr
++ = '.'; /* make '[.' start of path. */
11085 /* Add a subdirectory spec. Do not duplicate "." */
11086 if ( local_ptr
[-1] != '.'
11087 && local_ptr
[-1] != '['
11088 && local_ptr
[-1] != '<')
11089 *local_ptr
++ = '.';
11091 /* If this is ".." then the spec becomes "-" */
11092 if ( (unixname
[0] == '.')
11093 && (unixname
[1] == '.')
11094 && (unixname
[2] == '/'))
11096 /* Add "-" and skip the ".." */
11097 if ((local_ptr
[-1] == '.')
11098 && (local_ptr
[-2] == '['))
11099 local_ptr
--; /* prevent [.- */
11100 *local_ptr
++ = '-';
11105 /* Copy the subdirectory */
11106 while (*unixname
!= '/')
11107 *local_ptr
++= *unixname
++;
11109 unixname
++; /* Skip the "/" */
11112 /* Close the directory specification */
11113 if (local_ptr
[-1] == '.') /* no trailing periods */
11116 if (local_ptr
[-1] == '[') /* no dir needed */
11119 *local_ptr
++ = ']';
11122 /* Now add the filename. */
11125 *local_ptr
++ = *unixname
++;
11128 /* Now append it to the original VMS spec. */
11130 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
11132 /* If we put a [000000] in the filename, try to open it first. If this fails,
11133 remove the [000000], and return that name. This provides flexibility
11134 to the user in that they can use both rooted and non-rooted logical names
11135 to point to the location of the file. */
11137 if (check_filename_before_returning
)
11139 f
= open (fullname
, O_RDONLY
, 0666);
11142 /* The file name is OK as it is, so return it as is. */
11147 /* The filename did not work. Try to remove the [000000] from the name,
11150 basename
= index (fullname
, '[');
11151 local_ptr
= index (fullname
, ']') + 1;
11152 strcpy (basename
, local_ptr
); /* this gets rid of it */
11162 /* The following wrapper functions supply additional arguments to the VMS
11163 I/O routines to optimize performance with file handling. The arguments
11165 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11166 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11167 "fop=tef"- Truncate unused portions of file when closing file.
11168 "shr=nil"- Disallow file sharing while file is open. */
11171 VMS_freopen (fname
, type
, oldfile
)
11176 #undef freopen /* Get back the real freopen routine. */
11177 if (strcmp (type
, "w") == 0)
11178 return freopen (fname
, type
, oldfile
,
11179 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11180 return freopen (fname
, type
, oldfile
, "mbc=16");
11184 VMS_fopen (fname
, type
)
11188 #undef fopen /* Get back the real fopen routine. */
11189 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11190 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11191 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11192 FILE *(*vmslib_fopen
)() = (FILE *(*)()) fopen
;
11195 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
11196 "deq=64", "fop=tef", "shr=nil");
11198 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
11202 VMS_open (fname
, flags
, prot
)
11207 #undef open /* Get back the real open routine. */
11208 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
11211 /* more VMS hackery */
11215 extern unsigned long SYS$
PARSE(), SYS$
SEARCH();
11217 /* Work around another library bug. If a file is located via a searchlist,
11218 and if the device it's on is not the same device as the one specified
11219 in the first element of that searchlist, then both stat() and fstat()
11220 will fail to return info about it. `errno' will be set to EVMSERR, and
11221 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11222 We can get around this by fully parsing the filename and then passing
11223 that absolute name to stat().
11225 Without this fix, we can end up failing to find header files, which is
11226 bad enough, but then compounding the problem by reporting the reason for
11227 failure as "normal successful completion." */
11229 #undef fstat /* Get back to the library version. */
11232 VMS_fstat (fd
, statbuf
)
11234 struct stat
*statbuf
;
11236 int result
= fstat (fd
, statbuf
);
11241 char nambuf
[NAM$C_MAXRSS
+1];
11243 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
11244 result
= VMS_stat (nambuf
, statbuf
);
11245 /* No fclose(fp) here; that would close(fd) as well. */
11252 VMS_stat (name
, statbuf
)
11254 struct stat
*statbuf
;
11256 int result
= stat (name
, statbuf
);
11262 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for SYS$PARSE */
11263 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for SYS$SEARCH */
11266 fab
.fab$l_fna
= (char *) name
;
11267 fab
.fab$b_fns
= (unsigned char) strlen (name
);
11268 fab
.fab$l_nam
= (void *) &nam
;
11270 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
11271 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
11272 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
11273 if (SYS$
PARSE (&fab
) & 1)
11275 if (SYS$
SEARCH (&fab
) & 1)
11277 res_nam
[nam
.nam$b_rsl
] = '\0';
11278 result
= stat (res_nam
, statbuf
);
11280 /* Clean up searchlist context cached by the system. */
11281 nam
.nam$b_nop
= NAM$M_SYNCHK
;
11282 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
11283 (void) SYS$
PARSE (&fab
);
11291 VMS_fwrite (ptr
, size
, nitems
, stream
)
11297 /* VMS fwrite has undesirable results
11298 if STREAM happens to be a record oriented file.
11299 Work around this problem by writing each character individually. */
11300 char const *p
= ptr
;
11301 size_t bytes
= size
* nitems
;
11302 char *lim
= p
+ bytes
;
11305 if (putc (*p
++, stream
) == EOF
)