1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-99, 2000 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #ifdef HAVE_SYS_RESOURCE_H
27 # include <sys/resource.h>
30 typedef unsigned char U_CHAR
;
36 #ifdef MULTIBYTE_CHARS
39 #endif /* MULTIBYTE_CHARS */
41 #ifndef GET_ENV_PATH_LIST
42 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
45 #ifndef STANDARD_INCLUDE_DIR
46 # define STANDARD_INCLUDE_DIR "/usr/include"
49 /* By default, the suffix for object files is ".o". */
51 # define HAVE_OBJECT_SUFFIX
53 # define OBJECT_SUFFIX ".o"
56 /* VMS-specific definitions */
61 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
62 #define fopen(fname,mode) VMS_fopen (fname,mode)
63 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
64 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
65 #define fwrite(ptr,size,nitems,stream) VMS_fwrite (ptr,size,nitems,stream)
66 static int VMS_fstat (), VMS_stat ();
67 static int VMS_open ();
68 static FILE *VMS_fopen ();
69 static FILE *VMS_freopen ();
70 static size_t VMS_fwrite ();
71 static void hack_vms_include_specification ();
72 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
73 #define INO_T_HASH(a) 0
74 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
77 /* Windows does not natively support inodes, and neither does MSDOS.
78 Cygwin's emulation can generate non-unique inodes, so don't use it. */
79 #if (defined (_WIN32) && ! defined (_UWIN)) \
80 || defined (__MSDOS__)
81 #define INO_T_EQ(a, b) 0
85 #define INO_T_EQ(a, b) ((a) == (b))
89 #define INO_T_HASH(a) (a)
92 #ifndef INCLUDE_LEN_FUDGE
93 #define INCLUDE_LEN_FUDGE 0
96 /* External declarations. */
98 extern char *version_string
;
99 HOST_WIDEST_INT parse_escape
PARAMS ((char **, HOST_WIDEST_INT
));
100 HOST_WIDEST_INT parse_c_expression
PARAMS ((char *, int));
102 /* Name under which this program was invoked. */
104 static const char *progname
;
106 /* Nonzero means use extra default include directories for C++. */
108 static int cplusplus
;
110 /* Nonzero means handle cplusplus style comments */
112 static int cplusplus_comments
;
114 /* Nonzero means handle #import, for objective C. */
118 /* Nonzero means this is an assembly file, and allow
119 unknown directives, which could be comments. */
123 /* Current maximum length of directory names in the search path
124 for include files. (Altered as we get more of them.) */
126 static int max_include_len
;
128 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
130 static int for_lint
= 0;
132 /* Nonzero means copy comments into the output file. */
134 static int put_out_comments
= 0;
136 /* Nonzero means don't process the ANSI trigraph sequences. */
138 static int no_trigraphs
= 0;
140 /* Nonzero means print the names of included files rather than
141 the preprocessed output. 1 means just the #include "...",
142 2 means #include <...> as well. */
144 static int print_deps
= 0;
146 /* Nonzero if missing .h files in -M output are assumed to be generated
147 files and not errors. */
149 static int print_deps_missing_files
= 0;
151 /* Nonzero means print names of header files (-H). */
153 static int print_include_names
= 0;
155 /* Nonzero means don't output line number information. */
157 static int no_line_directives
;
159 /* Nonzero means output the text in failing conditionals,
160 inside #failed ... #endfailed. */
162 static int output_conditionals
;
164 /* dump_only means inhibit output of the preprocessed text
165 and instead output the definitions of all user-defined
166 macros in a form suitable for use as input to cccp.
167 dump_names means pass #define and the macro name through to output.
168 dump_definitions means pass the whole definition (plus #define) through
171 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
172 dump_macros
= dump_none
;
174 /* Nonzero means pass all #define and #undef directives which we actually
175 process through to the output stream. This feature is used primarily
176 to allow cc1 to record the #defines and #undefs for the sake of
177 debuggers which understand about preprocessor macros, but it may
178 also be useful with -E to figure out how symbols are defined, and
179 where they are defined. */
180 static int debug_output
= 0;
182 /* Nonzero means pass #include lines through to the output,
183 even if they are ifdefed out. */
184 static int dump_includes
;
186 /* Nonzero indicates special processing used by the pcp program. The
187 special effects of this mode are:
189 Inhibit all macro expansion, except those inside #if directives.
191 Process #define directives normally, and output their contents
194 Output preconditions to pcp_outfile indicating all the relevant
195 preconditions for use of this file in a later cpp run.
197 static FILE *pcp_outfile
;
199 /* Nonzero means we are inside an IF during a -pcp run. In this mode
200 macro expansion is done, and preconditions are output for all macro
201 uses requiring them. */
202 static int pcp_inside_if
;
204 /* Nonzero means never to include precompiled files.
205 This is 1 since there's no way now to make precompiled files,
206 so it's not worth testing for them. */
207 static int no_precomp
= 1;
209 /* Nonzero means give all the error messages the ANSI standard requires. */
213 /* Nonzero means try to make failure to fit ANSI C an error. */
215 static int pedantic_errors
;
217 /* Nonzero means don't print warning messages. -w. */
219 static int inhibit_warnings
= 0;
221 /* Nonzero means warn if slash-star appears in a slash-star comment,
222 or if newline-backslash appears in a slash-slash comment. */
224 static int warn_comments
;
226 /* Nonzero means warn if a macro argument is (or would be)
227 stringified with -traditional. */
229 static int warn_stringify
;
231 /* Nonzero means warn if there are any trigraphs. */
233 static int warn_trigraphs
;
235 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
237 static int warn_undef
;
239 /* Nonzero means warn if we find white space where it doesn't belong. */
241 static int warn_white_space
;
243 /* Nonzero means warn if #import is used. */
245 static int warn_import
= 1;
247 /* Nonzero means turn warnings into errors. */
249 static int warnings_are_errors
;
251 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
255 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
259 /* Nonzero for the 1999 C Standard. */
263 /* Nonzero causes output not to be done,
264 but directives such as #define that have side effects
267 static int no_output
;
269 /* Nonzero means we should look for header.gcc files that remap file names. */
272 /* Nonzero means this file was included with a -imacros or -include
273 command line and should not be recorded as an include file. */
275 static int no_record_file
;
277 /* Nonzero means that we have finished processing the command line options.
278 This flag is used to decide whether or not to issue certain errors
281 static int done_initializing
= 0;
283 /* Line where a newline was first seen in a string constant. */
285 static int multiline_string_line
= 0;
287 /* I/O buffer structure.
288 The `fname' field is nonzero for source files and #include files
289 and for the dummy text used for -D and -U.
290 It is zero for rescanning results of macro expansion
291 and for expanding macro arguments. */
292 #define INPUT_STACK_MAX 400
293 static struct file_buf
{
295 /* Filename specified with #line directive. */
296 const char *nominal_fname
;
297 /* The length of nominal_fname, which may contain embedded NULs. */
298 size_t nominal_fname_len
;
299 /* Include file description. */
300 struct include_file
*inc
;
301 /* Record where in the search path this file was found.
302 For #include_next. */
303 struct file_name_list
*dir
;
308 /* Macro that this level is the expansion of.
309 Included so that we can reenable the macro
310 at the end of this level. */
311 struct hashnode
*macro
;
312 /* Value of if_stack at start of this file.
313 Used to prohibit unmatched #endif (etc) in an include file. */
314 struct if_stack
*if_stack
;
315 /* Object to be freed at end of input at this level. */
317 /* True if this is a system header file; see is_system_include. */
318 char system_header_p
;
319 } instack
[INPUT_STACK_MAX
];
321 static int last_error_tick
; /* Incremented each time we print it. */
322 static int input_file_stack_tick
; /* Incremented when the status changes. */
324 /* Current nesting level of input sources.
325 `instack[indepth]' is the level currently being read. */
326 static int indepth
= -1;
327 #define CHECK_DEPTH(code) \
328 if (indepth >= (INPUT_STACK_MAX - 1)) \
330 error_with_line (line_for_error (instack[indepth].lineno), \
331 "macro or `#include' recursion too deep"); \
335 /* Current depth in #include directives that use <...>. */
336 static int system_include_depth
= 0;
338 typedef struct file_buf FILE_BUF
;
340 /* The output buffer. Its LENGTH field is the amount of room allocated
341 for the buffer, not the number of chars actually present. To get
342 that, subtract outbuf.buf from outbuf.bufp. */
344 #define OUTBUF_SIZE 10 /* initial size of output buffer */
345 static FILE_BUF outbuf
;
347 /* Grow output buffer OBUF points at
348 so it can hold at least NEEDED more chars. */
350 #define check_expand(OBUF, NEEDED) \
351 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
352 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
354 struct file_name_list
356 struct file_name_list
*next
;
357 /* If the following is 1, it is a C-language system include
359 int c_system_include_path
;
360 /* Mapping of file names for this directory. */
361 struct file_name_map
*name_map
;
362 /* Non-zero if name_map is valid. */
364 /* The include directory status. */
366 /* The include prefix: "" denotes the working directory,
367 otherwise fname must end in '/'.
368 The actual size is dynamically allocated. */
372 /* #include "file" looks in source file dir, then stack. */
373 /* #include <file> just looks in the stack. */
374 /* -I directories are added to the end, then the defaults are added. */
376 static struct default_include
{
377 const char *fname
; /* The name of the directory. */
378 const char *component
; /* The component containing the directory */
379 int cplusplus
; /* Only look here if we're compiling C++. */
380 int cxx_aware
; /* Includes in this directory don't need to
381 be wrapped in extern "C" when compiling
383 int included
; /* Set if the directory is acceptable. */
384 } include_defaults_array
[]
385 #ifdef INCLUDE_DEFAULTS
389 /* Pick up GNU C++ specific include files. */
390 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1, 0 },
392 /* This is the dir for fixincludes. Put it just before
393 the files that we fix. */
394 { GCC_INCLUDE_DIR
, "GCC", 0, 0, 0 },
395 /* For cross-compilation, this dir name is generated
396 automatically in Makefile.in. */
397 { CROSS_INCLUDE_DIR
, "GCC", 0, 0, 0 },
398 #ifdef TOOL_INCLUDE_DIR
399 /* This is another place that the target system's headers might be. */
400 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0, 0 },
402 #else /* not CROSS_COMPILE */
403 #ifdef LOCAL_INCLUDE_DIR
404 /* This should be /usr/local/include and should come before
405 the fixincludes-fixed header files. */
406 { LOCAL_INCLUDE_DIR
, 0, 0, 1, 0 },
408 #ifdef TOOL_INCLUDE_DIR
409 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
410 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
411 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0, 0 },
413 /* This is the dir for fixincludes. Put it just before
414 the files that we fix. */
415 { GCC_INCLUDE_DIR
, "GCC", 0, 0, 0 },
416 /* Some systems have an extra dir of include files. */
417 #ifdef SYSTEM_INCLUDE_DIR
418 { SYSTEM_INCLUDE_DIR
, 0, 0, 0, 0 },
420 #ifndef STANDARD_INCLUDE_COMPONENT
421 #define STANDARD_INCLUDE_COMPONENT 0
423 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0, 0 },
424 #endif /* not CROSS_COMPILE */
427 #endif /* no INCLUDE_DEFAULTS */
429 /* The code looks at the defaults through this pointer, rather than through
430 the constant structure above. This pointer gets changed if an environment
431 variable specifies other defaults. */
432 static struct default_include
*include_defaults
= include_defaults_array
;
434 static struct file_name_list
*include
= 0; /* First dir to search */
435 /* First dir to search for <file> */
436 /* This is the first element to use for #include <...>.
437 If it is 0, use the entire chain for such includes. */
438 static struct file_name_list
*first_bracket_include
= 0;
439 /* This is the first element in the chain that corresponds to
440 a directory of system header files. */
441 static struct file_name_list
*first_system_include
= 0;
442 static struct file_name_list
*last_include
= 0; /* Last in chain */
444 /* Chain of include directories to put at the end of the other chain. */
445 static struct file_name_list
*after_include
= 0;
446 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
448 /* Chain to put at the start of the system include files. */
449 static struct file_name_list
*before_system
= 0;
450 static struct file_name_list
*last_before_system
= 0; /* Last in chain */
452 /* Directory prefix that should replace `/usr' in the standard
453 include file directories. */
454 static char *include_prefix
;
456 /* Maintain and search list of included files. */
458 struct include_file
{
459 struct include_file
*next
; /* for include_hashtab */
460 struct include_file
*next_ino
; /* for include_ino_hashtab */
462 /* If the following is the empty string, it means #pragma once
463 was seen in this include file, or #import was applied to the file.
464 Otherwise, if it is nonzero, it is a macro name.
465 Don't include the file again if that macro is defined. */
466 const U_CHAR
*control_macro
;
467 /* Nonzero if the dependency on this include file has been output. */
472 /* Hash tables of files already included with #include or #import.
473 include_hashtab is by full name; include_ino_hashtab is by inode number. */
475 #define INCLUDE_HASHSIZE 61
476 static struct include_file
*include_hashtab
[INCLUDE_HASHSIZE
];
477 static struct include_file
*include_ino_hashtab
[INCLUDE_HASHSIZE
];
479 /* Global list of strings read in from precompiled files. This list
480 is kept in the order the strings are read in, with new strings being
481 added at the end through stringlist_tailp. We use this list to output
482 the strings at the end of the run.
484 static STRINGDEF
*stringlist
;
485 static STRINGDEF
**stringlist_tailp
= &stringlist
;
488 /* Structure returned by create_definition */
489 typedef struct macrodef MACRODEF
;
492 struct definition
*defn
;
493 const U_CHAR
*symnam
;
497 enum sharp_token_type
{
498 NO_SHARP_TOKEN
= 0, /* token not present */
500 SHARP_TOKEN
= '#', /* token spelled with # only */
501 WHITE_SHARP_TOKEN
, /* token spelled with # and white space */
503 PERCENT_COLON_TOKEN
= '%', /* token spelled with %: only */
504 WHITE_PERCENT_COLON_TOKEN
/* token spelled with %: and white space */
507 /* Structure allocated for every #define. For a simple replacement
510 nargs = -1, the `pattern' list is null, and the expansion is just
511 the replacement text. Nargs = 0 means a functionlike macro with no args,
513 #define getchar() getc (stdin) .
514 When there are args, the expansion is the replacement text with the
515 args squashed out, and the reflist is a list describing how to
516 build the output from the input: e.g., "3 chars, then the 1st arg,
517 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
518 The chars here come from the expansion. Whatever is left of the
519 expansion after the last arg-occurrence is copied after that arg.
520 Note that the reflist can be arbitrarily long---
521 its length depends on the number of times the arguments appear in
522 the replacement text, not how many args there are. Example:
523 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
525 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
526 where (x, y) means (nchars, argno). */
528 typedef struct definition DEFINITION
;
531 int length
; /* length of expansion string */
532 int predefined
; /* True if the macro was builtin or */
533 /* came from the command line */
535 int line
; /* Line number of definition */
536 const char *file
; /* File of definition */
537 size_t file_len
; /* Length of file (which can contain NULs) */
538 char rest_args
; /* Nonzero if last arg. absorbs the rest */
540 struct reflist
*next
;
542 enum sharp_token_type stringify
; /* set if a # operator before arg */
543 enum sharp_token_type raw_before
; /* set if a ## operator before arg */
544 enum sharp_token_type raw_after
; /* set if a ## operator after arg */
546 char rest_args
; /* Nonzero if this arg. absorbs the rest */
547 int nchars
; /* Number of literal chars to copy before
548 this arg occurrence. */
549 int argno
; /* Number of arg to substitute (origin-0) */
552 /* Names of macro args, concatenated in reverse order
553 with comma-space between them.
554 The only use of this is that we warn on redefinition
555 if this differs between the old and new definitions. */
560 /* different kinds of things that can appear in the value field
561 of a hash node. Actually, this may be useless now. */
569 * special extension string that can be added to the last macro argument to
570 * allow it to absorb the "rest" of the arguments when expanded. Ex:
571 * #define wow(a, b...) process (b, a, b)
572 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
573 * { wow (one, two); } -> { process (two, one, two); }
574 * if this "rest_arg" is used with the concat token '##' and if it is not
575 * supplied then the token attached to with ## will not be outputted. Ex:
576 * #define wow (a, b...) process (b ## , a, ## b)
577 * { wow (1, 2); } -> { process (2, 1, 2); }
578 * { wow (one); } -> { process (one); {
580 static char rest_extension
[] = "...";
581 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
583 /* This is the implicit parameter name when using variable number of
584 parameters for macros using the ISO C 99 extension. */
585 static char va_args_name
[] = "__VA_ARGS__";
586 #define VA_ARGS_NAME_LENGTH (sizeof (va_args_name) - 1)
588 /* The structure of a node in the hash table. The hash table
589 has entries for all tokens defined by #define directives (type T_MACRO),
590 plus some special tokens like __LINE__ (these each have their own
591 type, and the appropriate code is run when that type of node is seen.
592 It does not contain control words like "#define", which are recognized
593 by a separate piece of code. */
595 /* different flavors of hash nodes --- also used in keyword table */
597 T_DEFINE
= 1, /* the `#define' keyword */
598 T_INCLUDE
, /* the `#include' keyword */
599 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
600 T_IMPORT
, /* the `#import' keyword */
601 T_IFDEF
, /* the `#ifdef' keyword */
602 T_IFNDEF
, /* the `#ifndef' keyword */
603 T_IF
, /* the `#if' keyword */
604 T_ELSE
, /* `#else' */
605 T_PRAGMA
, /* `#pragma' */
606 T_ELIF
, /* `#elif' */
607 T_UNDEF
, /* `#undef' */
608 T_LINE
, /* `#line' */
609 T_ERROR
, /* `#error' */
610 T_WARNING
, /* `#warning' */
611 T_ENDIF
, /* `#endif' */
612 T_SCCS
, /* `#sccs', used on system V. */
613 T_IDENT
, /* `#ident', used on system V. */
614 T_ASSERT
, /* `#assert', taken from system V. */
615 T_UNASSERT
, /* `#unassert', taken from system V. */
616 T_SPECLINE
, /* special symbol `__LINE__' */
617 T_DATE
, /* `__DATE__' */
618 T_FILE
, /* `__FILE__' */
619 T_BASE_FILE
, /* `__BASE_FILE__' */
620 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
621 T_VERSION
, /* `__VERSION__' */
622 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
623 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
624 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
625 T_USER_LABEL_PREFIX_TYPE
, /* `__USER_LABEL_PREFIX__' */
626 T_REGISTER_PREFIX_TYPE
, /* `__REGISTER_PREFIX__' */
627 T_IMMEDIATE_PREFIX_TYPE
, /* `__IMMEDIATE_PREFIX__' */
628 T_TIME
, /* `__TIME__' */
629 T_CONST
, /* Constant value, used by `__STDC__' */
630 T_MACRO
, /* macro defined by `#define' */
631 T_DISABLED
, /* macro temporarily turned off for rescan */
632 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
633 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
634 T_POISON
, /* defined with `#pragma poison' */
635 T_UNUSED
/* Used for something not defined. */
639 struct hashnode
*next
; /* double links for easy deletion */
640 struct hashnode
*prev
;
641 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
642 chain is kept, in case the node is the head
643 of the chain and gets deleted. */
644 enum node_type type
; /* type of special token */
645 int length
; /* length of token, for quick comparison */
646 U_CHAR
*name
; /* the actual name */
647 union hashval value
; /* pointer to expansion, or whatever */
650 typedef struct hashnode HASHNODE
;
652 /* Some definitions for the hash table. The hash function MUST be
653 computed as shown in hashf () below. That is because the rescan
654 loop computes the hash value `on the fly' for most tokens,
655 in order to avoid the overhead of a lot of procedure calls to
656 the hashf () function. Hashf () only exists for the sake of
657 politeness, for use when speed isn't so important. */
659 #define HASHSIZE 1403
660 static HASHNODE
*hashtab
[HASHSIZE
];
661 #define HASHSTEP(old, c) ((old << 2) + c)
662 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
665 /* We let tm.h override the types used here, to handle trivial differences
666 such as the choice of unsigned int or long unsigned int for size_t.
667 When machines start needing nontrivial differences in the size type,
668 it would be best to do something here to figure out automatically
669 from other information what type to use. */
671 /* The string value for __SIZE_TYPE__. */
674 #define SIZE_TYPE "long unsigned int"
677 /* The string value for __PTRDIFF_TYPE__. */
680 #define PTRDIFF_TYPE "long int"
683 /* The string value for __WCHAR_TYPE__. */
686 #define WCHAR_TYPE "int"
688 static const char * wchar_type
= WCHAR_TYPE
;
691 /* The string value for __USER_LABEL_PREFIX__ */
693 #ifndef USER_LABEL_PREFIX
694 #define USER_LABEL_PREFIX ""
696 static const char * user_label_prefix
= USER_LABEL_PREFIX
;
697 #undef USER_LABEL_PREFIX
699 /* The string value for __REGISTER_PREFIX__ */
701 #ifndef REGISTER_PREFIX
702 #define REGISTER_PREFIX ""
705 /* The string value for __IMMEDIATE_PREFIX__ */
707 #ifndef IMMEDIATE_PREFIX
708 #define IMMEDIATE_PREFIX ""
711 /* In the definition of a #assert name, this structure forms
712 a list of the individual values asserted.
713 Each value is itself a list of "tokens".
714 These are strings that are compared by name. */
716 struct tokenlist_list
{
717 struct tokenlist_list
*next
;
718 struct arglist
*tokens
;
721 struct assertion_hashnode
{
722 struct assertion_hashnode
*next
; /* double links for easy deletion */
723 struct assertion_hashnode
*prev
;
724 /* also, a back pointer to this node's hash
725 chain is kept, in case the node is the head
726 of the chain and gets deleted. */
727 struct assertion_hashnode
**bucket_hdr
;
728 int length
; /* length of token, for quick comparison */
729 U_CHAR
*name
; /* the actual name */
730 /* List of token-sequences. */
731 struct tokenlist_list
*value
;
734 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
736 /* Some definitions for the hash table. The hash function MUST be
737 computed as shown in hashf below. That is because the rescan
738 loop computes the hash value `on the fly' for most tokens,
739 in order to avoid the overhead of a lot of procedure calls to
740 the hashf function. hashf only exists for the sake of
741 politeness, for use when speed isn't so important. */
743 #define ASSERTION_HASHSIZE 37
744 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
746 /* Nonzero means inhibit macroexpansion of what seem to be
747 assertion tests, in rescan. For #if. */
748 static int assertions_flag
;
750 /* `struct directive' defines one #-directive, including how to handle it. */
752 #define DO_PROTO PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
755 int length
; /* Length of name */
756 int (*func
) DO_PROTO
; /* Function to handle directive */
757 const char *name
; /* Name of directive */
758 enum node_type type
; /* Code which describes which directive. */
761 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
762 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
764 /* These functions are declared to return int instead of void since they
765 are going to be placed in the table and some old compilers have trouble with
766 pointers to functions returning void. */
768 static int do_assert DO_PROTO
;
769 static int do_define DO_PROTO
;
770 static int do_elif DO_PROTO
;
771 static int do_else DO_PROTO
;
772 static int do_endif DO_PROTO
;
773 static int do_error DO_PROTO
;
774 static int do_ident DO_PROTO
;
775 static int do_if DO_PROTO
;
776 static int do_include DO_PROTO
;
777 static int do_line DO_PROTO
;
778 static int do_pragma DO_PROTO
;
779 #ifdef SCCS_DIRECTIVE
780 static int do_sccs DO_PROTO
;
782 static int do_unassert DO_PROTO
;
783 static int do_undef DO_PROTO
;
784 static int do_xifdef DO_PROTO
;
786 /* Here is the actual list of #-directives, most-often-used first. */
788 static struct directive directive_table
[] = {
789 { 6, do_define
, "define", T_DEFINE
},
790 { 2, do_if
, "if", T_IF
},
791 { 5, do_xifdef
, "ifdef", T_IFDEF
},
792 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
793 { 5, do_endif
, "endif", T_ENDIF
},
794 { 4, do_else
, "else", T_ELSE
},
795 { 4, do_elif
, "elif", T_ELIF
},
796 { 4, do_line
, "line", T_LINE
},
797 { 7, do_include
, "include", T_INCLUDE
},
798 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
799 { 6, do_include
, "import", T_IMPORT
},
800 { 5, do_undef
, "undef", T_UNDEF
},
801 { 5, do_error
, "error", T_ERROR
},
802 { 7, do_error
, "warning", T_WARNING
},
803 #ifdef SCCS_DIRECTIVE
804 { 4, do_sccs
, "sccs", T_SCCS
},
806 { 6, do_pragma
, "pragma", T_PRAGMA
},
807 { 5, do_ident
, "ident", T_IDENT
},
808 { 6, do_assert
, "assert", T_ASSERT
},
809 { 8, do_unassert
, "unassert", T_UNASSERT
},
810 { -1, 0, "", T_UNUSED
},
813 /* When a directive handler is called,
814 this points to the # (or the : of the %:) that started the directive. */
815 U_CHAR
*directive_start
;
817 /* table to tell if char can be part of a C identifier. */
818 U_CHAR is_idchar
[256];
819 /* table to tell if char can be first char of a c identifier. */
820 U_CHAR is_idstart
[256];
821 /* table to tell if c is horizontal space. */
822 static U_CHAR is_hor_space
[256];
823 /* table to tell if c is horizontal or vertical space. */
824 U_CHAR is_space
[256];
826 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
827 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
829 static int errors
= 0; /* Error counter for exit code */
831 /* Name of output file, for error messages. */
832 static const char *out_fname
;
834 /* Nonzero to ignore \ in string constants. Use to treat #line 1 "A:\file.h
835 as a non-form feed. If you want it to be a form feed, you must use
837 static int ignore_escape_flag
= 1;
839 /* Stack of conditionals currently in progress
840 (including both successful and failing conditionals). */
843 struct if_stack
*next
; /* for chaining to the next stack frame */
844 const char *fname
; /* copied from input when frame is made */
845 size_t fname_len
; /* similarly */
846 int lineno
; /* similarly */
847 int if_succeeded
; /* true if a leg of this if-group
848 has been passed through rescan */
849 const U_CHAR
*control_macro
; /* For #ifndef at start of file,
850 this is the macro name tested. */
851 enum node_type type
; /* type of last directive seen in this group */
853 typedef struct if_stack IF_STACK_FRAME
;
854 static IF_STACK_FRAME
*if_stack
= NULL
;
856 /* Buffer of -M output. */
857 static char *deps_buffer
;
859 /* Number of bytes allocated in above. */
860 static int deps_allocated_size
;
862 /* Number of bytes used. */
863 static int deps_size
;
865 /* Number of bytes since the last newline. */
866 static int deps_column
;
868 /* Nonzero means -I- has been seen,
869 so don't look for #include "foo" the source-file directory. */
870 static int ignore_srcdir
;
872 static int safe_read
PARAMS ((int, char *, int));
873 static void safe_write
PARAMS ((int, const char *, int));
875 int main
PARAMS ((int, char **));
877 static void path_include
PARAMS ((char *));
879 static const U_CHAR
*index0
PARAMS ((const U_CHAR
*, int, size_t));
881 static void trigraph_pcp
PARAMS ((FILE_BUF
*));
882 static void check_white_space
PARAMS ((FILE_BUF
*));
884 static void newline_fix
PARAMS ((U_CHAR
*));
885 static void name_newline_fix
PARAMS ((U_CHAR
*));
887 static const char *get_lintcmd
PARAMS ((const U_CHAR
*, const U_CHAR
*,
888 const U_CHAR
**, int *, int *));
890 static void rescan
PARAMS ((FILE_BUF
*, int));
892 static FILE_BUF expand_to_temp_buffer
PARAMS ((const U_CHAR
*, const U_CHAR
*,
895 static int handle_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*));
897 static struct tm
*timestamp
PARAMS ((void));
898 static void special_symbol
PARAMS ((HASHNODE
*, FILE_BUF
*));
900 static int is_system_include
PARAMS ((const char *));
901 static char *base_name
PARAMS ((const char *));
902 static int absolute_filename
PARAMS ((const char *));
903 static size_t simplify_filename
PARAMS ((char *));
905 static char *read_filename_string
PARAMS ((int, FILE *));
906 static struct file_name_map
*read_name_map
PARAMS ((const char *));
907 static int open_include_file
PARAMS ((char *, struct file_name_list
*,
908 const U_CHAR
*, struct include_file
**));
909 static char *remap_include_file
PARAMS ((char *, struct file_name_list
*));
910 static int lookup_ino_include
PARAMS ((struct include_file
*));
912 static void finclude
PARAMS ((int, struct include_file
*, FILE_BUF
*, int,
913 struct file_name_list
*));
914 static void record_control_macro
PARAMS ((struct include_file
*,
917 static char *check_precompiled
PARAMS ((int, struct stat
*, const char *,
919 static int check_preconditions
PARAMS ((const char *));
920 static void pcfinclude
PARAMS ((U_CHAR
*, const U_CHAR
*, FILE_BUF
*));
921 static void pcstring_used
PARAMS ((HASHNODE
*));
922 static void write_output
PARAMS ((void));
923 static void pass_thru_directive
PARAMS ((const U_CHAR
*, const U_CHAR
*,
924 FILE_BUF
*, struct directive
*));
926 static MACRODEF create_definition
PARAMS ((const U_CHAR
*, const U_CHAR
*,
929 static int check_macro_name
PARAMS ((const U_CHAR
*, int));
930 static int compare_defs
PARAMS ((DEFINITION
*, DEFINITION
*));
931 static int comp_def_part
PARAMS ((int, const U_CHAR
*, int, const U_CHAR
*,
934 static DEFINITION
*collect_expansion
PARAMS ((const U_CHAR
*, const U_CHAR
*,
935 int, struct arglist
*));
937 int check_assertion
PARAMS ((const U_CHAR
*, int, int, struct arglist
*));
938 static int compare_token_lists
PARAMS ((struct arglist
*, struct arglist
*));
940 static struct arglist
*read_token_list
PARAMS ((const U_CHAR
**,
941 const U_CHAR
*, int *));
942 static void free_token_list
PARAMS ((struct arglist
*));
944 static ASSERTION_HASHNODE
*assertion_install
PARAMS ((const U_CHAR
*, int, int));
945 static ASSERTION_HASHNODE
*assertion_lookup
PARAMS ((const U_CHAR
*, int, int));
946 static void delete_assertion
PARAMS ((ASSERTION_HASHNODE
*));
948 static void do_once
PARAMS ((void));
950 static HOST_WIDEST_INT eval_if_expression
PARAMS ((const U_CHAR
*, int));
951 static void conditional_skip
PARAMS ((FILE_BUF
*, int, enum node_type
,
952 const U_CHAR
*, FILE_BUF
*));
953 static void skip_if_group
PARAMS ((FILE_BUF
*, int, FILE_BUF
*));
954 static void validate_else
PARAMS ((const U_CHAR
*, const U_CHAR
*));
956 static U_CHAR
*skip_to_end_of_comment
PARAMS ((FILE_BUF
*, int *, int));
957 static U_CHAR
*skip_quoted_string
PARAMS ((const U_CHAR
*, const U_CHAR
*,
958 int, int *, int *, int *));
959 static char *quote_string
PARAMS ((char *, const char *, size_t));
960 static U_CHAR
*skip_paren_group
PARAMS ((FILE_BUF
*));
962 /* Last arg to output_line_directive. */
963 enum file_change_code
{same_file
, enter_file
, leave_file
};
964 static void output_line_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*, int, enum file_change_code
));
966 static void macroexpand
PARAMS ((HASHNODE
*, FILE_BUF
*));
969 static int macarg
PARAMS ((struct argdata
*, int));
971 static U_CHAR
*macarg1
PARAMS ((U_CHAR
*, const U_CHAR
*, struct hashnode
*, int *, int *, int *, int));
973 static int discard_comments
PARAMS ((U_CHAR
*, int, int));
975 static void change_newlines
PARAMS ((struct argdata
*));
977 static void notice
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
978 static void vnotice
PARAMS ((const char *, va_list));
979 void error
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
980 void verror
PARAMS ((const char *, va_list));
981 static void error_from_errno
PARAMS ((const char *));
982 void warning
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
983 static void vwarning
PARAMS ((const char *, va_list));
984 static void error_with_line
PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
985 static void verror_with_line
PARAMS ((int, const char *, va_list));
986 static void vwarning_with_line
PARAMS ((int, const char *, va_list));
987 static void warning_with_line
PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
988 void pedwarn
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1
;
989 void pedwarn_with_line
PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
990 static void pedwarn_with_file_and_line
PARAMS ((const char *, size_t, int, const char *, ...)) ATTRIBUTE_PRINTF_4
;
991 static void pedwarn_strange_white_space
PARAMS ((int));
993 static void print_containing_files
PARAMS ((void));
995 static int line_for_error
PARAMS ((int));
996 static int grow_outbuf
PARAMS ((FILE_BUF
*, int));
998 static HASHNODE
*install
PARAMS ((const U_CHAR
*, int, enum node_type
,
1000 HASHNODE
*lookup
PARAMS ((const U_CHAR
*, int, int));
1001 static void delete_macro
PARAMS ((HASHNODE
*));
1002 static int hashf
PARAMS ((const U_CHAR
*, int, int));
1004 static void dump_single_macro
PARAMS ((HASHNODE
*, FILE *));
1005 static void dump_all_macros
PARAMS ((void));
1006 static void dump_defn_1
PARAMS ((const U_CHAR
*, int, int, FILE *));
1007 static void dump_arg_n
PARAMS ((DEFINITION
*, int, FILE *));
1009 static void initialize_char_syntax
PARAMS ((void));
1010 static void initialize_builtins
PARAMS ((FILE_BUF
*, FILE_BUF
*));
1012 static void make_definition
PARAMS ((char *));
1013 static void make_undef
PARAMS ((char *, FILE_BUF
*));
1015 static void make_assertion
PARAMS ((const char *, const char *));
1017 static struct file_name_list
*new_include_prefix
PARAMS ((struct file_name_list
*, const char *, const char *, const char *));
1018 static void append_include_chain
PARAMS ((struct file_name_list
*, struct file_name_list
*));
1020 static int quote_string_for_make
PARAMS ((char *, const char *));
1021 static void deps_output
PARAMS ((const char *, int));
1023 void fatal
PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN
;
1024 void fancy_abort
PARAMS ((void)) ATTRIBUTE_NORETURN
;
1025 static void perror_with_name
PARAMS ((const char *));
1026 static void pfatal_with_name
PARAMS ((const char *)) ATTRIBUTE_NORETURN
;
1027 static void pipe_closed
PARAMS ((int)) ATTRIBUTE_NORETURN
;
1029 static void memory_full
PARAMS ((void)) ATTRIBUTE_NORETURN
;
1030 static void print_help
PARAMS ((void));
1032 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1033 retrying if necessary. If MAX_READ_LEN is defined, read at most
1034 that bytes at a time. Return a negative value if an error occurs,
1035 otherwise return the actual number of bytes read,
1036 which must be LEN unless end-of-file was reached. */
1039 safe_read (desc
, ptr
, len
)
1044 int left
, rcount
, nchars
;
1050 if (rcount
> MAX_READ_LEN
)
1051 rcount
= MAX_READ_LEN
;
1053 nchars
= read (desc
, ptr
, rcount
);
1070 /* Write LEN bytes at PTR to descriptor DESC,
1071 retrying if necessary, and treating any real error as fatal.
1072 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1075 safe_write (desc
, ptr
, len
)
1080 int wcount
, written
;
1084 #ifdef MAX_WRITE_LEN
1085 if (wcount
> MAX_WRITE_LEN
)
1086 wcount
= MAX_WRITE_LEN
;
1088 written
= write (desc
, ptr
, wcount
);
1095 pfatal_with_name (out_fname
);
1106 printf ("Usage: %s [switches] input output\n", progname
);
1107 printf ("Switches:\n");
1108 printf (" -include <file> Include the contents of <file> before other files\n");
1109 printf (" -imacros <file> Accept definition of macros in <file>\n");
1110 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1111 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1112 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1113 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1114 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1115 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1116 printf (" -nostdinc Do not search the system include directories\n");
1117 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1118 printf (" -o <file> Put output into <file>\n");
1119 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1120 printf (" -traditional Follow K&R pre-processor behaviour\n");
1121 printf (" -trigraphs Support ANSI C trigraphs\n");
1122 printf (" -lang-c Assume that the input sources are in C\n");
1123 printf (" -lang-c89 Assume that the input is C89; depricated\n");
1124 printf (" -lang-c++ Assume that the input sources are in C++\n");
1125 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1126 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1127 printf (" -lang-asm Assume that the input sources are in assembler\n");
1128 printf (" -lang-fortran Assume that the input sources are in Fortran\n");
1129 printf (" -lang-chill Assume that the input sources are in Chill\n");
1130 printf (" -std=<std name> Specify the conformance standard; one of:\n");
1131 printf (" gnu89, gnu99, c89, c99, iso9899:1990,\n");
1132 printf (" iso9899:199409, iso9899:1999\n");
1133 printf (" -+ Allow parsing of C++ style features\n");
1134 printf (" -w Inhibit warning messages\n");
1135 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1136 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1137 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1138 printf (" -Wno-comment{s} Do not warn about comments\n");
1139 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1140 printf (" a string if -traditional is specified\n");
1141 printf (" -Wno-traditional Do not warn about stringification\n");
1142 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1143 printf (" -Wno-undef Do not warn about testing undefined macros\n");
1144 printf (" -Wimport Warn about the use of the #import directive\n");
1145 printf (" -Wno-import Do not warn about the use of #import\n");
1146 printf (" -Werror Treat all warnings as errors\n");
1147 printf (" -Wno-error Do not treat warnings as errors\n");
1148 printf (" -Wall Enable all preprocessor warnings\n");
1149 printf (" -M Generate make dependencies\n");
1150 printf (" -MM As -M, but ignore system header files\n");
1151 printf (" -MD As -M, but put output in a .d file\n");
1152 printf (" -MMD As -MD, but ignore system header files\n");
1153 printf (" -MG Treat missing header file as generated files\n");
1154 printf (" -g Include #define and #undef directives in the output\n");
1155 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1156 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1157 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1158 printf (" -U<macro> Undefine <macro> \n");
1159 printf (" -u or -undef Do not predefine any macros\n");
1160 printf (" -v Display the version number\n");
1161 printf (" -H Print the name of header files as they are used\n");
1162 printf (" -C Do not discard comments\n");
1163 printf (" -dM Display a list of macro definitions active at end\n");
1164 printf (" -dD Preserve macro definitions in output\n");
1165 printf (" -dN As -dD except that only the names are preserved\n");
1166 printf (" -dI Include #include directives in the output\n");
1167 printf (" -ifoutput Describe skipped code blocks in output \n");
1168 printf (" -P Do not generate #line directives\n");
1169 printf (" -$ Do not include '$' in identifiers\n");
1170 printf (" -remap Remap file names when including files.\n");
1171 printf (" -h or --help Display this information\n");
1180 const char *in_fname
;
1188 char **pend_assertions
;
1189 char **pend_includes
;
1191 /* Record the option used with each element of pend_assertions.
1192 This is preparation for supporting more than one option for making
1194 const char **pend_assertion_options
;
1195 int no_standard_includes
= 0;
1196 int no_standard_cplusplus_includes
= 0;
1197 int missing_newline
= 0;
1199 /* Non-0 means don't output the preprocessed program. */
1200 int inhibit_output
= 0;
1201 /* Non-0 means -v, so print the full set of include dirs. */
1204 /* File name which deps are being written to.
1205 This is 0 if deps are being written to stdout. */
1206 char *deps_file
= 0;
1207 /* Fopen file mode to open deps_file with. */
1208 const char *deps_mode
= "a";
1209 /* Stream on which to print the dependency information. */
1210 FILE *deps_stream
= 0;
1211 /* Target-name to write with the dependency information. */
1212 char *deps_target
= 0;
1214 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1215 /* Get rid of any avoidable limit on stack size. */
1219 /* Set the stack limit huge so that alloca (particularly stringtab
1220 in dbxread.c) does not fail. */
1221 getrlimit (RLIMIT_STACK
, &rlim
);
1222 rlim
.rlim_cur
= rlim
.rlim_max
;
1223 setrlimit (RLIMIT_STACK
, &rlim
);
1228 signal (SIGPIPE
, pipe_closed
);
1231 #ifdef HAVE_LC_MESSAGES
1232 setlocale (LC_MESSAGES
, "");
1234 (void) bindtextdomain (PACKAGE
, localedir
);
1235 (void) textdomain (PACKAGE
);
1237 progname
= base_name (argv
[0]);
1241 /* Remove extension from PROGNAME. */
1243 char *s
= xstrdup (progname
);
1246 if ((p
= rindex (s
, ';')) != 0) *p
= '\0'; /* strip version number */
1247 if ((p
= rindex (s
, '.')) != 0 /* strip type iff ".exe" */
1248 && (p
[1] == 'e' || p
[1] == 'E')
1249 && (p
[2] == 'x' || p
[2] == 'X')
1250 && (p
[3] == 'e' || p
[3] == 'E')
1256 /* Do not invoke xmalloc before this point, since locale and
1257 progname need to be set first, in case a diagnostic is issued. */
1259 pend_files
= (char **) xmalloc (argc
* sizeof (char *));
1260 pend_defs
= (char **) xmalloc ((2 * argc
) * sizeof (char *));
1261 pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
1262 pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
1263 pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
1264 pend_assertion_options
= (const char **) xmalloc (argc
* sizeof (char *));
1269 /* Initialize is_idchar. */
1270 initialize_char_syntax ();
1272 no_line_directives
= 0;
1274 dump_macros
= dump_none
;
1277 cplusplus_comments
= 1;
1279 bzero ((char *) pend_files
, argc
* sizeof (char *));
1280 bzero ((char *) pend_defs
, (2 * argc
) * sizeof (char *));
1281 bzero ((char *) pend_undefs
, argc
* sizeof (char *));
1282 bzero ((char *) pend_assertions
, argc
* sizeof (char *));
1283 bzero ((char *) pend_includes
, argc
* sizeof (char *));
1285 #ifdef MULTIBYTE_CHARS
1286 /* Change to the native locale for multibyte conversions. */
1287 setlocale (LC_CTYPE
, "");
1288 literal_codeset
= getenv ("LANG");
1291 /* Process switches and find input file name. */
1293 for (i
= 1; i
< argc
; i
++) {
1294 if (argv
[i
][0] != '-') {
1295 if (out_fname
!= NULL
)
1298 fatal ("Too many arguments");
1300 else if (in_fname
!= NULL
)
1301 out_fname
= argv
[i
];
1305 switch (argv
[i
][1]) {
1308 if (!strcmp (argv
[i
], "-include")) {
1310 fatal ("Filename missing after `-include' option");
1313 simplify_filename (pend_includes
[i
] = argv
[i
]);
1316 if (!strcmp (argv
[i
], "-imacros")) {
1318 fatal ("Filename missing after `-imacros' option");
1321 simplify_filename (pend_files
[i
] = argv
[i
]);
1324 if (!strcmp (argv
[i
], "-iprefix")) {
1326 fatal ("Filename missing after `-iprefix' option");
1328 include_prefix
= argv
[++i
];
1330 if (!strcmp (argv
[i
], "-ifoutput")) {
1331 output_conditionals
= 1;
1333 if (!strcmp (argv
[i
], "-isystem")) {
1334 struct file_name_list
*dirtmp
;
1336 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1339 dirtmp
->c_system_include_path
= 1;
1341 if (before_system
== 0)
1342 before_system
= dirtmp
;
1344 last_before_system
->next
= dirtmp
;
1345 last_before_system
= dirtmp
; /* Tail follows the last one */
1347 /* Add directory to end of path for includes,
1348 with the default prefix at the front of its name. */
1349 if (!strcmp (argv
[i
], "-iwithprefix")) {
1350 struct file_name_list
*dirtmp
;
1353 if (include_prefix
!= 0)
1354 prefix
= include_prefix
;
1356 prefix
= xstrdup (GCC_INCLUDE_DIR
);
1357 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1358 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1359 prefix
[strlen (prefix
) - 7] = 0;
1362 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1363 prefix
, argv
[++i
])))
1366 if (after_include
== 0)
1367 after_include
= dirtmp
;
1369 last_after_include
->next
= dirtmp
;
1370 last_after_include
= dirtmp
; /* Tail follows the last one */
1372 /* Add directory to main path for includes,
1373 with the default prefix at the front of its name. */
1374 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
1375 struct file_name_list
*dirtmp
;
1378 if (include_prefix
!= 0)
1379 prefix
= include_prefix
;
1381 prefix
= xstrdup (GCC_INCLUDE_DIR
);
1382 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1383 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1384 prefix
[strlen (prefix
) - 7] = 0;
1387 dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
, prefix
, argv
[++i
]);
1388 append_include_chain (dirtmp
, dirtmp
);
1390 /* Add directory to end of path for includes. */
1391 if (!strcmp (argv
[i
], "-idirafter")) {
1392 struct file_name_list
*dirtmp
;
1394 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1398 if (after_include
== 0)
1399 after_include
= dirtmp
;
1401 last_after_include
->next
= dirtmp
;
1402 last_after_include
= dirtmp
; /* Tail follows the last one */
1407 if (out_fname
!= NULL
)
1408 fatal ("Output filename specified twice");
1410 fatal ("Filename missing after -o option");
1411 out_fname
= argv
[++i
];
1412 if (!strcmp (out_fname
, "-"))
1417 if (!strcmp (argv
[i
], "-pedantic"))
1419 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1421 pedantic_errors
= 1;
1422 } else if (!strcmp (argv
[i
], "-pcp")) {
1425 fatal ("Filename missing after -pcp option");
1426 pcp_fname
= argv
[++i
];
1428 = ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1429 ? fopen (pcp_fname
, "w")
1431 if (pcp_outfile
== 0)
1432 pfatal_with_name (pcp_fname
);
1438 if (!strcmp (argv
[i
], "-traditional")) {
1440 cplusplus_comments
= 0;
1441 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1447 if (! strcmp (argv
[i
], "-lang-c"))
1448 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 1, objc
= 0;
1449 else if (! strcmp (argv
[i
], "-lang-c89"))
1451 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1453 pend_defs
[2*i
] = "__STRICT_ANSI__";
1455 else if (! strcmp (argv
[i
], "-lang-c++"))
1456 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c99
= 0, objc
= 0;
1457 else if (! strcmp (argv
[i
], "-lang-objc"))
1458 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 0, objc
= 1;
1459 else if (! strcmp (argv
[i
], "-lang-objc++"))
1460 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c99
= 0, objc
= 1;
1461 else if (! strcmp (argv
[i
], "-lang-asm"))
1463 else if (! strcmp (argv
[i
], "-lang-fortran"))
1464 /* Doesn't actually do anything. */ ;
1465 else if (! strcmp (argv
[i
], "-lint"))
1470 cplusplus
= 1, cplusplus_comments
= 1;
1474 if (!strcmp (argv
[i
], "-std=gnu89"))
1476 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1478 else if (!strcmp (argv
[i
], "-std=gnu9x")
1479 || !strcmp (argv
[i
], "-std=gnu99"))
1481 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 1, objc
= 0;
1482 pend_defs
[2*i
+1] = "__STDC_VERSION__=199901L";
1484 else if (!strcmp (argv
[i
], "-std=iso9899:1990")
1485 || !strcmp (argv
[i
], "-std=c89"))
1487 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1489 pend_defs
[2*i
] = "__STRICT_ANSI__";
1491 else if (!strcmp (argv
[i
], "-std=iso9899:199409"))
1493 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c99
= 0, objc
= 0;
1495 pend_defs
[2*i
] = "__STRICT_ANSI__";
1496 pend_defs
[2*i
+1] = "__STDC_VERSION__=199409L";
1498 else if (!strcmp (argv
[i
], "-std=iso9899:199x")
1499 || !strcmp (argv
[i
], "-std=iso9899:1999")
1500 || !strcmp (argv
[i
], "-std=c9x")
1501 || !strcmp (argv
[i
], "-std=c99"))
1503 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c99
= 1, objc
= 0;
1505 pend_defs
[2*i
] = "__STRICT_ANSI__";
1506 pend_defs
[2*i
+1] = "__STDC_VERSION__=199901L";
1511 inhibit_warnings
= 1;
1515 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1517 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1519 else if (!strcmp (argv
[i
], "-Wcomment"))
1521 else if (!strcmp (argv
[i
], "-Wno-comment"))
1523 else if (!strcmp (argv
[i
], "-Wcomments"))
1525 else if (!strcmp (argv
[i
], "-Wno-comments"))
1527 else if (!strcmp (argv
[i
], "-Wtraditional"))
1529 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1531 else if (!strcmp (argv
[i
], "-Wwhite-space"))
1532 warn_white_space
= 1;
1533 else if (!strcmp (argv
[i
], "-Wno-white-space"))
1534 warn_white_space
= 0;
1535 else if (!strcmp (argv
[i
], "-Wundef"))
1537 else if (!strcmp (argv
[i
], "-Wno-undef"))
1539 else if (!strcmp (argv
[i
], "-Wimport"))
1541 else if (!strcmp (argv
[i
], "-Wno-import"))
1543 else if (!strcmp (argv
[i
], "-Werror"))
1544 warnings_are_errors
= 1;
1545 else if (!strcmp (argv
[i
], "-Wno-error"))
1546 warnings_are_errors
= 0;
1547 else if (!strcmp (argv
[i
], "-Wall"))
1551 warn_white_space
= 1;
1556 if (!strcmp (argv
[i
], "-fleading-underscore"))
1557 user_label_prefix
= "_";
1558 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
1559 user_label_prefix
= "";
1563 /* The style of the choices here is a bit mixed.
1564 The chosen scheme is a hybrid of keeping all options in one string
1565 and specifying each option in a separate argument:
1566 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1567 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1568 -M[M][G][D file]. This is awkward to handle in specs, and is not
1570 /* ??? -MG must be specified in addition to one of -M or -MM.
1571 This can be relaxed in the future without breaking anything.
1572 The converse isn't true. */
1574 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1575 if (!strcmp (argv
[i
], "-MG"))
1577 print_deps_missing_files
= 1;
1580 if (!strcmp (argv
[i
], "-M"))
1582 else if (!strcmp (argv
[i
], "-MM"))
1584 else if (!strcmp (argv
[i
], "-MD"))
1586 else if (!strcmp (argv
[i
], "-MMD"))
1588 /* For -MD and -MMD options, write deps on file named by next arg. */
1589 if (!strcmp (argv
[i
], "-MD")
1590 || !strcmp (argv
[i
], "-MMD")) {
1592 fatal ("Filename missing after %s option", argv
[i
]);
1594 deps_file
= argv
[i
];
1597 /* For -M and -MM, write deps on standard output
1598 and suppress the usual output. */
1599 deps_stream
= stdout
;
1606 char *p
= argv
[i
] + 2;
1608 while ((c
= *p
++)) {
1609 /* Arg to -d specifies what parts of macros to dump */
1612 dump_macros
= dump_only
;
1616 dump_macros
= dump_names
;
1619 dump_macros
= dump_definitions
;
1630 if (argv
[i
][2] == '3')
1635 if (strcmp (argv
[i
], "--help") != 0)
1642 notice ("GNU CPP version %s", version_string
);
1643 #ifdef TARGET_VERSION
1646 fprintf (stderr
, "\n");
1651 print_include_names
= 1;
1655 if (argv
[i
][2] != 0)
1656 pend_defs
[2*i
] = argv
[i
] + 2;
1657 else if (i
+ 1 == argc
)
1658 fatal ("Macro name missing after -D option");
1660 i
++, pend_defs
[2*i
] = argv
[i
];
1667 if (argv
[i
][2] != 0)
1669 else if (i
+ 1 == argc
)
1670 fatal ("Assertion missing after -A option");
1674 if (!strcmp (p
, "-")) {
1675 /* -A- eliminates all predefined macros and assertions.
1676 Let's include also any that were specified earlier
1677 on the command line. That way we can get rid of any
1678 that were passed automatically in from GCC. */
1680 for (j
= 0; j
< i
; j
++)
1681 pend_defs
[2*j
] = pend_assertions
[j
] = 0;
1683 pend_assertions
[i
] = p
;
1684 pend_assertion_options
[i
] = "-A";
1689 case 'U': /* JF #undef something */
1690 if (argv
[i
][2] != 0)
1691 pend_undefs
[i
] = argv
[i
] + 2;
1692 else if (i
+ 1 == argc
)
1693 fatal ("Macro name missing after -U option");
1695 pend_undefs
[i
] = argv
[i
+1], i
++;
1699 put_out_comments
= 1;
1702 case 'E': /* -E comes from cc -E; ignore it. */
1706 no_line_directives
= 1;
1709 case '$': /* Don't include $ in identifiers. */
1710 is_idchar
['$'] = is_idstart
['$'] = 0;
1713 case 'I': /* Add directory to path for includes. */
1715 struct file_name_list
*dirtmp
;
1716 char *dir
= argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
];
1718 if (! ignore_srcdir
&& !strcmp (dir
, "-")) {
1720 /* Don't use any preceding -I directories for #include <...>. */
1721 first_bracket_include
= 0;
1724 dirtmp
= new_include_prefix (last_include
, NULL_PTR
, "", dir
);
1725 append_include_chain (dirtmp
, dirtmp
);
1731 if (!strcmp (argv
[i
], "-nostdinc"))
1732 /* -nostdinc causes no default include directories.
1733 You must specify all include-file directories with -I. */
1734 no_standard_includes
= 1;
1735 else if (!strcmp (argv
[i
], "-nostdinc++"))
1736 /* -nostdinc++ causes no default C++-specific include directories. */
1737 no_standard_cplusplus_includes
= 1;
1738 else if (!strcmp (argv
[i
], "-noprecomp"))
1743 if (!strcmp (argv
[i
], "-remap"))
1747 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1748 if (in_fname
== NULL
) {
1751 } else if (out_fname
== NULL
) {
1754 } /* else fall through into error */
1757 fatal ("Invalid option `%s'", argv
[i
]);
1762 /* Add dirs from CPATH after dirs from -I. */
1763 /* There seems to be confusion about what CPATH should do,
1764 so for the moment it is not documented. */
1765 /* Some people say that CPATH should replace the standard include dirs,
1766 but that seems pointless: it comes before them, so it overrides them
1768 GET_ENV_PATH_LIST (cp
, "CPATH");
1769 if (cp
&& ! no_standard_includes
)
1772 /* Initialize output buffer */
1774 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1775 outbuf
.bufp
= outbuf
.buf
;
1776 outbuf
.length
= OUTBUF_SIZE
;
1778 /* Do partial setup of input buffer for the sake of generating
1779 early #line directives (when -g is in effect). */
1781 fp
= &instack
[++indepth
];
1782 if (in_fname
== NULL
)
1784 fp
->nominal_fname
= fp
->fname
= in_fname
;
1785 fp
->nominal_fname_len
= strlen (in_fname
);
1788 /* In C++, wchar_t is a distinct basic type, and we can expect
1789 __wchar_t to be defined by cc1plus. */
1791 wchar_type
= "__wchar_t";
1793 /* Install __LINE__, etc. Must follow initialize_char_syntax
1794 and option processing. */
1795 initialize_builtins (fp
, &outbuf
);
1797 /* Now handle the command line options. */
1799 /* Do -U's, -D's and -A's in the order they were seen. */
1800 for (i
= 1; i
< argc
; i
++) {
1801 if (pend_undefs
[i
]) {
1803 output_line_directive (fp
, &outbuf
, 0, same_file
);
1804 make_undef (pend_undefs
[i
], &outbuf
);
1806 if (pend_defs
[2*i
]) {
1808 output_line_directive (fp
, &outbuf
, 0, same_file
);
1809 make_definition (pend_defs
[2*i
]);
1811 if (pend_defs
[2*i
+1]) {
1813 output_line_directive (fp
, &outbuf
, 0, same_file
);
1814 make_definition (pend_defs
[2*i
+1]);
1816 if (pend_assertions
[i
])
1817 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1820 done_initializing
= 1;
1822 { /* Read the appropriate environment variable and if it exists
1823 replace include_defaults with the listed path. */
1825 switch ((objc
<< 1) + cplusplus
)
1828 GET_ENV_PATH_LIST (epath
, "C_INCLUDE_PATH");
1831 GET_ENV_PATH_LIST (epath
, "CPLUS_INCLUDE_PATH");
1834 GET_ENV_PATH_LIST (epath
, "OBJC_INCLUDE_PATH");
1837 GET_ENV_PATH_LIST (epath
, "OBJCPLUS_INCLUDE_PATH");
1840 /* If the environment var for this language is set,
1841 add to the default list of include directories. */
1844 char *startp
, *endp
;
1846 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1847 if (*startp
== PATH_SEPARATOR
)
1850 = (struct default_include
*) xmalloc ((num_dirs
1851 * sizeof (struct default_include
))
1852 + sizeof (include_defaults_array
));
1853 startp
= endp
= epath
;
1857 if (c
== PATH_SEPARATOR
|| !c
) {
1859 include_defaults
[num_dirs
].fname
1860 = startp
== endp
? "." : xstrdup (startp
);
1862 include_defaults
[num_dirs
].component
= 0;
1863 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1864 include_defaults
[num_dirs
].cxx_aware
= 1;
1871 /* Put the usual defaults back in at the end. */
1872 bcopy ((const PTR
) include_defaults_array
,
1873 (PTR
) &include_defaults
[num_dirs
],
1874 sizeof (include_defaults_array
));
1878 append_include_chain (before_system
, last_before_system
);
1879 first_system_include
= before_system
;
1881 /* Unless -fnostdinc,
1882 tack on the standard include file dirs to the specified list */
1883 if (!no_standard_includes
) {
1884 struct default_include
*p
= include_defaults
;
1885 char *specd_prefix
= include_prefix
;
1886 char *default_prefix
= xstrdup (GCC_INCLUDE_DIR
);
1887 int default_len
= 0;
1888 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1889 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1890 default_len
= strlen (default_prefix
) - 7;
1891 default_prefix
[default_len
] = 0;
1893 /* Search "translated" versions of GNU directories.
1894 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1895 if (specd_prefix
!= 0 && default_len
!= 0)
1896 for (p
= include_defaults
; p
->fname
; p
++) {
1897 /* Some standard dirs are only for C++. */
1898 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1899 /* Does this dir start with the prefix? */
1900 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1901 /* Yes; change prefix and add to search list. */
1902 struct file_name_list
*new
1903 = new_include_prefix (NULL_PTR
, NULL_PTR
, specd_prefix
,
1904 p
->fname
+ default_len
);
1906 new->c_system_include_path
= !p
->cxx_aware
;
1907 append_include_chain (new, new);
1908 if (first_system_include
== 0)
1909 first_system_include
= new;
1915 /* Search ordinary names for GNU include directories. */
1916 for (p
= include_defaults
; p
->fname
; p
++) {
1917 /* Some standard dirs are only for C++. */
1918 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1919 struct file_name_list
*new
1920 = new_include_prefix (NULL_PTR
, p
->component
, "", p
->fname
);
1922 new->c_system_include_path
= !p
->cxx_aware
;
1923 append_include_chain (new, new);
1924 if (first_system_include
== 0)
1925 first_system_include
= new;
1932 /* Tack the after_include chain at the end of the include chain. */
1933 append_include_chain (after_include
, last_after_include
);
1934 if (first_system_include
== 0)
1935 first_system_include
= after_include
;
1937 /* With -v, print the list of dirs to search. */
1939 struct file_name_list
*p
;
1940 notice ("#include \"...\" search starts here:\n");
1941 for (p
= include
; p
; p
= p
->next
) {
1942 if (p
== first_bracket_include
)
1943 notice ("#include <...> search starts here:\n");
1945 fprintf (stderr
, " .\n");
1946 else if (!strcmp (p
->fname
, "/") || !strcmp (p
->fname
, "//"))
1947 fprintf (stderr
, " %s\n", p
->fname
);
1949 /* Omit trailing '/'. */
1950 fprintf (stderr
, " %.*s\n", (int) strlen (p
->fname
) - 1, p
->fname
);
1952 notice ("End of search list.\n");
1954 struct default_include
* d
;
1955 notice ("The following default directories have been omitted from the search path:\n");
1956 for (d
= include_defaults
; d
->fname
; d
++)
1958 fprintf (stderr
, " %s\n", d
->fname
);
1959 notice ("End of omitted list.\n");
1963 /* -MG doesn't select the form of output and must be specified with one of
1964 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1965 inhibit compilation. */
1966 if (print_deps_missing_files
&& (print_deps
== 0 || !inhibit_output
))
1967 fatal ("-MG must be specified with one of -M or -MM");
1969 /* Either of two environment variables can specify output of deps.
1970 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1971 where OUTPUT_FILE is the file to write deps info to
1972 and DEPS_TARGET is the target to mention in the deps. */
1975 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1976 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1977 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
1982 spec
= getenv ("SUNPRO_DEPENDENCIES");
1988 /* Find the space before the DEPS_TARGET, if there is one. */
1989 s
= index (spec
, ' ');
1991 deps_target
= s
+ 1;
1992 output_file
= xmalloc (s
- spec
+ 1);
1993 bcopy (spec
, output_file
, s
- spec
);
1994 output_file
[s
- spec
] = 0;
2000 deps_file
= output_file
;
2004 /* For -M, print the expected object file name
2005 as the target of this Make-rule. */
2007 deps_allocated_size
= 200;
2008 deps_buffer
= xmalloc (deps_allocated_size
);
2014 deps_output (deps_target
, ':');
2015 } else if (*in_fname
== 0) {
2016 deps_output ("-", ':');
2021 q
= base_name (in_fname
);
2023 /* Copy remainder to mungable area. */
2024 p
= (char *) alloca (strlen(q
) + 8);
2027 /* Output P, but remove known suffixes. */
2031 && p
[len
- 2] == '.'
2032 && index("cCsSm", p
[len
- 1]))
2035 && p
[len
- 3] == '.'
2036 && p
[len
- 2] == 'c'
2037 && p
[len
- 1] == 'c')
2040 && p
[len
- 4] == '.'
2041 && p
[len
- 3] == 'c'
2042 && p
[len
- 2] == 'x'
2043 && p
[len
- 1] == 'x')
2046 && p
[len
- 4] == '.'
2047 && p
[len
- 3] == 'c'
2048 && p
[len
- 2] == 'p'
2049 && p
[len
- 1] == 'p')
2052 /* Supply our own suffix. */
2053 strcpy (q
, OBJECT_SUFFIX
);
2055 deps_output (p
, ':');
2058 deps_output (in_fname
, ' ');
2061 /* Scan the -imacros files before the main input.
2062 Much like #including them, but with no_output set
2063 so that only their macro definitions matter. */
2065 no_output
++; no_record_file
++;
2066 for (i
= 1; i
< argc
; i
++)
2067 if (pend_files
[i
]) {
2068 struct include_file
*inc
;
2069 int fd
= open_include_file (pend_files
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2071 perror_with_name (pend_files
[i
]);
2072 return FATAL_EXIT_CODE
;
2074 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2076 no_output
--; no_record_file
--;
2078 /* Copy the entire contents of the main input file into
2079 the stacked input buffer previously allocated for it. */
2081 /* JF check for stdin */
2082 if (in_fname
== NULL
|| *in_fname
== 0) {
2085 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
2088 if (fstat (f
, &st
) != 0)
2089 pfatal_with_name (in_fname
);
2090 fp
->nominal_fname
= fp
->fname
= in_fname
;
2091 fp
->nominal_fname_len
= strlen (in_fname
);
2093 fp
->system_header_p
= 0;
2094 /* JF all this is mine about reading pipes and ttys */
2095 if (! S_ISREG (st
.st_mode
)) {
2096 /* Read input from a file that is not a normal disk file.
2097 We cannot preallocate a buffer with the correct size,
2098 so we must read in the file a piece at the time and make it bigger. */
2103 if (S_ISDIR (st
.st_mode
))
2104 fatal ("Input file `%s' is a directory", in_fname
);
2108 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
2110 cnt
= safe_read (f
, (char *) fp
->buf
+ size
, bsize
- size
);
2111 if (cnt
< 0) goto perror
; /* error! */
2113 if (size
!= bsize
) break; /* End of file */
2115 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
2119 /* Read a file whose size we can determine in advance.
2120 For the sake of VMS, st.st_size is just an upper bound. */
2121 size_t s
= (size_t) st
.st_size
;
2122 if (s
!= st
.st_size
|| s
+ 2 < s
)
2124 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
2125 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
2126 if (fp
->length
< 0) goto perror
;
2129 fp
->if_stack
= if_stack
;
2131 /* Make sure data ends with a newline. And put a null after it. */
2133 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
2134 /* Backslash-newline at end is not good enough. */
2135 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
2136 fp
->buf
[fp
->length
++] = '\n';
2137 missing_newline
= 1;
2139 fp
->buf
[fp
->length
] = '\0';
2141 /* Unless inhibited, convert trigraphs in the input. */
2146 if (warn_white_space
)
2147 check_white_space (fp
);
2149 /* Now that we know the input file is valid, open the output. */
2151 if (!out_fname
|| !strcmp (out_fname
, ""))
2152 out_fname
= "stdout";
2153 else if (! freopen (out_fname
, "w", stdout
))
2154 pfatal_with_name (out_fname
);
2156 output_line_directive (fp
, &outbuf
, 0, same_file
);
2158 /* Scan the -include files before the main input. */
2161 for (i
= 1; i
< argc
; i
++)
2162 if (pend_includes
[i
]) {
2163 struct include_file
*inc
;
2164 int fd
= open_include_file (pend_includes
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2166 perror_with_name (pend_includes
[i
]);
2167 return FATAL_EXIT_CODE
;
2169 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2173 /* Scan the input, processing macros and directives. */
2175 rescan (&outbuf
, 0);
2177 if (missing_newline
)
2180 if (pedantic
&& missing_newline
)
2181 pedwarn ("file does not end in newline");
2183 /* Now we have processed the entire input
2184 Write whichever kind of output has been requested. */
2186 if (dump_macros
== dump_only
)
2188 else if (! inhibit_output
) {
2193 /* Don't actually write the deps file if compilation has failed. */
2195 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, deps_mode
)))
2196 pfatal_with_name (deps_file
);
2197 fputs (deps_buffer
, deps_stream
);
2198 putc ('\n', deps_stream
);
2200 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
2201 fatal ("I/O error on output");
2206 if (pcp_outfile
&& pcp_outfile
!= stdout
2207 && (ferror (pcp_outfile
) || fclose (pcp_outfile
) != 0))
2208 fatal ("I/O error on `-pcp' output");
2210 if (ferror (stdout
) || fclose (stdout
) != 0)
2211 fatal ("I/O error on output");
2214 exit (FATAL_EXIT_CODE
);
2215 exit (SUCCESS_EXIT_CODE
);
2218 pfatal_with_name (in_fname
);
2222 /* Given a colon-separated list of file names PATH,
2223 add all the names to the search path for include files. */
2237 struct file_name_list
*dirtmp
;
2239 /* Find the end of this name. */
2240 while ((c
= *q
++) != PATH_SEPARATOR
&& c
)
2244 dirtmp
= new_include_prefix (last_include
, NULL_PTR
,
2245 "", p
== q
? "." : p
);
2247 append_include_chain (dirtmp
, dirtmp
);
2249 /* Advance past this name. */
2256 /* Return the address of the first character in S that equals C.
2257 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2258 Return 0 if there is no such character. Assume that C itself is not '\0'.
2259 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2260 but unfortunately memchr isn't autoconfigured yet. */
2262 static const U_CHAR
*
2268 const char *p
= (const char *) s
;
2270 const char *q
= index (p
, c
);
2272 return (const U_CHAR
*) q
;
2274 size_t l
= strlen (p
);
2284 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2285 before main CCCP processing. Name `pcp' is also in honor of the
2286 drugs the trigraph designers must have been on.
2288 Using an extra pass through the buffer takes a little extra time,
2289 but is infinitely less hairy than trying to handle trigraphs inside
2290 strings, etc. everywhere, and also makes sure that trigraphs are
2291 only translated in the top level of processing. */
2297 register U_CHAR c
, *bptr
;
2298 register const U_CHAR
*fptr
, *sptr
, *lptr
;
2301 fptr
= sptr
= bptr
= buf
->buf
;
2302 lptr
= fptr
+ buf
->length
;
2303 while ((sptr
= index0 (sptr
, '?', (size_t) (lptr
- sptr
))) != NULL
) {
2340 len
= sptr
- fptr
- 2;
2342 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2343 C, this will be memmove (). */
2344 if (bptr
!= fptr
&& len
> 0)
2345 bcopy ((const PTR
) fptr
, (PTR
) bptr
, len
);
2351 len
= buf
->length
- (fptr
- buf
->buf
);
2352 if (bptr
!= fptr
&& len
> 0)
2353 bcopy ((const PTR
) fptr
, (PTR
) bptr
, len
);
2354 buf
->length
-= fptr
- bptr
;
2355 buf
->buf
[buf
->length
] = '\0';
2356 if (warn_trigraphs
&& fptr
!= bptr
)
2357 warning_with_line (0, "%lu trigraph(s) encountered",
2358 (unsigned long) (fptr
- bptr
) / 2);
2361 /* Warn about white space between backslash and end of line. */
2364 check_white_space (buf
)
2367 register const U_CHAR
*sptr
= buf
->buf
;
2368 register const U_CHAR
*lptr
= sptr
+ buf
->length
;
2369 register const U_CHAR
*nptr
;
2372 nptr
= sptr
= buf
->buf
;
2373 lptr
= sptr
+ buf
->length
;
2375 (nptr
= index0 (nptr
, '\n', (size_t) (lptr
- nptr
))) != NULL
;
2377 register const U_CHAR
*p
= nptr
;
2379 for (p
= nptr
; sptr
< p
; p
--) {
2380 if (! is_hor_space
[p
[-1]]) {
2381 if (p
[-1] == '\\' && p
!= nptr
)
2382 warning_with_line (line
,
2383 "`\\' followed by white space at end of line");
2390 /* Move all backslash-newline pairs out of embarrassing places.
2391 Exchange all such pairs following BP
2392 with any potentially-embarrassing characters that follow them.
2393 Potentially-embarrassing characters are / and *
2394 (because a backslash-newline inside a comment delimiter
2395 would cause it not to be recognized).
2396 We assume that *BP == '\\'. */
2402 register U_CHAR
*p
= bp
;
2404 /* First count the backslash-newline pairs here. */
2409 } while (*p
== '\\');
2411 /* What follows the backslash-newlines is not embarrassing. */
2413 if (*p
!= '/' && *p
!= '*')
2414 /* What follows the backslash-newlines is not embarrassing. */
2417 /* Copy all potentially embarrassing characters
2418 that follow the backslash-newline pairs
2419 down to where the pairs originally started. */
2422 while (*p
== '*' || *p
== '/');
2424 /* Now write the same number of pairs after the embarrassing chars. */
2431 /* Like newline_fix but for use within a directive-name.
2432 Move any backslash-newlines up past any following symbol constituents. */
2435 name_newline_fix (bp
)
2438 register U_CHAR
*p
= bp
;
2440 /* First count the backslash-newline pairs here. */
2445 } while (*p
== '\\');
2447 /* What follows the backslash-newlines is not embarrassing. */
2450 /* What follows the backslash-newlines is not embarrassing. */
2453 /* Copy all potentially embarrassing characters
2454 that follow the backslash-newline pairs
2455 down to where the pairs originally started. */
2458 while (is_idchar
[*p
]);
2460 /* Now write the same number of pairs after the embarrassing chars. */
2467 /* Look for lint commands in comments.
2469 When we come in here, ibp points into a comment. Limit is as one expects.
2470 scan within the comment -- it should start, after lwsp, with a lint command.
2471 If so that command is returned as a (constant) string.
2473 Upon return, any arg will be pointed to with argstart and will be
2474 arglen long. Note that we don't parse that arg since it will just
2475 be printed out again. */
2478 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2479 register const U_CHAR
*ibp
;
2480 register const U_CHAR
*limit
;
2481 const U_CHAR
**argstart
; /* point to command arg */
2482 int *arglen
, *cmdlen
; /* how long they are */
2484 HOST_WIDEST_INT linsize
;
2485 register const U_CHAR
*numptr
; /* temp for arg parsing */
2489 SKIP_WHITE_SPACE (ibp
);
2491 if (ibp
>= limit
) return NULL
;
2493 linsize
= limit
- ibp
;
2495 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2496 if ((linsize
>= 10) && !bcmp (ibp
, "NOTREACHED", 10)) {
2498 return "NOTREACHED";
2500 if ((linsize
>= 8) && !bcmp (ibp
, "ARGSUSED", 8)) {
2504 if ((linsize
>= 11) && !bcmp (ibp
, "LINTLIBRARY", 11)) {
2506 return "LINTLIBRARY";
2508 if ((linsize
>= 7) && !bcmp (ibp
, "VARARGS", 7)) {
2510 ibp
+= 7; linsize
-= 7;
2511 if ((linsize
== 0) || ! ISDIGIT (*ibp
)) return "VARARGS";
2513 /* OK, read a number */
2514 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && ISDIGIT (*numptr
);
2516 *arglen
= numptr
- *argstart
;
2523 * The main loop of the program.
2525 * Read characters from the input stack, transferring them to the
2528 * Macros are expanded and push levels on the input stack.
2529 * At the end of such a level it is popped off and we keep reading.
2530 * At the end of any other kind of level, we return.
2531 * #-directives are handled, except within macros.
2533 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2534 * and insert them when appropriate. This is set while scanning macro
2535 * arguments before substitution. It is zero when scanning for final output.
2536 * There are two types of Newline markers:
2537 * * Newline - follows a macro name that was not expanded
2538 * because it appeared inside an expansion of the same macro.
2539 * This marker prevents future expansion of that identifier.
2540 * When the input is rescanned into the final output, these are deleted.
2541 * These are also deleted by ## concatenation.
2542 * * Newline Space (or Newline and any other whitespace character)
2543 * stands for a place that tokens must be separated or whitespace
2544 * is otherwise desirable, but where the ANSI standard specifies there
2545 * is no whitespace. This marker turns into a Space (or whichever other
2546 * whitespace char appears in the marker) in the final output,
2547 * but it turns into nothing in an argument that is stringified with #.
2548 * Such stringified arguments are the only place where the ANSI standard
2549 * specifies with precision that whitespace may not appear.
2551 * During this function, IP->bufp is kept cached in IBP for speed of access.
2552 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2553 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2554 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2555 * explicitly, and before RECACHE, since RECACHE uses OBP.
2559 rescan (op
, output_marks
)
2563 /* Character being scanned in main loop. */
2566 /* Length of pending accumulated identifier. */
2567 register int ident_length
= 0;
2569 /* Hash code of pending accumulated identifier. */
2570 register int hash
= 0;
2572 /* Current input level (&instack[indepth]). */
2575 /* Pointer for scanning input. */
2576 register U_CHAR
*ibp
;
2578 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2579 register U_CHAR
*limit
;
2581 /* Pointer for storing output. */
2582 register U_CHAR
*obp
;
2584 /* REDO_CHAR is nonzero if we are processing an identifier
2585 after backing up over the terminating character.
2586 Sometimes we process an identifier without backing up over
2587 the terminating character, if the terminating character
2588 is not special. Backing up is done so that the terminating character
2589 will be dispatched on again once the identifier is dealt with. */
2592 /* 1 if within an identifier inside of which a concatenation
2593 marker (Newline -) has been seen. */
2594 int concatenated
= 0;
2596 /* While scanning a comment or a string constant,
2597 this records the line it started on, for error messages. */
2600 /* Record position of last `real' newline. */
2601 U_CHAR
*beg_of_line
;
2603 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2606 do { ip->macro->type = T_MACRO; \
2607 if (ip->free_ptr) free (ip->free_ptr); \
2608 --indepth; } while (0)
2610 /* Reload `rescan's local variables that describe the current
2611 level of the input stack. */
2614 do { ip = &instack[indepth]; \
2616 limit = ip->buf + ip->length; \
2618 check_expand (op, limit - ibp); \
2620 obp = op->bufp; } while (0)
2622 if (no_output
&& instack
[indepth
].fname
!= 0)
2623 skip_if_group (&instack
[indepth
], 1, NULL
);
2630 /* Our caller must always put a null after the end of
2631 the input at each input stack level. */
2641 if (*ibp
== '\n' && !ip
->macro
) {
2642 /* At the top level, always merge lines ending with backslash-newline,
2643 even in middle of identifier. But do not merge lines in a macro,
2644 since backslash might be followed by a newline-space marker. */
2647 --obp
; /* remove backslash from obuf */
2650 /* If ANSI, backslash is just another character outside a string. */
2653 /* Otherwise, backslash suppresses specialness of following char,
2654 so copy it here to prevent the switch from seeing it.
2655 But first get any pending identifier processed. */
2656 if (ident_length
> 0)
2663 if (ident_length
|| ip
->macro
|| traditional
)
2665 while (*ibp
== '\\' && ibp
[1] == '\n') {
2671 /* Treat this %: digraph as if it were #. */
2675 if (assertions_flag
) {
2678 /* Copy #foo (bar lose) without macro expansion. */
2679 obp
[-1] = '#'; /* In case it was '%'. */
2680 SKIP_WHITE_SPACE (ibp
);
2681 while (is_idchar
[*ibp
])
2683 SKIP_WHITE_SPACE (ibp
);
2686 skip_paren_group (ip
);
2687 bcopy ((const PTR
) ibp
, (PTR
) obp
, ip
->bufp
- ibp
);
2688 obp
+= ip
->bufp
- ibp
;
2694 /* If this is expanding a macro definition, don't recognize
2695 preprocessing directives. */
2698 /* If this is expand_into_temp_buffer,
2699 don't recognize them either. Warn about them
2700 only after an actual newline at this level,
2701 not at the beginning of the input level. */
2703 if (ip
->buf
!= beg_of_line
)
2704 warning ("preprocessing directive not recognized within macro arg");
2711 /* # keyword: a # must be first nonblank char on the line */
2712 if (beg_of_line
== 0)
2717 /* Scan from start of line, skipping whitespace, comments
2718 and backslash-newlines, and see if we reach this #.
2719 If not, this # is not special. */
2721 /* If -traditional, require # to be at beginning of line. */
2724 if (is_hor_space
[*bp
])
2726 else if (*bp
== '\\' && bp
[1] == '\n')
2728 else if (*bp
== '/' && bp
[1] == '*') {
2742 #ifdef MULTIBYTE_CHARS
2744 length
= local_mblen (bp
, limit
- bp
);
2752 /* There is no point in trying to deal with C++ // comments here,
2753 because if there is one, then this # must be part of the
2754 comment and we would never reach here. */
2760 while (bp
[1] == '\\' && bp
[2] == '\n')
2764 /* %: appears at start of line; skip past the ':' too. */
2773 /* This # can start a directive. */
2775 --obp
; /* Don't copy the '#' */
2779 if (! handle_directive (ip
, op
)) {
2783 /* Not a known directive: treat it as ordinary text.
2784 IP, OP, IBP, etc. have not been changed. */
2785 if (no_output
&& instack
[indepth
].fname
) {
2786 /* If not generating expanded output,
2787 what we do with ordinary text is skip it.
2788 Discard everything until next # directive. */
2789 skip_if_group (&instack
[indepth
], 1, 0);
2794 *obp
++ = '#'; /* Copy # (even if it was originally %:). */
2795 /* Don't expand an identifier that could be a macro directive.
2796 (Section 3.8.3 of the ANSI C standard) */
2797 SKIP_WHITE_SPACE (ibp
);
2798 if (is_idstart
[*ibp
])
2801 while (is_idchar
[*ibp
])
2809 /* A # directive has been successfully processed. */
2810 /* If not generating expanded output, ignore everything until
2811 next # directive. */
2812 if (no_output
&& instack
[indepth
].fname
)
2813 skip_if_group (&instack
[indepth
], 1, 0);
2819 case '\"': /* skip quoted string */
2821 /* A single quoted string is treated like a double -- some
2822 programs (e.g., troff) are perverse this way */
2824 /* Handle any pending identifier;
2825 but the L in L'...' or L"..." is not an identifier. */
2827 if (! (ident_length
== 1 && hash
== HASHSTEP (0, 'L')))
2829 ident_length
= hash
= 0;
2832 start_line
= ip
->lineno
;
2834 /* Skip ahead to a matching quote. */
2838 if (ip
->macro
!= 0) {
2839 /* try harder: this string crosses a macro expansion boundary.
2840 This can happen naturally if -traditional.
2841 Otherwise, only -D can make a macro with an unmatched quote. */
2847 error_with_line (line_for_error (start_line
),
2848 "unterminated string or character constant");
2849 if (multiline_string_line
) {
2850 error_with_line (multiline_string_line
,
2851 "possible real start of unterminated constant");
2852 multiline_string_line
= 0;
2860 if (warn_white_space
&& ip
->fname
&& is_hor_space
[ibp
[-2]])
2861 warning ("white space at end of line in string");
2864 /* Traditionally, end of line ends a string constant with no error.
2865 So exit the loop and record the new line. */
2871 error_with_line (line_for_error (start_line
),
2872 "unterminated character constant");
2875 if (multiline_string_line
== 0) {
2877 pedwarn_with_line (line_for_error (start_line
),
2878 "string constant runs past end of line");
2879 multiline_string_line
= ip
->lineno
- 1;
2885 /* Backslash newline is replaced by nothing at all, but
2886 keep the line counts correct. But if we are reading
2887 from a macro, keep the backslash newline, since backslash
2888 newlines have already been processed. */
2897 /* ANSI stupidly requires that in \\ the second \
2898 is *not* prevented from combining with a newline. */
2900 while (*ibp
== '\\' && ibp
[1] == '\n') {
2916 #ifdef MULTIBYTE_CHARS
2921 length
= local_mblen (ibp
, limit
- ibp
);
2925 bcopy (ibp
, obp
, length
);
2945 && !(cplusplus_comments
&& *ibp
== '/'))
2951 /* C++ style comment... */
2952 start_line
= ip
->lineno
;
2954 /* Comments are equivalent to spaces. */
2955 if (! put_out_comments
)
2959 U_CHAR
*before_bp
= ibp
;
2961 while (++ibp
< limit
) {
2964 if (put_out_comments
) {
2965 bcopy ((const PTR
) before_bp
, (PTR
) obp
, ibp
- before_bp
);
2966 obp
+= ibp
- before_bp
;
2972 if (ibp
+ 1 < limit
&& ibp
[1] == '\n')
2975 warning ("multiline `//' comment");
2977 /* Copy the newline into the output buffer, in order to
2978 avoid the pain of a #line every time a multiline comment
2980 if (!put_out_comments
)
2988 #ifdef MULTIBYTE_CHARS
2990 length
= local_mblen (ibp
, limit
- ibp
);
2992 ibp
+= (length
- 1);
3000 /* Ordinary C comment. Skip it, optionally copying it to output. */
3002 start_line
= ip
->lineno
;
3004 ++ibp
; /* Skip the star. */
3006 /* If this cpp is for lint, we peek inside the comments: */
3008 const U_CHAR
*argbp
;
3010 const char *lintcmd
=
3011 get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
3013 if (lintcmd
!= NULL
) {
3015 check_expand (op
, cmdlen
+ arglen
+ 14);
3017 /* I believe it is always safe to emit this newline: */
3019 bcopy ("#pragma lint ", (char *) obp
, 13);
3021 bcopy (lintcmd
, (char *) obp
, cmdlen
);
3026 bcopy (argbp
, (char *) obp
, arglen
);
3030 /* OK, now bring us back to the state we were in before we entered
3031 this branch. We need #line because the #pragma's newline always
3032 messes up the line count. */
3034 output_line_directive (ip
, op
, 0, same_file
);
3035 check_expand (op
, limit
- ibp
+ 2);
3041 /* Comments are equivalent to spaces.
3042 Note that we already output the slash; we might not want it.
3043 For -traditional, a comment is equivalent to nothing. */
3044 if (! put_out_comments
) {
3054 U_CHAR
*before_bp
= ibp
;
3059 if (ibp
[-2] == '/' && warn_comments
)
3060 warning ("`/*' within comment");
3069 /* Copy the newline into the output buffer, in order to
3070 avoid the pain of a #line every time a multiline comment
3072 if (!put_out_comments
)
3079 error_with_line (line_for_error (start_line
),
3080 "unterminated comment");
3084 #ifdef MULTIBYTE_CHARS
3088 length
= local_mblen (ibp
, limit
- ibp
);
3090 ibp
+= (length
- 1);
3099 if (put_out_comments
) {
3100 bcopy ((const PTR
) before_bp
, (PTR
) obp
, ibp
- before_bp
);
3101 obp
+= ibp
- before_bp
;
3107 if (! is_idchar
['$'])
3110 pedwarn ("`$' in identifier");
3113 case '0': case '1': case '2': case '3': case '4':
3114 case '5': case '6': case '7': case '8': case '9':
3115 /* If digit is not part of identifier, it starts a number,
3116 which means that following letters are not an identifier.
3117 "0x5" does not refer to an identifier "x5".
3118 So copy all alphanumerics that follow without accumulating
3119 as an identifier. Periods also, for sake of "3.e7". */
3121 if (ident_length
== 0) {
3124 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3130 if (!is_idchar
[c
] && c
!= '.') {
3135 /* A sign can be part of a preprocessing number
3136 if it follows an `e' or `p'. */
3137 if (c
== 'e' || c
== 'E' || c
== 'p' || c
== 'P') {
3139 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3144 if (*ibp
== '+' || *ibp
== '-') {
3146 /* But traditional C does not let the token go past the sign,
3147 and C89 does not allow `p'. */
3148 if (traditional
|| (c89
&& (c
== 'p' || c
== 'P')))
3158 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3159 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3160 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3161 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3163 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3164 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3165 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3166 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3170 /* Compute step of hash function, to avoid a proc call on every token */
3171 hash
= HASHSTEP (hash
, c
);
3175 if (ip
->fname
== 0 && *ibp
== '-') {
3176 /* Newline - inhibits expansion of preceding token.
3177 If expanding a macro arg, we keep the newline -.
3178 In final output, it is deleted.
3179 We recognize Newline - in macro bodies and macro args. */
3180 if (! concatenated
) {
3185 if (!output_marks
) {
3188 /* If expanding a macro arg, keep the newline -. */
3194 /* If reprocessing a macro expansion, newline is a special marker. */
3195 else if (ip
->macro
!= 0) {
3196 /* Newline White is a "funny space" to separate tokens that are
3197 supposed to be separate but without space between.
3198 Here White means any whitespace character.
3199 Newline - marks a recursive macro use that is not
3200 supposed to be expandable. */
3202 if (is_space
[*ibp
]) {
3203 /* Newline Space does not prevent expansion of preceding token
3204 so expand the preceding token and then come back. */
3205 if (ident_length
> 0)
3208 /* If generating final output, newline space makes a space. */
3209 if (!output_marks
) {
3211 /* And Newline Newline makes a newline, so count it. */
3212 if (obp
[-1] == '\n')
3215 /* If expanding a macro arg, keep the newline space.
3216 If the arg gets stringified, newline space makes nothing. */
3219 } else abort (); /* Newline followed by something random? */
3223 /* If there is a pending identifier, handle it and come back here. */
3224 if (ident_length
> 0)
3229 /* Update the line counts and output a #line if necessary. */
3232 if (ip
->lineno
!= op
->lineno
) {
3234 output_line_directive (ip
, op
, 1, same_file
);
3235 check_expand (op
, limit
- ibp
);
3240 /* Come here either after (1) a null character that is part of the input
3241 or (2) at the end of the input, because there is a null there. */
3244 /* Our input really contains a null character. */
3248 /* At end of a macro-expansion level, pop it and read next level. */
3249 if (ip
->macro
!= 0) {
3252 /* If traditional, and we have an identifier that ends here,
3253 process it now, so we get the right error for recursion. */
3254 if (traditional
&& ident_length
3255 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
3264 /* If we don't have a pending identifier,
3265 return at end of input. */
3266 if (ident_length
== 0) {
3274 /* If we do have a pending identifier, just consider this null
3275 a special character and arrange to dispatch on it again.
3276 The second time, IDENT_LENGTH will be zero so we will return. */
3282 /* Handle the case of a character such as /, ', " or null
3283 seen following an identifier. Back over it so that
3284 after the identifier is processed the special char
3285 will be dispatched on again. */
3295 if (ident_length
> 0) {
3296 register HASHNODE
*hp
;
3298 /* We have just seen an identifier end. If it's a macro, expand it.
3300 IDENT_LENGTH is the length of the identifier
3301 and HASH is its hash code.
3303 The identifier has already been copied to the output,
3304 so if it is a macro we must remove it.
3306 If REDO_CHAR is 0, the char that terminated the identifier
3307 has been skipped in the output and the input.
3308 OBP-IDENT_LENGTH-1 points to the identifier.
3309 If the identifier is a macro, we must back over the terminator.
3311 If REDO_CHAR is 1, the terminating char has already been
3312 backed over. OBP-IDENT_LENGTH points to the identifier. */
3314 if (!pcp_outfile
|| pcp_inside_if
) {
3315 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
3318 if (hp
->length
== ident_length
) {
3319 int obufp_before_macroname
;
3320 int op_lineno_before_macroname
;
3321 register int i
= ident_length
;
3322 register U_CHAR
*p
= hp
->name
;
3323 register U_CHAR
*q
= obp
- i
;
3329 do { /* All this to avoid a strncmp () */
3334 /* We found a use of a macro name.
3335 see if the context shows it is a macro call. */
3337 /* Back up over terminating character if not already done. */
3343 /* Save this as a displacement from the beginning of the output
3344 buffer. We can not save this as a position in the output
3345 buffer, because it may get realloc'ed by RECACHE. */
3346 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
3347 op_lineno_before_macroname
= op
->lineno
;
3349 if (hp
->type
== T_PCSTRING
) {
3350 pcstring_used (hp
); /* Mark the definition of this key
3351 as needed, ensuring that it
3353 break; /* Exit loop, since the key cannot have a
3354 definition any longer. */
3357 /* Record whether the macro is disabled. */
3358 disabled
= hp
->type
== T_DISABLED
;
3360 /* This looks like a macro ref, but if the macro was disabled,
3361 just copy its name and put in a marker if requested. */
3365 /* This error check caught useful cases such as
3366 #define foo(x,y) bar (x (y,0), y)
3369 error ("recursive use of macro `%s'", hp
->name
);
3374 check_expand (op
, limit
- ibp
+ 2);
3382 /* If macro wants an arglist, verify that a '(' follows.
3383 first skip all whitespace, copying it to the output
3384 after the macro name. Then, if there is no '(',
3385 decide this is not a macro call and leave things that way. */
3386 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
3387 && hp
->value
.defn
->nargs
>= 0)
3389 U_CHAR
*old_ibp
= ibp
;
3390 U_CHAR
*old_obp
= obp
;
3391 int old_iln
= ip
->lineno
;
3392 int old_oln
= op
->lineno
;
3395 /* Scan forward over whitespace, copying it to the output. */
3396 if (ibp
== limit
&& ip
->macro
!= 0) {
3401 old_iln
= ip
->lineno
;
3402 old_oln
= op
->lineno
;
3404 else if (is_space
[*ibp
]) {
3406 if (ibp
[-1] == '\n') {
3407 if (ip
->macro
== 0) {
3408 /* Newline in a file. Count it. */
3411 } else if (!output_marks
) {
3412 /* A newline mark, and we don't want marks
3413 in the output. If it is newline-hyphen,
3414 discard it entirely. Otherwise, it is
3415 newline-whitechar, so keep the whitechar. */
3425 /* A newline mark; copy both chars to the output. */
3432 else if (*ibp
== '/') {
3433 /* If a comment, copy it unchanged or discard it. */
3435 newline_fix (ibp
+ 1);
3436 if (ibp
[1] == '*') {
3437 if (put_out_comments
) {
3440 } else if (! traditional
) {
3443 for (ibp
+= 2; ibp
< limit
; ibp
++) {
3444 /* We need not worry about newline-marks,
3445 since they are never found in comments. */
3446 if (ibp
[0] == '*') {
3448 newline_fix (ibp
+ 1);
3449 if (ibp
[1] == '/') {
3451 if (put_out_comments
) {
3458 else if (*ibp
== '\n') {
3459 /* Newline in a file. Count it. */
3465 #ifdef MULTIBYTE_CHARS
3467 length
= local_mblen (ibp
, limit
- ibp
);
3470 if (put_out_comments
)
3472 bcopy (ibp
, obp
, length
- 1);
3475 ibp
+= (length
- 1);
3479 if (put_out_comments
)
3482 } else if (ibp
[1] == '/' && cplusplus_comments
) {
3483 if (put_out_comments
) {
3486 } else if (! traditional
) {
3489 for (ibp
+= 2; ; ibp
++)
3493 if (*ibp
== '\\' && ibp
[1] == '\n')
3495 if (put_out_comments
)
3500 #ifdef MULTIBYTE_CHARS
3502 length
= local_mblen (ibp
, limit
- ibp
);
3505 if (put_out_comments
)
3507 bcopy (ibp
, obp
, length
- 1);
3510 ibp
+= (length
- 1);
3514 if (put_out_comments
)
3520 else if (ibp
[0] == '\\' && ibp
[1] == '\n') {
3527 /* It isn't a macro call.
3528 Put back the space that we just skipped. */
3531 ip
->lineno
= old_iln
;
3532 op
->lineno
= old_oln
;
3533 /* Exit the for loop. */
3538 /* This is now known to be a macro call.
3539 Discard the macro name from the output,
3540 along with any following whitespace just copied,
3541 but preserve newlines if not outputting marks since this
3542 is more likely to do the right thing with line numbers. */
3543 obp
= op
->buf
+ obufp_before_macroname
;
3545 op
->lineno
= op_lineno_before_macroname
;
3547 int newlines
= op
->lineno
- op_lineno_before_macroname
;
3548 while (0 < newlines
--)
3552 /* Prevent accidental token-pasting with a character
3553 before the macro call. */
3554 if (!traditional
&& obp
!= op
->buf
) {
3556 case '!': case '%': case '&': case '*':
3557 case '+': case '-': case '.': case '/':
3558 case ':': case '<': case '=': case '>':
3560 /* If we are expanding a macro arg, make a newline marker
3561 to separate the tokens. If we are making real output,
3562 a plain space will do. */
3569 /* Expand the macro, reading arguments as needed,
3570 and push the expansion on the input stack. */
3573 macroexpand (hp
, op
);
3575 /* Reexamine input stack, since macroexpand has pushed
3576 a new level on it. */
3583 } /* End hash-table-search loop */
3585 ident_length
= hash
= 0; /* Stop collecting identifier */
3588 } /* End if (ident_length > 0) */
3590 } /* End per-char loop */
3592 /* Come here to return -- but first give an error message
3593 if there was an unterminated successful conditional. */
3595 if (if_stack
!= ip
->if_stack
)
3599 switch (if_stack
->type
)
3620 error_with_line (line_for_error (if_stack
->lineno
),
3621 "unterminated `#%s' conditional", str
);
3623 if_stack
= ip
->if_stack
;
3627 * Rescan a string into a temporary buffer and return the result
3628 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3630 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3631 * and insert such markers when appropriate. See `rescan' for details.
3632 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3633 * before substitution; it is 0 for other uses.
3636 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
3638 const U_CHAR
*limit
;
3639 int output_marks
, assertions
;
3641 register FILE_BUF
*ip
;
3643 int length
= limit
- buf
;
3645 int odepth
= indepth
;
3646 int save_assertions_flag
= assertions_flag
;
3648 assertions_flag
= assertions
;
3653 /* Set up the input on the input stack. */
3655 buf1
= (U_CHAR
*) alloca (length
+ 1);
3657 register const U_CHAR
*p1
= buf
;
3658 register U_CHAR
*p2
= buf1
;
3665 /* Set up to receive the output. */
3667 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3668 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3669 obuf
.nominal_fname
= 0;
3676 obuf
.system_header_p
= 0;
3678 CHECK_DEPTH ({return obuf
;});
3682 ip
= &instack
[indepth
];
3684 ip
->nominal_fname
= 0;
3685 ip
->nominal_fname_len
= 0;
3687 ip
->system_header_p
= 0;
3690 ip
->length
= length
;
3691 ip
->buf
= ip
->bufp
= buf1
;
3692 ip
->if_stack
= if_stack
;
3694 ip
->lineno
= obuf
.lineno
= 1;
3696 /* Scan the input, create the output. */
3697 rescan (&obuf
, output_marks
);
3699 /* Pop input stack to original state. */
3702 if (indepth
!= odepth
)
3705 assertions_flag
= save_assertions_flag
;
3710 * Process a # directive. Expects IP->bufp to point after the '#', as in
3711 * `#define foo bar'. Passes to the directive handler
3712 * (do_define, do_include, etc.): the addresses of the 1st and
3713 * last chars of the directive (starting immediately after the #
3714 * keyword), plus op and the keyword table pointer. If the directive
3715 * contains comments it is copied into a temporary buffer sans comments
3716 * and the temporary buffer is passed to the directive handler instead.
3717 * Likewise for backslash-newlines.
3719 * Returns nonzero if this was a known # directive.
3720 * Otherwise, returns zero, without advancing the input pointer.
3724 handle_directive (ip
, op
)
3727 register U_CHAR
*bp
, *cp
;
3728 register struct directive
*kt
;
3729 register int ident_length
;
3732 /* Nonzero means we must copy the entire directive
3733 to get rid of comments or backslash-newlines. */
3734 int copy_directive
= 0;
3736 U_CHAR
*ident
, *after_ident
;
3740 /* Record where the directive started. do_xifdef needs this. */
3741 directive_start
= bp
- 1;
3743 ignore_escape_flag
= 1;
3745 /* Skip whitespace and \-newline. */
3747 if (is_hor_space
[*bp
]) {
3748 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3749 pedwarn_strange_white_space (*bp
);
3751 } else if (*bp
== '/') {
3753 newline_fix (bp
+ 1);
3754 if (! (bp
[1] == '*' || (cplusplus_comments
&& bp
[1] == '/')))
3757 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3759 } else if (*bp
== '\\' && bp
[1] == '\n') {
3760 bp
+= 2; ip
->lineno
++;
3764 /* Now find end of directive name.
3765 If we encounter a backslash-newline, exchange it with any following
3766 symbol-constituents so that we end up with a contiguous name. */
3774 name_newline_fix (cp
);
3780 ident_length
= cp
- bp
;
3784 /* A line of just `#' becomes blank. */
3786 if (ident_length
== 0 && *after_ident
== '\n') {
3787 ip
->bufp
= after_ident
;
3791 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3793 while (is_idchar
[*p
]) {
3794 if (*p
< '0' || *p
> '9')
3798 /* Handle # followed by a line number. */
3799 if (p
!= ident
&& !is_idchar
[*p
]) {
3800 static struct directive line_directive_table
[] = {
3801 { 4, do_line
, "line", T_LINE
},
3804 pedwarn ("`#' followed by integer");
3805 after_ident
= ident
;
3806 kt
= line_directive_table
;
3807 ignore_escape_flag
= 0;
3811 /* Avoid error for `###' and similar cases unless -pedantic. */
3813 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3815 if (pedantic
&& !lang_asm
)
3816 warning ("invalid preprocessing directive");
3822 error ("invalid preprocessing directive name");
3828 * Decode the keyword and call the appropriate expansion
3829 * routine, after moving the input pointer up to the next line.
3831 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3832 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3833 register U_CHAR
*buf
;
3834 register U_CHAR
*limit
;
3837 int *already_output
;
3839 /* Nonzero means do not delete comments within the directive.
3840 #define needs this when -traditional. */
3845 limit
= ip
->buf
+ ip
->length
;
3848 keep_comments
= traditional
&& kt
->type
== T_DEFINE
;
3849 /* #import is defined only in Objective C, or when on the NeXT. */
3850 if (kt
->type
== T_IMPORT
3851 && !(objc
|| lookup ((const U_CHAR
*) "__NeXT__", -1, -1)))
3854 /* Find the end of this directive (first newline not backslashed
3855 and not in a string or comment).
3856 Set COPY_DIRECTIVE if the directive must be copied
3857 (it contains a backslash-newline or a comment). */
3859 buf
= bp
= after_ident
;
3860 while (bp
< limit
) {
3861 register U_CHAR c
= *bp
++;
3868 } else if (traditional
&& bp
< limit
)
3873 /* "..." is special for #include. */
3874 if (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)) {
3875 while (bp
< limit
&& *bp
!= '\n') {
3880 if (*bp
== '\\' && bp
[1] == '\n') {
3891 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3892 /* Don't bother calling the directive if we already got an error
3893 message due to unterminated string. Skip everything and pretend
3894 we called the directive. */
3897 /* Traditional preprocessing permits unterminated strings. */
3906 /* <...> is special for #include. */
3908 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3910 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3911 if (*bp
== '\\' && bp
[1] == '\n') {
3924 || (cplusplus_comments
&& *bp
== '/')) {
3925 U_CHAR
*obp
= bp
- 1;
3927 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3929 /* No need to copy the directive because of a comment at the end;
3930 just don't include the comment in the directive. */
3931 if (!put_out_comments
) {
3933 for (p
= bp
; *p
== ' ' || *p
== '\t'; p
++)
3940 /* Don't remove the comments if -traditional. */
3941 if (! keep_comments
)
3950 pedwarn_strange_white_space (c
);
3954 --bp
; /* Point to the newline */
3962 resume_p
= ip
->bufp
;
3963 /* BP is the end of the directive.
3964 RESUME_P is the next interesting data after the directive.
3965 A comment may come between. */
3967 /* If a directive should be copied through, and -C was given,
3968 pass it through before removing comments. */
3969 if (!no_output
&& put_out_comments
3970 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
3971 ? dump_macros
== dump_definitions
3972 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
3973 : kt
->type
== T_PRAGMA
)) {
3976 /* Output directive name. */
3977 check_expand (op
, kt
->length
+ 2);
3978 /* Make sure # is at the start of a line */
3979 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3984 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3985 op
->bufp
+= kt
->length
;
3987 /* Output arguments. */
3989 check_expand (op
, len
);
3990 bcopy (buf
, (char *) op
->bufp
, len
);
3992 /* Take account of any (escaped) newlines just output. */
3994 if (buf
[len
] == '\n')
3997 already_output
= &junk
;
3998 } /* Don't we need a newline or #line? */
4000 if (copy_directive
) {
4001 register U_CHAR
*xp
= buf
;
4002 /* Need to copy entire directive into temp buffer before dispatching */
4004 /* room for directive plus some slop */
4005 cp
= (U_CHAR
*) alloca (2 * (bp
- buf
) + 5);
4008 /* Copy to the new buffer, deleting comments
4009 and backslash-newlines (and whitespace surrounding the latter
4010 if outside of char and string constants). */
4013 register U_CHAR c
= *xp
++;
4018 abort (); /* A bare newline should never part of the line. */
4021 /* <...> is special for #include. */
4023 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
4025 while (xp
< bp
&& c
!= '>') {
4027 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
4038 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
4039 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
4041 SKIP_WHITE_SPACE (xp
);
4042 } else if (is_hor_space
[*xp
]) {
4044 SKIP_WHITE_SPACE (xp
);
4046 } else if (traditional
&& xp
< bp
) {
4054 int backslash_newlines_p
;
4056 register const U_CHAR
*bp1
4057 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
4058 NULL_PTR
, &backslash_newlines_p
,
4060 if (backslash_newlines_p
)
4063 /* With something like:
4068 we should still remove the backslash-newline
4069 pair as part of phase two. */
4070 if (xp
[0] == '\\' && xp
[1] == '\n')
4076 /* This is the same as the loop above, but taking
4077 advantage of the fact that we know there are no
4078 backslash-newline pairs. */
4086 || (cplusplus_comments
&& *xp
== '/')) {
4088 /* If we already copied the directive through,
4089 already_output != 0 prevents outputting comment now. */
4090 skip_to_end_of_comment (ip
, already_output
, 0);
4092 while (xp
!= ip
->bufp
)
4094 /* Delete or replace the slash. */
4095 else if (traditional
)
4104 /* Null-terminate the copy. */
4110 ip
->bufp
= resume_p
;
4112 /* Some directives should be written out for cc1 to process,
4113 just as if they were not defined. And sometimes we're copying
4114 directives through. */
4116 if (!no_output
&& already_output
== 0
4117 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
4118 ? (int) dump_names
<= (int) dump_macros
4119 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
4120 : kt
->type
== T_PRAGMA
)) {
4123 /* Output directive name. */
4124 check_expand (op
, kt
->length
+ 1);
4126 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
4127 op
->bufp
+= kt
->length
;
4129 if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
4130 /* Output `#define name' only. */
4133 SKIP_WHITE_SPACE (xp
);
4135 while (is_idchar
[*xp
]) xp
++;
4137 check_expand (op
, len
+ 1);
4139 bcopy (yp
, (char *) op
->bufp
, len
);
4141 /* Output entire directive. */
4143 check_expand (op
, len
);
4144 bcopy (buf
, (char *) op
->bufp
, len
);
4149 /* Call the appropriate directive handler. buf now points to
4150 either the appropriate place in the input buffer, or to
4151 the temp buffer if it was necessary to make one. cp
4152 points to the first char after the contents of the (possibly
4153 copied) directive, in either case. */
4154 (*kt
->func
) (buf
, cp
, op
, kt
);
4155 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
4161 /* It is deliberate that we don't warn about undefined directives.
4162 That is the responsibility of cc1. */
4169 static struct tm tmbuf
;
4170 if (! tmbuf
.tm_mday
) {
4171 time_t t
= time ((time_t *) 0);
4172 struct tm
*tm
= localtime (&t
);
4176 /* Use 0000-01-01 00:00:00 if local time is not available. */
4177 tmbuf
.tm_year
= -1900;
4184 static const char * const monthnames
[] = {
4185 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4186 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4190 * expand things like __FILE__. Place the expansion into the output
4191 * buffer *without* rescanning.
4195 special_symbol (hp
, op
)
4202 FILE_BUF
*ip
= NULL
;
4205 int paren
= 0; /* For special `defined' keyword */
4207 if (pcp_outfile
&& pcp_inside_if
4208 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4209 error ("Predefined macro `%s' used inside `#if' during precompilation",
4212 for (i
= indepth
; i
>= 0; i
--)
4213 if (instack
[i
].fname
!= NULL
) {
4218 error ("cccp error: not in any file?!");
4219 return; /* the show must go on */
4226 FILE_BUF
*p
= hp
->type
== T_FILE
? ip
: &instack
[0];
4227 const char *string
= p
->nominal_fname
;
4231 size_t string_len
= p
->nominal_fname_len
;
4232 char *newbuf
= (char *) alloca (3 + 4 * string_len
);
4233 quote_string (newbuf
, string
, string_len
);
4242 case T_INCLUDE_LEVEL
:
4244 /* Eight bytes ought to be more than enough */
4245 char *newbuf
= (char *) alloca (8);
4247 for (i
= indepth
; i
>= 0; i
--)
4248 if (instack
[i
].fname
!= NULL
)
4250 sprintf (newbuf
, "%d", true_indepth
- 1);
4257 char *newbuf
= (char *) alloca (3 + strlen (version_string
));
4258 sprintf (newbuf
, "\"%s\"", version_string
);
4263 #ifndef NO_BUILTIN_SIZE_TYPE
4269 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4270 case T_PTRDIFF_TYPE
:
4279 case T_USER_LABEL_PREFIX_TYPE
:
4280 buf
= user_label_prefix
;
4283 case T_REGISTER_PREFIX_TYPE
:
4284 buf
= REGISTER_PREFIX
;
4287 case T_IMMEDIATE_PREFIX_TYPE
:
4288 buf
= IMMEDIATE_PREFIX
;
4292 buf
= hp
->value
.cpval
;
4293 #ifdef STDC_0_IN_SYSTEM_HEADERS
4294 if (ip
->system_header_p
4295 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
4296 && !lookup ((const U_CHAR
*) "__STRICT_ANSI__", -1, -1))
4299 if (pcp_inside_if
&& pcp_outfile
)
4300 /* Output a precondition for this macro use */
4301 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4306 char *newbuf
= (char *) alloca (10);
4307 sprintf (newbuf
, "%d", ip
->lineno
);
4315 char *newbuf
= (char *) alloca (20);
4316 timebuf
= timestamp ();
4317 if (hp
->type
== T_DATE
)
4318 sprintf (newbuf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4319 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4321 sprintf (newbuf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
,
4322 timebuf
->tm_min
, timebuf
->tm_sec
);
4327 case T_SPEC_DEFINED
:
4328 buf
= " 0 "; /* Assume symbol is not defined */
4329 ip
= &instack
[indepth
];
4330 SKIP_WHITE_SPACE (ip
->bufp
);
4331 if (*ip
->bufp
== '(') {
4333 ip
->bufp
++; /* Skip over the paren */
4334 SKIP_WHITE_SPACE (ip
->bufp
);
4337 if (!is_idstart
[*ip
->bufp
])
4339 if (ip
->bufp
[0] == 'L' && (ip
->bufp
[1] == '\'' || ip
->bufp
[1] == '"'))
4341 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4342 if (pcp_outfile
&& pcp_inside_if
4343 && (hp
->type
== T_CONST
4344 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4345 /* Output a precondition for this macro use. */
4346 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4347 if (hp
->type
== T_POISON
) {
4348 error("attempt to use poisoned `%s'.", hp
->name
);
4355 if (pcp_outfile
&& pcp_inside_if
) {
4356 /* Output a precondition for this macro use */
4357 U_CHAR
*cp
= ip
->bufp
;
4358 fprintf (pcp_outfile
, "#undef ");
4359 while (is_idchar
[*cp
]) /* Ick! */
4360 fputc (*cp
++, pcp_outfile
);
4361 putc ('\n', pcp_outfile
);
4363 while (is_idchar
[*ip
->bufp
])
4365 SKIP_WHITE_SPACE (ip
->bufp
);
4367 if (*ip
->bufp
!= ')')
4374 error("attempt to use poisoned `%s'.", hp
->name
);
4375 buf
= " 0 "; /* Consider poisoned symbol to not be defined */
4380 error ("`defined' without an identifier");
4384 error ("cccp error: invalid special hash type"); /* time for gdb */
4388 check_expand (op
, len
);
4389 bcopy (buf
, (char *) op
->bufp
, len
);
4396 /* Routines to handle #directives */
4398 /* Handle #include and #import.
4399 This function expects to see "fname" or <fname> on the input. */
4402 do_include (buf
, limit
, op
, keyword
)
4403 U_CHAR
*buf
, *limit
;
4405 struct directive
*keyword
;
4407 const U_CHAR
*importing
=
4408 keyword
->type
== T_IMPORT
? (const U_CHAR
*) "" : (const U_CHAR
*) 0;
4409 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4410 static int import_warning
= 0;
4411 char *fname
; /* Dynamically allocated fname buffer */
4414 char *fbeg
, *fend
; /* Beginning and end of fname */
4417 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4418 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4419 struct file_name_list
*searchptr
= 0;
4422 int f
= -3; /* file number */
4423 struct include_file
*inc
= 0;
4425 int retried
= 0; /* Have already tried macro
4426 expanding the include line*/
4427 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4429 int vaxc_include
= 0; /* 1 for token without punctuation */
4433 const char *pcfbuflimit
;
4436 if (pedantic
&& !instack
[indepth
].system_header_p
)
4439 pedwarn ("ANSI C does not allow `#import'");
4441 pedwarn ("ANSI C does not allow `#include_next'");
4444 if (importing
&& warn_import
&& !inhibit_warnings
4445 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4447 warning ("using `#import' is not recommended");
4448 notice ("The fact that a certain header file need not be processed more than once\n\
4449 should be indicated in the header file, not where it is used.\n\
4450 The best way to do this is with a conditional of this form:\n\
4452 #ifndef _FOO_H_INCLUDED\n\
4453 #define _FOO_H_INCLUDED\n\
4454 ... <real contents of file> ...\n\
4455 #endif /* Not _FOO_H_INCLUDED */\n\
4457 Then users can use `#include' any number of times.\n\
4458 GNU C automatically avoids processing the file more than once\n\
4459 when it is equipped with such a conditional.\n");
4465 SKIP_WHITE_SPACE (fin
);
4466 /* Discard trailing whitespace so we can easily see
4467 if we have parsed all the significant chars we were given. */
4468 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4469 fbeg
= fend
= (char *) alloca (limit
- fin
);
4475 /* Copy the operand text, concatenating the strings. */
4480 goto invalid_include_file_name
;
4488 /* If not at the end, there had better be another string. */
4489 /* Skip just horiz space, and don't go past limit. */
4490 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4491 if (fin
!= limit
&& *fin
== '\"')
4498 /* We have "filename". Figure out directory this source
4499 file is coming from and put it on the front of the list. */
4501 /* If -I- was specified, don't search current dir, only spec'd ones. */
4502 if (ignore_srcdir
) break;
4504 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4508 if ((fp
->nominal_fname
) != NULL
) {
4510 /* Found a named file. Figure out dir of the file,
4511 and put it in front of the search list. */
4512 dsp
= ((struct file_name_list
*)
4513 alloca (sizeof (struct file_name_list
)
4514 + fp
->nominal_fname_len
));
4515 strcpy (dsp
->fname
, fp
->nominal_fname
);
4516 simplify_filename (dsp
->fname
);
4517 nam
= base_name (dsp
->fname
);
4520 /* for hack_vms_include_specification(), a local
4521 dir specification must start with "./" on VMS. */
4522 if (nam
== dsp
->fname
)
4529 /* But for efficiency's sake, do not insert the dir
4530 if it matches the search list's first dir. */
4531 dsp
->next
= search_start
;
4532 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4534 n
= nam
- dsp
->fname
;
4535 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4536 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4538 dsp
[0].got_name_map
= 0;
4546 while (fin
!= limit
&& *fin
!= '>')
4548 if (*fin
== '>' && fin
+ 1 == limit
) {
4550 /* If -I-, start with the first -I dir after the -I-. */
4551 search_start
= first_bracket_include
;
4559 * Support '#include xyz' like VAX-C to allow for easy use of all the
4560 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4561 * code from case '<' is repeated here) and generates a warning.
4562 * (Note: macro expansion of `xyz' takes precedence.)
4564 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4565 the pre-decrement outside of the macro. */
4566 if (retried
&& (--fin
, ISALPHA(*(U_CHAR
*) (fin
)))) {
4567 while (fin
!= limit
&& (!ISSPACE(*fin
)))
4569 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4573 /* If -I-, start with the first -I dir after the -I-. */
4574 search_start
= first_bracket_include
;
4582 /* Expand buffer and then remove any newline markers.
4583 We can't just tell expand_to_temp_buffer to omit the markers,
4584 since it would put extra spaces in include file names. */
4586 int errors_before_expansion
= errors
;
4589 trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4590 if (errors
!= errors_before_expansion
) {
4592 goto invalid_include_file_name
;
4595 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4597 while (src
!= trybuf
.bufp
) {
4598 switch ((*limit
++ = *src
++)) {
4607 const U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4608 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4621 invalid_include_file_name
:
4622 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4626 /* For #include_next, skip in the search path
4627 past the dir in which the containing file was found. */
4630 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4631 if (fp
->fname
!= NULL
) {
4632 /* fp->dir is null if the containing file was specified
4633 with an absolute file name. In that case, don't skip anything. */
4635 search_start
= fp
->dir
->next
;
4641 flen
= simplify_filename (fbeg
);
4645 error ("empty file name in `#%s'", keyword
->name
);
4649 /* Allocate this permanently, because it gets stored in the definitions
4651 fname
= xmalloc (max_include_len
+ flen
+ 1);
4652 /* + 1 above for terminating null. */
4654 system_include_depth
+= angle_brackets
;
4656 /* If specified file name is absolute, just open it. */
4658 if (absolute_filename (fbeg
)) {
4659 strcpy (fname
, fbeg
);
4660 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4664 struct bypass_dir
*next
;
4666 struct file_name_list
*searchptr
;
4667 } **bypass_slot
= 0;
4669 /* Search directory path, trying to open the file.
4670 Copy each filename tried into FNAME. */
4672 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4674 if (searchptr
== first_bracket_include
) {
4675 /* Go to bypass directory if we know we've seen this file before. */
4676 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4677 struct bypass_dir
*p
;
4678 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4680 for (p
= *bypass_slot
; p
; p
= p
->next
)
4681 if (!strcmp (fbeg
, p
->fname
)) {
4682 searchptr
= p
->searchptr
;
4689 /* Change this 1/2 Unix 1/2 VMS file specification into a
4690 full VMS file specification */
4691 if (searchptr
->fname
[0])
4693 strcpy (fname
, searchptr
->fname
);
4694 if (fname
[strlen (fname
) - 1] == ':')
4697 slashp
= strchr (fbeg
, '/');
4699 /* start at root-dir of logical device if no path given. */
4701 strcat (fname
, "[000000]");
4703 strcat (fname
, fbeg
);
4705 /* Fix up the filename */
4706 hack_vms_include_specification (fname
, vaxc_include
);
4710 /* This is a normal VMS filespec, so use it unchanged. */
4711 strcpy (fname
, fbeg
);
4712 /* if it's '#include filename', add the missing .h */
4713 if (vaxc_include
&& index(fname
,'.')==NULL
)
4714 strcat (fname
, ".h");
4717 strcpy (fname
, searchptr
->fname
);
4718 strcat (fname
, fbeg
);
4720 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4722 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4723 /* This is the first time we found this include file,
4724 and we found it after first_bracket_include.
4725 Record its location so that we can bypass to here next time. */
4726 struct bypass_dir
*p
4727 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4728 p
->next
= *bypass_slot
;
4729 p
->fname
= fname
+ strlen (searchptr
->fname
);
4730 p
->searchptr
= searchptr
;
4736 /* Our VMS hacks can produce invalid filespecs, so don't worry
4737 about errors other than EACCES. */
4738 if (errno
== EACCES
)
4741 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
4751 /* The file was already included. */
4753 /* If generating dependencies and -MG was specified, we assume missing
4754 files are leaf files, living in the same directory as the source file
4755 or other similar place; these missing files may be generated from
4756 other files and may not exist yet (eg: y.tab.h). */
4757 } else if (print_deps_missing_files
4758 && (system_include_depth
!= 0) < print_deps
)
4760 /* If it was requested as a system header file,
4761 then assume it belongs in the first place to look for such. */
4765 char *p
= (char *) alloca (strlen (search_start
->fname
)
4766 + strlen (fbeg
) + 1);
4767 strcpy (p
, search_start
->fname
);
4769 deps_output (p
, ' ');
4774 /* Otherwise, omit the directory, as if the file existed
4775 in the directory with the source. */
4776 deps_output (fbeg
, ' ');
4779 /* If -M was specified, and this header file won't be added to the
4780 dependency list, then don't count this as an error, because we can
4781 still produce correct output. Otherwise, we can't produce correct
4782 output, because there may be dependencies we need inside the missing
4783 file, and we don't know what directory this missing file exists in. */
4784 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4785 warning ("No include path in which to find %s", fbeg
);
4787 error_from_errno (fbeg
);
4789 error ("No include path in which to find %s", fbeg
);
4793 /* Actually process the file. */
4795 pcftry
= (char *) alloca (strlen (fname
) + 30);
4802 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4804 pcf
= open (pcftry
, O_RDONLY
, 0666);
4809 if (fstat (pcf
, &s
) != 0)
4810 pfatal_with_name (pcftry
);
4811 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4812 || inc
->st
.st_dev
!= s
.st_dev
)
4814 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4815 /* Don't need it any more. */
4820 /* Don't need it at all. */
4825 } while (pcf
!= -1 && !pcfbuf
);
4828 /* Actually process the file */
4830 pcfname
= xstrdup (pcftry
);
4831 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) fname
, op
);
4834 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4837 system_include_depth
-= angle_brackets
;
4842 /* Return nonzero if the given FILENAME is an absolute pathname which
4843 designates a file within one of the known "system" include file
4844 directories. We assume here that if the given FILENAME looks like
4845 it is the name of a file which resides either directly in a "system"
4846 include file directory, or within any subdirectory thereof, then the
4847 given file must be a "system" include file. This function tells us
4848 if we should suppress pedantic errors/warnings for the given FILENAME.
4850 The value is 2 if the file is a C-language system header file
4851 for which C++ should (on most systems) assume `extern "C"'. */
4854 is_system_include (filename
)
4855 register const char *filename
;
4857 struct file_name_list
*searchptr
;
4859 for (searchptr
= first_system_include
; searchptr
;
4860 searchptr
= searchptr
->next
)
4861 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4862 return searchptr
->c_system_include_path
+ 1;
4866 /* Yield the non-directory suffix of a file name. */
4872 const char *s
= fname
;
4874 #if defined (__MSDOS__) || defined (_WIN32)
4875 if (ISALPHA (s
[0]) && s
[1] == ':') s
+= 2;
4878 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4879 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4880 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4884 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4885 #ifdef DIR_SEPARATOR
4886 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4891 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4894 absolute_filename (filename
)
4895 const char *filename
;
4897 #if defined (__MSDOS__) \
4898 || (defined (_WIN32) && !defined (__CYGWIN__) && !defined (_UWIN))
4899 if (ISALPHA (filename
[0]) && filename
[1] == ':') filename
+= 2;
4901 #if defined (__CYGWIN__)
4902 /* At present, any path that begins with a drive spec is absolute. */
4903 if (ISALPHA (filename
[0]) && filename
[1] == ':') return 1;
4906 if (index (filename
, ':') != 0) return 1;
4908 if (filename
[0] == '/') return 1;
4909 #ifdef DIR_SEPARATOR
4910 if (filename
[0] == DIR_SEPARATOR
) return 1;
4915 /* Returns whether or not a given character is a directory separator.
4916 Used by simplify_filename. */
4917 static inline int is_dir_separator
PARAMS ((int));
4921 is_dir_separator(ch
)
4924 return (ch
== DIR_SEPARATOR
)
4925 #if defined (DIR_SEPARATOR_2)
4926 || (ch
== DIR_SEPARATOR_2
)
4931 /* Remove unnecessary characters from FILENAME in place,
4932 to avoid unnecessary filename aliasing.
4933 Return the length of the resulting string.
4935 Do only the simplifications allowed by Posix.
4936 It is OK to miss simplifications on non-Posix hosts,
4937 since this merely leads to suboptimal results. */
4940 simplify_filename (filename
)
4943 register char *from
= filename
;
4944 register char *to
= filename
;
4947 /* Remove redundant initial /s. */
4948 if (is_dir_separator (*from
))
4950 *to
++ = DIR_SEPARATOR
;
4951 if (is_dir_separator (*++from
))
4953 if (is_dir_separator (*++from
))
4955 /* 3 or more initial /s are equivalent to 1 /. */
4956 while (is_dir_separator (*++from
))
4961 /* On some hosts // differs from /; Posix allows this. */
4962 *to
++ = DIR_SEPARATOR
;
4972 if (from
[0] == '.' && from
[1] == '/')
4977 /* Copy this component and trailing DIR_SEPARATOR, if any. */
4978 while (!is_dir_separator (*to
++ = *from
++))
4982 /* Trim . component at end of nonempty name. */
4983 to
-= filename
<= to
- 3 && to
[-3] == DIR_SEPARATOR
&& to
[-2] == '.';
4985 /* Trim unnecessary trailing /s. */
4986 while (to0
< --to
&& to
[-1] == DIR_SEPARATOR
)
4990 return to
- filename
;
4993 #if defined(DIR_SEPARATOR_2)
4994 /* Simplify to one directory separator. */
4995 to
[-1] = DIR_SEPARATOR
;
4999 /* Skip /s after a /. */
5000 while (is_dir_separator (*from
))
5005 /* The file_name_map structure holds a mapping of file names for a
5006 particular directory. This mapping is read from the file named
5007 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5008 map filenames on a file system with severe filename restrictions,
5009 such as DOS. The format of the file name map file is just a series
5010 of lines with two tokens on each line. The first token is the name
5011 to map, and the second token is the actual name to use. */
5013 struct file_name_map
5015 struct file_name_map
*map_next
;
5020 #define FILE_NAME_MAP_FILE "header.gcc"
5022 /* Read a space delimited string of unlimited length from a stdio
5026 read_filename_string (ch
, f
)
5034 set
= alloc
= xmalloc (len
+ 1);
5038 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5040 if (set
- alloc
== len
)
5043 alloc
= xrealloc (alloc
, len
+ 1);
5044 set
= alloc
+ len
/ 2;
5054 /* Read the file name map file for DIRNAME.
5055 If DIRNAME is empty, read the map file for the working directory;
5056 otherwise DIRNAME must end in '/'. */
5058 static struct file_name_map
*
5059 read_name_map (dirname
)
5060 const char *dirname
;
5062 /* This structure holds a linked list of file name maps, one per
5064 struct file_name_map_list
5066 struct file_name_map_list
*map_list_next
;
5067 char *map_list_name
;
5068 struct file_name_map
*map_list_map
;
5070 static struct file_name_map_list
*map_list
;
5071 register struct file_name_map_list
*map_list_ptr
;
5076 for (map_list_ptr
= map_list
; map_list_ptr
;
5077 map_list_ptr
= map_list_ptr
->map_list_next
)
5078 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5079 return map_list_ptr
->map_list_map
;
5081 map_list_ptr
= ((struct file_name_map_list
*)
5082 xmalloc (sizeof (struct file_name_map_list
)));
5083 map_list_ptr
->map_list_name
= xstrdup (dirname
);
5084 map_list_ptr
->map_list_map
= NULL
;
5086 dirlen
= strlen (dirname
);
5087 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
5088 strcpy (name
, dirname
);
5089 strcat (name
, FILE_NAME_MAP_FILE
);
5090 f
= fopen (name
, "r");
5092 map_list_ptr
->map_list_map
= NULL
;
5097 while ((ch
= getc (f
)) != EOF
)
5100 struct file_name_map
*ptr
;
5105 from
= read_filename_string (ch
, f
);
5106 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5108 to
= read_filename_string (ch
, f
);
5110 simplify_filename (from
);
5111 tolen
= simplify_filename (to
);
5113 ptr
= ((struct file_name_map
*)
5114 xmalloc (sizeof (struct file_name_map
)));
5115 ptr
->map_from
= from
;
5117 /* Make the real filename absolute. */
5118 if (absolute_filename (to
))
5122 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
5123 strcpy (ptr
->map_to
, dirname
);
5124 strcat (ptr
->map_to
, to
);
5128 ptr
->map_next
= map_list_ptr
->map_list_map
;
5129 map_list_ptr
->map_list_map
= ptr
;
5131 while ((ch
= getc (f
)) != '\n')
5138 map_list_ptr
->map_list_next
= map_list
;
5139 map_list
= map_list_ptr
;
5141 return map_list_ptr
->map_list_map
;
5144 /* Try to open include file FILENAME. SEARCHPTR is the directory
5145 being tried from the include file search path.
5146 IMPORTING is "" if we are importing, null otherwise.
5147 Return -2 if found, either a matching name or a matching inode.
5148 Otherwise, open the file and return a file descriptor if successful
5149 or -1 if unsuccessful.
5150 Unless unsuccessful, put a descriptor of the included file into *PINC.
5151 This function maps filenames on file systems based on information read by
5155 open_include_file (filename
, searchptr
, importing
, pinc
)
5157 struct file_name_list
*searchptr
;
5158 const U_CHAR
*importing
;
5159 struct include_file
**pinc
;
5161 char *fname
= remap
? remap_include_file (filename
, searchptr
) : filename
;
5164 /* Look up FNAME in include_hashtab. */
5165 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
5168 struct include_file
*inc
, *head
= *phead
;
5169 for (inc
= head
; inc
; inc
= inc
->next
)
5170 if (!strcmp (fname
, inc
->fname
))
5174 || ! inc
->control_macro
5175 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
5177 fd
= open (fname
, O_RDONLY
, 0);
5182 /* if #include <dir/file> fails, try again with hacked spec. */
5183 if (!hack_vms_include_specification (fname
, 0))
5185 fd
= open (fname
, O_RDONLY
, 0);
5192 /* FNAME was not in include_hashtab; insert a new entry. */
5193 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
5196 inc
->control_macro
= 0;
5197 inc
->deps_output
= 0;
5198 if (fstat (fd
, &inc
->st
) != 0)
5199 pfatal_with_name (fname
);
5202 /* Look for another file with the same inode and device. */
5203 if (lookup_ino_include (inc
)
5204 && inc
->control_macro
5205 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
5211 /* For -M, add this file to the dependencies. */
5212 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
5213 inc
->deps_output
= 1;
5214 deps_output (fname
, ' ');
5217 /* Handle -H option. */
5218 if (print_include_names
)
5219 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
5223 inc
->control_macro
= importing
;
5229 /* Return the remapped name of the include file FILENAME.
5230 SEARCHPTR is the directory being tried from the include file path. */
5233 remap_include_file (filename
, searchptr
)
5235 struct file_name_list
*searchptr
;
5237 register struct file_name_map
*map
;
5238 register const char *from
;
5242 if (! searchptr
->got_name_map
)
5244 searchptr
->name_map
= read_name_map (searchptr
->fname
);
5245 searchptr
->got_name_map
= 1;
5248 /* Check the mapping for the directory we are using. */
5249 from
= filename
+ strlen (searchptr
->fname
);
5250 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5251 if (! strcmp (map
->map_from
, from
))
5255 from
= base_name (filename
);
5257 if (from
!= filename
|| !searchptr
)
5259 /* Try to find a mapping file for the particular directory we are
5260 looking in. Thus #include <sys/types.h> will look up sys/types.h
5261 in /usr/include/header.gcc and look up types.h in
5262 /usr/include/sys/header.gcc. */
5264 char *dir
= (char *) alloca (from
- filename
+ 1);
5265 bcopy (filename
, dir
, from
- filename
);
5266 dir
[from
- filename
] = '\0';
5268 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
5269 if (! strcmp (map
->map_from
, from
))
5276 /* Insert INC into the include file table, hashed by device and inode number.
5277 If a file with different name but same dev+ino was already in the table,
5278 return 1 and set INC's control macro to the already-known macro. */
5281 lookup_ino_include (inc
)
5282 struct include_file
*inc
;
5284 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
5285 % INCLUDE_HASHSIZE
);
5286 struct include_file
*i
= include_ino_hashtab
[hash
];
5288 include_ino_hashtab
[hash
] = inc
;
5290 for (; i
; i
= i
->next_ino
)
5291 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
5292 && inc
->st
.st_dev
== i
->st
.st_dev
) {
5293 inc
->control_macro
= i
->control_macro
;
5300 /* Process file descriptor F, which corresponds to include file INC,
5302 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5303 "system" include directories (as decided by the `is_system_include'
5305 DIRPTR is the link in the dir path through which this file was found,
5306 or 0 if the file name was absolute. */
5309 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5311 struct include_file
*inc
;
5313 int system_header_p
;
5314 struct file_name_list
*dirptr
;
5316 char *fname
= inc
->fname
;
5318 FILE_BUF
*fp
; /* For input stack frame */
5319 int missing_newline
= 0;
5321 CHECK_DEPTH (return;);
5323 fp
= &instack
[indepth
+ 1];
5324 bzero ((char *) fp
, sizeof (FILE_BUF
));
5325 fp
->nominal_fname
= fp
->fname
= fname
;
5326 fp
->nominal_fname_len
= strlen (fname
);
5330 fp
->if_stack
= if_stack
;
5331 fp
->system_header_p
= system_header_p
;
5334 if (S_ISREG (inc
->st
.st_mode
)) {
5335 size_t s
= (size_t) inc
->st
.st_size
;
5336 if (s
!= inc
->st
.st_size
|| s
+ 2 < s
)
5338 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
5341 /* Read the file contents, knowing that s is an upper bound
5342 on the number of bytes we can read. */
5343 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
5344 if (fp
->length
< 0) goto nope
;
5346 else if (S_ISDIR (inc
->st
.st_mode
)) {
5347 error ("directory `%s' specified in #include", fname
);
5351 /* Cannot count its file size before reading.
5352 First read the entire file into heap and
5353 copy them into buffer on stack. */
5358 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5361 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5363 goto nope
; /* error! */
5365 if (st_size
!= bsize
)
5366 break; /* End of file */
5368 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5371 fp
->length
= st_size
;
5374 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5375 /* Backslash-newline at end is not good enough. */
5376 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5377 fp
->buf
[fp
->length
++] = '\n';
5378 missing_newline
= 1;
5380 fp
->buf
[fp
->length
] = '\0';
5382 /* Close descriptor now, so nesting does not use lots of descriptors. */
5385 /* Must do this before calling trigraph_pcp, so that the correct file name
5386 will be printed in warning messages. */
5389 input_file_stack_tick
++;
5394 if (warn_white_space
)
5395 check_white_space (fp
);
5397 output_line_directive (fp
, op
, 0, enter_file
);
5400 if (missing_newline
)
5403 if (pedantic
&& missing_newline
)
5404 pedwarn ("file does not end in newline");
5407 input_file_stack_tick
++;
5408 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5414 perror_with_name (fname
);
5419 /* Record that inclusion of the include file INC
5420 should be controlled by the macro named MACRO_NAME.
5421 This means that trying to include the file again
5422 will do something if that macro is defined. */
5425 record_control_macro (inc
, macro_name
)
5426 struct include_file
*inc
;
5427 const U_CHAR
*macro_name
;
5429 if (!inc
->control_macro
|| inc
->control_macro
[0])
5430 inc
->control_macro
= macro_name
;
5433 /* Load the specified precompiled header into core, and verify its
5434 preconditions. PCF indicates the file descriptor to read, which must
5435 be a regular file. *ST is its file status.
5436 FNAME indicates the file name of the original header.
5437 *LIMIT will be set to an address one past the end of the file.
5438 If the preconditions of the file are not satisfied, the buffer is
5439 freed and we return 0. If the preconditions are satisfied, return
5440 the address of the buffer following the preconditions. The buffer, in
5441 this case, should never be freed because various pieces of it will
5442 be referred to until all precompiled strings are output at the end of
5446 check_precompiled (pcf
, st
, fname
, limit
)
5449 const char *fname ATTRIBUTE_UNUSED
;
5459 if (S_ISREG (st
->st_mode
))
5461 size_t s
= (size_t) st
->st_size
;
5462 if (s
!= st
->st_size
|| s
+ 2 < s
)
5464 buf
= xmalloc (s
+ 2);
5465 length
= safe_read (pcf
, buf
, s
);
5472 if (length
> 0 && buf
[length
-1] != '\n')
5473 buf
[length
++] = '\n';
5476 *limit
= buf
+ length
;
5478 /* File is in core. Check the preconditions. */
5479 if (!check_preconditions (buf
))
5481 for (cp
= buf
; *cp
; cp
++)
5484 fprintf (stderr
, "Using preinclude %s\n", fname
);
5490 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5496 /* PREC (null terminated) points to the preconditions of a
5497 precompiled header. These are a series of #define and #undef
5498 lines which must match the current contents of the hash
5502 check_preconditions (prec
)
5506 const char *lineend
;
5509 lineend
= index (prec
, '\n');
5511 if (*prec
++ != '#') {
5512 error ("Bad format encountered while reading precompiled file");
5515 if (!strncmp (prec
, "define", 6)) {
5519 mdef
= create_definition ((const U_CHAR
*) prec
,
5520 (const U_CHAR
*) lineend
, NULL_PTR
);
5525 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5526 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5527 || (hp
->type
== T_MACRO
5528 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5529 && (mdef
.defn
->length
!= 2
5530 || mdef
.defn
->expansion
[0] != '\n'
5531 || mdef
.defn
->expansion
[1] != ' ')))
5533 } else if (!strncmp (prec
, "undef", 5)) {
5538 while (is_hor_space
[(U_CHAR
) *prec
])
5541 while (is_idchar
[(U_CHAR
) *prec
])
5545 if (lookup ((const U_CHAR
*) name
, len
, -1))
5548 error ("Bad format encountered while reading precompiled file");
5553 /* They all passed successfully */
5557 /* Process the main body of a precompiled file. BUF points to the
5558 string section of the file, following the preconditions. LIMIT is one
5559 character past the end. NAME is the name of the file being read
5560 in. OP is the main output buffer. */
5563 pcfinclude (buf
, name
, op
)
5572 /* First in the file comes 4 bytes indicating the number of strings, */
5573 /* in network byte order. (MSB first). */
5575 nstrings
= (nstrings
<< 8) | *cp
++;
5576 nstrings
= (nstrings
<< 8) | *cp
++;
5577 nstrings
= (nstrings
<< 8) | *cp
++;
5579 /* Looping over each string... */
5580 while (nstrings
--) {
5581 U_CHAR
*string_start
;
5582 U_CHAR
*endofthiskey
;
5586 /* Each string starts with a STRINGDEF structure (str), followed */
5587 /* by the text of the string (string_start) */
5589 /* First skip to a longword boundary */
5590 /* ??? Why a 4-byte boundary? On all machines? */
5591 /* NOTE: This works correctly even if size_t
5592 is narrower than a pointer.
5593 Do not try risky measures here to get another type to use!
5594 Do not include stddef.h--it will fail! */
5595 if ((size_t) cp
& 3)
5596 cp
+= 4 - ((size_t) cp
& 3);
5598 /* Now get the string. */
5599 str
= (STRINGDEF
*) (PTR
) cp
;
5600 string_start
= cp
+= sizeof (STRINGDEF
);
5602 for (; *cp
; cp
++) /* skip the string */
5605 /* We need to macro expand the string here to ensure that the
5606 proper definition environment is in place. If it were only
5607 expanded when we find out it is needed, macros necessary for
5608 its proper expansion might have had their definitions changed. */
5609 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5610 /* Lineno is already set in the precompiled file */
5611 str
->contents
= tmpbuf
.buf
;
5612 str
->len
= tmpbuf
.bufp
- tmpbuf
.buf
;
5614 str
->filename
= name
;
5615 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5618 *stringlist_tailp
= str
;
5619 stringlist_tailp
= &str
->chain
;
5621 /* Next comes a fourbyte number indicating the number of keys
5624 nkeys
= (nkeys
<< 8) | *cp
++;
5625 nkeys
= (nkeys
<< 8) | *cp
++;
5626 nkeys
= (nkeys
<< 8) | *cp
++;
5628 /* If this number is -1, then the string is mandatory. */
5632 /* Otherwise, for each key, */
5633 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5634 KEYDEF
*kp
= (KEYDEF
*) (PTR
) cp
;
5638 /* It starts with a KEYDEF structure */
5639 cp
+= sizeof (KEYDEF
);
5641 /* Find the end of the key. At the end of this for loop we
5642 advance CP to the start of the next key using this variable. */
5643 endofthiskey
= cp
+ strlen ((char *) cp
);
5646 /* Expand the key, and enter it into the hash table. */
5647 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5650 while (is_hor_space
[*bp
])
5652 if (!is_idstart
[*bp
] || bp
== tmpbuf
.bufp
) {
5657 hp
= lookup (bp
, -1, -1);
5660 install (bp
, -1, T_PCSTRING
, (char *) kp
, -1);
5662 else if (hp
->type
== T_PCSTRING
) {
5663 kp
->chain
= hp
->value
.keydef
;
5664 hp
->value
.keydef
= kp
;
5670 /* This output_line_directive serves to switch us back to the current
5671 input file in case some of these strings get output (which will
5672 result in line directives for the header file being output). */
5673 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5676 /* Called from rescan when it hits a key for strings. Mark them all
5677 used and clean up. */
5685 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5686 kp
->str
->writeflag
= 1;
5690 /* Write the output, interspersing precompiled strings in their
5691 appropriate places. */
5696 STRINGDEF
*next_string
;
5697 U_CHAR
*cur_buf_loc
;
5698 int line_directive_len
= 80;
5699 char *line_directive
= xmalloc (line_directive_len
);
5702 /* In each run through the loop, either cur_buf_loc ==
5703 next_string_loc, in which case we print a series of strings, or
5704 it is less than next_string_loc, in which case we write some of
5706 cur_buf_loc
= outbuf
.buf
;
5707 next_string
= stringlist
;
5709 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5711 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5712 if (next_string
->writeflag
) {
5713 len
= 4 * strlen ((const char *) next_string
->filename
) + 32;
5714 while (len
> line_directive_len
)
5715 line_directive
= xrealloc (line_directive
,
5716 line_directive_len
*= 2);
5717 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5718 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5719 (const char *) next_string
->filename
,
5720 strlen ((const char *) next_string
->filename
)),
5722 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5723 safe_write (fileno (stdout
),
5724 (char *) next_string
->contents
, next_string
->len
);
5726 next_string
= next_string
->chain
;
5730 ? (next_string
->output_mark
5731 - (cur_buf_loc
- outbuf
.buf
))
5732 : outbuf
.bufp
- cur_buf_loc
);
5734 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5738 free (line_directive
);
5741 /* Pass a directive through to the output file.
5742 BUF points to the contents of the directive, as a contiguous string.
5743 LIMIT points to the first character past the end of the directive.
5744 KEYWORD is the keyword-table entry for the directive. */
5747 pass_thru_directive (buf
, limit
, op
, keyword
)
5749 const U_CHAR
*limit
;
5751 struct directive
*keyword
;
5753 register int keyword_length
= keyword
->length
;
5755 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5757 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5758 op
->bufp
+= keyword_length
;
5759 if (limit
!= buf
&& buf
[0] != ' ')
5761 bcopy ((const PTR
) buf
, (PTR
) op
->bufp
, limit
- buf
);
5762 op
->bufp
+= (limit
- buf
);
5765 /* Count the line we have just made in the output,
5766 to get in sync properly. */
5771 /* The arglist structure is built by do_define to tell
5772 collect_definition where the argument names begin. That
5773 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5774 would contain pointers to the strings x, y, and z.
5775 Collect_definition would then build a DEFINITION node,
5776 with reflist nodes pointing to the places x, y, and z had
5777 appeared. So the arglist is just convenience data passed
5778 between these two routines. It is not kept around after
5779 the current #define has been processed and entered into the
5783 struct arglist
*next
;
5790 /* Create a DEFINITION node from a #define directive. Arguments are
5791 as for do_define. */
5794 create_definition (buf
, limit
, op
)
5795 const U_CHAR
*buf
, *limit
;
5798 const U_CHAR
*bp
; /* temp ptr into input buffer */
5799 const U_CHAR
*symname
; /* remember where symbol name starts */
5800 int sym_length
; /* and how long it is */
5801 int line
= instack
[indepth
].lineno
;
5802 const char *file
= instack
[indepth
].nominal_fname
;
5803 size_t file_len
= instack
[indepth
].nominal_fname_len
;
5807 int arglengths
= 0; /* Accumulate lengths of arg names
5808 plus number of args. */
5813 while (is_hor_space
[*bp
])
5816 symname
= bp
; /* remember where it starts */
5817 sym_length
= check_macro_name (bp
, 0);
5820 /* Lossage will occur if identifiers or control keywords are broken
5821 across lines using backslash. This is not the right place to take
5825 struct arglist
*arg_ptrs
= NULL
;
5828 bp
++; /* skip '(' */
5829 SKIP_WHITE_SPACE (bp
);
5831 /* Loop over macro argument names. */
5832 while (*bp
!= ')') {
5833 struct arglist
*temp
;
5835 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5837 temp
->next
= arg_ptrs
;
5838 temp
->argno
= argno
++;
5839 temp
->rest_args
= 0;
5843 pedwarn ("another parameter follows `%s'",
5846 if (!is_idstart
[*bp
])
5848 if (c99
&& limit
- bp
> (long) REST_EXTENSION_LENGTH
5849 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0)
5851 /* This is the ISO C 99 way to write macros with variable
5852 number of arguments. */
5854 temp
->rest_args
= 1;
5857 pedwarn ("invalid character in macro parameter name");
5860 /* Find the end of the arg name. */
5861 while (is_idchar
[*bp
]) {
5863 /* do we have a "special" rest-args extension here? */
5864 if (limit
- bp
> (long) REST_EXTENSION_LENGTH
5865 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5866 if (pedantic
&& !instack
[indepth
].system_header_p
)
5867 pedwarn ("ANSI C does not allow macro with variable arguments");
5869 temp
->rest_args
= 1;
5873 if (bp
== temp
->name
&& rest_args
== 1)
5875 /* This is the ISO C 99 style. */
5876 temp
->name
= (U_CHAR
*) va_args_name
;
5877 temp
->length
= VA_ARGS_NAME_LENGTH
;
5880 temp
->length
= bp
- temp
->name
;
5882 bp
+= REST_EXTENSION_LENGTH
;
5883 arglengths
+= temp
->length
+ 2;
5884 SKIP_WHITE_SPACE (bp
);
5885 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5886 error ("badly punctuated parameter list in `#define'");
5891 SKIP_WHITE_SPACE (bp
);
5892 /* A comma at this point can only be followed by an identifier. */
5893 if (!is_idstart
[*bp
]
5894 && !(c99
&& limit
- bp
> (long) REST_EXTENSION_LENGTH
5895 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0)) {
5896 error ("badly punctuated parameter list in `#define'");
5901 error ("unterminated parameter list in `#define'");
5905 struct arglist
*otemp
;
5907 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5908 if (temp
->length
== otemp
->length
5909 && bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0)
5911 error ("duplicate argument name `%.*s' in `#define'",
5912 temp
->length
, temp
->name
);
5915 if (rest_args
== 0 && temp
->length
== VA_ARGS_NAME_LENGTH
5916 && bcmp (temp
->name
, va_args_name
, VA_ARGS_NAME_LENGTH
) == 0)
5919 reserved name `%s' used as argument name in `#define'", va_args_name
);
5925 ++bp
; /* skip paren */
5926 SKIP_WHITE_SPACE (bp
);
5927 /* now everything from bp before limit is the definition. */
5928 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5929 defn
->rest_args
= rest_args
;
5931 /* Now set defn->args.argnames to the result of concatenating
5932 the argument names in reverse order
5933 with comma-space between them. */
5934 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5936 struct arglist
*temp
;
5938 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5939 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5941 if (temp
->next
!= 0) {
5942 defn
->args
.argnames
[i
++] = ',';
5943 defn
->args
.argnames
[i
++] = ' ';
5946 defn
->args
.argnames
[i
] = 0;
5949 /* Simple expansion or empty definition. */
5953 if (is_hor_space
[*bp
]) {
5955 SKIP_WHITE_SPACE (bp
);
5956 } else if (sym_length
) {
5958 case '!': case '"': case '#': case '%': case '&': case '\'':
5959 case ')': case '*': case '+': case ',': case '-': case '.':
5960 case '/': case ':': case ';': case '<': case '=': case '>':
5961 case '?': case '[': case '\\': case ']': case '^': case '{':
5962 case '|': case '}': case '~':
5963 warning ("missing white space after `#define %.*s'",
5964 sym_length
, symname
);
5968 pedwarn ("missing white space after `#define %.*s'",
5969 sym_length
, symname
);
5974 /* Now everything from bp before limit is the definition. */
5975 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5976 defn
->args
.argnames
= (U_CHAR
*) "";
5981 defn
->file_len
= file_len
;
5983 /* OP is null if this is a predefinition */
5984 defn
->predefined
= !op
;
5986 mdef
.symnam
= symname
;
5987 mdef
.symlen
= sym_length
;
5996 /* Process a #define directive.
5997 BUF points to the contents of the #define directive, as a contiguous string.
5998 LIMIT points to the first character past the end of the definition.
5999 KEYWORD is the keyword-table entry for #define. */
6002 do_define (buf
, limit
, op
, keyword
)
6003 U_CHAR
*buf
, *limit
;
6005 struct directive
*keyword
;
6009 enum node_type newtype
= keyword
->type
== T_DEFINE
? T_MACRO
: T_POISON
;
6011 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
6012 if (pcp_outfile
&& op
)
6013 pass_thru_directive (buf
, limit
, op
, keyword
);
6015 mdef
= create_definition (buf
, limit
, op
);
6019 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
6023 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
6025 /* Redefining a precompiled key is ok. */
6026 if (hp
->type
== T_PCSTRING
)
6028 /* Redefining a poisoned identifier is even worse than `not ok'. */
6029 else if (hp
->type
== T_POISON
)
6031 /* Poisoning anything else is not ok.
6032 The poison should always come first. */
6033 else if (newtype
== T_POISON
)
6035 /* Redefining a macro is ok if the definitions are the same. */
6036 else if (hp
->type
== T_MACRO
)
6037 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
6038 /* Redefining a constant is ok with -D. */
6039 else if (hp
->type
== T_CONST
)
6040 ok
= ! done_initializing
;
6042 /* Print the warning or error if it's not ok. */
6045 /* If we are passing through #define and #undef directives, do
6046 that for this re-definition now. */
6047 if (debug_output
&& op
)
6048 pass_thru_directive (buf
, limit
, op
, keyword
);
6050 if (hp
->type
== T_POISON
)
6051 error ("redefining poisoned `%.*s'", mdef
.symlen
, mdef
.symnam
);
6053 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
6054 if (hp
->type
== T_MACRO
)
6055 pedwarn_with_file_and_line (hp
->value
.defn
->file
,
6056 hp
->value
.defn
->file_len
,
6057 hp
->value
.defn
->line
,
6058 "this is the location of the previous definition");
6060 if (hp
->type
!= T_POISON
)
6062 /* Replace the old definition. */
6064 hp
->value
.defn
= mdef
.defn
;
6067 /* If we are passing through #define and #undef directives, do
6068 that for this new definition now. */
6069 if (debug_output
&& op
)
6070 pass_thru_directive (buf
, limit
, op
, keyword
);
6071 install (mdef
.symnam
, mdef
.symlen
, newtype
,
6072 (char *) mdef
.defn
, hashcode
);
6083 /* Check a purported macro name SYMNAME, and yield its length.
6084 ASSERTION is nonzero if this is really for an assertion name. */
6087 check_macro_name (symname
, assertion
)
6088 const U_CHAR
*symname
;
6094 for (p
= symname
; is_idchar
[*p
]; p
++)
6096 sym_length
= p
- symname
;
6098 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
6101 error ("invalid assertion name");
6103 error ("invalid macro name");
6105 else if (!is_idstart
[*symname
]
6106 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
6109 error ("invalid assertion name `%.*s'", sym_length
, symname
);
6111 error ("invalid macro name `%.*s'", sym_length
, symname
);
6116 /* Return zero if two DEFINITIONs are isomorphic. */
6119 compare_defs (d1
, d2
)
6120 DEFINITION
*d1
, *d2
;
6122 register struct reflist
*a1
, *a2
;
6123 register U_CHAR
*p1
= d1
->expansion
;
6124 register U_CHAR
*p2
= d2
->expansion
;
6127 if (d1
->nargs
!= d2
->nargs
)
6130 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
6132 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
6133 a1
= a1
->next
, a2
= a2
->next
) {
6134 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
6135 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
6136 || a1
->argno
!= a2
->argno
6137 || a1
->stringify
!= a2
->stringify
6138 || a1
->raw_before
!= a2
->raw_before
6139 || a1
->raw_after
!= a2
->raw_after
)
6147 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
6148 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
6153 /* Return 1 if two parts of two macro definitions are effectively different.
6154 One of the parts starts at BEG1 and has LEN1 chars;
6155 the other has LEN2 chars at BEG2.
6156 Any sequence of whitespace matches any other sequence of whitespace.
6157 FIRST means these parts are the first of a macro definition;
6158 so ignore leading whitespace entirely.
6159 LAST means these parts are the last of a macro definition;
6160 so ignore trailing whitespace entirely. */
6163 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
6165 const U_CHAR
*beg1
, *beg2
;
6169 register const U_CHAR
*end1
= beg1
+ len1
;
6170 register const U_CHAR
*end2
= beg2
+ len2
;
6172 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6173 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6176 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
6177 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
6179 while (beg1
!= end1
&& beg2
!= end2
) {
6180 if (is_space
[*beg1
] && is_space
[*beg2
]) {
6181 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6182 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6183 } else if (*beg1
== *beg2
) {
6187 return (beg1
!= end1
) || (beg2
!= end2
);
6190 /* Read a replacement list for a macro with parameters.
6191 Build the DEFINITION structure.
6192 Reads characters of text starting at BUF until END.
6193 ARGLIST specifies the formal parameters to look for
6194 in the text of the definition; NARGS is the number of args
6195 in that list, or -1 for a macro name that wants no argument list.
6196 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6197 and NAMELEN is its length in characters.
6199 Note that comments, backslash-newlines, and leading white space
6200 have already been deleted from the argument. */
6202 /* If there is no trailing whitespace, a Newline Space is added at the end
6203 to prevent concatenation that would be contrary to the standard. */
6206 collect_expansion (buf
, end
, nargs
, arglist
)
6210 struct arglist
*arglist
;
6213 register const U_CHAR
*p
;
6214 register const U_CHAR
*limit
;
6215 register U_CHAR
*lastp
, *exp_p
;
6216 struct reflist
*endpat
= NULL
;
6217 /* Pointer to first nonspace after last ## seen. */
6218 const U_CHAR
*concat
= 0;
6219 /* Pointer to first nonspace after last single-# seen. */
6220 const U_CHAR
*stringify
= 0;
6221 /* How those tokens were spelled. */
6222 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
6223 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
6225 int expected_delimiter
= '\0';
6227 /* Scan thru the replacement list, ignoring comments and quoted
6228 strings, picking up on the macro calls. It does a linear search
6229 thru the arg list on every potential symbol. Profiling might say
6230 that something smarter should happen. */
6235 /* Find the beginning of the trailing whitespace. */
6238 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
6240 /* Allocate space for the text in the macro definition.
6241 Each input char may or may not need 1 byte,
6242 so this is an upper bound.
6243 The extra 3 are for invented trailing newline-marker and final null. */
6244 maxsize
= (sizeof (DEFINITION
)
6246 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
6248 defn
->nargs
= nargs
;
6249 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
6254 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
6255 error ("`##' at start of macro definition");
6256 p
+= p
[0] == '#' ? 2 : 4;
6259 /* Process the main body of the definition. */
6261 int skipped_arg
= 0;
6262 register U_CHAR c
= *p
++;
6270 if (expected_delimiter
!= '\0') {
6271 if (c
== expected_delimiter
)
6272 expected_delimiter
= '\0';
6274 expected_delimiter
= c
;
6278 if (expected_delimiter
) {
6279 /* In a string, backslash goes through
6280 and makes next char ordinary. */
6286 if (!expected_delimiter
&& *p
== ':') {
6287 /* %: is not a digraph if preceded by an odd number of '<'s. */
6288 const U_CHAR
*p0
= p
- 1;
6289 while (buf
< p0
&& p0
[-1] == '<')
6292 /* Treat %:%: as ## and %: as #. */
6293 if (p
[1] == '%' && p
[2] == ':') {
6295 goto sharp_sharp_token
;
6306 /* # is ordinary inside a string. */
6307 if (expected_delimiter
)
6311 /* ##: concatenate preceding and following tokens. */
6312 /* Take out the first #, discard preceding whitespace. */
6314 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
6316 /* Skip the second #. */
6318 concat_sharp_token_type
= c
;
6319 if (is_hor_space
[*p
]) {
6320 concat_sharp_token_type
= c
+ 1;
6322 SKIP_WHITE_SPACE (p
);
6326 error ("`##' at end of macro definition");
6327 } else if (nargs
>= 0) {
6328 /* Single #: stringify following argument ref.
6329 Don't leave the # in the expansion. */
6332 stringify_sharp_token_type
= c
;
6333 if (is_hor_space
[*p
]) {
6334 stringify_sharp_token_type
= c
+ 1;
6336 SKIP_WHITE_SPACE (p
);
6338 if (! is_idstart
[*p
] || nargs
== 0
6339 || (*p
== 'L' && (p
[1] == '\'' || p
[1] == '"')))
6340 error ("`#' operator is not followed by a macro argument name");
6347 /* In -traditional mode, recognize arguments inside strings and
6348 character constants, and ignore special properties of #.
6349 Arguments inside strings are considered "stringified", but no
6350 extra quote marks are supplied. */
6354 if (expected_delimiter
!= '\0') {
6355 if (c
== expected_delimiter
)
6356 expected_delimiter
= '\0';
6358 expected_delimiter
= c
;
6362 /* Backslash quotes delimiters and itself, but not macro args. */
6363 if (expected_delimiter
!= 0 && p
< limit
6364 && (*p
== expected_delimiter
|| *p
== '\\')) {
6371 if (expected_delimiter
!= '\0') /* No comments inside strings. */
6374 /* If we find a comment that wasn't removed by handle_directive,
6375 this must be -traditional. So replace the comment with
6378 while (++p
< limit
) {
6379 if (p
[0] == '*' && p
[1] == '/') {
6385 /* Mark this as a concatenation-point, as if it had been ##. */
6393 #ifdef MULTIBYTE_CHARS
6394 /* Handle multibyte characters inside string and character literals. */
6395 if (expected_delimiter
!= '\0')
6399 length
= local_mblen (p
, limit
- p
);
6403 bcopy (p
, exp_p
, length
);
6412 /* Handle the start of a symbol. */
6413 if (is_idchar
[c
] && nargs
> 0) {
6414 const U_CHAR
*id_beg
= p
- 1;
6418 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6419 id_len
= p
- id_beg
;
6422 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
6423 register struct arglist
*arg
;
6425 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6426 struct reflist
*tpat
;
6428 if (arg
->name
[0] == c
6429 && arg
->length
== id_len
6430 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6431 enum sharp_token_type tpat_stringify
;
6432 if (expected_delimiter
) {
6433 if (warn_stringify
) {
6435 warning ("macro argument `%.*s' is stringified.",
6438 warning ("macro arg `%.*s' would be stringified with -traditional.",
6442 /* If ANSI, don't actually substitute inside a string. */
6445 tpat_stringify
= SHARP_TOKEN
;
6448 = (stringify
== id_beg
6449 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6451 /* make a pat node for this arg and append it to the end of
6453 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6456 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6457 tpat
->raw_after
= NO_SHARP_TOKEN
;
6458 tpat
->rest_args
= arg
->rest_args
;
6459 tpat
->stringify
= tpat_stringify
;
6462 defn
->pattern
= tpat
;
6464 endpat
->next
= tpat
;
6467 tpat
->argno
= arg
->argno
;
6468 tpat
->nchars
= exp_p
- lastp
;
6470 register const U_CHAR
*p1
= p
;
6471 SKIP_WHITE_SPACE (p1
);
6474 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6475 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6477 lastp
= exp_p
; /* place to start copying from next time */
6484 /* If this was not a macro arg, copy it into the expansion. */
6485 if (! skipped_arg
) {
6486 register const U_CHAR
*lim1
= p
;
6490 if (stringify
== id_beg
)
6491 error ("`#' operator should be followed by a macro argument name");
6496 if (!traditional
&& expected_delimiter
== 0) {
6497 /* If ANSI, put in a newline-space marker to prevent token pasting.
6498 But not if "inside a string" (which in ANSI mode happens only for
6506 defn
->length
= exp_p
- defn
->expansion
;
6508 /* Crash now if we overrun the allocated size. */
6509 if (defn
->length
+ 1 > maxsize
)
6513 /* This isn't worth the time it takes. */
6514 /* give back excess storage */
6515 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6522 do_assert (buf
, limit
, op
, keyword
)
6523 U_CHAR
*buf
, *limit
;
6524 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6525 struct directive
*keyword ATTRIBUTE_UNUSED
;
6527 const U_CHAR
*bp
; /* temp ptr into input buffer */
6528 const U_CHAR
*symname
; /* remember where symbol name starts */
6529 int sym_length
; /* and how long it is */
6530 struct arglist
*tokens
= NULL
;
6532 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6533 pedwarn ("ANSI C does not allow `#assert'");
6537 while (is_hor_space
[*bp
])
6540 symname
= bp
; /* remember where it starts */
6541 sym_length
= check_macro_name (bp
, 1);
6543 /* #define doesn't do this, but we should. */
6544 SKIP_WHITE_SPACE (bp
);
6546 /* Lossage will occur if identifiers or control tokens are broken
6547 across lines using backslash. This is not the right place to take
6551 error ("missing token-sequence in `#assert'");
6558 bp
++; /* skip '(' */
6559 SKIP_WHITE_SPACE (bp
);
6561 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6565 error ("empty token-sequence in `#assert'");
6569 ++bp
; /* skip paren */
6570 SKIP_WHITE_SPACE (bp
);
6573 /* If this name isn't already an assertion name, make it one.
6574 Error if it was already in use in some other way. */
6577 ASSERTION_HASHNODE
*hp
;
6578 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6579 struct tokenlist_list
*value
6580 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6582 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6584 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6585 error ("`defined' redefined as assertion");
6586 hp
= assertion_install (symname
, sym_length
, hashcode
);
6589 /* Add the spec'd token-sequence to the list of such. */
6590 value
->tokens
= tokens
;
6591 value
->next
= hp
->value
;
6599 do_unassert (buf
, limit
, op
, keyword
)
6600 U_CHAR
*buf
, *limit
;
6601 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6602 struct directive
*keyword ATTRIBUTE_UNUSED
;
6604 const U_CHAR
*bp
; /* temp ptr into input buffer */
6605 const U_CHAR
*symname
; /* remember where symbol name starts */
6606 int sym_length
; /* and how long it is */
6608 struct arglist
*tokens
= NULL
;
6609 int tokens_specified
= 0;
6611 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6612 pedwarn ("ANSI C does not allow `#unassert'");
6616 while (is_hor_space
[*bp
])
6619 symname
= bp
; /* remember where it starts */
6620 sym_length
= check_macro_name (bp
, 1);
6622 /* #define doesn't do this, but we should. */
6623 SKIP_WHITE_SPACE (bp
);
6625 /* Lossage will occur if identifiers or control tokens are broken
6626 across lines using backslash. This is not the right place to take
6632 bp
++; /* skip '(' */
6633 SKIP_WHITE_SPACE (bp
);
6635 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6639 error ("empty token list in `#unassert'");
6643 tokens_specified
= 1;
6645 ++bp
; /* skip paren */
6646 SKIP_WHITE_SPACE (bp
);
6650 ASSERTION_HASHNODE
*hp
;
6651 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6652 struct tokenlist_list
*tail
, *prev
;
6654 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6658 /* If no token list was specified, then eliminate this assertion
6660 if (! tokens_specified
) {
6661 struct tokenlist_list
*next
;
6662 for (tail
= hp
->value
; tail
; tail
= next
) {
6664 free_token_list (tail
->tokens
);
6667 delete_assertion (hp
);
6669 /* If a list of tokens was given, then delete any matching list. */
6674 struct tokenlist_list
*next
= tail
->next
;
6675 if (compare_token_lists (tail
->tokens
, tokens
)) {
6679 hp
->value
= tail
->next
;
6680 free_token_list (tail
->tokens
);
6693 /* Test whether there is an assertion named NAME
6694 and optionally whether it has an asserted token list TOKENS.
6695 NAME is not null terminated; its length is SYM_LENGTH.
6696 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6699 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6702 int tokens_specified
;
6703 struct arglist
*tokens
;
6705 ASSERTION_HASHNODE
*hp
;
6706 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6708 if (pedantic
&& !instack
[indepth
].system_header_p
)
6709 pedwarn ("ANSI C does not allow testing assertions");
6711 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6713 /* It is not an assertion; just return false. */
6716 /* If no token list was specified, then value is 1. */
6717 if (! tokens_specified
)
6721 struct tokenlist_list
*tail
;
6725 /* If a list of tokens was given,
6726 then succeed if the assertion records a matching list. */
6729 if (compare_token_lists (tail
->tokens
, tokens
))
6734 /* Fail if the assertion has no matching list. */
6739 /* Compare two lists of tokens for equality including order of tokens. */
6742 compare_token_lists (l1
, l2
)
6743 struct arglist
*l1
, *l2
;
6746 if (l1
->length
!= l2
->length
)
6748 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6754 /* Succeed if both lists end at the same time. */
6758 /* Read a space-separated list of tokens ending in a close parenthesis.
6759 Return a list of strings, in the order they were written.
6760 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6761 Parse the text starting at *BPP, and update *BPP.
6762 Don't parse beyond LIMIT. */
6764 static struct arglist
*
6765 read_token_list (bpp
, limit
, error_flag
)
6767 const U_CHAR
*limit
;
6770 struct arglist
*token_ptrs
= 0;
6771 const U_CHAR
*bp
= *bpp
;
6776 /* Loop over the assertion value tokens. */
6778 struct arglist
*temp
;
6781 const U_CHAR
*beg
= bp
;
6783 /* Find the end of the token. */
6787 } else if (*bp
== ')') {
6792 } else if (*bp
== '"' || *bp
== '\'')
6793 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6795 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6796 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6799 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6800 temp2
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6801 bcopy ((const PTR
) beg
, (PTR
) temp2
, bp
- beg
);
6802 temp2
[bp
- beg
] = 0;
6804 temp
->next
= token_ptrs
;
6806 temp
->length
= bp
- beg
;
6808 SKIP_WHITE_SPACE (bp
);
6811 error ("unterminated token sequence in `#assert' or `#unassert'");
6818 /* We accumulated the names in reverse order.
6819 Now reverse them to get the proper order. */
6821 register struct arglist
*prev
= 0, *this, *next
;
6822 for (this = token_ptrs
; this; this = next
) {
6832 free_token_list (tokens
)
6833 struct arglist
*tokens
;
6836 struct arglist
*next
= tokens
->next
;
6837 free ((PTR
) tokens
->name
);
6843 /* Install a name in the assertion hash table.
6845 If LEN is >= 0, it is the length of the name.
6846 Otherwise, compute the length by scanning the entire name.
6848 If HASH is >= 0, it is the precomputed hash code.
6849 Otherwise, compute the hash code. */
6851 static ASSERTION_HASHNODE
*
6852 assertion_install (name
, len
, hash
)
6857 register ASSERTION_HASHNODE
*hp
;
6858 register int i
, bucket
;
6860 register const U_CHAR
*q
;
6862 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6863 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6865 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6866 hp
->next
= assertion_hashtab
[bucket
];
6867 assertion_hashtab
[bucket
] = hp
;
6869 if (hp
->next
!= NULL
)
6870 hp
->next
->prev
= hp
;
6873 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6876 for (i
= 0; i
< len
; i
++)
6882 /* Find the most recent hash node for name "name" (ending with first
6883 non-identifier char) installed by install
6885 If LEN is >= 0, it is the length of the name.
6886 Otherwise, compute the length by scanning the entire name.
6888 If HASH is >= 0, it is the precomputed hash code.
6889 Otherwise, compute the hash code. */
6891 static ASSERTION_HASHNODE
*
6892 assertion_lookup (name
, len
, hash
)
6897 register ASSERTION_HASHNODE
*bucket
;
6899 bucket
= assertion_hashtab
[hash
];
6901 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6903 bucket
= bucket
->next
;
6909 delete_assertion (hp
)
6910 ASSERTION_HASHNODE
*hp
;
6913 if (hp
->prev
!= NULL
)
6914 hp
->prev
->next
= hp
->next
;
6915 if (hp
->next
!= NULL
)
6916 hp
->next
->prev
= hp
->prev
;
6918 /* Make sure that the bucket chain header that the deleted guy was
6919 on points to the right thing afterwards. */
6920 if (hp
== *hp
->bucket_hdr
)
6921 *hp
->bucket_hdr
= hp
->next
;
6927 * interpret #line directive. Remembers previously seen fnames
6928 * in its very own hash table.
6930 #define FNAME_HASHSIZE 37
6933 do_line (buf
, limit
, op
, keyword
)
6934 U_CHAR
*buf
, *limit
;
6936 struct directive
*keyword ATTRIBUTE_UNUSED
;
6938 register U_CHAR
*bp
;
6939 FILE_BUF
*ip
= &instack
[indepth
];
6942 enum file_change_code file_change
= same_file
;
6944 /* Expand any macros. */
6945 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6947 /* Point to macroexpanded line, which is null-terminated now. */
6950 SKIP_WHITE_SPACE (bp
);
6952 if (!ISDIGIT (*bp
)) {
6953 error ("invalid format `#line' directive");
6957 /* The Newline at the end of this line remains to be processed.
6958 To put the next line at the specified line number,
6959 we must store a line number now that is one less. */
6960 new_lineno
= atoi ((char *) bp
) - 1;
6962 /* NEW_LINENO is one less than the actual line number here. */
6963 if (pedantic
&& new_lineno
< 0)
6964 pedwarn ("line number out of range in `#line' directive");
6966 /* skip over the line number. */
6967 while (ISDIGIT (*bp
))
6970 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6971 if (*bp
&& !is_space
[*bp
]) {
6972 error ("invalid format `#line' directive");
6977 SKIP_WHITE_SPACE (bp
);
6980 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6981 HASHNODE
*hp
, **hash_bucket
;
6987 /* Turn the file name, which is a character string literal,
6988 into a null-terminated string. Do this in place. */
6991 switch ((*p
++ = *bp
++)) {
6993 if (! ignore_escape_flag
)
6995 char *bpc
= (char *) bp
;
6996 HOST_WIDEST_INT c
= parse_escape (&bpc
, (HOST_WIDEST_INT
) (U_CHAR
) (-1));
6997 bp
= (U_CHAR
*) bpc
;
7010 fname_length
= p
- fname
;
7012 SKIP_WHITE_SPACE (bp
);
7015 pedwarn ("garbage at end of `#line' directive");
7017 file_change
= enter_file
;
7018 else if (*bp
== '2')
7019 file_change
= leave_file
;
7020 else if (*bp
== '3')
7021 ip
->system_header_p
= 1;
7022 else if (*bp
== '4')
7023 ip
->system_header_p
= 2;
7025 error ("invalid format `#line' directive");
7030 SKIP_WHITE_SPACE (bp
);
7032 ip
->system_header_p
= 1;
7034 SKIP_WHITE_SPACE (bp
);
7037 ip
->system_header_p
= 2;
7039 SKIP_WHITE_SPACE (bp
);
7042 error ("invalid format `#line' directive");
7047 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
7048 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
7049 if (hp
->length
== fname_length
&&
7050 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
7051 ip
->nominal_fname
= hp
->value
.cpval
;
7052 ip
->nominal_fname_len
= fname_length
;
7056 /* Didn't find it; cons up a new one. */
7057 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
7058 hp
->next
= *hash_bucket
;
7061 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
7062 ip
->nominal_fname_len
= hp
->length
= fname_length
;
7063 bcopy (fname
, ((char *) hp
) + sizeof (HASHNODE
), fname_length
+ 1);
7066 error ("invalid format `#line' directive");
7070 ip
->lineno
= new_lineno
;
7071 output_line_directive (ip
, op
, 0, file_change
);
7072 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
7076 /* Remove the definition of a symbol from the symbol table.
7077 according to un*x /lib/cpp, it is not an error to undef
7078 something that has no definitions, so it isn't one here either. */
7081 do_undef (buf
, limit
, op
, keyword
)
7082 U_CHAR
*buf
, *limit
;
7084 struct directive
*keyword
;
7088 U_CHAR
*orig_buf
= buf
;
7090 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7091 if (pcp_outfile
&& op
)
7092 pass_thru_directive (buf
, limit
, op
, keyword
);
7094 SKIP_WHITE_SPACE (buf
);
7095 sym_length
= check_macro_name (buf
, 0);
7097 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
7098 /* If we are generating additional info for debugging (with -g) we
7099 need to pass through all effective #undef directives. */
7100 if (debug_output
&& op
)
7101 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
7102 if (hp
->type
== T_POISON
)
7103 error ("cannot undefine poisoned `%s'", hp
->name
);
7105 if (hp
->type
!= T_MACRO
)
7106 warning ("undefining `%s'", hp
->name
);
7113 SKIP_WHITE_SPACE (buf
);
7115 pedwarn ("garbage after `#undef' directive");
7121 /* Report an error detected by the program we are processing.
7122 Use the text of the line in the error message. */
7125 do_error (buf
, limit
, op
, keyword
)
7126 U_CHAR
*buf
, *limit
;
7127 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7128 struct directive
*keyword
;
7130 int length
= limit
- buf
;
7131 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
7132 bcopy ((const PTR
) buf
, (PTR
) copy
, length
);
7134 SKIP_WHITE_SPACE (copy
);
7136 switch (keyword
->type
) {
7138 error ("#error %s", copy
);
7142 if (pedantic
&& !instack
[indepth
].system_header_p
)
7143 pedwarn ("ANSI C does not allow `#warning'");
7144 warning ("#warning %s", copy
);
7153 /* Remember the name of the current file being read from so that we can
7154 avoid ever including it again. */
7161 for (i
= indepth
; i
>= 0; i
--)
7162 if (instack
[i
].inc
) {
7163 record_control_macro (instack
[i
].inc
, (const U_CHAR
*) "");
7168 /* Report program identification. */
7171 do_ident (buf
, limit
, op
, keyword
)
7172 U_CHAR
*buf
, *limit
;
7174 struct directive
*keyword ATTRIBUTE_UNUSED
;
7179 /* Allow #ident in system headers, since that's not user's fault. */
7180 if (pedantic
&& !instack
[indepth
].system_header_p
)
7181 pedwarn ("ANSI C does not allow `#ident'");
7183 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
7185 len
= trybuf
.bufp
- buf
;
7187 /* Output expanded directive. */
7188 check_expand (op
, 7 + len
);
7189 bcopy ("#ident ", (char *) op
->bufp
, 7);
7191 bcopy ((const PTR
) buf
, (PTR
) op
->bufp
, len
);
7198 /* #pragma and its argument line have already been copied to the output file.
7199 Just check for some recognized pragmas that need validation here. */
7202 do_pragma (buf
, limit
, op
, keyword
)
7203 U_CHAR
*buf
, *limit
;
7205 struct directive
*keyword
;
7207 SKIP_WHITE_SPACE (buf
);
7208 if (!strncmp ((char *) buf
, "once", 4)) {
7209 /* Allow #pragma once in system headers, since that's not the user's
7211 if (!instack
[indepth
].system_header_p
)
7212 warning ("`#pragma once' is obsolete");
7216 if (!strncmp ((char *) buf
, "poison", 6)) {
7217 /* Poison these symbols so that all subsequent usage produces an
7219 U_CHAR
*p
= buf
+ 6;
7221 SKIP_WHITE_SPACE (p
);
7226 while (end
< limit
&& is_idchar
[*end
])
7228 if (end
< limit
&& !is_space
[*end
])
7230 error ("invalid #pragma poison");
7233 do_define(p
, end
, op
, keyword
);
7235 SKIP_WHITE_SPACE (p
);
7239 if (!strncmp ((char *) buf
, "implementation", 14)) {
7240 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7241 been included yet. */
7244 U_CHAR
*p
= buf
+ 14, *fname
;
7245 SKIP_WHITE_SPACE (p
);
7250 p
= skip_quoted_string (p
, limit
, 0, NULL_PTR
, NULL_PTR
, NULL_PTR
);
7254 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
7255 struct include_file
*inc
;
7256 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
7257 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
7258 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
7268 /* This was a fun hack, but #pragma seems to start to be useful.
7269 By failing to recognize it, we pass it through unchanged to cc1. */
7271 /* The behavior of the #pragma directive is implementation defined.
7272 this implementation defines it as follows. */
7278 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
7281 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
7283 execl ("/usr/games/hack", "#pragma", 0);
7284 execl ("/usr/games/rogue", "#pragma", 0);
7285 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7286 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7288 fatal ("You are in a maze of twisty compiler features, all different");
7292 #ifdef SCCS_DIRECTIVE
7294 /* Just ignore #sccs, on systems where we define it at all. */
7297 do_sccs (buf
, limit
, op
, keyword
)
7298 U_CHAR
*buf ATTRIBUTE_UNUSED
;
7299 U_CHAR
*limit ATTRIBUTE_UNUSED
;
7300 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7301 struct directive
*keyword ATTRIBUTE_UNUSED
;
7304 pedwarn ("ANSI C does not allow `#sccs'");
7308 #endif /* defined (SCCS_DIRECTIVE) */
7310 /* Handle #if directive by
7311 1) inserting special `defined' keyword into the hash table
7312 that gets turned into 0 or 1 by special_symbol (thus,
7313 if the luser has a symbol called `defined' already, it won't
7314 work inside the #if directive)
7315 2) rescan the input into a temporary output buffer
7316 3) pass the output buffer to the yacc parser and collect a value
7317 4) clean up the mess left from steps 1 and 2.
7318 5) call conditional_skip to skip til the next #endif (etc.),
7319 or not, depending on the value from step 3. */
7322 do_if (buf
, limit
, op
, keyword
)
7323 U_CHAR
*buf
, *limit
;
7325 struct directive
*keyword ATTRIBUTE_UNUSED
;
7327 HOST_WIDEST_INT value
;
7328 FILE_BUF
*ip
= &instack
[indepth
];
7330 value
= eval_if_expression (buf
, limit
- buf
);
7331 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
7335 /* Handle a #elif directive by not changing if_stack either.
7336 see the comment above do_else. */
7339 do_elif (buf
, limit
, op
, keyword
)
7340 U_CHAR
*buf
, *limit
;
7342 struct directive
*keyword ATTRIBUTE_UNUSED
;
7344 HOST_WIDEST_INT value
;
7345 FILE_BUF
*ip
= &instack
[indepth
];
7347 if (if_stack
== instack
[indepth
].if_stack
) {
7348 error ("`#elif' not within a conditional");
7351 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7352 error ("`#elif' after `#else'");
7353 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7354 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7355 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7356 if_stack
->fname_len
))) {
7357 fprintf (stderr
, ", file ");
7358 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7359 if_stack
->fname_len
, stderr
);
7361 fprintf (stderr
, ")\n");
7363 if_stack
->type
= T_ELIF
;
7366 if (if_stack
->if_succeeded
)
7367 skip_if_group (ip
, 0, op
);
7369 value
= eval_if_expression (buf
, limit
- buf
);
7371 skip_if_group (ip
, 0, op
);
7373 ++if_stack
->if_succeeded
; /* continue processing input */
7374 output_line_directive (ip
, op
, 1, same_file
);
7380 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7381 result as a C expression and return the value as an int. */
7383 static HOST_WIDEST_INT
7384 eval_if_expression (buf
, length
)
7389 HASHNODE
*save_defined
;
7390 HOST_WIDEST_INT value
;
7392 save_defined
= install ((const U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
7395 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
7397 delete_macro (save_defined
); /* clean up special symbol */
7399 *temp_obuf
.bufp
= '\n';
7400 value
= parse_c_expression ((char *) temp_obuf
.buf
,
7401 warn_undef
&& !instack
[indepth
].system_header_p
);
7403 free (temp_obuf
.buf
);
7408 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7409 or don't skip to the #endif/#else/#elif depending on what directive
7410 is actually being processed. */
7413 do_xifdef (buf
, limit
, op
, keyword
)
7414 U_CHAR
*buf
, *limit
;
7416 struct directive
*keyword
;
7419 FILE_BUF
*ip
= &instack
[indepth
];
7421 int start_of_file
= 0;
7422 U_CHAR
*control_macro
= 0;
7424 /* Detect a #ifndef at start of file (not counting comments). */
7425 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7426 U_CHAR
*p
= ip
->buf
;
7427 while (p
!= directive_start
) {
7431 /* Make no special provision for backslash-newline here; this is
7432 slower if backslash-newlines are present, but it's correct,
7433 and it's not worth it to tune for the rare backslash-newline. */
7435 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7436 /* Skip this comment. */
7438 U_CHAR
*save_bufp
= ip
->bufp
;
7440 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7441 ip
->bufp
= save_bufp
;
7446 /* If we get here, this conditional is the beginning of the file. */
7451 /* Discard leading and trailing whitespace. */
7452 SKIP_WHITE_SPACE (buf
);
7453 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7455 /* Find the end of the identifier at the beginning. */
7456 for (end
= buf
; is_idchar
[*end
]; end
++);
7459 skip
= (keyword
->type
== T_IFDEF
);
7463 pedwarn ("`#%s' with no argument", keyword
->name
);
7465 pedwarn ("`#%s' argument starts with punctuation", keyword
->name
);
7470 if (! traditional
) {
7471 if (ISDIGIT (buf
[0]))
7472 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7473 else if (end
!= limit
)
7474 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7477 hp
= lookup (buf
, end
-buf
, -1);
7480 /* Output a precondition for this macro. */
7482 && (hp
->type
== T_CONST
7483 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7484 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7487 fprintf (pcp_outfile
, "#undef ");
7488 while (is_idchar
[*cp
]) /* Ick! */
7489 fputc (*cp
++, pcp_outfile
);
7490 putc ('\n', pcp_outfile
);
7494 if ((hp
!= NULL
) && (hp
->type
== T_POISON
)) {
7495 error("attempt to use poisoned `%s'.", hp
->name
);
7498 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7499 if (start_of_file
&& !skip
) {
7500 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7501 bcopy ((const PTR
) buf
, (PTR
) control_macro
, end
- buf
);
7502 control_macro
[end
- buf
] = 0;
7506 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7510 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7511 If this is a #ifndef starting at the beginning of a file,
7512 CONTROL_MACRO is the macro name tested by the #ifndef.
7513 Otherwise, CONTROL_MACRO is 0. */
7516 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7519 enum node_type type
;
7520 const U_CHAR
*control_macro
;
7523 IF_STACK_FRAME
*temp
;
7525 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7526 temp
->fname
= ip
->nominal_fname
;
7527 temp
->fname_len
= ip
->nominal_fname_len
;
7528 temp
->lineno
= ip
->lineno
;
7529 temp
->next
= if_stack
;
7530 temp
->control_macro
= control_macro
;
7533 if_stack
->type
= type
;
7536 skip_if_group (ip
, 0, op
);
7539 ++if_stack
->if_succeeded
;
7540 output_line_directive (ip
, &outbuf
, 1, same_file
);
7544 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7545 Leaves input ptr at the sharp sign found.
7546 If ANY is nonzero, return at next directive of any sort. */
7549 skip_if_group (ip
, any
, op
)
7554 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7555 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7556 struct directive
*kt
;
7557 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7558 U_CHAR
*beg_of_line
= bp
;
7559 register int ident_length
;
7560 U_CHAR
*ident
, *after_ident
;
7561 /* Save info about where the group starts. */
7562 U_CHAR
*beg_of_group
= bp
;
7563 int beg_lineno
= ip
->lineno
;
7564 int skipping_include_directive
= 0;
7566 if (output_conditionals
&& op
!= 0) {
7567 static const char * const ptr
= "#failed\n";
7568 int len
= strlen (ptr
);
7570 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7575 check_expand (op
, len
);
7576 bcopy (ptr
, (char *) op
->bufp
, len
);
7579 output_line_directive (ip
, op
, 1, 0);
7584 case '/': /* possible comment */
7588 || (cplusplus_comments
&& *bp
== '/')) {
7590 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7594 if (skipping_include_directive
) {
7595 while (bp
< endb
&& *bp
!= '>' && *bp
!= '\n') {
7596 if (*bp
== '\\' && bp
[1] == '\n') {
7605 if (skipping_include_directive
) {
7606 while (bp
< endb
&& *bp
!= '\n') {
7611 if (*bp
== '\\' && bp
[1] == '\n') {
7621 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7622 NULL_PTR
, NULL_PTR
);
7625 /* Char after backslash loses its special meaning in some cases. */
7629 } else if (traditional
&& bp
< endb
)
7635 skipping_include_directive
= 0;
7638 if (beg_of_line
== 0 || traditional
)
7641 while (bp
[0] == '\\' && bp
[1] == '\n')
7647 /* # keyword: a # must be first nonblank char on the line */
7648 if (beg_of_line
== 0)
7652 /* Scan from start of line, skipping whitespace, comments
7653 and backslash-newlines, and see if we reach this #.
7654 If not, this # is not special. */
7656 /* If -traditional, require # to be at beginning of line. */
7659 if (is_hor_space
[*bp
])
7661 else if (*bp
== '\\' && bp
[1] == '\n')
7663 else if (*bp
== '/' && bp
[1] == '*') {
7677 #ifdef MULTIBYTE_CHARS
7679 length
= local_mblen (bp
, endb
- bp
);
7687 /* There is no point in trying to deal with C++ // comments here,
7688 because if there is one, then this # must be part of the
7689 comment and we would never reach here. */
7693 if (bp
!= ip
->bufp
) {
7694 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7698 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7699 if (ip
->bufp
[0] == '%') {
7700 /* Skip past the ':' again. */
7701 while (*bp
== '\\') {
7708 /* Skip whitespace and \-newline. */
7710 if (is_hor_space
[*bp
])
7712 else if (*bp
== '\\' && bp
[1] == '\n')
7714 else if (*bp
== '/') {
7716 newline_fix (bp
+ 1);
7718 for (bp
+= 2; ; bp
++) {
7721 else if (*bp
== '*') {
7722 if (bp
[-1] == '/' && warn_comments
)
7723 warning ("`/*' within comment");
7725 newline_fix (bp
+ 1);
7731 #ifdef MULTIBYTE_CHARS
7733 length
= local_mblen (bp
, endb
- bp
);
7740 } else if (bp
[1] == '/' && cplusplus_comments
) {
7741 for (bp
+= 2; ; bp
++) {
7744 if (*bp
== '\\' && bp
[1] == '\n')
7747 warning ("multiline `//' comment");
7753 #ifdef MULTIBYTE_CHARS
7755 length
= local_mblen (bp
, endb
- bp
);
7769 /* Now find end of directive name.
7770 If we encounter a backslash-newline, exchange it with any following
7771 symbol-constituents so that we end up with a contiguous name. */
7778 name_newline_fix (bp
);
7784 ident_length
= bp
- cp
;
7788 /* A line of just `#' becomes blank. */
7790 if (ident_length
== 0 && *after_ident
== '\n') {
7794 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7796 while (is_idchar
[*p
]) {
7797 if (*p
< '0' || *p
> '9')
7801 /* Handle # followed by a line number. */
7802 if (p
!= ident
&& !is_idchar
[*p
]) {
7804 pedwarn ("`#' followed by integer");
7808 /* Avoid error for `###' and similar cases unless -pedantic. */
7810 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7812 if (pedantic
&& !lang_asm
)
7813 pedwarn ("invalid preprocessing directive");
7818 if (!lang_asm
&& pedantic
)
7819 pedwarn ("invalid preprocessing directive name");
7823 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7824 IF_STACK_FRAME
*temp
;
7825 if (ident_length
== kt
->length
7826 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7827 /* If we are asked to return on next directive, do so now. */
7835 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7836 temp
->next
= if_stack
;
7838 temp
->lineno
= ip
->lineno
;
7839 temp
->fname
= ip
->nominal_fname
;
7840 temp
->fname_len
= ip
->nominal_fname_len
;
7841 temp
->type
= kt
->type
;
7845 if (pedantic
&& if_stack
!= save_if_stack
)
7846 validate_else (bp
, endb
);
7848 if (if_stack
== instack
[indepth
].if_stack
) {
7849 error ("`#%s' not within a conditional", kt
->name
);
7852 else if (if_stack
== save_if_stack
)
7853 goto done
; /* found what we came for */
7855 if (kt
->type
!= T_ENDIF
) {
7856 if (if_stack
->type
== T_ELSE
)
7857 error ("`#else' or `#elif' after `#else'");
7858 if_stack
->type
= kt
->type
;
7863 if_stack
= if_stack
->next
;
7868 case T_INCLUDE_NEXT
:
7870 skipping_include_directive
= 1;
7879 /* Don't let erroneous code go by. */
7880 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7881 pedwarn ("invalid preprocessing directive name");
7886 /* after this returns, rescan will exit because ip->bufp
7887 now points to the end of the buffer.
7888 rescan is responsible for the error message also. */
7891 if (output_conditionals
&& op
!= 0) {
7892 static const char * const ptr
= "#endfailed\n";
7893 int len
= strlen (ptr
);
7895 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7900 check_expand (op
, beg_of_line
- beg_of_group
);
7901 bcopy ((const PTR
) beg_of_group
, (PTR
) op
->bufp
,
7902 beg_of_line
- beg_of_group
);
7903 op
->bufp
+= beg_of_line
- beg_of_group
;
7904 op
->lineno
+= ip
->lineno
- beg_lineno
;
7905 check_expand (op
, len
);
7906 bcopy (ptr
, (char *) op
->bufp
, len
);
7912 /* Handle a #else directive. Do this by just continuing processing
7913 without changing if_stack ; this is so that the error message
7914 for missing #endif's etc. will point to the original #if. It
7915 is possible that something different would be better. */
7918 do_else (buf
, limit
, op
, keyword
)
7919 U_CHAR
*buf
, *limit
;
7921 struct directive
*keyword ATTRIBUTE_UNUSED
;
7923 FILE_BUF
*ip
= &instack
[indepth
];
7926 SKIP_WHITE_SPACE (buf
);
7928 pedwarn ("text following `#else' violates ANSI standard");
7931 if (if_stack
== instack
[indepth
].if_stack
) {
7932 error ("`#else' not within a conditional");
7935 /* #ifndef can't have its special treatment for containing the whole file
7936 if it has a #else clause. */
7937 if_stack
->control_macro
= 0;
7939 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7940 error ("`#else' after `#else'");
7941 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7942 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7943 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7944 if_stack
->fname_len
))) {
7945 fprintf (stderr
, ", file ");
7946 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7947 if_stack
->fname_len
, stderr
);
7949 fprintf (stderr
, ")\n");
7951 if_stack
->type
= T_ELSE
;
7954 if (if_stack
->if_succeeded
)
7955 skip_if_group (ip
, 0, op
);
7957 ++if_stack
->if_succeeded
; /* continue processing input */
7958 output_line_directive (ip
, op
, 1, same_file
);
7963 /* Unstack after #endif directive. */
7966 do_endif (buf
, limit
, op
, keyword
)
7967 U_CHAR
*buf
, *limit
;
7969 struct directive
*keyword ATTRIBUTE_UNUSED
;
7972 SKIP_WHITE_SPACE (buf
);
7974 pedwarn ("text following `#endif' violates ANSI standard");
7977 if (if_stack
== instack
[indepth
].if_stack
)
7978 error ("unbalanced `#endif'");
7980 IF_STACK_FRAME
*temp
= if_stack
;
7981 if_stack
= if_stack
->next
;
7982 if (temp
->control_macro
!= 0) {
7983 /* This #endif matched a #ifndef at the start of the file.
7984 See if it is at the end of the file. */
7985 FILE_BUF
*ip
= &instack
[indepth
];
7986 U_CHAR
*p
= ip
->bufp
;
7987 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7993 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7994 /* Skip this comment. */
7996 U_CHAR
*save_bufp
= ip
->bufp
;
7998 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7999 ip
->bufp
= save_bufp
;
8004 /* If we get here, this #endif ends a #ifndef
8005 that contains all of the file (aside from whitespace).
8006 Arrange not to include the file again
8007 if the macro that was tested is defined.
8009 Do not do this for the top-level file in a -include or any
8010 file in a -imacros. */
8012 && ! (indepth
== 1 && no_record_file
)
8013 && ! (no_record_file
&& no_output
))
8014 record_control_macro (ip
->inc
, temp
->control_macro
);
8018 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
8023 /* When an #else or #endif is found while skipping failed conditional,
8024 if -pedantic was specified, this is called to warn about text after
8025 the directive name. P points to the first char after the directive
8029 validate_else (p
, limit
)
8030 register const U_CHAR
*p
;
8031 register const U_CHAR
*limit
;
8033 /* Advance P over whitespace and comments. */
8035 while (*p
== '\\' && p
[1] == '\n')
8037 if (is_hor_space
[*p
])
8039 else if (*p
== '/') {
8040 while (p
[1] == '\\' && p
[2] == '\n')
8043 /* Don't bother warning about unterminated comments
8044 since that will happen later. Just be sure to exit. */
8045 for (p
+= 2; ; p
++) {
8049 while (p
[1] == '\\' && p
[2] == '\n')
8058 #ifdef MULTIBYTE_CHARS
8060 length
= local_mblen (p
, limit
- p
);
8067 else if (cplusplus_comments
&& p
[1] == '/')
8073 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8076 /* Skip a comment, assuming the input ptr immediately follows the
8077 initial slash-star. Bump *LINE_COUNTER for each newline.
8078 (The canonical line counter is &ip->lineno.)
8079 Don't use this routine (or the next one) if bumping the line
8080 counter is not sufficient to deal with newlines in the string.
8082 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8083 This feature is useful when processing a comment that is going to
8084 be processed or was processed at another point in the preprocessor,
8085 to avoid a duplicate warning. Likewise for unterminated comment
8089 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
8090 register FILE_BUF
*ip
;
8091 int *line_counter
; /* place to remember newlines, or NULL */
8094 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8095 register U_CHAR
*bp
= ip
->bufp
;
8096 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
8097 int start_line
= line_counter
? *line_counter
: 0;
8099 /* JF this line_counter stuff is a crock to make sure the
8100 comment is only put out once, no matter how many times
8101 the comment is skipped. It almost works */
8104 *op
->bufp
++ = bp
[-1];
8106 if (cplusplus_comments
&& bp
[-1] == '/') {
8107 for (; bp
< limit
; bp
++) {
8110 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
8112 if (!nowarn
&& warn_comments
)
8113 warning ("multiline `//' comment");
8125 #ifdef MULTIBYTE_CHARS
8127 length
= local_mblen (bp
, limit
- bp
);
8132 bcopy (bp
, op
->bufp
, length
- 1);
8133 op
->bufp
+= (length
- 1);
8145 while (bp
< limit
) {
8150 /* If this is the end of the file, we have an unterminated comment.
8151 Don't swallow the newline. We are guaranteed that there will be a
8152 trailing newline and various pieces assume it's there. */
8159 if (line_counter
!= NULL
)
8165 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
8166 warning ("`/*' within comment");
8176 #ifdef MULTIBYTE_CHARS
8181 length
= local_mblen (bp
, limit
- bp
);
8187 bcopy (bp
, op
->bufp
, length
);
8197 error_with_line (line_for_error (start_line
), "unterminated comment");
8202 /* Skip over a quoted string. BP points to the opening quote.
8203 Returns a pointer after the closing quote. Don't go past LIMIT.
8204 START_LINE is the line number of the starting point (but it need
8205 not be valid if the starting point is inside a macro expansion).
8207 The input stack state is not changed.
8209 If COUNT_NEWLINES is nonzero, it points to an int to increment
8210 for each newline passed; also, warn about any white space
8211 just before line end.
8213 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8214 if we pass a backslash-newline.
8216 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8219 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
8220 register const U_CHAR
*bp
;
8221 register const U_CHAR
*limit
;
8223 int *count_newlines
;
8224 int *backslash_newlines_p
;
8227 register U_CHAR c
, match
;
8232 error_with_line (line_for_error (start_line
),
8233 "unterminated string or character constant");
8234 error_with_line (multiline_string_line
,
8235 "possible real start of unterminated constant");
8236 multiline_string_line
= 0;
8243 while (*bp
== '\\' && bp
[1] == '\n') {
8244 if (backslash_newlines_p
)
8245 *backslash_newlines_p
= 1;
8251 if (backslash_newlines_p
)
8252 *backslash_newlines_p
= 1;
8257 } else if (c
== '\n') {
8259 /* Unterminated strings and character constants are 'valid'. */
8260 bp
--; /* Don't consume the newline. */
8265 if (match
== '\'') {
8266 error_with_line (line_for_error (start_line
),
8267 "unterminated character constant");
8273 /* If not traditional, then allow newlines inside strings. */
8274 if (count_newlines
) {
8275 if (warn_white_space
&& is_hor_space
[bp
[-2]])
8276 warning ("white space at end of line in string");
8279 if (multiline_string_line
== 0) {
8281 pedwarn_with_line (line_for_error (start_line
),
8282 "string constant runs past end of line");
8283 multiline_string_line
= start_line
;
8285 } else if (c
== match
)
8287 #ifdef MULTIBYTE_CHARS
8291 length
= local_mblen (bp
, limit
- bp
);
8298 return (U_CHAR
*) bp
;
8301 /* Place into DST a quoted string representing the string SRC.
8302 SRCLEN is the length of SRC; SRC may contain null bytes.
8303 Return the address of DST's terminating null. */
8306 quote_string (dst
, src
, srclen
)
8312 const char *srclim
= src
+ srclen
;
8315 while (src
!= srclim
)
8316 switch ((c
= *src
++))
8323 sprintf (dst
, "\\%03o", c
);
8340 /* Skip across a group of balanced parens, starting from IP->bufp.
8341 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8343 This does not handle newlines, because it's used for the arg of #if,
8344 where there aren't any newlines. Also, backslash-newline can't appear. */
8347 skip_paren_group (ip
)
8348 register FILE_BUF
*ip
;
8350 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8351 U_CHAR
*p
= ip
->bufp
;
8353 int lines_dummy
= 0;
8355 while (p
!= limit
) {
8365 return ip
->bufp
= p
;
8371 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
8379 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
8381 return ip
->bufp
= p
;
8391 /* Write out a #line directive, for instance, after an #include file.
8392 If CONDITIONAL is nonzero, we can omit the #line if it would
8393 appear to be a no-op, and we can output a few newlines instead
8394 if we want to increase the line number by a small amount.
8395 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8398 output_line_directive (ip
, op
, conditional
, file_change
)
8401 enum file_change_code file_change
;
8404 char *line_directive_buf
, *line_end
;
8406 if (no_line_directives
8407 || ip
->fname
== NULL
8409 op
->lineno
= ip
->lineno
;
8414 if (ip
->lineno
== op
->lineno
)
8417 /* If the inherited line number is a little too small,
8418 output some newlines instead of a #line directive. */
8419 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
8420 check_expand (op
, 10);
8421 while (ip
->lineno
> op
->lineno
) {
8429 /* Output a positive line number if possible. */
8430 while (ip
->lineno
<= 0 && ip
->bufp
- ip
->buf
< ip
->length
8431 && *ip
->bufp
== '\n') {
8436 line_directive_buf
= (char *) alloca (4 * ip
->nominal_fname_len
+ 100);
8437 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
8438 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
8439 ip
->nominal_fname
, ip
->nominal_fname_len
);
8440 if (file_change
!= same_file
) {
8442 *line_end
++ = file_change
== enter_file
? '1' : '2';
8444 /* Tell cc1 if following text comes from a system header file. */
8445 if (ip
->system_header_p
) {
8449 #ifndef NO_IMPLICIT_EXTERN_C
8450 /* Tell cc1plus if following text should be treated as C. */
8451 if (ip
->system_header_p
== 2 && cplusplus
) {
8457 len
= line_end
- line_directive_buf
;
8458 check_expand (op
, len
+ 1);
8459 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
8461 bcopy ((const PTR
) line_directive_buf
, (PTR
) op
->bufp
, len
);
8463 op
->lineno
= ip
->lineno
;
8466 /* This structure represents one parsed argument in a macro call.
8467 `raw' points to the argument text as written (`raw_length' is its length).
8468 `expanded' points to the argument's macro-expansion
8469 (its length is `expand_length', and its allocated size is `expand_size').
8470 `stringified_length_bound' is an upper bound on the length
8471 the argument would have if stringified.
8472 `use_count' is the number of times this macro arg is substituted
8473 into the macro. If the actual use count exceeds 10,
8474 the value stored is 10.
8475 `free1' and `free2', if nonzero, point to blocks to be freed
8476 when the macro argument data is no longer needed.
8477 `free_ptr', if nonzero, points to a value of instack[i].free_ptr
8478 where the raw field points somewhere into this string. The purpose
8479 of this is to hold onto instack[i].buf for macro arguments, even
8480 when the element has been popped off the input stack.
8484 U_CHAR
*raw
, *expanded
;
8485 int raw_length
, expand_length
, expand_size
;
8486 int stringified_length_bound
;
8487 U_CHAR
*free1
, *free2
;
8493 /* Expand a macro call.
8494 HP points to the symbol that is the macro being called.
8495 Put the result of expansion onto the input stack
8496 so that subsequent input by our caller will use it.
8498 If macro wants arguments, caller has already verified that
8499 an argument list follows; arguments come from the input stack. */
8502 macroexpand (hp
, op
)
8507 DEFINITION
*defn
= hp
->value
.defn
;
8508 register U_CHAR
*xbuf
;
8510 int start_line
= instack
[indepth
].lineno
;
8511 int rest_args
, rest_zero
;
8513 CHECK_DEPTH (return;);
8515 /* it might not actually be a macro. */
8516 if (hp
->type
!= T_MACRO
) {
8517 special_symbol (hp
, op
);
8521 /* This macro is being used inside a #if, which means it must be */
8522 /* recorded as a precondition. */
8523 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
8524 dump_single_macro (hp
, pcp_outfile
);
8526 nargs
= defn
->nargs
;
8530 struct argdata
*args
;
8531 int parse_error
= 0;
8533 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
8535 for (i
= 0; i
< nargs
; i
++) {
8536 args
[i
].raw
= (U_CHAR
*) "";
8537 args
[i
].expanded
= 0;
8538 args
[i
].raw_length
= args
[i
].expand_length
= args
[i
].expand_size
8539 = args
[i
].stringified_length_bound
= 0;
8540 args
[i
].free1
= args
[i
].free2
= 0;
8541 args
[i
].free_ptr
= 0;
8542 args
[i
].use_count
= 0;
8545 /* Parse all the macro args that are supplied. I counts them.
8546 The first NARGS args are stored in ARGS.
8547 The rest are discarded.
8548 If rest_args is set then we assume macarg absorbed the rest of the args.
8553 /* Discard the open-parenthesis or comma before the next arg. */
8554 ++instack
[indepth
].bufp
;
8557 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
8558 /* If we are working on last arg which absorbs rest of args... */
8559 if (i
== nargs
- 1 && defn
->rest_args
)
8561 parse_error
= macarg (&args
[i
], rest_args
);
8564 parse_error
= macarg (NULL_PTR
, 0);
8566 error_with_line (line_for_error (start_line
),
8567 "unterminated macro call");
8571 } while (*instack
[indepth
].bufp
!= ')');
8573 /* If we got one arg but it was just whitespace, call that 0 args. */
8575 register const U_CHAR
*bp
= args
[0].raw
;
8576 register const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
8577 /* cpp.texi says for foo ( ) we provide one argument.
8578 However, if foo wants just 0 arguments, treat this as 0. */
8580 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8585 /* Don't output an error message if we have already output one for
8586 a parse error above. */
8588 if (nargs
== 0 && i
> 0) {
8590 error ("arguments given to macro `%s'", hp
->name
);
8591 } else if (i
< nargs
) {
8592 /* traditional C allows foo() if foo wants one argument. */
8593 if (nargs
== 1 && i
== 0 && traditional
)
8595 /* the rest args token is allowed to absorb 0 tokens */
8596 else if (i
== nargs
- 1 && defn
->rest_args
)
8598 else if (parse_error
)
8601 error ("macro `%s' used without args", hp
->name
);
8603 error ("macro `%s' used with just one arg", hp
->name
);
8605 error ("macro `%s' used with only %d args", hp
->name
, i
);
8606 } else if (i
> nargs
) {
8608 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8611 /* Swallow the closeparen. */
8612 ++instack
[indepth
].bufp
;
8614 /* If macro wants zero args, we parsed the arglist for checking only.
8615 Read directly from the macro definition. */
8617 xbuf
= defn
->expansion
;
8618 xbuf_len
= defn
->length
;
8620 register U_CHAR
*exp
= defn
->expansion
;
8621 register int offset
; /* offset in expansion,
8622 copied a piece at a time */
8623 register int totlen
; /* total amount of exp buffer filled so far */
8625 register struct reflist
*ap
, *last_ap
;
8627 /* Macro really takes args. Compute the expansion of this call. */
8629 /* Compute length in characters of the macro's expansion.
8630 Also count number of times each arg is used. */
8631 xbuf_len
= defn
->length
;
8632 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8633 if (ap
->stringify
&& args
[ap
->argno
].stringified_length_bound
== 0)
8634 /* macarg is not called for omitted arguments, as a result
8635 stringified_length_bound will be zero. We need to make
8636 enough space for "". */
8638 else if (ap
->stringify
)
8639 xbuf_len
+= args
[ap
->argno
].stringified_length_bound
;
8640 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8641 /* Add 4 for two newline-space markers to prevent
8642 token concatenation. */
8643 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8645 /* We have an ordinary (expanded) occurrence of the arg.
8646 So compute its expansion, if we have not already. */
8647 if (args
[ap
->argno
].expanded
== 0) {
8649 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8650 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8653 args
[ap
->argno
].expanded
= obuf
.buf
;
8654 args
[ap
->argno
].expand_length
= obuf
.bufp
- obuf
.buf
;
8655 args
[ap
->argno
].expand_size
= obuf
.length
;
8656 args
[ap
->argno
].free2
= obuf
.buf
;
8658 xbuf_len
+= args
[ap
->argno
].expand_length
;
8660 /* If the arg appears more than once, its later occurrences
8661 may have newline turned into backslash-'n', which is a
8662 factor of 2 expansion. */
8663 xbuf_len
+= 2 * args
[ap
->argno
].expand_length
;
8665 /* Add 4 for two newline-space markers to prevent
8666 token concatenation. */
8669 if (args
[ap
->argno
].use_count
< 10)
8670 args
[ap
->argno
].use_count
++;
8673 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8675 /* Generate in XBUF the complete expansion
8676 with arguments substituted in.
8677 TOTLEN is the total size generated so far.
8678 OFFSET is the index in the definition
8679 of where we are copying from. */
8680 offset
= totlen
= 0;
8681 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8682 last_ap
= ap
, ap
= ap
->next
) {
8683 register struct argdata
*arg
= &args
[ap
->argno
];
8684 int count_before
= totlen
;
8686 /* Add chars to XBUF. */
8687 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8688 xbuf
[totlen
++] = exp
[offset
];
8690 /* If followed by an empty rest arg with concatenation,
8691 delete the last run of nonwhite chars. */
8692 if (rest_zero
&& totlen
> count_before
8693 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8694 || (last_ap
!= NULL
&& last_ap
->rest_args
8695 && last_ap
->raw_after
!= 0))) {
8696 /* Delete final whitespace. */
8697 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8701 /* Delete the nonwhites before them. */
8702 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8707 if (ap
->stringify
!= 0) {
8708 int arglen
= arg
->raw_length
;
8714 && (c
= arg
->raw
[i
], is_space
[c
]))
8717 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8720 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8721 for (; i
< arglen
; i
++) {
8725 /* Generate nothing for backslash-newline in a string. */
8726 if (c
== '\\' && arg
->raw
[i
+ 1] == '\n') {
8732 generate nothing for a stringified argument. */
8738 /* Internal sequences of whitespace are replaced by one space
8739 except within a string or char token. */
8742 while (is_space
[(c
= arg
->raw
[i
])])
8743 /* Newline markers can occur within a whitespace sequence;
8744 consider them part of the sequence. */
8745 i
+= (c
== '\n') + 1;
8756 else if (in_string
) {
8761 #ifdef MULTIBYTE_CHARS
8763 length
= local_mblen (arg
->raw
+ i
, arglen
- i
);
8766 bcopy (arg
->raw
+ i
, xbuf
+ totlen
, length
);
8773 } else if (c
== '\"' || c
== '\'')
8777 /* Escape double-quote, and backslashes in strings.
8778 Newlines in strings are best escaped as \n, since
8779 otherwise backslash-backslash-newline-newline is
8780 mishandled. The C Standard doesn't allow newlines in
8781 strings, so we can escape newlines as we please. */
8785 || (c
== '\n' ? (c
= 'n', 1) : 0))))
8786 xbuf
[totlen
++] = '\\';
8787 /* We used to output e.g. \008 for control characters here,
8788 but this doesn't conform to the C Standard.
8789 Just output the characters as-is. */
8793 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8794 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8795 const U_CHAR
*p1
= arg
->raw
;
8796 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
8797 if (ap
->raw_before
!= 0) {
8798 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8799 while (p1
!= l1
&& is_idchar
[*p1
])
8800 xbuf
[totlen
++] = *p1
++;
8801 /* Delete any no-reexpansion marker that follows
8802 an identifier at the beginning of the argument
8803 if the argument is concatenated with what precedes it. */
8804 if (p1
[0] == '\n' && p1
[1] == '-')
8806 } else if (!traditional
) {
8807 /* Ordinary expanded use of the argument.
8808 Put in newline-space markers to prevent token pasting. */
8809 xbuf
[totlen
++] = '\n';
8810 xbuf
[totlen
++] = ' ';
8812 if (ap
->raw_after
!= 0) {
8813 /* Arg is concatenated after: delete trailing whitespace,
8814 whitespace markers, and no-reexpansion markers. */
8816 if (is_space
[l1
[-1]]) l1
--;
8817 else if (l1
[-1] == '-') {
8818 const U_CHAR
*p2
= l1
- 1;
8819 /* If a `-' is preceded by an odd number of newlines then it
8820 and the last newline are a no-reexpansion marker. */
8821 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8822 if ((l1
- 1 - p2
) & 1) {
8831 bcopy ((const PTR
) p1
, (PTR
) (xbuf
+ totlen
), l1
- p1
);
8833 if (!traditional
&& ap
->raw_after
== 0) {
8834 /* Ordinary expanded use of the argument.
8835 Put in newline-space markers to prevent token pasting. */
8836 xbuf
[totlen
++] = '\n';
8837 xbuf
[totlen
++] = ' ';
8840 /* Ordinary expanded use of the argument.
8841 Put in newline-space markers to prevent token pasting. */
8843 xbuf
[totlen
++] = '\n';
8844 xbuf
[totlen
++] = ' ';
8846 bcopy ((const PTR
) arg
->expanded
, (PTR
) (xbuf
+ totlen
),
8847 arg
->expand_length
);
8848 totlen
+= arg
->expand_length
;
8850 xbuf
[totlen
++] = '\n';
8851 xbuf
[totlen
++] = ' ';
8853 /* If a macro argument with newlines is used multiple times,
8854 then only expand the newlines once. This avoids creating output
8855 lines which don't correspond to any input line, which confuses
8857 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8858 /* Don't bother doing change_newlines for subsequent
8861 change_newlines (arg
);
8865 if (totlen
> xbuf_len
)
8869 /* If there is anything left of the definition after handling
8870 the arg list, copy that in too. */
8872 for (i
= offset
; i
< defn
->length
; i
++) {
8873 /* if we've reached the end of the macro */
8876 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8877 && last_ap
->raw_after
!= 0))
8878 xbuf
[totlen
++] = exp
[i
];
8884 for (i
= 0; i
< nargs
; i
++) {
8885 if (args
[i
].free_ptr
!= 0) {
8886 U_CHAR
*buf
= args
[i
].free_ptr
;
8888 for (d
= indepth
; d
>= 0; --d
) {
8889 if (instack
[d
].buf
== buf
) {
8890 instack
[d
].free_ptr
= buf
; /* Give ownership back to instack */
8894 free (buf
); /* buf is not on the stack; must have been popped */
8897 if (args
[i
].free1
!= 0)
8898 free (args
[i
].free1
);
8899 if (args
[i
].free2
!= 0)
8900 free (args
[i
].free2
);
8904 xbuf
= defn
->expansion
;
8905 xbuf_len
= defn
->length
;
8908 /* Now put the expansion on the input stack
8909 so our caller will commence reading from it. */
8911 register FILE_BUF
*ip2
;
8913 ip2
= &instack
[++indepth
];
8916 ip2
->nominal_fname
= 0;
8917 ip2
->nominal_fname_len
= 0;
8919 /* This may not be exactly correct, but will give much better error
8920 messages for nested macro calls than using a line number of zero. */
8921 ip2
->lineno
= start_line
;
8923 ip2
->length
= xbuf_len
;
8925 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8927 ip2
->if_stack
= if_stack
;
8928 ip2
->system_header_p
= 0;
8930 /* Recursive macro use sometimes works traditionally.
8931 #define foo(x,y) bar (x (y,0), y)
8935 hp
->type
= T_DISABLED
;
8939 /* Parse a macro argument and store the info on it into *ARGPTR.
8940 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8941 Return nonzero to indicate a syntax error. */
8944 macarg (argptr
, rest_args
)
8945 register struct argdata
*argptr
;
8948 FILE_BUF
*ip
= &instack
[indepth
];
8950 int lineno0
= ip
->lineno
;
8954 /* Try to parse as much of the argument as exists at this
8955 input stack level. */
8956 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
,
8957 &paren
, &ip
->lineno
, &comments
, rest_args
);
8959 /* If we find the end of the argument at this level,
8960 set up *ARGPTR to point at it in the input stack. */
8961 if (!(ip
->fname
!= 0 && (ip
->lineno
!= lineno0
|| comments
!= 0))
8962 && bp
!= ip
->buf
+ ip
->length
) {
8964 argptr
->raw
= ip
->bufp
;
8965 argptr
->raw_length
= bp
- ip
->bufp
;
8966 argptr
->newlines
= ip
->lineno
- lineno0
;
8967 /* The next two statements transfer ownership of the the buffer
8968 from ip to argptr. Note that the second statement ensures that
8969 a given free_ptr is owned by at most one macro argument. */
8970 argptr
->free_ptr
= ip
->free_ptr
;
8975 /* This input stack level ends before the macro argument does.
8976 We must pop levels and keep parsing.
8977 Therefore, we must allocate a temporary buffer and copy
8978 the macro argument into it. */
8979 int bufsize
= bp
- ip
->bufp
;
8980 int extra
= ip
->lineno
- lineno0
;
8981 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8982 int final_start
= 0;
8984 bcopy ((const PTR
) ip
->bufp
, (PTR
) buffer
, bufsize
);
8987 while (bp
== ip
->buf
+ ip
->length
) {
8988 if (instack
[indepth
].macro
== 0) {
8992 ip
->macro
->type
= T_MACRO
;
8994 free (ip
->free_ptr
);
8995 ip
= &instack
[--indepth
];
8996 lineno0
= ip
->lineno
;
8998 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
, &paren
,
8999 &ip
->lineno
, &comments
, rest_args
);
9000 final_start
= bufsize
;
9001 bufsize
+= bp
- ip
->bufp
;
9002 extra
+= ip
->lineno
- lineno0
;
9003 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
9004 bcopy ((const PTR
) ip
->bufp
, (PTR
) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
9009 /* Now, if arg is actually wanted, record its raw form,
9010 discarding comments and duplicating newlines in whatever
9011 part of it did not come from a macro expansion.
9012 EXTRA space has been preallocated for duplicating the newlines.
9013 FINAL_START is the index of the start of that part. */
9015 argptr
->raw
= buffer
;
9016 argptr
->raw_length
= bufsize
;
9017 argptr
->free1
= buffer
;
9018 argptr
->newlines
= ip
->lineno
- lineno0
;
9019 if ((argptr
->newlines
|| comments
) && ip
->fname
!= 0)
9022 discard_comments (argptr
->raw
+ final_start
,
9023 argptr
->raw_length
- final_start
,
9025 argptr
->raw
[argptr
->raw_length
] = 0;
9026 if (argptr
->raw_length
> bufsize
+ extra
)
9031 /* If we are not discarding this argument,
9032 macroexpand it and compute its length as stringified.
9033 All this info goes into *ARGPTR. */
9036 register const U_CHAR
*buf
, *lim
;
9037 register int totlen
;
9040 lim
= buf
+ argptr
->raw_length
;
9042 while (buf
!= lim
&& is_space
[*buf
])
9044 while (buf
!= lim
&& is_space
[lim
[-1]])
9046 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
9047 while (buf
!= lim
) {
9048 register U_CHAR c
= *buf
++;
9050 /* Internal sequences of whitespace are replaced by one space
9051 in most cases, but not always. So count all the whitespace
9052 in case we need to keep it all. */
9055 SKIP_ALL_WHITE_SPACE (buf
);
9058 if (c
== '\"' || c
== '\\' || c
== '\n') /* escape these chars */
9061 argptr
->stringified_length_bound
= totlen
;
9066 /* Scan text from START (inclusive) up to LIMIT (exclusive),
9067 taken from the expansion of MACRO,
9068 counting parens in *DEPTHPTR,
9069 and return if reach LIMIT
9070 or before a `)' that would make *DEPTHPTR negative
9071 or before a comma when *DEPTHPTR is zero.
9072 Single and double quotes are matched and termination
9073 is inhibited within them. Comments also inhibit it.
9074 Value returned is pointer to stopping place.
9076 Increment *NEWLINES each time a newline is passed.
9077 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
9078 Set *COMMENTS to 1 if a comment is seen. */
9081 macarg1 (start
, limit
, macro
, depthptr
, newlines
, comments
, rest_args
)
9083 register const U_CHAR
*limit
;
9084 struct hashnode
*macro
;
9085 int *depthptr
, *newlines
, *comments
;
9088 register U_CHAR
*bp
= start
;
9090 while (bp
< limit
) {
9096 if (--(*depthptr
) < 0)
9100 /* Traditionally, backslash makes following char not special. */
9101 if (traditional
&& bp
+ 1 < limit
&& bp
[1] != '\n')
9111 newline_fix (bp
+ 1);
9114 for (bp
+= 2; bp
< limit
; bp
++) {
9117 else if (*bp
== '*') {
9118 if (bp
[-1] == '/' && warn_comments
)
9119 warning ("`/*' within comment");
9121 newline_fix (bp
+ 1);
9129 #ifdef MULTIBYTE_CHARS
9131 length
= local_mblen (bp
, limit
- bp
);
9137 } else if (bp
[1] == '/' && cplusplus_comments
) {
9139 for (bp
+= 2; bp
< limit
; bp
++) {
9144 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
9148 warning ("multiline `//' comment");
9153 #ifdef MULTIBYTE_CHARS
9155 length
= local_mblen (bp
, limit
- bp
);
9167 for (quotec
= *bp
++; bp
< limit
&& *bp
!= quotec
; bp
++) {
9172 while (*bp
== '\\' && bp
[1] == '\n') {
9176 } else if (*bp
== '\n') {
9177 if (warn_white_space
&& is_hor_space
[bp
[-1]] && ! macro
)
9178 warning ("white space at end of line in string");
9185 #ifdef MULTIBYTE_CHARS
9187 length
= local_mblen (bp
, limit
- bp
);
9196 /* if we've returned to lowest level and we aren't absorbing all args */
9197 if ((*depthptr
) == 0 && rest_args
== 0)
9207 /* Discard comments and duplicate newlines
9208 in the string of length LENGTH at START,
9209 except inside of string constants.
9210 The string is copied into itself with its beginning staying fixed.
9212 NEWLINES is the number of newlines that must be duplicated.
9213 We assume that that much extra space is available past the end
9217 discard_comments (start
, length
, newlines
)
9222 register U_CHAR
*ibp
;
9223 register U_CHAR
*obp
;
9224 register U_CHAR
*limit
;
9227 /* If we have newlines to duplicate, copy everything
9228 that many characters up. Then, in the second part,
9229 we will have room to insert the newlines
9231 NEWLINES may actually be too large, because it counts
9232 newlines in string constants, and we don't duplicate those.
9233 But that does no harm. */
9235 ibp
= start
+ length
;
9236 obp
= ibp
+ newlines
;
9238 while (limit
!= ibp
)
9242 ibp
= start
+ newlines
;
9243 limit
= start
+ length
+ newlines
;
9246 while (ibp
< limit
) {
9247 *obp
++ = c
= *ibp
++;
9250 /* Duplicate the newline. */
9264 /* Delete any comment. */
9265 if (cplusplus_comments
&& ibp
[0] == '/') {
9266 /* Comments are equivalent to spaces. */
9273 if (*ibp
== '\\' && ibp
+ 1 < limit
&& ibp
[1] == '\n')
9277 #ifdef MULTIBYTE_CHARS
9278 int length
= local_mblen (ibp
, limit
- ibp
);
9280 ibp
+= (length
- 1);
9287 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
9289 /* Comments are equivalent to spaces.
9290 For -traditional, a comment is equivalent to nothing. */
9295 while (++ibp
< limit
) {
9296 if (ibp
[0] == '*') {
9298 newline_fix (ibp
+ 1);
9299 if (ibp
[1] == '/') {
9306 #ifdef MULTIBYTE_CHARS
9307 int length
= local_mblen (ibp
, limit
- ibp
);
9309 ibp
+= (length
- 1);
9317 /* Notice and skip strings, so that we don't
9318 think that comments start inside them,
9319 and so we don't duplicate newlines in them. */
9322 while (ibp
< limit
) {
9323 *obp
++ = c
= *ibp
++;
9331 else if (c
== '\\') {
9332 if (ibp
< limit
&& *ibp
== '\n') {
9336 while (*ibp
== '\\' && ibp
[1] == '\n')
9344 #ifdef MULTIBYTE_CHARS
9347 length
= local_mblen (ibp
, limit
- ibp
);
9351 bcopy (ibp
, obp
, length
);
9368 /* Turn newlines to spaces in the macro argument ARG.
9369 Remove backslash-newline from string constants,
9370 and turn other newlines in string constants to backslash-'n'. */
9373 change_newlines (arg
)
9374 struct argdata
*arg
;
9376 U_CHAR
*start
= arg
->expanded
;
9377 int length
= arg
->expand_length
;
9378 register U_CHAR
*ibp
;
9379 register U_CHAR
*obp
;
9380 register const U_CHAR
*limit
;
9384 limit
= start
+ length
;
9387 while (ibp
< limit
) {
9388 *obp
++ = c
= *ibp
++;
9391 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9392 string. Skip past the newline and its duplicate.
9393 Put a space in the output. */
9404 /* Notice and skip strings, so that we don't delete newlines in them. */
9407 while (ibp
< limit
) {
9408 *obp
++ = c
= *ibp
++;
9411 else if (c
== '\\' && ibp
< limit
&& *ibp
== '\n')
9420 #ifdef MULTIBYTE_CHARS
9423 length
= local_mblen (ibp
, limit
- ibp
);
9427 bcopy (ibp
, obp
, length
);
9441 arg
->expand_length
= obp
- arg
->expanded
;
9443 if (start
!= arg
->expanded
)
9447 /* notice - output message to stderr */
9450 notice
VPARAMS ((const char * msgid
, ...))
9452 #ifndef ANSI_PROTOTYPES
9457 VA_START (args
, msgid
);
9459 #ifndef ANSI_PROTOTYPES
9460 msgid
= va_arg (args
, const char *);
9463 vnotice (msgid
, args
);
9468 vnotice (msgid
, args
)
9472 vfprintf (stderr
, _(msgid
), args
);
9475 /* error - print error message and increment count of errors. */
9478 error
VPARAMS ((const char * msgid
, ...))
9480 #ifndef ANSI_PROTOTYPES
9485 VA_START (args
, msgid
);
9487 #ifndef ANSI_PROTOTYPES
9488 msgid
= va_arg (args
, const char *);
9491 verror (msgid
, args
);
9496 verror (msgid
, args
)
9501 FILE_BUF
*ip
= NULL
;
9503 print_containing_files ();
9505 for (i
= indepth
; i
>= 0; i
--)
9506 if (instack
[i
].fname
!= NULL
) {
9512 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9513 ip
->nominal_fname_len
, stderr
);
9514 fprintf (stderr
, ":%d: ", ip
->lineno
);
9516 vnotice (msgid
, args
);
9517 fprintf (stderr
, "\n");
9521 /* Error including a message from `errno'. */
9524 error_from_errno (name
)
9529 FILE_BUF
*ip
= NULL
;
9531 print_containing_files ();
9533 for (i
= indepth
; i
>= 0; i
--)
9534 if (instack
[i
].fname
!= NULL
) {
9540 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9541 ip
->nominal_fname_len
, stderr
);
9542 fprintf (stderr
, ":%d: ", ip
->lineno
);
9545 fprintf (stderr
, "%s: %s\n", name
, xstrerror (e
));
9550 /* Print error message but don't count it. */
9553 warning
VPARAMS ((const char * msgid
, ...))
9555 #ifndef ANSI_PROTOTYPES
9560 VA_START (args
, msgid
);
9562 #ifndef ANSI_PROTOTYPES
9563 msgid
= va_arg (args
, const char *);
9566 vwarning (msgid
, args
);
9571 vwarning (msgid
, args
)
9576 FILE_BUF
*ip
= NULL
;
9578 if (inhibit_warnings
)
9581 if (warnings_are_errors
)
9584 print_containing_files ();
9586 for (i
= indepth
; i
>= 0; i
--)
9587 if (instack
[i
].fname
!= NULL
) {
9593 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9594 ip
->nominal_fname_len
, stderr
);
9595 fprintf (stderr
, ":%d: ", ip
->lineno
);
9597 notice ("warning: ");
9598 vnotice (msgid
, args
);
9599 fprintf (stderr
, "\n");
9603 error_with_line
VPARAMS ((int line
, const char * msgid
, ...))
9605 #ifndef ANSI_PROTOTYPES
9611 VA_START (args
, msgid
);
9613 #ifndef ANSI_PROTOTYPES
9614 line
= va_arg (args
, int);
9615 msgid
= va_arg (args
, const char *);
9618 verror_with_line (line
, msgid
, args
);
9624 verror_with_line (line
, msgid
, args
)
9630 FILE_BUF
*ip
= NULL
;
9632 print_containing_files ();
9634 for (i
= indepth
; i
>= 0; i
--)
9635 if (instack
[i
].fname
!= NULL
) {
9641 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9642 ip
->nominal_fname_len
, stderr
);
9643 fprintf (stderr
, ":%d: ", line
);
9645 vnotice (msgid
, args
);
9646 fprintf (stderr
, "\n");
9651 warning_with_line
VPARAMS ((int line
, const char * msgid
, ...))
9653 #ifndef ANSI_PROTOTYPES
9659 VA_START (args
, msgid
);
9661 #ifndef ANSI_PROTOTYPES
9662 line
= va_arg (args
, int);
9663 msgid
= va_arg (args
, const char *);
9666 vwarning_with_line (line
, msgid
, args
);
9671 vwarning_with_line (line
, msgid
, args
)
9677 FILE_BUF
*ip
= NULL
;
9679 if (inhibit_warnings
)
9682 if (warnings_are_errors
)
9685 print_containing_files ();
9687 for (i
= indepth
; i
>= 0; i
--)
9688 if (instack
[i
].fname
!= NULL
) {
9694 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9695 ip
->nominal_fname_len
, stderr
);
9697 fprintf (stderr
, ":%d: ", line
);
9699 fputs (": ", stderr
);
9701 notice ("warning: ");
9702 vnotice (msgid
, args
);
9703 fprintf (stderr
, "\n");
9706 /* Print an error message and maybe count it. */
9709 pedwarn
VPARAMS ((const char * msgid
, ...))
9711 #ifndef ANSI_PROTOTYPES
9716 VA_START (args
, msgid
);
9718 #ifndef ANSI_PROTOTYPES
9719 msgid
= va_arg (args
, const char *);
9722 if (pedantic_errors
)
9723 verror (msgid
, args
);
9725 vwarning (msgid
, args
);
9730 pedwarn_with_line
VPARAMS ((int line
, const char * msgid
, ...))
9732 #ifndef ANSI_PROTOTYPES
9738 VA_START (args
, msgid
);
9740 #ifndef ANSI_PROTOTYPES
9741 line
= va_arg (args
, int);
9742 msgid
= va_arg (args
, const char *);
9745 if (pedantic_errors
)
9746 verror_with_line (line
, msgid
, args
);
9748 vwarning_with_line (line
, msgid
, args
);
9752 /* Report a warning (or an error if pedantic_errors)
9753 giving specified file name and line number, not current. */
9756 pedwarn_with_file_and_line
VPARAMS ((const char *file
, size_t file_len
,
9757 int line
, const char * msgid
, ...))
9759 #ifndef ANSI_PROTOTYPES
9767 if (!pedantic_errors
&& inhibit_warnings
)
9770 VA_START (args
, msgid
);
9772 #ifndef ANSI_PROTOTYPES
9773 file
= va_arg (args
, const char *);
9774 file_len
= va_arg (args
, size_t);
9775 line
= va_arg (args
, int);
9776 msgid
= va_arg (args
, const char *);
9780 fwrite (file
, sizeof file
[0], file_len
, stderr
);
9781 fprintf (stderr
, ":%d: ", line
);
9783 if (pedantic_errors
)
9785 if (!pedantic_errors
)
9786 notice ("warning: ");
9787 vnotice (msgid
, args
);
9789 fprintf (stderr
, "\n");
9793 pedwarn_strange_white_space (ch
)
9798 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9799 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9800 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9805 /* Print the file names and line numbers of the #include
9806 directives which led to the current file. */
9809 print_containing_files ()
9811 FILE_BUF
*ip
= NULL
;
9815 /* If stack of files hasn't changed since we last printed
9816 this info, don't repeat it. */
9817 if (last_error_tick
== input_file_stack_tick
)
9820 for (i
= indepth
; i
>= 0; i
--)
9821 if (instack
[i
].fname
!= NULL
) {
9826 /* Give up if we don't find a source file. */
9830 /* Find the other, outer source files. */
9831 for (i
--; i
>= 0; i
--)
9832 if (instack
[i
].fname
!= NULL
) {
9836 notice ( "In file included from ");
9838 notice (",\n from ");
9841 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9842 ip
->nominal_fname_len
, stderr
);
9843 fprintf (stderr
, ":%d", ip
->lineno
);
9846 fprintf (stderr
, ":\n");
9848 /* Record we have printed the status as of this time. */
9849 last_error_tick
= input_file_stack_tick
;
9852 /* Return the line at which an error occurred.
9853 The error is not necessarily associated with the current spot
9854 in the input stack, so LINE says where. LINE will have been
9855 copied from ip->lineno for the current input level.
9856 If the current level is for a file, we return LINE.
9857 But if the current level is not for a file, LINE is meaningless.
9858 In that case, we return the lineno of the innermost file. */
9861 line_for_error (line
)
9867 for (i
= indepth
; i
>= 0; ) {
9868 if (instack
[i
].fname
!= 0)
9873 line1
= instack
[i
].lineno
;
9881 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9883 * As things stand, nothing is ever placed in the output buffer to be
9884 * removed again except when it's KNOWN to be part of an identifier,
9885 * so flushing and moving down everything left, instead of expanding,
9889 /* You might think void was cleaner for the return type,
9890 but that would get type mismatch in check_expand in strict ANSI. */
9893 grow_outbuf (obuf
, needed
)
9894 register FILE_BUF
*obuf
;
9895 register int needed
;
9900 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9903 /* Make it at least twice as big as it is now. */
9905 /* Make it have at least 150% of the free space we will need. */
9906 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9907 if (minsize
> obuf
->length
)
9908 obuf
->length
= minsize
;
9910 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
9912 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9918 /* Symbol table for macro names and special symbols */
9921 * install a name in the main hash table, even if it is already there.
9922 * name stops with first non alphanumeric, except leading '#'.
9923 * caller must check against redefinition if that is desired.
9924 * delete_macro () removes things installed by install () in fifo order.
9925 * this is important because of the `defined' special symbol used
9926 * in #if, and also if pushdef/popdef directives are ever implemented.
9928 * If LEN is >= 0, it is the length of the name.
9929 * Otherwise, compute the length by scanning the entire name.
9931 * If HASH is >= 0, it is the precomputed hash code.
9932 * Otherwise, compute the hash code.
9936 install (name
, len
, type
, value
, hash
)
9939 enum node_type type
;
9943 register HASHNODE
*hp
;
9944 register int i
, bucket
;
9946 register const U_CHAR
*q
;
9950 while (is_idchar
[*q
])
9956 hash
= hashf (name
, len
, HASHSIZE
);
9958 i
= sizeof (HASHNODE
) + len
+ 1;
9959 hp
= (HASHNODE
*) xmalloc (i
);
9961 hp
->bucket_hdr
= &hashtab
[bucket
];
9962 hp
->next
= hashtab
[bucket
];
9963 hashtab
[bucket
] = hp
;
9965 if (hp
->next
!= NULL
)
9966 hp
->next
->prev
= hp
;
9969 hp
->value
.cpval
= value
;
9970 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9973 for (i
= 0; i
< len
; i
++)
9980 * find the most recent hash node for name "name" (ending with first
9981 * non-identifier char) installed by install
9983 * If LEN is >= 0, it is the length of the name.
9984 * Otherwise, compute the length by scanning the entire name.
9986 * If HASH is >= 0, it is the precomputed hash code.
9987 * Otherwise, compute the hash code.
9991 lookup (name
, len
, hash
)
9996 register const U_CHAR
*bp
;
9997 register HASHNODE
*bucket
;
10000 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
10005 hash
= hashf (name
, len
, HASHSIZE
);
10007 bucket
= hashtab
[hash
];
10009 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
10011 bucket
= bucket
->next
;
10017 * Delete a hash node. Some weirdness to free junk from macros.
10018 * More such weirdness will have to be added if you define more hash
10019 * types that need it.
10022 /* Note that the DEFINITION of a macro is removed from the hash table
10023 but its storage is not freed. This would be a storage leak
10024 except that it is not reasonable to keep undefining and redefining
10025 large numbers of macros many times.
10026 In any case, this is necessary, because a macro can be #undef'd
10027 in the middle of reading the arguments to a call to it.
10028 If #undef freed the DEFINITION, that would crash. */
10035 if (hp
->prev
!= NULL
)
10036 hp
->prev
->next
= hp
->next
;
10037 if (hp
->next
!= NULL
)
10038 hp
->next
->prev
= hp
->prev
;
10040 /* Make sure that the bucket chain header that the deleted guy was
10041 on points to the right thing afterwards. */
10042 if (hp
== *hp
->bucket_hdr
)
10043 *hp
->bucket_hdr
= hp
->next
;
10046 if (hp
->type
== T_MACRO
) {
10047 DEFINITION
*d
= hp
->value
.defn
;
10048 struct reflist
*ap
, *nextap
;
10050 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
10061 * return hash function on name. must be compatible with the one
10062 * computed a step at a time, elsewhere
10066 hashf (name
, len
, hashsize
)
10067 register const U_CHAR
*name
;
10071 register int r
= 0;
10074 r
= HASHSTEP (r
, *name
++);
10076 return MAKE_POS (r
) % hashsize
;
10080 /* Dump the definition of a single macro HP to OF. */
10083 dump_single_macro (hp
, of
)
10084 register HASHNODE
*hp
;
10087 register DEFINITION
*defn
= hp
->value
.defn
;
10088 struct reflist
*ap
;
10093 /* Print the definition of the macro HP. */
10095 fprintf (of
, "#define %s", hp
->name
);
10097 if (defn
->nargs
>= 0) {
10101 for (i
= 0; i
< defn
->nargs
; i
++) {
10102 dump_arg_n (defn
, i
, of
);
10103 if (i
+ 1 < defn
->nargs
)
10104 fprintf (of
, ", ");
10113 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
10114 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
10115 offset
+= ap
->nchars
;
10116 if (!traditional
) {
10117 if (ap
->nchars
!= 0)
10119 if (ap
->stringify
) {
10120 switch (ap
->stringify
) {
10121 case SHARP_TOKEN
: fprintf (of
, "#"); break;
10122 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
10123 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
10124 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
10128 if (ap
->raw_before
!= 0) {
10130 switch (ap
->raw_before
) {
10131 case WHITE_SHARP_TOKEN
:
10132 case WHITE_PERCENT_COLON_TOKEN
:
10139 switch (ap
->raw_before
) {
10140 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10141 case WHITE_SHARP_TOKEN
: fprintf (of
, "## "); break;
10142 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10143 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%: "); break;
10150 dump_arg_n (defn
, ap
->argno
, of
);
10151 if (!traditional
&& ap
->raw_after
!= 0) {
10152 switch (ap
->raw_after
) {
10153 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10154 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
10155 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10156 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
10162 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
10163 fprintf (of
, "\n");
10166 /* Dump all macro definitions as #defines to stdout. */
10173 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
10174 register HASHNODE
*hp
;
10176 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
10177 if (hp
->type
== T_MACRO
)
10178 dump_single_macro (hp
, stdout
);
10183 /* Output to OF a substring of a macro definition.
10184 BASE is the beginning of the definition.
10185 Output characters START thru LENGTH.
10186 Unless traditional, discard newlines outside of strings, thus
10187 converting funny-space markers to ordinary spaces. */
10190 dump_defn_1 (base
, start
, length
, of
)
10191 const U_CHAR
*base
;
10196 const U_CHAR
*p
= base
+ start
;
10197 const U_CHAR
*limit
= base
+ start
+ length
;
10200 fwrite (p
, sizeof (*p
), length
, of
);
10202 while (p
< limit
) {
10203 if (*p
== '\"' || *p
=='\'') {
10204 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
10205 NULL_PTR
, NULL_PTR
);
10206 fwrite (p
, sizeof (*p
), p1
- p
, of
);
10217 /* Print the name of argument number ARGNUM of macro definition DEFN
10219 Recall that DEFN->args.argnames contains all the arg names
10220 concatenated in reverse order with comma-space in between. */
10223 dump_arg_n (defn
, argnum
, of
)
10228 register U_CHAR
*p
= defn
->args
.argnames
;
10229 while (argnum
+ 1 < defn
->nargs
) {
10230 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
10234 while (*p
&& *p
!= ',') {
10240 /* Initialize syntactic classifications of characters. */
10243 initialize_char_syntax ()
10248 * Set up is_idchar and is_idstart tables. These should be
10249 * faster than saying (is_alpha (c) || c == '_'), etc.
10250 * Set up these things before calling any routines tthat
10253 for (i
= 'a'; i
<= 'z'; i
++) {
10254 is_idchar
[TOUPPER(i
)] = 1;
10256 is_idstart
[TOUPPER(i
)] = 1;
10259 for (i
= '0'; i
<= '9'; i
++)
10261 is_idchar
['_'] = 1;
10262 is_idstart
['_'] = 1;
10263 is_idchar
['$'] = 1;
10264 is_idstart
['$'] = 1;
10266 /* horizontal space table */
10267 is_hor_space
[' '] = 1;
10268 is_hor_space
['\t'] = 1;
10269 is_hor_space
['\v'] = 1;
10270 is_hor_space
['\f'] = 1;
10271 is_hor_space
['\r'] = 1;
10274 is_space
['\t'] = 1;
10275 is_space
['\v'] = 1;
10276 is_space
['\f'] = 1;
10277 is_space
['\n'] = 1;
10278 is_space
['\r'] = 1;
10281 /* Initialize the built-in macros. */
10284 initialize_builtins (inp
, outp
)
10288 install ((const U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
10289 install ((const U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
10290 install ((const U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
10291 install ((const U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
10292 install ((const U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
10293 install ((const U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
10294 #ifndef NO_BUILTIN_SIZE_TYPE
10295 install ((const U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
10297 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10298 install ((const U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
10300 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 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
10350 output_line_directive (inp
, outp
, 0, same_file
);
10351 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10354 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
10355 monthnames
[timebuf
->tm_mon
],
10356 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
10357 output_line_directive (inp
, outp
, 0, same_file
);
10358 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10361 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
10362 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
10363 output_line_directive (inp
, outp
, 0, same_file
);
10364 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10369 sprintf (directive
, " __STDC__ 1");
10370 output_line_directive (inp
, outp
, 0, same_file
);
10371 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10376 sprintf (directive
, " __OBJC__ 1");
10377 output_line_directive (inp
, outp
, 0, same_file
);
10378 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10385 * process a given definition string, for initialization
10386 * If STR is just an identifier, define it with value 1.
10387 * If STR has anything after the identifier, then it should
10388 * be identifier=definition.
10392 make_definition (str
)
10396 struct directive
*kt
;
10399 p
= buf
= (U_CHAR
*) str
;
10400 if (!is_idstart
[*p
]) {
10401 error ("malformed option `-D %s'", str
);
10404 while (is_idchar
[*++p
])
10407 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
10410 p
= (U_CHAR
*) str
; /* Error */
10413 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
10414 strcpy ((char *)buf
, str
);
10415 strcat ((char *)buf
, " 1");
10416 } else if (*p
!= '=') {
10417 error ("malformed option `-D %s'", str
);
10421 /* Copy the entire option so we can modify it. */
10422 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
10423 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
10424 /* Change the = to a space. */
10425 buf
[p
- (U_CHAR
*) str
] = ' ';
10426 /* Scan for any backslash-newline and remove it. */
10428 q
= &buf
[p
- (U_CHAR
*) str
];
10430 if (*p
== '\"' || *p
== '\'') {
10431 int unterminated
= 0;
10432 const U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
10433 NULL_PTR
, NULL_PTR
, &unterminated
);
10437 if (*p
== '\\' && p
[1] == '\n')
10439 else if (*p
== '\n')
10448 } else if (*p
== '\\' && p
[1] == '\n')
10450 /* Change newline chars into newline-markers. */
10451 else if (*p
== '\n')
10463 ip
= &instack
[++indepth
];
10464 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10465 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10467 ip
->buf
= ip
->bufp
= buf
;
10468 ip
->length
= strlen ((char *) buf
);
10472 ip
->if_stack
= if_stack
;
10473 ip
->system_header_p
= 0;
10475 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
10478 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10479 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
10483 /* JF, this does the work for the -U option */
10486 make_undef (str
, op
)
10491 struct directive
*kt
;
10493 ip
= &instack
[++indepth
];
10494 ip
->nominal_fname
= ip
->fname
= "*undef*";
10495 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10497 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
10498 ip
->length
= strlen (str
);
10502 ip
->if_stack
= if_stack
;
10503 ip
->system_header_p
= 0;
10505 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
10508 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
10512 /* Process the string STR as if it appeared as the body of a #assert.
10513 OPTION is the option name for which STR was the argument. */
10516 make_assertion (option
, str
)
10517 const char *option
;
10521 struct directive
*kt
;
10522 U_CHAR
*buf
, *p
, *q
;
10524 /* Copy the entire option so we can modify it. */
10525 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
10526 strcpy ((char *) buf
, str
);
10527 /* Scan for any backslash-newline and remove it. */
10530 if (*p
== '\\' && p
[1] == '\n')
10538 if (!is_idstart
[*p
]) {
10539 error ("malformed option `%s %s'", option
, str
);
10542 while (is_idchar
[*++p
])
10544 SKIP_WHITE_SPACE (p
);
10545 if (! (*p
== 0 || *p
== '(')) {
10546 error ("malformed option `%s %s'", option
, str
);
10550 ip
= &instack
[++indepth
];
10551 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10552 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10554 ip
->buf
= ip
->bufp
= buf
;
10555 ip
->length
= strlen ((char *) buf
);
10559 ip
->if_stack
= if_stack
;
10560 ip
->system_header_p
= 0;
10562 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
10565 /* Pass NULL as output ptr to do_define since we KNOW it never does
10567 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
10571 /* The previous include prefix, if any, is PREV_FILE_NAME.
10572 Translate any pathnames with COMPONENT.
10573 Allocate a new include prefix whose name is the
10574 simplified concatenation of PREFIX and NAME,
10575 with a trailing / added if needed.
10576 But return 0 if the include prefix should be ignored,
10577 e.g. because it is a duplicate of PREV_FILE_NAME. */
10579 static struct file_name_list
*
10580 new_include_prefix (prev_file_name
, component
, prefix
, name
)
10581 struct file_name_list
*prev_file_name
;
10582 const char *component
;
10583 const char *prefix
;
10587 fatal ("Directory name missing after command line option");
10590 /* Ignore the empty string. */
10593 prefix
= update_path (prefix
, component
);
10594 name
= update_path (name
, component
);
10597 struct file_name_list
*dir
10598 = ((struct file_name_list
*)
10599 xmalloc (sizeof (struct file_name_list
)
10600 + strlen (prefix
) + strlen (name
) + 2));
10602 strcpy (dir
->fname
, prefix
);
10603 strcat (dir
->fname
, name
);
10604 len
= simplify_filename (dir
->fname
);
10606 /* Convert directory name to a prefix. */
10607 if (len
&& dir
->fname
[len
- 1] != DIR_SEPARATOR
) {
10608 if (len
== 1 && dir
->fname
[len
- 1] == '.')
10612 /* must be '/', hack_vms_include_specification triggers on it. */
10613 dir
->fname
[len
++] = '/';
10615 dir
->fname
[len
++] = DIR_SEPARATOR
;
10617 dir
->fname
[len
] = 0;
10620 /* Ignore a directory whose name matches the previous one. */
10621 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
10622 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10623 if (!first_bracket_include
)
10624 first_bracket_include
= prev_file_name
;
10630 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10632 /* Add a trailing "." if there is a filename. This increases the number
10633 of systems that can stat directories. We remove it below. */
10636 dir
->fname
[len
] = '.';
10637 dir
->fname
[len
+ 1] = 0;
10640 /* Ignore a nonexistent directory. */
10641 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
10642 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
10643 error_from_errno (dir
->fname
);
10649 dir
->fname
[len
] = 0;
10651 /* Ignore a directory whose identity matches the previous one. */
10653 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
10654 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
10655 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10656 if (!first_bracket_include
)
10657 first_bracket_include
= prev_file_name
;
10664 dir
->c_system_include_path
= 0;
10665 dir
->got_name_map
= 0;
10671 /* Append a chain of `struct file_name_list's
10672 to the end of the main include chain.
10673 FIRST is the beginning of the chain to append, and LAST is the end. */
10676 append_include_chain (first
, last
)
10677 struct file_name_list
*first
, *last
;
10679 struct file_name_list
*dir
;
10681 if (!first
|| !last
)
10687 last_include
->next
= first
;
10689 if (first_bracket_include
== 0)
10690 first_bracket_include
= first
;
10692 for (dir
= first
; ; dir
= dir
->next
) {
10693 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
10694 if (len
> max_include_len
)
10695 max_include_len
= len
;
10701 last_include
= last
;
10704 /* Place into DST a representation of the file named SRC that is suitable
10705 for `make'. Do not null-terminate DST. Return its length. */
10707 quote_string_for_make (dst
, src
)
10711 const char *p
= src
;
10722 /* GNU make uses a weird quoting scheme for white space.
10723 A space or tab preceded by 2N+1 backslashes represents
10724 N backslashes followed by space; a space or tab
10725 preceded by 2N backslashes represents N backslashes at
10726 the end of a file name; and backslashes in other
10727 contexts should not be doubled. */
10729 for (q
= p
- 1; src
< q
&& q
[-1] == '\\'; q
--)
10741 goto ordinary_char
;
10747 /* Fall through. This can mishandle things like "$(" but
10748 there's no easy fix. */
10751 /* This can mishandle characters in the string "\0\n%*?[\\~";
10752 exactly which chars are mishandled depends on the `make' version.
10753 We know of no portable solution for this;
10754 even GNU make 3.76.1 doesn't solve the problem entirely.
10755 (Also, '\0' is mishandled due to our calling conventions.) */
10765 /* Add output to `deps_buffer' for the -M switch.
10766 STRING points to the text to be output.
10767 SPACER is ':' for targets, ' ' for dependencies. */
10770 deps_output (string
, spacer
)
10771 const char *string
;
10774 int size
= quote_string_for_make ((char *) 0, string
);
10779 #ifndef MAX_OUTPUT_COLUMNS
10780 #define MAX_OUTPUT_COLUMNS 72
10782 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
10783 && 1 < deps_column
) {
10784 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
10791 if (deps_size
+ 2 * size
+ 8 > deps_allocated_size
) {
10792 deps_allocated_size
= (deps_size
+ 2 * size
+ 50) * 2;
10793 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
10795 if (spacer
== ' ') {
10796 deps_buffer
[deps_size
++] = ' ';
10799 quote_string_for_make (&deps_buffer
[deps_size
], string
);
10801 deps_column
+= size
;
10802 if (spacer
== ':') {
10803 deps_buffer
[deps_size
++] = ':';
10806 deps_buffer
[deps_size
] = 0;
10810 fatal
VPARAMS ((const char * msgid
, ...))
10812 #ifndef ANSI_PROTOTYPES
10813 const char * msgid
;
10817 fprintf (stderr
, "%s: ", progname
);
10818 VA_START (args
, msgid
);
10820 #ifndef ANSI_PROTOTYPES
10821 msgid
= va_arg (args
, const char *);
10823 vnotice (msgid
, args
);
10825 fprintf (stderr
, "\n");
10826 exit (FATAL_EXIT_CODE
);
10829 /* More 'friendly' abort that prints the line and file.
10830 config.h can #define abort fancy_abort if you like that sort of thing. */
10835 fatal ("Internal gcc abort.");
10839 perror_with_name (name
)
10842 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, xstrerror (errno
));
10847 pfatal_with_name (name
)
10850 perror_with_name (name
);
10854 exit (FATAL_EXIT_CODE
);
10858 /* Handler for SIGPIPE. */
10861 pipe_closed (signo
)
10862 /* If this is missing, some compilers complain. */
10863 int signo ATTRIBUTE_UNUSED
;
10865 fatal ("output pipe has been closed");
10871 fatal ("Memory exhausted.");
10876 /* Under VMS we need to fix up the "include" specification filename.
10878 Rules for possible conversions
10880 fullname tried paths
10883 ./dir/name [.dir]name
10885 /name [000000]name, name
10886 dir/name dir:[000000]name, dir:name, dir/name
10887 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10888 path:/name path:[000000]name, path:name
10889 path:/dir/name path:[000000.dir]name, path:[dir]name
10890 path:dir/name path:[dir]name
10891 [path]:[dir]name [path.dir]name
10892 path/[dir]name [path.dir]name
10894 The path:/name input is constructed when expanding <> includes.
10896 return 1 if name was changed, 0 else. */
10899 hack_vms_include_specification (fullname
, vaxc_include
)
10903 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
10904 int f
, check_filename_before_returning
, must_revert
;
10907 check_filename_before_returning
= 0;
10909 /* See if we can find a 1st slash. If not, there's no path information. */
10910 first_slash
= index (fullname
, '/');
10911 if (first_slash
== 0)
10912 return 0; /* Nothing to do!!! */
10914 /* construct device spec if none given. */
10916 if (index (fullname
, ':') == 0)
10919 /* If fullname has a slash, take it as device spec. */
10921 if (first_slash
== fullname
)
10923 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
10925 *first_slash
= ':'; /* make device spec */
10926 for (basename
= fullname
; *basename
!= 0; basename
++)
10927 *basename
= *(basename
+1); /* remove leading slash */
10929 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
10930 && (first_slash
[-1] != ':')
10931 && (first_slash
[-1] != ']')) /* or a vms path */
10933 *first_slash
= ':';
10935 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
10936 && (first_slash
[-1] == '.'))
10940 /* Get part after first ':' (basename[-1] == ':')
10941 or last '/' (basename[-1] == '/'). */
10943 basename
= base_name (fullname
);
10946 * Check if we have a vax-c style '#include filename'
10947 * and add the missing .h
10950 if (vaxc_include
&& !index (basename
,'.'))
10951 strcat (basename
, ".h");
10953 local_ptr
= Local
; /* initialize */
10955 /* We are trying to do a number of things here. First of all, we are
10956 trying to hammer the filenames into a standard format, such that later
10957 processing can handle them.
10959 If the file name contains something like [dir.], then it recognizes this
10960 as a root, and strips the ".]". Later processing will add whatever is
10961 needed to get things working properly.
10963 If no device is specified, then the first directory name is taken to be
10964 a device name (or a rooted logical). */
10966 /* Point to the UNIX filename part (which needs to be fixed!)
10967 but skip vms path information.
10968 [basename != fullname since first_slash != 0]. */
10970 if ((basename
[-1] == ':') /* vms path spec. */
10971 || (basename
[-1] == ']')
10972 || (basename
[-1] == '>'))
10973 unixname
= basename
;
10975 unixname
= fullname
;
10977 if (*unixname
== '/')
10980 /* If the directory spec is not rooted, we can just copy
10981 the UNIX filename part and we are done. */
10983 if (((basename
- fullname
) > 1)
10984 && ( (basename
[-1] == ']')
10985 || (basename
[-1] == '>')))
10987 if (basename
[-2] != '.')
10990 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10991 -> PATH]:/name (basename = '/name', unixname = 'name')
10992 We strip the `]', and then splice the two parts of the name in the
10993 usual way. Given the default locations for include files in cccp.c,
10994 we will only use this code if the user specifies alternate locations
10995 with the /include (-I) switch on the command line. */
10997 basename
-= 1; /* Strip "]" */
10998 unixname
--; /* backspace */
11003 /* The VMS part has a ".]" at the end, and this will not do. Later
11004 processing will add a second directory spec, and this would be a syntax
11005 error. Thus we strip the ".]", and thus merge the directory specs.
11006 We also backspace unixname, so that it points to a '/'. This inhibits the
11007 generation of the 000000 root directory spec (which does not belong here
11010 basename
-= 2; /* Strip ".]" */
11011 unixname
--; /* backspace */
11019 /* We drop in here if there is no VMS style directory specification yet.
11020 If there is no device specification either, we make the first dir a
11021 device and try that. If we do not do this, then we will be essentially
11022 searching the users default directory (as if they did a #include "asdf.h").
11024 Then all we need to do is to push a '[' into the output string. Later
11025 processing will fill this in, and close the bracket. */
11027 if ((unixname
!= fullname
) /* vms path spec found. */
11028 && (basename
[-1] != ':'))
11029 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
11031 *local_ptr
++ = '['; /* Open the directory specification */
11034 if (unixname
== fullname
) /* no vms dir spec. */
11037 if ((first_slash
!= 0) /* unix dir spec. */
11038 && (*unixname
!= '/') /* not beginning with '/' */
11039 && (*unixname
!= '.')) /* or './' or '../' */
11040 *local_ptr
++ = '.'; /* dir is local ! */
11043 /* at this point we assume that we have the device spec, and (at least
11044 the opening "[" for a directory specification. We may have directories
11047 If there are no other slashes then the filename will be
11048 in the "root" directory. Otherwise, we need to add
11049 directory specifications. */
11051 if (index (unixname
, '/') == 0)
11053 /* if no directories specified yet and none are following. */
11054 if (local_ptr
[-1] == '[')
11056 /* Just add "000000]" as the directory string */
11057 strcpy (local_ptr
, "000000]");
11058 local_ptr
+= strlen (local_ptr
);
11059 check_filename_before_returning
= 1; /* we might need to fool with this later */
11065 /* As long as there are still subdirectories to add, do them. */
11066 while (index (unixname
, '/') != 0)
11068 /* If this token is "." we can ignore it
11069 if it's not at the beginning of a path. */
11070 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
11072 /* remove it at beginning of path. */
11073 if ( ((unixname
== fullname
) /* no device spec */
11074 && (fullname
+2 != basename
)) /* starts with ./ */
11076 || ((basename
[-1] == ':') /* device spec */
11077 && (unixname
-1 == basename
))) /* and ./ afterwards */
11078 *local_ptr
++ = '.'; /* make '[.' start of path. */
11083 /* Add a subdirectory spec. Do not duplicate "." */
11084 if ( local_ptr
[-1] != '.'
11085 && local_ptr
[-1] != '['
11086 && local_ptr
[-1] != '<')
11087 *local_ptr
++ = '.';
11089 /* If this is ".." then the spec becomes "-" */
11090 if ( (unixname
[0] == '.')
11091 && (unixname
[1] == '.')
11092 && (unixname
[2] == '/'))
11094 /* Add "-" and skip the ".." */
11095 if ((local_ptr
[-1] == '.')
11096 && (local_ptr
[-2] == '['))
11097 local_ptr
--; /* prevent [.- */
11098 *local_ptr
++ = '-';
11103 /* Copy the subdirectory */
11104 while (*unixname
!= '/')
11105 *local_ptr
++= *unixname
++;
11107 unixname
++; /* Skip the "/" */
11110 /* Close the directory specification */
11111 if (local_ptr
[-1] == '.') /* no trailing periods */
11114 if (local_ptr
[-1] == '[') /* no dir needed */
11117 *local_ptr
++ = ']';
11120 /* Now add the filename. */
11123 *local_ptr
++ = *unixname
++;
11126 /* Now append it to the original VMS spec. */
11128 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
11130 /* If we put a [000000] in the filename, try to open it first. If this fails,
11131 remove the [000000], and return that name. This provides flexibility
11132 to the user in that they can use both rooted and non-rooted logical names
11133 to point to the location of the file. */
11135 if (check_filename_before_returning
)
11137 f
= open (fullname
, O_RDONLY
, 0666);
11140 /* The file name is OK as it is, so return it as is. */
11145 /* The filename did not work. Try to remove the [000000] from the name,
11148 basename
= index (fullname
, '[');
11149 local_ptr
= index (fullname
, ']') + 1;
11150 strcpy (basename
, local_ptr
); /* this gets rid of it */
11160 /* The following wrapper functions supply additional arguments to the VMS
11161 I/O routines to optimize performance with file handling. The arguments
11163 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11164 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11165 "fop=tef"- Truncate unused portions of file when closing file.
11166 "shr=nil"- Disallow file sharing while file is open. */
11169 VMS_freopen (fname
, type
, oldfile
)
11174 #undef freopen /* Get back the real freopen routine. */
11175 if (strcmp (type
, "w") == 0)
11176 return freopen (fname
, type
, oldfile
,
11177 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11178 return freopen (fname
, type
, oldfile
, "mbc=16");
11182 VMS_fopen (fname
, type
)
11186 #undef fopen /* Get back the real fopen routine. */
11187 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11188 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11189 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11190 FILE *(*vmslib_fopen
)() = (FILE *(*)()) fopen
;
11193 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
11194 "deq=64", "fop=tef", "shr=nil");
11196 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
11200 VMS_open (fname
, flags
, prot
)
11205 #undef open /* Get back the real open routine. */
11206 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
11209 /* more VMS hackery */
11213 extern unsigned long SYS$
PARSE(), SYS$
SEARCH();
11215 /* Work around another library bug. If a file is located via a searchlist,
11216 and if the device it's on is not the same device as the one specified
11217 in the first element of that searchlist, then both stat() and fstat()
11218 will fail to return info about it. `errno' will be set to EVMSERR, and
11219 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11220 We can get around this by fully parsing the filename and then passing
11221 that absolute name to stat().
11223 Without this fix, we can end up failing to find header files, which is
11224 bad enough, but then compounding the problem by reporting the reason for
11225 failure as "normal successful completion." */
11227 #undef fstat /* Get back to the library version. */
11230 VMS_fstat (fd
, statbuf
)
11232 struct stat
*statbuf
;
11234 int result
= fstat (fd
, statbuf
);
11239 char nambuf
[NAM$C_MAXRSS
+1];
11241 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
11242 result
= VMS_stat (nambuf
, statbuf
);
11243 /* No fclose(fp) here; that would close(fd) as well. */
11250 VMS_stat (name
, statbuf
)
11252 struct stat
*statbuf
;
11254 int result
= stat (name
, statbuf
);
11260 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for SYS$PARSE */
11261 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for SYS$SEARCH */
11264 fab
.fab$l_fna
= (char *) name
;
11265 fab
.fab$b_fns
= (unsigned char) strlen (name
);
11266 fab
.fab$l_nam
= (void *) &nam
;
11268 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
11269 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
11270 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
11271 if (SYS$
PARSE (&fab
) & 1)
11273 if (SYS$
SEARCH (&fab
) & 1)
11275 res_nam
[nam
.nam$b_rsl
] = '\0';
11276 result
= stat (res_nam
, statbuf
);
11278 /* Clean up searchlist context cached by the system. */
11279 nam
.nam$b_nop
= NAM$M_SYNCHK
;
11280 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
11281 (void) SYS$
PARSE (&fab
);
11289 VMS_fwrite (ptr
, size
, nitems
, stream
)
11295 /* VMS fwrite has undesirable results
11296 if STREAM happens to be a record oriented file.
11297 Work around this problem by writing each character individually. */
11298 char const *p
= ptr
;
11299 size_t bytes
= size
* nitems
;
11300 char *lim
= p
+ bytes
;
11303 if (putc (*p
++, stream
) == EOF
)