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