allow all arm targets to use -mstructure-size-boundary=XX
[official-gcc.git] / gcc / cccp.c
blobb5f31a1ab2a4aac3beb4caa72ba2dac2b0079b29
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
9 later version.
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. */
21 #include "config.h"
23 #include "system.h"
24 #include <signal.h>
26 #ifdef HAVE_SYS_RESOURCE_H
27 # include <sys/resource.h>
28 #endif
30 typedef unsigned char U_CHAR;
32 #include "pcp.h"
33 #include "intl.h"
34 #include "prefix.h"
36 #ifdef MULTIBYTE_CHARS
37 #include "mbchar.h"
38 #include <locale.h>
39 #endif /* MULTIBYTE_CHARS */
41 #ifndef GET_ENV_PATH_LIST
42 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
43 #endif
45 #ifndef STANDARD_INCLUDE_DIR
46 # define STANDARD_INCLUDE_DIR "/usr/include"
47 #endif
49 /* By default, colon separates directories in a path. */
50 #ifndef PATH_SEPARATOR
51 # define PATH_SEPARATOR ':'
52 #endif
54 /* By default, a slash separates directory names. */
55 #ifndef DIR_SEPARATOR
56 # define DIR_SEPARATOR '/'
57 #endif
59 /* By default, the suffix for object files is ".o". */
60 #ifdef OBJECT_SUFFIX
61 # define HAVE_OBJECT_SUFFIX
62 #else
63 # define OBJECT_SUFFIX ".o"
64 #endif
66 /* VMS-specific definitions */
67 #ifdef VMS
68 #include <descrip.h>
69 #include <ssdef.h>
70 #include <syidef.h>
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 */
85 #endif /* VMS */
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
91 #endif
93 #ifndef INO_T_EQ
94 #define INO_T_EQ(a, b) ((a) == (b))
95 #endif
97 #ifndef INO_T_HASH
98 #define INO_T_HASH(a) (a)
99 #endif
101 #ifndef INCLUDE_LEN_FUDGE
102 #define INCLUDE_LEN_FUDGE 0
103 #endif
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. */
125 static int objc;
127 /* Nonzero means this is an assembly file, and allow
128 unknown directives, which could be comments. */
130 static int lang_asm;
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
201 to the output file.
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. */
220 int pedantic;
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. */
262 int traditional;
264 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
266 int c89;
268 /* Nonzero for the 199x C Standard. */
270 int c9x;
272 /* Nonzero causes output not to be done,
273 but directives such as #define that have side effects
274 are still obeyed. */
276 static int no_output;
278 /* Nonzero means we should look for header.gcc files that remap file names. */
279 static int remap;
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
288 and/or warnings. */
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 {
303 const char *fname;
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;
313 int lineno;
314 int length;
315 U_CHAR *buf;
316 U_CHAR *bufp;
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. */
325 U_CHAR *free_ptr;
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"); \
341 code; \
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
367 directory. */
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. */
372 int got_name_map;
373 /* The include directory status. */
374 struct stat st;
375 /* The include prefix: "" denotes the working directory,
376 otherwise fname must end in '/'.
377 The actual size is dynamically allocated. */
378 char fname[1];
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. */
384 /* The */
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
391 C++. */
392 int included; /* Set if the directory is acceptable. */
393 } include_defaults_array[]
394 #ifdef INCLUDE_DEFAULTS
395 = INCLUDE_DEFAULTS;
396 #else
398 /* Pick up GNU C++ specific include files. */
399 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1, 0 },
400 #ifdef CROSS_COMPILE
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 },
410 #endif
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 },
416 #endif
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 },
421 #endif
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 },
428 #endif
429 #ifndef STANDARD_INCLUDE_COMPONENT
430 #define STANDARD_INCLUDE_COMPONENT 0
431 #endif
432 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0, 0 },
433 #endif /* not CROSS_COMPILE */
434 { 0, 0, 0, 0, 0 }
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 */
470 char *fname;
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. */
477 int deps_output;
478 struct stat st;
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;
499 struct macrodef
501 struct definition *defn;
502 const U_CHAR *symnam;
503 int symlen;
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
517 such as
518 #define foo bar ,
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,
521 e.g.,
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
533 pattern list
534 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
535 where (x, y) means (nchars, argno). */
537 typedef struct definition DEFINITION;
538 struct definition {
539 int nargs;
540 int length; /* length of expansion string */
541 int predefined; /* True if the macro was builtin or */
542 /* came from the command line */
543 U_CHAR *expansion;
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 */
548 struct reflist {
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) */
559 } *pattern;
560 union {
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. */
565 U_CHAR *argnames;
566 } args;
569 /* different kinds of things that can appear in the value field
570 of a hash node. Actually, this may be useless now. */
571 union hashval {
572 const char *cpval;
573 DEFINITION *defn;
574 KEYDEF *keydef;
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 */
605 enum node_type {
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. */
647 struct hashnode {
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__. */
682 #ifndef SIZE_TYPE
683 #define SIZE_TYPE "long unsigned int"
684 #endif
686 /* The string value for __PTRDIFF_TYPE__. */
688 #ifndef PTRDIFF_TYPE
689 #define PTRDIFF_TYPE "long int"
690 #endif
692 /* The string value for __WCHAR_TYPE__. */
694 #ifndef WCHAR_TYPE
695 #define WCHAR_TYPE "int"
696 #endif
697 static const char * wchar_type = WCHAR_TYPE;
698 #undef WCHAR_TYPE
700 /* The string value for __USER_LABEL_PREFIX__ */
702 #ifndef USER_LABEL_PREFIX
703 #define USER_LABEL_PREFIX ""
704 #endif
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 ""
712 #endif
714 /* The string value for __IMMEDIATE_PREFIX__ */
716 #ifndef IMMEDIATE_PREFIX
717 #define IMMEDIATE_PREFIX ""
718 #endif
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 *))
763 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;
790 #endif
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},
814 #endif
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
845 # 1 "\f". */
846 static int ignore_escape_flag = 1;
848 /* Stack of conditionals currently in progress
849 (including both successful and failing conditionals). */
851 struct if_stack {
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 *,
902 int, int));
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 *,
925 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 *,
934 FILE_BUF *));
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 *,
939 int, int));
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 *,
948 int *));
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 *));
975 struct argdata;
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. */
1045 static int
1046 safe_read (desc, ptr, len)
1047 int desc;
1048 char *ptr;
1049 int len;
1051 int left, rcount, nchars;
1053 left = len;
1054 while (left > 0) {
1055 rcount = left;
1056 #ifdef MAX_READ_LEN
1057 if (rcount > MAX_READ_LEN)
1058 rcount = MAX_READ_LEN;
1059 #endif
1060 nchars = read (desc, ptr, rcount);
1061 if (nchars < 0)
1063 #ifdef EINTR
1064 if (errno == EINTR)
1065 continue;
1066 #endif
1067 return nchars;
1069 if (nchars == 0)
1070 break;
1071 ptr += nchars;
1072 left -= nchars;
1074 return len - left;
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. */
1081 static void
1082 safe_write (desc, ptr, len)
1083 int desc;
1084 const char *ptr;
1085 int len;
1087 int wcount, written;
1089 while (len > 0) {
1090 wcount = len;
1091 #ifdef MAX_WRITE_LEN
1092 if (wcount > MAX_WRITE_LEN)
1093 wcount = MAX_WRITE_LEN;
1094 #endif
1095 written = write (desc, ptr, wcount);
1096 if (written < 0)
1098 #ifdef EINTR
1099 if (errno == EINTR)
1100 continue;
1101 #endif
1102 pfatal_with_name (out_fname);
1104 ptr += written;
1105 len -= written;
1110 static void
1111 print_help ()
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");
1181 main (argc, argv)
1182 int argc;
1183 char **argv;
1185 struct stat st;
1186 const char *in_fname;
1187 char *cp;
1188 int f, i;
1189 FILE_BUF *fp;
1191 char **pend_files;
1192 char **pend_defs;
1193 char **pend_undefs;
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
1199 an assertion. */
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. */
1208 int verbose = 0;
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. */
1223 struct rlimit rlim;
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);
1231 #endif
1233 #ifdef SIGPIPE
1234 signal (SIGPIPE, pipe_closed);
1235 #endif
1237 #ifdef HAVE_LC_MESSAGES
1238 setlocale (LC_MESSAGES, "");
1239 #endif
1240 (void) bindtextdomain (PACKAGE, localedir);
1241 (void) textdomain (PACKAGE);
1243 progname = base_name (argv[0]);
1245 #ifdef VMS
1247 /* Remove extension from PROGNAME. */
1248 char *p;
1249 char *s = xstrdup (progname);
1250 progname = s;
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')
1257 && !p[4])
1258 *p = '\0';
1260 #endif
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 *));
1272 in_fname = NULL;
1273 out_fname = NULL;
1275 /* Initialize is_idchar. */
1276 initialize_char_syntax ();
1278 no_line_directives = 0;
1279 no_trigraphs = 1;
1280 dump_macros = dump_none;
1281 no_output = 0;
1282 cplusplus = 0;
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");
1295 #endif
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)
1303 print_help ();
1304 fatal ("Too many arguments");
1306 else if (in_fname != NULL)
1307 out_fname = argv[i];
1308 else
1309 in_fname = argv[i];
1310 } else {
1311 switch (argv[i][1]) {
1313 case 'i':
1314 if (!strcmp (argv[i], "-include")) {
1315 if (i + 1 == argc)
1316 fatal ("Filename missing after `-include' option");
1317 else {
1318 i++;
1319 simplify_filename (pend_includes[i] = argv[i]);
1322 if (!strcmp (argv[i], "-imacros")) {
1323 if (i + 1 == argc)
1324 fatal ("Filename missing after `-imacros' option");
1325 else {
1326 i++;
1327 simplify_filename (pend_files[i] = argv[i]);
1330 if (!strcmp (argv[i], "-iprefix")) {
1331 if (i + 1 == argc)
1332 fatal ("Filename missing after `-iprefix' option");
1333 else
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,
1343 "", argv[++i])))
1344 break;
1345 dirtmp->c_system_include_path = 1;
1347 if (before_system == 0)
1348 before_system = dirtmp;
1349 else
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;
1357 char *prefix;
1359 if (include_prefix != 0)
1360 prefix = include_prefix;
1361 else {
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])))
1370 break;
1372 if (after_include == 0)
1373 after_include = dirtmp;
1374 else
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;
1382 char *prefix;
1384 if (include_prefix != 0)
1385 prefix = include_prefix;
1386 else {
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,
1401 "", argv[++i])))
1402 break;
1404 if (after_include == 0)
1405 after_include = dirtmp;
1406 else
1407 last_after_include->next = dirtmp;
1408 last_after_include = dirtmp; /* Tail follows the last one */
1410 break;
1412 case 'o':
1413 if (out_fname != NULL)
1414 fatal ("Output filename specified twice");
1415 if (i + 1 == argc)
1416 fatal ("Filename missing after -o option");
1417 out_fname = argv[++i];
1418 if (!strcmp (out_fname, "-"))
1419 out_fname = "";
1420 break;
1422 case 'p':
1423 if (!strcmp (argv[i], "-pedantic"))
1424 pedantic = 1;
1425 else if (!strcmp (argv[i], "-pedantic-errors")) {
1426 pedantic = 1;
1427 pedantic_errors = 1;
1428 } else if (!strcmp (argv[i], "-pcp")) {
1429 char *pcp_fname;
1430 if (i + 1 == argc)
1431 fatal ("Filename missing after -pcp option");
1432 pcp_fname = argv[++i];
1433 pcp_outfile
1434 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1435 ? fopen (pcp_fname, "w")
1436 : stdout);
1437 if (pcp_outfile == 0)
1438 pfatal_with_name (pcp_fname);
1439 no_precomp = 1;
1441 break;
1443 case 't':
1444 if (!strcmp (argv[i], "-traditional")) {
1445 traditional = 1;
1446 cplusplus_comments = 0;
1447 } else if (!strcmp (argv[i], "-trigraphs")) {
1448 no_trigraphs = 0;
1450 break;
1452 case 'l':
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"))
1464 lang_asm = 1;
1465 else if (! strcmp (argv[i], "-lint"))
1466 for_lint = 1;
1467 break;
1469 case '+':
1470 cplusplus = 1, cplusplus_comments = 1;
1471 break;
1473 case 's':
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;
1483 break;
1485 case 'w':
1486 inhibit_warnings = 1;
1487 break;
1489 case 'W':
1490 if (!strcmp (argv[i], "-Wtrigraphs"))
1491 warn_trigraphs = 1;
1492 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1493 warn_trigraphs = 0;
1494 else if (!strcmp (argv[i], "-Wcomment"))
1495 warn_comments = 1;
1496 else if (!strcmp (argv[i], "-Wno-comment"))
1497 warn_comments = 0;
1498 else if (!strcmp (argv[i], "-Wcomments"))
1499 warn_comments = 1;
1500 else if (!strcmp (argv[i], "-Wno-comments"))
1501 warn_comments = 0;
1502 else if (!strcmp (argv[i], "-Wtraditional"))
1503 warn_stringify = 1;
1504 else if (!strcmp (argv[i], "-Wno-traditional"))
1505 warn_stringify = 0;
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"))
1511 warn_undef = 1;
1512 else if (!strcmp (argv[i], "-Wno-undef"))
1513 warn_undef = 0;
1514 else if (!strcmp (argv[i], "-Wimport"))
1515 warn_import = 1;
1516 else if (!strcmp (argv[i], "-Wno-import"))
1517 warn_import = 0;
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"))
1524 warn_trigraphs = 1;
1525 warn_comments = 1;
1526 warn_white_space = 1;
1528 break;
1530 case 'f':
1531 if (!strcmp (argv[i], "-fleading-underscore"))
1532 user_label_prefix = "_";
1533 else if (!strcmp (argv[i], "-fno-leading-underscore"))
1534 user_label_prefix = "";
1535 break;
1537 case 'M':
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
1544 as extensible. */
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;
1553 break;
1555 if (!strcmp (argv[i], "-M"))
1556 print_deps = 2;
1557 else if (!strcmp (argv[i], "-MM"))
1558 print_deps = 1;
1559 else if (!strcmp (argv[i], "-MD"))
1560 print_deps = 2;
1561 else if (!strcmp (argv[i], "-MMD"))
1562 print_deps = 1;
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")) {
1566 if (i + 1 == argc)
1567 fatal ("Filename missing after %s option", argv[i]);
1568 i++;
1569 deps_file = argv[i];
1570 deps_mode = "w";
1571 } else {
1572 /* For -M and -MM, write deps on standard output
1573 and suppress the usual output. */
1574 deps_stream = stdout;
1575 inhibit_output = 1;
1577 break;
1579 case 'd':
1581 char *p = argv[i] + 2;
1582 char c;
1583 while ((c = *p++)) {
1584 /* Arg to -d specifies what parts of macros to dump */
1585 switch (c) {
1586 case 'M':
1587 dump_macros = dump_only;
1588 no_output = 1;
1589 break;
1590 case 'N':
1591 dump_macros = dump_names;
1592 break;
1593 case 'D':
1594 dump_macros = dump_definitions;
1595 break;
1596 case 'I':
1597 dump_includes = 1;
1598 break;
1602 break;
1604 case 'g':
1605 if (argv[i][2] == '3')
1606 debug_output = 1;
1607 break;
1609 case '-':
1610 if (strcmp (argv[i], "--help") != 0)
1611 return i;
1612 print_help ();
1613 exit (0);
1614 break;
1616 case 'v':
1617 notice ("GNU CPP version %s", version_string);
1618 #ifdef TARGET_VERSION
1619 TARGET_VERSION;
1620 #endif
1621 fprintf (stderr, "\n");
1622 verbose = 1;
1623 break;
1625 case 'H':
1626 print_include_names = 1;
1627 break;
1629 case 'D':
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");
1634 else
1635 i++, pend_defs[i] = argv[i];
1636 break;
1638 case 'A':
1640 char *p;
1642 if (argv[i][2] != 0)
1643 p = argv[i] + 2;
1644 else if (i + 1 == argc)
1645 fatal ("Assertion missing after -A option");
1646 else
1647 p = argv[++i];
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. */
1654 int j;
1655 for (j = 0; j < i; j++)
1656 pend_defs[j] = pend_assertions[j] = 0;
1657 } else {
1658 pend_assertions[i] = p;
1659 pend_assertion_options[i] = "-A";
1662 break;
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");
1669 else
1670 pend_undefs[i] = argv[i+1], i++;
1671 break;
1673 case 'C':
1674 put_out_comments = 1;
1675 break;
1677 case 'E': /* -E comes from cc -E; ignore it. */
1678 break;
1680 case 'P':
1681 no_line_directives = 1;
1682 break;
1684 case '$': /* Don't include $ in identifiers. */
1685 is_idchar['$'] = is_idstart['$'] = 0;
1686 break;
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, "-")) {
1694 ignore_srcdir = 1;
1695 /* Don't use any preceding -I directories for #include <...>. */
1696 first_bracket_include = 0;
1698 else {
1699 dirtmp = new_include_prefix (last_include, NULL_PTR, "", dir);
1700 append_include_chain (dirtmp, dirtmp);
1703 break;
1705 case 'n':
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"))
1714 no_precomp = 1;
1715 break;
1717 case 'r':
1718 if (!strcmp (argv[i], "-remap"))
1719 remap = 1;
1720 break;
1722 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1723 if (in_fname == NULL) {
1724 in_fname = "";
1725 break;
1726 } else if (out_fname == NULL) {
1727 out_fname = "";
1728 break;
1729 } /* else fall through into error */
1731 default:
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
1742 anyway. */
1743 GET_ENV_PATH_LIST (cp, "CPATH");
1744 if (cp && ! no_standard_includes)
1745 path_include (cp);
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)
1758 in_fname = "";
1759 fp->nominal_fname = fp->fname = in_fname;
1760 fp->nominal_fname_len = strlen (in_fname);
1761 fp->lineno = 0;
1763 /* In C++, wchar_t is a distinct basic type, and we can expect
1764 __wchar_t to be defined by cc1plus. */
1765 if (cplusplus)
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]) {
1777 if (debug_output)
1778 output_line_directive (fp, &outbuf, 0, same_file);
1779 make_undef (pend_undefs[i], &outbuf);
1781 if (pend_defs[i]) {
1782 if (debug_output)
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. */
1794 char *epath = 0;
1795 switch ((objc << 1) + cplusplus)
1797 case 0:
1798 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
1799 break;
1800 case 1:
1801 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
1802 break;
1803 case 2:
1804 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
1805 break;
1806 case 3:
1807 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
1808 break;
1810 /* If the environment var for this language is set,
1811 add to the default list of include directories. */
1812 if (epath) {
1813 int num_dirs;
1814 char *startp, *endp;
1816 for (num_dirs = 1, startp = epath; *startp; startp++)
1817 if (*startp == PATH_SEPARATOR)
1818 num_dirs++;
1819 include_defaults
1820 = (struct default_include *) xmalloc ((num_dirs
1821 * sizeof (struct default_include))
1822 + sizeof (include_defaults_array));
1823 startp = endp = epath;
1824 num_dirs = 0;
1825 while (1) {
1826 char c = *endp++;
1827 if (c == PATH_SEPARATOR || !c) {
1828 endp[-1] = 0;
1829 include_defaults[num_dirs].fname
1830 = startp == endp ? "." : xstrdup (startp);
1831 endp[-1] = c;
1832 include_defaults[num_dirs].component = 0;
1833 include_defaults[num_dirs].cplusplus = cplusplus;
1834 include_defaults[num_dirs].cxx_aware = 1;
1835 num_dirs++;
1836 if (!c)
1837 break;
1838 startp = endp;
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);
1875 if (new) {
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;
1880 p->included = 1;
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);
1891 if (new) {
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;
1896 p->included = 1;
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. */
1908 if (verbose) {
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");
1914 if (!p->fname[0])
1915 fprintf (stderr, " .\n");
1916 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
1917 fprintf (stderr, " %s\n", p->fname);
1918 else
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++)
1927 if (! d->included)
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. */
1944 if (print_deps == 0
1945 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1946 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1947 char *spec = getenv ("DEPENDENCIES_OUTPUT");
1948 char *s;
1949 char *output_file;
1951 if (spec == 0) {
1952 spec = getenv ("SUNPRO_DEPENDENCIES");
1953 print_deps = 2;
1955 else
1956 print_deps = 1;
1958 /* Find the space before the DEPS_TARGET, if there is one. */
1959 s = index (spec, ' ');
1960 if (s) {
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;
1965 } else {
1966 deps_target = 0;
1967 output_file = spec;
1970 deps_file = output_file;
1971 deps_mode = "a";
1974 /* For -M, print the expected object file name
1975 as the target of this Make-rule. */
1976 if (print_deps) {
1977 deps_allocated_size = 200;
1978 deps_buffer = xmalloc (deps_allocated_size);
1979 deps_buffer[0] = 0;
1980 deps_size = 0;
1981 deps_column = 0;
1983 if (deps_target) {
1984 deps_output (deps_target, ':');
1985 } else if (*in_fname == 0) {
1986 deps_output ("-", ':');
1987 } else {
1988 char *p, *q;
1989 int len;
1991 q = base_name (in_fname);
1993 /* Copy remainder to mungable area. */
1994 p = (char *) alloca (strlen(q) + 8);
1995 strcpy (p, q);
1997 /* Output P, but remove known suffixes. */
1998 len = strlen (p);
1999 q = p + len;
2000 if (len >= 2
2001 && p[len - 2] == '.'
2002 && index("cCsSm", p[len - 1]))
2003 q = p + (len - 2);
2004 else if (len >= 3
2005 && p[len - 3] == '.'
2006 && p[len - 2] == 'c'
2007 && p[len - 1] == 'c')
2008 q = p + (len - 3);
2009 else if (len >= 4
2010 && p[len - 4] == '.'
2011 && p[len - 3] == 'c'
2012 && p[len - 2] == 'x'
2013 && p[len - 1] == 'x')
2014 q = p + (len - 4);
2015 else if (len >= 4
2016 && p[len - 4] == '.'
2017 && p[len - 3] == 'c'
2018 && p[len - 2] == 'p'
2019 && p[len - 1] == 'p')
2020 q = p + (len - 4);
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);
2040 if (fd < 0) {
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) {
2053 in_fname = "";
2054 f = 0;
2055 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2056 goto perror;
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);
2062 fp->lineno = 1;
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. */
2069 int size;
2070 int bsize;
2071 int cnt;
2073 if (S_ISDIR (st.st_mode))
2074 fatal ("Input file `%s' is a directory", in_fname);
2076 bsize = 2000;
2077 size = 0;
2078 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2079 for (;;) {
2080 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2081 if (cnt < 0) goto perror; /* error! */
2082 size += cnt;
2083 if (size != bsize) break; /* End of file */
2084 bsize *= 2;
2085 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2087 fp->length = size;
2088 } else {
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)
2093 memory_full ();
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;
2098 fp->bufp = fp->buf;
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. */
2113 if (!no_trigraphs)
2114 trigraph_pcp (fp);
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. */
2130 no_record_file++;
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);
2135 if (fd < 0) {
2136 perror_with_name (pend_includes[i]);
2137 return FATAL_EXIT_CODE;
2139 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2141 no_record_file--;
2143 /* Scan the input, processing macros and directives. */
2145 rescan (&outbuf, 0);
2147 if (missing_newline)
2148 fp->lineno--;
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)
2157 dump_all_macros ();
2158 else if (! inhibit_output) {
2159 write_output ();
2162 if (print_deps) {
2163 /* Don't actually write the deps file if compilation has failed. */
2164 if (errors == 0) {
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);
2169 if (deps_file) {
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");
2183 if (errors)
2184 exit (FATAL_EXIT_CODE);
2185 exit (SUCCESS_EXIT_CODE);
2187 perror:
2188 pfatal_with_name (in_fname);
2189 return 0;
2192 /* Given a colon-separated list of file names PATH,
2193 add all the names to the search path for include files. */
2195 static void
2196 path_include (path)
2197 char *path;
2199 char *p;
2201 p = path;
2203 if (*p)
2204 while (1) {
2205 char *q = p;
2206 char c;
2207 struct file_name_list *dirtmp;
2209 /* Find the end of this name. */
2210 while ((c = *q++) != PATH_SEPARATOR && c)
2211 continue;
2213 q[-1] = 0;
2214 dirtmp = new_include_prefix (last_include, NULL_PTR,
2215 "", p == q ? "." : p);
2216 q[-1] = c;
2217 append_include_chain (dirtmp, dirtmp);
2219 /* Advance past this name. */
2220 p = q;
2221 if (! c)
2222 break;
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 *
2233 index0 (s, c, n)
2234 const U_CHAR *s;
2235 int c;
2236 size_t n;
2238 const char *p = (const char *) s;
2239 for (;;) {
2240 const char *q = index (p, c);
2241 if (q)
2242 return q;
2243 else {
2244 size_t l = strlen (p);
2245 if (l == n)
2246 return 0;
2247 l++;
2248 p += l;
2249 n -= l;
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. */
2263 static void
2264 trigraph_pcp (buf)
2265 FILE_BUF *buf;
2267 register U_CHAR c, *bptr;
2268 register const U_CHAR *fptr, *sptr, *lptr;
2269 int len;
2271 fptr = sptr = bptr = buf->buf;
2272 lptr = fptr + buf->length;
2273 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2274 if (*++sptr != '?')
2275 continue;
2276 switch (*++sptr) {
2277 case '=':
2278 c = '#';
2279 break;
2280 case '(':
2281 c = '[';
2282 break;
2283 case '/':
2284 c = '\\';
2285 break;
2286 case ')':
2287 c = ']';
2288 break;
2289 case '\'':
2290 c = '^';
2291 break;
2292 case '<':
2293 c = '{';
2294 break;
2295 case '!':
2296 c = '|';
2297 break;
2298 case '>':
2299 c = '}';
2300 break;
2301 case '-':
2302 c = '~';
2303 break;
2304 case '?':
2305 sptr--;
2306 continue;
2307 default:
2308 continue;
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);
2317 bptr += len;
2318 *bptr++ = c;
2319 fptr = ++sptr;
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. */
2333 static void
2334 check_white_space (buf)
2335 FILE_BUF *buf;
2337 register const U_CHAR *sptr = buf->buf;
2338 register const U_CHAR *lptr = sptr + buf->length;
2339 register const U_CHAR *nptr;
2340 int line = 0;
2342 nptr = sptr = buf->buf;
2343 lptr = sptr + buf->length;
2344 for (nptr = sptr;
2345 (nptr = index0 (nptr, '\n', (size_t) (lptr - nptr))) != NULL;
2346 nptr ++) {
2347 register const U_CHAR *p = nptr;
2348 line++;
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");
2354 break;
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 == '\\'. */
2368 static void
2369 newline_fix (bp)
2370 U_CHAR *bp;
2372 register U_CHAR *p = bp;
2374 /* First count the backslash-newline pairs here. */
2375 do {
2376 if (p[1] != '\n')
2377 break;
2378 p += 2;
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. */
2385 return;
2387 /* Copy all potentially embarrassing characters
2388 that follow the backslash-newline pairs
2389 down to where the pairs originally started. */
2391 *bp++ = *p++;
2392 while (*p == '*' || *p == '/');
2394 /* Now write the same number of pairs after the embarrassing chars. */
2395 while (bp < p) {
2396 *bp++ = '\\';
2397 *bp++ = '\n';
2401 /* Like newline_fix but for use within a directive-name.
2402 Move any backslash-newlines up past any following symbol constituents. */
2404 static void
2405 name_newline_fix (bp)
2406 U_CHAR *bp;
2408 register U_CHAR *p = bp;
2410 /* First count the backslash-newline pairs here. */
2411 do {
2412 if (p[1] != '\n')
2413 break;
2414 p += 2;
2415 } while (*p == '\\');
2417 /* What follows the backslash-newlines is not embarrassing. */
2419 if (!is_idchar[*p])
2420 /* What follows the backslash-newlines is not embarrassing. */
2421 return;
2423 /* Copy all potentially embarrassing characters
2424 that follow the backslash-newline pairs
2425 down to where the pairs originally started. */
2427 *bp++ = *p++;
2428 while (is_idchar[*p]);
2430 /* Now write the same number of pairs after the embarrassing chars. */
2431 while (bp < p) {
2432 *bp++ = '\\';
2433 *bp++ = '\n';
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. */
2447 static const char *
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 */
2457 *arglen = 0;
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)) {
2467 *cmdlen = 10;
2468 return "NOTREACHED";
2470 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2471 *cmdlen = 8;
2472 return "ARGSUSED";
2474 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2475 *cmdlen = 11;
2476 return "LINTLIBRARY";
2478 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2479 *cmdlen = 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);
2485 numptr++);
2486 *arglen = numptr - *argstart;
2487 return "VARARGS";
2489 return NULL;
2493 * The main loop of the program.
2495 * Read characters from the input stack, transferring them to the
2496 * output buffer OP.
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.
2528 static void
2529 rescan (op, output_marks)
2530 FILE_BUF *op;
2531 int output_marks;
2533 /* Character being scanned in main loop. */
2534 register U_CHAR c;
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]). */
2543 FILE_BUF *ip;
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. */
2560 int redo_char = 0;
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. */
2568 int start_line;
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. */
2575 #define POPMACRO \
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. */
2583 #define RECACHE \
2584 do { ip = &instack[indepth]; \
2585 ibp = ip->bufp; \
2586 limit = ip->buf + ip->length; \
2587 op->bufp = obp; \
2588 check_expand (op, limit - ibp); \
2589 beg_of_line = 0; \
2590 obp = op->bufp; } while (0)
2592 if (no_output && instack[indepth].fname != 0)
2593 skip_if_group (&instack[indepth], 1, NULL);
2595 obp = op->bufp;
2596 RECACHE;
2598 beg_of_line = ibp;
2600 /* Our caller must always put a null after the end of
2601 the input at each input stack level. */
2602 if (*limit != 0)
2603 abort ();
2605 while (1) {
2606 c = *ibp++;
2607 *obp++ = c;
2609 switch (c) {
2610 case '\\':
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. */
2615 ++ibp;
2616 ++ip->lineno;
2617 --obp; /* remove backslash from obuf */
2618 break;
2620 /* If ANSI, backslash is just another character outside a string. */
2621 if (!traditional)
2622 goto randomchar;
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)
2627 goto specialchar;
2628 if (ibp < limit)
2629 *obp++ = *ibp++;
2630 break;
2632 case '%':
2633 if (ident_length || ip->macro || traditional)
2634 goto randomchar;
2635 while (*ibp == '\\' && ibp[1] == '\n') {
2636 ibp += 2;
2637 ++ip->lineno;
2639 if (*ibp != ':')
2640 break;
2641 /* Treat this %: digraph as if it were #. */
2642 /* Fall through. */
2644 case '#':
2645 if (assertions_flag) {
2646 if (ident_length)
2647 goto specialchar;
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])
2652 *obp++ = *ibp++;
2653 SKIP_WHITE_SPACE (ibp);
2654 if (*ibp == '(') {
2655 ip->bufp = ibp;
2656 skip_paren_group (ip);
2657 bcopy ((const PTR) ibp, (PTR) obp, ip->bufp - ibp);
2658 obp += ip->bufp - ibp;
2659 ibp = ip->bufp;
2661 break;
2664 /* If this is expanding a macro definition, don't recognize
2665 preprocessing directives. */
2666 if (ip->macro != 0)
2667 goto randomchar;
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. */
2672 if (! ip->fname) {
2673 if (ip->buf != beg_of_line)
2674 warning ("preprocessing directive not recognized within macro arg");
2675 goto randomchar;
2677 if (ident_length)
2678 goto specialchar;
2681 /* # keyword: a # must be first nonblank char on the line */
2682 if (beg_of_line == 0)
2683 goto randomchar;
2685 U_CHAR *bp;
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. */
2690 bp = beg_of_line;
2691 /* If -traditional, require # to be at beginning of line. */
2692 if (!traditional) {
2693 while (1) {
2694 if (is_hor_space[*bp])
2695 bp++;
2696 else if (*bp == '\\' && bp[1] == '\n')
2697 bp += 2;
2698 else if (*bp == '/' && bp[1] == '*') {
2699 bp += 2;
2700 while (1)
2702 if (*bp == '*')
2704 if (bp[1] == '/')
2706 bp += 2;
2707 break;
2710 else
2712 #ifdef MULTIBYTE_CHARS
2713 int length;
2714 length = local_mblen (bp, limit - bp);
2715 if (length > 1)
2716 bp += (length - 1);
2717 #endif
2719 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. */
2725 else break;
2727 if (c == '%') {
2728 if (bp[0] != '%')
2729 break;
2730 while (bp[1] == '\\' && bp[2] == '\n')
2731 bp += 2;
2732 if (bp + 1 != ibp)
2733 break;
2734 /* %: appears at start of line; skip past the ':' too. */
2735 bp++;
2736 ibp++;
2739 if (bp + 1 != ibp)
2740 goto randomchar;
2743 /* This # can start a directive. */
2745 --obp; /* Don't copy the '#' */
2747 ip->bufp = ibp;
2748 op->bufp = obp;
2749 if (! handle_directive (ip, op)) {
2750 #ifdef USE_C_ALLOCA
2751 alloca (0);
2752 #endif
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);
2760 RECACHE;
2761 beg_of_line = ibp;
2762 break;
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])
2770 *obp++ = *ibp++;
2771 while (is_idchar[*ibp])
2772 *obp++ = *ibp++;
2774 goto randomchar;
2776 #ifdef USE_C_ALLOCA
2777 alloca (0);
2778 #endif
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);
2784 obp = op->bufp;
2785 RECACHE;
2786 beg_of_line = ibp;
2787 break;
2789 case '\"': /* skip quoted string */
2790 case '\'':
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. */
2796 if (ident_length) {
2797 if (! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2798 goto specialchar;
2799 ident_length = hash = 0;
2802 start_line = ip->lineno;
2804 /* Skip ahead to a matching quote. */
2806 while (1) {
2807 if (ibp >= limit) {
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. */
2812 POPMACRO;
2813 RECACHE;
2814 continue;
2816 if (!traditional) {
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;
2825 break;
2827 *obp++ = *ibp;
2828 switch (*ibp++) {
2829 case '\n':
2830 if (warn_white_space && ip->fname && is_hor_space[ibp[-2]])
2831 warning ("white space at end of line in string");
2832 ++ip->lineno;
2833 ++op->lineno;
2834 /* Traditionally, end of line ends a string constant with no error.
2835 So exit the loop and record the new line. */
2836 if (traditional) {
2837 beg_of_line = ibp;
2838 goto while2end;
2840 if (c == '\'') {
2841 error_with_line (line_for_error (start_line),
2842 "unterminated character constant");
2843 goto while2end;
2845 if (multiline_string_line == 0) {
2846 if (pedantic)
2847 pedwarn_with_line (line_for_error (start_line),
2848 "string constant runs past end of line");
2849 multiline_string_line = ip->lineno - 1;
2851 break;
2853 case '\\':
2854 if (*ibp == '\n') {
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. */
2859 if (ip->macro) {
2860 *obp++ = '\n';
2861 ++op->lineno;
2862 } else
2863 --obp;
2864 ++ibp;
2865 ++ip->lineno;
2866 } else {
2867 /* ANSI stupidly requires that in \\ the second \
2868 is *not* prevented from combining with a newline. */
2869 if (!ip->macro) {
2870 while (*ibp == '\\' && ibp[1] == '\n') {
2871 *obp++ = *ibp++;
2872 *obp++ = *ibp++;
2873 ++ip->lineno;
2874 ++op->lineno;
2877 *obp++ = *ibp++;
2879 break;
2881 case '\"':
2882 case '\'':
2883 if (ibp[-1] == c)
2884 goto while2end;
2885 break;
2886 #ifdef MULTIBYTE_CHARS
2887 default:
2889 int length;
2890 --ibp;
2891 length = local_mblen (ibp, limit - ibp);
2892 if (length > 0)
2894 --obp;
2895 bcopy (ibp, obp, length);
2896 obp += length;
2897 ibp += length;
2899 else
2900 ++ibp;
2902 break;
2903 #endif
2906 while2end:
2907 break;
2909 case '/':
2910 if (ip->macro != 0)
2911 goto randomchar;
2912 if (*ibp == '\\')
2913 newline_fix (ibp);
2914 if (*ibp != '*'
2915 && !(cplusplus_comments && *ibp == '/'))
2916 goto randomchar;
2917 if (ident_length)
2918 goto specialchar;
2920 if (*ibp == '/') {
2921 /* C++ style comment... */
2922 start_line = ip->lineno;
2924 /* Comments are equivalent to spaces. */
2925 if (! put_out_comments)
2926 obp[-1] = ' ';
2929 U_CHAR *before_bp = ibp;
2931 while (++ibp < limit) {
2932 if (*ibp == '\n')
2934 if (put_out_comments) {
2935 bcopy ((const PTR) before_bp, (PTR) obp, ibp - before_bp);
2936 obp += ibp - before_bp;
2938 break;
2940 if (*ibp == '\\')
2942 if (ibp + 1 < limit && ibp[1] == '\n')
2944 if (warn_comments)
2945 warning ("multiline `//' comment");
2946 ++ip->lineno;
2947 /* Copy the newline into the output buffer, in order to
2948 avoid the pain of a #line every time a multiline comment
2949 is seen. */
2950 if (!put_out_comments)
2951 *obp++ = '\n';
2952 ++op->lineno;
2953 ++ibp;
2956 else
2958 #ifdef MULTIBYTE_CHARS
2959 int length;
2960 length = local_mblen (ibp, limit - ibp);
2961 if (length > 1)
2962 ibp += (length - 1);
2963 #endif
2966 break;
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: */
2977 if (for_lint) {
2978 const U_CHAR *argbp;
2979 int cmdlen, arglen;
2980 const char *lintcmd =
2981 get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2983 if (lintcmd != NULL) {
2984 op->bufp = obp;
2985 check_expand (op, cmdlen + arglen + 14);
2986 obp = op->bufp;
2987 /* I believe it is always safe to emit this newline: */
2988 obp[-1] = '\n';
2989 bcopy ("#pragma lint ", (char *) obp, 13);
2990 obp += 13;
2991 bcopy (lintcmd, (char *) obp, cmdlen);
2992 obp += cmdlen;
2994 if (arglen != 0) {
2995 *(obp++) = ' ';
2996 bcopy (argbp, (char *) obp, arglen);
2997 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. */
3003 op->bufp = obp;
3004 output_line_directive (ip, op, 0, same_file);
3005 check_expand (op, limit - ibp + 2);
3006 obp = op->bufp;
3007 *(obp++) = '/';
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) {
3015 if (traditional)
3016 obp--;
3017 else
3018 obp[-1] = ' ';
3020 else
3021 *obp++ = '*';
3024 U_CHAR *before_bp = ibp;
3026 for (;;) {
3027 switch (*ibp++) {
3028 case '*':
3029 if (ibp[-2] == '/' && warn_comments)
3030 warning ("`/*' within comment");
3031 if (*ibp == '\\')
3032 newline_fix (ibp);
3033 if (*ibp == '/')
3034 goto comment_end;
3035 break;
3037 case '\n':
3038 ++ip->lineno;
3039 /* Copy the newline into the output buffer, in order to
3040 avoid the pain of a #line every time a multiline comment
3041 is seen. */
3042 if (!put_out_comments)
3043 *obp++ = '\n';
3044 ++op->lineno;
3045 break;
3047 case 0:
3048 if (limit < ibp) {
3049 error_with_line (line_for_error (start_line),
3050 "unterminated comment");
3051 goto limit_reached;
3053 break;
3054 #ifdef MULTIBYTE_CHARS
3055 default:
3057 int length;
3058 length = local_mblen (ibp, limit - ibp);
3059 if (length > 1)
3060 ibp += (length - 1);
3062 break;
3063 #endif
3066 comment_end:
3068 ibp++;
3069 if (put_out_comments) {
3070 bcopy ((const PTR) before_bp, (PTR) obp, ibp - before_bp);
3071 obp += ibp - before_bp;
3074 break;
3076 case '$':
3077 if (! is_idchar['$'])
3078 goto randomchar;
3079 if (pedantic)
3080 pedwarn ("`$' in identifier");
3081 goto letter;
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) {
3092 for (;;) {
3093 if (!ip->macro) {
3094 while (ibp[0] == '\\' && ibp[1] == '\n') {
3095 ++ip->lineno;
3096 ibp += 2;
3099 c = *ibp++;
3100 if (!is_idchar[c] && c != '.') {
3101 --ibp;
3102 break;
3104 *obp++ = 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') {
3108 if (!ip->macro) {
3109 while (ibp[0] == '\\' && ibp[1] == '\n') {
3110 ++ip->lineno;
3111 ibp += 2;
3114 if (*ibp == '+' || *ibp == '-') {
3115 *obp++ = *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')))
3119 break;
3123 break;
3125 /* fall through */
3127 case '_':
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':
3132 case 'y': case 'z':
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':
3137 case 'Y': case 'Z':
3138 letter:
3139 ident_length++;
3140 /* Compute step of hash function, to avoid a proc call on every token */
3141 hash = HASHSTEP (hash, c);
3142 break;
3144 case '\n':
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) {
3151 ident_length = 0;
3152 hash = 0;
3154 ibp++;
3155 if (!output_marks) {
3156 obp--;
3157 } else {
3158 /* If expanding a macro arg, keep the newline -. */
3159 *obp++ = '-';
3161 break;
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)
3176 goto specialchar;
3178 /* If generating final output, newline space makes a space. */
3179 if (!output_marks) {
3180 obp[-1] = *ibp++;
3181 /* And Newline Newline makes a newline, so count it. */
3182 if (obp[-1] == '\n')
3183 op->lineno++;
3184 } else {
3185 /* If expanding a macro arg, keep the newline space.
3186 If the arg gets stringified, newline space makes nothing. */
3187 *obp++ = *ibp++;
3189 } else abort (); /* Newline followed by something random? */
3190 break;
3193 /* If there is a pending identifier, handle it and come back here. */
3194 if (ident_length > 0)
3195 goto specialchar;
3197 beg_of_line = ibp;
3199 /* Update the line counts and output a #line if necessary. */
3200 ++ip->lineno;
3201 ++op->lineno;
3202 if (ip->lineno != op->lineno) {
3203 op->bufp = obp;
3204 output_line_directive (ip, op, 1, same_file);
3205 check_expand (op, limit - ibp);
3206 obp = op->bufp;
3208 break;
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. */
3212 case 0:
3213 if (ibp <= limit)
3214 /* Our input really contains a null character. */
3215 goto randomchar;
3217 limit_reached:
3218 /* At end of a macro-expansion level, pop it and read next level. */
3219 if (ip->macro != 0) {
3220 obp--;
3221 ibp--;
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]) {
3226 redo_char = 1;
3227 goto randomchar;
3229 POPMACRO;
3230 RECACHE;
3231 break;
3234 /* If we don't have a pending identifier,
3235 return at end of input. */
3236 if (ident_length == 0) {
3237 obp--;
3238 ibp--;
3239 op->bufp = obp;
3240 ip->bufp = ibp;
3241 goto ending;
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. */
3248 /* Fall through */
3250 specialchar:
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. */
3257 ibp--;
3258 obp--;
3259 redo_char = 1;
3261 default:
3263 randomchar:
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;
3286 hp = hp->next) {
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;
3294 int disabled;
3296 if (! redo_char)
3297 q--;
3299 do { /* All this to avoid a strncmp () */
3300 if (*p++ != *q++)
3301 goto hashcollision;
3302 } while (--i);
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. */
3308 if (! redo_char) {
3309 ibp--;
3310 obp--;
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
3322 will be output. */
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. */
3333 if (disabled) {
3334 #if 0
3335 /* This error check caught useful cases such as
3336 #define foo(x,y) bar (x (y,0), y)
3337 foo (foo, baz) */
3338 if (traditional)
3339 error ("recursive use of macro `%s'", hp->name);
3340 #endif
3342 if (output_marks) {
3343 check_expand (op, limit - ibp + 2);
3344 *obp++ = '\n';
3345 *obp++ = '-';
3347 break;
3350 /* If macro wants an arglist, verify that a '(' follows.
3351 first skip all whitespace, copying it to the output
3352 after the macro name. Then, if there is no '(',
3353 decide this is not a macro call and leave things that way. */
3354 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3355 && hp->value.defn->nargs >= 0)
3357 U_CHAR *old_ibp = ibp;
3358 U_CHAR *old_obp = obp;
3359 int old_iln = ip->lineno;
3360 int old_oln = op->lineno;
3362 while (1) {
3363 /* Scan forward over whitespace, copying it to the output. */
3364 if (ibp == limit && ip->macro != 0) {
3365 POPMACRO;
3366 RECACHE;
3367 old_ibp = ibp;
3368 old_obp = obp;
3369 old_iln = ip->lineno;
3370 old_oln = op->lineno;
3372 else if (is_space[*ibp]) {
3373 *obp++ = *ibp++;
3374 if (ibp[-1] == '\n') {
3375 if (ip->macro == 0) {
3376 /* Newline in a file. Count it. */
3377 ++ip->lineno;
3378 ++op->lineno;
3379 } else if (!output_marks) {
3380 /* A newline mark, and we don't want marks
3381 in the output. If it is newline-hyphen,
3382 discard it entirely. Otherwise, it is
3383 newline-whitechar, so keep the whitechar. */
3384 obp--;
3385 if (*ibp == '-')
3386 ibp++;
3387 else {
3388 if (*ibp == '\n')
3389 ++op->lineno;
3390 *obp++ = *ibp++;
3392 } else {
3393 /* A newline mark; copy both chars to the output. */
3394 *obp++ = *ibp++;
3398 else if (ip->macro)
3399 break;
3400 else if (*ibp == '/') {
3401 /* If a comment, copy it unchanged or discard it. */
3402 if (ibp[1] == '\\')
3403 newline_fix (ibp + 1);
3404 if (ibp[1] == '*') {
3405 if (put_out_comments) {
3406 *obp++ = '/';
3407 *obp++ = '*';
3408 } else if (! traditional) {
3409 *obp++ = ' ';
3411 for (ibp += 2; ibp < limit; ibp++) {
3412 /* We need not worry about newline-marks,
3413 since they are never found in comments. */
3414 if (ibp[0] == '*') {
3415 if (ibp[1] == '\\')
3416 newline_fix (ibp + 1);
3417 if (ibp[1] == '/') {
3418 ibp += 2;
3419 if (put_out_comments) {
3420 *obp++ = '*';
3421 *obp++ = '/';
3423 break;
3426 else if (*ibp == '\n') {
3427 /* Newline in a file. Count it. */
3428 ++ip->lineno;
3429 ++op->lineno;
3431 else
3433 #ifdef MULTIBYTE_CHARS
3434 int length;
3435 length = local_mblen (ibp, limit - ibp);
3436 if (length > 1)
3438 if (put_out_comments)
3440 bcopy (ibp, obp, length - 1);
3441 obp += length - 1;
3443 ibp += (length - 1);
3445 #endif
3447 if (put_out_comments)
3448 *obp++ = *ibp;
3450 } else if (ibp[1] == '/' && cplusplus_comments) {
3451 if (put_out_comments) {
3452 *obp++ = '/';
3453 *obp++ = '/';
3454 } else if (! traditional) {
3455 *obp++ = ' ';
3457 for (ibp += 2; ; ibp++)
3459 if (*ibp == '\n')
3460 break;
3461 if (*ibp == '\\' && ibp[1] == '\n')
3463 if (put_out_comments)
3464 *obp++ = *ibp++;
3466 else
3468 #ifdef MULTIBYTE_CHARS
3469 int length;
3470 length = local_mblen (ibp, limit - ibp);
3471 if (length > 1)
3473 if (put_out_comments)
3475 bcopy (ibp, obp, length - 1);
3476 obp += length - 1;
3478 ibp += (length - 1);
3480 #endif
3482 if (put_out_comments)
3483 *obp++ = *ibp;
3485 } else
3486 break;
3488 else if (ibp[0] == '\\' && ibp[1] == '\n') {
3489 ibp += 2;
3490 ++ip->lineno;
3492 else break;
3494 if (*ibp != '(') {
3495 /* It isn't a macro call.
3496 Put back the space that we just skipped. */
3497 ibp = old_ibp;
3498 obp = old_obp;
3499 ip->lineno = old_iln;
3500 op->lineno = old_oln;
3501 /* Exit the for loop. */
3502 break;
3506 /* This is now known to be a macro call.
3507 Discard the macro name from the output,
3508 along with any following whitespace just copied,
3509 but preserve newlines if not outputting marks since this
3510 is more likely to do the right thing with line numbers. */
3511 obp = op->buf + obufp_before_macroname;
3512 if (output_marks)
3513 op->lineno = op_lineno_before_macroname;
3514 else {
3515 int newlines = op->lineno - op_lineno_before_macroname;
3516 while (0 < newlines--)
3517 *obp++ = '\n';
3520 /* Prevent accidental token-pasting with a character
3521 before the macro call. */
3522 if (!traditional && obp != op->buf) {
3523 switch (obp[-1]) {
3524 case '!': case '%': case '&': case '*':
3525 case '+': case '-': case '.': case '/':
3526 case ':': case '<': case '=': case '>':
3527 case '^': case '|':
3528 /* If we are expanding a macro arg, make a newline marker
3529 to separate the tokens. If we are making real output,
3530 a plain space will do. */
3531 if (output_marks)
3532 *obp++ = '\n';
3533 *obp++ = ' ';
3537 /* Expand the macro, reading arguments as needed,
3538 and push the expansion on the input stack. */
3539 ip->bufp = ibp;
3540 op->bufp = obp;
3541 macroexpand (hp, op);
3543 /* Reexamine input stack, since macroexpand has pushed
3544 a new level on it. */
3545 obp = op->bufp;
3546 RECACHE;
3547 break;
3549 hashcollision:
3551 } /* End hash-table-search loop */
3553 ident_length = hash = 0; /* Stop collecting identifier */
3554 redo_char = 0;
3555 concatenated = 0;
3556 } /* End if (ident_length > 0) */
3557 } /* End switch */
3558 } /* End per-char loop */
3560 /* Come here to return -- but first give an error message
3561 if there was an unterminated successful conditional. */
3562 ending:
3563 if (if_stack != ip->if_stack)
3565 const char *str;
3567 switch (if_stack->type)
3569 case T_IF:
3570 str = "if";
3571 break;
3572 case T_IFDEF:
3573 str = "ifdef";
3574 break;
3575 case T_IFNDEF:
3576 str = "ifndef";
3577 break;
3578 case T_ELSE:
3579 str = "else";
3580 break;
3581 case T_ELIF:
3582 str = "elif";
3583 break;
3584 default:
3585 abort ();
3588 error_with_line (line_for_error (if_stack->lineno),
3589 "unterminated `#%s' conditional", str);
3591 if_stack = ip->if_stack;
3595 * Rescan a string into a temporary buffer and return the result
3596 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3598 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3599 * and insert such markers when appropriate. See `rescan' for details.
3600 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3601 * before substitution; it is 0 for other uses.
3603 static FILE_BUF
3604 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3605 const U_CHAR *buf;
3606 const U_CHAR *limit;
3607 int output_marks, assertions;
3609 register FILE_BUF *ip;
3610 FILE_BUF obuf;
3611 int length = limit - buf;
3612 U_CHAR *buf1;
3613 int odepth = indepth;
3614 int save_assertions_flag = assertions_flag;
3616 assertions_flag = assertions;
3618 if (length < 0)
3619 abort ();
3621 /* Set up the input on the input stack. */
3623 buf1 = (U_CHAR *) alloca (length + 1);
3625 register const U_CHAR *p1 = buf;
3626 register U_CHAR *p2 = buf1;
3628 while (p1 != limit)
3629 *p2++ = *p1++;
3631 buf1[length] = 0;
3633 /* Set up to receive the output. */
3635 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3636 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3637 obuf.nominal_fname = 0;
3638 obuf.inc = 0;
3639 obuf.dir = 0;
3640 obuf.fname = 0;
3641 obuf.macro = 0;
3642 obuf.if_stack = 0;
3643 obuf.free_ptr = 0;
3644 obuf.system_header_p = 0;
3646 CHECK_DEPTH ({return obuf;});
3648 ++indepth;
3650 ip = &instack[indepth];
3651 ip->fname = 0;
3652 ip->nominal_fname = 0;
3653 ip->nominal_fname_len = 0;
3654 ip->inc = 0;
3655 ip->system_header_p = 0;
3656 ip->macro = 0;
3657 ip->free_ptr = 0;
3658 ip->length = length;
3659 ip->buf = ip->bufp = buf1;
3660 ip->if_stack = if_stack;
3662 ip->lineno = obuf.lineno = 1;
3664 /* Scan the input, create the output. */
3665 rescan (&obuf, output_marks);
3667 /* Pop input stack to original state. */
3668 --indepth;
3670 if (indepth != odepth)
3671 abort ();
3673 assertions_flag = save_assertions_flag;
3674 return obuf;
3678 * Process a # directive. Expects IP->bufp to point after the '#', as in
3679 * `#define foo bar'. Passes to the directive handler
3680 * (do_define, do_include, etc.): the addresses of the 1st and
3681 * last chars of the directive (starting immediately after the #
3682 * keyword), plus op and the keyword table pointer. If the directive
3683 * contains comments it is copied into a temporary buffer sans comments
3684 * and the temporary buffer is passed to the directive handler instead.
3685 * Likewise for backslash-newlines.
3687 * Returns nonzero if this was a known # directive.
3688 * Otherwise, returns zero, without advancing the input pointer.
3691 static int
3692 handle_directive (ip, op)
3693 FILE_BUF *ip, *op;
3695 register U_CHAR *bp, *cp;
3696 register struct directive *kt;
3697 register int ident_length;
3698 U_CHAR *resume_p;
3700 /* Nonzero means we must copy the entire directive
3701 to get rid of comments or backslash-newlines. */
3702 int copy_directive = 0;
3704 U_CHAR *ident, *after_ident;
3706 bp = ip->bufp;
3708 /* Record where the directive started. do_xifdef needs this. */
3709 directive_start = bp - 1;
3711 ignore_escape_flag = 1;
3713 /* Skip whitespace and \-newline. */
3714 while (1) {
3715 if (is_hor_space[*bp]) {
3716 if (*bp != ' ' && *bp != '\t' && pedantic)
3717 pedwarn_strange_white_space (*bp);
3718 bp++;
3719 } else if (*bp == '/') {
3720 if (bp[1] == '\\')
3721 newline_fix (bp + 1);
3722 if (! (bp[1] == '*' || (cplusplus_comments && bp[1] == '/')))
3723 break;
3724 ip->bufp = bp + 2;
3725 skip_to_end_of_comment (ip, &ip->lineno, 0);
3726 bp = ip->bufp;
3727 } else if (*bp == '\\' && bp[1] == '\n') {
3728 bp += 2; ip->lineno++;
3729 } else break;
3732 /* Now find end of directive name.
3733 If we encounter a backslash-newline, exchange it with any following
3734 symbol-constituents so that we end up with a contiguous name. */
3736 cp = bp;
3737 while (1) {
3738 if (is_idchar[*cp])
3739 cp++;
3740 else {
3741 if (*cp == '\\')
3742 name_newline_fix (cp);
3743 if (is_idchar[*cp])
3744 cp++;
3745 else break;
3748 ident_length = cp - bp;
3749 ident = bp;
3750 after_ident = cp;
3752 /* A line of just `#' becomes blank. */
3754 if (ident_length == 0 && *after_ident == '\n') {
3755 ip->bufp = after_ident;
3756 return 1;
3759 if (ident_length == 0 || !is_idstart[*ident]) {
3760 U_CHAR *p = ident;
3761 while (is_idchar[*p]) {
3762 if (*p < '0' || *p > '9')
3763 break;
3764 p++;
3766 /* Handle # followed by a line number. */
3767 if (p != ident && !is_idchar[*p]) {
3768 static struct directive line_directive_table[] = {
3769 { 4, do_line, "line", T_LINE},
3771 if (pedantic)
3772 pedwarn ("`#' followed by integer");
3773 after_ident = ident;
3774 kt = line_directive_table;
3775 ignore_escape_flag = 0;
3776 goto old_linenum;
3779 /* Avoid error for `###' and similar cases unless -pedantic. */
3780 if (p == ident) {
3781 while (*p == '#' || is_hor_space[*p]) p++;
3782 if (*p == '\n') {
3783 if (pedantic && !lang_asm)
3784 warning ("invalid preprocessing directive");
3785 return 0;
3789 if (!lang_asm)
3790 error ("invalid preprocessing directive name");
3792 return 0;
3796 * Decode the keyword and call the appropriate expansion
3797 * routine, after moving the input pointer up to the next line.
3799 for (kt = directive_table; kt->length > 0; kt++) {
3800 if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
3801 register U_CHAR *buf;
3802 register U_CHAR *limit;
3803 int unterminated;
3804 int junk;
3805 int *already_output;
3807 /* Nonzero means do not delete comments within the directive.
3808 #define needs this when -traditional. */
3809 int keep_comments;
3811 old_linenum:
3813 limit = ip->buf + ip->length;
3814 unterminated = 0;
3815 already_output = 0;
3816 keep_comments = traditional && kt->type == T_DEFINE;
3817 /* #import is defined only in Objective C, or when on the NeXT. */
3818 if (kt->type == T_IMPORT
3819 && !(objc || lookup ((const U_CHAR *) "__NeXT__", -1, -1)))
3820 break;
3822 /* Find the end of this directive (first newline not backslashed
3823 and not in a string or comment).
3824 Set COPY_DIRECTIVE if the directive must be copied
3825 (it contains a backslash-newline or a comment). */
3827 buf = bp = after_ident;
3828 while (bp < limit) {
3829 register U_CHAR c = *bp++;
3830 switch (c) {
3831 case '\\':
3832 if (*bp == '\n') {
3833 ip->lineno++;
3834 copy_directive = 1;
3835 bp++;
3836 } else if (traditional && bp < limit)
3837 bp++;
3838 break;
3840 case '"':
3841 /* "..." is special for #include. */
3842 if (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)) {
3843 while (bp < limit && *bp != '\n') {
3844 if (*bp == '"') {
3845 bp++;
3846 break;
3848 if (*bp == '\\' && bp[1] == '\n') {
3849 ip->lineno++;
3850 copy_directive = 1;
3851 bp++;
3853 bp++;
3855 break;
3857 /* Fall through. */
3858 case '\'':
3859 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_directive, &unterminated);
3860 /* Don't bother calling the directive if we already got an error
3861 message due to unterminated string. Skip everything and pretend
3862 we called the directive. */
3863 if (unterminated) {
3864 if (traditional) {
3865 /* Traditional preprocessing permits unterminated strings. */
3866 ip->bufp = bp;
3867 goto endloop1;
3869 ip->bufp = bp;
3870 return 1;
3872 break;
3874 /* <...> is special for #include. */
3875 case '<':
3876 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3877 break;
3878 while (bp < limit && *bp != '>' && *bp != '\n') {
3879 if (*bp == '\\' && bp[1] == '\n') {
3880 ip->lineno++;
3881 copy_directive = 1;
3882 bp++;
3884 bp++;
3886 break;
3888 case '/':
3889 if (*bp == '\\')
3890 newline_fix (bp);
3891 if (*bp == '*'
3892 || (cplusplus_comments && *bp == '/')) {
3893 U_CHAR *obp = bp - 1;
3894 ip->bufp = bp + 1;
3895 skip_to_end_of_comment (ip, &ip->lineno, 0);
3896 bp = ip->bufp;
3897 /* No need to copy the directive because of a comment at the end;
3898 just don't include the comment in the directive. */
3899 if (!put_out_comments) {
3900 U_CHAR *p;
3901 for (p = bp; *p == ' ' || *p == '\t'; p++)
3902 continue;
3903 if (*p == '\n') {
3904 bp = obp;
3905 goto endloop1;
3908 /* Don't remove the comments if -traditional. */
3909 if (! keep_comments)
3910 copy_directive++;
3912 break;
3914 case '\f':
3915 case '\r':
3916 case '\v':
3917 if (pedantic)
3918 pedwarn_strange_white_space (c);
3919 break;
3921 case '\n':
3922 --bp; /* Point to the newline */
3923 ip->bufp = bp;
3924 goto endloop1;
3927 ip->bufp = bp;
3929 endloop1:
3930 resume_p = ip->bufp;
3931 /* BP is the end of the directive.
3932 RESUME_P is the next interesting data after the directive.
3933 A comment may come between. */
3935 /* If a directive should be copied through, and -C was given,
3936 pass it through before removing comments. */
3937 if (!no_output && put_out_comments
3938 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
3939 ? dump_macros == dump_definitions
3940 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
3941 : kt->type == T_PRAGMA)) {
3942 int len;
3944 /* Output directive name. */
3945 check_expand (op, kt->length + 2);
3946 /* Make sure # is at the start of a line */
3947 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3948 op->lineno++;
3949 *op->bufp++ = '\n';
3951 *op->bufp++ = '#';
3952 bcopy (kt->name, op->bufp, kt->length);
3953 op->bufp += kt->length;
3955 /* Output arguments. */
3956 len = (bp - buf);
3957 check_expand (op, len);
3958 bcopy (buf, (char *) op->bufp, len);
3959 op->bufp += len;
3960 /* Take account of any (escaped) newlines just output. */
3961 while (--len >= 0)
3962 if (buf[len] == '\n')
3963 op->lineno++;
3965 already_output = &junk;
3966 } /* Don't we need a newline or #line? */
3968 if (copy_directive) {
3969 register U_CHAR *xp = buf;
3970 /* Need to copy entire directive into temp buffer before dispatching */
3972 /* room for directive plus some slop */
3973 cp = (U_CHAR *) alloca (2 * (bp - buf) + 5);
3974 buf = cp;
3976 /* Copy to the new buffer, deleting comments
3977 and backslash-newlines (and whitespace surrounding the latter
3978 if outside of char and string constants). */
3980 while (xp < bp) {
3981 register U_CHAR c = *xp++;
3982 *cp++ = c;
3984 switch (c) {
3985 case '\n':
3986 abort (); /* A bare newline should never part of the line. */
3987 break;
3989 /* <...> is special for #include. */
3990 case '<':
3991 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3992 break;
3993 while (xp < bp && c != '>') {
3994 c = *xp++;
3995 if (c == '\\' && xp < bp && *xp == '\n')
3996 xp++;
3997 else
3998 *cp++ = c;
4000 break;
4002 case '\\':
4003 if (*xp == '\n') {
4004 xp++;
4005 cp--;
4006 if (cp != buf && is_hor_space[cp[-1]]) {
4007 while (cp - 1 != buf && is_hor_space[cp[-2]])
4008 cp--;
4009 SKIP_WHITE_SPACE (xp);
4010 } else if (is_hor_space[*xp]) {
4011 *cp++ = *xp++;
4012 SKIP_WHITE_SPACE (xp);
4014 } else if (traditional && xp < bp) {
4015 *cp++ = *xp++;
4017 break;
4019 case '\'':
4020 case '\"':
4022 int backslash_newlines_p;
4024 register const U_CHAR *bp1
4025 = skip_quoted_string (xp - 1, bp, ip->lineno,
4026 NULL_PTR, &backslash_newlines_p,
4027 NULL_PTR);
4028 if (backslash_newlines_p)
4029 while (xp != bp1)
4031 /* With something like:
4033 #define X "a\
4036 we should still remove the backslash-newline
4037 pair as part of phase two. */
4038 if (xp[0] == '\\' && xp[1] == '\n')
4039 xp += 2;
4040 else
4041 *cp++ = *xp++;
4043 else
4044 /* This is the same as the loop above, but taking
4045 advantage of the fact that we know there are no
4046 backslash-newline pairs. */
4047 while (xp != bp1)
4048 *cp++ = *xp++;
4050 break;
4052 case '/':
4053 if (*xp == '*'
4054 || (cplusplus_comments && *xp == '/')) {
4055 ip->bufp = xp + 1;
4056 /* If we already copied the directive through,
4057 already_output != 0 prevents outputting comment now. */
4058 skip_to_end_of_comment (ip, already_output, 0);
4059 if (keep_comments)
4060 while (xp != ip->bufp)
4061 *cp++ = *xp++;
4062 /* Delete or replace the slash. */
4063 else if (traditional)
4064 cp--;
4065 else
4066 cp[-1] = ' ';
4067 xp = ip->bufp;
4072 /* Null-terminate the copy. */
4074 *cp = 0;
4075 } else
4076 cp = bp;
4078 ip->bufp = resume_p;
4080 /* Some directives should be written out for cc1 to process,
4081 just as if they were not defined. And sometimes we're copying
4082 directives through. */
4084 if (!no_output && already_output == 0
4085 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
4086 ? (int) dump_names <= (int) dump_macros
4087 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4088 : kt->type == T_PRAGMA)) {
4089 int len;
4091 /* Output directive name. */
4092 check_expand (op, kt->length + 1);
4093 *op->bufp++ = '#';
4094 bcopy (kt->name, (char *) op->bufp, kt->length);
4095 op->bufp += kt->length;
4097 if (kt->type == T_DEFINE && dump_macros == dump_names) {
4098 /* Output `#define name' only. */
4099 U_CHAR *xp = buf;
4100 U_CHAR *yp;
4101 SKIP_WHITE_SPACE (xp);
4102 yp = xp;
4103 while (is_idchar[*xp]) xp++;
4104 len = (xp - yp);
4105 check_expand (op, len + 1);
4106 *op->bufp++ = ' ';
4107 bcopy (yp, (char *) op->bufp, len);
4108 } else {
4109 /* Output entire directive. */
4110 len = (cp - buf);
4111 check_expand (op, len);
4112 bcopy (buf, (char *) op->bufp, len);
4114 op->bufp += len;
4117 /* Call the appropriate directive handler. buf now points to
4118 either the appropriate place in the input buffer, or to
4119 the temp buffer if it was necessary to make one. cp
4120 points to the first char after the contents of the (possibly
4121 copied) directive, in either case. */
4122 (*kt->func) (buf, cp, op, kt);
4123 check_expand (op, ip->length - (ip->bufp - ip->buf));
4125 return 1;
4129 /* It is deliberate that we don't warn about undefined directives.
4130 That is the responsibility of cc1. */
4131 return 0;
4134 static struct tm *
4135 timestamp ()
4137 static struct tm tmbuf;
4138 if (! tmbuf.tm_mday) {
4139 time_t t = time ((time_t *) 0);
4140 struct tm *tm = localtime (&t);
4141 if (tm)
4142 tmbuf = *tm;
4143 else {
4144 /* Use 0000-01-01 00:00:00 if local time is not available. */
4145 tmbuf.tm_year = -1900;
4146 tmbuf.tm_mday = 1;
4149 return &tmbuf;
4152 static const char * const monthnames[] = {
4153 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4154 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4158 * expand things like __FILE__. Place the expansion into the output
4159 * buffer *without* rescanning.
4162 static void
4163 special_symbol (hp, op)
4164 HASHNODE *hp;
4165 FILE_BUF *op;
4167 const char *buf;
4168 int i, len;
4169 int true_indepth;
4170 FILE_BUF *ip = NULL;
4171 struct tm *timebuf;
4173 int paren = 0; /* For special `defined' keyword */
4175 if (pcp_outfile && pcp_inside_if
4176 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
4177 error ("Predefined macro `%s' used inside `#if' during precompilation",
4178 hp->name);
4180 for (i = indepth; i >= 0; i--)
4181 if (instack[i].fname != NULL) {
4182 ip = &instack[i];
4183 break;
4185 if (ip == NULL) {
4186 error ("cccp error: not in any file?!");
4187 return; /* the show must go on */
4190 switch (hp->type) {
4191 case T_FILE:
4192 case T_BASE_FILE:
4194 FILE_BUF *p = hp->type == T_FILE ? ip : &instack[0];
4195 const char *string = p->nominal_fname;
4197 if (string)
4199 size_t string_len = p->nominal_fname_len;
4200 char *newbuf = (char *) alloca (3 + 4 * string_len);
4201 quote_string (newbuf, string, string_len);
4202 buf = newbuf;
4204 else
4205 buf = "\"\"";
4207 break;
4210 case T_INCLUDE_LEVEL:
4212 /* Eight bytes ought to be more than enough */
4213 char *newbuf = (char *) alloca (8);
4214 true_indepth = 0;
4215 for (i = indepth; i >= 0; i--)
4216 if (instack[i].fname != NULL)
4217 true_indepth++;
4218 sprintf (newbuf, "%d", true_indepth - 1);
4219 buf = newbuf;
4221 break;
4223 case T_VERSION:
4225 char *newbuf = (char *) alloca (3 + strlen (version_string));
4226 sprintf (newbuf, "\"%s\"", version_string);
4227 buf = newbuf;
4229 break;
4231 #ifndef NO_BUILTIN_SIZE_TYPE
4232 case T_SIZE_TYPE:
4233 buf = SIZE_TYPE;
4234 break;
4235 #endif
4237 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4238 case T_PTRDIFF_TYPE:
4239 buf = PTRDIFF_TYPE;
4240 break;
4241 #endif
4243 case T_WCHAR_TYPE:
4244 buf = wchar_type;
4245 break;
4247 case T_USER_LABEL_PREFIX_TYPE:
4248 buf = user_label_prefix;
4249 break;
4251 case T_REGISTER_PREFIX_TYPE:
4252 buf = REGISTER_PREFIX;
4253 break;
4255 case T_IMMEDIATE_PREFIX_TYPE:
4256 buf = IMMEDIATE_PREFIX;
4257 break;
4259 case T_CONST:
4260 buf = hp->value.cpval;
4261 #ifdef STDC_0_IN_SYSTEM_HEADERS
4262 if (ip->system_header_p
4263 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
4264 && !lookup ((const U_CHAR *) "__STRICT_ANSI__", -1, -1))
4265 buf = "0";
4266 #endif
4267 if (pcp_inside_if && pcp_outfile)
4268 /* Output a precondition for this macro use */
4269 fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
4270 break;
4272 case T_SPECLINE:
4274 char *newbuf = (char *) alloca (10);
4275 sprintf (newbuf, "%d", ip->lineno);
4276 buf = newbuf;
4278 break;
4280 case T_DATE:
4281 case T_TIME:
4283 char *newbuf = (char *) alloca (20);
4284 timebuf = timestamp ();
4285 if (hp->type == T_DATE)
4286 sprintf (newbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
4287 timebuf->tm_mday, timebuf->tm_year + 1900);
4288 else
4289 sprintf (newbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour,
4290 timebuf->tm_min, timebuf->tm_sec);
4291 buf = newbuf;
4293 break;
4295 case T_SPEC_DEFINED:
4296 buf = " 0 "; /* Assume symbol is not defined */
4297 ip = &instack[indepth];
4298 SKIP_WHITE_SPACE (ip->bufp);
4299 if (*ip->bufp == '(') {
4300 paren++;
4301 ip->bufp++; /* Skip over the paren */
4302 SKIP_WHITE_SPACE (ip->bufp);
4305 if (!is_idstart[*ip->bufp])
4306 goto oops;
4307 if (ip->bufp[0] == 'L' && (ip->bufp[1] == '\'' || ip->bufp[1] == '"'))
4308 goto oops;
4309 if ((hp = lookup (ip->bufp, -1, -1))) {
4310 if (pcp_outfile && pcp_inside_if
4311 && (hp->type == T_CONST
4312 || (hp->type == T_MACRO && hp->value.defn->predefined)))
4313 /* Output a precondition for this macro use. */
4314 fprintf (pcp_outfile, "#define %s\n", hp->name);
4315 if (hp->type == T_POISON) {
4316 error("attempt to use poisoned `%s'.", hp->name);
4317 buf = " 0 ";
4318 } else {
4319 buf = " 1 ";
4322 else
4323 if (pcp_outfile && pcp_inside_if) {
4324 /* Output a precondition for this macro use */
4325 U_CHAR *cp = ip->bufp;
4326 fprintf (pcp_outfile, "#undef ");
4327 while (is_idchar[*cp]) /* Ick! */
4328 fputc (*cp++, pcp_outfile);
4329 putc ('\n', pcp_outfile);
4331 while (is_idchar[*ip->bufp])
4332 ++ip->bufp;
4333 SKIP_WHITE_SPACE (ip->bufp);
4334 if (paren) {
4335 if (*ip->bufp != ')')
4336 goto oops;
4337 ++ip->bufp;
4339 break;
4341 case T_POISON:
4342 error("attempt to use poisoned `%s'.", hp->name);
4343 buf = " 0 "; /* Consider poisoned symbol to not be defined */
4344 break;
4346 oops:
4348 error ("`defined' without an identifier");
4349 break;
4351 default:
4352 error ("cccp error: invalid special hash type"); /* time for gdb */
4353 abort ();
4355 len = strlen (buf);
4356 check_expand (op, len);
4357 bcopy (buf, (char *) op->bufp, len);
4358 op->bufp += len;
4360 return;
4364 /* Routines to handle #directives */
4366 /* Handle #include and #import.
4367 This function expects to see "fname" or <fname> on the input. */
4369 static int
4370 do_include (buf, limit, op, keyword)
4371 U_CHAR *buf, *limit;
4372 FILE_BUF *op;
4373 struct directive *keyword;
4375 const U_CHAR *importing =
4376 keyword->type == T_IMPORT ? (const U_CHAR *) "" : (const U_CHAR *) 0;
4377 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4378 static int import_warning = 0;
4379 char *fname; /* Dynamically allocated fname buffer */
4380 char *pcftry;
4381 char *pcfname;
4382 char *fbeg, *fend; /* Beginning and end of fname */
4383 U_CHAR *fin;
4385 struct file_name_list *search_start = include; /* Chain of dirs to search */
4386 struct file_name_list *dsp; /* First in chain, if #include "..." */
4387 struct file_name_list *searchptr = 0;
4388 size_t flen;
4390 int f = -3; /* file number */
4391 struct include_file *inc = 0;
4393 int retried = 0; /* Have already tried macro
4394 expanding the include line*/
4395 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4396 #ifdef VMS
4397 int vaxc_include = 0; /* 1 for token without punctuation */
4398 #endif
4399 int pcf = -1;
4400 char *pcfbuf;
4401 const char *pcfbuflimit;
4402 int pcfnum;
4404 if (pedantic && !instack[indepth].system_header_p)
4406 if (importing)
4407 pedwarn ("ANSI C does not allow `#import'");
4408 if (skip_dirs)
4409 pedwarn ("ANSI C does not allow `#include_next'");
4412 if (importing && warn_import && !inhibit_warnings
4413 && !instack[indepth].system_header_p && !import_warning) {
4414 import_warning = 1;
4415 warning ("using `#import' is not recommended");
4416 notice ("The fact that a certain header file need not be processed more than once\n\
4417 should be indicated in the header file, not where it is used.\n\
4418 The best way to do this is with a conditional of this form:\n\
4420 #ifndef _FOO_H_INCLUDED\n\
4421 #define _FOO_H_INCLUDED\n\
4422 ... <real contents of file> ...\n\
4423 #endif /* Not _FOO_H_INCLUDED */\n\
4425 Then users can use `#include' any number of times.\n\
4426 GNU C automatically avoids processing the file more than once\n\
4427 when it is equipped with such a conditional.\n");
4430 get_filename:
4432 fin = buf;
4433 SKIP_WHITE_SPACE (fin);
4434 /* Discard trailing whitespace so we can easily see
4435 if we have parsed all the significant chars we were given. */
4436 while (limit != fin && is_hor_space[limit[-1]]) limit--;
4437 fbeg = fend = (char *) alloca (limit - fin);
4439 switch (*fin++) {
4440 case '\"':
4442 FILE_BUF *fp;
4443 /* Copy the operand text, concatenating the strings. */
4445 for (;;) {
4446 for (;;) {
4447 if (fin == limit)
4448 goto invalid_include_file_name;
4449 *fend = *fin++;
4450 if (*fend == '"')
4451 break;
4452 fend++;
4454 if (fin == limit)
4455 break;
4456 /* If not at the end, there had better be another string. */
4457 /* Skip just horiz space, and don't go past limit. */
4458 while (fin != limit && is_hor_space[*fin]) fin++;
4459 if (fin != limit && *fin == '\"')
4460 fin++;
4461 else
4462 goto fail;
4466 /* We have "filename". Figure out directory this source
4467 file is coming from and put it on the front of the list. */
4469 /* If -I- was specified, don't search current dir, only spec'd ones. */
4470 if (ignore_srcdir) break;
4472 for (fp = &instack[indepth]; fp >= instack; fp--)
4474 int n;
4476 if ((fp->nominal_fname) != NULL) {
4477 char *nam;
4478 /* Found a named file. Figure out dir of the file,
4479 and put it in front of the search list. */
4480 dsp = ((struct file_name_list *)
4481 alloca (sizeof (struct file_name_list)
4482 + fp->nominal_fname_len));
4483 strcpy (dsp->fname, fp->nominal_fname);
4484 simplify_filename (dsp->fname);
4485 nam = base_name (dsp->fname);
4486 *nam = 0;
4487 #ifdef VMS
4488 /* for hack_vms_include_specification(), a local
4489 dir specification must start with "./" on VMS. */
4490 if (nam == dsp->fname)
4492 *nam++ = '.';
4493 *nam++ = '/';
4494 *nam = 0;
4496 #endif
4497 /* But for efficiency's sake, do not insert the dir
4498 if it matches the search list's first dir. */
4499 dsp->next = search_start;
4500 if (!search_start || strcmp (dsp->fname, search_start->fname)) {
4501 search_start = dsp;
4502 n = nam - dsp->fname;
4503 if (n + INCLUDE_LEN_FUDGE > max_include_len)
4504 max_include_len = n + INCLUDE_LEN_FUDGE;
4506 dsp[0].got_name_map = 0;
4507 break;
4510 break;
4513 case '<':
4514 while (fin != limit && *fin != '>')
4515 *fend++ = *fin++;
4516 if (*fin == '>' && fin + 1 == limit) {
4517 angle_brackets = 1;
4518 /* If -I-, start with the first -I dir after the -I-. */
4519 search_start = first_bracket_include;
4520 break;
4522 goto fail;
4524 default:
4525 #ifdef VMS
4527 * Support '#include xyz' like VAX-C to allow for easy use of all the
4528 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4529 * code from case '<' is repeated here) and generates a warning.
4530 * (Note: macro expansion of `xyz' takes precedence.)
4532 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4533 the pre-decrement outside of the macro. */
4534 if (retried && (--fin, ISALPHA(*(U_CHAR *) (fin)))) {
4535 while (fin != limit && (!ISSPACE(*fin)))
4536 *fend++ = *fin++;
4537 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4538 vaxc_include = 1;
4539 if (fin == limit) {
4540 angle_brackets = 1;
4541 /* If -I-, start with the first -I dir after the -I-. */
4542 search_start = first_bracket_include;
4543 break;
4546 #endif
4548 fail:
4549 if (! retried) {
4550 /* Expand buffer and then remove any newline markers.
4551 We can't just tell expand_to_temp_buffer to omit the markers,
4552 since it would put extra spaces in include file names. */
4553 U_CHAR *src;
4554 int errors_before_expansion = errors;
4555 FILE_BUF trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
4556 if (errors != errors_before_expansion) {
4557 free (trybuf.buf);
4558 goto invalid_include_file_name;
4560 src = trybuf.buf;
4561 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
4562 limit = buf;
4563 while (src != trybuf.bufp) {
4564 switch ((*limit++ = *src++)) {
4565 case '\n':
4566 limit--;
4567 src++;
4568 break;
4570 case '\'':
4571 case '\"':
4573 const U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
4574 NULL_PTR, NULL_PTR, NULL_PTR);
4575 while (src != src1)
4576 *limit++ = *src++;
4578 break;
4581 *limit = 0;
4582 free (trybuf.buf);
4583 retried = 1;
4584 goto get_filename;
4587 invalid_include_file_name:
4588 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
4589 return 0;
4592 /* For #include_next, skip in the search path
4593 past the dir in which the containing file was found. */
4594 if (skip_dirs) {
4595 FILE_BUF *fp;
4596 for (fp = &instack[indepth]; fp >= instack; fp--)
4597 if (fp->fname != NULL) {
4598 /* fp->dir is null if the containing file was specified
4599 with an absolute file name. In that case, don't skip anything. */
4600 if (fp->dir)
4601 search_start = fp->dir->next;
4602 break;
4606 *fend = 0;
4607 flen = simplify_filename (fbeg);
4609 if (flen == 0)
4611 error ("empty file name in `#%s'", keyword->name);
4612 return 0;
4615 /* Allocate this permanently, because it gets stored in the definitions
4616 of macros. */
4617 fname = xmalloc (max_include_len + flen + 1);
4618 /* + 1 above for terminating null. */
4620 system_include_depth += angle_brackets;
4622 /* If specified file name is absolute, just open it. */
4624 if (absolute_filename (fbeg)) {
4625 strcpy (fname, fbeg);
4626 f = open_include_file (fname, NULL_PTR, importing, &inc);
4627 } else {
4629 struct bypass_dir {
4630 struct bypass_dir *next;
4631 char *fname;
4632 struct file_name_list *searchptr;
4633 } **bypass_slot = 0;
4635 /* Search directory path, trying to open the file.
4636 Copy each filename tried into FNAME. */
4638 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
4640 if (searchptr == first_bracket_include) {
4641 /* Go to bypass directory if we know we've seen this file before. */
4642 static struct bypass_dir *bypass_hashtab[INCLUDE_HASHSIZE];
4643 struct bypass_dir *p;
4644 bypass_slot = &bypass_hashtab[hashf ((U_CHAR *) fbeg, flen,
4645 INCLUDE_HASHSIZE)];
4646 for (p = *bypass_slot; p; p = p->next)
4647 if (!strcmp (fbeg, p->fname)) {
4648 searchptr = p->searchptr;
4649 bypass_slot = 0;
4650 break;
4654 #ifdef VMS
4655 /* Change this 1/2 Unix 1/2 VMS file specification into a
4656 full VMS file specification */
4657 if (searchptr->fname[0])
4659 strcpy (fname, searchptr->fname);
4660 if (fname[strlen (fname) - 1] == ':')
4662 char *slashp;
4663 slashp = strchr (fbeg, '/');
4665 /* start at root-dir of logical device if no path given. */
4666 if (slashp == 0)
4667 strcat (fname, "[000000]");
4669 strcat (fname, fbeg);
4671 /* Fix up the filename */
4672 hack_vms_include_specification (fname, vaxc_include);
4674 else
4676 /* This is a normal VMS filespec, so use it unchanged. */
4677 strcpy (fname, fbeg);
4678 /* if it's '#include filename', add the missing .h */
4679 if (vaxc_include && index(fname,'.')==NULL)
4680 strcat (fname, ".h");
4682 #else
4683 strcpy (fname, searchptr->fname);
4684 strcat (fname, fbeg);
4685 #endif /* VMS */
4686 f = open_include_file (fname, searchptr, importing, &inc);
4687 if (f != -1) {
4688 if (bypass_slot && searchptr != first_bracket_include) {
4689 /* This is the first time we found this include file,
4690 and we found it after first_bracket_include.
4691 Record its location so that we can bypass to here next time. */
4692 struct bypass_dir *p
4693 = (struct bypass_dir *) xmalloc (sizeof (struct bypass_dir));
4694 p->next = *bypass_slot;
4695 p->fname = fname + strlen (searchptr->fname);
4696 p->searchptr = searchptr;
4697 *bypass_slot = p;
4699 break;
4701 #ifdef VMS
4702 /* Our VMS hacks can produce invalid filespecs, so don't worry
4703 about errors other than EACCES. */
4704 if (errno == EACCES)
4705 break;
4706 #else
4707 if (errno != ENOENT && errno != ENOTDIR)
4708 break;
4709 #endif
4714 if (f < 0) {
4716 if (f == -2) {
4717 /* The file was already included. */
4719 /* If generating dependencies and -MG was specified, we assume missing
4720 files are leaf files, living in the same directory as the source file
4721 or other similar place; these missing files may be generated from
4722 other files and may not exist yet (eg: y.tab.h). */
4723 } else if (print_deps_missing_files
4724 && (system_include_depth != 0) < print_deps)
4726 /* If it was requested as a system header file,
4727 then assume it belongs in the first place to look for such. */
4728 if (angle_brackets)
4730 if (search_start) {
4731 char *p = (char *) alloca (strlen (search_start->fname)
4732 + strlen (fbeg) + 1);
4733 strcpy (p, search_start->fname);
4734 strcat (p, fbeg);
4735 deps_output (p, ' ');
4738 else
4740 /* Otherwise, omit the directory, as if the file existed
4741 in the directory with the source. */
4742 deps_output (fbeg, ' ');
4745 /* If -M was specified, and this header file won't be added to the
4746 dependency list, then don't count this as an error, because we can
4747 still produce correct output. Otherwise, we can't produce correct
4748 output, because there may be dependencies we need inside the missing
4749 file, and we don't know what directory this missing file exists in. */
4750 else if (0 < print_deps && print_deps <= (system_include_depth != 0))
4751 warning ("No include path in which to find %s", fbeg);
4752 else if (f != -3)
4753 error_from_errno (fbeg);
4754 else
4755 error ("No include path in which to find %s", fbeg);
4757 } else {
4759 /* Actually process the file. */
4761 pcftry = (char *) alloca (strlen (fname) + 30);
4762 pcfbuf = 0;
4763 pcfnum = 0;
4765 if (!no_precomp)
4767 do {
4768 sprintf (pcftry, "%s%d", fname, pcfnum++);
4770 pcf = open (pcftry, O_RDONLY, 0666);
4771 if (pcf != -1)
4773 struct stat s;
4775 if (fstat (pcf, &s) != 0)
4776 pfatal_with_name (pcftry);
4777 if (! INO_T_EQ (inc->st.st_ino, s.st_ino)
4778 || inc->st.st_dev != s.st_dev)
4780 pcfbuf = check_precompiled (pcf, &s, fname, &pcfbuflimit);
4781 /* Don't need it any more. */
4782 close (pcf);
4784 else
4786 /* Don't need it at all. */
4787 close (pcf);
4788 break;
4791 } while (pcf != -1 && !pcfbuf);
4794 /* Actually process the file */
4795 if (pcfbuf) {
4796 pcfname = xstrdup (pcftry);
4797 pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) fname, op);
4799 else
4800 finclude (f, inc, op, is_system_include (fname), searchptr);
4803 system_include_depth -= angle_brackets;
4805 return 0;
4808 /* Return nonzero if the given FILENAME is an absolute pathname which
4809 designates a file within one of the known "system" include file
4810 directories. We assume here that if the given FILENAME looks like
4811 it is the name of a file which resides either directly in a "system"
4812 include file directory, or within any subdirectory thereof, then the
4813 given file must be a "system" include file. This function tells us
4814 if we should suppress pedantic errors/warnings for the given FILENAME.
4816 The value is 2 if the file is a C-language system header file
4817 for which C++ should (on most systems) assume `extern "C"'. */
4819 static int
4820 is_system_include (filename)
4821 register const char *filename;
4823 struct file_name_list *searchptr;
4825 for (searchptr = first_system_include; searchptr;
4826 searchptr = searchptr->next)
4827 if (! strncmp (searchptr->fname, filename, strlen (searchptr->fname)))
4828 return searchptr->c_system_include_path + 1;
4829 return 0;
4832 /* Yield the non-directory suffix of a file name. */
4834 static char *
4835 base_name (fname)
4836 const char *fname;
4838 const char *s = fname;
4839 const char *p;
4840 #if defined (__MSDOS__) || defined (_WIN32)
4841 if (ISALPHA (s[0]) && s[1] == ':') s += 2;
4842 #endif
4843 #ifdef VMS
4844 if ((p = rindex (s, ':'))) s = p + 1; /* Skip device. */
4845 if ((p = rindex (s, ']'))) s = p + 1; /* Skip directory. */
4846 if ((p = rindex (s, '>'))) s = p + 1; /* Skip alternate (int'n'l) dir. */
4847 if (s != fname)
4848 return (char *) s;
4849 #endif
4850 if ((p = rindex (s, '/'))) s = p + 1;
4851 #ifdef DIR_SEPARATOR
4852 if ((p = rindex (s, DIR_SEPARATOR))) s = p + 1;
4853 #endif
4854 return (char *) s;
4857 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4859 static int
4860 absolute_filename (filename)
4861 const char *filename;
4863 #if defined (__MSDOS__) \
4864 || (defined (_WIN32) && !defined (__CYGWIN__) && !defined (_UWIN))
4865 if (ISALPHA (filename[0]) && filename[1] == ':') filename += 2;
4866 #endif
4867 #if defined (__CYGWIN__)
4868 /* At present, any path that begins with a drive spec is absolute. */
4869 if (ISALPHA (filename[0]) && filename[1] == ':') return 1;
4870 #endif
4871 #ifdef VMS
4872 if (index (filename, ':') != 0) return 1;
4873 #endif
4874 if (filename[0] == '/') return 1;
4875 #ifdef DIR_SEPARATOR
4876 if (filename[0] == DIR_SEPARATOR) return 1;
4877 #endif
4878 return 0;
4881 /* Returns whether or not a given character is a directory separator.
4882 Used by simplify_filename. */
4883 static inline int is_dir_separator PROTO ((int));
4885 static inline
4887 is_dir_separator(ch)
4888 char ch;
4890 return (ch == DIR_SEPARATOR)
4891 #if defined (DIR_SEPARATOR_2)
4892 || (ch == DIR_SEPARATOR_2)
4893 #endif
4897 /* Remove unnecessary characters from FILENAME in place,
4898 to avoid unnecessary filename aliasing.
4899 Return the length of the resulting string.
4901 Do only the simplifications allowed by Posix.
4902 It is OK to miss simplifications on non-Posix hosts,
4903 since this merely leads to suboptimal results. */
4905 static size_t
4906 simplify_filename (filename)
4907 char *filename;
4909 register char *from = filename;
4910 register char *to = filename;
4911 char *to0;
4913 /* Remove redundant initial /s. */
4914 if (is_dir_separator (*from))
4916 *to++ = DIR_SEPARATOR;
4917 if (is_dir_separator (*++from))
4919 if (is_dir_separator (*++from))
4921 /* 3 or more initial /s are equivalent to 1 /. */
4922 while (is_dir_separator (*++from))
4923 continue;
4925 else
4927 /* On some hosts // differs from /; Posix allows this. */
4928 *to++ = DIR_SEPARATOR;
4933 to0 = to;
4935 for (;;)
4937 #ifndef VMS
4938 if (from[0] == '.' && from[1] == '/')
4939 from += 2;
4940 else
4941 #endif
4943 /* Copy this component and trailing DIR_SEPARATOR, if any. */
4944 while (!is_dir_separator (*to++ = *from++))
4946 if (!to[-1])
4948 /* Trim . component at end of nonempty name. */
4949 to -= filename <= to - 3 && to[-3] == DIR_SEPARATOR && to[-2] == '.';
4951 /* Trim unnecessary trailing /s. */
4952 while (to0 < --to && to[-1] == DIR_SEPARATOR)
4953 continue;
4955 *to = 0;
4956 return to - filename;
4959 #if defined(DIR_SEPARATOR_2)
4960 /* Simplify to one directory separator. */
4961 to[-1] = DIR_SEPARATOR;
4962 #endif
4965 /* Skip /s after a /. */
4966 while (is_dir_separator (*from))
4967 from++;
4971 /* The file_name_map structure holds a mapping of file names for a
4972 particular directory. This mapping is read from the file named
4973 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
4974 map filenames on a file system with severe filename restrictions,
4975 such as DOS. The format of the file name map file is just a series
4976 of lines with two tokens on each line. The first token is the name
4977 to map, and the second token is the actual name to use. */
4979 struct file_name_map
4981 struct file_name_map *map_next;
4982 char *map_from;
4983 char *map_to;
4986 #define FILE_NAME_MAP_FILE "header.gcc"
4988 /* Read a space delimited string of unlimited length from a stdio
4989 file. */
4991 static char *
4992 read_filename_string (ch, f)
4993 int ch;
4994 FILE *f;
4996 char *alloc, *set;
4997 int len;
4999 len = 20;
5000 set = alloc = xmalloc (len + 1);
5001 if (! is_space[ch])
5003 *set++ = ch;
5004 while ((ch = getc (f)) != EOF && ! is_space[ch])
5006 if (set - alloc == len)
5008 len *= 2;
5009 alloc = xrealloc (alloc, len + 1);
5010 set = alloc + len / 2;
5012 *set++ = ch;
5015 *set = '\0';
5016 ungetc (ch, f);
5017 return alloc;
5020 /* Read the file name map file for DIRNAME.
5021 If DIRNAME is empty, read the map file for the working directory;
5022 otherwise DIRNAME must end in '/'. */
5024 static struct file_name_map *
5025 read_name_map (dirname)
5026 const char *dirname;
5028 /* This structure holds a linked list of file name maps, one per
5029 directory. */
5030 struct file_name_map_list
5032 struct file_name_map_list *map_list_next;
5033 char *map_list_name;
5034 struct file_name_map *map_list_map;
5036 static struct file_name_map_list *map_list;
5037 register struct file_name_map_list *map_list_ptr;
5038 char *name;
5039 FILE *f;
5040 size_t dirlen;
5042 for (map_list_ptr = map_list; map_list_ptr;
5043 map_list_ptr = map_list_ptr->map_list_next)
5044 if (! strcmp (map_list_ptr->map_list_name, dirname))
5045 return map_list_ptr->map_list_map;
5047 map_list_ptr = ((struct file_name_map_list *)
5048 xmalloc (sizeof (struct file_name_map_list)));
5049 map_list_ptr->map_list_name = xstrdup (dirname);
5050 map_list_ptr->map_list_map = NULL;
5052 dirlen = strlen (dirname);
5053 name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 1);
5054 strcpy (name, dirname);
5055 strcat (name, FILE_NAME_MAP_FILE);
5056 f = fopen (name, "r");
5057 if (!f)
5058 map_list_ptr->map_list_map = NULL;
5059 else
5061 int ch;
5063 while ((ch = getc (f)) != EOF)
5065 char *from, *to;
5066 struct file_name_map *ptr;
5067 size_t tolen;
5069 if (is_space[ch])
5070 continue;
5071 from = read_filename_string (ch, f);
5072 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5074 to = read_filename_string (ch, f);
5076 simplify_filename (from);
5077 tolen = simplify_filename (to);
5079 ptr = ((struct file_name_map *)
5080 xmalloc (sizeof (struct file_name_map)));
5081 ptr->map_from = from;
5083 /* Make the real filename absolute. */
5084 if (absolute_filename (to))
5085 ptr->map_to = to;
5086 else
5088 ptr->map_to = xmalloc (dirlen + tolen + 1);
5089 strcpy (ptr->map_to, dirname);
5090 strcat (ptr->map_to, to);
5091 free (to);
5094 ptr->map_next = map_list_ptr->map_list_map;
5095 map_list_ptr->map_list_map = ptr;
5097 while ((ch = getc (f)) != '\n')
5098 if (ch == EOF)
5099 break;
5101 fclose (f);
5104 map_list_ptr->map_list_next = map_list;
5105 map_list = map_list_ptr;
5107 return map_list_ptr->map_list_map;
5110 /* Try to open include file FILENAME. SEARCHPTR is the directory
5111 being tried from the include file search path.
5112 IMPORTING is "" if we are importing, null otherwise.
5113 Return -2 if found, either a matching name or a matching inode.
5114 Otherwise, open the file and return a file descriptor if successful
5115 or -1 if unsuccessful.
5116 Unless unsuccessful, put a descriptor of the included file into *PINC.
5117 This function maps filenames on file systems based on information read by
5118 read_name_map. */
5120 static int
5121 open_include_file (filename, searchptr, importing, pinc)
5122 char *filename;
5123 struct file_name_list *searchptr;
5124 const U_CHAR *importing;
5125 struct include_file **pinc;
5127 char *fname = remap ? remap_include_file (filename, searchptr) : filename;
5128 int fd = -2;
5130 /* Look up FNAME in include_hashtab. */
5131 struct include_file **phead = &include_hashtab[hashf ((U_CHAR *) fname,
5132 strlen (fname),
5133 INCLUDE_HASHSIZE)];
5134 struct include_file *inc, *head = *phead;
5135 for (inc = head; inc; inc = inc->next)
5136 if (!strcmp (fname, inc->fname))
5137 break;
5139 if (!inc
5140 || ! inc->control_macro
5141 || (inc->control_macro[0] && ! lookup (inc->control_macro, -1, -1))) {
5143 fd = open (fname, O_RDONLY, 0);
5145 if (fd < 0)
5147 #ifdef VMS
5148 /* if #include <dir/file> fails, try again with hacked spec. */
5149 if (!hack_vms_include_specification (fname, 0))
5150 return fd;
5151 fd = open (fname, O_RDONLY, 0);
5152 if (fd < 0)
5153 #endif
5154 return fd;
5157 if (!inc) {
5158 /* FNAME was not in include_hashtab; insert a new entry. */
5159 inc = (struct include_file *) xmalloc (sizeof (struct include_file));
5160 inc->next = head;
5161 inc->fname = fname;
5162 inc->control_macro = 0;
5163 inc->deps_output = 0;
5164 if (fstat (fd, &inc->st) != 0)
5165 pfatal_with_name (fname);
5166 *phead = inc;
5168 /* Look for another file with the same inode and device. */
5169 if (lookup_ino_include (inc)
5170 && inc->control_macro
5171 && (!inc->control_macro[0] || lookup (inc->control_macro, -1, -1))) {
5172 close (fd);
5173 fd = -2;
5177 /* For -M, add this file to the dependencies. */
5178 if (! inc->deps_output && (system_include_depth != 0) < print_deps) {
5179 inc->deps_output = 1;
5180 deps_output (fname, ' ');
5183 /* Handle -H option. */
5184 if (print_include_names)
5185 fprintf (stderr, "%*s%s\n", indepth, "", fname);
5188 if (importing)
5189 inc->control_macro = importing;
5191 *pinc = inc;
5192 return fd;
5195 /* Return the remapped name of the include file FILENAME.
5196 SEARCHPTR is the directory being tried from the include file path. */
5198 static char *
5199 remap_include_file (filename, searchptr)
5200 char *filename;
5201 struct file_name_list *searchptr;
5203 register struct file_name_map *map;
5204 register const char *from;
5206 if (searchptr)
5208 if (! searchptr->got_name_map)
5210 searchptr->name_map = read_name_map (searchptr->fname);
5211 searchptr->got_name_map = 1;
5214 /* Check the mapping for the directory we are using. */
5215 from = filename + strlen (searchptr->fname);
5216 for (map = searchptr->name_map; map; map = map->map_next)
5217 if (! strcmp (map->map_from, from))
5218 return map->map_to;
5221 from = base_name (filename);
5223 if (from != filename || !searchptr)
5225 /* Try to find a mapping file for the particular directory we are
5226 looking in. Thus #include <sys/types.h> will look up sys/types.h
5227 in /usr/include/header.gcc and look up types.h in
5228 /usr/include/sys/header.gcc. */
5230 char *dir = (char *) alloca (from - filename + 1);
5231 bcopy (filename, dir, from - filename);
5232 dir[from - filename] = '\0';
5234 for (map = read_name_map (dir); map; map = map->map_next)
5235 if (! strcmp (map->map_from, from))
5236 return map->map_to;
5239 return filename;
5242 /* Insert INC into the include file table, hashed by device and inode number.
5243 If a file with different name but same dev+ino was already in the table,
5244 return 1 and set INC's control macro to the already-known macro. */
5246 static int
5247 lookup_ino_include (inc)
5248 struct include_file *inc;
5250 int hash = ((unsigned) (inc->st.st_dev + INO_T_HASH (inc->st.st_ino))
5251 % INCLUDE_HASHSIZE);
5252 struct include_file *i = include_ino_hashtab[hash];
5253 inc->next_ino = i;
5254 include_ino_hashtab[hash] = inc;
5256 for (; i; i = i->next_ino)
5257 if (INO_T_EQ (inc->st.st_ino, i->st.st_ino)
5258 && inc->st.st_dev == i->st.st_dev) {
5259 inc->control_macro = i->control_macro;
5260 return 1;
5263 return 0;
5266 /* Process file descriptor F, which corresponds to include file INC,
5267 with output to OP.
5268 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5269 "system" include directories (as decided by the `is_system_include'
5270 function above).
5271 DIRPTR is the link in the dir path through which this file was found,
5272 or 0 if the file name was absolute. */
5274 static void
5275 finclude (f, inc, op, system_header_p, dirptr)
5276 int f;
5277 struct include_file *inc;
5278 FILE_BUF *op;
5279 int system_header_p;
5280 struct file_name_list *dirptr;
5282 char *fname = inc->fname;
5283 int i;
5284 FILE_BUF *fp; /* For input stack frame */
5285 int missing_newline = 0;
5287 CHECK_DEPTH (return;);
5289 fp = &instack[indepth + 1];
5290 bzero ((char *) fp, sizeof (FILE_BUF));
5291 fp->nominal_fname = fp->fname = fname;
5292 fp->nominal_fname_len = strlen (fname);
5293 fp->inc = inc;
5294 fp->length = 0;
5295 fp->lineno = 1;
5296 fp->if_stack = if_stack;
5297 fp->system_header_p = system_header_p;
5298 fp->dir = dirptr;
5300 if (S_ISREG (inc->st.st_mode)) {
5301 size_t s = (size_t) inc->st.st_size;
5302 if (s != inc->st.st_size || s + 2 < s)
5303 memory_full ();
5304 fp->buf = (U_CHAR *) xmalloc (s + 2);
5305 fp->bufp = fp->buf;
5307 /* Read the file contents, knowing that s is an upper bound
5308 on the number of bytes we can read. */
5309 fp->length = safe_read (f, (char *) fp->buf, s);
5310 if (fp->length < 0) goto nope;
5312 else if (S_ISDIR (inc->st.st_mode)) {
5313 error ("directory `%s' specified in #include", fname);
5314 close (f);
5315 return;
5316 } else {
5317 /* Cannot count its file size before reading.
5318 First read the entire file into heap and
5319 copy them into buffer on stack. */
5321 int bsize = 2000;
5322 int st_size = 0;
5324 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5326 for (;;) {
5327 i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
5328 if (i < 0)
5329 goto nope; /* error! */
5330 st_size += i;
5331 if (st_size != bsize)
5332 break; /* End of file */
5333 bsize *= 2;
5334 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5336 fp->bufp = fp->buf;
5337 fp->length = st_size;
5340 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5341 /* Backslash-newline at end is not good enough. */
5342 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5343 fp->buf[fp->length++] = '\n';
5344 missing_newline = 1;
5346 fp->buf[fp->length] = '\0';
5348 /* Close descriptor now, so nesting does not use lots of descriptors. */
5349 close (f);
5351 /* Must do this before calling trigraph_pcp, so that the correct file name
5352 will be printed in warning messages. */
5354 indepth++;
5355 input_file_stack_tick++;
5357 if (!no_trigraphs)
5358 trigraph_pcp (fp);
5360 if (warn_white_space)
5361 check_white_space (fp);
5363 output_line_directive (fp, op, 0, enter_file);
5364 rescan (op, 0);
5366 if (missing_newline)
5367 fp->lineno--;
5369 if (pedantic && missing_newline)
5370 pedwarn ("file does not end in newline");
5372 indepth--;
5373 input_file_stack_tick++;
5374 output_line_directive (&instack[indepth], op, 0, leave_file);
5375 free (fp->buf);
5376 return;
5378 nope:
5380 perror_with_name (fname);
5381 close (f);
5382 free (fp->buf);
5385 /* Record that inclusion of the include file INC
5386 should be controlled by the macro named MACRO_NAME.
5387 This means that trying to include the file again
5388 will do something if that macro is defined. */
5390 static void
5391 record_control_macro (inc, macro_name)
5392 struct include_file *inc;
5393 const U_CHAR *macro_name;
5395 if (!inc->control_macro || inc->control_macro[0])
5396 inc->control_macro = macro_name;
5399 /* Load the specified precompiled header into core, and verify its
5400 preconditions. PCF indicates the file descriptor to read, which must
5401 be a regular file. *ST is its file status.
5402 FNAME indicates the file name of the original header.
5403 *LIMIT will be set to an address one past the end of the file.
5404 If the preconditions of the file are not satisfied, the buffer is
5405 freed and we return 0. If the preconditions are satisfied, return
5406 the address of the buffer following the preconditions. The buffer, in
5407 this case, should never be freed because various pieces of it will
5408 be referred to until all precompiled strings are output at the end of
5409 the run. */
5411 static char *
5412 check_precompiled (pcf, st, fname, limit)
5413 int pcf;
5414 struct stat *st;
5415 const char *fname ATTRIBUTE_UNUSED;
5416 const char **limit;
5418 int length = 0;
5419 char *buf;
5420 char *cp;
5422 if (pcp_outfile)
5423 return 0;
5425 if (S_ISREG (st->st_mode))
5427 size_t s = (size_t) st->st_size;
5428 if (s != st->st_size || s + 2 < s)
5429 memory_full ();
5430 buf = xmalloc (s + 2);
5431 length = safe_read (pcf, buf, s);
5432 if (length < 0)
5433 goto nope;
5435 else
5436 abort ();
5438 if (length > 0 && buf[length-1] != '\n')
5439 buf[length++] = '\n';
5440 buf[length] = '\0';
5442 *limit = buf + length;
5444 /* File is in core. Check the preconditions. */
5445 if (!check_preconditions (buf))
5446 goto nope;
5447 for (cp = buf; *cp; cp++)
5449 #ifdef DEBUG_PCP
5450 fprintf (stderr, "Using preinclude %s\n", fname);
5451 #endif
5452 return cp + 1;
5454 nope:
5455 #ifdef DEBUG_PCP
5456 fprintf (stderr, "Cannot use preinclude %s\n", fname);
5457 #endif
5458 free (buf);
5459 return 0;
5462 /* PREC (null terminated) points to the preconditions of a
5463 precompiled header. These are a series of #define and #undef
5464 lines which must match the current contents of the hash
5465 table. */
5467 static int
5468 check_preconditions (prec)
5469 const char *prec;
5471 MACRODEF mdef;
5472 const char *lineend;
5474 while (*prec) {
5475 lineend = index (prec, '\n');
5477 if (*prec++ != '#') {
5478 error ("Bad format encountered while reading precompiled file");
5479 return 0;
5481 if (!strncmp (prec, "define", 6)) {
5482 HASHNODE *hp;
5484 prec += 6;
5485 mdef = create_definition ((const U_CHAR *) prec,
5486 (const U_CHAR *) lineend, NULL_PTR);
5488 if (mdef.defn == 0)
5489 abort ();
5491 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
5492 || (hp->type != T_MACRO && hp->type != T_CONST)
5493 || (hp->type == T_MACRO
5494 && !compare_defs (mdef.defn, hp->value.defn)
5495 && (mdef.defn->length != 2
5496 || mdef.defn->expansion[0] != '\n'
5497 || mdef.defn->expansion[1] != ' ')))
5498 return 0;
5499 } else if (!strncmp (prec, "undef", 5)) {
5500 const char *name;
5501 int len;
5503 prec += 5;
5504 while (is_hor_space[(U_CHAR) *prec])
5505 prec++;
5506 name = prec;
5507 while (is_idchar[(U_CHAR) *prec])
5508 prec++;
5509 len = prec - name;
5511 if (lookup ((const U_CHAR *) name, len, -1))
5512 return 0;
5513 } else {
5514 error ("Bad format encountered while reading precompiled file");
5515 return 0;
5517 prec = lineend + 1;
5519 /* They all passed successfully */
5520 return 1;
5523 /* Process the main body of a precompiled file. BUF points to the
5524 string section of the file, following the preconditions. LIMIT is one
5525 character past the end. NAME is the name of the file being read
5526 in. OP is the main output buffer. */
5528 static void
5529 pcfinclude (buf, name, op)
5530 U_CHAR *buf;
5531 const U_CHAR *name;
5532 FILE_BUF *op;
5534 FILE_BUF tmpbuf;
5535 int nstrings;
5536 U_CHAR *cp = buf;
5538 /* First in the file comes 4 bytes indicating the number of strings, */
5539 /* in network byte order. (MSB first). */
5540 nstrings = *cp++;
5541 nstrings = (nstrings << 8) | *cp++;
5542 nstrings = (nstrings << 8) | *cp++;
5543 nstrings = (nstrings << 8) | *cp++;
5545 /* Looping over each string... */
5546 while (nstrings--) {
5547 U_CHAR *string_start;
5548 U_CHAR *endofthiskey;
5549 STRINGDEF *str;
5550 int nkeys;
5552 /* Each string starts with a STRINGDEF structure (str), followed */
5553 /* by the text of the string (string_start) */
5555 /* First skip to a longword boundary */
5556 /* ??? Why a 4-byte boundary? On all machines? */
5557 /* NOTE: This works correctly even if size_t
5558 is narrower than a pointer.
5559 Do not try risky measures here to get another type to use!
5560 Do not include stddef.h--it will fail! */
5561 if ((size_t) cp & 3)
5562 cp += 4 - ((size_t) cp & 3);
5564 /* Now get the string. */
5565 str = (STRINGDEF *) (PTR) cp;
5566 string_start = cp += sizeof (STRINGDEF);
5568 for (; *cp; cp++) /* skip the string */
5571 /* We need to macro expand the string here to ensure that the
5572 proper definition environment is in place. If it were only
5573 expanded when we find out it is needed, macros necessary for
5574 its proper expansion might have had their definitions changed. */
5575 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
5576 /* Lineno is already set in the precompiled file */
5577 str->contents = tmpbuf.buf;
5578 str->len = tmpbuf.bufp - tmpbuf.buf;
5579 str->writeflag = 0;
5580 str->filename = name;
5581 str->output_mark = outbuf.bufp - outbuf.buf;
5583 str->chain = 0;
5584 *stringlist_tailp = str;
5585 stringlist_tailp = &str->chain;
5587 /* Next comes a fourbyte number indicating the number of keys
5588 for this string. */
5589 nkeys = *cp++;
5590 nkeys = (nkeys << 8) | *cp++;
5591 nkeys = (nkeys << 8) | *cp++;
5592 nkeys = (nkeys << 8) | *cp++;
5594 /* If this number is -1, then the string is mandatory. */
5595 if (nkeys == -1)
5596 str->writeflag = 1;
5597 else
5598 /* Otherwise, for each key, */
5599 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
5600 KEYDEF *kp = (KEYDEF *) (PTR) cp;
5601 HASHNODE *hp;
5602 U_CHAR *bp;
5604 /* It starts with a KEYDEF structure */
5605 cp += sizeof (KEYDEF);
5607 /* Find the end of the key. At the end of this for loop we
5608 advance CP to the start of the next key using this variable. */
5609 endofthiskey = cp + strlen ((char *) cp);
5610 kp->str = str;
5612 /* Expand the key, and enter it into the hash table. */
5613 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
5614 bp = tmpbuf.buf;
5616 while (is_hor_space[*bp])
5617 bp++;
5618 if (!is_idstart[*bp] || bp == tmpbuf.bufp) {
5619 str->writeflag = 1;
5620 continue;
5623 hp = lookup (bp, -1, -1);
5624 if (hp == NULL) {
5625 kp->chain = 0;
5626 install (bp, -1, T_PCSTRING, (char *) kp, -1);
5628 else if (hp->type == T_PCSTRING) {
5629 kp->chain = hp->value.keydef;
5630 hp->value.keydef = kp;
5632 else
5633 str->writeflag = 1;
5636 /* This output_line_directive serves to switch us back to the current
5637 input file in case some of these strings get output (which will
5638 result in line directives for the header file being output). */
5639 output_line_directive (&instack[indepth], op, 0, enter_file);
5642 /* Called from rescan when it hits a key for strings. Mark them all
5643 used and clean up. */
5645 static void
5646 pcstring_used (hp)
5647 HASHNODE *hp;
5649 KEYDEF *kp;
5651 for (kp = hp->value.keydef; kp; kp = kp->chain)
5652 kp->str->writeflag = 1;
5653 delete_macro (hp);
5656 /* Write the output, interspersing precompiled strings in their
5657 appropriate places. */
5659 static void
5660 write_output ()
5662 STRINGDEF *next_string;
5663 U_CHAR *cur_buf_loc;
5664 int line_directive_len = 80;
5665 char *line_directive = xmalloc (line_directive_len);
5666 int len;
5668 /* In each run through the loop, either cur_buf_loc ==
5669 next_string_loc, in which case we print a series of strings, or
5670 it is less than next_string_loc, in which case we write some of
5671 the buffer. */
5672 cur_buf_loc = outbuf.buf;
5673 next_string = stringlist;
5675 while (cur_buf_loc < outbuf.bufp || next_string) {
5676 if (next_string
5677 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
5678 if (next_string->writeflag) {
5679 len = 4 * strlen ((const char *) next_string->filename) + 32;
5680 while (len > line_directive_len)
5681 line_directive = xrealloc (line_directive,
5682 line_directive_len *= 2);
5683 sprintf (line_directive, "\n# %d ", next_string->lineno);
5684 strcpy (quote_string (line_directive + strlen (line_directive),
5685 (const char *) next_string->filename,
5686 strlen ((const char *) next_string->filename)),
5687 "\n");
5688 safe_write (fileno (stdout), line_directive, strlen (line_directive));
5689 safe_write (fileno (stdout),
5690 (char *) next_string->contents, next_string->len);
5692 next_string = next_string->chain;
5694 else {
5695 len = (next_string
5696 ? (next_string->output_mark
5697 - (cur_buf_loc - outbuf.buf))
5698 : outbuf.bufp - cur_buf_loc);
5700 safe_write (fileno (stdout), (char *) cur_buf_loc, len);
5701 cur_buf_loc += len;
5704 free (line_directive);
5707 /* Pass a directive through to the output file.
5708 BUF points to the contents of the directive, as a contiguous string.
5709 LIMIT points to the first character past the end of the directive.
5710 KEYWORD is the keyword-table entry for the directive. */
5712 static void
5713 pass_thru_directive (buf, limit, op, keyword)
5714 const U_CHAR *buf;
5715 const U_CHAR *limit;
5716 FILE_BUF *op;
5717 struct directive *keyword;
5719 register int keyword_length = keyword->length;
5721 check_expand (op, 1 + keyword_length + (limit - buf));
5722 *op->bufp++ = '#';
5723 bcopy (keyword->name, (char *) op->bufp, keyword_length);
5724 op->bufp += keyword_length;
5725 if (limit != buf && buf[0] != ' ')
5726 *op->bufp++ = ' ';
5727 bcopy ((const PTR) buf, (PTR) op->bufp, limit - buf);
5728 op->bufp += (limit - buf);
5729 #if 0
5730 *op->bufp++ = '\n';
5731 /* Count the line we have just made in the output,
5732 to get in sync properly. */
5733 op->lineno++;
5734 #endif
5737 /* The arglist structure is built by do_define to tell
5738 collect_definition where the argument names begin. That
5739 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5740 would contain pointers to the strings x, y, and z.
5741 Collect_definition would then build a DEFINITION node,
5742 with reflist nodes pointing to the places x, y, and z had
5743 appeared. So the arglist is just convenience data passed
5744 between these two routines. It is not kept around after
5745 the current #define has been processed and entered into the
5746 hash table. */
5748 struct arglist {
5749 struct arglist *next;
5750 const U_CHAR *name;
5751 int length;
5752 int argno;
5753 char rest_args;
5756 /* Create a DEFINITION node from a #define directive. Arguments are
5757 as for do_define. */
5759 static MACRODEF
5760 create_definition (buf, limit, op)
5761 const U_CHAR *buf, *limit;
5762 FILE_BUF *op;
5764 const U_CHAR *bp; /* temp ptr into input buffer */
5765 const U_CHAR *symname; /* remember where symbol name starts */
5766 int sym_length; /* and how long it is */
5767 int line = instack[indepth].lineno;
5768 const char *file = instack[indepth].nominal_fname;
5769 size_t file_len = instack[indepth].nominal_fname_len;
5770 int rest_args = 0;
5772 DEFINITION *defn;
5773 int arglengths = 0; /* Accumulate lengths of arg names
5774 plus number of args. */
5775 MACRODEF mdef;
5777 bp = buf;
5779 while (is_hor_space[*bp])
5780 bp++;
5782 symname = bp; /* remember where it starts */
5783 sym_length = check_macro_name (bp, 0);
5784 bp += sym_length;
5786 /* Lossage will occur if identifiers or control keywords are broken
5787 across lines using backslash. This is not the right place to take
5788 care of that. */
5790 if (*bp == '(') {
5791 struct arglist *arg_ptrs = NULL;
5792 int argno = 0;
5794 bp++; /* skip '(' */
5795 SKIP_WHITE_SPACE (bp);
5797 /* Loop over macro argument names. */
5798 while (*bp != ')') {
5799 struct arglist *temp;
5801 temp = (struct arglist *) alloca (sizeof (struct arglist));
5802 temp->name = bp;
5803 temp->next = arg_ptrs;
5804 temp->argno = argno++;
5805 temp->rest_args = 0;
5806 arg_ptrs = temp;
5808 if (rest_args)
5809 pedwarn ("another parameter follows `%s'",
5810 rest_extension);
5812 if (!is_idstart[*bp])
5814 if (c9x && limit - bp > (long) REST_EXTENSION_LENGTH
5815 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
5817 /* This is the ISO C 9x way to write macros with variable
5818 number of arguments. */
5819 rest_args = 1;
5820 temp->rest_args = 1;
5822 else
5823 pedwarn ("invalid character in macro parameter name");
5826 /* Find the end of the arg name. */
5827 while (is_idchar[*bp]) {
5828 bp++;
5829 /* do we have a "special" rest-args extension here? */
5830 if (limit - bp > (long) REST_EXTENSION_LENGTH
5831 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
5832 if (pedantic && !instack[indepth].system_header_p)
5833 pedwarn ("ANSI C does not allow macro with variable arguments");
5834 rest_args = 1;
5835 temp->rest_args = 1;
5836 break;
5839 if (bp == temp->name && rest_args == 1)
5841 /* This is the ISO C 9x style. */
5842 temp->name = (U_CHAR *) va_args_name;
5843 temp->length = VA_ARGS_NAME_LENGTH;
5845 else
5846 temp->length = bp - temp->name;
5847 if (rest_args == 1)
5848 bp += REST_EXTENSION_LENGTH;
5849 arglengths += temp->length + 2;
5850 SKIP_WHITE_SPACE (bp);
5851 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
5852 error ("badly punctuated parameter list in `#define'");
5853 goto nope;
5855 if (*bp == ',') {
5856 bp++;
5857 SKIP_WHITE_SPACE (bp);
5858 /* A comma at this point can only be followed by an identifier. */
5859 if (!is_idstart[*bp]
5860 && !(c9x && limit - bp > (long) REST_EXTENSION_LENGTH
5861 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)) {
5862 error ("badly punctuated parameter list in `#define'");
5863 goto nope;
5866 if (bp >= limit) {
5867 error ("unterminated parameter list in `#define'");
5868 goto nope;
5871 struct arglist *otemp;
5873 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
5874 if (temp->length == otemp->length
5875 && bcmp (temp->name, otemp->name, temp->length) == 0)
5877 error ("duplicate argument name `%.*s' in `#define'",
5878 temp->length, temp->name);
5879 goto nope;
5881 if (rest_args == 0 && temp->length == VA_ARGS_NAME_LENGTH
5882 && bcmp (temp->name, va_args_name, VA_ARGS_NAME_LENGTH) == 0)
5884 error ("\
5885 reserved name `%s' used as argument name in `#define'", va_args_name);
5886 goto nope;
5891 ++bp; /* skip paren */
5892 SKIP_WHITE_SPACE (bp);
5893 /* now everything from bp before limit is the definition. */
5894 defn = collect_expansion (bp, limit, argno, arg_ptrs);
5895 defn->rest_args = rest_args;
5897 /* Now set defn->args.argnames to the result of concatenating
5898 the argument names in reverse order
5899 with comma-space between them. */
5900 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
5902 struct arglist *temp;
5903 int i = 0;
5904 for (temp = arg_ptrs; temp; temp = temp->next) {
5905 bcopy (temp->name, &defn->args.argnames[i], temp->length);
5906 i += temp->length;
5907 if (temp->next != 0) {
5908 defn->args.argnames[i++] = ',';
5909 defn->args.argnames[i++] = ' ';
5912 defn->args.argnames[i] = 0;
5914 } else {
5915 /* Simple expansion or empty definition. */
5917 if (bp < limit)
5919 if (is_hor_space[*bp]) {
5920 bp++;
5921 SKIP_WHITE_SPACE (bp);
5922 } else if (sym_length) {
5923 switch (*bp) {
5924 case '!': case '"': case '#': case '%': case '&': case '\'':
5925 case ')': case '*': case '+': case ',': case '-': case '.':
5926 case '/': case ':': case ';': case '<': case '=': case '>':
5927 case '?': case '[': case '\\': case ']': case '^': case '{':
5928 case '|': case '}': case '~':
5929 warning ("missing white space after `#define %.*s'",
5930 sym_length, symname);
5931 break;
5933 default:
5934 pedwarn ("missing white space after `#define %.*s'",
5935 sym_length, symname);
5936 break;
5940 /* Now everything from bp before limit is the definition. */
5941 defn = collect_expansion (bp, limit, -1, NULL_PTR);
5942 defn->args.argnames = (U_CHAR *) "";
5945 defn->line = line;
5946 defn->file = file;
5947 defn->file_len = file_len;
5949 /* OP is null if this is a predefinition */
5950 defn->predefined = !op;
5951 mdef.defn = defn;
5952 mdef.symnam = symname;
5953 mdef.symlen = sym_length;
5955 return mdef;
5957 nope:
5958 mdef.defn = 0;
5959 return mdef;
5962 /* Process a #define directive.
5963 BUF points to the contents of the #define directive, as a contiguous string.
5964 LIMIT points to the first character past the end of the definition.
5965 KEYWORD is the keyword-table entry for #define. */
5967 static int
5968 do_define (buf, limit, op, keyword)
5969 U_CHAR *buf, *limit;
5970 FILE_BUF *op;
5971 struct directive *keyword;
5973 int hashcode;
5974 MACRODEF mdef;
5975 enum node_type newtype = keyword->type == T_DEFINE ? T_MACRO : T_POISON;
5977 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
5978 if (pcp_outfile && op)
5979 pass_thru_directive (buf, limit, op, keyword);
5981 mdef = create_definition (buf, limit, op);
5982 if (mdef.defn == 0)
5983 goto nope;
5985 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
5988 HASHNODE *hp;
5989 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
5990 int ok = 0;
5991 /* Redefining a precompiled key is ok. */
5992 if (hp->type == T_PCSTRING)
5993 ok = 1;
5994 /* Redefining a poisoned identifier is even worse than `not ok'. */
5995 else if (hp->type == T_POISON)
5996 ok = -1;
5997 /* Poisoning anything else is not ok.
5998 The poison should always come first. */
5999 else if (newtype == T_POISON)
6000 ok = 0;
6001 /* Redefining a macro is ok if the definitions are the same. */
6002 else if (hp->type == T_MACRO)
6003 ok = ! compare_defs (mdef.defn, hp->value.defn);
6004 /* Redefining a constant is ok with -D. */
6005 else if (hp->type == T_CONST)
6006 ok = ! done_initializing;
6008 /* Print the warning or error if it's not ok. */
6009 if (ok <= 0)
6011 /* If we are passing through #define and #undef directives, do
6012 that for this re-definition now. */
6013 if (debug_output && op)
6014 pass_thru_directive (buf, limit, op, keyword);
6016 if (hp->type == T_POISON)
6017 error ("redefining poisoned `%.*s'", mdef.symlen, mdef.symnam);
6018 else
6019 pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
6020 if (hp->type == T_MACRO)
6021 pedwarn_with_file_and_line (hp->value.defn->file,
6022 hp->value.defn->file_len,
6023 hp->value.defn->line,
6024 "this is the location of the previous definition");
6026 if (hp->type != T_POISON)
6028 /* Replace the old definition. */
6029 hp->type = newtype;
6030 hp->value.defn = mdef.defn;
6032 } else {
6033 /* If we are passing through #define and #undef directives, do
6034 that for this new definition now. */
6035 if (debug_output && op)
6036 pass_thru_directive (buf, limit, op, keyword);
6037 install (mdef.symnam, mdef.symlen, newtype,
6038 (char *) mdef.defn, hashcode);
6042 return 0;
6044 nope:
6046 return 1;
6049 /* Check a purported macro name SYMNAME, and yield its length.
6050 ASSERTION is nonzero if this is really for an assertion name. */
6052 static int
6053 check_macro_name (symname, assertion)
6054 const U_CHAR *symname;
6055 int assertion;
6057 const U_CHAR *p;
6058 int sym_length;
6060 for (p = symname; is_idchar[*p]; p++)
6062 sym_length = p - symname;
6063 if (sym_length == 0
6064 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
6065 error (assertion ? "invalid assertion name" : "invalid macro name");
6066 else if (!is_idstart[*symname]
6067 || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
6068 error ((assertion
6069 ? "invalid assertion name `%.*s'"
6070 : "invalid macro name `%.*s'"),
6071 sym_length, symname);
6072 return sym_length;
6075 /* Return zero if two DEFINITIONs are isomorphic. */
6077 static int
6078 compare_defs (d1, d2)
6079 DEFINITION *d1, *d2;
6081 register struct reflist *a1, *a2;
6082 register U_CHAR *p1 = d1->expansion;
6083 register U_CHAR *p2 = d2->expansion;
6084 int first = 1;
6086 if (d1->nargs != d2->nargs)
6087 return 1;
6088 if (pedantic
6089 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
6090 return 1;
6091 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
6092 a1 = a1->next, a2 = a2->next) {
6093 if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
6094 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
6095 || a1->argno != a2->argno
6096 || a1->stringify != a2->stringify
6097 || a1->raw_before != a2->raw_before
6098 || a1->raw_after != a2->raw_after)
6099 return 1;
6100 first = 0;
6101 p1 += a1->nchars;
6102 p2 += a2->nchars;
6104 if (a1 != a2)
6105 return 1;
6106 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
6107 p2, d2->length - (p2 - d2->expansion), 1))
6108 return 1;
6109 return 0;
6112 /* Return 1 if two parts of two macro definitions are effectively different.
6113 One of the parts starts at BEG1 and has LEN1 chars;
6114 the other has LEN2 chars at BEG2.
6115 Any sequence of whitespace matches any other sequence of whitespace.
6116 FIRST means these parts are the first of a macro definition;
6117 so ignore leading whitespace entirely.
6118 LAST means these parts are the last of a macro definition;
6119 so ignore trailing whitespace entirely. */
6121 static int
6122 comp_def_part (first, beg1, len1, beg2, len2, last)
6123 int first;
6124 const U_CHAR *beg1, *beg2;
6125 int len1, len2;
6126 int last;
6128 register const U_CHAR *end1 = beg1 + len1;
6129 register const U_CHAR *end2 = beg2 + len2;
6130 if (first) {
6131 while (beg1 != end1 && is_space[*beg1]) beg1++;
6132 while (beg2 != end2 && is_space[*beg2]) beg2++;
6134 if (last) {
6135 while (beg1 != end1 && is_space[end1[-1]]) end1--;
6136 while (beg2 != end2 && is_space[end2[-1]]) end2--;
6138 while (beg1 != end1 && beg2 != end2) {
6139 if (is_space[*beg1] && is_space[*beg2]) {
6140 while (beg1 != end1 && is_space[*beg1]) beg1++;
6141 while (beg2 != end2 && is_space[*beg2]) beg2++;
6142 } else if (*beg1 == *beg2) {
6143 beg1++; beg2++;
6144 } else break;
6146 return (beg1 != end1) || (beg2 != end2);
6149 /* Read a replacement list for a macro with parameters.
6150 Build the DEFINITION structure.
6151 Reads characters of text starting at BUF until END.
6152 ARGLIST specifies the formal parameters to look for
6153 in the text of the definition; NARGS is the number of args
6154 in that list, or -1 for a macro name that wants no argument list.
6155 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6156 and NAMELEN is its length in characters.
6158 Note that comments, backslash-newlines, and leading white space
6159 have already been deleted from the argument. */
6161 /* If there is no trailing whitespace, a Newline Space is added at the end
6162 to prevent concatenation that would be contrary to the standard. */
6164 static DEFINITION *
6165 collect_expansion (buf, end, nargs, arglist)
6166 const U_CHAR *buf;
6167 const U_CHAR *end;
6168 int nargs;
6169 struct arglist *arglist;
6171 DEFINITION *defn;
6172 register const U_CHAR *p;
6173 register const U_CHAR *limit;
6174 register U_CHAR *lastp, *exp_p;
6175 struct reflist *endpat = NULL;
6176 /* Pointer to first nonspace after last ## seen. */
6177 const U_CHAR *concat = 0;
6178 /* Pointer to first nonspace after last single-# seen. */
6179 const U_CHAR *stringify = 0;
6180 /* How those tokens were spelled. */
6181 enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
6182 enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
6183 int maxsize;
6184 int expected_delimiter = '\0';
6186 /* Scan thru the replacement list, ignoring comments and quoted
6187 strings, picking up on the macro calls. It does a linear search
6188 thru the arg list on every potential symbol. Profiling might say
6189 that something smarter should happen. */
6191 if (end < buf)
6192 abort ();
6194 /* Find the beginning of the trailing whitespace. */
6195 limit = end;
6196 p = buf;
6197 while (p < limit && is_space[limit[-1]]) limit--;
6199 /* Allocate space for the text in the macro definition.
6200 Each input char may or may not need 1 byte,
6201 so this is an upper bound.
6202 The extra 3 are for invented trailing newline-marker and final null. */
6203 maxsize = (sizeof (DEFINITION)
6204 + (limit - p) + 3);
6205 defn = (DEFINITION *) xcalloc (1, maxsize);
6207 defn->nargs = nargs;
6208 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
6209 lastp = exp_p;
6211 if (p[0] == '#'
6212 ? p[1] == '#'
6213 : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
6214 error ("`##' at start of macro definition");
6215 p += p[0] == '#' ? 2 : 4;
6218 /* Process the main body of the definition. */
6219 while (p < limit) {
6220 int skipped_arg = 0;
6221 register U_CHAR c = *p++;
6223 *exp_p++ = c;
6225 if (!traditional) {
6226 switch (c) {
6227 case '\'':
6228 case '\"':
6229 if (expected_delimiter != '\0') {
6230 if (c == expected_delimiter)
6231 expected_delimiter = '\0';
6232 } else
6233 expected_delimiter = c;
6234 break;
6236 case '\\':
6237 if (expected_delimiter) {
6238 /* In a string, backslash goes through
6239 and makes next char ordinary. */
6240 *exp_p++ = *p++;
6242 break;
6244 case '%':
6245 if (!expected_delimiter && *p == ':') {
6246 /* %: is not a digraph if preceded by an odd number of '<'s. */
6247 const U_CHAR *p0 = p - 1;
6248 while (buf < p0 && p0[-1] == '<')
6249 p0--;
6250 if ((p - p0) & 1) {
6251 /* Treat %:%: as ## and %: as #. */
6252 if (p[1] == '%' && p[2] == ':') {
6253 p += 2;
6254 goto sharp_sharp_token;
6256 if (nargs >= 0) {
6257 p++;
6258 goto sharp_token;
6262 break;
6264 case '#':
6265 /* # is ordinary inside a string. */
6266 if (expected_delimiter)
6267 break;
6268 if (*p == '#') {
6269 sharp_sharp_token:
6270 /* ##: concatenate preceding and following tokens. */
6271 /* Take out the first #, discard preceding whitespace. */
6272 exp_p--;
6273 while (exp_p > lastp && is_hor_space[exp_p[-1]])
6274 --exp_p;
6275 /* Skip the second #. */
6276 p++;
6277 concat_sharp_token_type = c;
6278 if (is_hor_space[*p]) {
6279 concat_sharp_token_type = c + 1;
6280 p++;
6281 SKIP_WHITE_SPACE (p);
6283 concat = p;
6284 if (p == limit)
6285 error ("`##' at end of macro definition");
6286 } else if (nargs >= 0) {
6287 /* Single #: stringify following argument ref.
6288 Don't leave the # in the expansion. */
6289 sharp_token:
6290 exp_p--;
6291 stringify_sharp_token_type = c;
6292 if (is_hor_space[*p]) {
6293 stringify_sharp_token_type = c + 1;
6294 p++;
6295 SKIP_WHITE_SPACE (p);
6297 if (! is_idstart[*p] || nargs == 0
6298 || (*p == 'L' && (p[1] == '\'' || p[1] == '"')))
6299 error ("`#' operator is not followed by a macro argument name");
6300 else
6301 stringify = p;
6303 break;
6305 } else {
6306 /* In -traditional mode, recognize arguments inside strings and
6307 character constants, and ignore special properties of #.
6308 Arguments inside strings are considered "stringified", but no
6309 extra quote marks are supplied. */
6310 switch (c) {
6311 case '\'':
6312 case '\"':
6313 if (expected_delimiter != '\0') {
6314 if (c == expected_delimiter)
6315 expected_delimiter = '\0';
6316 } else
6317 expected_delimiter = c;
6318 break;
6320 case '\\':
6321 /* Backslash quotes delimiters and itself, but not macro args. */
6322 if (expected_delimiter != 0 && p < limit
6323 && (*p == expected_delimiter || *p == '\\')) {
6324 *exp_p++ = *p++;
6325 continue;
6327 break;
6329 case '/':
6330 if (expected_delimiter != '\0') /* No comments inside strings. */
6331 break;
6332 if (*p == '*') {
6333 /* If we find a comment that wasn't removed by handle_directive,
6334 this must be -traditional. So replace the comment with
6335 nothing at all. */
6336 exp_p--;
6337 while (++p < limit) {
6338 if (p[0] == '*' && p[1] == '/') {
6339 p += 2;
6340 break;
6343 #if 0
6344 /* Mark this as a concatenation-point, as if it had been ##. */
6345 concat = p;
6346 #endif
6348 break;
6352 #ifdef MULTIBYTE_CHARS
6353 /* Handle multibyte characters inside string and character literals. */
6354 if (expected_delimiter != '\0')
6356 int length;
6357 --p;
6358 length = local_mblen (p, limit - p);
6359 if (length > 1)
6361 --exp_p;
6362 bcopy (p, exp_p, length);
6363 p += length;
6364 exp_p += length;
6365 continue;
6367 ++p;
6369 #endif
6371 /* Handle the start of a symbol. */
6372 if (is_idchar[c] && nargs > 0) {
6373 const U_CHAR *id_beg = p - 1;
6374 int id_len;
6376 --exp_p;
6377 while (p != limit && is_idchar[*p]) p++;
6378 id_len = p - id_beg;
6380 if (is_idstart[c]
6381 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
6382 register struct arglist *arg;
6384 for (arg = arglist; arg != NULL; arg = arg->next) {
6385 struct reflist *tpat;
6387 if (arg->name[0] == c
6388 && arg->length == id_len
6389 && bcmp (arg->name, id_beg, id_len) == 0) {
6390 enum sharp_token_type tpat_stringify;
6391 if (expected_delimiter) {
6392 if (warn_stringify) {
6393 if (traditional) {
6394 warning ("macro argument `%.*s' is stringified.",
6395 id_len, arg->name);
6396 } else {
6397 warning ("macro arg `%.*s' would be stringified with -traditional.",
6398 id_len, arg->name);
6401 /* If ANSI, don't actually substitute inside a string. */
6402 if (!traditional)
6403 break;
6404 tpat_stringify = SHARP_TOKEN;
6405 } else {
6406 tpat_stringify
6407 = (stringify == id_beg
6408 ? stringify_sharp_token_type : NO_SHARP_TOKEN);
6410 /* make a pat node for this arg and append it to the end of
6411 the pat list */
6412 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
6413 tpat->next = NULL;
6414 tpat->raw_before
6415 = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
6416 tpat->raw_after = NO_SHARP_TOKEN;
6417 tpat->rest_args = arg->rest_args;
6418 tpat->stringify = tpat_stringify;
6420 if (endpat == NULL)
6421 defn->pattern = tpat;
6422 else
6423 endpat->next = tpat;
6424 endpat = tpat;
6426 tpat->argno = arg->argno;
6427 tpat->nchars = exp_p - lastp;
6429 register const U_CHAR *p1 = p;
6430 SKIP_WHITE_SPACE (p1);
6431 if (p1[0]=='#'
6432 ? p1[1]=='#'
6433 : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
6434 tpat->raw_after = p1[0] + (p != p1);
6436 lastp = exp_p; /* place to start copying from next time */
6437 skipped_arg = 1;
6438 break;
6443 /* If this was not a macro arg, copy it into the expansion. */
6444 if (! skipped_arg) {
6445 register const U_CHAR *lim1 = p;
6446 p = id_beg;
6447 while (p != lim1)
6448 *exp_p++ = *p++;
6449 if (stringify == id_beg)
6450 error ("`#' operator should be followed by a macro argument name");
6455 if (!traditional && expected_delimiter == 0) {
6456 /* If ANSI, put in a newline-space marker to prevent token pasting.
6457 But not if "inside a string" (which in ANSI mode happens only for
6458 -D option). */
6459 *exp_p++ = '\n';
6460 *exp_p++ = ' ';
6463 *exp_p = '\0';
6465 defn->length = exp_p - defn->expansion;
6467 /* Crash now if we overrun the allocated size. */
6468 if (defn->length + 1 > maxsize)
6469 abort ();
6471 #if 0
6472 /* This isn't worth the time it takes. */
6473 /* give back excess storage */
6474 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
6475 #endif
6477 return defn;
6480 static int
6481 do_assert (buf, limit, op, keyword)
6482 U_CHAR *buf, *limit;
6483 FILE_BUF *op ATTRIBUTE_UNUSED;
6484 struct directive *keyword ATTRIBUTE_UNUSED;
6486 const U_CHAR *bp; /* temp ptr into input buffer */
6487 const U_CHAR *symname; /* remember where symbol name starts */
6488 int sym_length; /* and how long it is */
6489 struct arglist *tokens = NULL;
6491 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6492 pedwarn ("ANSI C does not allow `#assert'");
6494 bp = buf;
6496 while (is_hor_space[*bp])
6497 bp++;
6499 symname = bp; /* remember where it starts */
6500 sym_length = check_macro_name (bp, 1);
6501 bp += sym_length;
6502 /* #define doesn't do this, but we should. */
6503 SKIP_WHITE_SPACE (bp);
6505 /* Lossage will occur if identifiers or control tokens are broken
6506 across lines using backslash. This is not the right place to take
6507 care of that. */
6509 if (*bp != '(') {
6510 error ("missing token-sequence in `#assert'");
6511 return 1;
6515 int error_flag = 0;
6517 bp++; /* skip '(' */
6518 SKIP_WHITE_SPACE (bp);
6520 tokens = read_token_list (&bp, limit, &error_flag);
6521 if (error_flag)
6522 return 1;
6523 if (tokens == 0) {
6524 error ("empty token-sequence in `#assert'");
6525 return 1;
6528 ++bp; /* skip paren */
6529 SKIP_WHITE_SPACE (bp);
6532 /* If this name isn't already an assertion name, make it one.
6533 Error if it was already in use in some other way. */
6536 ASSERTION_HASHNODE *hp;
6537 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6538 struct tokenlist_list *value
6539 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6541 hp = assertion_lookup (symname, sym_length, hashcode);
6542 if (hp == NULL) {
6543 if (sym_length == 7 && ! bcmp (symname, "defined", 7))
6544 error ("`defined' redefined as assertion");
6545 hp = assertion_install (symname, sym_length, hashcode);
6548 /* Add the spec'd token-sequence to the list of such. */
6549 value->tokens = tokens;
6550 value->next = hp->value;
6551 hp->value = value;
6554 return 0;
6557 static int
6558 do_unassert (buf, limit, op, keyword)
6559 U_CHAR *buf, *limit;
6560 FILE_BUF *op ATTRIBUTE_UNUSED;
6561 struct directive *keyword ATTRIBUTE_UNUSED;
6563 const U_CHAR *bp; /* temp ptr into input buffer */
6564 const U_CHAR *symname; /* remember where symbol name starts */
6565 int sym_length; /* and how long it is */
6567 struct arglist *tokens = NULL;
6568 int tokens_specified = 0;
6570 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6571 pedwarn ("ANSI C does not allow `#unassert'");
6573 bp = buf;
6575 while (is_hor_space[*bp])
6576 bp++;
6578 symname = bp; /* remember where it starts */
6579 sym_length = check_macro_name (bp, 1);
6580 bp += sym_length;
6581 /* #define doesn't do this, but we should. */
6582 SKIP_WHITE_SPACE (bp);
6584 /* Lossage will occur if identifiers or control tokens are broken
6585 across lines using backslash. This is not the right place to take
6586 care of that. */
6588 if (*bp == '(') {
6589 int error_flag = 0;
6591 bp++; /* skip '(' */
6592 SKIP_WHITE_SPACE (bp);
6594 tokens = read_token_list (&bp, limit, &error_flag);
6595 if (error_flag)
6596 return 1;
6597 if (tokens == 0) {
6598 error ("empty token list in `#unassert'");
6599 return 1;
6602 tokens_specified = 1;
6604 ++bp; /* skip paren */
6605 SKIP_WHITE_SPACE (bp);
6609 ASSERTION_HASHNODE *hp;
6610 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6611 struct tokenlist_list *tail, *prev;
6613 hp = assertion_lookup (symname, sym_length, hashcode);
6614 if (hp == NULL)
6615 return 1;
6617 /* If no token list was specified, then eliminate this assertion
6618 entirely. */
6619 if (! tokens_specified) {
6620 struct tokenlist_list *next;
6621 for (tail = hp->value; tail; tail = next) {
6622 next = tail->next;
6623 free_token_list (tail->tokens);
6624 free (tail);
6626 delete_assertion (hp);
6627 } else {
6628 /* If a list of tokens was given, then delete any matching list. */
6630 tail = hp->value;
6631 prev = 0;
6632 while (tail) {
6633 struct tokenlist_list *next = tail->next;
6634 if (compare_token_lists (tail->tokens, tokens)) {
6635 if (prev)
6636 prev->next = next;
6637 else
6638 hp->value = tail->next;
6639 free_token_list (tail->tokens);
6640 free (tail);
6641 } else {
6642 prev = tail;
6644 tail = next;
6649 return 0;
6652 /* Test whether there is an assertion named NAME
6653 and optionally whether it has an asserted token list TOKENS.
6654 NAME is not null terminated; its length is SYM_LENGTH.
6655 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6658 check_assertion (name, sym_length, tokens_specified, tokens)
6659 const U_CHAR *name;
6660 int sym_length;
6661 int tokens_specified;
6662 struct arglist *tokens;
6664 ASSERTION_HASHNODE *hp;
6665 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6667 if (pedantic && !instack[indepth].system_header_p)
6668 pedwarn ("ANSI C does not allow testing assertions");
6670 hp = assertion_lookup (name, sym_length, hashcode);
6671 if (hp == NULL)
6672 /* It is not an assertion; just return false. */
6673 return 0;
6675 /* If no token list was specified, then value is 1. */
6676 if (! tokens_specified)
6677 return 1;
6680 struct tokenlist_list *tail;
6682 tail = hp->value;
6684 /* If a list of tokens was given,
6685 then succeed if the assertion records a matching list. */
6687 while (tail) {
6688 if (compare_token_lists (tail->tokens, tokens))
6689 return 1;
6690 tail = tail->next;
6693 /* Fail if the assertion has no matching list. */
6694 return 0;
6698 /* Compare two lists of tokens for equality including order of tokens. */
6700 static int
6701 compare_token_lists (l1, l2)
6702 struct arglist *l1, *l2;
6704 while (l1 && l2) {
6705 if (l1->length != l2->length)
6706 return 0;
6707 if (bcmp (l1->name, l2->name, l1->length))
6708 return 0;
6709 l1 = l1->next;
6710 l2 = l2->next;
6713 /* Succeed if both lists end at the same time. */
6714 return l1 == l2;
6717 /* Read a space-separated list of tokens ending in a close parenthesis.
6718 Return a list of strings, in the order they were written.
6719 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6720 Parse the text starting at *BPP, and update *BPP.
6721 Don't parse beyond LIMIT. */
6723 static struct arglist *
6724 read_token_list (bpp, limit, error_flag)
6725 const U_CHAR **bpp;
6726 const U_CHAR *limit;
6727 int *error_flag;
6729 struct arglist *token_ptrs = 0;
6730 const U_CHAR *bp = *bpp;
6731 int depth = 1;
6733 *error_flag = 0;
6735 /* Loop over the assertion value tokens. */
6736 while (depth > 0) {
6737 struct arglist *temp;
6738 U_CHAR *temp2;
6739 int eofp = 0;
6740 const U_CHAR *beg = bp;
6742 /* Find the end of the token. */
6743 if (*bp == '(') {
6744 bp++;
6745 depth++;
6746 } else if (*bp == ')') {
6747 depth--;
6748 if (depth == 0)
6749 break;
6750 bp++;
6751 } else if (*bp == '"' || *bp == '\'')
6752 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6753 else
6754 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
6755 && *bp != '"' && *bp != '\'' && bp != limit)
6756 bp++;
6758 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
6759 temp2 = (U_CHAR *) xmalloc (bp - beg + 1);
6760 bcopy ((const PTR) beg, (PTR) temp2, bp - beg);
6761 temp2[bp - beg] = 0;
6762 temp->name = temp2;
6763 temp->next = token_ptrs;
6764 token_ptrs = temp;
6765 temp->length = bp - beg;
6767 SKIP_WHITE_SPACE (bp);
6769 if (bp >= limit) {
6770 error ("unterminated token sequence in `#assert' or `#unassert'");
6771 *error_flag = -1;
6772 return 0;
6775 *bpp = bp;
6777 /* We accumulated the names in reverse order.
6778 Now reverse them to get the proper order. */
6780 register struct arglist *prev = 0, *this, *next;
6781 for (this = token_ptrs; this; this = next) {
6782 next = this->next;
6783 this->next = prev;
6784 prev = this;
6786 return prev;
6790 static void
6791 free_token_list (tokens)
6792 struct arglist *tokens;
6794 while (tokens) {
6795 struct arglist *next = tokens->next;
6796 free ((PTR) tokens->name);
6797 free (tokens);
6798 tokens = next;
6802 /* Install a name in the assertion hash table.
6804 If LEN is >= 0, it is the length of the name.
6805 Otherwise, compute the length by scanning the entire name.
6807 If HASH is >= 0, it is the precomputed hash code.
6808 Otherwise, compute the hash code. */
6810 static ASSERTION_HASHNODE *
6811 assertion_install (name, len, hash)
6812 const U_CHAR *name;
6813 int len;
6814 int hash;
6816 register ASSERTION_HASHNODE *hp;
6817 register int i, bucket;
6818 register U_CHAR *p;
6819 register const U_CHAR *q;
6821 i = sizeof (ASSERTION_HASHNODE) + len + 1;
6822 hp = (ASSERTION_HASHNODE *) xmalloc (i);
6823 bucket = hash;
6824 hp->bucket_hdr = &assertion_hashtab[bucket];
6825 hp->next = assertion_hashtab[bucket];
6826 assertion_hashtab[bucket] = hp;
6827 hp->prev = NULL;
6828 if (hp->next != NULL)
6829 hp->next->prev = hp;
6830 hp->length = len;
6831 hp->value = 0;
6832 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
6833 p = hp->name;
6834 q = name;
6835 for (i = 0; i < len; i++)
6836 *p++ = *q++;
6837 hp->name[len] = 0;
6838 return hp;
6841 /* Find the most recent hash node for name "name" (ending with first
6842 non-identifier char) installed by install
6844 If LEN is >= 0, it is the length of the name.
6845 Otherwise, compute the length by scanning the entire name.
6847 If HASH is >= 0, it is the precomputed hash code.
6848 Otherwise, compute the hash code. */
6850 static ASSERTION_HASHNODE *
6851 assertion_lookup (name, len, hash)
6852 const U_CHAR *name;
6853 int len;
6854 int hash;
6856 register ASSERTION_HASHNODE *bucket;
6858 bucket = assertion_hashtab[hash];
6859 while (bucket) {
6860 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
6861 return bucket;
6862 bucket = bucket->next;
6864 return NULL;
6867 static void
6868 delete_assertion (hp)
6869 ASSERTION_HASHNODE *hp;
6872 if (hp->prev != NULL)
6873 hp->prev->next = hp->next;
6874 if (hp->next != NULL)
6875 hp->next->prev = hp->prev;
6877 /* Make sure that the bucket chain header that the deleted guy was
6878 on points to the right thing afterwards. */
6879 if (hp == *hp->bucket_hdr)
6880 *hp->bucket_hdr = hp->next;
6882 free (hp);
6886 * interpret #line directive. Remembers previously seen fnames
6887 * in its very own hash table.
6889 #define FNAME_HASHSIZE 37
6891 static int
6892 do_line (buf, limit, op, keyword)
6893 U_CHAR *buf, *limit;
6894 FILE_BUF *op;
6895 struct directive *keyword ATTRIBUTE_UNUSED;
6897 register U_CHAR *bp;
6898 FILE_BUF *ip = &instack[indepth];
6899 FILE_BUF tem;
6900 int new_lineno;
6901 enum file_change_code file_change = same_file;
6903 /* Expand any macros. */
6904 tem = expand_to_temp_buffer (buf, limit, 0, 0);
6906 /* Point to macroexpanded line, which is null-terminated now. */
6907 bp = tem.buf;
6908 limit = tem.bufp;
6909 SKIP_WHITE_SPACE (bp);
6911 if (!ISDIGIT (*bp)) {
6912 error ("invalid format `#line' directive");
6913 return 0;
6916 /* The Newline at the end of this line remains to be processed.
6917 To put the next line at the specified line number,
6918 we must store a line number now that is one less. */
6919 new_lineno = atoi ((char *) bp) - 1;
6921 /* NEW_LINENO is one less than the actual line number here. */
6922 if (pedantic && new_lineno < 0)
6923 pedwarn ("line number out of range in `#line' directive");
6925 /* skip over the line number. */
6926 while (ISDIGIT (*bp))
6927 bp++;
6929 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6930 if (*bp && !is_space[*bp]) {
6931 error ("invalid format `#line' directive");
6932 return;
6934 #endif
6936 SKIP_WHITE_SPACE (bp);
6938 if (*bp == '\"') {
6939 static HASHNODE *fname_table[FNAME_HASHSIZE];
6940 HASHNODE *hp, **hash_bucket;
6941 U_CHAR *fname, *p;
6942 int fname_length;
6944 fname = ++bp;
6946 /* Turn the file name, which is a character string literal,
6947 into a null-terminated string. Do this in place. */
6948 p = bp;
6949 for (;;)
6950 switch ((*p++ = *bp++)) {
6951 case '\\':
6952 if (! ignore_escape_flag)
6954 char *bpc = (char *) bp;
6955 HOST_WIDEST_INT c = parse_escape (&bpc, (HOST_WIDEST_INT) (U_CHAR) (-1));
6956 bp = (U_CHAR *) bpc;
6957 if (c < 0)
6958 p--;
6959 else
6960 p[-1] = c;
6962 break;
6964 case '\"':
6965 *--p = 0;
6966 goto fname_done;
6968 fname_done:
6969 fname_length = p - fname;
6971 SKIP_WHITE_SPACE (bp);
6972 if (*bp) {
6973 if (pedantic)
6974 pedwarn ("garbage at end of `#line' directive");
6975 if (*bp == '1')
6976 file_change = enter_file;
6977 else if (*bp == '2')
6978 file_change = leave_file;
6979 else if (*bp == '3')
6980 ip->system_header_p = 1;
6981 else if (*bp == '4')
6982 ip->system_header_p = 2;
6983 else {
6984 error ("invalid format `#line' directive");
6985 return 0;
6988 bp++;
6989 SKIP_WHITE_SPACE (bp);
6990 if (*bp == '3') {
6991 ip->system_header_p = 1;
6992 bp++;
6993 SKIP_WHITE_SPACE (bp);
6995 if (*bp == '4') {
6996 ip->system_header_p = 2;
6997 bp++;
6998 SKIP_WHITE_SPACE (bp);
7000 if (*bp) {
7001 error ("invalid format `#line' directive");
7002 return 0;
7006 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
7007 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
7008 if (hp->length == fname_length &&
7009 bcmp (hp->value.cpval, fname, fname_length) == 0) {
7010 ip->nominal_fname = hp->value.cpval;
7011 ip->nominal_fname_len = fname_length;
7012 break;
7014 if (hp == 0) {
7015 /* Didn't find it; cons up a new one. */
7016 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
7017 hp->next = *hash_bucket;
7018 *hash_bucket = hp;
7020 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
7021 ip->nominal_fname_len = hp->length = fname_length;
7022 bcopy (fname, ((char *) hp) + sizeof (HASHNODE), fname_length + 1);
7024 } else if (*bp) {
7025 error ("invalid format `#line' directive");
7026 return 0;
7029 ip->lineno = new_lineno;
7030 output_line_directive (ip, op, 0, file_change);
7031 check_expand (op, ip->length - (ip->bufp - ip->buf));
7032 return 0;
7035 /* Remove the definition of a symbol from the symbol table.
7036 according to un*x /lib/cpp, it is not an error to undef
7037 something that has no definitions, so it isn't one here either. */
7039 static int
7040 do_undef (buf, limit, op, keyword)
7041 U_CHAR *buf, *limit;
7042 FILE_BUF *op;
7043 struct directive *keyword;
7045 int sym_length;
7046 HASHNODE *hp;
7047 U_CHAR *orig_buf = buf;
7049 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7050 if (pcp_outfile && op)
7051 pass_thru_directive (buf, limit, op, keyword);
7053 SKIP_WHITE_SPACE (buf);
7054 sym_length = check_macro_name (buf, 0);
7056 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
7057 /* If we are generating additional info for debugging (with -g) we
7058 need to pass through all effective #undef directives. */
7059 if (debug_output && op)
7060 pass_thru_directive (orig_buf, limit, op, keyword);
7061 if (hp->type == T_POISON)
7062 error ("cannot undefine poisoned `%s'", hp->name);
7063 else {
7064 if (hp->type != T_MACRO)
7065 warning ("undefining `%s'", hp->name);
7066 delete_macro (hp);
7070 if (pedantic) {
7071 buf += sym_length;
7072 SKIP_WHITE_SPACE (buf);
7073 if (buf != limit)
7074 pedwarn ("garbage after `#undef' directive");
7076 return 0;
7080 /* Report an error detected by the program we are processing.
7081 Use the text of the line in the error message. */
7083 static int
7084 do_error (buf, limit, op, keyword)
7085 U_CHAR *buf, *limit;
7086 FILE_BUF *op ATTRIBUTE_UNUSED;
7087 struct directive *keyword;
7089 int length = limit - buf;
7090 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
7091 bcopy ((const PTR) buf, (PTR) copy, length);
7092 copy[length] = 0;
7093 SKIP_WHITE_SPACE (copy);
7095 switch (keyword->type) {
7096 case T_ERROR:
7097 error ("#error %s", copy);
7098 break;
7100 case T_WARNING:
7101 if (pedantic && !instack[indepth].system_header_p)
7102 pedwarn ("ANSI C does not allow `#warning'");
7103 warning ("#warning %s", copy);
7104 break;
7106 default:
7107 abort ();
7110 return 0;
7112 /* Remember the name of the current file being read from so that we can
7113 avoid ever including it again. */
7115 static void
7116 do_once ()
7118 int i;
7120 for (i = indepth; i >= 0; i--)
7121 if (instack[i].inc) {
7122 record_control_macro (instack[i].inc, (const U_CHAR *) "");
7123 break;
7127 /* Report program identification. */
7129 static int
7130 do_ident (buf, limit, op, keyword)
7131 U_CHAR *buf, *limit;
7132 FILE_BUF *op;
7133 struct directive *keyword ATTRIBUTE_UNUSED;
7135 FILE_BUF trybuf;
7136 int len;
7138 /* Allow #ident in system headers, since that's not user's fault. */
7139 if (pedantic && !instack[indepth].system_header_p)
7140 pedwarn ("ANSI C does not allow `#ident'");
7142 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
7143 buf = trybuf.buf;
7144 len = trybuf.bufp - buf;
7146 /* Output expanded directive. */
7147 check_expand (op, 7 + len);
7148 bcopy ("#ident ", (char *) op->bufp, 7);
7149 op->bufp += 7;
7150 bcopy ((const PTR) buf, (PTR) op->bufp, len);
7151 op->bufp += len;
7153 free (buf);
7154 return 0;
7157 /* #pragma and its argument line have already been copied to the output file.
7158 Just check for some recognized pragmas that need validation here. */
7160 static int
7161 do_pragma (buf, limit, op, keyword)
7162 U_CHAR *buf, *limit;
7163 FILE_BUF *op;
7164 struct directive *keyword;
7166 SKIP_WHITE_SPACE (buf);
7167 if (!strncmp ((char *) buf, "once", 4)) {
7168 /* Allow #pragma once in system headers, since that's not the user's
7169 fault. */
7170 if (!instack[indepth].system_header_p)
7171 warning ("`#pragma once' is obsolete");
7172 do_once ();
7175 if (!strncmp (buf, "poison", 6)) {
7176 /* Poison these symbols so that all subsequent usage produces an
7177 error message. */
7178 U_CHAR *p = buf + 6;
7180 SKIP_WHITE_SPACE (p);
7181 while (p < limit)
7183 U_CHAR *end = p;
7185 while (end < limit && is_idchar[*end])
7186 end++;
7187 if (end < limit && !is_space[*end])
7189 error ("invalid #pragma poison");
7190 return 0;
7192 do_define(p, end, op, keyword);
7193 p = end;
7194 SKIP_WHITE_SPACE (p);
7198 if (!strncmp ((char *) buf, "implementation", 14)) {
7199 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7200 been included yet. */
7202 int h;
7203 U_CHAR *p = buf + 14, *fname;
7204 SKIP_WHITE_SPACE (p);
7205 if (*p != '\"')
7206 return 0;
7208 fname = p + 1;
7209 p = skip_quoted_string (p, limit, 0, NULL_PTR, NULL_PTR, NULL_PTR);
7210 if (p[-1] == '"')
7211 *--p = '\0';
7213 for (h = 0; h < INCLUDE_HASHSIZE; h++) {
7214 struct include_file *inc;
7215 for (inc = include_hashtab[h]; inc; inc = inc->next) {
7216 if (!strcmp (base_name (inc->fname), (char *) fname)) {
7217 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname);
7218 return 0;
7223 return 0;
7226 #if 0
7227 /* This was a fun hack, but #pragma seems to start to be useful.
7228 By failing to recognize it, we pass it through unchanged to cc1. */
7230 /* The behavior of the #pragma directive is implementation defined.
7231 this implementation defines it as follows. */
7233 static int
7234 do_pragma ()
7236 close (0);
7237 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
7238 goto nope;
7239 close (1);
7240 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
7241 goto nope;
7242 execl ("/usr/games/hack", "#pragma", 0);
7243 execl ("/usr/games/rogue", "#pragma", 0);
7244 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7245 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7246 nope:
7247 fatal ("You are in a maze of twisty compiler features, all different");
7249 #endif
7251 #ifdef SCCS_DIRECTIVE
7253 /* Just ignore #sccs, on systems where we define it at all. */
7255 static int
7256 do_sccs (buf, limit, op, keyword)
7257 U_CHAR *buf ATTRIBUTE_UNUSED;
7258 U_CHAR *limit ATTRIBUTE_UNUSED;
7259 FILE_BUF *op ATTRIBUTE_UNUSED;
7260 struct directive *keyword ATTRIBUTE_UNUSED;
7262 if (pedantic)
7263 pedwarn ("ANSI C does not allow `#sccs'");
7264 return 0;
7267 #endif /* defined (SCCS_DIRECTIVE) */
7269 /* Handle #if directive by
7270 1) inserting special `defined' keyword into the hash table
7271 that gets turned into 0 or 1 by special_symbol (thus,
7272 if the luser has a symbol called `defined' already, it won't
7273 work inside the #if directive)
7274 2) rescan the input into a temporary output buffer
7275 3) pass the output buffer to the yacc parser and collect a value
7276 4) clean up the mess left from steps 1 and 2.
7277 5) call conditional_skip to skip til the next #endif (etc.),
7278 or not, depending on the value from step 3. */
7280 static int
7281 do_if (buf, limit, op, keyword)
7282 U_CHAR *buf, *limit;
7283 FILE_BUF *op;
7284 struct directive *keyword ATTRIBUTE_UNUSED;
7286 HOST_WIDEST_INT value;
7287 FILE_BUF *ip = &instack[indepth];
7289 value = eval_if_expression (buf, limit - buf);
7290 conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
7291 return 0;
7294 /* Handle a #elif directive by not changing if_stack either.
7295 see the comment above do_else. */
7297 static int
7298 do_elif (buf, limit, op, keyword)
7299 U_CHAR *buf, *limit;
7300 FILE_BUF *op;
7301 struct directive *keyword ATTRIBUTE_UNUSED;
7303 HOST_WIDEST_INT value;
7304 FILE_BUF *ip = &instack[indepth];
7306 if (if_stack == instack[indepth].if_stack) {
7307 error ("`#elif' not within a conditional");
7308 return 0;
7309 } else {
7310 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7311 error ("`#elif' after `#else'");
7312 fprintf (stderr, " (matches line %d", if_stack->lineno);
7313 if (! (if_stack->fname_len == ip->nominal_fname_len
7314 && !bcmp (if_stack->fname, ip->nominal_fname,
7315 if_stack->fname_len))) {
7316 fprintf (stderr, ", file ");
7317 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7318 if_stack->fname_len, stderr);
7320 fprintf (stderr, ")\n");
7322 if_stack->type = T_ELIF;
7325 if (if_stack->if_succeeded)
7326 skip_if_group (ip, 0, op);
7327 else {
7328 value = eval_if_expression (buf, limit - buf);
7329 if (value == 0)
7330 skip_if_group (ip, 0, op);
7331 else {
7332 ++if_stack->if_succeeded; /* continue processing input */
7333 output_line_directive (ip, op, 1, same_file);
7336 return 0;
7339 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7340 result as a C expression and return the value as an int. */
7342 static HOST_WIDEST_INT
7343 eval_if_expression (buf, length)
7344 const U_CHAR *buf;
7345 int length;
7347 FILE_BUF temp_obuf;
7348 HASHNODE *save_defined;
7349 HOST_WIDEST_INT value;
7351 save_defined = install ((const U_CHAR *) "defined", -1, T_SPEC_DEFINED,
7352 NULL_PTR, -1);
7353 pcp_inside_if = 1;
7354 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
7355 pcp_inside_if = 0;
7356 delete_macro (save_defined); /* clean up special symbol */
7358 *temp_obuf.bufp = '\n';
7359 value = parse_c_expression ((char *) temp_obuf.buf,
7360 warn_undef && !instack[indepth].system_header_p);
7362 free (temp_obuf.buf);
7364 return value;
7367 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7368 or don't skip to the #endif/#else/#elif depending on what directive
7369 is actually being processed. */
7371 static int
7372 do_xifdef (buf, limit, op, keyword)
7373 U_CHAR *buf, *limit;
7374 FILE_BUF *op;
7375 struct directive *keyword;
7377 int skip;
7378 FILE_BUF *ip = &instack[indepth];
7379 U_CHAR *end;
7380 int start_of_file = 0;
7381 U_CHAR *control_macro = 0;
7383 /* Detect a #ifndef at start of file (not counting comments). */
7384 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
7385 U_CHAR *p = ip->buf;
7386 while (p != directive_start) {
7387 U_CHAR c = *p++;
7388 if (is_space[c])
7390 /* Make no special provision for backslash-newline here; this is
7391 slower if backslash-newlines are present, but it's correct,
7392 and it's not worth it to tune for the rare backslash-newline. */
7393 else if (c == '/'
7394 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7395 /* Skip this comment. */
7396 int junk = 0;
7397 U_CHAR *save_bufp = ip->bufp;
7398 ip->bufp = p + 1;
7399 p = skip_to_end_of_comment (ip, &junk, 1);
7400 ip->bufp = save_bufp;
7401 } else {
7402 goto fail;
7405 /* If we get here, this conditional is the beginning of the file. */
7406 start_of_file = 1;
7407 fail: ;
7410 /* Discard leading and trailing whitespace. */
7411 SKIP_WHITE_SPACE (buf);
7412 while (limit != buf && is_hor_space[limit[-1]]) limit--;
7414 /* Find the end of the identifier at the beginning. */
7415 for (end = buf; is_idchar[*end]; end++);
7417 if (end == buf) {
7418 skip = (keyword->type == T_IFDEF);
7419 if (! traditional)
7420 pedwarn (end == limit ? "`#%s' with no argument"
7421 : "`#%s' argument starts with punctuation",
7422 keyword->name);
7423 } else {
7424 HASHNODE *hp;
7426 if (! traditional) {
7427 if (ISDIGIT (buf[0]))
7428 pedwarn ("`#%s' argument starts with a digit", keyword->name);
7429 else if (end != limit)
7430 pedwarn ("garbage at end of `#%s' argument", keyword->name);
7433 hp = lookup (buf, end-buf, -1);
7435 if (pcp_outfile) {
7436 /* Output a precondition for this macro. */
7437 if (hp
7438 && (hp->type == T_CONST
7439 || (hp->type == T_MACRO && hp->value.defn->predefined)))
7440 fprintf (pcp_outfile, "#define %s\n", hp->name);
7441 else {
7442 U_CHAR *cp = buf;
7443 fprintf (pcp_outfile, "#undef ");
7444 while (is_idchar[*cp]) /* Ick! */
7445 fputc (*cp++, pcp_outfile);
7446 putc ('\n', pcp_outfile);
7450 if ((hp != NULL) && (hp->type == T_POISON)) {
7451 error("attempt to use poisoned `%s'.", hp->name);
7452 hp = NULL;
7454 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
7455 if (start_of_file && !skip) {
7456 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
7457 bcopy ((const PTR) buf, (PTR) control_macro, end - buf);
7458 control_macro[end - buf] = 0;
7462 conditional_skip (ip, skip, T_IF, control_macro, op);
7463 return 0;
7466 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7467 If this is a #ifndef starting at the beginning of a file,
7468 CONTROL_MACRO is the macro name tested by the #ifndef.
7469 Otherwise, CONTROL_MACRO is 0. */
7471 static void
7472 conditional_skip (ip, skip, type, control_macro, op)
7473 FILE_BUF *ip;
7474 int skip;
7475 enum node_type type;
7476 const U_CHAR *control_macro;
7477 FILE_BUF *op;
7479 IF_STACK_FRAME *temp;
7481 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7482 temp->fname = ip->nominal_fname;
7483 temp->fname_len = ip->nominal_fname_len;
7484 temp->lineno = ip->lineno;
7485 temp->next = if_stack;
7486 temp->control_macro = control_macro;
7487 if_stack = temp;
7489 if_stack->type = type;
7491 if (skip != 0) {
7492 skip_if_group (ip, 0, op);
7493 return;
7494 } else {
7495 ++if_stack->if_succeeded;
7496 output_line_directive (ip, &outbuf, 1, same_file);
7500 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7501 Leaves input ptr at the sharp sign found.
7502 If ANY is nonzero, return at next directive of any sort. */
7504 static void
7505 skip_if_group (ip, any, op)
7506 FILE_BUF *ip;
7507 int any;
7508 FILE_BUF *op;
7510 register U_CHAR *bp = ip->bufp, *cp;
7511 register U_CHAR *endb = ip->buf + ip->length;
7512 struct directive *kt;
7513 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
7514 U_CHAR *beg_of_line = bp;
7515 register int ident_length;
7516 U_CHAR *ident, *after_ident;
7517 /* Save info about where the group starts. */
7518 U_CHAR *beg_of_group = bp;
7519 int beg_lineno = ip->lineno;
7520 int skipping_include_directive = 0;
7522 if (output_conditionals && op != 0) {
7523 static const char * const ptr = "#failed\n";
7524 int len = strlen (ptr);
7526 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7528 *op->bufp++ = '\n';
7529 op->lineno++;
7531 check_expand (op, len);
7532 bcopy (ptr, (char *) op->bufp, len);
7533 op->bufp += len;
7534 op->lineno++;
7535 output_line_directive (ip, op, 1, 0);
7538 while (bp < endb) {
7539 switch (*bp++) {
7540 case '/': /* possible comment */
7541 if (*bp == '\\')
7542 newline_fix (bp);
7543 if (*bp == '*'
7544 || (cplusplus_comments && *bp == '/')) {
7545 ip->bufp = ++bp;
7546 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
7548 break;
7549 case '<':
7550 if (skipping_include_directive) {
7551 while (bp < endb && *bp != '>' && *bp != '\n') {
7552 if (*bp == '\\' && bp[1] == '\n') {
7553 ip->lineno++;
7554 bp++;
7556 bp++;
7559 break;
7560 case '\"':
7561 if (skipping_include_directive) {
7562 while (bp < endb && *bp != '\n') {
7563 if (*bp == '"') {
7564 bp++;
7565 break;
7567 if (*bp == '\\' && bp[1] == '\n') {
7568 ip->lineno++;
7569 bp++;
7571 bp++;
7573 break;
7575 /* Fall through. */
7576 case '\'':
7577 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
7578 NULL_PTR, NULL_PTR);
7579 break;
7580 case '\\':
7581 /* Char after backslash loses its special meaning in some cases. */
7582 if (*bp == '\n') {
7583 ++ip->lineno;
7584 bp++;
7585 } else if (traditional && bp < endb)
7586 bp++;
7587 break;
7588 case '\n':
7589 ++ip->lineno;
7590 beg_of_line = bp;
7591 skipping_include_directive = 0;
7592 break;
7593 case '%':
7594 if (beg_of_line == 0 || traditional)
7595 break;
7596 ip->bufp = bp - 1;
7597 while (bp[0] == '\\' && bp[1] == '\n')
7598 bp += 2;
7599 if (*bp == ':')
7600 goto sharp_token;
7601 break;
7602 case '#':
7603 /* # keyword: a # must be first nonblank char on the line */
7604 if (beg_of_line == 0)
7605 break;
7606 ip->bufp = bp - 1;
7607 sharp_token:
7608 /* Scan from start of line, skipping whitespace, comments
7609 and backslash-newlines, and see if we reach this #.
7610 If not, this # is not special. */
7611 bp = beg_of_line;
7612 /* If -traditional, require # to be at beginning of line. */
7613 if (!traditional) {
7614 while (1) {
7615 if (is_hor_space[*bp])
7616 bp++;
7617 else if (*bp == '\\' && bp[1] == '\n')
7618 bp += 2;
7619 else if (*bp == '/' && bp[1] == '*') {
7620 bp += 2;
7621 while (1)
7623 if (*bp == '*')
7625 if (bp[1] == '/')
7627 bp += 2;
7628 break;
7631 else
7633 #ifdef MULTIBYTE_CHARS
7634 int length;
7635 length = local_mblen (bp, endb - bp);
7636 if (length > 1)
7637 bp += (length - 1);
7638 #endif
7640 bp++;
7643 /* There is no point in trying to deal with C++ // comments here,
7644 because if there is one, then this # must be part of the
7645 comment and we would never reach here. */
7646 else break;
7649 if (bp != ip->bufp) {
7650 bp = ip->bufp + 1; /* Reset bp to after the #. */
7651 break;
7654 bp = ip->bufp + 1; /* Point after the '#' */
7655 if (ip->bufp[0] == '%') {
7656 /* Skip past the ':' again. */
7657 while (*bp == '\\') {
7658 ip->lineno++;
7659 bp += 2;
7661 bp++;
7664 /* Skip whitespace and \-newline. */
7665 while (1) {
7666 if (is_hor_space[*bp])
7667 bp++;
7668 else if (*bp == '\\' && bp[1] == '\n')
7669 bp += 2;
7670 else if (*bp == '/') {
7671 if (bp[1] == '\\')
7672 newline_fix (bp + 1);
7673 if (bp[1] == '*') {
7674 for (bp += 2; ; bp++) {
7675 if (*bp == '\n')
7676 ip->lineno++;
7677 else if (*bp == '*') {
7678 if (bp[-1] == '/' && warn_comments)
7679 warning ("`/*' within comment");
7680 if (bp[1] == '\\')
7681 newline_fix (bp + 1);
7682 if (bp[1] == '/')
7683 break;
7685 else
7687 #ifdef MULTIBYTE_CHARS
7688 int length;
7689 length = local_mblen (bp, endb - bp);
7690 if (length > 1)
7691 bp += (length - 1);
7692 #endif
7695 bp += 2;
7696 } else if (bp[1] == '/' && cplusplus_comments) {
7697 for (bp += 2; ; bp++) {
7698 if (*bp == '\n')
7699 break;
7700 if (*bp == '\\' && bp[1] == '\n')
7702 if (warn_comments)
7703 warning ("multiline `//' comment");
7704 ip->lineno++;
7705 bp++;
7707 else
7709 #ifdef MULTIBYTE_CHARS
7710 int length;
7711 length = local_mblen (bp, endb - bp);
7712 if (length > 1)
7713 bp += (length - 1);
7714 #endif
7717 } else
7718 break;
7719 } else
7720 break;
7723 cp = bp;
7725 /* Now find end of directive name.
7726 If we encounter a backslash-newline, exchange it with any following
7727 symbol-constituents so that we end up with a contiguous name. */
7729 while (1) {
7730 if (is_idchar[*bp])
7731 bp++;
7732 else {
7733 if (*bp == '\\')
7734 name_newline_fix (bp);
7735 if (is_idchar[*bp])
7736 bp++;
7737 else break;
7740 ident_length = bp - cp;
7741 ident = cp;
7742 after_ident = bp;
7744 /* A line of just `#' becomes blank. */
7746 if (ident_length == 0 && *after_ident == '\n') {
7747 continue;
7750 if (ident_length == 0 || !is_idstart[*ident]) {
7751 U_CHAR *p = ident;
7752 while (is_idchar[*p]) {
7753 if (*p < '0' || *p > '9')
7754 break;
7755 p++;
7757 /* Handle # followed by a line number. */
7758 if (p != ident && !is_idchar[*p]) {
7759 if (pedantic)
7760 pedwarn ("`#' followed by integer");
7761 continue;
7764 /* Avoid error for `###' and similar cases unless -pedantic. */
7765 if (p == ident) {
7766 while (*p == '#' || is_hor_space[*p]) p++;
7767 if (*p == '\n') {
7768 if (pedantic && !lang_asm)
7769 pedwarn ("invalid preprocessing directive");
7770 continue;
7774 if (!lang_asm && pedantic)
7775 pedwarn ("invalid preprocessing directive name");
7776 continue;
7779 for (kt = directive_table; kt->length >= 0; kt++) {
7780 IF_STACK_FRAME *temp;
7781 if (ident_length == kt->length
7782 && bcmp (cp, kt->name, kt->length) == 0) {
7783 /* If we are asked to return on next directive, do so now. */
7784 if (any)
7785 goto done;
7787 switch (kt->type) {
7788 case T_IF:
7789 case T_IFDEF:
7790 case T_IFNDEF:
7791 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7792 temp->next = if_stack;
7793 if_stack = temp;
7794 temp->lineno = ip->lineno;
7795 temp->fname = ip->nominal_fname;
7796 temp->fname_len = ip->nominal_fname_len;
7797 temp->type = kt->type;
7798 break;
7799 case T_ELSE:
7800 case T_ENDIF:
7801 if (pedantic && if_stack != save_if_stack)
7802 validate_else (bp, endb);
7803 case T_ELIF:
7804 if (if_stack == instack[indepth].if_stack) {
7805 error ("`#%s' not within a conditional", kt->name);
7806 break;
7808 else if (if_stack == save_if_stack)
7809 goto done; /* found what we came for */
7811 if (kt->type != T_ENDIF) {
7812 if (if_stack->type == T_ELSE)
7813 error ("`#else' or `#elif' after `#else'");
7814 if_stack->type = kt->type;
7815 break;
7818 temp = if_stack;
7819 if_stack = if_stack->next;
7820 free (temp);
7821 break;
7823 case T_INCLUDE:
7824 case T_INCLUDE_NEXT:
7825 case T_IMPORT:
7826 skipping_include_directive = 1;
7827 break;
7829 default:
7830 break;
7832 break;
7835 /* Don't let erroneous code go by. */
7836 if (kt->length < 0 && !lang_asm && pedantic)
7837 pedwarn ("invalid preprocessing directive name");
7841 ip->bufp = bp;
7842 /* after this returns, rescan will exit because ip->bufp
7843 now points to the end of the buffer.
7844 rescan is responsible for the error message also. */
7846 done:
7847 if (output_conditionals && op != 0) {
7848 static const char * const ptr = "#endfailed\n";
7849 int len = strlen (ptr);
7851 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7853 *op->bufp++ = '\n';
7854 op->lineno++;
7856 check_expand (op, beg_of_line - beg_of_group);
7857 bcopy ((const PTR) beg_of_group, (PTR) op->bufp,
7858 beg_of_line - beg_of_group);
7859 op->bufp += beg_of_line - beg_of_group;
7860 op->lineno += ip->lineno - beg_lineno;
7861 check_expand (op, len);
7862 bcopy (ptr, (char *) op->bufp, len);
7863 op->bufp += len;
7864 op->lineno++;
7868 /* Handle a #else directive. Do this by just continuing processing
7869 without changing if_stack ; this is so that the error message
7870 for missing #endif's etc. will point to the original #if. It
7871 is possible that something different would be better. */
7873 static int
7874 do_else (buf, limit, op, keyword)
7875 U_CHAR *buf, *limit;
7876 FILE_BUF *op;
7877 struct directive *keyword ATTRIBUTE_UNUSED;
7879 FILE_BUF *ip = &instack[indepth];
7881 if (pedantic) {
7882 SKIP_WHITE_SPACE (buf);
7883 if (buf != limit)
7884 pedwarn ("text following `#else' violates ANSI standard");
7887 if (if_stack == instack[indepth].if_stack) {
7888 error ("`#else' not within a conditional");
7889 return 0;
7890 } else {
7891 /* #ifndef can't have its special treatment for containing the whole file
7892 if it has a #else clause. */
7893 if_stack->control_macro = 0;
7895 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7896 error ("`#else' after `#else'");
7897 fprintf (stderr, " (matches line %d", if_stack->lineno);
7898 if (! (if_stack->fname_len == ip->nominal_fname_len
7899 && !bcmp (if_stack->fname, ip->nominal_fname,
7900 if_stack->fname_len))) {
7901 fprintf (stderr, ", file ");
7902 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7903 if_stack->fname_len, stderr);
7905 fprintf (stderr, ")\n");
7907 if_stack->type = T_ELSE;
7910 if (if_stack->if_succeeded)
7911 skip_if_group (ip, 0, op);
7912 else {
7913 ++if_stack->if_succeeded; /* continue processing input */
7914 output_line_directive (ip, op, 1, same_file);
7916 return 0;
7919 /* Unstack after #endif directive. */
7921 static int
7922 do_endif (buf, limit, op, keyword)
7923 U_CHAR *buf, *limit;
7924 FILE_BUF *op;
7925 struct directive *keyword ATTRIBUTE_UNUSED;
7927 if (pedantic) {
7928 SKIP_WHITE_SPACE (buf);
7929 if (buf != limit)
7930 pedwarn ("text following `#endif' violates ANSI standard");
7933 if (if_stack == instack[indepth].if_stack)
7934 error ("unbalanced `#endif'");
7935 else {
7936 IF_STACK_FRAME *temp = if_stack;
7937 if_stack = if_stack->next;
7938 if (temp->control_macro != 0) {
7939 /* This #endif matched a #ifndef at the start of the file.
7940 See if it is at the end of the file. */
7941 FILE_BUF *ip = &instack[indepth];
7942 U_CHAR *p = ip->bufp;
7943 U_CHAR *ep = ip->buf + ip->length;
7945 while (p != ep) {
7946 U_CHAR c = *p++;
7947 if (!is_space[c]) {
7948 if (c == '/'
7949 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7950 /* Skip this comment. */
7951 int junk = 0;
7952 U_CHAR *save_bufp = ip->bufp;
7953 ip->bufp = p + 1;
7954 p = skip_to_end_of_comment (ip, &junk, 1);
7955 ip->bufp = save_bufp;
7956 } else
7957 goto fail;
7960 /* If we get here, this #endif ends a #ifndef
7961 that contains all of the file (aside from whitespace).
7962 Arrange not to include the file again
7963 if the macro that was tested is defined.
7965 Do not do this for the top-level file in a -include or any
7966 file in a -imacros. */
7967 if (indepth != 0
7968 && ! (indepth == 1 && no_record_file)
7969 && ! (no_record_file && no_output))
7970 record_control_macro (ip->inc, temp->control_macro);
7971 fail: ;
7973 free (temp);
7974 output_line_directive (&instack[indepth], op, 1, same_file);
7976 return 0;
7979 /* When an #else or #endif is found while skipping failed conditional,
7980 if -pedantic was specified, this is called to warn about text after
7981 the directive name. P points to the first char after the directive
7982 name. */
7984 static void
7985 validate_else (p, limit)
7986 register const U_CHAR *p;
7987 register const U_CHAR *limit;
7989 /* Advance P over whitespace and comments. */
7990 while (1) {
7991 while (*p == '\\' && p[1] == '\n')
7992 p += 2;
7993 if (is_hor_space[*p])
7994 p++;
7995 else if (*p == '/') {
7996 while (p[1] == '\\' && p[2] == '\n')
7997 p += 2;
7998 if (p[1] == '*') {
7999 /* Don't bother warning about unterminated comments
8000 since that will happen later. Just be sure to exit. */
8001 for (p += 2; ; p++) {
8002 if (p == limit)
8003 return;
8004 if (*p == '*') {
8005 while (p[1] == '\\' && p[2] == '\n')
8006 p += 2;
8007 if (p[1] == '/') {
8008 p += 2;
8009 break;
8012 else
8014 #ifdef MULTIBYTE_CHARS
8015 int length;
8016 length = local_mblen (p, limit - p);
8017 if (length > 1)
8018 p += (length - 1);
8019 #endif
8023 else if (cplusplus_comments && p[1] == '/')
8024 return;
8025 else break;
8026 } else break;
8028 if (*p != '\n')
8029 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8032 /* Skip a comment, assuming the input ptr immediately follows the
8033 initial slash-star. Bump *LINE_COUNTER for each newline.
8034 (The canonical line counter is &ip->lineno.)
8035 Don't use this routine (or the next one) if bumping the line
8036 counter is not sufficient to deal with newlines in the string.
8038 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8039 This feature is useful when processing a comment that is going to
8040 be processed or was processed at another point in the preprocessor,
8041 to avoid a duplicate warning. Likewise for unterminated comment
8042 errors. */
8044 static U_CHAR *
8045 skip_to_end_of_comment (ip, line_counter, nowarn)
8046 register FILE_BUF *ip;
8047 int *line_counter; /* place to remember newlines, or NULL */
8048 int nowarn;
8050 register U_CHAR *limit = ip->buf + ip->length;
8051 register U_CHAR *bp = ip->bufp;
8052 FILE_BUF *op = put_out_comments && !line_counter ? &outbuf : (FILE_BUF *) 0;
8053 int start_line = line_counter ? *line_counter : 0;
8055 /* JF this line_counter stuff is a crock to make sure the
8056 comment is only put out once, no matter how many times
8057 the comment is skipped. It almost works */
8058 if (op) {
8059 *op->bufp++ = '/';
8060 *op->bufp++ = bp[-1];
8062 if (cplusplus_comments && bp[-1] == '/') {
8063 for (; bp < limit; bp++) {
8064 if (*bp == '\n')
8065 break;
8066 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
8068 if (!nowarn && warn_comments)
8069 warning ("multiline `//' comment");
8070 if (line_counter)
8071 ++*line_counter;
8072 if (op)
8074 ++op->lineno;
8075 *op->bufp++ = *bp;
8077 ++bp;
8079 else
8081 #ifdef MULTIBYTE_CHARS
8082 int length;
8083 length = local_mblen (bp, limit - bp);
8084 if (length > 1)
8086 if (op)
8088 bcopy (bp, op->bufp, length - 1);
8089 op->bufp += (length - 1);
8091 bp += (length - 1);
8093 #endif
8095 if (op)
8096 *op->bufp++ = *bp;
8098 ip->bufp = bp;
8099 return bp;
8101 while (bp < limit) {
8102 if (op)
8103 *op->bufp++ = *bp;
8104 switch (*bp++) {
8105 case '\n':
8106 /* If this is the end of the file, we have an unterminated comment.
8107 Don't swallow the newline. We are guaranteed that there will be a
8108 trailing newline and various pieces assume it's there. */
8109 if (bp == limit)
8111 --bp;
8112 --limit;
8113 break;
8115 if (line_counter != NULL)
8116 ++*line_counter;
8117 if (op)
8118 ++op->lineno;
8119 break;
8120 case '*':
8121 if (bp[-2] == '/' && !nowarn && warn_comments)
8122 warning ("`/*' within comment");
8123 if (*bp == '\\')
8124 newline_fix (bp);
8125 if (*bp == '/') {
8126 if (op)
8127 *op->bufp++ = '/';
8128 ip->bufp = ++bp;
8129 return bp;
8131 break;
8132 #ifdef MULTIBYTE_CHARS
8133 default:
8135 int length;
8136 bp--;
8137 length = local_mblen (bp, limit - bp);
8138 if (length <= 0)
8139 length = 1;
8140 if (op)
8142 op->bufp--;
8143 bcopy (bp, op->bufp, length);
8144 op->bufp += length;
8146 bp += length;
8148 #endif
8152 if (!nowarn)
8153 error_with_line (line_for_error (start_line), "unterminated comment");
8154 ip->bufp = bp;
8155 return bp;
8158 /* Skip over a quoted string. BP points to the opening quote.
8159 Returns a pointer after the closing quote. Don't go past LIMIT.
8160 START_LINE is the line number of the starting point (but it need
8161 not be valid if the starting point is inside a macro expansion).
8163 The input stack state is not changed.
8165 If COUNT_NEWLINES is nonzero, it points to an int to increment
8166 for each newline passed; also, warn about any white space
8167 just before line end.
8169 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8170 if we pass a backslash-newline.
8172 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8174 static U_CHAR *
8175 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
8176 register const U_CHAR *bp;
8177 register const U_CHAR *limit;
8178 int start_line;
8179 int *count_newlines;
8180 int *backslash_newlines_p;
8181 int *eofp;
8183 register U_CHAR c, match;
8185 match = *bp++;
8186 while (1) {
8187 if (bp >= limit) {
8188 error_with_line (line_for_error (start_line),
8189 "unterminated string or character constant");
8190 error_with_line (multiline_string_line,
8191 "possible real start of unterminated constant");
8192 multiline_string_line = 0;
8193 if (eofp)
8194 *eofp = 1;
8195 break;
8197 c = *bp++;
8198 if (c == '\\') {
8199 while (*bp == '\\' && bp[1] == '\n') {
8200 if (backslash_newlines_p)
8201 *backslash_newlines_p = 1;
8202 if (count_newlines)
8203 ++*count_newlines;
8204 bp += 2;
8206 if (*bp == '\n') {
8207 if (backslash_newlines_p)
8208 *backslash_newlines_p = 1;
8209 if (count_newlines)
8210 ++*count_newlines;
8212 bp++;
8213 } else if (c == '\n') {
8214 if (traditional) {
8215 /* Unterminated strings and character constants are 'valid'. */
8216 bp--; /* Don't consume the newline. */
8217 if (eofp)
8218 *eofp = 1;
8219 break;
8221 if (match == '\'') {
8222 error_with_line (line_for_error (start_line),
8223 "unterminated character constant");
8224 bp--;
8225 if (eofp)
8226 *eofp = 1;
8227 break;
8229 /* If not traditional, then allow newlines inside strings. */
8230 if (count_newlines) {
8231 if (warn_white_space && is_hor_space[bp[-2]])
8232 warning ("white space at end of line in string");
8233 ++*count_newlines;
8235 if (multiline_string_line == 0) {
8236 if (pedantic)
8237 pedwarn_with_line (line_for_error (start_line),
8238 "string constant runs past end of line");
8239 multiline_string_line = start_line;
8241 } else if (c == match)
8242 break;
8243 #ifdef MULTIBYTE_CHARS
8245 int length;
8246 --bp;
8247 length = local_mblen (bp, limit - bp);
8248 if (length <= 0)
8249 length = 1;
8250 bp += length;
8252 #endif
8254 return (U_CHAR *) bp;
8257 /* Place into DST a quoted string representing the string SRC.
8258 SRCLEN is the length of SRC; SRC may contain null bytes.
8259 Return the address of DST's terminating null. */
8261 static char *
8262 quote_string (dst, src, srclen)
8263 char *dst;
8264 const char *src;
8265 size_t srclen;
8267 U_CHAR c;
8268 const char *srclim = src + srclen;
8270 *dst++ = '\"';
8271 while (src != srclim)
8272 switch ((c = *src++))
8274 default:
8275 if (ISPRINT (c))
8276 *dst++ = c;
8277 else
8279 sprintf (dst, "\\%03o", c);
8280 dst += 4;
8282 break;
8284 case '\"':
8285 case '\\':
8286 *dst++ = '\\';
8287 *dst++ = c;
8288 break;
8291 *dst++ = '\"';
8292 *dst = '\0';
8293 return dst;
8296 /* Skip across a group of balanced parens, starting from IP->bufp.
8297 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8299 This does not handle newlines, because it's used for the arg of #if,
8300 where there aren't any newlines. Also, backslash-newline can't appear. */
8302 static U_CHAR *
8303 skip_paren_group (ip)
8304 register FILE_BUF *ip;
8306 U_CHAR *limit = ip->buf + ip->length;
8307 U_CHAR *p = ip->bufp;
8308 int depth = 0;
8309 int lines_dummy = 0;
8311 while (p != limit) {
8312 int c = *p++;
8313 switch (c) {
8314 case '(':
8315 depth++;
8316 break;
8318 case ')':
8319 depth--;
8320 if (depth == 0)
8321 return ip->bufp = p;
8322 break;
8324 case '/':
8325 if (*p == '*') {
8326 ip->bufp = p;
8327 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
8328 p = ip->bufp;
8331 case '"':
8332 case '\'':
8334 int eofp = 0;
8335 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
8336 if (eofp)
8337 return ip->bufp = p;
8339 break;
8343 ip->bufp = p;
8344 return p;
8347 /* Write out a #line directive, for instance, after an #include file.
8348 If CONDITIONAL is nonzero, we can omit the #line if it would
8349 appear to be a no-op, and we can output a few newlines instead
8350 if we want to increase the line number by a small amount.
8351 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8353 static void
8354 output_line_directive (ip, op, conditional, file_change)
8355 FILE_BUF *ip, *op;
8356 int conditional;
8357 enum file_change_code file_change;
8359 int len;
8360 char *line_directive_buf, *line_end;
8362 if (no_line_directives
8363 || ip->fname == NULL
8364 || no_output) {
8365 op->lineno = ip->lineno;
8366 return;
8369 if (conditional) {
8370 if (ip->lineno == op->lineno)
8371 return;
8373 /* If the inherited line number is a little too small,
8374 output some newlines instead of a #line directive. */
8375 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
8376 check_expand (op, 10);
8377 while (ip->lineno > op->lineno) {
8378 *op->bufp++ = '\n';
8379 op->lineno++;
8381 return;
8385 /* Output a positive line number if possible. */
8386 while (ip->lineno <= 0 && ip->bufp - ip->buf < ip->length
8387 && *ip->bufp == '\n') {
8388 ip->lineno++;
8389 ip->bufp++;
8392 line_directive_buf = (char *) alloca (4 * ip->nominal_fname_len + 100);
8393 sprintf (line_directive_buf, "# %d ", ip->lineno);
8394 line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
8395 ip->nominal_fname, ip->nominal_fname_len);
8396 if (file_change != same_file) {
8397 *line_end++ = ' ';
8398 *line_end++ = file_change == enter_file ? '1' : '2';
8400 /* Tell cc1 if following text comes from a system header file. */
8401 if (ip->system_header_p) {
8402 *line_end++ = ' ';
8403 *line_end++ = '3';
8405 #ifndef NO_IMPLICIT_EXTERN_C
8406 /* Tell cc1plus if following text should be treated as C. */
8407 if (ip->system_header_p == 2 && cplusplus) {
8408 *line_end++ = ' ';
8409 *line_end++ = '4';
8411 #endif
8412 *line_end++ = '\n';
8413 len = line_end - line_directive_buf;
8414 check_expand (op, len + 1);
8415 if (op->bufp > op->buf && op->bufp[-1] != '\n')
8416 *op->bufp++ = '\n';
8417 bcopy ((const PTR) line_directive_buf, (PTR) op->bufp, len);
8418 op->bufp += len;
8419 op->lineno = ip->lineno;
8422 /* This structure represents one parsed argument in a macro call.
8423 `raw' points to the argument text as written (`raw_length' is its length).
8424 `expanded' points to the argument's macro-expansion
8425 (its length is `expand_length', and its allocated size is `expand_size').
8426 `stringified_length_bound' is an upper bound on the length
8427 the argument would have if stringified.
8428 `use_count' is the number of times this macro arg is substituted
8429 into the macro. If the actual use count exceeds 10,
8430 the value stored is 10.
8431 `free1' and `free2', if nonzero, point to blocks to be freed
8432 when the macro argument data is no longer needed. */
8434 struct argdata {
8435 U_CHAR *raw, *expanded;
8436 int raw_length, expand_length, expand_size;
8437 int stringified_length_bound;
8438 U_CHAR *free1, *free2;
8439 char newlines;
8440 char use_count;
8443 /* Expand a macro call.
8444 HP points to the symbol that is the macro being called.
8445 Put the result of expansion onto the input stack
8446 so that subsequent input by our caller will use it.
8448 If macro wants arguments, caller has already verified that
8449 an argument list follows; arguments come from the input stack. */
8451 static void
8452 macroexpand (hp, op)
8453 HASHNODE *hp;
8454 FILE_BUF *op;
8456 int nargs;
8457 DEFINITION *defn = hp->value.defn;
8458 register U_CHAR *xbuf;
8459 int xbuf_len;
8460 int start_line = instack[indepth].lineno;
8461 int rest_args, rest_zero;
8463 CHECK_DEPTH (return;);
8465 /* it might not actually be a macro. */
8466 if (hp->type != T_MACRO) {
8467 special_symbol (hp, op);
8468 return;
8471 /* This macro is being used inside a #if, which means it must be */
8472 /* recorded as a precondition. */
8473 if (pcp_inside_if && pcp_outfile && defn->predefined)
8474 dump_single_macro (hp, pcp_outfile);
8476 nargs = defn->nargs;
8478 if (nargs >= 0) {
8479 register int i;
8480 struct argdata *args;
8481 int parse_error = 0;
8483 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
8485 for (i = 0; i < nargs; i++) {
8486 args[i].raw = (U_CHAR *) "";
8487 args[i].expanded = 0;
8488 args[i].raw_length = args[i].expand_length = args[i].expand_size
8489 = args[i].stringified_length_bound = 0;
8490 args[i].free1 = args[i].free2 = 0;
8491 args[i].use_count = 0;
8494 /* Parse all the macro args that are supplied. I counts them.
8495 The first NARGS args are stored in ARGS.
8496 The rest are discarded.
8497 If rest_args is set then we assume macarg absorbed the rest of the args.
8499 i = 0;
8500 rest_args = 0;
8501 do {
8502 /* Discard the open-parenthesis or comma before the next arg. */
8503 ++instack[indepth].bufp;
8504 if (rest_args)
8505 continue;
8506 if (i < nargs || (nargs == 0 && i == 0)) {
8507 /* If we are working on last arg which absorbs rest of args... */
8508 if (i == nargs - 1 && defn->rest_args)
8509 rest_args = 1;
8510 parse_error = macarg (&args[i], rest_args);
8512 else
8513 parse_error = macarg (NULL_PTR, 0);
8514 if (parse_error) {
8515 error_with_line (line_for_error (start_line),
8516 "unterminated macro call");
8517 break;
8519 i++;
8520 } while (*instack[indepth].bufp != ')');
8522 /* If we got one arg but it was just whitespace, call that 0 args. */
8523 if (i == 1) {
8524 register const U_CHAR *bp = args[0].raw;
8525 register const U_CHAR *lim = bp + args[0].raw_length;
8526 /* cpp.texi says for foo ( ) we provide one argument.
8527 However, if foo wants just 0 arguments, treat this as 0. */
8528 if (nargs == 0)
8529 while (bp != lim && is_space[*bp]) bp++;
8530 if (bp == lim)
8531 i = 0;
8534 /* Don't output an error message if we have already output one for
8535 a parse error above. */
8536 rest_zero = 0;
8537 if (nargs == 0 && i > 0) {
8538 if (! parse_error)
8539 error ("arguments given to macro `%s'", hp->name);
8540 } else if (i < nargs) {
8541 /* traditional C allows foo() if foo wants one argument. */
8542 if (nargs == 1 && i == 0 && traditional)
8544 /* the rest args token is allowed to absorb 0 tokens */
8545 else if (i == nargs - 1 && defn->rest_args)
8546 rest_zero = 1;
8547 else if (parse_error)
8549 else if (i == 0)
8550 error ("macro `%s' used without args", hp->name);
8551 else if (i == 1)
8552 error ("macro `%s' used with just one arg", hp->name);
8553 else
8554 error ("macro `%s' used with only %d args", hp->name, i);
8555 } else if (i > nargs) {
8556 if (! parse_error)
8557 error ("macro `%s' used with too many (%d) args", hp->name, i);
8560 /* Swallow the closeparen. */
8561 ++instack[indepth].bufp;
8563 /* If macro wants zero args, we parsed the arglist for checking only.
8564 Read directly from the macro definition. */
8565 if (nargs == 0) {
8566 xbuf = defn->expansion;
8567 xbuf_len = defn->length;
8568 } else {
8569 register U_CHAR *exp = defn->expansion;
8570 register int offset; /* offset in expansion,
8571 copied a piece at a time */
8572 register int totlen; /* total amount of exp buffer filled so far */
8574 register struct reflist *ap, *last_ap;
8576 /* Macro really takes args. Compute the expansion of this call. */
8578 /* Compute length in characters of the macro's expansion.
8579 Also count number of times each arg is used. */
8580 xbuf_len = defn->length;
8581 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
8582 if (ap->stringify)
8583 xbuf_len += args[ap->argno].stringified_length_bound;
8584 else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
8585 /* Add 4 for two newline-space markers to prevent
8586 token concatenation. */
8587 xbuf_len += args[ap->argno].raw_length + 4;
8588 else {
8589 /* We have an ordinary (expanded) occurrence of the arg.
8590 So compute its expansion, if we have not already. */
8591 if (args[ap->argno].expanded == 0) {
8592 FILE_BUF obuf;
8593 obuf = expand_to_temp_buffer (args[ap->argno].raw,
8594 args[ap->argno].raw + args[ap->argno].raw_length,
8595 1, 0);
8597 args[ap->argno].expanded = obuf.buf;
8598 args[ap->argno].expand_length = obuf.bufp - obuf.buf;
8599 args[ap->argno].expand_size = obuf.length;
8600 args[ap->argno].free2 = obuf.buf;
8602 xbuf_len += args[ap->argno].expand_length;
8603 } else {
8604 /* If the arg appears more than once, its later occurrences
8605 may have newline turned into backslash-'n', which is a
8606 factor of 2 expansion. */
8607 xbuf_len += 2 * args[ap->argno].expand_length;
8609 /* Add 4 for two newline-space markers to prevent
8610 token concatenation. */
8611 xbuf_len += 4;
8613 if (args[ap->argno].use_count < 10)
8614 args[ap->argno].use_count++;
8617 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
8619 /* Generate in XBUF the complete expansion
8620 with arguments substituted in.
8621 TOTLEN is the total size generated so far.
8622 OFFSET is the index in the definition
8623 of where we are copying from. */
8624 offset = totlen = 0;
8625 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
8626 last_ap = ap, ap = ap->next) {
8627 register struct argdata *arg = &args[ap->argno];
8628 int count_before = totlen;
8630 /* Add chars to XBUF. */
8631 for (i = 0; i < ap->nchars; i++, offset++)
8632 xbuf[totlen++] = exp[offset];
8634 /* If followed by an empty rest arg with concatenation,
8635 delete the last run of nonwhite chars. */
8636 if (rest_zero && totlen > count_before
8637 && ((ap->rest_args && ap->raw_before != 0)
8638 || (last_ap != NULL && last_ap->rest_args
8639 && last_ap->raw_after != 0))) {
8640 /* Delete final whitespace. */
8641 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
8642 totlen--;
8645 /* Delete the nonwhites before them. */
8646 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
8647 totlen--;
8651 if (ap->stringify != 0) {
8652 int arglen = arg->raw_length;
8653 int escaped = 0;
8654 int in_string = 0;
8655 int c;
8656 i = 0;
8657 while (i < arglen
8658 && (c = arg->raw[i], is_space[c]))
8659 i++;
8660 while (i < arglen
8661 && (c = arg->raw[arglen - 1], is_space[c]))
8662 arglen--;
8663 if (!traditional)
8664 xbuf[totlen++] = '\"'; /* insert beginning quote */
8665 for (; i < arglen; i++) {
8666 c = arg->raw[i];
8668 if (in_string) {
8669 /* Generate nothing for backslash-newline in a string. */
8670 if (c == '\\' && arg->raw[i + 1] == '\n') {
8671 i++;
8672 continue;
8674 } else {
8675 /* Special markers
8676 generate nothing for a stringified argument. */
8677 if (c == '\n') {
8678 i++;
8679 continue;
8682 /* Internal sequences of whitespace are replaced by one space
8683 except within a string or char token. */
8684 if (is_space[c]) {
8685 i++;
8686 while (is_space[(c = arg->raw[i])])
8687 /* Newline markers can occur within a whitespace sequence;
8688 consider them part of the sequence. */
8689 i += (c == '\n') + 1;
8690 i--;
8691 c = ' ';
8695 if (escaped)
8696 escaped = 0;
8697 else {
8698 if (c == '\\')
8699 escaped = 1;
8700 else if (in_string) {
8701 if (c == in_string)
8702 in_string = 0;
8703 else
8705 #ifdef MULTIBYTE_CHARS
8706 int length;
8707 length = local_mblen (arg->raw + i, arglen - i);
8708 if (length > 1)
8710 bcopy (arg->raw + i, xbuf + totlen, length);
8711 i += length - 1;
8712 totlen += length;
8713 continue;
8715 #endif
8717 } else if (c == '\"' || c == '\'')
8718 in_string = c;
8721 /* Escape double-quote, and backslashes in strings.
8722 Newlines in strings are best escaped as \n, since
8723 otherwise backslash-backslash-newline-newline is
8724 mishandled. The C Standard doesn't allow newlines in
8725 strings, so we can escape newlines as we please. */
8726 if (c == '\"'
8727 || (in_string
8728 && (c == '\\'
8729 || (c == '\n' ? (c = 'n', 1) : 0))))
8730 xbuf[totlen++] = '\\';
8731 /* We used to output e.g. \008 for control characters here,
8732 but this doesn't conform to the C Standard.
8733 Just output the characters as-is. */
8734 xbuf[totlen++] = c;
8736 if (!traditional)
8737 xbuf[totlen++] = '\"'; /* insert ending quote */
8738 } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
8739 const U_CHAR *p1 = arg->raw;
8740 const U_CHAR *l1 = p1 + arg->raw_length;
8741 if (ap->raw_before != 0) {
8742 while (p1 != l1 && is_space[*p1]) p1++;
8743 while (p1 != l1 && is_idchar[*p1])
8744 xbuf[totlen++] = *p1++;
8745 /* Delete any no-reexpansion marker that follows
8746 an identifier at the beginning of the argument
8747 if the argument is concatenated with what precedes it. */
8748 if (p1[0] == '\n' && p1[1] == '-')
8749 p1 += 2;
8750 } else if (!traditional) {
8751 /* Ordinary expanded use of the argument.
8752 Put in newline-space markers to prevent token pasting. */
8753 xbuf[totlen++] = '\n';
8754 xbuf[totlen++] = ' ';
8756 if (ap->raw_after != 0) {
8757 /* Arg is concatenated after: delete trailing whitespace,
8758 whitespace markers, and no-reexpansion markers. */
8759 while (p1 != l1) {
8760 if (is_space[l1[-1]]) l1--;
8761 else if (l1[-1] == '-') {
8762 const U_CHAR *p2 = l1 - 1;
8763 /* If a `-' is preceded by an odd number of newlines then it
8764 and the last newline are a no-reexpansion marker. */
8765 while (p2 != p1 && p2[-1] == '\n') p2--;
8766 if ((l1 - 1 - p2) & 1) {
8767 l1 -= 2;
8769 else break;
8771 else break;
8775 bcopy ((const PTR) p1, (PTR) (xbuf + totlen), l1 - p1);
8776 totlen += l1 - p1;
8777 if (!traditional && ap->raw_after == 0) {
8778 /* Ordinary expanded use of the argument.
8779 Put in newline-space markers to prevent token pasting. */
8780 xbuf[totlen++] = '\n';
8781 xbuf[totlen++] = ' ';
8783 } else {
8784 /* Ordinary expanded use of the argument.
8785 Put in newline-space markers to prevent token pasting. */
8786 if (!traditional) {
8787 xbuf[totlen++] = '\n';
8788 xbuf[totlen++] = ' ';
8790 bcopy ((const PTR) arg->expanded, (PTR) (xbuf + totlen),
8791 arg->expand_length);
8792 totlen += arg->expand_length;
8793 if (!traditional) {
8794 xbuf[totlen++] = '\n';
8795 xbuf[totlen++] = ' ';
8797 /* If a macro argument with newlines is used multiple times,
8798 then only expand the newlines once. This avoids creating output
8799 lines which don't correspond to any input line, which confuses
8800 gdb and gcov. */
8801 if (arg->use_count > 1 && arg->newlines > 0) {
8802 /* Don't bother doing change_newlines for subsequent
8803 uses of arg. */
8804 arg->use_count = 1;
8805 change_newlines (arg);
8809 if (totlen > xbuf_len)
8810 abort ();
8813 /* If there is anything left of the definition after handling
8814 the arg list, copy that in too. */
8816 for (i = offset; i < defn->length; i++) {
8817 /* if we've reached the end of the macro */
8818 if (exp[i] == ')')
8819 rest_zero = 0;
8820 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
8821 && last_ap->raw_after != 0))
8822 xbuf[totlen++] = exp[i];
8825 xbuf[totlen] = 0;
8826 xbuf_len = totlen;
8828 for (i = 0; i < nargs; i++) {
8829 if (args[i].free1 != 0)
8830 free (args[i].free1);
8831 if (args[i].free2 != 0)
8832 free (args[i].free2);
8835 } else {
8836 xbuf = defn->expansion;
8837 xbuf_len = defn->length;
8840 /* Now put the expansion on the input stack
8841 so our caller will commence reading from it. */
8843 register FILE_BUF *ip2;
8845 ip2 = &instack[++indepth];
8847 ip2->fname = 0;
8848 ip2->nominal_fname = 0;
8849 ip2->nominal_fname_len = 0;
8850 ip2->inc = 0;
8851 /* This may not be exactly correct, but will give much better error
8852 messages for nested macro calls than using a line number of zero. */
8853 ip2->lineno = start_line;
8854 ip2->buf = xbuf;
8855 ip2->length = xbuf_len;
8856 ip2->bufp = xbuf;
8857 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
8858 ip2->macro = hp;
8859 ip2->if_stack = if_stack;
8860 ip2->system_header_p = 0;
8862 /* Recursive macro use sometimes works traditionally.
8863 #define foo(x,y) bar (x (y,0), y)
8864 foo (foo, baz) */
8866 if (!traditional)
8867 hp->type = T_DISABLED;
8871 /* Parse a macro argument and store the info on it into *ARGPTR.
8872 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8873 Return nonzero to indicate a syntax error. */
8875 static int
8876 macarg (argptr, rest_args)
8877 register struct argdata *argptr;
8878 int rest_args;
8880 FILE_BUF *ip = &instack[indepth];
8881 int paren = 0;
8882 int lineno0 = ip->lineno;
8883 int comments = 0;
8884 int result = 0;
8886 /* Try to parse as much of the argument as exists at this
8887 input stack level. */
8888 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro,
8889 &paren, &ip->lineno, &comments, rest_args);
8891 /* If we find the end of the argument at this level,
8892 set up *ARGPTR to point at it in the input stack. */
8893 if (!(ip->fname != 0 && (ip->lineno != lineno0 || comments != 0))
8894 && bp != ip->buf + ip->length) {
8895 if (argptr != 0) {
8896 argptr->raw = ip->bufp;
8897 argptr->raw_length = bp - ip->bufp;
8898 argptr->newlines = ip->lineno - lineno0;
8900 ip->bufp = bp;
8901 } else {
8902 /* This input stack level ends before the macro argument does.
8903 We must pop levels and keep parsing.
8904 Therefore, we must allocate a temporary buffer and copy
8905 the macro argument into it. */
8906 int bufsize = bp - ip->bufp;
8907 int extra = ip->lineno - lineno0;
8908 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
8909 int final_start = 0;
8911 bcopy ((const PTR) ip->bufp, (PTR) buffer, bufsize);
8912 ip->bufp = bp;
8914 while (bp == ip->buf + ip->length) {
8915 if (instack[indepth].macro == 0) {
8916 result = 1;
8917 break;
8919 ip->macro->type = T_MACRO;
8920 if (ip->free_ptr)
8921 free (ip->free_ptr);
8922 ip = &instack[--indepth];
8923 lineno0 = ip->lineno;
8924 comments = 0;
8925 bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro, &paren,
8926 &ip->lineno, &comments, rest_args);
8927 final_start = bufsize;
8928 bufsize += bp - ip->bufp;
8929 extra += ip->lineno - lineno0;
8930 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
8931 bcopy ((const PTR) ip->bufp, (PTR) (buffer + bufsize - (bp - ip->bufp)),
8932 bp - ip->bufp);
8933 ip->bufp = bp;
8936 /* Now, if arg is actually wanted, record its raw form,
8937 discarding comments and duplicating newlines in whatever
8938 part of it did not come from a macro expansion.
8939 EXTRA space has been preallocated for duplicating the newlines.
8940 FINAL_START is the index of the start of that part. */
8941 if (argptr != 0) {
8942 argptr->raw = buffer;
8943 argptr->raw_length = bufsize;
8944 argptr->free1 = buffer;
8945 argptr->newlines = ip->lineno - lineno0;
8946 if ((argptr->newlines || comments) && ip->fname != 0)
8947 argptr->raw_length
8948 = final_start +
8949 discard_comments (argptr->raw + final_start,
8950 argptr->raw_length - final_start,
8951 argptr->newlines);
8952 argptr->raw[argptr->raw_length] = 0;
8953 if (argptr->raw_length > bufsize + extra)
8954 abort ();
8958 /* If we are not discarding this argument,
8959 macroexpand it and compute its length as stringified.
8960 All this info goes into *ARGPTR. */
8962 if (argptr != 0) {
8963 register const U_CHAR *buf, *lim;
8964 register int totlen;
8966 buf = argptr->raw;
8967 lim = buf + argptr->raw_length;
8969 while (buf != lim && is_space[*buf])
8970 buf++;
8971 while (buf != lim && is_space[lim[-1]])
8972 lim--;
8973 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
8974 while (buf != lim) {
8975 register U_CHAR c = *buf++;
8976 totlen++;
8977 /* Internal sequences of whitespace are replaced by one space
8978 in most cases, but not always. So count all the whitespace
8979 in case we need to keep it all. */
8980 #if 0
8981 if (is_space[c])
8982 SKIP_ALL_WHITE_SPACE (buf);
8983 else
8984 #endif
8985 if (c == '\"' || c == '\\' || c == '\n') /* escape these chars */
8986 totlen++;
8988 argptr->stringified_length_bound = totlen;
8990 return result;
8993 /* Scan text from START (inclusive) up to LIMIT (exclusive),
8994 taken from the expansion of MACRO,
8995 counting parens in *DEPTHPTR,
8996 and return if reach LIMIT
8997 or before a `)' that would make *DEPTHPTR negative
8998 or before a comma when *DEPTHPTR is zero.
8999 Single and double quotes are matched and termination
9000 is inhibited within them. Comments also inhibit it.
9001 Value returned is pointer to stopping place.
9003 Increment *NEWLINES each time a newline is passed.
9004 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
9005 Set *COMMENTS to 1 if a comment is seen. */
9007 static U_CHAR *
9008 macarg1 (start, limit, macro, depthptr, newlines, comments, rest_args)
9009 U_CHAR *start;
9010 register const U_CHAR *limit;
9011 struct hashnode *macro;
9012 int *depthptr, *newlines, *comments;
9013 int rest_args;
9015 register U_CHAR *bp = start;
9017 while (bp < limit) {
9018 switch (*bp) {
9019 case '(':
9020 (*depthptr)++;
9021 break;
9022 case ')':
9023 if (--(*depthptr) < 0)
9024 return bp;
9025 break;
9026 case '\\':
9027 /* Traditionally, backslash makes following char not special. */
9028 if (traditional && bp + 1 < limit && bp[1] != '\n')
9029 bp++;
9030 break;
9031 case '\n':
9032 ++*newlines;
9033 break;
9034 case '/':
9035 if (macro)
9036 break;
9037 if (bp[1] == '\\')
9038 newline_fix (bp + 1);
9039 if (bp[1] == '*') {
9040 *comments = 1;
9041 for (bp += 2; bp < limit; bp++) {
9042 if (*bp == '\n')
9043 ++*newlines;
9044 else if (*bp == '*') {
9045 if (bp[-1] == '/' && warn_comments)
9046 warning ("`/*' within comment");
9047 if (bp[1] == '\\')
9048 newline_fix (bp + 1);
9049 if (bp[1] == '/') {
9050 bp++;
9051 break;
9054 else
9056 #ifdef MULTIBYTE_CHARS
9057 int length;
9058 length = local_mblen (bp, limit - bp);
9059 if (length > 1)
9060 bp += (length - 1);
9061 #endif
9064 } else if (bp[1] == '/' && cplusplus_comments) {
9065 *comments = 1;
9066 for (bp += 2; bp < limit; bp++) {
9067 if (*bp == '\n') {
9068 ++*newlines;
9069 break;
9071 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
9073 ++*newlines;
9074 if (warn_comments)
9075 warning ("multiline `//' comment");
9076 ++bp;
9078 else
9080 #ifdef MULTIBYTE_CHARS
9081 int length;
9082 length = local_mblen (bp, limit - bp);
9083 if (length > 1)
9084 bp += (length - 1);
9085 #endif
9089 break;
9090 case '\'':
9091 case '\"':
9093 int quotec;
9094 for (quotec = *bp++; bp < limit && *bp != quotec; bp++) {
9095 if (*bp == '\\') {
9096 bp++;
9097 if (*bp == '\n')
9098 ++*newlines;
9099 while (*bp == '\\' && bp[1] == '\n') {
9100 bp += 2;
9101 ++*newlines;
9103 } else if (*bp == '\n') {
9104 if (warn_white_space && is_hor_space[bp[-1]] && ! macro)
9105 warning ("white space at end of line in string");
9106 ++*newlines;
9107 if (quotec == '\'')
9108 break;
9110 else
9112 #ifdef MULTIBYTE_CHARS
9113 int length;
9114 length = local_mblen (bp, limit - bp);
9115 if (length > 1)
9116 bp += (length - 1);
9117 #endif
9121 break;
9122 case ',':
9123 /* if we've returned to lowest level and we aren't absorbing all args */
9124 if ((*depthptr) == 0 && rest_args == 0)
9125 return bp;
9126 break;
9128 bp++;
9131 return bp;
9134 /* Discard comments and duplicate newlines
9135 in the string of length LENGTH at START,
9136 except inside of string constants.
9137 The string is copied into itself with its beginning staying fixed.
9139 NEWLINES is the number of newlines that must be duplicated.
9140 We assume that that much extra space is available past the end
9141 of the string. */
9143 static int
9144 discard_comments (start, length, newlines)
9145 U_CHAR *start;
9146 int length;
9147 int newlines;
9149 register U_CHAR *ibp;
9150 register U_CHAR *obp;
9151 register U_CHAR *limit;
9152 register int c;
9154 /* If we have newlines to duplicate, copy everything
9155 that many characters up. Then, in the second part,
9156 we will have room to insert the newlines
9157 while copying down.
9158 NEWLINES may actually be too large, because it counts
9159 newlines in string constants, and we don't duplicate those.
9160 But that does no harm. */
9161 if (newlines > 0) {
9162 ibp = start + length;
9163 obp = ibp + newlines;
9164 limit = start;
9165 while (limit != ibp)
9166 *--obp = *--ibp;
9169 ibp = start + newlines;
9170 limit = start + length + newlines;
9171 obp = start;
9173 while (ibp < limit) {
9174 *obp++ = c = *ibp++;
9175 switch (c) {
9176 case '\n':
9177 /* Duplicate the newline. */
9178 *obp++ = '\n';
9179 break;
9181 case '\\':
9182 if (*ibp == '\n') {
9183 obp--;
9184 ibp++;
9186 break;
9188 case '/':
9189 if (*ibp == '\\')
9190 newline_fix (ibp);
9191 /* Delete any comment. */
9192 if (cplusplus_comments && ibp[0] == '/') {
9193 /* Comments are equivalent to spaces. */
9194 obp[-1] = ' ';
9195 ibp++;
9196 while (ibp < limit)
9198 if (*ibp == '\n')
9199 break;
9200 if (*ibp == '\\' && ibp + 1 < limit && ibp[1] == '\n')
9201 ibp++;
9202 else
9204 #ifdef MULTIBYTE_CHARS
9205 int length = local_mblen (ibp, limit - ibp);
9206 if (length > 1)
9207 ibp += (length - 1);
9208 #endif
9210 ibp++;
9212 break;
9214 if (ibp[0] != '*' || ibp + 1 >= limit)
9215 break;
9216 /* Comments are equivalent to spaces.
9217 For -traditional, a comment is equivalent to nothing. */
9218 if (traditional)
9219 obp--;
9220 else
9221 obp[-1] = ' ';
9222 while (++ibp < limit) {
9223 if (ibp[0] == '*') {
9224 if (ibp[1] == '\\')
9225 newline_fix (ibp + 1);
9226 if (ibp[1] == '/') {
9227 ibp += 2;
9228 break;
9231 else
9233 #ifdef MULTIBYTE_CHARS
9234 int length = local_mblen (ibp, limit - ibp);
9235 if (length > 1)
9236 ibp += (length - 1);
9237 #endif
9240 break;
9242 case '\'':
9243 case '\"':
9244 /* Notice and skip strings, so that we don't
9245 think that comments start inside them,
9246 and so we don't duplicate newlines in them. */
9248 int quotec = c;
9249 while (ibp < limit) {
9250 *obp++ = c = *ibp++;
9251 if (c == quotec)
9252 break;
9253 if (c == '\n')
9255 if (quotec == '\'')
9256 break;
9258 else if (c == '\\') {
9259 if (ibp < limit && *ibp == '\n') {
9260 ibp++;
9261 obp--;
9262 } else {
9263 while (*ibp == '\\' && ibp[1] == '\n')
9264 ibp += 2;
9265 if (ibp < limit)
9266 *obp++ = *ibp++;
9269 else
9271 #ifdef MULTIBYTE_CHARS
9272 int length;
9273 ibp--;
9274 length = local_mblen (ibp, limit - ibp);
9275 if (length > 1)
9277 obp--;
9278 bcopy (ibp, obp, length);
9279 ibp += length;
9280 obp += length;
9282 else
9283 ibp++;
9284 #endif
9288 break;
9292 return obp - start;
9295 /* Turn newlines to spaces in the macro argument ARG.
9296 Remove backslash-newline from string constants,
9297 and turn other newlines in string constants to backslash-'n'. */
9299 static void
9300 change_newlines (arg)
9301 struct argdata *arg;
9303 U_CHAR *start = arg->expanded;
9304 int length = arg->expand_length;
9305 register U_CHAR *ibp;
9306 register U_CHAR *obp;
9307 register const U_CHAR *limit;
9308 register int c;
9310 ibp = start;
9311 limit = start + length;
9312 obp = start;
9314 while (ibp < limit) {
9315 *obp++ = c = *ibp++;
9316 switch (c) {
9317 case '\n':
9318 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9319 string. Skip past the newline and its duplicate.
9320 Put a space in the output. */
9321 if (*ibp == '\n')
9323 ibp++;
9324 obp--;
9325 *obp++ = ' ';
9327 break;
9329 case '\'':
9330 case '\"':
9331 /* Notice and skip strings, so that we don't delete newlines in them. */
9333 int quotec = c;
9334 while (ibp < limit) {
9335 *obp++ = c = *ibp++;
9336 if (c == quotec)
9337 break;
9338 else if (c == '\\' && ibp < limit && *ibp == '\n')
9339 *obp++ = *ibp++;
9340 else if (c == '\n')
9342 if (quotec == '\'')
9343 break;
9345 else
9347 #ifdef MULTIBYTE_CHARS
9348 int length;
9349 ibp--;
9350 length = local_mblen (ibp, limit - ibp);
9351 if (length > 1)
9353 obp--;
9354 bcopy (ibp, obp, length);
9355 ibp += length;
9356 obp += length;
9358 else
9359 ibp++;
9360 #endif
9364 break;
9368 arg->expand_length = obp - arg->expanded;
9370 if (start != arg->expanded)
9371 free (start);
9374 /* notice - output message to stderr */
9376 static void
9377 notice VPROTO ((const char * msgid, ...))
9379 #ifndef ANSI_PROTOTYPES
9380 const char * msgid;
9381 #endif
9382 va_list args;
9384 VA_START (args, msgid);
9386 #ifndef ANSI_PROTOTYPES
9387 msgid = va_arg (args, const char *);
9388 #endif
9390 vnotice (msgid, args);
9391 va_end (args);
9394 static void
9395 vnotice (msgid, args)
9396 const char *msgid;
9397 va_list args;
9399 vfprintf (stderr, _(msgid), args);
9402 /* error - print error message and increment count of errors. */
9404 void
9405 error VPROTO ((const char * msgid, ...))
9407 #ifndef ANSI_PROTOTYPES
9408 const char * msgid;
9409 #endif
9410 va_list args;
9412 VA_START (args, msgid);
9414 #ifndef ANSI_PROTOTYPES
9415 msgid = va_arg (args, const char *);
9416 #endif
9418 verror (msgid, args);
9419 va_end (args);
9422 void
9423 verror (msgid, args)
9424 const char *msgid;
9425 va_list args;
9427 int i;
9428 FILE_BUF *ip = NULL;
9430 print_containing_files ();
9432 for (i = indepth; i >= 0; i--)
9433 if (instack[i].fname != NULL) {
9434 ip = &instack[i];
9435 break;
9438 if (ip != NULL) {
9439 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9440 ip->nominal_fname_len, stderr);
9441 fprintf (stderr, ":%d: ", ip->lineno);
9443 vnotice (msgid, args);
9444 fprintf (stderr, "\n");
9445 errors++;
9448 /* Error including a message from `errno'. */
9450 static void
9451 error_from_errno (name)
9452 const char *name;
9454 int e = errno;
9455 int i;
9456 FILE_BUF *ip = NULL;
9458 print_containing_files ();
9460 for (i = indepth; i >= 0; i--)
9461 if (instack[i].fname != NULL) {
9462 ip = &instack[i];
9463 break;
9466 if (ip != NULL) {
9467 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9468 ip->nominal_fname_len, stderr);
9469 fprintf (stderr, ":%d: ", ip->lineno);
9472 fprintf (stderr, "%s: %s\n", name, xstrerror (e));
9474 errors++;
9477 /* Print error message but don't count it. */
9479 void
9480 warning VPROTO ((const char * msgid, ...))
9482 #ifndef ANSI_PROTOTYPES
9483 const char * msgid;
9484 #endif
9485 va_list args;
9487 VA_START (args, msgid);
9489 #ifndef ANSI_PROTOTYPES
9490 msgid = va_arg (args, const char *);
9491 #endif
9493 vwarning (msgid, args);
9494 va_end (args);
9497 static void
9498 vwarning (msgid, args)
9499 const char *msgid;
9500 va_list args;
9502 int i;
9503 FILE_BUF *ip = NULL;
9505 if (inhibit_warnings)
9506 return;
9508 if (warnings_are_errors)
9509 errors++;
9511 print_containing_files ();
9513 for (i = indepth; i >= 0; i--)
9514 if (instack[i].fname != NULL) {
9515 ip = &instack[i];
9516 break;
9519 if (ip != NULL) {
9520 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9521 ip->nominal_fname_len, stderr);
9522 fprintf (stderr, ":%d: ", ip->lineno);
9524 notice ("warning: ");
9525 vnotice (msgid, args);
9526 fprintf (stderr, "\n");
9529 static void
9530 error_with_line VPROTO ((int line, const char * msgid, ...))
9532 #ifndef ANSI_PROTOTYPES
9533 int line;
9534 const char * msgid;
9535 #endif
9536 va_list args;
9538 VA_START (args, msgid);
9540 #ifndef ANSI_PROTOTYPES
9541 line = va_arg (args, int);
9542 msgid = va_arg (args, const char *);
9543 #endif
9545 verror_with_line (line, msgid, args);
9546 va_end (args);
9550 static void
9551 verror_with_line (line, msgid, args)
9552 int line;
9553 const char *msgid;
9554 va_list args;
9556 int i;
9557 FILE_BUF *ip = NULL;
9559 print_containing_files ();
9561 for (i = indepth; i >= 0; i--)
9562 if (instack[i].fname != NULL) {
9563 ip = &instack[i];
9564 break;
9567 if (ip != NULL) {
9568 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9569 ip->nominal_fname_len, stderr);
9570 fprintf (stderr, ":%d: ", line);
9572 vnotice (msgid, args);
9573 fprintf (stderr, "\n");
9574 errors++;
9577 static void
9578 warning_with_line VPROTO ((int line, const char * msgid, ...))
9580 #ifndef ANSI_PROTOTYPES
9581 int line;
9582 const char * msgid;
9583 #endif
9584 va_list args;
9586 VA_START (args, msgid);
9588 #ifndef ANSI_PROTOTYPES
9589 line = va_arg (args, int);
9590 msgid = va_arg (args, const char *);
9591 #endif
9593 vwarning_with_line (line, msgid, args);
9594 va_end (args);
9597 static void
9598 vwarning_with_line (line, msgid, args)
9599 int line;
9600 const char *msgid;
9601 va_list args;
9603 int i;
9604 FILE_BUF *ip = NULL;
9606 if (inhibit_warnings)
9607 return;
9609 if (warnings_are_errors)
9610 errors++;
9612 print_containing_files ();
9614 for (i = indepth; i >= 0; i--)
9615 if (instack[i].fname != NULL) {
9616 ip = &instack[i];
9617 break;
9620 if (ip != NULL) {
9621 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9622 ip->nominal_fname_len, stderr);
9623 fprintf (stderr, line ? ":%d: " : ": ", line);
9625 notice ("warning: ");
9626 vnotice (msgid, args);
9627 fprintf (stderr, "\n");
9630 /* Print an error message and maybe count it. */
9632 void
9633 pedwarn VPROTO ((const char * msgid, ...))
9635 #ifndef ANSI_PROTOTYPES
9636 const char * msgid;
9637 #endif
9638 va_list args;
9640 VA_START (args, msgid);
9642 #ifndef ANSI_PROTOTYPES
9643 msgid = va_arg (args, const char *);
9644 #endif
9646 if (pedantic_errors)
9647 verror (msgid, args);
9648 else
9649 vwarning (msgid, args);
9650 va_end (args);
9653 void
9654 pedwarn_with_line VPROTO ((int line, const char * msgid, ...))
9656 #ifndef ANSI_PROTOTYPES
9657 int line;
9658 const char * msgid;
9659 #endif
9660 va_list args;
9662 VA_START (args, msgid);
9664 #ifndef ANSI_PROTOTYPES
9665 line = va_arg (args, int);
9666 msgid = va_arg (args, const char *);
9667 #endif
9669 if (pedantic_errors)
9670 verror_with_line (line, msgid, args);
9671 else
9672 vwarning_with_line (line, msgid, args);
9673 va_end (args);
9676 /* Report a warning (or an error if pedantic_errors)
9677 giving specified file name and line number, not current. */
9679 static void
9680 pedwarn_with_file_and_line VPROTO ((const char *file, size_t file_len, int line,
9681 const char * msgid, ...))
9683 #ifndef ANSI_PROTOTYPES
9684 const char *file;
9685 size_t file_len;
9686 int line;
9687 const char * msgid;
9688 #endif
9689 va_list args;
9691 if (!pedantic_errors && inhibit_warnings)
9692 return;
9694 VA_START (args, msgid);
9696 #ifndef ANSI_PROTOTYPES
9697 file = va_arg (args, const char *);
9698 file_len = va_arg (args, size_t);
9699 line = va_arg (args, int);
9700 msgid = va_arg (args, const char *);
9701 #endif
9703 if (file) {
9704 fwrite (file, sizeof file[0], file_len, stderr);
9705 fprintf (stderr, ":%d: ", line);
9707 if (pedantic_errors)
9708 errors++;
9709 if (!pedantic_errors)
9710 notice ("warning: ");
9711 vnotice (msgid, args);
9712 va_end (args);
9713 fprintf (stderr, "\n");
9716 static void
9717 pedwarn_strange_white_space (ch)
9718 int ch;
9720 switch (ch)
9722 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9723 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9724 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9725 default: abort ();
9729 /* Print the file names and line numbers of the #include
9730 directives which led to the current file. */
9732 static void
9733 print_containing_files ()
9735 FILE_BUF *ip = NULL;
9736 int i;
9737 int first = 1;
9739 /* If stack of files hasn't changed since we last printed
9740 this info, don't repeat it. */
9741 if (last_error_tick == input_file_stack_tick)
9742 return;
9744 for (i = indepth; i >= 0; i--)
9745 if (instack[i].fname != NULL) {
9746 ip = &instack[i];
9747 break;
9750 /* Give up if we don't find a source file. */
9751 if (ip == NULL)
9752 return;
9754 /* Find the other, outer source files. */
9755 for (i--; i >= 0; i--)
9756 if (instack[i].fname != NULL) {
9757 ip = &instack[i];
9758 if (first) {
9759 first = 0;
9760 notice ( "In file included from ");
9761 } else {
9762 notice (",\n from ");
9765 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9766 ip->nominal_fname_len, stderr);
9767 fprintf (stderr, ":%d", ip->lineno);
9769 if (! first)
9770 fprintf (stderr, ":\n");
9772 /* Record we have printed the status as of this time. */
9773 last_error_tick = input_file_stack_tick;
9776 /* Return the line at which an error occurred.
9777 The error is not necessarily associated with the current spot
9778 in the input stack, so LINE says where. LINE will have been
9779 copied from ip->lineno for the current input level.
9780 If the current level is for a file, we return LINE.
9781 But if the current level is not for a file, LINE is meaningless.
9782 In that case, we return the lineno of the innermost file. */
9784 static int
9785 line_for_error (line)
9786 int line;
9788 int i;
9789 int line1 = line;
9791 for (i = indepth; i >= 0; ) {
9792 if (instack[i].fname != 0)
9793 return line1;
9794 i--;
9795 if (i < 0)
9796 return 0;
9797 line1 = instack[i].lineno;
9799 abort ();
9800 /*NOTREACHED*/
9801 return 0;
9805 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9807 * As things stand, nothing is ever placed in the output buffer to be
9808 * removed again except when it's KNOWN to be part of an identifier,
9809 * so flushing and moving down everything left, instead of expanding,
9810 * should work ok.
9813 /* You might think void was cleaner for the return type,
9814 but that would get type mismatch in check_expand in strict ANSI. */
9816 static int
9817 grow_outbuf (obuf, needed)
9818 register FILE_BUF *obuf;
9819 register int needed;
9821 register U_CHAR *p;
9822 int minsize;
9824 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
9825 return 0;
9827 /* Make it at least twice as big as it is now. */
9828 obuf->length *= 2;
9829 /* Make it have at least 150% of the free space we will need. */
9830 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
9831 if (minsize > obuf->length)
9832 obuf->length = minsize;
9834 p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
9836 obuf->bufp = p + (obuf->bufp - obuf->buf);
9837 obuf->buf = p;
9839 return 0;
9842 /* Symbol table for macro names and special symbols */
9845 * install a name in the main hash table, even if it is already there.
9846 * name stops with first non alphanumeric, except leading '#'.
9847 * caller must check against redefinition if that is desired.
9848 * delete_macro () removes things installed by install () in fifo order.
9849 * this is important because of the `defined' special symbol used
9850 * in #if, and also if pushdef/popdef directives are ever implemented.
9852 * If LEN is >= 0, it is the length of the name.
9853 * Otherwise, compute the length by scanning the entire name.
9855 * If HASH is >= 0, it is the precomputed hash code.
9856 * Otherwise, compute the hash code.
9859 static HASHNODE *
9860 install (name, len, type, value, hash)
9861 const U_CHAR *name;
9862 int len;
9863 enum node_type type;
9864 const char *value;
9865 int hash;
9867 register HASHNODE *hp;
9868 register int i, bucket;
9869 register U_CHAR *p;
9870 register const U_CHAR *q;
9872 if (len < 0) {
9873 q = name;
9874 while (is_idchar[*q])
9875 q++;
9876 len = q - name;
9879 if (hash < 0)
9880 hash = hashf (name, len, HASHSIZE);
9882 i = sizeof (HASHNODE) + len + 1;
9883 hp = (HASHNODE *) xmalloc (i);
9884 bucket = hash;
9885 hp->bucket_hdr = &hashtab[bucket];
9886 hp->next = hashtab[bucket];
9887 hashtab[bucket] = hp;
9888 hp->prev = NULL;
9889 if (hp->next != NULL)
9890 hp->next->prev = hp;
9891 hp->type = type;
9892 hp->length = len;
9893 hp->value.cpval = value;
9894 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
9895 p = hp->name;
9896 q = name;
9897 for (i = 0; i < len; i++)
9898 *p++ = *q++;
9899 hp->name[len] = 0;
9900 return hp;
9904 * find the most recent hash node for name "name" (ending with first
9905 * non-identifier char) installed by install
9907 * If LEN is >= 0, it is the length of the name.
9908 * Otherwise, compute the length by scanning the entire name.
9910 * If HASH is >= 0, it is the precomputed hash code.
9911 * Otherwise, compute the hash code.
9914 HASHNODE *
9915 lookup (name, len, hash)
9916 const U_CHAR *name;
9917 int len;
9918 int hash;
9920 register const U_CHAR *bp;
9921 register HASHNODE *bucket;
9923 if (len < 0) {
9924 for (bp = name; is_idchar[*bp]; bp++) ;
9925 len = bp - name;
9928 if (hash < 0)
9929 hash = hashf (name, len, HASHSIZE);
9931 bucket = hashtab[hash];
9932 while (bucket) {
9933 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
9934 return bucket;
9935 bucket = bucket->next;
9937 return NULL;
9941 * Delete a hash node. Some weirdness to free junk from macros.
9942 * More such weirdness will have to be added if you define more hash
9943 * types that need it.
9946 /* Note that the DEFINITION of a macro is removed from the hash table
9947 but its storage is not freed. This would be a storage leak
9948 except that it is not reasonable to keep undefining and redefining
9949 large numbers of macros many times.
9950 In any case, this is necessary, because a macro can be #undef'd
9951 in the middle of reading the arguments to a call to it.
9952 If #undef freed the DEFINITION, that would crash. */
9954 static void
9955 delete_macro (hp)
9956 HASHNODE *hp;
9959 if (hp->prev != NULL)
9960 hp->prev->next = hp->next;
9961 if (hp->next != NULL)
9962 hp->next->prev = hp->prev;
9964 /* Make sure that the bucket chain header that the deleted guy was
9965 on points to the right thing afterwards. */
9966 if (hp == *hp->bucket_hdr)
9967 *hp->bucket_hdr = hp->next;
9969 #if 0
9970 if (hp->type == T_MACRO) {
9971 DEFINITION *d = hp->value.defn;
9972 struct reflist *ap, *nextap;
9974 for (ap = d->pattern; ap != NULL; ap = nextap) {
9975 nextap = ap->next;
9976 free (ap);
9978 free (d);
9980 #endif
9981 free (hp);
9985 * return hash function on name. must be compatible with the one
9986 * computed a step at a time, elsewhere
9989 static int
9990 hashf (name, len, hashsize)
9991 register const U_CHAR *name;
9992 register int len;
9993 int hashsize;
9995 register int r = 0;
9997 while (len--)
9998 r = HASHSTEP (r, *name++);
10000 return MAKE_POS (r) % hashsize;
10004 /* Dump the definition of a single macro HP to OF. */
10006 static void
10007 dump_single_macro (hp, of)
10008 register HASHNODE *hp;
10009 FILE *of;
10011 register DEFINITION *defn = hp->value.defn;
10012 struct reflist *ap;
10013 int offset;
10014 int concat;
10017 /* Print the definition of the macro HP. */
10019 fprintf (of, "#define %s", hp->name);
10021 if (defn->nargs >= 0) {
10022 int i;
10024 fprintf (of, "(");
10025 for (i = 0; i < defn->nargs; i++) {
10026 dump_arg_n (defn, i, of);
10027 if (i + 1 < defn->nargs)
10028 fprintf (of, ", ");
10030 fprintf (of, ")");
10033 fprintf (of, " ");
10035 offset = 0;
10036 concat = 0;
10037 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
10038 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
10039 offset += ap->nchars;
10040 if (!traditional) {
10041 if (ap->nchars != 0)
10042 concat = 0;
10043 if (ap->stringify) {
10044 switch (ap->stringify) {
10045 case SHARP_TOKEN: fprintf (of, "#"); break;
10046 case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
10047 case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
10048 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
10049 default: abort ();
10052 if (ap->raw_before != 0) {
10053 if (concat) {
10054 switch (ap->raw_before) {
10055 case WHITE_SHARP_TOKEN:
10056 case WHITE_PERCENT_COLON_TOKEN:
10057 fprintf (of, " ");
10058 break;
10059 default:
10060 break;
10062 } else {
10063 switch (ap->raw_before) {
10064 case SHARP_TOKEN: fprintf (of, "##"); break;
10065 case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
10066 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10067 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
10068 default: abort ();
10072 concat = 0;
10074 dump_arg_n (defn, ap->argno, of);
10075 if (!traditional && ap->raw_after != 0) {
10076 switch (ap->raw_after) {
10077 case SHARP_TOKEN: fprintf (of, "##"); break;
10078 case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
10079 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10080 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
10081 default: abort ();
10083 concat = 1;
10086 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
10087 fprintf (of, "\n");
10090 /* Dump all macro definitions as #defines to stdout. */
10092 static void
10093 dump_all_macros ()
10095 int bucket;
10097 for (bucket = 0; bucket < HASHSIZE; bucket++) {
10098 register HASHNODE *hp;
10100 for (hp = hashtab[bucket]; hp; hp= hp->next) {
10101 if (hp->type == T_MACRO)
10102 dump_single_macro (hp, stdout);
10107 /* Output to OF a substring of a macro definition.
10108 BASE is the beginning of the definition.
10109 Output characters START thru LENGTH.
10110 Unless traditional, discard newlines outside of strings, thus
10111 converting funny-space markers to ordinary spaces. */
10113 static void
10114 dump_defn_1 (base, start, length, of)
10115 const U_CHAR *base;
10116 int start;
10117 int length;
10118 FILE *of;
10120 const U_CHAR *p = base + start;
10121 const U_CHAR *limit = base + start + length;
10123 if (traditional)
10124 fwrite (p, sizeof (*p), length, of);
10125 else {
10126 while (p < limit) {
10127 if (*p == '\"' || *p =='\'') {
10128 const U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
10129 NULL_PTR, NULL_PTR);
10130 fwrite (p, sizeof (*p), p1 - p, of);
10131 p = p1;
10132 } else {
10133 if (*p != '\n')
10134 putc (*p, of);
10135 p++;
10141 /* Print the name of argument number ARGNUM of macro definition DEFN
10142 to OF.
10143 Recall that DEFN->args.argnames contains all the arg names
10144 concatenated in reverse order with comma-space in between. */
10146 static void
10147 dump_arg_n (defn, argnum, of)
10148 DEFINITION *defn;
10149 int argnum;
10150 FILE *of;
10152 register U_CHAR *p = defn->args.argnames;
10153 while (argnum + 1 < defn->nargs) {
10154 p = (U_CHAR *) index ((char *) p, ' ') + 1;
10155 argnum++;
10158 while (*p && *p != ',') {
10159 putc (*p, of);
10160 p++;
10164 /* Initialize syntactic classifications of characters. */
10166 static void
10167 initialize_char_syntax ()
10169 register int i;
10172 * Set up is_idchar and is_idstart tables. These should be
10173 * faster than saying (is_alpha (c) || c == '_'), etc.
10174 * Set up these things before calling any routines tthat
10175 * refer to them.
10177 for (i = 'a'; i <= 'z'; i++) {
10178 is_idchar[TOUPPER(i)] = 1;
10179 is_idchar[i] = 1;
10180 is_idstart[TOUPPER(i)] = 1;
10181 is_idstart[i] = 1;
10183 for (i = '0'; i <= '9'; i++)
10184 is_idchar[i] = 1;
10185 is_idchar['_'] = 1;
10186 is_idstart['_'] = 1;
10187 is_idchar['$'] = 1;
10188 is_idstart['$'] = 1;
10190 /* horizontal space table */
10191 is_hor_space[' '] = 1;
10192 is_hor_space['\t'] = 1;
10193 is_hor_space['\v'] = 1;
10194 is_hor_space['\f'] = 1;
10195 is_hor_space['\r'] = 1;
10197 is_space[' '] = 1;
10198 is_space['\t'] = 1;
10199 is_space['\v'] = 1;
10200 is_space['\f'] = 1;
10201 is_space['\n'] = 1;
10202 is_space['\r'] = 1;
10205 /* Initialize the built-in macros. */
10207 static void
10208 initialize_builtins (inp, outp)
10209 FILE_BUF *inp;
10210 FILE_BUF *outp;
10212 install ((const U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
10213 install ((const U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
10214 install ((const U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
10215 install ((const U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
10216 install ((const U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
10217 install ((const U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
10218 #ifndef NO_BUILTIN_SIZE_TYPE
10219 install ((const U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
10220 #endif
10221 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10222 install ((const U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
10223 #endif
10224 install ((const U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
10225 install ((const U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
10226 NULL_PTR, -1);
10227 install ((const U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
10228 NULL_PTR, -1);
10229 install ((const U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
10230 NULL_PTR, -1);
10231 install ((const U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
10232 if (!traditional) {
10233 install ((const U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
10234 install ((const U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
10236 /* This is supplied using a -D by the compiler driver
10237 so that it is present only when truly compiling with GNU C. */
10238 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10239 install ((const U_CHAR *) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST, "1", -1);
10241 if (debug_output)
10243 char directive[2048];
10244 U_CHAR *udirective = (U_CHAR *) directive;
10245 register struct directive *dp = &directive_table[0];
10246 struct tm *timebuf = timestamp ();
10248 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
10249 instack[0].nominal_fname);
10250 output_line_directive (inp, outp, 0, same_file);
10251 pass_thru_directive (udirective, &udirective[strlen (directive)],
10252 outp, dp);
10254 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
10255 output_line_directive (inp, outp, 0, same_file);
10256 pass_thru_directive (udirective, &udirective[strlen (directive)],
10257 outp, dp);
10259 #ifndef NO_BUILTIN_SIZE_TYPE
10260 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
10261 output_line_directive (inp, outp, 0, same_file);
10262 pass_thru_directive (udirective, &udirective[strlen (directive)],
10263 outp, dp);
10264 #endif
10266 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10267 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
10268 output_line_directive (inp, outp, 0, same_file);
10269 pass_thru_directive (udirective, &udirective[strlen (directive)],
10270 outp, dp);
10271 #endif
10273 sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
10274 output_line_directive (inp, outp, 0, same_file);
10275 pass_thru_directive (udirective, &udirective[strlen (directive)],
10276 outp, dp);
10278 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
10279 monthnames[timebuf->tm_mon],
10280 timebuf->tm_mday, timebuf->tm_year + 1900);
10281 output_line_directive (inp, outp, 0, same_file);
10282 pass_thru_directive (udirective, &udirective[strlen (directive)],
10283 outp, dp);
10285 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
10286 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
10287 output_line_directive (inp, outp, 0, same_file);
10288 pass_thru_directive (udirective, &udirective[strlen (directive)],
10289 outp, dp);
10291 if (!traditional)
10293 sprintf (directive, " __STDC__ 1");
10294 output_line_directive (inp, outp, 0, same_file);
10295 pass_thru_directive (udirective, &udirective[strlen (directive)],
10296 outp, dp);
10298 if (objc)
10300 sprintf (directive, " __OBJC__ 1");
10301 output_line_directive (inp, outp, 0, same_file);
10302 pass_thru_directive (udirective, &udirective[strlen (directive)],
10303 outp, dp);
10309 * process a given definition string, for initialization
10310 * If STR is just an identifier, define it with value 1.
10311 * If STR has anything after the identifier, then it should
10312 * be identifier=definition.
10315 static void
10316 make_definition (str)
10317 char *str;
10319 FILE_BUF *ip;
10320 struct directive *kt;
10321 U_CHAR *buf, *p;
10323 p = buf = (U_CHAR *) str;
10324 if (!is_idstart[*p]) {
10325 error ("malformed option `-D %s'", str);
10326 return;
10328 while (is_idchar[*++p])
10330 if (*p == '(') {
10331 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
10333 if (*p++ != ')')
10334 p = (U_CHAR *) str; /* Error */
10336 if (*p == 0) {
10337 buf = (U_CHAR *) alloca (p - buf + 4);
10338 strcpy ((char *)buf, str);
10339 strcat ((char *)buf, " 1");
10340 } else if (*p != '=') {
10341 error ("malformed option `-D %s'", str);
10342 return;
10343 } else {
10344 U_CHAR *q;
10345 /* Copy the entire option so we can modify it. */
10346 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
10347 strncpy ((char *) buf, str, p - (U_CHAR *) str);
10348 /* Change the = to a space. */
10349 buf[p - (U_CHAR *) str] = ' ';
10350 /* Scan for any backslash-newline and remove it. */
10351 p++;
10352 q = &buf[p - (U_CHAR *) str];
10353 while (*p) {
10354 if (*p == '\"' || *p == '\'') {
10355 int unterminated = 0;
10356 const U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
10357 NULL_PTR, NULL_PTR, &unterminated);
10358 if (unterminated)
10359 return;
10360 while (p != p1) {
10361 if (*p == '\\' && p[1] == '\n')
10362 p += 2;
10363 else if (*p == '\n')
10365 *q++ = '\\';
10366 *q++ = 'n';
10367 p++;
10369 else
10370 *q++ = *p++;
10372 } else if (*p == '\\' && p[1] == '\n')
10373 p += 2;
10374 /* Change newline chars into newline-markers. */
10375 else if (*p == '\n')
10377 *q++ = '\n';
10378 *q++ = '\n';
10379 p++;
10381 else
10382 *q++ = *p++;
10384 *q = 0;
10387 ip = &instack[++indepth];
10388 ip->nominal_fname = ip->fname = "*Initialization*";
10389 ip->nominal_fname_len = strlen (ip->nominal_fname);
10391 ip->buf = ip->bufp = buf;
10392 ip->length = strlen ((char *) buf);
10393 ip->lineno = 1;
10394 ip->macro = 0;
10395 ip->free_ptr = 0;
10396 ip->if_stack = if_stack;
10397 ip->system_header_p = 0;
10399 for (kt = directive_table; kt->type != T_DEFINE; kt++)
10402 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10403 do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
10404 --indepth;
10407 /* JF, this does the work for the -U option */
10409 static void
10410 make_undef (str, op)
10411 char *str;
10412 FILE_BUF *op;
10414 FILE_BUF *ip;
10415 struct directive *kt;
10417 ip = &instack[++indepth];
10418 ip->nominal_fname = ip->fname = "*undef*";
10419 ip->nominal_fname_len = strlen (ip->nominal_fname);
10421 ip->buf = ip->bufp = (U_CHAR *) str;
10422 ip->length = strlen (str);
10423 ip->lineno = 1;
10424 ip->macro = 0;
10425 ip->free_ptr = 0;
10426 ip->if_stack = if_stack;
10427 ip->system_header_p = 0;
10429 for (kt = directive_table; kt->type != T_UNDEF; kt++)
10432 do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
10433 --indepth;
10436 /* Process the string STR as if it appeared as the body of a #assert.
10437 OPTION is the option name for which STR was the argument. */
10439 static void
10440 make_assertion (option, str)
10441 const char *option;
10442 const char *str;
10444 FILE_BUF *ip;
10445 struct directive *kt;
10446 U_CHAR *buf, *p, *q;
10448 /* Copy the entire option so we can modify it. */
10449 buf = (U_CHAR *) alloca (strlen (str) + 1);
10450 strcpy ((char *) buf, str);
10451 /* Scan for any backslash-newline and remove it. */
10452 p = q = buf;
10453 while (*p) {
10454 if (*p == '\\' && p[1] == '\n')
10455 p += 2;
10456 else
10457 *q++ = *p++;
10459 *q = 0;
10461 p = buf;
10462 if (!is_idstart[*p]) {
10463 error ("malformed option `%s %s'", option, str);
10464 return;
10466 while (is_idchar[*++p])
10468 SKIP_WHITE_SPACE (p);
10469 if (! (*p == 0 || *p == '(')) {
10470 error ("malformed option `%s %s'", option, str);
10471 return;
10474 ip = &instack[++indepth];
10475 ip->nominal_fname = ip->fname = "*Initialization*";
10476 ip->nominal_fname_len = strlen (ip->nominal_fname);
10478 ip->buf = ip->bufp = buf;
10479 ip->length = strlen ((char *) buf);
10480 ip->lineno = 1;
10481 ip->macro = 0;
10482 ip->free_ptr = 0;
10483 ip->if_stack = if_stack;
10484 ip->system_header_p = 0;
10486 for (kt = directive_table; kt->type != T_ASSERT; kt++)
10489 /* Pass NULL as output ptr to do_define since we KNOW it never does
10490 any output.... */
10491 do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
10492 --indepth;
10495 /* The previous include prefix, if any, is PREV_FILE_NAME.
10496 Translate any pathnames with COMPONENT.
10497 Allocate a new include prefix whose name is the
10498 simplified concatenation of PREFIX and NAME,
10499 with a trailing / added if needed.
10500 But return 0 if the include prefix should be ignored,
10501 e.g. because it is a duplicate of PREV_FILE_NAME. */
10503 static struct file_name_list *
10504 new_include_prefix (prev_file_name, component, prefix, name)
10505 struct file_name_list *prev_file_name;
10506 const char *component;
10507 const char *prefix;
10508 const char *name;
10510 if (name == 0)
10511 fatal ("Directory name missing after command line option");
10513 if (*name == 0)
10514 /* Ignore the empty string. */
10515 return 0;
10517 prefix = update_path (prefix, component);
10518 name = update_path (name, component);
10521 struct file_name_list *dir
10522 = ((struct file_name_list *)
10523 xmalloc (sizeof (struct file_name_list)
10524 + strlen (prefix) + strlen (name) + 2));
10525 size_t len;
10526 strcpy (dir->fname, prefix);
10527 strcat (dir->fname, name);
10528 len = simplify_filename (dir->fname);
10530 /* Convert directory name to a prefix. */
10531 if (len && dir->fname[len - 1] != DIR_SEPARATOR) {
10532 if (len == 1 && dir->fname[len - 1] == '.')
10533 len = 0;
10534 else
10535 #ifdef VMS
10536 /* must be '/', hack_vms_include_specification triggers on it. */
10537 dir->fname[len++] = '/';
10538 #else
10539 dir->fname[len++] = DIR_SEPARATOR;
10540 #endif
10541 dir->fname[len] = 0;
10544 /* Ignore a directory whose name matches the previous one. */
10545 if (prev_file_name && !strcmp (prev_file_name->fname, dir->fname)) {
10546 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10547 if (!first_bracket_include)
10548 first_bracket_include = prev_file_name;
10549 free (dir);
10550 return 0;
10553 #ifndef VMS
10554 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10556 /* Add a trailing "." if there is a filename. This increases the number
10557 of systems that can stat directories. We remove it below. */
10558 if (len != 0)
10560 dir->fname[len] = '.';
10561 dir->fname[len + 1] = 0;
10564 /* Ignore a nonexistent directory. */
10565 if (stat (len ? dir->fname : ".", &dir->st) != 0) {
10566 if (errno != ENOENT && errno != ENOTDIR)
10567 error_from_errno (dir->fname);
10568 free (dir);
10569 return 0;
10572 if (len != 0)
10573 dir->fname[len] = 0;
10575 /* Ignore a directory whose identity matches the previous one. */
10576 if (prev_file_name
10577 && INO_T_EQ (prev_file_name->st.st_ino, dir->st.st_ino)
10578 && prev_file_name->st.st_dev == dir->st.st_dev) {
10579 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10580 if (!first_bracket_include)
10581 first_bracket_include = prev_file_name;
10582 free (dir);
10583 return 0;
10585 #endif /* ! VMS */
10587 dir->next = 0;
10588 dir->c_system_include_path = 0;
10589 dir->got_name_map = 0;
10591 return dir;
10595 /* Append a chain of `struct file_name_list's
10596 to the end of the main include chain.
10597 FIRST is the beginning of the chain to append, and LAST is the end. */
10599 static void
10600 append_include_chain (first, last)
10601 struct file_name_list *first, *last;
10603 struct file_name_list *dir;
10605 if (!first || !last)
10606 return;
10608 if (include == 0)
10609 include = first;
10610 else
10611 last_include->next = first;
10613 if (first_bracket_include == 0)
10614 first_bracket_include = first;
10616 for (dir = first; ; dir = dir->next) {
10617 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
10618 if (len > max_include_len)
10619 max_include_len = len;
10620 if (dir == last)
10621 break;
10624 last->next = NULL;
10625 last_include = last;
10628 /* Place into DST a representation of the file named SRC that is suitable
10629 for `make'. Do not null-terminate DST. Return its length. */
10630 static int
10631 quote_string_for_make (dst, src)
10632 char *dst;
10633 const char *src;
10635 const char *p = src;
10636 int i = 0;
10637 for (;;)
10639 char c = *p++;
10640 switch (c)
10642 case '\0':
10643 case ' ':
10644 case '\t':
10646 /* GNU make uses a weird quoting scheme for white space.
10647 A space or tab preceded by 2N+1 backslashes represents
10648 N backslashes followed by space; a space or tab
10649 preceded by 2N backslashes represents N backslashes at
10650 the end of a file name; and backslashes in other
10651 contexts should not be doubled. */
10652 const char *q;
10653 for (q = p - 1; src < q && q[-1] == '\\'; q--)
10655 if (dst)
10656 dst[i] = '\\';
10657 i++;
10660 if (!c)
10661 return i;
10662 if (dst)
10663 dst[i] = '\\';
10664 i++;
10665 goto ordinary_char;
10667 case '$':
10668 if (dst)
10669 dst[i] = c;
10670 i++;
10671 /* Fall through. This can mishandle things like "$(" but
10672 there's no easy fix. */
10673 default:
10674 ordinary_char:
10675 /* This can mishandle characters in the string "\0\n%*?[\\~";
10676 exactly which chars are mishandled depends on the `make' version.
10677 We know of no portable solution for this;
10678 even GNU make 3.76.1 doesn't solve the problem entirely.
10679 (Also, '\0' is mishandled due to our calling conventions.) */
10680 if (dst)
10681 dst[i] = c;
10682 i++;
10683 break;
10689 /* Add output to `deps_buffer' for the -M switch.
10690 STRING points to the text to be output.
10691 SPACER is ':' for targets, ' ' for dependencies. */
10693 static void
10694 deps_output (string, spacer)
10695 const char *string;
10696 int spacer;
10698 int size = quote_string_for_make ((char *) 0, string);
10700 if (size == 0)
10701 return;
10703 #ifndef MAX_OUTPUT_COLUMNS
10704 #define MAX_OUTPUT_COLUMNS 72
10705 #endif
10706 if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
10707 && 1 < deps_column) {
10708 bcopy (" \\\n ", &deps_buffer[deps_size], 4);
10709 deps_size += 4;
10710 deps_column = 1;
10711 if (spacer == ' ')
10712 spacer = 0;
10715 if (deps_size + 2 * size + 8 > deps_allocated_size) {
10716 deps_allocated_size = (deps_size + 2 * size + 50) * 2;
10717 deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
10719 if (spacer == ' ') {
10720 deps_buffer[deps_size++] = ' ';
10721 deps_column++;
10723 quote_string_for_make (&deps_buffer[deps_size], string);
10724 deps_size += size;
10725 deps_column += size;
10726 if (spacer == ':') {
10727 deps_buffer[deps_size++] = ':';
10728 deps_column++;
10730 deps_buffer[deps_size] = 0;
10733 void
10734 fatal VPROTO ((const char * msgid, ...))
10736 #ifndef ANSI_PROTOTYPES
10737 const char * msgid;
10738 #endif
10739 va_list args;
10741 fprintf (stderr, "%s: ", progname);
10742 VA_START (args, msgid);
10744 #ifndef ANSI_PROTOTYPES
10745 msgid = va_arg (args, const char *);
10746 #endif
10747 vnotice (msgid, args);
10748 va_end (args);
10749 fprintf (stderr, "\n");
10750 exit (FATAL_EXIT_CODE);
10753 /* More 'friendly' abort that prints the line and file.
10754 config.h can #define abort fancy_abort if you like that sort of thing. */
10756 void
10757 fancy_abort ()
10759 fatal ("Internal gcc abort.");
10762 static void
10763 perror_with_name (name)
10764 const char *name;
10766 fprintf (stderr, "%s: %s: %s\n", progname, name, xstrerror (errno));
10767 errors++;
10770 static void
10771 pfatal_with_name (name)
10772 const char *name;
10774 perror_with_name (name);
10775 #ifdef VMS
10776 exit (vaxc$errno);
10777 #else
10778 exit (FATAL_EXIT_CODE);
10779 #endif
10782 /* Handler for SIGPIPE. */
10784 static void
10785 pipe_closed (signo)
10786 /* If this is missing, some compilers complain. */
10787 int signo ATTRIBUTE_UNUSED;
10789 fatal ("output pipe has been closed");
10792 static void
10793 memory_full ()
10795 fatal ("Memory exhausted.");
10798 #ifdef VMS
10800 /* Under VMS we need to fix up the "include" specification filename.
10802 Rules for possible conversions
10804 fullname tried paths
10806 name name
10807 ./dir/name [.dir]name
10808 /dir/name dir:name
10809 /name [000000]name, name
10810 dir/name dir:[000000]name, dir:name, dir/name
10811 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10812 path:/name path:[000000]name, path:name
10813 path:/dir/name path:[000000.dir]name, path:[dir]name
10814 path:dir/name path:[dir]name
10815 [path]:[dir]name [path.dir]name
10816 path/[dir]name [path.dir]name
10818 The path:/name input is constructed when expanding <> includes.
10820 return 1 if name was changed, 0 else. */
10822 static int
10823 hack_vms_include_specification (fullname, vaxc_include)
10824 char *fullname;
10825 int vaxc_include;
10827 register char *basename, *unixname, *local_ptr, *first_slash;
10828 int f, check_filename_before_returning, must_revert;
10829 char Local[512];
10831 check_filename_before_returning = 0;
10832 must_revert = 0;
10833 /* See if we can find a 1st slash. If not, there's no path information. */
10834 first_slash = index (fullname, '/');
10835 if (first_slash == 0)
10836 return 0; /* Nothing to do!!! */
10838 /* construct device spec if none given. */
10840 if (index (fullname, ':') == 0)
10843 /* If fullname has a slash, take it as device spec. */
10845 if (first_slash == fullname)
10847 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
10848 if (first_slash)
10849 *first_slash = ':'; /* make device spec */
10850 for (basename = fullname; *basename != 0; basename++)
10851 *basename = *(basename+1); /* remove leading slash */
10853 else if ((first_slash[-1] != '.') /* keep ':/', './' */
10854 && (first_slash[-1] != ':')
10855 && (first_slash[-1] != ']')) /* or a vms path */
10857 *first_slash = ':';
10859 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
10860 && (first_slash[-1] == '.'))
10861 fullname += 2;
10864 /* Get part after first ':' (basename[-1] == ':')
10865 or last '/' (basename[-1] == '/'). */
10867 basename = base_name (fullname);
10870 * Check if we have a vax-c style '#include filename'
10871 * and add the missing .h
10874 if (vaxc_include && !index (basename,'.'))
10875 strcat (basename, ".h");
10877 local_ptr = Local; /* initialize */
10879 /* We are trying to do a number of things here. First of all, we are
10880 trying to hammer the filenames into a standard format, such that later
10881 processing can handle them.
10883 If the file name contains something like [dir.], then it recognizes this
10884 as a root, and strips the ".]". Later processing will add whatever is
10885 needed to get things working properly.
10887 If no device is specified, then the first directory name is taken to be
10888 a device name (or a rooted logical). */
10890 /* Point to the UNIX filename part (which needs to be fixed!)
10891 but skip vms path information.
10892 [basename != fullname since first_slash != 0]. */
10894 if ((basename[-1] == ':') /* vms path spec. */
10895 || (basename[-1] == ']')
10896 || (basename[-1] == '>'))
10897 unixname = basename;
10898 else
10899 unixname = fullname;
10901 if (*unixname == '/')
10902 unixname++;
10904 /* If the directory spec is not rooted, we can just copy
10905 the UNIX filename part and we are done. */
10907 if (((basename - fullname) > 1)
10908 && ( (basename[-1] == ']')
10909 || (basename[-1] == '>')))
10911 if (basename[-2] != '.')
10914 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10915 -> PATH]:/name (basename = '/name', unixname = 'name')
10916 We strip the `]', and then splice the two parts of the name in the
10917 usual way. Given the default locations for include files in cccp.c,
10918 we will only use this code if the user specifies alternate locations
10919 with the /include (-I) switch on the command line. */
10921 basename -= 1; /* Strip "]" */
10922 unixname--; /* backspace */
10924 else
10927 /* The VMS part has a ".]" at the end, and this will not do. Later
10928 processing will add a second directory spec, and this would be a syntax
10929 error. Thus we strip the ".]", and thus merge the directory specs.
10930 We also backspace unixname, so that it points to a '/'. This inhibits the
10931 generation of the 000000 root directory spec (which does not belong here
10932 in this case). */
10934 basename -= 2; /* Strip ".]" */
10935 unixname--; /* backspace */
10939 else
10943 /* We drop in here if there is no VMS style directory specification yet.
10944 If there is no device specification either, we make the first dir a
10945 device and try that. If we do not do this, then we will be essentially
10946 searching the users default directory (as if they did a #include "asdf.h").
10948 Then all we need to do is to push a '[' into the output string. Later
10949 processing will fill this in, and close the bracket. */
10951 if ((unixname != fullname) /* vms path spec found. */
10952 && (basename[-1] != ':'))
10953 *local_ptr++ = ':'; /* dev not in spec. take first dir */
10955 *local_ptr++ = '['; /* Open the directory specification */
10958 if (unixname == fullname) /* no vms dir spec. */
10960 must_revert = 1;
10961 if ((first_slash != 0) /* unix dir spec. */
10962 && (*unixname != '/') /* not beginning with '/' */
10963 && (*unixname != '.')) /* or './' or '../' */
10964 *local_ptr++ = '.'; /* dir is local ! */
10967 /* at this point we assume that we have the device spec, and (at least
10968 the opening "[" for a directory specification. We may have directories
10969 specified already.
10971 If there are no other slashes then the filename will be
10972 in the "root" directory. Otherwise, we need to add
10973 directory specifications. */
10975 if (index (unixname, '/') == 0)
10977 /* if no directories specified yet and none are following. */
10978 if (local_ptr[-1] == '[')
10980 /* Just add "000000]" as the directory string */
10981 strcpy (local_ptr, "000000]");
10982 local_ptr += strlen (local_ptr);
10983 check_filename_before_returning = 1; /* we might need to fool with this later */
10986 else
10989 /* As long as there are still subdirectories to add, do them. */
10990 while (index (unixname, '/') != 0)
10992 /* If this token is "." we can ignore it
10993 if it's not at the beginning of a path. */
10994 if ((unixname[0] == '.') && (unixname[1] == '/'))
10996 /* remove it at beginning of path. */
10997 if ( ((unixname == fullname) /* no device spec */
10998 && (fullname+2 != basename)) /* starts with ./ */
10999 /* or */
11000 || ((basename[-1] == ':') /* device spec */
11001 && (unixname-1 == basename))) /* and ./ afterwards */
11002 *local_ptr++ = '.'; /* make '[.' start of path. */
11003 unixname += 2;
11004 continue;
11007 /* Add a subdirectory spec. Do not duplicate "." */
11008 if ( local_ptr[-1] != '.'
11009 && local_ptr[-1] != '['
11010 && local_ptr[-1] != '<')
11011 *local_ptr++ = '.';
11013 /* If this is ".." then the spec becomes "-" */
11014 if ( (unixname[0] == '.')
11015 && (unixname[1] == '.')
11016 && (unixname[2] == '/'))
11018 /* Add "-" and skip the ".." */
11019 if ((local_ptr[-1] == '.')
11020 && (local_ptr[-2] == '['))
11021 local_ptr--; /* prevent [.- */
11022 *local_ptr++ = '-';
11023 unixname += 3;
11024 continue;
11027 /* Copy the subdirectory */
11028 while (*unixname != '/')
11029 *local_ptr++= *unixname++;
11031 unixname++; /* Skip the "/" */
11034 /* Close the directory specification */
11035 if (local_ptr[-1] == '.') /* no trailing periods */
11036 local_ptr--;
11038 if (local_ptr[-1] == '[') /* no dir needed */
11039 local_ptr--;
11040 else
11041 *local_ptr++ = ']';
11044 /* Now add the filename. */
11046 while (*unixname)
11047 *local_ptr++ = *unixname++;
11048 *local_ptr = 0;
11050 /* Now append it to the original VMS spec. */
11052 strcpy ((must_revert==1)?fullname:basename, Local);
11054 /* If we put a [000000] in the filename, try to open it first. If this fails,
11055 remove the [000000], and return that name. This provides flexibility
11056 to the user in that they can use both rooted and non-rooted logical names
11057 to point to the location of the file. */
11059 if (check_filename_before_returning)
11061 f = open (fullname, O_RDONLY, 0666);
11062 if (f >= 0)
11064 /* The file name is OK as it is, so return it as is. */
11065 close (f);
11066 return 1;
11069 /* The filename did not work. Try to remove the [000000] from the name,
11070 and return it. */
11072 basename = index (fullname, '[');
11073 local_ptr = index (fullname, ']') + 1;
11074 strcpy (basename, local_ptr); /* this gets rid of it */
11078 return 1;
11080 #endif /* VMS */
11082 #ifdef VMS
11084 /* The following wrapper functions supply additional arguments to the VMS
11085 I/O routines to optimize performance with file handling. The arguments
11086 are:
11087 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11088 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11089 "fop=tef"- Truncate unused portions of file when closing file.
11090 "shr=nil"- Disallow file sharing while file is open. */
11092 static FILE *
11093 VMS_freopen (fname, type, oldfile)
11094 char *fname;
11095 char *type;
11096 FILE *oldfile;
11098 #undef freopen /* Get back the real freopen routine. */
11099 if (strcmp (type, "w") == 0)
11100 return freopen (fname, type, oldfile,
11101 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11102 return freopen (fname, type, oldfile, "mbc=16");
11105 static FILE *
11106 VMS_fopen (fname, type)
11107 char *fname;
11108 char *type;
11110 #undef fopen /* Get back the real fopen routine. */
11111 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11112 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11113 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11114 FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
11116 if (*type == 'w')
11117 return (*vmslib_fopen) (fname, type, "mbc=32",
11118 "deq=64", "fop=tef", "shr=nil");
11119 else
11120 return (*vmslib_fopen) (fname, type, "mbc=32");
11123 static int
11124 VMS_open (fname, flags, prot)
11125 char *fname;
11126 int flags;
11127 int prot;
11129 #undef open /* Get back the real open routine. */
11130 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
11133 /* more VMS hackery */
11134 #include <fab.h>
11135 #include <nam.h>
11137 extern unsigned long SYS$PARSE(), SYS$SEARCH();
11139 /* Work around another library bug. If a file is located via a searchlist,
11140 and if the device it's on is not the same device as the one specified
11141 in the first element of that searchlist, then both stat() and fstat()
11142 will fail to return info about it. `errno' will be set to EVMSERR, and
11143 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11144 We can get around this by fully parsing the filename and then passing
11145 that absolute name to stat().
11147 Without this fix, we can end up failing to find header files, which is
11148 bad enough, but then compounding the problem by reporting the reason for
11149 failure as "normal successful completion." */
11151 #undef fstat /* Get back to the library version. */
11153 static int
11154 VMS_fstat (fd, statbuf)
11155 int fd;
11156 struct stat *statbuf;
11158 int result = fstat (fd, statbuf);
11160 if (result < 0)
11162 FILE *fp;
11163 char nambuf[NAM$C_MAXRSS+1];
11165 if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
11166 result = VMS_stat (nambuf, statbuf);
11167 /* No fclose(fp) here; that would close(fd) as well. */
11170 return result;
11173 static int
11174 VMS_stat (name, statbuf)
11175 const char *name;
11176 struct stat *statbuf;
11178 int result = stat (name, statbuf);
11180 if (result < 0)
11182 struct FAB fab;
11183 struct NAM nam;
11184 char exp_nam[NAM$C_MAXRSS+1], /* expanded name buffer for SYS$PARSE */
11185 res_nam[NAM$C_MAXRSS+1]; /* resultant name buffer for SYS$SEARCH */
11187 fab = cc$rms_fab;
11188 fab.fab$l_fna = (char *) name;
11189 fab.fab$b_fns = (unsigned char) strlen (name);
11190 fab.fab$l_nam = (void *) &nam;
11191 nam = cc$rms_nam;
11192 nam.nam$l_esa = exp_nam, nam.nam$b_ess = sizeof exp_nam - 1;
11193 nam.nam$l_rsa = res_nam, nam.nam$b_rss = sizeof res_nam - 1;
11194 nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
11195 if (SYS$PARSE (&fab) & 1)
11197 if (SYS$SEARCH (&fab) & 1)
11199 res_nam[nam.nam$b_rsl] = '\0';
11200 result = stat (res_nam, statbuf);
11202 /* Clean up searchlist context cached by the system. */
11203 nam.nam$b_nop = NAM$M_SYNCHK;
11204 fab.fab$l_fna = 0, fab.fab$b_fns = 0;
11205 (void) SYS$PARSE (&fab);
11209 return result;
11212 static size_t
11213 VMS_fwrite (ptr, size, nitems, stream)
11214 void const *ptr;
11215 size_t size;
11216 size_t nitems;
11217 FILE *stream;
11219 /* VMS fwrite has undesirable results
11220 if STREAM happens to be a record oriented file.
11221 Work around this problem by writing each character individually. */
11222 char const *p = ptr;
11223 size_t bytes = size * nitems;
11224 char *lim = p + bytes;
11226 while (p < lim)
11227 if (putc (*p++, stream) == EOF)
11228 return 0;
11230 return bytes;
11232 #endif /* VMS */