* stor-layout.c (new_record_layout_info): Fix typo inside ifdef
[official-gcc.git] / gcc / cccp.c
blobe91d8cf1b4c2f1867008e38c3a05e2ad577aeb72
1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000 Free Software Foundation, Inc.
4 Written by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
24 #include "system.h"
25 #include <signal.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 # include <sys/resource.h>
29 #endif
31 typedef unsigned char U_CHAR;
33 #include "pcp.h"
34 #include "intl.h"
35 #include "prefix.h"
36 #include "version.h"
38 #ifdef MULTIBYTE_CHARS
39 #include "mbchar.h"
40 #include <locale.h>
41 #endif /* MULTIBYTE_CHARS */
43 #ifndef GET_ENV_PATH_LIST
44 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
45 #endif
47 #ifndef STANDARD_INCLUDE_DIR
48 # define STANDARD_INCLUDE_DIR "/usr/include"
49 #endif
51 /* By default, the suffix for object files is ".o". */
52 #ifdef OBJECT_SUFFIX
53 # define HAVE_OBJECT_SUFFIX
54 #else
55 # define OBJECT_SUFFIX ".o"
56 #endif
58 /* VMS-specific definitions */
59 #ifdef VMS
60 #include <descrip.h>
61 #include <ssdef.h>
62 #include <syidef.h>
63 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
64 #define fopen(fname,mode) VMS_fopen (fname,mode)
65 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
66 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
67 #define fwrite(ptr,size,nitems,stream) VMS_fwrite (ptr,size,nitems,stream)
68 static int VMS_fstat (), VMS_stat ();
69 static int VMS_open ();
70 static FILE *VMS_fopen ();
71 static FILE *VMS_freopen ();
72 static size_t VMS_fwrite ();
73 static void hack_vms_include_specification ();
74 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
75 #define INO_T_HASH(a) 0
76 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
77 #endif /* VMS */
79 /* Windows does not natively support inodes, and neither does MSDOS.
80 Cygwin's emulation can generate non-unique inodes, so don't use it. */
81 #if (defined (_WIN32) && ! defined (_UWIN)) \
82 || defined (__MSDOS__)
83 #define INO_T_EQ(a, b) 0
84 #endif
86 #ifndef INO_T_EQ
87 #define INO_T_EQ(a, b) ((a) == (b))
88 #endif
90 #ifndef INO_T_HASH
91 #define INO_T_HASH(a) (a)
92 #endif
94 #ifndef INCLUDE_LEN_FUDGE
95 #define INCLUDE_LEN_FUDGE 0
96 #endif
98 /* External declarations. */
100 HOST_WIDEST_INT parse_escape PARAMS ((char **, HOST_WIDEST_INT));
101 HOST_WIDEST_INT parse_c_expression PARAMS ((char *, int));
103 /* Name under which this program was invoked. */
105 static const char *progname;
107 /* Nonzero means use extra default include directories for C++. */
109 static int cplusplus;
111 /* Nonzero means handle cplusplus style comments */
113 static int cplusplus_comments;
115 /* Nonzero means handle #import, for objective C. */
117 static int objc;
119 /* Nonzero means this is an assembly file, and allow
120 unknown directives, which could be comments. */
122 static int lang_asm;
124 /* Current maximum length of directory names in the search path
125 for include files. (Altered as we get more of them.) */
127 static int max_include_len;
129 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
131 static int for_lint = 0;
133 /* Nonzero means copy comments into the output file. */
135 static int put_out_comments = 0;
137 /* Nonzero means don't process the ANSI trigraph sequences. */
139 static int no_trigraphs = 0;
141 /* Nonzero means print the names of included files rather than
142 the preprocessed output. 1 means just the #include "...",
143 2 means #include <...> as well. */
145 static int print_deps = 0;
147 /* Nonzero if missing .h files in -M output are assumed to be generated
148 files and not errors. */
150 static int print_deps_missing_files = 0;
152 /* Nonzero means print names of header files (-H). */
154 static int print_include_names = 0;
156 /* Nonzero means don't output line number information. */
158 static int no_line_directives;
160 /* Nonzero means output the text in failing conditionals,
161 inside #failed ... #endfailed. */
163 static int output_conditionals;
165 /* dump_only means inhibit output of the preprocessed text
166 and instead output the definitions of all user-defined
167 macros in a form suitable for use as input to cccp.
168 dump_names means pass #define and the macro name through to output.
169 dump_definitions means pass the whole definition (plus #define) through
172 static enum {dump_none, dump_only, dump_names, dump_definitions}
173 dump_macros = dump_none;
175 /* Nonzero means pass all #define and #undef directives which we actually
176 process through to the output stream. This feature is used primarily
177 to allow cc1 to record the #defines and #undefs for the sake of
178 debuggers which understand about preprocessor macros, but it may
179 also be useful with -E to figure out how symbols are defined, and
180 where they are defined. */
181 static int debug_output = 0;
183 /* Nonzero means pass #include lines through to the output,
184 even if they are ifdefed out. */
185 static int dump_includes;
187 /* Nonzero indicates special processing used by the pcp program. The
188 special effects of this mode are:
190 Inhibit all macro expansion, except those inside #if directives.
192 Process #define directives normally, and output their contents
193 to the output file.
195 Output preconditions to pcp_outfile indicating all the relevant
196 preconditions for use of this file in a later cpp run.
198 static FILE *pcp_outfile;
200 /* Nonzero means we are inside an IF during a -pcp run. In this mode
201 macro expansion is done, and preconditions are output for all macro
202 uses requiring them. */
203 static int pcp_inside_if;
205 /* Nonzero means never to include precompiled files.
206 This is 1 since there's no way now to make precompiled files,
207 so it's not worth testing for them. */
208 static int no_precomp = 1;
210 /* Nonzero means give all the error messages the ANSI standard requires. */
212 int pedantic;
214 /* Nonzero means try to make failure to fit ANSI C an error. */
216 static int pedantic_errors;
218 /* Nonzero means don't print warning messages. -w. */
220 static int inhibit_warnings = 0;
222 /* Nonzero means warn if slash-star appears in a slash-star comment,
223 or if newline-backslash appears in a slash-slash comment. */
225 static int warn_comments;
227 /* Nonzero means warn if a macro argument is (or would be)
228 stringified with -traditional. */
230 static int warn_stringify;
232 /* Nonzero means warn if there are any trigraphs. */
234 static int warn_trigraphs;
236 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
238 static int warn_undef;
240 /* Nonzero means warn if we find white space where it doesn't belong. */
242 static int warn_white_space;
244 /* Nonzero means warn if #import is used. */
246 static int warn_import = 1;
248 /* Nonzero means turn warnings into errors. */
250 static int warnings_are_errors;
252 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
254 int traditional;
256 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
258 int c89;
260 /* Nonzero for the 1999 C Standard. */
262 int c99;
264 /* Nonzero causes output not to be done,
265 but directives such as #define that have side effects
266 are still obeyed. */
268 static int no_output;
270 /* Nonzero means we should look for header.gcc files that remap file names. */
271 static int remap;
273 /* Nonzero means this file was included with a -imacros or -include
274 command line and should not be recorded as an include file. */
276 static int no_record_file;
278 /* Nonzero means that we have finished processing the command line options.
279 This flag is used to decide whether or not to issue certain errors
280 and/or warnings. */
282 static int done_initializing = 0;
284 /* Line where a newline was first seen in a string constant. */
286 static int multiline_string_line = 0;
288 /* I/O buffer structure.
289 The `fname' field is nonzero for source files and #include files
290 and for the dummy text used for -D and -U.
291 It is zero for rescanning results of macro expansion
292 and for expanding macro arguments. */
293 #define INPUT_STACK_MAX 400
294 static struct file_buf {
295 const char *fname;
296 /* Filename specified with #line directive. */
297 const char *nominal_fname;
298 /* The length of nominal_fname, which may contain embedded NULs. */
299 size_t nominal_fname_len;
300 /* Include file description. */
301 struct include_file *inc;
302 /* Record where in the search path this file was found.
303 For #include_next. */
304 struct file_name_list *dir;
305 int lineno;
306 int length;
307 U_CHAR *buf;
308 U_CHAR *bufp;
309 /* Macro that this level is the expansion of.
310 Included so that we can reenable the macro
311 at the end of this level. */
312 struct hashnode *macro;
313 /* Value of if_stack at start of this file.
314 Used to prohibit unmatched #endif (etc) in an include file. */
315 struct if_stack *if_stack;
316 /* Object to be freed at end of input at this level. */
317 U_CHAR *free_ptr;
318 /* True if this is a system header file; see is_system_include. */
319 char system_header_p;
320 } instack[INPUT_STACK_MAX];
322 static int last_error_tick; /* Incremented each time we print it. */
323 static int input_file_stack_tick; /* Incremented when the status changes. */
325 /* Current nesting level of input sources.
326 `instack[indepth]' is the level currently being read. */
327 static int indepth = -1;
328 #define CHECK_DEPTH(code) \
329 if (indepth >= (INPUT_STACK_MAX - 1)) \
331 error_with_line (line_for_error (instack[indepth].lineno), \
332 "macro or `#include' recursion too deep"); \
333 code; \
336 /* Current depth in #include directives that use <...>. */
337 static int system_include_depth = 0;
339 typedef struct file_buf FILE_BUF;
341 /* The output buffer. Its LENGTH field is the amount of room allocated
342 for the buffer, not the number of chars actually present. To get
343 that, subtract outbuf.buf from outbuf.bufp. */
345 #define OUTBUF_SIZE 10 /* initial size of output buffer */
346 static FILE_BUF outbuf;
348 /* Grow output buffer OBUF points at
349 so it can hold at least NEEDED more chars. */
351 #define check_expand(OBUF, NEEDED) \
352 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
353 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
355 struct file_name_list
357 struct file_name_list *next;
358 /* If the following is 1, it is a C-language system include
359 directory. */
360 int c_system_include_path;
361 /* Mapping of file names for this directory. */
362 struct file_name_map *name_map;
363 /* Non-zero if name_map is valid. */
364 int got_name_map;
365 /* The include directory status. */
366 struct stat st;
367 /* The include prefix: "" denotes the working directory,
368 otherwise fname must end in '/'.
369 The actual size is dynamically allocated. */
370 char fname[1];
373 /* #include "file" looks in source file dir, then stack. */
374 /* #include <file> just looks in the stack. */
375 /* -I directories are added to the end, then the defaults are added. */
376 /* The */
377 static struct default_include {
378 const char *fname; /* The name of the directory. */
379 const char *component; /* The component containing the directory */
380 int cplusplus; /* Only look here if we're compiling C++. */
381 int cxx_aware; /* Includes in this directory don't need to
382 be wrapped in extern "C" when compiling
383 C++. */
384 int included; /* Set if the directory is acceptable. */
385 } include_defaults_array[]
386 #ifdef INCLUDE_DEFAULTS
387 = INCLUDE_DEFAULTS;
388 #else
390 /* Pick up GNU C++ specific include files. */
391 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1, 0 },
392 #ifdef CROSS_COMPILE
393 /* This is the dir for fixincludes. Put it just before
394 the files that we fix. */
395 { GCC_INCLUDE_DIR, "GCC", 0, 0, 0 },
396 /* For cross-compilation, this dir name is generated
397 automatically in Makefile.in. */
398 { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0 },
399 #ifdef TOOL_INCLUDE_DIR
400 /* This is another place that the target system's headers might be. */
401 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0, 0 },
402 #endif
403 #else /* not CROSS_COMPILE */
404 #ifdef LOCAL_INCLUDE_DIR
405 /* This should be /usr/local/include and should come before
406 the fixincludes-fixed header files. */
407 { LOCAL_INCLUDE_DIR, 0, 0, 1, 0 },
408 #endif
409 #ifdef TOOL_INCLUDE_DIR
410 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
411 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
412 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0, 0 },
413 #endif
414 /* This is the dir for fixincludes. Put it just before
415 the files that we fix. */
416 { GCC_INCLUDE_DIR, "GCC", 0, 0, 0 },
417 /* Some systems have an extra dir of include files. */
418 #ifdef SYSTEM_INCLUDE_DIR
419 { SYSTEM_INCLUDE_DIR, 0, 0, 0, 0 },
420 #endif
421 #ifndef STANDARD_INCLUDE_COMPONENT
422 #define STANDARD_INCLUDE_COMPONENT 0
423 #endif
424 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0, 0 },
425 #endif /* not CROSS_COMPILE */
426 { 0, 0, 0, 0, 0 }
428 #endif /* no INCLUDE_DEFAULTS */
430 /* The code looks at the defaults through this pointer, rather than through
431 the constant structure above. This pointer gets changed if an environment
432 variable specifies other defaults. */
433 static struct default_include *include_defaults = include_defaults_array;
435 static struct file_name_list *include = 0; /* First dir to search */
436 /* First dir to search for <file> */
437 /* This is the first element to use for #include <...>.
438 If it is 0, use the entire chain for such includes. */
439 static struct file_name_list *first_bracket_include = 0;
440 /* This is the first element in the chain that corresponds to
441 a directory of system header files. */
442 static struct file_name_list *first_system_include = 0;
443 static struct file_name_list *last_include = 0; /* Last in chain */
445 /* Chain of include directories to put at the end of the other chain. */
446 static struct file_name_list *after_include = 0;
447 static struct file_name_list *last_after_include = 0; /* Last in chain */
449 /* Chain to put at the start of the system include files. */
450 static struct file_name_list *before_system = 0;
451 static struct file_name_list *last_before_system = 0; /* Last in chain */
453 /* Directory prefix that should replace `/usr' in the standard
454 include file directories. */
455 static char *include_prefix;
457 /* Maintain and search list of included files. */
459 struct include_file {
460 struct include_file *next; /* for include_hashtab */
461 struct include_file *next_ino; /* for include_ino_hashtab */
462 char *fname;
463 /* If the following is the empty string, it means #pragma once
464 was seen in this include file, or #import was applied to the file.
465 Otherwise, if it is nonzero, it is a macro name.
466 Don't include the file again if that macro is defined. */
467 const U_CHAR *control_macro;
468 /* Nonzero if the dependency on this include file has been output. */
469 int deps_output;
470 struct stat st;
473 /* Hash tables of files already included with #include or #import.
474 include_hashtab is by full name; include_ino_hashtab is by inode number. */
476 #define INCLUDE_HASHSIZE 61
477 static struct include_file *include_hashtab[INCLUDE_HASHSIZE];
478 static struct include_file *include_ino_hashtab[INCLUDE_HASHSIZE];
480 /* Global list of strings read in from precompiled files. This list
481 is kept in the order the strings are read in, with new strings being
482 added at the end through stringlist_tailp. We use this list to output
483 the strings at the end of the run.
485 static STRINGDEF *stringlist;
486 static STRINGDEF **stringlist_tailp = &stringlist;
489 /* Structure returned by create_definition */
490 typedef struct macrodef MACRODEF;
491 struct macrodef
493 struct definition *defn;
494 const U_CHAR *symnam;
495 int symlen;
498 enum sharp_token_type {
499 NO_SHARP_TOKEN = 0, /* token not present */
501 SHARP_TOKEN = '#', /* token spelled with # only */
502 WHITE_SHARP_TOKEN, /* token spelled with # and white space */
504 PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */
505 WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */
508 /* Structure allocated for every #define. For a simple replacement
509 such as
510 #define foo bar ,
511 nargs = -1, the `pattern' list is null, and the expansion is just
512 the replacement text. Nargs = 0 means a functionlike macro with no args,
513 e.g.,
514 #define getchar() getc (stdin) .
515 When there are args, the expansion is the replacement text with the
516 args squashed out, and the reflist is a list describing how to
517 build the output from the input: e.g., "3 chars, then the 1st arg,
518 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
519 The chars here come from the expansion. Whatever is left of the
520 expansion after the last arg-occurrence is copied after that arg.
521 Note that the reflist can be arbitrarily long---
522 its length depends on the number of times the arguments appear in
523 the replacement text, not how many args there are. Example:
524 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
525 pattern list
526 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
527 where (x, y) means (nchars, argno). */
529 typedef struct definition DEFINITION;
530 struct definition {
531 int nargs;
532 int length; /* length of expansion string */
533 int predefined; /* True if the macro was builtin or */
534 /* came from the command line */
535 U_CHAR *expansion;
536 int line; /* Line number of definition */
537 const char *file; /* File of definition */
538 size_t file_len; /* Length of file (which can contain NULs) */
539 char rest_args; /* Nonzero if last arg. absorbs the rest */
540 struct reflist {
541 struct reflist *next;
543 enum sharp_token_type stringify; /* set if a # operator before arg */
544 enum sharp_token_type raw_before; /* set if a ## operator before arg */
545 enum sharp_token_type raw_after; /* set if a ## operator after arg */
547 char rest_args; /* Nonzero if this arg. absorbs the rest */
548 int nchars; /* Number of literal chars to copy before
549 this arg occurrence. */
550 int argno; /* Number of arg to substitute (origin-0) */
551 } *pattern;
552 union {
553 /* Names of macro args, concatenated in reverse order
554 with comma-space between them.
555 The only use of this is that we warn on redefinition
556 if this differs between the old and new definitions. */
557 U_CHAR *argnames;
558 } args;
561 /* different kinds of things that can appear in the value field
562 of a hash node. Actually, this may be useless now. */
563 union hashval {
564 const char *cpval;
565 DEFINITION *defn;
566 KEYDEF *keydef;
570 * special extension string that can be added to the last macro argument to
571 * allow it to absorb the "rest" of the arguments when expanded. Ex:
572 * #define wow(a, b...) process (b, a, b)
573 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
574 * { wow (one, two); } -> { process (two, one, two); }
575 * if this "rest_arg" is used with the concat token '##' and if it is not
576 * supplied then the token attached to with ## will not be outputted. Ex:
577 * #define wow (a, b...) process (b ## , a, ## b)
578 * { wow (1, 2); } -> { process (2, 1, 2); }
579 * { wow (one); } -> { process (one); {
581 static char rest_extension[] = "...";
582 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
584 /* This is the implicit parameter name when using variable number of
585 parameters for macros using the ISO C 99 extension. */
586 static char va_args_name[] = "__VA_ARGS__";
587 #define VA_ARGS_NAME_LENGTH (sizeof (va_args_name) - 1)
589 /* The structure of a node in the hash table. The hash table
590 has entries for all tokens defined by #define directives (type T_MACRO),
591 plus some special tokens like __LINE__ (these each have their own
592 type, and the appropriate code is run when that type of node is seen.
593 It does not contain control words like "#define", which are recognized
594 by a separate piece of code. */
596 /* different flavors of hash nodes --- also used in keyword table */
597 enum node_type {
598 T_DEFINE = 1, /* the `#define' keyword */
599 T_INCLUDE, /* the `#include' keyword */
600 T_INCLUDE_NEXT, /* the `#include_next' keyword */
601 T_IMPORT, /* the `#import' keyword */
602 T_IFDEF, /* the `#ifdef' keyword */
603 T_IFNDEF, /* the `#ifndef' keyword */
604 T_IF, /* the `#if' keyword */
605 T_ELSE, /* `#else' */
606 T_PRAGMA, /* `#pragma' */
607 T_ELIF, /* `#elif' */
608 T_UNDEF, /* `#undef' */
609 T_LINE, /* `#line' */
610 T_ERROR, /* `#error' */
611 T_WARNING, /* `#warning' */
612 T_ENDIF, /* `#endif' */
613 T_SCCS, /* `#sccs', used on system V. */
614 T_IDENT, /* `#ident', used on system V. */
615 T_ASSERT, /* `#assert', taken from system V. */
616 T_UNASSERT, /* `#unassert', taken from system V. */
617 T_SPECLINE, /* special symbol `__LINE__' */
618 T_DATE, /* `__DATE__' */
619 T_FILE, /* `__FILE__' */
620 T_BASE_FILE, /* `__BASE_FILE__' */
621 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
622 T_VERSION, /* `__VERSION__' */
623 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
624 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
625 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
626 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
627 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
628 T_IMMEDIATE_PREFIX_TYPE, /* `__IMMEDIATE_PREFIX__' */
629 T_TIME, /* `__TIME__' */
630 T_CONST, /* Constant value, used by `__STDC__' */
631 T_MACRO, /* macro defined by `#define' */
632 T_DISABLED, /* macro temporarily turned off for rescan */
633 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
634 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
635 T_POISON, /* defined with `#pragma poison' */
636 T_UNUSED /* Used for something not defined. */
639 struct hashnode {
640 struct hashnode *next; /* double links for easy deletion */
641 struct hashnode *prev;
642 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
643 chain is kept, in case the node is the head
644 of the chain and gets deleted. */
645 enum node_type type; /* type of special token */
646 int length; /* length of token, for quick comparison */
647 U_CHAR *name; /* the actual name */
648 union hashval value; /* pointer to expansion, or whatever */
651 typedef struct hashnode HASHNODE;
653 /* Some definitions for the hash table. The hash function MUST be
654 computed as shown in hashf () below. That is because the rescan
655 loop computes the hash value `on the fly' for most tokens,
656 in order to avoid the overhead of a lot of procedure calls to
657 the hashf () function. Hashf () only exists for the sake of
658 politeness, for use when speed isn't so important. */
660 #define HASHSIZE 1403
661 static HASHNODE *hashtab[HASHSIZE];
662 #define HASHSTEP(old, c) ((old << 2) + c)
663 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
666 /* We let tm.h override the types used here, to handle trivial differences
667 such as the choice of unsigned int or long unsigned int for size_t.
668 When machines start needing nontrivial differences in the size type,
669 it would be best to do something here to figure out automatically
670 from other information what type to use. */
672 /* The string value for __SIZE_TYPE__. */
674 #ifndef SIZE_TYPE
675 #define SIZE_TYPE "long unsigned int"
676 #endif
678 /* The string value for __PTRDIFF_TYPE__. */
680 #ifndef PTRDIFF_TYPE
681 #define PTRDIFF_TYPE "long int"
682 #endif
684 /* The string value for __WCHAR_TYPE__. */
686 #ifndef WCHAR_TYPE
687 #define WCHAR_TYPE "int"
688 #endif
689 static const char * wchar_type = WCHAR_TYPE;
690 #undef WCHAR_TYPE
692 /* The string value for __USER_LABEL_PREFIX__ */
694 #ifndef USER_LABEL_PREFIX
695 #define USER_LABEL_PREFIX ""
696 #endif
697 static const char * user_label_prefix = USER_LABEL_PREFIX;
698 #undef USER_LABEL_PREFIX
700 /* The string value for __REGISTER_PREFIX__ */
702 #ifndef REGISTER_PREFIX
703 #define REGISTER_PREFIX ""
704 #endif
706 /* The string value for __IMMEDIATE_PREFIX__ */
708 #ifndef IMMEDIATE_PREFIX
709 #define IMMEDIATE_PREFIX ""
710 #endif
712 /* In the definition of a #assert name, this structure forms
713 a list of the individual values asserted.
714 Each value is itself a list of "tokens".
715 These are strings that are compared by name. */
717 struct tokenlist_list {
718 struct tokenlist_list *next;
719 struct arglist *tokens;
722 struct assertion_hashnode {
723 struct assertion_hashnode *next; /* double links for easy deletion */
724 struct assertion_hashnode *prev;
725 /* also, a back pointer to this node's hash
726 chain is kept, in case the node is the head
727 of the chain and gets deleted. */
728 struct assertion_hashnode **bucket_hdr;
729 int length; /* length of token, for quick comparison */
730 U_CHAR *name; /* the actual name */
731 /* List of token-sequences. */
732 struct tokenlist_list *value;
735 typedef struct assertion_hashnode ASSERTION_HASHNODE;
737 /* Some definitions for the hash table. The hash function MUST be
738 computed as shown in hashf below. That is because the rescan
739 loop computes the hash value `on the fly' for most tokens,
740 in order to avoid the overhead of a lot of procedure calls to
741 the hashf function. hashf only exists for the sake of
742 politeness, for use when speed isn't so important. */
744 #define ASSERTION_HASHSIZE 37
745 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
747 /* Nonzero means inhibit macroexpansion of what seem to be
748 assertion tests, in rescan. For #if. */
749 static int assertions_flag;
751 /* `struct directive' defines one #-directive, including how to handle it. */
753 #define DO_PROTO PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
755 struct directive {
756 int length; /* Length of name */
757 int (*func) DO_PROTO; /* Function to handle directive */
758 const char *name; /* Name of directive */
759 enum node_type type; /* Code which describes which directive. */
762 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
763 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
765 /* These functions are declared to return int instead of void since they
766 are going to be placed in the table and some old compilers have trouble with
767 pointers to functions returning void. */
769 static int do_assert DO_PROTO;
770 static int do_define DO_PROTO;
771 static int do_elif DO_PROTO;
772 static int do_else DO_PROTO;
773 static int do_endif DO_PROTO;
774 static int do_error DO_PROTO;
775 static int do_ident DO_PROTO;
776 static int do_if DO_PROTO;
777 static int do_include DO_PROTO;
778 static int do_line DO_PROTO;
779 static int do_pragma DO_PROTO;
780 #ifdef SCCS_DIRECTIVE
781 static int do_sccs DO_PROTO;
782 #endif
783 static int do_unassert DO_PROTO;
784 static int do_undef DO_PROTO;
785 static int do_xifdef DO_PROTO;
787 /* Here is the actual list of #-directives, most-often-used first. */
789 static struct directive directive_table[] = {
790 { 6, do_define, "define", T_DEFINE},
791 { 2, do_if, "if", T_IF},
792 { 5, do_xifdef, "ifdef", T_IFDEF},
793 { 6, do_xifdef, "ifndef", T_IFNDEF},
794 { 5, do_endif, "endif", T_ENDIF},
795 { 4, do_else, "else", T_ELSE},
796 { 4, do_elif, "elif", T_ELIF},
797 { 4, do_line, "line", T_LINE},
798 { 7, do_include, "include", T_INCLUDE},
799 { 12, do_include, "include_next", T_INCLUDE_NEXT},
800 { 6, do_include, "import", T_IMPORT},
801 { 5, do_undef, "undef", T_UNDEF},
802 { 5, do_error, "error", T_ERROR},
803 { 7, do_error, "warning", T_WARNING},
804 #ifdef SCCS_DIRECTIVE
805 { 4, do_sccs, "sccs", T_SCCS},
806 #endif
807 { 6, do_pragma, "pragma", T_PRAGMA},
808 { 5, do_ident, "ident", T_IDENT},
809 { 6, do_assert, "assert", T_ASSERT},
810 { 8, do_unassert, "unassert", T_UNASSERT},
811 { -1, 0, "", T_UNUSED},
814 /* When a directive handler is called,
815 this points to the # (or the : of the %:) that started the directive. */
816 U_CHAR *directive_start;
818 /* table to tell if char can be part of a C identifier. */
819 U_CHAR is_idchar[256];
820 /* table to tell if char can be first char of a c identifier. */
821 U_CHAR is_idstart[256];
822 /* table to tell if c is horizontal space. */
823 static U_CHAR is_hor_space[256];
824 /* table to tell if c is horizontal or vertical space. */
825 U_CHAR is_space[256];
827 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
828 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
830 static int errors = 0; /* Error counter for exit code */
832 /* Name of output file, for error messages. */
833 static const char *out_fname;
835 /* Nonzero to ignore \ in string constants. Use to treat #line 1 "A:\file.h
836 as a non-form feed. If you want it to be a form feed, you must use
837 # 1 "\f". */
838 static int ignore_escape_flag = 1;
840 /* Stack of conditionals currently in progress
841 (including both successful and failing conditionals). */
843 struct if_stack {
844 struct if_stack *next; /* for chaining to the next stack frame */
845 const char *fname; /* copied from input when frame is made */
846 size_t fname_len; /* similarly */
847 int lineno; /* similarly */
848 int if_succeeded; /* true if a leg of this if-group
849 has been passed through rescan */
850 const U_CHAR *control_macro; /* For #ifndef at start of file,
851 this is the macro name tested. */
852 enum node_type type; /* type of last directive seen in this group */
854 typedef struct if_stack IF_STACK_FRAME;
855 static IF_STACK_FRAME *if_stack = NULL;
857 /* Buffer of -M output. */
858 static char *deps_buffer;
860 /* Number of bytes allocated in above. */
861 static int deps_allocated_size;
863 /* Number of bytes used. */
864 static int deps_size;
866 /* Number of bytes since the last newline. */
867 static int deps_column;
869 /* Nonzero means -I- has been seen,
870 so don't look for #include "foo" the source-file directory. */
871 static int ignore_srcdir;
873 static int safe_read PARAMS ((int, char *, int));
874 static void safe_write PARAMS ((int, const char *, int));
876 int main PARAMS ((int, char **));
878 static void path_include PARAMS ((char *));
880 static const U_CHAR *index0 PARAMS ((const U_CHAR *, int, size_t));
882 static void trigraph_pcp PARAMS ((FILE_BUF *));
883 static void check_white_space PARAMS ((FILE_BUF *));
885 static void newline_fix PARAMS ((U_CHAR *));
886 static void name_newline_fix PARAMS ((U_CHAR *));
888 static const char *get_lintcmd PARAMS ((const U_CHAR *, const U_CHAR *,
889 const U_CHAR **, int *, int *));
891 static void rescan PARAMS ((FILE_BUF *, int));
893 static FILE_BUF expand_to_temp_buffer PARAMS ((const U_CHAR *, const U_CHAR *,
894 int, int));
896 static int handle_directive PARAMS ((FILE_BUF *, FILE_BUF *));
898 static struct tm *timestamp PARAMS ((void));
899 static void special_symbol PARAMS ((HASHNODE *, FILE_BUF *));
901 static int is_system_include PARAMS ((const char *));
902 static char *base_name PARAMS ((const char *));
903 static int absolute_filename PARAMS ((const char *));
904 static size_t simplify_filename PARAMS ((char *));
906 static char *read_filename_string PARAMS ((int, FILE *));
907 static struct file_name_map *read_name_map PARAMS ((const char *));
908 static int open_include_file PARAMS ((char *, struct file_name_list *,
909 const U_CHAR *, struct include_file **));
910 static char *remap_include_file PARAMS ((char *, struct file_name_list *));
911 static int lookup_ino_include PARAMS ((struct include_file *));
913 static void finclude PARAMS ((int, struct include_file *, FILE_BUF *, int,
914 struct file_name_list *));
915 static void record_control_macro PARAMS ((struct include_file *,
916 const U_CHAR *));
918 static char *check_precompiled PARAMS ((int, struct stat *, const char *,
919 const char **));
920 static int check_preconditions PARAMS ((const char *));
921 static void pcfinclude PARAMS ((U_CHAR *, const U_CHAR *, FILE_BUF *));
922 static void pcstring_used PARAMS ((HASHNODE *));
923 static void write_output PARAMS ((void));
924 static void pass_thru_directive PARAMS ((const U_CHAR *, const U_CHAR *,
925 FILE_BUF *, struct directive *));
927 static MACRODEF create_definition PARAMS ((const U_CHAR *, const U_CHAR *,
928 FILE_BUF *));
930 static int check_macro_name PARAMS ((const U_CHAR *, int));
931 static int compare_defs PARAMS ((DEFINITION *, DEFINITION *));
932 static int comp_def_part PARAMS ((int, const U_CHAR *, int, const U_CHAR *,
933 int, int));
935 static DEFINITION *collect_expansion PARAMS ((const U_CHAR *, const U_CHAR *,
936 int, struct arglist *));
938 int check_assertion PARAMS ((const U_CHAR *, int, int, struct arglist *));
939 static int compare_token_lists PARAMS ((struct arglist *, struct arglist *));
941 static struct arglist *read_token_list PARAMS ((const U_CHAR **,
942 const U_CHAR *, int *));
943 static void free_token_list PARAMS ((struct arglist *));
945 static ASSERTION_HASHNODE *assertion_install PARAMS ((const U_CHAR *, int, int));
946 static ASSERTION_HASHNODE *assertion_lookup PARAMS ((const U_CHAR *, int, int));
947 static void delete_assertion PARAMS ((ASSERTION_HASHNODE *));
949 static void do_once PARAMS ((void));
951 static HOST_WIDEST_INT eval_if_expression PARAMS ((const U_CHAR *, int));
952 static void conditional_skip PARAMS ((FILE_BUF *, int, enum node_type,
953 const U_CHAR *, FILE_BUF *));
954 static void skip_if_group PARAMS ((FILE_BUF *, int, FILE_BUF *));
955 static void validate_else PARAMS ((const U_CHAR *, const U_CHAR *));
957 static U_CHAR *skip_to_end_of_comment PARAMS ((FILE_BUF *, int *, int));
958 static U_CHAR *skip_quoted_string PARAMS ((const U_CHAR *, const U_CHAR *,
959 int, int *, int *, int *));
960 static char *quote_string PARAMS ((char *, const char *, size_t));
961 static U_CHAR *skip_paren_group PARAMS ((FILE_BUF *));
963 /* Last arg to output_line_directive. */
964 enum file_change_code {same_file, enter_file, leave_file};
965 static void output_line_directive PARAMS ((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
967 static void macroexpand PARAMS ((HASHNODE *, FILE_BUF *));
969 struct argdata;
970 static int macarg PARAMS ((struct argdata *, int));
972 static U_CHAR *macarg1 PARAMS ((U_CHAR *, const U_CHAR *, struct hashnode *, int *, int *, int *, int));
974 static int discard_comments PARAMS ((U_CHAR *, int, int));
976 static void change_newlines PARAMS ((struct argdata *));
978 static void notice PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
979 static void vnotice PARAMS ((const char *, va_list));
980 void error PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
981 void verror PARAMS ((const char *, va_list));
982 static void error_from_errno PARAMS ((const char *));
983 void warning PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
984 static void vwarning PARAMS ((const char *, va_list));
985 static void error_with_line PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2;
986 static void verror_with_line PARAMS ((int, const char *, va_list));
987 static void vwarning_with_line PARAMS ((int, const char *, va_list));
988 static void warning_with_line PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2;
989 void pedwarn PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
990 void pedwarn_with_line PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2;
991 static void pedwarn_with_file_and_line PARAMS ((const char *, size_t, int, const char *, ...)) ATTRIBUTE_PRINTF_4;
992 static void pedwarn_strange_white_space PARAMS ((int));
994 static void print_containing_files PARAMS ((void));
996 static int line_for_error PARAMS ((int));
997 static int grow_outbuf PARAMS ((FILE_BUF *, int));
999 static HASHNODE *install PARAMS ((const U_CHAR *, int, enum node_type,
1000 const char *, int));
1001 HASHNODE *lookup PARAMS ((const U_CHAR *, int, int));
1002 static void delete_macro PARAMS ((HASHNODE *));
1003 static int hashf PARAMS ((const U_CHAR *, int, int));
1005 static void dump_single_macro PARAMS ((HASHNODE *, FILE *));
1006 static void dump_all_macros PARAMS ((void));
1007 static void dump_defn_1 PARAMS ((const U_CHAR *, int, int, FILE *));
1008 static void dump_arg_n PARAMS ((DEFINITION *, int, FILE *));
1010 static void initialize_char_syntax PARAMS ((void));
1011 static void initialize_builtins PARAMS ((FILE_BUF *, FILE_BUF *));
1013 static void make_definition PARAMS ((char *));
1014 static void make_undef PARAMS ((char *, FILE_BUF *));
1016 static void make_assertion PARAMS ((const char *, const char *));
1018 static struct file_name_list *new_include_prefix PARAMS ((struct file_name_list *, const char *, const char *, const char *));
1019 static void append_include_chain PARAMS ((struct file_name_list *, struct file_name_list *));
1021 static int quote_string_for_make PARAMS ((char *, const char *));
1022 static void deps_output PARAMS ((const char *, int));
1024 void fatal PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
1025 void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
1026 static void perror_with_name PARAMS ((const char *));
1027 static void pfatal_with_name PARAMS ((const char *)) ATTRIBUTE_NORETURN;
1028 static void pipe_closed PARAMS ((int)) ATTRIBUTE_NORETURN;
1030 static void memory_full PARAMS ((void)) ATTRIBUTE_NORETURN;
1031 static void print_help PARAMS ((void));
1033 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1034 retrying if necessary. If MAX_READ_LEN is defined, read at most
1035 that bytes at a time. Return a negative value if an error occurs,
1036 otherwise return the actual number of bytes read,
1037 which must be LEN unless end-of-file was reached. */
1039 static int
1040 safe_read (desc, ptr, len)
1041 int desc;
1042 char *ptr;
1043 int len;
1045 int left, rcount, nchars;
1047 left = len;
1048 while (left > 0) {
1049 rcount = left;
1050 #ifdef MAX_READ_LEN
1051 if (rcount > MAX_READ_LEN)
1052 rcount = MAX_READ_LEN;
1053 #endif
1054 nchars = read (desc, ptr, rcount);
1055 if (nchars < 0)
1057 #ifdef EINTR
1058 if (errno == EINTR)
1059 continue;
1060 #endif
1061 return nchars;
1063 if (nchars == 0)
1064 break;
1065 ptr += nchars;
1066 left -= nchars;
1068 return len - left;
1071 /* Write LEN bytes at PTR to descriptor DESC,
1072 retrying if necessary, and treating any real error as fatal.
1073 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1075 static void
1076 safe_write (desc, ptr, len)
1077 int desc;
1078 const char *ptr;
1079 int len;
1081 int wcount, written;
1083 while (len > 0) {
1084 wcount = len;
1085 #ifdef MAX_WRITE_LEN
1086 if (wcount > MAX_WRITE_LEN)
1087 wcount = MAX_WRITE_LEN;
1088 #endif
1089 written = write (desc, ptr, wcount);
1090 if (written < 0)
1092 #ifdef EINTR
1093 if (errno == EINTR)
1094 continue;
1095 #endif
1096 pfatal_with_name (out_fname);
1098 ptr += written;
1099 len -= written;
1104 static void
1105 print_help ()
1107 printf ("Usage: %s [switches] input output\n", progname);
1108 printf ("Switches:\n");
1109 printf (" -include <file> Include the contents of <file> before other files\n");
1110 printf (" -imacros <file> Accept definition of macros in <file>\n");
1111 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1112 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1113 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1114 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1115 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1116 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1117 printf (" -nostdinc Do not search the system include directories\n");
1118 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1119 printf (" -o <file> Put output into <file>\n");
1120 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1121 printf (" -traditional Follow K&R pre-processor behaviour\n");
1122 printf (" -trigraphs Support ANSI C trigraphs\n");
1123 printf (" -lang-c Assume that the input sources are in C\n");
1124 printf (" -lang-c89 Assume that the input is C89; depricated\n");
1125 printf (" -lang-c++ Assume that the input sources are in C++\n");
1126 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1127 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1128 printf (" -lang-asm Assume that the input sources are in assembler\n");
1129 printf (" -lang-fortran Assume that the input sources are in Fortran\n");
1130 printf (" -lang-chill Assume that the input sources are in Chill\n");
1131 printf (" -std=<std name> Specify the conformance standard; one of:\n");
1132 printf (" gnu89, gnu99, c89, c99, iso9899:1990,\n");
1133 printf (" iso9899:199409, iso9899:1999\n");
1134 printf (" -+ Allow parsing of C++ style features\n");
1135 printf (" -w Inhibit warning messages\n");
1136 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1137 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1138 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1139 printf (" -Wno-comment{s} Do not warn about comments\n");
1140 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1141 printf (" a string if -traditional is specified\n");
1142 printf (" -Wno-traditional Do not warn about stringification\n");
1143 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1144 printf (" -Wno-undef Do not warn about testing undefined macros\n");
1145 printf (" -Wimport Warn about the use of the #import directive\n");
1146 printf (" -Wno-import Do not warn about the use of #import\n");
1147 printf (" -Werror Treat all warnings as errors\n");
1148 printf (" -Wno-error Do not treat warnings as errors\n");
1149 printf (" -Wall Enable all preprocessor warnings\n");
1150 printf (" -M Generate make dependencies\n");
1151 printf (" -MM As -M, but ignore system header files\n");
1152 printf (" -MD As -M, but put output in a .d file\n");
1153 printf (" -MMD As -MD, but ignore system header files\n");
1154 printf (" -MG Treat missing header file as generated files\n");
1155 printf (" -g Include #define and #undef directives in the output\n");
1156 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1157 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1158 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1159 printf (" -U<macro> Undefine <macro> \n");
1160 printf (" -u or -undef Do not predefine any macros\n");
1161 printf (" -v Display the version number\n");
1162 printf (" -H Print the name of header files as they are used\n");
1163 printf (" -C Do not discard comments\n");
1164 printf (" -dM Display a list of macro definitions active at end\n");
1165 printf (" -dD Preserve macro definitions in output\n");
1166 printf (" -dN As -dD except that only the names are preserved\n");
1167 printf (" -dI Include #include directives in the output\n");
1168 printf (" -ifoutput Describe skipped code blocks in output \n");
1169 printf (" -P Do not generate #line directives\n");
1170 printf (" -$ Do not include '$' in identifiers\n");
1171 printf (" -remap Remap file names when including files.\n");
1172 printf (" -h or --help Display this information\n");
1176 main (argc, argv)
1177 int argc;
1178 char **argv;
1180 struct stat st;
1181 const char *in_fname;
1182 char *cp;
1183 int f, i;
1184 FILE_BUF *fp;
1186 char **pend_files;
1187 char **pend_defs;
1188 char **pend_undefs;
1189 char **pend_assertions;
1190 char **pend_includes;
1192 /* Record the option used with each element of pend_assertions.
1193 This is preparation for supporting more than one option for making
1194 an assertion. */
1195 const char **pend_assertion_options;
1196 int no_standard_includes = 0;
1197 int no_standard_cplusplus_includes = 0;
1198 int missing_newline = 0;
1200 /* Non-0 means don't output the preprocessed program. */
1201 int inhibit_output = 0;
1202 /* Non-0 means -v, so print the full set of include dirs. */
1203 int verbose = 0;
1205 /* File name which deps are being written to.
1206 This is 0 if deps are being written to stdout. */
1207 char *deps_file = 0;
1208 /* Fopen file mode to open deps_file with. */
1209 const char *deps_mode = "a";
1210 /* Stream on which to print the dependency information. */
1211 FILE *deps_stream = 0;
1212 /* Target-name to write with the dependency information. */
1213 char *deps_target = 0;
1215 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1216 /* Get rid of any avoidable limit on stack size. */
1218 struct rlimit rlim;
1220 /* Set the stack limit huge so that alloca (particularly stringtab
1221 in dbxread.c) does not fail. */
1222 getrlimit (RLIMIT_STACK, &rlim);
1223 rlim.rlim_cur = rlim.rlim_max;
1224 setrlimit (RLIMIT_STACK, &rlim);
1226 #endif
1228 #ifdef SIGPIPE
1229 signal (SIGPIPE, pipe_closed);
1230 #endif
1232 #ifdef HAVE_LC_MESSAGES
1233 setlocale (LC_MESSAGES, "");
1234 #endif
1235 (void) bindtextdomain (PACKAGE, localedir);
1236 (void) textdomain (PACKAGE);
1238 progname = base_name (argv[0]);
1240 #ifdef VMS
1242 /* Remove extension from PROGNAME. */
1243 char *p;
1244 char *s = xstrdup (progname);
1245 progname = s;
1247 if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */
1248 if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */
1249 && (p[1] == 'e' || p[1] == 'E')
1250 && (p[2] == 'x' || p[2] == 'X')
1251 && (p[3] == 'e' || p[3] == 'E')
1252 && !p[4])
1253 *p = '\0';
1255 #endif
1257 /* Do not invoke xmalloc before this point, since locale and
1258 progname need to be set first, in case a diagnostic is issued. */
1260 pend_files = (char **) xmalloc (argc * sizeof (char *));
1261 pend_defs = (char **) xmalloc ((2 * argc) * sizeof (char *));
1262 pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1263 pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1264 pend_includes = (char **) xmalloc (argc * sizeof (char *));
1265 pend_assertion_options = (const char **) xmalloc (argc * sizeof (char *));
1267 in_fname = NULL;
1268 out_fname = NULL;
1270 /* Initialize is_idchar. */
1271 initialize_char_syntax ();
1273 no_line_directives = 0;
1274 no_trigraphs = 1;
1275 dump_macros = dump_none;
1276 no_output = 0;
1277 cplusplus = 0;
1278 cplusplus_comments = 1;
1280 bzero ((char *) pend_files, argc * sizeof (char *));
1281 bzero ((char *) pend_defs, (2 * argc) * sizeof (char *));
1282 bzero ((char *) pend_undefs, argc * sizeof (char *));
1283 bzero ((char *) pend_assertions, argc * sizeof (char *));
1284 bzero ((char *) pend_includes, argc * sizeof (char *));
1286 #ifdef MULTIBYTE_CHARS
1287 /* Change to the native locale for multibyte conversions. */
1288 setlocale (LC_CTYPE, "");
1289 literal_codeset = getenv ("LANG");
1290 #endif
1292 /* Process switches and find input file name. */
1294 for (i = 1; i < argc; i++) {
1295 if (argv[i][0] != '-') {
1296 if (out_fname != NULL)
1298 print_help ();
1299 fatal ("Too many arguments");
1301 else if (in_fname != NULL)
1302 out_fname = argv[i];
1303 else
1304 in_fname = argv[i];
1305 } else {
1306 switch (argv[i][1]) {
1308 case 'i':
1309 if (!strcmp (argv[i], "-include")) {
1310 if (i + 1 == argc)
1311 fatal ("Filename missing after `-include' option");
1312 else {
1313 i++;
1314 simplify_filename (pend_includes[i] = argv[i]);
1317 if (!strcmp (argv[i], "-imacros")) {
1318 if (i + 1 == argc)
1319 fatal ("Filename missing after `-imacros' option");
1320 else {
1321 i++;
1322 simplify_filename (pend_files[i] = argv[i]);
1325 if (!strcmp (argv[i], "-iprefix")) {
1326 if (i + 1 == argc)
1327 fatal ("Filename missing after `-iprefix' option");
1328 else
1329 include_prefix = argv[++i];
1331 if (!strcmp (argv[i], "-ifoutput")) {
1332 output_conditionals = 1;
1334 if (!strcmp (argv[i], "-isystem")) {
1335 struct file_name_list *dirtmp;
1337 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1338 "", argv[++i])))
1339 break;
1340 dirtmp->c_system_include_path = 1;
1342 if (before_system == 0)
1343 before_system = dirtmp;
1344 else
1345 last_before_system->next = dirtmp;
1346 last_before_system = dirtmp; /* Tail follows the last one */
1348 /* Add directory to end of path for includes,
1349 with the default prefix at the front of its name. */
1350 if (!strcmp (argv[i], "-iwithprefix")) {
1351 struct file_name_list *dirtmp;
1352 char *prefix;
1354 if (include_prefix != 0)
1355 prefix = include_prefix;
1356 else {
1357 prefix = xstrdup (GCC_INCLUDE_DIR);
1358 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1359 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1360 prefix[strlen (prefix) - 7] = 0;
1363 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1364 prefix, argv[++i])))
1365 break;
1367 if (after_include == 0)
1368 after_include = dirtmp;
1369 else
1370 last_after_include->next = dirtmp;
1371 last_after_include = dirtmp; /* Tail follows the last one */
1373 /* Add directory to main path for includes,
1374 with the default prefix at the front of its name. */
1375 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1376 struct file_name_list *dirtmp;
1377 char *prefix;
1379 if (include_prefix != 0)
1380 prefix = include_prefix;
1381 else {
1382 prefix = xstrdup (GCC_INCLUDE_DIR);
1383 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1384 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1385 prefix[strlen (prefix) - 7] = 0;
1388 dirtmp = new_include_prefix (NULL_PTR, NULL_PTR, prefix, argv[++i]);
1389 append_include_chain (dirtmp, dirtmp);
1391 /* Add directory to end of path for includes. */
1392 if (!strcmp (argv[i], "-idirafter")) {
1393 struct file_name_list *dirtmp;
1395 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1396 "", argv[++i])))
1397 break;
1399 if (after_include == 0)
1400 after_include = dirtmp;
1401 else
1402 last_after_include->next = dirtmp;
1403 last_after_include = dirtmp; /* Tail follows the last one */
1405 break;
1407 case 'o':
1408 if (out_fname != NULL)
1409 fatal ("Output filename specified twice");
1410 if (i + 1 == argc)
1411 fatal ("Filename missing after -o option");
1412 out_fname = argv[++i];
1413 if (!strcmp (out_fname, "-"))
1414 out_fname = "";
1415 break;
1417 case 'p':
1418 if (!strcmp (argv[i], "-pedantic"))
1419 pedantic = 1;
1420 else if (!strcmp (argv[i], "-pedantic-errors")) {
1421 pedantic = 1;
1422 pedantic_errors = 1;
1423 } else if (!strcmp (argv[i], "-pcp")) {
1424 char *pcp_fname;
1425 if (i + 1 == argc)
1426 fatal ("Filename missing after -pcp option");
1427 pcp_fname = argv[++i];
1428 pcp_outfile
1429 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1430 ? fopen (pcp_fname, "w")
1431 : stdout);
1432 if (pcp_outfile == 0)
1433 pfatal_with_name (pcp_fname);
1434 no_precomp = 1;
1436 break;
1438 case 't':
1439 if (!strcmp (argv[i], "-traditional")) {
1440 traditional = 1;
1441 cplusplus_comments = 0;
1442 } else if (!strcmp (argv[i], "-trigraphs")) {
1443 no_trigraphs = 0;
1445 break;
1447 case 'l':
1448 if (! strcmp (argv[i], "-lang-c"))
1449 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 1, objc = 0;
1450 else if (! strcmp (argv[i], "-lang-c89"))
1452 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1453 no_trigraphs = 0;
1454 pend_defs[2*i] = "__STRICT_ANSI__";
1456 else if (! strcmp (argv[i], "-lang-c++"))
1457 cplusplus = 1, cplusplus_comments = 1, c89 = 0, c99 = 0, objc = 0;
1458 else if (! strcmp (argv[i], "-lang-objc"))
1459 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 0, objc = 1;
1460 else if (! strcmp (argv[i], "-lang-objc++"))
1461 cplusplus = 1, cplusplus_comments = 1, c89 = 0, c99 = 0, objc = 1;
1462 else if (! strcmp (argv[i], "-lang-asm"))
1463 lang_asm = 1;
1464 else if (! strcmp (argv[i], "-lang-fortran"))
1465 /* Doesn't actually do anything. */ ;
1466 else if (! strcmp (argv[i], "-lint"))
1467 for_lint = 1;
1468 break;
1470 case '+':
1471 cplusplus = 1, cplusplus_comments = 1;
1472 break;
1474 case 's':
1475 if (!strcmp (argv[i], "-std=gnu89"))
1477 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1479 else if (!strcmp (argv[i], "-std=gnu9x")
1480 || !strcmp (argv[i], "-std=gnu99"))
1482 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 1, objc = 0;
1483 pend_defs[2*i+1] = "__STDC_VERSION__=199901L";
1485 else if (!strcmp (argv[i], "-std=iso9899:1990")
1486 || !strcmp (argv[i], "-std=c89"))
1488 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1489 no_trigraphs = 0;
1490 pend_defs[2*i] = "__STRICT_ANSI__";
1492 else if (!strcmp (argv[i], "-std=iso9899:199409"))
1494 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1495 no_trigraphs = 0;
1496 pend_defs[2*i] = "__STRICT_ANSI__";
1497 pend_defs[2*i+1] = "__STDC_VERSION__=199409L";
1499 else if (!strcmp (argv[i], "-std=iso9899:199x")
1500 || !strcmp (argv[i], "-std=iso9899:1999")
1501 || !strcmp (argv[i], "-std=c9x")
1502 || !strcmp (argv[i], "-std=c99"))
1504 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 1, objc = 0;
1505 no_trigraphs = 0;
1506 pend_defs[2*i] = "__STRICT_ANSI__";
1507 pend_defs[2*i+1] = "__STDC_VERSION__=199901L";
1509 break;
1511 case 'w':
1512 inhibit_warnings = 1;
1513 break;
1515 case 'W':
1516 if (!strcmp (argv[i], "-Wtrigraphs"))
1517 warn_trigraphs = 1;
1518 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1519 warn_trigraphs = 0;
1520 else if (!strcmp (argv[i], "-Wcomment"))
1521 warn_comments = 1;
1522 else if (!strcmp (argv[i], "-Wno-comment"))
1523 warn_comments = 0;
1524 else if (!strcmp (argv[i], "-Wcomments"))
1525 warn_comments = 1;
1526 else if (!strcmp (argv[i], "-Wno-comments"))
1527 warn_comments = 0;
1528 else if (!strcmp (argv[i], "-Wtraditional"))
1529 warn_stringify = 1;
1530 else if (!strcmp (argv[i], "-Wno-traditional"))
1531 warn_stringify = 0;
1532 else if (!strcmp (argv[i], "-Wwhite-space"))
1533 warn_white_space = 1;
1534 else if (!strcmp (argv[i], "-Wno-white-space"))
1535 warn_white_space = 0;
1536 else if (!strcmp (argv[i], "-Wundef"))
1537 warn_undef = 1;
1538 else if (!strcmp (argv[i], "-Wno-undef"))
1539 warn_undef = 0;
1540 else if (!strcmp (argv[i], "-Wimport"))
1541 warn_import = 1;
1542 else if (!strcmp (argv[i], "-Wno-import"))
1543 warn_import = 0;
1544 else if (!strcmp (argv[i], "-Werror"))
1545 warnings_are_errors = 1;
1546 else if (!strcmp (argv[i], "-Wno-error"))
1547 warnings_are_errors = 0;
1548 else if (!strcmp (argv[i], "-Wall"))
1550 warn_trigraphs = 1;
1551 warn_comments = 1;
1552 warn_white_space = 1;
1554 break;
1556 case 'f':
1557 if (!strcmp (argv[i], "-fleading-underscore"))
1558 user_label_prefix = "_";
1559 else if (!strcmp (argv[i], "-fno-leading-underscore"))
1560 user_label_prefix = "";
1561 break;
1563 case 'M':
1564 /* The style of the choices here is a bit mixed.
1565 The chosen scheme is a hybrid of keeping all options in one string
1566 and specifying each option in a separate argument:
1567 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1568 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1569 -M[M][G][D file]. This is awkward to handle in specs, and is not
1570 as extensible. */
1571 /* ??? -MG must be specified in addition to one of -M or -MM.
1572 This can be relaxed in the future without breaking anything.
1573 The converse isn't true. */
1575 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1576 if (!strcmp (argv[i], "-MG"))
1578 print_deps_missing_files = 1;
1579 break;
1581 if (!strcmp (argv[i], "-M"))
1582 print_deps = 2;
1583 else if (!strcmp (argv[i], "-MM"))
1584 print_deps = 1;
1585 else if (!strcmp (argv[i], "-MD"))
1586 print_deps = 2;
1587 else if (!strcmp (argv[i], "-MMD"))
1588 print_deps = 1;
1589 /* For -MD and -MMD options, write deps on file named by next arg. */
1590 if (!strcmp (argv[i], "-MD")
1591 || !strcmp (argv[i], "-MMD")) {
1592 if (i + 1 == argc)
1593 fatal ("Filename missing after %s option", argv[i]);
1594 i++;
1595 deps_file = argv[i];
1596 deps_mode = "w";
1597 } else {
1598 /* For -M and -MM, write deps on standard output
1599 and suppress the usual output. */
1600 deps_stream = stdout;
1601 inhibit_output = 1;
1603 break;
1605 case 'd':
1607 char *p = argv[i] + 2;
1608 char c;
1609 while ((c = *p++)) {
1610 /* Arg to -d specifies what parts of macros to dump */
1611 switch (c) {
1612 case 'M':
1613 dump_macros = dump_only;
1614 no_output = 1;
1615 break;
1616 case 'N':
1617 dump_macros = dump_names;
1618 break;
1619 case 'D':
1620 dump_macros = dump_definitions;
1621 break;
1622 case 'I':
1623 dump_includes = 1;
1624 break;
1628 break;
1630 case 'g':
1631 if (argv[i][2] == '3')
1632 debug_output = 1;
1633 break;
1635 case '-':
1636 if (strcmp (argv[i], "--help") != 0)
1637 return i;
1638 print_help ();
1639 exit (0);
1640 break;
1642 case 'v':
1643 notice ("GNU CPP version %s", version_string);
1644 #ifdef TARGET_VERSION
1645 TARGET_VERSION;
1646 #endif
1647 fprintf (stderr, "\n");
1648 verbose = 1;
1649 break;
1651 case 'H':
1652 print_include_names = 1;
1653 break;
1655 case 'D':
1656 if (argv[i][2] != 0)
1657 pend_defs[2*i] = argv[i] + 2;
1658 else if (i + 1 == argc)
1659 fatal ("Macro name missing after -D option");
1660 else
1661 i++, pend_defs[2*i] = argv[i];
1662 break;
1664 case 'A':
1666 char *p;
1668 if (argv[i][2] != 0)
1669 p = argv[i] + 2;
1670 else if (i + 1 == argc)
1671 fatal ("Assertion missing after -A option");
1672 else
1673 p = argv[++i];
1675 if (!strcmp (p, "-")) {
1676 /* -A- eliminates all predefined macros and assertions.
1677 Let's include also any that were specified earlier
1678 on the command line. That way we can get rid of any
1679 that were passed automatically in from GCC. */
1680 int j;
1681 for (j = 0; j < i; j++)
1682 pend_defs[2*j] = pend_assertions[j] = 0;
1683 } else {
1684 pend_assertions[i] = p;
1685 pend_assertion_options[i] = "-A";
1688 break;
1690 case 'U': /* JF #undef something */
1691 if (argv[i][2] != 0)
1692 pend_undefs[i] = argv[i] + 2;
1693 else if (i + 1 == argc)
1694 fatal ("Macro name missing after -U option");
1695 else
1696 pend_undefs[i] = argv[i+1], i++;
1697 break;
1699 case 'C':
1700 put_out_comments = 1;
1701 break;
1703 case 'E': /* -E comes from cc -E; ignore it. */
1704 break;
1706 case 'P':
1707 no_line_directives = 1;
1708 break;
1710 case '$': /* Don't include $ in identifiers. */
1711 is_idchar['$'] = is_idstart['$'] = 0;
1712 break;
1714 case 'I': /* Add directory to path for includes. */
1716 struct file_name_list *dirtmp;
1717 char *dir = argv[i][2] ? argv[i] + 2 : argv[++i];
1719 if (! ignore_srcdir && dir && !strcmp (dir, "-")) {
1720 ignore_srcdir = 1;
1721 /* Don't use any preceding -I directories for #include <...>. */
1722 first_bracket_include = 0;
1724 else {
1725 dirtmp = new_include_prefix (last_include, NULL_PTR, "", dir);
1726 append_include_chain (dirtmp, dirtmp);
1729 break;
1731 case 'n':
1732 if (!strcmp (argv[i], "-nostdinc"))
1733 /* -nostdinc causes no default include directories.
1734 You must specify all include-file directories with -I. */
1735 no_standard_includes = 1;
1736 else if (!strcmp (argv[i], "-nostdinc++"))
1737 /* -nostdinc++ causes no default C++-specific include directories. */
1738 no_standard_cplusplus_includes = 1;
1739 else if (!strcmp (argv[i], "-noprecomp"))
1740 no_precomp = 1;
1741 break;
1743 case 'r':
1744 if (!strcmp (argv[i], "-remap"))
1745 remap = 1;
1746 break;
1748 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1749 if (in_fname == NULL) {
1750 in_fname = "";
1751 break;
1752 } else if (out_fname == NULL) {
1753 out_fname = "";
1754 break;
1755 } /* else fall through into error */
1757 default:
1758 fatal ("Invalid option `%s'", argv[i]);
1763 /* Add dirs from CPATH after dirs from -I. */
1764 /* There seems to be confusion about what CPATH should do,
1765 so for the moment it is not documented. */
1766 /* Some people say that CPATH should replace the standard include dirs,
1767 but that seems pointless: it comes before them, so it overrides them
1768 anyway. */
1769 GET_ENV_PATH_LIST (cp, "CPATH");
1770 if (cp && ! no_standard_includes)
1771 path_include (cp);
1773 /* Initialize output buffer */
1775 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1776 outbuf.bufp = outbuf.buf;
1777 outbuf.length = OUTBUF_SIZE;
1779 /* Do partial setup of input buffer for the sake of generating
1780 early #line directives (when -g is in effect). */
1782 fp = &instack[++indepth];
1783 if (in_fname == NULL)
1784 in_fname = "";
1785 fp->nominal_fname = fp->fname = in_fname;
1786 fp->nominal_fname_len = strlen (in_fname);
1787 fp->lineno = 0;
1789 /* Install __LINE__, etc. Must follow initialize_char_syntax
1790 and option processing. */
1791 initialize_builtins (fp, &outbuf);
1793 /* Now handle the command line options. */
1795 /* Do -U's, -D's and -A's in the order they were seen. */
1796 for (i = 1; i < argc; i++) {
1797 if (pend_undefs[i]) {
1798 if (debug_output)
1799 output_line_directive (fp, &outbuf, 0, same_file);
1800 make_undef (pend_undefs[i], &outbuf);
1802 if (pend_defs[2*i]) {
1803 if (debug_output)
1804 output_line_directive (fp, &outbuf, 0, same_file);
1805 make_definition (pend_defs[2*i]);
1807 if (pend_defs[2*i+1]) {
1808 if (debug_output)
1809 output_line_directive (fp, &outbuf, 0, same_file);
1810 make_definition (pend_defs[2*i+1]);
1812 if (pend_assertions[i])
1813 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1816 done_initializing = 1;
1818 { /* Read the appropriate environment variable and if it exists
1819 replace include_defaults with the listed path. */
1820 char *epath = 0;
1821 switch ((objc << 1) + cplusplus)
1823 case 0:
1824 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
1825 break;
1826 case 1:
1827 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
1828 break;
1829 case 2:
1830 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
1831 break;
1832 case 3:
1833 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
1834 break;
1836 /* If the environment var for this language is set,
1837 add to the default list of include directories. */
1838 if (epath) {
1839 int num_dirs;
1840 char *startp, *endp;
1842 for (num_dirs = 1, startp = epath; *startp; startp++)
1843 if (*startp == PATH_SEPARATOR)
1844 num_dirs++;
1845 include_defaults
1846 = (struct default_include *) xmalloc ((num_dirs
1847 * sizeof (struct default_include))
1848 + sizeof (include_defaults_array));
1849 startp = endp = epath;
1850 num_dirs = 0;
1851 while (1) {
1852 char c = *endp++;
1853 if (c == PATH_SEPARATOR || !c) {
1854 endp[-1] = 0;
1855 include_defaults[num_dirs].fname
1856 = startp == endp ? "." : xstrdup (startp);
1857 endp[-1] = c;
1858 include_defaults[num_dirs].component = 0;
1859 include_defaults[num_dirs].cplusplus = cplusplus;
1860 include_defaults[num_dirs].cxx_aware = 1;
1861 num_dirs++;
1862 if (!c)
1863 break;
1864 startp = endp;
1867 /* Put the usual defaults back in at the end. */
1868 bcopy ((const PTR) include_defaults_array,
1869 (PTR) &include_defaults[num_dirs],
1870 sizeof (include_defaults_array));
1874 append_include_chain (before_system, last_before_system);
1875 first_system_include = before_system;
1877 /* Unless -fnostdinc,
1878 tack on the standard include file dirs to the specified list */
1879 if (!no_standard_includes) {
1880 struct default_include *p = include_defaults;
1881 char *specd_prefix = include_prefix;
1882 char *default_prefix = xstrdup (GCC_INCLUDE_DIR);
1883 int default_len = 0;
1884 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1885 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1886 default_len = strlen (default_prefix) - 7;
1887 default_prefix[default_len] = 0;
1889 /* Search "translated" versions of GNU directories.
1890 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1891 if (specd_prefix != 0 && default_len != 0)
1892 for (p = include_defaults; p->fname; p++) {
1893 /* Some standard dirs are only for C++. */
1894 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1895 /* Does this dir start with the prefix? */
1896 if (!strncmp (p->fname, default_prefix, default_len)) {
1897 /* Yes; change prefix and add to search list. */
1898 struct file_name_list *new
1899 = new_include_prefix (NULL_PTR, NULL_PTR, specd_prefix,
1900 p->fname + default_len);
1901 if (new) {
1902 new->c_system_include_path = !p->cxx_aware;
1903 append_include_chain (new, new);
1904 if (first_system_include == 0)
1905 first_system_include = new;
1906 p->included = 1;
1911 /* Search ordinary names for GNU include directories. */
1912 for (p = include_defaults; p->fname; p++) {
1913 /* Some standard dirs are only for C++. */
1914 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1915 struct file_name_list *new
1916 = new_include_prefix (NULL_PTR, p->component, "", p->fname);
1917 if (new) {
1918 new->c_system_include_path = !p->cxx_aware;
1919 append_include_chain (new, new);
1920 if (first_system_include == 0)
1921 first_system_include = new;
1922 p->included = 1;
1928 /* Tack the after_include chain at the end of the include chain. */
1929 append_include_chain (after_include, last_after_include);
1930 if (first_system_include == 0)
1931 first_system_include = after_include;
1933 /* With -v, print the list of dirs to search. */
1934 if (verbose) {
1935 struct file_name_list *p;
1936 notice ("#include \"...\" search starts here:\n");
1937 for (p = include; p; p = p->next) {
1938 if (p == first_bracket_include)
1939 notice ("#include <...> search starts here:\n");
1940 if (!p->fname[0])
1941 fprintf (stderr, " .\n");
1942 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
1943 fprintf (stderr, " %s\n", p->fname);
1944 else
1945 /* Omit trailing '/'. */
1946 fprintf (stderr, " %.*s\n", (int) strlen (p->fname) - 1, p->fname);
1948 notice ("End of search list.\n");
1950 struct default_include * d;
1951 notice ("The following default directories have been omitted from the search path:\n");
1952 for (d = include_defaults; d->fname; d++)
1953 if (! d->included)
1954 fprintf (stderr, " %s\n", d->fname);
1955 notice ("End of omitted list.\n");
1959 /* -MG doesn't select the form of output and must be specified with one of
1960 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1961 inhibit compilation. */
1962 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
1963 fatal ("-MG must be specified with one of -M or -MM");
1965 /* Either of two environment variables can specify output of deps.
1966 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1967 where OUTPUT_FILE is the file to write deps info to
1968 and DEPS_TARGET is the target to mention in the deps. */
1970 if (print_deps == 0
1971 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1972 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1973 char *spec = getenv ("DEPENDENCIES_OUTPUT");
1974 char *s;
1975 char *output_file;
1977 if (spec == 0) {
1978 spec = getenv ("SUNPRO_DEPENDENCIES");
1979 print_deps = 2;
1981 else
1982 print_deps = 1;
1984 /* Find the space before the DEPS_TARGET, if there is one. */
1985 s = index (spec, ' ');
1986 if (s) {
1987 deps_target = s + 1;
1988 output_file = xmalloc (s - spec + 1);
1989 bcopy (spec, output_file, s - spec);
1990 output_file[s - spec] = 0;
1991 } else {
1992 deps_target = 0;
1993 output_file = spec;
1996 deps_file = output_file;
1997 deps_mode = "a";
2000 /* For -M, print the expected object file name
2001 as the target of this Make-rule. */
2002 if (print_deps) {
2003 deps_allocated_size = 200;
2004 deps_buffer = xmalloc (deps_allocated_size);
2005 deps_buffer[0] = 0;
2006 deps_size = 0;
2007 deps_column = 0;
2009 if (deps_target) {
2010 deps_output (deps_target, ':');
2011 } else if (*in_fname == 0) {
2012 deps_output ("-", ':');
2013 } else {
2014 char *p, *q;
2015 int len;
2017 q = base_name (in_fname);
2019 /* Copy remainder to mungable area. */
2020 p = (char *) alloca (strlen(q) + 8);
2021 strcpy (p, q);
2023 /* Output P, but remove known suffixes. */
2024 len = strlen (p);
2025 q = p + len;
2026 if (len >= 2
2027 && p[len - 2] == '.'
2028 && index("cCsSm", p[len - 1]))
2029 q = p + (len - 2);
2030 else if (len >= 3
2031 && p[len - 3] == '.'
2032 && p[len - 2] == 'c'
2033 && p[len - 1] == 'c')
2034 q = p + (len - 3);
2035 else if (len >= 4
2036 && p[len - 4] == '.'
2037 && p[len - 3] == 'c'
2038 && p[len - 2] == 'x'
2039 && p[len - 1] == 'x')
2040 q = p + (len - 4);
2041 else if (len >= 4
2042 && p[len - 4] == '.'
2043 && p[len - 3] == 'c'
2044 && p[len - 2] == 'p'
2045 && p[len - 1] == 'p')
2046 q = p + (len - 4);
2048 /* Supply our own suffix. */
2049 strcpy (q, OBJECT_SUFFIX);
2051 deps_output (p, ':');
2054 deps_output (in_fname, ' ');
2057 /* Scan the -imacros files before the main input.
2058 Much like #including them, but with no_output set
2059 so that only their macro definitions matter. */
2061 no_output++; no_record_file++;
2062 for (i = 1; i < argc; i++)
2063 if (pend_files[i]) {
2064 struct include_file *inc;
2065 int fd = open_include_file (pend_files[i], NULL_PTR, NULL_PTR, &inc);
2066 if (fd < 0) {
2067 perror_with_name (pend_files[i]);
2068 return FATAL_EXIT_CODE;
2070 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2072 no_output--; no_record_file--;
2074 /* Copy the entire contents of the main input file into
2075 the stacked input buffer previously allocated for it. */
2077 /* JF check for stdin */
2078 if (in_fname == NULL || *in_fname == 0) {
2079 in_fname = "";
2080 f = 0;
2081 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2082 goto perror;
2084 if (fstat (f, &st) != 0)
2085 pfatal_with_name (in_fname);
2086 fp->nominal_fname = fp->fname = in_fname;
2087 fp->nominal_fname_len = strlen (in_fname);
2088 fp->lineno = 1;
2089 fp->system_header_p = 0;
2090 /* JF all this is mine about reading pipes and ttys */
2091 if (! S_ISREG (st.st_mode)) {
2092 /* Read input from a file that is not a normal disk file.
2093 We cannot preallocate a buffer with the correct size,
2094 so we must read in the file a piece at the time and make it bigger. */
2095 int size;
2096 int bsize;
2097 int cnt;
2099 if (S_ISDIR (st.st_mode))
2100 fatal ("Input file `%s' is a directory", in_fname);
2102 bsize = 2000;
2103 size = 0;
2104 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2105 for (;;) {
2106 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2107 if (cnt < 0) goto perror; /* error! */
2108 size += cnt;
2109 if (size != bsize) break; /* End of file */
2110 bsize *= 2;
2111 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2113 fp->length = size;
2114 } else {
2115 /* Read a file whose size we can determine in advance.
2116 For the sake of VMS, st.st_size is just an upper bound. */
2117 size_t s = (size_t) st.st_size;
2118 if (s != st.st_size || s + 2 < s)
2119 memory_full ();
2120 fp->buf = (U_CHAR *) xmalloc (s + 2);
2121 fp->length = safe_read (f, (char *) fp->buf, s);
2122 if (fp->length < 0) goto perror;
2124 fp->bufp = fp->buf;
2125 fp->if_stack = if_stack;
2127 /* Make sure data ends with a newline. And put a null after it. */
2129 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2130 /* Backslash-newline at end is not good enough. */
2131 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2132 fp->buf[fp->length++] = '\n';
2133 missing_newline = 1;
2135 fp->buf[fp->length] = '\0';
2137 /* Unless inhibited, convert trigraphs in the input. */
2139 if (!no_trigraphs)
2140 trigraph_pcp (fp);
2142 if (warn_white_space)
2143 check_white_space (fp);
2145 /* Now that we know the input file is valid, open the output. */
2147 if (!out_fname || !strcmp (out_fname, ""))
2148 out_fname = "stdout";
2149 else if (! freopen (out_fname, "w", stdout))
2150 pfatal_with_name (out_fname);
2152 output_line_directive (fp, &outbuf, 0, same_file);
2154 /* Scan the -include files before the main input. */
2156 no_record_file++;
2157 for (i = 1; i < argc; i++)
2158 if (pend_includes[i]) {
2159 struct include_file *inc;
2160 int fd = open_include_file (pend_includes[i], NULL_PTR, NULL_PTR, &inc);
2161 if (fd < 0) {
2162 perror_with_name (pend_includes[i]);
2163 return FATAL_EXIT_CODE;
2165 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2167 no_record_file--;
2169 /* Scan the input, processing macros and directives. */
2171 rescan (&outbuf, 0);
2173 if (missing_newline)
2174 fp->lineno--;
2176 if (pedantic && missing_newline)
2177 pedwarn ("file does not end in newline");
2179 /* Now we have processed the entire input
2180 Write whichever kind of output has been requested. */
2182 if (dump_macros == dump_only)
2183 dump_all_macros ();
2184 else if (! inhibit_output) {
2185 write_output ();
2188 if (print_deps) {
2189 /* Don't actually write the deps file if compilation has failed. */
2190 if (errors == 0) {
2191 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2192 pfatal_with_name (deps_file);
2193 fputs (deps_buffer, deps_stream);
2194 putc ('\n', deps_stream);
2195 if (deps_file) {
2196 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2197 fatal ("I/O error on output");
2202 if (pcp_outfile && pcp_outfile != stdout
2203 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2204 fatal ("I/O error on `-pcp' output");
2206 if (ferror (stdout) || fclose (stdout) != 0)
2207 fatal ("I/O error on output");
2209 if (errors)
2210 exit (FATAL_EXIT_CODE);
2211 exit (SUCCESS_EXIT_CODE);
2213 perror:
2214 pfatal_with_name (in_fname);
2215 return 0;
2218 /* Given a colon-separated list of file names PATH,
2219 add all the names to the search path for include files. */
2221 static void
2222 path_include (path)
2223 char *path;
2225 char *p;
2227 p = path;
2229 if (*p)
2230 while (1) {
2231 char *q = p;
2232 char c;
2233 struct file_name_list *dirtmp;
2235 /* Find the end of this name. */
2236 while ((c = *q++) != PATH_SEPARATOR && c)
2237 continue;
2239 q[-1] = 0;
2240 dirtmp = new_include_prefix (last_include, NULL_PTR,
2241 "", p == q ? "." : p);
2242 q[-1] = c;
2243 append_include_chain (dirtmp, dirtmp);
2245 /* Advance past this name. */
2246 p = q;
2247 if (! c)
2248 break;
2252 /* Return the address of the first character in S that equals C.
2253 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2254 Return 0 if there is no such character. Assume that C itself is not '\0'.
2255 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2256 but unfortunately memchr isn't autoconfigured yet. */
2258 static const U_CHAR *
2259 index0 (s, c, n)
2260 const U_CHAR *s;
2261 int c;
2262 size_t n;
2264 const char *p = (const char *) s;
2265 for (;;) {
2266 const char *q = index (p, c);
2267 if (q)
2268 return (const U_CHAR *) q;
2269 else {
2270 size_t l = strlen (p);
2271 if (l == n)
2272 return 0;
2273 l++;
2274 p += l;
2275 n -= l;
2280 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2281 before main CCCP processing. Name `pcp' is also in honor of the
2282 drugs the trigraph designers must have been on.
2284 Using an extra pass through the buffer takes a little extra time,
2285 but is infinitely less hairy than trying to handle trigraphs inside
2286 strings, etc. everywhere, and also makes sure that trigraphs are
2287 only translated in the top level of processing. */
2289 static void
2290 trigraph_pcp (buf)
2291 FILE_BUF *buf;
2293 register U_CHAR c, *bptr;
2294 register const U_CHAR *fptr, *sptr, *lptr;
2295 int len;
2297 fptr = sptr = bptr = buf->buf;
2298 lptr = fptr + buf->length;
2299 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2300 if (*++sptr != '?')
2301 continue;
2302 switch (*++sptr) {
2303 case '=':
2304 c = '#';
2305 break;
2306 case '(':
2307 c = '[';
2308 break;
2309 case '/':
2310 c = '\\';
2311 break;
2312 case ')':
2313 c = ']';
2314 break;
2315 case '\'':
2316 c = '^';
2317 break;
2318 case '<':
2319 c = '{';
2320 break;
2321 case '!':
2322 c = '|';
2323 break;
2324 case '>':
2325 c = '}';
2326 break;
2327 case '-':
2328 c = '~';
2329 break;
2330 case '?':
2331 sptr--;
2332 continue;
2333 default:
2334 continue;
2336 len = sptr - fptr - 2;
2338 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2339 C, this will be memmove (). */
2340 if (bptr != fptr && len > 0)
2341 bcopy ((const PTR) fptr, (PTR) bptr, len);
2343 bptr += len;
2344 *bptr++ = c;
2345 fptr = ++sptr;
2347 len = buf->length - (fptr - buf->buf);
2348 if (bptr != fptr && len > 0)
2349 bcopy ((const PTR) fptr, (PTR) bptr, len);
2350 buf->length -= fptr - bptr;
2351 buf->buf[buf->length] = '\0';
2352 if (warn_trigraphs && fptr != bptr)
2353 warning_with_line (0, "%lu trigraph(s) encountered",
2354 (unsigned long) (fptr - bptr) / 2);
2357 /* Warn about white space between backslash and end of line. */
2359 static void
2360 check_white_space (buf)
2361 FILE_BUF *buf;
2363 register const U_CHAR *sptr = buf->buf;
2364 register const U_CHAR *lptr = sptr + buf->length;
2365 register const U_CHAR *nptr;
2366 int line = 0;
2368 nptr = sptr = buf->buf;
2369 lptr = sptr + buf->length;
2370 for (nptr = sptr;
2371 (nptr = index0 (nptr, '\n', (size_t) (lptr - nptr))) != NULL;
2372 nptr ++) {
2373 register const U_CHAR *p = nptr;
2374 line++;
2375 for (p = nptr; sptr < p; p--) {
2376 if (! is_hor_space[p[-1]]) {
2377 if (p[-1] == '\\' && p != nptr)
2378 warning_with_line (line,
2379 "`\\' followed by white space at end of line");
2380 break;
2386 /* Move all backslash-newline pairs out of embarrassing places.
2387 Exchange all such pairs following BP
2388 with any potentially-embarrassing characters that follow them.
2389 Potentially-embarrassing characters are / and *
2390 (because a backslash-newline inside a comment delimiter
2391 would cause it not to be recognized).
2392 We assume that *BP == '\\'. */
2394 static void
2395 newline_fix (bp)
2396 U_CHAR *bp;
2398 register U_CHAR *p = bp;
2400 /* First count the backslash-newline pairs here. */
2401 do {
2402 if (p[1] != '\n')
2403 break;
2404 p += 2;
2405 } while (*p == '\\');
2407 /* What follows the backslash-newlines is not embarrassing. */
2409 if (*p != '/' && *p != '*')
2410 /* What follows the backslash-newlines is not embarrassing. */
2411 return;
2413 /* Copy all potentially embarrassing characters
2414 that follow the backslash-newline pairs
2415 down to where the pairs originally started. */
2417 *bp++ = *p++;
2418 while (*p == '*' || *p == '/');
2420 /* Now write the same number of pairs after the embarrassing chars. */
2421 while (bp < p) {
2422 *bp++ = '\\';
2423 *bp++ = '\n';
2427 /* Like newline_fix but for use within a directive-name.
2428 Move any backslash-newlines up past any following symbol constituents. */
2430 static void
2431 name_newline_fix (bp)
2432 U_CHAR *bp;
2434 register U_CHAR *p = bp;
2436 /* First count the backslash-newline pairs here. */
2437 do {
2438 if (p[1] != '\n')
2439 break;
2440 p += 2;
2441 } while (*p == '\\');
2443 /* What follows the backslash-newlines is not embarrassing. */
2445 if (!is_idchar[*p])
2446 /* What follows the backslash-newlines is not embarrassing. */
2447 return;
2449 /* Copy all potentially embarrassing characters
2450 that follow the backslash-newline pairs
2451 down to where the pairs originally started. */
2453 *bp++ = *p++;
2454 while (is_idchar[*p]);
2456 /* Now write the same number of pairs after the embarrassing chars. */
2457 while (bp < p) {
2458 *bp++ = '\\';
2459 *bp++ = '\n';
2463 /* Look for lint commands in comments.
2465 When we come in here, ibp points into a comment. Limit is as one expects.
2466 scan within the comment -- it should start, after lwsp, with a lint command.
2467 If so that command is returned as a (constant) string.
2469 Upon return, any arg will be pointed to with argstart and will be
2470 arglen long. Note that we don't parse that arg since it will just
2471 be printed out again. */
2473 static const char *
2474 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2475 register const U_CHAR *ibp;
2476 register const U_CHAR *limit;
2477 const U_CHAR **argstart; /* point to command arg */
2478 int *arglen, *cmdlen; /* how long they are */
2480 HOST_WIDEST_INT linsize;
2481 register const U_CHAR *numptr; /* temp for arg parsing */
2483 *arglen = 0;
2485 SKIP_WHITE_SPACE (ibp);
2487 if (ibp >= limit) return NULL;
2489 linsize = limit - ibp;
2491 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2492 if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
2493 *cmdlen = 10;
2494 return "NOTREACHED";
2496 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2497 *cmdlen = 8;
2498 return "ARGSUSED";
2500 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2501 *cmdlen = 11;
2502 return "LINTLIBRARY";
2504 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2505 *cmdlen = 7;
2506 ibp += 7; linsize -= 7;
2507 if ((linsize == 0) || ! ISDIGIT (*ibp)) return "VARARGS";
2509 /* OK, read a number */
2510 for (numptr = *argstart = ibp; (numptr < limit) && ISDIGIT (*numptr);
2511 numptr++);
2512 *arglen = numptr - *argstart;
2513 return "VARARGS";
2515 return NULL;
2519 * The main loop of the program.
2521 * Read characters from the input stack, transferring them to the
2522 * output buffer OP.
2524 * Macros are expanded and push levels on the input stack.
2525 * At the end of such a level it is popped off and we keep reading.
2526 * At the end of any other kind of level, we return.
2527 * #-directives are handled, except within macros.
2529 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2530 * and insert them when appropriate. This is set while scanning macro
2531 * arguments before substitution. It is zero when scanning for final output.
2532 * There are two types of Newline markers:
2533 * * Newline - follows a macro name that was not expanded
2534 * because it appeared inside an expansion of the same macro.
2535 * This marker prevents future expansion of that identifier.
2536 * When the input is rescanned into the final output, these are deleted.
2537 * These are also deleted by ## concatenation.
2538 * * Newline Space (or Newline and any other whitespace character)
2539 * stands for a place that tokens must be separated or whitespace
2540 * is otherwise desirable, but where the ANSI standard specifies there
2541 * is no whitespace. This marker turns into a Space (or whichever other
2542 * whitespace char appears in the marker) in the final output,
2543 * but it turns into nothing in an argument that is stringified with #.
2544 * Such stringified arguments are the only place where the ANSI standard
2545 * specifies with precision that whitespace may not appear.
2547 * During this function, IP->bufp is kept cached in IBP for speed of access.
2548 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2549 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2550 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2551 * explicitly, and before RECACHE, since RECACHE uses OBP.
2554 static void
2555 rescan (op, output_marks)
2556 FILE_BUF *op;
2557 int output_marks;
2559 /* Character being scanned in main loop. */
2560 register U_CHAR c;
2562 /* Length of pending accumulated identifier. */
2563 register int ident_length = 0;
2565 /* Hash code of pending accumulated identifier. */
2566 register int hash = 0;
2568 /* Current input level (&instack[indepth]). */
2569 FILE_BUF *ip;
2571 /* Pointer for scanning input. */
2572 register U_CHAR *ibp;
2574 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2575 register U_CHAR *limit;
2577 /* Pointer for storing output. */
2578 register U_CHAR *obp;
2580 /* REDO_CHAR is nonzero if we are processing an identifier
2581 after backing up over the terminating character.
2582 Sometimes we process an identifier without backing up over
2583 the terminating character, if the terminating character
2584 is not special. Backing up is done so that the terminating character
2585 will be dispatched on again once the identifier is dealt with. */
2586 int redo_char = 0;
2588 /* 1 if within an identifier inside of which a concatenation
2589 marker (Newline -) has been seen. */
2590 int concatenated = 0;
2592 /* While scanning a comment or a string constant,
2593 this records the line it started on, for error messages. */
2594 int start_line;
2596 /* Record position of last `real' newline. */
2597 U_CHAR *beg_of_line;
2599 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2601 #define POPMACRO \
2602 do { ip->macro->type = T_MACRO; \
2603 if (ip->free_ptr) free (ip->free_ptr); \
2604 --indepth; } while (0)
2606 /* Reload `rescan's local variables that describe the current
2607 level of the input stack. */
2609 #define RECACHE \
2610 do { ip = &instack[indepth]; \
2611 ibp = ip->bufp; \
2612 limit = ip->buf + ip->length; \
2613 op->bufp = obp; \
2614 check_expand (op, limit - ibp); \
2615 beg_of_line = 0; \
2616 obp = op->bufp; } while (0)
2618 if (no_output && instack[indepth].fname != 0)
2619 skip_if_group (&instack[indepth], 1, NULL);
2621 obp = op->bufp;
2622 RECACHE;
2624 beg_of_line = ibp;
2626 /* Our caller must always put a null after the end of
2627 the input at each input stack level. */
2628 if (*limit != 0)
2629 abort ();
2631 while (1) {
2632 c = *ibp++;
2633 *obp++ = c;
2635 switch (c) {
2636 case '\\':
2637 if (*ibp == '\n' && !ip->macro) {
2638 /* At the top level, always merge lines ending with backslash-newline,
2639 even in middle of identifier. But do not merge lines in a macro,
2640 since backslash might be followed by a newline-space marker. */
2641 ++ibp;
2642 ++ip->lineno;
2643 --obp; /* remove backslash from obuf */
2644 break;
2646 /* If ANSI, backslash is just another character outside a string. */
2647 if (!traditional)
2648 goto randomchar;
2649 /* Otherwise, backslash suppresses specialness of following char,
2650 so copy it here to prevent the switch from seeing it.
2651 But first get any pending identifier processed. */
2652 if (ident_length > 0)
2653 goto specialchar;
2654 if (ibp < limit)
2655 *obp++ = *ibp++;
2656 break;
2658 case '%':
2659 if (ident_length || ip->macro || traditional)
2660 goto randomchar;
2661 while (*ibp == '\\' && ibp[1] == '\n') {
2662 ibp += 2;
2663 ++ip->lineno;
2665 if (*ibp != ':')
2666 break;
2667 /* Treat this %: digraph as if it were #. */
2668 /* Fall through. */
2670 case '#':
2671 if (assertions_flag) {
2672 if (ident_length)
2673 goto specialchar;
2674 /* Copy #foo (bar lose) without macro expansion. */
2675 obp[-1] = '#'; /* In case it was '%'. */
2676 SKIP_WHITE_SPACE (ibp);
2677 while (is_idchar[*ibp])
2678 *obp++ = *ibp++;
2679 SKIP_WHITE_SPACE (ibp);
2680 if (*ibp == '(') {
2681 ip->bufp = ibp;
2682 skip_paren_group (ip);
2683 bcopy ((const PTR) ibp, (PTR) obp, ip->bufp - ibp);
2684 obp += ip->bufp - ibp;
2685 ibp = ip->bufp;
2687 break;
2690 /* If this is expanding a macro definition, don't recognize
2691 preprocessing directives. */
2692 if (ip->macro != 0)
2693 goto randomchar;
2694 /* If this is expand_into_temp_buffer,
2695 don't recognize them either. Warn about them
2696 only after an actual newline at this level,
2697 not at the beginning of the input level. */
2698 if (! ip->fname) {
2699 if (ip->buf != beg_of_line)
2700 warning ("preprocessing directive not recognized within macro arg");
2701 goto randomchar;
2703 if (ident_length)
2704 goto specialchar;
2707 /* # keyword: a # must be first nonblank char on the line */
2708 if (beg_of_line == 0)
2709 goto randomchar;
2711 U_CHAR *bp;
2713 /* Scan from start of line, skipping whitespace, comments
2714 and backslash-newlines, and see if we reach this #.
2715 If not, this # is not special. */
2716 bp = beg_of_line;
2717 /* If -traditional, require # to be at beginning of line. */
2718 if (!traditional) {
2719 while (1) {
2720 if (is_hor_space[*bp])
2721 bp++;
2722 else if (*bp == '\\' && bp[1] == '\n')
2723 bp += 2;
2724 else if (*bp == '/' && bp[1] == '*') {
2725 bp += 2;
2726 while (1)
2728 if (*bp == '*')
2730 if (bp[1] == '/')
2732 bp += 2;
2733 break;
2736 else
2738 #ifdef MULTIBYTE_CHARS
2739 int length;
2740 length = local_mblen (bp, limit - bp);
2741 if (length > 1)
2742 bp += (length - 1);
2743 #endif
2745 bp++;
2748 /* There is no point in trying to deal with C++ // comments here,
2749 because if there is one, then this # must be part of the
2750 comment and we would never reach here. */
2751 else break;
2753 if (c == '%') {
2754 if (bp[0] != '%')
2755 break;
2756 while (bp[1] == '\\' && bp[2] == '\n')
2757 bp += 2;
2758 if (bp + 1 != ibp)
2759 break;
2760 /* %: appears at start of line; skip past the ':' too. */
2761 bp++;
2762 ibp++;
2765 if (bp + 1 != ibp)
2766 goto randomchar;
2769 /* This # can start a directive. */
2771 --obp; /* Don't copy the '#' */
2773 ip->bufp = ibp;
2774 op->bufp = obp;
2775 if (! handle_directive (ip, op)) {
2776 #ifdef USE_C_ALLOCA
2777 alloca (0);
2778 #endif
2779 /* Not a known directive: treat it as ordinary text.
2780 IP, OP, IBP, etc. have not been changed. */
2781 if (no_output && instack[indepth].fname) {
2782 /* If not generating expanded output,
2783 what we do with ordinary text is skip it.
2784 Discard everything until next # directive. */
2785 skip_if_group (&instack[indepth], 1, 0);
2786 RECACHE;
2787 beg_of_line = ibp;
2788 break;
2790 *obp++ = '#'; /* Copy # (even if it was originally %:). */
2791 /* Don't expand an identifier that could be a macro directive.
2792 (Section 3.8.3 of the ANSI C standard) */
2793 SKIP_WHITE_SPACE (ibp);
2794 if (is_idstart[*ibp])
2796 *obp++ = *ibp++;
2797 while (is_idchar[*ibp])
2798 *obp++ = *ibp++;
2800 goto randomchar;
2802 #ifdef USE_C_ALLOCA
2803 alloca (0);
2804 #endif
2805 /* A # directive has been successfully processed. */
2806 /* If not generating expanded output, ignore everything until
2807 next # directive. */
2808 if (no_output && instack[indepth].fname)
2809 skip_if_group (&instack[indepth], 1, 0);
2810 obp = op->bufp;
2811 RECACHE;
2812 beg_of_line = ibp;
2813 break;
2815 case '\"': /* skip quoted string */
2816 case '\'':
2817 /* A single quoted string is treated like a double -- some
2818 programs (e.g., troff) are perverse this way */
2820 /* Handle any pending identifier;
2821 but the L in L'...' or L"..." is not an identifier. */
2822 if (ident_length) {
2823 if (! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2824 goto specialchar;
2825 ident_length = hash = 0;
2828 start_line = ip->lineno;
2830 /* Skip ahead to a matching quote. */
2832 while (1) {
2833 if (ibp >= limit) {
2834 if (ip->macro != 0) {
2835 /* try harder: this string crosses a macro expansion boundary.
2836 This can happen naturally if -traditional.
2837 Otherwise, only -D can make a macro with an unmatched quote. */
2838 POPMACRO;
2839 RECACHE;
2840 continue;
2842 if (!traditional) {
2843 error_with_line (line_for_error (start_line),
2844 "unterminated string or character constant");
2845 if (multiline_string_line) {
2846 error_with_line (multiline_string_line,
2847 "possible real start of unterminated constant");
2848 multiline_string_line = 0;
2851 break;
2853 *obp++ = *ibp;
2854 switch (*ibp++) {
2855 case '\n':
2856 if (warn_white_space && ip->fname && is_hor_space[ibp[-2]])
2857 warning ("white space at end of line in string");
2858 ++ip->lineno;
2859 ++op->lineno;
2860 /* Traditionally, end of line ends a string constant with no error.
2861 So exit the loop and record the new line. */
2862 if (traditional) {
2863 beg_of_line = ibp;
2864 goto while2end;
2866 if (c == '\'') {
2867 error_with_line (line_for_error (start_line),
2868 "unterminated character constant");
2869 goto while2end;
2871 if (multiline_string_line == 0) {
2872 if (pedantic)
2873 pedwarn_with_line (line_for_error (start_line),
2874 "string constant runs past end of line");
2875 multiline_string_line = ip->lineno - 1;
2877 break;
2879 case '\\':
2880 if (*ibp == '\n') {
2881 /* Backslash newline is replaced by nothing at all, but
2882 keep the line counts correct. But if we are reading
2883 from a macro, keep the backslash newline, since backslash
2884 newlines have already been processed. */
2885 if (ip->macro) {
2886 *obp++ = '\n';
2887 ++op->lineno;
2888 } else
2889 --obp;
2890 ++ibp;
2891 ++ip->lineno;
2892 } else {
2893 /* ANSI stupidly requires that in \\ the second \
2894 is *not* prevented from combining with a newline. */
2895 if (!ip->macro) {
2896 while (*ibp == '\\' && ibp[1] == '\n') {
2897 *obp++ = *ibp++;
2898 *obp++ = *ibp++;
2899 ++ip->lineno;
2900 ++op->lineno;
2903 *obp++ = *ibp++;
2905 break;
2907 case '\"':
2908 case '\'':
2909 if (ibp[-1] == c)
2910 goto while2end;
2911 break;
2912 #ifdef MULTIBYTE_CHARS
2913 default:
2915 int length;
2916 --ibp;
2917 length = local_mblen (ibp, limit - ibp);
2918 if (length > 0)
2920 --obp;
2921 bcopy (ibp, obp, length);
2922 obp += length;
2923 ibp += length;
2925 else
2926 ++ibp;
2928 break;
2929 #endif
2932 while2end:
2933 break;
2935 case '/':
2936 if (ip->macro != 0)
2937 goto randomchar;
2938 if (*ibp == '\\')
2939 newline_fix (ibp);
2940 if (*ibp != '*'
2941 && !(cplusplus_comments && *ibp == '/'))
2942 goto randomchar;
2943 if (ident_length)
2944 goto specialchar;
2946 if (*ibp == '/') {
2947 /* C++ style comment... */
2948 start_line = ip->lineno;
2950 /* Comments are equivalent to spaces. */
2951 if (! put_out_comments)
2952 obp[-1] = ' ';
2955 U_CHAR *before_bp = ibp;
2957 while (++ibp < limit) {
2958 if (*ibp == '\n')
2960 if (put_out_comments) {
2961 bcopy ((const PTR) before_bp, (PTR) obp, ibp - before_bp);
2962 obp += ibp - before_bp;
2964 break;
2966 if (*ibp == '\\')
2968 if (ibp + 1 < limit && ibp[1] == '\n')
2970 if (warn_comments)
2971 warning ("multiline `//' comment");
2972 ++ip->lineno;
2973 /* Copy the newline into the output buffer, in order to
2974 avoid the pain of a #line every time a multiline comment
2975 is seen. */
2976 if (!put_out_comments)
2977 *obp++ = '\n';
2978 ++op->lineno;
2979 ++ibp;
2982 else
2984 #ifdef MULTIBYTE_CHARS
2985 int length;
2986 length = local_mblen (ibp, limit - ibp);
2987 if (length > 1)
2988 ibp += (length - 1);
2989 #endif
2992 break;
2996 /* Ordinary C comment. Skip it, optionally copying it to output. */
2998 start_line = ip->lineno;
3000 ++ibp; /* Skip the star. */
3002 /* If this cpp is for lint, we peek inside the comments: */
3003 if (for_lint) {
3004 const U_CHAR *argbp;
3005 int cmdlen, arglen;
3006 const char *lintcmd =
3007 get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
3009 if (lintcmd != NULL) {
3010 op->bufp = obp;
3011 check_expand (op, cmdlen + arglen + 14);
3012 obp = op->bufp;
3013 /* I believe it is always safe to emit this newline: */
3014 obp[-1] = '\n';
3015 bcopy ("#pragma lint ", (char *) obp, 13);
3016 obp += 13;
3017 bcopy (lintcmd, (char *) obp, cmdlen);
3018 obp += cmdlen;
3020 if (arglen != 0) {
3021 *(obp++) = ' ';
3022 bcopy (argbp, (char *) obp, arglen);
3023 obp += arglen;
3026 /* OK, now bring us back to the state we were in before we entered
3027 this branch. We need #line because the #pragma's newline always
3028 messes up the line count. */
3029 op->bufp = obp;
3030 output_line_directive (ip, op, 0, same_file);
3031 check_expand (op, limit - ibp + 2);
3032 obp = op->bufp;
3033 *(obp++) = '/';
3037 /* Comments are equivalent to spaces.
3038 Note that we already output the slash; we might not want it.
3039 For -traditional, a comment is equivalent to nothing. */
3040 if (! put_out_comments) {
3041 if (traditional)
3042 obp--;
3043 else
3044 obp[-1] = ' ';
3046 else
3047 *obp++ = '*';
3050 U_CHAR *before_bp = ibp;
3052 for (;;) {
3053 switch (*ibp++) {
3054 case '*':
3055 if (ibp[-2] == '/' && warn_comments)
3056 warning ("`/*' within comment");
3057 if (*ibp == '\\')
3058 newline_fix (ibp);
3059 if (*ibp == '/')
3060 goto comment_end;
3061 break;
3063 case '\n':
3064 ++ip->lineno;
3065 /* Copy the newline into the output buffer, in order to
3066 avoid the pain of a #line every time a multiline comment
3067 is seen. */
3068 if (!put_out_comments)
3069 *obp++ = '\n';
3070 ++op->lineno;
3071 break;
3073 case 0:
3074 if (limit < ibp) {
3075 error_with_line (line_for_error (start_line),
3076 "unterminated comment");
3077 goto limit_reached;
3079 break;
3080 #ifdef MULTIBYTE_CHARS
3081 default:
3083 int length;
3084 length = local_mblen (ibp, limit - ibp);
3085 if (length > 1)
3086 ibp += (length - 1);
3088 break;
3089 #endif
3092 comment_end:
3094 ibp++;
3095 if (put_out_comments) {
3096 bcopy ((const PTR) before_bp, (PTR) obp, ibp - before_bp);
3097 obp += ibp - before_bp;
3100 break;
3102 case '$':
3103 if (! is_idchar['$'])
3104 goto randomchar;
3105 if (pedantic)
3106 pedwarn ("`$' in identifier");
3107 goto letter;
3109 case '0': case '1': case '2': case '3': case '4':
3110 case '5': case '6': case '7': case '8': case '9':
3111 /* If digit is not part of identifier, it starts a number,
3112 which means that following letters are not an identifier.
3113 "0x5" does not refer to an identifier "x5".
3114 So copy all alphanumerics that follow without accumulating
3115 as an identifier. Periods also, for sake of "3.e7". */
3117 if (ident_length == 0) {
3118 for (;;) {
3119 if (!ip->macro) {
3120 while (ibp[0] == '\\' && ibp[1] == '\n') {
3121 ++ip->lineno;
3122 ibp += 2;
3125 c = *ibp++;
3126 if (!is_idchar[c] && c != '.') {
3127 --ibp;
3128 break;
3130 *obp++ = c;
3131 /* A sign can be part of a preprocessing number
3132 if it follows an `e' or `p'. */
3133 if (c == 'e' || c == 'E' || c == 'p' || c == 'P') {
3134 if (!ip->macro) {
3135 while (ibp[0] == '\\' && ibp[1] == '\n') {
3136 ++ip->lineno;
3137 ibp += 2;
3140 if (*ibp == '+' || *ibp == '-') {
3141 *obp++ = *ibp++;
3142 /* But traditional C does not let the token go past the sign,
3143 and C89 does not allow `p'. */
3144 if (traditional || (c89 && (c == 'p' || c == 'P')))
3145 break;
3149 break;
3151 /* fall through */
3153 case '_':
3154 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3155 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3156 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3157 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3158 case 'y': case 'z':
3159 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3160 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3161 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3162 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3163 case 'Y': case 'Z':
3164 letter:
3165 ident_length++;
3166 /* Compute step of hash function, to avoid a proc call on every token */
3167 hash = HASHSTEP (hash, c);
3168 break;
3170 case '\n':
3171 if (ip->fname == 0 && *ibp == '-') {
3172 /* Newline - inhibits expansion of preceding token.
3173 If expanding a macro arg, we keep the newline -.
3174 In final output, it is deleted.
3175 We recognize Newline - in macro bodies and macro args. */
3176 if (! concatenated) {
3177 ident_length = 0;
3178 hash = 0;
3180 ibp++;
3181 if (!output_marks) {
3182 obp--;
3183 } else {
3184 /* If expanding a macro arg, keep the newline -. */
3185 *obp++ = '-';
3187 break;
3190 /* If reprocessing a macro expansion, newline is a special marker. */
3191 else if (ip->macro != 0) {
3192 /* Newline White is a "funny space" to separate tokens that are
3193 supposed to be separate but without space between.
3194 Here White means any whitespace character.
3195 Newline - marks a recursive macro use that is not
3196 supposed to be expandable. */
3198 if (is_space[*ibp]) {
3199 /* Newline Space does not prevent expansion of preceding token
3200 so expand the preceding token and then come back. */
3201 if (ident_length > 0)
3202 goto specialchar;
3204 /* If generating final output, newline space makes a space. */
3205 if (!output_marks) {
3206 obp[-1] = *ibp++;
3207 /* And Newline Newline makes a newline, so count it. */
3208 if (obp[-1] == '\n')
3209 op->lineno++;
3210 } else {
3211 /* If expanding a macro arg, keep the newline space.
3212 If the arg gets stringified, newline space makes nothing. */
3213 *obp++ = *ibp++;
3215 } else abort (); /* Newline followed by something random? */
3216 break;
3219 /* If there is a pending identifier, handle it and come back here. */
3220 if (ident_length > 0)
3221 goto specialchar;
3223 beg_of_line = ibp;
3225 /* Update the line counts and output a #line if necessary. */
3226 ++ip->lineno;
3227 ++op->lineno;
3228 if (ip->lineno != op->lineno) {
3229 op->bufp = obp;
3230 output_line_directive (ip, op, 1, same_file);
3231 check_expand (op, limit - ibp);
3232 obp = op->bufp;
3234 break;
3236 /* Come here either after (1) a null character that is part of the input
3237 or (2) at the end of the input, because there is a null there. */
3238 case 0:
3239 if (ibp <= limit)
3240 /* Our input really contains a null character. */
3241 goto randomchar;
3243 limit_reached:
3244 /* At end of a macro-expansion level, pop it and read next level. */
3245 if (ip->macro != 0) {
3246 obp--;
3247 ibp--;
3248 /* If traditional, and we have an identifier that ends here,
3249 process it now, so we get the right error for recursion. */
3250 if (traditional && ident_length
3251 && ! is_idchar[*instack[indepth - 1].bufp]) {
3252 redo_char = 1;
3253 goto randomchar;
3255 POPMACRO;
3256 RECACHE;
3257 break;
3260 /* If we don't have a pending identifier,
3261 return at end of input. */
3262 if (ident_length == 0) {
3263 obp--;
3264 ibp--;
3265 op->bufp = obp;
3266 ip->bufp = ibp;
3267 goto ending;
3270 /* If we do have a pending identifier, just consider this null
3271 a special character and arrange to dispatch on it again.
3272 The second time, IDENT_LENGTH will be zero so we will return. */
3274 /* Fall through */
3276 specialchar:
3278 /* Handle the case of a character such as /, ', " or null
3279 seen following an identifier. Back over it so that
3280 after the identifier is processed the special char
3281 will be dispatched on again. */
3283 ibp--;
3284 obp--;
3285 redo_char = 1;
3287 default:
3289 randomchar:
3291 if (ident_length > 0) {
3292 register HASHNODE *hp;
3294 /* We have just seen an identifier end. If it's a macro, expand it.
3296 IDENT_LENGTH is the length of the identifier
3297 and HASH is its hash code.
3299 The identifier has already been copied to the output,
3300 so if it is a macro we must remove it.
3302 If REDO_CHAR is 0, the char that terminated the identifier
3303 has been skipped in the output and the input.
3304 OBP-IDENT_LENGTH-1 points to the identifier.
3305 If the identifier is a macro, we must back over the terminator.
3307 If REDO_CHAR is 1, the terminating char has already been
3308 backed over. OBP-IDENT_LENGTH points to the identifier. */
3310 if (!pcp_outfile || pcp_inside_if) {
3311 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3312 hp = hp->next) {
3314 if (hp->length == ident_length) {
3315 int obufp_before_macroname;
3316 int op_lineno_before_macroname;
3317 register int i = ident_length;
3318 register U_CHAR *p = hp->name;
3319 register U_CHAR *q = obp - i;
3320 int disabled;
3322 if (! redo_char)
3323 q--;
3325 do { /* All this to avoid a strncmp () */
3326 if (*p++ != *q++)
3327 goto hashcollision;
3328 } while (--i);
3330 /* We found a use of a macro name.
3331 see if the context shows it is a macro call. */
3333 /* Back up over terminating character if not already done. */
3334 if (! redo_char) {
3335 ibp--;
3336 obp--;
3339 /* Save this as a displacement from the beginning of the output
3340 buffer. We can not save this as a position in the output
3341 buffer, because it may get realloc'ed by RECACHE. */
3342 obufp_before_macroname = (obp - op->buf) - ident_length;
3343 op_lineno_before_macroname = op->lineno;
3345 if (hp->type == T_PCSTRING) {
3346 pcstring_used (hp); /* Mark the definition of this key
3347 as needed, ensuring that it
3348 will be output. */
3349 break; /* Exit loop, since the key cannot have a
3350 definition any longer. */
3353 /* Record whether the macro is disabled. */
3354 disabled = hp->type == T_DISABLED;
3356 /* This looks like a macro ref, but if the macro was disabled,
3357 just copy its name and put in a marker if requested. */
3359 if (disabled) {
3360 #if 0
3361 /* This error check caught useful cases such as
3362 #define foo(x,y) bar (x (y,0), y)
3363 foo (foo, baz) */
3364 if (traditional)
3365 error ("recursive use of macro `%s'", hp->name);
3366 #endif
3368 if (output_marks) {
3369 op->bufp = obp;
3370 check_expand (op, limit - ibp + 2);
3371 obp = op->bufp;
3372 *obp++ = '\n';
3373 *obp++ = '-';
3375 break;
3378 /* If macro wants an arglist, verify that a '(' follows.
3379 first skip all whitespace, copying it to the output
3380 after the macro name. Then, if there is no '(',
3381 decide this is not a macro call and leave things that way. */
3382 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3383 && hp->value.defn->nargs >= 0)
3385 U_CHAR *old_ibp = ibp;
3386 U_CHAR *old_obp = obp;
3387 int old_iln = ip->lineno;
3388 int old_oln = op->lineno;
3390 while (1) {
3391 /* Scan forward over whitespace, copying it to the output. */
3392 if (ibp == limit && ip->macro != 0) {
3393 POPMACRO;
3394 RECACHE;
3395 old_ibp = ibp;
3396 old_obp = obp;
3397 old_iln = ip->lineno;
3398 old_oln = op->lineno;
3400 else if (is_space[*ibp]) {
3401 *obp++ = *ibp++;
3402 if (ibp[-1] == '\n') {
3403 if (ip->macro == 0) {
3404 /* Newline in a file. Count it. */
3405 ++ip->lineno;
3406 ++op->lineno;
3407 } else if (!output_marks) {
3408 /* A newline mark, and we don't want marks
3409 in the output. If it is newline-hyphen,
3410 discard it entirely. Otherwise, it is
3411 newline-whitechar, so keep the whitechar. */
3412 obp--;
3413 if (*ibp == '-')
3414 ibp++;
3415 else {
3416 if (*ibp == '\n')
3417 ++op->lineno;
3418 *obp++ = *ibp++;
3420 } else {
3421 /* A newline mark; copy both chars to the output. */
3422 *obp++ = *ibp++;
3426 else if (ip->macro)
3427 break;
3428 else if (*ibp == '/') {
3429 /* If a comment, copy it unchanged or discard it. */
3430 if (ibp[1] == '\\')
3431 newline_fix (ibp + 1);
3432 if (ibp[1] == '*') {
3433 if (put_out_comments) {
3434 *obp++ = '/';
3435 *obp++ = '*';
3436 } else if (! traditional) {
3437 *obp++ = ' ';
3439 for (ibp += 2; ibp < limit; ibp++) {
3440 /* We need not worry about newline-marks,
3441 since they are never found in comments. */
3442 if (ibp[0] == '*') {
3443 if (ibp[1] == '\\')
3444 newline_fix (ibp + 1);
3445 if (ibp[1] == '/') {
3446 ibp += 2;
3447 if (put_out_comments) {
3448 *obp++ = '*';
3449 *obp++ = '/';
3451 break;
3454 else if (*ibp == '\n') {
3455 /* Newline in a file. Count it. */
3456 ++ip->lineno;
3457 ++op->lineno;
3459 else
3461 #ifdef MULTIBYTE_CHARS
3462 int length;
3463 length = local_mblen (ibp, limit - ibp);
3464 if (length > 1)
3466 if (put_out_comments)
3468 bcopy (ibp, obp, length - 1);
3469 obp += length - 1;
3471 ibp += (length - 1);
3473 #endif
3475 if (put_out_comments)
3476 *obp++ = *ibp;
3478 } else if (ibp[1] == '/' && cplusplus_comments) {
3479 if (put_out_comments) {
3480 *obp++ = '/';
3481 *obp++ = '/';
3482 } else if (! traditional) {
3483 *obp++ = ' ';
3485 for (ibp += 2; ; ibp++)
3487 if (*ibp == '\n')
3488 break;
3489 if (*ibp == '\\' && ibp[1] == '\n')
3491 if (put_out_comments)
3492 *obp++ = *ibp++;
3494 else
3496 #ifdef MULTIBYTE_CHARS
3497 int length;
3498 length = local_mblen (ibp, limit - ibp);
3499 if (length > 1)
3501 if (put_out_comments)
3503 bcopy (ibp, obp, length - 1);
3504 obp += length - 1;
3506 ibp += (length - 1);
3508 #endif
3510 if (put_out_comments)
3511 *obp++ = *ibp;
3513 } else
3514 break;
3516 else if (ibp[0] == '\\' && ibp[1] == '\n') {
3517 ibp += 2;
3518 ++ip->lineno;
3520 else break;
3522 if (*ibp != '(') {
3523 /* It isn't a macro call.
3524 Put back the space that we just skipped. */
3525 ibp = old_ibp;
3526 obp = old_obp;
3527 ip->lineno = old_iln;
3528 op->lineno = old_oln;
3529 /* Exit the for loop. */
3530 break;
3534 /* This is now known to be a macro call.
3535 Discard the macro name from the output,
3536 along with any following whitespace just copied,
3537 but preserve newlines if not outputting marks since this
3538 is more likely to do the right thing with line numbers. */
3539 obp = op->buf + obufp_before_macroname;
3540 if (output_marks)
3541 op->lineno = op_lineno_before_macroname;
3542 else {
3543 int newlines = op->lineno - op_lineno_before_macroname;
3544 while (0 < newlines--)
3545 *obp++ = '\n';
3548 /* Prevent accidental token-pasting with a character
3549 before the macro call. */
3550 if (!traditional && obp != op->buf) {
3551 switch (obp[-1]) {
3552 case '!': case '%': case '&': case '*':
3553 case '+': case '-': case '.': case '/':
3554 case ':': case '<': case '=': case '>':
3555 case '^': case '|':
3556 /* If we are expanding a macro arg, make a newline marker
3557 to separate the tokens. If we are making real output,
3558 a plain space will do. */
3559 if (output_marks)
3560 *obp++ = '\n';
3561 *obp++ = ' ';
3565 /* Expand the macro, reading arguments as needed,
3566 and push the expansion on the input stack. */
3567 ip->bufp = ibp;
3568 op->bufp = obp;
3569 macroexpand (hp, op);
3571 /* Reexamine input stack, since macroexpand has pushed
3572 a new level on it. */
3573 obp = op->bufp;
3574 RECACHE;
3575 break;
3577 hashcollision:
3579 } /* End hash-table-search loop */
3581 ident_length = hash = 0; /* Stop collecting identifier */
3582 redo_char = 0;
3583 concatenated = 0;
3584 } /* End if (ident_length > 0) */
3585 } /* End switch */
3586 } /* End per-char loop */
3588 /* Come here to return -- but first give an error message
3589 if there was an unterminated successful conditional. */
3590 ending:
3591 if (if_stack != ip->if_stack)
3593 const char *str;
3595 switch (if_stack->type)
3597 case T_IF:
3598 str = "if";
3599 break;
3600 case T_IFDEF:
3601 str = "ifdef";
3602 break;
3603 case T_IFNDEF:
3604 str = "ifndef";
3605 break;
3606 case T_ELSE:
3607 str = "else";
3608 break;
3609 case T_ELIF:
3610 str = "elif";
3611 break;
3612 default:
3613 abort ();
3616 error_with_line (line_for_error (if_stack->lineno),
3617 "unterminated `#%s' conditional", str);
3619 if_stack = ip->if_stack;
3623 * Rescan a string into a temporary buffer and return the result
3624 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3626 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3627 * and insert such markers when appropriate. See `rescan' for details.
3628 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3629 * before substitution; it is 0 for other uses.
3631 static FILE_BUF
3632 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3633 const U_CHAR *buf;
3634 const U_CHAR *limit;
3635 int output_marks, assertions;
3637 register FILE_BUF *ip;
3638 FILE_BUF obuf;
3639 int length = limit - buf;
3640 U_CHAR *buf1;
3641 int odepth = indepth;
3642 int save_assertions_flag = assertions_flag;
3644 assertions_flag = assertions;
3646 if (length < 0)
3647 abort ();
3649 /* Set up the input on the input stack. */
3651 buf1 = (U_CHAR *) alloca (length + 1);
3653 register const U_CHAR *p1 = buf;
3654 register U_CHAR *p2 = buf1;
3656 while (p1 != limit)
3657 *p2++ = *p1++;
3659 buf1[length] = 0;
3661 /* Set up to receive the output. */
3663 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3664 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3665 obuf.nominal_fname = 0;
3666 obuf.inc = 0;
3667 obuf.dir = 0;
3668 obuf.fname = 0;
3669 obuf.macro = 0;
3670 obuf.if_stack = 0;
3671 obuf.free_ptr = 0;
3672 obuf.system_header_p = 0;
3674 CHECK_DEPTH ({return obuf;});
3676 ++indepth;
3678 ip = &instack[indepth];
3679 ip->fname = 0;
3680 ip->nominal_fname = 0;
3681 ip->nominal_fname_len = 0;
3682 ip->inc = 0;
3683 ip->system_header_p = 0;
3684 ip->macro = 0;
3685 ip->free_ptr = 0;
3686 ip->length = length;
3687 ip->buf = ip->bufp = buf1;
3688 ip->if_stack = if_stack;
3690 ip->lineno = obuf.lineno = 1;
3692 /* Scan the input, create the output. */
3693 rescan (&obuf, output_marks);
3695 /* Pop input stack to original state. */
3696 --indepth;
3698 if (indepth != odepth)
3699 abort ();
3701 assertions_flag = save_assertions_flag;
3702 return obuf;
3706 * Process a # directive. Expects IP->bufp to point after the '#', as in
3707 * `#define foo bar'. Passes to the directive handler
3708 * (do_define, do_include, etc.): the addresses of the 1st and
3709 * last chars of the directive (starting immediately after the #
3710 * keyword), plus op and the keyword table pointer. If the directive
3711 * contains comments it is copied into a temporary buffer sans comments
3712 * and the temporary buffer is passed to the directive handler instead.
3713 * Likewise for backslash-newlines.
3715 * Returns nonzero if this was a known # directive.
3716 * Otherwise, returns zero, without advancing the input pointer.
3719 static int
3720 handle_directive (ip, op)
3721 FILE_BUF *ip, *op;
3723 register U_CHAR *bp, *cp;
3724 register struct directive *kt;
3725 register int ident_length;
3726 U_CHAR *resume_p;
3728 /* Nonzero means we must copy the entire directive
3729 to get rid of comments or backslash-newlines. */
3730 int copy_directive = 0;
3732 U_CHAR *ident, *after_ident;
3734 bp = ip->bufp;
3736 /* Record where the directive started. do_xifdef needs this. */
3737 directive_start = bp - 1;
3739 ignore_escape_flag = 1;
3741 /* Skip whitespace and \-newline. */
3742 while (1) {
3743 if (is_hor_space[*bp]) {
3744 if (*bp != ' ' && *bp != '\t' && pedantic)
3745 pedwarn_strange_white_space (*bp);
3746 bp++;
3747 } else if (*bp == '/') {
3748 if (bp[1] == '\\')
3749 newline_fix (bp + 1);
3750 if (! (bp[1] == '*' || (cplusplus_comments && bp[1] == '/')))
3751 break;
3752 ip->bufp = bp + 2;
3753 skip_to_end_of_comment (ip, &ip->lineno, 0);
3754 bp = ip->bufp;
3755 } else if (*bp == '\\' && bp[1] == '\n') {
3756 bp += 2; ip->lineno++;
3757 } else break;
3760 /* Now find end of directive name.
3761 If we encounter a backslash-newline, exchange it with any following
3762 symbol-constituents so that we end up with a contiguous name. */
3764 cp = bp;
3765 while (1) {
3766 if (is_idchar[*cp])
3767 cp++;
3768 else {
3769 if (*cp == '\\')
3770 name_newline_fix (cp);
3771 if (is_idchar[*cp])
3772 cp++;
3773 else break;
3776 ident_length = cp - bp;
3777 ident = bp;
3778 after_ident = cp;
3780 /* A line of just `#' becomes blank. */
3782 if (ident_length == 0 && *after_ident == '\n') {
3783 ip->bufp = after_ident;
3784 return 1;
3787 if (ident_length == 0 || !is_idstart[*ident]) {
3788 U_CHAR *p = ident;
3789 while (is_idchar[*p]) {
3790 if (*p < '0' || *p > '9')
3791 break;
3792 p++;
3794 /* Handle # followed by a line number. */
3795 if (p != ident && !is_idchar[*p]) {
3796 static struct directive line_directive_table[] = {
3797 { 4, do_line, "line", T_LINE},
3799 if (pedantic)
3800 pedwarn ("`#' followed by integer");
3801 after_ident = ident;
3802 kt = line_directive_table;
3803 ignore_escape_flag = 0;
3804 goto old_linenum;
3807 /* Avoid error for `###' and similar cases unless -pedantic. */
3808 if (p == ident) {
3809 while (*p == '#' || is_hor_space[*p]) p++;
3810 if (*p == '\n') {
3811 if (pedantic && !lang_asm)
3812 warning ("invalid preprocessing directive");
3813 return 0;
3817 if (!lang_asm)
3818 error ("invalid preprocessing directive name");
3820 return 0;
3824 * Decode the keyword and call the appropriate expansion
3825 * routine, after moving the input pointer up to the next line.
3827 for (kt = directive_table; kt->length > 0; kt++) {
3828 if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
3829 register U_CHAR *buf;
3830 register U_CHAR *limit;
3831 int unterminated;
3832 int junk;
3833 int *already_output;
3835 /* Nonzero means do not delete comments within the directive.
3836 #define needs this when -traditional. */
3837 int keep_comments;
3839 old_linenum:
3841 limit = ip->buf + ip->length;
3842 unterminated = 0;
3843 already_output = 0;
3844 keep_comments = traditional && kt->type == T_DEFINE;
3845 /* #import is defined only in Objective C, or when on the NeXT. */
3846 if (kt->type == T_IMPORT
3847 && !(objc || lookup ((const U_CHAR *) "__NeXT__", -1, -1)))
3848 break;
3850 /* Find the end of this directive (first newline not backslashed
3851 and not in a string or comment).
3852 Set COPY_DIRECTIVE if the directive must be copied
3853 (it contains a backslash-newline or a comment). */
3855 buf = bp = after_ident;
3856 while (bp < limit) {
3857 register U_CHAR c = *bp++;
3858 switch (c) {
3859 case '\\':
3860 if (*bp == '\n') {
3861 ip->lineno++;
3862 copy_directive = 1;
3863 bp++;
3864 } else if (traditional && bp < limit)
3865 bp++;
3866 break;
3868 case '"':
3869 /* "..." is special for #include. */
3870 if (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)) {
3871 while (bp < limit && *bp != '\n') {
3872 if (*bp == '"') {
3873 bp++;
3874 break;
3876 if (*bp == '\\' && bp[1] == '\n') {
3877 ip->lineno++;
3878 copy_directive = 1;
3879 bp++;
3881 bp++;
3883 break;
3885 /* Fall through. */
3886 case '\'':
3887 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_directive, &unterminated);
3888 /* Don't bother calling the directive if we already got an error
3889 message due to unterminated string. Skip everything and pretend
3890 we called the directive. */
3891 if (unterminated) {
3892 if (traditional) {
3893 /* Traditional preprocessing permits unterminated strings. */
3894 ip->bufp = bp;
3895 goto endloop1;
3897 ip->bufp = bp;
3898 return 1;
3900 break;
3902 /* <...> is special for #include. */
3903 case '<':
3904 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3905 break;
3906 while (bp < limit && *bp != '>' && *bp != '\n') {
3907 if (*bp == '\\' && bp[1] == '\n') {
3908 ip->lineno++;
3909 copy_directive = 1;
3910 bp++;
3912 bp++;
3914 break;
3916 case '/':
3917 if (*bp == '\\')
3918 newline_fix (bp);
3919 if (*bp == '*'
3920 || (cplusplus_comments && *bp == '/')) {
3921 U_CHAR *obp = bp - 1;
3922 ip->bufp = bp + 1;
3923 skip_to_end_of_comment (ip, &ip->lineno, 0);
3924 bp = ip->bufp;
3925 /* No need to copy the directive because of a comment at the end;
3926 just don't include the comment in the directive. */
3927 if (!put_out_comments) {
3928 U_CHAR *p;
3929 for (p = bp; *p == ' ' || *p == '\t'; p++)
3930 continue;
3931 if (*p == '\n') {
3932 bp = obp;
3933 goto endloop1;
3936 /* Don't remove the comments if -traditional. */
3937 if (! keep_comments)
3938 copy_directive++;
3940 break;
3942 case '\f':
3943 case '\r':
3944 case '\v':
3945 if (pedantic)
3946 pedwarn_strange_white_space (c);
3947 break;
3949 case '\n':
3950 --bp; /* Point to the newline */
3951 ip->bufp = bp;
3952 goto endloop1;
3955 ip->bufp = bp;
3957 endloop1:
3958 resume_p = ip->bufp;
3959 /* BP is the end of the directive.
3960 RESUME_P is the next interesting data after the directive.
3961 A comment may come between. */
3963 /* If a directive should be copied through, and -C was given,
3964 pass it through before removing comments. */
3965 if (!no_output && put_out_comments
3966 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
3967 ? dump_macros == dump_definitions
3968 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
3969 : kt->type == T_PRAGMA)) {
3970 int len;
3972 /* Output directive name. */
3973 check_expand (op, kt->length + 2);
3974 /* Make sure # is at the start of a line */
3975 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3976 op->lineno++;
3977 *op->bufp++ = '\n';
3979 *op->bufp++ = '#';
3980 bcopy (kt->name, op->bufp, kt->length);
3981 op->bufp += kt->length;
3983 /* Output arguments. */
3984 len = (bp - buf);
3985 check_expand (op, len);
3986 bcopy (buf, (char *) op->bufp, len);
3987 op->bufp += len;
3988 /* Take account of any (escaped) newlines just output. */
3989 while (--len >= 0)
3990 if (buf[len] == '\n')
3991 op->lineno++;
3993 already_output = &junk;
3994 } /* Don't we need a newline or #line? */
3996 if (copy_directive) {
3997 register U_CHAR *xp = buf;
3998 /* Need to copy entire directive into temp buffer before dispatching */
4000 /* room for directive plus some slop */
4001 cp = (U_CHAR *) alloca (2 * (bp - buf) + 5);
4002 buf = cp;
4004 /* Copy to the new buffer, deleting comments
4005 and backslash-newlines (and whitespace surrounding the latter
4006 if outside of char and string constants). */
4008 while (xp < bp) {
4009 register U_CHAR c = *xp++;
4010 *cp++ = c;
4012 switch (c) {
4013 case '\n':
4014 abort (); /* A bare newline should never part of the line. */
4015 break;
4017 /* <...> is special for #include. */
4018 case '<':
4019 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
4020 break;
4021 while (xp < bp && c != '>') {
4022 c = *xp++;
4023 if (c == '\\' && xp < bp && *xp == '\n')
4024 xp++;
4025 else
4026 *cp++ = c;
4028 break;
4030 case '\\':
4031 if (*xp == '\n') {
4032 xp++;
4033 cp--;
4034 if (cp != buf && is_hor_space[cp[-1]]) {
4035 while (cp - 1 != buf && is_hor_space[cp[-2]])
4036 cp--;
4037 SKIP_WHITE_SPACE (xp);
4038 } else if (is_hor_space[*xp]) {
4039 *cp++ = *xp++;
4040 SKIP_WHITE_SPACE (xp);
4042 } else if (traditional && xp < bp) {
4043 *cp++ = *xp++;
4045 break;
4047 case '\'':
4048 case '\"':
4050 int backslash_newlines_p = 0;
4052 register const U_CHAR *bp1
4053 = skip_quoted_string (xp - 1, bp, ip->lineno,
4054 NULL_PTR, &backslash_newlines_p,
4055 NULL_PTR);
4056 if (backslash_newlines_p)
4057 while (xp != bp1)
4059 /* With something like:
4061 #define X "a\
4064 we should still remove the backslash-newline
4065 pair as part of phase two. */
4066 if (xp[0] == '\\' && xp[1] == '\n')
4067 xp += 2;
4068 else
4069 *cp++ = *xp++;
4071 else
4072 /* This is the same as the loop above, but taking
4073 advantage of the fact that we know there are no
4074 backslash-newline pairs. */
4075 while (xp != bp1)
4076 *cp++ = *xp++;
4078 break;
4080 case '/':
4081 if (*xp == '*'
4082 || (cplusplus_comments && *xp == '/')) {
4083 ip->bufp = xp + 1;
4084 /* If we already copied the directive through,
4085 already_output != 0 prevents outputting comment now. */
4086 skip_to_end_of_comment (ip, already_output, 0);
4087 if (keep_comments)
4088 while (xp != ip->bufp)
4089 *cp++ = *xp++;
4090 /* Delete or replace the slash. */
4091 else if (traditional)
4092 cp--;
4093 else
4094 cp[-1] = ' ';
4095 xp = ip->bufp;
4100 /* Null-terminate the copy. */
4102 *cp = 0;
4103 } else
4104 cp = bp;
4106 ip->bufp = resume_p;
4108 /* Some directives should be written out for cc1 to process,
4109 just as if they were not defined. And sometimes we're copying
4110 directives through. */
4112 if (!no_output && already_output == 0
4113 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
4114 ? (int) dump_names <= (int) dump_macros
4115 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4116 : kt->type == T_PRAGMA)) {
4117 int len;
4119 /* Output directive name. */
4120 check_expand (op, kt->length + 1);
4121 *op->bufp++ = '#';
4122 bcopy (kt->name, (char *) op->bufp, kt->length);
4123 op->bufp += kt->length;
4125 if (kt->type == T_DEFINE && dump_macros == dump_names) {
4126 /* Output `#define name' only. */
4127 U_CHAR *xp = buf;
4128 U_CHAR *yp;
4129 SKIP_WHITE_SPACE (xp);
4130 yp = xp;
4131 while (is_idchar[*xp]) xp++;
4132 len = (xp - yp);
4133 check_expand (op, len + 1);
4134 *op->bufp++ = ' ';
4135 bcopy (yp, (char *) op->bufp, len);
4136 } else {
4137 /* Output entire directive. */
4138 len = (cp - buf);
4139 check_expand (op, len);
4140 bcopy (buf, (char *) op->bufp, len);
4142 op->bufp += len;
4145 /* Call the appropriate directive handler. buf now points to
4146 either the appropriate place in the input buffer, or to
4147 the temp buffer if it was necessary to make one. cp
4148 points to the first char after the contents of the (possibly
4149 copied) directive, in either case. */
4150 (*kt->func) (buf, cp, op, kt);
4151 check_expand (op, ip->length - (ip->bufp - ip->buf));
4153 return 1;
4157 /* It is deliberate that we don't warn about undefined directives.
4158 That is the responsibility of cc1. */
4159 return 0;
4162 static struct tm *
4163 timestamp ()
4165 static struct tm tmbuf;
4166 if (! tmbuf.tm_mday) {
4167 time_t t = time ((time_t *) 0);
4168 struct tm *tm = localtime (&t);
4169 if (tm)
4170 tmbuf = *tm;
4171 else {
4172 /* Use 0000-01-01 00:00:00 if local time is not available. */
4173 tmbuf.tm_year = -1900;
4174 tmbuf.tm_mday = 1;
4177 return &tmbuf;
4180 static const char * const monthnames[] = {
4181 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4182 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4186 * expand things like __FILE__. Place the expansion into the output
4187 * buffer *without* rescanning.
4190 static void
4191 special_symbol (hp, op)
4192 HASHNODE *hp;
4193 FILE_BUF *op;
4195 const char *buf;
4196 int i, len;
4197 int true_indepth;
4198 FILE_BUF *ip = NULL;
4199 struct tm *timebuf;
4201 int paren = 0; /* For special `defined' keyword */
4203 if (pcp_outfile && pcp_inside_if
4204 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
4205 error ("Predefined macro `%s' used inside `#if' during precompilation",
4206 hp->name);
4208 for (i = indepth; i >= 0; i--)
4209 if (instack[i].fname != NULL) {
4210 ip = &instack[i];
4211 break;
4213 if (ip == NULL) {
4214 error ("cccp error: not in any file?!");
4215 return; /* the show must go on */
4218 switch (hp->type) {
4219 case T_FILE:
4220 case T_BASE_FILE:
4222 FILE_BUF *p = hp->type == T_FILE ? ip : &instack[0];
4223 const char *string = p->nominal_fname;
4225 if (string)
4227 size_t string_len = p->nominal_fname_len;
4228 char *newbuf = (char *) alloca (3 + 4 * string_len);
4229 quote_string (newbuf, string, string_len);
4230 buf = newbuf;
4232 else
4233 buf = "\"\"";
4235 break;
4238 case T_INCLUDE_LEVEL:
4240 /* Eight bytes ought to be more than enough */
4241 char *newbuf = (char *) alloca (8);
4242 true_indepth = 0;
4243 for (i = indepth; i >= 0; i--)
4244 if (instack[i].fname != NULL)
4245 true_indepth++;
4246 sprintf (newbuf, "%d", true_indepth - 1);
4247 buf = newbuf;
4249 break;
4251 case T_VERSION:
4253 char *newbuf = (char *) alloca (3 + strlen (version_string));
4254 sprintf (newbuf, "\"%s\"", version_string);
4255 buf = newbuf;
4257 break;
4259 #ifndef NO_BUILTIN_SIZE_TYPE
4260 case T_SIZE_TYPE:
4261 buf = SIZE_TYPE;
4262 break;
4263 #endif
4265 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4266 case T_PTRDIFF_TYPE:
4267 buf = PTRDIFF_TYPE;
4268 break;
4269 #endif
4271 #ifndef NO_BUILTIN_WCHAR_TYPE
4272 case T_WCHAR_TYPE:
4273 buf = wchar_type;
4274 break;
4275 #endif
4277 case T_USER_LABEL_PREFIX_TYPE:
4278 buf = user_label_prefix;
4279 break;
4281 case T_REGISTER_PREFIX_TYPE:
4282 buf = REGISTER_PREFIX;
4283 break;
4285 case T_IMMEDIATE_PREFIX_TYPE:
4286 buf = IMMEDIATE_PREFIX;
4287 break;
4289 case T_CONST:
4290 buf = hp->value.cpval;
4291 #ifdef STDC_0_IN_SYSTEM_HEADERS
4292 if (ip->system_header_p
4293 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
4294 && !lookup ((const U_CHAR *) "__STRICT_ANSI__", -1, -1))
4295 buf = "0";
4296 #endif
4297 if (pcp_inside_if && pcp_outfile)
4298 /* Output a precondition for this macro use */
4299 fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
4300 break;
4302 case T_SPECLINE:
4304 char *newbuf = (char *) alloca (10);
4305 sprintf (newbuf, "%d", ip->lineno);
4306 buf = newbuf;
4308 break;
4310 case T_DATE:
4311 case T_TIME:
4313 char *newbuf = (char *) alloca (20);
4314 timebuf = timestamp ();
4315 if (hp->type == T_DATE)
4316 sprintf (newbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
4317 timebuf->tm_mday, timebuf->tm_year + 1900);
4318 else
4319 sprintf (newbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour,
4320 timebuf->tm_min, timebuf->tm_sec);
4321 buf = newbuf;
4323 break;
4325 case T_SPEC_DEFINED:
4326 buf = " 0 "; /* Assume symbol is not defined */
4327 ip = &instack[indepth];
4328 SKIP_WHITE_SPACE (ip->bufp);
4329 if (*ip->bufp == '(') {
4330 paren++;
4331 ip->bufp++; /* Skip over the paren */
4332 SKIP_WHITE_SPACE (ip->bufp);
4335 if (!is_idstart[*ip->bufp])
4336 goto oops;
4337 if (ip->bufp[0] == 'L' && (ip->bufp[1] == '\'' || ip->bufp[1] == '"'))
4338 goto oops;
4339 if ((hp = lookup (ip->bufp, -1, -1))) {
4340 if (pcp_outfile && pcp_inside_if
4341 && (hp->type == T_CONST
4342 || (hp->type == T_MACRO && hp->value.defn->predefined)))
4343 /* Output a precondition for this macro use. */
4344 fprintf (pcp_outfile, "#define %s\n", hp->name);
4345 if (hp->type == T_POISON) {
4346 error("attempt to use poisoned `%s'.", hp->name);
4347 buf = " 0 ";
4348 } else {
4349 buf = " 1 ";
4352 else
4353 if (pcp_outfile && pcp_inside_if) {
4354 /* Output a precondition for this macro use */
4355 U_CHAR *cp = ip->bufp;
4356 fprintf (pcp_outfile, "#undef ");
4357 while (is_idchar[*cp]) /* Ick! */
4358 fputc (*cp++, pcp_outfile);
4359 putc ('\n', pcp_outfile);
4361 while (is_idchar[*ip->bufp])
4362 ++ip->bufp;
4363 SKIP_WHITE_SPACE (ip->bufp);
4364 if (paren) {
4365 if (*ip->bufp != ')')
4366 goto oops;
4367 ++ip->bufp;
4369 break;
4371 case T_POISON:
4372 error("attempt to use poisoned `%s'.", hp->name);
4373 buf = " 0 "; /* Consider poisoned symbol to not be defined */
4374 break;
4376 oops:
4378 error ("`defined' without an identifier");
4379 break;
4381 default:
4382 error ("cccp error: invalid special hash type"); /* time for gdb */
4383 abort ();
4385 len = strlen (buf);
4386 check_expand (op, len);
4387 bcopy (buf, (char *) op->bufp, len);
4388 op->bufp += len;
4390 return;
4394 /* Routines to handle #directives */
4396 /* Handle #include and #import.
4397 This function expects to see "fname" or <fname> on the input. */
4399 static int
4400 do_include (buf, limit, op, keyword)
4401 U_CHAR *buf, *limit;
4402 FILE_BUF *op;
4403 struct directive *keyword;
4405 const U_CHAR *importing =
4406 keyword->type == T_IMPORT ? (const U_CHAR *) "" : (const U_CHAR *) 0;
4407 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4408 static int import_warning = 0;
4409 char *fname; /* Dynamically allocated fname buffer */
4410 char *pcftry;
4411 char *pcfname;
4412 char *fbeg, *fend; /* Beginning and end of fname */
4413 U_CHAR *fin;
4415 struct file_name_list *search_start = include; /* Chain of dirs to search */
4416 struct file_name_list *dsp; /* First in chain, if #include "..." */
4417 struct file_name_list *searchptr = 0;
4418 size_t flen;
4420 int f = -3; /* file number */
4421 struct include_file *inc = 0;
4423 int retried = 0; /* Have already tried macro
4424 expanding the include line*/
4425 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4426 #ifdef VMS
4427 int vaxc_include = 0; /* 1 for token without punctuation */
4428 #endif
4429 int pcf = -1;
4430 char *pcfbuf;
4431 const char *pcfbuflimit;
4432 int pcfnum;
4434 if (pedantic && !instack[indepth].system_header_p)
4436 if (importing)
4437 pedwarn ("ANSI C does not allow `#import'");
4438 if (skip_dirs)
4439 pedwarn ("ANSI C does not allow `#include_next'");
4442 if (importing && warn_import && !inhibit_warnings
4443 && !instack[indepth].system_header_p && !import_warning) {
4444 import_warning = 1;
4445 warning ("using `#import' is not recommended");
4446 notice ("The fact that a certain header file need not be processed more than once\n\
4447 should be indicated in the header file, not where it is used.\n\
4448 The best way to do this is with a conditional of this form:\n\
4450 #ifndef _FOO_H_INCLUDED\n\
4451 #define _FOO_H_INCLUDED\n\
4452 ... <real contents of file> ...\n\
4453 #endif /* Not _FOO_H_INCLUDED */\n\
4455 Then users can use `#include' any number of times.\n\
4456 GNU C automatically avoids processing the file more than once\n\
4457 when it is equipped with such a conditional.\n");
4460 get_filename:
4462 fin = buf;
4463 SKIP_WHITE_SPACE (fin);
4464 /* Discard trailing whitespace so we can easily see
4465 if we have parsed all the significant chars we were given. */
4466 while (limit != fin && is_hor_space[limit[-1]]) limit--;
4467 fbeg = fend = (char *) alloca (limit - fin);
4469 switch (*fin++) {
4470 case '\"':
4472 FILE_BUF *fp;
4473 /* Copy the operand text, concatenating the strings. */
4475 for (;;) {
4476 for (;;) {
4477 if (fin == limit)
4478 goto invalid_include_file_name;
4479 *fend = *fin++;
4480 if (*fend == '"')
4481 break;
4482 fend++;
4484 if (fin == limit)
4485 break;
4486 /* If not at the end, there had better be another string. */
4487 /* Skip just horiz space, and don't go past limit. */
4488 while (fin != limit && is_hor_space[*fin]) fin++;
4489 if (fin != limit && *fin == '\"')
4490 fin++;
4491 else
4492 goto fail;
4496 /* We have "filename". Figure out directory this source
4497 file is coming from and put it on the front of the list. */
4499 /* If -I- was specified, don't search current dir, only spec'd ones. */
4500 if (ignore_srcdir) break;
4502 for (fp = &instack[indepth]; fp >= instack; fp--)
4504 int n;
4506 if ((fp->nominal_fname) != NULL) {
4507 char *nam;
4508 /* Found a named file. Figure out dir of the file,
4509 and put it in front of the search list. */
4510 dsp = ((struct file_name_list *)
4511 alloca (sizeof (struct file_name_list)
4512 + fp->nominal_fname_len));
4513 strcpy (dsp->fname, fp->nominal_fname);
4514 simplify_filename (dsp->fname);
4515 nam = base_name (dsp->fname);
4516 *nam = 0;
4517 #ifdef VMS
4518 /* for hack_vms_include_specification(), a local
4519 dir specification must start with "./" on VMS. */
4520 if (nam == dsp->fname)
4522 *nam++ = '.';
4523 *nam++ = '/';
4524 *nam = 0;
4526 #endif
4527 /* But for efficiency's sake, do not insert the dir
4528 if it matches the search list's first dir. */
4529 dsp->next = search_start;
4530 if (!search_start || strcmp (dsp->fname, search_start->fname)) {
4531 search_start = dsp;
4532 n = nam - dsp->fname;
4533 if (n + INCLUDE_LEN_FUDGE > max_include_len)
4534 max_include_len = n + INCLUDE_LEN_FUDGE;
4536 dsp[0].got_name_map = 0;
4537 break;
4540 break;
4543 case '<':
4544 while (fin != limit && *fin != '>')
4545 *fend++ = *fin++;
4546 if (*fin == '>' && fin + 1 == limit) {
4547 angle_brackets = 1;
4548 /* If -I-, start with the first -I dir after the -I-. */
4549 search_start = first_bracket_include;
4550 break;
4552 goto fail;
4554 default:
4555 #ifdef VMS
4557 * Support '#include xyz' like VAX-C to allow for easy use of all the
4558 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4559 * code from case '<' is repeated here) and generates a warning.
4560 * (Note: macro expansion of `xyz' takes precedence.)
4562 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4563 the pre-decrement outside of the macro. */
4564 if (retried && (--fin, ISALPHA(*(U_CHAR *) (fin)))) {
4565 while (fin != limit && (!ISSPACE(*fin)))
4566 *fend++ = *fin++;
4567 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4568 vaxc_include = 1;
4569 if (fin == limit) {
4570 angle_brackets = 1;
4571 /* If -I-, start with the first -I dir after the -I-. */
4572 search_start = first_bracket_include;
4573 break;
4576 #endif
4578 fail:
4579 if (! retried) {
4580 /* Expand buffer and then remove any newline markers.
4581 We can't just tell expand_to_temp_buffer to omit the markers,
4582 since it would put extra spaces in include file names. */
4583 U_CHAR *src;
4584 int errors_before_expansion = errors;
4585 FILE_BUF trybuf;
4587 trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
4588 if (errors != errors_before_expansion) {
4589 free (trybuf.buf);
4590 goto invalid_include_file_name;
4592 src = trybuf.buf;
4593 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
4594 limit = buf;
4595 while (src != trybuf.bufp) {
4596 switch ((*limit++ = *src++)) {
4597 case '\n':
4598 limit--;
4599 src++;
4600 break;
4602 case '\'':
4603 case '\"':
4605 const U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
4606 NULL_PTR, NULL_PTR, NULL_PTR);
4607 while (src != src1)
4608 *limit++ = *src++;
4610 break;
4613 *limit = 0;
4614 free (trybuf.buf);
4615 retried = 1;
4616 goto get_filename;
4619 invalid_include_file_name:
4620 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
4621 return 0;
4624 /* For #include_next, skip in the search path
4625 past the dir in which the containing file was found. */
4626 if (skip_dirs) {
4627 FILE_BUF *fp;
4628 for (fp = &instack[indepth]; fp >= instack; fp--)
4629 if (fp->fname != NULL) {
4630 /* fp->dir is null if the containing file was specified
4631 with an absolute file name. In that case, don't skip anything. */
4632 if (fp->dir)
4633 search_start = fp->dir->next;
4634 break;
4638 *fend = 0;
4639 flen = simplify_filename (fbeg);
4641 if (flen == 0)
4643 error ("empty file name in `#%s'", keyword->name);
4644 return 0;
4647 /* Allocate this permanently, because it gets stored in the definitions
4648 of macros. */
4649 fname = xmalloc (max_include_len + flen + 1);
4650 /* + 1 above for terminating null. */
4652 system_include_depth += angle_brackets;
4654 /* If specified file name is absolute, just open it. */
4656 if (absolute_filename (fbeg)) {
4657 strcpy (fname, fbeg);
4658 f = open_include_file (fname, NULL_PTR, importing, &inc);
4659 } else {
4661 struct bypass_dir {
4662 struct bypass_dir *next;
4663 char *fname;
4664 struct file_name_list *searchptr;
4665 } **bypass_slot = 0;
4667 /* Search directory path, trying to open the file.
4668 Copy each filename tried into FNAME. */
4670 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
4672 if (searchptr == first_bracket_include) {
4673 /* Go to bypass directory if we know we've seen this file before. */
4674 static struct bypass_dir *bypass_hashtab[INCLUDE_HASHSIZE];
4675 struct bypass_dir *p;
4676 bypass_slot = &bypass_hashtab[hashf ((U_CHAR *) fbeg, flen,
4677 INCLUDE_HASHSIZE)];
4678 for (p = *bypass_slot; p; p = p->next)
4679 if (!strcmp (fbeg, p->fname)) {
4680 searchptr = p->searchptr;
4681 bypass_slot = 0;
4682 break;
4686 #ifdef VMS
4687 /* Change this 1/2 Unix 1/2 VMS file specification into a
4688 full VMS file specification */
4689 if (searchptr->fname[0])
4691 strcpy (fname, searchptr->fname);
4692 if (fname[strlen (fname) - 1] == ':')
4694 char *slashp;
4695 slashp = strchr (fbeg, '/');
4697 /* start at root-dir of logical device if no path given. */
4698 if (slashp == 0)
4699 strcat (fname, "[000000]");
4701 strcat (fname, fbeg);
4703 /* Fix up the filename */
4704 hack_vms_include_specification (fname, vaxc_include);
4706 else
4708 /* This is a normal VMS filespec, so use it unchanged. */
4709 strcpy (fname, fbeg);
4710 /* if it's '#include filename', add the missing .h */
4711 if (vaxc_include && index(fname,'.')==NULL)
4712 strcat (fname, ".h");
4714 #else
4715 strcpy (fname, searchptr->fname);
4716 strcat (fname, fbeg);
4717 #endif /* VMS */
4718 f = open_include_file (fname, searchptr, importing, &inc);
4719 if (f != -1) {
4720 if (bypass_slot && searchptr != first_bracket_include) {
4721 /* This is the first time we found this include file,
4722 and we found it after first_bracket_include.
4723 Record its location so that we can bypass to here next time. */
4724 struct bypass_dir *p
4725 = (struct bypass_dir *) xmalloc (sizeof (struct bypass_dir));
4726 p->next = *bypass_slot;
4727 p->fname = fname + strlen (searchptr->fname);
4728 p->searchptr = searchptr;
4729 *bypass_slot = p;
4731 break;
4733 #ifdef VMS
4734 /* Our VMS hacks can produce invalid filespecs, so don't worry
4735 about errors other than EACCES. */
4736 if (errno == EACCES)
4737 break;
4738 #else
4739 if (errno != ENOENT && errno != ENOTDIR)
4740 break;
4741 #endif
4746 if (f < 0) {
4748 if (f == -2) {
4749 /* The file was already included. */
4751 /* If generating dependencies and -MG was specified, we assume missing
4752 files are leaf files, living in the same directory as the source file
4753 or other similar place; these missing files may be generated from
4754 other files and may not exist yet (eg: y.tab.h). */
4755 } else if (print_deps_missing_files
4756 && (system_include_depth != 0) < print_deps)
4758 /* If it was requested as a system header file,
4759 then assume it belongs in the first place to look for such. */
4760 if (angle_brackets)
4762 if (search_start) {
4763 char *p = (char *) alloca (strlen (search_start->fname)
4764 + strlen (fbeg) + 1);
4765 strcpy (p, search_start->fname);
4766 strcat (p, fbeg);
4767 deps_output (p, ' ');
4770 else
4772 /* Otherwise, omit the directory, as if the file existed
4773 in the directory with the source. */
4774 deps_output (fbeg, ' ');
4777 /* If -M was specified, and this header file won't be added to the
4778 dependency list, then don't count this as an error, because we can
4779 still produce correct output. Otherwise, we can't produce correct
4780 output, because there may be dependencies we need inside the missing
4781 file, and we don't know what directory this missing file exists in. */
4782 else if (0 < print_deps && print_deps <= (system_include_depth != 0))
4783 warning ("No include path in which to find %s", fbeg);
4784 else if (f != -3)
4785 error_from_errno (fbeg);
4786 else
4787 error ("No include path in which to find %s", fbeg);
4789 } else {
4791 /* Actually process the file. */
4793 pcftry = (char *) alloca (strlen (fname) + 30);
4794 pcfbuf = 0;
4795 pcfnum = 0;
4797 if (!no_precomp)
4799 do {
4800 sprintf (pcftry, "%s%d", fname, pcfnum++);
4802 pcf = open (pcftry, O_RDONLY, 0666);
4803 if (pcf != -1)
4805 struct stat s;
4807 if (fstat (pcf, &s) != 0)
4808 pfatal_with_name (pcftry);
4809 if (! INO_T_EQ (inc->st.st_ino, s.st_ino)
4810 || inc->st.st_dev != s.st_dev)
4812 pcfbuf = check_precompiled (pcf, &s, fname, &pcfbuflimit);
4813 /* Don't need it any more. */
4814 close (pcf);
4816 else
4818 /* Don't need it at all. */
4819 close (pcf);
4820 break;
4823 } while (pcf != -1 && !pcfbuf);
4826 /* Actually process the file */
4827 if (pcfbuf) {
4828 pcfname = xstrdup (pcftry);
4829 pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) fname, op);
4831 else
4832 finclude (f, inc, op, is_system_include (fname), searchptr);
4835 system_include_depth -= angle_brackets;
4837 return 0;
4840 /* Return nonzero if the given FILENAME is an absolute pathname which
4841 designates a file within one of the known "system" include file
4842 directories. We assume here that if the given FILENAME looks like
4843 it is the name of a file which resides either directly in a "system"
4844 include file directory, or within any subdirectory thereof, then the
4845 given file must be a "system" include file. This function tells us
4846 if we should suppress pedantic errors/warnings for the given FILENAME.
4848 The value is 2 if the file is a C-language system header file
4849 for which C++ should (on most systems) assume `extern "C"'. */
4851 static int
4852 is_system_include (filename)
4853 register const char *filename;
4855 struct file_name_list *searchptr;
4857 for (searchptr = first_system_include; searchptr;
4858 searchptr = searchptr->next)
4859 if (! strncmp (searchptr->fname, filename, strlen (searchptr->fname)))
4860 return searchptr->c_system_include_path + 1;
4861 return 0;
4864 /* Yield the non-directory suffix of a file name. */
4866 static char *
4867 base_name (fname)
4868 const char *fname;
4870 const char *s = fname;
4871 const char *p;
4872 #if defined (__MSDOS__) || defined (_WIN32)
4873 if (ISALPHA (s[0]) && s[1] == ':') s += 2;
4874 #endif
4875 #ifdef VMS
4876 if ((p = rindex (s, ':'))) s = p + 1; /* Skip device. */
4877 if ((p = rindex (s, ']'))) s = p + 1; /* Skip directory. */
4878 if ((p = rindex (s, '>'))) s = p + 1; /* Skip alternate (int'n'l) dir. */
4879 if (s != fname)
4880 return (char *) s;
4881 #endif
4882 if ((p = rindex (s, '/'))) s = p + 1;
4883 #ifdef DIR_SEPARATOR
4884 if ((p = rindex (s, DIR_SEPARATOR))) s = p + 1;
4885 #endif
4886 return (char *) s;
4889 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4891 static int
4892 absolute_filename (filename)
4893 const char *filename;
4895 #if defined (__MSDOS__) \
4896 || (defined (_WIN32) && !defined (__CYGWIN__) && !defined (_UWIN))
4897 if (ISALPHA (filename[0]) && filename[1] == ':') filename += 2;
4898 #endif
4899 #if defined (__CYGWIN__)
4900 /* At present, any path that begins with a drive spec is absolute. */
4901 if (ISALPHA (filename[0]) && filename[1] == ':') return 1;
4902 #endif
4903 #ifdef VMS
4904 if (index (filename, ':') != 0) return 1;
4905 #endif
4906 if (filename[0] == '/') return 1;
4907 #ifdef DIR_SEPARATOR
4908 if (filename[0] == DIR_SEPARATOR) return 1;
4909 #endif
4910 return 0;
4913 /* Returns whether or not a given character is a directory separator.
4914 Used by simplify_filename. */
4915 static inline int is_dir_separator PARAMS ((int));
4917 static inline
4919 is_dir_separator(ch)
4920 char ch;
4922 return (ch == DIR_SEPARATOR)
4923 #if defined (DIR_SEPARATOR_2)
4924 || (ch == DIR_SEPARATOR_2)
4925 #endif
4929 /* Remove unnecessary characters from FILENAME in place,
4930 to avoid unnecessary filename aliasing.
4931 Return the length of the resulting string.
4933 Do only the simplifications allowed by Posix.
4934 It is OK to miss simplifications on non-Posix hosts,
4935 since this merely leads to suboptimal results. */
4937 static size_t
4938 simplify_filename (filename)
4939 char *filename;
4941 register char *from = filename;
4942 register char *to = filename;
4943 char *to0;
4945 /* Remove redundant initial /s. */
4946 if (is_dir_separator (*from))
4948 *to++ = DIR_SEPARATOR;
4949 if (is_dir_separator (*++from))
4951 if (is_dir_separator (*++from))
4953 /* 3 or more initial /s are equivalent to 1 /. */
4954 while (is_dir_separator (*++from))
4955 continue;
4957 else
4959 /* On some hosts // differs from /; Posix allows this. */
4960 *to++ = DIR_SEPARATOR;
4965 to0 = to;
4967 for (;;)
4969 #ifndef VMS
4970 if (from[0] == '.' && from[1] == '/')
4971 from += 2;
4972 else
4973 #endif
4975 /* Copy this component and trailing DIR_SEPARATOR, if any. */
4976 while (!is_dir_separator (*to++ = *from++))
4978 if (!to[-1])
4980 /* Trim . component at end of nonempty name. */
4981 to -= filename <= to - 3 && to[-3] == DIR_SEPARATOR && to[-2] == '.';
4983 /* Trim unnecessary trailing /s. */
4984 while (to0 < --to && to[-1] == DIR_SEPARATOR)
4985 continue;
4987 *to = 0;
4988 return to - filename;
4991 #if defined(DIR_SEPARATOR_2)
4992 /* Simplify to one directory separator. */
4993 to[-1] = DIR_SEPARATOR;
4994 #endif
4997 /* Skip /s after a /. */
4998 while (is_dir_separator (*from))
4999 from++;
5003 /* The file_name_map structure holds a mapping of file names for a
5004 particular directory. This mapping is read from the file named
5005 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5006 map filenames on a file system with severe filename restrictions,
5007 such as DOS. The format of the file name map file is just a series
5008 of lines with two tokens on each line. The first token is the name
5009 to map, and the second token is the actual name to use. */
5011 struct file_name_map
5013 struct file_name_map *map_next;
5014 char *map_from;
5015 char *map_to;
5018 #define FILE_NAME_MAP_FILE "header.gcc"
5020 /* Read a space delimited string of unlimited length from a stdio
5021 file. */
5023 static char *
5024 read_filename_string (ch, f)
5025 int ch;
5026 FILE *f;
5028 char *alloc, *set;
5029 int len;
5031 len = 20;
5032 set = alloc = xmalloc (len + 1);
5033 if (! is_space[ch])
5035 *set++ = ch;
5036 while ((ch = getc (f)) != EOF && ! is_space[ch])
5038 if (set - alloc == len)
5040 len *= 2;
5041 alloc = xrealloc (alloc, len + 1);
5042 set = alloc + len / 2;
5044 *set++ = ch;
5047 *set = '\0';
5048 ungetc (ch, f);
5049 return alloc;
5052 /* Read the file name map file for DIRNAME.
5053 If DIRNAME is empty, read the map file for the working directory;
5054 otherwise DIRNAME must end in '/'. */
5056 static struct file_name_map *
5057 read_name_map (dirname)
5058 const char *dirname;
5060 /* This structure holds a linked list of file name maps, one per
5061 directory. */
5062 struct file_name_map_list
5064 struct file_name_map_list *map_list_next;
5065 char *map_list_name;
5066 struct file_name_map *map_list_map;
5068 static struct file_name_map_list *map_list;
5069 register struct file_name_map_list *map_list_ptr;
5070 char *name;
5071 FILE *f;
5072 size_t dirlen;
5074 for (map_list_ptr = map_list; map_list_ptr;
5075 map_list_ptr = map_list_ptr->map_list_next)
5076 if (! strcmp (map_list_ptr->map_list_name, dirname))
5077 return map_list_ptr->map_list_map;
5079 map_list_ptr = ((struct file_name_map_list *)
5080 xmalloc (sizeof (struct file_name_map_list)));
5081 map_list_ptr->map_list_name = xstrdup (dirname);
5082 map_list_ptr->map_list_map = NULL;
5084 dirlen = strlen (dirname);
5085 name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 1);
5086 strcpy (name, dirname);
5087 strcat (name, FILE_NAME_MAP_FILE);
5088 f = fopen (name, "r");
5089 if (!f)
5090 map_list_ptr->map_list_map = NULL;
5091 else
5093 int ch;
5095 while ((ch = getc (f)) != EOF)
5097 char *from, *to;
5098 struct file_name_map *ptr;
5099 size_t tolen;
5101 if (is_space[ch])
5102 continue;
5103 from = read_filename_string (ch, f);
5104 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5106 to = read_filename_string (ch, f);
5108 simplify_filename (from);
5109 tolen = simplify_filename (to);
5111 ptr = ((struct file_name_map *)
5112 xmalloc (sizeof (struct file_name_map)));
5113 ptr->map_from = from;
5115 /* Make the real filename absolute. */
5116 if (absolute_filename (to))
5117 ptr->map_to = to;
5118 else
5120 ptr->map_to = xmalloc (dirlen + tolen + 1);
5121 strcpy (ptr->map_to, dirname);
5122 strcat (ptr->map_to, to);
5123 free (to);
5126 ptr->map_next = map_list_ptr->map_list_map;
5127 map_list_ptr->map_list_map = ptr;
5129 while ((ch = getc (f)) != '\n')
5130 if (ch == EOF)
5131 break;
5133 fclose (f);
5136 map_list_ptr->map_list_next = map_list;
5137 map_list = map_list_ptr;
5139 return map_list_ptr->map_list_map;
5142 /* Try to open include file FILENAME. SEARCHPTR is the directory
5143 being tried from the include file search path.
5144 IMPORTING is "" if we are importing, null otherwise.
5145 Return -2 if found, either a matching name or a matching inode.
5146 Otherwise, open the file and return a file descriptor if successful
5147 or -1 if unsuccessful.
5148 Unless unsuccessful, put a descriptor of the included file into *PINC.
5149 This function maps filenames on file systems based on information read by
5150 read_name_map. */
5152 static int
5153 open_include_file (filename, searchptr, importing, pinc)
5154 char *filename;
5155 struct file_name_list *searchptr;
5156 const U_CHAR *importing;
5157 struct include_file **pinc;
5159 char *fname = remap ? remap_include_file (filename, searchptr) : filename;
5160 int fd = -2;
5162 /* Look up FNAME in include_hashtab. */
5163 struct include_file **phead = &include_hashtab[hashf ((U_CHAR *) fname,
5164 strlen (fname),
5165 INCLUDE_HASHSIZE)];
5166 struct include_file *inc, *head = *phead;
5167 for (inc = head; inc; inc = inc->next)
5168 if (!strcmp (fname, inc->fname))
5169 break;
5171 if (!inc
5172 || ! inc->control_macro
5173 || (inc->control_macro[0] && ! lookup (inc->control_macro, -1, -1))) {
5175 fd = open (fname, O_RDONLY, 0);
5177 if (fd < 0)
5179 #ifdef VMS
5180 /* if #include <dir/file> fails, try again with hacked spec. */
5181 if (!hack_vms_include_specification (fname, 0))
5182 return fd;
5183 fd = open (fname, O_RDONLY, 0);
5184 if (fd < 0)
5185 #endif
5186 return fd;
5189 if (!inc) {
5190 /* FNAME was not in include_hashtab; insert a new entry. */
5191 inc = (struct include_file *) xmalloc (sizeof (struct include_file));
5192 inc->next = head;
5193 inc->fname = fname;
5194 inc->control_macro = 0;
5195 inc->deps_output = 0;
5196 if (fstat (fd, &inc->st) != 0)
5197 pfatal_with_name (fname);
5198 *phead = inc;
5200 /* Look for another file with the same inode and device. */
5201 if (lookup_ino_include (inc)
5202 && inc->control_macro
5203 && (!inc->control_macro[0] || lookup (inc->control_macro, -1, -1))) {
5204 close (fd);
5205 fd = -2;
5209 /* For -M, add this file to the dependencies. */
5210 if (! inc->deps_output && (system_include_depth != 0) < print_deps) {
5211 inc->deps_output = 1;
5212 deps_output (fname, ' ');
5215 /* Handle -H option. */
5216 if (print_include_names)
5217 fprintf (stderr, "%*s%s\n", indepth, "", fname);
5220 if (importing)
5221 inc->control_macro = importing;
5223 *pinc = inc;
5224 return fd;
5227 /* Return the remapped name of the include file FILENAME.
5228 SEARCHPTR is the directory being tried from the include file path. */
5230 static char *
5231 remap_include_file (filename, searchptr)
5232 char *filename;
5233 struct file_name_list *searchptr;
5235 register struct file_name_map *map;
5236 register const char *from;
5238 if (searchptr)
5240 if (! searchptr->got_name_map)
5242 searchptr->name_map = read_name_map (searchptr->fname);
5243 searchptr->got_name_map = 1;
5246 /* Check the mapping for the directory we are using. */
5247 from = filename + strlen (searchptr->fname);
5248 for (map = searchptr->name_map; map; map = map->map_next)
5249 if (! strcmp (map->map_from, from))
5250 return map->map_to;
5253 from = base_name (filename);
5255 if (from != filename || !searchptr)
5257 /* Try to find a mapping file for the particular directory we are
5258 looking in. Thus #include <sys/types.h> will look up sys/types.h
5259 in /usr/include/header.gcc and look up types.h in
5260 /usr/include/sys/header.gcc. */
5262 char *dir = (char *) alloca (from - filename + 1);
5263 bcopy (filename, dir, from - filename);
5264 dir[from - filename] = '\0';
5266 for (map = read_name_map (dir); map; map = map->map_next)
5267 if (! strcmp (map->map_from, from))
5268 return map->map_to;
5271 return filename;
5274 /* Insert INC into the include file table, hashed by device and inode number.
5275 If a file with different name but same dev+ino was already in the table,
5276 return 1 and set INC's control macro to the already-known macro. */
5278 static int
5279 lookup_ino_include (inc)
5280 struct include_file *inc;
5282 int hash = ((unsigned) (inc->st.st_dev + INO_T_HASH (inc->st.st_ino))
5283 % INCLUDE_HASHSIZE);
5284 struct include_file *i = include_ino_hashtab[hash];
5285 inc->next_ino = i;
5286 include_ino_hashtab[hash] = inc;
5288 for (; i; i = i->next_ino)
5289 if (INO_T_EQ (inc->st.st_ino, i->st.st_ino)
5290 && inc->st.st_dev == i->st.st_dev) {
5291 inc->control_macro = i->control_macro;
5292 return 1;
5295 return 0;
5298 /* Process file descriptor F, which corresponds to include file INC,
5299 with output to OP.
5300 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5301 "system" include directories (as decided by the `is_system_include'
5302 function above).
5303 DIRPTR is the link in the dir path through which this file was found,
5304 or 0 if the file name was absolute. */
5306 static void
5307 finclude (f, inc, op, system_header_p, dirptr)
5308 int f;
5309 struct include_file *inc;
5310 FILE_BUF *op;
5311 int system_header_p;
5312 struct file_name_list *dirptr;
5314 char *fname = inc->fname;
5315 int i;
5316 FILE_BUF *fp; /* For input stack frame */
5317 int missing_newline = 0;
5319 CHECK_DEPTH (return;);
5321 fp = &instack[indepth + 1];
5322 bzero ((char *) fp, sizeof (FILE_BUF));
5323 fp->nominal_fname = fp->fname = fname;
5324 fp->nominal_fname_len = strlen (fname);
5325 fp->inc = inc;
5326 fp->length = 0;
5327 fp->lineno = 1;
5328 fp->if_stack = if_stack;
5329 fp->system_header_p = system_header_p;
5330 fp->dir = dirptr;
5332 if (S_ISREG (inc->st.st_mode)) {
5333 size_t s = (size_t) inc->st.st_size;
5334 if (s != inc->st.st_size || s + 2 < s)
5335 memory_full ();
5336 fp->buf = (U_CHAR *) xmalloc (s + 2);
5337 fp->bufp = fp->buf;
5339 /* Read the file contents, knowing that s is an upper bound
5340 on the number of bytes we can read. */
5341 fp->length = safe_read (f, (char *) fp->buf, s);
5342 if (fp->length < 0) goto nope;
5344 else if (S_ISDIR (inc->st.st_mode)) {
5345 error ("directory `%s' specified in #include", fname);
5346 close (f);
5347 return;
5348 } else {
5349 /* Cannot count its file size before reading.
5350 First read the entire file into heap and
5351 copy them into buffer on stack. */
5353 int bsize = 2000;
5354 int st_size = 0;
5356 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5358 for (;;) {
5359 i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
5360 if (i < 0)
5361 goto nope; /* error! */
5362 st_size += i;
5363 if (st_size != bsize)
5364 break; /* End of file */
5365 bsize *= 2;
5366 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5368 fp->bufp = fp->buf;
5369 fp->length = st_size;
5372 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5373 /* Backslash-newline at end is not good enough. */
5374 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5375 fp->buf[fp->length++] = '\n';
5376 missing_newline = 1;
5378 fp->buf[fp->length] = '\0';
5380 /* Close descriptor now, so nesting does not use lots of descriptors. */
5381 close (f);
5383 /* Must do this before calling trigraph_pcp, so that the correct file name
5384 will be printed in warning messages. */
5386 indepth++;
5387 input_file_stack_tick++;
5389 if (!no_trigraphs)
5390 trigraph_pcp (fp);
5392 if (warn_white_space)
5393 check_white_space (fp);
5395 output_line_directive (fp, op, 0, enter_file);
5396 rescan (op, 0);
5398 if (missing_newline)
5399 fp->lineno--;
5401 if (pedantic && missing_newline)
5402 pedwarn ("file does not end in newline");
5404 indepth--;
5405 input_file_stack_tick++;
5406 output_line_directive (&instack[indepth], op, 0, leave_file);
5407 free (fp->buf);
5408 return;
5410 nope:
5412 perror_with_name (fname);
5413 close (f);
5414 free (fp->buf);
5417 /* Record that inclusion of the include file INC
5418 should be controlled by the macro named MACRO_NAME.
5419 This means that trying to include the file again
5420 will do something if that macro is defined. */
5422 static void
5423 record_control_macro (inc, macro_name)
5424 struct include_file *inc;
5425 const U_CHAR *macro_name;
5427 if (!inc->control_macro || inc->control_macro[0])
5428 inc->control_macro = macro_name;
5431 /* Load the specified precompiled header into core, and verify its
5432 preconditions. PCF indicates the file descriptor to read, which must
5433 be a regular file. *ST is its file status.
5434 FNAME indicates the file name of the original header.
5435 *LIMIT will be set to an address one past the end of the file.
5436 If the preconditions of the file are not satisfied, the buffer is
5437 freed and we return 0. If the preconditions are satisfied, return
5438 the address of the buffer following the preconditions. The buffer, in
5439 this case, should never be freed because various pieces of it will
5440 be referred to until all precompiled strings are output at the end of
5441 the run. */
5443 static char *
5444 check_precompiled (pcf, st, fname, limit)
5445 int pcf;
5446 struct stat *st;
5447 const char *fname ATTRIBUTE_UNUSED;
5448 const char **limit;
5450 int length = 0;
5451 char *buf;
5452 char *cp;
5454 if (pcp_outfile)
5455 return 0;
5457 if (S_ISREG (st->st_mode))
5459 size_t s = (size_t) st->st_size;
5460 if (s != st->st_size || s + 2 < s)
5461 memory_full ();
5462 buf = xmalloc (s + 2);
5463 length = safe_read (pcf, buf, s);
5464 if (length < 0)
5465 goto nope;
5467 else
5468 abort ();
5470 if (length > 0 && buf[length-1] != '\n')
5471 buf[length++] = '\n';
5472 buf[length] = '\0';
5474 *limit = buf + length;
5476 /* File is in core. Check the preconditions. */
5477 if (!check_preconditions (buf))
5478 goto nope;
5479 for (cp = buf; *cp; cp++)
5481 #ifdef DEBUG_PCP
5482 fprintf (stderr, "Using preinclude %s\n", fname);
5483 #endif
5484 return cp + 1;
5486 nope:
5487 #ifdef DEBUG_PCP
5488 fprintf (stderr, "Cannot use preinclude %s\n", fname);
5489 #endif
5490 free (buf);
5491 return 0;
5494 /* PREC (null terminated) points to the preconditions of a
5495 precompiled header. These are a series of #define and #undef
5496 lines which must match the current contents of the hash
5497 table. */
5499 static int
5500 check_preconditions (prec)
5501 const char *prec;
5503 MACRODEF mdef;
5504 const char *lineend;
5506 while (*prec) {
5507 lineend = index (prec, '\n');
5509 if (*prec++ != '#') {
5510 error ("Bad format encountered while reading precompiled file");
5511 return 0;
5513 if (!strncmp (prec, "define", 6)) {
5514 HASHNODE *hp;
5516 prec += 6;
5517 mdef = create_definition ((const U_CHAR *) prec,
5518 (const U_CHAR *) lineend, NULL_PTR);
5520 if (mdef.defn == 0)
5521 abort ();
5523 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
5524 || (hp->type != T_MACRO && hp->type != T_CONST)
5525 || (hp->type == T_MACRO
5526 && !compare_defs (mdef.defn, hp->value.defn)
5527 && (mdef.defn->length != 2
5528 || mdef.defn->expansion[0] != '\n'
5529 || mdef.defn->expansion[1] != ' ')))
5530 return 0;
5531 } else if (!strncmp (prec, "undef", 5)) {
5532 const char *name;
5533 int len;
5535 prec += 5;
5536 while (is_hor_space[(U_CHAR) *prec])
5537 prec++;
5538 name = prec;
5539 while (is_idchar[(U_CHAR) *prec])
5540 prec++;
5541 len = prec - name;
5543 if (lookup ((const U_CHAR *) name, len, -1))
5544 return 0;
5545 } else {
5546 error ("Bad format encountered while reading precompiled file");
5547 return 0;
5549 prec = lineend + 1;
5551 /* They all passed successfully */
5552 return 1;
5555 /* Process the main body of a precompiled file. BUF points to the
5556 string section of the file, following the preconditions. LIMIT is one
5557 character past the end. NAME is the name of the file being read
5558 in. OP is the main output buffer. */
5560 static void
5561 pcfinclude (buf, name, op)
5562 U_CHAR *buf;
5563 const U_CHAR *name;
5564 FILE_BUF *op;
5566 FILE_BUF tmpbuf;
5567 int nstrings;
5568 U_CHAR *cp = buf;
5570 /* First in the file comes 4 bytes indicating the number of strings, */
5571 /* in network byte order. (MSB first). */
5572 nstrings = *cp++;
5573 nstrings = (nstrings << 8) | *cp++;
5574 nstrings = (nstrings << 8) | *cp++;
5575 nstrings = (nstrings << 8) | *cp++;
5577 /* Looping over each string... */
5578 while (nstrings--) {
5579 U_CHAR *string_start;
5580 U_CHAR *endofthiskey;
5581 STRINGDEF *str;
5582 int nkeys;
5584 /* Each string starts with a STRINGDEF structure (str), followed */
5585 /* by the text of the string (string_start) */
5587 /* First skip to a longword boundary */
5588 /* ??? Why a 4-byte boundary? On all machines? */
5589 /* NOTE: This works correctly even if size_t
5590 is narrower than a pointer.
5591 Do not try risky measures here to get another type to use!
5592 Do not include stddef.h--it will fail! */
5593 if ((size_t) cp & 3)
5594 cp += 4 - ((size_t) cp & 3);
5596 /* Now get the string. */
5597 str = (STRINGDEF *) (PTR) cp;
5598 string_start = cp += sizeof (STRINGDEF);
5600 for (; *cp; cp++) /* skip the string */
5603 /* We need to macro expand the string here to ensure that the
5604 proper definition environment is in place. If it were only
5605 expanded when we find out it is needed, macros necessary for
5606 its proper expansion might have had their definitions changed. */
5607 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
5608 /* Lineno is already set in the precompiled file */
5609 str->contents = tmpbuf.buf;
5610 str->len = tmpbuf.bufp - tmpbuf.buf;
5611 str->writeflag = 0;
5612 str->filename = name;
5613 str->output_mark = outbuf.bufp - outbuf.buf;
5615 str->chain = 0;
5616 *stringlist_tailp = str;
5617 stringlist_tailp = &str->chain;
5619 /* Next comes a fourbyte number indicating the number of keys
5620 for this string. */
5621 nkeys = *cp++;
5622 nkeys = (nkeys << 8) | *cp++;
5623 nkeys = (nkeys << 8) | *cp++;
5624 nkeys = (nkeys << 8) | *cp++;
5626 /* If this number is -1, then the string is mandatory. */
5627 if (nkeys == -1)
5628 str->writeflag = 1;
5629 else
5630 /* Otherwise, for each key, */
5631 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
5632 KEYDEF *kp = (KEYDEF *) (PTR) cp;
5633 HASHNODE *hp;
5634 U_CHAR *bp;
5636 /* It starts with a KEYDEF structure */
5637 cp += sizeof (KEYDEF);
5639 /* Find the end of the key. At the end of this for loop we
5640 advance CP to the start of the next key using this variable. */
5641 endofthiskey = cp + strlen ((char *) cp);
5642 kp->str = str;
5644 /* Expand the key, and enter it into the hash table. */
5645 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
5646 bp = tmpbuf.buf;
5648 while (is_hor_space[*bp])
5649 bp++;
5650 if (!is_idstart[*bp] || bp == tmpbuf.bufp) {
5651 str->writeflag = 1;
5652 continue;
5655 hp = lookup (bp, -1, -1);
5656 if (hp == NULL) {
5657 kp->chain = 0;
5658 install (bp, -1, T_PCSTRING, (char *) kp, -1);
5660 else if (hp->type == T_PCSTRING) {
5661 kp->chain = hp->value.keydef;
5662 hp->value.keydef = kp;
5664 else
5665 str->writeflag = 1;
5668 /* This output_line_directive serves to switch us back to the current
5669 input file in case some of these strings get output (which will
5670 result in line directives for the header file being output). */
5671 output_line_directive (&instack[indepth], op, 0, enter_file);
5674 /* Called from rescan when it hits a key for strings. Mark them all
5675 used and clean up. */
5677 static void
5678 pcstring_used (hp)
5679 HASHNODE *hp;
5681 KEYDEF *kp;
5683 for (kp = hp->value.keydef; kp; kp = kp->chain)
5684 kp->str->writeflag = 1;
5685 delete_macro (hp);
5688 /* Write the output, interspersing precompiled strings in their
5689 appropriate places. */
5691 static void
5692 write_output ()
5694 STRINGDEF *next_string;
5695 U_CHAR *cur_buf_loc;
5696 int line_directive_len = 80;
5697 char *line_directive = xmalloc (line_directive_len);
5698 int len;
5700 /* In each run through the loop, either cur_buf_loc ==
5701 next_string_loc, in which case we print a series of strings, or
5702 it is less than next_string_loc, in which case we write some of
5703 the buffer. */
5704 cur_buf_loc = outbuf.buf;
5705 next_string = stringlist;
5707 while (cur_buf_loc < outbuf.bufp || next_string) {
5708 if (next_string
5709 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
5710 if (next_string->writeflag) {
5711 len = 4 * strlen ((const char *) next_string->filename) + 32;
5712 while (len > line_directive_len)
5713 line_directive = xrealloc (line_directive,
5714 line_directive_len *= 2);
5715 sprintf (line_directive, "\n# %d ", next_string->lineno);
5716 strcpy (quote_string (line_directive + strlen (line_directive),
5717 (const char *) next_string->filename,
5718 strlen ((const char *) next_string->filename)),
5719 "\n");
5720 safe_write (fileno (stdout), line_directive, strlen (line_directive));
5721 safe_write (fileno (stdout),
5722 (char *) next_string->contents, next_string->len);
5724 next_string = next_string->chain;
5726 else {
5727 len = (next_string
5728 ? (next_string->output_mark
5729 - (cur_buf_loc - outbuf.buf))
5730 : outbuf.bufp - cur_buf_loc);
5732 safe_write (fileno (stdout), (char *) cur_buf_loc, len);
5733 cur_buf_loc += len;
5736 free (line_directive);
5739 /* Pass a directive through to the output file.
5740 BUF points to the contents of the directive, as a contiguous string.
5741 LIMIT points to the first character past the end of the directive.
5742 KEYWORD is the keyword-table entry for the directive. */
5744 static void
5745 pass_thru_directive (buf, limit, op, keyword)
5746 const U_CHAR *buf;
5747 const U_CHAR *limit;
5748 FILE_BUF *op;
5749 struct directive *keyword;
5751 register int keyword_length = keyword->length;
5753 check_expand (op, 1 + keyword_length + (limit - buf));
5754 *op->bufp++ = '#';
5755 bcopy (keyword->name, (char *) op->bufp, keyword_length);
5756 op->bufp += keyword_length;
5757 if (limit != buf && buf[0] != ' ')
5758 *op->bufp++ = ' ';
5759 bcopy ((const PTR) buf, (PTR) op->bufp, limit - buf);
5760 op->bufp += (limit - buf);
5761 #if 0
5762 *op->bufp++ = '\n';
5763 /* Count the line we have just made in the output,
5764 to get in sync properly. */
5765 op->lineno++;
5766 #endif
5769 /* The arglist structure is built by do_define to tell
5770 collect_definition where the argument names begin. That
5771 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5772 would contain pointers to the strings x, y, and z.
5773 Collect_definition would then build a DEFINITION node,
5774 with reflist nodes pointing to the places x, y, and z had
5775 appeared. So the arglist is just convenience data passed
5776 between these two routines. It is not kept around after
5777 the current #define has been processed and entered into the
5778 hash table. */
5780 struct arglist {
5781 struct arglist *next;
5782 const U_CHAR *name;
5783 int length;
5784 int argno;
5785 char rest_args;
5788 /* Create a DEFINITION node from a #define directive. Arguments are
5789 as for do_define. */
5791 static MACRODEF
5792 create_definition (buf, limit, op)
5793 const U_CHAR *buf, *limit;
5794 FILE_BUF *op;
5796 const U_CHAR *bp; /* temp ptr into input buffer */
5797 const U_CHAR *symname; /* remember where symbol name starts */
5798 int sym_length; /* and how long it is */
5799 int line = instack[indepth].lineno;
5800 const char *file = instack[indepth].nominal_fname;
5801 size_t file_len = instack[indepth].nominal_fname_len;
5802 int rest_args = 0;
5804 DEFINITION *defn;
5805 int arglengths = 0; /* Accumulate lengths of arg names
5806 plus number of args. */
5807 MACRODEF mdef;
5809 bp = buf;
5811 while (is_hor_space[*bp])
5812 bp++;
5814 symname = bp; /* remember where it starts */
5815 sym_length = check_macro_name (bp, 0);
5816 bp += sym_length;
5818 /* Lossage will occur if identifiers or control keywords are broken
5819 across lines using backslash. This is not the right place to take
5820 care of that. */
5822 if (*bp == '(') {
5823 struct arglist *arg_ptrs = NULL;
5824 int argno = 0;
5826 bp++; /* skip '(' */
5827 SKIP_WHITE_SPACE (bp);
5829 /* Loop over macro argument names. */
5830 while (*bp != ')') {
5831 struct arglist *temp;
5833 temp = (struct arglist *) alloca (sizeof (struct arglist));
5834 temp->name = bp;
5835 temp->next = arg_ptrs;
5836 temp->argno = argno++;
5837 temp->rest_args = 0;
5838 arg_ptrs = temp;
5840 if (rest_args)
5841 pedwarn ("another parameter follows `%s'",
5842 rest_extension);
5844 if (!is_idstart[*bp])
5846 if (c99 && limit - bp > (long) REST_EXTENSION_LENGTH
5847 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
5849 /* This is the ISO C 99 way to write macros with variable
5850 number of arguments. */
5851 rest_args = 1;
5852 temp->rest_args = 1;
5854 else
5855 pedwarn ("invalid character in macro parameter name");
5858 /* Find the end of the arg name. */
5859 while (is_idchar[*bp]) {
5860 bp++;
5861 /* do we have a "special" rest-args extension here? */
5862 if (limit - bp > (long) REST_EXTENSION_LENGTH
5863 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
5864 if (pedantic && !instack[indepth].system_header_p)
5865 pedwarn ("ANSI C does not allow macro with variable arguments");
5866 rest_args = 1;
5867 temp->rest_args = 1;
5868 break;
5871 if (bp == temp->name && rest_args == 1)
5873 /* This is the ISO C 99 style. */
5874 temp->name = (U_CHAR *) va_args_name;
5875 temp->length = VA_ARGS_NAME_LENGTH;
5877 else
5878 temp->length = bp - temp->name;
5879 if (rest_args == 1)
5880 bp += REST_EXTENSION_LENGTH;
5881 arglengths += temp->length + 2;
5882 SKIP_WHITE_SPACE (bp);
5883 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
5884 error ("badly punctuated parameter list in `#define'");
5885 goto nope;
5887 if (*bp == ',') {
5888 bp++;
5889 SKIP_WHITE_SPACE (bp);
5890 /* A comma at this point can only be followed by an identifier. */
5891 if (!is_idstart[*bp]
5892 && !(c99 && limit - bp > (long) REST_EXTENSION_LENGTH
5893 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)) {
5894 error ("badly punctuated parameter list in `#define'");
5895 goto nope;
5898 if (bp >= limit) {
5899 error ("unterminated parameter list in `#define'");
5900 goto nope;
5903 struct arglist *otemp;
5905 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
5906 if (temp->length == otemp->length
5907 && bcmp (temp->name, otemp->name, temp->length) == 0)
5909 error ("duplicate argument name `%.*s' in `#define'",
5910 temp->length, temp->name);
5911 goto nope;
5913 if (rest_args == 0 && temp->length == VA_ARGS_NAME_LENGTH
5914 && bcmp (temp->name, va_args_name, VA_ARGS_NAME_LENGTH) == 0)
5916 error ("\
5917 reserved name `%s' used as argument name in `#define'", va_args_name);
5918 goto nope;
5923 ++bp; /* skip paren */
5924 SKIP_WHITE_SPACE (bp);
5925 /* now everything from bp before limit is the definition. */
5926 defn = collect_expansion (bp, limit, argno, arg_ptrs);
5927 defn->rest_args = rest_args;
5929 /* Now set defn->args.argnames to the result of concatenating
5930 the argument names in reverse order
5931 with comma-space between them. */
5932 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
5934 struct arglist *temp;
5935 int i = 0;
5936 for (temp = arg_ptrs; temp; temp = temp->next) {
5937 bcopy (temp->name, &defn->args.argnames[i], temp->length);
5938 i += temp->length;
5939 if (temp->next != 0) {
5940 defn->args.argnames[i++] = ',';
5941 defn->args.argnames[i++] = ' ';
5944 defn->args.argnames[i] = 0;
5946 } else {
5947 /* Simple expansion or empty definition. */
5949 if (bp < limit)
5951 if (is_hor_space[*bp]) {
5952 bp++;
5953 SKIP_WHITE_SPACE (bp);
5954 } else if (sym_length) {
5955 switch (*bp) {
5956 case '!': case '"': case '#': case '%': case '&': case '\'':
5957 case ')': case '*': case '+': case ',': case '-': case '.':
5958 case '/': case ':': case ';': case '<': case '=': case '>':
5959 case '?': case '[': case '\\': case ']': case '^': case '{':
5960 case '|': case '}': case '~':
5961 warning ("missing white space after `#define %.*s'",
5962 sym_length, symname);
5963 break;
5965 default:
5966 pedwarn ("missing white space after `#define %.*s'",
5967 sym_length, symname);
5968 break;
5972 /* Now everything from bp before limit is the definition. */
5973 defn = collect_expansion (bp, limit, -1, NULL_PTR);
5974 defn->args.argnames = (U_CHAR *) "";
5977 defn->line = line;
5978 defn->file = file;
5979 defn->file_len = file_len;
5981 /* OP is null if this is a predefinition */
5982 defn->predefined = !op;
5983 mdef.defn = defn;
5984 mdef.symnam = symname;
5985 mdef.symlen = sym_length;
5987 return mdef;
5989 nope:
5990 mdef.defn = 0;
5991 return mdef;
5994 /* Process a #define directive.
5995 BUF points to the contents of the #define directive, as a contiguous string.
5996 LIMIT points to the first character past the end of the definition.
5997 KEYWORD is the keyword-table entry for #define. */
5999 static int
6000 do_define (buf, limit, op, keyword)
6001 U_CHAR *buf, *limit;
6002 FILE_BUF *op;
6003 struct directive *keyword;
6005 int hashcode;
6006 MACRODEF mdef;
6007 enum node_type newtype = keyword->type == T_DEFINE ? T_MACRO : T_POISON;
6009 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
6010 if (pcp_outfile && op)
6011 pass_thru_directive (buf, limit, op, keyword);
6013 mdef = create_definition (buf, limit, op);
6014 if (mdef.defn == 0)
6015 goto nope;
6017 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
6020 HASHNODE *hp;
6021 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
6022 int ok = 0;
6023 /* Redefining a precompiled key is ok. */
6024 if (hp->type == T_PCSTRING)
6025 ok = 1;
6026 /* Redefining a poisoned identifier is even worse than `not ok'. */
6027 else if (hp->type == T_POISON)
6028 ok = -1;
6029 /* Poisoning anything else is not ok.
6030 The poison should always come first. */
6031 else if (newtype == T_POISON)
6032 ok = 0;
6033 /* Redefining a macro is ok if the definitions are the same. */
6034 else if (hp->type == T_MACRO)
6035 ok = ! compare_defs (mdef.defn, hp->value.defn);
6036 /* Redefining a constant is ok with -D. */
6037 else if (hp->type == T_CONST)
6038 ok = ! done_initializing;
6040 /* Print the warning or error if it's not ok. */
6041 if (ok <= 0)
6043 /* If we are passing through #define and #undef directives, do
6044 that for this re-definition now. */
6045 if (debug_output && op)
6046 pass_thru_directive (buf, limit, op, keyword);
6048 if (hp->type == T_POISON)
6049 error ("redefining poisoned `%.*s'", mdef.symlen, mdef.symnam);
6050 else
6051 pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
6052 if (hp->type == T_MACRO)
6053 pedwarn_with_file_and_line (hp->value.defn->file,
6054 hp->value.defn->file_len,
6055 hp->value.defn->line,
6056 "this is the location of the previous definition");
6058 if (hp->type != T_POISON)
6060 /* Replace the old definition. */
6061 hp->type = newtype;
6062 hp->value.defn = mdef.defn;
6064 } else {
6065 /* If we are passing through #define and #undef directives, do
6066 that for this new definition now. */
6067 if (debug_output && op)
6068 pass_thru_directive (buf, limit, op, keyword);
6069 install (mdef.symnam, mdef.symlen, newtype,
6070 (char *) mdef.defn, hashcode);
6074 return 0;
6076 nope:
6078 return 1;
6081 /* Check a purported macro name SYMNAME, and yield its length.
6082 ASSERTION is nonzero if this is really for an assertion name. */
6084 static int
6085 check_macro_name (symname, assertion)
6086 const U_CHAR *symname;
6087 int assertion;
6089 const U_CHAR *p;
6090 int sym_length;
6092 for (p = symname; is_idchar[*p]; p++)
6094 sym_length = p - symname;
6095 if (sym_length == 0
6096 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
6098 if (assertion)
6099 error ("invalid assertion name");
6100 else
6101 error ("invalid macro name");
6103 else if (!is_idstart[*symname]
6104 || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
6106 if (assertion)
6107 error ("invalid assertion name `%.*s'", sym_length, symname);
6108 else
6109 error ("invalid macro name `%.*s'", sym_length, symname);
6111 return sym_length;
6114 /* Return zero if two DEFINITIONs are isomorphic. */
6116 static int
6117 compare_defs (d1, d2)
6118 DEFINITION *d1, *d2;
6120 register struct reflist *a1, *a2;
6121 register U_CHAR *p1 = d1->expansion;
6122 register U_CHAR *p2 = d2->expansion;
6123 int first = 1;
6125 if (d1->nargs != d2->nargs)
6126 return 1;
6127 if (pedantic
6128 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
6129 return 1;
6130 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
6131 a1 = a1->next, a2 = a2->next) {
6132 if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
6133 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
6134 || a1->argno != a2->argno
6135 || a1->stringify != a2->stringify
6136 || a1->raw_before != a2->raw_before
6137 || a1->raw_after != a2->raw_after)
6138 return 1;
6139 first = 0;
6140 p1 += a1->nchars;
6141 p2 += a2->nchars;
6143 if (a1 != a2)
6144 return 1;
6145 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
6146 p2, d2->length - (p2 - d2->expansion), 1))
6147 return 1;
6148 return 0;
6151 /* Return 1 if two parts of two macro definitions are effectively different.
6152 One of the parts starts at BEG1 and has LEN1 chars;
6153 the other has LEN2 chars at BEG2.
6154 Any sequence of whitespace matches any other sequence of whitespace.
6155 FIRST means these parts are the first of a macro definition;
6156 so ignore leading whitespace entirely.
6157 LAST means these parts are the last of a macro definition;
6158 so ignore trailing whitespace entirely. */
6160 static int
6161 comp_def_part (first, beg1, len1, beg2, len2, last)
6162 int first;
6163 const U_CHAR *beg1, *beg2;
6164 int len1, len2;
6165 int last;
6167 register const U_CHAR *end1 = beg1 + len1;
6168 register const U_CHAR *end2 = beg2 + len2;
6169 if (first) {
6170 while (beg1 != end1 && is_space[*beg1]) beg1++;
6171 while (beg2 != end2 && is_space[*beg2]) beg2++;
6173 if (last) {
6174 while (beg1 != end1 && is_space[end1[-1]]) end1--;
6175 while (beg2 != end2 && is_space[end2[-1]]) end2--;
6177 while (beg1 != end1 && beg2 != end2) {
6178 if (is_space[*beg1] && is_space[*beg2]) {
6179 while (beg1 != end1 && is_space[*beg1]) beg1++;
6180 while (beg2 != end2 && is_space[*beg2]) beg2++;
6181 } else if (*beg1 == *beg2) {
6182 beg1++; beg2++;
6183 } else break;
6185 return (beg1 != end1) || (beg2 != end2);
6188 /* Read a replacement list for a macro with parameters.
6189 Build the DEFINITION structure.
6190 Reads characters of text starting at BUF until END.
6191 ARGLIST specifies the formal parameters to look for
6192 in the text of the definition; NARGS is the number of args
6193 in that list, or -1 for a macro name that wants no argument list.
6194 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6195 and NAMELEN is its length in characters.
6197 Note that comments, backslash-newlines, and leading white space
6198 have already been deleted from the argument. */
6200 /* If there is no trailing whitespace, a Newline Space is added at the end
6201 to prevent concatenation that would be contrary to the standard. */
6203 static DEFINITION *
6204 collect_expansion (buf, end, nargs, arglist)
6205 const U_CHAR *buf;
6206 const U_CHAR *end;
6207 int nargs;
6208 struct arglist *arglist;
6210 DEFINITION *defn;
6211 register const U_CHAR *p;
6212 register const U_CHAR *limit;
6213 register U_CHAR *lastp, *exp_p;
6214 struct reflist *endpat = NULL;
6215 /* Pointer to first nonspace after last ## seen. */
6216 const U_CHAR *concat = 0;
6217 /* Pointer to first nonspace after last single-# seen. */
6218 const U_CHAR *stringify = 0;
6219 /* How those tokens were spelled. */
6220 enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
6221 enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
6222 int maxsize;
6223 int expected_delimiter = '\0';
6225 /* Scan thru the replacement list, ignoring comments and quoted
6226 strings, picking up on the macro calls. It does a linear search
6227 thru the arg list on every potential symbol. Profiling might say
6228 that something smarter should happen. */
6230 if (end < buf)
6231 abort ();
6233 /* Find the beginning of the trailing whitespace. */
6234 limit = end;
6235 p = buf;
6236 while (p < limit && is_space[limit[-1]]) limit--;
6238 /* Allocate space for the text in the macro definition.
6239 Each input char may or may not need 1 byte,
6240 so this is an upper bound.
6241 The extra 3 are for invented trailing newline-marker and final null. */
6242 maxsize = (sizeof (DEFINITION)
6243 + (limit - p) + 3);
6244 defn = (DEFINITION *) xcalloc (1, maxsize);
6246 defn->nargs = nargs;
6247 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
6248 lastp = exp_p;
6250 if (p[0] == '#'
6251 ? p[1] == '#'
6252 : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
6253 error ("`##' at start of macro definition");
6254 p += p[0] == '#' ? 2 : 4;
6257 /* Process the main body of the definition. */
6258 while (p < limit) {
6259 int skipped_arg = 0;
6260 register U_CHAR c = *p++;
6262 *exp_p++ = c;
6264 if (!traditional) {
6265 switch (c) {
6266 case '\'':
6267 case '\"':
6268 if (expected_delimiter != '\0') {
6269 if (c == expected_delimiter)
6270 expected_delimiter = '\0';
6271 } else
6272 expected_delimiter = c;
6273 break;
6275 case '\\':
6276 if (expected_delimiter) {
6277 /* In a string, backslash goes through
6278 and makes next char ordinary. */
6279 *exp_p++ = *p++;
6281 break;
6283 case '%':
6284 if (!expected_delimiter && *p == ':') {
6285 /* %: is not a digraph if preceded by an odd number of '<'s. */
6286 const U_CHAR *p0 = p - 1;
6287 while (buf < p0 && p0[-1] == '<')
6288 p0--;
6289 if ((p - p0) & 1) {
6290 /* Treat %:%: as ## and %: as #. */
6291 if (p[1] == '%' && p[2] == ':') {
6292 p += 2;
6293 goto sharp_sharp_token;
6295 if (nargs >= 0) {
6296 p++;
6297 goto sharp_token;
6301 break;
6303 case '#':
6304 /* # is ordinary inside a string. */
6305 if (expected_delimiter)
6306 break;
6307 if (*p == '#') {
6308 sharp_sharp_token:
6309 /* ##: concatenate preceding and following tokens. */
6310 /* Take out the first #, discard preceding whitespace. */
6311 exp_p--;
6312 while (exp_p > lastp && is_hor_space[exp_p[-1]])
6313 --exp_p;
6314 /* Skip the second #. */
6315 p++;
6316 concat_sharp_token_type = c;
6317 if (is_hor_space[*p]) {
6318 concat_sharp_token_type = c + 1;
6319 p++;
6320 SKIP_WHITE_SPACE (p);
6322 concat = p;
6323 if (p == limit)
6324 error ("`##' at end of macro definition");
6325 } else if (nargs >= 0) {
6326 /* Single #: stringify following argument ref.
6327 Don't leave the # in the expansion. */
6328 sharp_token:
6329 exp_p--;
6330 stringify_sharp_token_type = c;
6331 if (is_hor_space[*p]) {
6332 stringify_sharp_token_type = c + 1;
6333 p++;
6334 SKIP_WHITE_SPACE (p);
6336 if (! is_idstart[*p] || nargs == 0
6337 || (*p == 'L' && (p[1] == '\'' || p[1] == '"')))
6338 error ("`#' operator is not followed by a macro argument name");
6339 else
6340 stringify = p;
6342 break;
6344 } else {
6345 /* In -traditional mode, recognize arguments inside strings and
6346 character constants, and ignore special properties of #.
6347 Arguments inside strings are considered "stringified", but no
6348 extra quote marks are supplied. */
6349 switch (c) {
6350 case '\'':
6351 case '\"':
6352 if (expected_delimiter != '\0') {
6353 if (c == expected_delimiter)
6354 expected_delimiter = '\0';
6355 } else
6356 expected_delimiter = c;
6357 break;
6359 case '\\':
6360 /* Backslash quotes delimiters and itself, but not macro args. */
6361 if (expected_delimiter != 0 && p < limit
6362 && (*p == expected_delimiter || *p == '\\')) {
6363 *exp_p++ = *p++;
6364 continue;
6366 break;
6368 case '/':
6369 if (expected_delimiter != '\0') /* No comments inside strings. */
6370 break;
6371 if (*p == '*') {
6372 /* If we find a comment that wasn't removed by handle_directive,
6373 this must be -traditional. So replace the comment with
6374 nothing at all. */
6375 exp_p--;
6376 while (++p < limit) {
6377 if (p[0] == '*' && p[1] == '/') {
6378 p += 2;
6379 break;
6382 #if 0
6383 /* Mark this as a concatenation-point, as if it had been ##. */
6384 concat = p;
6385 #endif
6387 break;
6391 #ifdef MULTIBYTE_CHARS
6392 /* Handle multibyte characters inside string and character literals. */
6393 if (expected_delimiter != '\0')
6395 int length;
6396 --p;
6397 length = local_mblen (p, limit - p);
6398 if (length > 1)
6400 --exp_p;
6401 bcopy (p, exp_p, length);
6402 p += length;
6403 exp_p += length;
6404 continue;
6406 ++p;
6408 #endif
6410 /* Handle the start of a symbol. */
6411 if (is_idchar[c] && nargs > 0) {
6412 const U_CHAR *id_beg = p - 1;
6413 int id_len;
6415 --exp_p;
6416 while (p != limit && is_idchar[*p]) p++;
6417 id_len = p - id_beg;
6419 if (is_idstart[c]
6420 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
6421 register struct arglist *arg;
6423 for (arg = arglist; arg != NULL; arg = arg->next) {
6424 struct reflist *tpat;
6426 if (arg->name[0] == c
6427 && arg->length == id_len
6428 && bcmp (arg->name, id_beg, id_len) == 0) {
6429 enum sharp_token_type tpat_stringify;
6430 if (expected_delimiter) {
6431 if (warn_stringify) {
6432 if (traditional) {
6433 warning ("macro argument `%.*s' is stringified.",
6434 id_len, arg->name);
6435 } else {
6436 warning ("macro arg `%.*s' would be stringified with -traditional.",
6437 id_len, arg->name);
6440 /* If ANSI, don't actually substitute inside a string. */
6441 if (!traditional)
6442 break;
6443 tpat_stringify = SHARP_TOKEN;
6444 } else {
6445 tpat_stringify
6446 = (stringify == id_beg
6447 ? stringify_sharp_token_type : NO_SHARP_TOKEN);
6449 /* make a pat node for this arg and append it to the end of
6450 the pat list */
6451 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
6452 tpat->next = NULL;
6453 tpat->raw_before
6454 = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
6455 tpat->raw_after = NO_SHARP_TOKEN;
6456 tpat->rest_args = arg->rest_args;
6457 tpat->stringify = tpat_stringify;
6459 if (endpat == NULL)
6460 defn->pattern = tpat;
6461 else
6462 endpat->next = tpat;
6463 endpat = tpat;
6465 tpat->argno = arg->argno;
6466 tpat->nchars = exp_p - lastp;
6468 register const U_CHAR *p1 = p;
6469 SKIP_WHITE_SPACE (p1);
6470 if (p1[0]=='#'
6471 ? p1[1]=='#'
6472 : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
6473 tpat->raw_after = p1[0] + (p != p1);
6475 lastp = exp_p; /* place to start copying from next time */
6476 skipped_arg = 1;
6477 break;
6482 /* If this was not a macro arg, copy it into the expansion. */
6483 if (! skipped_arg) {
6484 register const U_CHAR *lim1 = p;
6485 p = id_beg;
6486 while (p != lim1)
6487 *exp_p++ = *p++;
6488 if (stringify == id_beg)
6489 error ("`#' operator should be followed by a macro argument name");
6494 if (!traditional && expected_delimiter == 0) {
6495 /* If ANSI, put in a newline-space marker to prevent token pasting.
6496 But not if "inside a string" (which in ANSI mode happens only for
6497 -D option). */
6498 *exp_p++ = '\n';
6499 *exp_p++ = ' ';
6502 *exp_p = '\0';
6504 defn->length = exp_p - defn->expansion;
6506 /* Crash now if we overrun the allocated size. */
6507 if (defn->length + 1 > maxsize)
6508 abort ();
6510 #if 0
6511 /* This isn't worth the time it takes. */
6512 /* give back excess storage */
6513 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
6514 #endif
6516 return defn;
6519 static int
6520 do_assert (buf, limit, op, keyword)
6521 U_CHAR *buf, *limit;
6522 FILE_BUF *op ATTRIBUTE_UNUSED;
6523 struct directive *keyword ATTRIBUTE_UNUSED;
6525 const U_CHAR *bp; /* temp ptr into input buffer */
6526 const U_CHAR *symname; /* remember where symbol name starts */
6527 int sym_length; /* and how long it is */
6528 struct arglist *tokens = NULL;
6530 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6531 pedwarn ("ANSI C does not allow `#assert'");
6533 bp = buf;
6535 while (is_hor_space[*bp])
6536 bp++;
6538 symname = bp; /* remember where it starts */
6539 sym_length = check_macro_name (bp, 1);
6540 bp += sym_length;
6541 /* #define doesn't do this, but we should. */
6542 SKIP_WHITE_SPACE (bp);
6544 /* Lossage will occur if identifiers or control tokens are broken
6545 across lines using backslash. This is not the right place to take
6546 care of that. */
6548 if (*bp != '(') {
6549 error ("missing token-sequence in `#assert'");
6550 return 1;
6554 int error_flag = 0;
6556 bp++; /* skip '(' */
6557 SKIP_WHITE_SPACE (bp);
6559 tokens = read_token_list (&bp, limit, &error_flag);
6560 if (error_flag)
6561 return 1;
6562 if (tokens == 0) {
6563 error ("empty token-sequence in `#assert'");
6564 return 1;
6567 ++bp; /* skip paren */
6568 SKIP_WHITE_SPACE (bp);
6571 /* If this name isn't already an assertion name, make it one.
6572 Error if it was already in use in some other way. */
6575 ASSERTION_HASHNODE *hp;
6576 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6577 struct tokenlist_list *value
6578 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6580 hp = assertion_lookup (symname, sym_length, hashcode);
6581 if (hp == NULL) {
6582 if (sym_length == 7 && ! bcmp (symname, "defined", 7))
6583 error ("`defined' redefined as assertion");
6584 hp = assertion_install (symname, sym_length, hashcode);
6587 /* Add the spec'd token-sequence to the list of such. */
6588 value->tokens = tokens;
6589 value->next = hp->value;
6590 hp->value = value;
6593 return 0;
6596 static int
6597 do_unassert (buf, limit, op, keyword)
6598 U_CHAR *buf, *limit;
6599 FILE_BUF *op ATTRIBUTE_UNUSED;
6600 struct directive *keyword ATTRIBUTE_UNUSED;
6602 const U_CHAR *bp; /* temp ptr into input buffer */
6603 const U_CHAR *symname; /* remember where symbol name starts */
6604 int sym_length; /* and how long it is */
6606 struct arglist *tokens = NULL;
6607 int tokens_specified = 0;
6609 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6610 pedwarn ("ANSI C does not allow `#unassert'");
6612 bp = buf;
6614 while (is_hor_space[*bp])
6615 bp++;
6617 symname = bp; /* remember where it starts */
6618 sym_length = check_macro_name (bp, 1);
6619 bp += sym_length;
6620 /* #define doesn't do this, but we should. */
6621 SKIP_WHITE_SPACE (bp);
6623 /* Lossage will occur if identifiers or control tokens are broken
6624 across lines using backslash. This is not the right place to take
6625 care of that. */
6627 if (*bp == '(') {
6628 int error_flag = 0;
6630 bp++; /* skip '(' */
6631 SKIP_WHITE_SPACE (bp);
6633 tokens = read_token_list (&bp, limit, &error_flag);
6634 if (error_flag)
6635 return 1;
6636 if (tokens == 0) {
6637 error ("empty token list in `#unassert'");
6638 return 1;
6641 tokens_specified = 1;
6643 ++bp; /* skip paren */
6644 SKIP_WHITE_SPACE (bp);
6648 ASSERTION_HASHNODE *hp;
6649 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6650 struct tokenlist_list *tail, *prev;
6652 hp = assertion_lookup (symname, sym_length, hashcode);
6653 if (hp == NULL)
6654 return 1;
6656 /* If no token list was specified, then eliminate this assertion
6657 entirely. */
6658 if (! tokens_specified) {
6659 struct tokenlist_list *next;
6660 for (tail = hp->value; tail; tail = next) {
6661 next = tail->next;
6662 free_token_list (tail->tokens);
6663 free (tail);
6665 delete_assertion (hp);
6666 } else {
6667 /* If a list of tokens was given, then delete any matching list. */
6669 tail = hp->value;
6670 prev = 0;
6671 while (tail) {
6672 struct tokenlist_list *next = tail->next;
6673 if (compare_token_lists (tail->tokens, tokens)) {
6674 if (prev)
6675 prev->next = next;
6676 else
6677 hp->value = tail->next;
6678 free_token_list (tail->tokens);
6679 free (tail);
6680 } else {
6681 prev = tail;
6683 tail = next;
6688 return 0;
6691 /* Test whether there is an assertion named NAME
6692 and optionally whether it has an asserted token list TOKENS.
6693 NAME is not null terminated; its length is SYM_LENGTH.
6694 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6697 check_assertion (name, sym_length, tokens_specified, tokens)
6698 const U_CHAR *name;
6699 int sym_length;
6700 int tokens_specified;
6701 struct arglist *tokens;
6703 ASSERTION_HASHNODE *hp;
6704 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6706 if (pedantic && !instack[indepth].system_header_p)
6707 pedwarn ("ANSI C does not allow testing assertions");
6709 hp = assertion_lookup (name, sym_length, hashcode);
6710 if (hp == NULL)
6711 /* It is not an assertion; just return false. */
6712 return 0;
6714 /* If no token list was specified, then value is 1. */
6715 if (! tokens_specified)
6716 return 1;
6719 struct tokenlist_list *tail;
6721 tail = hp->value;
6723 /* If a list of tokens was given,
6724 then succeed if the assertion records a matching list. */
6726 while (tail) {
6727 if (compare_token_lists (tail->tokens, tokens))
6728 return 1;
6729 tail = tail->next;
6732 /* Fail if the assertion has no matching list. */
6733 return 0;
6737 /* Compare two lists of tokens for equality including order of tokens. */
6739 static int
6740 compare_token_lists (l1, l2)
6741 struct arglist *l1, *l2;
6743 while (l1 && l2) {
6744 if (l1->length != l2->length)
6745 return 0;
6746 if (bcmp (l1->name, l2->name, l1->length))
6747 return 0;
6748 l1 = l1->next;
6749 l2 = l2->next;
6752 /* Succeed if both lists end at the same time. */
6753 return l1 == l2;
6756 /* Read a space-separated list of tokens ending in a close parenthesis.
6757 Return a list of strings, in the order they were written.
6758 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6759 Parse the text starting at *BPP, and update *BPP.
6760 Don't parse beyond LIMIT. */
6762 static struct arglist *
6763 read_token_list (bpp, limit, error_flag)
6764 const U_CHAR **bpp;
6765 const U_CHAR *limit;
6766 int *error_flag;
6768 struct arglist *token_ptrs = 0;
6769 const U_CHAR *bp = *bpp;
6770 int depth = 1;
6772 *error_flag = 0;
6774 /* Loop over the assertion value tokens. */
6775 while (depth > 0) {
6776 struct arglist *temp;
6777 U_CHAR *temp2;
6778 int eofp = 0;
6779 const U_CHAR *beg = bp;
6781 /* Find the end of the token. */
6782 if (*bp == '(') {
6783 bp++;
6784 depth++;
6785 } else if (*bp == ')') {
6786 depth--;
6787 if (depth == 0)
6788 break;
6789 bp++;
6790 } else if (*bp == '"' || *bp == '\'')
6791 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6792 else
6793 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
6794 && *bp != '"' && *bp != '\'' && bp != limit)
6795 bp++;
6797 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
6798 temp2 = (U_CHAR *) xmalloc (bp - beg + 1);
6799 bcopy ((const PTR) beg, (PTR) temp2, bp - beg);
6800 temp2[bp - beg] = 0;
6801 temp->name = temp2;
6802 temp->next = token_ptrs;
6803 token_ptrs = temp;
6804 temp->length = bp - beg;
6806 SKIP_WHITE_SPACE (bp);
6808 if (bp >= limit) {
6809 error ("unterminated token sequence in `#assert' or `#unassert'");
6810 *error_flag = -1;
6811 return 0;
6814 *bpp = bp;
6816 /* We accumulated the names in reverse order.
6817 Now reverse them to get the proper order. */
6819 register struct arglist *prev = 0, *this, *next;
6820 for (this = token_ptrs; this; this = next) {
6821 next = this->next;
6822 this->next = prev;
6823 prev = this;
6825 return prev;
6829 static void
6830 free_token_list (tokens)
6831 struct arglist *tokens;
6833 while (tokens) {
6834 struct arglist *next = tokens->next;
6835 free ((PTR) tokens->name);
6836 free (tokens);
6837 tokens = next;
6841 /* Install a name in the assertion hash table.
6843 If LEN is >= 0, it is the length of the name.
6844 Otherwise, compute the length by scanning the entire name.
6846 If HASH is >= 0, it is the precomputed hash code.
6847 Otherwise, compute the hash code. */
6849 static ASSERTION_HASHNODE *
6850 assertion_install (name, len, hash)
6851 const U_CHAR *name;
6852 int len;
6853 int hash;
6855 register ASSERTION_HASHNODE *hp;
6856 register int i, bucket;
6857 register U_CHAR *p;
6858 register const U_CHAR *q;
6860 i = sizeof (ASSERTION_HASHNODE) + len + 1;
6861 hp = (ASSERTION_HASHNODE *) xmalloc (i);
6862 bucket = hash;
6863 hp->bucket_hdr = &assertion_hashtab[bucket];
6864 hp->next = assertion_hashtab[bucket];
6865 assertion_hashtab[bucket] = hp;
6866 hp->prev = NULL;
6867 if (hp->next != NULL)
6868 hp->next->prev = hp;
6869 hp->length = len;
6870 hp->value = 0;
6871 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
6872 p = hp->name;
6873 q = name;
6874 for (i = 0; i < len; i++)
6875 *p++ = *q++;
6876 hp->name[len] = 0;
6877 return hp;
6880 /* Find the most recent hash node for name "name" (ending with first
6881 non-identifier char) installed by install
6883 If LEN is >= 0, it is the length of the name.
6884 Otherwise, compute the length by scanning the entire name.
6886 If HASH is >= 0, it is the precomputed hash code.
6887 Otherwise, compute the hash code. */
6889 static ASSERTION_HASHNODE *
6890 assertion_lookup (name, len, hash)
6891 const U_CHAR *name;
6892 int len;
6893 int hash;
6895 register ASSERTION_HASHNODE *bucket;
6897 bucket = assertion_hashtab[hash];
6898 while (bucket) {
6899 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
6900 return bucket;
6901 bucket = bucket->next;
6903 return NULL;
6906 static void
6907 delete_assertion (hp)
6908 ASSERTION_HASHNODE *hp;
6911 if (hp->prev != NULL)
6912 hp->prev->next = hp->next;
6913 if (hp->next != NULL)
6914 hp->next->prev = hp->prev;
6916 /* Make sure that the bucket chain header that the deleted guy was
6917 on points to the right thing afterwards. */
6918 if (hp == *hp->bucket_hdr)
6919 *hp->bucket_hdr = hp->next;
6921 free (hp);
6925 * interpret #line directive. Remembers previously seen fnames
6926 * in its very own hash table.
6928 #define FNAME_HASHSIZE 37
6930 static int
6931 do_line (buf, limit, op, keyword)
6932 U_CHAR *buf, *limit;
6933 FILE_BUF *op;
6934 struct directive *keyword ATTRIBUTE_UNUSED;
6936 register U_CHAR *bp;
6937 FILE_BUF *ip = &instack[indepth];
6938 FILE_BUF tem;
6939 int new_lineno;
6940 enum file_change_code file_change = same_file;
6942 /* Expand any macros. */
6943 tem = expand_to_temp_buffer (buf, limit, 0, 0);
6945 /* Point to macroexpanded line, which is null-terminated now. */
6946 bp = tem.buf;
6947 limit = tem.bufp;
6948 SKIP_WHITE_SPACE (bp);
6950 if (!ISDIGIT (*bp)) {
6951 error ("invalid format `#line' directive");
6952 return 0;
6955 /* The Newline at the end of this line remains to be processed.
6956 To put the next line at the specified line number,
6957 we must store a line number now that is one less. */
6958 new_lineno = atoi ((char *) bp) - 1;
6960 /* NEW_LINENO is one less than the actual line number here. */
6961 if (pedantic && new_lineno < 0)
6962 pedwarn ("line number out of range in `#line' directive");
6964 /* skip over the line number. */
6965 while (ISDIGIT (*bp))
6966 bp++;
6968 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6969 if (*bp && !is_space[*bp]) {
6970 error ("invalid format `#line' directive");
6971 return;
6973 #endif
6975 SKIP_WHITE_SPACE (bp);
6977 if (*bp == '\"') {
6978 static HASHNODE *fname_table[FNAME_HASHSIZE];
6979 HASHNODE *hp, **hash_bucket;
6980 U_CHAR *fname, *p;
6981 int fname_length;
6983 fname = ++bp;
6985 /* Turn the file name, which is a character string literal,
6986 into a null-terminated string. Do this in place. */
6987 p = bp;
6988 for (;;)
6989 switch ((*p++ = *bp++)) {
6990 case '\\':
6991 if (! ignore_escape_flag)
6993 char *bpc = (char *) bp;
6994 HOST_WIDEST_INT c = parse_escape (&bpc, (HOST_WIDEST_INT) (U_CHAR) (-1));
6995 bp = (U_CHAR *) bpc;
6996 if (c < 0)
6997 p--;
6998 else
6999 p[-1] = c;
7001 break;
7003 case '\"':
7004 *--p = 0;
7005 goto fname_done;
7007 fname_done:
7008 fname_length = p - fname;
7010 SKIP_WHITE_SPACE (bp);
7011 if (*bp) {
7012 if (pedantic)
7013 pedwarn ("garbage at end of `#line' directive");
7014 if (*bp == '1')
7015 file_change = enter_file;
7016 else if (*bp == '2')
7017 file_change = leave_file;
7018 else if (*bp == '3')
7019 ip->system_header_p = 1;
7020 else if (*bp == '4')
7021 ip->system_header_p = 2;
7022 else {
7023 error ("invalid format `#line' directive");
7024 return 0;
7027 bp++;
7028 SKIP_WHITE_SPACE (bp);
7029 if (*bp == '3') {
7030 ip->system_header_p = 1;
7031 bp++;
7032 SKIP_WHITE_SPACE (bp);
7034 if (*bp == '4') {
7035 ip->system_header_p = 2;
7036 bp++;
7037 SKIP_WHITE_SPACE (bp);
7039 if (*bp) {
7040 error ("invalid format `#line' directive");
7041 return 0;
7045 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
7046 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
7047 if (hp->length == fname_length &&
7048 bcmp (hp->value.cpval, fname, fname_length) == 0) {
7049 ip->nominal_fname = hp->value.cpval;
7050 ip->nominal_fname_len = fname_length;
7051 break;
7053 if (hp == 0) {
7054 /* Didn't find it; cons up a new one. */
7055 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
7056 hp->next = *hash_bucket;
7057 *hash_bucket = hp;
7059 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
7060 ip->nominal_fname_len = hp->length = fname_length;
7061 bcopy (fname, ((char *) hp) + sizeof (HASHNODE), fname_length + 1);
7063 } else if (*bp) {
7064 error ("invalid format `#line' directive");
7065 return 0;
7068 ip->lineno = new_lineno;
7069 output_line_directive (ip, op, 0, file_change);
7070 check_expand (op, ip->length - (ip->bufp - ip->buf));
7071 return 0;
7074 /* Remove the definition of a symbol from the symbol table.
7075 according to un*x /lib/cpp, it is not an error to undef
7076 something that has no definitions, so it isn't one here either. */
7078 static int
7079 do_undef (buf, limit, op, keyword)
7080 U_CHAR *buf, *limit;
7081 FILE_BUF *op;
7082 struct directive *keyword;
7084 int sym_length;
7085 HASHNODE *hp;
7086 U_CHAR *orig_buf = buf;
7088 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7089 if (pcp_outfile && op)
7090 pass_thru_directive (buf, limit, op, keyword);
7092 SKIP_WHITE_SPACE (buf);
7093 sym_length = check_macro_name (buf, 0);
7095 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
7096 /* If we are generating additional info for debugging (with -g) we
7097 need to pass through all effective #undef directives. */
7098 if (debug_output && op)
7099 pass_thru_directive (orig_buf, limit, op, keyword);
7100 if (hp->type == T_POISON)
7101 error ("cannot undefine poisoned `%s'", hp->name);
7102 else {
7103 if (hp->type != T_MACRO)
7104 warning ("undefining `%s'", hp->name);
7105 delete_macro (hp);
7109 if (pedantic) {
7110 buf += sym_length;
7111 SKIP_WHITE_SPACE (buf);
7112 if (buf != limit)
7113 pedwarn ("garbage after `#undef' directive");
7115 return 0;
7119 /* Report an error detected by the program we are processing.
7120 Use the text of the line in the error message. */
7122 static int
7123 do_error (buf, limit, op, keyword)
7124 U_CHAR *buf, *limit;
7125 FILE_BUF *op ATTRIBUTE_UNUSED;
7126 struct directive *keyword;
7128 int length = limit - buf;
7129 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
7130 bcopy ((const PTR) buf, (PTR) copy, length);
7131 copy[length] = 0;
7132 SKIP_WHITE_SPACE (copy);
7134 switch (keyword->type) {
7135 case T_ERROR:
7136 error ("#error %s", copy);
7137 break;
7139 case T_WARNING:
7140 if (pedantic && !instack[indepth].system_header_p)
7141 pedwarn ("ANSI C does not allow `#warning'");
7142 warning ("#warning %s", copy);
7143 break;
7145 default:
7146 abort ();
7149 return 0;
7151 /* Remember the name of the current file being read from so that we can
7152 avoid ever including it again. */
7154 static void
7155 do_once ()
7157 int i;
7159 for (i = indepth; i >= 0; i--)
7160 if (instack[i].inc) {
7161 record_control_macro (instack[i].inc, (const U_CHAR *) "");
7162 break;
7166 /* Report program identification. */
7168 static int
7169 do_ident (buf, limit, op, keyword)
7170 U_CHAR *buf, *limit;
7171 FILE_BUF *op;
7172 struct directive *keyword ATTRIBUTE_UNUSED;
7174 FILE_BUF trybuf;
7175 int len;
7177 /* Allow #ident in system headers, since that's not user's fault. */
7178 if (pedantic && !instack[indepth].system_header_p)
7179 pedwarn ("ANSI C does not allow `#ident'");
7181 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
7182 buf = trybuf.buf;
7183 len = trybuf.bufp - buf;
7185 /* Output expanded directive. */
7186 check_expand (op, 7 + len);
7187 bcopy ("#ident ", (char *) op->bufp, 7);
7188 op->bufp += 7;
7189 bcopy ((const PTR) buf, (PTR) op->bufp, len);
7190 op->bufp += len;
7192 free (buf);
7193 return 0;
7196 /* #pragma and its argument line have already been copied to the output file.
7197 Just check for some recognized pragmas that need validation here. */
7199 static int
7200 do_pragma (buf, limit, op, keyword)
7201 U_CHAR *buf, *limit;
7202 FILE_BUF *op;
7203 struct directive *keyword;
7205 SKIP_WHITE_SPACE (buf);
7206 if (!strncmp ((char *) buf, "once", 4)) {
7207 /* Allow #pragma once in system headers, since that's not the user's
7208 fault. */
7209 if (!instack[indepth].system_header_p)
7210 warning ("`#pragma once' is obsolete");
7211 do_once ();
7214 if (!strncmp ((char *) buf, "poison", 6)) {
7215 /* Poison these symbols so that all subsequent usage produces an
7216 error message. */
7217 U_CHAR *p = buf + 6;
7219 SKIP_WHITE_SPACE (p);
7220 while (p < limit)
7222 U_CHAR *end = p;
7224 while (end < limit && is_idchar[*end])
7225 end++;
7226 if (end < limit && !is_space[*end])
7228 error ("invalid #pragma poison");
7229 return 0;
7231 do_define(p, end, op, keyword);
7232 p = end;
7233 SKIP_WHITE_SPACE (p);
7237 if (!strncmp ((char *) buf, "implementation", 14)) {
7238 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7239 been included yet. */
7241 int h;
7242 U_CHAR *p = buf + 14, *fname;
7243 SKIP_WHITE_SPACE (p);
7244 if (*p != '\"')
7245 return 0;
7247 fname = p + 1;
7248 p = skip_quoted_string (p, limit, 0, NULL_PTR, NULL_PTR, NULL_PTR);
7249 if (p[-1] == '"')
7250 *--p = '\0';
7252 for (h = 0; h < INCLUDE_HASHSIZE; h++) {
7253 struct include_file *inc;
7254 for (inc = include_hashtab[h]; inc; inc = inc->next) {
7255 if (!strcmp (base_name (inc->fname), (char *) fname)) {
7256 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname);
7257 return 0;
7262 return 0;
7265 #if 0
7266 /* This was a fun hack, but #pragma seems to start to be useful.
7267 By failing to recognize it, we pass it through unchanged to cc1. */
7269 /* The behavior of the #pragma directive is implementation defined.
7270 this implementation defines it as follows. */
7272 static int
7273 do_pragma ()
7275 close (0);
7276 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
7277 goto nope;
7278 close (1);
7279 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
7280 goto nope;
7281 execl ("/usr/games/hack", "#pragma", 0);
7282 execl ("/usr/games/rogue", "#pragma", 0);
7283 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7284 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7285 nope:
7286 fatal ("You are in a maze of twisty compiler features, all different");
7288 #endif
7290 #ifdef SCCS_DIRECTIVE
7292 /* Just ignore #sccs, on systems where we define it at all. */
7294 static int
7295 do_sccs (buf, limit, op, keyword)
7296 U_CHAR *buf ATTRIBUTE_UNUSED;
7297 U_CHAR *limit ATTRIBUTE_UNUSED;
7298 FILE_BUF *op ATTRIBUTE_UNUSED;
7299 struct directive *keyword ATTRIBUTE_UNUSED;
7301 if (pedantic)
7302 pedwarn ("ANSI C does not allow `#sccs'");
7303 return 0;
7306 #endif /* defined (SCCS_DIRECTIVE) */
7308 /* Handle #if directive by
7309 1) inserting special `defined' keyword into the hash table
7310 that gets turned into 0 or 1 by special_symbol (thus,
7311 if the luser has a symbol called `defined' already, it won't
7312 work inside the #if directive)
7313 2) rescan the input into a temporary output buffer
7314 3) pass the output buffer to the yacc parser and collect a value
7315 4) clean up the mess left from steps 1 and 2.
7316 5) call conditional_skip to skip til the next #endif (etc.),
7317 or not, depending on the value from step 3. */
7319 static int
7320 do_if (buf, limit, op, keyword)
7321 U_CHAR *buf, *limit;
7322 FILE_BUF *op;
7323 struct directive *keyword ATTRIBUTE_UNUSED;
7325 HOST_WIDEST_INT value;
7326 FILE_BUF *ip = &instack[indepth];
7328 value = eval_if_expression (buf, limit - buf);
7329 conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
7330 return 0;
7333 /* Handle a #elif directive by not changing if_stack either.
7334 see the comment above do_else. */
7336 static int
7337 do_elif (buf, limit, op, keyword)
7338 U_CHAR *buf, *limit;
7339 FILE_BUF *op;
7340 struct directive *keyword ATTRIBUTE_UNUSED;
7342 HOST_WIDEST_INT value;
7343 FILE_BUF *ip = &instack[indepth];
7345 if (if_stack == instack[indepth].if_stack) {
7346 error ("`#elif' not within a conditional");
7347 return 0;
7348 } else {
7349 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7350 error ("`#elif' after `#else'");
7351 fprintf (stderr, " (matches line %d", if_stack->lineno);
7352 if (! (if_stack->fname_len == ip->nominal_fname_len
7353 && !bcmp (if_stack->fname, ip->nominal_fname,
7354 if_stack->fname_len))) {
7355 fprintf (stderr, ", file ");
7356 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7357 if_stack->fname_len, stderr);
7359 fprintf (stderr, ")\n");
7361 if_stack->type = T_ELIF;
7364 if (if_stack->if_succeeded)
7365 skip_if_group (ip, 0, op);
7366 else {
7367 value = eval_if_expression (buf, limit - buf);
7368 if (value == 0)
7369 skip_if_group (ip, 0, op);
7370 else {
7371 ++if_stack->if_succeeded; /* continue processing input */
7372 output_line_directive (ip, op, 1, same_file);
7375 return 0;
7378 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7379 result as a C expression and return the value as an int. */
7381 static HOST_WIDEST_INT
7382 eval_if_expression (buf, length)
7383 const U_CHAR *buf;
7384 int length;
7386 FILE_BUF temp_obuf;
7387 HASHNODE *save_defined;
7388 HOST_WIDEST_INT value;
7390 save_defined = install ((const U_CHAR *) "defined", -1, T_SPEC_DEFINED,
7391 NULL_PTR, -1);
7392 pcp_inside_if = 1;
7393 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
7394 pcp_inside_if = 0;
7395 delete_macro (save_defined); /* clean up special symbol */
7397 *temp_obuf.bufp = '\n';
7398 value = parse_c_expression ((char *) temp_obuf.buf,
7399 warn_undef && !instack[indepth].system_header_p);
7401 free (temp_obuf.buf);
7403 return value;
7406 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7407 or don't skip to the #endif/#else/#elif depending on what directive
7408 is actually being processed. */
7410 static int
7411 do_xifdef (buf, limit, op, keyword)
7412 U_CHAR *buf, *limit;
7413 FILE_BUF *op;
7414 struct directive *keyword;
7416 int skip;
7417 FILE_BUF *ip = &instack[indepth];
7418 U_CHAR *end;
7419 int start_of_file = 0;
7420 U_CHAR *control_macro = 0;
7422 /* Detect a #ifndef at start of file (not counting comments). */
7423 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
7424 U_CHAR *p = ip->buf;
7425 while (p != directive_start) {
7426 U_CHAR c = *p++;
7427 if (is_space[c])
7429 /* Make no special provision for backslash-newline here; this is
7430 slower if backslash-newlines are present, but it's correct,
7431 and it's not worth it to tune for the rare backslash-newline. */
7432 else if (c == '/'
7433 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7434 /* Skip this comment. */
7435 int junk = 0;
7436 U_CHAR *save_bufp = ip->bufp;
7437 ip->bufp = p + 1;
7438 p = skip_to_end_of_comment (ip, &junk, 1);
7439 ip->bufp = save_bufp;
7440 } else {
7441 goto fail;
7444 /* If we get here, this conditional is the beginning of the file. */
7445 start_of_file = 1;
7446 fail: ;
7449 /* Discard leading and trailing whitespace. */
7450 SKIP_WHITE_SPACE (buf);
7451 while (limit != buf && is_hor_space[limit[-1]]) limit--;
7453 /* Find the end of the identifier at the beginning. */
7454 for (end = buf; is_idchar[*end]; end++);
7456 if (end == buf) {
7457 skip = (keyword->type == T_IFDEF);
7458 if (! traditional)
7460 if (end == limit)
7461 pedwarn ("`#%s' with no argument", keyword->name);
7462 else
7463 pedwarn ("`#%s' argument starts with punctuation", keyword->name);
7465 } else {
7466 HASHNODE *hp;
7468 if (! traditional) {
7469 if (ISDIGIT (buf[0]))
7470 pedwarn ("`#%s' argument starts with a digit", keyword->name);
7471 else if (end != limit)
7472 pedwarn ("garbage at end of `#%s' argument", keyword->name);
7475 hp = lookup (buf, end-buf, -1);
7477 if (pcp_outfile) {
7478 /* Output a precondition for this macro. */
7479 if (hp
7480 && (hp->type == T_CONST
7481 || (hp->type == T_MACRO && hp->value.defn->predefined)))
7482 fprintf (pcp_outfile, "#define %s\n", hp->name);
7483 else {
7484 U_CHAR *cp = buf;
7485 fprintf (pcp_outfile, "#undef ");
7486 while (is_idchar[*cp]) /* Ick! */
7487 fputc (*cp++, pcp_outfile);
7488 putc ('\n', pcp_outfile);
7492 if ((hp != NULL) && (hp->type == T_POISON)) {
7493 error("attempt to use poisoned `%s'.", hp->name);
7494 hp = NULL;
7496 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
7497 if (start_of_file && !skip) {
7498 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
7499 bcopy ((const PTR) buf, (PTR) control_macro, end - buf);
7500 control_macro[end - buf] = 0;
7504 conditional_skip (ip, skip, T_IF, control_macro, op);
7505 return 0;
7508 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7509 If this is a #ifndef starting at the beginning of a file,
7510 CONTROL_MACRO is the macro name tested by the #ifndef.
7511 Otherwise, CONTROL_MACRO is 0. */
7513 static void
7514 conditional_skip (ip, skip, type, control_macro, op)
7515 FILE_BUF *ip;
7516 int skip;
7517 enum node_type type;
7518 const U_CHAR *control_macro;
7519 FILE_BUF *op;
7521 IF_STACK_FRAME *temp;
7523 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7524 temp->fname = ip->nominal_fname;
7525 temp->fname_len = ip->nominal_fname_len;
7526 temp->lineno = ip->lineno;
7527 temp->next = if_stack;
7528 temp->control_macro = control_macro;
7529 if_stack = temp;
7531 if_stack->type = type;
7533 if (skip != 0) {
7534 skip_if_group (ip, 0, op);
7535 return;
7536 } else {
7537 ++if_stack->if_succeeded;
7538 output_line_directive (ip, &outbuf, 1, same_file);
7542 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7543 Leaves input ptr at the sharp sign found.
7544 If ANY is nonzero, return at next directive of any sort. */
7546 static void
7547 skip_if_group (ip, any, op)
7548 FILE_BUF *ip;
7549 int any;
7550 FILE_BUF *op;
7552 register U_CHAR *bp = ip->bufp, *cp;
7553 register U_CHAR *endb = ip->buf + ip->length;
7554 struct directive *kt;
7555 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
7556 U_CHAR *beg_of_line = bp;
7557 register int ident_length;
7558 U_CHAR *ident, *after_ident;
7559 /* Save info about where the group starts. */
7560 U_CHAR *beg_of_group = bp;
7561 int beg_lineno = ip->lineno;
7562 int skipping_include_directive = 0;
7564 if (output_conditionals && op != 0) {
7565 static const char * const ptr = "#failed\n";
7566 int len = strlen (ptr);
7568 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7570 *op->bufp++ = '\n';
7571 op->lineno++;
7573 check_expand (op, len);
7574 bcopy (ptr, (char *) op->bufp, len);
7575 op->bufp += len;
7576 op->lineno++;
7577 output_line_directive (ip, op, 1, 0);
7580 while (bp < endb) {
7581 switch (*bp++) {
7582 case '/': /* possible comment */
7583 if (*bp == '\\')
7584 newline_fix (bp);
7585 if (*bp == '*'
7586 || (cplusplus_comments && *bp == '/')) {
7587 ip->bufp = ++bp;
7588 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
7590 break;
7591 case '<':
7592 if (skipping_include_directive) {
7593 while (bp < endb && *bp != '>' && *bp != '\n') {
7594 if (*bp == '\\' && bp[1] == '\n') {
7595 ip->lineno++;
7596 bp++;
7598 bp++;
7601 break;
7602 case '\"':
7603 if (skipping_include_directive) {
7604 while (bp < endb && *bp != '\n') {
7605 if (*bp == '"') {
7606 bp++;
7607 break;
7609 if (*bp == '\\' && bp[1] == '\n') {
7610 ip->lineno++;
7611 bp++;
7613 bp++;
7615 break;
7617 /* Fall through. */
7618 case '\'':
7619 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
7620 NULL_PTR, NULL_PTR);
7621 break;
7622 case '\\':
7623 /* Char after backslash loses its special meaning in some cases. */
7624 if (*bp == '\n') {
7625 ++ip->lineno;
7626 bp++;
7627 } else if (traditional && bp < endb)
7628 bp++;
7629 break;
7630 case '\n':
7631 ++ip->lineno;
7632 beg_of_line = bp;
7633 skipping_include_directive = 0;
7634 break;
7635 case '%':
7636 if (beg_of_line == 0 || traditional)
7637 break;
7638 ip->bufp = bp - 1;
7639 while (bp[0] == '\\' && bp[1] == '\n')
7640 bp += 2;
7641 if (*bp == ':')
7642 goto sharp_token;
7643 break;
7644 case '#':
7645 /* # keyword: a # must be first nonblank char on the line */
7646 if (beg_of_line == 0)
7647 break;
7648 ip->bufp = bp - 1;
7649 sharp_token:
7650 /* Scan from start of line, skipping whitespace, comments
7651 and backslash-newlines, and see if we reach this #.
7652 If not, this # is not special. */
7653 bp = beg_of_line;
7654 /* If -traditional, require # to be at beginning of line. */
7655 if (!traditional) {
7656 while (1) {
7657 if (is_hor_space[*bp])
7658 bp++;
7659 else if (*bp == '\\' && bp[1] == '\n')
7660 bp += 2;
7661 else if (*bp == '/' && bp[1] == '*') {
7662 bp += 2;
7663 while (1)
7665 if (*bp == '*')
7667 if (bp[1] == '/')
7669 bp += 2;
7670 break;
7673 else
7675 #ifdef MULTIBYTE_CHARS
7676 int length;
7677 length = local_mblen (bp, endb - bp);
7678 if (length > 1)
7679 bp += (length - 1);
7680 #endif
7682 bp++;
7685 /* There is no point in trying to deal with C++ // comments here,
7686 because if there is one, then this # must be part of the
7687 comment and we would never reach here. */
7688 else break;
7691 if (bp != ip->bufp) {
7692 bp = ip->bufp + 1; /* Reset bp to after the #. */
7693 break;
7696 bp = ip->bufp + 1; /* Point after the '#' */
7697 if (ip->bufp[0] == '%') {
7698 /* Skip past the ':' again. */
7699 while (*bp == '\\') {
7700 ip->lineno++;
7701 bp += 2;
7703 bp++;
7706 /* Skip whitespace and \-newline. */
7707 while (1) {
7708 if (is_hor_space[*bp])
7709 bp++;
7710 else if (*bp == '\\' && bp[1] == '\n')
7711 bp += 2;
7712 else if (*bp == '/') {
7713 if (bp[1] == '\\')
7714 newline_fix (bp + 1);
7715 if (bp[1] == '*') {
7716 for (bp += 2; ; bp++) {
7717 if (*bp == '\n')
7718 ip->lineno++;
7719 else if (*bp == '*') {
7720 if (bp[-1] == '/' && warn_comments)
7721 warning ("`/*' within comment");
7722 if (bp[1] == '\\')
7723 newline_fix (bp + 1);
7724 if (bp[1] == '/')
7725 break;
7727 else
7729 #ifdef MULTIBYTE_CHARS
7730 int length;
7731 length = local_mblen (bp, endb - bp);
7732 if (length > 1)
7733 bp += (length - 1);
7734 #endif
7737 bp += 2;
7738 } else if (bp[1] == '/' && cplusplus_comments) {
7739 for (bp += 2; ; bp++) {
7740 if (*bp == '\n')
7741 break;
7742 if (*bp == '\\' && bp[1] == '\n')
7744 if (warn_comments)
7745 warning ("multiline `//' comment");
7746 ip->lineno++;
7747 bp++;
7749 else
7751 #ifdef MULTIBYTE_CHARS
7752 int length;
7753 length = local_mblen (bp, endb - bp);
7754 if (length > 1)
7755 bp += (length - 1);
7756 #endif
7759 } else
7760 break;
7761 } else
7762 break;
7765 cp = bp;
7767 /* Now find end of directive name.
7768 If we encounter a backslash-newline, exchange it with any following
7769 symbol-constituents so that we end up with a contiguous name. */
7771 while (1) {
7772 if (is_idchar[*bp])
7773 bp++;
7774 else {
7775 if (*bp == '\\')
7776 name_newline_fix (bp);
7777 if (is_idchar[*bp])
7778 bp++;
7779 else break;
7782 ident_length = bp - cp;
7783 ident = cp;
7784 after_ident = bp;
7786 /* A line of just `#' becomes blank. */
7788 if (ident_length == 0 && *after_ident == '\n') {
7789 continue;
7792 if (ident_length == 0 || !is_idstart[*ident]) {
7793 U_CHAR *p = ident;
7794 while (is_idchar[*p]) {
7795 if (*p < '0' || *p > '9')
7796 break;
7797 p++;
7799 /* Handle # followed by a line number. */
7800 if (p != ident && !is_idchar[*p]) {
7801 if (pedantic)
7802 pedwarn ("`#' followed by integer");
7803 continue;
7806 /* Avoid error for `###' and similar cases unless -pedantic. */
7807 if (p == ident) {
7808 while (*p == '#' || is_hor_space[*p]) p++;
7809 if (*p == '\n') {
7810 if (pedantic && !lang_asm)
7811 pedwarn ("invalid preprocessing directive");
7812 continue;
7816 if (!lang_asm && pedantic)
7817 pedwarn ("invalid preprocessing directive name");
7818 continue;
7821 for (kt = directive_table; kt->length >= 0; kt++) {
7822 IF_STACK_FRAME *temp;
7823 if (ident_length == kt->length
7824 && bcmp (cp, kt->name, kt->length) == 0) {
7825 /* If we are asked to return on next directive, do so now. */
7826 if (any)
7827 goto done;
7829 switch (kt->type) {
7830 case T_IF:
7831 case T_IFDEF:
7832 case T_IFNDEF:
7833 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7834 temp->next = if_stack;
7835 if_stack = temp;
7836 temp->lineno = ip->lineno;
7837 temp->fname = ip->nominal_fname;
7838 temp->fname_len = ip->nominal_fname_len;
7839 temp->type = kt->type;
7840 break;
7841 case T_ELSE:
7842 case T_ENDIF:
7843 if (pedantic && if_stack != save_if_stack)
7844 validate_else (bp, endb);
7845 case T_ELIF:
7846 if (if_stack == instack[indepth].if_stack) {
7847 error ("`#%s' not within a conditional", kt->name);
7848 break;
7850 else if (if_stack == save_if_stack)
7851 goto done; /* found what we came for */
7853 if (kt->type != T_ENDIF) {
7854 if (if_stack->type == T_ELSE)
7855 error ("`#else' or `#elif' after `#else'");
7856 if_stack->type = kt->type;
7857 break;
7860 temp = if_stack;
7861 if_stack = if_stack->next;
7862 free (temp);
7863 break;
7865 case T_INCLUDE:
7866 case T_INCLUDE_NEXT:
7867 case T_IMPORT:
7868 skipping_include_directive = 1;
7869 break;
7871 default:
7872 break;
7874 break;
7877 /* Don't let erroneous code go by. */
7878 if (kt->length < 0 && !lang_asm && pedantic)
7879 pedwarn ("invalid preprocessing directive name");
7883 ip->bufp = bp;
7884 /* after this returns, rescan will exit because ip->bufp
7885 now points to the end of the buffer.
7886 rescan is responsible for the error message also. */
7888 done:
7889 if (output_conditionals && op != 0) {
7890 static const char * const ptr = "#endfailed\n";
7891 int len = strlen (ptr);
7893 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7895 *op->bufp++ = '\n';
7896 op->lineno++;
7898 check_expand (op, beg_of_line - beg_of_group);
7899 bcopy ((const PTR) beg_of_group, (PTR) op->bufp,
7900 beg_of_line - beg_of_group);
7901 op->bufp += beg_of_line - beg_of_group;
7902 op->lineno += ip->lineno - beg_lineno;
7903 check_expand (op, len);
7904 bcopy (ptr, (char *) op->bufp, len);
7905 op->bufp += len;
7906 op->lineno++;
7910 /* Handle a #else directive. Do this by just continuing processing
7911 without changing if_stack ; this is so that the error message
7912 for missing #endif's etc. will point to the original #if. It
7913 is possible that something different would be better. */
7915 static int
7916 do_else (buf, limit, op, keyword)
7917 U_CHAR *buf, *limit;
7918 FILE_BUF *op;
7919 struct directive *keyword ATTRIBUTE_UNUSED;
7921 FILE_BUF *ip = &instack[indepth];
7923 if (pedantic) {
7924 SKIP_WHITE_SPACE (buf);
7925 if (buf != limit)
7926 pedwarn ("text following `#else' violates ANSI standard");
7929 if (if_stack == instack[indepth].if_stack) {
7930 error ("`#else' not within a conditional");
7931 return 0;
7932 } else {
7933 /* #ifndef can't have its special treatment for containing the whole file
7934 if it has a #else clause. */
7935 if_stack->control_macro = 0;
7937 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7938 error ("`#else' after `#else'");
7939 fprintf (stderr, " (matches line %d", if_stack->lineno);
7940 if (! (if_stack->fname_len == ip->nominal_fname_len
7941 && !bcmp (if_stack->fname, ip->nominal_fname,
7942 if_stack->fname_len))) {
7943 fprintf (stderr, ", file ");
7944 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7945 if_stack->fname_len, stderr);
7947 fprintf (stderr, ")\n");
7949 if_stack->type = T_ELSE;
7952 if (if_stack->if_succeeded)
7953 skip_if_group (ip, 0, op);
7954 else {
7955 ++if_stack->if_succeeded; /* continue processing input */
7956 output_line_directive (ip, op, 1, same_file);
7958 return 0;
7961 /* Unstack after #endif directive. */
7963 static int
7964 do_endif (buf, limit, op, keyword)
7965 U_CHAR *buf, *limit;
7966 FILE_BUF *op;
7967 struct directive *keyword ATTRIBUTE_UNUSED;
7969 if (pedantic) {
7970 SKIP_WHITE_SPACE (buf);
7971 if (buf != limit)
7972 pedwarn ("text following `#endif' violates ANSI standard");
7975 if (if_stack == instack[indepth].if_stack)
7976 error ("unbalanced `#endif'");
7977 else {
7978 IF_STACK_FRAME *temp = if_stack;
7979 if_stack = if_stack->next;
7980 if (temp->control_macro != 0) {
7981 /* This #endif matched a #ifndef at the start of the file.
7982 See if it is at the end of the file. */
7983 FILE_BUF *ip = &instack[indepth];
7984 U_CHAR *p = ip->bufp;
7985 U_CHAR *ep = ip->buf + ip->length;
7987 while (p != ep) {
7988 U_CHAR c = *p++;
7989 if (!is_space[c]) {
7990 if (c == '/'
7991 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7992 /* Skip this comment. */
7993 int junk = 0;
7994 U_CHAR *save_bufp = ip->bufp;
7995 ip->bufp = p + 1;
7996 p = skip_to_end_of_comment (ip, &junk, 1);
7997 ip->bufp = save_bufp;
7998 } else
7999 goto fail;
8002 /* If we get here, this #endif ends a #ifndef
8003 that contains all of the file (aside from whitespace).
8004 Arrange not to include the file again
8005 if the macro that was tested is defined.
8007 Do not do this for the top-level file in a -include or any
8008 file in a -imacros. */
8009 if (indepth != 0
8010 && ! (indepth == 1 && no_record_file)
8011 && ! (no_record_file && no_output))
8012 record_control_macro (ip->inc, temp->control_macro);
8013 fail: ;
8015 free (temp);
8016 output_line_directive (&instack[indepth], op, 1, same_file);
8018 return 0;
8021 /* When an #else or #endif is found while skipping failed conditional,
8022 if -pedantic was specified, this is called to warn about text after
8023 the directive name. P points to the first char after the directive
8024 name. */
8026 static void
8027 validate_else (p, limit)
8028 register const U_CHAR *p;
8029 register const U_CHAR *limit;
8031 /* Advance P over whitespace and comments. */
8032 while (1) {
8033 while (*p == '\\' && p[1] == '\n')
8034 p += 2;
8035 if (is_hor_space[*p])
8036 p++;
8037 else if (*p == '/') {
8038 while (p[1] == '\\' && p[2] == '\n')
8039 p += 2;
8040 if (p[1] == '*') {
8041 /* Don't bother warning about unterminated comments
8042 since that will happen later. Just be sure to exit. */
8043 for (p += 2; ; p++) {
8044 if (p == limit)
8045 return;
8046 if (*p == '*') {
8047 while (p[1] == '\\' && p[2] == '\n')
8048 p += 2;
8049 if (p[1] == '/') {
8050 p += 2;
8051 break;
8054 else
8056 #ifdef MULTIBYTE_CHARS
8057 int length;
8058 length = local_mblen (p, limit - p);
8059 if (length > 1)
8060 p += (length - 1);
8061 #endif
8065 else if (cplusplus_comments && p[1] == '/')
8066 return;
8067 else break;
8068 } else break;
8070 if (*p != '\n')
8071 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8074 /* Skip a comment, assuming the input ptr immediately follows the
8075 initial slash-star. Bump *LINE_COUNTER for each newline.
8076 (The canonical line counter is &ip->lineno.)
8077 Don't use this routine (or the next one) if bumping the line
8078 counter is not sufficient to deal with newlines in the string.
8080 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8081 This feature is useful when processing a comment that is going to
8082 be processed or was processed at another point in the preprocessor,
8083 to avoid a duplicate warning. Likewise for unterminated comment
8084 errors. */
8086 static U_CHAR *
8087 skip_to_end_of_comment (ip, line_counter, nowarn)
8088 register FILE_BUF *ip;
8089 int *line_counter; /* place to remember newlines, or NULL */
8090 int nowarn;
8092 register U_CHAR *limit = ip->buf + ip->length;
8093 register U_CHAR *bp = ip->bufp;
8094 FILE_BUF *op = put_out_comments && !line_counter ? &outbuf : (FILE_BUF *) 0;
8095 int start_line = line_counter ? *line_counter : 0;
8097 /* JF this line_counter stuff is a crock to make sure the
8098 comment is only put out once, no matter how many times
8099 the comment is skipped. It almost works */
8100 if (op) {
8101 *op->bufp++ = '/';
8102 *op->bufp++ = bp[-1];
8104 if (cplusplus_comments && bp[-1] == '/') {
8105 for (; bp < limit; bp++) {
8106 if (*bp == '\n')
8107 break;
8108 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
8110 if (!nowarn && warn_comments)
8111 warning ("multiline `//' comment");
8112 if (line_counter)
8113 ++*line_counter;
8114 if (op)
8116 ++op->lineno;
8117 *op->bufp++ = *bp;
8119 ++bp;
8121 else
8123 #ifdef MULTIBYTE_CHARS
8124 int length;
8125 length = local_mblen (bp, limit - bp);
8126 if (length > 1)
8128 if (op)
8130 bcopy (bp, op->bufp, length - 1);
8131 op->bufp += (length - 1);
8133 bp += (length - 1);
8135 #endif
8137 if (op)
8138 *op->bufp++ = *bp;
8140 ip->bufp = bp;
8141 return bp;
8143 while (bp < limit) {
8144 if (op)
8145 *op->bufp++ = *bp;
8146 switch (*bp++) {
8147 case '\n':
8148 /* If this is the end of the file, we have an unterminated comment.
8149 Don't swallow the newline. We are guaranteed that there will be a
8150 trailing newline and various pieces assume it's there. */
8151 if (bp == limit)
8153 --bp;
8154 --limit;
8155 break;
8157 if (line_counter != NULL)
8158 ++*line_counter;
8159 if (op)
8160 ++op->lineno;
8161 break;
8162 case '*':
8163 if (bp[-2] == '/' && !nowarn && warn_comments)
8164 warning ("`/*' within comment");
8165 if (*bp == '\\')
8166 newline_fix (bp);
8167 if (*bp == '/') {
8168 if (op)
8169 *op->bufp++ = '/';
8170 ip->bufp = ++bp;
8171 return bp;
8173 break;
8174 #ifdef MULTIBYTE_CHARS
8175 default:
8177 int length;
8178 bp--;
8179 length = local_mblen (bp, limit - bp);
8180 if (length <= 0)
8181 length = 1;
8182 if (op)
8184 op->bufp--;
8185 bcopy (bp, op->bufp, length);
8186 op->bufp += length;
8188 bp += length;
8190 #endif
8194 if (!nowarn)
8195 error_with_line (line_for_error (start_line), "unterminated comment");
8196 ip->bufp = bp;
8197 return bp;
8200 /* Skip over a quoted string. BP points to the opening quote.
8201 Returns a pointer after the closing quote. Don't go past LIMIT.
8202 START_LINE is the line number of the starting point (but it need
8203 not be valid if the starting point is inside a macro expansion).
8205 The input stack state is not changed.
8207 If COUNT_NEWLINES is nonzero, it points to an int to increment
8208 for each newline passed; also, warn about any white space
8209 just before line end.
8211 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8212 if we pass a backslash-newline.
8214 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8216 static U_CHAR *
8217 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
8218 register const U_CHAR *bp;
8219 register const U_CHAR *limit;
8220 int start_line;
8221 int *count_newlines;
8222 int *backslash_newlines_p;
8223 int *eofp;
8225 register U_CHAR c, match;
8227 match = *bp++;
8228 while (1) {
8229 if (bp >= limit) {
8230 error_with_line (line_for_error (start_line),
8231 "unterminated string or character constant");
8232 error_with_line (multiline_string_line,
8233 "possible real start of unterminated constant");
8234 multiline_string_line = 0;
8235 if (eofp)
8236 *eofp = 1;
8237 break;
8239 c = *bp++;
8240 if (c == '\\') {
8241 while (*bp == '\\' && bp[1] == '\n') {
8242 if (backslash_newlines_p)
8243 *backslash_newlines_p = 1;
8244 if (count_newlines)
8245 ++*count_newlines;
8246 bp += 2;
8248 if (*bp == '\n') {
8249 if (backslash_newlines_p)
8250 *backslash_newlines_p = 1;
8251 if (count_newlines)
8252 ++*count_newlines;
8254 bp++;
8255 } else if (c == '\n') {
8256 if (traditional) {
8257 /* Unterminated strings and character constants are 'valid'. */
8258 bp--; /* Don't consume the newline. */
8259 if (eofp)
8260 *eofp = 1;
8261 break;
8263 if (match == '\'') {
8264 error_with_line (line_for_error (start_line),
8265 "unterminated character constant");
8266 bp--;
8267 if (eofp)
8268 *eofp = 1;
8269 break;
8271 /* If not traditional, then allow newlines inside strings. */
8272 if (count_newlines) {
8273 if (warn_white_space && is_hor_space[bp[-2]])
8274 warning ("white space at end of line in string");
8275 ++*count_newlines;
8277 if (multiline_string_line == 0) {
8278 if (pedantic)
8279 pedwarn_with_line (line_for_error (start_line),
8280 "string constant runs past end of line");
8281 multiline_string_line = start_line;
8283 } else if (c == match)
8284 break;
8285 #ifdef MULTIBYTE_CHARS
8287 int length;
8288 --bp;
8289 length = local_mblen (bp, limit - bp);
8290 if (length <= 0)
8291 length = 1;
8292 bp += length;
8294 #endif
8296 return (U_CHAR *) bp;
8299 /* Place into DST a quoted string representing the string SRC.
8300 SRCLEN is the length of SRC; SRC may contain null bytes.
8301 Return the address of DST's terminating null. */
8303 static char *
8304 quote_string (dst, src, srclen)
8305 char *dst;
8306 const char *src;
8307 size_t srclen;
8309 U_CHAR c;
8310 const char *srclim = src + srclen;
8312 *dst++ = '\"';
8313 while (src != srclim)
8314 switch ((c = *src++))
8316 default:
8317 if (ISPRINT (c))
8318 *dst++ = c;
8319 else
8321 sprintf (dst, "\\%03o", c);
8322 dst += 4;
8324 break;
8326 case '\"':
8327 case '\\':
8328 *dst++ = '\\';
8329 *dst++ = c;
8330 break;
8333 *dst++ = '\"';
8334 *dst = '\0';
8335 return dst;
8338 /* Skip across a group of balanced parens, starting from IP->bufp.
8339 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8341 This does not handle newlines, because it's used for the arg of #if,
8342 where there aren't any newlines. Also, backslash-newline can't appear. */
8344 static U_CHAR *
8345 skip_paren_group (ip)
8346 register FILE_BUF *ip;
8348 U_CHAR *limit = ip->buf + ip->length;
8349 U_CHAR *p = ip->bufp;
8350 int depth = 0;
8351 int lines_dummy = 0;
8353 while (p != limit) {
8354 int c = *p++;
8355 switch (c) {
8356 case '(':
8357 depth++;
8358 break;
8360 case ')':
8361 depth--;
8362 if (depth == 0)
8363 return ip->bufp = p;
8364 break;
8366 case '/':
8367 if (*p == '*') {
8368 ip->bufp = p;
8369 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
8370 p = ip->bufp;
8373 case '"':
8374 case '\'':
8376 int eofp = 0;
8377 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
8378 if (eofp)
8379 return ip->bufp = p;
8381 break;
8385 ip->bufp = p;
8386 return p;
8389 /* Write out a #line directive, for instance, after an #include file.
8390 If CONDITIONAL is nonzero, we can omit the #line if it would
8391 appear to be a no-op, and we can output a few newlines instead
8392 if we want to increase the line number by a small amount.
8393 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8395 static void
8396 output_line_directive (ip, op, conditional, file_change)
8397 FILE_BUF *ip, *op;
8398 int conditional;
8399 enum file_change_code file_change;
8401 int len;
8402 char *line_directive_buf, *line_end;
8404 if (no_line_directives
8405 || ip->fname == NULL
8406 || no_output) {
8407 op->lineno = ip->lineno;
8408 return;
8411 if (conditional) {
8412 if (ip->lineno == op->lineno)
8413 return;
8415 /* If the inherited line number is a little too small,
8416 output some newlines instead of a #line directive. */
8417 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
8418 check_expand (op, 10);
8419 while (ip->lineno > op->lineno) {
8420 *op->bufp++ = '\n';
8421 op->lineno++;
8423 return;
8427 /* Output a positive line number if possible. */
8428 while (ip->lineno <= 0 && ip->bufp - ip->buf < ip->length
8429 && *ip->bufp == '\n') {
8430 ip->lineno++;
8431 ip->bufp++;
8434 line_directive_buf = (char *) alloca (4 * ip->nominal_fname_len + 100);
8435 sprintf (line_directive_buf, "# %d ", ip->lineno);
8436 line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
8437 ip->nominal_fname, ip->nominal_fname_len);
8438 if (file_change != same_file) {
8439 *line_end++ = ' ';
8440 *line_end++ = file_change == enter_file ? '1' : '2';
8442 /* Tell cc1 if following text comes from a system header file. */
8443 if (ip->system_header_p) {
8444 *line_end++ = ' ';
8445 *line_end++ = '3';
8447 #ifndef NO_IMPLICIT_EXTERN_C
8448 /* Tell cc1plus if following text should be treated as C. */
8449 if (ip->system_header_p == 2 && cplusplus) {
8450 *line_end++ = ' ';
8451 *line_end++ = '4';
8453 #endif
8454 *line_end++ = '\n';
8455 len = line_end - line_directive_buf;
8456 check_expand (op, len + 1);
8457 if (op->bufp > op->buf && op->bufp[-1] != '\n')
8458 *op->bufp++ = '\n';
8459 bcopy ((const PTR) line_directive_buf, (PTR) op->bufp, len);
8460 op->bufp += len;
8461 op->lineno = ip->lineno;
8464 /* This structure represents one parsed argument in a macro call.
8465 `raw' points to the argument text as written (`raw_length' is its length).
8466 `expanded' points to the argument's macro-expansion
8467 (its length is `expand_length', and its allocated size is `expand_size').
8468 `stringified_length_bound' is an upper bound on the length
8469 the argument would have if stringified.
8470 `use_count' is the number of times this macro arg is substituted
8471 into the macro. If the actual use count exceeds 10,
8472 the value stored is 10.
8473 `free1' and `free2', if nonzero, point to blocks to be freed
8474 when the macro argument data is no longer needed.
8475 `free_ptr', if nonzero, points to a value of instack[i].free_ptr
8476 where the raw field points somewhere into this string. The purpose
8477 of this is to hold onto instack[i].buf for macro arguments, even
8478 when the element has been popped off the input stack.
8481 struct argdata {
8482 U_CHAR *raw, *expanded;
8483 int raw_length, expand_length, expand_size;
8484 int stringified_length_bound;
8485 U_CHAR *free1, *free2;
8486 U_CHAR *free_ptr;
8487 int newlines;
8488 char use_count;
8491 /* Expand a macro call.
8492 HP points to the symbol that is the macro being called.
8493 Put the result of expansion onto the input stack
8494 so that subsequent input by our caller will use it.
8496 If macro wants arguments, caller has already verified that
8497 an argument list follows; arguments come from the input stack. */
8499 static void
8500 macroexpand (hp, op)
8501 HASHNODE *hp;
8502 FILE_BUF *op;
8504 int nargs;
8505 DEFINITION *defn = hp->value.defn;
8506 register U_CHAR *xbuf;
8507 int xbuf_len;
8508 int start_line = instack[indepth].lineno;
8509 int rest_args, rest_zero;
8511 CHECK_DEPTH (return;);
8513 /* it might not actually be a macro. */
8514 if (hp->type != T_MACRO) {
8515 special_symbol (hp, op);
8516 return;
8519 /* This macro is being used inside a #if, which means it must be */
8520 /* recorded as a precondition. */
8521 if (pcp_inside_if && pcp_outfile && defn->predefined)
8522 dump_single_macro (hp, pcp_outfile);
8524 nargs = defn->nargs;
8526 if (nargs >= 0) {
8527 register int i;
8528 struct argdata *args;
8529 int parse_error = 0;
8531 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
8533 for (i = 0; i < nargs; i++) {
8534 args[i].raw = (U_CHAR *) "";
8535 args[i].expanded = 0;
8536 args[i].raw_length = args[i].expand_length = args[i].expand_size
8537 = args[i].stringified_length_bound = 0;
8538 args[i].free1 = args[i].free2 = 0;
8539 args[i].free_ptr = 0;
8540 args[i].use_count = 0;
8543 /* Parse all the macro args that are supplied. I counts them.
8544 The first NARGS args are stored in ARGS.
8545 The rest are discarded.
8546 If rest_args is set then we assume macarg absorbed the rest of the args.
8548 i = 0;
8549 rest_args = 0;
8550 do {
8551 /* Discard the open-parenthesis or comma before the next arg. */
8552 ++instack[indepth].bufp;
8553 if (rest_args)
8554 continue;
8555 if (i < nargs || (nargs == 0 && i == 0)) {
8556 /* If we are working on last arg which absorbs rest of args... */
8557 if (i == nargs - 1 && defn->rest_args)
8558 rest_args = 1;
8559 parse_error = macarg (&args[i], rest_args);
8561 else
8562 parse_error = macarg (NULL_PTR, 0);
8563 if (parse_error) {
8564 error_with_line (line_for_error (start_line),
8565 "unterminated macro call");
8566 break;
8568 i++;
8569 } while (*instack[indepth].bufp != ')');
8571 /* If we got one arg but it was just whitespace, call that 0 args. */
8572 if (i == 1) {
8573 register const U_CHAR *bp = args[0].raw;
8574 register const U_CHAR *lim = bp + args[0].raw_length;
8575 /* cpp.texi says for foo ( ) we provide one argument.
8576 However, if foo wants just 0 arguments, treat this as 0. */
8577 if (nargs == 0)
8578 while (bp != lim && is_space[*bp]) bp++;
8579 if (bp == lim)
8580 i = 0;
8583 /* Don't output an error message if we have already output one for
8584 a parse error above. */
8585 rest_zero = 0;
8586 if (nargs == 0 && i > 0) {
8587 if (! parse_error)
8588 error ("arguments given to macro `%s'", hp->name);
8589 } else if (i < nargs) {
8590 /* traditional C allows foo() if foo wants one argument. */
8591 if (nargs == 1 && i == 0 && traditional)
8593 /* the rest args token is allowed to absorb 0 tokens */
8594 else if (i == nargs - 1 && defn->rest_args)
8595 rest_zero = 1;
8596 else if (parse_error)
8598 else if (i == 0)
8599 error ("macro `%s' used without args", hp->name);
8600 else if (i == 1)
8601 error ("macro `%s' used with just one arg", hp->name);
8602 else
8603 error ("macro `%s' used with only %d args", hp->name, i);
8604 } else if (i > nargs) {
8605 if (! parse_error)
8606 error ("macro `%s' used with too many (%d) args", hp->name, i);
8609 /* Swallow the closeparen. */
8610 ++instack[indepth].bufp;
8612 /* If macro wants zero args, we parsed the arglist for checking only.
8613 Read directly from the macro definition. */
8614 if (nargs == 0) {
8615 xbuf = defn->expansion;
8616 xbuf_len = defn->length;
8617 } else {
8618 register U_CHAR *exp = defn->expansion;
8619 register int offset; /* offset in expansion,
8620 copied a piece at a time */
8621 register int totlen; /* total amount of exp buffer filled so far */
8623 register struct reflist *ap, *last_ap;
8625 /* Macro really takes args. Compute the expansion of this call. */
8627 /* Compute length in characters of the macro's expansion.
8628 Also count number of times each arg is used. */
8629 xbuf_len = defn->length;
8630 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
8631 if (ap->stringify && args[ap->argno].stringified_length_bound == 0)
8632 /* macarg is not called for omitted arguments, as a result
8633 stringified_length_bound will be zero. We need to make
8634 enough space for "". */
8635 xbuf_len += 2;
8636 else if (ap->stringify)
8637 xbuf_len += args[ap->argno].stringified_length_bound;
8638 else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
8639 /* Add 4 for two newline-space markers to prevent
8640 token concatenation. */
8641 xbuf_len += args[ap->argno].raw_length + 4;
8642 else {
8643 /* We have an ordinary (expanded) occurrence of the arg.
8644 So compute its expansion, if we have not already. */
8645 if (args[ap->argno].expanded == 0) {
8646 FILE_BUF obuf;
8647 obuf = expand_to_temp_buffer (args[ap->argno].raw,
8648 args[ap->argno].raw + args[ap->argno].raw_length,
8649 1, 0);
8651 args[ap->argno].expanded = obuf.buf;
8652 args[ap->argno].expand_length = obuf.bufp - obuf.buf;
8653 args[ap->argno].expand_size = obuf.length;
8654 args[ap->argno].free2 = obuf.buf;
8656 xbuf_len += args[ap->argno].expand_length;
8657 } else {
8658 /* If the arg appears more than once, its later occurrences
8659 may have newline turned into backslash-'n', which is a
8660 factor of 2 expansion. */
8661 xbuf_len += 2 * args[ap->argno].expand_length;
8663 /* Add 4 for two newline-space markers to prevent
8664 token concatenation. */
8665 xbuf_len += 4;
8667 if (args[ap->argno].use_count < 10)
8668 args[ap->argno].use_count++;
8671 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
8673 /* Generate in XBUF the complete expansion
8674 with arguments substituted in.
8675 TOTLEN is the total size generated so far.
8676 OFFSET is the index in the definition
8677 of where we are copying from. */
8678 offset = totlen = 0;
8679 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
8680 last_ap = ap, ap = ap->next) {
8681 register struct argdata *arg = &args[ap->argno];
8682 int count_before = totlen;
8684 /* Add chars to XBUF. */
8685 for (i = 0; i < ap->nchars; i++, offset++)
8686 xbuf[totlen++] = exp[offset];
8688 /* If followed by an empty rest arg with concatenation,
8689 delete the last run of nonwhite chars. */
8690 if (rest_zero && totlen > count_before
8691 && ((ap->rest_args && ap->raw_before != 0)
8692 || (last_ap != NULL && last_ap->rest_args
8693 && last_ap->raw_after != 0))) {
8694 /* Delete final whitespace. */
8695 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
8696 totlen--;
8699 /* Delete the nonwhites before them. */
8700 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
8701 totlen--;
8705 if (ap->stringify != 0) {
8706 int arglen = arg->raw_length;
8707 int escaped = 0;
8708 int in_string = 0;
8709 int c;
8710 i = 0;
8711 while (i < arglen
8712 && (c = arg->raw[i], is_space[c]))
8713 i++;
8714 while (i < arglen
8715 && (c = arg->raw[arglen - 1], is_space[c]))
8716 arglen--;
8717 if (!traditional)
8718 xbuf[totlen++] = '\"'; /* insert beginning quote */
8719 for (; i < arglen; i++) {
8720 c = arg->raw[i];
8722 if (in_string) {
8723 /* Generate nothing for backslash-newline in a string. */
8724 if (c == '\\' && arg->raw[i + 1] == '\n') {
8725 i++;
8726 continue;
8728 } else {
8729 /* Special markers
8730 generate nothing for a stringified argument. */
8731 if (c == '\n') {
8732 i++;
8733 continue;
8736 /* Internal sequences of whitespace are replaced by one space
8737 except within a string or char token. */
8738 if (is_space[c]) {
8739 i++;
8740 while (is_space[(c = arg->raw[i])])
8741 /* Newline markers can occur within a whitespace sequence;
8742 consider them part of the sequence. */
8743 i += (c == '\n') + 1;
8744 i--;
8745 c = ' ';
8749 if (escaped)
8750 escaped = 0;
8751 else {
8752 if (c == '\\')
8753 escaped = 1;
8754 else if (in_string) {
8755 if (c == in_string)
8756 in_string = 0;
8757 else
8759 #ifdef MULTIBYTE_CHARS
8760 int length;
8761 length = local_mblen (arg->raw + i, arglen - i);
8762 if (length > 1)
8764 bcopy (arg->raw + i, xbuf + totlen, length);
8765 i += length - 1;
8766 totlen += length;
8767 continue;
8769 #endif
8771 } else if (c == '\"' || c == '\'')
8772 in_string = c;
8775 /* Escape double-quote, and backslashes in strings.
8776 Newlines in strings are best escaped as \n, since
8777 otherwise backslash-backslash-newline-newline is
8778 mishandled. The C Standard doesn't allow newlines in
8779 strings, so we can escape newlines as we please. */
8780 if (c == '\"'
8781 || (in_string
8782 && (c == '\\'
8783 || (c == '\n' ? (c = 'n', 1) : 0))))
8784 xbuf[totlen++] = '\\';
8785 /* We used to output e.g. \008 for control characters here,
8786 but this doesn't conform to the C Standard.
8787 Just output the characters as-is. */
8788 xbuf[totlen++] = c;
8790 if (!traditional)
8791 xbuf[totlen++] = '\"'; /* insert ending quote */
8792 } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
8793 const U_CHAR *p1 = arg->raw;
8794 const U_CHAR *l1 = p1 + arg->raw_length;
8795 if (ap->raw_before != 0) {
8796 while (p1 != l1 && is_space[*p1]) p1++;
8797 while (p1 != l1 && is_idchar[*p1])
8798 xbuf[totlen++] = *p1++;
8799 /* Delete any no-reexpansion marker that follows
8800 an identifier at the beginning of the argument
8801 if the argument is concatenated with what precedes it. */
8802 if (p1[0] == '\n' && p1[1] == '-')
8803 p1 += 2;
8804 } else if (!traditional) {
8805 /* Ordinary expanded use of the argument.
8806 Put in newline-space markers to prevent token pasting. */
8807 xbuf[totlen++] = '\n';
8808 xbuf[totlen++] = ' ';
8810 if (ap->raw_after != 0) {
8811 /* Arg is concatenated after: delete trailing whitespace,
8812 whitespace markers, and no-reexpansion markers. */
8813 while (p1 != l1) {
8814 if (is_space[l1[-1]]) l1--;
8815 else if (l1[-1] == '-') {
8816 const U_CHAR *p2 = l1 - 1;
8817 /* If a `-' is preceded by an odd number of newlines then it
8818 and the last newline are a no-reexpansion marker. */
8819 while (p2 != p1 && p2[-1] == '\n') p2--;
8820 if ((l1 - 1 - p2) & 1) {
8821 l1 -= 2;
8823 else break;
8825 else break;
8829 bcopy ((const PTR) p1, (PTR) (xbuf + totlen), l1 - p1);
8830 totlen += l1 - p1;
8831 if (!traditional && ap->raw_after == 0) {
8832 /* Ordinary expanded use of the argument.
8833 Put in newline-space markers to prevent token pasting. */
8834 xbuf[totlen++] = '\n';
8835 xbuf[totlen++] = ' ';
8837 } else {
8838 /* Ordinary expanded use of the argument.
8839 Put in newline-space markers to prevent token pasting. */
8840 if (!traditional) {
8841 xbuf[totlen++] = '\n';
8842 xbuf[totlen++] = ' ';
8844 bcopy ((const PTR) arg->expanded, (PTR) (xbuf + totlen),
8845 arg->expand_length);
8846 totlen += arg->expand_length;
8847 if (!traditional) {
8848 xbuf[totlen++] = '\n';
8849 xbuf[totlen++] = ' ';
8851 /* If a macro argument with newlines is used multiple times,
8852 then only expand the newlines once. This avoids creating output
8853 lines which don't correspond to any input line, which confuses
8854 gdb and gcov. */
8855 if (arg->use_count > 1 && arg->newlines > 0) {
8856 /* Don't bother doing change_newlines for subsequent
8857 uses of arg. */
8858 arg->use_count = 1;
8859 change_newlines (arg);
8863 if (totlen > xbuf_len)
8864 abort ();
8867 /* If there is anything left of the definition after handling
8868 the arg list, copy that in too. */
8870 for (i = offset; i < defn->length; i++) {
8871 /* if we've reached the end of the macro */
8872 if (exp[i] == ')')
8873 rest_zero = 0;
8874 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
8875 && last_ap->raw_after != 0))
8876 xbuf[totlen++] = exp[i];
8879 xbuf[totlen] = 0;
8880 xbuf_len = totlen;
8882 for (i = 0; i < nargs; i++) {
8883 if (args[i].free_ptr != 0) {
8884 U_CHAR *buf = args[i].free_ptr;
8885 int d;
8886 for (d = indepth; d >= 0; --d) {
8887 if (instack[d].buf == buf) {
8888 instack[d].free_ptr = buf; /* Give ownership back to instack */
8889 goto no_free;
8892 free (buf); /* buf is not on the stack; must have been popped */
8893 no_free:;
8895 if (args[i].free1 != 0)
8896 free (args[i].free1);
8897 if (args[i].free2 != 0)
8898 free (args[i].free2);
8901 } else {
8902 xbuf = defn->expansion;
8903 xbuf_len = defn->length;
8906 /* Now put the expansion on the input stack
8907 so our caller will commence reading from it. */
8909 register FILE_BUF *ip2;
8911 ip2 = &instack[++indepth];
8913 ip2->fname = 0;
8914 ip2->nominal_fname = 0;
8915 ip2->nominal_fname_len = 0;
8916 ip2->inc = 0;
8917 /* This may not be exactly correct, but will give much better error
8918 messages for nested macro calls than using a line number of zero. */
8919 ip2->lineno = start_line;
8920 ip2->buf = xbuf;
8921 ip2->length = xbuf_len;
8922 ip2->bufp = xbuf;
8923 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
8924 ip2->macro = hp;
8925 ip2->if_stack = if_stack;
8926 ip2->system_header_p = 0;
8928 /* Recursive macro use sometimes works traditionally.
8929 #define foo(x,y) bar (x (y,0), y)
8930 foo (foo, baz) */
8932 if (!traditional)
8933 hp->type = T_DISABLED;
8937 /* Parse a macro argument and store the info on it into *ARGPTR.
8938 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8939 Return nonzero to indicate a syntax error. */
8941 static int
8942 macarg (argptr, rest_args)
8943 register struct argdata *argptr;
8944 int rest_args;
8946 FILE_BUF *ip = &instack[indepth];
8947 int paren = 0;
8948 int lineno0 = ip->lineno;
8949 int comments = 0;
8950 int result = 0;
8952 /* Try to parse as much of the argument as exists at this
8953 input stack level. */
8954 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro,
8955 &paren, &ip->lineno, &comments, rest_args);
8957 /* If we find the end of the argument at this level,
8958 set up *ARGPTR to point at it in the input stack. */
8959 if (!(ip->fname != 0 && (ip->lineno != lineno0 || comments != 0))
8960 && bp != ip->buf + ip->length) {
8961 if (argptr != 0) {
8962 argptr->raw = ip->bufp;
8963 argptr->raw_length = bp - ip->bufp;
8964 argptr->newlines = ip->lineno - lineno0;
8965 /* The next two statements transfer ownership of the the buffer
8966 from ip to argptr. Note that the second statement ensures that
8967 a given free_ptr is owned by at most one macro argument. */
8968 argptr->free_ptr = ip->free_ptr;
8969 ip->free_ptr = 0;
8971 ip->bufp = bp;
8972 } else {
8973 /* This input stack level ends before the macro argument does.
8974 We must pop levels and keep parsing.
8975 Therefore, we must allocate a temporary buffer and copy
8976 the macro argument into it. */
8977 int bufsize = bp - ip->bufp;
8978 int extra = ip->lineno - lineno0;
8979 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
8980 int final_start = 0;
8982 bcopy ((const PTR) ip->bufp, (PTR) buffer, bufsize);
8983 ip->bufp = bp;
8985 while (bp == ip->buf + ip->length) {
8986 if (instack[indepth].macro == 0) {
8987 result = 1;
8988 break;
8990 ip->macro->type = T_MACRO;
8991 if (ip->free_ptr)
8992 free (ip->free_ptr);
8993 ip = &instack[--indepth];
8994 lineno0 = ip->lineno;
8995 comments = 0;
8996 bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro, &paren,
8997 &ip->lineno, &comments, rest_args);
8998 final_start = bufsize;
8999 bufsize += bp - ip->bufp;
9000 extra += ip->lineno - lineno0;
9001 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
9002 bcopy ((const PTR) ip->bufp, (PTR) (buffer + bufsize - (bp - ip->bufp)),
9003 bp - ip->bufp);
9004 ip->bufp = bp;
9007 /* Now, if arg is actually wanted, record its raw form,
9008 discarding comments and duplicating newlines in whatever
9009 part of it did not come from a macro expansion.
9010 EXTRA space has been preallocated for duplicating the newlines.
9011 FINAL_START is the index of the start of that part. */
9012 if (argptr != 0) {
9013 argptr->raw = buffer;
9014 argptr->raw_length = bufsize;
9015 argptr->free1 = buffer;
9016 argptr->newlines = ip->lineno - lineno0;
9017 if ((argptr->newlines || comments) && ip->fname != 0)
9018 argptr->raw_length
9019 = final_start +
9020 discard_comments (argptr->raw + final_start,
9021 argptr->raw_length - final_start,
9022 argptr->newlines);
9023 argptr->raw[argptr->raw_length] = 0;
9024 if (argptr->raw_length > bufsize + extra)
9025 abort ();
9029 /* If we are not discarding this argument,
9030 macroexpand it and compute its length as stringified.
9031 All this info goes into *ARGPTR. */
9033 if (argptr != 0) {
9034 register const U_CHAR *buf, *lim;
9035 register int totlen;
9037 buf = argptr->raw;
9038 lim = buf + argptr->raw_length;
9040 while (buf != lim && is_space[*buf])
9041 buf++;
9042 while (buf != lim && is_space[lim[-1]])
9043 lim--;
9044 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
9045 while (buf != lim) {
9046 register U_CHAR c = *buf++;
9047 totlen++;
9048 /* Internal sequences of whitespace are replaced by one space
9049 in most cases, but not always. So count all the whitespace
9050 in case we need to keep it all. */
9051 #if 0
9052 if (is_space[c])
9053 SKIP_ALL_WHITE_SPACE (buf);
9054 else
9055 #endif
9056 if (c == '\"' || c == '\\' || c == '\n') /* escape these chars */
9057 totlen++;
9059 argptr->stringified_length_bound = totlen;
9061 return result;
9064 /* Scan text from START (inclusive) up to LIMIT (exclusive),
9065 taken from the expansion of MACRO,
9066 counting parens in *DEPTHPTR,
9067 and return if reach LIMIT
9068 or before a `)' that would make *DEPTHPTR negative
9069 or before a comma when *DEPTHPTR is zero.
9070 Single and double quotes are matched and termination
9071 is inhibited within them. Comments also inhibit it.
9072 Value returned is pointer to stopping place.
9074 Increment *NEWLINES each time a newline is passed.
9075 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
9076 Set *COMMENTS to 1 if a comment is seen. */
9078 static U_CHAR *
9079 macarg1 (start, limit, macro, depthptr, newlines, comments, rest_args)
9080 U_CHAR *start;
9081 register const U_CHAR *limit;
9082 struct hashnode *macro;
9083 int *depthptr, *newlines, *comments;
9084 int rest_args;
9086 register U_CHAR *bp = start;
9088 while (bp < limit) {
9089 switch (*bp) {
9090 case '(':
9091 (*depthptr)++;
9092 break;
9093 case ')':
9094 if (--(*depthptr) < 0)
9095 return bp;
9096 break;
9097 case '\\':
9098 /* Traditionally, backslash makes following char not special. */
9099 if (traditional && bp + 1 < limit && bp[1] != '\n')
9100 bp++;
9101 break;
9102 case '\n':
9103 ++*newlines;
9104 break;
9105 case '/':
9106 if (macro)
9107 break;
9108 if (bp[1] == '\\')
9109 newline_fix (bp + 1);
9110 if (bp[1] == '*') {
9111 *comments = 1;
9112 for (bp += 2; bp < limit; bp++) {
9113 if (*bp == '\n')
9114 ++*newlines;
9115 else if (*bp == '*') {
9116 if (bp[-1] == '/' && warn_comments)
9117 warning ("`/*' within comment");
9118 if (bp[1] == '\\')
9119 newline_fix (bp + 1);
9120 if (bp[1] == '/') {
9121 bp++;
9122 break;
9125 else
9127 #ifdef MULTIBYTE_CHARS
9128 int length;
9129 length = local_mblen (bp, limit - bp);
9130 if (length > 1)
9131 bp += (length - 1);
9132 #endif
9135 } else if (bp[1] == '/' && cplusplus_comments) {
9136 *comments = 1;
9137 for (bp += 2; bp < limit; bp++) {
9138 if (*bp == '\n') {
9139 ++*newlines;
9140 break;
9142 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
9144 ++*newlines;
9145 if (warn_comments)
9146 warning ("multiline `//' comment");
9147 ++bp;
9149 else
9151 #ifdef MULTIBYTE_CHARS
9152 int length;
9153 length = local_mblen (bp, limit - bp);
9154 if (length > 1)
9155 bp += (length - 1);
9156 #endif
9160 break;
9161 case '\'':
9162 case '\"':
9164 int quotec;
9165 for (quotec = *bp++; bp < limit && *bp != quotec; bp++) {
9166 if (*bp == '\\') {
9167 bp++;
9168 if (*bp == '\n')
9169 ++*newlines;
9170 while (*bp == '\\' && bp[1] == '\n') {
9171 bp += 2;
9172 ++*newlines;
9174 } else if (*bp == '\n') {
9175 if (warn_white_space && is_hor_space[bp[-1]] && ! macro)
9176 warning ("white space at end of line in string");
9177 ++*newlines;
9178 if (quotec == '\'')
9179 break;
9181 else
9183 #ifdef MULTIBYTE_CHARS
9184 int length;
9185 length = local_mblen (bp, limit - bp);
9186 if (length > 1)
9187 bp += (length - 1);
9188 #endif
9192 break;
9193 case ',':
9194 /* if we've returned to lowest level and we aren't absorbing all args */
9195 if ((*depthptr) == 0 && rest_args == 0)
9196 return bp;
9197 break;
9199 bp++;
9202 return bp;
9205 /* Discard comments and duplicate newlines
9206 in the string of length LENGTH at START,
9207 except inside of string constants.
9208 The string is copied into itself with its beginning staying fixed.
9210 NEWLINES is the number of newlines that must be duplicated.
9211 We assume that that much extra space is available past the end
9212 of the string. */
9214 static int
9215 discard_comments (start, length, newlines)
9216 U_CHAR *start;
9217 int length;
9218 int newlines;
9220 register U_CHAR *ibp;
9221 register U_CHAR *obp;
9222 register U_CHAR *limit;
9223 register int c;
9225 /* If we have newlines to duplicate, copy everything
9226 that many characters up. Then, in the second part,
9227 we will have room to insert the newlines
9228 while copying down.
9229 NEWLINES may actually be too large, because it counts
9230 newlines in string constants, and we don't duplicate those.
9231 But that does no harm. */
9232 if (newlines > 0) {
9233 ibp = start + length;
9234 obp = ibp + newlines;
9235 limit = start;
9236 while (limit != ibp)
9237 *--obp = *--ibp;
9240 ibp = start + newlines;
9241 limit = start + length + newlines;
9242 obp = start;
9244 while (ibp < limit) {
9245 *obp++ = c = *ibp++;
9246 switch (c) {
9247 case '\n':
9248 /* Duplicate the newline. */
9249 *obp++ = '\n';
9250 break;
9252 case '\\':
9253 if (*ibp == '\n') {
9254 obp--;
9255 ibp++;
9257 break;
9259 case '/':
9260 if (*ibp == '\\')
9261 newline_fix (ibp);
9262 /* Delete any comment. */
9263 if (cplusplus_comments && ibp[0] == '/') {
9264 /* Comments are equivalent to spaces. */
9265 obp[-1] = ' ';
9266 ibp++;
9267 while (ibp < limit)
9269 if (*ibp == '\n')
9270 break;
9271 if (*ibp == '\\' && ibp + 1 < limit && ibp[1] == '\n')
9272 ibp++;
9273 else
9275 #ifdef MULTIBYTE_CHARS
9276 int length = local_mblen (ibp, limit - ibp);
9277 if (length > 1)
9278 ibp += (length - 1);
9279 #endif
9281 ibp++;
9283 break;
9285 if (ibp[0] != '*' || ibp + 1 >= limit)
9286 break;
9287 /* Comments are equivalent to spaces.
9288 For -traditional, a comment is equivalent to nothing. */
9289 if (traditional)
9290 obp--;
9291 else
9292 obp[-1] = ' ';
9293 while (++ibp < limit) {
9294 if (ibp[0] == '*') {
9295 if (ibp[1] == '\\')
9296 newline_fix (ibp + 1);
9297 if (ibp[1] == '/') {
9298 ibp += 2;
9299 break;
9302 else
9304 #ifdef MULTIBYTE_CHARS
9305 int length = local_mblen (ibp, limit - ibp);
9306 if (length > 1)
9307 ibp += (length - 1);
9308 #endif
9311 break;
9313 case '\'':
9314 case '\"':
9315 /* Notice and skip strings, so that we don't
9316 think that comments start inside them,
9317 and so we don't duplicate newlines in them. */
9319 int quotec = c;
9320 while (ibp < limit) {
9321 *obp++ = c = *ibp++;
9322 if (c == quotec)
9323 break;
9324 if (c == '\n')
9326 if (quotec == '\'')
9327 break;
9329 else if (c == '\\') {
9330 if (ibp < limit && *ibp == '\n') {
9331 ibp++;
9332 obp--;
9333 } else {
9334 while (*ibp == '\\' && ibp[1] == '\n')
9335 ibp += 2;
9336 if (ibp < limit)
9337 *obp++ = *ibp++;
9340 else
9342 #ifdef MULTIBYTE_CHARS
9343 int length;
9344 ibp--;
9345 length = local_mblen (ibp, limit - ibp);
9346 if (length > 1)
9348 obp--;
9349 bcopy (ibp, obp, length);
9350 ibp += length;
9351 obp += length;
9353 else
9354 ibp++;
9355 #endif
9359 break;
9363 return obp - start;
9366 /* Turn newlines to spaces in the macro argument ARG.
9367 Remove backslash-newline from string constants,
9368 and turn other newlines in string constants to backslash-'n'. */
9370 static void
9371 change_newlines (arg)
9372 struct argdata *arg;
9374 U_CHAR *start = arg->expanded;
9375 int length = arg->expand_length;
9376 register U_CHAR *ibp;
9377 register U_CHAR *obp;
9378 register const U_CHAR *limit;
9379 register int c;
9381 ibp = start;
9382 limit = start + length;
9383 obp = start;
9385 while (ibp < limit) {
9386 *obp++ = c = *ibp++;
9387 switch (c) {
9388 case '\n':
9389 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9390 string. Skip past the newline and its duplicate.
9391 Put a space in the output. */
9392 if (*ibp == '\n')
9394 ibp++;
9395 obp--;
9396 *obp++ = ' ';
9398 break;
9400 case '\'':
9401 case '\"':
9402 /* Notice and skip strings, so that we don't delete newlines in them. */
9404 int quotec = c;
9405 while (ibp < limit) {
9406 *obp++ = c = *ibp++;
9407 if (c == quotec)
9408 break;
9409 else if (c == '\\' && ibp < limit && *ibp == '\n')
9410 *obp++ = *ibp++;
9411 else if (c == '\n')
9413 if (quotec == '\'')
9414 break;
9416 else
9418 #ifdef MULTIBYTE_CHARS
9419 int length;
9420 ibp--;
9421 length = local_mblen (ibp, limit - ibp);
9422 if (length > 1)
9424 obp--;
9425 bcopy (ibp, obp, length);
9426 ibp += length;
9427 obp += length;
9429 else
9430 ibp++;
9431 #endif
9435 break;
9439 arg->expand_length = obp - arg->expanded;
9441 if (start != arg->expanded)
9442 free (start);
9445 /* notice - output message to stderr */
9447 static void
9448 notice VPARAMS ((const char * msgid, ...))
9450 #ifndef ANSI_PROTOTYPES
9451 const char * msgid;
9452 #endif
9453 va_list args;
9455 VA_START (args, msgid);
9457 #ifndef ANSI_PROTOTYPES
9458 msgid = va_arg (args, const char *);
9459 #endif
9461 vnotice (msgid, args);
9462 va_end (args);
9465 static void
9466 vnotice (msgid, args)
9467 const char *msgid;
9468 va_list args;
9470 vfprintf (stderr, _(msgid), args);
9473 /* error - print error message and increment count of errors. */
9475 void
9476 error VPARAMS ((const char * msgid, ...))
9478 #ifndef ANSI_PROTOTYPES
9479 const char * msgid;
9480 #endif
9481 va_list args;
9483 VA_START (args, msgid);
9485 #ifndef ANSI_PROTOTYPES
9486 msgid = va_arg (args, const char *);
9487 #endif
9489 verror (msgid, args);
9490 va_end (args);
9493 void
9494 verror (msgid, args)
9495 const char *msgid;
9496 va_list args;
9498 int i;
9499 FILE_BUF *ip = NULL;
9501 print_containing_files ();
9503 for (i = indepth; i >= 0; i--)
9504 if (instack[i].fname != NULL) {
9505 ip = &instack[i];
9506 break;
9509 if (ip != NULL) {
9510 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9511 ip->nominal_fname_len, stderr);
9512 fprintf (stderr, ":%d: ", ip->lineno);
9514 vnotice (msgid, args);
9515 fprintf (stderr, "\n");
9516 errors++;
9519 /* Error including a message from `errno'. */
9521 static void
9522 error_from_errno (name)
9523 const char *name;
9525 int e = errno;
9526 int i;
9527 FILE_BUF *ip = NULL;
9529 print_containing_files ();
9531 for (i = indepth; i >= 0; i--)
9532 if (instack[i].fname != NULL) {
9533 ip = &instack[i];
9534 break;
9537 if (ip != NULL) {
9538 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9539 ip->nominal_fname_len, stderr);
9540 fprintf (stderr, ":%d: ", ip->lineno);
9543 fprintf (stderr, "%s: %s\n", name, xstrerror (e));
9545 errors++;
9548 /* Print error message but don't count it. */
9550 void
9551 warning VPARAMS ((const char * msgid, ...))
9553 #ifndef ANSI_PROTOTYPES
9554 const char * msgid;
9555 #endif
9556 va_list args;
9558 VA_START (args, msgid);
9560 #ifndef ANSI_PROTOTYPES
9561 msgid = va_arg (args, const char *);
9562 #endif
9564 vwarning (msgid, args);
9565 va_end (args);
9568 static void
9569 vwarning (msgid, args)
9570 const char *msgid;
9571 va_list args;
9573 int i;
9574 FILE_BUF *ip = NULL;
9576 if (inhibit_warnings)
9577 return;
9579 if (warnings_are_errors)
9580 errors++;
9582 print_containing_files ();
9584 for (i = indepth; i >= 0; i--)
9585 if (instack[i].fname != NULL) {
9586 ip = &instack[i];
9587 break;
9590 if (ip != NULL) {
9591 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9592 ip->nominal_fname_len, stderr);
9593 fprintf (stderr, ":%d: ", ip->lineno);
9595 notice ("warning: ");
9596 vnotice (msgid, args);
9597 fprintf (stderr, "\n");
9600 static void
9601 error_with_line VPARAMS ((int line, const char * msgid, ...))
9603 #ifndef ANSI_PROTOTYPES
9604 int line;
9605 const char * msgid;
9606 #endif
9607 va_list args;
9609 VA_START (args, msgid);
9611 #ifndef ANSI_PROTOTYPES
9612 line = va_arg (args, int);
9613 msgid = va_arg (args, const char *);
9614 #endif
9616 verror_with_line (line, msgid, args);
9617 va_end (args);
9621 static void
9622 verror_with_line (line, msgid, args)
9623 int line;
9624 const char *msgid;
9625 va_list args;
9627 int i;
9628 FILE_BUF *ip = NULL;
9630 print_containing_files ();
9632 for (i = indepth; i >= 0; i--)
9633 if (instack[i].fname != NULL) {
9634 ip = &instack[i];
9635 break;
9638 if (ip != NULL) {
9639 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9640 ip->nominal_fname_len, stderr);
9641 fprintf (stderr, ":%d: ", line);
9643 vnotice (msgid, args);
9644 fprintf (stderr, "\n");
9645 errors++;
9648 static void
9649 warning_with_line VPARAMS ((int line, const char * msgid, ...))
9651 #ifndef ANSI_PROTOTYPES
9652 int line;
9653 const char * msgid;
9654 #endif
9655 va_list args;
9657 VA_START (args, msgid);
9659 #ifndef ANSI_PROTOTYPES
9660 line = va_arg (args, int);
9661 msgid = va_arg (args, const char *);
9662 #endif
9664 vwarning_with_line (line, msgid, args);
9665 va_end (args);
9668 static void
9669 vwarning_with_line (line, msgid, args)
9670 int line;
9671 const char *msgid;
9672 va_list args;
9674 int i;
9675 FILE_BUF *ip = NULL;
9677 if (inhibit_warnings)
9678 return;
9680 if (warnings_are_errors)
9681 errors++;
9683 print_containing_files ();
9685 for (i = indepth; i >= 0; i--)
9686 if (instack[i].fname != NULL) {
9687 ip = &instack[i];
9688 break;
9691 if (ip != NULL) {
9692 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9693 ip->nominal_fname_len, stderr);
9694 if (line)
9695 fprintf (stderr, ":%d: ", line);
9696 else
9697 fputs (": ", stderr);
9699 notice ("warning: ");
9700 vnotice (msgid, args);
9701 fprintf (stderr, "\n");
9704 /* Print an error message and maybe count it. */
9706 void
9707 pedwarn VPARAMS ((const char * msgid, ...))
9709 #ifndef ANSI_PROTOTYPES
9710 const char * msgid;
9711 #endif
9712 va_list args;
9714 VA_START (args, msgid);
9716 #ifndef ANSI_PROTOTYPES
9717 msgid = va_arg (args, const char *);
9718 #endif
9720 if (pedantic_errors)
9721 verror (msgid, args);
9722 else
9723 vwarning (msgid, args);
9724 va_end (args);
9727 void
9728 pedwarn_with_line VPARAMS ((int line, const char * msgid, ...))
9730 #ifndef ANSI_PROTOTYPES
9731 int line;
9732 const char * msgid;
9733 #endif
9734 va_list args;
9736 VA_START (args, msgid);
9738 #ifndef ANSI_PROTOTYPES
9739 line = va_arg (args, int);
9740 msgid = va_arg (args, const char *);
9741 #endif
9743 if (pedantic_errors)
9744 verror_with_line (line, msgid, args);
9745 else
9746 vwarning_with_line (line, msgid, args);
9747 va_end (args);
9750 /* Report a warning (or an error if pedantic_errors)
9751 giving specified file name and line number, not current. */
9753 static void
9754 pedwarn_with_file_and_line VPARAMS ((const char *file, size_t file_len,
9755 int line, const char * msgid, ...))
9757 #ifndef ANSI_PROTOTYPES
9758 const char *file;
9759 size_t file_len;
9760 int line;
9761 const char * msgid;
9762 #endif
9763 va_list args;
9765 if (!pedantic_errors && inhibit_warnings)
9766 return;
9768 VA_START (args, msgid);
9770 #ifndef ANSI_PROTOTYPES
9771 file = va_arg (args, const char *);
9772 file_len = va_arg (args, size_t);
9773 line = va_arg (args, int);
9774 msgid = va_arg (args, const char *);
9775 #endif
9777 if (file) {
9778 fwrite (file, sizeof file[0], file_len, stderr);
9779 fprintf (stderr, ":%d: ", line);
9781 if (pedantic_errors)
9782 errors++;
9783 if (!pedantic_errors)
9784 notice ("warning: ");
9785 vnotice (msgid, args);
9786 va_end (args);
9787 fprintf (stderr, "\n");
9790 static void
9791 pedwarn_strange_white_space (ch)
9792 int ch;
9794 switch (ch)
9796 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9797 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9798 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9799 default: abort ();
9803 /* Print the file names and line numbers of the #include
9804 directives which led to the current file. */
9806 static void
9807 print_containing_files ()
9809 FILE_BUF *ip = NULL;
9810 int i;
9811 int first = 1;
9813 /* If stack of files hasn't changed since we last printed
9814 this info, don't repeat it. */
9815 if (last_error_tick == input_file_stack_tick)
9816 return;
9818 for (i = indepth; i >= 0; i--)
9819 if (instack[i].fname != NULL) {
9820 ip = &instack[i];
9821 break;
9824 /* Give up if we don't find a source file. */
9825 if (ip == NULL)
9826 return;
9828 /* Find the other, outer source files. */
9829 for (i--; i >= 0; i--)
9830 if (instack[i].fname != NULL) {
9831 ip = &instack[i];
9832 if (first) {
9833 first = 0;
9834 notice ( "In file included from ");
9835 } else {
9836 notice (",\n from ");
9839 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9840 ip->nominal_fname_len, stderr);
9841 fprintf (stderr, ":%d", ip->lineno);
9843 if (! first)
9844 fprintf (stderr, ":\n");
9846 /* Record we have printed the status as of this time. */
9847 last_error_tick = input_file_stack_tick;
9850 /* Return the line at which an error occurred.
9851 The error is not necessarily associated with the current spot
9852 in the input stack, so LINE says where. LINE will have been
9853 copied from ip->lineno for the current input level.
9854 If the current level is for a file, we return LINE.
9855 But if the current level is not for a file, LINE is meaningless.
9856 In that case, we return the lineno of the innermost file. */
9858 static int
9859 line_for_error (line)
9860 int line;
9862 int i;
9863 int line1 = line;
9865 for (i = indepth; i >= 0; ) {
9866 if (instack[i].fname != 0)
9867 return line1;
9868 i--;
9869 if (i < 0)
9870 return 0;
9871 line1 = instack[i].lineno;
9873 abort ();
9874 /*NOTREACHED*/
9875 return 0;
9879 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9881 * As things stand, nothing is ever placed in the output buffer to be
9882 * removed again except when it's KNOWN to be part of an identifier,
9883 * so flushing and moving down everything left, instead of expanding,
9884 * should work ok.
9887 /* You might think void was cleaner for the return type,
9888 but that would get type mismatch in check_expand in strict ANSI. */
9890 static int
9891 grow_outbuf (obuf, needed)
9892 register FILE_BUF *obuf;
9893 register int needed;
9895 register U_CHAR *p;
9896 int minsize;
9898 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
9899 return 0;
9901 /* Make it at least twice as big as it is now. */
9902 obuf->length *= 2;
9903 /* Make it have at least 150% of the free space we will need. */
9904 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
9905 if (minsize > obuf->length)
9906 obuf->length = minsize;
9908 p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
9910 obuf->bufp = p + (obuf->bufp - obuf->buf);
9911 obuf->buf = p;
9913 return 0;
9916 /* Symbol table for macro names and special symbols */
9919 * install a name in the main hash table, even if it is already there.
9920 * name stops with first non alphanumeric, except leading '#'.
9921 * caller must check against redefinition if that is desired.
9922 * delete_macro () removes things installed by install () in fifo order.
9923 * this is important because of the `defined' special symbol used
9924 * in #if, and also if pushdef/popdef directives are ever implemented.
9926 * If LEN is >= 0, it is the length of the name.
9927 * Otherwise, compute the length by scanning the entire name.
9929 * If HASH is >= 0, it is the precomputed hash code.
9930 * Otherwise, compute the hash code.
9933 static HASHNODE *
9934 install (name, len, type, value, hash)
9935 const U_CHAR *name;
9936 int len;
9937 enum node_type type;
9938 const char *value;
9939 int hash;
9941 register HASHNODE *hp;
9942 register int i, bucket;
9943 register U_CHAR *p;
9944 register const U_CHAR *q;
9946 if (len < 0) {
9947 q = name;
9948 while (is_idchar[*q])
9949 q++;
9950 len = q - name;
9953 if (hash < 0)
9954 hash = hashf (name, len, HASHSIZE);
9956 i = sizeof (HASHNODE) + len + 1;
9957 hp = (HASHNODE *) xmalloc (i);
9958 bucket = hash;
9959 hp->bucket_hdr = &hashtab[bucket];
9960 hp->next = hashtab[bucket];
9961 hashtab[bucket] = hp;
9962 hp->prev = NULL;
9963 if (hp->next != NULL)
9964 hp->next->prev = hp;
9965 hp->type = type;
9966 hp->length = len;
9967 hp->value.cpval = value;
9968 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
9969 p = hp->name;
9970 q = name;
9971 for (i = 0; i < len; i++)
9972 *p++ = *q++;
9973 hp->name[len] = 0;
9974 return hp;
9978 * find the most recent hash node for name "name" (ending with first
9979 * non-identifier char) installed by install
9981 * If LEN is >= 0, it is the length of the name.
9982 * Otherwise, compute the length by scanning the entire name.
9984 * If HASH is >= 0, it is the precomputed hash code.
9985 * Otherwise, compute the hash code.
9988 HASHNODE *
9989 lookup (name, len, hash)
9990 const U_CHAR *name;
9991 int len;
9992 int hash;
9994 register const U_CHAR *bp;
9995 register HASHNODE *bucket;
9997 if (len < 0) {
9998 for (bp = name; is_idchar[*bp]; bp++) ;
9999 len = bp - name;
10002 if (hash < 0)
10003 hash = hashf (name, len, HASHSIZE);
10005 bucket = hashtab[hash];
10006 while (bucket) {
10007 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
10008 return bucket;
10009 bucket = bucket->next;
10011 return NULL;
10015 * Delete a hash node. Some weirdness to free junk from macros.
10016 * More such weirdness will have to be added if you define more hash
10017 * types that need it.
10020 /* Note that the DEFINITION of a macro is removed from the hash table
10021 but its storage is not freed. This would be a storage leak
10022 except that it is not reasonable to keep undefining and redefining
10023 large numbers of macros many times.
10024 In any case, this is necessary, because a macro can be #undef'd
10025 in the middle of reading the arguments to a call to it.
10026 If #undef freed the DEFINITION, that would crash. */
10028 static void
10029 delete_macro (hp)
10030 HASHNODE *hp;
10033 if (hp->prev != NULL)
10034 hp->prev->next = hp->next;
10035 if (hp->next != NULL)
10036 hp->next->prev = hp->prev;
10038 /* Make sure that the bucket chain header that the deleted guy was
10039 on points to the right thing afterwards. */
10040 if (hp == *hp->bucket_hdr)
10041 *hp->bucket_hdr = hp->next;
10043 #if 0
10044 if (hp->type == T_MACRO) {
10045 DEFINITION *d = hp->value.defn;
10046 struct reflist *ap, *nextap;
10048 for (ap = d->pattern; ap != NULL; ap = nextap) {
10049 nextap = ap->next;
10050 free (ap);
10052 free (d);
10054 #endif
10055 free (hp);
10059 * return hash function on name. must be compatible with the one
10060 * computed a step at a time, elsewhere
10063 static int
10064 hashf (name, len, hashsize)
10065 register const U_CHAR *name;
10066 register int len;
10067 int hashsize;
10069 register int r = 0;
10071 while (len--)
10072 r = HASHSTEP (r, *name++);
10074 return MAKE_POS (r) % hashsize;
10078 /* Dump the definition of a single macro HP to OF. */
10080 static void
10081 dump_single_macro (hp, of)
10082 register HASHNODE *hp;
10083 FILE *of;
10085 register DEFINITION *defn = hp->value.defn;
10086 struct reflist *ap;
10087 int offset;
10088 int concat;
10091 /* Print the definition of the macro HP. */
10093 fprintf (of, "#define %s", hp->name);
10095 if (defn->nargs >= 0) {
10096 int i;
10098 fprintf (of, "(");
10099 for (i = 0; i < defn->nargs; i++) {
10100 dump_arg_n (defn, i, of);
10101 if (i + 1 < defn->nargs)
10102 fprintf (of, ", ");
10104 fprintf (of, ")");
10107 fprintf (of, " ");
10109 offset = 0;
10110 concat = 0;
10111 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
10112 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
10113 offset += ap->nchars;
10114 if (!traditional) {
10115 if (ap->nchars != 0)
10116 concat = 0;
10117 if (ap->stringify) {
10118 switch (ap->stringify) {
10119 case SHARP_TOKEN: fprintf (of, "#"); break;
10120 case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
10121 case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
10122 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
10123 default: abort ();
10126 if (ap->raw_before != 0) {
10127 if (concat) {
10128 switch (ap->raw_before) {
10129 case WHITE_SHARP_TOKEN:
10130 case WHITE_PERCENT_COLON_TOKEN:
10131 fprintf (of, " ");
10132 break;
10133 default:
10134 break;
10136 } else {
10137 switch (ap->raw_before) {
10138 case SHARP_TOKEN: fprintf (of, "##"); break;
10139 case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
10140 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10141 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
10142 default: abort ();
10146 concat = 0;
10148 dump_arg_n (defn, ap->argno, of);
10149 if (!traditional && ap->raw_after != 0) {
10150 switch (ap->raw_after) {
10151 case SHARP_TOKEN: fprintf (of, "##"); break;
10152 case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
10153 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10154 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
10155 default: abort ();
10157 concat = 1;
10160 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
10161 fprintf (of, "\n");
10164 /* Dump all macro definitions as #defines to stdout. */
10166 static void
10167 dump_all_macros ()
10169 int bucket;
10171 for (bucket = 0; bucket < HASHSIZE; bucket++) {
10172 register HASHNODE *hp;
10174 for (hp = hashtab[bucket]; hp; hp= hp->next) {
10175 if (hp->type == T_MACRO)
10176 dump_single_macro (hp, stdout);
10181 /* Output to OF a substring of a macro definition.
10182 BASE is the beginning of the definition.
10183 Output characters START thru LENGTH.
10184 Unless traditional, discard newlines outside of strings, thus
10185 converting funny-space markers to ordinary spaces. */
10187 static void
10188 dump_defn_1 (base, start, length, of)
10189 const U_CHAR *base;
10190 int start;
10191 int length;
10192 FILE *of;
10194 const U_CHAR *p = base + start;
10195 const U_CHAR *limit = base + start + length;
10197 if (traditional)
10198 fwrite (p, sizeof (*p), length, of);
10199 else {
10200 while (p < limit) {
10201 if (*p == '\"' || *p =='\'') {
10202 const U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
10203 NULL_PTR, NULL_PTR);
10204 fwrite (p, sizeof (*p), p1 - p, of);
10205 p = p1;
10206 } else {
10207 if (*p != '\n')
10208 putc (*p, of);
10209 p++;
10215 /* Print the name of argument number ARGNUM of macro definition DEFN
10216 to OF.
10217 Recall that DEFN->args.argnames contains all the arg names
10218 concatenated in reverse order with comma-space in between. */
10220 static void
10221 dump_arg_n (defn, argnum, of)
10222 DEFINITION *defn;
10223 int argnum;
10224 FILE *of;
10226 register U_CHAR *p = defn->args.argnames;
10227 while (argnum + 1 < defn->nargs) {
10228 p = (U_CHAR *) index ((char *) p, ' ') + 1;
10229 argnum++;
10232 while (*p && *p != ',') {
10233 putc (*p, of);
10234 p++;
10238 /* Initialize syntactic classifications of characters. */
10240 static void
10241 initialize_char_syntax ()
10243 register int i;
10246 * Set up is_idchar and is_idstart tables. These should be
10247 * faster than saying (is_alpha (c) || c == '_'), etc.
10248 * Set up these things before calling any routines tthat
10249 * refer to them.
10251 for (i = 'a'; i <= 'z'; i++) {
10252 is_idchar[TOUPPER(i)] = 1;
10253 is_idchar[i] = 1;
10254 is_idstart[TOUPPER(i)] = 1;
10255 is_idstart[i] = 1;
10257 for (i = '0'; i <= '9'; i++)
10258 is_idchar[i] = 1;
10259 is_idchar['_'] = 1;
10260 is_idstart['_'] = 1;
10261 is_idchar['$'] = 1;
10262 is_idstart['$'] = 1;
10264 /* horizontal space table */
10265 is_hor_space[' '] = 1;
10266 is_hor_space['\t'] = 1;
10267 is_hor_space['\v'] = 1;
10268 is_hor_space['\f'] = 1;
10269 is_hor_space['\r'] = 1;
10271 is_space[' '] = 1;
10272 is_space['\t'] = 1;
10273 is_space['\v'] = 1;
10274 is_space['\f'] = 1;
10275 is_space['\n'] = 1;
10276 is_space['\r'] = 1;
10279 /* Initialize the built-in macros. */
10281 static void
10282 initialize_builtins (inp, outp)
10283 FILE_BUF *inp;
10284 FILE_BUF *outp;
10286 install ((const U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
10287 install ((const U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
10288 install ((const U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
10289 install ((const U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
10290 install ((const U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
10291 install ((const U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
10292 #ifndef NO_BUILTIN_SIZE_TYPE
10293 install ((const U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
10294 #endif
10295 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10296 install ((const U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
10297 #endif
10298 #ifndef NO_BUILTIN_WCHAR_TYPE
10299 install ((const U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
10300 #endif
10301 install ((const U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
10302 NULL_PTR, -1);
10303 install ((const U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
10304 NULL_PTR, -1);
10305 install ((const U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
10306 NULL_PTR, -1);
10307 install ((const U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
10308 if (!traditional) {
10309 install ((const U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
10310 install ((const U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
10312 /* This is supplied using a -D by the compiler driver
10313 so that it is present only when truly compiling with GNU C. */
10314 /* install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1); */
10315 install ((const U_CHAR *) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST, "1", -1);
10317 if (debug_output)
10319 char directive[2048];
10320 U_CHAR *udirective = (U_CHAR *) directive;
10321 register struct directive *dp = &directive_table[0];
10322 struct tm *timebuf = timestamp ();
10324 sprintf (directive, " __BASE_FILE__ \"%s\"\n",
10325 instack[0].nominal_fname);
10326 output_line_directive (inp, outp, 0, same_file);
10327 pass_thru_directive (udirective, &udirective[strlen (directive)],
10328 outp, dp);
10330 sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
10331 output_line_directive (inp, outp, 0, same_file);
10332 pass_thru_directive (udirective, &udirective[strlen (directive)],
10333 outp, dp);
10335 #ifndef NO_BUILTIN_SIZE_TYPE
10336 sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
10337 output_line_directive (inp, outp, 0, same_file);
10338 pass_thru_directive (udirective, &udirective[strlen (directive)],
10339 outp, dp);
10340 #endif
10342 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10343 sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
10344 output_line_directive (inp, outp, 0, same_file);
10345 pass_thru_directive (udirective, &udirective[strlen (directive)],
10346 outp, dp);
10347 #endif
10349 #ifndef NO_BUILTIN_WCHAR_TYPE
10350 sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
10351 output_line_directive (inp, outp, 0, same_file);
10352 pass_thru_directive (udirective, &udirective[strlen (directive)],
10353 outp, dp);
10354 #endif
10356 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
10357 monthnames[timebuf->tm_mon],
10358 timebuf->tm_mday, timebuf->tm_year + 1900);
10359 output_line_directive (inp, outp, 0, same_file);
10360 pass_thru_directive (udirective, &udirective[strlen (directive)],
10361 outp, dp);
10363 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
10364 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
10365 output_line_directive (inp, outp, 0, same_file);
10366 pass_thru_directive (udirective, &udirective[strlen (directive)],
10367 outp, dp);
10369 if (!traditional)
10371 sprintf (directive, " __STDC__ 1");
10372 output_line_directive (inp, outp, 0, same_file);
10373 pass_thru_directive (udirective, &udirective[strlen (directive)],
10374 outp, dp);
10376 if (objc)
10378 sprintf (directive, " __OBJC__ 1");
10379 output_line_directive (inp, outp, 0, same_file);
10380 pass_thru_directive (udirective, &udirective[strlen (directive)],
10381 outp, dp);
10387 * process a given definition string, for initialization
10388 * If STR is just an identifier, define it with value 1.
10389 * If STR has anything after the identifier, then it should
10390 * be identifier=definition.
10393 static void
10394 make_definition (str)
10395 char *str;
10397 FILE_BUF *ip;
10398 struct directive *kt;
10399 U_CHAR *buf, *p;
10401 p = buf = (U_CHAR *) str;
10402 if (!is_idstart[*p]) {
10403 error ("malformed option `-D %s'", str);
10404 return;
10406 while (is_idchar[*++p])
10408 if (*p == '(') {
10409 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
10411 if (*p++ != ')')
10412 p = (U_CHAR *) str; /* Error */
10414 if (*p == 0) {
10415 buf = (U_CHAR *) alloca (p - buf + 4);
10416 strcpy ((char *)buf, str);
10417 strcat ((char *)buf, " 1");
10418 } else if (*p != '=') {
10419 error ("malformed option `-D %s'", str);
10420 return;
10421 } else {
10422 U_CHAR *q;
10423 /* Copy the entire option so we can modify it. */
10424 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
10425 strncpy ((char *) buf, str, p - (U_CHAR *) str);
10426 /* Change the = to a space. */
10427 buf[p - (U_CHAR *) str] = ' ';
10428 /* Scan for any backslash-newline and remove it. */
10429 p++;
10430 q = &buf[p - (U_CHAR *) str];
10431 while (*p) {
10432 if (*p == '\"' || *p == '\'') {
10433 int unterminated = 0;
10434 const U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
10435 NULL_PTR, NULL_PTR, &unterminated);
10436 if (unterminated)
10437 return;
10438 while (p != p1) {
10439 if (*p == '\\' && p[1] == '\n')
10440 p += 2;
10441 else if (*p == '\n')
10443 *q++ = '\\';
10444 *q++ = 'n';
10445 p++;
10447 else
10448 *q++ = *p++;
10450 } else if (*p == '\\' && p[1] == '\n')
10451 p += 2;
10452 /* Change newline chars into newline-markers. */
10453 else if (*p == '\n')
10455 *q++ = '\n';
10456 *q++ = '\n';
10457 p++;
10459 else
10460 *q++ = *p++;
10462 *q = 0;
10465 ip = &instack[++indepth];
10466 ip->nominal_fname = ip->fname = "*Initialization*";
10467 ip->nominal_fname_len = strlen (ip->nominal_fname);
10469 ip->buf = ip->bufp = buf;
10470 ip->length = strlen ((char *) buf);
10471 ip->lineno = 1;
10472 ip->macro = 0;
10473 ip->free_ptr = 0;
10474 ip->if_stack = if_stack;
10475 ip->system_header_p = 0;
10477 for (kt = directive_table; kt->type != T_DEFINE; kt++)
10480 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10481 do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
10482 --indepth;
10485 /* JF, this does the work for the -U option */
10487 static void
10488 make_undef (str, op)
10489 char *str;
10490 FILE_BUF *op;
10492 FILE_BUF *ip;
10493 struct directive *kt;
10495 ip = &instack[++indepth];
10496 ip->nominal_fname = ip->fname = "*undef*";
10497 ip->nominal_fname_len = strlen (ip->nominal_fname);
10499 ip->buf = ip->bufp = (U_CHAR *) str;
10500 ip->length = strlen (str);
10501 ip->lineno = 1;
10502 ip->macro = 0;
10503 ip->free_ptr = 0;
10504 ip->if_stack = if_stack;
10505 ip->system_header_p = 0;
10507 for (kt = directive_table; kt->type != T_UNDEF; kt++)
10510 do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
10511 --indepth;
10514 /* Process the string STR as if it appeared as the body of a #assert.
10515 OPTION is the option name for which STR was the argument. */
10517 static void
10518 make_assertion (option, str)
10519 const char *option;
10520 const char *str;
10522 FILE_BUF *ip;
10523 struct directive *kt;
10524 U_CHAR *buf, *p, *q;
10526 /* Copy the entire option so we can modify it. */
10527 buf = (U_CHAR *) alloca (strlen (str) + 1);
10528 strcpy ((char *) buf, str);
10529 /* Scan for any backslash-newline and remove it. */
10530 p = q = buf;
10531 while (*p) {
10532 if (*p == '\\' && p[1] == '\n')
10533 p += 2;
10534 else
10535 *q++ = *p++;
10537 *q = 0;
10539 p = buf;
10540 if (!is_idstart[*p]) {
10541 error ("malformed option `%s %s'", option, str);
10542 return;
10544 while (is_idchar[*++p])
10546 SKIP_WHITE_SPACE (p);
10547 if (! (*p == 0 || *p == '(')) {
10548 error ("malformed option `%s %s'", option, str);
10549 return;
10552 ip = &instack[++indepth];
10553 ip->nominal_fname = ip->fname = "*Initialization*";
10554 ip->nominal_fname_len = strlen (ip->nominal_fname);
10556 ip->buf = ip->bufp = buf;
10557 ip->length = strlen ((char *) buf);
10558 ip->lineno = 1;
10559 ip->macro = 0;
10560 ip->free_ptr = 0;
10561 ip->if_stack = if_stack;
10562 ip->system_header_p = 0;
10564 for (kt = directive_table; kt->type != T_ASSERT; kt++)
10567 /* Pass NULL as output ptr to do_define since we KNOW it never does
10568 any output.... */
10569 do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
10570 --indepth;
10573 /* The previous include prefix, if any, is PREV_FILE_NAME.
10574 Translate any pathnames with COMPONENT.
10575 Allocate a new include prefix whose name is the
10576 simplified concatenation of PREFIX and NAME,
10577 with a trailing / added if needed.
10578 But return 0 if the include prefix should be ignored,
10579 e.g. because it is a duplicate of PREV_FILE_NAME. */
10581 static struct file_name_list *
10582 new_include_prefix (prev_file_name, component, prefix, name)
10583 struct file_name_list *prev_file_name;
10584 const char *component;
10585 const char *prefix;
10586 const char *name;
10588 if (name == 0)
10589 fatal ("Directory name missing after command line option");
10591 if (*name == 0)
10592 /* Ignore the empty string. */
10593 return 0;
10595 prefix = update_path (prefix, component);
10596 name = update_path (name, component);
10599 struct file_name_list *dir
10600 = ((struct file_name_list *)
10601 xmalloc (sizeof (struct file_name_list)
10602 + strlen (prefix) + strlen (name) + 2));
10603 size_t len;
10604 strcpy (dir->fname, prefix);
10605 strcat (dir->fname, name);
10606 len = simplify_filename (dir->fname);
10608 /* Convert directory name to a prefix. */
10609 if (len && dir->fname[len - 1] != DIR_SEPARATOR) {
10610 if (len == 1 && dir->fname[len - 1] == '.')
10611 len = 0;
10612 else
10613 #ifdef VMS
10614 /* must be '/', hack_vms_include_specification triggers on it. */
10615 dir->fname[len++] = '/';
10616 #else
10617 dir->fname[len++] = DIR_SEPARATOR;
10618 #endif
10619 dir->fname[len] = 0;
10622 /* Ignore a directory whose name matches the previous one. */
10623 if (prev_file_name && !strcmp (prev_file_name->fname, dir->fname)) {
10624 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10625 if (!first_bracket_include)
10626 first_bracket_include = prev_file_name;
10627 free (dir);
10628 return 0;
10631 #ifndef VMS
10632 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10634 /* Add a trailing "." if there is a filename. This increases the number
10635 of systems that can stat directories. We remove it below. */
10636 if (len != 0)
10638 dir->fname[len] = '.';
10639 dir->fname[len + 1] = 0;
10642 /* Ignore a nonexistent directory. */
10643 if (stat (len ? dir->fname : ".", &dir->st) != 0) {
10644 if (errno != ENOENT && errno != ENOTDIR)
10645 error_from_errno (dir->fname);
10646 free (dir);
10647 return 0;
10650 if (len != 0)
10651 dir->fname[len] = 0;
10653 /* Ignore a directory whose identity matches the previous one. */
10654 if (prev_file_name
10655 && INO_T_EQ (prev_file_name->st.st_ino, dir->st.st_ino)
10656 && prev_file_name->st.st_dev == dir->st.st_dev) {
10657 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10658 if (!first_bracket_include)
10659 first_bracket_include = prev_file_name;
10660 free (dir);
10661 return 0;
10663 #endif /* ! VMS */
10665 dir->next = 0;
10666 dir->c_system_include_path = 0;
10667 dir->got_name_map = 0;
10669 return dir;
10673 /* Append a chain of `struct file_name_list's
10674 to the end of the main include chain.
10675 FIRST is the beginning of the chain to append, and LAST is the end. */
10677 static void
10678 append_include_chain (first, last)
10679 struct file_name_list *first, *last;
10681 struct file_name_list *dir;
10683 if (!first || !last)
10684 return;
10686 if (include == 0)
10687 include = first;
10688 else
10689 last_include->next = first;
10691 if (first_bracket_include == 0)
10692 first_bracket_include = first;
10694 for (dir = first; ; dir = dir->next) {
10695 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
10696 if (len > max_include_len)
10697 max_include_len = len;
10698 if (dir == last)
10699 break;
10702 last->next = NULL;
10703 last_include = last;
10706 /* Place into DST a representation of the file named SRC that is suitable
10707 for `make'. Do not null-terminate DST. Return its length. */
10708 static int
10709 quote_string_for_make (dst, src)
10710 char *dst;
10711 const char *src;
10713 const char *p = src;
10714 int i = 0;
10715 for (;;)
10717 char c = *p++;
10718 switch (c)
10720 case '\0':
10721 case ' ':
10722 case '\t':
10724 /* GNU make uses a weird quoting scheme for white space.
10725 A space or tab preceded by 2N+1 backslashes represents
10726 N backslashes followed by space; a space or tab
10727 preceded by 2N backslashes represents N backslashes at
10728 the end of a file name; and backslashes in other
10729 contexts should not be doubled. */
10730 const char *q;
10731 for (q = p - 1; src < q && q[-1] == '\\'; q--)
10733 if (dst)
10734 dst[i] = '\\';
10735 i++;
10738 if (!c)
10739 return i;
10740 if (dst)
10741 dst[i] = '\\';
10742 i++;
10743 goto ordinary_char;
10745 case '$':
10746 if (dst)
10747 dst[i] = c;
10748 i++;
10749 /* Fall through. This can mishandle things like "$(" but
10750 there's no easy fix. */
10751 default:
10752 ordinary_char:
10753 /* This can mishandle characters in the string "\0\n%*?[\\~";
10754 exactly which chars are mishandled depends on the `make' version.
10755 We know of no portable solution for this;
10756 even GNU make 3.76.1 doesn't solve the problem entirely.
10757 (Also, '\0' is mishandled due to our calling conventions.) */
10758 if (dst)
10759 dst[i] = c;
10760 i++;
10761 break;
10767 /* Add output to `deps_buffer' for the -M switch.
10768 STRING points to the text to be output.
10769 SPACER is ':' for targets, ' ' for dependencies. */
10771 static void
10772 deps_output (string, spacer)
10773 const char *string;
10774 int spacer;
10776 int size = quote_string_for_make ((char *) 0, string);
10778 if (size == 0)
10779 return;
10781 #ifndef MAX_OUTPUT_COLUMNS
10782 #define MAX_OUTPUT_COLUMNS 72
10783 #endif
10784 if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
10785 && 1 < deps_column) {
10786 bcopy (" \\\n ", &deps_buffer[deps_size], 4);
10787 deps_size += 4;
10788 deps_column = 1;
10789 if (spacer == ' ')
10790 spacer = 0;
10793 if (deps_size + 2 * size + 8 > deps_allocated_size) {
10794 deps_allocated_size = (deps_size + 2 * size + 50) * 2;
10795 deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
10797 if (spacer == ' ') {
10798 deps_buffer[deps_size++] = ' ';
10799 deps_column++;
10801 quote_string_for_make (&deps_buffer[deps_size], string);
10802 deps_size += size;
10803 deps_column += size;
10804 if (spacer == ':') {
10805 deps_buffer[deps_size++] = ':';
10806 deps_column++;
10808 deps_buffer[deps_size] = 0;
10811 void
10812 fatal VPARAMS ((const char * msgid, ...))
10814 #ifndef ANSI_PROTOTYPES
10815 const char * msgid;
10816 #endif
10817 va_list args;
10819 fprintf (stderr, "%s: ", progname);
10820 VA_START (args, msgid);
10822 #ifndef ANSI_PROTOTYPES
10823 msgid = va_arg (args, const char *);
10824 #endif
10825 vnotice (msgid, args);
10826 va_end (args);
10827 fprintf (stderr, "\n");
10828 exit (FATAL_EXIT_CODE);
10831 /* More 'friendly' abort that prints the line and file.
10832 config.h can #define abort fancy_abort if you like that sort of thing. */
10834 void
10835 fancy_abort ()
10837 fatal ("Internal gcc abort.");
10840 static void
10841 perror_with_name (name)
10842 const char *name;
10844 fprintf (stderr, "%s: %s: %s\n", progname, name, xstrerror (errno));
10845 errors++;
10848 static void
10849 pfatal_with_name (name)
10850 const char *name;
10852 perror_with_name (name);
10853 #ifdef VMS
10854 exit (vaxc$errno);
10855 #else
10856 exit (FATAL_EXIT_CODE);
10857 #endif
10860 /* Handler for SIGPIPE. */
10862 static void
10863 pipe_closed (signo)
10864 /* If this is missing, some compilers complain. */
10865 int signo ATTRIBUTE_UNUSED;
10867 fatal ("output pipe has been closed");
10870 static void
10871 memory_full ()
10873 fatal ("Memory exhausted.");
10876 #ifdef VMS
10878 /* Under VMS we need to fix up the "include" specification filename.
10880 Rules for possible conversions
10882 fullname tried paths
10884 name name
10885 ./dir/name [.dir]name
10886 /dir/name dir:name
10887 /name [000000]name, name
10888 dir/name dir:[000000]name, dir:name, dir/name
10889 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10890 path:/name path:[000000]name, path:name
10891 path:/dir/name path:[000000.dir]name, path:[dir]name
10892 path:dir/name path:[dir]name
10893 [path]:[dir]name [path.dir]name
10894 path/[dir]name [path.dir]name
10896 The path:/name input is constructed when expanding <> includes.
10898 return 1 if name was changed, 0 else. */
10900 static int
10901 hack_vms_include_specification (fullname, vaxc_include)
10902 char *fullname;
10903 int vaxc_include;
10905 register char *basename, *unixname, *local_ptr, *first_slash;
10906 int f, check_filename_before_returning, must_revert;
10907 char Local[512];
10909 check_filename_before_returning = 0;
10910 must_revert = 0;
10911 /* See if we can find a 1st slash. If not, there's no path information. */
10912 first_slash = index (fullname, '/');
10913 if (first_slash == 0)
10914 return 0; /* Nothing to do!!! */
10916 /* construct device spec if none given. */
10918 if (index (fullname, ':') == 0)
10921 /* If fullname has a slash, take it as device spec. */
10923 if (first_slash == fullname)
10925 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
10926 if (first_slash)
10927 *first_slash = ':'; /* make device spec */
10928 for (basename = fullname; *basename != 0; basename++)
10929 *basename = *(basename+1); /* remove leading slash */
10931 else if ((first_slash[-1] != '.') /* keep ':/', './' */
10932 && (first_slash[-1] != ':')
10933 && (first_slash[-1] != ']')) /* or a vms path */
10935 *first_slash = ':';
10937 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
10938 && (first_slash[-1] == '.'))
10939 fullname += 2;
10942 /* Get part after first ':' (basename[-1] == ':')
10943 or last '/' (basename[-1] == '/'). */
10945 basename = base_name (fullname);
10948 * Check if we have a vax-c style '#include filename'
10949 * and add the missing .h
10952 if (vaxc_include && !index (basename,'.'))
10953 strcat (basename, ".h");
10955 local_ptr = Local; /* initialize */
10957 /* We are trying to do a number of things here. First of all, we are
10958 trying to hammer the filenames into a standard format, such that later
10959 processing can handle them.
10961 If the file name contains something like [dir.], then it recognizes this
10962 as a root, and strips the ".]". Later processing will add whatever is
10963 needed to get things working properly.
10965 If no device is specified, then the first directory name is taken to be
10966 a device name (or a rooted logical). */
10968 /* Point to the UNIX filename part (which needs to be fixed!)
10969 but skip vms path information.
10970 [basename != fullname since first_slash != 0]. */
10972 if ((basename[-1] == ':') /* vms path spec. */
10973 || (basename[-1] == ']')
10974 || (basename[-1] == '>'))
10975 unixname = basename;
10976 else
10977 unixname = fullname;
10979 if (*unixname == '/')
10980 unixname++;
10982 /* If the directory spec is not rooted, we can just copy
10983 the UNIX filename part and we are done. */
10985 if (((basename - fullname) > 1)
10986 && ( (basename[-1] == ']')
10987 || (basename[-1] == '>')))
10989 if (basename[-2] != '.')
10992 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10993 -> PATH]:/name (basename = '/name', unixname = 'name')
10994 We strip the `]', and then splice the two parts of the name in the
10995 usual way. Given the default locations for include files in cccp.c,
10996 we will only use this code if the user specifies alternate locations
10997 with the /include (-I) switch on the command line. */
10999 basename -= 1; /* Strip "]" */
11000 unixname--; /* backspace */
11002 else
11005 /* The VMS part has a ".]" at the end, and this will not do. Later
11006 processing will add a second directory spec, and this would be a syntax
11007 error. Thus we strip the ".]", and thus merge the directory specs.
11008 We also backspace unixname, so that it points to a '/'. This inhibits the
11009 generation of the 000000 root directory spec (which does not belong here
11010 in this case). */
11012 basename -= 2; /* Strip ".]" */
11013 unixname--; /* backspace */
11017 else
11021 /* We drop in here if there is no VMS style directory specification yet.
11022 If there is no device specification either, we make the first dir a
11023 device and try that. If we do not do this, then we will be essentially
11024 searching the users default directory (as if they did a #include "asdf.h").
11026 Then all we need to do is to push a '[' into the output string. Later
11027 processing will fill this in, and close the bracket. */
11029 if ((unixname != fullname) /* vms path spec found. */
11030 && (basename[-1] != ':'))
11031 *local_ptr++ = ':'; /* dev not in spec. take first dir */
11033 *local_ptr++ = '['; /* Open the directory specification */
11036 if (unixname == fullname) /* no vms dir spec. */
11038 must_revert = 1;
11039 if ((first_slash != 0) /* unix dir spec. */
11040 && (*unixname != '/') /* not beginning with '/' */
11041 && (*unixname != '.')) /* or './' or '../' */
11042 *local_ptr++ = '.'; /* dir is local ! */
11045 /* at this point we assume that we have the device spec, and (at least
11046 the opening "[" for a directory specification. We may have directories
11047 specified already.
11049 If there are no other slashes then the filename will be
11050 in the "root" directory. Otherwise, we need to add
11051 directory specifications. */
11053 if (index (unixname, '/') == 0)
11055 /* if no directories specified yet and none are following. */
11056 if (local_ptr[-1] == '[')
11058 /* Just add "000000]" as the directory string */
11059 strcpy (local_ptr, "000000]");
11060 local_ptr += strlen (local_ptr);
11061 check_filename_before_returning = 1; /* we might need to fool with this later */
11064 else
11067 /* As long as there are still subdirectories to add, do them. */
11068 while (index (unixname, '/') != 0)
11070 /* If this token is "." we can ignore it
11071 if it's not at the beginning of a path. */
11072 if ((unixname[0] == '.') && (unixname[1] == '/'))
11074 /* remove it at beginning of path. */
11075 if ( ((unixname == fullname) /* no device spec */
11076 && (fullname+2 != basename)) /* starts with ./ */
11077 /* or */
11078 || ((basename[-1] == ':') /* device spec */
11079 && (unixname-1 == basename))) /* and ./ afterwards */
11080 *local_ptr++ = '.'; /* make '[.' start of path. */
11081 unixname += 2;
11082 continue;
11085 /* Add a subdirectory spec. Do not duplicate "." */
11086 if ( local_ptr[-1] != '.'
11087 && local_ptr[-1] != '['
11088 && local_ptr[-1] != '<')
11089 *local_ptr++ = '.';
11091 /* If this is ".." then the spec becomes "-" */
11092 if ( (unixname[0] == '.')
11093 && (unixname[1] == '.')
11094 && (unixname[2] == '/'))
11096 /* Add "-" and skip the ".." */
11097 if ((local_ptr[-1] == '.')
11098 && (local_ptr[-2] == '['))
11099 local_ptr--; /* prevent [.- */
11100 *local_ptr++ = '-';
11101 unixname += 3;
11102 continue;
11105 /* Copy the subdirectory */
11106 while (*unixname != '/')
11107 *local_ptr++= *unixname++;
11109 unixname++; /* Skip the "/" */
11112 /* Close the directory specification */
11113 if (local_ptr[-1] == '.') /* no trailing periods */
11114 local_ptr--;
11116 if (local_ptr[-1] == '[') /* no dir needed */
11117 local_ptr--;
11118 else
11119 *local_ptr++ = ']';
11122 /* Now add the filename. */
11124 while (*unixname)
11125 *local_ptr++ = *unixname++;
11126 *local_ptr = 0;
11128 /* Now append it to the original VMS spec. */
11130 strcpy ((must_revert==1)?fullname:basename, Local);
11132 /* If we put a [000000] in the filename, try to open it first. If this fails,
11133 remove the [000000], and return that name. This provides flexibility
11134 to the user in that they can use both rooted and non-rooted logical names
11135 to point to the location of the file. */
11137 if (check_filename_before_returning)
11139 f = open (fullname, O_RDONLY, 0666);
11140 if (f >= 0)
11142 /* The file name is OK as it is, so return it as is. */
11143 close (f);
11144 return 1;
11147 /* The filename did not work. Try to remove the [000000] from the name,
11148 and return it. */
11150 basename = index (fullname, '[');
11151 local_ptr = index (fullname, ']') + 1;
11152 strcpy (basename, local_ptr); /* this gets rid of it */
11156 return 1;
11158 #endif /* VMS */
11160 #ifdef VMS
11162 /* The following wrapper functions supply additional arguments to the VMS
11163 I/O routines to optimize performance with file handling. The arguments
11164 are:
11165 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11166 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11167 "fop=tef"- Truncate unused portions of file when closing file.
11168 "shr=nil"- Disallow file sharing while file is open. */
11170 static FILE *
11171 VMS_freopen (fname, type, oldfile)
11172 char *fname;
11173 char *type;
11174 FILE *oldfile;
11176 #undef freopen /* Get back the real freopen routine. */
11177 if (strcmp (type, "w") == 0)
11178 return freopen (fname, type, oldfile,
11179 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11180 return freopen (fname, type, oldfile, "mbc=16");
11183 static FILE *
11184 VMS_fopen (fname, type)
11185 char *fname;
11186 char *type;
11188 #undef fopen /* Get back the real fopen routine. */
11189 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11190 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11191 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11192 FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
11194 if (*type == 'w')
11195 return (*vmslib_fopen) (fname, type, "mbc=32",
11196 "deq=64", "fop=tef", "shr=nil");
11197 else
11198 return (*vmslib_fopen) (fname, type, "mbc=32");
11201 static int
11202 VMS_open (fname, flags, prot)
11203 char *fname;
11204 int flags;
11205 int prot;
11207 #undef open /* Get back the real open routine. */
11208 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
11211 /* more VMS hackery */
11212 #include <fab.h>
11213 #include <nam.h>
11215 extern unsigned long SYS$PARSE(), SYS$SEARCH();
11217 /* Work around another library bug. If a file is located via a searchlist,
11218 and if the device it's on is not the same device as the one specified
11219 in the first element of that searchlist, then both stat() and fstat()
11220 will fail to return info about it. `errno' will be set to EVMSERR, and
11221 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11222 We can get around this by fully parsing the filename and then passing
11223 that absolute name to stat().
11225 Without this fix, we can end up failing to find header files, which is
11226 bad enough, but then compounding the problem by reporting the reason for
11227 failure as "normal successful completion." */
11229 #undef fstat /* Get back to the library version. */
11231 static int
11232 VMS_fstat (fd, statbuf)
11233 int fd;
11234 struct stat *statbuf;
11236 int result = fstat (fd, statbuf);
11238 if (result < 0)
11240 FILE *fp;
11241 char nambuf[NAM$C_MAXRSS+1];
11243 if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
11244 result = VMS_stat (nambuf, statbuf);
11245 /* No fclose(fp) here; that would close(fd) as well. */
11248 return result;
11251 static int
11252 VMS_stat (name, statbuf)
11253 const char *name;
11254 struct stat *statbuf;
11256 int result = stat (name, statbuf);
11258 if (result < 0)
11260 struct FAB fab;
11261 struct NAM nam;
11262 char exp_nam[NAM$C_MAXRSS+1], /* expanded name buffer for SYS$PARSE */
11263 res_nam[NAM$C_MAXRSS+1]; /* resultant name buffer for SYS$SEARCH */
11265 fab = cc$rms_fab;
11266 fab.fab$l_fna = (char *) name;
11267 fab.fab$b_fns = (unsigned char) strlen (name);
11268 fab.fab$l_nam = (void *) &nam;
11269 nam = cc$rms_nam;
11270 nam.nam$l_esa = exp_nam, nam.nam$b_ess = sizeof exp_nam - 1;
11271 nam.nam$l_rsa = res_nam, nam.nam$b_rss = sizeof res_nam - 1;
11272 nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
11273 if (SYS$PARSE (&fab) & 1)
11275 if (SYS$SEARCH (&fab) & 1)
11277 res_nam[nam.nam$b_rsl] = '\0';
11278 result = stat (res_nam, statbuf);
11280 /* Clean up searchlist context cached by the system. */
11281 nam.nam$b_nop = NAM$M_SYNCHK;
11282 fab.fab$l_fna = 0, fab.fab$b_fns = 0;
11283 (void) SYS$PARSE (&fab);
11287 return result;
11290 static size_t
11291 VMS_fwrite (ptr, size, nitems, stream)
11292 void const *ptr;
11293 size_t size;
11294 size_t nitems;
11295 FILE *stream;
11297 /* VMS fwrite has undesirable results
11298 if STREAM happens to be a record oriented file.
11299 Work around this problem by writing each character individually. */
11300 char const *p = ptr;
11301 size_t bytes = size * nitems;
11302 char *lim = p + bytes;
11304 while (p < lim)
11305 if (putc (*p++, stream) == EOF)
11306 return 0;
11308 return bytes;
11310 #endif /* VMS */