1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 2000, 2001 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
5 Dusted off, polished, and adapted for use as traditional
6 preprocessor only, Zack Weinberg, Jul 2000
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "cppdefault.h"
30 typedef unsigned char U_CHAR
;
32 /* Name under which this program was invoked. */
34 static const char *progname
;
36 /* Current maximum length of directory names in the search path
37 for include files. (Altered as we get more of them.) */
39 size_t max_include_len
;
41 /* Nonzero means copy comments into the output file. */
43 int put_out_comments
= 0;
45 /* mkdeps.h opaque structure that encapsulates dependency information. */
48 /* Nonzero means print the names of included files rather than
49 the preprocessed output. 1 means just the #include "...",
50 2 means #include <...> as well. */
54 /* Nonzero means print dummy targets for each header file. */
56 int print_deps_phony_targets
= 0;
58 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
62 /* File name which deps are being written to. This is 0 if deps are
63 being written to stdout. */
65 const char *deps_file
= 0;
67 /* Nonzero if missing .h files in -M output are assumed to be
68 generated files and not errors. */
70 int deps_missing_files
= 0;
72 /* Nonzero means don't output line number information. */
76 /* Nonzero means inhibit output of the preprocessed text
77 and instead output the definitions of all user-defined macros
78 in a form suitable for use as input to cccp. */
82 /* Nonzero means don't print warning messages. -w. */
84 int inhibit_warnings
= 0;
86 /* Non-0 means don't output the preprocessed program. */
87 int inhibit_output
= 0;
89 /* Nonzero means chars are signed. */
90 #if DEFAULT_SIGNED_CHAR
91 int flag_signed_char
= 1;
93 int flag_signed_char
= 0;
96 /* Nonzero means warn if slash-star appears in a comment. */
100 /* Nonzero causes output not to be done,
101 but directives such as #define that have side effects
106 /* Value of __USER_LABEL_PREFIX__. Target-dependent, also controlled
107 by -f(no-)leading-underscore. */
108 static const char *user_label_prefix
;
110 /* I/O buffer structure.
111 The `fname' field is nonzero for source files and #include files
112 and for the dummy text used for -D and -U.
113 It is zero for rescanning results of macro expansion
114 and for expanding macro arguments. */
115 #define INPUT_STACK_MAX 200
116 struct file_name_list
;
123 /* Macro that this level is the expansion of.
124 Included so that we can reenable the macro
125 at the end of this level. */
126 struct hashnode
*macro
;
127 /* Value of if_stack at start of this file.
128 Used to prohibit unmatched #endif (etc) in an include file. */
129 struct if_stack
*if_stack
;
130 /* Object to be freed at end of input at this level. */
132 /* Position to start scanning for #include_next in this file. */
133 struct file_name_list
*next_header_dir
;
134 } instack
[INPUT_STACK_MAX
];
136 typedef struct file_buf FILE_BUF
;
138 /* Current nesting level of input sources.
139 `instack[indepth]' is the level currently being read. */
141 #define CHECK_DEPTH(code) \
142 if (indepth >= (INPUT_STACK_MAX - 1)) \
144 error_with_line (line_for_error (instack[indepth].lineno), \
145 "macro or #include recursion too deep"); \
149 /* Current depth in #include directives that use <...>. */
150 int system_include_depth
= 0;
152 /* The output buffer. Its LENGTH field is the amount of room allocated
153 for the buffer, not the number of chars actually present. To get
154 that, subtract outbuf.buf from outbuf.bufp. */
156 #define OUTBUF_SIZE 10 /* initial size of output buffer */
159 /* Grow output buffer OBUF points at
160 so it can hold at least NEEDED more chars. */
162 #define check_expand(OBUF, NEEDED) do { \
163 if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
164 grow_outbuf ((OBUF), (NEEDED)); \
167 struct file_name_list
169 struct file_name_list
*next
;
173 struct file_name_list
*include
= 0; /* First dir to search */
174 /* First dir to search for <file> */
175 struct file_name_list
*first_bracket_include
= 0;
176 struct file_name_list
*last_include
= 0; /* Last in chain */
178 /* List of included files that contained #once. */
179 struct file_name_list
*dont_repeat_files
= 0;
181 /* List of other included files. */
182 struct file_name_list
*all_include_files
= 0;
184 /* Structure allocated for every #define. For a simple replacement
187 nargs = -1, the `pattern' list is null, and the expansion is just
188 the replacement text. Nargs = 0 means a functionlike macro with no args,
190 #define getchar() getc (stdin) .
191 When there are args, the expansion is the replacement text with the
192 args squashed out, and the reflist is a list describing how to
193 build the output from the input: e.g., "3 chars, then the 1st arg,
194 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
195 The chars here come from the expansion. Whatever is left of the
196 expansion after the last arg-occurrence is copied after that arg.
197 Note that the reflist can be arbitrarily long---
198 its length depends on the number of times the arguments appear in
199 the replacement text, not how many args there are. Example:
200 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
202 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
203 where (x, y) means (nchars, argno). */
205 typedef struct definition DEFINITION
;
208 int length
; /* length of expansion string */
211 struct reflist
*next
;
212 char stringify
; /* nonzero if this arg was preceded by a
214 char raw_before
; /* Nonzero if a ## operator before arg. */
215 char raw_after
; /* Nonzero if a ## operator after arg. */
216 int nchars
; /* Number of literal chars to copy before
217 this arg occurrence. */
218 int argno
; /* Number of arg to substitute (origin-0) */
220 /* Names of macro args, concatenated in reverse order
221 with comma-space between them.
222 The only use of this is that we warn on redefinition
223 if this differs between the old and new definitions. */
224 const U_CHAR
*argnames
;
227 /* Chained list of answers to an assertion. */
231 const unsigned char *answer
;
235 /* different kinds of things that can appear in the value field
236 of a hash node. Actually, this may be useless now. */
240 struct answer
*answers
;
243 /* The structure of a node in the hash table. The hash table
244 has entries for all tokens defined by #define commands (type T_MACRO),
245 plus some special tokens like __LINE__ (these each have their own
246 type, and the appropriate code is run when that type of node is seen.
247 It does not contain control words like "#define", which are recognized
248 by a separate piece of code. */
250 /* different flavors of hash nodes --- also used in keyword table */
252 T_DEFINE
= 1, /* `#define' */
253 T_INCLUDE
, /* `#include' */
254 T_INCLUDE_NEXT
,/* `#include_next' */
255 T_IFDEF
, /* `#ifdef' */
256 T_IFNDEF
, /* `#ifndef' */
258 T_ELSE
, /* `#else' */
259 T_ELIF
, /* `#elif' */
260 T_UNDEF
, /* `#undef' */
261 T_LINE
, /* `#line' */
262 T_ENDIF
, /* `#endif' */
263 T_ERROR
, /* `#error' */
264 T_WARNING
, /* `#warning' */
265 T_ASSERT
, /* `#assert' */
266 T_UNASSERT
, /* `#unassert' */
267 T_SPECLINE
, /* special symbol `__LINE__' */
268 T_DATE
, /* `__DATE__' */
269 T_FILE
, /* `__FILE__' */
270 T_BASE_FILE
, /* `__BASE_FILE__' */
271 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
272 T_VERSION
, /* `__VERSION__' */
273 T_TIME
, /* `__TIME__' */
274 T_CONST
, /* Constant value, used by `__STDC__' */
275 T_MACRO
, /* macro defined by `#define' */
276 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
277 T_UNUSED
/* Used for something not defined. */
281 struct hashnode
*next
; /* double links for easy deletion */
282 struct hashnode
*prev
;
283 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
284 chain is kept, in case the node is the head
285 of the chain and gets deleted. */
286 enum node_type type
; /* type of special token */
287 int length
; /* length of token, for quick comparison */
288 U_CHAR
*name
; /* the actual name */
289 union hashval value
; /* pointer to expansion, or whatever */
292 typedef struct hashnode HASHNODE
;
294 static HASHNODE
*parse_assertion
PARAMS ((const unsigned char *,
295 const unsigned char *,
296 struct answer
**, int));
297 static struct answer
**find_answer
PARAMS ((HASHNODE
*,
298 const struct answer
*));
299 static int parse_answer
PARAMS ((const unsigned char *, const unsigned char *,
300 struct answer
**, int));
301 static unsigned char *canonicalize_text
PARAMS ((const unsigned char *,
302 const unsigned char *,
303 const unsigned char **));
305 /* Some definitions for the hash table. The hash function MUST be
306 computed as shown in hashf () below. That is because the rescan
307 loop computes the hash value `on the fly' for most tokens,
308 in order to avoid the overhead of a lot of procedure calls to
309 the hashf () function. Hashf () only exists for the sake of
310 politeness, for use when speed isn't so important. */
312 #define HASHSIZE 1403
313 HASHNODE
*hashtab
[HASHSIZE
];
314 #define HASHSTEP(old, c) ((old << 2) + c)
315 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
317 /* `struct directive' defines one #-directive, including how to handle it. */
320 const int length
; /* Length of name */
321 void (*const func
) PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
322 /* Function to handle directive */
323 const char *const name
; /* Name of directive */
324 const enum node_type type
; /* Code which describes which directive. */
327 /* Last arg to output_line_command. */
328 enum file_change_code
{same_file
, enter_file
, leave_file
};
330 /* This structure represents one parsed argument in a macro call.
331 `raw' points to the argument text as written (`raw_length' is its length).
332 `expanded' points to the argument's macro-expansion
333 (its length is `expand_length').
334 `stringified_length' is the length the argument would have
336 `free1' and `free2', if nonzero, point to blocks to be freed
337 when the macro argument data is no longer needed. */
340 U_CHAR
*raw
, *expanded
;
341 int raw_length
, expand_length
;
342 int stringified_length
;
343 U_CHAR
*free1
, *free2
;
348 /* The arglist structure is built by do_define to tell
349 collect_definition where the argument names begin. That
350 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
351 would contain pointers to the strings x, y, and z.
352 Collect_definition would then build a DEFINITION node,
353 with reflist nodes pointing to the places x, y, and z had
354 appeared. So the arglist is just convenience data passed
355 between these two routines. It is not kept around after
356 the current #define has been processed and entered into the
360 struct arglist
*next
;
366 /* Function prototypes. */
368 static void do_define
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
369 static void do_error
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
370 static void do_warning
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
371 static void do_line
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
372 static void do_include
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
373 static void do_include_next
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
374 static void do_undef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
375 static void do_if
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
376 static void do_ifdef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
377 static void do_ifndef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
378 static void do_else
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
379 static void do_elif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
380 static void do_endif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
381 static void do_assert
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
382 static void do_unassert
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
383 static void do_xifdef
PARAMS ((U_CHAR
*, U_CHAR
*, enum node_type
));
385 static struct hashnode
*install
PARAMS ((const U_CHAR
*, int, enum node_type
, int));
386 static int hashf
PARAMS ((const U_CHAR
*, int, int));
387 static int compare_defs
PARAMS ((DEFINITION
*, DEFINITION
*));
388 static int comp_def_part
PARAMS ((int, const U_CHAR
*, int,
389 const U_CHAR
*, int, int));
390 static void delete_macro
PARAMS ((HASHNODE
*));
392 /* First arg to v_message. */
393 enum msgtype
{ MT_WARNING
= 0, MT_ERROR
, MT_FATAL
};
394 static void v_message
PARAMS ((enum msgtype mtype
, int line
,
395 const char *msgid
, va_list ap
))
396 ATTRIBUTE_PRINTF (3, 0);
398 static int line_for_error
PARAMS ((int));
400 /* We know perfectly well which file this is, so we don't need to
403 #if (GCC_VERSION >= 2007)
404 #define abort() fancy_abort(__LINE__, __FUNCTION__)
406 #define abort() fancy_abort(__LINE__, 0);
409 static void macroexpand
PARAMS ((HASHNODE
*, FILE_BUF
*));
410 static void special_symbol
PARAMS ((HASHNODE
*, FILE_BUF
*));
411 static void dump_all_macros
PARAMS ((void));
412 static void dump_defn_1
PARAMS ((const U_CHAR
*, int, int));
413 static void dump_arg_n
PARAMS ((DEFINITION
*, int));
414 static void conditional_skip
PARAMS ((FILE_BUF
*, int, enum node_type
));
415 static void skip_if_group
PARAMS ((FILE_BUF
*, int));
416 static void output_line_command
PARAMS ((FILE_BUF
*, FILE_BUF
*,
417 int, enum file_change_code
));
419 static int eval_if_expression
PARAMS ((const U_CHAR
*, int));
421 static void output_deps
PARAMS ((void));
422 static void initialize_builtins
PARAMS ((void));
423 static void run_directive
PARAMS ((const char *, size_t,
425 static void make_definition
PARAMS ((const char *));
426 static void make_undef
PARAMS ((const char *));
427 static void make_assertion
PARAMS ((const char *));
429 static void grow_outbuf
PARAMS ((FILE_BUF
*, int));
430 static int handle_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*));
431 static void process_include
PARAMS ((struct file_name_list
*,
432 const U_CHAR
*, int, int, FILE_BUF
*));
433 static void finclude
PARAMS ((int, const char *,
434 struct file_name_list
*, FILE_BUF
*));
435 static void init_dependency_output
PARAMS ((void));
436 static void rescan
PARAMS ((FILE_BUF
*, int));
437 static void newline_fix
PARAMS ((U_CHAR
*));
438 static void name_newline_fix
PARAMS ((U_CHAR
*));
439 static U_CHAR
*macarg1
PARAMS ((U_CHAR
*, const U_CHAR
*, int *,
441 static const char *macarg
PARAMS ((struct argdata
*));
442 static int discard_comments
PARAMS ((U_CHAR
*, int, int));
443 static int file_size_and_mode
PARAMS ((int, int *, long *));
445 static U_CHAR
*skip_to_end_of_comment
PARAMS ((FILE_BUF
*, int *));
446 static U_CHAR
*skip_quoted_string
PARAMS ((const U_CHAR
*, const U_CHAR
*,
447 int, int *, int *, int *));
449 int main
PARAMS ((int, char **));
451 /* Convenience. Write U"string" to get an unsigned string constant. */
452 #define U (const unsigned char *)
454 /* Here is the actual list of #-directives, most-often-used first. */
456 static const struct directive directive_table
[] = {
457 { 6, do_define
, "define", T_DEFINE
},
458 { 7, do_include
, "include", T_INCLUDE
},
459 { 5, do_endif
, "endif", T_ENDIF
},
460 { 5, do_ifdef
, "ifdef", T_IFDEF
},
461 { 2, do_if
, "if", T_IF
, },
462 { 4, do_else
, "else", T_ELSE
},
463 { 6, do_ifndef
, "ifndef", T_IFNDEF
},
464 { 5, do_undef
, "undef", T_UNDEF
},
465 { 4, do_line
, "line", T_LINE
},
466 { 4, do_elif
, "elif", T_ELIF
},
467 { 5, do_error
, "error", T_ERROR
},
468 { 7, do_warning
, "warning", T_WARNING
},
469 { 12, do_include_next
, "include_next", T_INCLUDE_NEXT
},
470 { 6, do_assert
, "assert", T_ASSERT
},
471 { 8, do_unassert
,"unassert",T_UNASSERT
},
472 { -1, 0, "", T_UNUSED
},
475 #define SKIP_WHITE_SPACE(p) do { while (is_nvspace(*p)) p++; } while (0)
476 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space(*p)) p++; } while (0)
478 int errors
= 0; /* Error counter for exit code */
480 static FILE_BUF expand_to_temp_buffer
PARAMS ((const U_CHAR
*, const U_CHAR
*, int));
481 static DEFINITION
*collect_expansion
PARAMS ((U_CHAR
*, U_CHAR
*, int,
484 /* Stack of conditionals currently in progress
485 (including both successful and failing conditionals). */
488 struct if_stack
*next
; /* for chaining to the next stack frame */
489 const char *fname
; /* copied from input when frame is made */
490 int lineno
; /* similarly */
491 int if_succeeded
; /* true if a leg of this if-group
492 has been passed through rescan */
493 enum node_type type
; /* type of last directive seen in this group */
495 typedef struct if_stack IF_STACK_FRAME
;
496 IF_STACK_FRAME
*if_stack
= NULL
;
498 /* Nonzero means -I- has been seen,
499 so don't look for #include "foo" the source-file directory. */
502 /* Pending directives. */
503 enum pending_dir_t
{PD_NONE
= 0, PD_DEFINE
, PD_UNDEF
, PD_ASSERTION
, PD_FILE
};
505 typedef struct pending_dir pending_dir
;
509 enum pending_dir_t type
;
519 const char *in_fname
, *out_fname
;
522 pending_dir
*pend
= (pending_dir
*) xcalloc (argc
, sizeof (pending_dir
));
523 int no_standard_includes
= 0;
528 /* Get rid of any avoidable limit on stack size. */
532 /* Set the stack limit huge so that alloca (particularly stringtab
533 * in dbxread.c) does not fail. */
534 getrlimit (RLIMIT_STACK
, &rlim
);
535 rlim
.rlim_cur
= rlim
.rlim_max
;
536 setrlimit (RLIMIT_STACK
, &rlim
);
538 #endif /* RLIMIT_STACK defined */
545 no_line_commands
= 0;
549 max_include_len
= cpp_GCC_INCLUDE_DIR_len
+ 7; /* ??? */
553 /* It's simplest to just create this struct whether or not it will
557 /* Process switches and find input file name. */
559 for (i
= 1; i
< argc
; i
++) {
560 if (argv
[i
][0] != '-') {
561 if (out_fname
!= NULL
)
562 fatal ("usage: %s [switches] input output", argv
[0]);
563 else if (in_fname
!= NULL
)
573 break; /* Ignore for compatibility with ISO/extended cpp. */
576 if (!strcmp (argv
[i
], "-lang-c++")
577 || !strcmp (argv
[i
], "-lang-objc++"))
578 fatal ("-traditional is not supported in C++");
579 else if (!strcmp (argv
[i
], "-lang-c89"))
580 fatal ("-traditional and -ansi are mutually exclusive");
581 else if (!strcmp (argv
[i
], "-lang-objc"))
582 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__OBJC__";
583 else if (!strcmp (argv
[i
], "-lang-asm"))
584 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__ASSEMBLER__";
585 else if (!strcmp (argv
[i
], "-lang-fortran"))
586 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "_LANGUAGE_FORTRAN";
587 /* All other possibilities ignored. */
591 if (!strcmp (argv
[i
], "-include"))
594 fatal ("filename missing after -i option");
596 pend
[i
].type
= PD_FILE
, pend
[i
].arg
= argv
[i
+ 1], i
++;
598 else if (!strcmp (argv
[i
], "-iprefix"))
599 i
++; /* Ignore for compatibility */
600 else if (!strcmp (argv
[i
], "-isystem")
601 || !strcmp (argv
[i
], "-iwithprefix")
602 || !strcmp (argv
[i
], "-iwithprefixbefore")
603 || !strcmp (argv
[i
], "-idirafter"))
604 goto add_include
; /* best we can do */
609 if (out_fname
!= NULL
)
610 fatal ("output filename specified twice");
612 fatal ("filename missing after -o option");
613 out_fname
= argv
[++i
];
614 if (!strcmp (out_fname
, "-"))
619 inhibit_warnings
= 1;
623 if (!strcmp (argv
[i
], "-Wcomments"))
625 else if (!strcmp (argv
[i
], "-Wcomment"))
627 else if (!strcmp (argv
[i
], "-Wall")) {
633 if (!strcmp (argv
[i
], "-fleading-underscore"))
634 user_label_prefix
= "_";
635 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
636 user_label_prefix
= "";
637 else if (!strcmp (argv
[i
], "-fsigned-char"))
638 flag_signed_char
= 1;
639 else if (!strcmp (argv
[i
], "-funsigned-char"))
640 flag_signed_char
= 0;
647 /* -MD and -MMD for tradcpp are deprecated and undocumented
648 (use -M or -MM with -MF instead), and probably should be
649 removed with the next major GCC version. For the moment
650 we allow these for the benefit of Automake 1.4, which
651 uses these when dependency tracking is enabled. Automake
652 1.5 will fix this. */
653 if (!strncmp (argv
[i
], "-MD", 3)) {
656 } else if (!strncmp (argv
[i
], "-MMD", 4)) {
659 } else if (!strcmp (argv
[i
], "-M")) {
661 } else if (!strcmp (argv
[i
], "-MM")) {
663 } else if (!strcmp (argv
[i
], "-MG")) {
664 deps_missing_files
= 1;
665 } else if (!strcmp (argv
[i
], "-MF")) {
667 } else if (!strcmp (argv
[i
], "-MP")) {
668 print_deps_phony_targets
= 1;
669 } else if (!strcmp (argv
[i
], "-MQ") || !strcmp (argv
[i
], "-MT")) {
670 /* Add a target. -MQ quotes for Make. */
671 const char *tgt
= argv
[i
] + 3;
672 int quoted
= argv
[i
][2] == 'Q';
674 if (*tgt
== '\0' && i
+ 1 == argc
)
675 fatal ("target missing after %s option", argv
[i
]);
681 deps_add_target (deps
, tgt
, quoted
);
688 else if (i
+ 1 == argc
)
689 fatal ("filename missing after %s option", argv
[i
]);
691 deps_file
= argv
[++i
];
702 fprintf (stderr
, "GNU traditional CPP version %s\n", version_string
);
713 else if (i
+ 1 == argc
)
714 fatal ("macro name missing after -%c option", c
);
719 pend
[i
].type
= PD_DEFINE
;
721 pend
[i
].type
= PD_UNDEF
;
723 pend
[i
].type
= PD_ASSERTION
;
729 put_out_comments
= 1;
733 if (!strcmp (argv
[i
], "-pedantic"))
734 fatal ("-pedantic and -traditional are mutually exclusive");
738 if (!strcmp (argv
[i
], "-trigraphs"))
739 fatal ("-trigraphs and -traditional are mutually exclusive");
743 no_line_commands
= 1;
746 case 'I': /* Add directory to path for includes. */
749 struct file_name_list
*dirtmp
;
751 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
754 dirtmp
= (struct file_name_list
*)
755 xmalloc (sizeof (struct file_name_list
));
756 dirtmp
->next
= 0; /* New one goes on the end */
760 last_include
->next
= dirtmp
;
761 last_include
= dirtmp
; /* Tail follows the last one */
762 if (argv
[i
][1] == 'I' && argv
[i
][2] != 0)
763 dirtmp
->fname
= argv
[i
] + 2;
764 else if (i
+ 1 == argc
)
765 fatal ("directory name missing after -I option");
767 dirtmp
->fname
= argv
[++i
];
768 if (strlen (dirtmp
->fname
) > max_include_len
)
769 max_include_len
= strlen (dirtmp
->fname
);
770 if (ignore_srcdir
&& first_bracket_include
== 0)
771 first_bracket_include
= dirtmp
;
777 /* -nostdinc causes no default include directories.
778 You must specify all include-file directories with -I. */
779 no_standard_includes
= 1;
782 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
783 if (in_fname
== NULL
) {
786 } else if (out_fname
== NULL
) {
789 } /* else fall through into error */
792 fatal ("invalid option `%s'", argv
[i
]);
797 init_dependency_output ();
799 /* After checking the environment variables, check if -M or -MM has
800 not been specified, but other -M options have. */
802 && (deps_missing_files
|| deps_file
|| print_deps_phony_targets
))
803 fatal ("you must additionally specify either -M or -MM");
805 if (user_label_prefix
== 0)
806 user_label_prefix
= USER_LABEL_PREFIX
;
810 /* Set the default target (if there is none already), and
811 the dependency on the main file. */
812 deps_add_default_target (deps
, in_fname
);
814 deps_add_dep (deps
, in_fname
);
817 /* Install __LINE__, etc. Must follow option processing. */
818 initialize_builtins ();
820 /* Do defines specified with -D and undefines specified with -U. */
821 for (i
= 1; i
< argc
; i
++)
822 if (pend
[i
].type
== PD_DEFINE
)
823 make_definition (pend
[i
].arg
);
824 else if (pend
[i
].type
== PD_UNDEF
)
825 make_undef (pend
[i
].arg
);
826 else if (pend
[i
].type
== PD_ASSERTION
)
827 make_assertion (pend
[i
].arg
);
829 /* Unless -fnostdinc,
830 tack on the standard include file dirs to the specified list */
831 if (!no_standard_includes
) {
832 const struct default_include
*di
;
833 struct file_name_list
*old_last_include
= last_include
;
834 struct file_name_list
*dirtmp
;
835 for (di
= cpp_include_defaults
; di
->fname
; di
++) {
838 dirtmp
= (struct file_name_list
*)
839 xmalloc (sizeof (struct file_name_list
));
840 dirtmp
->next
= 0; /* New one goes on the end */
844 last_include
->next
= dirtmp
;
845 last_include
= dirtmp
; /* Tail follows the last one */
846 dirtmp
->fname
= di
->fname
;
847 if (strlen (dirtmp
->fname
) > max_include_len
)
848 max_include_len
= strlen (dirtmp
->fname
);
851 if (ignore_srcdir
&& first_bracket_include
== 0)
852 first_bracket_include
= old_last_include
->next
;
855 /* Initialize output buffer */
857 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
858 outbuf
.bufp
= outbuf
.buf
;
859 outbuf
.length
= OUTBUF_SIZE
;
861 /* Scan the -i files before the main input.
862 Much like #including them, but with no_output set
863 so that only their macro definitions matter. */
867 for (i
= 1; i
< argc
; i
++)
868 if (pend
[i
].type
== PD_FILE
)
870 int fd
= open (pend
[i
].arg
, O_RDONLY
, 0666);
873 perror_with_name (pend
[i
].arg
);
874 return FATAL_EXIT_CODE
;
877 /* For -M, add this file to the dependencies. */
879 deps_add_dep (deps
, pend
[i
].arg
);
881 finclude (fd
, pend
[i
].arg
, 0, &outbuf
);
886 /* Pending directives no longer needed. */
889 /* Create an input stack level for the main input file
890 and copy the entire contents of the file into it. */
892 fp
= &instack
[++indepth
];
894 /* JF check for stdin */
895 if (in_fname
== NULL
|| *in_fname
== 0) {
898 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
901 if (file_size_and_mode (f
, &st_mode
, &st_size
))
903 fp
->fname
= in_fname
;
905 /* JF all this is mine about reading pipes and ttys */
906 if (!S_ISREG (st_mode
)) {
907 /* Read input from a file that is not a normal disk file.
908 We cannot preallocate a buffer with the correct size,
909 so we must read in the file a piece at the time and make it bigger. */
917 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
920 cnt
= read (f
, bufp
, bsize
- size
);
921 if (cnt
< 0) goto sys_error
; /* error! */
922 if (cnt
== 0) break; /* End of file */
925 if (bsize
== size
) { /* Buffer is full! */
927 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
928 bufp
= fp
->buf
+ size
; /* May have moved */
933 /* Read a file whose size we can determine in advance.
934 For the sake of VMS, st_size is just an upper bound. */
937 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
939 while (st_size
> 0) {
940 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
950 fp
->if_stack
= if_stack
;
952 /* Make sure data ends with a newline. And put a null after it. */
954 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
955 fp
->buf
[fp
->length
++] = '\n';
956 fp
->buf
[fp
->length
] = '\0';
958 /* Now that we know the input file is valid, open the output. */
960 if (!out_fname
|| !strcmp (out_fname
, ""))
961 out_fname
= "stdout";
962 else if (! freopen (out_fname
, "w", stdout
))
963 pfatal_with_name (out_fname
);
965 output_line_command (fp
, &outbuf
, 0, same_file
);
967 /* Scan the input, processing macros and directives. */
971 /* Now we have processed the entire input
972 Write whichever kind of output has been requested. */
977 else if (! inhibit_output
)
978 if (write (fileno (stdout
), outbuf
.buf
, outbuf
.bufp
- outbuf
.buf
) < 0)
979 fatal ("I/O error on output");
981 /* Don't write the deps file if preprocessing has failed. */
982 if (print_deps
&& errors
== 0)
985 /* Destruct the deps object. */
989 fatal ("I/O error on output");
992 exit (FATAL_EXIT_CODE
);
993 exit (SUCCESS_EXIT_CODE
);
996 pfatal_with_name (in_fname
);
999 /* Set up dependency-file output. */
1001 init_dependency_output ()
1003 char *spec
, *s
, *output_file
;
1005 /* Either of two environment variables can specify output of deps.
1006 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1007 where OUTPUT_FILE is the file to write deps info to
1008 and DEPS_TARGET is the target to mention in the deps. */
1010 if (print_deps
== 0)
1012 spec
= getenv ("DEPENDENCIES_OUTPUT");
1017 spec
= getenv ("SUNPRO_DEPENDENCIES");
1024 /* Find the space before the DEPS_TARGET, if there is one. */
1025 s
= strchr (spec
, ' ');
1028 /* Let the caller perform MAKE quoting. */
1029 deps_add_target (deps
, s
+ 1, 0);
1030 output_file
= (char *) xmalloc (s
- spec
+ 1);
1031 memcpy (output_file
, spec
, s
- spec
);
1032 output_file
[s
- spec
] = 0;
1037 /* Command line overrides environment variables. */
1039 deps_file
= output_file
;
1043 /* If dependencies go to standard output, or -MG is used, we should
1044 suppress output. The user may be requesting other stuff to
1045 stdout, with -dM, -v etc. We let them shoot themselves in the
1047 if (deps_file
== 0 || deps_missing_files
)
1051 /* Use mkdeps.c to output dependency information. */
1055 /* Stream on which to print the dependency information. */
1056 FILE *deps_stream
= 0;
1057 const char *const deps_mode
= deps_append
? "a" : "w";
1060 deps_stream
= stdout
;
1063 deps_stream
= fopen (deps_file
, deps_mode
);
1064 if (deps_stream
== 0)
1066 error_from_errno (deps_file
);
1071 deps_write (deps
, deps_stream
, 72);
1073 if (print_deps_phony_targets
)
1074 deps_phony_targets (deps
, deps_stream
);
1076 /* Don't close stdout. */
1079 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
1080 fatal ("I/O error on output");
1084 /* Move all backslash-newline pairs out of embarrassing places.
1085 Exchange all such pairs following BP
1086 with any potentially-embarrasing characters that follow them.
1087 Potentially-embarrassing characters are / and *
1088 (because a backslash-newline inside a comment delimiter
1089 would cause it not to be recognized). */
1097 /* First count the backslash-newline pairs here. */
1099 while (*p
++ == '\\' && *p
++ == '\n')
1104 /* Exit if what follows the backslash-newlines is not embarrassing. */
1106 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
1109 /* Copy all potentially embarrassing characters
1110 that follow the backslash-newline pairs
1111 down to where the pairs originally started. */
1113 while (*p
== '*' || *p
== '/')
1116 /* Now write the same number of pairs after the embarrassing chars. */
1117 while (count
-- > 0) {
1123 /* Like newline_fix but for use within a directive-name.
1124 Move any backslash-newlines up past any following symbol constituents. */
1126 name_newline_fix (bp
)
1132 /* First count the backslash-newline pairs here. */
1134 while (*p
++ == '\\' && *p
++ == '\n')
1139 /* What follows the backslash-newlines is not embarrassing. */
1141 if (count
== 0 || !is_idchar (*p
))
1144 /* Copy all potentially embarrassing characters
1145 that follow the backslash-newline pairs
1146 down to where the pairs originally started. */
1148 while (is_idchar (*p
))
1151 /* Now write the same number of pairs after the embarrassing chars. */
1152 while (count
-- > 0) {
1159 * The main loop of the program.
1161 * Read characters from the input stack, transferring them to the
1164 * Macros are expanded and push levels on the input stack.
1165 * At the end of such a level it is popped off and we keep reading.
1166 * At the end of any other kind of level, we return.
1167 * #-directives are handled, except within macros.
1169 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1170 * and insert them when appropriate. This is set while scanning macro
1171 * arguments before substitution. It is zero when scanning for final output.
1172 * There are three types of Newline markers:
1173 * * Newline - follows a macro name that was not expanded
1174 * because it appeared inside an expansion of the same macro.
1175 * This marker prevents future expansion of that identifier.
1176 * When the input is rescanned into the final output, these are deleted.
1177 * These are also deleted by ## concatenation.
1178 * * Newline Space (or Newline and any other whitespace character)
1179 * stands for a place that tokens must be separated or whitespace
1180 * is otherwise desirable, but where the ANSI standard specifies there
1181 * is no whitespace. This marker turns into a Space (or whichever other
1182 * whitespace char appears in the marker) in the final output,
1183 * but it turns into nothing in an argument that is stringified with #.
1184 * Such stringified arguments are the only place where the ANSI standard
1185 * specifies with precision that whitespace may not appear.
1187 * During this function, IP->bufp is kept cached in IBP for speed of access.
1188 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1189 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1190 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1191 * explicitly, and before RECACHE, since RECACHE uses OBP.
1195 rescan (op
, output_marks
)
1199 /* Character being scanned in main loop. */
1202 /* Length of pending accumulated identifier. */
1203 int ident_length
= 0;
1205 /* Hash code of pending accumulated identifier. */
1208 /* Current input level (&instack[indepth]). */
1211 /* Pointer for scanning input. */
1214 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1217 /* Pointer for storing output. */
1220 /* REDO_CHAR is nonzero if we are processing an identifier
1221 after backing up over the terminating character.
1222 Sometimes we process an identifier without backing up over
1223 the terminating character, if the terminating character
1224 is not special. Backing up is done so that the terminating character
1225 will be dispatched on again once the identifier is dealt with. */
1228 /* 1 if within an identifier inside of which a concatenation
1229 marker (Newline -) has been seen. */
1230 int concatenated
= 0;
1232 /* While scanning a comment or a string constant,
1233 this records the line it started on, for error messages. */
1236 /* Record position of last `real' newline. */
1237 U_CHAR
*beg_of_line
;
1239 /* This has to be a global bacause of RECACHE. */
1240 U_CHAR
*obufp_before_macroname
= NULL
;
1242 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1245 do { ip->macro->type = T_MACRO; \
1246 if (ip->free_ptr) free (ip->free_ptr); \
1247 --indepth; } while (0)
1249 /* Reload `rescan's local variables that describe the current
1250 level of the input stack. */
1253 do { ip = &instack[indepth]; \
1255 limit = ip->buf + ip->length; \
1257 check_expand (op, limit - ibp); \
1259 obufp_before_macroname += op->bufp - obp; \
1260 obp = op->bufp; } while (0)
1262 if (no_output
&& instack
[indepth
].fname
!= 0)
1263 skip_if_group (&instack
[indepth
], 1);
1269 /* Our caller must always put a null after the end of
1270 the input at each input stack level. */
1283 /* Always merge lines ending with backslash-newline,
1284 even in middle of identifier. */
1287 --obp
; /* remove backslash from obuf */
1290 /* Otherwise, backslash suppresses specialness of following char,
1291 so copy it here to prevent the switch from seeing it.
1292 But first get any pending identifier processed. */
1293 if (ident_length
> 0)
1299 /* If this is expanding a macro definition, don't recognize
1300 preprocessor directives. */
1306 /* # keyword: a # must be the first char on the line */
1307 if (beg_of_line
== 0)
1309 if (beg_of_line
+ 1 != ibp
)
1312 /* This # can start a directive. */
1314 --obp
; /* Don't copy the '#' */
1318 if (! handle_directive (ip
, op
)) {
1322 /* Not a known directive: treat it as ordinary text.
1323 IP, OP, IBP, etc. have not been changed. */
1324 if (no_output
&& instack
[indepth
].fname
) {
1325 /* If not generating expanded output,
1326 what we do with ordinary text is skip it.
1327 Discard everything until next # directive. */
1328 skip_if_group (&instack
[indepth
], 1);
1333 ++obp
; /* Copy the '#' after all */
1339 /* A # directive has been successfully processed. */
1340 /* If not generating expanded output, ignore everything until
1341 next # directive. */
1342 if (no_output
&& instack
[indepth
].fname
)
1343 skip_if_group (&instack
[indepth
], 1);
1349 case '\"': /* skip quoted string */
1351 /* A single quoted string is treated like a double -- some
1352 programs (e.g., troff) are perverse this way */
1357 start_line
= ip
->lineno
;
1359 /* Skip ahead to a matching quote. */
1363 if (ip
->macro
!= 0) {
1364 /* try harder: this string crosses a macro expansion boundary */
1376 /* Traditionally, end of line ends a string constant with no error.
1377 So exit the loop and record the new line. */
1385 /* Backslash newline is replaced by nothing at all,
1386 but keep the line counts correct. */
1391 /* ANSI stupidly requires that in \\ the second \
1392 is *not* prevented from combining with a newline. */
1393 while (*ibp
== '\\' && ibp
[1] == '\n') {
1412 if (*ibp
== '\\' && ibp
[1] == '\n')
1414 /* Don't look for comments inside a macro definition. */
1417 /* A comment constitutes white space, so it can terminate an identifier.
1418 Process the identifier, if any. */
1425 /* We have a comment. Skip it, optionally copying it to output. */
1427 start_line
= ip
->lineno
;
1429 ++ibp
; /* Skip the star. */
1431 /* In K+R C, a comment is equivalent to nothing. Note that we
1432 already output the slash; we might not want it. */
1433 if (! put_out_comments
)
1439 U_CHAR
*before_bp
= ibp
;
1441 while (ibp
< limit
) {
1444 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
1445 warning("`/*' within comment");
1448 if (*ibp
== '\\' && ibp
[1] == '\n')
1450 if (ibp
>= limit
|| *ibp
== '/')
1455 /* Copy the newline into the output buffer, in order to
1456 avoid the pain of a #line every time a multiline comment
1458 if (!put_out_comments
)
1466 error_with_line (line_for_error (start_line
),
1467 "unterminated comment");
1470 if (put_out_comments
) {
1471 memcpy (obp
, before_bp
, ibp
- before_bp
);
1472 obp
+= ibp
- before_bp
;
1478 case '0': case '1': case '2': case '3': case '4':
1479 case '5': case '6': case '7': case '8': case '9':
1480 /* If digit is not part of identifier, it starts a number,
1481 which means that following letters are not an identifier.
1482 "0x5" does not refer to an identifier "x5".
1483 So copy all alphanumerics that follow without accumulating
1484 as an identifier. Periods also, for sake of "3.e7". */
1486 if (ident_length
== 0) {
1487 while (ibp
< limit
) {
1488 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1493 if (! ISIDNUM (c
) && c
!= '.') {
1498 /* A sign can be part of a preprocessing number
1499 if it follows an e. */
1500 if (c
== 'e' || c
== 'E') {
1501 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1505 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
1507 /* Traditional C does not let the token go past the sign. */
1517 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1518 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1519 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1520 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1522 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1523 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1524 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1525 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1528 /* Compute step of hash function, to avoid a proc call on every token */
1529 hash
= HASHSTEP (hash
, c
);
1533 /* If reprocessing a macro expansion, newline is a special marker. */
1534 if (ip
->macro
!= 0) {
1535 /* Newline White is a "funny space" to separate tokens that are
1536 supposed to be separate but without space between.
1537 Here White means any horizontal whitespace character.
1538 Newline - marks a recursive macro use that is not
1539 supposed to be expandable. */
1542 /* Newline - inhibits expansion of preceding token.
1543 If expanding a macro arg, we keep the newline -.
1544 In final output, it is deleted. */
1545 if (! concatenated
) {
1550 if (!output_marks
) {
1553 /* If expanding a macro arg, keep the newline -. */
1556 } else if (is_space (*ibp
)) {
1557 /* Newline Space does not prevent expansion of preceding token
1558 so expand the preceding token and then come back. */
1559 if (ident_length
> 0)
1562 /* If generating final output, newline space makes a space. */
1563 if (!output_marks
) {
1565 /* And Newline Newline makes a newline, so count it. */
1566 if (obp
[-1] == '\n')
1569 /* If expanding a macro arg, keep the newline space.
1570 If the arg gets stringified, newline space makes nothing. */
1573 } else abort (); /* Newline followed by something random? */
1577 /* If there is a pending identifier, handle it and come back here. */
1578 if (ident_length
> 0)
1583 /* Update the line counts and output a #line if necessary. */
1586 if (ip
->lineno
!= op
->lineno
) {
1588 output_line_command (ip
, op
, 1, same_file
);
1589 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
1594 /* Come here either after (1) a null character that is part of the input
1595 or (2) at the end of the input, because there is a null there. */
1598 /* Our input really contains a null character. */
1601 /* At end of a macro-expansion level, pop it and read next level. */
1602 if (ip
->macro
!= 0) {
1605 /* If we have an identifier that ends here, process it now, so
1606 we get the right error for recursion. */
1607 if (ident_length
&& ! is_idchar (*instack
[indepth
- 1].bufp
)) {
1616 /* If we don't have a pending identifier,
1617 return at end of input. */
1618 if (ident_length
== 0) {
1626 /* If we do have a pending identifier, just consider this null
1627 a special character and arrange to dispatch on it again.
1628 The second time, IDENT_LENGTH will be zero so we will return. */
1634 /* Handle the case of a character such as /, ', " or null
1635 seen following an identifier. Back over it so that
1636 after the identifier is processed the special char
1637 will be dispatched on again. */
1647 if (ident_length
> 0) {
1650 /* We have just seen an identifier end. If it's a macro, expand it.
1652 IDENT_LENGTH is the length of the identifier
1653 and HASH is its hash code.
1655 The identifier has already been copied to the output,
1656 so if it is a macro we must remove it.
1658 If REDO_CHAR is 0, the char that terminated the identifier
1659 has been skipped in the output and the input.
1660 OBP-IDENT_LENGTH-1 points to the identifier.
1661 If the identifier is a macro, we must back over the terminator.
1663 If REDO_CHAR is 1, the terminating char has already been
1664 backed over. OBP-IDENT_LENGTH points to the identifier. */
1666 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
1669 if (hp
->length
== ident_length
) {
1670 /* obufp_before_macroname is used only in this block,
1671 but it has to be global because of RECACHE. */
1672 int op_lineno_before_macroname
;
1673 int i
= ident_length
;
1674 U_CHAR
*p
= hp
->name
;
1675 U_CHAR
*q
= obp
- i
;
1680 do { /* All this to avoid a strncmp () */
1685 /* We found a use of a macro name.
1686 see if the context shows it is a macro call. */
1688 /* Back up over terminating character if not already done. */
1694 obufp_before_macroname
= obp
- ident_length
;
1695 op_lineno_before_macroname
= op
->lineno
;
1697 /* If macro wants an arglist, verify that a '(' follows.
1698 first skip all whitespace, copying it to the output
1699 after the macro name. Then, if there is no '(',
1700 decide this is not a macro call and leave things that way. */
1701 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
1704 /* Scan forward over whitespace, copying it to the output. */
1705 if (ibp
== limit
&& ip
->macro
!= 0) {
1709 /* A comment: copy it unchanged or discard it. */
1710 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
1711 if (put_out_comments
) {
1716 while (ibp
+ 1 != limit
1717 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
1718 /* We need not worry about newline-marks,
1719 since they are never found in comments. */
1721 /* Newline in a file. Count it. */
1725 if (put_out_comments
)
1731 if (put_out_comments
) {
1736 else if (is_space (*ibp
)) {
1738 if (ibp
[-1] == '\n') {
1739 if (ip
->macro
== 0) {
1740 /* Newline in a file. Count it. */
1743 } else if (!output_marks
) {
1744 /* A newline mark, and we don't want marks
1745 in the output. If it is newline-hyphen,
1746 discard it entirely. Otherwise, it is
1747 newline-whitechar, so keep the whitechar. */
1757 /* A newline mark; copy both chars to the output. */
1768 /* This is now known to be a macro call.
1769 Discard the macro name from the output,
1770 along with any following whitespace just copied. */
1771 obp
= obufp_before_macroname
;
1772 op
->lineno
= op_lineno_before_macroname
;
1774 /* Expand the macro, reading arguments as needed,
1775 and push the expansion on the input stack. */
1778 macroexpand (hp
, op
);
1780 /* Reexamine input stack, since macroexpand has pushed
1781 a new level on it. */
1788 } /* End hash-table-search loop */
1789 ident_length
= hash
= 0; /* Stop collecting identifier */
1792 } /* End if (ident_length > 0) */
1794 } /* End per-char loop */
1796 /* Come here to return -- but first give an error message
1797 if there was an unterminated successful conditional. */
1799 if (if_stack
!= ip
->if_stack
) {
1801 switch (if_stack
->type
) {
1820 error_with_line (line_for_error (if_stack
->lineno
),
1821 "unterminated #%s conditional", str
);
1823 if_stack
= ip
->if_stack
;
1827 * Rescan a string into a temporary buffer and return the result
1828 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1830 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1831 * and insert such markers when appropriate. See `rescan' for details.
1832 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1833 * before substitution; it is 0 for other uses.
1836 expand_to_temp_buffer (buf
, limit
, output_marks
)
1837 const U_CHAR
*buf
, *limit
;
1842 int length
= limit
- buf
;
1844 int odepth
= indepth
;
1849 /* Set up the input on the input stack. */
1851 buf1
= (U_CHAR
*) alloca (length
+ 1);
1853 const U_CHAR
*p1
= buf
;
1861 /* Set up to receive the output. */
1863 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
1864 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
1869 CHECK_DEPTH ({return obuf
;});
1873 ip
= &instack
[indepth
];
1877 ip
->length
= length
;
1878 ip
->buf
= ip
->bufp
= buf1
;
1879 ip
->if_stack
= if_stack
;
1881 ip
->lineno
= obuf
.lineno
= 1;
1883 /* Scan the input, create the output. */
1885 rescan (&obuf
, output_marks
);
1887 /* Pop input stack to original state. */
1890 if (indepth
!= odepth
)
1893 /* Record the output. */
1894 obuf
.length
= obuf
.bufp
- obuf
.buf
;
1900 * Process a # directive. Expects IP->bufp to point to the '#', as in
1901 * `#define foo bar'. Passes to the command handler
1902 * (do_define, do_include, etc.): the addresses of the 1st and
1903 * last chars of the command (starting immediately after the #
1904 * keyword), plus op and the keyword table pointer. If the command
1905 * contains comments it is copied into a temporary buffer sans comments
1906 * and the temporary buffer is passed to the command handler instead.
1907 * Likewise for backslash-newlines.
1909 * Returns nonzero if this was a known # directive.
1910 * Otherwise, returns zero, without advancing the input pointer.
1914 handle_directive (ip
, op
)
1918 const struct directive
*kt
;
1922 /* Nonzero means we must copy the entire command
1923 to get rid of comments or backslash-newlines. */
1924 int copy_command
= 0;
1926 U_CHAR
*ident
, *after_ident
;
1929 /* Skip whitespace and \-newline. */
1931 if (is_nvspace (*bp
))
1933 else if (*bp
== '/' && (newline_fix (bp
+ 1), bp
[1]) == '*') {
1935 skip_to_end_of_comment (ip
, &ip
->lineno
);
1937 } else if (*bp
== '\\' && bp
[1] == '\n') {
1938 bp
+= 2; ip
->lineno
++;
1942 /* Now find end of directive name.
1943 If we encounter a backslash-newline, exchange it with any following
1944 symbol-constituents so that we end up with a contiguous name. */
1948 if (is_idchar (*cp
))
1951 if (*cp
== '\\' && cp
[1] == '\n')
1952 name_newline_fix (cp
);
1953 if (is_idchar (*cp
))
1958 ident_length
= cp
- bp
;
1962 /* A line of just `#' becomes blank. */
1964 if (ident_length
== 0 && *after_ident
== '\n') {
1965 ip
->bufp
= after_ident
;
1970 * Decode the keyword and call the appropriate expansion
1971 * routine, after moving the input pointer up to the next line.
1973 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
1974 if (kt
->length
== ident_length
1975 && !strncmp (kt
->name
, (const char *)ident
, ident_length
)) {
1977 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
1978 int unterminated
= 0;
1980 /* Nonzero means do not delete comments within the directive.
1981 #define needs this to detect traditional token paste. */
1982 int keep_comments
= kt
->type
== T_DEFINE
;
1984 /* Find the end of this command (first newline not backslashed
1985 and not in a string or comment).
1986 Set COPY_COMMAND if the command must be copied
1987 (it contains a backslash-newline or a comment). */
1989 buf
= bp
= after_ident
;
1990 while (bp
< limit
) {
2005 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
2007 /* Traditional preprocessing permits unterminated strings. */
2013 /* <...> is special for #include. */
2015 if (kt
->type
!= T_INCLUDE
)
2017 while (*bp
&& *bp
!= '>') bp
++;
2021 if (*bp
== '\\' && bp
[1] == '\n')
2024 U_CHAR
*obp
= bp
- 1;
2026 skip_to_end_of_comment (ip
, &ip
->lineno
);
2028 /* No need to copy the command because of a comment at the end;
2029 just don't include the comment in the directive. */
2030 if (bp
== limit
|| *bp
== '\n') {
2034 /* Don't remove the comments if this is #define. */
2035 if (! keep_comments
)
2041 --bp
; /* Point to the newline */
2049 resume_p
= ip
->bufp
;
2050 /* BP is the end of the directive.
2051 RESUME_P is the next interesting data after the directive.
2052 A comment may come between. */
2056 /* Need to copy entire command into temp buffer before dispatching */
2058 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
2062 /* Copy to the new buffer, deleting comments
2063 and backslash-newlines (and whitespace surrounding the latter). */
2073 /* <...> is special for #include. */
2075 if (kt
->type
!= T_INCLUDE
)
2077 while (xp
< bp
&& c
!= '>') {
2079 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
2090 if (cp
!= buf
&& is_space (cp
[-1])) {
2091 while (cp
!= buf
&& is_space(cp
[-1])) cp
--;
2093 SKIP_WHITE_SPACE (xp
);
2094 } else if (is_nvspace (*xp
)) {
2096 SKIP_WHITE_SPACE (xp
);
2107 = skip_quoted_string (xp
- 1, limit
, ip
->lineno
, 0, 0, 0);
2116 skip_to_end_of_comment (ip
, 0);
2118 while (xp
!= ip
->bufp
)
2120 /* Delete the slash. */
2128 /* Null-terminate the copy. */
2135 ip
->bufp
= resume_p
;
2137 /* Call the appropriate command handler. buf now points to
2138 either the appropriate place in the input buffer, or to
2139 the temp buffer if it was necessary to make one. cp
2140 points to the first char after the contents of the (possibly
2141 copied) command, in either case. */
2142 (*kt
->func
) (buf
, cp
, op
);
2143 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2152 static const char *const
2153 monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2154 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2157 * expand things like __FILE__. Place the expansion into the output
2158 * buffer *without* rescanning.
2161 special_symbol (hp
, op
)
2169 FILE_BUF
*ip
= NULL
;
2170 static struct tm
*timebuf
= NULL
;
2172 int paren
= 0; /* For special `defined' keyword */
2174 for (i
= indepth
; i
>= 0; i
--)
2175 if (instack
[i
].fname
!= NULL
) {
2180 fatal ("not in any file?!");
2187 if (hp
->type
== T_FILE
)
2190 string
= instack
[0].fname
;
2194 char *tmp
= (char *) alloca (3 + strlen (string
));
2195 sprintf (tmp
, "\"%s\"", string
);
2204 case T_INCLUDE_LEVEL
:
2206 char *tmp
= (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2208 for (i
= indepth
; i
>= 0; i
--)
2209 if (instack
[i
].fname
!= NULL
)
2212 sprintf (tmp
, "%d", true_indepth
- 1);
2219 char *tmp
= (char *) alloca (3 + strlen (version_string
));
2220 sprintf (tmp
, "\"%s\"", version_string
);
2226 buf
= hp
->value
.cpval
;
2231 char *tmp
= (char *) alloca (10);
2232 sprintf (tmp
, "%d", ip
->lineno
);
2240 char *tmp
= (char *) alloca (20);
2242 if (timebuf
== NULL
) {
2244 timebuf
= localtime (&t
);
2246 if (hp
->type
== T_DATE
)
2247 sprintf (tmp
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2248 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2250 sprintf (tmp
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2256 case T_SPEC_DEFINED
:
2257 buf
= " 0 "; /* Assume symbol is not defined */
2258 ip
= &instack
[indepth
];
2259 SKIP_WHITE_SPACE (ip
->bufp
);
2260 if (*ip
->bufp
== '(') {
2262 ip
->bufp
++; /* Skip over the paren */
2263 SKIP_WHITE_SPACE (ip
->bufp
);
2266 if (!is_idstart (*ip
->bufp
))
2269 HASHNODE
*hp
= lookup (ip
->bufp
, -1, -1);
2271 if (hp
&& hp
->type
!= T_UNUSED
&& hp
->type
!= T_SPEC_DEFINED
)
2274 while (is_idchar (*ip
->bufp
))
2276 SKIP_WHITE_SPACE (ip
->bufp
);
2278 if (*ip
->bufp
!= ')')
2286 error ("`defined' must be followed by ident or (ident)");
2290 error ("cccp error: invalid special hash type"); /* time for gdb */
2294 check_expand (op
, len
);
2295 memcpy (op
->bufp
, buf
, len
);
2300 /* Routines to handle #directives */
2303 * Process include file by reading it in and calling rescan.
2304 * Expects to see "fname" or <fname> on the input.
2307 do_include (buf
, limit
, op
)
2308 U_CHAR
*buf
, *limit
;
2311 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2313 struct file_name_list
*stackp
= include
; /* Chain of dirs to search */
2314 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
2317 int retried
= 0; /* Have already tried macro
2318 expanding the include line*/
2319 FILE_BUF trybuf
; /* It got expanded into here */
2320 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2325 SKIP_WHITE_SPACE (fbeg
);
2326 /* Discard trailing whitespace so we can easily see
2327 if we have parsed all the significant chars we were given. */
2328 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2333 while (fend
!= limit
&& *fend
!= '\"')
2335 if (*fend
== '\"' && fend
+ 1 == limit
) {
2338 /* We have "filename". Figure out directory this source
2339 file is coming from and put it on the front of the list. */
2341 /* If -I- was specified, don't search current dir, only spec'd ones. */
2342 if (ignore_srcdir
) break;
2344 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
2347 const char *ep
, *nam
;
2349 if ((nam
= fp
->fname
) != NULL
) {
2350 /* Found a named file. Figure out dir of the file,
2351 and put it in front of the search list. */
2352 dsp
[0].next
= stackp
;
2354 ep
= strrchr (nam
, '/');
2358 f
= (char *) alloca (n
+ 1);
2359 strncpy (f
, nam
, n
);
2362 if (n
> max_include_len
) max_include_len
= n
;
2364 dsp
[0].fname
= 0; /* Current directory */
2375 while (fend
!= limit
&& *fend
!= '>') fend
++;
2376 if (*fend
== '>' && fend
+ 1 == limit
) {
2377 system_header_p
= 1;
2378 /* If -I-, start with the first -I dir after the -I-. */
2379 if (first_bracket_include
)
2380 stackp
= first_bracket_include
;
2388 error ("#include expects \"fname\" or <fname>");
2391 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2392 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2393 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2394 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2402 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2406 do_include_next (buf
, limit
, op
)
2407 U_CHAR
*buf
, *limit
;
2410 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2412 struct file_name_list
*stackp
; /* Chain of dirs to search */
2415 int retried
= 0; /* Have already tried macro
2416 expanding the include line*/
2417 FILE_BUF trybuf
; /* It got expanded into here */
2418 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2420 /* Treat as plain #include if we don't know where to start
2422 stackp
= instack
[indepth
].next_header_dir
;
2425 do_include (buf
, limit
, op
);
2432 SKIP_WHITE_SPACE (fbeg
);
2433 /* Discard trailing whitespace so we can easily see
2434 if we have parsed all the significant chars we were given. */
2435 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2440 while (fend
!= limit
&& *fend
!= '\"')
2442 if (*fend
== '\"' && fend
+ 1 == limit
)
2448 while (fend
!= limit
&& *fend
!= '>') fend
++;
2449 if (*fend
== '>' && fend
+ 1 == limit
) {
2450 system_header_p
= 1;
2458 error ("#include expects \"fname\" or <fname>");
2461 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2462 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2463 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2464 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2472 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2476 process_include (stackp
, fbeg
, flen
, system_header_p
, op
)
2477 struct file_name_list
*stackp
;
2480 int system_header_p
;
2484 int f
= -1; /* file number */
2486 fname
= (char *) alloca (max_include_len
+ flen
+ 2);
2487 /* + 2 above for slash and terminating null. */
2489 /* If specified file name is absolute, just open it. */
2491 if (IS_ABSOLUTE_PATHNAME (fbeg
)) {
2492 strncpy (fname
, (const char *)fbeg
, flen
);
2494 f
= open (fname
, O_RDONLY
, 0666);
2496 /* Search directory path, trying to open the file.
2497 Copy each filename tried into FNAME. */
2499 for (; stackp
; stackp
= stackp
->next
) {
2500 if (stackp
->fname
) {
2501 strcpy (fname
, stackp
->fname
);
2502 strcat (fname
, "/");
2503 fname
[strlen (fname
) + flen
] = 0;
2507 strncat (fname
, (const char *)fbeg
, flen
);
2508 if ((f
= open (fname
, O_RDONLY
, 0666)) >= 0)
2514 strncpy (fname
, (const char *)fbeg
, flen
);
2516 if (deps_missing_files
2517 && print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2519 /* If requested as a system header, assume it belongs in
2520 the first system header directory. */
2521 if (first_bracket_include
)
2522 stackp
= first_bracket_include
;
2526 if (!system_header_p
|| IS_ABSOLUTE_PATHNAME (fbeg
) || !stackp
->fname
)
2527 deps_add_dep (deps
, fname
);
2530 int len
= strlen(stackp
->fname
);
2532 p
= (char *) alloca (len
+ flen
+ 2);
2533 memcpy (p
, stackp
->fname
, len
);
2535 memcpy (p
+ len
, fbeg
, flen
);
2538 deps_add_dep (deps
, p
);
2540 } else if (print_deps
2541 && print_deps
<= (system_header_p
2542 || (system_include_depth
> 0)))
2543 warning ("no include path in which to find %.*s", flen
, fbeg
);
2545 error_from_errno (fname
);
2549 /* Check to see if this include file is a once-only include file.
2552 struct file_name_list
* ptr
;
2554 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
2555 if (!strcmp (ptr
->fname
, fname
)) {
2557 return; /* This file was once'd. */
2561 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
2562 if (!strcmp (ptr
->fname
, fname
))
2563 break; /* This file was included before. */
2567 /* This is the first time for this file. */
2568 /* Add it to list of files included. */
2570 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
2571 ptr
->next
= all_include_files
;
2572 all_include_files
= ptr
;
2573 ptr
->fname
= xstrdup (fname
);
2575 /* For -M, add this file to the dependencies. */
2576 if (print_deps
> (system_header_p
|| (system_include_depth
> 0)))
2577 deps_add_dep (deps
, fname
);
2580 if (system_header_p
)
2581 system_include_depth
++;
2583 /* Actually process the file. */
2584 finclude (f
, fname
, stackp
->next
, op
);
2586 if (system_header_p
)
2587 system_include_depth
--;
2593 /* Process the contents of include file FNAME, already open on descriptor F,
2594 with output to OP. */
2597 finclude (f
, fname
, nhd
, op
)
2600 struct file_name_list
*nhd
;
2606 FILE_BUF
*fp
; /* For input stack frame */
2608 CHECK_DEPTH (return;);
2610 if (file_size_and_mode (f
, &st_mode
, &st_size
))
2613 fp
= &instack
[indepth
+ 1];
2614 memset (fp
, 0, sizeof (FILE_BUF
));
2618 fp
->if_stack
= if_stack
;
2619 fp
->next_header_dir
= nhd
;
2621 if (S_ISREG (st_mode
)) {
2622 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
2625 /* Read the file contents, knowing that st_size is an upper bound
2626 on the number of bytes we can read. */
2627 while (st_size
> 0) {
2628 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
2638 /* Cannot count its file size before reading. */
2645 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
2649 i
= read (f
, bufp
, bsize
- st_size
);
2651 goto nope
; /* error! */
2653 break; /* End of file */
2656 if (bsize
== st_size
) { /* Buffer is full! */
2658 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
2659 bufp
= basep
+ st_size
; /* May have moved */
2664 fp
->length
= st_size
;
2668 /* Make sure data ends with a newline. And put a null after it. */
2670 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
2671 fp
->buf
[fp
->length
++] = '\n';
2672 fp
->buf
[fp
->length
] = '\0';
2675 output_line_command (fp
, op
, 0, enter_file
);
2678 instack
[indepth
].lineno
++;
2679 instack
[indepth
].bufp
++; /* Skip the new line. */
2680 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
2685 perror_with_name (fname
);
2690 /* Process a #define command.
2691 BUF points to the contents of the #define command, as a continguous string.
2692 LIMIT points to the first character past the end of the definition.
2693 KEYWORD is the keyword-table entry for #define. */
2696 do_define (buf
, limit
, op
)
2697 U_CHAR
*buf
, *limit
;
2698 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2700 U_CHAR
*bp
; /* temp ptr into input buffer */
2701 U_CHAR
*symname
; /* remember where symbol name starts */
2702 int sym_length
; /* and how long it is */
2705 int arglengths
= 0; /* Accumulate lengths of arg names
2706 plus number of args. */
2711 while (is_nvspace (*bp
))
2714 symname
= bp
; /* remember where it starts */
2715 while (is_idchar (*bp
) && bp
< limit
) {
2718 sym_length
= bp
- symname
;
2719 if (sym_length
== 0)
2721 error ("invalid macro name");
2724 else if (!is_idstart (*symname
)) {
2725 U_CHAR
*msg
; /* what pain... */
2726 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
2727 memcpy (msg
, symname
, sym_length
);
2728 msg
[sym_length
] = 0;
2729 error ("invalid macro name `%s'", msg
);
2732 if (! strncmp ((const char *)symname
, "defined", 7) && sym_length
== 7)
2734 error ("\"defined\" cannot be used as a macro name");
2739 /* lossage will occur if identifiers or control keywords are broken
2740 across lines using backslash. This is not the right place to take
2744 struct arglist
*arg_ptrs
= NULL
;
2747 bp
++; /* skip '(' */
2748 SKIP_WHITE_SPACE (bp
);
2750 /* Loop over macro argument names. */
2751 while (*bp
!= ')') {
2752 struct arglist
*temp
;
2754 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
2756 temp
->next
= arg_ptrs
;
2757 temp
->argno
= argno
++;
2760 if (!is_idstart (*bp
))
2761 warning ("parameter name starts with a digit in #define");
2763 /* Find the end of the arg name. */
2764 while (is_idchar (*bp
)) {
2767 temp
->length
= bp
- temp
->name
;
2768 arglengths
+= temp
->length
+ 2;
2769 SKIP_WHITE_SPACE (bp
);
2770 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
2771 error ("badly punctuated parameter list in #define");
2776 SKIP_WHITE_SPACE (bp
);
2779 error ("unterminated parameter list in #define");
2784 ++bp
; /* skip paren */
2785 while (is_nvspace (*bp
) && bp
< limit
) /* and leading whitespace */
2787 /* now everything from bp before limit is the definition. */
2788 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
2790 /* Now set defn->argnames to the result of concatenating
2791 the argument names in reverse order
2792 with comma-space between them. */
2794 struct arglist
*temp
;
2796 U_CHAR
*tmp
= (U_CHAR
*) xmalloc (arglengths
+ 1);
2798 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
2799 memcpy (&tmp
[i
], temp
->name
, temp
->length
);
2801 if (temp
->next
!= 0) {
2807 defn
->argnames
= tmp
;
2811 /* simple expansion or empty definition; skip leading whitespace */
2812 while (is_nvspace (*bp
) && bp
< limit
)
2814 /* now everything from bp before limit is the definition. */
2815 defn
= collect_expansion (bp
, limit
, -1, 0);
2816 defn
->argnames
= (const U_CHAR
*) "";
2819 hashcode
= hashf (symname
, sym_length
, HASHSIZE
);
2823 if ((hp
= lookup (symname
, sym_length
, hashcode
)) == NULL
)
2824 hp
= install (symname
, sym_length
, T_MACRO
, hashcode
);
2826 if (hp
->type
!= T_MACRO
|| compare_defs (defn
, hp
->value
.defn
))
2827 warning ("\"%.*s\" redefined", sym_length
, symname
);
2829 /* Replace the old definition. */
2833 hp
->value
.defn
= defn
;
2838 * return zero if two DEFINITIONs are isomorphic
2841 compare_defs (d1
, d2
)
2842 DEFINITION
*d1
, *d2
;
2844 struct reflist
*a1
, *a2
;
2845 U_CHAR
*p1
= d1
->expansion
;
2846 U_CHAR
*p2
= d2
->expansion
;
2849 if (d1
->nargs
!= d2
->nargs
)
2851 if (strcmp ((const char *)d1
->argnames
, (const char *)d2
->argnames
))
2853 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
2854 a1
= a1
->next
, a2
= a2
->next
) {
2855 if (!((a1
->nchars
== a2
->nchars
2856 && ! strncmp ((const char *)p1
, (const char *)p2
, a1
->nchars
))
2857 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
2858 || a1
->argno
!= a2
->argno
2859 || a1
->stringify
!= a2
->stringify
2860 || a1
->raw_before
!= a2
->raw_before
2861 || a1
->raw_after
!= a2
->raw_after
)
2869 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
2870 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
2875 /* Return 1 if two parts of two macro definitions are effectively different.
2876 One of the parts starts at BEG1 and has LEN1 chars;
2877 the other has LEN2 chars at BEG2.
2878 Any sequence of whitespace matches any other sequence of whitespace.
2879 FIRST means these parts are the first of a macro definition;
2880 so ignore leading whitespace entirely.
2881 LAST means these parts are the last of a macro definition;
2882 so ignore trailing whitespace entirely. */
2884 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
2886 const U_CHAR
*beg1
, *beg2
;
2890 const U_CHAR
*end1
= beg1
+ len1
;
2891 const U_CHAR
*end2
= beg2
+ len2
;
2893 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2894 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2897 while (beg1
!= end1
&& is_space (end1
[-1])) end1
--;
2898 while (beg2
!= end2
&& is_space (end2
[-1])) end2
--;
2900 while (beg1
!= end1
&& beg2
!= end2
) {
2901 if (is_space (*beg1
) && is_space (*beg2
)) {
2902 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2903 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2904 } else if (*beg1
== *beg2
) {
2908 return (beg1
!= end1
) || (beg2
!= end2
);
2911 /* Read a replacement list for a macro with parameters.
2912 Build the DEFINITION structure.
2913 Reads characters of text starting at BUF until LIMIT.
2914 ARGLIST specifies the formal parameters to look for
2915 in the text of the definition; NARGS is the number of args
2916 in that list, or -1 for a macro name that wants no argument list.
2917 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2918 and NAMELEN is its length in characters.
2920 Note that comments and backslash-newlines have already been deleted
2921 from the argument. */
2923 /* Leading and trailing Space, Tab, etc. are converted to markers
2924 Newline Space, Newline Tab, etc.
2925 Newline Space makes a space in the final output
2926 but is discarded if stringified. (Newline Tab is similar but
2927 makes a Tab instead.)
2929 If there is no trailing whitespace, a Newline Space is added at the end
2930 to prevent concatenation that would be contrary to the standard. */
2933 collect_expansion (buf
, end
, nargs
, arglist
)
2936 struct arglist
*arglist
;
2939 U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
2940 struct reflist
*endpat
= NULL
;
2941 /* Pointer to first nonspace after last ## seen. */
2943 /* Pointer to first nonspace after last single-# seen. */
2944 U_CHAR
*stringify
= 0;
2946 int expected_delimiter
= '\0';
2948 /* Scan thru the replacement list, ignoring comments and quoted
2949 strings, picking up on the macro calls. It does a linear search
2950 thru the arg list on every potential symbol. Profiling might say
2951 that something smarter should happen. */
2956 /* Find the beginning of the trailing whitespace. */
2957 /* Find end of leading whitespace. */
2960 while (p
< limit
&& is_space (limit
[-1])) limit
--;
2961 while (p
< limit
&& is_space (*p
)) p
++;
2963 /* Allocate space for the text in the macro definition.
2964 Leading and trailing whitespace chars need 2 bytes each.
2965 Each other input char may or may not need 1 byte,
2966 so this is an upper bound.
2967 The extra 2 are for invented trailing newline-marker and final null. */
2968 maxsize
= (sizeof (DEFINITION
)
2969 + 2 * (end
- limit
) + 2 * (p
- buf
)
2971 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
2973 defn
->nargs
= nargs
;
2974 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
2979 /* Convert leading whitespace to Newline-markers. */
2980 while (p
< limit
&& is_space (*p
)) {
2985 /* Process the main body of the definition. */
2987 int skipped_arg
= 0;
2992 /* In -traditional mode, recognize arguments inside strings and
2993 and character constants, and ignore special properties of #.
2994 Arguments inside strings are considered "stringified", but no
2995 extra quote marks are supplied. */
2999 if (expected_delimiter
!= '\0') {
3000 if (c
== expected_delimiter
)
3001 expected_delimiter
= '\0';
3003 expected_delimiter
= c
;
3007 /* Backslash quotes delimiters and itself, but not macro args. */
3008 if (expected_delimiter
!= 0 && p
< limit
3009 && (*p
== expected_delimiter
|| *p
== '\\')) {
3016 if (expected_delimiter
!= '\0') /* No comments inside strings. */
3019 /* If we find a comment that wasn't removed by handle_directive,
3020 this must be -traditional. So replace the comment with
3024 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
3030 if (is_idchar (c
) && nargs
> 0) {
3031 U_CHAR
*id_beg
= p
- 1;
3035 while (p
!= limit
&& is_idchar (*p
)) p
++;
3036 id_len
= p
- id_beg
;
3038 if (is_idstart (c
)) {
3039 struct arglist
*arg
;
3041 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
3042 struct reflist
*tpat
;
3044 if (arg
->name
[0] == c
3045 && arg
->length
== id_len
3046 && strncmp ((const char *)arg
->name
,
3047 (const char *)id_beg
, id_len
) == 0) {
3048 /* make a pat node for this arg and append it to the end of
3050 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
3052 tpat
->raw_before
= concat
== id_beg
;
3053 tpat
->raw_after
= 0;
3054 tpat
->stringify
= expected_delimiter
!= '\0';
3057 defn
->pattern
= tpat
;
3059 endpat
->next
= tpat
;
3062 tpat
->argno
= arg
->argno
;
3063 tpat
->nchars
= exp_p
- lastp
;
3066 SKIP_WHITE_SPACE (p1
);
3067 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
3068 tpat
->raw_after
= 1;
3070 lastp
= exp_p
; /* place to start copying from next time */
3077 /* If this was not a macro arg, copy it into the expansion. */
3078 if (! skipped_arg
) {
3083 if (stringify
== id_beg
)
3084 error ("# operator should be followed by a macro argument name");
3090 /* Convert trailing whitespace to Newline-markers. */
3091 while (limit
< end
&& is_space (*limit
)) {
3093 *exp_p
++ = *limit
++;
3098 defn
->length
= exp_p
- defn
->expansion
;
3100 /* Crash now if we overrun the allocated size. */
3101 if (defn
->length
+ 1 > maxsize
)
3108 * interpret #line command. Remembers previously seen fnames
3109 * in its very own hash table.
3111 #define FNAME_HASHSIZE 37
3113 do_line (buf
, limit
, op
)
3114 U_CHAR
*buf
, *limit
;
3118 FILE_BUF
*ip
= &instack
[indepth
];
3121 enum file_change_code file_change
= same_file
;
3123 /* Expand any macros. */
3124 tem
= expand_to_temp_buffer (buf
, limit
, 0);
3126 /* Point to macroexpanded line, which is null-terminated now. */
3128 SKIP_WHITE_SPACE (bp
);
3130 if (!ISDIGIT (*bp
)) {
3131 error ("invalid format #line command");
3135 /* The Newline at the end of this line remains to be processed.
3136 To put the next line at the specified line number,
3137 we must store a line number now that is one less. */
3138 new_lineno
= atoi ((const char *)bp
);
3140 /* skip over the line number. */
3141 while (ISDIGIT (*bp
))
3144 SKIP_WHITE_SPACE (bp
);
3147 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3148 HASHNODE
*hp
, **hash_bucket
;
3154 while (*bp
&& *bp
!= '\"')
3157 error ("invalid format #line command");
3161 fname_length
= bp
- fname
;
3164 SKIP_WHITE_SPACE (bp
);
3167 file_change
= enter_file
;
3168 else if (*bp
== '2')
3169 file_change
= leave_file
;
3171 error ("invalid format #line command");
3176 SKIP_WHITE_SPACE (bp
);
3178 error ("invalid format #line command");
3184 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3185 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3186 if (hp
->length
== fname_length
&&
3187 strncmp (hp
->value
.cpval
, (const char *)fname
, fname_length
) == 0) {
3188 ip
->fname
= hp
->value
.cpval
;
3193 /* Didn't find it; cons up a new one. */
3194 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3195 hp
->next
= *hash_bucket
;
3198 hp
->length
= fname_length
;
3199 ip
->fname
= hp
->value
.cpval
= q
= ((char *) hp
) + sizeof (HASHNODE
);
3200 memcpy (q
, fname
, fname_length
);
3203 error ("invalid format #line command");
3207 ip
->lineno
= new_lineno
;
3208 output_line_command (ip
, op
, 0, file_change
);
3209 ip
->bufp
++; /* Skip the new line. */
3210 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3214 * remove all definitions of symbol from symbol table.
3215 * according to un*x /lib/cpp, it is not an error to undef
3216 * something that has no definitions, so it isn't one here either.
3219 do_undef (buf
, limit
, op
)
3221 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3222 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3226 SKIP_WHITE_SPACE (buf
);
3228 if (! strncmp ((const char *)buf
, "defined", 7) && ! is_idchar (buf
[7]))
3229 warning ("undefining `defined'");
3231 while ((hp
= lookup (buf
, -1, -1)) != NULL
) {
3232 if (hp
->type
!= T_MACRO
)
3233 warning ("undefining `%s'", hp
->name
);
3238 /* Read the tokens of the answer into the macro pool. Only commit the
3239 memory if we intend it as permanent storage, i.e. the #assert case.
3240 Returns 0 on success. */
3243 parse_answer (buf
, limit
, answerp
, type
)
3244 const unsigned char *buf
, *limit
;
3245 struct answer
**answerp
;
3248 const unsigned char *start
;
3250 /* Skip leading whitespace. */
3251 if (buf
< limit
&& *buf
== ' ')
3254 /* Parentheses are optional here. */
3255 if (buf
== limit
&& type
== T_UNASSERT
)
3258 if (buf
== limit
|| *buf
++ != '(')
3263 error ("missing '(' after predicate");
3267 /* Drop whitespace at start. */
3268 while (buf
< limit
&& *buf
== ' ')
3272 while (buf
< limit
&& *buf
!= ')')
3277 error ("missing ')' to complete answer");
3283 error ("predicate's answer is empty");
3287 if ((type
== T_ASSERT
|| type
== T_UNASSERT
) && buf
+ 1 != limit
)
3289 error ("extra text at end of directive");
3293 /* Lose trailing whitespace. */
3297 *answerp
= (struct answer
*) xmalloc (sizeof (struct answer
));
3298 (*answerp
)->answer
= start
;
3299 (*answerp
)->len
= buf
- start
;
3304 /* Parses an assertion, returning a pointer to the hash node of the
3305 predicate, or 0 on error. If an answer was supplied, it is placed
3306 in ANSWERP, otherwise it is set to 0. */
3308 parse_assertion (buf
, limit
, answerp
, type
)
3309 const unsigned char *buf
, *limit
;
3310 struct answer
**answerp
;
3313 HASHNODE
*result
= 0;
3314 const unsigned char *climit
;
3315 unsigned char *bp
, *symname
= canonicalize_text (buf
, limit
, &climit
);
3319 if (bp
< climit
&& is_idstart (*bp
))
3323 while (bp
< climit
&& is_idchar (*bp
));
3330 if (symname
== climit
)
3331 error ("assertion without predicate");
3333 error ("predicate must be an identifier");
3335 /* Unfortunately, because of the way we handle #if, we don't avoid
3336 macro expansion in answers. This is not easy to fix. */
3337 else if (parse_answer (bp
, climit
, answerp
, type
) == 0)
3339 unsigned char *sym
= alloca (len
+ 1);
3342 /* Prefix '#' to get it out of macro namespace. */
3344 memcpy (sym
+ 1, symname
, len
);
3346 hashcode
= hashf (sym
, len
+ 1, HASHSIZE
);
3347 result
= lookup (sym
, len
+ 1, hashcode
);
3349 result
= install (sym
, len
+ 1, T_UNUSED
, hashcode
);
3355 /* Test an assertion within a preprocessor conditional. Returns zero
3356 on error or failure, one on success. */
3358 test_assertion (pbuf
)
3359 unsigned char **pbuf
; /* NUL-terminated. */
3361 unsigned char *buf
= *pbuf
;
3362 unsigned char *limit
= buf
+ strlen ((char *) buf
);
3363 struct answer
*answer
;
3367 node
= parse_assertion (buf
, limit
, &answer
, T_IF
);
3370 result
= (node
->type
== T_ASSERT
&&
3371 (answer
== 0 || *find_answer (node
, answer
) != 0));
3373 /* Yuk. We update pbuf to point after the assertion test.
3374 First, move past the identifier. */
3375 if (is_space (*buf
))
3377 while (is_idchar (*buf
))
3379 /* If we have an answer, we need to move past the parentheses. */
3381 while (*buf
++ != ')')
3389 /* Handle a #error directive. */
3391 do_error (buf
, limit
, op
)
3394 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3396 error ("#error%.*s", (int) (limit
- buf
), buf
);
3399 /* Handle a #warning directive. */
3401 do_warning (buf
, limit
, op
)
3404 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3406 warning ("#warning%.*s", (int) (limit
- buf
), buf
);
3409 /* Handle a #assert directive. */
3411 do_assert (buf
, limit
, op
)
3414 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3416 struct answer
*new_answer
;
3419 node
= parse_assertion (buf
, limit
, &new_answer
, T_ASSERT
);
3422 /* Place the new answer in the answer list. First check there
3423 is not a duplicate. */
3424 new_answer
->next
= 0;
3425 if (node
->type
== T_ASSERT
)
3427 if (*find_answer (node
, new_answer
))
3430 warning ("\"%s\" re-asserted", node
->name
+ 1);
3433 new_answer
->next
= node
->value
.answers
;
3435 node
->type
= T_ASSERT
;
3436 node
->value
.answers
= new_answer
;
3440 /* Function body to be provided later. */
3442 do_unassert (buf
, limit
, op
)
3445 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3448 struct answer
*answer
;
3450 node
= parse_assertion (buf
, limit
, &answer
, T_UNASSERT
);
3451 /* It isn't an error to #unassert something that isn't asserted. */
3454 if (node
->type
== T_ASSERT
)
3458 struct answer
**p
= find_answer (node
, answer
), *temp
;
3460 /* Remove the answer from the list. */
3465 /* Did we free the last answer? */
3466 if (node
->value
.answers
== 0)
3467 delete_macro (node
);
3470 delete_macro (node
);
3477 /* Returns a pointer to the pointer to the answer in the answer chain,
3478 or a pointer to NULL if the answer is not in the chain. */
3479 static struct answer
**
3480 find_answer (node
, candidate
)
3482 const struct answer
*candidate
;
3484 struct answer
**result
;
3486 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
3488 struct answer
*answer
= *result
;
3490 if (answer
->len
== candidate
->len
3491 && !memcmp (answer
->answer
, candidate
->answer
, answer
->len
))
3498 /* Return a malloced buffer with leading and trailing whitespace
3499 removed, and all instances of internal whitespace reduced to a
3501 static unsigned char *
3502 canonicalize_text (buf
, limit
, climit
)
3503 const unsigned char *buf
, *limit
, **climit
;
3505 unsigned int len
= limit
- buf
;
3506 unsigned char *result
= (unsigned char *) xmalloc (len
), *dest
;
3508 for (dest
= result
; buf
< limit
;)
3510 if (! is_space (*buf
))
3514 while (++buf
< limit
&& is_space (*buf
))
3516 if (dest
!= result
&& buf
!= limit
)
3526 * handle #if command by
3527 * 1) inserting special `defined' keyword into the hash table
3528 * that gets turned into 0 or 1 by special_symbol (thus,
3529 * if the luser has a symbol called `defined' already, it won't
3530 * work inside the #if command)
3531 * 2) rescan the input into a temporary output buffer
3532 * 3) pass the output buffer to the yacc parser and collect a value
3533 * 4) clean up the mess left from steps 1 and 2.
3534 * 5) call conditional_skip to skip til the next #endif (etc.),
3535 * or not, depending on the value from step 3.
3538 do_if (buf
, limit
, op
)
3539 U_CHAR
*buf
, *limit
;
3540 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3543 FILE_BUF
*ip
= &instack
[indepth
];
3545 value
= eval_if_expression (buf
, limit
- buf
);
3546 conditional_skip (ip
, value
== 0, T_IF
);
3550 * handle a #elif directive by not changing if_stack either.
3551 * see the comment above do_else.
3554 do_elif (buf
, limit
, op
)
3555 U_CHAR
*buf
, *limit
;
3559 FILE_BUF
*ip
= &instack
[indepth
];
3561 if (if_stack
== instack
[indepth
].if_stack
) {
3562 error ("#elif not within a conditional");
3565 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3566 error ("#elif after #else");
3567 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3568 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
3569 strcmp (if_stack
->fname
, ip
->fname
) != 0)
3570 fprintf (stderr
, ", file %s", if_stack
->fname
);
3571 fprintf (stderr
, ")\n");
3573 if_stack
->type
= T_ELIF
;
3576 if (if_stack
->if_succeeded
)
3577 skip_if_group (ip
, 0);
3579 value
= eval_if_expression (buf
, limit
- buf
);
3581 skip_if_group (ip
, 0);
3583 ++if_stack
->if_succeeded
; /* continue processing input */
3584 output_line_command (ip
, op
, 1, same_file
);
3590 * evaluate a #if expression in BUF, of length LENGTH,
3591 * then parse the result as a C expression and return the value as an int.
3594 eval_if_expression (buf
, length
)
3599 HASHNODE
*save_defined
;
3602 save_defined
= install (U
"defined", -1, T_SPEC_DEFINED
, -1);
3603 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0);
3604 delete_macro (save_defined
); /* clean up special symbol */
3606 value
= parse_c_expression ((const char *)temp_obuf
.buf
);
3608 free (temp_obuf
.buf
);
3614 * routine to handle ifdef/ifndef. Try to look up the symbol,
3615 * then do or don't skip to the #endif/#else/#elif depending
3616 * on what directive is actually being processed.
3619 do_xifdef (buf
, limit
, type
)
3620 U_CHAR
*buf
, *limit
;
3621 enum node_type type
;
3624 FILE_BUF
*ip
= &instack
[indepth
];
3627 /* Discard leading and trailing whitespace. */
3628 SKIP_WHITE_SPACE (buf
);
3629 while (limit
!= buf
&& is_nvspace (limit
[-1])) limit
--;
3631 /* Find the end of the identifier at the beginning. */
3632 for (end
= buf
; is_idchar (*end
); end
++);
3635 skip
= (type
== T_IFDEF
);
3637 skip
= (lookup (buf
, end
-buf
, -1) == NULL
) ^ (type
== T_IFNDEF
);
3639 conditional_skip (ip
, skip
, T_IF
);
3643 do_ifdef (buf
, limit
, op
)
3644 U_CHAR
*buf
, *limit
;
3645 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3647 do_xifdef (buf
, limit
, T_IFDEF
);
3651 do_ifndef (buf
, limit
, op
)
3652 U_CHAR
*buf
, *limit
;
3653 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3655 do_xifdef (buf
, limit
, T_IFNDEF
);
3659 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3662 conditional_skip (ip
, skip
, type
)
3665 enum node_type type
;
3667 IF_STACK_FRAME
*temp
;
3669 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3670 temp
->fname
= ip
->fname
;
3671 temp
->lineno
= ip
->lineno
;
3672 temp
->next
= if_stack
;
3675 if_stack
->type
= type
;
3678 skip_if_group (ip
, 0);
3681 ++if_stack
->if_succeeded
;
3682 output_line_command (ip
, &outbuf
, 1, same_file
);
3687 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3688 * leaves input ptr at the sharp sign found.
3689 * If ANY is nonzero, return at next directive of any sort.
3692 skip_if_group (ip
, any
)
3696 U_CHAR
*bp
= ip
->bufp
, *cp
;
3697 U_CHAR
*endb
= ip
->buf
+ ip
->length
;
3698 const struct directive
*kt
;
3699 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
3700 U_CHAR
*beg_of_line
= bp
;
3704 case '/': /* possible comment */
3705 if (*bp
== '\\' && bp
[1] == '\n')
3709 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
);
3714 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
3717 /* Char after backslash loses its special meaning. */
3720 ++ip
->lineno
; /* But do update the line-count. */
3731 /* # keyword: a # must be first nonblank char on the line */
3732 if (beg_of_line
== 0)
3734 /* Scan from start of line, skipping whitespace, comments
3735 and backslash-newlines, and see if we reach this #.
3736 If not, this # is not special. */
3739 if (is_nvspace (*bp
))
3741 else if (*bp
== '\\' && bp
[1] == '\n')
3743 else if (*bp
== '/' && bp
[1] == '*') {
3745 while (!(*bp
== '*' && bp
[1] == '/')) {
3754 if (bp
!= ip
->bufp
) {
3755 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
3759 bp
= ip
->bufp
+ 1; /* Point after '#'. */
3761 /* Skip whitespace and \-newline. */
3763 if (is_nvspace (*bp
))
3765 else if (*bp
== '\\' && bp
[1] == '\n')
3767 else if (*bp
== '/' && bp
[1] == '*') {
3769 while (!(*bp
== '*' && bp
[1] == '/'))
3778 /* Now find end of directive name.
3779 If we encounter a backslash-newline, exchange it with any following
3780 symbol-constituents so that we end up with a contiguous name. */
3783 if (is_idchar (*bp
))
3786 if (*bp
== '\\' && bp
[1] == '\n')
3787 name_newline_fix (bp
);
3788 if (is_idchar (*bp
))
3794 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
3795 IF_STACK_FRAME
*temp
;
3796 if (strncmp ((const char *)cp
, kt
->name
, kt
->length
) == 0
3797 && !is_idchar (cp
[kt
->length
])) {
3799 /* If we are asked to return on next directive,
3808 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3809 temp
->next
= if_stack
;
3811 temp
->lineno
= ip
->lineno
;
3812 temp
->fname
= ip
->fname
;
3813 temp
->type
= kt
->type
;
3818 if (if_stack
== instack
[indepth
].if_stack
) {
3819 error ("#%s not within a conditional", kt
->name
);
3822 else if (if_stack
== save_if_stack
)
3823 return; /* found what we came for */
3825 if (kt
->type
!= T_ENDIF
) {
3826 if (if_stack
->type
== T_ELSE
)
3827 error ("#else or #elif after #else");
3828 if_stack
->type
= kt
->type
;
3833 if_stack
= if_stack
->next
;
3838 /* Anything else is ignored. */
3847 /* after this returns, rescan will exit because ip->bufp
3848 now points to the end of the buffer.
3849 rescan is responsible for the error message also. */
3853 * handle a #else directive. Do this by just continuing processing
3854 * without changing if_stack ; this is so that the error message
3855 * for missing #endif's etc. will point to the original #if. It
3856 * is possible that something different would be better.
3859 do_else (buf
, limit
, op
)
3860 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3861 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3864 FILE_BUF
*ip
= &instack
[indepth
];
3866 if (if_stack
== instack
[indepth
].if_stack
) {
3867 error ("#else not within a conditional");
3870 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3871 error ("#else after #else");
3872 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3873 if (strcmp (if_stack
->fname
, ip
->fname
) != 0)
3874 fprintf (stderr
, ", file %s", if_stack
->fname
);
3875 fprintf (stderr
, ")\n");
3877 if_stack
->type
= T_ELSE
;
3880 if (if_stack
->if_succeeded
)
3881 skip_if_group (ip
, 0);
3883 ++if_stack
->if_succeeded
; /* continue processing input */
3884 output_line_command (ip
, op
, 1, same_file
);
3889 * unstack after #endif command
3892 do_endif (buf
, limit
, op
)
3893 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3894 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3897 if (if_stack
== instack
[indepth
].if_stack
)
3898 error ("unbalanced #endif");
3900 IF_STACK_FRAME
*temp
= if_stack
;
3901 if_stack
= if_stack
->next
;
3903 output_line_command (&instack
[indepth
], op
, 1, same_file
);
3908 * Skip a comment, assuming the input ptr immediately follows the
3909 * initial slash-star. Bump line counter as necessary.
3910 * (The canonical line counter is &ip->lineno).
3911 * Don't use this routine (or the next one) if bumping the line
3912 * counter is not sufficient to deal with newlines in the string.
3915 skip_to_end_of_comment (ip
, line_counter
)
3917 int *line_counter
; /* place to remember newlines, or NULL */
3919 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
3920 U_CHAR
*bp
= ip
->bufp
;
3921 FILE_BUF
*op
= &outbuf
; /* JF */
3922 int output
= put_out_comments
&& !line_counter
;
3924 /* JF this line_counter stuff is a crock to make sure the
3925 comment is only put out once, no matter how many times
3926 the comment is skipped. It almost works */
3931 while (bp
< limit
) {
3936 if (warn_comments
&& bp
< limit
&& *bp
== '*')
3937 warning("`/*' within comment");
3940 if (line_counter
!= NULL
)
3946 if (*bp
== '\\' && bp
[1] == '\n')
3962 * Skip over a quoted string. BP points to the opening quote.
3963 * Returns a pointer after the closing quote. Don't go past LIMIT.
3964 * START_LINE is the line number of the starting point (but it need
3965 * not be valid if the starting point is inside a macro expansion).
3967 * The input stack state is not changed.
3969 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3970 * for each newline passed.
3972 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3973 * if we pass a backslash-newline.
3975 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3978 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
3980 const U_CHAR
*limit
;
3982 int *count_newlines
;
3983 int *backslash_newlines_p
;
3991 error_with_line (line_for_error (start_line
),
3992 "unterminated string or character constant");
3999 while (*bp
== '\\' && bp
[1] == '\n') {
4000 if (backslash_newlines_p
)
4001 *backslash_newlines_p
= 1;
4006 if (*bp
== '\n' && count_newlines
) {
4007 if (backslash_newlines_p
)
4008 *backslash_newlines_p
= 1;
4012 } else if (c
== '\n') {
4013 /* Unterminated strings and character constants are 'legal'. */
4014 bp
--; /* Don't consume the newline. */
4018 } else if (c
== match
)
4021 return (U_CHAR
*) bp
;
4025 * write out a #line command, for instance, after an #include file.
4026 * If CONDITIONAL is nonzero, we can omit the #line if it would
4027 * appear to be a no-op, and we can output a few newlines instead
4028 * if we want to increase the line number by a small amount.
4029 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
4033 output_line_command (ip
, op
, conditional
, file_change
)
4036 enum file_change_code file_change
;
4039 char line_cmd_buf
[500];
4041 if (no_line_commands
4042 || ip
->fname
== NULL
4044 op
->lineno
= ip
->lineno
;
4049 if (ip
->lineno
== op
->lineno
)
4052 /* If the inherited line number is a little too small,
4053 output some newlines instead of a #line command. */
4054 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
4055 check_expand (op
, 10);
4056 while (ip
->lineno
> op
->lineno
) {
4064 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->fname
);
4065 if (file_change
!= same_file
)
4066 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
4067 if (system_include_depth
> 0)
4068 strcat (line_cmd_buf
, " 3");
4069 len
= strlen (line_cmd_buf
);
4070 line_cmd_buf
[len
++] = '\n';
4071 check_expand (op
, len
+ 1);
4072 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
4074 memcpy (op
->bufp
, line_cmd_buf
, len
);
4076 op
->lineno
= ip
->lineno
;
4080 /* Expand a macro call.
4081 HP points to the symbol that is the macro being called.
4082 Put the result of expansion onto the input stack
4083 so that subsequent input by our caller will use it.
4085 If macro wants arguments, caller has already verified that
4086 an argument list follows; arguments come from the input stack. */
4089 macroexpand (hp
, op
)
4094 DEFINITION
*defn
= hp
->value
.defn
;
4097 int start_line
= instack
[indepth
].lineno
;
4099 CHECK_DEPTH (return;);
4101 /* it might not actually be a macro. */
4102 if (hp
->type
!= T_MACRO
) {
4103 special_symbol (hp
, op
);
4107 nargs
= defn
->nargs
;
4111 struct argdata
*args
;
4112 const char *parse_error
= 0;
4114 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
4116 for (i
= 0; i
< nargs
; i
++) {
4117 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
4118 args
[i
].raw_length
= args
[i
].expand_length
4119 = args
[i
].stringified_length
= 0;
4120 args
[i
].free1
= args
[i
].free2
= 0;
4123 /* Parse all the macro args that are supplied. I counts them.
4124 The first NARGS args are stored in ARGS.
4125 The rest are discarded. */
4128 /* Discard the open-parenthesis or comma before the next arg. */
4129 ++instack
[indepth
].bufp
;
4131 = macarg ((i
< nargs
|| (nargs
== 0 && i
== 0)) ? &args
[i
] : 0);
4134 error_with_line (line_for_error (start_line
), "%s", parse_error
);
4138 } while (*instack
[indepth
].bufp
!= ')');
4140 /* If we got one arg but it was just whitespace, call that 0 args. */
4142 const U_CHAR
*bp
= args
[0].raw
;
4143 const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
4144 while (bp
!= lim
&& is_space (*bp
)) bp
++;
4149 if (nargs
== 0 && i
> 0)
4150 error ("arguments given to macro `%s'", hp
->name
);
4151 else if (i
< nargs
) {
4152 /* traditional C allows foo() if foo wants one argument. */
4153 if (nargs
== 1 && i
== 0)
4156 error ("no args to macro `%s'", hp
->name
);
4158 error ("only 1 arg to macro `%s'", hp
->name
);
4160 error ("only %d args to macro `%s'", i
, hp
->name
);
4161 } else if (i
> nargs
)
4162 error ("too many (%d) args to macro `%s'", i
, hp
->name
);
4164 /* Swallow the closeparen. */
4165 ++instack
[indepth
].bufp
;
4167 /* If macro wants zero args, we parsed the arglist for checking only.
4168 Read directly from the macro definition. */
4170 xbuf
= defn
->expansion
;
4171 xbuf_len
= defn
->length
;
4173 U_CHAR
*exp
= defn
->expansion
;
4174 int offset
; /* offset in expansion,
4175 copied a piece at a time */
4176 int totlen
; /* total amount of exp buffer filled so far */
4180 /* Macro really takes args. Compute the expansion of this call. */
4182 /* Compute length in characters of the macro's expansion. */
4183 xbuf_len
= defn
->length
;
4184 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4186 xbuf_len
+= args
[ap
->argno
].stringified_length
;
4188 xbuf_len
+= args
[ap
->argno
].raw_length
;
4191 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
4193 /* Generate in XBUF the complete expansion
4194 with arguments substituted in.
4195 TOTLEN is the total size generated so far.
4196 OFFSET is the index in the definition
4197 of where we are copying from. */
4198 offset
= totlen
= 0;
4199 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4200 struct argdata
*arg
= &args
[ap
->argno
];
4202 for (i
= 0; i
< ap
->nchars
; i
++)
4203 xbuf
[totlen
++] = exp
[offset
++];
4205 if (ap
->stringify
!= 0) {
4206 int arglen
= arg
->raw_length
;
4212 && (c
= arg
->raw
[i
], is_space (c
)))
4215 && (c
= arg
->raw
[arglen
- 1], is_space (c
)))
4217 for (; i
< arglen
; i
++) {
4220 /* Special markers Newline Space
4221 generate nothing for a stringified argument. */
4222 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
4227 /* Internal sequences of whitespace are replaced by one space
4228 except within an string or char token. */
4230 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space (c
))) {
4232 /* Note that Newline Space does occur within whitespace
4233 sequences; consider it part of the sequence. */
4234 if (c
== '\n' && is_space (arg
->raw
[i
+1]))
4236 else if (c
!= '\n' && is_space (c
))
4253 } else if (c
== '\"' || c
== '\'')
4257 /* Escape these chars */
4258 if (c
== '\"' || (in_string
&& c
== '\\'))
4259 xbuf
[totlen
++] = '\\';
4263 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
4268 const U_CHAR
*p1
= arg
->raw
;
4269 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
4271 if (ap
->raw_before
) {
4272 while (p1
!= l1
&& is_space (*p1
)) p1
++;
4273 while (p1
!= l1
&& is_idchar (*p1
))
4274 xbuf
[totlen
++] = *p1
++;
4275 /* Delete any no-reexpansion marker that follows
4276 an identifier at the beginning of the argument
4277 if the argument is concatenated with what precedes it. */
4278 if (p1
[0] == '\n' && p1
[1] == '-')
4281 if (ap
->raw_after
) {
4282 /* Arg is concatenated after: delete trailing whitespace,
4283 whitespace markers, and no-reexpansion markers. */
4285 if (is_space (l1
[-1])) l1
--;
4286 else if (l1
[-1] == '-') {
4287 const U_CHAR
*p2
= l1
- 1;
4288 /* If a `-' is preceded by an odd number of newlines then it
4289 and the last newline are a no-reexpansion marker. */
4290 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
4291 if ((l1
- 1 - p2
) & 1) {
4299 memmove (xbuf
+ totlen
, p1
, l1
- p1
);
4303 if (totlen
> xbuf_len
)
4307 /* if there is anything left of the definition
4308 after handling the arg list, copy that in too. */
4310 for (i
= offset
; i
< defn
->length
; i
++)
4311 xbuf
[totlen
++] = exp
[i
];
4316 for (i
= 0; i
< nargs
; i
++) {
4317 if (args
[i
].free1
!= 0)
4318 free (args
[i
].free1
);
4319 if (args
[i
].free2
!= 0)
4320 free (args
[i
].free2
);
4324 xbuf
= defn
->expansion
;
4325 xbuf_len
= defn
->length
;
4328 /* Now put the expansion on the input stack
4329 so our caller will commence reading from it. */
4333 ip2
= &instack
[++indepth
];
4338 ip2
->length
= xbuf_len
;
4340 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
4342 ip2
->if_stack
= if_stack
;
4347 * Parse a macro argument and store the info on it into *ARGPTR.
4348 * Return nonzero to indicate a syntax error.
4353 struct argdata
*argptr
;
4355 FILE_BUF
*ip
= &instack
[indepth
];
4360 /* Try to parse as much of the argument as exists at this
4361 input stack level. */
4362 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
4363 &paren
, &newlines
, &comments
);
4365 /* If we find the end of the argument at this level,
4366 set up *ARGPTR to point at it in the input stack. */
4367 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
4368 && bp
!= ip
->buf
+ ip
->length
) {
4370 argptr
->raw
= ip
->bufp
;
4371 argptr
->raw_length
= bp
- ip
->bufp
;
4375 /* This input stack level ends before the macro argument does.
4376 We must pop levels and keep parsing.
4377 Therefore, we must allocate a temporary buffer and copy
4378 the macro argument into it. */
4379 int bufsize
= bp
- ip
->bufp
;
4380 int extra
= newlines
;
4381 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
4382 int final_start
= 0;
4384 memcpy (buffer
, ip
->bufp
, bufsize
);
4386 ip
->lineno
+= newlines
;
4388 while (bp
== ip
->buf
+ ip
->length
) {
4389 if (instack
[indepth
].macro
== 0) {
4391 return "unterminated macro call";
4393 ip
->macro
->type
= T_MACRO
;
4395 free (ip
->free_ptr
);
4396 ip
= &instack
[--indepth
];
4399 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
4400 &newlines
, &comments
);
4401 final_start
= bufsize
;
4402 bufsize
+= bp
- ip
->bufp
;
4404 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
4405 memcpy (buffer
+ bufsize
- (bp
- ip
->bufp
), ip
->bufp
, bp
- ip
->bufp
);
4407 ip
->lineno
+= newlines
;
4410 /* Now, if arg is actually wanted, record its raw form,
4411 discarding comments and duplicating newlines in whatever
4412 part of it did not come from a macro expansion.
4413 EXTRA space has been preallocated for duplicating the newlines.
4414 FINAL_START is the index of the start of that part. */
4416 argptr
->raw
= buffer
;
4417 argptr
->raw_length
= bufsize
;
4418 argptr
->free1
= buffer
;
4419 argptr
->newlines
= newlines
;
4420 argptr
->comments
= comments
;
4421 if ((newlines
|| comments
) && ip
->fname
!= 0)
4424 discard_comments (argptr
->raw
+ final_start
,
4425 argptr
->raw_length
- final_start
,
4427 argptr
->raw
[argptr
->raw_length
] = 0;
4428 if (argptr
->raw_length
> bufsize
+ extra
)
4433 /* If we are not discarding this argument,
4434 macroexpand it and compute its length as stringified.
4435 All this info goes into *ARGPTR. */
4439 const U_CHAR
*buf
, *lim
;
4442 obuf
= expand_to_temp_buffer (argptr
->raw
,
4443 argptr
->raw
+ argptr
->raw_length
,
4446 argptr
->expanded
= obuf
.buf
;
4447 argptr
->expand_length
= obuf
.length
;
4448 argptr
->free2
= obuf
.buf
;
4451 lim
= buf
+ argptr
->raw_length
;
4454 while (buf
!= lim
) {
4457 /* Internal sequences of whitespace are replaced by one space
4458 in most cases, but not always. So count all the whitespace
4459 in case we need to keep it all. */
4460 if (c
== '\"' || c
== '\\') /* escape these chars */
4462 else if (!ISPRINT (c
))
4465 argptr
->stringified_length
= totlen
;
4470 /* Scan text from START (inclusive) up to LIMIT (exclusive),
4471 counting parens in *DEPTHPTR,
4472 and return if reach LIMIT
4473 or before a `)' that would make *DEPTHPTR negative
4474 or before a comma when *DEPTHPTR is zero.
4475 Single and double quotes are matched and termination
4476 is inhibited within them. Comments also inhibit it.
4477 Value returned is pointer to stopping place.
4479 Increment *NEWLINES each time a newline is passed.
4480 Set *COMMENTS to 1 if a comment is seen. */
4483 macarg1 (start
, limit
, depthptr
, newlines
, comments
)
4485 const U_CHAR
*limit
;
4486 int *depthptr
, *newlines
, *comments
;
4490 while (bp
< limit
) {
4496 if (--(*depthptr
) < 0)
4500 /* Traditionally, backslash makes following char not special. */
4504 /* But count source lines anyway. */
4513 if (bp
[1] == '\\' && bp
[2] == '\n')
4514 newline_fix (bp
+ 1);
4515 if (bp
[1] != '*' || bp
+ 1 >= limit
)
4519 while (bp
+ 1 < limit
) {
4521 && bp
[1] == '\\' && bp
[2] == '\n')
4522 newline_fix (bp
+ 1);
4523 if (bp
[0] == '*' && bp
[1] == '/')
4525 if (*bp
== '\n') ++*newlines
;
4534 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
4539 while (*bp
== '\\' && bp
[1] == '\n') {
4542 } else if (*bp
== '\n') {
4551 if ((*depthptr
) == 0)
4561 /* Discard comments and duplicate newlines
4562 in the string of length LENGTH at START,
4563 except inside of string constants.
4564 The string is copied into itself with its beginning staying fixed.
4566 NEWLINES is the number of newlines that must be duplicated.
4567 We assume that that much extra space is available past the end
4571 discard_comments (start
, length
, newlines
)
4578 const U_CHAR
*limit
;
4581 /* If we have newlines to duplicate, copy everything
4582 that many characters up. Then, in the second part,
4583 we will have room to insert the newlines
4585 NEWLINES may actually be too large, because it counts
4586 newlines in string constants, and we don't duplicate those.
4587 But that does no harm. */
4589 ibp
= start
+ length
;
4590 obp
= ibp
+ newlines
;
4592 while (limit
!= ibp
)
4596 ibp
= start
+ newlines
;
4597 limit
= start
+ length
+ newlines
;
4600 while (ibp
< limit
) {
4601 *obp
++ = c
= *ibp
++;
4604 /* Duplicate the newline. */
4616 if (*ibp
== '\\' && ibp
[1] == '\n')
4618 /* Delete any comment. */
4619 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
4623 while (ibp
+ 1 < limit
) {
4625 && ibp
[1] == '\\' && ibp
[2] == '\n')
4626 newline_fix (ibp
+ 1);
4627 if (ibp
[0] == '*' && ibp
[1] == '/')
4636 /* Notice and skip strings, so that we don't
4637 think that comments start inside them,
4638 and so we don't duplicate newlines in them. */
4641 while (ibp
< limit
) {
4642 *obp
++ = c
= *ibp
++;
4645 if (c
== '\n' && quotec
== '\'')
4647 if (c
== '\\' && ibp
< limit
) {
4648 while (*ibp
== '\\' && ibp
[1] == '\n')
4662 /* Core error handling routine. */
4664 v_message (mtype
, line
, msgid
, ap
)
4670 const char *fname
= 0;
4673 if (mtype
== MT_WARNING
&& inhibit_warnings
)
4676 for (i
= indepth
; i
>= 0; i
--)
4677 if (instack
[i
].fname
!= NULL
) {
4679 line
= instack
[i
].lineno
;
4680 fname
= instack
[i
].fname
;
4685 fprintf (stderr
, "%s:%d: ", fname
, line
);
4687 fprintf (stderr
, "%s: ", progname
);
4689 if (mtype
== MT_WARNING
)
4690 fputs (_("warning: "), stderr
);
4692 vfprintf (stderr
, _(msgid
), ap
);
4693 putc ('\n', stderr
);
4695 if (mtype
== MT_ERROR
)
4700 * error - print error message and increment count of errors.
4703 error
VPARAMS ((const char *msgid
, ...))
4706 VA_FIXEDARG (ap
, const char *, msgid
);
4708 v_message (MT_ERROR
, 0, msgid
, ap
);
4713 error_with_line
VPARAMS ((int line
, const char *msgid
, ...))
4716 VA_FIXEDARG (ap
, int, line
);
4717 VA_FIXEDARG (ap
, const char *, msgid
);
4719 v_message (MT_ERROR
, line
, msgid
, ap
);
4723 /* Error including a message from `errno'. */
4725 error_from_errno (name
)
4728 error ("%s: %s", name
, strerror (errno
));
4731 /* Print error message but don't count it. */
4733 warning
VPARAMS ((const char *msgid
, ...))
4736 VA_FIXEDARG (ap
, const char *, msgid
);
4738 v_message (MT_WARNING
, 0, msgid
, ap
);
4743 fatal
VPARAMS ((const char *msgid
, ...))
4746 VA_FIXEDARG (ap
, const char *, msgid
);
4748 v_message (MT_FATAL
, 0, msgid
, ap
);
4750 exit (FATAL_EXIT_CODE
);
4753 /* More 'friendly' abort that prints the location at which we died. */
4755 fancy_abort (line
, func
)
4759 fatal ("internal error in %s, at tradcpp.c:%d\n\
4760 Please submit a full bug report.\n\
4761 See %s for instructions.", func
, line
, GCCBUGURL
);
4765 perror_with_name (name
)
4768 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, strerror (errno
));
4773 pfatal_with_name (name
)
4776 perror_with_name (name
);
4777 exit (FATAL_EXIT_CODE
);
4780 /* Return the line at which an error occurred.
4781 The error is not necessarily associated with the current spot
4782 in the input stack, so LINE says where. LINE will have been
4783 copied from ip->lineno for the current input level.
4784 If the current level is for a file, we return LINE.
4785 But if the current level is not for a file, LINE is meaningless.
4786 In that case, we return the lineno of the innermost file. */
4788 line_for_error (line
)
4794 for (i
= indepth
; i
>= 0; ) {
4795 if (instack
[i
].fname
!= 0)
4800 line1
= instack
[i
].lineno
;
4806 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4808 * As things stand, nothing is ever placed in the output buffer to be
4809 * removed again except when it's KNOWN to be part of an identifier,
4810 * so flushing and moving down everything left, instead of expanding,
4815 grow_outbuf (obuf
, needed
)
4822 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
4825 /* Make it at least twice as big as it is now. */
4827 /* Make it have at least 150% of the free space we will need. */
4828 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
4829 if (minsize
> obuf
->length
)
4830 obuf
->length
= minsize
;
4832 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
4833 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
4837 /* Symbol table for macro names and special symbols */
4840 * install a name in the main hash table, even if it is already there.
4841 * name stops with first non alphanumeric, except leading '#'.
4842 * caller must check against redefinition if that is desired.
4843 * delete_macro () removes things installed by install () in fifo order.
4844 * this is important because of the `defined' special symbol used
4845 * in #if, and also if pushdef/popdef directives are ever implemented.
4847 * If LEN is >= 0, it is the length of the name.
4848 * Otherwise, compute the length by scanning the entire name.
4850 * If HASH is >= 0, it is the precomputed hash code.
4851 * Otherwise, compute the hash code.
4853 * caller must set the value, if any is desired.
4856 install (name
, len
, type
, hash
)
4859 enum node_type type
;
4861 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4870 while (is_idchar (*p
))
4876 hash
= hashf (name
, len
, HASHSIZE
);
4878 hp
= (HASHNODE
*) xmalloc (sizeof (HASHNODE
) + len
+ 1);
4880 hp
->bucket_hdr
= &hashtab
[bucket
];
4881 hp
->next
= hashtab
[bucket
];
4882 hashtab
[bucket
] = hp
;
4884 if (hp
->next
!= NULL
)
4885 hp
->next
->prev
= hp
;
4888 hp
->name
= q
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
4889 memcpy (q
, name
, len
);
4895 * find the most recent hash node for name name (ending with first
4896 * non-identifier char) installed by install
4898 * If LEN is >= 0, it is the length of the name.
4899 * Otherwise, compute the length by scanning the entire name.
4901 * If HASH is >= 0, it is the precomputed hash code.
4902 * Otherwise, compute the hash code.
4905 lookup (name
, len
, hash
)
4914 for (bp
= name
; is_idchar (*bp
); bp
++) ;
4919 hash
= hashf (name
, len
, HASHSIZE
);
4921 bucket
= hashtab
[hash
];
4923 if (bucket
->length
== len
4924 && strncmp ((const char *)bucket
->name
, (const char *)name
, len
) == 0)
4926 bucket
= bucket
->next
;
4932 * Delete a hash node. Some weirdness to free junk from macros.
4933 * More such weirdness will have to be added if you define more hash
4934 * types that need it.
4937 /* Note that the DEFINITION of a macro is removed from the hash table
4938 but its storage is not freed. This would be a storage leak
4939 except that it is not reasonable to keep undefining and redefining
4940 large numbers of macros many times.
4941 In any case, this is necessary, because a macro can be #undef'd
4942 in the middle of reading the arguments to a call to it.
4943 If #undef freed the DEFINITION, that would crash. */
4949 if (hp
->prev
!= NULL
)
4950 hp
->prev
->next
= hp
->next
;
4951 if (hp
->next
!= NULL
)
4952 hp
->next
->prev
= hp
->prev
;
4954 /* make sure that the bucket chain header that
4955 the deleted guy was on points to the right thing afterwards. */
4956 if (hp
== *hp
->bucket_hdr
)
4957 *hp
->bucket_hdr
= hp
->next
;
4963 * return hash function on name. must be compatible with the one
4964 * computed a step at a time, elsewhere
4967 hashf (name
, len
, hashsize
)
4975 r
= HASHSTEP (r
, *name
++);
4977 return MAKE_POS (r
) % hashsize
;
4980 /* Dump all macro definitions as #defines to stdout. */
4987 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
4990 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
4991 if (hp
->type
== T_MACRO
) {
4992 DEFINITION
*defn
= hp
->value
.defn
;
4998 /* Print the definition of the macro HP. */
5000 printf ("#define %s", hp
->name
);
5001 if (defn
->nargs
>= 0) {
5005 for (i
= 0; i
< defn
->nargs
; i
++) {
5006 dump_arg_n (defn
, i
);
5007 if (i
+ 1 < defn
->nargs
)
5017 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
5018 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
);
5019 if (ap
->nchars
!= 0)
5021 offset
+= ap
->nchars
;
5024 if (ap
->raw_before
&& !concat
)
5027 dump_arg_n (defn
, ap
->argno
);
5028 if (ap
->raw_after
) {
5033 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
);
5040 /* Output to stdout a substring of a macro definition.
5041 BASE is the beginning of the definition.
5042 Output characters START thru LENGTH.
5043 Discard newlines outside of strings, thus
5044 converting funny-space markers to ordinary spaces. */
5046 dump_defn_1 (base
, start
, length
)
5051 const U_CHAR
*p
= base
+ start
;
5052 const U_CHAR
*limit
= base
+ start
+ length
;
5057 else if (*p
== '\"' || *p
=='\'') {
5058 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
5059 fwrite (p
, p1
- p
, 1, stdout
);
5066 /* Print the name of argument number ARGNUM of macro definition DEFN.
5067 Recall that DEFN->argnames contains all the arg names
5068 concatenated in reverse order with comma-space in between. */
5070 dump_arg_n (defn
, argnum
)
5074 const U_CHAR
*p
= defn
->argnames
;
5075 while (argnum
+ 1 < defn
->nargs
) {
5076 p
= (const U_CHAR
*) strchr ((const char *)p
, ' ') + 1;
5080 while (*p
&& *p
!= ',') {
5086 /* Initialize the built-in macros. */
5087 #define DSC(x) U x, sizeof x - 1
5088 #define install_spec(name, type) \
5089 install(DSC(name), type, -1);
5090 #define install_value(name, val) \
5091 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
5093 initialize_builtins ()
5097 install_spec ("__BASE_FILE__", T_BASE_FILE
);
5098 install_spec ("__DATE__", T_DATE
);
5099 install_spec ("__FILE__", T_FILE
);
5100 install_spec ("__TIME__", T_TIME
);
5101 install_spec ("__VERSION__", T_VERSION
);
5102 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL
);
5103 install_spec ("__LINE__", T_SPECLINE
);
5105 #ifndef NO_BUILTIN_SIZE_TYPE
5106 install_value ("__SIZE_TYPE__", SIZE_TYPE
);
5108 #ifndef NO_BUILTIN_PTRDIFF_TYPE
5109 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
);
5111 #ifndef NO_BUILTIN_WCHAR_TYPE
5112 install_value ("__WCHAR_TYPE__", WCHAR_TYPE
);
5114 #ifndef NO_BUILTIN_WINT_TYPE
5115 install_value ("__WINT_TYPE__", WINT_TYPE
);
5117 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX
);
5118 install_value ("__USER_LABEL_PREFIX__", user_label_prefix
);
5120 if (flag_signed_char
== 0)
5121 install_value ("__CHAR_UNSIGNED__", "1");
5125 #undef install_value
5127 /* Common handler of command line directives -U, -D and -A. */
5129 run_directive (str
, len
, type
)
5132 enum node_type type
;
5134 const struct directive
*kt
;
5135 FILE_BUF
*ip
= &instack
[++indepth
];
5136 ip
->fname
= "*command line*";
5138 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
5143 ip
->if_stack
= if_stack
;
5145 for (kt
= directive_table
; kt
->type
!= type
; kt
++)
5148 (*kt
->func
) ((U_CHAR
*) str
, (U_CHAR
*) str
+ len
, NULL
);
5152 /* Handle the -D option. If STR is just an identifier, define it with
5153 * value 1. If STR has anything after the identifier, then it should
5154 * be identifier-space-definition. */
5156 make_definition (str
)
5162 /* Copy the entire option so we can modify it.
5163 Change the first "=" in the string to a space. If there is none,
5164 tack " 1" on the end. */
5166 /* Length including the null. */
5167 count
= strlen (str
);
5168 buf
= (char *) alloca (count
+ 2);
5169 memcpy (buf
, str
, count
);
5171 p
= strchr (str
, '=');
5180 run_directive (buf
, count
, T_DEFINE
);
5183 /* Handle the -U option. */
5188 run_directive (str
, strlen (str
), T_UNDEF
);
5191 /* Handles the #assert (-A) and #unassert (-A-) command line options. */
5193 make_assertion (str
)
5196 enum node_type type
= T_ASSERT
;
5206 count
= strlen (str
);
5207 p
= strchr (str
, '=');
5210 /* Copy the entire option so we can modify it. Change the first
5211 "=" in the string to a '(', and tack a ')' on the end. */
5212 char *buf
= (char *) alloca (count
+ 1);
5214 memcpy (buf
, str
, count
);
5220 run_directive (str
, count
, type
);
5223 /* Get the file-mode and data size of the file open on FD
5224 and store them in *MODE_POINTER and *SIZE_POINTER. */
5227 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
5234 if (fstat (fd
, &sbuf
) < 0) return -1;
5235 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
5236 if (size_pointer
) *size_pointer
= sbuf
.st_size
;