1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-98, 1999 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, colon separates directories in a path. */
50 #ifndef PATH_SEPARATOR
51 # define PATH_SEPARATOR ':'
54 /* By default, a slash separates directory names. */
56 # define DIR_SEPARATOR '/'
59 /* By default, the suffix for object files is ".o". */
61 # define HAVE_OBJECT_SUFFIX
63 # define OBJECT_SUFFIX ".o"
66 /* VMS-specific definitions */
71 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
72 #define fopen(fname,mode) VMS_fopen (fname,mode)
73 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
74 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
75 #define fwrite(ptr,size,nitems,stream) VMS_fwrite (ptr,size,nitems,stream)
76 static int VMS_fstat (), VMS_stat ();
77 static int VMS_open ();
78 static FILE *VMS_fopen ();
79 static FILE *VMS_freopen ();
80 static size_t VMS_fwrite ();
81 static void hack_vms_include_specification ();
82 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
83 #define INO_T_HASH(a) 0
84 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
87 /* Windows does not natively support inodes, and neither does MSDOS. */
88 #if (defined (_WIN32) && ! defined (__CYGWIN__) && ! defined (_UWIN)) \
89 || defined (__MSDOS__)
90 #define INO_T_EQ(a, b) 0
94 #define INO_T_EQ(a, b) ((a) == (b))
98 #define INO_T_HASH(a) (a)
101 #ifndef INCLUDE_LEN_FUDGE
102 #define INCLUDE_LEN_FUDGE 0
105 /* External declarations. */
107 extern char *version_string
;
108 HOST_WIDEST_INT parse_escape
PROTO((char **, HOST_WIDEST_INT
));
109 HOST_WIDEST_INT parse_c_expression
PROTO((char *, int));
111 /* Name under which this program was invoked. */
113 static const char *progname
;
115 /* Nonzero means use extra default include directories for C++. */
117 static int cplusplus
;
119 /* Nonzero means handle cplusplus style comments */
121 static int cplusplus_comments
;
123 /* Nonzero means handle #import, for objective C. */
127 /* Nonzero means this is an assembly file, and allow
128 unknown directives, which could be comments. */
132 /* Current maximum length of directory names in the search path
133 for include files. (Altered as we get more of them.) */
135 static int max_include_len
;
137 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
139 static int for_lint
= 0;
141 /* Nonzero means copy comments into the output file. */
143 static int put_out_comments
= 0;
145 /* Nonzero means don't process the ANSI trigraph sequences. */
147 static int no_trigraphs
= 0;
149 /* Nonzero means print the names of included files rather than
150 the preprocessed output. 1 means just the #include "...",
151 2 means #include <...> as well. */
153 static int print_deps
= 0;
155 /* Nonzero if missing .h files in -M output are assumed to be generated
156 files and not errors. */
158 static int print_deps_missing_files
= 0;
160 /* Nonzero means print names of header files (-H). */
162 static int print_include_names
= 0;
164 /* Nonzero means don't output line number information. */
166 static int no_line_directives
;
168 /* Nonzero means output the text in failing conditionals,
169 inside #failed ... #endfailed. */
171 static int output_conditionals
;
173 /* dump_only means inhibit output of the preprocessed text
174 and instead output the definitions of all user-defined
175 macros in a form suitable for use as input to cccp.
176 dump_names means pass #define and the macro name through to output.
177 dump_definitions means pass the whole definition (plus #define) through
180 static enum {dump_none
, dump_only
, dump_names
, dump_definitions
}
181 dump_macros
= dump_none
;
183 /* Nonzero means pass all #define and #undef directives which we actually
184 process through to the output stream. This feature is used primarily
185 to allow cc1 to record the #defines and #undefs for the sake of
186 debuggers which understand about preprocessor macros, but it may
187 also be useful with -E to figure out how symbols are defined, and
188 where they are defined. */
189 static int debug_output
= 0;
191 /* Nonzero means pass #include lines through to the output,
192 even if they are ifdefed out. */
193 static int dump_includes
;
195 /* Nonzero indicates special processing used by the pcp program. The
196 special effects of this mode are:
198 Inhibit all macro expansion, except those inside #if directives.
200 Process #define directives normally, and output their contents
203 Output preconditions to pcp_outfile indicating all the relevant
204 preconditions for use of this file in a later cpp run.
206 static FILE *pcp_outfile
;
208 /* Nonzero means we are inside an IF during a -pcp run. In this mode
209 macro expansion is done, and preconditions are output for all macro
210 uses requiring them. */
211 static int pcp_inside_if
;
213 /* Nonzero means never to include precompiled files.
214 This is 1 since there's no way now to make precompiled files,
215 so it's not worth testing for them. */
216 static int no_precomp
= 1;
218 /* Nonzero means give all the error messages the ANSI standard requires. */
222 /* Nonzero means try to make failure to fit ANSI C an error. */
224 static int pedantic_errors
;
226 /* Nonzero means don't print warning messages. -w. */
228 static int inhibit_warnings
= 0;
230 /* Nonzero means warn if slash-star appears in a slash-star comment,
231 or if newline-backslash appears in a slash-slash comment. */
233 static int warn_comments
;
235 /* Nonzero means warn if a macro argument is (or would be)
236 stringified with -traditional. */
238 static int warn_stringify
;
240 /* Nonzero means warn if there are any trigraphs. */
242 static int warn_trigraphs
;
244 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
246 static int warn_undef
;
248 /* Nonzero means warn if we find white space where it doesn't belong. */
250 static int warn_white_space
;
252 /* Nonzero means warn if #import is used. */
254 static int warn_import
= 1;
256 /* Nonzero means turn warnings into errors. */
258 static int warnings_are_errors
;
260 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
264 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
268 /* Nonzero for the 199x C Standard. */
272 /* Nonzero causes output not to be done,
273 but directives such as #define that have side effects
276 static int no_output
;
278 /* Nonzero means we should look for header.gcc files that remap file names. */
281 /* Nonzero means this file was included with a -imacros or -include
282 command line and should not be recorded as an include file. */
284 static int no_record_file
;
286 /* Nonzero means that we have finished processing the command line options.
287 This flag is used to decide whether or not to issue certain errors
290 static int done_initializing
= 0;
292 /* Line where a newline was first seen in a string constant. */
294 static int multiline_string_line
= 0;
296 /* I/O buffer structure.
297 The `fname' field is nonzero for source files and #include files
298 and for the dummy text used for -D and -U.
299 It is zero for rescanning results of macro expansion
300 and for expanding macro arguments. */
301 #define INPUT_STACK_MAX 400
302 static struct file_buf
{
304 /* Filename specified with #line directive. */
305 const char *nominal_fname
;
306 /* The length of nominal_fname, which may contain embedded NULs. */
307 size_t nominal_fname_len
;
308 /* Include file description. */
309 struct include_file
*inc
;
310 /* Record where in the search path this file was found.
311 For #include_next. */
312 struct file_name_list
*dir
;
317 /* Macro that this level is the expansion of.
318 Included so that we can reenable the macro
319 at the end of this level. */
320 struct hashnode
*macro
;
321 /* Value of if_stack at start of this file.
322 Used to prohibit unmatched #endif (etc) in an include file. */
323 struct if_stack
*if_stack
;
324 /* Object to be freed at end of input at this level. */
326 /* True if this is a system header file; see is_system_include. */
327 char system_header_p
;
328 } instack
[INPUT_STACK_MAX
];
330 static int last_error_tick
; /* Incremented each time we print it. */
331 static int input_file_stack_tick
; /* Incremented when the status changes. */
333 /* Current nesting level of input sources.
334 `instack[indepth]' is the level currently being read. */
335 static int indepth
= -1;
336 #define CHECK_DEPTH(code) \
337 if (indepth >= (INPUT_STACK_MAX - 1)) \
339 error_with_line (line_for_error (instack[indepth].lineno), \
340 "macro or `#include' recursion too deep"); \
344 /* Current depth in #include directives that use <...>. */
345 static int system_include_depth
= 0;
347 typedef struct file_buf FILE_BUF
;
349 /* The output buffer. Its LENGTH field is the amount of room allocated
350 for the buffer, not the number of chars actually present. To get
351 that, subtract outbuf.buf from outbuf.bufp. */
353 #define OUTBUF_SIZE 10 /* initial size of output buffer */
354 static FILE_BUF outbuf
;
356 /* Grow output buffer OBUF points at
357 so it can hold at least NEEDED more chars. */
359 #define check_expand(OBUF, NEEDED) \
360 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
361 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
363 struct file_name_list
365 struct file_name_list
*next
;
366 /* If the following is 1, it is a C-language system include
368 int c_system_include_path
;
369 /* Mapping of file names for this directory. */
370 struct file_name_map
*name_map
;
371 /* Non-zero if name_map is valid. */
373 /* The include directory status. */
375 /* The include prefix: "" denotes the working directory,
376 otherwise fname must end in '/'.
377 The actual size is dynamically allocated. */
381 /* #include "file" looks in source file dir, then stack. */
382 /* #include <file> just looks in the stack. */
383 /* -I directories are added to the end, then the defaults are added. */
385 static struct default_include
{
386 const char *fname
; /* The name of the directory. */
387 const char *component
; /* The component containing the directory */
388 int cplusplus
; /* Only look here if we're compiling C++. */
389 int cxx_aware
; /* Includes in this directory don't need to
390 be wrapped in extern "C" when compiling
392 int included
; /* Set if the directory is acceptable. */
393 } include_defaults_array
[]
394 #ifdef INCLUDE_DEFAULTS
398 /* Pick up GNU C++ specific include files. */
399 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1, 0 },
401 /* This is the dir for fixincludes. Put it just before
402 the files that we fix. */
403 { GCC_INCLUDE_DIR
, "GCC", 0, 0, 0 },
404 /* For cross-compilation, this dir name is generated
405 automatically in Makefile.in. */
406 { CROSS_INCLUDE_DIR
, "GCC", 0, 0, 0 },
407 #ifdef TOOL_INCLUDE_DIR
408 /* This is another place that the target system's headers might be. */
409 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0, 0 },
411 #else /* not CROSS_COMPILE */
412 #ifdef LOCAL_INCLUDE_DIR
413 /* This should be /usr/local/include and should come before
414 the fixincludes-fixed header files. */
415 { LOCAL_INCLUDE_DIR
, 0, 0, 1, 0 },
417 #ifdef TOOL_INCLUDE_DIR
418 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
419 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
420 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 0, 0 },
422 /* This is the dir for fixincludes. Put it just before
423 the files that we fix. */
424 { GCC_INCLUDE_DIR
, "GCC", 0, 0, 0 },
425 /* Some systems have an extra dir of include files. */
426 #ifdef SYSTEM_INCLUDE_DIR
427 { SYSTEM_INCLUDE_DIR
, 0, 0, 0, 0 },
429 #ifndef STANDARD_INCLUDE_COMPONENT
430 #define STANDARD_INCLUDE_COMPONENT 0
432 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0, 0 },
433 #endif /* not CROSS_COMPILE */
436 #endif /* no INCLUDE_DEFAULTS */
438 /* The code looks at the defaults through this pointer, rather than through
439 the constant structure above. This pointer gets changed if an environment
440 variable specifies other defaults. */
441 static struct default_include
*include_defaults
= include_defaults_array
;
443 static struct file_name_list
*include
= 0; /* First dir to search */
444 /* First dir to search for <file> */
445 /* This is the first element to use for #include <...>.
446 If it is 0, use the entire chain for such includes. */
447 static struct file_name_list
*first_bracket_include
= 0;
448 /* This is the first element in the chain that corresponds to
449 a directory of system header files. */
450 static struct file_name_list
*first_system_include
= 0;
451 static struct file_name_list
*last_include
= 0; /* Last in chain */
453 /* Chain of include directories to put at the end of the other chain. */
454 static struct file_name_list
*after_include
= 0;
455 static struct file_name_list
*last_after_include
= 0; /* Last in chain */
457 /* Chain to put at the start of the system include files. */
458 static struct file_name_list
*before_system
= 0;
459 static struct file_name_list
*last_before_system
= 0; /* Last in chain */
461 /* Directory prefix that should replace `/usr' in the standard
462 include file directories. */
463 static char *include_prefix
;
465 /* Maintain and search list of included files. */
467 struct include_file
{
468 struct include_file
*next
; /* for include_hashtab */
469 struct include_file
*next_ino
; /* for include_ino_hashtab */
471 /* If the following is the empty string, it means #pragma once
472 was seen in this include file, or #import was applied to the file.
473 Otherwise, if it is nonzero, it is a macro name.
474 Don't include the file again if that macro is defined. */
475 const U_CHAR
*control_macro
;
476 /* Nonzero if the dependency on this include file has been output. */
481 /* Hash tables of files already included with #include or #import.
482 include_hashtab is by full name; include_ino_hashtab is by inode number. */
484 #define INCLUDE_HASHSIZE 61
485 static struct include_file
*include_hashtab
[INCLUDE_HASHSIZE
];
486 static struct include_file
*include_ino_hashtab
[INCLUDE_HASHSIZE
];
488 /* Global list of strings read in from precompiled files. This list
489 is kept in the order the strings are read in, with new strings being
490 added at the end through stringlist_tailp. We use this list to output
491 the strings at the end of the run.
493 static STRINGDEF
*stringlist
;
494 static STRINGDEF
**stringlist_tailp
= &stringlist
;
497 /* Structure returned by create_definition */
498 typedef struct macrodef MACRODEF
;
501 struct definition
*defn
;
502 const U_CHAR
*symnam
;
506 enum sharp_token_type
{
507 NO_SHARP_TOKEN
= 0, /* token not present */
509 SHARP_TOKEN
= '#', /* token spelled with # only */
510 WHITE_SHARP_TOKEN
, /* token spelled with # and white space */
512 PERCENT_COLON_TOKEN
= '%', /* token spelled with %: only */
513 WHITE_PERCENT_COLON_TOKEN
/* token spelled with %: and white space */
516 /* Structure allocated for every #define. For a simple replacement
519 nargs = -1, the `pattern' list is null, and the expansion is just
520 the replacement text. Nargs = 0 means a functionlike macro with no args,
522 #define getchar() getc (stdin) .
523 When there are args, the expansion is the replacement text with the
524 args squashed out, and the reflist is a list describing how to
525 build the output from the input: e.g., "3 chars, then the 1st arg,
526 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
527 The chars here come from the expansion. Whatever is left of the
528 expansion after the last arg-occurrence is copied after that arg.
529 Note that the reflist can be arbitrarily long---
530 its length depends on the number of times the arguments appear in
531 the replacement text, not how many args there are. Example:
532 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
534 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
535 where (x, y) means (nchars, argno). */
537 typedef struct definition DEFINITION
;
540 int length
; /* length of expansion string */
541 int predefined
; /* True if the macro was builtin or */
542 /* came from the command line */
544 int line
; /* Line number of definition */
545 const char *file
; /* File of definition */
546 size_t file_len
; /* Length of file (which can contain NULs) */
547 char rest_args
; /* Nonzero if last arg. absorbs the rest */
549 struct reflist
*next
;
551 enum sharp_token_type stringify
; /* set if a # operator before arg */
552 enum sharp_token_type raw_before
; /* set if a ## operator before arg */
553 enum sharp_token_type raw_after
; /* set if a ## operator after arg */
555 char rest_args
; /* Nonzero if this arg. absorbs the rest */
556 int nchars
; /* Number of literal chars to copy before
557 this arg occurrence. */
558 int argno
; /* Number of arg to substitute (origin-0) */
561 /* Names of macro args, concatenated in reverse order
562 with comma-space between them.
563 The only use of this is that we warn on redefinition
564 if this differs between the old and new definitions. */
569 /* different kinds of things that can appear in the value field
570 of a hash node. Actually, this may be useless now. */
578 * special extension string that can be added to the last macro argument to
579 * allow it to absorb the "rest" of the arguments when expanded. Ex:
580 * #define wow(a, b...) process (b, a, b)
581 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
582 * { wow (one, two); } -> { process (two, one, two); }
583 * if this "rest_arg" is used with the concat token '##' and if it is not
584 * supplied then the token attached to with ## will not be outputted. Ex:
585 * #define wow (a, b...) process (b ## , a, ## b)
586 * { wow (1, 2); } -> { process (2, 1, 2); }
587 * { wow (one); } -> { process (one); {
589 static char rest_extension
[] = "...";
590 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
592 /* This is the implicit parameter name when using variable number of
593 parameters for macros using the ISO C 9x extension. */
594 static char va_args_name
[] = "__VA_ARGS__";
595 #define VA_ARGS_NAME_LENGTH (sizeof (va_args_name) - 1)
597 /* The structure of a node in the hash table. The hash table
598 has entries for all tokens defined by #define directives (type T_MACRO),
599 plus some special tokens like __LINE__ (these each have their own
600 type, and the appropriate code is run when that type of node is seen.
601 It does not contain control words like "#define", which are recognized
602 by a separate piece of code. */
604 /* different flavors of hash nodes --- also used in keyword table */
606 T_DEFINE
= 1, /* the `#define' keyword */
607 T_INCLUDE
, /* the `#include' keyword */
608 T_INCLUDE_NEXT
, /* the `#include_next' keyword */
609 T_IMPORT
, /* the `#import' keyword */
610 T_IFDEF
, /* the `#ifdef' keyword */
611 T_IFNDEF
, /* the `#ifndef' keyword */
612 T_IF
, /* the `#if' keyword */
613 T_ELSE
, /* `#else' */
614 T_PRAGMA
, /* `#pragma' */
615 T_ELIF
, /* `#elif' */
616 T_UNDEF
, /* `#undef' */
617 T_LINE
, /* `#line' */
618 T_ERROR
, /* `#error' */
619 T_WARNING
, /* `#warning' */
620 T_ENDIF
, /* `#endif' */
621 T_SCCS
, /* `#sccs', used on system V. */
622 T_IDENT
, /* `#ident', used on system V. */
623 T_ASSERT
, /* `#assert', taken from system V. */
624 T_UNASSERT
, /* `#unassert', taken from system V. */
625 T_SPECLINE
, /* special symbol `__LINE__' */
626 T_DATE
, /* `__DATE__' */
627 T_FILE
, /* `__FILE__' */
628 T_BASE_FILE
, /* `__BASE_FILE__' */
629 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
630 T_VERSION
, /* `__VERSION__' */
631 T_SIZE_TYPE
, /* `__SIZE_TYPE__' */
632 T_PTRDIFF_TYPE
, /* `__PTRDIFF_TYPE__' */
633 T_WCHAR_TYPE
, /* `__WCHAR_TYPE__' */
634 T_USER_LABEL_PREFIX_TYPE
, /* `__USER_LABEL_PREFIX__' */
635 T_REGISTER_PREFIX_TYPE
, /* `__REGISTER_PREFIX__' */
636 T_IMMEDIATE_PREFIX_TYPE
, /* `__IMMEDIATE_PREFIX__' */
637 T_TIME
, /* `__TIME__' */
638 T_CONST
, /* Constant value, used by `__STDC__' */
639 T_MACRO
, /* macro defined by `#define' */
640 T_DISABLED
, /* macro temporarily turned off for rescan */
641 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
642 T_PCSTRING
, /* precompiled string (hashval is KEYDEF *) */
643 T_POISON
, /* defined with `#pragma poison' */
644 T_UNUSED
/* Used for something not defined. */
648 struct hashnode
*next
; /* double links for easy deletion */
649 struct hashnode
*prev
;
650 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
651 chain is kept, in case the node is the head
652 of the chain and gets deleted. */
653 enum node_type type
; /* type of special token */
654 int length
; /* length of token, for quick comparison */
655 U_CHAR
*name
; /* the actual name */
656 union hashval value
; /* pointer to expansion, or whatever */
659 typedef struct hashnode HASHNODE
;
661 /* Some definitions for the hash table. The hash function MUST be
662 computed as shown in hashf () below. That is because the rescan
663 loop computes the hash value `on the fly' for most tokens,
664 in order to avoid the overhead of a lot of procedure calls to
665 the hashf () function. Hashf () only exists for the sake of
666 politeness, for use when speed isn't so important. */
668 #define HASHSIZE 1403
669 static HASHNODE
*hashtab
[HASHSIZE
];
670 #define HASHSTEP(old, c) ((old << 2) + c)
671 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
674 /* We let tm.h override the types used here, to handle trivial differences
675 such as the choice of unsigned int or long unsigned int for size_t.
676 When machines start needing nontrivial differences in the size type,
677 it would be best to do something here to figure out automatically
678 from other information what type to use. */
680 /* The string value for __SIZE_TYPE__. */
683 #define SIZE_TYPE "long unsigned int"
686 /* The string value for __PTRDIFF_TYPE__. */
689 #define PTRDIFF_TYPE "long int"
692 /* The string value for __WCHAR_TYPE__. */
695 #define WCHAR_TYPE "int"
697 static const char * wchar_type
= WCHAR_TYPE
;
700 /* The string value for __USER_LABEL_PREFIX__ */
702 #ifndef USER_LABEL_PREFIX
703 #define USER_LABEL_PREFIX ""
705 static const char * user_label_prefix
= USER_LABEL_PREFIX
;
706 #undef USER_LABEL_PREFIX
708 /* The string value for __REGISTER_PREFIX__ */
710 #ifndef REGISTER_PREFIX
711 #define REGISTER_PREFIX ""
714 /* The string value for __IMMEDIATE_PREFIX__ */
716 #ifndef IMMEDIATE_PREFIX
717 #define IMMEDIATE_PREFIX ""
720 /* In the definition of a #assert name, this structure forms
721 a list of the individual values asserted.
722 Each value is itself a list of "tokens".
723 These are strings that are compared by name. */
725 struct tokenlist_list
{
726 struct tokenlist_list
*next
;
727 struct arglist
*tokens
;
730 struct assertion_hashnode
{
731 struct assertion_hashnode
*next
; /* double links for easy deletion */
732 struct assertion_hashnode
*prev
;
733 /* also, a back pointer to this node's hash
734 chain is kept, in case the node is the head
735 of the chain and gets deleted. */
736 struct assertion_hashnode
**bucket_hdr
;
737 int length
; /* length of token, for quick comparison */
738 U_CHAR
*name
; /* the actual name */
739 /* List of token-sequences. */
740 struct tokenlist_list
*value
;
743 typedef struct assertion_hashnode ASSERTION_HASHNODE
;
745 /* Some definitions for the hash table. The hash function MUST be
746 computed as shown in hashf below. That is because the rescan
747 loop computes the hash value `on the fly' for most tokens,
748 in order to avoid the overhead of a lot of procedure calls to
749 the hashf function. hashf only exists for the sake of
750 politeness, for use when speed isn't so important. */
752 #define ASSERTION_HASHSIZE 37
753 static ASSERTION_HASHNODE
*assertion_hashtab
[ASSERTION_HASHSIZE
];
755 /* Nonzero means inhibit macroexpansion of what seem to be
756 assertion tests, in rescan. For #if. */
757 static int assertions_flag
;
759 /* `struct directive' defines one #-directive, including how to handle it. */
761 #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
764 int length
; /* Length of name */
765 int (*func
) DO_PROTO
; /* Function to handle directive */
766 const char *name
; /* Name of directive */
767 enum node_type type
; /* Code which describes which directive. */
770 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
771 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
773 /* These functions are declared to return int instead of void since they
774 are going to be placed in the table and some old compilers have trouble with
775 pointers to functions returning void. */
777 static int do_assert DO_PROTO
;
778 static int do_define DO_PROTO
;
779 static int do_elif DO_PROTO
;
780 static int do_else DO_PROTO
;
781 static int do_endif DO_PROTO
;
782 static int do_error DO_PROTO
;
783 static int do_ident DO_PROTO
;
784 static int do_if DO_PROTO
;
785 static int do_include DO_PROTO
;
786 static int do_line DO_PROTO
;
787 static int do_pragma DO_PROTO
;
788 #ifdef SCCS_DIRECTIVE
789 static int do_sccs DO_PROTO
;
791 static int do_unassert DO_PROTO
;
792 static int do_undef DO_PROTO
;
793 static int do_xifdef DO_PROTO
;
795 /* Here is the actual list of #-directives, most-often-used first. */
797 static struct directive directive_table
[] = {
798 { 6, do_define
, "define", T_DEFINE
},
799 { 2, do_if
, "if", T_IF
},
800 { 5, do_xifdef
, "ifdef", T_IFDEF
},
801 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
802 { 5, do_endif
, "endif", T_ENDIF
},
803 { 4, do_else
, "else", T_ELSE
},
804 { 4, do_elif
, "elif", T_ELIF
},
805 { 4, do_line
, "line", T_LINE
},
806 { 7, do_include
, "include", T_INCLUDE
},
807 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
808 { 6, do_include
, "import", T_IMPORT
},
809 { 5, do_undef
, "undef", T_UNDEF
},
810 { 5, do_error
, "error", T_ERROR
},
811 { 7, do_error
, "warning", T_WARNING
},
812 #ifdef SCCS_DIRECTIVE
813 { 4, do_sccs
, "sccs", T_SCCS
},
815 { 6, do_pragma
, "pragma", T_PRAGMA
},
816 { 5, do_ident
, "ident", T_IDENT
},
817 { 6, do_assert
, "assert", T_ASSERT
},
818 { 8, do_unassert
, "unassert", T_UNASSERT
},
819 { -1, 0, "", T_UNUSED
},
822 /* When a directive handler is called,
823 this points to the # (or the : of the %:) that started the directive. */
824 U_CHAR
*directive_start
;
826 /* table to tell if char can be part of a C identifier. */
827 U_CHAR is_idchar
[256];
828 /* table to tell if char can be first char of a c identifier. */
829 U_CHAR is_idstart
[256];
830 /* table to tell if c is horizontal space. */
831 static U_CHAR is_hor_space
[256];
832 /* table to tell if c is horizontal or vertical space. */
833 U_CHAR is_space
[256];
835 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
836 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
838 static int errors
= 0; /* Error counter for exit code */
840 /* Name of output file, for error messages. */
841 static const char *out_fname
;
843 /* Nonzero to ignore \ in string constants. Use to treat #line 1 "A:\file.h
844 as a non-form feed. If you want it to be a form feed, you must use
846 static int ignore_escape_flag
= 1;
848 /* Stack of conditionals currently in progress
849 (including both successful and failing conditionals). */
852 struct if_stack
*next
; /* for chaining to the next stack frame */
853 const char *fname
; /* copied from input when frame is made */
854 size_t fname_len
; /* similarly */
855 int lineno
; /* similarly */
856 int if_succeeded
; /* true if a leg of this if-group
857 has been passed through rescan */
858 const U_CHAR
*control_macro
; /* For #ifndef at start of file,
859 this is the macro name tested. */
860 enum node_type type
; /* type of last directive seen in this group */
862 typedef struct if_stack IF_STACK_FRAME
;
863 static IF_STACK_FRAME
*if_stack
= NULL
;
865 /* Buffer of -M output. */
866 static char *deps_buffer
;
868 /* Number of bytes allocated in above. */
869 static int deps_allocated_size
;
871 /* Number of bytes used. */
872 static int deps_size
;
874 /* Number of bytes since the last newline. */
875 static int deps_column
;
877 /* Nonzero means -I- has been seen,
878 so don't look for #include "foo" the source-file directory. */
879 static int ignore_srcdir
;
881 static int safe_read
PROTO((int, char *, int));
882 static void safe_write
PROTO((int, const char *, int));
884 int main
PROTO((int, char **));
886 static void path_include
PROTO((char *));
888 static const U_CHAR
*index0
PROTO((const U_CHAR
*, int, size_t));
890 static void trigraph_pcp
PROTO((FILE_BUF
*));
891 static void check_white_space
PROTO((FILE_BUF
*));
893 static void newline_fix
PROTO((U_CHAR
*));
894 static void name_newline_fix
PROTO((U_CHAR
*));
896 static const char *get_lintcmd
PROTO((const U_CHAR
*, const U_CHAR
*,
897 const U_CHAR
**, int *, int *));
899 static void rescan
PROTO((FILE_BUF
*, int));
901 static FILE_BUF expand_to_temp_buffer
PROTO((const U_CHAR
*, const U_CHAR
*,
904 static int handle_directive
PROTO((FILE_BUF
*, FILE_BUF
*));
906 static struct tm
*timestamp
PROTO((void));
907 static void special_symbol
PROTO((HASHNODE
*, FILE_BUF
*));
909 static int is_system_include
PROTO((const char *));
910 static char *base_name
PROTO((const char *));
911 static int absolute_filename
PROTO((const char *));
912 static size_t simplify_filename
PROTO((char *));
914 static char *read_filename_string
PROTO((int, FILE *));
915 static struct file_name_map
*read_name_map
PROTO((const char *));
916 static int open_include_file
PROTO((char *, struct file_name_list
*,
917 const U_CHAR
*, struct include_file
**));
918 static char *remap_include_file
PROTO((char *, struct file_name_list
*));
919 static int lookup_ino_include
PROTO((struct include_file
*));
921 static void finclude
PROTO((int, struct include_file
*, FILE_BUF
*, int, struct file_name_list
*));
922 static void record_control_macro
PROTO((struct include_file
*, const U_CHAR
*));
924 static char *check_precompiled
PROTO((int, struct stat
*, const char *,
926 static int check_preconditions
PROTO((const char *));
927 static void pcfinclude
PROTO((U_CHAR
*, const U_CHAR
*, FILE_BUF
*));
928 static void pcstring_used
PROTO((HASHNODE
*));
929 static void write_output
PROTO((void));
930 static void pass_thru_directive
PROTO((const U_CHAR
*, const U_CHAR
*,
931 FILE_BUF
*, struct directive
*));
933 static MACRODEF create_definition
PROTO((const U_CHAR
*, const U_CHAR
*,
936 static int check_macro_name
PROTO((const U_CHAR
*, int));
937 static int compare_defs
PROTO((DEFINITION
*, DEFINITION
*));
938 static int comp_def_part
PROTO((int, const U_CHAR
*, int, const U_CHAR
*,
941 static DEFINITION
*collect_expansion
PROTO((const U_CHAR
*, const U_CHAR
*,
942 int, struct arglist
*));
944 int check_assertion
PROTO((const U_CHAR
*, int, int, struct arglist
*));
945 static int compare_token_lists
PROTO((struct arglist
*, struct arglist
*));
947 static struct arglist
*read_token_list
PROTO((const U_CHAR
**, const U_CHAR
*,
949 static void free_token_list
PROTO((struct arglist
*));
951 static ASSERTION_HASHNODE
*assertion_install
PROTO((const U_CHAR
*, int, int));
952 static ASSERTION_HASHNODE
*assertion_lookup
PROTO((const U_CHAR
*, int, int));
953 static void delete_assertion
PROTO((ASSERTION_HASHNODE
*));
955 static void do_once
PROTO((void));
957 static HOST_WIDEST_INT eval_if_expression
PROTO((const U_CHAR
*, int));
958 static void conditional_skip
PROTO((FILE_BUF
*, int, enum node_type
,
959 const U_CHAR
*, FILE_BUF
*));
960 static void skip_if_group
PROTO((FILE_BUF
*, int, FILE_BUF
*));
961 static void validate_else
PROTO((const U_CHAR
*, const U_CHAR
*));
963 static U_CHAR
*skip_to_end_of_comment
PROTO((FILE_BUF
*, int *, int));
964 static U_CHAR
*skip_quoted_string
PROTO((const U_CHAR
*, const U_CHAR
*,
965 int, int *, int *, int *));
966 static char *quote_string
PROTO((char *, const char *, size_t));
967 static U_CHAR
*skip_paren_group
PROTO((FILE_BUF
*));
969 /* Last arg to output_line_directive. */
970 enum file_change_code
{same_file
, enter_file
, leave_file
};
971 static void output_line_directive
PROTO((FILE_BUF
*, FILE_BUF
*, int, enum file_change_code
));
973 static void macroexpand
PROTO((HASHNODE
*, FILE_BUF
*));
976 static int macarg
PROTO((struct argdata
*, int));
978 static U_CHAR
*macarg1
PROTO((U_CHAR
*, const U_CHAR
*, struct hashnode
*, int *, int *, int *, int));
980 static int discard_comments
PROTO((U_CHAR
*, int, int));
982 static void change_newlines
PROTO((struct argdata
*));
984 static void notice
PVPROTO((const char *, ...)) ATTRIBUTE_PRINTF_1
;
985 static void vnotice
PROTO((const char *, va_list));
986 void error
PVPROTO((const char *, ...)) ATTRIBUTE_PRINTF_1
;
987 void verror
PROTO((const char *, va_list));
988 static void error_from_errno
PROTO((const char *));
989 void warning
PVPROTO((const char *, ...)) ATTRIBUTE_PRINTF_1
;
990 static void vwarning
PROTO((const char *, va_list));
991 static void error_with_line
PVPROTO((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
992 static void verror_with_line
PROTO((int, const char *, va_list));
993 static void vwarning_with_line
PROTO((int, const char *, va_list));
994 static void warning_with_line
PVPROTO((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
995 void pedwarn
PVPROTO((const char *, ...)) ATTRIBUTE_PRINTF_1
;
996 void pedwarn_with_line
PVPROTO((int, const char *, ...)) ATTRIBUTE_PRINTF_2
;
997 static void pedwarn_with_file_and_line
PVPROTO((const char *, size_t, int, const char *, ...)) ATTRIBUTE_PRINTF_4
;
998 static void pedwarn_strange_white_space
PROTO((int));
1000 static void print_containing_files
PROTO((void));
1002 static int line_for_error
PROTO((int));
1003 static int grow_outbuf
PROTO((FILE_BUF
*, int));
1005 static HASHNODE
*install
PROTO((const U_CHAR
*, int, enum node_type
,
1006 const char *, int));
1007 HASHNODE
*lookup
PROTO((const U_CHAR
*, int, int));
1008 static void delete_macro
PROTO((HASHNODE
*));
1009 static int hashf
PROTO((const U_CHAR
*, int, int));
1011 static void dump_single_macro
PROTO((HASHNODE
*, FILE *));
1012 static void dump_all_macros
PROTO((void));
1013 static void dump_defn_1
PROTO((const U_CHAR
*, int, int, FILE *));
1014 static void dump_arg_n
PROTO((DEFINITION
*, int, FILE *));
1016 static void initialize_char_syntax
PROTO((void));
1017 static void initialize_builtins
PROTO((FILE_BUF
*, FILE_BUF
*));
1019 static void make_definition
PROTO((char *));
1020 static void make_undef
PROTO((char *, FILE_BUF
*));
1022 static void make_assertion
PROTO((const char *, const char *));
1024 static struct file_name_list
*new_include_prefix
PROTO((struct file_name_list
*, const char *, const char *, const char *));
1025 static void append_include_chain
PROTO((struct file_name_list
*, struct file_name_list
*));
1027 static int quote_string_for_make
PROTO((char *, const char *));
1028 static void deps_output
PROTO((const char *, int));
1030 void fatal
PVPROTO((const char *, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN
;
1031 void fancy_abort
PROTO((void)) ATTRIBUTE_NORETURN
;
1032 static void perror_with_name
PROTO((const char *));
1033 static void pfatal_with_name
PROTO((const char *)) ATTRIBUTE_NORETURN
;
1034 static void pipe_closed
PROTO((int)) ATTRIBUTE_NORETURN
;
1036 static void memory_full
PROTO((void)) ATTRIBUTE_NORETURN
;
1037 static void print_help
PROTO((void));
1039 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1040 retrying if necessary. If MAX_READ_LEN is defined, read at most
1041 that bytes at a time. Return a negative value if an error occurs,
1042 otherwise return the actual number of bytes read,
1043 which must be LEN unless end-of-file was reached. */
1046 safe_read (desc
, ptr
, len
)
1051 int left
, rcount
, nchars
;
1057 if (rcount
> MAX_READ_LEN
)
1058 rcount
= MAX_READ_LEN
;
1060 nchars
= read (desc
, ptr
, rcount
);
1077 /* Write LEN bytes at PTR to descriptor DESC,
1078 retrying if necessary, and treating any real error as fatal.
1079 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1082 safe_write (desc
, ptr
, len
)
1087 int wcount
, written
;
1091 #ifdef MAX_WRITE_LEN
1092 if (wcount
> MAX_WRITE_LEN
)
1093 wcount
= MAX_WRITE_LEN
;
1095 written
= write (desc
, ptr
, wcount
);
1102 pfatal_with_name (out_fname
);
1113 printf ("Usage: %s [switches] input output\n", progname
);
1114 printf ("Switches:\n");
1115 printf (" -include <file> Include the contents of <file> before other files\n");
1116 printf (" -imacros <file> Accept definition of macros in <file>\n");
1117 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1118 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1119 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1120 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1121 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1122 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1123 printf (" -nostdinc Do not search the system include directories\n");
1124 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1125 printf (" -o <file> Put output into <file>\n");
1126 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1127 printf (" -traditional Follow K&R pre-processor behaviour\n");
1128 printf (" -trigraphs Support ANSI C trigraphs\n");
1129 printf (" -lang-c Assume that the input sources are in C\n");
1130 printf (" -lang-c89 Assume that the input is C89; depricated\n");
1131 printf (" -lang-c++ Assume that the input sources are in C++\n");
1132 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1133 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1134 printf (" -lang-asm Assume that the input sources are in assembler\n");
1135 printf (" -lang-chill Assume that the input sources are in Chill\n");
1136 printf (" -std=<std name> Specify the conformance standard; one of:\n");
1137 printf (" gnu89, gnu9x, c89, c9x, iso9899:1990,\n");
1138 printf (" iso9899:199409, iso9899:199x\n");
1139 printf (" -+ Allow parsing of C++ style features\n");
1140 printf (" -w Inhibit warning messages\n");
1141 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1142 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1143 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1144 printf (" -Wno-comment{s} Do not warn about comments\n");
1145 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1146 printf (" a string if -traditional is specified\n");
1147 printf (" -Wno-traditional Do not warn about stringification\n");
1148 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1149 printf (" -Wno-undef Do not warn about testing undefined macros\n");
1150 printf (" -Wimport Warn about the use of the #import directive\n");
1151 printf (" -Wno-import Do not warn about the use of #import\n");
1152 printf (" -Werror Treat all warnings as errors\n");
1153 printf (" -Wno-error Do not treat warnings as errors\n");
1154 printf (" -Wall Enable all preprocessor warnings\n");
1155 printf (" -M Generate make dependencies\n");
1156 printf (" -MM As -M, but ignore system header files\n");
1157 printf (" -MD As -M, but put output in a .d file\n");
1158 printf (" -MMD As -MD, but ignore system header files\n");
1159 printf (" -MG Treat missing header file as generated files\n");
1160 printf (" -g Include #define and #undef directives in the output\n");
1161 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1162 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1163 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1164 printf (" -U<macro> Undefine <macro> \n");
1165 printf (" -u or -undef Do not predefine any macros\n");
1166 printf (" -v Display the version number\n");
1167 printf (" -H Print the name of header files as they are used\n");
1168 printf (" -C Do not discard comments\n");
1169 printf (" -dM Display a list of macro definitions active at end\n");
1170 printf (" -dD Preserve macro definitions in output\n");
1171 printf (" -dN As -dD except that only the names are preserved\n");
1172 printf (" -dI Include #include directives in the output\n");
1173 printf (" -ifoutput Describe skipped code blocks in output \n");
1174 printf (" -P Do not generate #line directives\n");
1175 printf (" -$ Do not include '$' in identifiers\n");
1176 printf (" -remap Remap file names when including files.\n");
1177 printf (" -h or --help Display this information\n");
1186 const char *in_fname
;
1194 char **pend_assertions
;
1195 char **pend_includes
;
1197 /* Record the option used with each element of pend_assertions.
1198 This is preparation for supporting more than one option for making
1200 const char **pend_assertion_options
;
1201 int no_standard_includes
= 0;
1202 int no_standard_cplusplus_includes
= 0;
1203 int missing_newline
= 0;
1205 /* Non-0 means don't output the preprocessed program. */
1206 int inhibit_output
= 0;
1207 /* Non-0 means -v, so print the full set of include dirs. */
1210 /* File name which deps are being written to.
1211 This is 0 if deps are being written to stdout. */
1212 char *deps_file
= 0;
1213 /* Fopen file mode to open deps_file with. */
1214 const char *deps_mode
= "a";
1215 /* Stream on which to print the dependency information. */
1216 FILE *deps_stream
= 0;
1217 /* Target-name to write with the dependency information. */
1218 char *deps_target
= 0;
1220 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1221 /* Get rid of any avoidable limit on stack size. */
1225 /* Set the stack limit huge so that alloca (particularly stringtab
1226 in dbxread.c) does not fail. */
1227 getrlimit (RLIMIT_STACK
, &rlim
);
1228 rlim
.rlim_cur
= rlim
.rlim_max
;
1229 setrlimit (RLIMIT_STACK
, &rlim
);
1234 signal (SIGPIPE
, pipe_closed
);
1237 #ifdef HAVE_LC_MESSAGES
1238 setlocale (LC_MESSAGES
, "");
1240 (void) bindtextdomain (PACKAGE
, localedir
);
1241 (void) textdomain (PACKAGE
);
1243 progname
= base_name (argv
[0]);
1247 /* Remove extension from PROGNAME. */
1249 char *s
= xstrdup (progname
);
1252 if ((p
= rindex (s
, ';')) != 0) *p
= '\0'; /* strip version number */
1253 if ((p
= rindex (s
, '.')) != 0 /* strip type iff ".exe" */
1254 && (p
[1] == 'e' || p
[1] == 'E')
1255 && (p
[2] == 'x' || p
[2] == 'X')
1256 && (p
[3] == 'e' || p
[3] == 'E')
1262 /* Do not invoke xmalloc before this point, since locale and
1263 progname need to be set first, in case a diagnostic is issued. */
1265 pend_files
= (char **) xmalloc (argc
* sizeof (char *));
1266 pend_defs
= (char **) xmalloc (argc
* sizeof (char *));
1267 pend_undefs
= (char **) xmalloc (argc
* sizeof (char *));
1268 pend_assertions
= (char **) xmalloc (argc
* sizeof (char *));
1269 pend_includes
= (char **) xmalloc (argc
* sizeof (char *));
1270 pend_assertion_options
= (const char **) xmalloc (argc
* sizeof (char *));
1275 /* Initialize is_idchar. */
1276 initialize_char_syntax ();
1278 no_line_directives
= 0;
1280 dump_macros
= dump_none
;
1283 cplusplus_comments
= 1;
1285 bzero ((char *) pend_files
, argc
* sizeof (char *));
1286 bzero ((char *) pend_defs
, argc
* sizeof (char *));
1287 bzero ((char *) pend_undefs
, argc
* sizeof (char *));
1288 bzero ((char *) pend_assertions
, argc
* sizeof (char *));
1289 bzero ((char *) pend_includes
, argc
* sizeof (char *));
1291 #ifdef MULTIBYTE_CHARS
1292 /* Change to the native locale for multibyte conversions. */
1293 setlocale (LC_CTYPE
, "");
1294 literal_codeset
= getenv ("LANG");
1297 /* Process switches and find input file name. */
1299 for (i
= 1; i
< argc
; i
++) {
1300 if (argv
[i
][0] != '-') {
1301 if (out_fname
!= NULL
)
1304 fatal ("Too many arguments");
1306 else if (in_fname
!= NULL
)
1307 out_fname
= argv
[i
];
1311 switch (argv
[i
][1]) {
1314 if (!strcmp (argv
[i
], "-include")) {
1316 fatal ("Filename missing after `-include' option");
1319 simplify_filename (pend_includes
[i
] = argv
[i
]);
1322 if (!strcmp (argv
[i
], "-imacros")) {
1324 fatal ("Filename missing after `-imacros' option");
1327 simplify_filename (pend_files
[i
] = argv
[i
]);
1330 if (!strcmp (argv
[i
], "-iprefix")) {
1332 fatal ("Filename missing after `-iprefix' option");
1334 include_prefix
= argv
[++i
];
1336 if (!strcmp (argv
[i
], "-ifoutput")) {
1337 output_conditionals
= 1;
1339 if (!strcmp (argv
[i
], "-isystem")) {
1340 struct file_name_list
*dirtmp
;
1342 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1345 dirtmp
->c_system_include_path
= 1;
1347 if (before_system
== 0)
1348 before_system
= dirtmp
;
1350 last_before_system
->next
= dirtmp
;
1351 last_before_system
= dirtmp
; /* Tail follows the last one */
1353 /* Add directory to end of path for includes,
1354 with the default prefix at the front of its name. */
1355 if (!strcmp (argv
[i
], "-iwithprefix")) {
1356 struct file_name_list
*dirtmp
;
1359 if (include_prefix
!= 0)
1360 prefix
= include_prefix
;
1362 prefix
= xstrdup (GCC_INCLUDE_DIR
);
1363 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1364 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1365 prefix
[strlen (prefix
) - 7] = 0;
1368 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1369 prefix
, argv
[++i
])))
1372 if (after_include
== 0)
1373 after_include
= dirtmp
;
1375 last_after_include
->next
= dirtmp
;
1376 last_after_include
= dirtmp
; /* Tail follows the last one */
1378 /* Add directory to main path for includes,
1379 with the default prefix at the front of its name. */
1380 if (!strcmp (argv
[i
], "-iwithprefixbefore")) {
1381 struct file_name_list
*dirtmp
;
1384 if (include_prefix
!= 0)
1385 prefix
= include_prefix
;
1387 prefix
= xstrdup (GCC_INCLUDE_DIR
);
1388 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1389 if (!strcmp (prefix
+ strlen (prefix
) - 8, "/include"))
1390 prefix
[strlen (prefix
) - 7] = 0;
1393 dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
, prefix
, argv
[++i
]);
1394 append_include_chain (dirtmp
, dirtmp
);
1396 /* Add directory to end of path for includes. */
1397 if (!strcmp (argv
[i
], "-idirafter")) {
1398 struct file_name_list
*dirtmp
;
1400 if (! (dirtmp
= new_include_prefix (NULL_PTR
, NULL_PTR
,
1404 if (after_include
== 0)
1405 after_include
= dirtmp
;
1407 last_after_include
->next
= dirtmp
;
1408 last_after_include
= dirtmp
; /* Tail follows the last one */
1413 if (out_fname
!= NULL
)
1414 fatal ("Output filename specified twice");
1416 fatal ("Filename missing after -o option");
1417 out_fname
= argv
[++i
];
1418 if (!strcmp (out_fname
, "-"))
1423 if (!strcmp (argv
[i
], "-pedantic"))
1425 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
1427 pedantic_errors
= 1;
1428 } else if (!strcmp (argv
[i
], "-pcp")) {
1431 fatal ("Filename missing after -pcp option");
1432 pcp_fname
= argv
[++i
];
1434 = ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
1435 ? fopen (pcp_fname
, "w")
1437 if (pcp_outfile
== 0)
1438 pfatal_with_name (pcp_fname
);
1444 if (!strcmp (argv
[i
], "-traditional")) {
1446 cplusplus_comments
= 0;
1447 } else if (!strcmp (argv
[i
], "-trigraphs")) {
1453 if (! strcmp (argv
[i
], "-lang-c"))
1454 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c9x
= 1, objc
= 0;
1455 else if (! strcmp (argv
[i
], "-lang-c89"))
1456 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c9x
= 0, objc
= 0;
1457 else if (! strcmp (argv
[i
], "-lang-c++"))
1458 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c9x
= 0, objc
= 0;
1459 else if (! strcmp (argv
[i
], "-lang-objc"))
1460 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c9x
= 0, objc
= 1;
1461 else if (! strcmp (argv
[i
], "-lang-objc++"))
1462 cplusplus
= 1, cplusplus_comments
= 1, c89
= 0, c9x
= 0, objc
= 1;
1463 else if (! strcmp (argv
[i
], "-lang-asm"))
1465 else if (! strcmp (argv
[i
], "-lint"))
1470 cplusplus
= 1, cplusplus_comments
= 1;
1474 if (!strcmp (argv
[i
], "-std=iso9899:1990")
1475 || !strcmp (argv
[i
], "-std=iso9899:199409")
1476 || !strcmp (argv
[i
], "-std=c89")
1477 || !strcmp (argv
[i
], "-std=gnu89"))
1478 cplusplus
= 0, cplusplus_comments
= 0, c89
= 1, c9x
= 0, objc
= 0;
1479 else if (!strcmp (argv
[i
], "-std=iso9899:199x")
1480 || !strcmp (argv
[i
], "-std=c9x")
1481 || !strcmp (argv
[i
], "-std=gnu9x"))
1482 cplusplus
= 0, cplusplus_comments
= 1, c89
= 0, c9x
= 1, objc
= 0;
1486 inhibit_warnings
= 1;
1490 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1492 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1494 else if (!strcmp (argv
[i
], "-Wcomment"))
1496 else if (!strcmp (argv
[i
], "-Wno-comment"))
1498 else if (!strcmp (argv
[i
], "-Wcomments"))
1500 else if (!strcmp (argv
[i
], "-Wno-comments"))
1502 else if (!strcmp (argv
[i
], "-Wtraditional"))
1504 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1506 else if (!strcmp (argv
[i
], "-Wwhite-space"))
1507 warn_white_space
= 1;
1508 else if (!strcmp (argv
[i
], "-Wno-white-space"))
1509 warn_white_space
= 0;
1510 else if (!strcmp (argv
[i
], "-Wundef"))
1512 else if (!strcmp (argv
[i
], "-Wno-undef"))
1514 else if (!strcmp (argv
[i
], "-Wimport"))
1516 else if (!strcmp (argv
[i
], "-Wno-import"))
1518 else if (!strcmp (argv
[i
], "-Werror"))
1519 warnings_are_errors
= 1;
1520 else if (!strcmp (argv
[i
], "-Wno-error"))
1521 warnings_are_errors
= 0;
1522 else if (!strcmp (argv
[i
], "-Wall"))
1526 warn_white_space
= 1;
1531 if (!strcmp (argv
[i
], "-fleading-underscore"))
1532 user_label_prefix
= "_";
1533 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
1534 user_label_prefix
= "";
1538 /* The style of the choices here is a bit mixed.
1539 The chosen scheme is a hybrid of keeping all options in one string
1540 and specifying each option in a separate argument:
1541 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1542 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1543 -M[M][G][D file]. This is awkward to handle in specs, and is not
1545 /* ??? -MG must be specified in addition to one of -M or -MM.
1546 This can be relaxed in the future without breaking anything.
1547 The converse isn't true. */
1549 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1550 if (!strcmp (argv
[i
], "-MG"))
1552 print_deps_missing_files
= 1;
1555 if (!strcmp (argv
[i
], "-M"))
1557 else if (!strcmp (argv
[i
], "-MM"))
1559 else if (!strcmp (argv
[i
], "-MD"))
1561 else if (!strcmp (argv
[i
], "-MMD"))
1563 /* For -MD and -MMD options, write deps on file named by next arg. */
1564 if (!strcmp (argv
[i
], "-MD")
1565 || !strcmp (argv
[i
], "-MMD")) {
1567 fatal ("Filename missing after %s option", argv
[i
]);
1569 deps_file
= argv
[i
];
1572 /* For -M and -MM, write deps on standard output
1573 and suppress the usual output. */
1574 deps_stream
= stdout
;
1581 char *p
= argv
[i
] + 2;
1583 while ((c
= *p
++)) {
1584 /* Arg to -d specifies what parts of macros to dump */
1587 dump_macros
= dump_only
;
1591 dump_macros
= dump_names
;
1594 dump_macros
= dump_definitions
;
1605 if (argv
[i
][2] == '3')
1610 if (strcmp (argv
[i
], "--help") != 0)
1617 notice ("GNU CPP version %s", version_string
);
1618 #ifdef TARGET_VERSION
1621 fprintf (stderr
, "\n");
1626 print_include_names
= 1;
1630 if (argv
[i
][2] != 0)
1631 pend_defs
[i
] = argv
[i
] + 2;
1632 else if (i
+ 1 == argc
)
1633 fatal ("Macro name missing after -D option");
1635 i
++, pend_defs
[i
] = argv
[i
];
1642 if (argv
[i
][2] != 0)
1644 else if (i
+ 1 == argc
)
1645 fatal ("Assertion missing after -A option");
1649 if (!strcmp (p
, "-")) {
1650 /* -A- eliminates all predefined macros and assertions.
1651 Let's include also any that were specified earlier
1652 on the command line. That way we can get rid of any
1653 that were passed automatically in from GCC. */
1655 for (j
= 0; j
< i
; j
++)
1656 pend_defs
[j
] = pend_assertions
[j
] = 0;
1658 pend_assertions
[i
] = p
;
1659 pend_assertion_options
[i
] = "-A";
1664 case 'U': /* JF #undef something */
1665 if (argv
[i
][2] != 0)
1666 pend_undefs
[i
] = argv
[i
] + 2;
1667 else if (i
+ 1 == argc
)
1668 fatal ("Macro name missing after -U option");
1670 pend_undefs
[i
] = argv
[i
+1], i
++;
1674 put_out_comments
= 1;
1677 case 'E': /* -E comes from cc -E; ignore it. */
1681 no_line_directives
= 1;
1684 case '$': /* Don't include $ in identifiers. */
1685 is_idchar
['$'] = is_idstart
['$'] = 0;
1688 case 'I': /* Add directory to path for includes. */
1690 struct file_name_list
*dirtmp
;
1691 char *dir
= argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
];
1693 if (! ignore_srcdir
&& !strcmp (dir
, "-")) {
1695 /* Don't use any preceding -I directories for #include <...>. */
1696 first_bracket_include
= 0;
1699 dirtmp
= new_include_prefix (last_include
, NULL_PTR
, "", dir
);
1700 append_include_chain (dirtmp
, dirtmp
);
1706 if (!strcmp (argv
[i
], "-nostdinc"))
1707 /* -nostdinc causes no default include directories.
1708 You must specify all include-file directories with -I. */
1709 no_standard_includes
= 1;
1710 else if (!strcmp (argv
[i
], "-nostdinc++"))
1711 /* -nostdinc++ causes no default C++-specific include directories. */
1712 no_standard_cplusplus_includes
= 1;
1713 else if (!strcmp (argv
[i
], "-noprecomp"))
1718 if (!strcmp (argv
[i
], "-remap"))
1722 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1723 if (in_fname
== NULL
) {
1726 } else if (out_fname
== NULL
) {
1729 } /* else fall through into error */
1732 fatal ("Invalid option `%s'", argv
[i
]);
1737 /* Add dirs from CPATH after dirs from -I. */
1738 /* There seems to be confusion about what CPATH should do,
1739 so for the moment it is not documented. */
1740 /* Some people say that CPATH should replace the standard include dirs,
1741 but that seems pointless: it comes before them, so it overrides them
1743 GET_ENV_PATH_LIST (cp
, "CPATH");
1744 if (cp
&& ! no_standard_includes
)
1747 /* Initialize output buffer */
1749 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
1750 outbuf
.bufp
= outbuf
.buf
;
1751 outbuf
.length
= OUTBUF_SIZE
;
1753 /* Do partial setup of input buffer for the sake of generating
1754 early #line directives (when -g is in effect). */
1756 fp
= &instack
[++indepth
];
1757 if (in_fname
== NULL
)
1759 fp
->nominal_fname
= fp
->fname
= in_fname
;
1760 fp
->nominal_fname_len
= strlen (in_fname
);
1763 /* In C++, wchar_t is a distinct basic type, and we can expect
1764 __wchar_t to be defined by cc1plus. */
1766 wchar_type
= "__wchar_t";
1768 /* Install __LINE__, etc. Must follow initialize_char_syntax
1769 and option processing. */
1770 initialize_builtins (fp
, &outbuf
);
1772 /* Now handle the command line options. */
1774 /* Do -U's, -D's and -A's in the order they were seen. */
1775 for (i
= 1; i
< argc
; i
++) {
1776 if (pend_undefs
[i
]) {
1778 output_line_directive (fp
, &outbuf
, 0, same_file
);
1779 make_undef (pend_undefs
[i
], &outbuf
);
1783 output_line_directive (fp
, &outbuf
, 0, same_file
);
1784 make_definition (pend_defs
[i
]);
1786 if (pend_assertions
[i
])
1787 make_assertion (pend_assertion_options
[i
], pend_assertions
[i
]);
1790 done_initializing
= 1;
1792 { /* Read the appropriate environment variable and if it exists
1793 replace include_defaults with the listed path. */
1795 switch ((objc
<< 1) + cplusplus
)
1798 GET_ENV_PATH_LIST (epath
, "C_INCLUDE_PATH");
1801 GET_ENV_PATH_LIST (epath
, "CPLUS_INCLUDE_PATH");
1804 GET_ENV_PATH_LIST (epath
, "OBJC_INCLUDE_PATH");
1807 GET_ENV_PATH_LIST (epath
, "OBJCPLUS_INCLUDE_PATH");
1810 /* If the environment var for this language is set,
1811 add to the default list of include directories. */
1814 char *startp
, *endp
;
1816 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
1817 if (*startp
== PATH_SEPARATOR
)
1820 = (struct default_include
*) xmalloc ((num_dirs
1821 * sizeof (struct default_include
))
1822 + sizeof (include_defaults_array
));
1823 startp
= endp
= epath
;
1827 if (c
== PATH_SEPARATOR
|| !c
) {
1829 include_defaults
[num_dirs
].fname
1830 = startp
== endp
? "." : xstrdup (startp
);
1832 include_defaults
[num_dirs
].component
= 0;
1833 include_defaults
[num_dirs
].cplusplus
= cplusplus
;
1834 include_defaults
[num_dirs
].cxx_aware
= 1;
1841 /* Put the usual defaults back in at the end. */
1842 bcopy ((const PTR
) include_defaults_array
,
1843 (PTR
) &include_defaults
[num_dirs
],
1844 sizeof (include_defaults_array
));
1848 append_include_chain (before_system
, last_before_system
);
1849 first_system_include
= before_system
;
1851 /* Unless -fnostdinc,
1852 tack on the standard include file dirs to the specified list */
1853 if (!no_standard_includes
) {
1854 struct default_include
*p
= include_defaults
;
1855 char *specd_prefix
= include_prefix
;
1856 char *default_prefix
= xstrdup (GCC_INCLUDE_DIR
);
1857 int default_len
= 0;
1858 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1859 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
1860 default_len
= strlen (default_prefix
) - 7;
1861 default_prefix
[default_len
] = 0;
1863 /* Search "translated" versions of GNU directories.
1864 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1865 if (specd_prefix
!= 0 && default_len
!= 0)
1866 for (p
= include_defaults
; p
->fname
; p
++) {
1867 /* Some standard dirs are only for C++. */
1868 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1869 /* Does this dir start with the prefix? */
1870 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
1871 /* Yes; change prefix and add to search list. */
1872 struct file_name_list
*new
1873 = new_include_prefix (NULL_PTR
, NULL_PTR
, specd_prefix
,
1874 p
->fname
+ default_len
);
1876 new->c_system_include_path
= !p
->cxx_aware
;
1877 append_include_chain (new, new);
1878 if (first_system_include
== 0)
1879 first_system_include
= new;
1885 /* Search ordinary names for GNU include directories. */
1886 for (p
= include_defaults
; p
->fname
; p
++) {
1887 /* Some standard dirs are only for C++. */
1888 if (!p
->cplusplus
|| (cplusplus
&& !no_standard_cplusplus_includes
)) {
1889 struct file_name_list
*new
1890 = new_include_prefix (NULL_PTR
, p
->component
, "", p
->fname
);
1892 new->c_system_include_path
= !p
->cxx_aware
;
1893 append_include_chain (new, new);
1894 if (first_system_include
== 0)
1895 first_system_include
= new;
1902 /* Tack the after_include chain at the end of the include chain. */
1903 append_include_chain (after_include
, last_after_include
);
1904 if (first_system_include
== 0)
1905 first_system_include
= after_include
;
1907 /* With -v, print the list of dirs to search. */
1909 struct file_name_list
*p
;
1910 notice ("#include \"...\" search starts here:\n");
1911 for (p
= include
; p
; p
= p
->next
) {
1912 if (p
== first_bracket_include
)
1913 notice ("#include <...> search starts here:\n");
1915 fprintf (stderr
, " .\n");
1916 else if (!strcmp (p
->fname
, "/") || !strcmp (p
->fname
, "//"))
1917 fprintf (stderr
, " %s\n", p
->fname
);
1919 /* Omit trailing '/'. */
1920 fprintf (stderr
, " %.*s\n", (int) strlen (p
->fname
) - 1, p
->fname
);
1922 notice ("End of search list.\n");
1924 struct default_include
* d
;
1925 notice ("The following default directories have been omitted from the search path:\n");
1926 for (d
= include_defaults
; d
->fname
; d
++)
1928 fprintf (stderr
, " %s\n", d
->fname
);
1929 notice ("End of omitted list.\n");
1933 /* -MG doesn't select the form of output and must be specified with one of
1934 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1935 inhibit compilation. */
1936 if (print_deps_missing_files
&& (print_deps
== 0 || !inhibit_output
))
1937 fatal ("-MG must be specified with one of -M or -MM");
1939 /* Either of two environment variables can specify output of deps.
1940 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1941 where OUTPUT_FILE is the file to write deps info to
1942 and DEPS_TARGET is the target to mention in the deps. */
1945 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1946 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1947 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
1952 spec
= getenv ("SUNPRO_DEPENDENCIES");
1958 /* Find the space before the DEPS_TARGET, if there is one. */
1959 s
= index (spec
, ' ');
1961 deps_target
= s
+ 1;
1962 output_file
= xmalloc (s
- spec
+ 1);
1963 bcopy (spec
, output_file
, s
- spec
);
1964 output_file
[s
- spec
] = 0;
1970 deps_file
= output_file
;
1974 /* For -M, print the expected object file name
1975 as the target of this Make-rule. */
1977 deps_allocated_size
= 200;
1978 deps_buffer
= xmalloc (deps_allocated_size
);
1984 deps_output (deps_target
, ':');
1985 } else if (*in_fname
== 0) {
1986 deps_output ("-", ':');
1991 q
= base_name (in_fname
);
1993 /* Copy remainder to mungable area. */
1994 p
= (char *) alloca (strlen(q
) + 8);
1997 /* Output P, but remove known suffixes. */
2001 && p
[len
- 2] == '.'
2002 && index("cCsSm", p
[len
- 1]))
2005 && p
[len
- 3] == '.'
2006 && p
[len
- 2] == 'c'
2007 && p
[len
- 1] == 'c')
2010 && p
[len
- 4] == '.'
2011 && p
[len
- 3] == 'c'
2012 && p
[len
- 2] == 'x'
2013 && p
[len
- 1] == 'x')
2016 && p
[len
- 4] == '.'
2017 && p
[len
- 3] == 'c'
2018 && p
[len
- 2] == 'p'
2019 && p
[len
- 1] == 'p')
2022 /* Supply our own suffix. */
2023 strcpy (q
, OBJECT_SUFFIX
);
2025 deps_output (p
, ':');
2028 deps_output (in_fname
, ' ');
2031 /* Scan the -imacros files before the main input.
2032 Much like #including them, but with no_output set
2033 so that only their macro definitions matter. */
2035 no_output
++; no_record_file
++;
2036 for (i
= 1; i
< argc
; i
++)
2037 if (pend_files
[i
]) {
2038 struct include_file
*inc
;
2039 int fd
= open_include_file (pend_files
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2041 perror_with_name (pend_files
[i
]);
2042 return FATAL_EXIT_CODE
;
2044 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2046 no_output
--; no_record_file
--;
2048 /* Copy the entire contents of the main input file into
2049 the stacked input buffer previously allocated for it. */
2051 /* JF check for stdin */
2052 if (in_fname
== NULL
|| *in_fname
== 0) {
2055 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
2058 if (fstat (f
, &st
) != 0)
2059 pfatal_with_name (in_fname
);
2060 fp
->nominal_fname
= fp
->fname
= in_fname
;
2061 fp
->nominal_fname_len
= strlen (in_fname
);
2063 fp
->system_header_p
= 0;
2064 /* JF all this is mine about reading pipes and ttys */
2065 if (! S_ISREG (st
.st_mode
)) {
2066 /* Read input from a file that is not a normal disk file.
2067 We cannot preallocate a buffer with the correct size,
2068 so we must read in the file a piece at the time and make it bigger. */
2073 if (S_ISDIR (st
.st_mode
))
2074 fatal ("Input file `%s' is a directory", in_fname
);
2078 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
2080 cnt
= safe_read (f
, (char *) fp
->buf
+ size
, bsize
- size
);
2081 if (cnt
< 0) goto perror
; /* error! */
2083 if (size
!= bsize
) break; /* End of file */
2085 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
2089 /* Read a file whose size we can determine in advance.
2090 For the sake of VMS, st.st_size is just an upper bound. */
2091 size_t s
= (size_t) st
.st_size
;
2092 if (s
!= st
.st_size
|| s
+ 2 < s
)
2094 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
2095 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
2096 if (fp
->length
< 0) goto perror
;
2099 fp
->if_stack
= if_stack
;
2101 /* Make sure data ends with a newline. And put a null after it. */
2103 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
2104 /* Backslash-newline at end is not good enough. */
2105 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
2106 fp
->buf
[fp
->length
++] = '\n';
2107 missing_newline
= 1;
2109 fp
->buf
[fp
->length
] = '\0';
2111 /* Unless inhibited, convert trigraphs in the input. */
2116 if (warn_white_space
)
2117 check_white_space (fp
);
2119 /* Now that we know the input file is valid, open the output. */
2121 if (!out_fname
|| !strcmp (out_fname
, ""))
2122 out_fname
= "stdout";
2123 else if (! freopen (out_fname
, "w", stdout
))
2124 pfatal_with_name (out_fname
);
2126 output_line_directive (fp
, &outbuf
, 0, same_file
);
2128 /* Scan the -include files before the main input. */
2131 for (i
= 1; i
< argc
; i
++)
2132 if (pend_includes
[i
]) {
2133 struct include_file
*inc
;
2134 int fd
= open_include_file (pend_includes
[i
], NULL_PTR
, NULL_PTR
, &inc
);
2136 perror_with_name (pend_includes
[i
]);
2137 return FATAL_EXIT_CODE
;
2139 finclude (fd
, inc
, &outbuf
, 0, NULL_PTR
);
2143 /* Scan the input, processing macros and directives. */
2145 rescan (&outbuf
, 0);
2147 if (missing_newline
)
2150 if (pedantic
&& missing_newline
)
2151 pedwarn ("file does not end in newline");
2153 /* Now we have processed the entire input
2154 Write whichever kind of output has been requested. */
2156 if (dump_macros
== dump_only
)
2158 else if (! inhibit_output
) {
2163 /* Don't actually write the deps file if compilation has failed. */
2165 if (deps_file
&& ! (deps_stream
= fopen (deps_file
, deps_mode
)))
2166 pfatal_with_name (deps_file
);
2167 fputs (deps_buffer
, deps_stream
);
2168 putc ('\n', deps_stream
);
2170 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
2171 fatal ("I/O error on output");
2176 if (pcp_outfile
&& pcp_outfile
!= stdout
2177 && (ferror (pcp_outfile
) || fclose (pcp_outfile
) != 0))
2178 fatal ("I/O error on `-pcp' output");
2180 if (ferror (stdout
) || fclose (stdout
) != 0)
2181 fatal ("I/O error on output");
2184 exit (FATAL_EXIT_CODE
);
2185 exit (SUCCESS_EXIT_CODE
);
2188 pfatal_with_name (in_fname
);
2192 /* Given a colon-separated list of file names PATH,
2193 add all the names to the search path for include files. */
2207 struct file_name_list
*dirtmp
;
2209 /* Find the end of this name. */
2210 while ((c
= *q
++) != PATH_SEPARATOR
&& c
)
2214 dirtmp
= new_include_prefix (last_include
, NULL_PTR
,
2215 "", p
== q
? "." : p
);
2217 append_include_chain (dirtmp
, dirtmp
);
2219 /* Advance past this name. */
2226 /* Return the address of the first character in S that equals C.
2227 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2228 Return 0 if there is no such character. Assume that C itself is not '\0'.
2229 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2230 but unfortunately memchr isn't autoconfigured yet. */
2232 static const U_CHAR
*
2238 const char *p
= (const char *) s
;
2240 const char *q
= index (p
, c
);
2244 size_t l
= strlen (p
);
2254 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2255 before main CCCP processing. Name `pcp' is also in honor of the
2256 drugs the trigraph designers must have been on.
2258 Using an extra pass through the buffer takes a little extra time,
2259 but is infinitely less hairy than trying to handle trigraphs inside
2260 strings, etc. everywhere, and also makes sure that trigraphs are
2261 only translated in the top level of processing. */
2267 register U_CHAR c
, *bptr
;
2268 register const U_CHAR
*fptr
, *sptr
, *lptr
;
2271 fptr
= sptr
= bptr
= buf
->buf
;
2272 lptr
= fptr
+ buf
->length
;
2273 while ((sptr
= index0 (sptr
, '?', (size_t) (lptr
- sptr
))) != NULL
) {
2310 len
= sptr
- fptr
- 2;
2312 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2313 C, this will be memmove (). */
2314 if (bptr
!= fptr
&& len
> 0)
2315 bcopy ((const PTR
) fptr
, (PTR
) bptr
, len
);
2321 len
= buf
->length
- (fptr
- buf
->buf
);
2322 if (bptr
!= fptr
&& len
> 0)
2323 bcopy ((const PTR
) fptr
, (PTR
) bptr
, len
);
2324 buf
->length
-= fptr
- bptr
;
2325 buf
->buf
[buf
->length
] = '\0';
2326 if (warn_trigraphs
&& fptr
!= bptr
)
2327 warning_with_line (0, "%lu trigraph(s) encountered",
2328 (unsigned long) (fptr
- bptr
) / 2);
2331 /* Warn about white space between backslash and end of line. */
2334 check_white_space (buf
)
2337 register const U_CHAR
*sptr
= buf
->buf
;
2338 register const U_CHAR
*lptr
= sptr
+ buf
->length
;
2339 register const U_CHAR
*nptr
;
2342 nptr
= sptr
= buf
->buf
;
2343 lptr
= sptr
+ buf
->length
;
2345 (nptr
= index0 (nptr
, '\n', (size_t) (lptr
- nptr
))) != NULL
;
2347 register const U_CHAR
*p
= nptr
;
2349 for (p
= nptr
; sptr
< p
; p
--) {
2350 if (! is_hor_space
[p
[-1]]) {
2351 if (p
[-1] == '\\' && p
!= nptr
)
2352 warning_with_line (line
,
2353 "`\\' followed by white space at end of line");
2360 /* Move all backslash-newline pairs out of embarrassing places.
2361 Exchange all such pairs following BP
2362 with any potentially-embarrassing characters that follow them.
2363 Potentially-embarrassing characters are / and *
2364 (because a backslash-newline inside a comment delimiter
2365 would cause it not to be recognized).
2366 We assume that *BP == '\\'. */
2372 register U_CHAR
*p
= bp
;
2374 /* First count the backslash-newline pairs here. */
2379 } while (*p
== '\\');
2381 /* What follows the backslash-newlines is not embarrassing. */
2383 if (*p
!= '/' && *p
!= '*')
2384 /* What follows the backslash-newlines is not embarrassing. */
2387 /* Copy all potentially embarrassing characters
2388 that follow the backslash-newline pairs
2389 down to where the pairs originally started. */
2392 while (*p
== '*' || *p
== '/');
2394 /* Now write the same number of pairs after the embarrassing chars. */
2401 /* Like newline_fix but for use within a directive-name.
2402 Move any backslash-newlines up past any following symbol constituents. */
2405 name_newline_fix (bp
)
2408 register U_CHAR
*p
= bp
;
2410 /* First count the backslash-newline pairs here. */
2415 } while (*p
== '\\');
2417 /* What follows the backslash-newlines is not embarrassing. */
2420 /* What follows the backslash-newlines is not embarrassing. */
2423 /* Copy all potentially embarrassing characters
2424 that follow the backslash-newline pairs
2425 down to where the pairs originally started. */
2428 while (is_idchar
[*p
]);
2430 /* Now write the same number of pairs after the embarrassing chars. */
2437 /* Look for lint commands in comments.
2439 When we come in here, ibp points into a comment. Limit is as one expects.
2440 scan within the comment -- it should start, after lwsp, with a lint command.
2441 If so that command is returned as a (constant) string.
2443 Upon return, any arg will be pointed to with argstart and will be
2444 arglen long. Note that we don't parse that arg since it will just
2445 be printed out again. */
2448 get_lintcmd (ibp
, limit
, argstart
, arglen
, cmdlen
)
2449 register const U_CHAR
*ibp
;
2450 register const U_CHAR
*limit
;
2451 const U_CHAR
**argstart
; /* point to command arg */
2452 int *arglen
, *cmdlen
; /* how long they are */
2454 HOST_WIDEST_INT linsize
;
2455 register const U_CHAR
*numptr
; /* temp for arg parsing */
2459 SKIP_WHITE_SPACE (ibp
);
2461 if (ibp
>= limit
) return NULL
;
2463 linsize
= limit
- ibp
;
2465 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2466 if ((linsize
>= 10) && !bcmp (ibp
, "NOTREACHED", 10)) {
2468 return "NOTREACHED";
2470 if ((linsize
>= 8) && !bcmp (ibp
, "ARGSUSED", 8)) {
2474 if ((linsize
>= 11) && !bcmp (ibp
, "LINTLIBRARY", 11)) {
2476 return "LINTLIBRARY";
2478 if ((linsize
>= 7) && !bcmp (ibp
, "VARARGS", 7)) {
2480 ibp
+= 7; linsize
-= 7;
2481 if ((linsize
== 0) || ! ISDIGIT (*ibp
)) return "VARARGS";
2483 /* OK, read a number */
2484 for (numptr
= *argstart
= ibp
; (numptr
< limit
) && ISDIGIT (*numptr
);
2486 *arglen
= numptr
- *argstart
;
2493 * The main loop of the program.
2495 * Read characters from the input stack, transferring them to the
2498 * Macros are expanded and push levels on the input stack.
2499 * At the end of such a level it is popped off and we keep reading.
2500 * At the end of any other kind of level, we return.
2501 * #-directives are handled, except within macros.
2503 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2504 * and insert them when appropriate. This is set while scanning macro
2505 * arguments before substitution. It is zero when scanning for final output.
2506 * There are two types of Newline markers:
2507 * * Newline - follows a macro name that was not expanded
2508 * because it appeared inside an expansion of the same macro.
2509 * This marker prevents future expansion of that identifier.
2510 * When the input is rescanned into the final output, these are deleted.
2511 * These are also deleted by ## concatenation.
2512 * * Newline Space (or Newline and any other whitespace character)
2513 * stands for a place that tokens must be separated or whitespace
2514 * is otherwise desirable, but where the ANSI standard specifies there
2515 * is no whitespace. This marker turns into a Space (or whichever other
2516 * whitespace char appears in the marker) in the final output,
2517 * but it turns into nothing in an argument that is stringified with #.
2518 * Such stringified arguments are the only place where the ANSI standard
2519 * specifies with precision that whitespace may not appear.
2521 * During this function, IP->bufp is kept cached in IBP for speed of access.
2522 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2523 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2524 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2525 * explicitly, and before RECACHE, since RECACHE uses OBP.
2529 rescan (op
, output_marks
)
2533 /* Character being scanned in main loop. */
2536 /* Length of pending accumulated identifier. */
2537 register int ident_length
= 0;
2539 /* Hash code of pending accumulated identifier. */
2540 register int hash
= 0;
2542 /* Current input level (&instack[indepth]). */
2545 /* Pointer for scanning input. */
2546 register U_CHAR
*ibp
;
2548 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2549 register U_CHAR
*limit
;
2551 /* Pointer for storing output. */
2552 register U_CHAR
*obp
;
2554 /* REDO_CHAR is nonzero if we are processing an identifier
2555 after backing up over the terminating character.
2556 Sometimes we process an identifier without backing up over
2557 the terminating character, if the terminating character
2558 is not special. Backing up is done so that the terminating character
2559 will be dispatched on again once the identifier is dealt with. */
2562 /* 1 if within an identifier inside of which a concatenation
2563 marker (Newline -) has been seen. */
2564 int concatenated
= 0;
2566 /* While scanning a comment or a string constant,
2567 this records the line it started on, for error messages. */
2570 /* Record position of last `real' newline. */
2571 U_CHAR
*beg_of_line
;
2573 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2576 do { ip->macro->type = T_MACRO; \
2577 if (ip->free_ptr) free (ip->free_ptr); \
2578 --indepth; } while (0)
2580 /* Reload `rescan's local variables that describe the current
2581 level of the input stack. */
2584 do { ip = &instack[indepth]; \
2586 limit = ip->buf + ip->length; \
2588 check_expand (op, limit - ibp); \
2590 obp = op->bufp; } while (0)
2592 if (no_output
&& instack
[indepth
].fname
!= 0)
2593 skip_if_group (&instack
[indepth
], 1, NULL
);
2600 /* Our caller must always put a null after the end of
2601 the input at each input stack level. */
2611 if (*ibp
== '\n' && !ip
->macro
) {
2612 /* At the top level, always merge lines ending with backslash-newline,
2613 even in middle of identifier. But do not merge lines in a macro,
2614 since backslash might be followed by a newline-space marker. */
2617 --obp
; /* remove backslash from obuf */
2620 /* If ANSI, backslash is just another character outside a string. */
2623 /* Otherwise, backslash suppresses specialness of following char,
2624 so copy it here to prevent the switch from seeing it.
2625 But first get any pending identifier processed. */
2626 if (ident_length
> 0)
2633 if (ident_length
|| ip
->macro
|| traditional
)
2635 while (*ibp
== '\\' && ibp
[1] == '\n') {
2641 /* Treat this %: digraph as if it were #. */
2645 if (assertions_flag
) {
2648 /* Copy #foo (bar lose) without macro expansion. */
2649 obp
[-1] = '#'; /* In case it was '%'. */
2650 SKIP_WHITE_SPACE (ibp
);
2651 while (is_idchar
[*ibp
])
2653 SKIP_WHITE_SPACE (ibp
);
2656 skip_paren_group (ip
);
2657 bcopy ((const PTR
) ibp
, (PTR
) obp
, ip
->bufp
- ibp
);
2658 obp
+= ip
->bufp
- ibp
;
2664 /* If this is expanding a macro definition, don't recognize
2665 preprocessing directives. */
2668 /* If this is expand_into_temp_buffer,
2669 don't recognize them either. Warn about them
2670 only after an actual newline at this level,
2671 not at the beginning of the input level. */
2673 if (ip
->buf
!= beg_of_line
)
2674 warning ("preprocessing directive not recognized within macro arg");
2681 /* # keyword: a # must be first nonblank char on the line */
2682 if (beg_of_line
== 0)
2687 /* Scan from start of line, skipping whitespace, comments
2688 and backslash-newlines, and see if we reach this #.
2689 If not, this # is not special. */
2691 /* If -traditional, require # to be at beginning of line. */
2694 if (is_hor_space
[*bp
])
2696 else if (*bp
== '\\' && bp
[1] == '\n')
2698 else if (*bp
== '/' && bp
[1] == '*') {
2712 #ifdef MULTIBYTE_CHARS
2714 length
= local_mblen (bp
, limit
- bp
);
2722 /* There is no point in trying to deal with C++ // comments here,
2723 because if there is one, then this # must be part of the
2724 comment and we would never reach here. */
2730 while (bp
[1] == '\\' && bp
[2] == '\n')
2734 /* %: appears at start of line; skip past the ':' too. */
2743 /* This # can start a directive. */
2745 --obp
; /* Don't copy the '#' */
2749 if (! handle_directive (ip
, op
)) {
2753 /* Not a known directive: treat it as ordinary text.
2754 IP, OP, IBP, etc. have not been changed. */
2755 if (no_output
&& instack
[indepth
].fname
) {
2756 /* If not generating expanded output,
2757 what we do with ordinary text is skip it.
2758 Discard everything until next # directive. */
2759 skip_if_group (&instack
[indepth
], 1, 0);
2764 *obp
++ = '#'; /* Copy # (even if it was originally %:). */
2765 /* Don't expand an identifier that could be a macro directive.
2766 (Section 3.8.3 of the ANSI C standard) */
2767 SKIP_WHITE_SPACE (ibp
);
2768 if (is_idstart
[*ibp
])
2771 while (is_idchar
[*ibp
])
2779 /* A # directive has been successfully processed. */
2780 /* If not generating expanded output, ignore everything until
2781 next # directive. */
2782 if (no_output
&& instack
[indepth
].fname
)
2783 skip_if_group (&instack
[indepth
], 1, 0);
2789 case '\"': /* skip quoted string */
2791 /* A single quoted string is treated like a double -- some
2792 programs (e.g., troff) are perverse this way */
2794 /* Handle any pending identifier;
2795 but the L in L'...' or L"..." is not an identifier. */
2797 if (! (ident_length
== 1 && hash
== HASHSTEP (0, 'L')))
2799 ident_length
= hash
= 0;
2802 start_line
= ip
->lineno
;
2804 /* Skip ahead to a matching quote. */
2808 if (ip
->macro
!= 0) {
2809 /* try harder: this string crosses a macro expansion boundary.
2810 This can happen naturally if -traditional.
2811 Otherwise, only -D can make a macro with an unmatched quote. */
2817 error_with_line (line_for_error (start_line
),
2818 "unterminated string or character constant");
2819 if (multiline_string_line
) {
2820 error_with_line (multiline_string_line
,
2821 "possible real start of unterminated constant");
2822 multiline_string_line
= 0;
2830 if (warn_white_space
&& ip
->fname
&& is_hor_space
[ibp
[-2]])
2831 warning ("white space at end of line in string");
2834 /* Traditionally, end of line ends a string constant with no error.
2835 So exit the loop and record the new line. */
2841 error_with_line (line_for_error (start_line
),
2842 "unterminated character constant");
2845 if (multiline_string_line
== 0) {
2847 pedwarn_with_line (line_for_error (start_line
),
2848 "string constant runs past end of line");
2849 multiline_string_line
= ip
->lineno
- 1;
2855 /* Backslash newline is replaced by nothing at all, but
2856 keep the line counts correct. But if we are reading
2857 from a macro, keep the backslash newline, since backslash
2858 newlines have already been processed. */
2867 /* ANSI stupidly requires that in \\ the second \
2868 is *not* prevented from combining with a newline. */
2870 while (*ibp
== '\\' && ibp
[1] == '\n') {
2886 #ifdef MULTIBYTE_CHARS
2891 length
= local_mblen (ibp
, limit
- ibp
);
2895 bcopy (ibp
, obp
, length
);
2915 && !(cplusplus_comments
&& *ibp
== '/'))
2921 /* C++ style comment... */
2922 start_line
= ip
->lineno
;
2924 /* Comments are equivalent to spaces. */
2925 if (! put_out_comments
)
2929 U_CHAR
*before_bp
= ibp
;
2931 while (++ibp
< limit
) {
2934 if (put_out_comments
) {
2935 bcopy ((const PTR
) before_bp
, (PTR
) obp
, ibp
- before_bp
);
2936 obp
+= ibp
- before_bp
;
2942 if (ibp
+ 1 < limit
&& ibp
[1] == '\n')
2945 warning ("multiline `//' comment");
2947 /* Copy the newline into the output buffer, in order to
2948 avoid the pain of a #line every time a multiline comment
2950 if (!put_out_comments
)
2958 #ifdef MULTIBYTE_CHARS
2960 length
= local_mblen (ibp
, limit
- ibp
);
2962 ibp
+= (length
- 1);
2970 /* Ordinary C comment. Skip it, optionally copying it to output. */
2972 start_line
= ip
->lineno
;
2974 ++ibp
; /* Skip the star. */
2976 /* If this cpp is for lint, we peek inside the comments: */
2978 const U_CHAR
*argbp
;
2980 const char *lintcmd
=
2981 get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2983 if (lintcmd
!= NULL
) {
2985 check_expand (op
, cmdlen
+ arglen
+ 14);
2987 /* I believe it is always safe to emit this newline: */
2989 bcopy ("#pragma lint ", (char *) obp
, 13);
2991 bcopy (lintcmd
, (char *) obp
, cmdlen
);
2996 bcopy (argbp
, (char *) obp
, arglen
);
3000 /* OK, now bring us back to the state we were in before we entered
3001 this branch. We need #line because the #pragma's newline always
3002 messes up the line count. */
3004 output_line_directive (ip
, op
, 0, same_file
);
3005 check_expand (op
, limit
- ibp
+ 2);
3011 /* Comments are equivalent to spaces.
3012 Note that we already output the slash; we might not want it.
3013 For -traditional, a comment is equivalent to nothing. */
3014 if (! put_out_comments
) {
3024 U_CHAR
*before_bp
= ibp
;
3029 if (ibp
[-2] == '/' && warn_comments
)
3030 warning ("`/*' within comment");
3039 /* Copy the newline into the output buffer, in order to
3040 avoid the pain of a #line every time a multiline comment
3042 if (!put_out_comments
)
3049 error_with_line (line_for_error (start_line
),
3050 "unterminated comment");
3054 #ifdef MULTIBYTE_CHARS
3058 length
= local_mblen (ibp
, limit
- ibp
);
3060 ibp
+= (length
- 1);
3069 if (put_out_comments
) {
3070 bcopy ((const PTR
) before_bp
, (PTR
) obp
, ibp
- before_bp
);
3071 obp
+= ibp
- before_bp
;
3077 if (! is_idchar
['$'])
3080 pedwarn ("`$' in identifier");
3083 case '0': case '1': case '2': case '3': case '4':
3084 case '5': case '6': case '7': case '8': case '9':
3085 /* If digit is not part of identifier, it starts a number,
3086 which means that following letters are not an identifier.
3087 "0x5" does not refer to an identifier "x5".
3088 So copy all alphanumerics that follow without accumulating
3089 as an identifier. Periods also, for sake of "3.e7". */
3091 if (ident_length
== 0) {
3094 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3100 if (!is_idchar
[c
] && c
!= '.') {
3105 /* A sign can be part of a preprocessing number
3106 if it follows an `e' or `p'. */
3107 if (c
== 'e' || c
== 'E' || c
== 'p' || c
== 'P') {
3109 while (ibp
[0] == '\\' && ibp
[1] == '\n') {
3114 if (*ibp
== '+' || *ibp
== '-') {
3116 /* But traditional C does not let the token go past the sign,
3117 and C89 does not allow `p'. */
3118 if (traditional
|| (c89
&& (c
== 'p' || c
== 'P')))
3128 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3129 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3130 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3131 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3133 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3134 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3135 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3136 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3140 /* Compute step of hash function, to avoid a proc call on every token */
3141 hash
= HASHSTEP (hash
, c
);
3145 if (ip
->fname
== 0 && *ibp
== '-') {
3146 /* Newline - inhibits expansion of preceding token.
3147 If expanding a macro arg, we keep the newline -.
3148 In final output, it is deleted.
3149 We recognize Newline - in macro bodies and macro args. */
3150 if (! concatenated
) {
3155 if (!output_marks
) {
3158 /* If expanding a macro arg, keep the newline -. */
3164 /* If reprocessing a macro expansion, newline is a special marker. */
3165 else if (ip
->macro
!= 0) {
3166 /* Newline White is a "funny space" to separate tokens that are
3167 supposed to be separate but without space between.
3168 Here White means any whitespace character.
3169 Newline - marks a recursive macro use that is not
3170 supposed to be expandable. */
3172 if (is_space
[*ibp
]) {
3173 /* Newline Space does not prevent expansion of preceding token
3174 so expand the preceding token and then come back. */
3175 if (ident_length
> 0)
3178 /* If generating final output, newline space makes a space. */
3179 if (!output_marks
) {
3181 /* And Newline Newline makes a newline, so count it. */
3182 if (obp
[-1] == '\n')
3185 /* If expanding a macro arg, keep the newline space.
3186 If the arg gets stringified, newline space makes nothing. */
3189 } else abort (); /* Newline followed by something random? */
3193 /* If there is a pending identifier, handle it and come back here. */
3194 if (ident_length
> 0)
3199 /* Update the line counts and output a #line if necessary. */
3202 if (ip
->lineno
!= op
->lineno
) {
3204 output_line_directive (ip
, op
, 1, same_file
);
3205 check_expand (op
, limit
- ibp
);
3210 /* Come here either after (1) a null character that is part of the input
3211 or (2) at the end of the input, because there is a null there. */
3214 /* Our input really contains a null character. */
3218 /* At end of a macro-expansion level, pop it and read next level. */
3219 if (ip
->macro
!= 0) {
3222 /* If traditional, and we have an identifier that ends here,
3223 process it now, so we get the right error for recursion. */
3224 if (traditional
&& ident_length
3225 && ! is_idchar
[*instack
[indepth
- 1].bufp
]) {
3234 /* If we don't have a pending identifier,
3235 return at end of input. */
3236 if (ident_length
== 0) {
3244 /* If we do have a pending identifier, just consider this null
3245 a special character and arrange to dispatch on it again.
3246 The second time, IDENT_LENGTH will be zero so we will return. */
3252 /* Handle the case of a character such as /, ', " or null
3253 seen following an identifier. Back over it so that
3254 after the identifier is processed the special char
3255 will be dispatched on again. */
3265 if (ident_length
> 0) {
3266 register HASHNODE
*hp
;
3268 /* We have just seen an identifier end. If it's a macro, expand it.
3270 IDENT_LENGTH is the length of the identifier
3271 and HASH is its hash code.
3273 The identifier has already been copied to the output,
3274 so if it is a macro we must remove it.
3276 If REDO_CHAR is 0, the char that terminated the identifier
3277 has been skipped in the output and the input.
3278 OBP-IDENT_LENGTH-1 points to the identifier.
3279 If the identifier is a macro, we must back over the terminator.
3281 If REDO_CHAR is 1, the terminating char has already been
3282 backed over. OBP-IDENT_LENGTH points to the identifier. */
3284 if (!pcp_outfile
|| pcp_inside_if
) {
3285 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
3288 if (hp
->length
== ident_length
) {
3289 int obufp_before_macroname
;
3290 int op_lineno_before_macroname
;
3291 register int i
= ident_length
;
3292 register U_CHAR
*p
= hp
->name
;
3293 register U_CHAR
*q
= obp
- i
;
3299 do { /* All this to avoid a strncmp () */
3304 /* We found a use of a macro name.
3305 see if the context shows it is a macro call. */
3307 /* Back up over terminating character if not already done. */
3313 /* Save this as a displacement from the beginning of the output
3314 buffer. We can not save this as a position in the output
3315 buffer, because it may get realloc'ed by RECACHE. */
3316 obufp_before_macroname
= (obp
- op
->buf
) - ident_length
;
3317 op_lineno_before_macroname
= op
->lineno
;
3319 if (hp
->type
== T_PCSTRING
) {
3320 pcstring_used (hp
); /* Mark the definition of this key
3321 as needed, ensuring that it
3323 break; /* Exit loop, since the key cannot have a
3324 definition any longer. */
3327 /* Record whether the macro is disabled. */
3328 disabled
= hp
->type
== T_DISABLED
;
3330 /* This looks like a macro ref, but if the macro was disabled,
3331 just copy its name and put in a marker if requested. */
3335 /* This error check caught useful cases such as
3336 #define foo(x,y) bar (x (y,0), y)
3339 error ("recursive use of macro `%s'", hp
->name
);
3344 check_expand (op
, limit
- ibp
+ 2);
3352 /* If macro wants an arglist, verify that a '(' follows.
3353 first skip all whitespace, copying it to the output
3354 after the macro name. Then, if there is no '(',
3355 decide this is not a macro call and leave things that way. */
3356 if ((hp
->type
== T_MACRO
|| hp
->type
== T_DISABLED
)
3357 && hp
->value
.defn
->nargs
>= 0)
3359 U_CHAR
*old_ibp
= ibp
;
3360 U_CHAR
*old_obp
= obp
;
3361 int old_iln
= ip
->lineno
;
3362 int old_oln
= op
->lineno
;
3365 /* Scan forward over whitespace, copying it to the output. */
3366 if (ibp
== limit
&& ip
->macro
!= 0) {
3371 old_iln
= ip
->lineno
;
3372 old_oln
= op
->lineno
;
3374 else if (is_space
[*ibp
]) {
3376 if (ibp
[-1] == '\n') {
3377 if (ip
->macro
== 0) {
3378 /* Newline in a file. Count it. */
3381 } else if (!output_marks
) {
3382 /* A newline mark, and we don't want marks
3383 in the output. If it is newline-hyphen,
3384 discard it entirely. Otherwise, it is
3385 newline-whitechar, so keep the whitechar. */
3395 /* A newline mark; copy both chars to the output. */
3402 else if (*ibp
== '/') {
3403 /* If a comment, copy it unchanged or discard it. */
3405 newline_fix (ibp
+ 1);
3406 if (ibp
[1] == '*') {
3407 if (put_out_comments
) {
3410 } else if (! traditional
) {
3413 for (ibp
+= 2; ibp
< limit
; ibp
++) {
3414 /* We need not worry about newline-marks,
3415 since they are never found in comments. */
3416 if (ibp
[0] == '*') {
3418 newline_fix (ibp
+ 1);
3419 if (ibp
[1] == '/') {
3421 if (put_out_comments
) {
3428 else if (*ibp
== '\n') {
3429 /* Newline in a file. Count it. */
3435 #ifdef MULTIBYTE_CHARS
3437 length
= local_mblen (ibp
, limit
- ibp
);
3440 if (put_out_comments
)
3442 bcopy (ibp
, obp
, length
- 1);
3445 ibp
+= (length
- 1);
3449 if (put_out_comments
)
3452 } else if (ibp
[1] == '/' && cplusplus_comments
) {
3453 if (put_out_comments
) {
3456 } else if (! traditional
) {
3459 for (ibp
+= 2; ; ibp
++)
3463 if (*ibp
== '\\' && ibp
[1] == '\n')
3465 if (put_out_comments
)
3470 #ifdef MULTIBYTE_CHARS
3472 length
= local_mblen (ibp
, limit
- ibp
);
3475 if (put_out_comments
)
3477 bcopy (ibp
, obp
, length
- 1);
3480 ibp
+= (length
- 1);
3484 if (put_out_comments
)
3490 else if (ibp
[0] == '\\' && ibp
[1] == '\n') {
3497 /* It isn't a macro call.
3498 Put back the space that we just skipped. */
3501 ip
->lineno
= old_iln
;
3502 op
->lineno
= old_oln
;
3503 /* Exit the for loop. */
3508 /* This is now known to be a macro call.
3509 Discard the macro name from the output,
3510 along with any following whitespace just copied,
3511 but preserve newlines if not outputting marks since this
3512 is more likely to do the right thing with line numbers. */
3513 obp
= op
->buf
+ obufp_before_macroname
;
3515 op
->lineno
= op_lineno_before_macroname
;
3517 int newlines
= op
->lineno
- op_lineno_before_macroname
;
3518 while (0 < newlines
--)
3522 /* Prevent accidental token-pasting with a character
3523 before the macro call. */
3524 if (!traditional
&& obp
!= op
->buf
) {
3526 case '!': case '%': case '&': case '*':
3527 case '+': case '-': case '.': case '/':
3528 case ':': case '<': case '=': case '>':
3530 /* If we are expanding a macro arg, make a newline marker
3531 to separate the tokens. If we are making real output,
3532 a plain space will do. */
3539 /* Expand the macro, reading arguments as needed,
3540 and push the expansion on the input stack. */
3543 macroexpand (hp
, op
);
3545 /* Reexamine input stack, since macroexpand has pushed
3546 a new level on it. */
3553 } /* End hash-table-search loop */
3555 ident_length
= hash
= 0; /* Stop collecting identifier */
3558 } /* End if (ident_length > 0) */
3560 } /* End per-char loop */
3562 /* Come here to return -- but first give an error message
3563 if there was an unterminated successful conditional. */
3565 if (if_stack
!= ip
->if_stack
)
3569 switch (if_stack
->type
)
3590 error_with_line (line_for_error (if_stack
->lineno
),
3591 "unterminated `#%s' conditional", str
);
3593 if_stack
= ip
->if_stack
;
3597 * Rescan a string into a temporary buffer and return the result
3598 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3600 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3601 * and insert such markers when appropriate. See `rescan' for details.
3602 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3603 * before substitution; it is 0 for other uses.
3606 expand_to_temp_buffer (buf
, limit
, output_marks
, assertions
)
3608 const U_CHAR
*limit
;
3609 int output_marks
, assertions
;
3611 register FILE_BUF
*ip
;
3613 int length
= limit
- buf
;
3615 int odepth
= indepth
;
3616 int save_assertions_flag
= assertions_flag
;
3618 assertions_flag
= assertions
;
3623 /* Set up the input on the input stack. */
3625 buf1
= (U_CHAR
*) alloca (length
+ 1);
3627 register const U_CHAR
*p1
= buf
;
3628 register U_CHAR
*p2
= buf1
;
3635 /* Set up to receive the output. */
3637 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
3638 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
3639 obuf
.nominal_fname
= 0;
3646 obuf
.system_header_p
= 0;
3648 CHECK_DEPTH ({return obuf
;});
3652 ip
= &instack
[indepth
];
3654 ip
->nominal_fname
= 0;
3655 ip
->nominal_fname_len
= 0;
3657 ip
->system_header_p
= 0;
3660 ip
->length
= length
;
3661 ip
->buf
= ip
->bufp
= buf1
;
3662 ip
->if_stack
= if_stack
;
3664 ip
->lineno
= obuf
.lineno
= 1;
3666 /* Scan the input, create the output. */
3667 rescan (&obuf
, output_marks
);
3669 /* Pop input stack to original state. */
3672 if (indepth
!= odepth
)
3675 assertions_flag
= save_assertions_flag
;
3680 * Process a # directive. Expects IP->bufp to point after the '#', as in
3681 * `#define foo bar'. Passes to the directive handler
3682 * (do_define, do_include, etc.): the addresses of the 1st and
3683 * last chars of the directive (starting immediately after the #
3684 * keyword), plus op and the keyword table pointer. If the directive
3685 * contains comments it is copied into a temporary buffer sans comments
3686 * and the temporary buffer is passed to the directive handler instead.
3687 * Likewise for backslash-newlines.
3689 * Returns nonzero if this was a known # directive.
3690 * Otherwise, returns zero, without advancing the input pointer.
3694 handle_directive (ip
, op
)
3697 register U_CHAR
*bp
, *cp
;
3698 register struct directive
*kt
;
3699 register int ident_length
;
3702 /* Nonzero means we must copy the entire directive
3703 to get rid of comments or backslash-newlines. */
3704 int copy_directive
= 0;
3706 U_CHAR
*ident
, *after_ident
;
3710 /* Record where the directive started. do_xifdef needs this. */
3711 directive_start
= bp
- 1;
3713 ignore_escape_flag
= 1;
3715 /* Skip whitespace and \-newline. */
3717 if (is_hor_space
[*bp
]) {
3718 if (*bp
!= ' ' && *bp
!= '\t' && pedantic
)
3719 pedwarn_strange_white_space (*bp
);
3721 } else if (*bp
== '/') {
3723 newline_fix (bp
+ 1);
3724 if (! (bp
[1] == '*' || (cplusplus_comments
&& bp
[1] == '/')))
3727 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3729 } else if (*bp
== '\\' && bp
[1] == '\n') {
3730 bp
+= 2; ip
->lineno
++;
3734 /* Now find end of directive name.
3735 If we encounter a backslash-newline, exchange it with any following
3736 symbol-constituents so that we end up with a contiguous name. */
3744 name_newline_fix (cp
);
3750 ident_length
= cp
- bp
;
3754 /* A line of just `#' becomes blank. */
3756 if (ident_length
== 0 && *after_ident
== '\n') {
3757 ip
->bufp
= after_ident
;
3761 if (ident_length
== 0 || !is_idstart
[*ident
]) {
3763 while (is_idchar
[*p
]) {
3764 if (*p
< '0' || *p
> '9')
3768 /* Handle # followed by a line number. */
3769 if (p
!= ident
&& !is_idchar
[*p
]) {
3770 static struct directive line_directive_table
[] = {
3771 { 4, do_line
, "line", T_LINE
},
3774 pedwarn ("`#' followed by integer");
3775 after_ident
= ident
;
3776 kt
= line_directive_table
;
3777 ignore_escape_flag
= 0;
3781 /* Avoid error for `###' and similar cases unless -pedantic. */
3783 while (*p
== '#' || is_hor_space
[*p
]) p
++;
3785 if (pedantic
&& !lang_asm
)
3786 warning ("invalid preprocessing directive");
3792 error ("invalid preprocessing directive name");
3798 * Decode the keyword and call the appropriate expansion
3799 * routine, after moving the input pointer up to the next line.
3801 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
3802 if (kt
->length
== ident_length
&& !bcmp (kt
->name
, ident
, ident_length
)) {
3803 register U_CHAR
*buf
;
3804 register U_CHAR
*limit
;
3807 int *already_output
;
3809 /* Nonzero means do not delete comments within the directive.
3810 #define needs this when -traditional. */
3815 limit
= ip
->buf
+ ip
->length
;
3818 keep_comments
= traditional
&& kt
->type
== T_DEFINE
;
3819 /* #import is defined only in Objective C, or when on the NeXT. */
3820 if (kt
->type
== T_IMPORT
3821 && !(objc
|| lookup ((const U_CHAR
*) "__NeXT__", -1, -1)))
3824 /* Find the end of this directive (first newline not backslashed
3825 and not in a string or comment).
3826 Set COPY_DIRECTIVE if the directive must be copied
3827 (it contains a backslash-newline or a comment). */
3829 buf
= bp
= after_ident
;
3830 while (bp
< limit
) {
3831 register U_CHAR c
= *bp
++;
3838 } else if (traditional
&& bp
< limit
)
3843 /* "..." is special for #include. */
3844 if (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)) {
3845 while (bp
< limit
&& *bp
!= '\n') {
3850 if (*bp
== '\\' && bp
[1] == '\n') {
3861 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_directive
, &unterminated
);
3862 /* Don't bother calling the directive if we already got an error
3863 message due to unterminated string. Skip everything and pretend
3864 we called the directive. */
3867 /* Traditional preprocessing permits unterminated strings. */
3876 /* <...> is special for #include. */
3878 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3880 while (bp
< limit
&& *bp
!= '>' && *bp
!= '\n') {
3881 if (*bp
== '\\' && bp
[1] == '\n') {
3894 || (cplusplus_comments
&& *bp
== '/')) {
3895 U_CHAR
*obp
= bp
- 1;
3897 skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
3899 /* No need to copy the directive because of a comment at the end;
3900 just don't include the comment in the directive. */
3901 if (!put_out_comments
) {
3903 for (p
= bp
; *p
== ' ' || *p
== '\t'; p
++)
3910 /* Don't remove the comments if -traditional. */
3911 if (! keep_comments
)
3920 pedwarn_strange_white_space (c
);
3924 --bp
; /* Point to the newline */
3932 resume_p
= ip
->bufp
;
3933 /* BP is the end of the directive.
3934 RESUME_P is the next interesting data after the directive.
3935 A comment may come between. */
3937 /* If a directive should be copied through, and -C was given,
3938 pass it through before removing comments. */
3939 if (!no_output
&& put_out_comments
3940 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
3941 ? dump_macros
== dump_definitions
3942 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
3943 : kt
->type
== T_PRAGMA
)) {
3946 /* Output directive name. */
3947 check_expand (op
, kt
->length
+ 2);
3948 /* Make sure # is at the start of a line */
3949 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n') {
3954 bcopy (kt
->name
, op
->bufp
, kt
->length
);
3955 op
->bufp
+= kt
->length
;
3957 /* Output arguments. */
3959 check_expand (op
, len
);
3960 bcopy (buf
, (char *) op
->bufp
, len
);
3962 /* Take account of any (escaped) newlines just output. */
3964 if (buf
[len
] == '\n')
3967 already_output
= &junk
;
3968 } /* Don't we need a newline or #line? */
3970 if (copy_directive
) {
3971 register U_CHAR
*xp
= buf
;
3972 /* Need to copy entire directive into temp buffer before dispatching */
3974 /* room for directive plus some slop */
3975 cp
= (U_CHAR
*) alloca (2 * (bp
- buf
) + 5);
3978 /* Copy to the new buffer, deleting comments
3979 and backslash-newlines (and whitespace surrounding the latter
3980 if outside of char and string constants). */
3983 register U_CHAR c
= *xp
++;
3988 abort (); /* A bare newline should never part of the line. */
3991 /* <...> is special for #include. */
3993 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
))
3995 while (xp
< bp
&& c
!= '>') {
3997 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
4008 if (cp
!= buf
&& is_hor_space
[cp
[-1]]) {
4009 while (cp
- 1 != buf
&& is_hor_space
[cp
[-2]])
4011 SKIP_WHITE_SPACE (xp
);
4012 } else if (is_hor_space
[*xp
]) {
4014 SKIP_WHITE_SPACE (xp
);
4016 } else if (traditional
&& xp
< bp
) {
4024 int backslash_newlines_p
;
4026 register const U_CHAR
*bp1
4027 = skip_quoted_string (xp
- 1, bp
, ip
->lineno
,
4028 NULL_PTR
, &backslash_newlines_p
,
4030 if (backslash_newlines_p
)
4033 /* With something like:
4038 we should still remove the backslash-newline
4039 pair as part of phase two. */
4040 if (xp
[0] == '\\' && xp
[1] == '\n')
4046 /* This is the same as the loop above, but taking
4047 advantage of the fact that we know there are no
4048 backslash-newline pairs. */
4056 || (cplusplus_comments
&& *xp
== '/')) {
4058 /* If we already copied the directive through,
4059 already_output != 0 prevents outputting comment now. */
4060 skip_to_end_of_comment (ip
, already_output
, 0);
4062 while (xp
!= ip
->bufp
)
4064 /* Delete or replace the slash. */
4065 else if (traditional
)
4074 /* Null-terminate the copy. */
4080 ip
->bufp
= resume_p
;
4082 /* Some directives should be written out for cc1 to process,
4083 just as if they were not defined. And sometimes we're copying
4084 directives through. */
4086 if (!no_output
&& already_output
== 0
4087 && ((kt
->type
== T_DEFINE
|| kt
->type
== T_UNDEF
)
4088 ? (int) dump_names
<= (int) dump_macros
4089 : IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
) ? dump_includes
4090 : kt
->type
== T_PRAGMA
)) {
4093 /* Output directive name. */
4094 check_expand (op
, kt
->length
+ 1);
4096 bcopy (kt
->name
, (char *) op
->bufp
, kt
->length
);
4097 op
->bufp
+= kt
->length
;
4099 if (kt
->type
== T_DEFINE
&& dump_macros
== dump_names
) {
4100 /* Output `#define name' only. */
4103 SKIP_WHITE_SPACE (xp
);
4105 while (is_idchar
[*xp
]) xp
++;
4107 check_expand (op
, len
+ 1);
4109 bcopy (yp
, (char *) op
->bufp
, len
);
4111 /* Output entire directive. */
4113 check_expand (op
, len
);
4114 bcopy (buf
, (char *) op
->bufp
, len
);
4119 /* Call the appropriate directive handler. buf now points to
4120 either the appropriate place in the input buffer, or to
4121 the temp buffer if it was necessary to make one. cp
4122 points to the first char after the contents of the (possibly
4123 copied) directive, in either case. */
4124 (*kt
->func
) (buf
, cp
, op
, kt
);
4125 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
4131 /* It is deliberate that we don't warn about undefined directives.
4132 That is the responsibility of cc1. */
4139 static struct tm tmbuf
;
4140 if (! tmbuf
.tm_mday
) {
4141 time_t t
= time ((time_t *) 0);
4142 struct tm
*tm
= localtime (&t
);
4146 /* Use 0000-01-01 00:00:00 if local time is not available. */
4147 tmbuf
.tm_year
= -1900;
4154 static const char * const monthnames
[] = {
4155 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4156 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4160 * expand things like __FILE__. Place the expansion into the output
4161 * buffer *without* rescanning.
4165 special_symbol (hp
, op
)
4172 FILE_BUF
*ip
= NULL
;
4175 int paren
= 0; /* For special `defined' keyword */
4177 if (pcp_outfile
&& pcp_inside_if
4178 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
4179 error ("Predefined macro `%s' used inside `#if' during precompilation",
4182 for (i
= indepth
; i
>= 0; i
--)
4183 if (instack
[i
].fname
!= NULL
) {
4188 error ("cccp error: not in any file?!");
4189 return; /* the show must go on */
4196 FILE_BUF
*p
= hp
->type
== T_FILE
? ip
: &instack
[0];
4197 const char *string
= p
->nominal_fname
;
4201 size_t string_len
= p
->nominal_fname_len
;
4202 char *newbuf
= (char *) alloca (3 + 4 * string_len
);
4203 quote_string (newbuf
, string
, string_len
);
4212 case T_INCLUDE_LEVEL
:
4214 /* Eight bytes ought to be more than enough */
4215 char *newbuf
= (char *) alloca (8);
4217 for (i
= indepth
; i
>= 0; i
--)
4218 if (instack
[i
].fname
!= NULL
)
4220 sprintf (newbuf
, "%d", true_indepth
- 1);
4227 char *newbuf
= (char *) alloca (3 + strlen (version_string
));
4228 sprintf (newbuf
, "\"%s\"", version_string
);
4233 #ifndef NO_BUILTIN_SIZE_TYPE
4239 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4240 case T_PTRDIFF_TYPE
:
4249 case T_USER_LABEL_PREFIX_TYPE
:
4250 buf
= user_label_prefix
;
4253 case T_REGISTER_PREFIX_TYPE
:
4254 buf
= REGISTER_PREFIX
;
4257 case T_IMMEDIATE_PREFIX_TYPE
:
4258 buf
= IMMEDIATE_PREFIX
;
4262 buf
= hp
->value
.cpval
;
4263 #ifdef STDC_0_IN_SYSTEM_HEADERS
4264 if (ip
->system_header_p
4265 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
4266 && !lookup ((const U_CHAR
*) "__STRICT_ANSI__", -1, -1))
4269 if (pcp_inside_if
&& pcp_outfile
)
4270 /* Output a precondition for this macro use */
4271 fprintf (pcp_outfile
, "#define %s %s\n", hp
->name
, buf
);
4276 char *newbuf
= (char *) alloca (10);
4277 sprintf (newbuf
, "%d", ip
->lineno
);
4285 char *newbuf
= (char *) alloca (20);
4286 timebuf
= timestamp ();
4287 if (hp
->type
== T_DATE
)
4288 sprintf (newbuf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
4289 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
4291 sprintf (newbuf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
,
4292 timebuf
->tm_min
, timebuf
->tm_sec
);
4297 case T_SPEC_DEFINED
:
4298 buf
= " 0 "; /* Assume symbol is not defined */
4299 ip
= &instack
[indepth
];
4300 SKIP_WHITE_SPACE (ip
->bufp
);
4301 if (*ip
->bufp
== '(') {
4303 ip
->bufp
++; /* Skip over the paren */
4304 SKIP_WHITE_SPACE (ip
->bufp
);
4307 if (!is_idstart
[*ip
->bufp
])
4309 if (ip
->bufp
[0] == 'L' && (ip
->bufp
[1] == '\'' || ip
->bufp
[1] == '"'))
4311 if ((hp
= lookup (ip
->bufp
, -1, -1))) {
4312 if (pcp_outfile
&& pcp_inside_if
4313 && (hp
->type
== T_CONST
4314 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
4315 /* Output a precondition for this macro use. */
4316 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
4317 if (hp
->type
== T_POISON
) {
4318 error("attempt to use poisoned `%s'.", hp
->name
);
4325 if (pcp_outfile
&& pcp_inside_if
) {
4326 /* Output a precondition for this macro use */
4327 U_CHAR
*cp
= ip
->bufp
;
4328 fprintf (pcp_outfile
, "#undef ");
4329 while (is_idchar
[*cp
]) /* Ick! */
4330 fputc (*cp
++, pcp_outfile
);
4331 putc ('\n', pcp_outfile
);
4333 while (is_idchar
[*ip
->bufp
])
4335 SKIP_WHITE_SPACE (ip
->bufp
);
4337 if (*ip
->bufp
!= ')')
4344 error("attempt to use poisoned `%s'.", hp
->name
);
4345 buf
= " 0 "; /* Consider poisoned symbol to not be defined */
4350 error ("`defined' without an identifier");
4354 error ("cccp error: invalid special hash type"); /* time for gdb */
4358 check_expand (op
, len
);
4359 bcopy (buf
, (char *) op
->bufp
, len
);
4366 /* Routines to handle #directives */
4368 /* Handle #include and #import.
4369 This function expects to see "fname" or <fname> on the input. */
4372 do_include (buf
, limit
, op
, keyword
)
4373 U_CHAR
*buf
, *limit
;
4375 struct directive
*keyword
;
4377 const U_CHAR
*importing
=
4378 keyword
->type
== T_IMPORT
? (const U_CHAR
*) "" : (const U_CHAR
*) 0;
4379 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
4380 static int import_warning
= 0;
4381 char *fname
; /* Dynamically allocated fname buffer */
4384 char *fbeg
, *fend
; /* Beginning and end of fname */
4387 struct file_name_list
*search_start
= include
; /* Chain of dirs to search */
4388 struct file_name_list
*dsp
; /* First in chain, if #include "..." */
4389 struct file_name_list
*searchptr
= 0;
4392 int f
= -3; /* file number */
4393 struct include_file
*inc
= 0;
4395 int retried
= 0; /* Have already tried macro
4396 expanding the include line*/
4397 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
4399 int vaxc_include
= 0; /* 1 for token without punctuation */
4403 const char *pcfbuflimit
;
4406 if (pedantic
&& !instack
[indepth
].system_header_p
)
4409 pedwarn ("ANSI C does not allow `#import'");
4411 pedwarn ("ANSI C does not allow `#include_next'");
4414 if (importing
&& warn_import
&& !inhibit_warnings
4415 && !instack
[indepth
].system_header_p
&& !import_warning
) {
4417 warning ("using `#import' is not recommended");
4418 notice ("The fact that a certain header file need not be processed more than once\n\
4419 should be indicated in the header file, not where it is used.\n\
4420 The best way to do this is with a conditional of this form:\n\
4422 #ifndef _FOO_H_INCLUDED\n\
4423 #define _FOO_H_INCLUDED\n\
4424 ... <real contents of file> ...\n\
4425 #endif /* Not _FOO_H_INCLUDED */\n\
4427 Then users can use `#include' any number of times.\n\
4428 GNU C automatically avoids processing the file more than once\n\
4429 when it is equipped with such a conditional.\n");
4435 SKIP_WHITE_SPACE (fin
);
4436 /* Discard trailing whitespace so we can easily see
4437 if we have parsed all the significant chars we were given. */
4438 while (limit
!= fin
&& is_hor_space
[limit
[-1]]) limit
--;
4439 fbeg
= fend
= (char *) alloca (limit
- fin
);
4445 /* Copy the operand text, concatenating the strings. */
4450 goto invalid_include_file_name
;
4458 /* If not at the end, there had better be another string. */
4459 /* Skip just horiz space, and don't go past limit. */
4460 while (fin
!= limit
&& is_hor_space
[*fin
]) fin
++;
4461 if (fin
!= limit
&& *fin
== '\"')
4468 /* We have "filename". Figure out directory this source
4469 file is coming from and put it on the front of the list. */
4471 /* If -I- was specified, don't search current dir, only spec'd ones. */
4472 if (ignore_srcdir
) break;
4474 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4478 if ((fp
->nominal_fname
) != NULL
) {
4480 /* Found a named file. Figure out dir of the file,
4481 and put it in front of the search list. */
4482 dsp
= ((struct file_name_list
*)
4483 alloca (sizeof (struct file_name_list
)
4484 + fp
->nominal_fname_len
));
4485 strcpy (dsp
->fname
, fp
->nominal_fname
);
4486 simplify_filename (dsp
->fname
);
4487 nam
= base_name (dsp
->fname
);
4490 /* for hack_vms_include_specification(), a local
4491 dir specification must start with "./" on VMS. */
4492 if (nam
== dsp
->fname
)
4499 /* But for efficiency's sake, do not insert the dir
4500 if it matches the search list's first dir. */
4501 dsp
->next
= search_start
;
4502 if (!search_start
|| strcmp (dsp
->fname
, search_start
->fname
)) {
4504 n
= nam
- dsp
->fname
;
4505 if (n
+ INCLUDE_LEN_FUDGE
> max_include_len
)
4506 max_include_len
= n
+ INCLUDE_LEN_FUDGE
;
4508 dsp
[0].got_name_map
= 0;
4516 while (fin
!= limit
&& *fin
!= '>')
4518 if (*fin
== '>' && fin
+ 1 == limit
) {
4520 /* If -I-, start with the first -I dir after the -I-. */
4521 search_start
= first_bracket_include
;
4529 * Support '#include xyz' like VAX-C to allow for easy use of all the
4530 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4531 * code from case '<' is repeated here) and generates a warning.
4532 * (Note: macro expansion of `xyz' takes precedence.)
4534 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4535 the pre-decrement outside of the macro. */
4536 if (retried
&& (--fin
, ISALPHA(*(U_CHAR
*) (fin
)))) {
4537 while (fin
!= limit
&& (!ISSPACE(*fin
)))
4539 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4543 /* If -I-, start with the first -I dir after the -I-. */
4544 search_start
= first_bracket_include
;
4552 /* Expand buffer and then remove any newline markers.
4553 We can't just tell expand_to_temp_buffer to omit the markers,
4554 since it would put extra spaces in include file names. */
4556 int errors_before_expansion
= errors
;
4557 FILE_BUF trybuf
= expand_to_temp_buffer (buf
, limit
, 1, 0);
4558 if (errors
!= errors_before_expansion
) {
4560 goto invalid_include_file_name
;
4563 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
4565 while (src
!= trybuf
.bufp
) {
4566 switch ((*limit
++ = *src
++)) {
4575 const U_CHAR
*src1
= skip_quoted_string (src
- 1, trybuf
.bufp
, 0,
4576 NULL_PTR
, NULL_PTR
, NULL_PTR
);
4589 invalid_include_file_name
:
4590 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
4594 /* For #include_next, skip in the search path
4595 past the dir in which the containing file was found. */
4598 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
4599 if (fp
->fname
!= NULL
) {
4600 /* fp->dir is null if the containing file was specified
4601 with an absolute file name. In that case, don't skip anything. */
4603 search_start
= fp
->dir
->next
;
4609 flen
= simplify_filename (fbeg
);
4613 error ("empty file name in `#%s'", keyword
->name
);
4617 /* Allocate this permanently, because it gets stored in the definitions
4619 fname
= xmalloc (max_include_len
+ flen
+ 1);
4620 /* + 1 above for terminating null. */
4622 system_include_depth
+= angle_brackets
;
4624 /* If specified file name is absolute, just open it. */
4626 if (absolute_filename (fbeg
)) {
4627 strcpy (fname
, fbeg
);
4628 f
= open_include_file (fname
, NULL_PTR
, importing
, &inc
);
4632 struct bypass_dir
*next
;
4634 struct file_name_list
*searchptr
;
4635 } **bypass_slot
= 0;
4637 /* Search directory path, trying to open the file.
4638 Copy each filename tried into FNAME. */
4640 for (searchptr
= search_start
; searchptr
; searchptr
= searchptr
->next
) {
4642 if (searchptr
== first_bracket_include
) {
4643 /* Go to bypass directory if we know we've seen this file before. */
4644 static struct bypass_dir
*bypass_hashtab
[INCLUDE_HASHSIZE
];
4645 struct bypass_dir
*p
;
4646 bypass_slot
= &bypass_hashtab
[hashf ((U_CHAR
*) fbeg
, flen
,
4648 for (p
= *bypass_slot
; p
; p
= p
->next
)
4649 if (!strcmp (fbeg
, p
->fname
)) {
4650 searchptr
= p
->searchptr
;
4657 /* Change this 1/2 Unix 1/2 VMS file specification into a
4658 full VMS file specification */
4659 if (searchptr
->fname
[0])
4661 strcpy (fname
, searchptr
->fname
);
4662 if (fname
[strlen (fname
) - 1] == ':')
4665 slashp
= strchr (fbeg
, '/');
4667 /* start at root-dir of logical device if no path given. */
4669 strcat (fname
, "[000000]");
4671 strcat (fname
, fbeg
);
4673 /* Fix up the filename */
4674 hack_vms_include_specification (fname
, vaxc_include
);
4678 /* This is a normal VMS filespec, so use it unchanged. */
4679 strcpy (fname
, fbeg
);
4680 /* if it's '#include filename', add the missing .h */
4681 if (vaxc_include
&& index(fname
,'.')==NULL
)
4682 strcat (fname
, ".h");
4685 strcpy (fname
, searchptr
->fname
);
4686 strcat (fname
, fbeg
);
4688 f
= open_include_file (fname
, searchptr
, importing
, &inc
);
4690 if (bypass_slot
&& searchptr
!= first_bracket_include
) {
4691 /* This is the first time we found this include file,
4692 and we found it after first_bracket_include.
4693 Record its location so that we can bypass to here next time. */
4694 struct bypass_dir
*p
4695 = (struct bypass_dir
*) xmalloc (sizeof (struct bypass_dir
));
4696 p
->next
= *bypass_slot
;
4697 p
->fname
= fname
+ strlen (searchptr
->fname
);
4698 p
->searchptr
= searchptr
;
4704 /* Our VMS hacks can produce invalid filespecs, so don't worry
4705 about errors other than EACCES. */
4706 if (errno
== EACCES
)
4709 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
4719 /* The file was already included. */
4721 /* If generating dependencies and -MG was specified, we assume missing
4722 files are leaf files, living in the same directory as the source file
4723 or other similar place; these missing files may be generated from
4724 other files and may not exist yet (eg: y.tab.h). */
4725 } else if (print_deps_missing_files
4726 && (system_include_depth
!= 0) < print_deps
)
4728 /* If it was requested as a system header file,
4729 then assume it belongs in the first place to look for such. */
4733 char *p
= (char *) alloca (strlen (search_start
->fname
)
4734 + strlen (fbeg
) + 1);
4735 strcpy (p
, search_start
->fname
);
4737 deps_output (p
, ' ');
4742 /* Otherwise, omit the directory, as if the file existed
4743 in the directory with the source. */
4744 deps_output (fbeg
, ' ');
4747 /* If -M was specified, and this header file won't be added to the
4748 dependency list, then don't count this as an error, because we can
4749 still produce correct output. Otherwise, we can't produce correct
4750 output, because there may be dependencies we need inside the missing
4751 file, and we don't know what directory this missing file exists in. */
4752 else if (0 < print_deps
&& print_deps
<= (system_include_depth
!= 0))
4753 warning ("No include path in which to find %s", fbeg
);
4755 error_from_errno (fbeg
);
4757 error ("No include path in which to find %s", fbeg
);
4761 /* Actually process the file. */
4763 pcftry
= (char *) alloca (strlen (fname
) + 30);
4770 sprintf (pcftry
, "%s%d", fname
, pcfnum
++);
4772 pcf
= open (pcftry
, O_RDONLY
, 0666);
4777 if (fstat (pcf
, &s
) != 0)
4778 pfatal_with_name (pcftry
);
4779 if (! INO_T_EQ (inc
->st
.st_ino
, s
.st_ino
)
4780 || inc
->st
.st_dev
!= s
.st_dev
)
4782 pcfbuf
= check_precompiled (pcf
, &s
, fname
, &pcfbuflimit
);
4783 /* Don't need it any more. */
4788 /* Don't need it at all. */
4793 } while (pcf
!= -1 && !pcfbuf
);
4796 /* Actually process the file */
4798 pcfname
= xstrdup (pcftry
);
4799 pcfinclude ((U_CHAR
*) pcfbuf
, (U_CHAR
*) fname
, op
);
4802 finclude (f
, inc
, op
, is_system_include (fname
), searchptr
);
4805 system_include_depth
-= angle_brackets
;
4810 /* Return nonzero if the given FILENAME is an absolute pathname which
4811 designates a file within one of the known "system" include file
4812 directories. We assume here that if the given FILENAME looks like
4813 it is the name of a file which resides either directly in a "system"
4814 include file directory, or within any subdirectory thereof, then the
4815 given file must be a "system" include file. This function tells us
4816 if we should suppress pedantic errors/warnings for the given FILENAME.
4818 The value is 2 if the file is a C-language system header file
4819 for which C++ should (on most systems) assume `extern "C"'. */
4822 is_system_include (filename
)
4823 register const char *filename
;
4825 struct file_name_list
*searchptr
;
4827 for (searchptr
= first_system_include
; searchptr
;
4828 searchptr
= searchptr
->next
)
4829 if (! strncmp (searchptr
->fname
, filename
, strlen (searchptr
->fname
)))
4830 return searchptr
->c_system_include_path
+ 1;
4834 /* Yield the non-directory suffix of a file name. */
4840 const char *s
= fname
;
4842 #if defined (__MSDOS__) || defined (_WIN32)
4843 if (ISALPHA (s
[0]) && s
[1] == ':') s
+= 2;
4846 if ((p
= rindex (s
, ':'))) s
= p
+ 1; /* Skip device. */
4847 if ((p
= rindex (s
, ']'))) s
= p
+ 1; /* Skip directory. */
4848 if ((p
= rindex (s
, '>'))) s
= p
+ 1; /* Skip alternate (int'n'l) dir. */
4852 if ((p
= rindex (s
, '/'))) s
= p
+ 1;
4853 #ifdef DIR_SEPARATOR
4854 if ((p
= rindex (s
, DIR_SEPARATOR
))) s
= p
+ 1;
4859 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4862 absolute_filename (filename
)
4863 const char *filename
;
4865 #if defined (__MSDOS__) \
4866 || (defined (_WIN32) && !defined (__CYGWIN__) && !defined (_UWIN))
4867 if (ISALPHA (filename
[0]) && filename
[1] == ':') filename
+= 2;
4869 #if defined (__CYGWIN__)
4870 /* At present, any path that begins with a drive spec is absolute. */
4871 if (ISALPHA (filename
[0]) && filename
[1] == ':') return 1;
4874 if (index (filename
, ':') != 0) return 1;
4876 if (filename
[0] == '/') return 1;
4877 #ifdef DIR_SEPARATOR
4878 if (filename
[0] == DIR_SEPARATOR
) return 1;
4883 /* Returns whether or not a given character is a directory separator.
4884 Used by simplify_filename. */
4885 static inline int is_dir_separator
PROTO ((int));
4889 is_dir_separator(ch
)
4892 return (ch
== DIR_SEPARATOR
)
4893 #if defined (DIR_SEPARATOR_2)
4894 || (ch
== DIR_SEPARATOR_2
)
4899 /* Remove unnecessary characters from FILENAME in place,
4900 to avoid unnecessary filename aliasing.
4901 Return the length of the resulting string.
4903 Do only the simplifications allowed by Posix.
4904 It is OK to miss simplifications on non-Posix hosts,
4905 since this merely leads to suboptimal results. */
4908 simplify_filename (filename
)
4911 register char *from
= filename
;
4912 register char *to
= filename
;
4915 /* Remove redundant initial /s. */
4916 if (is_dir_separator (*from
))
4918 *to
++ = DIR_SEPARATOR
;
4919 if (is_dir_separator (*++from
))
4921 if (is_dir_separator (*++from
))
4923 /* 3 or more initial /s are equivalent to 1 /. */
4924 while (is_dir_separator (*++from
))
4929 /* On some hosts // differs from /; Posix allows this. */
4930 *to
++ = DIR_SEPARATOR
;
4940 if (from
[0] == '.' && from
[1] == '/')
4945 /* Copy this component and trailing DIR_SEPARATOR, if any. */
4946 while (!is_dir_separator (*to
++ = *from
++))
4950 /* Trim . component at end of nonempty name. */
4951 to
-= filename
<= to
- 3 && to
[-3] == DIR_SEPARATOR
&& to
[-2] == '.';
4953 /* Trim unnecessary trailing /s. */
4954 while (to0
< --to
&& to
[-1] == DIR_SEPARATOR
)
4958 return to
- filename
;
4961 #if defined(DIR_SEPARATOR_2)
4962 /* Simplify to one directory separator. */
4963 to
[-1] = DIR_SEPARATOR
;
4967 /* Skip /s after a /. */
4968 while (is_dir_separator (*from
))
4973 /* The file_name_map structure holds a mapping of file names for a
4974 particular directory. This mapping is read from the file named
4975 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4976 map filenames on a file system with severe filename restrictions,
4977 such as DOS. The format of the file name map file is just a series
4978 of lines with two tokens on each line. The first token is the name
4979 to map, and the second token is the actual name to use. */
4981 struct file_name_map
4983 struct file_name_map
*map_next
;
4988 #define FILE_NAME_MAP_FILE "header.gcc"
4990 /* Read a space delimited string of unlimited length from a stdio
4994 read_filename_string (ch
, f
)
5002 set
= alloc
= xmalloc (len
+ 1);
5006 while ((ch
= getc (f
)) != EOF
&& ! is_space
[ch
])
5008 if (set
- alloc
== len
)
5011 alloc
= xrealloc (alloc
, len
+ 1);
5012 set
= alloc
+ len
/ 2;
5022 /* Read the file name map file for DIRNAME.
5023 If DIRNAME is empty, read the map file for the working directory;
5024 otherwise DIRNAME must end in '/'. */
5026 static struct file_name_map
*
5027 read_name_map (dirname
)
5028 const char *dirname
;
5030 /* This structure holds a linked list of file name maps, one per
5032 struct file_name_map_list
5034 struct file_name_map_list
*map_list_next
;
5035 char *map_list_name
;
5036 struct file_name_map
*map_list_map
;
5038 static struct file_name_map_list
*map_list
;
5039 register struct file_name_map_list
*map_list_ptr
;
5044 for (map_list_ptr
= map_list
; map_list_ptr
;
5045 map_list_ptr
= map_list_ptr
->map_list_next
)
5046 if (! strcmp (map_list_ptr
->map_list_name
, dirname
))
5047 return map_list_ptr
->map_list_map
;
5049 map_list_ptr
= ((struct file_name_map_list
*)
5050 xmalloc (sizeof (struct file_name_map_list
)));
5051 map_list_ptr
->map_list_name
= xstrdup (dirname
);
5052 map_list_ptr
->map_list_map
= NULL
;
5054 dirlen
= strlen (dirname
);
5055 name
= (char *) alloca (dirlen
+ strlen (FILE_NAME_MAP_FILE
) + 1);
5056 strcpy (name
, dirname
);
5057 strcat (name
, FILE_NAME_MAP_FILE
);
5058 f
= fopen (name
, "r");
5060 map_list_ptr
->map_list_map
= NULL
;
5065 while ((ch
= getc (f
)) != EOF
)
5068 struct file_name_map
*ptr
;
5073 from
= read_filename_string (ch
, f
);
5074 while ((ch
= getc (f
)) != EOF
&& is_hor_space
[ch
])
5076 to
= read_filename_string (ch
, f
);
5078 simplify_filename (from
);
5079 tolen
= simplify_filename (to
);
5081 ptr
= ((struct file_name_map
*)
5082 xmalloc (sizeof (struct file_name_map
)));
5083 ptr
->map_from
= from
;
5085 /* Make the real filename absolute. */
5086 if (absolute_filename (to
))
5090 ptr
->map_to
= xmalloc (dirlen
+ tolen
+ 1);
5091 strcpy (ptr
->map_to
, dirname
);
5092 strcat (ptr
->map_to
, to
);
5096 ptr
->map_next
= map_list_ptr
->map_list_map
;
5097 map_list_ptr
->map_list_map
= ptr
;
5099 while ((ch
= getc (f
)) != '\n')
5106 map_list_ptr
->map_list_next
= map_list
;
5107 map_list
= map_list_ptr
;
5109 return map_list_ptr
->map_list_map
;
5112 /* Try to open include file FILENAME. SEARCHPTR is the directory
5113 being tried from the include file search path.
5114 IMPORTING is "" if we are importing, null otherwise.
5115 Return -2 if found, either a matching name or a matching inode.
5116 Otherwise, open the file and return a file descriptor if successful
5117 or -1 if unsuccessful.
5118 Unless unsuccessful, put a descriptor of the included file into *PINC.
5119 This function maps filenames on file systems based on information read by
5123 open_include_file (filename
, searchptr
, importing
, pinc
)
5125 struct file_name_list
*searchptr
;
5126 const U_CHAR
*importing
;
5127 struct include_file
**pinc
;
5129 char *fname
= remap
? remap_include_file (filename
, searchptr
) : filename
;
5132 /* Look up FNAME in include_hashtab. */
5133 struct include_file
**phead
= &include_hashtab
[hashf ((U_CHAR
*) fname
,
5136 struct include_file
*inc
, *head
= *phead
;
5137 for (inc
= head
; inc
; inc
= inc
->next
)
5138 if (!strcmp (fname
, inc
->fname
))
5142 || ! inc
->control_macro
5143 || (inc
->control_macro
[0] && ! lookup (inc
->control_macro
, -1, -1))) {
5145 fd
= open (fname
, O_RDONLY
, 0);
5150 /* if #include <dir/file> fails, try again with hacked spec. */
5151 if (!hack_vms_include_specification (fname
, 0))
5153 fd
= open (fname
, O_RDONLY
, 0);
5160 /* FNAME was not in include_hashtab; insert a new entry. */
5161 inc
= (struct include_file
*) xmalloc (sizeof (struct include_file
));
5164 inc
->control_macro
= 0;
5165 inc
->deps_output
= 0;
5166 if (fstat (fd
, &inc
->st
) != 0)
5167 pfatal_with_name (fname
);
5170 /* Look for another file with the same inode and device. */
5171 if (lookup_ino_include (inc
)
5172 && inc
->control_macro
5173 && (!inc
->control_macro
[0] || lookup (inc
->control_macro
, -1, -1))) {
5179 /* For -M, add this file to the dependencies. */
5180 if (! inc
->deps_output
&& (system_include_depth
!= 0) < print_deps
) {
5181 inc
->deps_output
= 1;
5182 deps_output (fname
, ' ');
5185 /* Handle -H option. */
5186 if (print_include_names
)
5187 fprintf (stderr
, "%*s%s\n", indepth
, "", fname
);
5191 inc
->control_macro
= importing
;
5197 /* Return the remapped name of the include file FILENAME.
5198 SEARCHPTR is the directory being tried from the include file path. */
5201 remap_include_file (filename
, searchptr
)
5203 struct file_name_list
*searchptr
;
5205 register struct file_name_map
*map
;
5206 register const char *from
;
5210 if (! searchptr
->got_name_map
)
5212 searchptr
->name_map
= read_name_map (searchptr
->fname
);
5213 searchptr
->got_name_map
= 1;
5216 /* Check the mapping for the directory we are using. */
5217 from
= filename
+ strlen (searchptr
->fname
);
5218 for (map
= searchptr
->name_map
; map
; map
= map
->map_next
)
5219 if (! strcmp (map
->map_from
, from
))
5223 from
= base_name (filename
);
5225 if (from
!= filename
|| !searchptr
)
5227 /* Try to find a mapping file for the particular directory we are
5228 looking in. Thus #include <sys/types.h> will look up sys/types.h
5229 in /usr/include/header.gcc and look up types.h in
5230 /usr/include/sys/header.gcc. */
5232 char *dir
= (char *) alloca (from
- filename
+ 1);
5233 bcopy (filename
, dir
, from
- filename
);
5234 dir
[from
- filename
] = '\0';
5236 for (map
= read_name_map (dir
); map
; map
= map
->map_next
)
5237 if (! strcmp (map
->map_from
, from
))
5244 /* Insert INC into the include file table, hashed by device and inode number.
5245 If a file with different name but same dev+ino was already in the table,
5246 return 1 and set INC's control macro to the already-known macro. */
5249 lookup_ino_include (inc
)
5250 struct include_file
*inc
;
5252 int hash
= ((unsigned) (inc
->st
.st_dev
+ INO_T_HASH (inc
->st
.st_ino
))
5253 % INCLUDE_HASHSIZE
);
5254 struct include_file
*i
= include_ino_hashtab
[hash
];
5256 include_ino_hashtab
[hash
] = inc
;
5258 for (; i
; i
= i
->next_ino
)
5259 if (INO_T_EQ (inc
->st
.st_ino
, i
->st
.st_ino
)
5260 && inc
->st
.st_dev
== i
->st
.st_dev
) {
5261 inc
->control_macro
= i
->control_macro
;
5268 /* Process file descriptor F, which corresponds to include file INC,
5270 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5271 "system" include directories (as decided by the `is_system_include'
5273 DIRPTR is the link in the dir path through which this file was found,
5274 or 0 if the file name was absolute. */
5277 finclude (f
, inc
, op
, system_header_p
, dirptr
)
5279 struct include_file
*inc
;
5281 int system_header_p
;
5282 struct file_name_list
*dirptr
;
5284 char *fname
= inc
->fname
;
5286 FILE_BUF
*fp
; /* For input stack frame */
5287 int missing_newline
= 0;
5289 CHECK_DEPTH (return;);
5291 fp
= &instack
[indepth
+ 1];
5292 bzero ((char *) fp
, sizeof (FILE_BUF
));
5293 fp
->nominal_fname
= fp
->fname
= fname
;
5294 fp
->nominal_fname_len
= strlen (fname
);
5298 fp
->if_stack
= if_stack
;
5299 fp
->system_header_p
= system_header_p
;
5302 if (S_ISREG (inc
->st
.st_mode
)) {
5303 size_t s
= (size_t) inc
->st
.st_size
;
5304 if (s
!= inc
->st
.st_size
|| s
+ 2 < s
)
5306 fp
->buf
= (U_CHAR
*) xmalloc (s
+ 2);
5309 /* Read the file contents, knowing that s is an upper bound
5310 on the number of bytes we can read. */
5311 fp
->length
= safe_read (f
, (char *) fp
->buf
, s
);
5312 if (fp
->length
< 0) goto nope
;
5314 else if (S_ISDIR (inc
->st
.st_mode
)) {
5315 error ("directory `%s' specified in #include", fname
);
5319 /* Cannot count its file size before reading.
5320 First read the entire file into heap and
5321 copy them into buffer on stack. */
5326 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
5329 i
= safe_read (f
, (char *) fp
->buf
+ st_size
, bsize
- st_size
);
5331 goto nope
; /* error! */
5333 if (st_size
!= bsize
)
5334 break; /* End of file */
5336 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
5339 fp
->length
= st_size
;
5342 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5343 /* Backslash-newline at end is not good enough. */
5344 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5345 fp
->buf
[fp
->length
++] = '\n';
5346 missing_newline
= 1;
5348 fp
->buf
[fp
->length
] = '\0';
5350 /* Close descriptor now, so nesting does not use lots of descriptors. */
5353 /* Must do this before calling trigraph_pcp, so that the correct file name
5354 will be printed in warning messages. */
5357 input_file_stack_tick
++;
5362 if (warn_white_space
)
5363 check_white_space (fp
);
5365 output_line_directive (fp
, op
, 0, enter_file
);
5368 if (missing_newline
)
5371 if (pedantic
&& missing_newline
)
5372 pedwarn ("file does not end in newline");
5375 input_file_stack_tick
++;
5376 output_line_directive (&instack
[indepth
], op
, 0, leave_file
);
5382 perror_with_name (fname
);
5387 /* Record that inclusion of the include file INC
5388 should be controlled by the macro named MACRO_NAME.
5389 This means that trying to include the file again
5390 will do something if that macro is defined. */
5393 record_control_macro (inc
, macro_name
)
5394 struct include_file
*inc
;
5395 const U_CHAR
*macro_name
;
5397 if (!inc
->control_macro
|| inc
->control_macro
[0])
5398 inc
->control_macro
= macro_name
;
5401 /* Load the specified precompiled header into core, and verify its
5402 preconditions. PCF indicates the file descriptor to read, which must
5403 be a regular file. *ST is its file status.
5404 FNAME indicates the file name of the original header.
5405 *LIMIT will be set to an address one past the end of the file.
5406 If the preconditions of the file are not satisfied, the buffer is
5407 freed and we return 0. If the preconditions are satisfied, return
5408 the address of the buffer following the preconditions. The buffer, in
5409 this case, should never be freed because various pieces of it will
5410 be referred to until all precompiled strings are output at the end of
5414 check_precompiled (pcf
, st
, fname
, limit
)
5417 const char *fname ATTRIBUTE_UNUSED
;
5427 if (S_ISREG (st
->st_mode
))
5429 size_t s
= (size_t) st
->st_size
;
5430 if (s
!= st
->st_size
|| s
+ 2 < s
)
5432 buf
= xmalloc (s
+ 2);
5433 length
= safe_read (pcf
, buf
, s
);
5440 if (length
> 0 && buf
[length
-1] != '\n')
5441 buf
[length
++] = '\n';
5444 *limit
= buf
+ length
;
5446 /* File is in core. Check the preconditions. */
5447 if (!check_preconditions (buf
))
5449 for (cp
= buf
; *cp
; cp
++)
5452 fprintf (stderr
, "Using preinclude %s\n", fname
);
5458 fprintf (stderr
, "Cannot use preinclude %s\n", fname
);
5464 /* PREC (null terminated) points to the preconditions of a
5465 precompiled header. These are a series of #define and #undef
5466 lines which must match the current contents of the hash
5470 check_preconditions (prec
)
5474 const char *lineend
;
5477 lineend
= index (prec
, '\n');
5479 if (*prec
++ != '#') {
5480 error ("Bad format encountered while reading precompiled file");
5483 if (!strncmp (prec
, "define", 6)) {
5487 mdef
= create_definition ((const U_CHAR
*) prec
,
5488 (const U_CHAR
*) lineend
, NULL_PTR
);
5493 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, -1)) == NULL
5494 || (hp
->type
!= T_MACRO
&& hp
->type
!= T_CONST
)
5495 || (hp
->type
== T_MACRO
5496 && !compare_defs (mdef
.defn
, hp
->value
.defn
)
5497 && (mdef
.defn
->length
!= 2
5498 || mdef
.defn
->expansion
[0] != '\n'
5499 || mdef
.defn
->expansion
[1] != ' ')))
5501 } else if (!strncmp (prec
, "undef", 5)) {
5506 while (is_hor_space
[(U_CHAR
) *prec
])
5509 while (is_idchar
[(U_CHAR
) *prec
])
5513 if (lookup ((const U_CHAR
*) name
, len
, -1))
5516 error ("Bad format encountered while reading precompiled file");
5521 /* They all passed successfully */
5525 /* Process the main body of a precompiled file. BUF points to the
5526 string section of the file, following the preconditions. LIMIT is one
5527 character past the end. NAME is the name of the file being read
5528 in. OP is the main output buffer. */
5531 pcfinclude (buf
, name
, op
)
5540 /* First in the file comes 4 bytes indicating the number of strings, */
5541 /* in network byte order. (MSB first). */
5543 nstrings
= (nstrings
<< 8) | *cp
++;
5544 nstrings
= (nstrings
<< 8) | *cp
++;
5545 nstrings
= (nstrings
<< 8) | *cp
++;
5547 /* Looping over each string... */
5548 while (nstrings
--) {
5549 U_CHAR
*string_start
;
5550 U_CHAR
*endofthiskey
;
5554 /* Each string starts with a STRINGDEF structure (str), followed */
5555 /* by the text of the string (string_start) */
5557 /* First skip to a longword boundary */
5558 /* ??? Why a 4-byte boundary? On all machines? */
5559 /* NOTE: This works correctly even if size_t
5560 is narrower than a pointer.
5561 Do not try risky measures here to get another type to use!
5562 Do not include stddef.h--it will fail! */
5563 if ((size_t) cp
& 3)
5564 cp
+= 4 - ((size_t) cp
& 3);
5566 /* Now get the string. */
5567 str
= (STRINGDEF
*) (PTR
) cp
;
5568 string_start
= cp
+= sizeof (STRINGDEF
);
5570 for (; *cp
; cp
++) /* skip the string */
5573 /* We need to macro expand the string here to ensure that the
5574 proper definition environment is in place. If it were only
5575 expanded when we find out it is needed, macros necessary for
5576 its proper expansion might have had their definitions changed. */
5577 tmpbuf
= expand_to_temp_buffer (string_start
, cp
++, 0, 0);
5578 /* Lineno is already set in the precompiled file */
5579 str
->contents
= tmpbuf
.buf
;
5580 str
->len
= tmpbuf
.bufp
- tmpbuf
.buf
;
5582 str
->filename
= name
;
5583 str
->output_mark
= outbuf
.bufp
- outbuf
.buf
;
5586 *stringlist_tailp
= str
;
5587 stringlist_tailp
= &str
->chain
;
5589 /* Next comes a fourbyte number indicating the number of keys
5592 nkeys
= (nkeys
<< 8) | *cp
++;
5593 nkeys
= (nkeys
<< 8) | *cp
++;
5594 nkeys
= (nkeys
<< 8) | *cp
++;
5596 /* If this number is -1, then the string is mandatory. */
5600 /* Otherwise, for each key, */
5601 for (; nkeys
--; free (tmpbuf
.buf
), cp
= endofthiskey
+ 1) {
5602 KEYDEF
*kp
= (KEYDEF
*) (PTR
) cp
;
5606 /* It starts with a KEYDEF structure */
5607 cp
+= sizeof (KEYDEF
);
5609 /* Find the end of the key. At the end of this for loop we
5610 advance CP to the start of the next key using this variable. */
5611 endofthiskey
= cp
+ strlen ((char *) cp
);
5614 /* Expand the key, and enter it into the hash table. */
5615 tmpbuf
= expand_to_temp_buffer (cp
, endofthiskey
, 0, 0);
5618 while (is_hor_space
[*bp
])
5620 if (!is_idstart
[*bp
] || bp
== tmpbuf
.bufp
) {
5625 hp
= lookup (bp
, -1, -1);
5628 install (bp
, -1, T_PCSTRING
, (char *) kp
, -1);
5630 else if (hp
->type
== T_PCSTRING
) {
5631 kp
->chain
= hp
->value
.keydef
;
5632 hp
->value
.keydef
= kp
;
5638 /* This output_line_directive serves to switch us back to the current
5639 input file in case some of these strings get output (which will
5640 result in line directives for the header file being output). */
5641 output_line_directive (&instack
[indepth
], op
, 0, enter_file
);
5644 /* Called from rescan when it hits a key for strings. Mark them all
5645 used and clean up. */
5653 for (kp
= hp
->value
.keydef
; kp
; kp
= kp
->chain
)
5654 kp
->str
->writeflag
= 1;
5658 /* Write the output, interspersing precompiled strings in their
5659 appropriate places. */
5664 STRINGDEF
*next_string
;
5665 U_CHAR
*cur_buf_loc
;
5666 int line_directive_len
= 80;
5667 char *line_directive
= xmalloc (line_directive_len
);
5670 /* In each run through the loop, either cur_buf_loc ==
5671 next_string_loc, in which case we print a series of strings, or
5672 it is less than next_string_loc, in which case we write some of
5674 cur_buf_loc
= outbuf
.buf
;
5675 next_string
= stringlist
;
5677 while (cur_buf_loc
< outbuf
.bufp
|| next_string
) {
5679 && cur_buf_loc
- outbuf
.buf
== next_string
->output_mark
) {
5680 if (next_string
->writeflag
) {
5681 len
= 4 * strlen ((const char *) next_string
->filename
) + 32;
5682 while (len
> line_directive_len
)
5683 line_directive
= xrealloc (line_directive
,
5684 line_directive_len
*= 2);
5685 sprintf (line_directive
, "\n# %d ", next_string
->lineno
);
5686 strcpy (quote_string (line_directive
+ strlen (line_directive
),
5687 (const char *) next_string
->filename
,
5688 strlen ((const char *) next_string
->filename
)),
5690 safe_write (fileno (stdout
), line_directive
, strlen (line_directive
));
5691 safe_write (fileno (stdout
),
5692 (char *) next_string
->contents
, next_string
->len
);
5694 next_string
= next_string
->chain
;
5698 ? (next_string
->output_mark
5699 - (cur_buf_loc
- outbuf
.buf
))
5700 : outbuf
.bufp
- cur_buf_loc
);
5702 safe_write (fileno (stdout
), (char *) cur_buf_loc
, len
);
5706 free (line_directive
);
5709 /* Pass a directive through to the output file.
5710 BUF points to the contents of the directive, as a contiguous string.
5711 LIMIT points to the first character past the end of the directive.
5712 KEYWORD is the keyword-table entry for the directive. */
5715 pass_thru_directive (buf
, limit
, op
, keyword
)
5717 const U_CHAR
*limit
;
5719 struct directive
*keyword
;
5721 register int keyword_length
= keyword
->length
;
5723 check_expand (op
, 1 + keyword_length
+ (limit
- buf
));
5725 bcopy (keyword
->name
, (char *) op
->bufp
, keyword_length
);
5726 op
->bufp
+= keyword_length
;
5727 if (limit
!= buf
&& buf
[0] != ' ')
5729 bcopy ((const PTR
) buf
, (PTR
) op
->bufp
, limit
- buf
);
5730 op
->bufp
+= (limit
- buf
);
5733 /* Count the line we have just made in the output,
5734 to get in sync properly. */
5739 /* The arglist structure is built by do_define to tell
5740 collect_definition where the argument names begin. That
5741 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5742 would contain pointers to the strings x, y, and z.
5743 Collect_definition would then build a DEFINITION node,
5744 with reflist nodes pointing to the places x, y, and z had
5745 appeared. So the arglist is just convenience data passed
5746 between these two routines. It is not kept around after
5747 the current #define has been processed and entered into the
5751 struct arglist
*next
;
5758 /* Create a DEFINITION node from a #define directive. Arguments are
5759 as for do_define. */
5762 create_definition (buf
, limit
, op
)
5763 const U_CHAR
*buf
, *limit
;
5766 const U_CHAR
*bp
; /* temp ptr into input buffer */
5767 const U_CHAR
*symname
; /* remember where symbol name starts */
5768 int sym_length
; /* and how long it is */
5769 int line
= instack
[indepth
].lineno
;
5770 const char *file
= instack
[indepth
].nominal_fname
;
5771 size_t file_len
= instack
[indepth
].nominal_fname_len
;
5775 int arglengths
= 0; /* Accumulate lengths of arg names
5776 plus number of args. */
5781 while (is_hor_space
[*bp
])
5784 symname
= bp
; /* remember where it starts */
5785 sym_length
= check_macro_name (bp
, 0);
5788 /* Lossage will occur if identifiers or control keywords are broken
5789 across lines using backslash. This is not the right place to take
5793 struct arglist
*arg_ptrs
= NULL
;
5796 bp
++; /* skip '(' */
5797 SKIP_WHITE_SPACE (bp
);
5799 /* Loop over macro argument names. */
5800 while (*bp
!= ')') {
5801 struct arglist
*temp
;
5803 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
5805 temp
->next
= arg_ptrs
;
5806 temp
->argno
= argno
++;
5807 temp
->rest_args
= 0;
5811 pedwarn ("another parameter follows `%s'",
5814 if (!is_idstart
[*bp
])
5816 if (c9x
&& limit
- bp
> (long) REST_EXTENSION_LENGTH
5817 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0)
5819 /* This is the ISO C 9x way to write macros with variable
5820 number of arguments. */
5822 temp
->rest_args
= 1;
5825 pedwarn ("invalid character in macro parameter name");
5828 /* Find the end of the arg name. */
5829 while (is_idchar
[*bp
]) {
5831 /* do we have a "special" rest-args extension here? */
5832 if (limit
- bp
> (long) REST_EXTENSION_LENGTH
5833 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
5834 if (pedantic
&& !instack
[indepth
].system_header_p
)
5835 pedwarn ("ANSI C does not allow macro with variable arguments");
5837 temp
->rest_args
= 1;
5841 if (bp
== temp
->name
&& rest_args
== 1)
5843 /* This is the ISO C 9x style. */
5844 temp
->name
= (U_CHAR
*) va_args_name
;
5845 temp
->length
= VA_ARGS_NAME_LENGTH
;
5848 temp
->length
= bp
- temp
->name
;
5850 bp
+= REST_EXTENSION_LENGTH
;
5851 arglengths
+= temp
->length
+ 2;
5852 SKIP_WHITE_SPACE (bp
);
5853 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
5854 error ("badly punctuated parameter list in `#define'");
5859 SKIP_WHITE_SPACE (bp
);
5860 /* A comma at this point can only be followed by an identifier. */
5861 if (!is_idstart
[*bp
]
5862 && !(c9x
&& limit
- bp
> (long) REST_EXTENSION_LENGTH
5863 && bcmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0)) {
5864 error ("badly punctuated parameter list in `#define'");
5869 error ("unterminated parameter list in `#define'");
5873 struct arglist
*otemp
;
5875 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
5876 if (temp
->length
== otemp
->length
5877 && bcmp (temp
->name
, otemp
->name
, temp
->length
) == 0)
5879 error ("duplicate argument name `%.*s' in `#define'",
5880 temp
->length
, temp
->name
);
5883 if (rest_args
== 0 && temp
->length
== VA_ARGS_NAME_LENGTH
5884 && bcmp (temp
->name
, va_args_name
, VA_ARGS_NAME_LENGTH
) == 0)
5887 reserved name `%s' used as argument name in `#define'", va_args_name
);
5893 ++bp
; /* skip paren */
5894 SKIP_WHITE_SPACE (bp
);
5895 /* now everything from bp before limit is the definition. */
5896 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
5897 defn
->rest_args
= rest_args
;
5899 /* Now set defn->args.argnames to the result of concatenating
5900 the argument names in reverse order
5901 with comma-space between them. */
5902 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
5904 struct arglist
*temp
;
5906 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
5907 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
5909 if (temp
->next
!= 0) {
5910 defn
->args
.argnames
[i
++] = ',';
5911 defn
->args
.argnames
[i
++] = ' ';
5914 defn
->args
.argnames
[i
] = 0;
5917 /* Simple expansion or empty definition. */
5921 if (is_hor_space
[*bp
]) {
5923 SKIP_WHITE_SPACE (bp
);
5924 } else if (sym_length
) {
5926 case '!': case '"': case '#': case '%': case '&': case '\'':
5927 case ')': case '*': case '+': case ',': case '-': case '.':
5928 case '/': case ':': case ';': case '<': case '=': case '>':
5929 case '?': case '[': case '\\': case ']': case '^': case '{':
5930 case '|': case '}': case '~':
5931 warning ("missing white space after `#define %.*s'",
5932 sym_length
, symname
);
5936 pedwarn ("missing white space after `#define %.*s'",
5937 sym_length
, symname
);
5942 /* Now everything from bp before limit is the definition. */
5943 defn
= collect_expansion (bp
, limit
, -1, NULL_PTR
);
5944 defn
->args
.argnames
= (U_CHAR
*) "";
5949 defn
->file_len
= file_len
;
5951 /* OP is null if this is a predefinition */
5952 defn
->predefined
= !op
;
5954 mdef
.symnam
= symname
;
5955 mdef
.symlen
= sym_length
;
5964 /* Process a #define directive.
5965 BUF points to the contents of the #define directive, as a contiguous string.
5966 LIMIT points to the first character past the end of the definition.
5967 KEYWORD is the keyword-table entry for #define. */
5970 do_define (buf
, limit
, op
, keyword
)
5971 U_CHAR
*buf
, *limit
;
5973 struct directive
*keyword
;
5977 enum node_type newtype
= keyword
->type
== T_DEFINE
? T_MACRO
: T_POISON
;
5979 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5980 if (pcp_outfile
&& op
)
5981 pass_thru_directive (buf
, limit
, op
, keyword
);
5983 mdef
= create_definition (buf
, limit
, op
);
5987 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
5991 if ((hp
= lookup (mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
) {
5993 /* Redefining a precompiled key is ok. */
5994 if (hp
->type
== T_PCSTRING
)
5996 /* Redefining a poisoned identifier is even worse than `not ok'. */
5997 else if (hp
->type
== T_POISON
)
5999 /* Poisoning anything else is not ok.
6000 The poison should always come first. */
6001 else if (newtype
== T_POISON
)
6003 /* Redefining a macro is ok if the definitions are the same. */
6004 else if (hp
->type
== T_MACRO
)
6005 ok
= ! compare_defs (mdef
.defn
, hp
->value
.defn
);
6006 /* Redefining a constant is ok with -D. */
6007 else if (hp
->type
== T_CONST
)
6008 ok
= ! done_initializing
;
6010 /* Print the warning or error if it's not ok. */
6013 /* If we are passing through #define and #undef directives, do
6014 that for this re-definition now. */
6015 if (debug_output
&& op
)
6016 pass_thru_directive (buf
, limit
, op
, keyword
);
6018 if (hp
->type
== T_POISON
)
6019 error ("redefining poisoned `%.*s'", mdef
.symlen
, mdef
.symnam
);
6021 pedwarn ("`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
6022 if (hp
->type
== T_MACRO
)
6023 pedwarn_with_file_and_line (hp
->value
.defn
->file
,
6024 hp
->value
.defn
->file_len
,
6025 hp
->value
.defn
->line
,
6026 "this is the location of the previous definition");
6028 if (hp
->type
!= T_POISON
)
6030 /* Replace the old definition. */
6032 hp
->value
.defn
= mdef
.defn
;
6035 /* If we are passing through #define and #undef directives, do
6036 that for this new definition now. */
6037 if (debug_output
&& op
)
6038 pass_thru_directive (buf
, limit
, op
, keyword
);
6039 install (mdef
.symnam
, mdef
.symlen
, newtype
,
6040 (char *) mdef
.defn
, hashcode
);
6051 /* Check a purported macro name SYMNAME, and yield its length.
6052 ASSERTION is nonzero if this is really for an assertion name. */
6055 check_macro_name (symname
, assertion
)
6056 const U_CHAR
*symname
;
6062 for (p
= symname
; is_idchar
[*p
]; p
++)
6064 sym_length
= p
- symname
;
6066 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
6067 error (assertion
? "invalid assertion name" : "invalid macro name");
6068 else if (!is_idstart
[*symname
]
6069 || (sym_length
== 7 && ! bcmp (symname
, "defined", 7)))
6071 ? "invalid assertion name `%.*s'"
6072 : "invalid macro name `%.*s'"),
6073 sym_length
, symname
);
6077 /* Return zero if two DEFINITIONs are isomorphic. */
6080 compare_defs (d1
, d2
)
6081 DEFINITION
*d1
, *d2
;
6083 register struct reflist
*a1
, *a2
;
6084 register U_CHAR
*p1
= d1
->expansion
;
6085 register U_CHAR
*p2
= d2
->expansion
;
6088 if (d1
->nargs
!= d2
->nargs
)
6091 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
6093 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
6094 a1
= a1
->next
, a2
= a2
->next
) {
6095 if (!((a1
->nchars
== a2
->nchars
&& ! bcmp (p1
, p2
, a1
->nchars
))
6096 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
6097 || a1
->argno
!= a2
->argno
6098 || a1
->stringify
!= a2
->stringify
6099 || a1
->raw_before
!= a2
->raw_before
6100 || a1
->raw_after
!= a2
->raw_after
)
6108 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
6109 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
6114 /* Return 1 if two parts of two macro definitions are effectively different.
6115 One of the parts starts at BEG1 and has LEN1 chars;
6116 the other has LEN2 chars at BEG2.
6117 Any sequence of whitespace matches any other sequence of whitespace.
6118 FIRST means these parts are the first of a macro definition;
6119 so ignore leading whitespace entirely.
6120 LAST means these parts are the last of a macro definition;
6121 so ignore trailing whitespace entirely. */
6124 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
6126 const U_CHAR
*beg1
, *beg2
;
6130 register const U_CHAR
*end1
= beg1
+ len1
;
6131 register const U_CHAR
*end2
= beg2
+ len2
;
6133 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6134 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6137 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
6138 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
6140 while (beg1
!= end1
&& beg2
!= end2
) {
6141 if (is_space
[*beg1
] && is_space
[*beg2
]) {
6142 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
6143 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
6144 } else if (*beg1
== *beg2
) {
6148 return (beg1
!= end1
) || (beg2
!= end2
);
6151 /* Read a replacement list for a macro with parameters.
6152 Build the DEFINITION structure.
6153 Reads characters of text starting at BUF until END.
6154 ARGLIST specifies the formal parameters to look for
6155 in the text of the definition; NARGS is the number of args
6156 in that list, or -1 for a macro name that wants no argument list.
6157 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6158 and NAMELEN is its length in characters.
6160 Note that comments, backslash-newlines, and leading white space
6161 have already been deleted from the argument. */
6163 /* If there is no trailing whitespace, a Newline Space is added at the end
6164 to prevent concatenation that would be contrary to the standard. */
6167 collect_expansion (buf
, end
, nargs
, arglist
)
6171 struct arglist
*arglist
;
6174 register const U_CHAR
*p
;
6175 register const U_CHAR
*limit
;
6176 register U_CHAR
*lastp
, *exp_p
;
6177 struct reflist
*endpat
= NULL
;
6178 /* Pointer to first nonspace after last ## seen. */
6179 const U_CHAR
*concat
= 0;
6180 /* Pointer to first nonspace after last single-# seen. */
6181 const U_CHAR
*stringify
= 0;
6182 /* How those tokens were spelled. */
6183 enum sharp_token_type concat_sharp_token_type
= NO_SHARP_TOKEN
;
6184 enum sharp_token_type stringify_sharp_token_type
= NO_SHARP_TOKEN
;
6186 int expected_delimiter
= '\0';
6188 /* Scan thru the replacement list, ignoring comments and quoted
6189 strings, picking up on the macro calls. It does a linear search
6190 thru the arg list on every potential symbol. Profiling might say
6191 that something smarter should happen. */
6196 /* Find the beginning of the trailing whitespace. */
6199 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
6201 /* Allocate space for the text in the macro definition.
6202 Each input char may or may not need 1 byte,
6203 so this is an upper bound.
6204 The extra 3 are for invented trailing newline-marker and final null. */
6205 maxsize
= (sizeof (DEFINITION
)
6207 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
6209 defn
->nargs
= nargs
;
6210 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
6215 : p
[0] == '%' && p
[1] == ':' && p
[2] == '%' && p
[3] == ':') {
6216 error ("`##' at start of macro definition");
6217 p
+= p
[0] == '#' ? 2 : 4;
6220 /* Process the main body of the definition. */
6222 int skipped_arg
= 0;
6223 register U_CHAR c
= *p
++;
6231 if (expected_delimiter
!= '\0') {
6232 if (c
== expected_delimiter
)
6233 expected_delimiter
= '\0';
6235 expected_delimiter
= c
;
6239 if (expected_delimiter
) {
6240 /* In a string, backslash goes through
6241 and makes next char ordinary. */
6247 if (!expected_delimiter
&& *p
== ':') {
6248 /* %: is not a digraph if preceded by an odd number of '<'s. */
6249 const U_CHAR
*p0
= p
- 1;
6250 while (buf
< p0
&& p0
[-1] == '<')
6253 /* Treat %:%: as ## and %: as #. */
6254 if (p
[1] == '%' && p
[2] == ':') {
6256 goto sharp_sharp_token
;
6267 /* # is ordinary inside a string. */
6268 if (expected_delimiter
)
6272 /* ##: concatenate preceding and following tokens. */
6273 /* Take out the first #, discard preceding whitespace. */
6275 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
6277 /* Skip the second #. */
6279 concat_sharp_token_type
= c
;
6280 if (is_hor_space
[*p
]) {
6281 concat_sharp_token_type
= c
+ 1;
6283 SKIP_WHITE_SPACE (p
);
6287 error ("`##' at end of macro definition");
6288 } else if (nargs
>= 0) {
6289 /* Single #: stringify following argument ref.
6290 Don't leave the # in the expansion. */
6293 stringify_sharp_token_type
= c
;
6294 if (is_hor_space
[*p
]) {
6295 stringify_sharp_token_type
= c
+ 1;
6297 SKIP_WHITE_SPACE (p
);
6299 if (! is_idstart
[*p
] || nargs
== 0
6300 || (*p
== 'L' && (p
[1] == '\'' || p
[1] == '"')))
6301 error ("`#' operator is not followed by a macro argument name");
6308 /* In -traditional mode, recognize arguments inside strings and
6309 character constants, and ignore special properties of #.
6310 Arguments inside strings are considered "stringified", but no
6311 extra quote marks are supplied. */
6315 if (expected_delimiter
!= '\0') {
6316 if (c
== expected_delimiter
)
6317 expected_delimiter
= '\0';
6319 expected_delimiter
= c
;
6323 /* Backslash quotes delimiters and itself, but not macro args. */
6324 if (expected_delimiter
!= 0 && p
< limit
6325 && (*p
== expected_delimiter
|| *p
== '\\')) {
6332 if (expected_delimiter
!= '\0') /* No comments inside strings. */
6335 /* If we find a comment that wasn't removed by handle_directive,
6336 this must be -traditional. So replace the comment with
6339 while (++p
< limit
) {
6340 if (p
[0] == '*' && p
[1] == '/') {
6346 /* Mark this as a concatenation-point, as if it had been ##. */
6354 #ifdef MULTIBYTE_CHARS
6355 /* Handle multibyte characters inside string and character literals. */
6356 if (expected_delimiter
!= '\0')
6360 length
= local_mblen (p
, limit
- p
);
6364 bcopy (p
, exp_p
, length
);
6373 /* Handle the start of a symbol. */
6374 if (is_idchar
[c
] && nargs
> 0) {
6375 const U_CHAR
*id_beg
= p
- 1;
6379 while (p
!= limit
&& is_idchar
[*p
]) p
++;
6380 id_len
= p
- id_beg
;
6383 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
6384 register struct arglist
*arg
;
6386 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
6387 struct reflist
*tpat
;
6389 if (arg
->name
[0] == c
6390 && arg
->length
== id_len
6391 && bcmp (arg
->name
, id_beg
, id_len
) == 0) {
6392 enum sharp_token_type tpat_stringify
;
6393 if (expected_delimiter
) {
6394 if (warn_stringify
) {
6396 warning ("macro argument `%.*s' is stringified.",
6399 warning ("macro arg `%.*s' would be stringified with -traditional.",
6403 /* If ANSI, don't actually substitute inside a string. */
6406 tpat_stringify
= SHARP_TOKEN
;
6409 = (stringify
== id_beg
6410 ? stringify_sharp_token_type
: NO_SHARP_TOKEN
);
6412 /* make a pat node for this arg and append it to the end of
6414 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
6417 = concat
== id_beg
? concat_sharp_token_type
: NO_SHARP_TOKEN
;
6418 tpat
->raw_after
= NO_SHARP_TOKEN
;
6419 tpat
->rest_args
= arg
->rest_args
;
6420 tpat
->stringify
= tpat_stringify
;
6423 defn
->pattern
= tpat
;
6425 endpat
->next
= tpat
;
6428 tpat
->argno
= arg
->argno
;
6429 tpat
->nchars
= exp_p
- lastp
;
6431 register const U_CHAR
*p1
= p
;
6432 SKIP_WHITE_SPACE (p1
);
6435 : p1
[0]=='%' && p1
[1]==':' && p1
[2]=='%' && p1
[3]==':')
6436 tpat
->raw_after
= p1
[0] + (p
!= p1
);
6438 lastp
= exp_p
; /* place to start copying from next time */
6445 /* If this was not a macro arg, copy it into the expansion. */
6446 if (! skipped_arg
) {
6447 register const U_CHAR
*lim1
= p
;
6451 if (stringify
== id_beg
)
6452 error ("`#' operator should be followed by a macro argument name");
6457 if (!traditional
&& expected_delimiter
== 0) {
6458 /* If ANSI, put in a newline-space marker to prevent token pasting.
6459 But not if "inside a string" (which in ANSI mode happens only for
6467 defn
->length
= exp_p
- defn
->expansion
;
6469 /* Crash now if we overrun the allocated size. */
6470 if (defn
->length
+ 1 > maxsize
)
6474 /* This isn't worth the time it takes. */
6475 /* give back excess storage */
6476 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
6483 do_assert (buf
, limit
, op
, keyword
)
6484 U_CHAR
*buf
, *limit
;
6485 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6486 struct directive
*keyword ATTRIBUTE_UNUSED
;
6488 const U_CHAR
*bp
; /* temp ptr into input buffer */
6489 const U_CHAR
*symname
; /* remember where symbol name starts */
6490 int sym_length
; /* and how long it is */
6491 struct arglist
*tokens
= NULL
;
6493 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6494 pedwarn ("ANSI C does not allow `#assert'");
6498 while (is_hor_space
[*bp
])
6501 symname
= bp
; /* remember where it starts */
6502 sym_length
= check_macro_name (bp
, 1);
6504 /* #define doesn't do this, but we should. */
6505 SKIP_WHITE_SPACE (bp
);
6507 /* Lossage will occur if identifiers or control tokens are broken
6508 across lines using backslash. This is not the right place to take
6512 error ("missing token-sequence in `#assert'");
6519 bp
++; /* skip '(' */
6520 SKIP_WHITE_SPACE (bp
);
6522 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6526 error ("empty token-sequence in `#assert'");
6530 ++bp
; /* skip paren */
6531 SKIP_WHITE_SPACE (bp
);
6534 /* If this name isn't already an assertion name, make it one.
6535 Error if it was already in use in some other way. */
6538 ASSERTION_HASHNODE
*hp
;
6539 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6540 struct tokenlist_list
*value
6541 = (struct tokenlist_list
*) xmalloc (sizeof (struct tokenlist_list
));
6543 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6545 if (sym_length
== 7 && ! bcmp (symname
, "defined", 7))
6546 error ("`defined' redefined as assertion");
6547 hp
= assertion_install (symname
, sym_length
, hashcode
);
6550 /* Add the spec'd token-sequence to the list of such. */
6551 value
->tokens
= tokens
;
6552 value
->next
= hp
->value
;
6560 do_unassert (buf
, limit
, op
, keyword
)
6561 U_CHAR
*buf
, *limit
;
6562 FILE_BUF
*op ATTRIBUTE_UNUSED
;
6563 struct directive
*keyword ATTRIBUTE_UNUSED
;
6565 const U_CHAR
*bp
; /* temp ptr into input buffer */
6566 const U_CHAR
*symname
; /* remember where symbol name starts */
6567 int sym_length
; /* and how long it is */
6569 struct arglist
*tokens
= NULL
;
6570 int tokens_specified
= 0;
6572 if (pedantic
&& done_initializing
&& !instack
[indepth
].system_header_p
)
6573 pedwarn ("ANSI C does not allow `#unassert'");
6577 while (is_hor_space
[*bp
])
6580 symname
= bp
; /* remember where it starts */
6581 sym_length
= check_macro_name (bp
, 1);
6583 /* #define doesn't do this, but we should. */
6584 SKIP_WHITE_SPACE (bp
);
6586 /* Lossage will occur if identifiers or control tokens are broken
6587 across lines using backslash. This is not the right place to take
6593 bp
++; /* skip '(' */
6594 SKIP_WHITE_SPACE (bp
);
6596 tokens
= read_token_list (&bp
, limit
, &error_flag
);
6600 error ("empty token list in `#unassert'");
6604 tokens_specified
= 1;
6606 ++bp
; /* skip paren */
6607 SKIP_WHITE_SPACE (bp
);
6611 ASSERTION_HASHNODE
*hp
;
6612 int hashcode
= hashf (symname
, sym_length
, ASSERTION_HASHSIZE
);
6613 struct tokenlist_list
*tail
, *prev
;
6615 hp
= assertion_lookup (symname
, sym_length
, hashcode
);
6619 /* If no token list was specified, then eliminate this assertion
6621 if (! tokens_specified
) {
6622 struct tokenlist_list
*next
;
6623 for (tail
= hp
->value
; tail
; tail
= next
) {
6625 free_token_list (tail
->tokens
);
6628 delete_assertion (hp
);
6630 /* If a list of tokens was given, then delete any matching list. */
6635 struct tokenlist_list
*next
= tail
->next
;
6636 if (compare_token_lists (tail
->tokens
, tokens
)) {
6640 hp
->value
= tail
->next
;
6641 free_token_list (tail
->tokens
);
6654 /* Test whether there is an assertion named NAME
6655 and optionally whether it has an asserted token list TOKENS.
6656 NAME is not null terminated; its length is SYM_LENGTH.
6657 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6660 check_assertion (name
, sym_length
, tokens_specified
, tokens
)
6663 int tokens_specified
;
6664 struct arglist
*tokens
;
6666 ASSERTION_HASHNODE
*hp
;
6667 int hashcode
= hashf (name
, sym_length
, ASSERTION_HASHSIZE
);
6669 if (pedantic
&& !instack
[indepth
].system_header_p
)
6670 pedwarn ("ANSI C does not allow testing assertions");
6672 hp
= assertion_lookup (name
, sym_length
, hashcode
);
6674 /* It is not an assertion; just return false. */
6677 /* If no token list was specified, then value is 1. */
6678 if (! tokens_specified
)
6682 struct tokenlist_list
*tail
;
6686 /* If a list of tokens was given,
6687 then succeed if the assertion records a matching list. */
6690 if (compare_token_lists (tail
->tokens
, tokens
))
6695 /* Fail if the assertion has no matching list. */
6700 /* Compare two lists of tokens for equality including order of tokens. */
6703 compare_token_lists (l1
, l2
)
6704 struct arglist
*l1
, *l2
;
6707 if (l1
->length
!= l2
->length
)
6709 if (bcmp (l1
->name
, l2
->name
, l1
->length
))
6715 /* Succeed if both lists end at the same time. */
6719 /* Read a space-separated list of tokens ending in a close parenthesis.
6720 Return a list of strings, in the order they were written.
6721 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6722 Parse the text starting at *BPP, and update *BPP.
6723 Don't parse beyond LIMIT. */
6725 static struct arglist
*
6726 read_token_list (bpp
, limit
, error_flag
)
6728 const U_CHAR
*limit
;
6731 struct arglist
*token_ptrs
= 0;
6732 const U_CHAR
*bp
= *bpp
;
6737 /* Loop over the assertion value tokens. */
6739 struct arglist
*temp
;
6742 const U_CHAR
*beg
= bp
;
6744 /* Find the end of the token. */
6748 } else if (*bp
== ')') {
6753 } else if (*bp
== '"' || *bp
== '\'')
6754 bp
= skip_quoted_string (bp
, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
6756 while (! is_hor_space
[*bp
] && *bp
!= '(' && *bp
!= ')'
6757 && *bp
!= '"' && *bp
!= '\'' && bp
!= limit
)
6760 temp
= (struct arglist
*) xmalloc (sizeof (struct arglist
));
6761 temp2
= (U_CHAR
*) xmalloc (bp
- beg
+ 1);
6762 bcopy ((const PTR
) beg
, (PTR
) temp2
, bp
- beg
);
6763 temp2
[bp
- beg
] = 0;
6765 temp
->next
= token_ptrs
;
6767 temp
->length
= bp
- beg
;
6769 SKIP_WHITE_SPACE (bp
);
6772 error ("unterminated token sequence in `#assert' or `#unassert'");
6779 /* We accumulated the names in reverse order.
6780 Now reverse them to get the proper order. */
6782 register struct arglist
*prev
= 0, *this, *next
;
6783 for (this = token_ptrs
; this; this = next
) {
6793 free_token_list (tokens
)
6794 struct arglist
*tokens
;
6797 struct arglist
*next
= tokens
->next
;
6798 free ((PTR
) tokens
->name
);
6804 /* Install a name in the assertion hash table.
6806 If LEN is >= 0, it is the length of the name.
6807 Otherwise, compute the length by scanning the entire name.
6809 If HASH is >= 0, it is the precomputed hash code.
6810 Otherwise, compute the hash code. */
6812 static ASSERTION_HASHNODE
*
6813 assertion_install (name
, len
, hash
)
6818 register ASSERTION_HASHNODE
*hp
;
6819 register int i
, bucket
;
6821 register const U_CHAR
*q
;
6823 i
= sizeof (ASSERTION_HASHNODE
) + len
+ 1;
6824 hp
= (ASSERTION_HASHNODE
*) xmalloc (i
);
6826 hp
->bucket_hdr
= &assertion_hashtab
[bucket
];
6827 hp
->next
= assertion_hashtab
[bucket
];
6828 assertion_hashtab
[bucket
] = hp
;
6830 if (hp
->next
!= NULL
)
6831 hp
->next
->prev
= hp
;
6834 hp
->name
= ((U_CHAR
*) hp
) + sizeof (ASSERTION_HASHNODE
);
6837 for (i
= 0; i
< len
; i
++)
6843 /* Find the most recent hash node for name "name" (ending with first
6844 non-identifier char) installed by install
6846 If LEN is >= 0, it is the length of the name.
6847 Otherwise, compute the length by scanning the entire name.
6849 If HASH is >= 0, it is the precomputed hash code.
6850 Otherwise, compute the hash code. */
6852 static ASSERTION_HASHNODE
*
6853 assertion_lookup (name
, len
, hash
)
6858 register ASSERTION_HASHNODE
*bucket
;
6860 bucket
= assertion_hashtab
[hash
];
6862 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
6864 bucket
= bucket
->next
;
6870 delete_assertion (hp
)
6871 ASSERTION_HASHNODE
*hp
;
6874 if (hp
->prev
!= NULL
)
6875 hp
->prev
->next
= hp
->next
;
6876 if (hp
->next
!= NULL
)
6877 hp
->next
->prev
= hp
->prev
;
6879 /* Make sure that the bucket chain header that the deleted guy was
6880 on points to the right thing afterwards. */
6881 if (hp
== *hp
->bucket_hdr
)
6882 *hp
->bucket_hdr
= hp
->next
;
6888 * interpret #line directive. Remembers previously seen fnames
6889 * in its very own hash table.
6891 #define FNAME_HASHSIZE 37
6894 do_line (buf
, limit
, op
, keyword
)
6895 U_CHAR
*buf
, *limit
;
6897 struct directive
*keyword ATTRIBUTE_UNUSED
;
6899 register U_CHAR
*bp
;
6900 FILE_BUF
*ip
= &instack
[indepth
];
6903 enum file_change_code file_change
= same_file
;
6905 /* Expand any macros. */
6906 tem
= expand_to_temp_buffer (buf
, limit
, 0, 0);
6908 /* Point to macroexpanded line, which is null-terminated now. */
6911 SKIP_WHITE_SPACE (bp
);
6913 if (!ISDIGIT (*bp
)) {
6914 error ("invalid format `#line' directive");
6918 /* The Newline at the end of this line remains to be processed.
6919 To put the next line at the specified line number,
6920 we must store a line number now that is one less. */
6921 new_lineno
= atoi ((char *) bp
) - 1;
6923 /* NEW_LINENO is one less than the actual line number here. */
6924 if (pedantic
&& new_lineno
< 0)
6925 pedwarn ("line number out of range in `#line' directive");
6927 /* skip over the line number. */
6928 while (ISDIGIT (*bp
))
6931 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6932 if (*bp
&& !is_space
[*bp
]) {
6933 error ("invalid format `#line' directive");
6938 SKIP_WHITE_SPACE (bp
);
6941 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
6942 HASHNODE
*hp
, **hash_bucket
;
6948 /* Turn the file name, which is a character string literal,
6949 into a null-terminated string. Do this in place. */
6952 switch ((*p
++ = *bp
++)) {
6954 if (! ignore_escape_flag
)
6956 char *bpc
= (char *) bp
;
6957 HOST_WIDEST_INT c
= parse_escape (&bpc
, (HOST_WIDEST_INT
) (U_CHAR
) (-1));
6958 bp
= (U_CHAR
*) bpc
;
6971 fname_length
= p
- fname
;
6973 SKIP_WHITE_SPACE (bp
);
6976 pedwarn ("garbage at end of `#line' directive");
6978 file_change
= enter_file
;
6979 else if (*bp
== '2')
6980 file_change
= leave_file
;
6981 else if (*bp
== '3')
6982 ip
->system_header_p
= 1;
6983 else if (*bp
== '4')
6984 ip
->system_header_p
= 2;
6986 error ("invalid format `#line' directive");
6991 SKIP_WHITE_SPACE (bp
);
6993 ip
->system_header_p
= 1;
6995 SKIP_WHITE_SPACE (bp
);
6998 ip
->system_header_p
= 2;
7000 SKIP_WHITE_SPACE (bp
);
7003 error ("invalid format `#line' directive");
7008 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
7009 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
7010 if (hp
->length
== fname_length
&&
7011 bcmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
7012 ip
->nominal_fname
= hp
->value
.cpval
;
7013 ip
->nominal_fname_len
= fname_length
;
7017 /* Didn't find it; cons up a new one. */
7018 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
7019 hp
->next
= *hash_bucket
;
7022 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
7023 ip
->nominal_fname_len
= hp
->length
= fname_length
;
7024 bcopy (fname
, ((char *) hp
) + sizeof (HASHNODE
), fname_length
+ 1);
7027 error ("invalid format `#line' directive");
7031 ip
->lineno
= new_lineno
;
7032 output_line_directive (ip
, op
, 0, file_change
);
7033 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
7037 /* Remove the definition of a symbol from the symbol table.
7038 according to un*x /lib/cpp, it is not an error to undef
7039 something that has no definitions, so it isn't one here either. */
7042 do_undef (buf
, limit
, op
, keyword
)
7043 U_CHAR
*buf
, *limit
;
7045 struct directive
*keyword
;
7049 U_CHAR
*orig_buf
= buf
;
7051 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7052 if (pcp_outfile
&& op
)
7053 pass_thru_directive (buf
, limit
, op
, keyword
);
7055 SKIP_WHITE_SPACE (buf
);
7056 sym_length
= check_macro_name (buf
, 0);
7058 while ((hp
= lookup (buf
, sym_length
, -1)) != NULL
) {
7059 /* If we are generating additional info for debugging (with -g) we
7060 need to pass through all effective #undef directives. */
7061 if (debug_output
&& op
)
7062 pass_thru_directive (orig_buf
, limit
, op
, keyword
);
7063 if (hp
->type
== T_POISON
)
7064 error ("cannot undefine poisoned `%s'", hp
->name
);
7066 if (hp
->type
!= T_MACRO
)
7067 warning ("undefining `%s'", hp
->name
);
7074 SKIP_WHITE_SPACE (buf
);
7076 pedwarn ("garbage after `#undef' directive");
7082 /* Report an error detected by the program we are processing.
7083 Use the text of the line in the error message. */
7086 do_error (buf
, limit
, op
, keyword
)
7087 U_CHAR
*buf
, *limit
;
7088 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7089 struct directive
*keyword
;
7091 int length
= limit
- buf
;
7092 U_CHAR
*copy
= (U_CHAR
*) alloca (length
+ 1);
7093 bcopy ((const PTR
) buf
, (PTR
) copy
, length
);
7095 SKIP_WHITE_SPACE (copy
);
7097 switch (keyword
->type
) {
7099 error ("#error %s", copy
);
7103 if (pedantic
&& !instack
[indepth
].system_header_p
)
7104 pedwarn ("ANSI C does not allow `#warning'");
7105 warning ("#warning %s", copy
);
7114 /* Remember the name of the current file being read from so that we can
7115 avoid ever including it again. */
7122 for (i
= indepth
; i
>= 0; i
--)
7123 if (instack
[i
].inc
) {
7124 record_control_macro (instack
[i
].inc
, (const U_CHAR
*) "");
7129 /* Report program identification. */
7132 do_ident (buf
, limit
, op
, keyword
)
7133 U_CHAR
*buf
, *limit
;
7135 struct directive
*keyword ATTRIBUTE_UNUSED
;
7140 /* Allow #ident in system headers, since that's not user's fault. */
7141 if (pedantic
&& !instack
[indepth
].system_header_p
)
7142 pedwarn ("ANSI C does not allow `#ident'");
7144 trybuf
= expand_to_temp_buffer (buf
, limit
, 0, 0);
7146 len
= trybuf
.bufp
- buf
;
7148 /* Output expanded directive. */
7149 check_expand (op
, 7 + len
);
7150 bcopy ("#ident ", (char *) op
->bufp
, 7);
7152 bcopy ((const PTR
) buf
, (PTR
) op
->bufp
, len
);
7159 /* #pragma and its argument line have already been copied to the output file.
7160 Just check for some recognized pragmas that need validation here. */
7163 do_pragma (buf
, limit
, op
, keyword
)
7164 U_CHAR
*buf
, *limit
;
7166 struct directive
*keyword
;
7168 SKIP_WHITE_SPACE (buf
);
7169 if (!strncmp ((char *) buf
, "once", 4)) {
7170 /* Allow #pragma once in system headers, since that's not the user's
7172 if (!instack
[indepth
].system_header_p
)
7173 warning ("`#pragma once' is obsolete");
7177 if (!strncmp (buf
, "poison", 6)) {
7178 /* Poison these symbols so that all subsequent usage produces an
7180 U_CHAR
*p
= buf
+ 6;
7182 SKIP_WHITE_SPACE (p
);
7187 while (end
< limit
&& is_idchar
[*end
])
7189 if (end
< limit
&& !is_space
[*end
])
7191 error ("invalid #pragma poison");
7194 do_define(p
, end
, op
, keyword
);
7196 SKIP_WHITE_SPACE (p
);
7200 if (!strncmp ((char *) buf
, "implementation", 14)) {
7201 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7202 been included yet. */
7205 U_CHAR
*p
= buf
+ 14, *fname
;
7206 SKIP_WHITE_SPACE (p
);
7211 p
= skip_quoted_string (p
, limit
, 0, NULL_PTR
, NULL_PTR
, NULL_PTR
);
7215 for (h
= 0; h
< INCLUDE_HASHSIZE
; h
++) {
7216 struct include_file
*inc
;
7217 for (inc
= include_hashtab
[h
]; inc
; inc
= inc
->next
) {
7218 if (!strcmp (base_name (inc
->fname
), (char *) fname
)) {
7219 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname
);
7229 /* This was a fun hack, but #pragma seems to start to be useful.
7230 By failing to recognize it, we pass it through unchanged to cc1. */
7232 /* The behavior of the #pragma directive is implementation defined.
7233 this implementation defines it as follows. */
7239 if (open ("/dev/tty", O_RDONLY
, 0666) != 0)
7242 if (open ("/dev/tty", O_WRONLY
, 0666) != 1)
7244 execl ("/usr/games/hack", "#pragma", 0);
7245 execl ("/usr/games/rogue", "#pragma", 0);
7246 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7247 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7249 fatal ("You are in a maze of twisty compiler features, all different");
7253 #ifdef SCCS_DIRECTIVE
7255 /* Just ignore #sccs, on systems where we define it at all. */
7258 do_sccs (buf
, limit
, op
, keyword
)
7259 U_CHAR
*buf ATTRIBUTE_UNUSED
;
7260 U_CHAR
*limit ATTRIBUTE_UNUSED
;
7261 FILE_BUF
*op ATTRIBUTE_UNUSED
;
7262 struct directive
*keyword ATTRIBUTE_UNUSED
;
7265 pedwarn ("ANSI C does not allow `#sccs'");
7269 #endif /* defined (SCCS_DIRECTIVE) */
7271 /* Handle #if directive by
7272 1) inserting special `defined' keyword into the hash table
7273 that gets turned into 0 or 1 by special_symbol (thus,
7274 if the luser has a symbol called `defined' already, it won't
7275 work inside the #if directive)
7276 2) rescan the input into a temporary output buffer
7277 3) pass the output buffer to the yacc parser and collect a value
7278 4) clean up the mess left from steps 1 and 2.
7279 5) call conditional_skip to skip til the next #endif (etc.),
7280 or not, depending on the value from step 3. */
7283 do_if (buf
, limit
, op
, keyword
)
7284 U_CHAR
*buf
, *limit
;
7286 struct directive
*keyword ATTRIBUTE_UNUSED
;
7288 HOST_WIDEST_INT value
;
7289 FILE_BUF
*ip
= &instack
[indepth
];
7291 value
= eval_if_expression (buf
, limit
- buf
);
7292 conditional_skip (ip
, value
== 0, T_IF
, NULL_PTR
, op
);
7296 /* Handle a #elif directive by not changing if_stack either.
7297 see the comment above do_else. */
7300 do_elif (buf
, limit
, op
, keyword
)
7301 U_CHAR
*buf
, *limit
;
7303 struct directive
*keyword ATTRIBUTE_UNUSED
;
7305 HOST_WIDEST_INT value
;
7306 FILE_BUF
*ip
= &instack
[indepth
];
7308 if (if_stack
== instack
[indepth
].if_stack
) {
7309 error ("`#elif' not within a conditional");
7312 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7313 error ("`#elif' after `#else'");
7314 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7315 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7316 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7317 if_stack
->fname_len
))) {
7318 fprintf (stderr
, ", file ");
7319 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7320 if_stack
->fname_len
, stderr
);
7322 fprintf (stderr
, ")\n");
7324 if_stack
->type
= T_ELIF
;
7327 if (if_stack
->if_succeeded
)
7328 skip_if_group (ip
, 0, op
);
7330 value
= eval_if_expression (buf
, limit
- buf
);
7332 skip_if_group (ip
, 0, op
);
7334 ++if_stack
->if_succeeded
; /* continue processing input */
7335 output_line_directive (ip
, op
, 1, same_file
);
7341 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7342 result as a C expression and return the value as an int. */
7344 static HOST_WIDEST_INT
7345 eval_if_expression (buf
, length
)
7350 HASHNODE
*save_defined
;
7351 HOST_WIDEST_INT value
;
7353 save_defined
= install ((const U_CHAR
*) "defined", -1, T_SPEC_DEFINED
,
7356 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0, 1);
7358 delete_macro (save_defined
); /* clean up special symbol */
7360 *temp_obuf
.bufp
= '\n';
7361 value
= parse_c_expression ((char *) temp_obuf
.buf
,
7362 warn_undef
&& !instack
[indepth
].system_header_p
);
7364 free (temp_obuf
.buf
);
7369 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7370 or don't skip to the #endif/#else/#elif depending on what directive
7371 is actually being processed. */
7374 do_xifdef (buf
, limit
, op
, keyword
)
7375 U_CHAR
*buf
, *limit
;
7377 struct directive
*keyword
;
7380 FILE_BUF
*ip
= &instack
[indepth
];
7382 int start_of_file
= 0;
7383 U_CHAR
*control_macro
= 0;
7385 /* Detect a #ifndef at start of file (not counting comments). */
7386 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
) {
7387 U_CHAR
*p
= ip
->buf
;
7388 while (p
!= directive_start
) {
7392 /* Make no special provision for backslash-newline here; this is
7393 slower if backslash-newlines are present, but it's correct,
7394 and it's not worth it to tune for the rare backslash-newline. */
7396 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7397 /* Skip this comment. */
7399 U_CHAR
*save_bufp
= ip
->bufp
;
7401 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7402 ip
->bufp
= save_bufp
;
7407 /* If we get here, this conditional is the beginning of the file. */
7412 /* Discard leading and trailing whitespace. */
7413 SKIP_WHITE_SPACE (buf
);
7414 while (limit
!= buf
&& is_hor_space
[limit
[-1]]) limit
--;
7416 /* Find the end of the identifier at the beginning. */
7417 for (end
= buf
; is_idchar
[*end
]; end
++);
7420 skip
= (keyword
->type
== T_IFDEF
);
7422 pedwarn (end
== limit
? "`#%s' with no argument"
7423 : "`#%s' argument starts with punctuation",
7428 if (! traditional
) {
7429 if (ISDIGIT (buf
[0]))
7430 pedwarn ("`#%s' argument starts with a digit", keyword
->name
);
7431 else if (end
!= limit
)
7432 pedwarn ("garbage at end of `#%s' argument", keyword
->name
);
7435 hp
= lookup (buf
, end
-buf
, -1);
7438 /* Output a precondition for this macro. */
7440 && (hp
->type
== T_CONST
7441 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
7442 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
7445 fprintf (pcp_outfile
, "#undef ");
7446 while (is_idchar
[*cp
]) /* Ick! */
7447 fputc (*cp
++, pcp_outfile
);
7448 putc ('\n', pcp_outfile
);
7452 if ((hp
!= NULL
) && (hp
->type
== T_POISON
)) {
7453 error("attempt to use poisoned `%s'.", hp
->name
);
7456 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
7457 if (start_of_file
&& !skip
) {
7458 control_macro
= (U_CHAR
*) xmalloc (end
- buf
+ 1);
7459 bcopy ((const PTR
) buf
, (PTR
) control_macro
, end
- buf
);
7460 control_macro
[end
- buf
] = 0;
7464 conditional_skip (ip
, skip
, T_IF
, control_macro
, op
);
7468 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7469 If this is a #ifndef starting at the beginning of a file,
7470 CONTROL_MACRO is the macro name tested by the #ifndef.
7471 Otherwise, CONTROL_MACRO is 0. */
7474 conditional_skip (ip
, skip
, type
, control_macro
, op
)
7477 enum node_type type
;
7478 const U_CHAR
*control_macro
;
7481 IF_STACK_FRAME
*temp
;
7483 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7484 temp
->fname
= ip
->nominal_fname
;
7485 temp
->fname_len
= ip
->nominal_fname_len
;
7486 temp
->lineno
= ip
->lineno
;
7487 temp
->next
= if_stack
;
7488 temp
->control_macro
= control_macro
;
7491 if_stack
->type
= type
;
7494 skip_if_group (ip
, 0, op
);
7497 ++if_stack
->if_succeeded
;
7498 output_line_directive (ip
, &outbuf
, 1, same_file
);
7502 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7503 Leaves input ptr at the sharp sign found.
7504 If ANY is nonzero, return at next directive of any sort. */
7507 skip_if_group (ip
, any
, op
)
7512 register U_CHAR
*bp
= ip
->bufp
, *cp
;
7513 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
7514 struct directive
*kt
;
7515 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
7516 U_CHAR
*beg_of_line
= bp
;
7517 register int ident_length
;
7518 U_CHAR
*ident
, *after_ident
;
7519 /* Save info about where the group starts. */
7520 U_CHAR
*beg_of_group
= bp
;
7521 int beg_lineno
= ip
->lineno
;
7522 int skipping_include_directive
= 0;
7524 if (output_conditionals
&& op
!= 0) {
7525 static const char * const ptr
= "#failed\n";
7526 int len
= strlen (ptr
);
7528 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7533 check_expand (op
, len
);
7534 bcopy (ptr
, (char *) op
->bufp
, len
);
7537 output_line_directive (ip
, op
, 1, 0);
7542 case '/': /* possible comment */
7546 || (cplusplus_comments
&& *bp
== '/')) {
7548 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
, 0);
7552 if (skipping_include_directive
) {
7553 while (bp
< endb
&& *bp
!= '>' && *bp
!= '\n') {
7554 if (*bp
== '\\' && bp
[1] == '\n') {
7563 if (skipping_include_directive
) {
7564 while (bp
< endb
&& *bp
!= '\n') {
7569 if (*bp
== '\\' && bp
[1] == '\n') {
7579 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
,
7580 NULL_PTR
, NULL_PTR
);
7583 /* Char after backslash loses its special meaning in some cases. */
7587 } else if (traditional
&& bp
< endb
)
7593 skipping_include_directive
= 0;
7596 if (beg_of_line
== 0 || traditional
)
7599 while (bp
[0] == '\\' && bp
[1] == '\n')
7605 /* # keyword: a # must be first nonblank char on the line */
7606 if (beg_of_line
== 0)
7610 /* Scan from start of line, skipping whitespace, comments
7611 and backslash-newlines, and see if we reach this #.
7612 If not, this # is not special. */
7614 /* If -traditional, require # to be at beginning of line. */
7617 if (is_hor_space
[*bp
])
7619 else if (*bp
== '\\' && bp
[1] == '\n')
7621 else if (*bp
== '/' && bp
[1] == '*') {
7635 #ifdef MULTIBYTE_CHARS
7637 length
= local_mblen (bp
, endb
- bp
);
7645 /* There is no point in trying to deal with C++ // comments here,
7646 because if there is one, then this # must be part of the
7647 comment and we would never reach here. */
7651 if (bp
!= ip
->bufp
) {
7652 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
7656 bp
= ip
->bufp
+ 1; /* Point after the '#' */
7657 if (ip
->bufp
[0] == '%') {
7658 /* Skip past the ':' again. */
7659 while (*bp
== '\\') {
7666 /* Skip whitespace and \-newline. */
7668 if (is_hor_space
[*bp
])
7670 else if (*bp
== '\\' && bp
[1] == '\n')
7672 else if (*bp
== '/') {
7674 newline_fix (bp
+ 1);
7676 for (bp
+= 2; ; bp
++) {
7679 else if (*bp
== '*') {
7680 if (bp
[-1] == '/' && warn_comments
)
7681 warning ("`/*' within comment");
7683 newline_fix (bp
+ 1);
7689 #ifdef MULTIBYTE_CHARS
7691 length
= local_mblen (bp
, endb
- bp
);
7698 } else if (bp
[1] == '/' && cplusplus_comments
) {
7699 for (bp
+= 2; ; bp
++) {
7702 if (*bp
== '\\' && bp
[1] == '\n')
7705 warning ("multiline `//' comment");
7711 #ifdef MULTIBYTE_CHARS
7713 length
= local_mblen (bp
, endb
- bp
);
7727 /* Now find end of directive name.
7728 If we encounter a backslash-newline, exchange it with any following
7729 symbol-constituents so that we end up with a contiguous name. */
7736 name_newline_fix (bp
);
7742 ident_length
= bp
- cp
;
7746 /* A line of just `#' becomes blank. */
7748 if (ident_length
== 0 && *after_ident
== '\n') {
7752 if (ident_length
== 0 || !is_idstart
[*ident
]) {
7754 while (is_idchar
[*p
]) {
7755 if (*p
< '0' || *p
> '9')
7759 /* Handle # followed by a line number. */
7760 if (p
!= ident
&& !is_idchar
[*p
]) {
7762 pedwarn ("`#' followed by integer");
7766 /* Avoid error for `###' and similar cases unless -pedantic. */
7768 while (*p
== '#' || is_hor_space
[*p
]) p
++;
7770 if (pedantic
&& !lang_asm
)
7771 pedwarn ("invalid preprocessing directive");
7776 if (!lang_asm
&& pedantic
)
7777 pedwarn ("invalid preprocessing directive name");
7781 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
7782 IF_STACK_FRAME
*temp
;
7783 if (ident_length
== kt
->length
7784 && bcmp (cp
, kt
->name
, kt
->length
) == 0) {
7785 /* If we are asked to return on next directive, do so now. */
7793 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
7794 temp
->next
= if_stack
;
7796 temp
->lineno
= ip
->lineno
;
7797 temp
->fname
= ip
->nominal_fname
;
7798 temp
->fname_len
= ip
->nominal_fname_len
;
7799 temp
->type
= kt
->type
;
7803 if (pedantic
&& if_stack
!= save_if_stack
)
7804 validate_else (bp
, endb
);
7806 if (if_stack
== instack
[indepth
].if_stack
) {
7807 error ("`#%s' not within a conditional", kt
->name
);
7810 else if (if_stack
== save_if_stack
)
7811 goto done
; /* found what we came for */
7813 if (kt
->type
!= T_ENDIF
) {
7814 if (if_stack
->type
== T_ELSE
)
7815 error ("`#else' or `#elif' after `#else'");
7816 if_stack
->type
= kt
->type
;
7821 if_stack
= if_stack
->next
;
7826 case T_INCLUDE_NEXT
:
7828 skipping_include_directive
= 1;
7837 /* Don't let erroneous code go by. */
7838 if (kt
->length
< 0 && !lang_asm
&& pedantic
)
7839 pedwarn ("invalid preprocessing directive name");
7844 /* after this returns, rescan will exit because ip->bufp
7845 now points to the end of the buffer.
7846 rescan is responsible for the error message also. */
7849 if (output_conditionals
&& op
!= 0) {
7850 static const char * const ptr
= "#endfailed\n";
7851 int len
= strlen (ptr
);
7853 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
7858 check_expand (op
, beg_of_line
- beg_of_group
);
7859 bcopy ((const PTR
) beg_of_group
, (PTR
) op
->bufp
,
7860 beg_of_line
- beg_of_group
);
7861 op
->bufp
+= beg_of_line
- beg_of_group
;
7862 op
->lineno
+= ip
->lineno
- beg_lineno
;
7863 check_expand (op
, len
);
7864 bcopy (ptr
, (char *) op
->bufp
, len
);
7870 /* Handle a #else directive. Do this by just continuing processing
7871 without changing if_stack ; this is so that the error message
7872 for missing #endif's etc. will point to the original #if. It
7873 is possible that something different would be better. */
7876 do_else (buf
, limit
, op
, keyword
)
7877 U_CHAR
*buf
, *limit
;
7879 struct directive
*keyword ATTRIBUTE_UNUSED
;
7881 FILE_BUF
*ip
= &instack
[indepth
];
7884 SKIP_WHITE_SPACE (buf
);
7886 pedwarn ("text following `#else' violates ANSI standard");
7889 if (if_stack
== instack
[indepth
].if_stack
) {
7890 error ("`#else' not within a conditional");
7893 /* #ifndef can't have its special treatment for containing the whole file
7894 if it has a #else clause. */
7895 if_stack
->control_macro
= 0;
7897 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
7898 error ("`#else' after `#else'");
7899 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
7900 if (! (if_stack
->fname_len
== ip
->nominal_fname_len
7901 && !bcmp (if_stack
->fname
, ip
->nominal_fname
,
7902 if_stack
->fname_len
))) {
7903 fprintf (stderr
, ", file ");
7904 fwrite (if_stack
->fname
, sizeof if_stack
->fname
[0],
7905 if_stack
->fname_len
, stderr
);
7907 fprintf (stderr
, ")\n");
7909 if_stack
->type
= T_ELSE
;
7912 if (if_stack
->if_succeeded
)
7913 skip_if_group (ip
, 0, op
);
7915 ++if_stack
->if_succeeded
; /* continue processing input */
7916 output_line_directive (ip
, op
, 1, same_file
);
7921 /* Unstack after #endif directive. */
7924 do_endif (buf
, limit
, op
, keyword
)
7925 U_CHAR
*buf
, *limit
;
7927 struct directive
*keyword ATTRIBUTE_UNUSED
;
7930 SKIP_WHITE_SPACE (buf
);
7932 pedwarn ("text following `#endif' violates ANSI standard");
7935 if (if_stack
== instack
[indepth
].if_stack
)
7936 error ("unbalanced `#endif'");
7938 IF_STACK_FRAME
*temp
= if_stack
;
7939 if_stack
= if_stack
->next
;
7940 if (temp
->control_macro
!= 0) {
7941 /* This #endif matched a #ifndef at the start of the file.
7942 See if it is at the end of the file. */
7943 FILE_BUF
*ip
= &instack
[indepth
];
7944 U_CHAR
*p
= ip
->bufp
;
7945 U_CHAR
*ep
= ip
->buf
+ ip
->length
;
7951 && (*p
== '*' || (cplusplus_comments
&& *p
== '/'))) {
7952 /* Skip this comment. */
7954 U_CHAR
*save_bufp
= ip
->bufp
;
7956 p
= skip_to_end_of_comment (ip
, &junk
, 1);
7957 ip
->bufp
= save_bufp
;
7962 /* If we get here, this #endif ends a #ifndef
7963 that contains all of the file (aside from whitespace).
7964 Arrange not to include the file again
7965 if the macro that was tested is defined.
7967 Do not do this for the top-level file in a -include or any
7968 file in a -imacros. */
7970 && ! (indepth
== 1 && no_record_file
)
7971 && ! (no_record_file
&& no_output
))
7972 record_control_macro (ip
->inc
, temp
->control_macro
);
7976 output_line_directive (&instack
[indepth
], op
, 1, same_file
);
7981 /* When an #else or #endif is found while skipping failed conditional,
7982 if -pedantic was specified, this is called to warn about text after
7983 the directive name. P points to the first char after the directive
7987 validate_else (p
, limit
)
7988 register const U_CHAR
*p
;
7989 register const U_CHAR
*limit
;
7991 /* Advance P over whitespace and comments. */
7993 while (*p
== '\\' && p
[1] == '\n')
7995 if (is_hor_space
[*p
])
7997 else if (*p
== '/') {
7998 while (p
[1] == '\\' && p
[2] == '\n')
8001 /* Don't bother warning about unterminated comments
8002 since that will happen later. Just be sure to exit. */
8003 for (p
+= 2; ; p
++) {
8007 while (p
[1] == '\\' && p
[2] == '\n')
8016 #ifdef MULTIBYTE_CHARS
8018 length
= local_mblen (p
, limit
- p
);
8025 else if (cplusplus_comments
&& p
[1] == '/')
8031 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8034 /* Skip a comment, assuming the input ptr immediately follows the
8035 initial slash-star. Bump *LINE_COUNTER for each newline.
8036 (The canonical line counter is &ip->lineno.)
8037 Don't use this routine (or the next one) if bumping the line
8038 counter is not sufficient to deal with newlines in the string.
8040 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8041 This feature is useful when processing a comment that is going to
8042 be processed or was processed at another point in the preprocessor,
8043 to avoid a duplicate warning. Likewise for unterminated comment
8047 skip_to_end_of_comment (ip
, line_counter
, nowarn
)
8048 register FILE_BUF
*ip
;
8049 int *line_counter
; /* place to remember newlines, or NULL */
8052 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8053 register U_CHAR
*bp
= ip
->bufp
;
8054 FILE_BUF
*op
= put_out_comments
&& !line_counter
? &outbuf
: (FILE_BUF
*) 0;
8055 int start_line
= line_counter
? *line_counter
: 0;
8057 /* JF this line_counter stuff is a crock to make sure the
8058 comment is only put out once, no matter how many times
8059 the comment is skipped. It almost works */
8062 *op
->bufp
++ = bp
[-1];
8064 if (cplusplus_comments
&& bp
[-1] == '/') {
8065 for (; bp
< limit
; bp
++) {
8068 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
8070 if (!nowarn
&& warn_comments
)
8071 warning ("multiline `//' comment");
8083 #ifdef MULTIBYTE_CHARS
8085 length
= local_mblen (bp
, limit
- bp
);
8090 bcopy (bp
, op
->bufp
, length
- 1);
8091 op
->bufp
+= (length
- 1);
8103 while (bp
< limit
) {
8108 /* If this is the end of the file, we have an unterminated comment.
8109 Don't swallow the newline. We are guaranteed that there will be a
8110 trailing newline and various pieces assume it's there. */
8117 if (line_counter
!= NULL
)
8123 if (bp
[-2] == '/' && !nowarn
&& warn_comments
)
8124 warning ("`/*' within comment");
8134 #ifdef MULTIBYTE_CHARS
8139 length
= local_mblen (bp
, limit
- bp
);
8145 bcopy (bp
, op
->bufp
, length
);
8155 error_with_line (line_for_error (start_line
), "unterminated comment");
8160 /* Skip over a quoted string. BP points to the opening quote.
8161 Returns a pointer after the closing quote. Don't go past LIMIT.
8162 START_LINE is the line number of the starting point (but it need
8163 not be valid if the starting point is inside a macro expansion).
8165 The input stack state is not changed.
8167 If COUNT_NEWLINES is nonzero, it points to an int to increment
8168 for each newline passed; also, warn about any white space
8169 just before line end.
8171 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8172 if we pass a backslash-newline.
8174 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8177 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
8178 register const U_CHAR
*bp
;
8179 register const U_CHAR
*limit
;
8181 int *count_newlines
;
8182 int *backslash_newlines_p
;
8185 register U_CHAR c
, match
;
8190 error_with_line (line_for_error (start_line
),
8191 "unterminated string or character constant");
8192 error_with_line (multiline_string_line
,
8193 "possible real start of unterminated constant");
8194 multiline_string_line
= 0;
8201 while (*bp
== '\\' && bp
[1] == '\n') {
8202 if (backslash_newlines_p
)
8203 *backslash_newlines_p
= 1;
8209 if (backslash_newlines_p
)
8210 *backslash_newlines_p
= 1;
8215 } else if (c
== '\n') {
8217 /* Unterminated strings and character constants are 'valid'. */
8218 bp
--; /* Don't consume the newline. */
8223 if (match
== '\'') {
8224 error_with_line (line_for_error (start_line
),
8225 "unterminated character constant");
8231 /* If not traditional, then allow newlines inside strings. */
8232 if (count_newlines
) {
8233 if (warn_white_space
&& is_hor_space
[bp
[-2]])
8234 warning ("white space at end of line in string");
8237 if (multiline_string_line
== 0) {
8239 pedwarn_with_line (line_for_error (start_line
),
8240 "string constant runs past end of line");
8241 multiline_string_line
= start_line
;
8243 } else if (c
== match
)
8245 #ifdef MULTIBYTE_CHARS
8249 length
= local_mblen (bp
, limit
- bp
);
8256 return (U_CHAR
*) bp
;
8259 /* Place into DST a quoted string representing the string SRC.
8260 SRCLEN is the length of SRC; SRC may contain null bytes.
8261 Return the address of DST's terminating null. */
8264 quote_string (dst
, src
, srclen
)
8270 const char *srclim
= src
+ srclen
;
8273 while (src
!= srclim
)
8274 switch ((c
= *src
++))
8281 sprintf (dst
, "\\%03o", c
);
8298 /* Skip across a group of balanced parens, starting from IP->bufp.
8299 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8301 This does not handle newlines, because it's used for the arg of #if,
8302 where there aren't any newlines. Also, backslash-newline can't appear. */
8305 skip_paren_group (ip
)
8306 register FILE_BUF
*ip
;
8308 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
8309 U_CHAR
*p
= ip
->bufp
;
8311 int lines_dummy
= 0;
8313 while (p
!= limit
) {
8323 return ip
->bufp
= p
;
8329 p
= skip_to_end_of_comment (ip
, &lines_dummy
, 0);
8337 p
= skip_quoted_string (p
- 1, limit
, 0, NULL_PTR
, NULL_PTR
, &eofp
);
8339 return ip
->bufp
= p
;
8349 /* Write out a #line directive, for instance, after an #include file.
8350 If CONDITIONAL is nonzero, we can omit the #line if it would
8351 appear to be a no-op, and we can output a few newlines instead
8352 if we want to increase the line number by a small amount.
8353 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8356 output_line_directive (ip
, op
, conditional
, file_change
)
8359 enum file_change_code file_change
;
8362 char *line_directive_buf
, *line_end
;
8364 if (no_line_directives
8365 || ip
->fname
== NULL
8367 op
->lineno
= ip
->lineno
;
8372 if (ip
->lineno
== op
->lineno
)
8375 /* If the inherited line number is a little too small,
8376 output some newlines instead of a #line directive. */
8377 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
8378 check_expand (op
, 10);
8379 while (ip
->lineno
> op
->lineno
) {
8387 /* Output a positive line number if possible. */
8388 while (ip
->lineno
<= 0 && ip
->bufp
- ip
->buf
< ip
->length
8389 && *ip
->bufp
== '\n') {
8394 line_directive_buf
= (char *) alloca (4 * ip
->nominal_fname_len
+ 100);
8395 sprintf (line_directive_buf
, "# %d ", ip
->lineno
);
8396 line_end
= quote_string (line_directive_buf
+ strlen (line_directive_buf
),
8397 ip
->nominal_fname
, ip
->nominal_fname_len
);
8398 if (file_change
!= same_file
) {
8400 *line_end
++ = file_change
== enter_file
? '1' : '2';
8402 /* Tell cc1 if following text comes from a system header file. */
8403 if (ip
->system_header_p
) {
8407 #ifndef NO_IMPLICIT_EXTERN_C
8408 /* Tell cc1plus if following text should be treated as C. */
8409 if (ip
->system_header_p
== 2 && cplusplus
) {
8415 len
= line_end
- line_directive_buf
;
8416 check_expand (op
, len
+ 1);
8417 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
8419 bcopy ((const PTR
) line_directive_buf
, (PTR
) op
->bufp
, len
);
8421 op
->lineno
= ip
->lineno
;
8424 /* This structure represents one parsed argument in a macro call.
8425 `raw' points to the argument text as written (`raw_length' is its length).
8426 `expanded' points to the argument's macro-expansion
8427 (its length is `expand_length', and its allocated size is `expand_size').
8428 `stringified_length_bound' is an upper bound on the length
8429 the argument would have if stringified.
8430 `use_count' is the number of times this macro arg is substituted
8431 into the macro. If the actual use count exceeds 10,
8432 the value stored is 10.
8433 `free1' and `free2', if nonzero, point to blocks to be freed
8434 when the macro argument data is no longer needed. */
8437 U_CHAR
*raw
, *expanded
;
8438 int raw_length
, expand_length
, expand_size
;
8439 int stringified_length_bound
;
8440 U_CHAR
*free1
, *free2
;
8445 /* Expand a macro call.
8446 HP points to the symbol that is the macro being called.
8447 Put the result of expansion onto the input stack
8448 so that subsequent input by our caller will use it.
8450 If macro wants arguments, caller has already verified that
8451 an argument list follows; arguments come from the input stack. */
8454 macroexpand (hp
, op
)
8459 DEFINITION
*defn
= hp
->value
.defn
;
8460 register U_CHAR
*xbuf
;
8462 int start_line
= instack
[indepth
].lineno
;
8463 int rest_args
, rest_zero
;
8465 CHECK_DEPTH (return;);
8467 /* it might not actually be a macro. */
8468 if (hp
->type
!= T_MACRO
) {
8469 special_symbol (hp
, op
);
8473 /* This macro is being used inside a #if, which means it must be */
8474 /* recorded as a precondition. */
8475 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
8476 dump_single_macro (hp
, pcp_outfile
);
8478 nargs
= defn
->nargs
;
8482 struct argdata
*args
;
8483 int parse_error
= 0;
8485 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
8487 for (i
= 0; i
< nargs
; i
++) {
8488 args
[i
].raw
= (U_CHAR
*) "";
8489 args
[i
].expanded
= 0;
8490 args
[i
].raw_length
= args
[i
].expand_length
= args
[i
].expand_size
8491 = args
[i
].stringified_length_bound
= 0;
8492 args
[i
].free1
= args
[i
].free2
= 0;
8493 args
[i
].use_count
= 0;
8496 /* Parse all the macro args that are supplied. I counts them.
8497 The first NARGS args are stored in ARGS.
8498 The rest are discarded.
8499 If rest_args is set then we assume macarg absorbed the rest of the args.
8504 /* Discard the open-parenthesis or comma before the next arg. */
8505 ++instack
[indepth
].bufp
;
8508 if (i
< nargs
|| (nargs
== 0 && i
== 0)) {
8509 /* If we are working on last arg which absorbs rest of args... */
8510 if (i
== nargs
- 1 && defn
->rest_args
)
8512 parse_error
= macarg (&args
[i
], rest_args
);
8515 parse_error
= macarg (NULL_PTR
, 0);
8517 error_with_line (line_for_error (start_line
),
8518 "unterminated macro call");
8522 } while (*instack
[indepth
].bufp
!= ')');
8524 /* If we got one arg but it was just whitespace, call that 0 args. */
8526 register const U_CHAR
*bp
= args
[0].raw
;
8527 register const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
8528 /* cpp.texi says for foo ( ) we provide one argument.
8529 However, if foo wants just 0 arguments, treat this as 0. */
8531 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
8536 /* Don't output an error message if we have already output one for
8537 a parse error above. */
8539 if (nargs
== 0 && i
> 0) {
8541 error ("arguments given to macro `%s'", hp
->name
);
8542 } else if (i
< nargs
) {
8543 /* traditional C allows foo() if foo wants one argument. */
8544 if (nargs
== 1 && i
== 0 && traditional
)
8546 /* the rest args token is allowed to absorb 0 tokens */
8547 else if (i
== nargs
- 1 && defn
->rest_args
)
8549 else if (parse_error
)
8552 error ("macro `%s' used without args", hp
->name
);
8554 error ("macro `%s' used with just one arg", hp
->name
);
8556 error ("macro `%s' used with only %d args", hp
->name
, i
);
8557 } else if (i
> nargs
) {
8559 error ("macro `%s' used with too many (%d) args", hp
->name
, i
);
8562 /* Swallow the closeparen. */
8563 ++instack
[indepth
].bufp
;
8565 /* If macro wants zero args, we parsed the arglist for checking only.
8566 Read directly from the macro definition. */
8568 xbuf
= defn
->expansion
;
8569 xbuf_len
= defn
->length
;
8571 register U_CHAR
*exp
= defn
->expansion
;
8572 register int offset
; /* offset in expansion,
8573 copied a piece at a time */
8574 register int totlen
; /* total amount of exp buffer filled so far */
8576 register struct reflist
*ap
, *last_ap
;
8578 /* Macro really takes args. Compute the expansion of this call. */
8580 /* Compute length in characters of the macro's expansion.
8581 Also count number of times each arg is used. */
8582 xbuf_len
= defn
->length
;
8583 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
8584 if (ap
->stringify
&& args
[ap
->argno
].stringified_length_bound
== 0)
8585 /* macarg is not called for omitted arguments, as a result
8586 stringified_length_bound will be zero. We need to make
8587 enough space for "". */
8589 else if (ap
->stringify
)
8590 xbuf_len
+= args
[ap
->argno
].stringified_length_bound
;
8591 else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
)
8592 /* Add 4 for two newline-space markers to prevent
8593 token concatenation. */
8594 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
8596 /* We have an ordinary (expanded) occurrence of the arg.
8597 So compute its expansion, if we have not already. */
8598 if (args
[ap
->argno
].expanded
== 0) {
8600 obuf
= expand_to_temp_buffer (args
[ap
->argno
].raw
,
8601 args
[ap
->argno
].raw
+ args
[ap
->argno
].raw_length
,
8604 args
[ap
->argno
].expanded
= obuf
.buf
;
8605 args
[ap
->argno
].expand_length
= obuf
.bufp
- obuf
.buf
;
8606 args
[ap
->argno
].expand_size
= obuf
.length
;
8607 args
[ap
->argno
].free2
= obuf
.buf
;
8609 xbuf_len
+= args
[ap
->argno
].expand_length
;
8611 /* If the arg appears more than once, its later occurrences
8612 may have newline turned into backslash-'n', which is a
8613 factor of 2 expansion. */
8614 xbuf_len
+= 2 * args
[ap
->argno
].expand_length
;
8616 /* Add 4 for two newline-space markers to prevent
8617 token concatenation. */
8620 if (args
[ap
->argno
].use_count
< 10)
8621 args
[ap
->argno
].use_count
++;
8624 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
8626 /* Generate in XBUF the complete expansion
8627 with arguments substituted in.
8628 TOTLEN is the total size generated so far.
8629 OFFSET is the index in the definition
8630 of where we are copying from. */
8631 offset
= totlen
= 0;
8632 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
8633 last_ap
= ap
, ap
= ap
->next
) {
8634 register struct argdata
*arg
= &args
[ap
->argno
];
8635 int count_before
= totlen
;
8637 /* Add chars to XBUF. */
8638 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
8639 xbuf
[totlen
++] = exp
[offset
];
8641 /* If followed by an empty rest arg with concatenation,
8642 delete the last run of nonwhite chars. */
8643 if (rest_zero
&& totlen
> count_before
8644 && ((ap
->rest_args
&& ap
->raw_before
!= 0)
8645 || (last_ap
!= NULL
&& last_ap
->rest_args
8646 && last_ap
->raw_after
!= 0))) {
8647 /* Delete final whitespace. */
8648 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]]) {
8652 /* Delete the nonwhites before them. */
8653 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]]) {
8658 if (ap
->stringify
!= 0) {
8659 int arglen
= arg
->raw_length
;
8665 && (c
= arg
->raw
[i
], is_space
[c
]))
8668 && (c
= arg
->raw
[arglen
- 1], is_space
[c
]))
8671 xbuf
[totlen
++] = '\"'; /* insert beginning quote */
8672 for (; i
< arglen
; i
++) {
8676 /* Generate nothing for backslash-newline in a string. */
8677 if (c
== '\\' && arg
->raw
[i
+ 1] == '\n') {
8683 generate nothing for a stringified argument. */
8689 /* Internal sequences of whitespace are replaced by one space
8690 except within a string or char token. */
8693 while (is_space
[(c
= arg
->raw
[i
])])
8694 /* Newline markers can occur within a whitespace sequence;
8695 consider them part of the sequence. */
8696 i
+= (c
== '\n') + 1;
8707 else if (in_string
) {
8712 #ifdef MULTIBYTE_CHARS
8714 length
= local_mblen (arg
->raw
+ i
, arglen
- i
);
8717 bcopy (arg
->raw
+ i
, xbuf
+ totlen
, length
);
8724 } else if (c
== '\"' || c
== '\'')
8728 /* Escape double-quote, and backslashes in strings.
8729 Newlines in strings are best escaped as \n, since
8730 otherwise backslash-backslash-newline-newline is
8731 mishandled. The C Standard doesn't allow newlines in
8732 strings, so we can escape newlines as we please. */
8736 || (c
== '\n' ? (c
= 'n', 1) : 0))))
8737 xbuf
[totlen
++] = '\\';
8738 /* We used to output e.g. \008 for control characters here,
8739 but this doesn't conform to the C Standard.
8740 Just output the characters as-is. */
8744 xbuf
[totlen
++] = '\"'; /* insert ending quote */
8745 } else if (ap
->raw_before
!= 0 || ap
->raw_after
!= 0 || traditional
) {
8746 const U_CHAR
*p1
= arg
->raw
;
8747 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
8748 if (ap
->raw_before
!= 0) {
8749 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
8750 while (p1
!= l1
&& is_idchar
[*p1
])
8751 xbuf
[totlen
++] = *p1
++;
8752 /* Delete any no-reexpansion marker that follows
8753 an identifier at the beginning of the argument
8754 if the argument is concatenated with what precedes it. */
8755 if (p1
[0] == '\n' && p1
[1] == '-')
8757 } else if (!traditional
) {
8758 /* Ordinary expanded use of the argument.
8759 Put in newline-space markers to prevent token pasting. */
8760 xbuf
[totlen
++] = '\n';
8761 xbuf
[totlen
++] = ' ';
8763 if (ap
->raw_after
!= 0) {
8764 /* Arg is concatenated after: delete trailing whitespace,
8765 whitespace markers, and no-reexpansion markers. */
8767 if (is_space
[l1
[-1]]) l1
--;
8768 else if (l1
[-1] == '-') {
8769 const U_CHAR
*p2
= l1
- 1;
8770 /* If a `-' is preceded by an odd number of newlines then it
8771 and the last newline are a no-reexpansion marker. */
8772 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
8773 if ((l1
- 1 - p2
) & 1) {
8782 bcopy ((const PTR
) p1
, (PTR
) (xbuf
+ totlen
), l1
- p1
);
8784 if (!traditional
&& ap
->raw_after
== 0) {
8785 /* Ordinary expanded use of the argument.
8786 Put in newline-space markers to prevent token pasting. */
8787 xbuf
[totlen
++] = '\n';
8788 xbuf
[totlen
++] = ' ';
8791 /* Ordinary expanded use of the argument.
8792 Put in newline-space markers to prevent token pasting. */
8794 xbuf
[totlen
++] = '\n';
8795 xbuf
[totlen
++] = ' ';
8797 bcopy ((const PTR
) arg
->expanded
, (PTR
) (xbuf
+ totlen
),
8798 arg
->expand_length
);
8799 totlen
+= arg
->expand_length
;
8801 xbuf
[totlen
++] = '\n';
8802 xbuf
[totlen
++] = ' ';
8804 /* If a macro argument with newlines is used multiple times,
8805 then only expand the newlines once. This avoids creating output
8806 lines which don't correspond to any input line, which confuses
8808 if (arg
->use_count
> 1 && arg
->newlines
> 0) {
8809 /* Don't bother doing change_newlines for subsequent
8812 change_newlines (arg
);
8816 if (totlen
> xbuf_len
)
8820 /* If there is anything left of the definition after handling
8821 the arg list, copy that in too. */
8823 for (i
= offset
; i
< defn
->length
; i
++) {
8824 /* if we've reached the end of the macro */
8827 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
8828 && last_ap
->raw_after
!= 0))
8829 xbuf
[totlen
++] = exp
[i
];
8835 for (i
= 0; i
< nargs
; i
++) {
8836 if (args
[i
].free1
!= 0)
8837 free (args
[i
].free1
);
8838 if (args
[i
].free2
!= 0)
8839 free (args
[i
].free2
);
8843 xbuf
= defn
->expansion
;
8844 xbuf_len
= defn
->length
;
8847 /* Now put the expansion on the input stack
8848 so our caller will commence reading from it. */
8850 register FILE_BUF
*ip2
;
8852 ip2
= &instack
[++indepth
];
8855 ip2
->nominal_fname
= 0;
8856 ip2
->nominal_fname_len
= 0;
8858 /* This may not be exactly correct, but will give much better error
8859 messages for nested macro calls than using a line number of zero. */
8860 ip2
->lineno
= start_line
;
8862 ip2
->length
= xbuf_len
;
8864 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
8866 ip2
->if_stack
= if_stack
;
8867 ip2
->system_header_p
= 0;
8869 /* Recursive macro use sometimes works traditionally.
8870 #define foo(x,y) bar (x (y,0), y)
8874 hp
->type
= T_DISABLED
;
8878 /* Parse a macro argument and store the info on it into *ARGPTR.
8879 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8880 Return nonzero to indicate a syntax error. */
8883 macarg (argptr
, rest_args
)
8884 register struct argdata
*argptr
;
8887 FILE_BUF
*ip
= &instack
[indepth
];
8889 int lineno0
= ip
->lineno
;
8893 /* Try to parse as much of the argument as exists at this
8894 input stack level. */
8895 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
,
8896 &paren
, &ip
->lineno
, &comments
, rest_args
);
8898 /* If we find the end of the argument at this level,
8899 set up *ARGPTR to point at it in the input stack. */
8900 if (!(ip
->fname
!= 0 && (ip
->lineno
!= lineno0
|| comments
!= 0))
8901 && bp
!= ip
->buf
+ ip
->length
) {
8903 argptr
->raw
= ip
->bufp
;
8904 argptr
->raw_length
= bp
- ip
->bufp
;
8905 argptr
->newlines
= ip
->lineno
- lineno0
;
8909 /* This input stack level ends before the macro argument does.
8910 We must pop levels and keep parsing.
8911 Therefore, we must allocate a temporary buffer and copy
8912 the macro argument into it. */
8913 int bufsize
= bp
- ip
->bufp
;
8914 int extra
= ip
->lineno
- lineno0
;
8915 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
8916 int final_start
= 0;
8918 bcopy ((const PTR
) ip
->bufp
, (PTR
) buffer
, bufsize
);
8921 while (bp
== ip
->buf
+ ip
->length
) {
8922 if (instack
[indepth
].macro
== 0) {
8926 ip
->macro
->type
= T_MACRO
;
8928 free (ip
->free_ptr
);
8929 ip
= &instack
[--indepth
];
8930 lineno0
= ip
->lineno
;
8932 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, ip
->macro
, &paren
,
8933 &ip
->lineno
, &comments
, rest_args
);
8934 final_start
= bufsize
;
8935 bufsize
+= bp
- ip
->bufp
;
8936 extra
+= ip
->lineno
- lineno0
;
8937 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
8938 bcopy ((const PTR
) ip
->bufp
, (PTR
) (buffer
+ bufsize
- (bp
- ip
->bufp
)),
8943 /* Now, if arg is actually wanted, record its raw form,
8944 discarding comments and duplicating newlines in whatever
8945 part of it did not come from a macro expansion.
8946 EXTRA space has been preallocated for duplicating the newlines.
8947 FINAL_START is the index of the start of that part. */
8949 argptr
->raw
= buffer
;
8950 argptr
->raw_length
= bufsize
;
8951 argptr
->free1
= buffer
;
8952 argptr
->newlines
= ip
->lineno
- lineno0
;
8953 if ((argptr
->newlines
|| comments
) && ip
->fname
!= 0)
8956 discard_comments (argptr
->raw
+ final_start
,
8957 argptr
->raw_length
- final_start
,
8959 argptr
->raw
[argptr
->raw_length
] = 0;
8960 if (argptr
->raw_length
> bufsize
+ extra
)
8965 /* If we are not discarding this argument,
8966 macroexpand it and compute its length as stringified.
8967 All this info goes into *ARGPTR. */
8970 register const U_CHAR
*buf
, *lim
;
8971 register int totlen
;
8974 lim
= buf
+ argptr
->raw_length
;
8976 while (buf
!= lim
&& is_space
[*buf
])
8978 while (buf
!= lim
&& is_space
[lim
[-1]])
8980 totlen
= traditional
? 0 : 2; /* Count opening and closing quote. */
8981 while (buf
!= lim
) {
8982 register U_CHAR c
= *buf
++;
8984 /* Internal sequences of whitespace are replaced by one space
8985 in most cases, but not always. So count all the whitespace
8986 in case we need to keep it all. */
8989 SKIP_ALL_WHITE_SPACE (buf
);
8992 if (c
== '\"' || c
== '\\' || c
== '\n') /* escape these chars */
8995 argptr
->stringified_length_bound
= totlen
;
9000 /* Scan text from START (inclusive) up to LIMIT (exclusive),
9001 taken from the expansion of MACRO,
9002 counting parens in *DEPTHPTR,
9003 and return if reach LIMIT
9004 or before a `)' that would make *DEPTHPTR negative
9005 or before a comma when *DEPTHPTR is zero.
9006 Single and double quotes are matched and termination
9007 is inhibited within them. Comments also inhibit it.
9008 Value returned is pointer to stopping place.
9010 Increment *NEWLINES each time a newline is passed.
9011 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
9012 Set *COMMENTS to 1 if a comment is seen. */
9015 macarg1 (start
, limit
, macro
, depthptr
, newlines
, comments
, rest_args
)
9017 register const U_CHAR
*limit
;
9018 struct hashnode
*macro
;
9019 int *depthptr
, *newlines
, *comments
;
9022 register U_CHAR
*bp
= start
;
9024 while (bp
< limit
) {
9030 if (--(*depthptr
) < 0)
9034 /* Traditionally, backslash makes following char not special. */
9035 if (traditional
&& bp
+ 1 < limit
&& bp
[1] != '\n')
9045 newline_fix (bp
+ 1);
9048 for (bp
+= 2; bp
< limit
; bp
++) {
9051 else if (*bp
== '*') {
9052 if (bp
[-1] == '/' && warn_comments
)
9053 warning ("`/*' within comment");
9055 newline_fix (bp
+ 1);
9063 #ifdef MULTIBYTE_CHARS
9065 length
= local_mblen (bp
, limit
- bp
);
9071 } else if (bp
[1] == '/' && cplusplus_comments
) {
9073 for (bp
+= 2; bp
< limit
; bp
++) {
9078 if (*bp
== '\\' && bp
+ 1 < limit
&& bp
[1] == '\n')
9082 warning ("multiline `//' comment");
9087 #ifdef MULTIBYTE_CHARS
9089 length
= local_mblen (bp
, limit
- bp
);
9101 for (quotec
= *bp
++; bp
< limit
&& *bp
!= quotec
; bp
++) {
9106 while (*bp
== '\\' && bp
[1] == '\n') {
9110 } else if (*bp
== '\n') {
9111 if (warn_white_space
&& is_hor_space
[bp
[-1]] && ! macro
)
9112 warning ("white space at end of line in string");
9119 #ifdef MULTIBYTE_CHARS
9121 length
= local_mblen (bp
, limit
- bp
);
9130 /* if we've returned to lowest level and we aren't absorbing all args */
9131 if ((*depthptr
) == 0 && rest_args
== 0)
9141 /* Discard comments and duplicate newlines
9142 in the string of length LENGTH at START,
9143 except inside of string constants.
9144 The string is copied into itself with its beginning staying fixed.
9146 NEWLINES is the number of newlines that must be duplicated.
9147 We assume that that much extra space is available past the end
9151 discard_comments (start
, length
, newlines
)
9156 register U_CHAR
*ibp
;
9157 register U_CHAR
*obp
;
9158 register U_CHAR
*limit
;
9161 /* If we have newlines to duplicate, copy everything
9162 that many characters up. Then, in the second part,
9163 we will have room to insert the newlines
9165 NEWLINES may actually be too large, because it counts
9166 newlines in string constants, and we don't duplicate those.
9167 But that does no harm. */
9169 ibp
= start
+ length
;
9170 obp
= ibp
+ newlines
;
9172 while (limit
!= ibp
)
9176 ibp
= start
+ newlines
;
9177 limit
= start
+ length
+ newlines
;
9180 while (ibp
< limit
) {
9181 *obp
++ = c
= *ibp
++;
9184 /* Duplicate the newline. */
9198 /* Delete any comment. */
9199 if (cplusplus_comments
&& ibp
[0] == '/') {
9200 /* Comments are equivalent to spaces. */
9207 if (*ibp
== '\\' && ibp
+ 1 < limit
&& ibp
[1] == '\n')
9211 #ifdef MULTIBYTE_CHARS
9212 int length
= local_mblen (ibp
, limit
- ibp
);
9214 ibp
+= (length
- 1);
9221 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
9223 /* Comments are equivalent to spaces.
9224 For -traditional, a comment is equivalent to nothing. */
9229 while (++ibp
< limit
) {
9230 if (ibp
[0] == '*') {
9232 newline_fix (ibp
+ 1);
9233 if (ibp
[1] == '/') {
9240 #ifdef MULTIBYTE_CHARS
9241 int length
= local_mblen (ibp
, limit
- ibp
);
9243 ibp
+= (length
- 1);
9251 /* Notice and skip strings, so that we don't
9252 think that comments start inside them,
9253 and so we don't duplicate newlines in them. */
9256 while (ibp
< limit
) {
9257 *obp
++ = c
= *ibp
++;
9265 else if (c
== '\\') {
9266 if (ibp
< limit
&& *ibp
== '\n') {
9270 while (*ibp
== '\\' && ibp
[1] == '\n')
9278 #ifdef MULTIBYTE_CHARS
9281 length
= local_mblen (ibp
, limit
- ibp
);
9285 bcopy (ibp
, obp
, length
);
9302 /* Turn newlines to spaces in the macro argument ARG.
9303 Remove backslash-newline from string constants,
9304 and turn other newlines in string constants to backslash-'n'. */
9307 change_newlines (arg
)
9308 struct argdata
*arg
;
9310 U_CHAR
*start
= arg
->expanded
;
9311 int length
= arg
->expand_length
;
9312 register U_CHAR
*ibp
;
9313 register U_CHAR
*obp
;
9314 register const U_CHAR
*limit
;
9318 limit
= start
+ length
;
9321 while (ibp
< limit
) {
9322 *obp
++ = c
= *ibp
++;
9325 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9326 string. Skip past the newline and its duplicate.
9327 Put a space in the output. */
9338 /* Notice and skip strings, so that we don't delete newlines in them. */
9341 while (ibp
< limit
) {
9342 *obp
++ = c
= *ibp
++;
9345 else if (c
== '\\' && ibp
< limit
&& *ibp
== '\n')
9354 #ifdef MULTIBYTE_CHARS
9357 length
= local_mblen (ibp
, limit
- ibp
);
9361 bcopy (ibp
, obp
, length
);
9375 arg
->expand_length
= obp
- arg
->expanded
;
9377 if (start
!= arg
->expanded
)
9381 /* notice - output message to stderr */
9384 notice
VPROTO ((const char * msgid
, ...))
9386 #ifndef ANSI_PROTOTYPES
9391 VA_START (args
, msgid
);
9393 #ifndef ANSI_PROTOTYPES
9394 msgid
= va_arg (args
, const char *);
9397 vnotice (msgid
, args
);
9402 vnotice (msgid
, args
)
9406 vfprintf (stderr
, _(msgid
), args
);
9409 /* error - print error message and increment count of errors. */
9412 error
VPROTO ((const char * msgid
, ...))
9414 #ifndef ANSI_PROTOTYPES
9419 VA_START (args
, msgid
);
9421 #ifndef ANSI_PROTOTYPES
9422 msgid
= va_arg (args
, const char *);
9425 verror (msgid
, args
);
9430 verror (msgid
, args
)
9435 FILE_BUF
*ip
= NULL
;
9437 print_containing_files ();
9439 for (i
= indepth
; i
>= 0; i
--)
9440 if (instack
[i
].fname
!= NULL
) {
9446 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9447 ip
->nominal_fname_len
, stderr
);
9448 fprintf (stderr
, ":%d: ", ip
->lineno
);
9450 vnotice (msgid
, args
);
9451 fprintf (stderr
, "\n");
9455 /* Error including a message from `errno'. */
9458 error_from_errno (name
)
9463 FILE_BUF
*ip
= NULL
;
9465 print_containing_files ();
9467 for (i
= indepth
; i
>= 0; i
--)
9468 if (instack
[i
].fname
!= NULL
) {
9474 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9475 ip
->nominal_fname_len
, stderr
);
9476 fprintf (stderr
, ":%d: ", ip
->lineno
);
9479 fprintf (stderr
, "%s: %s\n", name
, xstrerror (e
));
9484 /* Print error message but don't count it. */
9487 warning
VPROTO ((const char * msgid
, ...))
9489 #ifndef ANSI_PROTOTYPES
9494 VA_START (args
, msgid
);
9496 #ifndef ANSI_PROTOTYPES
9497 msgid
= va_arg (args
, const char *);
9500 vwarning (msgid
, args
);
9505 vwarning (msgid
, args
)
9510 FILE_BUF
*ip
= NULL
;
9512 if (inhibit_warnings
)
9515 if (warnings_are_errors
)
9518 print_containing_files ();
9520 for (i
= indepth
; i
>= 0; i
--)
9521 if (instack
[i
].fname
!= NULL
) {
9527 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9528 ip
->nominal_fname_len
, stderr
);
9529 fprintf (stderr
, ":%d: ", ip
->lineno
);
9531 notice ("warning: ");
9532 vnotice (msgid
, args
);
9533 fprintf (stderr
, "\n");
9537 error_with_line
VPROTO ((int line
, const char * msgid
, ...))
9539 #ifndef ANSI_PROTOTYPES
9545 VA_START (args
, msgid
);
9547 #ifndef ANSI_PROTOTYPES
9548 line
= va_arg (args
, int);
9549 msgid
= va_arg (args
, const char *);
9552 verror_with_line (line
, msgid
, args
);
9558 verror_with_line (line
, msgid
, args
)
9564 FILE_BUF
*ip
= NULL
;
9566 print_containing_files ();
9568 for (i
= indepth
; i
>= 0; i
--)
9569 if (instack
[i
].fname
!= NULL
) {
9575 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9576 ip
->nominal_fname_len
, stderr
);
9577 fprintf (stderr
, ":%d: ", line
);
9579 vnotice (msgid
, args
);
9580 fprintf (stderr
, "\n");
9585 warning_with_line
VPROTO ((int line
, const char * msgid
, ...))
9587 #ifndef ANSI_PROTOTYPES
9593 VA_START (args
, msgid
);
9595 #ifndef ANSI_PROTOTYPES
9596 line
= va_arg (args
, int);
9597 msgid
= va_arg (args
, const char *);
9600 vwarning_with_line (line
, msgid
, args
);
9605 vwarning_with_line (line
, msgid
, args
)
9611 FILE_BUF
*ip
= NULL
;
9613 if (inhibit_warnings
)
9616 if (warnings_are_errors
)
9619 print_containing_files ();
9621 for (i
= indepth
; i
>= 0; i
--)
9622 if (instack
[i
].fname
!= NULL
) {
9628 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9629 ip
->nominal_fname_len
, stderr
);
9630 fprintf (stderr
, line
? ":%d: " : ": ", line
);
9632 notice ("warning: ");
9633 vnotice (msgid
, args
);
9634 fprintf (stderr
, "\n");
9637 /* Print an error message and maybe count it. */
9640 pedwarn
VPROTO ((const char * msgid
, ...))
9642 #ifndef ANSI_PROTOTYPES
9647 VA_START (args
, msgid
);
9649 #ifndef ANSI_PROTOTYPES
9650 msgid
= va_arg (args
, const char *);
9653 if (pedantic_errors
)
9654 verror (msgid
, args
);
9656 vwarning (msgid
, args
);
9661 pedwarn_with_line
VPROTO ((int line
, const char * msgid
, ...))
9663 #ifndef ANSI_PROTOTYPES
9669 VA_START (args
, msgid
);
9671 #ifndef ANSI_PROTOTYPES
9672 line
= va_arg (args
, int);
9673 msgid
= va_arg (args
, const char *);
9676 if (pedantic_errors
)
9677 verror_with_line (line
, msgid
, args
);
9679 vwarning_with_line (line
, msgid
, args
);
9683 /* Report a warning (or an error if pedantic_errors)
9684 giving specified file name and line number, not current. */
9687 pedwarn_with_file_and_line
VPROTO ((const char *file
, size_t file_len
, int line
,
9688 const char * msgid
, ...))
9690 #ifndef ANSI_PROTOTYPES
9698 if (!pedantic_errors
&& inhibit_warnings
)
9701 VA_START (args
, msgid
);
9703 #ifndef ANSI_PROTOTYPES
9704 file
= va_arg (args
, const char *);
9705 file_len
= va_arg (args
, size_t);
9706 line
= va_arg (args
, int);
9707 msgid
= va_arg (args
, const char *);
9711 fwrite (file
, sizeof file
[0], file_len
, stderr
);
9712 fprintf (stderr
, ":%d: ", line
);
9714 if (pedantic_errors
)
9716 if (!pedantic_errors
)
9717 notice ("warning: ");
9718 vnotice (msgid
, args
);
9720 fprintf (stderr
, "\n");
9724 pedwarn_strange_white_space (ch
)
9729 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9730 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9731 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9736 /* Print the file names and line numbers of the #include
9737 directives which led to the current file. */
9740 print_containing_files ()
9742 FILE_BUF
*ip
= NULL
;
9746 /* If stack of files hasn't changed since we last printed
9747 this info, don't repeat it. */
9748 if (last_error_tick
== input_file_stack_tick
)
9751 for (i
= indepth
; i
>= 0; i
--)
9752 if (instack
[i
].fname
!= NULL
) {
9757 /* Give up if we don't find a source file. */
9761 /* Find the other, outer source files. */
9762 for (i
--; i
>= 0; i
--)
9763 if (instack
[i
].fname
!= NULL
) {
9767 notice ( "In file included from ");
9769 notice (",\n from ");
9772 fwrite (ip
->nominal_fname
, sizeof ip
->nominal_fname
[0],
9773 ip
->nominal_fname_len
, stderr
);
9774 fprintf (stderr
, ":%d", ip
->lineno
);
9777 fprintf (stderr
, ":\n");
9779 /* Record we have printed the status as of this time. */
9780 last_error_tick
= input_file_stack_tick
;
9783 /* Return the line at which an error occurred.
9784 The error is not necessarily associated with the current spot
9785 in the input stack, so LINE says where. LINE will have been
9786 copied from ip->lineno for the current input level.
9787 If the current level is for a file, we return LINE.
9788 But if the current level is not for a file, LINE is meaningless.
9789 In that case, we return the lineno of the innermost file. */
9792 line_for_error (line
)
9798 for (i
= indepth
; i
>= 0; ) {
9799 if (instack
[i
].fname
!= 0)
9804 line1
= instack
[i
].lineno
;
9812 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9814 * As things stand, nothing is ever placed in the output buffer to be
9815 * removed again except when it's KNOWN to be part of an identifier,
9816 * so flushing and moving down everything left, instead of expanding,
9820 /* You might think void was cleaner for the return type,
9821 but that would get type mismatch in check_expand in strict ANSI. */
9824 grow_outbuf (obuf
, needed
)
9825 register FILE_BUF
*obuf
;
9826 register int needed
;
9831 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
9834 /* Make it at least twice as big as it is now. */
9836 /* Make it have at least 150% of the free space we will need. */
9837 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
9838 if (minsize
> obuf
->length
)
9839 obuf
->length
= minsize
;
9841 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
9843 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
9849 /* Symbol table for macro names and special symbols */
9852 * install a name in the main hash table, even if it is already there.
9853 * name stops with first non alphanumeric, except leading '#'.
9854 * caller must check against redefinition if that is desired.
9855 * delete_macro () removes things installed by install () in fifo order.
9856 * this is important because of the `defined' special symbol used
9857 * in #if, and also if pushdef/popdef directives are ever implemented.
9859 * If LEN is >= 0, it is the length of the name.
9860 * Otherwise, compute the length by scanning the entire name.
9862 * If HASH is >= 0, it is the precomputed hash code.
9863 * Otherwise, compute the hash code.
9867 install (name
, len
, type
, value
, hash
)
9870 enum node_type type
;
9874 register HASHNODE
*hp
;
9875 register int i
, bucket
;
9877 register const U_CHAR
*q
;
9881 while (is_idchar
[*q
])
9887 hash
= hashf (name
, len
, HASHSIZE
);
9889 i
= sizeof (HASHNODE
) + len
+ 1;
9890 hp
= (HASHNODE
*) xmalloc (i
);
9892 hp
->bucket_hdr
= &hashtab
[bucket
];
9893 hp
->next
= hashtab
[bucket
];
9894 hashtab
[bucket
] = hp
;
9896 if (hp
->next
!= NULL
)
9897 hp
->next
->prev
= hp
;
9900 hp
->value
.cpval
= value
;
9901 hp
->name
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
9904 for (i
= 0; i
< len
; i
++)
9911 * find the most recent hash node for name "name" (ending with first
9912 * non-identifier char) installed by install
9914 * If LEN is >= 0, it is the length of the name.
9915 * Otherwise, compute the length by scanning the entire name.
9917 * If HASH is >= 0, it is the precomputed hash code.
9918 * Otherwise, compute the hash code.
9922 lookup (name
, len
, hash
)
9927 register const U_CHAR
*bp
;
9928 register HASHNODE
*bucket
;
9931 for (bp
= name
; is_idchar
[*bp
]; bp
++) ;
9936 hash
= hashf (name
, len
, HASHSIZE
);
9938 bucket
= hashtab
[hash
];
9940 if (bucket
->length
== len
&& bcmp (bucket
->name
, name
, len
) == 0)
9942 bucket
= bucket
->next
;
9948 * Delete a hash node. Some weirdness to free junk from macros.
9949 * More such weirdness will have to be added if you define more hash
9950 * types that need it.
9953 /* Note that the DEFINITION of a macro is removed from the hash table
9954 but its storage is not freed. This would be a storage leak
9955 except that it is not reasonable to keep undefining and redefining
9956 large numbers of macros many times.
9957 In any case, this is necessary, because a macro can be #undef'd
9958 in the middle of reading the arguments to a call to it.
9959 If #undef freed the DEFINITION, that would crash. */
9966 if (hp
->prev
!= NULL
)
9967 hp
->prev
->next
= hp
->next
;
9968 if (hp
->next
!= NULL
)
9969 hp
->next
->prev
= hp
->prev
;
9971 /* Make sure that the bucket chain header that the deleted guy was
9972 on points to the right thing afterwards. */
9973 if (hp
== *hp
->bucket_hdr
)
9974 *hp
->bucket_hdr
= hp
->next
;
9977 if (hp
->type
== T_MACRO
) {
9978 DEFINITION
*d
= hp
->value
.defn
;
9979 struct reflist
*ap
, *nextap
;
9981 for (ap
= d
->pattern
; ap
!= NULL
; ap
= nextap
) {
9992 * return hash function on name. must be compatible with the one
9993 * computed a step at a time, elsewhere
9997 hashf (name
, len
, hashsize
)
9998 register const U_CHAR
*name
;
10002 register int r
= 0;
10005 r
= HASHSTEP (r
, *name
++);
10007 return MAKE_POS (r
) % hashsize
;
10011 /* Dump the definition of a single macro HP to OF. */
10014 dump_single_macro (hp
, of
)
10015 register HASHNODE
*hp
;
10018 register DEFINITION
*defn
= hp
->value
.defn
;
10019 struct reflist
*ap
;
10024 /* Print the definition of the macro HP. */
10026 fprintf (of
, "#define %s", hp
->name
);
10028 if (defn
->nargs
>= 0) {
10032 for (i
= 0; i
< defn
->nargs
; i
++) {
10033 dump_arg_n (defn
, i
, of
);
10034 if (i
+ 1 < defn
->nargs
)
10035 fprintf (of
, ", ");
10044 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
10045 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
, of
);
10046 offset
+= ap
->nchars
;
10047 if (!traditional
) {
10048 if (ap
->nchars
!= 0)
10050 if (ap
->stringify
) {
10051 switch (ap
->stringify
) {
10052 case SHARP_TOKEN
: fprintf (of
, "#"); break;
10053 case WHITE_SHARP_TOKEN
: fprintf (of
, "# "); break;
10054 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:"); break;
10055 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%: "); break;
10059 if (ap
->raw_before
!= 0) {
10061 switch (ap
->raw_before
) {
10062 case WHITE_SHARP_TOKEN
:
10063 case WHITE_PERCENT_COLON_TOKEN
:
10070 switch (ap
->raw_before
) {
10071 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10072 case WHITE_SHARP_TOKEN
: fprintf (of
, "## "); break;
10073 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10074 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%: "); break;
10081 dump_arg_n (defn
, ap
->argno
, of
);
10082 if (!traditional
&& ap
->raw_after
!= 0) {
10083 switch (ap
->raw_after
) {
10084 case SHARP_TOKEN
: fprintf (of
, "##"); break;
10085 case WHITE_SHARP_TOKEN
: fprintf (of
, " ##"); break;
10086 case PERCENT_COLON_TOKEN
: fprintf (of
, "%%:%%:"); break;
10087 case WHITE_PERCENT_COLON_TOKEN
: fprintf (of
, " %%:%%:"); break;
10093 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
, of
);
10094 fprintf (of
, "\n");
10097 /* Dump all macro definitions as #defines to stdout. */
10104 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
10105 register HASHNODE
*hp
;
10107 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
10108 if (hp
->type
== T_MACRO
)
10109 dump_single_macro (hp
, stdout
);
10114 /* Output to OF a substring of a macro definition.
10115 BASE is the beginning of the definition.
10116 Output characters START thru LENGTH.
10117 Unless traditional, discard newlines outside of strings, thus
10118 converting funny-space markers to ordinary spaces. */
10121 dump_defn_1 (base
, start
, length
, of
)
10122 const U_CHAR
*base
;
10127 const U_CHAR
*p
= base
+ start
;
10128 const U_CHAR
*limit
= base
+ start
+ length
;
10131 fwrite (p
, sizeof (*p
), length
, of
);
10133 while (p
< limit
) {
10134 if (*p
== '\"' || *p
=='\'') {
10135 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, NULL_PTR
,
10136 NULL_PTR
, NULL_PTR
);
10137 fwrite (p
, sizeof (*p
), p1
- p
, of
);
10148 /* Print the name of argument number ARGNUM of macro definition DEFN
10150 Recall that DEFN->args.argnames contains all the arg names
10151 concatenated in reverse order with comma-space in between. */
10154 dump_arg_n (defn
, argnum
, of
)
10159 register U_CHAR
*p
= defn
->args
.argnames
;
10160 while (argnum
+ 1 < defn
->nargs
) {
10161 p
= (U_CHAR
*) index ((char *) p
, ' ') + 1;
10165 while (*p
&& *p
!= ',') {
10171 /* Initialize syntactic classifications of characters. */
10174 initialize_char_syntax ()
10179 * Set up is_idchar and is_idstart tables. These should be
10180 * faster than saying (is_alpha (c) || c == '_'), etc.
10181 * Set up these things before calling any routines tthat
10184 for (i
= 'a'; i
<= 'z'; i
++) {
10185 is_idchar
[TOUPPER(i
)] = 1;
10187 is_idstart
[TOUPPER(i
)] = 1;
10190 for (i
= '0'; i
<= '9'; i
++)
10192 is_idchar
['_'] = 1;
10193 is_idstart
['_'] = 1;
10194 is_idchar
['$'] = 1;
10195 is_idstart
['$'] = 1;
10197 /* horizontal space table */
10198 is_hor_space
[' '] = 1;
10199 is_hor_space
['\t'] = 1;
10200 is_hor_space
['\v'] = 1;
10201 is_hor_space
['\f'] = 1;
10202 is_hor_space
['\r'] = 1;
10205 is_space
['\t'] = 1;
10206 is_space
['\v'] = 1;
10207 is_space
['\f'] = 1;
10208 is_space
['\n'] = 1;
10209 is_space
['\r'] = 1;
10212 /* Initialize the built-in macros. */
10215 initialize_builtins (inp
, outp
)
10219 install ((const U_CHAR
*) "__LINE__", -1, T_SPECLINE
, NULL_PTR
, -1);
10220 install ((const U_CHAR
*) "__DATE__", -1, T_DATE
, NULL_PTR
, -1);
10221 install ((const U_CHAR
*) "__FILE__", -1, T_FILE
, NULL_PTR
, -1);
10222 install ((const U_CHAR
*) "__BASE_FILE__", -1, T_BASE_FILE
, NULL_PTR
, -1);
10223 install ((const U_CHAR
*) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, NULL_PTR
, -1);
10224 install ((const U_CHAR
*) "__VERSION__", -1, T_VERSION
, NULL_PTR
, -1);
10225 #ifndef NO_BUILTIN_SIZE_TYPE
10226 install ((const U_CHAR
*) "__SIZE_TYPE__", -1, T_SIZE_TYPE
, NULL_PTR
, -1);
10228 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10229 install ((const U_CHAR
*) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, NULL_PTR
, -1);
10231 install ((const U_CHAR
*) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, NULL_PTR
, -1);
10232 install ((const U_CHAR
*) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
,
10234 install ((const U_CHAR
*) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
,
10236 install ((const U_CHAR
*) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE
,
10238 install ((const U_CHAR
*) "__TIME__", -1, T_TIME
, NULL_PTR
, -1);
10239 if (!traditional
) {
10240 install ((const U_CHAR
*) "__STDC__", -1, T_CONST
, "1", -1);
10241 install ((const U_CHAR
*) "__STDC_VERSION__", -1, T_CONST
, "199409L", -1);
10243 /* This is supplied using a -D by the compiler driver
10244 so that it is present only when truly compiling with GNU C. */
10245 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10246 install ((const U_CHAR
*) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST
, "1", -1);
10250 char directive
[2048];
10251 U_CHAR
*udirective
= (U_CHAR
*) directive
;
10252 register struct directive
*dp
= &directive_table
[0];
10253 struct tm
*timebuf
= timestamp ();
10255 sprintf (directive
, " __BASE_FILE__ \"%s\"\n",
10256 instack
[0].nominal_fname
);
10257 output_line_directive (inp
, outp
, 0, same_file
);
10258 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10261 sprintf (directive
, " __VERSION__ \"%s\"\n", version_string
);
10262 output_line_directive (inp
, outp
, 0, same_file
);
10263 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10266 #ifndef NO_BUILTIN_SIZE_TYPE
10267 sprintf (directive
, " __SIZE_TYPE__ %s\n", SIZE_TYPE
);
10268 output_line_directive (inp
, outp
, 0, same_file
);
10269 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10273 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10274 sprintf (directive
, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE
);
10275 output_line_directive (inp
, outp
, 0, same_file
);
10276 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10280 sprintf (directive
, " __WCHAR_TYPE__ %s\n", wchar_type
);
10281 output_line_directive (inp
, outp
, 0, same_file
);
10282 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10285 sprintf (directive
, " __DATE__ \"%s %2d %4d\"\n",
10286 monthnames
[timebuf
->tm_mon
],
10287 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
10288 output_line_directive (inp
, outp
, 0, same_file
);
10289 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10292 sprintf (directive
, " __TIME__ \"%02d:%02d:%02d\"\n",
10293 timebuf
->tm_hour
, timebuf
->tm_min
, timebuf
->tm_sec
);
10294 output_line_directive (inp
, outp
, 0, same_file
);
10295 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10300 sprintf (directive
, " __STDC__ 1");
10301 output_line_directive (inp
, outp
, 0, same_file
);
10302 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10307 sprintf (directive
, " __OBJC__ 1");
10308 output_line_directive (inp
, outp
, 0, same_file
);
10309 pass_thru_directive (udirective
, &udirective
[strlen (directive
)],
10316 * process a given definition string, for initialization
10317 * If STR is just an identifier, define it with value 1.
10318 * If STR has anything after the identifier, then it should
10319 * be identifier=definition.
10323 make_definition (str
)
10327 struct directive
*kt
;
10330 p
= buf
= (U_CHAR
*) str
;
10331 if (!is_idstart
[*p
]) {
10332 error ("malformed option `-D %s'", str
);
10335 while (is_idchar
[*++p
])
10338 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
10341 p
= (U_CHAR
*) str
; /* Error */
10344 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
10345 strcpy ((char *)buf
, str
);
10346 strcat ((char *)buf
, " 1");
10347 } else if (*p
!= '=') {
10348 error ("malformed option `-D %s'", str
);
10352 /* Copy the entire option so we can modify it. */
10353 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
10354 strncpy ((char *) buf
, str
, p
- (U_CHAR
*) str
);
10355 /* Change the = to a space. */
10356 buf
[p
- (U_CHAR
*) str
] = ' ';
10357 /* Scan for any backslash-newline and remove it. */
10359 q
= &buf
[p
- (U_CHAR
*) str
];
10361 if (*p
== '\"' || *p
== '\'') {
10362 int unterminated
= 0;
10363 const U_CHAR
*p1
= skip_quoted_string (p
, p
+ strlen ((char *) p
), 0,
10364 NULL_PTR
, NULL_PTR
, &unterminated
);
10368 if (*p
== '\\' && p
[1] == '\n')
10370 else if (*p
== '\n')
10379 } else if (*p
== '\\' && p
[1] == '\n')
10381 /* Change newline chars into newline-markers. */
10382 else if (*p
== '\n')
10394 ip
= &instack
[++indepth
];
10395 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10396 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10398 ip
->buf
= ip
->bufp
= buf
;
10399 ip
->length
= strlen ((char *) buf
);
10403 ip
->if_stack
= if_stack
;
10404 ip
->system_header_p
= 0;
10406 for (kt
= directive_table
; kt
->type
!= T_DEFINE
; kt
++)
10409 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10410 do_define (buf
, buf
+ strlen ((char *) buf
), NULL_PTR
, kt
);
10414 /* JF, this does the work for the -U option */
10417 make_undef (str
, op
)
10422 struct directive
*kt
;
10424 ip
= &instack
[++indepth
];
10425 ip
->nominal_fname
= ip
->fname
= "*undef*";
10426 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10428 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
10429 ip
->length
= strlen (str
);
10433 ip
->if_stack
= if_stack
;
10434 ip
->system_header_p
= 0;
10436 for (kt
= directive_table
; kt
->type
!= T_UNDEF
; kt
++)
10439 do_undef ((U_CHAR
*) str
, (U_CHAR
*) str
+ strlen (str
), op
, kt
);
10443 /* Process the string STR as if it appeared as the body of a #assert.
10444 OPTION is the option name for which STR was the argument. */
10447 make_assertion (option
, str
)
10448 const char *option
;
10452 struct directive
*kt
;
10453 U_CHAR
*buf
, *p
, *q
;
10455 /* Copy the entire option so we can modify it. */
10456 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
10457 strcpy ((char *) buf
, str
);
10458 /* Scan for any backslash-newline and remove it. */
10461 if (*p
== '\\' && p
[1] == '\n')
10469 if (!is_idstart
[*p
]) {
10470 error ("malformed option `%s %s'", option
, str
);
10473 while (is_idchar
[*++p
])
10475 SKIP_WHITE_SPACE (p
);
10476 if (! (*p
== 0 || *p
== '(')) {
10477 error ("malformed option `%s %s'", option
, str
);
10481 ip
= &instack
[++indepth
];
10482 ip
->nominal_fname
= ip
->fname
= "*Initialization*";
10483 ip
->nominal_fname_len
= strlen (ip
->nominal_fname
);
10485 ip
->buf
= ip
->bufp
= buf
;
10486 ip
->length
= strlen ((char *) buf
);
10490 ip
->if_stack
= if_stack
;
10491 ip
->system_header_p
= 0;
10493 for (kt
= directive_table
; kt
->type
!= T_ASSERT
; kt
++)
10496 /* Pass NULL as output ptr to do_define since we KNOW it never does
10498 do_assert (buf
, buf
+ strlen ((char *) buf
) , NULL_PTR
, kt
);
10502 /* The previous include prefix, if any, is PREV_FILE_NAME.
10503 Translate any pathnames with COMPONENT.
10504 Allocate a new include prefix whose name is the
10505 simplified concatenation of PREFIX and NAME,
10506 with a trailing / added if needed.
10507 But return 0 if the include prefix should be ignored,
10508 e.g. because it is a duplicate of PREV_FILE_NAME. */
10510 static struct file_name_list
*
10511 new_include_prefix (prev_file_name
, component
, prefix
, name
)
10512 struct file_name_list
*prev_file_name
;
10513 const char *component
;
10514 const char *prefix
;
10518 fatal ("Directory name missing after command line option");
10521 /* Ignore the empty string. */
10524 prefix
= update_path (prefix
, component
);
10525 name
= update_path (name
, component
);
10528 struct file_name_list
*dir
10529 = ((struct file_name_list
*)
10530 xmalloc (sizeof (struct file_name_list
)
10531 + strlen (prefix
) + strlen (name
) + 2));
10533 strcpy (dir
->fname
, prefix
);
10534 strcat (dir
->fname
, name
);
10535 len
= simplify_filename (dir
->fname
);
10537 /* Convert directory name to a prefix. */
10538 if (len
&& dir
->fname
[len
- 1] != DIR_SEPARATOR
) {
10539 if (len
== 1 && dir
->fname
[len
- 1] == '.')
10543 /* must be '/', hack_vms_include_specification triggers on it. */
10544 dir
->fname
[len
++] = '/';
10546 dir
->fname
[len
++] = DIR_SEPARATOR
;
10548 dir
->fname
[len
] = 0;
10551 /* Ignore a directory whose name matches the previous one. */
10552 if (prev_file_name
&& !strcmp (prev_file_name
->fname
, dir
->fname
)) {
10553 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10554 if (!first_bracket_include
)
10555 first_bracket_include
= prev_file_name
;
10561 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10563 /* Add a trailing "." if there is a filename. This increases the number
10564 of systems that can stat directories. We remove it below. */
10567 dir
->fname
[len
] = '.';
10568 dir
->fname
[len
+ 1] = 0;
10571 /* Ignore a nonexistent directory. */
10572 if (stat (len
? dir
->fname
: ".", &dir
->st
) != 0) {
10573 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
10574 error_from_errno (dir
->fname
);
10580 dir
->fname
[len
] = 0;
10582 /* Ignore a directory whose identity matches the previous one. */
10584 && INO_T_EQ (prev_file_name
->st
.st_ino
, dir
->st
.st_ino
)
10585 && prev_file_name
->st
.st_dev
== dir
->st
.st_dev
) {
10586 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10587 if (!first_bracket_include
)
10588 first_bracket_include
= prev_file_name
;
10595 dir
->c_system_include_path
= 0;
10596 dir
->got_name_map
= 0;
10602 /* Append a chain of `struct file_name_list's
10603 to the end of the main include chain.
10604 FIRST is the beginning of the chain to append, and LAST is the end. */
10607 append_include_chain (first
, last
)
10608 struct file_name_list
*first
, *last
;
10610 struct file_name_list
*dir
;
10612 if (!first
|| !last
)
10618 last_include
->next
= first
;
10620 if (first_bracket_include
== 0)
10621 first_bracket_include
= first
;
10623 for (dir
= first
; ; dir
= dir
->next
) {
10624 int len
= strlen (dir
->fname
) + INCLUDE_LEN_FUDGE
;
10625 if (len
> max_include_len
)
10626 max_include_len
= len
;
10632 last_include
= last
;
10635 /* Place into DST a representation of the file named SRC that is suitable
10636 for `make'. Do not null-terminate DST. Return its length. */
10638 quote_string_for_make (dst
, src
)
10642 const char *p
= src
;
10653 /* GNU make uses a weird quoting scheme for white space.
10654 A space or tab preceded by 2N+1 backslashes represents
10655 N backslashes followed by space; a space or tab
10656 preceded by 2N backslashes represents N backslashes at
10657 the end of a file name; and backslashes in other
10658 contexts should not be doubled. */
10660 for (q
= p
- 1; src
< q
&& q
[-1] == '\\'; q
--)
10672 goto ordinary_char
;
10678 /* Fall through. This can mishandle things like "$(" but
10679 there's no easy fix. */
10682 /* This can mishandle characters in the string "\0\n%*?[\\~";
10683 exactly which chars are mishandled depends on the `make' version.
10684 We know of no portable solution for this;
10685 even GNU make 3.76.1 doesn't solve the problem entirely.
10686 (Also, '\0' is mishandled due to our calling conventions.) */
10696 /* Add output to `deps_buffer' for the -M switch.
10697 STRING points to the text to be output.
10698 SPACER is ':' for targets, ' ' for dependencies. */
10701 deps_output (string
, spacer
)
10702 const char *string
;
10705 int size
= quote_string_for_make ((char *) 0, string
);
10710 #ifndef MAX_OUTPUT_COLUMNS
10711 #define MAX_OUTPUT_COLUMNS 72
10713 if (MAX_OUTPUT_COLUMNS
- 1 /*spacer*/ - 2 /*` \'*/ < deps_column
+ size
10714 && 1 < deps_column
) {
10715 bcopy (" \\\n ", &deps_buffer
[deps_size
], 4);
10722 if (deps_size
+ 2 * size
+ 8 > deps_allocated_size
) {
10723 deps_allocated_size
= (deps_size
+ 2 * size
+ 50) * 2;
10724 deps_buffer
= xrealloc (deps_buffer
, deps_allocated_size
);
10726 if (spacer
== ' ') {
10727 deps_buffer
[deps_size
++] = ' ';
10730 quote_string_for_make (&deps_buffer
[deps_size
], string
);
10732 deps_column
+= size
;
10733 if (spacer
== ':') {
10734 deps_buffer
[deps_size
++] = ':';
10737 deps_buffer
[deps_size
] = 0;
10741 fatal
VPROTO ((const char * msgid
, ...))
10743 #ifndef ANSI_PROTOTYPES
10744 const char * msgid
;
10748 fprintf (stderr
, "%s: ", progname
);
10749 VA_START (args
, msgid
);
10751 #ifndef ANSI_PROTOTYPES
10752 msgid
= va_arg (args
, const char *);
10754 vnotice (msgid
, args
);
10756 fprintf (stderr
, "\n");
10757 exit (FATAL_EXIT_CODE
);
10760 /* More 'friendly' abort that prints the line and file.
10761 config.h can #define abort fancy_abort if you like that sort of thing. */
10766 fatal ("Internal gcc abort.");
10770 perror_with_name (name
)
10773 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, xstrerror (errno
));
10778 pfatal_with_name (name
)
10781 perror_with_name (name
);
10785 exit (FATAL_EXIT_CODE
);
10789 /* Handler for SIGPIPE. */
10792 pipe_closed (signo
)
10793 /* If this is missing, some compilers complain. */
10794 int signo ATTRIBUTE_UNUSED
;
10796 fatal ("output pipe has been closed");
10802 fatal ("Memory exhausted.");
10807 /* Under VMS we need to fix up the "include" specification filename.
10809 Rules for possible conversions
10811 fullname tried paths
10814 ./dir/name [.dir]name
10816 /name [000000]name, name
10817 dir/name dir:[000000]name, dir:name, dir/name
10818 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10819 path:/name path:[000000]name, path:name
10820 path:/dir/name path:[000000.dir]name, path:[dir]name
10821 path:dir/name path:[dir]name
10822 [path]:[dir]name [path.dir]name
10823 path/[dir]name [path.dir]name
10825 The path:/name input is constructed when expanding <> includes.
10827 return 1 if name was changed, 0 else. */
10830 hack_vms_include_specification (fullname
, vaxc_include
)
10834 register char *basename
, *unixname
, *local_ptr
, *first_slash
;
10835 int f
, check_filename_before_returning
, must_revert
;
10838 check_filename_before_returning
= 0;
10840 /* See if we can find a 1st slash. If not, there's no path information. */
10841 first_slash
= index (fullname
, '/');
10842 if (first_slash
== 0)
10843 return 0; /* Nothing to do!!! */
10845 /* construct device spec if none given. */
10847 if (index (fullname
, ':') == 0)
10850 /* If fullname has a slash, take it as device spec. */
10852 if (first_slash
== fullname
)
10854 first_slash
= index (fullname
+1, '/'); /* 2nd slash ? */
10856 *first_slash
= ':'; /* make device spec */
10857 for (basename
= fullname
; *basename
!= 0; basename
++)
10858 *basename
= *(basename
+1); /* remove leading slash */
10860 else if ((first_slash
[-1] != '.') /* keep ':/', './' */
10861 && (first_slash
[-1] != ':')
10862 && (first_slash
[-1] != ']')) /* or a vms path */
10864 *first_slash
= ':';
10866 else if ((first_slash
[1] == '[') /* skip './' in './[dir' */
10867 && (first_slash
[-1] == '.'))
10871 /* Get part after first ':' (basename[-1] == ':')
10872 or last '/' (basename[-1] == '/'). */
10874 basename
= base_name (fullname
);
10877 * Check if we have a vax-c style '#include filename'
10878 * and add the missing .h
10881 if (vaxc_include
&& !index (basename
,'.'))
10882 strcat (basename
, ".h");
10884 local_ptr
= Local
; /* initialize */
10886 /* We are trying to do a number of things here. First of all, we are
10887 trying to hammer the filenames into a standard format, such that later
10888 processing can handle them.
10890 If the file name contains something like [dir.], then it recognizes this
10891 as a root, and strips the ".]". Later processing will add whatever is
10892 needed to get things working properly.
10894 If no device is specified, then the first directory name is taken to be
10895 a device name (or a rooted logical). */
10897 /* Point to the UNIX filename part (which needs to be fixed!)
10898 but skip vms path information.
10899 [basename != fullname since first_slash != 0]. */
10901 if ((basename
[-1] == ':') /* vms path spec. */
10902 || (basename
[-1] == ']')
10903 || (basename
[-1] == '>'))
10904 unixname
= basename
;
10906 unixname
= fullname
;
10908 if (*unixname
== '/')
10911 /* If the directory spec is not rooted, we can just copy
10912 the UNIX filename part and we are done. */
10914 if (((basename
- fullname
) > 1)
10915 && ( (basename
[-1] == ']')
10916 || (basename
[-1] == '>')))
10918 if (basename
[-2] != '.')
10921 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10922 -> PATH]:/name (basename = '/name', unixname = 'name')
10923 We strip the `]', and then splice the two parts of the name in the
10924 usual way. Given the default locations for include files in cccp.c,
10925 we will only use this code if the user specifies alternate locations
10926 with the /include (-I) switch on the command line. */
10928 basename
-= 1; /* Strip "]" */
10929 unixname
--; /* backspace */
10934 /* The VMS part has a ".]" at the end, and this will not do. Later
10935 processing will add a second directory spec, and this would be a syntax
10936 error. Thus we strip the ".]", and thus merge the directory specs.
10937 We also backspace unixname, so that it points to a '/'. This inhibits the
10938 generation of the 000000 root directory spec (which does not belong here
10941 basename
-= 2; /* Strip ".]" */
10942 unixname
--; /* backspace */
10950 /* We drop in here if there is no VMS style directory specification yet.
10951 If there is no device specification either, we make the first dir a
10952 device and try that. If we do not do this, then we will be essentially
10953 searching the users default directory (as if they did a #include "asdf.h").
10955 Then all we need to do is to push a '[' into the output string. Later
10956 processing will fill this in, and close the bracket. */
10958 if ((unixname
!= fullname
) /* vms path spec found. */
10959 && (basename
[-1] != ':'))
10960 *local_ptr
++ = ':'; /* dev not in spec. take first dir */
10962 *local_ptr
++ = '['; /* Open the directory specification */
10965 if (unixname
== fullname
) /* no vms dir spec. */
10968 if ((first_slash
!= 0) /* unix dir spec. */
10969 && (*unixname
!= '/') /* not beginning with '/' */
10970 && (*unixname
!= '.')) /* or './' or '../' */
10971 *local_ptr
++ = '.'; /* dir is local ! */
10974 /* at this point we assume that we have the device spec, and (at least
10975 the opening "[" for a directory specification. We may have directories
10978 If there are no other slashes then the filename will be
10979 in the "root" directory. Otherwise, we need to add
10980 directory specifications. */
10982 if (index (unixname
, '/') == 0)
10984 /* if no directories specified yet and none are following. */
10985 if (local_ptr
[-1] == '[')
10987 /* Just add "000000]" as the directory string */
10988 strcpy (local_ptr
, "000000]");
10989 local_ptr
+= strlen (local_ptr
);
10990 check_filename_before_returning
= 1; /* we might need to fool with this later */
10996 /* As long as there are still subdirectories to add, do them. */
10997 while (index (unixname
, '/') != 0)
10999 /* If this token is "." we can ignore it
11000 if it's not at the beginning of a path. */
11001 if ((unixname
[0] == '.') && (unixname
[1] == '/'))
11003 /* remove it at beginning of path. */
11004 if ( ((unixname
== fullname
) /* no device spec */
11005 && (fullname
+2 != basename
)) /* starts with ./ */
11007 || ((basename
[-1] == ':') /* device spec */
11008 && (unixname
-1 == basename
))) /* and ./ afterwards */
11009 *local_ptr
++ = '.'; /* make '[.' start of path. */
11014 /* Add a subdirectory spec. Do not duplicate "." */
11015 if ( local_ptr
[-1] != '.'
11016 && local_ptr
[-1] != '['
11017 && local_ptr
[-1] != '<')
11018 *local_ptr
++ = '.';
11020 /* If this is ".." then the spec becomes "-" */
11021 if ( (unixname
[0] == '.')
11022 && (unixname
[1] == '.')
11023 && (unixname
[2] == '/'))
11025 /* Add "-" and skip the ".." */
11026 if ((local_ptr
[-1] == '.')
11027 && (local_ptr
[-2] == '['))
11028 local_ptr
--; /* prevent [.- */
11029 *local_ptr
++ = '-';
11034 /* Copy the subdirectory */
11035 while (*unixname
!= '/')
11036 *local_ptr
++= *unixname
++;
11038 unixname
++; /* Skip the "/" */
11041 /* Close the directory specification */
11042 if (local_ptr
[-1] == '.') /* no trailing periods */
11045 if (local_ptr
[-1] == '[') /* no dir needed */
11048 *local_ptr
++ = ']';
11051 /* Now add the filename. */
11054 *local_ptr
++ = *unixname
++;
11057 /* Now append it to the original VMS spec. */
11059 strcpy ((must_revert
==1)?fullname
:basename
, Local
);
11061 /* If we put a [000000] in the filename, try to open it first. If this fails,
11062 remove the [000000], and return that name. This provides flexibility
11063 to the user in that they can use both rooted and non-rooted logical names
11064 to point to the location of the file. */
11066 if (check_filename_before_returning
)
11068 f
= open (fullname
, O_RDONLY
, 0666);
11071 /* The file name is OK as it is, so return it as is. */
11076 /* The filename did not work. Try to remove the [000000] from the name,
11079 basename
= index (fullname
, '[');
11080 local_ptr
= index (fullname
, ']') + 1;
11081 strcpy (basename
, local_ptr
); /* this gets rid of it */
11091 /* The following wrapper functions supply additional arguments to the VMS
11092 I/O routines to optimize performance with file handling. The arguments
11094 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11095 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11096 "fop=tef"- Truncate unused portions of file when closing file.
11097 "shr=nil"- Disallow file sharing while file is open. */
11100 VMS_freopen (fname
, type
, oldfile
)
11105 #undef freopen /* Get back the real freopen routine. */
11106 if (strcmp (type
, "w") == 0)
11107 return freopen (fname
, type
, oldfile
,
11108 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11109 return freopen (fname
, type
, oldfile
, "mbc=16");
11113 VMS_fopen (fname
, type
)
11117 #undef fopen /* Get back the real fopen routine. */
11118 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11119 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11120 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11121 FILE *(*vmslib_fopen
)() = (FILE *(*)()) fopen
;
11124 return (*vmslib_fopen
) (fname
, type
, "mbc=32",
11125 "deq=64", "fop=tef", "shr=nil");
11127 return (*vmslib_fopen
) (fname
, type
, "mbc=32");
11131 VMS_open (fname
, flags
, prot
)
11136 #undef open /* Get back the real open routine. */
11137 return open (fname
, flags
, prot
, "mbc=16", "deq=64", "fop=tef");
11140 /* more VMS hackery */
11144 extern unsigned long SYS$
PARSE(), SYS$
SEARCH();
11146 /* Work around another library bug. If a file is located via a searchlist,
11147 and if the device it's on is not the same device as the one specified
11148 in the first element of that searchlist, then both stat() and fstat()
11149 will fail to return info about it. `errno' will be set to EVMSERR, and
11150 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11151 We can get around this by fully parsing the filename and then passing
11152 that absolute name to stat().
11154 Without this fix, we can end up failing to find header files, which is
11155 bad enough, but then compounding the problem by reporting the reason for
11156 failure as "normal successful completion." */
11158 #undef fstat /* Get back to the library version. */
11161 VMS_fstat (fd
, statbuf
)
11163 struct stat
*statbuf
;
11165 int result
= fstat (fd
, statbuf
);
11170 char nambuf
[NAM$C_MAXRSS
+1];
11172 if ((fp
= fdopen (fd
, "r")) != 0 && fgetname (fp
, nambuf
) != 0)
11173 result
= VMS_stat (nambuf
, statbuf
);
11174 /* No fclose(fp) here; that would close(fd) as well. */
11181 VMS_stat (name
, statbuf
)
11183 struct stat
*statbuf
;
11185 int result
= stat (name
, statbuf
);
11191 char exp_nam
[NAM$C_MAXRSS
+1], /* expanded name buffer for SYS$PARSE */
11192 res_nam
[NAM$C_MAXRSS
+1]; /* resultant name buffer for SYS$SEARCH */
11195 fab
.fab$l_fna
= (char *) name
;
11196 fab
.fab$b_fns
= (unsigned char) strlen (name
);
11197 fab
.fab$l_nam
= (void *) &nam
;
11199 nam
.nam$l_esa
= exp_nam
, nam
.nam$b_ess
= sizeof exp_nam
- 1;
11200 nam
.nam$l_rsa
= res_nam
, nam
.nam$b_rss
= sizeof res_nam
- 1;
11201 nam
.nam$b_nop
= NAM$M_PWD
| NAM$M_NOCONCEAL
;
11202 if (SYS$
PARSE (&fab
) & 1)
11204 if (SYS$
SEARCH (&fab
) & 1)
11206 res_nam
[nam
.nam$b_rsl
] = '\0';
11207 result
= stat (res_nam
, statbuf
);
11209 /* Clean up searchlist context cached by the system. */
11210 nam
.nam$b_nop
= NAM$M_SYNCHK
;
11211 fab
.fab$l_fna
= 0, fab
.fab$b_fns
= 0;
11212 (void) SYS$
PARSE (&fab
);
11220 VMS_fwrite (ptr
, size
, nitems
, stream
)
11226 /* VMS fwrite has undesirable results
11227 if STREAM happens to be a record oriented file.
11228 Work around this problem by writing each character individually. */
11229 char const *p
= ptr
;
11230 size_t bytes
= size
* nitems
;
11231 char *lim
= p
+ bytes
;
11234 if (putc (*p
++, stream
) == EOF
)