oops - omitted from previous delta fixing UNIQUE_SECTION
[official-gcc.git] / gcc / cccp.c
blob8771d0d38441915a152271688ff8b95b807389e3
1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 87, 89, 92-99, 2000 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
23 #include "system.h"
24 #include <signal.h>
26 #ifdef HAVE_SYS_RESOURCE_H
27 # include <sys/resource.h>
28 #endif
30 typedef unsigned char U_CHAR;
32 #include "pcp.h"
33 #include "intl.h"
34 #include "prefix.h"
36 #ifdef MULTIBYTE_CHARS
37 #include "mbchar.h"
38 #include <locale.h>
39 #endif /* MULTIBYTE_CHARS */
41 #ifndef GET_ENV_PATH_LIST
42 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
43 #endif
45 #ifndef STANDARD_INCLUDE_DIR
46 # define STANDARD_INCLUDE_DIR "/usr/include"
47 #endif
49 /* By default, the suffix for object files is ".o". */
50 #ifdef OBJECT_SUFFIX
51 # define HAVE_OBJECT_SUFFIX
52 #else
53 # define OBJECT_SUFFIX ".o"
54 #endif
56 /* VMS-specific definitions */
57 #ifdef VMS
58 #include <descrip.h>
59 #include <ssdef.h>
60 #include <syidef.h>
61 #define open(fname,mode,prot) VMS_open (fname,mode,prot)
62 #define fopen(fname,mode) VMS_fopen (fname,mode)
63 #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
64 #define fstat(fd,stbuf) VMS_fstat (fd,stbuf)
65 #define fwrite(ptr,size,nitems,stream) VMS_fwrite (ptr,size,nitems,stream)
66 static int VMS_fstat (), VMS_stat ();
67 static int VMS_open ();
68 static FILE *VMS_fopen ();
69 static FILE *VMS_freopen ();
70 static size_t VMS_fwrite ();
71 static void hack_vms_include_specification ();
72 #define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
73 #define INO_T_HASH(a) 0
74 #define INCLUDE_LEN_FUDGE 12 /* leave room for VMS syntax conversion */
75 #endif /* VMS */
77 /* Windows does not natively support inodes, and neither does MSDOS.
78 Cygwin's emulation can generate non-unique inodes, so don't use it. */
79 #if (defined (_WIN32) && ! defined (_UWIN)) \
80 || defined (__MSDOS__)
81 #define INO_T_EQ(a, b) 0
82 #endif
84 #ifndef INO_T_EQ
85 #define INO_T_EQ(a, b) ((a) == (b))
86 #endif
88 #ifndef INO_T_HASH
89 #define INO_T_HASH(a) (a)
90 #endif
92 #ifndef INCLUDE_LEN_FUDGE
93 #define INCLUDE_LEN_FUDGE 0
94 #endif
96 /* External declarations. */
98 extern char *version_string;
99 HOST_WIDEST_INT parse_escape PARAMS ((char **, HOST_WIDEST_INT));
100 HOST_WIDEST_INT parse_c_expression PARAMS ((char *, int));
102 /* Name under which this program was invoked. */
104 static const char *progname;
106 /* Nonzero means use extra default include directories for C++. */
108 static int cplusplus;
110 /* Nonzero means handle cplusplus style comments */
112 static int cplusplus_comments;
114 /* Nonzero means handle #import, for objective C. */
116 static int objc;
118 /* Nonzero means this is an assembly file, and allow
119 unknown directives, which could be comments. */
121 static int lang_asm;
123 /* Current maximum length of directory names in the search path
124 for include files. (Altered as we get more of them.) */
126 static int max_include_len;
128 /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
130 static int for_lint = 0;
132 /* Nonzero means copy comments into the output file. */
134 static int put_out_comments = 0;
136 /* Nonzero means don't process the ANSI trigraph sequences. */
138 static int no_trigraphs = 0;
140 /* Nonzero means print the names of included files rather than
141 the preprocessed output. 1 means just the #include "...",
142 2 means #include <...> as well. */
144 static int print_deps = 0;
146 /* Nonzero if missing .h files in -M output are assumed to be generated
147 files and not errors. */
149 static int print_deps_missing_files = 0;
151 /* Nonzero means print names of header files (-H). */
153 static int print_include_names = 0;
155 /* Nonzero means don't output line number information. */
157 static int no_line_directives;
159 /* Nonzero means output the text in failing conditionals,
160 inside #failed ... #endfailed. */
162 static int output_conditionals;
164 /* dump_only means inhibit output of the preprocessed text
165 and instead output the definitions of all user-defined
166 macros in a form suitable for use as input to cccp.
167 dump_names means pass #define and the macro name through to output.
168 dump_definitions means pass the whole definition (plus #define) through
171 static enum {dump_none, dump_only, dump_names, dump_definitions}
172 dump_macros = dump_none;
174 /* Nonzero means pass all #define and #undef directives which we actually
175 process through to the output stream. This feature is used primarily
176 to allow cc1 to record the #defines and #undefs for the sake of
177 debuggers which understand about preprocessor macros, but it may
178 also be useful with -E to figure out how symbols are defined, and
179 where they are defined. */
180 static int debug_output = 0;
182 /* Nonzero means pass #include lines through to the output,
183 even if they are ifdefed out. */
184 static int dump_includes;
186 /* Nonzero indicates special processing used by the pcp program. The
187 special effects of this mode are:
189 Inhibit all macro expansion, except those inside #if directives.
191 Process #define directives normally, and output their contents
192 to the output file.
194 Output preconditions to pcp_outfile indicating all the relevant
195 preconditions for use of this file in a later cpp run.
197 static FILE *pcp_outfile;
199 /* Nonzero means we are inside an IF during a -pcp run. In this mode
200 macro expansion is done, and preconditions are output for all macro
201 uses requiring them. */
202 static int pcp_inside_if;
204 /* Nonzero means never to include precompiled files.
205 This is 1 since there's no way now to make precompiled files,
206 so it's not worth testing for them. */
207 static int no_precomp = 1;
209 /* Nonzero means give all the error messages the ANSI standard requires. */
211 int pedantic;
213 /* Nonzero means try to make failure to fit ANSI C an error. */
215 static int pedantic_errors;
217 /* Nonzero means don't print warning messages. -w. */
219 static int inhibit_warnings = 0;
221 /* Nonzero means warn if slash-star appears in a slash-star comment,
222 or if newline-backslash appears in a slash-slash comment. */
224 static int warn_comments;
226 /* Nonzero means warn if a macro argument is (or would be)
227 stringified with -traditional. */
229 static int warn_stringify;
231 /* Nonzero means warn if there are any trigraphs. */
233 static int warn_trigraphs;
235 /* Nonzero means warn if undefined identifiers are evaluated in an #if. */
237 static int warn_undef;
239 /* Nonzero means warn if we find white space where it doesn't belong. */
241 static int warn_white_space;
243 /* Nonzero means warn if #import is used. */
245 static int warn_import = 1;
247 /* Nonzero means turn warnings into errors. */
249 static int warnings_are_errors;
251 /* Nonzero means try to imitate old fashioned non-ANSI preprocessor. */
253 int traditional;
255 /* Nonzero for the 1989 C Standard, including corrigenda and amendments. */
257 int c89;
259 /* Nonzero for the 1999 C Standard. */
261 int c99;
263 /* Nonzero causes output not to be done,
264 but directives such as #define that have side effects
265 are still obeyed. */
267 static int no_output;
269 /* Nonzero means we should look for header.gcc files that remap file names. */
270 static int remap;
272 /* Nonzero means this file was included with a -imacros or -include
273 command line and should not be recorded as an include file. */
275 static int no_record_file;
277 /* Nonzero means that we have finished processing the command line options.
278 This flag is used to decide whether or not to issue certain errors
279 and/or warnings. */
281 static int done_initializing = 0;
283 /* Line where a newline was first seen in a string constant. */
285 static int multiline_string_line = 0;
287 /* I/O buffer structure.
288 The `fname' field is nonzero for source files and #include files
289 and for the dummy text used for -D and -U.
290 It is zero for rescanning results of macro expansion
291 and for expanding macro arguments. */
292 #define INPUT_STACK_MAX 400
293 static struct file_buf {
294 const char *fname;
295 /* Filename specified with #line directive. */
296 const char *nominal_fname;
297 /* The length of nominal_fname, which may contain embedded NULs. */
298 size_t nominal_fname_len;
299 /* Include file description. */
300 struct include_file *inc;
301 /* Record where in the search path this file was found.
302 For #include_next. */
303 struct file_name_list *dir;
304 int lineno;
305 int length;
306 U_CHAR *buf;
307 U_CHAR *bufp;
308 /* Macro that this level is the expansion of.
309 Included so that we can reenable the macro
310 at the end of this level. */
311 struct hashnode *macro;
312 /* Value of if_stack at start of this file.
313 Used to prohibit unmatched #endif (etc) in an include file. */
314 struct if_stack *if_stack;
315 /* Object to be freed at end of input at this level. */
316 U_CHAR *free_ptr;
317 /* True if this is a system header file; see is_system_include. */
318 char system_header_p;
319 } instack[INPUT_STACK_MAX];
321 static int last_error_tick; /* Incremented each time we print it. */
322 static int input_file_stack_tick; /* Incremented when the status changes. */
324 /* Current nesting level of input sources.
325 `instack[indepth]' is the level currently being read. */
326 static int indepth = -1;
327 #define CHECK_DEPTH(code) \
328 if (indepth >= (INPUT_STACK_MAX - 1)) \
330 error_with_line (line_for_error (instack[indepth].lineno), \
331 "macro or `#include' recursion too deep"); \
332 code; \
335 /* Current depth in #include directives that use <...>. */
336 static int system_include_depth = 0;
338 typedef struct file_buf FILE_BUF;
340 /* The output buffer. Its LENGTH field is the amount of room allocated
341 for the buffer, not the number of chars actually present. To get
342 that, subtract outbuf.buf from outbuf.bufp. */
344 #define OUTBUF_SIZE 10 /* initial size of output buffer */
345 static FILE_BUF outbuf;
347 /* Grow output buffer OBUF points at
348 so it can hold at least NEEDED more chars. */
350 #define check_expand(OBUF, NEEDED) \
351 (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
352 ? grow_outbuf ((OBUF), (NEEDED)) : 0)
354 struct file_name_list
356 struct file_name_list *next;
357 /* If the following is 1, it is a C-language system include
358 directory. */
359 int c_system_include_path;
360 /* Mapping of file names for this directory. */
361 struct file_name_map *name_map;
362 /* Non-zero if name_map is valid. */
363 int got_name_map;
364 /* The include directory status. */
365 struct stat st;
366 /* The include prefix: "" denotes the working directory,
367 otherwise fname must end in '/'.
368 The actual size is dynamically allocated. */
369 char fname[1];
372 /* #include "file" looks in source file dir, then stack. */
373 /* #include <file> just looks in the stack. */
374 /* -I directories are added to the end, then the defaults are added. */
375 /* The */
376 static struct default_include {
377 const char *fname; /* The name of the directory. */
378 const char *component; /* The component containing the directory */
379 int cplusplus; /* Only look here if we're compiling C++. */
380 int cxx_aware; /* Includes in this directory don't need to
381 be wrapped in extern "C" when compiling
382 C++. */
383 int included; /* Set if the directory is acceptable. */
384 } include_defaults_array[]
385 #ifdef INCLUDE_DEFAULTS
386 = INCLUDE_DEFAULTS;
387 #else
389 /* Pick up GNU C++ specific include files. */
390 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1, 0 },
391 #ifdef CROSS_COMPILE
392 /* This is the dir for fixincludes. Put it just before
393 the files that we fix. */
394 { GCC_INCLUDE_DIR, "GCC", 0, 0, 0 },
395 /* For cross-compilation, this dir name is generated
396 automatically in Makefile.in. */
397 { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0 },
398 #ifdef TOOL_INCLUDE_DIR
399 /* This is another place that the target system's headers might be. */
400 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0, 0 },
401 #endif
402 #else /* not CROSS_COMPILE */
403 #ifdef LOCAL_INCLUDE_DIR
404 /* This should be /usr/local/include and should come before
405 the fixincludes-fixed header files. */
406 { LOCAL_INCLUDE_DIR, 0, 0, 1, 0 },
407 #endif
408 #ifdef TOOL_INCLUDE_DIR
409 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
410 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
411 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0, 0 },
412 #endif
413 /* This is the dir for fixincludes. Put it just before
414 the files that we fix. */
415 { GCC_INCLUDE_DIR, "GCC", 0, 0, 0 },
416 /* Some systems have an extra dir of include files. */
417 #ifdef SYSTEM_INCLUDE_DIR
418 { SYSTEM_INCLUDE_DIR, 0, 0, 0, 0 },
419 #endif
420 #ifndef STANDARD_INCLUDE_COMPONENT
421 #define STANDARD_INCLUDE_COMPONENT 0
422 #endif
423 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0, 0 },
424 #endif /* not CROSS_COMPILE */
425 { 0, 0, 0, 0, 0 }
427 #endif /* no INCLUDE_DEFAULTS */
429 /* The code looks at the defaults through this pointer, rather than through
430 the constant structure above. This pointer gets changed if an environment
431 variable specifies other defaults. */
432 static struct default_include *include_defaults = include_defaults_array;
434 static struct file_name_list *include = 0; /* First dir to search */
435 /* First dir to search for <file> */
436 /* This is the first element to use for #include <...>.
437 If it is 0, use the entire chain for such includes. */
438 static struct file_name_list *first_bracket_include = 0;
439 /* This is the first element in the chain that corresponds to
440 a directory of system header files. */
441 static struct file_name_list *first_system_include = 0;
442 static struct file_name_list *last_include = 0; /* Last in chain */
444 /* Chain of include directories to put at the end of the other chain. */
445 static struct file_name_list *after_include = 0;
446 static struct file_name_list *last_after_include = 0; /* Last in chain */
448 /* Chain to put at the start of the system include files. */
449 static struct file_name_list *before_system = 0;
450 static struct file_name_list *last_before_system = 0; /* Last in chain */
452 /* Directory prefix that should replace `/usr' in the standard
453 include file directories. */
454 static char *include_prefix;
456 /* Maintain and search list of included files. */
458 struct include_file {
459 struct include_file *next; /* for include_hashtab */
460 struct include_file *next_ino; /* for include_ino_hashtab */
461 char *fname;
462 /* If the following is the empty string, it means #pragma once
463 was seen in this include file, or #import was applied to the file.
464 Otherwise, if it is nonzero, it is a macro name.
465 Don't include the file again if that macro is defined. */
466 const U_CHAR *control_macro;
467 /* Nonzero if the dependency on this include file has been output. */
468 int deps_output;
469 struct stat st;
472 /* Hash tables of files already included with #include or #import.
473 include_hashtab is by full name; include_ino_hashtab is by inode number. */
475 #define INCLUDE_HASHSIZE 61
476 static struct include_file *include_hashtab[INCLUDE_HASHSIZE];
477 static struct include_file *include_ino_hashtab[INCLUDE_HASHSIZE];
479 /* Global list of strings read in from precompiled files. This list
480 is kept in the order the strings are read in, with new strings being
481 added at the end through stringlist_tailp. We use this list to output
482 the strings at the end of the run.
484 static STRINGDEF *stringlist;
485 static STRINGDEF **stringlist_tailp = &stringlist;
488 /* Structure returned by create_definition */
489 typedef struct macrodef MACRODEF;
490 struct macrodef
492 struct definition *defn;
493 const U_CHAR *symnam;
494 int symlen;
497 enum sharp_token_type {
498 NO_SHARP_TOKEN = 0, /* token not present */
500 SHARP_TOKEN = '#', /* token spelled with # only */
501 WHITE_SHARP_TOKEN, /* token spelled with # and white space */
503 PERCENT_COLON_TOKEN = '%', /* token spelled with %: only */
504 WHITE_PERCENT_COLON_TOKEN /* token spelled with %: and white space */
507 /* Structure allocated for every #define. For a simple replacement
508 such as
509 #define foo bar ,
510 nargs = -1, the `pattern' list is null, and the expansion is just
511 the replacement text. Nargs = 0 means a functionlike macro with no args,
512 e.g.,
513 #define getchar() getc (stdin) .
514 When there are args, the expansion is the replacement text with the
515 args squashed out, and the reflist is a list describing how to
516 build the output from the input: e.g., "3 chars, then the 1st arg,
517 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
518 The chars here come from the expansion. Whatever is left of the
519 expansion after the last arg-occurrence is copied after that arg.
520 Note that the reflist can be arbitrarily long---
521 its length depends on the number of times the arguments appear in
522 the replacement text, not how many args there are. Example:
523 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
524 pattern list
525 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
526 where (x, y) means (nchars, argno). */
528 typedef struct definition DEFINITION;
529 struct definition {
530 int nargs;
531 int length; /* length of expansion string */
532 int predefined; /* True if the macro was builtin or */
533 /* came from the command line */
534 U_CHAR *expansion;
535 int line; /* Line number of definition */
536 const char *file; /* File of definition */
537 size_t file_len; /* Length of file (which can contain NULs) */
538 char rest_args; /* Nonzero if last arg. absorbs the rest */
539 struct reflist {
540 struct reflist *next;
542 enum sharp_token_type stringify; /* set if a # operator before arg */
543 enum sharp_token_type raw_before; /* set if a ## operator before arg */
544 enum sharp_token_type raw_after; /* set if a ## operator after arg */
546 char rest_args; /* Nonzero if this arg. absorbs the rest */
547 int nchars; /* Number of literal chars to copy before
548 this arg occurrence. */
549 int argno; /* Number of arg to substitute (origin-0) */
550 } *pattern;
551 union {
552 /* Names of macro args, concatenated in reverse order
553 with comma-space between them.
554 The only use of this is that we warn on redefinition
555 if this differs between the old and new definitions. */
556 U_CHAR *argnames;
557 } args;
560 /* different kinds of things that can appear in the value field
561 of a hash node. Actually, this may be useless now. */
562 union hashval {
563 const char *cpval;
564 DEFINITION *defn;
565 KEYDEF *keydef;
569 * special extension string that can be added to the last macro argument to
570 * allow it to absorb the "rest" of the arguments when expanded. Ex:
571 * #define wow(a, b...) process (b, a, b)
572 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
573 * { wow (one, two); } -> { process (two, one, two); }
574 * if this "rest_arg" is used with the concat token '##' and if it is not
575 * supplied then the token attached to with ## will not be outputted. Ex:
576 * #define wow (a, b...) process (b ## , a, ## b)
577 * { wow (1, 2); } -> { process (2, 1, 2); }
578 * { wow (one); } -> { process (one); {
580 static char rest_extension[] = "...";
581 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
583 /* This is the implicit parameter name when using variable number of
584 parameters for macros using the ISO C 99 extension. */
585 static char va_args_name[] = "__VA_ARGS__";
586 #define VA_ARGS_NAME_LENGTH (sizeof (va_args_name) - 1)
588 /* The structure of a node in the hash table. The hash table
589 has entries for all tokens defined by #define directives (type T_MACRO),
590 plus some special tokens like __LINE__ (these each have their own
591 type, and the appropriate code is run when that type of node is seen.
592 It does not contain control words like "#define", which are recognized
593 by a separate piece of code. */
595 /* different flavors of hash nodes --- also used in keyword table */
596 enum node_type {
597 T_DEFINE = 1, /* the `#define' keyword */
598 T_INCLUDE, /* the `#include' keyword */
599 T_INCLUDE_NEXT, /* the `#include_next' keyword */
600 T_IMPORT, /* the `#import' keyword */
601 T_IFDEF, /* the `#ifdef' keyword */
602 T_IFNDEF, /* the `#ifndef' keyword */
603 T_IF, /* the `#if' keyword */
604 T_ELSE, /* `#else' */
605 T_PRAGMA, /* `#pragma' */
606 T_ELIF, /* `#elif' */
607 T_UNDEF, /* `#undef' */
608 T_LINE, /* `#line' */
609 T_ERROR, /* `#error' */
610 T_WARNING, /* `#warning' */
611 T_ENDIF, /* `#endif' */
612 T_SCCS, /* `#sccs', used on system V. */
613 T_IDENT, /* `#ident', used on system V. */
614 T_ASSERT, /* `#assert', taken from system V. */
615 T_UNASSERT, /* `#unassert', taken from system V. */
616 T_SPECLINE, /* special symbol `__LINE__' */
617 T_DATE, /* `__DATE__' */
618 T_FILE, /* `__FILE__' */
619 T_BASE_FILE, /* `__BASE_FILE__' */
620 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
621 T_VERSION, /* `__VERSION__' */
622 T_SIZE_TYPE, /* `__SIZE_TYPE__' */
623 T_PTRDIFF_TYPE, /* `__PTRDIFF_TYPE__' */
624 T_WCHAR_TYPE, /* `__WCHAR_TYPE__' */
625 T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
626 T_REGISTER_PREFIX_TYPE, /* `__REGISTER_PREFIX__' */
627 T_IMMEDIATE_PREFIX_TYPE, /* `__IMMEDIATE_PREFIX__' */
628 T_TIME, /* `__TIME__' */
629 T_CONST, /* Constant value, used by `__STDC__' */
630 T_MACRO, /* macro defined by `#define' */
631 T_DISABLED, /* macro temporarily turned off for rescan */
632 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
633 T_PCSTRING, /* precompiled string (hashval is KEYDEF *) */
634 T_POISON, /* defined with `#pragma poison' */
635 T_UNUSED /* Used for something not defined. */
638 struct hashnode {
639 struct hashnode *next; /* double links for easy deletion */
640 struct hashnode *prev;
641 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
642 chain is kept, in case the node is the head
643 of the chain and gets deleted. */
644 enum node_type type; /* type of special token */
645 int length; /* length of token, for quick comparison */
646 U_CHAR *name; /* the actual name */
647 union hashval value; /* pointer to expansion, or whatever */
650 typedef struct hashnode HASHNODE;
652 /* Some definitions for the hash table. The hash function MUST be
653 computed as shown in hashf () below. That is because the rescan
654 loop computes the hash value `on the fly' for most tokens,
655 in order to avoid the overhead of a lot of procedure calls to
656 the hashf () function. Hashf () only exists for the sake of
657 politeness, for use when speed isn't so important. */
659 #define HASHSIZE 1403
660 static HASHNODE *hashtab[HASHSIZE];
661 #define HASHSTEP(old, c) ((old << 2) + c)
662 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
665 /* We let tm.h override the types used here, to handle trivial differences
666 such as the choice of unsigned int or long unsigned int for size_t.
667 When machines start needing nontrivial differences in the size type,
668 it would be best to do something here to figure out automatically
669 from other information what type to use. */
671 /* The string value for __SIZE_TYPE__. */
673 #ifndef SIZE_TYPE
674 #define SIZE_TYPE "long unsigned int"
675 #endif
677 /* The string value for __PTRDIFF_TYPE__. */
679 #ifndef PTRDIFF_TYPE
680 #define PTRDIFF_TYPE "long int"
681 #endif
683 /* The string value for __WCHAR_TYPE__. */
685 #ifndef WCHAR_TYPE
686 #define WCHAR_TYPE "int"
687 #endif
688 static const char * wchar_type = WCHAR_TYPE;
689 #undef WCHAR_TYPE
691 /* The string value for __USER_LABEL_PREFIX__ */
693 #ifndef USER_LABEL_PREFIX
694 #define USER_LABEL_PREFIX ""
695 #endif
696 static const char * user_label_prefix = USER_LABEL_PREFIX;
697 #undef USER_LABEL_PREFIX
699 /* The string value for __REGISTER_PREFIX__ */
701 #ifndef REGISTER_PREFIX
702 #define REGISTER_PREFIX ""
703 #endif
705 /* The string value for __IMMEDIATE_PREFIX__ */
707 #ifndef IMMEDIATE_PREFIX
708 #define IMMEDIATE_PREFIX ""
709 #endif
711 /* In the definition of a #assert name, this structure forms
712 a list of the individual values asserted.
713 Each value is itself a list of "tokens".
714 These are strings that are compared by name. */
716 struct tokenlist_list {
717 struct tokenlist_list *next;
718 struct arglist *tokens;
721 struct assertion_hashnode {
722 struct assertion_hashnode *next; /* double links for easy deletion */
723 struct assertion_hashnode *prev;
724 /* also, a back pointer to this node's hash
725 chain is kept, in case the node is the head
726 of the chain and gets deleted. */
727 struct assertion_hashnode **bucket_hdr;
728 int length; /* length of token, for quick comparison */
729 U_CHAR *name; /* the actual name */
730 /* List of token-sequences. */
731 struct tokenlist_list *value;
734 typedef struct assertion_hashnode ASSERTION_HASHNODE;
736 /* Some definitions for the hash table. The hash function MUST be
737 computed as shown in hashf below. That is because the rescan
738 loop computes the hash value `on the fly' for most tokens,
739 in order to avoid the overhead of a lot of procedure calls to
740 the hashf function. hashf only exists for the sake of
741 politeness, for use when speed isn't so important. */
743 #define ASSERTION_HASHSIZE 37
744 static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
746 /* Nonzero means inhibit macroexpansion of what seem to be
747 assertion tests, in rescan. For #if. */
748 static int assertions_flag;
750 /* `struct directive' defines one #-directive, including how to handle it. */
752 #define DO_PROTO PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
754 struct directive {
755 int length; /* Length of name */
756 int (*func) DO_PROTO; /* Function to handle directive */
757 const char *name; /* Name of directive */
758 enum node_type type; /* Code which describes which directive. */
761 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
762 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
764 /* These functions are declared to return int instead of void since they
765 are going to be placed in the table and some old compilers have trouble with
766 pointers to functions returning void. */
768 static int do_assert DO_PROTO;
769 static int do_define DO_PROTO;
770 static int do_elif DO_PROTO;
771 static int do_else DO_PROTO;
772 static int do_endif DO_PROTO;
773 static int do_error DO_PROTO;
774 static int do_ident DO_PROTO;
775 static int do_if DO_PROTO;
776 static int do_include DO_PROTO;
777 static int do_line DO_PROTO;
778 static int do_pragma DO_PROTO;
779 #ifdef SCCS_DIRECTIVE
780 static int do_sccs DO_PROTO;
781 #endif
782 static int do_unassert DO_PROTO;
783 static int do_undef DO_PROTO;
784 static int do_xifdef DO_PROTO;
786 /* Here is the actual list of #-directives, most-often-used first. */
788 static struct directive directive_table[] = {
789 { 6, do_define, "define", T_DEFINE},
790 { 2, do_if, "if", T_IF},
791 { 5, do_xifdef, "ifdef", T_IFDEF},
792 { 6, do_xifdef, "ifndef", T_IFNDEF},
793 { 5, do_endif, "endif", T_ENDIF},
794 { 4, do_else, "else", T_ELSE},
795 { 4, do_elif, "elif", T_ELIF},
796 { 4, do_line, "line", T_LINE},
797 { 7, do_include, "include", T_INCLUDE},
798 { 12, do_include, "include_next", T_INCLUDE_NEXT},
799 { 6, do_include, "import", T_IMPORT},
800 { 5, do_undef, "undef", T_UNDEF},
801 { 5, do_error, "error", T_ERROR},
802 { 7, do_error, "warning", T_WARNING},
803 #ifdef SCCS_DIRECTIVE
804 { 4, do_sccs, "sccs", T_SCCS},
805 #endif
806 { 6, do_pragma, "pragma", T_PRAGMA},
807 { 5, do_ident, "ident", T_IDENT},
808 { 6, do_assert, "assert", T_ASSERT},
809 { 8, do_unassert, "unassert", T_UNASSERT},
810 { -1, 0, "", T_UNUSED},
813 /* When a directive handler is called,
814 this points to the # (or the : of the %:) that started the directive. */
815 U_CHAR *directive_start;
817 /* table to tell if char can be part of a C identifier. */
818 U_CHAR is_idchar[256];
819 /* table to tell if char can be first char of a c identifier. */
820 U_CHAR is_idstart[256];
821 /* table to tell if c is horizontal space. */
822 static U_CHAR is_hor_space[256];
823 /* table to tell if c is horizontal or vertical space. */
824 U_CHAR is_space[256];
826 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
827 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
829 static int errors = 0; /* Error counter for exit code */
831 /* Name of output file, for error messages. */
832 static const char *out_fname;
834 /* Nonzero to ignore \ in string constants. Use to treat #line 1 "A:\file.h
835 as a non-form feed. If you want it to be a form feed, you must use
836 # 1 "\f". */
837 static int ignore_escape_flag = 1;
839 /* Stack of conditionals currently in progress
840 (including both successful and failing conditionals). */
842 struct if_stack {
843 struct if_stack *next; /* for chaining to the next stack frame */
844 const char *fname; /* copied from input when frame is made */
845 size_t fname_len; /* similarly */
846 int lineno; /* similarly */
847 int if_succeeded; /* true if a leg of this if-group
848 has been passed through rescan */
849 const U_CHAR *control_macro; /* For #ifndef at start of file,
850 this is the macro name tested. */
851 enum node_type type; /* type of last directive seen in this group */
853 typedef struct if_stack IF_STACK_FRAME;
854 static IF_STACK_FRAME *if_stack = NULL;
856 /* Buffer of -M output. */
857 static char *deps_buffer;
859 /* Number of bytes allocated in above. */
860 static int deps_allocated_size;
862 /* Number of bytes used. */
863 static int deps_size;
865 /* Number of bytes since the last newline. */
866 static int deps_column;
868 /* Nonzero means -I- has been seen,
869 so don't look for #include "foo" the source-file directory. */
870 static int ignore_srcdir;
872 static int safe_read PARAMS ((int, char *, int));
873 static void safe_write PARAMS ((int, const char *, int));
875 int main PARAMS ((int, char **));
877 static void path_include PARAMS ((char *));
879 static const U_CHAR *index0 PARAMS ((const U_CHAR *, int, size_t));
881 static void trigraph_pcp PARAMS ((FILE_BUF *));
882 static void check_white_space PARAMS ((FILE_BUF *));
884 static void newline_fix PARAMS ((U_CHAR *));
885 static void name_newline_fix PARAMS ((U_CHAR *));
887 static const char *get_lintcmd PARAMS ((const U_CHAR *, const U_CHAR *,
888 const U_CHAR **, int *, int *));
890 static void rescan PARAMS ((FILE_BUF *, int));
892 static FILE_BUF expand_to_temp_buffer PARAMS ((const U_CHAR *, const U_CHAR *,
893 int, int));
895 static int handle_directive PARAMS ((FILE_BUF *, FILE_BUF *));
897 static struct tm *timestamp PARAMS ((void));
898 static void special_symbol PARAMS ((HASHNODE *, FILE_BUF *));
900 static int is_system_include PARAMS ((const char *));
901 static char *base_name PARAMS ((const char *));
902 static int absolute_filename PARAMS ((const char *));
903 static size_t simplify_filename PARAMS ((char *));
905 static char *read_filename_string PARAMS ((int, FILE *));
906 static struct file_name_map *read_name_map PARAMS ((const char *));
907 static int open_include_file PARAMS ((char *, struct file_name_list *,
908 const U_CHAR *, struct include_file **));
909 static char *remap_include_file PARAMS ((char *, struct file_name_list *));
910 static int lookup_ino_include PARAMS ((struct include_file *));
912 static void finclude PARAMS ((int, struct include_file *, FILE_BUF *, int,
913 struct file_name_list *));
914 static void record_control_macro PARAMS ((struct include_file *,
915 const U_CHAR *));
917 static char *check_precompiled PARAMS ((int, struct stat *, const char *,
918 const char **));
919 static int check_preconditions PARAMS ((const char *));
920 static void pcfinclude PARAMS ((U_CHAR *, const U_CHAR *, FILE_BUF *));
921 static void pcstring_used PARAMS ((HASHNODE *));
922 static void write_output PARAMS ((void));
923 static void pass_thru_directive PARAMS ((const U_CHAR *, const U_CHAR *,
924 FILE_BUF *, struct directive *));
926 static MACRODEF create_definition PARAMS ((const U_CHAR *, const U_CHAR *,
927 FILE_BUF *));
929 static int check_macro_name PARAMS ((const U_CHAR *, int));
930 static int compare_defs PARAMS ((DEFINITION *, DEFINITION *));
931 static int comp_def_part PARAMS ((int, const U_CHAR *, int, const U_CHAR *,
932 int, int));
934 static DEFINITION *collect_expansion PARAMS ((const U_CHAR *, const U_CHAR *,
935 int, struct arglist *));
937 int check_assertion PARAMS ((const U_CHAR *, int, int, struct arglist *));
938 static int compare_token_lists PARAMS ((struct arglist *, struct arglist *));
940 static struct arglist *read_token_list PARAMS ((const U_CHAR **,
941 const U_CHAR *, int *));
942 static void free_token_list PARAMS ((struct arglist *));
944 static ASSERTION_HASHNODE *assertion_install PARAMS ((const U_CHAR *, int, int));
945 static ASSERTION_HASHNODE *assertion_lookup PARAMS ((const U_CHAR *, int, int));
946 static void delete_assertion PARAMS ((ASSERTION_HASHNODE *));
948 static void do_once PARAMS ((void));
950 static HOST_WIDEST_INT eval_if_expression PARAMS ((const U_CHAR *, int));
951 static void conditional_skip PARAMS ((FILE_BUF *, int, enum node_type,
952 const U_CHAR *, FILE_BUF *));
953 static void skip_if_group PARAMS ((FILE_BUF *, int, FILE_BUF *));
954 static void validate_else PARAMS ((const U_CHAR *, const U_CHAR *));
956 static U_CHAR *skip_to_end_of_comment PARAMS ((FILE_BUF *, int *, int));
957 static U_CHAR *skip_quoted_string PARAMS ((const U_CHAR *, const U_CHAR *,
958 int, int *, int *, int *));
959 static char *quote_string PARAMS ((char *, const char *, size_t));
960 static U_CHAR *skip_paren_group PARAMS ((FILE_BUF *));
962 /* Last arg to output_line_directive. */
963 enum file_change_code {same_file, enter_file, leave_file};
964 static void output_line_directive PARAMS ((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
966 static void macroexpand PARAMS ((HASHNODE *, FILE_BUF *));
968 struct argdata;
969 static int macarg PARAMS ((struct argdata *, int));
971 static U_CHAR *macarg1 PARAMS ((U_CHAR *, const U_CHAR *, struct hashnode *, int *, int *, int *, int));
973 static int discard_comments PARAMS ((U_CHAR *, int, int));
975 static void change_newlines PARAMS ((struct argdata *));
977 static void notice PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
978 static void vnotice PARAMS ((const char *, va_list));
979 void error PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
980 void verror PARAMS ((const char *, va_list));
981 static void error_from_errno PARAMS ((const char *));
982 void warning PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
983 static void vwarning PARAMS ((const char *, va_list));
984 static void error_with_line PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2;
985 static void verror_with_line PARAMS ((int, const char *, va_list));
986 static void vwarning_with_line PARAMS ((int, const char *, va_list));
987 static void warning_with_line PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2;
988 void pedwarn PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
989 void pedwarn_with_line PARAMS ((int, const char *, ...)) ATTRIBUTE_PRINTF_2;
990 static void pedwarn_with_file_and_line PARAMS ((const char *, size_t, int, const char *, ...)) ATTRIBUTE_PRINTF_4;
991 static void pedwarn_strange_white_space PARAMS ((int));
993 static void print_containing_files PARAMS ((void));
995 static int line_for_error PARAMS ((int));
996 static int grow_outbuf PARAMS ((FILE_BUF *, int));
998 static HASHNODE *install PARAMS ((const U_CHAR *, int, enum node_type,
999 const char *, int));
1000 HASHNODE *lookup PARAMS ((const U_CHAR *, int, int));
1001 static void delete_macro PARAMS ((HASHNODE *));
1002 static int hashf PARAMS ((const U_CHAR *, int, int));
1004 static void dump_single_macro PARAMS ((HASHNODE *, FILE *));
1005 static void dump_all_macros PARAMS ((void));
1006 static void dump_defn_1 PARAMS ((const U_CHAR *, int, int, FILE *));
1007 static void dump_arg_n PARAMS ((DEFINITION *, int, FILE *));
1009 static void initialize_char_syntax PARAMS ((void));
1010 static void initialize_builtins PARAMS ((FILE_BUF *, FILE_BUF *));
1012 static void make_definition PARAMS ((char *));
1013 static void make_undef PARAMS ((char *, FILE_BUF *));
1015 static void make_assertion PARAMS ((const char *, const char *));
1017 static struct file_name_list *new_include_prefix PARAMS ((struct file_name_list *, const char *, const char *, const char *));
1018 static void append_include_chain PARAMS ((struct file_name_list *, struct file_name_list *));
1020 static int quote_string_for_make PARAMS ((char *, const char *));
1021 static void deps_output PARAMS ((const char *, int));
1023 void fatal PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
1024 void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
1025 static void perror_with_name PARAMS ((const char *));
1026 static void pfatal_with_name PARAMS ((const char *)) ATTRIBUTE_NORETURN;
1027 static void pipe_closed PARAMS ((int)) ATTRIBUTE_NORETURN;
1029 static void memory_full PARAMS ((void)) ATTRIBUTE_NORETURN;
1030 static void print_help PARAMS ((void));
1032 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
1033 retrying if necessary. If MAX_READ_LEN is defined, read at most
1034 that bytes at a time. Return a negative value if an error occurs,
1035 otherwise return the actual number of bytes read,
1036 which must be LEN unless end-of-file was reached. */
1038 static int
1039 safe_read (desc, ptr, len)
1040 int desc;
1041 char *ptr;
1042 int len;
1044 int left, rcount, nchars;
1046 left = len;
1047 while (left > 0) {
1048 rcount = left;
1049 #ifdef MAX_READ_LEN
1050 if (rcount > MAX_READ_LEN)
1051 rcount = MAX_READ_LEN;
1052 #endif
1053 nchars = read (desc, ptr, rcount);
1054 if (nchars < 0)
1056 #ifdef EINTR
1057 if (errno == EINTR)
1058 continue;
1059 #endif
1060 return nchars;
1062 if (nchars == 0)
1063 break;
1064 ptr += nchars;
1065 left -= nchars;
1067 return len - left;
1070 /* Write LEN bytes at PTR to descriptor DESC,
1071 retrying if necessary, and treating any real error as fatal.
1072 If MAX_WRITE_LEN is defined, write at most that many bytes at a time. */
1074 static void
1075 safe_write (desc, ptr, len)
1076 int desc;
1077 const char *ptr;
1078 int len;
1080 int wcount, written;
1082 while (len > 0) {
1083 wcount = len;
1084 #ifdef MAX_WRITE_LEN
1085 if (wcount > MAX_WRITE_LEN)
1086 wcount = MAX_WRITE_LEN;
1087 #endif
1088 written = write (desc, ptr, wcount);
1089 if (written < 0)
1091 #ifdef EINTR
1092 if (errno == EINTR)
1093 continue;
1094 #endif
1095 pfatal_with_name (out_fname);
1097 ptr += written;
1098 len -= written;
1103 static void
1104 print_help ()
1106 printf ("Usage: %s [switches] input output\n", progname);
1107 printf ("Switches:\n");
1108 printf (" -include <file> Include the contents of <file> before other files\n");
1109 printf (" -imacros <file> Accept definition of macros in <file>\n");
1110 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
1111 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
1112 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
1113 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
1114 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
1115 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
1116 printf (" -nostdinc Do not search the system include directories\n");
1117 printf (" -nostdinc++ Do not search the system include directories for C++\n");
1118 printf (" -o <file> Put output into <file>\n");
1119 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
1120 printf (" -traditional Follow K&R pre-processor behaviour\n");
1121 printf (" -trigraphs Support ANSI C trigraphs\n");
1122 printf (" -lang-c Assume that the input sources are in C\n");
1123 printf (" -lang-c89 Assume that the input is C89; depricated\n");
1124 printf (" -lang-c++ Assume that the input sources are in C++\n");
1125 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
1126 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
1127 printf (" -lang-asm Assume that the input sources are in assembler\n");
1128 printf (" -lang-fortran Assume that the input sources are in Fortran\n");
1129 printf (" -lang-chill Assume that the input sources are in Chill\n");
1130 printf (" -std=<std name> Specify the conformance standard; one of:\n");
1131 printf (" gnu89, gnu99, c89, c99, iso9899:1990,\n");
1132 printf (" iso9899:199409, iso9899:1999\n");
1133 printf (" -+ Allow parsing of C++ style features\n");
1134 printf (" -w Inhibit warning messages\n");
1135 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
1136 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
1137 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
1138 printf (" -Wno-comment{s} Do not warn about comments\n");
1139 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
1140 printf (" a string if -traditional is specified\n");
1141 printf (" -Wno-traditional Do not warn about stringification\n");
1142 printf (" -Wundef Warn if an undefined macro is used by #if\n");
1143 printf (" -Wno-undef Do not warn about testing undefined macros\n");
1144 printf (" -Wimport Warn about the use of the #import directive\n");
1145 printf (" -Wno-import Do not warn about the use of #import\n");
1146 printf (" -Werror Treat all warnings as errors\n");
1147 printf (" -Wno-error Do not treat warnings as errors\n");
1148 printf (" -Wall Enable all preprocessor warnings\n");
1149 printf (" -M Generate make dependencies\n");
1150 printf (" -MM As -M, but ignore system header files\n");
1151 printf (" -MD As -M, but put output in a .d file\n");
1152 printf (" -MMD As -MD, but ignore system header files\n");
1153 printf (" -MG Treat missing header file as generated files\n");
1154 printf (" -g Include #define and #undef directives in the output\n");
1155 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
1156 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
1157 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
1158 printf (" -U<macro> Undefine <macro> \n");
1159 printf (" -u or -undef Do not predefine any macros\n");
1160 printf (" -v Display the version number\n");
1161 printf (" -H Print the name of header files as they are used\n");
1162 printf (" -C Do not discard comments\n");
1163 printf (" -dM Display a list of macro definitions active at end\n");
1164 printf (" -dD Preserve macro definitions in output\n");
1165 printf (" -dN As -dD except that only the names are preserved\n");
1166 printf (" -dI Include #include directives in the output\n");
1167 printf (" -ifoutput Describe skipped code blocks in output \n");
1168 printf (" -P Do not generate #line directives\n");
1169 printf (" -$ Do not include '$' in identifiers\n");
1170 printf (" -remap Remap file names when including files.\n");
1171 printf (" -h or --help Display this information\n");
1175 main (argc, argv)
1176 int argc;
1177 char **argv;
1179 struct stat st;
1180 const char *in_fname;
1181 char *cp;
1182 int f, i;
1183 FILE_BUF *fp;
1185 char **pend_files;
1186 char **pend_defs;
1187 char **pend_undefs;
1188 char **pend_assertions;
1189 char **pend_includes;
1191 /* Record the option used with each element of pend_assertions.
1192 This is preparation for supporting more than one option for making
1193 an assertion. */
1194 const char **pend_assertion_options;
1195 int no_standard_includes = 0;
1196 int no_standard_cplusplus_includes = 0;
1197 int missing_newline = 0;
1199 /* Non-0 means don't output the preprocessed program. */
1200 int inhibit_output = 0;
1201 /* Non-0 means -v, so print the full set of include dirs. */
1202 int verbose = 0;
1204 /* File name which deps are being written to.
1205 This is 0 if deps are being written to stdout. */
1206 char *deps_file = 0;
1207 /* Fopen file mode to open deps_file with. */
1208 const char *deps_mode = "a";
1209 /* Stream on which to print the dependency information. */
1210 FILE *deps_stream = 0;
1211 /* Target-name to write with the dependency information. */
1212 char *deps_target = 0;
1214 #if defined (RLIMIT_STACK) && defined (HAVE_GETRLIMIT) && defined (HAVE_SETRLIMIT)
1215 /* Get rid of any avoidable limit on stack size. */
1217 struct rlimit rlim;
1219 /* Set the stack limit huge so that alloca (particularly stringtab
1220 in dbxread.c) does not fail. */
1221 getrlimit (RLIMIT_STACK, &rlim);
1222 rlim.rlim_cur = rlim.rlim_max;
1223 setrlimit (RLIMIT_STACK, &rlim);
1225 #endif
1227 #ifdef SIGPIPE
1228 signal (SIGPIPE, pipe_closed);
1229 #endif
1231 #ifdef HAVE_LC_MESSAGES
1232 setlocale (LC_MESSAGES, "");
1233 #endif
1234 (void) bindtextdomain (PACKAGE, localedir);
1235 (void) textdomain (PACKAGE);
1237 progname = base_name (argv[0]);
1239 #ifdef VMS
1241 /* Remove extension from PROGNAME. */
1242 char *p;
1243 char *s = xstrdup (progname);
1244 progname = s;
1246 if ((p = rindex (s, ';')) != 0) *p = '\0'; /* strip version number */
1247 if ((p = rindex (s, '.')) != 0 /* strip type iff ".exe" */
1248 && (p[1] == 'e' || p[1] == 'E')
1249 && (p[2] == 'x' || p[2] == 'X')
1250 && (p[3] == 'e' || p[3] == 'E')
1251 && !p[4])
1252 *p = '\0';
1254 #endif
1256 /* Do not invoke xmalloc before this point, since locale and
1257 progname need to be set first, in case a diagnostic is issued. */
1259 pend_files = (char **) xmalloc (argc * sizeof (char *));
1260 pend_defs = (char **) xmalloc ((2 * argc) * sizeof (char *));
1261 pend_undefs = (char **) xmalloc (argc * sizeof (char *));
1262 pend_assertions = (char **) xmalloc (argc * sizeof (char *));
1263 pend_includes = (char **) xmalloc (argc * sizeof (char *));
1264 pend_assertion_options = (const char **) xmalloc (argc * sizeof (char *));
1266 in_fname = NULL;
1267 out_fname = NULL;
1269 /* Initialize is_idchar. */
1270 initialize_char_syntax ();
1272 no_line_directives = 0;
1273 no_trigraphs = 1;
1274 dump_macros = dump_none;
1275 no_output = 0;
1276 cplusplus = 0;
1277 cplusplus_comments = 1;
1279 bzero ((char *) pend_files, argc * sizeof (char *));
1280 bzero ((char *) pend_defs, (2 * argc) * sizeof (char *));
1281 bzero ((char *) pend_undefs, argc * sizeof (char *));
1282 bzero ((char *) pend_assertions, argc * sizeof (char *));
1283 bzero ((char *) pend_includes, argc * sizeof (char *));
1285 #ifdef MULTIBYTE_CHARS
1286 /* Change to the native locale for multibyte conversions. */
1287 setlocale (LC_CTYPE, "");
1288 literal_codeset = getenv ("LANG");
1289 #endif
1291 /* Process switches and find input file name. */
1293 for (i = 1; i < argc; i++) {
1294 if (argv[i][0] != '-') {
1295 if (out_fname != NULL)
1297 print_help ();
1298 fatal ("Too many arguments");
1300 else if (in_fname != NULL)
1301 out_fname = argv[i];
1302 else
1303 in_fname = argv[i];
1304 } else {
1305 switch (argv[i][1]) {
1307 case 'i':
1308 if (!strcmp (argv[i], "-include")) {
1309 if (i + 1 == argc)
1310 fatal ("Filename missing after `-include' option");
1311 else {
1312 i++;
1313 simplify_filename (pend_includes[i] = argv[i]);
1316 if (!strcmp (argv[i], "-imacros")) {
1317 if (i + 1 == argc)
1318 fatal ("Filename missing after `-imacros' option");
1319 else {
1320 i++;
1321 simplify_filename (pend_files[i] = argv[i]);
1324 if (!strcmp (argv[i], "-iprefix")) {
1325 if (i + 1 == argc)
1326 fatal ("Filename missing after `-iprefix' option");
1327 else
1328 include_prefix = argv[++i];
1330 if (!strcmp (argv[i], "-ifoutput")) {
1331 output_conditionals = 1;
1333 if (!strcmp (argv[i], "-isystem")) {
1334 struct file_name_list *dirtmp;
1336 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1337 "", argv[++i])))
1338 break;
1339 dirtmp->c_system_include_path = 1;
1341 if (before_system == 0)
1342 before_system = dirtmp;
1343 else
1344 last_before_system->next = dirtmp;
1345 last_before_system = dirtmp; /* Tail follows the last one */
1347 /* Add directory to end of path for includes,
1348 with the default prefix at the front of its name. */
1349 if (!strcmp (argv[i], "-iwithprefix")) {
1350 struct file_name_list *dirtmp;
1351 char *prefix;
1353 if (include_prefix != 0)
1354 prefix = include_prefix;
1355 else {
1356 prefix = xstrdup (GCC_INCLUDE_DIR);
1357 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1358 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1359 prefix[strlen (prefix) - 7] = 0;
1362 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1363 prefix, argv[++i])))
1364 break;
1366 if (after_include == 0)
1367 after_include = dirtmp;
1368 else
1369 last_after_include->next = dirtmp;
1370 last_after_include = dirtmp; /* Tail follows the last one */
1372 /* Add directory to main path for includes,
1373 with the default prefix at the front of its name. */
1374 if (!strcmp (argv[i], "-iwithprefixbefore")) {
1375 struct file_name_list *dirtmp;
1376 char *prefix;
1378 if (include_prefix != 0)
1379 prefix = include_prefix;
1380 else {
1381 prefix = xstrdup (GCC_INCLUDE_DIR);
1382 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1383 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
1384 prefix[strlen (prefix) - 7] = 0;
1387 dirtmp = new_include_prefix (NULL_PTR, NULL_PTR, prefix, argv[++i]);
1388 append_include_chain (dirtmp, dirtmp);
1390 /* Add directory to end of path for includes. */
1391 if (!strcmp (argv[i], "-idirafter")) {
1392 struct file_name_list *dirtmp;
1394 if (! (dirtmp = new_include_prefix (NULL_PTR, NULL_PTR,
1395 "", argv[++i])))
1396 break;
1398 if (after_include == 0)
1399 after_include = dirtmp;
1400 else
1401 last_after_include->next = dirtmp;
1402 last_after_include = dirtmp; /* Tail follows the last one */
1404 break;
1406 case 'o':
1407 if (out_fname != NULL)
1408 fatal ("Output filename specified twice");
1409 if (i + 1 == argc)
1410 fatal ("Filename missing after -o option");
1411 out_fname = argv[++i];
1412 if (!strcmp (out_fname, "-"))
1413 out_fname = "";
1414 break;
1416 case 'p':
1417 if (!strcmp (argv[i], "-pedantic"))
1418 pedantic = 1;
1419 else if (!strcmp (argv[i], "-pedantic-errors")) {
1420 pedantic = 1;
1421 pedantic_errors = 1;
1422 } else if (!strcmp (argv[i], "-pcp")) {
1423 char *pcp_fname;
1424 if (i + 1 == argc)
1425 fatal ("Filename missing after -pcp option");
1426 pcp_fname = argv[++i];
1427 pcp_outfile
1428 = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1429 ? fopen (pcp_fname, "w")
1430 : stdout);
1431 if (pcp_outfile == 0)
1432 pfatal_with_name (pcp_fname);
1433 no_precomp = 1;
1435 break;
1437 case 't':
1438 if (!strcmp (argv[i], "-traditional")) {
1439 traditional = 1;
1440 cplusplus_comments = 0;
1441 } else if (!strcmp (argv[i], "-trigraphs")) {
1442 no_trigraphs = 0;
1444 break;
1446 case 'l':
1447 if (! strcmp (argv[i], "-lang-c"))
1448 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 1, objc = 0;
1449 else if (! strcmp (argv[i], "-lang-c89"))
1451 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1452 no_trigraphs = 0;
1453 pend_defs[2*i] = "__STRICT_ANSI__";
1455 else if (! strcmp (argv[i], "-lang-c++"))
1456 cplusplus = 1, cplusplus_comments = 1, c89 = 0, c99 = 0, objc = 0;
1457 else if (! strcmp (argv[i], "-lang-objc"))
1458 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 0, objc = 1;
1459 else if (! strcmp (argv[i], "-lang-objc++"))
1460 cplusplus = 1, cplusplus_comments = 1, c89 = 0, c99 = 0, objc = 1;
1461 else if (! strcmp (argv[i], "-lang-asm"))
1462 lang_asm = 1;
1463 else if (! strcmp (argv[i], "-lang-fortran"))
1464 /* Doesn't actually do anything. */ ;
1465 else if (! strcmp (argv[i], "-lint"))
1466 for_lint = 1;
1467 break;
1469 case '+':
1470 cplusplus = 1, cplusplus_comments = 1;
1471 break;
1473 case 's':
1474 if (!strcmp (argv[i], "-std=gnu89"))
1476 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1478 else if (!strcmp (argv[i], "-std=gnu9x")
1479 || !strcmp (argv[i], "-std=gnu99"))
1481 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 1, objc = 0;
1482 pend_defs[2*i+1] = "__STDC_VERSION__=199901L";
1484 else if (!strcmp (argv[i], "-std=iso9899:1990")
1485 || !strcmp (argv[i], "-std=c89"))
1487 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1488 no_trigraphs = 0;
1489 pend_defs[2*i] = "__STRICT_ANSI__";
1491 else if (!strcmp (argv[i], "-std=iso9899:199409"))
1493 cplusplus = 0, cplusplus_comments = 0, c89 = 1, c99 = 0, objc = 0;
1494 no_trigraphs = 0;
1495 pend_defs[2*i] = "__STRICT_ANSI__";
1496 pend_defs[2*i+1] = "__STDC_VERSION__=199409L";
1498 else if (!strcmp (argv[i], "-std=iso9899:199x")
1499 || !strcmp (argv[i], "-std=iso9899:1999")
1500 || !strcmp (argv[i], "-std=c9x")
1501 || !strcmp (argv[i], "-std=c99"))
1503 cplusplus = 0, cplusplus_comments = 1, c89 = 0, c99 = 1, objc = 0;
1504 no_trigraphs = 0;
1505 pend_defs[2*i] = "__STRICT_ANSI__";
1506 pend_defs[2*i+1] = "__STDC_VERSION__=199901L";
1508 break;
1510 case 'w':
1511 inhibit_warnings = 1;
1512 break;
1514 case 'W':
1515 if (!strcmp (argv[i], "-Wtrigraphs"))
1516 warn_trigraphs = 1;
1517 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1518 warn_trigraphs = 0;
1519 else if (!strcmp (argv[i], "-Wcomment"))
1520 warn_comments = 1;
1521 else if (!strcmp (argv[i], "-Wno-comment"))
1522 warn_comments = 0;
1523 else if (!strcmp (argv[i], "-Wcomments"))
1524 warn_comments = 1;
1525 else if (!strcmp (argv[i], "-Wno-comments"))
1526 warn_comments = 0;
1527 else if (!strcmp (argv[i], "-Wtraditional"))
1528 warn_stringify = 1;
1529 else if (!strcmp (argv[i], "-Wno-traditional"))
1530 warn_stringify = 0;
1531 else if (!strcmp (argv[i], "-Wwhite-space"))
1532 warn_white_space = 1;
1533 else if (!strcmp (argv[i], "-Wno-white-space"))
1534 warn_white_space = 0;
1535 else if (!strcmp (argv[i], "-Wundef"))
1536 warn_undef = 1;
1537 else if (!strcmp (argv[i], "-Wno-undef"))
1538 warn_undef = 0;
1539 else if (!strcmp (argv[i], "-Wimport"))
1540 warn_import = 1;
1541 else if (!strcmp (argv[i], "-Wno-import"))
1542 warn_import = 0;
1543 else if (!strcmp (argv[i], "-Werror"))
1544 warnings_are_errors = 1;
1545 else if (!strcmp (argv[i], "-Wno-error"))
1546 warnings_are_errors = 0;
1547 else if (!strcmp (argv[i], "-Wall"))
1549 warn_trigraphs = 1;
1550 warn_comments = 1;
1551 warn_white_space = 1;
1553 break;
1555 case 'f':
1556 if (!strcmp (argv[i], "-fleading-underscore"))
1557 user_label_prefix = "_";
1558 else if (!strcmp (argv[i], "-fno-leading-underscore"))
1559 user_label_prefix = "";
1560 break;
1562 case 'M':
1563 /* The style of the choices here is a bit mixed.
1564 The chosen scheme is a hybrid of keeping all options in one string
1565 and specifying each option in a separate argument:
1566 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1567 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1568 -M[M][G][D file]. This is awkward to handle in specs, and is not
1569 as extensible. */
1570 /* ??? -MG must be specified in addition to one of -M or -MM.
1571 This can be relaxed in the future without breaking anything.
1572 The converse isn't true. */
1574 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1575 if (!strcmp (argv[i], "-MG"))
1577 print_deps_missing_files = 1;
1578 break;
1580 if (!strcmp (argv[i], "-M"))
1581 print_deps = 2;
1582 else if (!strcmp (argv[i], "-MM"))
1583 print_deps = 1;
1584 else if (!strcmp (argv[i], "-MD"))
1585 print_deps = 2;
1586 else if (!strcmp (argv[i], "-MMD"))
1587 print_deps = 1;
1588 /* For -MD and -MMD options, write deps on file named by next arg. */
1589 if (!strcmp (argv[i], "-MD")
1590 || !strcmp (argv[i], "-MMD")) {
1591 if (i + 1 == argc)
1592 fatal ("Filename missing after %s option", argv[i]);
1593 i++;
1594 deps_file = argv[i];
1595 deps_mode = "w";
1596 } else {
1597 /* For -M and -MM, write deps on standard output
1598 and suppress the usual output. */
1599 deps_stream = stdout;
1600 inhibit_output = 1;
1602 break;
1604 case 'd':
1606 char *p = argv[i] + 2;
1607 char c;
1608 while ((c = *p++)) {
1609 /* Arg to -d specifies what parts of macros to dump */
1610 switch (c) {
1611 case 'M':
1612 dump_macros = dump_only;
1613 no_output = 1;
1614 break;
1615 case 'N':
1616 dump_macros = dump_names;
1617 break;
1618 case 'D':
1619 dump_macros = dump_definitions;
1620 break;
1621 case 'I':
1622 dump_includes = 1;
1623 break;
1627 break;
1629 case 'g':
1630 if (argv[i][2] == '3')
1631 debug_output = 1;
1632 break;
1634 case '-':
1635 if (strcmp (argv[i], "--help") != 0)
1636 return i;
1637 print_help ();
1638 exit (0);
1639 break;
1641 case 'v':
1642 notice ("GNU CPP version %s", version_string);
1643 #ifdef TARGET_VERSION
1644 TARGET_VERSION;
1645 #endif
1646 fprintf (stderr, "\n");
1647 verbose = 1;
1648 break;
1650 case 'H':
1651 print_include_names = 1;
1652 break;
1654 case 'D':
1655 if (argv[i][2] != 0)
1656 pend_defs[2*i] = argv[i] + 2;
1657 else if (i + 1 == argc)
1658 fatal ("Macro name missing after -D option");
1659 else
1660 i++, pend_defs[2*i] = argv[i];
1661 break;
1663 case 'A':
1665 char *p;
1667 if (argv[i][2] != 0)
1668 p = argv[i] + 2;
1669 else if (i + 1 == argc)
1670 fatal ("Assertion missing after -A option");
1671 else
1672 p = argv[++i];
1674 if (!strcmp (p, "-")) {
1675 /* -A- eliminates all predefined macros and assertions.
1676 Let's include also any that were specified earlier
1677 on the command line. That way we can get rid of any
1678 that were passed automatically in from GCC. */
1679 int j;
1680 for (j = 0; j < i; j++)
1681 pend_defs[2*j] = pend_assertions[j] = 0;
1682 } else {
1683 pend_assertions[i] = p;
1684 pend_assertion_options[i] = "-A";
1687 break;
1689 case 'U': /* JF #undef something */
1690 if (argv[i][2] != 0)
1691 pend_undefs[i] = argv[i] + 2;
1692 else if (i + 1 == argc)
1693 fatal ("Macro name missing after -U option");
1694 else
1695 pend_undefs[i] = argv[i+1], i++;
1696 break;
1698 case 'C':
1699 put_out_comments = 1;
1700 break;
1702 case 'E': /* -E comes from cc -E; ignore it. */
1703 break;
1705 case 'P':
1706 no_line_directives = 1;
1707 break;
1709 case '$': /* Don't include $ in identifiers. */
1710 is_idchar['$'] = is_idstart['$'] = 0;
1711 break;
1713 case 'I': /* Add directory to path for includes. */
1715 struct file_name_list *dirtmp;
1716 char *dir = argv[i][2] ? argv[i] + 2 : argv[++i];
1718 if (! ignore_srcdir && !strcmp (dir, "-")) {
1719 ignore_srcdir = 1;
1720 /* Don't use any preceding -I directories for #include <...>. */
1721 first_bracket_include = 0;
1723 else {
1724 dirtmp = new_include_prefix (last_include, NULL_PTR, "", dir);
1725 append_include_chain (dirtmp, dirtmp);
1728 break;
1730 case 'n':
1731 if (!strcmp (argv[i], "-nostdinc"))
1732 /* -nostdinc causes no default include directories.
1733 You must specify all include-file directories with -I. */
1734 no_standard_includes = 1;
1735 else if (!strcmp (argv[i], "-nostdinc++"))
1736 /* -nostdinc++ causes no default C++-specific include directories. */
1737 no_standard_cplusplus_includes = 1;
1738 else if (!strcmp (argv[i], "-noprecomp"))
1739 no_precomp = 1;
1740 break;
1742 case 'r':
1743 if (!strcmp (argv[i], "-remap"))
1744 remap = 1;
1745 break;
1747 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1748 if (in_fname == NULL) {
1749 in_fname = "";
1750 break;
1751 } else if (out_fname == NULL) {
1752 out_fname = "";
1753 break;
1754 } /* else fall through into error */
1756 default:
1757 fatal ("Invalid option `%s'", argv[i]);
1762 /* Add dirs from CPATH after dirs from -I. */
1763 /* There seems to be confusion about what CPATH should do,
1764 so for the moment it is not documented. */
1765 /* Some people say that CPATH should replace the standard include dirs,
1766 but that seems pointless: it comes before them, so it overrides them
1767 anyway. */
1768 GET_ENV_PATH_LIST (cp, "CPATH");
1769 if (cp && ! no_standard_includes)
1770 path_include (cp);
1772 /* Initialize output buffer */
1774 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
1775 outbuf.bufp = outbuf.buf;
1776 outbuf.length = OUTBUF_SIZE;
1778 /* Do partial setup of input buffer for the sake of generating
1779 early #line directives (when -g is in effect). */
1781 fp = &instack[++indepth];
1782 if (in_fname == NULL)
1783 in_fname = "";
1784 fp->nominal_fname = fp->fname = in_fname;
1785 fp->nominal_fname_len = strlen (in_fname);
1786 fp->lineno = 0;
1788 /* In C++, wchar_t is a distinct basic type, and we can expect
1789 __wchar_t to be defined by cc1plus. */
1790 if (cplusplus)
1791 wchar_type = "__wchar_t";
1793 /* Install __LINE__, etc. Must follow initialize_char_syntax
1794 and option processing. */
1795 initialize_builtins (fp, &outbuf);
1797 /* Now handle the command line options. */
1799 /* Do -U's, -D's and -A's in the order they were seen. */
1800 for (i = 1; i < argc; i++) {
1801 if (pend_undefs[i]) {
1802 if (debug_output)
1803 output_line_directive (fp, &outbuf, 0, same_file);
1804 make_undef (pend_undefs[i], &outbuf);
1806 if (pend_defs[2*i]) {
1807 if (debug_output)
1808 output_line_directive (fp, &outbuf, 0, same_file);
1809 make_definition (pend_defs[2*i]);
1811 if (pend_defs[2*i+1]) {
1812 if (debug_output)
1813 output_line_directive (fp, &outbuf, 0, same_file);
1814 make_definition (pend_defs[2*i+1]);
1816 if (pend_assertions[i])
1817 make_assertion (pend_assertion_options[i], pend_assertions[i]);
1820 done_initializing = 1;
1822 { /* Read the appropriate environment variable and if it exists
1823 replace include_defaults with the listed path. */
1824 char *epath = 0;
1825 switch ((objc << 1) + cplusplus)
1827 case 0:
1828 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
1829 break;
1830 case 1:
1831 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
1832 break;
1833 case 2:
1834 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
1835 break;
1836 case 3:
1837 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
1838 break;
1840 /* If the environment var for this language is set,
1841 add to the default list of include directories. */
1842 if (epath) {
1843 int num_dirs;
1844 char *startp, *endp;
1846 for (num_dirs = 1, startp = epath; *startp; startp++)
1847 if (*startp == PATH_SEPARATOR)
1848 num_dirs++;
1849 include_defaults
1850 = (struct default_include *) xmalloc ((num_dirs
1851 * sizeof (struct default_include))
1852 + sizeof (include_defaults_array));
1853 startp = endp = epath;
1854 num_dirs = 0;
1855 while (1) {
1856 char c = *endp++;
1857 if (c == PATH_SEPARATOR || !c) {
1858 endp[-1] = 0;
1859 include_defaults[num_dirs].fname
1860 = startp == endp ? "." : xstrdup (startp);
1861 endp[-1] = c;
1862 include_defaults[num_dirs].component = 0;
1863 include_defaults[num_dirs].cplusplus = cplusplus;
1864 include_defaults[num_dirs].cxx_aware = 1;
1865 num_dirs++;
1866 if (!c)
1867 break;
1868 startp = endp;
1871 /* Put the usual defaults back in at the end. */
1872 bcopy ((const PTR) include_defaults_array,
1873 (PTR) &include_defaults[num_dirs],
1874 sizeof (include_defaults_array));
1878 append_include_chain (before_system, last_before_system);
1879 first_system_include = before_system;
1881 /* Unless -fnostdinc,
1882 tack on the standard include file dirs to the specified list */
1883 if (!no_standard_includes) {
1884 struct default_include *p = include_defaults;
1885 char *specd_prefix = include_prefix;
1886 char *default_prefix = xstrdup (GCC_INCLUDE_DIR);
1887 int default_len = 0;
1888 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1889 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
1890 default_len = strlen (default_prefix) - 7;
1891 default_prefix[default_len] = 0;
1893 /* Search "translated" versions of GNU directories.
1894 These have /usr/local/lib/gcc... replaced by specd_prefix. */
1895 if (specd_prefix != 0 && default_len != 0)
1896 for (p = include_defaults; p->fname; p++) {
1897 /* Some standard dirs are only for C++. */
1898 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1899 /* Does this dir start with the prefix? */
1900 if (!strncmp (p->fname, default_prefix, default_len)) {
1901 /* Yes; change prefix and add to search list. */
1902 struct file_name_list *new
1903 = new_include_prefix (NULL_PTR, NULL_PTR, specd_prefix,
1904 p->fname + default_len);
1905 if (new) {
1906 new->c_system_include_path = !p->cxx_aware;
1907 append_include_chain (new, new);
1908 if (first_system_include == 0)
1909 first_system_include = new;
1910 p->included = 1;
1915 /* Search ordinary names for GNU include directories. */
1916 for (p = include_defaults; p->fname; p++) {
1917 /* Some standard dirs are only for C++. */
1918 if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1919 struct file_name_list *new
1920 = new_include_prefix (NULL_PTR, p->component, "", p->fname);
1921 if (new) {
1922 new->c_system_include_path = !p->cxx_aware;
1923 append_include_chain (new, new);
1924 if (first_system_include == 0)
1925 first_system_include = new;
1926 p->included = 1;
1932 /* Tack the after_include chain at the end of the include chain. */
1933 append_include_chain (after_include, last_after_include);
1934 if (first_system_include == 0)
1935 first_system_include = after_include;
1937 /* With -v, print the list of dirs to search. */
1938 if (verbose) {
1939 struct file_name_list *p;
1940 notice ("#include \"...\" search starts here:\n");
1941 for (p = include; p; p = p->next) {
1942 if (p == first_bracket_include)
1943 notice ("#include <...> search starts here:\n");
1944 if (!p->fname[0])
1945 fprintf (stderr, " .\n");
1946 else if (!strcmp (p->fname, "/") || !strcmp (p->fname, "//"))
1947 fprintf (stderr, " %s\n", p->fname);
1948 else
1949 /* Omit trailing '/'. */
1950 fprintf (stderr, " %.*s\n", (int) strlen (p->fname) - 1, p->fname);
1952 notice ("End of search list.\n");
1954 struct default_include * d;
1955 notice ("The following default directories have been omitted from the search path:\n");
1956 for (d = include_defaults; d->fname; d++)
1957 if (! d->included)
1958 fprintf (stderr, " %s\n", d->fname);
1959 notice ("End of omitted list.\n");
1963 /* -MG doesn't select the form of output and must be specified with one of
1964 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
1965 inhibit compilation. */
1966 if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
1967 fatal ("-MG must be specified with one of -M or -MM");
1969 /* Either of two environment variables can specify output of deps.
1970 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1971 where OUTPUT_FILE is the file to write deps info to
1972 and DEPS_TARGET is the target to mention in the deps. */
1974 if (print_deps == 0
1975 && (getenv ("SUNPRO_DEPENDENCIES") != 0
1976 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
1977 char *spec = getenv ("DEPENDENCIES_OUTPUT");
1978 char *s;
1979 char *output_file;
1981 if (spec == 0) {
1982 spec = getenv ("SUNPRO_DEPENDENCIES");
1983 print_deps = 2;
1985 else
1986 print_deps = 1;
1988 /* Find the space before the DEPS_TARGET, if there is one. */
1989 s = index (spec, ' ');
1990 if (s) {
1991 deps_target = s + 1;
1992 output_file = xmalloc (s - spec + 1);
1993 bcopy (spec, output_file, s - spec);
1994 output_file[s - spec] = 0;
1995 } else {
1996 deps_target = 0;
1997 output_file = spec;
2000 deps_file = output_file;
2001 deps_mode = "a";
2004 /* For -M, print the expected object file name
2005 as the target of this Make-rule. */
2006 if (print_deps) {
2007 deps_allocated_size = 200;
2008 deps_buffer = xmalloc (deps_allocated_size);
2009 deps_buffer[0] = 0;
2010 deps_size = 0;
2011 deps_column = 0;
2013 if (deps_target) {
2014 deps_output (deps_target, ':');
2015 } else if (*in_fname == 0) {
2016 deps_output ("-", ':');
2017 } else {
2018 char *p, *q;
2019 int len;
2021 q = base_name (in_fname);
2023 /* Copy remainder to mungable area. */
2024 p = (char *) alloca (strlen(q) + 8);
2025 strcpy (p, q);
2027 /* Output P, but remove known suffixes. */
2028 len = strlen (p);
2029 q = p + len;
2030 if (len >= 2
2031 && p[len - 2] == '.'
2032 && index("cCsSm", p[len - 1]))
2033 q = p + (len - 2);
2034 else if (len >= 3
2035 && p[len - 3] == '.'
2036 && p[len - 2] == 'c'
2037 && p[len - 1] == 'c')
2038 q = p + (len - 3);
2039 else if (len >= 4
2040 && p[len - 4] == '.'
2041 && p[len - 3] == 'c'
2042 && p[len - 2] == 'x'
2043 && p[len - 1] == 'x')
2044 q = p + (len - 4);
2045 else if (len >= 4
2046 && p[len - 4] == '.'
2047 && p[len - 3] == 'c'
2048 && p[len - 2] == 'p'
2049 && p[len - 1] == 'p')
2050 q = p + (len - 4);
2052 /* Supply our own suffix. */
2053 strcpy (q, OBJECT_SUFFIX);
2055 deps_output (p, ':');
2058 deps_output (in_fname, ' ');
2061 /* Scan the -imacros files before the main input.
2062 Much like #including them, but with no_output set
2063 so that only their macro definitions matter. */
2065 no_output++; no_record_file++;
2066 for (i = 1; i < argc; i++)
2067 if (pend_files[i]) {
2068 struct include_file *inc;
2069 int fd = open_include_file (pend_files[i], NULL_PTR, NULL_PTR, &inc);
2070 if (fd < 0) {
2071 perror_with_name (pend_files[i]);
2072 return FATAL_EXIT_CODE;
2074 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2076 no_output--; no_record_file--;
2078 /* Copy the entire contents of the main input file into
2079 the stacked input buffer previously allocated for it. */
2081 /* JF check for stdin */
2082 if (in_fname == NULL || *in_fname == 0) {
2083 in_fname = "";
2084 f = 0;
2085 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
2086 goto perror;
2088 if (fstat (f, &st) != 0)
2089 pfatal_with_name (in_fname);
2090 fp->nominal_fname = fp->fname = in_fname;
2091 fp->nominal_fname_len = strlen (in_fname);
2092 fp->lineno = 1;
2093 fp->system_header_p = 0;
2094 /* JF all this is mine about reading pipes and ttys */
2095 if (! S_ISREG (st.st_mode)) {
2096 /* Read input from a file that is not a normal disk file.
2097 We cannot preallocate a buffer with the correct size,
2098 so we must read in the file a piece at the time and make it bigger. */
2099 int size;
2100 int bsize;
2101 int cnt;
2103 if (S_ISDIR (st.st_mode))
2104 fatal ("Input file `%s' is a directory", in_fname);
2106 bsize = 2000;
2107 size = 0;
2108 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
2109 for (;;) {
2110 cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
2111 if (cnt < 0) goto perror; /* error! */
2112 size += cnt;
2113 if (size != bsize) break; /* End of file */
2114 bsize *= 2;
2115 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
2117 fp->length = size;
2118 } else {
2119 /* Read a file whose size we can determine in advance.
2120 For the sake of VMS, st.st_size is just an upper bound. */
2121 size_t s = (size_t) st.st_size;
2122 if (s != st.st_size || s + 2 < s)
2123 memory_full ();
2124 fp->buf = (U_CHAR *) xmalloc (s + 2);
2125 fp->length = safe_read (f, (char *) fp->buf, s);
2126 if (fp->length < 0) goto perror;
2128 fp->bufp = fp->buf;
2129 fp->if_stack = if_stack;
2131 /* Make sure data ends with a newline. And put a null after it. */
2133 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
2134 /* Backslash-newline at end is not good enough. */
2135 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
2136 fp->buf[fp->length++] = '\n';
2137 missing_newline = 1;
2139 fp->buf[fp->length] = '\0';
2141 /* Unless inhibited, convert trigraphs in the input. */
2143 if (!no_trigraphs)
2144 trigraph_pcp (fp);
2146 if (warn_white_space)
2147 check_white_space (fp);
2149 /* Now that we know the input file is valid, open the output. */
2151 if (!out_fname || !strcmp (out_fname, ""))
2152 out_fname = "stdout";
2153 else if (! freopen (out_fname, "w", stdout))
2154 pfatal_with_name (out_fname);
2156 output_line_directive (fp, &outbuf, 0, same_file);
2158 /* Scan the -include files before the main input. */
2160 no_record_file++;
2161 for (i = 1; i < argc; i++)
2162 if (pend_includes[i]) {
2163 struct include_file *inc;
2164 int fd = open_include_file (pend_includes[i], NULL_PTR, NULL_PTR, &inc);
2165 if (fd < 0) {
2166 perror_with_name (pend_includes[i]);
2167 return FATAL_EXIT_CODE;
2169 finclude (fd, inc, &outbuf, 0, NULL_PTR);
2171 no_record_file--;
2173 /* Scan the input, processing macros and directives. */
2175 rescan (&outbuf, 0);
2177 if (missing_newline)
2178 fp->lineno--;
2180 if (pedantic && missing_newline)
2181 pedwarn ("file does not end in newline");
2183 /* Now we have processed the entire input
2184 Write whichever kind of output has been requested. */
2186 if (dump_macros == dump_only)
2187 dump_all_macros ();
2188 else if (! inhibit_output) {
2189 write_output ();
2192 if (print_deps) {
2193 /* Don't actually write the deps file if compilation has failed. */
2194 if (errors == 0) {
2195 if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
2196 pfatal_with_name (deps_file);
2197 fputs (deps_buffer, deps_stream);
2198 putc ('\n', deps_stream);
2199 if (deps_file) {
2200 if (ferror (deps_stream) || fclose (deps_stream) != 0)
2201 fatal ("I/O error on output");
2206 if (pcp_outfile && pcp_outfile != stdout
2207 && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
2208 fatal ("I/O error on `-pcp' output");
2210 if (ferror (stdout) || fclose (stdout) != 0)
2211 fatal ("I/O error on output");
2213 if (errors)
2214 exit (FATAL_EXIT_CODE);
2215 exit (SUCCESS_EXIT_CODE);
2217 perror:
2218 pfatal_with_name (in_fname);
2219 return 0;
2222 /* Given a colon-separated list of file names PATH,
2223 add all the names to the search path for include files. */
2225 static void
2226 path_include (path)
2227 char *path;
2229 char *p;
2231 p = path;
2233 if (*p)
2234 while (1) {
2235 char *q = p;
2236 char c;
2237 struct file_name_list *dirtmp;
2239 /* Find the end of this name. */
2240 while ((c = *q++) != PATH_SEPARATOR && c)
2241 continue;
2243 q[-1] = 0;
2244 dirtmp = new_include_prefix (last_include, NULL_PTR,
2245 "", p == q ? "." : p);
2246 q[-1] = c;
2247 append_include_chain (dirtmp, dirtmp);
2249 /* Advance past this name. */
2250 p = q;
2251 if (! c)
2252 break;
2256 /* Return the address of the first character in S that equals C.
2257 S is an array of length N, possibly containing '\0's, and followed by '\0'.
2258 Return 0 if there is no such character. Assume that C itself is not '\0'.
2259 If we knew we could use memchr, we could just invoke memchr (S, C, N),
2260 but unfortunately memchr isn't autoconfigured yet. */
2262 static const U_CHAR *
2263 index0 (s, c, n)
2264 const U_CHAR *s;
2265 int c;
2266 size_t n;
2268 const char *p = (const char *) s;
2269 for (;;) {
2270 const char *q = index (p, c);
2271 if (q)
2272 return (const U_CHAR *) q;
2273 else {
2274 size_t l = strlen (p);
2275 if (l == n)
2276 return 0;
2277 l++;
2278 p += l;
2279 n -= l;
2284 /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
2285 before main CCCP processing. Name `pcp' is also in honor of the
2286 drugs the trigraph designers must have been on.
2288 Using an extra pass through the buffer takes a little extra time,
2289 but is infinitely less hairy than trying to handle trigraphs inside
2290 strings, etc. everywhere, and also makes sure that trigraphs are
2291 only translated in the top level of processing. */
2293 static void
2294 trigraph_pcp (buf)
2295 FILE_BUF *buf;
2297 register U_CHAR c, *bptr;
2298 register const U_CHAR *fptr, *sptr, *lptr;
2299 int len;
2301 fptr = sptr = bptr = buf->buf;
2302 lptr = fptr + buf->length;
2303 while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
2304 if (*++sptr != '?')
2305 continue;
2306 switch (*++sptr) {
2307 case '=':
2308 c = '#';
2309 break;
2310 case '(':
2311 c = '[';
2312 break;
2313 case '/':
2314 c = '\\';
2315 break;
2316 case ')':
2317 c = ']';
2318 break;
2319 case '\'':
2320 c = '^';
2321 break;
2322 case '<':
2323 c = '{';
2324 break;
2325 case '!':
2326 c = '|';
2327 break;
2328 case '>':
2329 c = '}';
2330 break;
2331 case '-':
2332 c = '~';
2333 break;
2334 case '?':
2335 sptr--;
2336 continue;
2337 default:
2338 continue;
2340 len = sptr - fptr - 2;
2342 /* BSD doc says bcopy () works right for overlapping strings. In ANSI
2343 C, this will be memmove (). */
2344 if (bptr != fptr && len > 0)
2345 bcopy ((const PTR) fptr, (PTR) bptr, len);
2347 bptr += len;
2348 *bptr++ = c;
2349 fptr = ++sptr;
2351 len = buf->length - (fptr - buf->buf);
2352 if (bptr != fptr && len > 0)
2353 bcopy ((const PTR) fptr, (PTR) bptr, len);
2354 buf->length -= fptr - bptr;
2355 buf->buf[buf->length] = '\0';
2356 if (warn_trigraphs && fptr != bptr)
2357 warning_with_line (0, "%lu trigraph(s) encountered",
2358 (unsigned long) (fptr - bptr) / 2);
2361 /* Warn about white space between backslash and end of line. */
2363 static void
2364 check_white_space (buf)
2365 FILE_BUF *buf;
2367 register const U_CHAR *sptr = buf->buf;
2368 register const U_CHAR *lptr = sptr + buf->length;
2369 register const U_CHAR *nptr;
2370 int line = 0;
2372 nptr = sptr = buf->buf;
2373 lptr = sptr + buf->length;
2374 for (nptr = sptr;
2375 (nptr = index0 (nptr, '\n', (size_t) (lptr - nptr))) != NULL;
2376 nptr ++) {
2377 register const U_CHAR *p = nptr;
2378 line++;
2379 for (p = nptr; sptr < p; p--) {
2380 if (! is_hor_space[p[-1]]) {
2381 if (p[-1] == '\\' && p != nptr)
2382 warning_with_line (line,
2383 "`\\' followed by white space at end of line");
2384 break;
2390 /* Move all backslash-newline pairs out of embarrassing places.
2391 Exchange all such pairs following BP
2392 with any potentially-embarrassing characters that follow them.
2393 Potentially-embarrassing characters are / and *
2394 (because a backslash-newline inside a comment delimiter
2395 would cause it not to be recognized).
2396 We assume that *BP == '\\'. */
2398 static void
2399 newline_fix (bp)
2400 U_CHAR *bp;
2402 register U_CHAR *p = bp;
2404 /* First count the backslash-newline pairs here. */
2405 do {
2406 if (p[1] != '\n')
2407 break;
2408 p += 2;
2409 } while (*p == '\\');
2411 /* What follows the backslash-newlines is not embarrassing. */
2413 if (*p != '/' && *p != '*')
2414 /* What follows the backslash-newlines is not embarrassing. */
2415 return;
2417 /* Copy all potentially embarrassing characters
2418 that follow the backslash-newline pairs
2419 down to where the pairs originally started. */
2421 *bp++ = *p++;
2422 while (*p == '*' || *p == '/');
2424 /* Now write the same number of pairs after the embarrassing chars. */
2425 while (bp < p) {
2426 *bp++ = '\\';
2427 *bp++ = '\n';
2431 /* Like newline_fix but for use within a directive-name.
2432 Move any backslash-newlines up past any following symbol constituents. */
2434 static void
2435 name_newline_fix (bp)
2436 U_CHAR *bp;
2438 register U_CHAR *p = bp;
2440 /* First count the backslash-newline pairs here. */
2441 do {
2442 if (p[1] != '\n')
2443 break;
2444 p += 2;
2445 } while (*p == '\\');
2447 /* What follows the backslash-newlines is not embarrassing. */
2449 if (!is_idchar[*p])
2450 /* What follows the backslash-newlines is not embarrassing. */
2451 return;
2453 /* Copy all potentially embarrassing characters
2454 that follow the backslash-newline pairs
2455 down to where the pairs originally started. */
2457 *bp++ = *p++;
2458 while (is_idchar[*p]);
2460 /* Now write the same number of pairs after the embarrassing chars. */
2461 while (bp < p) {
2462 *bp++ = '\\';
2463 *bp++ = '\n';
2467 /* Look for lint commands in comments.
2469 When we come in here, ibp points into a comment. Limit is as one expects.
2470 scan within the comment -- it should start, after lwsp, with a lint command.
2471 If so that command is returned as a (constant) string.
2473 Upon return, any arg will be pointed to with argstart and will be
2474 arglen long. Note that we don't parse that arg since it will just
2475 be printed out again. */
2477 static const char *
2478 get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
2479 register const U_CHAR *ibp;
2480 register const U_CHAR *limit;
2481 const U_CHAR **argstart; /* point to command arg */
2482 int *arglen, *cmdlen; /* how long they are */
2484 HOST_WIDEST_INT linsize;
2485 register const U_CHAR *numptr; /* temp for arg parsing */
2487 *arglen = 0;
2489 SKIP_WHITE_SPACE (ibp);
2491 if (ibp >= limit) return NULL;
2493 linsize = limit - ibp;
2495 /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
2496 if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
2497 *cmdlen = 10;
2498 return "NOTREACHED";
2500 if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
2501 *cmdlen = 8;
2502 return "ARGSUSED";
2504 if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
2505 *cmdlen = 11;
2506 return "LINTLIBRARY";
2508 if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
2509 *cmdlen = 7;
2510 ibp += 7; linsize -= 7;
2511 if ((linsize == 0) || ! ISDIGIT (*ibp)) return "VARARGS";
2513 /* OK, read a number */
2514 for (numptr = *argstart = ibp; (numptr < limit) && ISDIGIT (*numptr);
2515 numptr++);
2516 *arglen = numptr - *argstart;
2517 return "VARARGS";
2519 return NULL;
2523 * The main loop of the program.
2525 * Read characters from the input stack, transferring them to the
2526 * output buffer OP.
2528 * Macros are expanded and push levels on the input stack.
2529 * At the end of such a level it is popped off and we keep reading.
2530 * At the end of any other kind of level, we return.
2531 * #-directives are handled, except within macros.
2533 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
2534 * and insert them when appropriate. This is set while scanning macro
2535 * arguments before substitution. It is zero when scanning for final output.
2536 * There are two types of Newline markers:
2537 * * Newline - follows a macro name that was not expanded
2538 * because it appeared inside an expansion of the same macro.
2539 * This marker prevents future expansion of that identifier.
2540 * When the input is rescanned into the final output, these are deleted.
2541 * These are also deleted by ## concatenation.
2542 * * Newline Space (or Newline and any other whitespace character)
2543 * stands for a place that tokens must be separated or whitespace
2544 * is otherwise desirable, but where the ANSI standard specifies there
2545 * is no whitespace. This marker turns into a Space (or whichever other
2546 * whitespace char appears in the marker) in the final output,
2547 * but it turns into nothing in an argument that is stringified with #.
2548 * Such stringified arguments are the only place where the ANSI standard
2549 * specifies with precision that whitespace may not appear.
2551 * During this function, IP->bufp is kept cached in IBP for speed of access.
2552 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
2553 * IBP, IP and OBP must be copied back to memory. IP and IBP are
2554 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
2555 * explicitly, and before RECACHE, since RECACHE uses OBP.
2558 static void
2559 rescan (op, output_marks)
2560 FILE_BUF *op;
2561 int output_marks;
2563 /* Character being scanned in main loop. */
2564 register U_CHAR c;
2566 /* Length of pending accumulated identifier. */
2567 register int ident_length = 0;
2569 /* Hash code of pending accumulated identifier. */
2570 register int hash = 0;
2572 /* Current input level (&instack[indepth]). */
2573 FILE_BUF *ip;
2575 /* Pointer for scanning input. */
2576 register U_CHAR *ibp;
2578 /* Pointer to end of input. End of scan is controlled by LIMIT. */
2579 register U_CHAR *limit;
2581 /* Pointer for storing output. */
2582 register U_CHAR *obp;
2584 /* REDO_CHAR is nonzero if we are processing an identifier
2585 after backing up over the terminating character.
2586 Sometimes we process an identifier without backing up over
2587 the terminating character, if the terminating character
2588 is not special. Backing up is done so that the terminating character
2589 will be dispatched on again once the identifier is dealt with. */
2590 int redo_char = 0;
2592 /* 1 if within an identifier inside of which a concatenation
2593 marker (Newline -) has been seen. */
2594 int concatenated = 0;
2596 /* While scanning a comment or a string constant,
2597 this records the line it started on, for error messages. */
2598 int start_line;
2600 /* Record position of last `real' newline. */
2601 U_CHAR *beg_of_line;
2603 /* Pop the innermost input stack level, assuming it is a macro expansion. */
2605 #define POPMACRO \
2606 do { ip->macro->type = T_MACRO; \
2607 if (ip->free_ptr) free (ip->free_ptr); \
2608 --indepth; } while (0)
2610 /* Reload `rescan's local variables that describe the current
2611 level of the input stack. */
2613 #define RECACHE \
2614 do { ip = &instack[indepth]; \
2615 ibp = ip->bufp; \
2616 limit = ip->buf + ip->length; \
2617 op->bufp = obp; \
2618 check_expand (op, limit - ibp); \
2619 beg_of_line = 0; \
2620 obp = op->bufp; } while (0)
2622 if (no_output && instack[indepth].fname != 0)
2623 skip_if_group (&instack[indepth], 1, NULL);
2625 obp = op->bufp;
2626 RECACHE;
2628 beg_of_line = ibp;
2630 /* Our caller must always put a null after the end of
2631 the input at each input stack level. */
2632 if (*limit != 0)
2633 abort ();
2635 while (1) {
2636 c = *ibp++;
2637 *obp++ = c;
2639 switch (c) {
2640 case '\\':
2641 if (*ibp == '\n' && !ip->macro) {
2642 /* At the top level, always merge lines ending with backslash-newline,
2643 even in middle of identifier. But do not merge lines in a macro,
2644 since backslash might be followed by a newline-space marker. */
2645 ++ibp;
2646 ++ip->lineno;
2647 --obp; /* remove backslash from obuf */
2648 break;
2650 /* If ANSI, backslash is just another character outside a string. */
2651 if (!traditional)
2652 goto randomchar;
2653 /* Otherwise, backslash suppresses specialness of following char,
2654 so copy it here to prevent the switch from seeing it.
2655 But first get any pending identifier processed. */
2656 if (ident_length > 0)
2657 goto specialchar;
2658 if (ibp < limit)
2659 *obp++ = *ibp++;
2660 break;
2662 case '%':
2663 if (ident_length || ip->macro || traditional)
2664 goto randomchar;
2665 while (*ibp == '\\' && ibp[1] == '\n') {
2666 ibp += 2;
2667 ++ip->lineno;
2669 if (*ibp != ':')
2670 break;
2671 /* Treat this %: digraph as if it were #. */
2672 /* Fall through. */
2674 case '#':
2675 if (assertions_flag) {
2676 if (ident_length)
2677 goto specialchar;
2678 /* Copy #foo (bar lose) without macro expansion. */
2679 obp[-1] = '#'; /* In case it was '%'. */
2680 SKIP_WHITE_SPACE (ibp);
2681 while (is_idchar[*ibp])
2682 *obp++ = *ibp++;
2683 SKIP_WHITE_SPACE (ibp);
2684 if (*ibp == '(') {
2685 ip->bufp = ibp;
2686 skip_paren_group (ip);
2687 bcopy ((const PTR) ibp, (PTR) obp, ip->bufp - ibp);
2688 obp += ip->bufp - ibp;
2689 ibp = ip->bufp;
2691 break;
2694 /* If this is expanding a macro definition, don't recognize
2695 preprocessing directives. */
2696 if (ip->macro != 0)
2697 goto randomchar;
2698 /* If this is expand_into_temp_buffer,
2699 don't recognize them either. Warn about them
2700 only after an actual newline at this level,
2701 not at the beginning of the input level. */
2702 if (! ip->fname) {
2703 if (ip->buf != beg_of_line)
2704 warning ("preprocessing directive not recognized within macro arg");
2705 goto randomchar;
2707 if (ident_length)
2708 goto specialchar;
2711 /* # keyword: a # must be first nonblank char on the line */
2712 if (beg_of_line == 0)
2713 goto randomchar;
2715 U_CHAR *bp;
2717 /* Scan from start of line, skipping whitespace, comments
2718 and backslash-newlines, and see if we reach this #.
2719 If not, this # is not special. */
2720 bp = beg_of_line;
2721 /* If -traditional, require # to be at beginning of line. */
2722 if (!traditional) {
2723 while (1) {
2724 if (is_hor_space[*bp])
2725 bp++;
2726 else if (*bp == '\\' && bp[1] == '\n')
2727 bp += 2;
2728 else if (*bp == '/' && bp[1] == '*') {
2729 bp += 2;
2730 while (1)
2732 if (*bp == '*')
2734 if (bp[1] == '/')
2736 bp += 2;
2737 break;
2740 else
2742 #ifdef MULTIBYTE_CHARS
2743 int length;
2744 length = local_mblen (bp, limit - bp);
2745 if (length > 1)
2746 bp += (length - 1);
2747 #endif
2749 bp++;
2752 /* There is no point in trying to deal with C++ // comments here,
2753 because if there is one, then this # must be part of the
2754 comment and we would never reach here. */
2755 else break;
2757 if (c == '%') {
2758 if (bp[0] != '%')
2759 break;
2760 while (bp[1] == '\\' && bp[2] == '\n')
2761 bp += 2;
2762 if (bp + 1 != ibp)
2763 break;
2764 /* %: appears at start of line; skip past the ':' too. */
2765 bp++;
2766 ibp++;
2769 if (bp + 1 != ibp)
2770 goto randomchar;
2773 /* This # can start a directive. */
2775 --obp; /* Don't copy the '#' */
2777 ip->bufp = ibp;
2778 op->bufp = obp;
2779 if (! handle_directive (ip, op)) {
2780 #ifdef USE_C_ALLOCA
2781 alloca (0);
2782 #endif
2783 /* Not a known directive: treat it as ordinary text.
2784 IP, OP, IBP, etc. have not been changed. */
2785 if (no_output && instack[indepth].fname) {
2786 /* If not generating expanded output,
2787 what we do with ordinary text is skip it.
2788 Discard everything until next # directive. */
2789 skip_if_group (&instack[indepth], 1, 0);
2790 RECACHE;
2791 beg_of_line = ibp;
2792 break;
2794 *obp++ = '#'; /* Copy # (even if it was originally %:). */
2795 /* Don't expand an identifier that could be a macro directive.
2796 (Section 3.8.3 of the ANSI C standard) */
2797 SKIP_WHITE_SPACE (ibp);
2798 if (is_idstart[*ibp])
2800 *obp++ = *ibp++;
2801 while (is_idchar[*ibp])
2802 *obp++ = *ibp++;
2804 goto randomchar;
2806 #ifdef USE_C_ALLOCA
2807 alloca (0);
2808 #endif
2809 /* A # directive has been successfully processed. */
2810 /* If not generating expanded output, ignore everything until
2811 next # directive. */
2812 if (no_output && instack[indepth].fname)
2813 skip_if_group (&instack[indepth], 1, 0);
2814 obp = op->bufp;
2815 RECACHE;
2816 beg_of_line = ibp;
2817 break;
2819 case '\"': /* skip quoted string */
2820 case '\'':
2821 /* A single quoted string is treated like a double -- some
2822 programs (e.g., troff) are perverse this way */
2824 /* Handle any pending identifier;
2825 but the L in L'...' or L"..." is not an identifier. */
2826 if (ident_length) {
2827 if (! (ident_length == 1 && hash == HASHSTEP (0, 'L')))
2828 goto specialchar;
2829 ident_length = hash = 0;
2832 start_line = ip->lineno;
2834 /* Skip ahead to a matching quote. */
2836 while (1) {
2837 if (ibp >= limit) {
2838 if (ip->macro != 0) {
2839 /* try harder: this string crosses a macro expansion boundary.
2840 This can happen naturally if -traditional.
2841 Otherwise, only -D can make a macro with an unmatched quote. */
2842 POPMACRO;
2843 RECACHE;
2844 continue;
2846 if (!traditional) {
2847 error_with_line (line_for_error (start_line),
2848 "unterminated string or character constant");
2849 if (multiline_string_line) {
2850 error_with_line (multiline_string_line,
2851 "possible real start of unterminated constant");
2852 multiline_string_line = 0;
2855 break;
2857 *obp++ = *ibp;
2858 switch (*ibp++) {
2859 case '\n':
2860 if (warn_white_space && ip->fname && is_hor_space[ibp[-2]])
2861 warning ("white space at end of line in string");
2862 ++ip->lineno;
2863 ++op->lineno;
2864 /* Traditionally, end of line ends a string constant with no error.
2865 So exit the loop and record the new line. */
2866 if (traditional) {
2867 beg_of_line = ibp;
2868 goto while2end;
2870 if (c == '\'') {
2871 error_with_line (line_for_error (start_line),
2872 "unterminated character constant");
2873 goto while2end;
2875 if (multiline_string_line == 0) {
2876 if (pedantic)
2877 pedwarn_with_line (line_for_error (start_line),
2878 "string constant runs past end of line");
2879 multiline_string_line = ip->lineno - 1;
2881 break;
2883 case '\\':
2884 if (*ibp == '\n') {
2885 /* Backslash newline is replaced by nothing at all, but
2886 keep the line counts correct. But if we are reading
2887 from a macro, keep the backslash newline, since backslash
2888 newlines have already been processed. */
2889 if (ip->macro) {
2890 *obp++ = '\n';
2891 ++op->lineno;
2892 } else
2893 --obp;
2894 ++ibp;
2895 ++ip->lineno;
2896 } else {
2897 /* ANSI stupidly requires that in \\ the second \
2898 is *not* prevented from combining with a newline. */
2899 if (!ip->macro) {
2900 while (*ibp == '\\' && ibp[1] == '\n') {
2901 *obp++ = *ibp++;
2902 *obp++ = *ibp++;
2903 ++ip->lineno;
2904 ++op->lineno;
2907 *obp++ = *ibp++;
2909 break;
2911 case '\"':
2912 case '\'':
2913 if (ibp[-1] == c)
2914 goto while2end;
2915 break;
2916 #ifdef MULTIBYTE_CHARS
2917 default:
2919 int length;
2920 --ibp;
2921 length = local_mblen (ibp, limit - ibp);
2922 if (length > 0)
2924 --obp;
2925 bcopy (ibp, obp, length);
2926 obp += length;
2927 ibp += length;
2929 else
2930 ++ibp;
2932 break;
2933 #endif
2936 while2end:
2937 break;
2939 case '/':
2940 if (ip->macro != 0)
2941 goto randomchar;
2942 if (*ibp == '\\')
2943 newline_fix (ibp);
2944 if (*ibp != '*'
2945 && !(cplusplus_comments && *ibp == '/'))
2946 goto randomchar;
2947 if (ident_length)
2948 goto specialchar;
2950 if (*ibp == '/') {
2951 /* C++ style comment... */
2952 start_line = ip->lineno;
2954 /* Comments are equivalent to spaces. */
2955 if (! put_out_comments)
2956 obp[-1] = ' ';
2959 U_CHAR *before_bp = ibp;
2961 while (++ibp < limit) {
2962 if (*ibp == '\n')
2964 if (put_out_comments) {
2965 bcopy ((const PTR) before_bp, (PTR) obp, ibp - before_bp);
2966 obp += ibp - before_bp;
2968 break;
2970 if (*ibp == '\\')
2972 if (ibp + 1 < limit && ibp[1] == '\n')
2974 if (warn_comments)
2975 warning ("multiline `//' comment");
2976 ++ip->lineno;
2977 /* Copy the newline into the output buffer, in order to
2978 avoid the pain of a #line every time a multiline comment
2979 is seen. */
2980 if (!put_out_comments)
2981 *obp++ = '\n';
2982 ++op->lineno;
2983 ++ibp;
2986 else
2988 #ifdef MULTIBYTE_CHARS
2989 int length;
2990 length = local_mblen (ibp, limit - ibp);
2991 if (length > 1)
2992 ibp += (length - 1);
2993 #endif
2996 break;
3000 /* Ordinary C comment. Skip it, optionally copying it to output. */
3002 start_line = ip->lineno;
3004 ++ibp; /* Skip the star. */
3006 /* If this cpp is for lint, we peek inside the comments: */
3007 if (for_lint) {
3008 const U_CHAR *argbp;
3009 int cmdlen, arglen;
3010 const char *lintcmd =
3011 get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
3013 if (lintcmd != NULL) {
3014 op->bufp = obp;
3015 check_expand (op, cmdlen + arglen + 14);
3016 obp = op->bufp;
3017 /* I believe it is always safe to emit this newline: */
3018 obp[-1] = '\n';
3019 bcopy ("#pragma lint ", (char *) obp, 13);
3020 obp += 13;
3021 bcopy (lintcmd, (char *) obp, cmdlen);
3022 obp += cmdlen;
3024 if (arglen != 0) {
3025 *(obp++) = ' ';
3026 bcopy (argbp, (char *) obp, arglen);
3027 obp += arglen;
3030 /* OK, now bring us back to the state we were in before we entered
3031 this branch. We need #line because the #pragma's newline always
3032 messes up the line count. */
3033 op->bufp = obp;
3034 output_line_directive (ip, op, 0, same_file);
3035 check_expand (op, limit - ibp + 2);
3036 obp = op->bufp;
3037 *(obp++) = '/';
3041 /* Comments are equivalent to spaces.
3042 Note that we already output the slash; we might not want it.
3043 For -traditional, a comment is equivalent to nothing. */
3044 if (! put_out_comments) {
3045 if (traditional)
3046 obp--;
3047 else
3048 obp[-1] = ' ';
3050 else
3051 *obp++ = '*';
3054 U_CHAR *before_bp = ibp;
3056 for (;;) {
3057 switch (*ibp++) {
3058 case '*':
3059 if (ibp[-2] == '/' && warn_comments)
3060 warning ("`/*' within comment");
3061 if (*ibp == '\\')
3062 newline_fix (ibp);
3063 if (*ibp == '/')
3064 goto comment_end;
3065 break;
3067 case '\n':
3068 ++ip->lineno;
3069 /* Copy the newline into the output buffer, in order to
3070 avoid the pain of a #line every time a multiline comment
3071 is seen. */
3072 if (!put_out_comments)
3073 *obp++ = '\n';
3074 ++op->lineno;
3075 break;
3077 case 0:
3078 if (limit < ibp) {
3079 error_with_line (line_for_error (start_line),
3080 "unterminated comment");
3081 goto limit_reached;
3083 break;
3084 #ifdef MULTIBYTE_CHARS
3085 default:
3087 int length;
3088 length = local_mblen (ibp, limit - ibp);
3089 if (length > 1)
3090 ibp += (length - 1);
3092 break;
3093 #endif
3096 comment_end:
3098 ibp++;
3099 if (put_out_comments) {
3100 bcopy ((const PTR) before_bp, (PTR) obp, ibp - before_bp);
3101 obp += ibp - before_bp;
3104 break;
3106 case '$':
3107 if (! is_idchar['$'])
3108 goto randomchar;
3109 if (pedantic)
3110 pedwarn ("`$' in identifier");
3111 goto letter;
3113 case '0': case '1': case '2': case '3': case '4':
3114 case '5': case '6': case '7': case '8': case '9':
3115 /* If digit is not part of identifier, it starts a number,
3116 which means that following letters are not an identifier.
3117 "0x5" does not refer to an identifier "x5".
3118 So copy all alphanumerics that follow without accumulating
3119 as an identifier. Periods also, for sake of "3.e7". */
3121 if (ident_length == 0) {
3122 for (;;) {
3123 if (!ip->macro) {
3124 while (ibp[0] == '\\' && ibp[1] == '\n') {
3125 ++ip->lineno;
3126 ibp += 2;
3129 c = *ibp++;
3130 if (!is_idchar[c] && c != '.') {
3131 --ibp;
3132 break;
3134 *obp++ = c;
3135 /* A sign can be part of a preprocessing number
3136 if it follows an `e' or `p'. */
3137 if (c == 'e' || c == 'E' || c == 'p' || c == 'P') {
3138 if (!ip->macro) {
3139 while (ibp[0] == '\\' && ibp[1] == '\n') {
3140 ++ip->lineno;
3141 ibp += 2;
3144 if (*ibp == '+' || *ibp == '-') {
3145 *obp++ = *ibp++;
3146 /* But traditional C does not let the token go past the sign,
3147 and C89 does not allow `p'. */
3148 if (traditional || (c89 && (c == 'p' || c == 'P')))
3149 break;
3153 break;
3155 /* fall through */
3157 case '_':
3158 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3159 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
3160 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
3161 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
3162 case 'y': case 'z':
3163 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3164 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
3165 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
3166 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
3167 case 'Y': case 'Z':
3168 letter:
3169 ident_length++;
3170 /* Compute step of hash function, to avoid a proc call on every token */
3171 hash = HASHSTEP (hash, c);
3172 break;
3174 case '\n':
3175 if (ip->fname == 0 && *ibp == '-') {
3176 /* Newline - inhibits expansion of preceding token.
3177 If expanding a macro arg, we keep the newline -.
3178 In final output, it is deleted.
3179 We recognize Newline - in macro bodies and macro args. */
3180 if (! concatenated) {
3181 ident_length = 0;
3182 hash = 0;
3184 ibp++;
3185 if (!output_marks) {
3186 obp--;
3187 } else {
3188 /* If expanding a macro arg, keep the newline -. */
3189 *obp++ = '-';
3191 break;
3194 /* If reprocessing a macro expansion, newline is a special marker. */
3195 else if (ip->macro != 0) {
3196 /* Newline White is a "funny space" to separate tokens that are
3197 supposed to be separate but without space between.
3198 Here White means any whitespace character.
3199 Newline - marks a recursive macro use that is not
3200 supposed to be expandable. */
3202 if (is_space[*ibp]) {
3203 /* Newline Space does not prevent expansion of preceding token
3204 so expand the preceding token and then come back. */
3205 if (ident_length > 0)
3206 goto specialchar;
3208 /* If generating final output, newline space makes a space. */
3209 if (!output_marks) {
3210 obp[-1] = *ibp++;
3211 /* And Newline Newline makes a newline, so count it. */
3212 if (obp[-1] == '\n')
3213 op->lineno++;
3214 } else {
3215 /* If expanding a macro arg, keep the newline space.
3216 If the arg gets stringified, newline space makes nothing. */
3217 *obp++ = *ibp++;
3219 } else abort (); /* Newline followed by something random? */
3220 break;
3223 /* If there is a pending identifier, handle it and come back here. */
3224 if (ident_length > 0)
3225 goto specialchar;
3227 beg_of_line = ibp;
3229 /* Update the line counts and output a #line if necessary. */
3230 ++ip->lineno;
3231 ++op->lineno;
3232 if (ip->lineno != op->lineno) {
3233 op->bufp = obp;
3234 output_line_directive (ip, op, 1, same_file);
3235 check_expand (op, limit - ibp);
3236 obp = op->bufp;
3238 break;
3240 /* Come here either after (1) a null character that is part of the input
3241 or (2) at the end of the input, because there is a null there. */
3242 case 0:
3243 if (ibp <= limit)
3244 /* Our input really contains a null character. */
3245 goto randomchar;
3247 limit_reached:
3248 /* At end of a macro-expansion level, pop it and read next level. */
3249 if (ip->macro != 0) {
3250 obp--;
3251 ibp--;
3252 /* If traditional, and we have an identifier that ends here,
3253 process it now, so we get the right error for recursion. */
3254 if (traditional && ident_length
3255 && ! is_idchar[*instack[indepth - 1].bufp]) {
3256 redo_char = 1;
3257 goto randomchar;
3259 POPMACRO;
3260 RECACHE;
3261 break;
3264 /* If we don't have a pending identifier,
3265 return at end of input. */
3266 if (ident_length == 0) {
3267 obp--;
3268 ibp--;
3269 op->bufp = obp;
3270 ip->bufp = ibp;
3271 goto ending;
3274 /* If we do have a pending identifier, just consider this null
3275 a special character and arrange to dispatch on it again.
3276 The second time, IDENT_LENGTH will be zero so we will return. */
3278 /* Fall through */
3280 specialchar:
3282 /* Handle the case of a character such as /, ', " or null
3283 seen following an identifier. Back over it so that
3284 after the identifier is processed the special char
3285 will be dispatched on again. */
3287 ibp--;
3288 obp--;
3289 redo_char = 1;
3291 default:
3293 randomchar:
3295 if (ident_length > 0) {
3296 register HASHNODE *hp;
3298 /* We have just seen an identifier end. If it's a macro, expand it.
3300 IDENT_LENGTH is the length of the identifier
3301 and HASH is its hash code.
3303 The identifier has already been copied to the output,
3304 so if it is a macro we must remove it.
3306 If REDO_CHAR is 0, the char that terminated the identifier
3307 has been skipped in the output and the input.
3308 OBP-IDENT_LENGTH-1 points to the identifier.
3309 If the identifier is a macro, we must back over the terminator.
3311 If REDO_CHAR is 1, the terminating char has already been
3312 backed over. OBP-IDENT_LENGTH points to the identifier. */
3314 if (!pcp_outfile || pcp_inside_if) {
3315 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
3316 hp = hp->next) {
3318 if (hp->length == ident_length) {
3319 int obufp_before_macroname;
3320 int op_lineno_before_macroname;
3321 register int i = ident_length;
3322 register U_CHAR *p = hp->name;
3323 register U_CHAR *q = obp - i;
3324 int disabled;
3326 if (! redo_char)
3327 q--;
3329 do { /* All this to avoid a strncmp () */
3330 if (*p++ != *q++)
3331 goto hashcollision;
3332 } while (--i);
3334 /* We found a use of a macro name.
3335 see if the context shows it is a macro call. */
3337 /* Back up over terminating character if not already done. */
3338 if (! redo_char) {
3339 ibp--;
3340 obp--;
3343 /* Save this as a displacement from the beginning of the output
3344 buffer. We can not save this as a position in the output
3345 buffer, because it may get realloc'ed by RECACHE. */
3346 obufp_before_macroname = (obp - op->buf) - ident_length;
3347 op_lineno_before_macroname = op->lineno;
3349 if (hp->type == T_PCSTRING) {
3350 pcstring_used (hp); /* Mark the definition of this key
3351 as needed, ensuring that it
3352 will be output. */
3353 break; /* Exit loop, since the key cannot have a
3354 definition any longer. */
3357 /* Record whether the macro is disabled. */
3358 disabled = hp->type == T_DISABLED;
3360 /* This looks like a macro ref, but if the macro was disabled,
3361 just copy its name and put in a marker if requested. */
3363 if (disabled) {
3364 #if 0
3365 /* This error check caught useful cases such as
3366 #define foo(x,y) bar (x (y,0), y)
3367 foo (foo, baz) */
3368 if (traditional)
3369 error ("recursive use of macro `%s'", hp->name);
3370 #endif
3372 if (output_marks) {
3373 op->bufp = obp;
3374 check_expand (op, limit - ibp + 2);
3375 obp = op->bufp;
3376 *obp++ = '\n';
3377 *obp++ = '-';
3379 break;
3382 /* If macro wants an arglist, verify that a '(' follows.
3383 first skip all whitespace, copying it to the output
3384 after the macro name. Then, if there is no '(',
3385 decide this is not a macro call and leave things that way. */
3386 if ((hp->type == T_MACRO || hp->type == T_DISABLED)
3387 && hp->value.defn->nargs >= 0)
3389 U_CHAR *old_ibp = ibp;
3390 U_CHAR *old_obp = obp;
3391 int old_iln = ip->lineno;
3392 int old_oln = op->lineno;
3394 while (1) {
3395 /* Scan forward over whitespace, copying it to the output. */
3396 if (ibp == limit && ip->macro != 0) {
3397 POPMACRO;
3398 RECACHE;
3399 old_ibp = ibp;
3400 old_obp = obp;
3401 old_iln = ip->lineno;
3402 old_oln = op->lineno;
3404 else if (is_space[*ibp]) {
3405 *obp++ = *ibp++;
3406 if (ibp[-1] == '\n') {
3407 if (ip->macro == 0) {
3408 /* Newline in a file. Count it. */
3409 ++ip->lineno;
3410 ++op->lineno;
3411 } else if (!output_marks) {
3412 /* A newline mark, and we don't want marks
3413 in the output. If it is newline-hyphen,
3414 discard it entirely. Otherwise, it is
3415 newline-whitechar, so keep the whitechar. */
3416 obp--;
3417 if (*ibp == '-')
3418 ibp++;
3419 else {
3420 if (*ibp == '\n')
3421 ++op->lineno;
3422 *obp++ = *ibp++;
3424 } else {
3425 /* A newline mark; copy both chars to the output. */
3426 *obp++ = *ibp++;
3430 else if (ip->macro)
3431 break;
3432 else if (*ibp == '/') {
3433 /* If a comment, copy it unchanged or discard it. */
3434 if (ibp[1] == '\\')
3435 newline_fix (ibp + 1);
3436 if (ibp[1] == '*') {
3437 if (put_out_comments) {
3438 *obp++ = '/';
3439 *obp++ = '*';
3440 } else if (! traditional) {
3441 *obp++ = ' ';
3443 for (ibp += 2; ibp < limit; ibp++) {
3444 /* We need not worry about newline-marks,
3445 since they are never found in comments. */
3446 if (ibp[0] == '*') {
3447 if (ibp[1] == '\\')
3448 newline_fix (ibp + 1);
3449 if (ibp[1] == '/') {
3450 ibp += 2;
3451 if (put_out_comments) {
3452 *obp++ = '*';
3453 *obp++ = '/';
3455 break;
3458 else if (*ibp == '\n') {
3459 /* Newline in a file. Count it. */
3460 ++ip->lineno;
3461 ++op->lineno;
3463 else
3465 #ifdef MULTIBYTE_CHARS
3466 int length;
3467 length = local_mblen (ibp, limit - ibp);
3468 if (length > 1)
3470 if (put_out_comments)
3472 bcopy (ibp, obp, length - 1);
3473 obp += length - 1;
3475 ibp += (length - 1);
3477 #endif
3479 if (put_out_comments)
3480 *obp++ = *ibp;
3482 } else if (ibp[1] == '/' && cplusplus_comments) {
3483 if (put_out_comments) {
3484 *obp++ = '/';
3485 *obp++ = '/';
3486 } else if (! traditional) {
3487 *obp++ = ' ';
3489 for (ibp += 2; ; ibp++)
3491 if (*ibp == '\n')
3492 break;
3493 if (*ibp == '\\' && ibp[1] == '\n')
3495 if (put_out_comments)
3496 *obp++ = *ibp++;
3498 else
3500 #ifdef MULTIBYTE_CHARS
3501 int length;
3502 length = local_mblen (ibp, limit - ibp);
3503 if (length > 1)
3505 if (put_out_comments)
3507 bcopy (ibp, obp, length - 1);
3508 obp += length - 1;
3510 ibp += (length - 1);
3512 #endif
3514 if (put_out_comments)
3515 *obp++ = *ibp;
3517 } else
3518 break;
3520 else if (ibp[0] == '\\' && ibp[1] == '\n') {
3521 ibp += 2;
3522 ++ip->lineno;
3524 else break;
3526 if (*ibp != '(') {
3527 /* It isn't a macro call.
3528 Put back the space that we just skipped. */
3529 ibp = old_ibp;
3530 obp = old_obp;
3531 ip->lineno = old_iln;
3532 op->lineno = old_oln;
3533 /* Exit the for loop. */
3534 break;
3538 /* This is now known to be a macro call.
3539 Discard the macro name from the output,
3540 along with any following whitespace just copied,
3541 but preserve newlines if not outputting marks since this
3542 is more likely to do the right thing with line numbers. */
3543 obp = op->buf + obufp_before_macroname;
3544 if (output_marks)
3545 op->lineno = op_lineno_before_macroname;
3546 else {
3547 int newlines = op->lineno - op_lineno_before_macroname;
3548 while (0 < newlines--)
3549 *obp++ = '\n';
3552 /* Prevent accidental token-pasting with a character
3553 before the macro call. */
3554 if (!traditional && obp != op->buf) {
3555 switch (obp[-1]) {
3556 case '!': case '%': case '&': case '*':
3557 case '+': case '-': case '.': case '/':
3558 case ':': case '<': case '=': case '>':
3559 case '^': case '|':
3560 /* If we are expanding a macro arg, make a newline marker
3561 to separate the tokens. If we are making real output,
3562 a plain space will do. */
3563 if (output_marks)
3564 *obp++ = '\n';
3565 *obp++ = ' ';
3569 /* Expand the macro, reading arguments as needed,
3570 and push the expansion on the input stack. */
3571 ip->bufp = ibp;
3572 op->bufp = obp;
3573 macroexpand (hp, op);
3575 /* Reexamine input stack, since macroexpand has pushed
3576 a new level on it. */
3577 obp = op->bufp;
3578 RECACHE;
3579 break;
3581 hashcollision:
3583 } /* End hash-table-search loop */
3585 ident_length = hash = 0; /* Stop collecting identifier */
3586 redo_char = 0;
3587 concatenated = 0;
3588 } /* End if (ident_length > 0) */
3589 } /* End switch */
3590 } /* End per-char loop */
3592 /* Come here to return -- but first give an error message
3593 if there was an unterminated successful conditional. */
3594 ending:
3595 if (if_stack != ip->if_stack)
3597 const char *str;
3599 switch (if_stack->type)
3601 case T_IF:
3602 str = "if";
3603 break;
3604 case T_IFDEF:
3605 str = "ifdef";
3606 break;
3607 case T_IFNDEF:
3608 str = "ifndef";
3609 break;
3610 case T_ELSE:
3611 str = "else";
3612 break;
3613 case T_ELIF:
3614 str = "elif";
3615 break;
3616 default:
3617 abort ();
3620 error_with_line (line_for_error (if_stack->lineno),
3621 "unterminated `#%s' conditional", str);
3623 if_stack = ip->if_stack;
3627 * Rescan a string into a temporary buffer and return the result
3628 * as a FILE_BUF. Note this function returns a struct, not a pointer.
3630 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
3631 * and insert such markers when appropriate. See `rescan' for details.
3632 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
3633 * before substitution; it is 0 for other uses.
3635 static FILE_BUF
3636 expand_to_temp_buffer (buf, limit, output_marks, assertions)
3637 const U_CHAR *buf;
3638 const U_CHAR *limit;
3639 int output_marks, assertions;
3641 register FILE_BUF *ip;
3642 FILE_BUF obuf;
3643 int length = limit - buf;
3644 U_CHAR *buf1;
3645 int odepth = indepth;
3646 int save_assertions_flag = assertions_flag;
3648 assertions_flag = assertions;
3650 if (length < 0)
3651 abort ();
3653 /* Set up the input on the input stack. */
3655 buf1 = (U_CHAR *) alloca (length + 1);
3657 register const U_CHAR *p1 = buf;
3658 register U_CHAR *p2 = buf1;
3660 while (p1 != limit)
3661 *p2++ = *p1++;
3663 buf1[length] = 0;
3665 /* Set up to receive the output. */
3667 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
3668 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
3669 obuf.nominal_fname = 0;
3670 obuf.inc = 0;
3671 obuf.dir = 0;
3672 obuf.fname = 0;
3673 obuf.macro = 0;
3674 obuf.if_stack = 0;
3675 obuf.free_ptr = 0;
3676 obuf.system_header_p = 0;
3678 CHECK_DEPTH ({return obuf;});
3680 ++indepth;
3682 ip = &instack[indepth];
3683 ip->fname = 0;
3684 ip->nominal_fname = 0;
3685 ip->nominal_fname_len = 0;
3686 ip->inc = 0;
3687 ip->system_header_p = 0;
3688 ip->macro = 0;
3689 ip->free_ptr = 0;
3690 ip->length = length;
3691 ip->buf = ip->bufp = buf1;
3692 ip->if_stack = if_stack;
3694 ip->lineno = obuf.lineno = 1;
3696 /* Scan the input, create the output. */
3697 rescan (&obuf, output_marks);
3699 /* Pop input stack to original state. */
3700 --indepth;
3702 if (indepth != odepth)
3703 abort ();
3705 assertions_flag = save_assertions_flag;
3706 return obuf;
3710 * Process a # directive. Expects IP->bufp to point after the '#', as in
3711 * `#define foo bar'. Passes to the directive handler
3712 * (do_define, do_include, etc.): the addresses of the 1st and
3713 * last chars of the directive (starting immediately after the #
3714 * keyword), plus op and the keyword table pointer. If the directive
3715 * contains comments it is copied into a temporary buffer sans comments
3716 * and the temporary buffer is passed to the directive handler instead.
3717 * Likewise for backslash-newlines.
3719 * Returns nonzero if this was a known # directive.
3720 * Otherwise, returns zero, without advancing the input pointer.
3723 static int
3724 handle_directive (ip, op)
3725 FILE_BUF *ip, *op;
3727 register U_CHAR *bp, *cp;
3728 register struct directive *kt;
3729 register int ident_length;
3730 U_CHAR *resume_p;
3732 /* Nonzero means we must copy the entire directive
3733 to get rid of comments or backslash-newlines. */
3734 int copy_directive = 0;
3736 U_CHAR *ident, *after_ident;
3738 bp = ip->bufp;
3740 /* Record where the directive started. do_xifdef needs this. */
3741 directive_start = bp - 1;
3743 ignore_escape_flag = 1;
3745 /* Skip whitespace and \-newline. */
3746 while (1) {
3747 if (is_hor_space[*bp]) {
3748 if (*bp != ' ' && *bp != '\t' && pedantic)
3749 pedwarn_strange_white_space (*bp);
3750 bp++;
3751 } else if (*bp == '/') {
3752 if (bp[1] == '\\')
3753 newline_fix (bp + 1);
3754 if (! (bp[1] == '*' || (cplusplus_comments && bp[1] == '/')))
3755 break;
3756 ip->bufp = bp + 2;
3757 skip_to_end_of_comment (ip, &ip->lineno, 0);
3758 bp = ip->bufp;
3759 } else if (*bp == '\\' && bp[1] == '\n') {
3760 bp += 2; ip->lineno++;
3761 } else break;
3764 /* Now find end of directive name.
3765 If we encounter a backslash-newline, exchange it with any following
3766 symbol-constituents so that we end up with a contiguous name. */
3768 cp = bp;
3769 while (1) {
3770 if (is_idchar[*cp])
3771 cp++;
3772 else {
3773 if (*cp == '\\')
3774 name_newline_fix (cp);
3775 if (is_idchar[*cp])
3776 cp++;
3777 else break;
3780 ident_length = cp - bp;
3781 ident = bp;
3782 after_ident = cp;
3784 /* A line of just `#' becomes blank. */
3786 if (ident_length == 0 && *after_ident == '\n') {
3787 ip->bufp = after_ident;
3788 return 1;
3791 if (ident_length == 0 || !is_idstart[*ident]) {
3792 U_CHAR *p = ident;
3793 while (is_idchar[*p]) {
3794 if (*p < '0' || *p > '9')
3795 break;
3796 p++;
3798 /* Handle # followed by a line number. */
3799 if (p != ident && !is_idchar[*p]) {
3800 static struct directive line_directive_table[] = {
3801 { 4, do_line, "line", T_LINE},
3803 if (pedantic)
3804 pedwarn ("`#' followed by integer");
3805 after_ident = ident;
3806 kt = line_directive_table;
3807 ignore_escape_flag = 0;
3808 goto old_linenum;
3811 /* Avoid error for `###' and similar cases unless -pedantic. */
3812 if (p == ident) {
3813 while (*p == '#' || is_hor_space[*p]) p++;
3814 if (*p == '\n') {
3815 if (pedantic && !lang_asm)
3816 warning ("invalid preprocessing directive");
3817 return 0;
3821 if (!lang_asm)
3822 error ("invalid preprocessing directive name");
3824 return 0;
3828 * Decode the keyword and call the appropriate expansion
3829 * routine, after moving the input pointer up to the next line.
3831 for (kt = directive_table; kt->length > 0; kt++) {
3832 if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
3833 register U_CHAR *buf;
3834 register U_CHAR *limit;
3835 int unterminated;
3836 int junk;
3837 int *already_output;
3839 /* Nonzero means do not delete comments within the directive.
3840 #define needs this when -traditional. */
3841 int keep_comments;
3843 old_linenum:
3845 limit = ip->buf + ip->length;
3846 unterminated = 0;
3847 already_output = 0;
3848 keep_comments = traditional && kt->type == T_DEFINE;
3849 /* #import is defined only in Objective C, or when on the NeXT. */
3850 if (kt->type == T_IMPORT
3851 && !(objc || lookup ((const U_CHAR *) "__NeXT__", -1, -1)))
3852 break;
3854 /* Find the end of this directive (first newline not backslashed
3855 and not in a string or comment).
3856 Set COPY_DIRECTIVE if the directive must be copied
3857 (it contains a backslash-newline or a comment). */
3859 buf = bp = after_ident;
3860 while (bp < limit) {
3861 register U_CHAR c = *bp++;
3862 switch (c) {
3863 case '\\':
3864 if (*bp == '\n') {
3865 ip->lineno++;
3866 copy_directive = 1;
3867 bp++;
3868 } else if (traditional && bp < limit)
3869 bp++;
3870 break;
3872 case '"':
3873 /* "..." is special for #include. */
3874 if (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)) {
3875 while (bp < limit && *bp != '\n') {
3876 if (*bp == '"') {
3877 bp++;
3878 break;
3880 if (*bp == '\\' && bp[1] == '\n') {
3881 ip->lineno++;
3882 copy_directive = 1;
3883 bp++;
3885 bp++;
3887 break;
3889 /* Fall through. */
3890 case '\'':
3891 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_directive, &unterminated);
3892 /* Don't bother calling the directive if we already got an error
3893 message due to unterminated string. Skip everything and pretend
3894 we called the directive. */
3895 if (unterminated) {
3896 if (traditional) {
3897 /* Traditional preprocessing permits unterminated strings. */
3898 ip->bufp = bp;
3899 goto endloop1;
3901 ip->bufp = bp;
3902 return 1;
3904 break;
3906 /* <...> is special for #include. */
3907 case '<':
3908 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
3909 break;
3910 while (bp < limit && *bp != '>' && *bp != '\n') {
3911 if (*bp == '\\' && bp[1] == '\n') {
3912 ip->lineno++;
3913 copy_directive = 1;
3914 bp++;
3916 bp++;
3918 break;
3920 case '/':
3921 if (*bp == '\\')
3922 newline_fix (bp);
3923 if (*bp == '*'
3924 || (cplusplus_comments && *bp == '/')) {
3925 U_CHAR *obp = bp - 1;
3926 ip->bufp = bp + 1;
3927 skip_to_end_of_comment (ip, &ip->lineno, 0);
3928 bp = ip->bufp;
3929 /* No need to copy the directive because of a comment at the end;
3930 just don't include the comment in the directive. */
3931 if (!put_out_comments) {
3932 U_CHAR *p;
3933 for (p = bp; *p == ' ' || *p == '\t'; p++)
3934 continue;
3935 if (*p == '\n') {
3936 bp = obp;
3937 goto endloop1;
3940 /* Don't remove the comments if -traditional. */
3941 if (! keep_comments)
3942 copy_directive++;
3944 break;
3946 case '\f':
3947 case '\r':
3948 case '\v':
3949 if (pedantic)
3950 pedwarn_strange_white_space (c);
3951 break;
3953 case '\n':
3954 --bp; /* Point to the newline */
3955 ip->bufp = bp;
3956 goto endloop1;
3959 ip->bufp = bp;
3961 endloop1:
3962 resume_p = ip->bufp;
3963 /* BP is the end of the directive.
3964 RESUME_P is the next interesting data after the directive.
3965 A comment may come between. */
3967 /* If a directive should be copied through, and -C was given,
3968 pass it through before removing comments. */
3969 if (!no_output && put_out_comments
3970 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
3971 ? dump_macros == dump_definitions
3972 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
3973 : kt->type == T_PRAGMA)) {
3974 int len;
3976 /* Output directive name. */
3977 check_expand (op, kt->length + 2);
3978 /* Make sure # is at the start of a line */
3979 if (op->bufp > op->buf && op->bufp[-1] != '\n') {
3980 op->lineno++;
3981 *op->bufp++ = '\n';
3983 *op->bufp++ = '#';
3984 bcopy (kt->name, op->bufp, kt->length);
3985 op->bufp += kt->length;
3987 /* Output arguments. */
3988 len = (bp - buf);
3989 check_expand (op, len);
3990 bcopy (buf, (char *) op->bufp, len);
3991 op->bufp += len;
3992 /* Take account of any (escaped) newlines just output. */
3993 while (--len >= 0)
3994 if (buf[len] == '\n')
3995 op->lineno++;
3997 already_output = &junk;
3998 } /* Don't we need a newline or #line? */
4000 if (copy_directive) {
4001 register U_CHAR *xp = buf;
4002 /* Need to copy entire directive into temp buffer before dispatching */
4004 /* room for directive plus some slop */
4005 cp = (U_CHAR *) alloca (2 * (bp - buf) + 5);
4006 buf = cp;
4008 /* Copy to the new buffer, deleting comments
4009 and backslash-newlines (and whitespace surrounding the latter
4010 if outside of char and string constants). */
4012 while (xp < bp) {
4013 register U_CHAR c = *xp++;
4014 *cp++ = c;
4016 switch (c) {
4017 case '\n':
4018 abort (); /* A bare newline should never part of the line. */
4019 break;
4021 /* <...> is special for #include. */
4022 case '<':
4023 if (! IS_INCLUDE_DIRECTIVE_TYPE (kt->type))
4024 break;
4025 while (xp < bp && c != '>') {
4026 c = *xp++;
4027 if (c == '\\' && xp < bp && *xp == '\n')
4028 xp++;
4029 else
4030 *cp++ = c;
4032 break;
4034 case '\\':
4035 if (*xp == '\n') {
4036 xp++;
4037 cp--;
4038 if (cp != buf && is_hor_space[cp[-1]]) {
4039 while (cp - 1 != buf && is_hor_space[cp[-2]])
4040 cp--;
4041 SKIP_WHITE_SPACE (xp);
4042 } else if (is_hor_space[*xp]) {
4043 *cp++ = *xp++;
4044 SKIP_WHITE_SPACE (xp);
4046 } else if (traditional && xp < bp) {
4047 *cp++ = *xp++;
4049 break;
4051 case '\'':
4052 case '\"':
4054 int backslash_newlines_p;
4056 register const U_CHAR *bp1
4057 = skip_quoted_string (xp - 1, bp, ip->lineno,
4058 NULL_PTR, &backslash_newlines_p,
4059 NULL_PTR);
4060 if (backslash_newlines_p)
4061 while (xp != bp1)
4063 /* With something like:
4065 #define X "a\
4068 we should still remove the backslash-newline
4069 pair as part of phase two. */
4070 if (xp[0] == '\\' && xp[1] == '\n')
4071 xp += 2;
4072 else
4073 *cp++ = *xp++;
4075 else
4076 /* This is the same as the loop above, but taking
4077 advantage of the fact that we know there are no
4078 backslash-newline pairs. */
4079 while (xp != bp1)
4080 *cp++ = *xp++;
4082 break;
4084 case '/':
4085 if (*xp == '*'
4086 || (cplusplus_comments && *xp == '/')) {
4087 ip->bufp = xp + 1;
4088 /* If we already copied the directive through,
4089 already_output != 0 prevents outputting comment now. */
4090 skip_to_end_of_comment (ip, already_output, 0);
4091 if (keep_comments)
4092 while (xp != ip->bufp)
4093 *cp++ = *xp++;
4094 /* Delete or replace the slash. */
4095 else if (traditional)
4096 cp--;
4097 else
4098 cp[-1] = ' ';
4099 xp = ip->bufp;
4104 /* Null-terminate the copy. */
4106 *cp = 0;
4107 } else
4108 cp = bp;
4110 ip->bufp = resume_p;
4112 /* Some directives should be written out for cc1 to process,
4113 just as if they were not defined. And sometimes we're copying
4114 directives through. */
4116 if (!no_output && already_output == 0
4117 && ((kt->type == T_DEFINE || kt->type == T_UNDEF)
4118 ? (int) dump_names <= (int) dump_macros
4119 : IS_INCLUDE_DIRECTIVE_TYPE (kt->type) ? dump_includes
4120 : kt->type == T_PRAGMA)) {
4121 int len;
4123 /* Output directive name. */
4124 check_expand (op, kt->length + 1);
4125 *op->bufp++ = '#';
4126 bcopy (kt->name, (char *) op->bufp, kt->length);
4127 op->bufp += kt->length;
4129 if (kt->type == T_DEFINE && dump_macros == dump_names) {
4130 /* Output `#define name' only. */
4131 U_CHAR *xp = buf;
4132 U_CHAR *yp;
4133 SKIP_WHITE_SPACE (xp);
4134 yp = xp;
4135 while (is_idchar[*xp]) xp++;
4136 len = (xp - yp);
4137 check_expand (op, len + 1);
4138 *op->bufp++ = ' ';
4139 bcopy (yp, (char *) op->bufp, len);
4140 } else {
4141 /* Output entire directive. */
4142 len = (cp - buf);
4143 check_expand (op, len);
4144 bcopy (buf, (char *) op->bufp, len);
4146 op->bufp += len;
4149 /* Call the appropriate directive handler. buf now points to
4150 either the appropriate place in the input buffer, or to
4151 the temp buffer if it was necessary to make one. cp
4152 points to the first char after the contents of the (possibly
4153 copied) directive, in either case. */
4154 (*kt->func) (buf, cp, op, kt);
4155 check_expand (op, ip->length - (ip->bufp - ip->buf));
4157 return 1;
4161 /* It is deliberate that we don't warn about undefined directives.
4162 That is the responsibility of cc1. */
4163 return 0;
4166 static struct tm *
4167 timestamp ()
4169 static struct tm tmbuf;
4170 if (! tmbuf.tm_mday) {
4171 time_t t = time ((time_t *) 0);
4172 struct tm *tm = localtime (&t);
4173 if (tm)
4174 tmbuf = *tm;
4175 else {
4176 /* Use 0000-01-01 00:00:00 if local time is not available. */
4177 tmbuf.tm_year = -1900;
4178 tmbuf.tm_mday = 1;
4181 return &tmbuf;
4184 static const char * const monthnames[] = {
4185 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4186 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4190 * expand things like __FILE__. Place the expansion into the output
4191 * buffer *without* rescanning.
4194 static void
4195 special_symbol (hp, op)
4196 HASHNODE *hp;
4197 FILE_BUF *op;
4199 const char *buf;
4200 int i, len;
4201 int true_indepth;
4202 FILE_BUF *ip = NULL;
4203 struct tm *timebuf;
4205 int paren = 0; /* For special `defined' keyword */
4207 if (pcp_outfile && pcp_inside_if
4208 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
4209 error ("Predefined macro `%s' used inside `#if' during precompilation",
4210 hp->name);
4212 for (i = indepth; i >= 0; i--)
4213 if (instack[i].fname != NULL) {
4214 ip = &instack[i];
4215 break;
4217 if (ip == NULL) {
4218 error ("cccp error: not in any file?!");
4219 return; /* the show must go on */
4222 switch (hp->type) {
4223 case T_FILE:
4224 case T_BASE_FILE:
4226 FILE_BUF *p = hp->type == T_FILE ? ip : &instack[0];
4227 const char *string = p->nominal_fname;
4229 if (string)
4231 size_t string_len = p->nominal_fname_len;
4232 char *newbuf = (char *) alloca (3 + 4 * string_len);
4233 quote_string (newbuf, string, string_len);
4234 buf = newbuf;
4236 else
4237 buf = "\"\"";
4239 break;
4242 case T_INCLUDE_LEVEL:
4244 /* Eight bytes ought to be more than enough */
4245 char *newbuf = (char *) alloca (8);
4246 true_indepth = 0;
4247 for (i = indepth; i >= 0; i--)
4248 if (instack[i].fname != NULL)
4249 true_indepth++;
4250 sprintf (newbuf, "%d", true_indepth - 1);
4251 buf = newbuf;
4253 break;
4255 case T_VERSION:
4257 char *newbuf = (char *) alloca (3 + strlen (version_string));
4258 sprintf (newbuf, "\"%s\"", version_string);
4259 buf = newbuf;
4261 break;
4263 #ifndef NO_BUILTIN_SIZE_TYPE
4264 case T_SIZE_TYPE:
4265 buf = SIZE_TYPE;
4266 break;
4267 #endif
4269 #ifndef NO_BUILTIN_PTRDIFF_TYPE
4270 case T_PTRDIFF_TYPE:
4271 buf = PTRDIFF_TYPE;
4272 break;
4273 #endif
4275 case T_WCHAR_TYPE:
4276 buf = wchar_type;
4277 break;
4279 case T_USER_LABEL_PREFIX_TYPE:
4280 buf = user_label_prefix;
4281 break;
4283 case T_REGISTER_PREFIX_TYPE:
4284 buf = REGISTER_PREFIX;
4285 break;
4287 case T_IMMEDIATE_PREFIX_TYPE:
4288 buf = IMMEDIATE_PREFIX;
4289 break;
4291 case T_CONST:
4292 buf = hp->value.cpval;
4293 #ifdef STDC_0_IN_SYSTEM_HEADERS
4294 if (ip->system_header_p
4295 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
4296 && !lookup ((const U_CHAR *) "__STRICT_ANSI__", -1, -1))
4297 buf = "0";
4298 #endif
4299 if (pcp_inside_if && pcp_outfile)
4300 /* Output a precondition for this macro use */
4301 fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
4302 break;
4304 case T_SPECLINE:
4306 char *newbuf = (char *) alloca (10);
4307 sprintf (newbuf, "%d", ip->lineno);
4308 buf = newbuf;
4310 break;
4312 case T_DATE:
4313 case T_TIME:
4315 char *newbuf = (char *) alloca (20);
4316 timebuf = timestamp ();
4317 if (hp->type == T_DATE)
4318 sprintf (newbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
4319 timebuf->tm_mday, timebuf->tm_year + 1900);
4320 else
4321 sprintf (newbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour,
4322 timebuf->tm_min, timebuf->tm_sec);
4323 buf = newbuf;
4325 break;
4327 case T_SPEC_DEFINED:
4328 buf = " 0 "; /* Assume symbol is not defined */
4329 ip = &instack[indepth];
4330 SKIP_WHITE_SPACE (ip->bufp);
4331 if (*ip->bufp == '(') {
4332 paren++;
4333 ip->bufp++; /* Skip over the paren */
4334 SKIP_WHITE_SPACE (ip->bufp);
4337 if (!is_idstart[*ip->bufp])
4338 goto oops;
4339 if (ip->bufp[0] == 'L' && (ip->bufp[1] == '\'' || ip->bufp[1] == '"'))
4340 goto oops;
4341 if ((hp = lookup (ip->bufp, -1, -1))) {
4342 if (pcp_outfile && pcp_inside_if
4343 && (hp->type == T_CONST
4344 || (hp->type == T_MACRO && hp->value.defn->predefined)))
4345 /* Output a precondition for this macro use. */
4346 fprintf (pcp_outfile, "#define %s\n", hp->name);
4347 if (hp->type == T_POISON) {
4348 error("attempt to use poisoned `%s'.", hp->name);
4349 buf = " 0 ";
4350 } else {
4351 buf = " 1 ";
4354 else
4355 if (pcp_outfile && pcp_inside_if) {
4356 /* Output a precondition for this macro use */
4357 U_CHAR *cp = ip->bufp;
4358 fprintf (pcp_outfile, "#undef ");
4359 while (is_idchar[*cp]) /* Ick! */
4360 fputc (*cp++, pcp_outfile);
4361 putc ('\n', pcp_outfile);
4363 while (is_idchar[*ip->bufp])
4364 ++ip->bufp;
4365 SKIP_WHITE_SPACE (ip->bufp);
4366 if (paren) {
4367 if (*ip->bufp != ')')
4368 goto oops;
4369 ++ip->bufp;
4371 break;
4373 case T_POISON:
4374 error("attempt to use poisoned `%s'.", hp->name);
4375 buf = " 0 "; /* Consider poisoned symbol to not be defined */
4376 break;
4378 oops:
4380 error ("`defined' without an identifier");
4381 break;
4383 default:
4384 error ("cccp error: invalid special hash type"); /* time for gdb */
4385 abort ();
4387 len = strlen (buf);
4388 check_expand (op, len);
4389 bcopy (buf, (char *) op->bufp, len);
4390 op->bufp += len;
4392 return;
4396 /* Routines to handle #directives */
4398 /* Handle #include and #import.
4399 This function expects to see "fname" or <fname> on the input. */
4401 static int
4402 do_include (buf, limit, op, keyword)
4403 U_CHAR *buf, *limit;
4404 FILE_BUF *op;
4405 struct directive *keyword;
4407 const U_CHAR *importing =
4408 keyword->type == T_IMPORT ? (const U_CHAR *) "" : (const U_CHAR *) 0;
4409 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
4410 static int import_warning = 0;
4411 char *fname; /* Dynamically allocated fname buffer */
4412 char *pcftry;
4413 char *pcfname;
4414 char *fbeg, *fend; /* Beginning and end of fname */
4415 U_CHAR *fin;
4417 struct file_name_list *search_start = include; /* Chain of dirs to search */
4418 struct file_name_list *dsp; /* First in chain, if #include "..." */
4419 struct file_name_list *searchptr = 0;
4420 size_t flen;
4422 int f = -3; /* file number */
4423 struct include_file *inc = 0;
4425 int retried = 0; /* Have already tried macro
4426 expanding the include line*/
4427 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
4428 #ifdef VMS
4429 int vaxc_include = 0; /* 1 for token without punctuation */
4430 #endif
4431 int pcf = -1;
4432 char *pcfbuf;
4433 const char *pcfbuflimit;
4434 int pcfnum;
4436 if (pedantic && !instack[indepth].system_header_p)
4438 if (importing)
4439 pedwarn ("ANSI C does not allow `#import'");
4440 if (skip_dirs)
4441 pedwarn ("ANSI C does not allow `#include_next'");
4444 if (importing && warn_import && !inhibit_warnings
4445 && !instack[indepth].system_header_p && !import_warning) {
4446 import_warning = 1;
4447 warning ("using `#import' is not recommended");
4448 notice ("The fact that a certain header file need not be processed more than once\n\
4449 should be indicated in the header file, not where it is used.\n\
4450 The best way to do this is with a conditional of this form:\n\
4452 #ifndef _FOO_H_INCLUDED\n\
4453 #define _FOO_H_INCLUDED\n\
4454 ... <real contents of file> ...\n\
4455 #endif /* Not _FOO_H_INCLUDED */\n\
4457 Then users can use `#include' any number of times.\n\
4458 GNU C automatically avoids processing the file more than once\n\
4459 when it is equipped with such a conditional.\n");
4462 get_filename:
4464 fin = buf;
4465 SKIP_WHITE_SPACE (fin);
4466 /* Discard trailing whitespace so we can easily see
4467 if we have parsed all the significant chars we were given. */
4468 while (limit != fin && is_hor_space[limit[-1]]) limit--;
4469 fbeg = fend = (char *) alloca (limit - fin);
4471 switch (*fin++) {
4472 case '\"':
4474 FILE_BUF *fp;
4475 /* Copy the operand text, concatenating the strings. */
4477 for (;;) {
4478 for (;;) {
4479 if (fin == limit)
4480 goto invalid_include_file_name;
4481 *fend = *fin++;
4482 if (*fend == '"')
4483 break;
4484 fend++;
4486 if (fin == limit)
4487 break;
4488 /* If not at the end, there had better be another string. */
4489 /* Skip just horiz space, and don't go past limit. */
4490 while (fin != limit && is_hor_space[*fin]) fin++;
4491 if (fin != limit && *fin == '\"')
4492 fin++;
4493 else
4494 goto fail;
4498 /* We have "filename". Figure out directory this source
4499 file is coming from and put it on the front of the list. */
4501 /* If -I- was specified, don't search current dir, only spec'd ones. */
4502 if (ignore_srcdir) break;
4504 for (fp = &instack[indepth]; fp >= instack; fp--)
4506 int n;
4508 if ((fp->nominal_fname) != NULL) {
4509 char *nam;
4510 /* Found a named file. Figure out dir of the file,
4511 and put it in front of the search list. */
4512 dsp = ((struct file_name_list *)
4513 alloca (sizeof (struct file_name_list)
4514 + fp->nominal_fname_len));
4515 strcpy (dsp->fname, fp->nominal_fname);
4516 simplify_filename (dsp->fname);
4517 nam = base_name (dsp->fname);
4518 *nam = 0;
4519 #ifdef VMS
4520 /* for hack_vms_include_specification(), a local
4521 dir specification must start with "./" on VMS. */
4522 if (nam == dsp->fname)
4524 *nam++ = '.';
4525 *nam++ = '/';
4526 *nam = 0;
4528 #endif
4529 /* But for efficiency's sake, do not insert the dir
4530 if it matches the search list's first dir. */
4531 dsp->next = search_start;
4532 if (!search_start || strcmp (dsp->fname, search_start->fname)) {
4533 search_start = dsp;
4534 n = nam - dsp->fname;
4535 if (n + INCLUDE_LEN_FUDGE > max_include_len)
4536 max_include_len = n + INCLUDE_LEN_FUDGE;
4538 dsp[0].got_name_map = 0;
4539 break;
4542 break;
4545 case '<':
4546 while (fin != limit && *fin != '>')
4547 *fend++ = *fin++;
4548 if (*fin == '>' && fin + 1 == limit) {
4549 angle_brackets = 1;
4550 /* If -I-, start with the first -I dir after the -I-. */
4551 search_start = first_bracket_include;
4552 break;
4554 goto fail;
4556 default:
4557 #ifdef VMS
4559 * Support '#include xyz' like VAX-C to allow for easy use of all the
4560 * decwindow include files. It defaults to '#include <xyz.h>' (so the
4561 * code from case '<' is repeated here) and generates a warning.
4562 * (Note: macro expansion of `xyz' takes precedence.)
4564 /* Note: The argument of ISALPHA() can be evaluated twice, so do
4565 the pre-decrement outside of the macro. */
4566 if (retried && (--fin, ISALPHA(*(U_CHAR *) (fin)))) {
4567 while (fin != limit && (!ISSPACE(*fin)))
4568 *fend++ = *fin++;
4569 warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
4570 vaxc_include = 1;
4571 if (fin == limit) {
4572 angle_brackets = 1;
4573 /* If -I-, start with the first -I dir after the -I-. */
4574 search_start = first_bracket_include;
4575 break;
4578 #endif
4580 fail:
4581 if (! retried) {
4582 /* Expand buffer and then remove any newline markers.
4583 We can't just tell expand_to_temp_buffer to omit the markers,
4584 since it would put extra spaces in include file names. */
4585 U_CHAR *src;
4586 int errors_before_expansion = errors;
4587 FILE_BUF trybuf;
4589 trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
4590 if (errors != errors_before_expansion) {
4591 free (trybuf.buf);
4592 goto invalid_include_file_name;
4594 src = trybuf.buf;
4595 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
4596 limit = buf;
4597 while (src != trybuf.bufp) {
4598 switch ((*limit++ = *src++)) {
4599 case '\n':
4600 limit--;
4601 src++;
4602 break;
4604 case '\'':
4605 case '\"':
4607 const U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
4608 NULL_PTR, NULL_PTR, NULL_PTR);
4609 while (src != src1)
4610 *limit++ = *src++;
4612 break;
4615 *limit = 0;
4616 free (trybuf.buf);
4617 retried = 1;
4618 goto get_filename;
4621 invalid_include_file_name:
4622 error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
4623 return 0;
4626 /* For #include_next, skip in the search path
4627 past the dir in which the containing file was found. */
4628 if (skip_dirs) {
4629 FILE_BUF *fp;
4630 for (fp = &instack[indepth]; fp >= instack; fp--)
4631 if (fp->fname != NULL) {
4632 /* fp->dir is null if the containing file was specified
4633 with an absolute file name. In that case, don't skip anything. */
4634 if (fp->dir)
4635 search_start = fp->dir->next;
4636 break;
4640 *fend = 0;
4641 flen = simplify_filename (fbeg);
4643 if (flen == 0)
4645 error ("empty file name in `#%s'", keyword->name);
4646 return 0;
4649 /* Allocate this permanently, because it gets stored in the definitions
4650 of macros. */
4651 fname = xmalloc (max_include_len + flen + 1);
4652 /* + 1 above for terminating null. */
4654 system_include_depth += angle_brackets;
4656 /* If specified file name is absolute, just open it. */
4658 if (absolute_filename (fbeg)) {
4659 strcpy (fname, fbeg);
4660 f = open_include_file (fname, NULL_PTR, importing, &inc);
4661 } else {
4663 struct bypass_dir {
4664 struct bypass_dir *next;
4665 char *fname;
4666 struct file_name_list *searchptr;
4667 } **bypass_slot = 0;
4669 /* Search directory path, trying to open the file.
4670 Copy each filename tried into FNAME. */
4672 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
4674 if (searchptr == first_bracket_include) {
4675 /* Go to bypass directory if we know we've seen this file before. */
4676 static struct bypass_dir *bypass_hashtab[INCLUDE_HASHSIZE];
4677 struct bypass_dir *p;
4678 bypass_slot = &bypass_hashtab[hashf ((U_CHAR *) fbeg, flen,
4679 INCLUDE_HASHSIZE)];
4680 for (p = *bypass_slot; p; p = p->next)
4681 if (!strcmp (fbeg, p->fname)) {
4682 searchptr = p->searchptr;
4683 bypass_slot = 0;
4684 break;
4688 #ifdef VMS
4689 /* Change this 1/2 Unix 1/2 VMS file specification into a
4690 full VMS file specification */
4691 if (searchptr->fname[0])
4693 strcpy (fname, searchptr->fname);
4694 if (fname[strlen (fname) - 1] == ':')
4696 char *slashp;
4697 slashp = strchr (fbeg, '/');
4699 /* start at root-dir of logical device if no path given. */
4700 if (slashp == 0)
4701 strcat (fname, "[000000]");
4703 strcat (fname, fbeg);
4705 /* Fix up the filename */
4706 hack_vms_include_specification (fname, vaxc_include);
4708 else
4710 /* This is a normal VMS filespec, so use it unchanged. */
4711 strcpy (fname, fbeg);
4712 /* if it's '#include filename', add the missing .h */
4713 if (vaxc_include && index(fname,'.')==NULL)
4714 strcat (fname, ".h");
4716 #else
4717 strcpy (fname, searchptr->fname);
4718 strcat (fname, fbeg);
4719 #endif /* VMS */
4720 f = open_include_file (fname, searchptr, importing, &inc);
4721 if (f != -1) {
4722 if (bypass_slot && searchptr != first_bracket_include) {
4723 /* This is the first time we found this include file,
4724 and we found it after first_bracket_include.
4725 Record its location so that we can bypass to here next time. */
4726 struct bypass_dir *p
4727 = (struct bypass_dir *) xmalloc (sizeof (struct bypass_dir));
4728 p->next = *bypass_slot;
4729 p->fname = fname + strlen (searchptr->fname);
4730 p->searchptr = searchptr;
4731 *bypass_slot = p;
4733 break;
4735 #ifdef VMS
4736 /* Our VMS hacks can produce invalid filespecs, so don't worry
4737 about errors other than EACCES. */
4738 if (errno == EACCES)
4739 break;
4740 #else
4741 if (errno != ENOENT && errno != ENOTDIR)
4742 break;
4743 #endif
4748 if (f < 0) {
4750 if (f == -2) {
4751 /* The file was already included. */
4753 /* If generating dependencies and -MG was specified, we assume missing
4754 files are leaf files, living in the same directory as the source file
4755 or other similar place; these missing files may be generated from
4756 other files and may not exist yet (eg: y.tab.h). */
4757 } else if (print_deps_missing_files
4758 && (system_include_depth != 0) < print_deps)
4760 /* If it was requested as a system header file,
4761 then assume it belongs in the first place to look for such. */
4762 if (angle_brackets)
4764 if (search_start) {
4765 char *p = (char *) alloca (strlen (search_start->fname)
4766 + strlen (fbeg) + 1);
4767 strcpy (p, search_start->fname);
4768 strcat (p, fbeg);
4769 deps_output (p, ' ');
4772 else
4774 /* Otherwise, omit the directory, as if the file existed
4775 in the directory with the source. */
4776 deps_output (fbeg, ' ');
4779 /* If -M was specified, and this header file won't be added to the
4780 dependency list, then don't count this as an error, because we can
4781 still produce correct output. Otherwise, we can't produce correct
4782 output, because there may be dependencies we need inside the missing
4783 file, and we don't know what directory this missing file exists in. */
4784 else if (0 < print_deps && print_deps <= (system_include_depth != 0))
4785 warning ("No include path in which to find %s", fbeg);
4786 else if (f != -3)
4787 error_from_errno (fbeg);
4788 else
4789 error ("No include path in which to find %s", fbeg);
4791 } else {
4793 /* Actually process the file. */
4795 pcftry = (char *) alloca (strlen (fname) + 30);
4796 pcfbuf = 0;
4797 pcfnum = 0;
4799 if (!no_precomp)
4801 do {
4802 sprintf (pcftry, "%s%d", fname, pcfnum++);
4804 pcf = open (pcftry, O_RDONLY, 0666);
4805 if (pcf != -1)
4807 struct stat s;
4809 if (fstat (pcf, &s) != 0)
4810 pfatal_with_name (pcftry);
4811 if (! INO_T_EQ (inc->st.st_ino, s.st_ino)
4812 || inc->st.st_dev != s.st_dev)
4814 pcfbuf = check_precompiled (pcf, &s, fname, &pcfbuflimit);
4815 /* Don't need it any more. */
4816 close (pcf);
4818 else
4820 /* Don't need it at all. */
4821 close (pcf);
4822 break;
4825 } while (pcf != -1 && !pcfbuf);
4828 /* Actually process the file */
4829 if (pcfbuf) {
4830 pcfname = xstrdup (pcftry);
4831 pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) fname, op);
4833 else
4834 finclude (f, inc, op, is_system_include (fname), searchptr);
4837 system_include_depth -= angle_brackets;
4839 return 0;
4842 /* Return nonzero if the given FILENAME is an absolute pathname which
4843 designates a file within one of the known "system" include file
4844 directories. We assume here that if the given FILENAME looks like
4845 it is the name of a file which resides either directly in a "system"
4846 include file directory, or within any subdirectory thereof, then the
4847 given file must be a "system" include file. This function tells us
4848 if we should suppress pedantic errors/warnings for the given FILENAME.
4850 The value is 2 if the file is a C-language system header file
4851 for which C++ should (on most systems) assume `extern "C"'. */
4853 static int
4854 is_system_include (filename)
4855 register const char *filename;
4857 struct file_name_list *searchptr;
4859 for (searchptr = first_system_include; searchptr;
4860 searchptr = searchptr->next)
4861 if (! strncmp (searchptr->fname, filename, strlen (searchptr->fname)))
4862 return searchptr->c_system_include_path + 1;
4863 return 0;
4866 /* Yield the non-directory suffix of a file name. */
4868 static char *
4869 base_name (fname)
4870 const char *fname;
4872 const char *s = fname;
4873 const char *p;
4874 #if defined (__MSDOS__) || defined (_WIN32)
4875 if (ISALPHA (s[0]) && s[1] == ':') s += 2;
4876 #endif
4877 #ifdef VMS
4878 if ((p = rindex (s, ':'))) s = p + 1; /* Skip device. */
4879 if ((p = rindex (s, ']'))) s = p + 1; /* Skip directory. */
4880 if ((p = rindex (s, '>'))) s = p + 1; /* Skip alternate (int'n'l) dir. */
4881 if (s != fname)
4882 return (char *) s;
4883 #endif
4884 if ((p = rindex (s, '/'))) s = p + 1;
4885 #ifdef DIR_SEPARATOR
4886 if ((p = rindex (s, DIR_SEPARATOR))) s = p + 1;
4887 #endif
4888 return (char *) s;
4891 /* Yield nonzero if FILENAME is absolute (i.e. not relative). */
4893 static int
4894 absolute_filename (filename)
4895 const char *filename;
4897 #if defined (__MSDOS__) \
4898 || (defined (_WIN32) && !defined (__CYGWIN__) && !defined (_UWIN))
4899 if (ISALPHA (filename[0]) && filename[1] == ':') filename += 2;
4900 #endif
4901 #if defined (__CYGWIN__)
4902 /* At present, any path that begins with a drive spec is absolute. */
4903 if (ISALPHA (filename[0]) && filename[1] == ':') return 1;
4904 #endif
4905 #ifdef VMS
4906 if (index (filename, ':') != 0) return 1;
4907 #endif
4908 if (filename[0] == '/') return 1;
4909 #ifdef DIR_SEPARATOR
4910 if (filename[0] == DIR_SEPARATOR) return 1;
4911 #endif
4912 return 0;
4915 /* Returns whether or not a given character is a directory separator.
4916 Used by simplify_filename. */
4917 static inline int is_dir_separator PARAMS ((int));
4919 static inline
4921 is_dir_separator(ch)
4922 char ch;
4924 return (ch == DIR_SEPARATOR)
4925 #if defined (DIR_SEPARATOR_2)
4926 || (ch == DIR_SEPARATOR_2)
4927 #endif
4931 /* Remove unnecessary characters from FILENAME in place,
4932 to avoid unnecessary filename aliasing.
4933 Return the length of the resulting string.
4935 Do only the simplifications allowed by Posix.
4936 It is OK to miss simplifications on non-Posix hosts,
4937 since this merely leads to suboptimal results. */
4939 static size_t
4940 simplify_filename (filename)
4941 char *filename;
4943 register char *from = filename;
4944 register char *to = filename;
4945 char *to0;
4947 /* Remove redundant initial /s. */
4948 if (is_dir_separator (*from))
4950 *to++ = DIR_SEPARATOR;
4951 if (is_dir_separator (*++from))
4953 if (is_dir_separator (*++from))
4955 /* 3 or more initial /s are equivalent to 1 /. */
4956 while (is_dir_separator (*++from))
4957 continue;
4959 else
4961 /* On some hosts // differs from /; Posix allows this. */
4962 *to++ = DIR_SEPARATOR;
4967 to0 = to;
4969 for (;;)
4971 #ifndef VMS
4972 if (from[0] == '.' && from[1] == '/')
4973 from += 2;
4974 else
4975 #endif
4977 /* Copy this component and trailing DIR_SEPARATOR, if any. */
4978 while (!is_dir_separator (*to++ = *from++))
4980 if (!to[-1])
4982 /* Trim . component at end of nonempty name. */
4983 to -= filename <= to - 3 && to[-3] == DIR_SEPARATOR && to[-2] == '.';
4985 /* Trim unnecessary trailing /s. */
4986 while (to0 < --to && to[-1] == DIR_SEPARATOR)
4987 continue;
4989 *to = 0;
4990 return to - filename;
4993 #if defined(DIR_SEPARATOR_2)
4994 /* Simplify to one directory separator. */
4995 to[-1] = DIR_SEPARATOR;
4996 #endif
4999 /* Skip /s after a /. */
5000 while (is_dir_separator (*from))
5001 from++;
5005 /* The file_name_map structure holds a mapping of file names for a
5006 particular directory. This mapping is read from the file named
5007 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5008 map filenames on a file system with severe filename restrictions,
5009 such as DOS. The format of the file name map file is just a series
5010 of lines with two tokens on each line. The first token is the name
5011 to map, and the second token is the actual name to use. */
5013 struct file_name_map
5015 struct file_name_map *map_next;
5016 char *map_from;
5017 char *map_to;
5020 #define FILE_NAME_MAP_FILE "header.gcc"
5022 /* Read a space delimited string of unlimited length from a stdio
5023 file. */
5025 static char *
5026 read_filename_string (ch, f)
5027 int ch;
5028 FILE *f;
5030 char *alloc, *set;
5031 int len;
5033 len = 20;
5034 set = alloc = xmalloc (len + 1);
5035 if (! is_space[ch])
5037 *set++ = ch;
5038 while ((ch = getc (f)) != EOF && ! is_space[ch])
5040 if (set - alloc == len)
5042 len *= 2;
5043 alloc = xrealloc (alloc, len + 1);
5044 set = alloc + len / 2;
5046 *set++ = ch;
5049 *set = '\0';
5050 ungetc (ch, f);
5051 return alloc;
5054 /* Read the file name map file for DIRNAME.
5055 If DIRNAME is empty, read the map file for the working directory;
5056 otherwise DIRNAME must end in '/'. */
5058 static struct file_name_map *
5059 read_name_map (dirname)
5060 const char *dirname;
5062 /* This structure holds a linked list of file name maps, one per
5063 directory. */
5064 struct file_name_map_list
5066 struct file_name_map_list *map_list_next;
5067 char *map_list_name;
5068 struct file_name_map *map_list_map;
5070 static struct file_name_map_list *map_list;
5071 register struct file_name_map_list *map_list_ptr;
5072 char *name;
5073 FILE *f;
5074 size_t dirlen;
5076 for (map_list_ptr = map_list; map_list_ptr;
5077 map_list_ptr = map_list_ptr->map_list_next)
5078 if (! strcmp (map_list_ptr->map_list_name, dirname))
5079 return map_list_ptr->map_list_map;
5081 map_list_ptr = ((struct file_name_map_list *)
5082 xmalloc (sizeof (struct file_name_map_list)));
5083 map_list_ptr->map_list_name = xstrdup (dirname);
5084 map_list_ptr->map_list_map = NULL;
5086 dirlen = strlen (dirname);
5087 name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 1);
5088 strcpy (name, dirname);
5089 strcat (name, FILE_NAME_MAP_FILE);
5090 f = fopen (name, "r");
5091 if (!f)
5092 map_list_ptr->map_list_map = NULL;
5093 else
5095 int ch;
5097 while ((ch = getc (f)) != EOF)
5099 char *from, *to;
5100 struct file_name_map *ptr;
5101 size_t tolen;
5103 if (is_space[ch])
5104 continue;
5105 from = read_filename_string (ch, f);
5106 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5108 to = read_filename_string (ch, f);
5110 simplify_filename (from);
5111 tolen = simplify_filename (to);
5113 ptr = ((struct file_name_map *)
5114 xmalloc (sizeof (struct file_name_map)));
5115 ptr->map_from = from;
5117 /* Make the real filename absolute. */
5118 if (absolute_filename (to))
5119 ptr->map_to = to;
5120 else
5122 ptr->map_to = xmalloc (dirlen + tolen + 1);
5123 strcpy (ptr->map_to, dirname);
5124 strcat (ptr->map_to, to);
5125 free (to);
5128 ptr->map_next = map_list_ptr->map_list_map;
5129 map_list_ptr->map_list_map = ptr;
5131 while ((ch = getc (f)) != '\n')
5132 if (ch == EOF)
5133 break;
5135 fclose (f);
5138 map_list_ptr->map_list_next = map_list;
5139 map_list = map_list_ptr;
5141 return map_list_ptr->map_list_map;
5144 /* Try to open include file FILENAME. SEARCHPTR is the directory
5145 being tried from the include file search path.
5146 IMPORTING is "" if we are importing, null otherwise.
5147 Return -2 if found, either a matching name or a matching inode.
5148 Otherwise, open the file and return a file descriptor if successful
5149 or -1 if unsuccessful.
5150 Unless unsuccessful, put a descriptor of the included file into *PINC.
5151 This function maps filenames on file systems based on information read by
5152 read_name_map. */
5154 static int
5155 open_include_file (filename, searchptr, importing, pinc)
5156 char *filename;
5157 struct file_name_list *searchptr;
5158 const U_CHAR *importing;
5159 struct include_file **pinc;
5161 char *fname = remap ? remap_include_file (filename, searchptr) : filename;
5162 int fd = -2;
5164 /* Look up FNAME in include_hashtab. */
5165 struct include_file **phead = &include_hashtab[hashf ((U_CHAR *) fname,
5166 strlen (fname),
5167 INCLUDE_HASHSIZE)];
5168 struct include_file *inc, *head = *phead;
5169 for (inc = head; inc; inc = inc->next)
5170 if (!strcmp (fname, inc->fname))
5171 break;
5173 if (!inc
5174 || ! inc->control_macro
5175 || (inc->control_macro[0] && ! lookup (inc->control_macro, -1, -1))) {
5177 fd = open (fname, O_RDONLY, 0);
5179 if (fd < 0)
5181 #ifdef VMS
5182 /* if #include <dir/file> fails, try again with hacked spec. */
5183 if (!hack_vms_include_specification (fname, 0))
5184 return fd;
5185 fd = open (fname, O_RDONLY, 0);
5186 if (fd < 0)
5187 #endif
5188 return fd;
5191 if (!inc) {
5192 /* FNAME was not in include_hashtab; insert a new entry. */
5193 inc = (struct include_file *) xmalloc (sizeof (struct include_file));
5194 inc->next = head;
5195 inc->fname = fname;
5196 inc->control_macro = 0;
5197 inc->deps_output = 0;
5198 if (fstat (fd, &inc->st) != 0)
5199 pfatal_with_name (fname);
5200 *phead = inc;
5202 /* Look for another file with the same inode and device. */
5203 if (lookup_ino_include (inc)
5204 && inc->control_macro
5205 && (!inc->control_macro[0] || lookup (inc->control_macro, -1, -1))) {
5206 close (fd);
5207 fd = -2;
5211 /* For -M, add this file to the dependencies. */
5212 if (! inc->deps_output && (system_include_depth != 0) < print_deps) {
5213 inc->deps_output = 1;
5214 deps_output (fname, ' ');
5217 /* Handle -H option. */
5218 if (print_include_names)
5219 fprintf (stderr, "%*s%s\n", indepth, "", fname);
5222 if (importing)
5223 inc->control_macro = importing;
5225 *pinc = inc;
5226 return fd;
5229 /* Return the remapped name of the include file FILENAME.
5230 SEARCHPTR is the directory being tried from the include file path. */
5232 static char *
5233 remap_include_file (filename, searchptr)
5234 char *filename;
5235 struct file_name_list *searchptr;
5237 register struct file_name_map *map;
5238 register const char *from;
5240 if (searchptr)
5242 if (! searchptr->got_name_map)
5244 searchptr->name_map = read_name_map (searchptr->fname);
5245 searchptr->got_name_map = 1;
5248 /* Check the mapping for the directory we are using. */
5249 from = filename + strlen (searchptr->fname);
5250 for (map = searchptr->name_map; map; map = map->map_next)
5251 if (! strcmp (map->map_from, from))
5252 return map->map_to;
5255 from = base_name (filename);
5257 if (from != filename || !searchptr)
5259 /* Try to find a mapping file for the particular directory we are
5260 looking in. Thus #include <sys/types.h> will look up sys/types.h
5261 in /usr/include/header.gcc and look up types.h in
5262 /usr/include/sys/header.gcc. */
5264 char *dir = (char *) alloca (from - filename + 1);
5265 bcopy (filename, dir, from - filename);
5266 dir[from - filename] = '\0';
5268 for (map = read_name_map (dir); map; map = map->map_next)
5269 if (! strcmp (map->map_from, from))
5270 return map->map_to;
5273 return filename;
5276 /* Insert INC into the include file table, hashed by device and inode number.
5277 If a file with different name but same dev+ino was already in the table,
5278 return 1 and set INC's control macro to the already-known macro. */
5280 static int
5281 lookup_ino_include (inc)
5282 struct include_file *inc;
5284 int hash = ((unsigned) (inc->st.st_dev + INO_T_HASH (inc->st.st_ino))
5285 % INCLUDE_HASHSIZE);
5286 struct include_file *i = include_ino_hashtab[hash];
5287 inc->next_ino = i;
5288 include_ino_hashtab[hash] = inc;
5290 for (; i; i = i->next_ino)
5291 if (INO_T_EQ (inc->st.st_ino, i->st.st_ino)
5292 && inc->st.st_dev == i->st.st_dev) {
5293 inc->control_macro = i->control_macro;
5294 return 1;
5297 return 0;
5300 /* Process file descriptor F, which corresponds to include file INC,
5301 with output to OP.
5302 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5303 "system" include directories (as decided by the `is_system_include'
5304 function above).
5305 DIRPTR is the link in the dir path through which this file was found,
5306 or 0 if the file name was absolute. */
5308 static void
5309 finclude (f, inc, op, system_header_p, dirptr)
5310 int f;
5311 struct include_file *inc;
5312 FILE_BUF *op;
5313 int system_header_p;
5314 struct file_name_list *dirptr;
5316 char *fname = inc->fname;
5317 int i;
5318 FILE_BUF *fp; /* For input stack frame */
5319 int missing_newline = 0;
5321 CHECK_DEPTH (return;);
5323 fp = &instack[indepth + 1];
5324 bzero ((char *) fp, sizeof (FILE_BUF));
5325 fp->nominal_fname = fp->fname = fname;
5326 fp->nominal_fname_len = strlen (fname);
5327 fp->inc = inc;
5328 fp->length = 0;
5329 fp->lineno = 1;
5330 fp->if_stack = if_stack;
5331 fp->system_header_p = system_header_p;
5332 fp->dir = dirptr;
5334 if (S_ISREG (inc->st.st_mode)) {
5335 size_t s = (size_t) inc->st.st_size;
5336 if (s != inc->st.st_size || s + 2 < s)
5337 memory_full ();
5338 fp->buf = (U_CHAR *) xmalloc (s + 2);
5339 fp->bufp = fp->buf;
5341 /* Read the file contents, knowing that s is an upper bound
5342 on the number of bytes we can read. */
5343 fp->length = safe_read (f, (char *) fp->buf, s);
5344 if (fp->length < 0) goto nope;
5346 else if (S_ISDIR (inc->st.st_mode)) {
5347 error ("directory `%s' specified in #include", fname);
5348 close (f);
5349 return;
5350 } else {
5351 /* Cannot count its file size before reading.
5352 First read the entire file into heap and
5353 copy them into buffer on stack. */
5355 int bsize = 2000;
5356 int st_size = 0;
5358 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5360 for (;;) {
5361 i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
5362 if (i < 0)
5363 goto nope; /* error! */
5364 st_size += i;
5365 if (st_size != bsize)
5366 break; /* End of file */
5367 bsize *= 2;
5368 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5370 fp->bufp = fp->buf;
5371 fp->length = st_size;
5374 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5375 /* Backslash-newline at end is not good enough. */
5376 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5377 fp->buf[fp->length++] = '\n';
5378 missing_newline = 1;
5380 fp->buf[fp->length] = '\0';
5382 /* Close descriptor now, so nesting does not use lots of descriptors. */
5383 close (f);
5385 /* Must do this before calling trigraph_pcp, so that the correct file name
5386 will be printed in warning messages. */
5388 indepth++;
5389 input_file_stack_tick++;
5391 if (!no_trigraphs)
5392 trigraph_pcp (fp);
5394 if (warn_white_space)
5395 check_white_space (fp);
5397 output_line_directive (fp, op, 0, enter_file);
5398 rescan (op, 0);
5400 if (missing_newline)
5401 fp->lineno--;
5403 if (pedantic && missing_newline)
5404 pedwarn ("file does not end in newline");
5406 indepth--;
5407 input_file_stack_tick++;
5408 output_line_directive (&instack[indepth], op, 0, leave_file);
5409 free (fp->buf);
5410 return;
5412 nope:
5414 perror_with_name (fname);
5415 close (f);
5416 free (fp->buf);
5419 /* Record that inclusion of the include file INC
5420 should be controlled by the macro named MACRO_NAME.
5421 This means that trying to include the file again
5422 will do something if that macro is defined. */
5424 static void
5425 record_control_macro (inc, macro_name)
5426 struct include_file *inc;
5427 const U_CHAR *macro_name;
5429 if (!inc->control_macro || inc->control_macro[0])
5430 inc->control_macro = macro_name;
5433 /* Load the specified precompiled header into core, and verify its
5434 preconditions. PCF indicates the file descriptor to read, which must
5435 be a regular file. *ST is its file status.
5436 FNAME indicates the file name of the original header.
5437 *LIMIT will be set to an address one past the end of the file.
5438 If the preconditions of the file are not satisfied, the buffer is
5439 freed and we return 0. If the preconditions are satisfied, return
5440 the address of the buffer following the preconditions. The buffer, in
5441 this case, should never be freed because various pieces of it will
5442 be referred to until all precompiled strings are output at the end of
5443 the run. */
5445 static char *
5446 check_precompiled (pcf, st, fname, limit)
5447 int pcf;
5448 struct stat *st;
5449 const char *fname ATTRIBUTE_UNUSED;
5450 const char **limit;
5452 int length = 0;
5453 char *buf;
5454 char *cp;
5456 if (pcp_outfile)
5457 return 0;
5459 if (S_ISREG (st->st_mode))
5461 size_t s = (size_t) st->st_size;
5462 if (s != st->st_size || s + 2 < s)
5463 memory_full ();
5464 buf = xmalloc (s + 2);
5465 length = safe_read (pcf, buf, s);
5466 if (length < 0)
5467 goto nope;
5469 else
5470 abort ();
5472 if (length > 0 && buf[length-1] != '\n')
5473 buf[length++] = '\n';
5474 buf[length] = '\0';
5476 *limit = buf + length;
5478 /* File is in core. Check the preconditions. */
5479 if (!check_preconditions (buf))
5480 goto nope;
5481 for (cp = buf; *cp; cp++)
5483 #ifdef DEBUG_PCP
5484 fprintf (stderr, "Using preinclude %s\n", fname);
5485 #endif
5486 return cp + 1;
5488 nope:
5489 #ifdef DEBUG_PCP
5490 fprintf (stderr, "Cannot use preinclude %s\n", fname);
5491 #endif
5492 free (buf);
5493 return 0;
5496 /* PREC (null terminated) points to the preconditions of a
5497 precompiled header. These are a series of #define and #undef
5498 lines which must match the current contents of the hash
5499 table. */
5501 static int
5502 check_preconditions (prec)
5503 const char *prec;
5505 MACRODEF mdef;
5506 const char *lineend;
5508 while (*prec) {
5509 lineend = index (prec, '\n');
5511 if (*prec++ != '#') {
5512 error ("Bad format encountered while reading precompiled file");
5513 return 0;
5515 if (!strncmp (prec, "define", 6)) {
5516 HASHNODE *hp;
5518 prec += 6;
5519 mdef = create_definition ((const U_CHAR *) prec,
5520 (const U_CHAR *) lineend, NULL_PTR);
5522 if (mdef.defn == 0)
5523 abort ();
5525 if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
5526 || (hp->type != T_MACRO && hp->type != T_CONST)
5527 || (hp->type == T_MACRO
5528 && !compare_defs (mdef.defn, hp->value.defn)
5529 && (mdef.defn->length != 2
5530 || mdef.defn->expansion[0] != '\n'
5531 || mdef.defn->expansion[1] != ' ')))
5532 return 0;
5533 } else if (!strncmp (prec, "undef", 5)) {
5534 const char *name;
5535 int len;
5537 prec += 5;
5538 while (is_hor_space[(U_CHAR) *prec])
5539 prec++;
5540 name = prec;
5541 while (is_idchar[(U_CHAR) *prec])
5542 prec++;
5543 len = prec - name;
5545 if (lookup ((const U_CHAR *) name, len, -1))
5546 return 0;
5547 } else {
5548 error ("Bad format encountered while reading precompiled file");
5549 return 0;
5551 prec = lineend + 1;
5553 /* They all passed successfully */
5554 return 1;
5557 /* Process the main body of a precompiled file. BUF points to the
5558 string section of the file, following the preconditions. LIMIT is one
5559 character past the end. NAME is the name of the file being read
5560 in. OP is the main output buffer. */
5562 static void
5563 pcfinclude (buf, name, op)
5564 U_CHAR *buf;
5565 const U_CHAR *name;
5566 FILE_BUF *op;
5568 FILE_BUF tmpbuf;
5569 int nstrings;
5570 U_CHAR *cp = buf;
5572 /* First in the file comes 4 bytes indicating the number of strings, */
5573 /* in network byte order. (MSB first). */
5574 nstrings = *cp++;
5575 nstrings = (nstrings << 8) | *cp++;
5576 nstrings = (nstrings << 8) | *cp++;
5577 nstrings = (nstrings << 8) | *cp++;
5579 /* Looping over each string... */
5580 while (nstrings--) {
5581 U_CHAR *string_start;
5582 U_CHAR *endofthiskey;
5583 STRINGDEF *str;
5584 int nkeys;
5586 /* Each string starts with a STRINGDEF structure (str), followed */
5587 /* by the text of the string (string_start) */
5589 /* First skip to a longword boundary */
5590 /* ??? Why a 4-byte boundary? On all machines? */
5591 /* NOTE: This works correctly even if size_t
5592 is narrower than a pointer.
5593 Do not try risky measures here to get another type to use!
5594 Do not include stddef.h--it will fail! */
5595 if ((size_t) cp & 3)
5596 cp += 4 - ((size_t) cp & 3);
5598 /* Now get the string. */
5599 str = (STRINGDEF *) (PTR) cp;
5600 string_start = cp += sizeof (STRINGDEF);
5602 for (; *cp; cp++) /* skip the string */
5605 /* We need to macro expand the string here to ensure that the
5606 proper definition environment is in place. If it were only
5607 expanded when we find out it is needed, macros necessary for
5608 its proper expansion might have had their definitions changed. */
5609 tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
5610 /* Lineno is already set in the precompiled file */
5611 str->contents = tmpbuf.buf;
5612 str->len = tmpbuf.bufp - tmpbuf.buf;
5613 str->writeflag = 0;
5614 str->filename = name;
5615 str->output_mark = outbuf.bufp - outbuf.buf;
5617 str->chain = 0;
5618 *stringlist_tailp = str;
5619 stringlist_tailp = &str->chain;
5621 /* Next comes a fourbyte number indicating the number of keys
5622 for this string. */
5623 nkeys = *cp++;
5624 nkeys = (nkeys << 8) | *cp++;
5625 nkeys = (nkeys << 8) | *cp++;
5626 nkeys = (nkeys << 8) | *cp++;
5628 /* If this number is -1, then the string is mandatory. */
5629 if (nkeys == -1)
5630 str->writeflag = 1;
5631 else
5632 /* Otherwise, for each key, */
5633 for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
5634 KEYDEF *kp = (KEYDEF *) (PTR) cp;
5635 HASHNODE *hp;
5636 U_CHAR *bp;
5638 /* It starts with a KEYDEF structure */
5639 cp += sizeof (KEYDEF);
5641 /* Find the end of the key. At the end of this for loop we
5642 advance CP to the start of the next key using this variable. */
5643 endofthiskey = cp + strlen ((char *) cp);
5644 kp->str = str;
5646 /* Expand the key, and enter it into the hash table. */
5647 tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
5648 bp = tmpbuf.buf;
5650 while (is_hor_space[*bp])
5651 bp++;
5652 if (!is_idstart[*bp] || bp == tmpbuf.bufp) {
5653 str->writeflag = 1;
5654 continue;
5657 hp = lookup (bp, -1, -1);
5658 if (hp == NULL) {
5659 kp->chain = 0;
5660 install (bp, -1, T_PCSTRING, (char *) kp, -1);
5662 else if (hp->type == T_PCSTRING) {
5663 kp->chain = hp->value.keydef;
5664 hp->value.keydef = kp;
5666 else
5667 str->writeflag = 1;
5670 /* This output_line_directive serves to switch us back to the current
5671 input file in case some of these strings get output (which will
5672 result in line directives for the header file being output). */
5673 output_line_directive (&instack[indepth], op, 0, enter_file);
5676 /* Called from rescan when it hits a key for strings. Mark them all
5677 used and clean up. */
5679 static void
5680 pcstring_used (hp)
5681 HASHNODE *hp;
5683 KEYDEF *kp;
5685 for (kp = hp->value.keydef; kp; kp = kp->chain)
5686 kp->str->writeflag = 1;
5687 delete_macro (hp);
5690 /* Write the output, interspersing precompiled strings in their
5691 appropriate places. */
5693 static void
5694 write_output ()
5696 STRINGDEF *next_string;
5697 U_CHAR *cur_buf_loc;
5698 int line_directive_len = 80;
5699 char *line_directive = xmalloc (line_directive_len);
5700 int len;
5702 /* In each run through the loop, either cur_buf_loc ==
5703 next_string_loc, in which case we print a series of strings, or
5704 it is less than next_string_loc, in which case we write some of
5705 the buffer. */
5706 cur_buf_loc = outbuf.buf;
5707 next_string = stringlist;
5709 while (cur_buf_loc < outbuf.bufp || next_string) {
5710 if (next_string
5711 && cur_buf_loc - outbuf.buf == next_string->output_mark) {
5712 if (next_string->writeflag) {
5713 len = 4 * strlen ((const char *) next_string->filename) + 32;
5714 while (len > line_directive_len)
5715 line_directive = xrealloc (line_directive,
5716 line_directive_len *= 2);
5717 sprintf (line_directive, "\n# %d ", next_string->lineno);
5718 strcpy (quote_string (line_directive + strlen (line_directive),
5719 (const char *) next_string->filename,
5720 strlen ((const char *) next_string->filename)),
5721 "\n");
5722 safe_write (fileno (stdout), line_directive, strlen (line_directive));
5723 safe_write (fileno (stdout),
5724 (char *) next_string->contents, next_string->len);
5726 next_string = next_string->chain;
5728 else {
5729 len = (next_string
5730 ? (next_string->output_mark
5731 - (cur_buf_loc - outbuf.buf))
5732 : outbuf.bufp - cur_buf_loc);
5734 safe_write (fileno (stdout), (char *) cur_buf_loc, len);
5735 cur_buf_loc += len;
5738 free (line_directive);
5741 /* Pass a directive through to the output file.
5742 BUF points to the contents of the directive, as a contiguous string.
5743 LIMIT points to the first character past the end of the directive.
5744 KEYWORD is the keyword-table entry for the directive. */
5746 static void
5747 pass_thru_directive (buf, limit, op, keyword)
5748 const U_CHAR *buf;
5749 const U_CHAR *limit;
5750 FILE_BUF *op;
5751 struct directive *keyword;
5753 register int keyword_length = keyword->length;
5755 check_expand (op, 1 + keyword_length + (limit - buf));
5756 *op->bufp++ = '#';
5757 bcopy (keyword->name, (char *) op->bufp, keyword_length);
5758 op->bufp += keyword_length;
5759 if (limit != buf && buf[0] != ' ')
5760 *op->bufp++ = ' ';
5761 bcopy ((const PTR) buf, (PTR) op->bufp, limit - buf);
5762 op->bufp += (limit - buf);
5763 #if 0
5764 *op->bufp++ = '\n';
5765 /* Count the line we have just made in the output,
5766 to get in sync properly. */
5767 op->lineno++;
5768 #endif
5771 /* The arglist structure is built by do_define to tell
5772 collect_definition where the argument names begin. That
5773 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
5774 would contain pointers to the strings x, y, and z.
5775 Collect_definition would then build a DEFINITION node,
5776 with reflist nodes pointing to the places x, y, and z had
5777 appeared. So the arglist is just convenience data passed
5778 between these two routines. It is not kept around after
5779 the current #define has been processed and entered into the
5780 hash table. */
5782 struct arglist {
5783 struct arglist *next;
5784 const U_CHAR *name;
5785 int length;
5786 int argno;
5787 char rest_args;
5790 /* Create a DEFINITION node from a #define directive. Arguments are
5791 as for do_define. */
5793 static MACRODEF
5794 create_definition (buf, limit, op)
5795 const U_CHAR *buf, *limit;
5796 FILE_BUF *op;
5798 const U_CHAR *bp; /* temp ptr into input buffer */
5799 const U_CHAR *symname; /* remember where symbol name starts */
5800 int sym_length; /* and how long it is */
5801 int line = instack[indepth].lineno;
5802 const char *file = instack[indepth].nominal_fname;
5803 size_t file_len = instack[indepth].nominal_fname_len;
5804 int rest_args = 0;
5806 DEFINITION *defn;
5807 int arglengths = 0; /* Accumulate lengths of arg names
5808 plus number of args. */
5809 MACRODEF mdef;
5811 bp = buf;
5813 while (is_hor_space[*bp])
5814 bp++;
5816 symname = bp; /* remember where it starts */
5817 sym_length = check_macro_name (bp, 0);
5818 bp += sym_length;
5820 /* Lossage will occur if identifiers or control keywords are broken
5821 across lines using backslash. This is not the right place to take
5822 care of that. */
5824 if (*bp == '(') {
5825 struct arglist *arg_ptrs = NULL;
5826 int argno = 0;
5828 bp++; /* skip '(' */
5829 SKIP_WHITE_SPACE (bp);
5831 /* Loop over macro argument names. */
5832 while (*bp != ')') {
5833 struct arglist *temp;
5835 temp = (struct arglist *) alloca (sizeof (struct arglist));
5836 temp->name = bp;
5837 temp->next = arg_ptrs;
5838 temp->argno = argno++;
5839 temp->rest_args = 0;
5840 arg_ptrs = temp;
5842 if (rest_args)
5843 pedwarn ("another parameter follows `%s'",
5844 rest_extension);
5846 if (!is_idstart[*bp])
5848 if (c99 && limit - bp > (long) REST_EXTENSION_LENGTH
5849 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)
5851 /* This is the ISO C 99 way to write macros with variable
5852 number of arguments. */
5853 rest_args = 1;
5854 temp->rest_args = 1;
5856 else
5857 pedwarn ("invalid character in macro parameter name");
5860 /* Find the end of the arg name. */
5861 while (is_idchar[*bp]) {
5862 bp++;
5863 /* do we have a "special" rest-args extension here? */
5864 if (limit - bp > (long) REST_EXTENSION_LENGTH
5865 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
5866 if (pedantic && !instack[indepth].system_header_p)
5867 pedwarn ("ANSI C does not allow macro with variable arguments");
5868 rest_args = 1;
5869 temp->rest_args = 1;
5870 break;
5873 if (bp == temp->name && rest_args == 1)
5875 /* This is the ISO C 99 style. */
5876 temp->name = (U_CHAR *) va_args_name;
5877 temp->length = VA_ARGS_NAME_LENGTH;
5879 else
5880 temp->length = bp - temp->name;
5881 if (rest_args == 1)
5882 bp += REST_EXTENSION_LENGTH;
5883 arglengths += temp->length + 2;
5884 SKIP_WHITE_SPACE (bp);
5885 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
5886 error ("badly punctuated parameter list in `#define'");
5887 goto nope;
5889 if (*bp == ',') {
5890 bp++;
5891 SKIP_WHITE_SPACE (bp);
5892 /* A comma at this point can only be followed by an identifier. */
5893 if (!is_idstart[*bp]
5894 && !(c99 && limit - bp > (long) REST_EXTENSION_LENGTH
5895 && bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0)) {
5896 error ("badly punctuated parameter list in `#define'");
5897 goto nope;
5900 if (bp >= limit) {
5901 error ("unterminated parameter list in `#define'");
5902 goto nope;
5905 struct arglist *otemp;
5907 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
5908 if (temp->length == otemp->length
5909 && bcmp (temp->name, otemp->name, temp->length) == 0)
5911 error ("duplicate argument name `%.*s' in `#define'",
5912 temp->length, temp->name);
5913 goto nope;
5915 if (rest_args == 0 && temp->length == VA_ARGS_NAME_LENGTH
5916 && bcmp (temp->name, va_args_name, VA_ARGS_NAME_LENGTH) == 0)
5918 error ("\
5919 reserved name `%s' used as argument name in `#define'", va_args_name);
5920 goto nope;
5925 ++bp; /* skip paren */
5926 SKIP_WHITE_SPACE (bp);
5927 /* now everything from bp before limit is the definition. */
5928 defn = collect_expansion (bp, limit, argno, arg_ptrs);
5929 defn->rest_args = rest_args;
5931 /* Now set defn->args.argnames to the result of concatenating
5932 the argument names in reverse order
5933 with comma-space between them. */
5934 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
5936 struct arglist *temp;
5937 int i = 0;
5938 for (temp = arg_ptrs; temp; temp = temp->next) {
5939 bcopy (temp->name, &defn->args.argnames[i], temp->length);
5940 i += temp->length;
5941 if (temp->next != 0) {
5942 defn->args.argnames[i++] = ',';
5943 defn->args.argnames[i++] = ' ';
5946 defn->args.argnames[i] = 0;
5948 } else {
5949 /* Simple expansion or empty definition. */
5951 if (bp < limit)
5953 if (is_hor_space[*bp]) {
5954 bp++;
5955 SKIP_WHITE_SPACE (bp);
5956 } else if (sym_length) {
5957 switch (*bp) {
5958 case '!': case '"': case '#': case '%': case '&': case '\'':
5959 case ')': case '*': case '+': case ',': case '-': case '.':
5960 case '/': case ':': case ';': case '<': case '=': case '>':
5961 case '?': case '[': case '\\': case ']': case '^': case '{':
5962 case '|': case '}': case '~':
5963 warning ("missing white space after `#define %.*s'",
5964 sym_length, symname);
5965 break;
5967 default:
5968 pedwarn ("missing white space after `#define %.*s'",
5969 sym_length, symname);
5970 break;
5974 /* Now everything from bp before limit is the definition. */
5975 defn = collect_expansion (bp, limit, -1, NULL_PTR);
5976 defn->args.argnames = (U_CHAR *) "";
5979 defn->line = line;
5980 defn->file = file;
5981 defn->file_len = file_len;
5983 /* OP is null if this is a predefinition */
5984 defn->predefined = !op;
5985 mdef.defn = defn;
5986 mdef.symnam = symname;
5987 mdef.symlen = sym_length;
5989 return mdef;
5991 nope:
5992 mdef.defn = 0;
5993 return mdef;
5996 /* Process a #define directive.
5997 BUF points to the contents of the #define directive, as a contiguous string.
5998 LIMIT points to the first character past the end of the definition.
5999 KEYWORD is the keyword-table entry for #define. */
6001 static int
6002 do_define (buf, limit, op, keyword)
6003 U_CHAR *buf, *limit;
6004 FILE_BUF *op;
6005 struct directive *keyword;
6007 int hashcode;
6008 MACRODEF mdef;
6009 enum node_type newtype = keyword->type == T_DEFINE ? T_MACRO : T_POISON;
6011 /* If this is a precompiler run (with -pcp) pass thru #define directives. */
6012 if (pcp_outfile && op)
6013 pass_thru_directive (buf, limit, op, keyword);
6015 mdef = create_definition (buf, limit, op);
6016 if (mdef.defn == 0)
6017 goto nope;
6019 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
6022 HASHNODE *hp;
6023 if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
6024 int ok = 0;
6025 /* Redefining a precompiled key is ok. */
6026 if (hp->type == T_PCSTRING)
6027 ok = 1;
6028 /* Redefining a poisoned identifier is even worse than `not ok'. */
6029 else if (hp->type == T_POISON)
6030 ok = -1;
6031 /* Poisoning anything else is not ok.
6032 The poison should always come first. */
6033 else if (newtype == T_POISON)
6034 ok = 0;
6035 /* Redefining a macro is ok if the definitions are the same. */
6036 else if (hp->type == T_MACRO)
6037 ok = ! compare_defs (mdef.defn, hp->value.defn);
6038 /* Redefining a constant is ok with -D. */
6039 else if (hp->type == T_CONST)
6040 ok = ! done_initializing;
6042 /* Print the warning or error if it's not ok. */
6043 if (ok <= 0)
6045 /* If we are passing through #define and #undef directives, do
6046 that for this re-definition now. */
6047 if (debug_output && op)
6048 pass_thru_directive (buf, limit, op, keyword);
6050 if (hp->type == T_POISON)
6051 error ("redefining poisoned `%.*s'", mdef.symlen, mdef.symnam);
6052 else
6053 pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
6054 if (hp->type == T_MACRO)
6055 pedwarn_with_file_and_line (hp->value.defn->file,
6056 hp->value.defn->file_len,
6057 hp->value.defn->line,
6058 "this is the location of the previous definition");
6060 if (hp->type != T_POISON)
6062 /* Replace the old definition. */
6063 hp->type = newtype;
6064 hp->value.defn = mdef.defn;
6066 } else {
6067 /* If we are passing through #define and #undef directives, do
6068 that for this new definition now. */
6069 if (debug_output && op)
6070 pass_thru_directive (buf, limit, op, keyword);
6071 install (mdef.symnam, mdef.symlen, newtype,
6072 (char *) mdef.defn, hashcode);
6076 return 0;
6078 nope:
6080 return 1;
6083 /* Check a purported macro name SYMNAME, and yield its length.
6084 ASSERTION is nonzero if this is really for an assertion name. */
6086 static int
6087 check_macro_name (symname, assertion)
6088 const U_CHAR *symname;
6089 int assertion;
6091 const U_CHAR *p;
6092 int sym_length;
6094 for (p = symname; is_idchar[*p]; p++)
6096 sym_length = p - symname;
6097 if (sym_length == 0
6098 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
6100 if (assertion)
6101 error ("invalid assertion name");
6102 else
6103 error ("invalid macro name");
6105 else if (!is_idstart[*symname]
6106 || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
6108 if (assertion)
6109 error ("invalid assertion name `%.*s'", sym_length, symname);
6110 else
6111 error ("invalid macro name `%.*s'", sym_length, symname);
6113 return sym_length;
6116 /* Return zero if two DEFINITIONs are isomorphic. */
6118 static int
6119 compare_defs (d1, d2)
6120 DEFINITION *d1, *d2;
6122 register struct reflist *a1, *a2;
6123 register U_CHAR *p1 = d1->expansion;
6124 register U_CHAR *p2 = d2->expansion;
6125 int first = 1;
6127 if (d1->nargs != d2->nargs)
6128 return 1;
6129 if (pedantic
6130 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
6131 return 1;
6132 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
6133 a1 = a1->next, a2 = a2->next) {
6134 if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
6135 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
6136 || a1->argno != a2->argno
6137 || a1->stringify != a2->stringify
6138 || a1->raw_before != a2->raw_before
6139 || a1->raw_after != a2->raw_after)
6140 return 1;
6141 first = 0;
6142 p1 += a1->nchars;
6143 p2 += a2->nchars;
6145 if (a1 != a2)
6146 return 1;
6147 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
6148 p2, d2->length - (p2 - d2->expansion), 1))
6149 return 1;
6150 return 0;
6153 /* Return 1 if two parts of two macro definitions are effectively different.
6154 One of the parts starts at BEG1 and has LEN1 chars;
6155 the other has LEN2 chars at BEG2.
6156 Any sequence of whitespace matches any other sequence of whitespace.
6157 FIRST means these parts are the first of a macro definition;
6158 so ignore leading whitespace entirely.
6159 LAST means these parts are the last of a macro definition;
6160 so ignore trailing whitespace entirely. */
6162 static int
6163 comp_def_part (first, beg1, len1, beg2, len2, last)
6164 int first;
6165 const U_CHAR *beg1, *beg2;
6166 int len1, len2;
6167 int last;
6169 register const U_CHAR *end1 = beg1 + len1;
6170 register const U_CHAR *end2 = beg2 + len2;
6171 if (first) {
6172 while (beg1 != end1 && is_space[*beg1]) beg1++;
6173 while (beg2 != end2 && is_space[*beg2]) beg2++;
6175 if (last) {
6176 while (beg1 != end1 && is_space[end1[-1]]) end1--;
6177 while (beg2 != end2 && is_space[end2[-1]]) end2--;
6179 while (beg1 != end1 && beg2 != end2) {
6180 if (is_space[*beg1] && is_space[*beg2]) {
6181 while (beg1 != end1 && is_space[*beg1]) beg1++;
6182 while (beg2 != end2 && is_space[*beg2]) beg2++;
6183 } else if (*beg1 == *beg2) {
6184 beg1++; beg2++;
6185 } else break;
6187 return (beg1 != end1) || (beg2 != end2);
6190 /* Read a replacement list for a macro with parameters.
6191 Build the DEFINITION structure.
6192 Reads characters of text starting at BUF until END.
6193 ARGLIST specifies the formal parameters to look for
6194 in the text of the definition; NARGS is the number of args
6195 in that list, or -1 for a macro name that wants no argument list.
6196 MACRONAME is the macro name itself (so we can avoid recursive expansion)
6197 and NAMELEN is its length in characters.
6199 Note that comments, backslash-newlines, and leading white space
6200 have already been deleted from the argument. */
6202 /* If there is no trailing whitespace, a Newline Space is added at the end
6203 to prevent concatenation that would be contrary to the standard. */
6205 static DEFINITION *
6206 collect_expansion (buf, end, nargs, arglist)
6207 const U_CHAR *buf;
6208 const U_CHAR *end;
6209 int nargs;
6210 struct arglist *arglist;
6212 DEFINITION *defn;
6213 register const U_CHAR *p;
6214 register const U_CHAR *limit;
6215 register U_CHAR *lastp, *exp_p;
6216 struct reflist *endpat = NULL;
6217 /* Pointer to first nonspace after last ## seen. */
6218 const U_CHAR *concat = 0;
6219 /* Pointer to first nonspace after last single-# seen. */
6220 const U_CHAR *stringify = 0;
6221 /* How those tokens were spelled. */
6222 enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
6223 enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
6224 int maxsize;
6225 int expected_delimiter = '\0';
6227 /* Scan thru the replacement list, ignoring comments and quoted
6228 strings, picking up on the macro calls. It does a linear search
6229 thru the arg list on every potential symbol. Profiling might say
6230 that something smarter should happen. */
6232 if (end < buf)
6233 abort ();
6235 /* Find the beginning of the trailing whitespace. */
6236 limit = end;
6237 p = buf;
6238 while (p < limit && is_space[limit[-1]]) limit--;
6240 /* Allocate space for the text in the macro definition.
6241 Each input char may or may not need 1 byte,
6242 so this is an upper bound.
6243 The extra 3 are for invented trailing newline-marker and final null. */
6244 maxsize = (sizeof (DEFINITION)
6245 + (limit - p) + 3);
6246 defn = (DEFINITION *) xcalloc (1, maxsize);
6248 defn->nargs = nargs;
6249 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
6250 lastp = exp_p;
6252 if (p[0] == '#'
6253 ? p[1] == '#'
6254 : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
6255 error ("`##' at start of macro definition");
6256 p += p[0] == '#' ? 2 : 4;
6259 /* Process the main body of the definition. */
6260 while (p < limit) {
6261 int skipped_arg = 0;
6262 register U_CHAR c = *p++;
6264 *exp_p++ = c;
6266 if (!traditional) {
6267 switch (c) {
6268 case '\'':
6269 case '\"':
6270 if (expected_delimiter != '\0') {
6271 if (c == expected_delimiter)
6272 expected_delimiter = '\0';
6273 } else
6274 expected_delimiter = c;
6275 break;
6277 case '\\':
6278 if (expected_delimiter) {
6279 /* In a string, backslash goes through
6280 and makes next char ordinary. */
6281 *exp_p++ = *p++;
6283 break;
6285 case '%':
6286 if (!expected_delimiter && *p == ':') {
6287 /* %: is not a digraph if preceded by an odd number of '<'s. */
6288 const U_CHAR *p0 = p - 1;
6289 while (buf < p0 && p0[-1] == '<')
6290 p0--;
6291 if ((p - p0) & 1) {
6292 /* Treat %:%: as ## and %: as #. */
6293 if (p[1] == '%' && p[2] == ':') {
6294 p += 2;
6295 goto sharp_sharp_token;
6297 if (nargs >= 0) {
6298 p++;
6299 goto sharp_token;
6303 break;
6305 case '#':
6306 /* # is ordinary inside a string. */
6307 if (expected_delimiter)
6308 break;
6309 if (*p == '#') {
6310 sharp_sharp_token:
6311 /* ##: concatenate preceding and following tokens. */
6312 /* Take out the first #, discard preceding whitespace. */
6313 exp_p--;
6314 while (exp_p > lastp && is_hor_space[exp_p[-1]])
6315 --exp_p;
6316 /* Skip the second #. */
6317 p++;
6318 concat_sharp_token_type = c;
6319 if (is_hor_space[*p]) {
6320 concat_sharp_token_type = c + 1;
6321 p++;
6322 SKIP_WHITE_SPACE (p);
6324 concat = p;
6325 if (p == limit)
6326 error ("`##' at end of macro definition");
6327 } else if (nargs >= 0) {
6328 /* Single #: stringify following argument ref.
6329 Don't leave the # in the expansion. */
6330 sharp_token:
6331 exp_p--;
6332 stringify_sharp_token_type = c;
6333 if (is_hor_space[*p]) {
6334 stringify_sharp_token_type = c + 1;
6335 p++;
6336 SKIP_WHITE_SPACE (p);
6338 if (! is_idstart[*p] || nargs == 0
6339 || (*p == 'L' && (p[1] == '\'' || p[1] == '"')))
6340 error ("`#' operator is not followed by a macro argument name");
6341 else
6342 stringify = p;
6344 break;
6346 } else {
6347 /* In -traditional mode, recognize arguments inside strings and
6348 character constants, and ignore special properties of #.
6349 Arguments inside strings are considered "stringified", but no
6350 extra quote marks are supplied. */
6351 switch (c) {
6352 case '\'':
6353 case '\"':
6354 if (expected_delimiter != '\0') {
6355 if (c == expected_delimiter)
6356 expected_delimiter = '\0';
6357 } else
6358 expected_delimiter = c;
6359 break;
6361 case '\\':
6362 /* Backslash quotes delimiters and itself, but not macro args. */
6363 if (expected_delimiter != 0 && p < limit
6364 && (*p == expected_delimiter || *p == '\\')) {
6365 *exp_p++ = *p++;
6366 continue;
6368 break;
6370 case '/':
6371 if (expected_delimiter != '\0') /* No comments inside strings. */
6372 break;
6373 if (*p == '*') {
6374 /* If we find a comment that wasn't removed by handle_directive,
6375 this must be -traditional. So replace the comment with
6376 nothing at all. */
6377 exp_p--;
6378 while (++p < limit) {
6379 if (p[0] == '*' && p[1] == '/') {
6380 p += 2;
6381 break;
6384 #if 0
6385 /* Mark this as a concatenation-point, as if it had been ##. */
6386 concat = p;
6387 #endif
6389 break;
6393 #ifdef MULTIBYTE_CHARS
6394 /* Handle multibyte characters inside string and character literals. */
6395 if (expected_delimiter != '\0')
6397 int length;
6398 --p;
6399 length = local_mblen (p, limit - p);
6400 if (length > 1)
6402 --exp_p;
6403 bcopy (p, exp_p, length);
6404 p += length;
6405 exp_p += length;
6406 continue;
6408 ++p;
6410 #endif
6412 /* Handle the start of a symbol. */
6413 if (is_idchar[c] && nargs > 0) {
6414 const U_CHAR *id_beg = p - 1;
6415 int id_len;
6417 --exp_p;
6418 while (p != limit && is_idchar[*p]) p++;
6419 id_len = p - id_beg;
6421 if (is_idstart[c]
6422 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
6423 register struct arglist *arg;
6425 for (arg = arglist; arg != NULL; arg = arg->next) {
6426 struct reflist *tpat;
6428 if (arg->name[0] == c
6429 && arg->length == id_len
6430 && bcmp (arg->name, id_beg, id_len) == 0) {
6431 enum sharp_token_type tpat_stringify;
6432 if (expected_delimiter) {
6433 if (warn_stringify) {
6434 if (traditional) {
6435 warning ("macro argument `%.*s' is stringified.",
6436 id_len, arg->name);
6437 } else {
6438 warning ("macro arg `%.*s' would be stringified with -traditional.",
6439 id_len, arg->name);
6442 /* If ANSI, don't actually substitute inside a string. */
6443 if (!traditional)
6444 break;
6445 tpat_stringify = SHARP_TOKEN;
6446 } else {
6447 tpat_stringify
6448 = (stringify == id_beg
6449 ? stringify_sharp_token_type : NO_SHARP_TOKEN);
6451 /* make a pat node for this arg and append it to the end of
6452 the pat list */
6453 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
6454 tpat->next = NULL;
6455 tpat->raw_before
6456 = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
6457 tpat->raw_after = NO_SHARP_TOKEN;
6458 tpat->rest_args = arg->rest_args;
6459 tpat->stringify = tpat_stringify;
6461 if (endpat == NULL)
6462 defn->pattern = tpat;
6463 else
6464 endpat->next = tpat;
6465 endpat = tpat;
6467 tpat->argno = arg->argno;
6468 tpat->nchars = exp_p - lastp;
6470 register const U_CHAR *p1 = p;
6471 SKIP_WHITE_SPACE (p1);
6472 if (p1[0]=='#'
6473 ? p1[1]=='#'
6474 : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
6475 tpat->raw_after = p1[0] + (p != p1);
6477 lastp = exp_p; /* place to start copying from next time */
6478 skipped_arg = 1;
6479 break;
6484 /* If this was not a macro arg, copy it into the expansion. */
6485 if (! skipped_arg) {
6486 register const U_CHAR *lim1 = p;
6487 p = id_beg;
6488 while (p != lim1)
6489 *exp_p++ = *p++;
6490 if (stringify == id_beg)
6491 error ("`#' operator should be followed by a macro argument name");
6496 if (!traditional && expected_delimiter == 0) {
6497 /* If ANSI, put in a newline-space marker to prevent token pasting.
6498 But not if "inside a string" (which in ANSI mode happens only for
6499 -D option). */
6500 *exp_p++ = '\n';
6501 *exp_p++ = ' ';
6504 *exp_p = '\0';
6506 defn->length = exp_p - defn->expansion;
6508 /* Crash now if we overrun the allocated size. */
6509 if (defn->length + 1 > maxsize)
6510 abort ();
6512 #if 0
6513 /* This isn't worth the time it takes. */
6514 /* give back excess storage */
6515 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
6516 #endif
6518 return defn;
6521 static int
6522 do_assert (buf, limit, op, keyword)
6523 U_CHAR *buf, *limit;
6524 FILE_BUF *op ATTRIBUTE_UNUSED;
6525 struct directive *keyword ATTRIBUTE_UNUSED;
6527 const U_CHAR *bp; /* temp ptr into input buffer */
6528 const U_CHAR *symname; /* remember where symbol name starts */
6529 int sym_length; /* and how long it is */
6530 struct arglist *tokens = NULL;
6532 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6533 pedwarn ("ANSI C does not allow `#assert'");
6535 bp = buf;
6537 while (is_hor_space[*bp])
6538 bp++;
6540 symname = bp; /* remember where it starts */
6541 sym_length = check_macro_name (bp, 1);
6542 bp += sym_length;
6543 /* #define doesn't do this, but we should. */
6544 SKIP_WHITE_SPACE (bp);
6546 /* Lossage will occur if identifiers or control tokens are broken
6547 across lines using backslash. This is not the right place to take
6548 care of that. */
6550 if (*bp != '(') {
6551 error ("missing token-sequence in `#assert'");
6552 return 1;
6556 int error_flag = 0;
6558 bp++; /* skip '(' */
6559 SKIP_WHITE_SPACE (bp);
6561 tokens = read_token_list (&bp, limit, &error_flag);
6562 if (error_flag)
6563 return 1;
6564 if (tokens == 0) {
6565 error ("empty token-sequence in `#assert'");
6566 return 1;
6569 ++bp; /* skip paren */
6570 SKIP_WHITE_SPACE (bp);
6573 /* If this name isn't already an assertion name, make it one.
6574 Error if it was already in use in some other way. */
6577 ASSERTION_HASHNODE *hp;
6578 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6579 struct tokenlist_list *value
6580 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6582 hp = assertion_lookup (symname, sym_length, hashcode);
6583 if (hp == NULL) {
6584 if (sym_length == 7 && ! bcmp (symname, "defined", 7))
6585 error ("`defined' redefined as assertion");
6586 hp = assertion_install (symname, sym_length, hashcode);
6589 /* Add the spec'd token-sequence to the list of such. */
6590 value->tokens = tokens;
6591 value->next = hp->value;
6592 hp->value = value;
6595 return 0;
6598 static int
6599 do_unassert (buf, limit, op, keyword)
6600 U_CHAR *buf, *limit;
6601 FILE_BUF *op ATTRIBUTE_UNUSED;
6602 struct directive *keyword ATTRIBUTE_UNUSED;
6604 const U_CHAR *bp; /* temp ptr into input buffer */
6605 const U_CHAR *symname; /* remember where symbol name starts */
6606 int sym_length; /* and how long it is */
6608 struct arglist *tokens = NULL;
6609 int tokens_specified = 0;
6611 if (pedantic && done_initializing && !instack[indepth].system_header_p)
6612 pedwarn ("ANSI C does not allow `#unassert'");
6614 bp = buf;
6616 while (is_hor_space[*bp])
6617 bp++;
6619 symname = bp; /* remember where it starts */
6620 sym_length = check_macro_name (bp, 1);
6621 bp += sym_length;
6622 /* #define doesn't do this, but we should. */
6623 SKIP_WHITE_SPACE (bp);
6625 /* Lossage will occur if identifiers or control tokens are broken
6626 across lines using backslash. This is not the right place to take
6627 care of that. */
6629 if (*bp == '(') {
6630 int error_flag = 0;
6632 bp++; /* skip '(' */
6633 SKIP_WHITE_SPACE (bp);
6635 tokens = read_token_list (&bp, limit, &error_flag);
6636 if (error_flag)
6637 return 1;
6638 if (tokens == 0) {
6639 error ("empty token list in `#unassert'");
6640 return 1;
6643 tokens_specified = 1;
6645 ++bp; /* skip paren */
6646 SKIP_WHITE_SPACE (bp);
6650 ASSERTION_HASHNODE *hp;
6651 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6652 struct tokenlist_list *tail, *prev;
6654 hp = assertion_lookup (symname, sym_length, hashcode);
6655 if (hp == NULL)
6656 return 1;
6658 /* If no token list was specified, then eliminate this assertion
6659 entirely. */
6660 if (! tokens_specified) {
6661 struct tokenlist_list *next;
6662 for (tail = hp->value; tail; tail = next) {
6663 next = tail->next;
6664 free_token_list (tail->tokens);
6665 free (tail);
6667 delete_assertion (hp);
6668 } else {
6669 /* If a list of tokens was given, then delete any matching list. */
6671 tail = hp->value;
6672 prev = 0;
6673 while (tail) {
6674 struct tokenlist_list *next = tail->next;
6675 if (compare_token_lists (tail->tokens, tokens)) {
6676 if (prev)
6677 prev->next = next;
6678 else
6679 hp->value = tail->next;
6680 free_token_list (tail->tokens);
6681 free (tail);
6682 } else {
6683 prev = tail;
6685 tail = next;
6690 return 0;
6693 /* Test whether there is an assertion named NAME
6694 and optionally whether it has an asserted token list TOKENS.
6695 NAME is not null terminated; its length is SYM_LENGTH.
6696 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6699 check_assertion (name, sym_length, tokens_specified, tokens)
6700 const U_CHAR *name;
6701 int sym_length;
6702 int tokens_specified;
6703 struct arglist *tokens;
6705 ASSERTION_HASHNODE *hp;
6706 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6708 if (pedantic && !instack[indepth].system_header_p)
6709 pedwarn ("ANSI C does not allow testing assertions");
6711 hp = assertion_lookup (name, sym_length, hashcode);
6712 if (hp == NULL)
6713 /* It is not an assertion; just return false. */
6714 return 0;
6716 /* If no token list was specified, then value is 1. */
6717 if (! tokens_specified)
6718 return 1;
6721 struct tokenlist_list *tail;
6723 tail = hp->value;
6725 /* If a list of tokens was given,
6726 then succeed if the assertion records a matching list. */
6728 while (tail) {
6729 if (compare_token_lists (tail->tokens, tokens))
6730 return 1;
6731 tail = tail->next;
6734 /* Fail if the assertion has no matching list. */
6735 return 0;
6739 /* Compare two lists of tokens for equality including order of tokens. */
6741 static int
6742 compare_token_lists (l1, l2)
6743 struct arglist *l1, *l2;
6745 while (l1 && l2) {
6746 if (l1->length != l2->length)
6747 return 0;
6748 if (bcmp (l1->name, l2->name, l1->length))
6749 return 0;
6750 l1 = l1->next;
6751 l2 = l2->next;
6754 /* Succeed if both lists end at the same time. */
6755 return l1 == l2;
6758 /* Read a space-separated list of tokens ending in a close parenthesis.
6759 Return a list of strings, in the order they were written.
6760 (In case of error, return 0 and store -1 in *ERROR_FLAG.)
6761 Parse the text starting at *BPP, and update *BPP.
6762 Don't parse beyond LIMIT. */
6764 static struct arglist *
6765 read_token_list (bpp, limit, error_flag)
6766 const U_CHAR **bpp;
6767 const U_CHAR *limit;
6768 int *error_flag;
6770 struct arglist *token_ptrs = 0;
6771 const U_CHAR *bp = *bpp;
6772 int depth = 1;
6774 *error_flag = 0;
6776 /* Loop over the assertion value tokens. */
6777 while (depth > 0) {
6778 struct arglist *temp;
6779 U_CHAR *temp2;
6780 int eofp = 0;
6781 const U_CHAR *beg = bp;
6783 /* Find the end of the token. */
6784 if (*bp == '(') {
6785 bp++;
6786 depth++;
6787 } else if (*bp == ')') {
6788 depth--;
6789 if (depth == 0)
6790 break;
6791 bp++;
6792 } else if (*bp == '"' || *bp == '\'')
6793 bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
6794 else
6795 while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
6796 && *bp != '"' && *bp != '\'' && bp != limit)
6797 bp++;
6799 temp = (struct arglist *) xmalloc (sizeof (struct arglist));
6800 temp2 = (U_CHAR *) xmalloc (bp - beg + 1);
6801 bcopy ((const PTR) beg, (PTR) temp2, bp - beg);
6802 temp2[bp - beg] = 0;
6803 temp->name = temp2;
6804 temp->next = token_ptrs;
6805 token_ptrs = temp;
6806 temp->length = bp - beg;
6808 SKIP_WHITE_SPACE (bp);
6810 if (bp >= limit) {
6811 error ("unterminated token sequence in `#assert' or `#unassert'");
6812 *error_flag = -1;
6813 return 0;
6816 *bpp = bp;
6818 /* We accumulated the names in reverse order.
6819 Now reverse them to get the proper order. */
6821 register struct arglist *prev = 0, *this, *next;
6822 for (this = token_ptrs; this; this = next) {
6823 next = this->next;
6824 this->next = prev;
6825 prev = this;
6827 return prev;
6831 static void
6832 free_token_list (tokens)
6833 struct arglist *tokens;
6835 while (tokens) {
6836 struct arglist *next = tokens->next;
6837 free ((PTR) tokens->name);
6838 free (tokens);
6839 tokens = next;
6843 /* Install a name in the assertion hash table.
6845 If LEN is >= 0, it is the length of the name.
6846 Otherwise, compute the length by scanning the entire name.
6848 If HASH is >= 0, it is the precomputed hash code.
6849 Otherwise, compute the hash code. */
6851 static ASSERTION_HASHNODE *
6852 assertion_install (name, len, hash)
6853 const U_CHAR *name;
6854 int len;
6855 int hash;
6857 register ASSERTION_HASHNODE *hp;
6858 register int i, bucket;
6859 register U_CHAR *p;
6860 register const U_CHAR *q;
6862 i = sizeof (ASSERTION_HASHNODE) + len + 1;
6863 hp = (ASSERTION_HASHNODE *) xmalloc (i);
6864 bucket = hash;
6865 hp->bucket_hdr = &assertion_hashtab[bucket];
6866 hp->next = assertion_hashtab[bucket];
6867 assertion_hashtab[bucket] = hp;
6868 hp->prev = NULL;
6869 if (hp->next != NULL)
6870 hp->next->prev = hp;
6871 hp->length = len;
6872 hp->value = 0;
6873 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
6874 p = hp->name;
6875 q = name;
6876 for (i = 0; i < len; i++)
6877 *p++ = *q++;
6878 hp->name[len] = 0;
6879 return hp;
6882 /* Find the most recent hash node for name "name" (ending with first
6883 non-identifier char) installed by install
6885 If LEN is >= 0, it is the length of the name.
6886 Otherwise, compute the length by scanning the entire name.
6888 If HASH is >= 0, it is the precomputed hash code.
6889 Otherwise, compute the hash code. */
6891 static ASSERTION_HASHNODE *
6892 assertion_lookup (name, len, hash)
6893 const U_CHAR *name;
6894 int len;
6895 int hash;
6897 register ASSERTION_HASHNODE *bucket;
6899 bucket = assertion_hashtab[hash];
6900 while (bucket) {
6901 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
6902 return bucket;
6903 bucket = bucket->next;
6905 return NULL;
6908 static void
6909 delete_assertion (hp)
6910 ASSERTION_HASHNODE *hp;
6913 if (hp->prev != NULL)
6914 hp->prev->next = hp->next;
6915 if (hp->next != NULL)
6916 hp->next->prev = hp->prev;
6918 /* Make sure that the bucket chain header that the deleted guy was
6919 on points to the right thing afterwards. */
6920 if (hp == *hp->bucket_hdr)
6921 *hp->bucket_hdr = hp->next;
6923 free (hp);
6927 * interpret #line directive. Remembers previously seen fnames
6928 * in its very own hash table.
6930 #define FNAME_HASHSIZE 37
6932 static int
6933 do_line (buf, limit, op, keyword)
6934 U_CHAR *buf, *limit;
6935 FILE_BUF *op;
6936 struct directive *keyword ATTRIBUTE_UNUSED;
6938 register U_CHAR *bp;
6939 FILE_BUF *ip = &instack[indepth];
6940 FILE_BUF tem;
6941 int new_lineno;
6942 enum file_change_code file_change = same_file;
6944 /* Expand any macros. */
6945 tem = expand_to_temp_buffer (buf, limit, 0, 0);
6947 /* Point to macroexpanded line, which is null-terminated now. */
6948 bp = tem.buf;
6949 limit = tem.bufp;
6950 SKIP_WHITE_SPACE (bp);
6952 if (!ISDIGIT (*bp)) {
6953 error ("invalid format `#line' directive");
6954 return 0;
6957 /* The Newline at the end of this line remains to be processed.
6958 To put the next line at the specified line number,
6959 we must store a line number now that is one less. */
6960 new_lineno = atoi ((char *) bp) - 1;
6962 /* NEW_LINENO is one less than the actual line number here. */
6963 if (pedantic && new_lineno < 0)
6964 pedwarn ("line number out of range in `#line' directive");
6966 /* skip over the line number. */
6967 while (ISDIGIT (*bp))
6968 bp++;
6970 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
6971 if (*bp && !is_space[*bp]) {
6972 error ("invalid format `#line' directive");
6973 return;
6975 #endif
6977 SKIP_WHITE_SPACE (bp);
6979 if (*bp == '\"') {
6980 static HASHNODE *fname_table[FNAME_HASHSIZE];
6981 HASHNODE *hp, **hash_bucket;
6982 U_CHAR *fname, *p;
6983 int fname_length;
6985 fname = ++bp;
6987 /* Turn the file name, which is a character string literal,
6988 into a null-terminated string. Do this in place. */
6989 p = bp;
6990 for (;;)
6991 switch ((*p++ = *bp++)) {
6992 case '\\':
6993 if (! ignore_escape_flag)
6995 char *bpc = (char *) bp;
6996 HOST_WIDEST_INT c = parse_escape (&bpc, (HOST_WIDEST_INT) (U_CHAR) (-1));
6997 bp = (U_CHAR *) bpc;
6998 if (c < 0)
6999 p--;
7000 else
7001 p[-1] = c;
7003 break;
7005 case '\"':
7006 *--p = 0;
7007 goto fname_done;
7009 fname_done:
7010 fname_length = p - fname;
7012 SKIP_WHITE_SPACE (bp);
7013 if (*bp) {
7014 if (pedantic)
7015 pedwarn ("garbage at end of `#line' directive");
7016 if (*bp == '1')
7017 file_change = enter_file;
7018 else if (*bp == '2')
7019 file_change = leave_file;
7020 else if (*bp == '3')
7021 ip->system_header_p = 1;
7022 else if (*bp == '4')
7023 ip->system_header_p = 2;
7024 else {
7025 error ("invalid format `#line' directive");
7026 return 0;
7029 bp++;
7030 SKIP_WHITE_SPACE (bp);
7031 if (*bp == '3') {
7032 ip->system_header_p = 1;
7033 bp++;
7034 SKIP_WHITE_SPACE (bp);
7036 if (*bp == '4') {
7037 ip->system_header_p = 2;
7038 bp++;
7039 SKIP_WHITE_SPACE (bp);
7041 if (*bp) {
7042 error ("invalid format `#line' directive");
7043 return 0;
7047 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
7048 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
7049 if (hp->length == fname_length &&
7050 bcmp (hp->value.cpval, fname, fname_length) == 0) {
7051 ip->nominal_fname = hp->value.cpval;
7052 ip->nominal_fname_len = fname_length;
7053 break;
7055 if (hp == 0) {
7056 /* Didn't find it; cons up a new one. */
7057 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
7058 hp->next = *hash_bucket;
7059 *hash_bucket = hp;
7061 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
7062 ip->nominal_fname_len = hp->length = fname_length;
7063 bcopy (fname, ((char *) hp) + sizeof (HASHNODE), fname_length + 1);
7065 } else if (*bp) {
7066 error ("invalid format `#line' directive");
7067 return 0;
7070 ip->lineno = new_lineno;
7071 output_line_directive (ip, op, 0, file_change);
7072 check_expand (op, ip->length - (ip->bufp - ip->buf));
7073 return 0;
7076 /* Remove the definition of a symbol from the symbol table.
7077 according to un*x /lib/cpp, it is not an error to undef
7078 something that has no definitions, so it isn't one here either. */
7080 static int
7081 do_undef (buf, limit, op, keyword)
7082 U_CHAR *buf, *limit;
7083 FILE_BUF *op;
7084 struct directive *keyword;
7086 int sym_length;
7087 HASHNODE *hp;
7088 U_CHAR *orig_buf = buf;
7090 /* If this is a precompiler run (with -pcp) pass thru #undef directives. */
7091 if (pcp_outfile && op)
7092 pass_thru_directive (buf, limit, op, keyword);
7094 SKIP_WHITE_SPACE (buf);
7095 sym_length = check_macro_name (buf, 0);
7097 while ((hp = lookup (buf, sym_length, -1)) != NULL) {
7098 /* If we are generating additional info for debugging (with -g) we
7099 need to pass through all effective #undef directives. */
7100 if (debug_output && op)
7101 pass_thru_directive (orig_buf, limit, op, keyword);
7102 if (hp->type == T_POISON)
7103 error ("cannot undefine poisoned `%s'", hp->name);
7104 else {
7105 if (hp->type != T_MACRO)
7106 warning ("undefining `%s'", hp->name);
7107 delete_macro (hp);
7111 if (pedantic) {
7112 buf += sym_length;
7113 SKIP_WHITE_SPACE (buf);
7114 if (buf != limit)
7115 pedwarn ("garbage after `#undef' directive");
7117 return 0;
7121 /* Report an error detected by the program we are processing.
7122 Use the text of the line in the error message. */
7124 static int
7125 do_error (buf, limit, op, keyword)
7126 U_CHAR *buf, *limit;
7127 FILE_BUF *op ATTRIBUTE_UNUSED;
7128 struct directive *keyword;
7130 int length = limit - buf;
7131 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
7132 bcopy ((const PTR) buf, (PTR) copy, length);
7133 copy[length] = 0;
7134 SKIP_WHITE_SPACE (copy);
7136 switch (keyword->type) {
7137 case T_ERROR:
7138 error ("#error %s", copy);
7139 break;
7141 case T_WARNING:
7142 if (pedantic && !instack[indepth].system_header_p)
7143 pedwarn ("ANSI C does not allow `#warning'");
7144 warning ("#warning %s", copy);
7145 break;
7147 default:
7148 abort ();
7151 return 0;
7153 /* Remember the name of the current file being read from so that we can
7154 avoid ever including it again. */
7156 static void
7157 do_once ()
7159 int i;
7161 for (i = indepth; i >= 0; i--)
7162 if (instack[i].inc) {
7163 record_control_macro (instack[i].inc, (const U_CHAR *) "");
7164 break;
7168 /* Report program identification. */
7170 static int
7171 do_ident (buf, limit, op, keyword)
7172 U_CHAR *buf, *limit;
7173 FILE_BUF *op;
7174 struct directive *keyword ATTRIBUTE_UNUSED;
7176 FILE_BUF trybuf;
7177 int len;
7179 /* Allow #ident in system headers, since that's not user's fault. */
7180 if (pedantic && !instack[indepth].system_header_p)
7181 pedwarn ("ANSI C does not allow `#ident'");
7183 trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
7184 buf = trybuf.buf;
7185 len = trybuf.bufp - buf;
7187 /* Output expanded directive. */
7188 check_expand (op, 7 + len);
7189 bcopy ("#ident ", (char *) op->bufp, 7);
7190 op->bufp += 7;
7191 bcopy ((const PTR) buf, (PTR) op->bufp, len);
7192 op->bufp += len;
7194 free (buf);
7195 return 0;
7198 /* #pragma and its argument line have already been copied to the output file.
7199 Just check for some recognized pragmas that need validation here. */
7201 static int
7202 do_pragma (buf, limit, op, keyword)
7203 U_CHAR *buf, *limit;
7204 FILE_BUF *op;
7205 struct directive *keyword;
7207 SKIP_WHITE_SPACE (buf);
7208 if (!strncmp ((char *) buf, "once", 4)) {
7209 /* Allow #pragma once in system headers, since that's not the user's
7210 fault. */
7211 if (!instack[indepth].system_header_p)
7212 warning ("`#pragma once' is obsolete");
7213 do_once ();
7216 if (!strncmp ((char *) buf, "poison", 6)) {
7217 /* Poison these symbols so that all subsequent usage produces an
7218 error message. */
7219 U_CHAR *p = buf + 6;
7221 SKIP_WHITE_SPACE (p);
7222 while (p < limit)
7224 U_CHAR *end = p;
7226 while (end < limit && is_idchar[*end])
7227 end++;
7228 if (end < limit && !is_space[*end])
7230 error ("invalid #pragma poison");
7231 return 0;
7233 do_define(p, end, op, keyword);
7234 p = end;
7235 SKIP_WHITE_SPACE (p);
7239 if (!strncmp ((char *) buf, "implementation", 14)) {
7240 /* Be quiet about `#pragma implementation' for a file only if it hasn't
7241 been included yet. */
7243 int h;
7244 U_CHAR *p = buf + 14, *fname;
7245 SKIP_WHITE_SPACE (p);
7246 if (*p != '\"')
7247 return 0;
7249 fname = p + 1;
7250 p = skip_quoted_string (p, limit, 0, NULL_PTR, NULL_PTR, NULL_PTR);
7251 if (p[-1] == '"')
7252 *--p = '\0';
7254 for (h = 0; h < INCLUDE_HASHSIZE; h++) {
7255 struct include_file *inc;
7256 for (inc = include_hashtab[h]; inc; inc = inc->next) {
7257 if (!strcmp (base_name (inc->fname), (char *) fname)) {
7258 warning ("`#pragma implementation' for \"%s\" appears after its #include",fname);
7259 return 0;
7264 return 0;
7267 #if 0
7268 /* This was a fun hack, but #pragma seems to start to be useful.
7269 By failing to recognize it, we pass it through unchanged to cc1. */
7271 /* The behavior of the #pragma directive is implementation defined.
7272 this implementation defines it as follows. */
7274 static int
7275 do_pragma ()
7277 close (0);
7278 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
7279 goto nope;
7280 close (1);
7281 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
7282 goto nope;
7283 execl ("/usr/games/hack", "#pragma", 0);
7284 execl ("/usr/games/rogue", "#pragma", 0);
7285 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
7286 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
7287 nope:
7288 fatal ("You are in a maze of twisty compiler features, all different");
7290 #endif
7292 #ifdef SCCS_DIRECTIVE
7294 /* Just ignore #sccs, on systems where we define it at all. */
7296 static int
7297 do_sccs (buf, limit, op, keyword)
7298 U_CHAR *buf ATTRIBUTE_UNUSED;
7299 U_CHAR *limit ATTRIBUTE_UNUSED;
7300 FILE_BUF *op ATTRIBUTE_UNUSED;
7301 struct directive *keyword ATTRIBUTE_UNUSED;
7303 if (pedantic)
7304 pedwarn ("ANSI C does not allow `#sccs'");
7305 return 0;
7308 #endif /* defined (SCCS_DIRECTIVE) */
7310 /* Handle #if directive by
7311 1) inserting special `defined' keyword into the hash table
7312 that gets turned into 0 or 1 by special_symbol (thus,
7313 if the luser has a symbol called `defined' already, it won't
7314 work inside the #if directive)
7315 2) rescan the input into a temporary output buffer
7316 3) pass the output buffer to the yacc parser and collect a value
7317 4) clean up the mess left from steps 1 and 2.
7318 5) call conditional_skip to skip til the next #endif (etc.),
7319 or not, depending on the value from step 3. */
7321 static int
7322 do_if (buf, limit, op, keyword)
7323 U_CHAR *buf, *limit;
7324 FILE_BUF *op;
7325 struct directive *keyword ATTRIBUTE_UNUSED;
7327 HOST_WIDEST_INT value;
7328 FILE_BUF *ip = &instack[indepth];
7330 value = eval_if_expression (buf, limit - buf);
7331 conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
7332 return 0;
7335 /* Handle a #elif directive by not changing if_stack either.
7336 see the comment above do_else. */
7338 static int
7339 do_elif (buf, limit, op, keyword)
7340 U_CHAR *buf, *limit;
7341 FILE_BUF *op;
7342 struct directive *keyword ATTRIBUTE_UNUSED;
7344 HOST_WIDEST_INT value;
7345 FILE_BUF *ip = &instack[indepth];
7347 if (if_stack == instack[indepth].if_stack) {
7348 error ("`#elif' not within a conditional");
7349 return 0;
7350 } else {
7351 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7352 error ("`#elif' after `#else'");
7353 fprintf (stderr, " (matches line %d", if_stack->lineno);
7354 if (! (if_stack->fname_len == ip->nominal_fname_len
7355 && !bcmp (if_stack->fname, ip->nominal_fname,
7356 if_stack->fname_len))) {
7357 fprintf (stderr, ", file ");
7358 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7359 if_stack->fname_len, stderr);
7361 fprintf (stderr, ")\n");
7363 if_stack->type = T_ELIF;
7366 if (if_stack->if_succeeded)
7367 skip_if_group (ip, 0, op);
7368 else {
7369 value = eval_if_expression (buf, limit - buf);
7370 if (value == 0)
7371 skip_if_group (ip, 0, op);
7372 else {
7373 ++if_stack->if_succeeded; /* continue processing input */
7374 output_line_directive (ip, op, 1, same_file);
7377 return 0;
7380 /* Evaluate a #if expression in BUF, of length LENGTH, then parse the
7381 result as a C expression and return the value as an int. */
7383 static HOST_WIDEST_INT
7384 eval_if_expression (buf, length)
7385 const U_CHAR *buf;
7386 int length;
7388 FILE_BUF temp_obuf;
7389 HASHNODE *save_defined;
7390 HOST_WIDEST_INT value;
7392 save_defined = install ((const U_CHAR *) "defined", -1, T_SPEC_DEFINED,
7393 NULL_PTR, -1);
7394 pcp_inside_if = 1;
7395 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
7396 pcp_inside_if = 0;
7397 delete_macro (save_defined); /* clean up special symbol */
7399 *temp_obuf.bufp = '\n';
7400 value = parse_c_expression ((char *) temp_obuf.buf,
7401 warn_undef && !instack[indepth].system_header_p);
7403 free (temp_obuf.buf);
7405 return value;
7408 /* routine to handle ifdef/ifndef. Try to look up the symbol, then do
7409 or don't skip to the #endif/#else/#elif depending on what directive
7410 is actually being processed. */
7412 static int
7413 do_xifdef (buf, limit, op, keyword)
7414 U_CHAR *buf, *limit;
7415 FILE_BUF *op;
7416 struct directive *keyword;
7418 int skip;
7419 FILE_BUF *ip = &instack[indepth];
7420 U_CHAR *end;
7421 int start_of_file = 0;
7422 U_CHAR *control_macro = 0;
7424 /* Detect a #ifndef at start of file (not counting comments). */
7425 if (ip->fname != 0 && keyword->type == T_IFNDEF) {
7426 U_CHAR *p = ip->buf;
7427 while (p != directive_start) {
7428 U_CHAR c = *p++;
7429 if (is_space[c])
7431 /* Make no special provision for backslash-newline here; this is
7432 slower if backslash-newlines are present, but it's correct,
7433 and it's not worth it to tune for the rare backslash-newline. */
7434 else if (c == '/'
7435 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7436 /* Skip this comment. */
7437 int junk = 0;
7438 U_CHAR *save_bufp = ip->bufp;
7439 ip->bufp = p + 1;
7440 p = skip_to_end_of_comment (ip, &junk, 1);
7441 ip->bufp = save_bufp;
7442 } else {
7443 goto fail;
7446 /* If we get here, this conditional is the beginning of the file. */
7447 start_of_file = 1;
7448 fail: ;
7451 /* Discard leading and trailing whitespace. */
7452 SKIP_WHITE_SPACE (buf);
7453 while (limit != buf && is_hor_space[limit[-1]]) limit--;
7455 /* Find the end of the identifier at the beginning. */
7456 for (end = buf; is_idchar[*end]; end++);
7458 if (end == buf) {
7459 skip = (keyword->type == T_IFDEF);
7460 if (! traditional)
7462 if (end == limit)
7463 pedwarn ("`#%s' with no argument", keyword->name);
7464 else
7465 pedwarn ("`#%s' argument starts with punctuation", keyword->name);
7467 } else {
7468 HASHNODE *hp;
7470 if (! traditional) {
7471 if (ISDIGIT (buf[0]))
7472 pedwarn ("`#%s' argument starts with a digit", keyword->name);
7473 else if (end != limit)
7474 pedwarn ("garbage at end of `#%s' argument", keyword->name);
7477 hp = lookup (buf, end-buf, -1);
7479 if (pcp_outfile) {
7480 /* Output a precondition for this macro. */
7481 if (hp
7482 && (hp->type == T_CONST
7483 || (hp->type == T_MACRO && hp->value.defn->predefined)))
7484 fprintf (pcp_outfile, "#define %s\n", hp->name);
7485 else {
7486 U_CHAR *cp = buf;
7487 fprintf (pcp_outfile, "#undef ");
7488 while (is_idchar[*cp]) /* Ick! */
7489 fputc (*cp++, pcp_outfile);
7490 putc ('\n', pcp_outfile);
7494 if ((hp != NULL) && (hp->type == T_POISON)) {
7495 error("attempt to use poisoned `%s'.", hp->name);
7496 hp = NULL;
7498 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
7499 if (start_of_file && !skip) {
7500 control_macro = (U_CHAR *) xmalloc (end - buf + 1);
7501 bcopy ((const PTR) buf, (PTR) control_macro, end - buf);
7502 control_macro[end - buf] = 0;
7506 conditional_skip (ip, skip, T_IF, control_macro, op);
7507 return 0;
7510 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
7511 If this is a #ifndef starting at the beginning of a file,
7512 CONTROL_MACRO is the macro name tested by the #ifndef.
7513 Otherwise, CONTROL_MACRO is 0. */
7515 static void
7516 conditional_skip (ip, skip, type, control_macro, op)
7517 FILE_BUF *ip;
7518 int skip;
7519 enum node_type type;
7520 const U_CHAR *control_macro;
7521 FILE_BUF *op;
7523 IF_STACK_FRAME *temp;
7525 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7526 temp->fname = ip->nominal_fname;
7527 temp->fname_len = ip->nominal_fname_len;
7528 temp->lineno = ip->lineno;
7529 temp->next = if_stack;
7530 temp->control_macro = control_macro;
7531 if_stack = temp;
7533 if_stack->type = type;
7535 if (skip != 0) {
7536 skip_if_group (ip, 0, op);
7537 return;
7538 } else {
7539 ++if_stack->if_succeeded;
7540 output_line_directive (ip, &outbuf, 1, same_file);
7544 /* Skip to #endif, #else, or #elif. adjust line numbers, etc.
7545 Leaves input ptr at the sharp sign found.
7546 If ANY is nonzero, return at next directive of any sort. */
7548 static void
7549 skip_if_group (ip, any, op)
7550 FILE_BUF *ip;
7551 int any;
7552 FILE_BUF *op;
7554 register U_CHAR *bp = ip->bufp, *cp;
7555 register U_CHAR *endb = ip->buf + ip->length;
7556 struct directive *kt;
7557 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
7558 U_CHAR *beg_of_line = bp;
7559 register int ident_length;
7560 U_CHAR *ident, *after_ident;
7561 /* Save info about where the group starts. */
7562 U_CHAR *beg_of_group = bp;
7563 int beg_lineno = ip->lineno;
7564 int skipping_include_directive = 0;
7566 if (output_conditionals && op != 0) {
7567 static const char * const ptr = "#failed\n";
7568 int len = strlen (ptr);
7570 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7572 *op->bufp++ = '\n';
7573 op->lineno++;
7575 check_expand (op, len);
7576 bcopy (ptr, (char *) op->bufp, len);
7577 op->bufp += len;
7578 op->lineno++;
7579 output_line_directive (ip, op, 1, 0);
7582 while (bp < endb) {
7583 switch (*bp++) {
7584 case '/': /* possible comment */
7585 if (*bp == '\\')
7586 newline_fix (bp);
7587 if (*bp == '*'
7588 || (cplusplus_comments && *bp == '/')) {
7589 ip->bufp = ++bp;
7590 bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
7592 break;
7593 case '<':
7594 if (skipping_include_directive) {
7595 while (bp < endb && *bp != '>' && *bp != '\n') {
7596 if (*bp == '\\' && bp[1] == '\n') {
7597 ip->lineno++;
7598 bp++;
7600 bp++;
7603 break;
7604 case '\"':
7605 if (skipping_include_directive) {
7606 while (bp < endb && *bp != '\n') {
7607 if (*bp == '"') {
7608 bp++;
7609 break;
7611 if (*bp == '\\' && bp[1] == '\n') {
7612 ip->lineno++;
7613 bp++;
7615 bp++;
7617 break;
7619 /* Fall through. */
7620 case '\'':
7621 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
7622 NULL_PTR, NULL_PTR);
7623 break;
7624 case '\\':
7625 /* Char after backslash loses its special meaning in some cases. */
7626 if (*bp == '\n') {
7627 ++ip->lineno;
7628 bp++;
7629 } else if (traditional && bp < endb)
7630 bp++;
7631 break;
7632 case '\n':
7633 ++ip->lineno;
7634 beg_of_line = bp;
7635 skipping_include_directive = 0;
7636 break;
7637 case '%':
7638 if (beg_of_line == 0 || traditional)
7639 break;
7640 ip->bufp = bp - 1;
7641 while (bp[0] == '\\' && bp[1] == '\n')
7642 bp += 2;
7643 if (*bp == ':')
7644 goto sharp_token;
7645 break;
7646 case '#':
7647 /* # keyword: a # must be first nonblank char on the line */
7648 if (beg_of_line == 0)
7649 break;
7650 ip->bufp = bp - 1;
7651 sharp_token:
7652 /* Scan from start of line, skipping whitespace, comments
7653 and backslash-newlines, and see if we reach this #.
7654 If not, this # is not special. */
7655 bp = beg_of_line;
7656 /* If -traditional, require # to be at beginning of line. */
7657 if (!traditional) {
7658 while (1) {
7659 if (is_hor_space[*bp])
7660 bp++;
7661 else if (*bp == '\\' && bp[1] == '\n')
7662 bp += 2;
7663 else if (*bp == '/' && bp[1] == '*') {
7664 bp += 2;
7665 while (1)
7667 if (*bp == '*')
7669 if (bp[1] == '/')
7671 bp += 2;
7672 break;
7675 else
7677 #ifdef MULTIBYTE_CHARS
7678 int length;
7679 length = local_mblen (bp, endb - bp);
7680 if (length > 1)
7681 bp += (length - 1);
7682 #endif
7684 bp++;
7687 /* There is no point in trying to deal with C++ // comments here,
7688 because if there is one, then this # must be part of the
7689 comment and we would never reach here. */
7690 else break;
7693 if (bp != ip->bufp) {
7694 bp = ip->bufp + 1; /* Reset bp to after the #. */
7695 break;
7698 bp = ip->bufp + 1; /* Point after the '#' */
7699 if (ip->bufp[0] == '%') {
7700 /* Skip past the ':' again. */
7701 while (*bp == '\\') {
7702 ip->lineno++;
7703 bp += 2;
7705 bp++;
7708 /* Skip whitespace and \-newline. */
7709 while (1) {
7710 if (is_hor_space[*bp])
7711 bp++;
7712 else if (*bp == '\\' && bp[1] == '\n')
7713 bp += 2;
7714 else if (*bp == '/') {
7715 if (bp[1] == '\\')
7716 newline_fix (bp + 1);
7717 if (bp[1] == '*') {
7718 for (bp += 2; ; bp++) {
7719 if (*bp == '\n')
7720 ip->lineno++;
7721 else if (*bp == '*') {
7722 if (bp[-1] == '/' && warn_comments)
7723 warning ("`/*' within comment");
7724 if (bp[1] == '\\')
7725 newline_fix (bp + 1);
7726 if (bp[1] == '/')
7727 break;
7729 else
7731 #ifdef MULTIBYTE_CHARS
7732 int length;
7733 length = local_mblen (bp, endb - bp);
7734 if (length > 1)
7735 bp += (length - 1);
7736 #endif
7739 bp += 2;
7740 } else if (bp[1] == '/' && cplusplus_comments) {
7741 for (bp += 2; ; bp++) {
7742 if (*bp == '\n')
7743 break;
7744 if (*bp == '\\' && bp[1] == '\n')
7746 if (warn_comments)
7747 warning ("multiline `//' comment");
7748 ip->lineno++;
7749 bp++;
7751 else
7753 #ifdef MULTIBYTE_CHARS
7754 int length;
7755 length = local_mblen (bp, endb - bp);
7756 if (length > 1)
7757 bp += (length - 1);
7758 #endif
7761 } else
7762 break;
7763 } else
7764 break;
7767 cp = bp;
7769 /* Now find end of directive name.
7770 If we encounter a backslash-newline, exchange it with any following
7771 symbol-constituents so that we end up with a contiguous name. */
7773 while (1) {
7774 if (is_idchar[*bp])
7775 bp++;
7776 else {
7777 if (*bp == '\\')
7778 name_newline_fix (bp);
7779 if (is_idchar[*bp])
7780 bp++;
7781 else break;
7784 ident_length = bp - cp;
7785 ident = cp;
7786 after_ident = bp;
7788 /* A line of just `#' becomes blank. */
7790 if (ident_length == 0 && *after_ident == '\n') {
7791 continue;
7794 if (ident_length == 0 || !is_idstart[*ident]) {
7795 U_CHAR *p = ident;
7796 while (is_idchar[*p]) {
7797 if (*p < '0' || *p > '9')
7798 break;
7799 p++;
7801 /* Handle # followed by a line number. */
7802 if (p != ident && !is_idchar[*p]) {
7803 if (pedantic)
7804 pedwarn ("`#' followed by integer");
7805 continue;
7808 /* Avoid error for `###' and similar cases unless -pedantic. */
7809 if (p == ident) {
7810 while (*p == '#' || is_hor_space[*p]) p++;
7811 if (*p == '\n') {
7812 if (pedantic && !lang_asm)
7813 pedwarn ("invalid preprocessing directive");
7814 continue;
7818 if (!lang_asm && pedantic)
7819 pedwarn ("invalid preprocessing directive name");
7820 continue;
7823 for (kt = directive_table; kt->length >= 0; kt++) {
7824 IF_STACK_FRAME *temp;
7825 if (ident_length == kt->length
7826 && bcmp (cp, kt->name, kt->length) == 0) {
7827 /* If we are asked to return on next directive, do so now. */
7828 if (any)
7829 goto done;
7831 switch (kt->type) {
7832 case T_IF:
7833 case T_IFDEF:
7834 case T_IFNDEF:
7835 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
7836 temp->next = if_stack;
7837 if_stack = temp;
7838 temp->lineno = ip->lineno;
7839 temp->fname = ip->nominal_fname;
7840 temp->fname_len = ip->nominal_fname_len;
7841 temp->type = kt->type;
7842 break;
7843 case T_ELSE:
7844 case T_ENDIF:
7845 if (pedantic && if_stack != save_if_stack)
7846 validate_else (bp, endb);
7847 case T_ELIF:
7848 if (if_stack == instack[indepth].if_stack) {
7849 error ("`#%s' not within a conditional", kt->name);
7850 break;
7852 else if (if_stack == save_if_stack)
7853 goto done; /* found what we came for */
7855 if (kt->type != T_ENDIF) {
7856 if (if_stack->type == T_ELSE)
7857 error ("`#else' or `#elif' after `#else'");
7858 if_stack->type = kt->type;
7859 break;
7862 temp = if_stack;
7863 if_stack = if_stack->next;
7864 free (temp);
7865 break;
7867 case T_INCLUDE:
7868 case T_INCLUDE_NEXT:
7869 case T_IMPORT:
7870 skipping_include_directive = 1;
7871 break;
7873 default:
7874 break;
7876 break;
7879 /* Don't let erroneous code go by. */
7880 if (kt->length < 0 && !lang_asm && pedantic)
7881 pedwarn ("invalid preprocessing directive name");
7885 ip->bufp = bp;
7886 /* after this returns, rescan will exit because ip->bufp
7887 now points to the end of the buffer.
7888 rescan is responsible for the error message also. */
7890 done:
7891 if (output_conditionals && op != 0) {
7892 static const char * const ptr = "#endfailed\n";
7893 int len = strlen (ptr);
7895 if (op->bufp > op->buf && op->bufp[-1] != '\n')
7897 *op->bufp++ = '\n';
7898 op->lineno++;
7900 check_expand (op, beg_of_line - beg_of_group);
7901 bcopy ((const PTR) beg_of_group, (PTR) op->bufp,
7902 beg_of_line - beg_of_group);
7903 op->bufp += beg_of_line - beg_of_group;
7904 op->lineno += ip->lineno - beg_lineno;
7905 check_expand (op, len);
7906 bcopy (ptr, (char *) op->bufp, len);
7907 op->bufp += len;
7908 op->lineno++;
7912 /* Handle a #else directive. Do this by just continuing processing
7913 without changing if_stack ; this is so that the error message
7914 for missing #endif's etc. will point to the original #if. It
7915 is possible that something different would be better. */
7917 static int
7918 do_else (buf, limit, op, keyword)
7919 U_CHAR *buf, *limit;
7920 FILE_BUF *op;
7921 struct directive *keyword ATTRIBUTE_UNUSED;
7923 FILE_BUF *ip = &instack[indepth];
7925 if (pedantic) {
7926 SKIP_WHITE_SPACE (buf);
7927 if (buf != limit)
7928 pedwarn ("text following `#else' violates ANSI standard");
7931 if (if_stack == instack[indepth].if_stack) {
7932 error ("`#else' not within a conditional");
7933 return 0;
7934 } else {
7935 /* #ifndef can't have its special treatment for containing the whole file
7936 if it has a #else clause. */
7937 if_stack->control_macro = 0;
7939 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
7940 error ("`#else' after `#else'");
7941 fprintf (stderr, " (matches line %d", if_stack->lineno);
7942 if (! (if_stack->fname_len == ip->nominal_fname_len
7943 && !bcmp (if_stack->fname, ip->nominal_fname,
7944 if_stack->fname_len))) {
7945 fprintf (stderr, ", file ");
7946 fwrite (if_stack->fname, sizeof if_stack->fname[0],
7947 if_stack->fname_len, stderr);
7949 fprintf (stderr, ")\n");
7951 if_stack->type = T_ELSE;
7954 if (if_stack->if_succeeded)
7955 skip_if_group (ip, 0, op);
7956 else {
7957 ++if_stack->if_succeeded; /* continue processing input */
7958 output_line_directive (ip, op, 1, same_file);
7960 return 0;
7963 /* Unstack after #endif directive. */
7965 static int
7966 do_endif (buf, limit, op, keyword)
7967 U_CHAR *buf, *limit;
7968 FILE_BUF *op;
7969 struct directive *keyword ATTRIBUTE_UNUSED;
7971 if (pedantic) {
7972 SKIP_WHITE_SPACE (buf);
7973 if (buf != limit)
7974 pedwarn ("text following `#endif' violates ANSI standard");
7977 if (if_stack == instack[indepth].if_stack)
7978 error ("unbalanced `#endif'");
7979 else {
7980 IF_STACK_FRAME *temp = if_stack;
7981 if_stack = if_stack->next;
7982 if (temp->control_macro != 0) {
7983 /* This #endif matched a #ifndef at the start of the file.
7984 See if it is at the end of the file. */
7985 FILE_BUF *ip = &instack[indepth];
7986 U_CHAR *p = ip->bufp;
7987 U_CHAR *ep = ip->buf + ip->length;
7989 while (p != ep) {
7990 U_CHAR c = *p++;
7991 if (!is_space[c]) {
7992 if (c == '/'
7993 && (*p == '*' || (cplusplus_comments && *p == '/'))) {
7994 /* Skip this comment. */
7995 int junk = 0;
7996 U_CHAR *save_bufp = ip->bufp;
7997 ip->bufp = p + 1;
7998 p = skip_to_end_of_comment (ip, &junk, 1);
7999 ip->bufp = save_bufp;
8000 } else
8001 goto fail;
8004 /* If we get here, this #endif ends a #ifndef
8005 that contains all of the file (aside from whitespace).
8006 Arrange not to include the file again
8007 if the macro that was tested is defined.
8009 Do not do this for the top-level file in a -include or any
8010 file in a -imacros. */
8011 if (indepth != 0
8012 && ! (indepth == 1 && no_record_file)
8013 && ! (no_record_file && no_output))
8014 record_control_macro (ip->inc, temp->control_macro);
8015 fail: ;
8017 free (temp);
8018 output_line_directive (&instack[indepth], op, 1, same_file);
8020 return 0;
8023 /* When an #else or #endif is found while skipping failed conditional,
8024 if -pedantic was specified, this is called to warn about text after
8025 the directive name. P points to the first char after the directive
8026 name. */
8028 static void
8029 validate_else (p, limit)
8030 register const U_CHAR *p;
8031 register const U_CHAR *limit;
8033 /* Advance P over whitespace and comments. */
8034 while (1) {
8035 while (*p == '\\' && p[1] == '\n')
8036 p += 2;
8037 if (is_hor_space[*p])
8038 p++;
8039 else if (*p == '/') {
8040 while (p[1] == '\\' && p[2] == '\n')
8041 p += 2;
8042 if (p[1] == '*') {
8043 /* Don't bother warning about unterminated comments
8044 since that will happen later. Just be sure to exit. */
8045 for (p += 2; ; p++) {
8046 if (p == limit)
8047 return;
8048 if (*p == '*') {
8049 while (p[1] == '\\' && p[2] == '\n')
8050 p += 2;
8051 if (p[1] == '/') {
8052 p += 2;
8053 break;
8056 else
8058 #ifdef MULTIBYTE_CHARS
8059 int length;
8060 length = local_mblen (p, limit - p);
8061 if (length > 1)
8062 p += (length - 1);
8063 #endif
8067 else if (cplusplus_comments && p[1] == '/')
8068 return;
8069 else break;
8070 } else break;
8072 if (*p != '\n')
8073 pedwarn ("text following `#else' or `#endif' violates ANSI standard");
8076 /* Skip a comment, assuming the input ptr immediately follows the
8077 initial slash-star. Bump *LINE_COUNTER for each newline.
8078 (The canonical line counter is &ip->lineno.)
8079 Don't use this routine (or the next one) if bumping the line
8080 counter is not sufficient to deal with newlines in the string.
8082 If NOWARN is nonzero, don't warn about slash-star inside a comment.
8083 This feature is useful when processing a comment that is going to
8084 be processed or was processed at another point in the preprocessor,
8085 to avoid a duplicate warning. Likewise for unterminated comment
8086 errors. */
8088 static U_CHAR *
8089 skip_to_end_of_comment (ip, line_counter, nowarn)
8090 register FILE_BUF *ip;
8091 int *line_counter; /* place to remember newlines, or NULL */
8092 int nowarn;
8094 register U_CHAR *limit = ip->buf + ip->length;
8095 register U_CHAR *bp = ip->bufp;
8096 FILE_BUF *op = put_out_comments && !line_counter ? &outbuf : (FILE_BUF *) 0;
8097 int start_line = line_counter ? *line_counter : 0;
8099 /* JF this line_counter stuff is a crock to make sure the
8100 comment is only put out once, no matter how many times
8101 the comment is skipped. It almost works */
8102 if (op) {
8103 *op->bufp++ = '/';
8104 *op->bufp++ = bp[-1];
8106 if (cplusplus_comments && bp[-1] == '/') {
8107 for (; bp < limit; bp++) {
8108 if (*bp == '\n')
8109 break;
8110 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
8112 if (!nowarn && warn_comments)
8113 warning ("multiline `//' comment");
8114 if (line_counter)
8115 ++*line_counter;
8116 if (op)
8118 ++op->lineno;
8119 *op->bufp++ = *bp;
8121 ++bp;
8123 else
8125 #ifdef MULTIBYTE_CHARS
8126 int length;
8127 length = local_mblen (bp, limit - bp);
8128 if (length > 1)
8130 if (op)
8132 bcopy (bp, op->bufp, length - 1);
8133 op->bufp += (length - 1);
8135 bp += (length - 1);
8137 #endif
8139 if (op)
8140 *op->bufp++ = *bp;
8142 ip->bufp = bp;
8143 return bp;
8145 while (bp < limit) {
8146 if (op)
8147 *op->bufp++ = *bp;
8148 switch (*bp++) {
8149 case '\n':
8150 /* If this is the end of the file, we have an unterminated comment.
8151 Don't swallow the newline. We are guaranteed that there will be a
8152 trailing newline and various pieces assume it's there. */
8153 if (bp == limit)
8155 --bp;
8156 --limit;
8157 break;
8159 if (line_counter != NULL)
8160 ++*line_counter;
8161 if (op)
8162 ++op->lineno;
8163 break;
8164 case '*':
8165 if (bp[-2] == '/' && !nowarn && warn_comments)
8166 warning ("`/*' within comment");
8167 if (*bp == '\\')
8168 newline_fix (bp);
8169 if (*bp == '/') {
8170 if (op)
8171 *op->bufp++ = '/';
8172 ip->bufp = ++bp;
8173 return bp;
8175 break;
8176 #ifdef MULTIBYTE_CHARS
8177 default:
8179 int length;
8180 bp--;
8181 length = local_mblen (bp, limit - bp);
8182 if (length <= 0)
8183 length = 1;
8184 if (op)
8186 op->bufp--;
8187 bcopy (bp, op->bufp, length);
8188 op->bufp += length;
8190 bp += length;
8192 #endif
8196 if (!nowarn)
8197 error_with_line (line_for_error (start_line), "unterminated comment");
8198 ip->bufp = bp;
8199 return bp;
8202 /* Skip over a quoted string. BP points to the opening quote.
8203 Returns a pointer after the closing quote. Don't go past LIMIT.
8204 START_LINE is the line number of the starting point (but it need
8205 not be valid if the starting point is inside a macro expansion).
8207 The input stack state is not changed.
8209 If COUNT_NEWLINES is nonzero, it points to an int to increment
8210 for each newline passed; also, warn about any white space
8211 just before line end.
8213 If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
8214 if we pass a backslash-newline.
8216 If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. */
8218 static U_CHAR *
8219 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
8220 register const U_CHAR *bp;
8221 register const U_CHAR *limit;
8222 int start_line;
8223 int *count_newlines;
8224 int *backslash_newlines_p;
8225 int *eofp;
8227 register U_CHAR c, match;
8229 match = *bp++;
8230 while (1) {
8231 if (bp >= limit) {
8232 error_with_line (line_for_error (start_line),
8233 "unterminated string or character constant");
8234 error_with_line (multiline_string_line,
8235 "possible real start of unterminated constant");
8236 multiline_string_line = 0;
8237 if (eofp)
8238 *eofp = 1;
8239 break;
8241 c = *bp++;
8242 if (c == '\\') {
8243 while (*bp == '\\' && bp[1] == '\n') {
8244 if (backslash_newlines_p)
8245 *backslash_newlines_p = 1;
8246 if (count_newlines)
8247 ++*count_newlines;
8248 bp += 2;
8250 if (*bp == '\n') {
8251 if (backslash_newlines_p)
8252 *backslash_newlines_p = 1;
8253 if (count_newlines)
8254 ++*count_newlines;
8256 bp++;
8257 } else if (c == '\n') {
8258 if (traditional) {
8259 /* Unterminated strings and character constants are 'valid'. */
8260 bp--; /* Don't consume the newline. */
8261 if (eofp)
8262 *eofp = 1;
8263 break;
8265 if (match == '\'') {
8266 error_with_line (line_for_error (start_line),
8267 "unterminated character constant");
8268 bp--;
8269 if (eofp)
8270 *eofp = 1;
8271 break;
8273 /* If not traditional, then allow newlines inside strings. */
8274 if (count_newlines) {
8275 if (warn_white_space && is_hor_space[bp[-2]])
8276 warning ("white space at end of line in string");
8277 ++*count_newlines;
8279 if (multiline_string_line == 0) {
8280 if (pedantic)
8281 pedwarn_with_line (line_for_error (start_line),
8282 "string constant runs past end of line");
8283 multiline_string_line = start_line;
8285 } else if (c == match)
8286 break;
8287 #ifdef MULTIBYTE_CHARS
8289 int length;
8290 --bp;
8291 length = local_mblen (bp, limit - bp);
8292 if (length <= 0)
8293 length = 1;
8294 bp += length;
8296 #endif
8298 return (U_CHAR *) bp;
8301 /* Place into DST a quoted string representing the string SRC.
8302 SRCLEN is the length of SRC; SRC may contain null bytes.
8303 Return the address of DST's terminating null. */
8305 static char *
8306 quote_string (dst, src, srclen)
8307 char *dst;
8308 const char *src;
8309 size_t srclen;
8311 U_CHAR c;
8312 const char *srclim = src + srclen;
8314 *dst++ = '\"';
8315 while (src != srclim)
8316 switch ((c = *src++))
8318 default:
8319 if (ISPRINT (c))
8320 *dst++ = c;
8321 else
8323 sprintf (dst, "\\%03o", c);
8324 dst += 4;
8326 break;
8328 case '\"':
8329 case '\\':
8330 *dst++ = '\\';
8331 *dst++ = c;
8332 break;
8335 *dst++ = '\"';
8336 *dst = '\0';
8337 return dst;
8340 /* Skip across a group of balanced parens, starting from IP->bufp.
8341 IP->bufp is updated. Use this with IP->bufp pointing at an open-paren.
8343 This does not handle newlines, because it's used for the arg of #if,
8344 where there aren't any newlines. Also, backslash-newline can't appear. */
8346 static U_CHAR *
8347 skip_paren_group (ip)
8348 register FILE_BUF *ip;
8350 U_CHAR *limit = ip->buf + ip->length;
8351 U_CHAR *p = ip->bufp;
8352 int depth = 0;
8353 int lines_dummy = 0;
8355 while (p != limit) {
8356 int c = *p++;
8357 switch (c) {
8358 case '(':
8359 depth++;
8360 break;
8362 case ')':
8363 depth--;
8364 if (depth == 0)
8365 return ip->bufp = p;
8366 break;
8368 case '/':
8369 if (*p == '*') {
8370 ip->bufp = p;
8371 p = skip_to_end_of_comment (ip, &lines_dummy, 0);
8372 p = ip->bufp;
8375 case '"':
8376 case '\'':
8378 int eofp = 0;
8379 p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
8380 if (eofp)
8381 return ip->bufp = p;
8383 break;
8387 ip->bufp = p;
8388 return p;
8391 /* Write out a #line directive, for instance, after an #include file.
8392 If CONDITIONAL is nonzero, we can omit the #line if it would
8393 appear to be a no-op, and we can output a few newlines instead
8394 if we want to increase the line number by a small amount.
8395 FILE_CHANGE says whether we are entering a file, leaving, or neither. */
8397 static void
8398 output_line_directive (ip, op, conditional, file_change)
8399 FILE_BUF *ip, *op;
8400 int conditional;
8401 enum file_change_code file_change;
8403 int len;
8404 char *line_directive_buf, *line_end;
8406 if (no_line_directives
8407 || ip->fname == NULL
8408 || no_output) {
8409 op->lineno = ip->lineno;
8410 return;
8413 if (conditional) {
8414 if (ip->lineno == op->lineno)
8415 return;
8417 /* If the inherited line number is a little too small,
8418 output some newlines instead of a #line directive. */
8419 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
8420 check_expand (op, 10);
8421 while (ip->lineno > op->lineno) {
8422 *op->bufp++ = '\n';
8423 op->lineno++;
8425 return;
8429 /* Output a positive line number if possible. */
8430 while (ip->lineno <= 0 && ip->bufp - ip->buf < ip->length
8431 && *ip->bufp == '\n') {
8432 ip->lineno++;
8433 ip->bufp++;
8436 line_directive_buf = (char *) alloca (4 * ip->nominal_fname_len + 100);
8437 sprintf (line_directive_buf, "# %d ", ip->lineno);
8438 line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
8439 ip->nominal_fname, ip->nominal_fname_len);
8440 if (file_change != same_file) {
8441 *line_end++ = ' ';
8442 *line_end++ = file_change == enter_file ? '1' : '2';
8444 /* Tell cc1 if following text comes from a system header file. */
8445 if (ip->system_header_p) {
8446 *line_end++ = ' ';
8447 *line_end++ = '3';
8449 #ifndef NO_IMPLICIT_EXTERN_C
8450 /* Tell cc1plus if following text should be treated as C. */
8451 if (ip->system_header_p == 2 && cplusplus) {
8452 *line_end++ = ' ';
8453 *line_end++ = '4';
8455 #endif
8456 *line_end++ = '\n';
8457 len = line_end - line_directive_buf;
8458 check_expand (op, len + 1);
8459 if (op->bufp > op->buf && op->bufp[-1] != '\n')
8460 *op->bufp++ = '\n';
8461 bcopy ((const PTR) line_directive_buf, (PTR) op->bufp, len);
8462 op->bufp += len;
8463 op->lineno = ip->lineno;
8466 /* This structure represents one parsed argument in a macro call.
8467 `raw' points to the argument text as written (`raw_length' is its length).
8468 `expanded' points to the argument's macro-expansion
8469 (its length is `expand_length', and its allocated size is `expand_size').
8470 `stringified_length_bound' is an upper bound on the length
8471 the argument would have if stringified.
8472 `use_count' is the number of times this macro arg is substituted
8473 into the macro. If the actual use count exceeds 10,
8474 the value stored is 10.
8475 `free1' and `free2', if nonzero, point to blocks to be freed
8476 when the macro argument data is no longer needed.
8477 `free_ptr', if nonzero, points to a value of instack[i].free_ptr
8478 where the raw field points somewhere into this string. The purpose
8479 of this is to hold onto instack[i].buf for macro arguments, even
8480 when the element has been popped off the input stack.
8483 struct argdata {
8484 U_CHAR *raw, *expanded;
8485 int raw_length, expand_length, expand_size;
8486 int stringified_length_bound;
8487 U_CHAR *free1, *free2;
8488 U_CHAR *free_ptr;
8489 int newlines;
8490 char use_count;
8493 /* Expand a macro call.
8494 HP points to the symbol that is the macro being called.
8495 Put the result of expansion onto the input stack
8496 so that subsequent input by our caller will use it.
8498 If macro wants arguments, caller has already verified that
8499 an argument list follows; arguments come from the input stack. */
8501 static void
8502 macroexpand (hp, op)
8503 HASHNODE *hp;
8504 FILE_BUF *op;
8506 int nargs;
8507 DEFINITION *defn = hp->value.defn;
8508 register U_CHAR *xbuf;
8509 int xbuf_len;
8510 int start_line = instack[indepth].lineno;
8511 int rest_args, rest_zero;
8513 CHECK_DEPTH (return;);
8515 /* it might not actually be a macro. */
8516 if (hp->type != T_MACRO) {
8517 special_symbol (hp, op);
8518 return;
8521 /* This macro is being used inside a #if, which means it must be */
8522 /* recorded as a precondition. */
8523 if (pcp_inside_if && pcp_outfile && defn->predefined)
8524 dump_single_macro (hp, pcp_outfile);
8526 nargs = defn->nargs;
8528 if (nargs >= 0) {
8529 register int i;
8530 struct argdata *args;
8531 int parse_error = 0;
8533 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
8535 for (i = 0; i < nargs; i++) {
8536 args[i].raw = (U_CHAR *) "";
8537 args[i].expanded = 0;
8538 args[i].raw_length = args[i].expand_length = args[i].expand_size
8539 = args[i].stringified_length_bound = 0;
8540 args[i].free1 = args[i].free2 = 0;
8541 args[i].free_ptr = 0;
8542 args[i].use_count = 0;
8545 /* Parse all the macro args that are supplied. I counts them.
8546 The first NARGS args are stored in ARGS.
8547 The rest are discarded.
8548 If rest_args is set then we assume macarg absorbed the rest of the args.
8550 i = 0;
8551 rest_args = 0;
8552 do {
8553 /* Discard the open-parenthesis or comma before the next arg. */
8554 ++instack[indepth].bufp;
8555 if (rest_args)
8556 continue;
8557 if (i < nargs || (nargs == 0 && i == 0)) {
8558 /* If we are working on last arg which absorbs rest of args... */
8559 if (i == nargs - 1 && defn->rest_args)
8560 rest_args = 1;
8561 parse_error = macarg (&args[i], rest_args);
8563 else
8564 parse_error = macarg (NULL_PTR, 0);
8565 if (parse_error) {
8566 error_with_line (line_for_error (start_line),
8567 "unterminated macro call");
8568 break;
8570 i++;
8571 } while (*instack[indepth].bufp != ')');
8573 /* If we got one arg but it was just whitespace, call that 0 args. */
8574 if (i == 1) {
8575 register const U_CHAR *bp = args[0].raw;
8576 register const U_CHAR *lim = bp + args[0].raw_length;
8577 /* cpp.texi says for foo ( ) we provide one argument.
8578 However, if foo wants just 0 arguments, treat this as 0. */
8579 if (nargs == 0)
8580 while (bp != lim && is_space[*bp]) bp++;
8581 if (bp == lim)
8582 i = 0;
8585 /* Don't output an error message if we have already output one for
8586 a parse error above. */
8587 rest_zero = 0;
8588 if (nargs == 0 && i > 0) {
8589 if (! parse_error)
8590 error ("arguments given to macro `%s'", hp->name);
8591 } else if (i < nargs) {
8592 /* traditional C allows foo() if foo wants one argument. */
8593 if (nargs == 1 && i == 0 && traditional)
8595 /* the rest args token is allowed to absorb 0 tokens */
8596 else if (i == nargs - 1 && defn->rest_args)
8597 rest_zero = 1;
8598 else if (parse_error)
8600 else if (i == 0)
8601 error ("macro `%s' used without args", hp->name);
8602 else if (i == 1)
8603 error ("macro `%s' used with just one arg", hp->name);
8604 else
8605 error ("macro `%s' used with only %d args", hp->name, i);
8606 } else if (i > nargs) {
8607 if (! parse_error)
8608 error ("macro `%s' used with too many (%d) args", hp->name, i);
8611 /* Swallow the closeparen. */
8612 ++instack[indepth].bufp;
8614 /* If macro wants zero args, we parsed the arglist for checking only.
8615 Read directly from the macro definition. */
8616 if (nargs == 0) {
8617 xbuf = defn->expansion;
8618 xbuf_len = defn->length;
8619 } else {
8620 register U_CHAR *exp = defn->expansion;
8621 register int offset; /* offset in expansion,
8622 copied a piece at a time */
8623 register int totlen; /* total amount of exp buffer filled so far */
8625 register struct reflist *ap, *last_ap;
8627 /* Macro really takes args. Compute the expansion of this call. */
8629 /* Compute length in characters of the macro's expansion.
8630 Also count number of times each arg is used. */
8631 xbuf_len = defn->length;
8632 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
8633 if (ap->stringify && args[ap->argno].stringified_length_bound == 0)
8634 /* macarg is not called for omitted arguments, as a result
8635 stringified_length_bound will be zero. We need to make
8636 enough space for "". */
8637 xbuf_len += 2;
8638 else if (ap->stringify)
8639 xbuf_len += args[ap->argno].stringified_length_bound;
8640 else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
8641 /* Add 4 for two newline-space markers to prevent
8642 token concatenation. */
8643 xbuf_len += args[ap->argno].raw_length + 4;
8644 else {
8645 /* We have an ordinary (expanded) occurrence of the arg.
8646 So compute its expansion, if we have not already. */
8647 if (args[ap->argno].expanded == 0) {
8648 FILE_BUF obuf;
8649 obuf = expand_to_temp_buffer (args[ap->argno].raw,
8650 args[ap->argno].raw + args[ap->argno].raw_length,
8651 1, 0);
8653 args[ap->argno].expanded = obuf.buf;
8654 args[ap->argno].expand_length = obuf.bufp - obuf.buf;
8655 args[ap->argno].expand_size = obuf.length;
8656 args[ap->argno].free2 = obuf.buf;
8658 xbuf_len += args[ap->argno].expand_length;
8659 } else {
8660 /* If the arg appears more than once, its later occurrences
8661 may have newline turned into backslash-'n', which is a
8662 factor of 2 expansion. */
8663 xbuf_len += 2 * args[ap->argno].expand_length;
8665 /* Add 4 for two newline-space markers to prevent
8666 token concatenation. */
8667 xbuf_len += 4;
8669 if (args[ap->argno].use_count < 10)
8670 args[ap->argno].use_count++;
8673 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
8675 /* Generate in XBUF the complete expansion
8676 with arguments substituted in.
8677 TOTLEN is the total size generated so far.
8678 OFFSET is the index in the definition
8679 of where we are copying from. */
8680 offset = totlen = 0;
8681 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
8682 last_ap = ap, ap = ap->next) {
8683 register struct argdata *arg = &args[ap->argno];
8684 int count_before = totlen;
8686 /* Add chars to XBUF. */
8687 for (i = 0; i < ap->nchars; i++, offset++)
8688 xbuf[totlen++] = exp[offset];
8690 /* If followed by an empty rest arg with concatenation,
8691 delete the last run of nonwhite chars. */
8692 if (rest_zero && totlen > count_before
8693 && ((ap->rest_args && ap->raw_before != 0)
8694 || (last_ap != NULL && last_ap->rest_args
8695 && last_ap->raw_after != 0))) {
8696 /* Delete final whitespace. */
8697 while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
8698 totlen--;
8701 /* Delete the nonwhites before them. */
8702 while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
8703 totlen--;
8707 if (ap->stringify != 0) {
8708 int arglen = arg->raw_length;
8709 int escaped = 0;
8710 int in_string = 0;
8711 int c;
8712 i = 0;
8713 while (i < arglen
8714 && (c = arg->raw[i], is_space[c]))
8715 i++;
8716 while (i < arglen
8717 && (c = arg->raw[arglen - 1], is_space[c]))
8718 arglen--;
8719 if (!traditional)
8720 xbuf[totlen++] = '\"'; /* insert beginning quote */
8721 for (; i < arglen; i++) {
8722 c = arg->raw[i];
8724 if (in_string) {
8725 /* Generate nothing for backslash-newline in a string. */
8726 if (c == '\\' && arg->raw[i + 1] == '\n') {
8727 i++;
8728 continue;
8730 } else {
8731 /* Special markers
8732 generate nothing for a stringified argument. */
8733 if (c == '\n') {
8734 i++;
8735 continue;
8738 /* Internal sequences of whitespace are replaced by one space
8739 except within a string or char token. */
8740 if (is_space[c]) {
8741 i++;
8742 while (is_space[(c = arg->raw[i])])
8743 /* Newline markers can occur within a whitespace sequence;
8744 consider them part of the sequence. */
8745 i += (c == '\n') + 1;
8746 i--;
8747 c = ' ';
8751 if (escaped)
8752 escaped = 0;
8753 else {
8754 if (c == '\\')
8755 escaped = 1;
8756 else if (in_string) {
8757 if (c == in_string)
8758 in_string = 0;
8759 else
8761 #ifdef MULTIBYTE_CHARS
8762 int length;
8763 length = local_mblen (arg->raw + i, arglen - i);
8764 if (length > 1)
8766 bcopy (arg->raw + i, xbuf + totlen, length);
8767 i += length - 1;
8768 totlen += length;
8769 continue;
8771 #endif
8773 } else if (c == '\"' || c == '\'')
8774 in_string = c;
8777 /* Escape double-quote, and backslashes in strings.
8778 Newlines in strings are best escaped as \n, since
8779 otherwise backslash-backslash-newline-newline is
8780 mishandled. The C Standard doesn't allow newlines in
8781 strings, so we can escape newlines as we please. */
8782 if (c == '\"'
8783 || (in_string
8784 && (c == '\\'
8785 || (c == '\n' ? (c = 'n', 1) : 0))))
8786 xbuf[totlen++] = '\\';
8787 /* We used to output e.g. \008 for control characters here,
8788 but this doesn't conform to the C Standard.
8789 Just output the characters as-is. */
8790 xbuf[totlen++] = c;
8792 if (!traditional)
8793 xbuf[totlen++] = '\"'; /* insert ending quote */
8794 } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
8795 const U_CHAR *p1 = arg->raw;
8796 const U_CHAR *l1 = p1 + arg->raw_length;
8797 if (ap->raw_before != 0) {
8798 while (p1 != l1 && is_space[*p1]) p1++;
8799 while (p1 != l1 && is_idchar[*p1])
8800 xbuf[totlen++] = *p1++;
8801 /* Delete any no-reexpansion marker that follows
8802 an identifier at the beginning of the argument
8803 if the argument is concatenated with what precedes it. */
8804 if (p1[0] == '\n' && p1[1] == '-')
8805 p1 += 2;
8806 } else if (!traditional) {
8807 /* Ordinary expanded use of the argument.
8808 Put in newline-space markers to prevent token pasting. */
8809 xbuf[totlen++] = '\n';
8810 xbuf[totlen++] = ' ';
8812 if (ap->raw_after != 0) {
8813 /* Arg is concatenated after: delete trailing whitespace,
8814 whitespace markers, and no-reexpansion markers. */
8815 while (p1 != l1) {
8816 if (is_space[l1[-1]]) l1--;
8817 else if (l1[-1] == '-') {
8818 const U_CHAR *p2 = l1 - 1;
8819 /* If a `-' is preceded by an odd number of newlines then it
8820 and the last newline are a no-reexpansion marker. */
8821 while (p2 != p1 && p2[-1] == '\n') p2--;
8822 if ((l1 - 1 - p2) & 1) {
8823 l1 -= 2;
8825 else break;
8827 else break;
8831 bcopy ((const PTR) p1, (PTR) (xbuf + totlen), l1 - p1);
8832 totlen += l1 - p1;
8833 if (!traditional && ap->raw_after == 0) {
8834 /* Ordinary expanded use of the argument.
8835 Put in newline-space markers to prevent token pasting. */
8836 xbuf[totlen++] = '\n';
8837 xbuf[totlen++] = ' ';
8839 } else {
8840 /* Ordinary expanded use of the argument.
8841 Put in newline-space markers to prevent token pasting. */
8842 if (!traditional) {
8843 xbuf[totlen++] = '\n';
8844 xbuf[totlen++] = ' ';
8846 bcopy ((const PTR) arg->expanded, (PTR) (xbuf + totlen),
8847 arg->expand_length);
8848 totlen += arg->expand_length;
8849 if (!traditional) {
8850 xbuf[totlen++] = '\n';
8851 xbuf[totlen++] = ' ';
8853 /* If a macro argument with newlines is used multiple times,
8854 then only expand the newlines once. This avoids creating output
8855 lines which don't correspond to any input line, which confuses
8856 gdb and gcov. */
8857 if (arg->use_count > 1 && arg->newlines > 0) {
8858 /* Don't bother doing change_newlines for subsequent
8859 uses of arg. */
8860 arg->use_count = 1;
8861 change_newlines (arg);
8865 if (totlen > xbuf_len)
8866 abort ();
8869 /* If there is anything left of the definition after handling
8870 the arg list, copy that in too. */
8872 for (i = offset; i < defn->length; i++) {
8873 /* if we've reached the end of the macro */
8874 if (exp[i] == ')')
8875 rest_zero = 0;
8876 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
8877 && last_ap->raw_after != 0))
8878 xbuf[totlen++] = exp[i];
8881 xbuf[totlen] = 0;
8882 xbuf_len = totlen;
8884 for (i = 0; i < nargs; i++) {
8885 if (args[i].free_ptr != 0) {
8886 U_CHAR *buf = args[i].free_ptr;
8887 int d;
8888 for (d = indepth; d >= 0; --d) {
8889 if (instack[d].buf == buf) {
8890 instack[d].free_ptr = buf; /* Give ownership back to instack */
8891 goto no_free;
8894 free (buf); /* buf is not on the stack; must have been popped */
8895 no_free:;
8897 if (args[i].free1 != 0)
8898 free (args[i].free1);
8899 if (args[i].free2 != 0)
8900 free (args[i].free2);
8903 } else {
8904 xbuf = defn->expansion;
8905 xbuf_len = defn->length;
8908 /* Now put the expansion on the input stack
8909 so our caller will commence reading from it. */
8911 register FILE_BUF *ip2;
8913 ip2 = &instack[++indepth];
8915 ip2->fname = 0;
8916 ip2->nominal_fname = 0;
8917 ip2->nominal_fname_len = 0;
8918 ip2->inc = 0;
8919 /* This may not be exactly correct, but will give much better error
8920 messages for nested macro calls than using a line number of zero. */
8921 ip2->lineno = start_line;
8922 ip2->buf = xbuf;
8923 ip2->length = xbuf_len;
8924 ip2->bufp = xbuf;
8925 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
8926 ip2->macro = hp;
8927 ip2->if_stack = if_stack;
8928 ip2->system_header_p = 0;
8930 /* Recursive macro use sometimes works traditionally.
8931 #define foo(x,y) bar (x (y,0), y)
8932 foo (foo, baz) */
8934 if (!traditional)
8935 hp->type = T_DISABLED;
8939 /* Parse a macro argument and store the info on it into *ARGPTR.
8940 REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
8941 Return nonzero to indicate a syntax error. */
8943 static int
8944 macarg (argptr, rest_args)
8945 register struct argdata *argptr;
8946 int rest_args;
8948 FILE_BUF *ip = &instack[indepth];
8949 int paren = 0;
8950 int lineno0 = ip->lineno;
8951 int comments = 0;
8952 int result = 0;
8954 /* Try to parse as much of the argument as exists at this
8955 input stack level. */
8956 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro,
8957 &paren, &ip->lineno, &comments, rest_args);
8959 /* If we find the end of the argument at this level,
8960 set up *ARGPTR to point at it in the input stack. */
8961 if (!(ip->fname != 0 && (ip->lineno != lineno0 || comments != 0))
8962 && bp != ip->buf + ip->length) {
8963 if (argptr != 0) {
8964 argptr->raw = ip->bufp;
8965 argptr->raw_length = bp - ip->bufp;
8966 argptr->newlines = ip->lineno - lineno0;
8967 /* The next two statements transfer ownership of the the buffer
8968 from ip to argptr. Note that the second statement ensures that
8969 a given free_ptr is owned by at most one macro argument. */
8970 argptr->free_ptr = ip->free_ptr;
8971 ip->free_ptr = 0;
8973 ip->bufp = bp;
8974 } else {
8975 /* This input stack level ends before the macro argument does.
8976 We must pop levels and keep parsing.
8977 Therefore, we must allocate a temporary buffer and copy
8978 the macro argument into it. */
8979 int bufsize = bp - ip->bufp;
8980 int extra = ip->lineno - lineno0;
8981 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
8982 int final_start = 0;
8984 bcopy ((const PTR) ip->bufp, (PTR) buffer, bufsize);
8985 ip->bufp = bp;
8987 while (bp == ip->buf + ip->length) {
8988 if (instack[indepth].macro == 0) {
8989 result = 1;
8990 break;
8992 ip->macro->type = T_MACRO;
8993 if (ip->free_ptr)
8994 free (ip->free_ptr);
8995 ip = &instack[--indepth];
8996 lineno0 = ip->lineno;
8997 comments = 0;
8998 bp = macarg1 (ip->bufp, ip->buf + ip->length, ip->macro, &paren,
8999 &ip->lineno, &comments, rest_args);
9000 final_start = bufsize;
9001 bufsize += bp - ip->bufp;
9002 extra += ip->lineno - lineno0;
9003 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
9004 bcopy ((const PTR) ip->bufp, (PTR) (buffer + bufsize - (bp - ip->bufp)),
9005 bp - ip->bufp);
9006 ip->bufp = bp;
9009 /* Now, if arg is actually wanted, record its raw form,
9010 discarding comments and duplicating newlines in whatever
9011 part of it did not come from a macro expansion.
9012 EXTRA space has been preallocated for duplicating the newlines.
9013 FINAL_START is the index of the start of that part. */
9014 if (argptr != 0) {
9015 argptr->raw = buffer;
9016 argptr->raw_length = bufsize;
9017 argptr->free1 = buffer;
9018 argptr->newlines = ip->lineno - lineno0;
9019 if ((argptr->newlines || comments) && ip->fname != 0)
9020 argptr->raw_length
9021 = final_start +
9022 discard_comments (argptr->raw + final_start,
9023 argptr->raw_length - final_start,
9024 argptr->newlines);
9025 argptr->raw[argptr->raw_length] = 0;
9026 if (argptr->raw_length > bufsize + extra)
9027 abort ();
9031 /* If we are not discarding this argument,
9032 macroexpand it and compute its length as stringified.
9033 All this info goes into *ARGPTR. */
9035 if (argptr != 0) {
9036 register const U_CHAR *buf, *lim;
9037 register int totlen;
9039 buf = argptr->raw;
9040 lim = buf + argptr->raw_length;
9042 while (buf != lim && is_space[*buf])
9043 buf++;
9044 while (buf != lim && is_space[lim[-1]])
9045 lim--;
9046 totlen = traditional ? 0 : 2; /* Count opening and closing quote. */
9047 while (buf != lim) {
9048 register U_CHAR c = *buf++;
9049 totlen++;
9050 /* Internal sequences of whitespace are replaced by one space
9051 in most cases, but not always. So count all the whitespace
9052 in case we need to keep it all. */
9053 #if 0
9054 if (is_space[c])
9055 SKIP_ALL_WHITE_SPACE (buf);
9056 else
9057 #endif
9058 if (c == '\"' || c == '\\' || c == '\n') /* escape these chars */
9059 totlen++;
9061 argptr->stringified_length_bound = totlen;
9063 return result;
9066 /* Scan text from START (inclusive) up to LIMIT (exclusive),
9067 taken from the expansion of MACRO,
9068 counting parens in *DEPTHPTR,
9069 and return if reach LIMIT
9070 or before a `)' that would make *DEPTHPTR negative
9071 or before a comma when *DEPTHPTR is zero.
9072 Single and double quotes are matched and termination
9073 is inhibited within them. Comments also inhibit it.
9074 Value returned is pointer to stopping place.
9076 Increment *NEWLINES each time a newline is passed.
9077 REST_ARGS notifies macarg1 that it should absorb the rest of the args.
9078 Set *COMMENTS to 1 if a comment is seen. */
9080 static U_CHAR *
9081 macarg1 (start, limit, macro, depthptr, newlines, comments, rest_args)
9082 U_CHAR *start;
9083 register const U_CHAR *limit;
9084 struct hashnode *macro;
9085 int *depthptr, *newlines, *comments;
9086 int rest_args;
9088 register U_CHAR *bp = start;
9090 while (bp < limit) {
9091 switch (*bp) {
9092 case '(':
9093 (*depthptr)++;
9094 break;
9095 case ')':
9096 if (--(*depthptr) < 0)
9097 return bp;
9098 break;
9099 case '\\':
9100 /* Traditionally, backslash makes following char not special. */
9101 if (traditional && bp + 1 < limit && bp[1] != '\n')
9102 bp++;
9103 break;
9104 case '\n':
9105 ++*newlines;
9106 break;
9107 case '/':
9108 if (macro)
9109 break;
9110 if (bp[1] == '\\')
9111 newline_fix (bp + 1);
9112 if (bp[1] == '*') {
9113 *comments = 1;
9114 for (bp += 2; bp < limit; bp++) {
9115 if (*bp == '\n')
9116 ++*newlines;
9117 else if (*bp == '*') {
9118 if (bp[-1] == '/' && warn_comments)
9119 warning ("`/*' within comment");
9120 if (bp[1] == '\\')
9121 newline_fix (bp + 1);
9122 if (bp[1] == '/') {
9123 bp++;
9124 break;
9127 else
9129 #ifdef MULTIBYTE_CHARS
9130 int length;
9131 length = local_mblen (bp, limit - bp);
9132 if (length > 1)
9133 bp += (length - 1);
9134 #endif
9137 } else if (bp[1] == '/' && cplusplus_comments) {
9138 *comments = 1;
9139 for (bp += 2; bp < limit; bp++) {
9140 if (*bp == '\n') {
9141 ++*newlines;
9142 break;
9144 if (*bp == '\\' && bp + 1 < limit && bp[1] == '\n')
9146 ++*newlines;
9147 if (warn_comments)
9148 warning ("multiline `//' comment");
9149 ++bp;
9151 else
9153 #ifdef MULTIBYTE_CHARS
9154 int length;
9155 length = local_mblen (bp, limit - bp);
9156 if (length > 1)
9157 bp += (length - 1);
9158 #endif
9162 break;
9163 case '\'':
9164 case '\"':
9166 int quotec;
9167 for (quotec = *bp++; bp < limit && *bp != quotec; bp++) {
9168 if (*bp == '\\') {
9169 bp++;
9170 if (*bp == '\n')
9171 ++*newlines;
9172 while (*bp == '\\' && bp[1] == '\n') {
9173 bp += 2;
9174 ++*newlines;
9176 } else if (*bp == '\n') {
9177 if (warn_white_space && is_hor_space[bp[-1]] && ! macro)
9178 warning ("white space at end of line in string");
9179 ++*newlines;
9180 if (quotec == '\'')
9181 break;
9183 else
9185 #ifdef MULTIBYTE_CHARS
9186 int length;
9187 length = local_mblen (bp, limit - bp);
9188 if (length > 1)
9189 bp += (length - 1);
9190 #endif
9194 break;
9195 case ',':
9196 /* if we've returned to lowest level and we aren't absorbing all args */
9197 if ((*depthptr) == 0 && rest_args == 0)
9198 return bp;
9199 break;
9201 bp++;
9204 return bp;
9207 /* Discard comments and duplicate newlines
9208 in the string of length LENGTH at START,
9209 except inside of string constants.
9210 The string is copied into itself with its beginning staying fixed.
9212 NEWLINES is the number of newlines that must be duplicated.
9213 We assume that that much extra space is available past the end
9214 of the string. */
9216 static int
9217 discard_comments (start, length, newlines)
9218 U_CHAR *start;
9219 int length;
9220 int newlines;
9222 register U_CHAR *ibp;
9223 register U_CHAR *obp;
9224 register U_CHAR *limit;
9225 register int c;
9227 /* If we have newlines to duplicate, copy everything
9228 that many characters up. Then, in the second part,
9229 we will have room to insert the newlines
9230 while copying down.
9231 NEWLINES may actually be too large, because it counts
9232 newlines in string constants, and we don't duplicate those.
9233 But that does no harm. */
9234 if (newlines > 0) {
9235 ibp = start + length;
9236 obp = ibp + newlines;
9237 limit = start;
9238 while (limit != ibp)
9239 *--obp = *--ibp;
9242 ibp = start + newlines;
9243 limit = start + length + newlines;
9244 obp = start;
9246 while (ibp < limit) {
9247 *obp++ = c = *ibp++;
9248 switch (c) {
9249 case '\n':
9250 /* Duplicate the newline. */
9251 *obp++ = '\n';
9252 break;
9254 case '\\':
9255 if (*ibp == '\n') {
9256 obp--;
9257 ibp++;
9259 break;
9261 case '/':
9262 if (*ibp == '\\')
9263 newline_fix (ibp);
9264 /* Delete any comment. */
9265 if (cplusplus_comments && ibp[0] == '/') {
9266 /* Comments are equivalent to spaces. */
9267 obp[-1] = ' ';
9268 ibp++;
9269 while (ibp < limit)
9271 if (*ibp == '\n')
9272 break;
9273 if (*ibp == '\\' && ibp + 1 < limit && ibp[1] == '\n')
9274 ibp++;
9275 else
9277 #ifdef MULTIBYTE_CHARS
9278 int length = local_mblen (ibp, limit - ibp);
9279 if (length > 1)
9280 ibp += (length - 1);
9281 #endif
9283 ibp++;
9285 break;
9287 if (ibp[0] != '*' || ibp + 1 >= limit)
9288 break;
9289 /* Comments are equivalent to spaces.
9290 For -traditional, a comment is equivalent to nothing. */
9291 if (traditional)
9292 obp--;
9293 else
9294 obp[-1] = ' ';
9295 while (++ibp < limit) {
9296 if (ibp[0] == '*') {
9297 if (ibp[1] == '\\')
9298 newline_fix (ibp + 1);
9299 if (ibp[1] == '/') {
9300 ibp += 2;
9301 break;
9304 else
9306 #ifdef MULTIBYTE_CHARS
9307 int length = local_mblen (ibp, limit - ibp);
9308 if (length > 1)
9309 ibp += (length - 1);
9310 #endif
9313 break;
9315 case '\'':
9316 case '\"':
9317 /* Notice and skip strings, so that we don't
9318 think that comments start inside them,
9319 and so we don't duplicate newlines in them. */
9321 int quotec = c;
9322 while (ibp < limit) {
9323 *obp++ = c = *ibp++;
9324 if (c == quotec)
9325 break;
9326 if (c == '\n')
9328 if (quotec == '\'')
9329 break;
9331 else if (c == '\\') {
9332 if (ibp < limit && *ibp == '\n') {
9333 ibp++;
9334 obp--;
9335 } else {
9336 while (*ibp == '\\' && ibp[1] == '\n')
9337 ibp += 2;
9338 if (ibp < limit)
9339 *obp++ = *ibp++;
9342 else
9344 #ifdef MULTIBYTE_CHARS
9345 int length;
9346 ibp--;
9347 length = local_mblen (ibp, limit - ibp);
9348 if (length > 1)
9350 obp--;
9351 bcopy (ibp, obp, length);
9352 ibp += length;
9353 obp += length;
9355 else
9356 ibp++;
9357 #endif
9361 break;
9365 return obp - start;
9368 /* Turn newlines to spaces in the macro argument ARG.
9369 Remove backslash-newline from string constants,
9370 and turn other newlines in string constants to backslash-'n'. */
9372 static void
9373 change_newlines (arg)
9374 struct argdata *arg;
9376 U_CHAR *start = arg->expanded;
9377 int length = arg->expand_length;
9378 register U_CHAR *ibp;
9379 register U_CHAR *obp;
9380 register const U_CHAR *limit;
9381 register int c;
9383 ibp = start;
9384 limit = start + length;
9385 obp = start;
9387 while (ibp < limit) {
9388 *obp++ = c = *ibp++;
9389 switch (c) {
9390 case '\n':
9391 /* If this is a NEWLINE NEWLINE, then this is a real newline in the
9392 string. Skip past the newline and its duplicate.
9393 Put a space in the output. */
9394 if (*ibp == '\n')
9396 ibp++;
9397 obp--;
9398 *obp++ = ' ';
9400 break;
9402 case '\'':
9403 case '\"':
9404 /* Notice and skip strings, so that we don't delete newlines in them. */
9406 int quotec = c;
9407 while (ibp < limit) {
9408 *obp++ = c = *ibp++;
9409 if (c == quotec)
9410 break;
9411 else if (c == '\\' && ibp < limit && *ibp == '\n')
9412 *obp++ = *ibp++;
9413 else if (c == '\n')
9415 if (quotec == '\'')
9416 break;
9418 else
9420 #ifdef MULTIBYTE_CHARS
9421 int length;
9422 ibp--;
9423 length = local_mblen (ibp, limit - ibp);
9424 if (length > 1)
9426 obp--;
9427 bcopy (ibp, obp, length);
9428 ibp += length;
9429 obp += length;
9431 else
9432 ibp++;
9433 #endif
9437 break;
9441 arg->expand_length = obp - arg->expanded;
9443 if (start != arg->expanded)
9444 free (start);
9447 /* notice - output message to stderr */
9449 static void
9450 notice VPARAMS ((const char * msgid, ...))
9452 #ifndef ANSI_PROTOTYPES
9453 const char * msgid;
9454 #endif
9455 va_list args;
9457 VA_START (args, msgid);
9459 #ifndef ANSI_PROTOTYPES
9460 msgid = va_arg (args, const char *);
9461 #endif
9463 vnotice (msgid, args);
9464 va_end (args);
9467 static void
9468 vnotice (msgid, args)
9469 const char *msgid;
9470 va_list args;
9472 vfprintf (stderr, _(msgid), args);
9475 /* error - print error message and increment count of errors. */
9477 void
9478 error VPARAMS ((const char * msgid, ...))
9480 #ifndef ANSI_PROTOTYPES
9481 const char * msgid;
9482 #endif
9483 va_list args;
9485 VA_START (args, msgid);
9487 #ifndef ANSI_PROTOTYPES
9488 msgid = va_arg (args, const char *);
9489 #endif
9491 verror (msgid, args);
9492 va_end (args);
9495 void
9496 verror (msgid, args)
9497 const char *msgid;
9498 va_list args;
9500 int i;
9501 FILE_BUF *ip = NULL;
9503 print_containing_files ();
9505 for (i = indepth; i >= 0; i--)
9506 if (instack[i].fname != NULL) {
9507 ip = &instack[i];
9508 break;
9511 if (ip != NULL) {
9512 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9513 ip->nominal_fname_len, stderr);
9514 fprintf (stderr, ":%d: ", ip->lineno);
9516 vnotice (msgid, args);
9517 fprintf (stderr, "\n");
9518 errors++;
9521 /* Error including a message from `errno'. */
9523 static void
9524 error_from_errno (name)
9525 const char *name;
9527 int e = errno;
9528 int i;
9529 FILE_BUF *ip = NULL;
9531 print_containing_files ();
9533 for (i = indepth; i >= 0; i--)
9534 if (instack[i].fname != NULL) {
9535 ip = &instack[i];
9536 break;
9539 if (ip != NULL) {
9540 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9541 ip->nominal_fname_len, stderr);
9542 fprintf (stderr, ":%d: ", ip->lineno);
9545 fprintf (stderr, "%s: %s\n", name, xstrerror (e));
9547 errors++;
9550 /* Print error message but don't count it. */
9552 void
9553 warning VPARAMS ((const char * msgid, ...))
9555 #ifndef ANSI_PROTOTYPES
9556 const char * msgid;
9557 #endif
9558 va_list args;
9560 VA_START (args, msgid);
9562 #ifndef ANSI_PROTOTYPES
9563 msgid = va_arg (args, const char *);
9564 #endif
9566 vwarning (msgid, args);
9567 va_end (args);
9570 static void
9571 vwarning (msgid, args)
9572 const char *msgid;
9573 va_list args;
9575 int i;
9576 FILE_BUF *ip = NULL;
9578 if (inhibit_warnings)
9579 return;
9581 if (warnings_are_errors)
9582 errors++;
9584 print_containing_files ();
9586 for (i = indepth; i >= 0; i--)
9587 if (instack[i].fname != NULL) {
9588 ip = &instack[i];
9589 break;
9592 if (ip != NULL) {
9593 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9594 ip->nominal_fname_len, stderr);
9595 fprintf (stderr, ":%d: ", ip->lineno);
9597 notice ("warning: ");
9598 vnotice (msgid, args);
9599 fprintf (stderr, "\n");
9602 static void
9603 error_with_line VPARAMS ((int line, const char * msgid, ...))
9605 #ifndef ANSI_PROTOTYPES
9606 int line;
9607 const char * msgid;
9608 #endif
9609 va_list args;
9611 VA_START (args, msgid);
9613 #ifndef ANSI_PROTOTYPES
9614 line = va_arg (args, int);
9615 msgid = va_arg (args, const char *);
9616 #endif
9618 verror_with_line (line, msgid, args);
9619 va_end (args);
9623 static void
9624 verror_with_line (line, msgid, args)
9625 int line;
9626 const char *msgid;
9627 va_list args;
9629 int i;
9630 FILE_BUF *ip = NULL;
9632 print_containing_files ();
9634 for (i = indepth; i >= 0; i--)
9635 if (instack[i].fname != NULL) {
9636 ip = &instack[i];
9637 break;
9640 if (ip != NULL) {
9641 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9642 ip->nominal_fname_len, stderr);
9643 fprintf (stderr, ":%d: ", line);
9645 vnotice (msgid, args);
9646 fprintf (stderr, "\n");
9647 errors++;
9650 static void
9651 warning_with_line VPARAMS ((int line, const char * msgid, ...))
9653 #ifndef ANSI_PROTOTYPES
9654 int line;
9655 const char * msgid;
9656 #endif
9657 va_list args;
9659 VA_START (args, msgid);
9661 #ifndef ANSI_PROTOTYPES
9662 line = va_arg (args, int);
9663 msgid = va_arg (args, const char *);
9664 #endif
9666 vwarning_with_line (line, msgid, args);
9667 va_end (args);
9670 static void
9671 vwarning_with_line (line, msgid, args)
9672 int line;
9673 const char *msgid;
9674 va_list args;
9676 int i;
9677 FILE_BUF *ip = NULL;
9679 if (inhibit_warnings)
9680 return;
9682 if (warnings_are_errors)
9683 errors++;
9685 print_containing_files ();
9687 for (i = indepth; i >= 0; i--)
9688 if (instack[i].fname != NULL) {
9689 ip = &instack[i];
9690 break;
9693 if (ip != NULL) {
9694 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9695 ip->nominal_fname_len, stderr);
9696 if (line)
9697 fprintf (stderr, ":%d: ", line);
9698 else
9699 fputs (": ", stderr);
9701 notice ("warning: ");
9702 vnotice (msgid, args);
9703 fprintf (stderr, "\n");
9706 /* Print an error message and maybe count it. */
9708 void
9709 pedwarn VPARAMS ((const char * msgid, ...))
9711 #ifndef ANSI_PROTOTYPES
9712 const char * msgid;
9713 #endif
9714 va_list args;
9716 VA_START (args, msgid);
9718 #ifndef ANSI_PROTOTYPES
9719 msgid = va_arg (args, const char *);
9720 #endif
9722 if (pedantic_errors)
9723 verror (msgid, args);
9724 else
9725 vwarning (msgid, args);
9726 va_end (args);
9729 void
9730 pedwarn_with_line VPARAMS ((int line, const char * msgid, ...))
9732 #ifndef ANSI_PROTOTYPES
9733 int line;
9734 const char * msgid;
9735 #endif
9736 va_list args;
9738 VA_START (args, msgid);
9740 #ifndef ANSI_PROTOTYPES
9741 line = va_arg (args, int);
9742 msgid = va_arg (args, const char *);
9743 #endif
9745 if (pedantic_errors)
9746 verror_with_line (line, msgid, args);
9747 else
9748 vwarning_with_line (line, msgid, args);
9749 va_end (args);
9752 /* Report a warning (or an error if pedantic_errors)
9753 giving specified file name and line number, not current. */
9755 static void
9756 pedwarn_with_file_and_line VPARAMS ((const char *file, size_t file_len,
9757 int line, const char * msgid, ...))
9759 #ifndef ANSI_PROTOTYPES
9760 const char *file;
9761 size_t file_len;
9762 int line;
9763 const char * msgid;
9764 #endif
9765 va_list args;
9767 if (!pedantic_errors && inhibit_warnings)
9768 return;
9770 VA_START (args, msgid);
9772 #ifndef ANSI_PROTOTYPES
9773 file = va_arg (args, const char *);
9774 file_len = va_arg (args, size_t);
9775 line = va_arg (args, int);
9776 msgid = va_arg (args, const char *);
9777 #endif
9779 if (file) {
9780 fwrite (file, sizeof file[0], file_len, stderr);
9781 fprintf (stderr, ":%d: ", line);
9783 if (pedantic_errors)
9784 errors++;
9785 if (!pedantic_errors)
9786 notice ("warning: ");
9787 vnotice (msgid, args);
9788 va_end (args);
9789 fprintf (stderr, "\n");
9792 static void
9793 pedwarn_strange_white_space (ch)
9794 int ch;
9796 switch (ch)
9798 case '\f': pedwarn ("formfeed in preprocessing directive"); break;
9799 case '\r': pedwarn ("carriage return in preprocessing directive"); break;
9800 case '\v': pedwarn ("vertical tab in preprocessing directive"); break;
9801 default: abort ();
9805 /* Print the file names and line numbers of the #include
9806 directives which led to the current file. */
9808 static void
9809 print_containing_files ()
9811 FILE_BUF *ip = NULL;
9812 int i;
9813 int first = 1;
9815 /* If stack of files hasn't changed since we last printed
9816 this info, don't repeat it. */
9817 if (last_error_tick == input_file_stack_tick)
9818 return;
9820 for (i = indepth; i >= 0; i--)
9821 if (instack[i].fname != NULL) {
9822 ip = &instack[i];
9823 break;
9826 /* Give up if we don't find a source file. */
9827 if (ip == NULL)
9828 return;
9830 /* Find the other, outer source files. */
9831 for (i--; i >= 0; i--)
9832 if (instack[i].fname != NULL) {
9833 ip = &instack[i];
9834 if (first) {
9835 first = 0;
9836 notice ( "In file included from ");
9837 } else {
9838 notice (",\n from ");
9841 fwrite (ip->nominal_fname, sizeof ip->nominal_fname[0],
9842 ip->nominal_fname_len, stderr);
9843 fprintf (stderr, ":%d", ip->lineno);
9845 if (! first)
9846 fprintf (stderr, ":\n");
9848 /* Record we have printed the status as of this time. */
9849 last_error_tick = input_file_stack_tick;
9852 /* Return the line at which an error occurred.
9853 The error is not necessarily associated with the current spot
9854 in the input stack, so LINE says where. LINE will have been
9855 copied from ip->lineno for the current input level.
9856 If the current level is for a file, we return LINE.
9857 But if the current level is not for a file, LINE is meaningless.
9858 In that case, we return the lineno of the innermost file. */
9860 static int
9861 line_for_error (line)
9862 int line;
9864 int i;
9865 int line1 = line;
9867 for (i = indepth; i >= 0; ) {
9868 if (instack[i].fname != 0)
9869 return line1;
9870 i--;
9871 if (i < 0)
9872 return 0;
9873 line1 = instack[i].lineno;
9875 abort ();
9876 /*NOTREACHED*/
9877 return 0;
9881 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
9883 * As things stand, nothing is ever placed in the output buffer to be
9884 * removed again except when it's KNOWN to be part of an identifier,
9885 * so flushing and moving down everything left, instead of expanding,
9886 * should work ok.
9889 /* You might think void was cleaner for the return type,
9890 but that would get type mismatch in check_expand in strict ANSI. */
9892 static int
9893 grow_outbuf (obuf, needed)
9894 register FILE_BUF *obuf;
9895 register int needed;
9897 register U_CHAR *p;
9898 int minsize;
9900 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
9901 return 0;
9903 /* Make it at least twice as big as it is now. */
9904 obuf->length *= 2;
9905 /* Make it have at least 150% of the free space we will need. */
9906 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
9907 if (minsize > obuf->length)
9908 obuf->length = minsize;
9910 p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
9912 obuf->bufp = p + (obuf->bufp - obuf->buf);
9913 obuf->buf = p;
9915 return 0;
9918 /* Symbol table for macro names and special symbols */
9921 * install a name in the main hash table, even if it is already there.
9922 * name stops with first non alphanumeric, except leading '#'.
9923 * caller must check against redefinition if that is desired.
9924 * delete_macro () removes things installed by install () in fifo order.
9925 * this is important because of the `defined' special symbol used
9926 * in #if, and also if pushdef/popdef directives are ever implemented.
9928 * If LEN is >= 0, it is the length of the name.
9929 * Otherwise, compute the length by scanning the entire name.
9931 * If HASH is >= 0, it is the precomputed hash code.
9932 * Otherwise, compute the hash code.
9935 static HASHNODE *
9936 install (name, len, type, value, hash)
9937 const U_CHAR *name;
9938 int len;
9939 enum node_type type;
9940 const char *value;
9941 int hash;
9943 register HASHNODE *hp;
9944 register int i, bucket;
9945 register U_CHAR *p;
9946 register const U_CHAR *q;
9948 if (len < 0) {
9949 q = name;
9950 while (is_idchar[*q])
9951 q++;
9952 len = q - name;
9955 if (hash < 0)
9956 hash = hashf (name, len, HASHSIZE);
9958 i = sizeof (HASHNODE) + len + 1;
9959 hp = (HASHNODE *) xmalloc (i);
9960 bucket = hash;
9961 hp->bucket_hdr = &hashtab[bucket];
9962 hp->next = hashtab[bucket];
9963 hashtab[bucket] = hp;
9964 hp->prev = NULL;
9965 if (hp->next != NULL)
9966 hp->next->prev = hp;
9967 hp->type = type;
9968 hp->length = len;
9969 hp->value.cpval = value;
9970 hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
9971 p = hp->name;
9972 q = name;
9973 for (i = 0; i < len; i++)
9974 *p++ = *q++;
9975 hp->name[len] = 0;
9976 return hp;
9980 * find the most recent hash node for name "name" (ending with first
9981 * non-identifier char) installed by install
9983 * If LEN is >= 0, it is the length of the name.
9984 * Otherwise, compute the length by scanning the entire name.
9986 * If HASH is >= 0, it is the precomputed hash code.
9987 * Otherwise, compute the hash code.
9990 HASHNODE *
9991 lookup (name, len, hash)
9992 const U_CHAR *name;
9993 int len;
9994 int hash;
9996 register const U_CHAR *bp;
9997 register HASHNODE *bucket;
9999 if (len < 0) {
10000 for (bp = name; is_idchar[*bp]; bp++) ;
10001 len = bp - name;
10004 if (hash < 0)
10005 hash = hashf (name, len, HASHSIZE);
10007 bucket = hashtab[hash];
10008 while (bucket) {
10009 if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
10010 return bucket;
10011 bucket = bucket->next;
10013 return NULL;
10017 * Delete a hash node. Some weirdness to free junk from macros.
10018 * More such weirdness will have to be added if you define more hash
10019 * types that need it.
10022 /* Note that the DEFINITION of a macro is removed from the hash table
10023 but its storage is not freed. This would be a storage leak
10024 except that it is not reasonable to keep undefining and redefining
10025 large numbers of macros many times.
10026 In any case, this is necessary, because a macro can be #undef'd
10027 in the middle of reading the arguments to a call to it.
10028 If #undef freed the DEFINITION, that would crash. */
10030 static void
10031 delete_macro (hp)
10032 HASHNODE *hp;
10035 if (hp->prev != NULL)
10036 hp->prev->next = hp->next;
10037 if (hp->next != NULL)
10038 hp->next->prev = hp->prev;
10040 /* Make sure that the bucket chain header that the deleted guy was
10041 on points to the right thing afterwards. */
10042 if (hp == *hp->bucket_hdr)
10043 *hp->bucket_hdr = hp->next;
10045 #if 0
10046 if (hp->type == T_MACRO) {
10047 DEFINITION *d = hp->value.defn;
10048 struct reflist *ap, *nextap;
10050 for (ap = d->pattern; ap != NULL; ap = nextap) {
10051 nextap = ap->next;
10052 free (ap);
10054 free (d);
10056 #endif
10057 free (hp);
10061 * return hash function on name. must be compatible with the one
10062 * computed a step at a time, elsewhere
10065 static int
10066 hashf (name, len, hashsize)
10067 register const U_CHAR *name;
10068 register int len;
10069 int hashsize;
10071 register int r = 0;
10073 while (len--)
10074 r = HASHSTEP (r, *name++);
10076 return MAKE_POS (r) % hashsize;
10080 /* Dump the definition of a single macro HP to OF. */
10082 static void
10083 dump_single_macro (hp, of)
10084 register HASHNODE *hp;
10085 FILE *of;
10087 register DEFINITION *defn = hp->value.defn;
10088 struct reflist *ap;
10089 int offset;
10090 int concat;
10093 /* Print the definition of the macro HP. */
10095 fprintf (of, "#define %s", hp->name);
10097 if (defn->nargs >= 0) {
10098 int i;
10100 fprintf (of, "(");
10101 for (i = 0; i < defn->nargs; i++) {
10102 dump_arg_n (defn, i, of);
10103 if (i + 1 < defn->nargs)
10104 fprintf (of, ", ");
10106 fprintf (of, ")");
10109 fprintf (of, " ");
10111 offset = 0;
10112 concat = 0;
10113 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
10114 dump_defn_1 (defn->expansion, offset, ap->nchars, of);
10115 offset += ap->nchars;
10116 if (!traditional) {
10117 if (ap->nchars != 0)
10118 concat = 0;
10119 if (ap->stringify) {
10120 switch (ap->stringify) {
10121 case SHARP_TOKEN: fprintf (of, "#"); break;
10122 case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
10123 case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
10124 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
10125 default: abort ();
10128 if (ap->raw_before != 0) {
10129 if (concat) {
10130 switch (ap->raw_before) {
10131 case WHITE_SHARP_TOKEN:
10132 case WHITE_PERCENT_COLON_TOKEN:
10133 fprintf (of, " ");
10134 break;
10135 default:
10136 break;
10138 } else {
10139 switch (ap->raw_before) {
10140 case SHARP_TOKEN: fprintf (of, "##"); break;
10141 case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
10142 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10143 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
10144 default: abort ();
10148 concat = 0;
10150 dump_arg_n (defn, ap->argno, of);
10151 if (!traditional && ap->raw_after != 0) {
10152 switch (ap->raw_after) {
10153 case SHARP_TOKEN: fprintf (of, "##"); break;
10154 case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
10155 case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
10156 case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
10157 default: abort ();
10159 concat = 1;
10162 dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
10163 fprintf (of, "\n");
10166 /* Dump all macro definitions as #defines to stdout. */
10168 static void
10169 dump_all_macros ()
10171 int bucket;
10173 for (bucket = 0; bucket < HASHSIZE; bucket++) {
10174 register HASHNODE *hp;
10176 for (hp = hashtab[bucket]; hp; hp= hp->next) {
10177 if (hp->type == T_MACRO)
10178 dump_single_macro (hp, stdout);
10183 /* Output to OF a substring of a macro definition.
10184 BASE is the beginning of the definition.
10185 Output characters START thru LENGTH.
10186 Unless traditional, discard newlines outside of strings, thus
10187 converting funny-space markers to ordinary spaces. */
10189 static void
10190 dump_defn_1 (base, start, length, of)
10191 const U_CHAR *base;
10192 int start;
10193 int length;
10194 FILE *of;
10196 const U_CHAR *p = base + start;
10197 const U_CHAR *limit = base + start + length;
10199 if (traditional)
10200 fwrite (p, sizeof (*p), length, of);
10201 else {
10202 while (p < limit) {
10203 if (*p == '\"' || *p =='\'') {
10204 const U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
10205 NULL_PTR, NULL_PTR);
10206 fwrite (p, sizeof (*p), p1 - p, of);
10207 p = p1;
10208 } else {
10209 if (*p != '\n')
10210 putc (*p, of);
10211 p++;
10217 /* Print the name of argument number ARGNUM of macro definition DEFN
10218 to OF.
10219 Recall that DEFN->args.argnames contains all the arg names
10220 concatenated in reverse order with comma-space in between. */
10222 static void
10223 dump_arg_n (defn, argnum, of)
10224 DEFINITION *defn;
10225 int argnum;
10226 FILE *of;
10228 register U_CHAR *p = defn->args.argnames;
10229 while (argnum + 1 < defn->nargs) {
10230 p = (U_CHAR *) index ((char *) p, ' ') + 1;
10231 argnum++;
10234 while (*p && *p != ',') {
10235 putc (*p, of);
10236 p++;
10240 /* Initialize syntactic classifications of characters. */
10242 static void
10243 initialize_char_syntax ()
10245 register int i;
10248 * Set up is_idchar and is_idstart tables. These should be
10249 * faster than saying (is_alpha (c) || c == '_'), etc.
10250 * Set up these things before calling any routines tthat
10251 * refer to them.
10253 for (i = 'a'; i <= 'z'; i++) {
10254 is_idchar[TOUPPER(i)] = 1;
10255 is_idchar[i] = 1;
10256 is_idstart[TOUPPER(i)] = 1;
10257 is_idstart[i] = 1;
10259 for (i = '0'; i <= '9'; i++)
10260 is_idchar[i] = 1;
10261 is_idchar['_'] = 1;
10262 is_idstart['_'] = 1;
10263 is_idchar['$'] = 1;
10264 is_idstart['$'] = 1;
10266 /* horizontal space table */
10267 is_hor_space[' '] = 1;
10268 is_hor_space['\t'] = 1;
10269 is_hor_space['\v'] = 1;
10270 is_hor_space['\f'] = 1;
10271 is_hor_space['\r'] = 1;
10273 is_space[' '] = 1;
10274 is_space['\t'] = 1;
10275 is_space['\v'] = 1;
10276 is_space['\f'] = 1;
10277 is_space['\n'] = 1;
10278 is_space['\r'] = 1;
10281 /* Initialize the built-in macros. */
10283 static void
10284 initialize_builtins (inp, outp)
10285 FILE_BUF *inp;
10286 FILE_BUF *outp;
10288 install ((const U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
10289 install ((const U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
10290 install ((const U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
10291 install ((const U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
10292 install ((const U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
10293 install ((const U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
10294 #ifndef NO_BUILTIN_SIZE_TYPE
10295 install ((const U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
10296 #endif
10297 #ifndef NO_BUILTIN_PTRDIFF_TYPE
10298 install ((const U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
10299 #endif
10300 install ((const U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
10301 install ((const U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
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 sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
10350 output_line_directive (inp, outp, 0, same_file);
10351 pass_thru_directive (udirective, &udirective[strlen (directive)],
10352 outp, dp);
10354 sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
10355 monthnames[timebuf->tm_mon],
10356 timebuf->tm_mday, timebuf->tm_year + 1900);
10357 output_line_directive (inp, outp, 0, same_file);
10358 pass_thru_directive (udirective, &udirective[strlen (directive)],
10359 outp, dp);
10361 sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
10362 timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
10363 output_line_directive (inp, outp, 0, same_file);
10364 pass_thru_directive (udirective, &udirective[strlen (directive)],
10365 outp, dp);
10367 if (!traditional)
10369 sprintf (directive, " __STDC__ 1");
10370 output_line_directive (inp, outp, 0, same_file);
10371 pass_thru_directive (udirective, &udirective[strlen (directive)],
10372 outp, dp);
10374 if (objc)
10376 sprintf (directive, " __OBJC__ 1");
10377 output_line_directive (inp, outp, 0, same_file);
10378 pass_thru_directive (udirective, &udirective[strlen (directive)],
10379 outp, dp);
10385 * process a given definition string, for initialization
10386 * If STR is just an identifier, define it with value 1.
10387 * If STR has anything after the identifier, then it should
10388 * be identifier=definition.
10391 static void
10392 make_definition (str)
10393 char *str;
10395 FILE_BUF *ip;
10396 struct directive *kt;
10397 U_CHAR *buf, *p;
10399 p = buf = (U_CHAR *) str;
10400 if (!is_idstart[*p]) {
10401 error ("malformed option `-D %s'", str);
10402 return;
10404 while (is_idchar[*++p])
10406 if (*p == '(') {
10407 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
10409 if (*p++ != ')')
10410 p = (U_CHAR *) str; /* Error */
10412 if (*p == 0) {
10413 buf = (U_CHAR *) alloca (p - buf + 4);
10414 strcpy ((char *)buf, str);
10415 strcat ((char *)buf, " 1");
10416 } else if (*p != '=') {
10417 error ("malformed option `-D %s'", str);
10418 return;
10419 } else {
10420 U_CHAR *q;
10421 /* Copy the entire option so we can modify it. */
10422 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
10423 strncpy ((char *) buf, str, p - (U_CHAR *) str);
10424 /* Change the = to a space. */
10425 buf[p - (U_CHAR *) str] = ' ';
10426 /* Scan for any backslash-newline and remove it. */
10427 p++;
10428 q = &buf[p - (U_CHAR *) str];
10429 while (*p) {
10430 if (*p == '\"' || *p == '\'') {
10431 int unterminated = 0;
10432 const U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
10433 NULL_PTR, NULL_PTR, &unterminated);
10434 if (unterminated)
10435 return;
10436 while (p != p1) {
10437 if (*p == '\\' && p[1] == '\n')
10438 p += 2;
10439 else if (*p == '\n')
10441 *q++ = '\\';
10442 *q++ = 'n';
10443 p++;
10445 else
10446 *q++ = *p++;
10448 } else if (*p == '\\' && p[1] == '\n')
10449 p += 2;
10450 /* Change newline chars into newline-markers. */
10451 else if (*p == '\n')
10453 *q++ = '\n';
10454 *q++ = '\n';
10455 p++;
10457 else
10458 *q++ = *p++;
10460 *q = 0;
10463 ip = &instack[++indepth];
10464 ip->nominal_fname = ip->fname = "*Initialization*";
10465 ip->nominal_fname_len = strlen (ip->nominal_fname);
10467 ip->buf = ip->bufp = buf;
10468 ip->length = strlen ((char *) buf);
10469 ip->lineno = 1;
10470 ip->macro = 0;
10471 ip->free_ptr = 0;
10472 ip->if_stack = if_stack;
10473 ip->system_header_p = 0;
10475 for (kt = directive_table; kt->type != T_DEFINE; kt++)
10478 /* Pass NULL instead of OP, since this is a "predefined" macro. */
10479 do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
10480 --indepth;
10483 /* JF, this does the work for the -U option */
10485 static void
10486 make_undef (str, op)
10487 char *str;
10488 FILE_BUF *op;
10490 FILE_BUF *ip;
10491 struct directive *kt;
10493 ip = &instack[++indepth];
10494 ip->nominal_fname = ip->fname = "*undef*";
10495 ip->nominal_fname_len = strlen (ip->nominal_fname);
10497 ip->buf = ip->bufp = (U_CHAR *) str;
10498 ip->length = strlen (str);
10499 ip->lineno = 1;
10500 ip->macro = 0;
10501 ip->free_ptr = 0;
10502 ip->if_stack = if_stack;
10503 ip->system_header_p = 0;
10505 for (kt = directive_table; kt->type != T_UNDEF; kt++)
10508 do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
10509 --indepth;
10512 /* Process the string STR as if it appeared as the body of a #assert.
10513 OPTION is the option name for which STR was the argument. */
10515 static void
10516 make_assertion (option, str)
10517 const char *option;
10518 const char *str;
10520 FILE_BUF *ip;
10521 struct directive *kt;
10522 U_CHAR *buf, *p, *q;
10524 /* Copy the entire option so we can modify it. */
10525 buf = (U_CHAR *) alloca (strlen (str) + 1);
10526 strcpy ((char *) buf, str);
10527 /* Scan for any backslash-newline and remove it. */
10528 p = q = buf;
10529 while (*p) {
10530 if (*p == '\\' && p[1] == '\n')
10531 p += 2;
10532 else
10533 *q++ = *p++;
10535 *q = 0;
10537 p = buf;
10538 if (!is_idstart[*p]) {
10539 error ("malformed option `%s %s'", option, str);
10540 return;
10542 while (is_idchar[*++p])
10544 SKIP_WHITE_SPACE (p);
10545 if (! (*p == 0 || *p == '(')) {
10546 error ("malformed option `%s %s'", option, str);
10547 return;
10550 ip = &instack[++indepth];
10551 ip->nominal_fname = ip->fname = "*Initialization*";
10552 ip->nominal_fname_len = strlen (ip->nominal_fname);
10554 ip->buf = ip->bufp = buf;
10555 ip->length = strlen ((char *) buf);
10556 ip->lineno = 1;
10557 ip->macro = 0;
10558 ip->free_ptr = 0;
10559 ip->if_stack = if_stack;
10560 ip->system_header_p = 0;
10562 for (kt = directive_table; kt->type != T_ASSERT; kt++)
10565 /* Pass NULL as output ptr to do_define since we KNOW it never does
10566 any output.... */
10567 do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
10568 --indepth;
10571 /* The previous include prefix, if any, is PREV_FILE_NAME.
10572 Translate any pathnames with COMPONENT.
10573 Allocate a new include prefix whose name is the
10574 simplified concatenation of PREFIX and NAME,
10575 with a trailing / added if needed.
10576 But return 0 if the include prefix should be ignored,
10577 e.g. because it is a duplicate of PREV_FILE_NAME. */
10579 static struct file_name_list *
10580 new_include_prefix (prev_file_name, component, prefix, name)
10581 struct file_name_list *prev_file_name;
10582 const char *component;
10583 const char *prefix;
10584 const char *name;
10586 if (name == 0)
10587 fatal ("Directory name missing after command line option");
10589 if (*name == 0)
10590 /* Ignore the empty string. */
10591 return 0;
10593 prefix = update_path (prefix, component);
10594 name = update_path (name, component);
10597 struct file_name_list *dir
10598 = ((struct file_name_list *)
10599 xmalloc (sizeof (struct file_name_list)
10600 + strlen (prefix) + strlen (name) + 2));
10601 size_t len;
10602 strcpy (dir->fname, prefix);
10603 strcat (dir->fname, name);
10604 len = simplify_filename (dir->fname);
10606 /* Convert directory name to a prefix. */
10607 if (len && dir->fname[len - 1] != DIR_SEPARATOR) {
10608 if (len == 1 && dir->fname[len - 1] == '.')
10609 len = 0;
10610 else
10611 #ifdef VMS
10612 /* must be '/', hack_vms_include_specification triggers on it. */
10613 dir->fname[len++] = '/';
10614 #else
10615 dir->fname[len++] = DIR_SEPARATOR;
10616 #endif
10617 dir->fname[len] = 0;
10620 /* Ignore a directory whose name matches the previous one. */
10621 if (prev_file_name && !strcmp (prev_file_name->fname, dir->fname)) {
10622 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10623 if (!first_bracket_include)
10624 first_bracket_include = prev_file_name;
10625 free (dir);
10626 return 0;
10629 #ifndef VMS
10630 /* VMS can't stat dir prefixes, so skip these optimizations in VMS. */
10632 /* Add a trailing "." if there is a filename. This increases the number
10633 of systems that can stat directories. We remove it below. */
10634 if (len != 0)
10636 dir->fname[len] = '.';
10637 dir->fname[len + 1] = 0;
10640 /* Ignore a nonexistent directory. */
10641 if (stat (len ? dir->fname : ".", &dir->st) != 0) {
10642 if (errno != ENOENT && errno != ENOTDIR)
10643 error_from_errno (dir->fname);
10644 free (dir);
10645 return 0;
10648 if (len != 0)
10649 dir->fname[len] = 0;
10651 /* Ignore a directory whose identity matches the previous one. */
10652 if (prev_file_name
10653 && INO_T_EQ (prev_file_name->st.st_ino, dir->st.st_ino)
10654 && prev_file_name->st.st_dev == dir->st.st_dev) {
10655 /* But treat `-Idir -I- -Idir' as `-I- -Idir'. */
10656 if (!first_bracket_include)
10657 first_bracket_include = prev_file_name;
10658 free (dir);
10659 return 0;
10661 #endif /* ! VMS */
10663 dir->next = 0;
10664 dir->c_system_include_path = 0;
10665 dir->got_name_map = 0;
10667 return dir;
10671 /* Append a chain of `struct file_name_list's
10672 to the end of the main include chain.
10673 FIRST is the beginning of the chain to append, and LAST is the end. */
10675 static void
10676 append_include_chain (first, last)
10677 struct file_name_list *first, *last;
10679 struct file_name_list *dir;
10681 if (!first || !last)
10682 return;
10684 if (include == 0)
10685 include = first;
10686 else
10687 last_include->next = first;
10689 if (first_bracket_include == 0)
10690 first_bracket_include = first;
10692 for (dir = first; ; dir = dir->next) {
10693 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
10694 if (len > max_include_len)
10695 max_include_len = len;
10696 if (dir == last)
10697 break;
10700 last->next = NULL;
10701 last_include = last;
10704 /* Place into DST a representation of the file named SRC that is suitable
10705 for `make'. Do not null-terminate DST. Return its length. */
10706 static int
10707 quote_string_for_make (dst, src)
10708 char *dst;
10709 const char *src;
10711 const char *p = src;
10712 int i = 0;
10713 for (;;)
10715 char c = *p++;
10716 switch (c)
10718 case '\0':
10719 case ' ':
10720 case '\t':
10722 /* GNU make uses a weird quoting scheme for white space.
10723 A space or tab preceded by 2N+1 backslashes represents
10724 N backslashes followed by space; a space or tab
10725 preceded by 2N backslashes represents N backslashes at
10726 the end of a file name; and backslashes in other
10727 contexts should not be doubled. */
10728 const char *q;
10729 for (q = p - 1; src < q && q[-1] == '\\'; q--)
10731 if (dst)
10732 dst[i] = '\\';
10733 i++;
10736 if (!c)
10737 return i;
10738 if (dst)
10739 dst[i] = '\\';
10740 i++;
10741 goto ordinary_char;
10743 case '$':
10744 if (dst)
10745 dst[i] = c;
10746 i++;
10747 /* Fall through. This can mishandle things like "$(" but
10748 there's no easy fix. */
10749 default:
10750 ordinary_char:
10751 /* This can mishandle characters in the string "\0\n%*?[\\~";
10752 exactly which chars are mishandled depends on the `make' version.
10753 We know of no portable solution for this;
10754 even GNU make 3.76.1 doesn't solve the problem entirely.
10755 (Also, '\0' is mishandled due to our calling conventions.) */
10756 if (dst)
10757 dst[i] = c;
10758 i++;
10759 break;
10765 /* Add output to `deps_buffer' for the -M switch.
10766 STRING points to the text to be output.
10767 SPACER is ':' for targets, ' ' for dependencies. */
10769 static void
10770 deps_output (string, spacer)
10771 const char *string;
10772 int spacer;
10774 int size = quote_string_for_make ((char *) 0, string);
10776 if (size == 0)
10777 return;
10779 #ifndef MAX_OUTPUT_COLUMNS
10780 #define MAX_OUTPUT_COLUMNS 72
10781 #endif
10782 if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
10783 && 1 < deps_column) {
10784 bcopy (" \\\n ", &deps_buffer[deps_size], 4);
10785 deps_size += 4;
10786 deps_column = 1;
10787 if (spacer == ' ')
10788 spacer = 0;
10791 if (deps_size + 2 * size + 8 > deps_allocated_size) {
10792 deps_allocated_size = (deps_size + 2 * size + 50) * 2;
10793 deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
10795 if (spacer == ' ') {
10796 deps_buffer[deps_size++] = ' ';
10797 deps_column++;
10799 quote_string_for_make (&deps_buffer[deps_size], string);
10800 deps_size += size;
10801 deps_column += size;
10802 if (spacer == ':') {
10803 deps_buffer[deps_size++] = ':';
10804 deps_column++;
10806 deps_buffer[deps_size] = 0;
10809 void
10810 fatal VPARAMS ((const char * msgid, ...))
10812 #ifndef ANSI_PROTOTYPES
10813 const char * msgid;
10814 #endif
10815 va_list args;
10817 fprintf (stderr, "%s: ", progname);
10818 VA_START (args, msgid);
10820 #ifndef ANSI_PROTOTYPES
10821 msgid = va_arg (args, const char *);
10822 #endif
10823 vnotice (msgid, args);
10824 va_end (args);
10825 fprintf (stderr, "\n");
10826 exit (FATAL_EXIT_CODE);
10829 /* More 'friendly' abort that prints the line and file.
10830 config.h can #define abort fancy_abort if you like that sort of thing. */
10832 void
10833 fancy_abort ()
10835 fatal ("Internal gcc abort.");
10838 static void
10839 perror_with_name (name)
10840 const char *name;
10842 fprintf (stderr, "%s: %s: %s\n", progname, name, xstrerror (errno));
10843 errors++;
10846 static void
10847 pfatal_with_name (name)
10848 const char *name;
10850 perror_with_name (name);
10851 #ifdef VMS
10852 exit (vaxc$errno);
10853 #else
10854 exit (FATAL_EXIT_CODE);
10855 #endif
10858 /* Handler for SIGPIPE. */
10860 static void
10861 pipe_closed (signo)
10862 /* If this is missing, some compilers complain. */
10863 int signo ATTRIBUTE_UNUSED;
10865 fatal ("output pipe has been closed");
10868 static void
10869 memory_full ()
10871 fatal ("Memory exhausted.");
10874 #ifdef VMS
10876 /* Under VMS we need to fix up the "include" specification filename.
10878 Rules for possible conversions
10880 fullname tried paths
10882 name name
10883 ./dir/name [.dir]name
10884 /dir/name dir:name
10885 /name [000000]name, name
10886 dir/name dir:[000000]name, dir:name, dir/name
10887 dir1/dir2/name dir1:[dir2]name, dir1:[000000.dir2]name
10888 path:/name path:[000000]name, path:name
10889 path:/dir/name path:[000000.dir]name, path:[dir]name
10890 path:dir/name path:[dir]name
10891 [path]:[dir]name [path.dir]name
10892 path/[dir]name [path.dir]name
10894 The path:/name input is constructed when expanding <> includes.
10896 return 1 if name was changed, 0 else. */
10898 static int
10899 hack_vms_include_specification (fullname, vaxc_include)
10900 char *fullname;
10901 int vaxc_include;
10903 register char *basename, *unixname, *local_ptr, *first_slash;
10904 int f, check_filename_before_returning, must_revert;
10905 char Local[512];
10907 check_filename_before_returning = 0;
10908 must_revert = 0;
10909 /* See if we can find a 1st slash. If not, there's no path information. */
10910 first_slash = index (fullname, '/');
10911 if (first_slash == 0)
10912 return 0; /* Nothing to do!!! */
10914 /* construct device spec if none given. */
10916 if (index (fullname, ':') == 0)
10919 /* If fullname has a slash, take it as device spec. */
10921 if (first_slash == fullname)
10923 first_slash = index (fullname+1, '/'); /* 2nd slash ? */
10924 if (first_slash)
10925 *first_slash = ':'; /* make device spec */
10926 for (basename = fullname; *basename != 0; basename++)
10927 *basename = *(basename+1); /* remove leading slash */
10929 else if ((first_slash[-1] != '.') /* keep ':/', './' */
10930 && (first_slash[-1] != ':')
10931 && (first_slash[-1] != ']')) /* or a vms path */
10933 *first_slash = ':';
10935 else if ((first_slash[1] == '[') /* skip './' in './[dir' */
10936 && (first_slash[-1] == '.'))
10937 fullname += 2;
10940 /* Get part after first ':' (basename[-1] == ':')
10941 or last '/' (basename[-1] == '/'). */
10943 basename = base_name (fullname);
10946 * Check if we have a vax-c style '#include filename'
10947 * and add the missing .h
10950 if (vaxc_include && !index (basename,'.'))
10951 strcat (basename, ".h");
10953 local_ptr = Local; /* initialize */
10955 /* We are trying to do a number of things here. First of all, we are
10956 trying to hammer the filenames into a standard format, such that later
10957 processing can handle them.
10959 If the file name contains something like [dir.], then it recognizes this
10960 as a root, and strips the ".]". Later processing will add whatever is
10961 needed to get things working properly.
10963 If no device is specified, then the first directory name is taken to be
10964 a device name (or a rooted logical). */
10966 /* Point to the UNIX filename part (which needs to be fixed!)
10967 but skip vms path information.
10968 [basename != fullname since first_slash != 0]. */
10970 if ((basename[-1] == ':') /* vms path spec. */
10971 || (basename[-1] == ']')
10972 || (basename[-1] == '>'))
10973 unixname = basename;
10974 else
10975 unixname = fullname;
10977 if (*unixname == '/')
10978 unixname++;
10980 /* If the directory spec is not rooted, we can just copy
10981 the UNIX filename part and we are done. */
10983 if (((basename - fullname) > 1)
10984 && ( (basename[-1] == ']')
10985 || (basename[-1] == '>')))
10987 if (basename[-2] != '.')
10990 /* The VMS part ends in a `]', and the preceding character is not a `.'.
10991 -> PATH]:/name (basename = '/name', unixname = 'name')
10992 We strip the `]', and then splice the two parts of the name in the
10993 usual way. Given the default locations for include files in cccp.c,
10994 we will only use this code if the user specifies alternate locations
10995 with the /include (-I) switch on the command line. */
10997 basename -= 1; /* Strip "]" */
10998 unixname--; /* backspace */
11000 else
11003 /* The VMS part has a ".]" at the end, and this will not do. Later
11004 processing will add a second directory spec, and this would be a syntax
11005 error. Thus we strip the ".]", and thus merge the directory specs.
11006 We also backspace unixname, so that it points to a '/'. This inhibits the
11007 generation of the 000000 root directory spec (which does not belong here
11008 in this case). */
11010 basename -= 2; /* Strip ".]" */
11011 unixname--; /* backspace */
11015 else
11019 /* We drop in here if there is no VMS style directory specification yet.
11020 If there is no device specification either, we make the first dir a
11021 device and try that. If we do not do this, then we will be essentially
11022 searching the users default directory (as if they did a #include "asdf.h").
11024 Then all we need to do is to push a '[' into the output string. Later
11025 processing will fill this in, and close the bracket. */
11027 if ((unixname != fullname) /* vms path spec found. */
11028 && (basename[-1] != ':'))
11029 *local_ptr++ = ':'; /* dev not in spec. take first dir */
11031 *local_ptr++ = '['; /* Open the directory specification */
11034 if (unixname == fullname) /* no vms dir spec. */
11036 must_revert = 1;
11037 if ((first_slash != 0) /* unix dir spec. */
11038 && (*unixname != '/') /* not beginning with '/' */
11039 && (*unixname != '.')) /* or './' or '../' */
11040 *local_ptr++ = '.'; /* dir is local ! */
11043 /* at this point we assume that we have the device spec, and (at least
11044 the opening "[" for a directory specification. We may have directories
11045 specified already.
11047 If there are no other slashes then the filename will be
11048 in the "root" directory. Otherwise, we need to add
11049 directory specifications. */
11051 if (index (unixname, '/') == 0)
11053 /* if no directories specified yet and none are following. */
11054 if (local_ptr[-1] == '[')
11056 /* Just add "000000]" as the directory string */
11057 strcpy (local_ptr, "000000]");
11058 local_ptr += strlen (local_ptr);
11059 check_filename_before_returning = 1; /* we might need to fool with this later */
11062 else
11065 /* As long as there are still subdirectories to add, do them. */
11066 while (index (unixname, '/') != 0)
11068 /* If this token is "." we can ignore it
11069 if it's not at the beginning of a path. */
11070 if ((unixname[0] == '.') && (unixname[1] == '/'))
11072 /* remove it at beginning of path. */
11073 if ( ((unixname == fullname) /* no device spec */
11074 && (fullname+2 != basename)) /* starts with ./ */
11075 /* or */
11076 || ((basename[-1] == ':') /* device spec */
11077 && (unixname-1 == basename))) /* and ./ afterwards */
11078 *local_ptr++ = '.'; /* make '[.' start of path. */
11079 unixname += 2;
11080 continue;
11083 /* Add a subdirectory spec. Do not duplicate "." */
11084 if ( local_ptr[-1] != '.'
11085 && local_ptr[-1] != '['
11086 && local_ptr[-1] != '<')
11087 *local_ptr++ = '.';
11089 /* If this is ".." then the spec becomes "-" */
11090 if ( (unixname[0] == '.')
11091 && (unixname[1] == '.')
11092 && (unixname[2] == '/'))
11094 /* Add "-" and skip the ".." */
11095 if ((local_ptr[-1] == '.')
11096 && (local_ptr[-2] == '['))
11097 local_ptr--; /* prevent [.- */
11098 *local_ptr++ = '-';
11099 unixname += 3;
11100 continue;
11103 /* Copy the subdirectory */
11104 while (*unixname != '/')
11105 *local_ptr++= *unixname++;
11107 unixname++; /* Skip the "/" */
11110 /* Close the directory specification */
11111 if (local_ptr[-1] == '.') /* no trailing periods */
11112 local_ptr--;
11114 if (local_ptr[-1] == '[') /* no dir needed */
11115 local_ptr--;
11116 else
11117 *local_ptr++ = ']';
11120 /* Now add the filename. */
11122 while (*unixname)
11123 *local_ptr++ = *unixname++;
11124 *local_ptr = 0;
11126 /* Now append it to the original VMS spec. */
11128 strcpy ((must_revert==1)?fullname:basename, Local);
11130 /* If we put a [000000] in the filename, try to open it first. If this fails,
11131 remove the [000000], and return that name. This provides flexibility
11132 to the user in that they can use both rooted and non-rooted logical names
11133 to point to the location of the file. */
11135 if (check_filename_before_returning)
11137 f = open (fullname, O_RDONLY, 0666);
11138 if (f >= 0)
11140 /* The file name is OK as it is, so return it as is. */
11141 close (f);
11142 return 1;
11145 /* The filename did not work. Try to remove the [000000] from the name,
11146 and return it. */
11148 basename = index (fullname, '[');
11149 local_ptr = index (fullname, ']') + 1;
11150 strcpy (basename, local_ptr); /* this gets rid of it */
11154 return 1;
11156 #endif /* VMS */
11158 #ifdef VMS
11160 /* The following wrapper functions supply additional arguments to the VMS
11161 I/O routines to optimize performance with file handling. The arguments
11162 are:
11163 "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
11164 "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
11165 "fop=tef"- Truncate unused portions of file when closing file.
11166 "shr=nil"- Disallow file sharing while file is open. */
11168 static FILE *
11169 VMS_freopen (fname, type, oldfile)
11170 char *fname;
11171 char *type;
11172 FILE *oldfile;
11174 #undef freopen /* Get back the real freopen routine. */
11175 if (strcmp (type, "w") == 0)
11176 return freopen (fname, type, oldfile,
11177 "mbc=16", "deq=64", "fop=tef", "shr=nil");
11178 return freopen (fname, type, oldfile, "mbc=16");
11181 static FILE *
11182 VMS_fopen (fname, type)
11183 char *fname;
11184 char *type;
11186 #undef fopen /* Get back the real fopen routine. */
11187 /* The gcc-vms-1.42 distribution's header files prototype fopen with two
11188 fixed arguments, which matches ANSI's specification but not VAXCRTL's
11189 pre-ANSI implementation. This hack circumvents the mismatch problem. */
11190 FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
11192 if (*type == 'w')
11193 return (*vmslib_fopen) (fname, type, "mbc=32",
11194 "deq=64", "fop=tef", "shr=nil");
11195 else
11196 return (*vmslib_fopen) (fname, type, "mbc=32");
11199 static int
11200 VMS_open (fname, flags, prot)
11201 char *fname;
11202 int flags;
11203 int prot;
11205 #undef open /* Get back the real open routine. */
11206 return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
11209 /* more VMS hackery */
11210 #include <fab.h>
11211 #include <nam.h>
11213 extern unsigned long SYS$PARSE(), SYS$SEARCH();
11215 /* Work around another library bug. If a file is located via a searchlist,
11216 and if the device it's on is not the same device as the one specified
11217 in the first element of that searchlist, then both stat() and fstat()
11218 will fail to return info about it. `errno' will be set to EVMSERR, and
11219 `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
11220 We can get around this by fully parsing the filename and then passing
11221 that absolute name to stat().
11223 Without this fix, we can end up failing to find header files, which is
11224 bad enough, but then compounding the problem by reporting the reason for
11225 failure as "normal successful completion." */
11227 #undef fstat /* Get back to the library version. */
11229 static int
11230 VMS_fstat (fd, statbuf)
11231 int fd;
11232 struct stat *statbuf;
11234 int result = fstat (fd, statbuf);
11236 if (result < 0)
11238 FILE *fp;
11239 char nambuf[NAM$C_MAXRSS+1];
11241 if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
11242 result = VMS_stat (nambuf, statbuf);
11243 /* No fclose(fp) here; that would close(fd) as well. */
11246 return result;
11249 static int
11250 VMS_stat (name, statbuf)
11251 const char *name;
11252 struct stat *statbuf;
11254 int result = stat (name, statbuf);
11256 if (result < 0)
11258 struct FAB fab;
11259 struct NAM nam;
11260 char exp_nam[NAM$C_MAXRSS+1], /* expanded name buffer for SYS$PARSE */
11261 res_nam[NAM$C_MAXRSS+1]; /* resultant name buffer for SYS$SEARCH */
11263 fab = cc$rms_fab;
11264 fab.fab$l_fna = (char *) name;
11265 fab.fab$b_fns = (unsigned char) strlen (name);
11266 fab.fab$l_nam = (void *) &nam;
11267 nam = cc$rms_nam;
11268 nam.nam$l_esa = exp_nam, nam.nam$b_ess = sizeof exp_nam - 1;
11269 nam.nam$l_rsa = res_nam, nam.nam$b_rss = sizeof res_nam - 1;
11270 nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
11271 if (SYS$PARSE (&fab) & 1)
11273 if (SYS$SEARCH (&fab) & 1)
11275 res_nam[nam.nam$b_rsl] = '\0';
11276 result = stat (res_nam, statbuf);
11278 /* Clean up searchlist context cached by the system. */
11279 nam.nam$b_nop = NAM$M_SYNCHK;
11280 fab.fab$l_fna = 0, fab.fab$b_fns = 0;
11281 (void) SYS$PARSE (&fab);
11285 return result;
11288 static size_t
11289 VMS_fwrite (ptr, size, nitems, stream)
11290 void const *ptr;
11291 size_t size;
11292 size_t nitems;
11293 FILE *stream;
11295 /* VMS fwrite has undesirable results
11296 if STREAM happens to be a record oriented file.
11297 Work around this problem by writing each character individually. */
11298 char const *p = ptr;
11299 size_t bytes = size * nitems;
11300 char *lim = p + bytes;
11302 while (p < lim)
11303 if (putc (*p++, stream) == EOF)
11304 return 0;
11306 return bytes;
11308 #endif /* VMS */