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"
29 typedef unsigned char U_CHAR
;
31 /* Name under which this program was invoked. */
33 static const char *progname
;
35 /* Current maximum length of directory names in the search path
36 for include files. (Altered as we get more of them.) */
38 size_t max_include_len
;
40 /* Nonzero means copy comments into the output file. */
42 int put_out_comments
= 0;
44 /* mkdeps.h opaque structure that encapsulates dependency information. */
47 /* Nonzero means print the names of included files rather than
48 the preprocessed output. 1 means just the #include "...",
49 2 means #include <...> as well. */
53 /* Nonzero means print dummy targets for each header file. */
55 int print_deps_phony_targets
= 0;
57 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
61 /* File name which deps are being written to. This is 0 if deps are
62 being written to stdout. */
64 const char *deps_file
= 0;
66 /* Nonzero if missing .h files in -M output are assumed to be
67 generated files and not errors. */
69 int deps_missing_files
= 0;
71 /* Nonzero means don't output line number information. */
75 /* Nonzero means inhibit output of the preprocessed text
76 and instead output the definitions of all user-defined macros
77 in a form suitable for use as input to cccp. */
81 /* Nonzero means don't print warning messages. -w. */
83 int inhibit_warnings
= 0;
85 /* Non-0 means don't output the preprocessed program. */
86 int inhibit_output
= 0;
88 /* Nonzero means warn if slash-star appears in a comment. */
92 /* Nonzero causes output not to be done,
93 but directives such as #define that have side effects
98 /* Value of __USER_LABEL_PREFIX__. Target-dependent, also controlled
99 by -f(no-)leading-underscore. */
100 static const char *user_label_prefix
;
102 /* I/O buffer structure.
103 The `fname' field is nonzero for source files and #include files
104 and for the dummy text used for -D and -U.
105 It is zero for rescanning results of macro expansion
106 and for expanding macro arguments. */
107 #define INPUT_STACK_MAX 200
108 struct file_name_list
;
115 /* Macro that this level is the expansion of.
116 Included so that we can reenable the macro
117 at the end of this level. */
118 struct hashnode
*macro
;
119 /* Value of if_stack at start of this file.
120 Used to prohibit unmatched #endif (etc) in an include file. */
121 struct if_stack
*if_stack
;
122 /* Object to be freed at end of input at this level. */
124 /* Position to start scanning for #include_next in this file. */
125 struct file_name_list
*next_header_dir
;
126 } instack
[INPUT_STACK_MAX
];
128 typedef struct file_buf FILE_BUF
;
130 /* Current nesting level of input sources.
131 `instack[indepth]' is the level currently being read. */
133 #define CHECK_DEPTH(code) \
134 if (indepth >= (INPUT_STACK_MAX - 1)) \
136 error_with_line (line_for_error (instack[indepth].lineno), \
137 "macro or #include recursion too deep"); \
141 /* Current depth in #include directives that use <...>. */
142 int system_include_depth
= 0;
144 /* The output buffer. Its LENGTH field is the amount of room allocated
145 for the buffer, not the number of chars actually present. To get
146 that, subtract outbuf.buf from outbuf.bufp. */
148 #define OUTBUF_SIZE 10 /* initial size of output buffer */
151 /* Grow output buffer OBUF points at
152 so it can hold at least NEEDED more chars. */
154 #define check_expand(OBUF, NEEDED) do { \
155 if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
156 grow_outbuf ((OBUF), (NEEDED)); \
159 struct file_name_list
161 struct file_name_list
*next
;
165 struct file_name_list
*include
= 0; /* First dir to search */
166 /* First dir to search for <file> */
167 struct file_name_list
*first_bracket_include
= 0;
168 struct file_name_list
*last_include
= 0; /* Last in chain */
170 /* List of included files that contained #once. */
171 struct file_name_list
*dont_repeat_files
= 0;
173 /* List of other included files. */
174 struct file_name_list
*all_include_files
= 0;
176 /* Structure allocated for every #define. For a simple replacement
179 nargs = -1, the `pattern' list is null, and the expansion is just
180 the replacement text. Nargs = 0 means a functionlike macro with no args,
182 #define getchar() getc (stdin) .
183 When there are args, the expansion is the replacement text with the
184 args squashed out, and the reflist is a list describing how to
185 build the output from the input: e.g., "3 chars, then the 1st arg,
186 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
187 The chars here come from the expansion. Whatever is left of the
188 expansion after the last arg-occurrence is copied after that arg.
189 Note that the reflist can be arbitrarily long---
190 its length depends on the number of times the arguments appear in
191 the replacement text, not how many args there are. Example:
192 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
194 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
195 where (x, y) means (nchars, argno). */
197 typedef struct definition DEFINITION
;
200 int length
; /* length of expansion string */
203 struct reflist
*next
;
204 char stringify
; /* nonzero if this arg was preceded by a
206 char raw_before
; /* Nonzero if a ## operator before arg. */
207 char raw_after
; /* Nonzero if a ## operator after arg. */
208 int nchars
; /* Number of literal chars to copy before
209 this arg occurrence. */
210 int argno
; /* Number of arg to substitute (origin-0) */
212 /* Names of macro args, concatenated in reverse order
213 with comma-space between them.
214 The only use of this is that we warn on redefinition
215 if this differs between the old and new definitions. */
216 const U_CHAR
*argnames
;
219 /* Chained list of answers to an assertion. */
223 const unsigned char *answer
;
227 /* different kinds of things that can appear in the value field
228 of a hash node. Actually, this may be useless now. */
232 struct answer
*answers
;
235 /* The structure of a node in the hash table. The hash table
236 has entries for all tokens defined by #define commands (type T_MACRO),
237 plus some special tokens like __LINE__ (these each have their own
238 type, and the appropriate code is run when that type of node is seen.
239 It does not contain control words like "#define", which are recognized
240 by a separate piece of code. */
242 /* different flavors of hash nodes --- also used in keyword table */
244 T_DEFINE
= 1, /* `#define' */
245 T_INCLUDE
, /* `#include' */
246 T_INCLUDE_NEXT
,/* `#include_next' */
247 T_IFDEF
, /* `#ifdef' */
248 T_IFNDEF
, /* `#ifndef' */
250 T_ELSE
, /* `#else' */
251 T_ELIF
, /* `#elif' */
252 T_UNDEF
, /* `#undef' */
253 T_LINE
, /* `#line' */
254 T_ENDIF
, /* `#endif' */
255 T_ERROR
, /* `#error' */
256 T_WARNING
, /* `#warning' */
257 T_ASSERT
, /* `#assert' */
258 T_UNASSERT
, /* `#unassert' */
259 T_SPECLINE
, /* special symbol `__LINE__' */
260 T_DATE
, /* `__DATE__' */
261 T_FILE
, /* `__FILE__' */
262 T_BASE_FILE
, /* `__BASE_FILE__' */
263 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
264 T_VERSION
, /* `__VERSION__' */
265 T_TIME
, /* `__TIME__' */
266 T_CONST
, /* Constant value, used by `__STDC__' */
267 T_MACRO
, /* macro defined by `#define' */
268 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
269 T_UNUSED
/* Used for something not defined. */
273 struct hashnode
*next
; /* double links for easy deletion */
274 struct hashnode
*prev
;
275 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
276 chain is kept, in case the node is the head
277 of the chain and gets deleted. */
278 enum node_type type
; /* type of special token */
279 int length
; /* length of token, for quick comparison */
280 U_CHAR
*name
; /* the actual name */
281 union hashval value
; /* pointer to expansion, or whatever */
284 typedef struct hashnode HASHNODE
;
286 static HASHNODE
*parse_assertion
PARAMS ((const unsigned char *,
287 const unsigned char *,
288 struct answer
**, int));
289 static struct answer
**find_answer
PARAMS ((HASHNODE
*,
290 const struct answer
*));
291 static int parse_answer
PARAMS ((const unsigned char *, const unsigned char *,
292 struct answer
**, int));
293 static unsigned char *canonicalize_text
PARAMS ((const unsigned char *,
294 const unsigned char *,
295 const unsigned char **));
297 /* Some definitions for the hash table. The hash function MUST be
298 computed as shown in hashf () below. That is because the rescan
299 loop computes the hash value `on the fly' for most tokens,
300 in order to avoid the overhead of a lot of procedure calls to
301 the hashf () function. Hashf () only exists for the sake of
302 politeness, for use when speed isn't so important. */
304 #define HASHSIZE 1403
305 HASHNODE
*hashtab
[HASHSIZE
];
306 #define HASHSTEP(old, c) ((old << 2) + c)
307 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
309 /* `struct directive' defines one #-directive, including how to handle it. */
312 int length
; /* Length of name */
313 void (*func
) PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
314 /* Function to handle directive */
315 const char *name
; /* Name of directive */
316 enum node_type type
; /* Code which describes which directive. */
319 /* Last arg to output_line_command. */
320 enum file_change_code
{same_file
, enter_file
, leave_file
};
322 /* This structure represents one parsed argument in a macro call.
323 `raw' points to the argument text as written (`raw_length' is its length).
324 `expanded' points to the argument's macro-expansion
325 (its length is `expand_length').
326 `stringified_length' is the length the argument would have
328 `free1' and `free2', if nonzero, point to blocks to be freed
329 when the macro argument data is no longer needed. */
332 U_CHAR
*raw
, *expanded
;
333 int raw_length
, expand_length
;
334 int stringified_length
;
335 U_CHAR
*free1
, *free2
;
340 /* The arglist structure is built by do_define to tell
341 collect_definition where the argument names begin. That
342 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
343 would contain pointers to the strings x, y, and z.
344 Collect_definition would then build a DEFINITION node,
345 with reflist nodes pointing to the places x, y, and z had
346 appeared. So the arglist is just convenience data passed
347 between these two routines. It is not kept around after
348 the current #define has been processed and entered into the
352 struct arglist
*next
;
358 /* Function prototypes. */
360 static void do_define
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
361 static void do_error
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
362 static void do_warning
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
363 static void do_line
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
364 static void do_include
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
365 static void do_include_next
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
366 static void do_undef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
367 static void do_if
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
368 static void do_ifdef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
369 static void do_ifndef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
370 static void do_else
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
371 static void do_elif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
372 static void do_endif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
373 static void do_assert
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
374 static void do_unassert
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
375 static void do_xifdef
PARAMS ((U_CHAR
*, U_CHAR
*, enum node_type
));
377 static struct hashnode
*install
PARAMS ((const U_CHAR
*, int, enum node_type
, int));
378 static int hashf
PARAMS ((const U_CHAR
*, int, int));
379 static int compare_defs
PARAMS ((DEFINITION
*, DEFINITION
*));
380 static int comp_def_part
PARAMS ((int, const U_CHAR
*, int,
381 const U_CHAR
*, int, int));
382 static void delete_macro
PARAMS ((HASHNODE
*));
384 /* First arg to v_message. */
385 enum msgtype
{ WARNING
= 0, ERROR
, FATAL
};
386 static void v_message
PARAMS ((enum msgtype mtype
, int line
,
387 const char *msgid
, va_list ap
))
388 ATTRIBUTE_PRINTF (3, 0);
390 static int line_for_error
PARAMS ((int));
392 /* We know perfectly well which file this is, so we don't need to
395 #if (GCC_VERSION >= 2007)
396 #define abort() fancy_abort(__LINE__, __FUNCTION__)
398 #define abort() fancy_abort(__LINE__, 0);
401 static void macroexpand
PARAMS ((HASHNODE
*, FILE_BUF
*));
402 static void special_symbol
PARAMS ((HASHNODE
*, FILE_BUF
*));
403 static void dump_all_macros
PARAMS ((void));
404 static void dump_defn_1
PARAMS ((const U_CHAR
*, int, int));
405 static void dump_arg_n
PARAMS ((DEFINITION
*, int));
406 static void conditional_skip
PARAMS ((FILE_BUF
*, int, enum node_type
));
407 static void skip_if_group
PARAMS ((FILE_BUF
*, int));
408 static void output_line_command
PARAMS ((FILE_BUF
*, FILE_BUF
*,
409 int, enum file_change_code
));
411 static int eval_if_expression
PARAMS ((const U_CHAR
*, int));
413 static void output_deps
PARAMS ((void));
414 static void initialize_builtins
PARAMS ((void));
415 static void run_directive
PARAMS ((const char *, size_t,
417 static void make_definition
PARAMS ((const char *));
418 static void make_undef
PARAMS ((const char *));
419 static void make_assertion
PARAMS ((const char *));
421 static void grow_outbuf
PARAMS ((FILE_BUF
*, int));
422 static int handle_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*));
423 static void process_include
PARAMS ((struct file_name_list
*,
424 const U_CHAR
*, int, int, FILE_BUF
*));
425 static void finclude
PARAMS ((int, const char *,
426 struct file_name_list
*, FILE_BUF
*));
427 static void init_dependency_output
PARAMS ((void));
428 static void rescan
PARAMS ((FILE_BUF
*, int));
429 static void newline_fix
PARAMS ((U_CHAR
*));
430 static void name_newline_fix
PARAMS ((U_CHAR
*));
431 static U_CHAR
*macarg1
PARAMS ((U_CHAR
*, const U_CHAR
*, int *,
433 static const char *macarg
PARAMS ((struct argdata
*));
434 static int discard_comments
PARAMS ((U_CHAR
*, int, int));
435 static int file_size_and_mode
PARAMS ((int, int *, long *));
437 static U_CHAR
*skip_to_end_of_comment
PARAMS ((FILE_BUF
*, int *));
438 static U_CHAR
*skip_quoted_string
PARAMS ((const U_CHAR
*, const U_CHAR
*,
439 int, int *, int *, int *));
441 int main
PARAMS ((int, char **));
443 /* Convenience. Write U"string" to get an unsigned string constant. */
444 #define U (const unsigned char *)
446 /* Here is the actual list of #-directives, most-often-used first. */
448 struct directive directive_table
[] = {
449 { 6, do_define
, "define", T_DEFINE
},
450 { 7, do_include
, "include", T_INCLUDE
},
451 { 5, do_endif
, "endif", T_ENDIF
},
452 { 5, do_ifdef
, "ifdef", T_IFDEF
},
453 { 2, do_if
, "if", T_IF
, },
454 { 4, do_else
, "else", T_ELSE
},
455 { 6, do_ifndef
, "ifndef", T_IFNDEF
},
456 { 5, do_undef
, "undef", T_UNDEF
},
457 { 4, do_line
, "line", T_LINE
},
458 { 4, do_elif
, "elif", T_ELIF
},
459 { 5, do_error
, "error", T_ERROR
},
460 { 7, do_warning
, "warning", T_WARNING
},
461 { 12, do_include_next
, "include_next", T_INCLUDE_NEXT
},
462 { 6, do_assert
, "assert", T_ASSERT
},
463 { 8, do_unassert
,"unassert",T_UNASSERT
},
464 { -1, 0, "", T_UNUSED
},
467 #define SKIP_WHITE_SPACE(p) do { while (is_nvspace(*p)) p++; } while (0)
468 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space(*p)) p++; } while (0)
470 int errors
= 0; /* Error counter for exit code */
472 static FILE_BUF expand_to_temp_buffer
PARAMS ((const U_CHAR
*, const U_CHAR
*, int));
473 static DEFINITION
*collect_expansion
PARAMS ((U_CHAR
*, U_CHAR
*, int,
476 /* Stack of conditionals currently in progress
477 (including both successful and failing conditionals). */
480 struct if_stack
*next
; /* for chaining to the next stack frame */
481 const char *fname
; /* copied from input when frame is made */
482 int lineno
; /* similarly */
483 int if_succeeded
; /* true if a leg of this if-group
484 has been passed through rescan */
485 enum node_type type
; /* type of last directive seen in this group */
487 typedef struct if_stack IF_STACK_FRAME
;
488 IF_STACK_FRAME
*if_stack
= NULL
;
490 /* Nonzero means -I- has been seen,
491 so don't look for #include "foo" the source-file directory. */
494 /* Pending directives. */
495 enum pending_dir_t
{PD_NONE
= 0, PD_DEFINE
, PD_UNDEF
, PD_ASSERTION
, PD_FILE
};
497 typedef struct pending_dir pending_dir
;
501 enum pending_dir_t type
;
511 const char *in_fname
, *out_fname
;
514 pending_dir
*pend
= (pending_dir
*) xcalloc (argc
, sizeof (pending_dir
));
515 int no_standard_includes
= 0;
518 /* Get rid of any avoidable limit on stack size. */
522 /* Set the stack limit huge so that alloca (particularly stringtab
523 * in dbxread.c) does not fail. */
524 getrlimit (RLIMIT_STACK
, &rlim
);
525 rlim
.rlim_cur
= rlim
.rlim_max
;
526 setrlimit (RLIMIT_STACK
, &rlim
);
528 #endif /* RLIMIT_STACK defined */
535 no_line_commands
= 0;
539 max_include_len
= cpp_GCC_INCLUDE_DIR_len
+ 7; /* ??? */
541 /* It's simplest to just create this struct whether or not it will
545 /* Process switches and find input file name. */
547 for (i
= 1; i
< argc
; i
++) {
548 if (argv
[i
][0] != '-') {
549 if (out_fname
!= NULL
)
550 fatal ("Usage: %s [switches] input output", argv
[0]);
551 else if (in_fname
!= NULL
)
561 break; /* Ignore for compatibility with ISO/extended cpp. */
564 if (!strcmp (argv
[i
], "-lang-c++")
565 || !strcmp (argv
[i
], "-lang-objc++"))
566 fatal ("-traditional is not supported in C++");
567 else if (!strcmp (argv
[i
], "-lang-c89"))
568 fatal ("-traditional and -ansi are mutually exclusive");
569 else if (!strcmp (argv
[i
], "-lang-objc"))
570 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__OBJC__";
571 else if (!strcmp (argv
[i
], "-lang-asm"))
572 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__ASSEMBLER__";
573 else if (!strcmp (argv
[i
], "-lang-fortran"))
574 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "_LANGUAGE_FORTRAN";
575 /* All other possibilities ignored. */
579 if (!strcmp (argv
[i
], "-include"))
582 fatal ("Filename missing after -i option");
584 pend
[i
].type
= PD_FILE
, pend
[i
].arg
= argv
[i
+ 1], i
++;
586 else if (!strcmp (argv
[i
], "-iprefix"))
587 i
++; /* Ignore for compatibility */
588 else if (!strcmp (argv
[i
], "-isystem")
589 || !strcmp (argv
[i
], "-iwithprefix")
590 || !strcmp (argv
[i
], "-iwithprefixbefore")
591 || !strcmp (argv
[i
], "-idirafter"))
592 goto add_include
; /* best we can do */
597 if (out_fname
!= NULL
)
598 fatal ("Output filename specified twice");
600 fatal ("Filename missing after -o option");
601 out_fname
= argv
[++i
];
602 if (!strcmp (out_fname
, "-"))
607 inhibit_warnings
= 1;
611 if (!strcmp (argv
[i
], "-Wcomments"))
613 else if (!strcmp (argv
[i
], "-Wcomment"))
615 else if (!strcmp (argv
[i
], "-Wall")) {
621 if (!strcmp (argv
[i
], "-fleading-underscore"))
622 user_label_prefix
= "_";
623 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
624 user_label_prefix
= "";
631 /* -MD and -MMD for tradcpp are deprecated and undocumented
632 (use -M or -MM with -MF instead), and probably should be
633 removed with the next major GCC version. For the moment
634 we allow these for the benefit of Automake 1.4, which
635 uses these when dependency tracking is enabled. Automake
636 1.5 will fix this. */
637 if (!strncmp (argv
[i
], "-MD", 3)) {
640 } else if (!strncmp (argv
[i
], "-MMD", 4)) {
643 } else if (!strcmp (argv
[i
], "-M")) {
645 } else if (!strcmp (argv
[i
], "-MM")) {
647 } else if (!strcmp (argv
[i
], "-MG")) {
648 deps_missing_files
= 1;
649 } else if (!strcmp (argv
[i
], "-MF")) {
651 } else if (!strcmp (argv
[i
], "-MP")) {
652 print_deps_phony_targets
= 1;
653 } else if (!strcmp (argv
[i
], "-MQ") || !strcmp (argv
[i
], "-MT")) {
654 /* Add a target. -MQ quotes for Make. */
655 const char *tgt
= argv
[i
] + 3;
656 int quoted
= argv
[i
][2] == 'Q';
658 if (*tgt
== '\0' && i
+ 1 == argc
)
659 fatal ("Target missing after %s option", argv
[i
]);
665 deps_add_target (deps
, tgt
, quoted
);
672 else if (i
+ 1 == argc
)
673 fatal ("Filename missing after %s option", argv
[i
]);
675 deps_file
= argv
[++i
];
686 fprintf (stderr
, "GNU traditional CPP version %s\n", version_string
);
697 else if (i
+ 1 == argc
)
698 fatal ("Macro name missing after -%c option", c
);
703 pend
[i
].type
= PD_DEFINE
;
705 pend
[i
].type
= PD_UNDEF
;
707 pend
[i
].type
= PD_ASSERTION
;
713 put_out_comments
= 1;
717 if (!strcmp (argv
[i
], "-pedantic"))
718 fatal ("-pedantic and -traditional are mutually exclusive");
722 if (!strcmp (argv
[i
], "-trigraphs"))
723 fatal ("-trigraphs and -traditional are mutually exclusive");
727 no_line_commands
= 1;
730 case 'I': /* Add directory to path for includes. */
733 struct file_name_list
*dirtmp
;
735 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
738 dirtmp
= (struct file_name_list
*)
739 xmalloc (sizeof (struct file_name_list
));
740 dirtmp
->next
= 0; /* New one goes on the end */
744 last_include
->next
= dirtmp
;
745 last_include
= dirtmp
; /* Tail follows the last one */
746 if (argv
[i
][1] == 'I' && argv
[i
][2] != 0)
747 dirtmp
->fname
= argv
[i
] + 2;
748 else if (i
+ 1 == argc
)
749 fatal ("Directory name missing after -I option");
751 dirtmp
->fname
= argv
[++i
];
752 if (strlen (dirtmp
->fname
) > max_include_len
)
753 max_include_len
= strlen (dirtmp
->fname
);
754 if (ignore_srcdir
&& first_bracket_include
== 0)
755 first_bracket_include
= dirtmp
;
761 /* -nostdinc causes no default include directories.
762 You must specify all include-file directories with -I. */
763 no_standard_includes
= 1;
766 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
767 if (in_fname
== NULL
) {
770 } else if (out_fname
== NULL
) {
773 } /* else fall through into error */
776 fatal ("Invalid option `%s'", argv
[i
]);
781 init_dependency_output ();
783 /* After checking the environment variables, check if -M or -MM has
784 not been specified, but other -M options have. */
786 && (deps_missing_files
|| deps_file
|| print_deps_phony_targets
))
787 fatal ("you must additionally specify either -M or -MM");
789 if (user_label_prefix
== 0)
790 user_label_prefix
= USER_LABEL_PREFIX
;
794 /* Set the default target (if there is none already), and
795 the dependency on the main file. */
796 deps_add_default_target (deps
, in_fname
);
798 deps_add_dep (deps
, in_fname
);
801 /* Install __LINE__, etc. Must follow option processing. */
802 initialize_builtins ();
804 /* Do defines specified with -D and undefines specified with -U. */
805 for (i
= 1; i
< argc
; i
++)
806 if (pend
[i
].type
== PD_DEFINE
)
807 make_definition (pend
[i
].arg
);
808 else if (pend
[i
].type
== PD_UNDEF
)
809 make_undef (pend
[i
].arg
);
810 else if (pend
[i
].type
== PD_ASSERTION
)
811 make_assertion (pend
[i
].arg
);
813 /* Unless -fnostdinc,
814 tack on the standard include file dirs to the specified list */
815 if (!no_standard_includes
) {
816 const struct default_include
*di
;
817 struct file_name_list
*old_last_include
= last_include
;
818 struct file_name_list
*dirtmp
;
819 for (di
= cpp_include_defaults
; di
->fname
; di
++) {
822 dirtmp
= (struct file_name_list
*)
823 xmalloc (sizeof (struct file_name_list
));
824 dirtmp
->next
= 0; /* New one goes on the end */
828 last_include
->next
= dirtmp
;
829 last_include
= dirtmp
; /* Tail follows the last one */
830 dirtmp
->fname
= di
->fname
;
831 if (strlen (dirtmp
->fname
) > max_include_len
)
832 max_include_len
= strlen (dirtmp
->fname
);
835 if (ignore_srcdir
&& first_bracket_include
== 0)
836 first_bracket_include
= old_last_include
->next
;
839 /* Initialize output buffer */
841 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
842 outbuf
.bufp
= outbuf
.buf
;
843 outbuf
.length
= OUTBUF_SIZE
;
845 /* Scan the -i files before the main input.
846 Much like #including them, but with no_output set
847 so that only their macro definitions matter. */
851 for (i
= 1; i
< argc
; i
++)
852 if (pend
[i
].type
== PD_FILE
)
854 int fd
= open (pend
[i
].arg
, O_RDONLY
, 0666);
857 perror_with_name (pend
[i
].arg
);
858 return FATAL_EXIT_CODE
;
861 /* For -M, add this file to the dependencies. */
863 deps_add_dep (deps
, pend
[i
].arg
);
865 finclude (fd
, pend
[i
].arg
, 0, &outbuf
);
870 /* Pending directives no longer needed. */
873 /* Create an input stack level for the main input file
874 and copy the entire contents of the file into it. */
876 fp
= &instack
[++indepth
];
878 /* JF check for stdin */
879 if (in_fname
== NULL
|| *in_fname
== 0) {
882 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
885 if (file_size_and_mode (f
, &st_mode
, &st_size
))
887 fp
->fname
= in_fname
;
889 /* JF all this is mine about reading pipes and ttys */
890 if (!S_ISREG (st_mode
)) {
891 /* Read input from a file that is not a normal disk file.
892 We cannot preallocate a buffer with the correct size,
893 so we must read in the file a piece at the time and make it bigger. */
901 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
904 cnt
= read (f
, bufp
, bsize
- size
);
905 if (cnt
< 0) goto sys_error
; /* error! */
906 if (cnt
== 0) break; /* End of file */
909 if (bsize
== size
) { /* Buffer is full! */
911 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
912 bufp
= fp
->buf
+ size
; /* May have moved */
917 /* Read a file whose size we can determine in advance.
918 For the sake of VMS, st_size is just an upper bound. */
921 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
923 while (st_size
> 0) {
924 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
934 fp
->if_stack
= if_stack
;
936 /* Make sure data ends with a newline. And put a null after it. */
938 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
939 fp
->buf
[fp
->length
++] = '\n';
940 fp
->buf
[fp
->length
] = '\0';
942 /* Now that we know the input file is valid, open the output. */
944 if (!out_fname
|| !strcmp (out_fname
, ""))
945 out_fname
= "stdout";
946 else if (! freopen (out_fname
, "w", stdout
))
947 pfatal_with_name (out_fname
);
949 output_line_command (fp
, &outbuf
, 0, same_file
);
951 /* Scan the input, processing macros and directives. */
955 /* Now we have processed the entire input
956 Write whichever kind of output has been requested. */
961 else if (! inhibit_output
)
962 if (write (fileno (stdout
), outbuf
.buf
, outbuf
.bufp
- outbuf
.buf
) < 0)
963 fatal ("I/O error on output");
965 /* Don't write the deps file if preprocessing has failed. */
966 if (print_deps
&& errors
== 0)
969 /* Destruct the deps object. */
973 fatal ("I/O error on output");
976 exit (FATAL_EXIT_CODE
);
977 exit (SUCCESS_EXIT_CODE
);
980 pfatal_with_name (in_fname
);
983 /* Set up dependency-file output. */
985 init_dependency_output ()
987 char *spec
, *s
, *output_file
;
989 /* Either of two environment variables can specify output of deps.
990 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
991 where OUTPUT_FILE is the file to write deps info to
992 and DEPS_TARGET is the target to mention in the deps. */
996 spec
= getenv ("DEPENDENCIES_OUTPUT");
1001 spec
= getenv ("SUNPRO_DEPENDENCIES");
1008 /* Find the space before the DEPS_TARGET, if there is one. */
1009 s
= strchr (spec
, ' ');
1012 /* Let the caller perform MAKE quoting. */
1013 deps_add_target (deps
, s
+ 1, 0);
1014 output_file
= (char *) xmalloc (s
- spec
+ 1);
1015 memcpy (output_file
, spec
, s
- spec
);
1016 output_file
[s
- spec
] = 0;
1021 /* Command line overrides environment variables. */
1023 deps_file
= output_file
;
1027 /* If dependencies go to standard output, or -MG is used, we should
1028 suppress output. The user may be requesting other stuff to
1029 stdout, with -dM, -v etc. We let them shoot themselves in the
1031 if (deps_file
== 0 || deps_missing_files
)
1035 /* Use mkdeps.c to output dependency information. */
1039 /* Stream on which to print the dependency information. */
1040 FILE *deps_stream
= 0;
1041 const char *const deps_mode
= deps_append
? "a" : "w";
1044 deps_stream
= stdout
;
1047 deps_stream
= fopen (deps_file
, deps_mode
);
1048 if (deps_stream
== 0)
1050 error_from_errno (deps_file
);
1055 deps_write (deps
, deps_stream
, 72);
1057 if (print_deps_phony_targets
)
1058 deps_phony_targets (deps
, deps_stream
);
1060 /* Don't close stdout. */
1063 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
1064 fatal ("I/O error on output");
1068 /* Move all backslash-newline pairs out of embarrassing places.
1069 Exchange all such pairs following BP
1070 with any potentially-embarrasing characters that follow them.
1071 Potentially-embarrassing characters are / and *
1072 (because a backslash-newline inside a comment delimiter
1073 would cause it not to be recognized). */
1078 register U_CHAR
*p
= bp
;
1079 register int count
= 0;
1081 /* First count the backslash-newline pairs here. */
1083 while (*p
++ == '\\' && *p
++ == '\n')
1088 /* Exit if what follows the backslash-newlines is not embarrassing. */
1090 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
1093 /* Copy all potentially embarrassing characters
1094 that follow the backslash-newline pairs
1095 down to where the pairs originally started. */
1097 while (*p
== '*' || *p
== '/')
1100 /* Now write the same number of pairs after the embarrassing chars. */
1101 while (count
-- > 0) {
1107 /* Like newline_fix but for use within a directive-name.
1108 Move any backslash-newlines up past any following symbol constituents. */
1110 name_newline_fix (bp
)
1113 register U_CHAR
*p
= bp
;
1114 register int count
= 0;
1116 /* First count the backslash-newline pairs here. */
1118 while (*p
++ == '\\' && *p
++ == '\n')
1123 /* What follows the backslash-newlines is not embarrassing. */
1125 if (count
== 0 || !is_idchar (*p
))
1128 /* Copy all potentially embarrassing characters
1129 that follow the backslash-newline pairs
1130 down to where the pairs originally started. */
1132 while (is_idchar (*p
))
1135 /* Now write the same number of pairs after the embarrassing chars. */
1136 while (count
-- > 0) {
1143 * The main loop of the program.
1145 * Read characters from the input stack, transferring them to the
1148 * Macros are expanded and push levels on the input stack.
1149 * At the end of such a level it is popped off and we keep reading.
1150 * At the end of any other kind of level, we return.
1151 * #-directives are handled, except within macros.
1153 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1154 * and insert them when appropriate. This is set while scanning macro
1155 * arguments before substitution. It is zero when scanning for final output.
1156 * There are three types of Newline markers:
1157 * * Newline - follows a macro name that was not expanded
1158 * because it appeared inside an expansion of the same macro.
1159 * This marker prevents future expansion of that identifier.
1160 * When the input is rescanned into the final output, these are deleted.
1161 * These are also deleted by ## concatenation.
1162 * * Newline Space (or Newline and any other whitespace character)
1163 * stands for a place that tokens must be separated or whitespace
1164 * is otherwise desirable, but where the ANSI standard specifies there
1165 * is no whitespace. This marker turns into a Space (or whichever other
1166 * whitespace char appears in the marker) in the final output,
1167 * but it turns into nothing in an argument that is stringified with #.
1168 * Such stringified arguments are the only place where the ANSI standard
1169 * specifies with precision that whitespace may not appear.
1171 * During this function, IP->bufp is kept cached in IBP for speed of access.
1172 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1173 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1174 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1175 * explicitly, and before RECACHE, since RECACHE uses OBP.
1179 rescan (op
, output_marks
)
1183 /* Character being scanned in main loop. */
1186 /* Length of pending accumulated identifier. */
1187 register int ident_length
= 0;
1189 /* Hash code of pending accumulated identifier. */
1190 register int hash
= 0;
1192 /* Current input level (&instack[indepth]). */
1195 /* Pointer for scanning input. */
1196 register U_CHAR
*ibp
;
1198 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1199 register U_CHAR
*limit
;
1201 /* Pointer for storing output. */
1202 register U_CHAR
*obp
;
1204 /* REDO_CHAR is nonzero if we are processing an identifier
1205 after backing up over the terminating character.
1206 Sometimes we process an identifier without backing up over
1207 the terminating character, if the terminating character
1208 is not special. Backing up is done so that the terminating character
1209 will be dispatched on again once the identifier is dealt with. */
1212 /* 1 if within an identifier inside of which a concatenation
1213 marker (Newline -) has been seen. */
1214 int concatenated
= 0;
1216 /* While scanning a comment or a string constant,
1217 this records the line it started on, for error messages. */
1220 /* Record position of last `real' newline. */
1221 U_CHAR
*beg_of_line
;
1223 /* This has to be a global bacause of RECACHE. */
1224 U_CHAR
*obufp_before_macroname
= NULL
;
1226 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1229 do { ip->macro->type = T_MACRO; \
1230 if (ip->free_ptr) free (ip->free_ptr); \
1231 --indepth; } while (0)
1233 /* Reload `rescan's local variables that describe the current
1234 level of the input stack. */
1237 do { ip = &instack[indepth]; \
1239 limit = ip->buf + ip->length; \
1241 check_expand (op, limit - ibp); \
1243 obufp_before_macroname += op->bufp - obp; \
1244 obp = op->bufp; } while (0)
1246 if (no_output
&& instack
[indepth
].fname
!= 0)
1247 skip_if_group (&instack
[indepth
], 1);
1253 /* Our caller must always put a null after the end of
1254 the input at each input stack level. */
1267 /* Always merge lines ending with backslash-newline,
1268 even in middle of identifier. */
1271 --obp
; /* remove backslash from obuf */
1274 /* Otherwise, backslash suppresses specialness of following char,
1275 so copy it here to prevent the switch from seeing it.
1276 But first get any pending identifier processed. */
1277 if (ident_length
> 0)
1283 /* If this is expanding a macro definition, don't recognize
1284 preprocessor directives. */
1290 /* # keyword: a # must be the first char on the line */
1291 if (beg_of_line
== 0)
1293 if (beg_of_line
+ 1 != ibp
)
1296 /* This # can start a directive. */
1298 --obp
; /* Don't copy the '#' */
1302 if (! handle_directive (ip
, op
)) {
1306 /* Not a known directive: treat it as ordinary text.
1307 IP, OP, IBP, etc. have not been changed. */
1308 if (no_output
&& instack
[indepth
].fname
) {
1309 /* If not generating expanded output,
1310 what we do with ordinary text is skip it.
1311 Discard everything until next # directive. */
1312 skip_if_group (&instack
[indepth
], 1);
1317 ++obp
; /* Copy the '#' after all */
1323 /* A # directive has been successfully processed. */
1324 /* If not generating expanded output, ignore everything until
1325 next # directive. */
1326 if (no_output
&& instack
[indepth
].fname
)
1327 skip_if_group (&instack
[indepth
], 1);
1333 case '\"': /* skip quoted string */
1335 /* A single quoted string is treated like a double -- some
1336 programs (e.g., troff) are perverse this way */
1341 start_line
= ip
->lineno
;
1343 /* Skip ahead to a matching quote. */
1347 if (ip
->macro
!= 0) {
1348 /* try harder: this string crosses a macro expansion boundary */
1360 /* Traditionally, end of line ends a string constant with no error.
1361 So exit the loop and record the new line. */
1369 /* Backslash newline is replaced by nothing at all,
1370 but keep the line counts correct. */
1375 /* ANSI stupidly requires that in \\ the second \
1376 is *not* prevented from combining with a newline. */
1377 while (*ibp
== '\\' && ibp
[1] == '\n') {
1396 if (*ibp
== '\\' && ibp
[1] == '\n')
1398 /* Don't look for comments inside a macro definition. */
1401 /* A comment constitutes white space, so it can terminate an identifier.
1402 Process the identifier, if any. */
1409 /* We have a comment. Skip it, optionally copying it to output. */
1411 start_line
= ip
->lineno
;
1413 ++ibp
; /* Skip the star. */
1415 /* In K+R C, a comment is equivalent to nothing. Note that we
1416 already output the slash; we might not want it. */
1417 if (! put_out_comments
)
1423 U_CHAR
*before_bp
= ibp
;
1425 while (ibp
< limit
) {
1428 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
1429 warning("`/*' within comment");
1432 if (*ibp
== '\\' && ibp
[1] == '\n')
1434 if (ibp
>= limit
|| *ibp
== '/')
1439 /* Copy the newline into the output buffer, in order to
1440 avoid the pain of a #line every time a multiline comment
1442 if (!put_out_comments
)
1450 error_with_line (line_for_error (start_line
),
1451 "unterminated comment");
1454 if (put_out_comments
) {
1455 memcpy (obp
, before_bp
, ibp
- before_bp
);
1456 obp
+= ibp
- before_bp
;
1462 case '0': case '1': case '2': case '3': case '4':
1463 case '5': case '6': case '7': case '8': case '9':
1464 /* If digit is not part of identifier, it starts a number,
1465 which means that following letters are not an identifier.
1466 "0x5" does not refer to an identifier "x5".
1467 So copy all alphanumerics that follow without accumulating
1468 as an identifier. Periods also, for sake of "3.e7". */
1470 if (ident_length
== 0) {
1471 while (ibp
< limit
) {
1472 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1477 if (!ISALNUM (c
) && c
!= '.' && c
!= '_') {
1482 /* A sign can be part of a preprocessing number
1483 if it follows an e. */
1484 if (c
== 'e' || c
== 'E') {
1485 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1489 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
1491 /* Traditional C does not let the token go past the sign. */
1501 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1502 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1503 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1504 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1506 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1507 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1508 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1509 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1512 /* Compute step of hash function, to avoid a proc call on every token */
1513 hash
= HASHSTEP (hash
, c
);
1517 /* If reprocessing a macro expansion, newline is a special marker. */
1518 if (ip
->macro
!= 0) {
1519 /* Newline White is a "funny space" to separate tokens that are
1520 supposed to be separate but without space between.
1521 Here White means any horizontal whitespace character.
1522 Newline - marks a recursive macro use that is not
1523 supposed to be expandable. */
1526 /* Newline - inhibits expansion of preceding token.
1527 If expanding a macro arg, we keep the newline -.
1528 In final output, it is deleted. */
1529 if (! concatenated
) {
1534 if (!output_marks
) {
1537 /* If expanding a macro arg, keep the newline -. */
1540 } else if (is_space (*ibp
)) {
1541 /* Newline Space does not prevent expansion of preceding token
1542 so expand the preceding token and then come back. */
1543 if (ident_length
> 0)
1546 /* If generating final output, newline space makes a space. */
1547 if (!output_marks
) {
1549 /* And Newline Newline makes a newline, so count it. */
1550 if (obp
[-1] == '\n')
1553 /* If expanding a macro arg, keep the newline space.
1554 If the arg gets stringified, newline space makes nothing. */
1557 } else abort (); /* Newline followed by something random? */
1561 /* If there is a pending identifier, handle it and come back here. */
1562 if (ident_length
> 0)
1567 /* Update the line counts and output a #line if necessary. */
1570 if (ip
->lineno
!= op
->lineno
) {
1572 output_line_command (ip
, op
, 1, same_file
);
1573 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
1578 /* Come here either after (1) a null character that is part of the input
1579 or (2) at the end of the input, because there is a null there. */
1582 /* Our input really contains a null character. */
1585 /* At end of a macro-expansion level, pop it and read next level. */
1586 if (ip
->macro
!= 0) {
1589 /* If we have an identifier that ends here, process it now, so
1590 we get the right error for recursion. */
1591 if (ident_length
&& ! is_idchar (*instack
[indepth
- 1].bufp
)) {
1600 /* If we don't have a pending identifier,
1601 return at end of input. */
1602 if (ident_length
== 0) {
1610 /* If we do have a pending identifier, just consider this null
1611 a special character and arrange to dispatch on it again.
1612 The second time, IDENT_LENGTH will be zero so we will return. */
1618 /* Handle the case of a character such as /, ', " or null
1619 seen following an identifier. Back over it so that
1620 after the identifier is processed the special char
1621 will be dispatched on again. */
1631 if (ident_length
> 0) {
1632 register HASHNODE
*hp
;
1634 /* We have just seen an identifier end. If it's a macro, expand it.
1636 IDENT_LENGTH is the length of the identifier
1637 and HASH is its hash code.
1639 The identifier has already been copied to the output,
1640 so if it is a macro we must remove it.
1642 If REDO_CHAR is 0, the char that terminated the identifier
1643 has been skipped in the output and the input.
1644 OBP-IDENT_LENGTH-1 points to the identifier.
1645 If the identifier is a macro, we must back over the terminator.
1647 If REDO_CHAR is 1, the terminating char has already been
1648 backed over. OBP-IDENT_LENGTH points to the identifier. */
1650 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
1653 if (hp
->length
== ident_length
) {
1654 /* obufp_before_macroname is used only in this block,
1655 but it has to be global because of RECACHE. */
1656 int op_lineno_before_macroname
;
1657 register int i
= ident_length
;
1658 register U_CHAR
*p
= hp
->name
;
1659 register U_CHAR
*q
= obp
- i
;
1664 do { /* All this to avoid a strncmp () */
1669 /* We found a use of a macro name.
1670 see if the context shows it is a macro call. */
1672 /* Back up over terminating character if not already done. */
1678 obufp_before_macroname
= obp
- ident_length
;
1679 op_lineno_before_macroname
= op
->lineno
;
1681 /* If macro wants an arglist, verify that a '(' follows.
1682 first skip all whitespace, copying it to the output
1683 after the macro name. Then, if there is no '(',
1684 decide this is not a macro call and leave things that way. */
1685 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
1688 /* Scan forward over whitespace, copying it to the output. */
1689 if (ibp
== limit
&& ip
->macro
!= 0) {
1693 /* A comment: copy it unchanged or discard it. */
1694 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
1695 if (put_out_comments
) {
1700 while (ibp
+ 1 != limit
1701 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
1702 /* We need not worry about newline-marks,
1703 since they are never found in comments. */
1705 /* Newline in a file. Count it. */
1709 if (put_out_comments
)
1715 if (put_out_comments
) {
1720 else if (is_space (*ibp
)) {
1722 if (ibp
[-1] == '\n') {
1723 if (ip
->macro
== 0) {
1724 /* Newline in a file. Count it. */
1727 } else if (!output_marks
) {
1728 /* A newline mark, and we don't want marks
1729 in the output. If it is newline-hyphen,
1730 discard it entirely. Otherwise, it is
1731 newline-whitechar, so keep the whitechar. */
1741 /* A newline mark; copy both chars to the output. */
1752 /* This is now known to be a macro call.
1753 Discard the macro name from the output,
1754 along with any following whitespace just copied. */
1755 obp
= obufp_before_macroname
;
1756 op
->lineno
= op_lineno_before_macroname
;
1758 /* Expand the macro, reading arguments as needed,
1759 and push the expansion on the input stack. */
1762 macroexpand (hp
, op
);
1764 /* Reexamine input stack, since macroexpand has pushed
1765 a new level on it. */
1772 } /* End hash-table-search loop */
1773 ident_length
= hash
= 0; /* Stop collecting identifier */
1776 } /* End if (ident_length > 0) */
1778 } /* End per-char loop */
1780 /* Come here to return -- but first give an error message
1781 if there was an unterminated successful conditional. */
1783 if (if_stack
!= ip
->if_stack
) {
1785 switch (if_stack
->type
) {
1804 error_with_line (line_for_error (if_stack
->lineno
),
1805 "unterminated #%s conditional", str
);
1807 if_stack
= ip
->if_stack
;
1811 * Rescan a string into a temporary buffer and return the result
1812 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1814 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1815 * and insert such markers when appropriate. See `rescan' for details.
1816 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1817 * before substitution; it is 0 for other uses.
1820 expand_to_temp_buffer (buf
, limit
, output_marks
)
1821 const U_CHAR
*buf
, *limit
;
1824 register FILE_BUF
*ip
;
1826 int length
= limit
- buf
;
1828 int odepth
= indepth
;
1833 /* Set up the input on the input stack. */
1835 buf1
= (U_CHAR
*) alloca (length
+ 1);
1837 register const U_CHAR
*p1
= buf
;
1838 register U_CHAR
*p2
= buf1
;
1845 /* Set up to receive the output. */
1847 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
1848 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
1853 CHECK_DEPTH ({return obuf
;});
1857 ip
= &instack
[indepth
];
1861 ip
->length
= length
;
1862 ip
->buf
= ip
->bufp
= buf1
;
1863 ip
->if_stack
= if_stack
;
1865 ip
->lineno
= obuf
.lineno
= 1;
1867 /* Scan the input, create the output. */
1869 rescan (&obuf
, output_marks
);
1871 /* Pop input stack to original state. */
1874 if (indepth
!= odepth
)
1877 /* Record the output. */
1878 obuf
.length
= obuf
.bufp
- obuf
.buf
;
1884 * Process a # directive. Expects IP->bufp to point to the '#', as in
1885 * `#define foo bar'. Passes to the command handler
1886 * (do_define, do_include, etc.): the addresses of the 1st and
1887 * last chars of the command (starting immediately after the #
1888 * keyword), plus op and the keyword table pointer. If the command
1889 * contains comments it is copied into a temporary buffer sans comments
1890 * and the temporary buffer is passed to the command handler instead.
1891 * Likewise for backslash-newlines.
1893 * Returns nonzero if this was a known # directive.
1894 * Otherwise, returns zero, without advancing the input pointer.
1898 handle_directive (ip
, op
)
1901 register U_CHAR
*bp
, *cp
;
1902 register struct directive
*kt
;
1903 register int ident_length
;
1906 /* Nonzero means we must copy the entire command
1907 to get rid of comments or backslash-newlines. */
1908 int copy_command
= 0;
1910 U_CHAR
*ident
, *after_ident
;
1913 /* Skip whitespace and \-newline. */
1915 if (is_nvspace (*bp
))
1917 else if (*bp
== '/' && (newline_fix (bp
+ 1), bp
[1]) == '*') {
1919 skip_to_end_of_comment (ip
, &ip
->lineno
);
1921 } else if (*bp
== '\\' && bp
[1] == '\n') {
1922 bp
+= 2; ip
->lineno
++;
1926 /* Now find end of directive name.
1927 If we encounter a backslash-newline, exchange it with any following
1928 symbol-constituents so that we end up with a contiguous name. */
1932 if (is_idchar (*cp
))
1935 if (*cp
== '\\' && cp
[1] == '\n')
1936 name_newline_fix (cp
);
1937 if (is_idchar (*cp
))
1942 ident_length
= cp
- bp
;
1946 /* A line of just `#' becomes blank. */
1948 if (ident_length
== 0 && *after_ident
== '\n') {
1949 ip
->bufp
= after_ident
;
1954 * Decode the keyword and call the appropriate expansion
1955 * routine, after moving the input pointer up to the next line.
1957 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
1958 if (kt
->length
== ident_length
1959 && !strncmp (kt
->name
, (const char *)ident
, ident_length
)) {
1960 register U_CHAR
*buf
;
1961 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
1962 int unterminated
= 0;
1964 /* Nonzero means do not delete comments within the directive.
1965 #define needs this to detect traditional token paste. */
1966 int keep_comments
= kt
->type
== T_DEFINE
;
1968 /* Find the end of this command (first newline not backslashed
1969 and not in a string or comment).
1970 Set COPY_COMMAND if the command must be copied
1971 (it contains a backslash-newline or a comment). */
1973 buf
= bp
= after_ident
;
1974 while (bp
< limit
) {
1975 register U_CHAR c
= *bp
++;
1989 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
1991 /* Traditional preprocessing permits unterminated strings. */
1997 /* <...> is special for #include. */
1999 if (kt
->type
!= T_INCLUDE
)
2001 while (*bp
&& *bp
!= '>') bp
++;
2005 if (*bp
== '\\' && bp
[1] == '\n')
2008 U_CHAR
*obp
= bp
- 1;
2010 skip_to_end_of_comment (ip
, &ip
->lineno
);
2012 /* No need to copy the command because of a comment at the end;
2013 just don't include the comment in the directive. */
2014 if (bp
== limit
|| *bp
== '\n') {
2018 /* Don't remove the comments if this is #define. */
2019 if (! keep_comments
)
2025 --bp
; /* Point to the newline */
2033 resume_p
= ip
->bufp
;
2034 /* BP is the end of the directive.
2035 RESUME_P is the next interesting data after the directive.
2036 A comment may come between. */
2039 register U_CHAR
*xp
= buf
;
2040 /* Need to copy entire command into temp buffer before dispatching */
2042 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
2046 /* Copy to the new buffer, deleting comments
2047 and backslash-newlines (and whitespace surrounding the latter). */
2050 register U_CHAR c
= *xp
++;
2057 /* <...> is special for #include. */
2059 if (kt
->type
!= T_INCLUDE
)
2061 while (xp
< bp
&& c
!= '>') {
2063 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
2074 if (cp
!= buf
&& is_space (cp
[-1])) {
2075 while (cp
!= buf
&& is_space(cp
[-1])) cp
--;
2077 SKIP_WHITE_SPACE (xp
);
2078 } else if (is_nvspace (*xp
)) {
2080 SKIP_WHITE_SPACE (xp
);
2090 register const U_CHAR
*bp1
2091 = skip_quoted_string (xp
- 1, limit
, ip
->lineno
, 0, 0, 0);
2100 skip_to_end_of_comment (ip
, 0);
2102 while (xp
!= ip
->bufp
)
2104 /* Delete the slash. */
2112 /* Null-terminate the copy. */
2119 ip
->bufp
= resume_p
;
2121 /* Call the appropriate command handler. buf now points to
2122 either the appropriate place in the input buffer, or to
2123 the temp buffer if it was necessary to make one. cp
2124 points to the first char after the contents of the (possibly
2125 copied) command, in either case. */
2126 (*kt
->func
) (buf
, cp
, op
);
2127 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2136 static const char *const
2137 monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2138 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2141 * expand things like __FILE__. Place the expansion into the output
2142 * buffer *without* rescanning.
2145 special_symbol (hp
, op
)
2153 FILE_BUF
*ip
= NULL
;
2154 static struct tm
*timebuf
= NULL
;
2156 int paren
= 0; /* For special `defined' keyword */
2158 for (i
= indepth
; i
>= 0; i
--)
2159 if (instack
[i
].fname
!= NULL
) {
2164 fatal ("not in any file?!");
2171 if (hp
->type
== T_FILE
)
2174 string
= instack
[0].fname
;
2178 char *tmp
= (char *) alloca (3 + strlen (string
));
2179 sprintf (tmp
, "\"%s\"", string
);
2188 case T_INCLUDE_LEVEL
:
2190 char *tmp
= (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2192 for (i
= indepth
; i
>= 0; i
--)
2193 if (instack
[i
].fname
!= NULL
)
2196 sprintf (tmp
, "%d", true_indepth
- 1);
2203 char *tmp
= (char *) alloca (3 + strlen (version_string
));
2204 sprintf (tmp
, "\"%s\"", version_string
);
2210 buf
= hp
->value
.cpval
;
2215 char *tmp
= (char *) alloca (10);
2216 sprintf (tmp
, "%d", ip
->lineno
);
2224 char *tmp
= (char *) alloca (20);
2226 if (timebuf
== NULL
) {
2228 timebuf
= localtime (&t
);
2230 if (hp
->type
== T_DATE
)
2231 sprintf (tmp
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2232 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2234 sprintf (tmp
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2240 case T_SPEC_DEFINED
:
2241 buf
= " 0 "; /* Assume symbol is not defined */
2242 ip
= &instack
[indepth
];
2243 SKIP_WHITE_SPACE (ip
->bufp
);
2244 if (*ip
->bufp
== '(') {
2246 ip
->bufp
++; /* Skip over the paren */
2247 SKIP_WHITE_SPACE (ip
->bufp
);
2250 if (!is_idstart (*ip
->bufp
))
2253 HASHNODE
*hp
= lookup (ip
->bufp
, -1, -1);
2255 if (hp
&& hp
->type
!= T_UNUSED
&& hp
->type
!= T_SPEC_DEFINED
)
2258 while (is_idchar (*ip
->bufp
))
2260 SKIP_WHITE_SPACE (ip
->bufp
);
2262 if (*ip
->bufp
!= ')')
2270 error ("`defined' must be followed by ident or (ident)");
2274 error ("cccp error: invalid special hash type"); /* time for gdb */
2278 check_expand (op
, len
);
2279 memcpy (op
->bufp
, buf
, len
);
2284 /* Routines to handle #directives */
2287 * Process include file by reading it in and calling rescan.
2288 * Expects to see "fname" or <fname> on the input.
2291 do_include (buf
, limit
, op
)
2292 U_CHAR
*buf
, *limit
;
2295 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2297 struct file_name_list
*stackp
= include
; /* Chain of dirs to search */
2298 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
2301 int retried
= 0; /* Have already tried macro
2302 expanding the include line*/
2303 FILE_BUF trybuf
; /* It got expanded into here */
2304 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2309 SKIP_WHITE_SPACE (fbeg
);
2310 /* Discard trailing whitespace so we can easily see
2311 if we have parsed all the significant chars we were given. */
2312 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2317 while (fend
!= limit
&& *fend
!= '\"')
2319 if (*fend
== '\"' && fend
+ 1 == limit
) {
2322 /* We have "filename". Figure out directory this source
2323 file is coming from and put it on the front of the list. */
2325 /* If -I- was specified, don't search current dir, only spec'd ones. */
2326 if (ignore_srcdir
) break;
2328 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
2331 const char *ep
, *nam
;
2333 if ((nam
= fp
->fname
) != NULL
) {
2334 /* Found a named file. Figure out dir of the file,
2335 and put it in front of the search list. */
2336 dsp
[0].next
= stackp
;
2338 ep
= strrchr (nam
, '/');
2342 f
= (char *) alloca (n
+ 1);
2343 strncpy (f
, nam
, n
);
2346 if (n
> max_include_len
) max_include_len
= n
;
2348 dsp
[0].fname
= 0; /* Current directory */
2359 while (fend
!= limit
&& *fend
!= '>') fend
++;
2360 if (*fend
== '>' && fend
+ 1 == limit
) {
2361 system_header_p
= 1;
2362 /* If -I-, start with the first -I dir after the -I-. */
2363 if (first_bracket_include
)
2364 stackp
= first_bracket_include
;
2372 error ("#include expects \"fname\" or <fname>");
2375 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2376 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2377 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2378 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2386 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2390 do_include_next (buf
, limit
, op
)
2391 U_CHAR
*buf
, *limit
;
2394 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2396 struct file_name_list
*stackp
; /* Chain of dirs to search */
2399 int retried
= 0; /* Have already tried macro
2400 expanding the include line*/
2401 FILE_BUF trybuf
; /* It got expanded into here */
2402 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2404 /* Treat as plain #include if we don't know where to start
2406 stackp
= instack
[indepth
].next_header_dir
;
2409 do_include (buf
, limit
, op
);
2416 SKIP_WHITE_SPACE (fbeg
);
2417 /* Discard trailing whitespace so we can easily see
2418 if we have parsed all the significant chars we were given. */
2419 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2424 while (fend
!= limit
&& *fend
!= '\"')
2426 if (*fend
== '\"' && fend
+ 1 == limit
)
2432 while (fend
!= limit
&& *fend
!= '>') fend
++;
2433 if (*fend
== '>' && fend
+ 1 == limit
) {
2434 system_header_p
= 1;
2442 error ("#include expects \"fname\" or <fname>");
2445 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2446 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2447 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2448 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2456 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2460 process_include (stackp
, fbeg
, flen
, system_header_p
, op
)
2461 struct file_name_list
*stackp
;
2464 int system_header_p
;
2468 int f
= -1; /* file number */
2470 fname
= (char *) alloca (max_include_len
+ flen
+ 2);
2471 /* + 2 above for slash and terminating null. */
2473 /* If specified file name is absolute, just open it. */
2475 if (IS_ABSOLUTE_PATHNAME (fbeg
)) {
2476 strncpy (fname
, (const char *)fbeg
, flen
);
2478 f
= open (fname
, O_RDONLY
, 0666);
2480 /* Search directory path, trying to open the file.
2481 Copy each filename tried into FNAME. */
2483 for (; stackp
; stackp
= stackp
->next
) {
2484 if (stackp
->fname
) {
2485 strcpy (fname
, stackp
->fname
);
2486 strcat (fname
, "/");
2487 fname
[strlen (fname
) + flen
] = 0;
2491 strncat (fname
, (const char *)fbeg
, flen
);
2492 if ((f
= open (fname
, O_RDONLY
, 0666)) >= 0)
2498 strncpy (fname
, (const char *)fbeg
, flen
);
2500 if (deps_missing_files
2501 && print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2503 /* If requested as a system header, assume it belongs in
2504 the first system header directory. */
2505 if (first_bracket_include
)
2506 stackp
= first_bracket_include
;
2510 if (!system_header_p
|| IS_ABSOLUTE_PATHNAME (fbeg
) || !stackp
->fname
)
2511 deps_add_dep (deps
, fname
);
2514 int len
= strlen(stackp
->fname
);
2516 p
= (char *) alloca (len
+ flen
+ 2);
2517 memcpy (p
, stackp
->fname
, len
);
2519 memcpy (p
+ len
, fbeg
, flen
);
2522 deps_add_dep (deps
, p
);
2524 } else if (print_deps
2525 && print_deps
<= (system_header_p
2526 || (system_include_depth
> 0)))
2527 warning ("No include path in which to find %.*s", flen
, fbeg
);
2529 error_from_errno (fname
);
2533 /* Check to see if this include file is a once-only include file.
2536 struct file_name_list
* ptr
;
2538 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
2539 if (!strcmp (ptr
->fname
, fname
)) {
2541 return; /* This file was once'd. */
2545 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
2546 if (!strcmp (ptr
->fname
, fname
))
2547 break; /* This file was included before. */
2551 /* This is the first time for this file. */
2552 /* Add it to list of files included. */
2554 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
2555 ptr
->next
= all_include_files
;
2556 all_include_files
= ptr
;
2557 ptr
->fname
= xstrdup (fname
);
2559 /* For -M, add this file to the dependencies. */
2560 if (print_deps
> (system_header_p
|| (system_include_depth
> 0)))
2561 deps_add_dep (deps
, fname
);
2564 if (system_header_p
)
2565 system_include_depth
++;
2567 /* Actually process the file. */
2568 finclude (f
, fname
, stackp
->next
, op
);
2570 if (system_header_p
)
2571 system_include_depth
--;
2577 /* Process the contents of include file FNAME, already open on descriptor F,
2578 with output to OP. */
2581 finclude (f
, fname
, nhd
, op
)
2584 struct file_name_list
*nhd
;
2590 FILE_BUF
*fp
; /* For input stack frame */
2592 CHECK_DEPTH (return;);
2594 if (file_size_and_mode (f
, &st_mode
, &st_size
))
2597 fp
= &instack
[indepth
+ 1];
2598 memset (fp
, 0, sizeof (FILE_BUF
));
2602 fp
->if_stack
= if_stack
;
2603 fp
->next_header_dir
= nhd
;
2605 if (S_ISREG (st_mode
)) {
2606 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
2609 /* Read the file contents, knowing that st_size is an upper bound
2610 on the number of bytes we can read. */
2611 while (st_size
> 0) {
2612 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
2622 /* Cannot count its file size before reading. */
2629 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
2633 i
= read (f
, bufp
, bsize
- st_size
);
2635 goto nope
; /* error! */
2637 break; /* End of file */
2640 if (bsize
== st_size
) { /* Buffer is full! */
2642 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
2643 bufp
= basep
+ st_size
; /* May have moved */
2648 fp
->length
= st_size
;
2652 /* Make sure data ends with a newline. And put a null after it. */
2654 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
2655 fp
->buf
[fp
->length
++] = '\n';
2656 fp
->buf
[fp
->length
] = '\0';
2659 output_line_command (fp
, op
, 0, enter_file
);
2662 instack
[indepth
].lineno
++;
2663 instack
[indepth
].bufp
++; /* Skip the new line. */
2664 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
2669 perror_with_name (fname
);
2674 /* Process a #define command.
2675 BUF points to the contents of the #define command, as a continguous string.
2676 LIMIT points to the first character past the end of the definition.
2677 KEYWORD is the keyword-table entry for #define. */
2680 do_define (buf
, limit
, op
)
2681 U_CHAR
*buf
, *limit
;
2682 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2684 U_CHAR
*bp
; /* temp ptr into input buffer */
2685 U_CHAR
*symname
; /* remember where symbol name starts */
2686 int sym_length
; /* and how long it is */
2689 int arglengths
= 0; /* Accumulate lengths of arg names
2690 plus number of args. */
2695 while (is_nvspace (*bp
))
2698 symname
= bp
; /* remember where it starts */
2699 while (is_idchar (*bp
) && bp
< limit
) {
2702 sym_length
= bp
- symname
;
2703 if (sym_length
== 0)
2705 error ("invalid macro name");
2708 else if (!is_idstart (*symname
)) {
2709 U_CHAR
*msg
; /* what pain... */
2710 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
2711 memcpy (msg
, symname
, sym_length
);
2712 msg
[sym_length
] = 0;
2713 error ("invalid macro name `%s'", msg
);
2716 if (! strncmp ((const char *)symname
, "defined", 7) && sym_length
== 7)
2718 error ("\"defined\" cannot be used as a macro name");
2723 /* lossage will occur if identifiers or control keywords are broken
2724 across lines using backslash. This is not the right place to take
2728 struct arglist
*arg_ptrs
= NULL
;
2731 bp
++; /* skip '(' */
2732 SKIP_WHITE_SPACE (bp
);
2734 /* Loop over macro argument names. */
2735 while (*bp
!= ')') {
2736 struct arglist
*temp
;
2738 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
2740 temp
->next
= arg_ptrs
;
2741 temp
->argno
= argno
++;
2744 if (!is_idstart (*bp
))
2745 warning ("parameter name starts with a digit in #define");
2747 /* Find the end of the arg name. */
2748 while (is_idchar (*bp
)) {
2751 temp
->length
= bp
- temp
->name
;
2752 arglengths
+= temp
->length
+ 2;
2753 SKIP_WHITE_SPACE (bp
);
2754 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
2755 error ("badly punctuated parameter list in #define");
2760 SKIP_WHITE_SPACE (bp
);
2763 error ("unterminated parameter list in #define");
2768 ++bp
; /* skip paren */
2769 while (is_nvspace (*bp
) && bp
< limit
) /* and leading whitespace */
2771 /* now everything from bp before limit is the definition. */
2772 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
2774 /* Now set defn->argnames to the result of concatenating
2775 the argument names in reverse order
2776 with comma-space between them. */
2778 struct arglist
*temp
;
2780 U_CHAR
*tmp
= (U_CHAR
*) xmalloc (arglengths
+ 1);
2782 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
2783 memcpy (&tmp
[i
], temp
->name
, temp
->length
);
2785 if (temp
->next
!= 0) {
2791 defn
->argnames
= tmp
;
2795 /* simple expansion or empty definition; skip leading whitespace */
2796 while (is_nvspace (*bp
) && bp
< limit
)
2798 /* now everything from bp before limit is the definition. */
2799 defn
= collect_expansion (bp
, limit
, -1, 0);
2800 defn
->argnames
= (const U_CHAR
*) "";
2803 hashcode
= hashf (symname
, sym_length
, HASHSIZE
);
2807 if ((hp
= lookup (symname
, sym_length
, hashcode
)) == NULL
)
2808 hp
= install (symname
, sym_length
, T_MACRO
, hashcode
);
2810 if (hp
->type
!= T_MACRO
|| compare_defs (defn
, hp
->value
.defn
))
2811 warning ("\"%.*s\" redefined", sym_length
, symname
);
2813 /* Replace the old definition. */
2817 hp
->value
.defn
= defn
;
2822 * return zero if two DEFINITIONs are isomorphic
2825 compare_defs (d1
, d2
)
2826 DEFINITION
*d1
, *d2
;
2828 register struct reflist
*a1
, *a2
;
2829 register U_CHAR
*p1
= d1
->expansion
;
2830 register U_CHAR
*p2
= d2
->expansion
;
2833 if (d1
->nargs
!= d2
->nargs
)
2835 if (strcmp ((const char *)d1
->argnames
, (const char *)d2
->argnames
))
2837 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
2838 a1
= a1
->next
, a2
= a2
->next
) {
2839 if (!((a1
->nchars
== a2
->nchars
2840 && ! strncmp ((const char *)p1
, (const char *)p2
, a1
->nchars
))
2841 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
2842 || a1
->argno
!= a2
->argno
2843 || a1
->stringify
!= a2
->stringify
2844 || a1
->raw_before
!= a2
->raw_before
2845 || a1
->raw_after
!= a2
->raw_after
)
2853 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
2854 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
2859 /* Return 1 if two parts of two macro definitions are effectively different.
2860 One of the parts starts at BEG1 and has LEN1 chars;
2861 the other has LEN2 chars at BEG2.
2862 Any sequence of whitespace matches any other sequence of whitespace.
2863 FIRST means these parts are the first of a macro definition;
2864 so ignore leading whitespace entirely.
2865 LAST means these parts are the last of a macro definition;
2866 so ignore trailing whitespace entirely. */
2868 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
2870 const U_CHAR
*beg1
, *beg2
;
2874 register const U_CHAR
*end1
= beg1
+ len1
;
2875 register const U_CHAR
*end2
= beg2
+ len2
;
2877 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2878 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2881 while (beg1
!= end1
&& is_space (end1
[-1])) end1
--;
2882 while (beg2
!= end2
&& is_space (end2
[-1])) end2
--;
2884 while (beg1
!= end1
&& beg2
!= end2
) {
2885 if (is_space (*beg1
) && is_space (*beg2
)) {
2886 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2887 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2888 } else if (*beg1
== *beg2
) {
2892 return (beg1
!= end1
) || (beg2
!= end2
);
2895 /* Read a replacement list for a macro with parameters.
2896 Build the DEFINITION structure.
2897 Reads characters of text starting at BUF until LIMIT.
2898 ARGLIST specifies the formal parameters to look for
2899 in the text of the definition; NARGS is the number of args
2900 in that list, or -1 for a macro name that wants no argument list.
2901 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2902 and NAMELEN is its length in characters.
2904 Note that comments and backslash-newlines have already been deleted
2905 from the argument. */
2907 /* Leading and trailing Space, Tab, etc. are converted to markers
2908 Newline Space, Newline Tab, etc.
2909 Newline Space makes a space in the final output
2910 but is discarded if stringified. (Newline Tab is similar but
2911 makes a Tab instead.)
2913 If there is no trailing whitespace, a Newline Space is added at the end
2914 to prevent concatenation that would be contrary to the standard. */
2917 collect_expansion (buf
, end
, nargs
, arglist
)
2920 struct arglist
*arglist
;
2923 register U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
2924 struct reflist
*endpat
= NULL
;
2925 /* Pointer to first nonspace after last ## seen. */
2927 /* Pointer to first nonspace after last single-# seen. */
2928 U_CHAR
*stringify
= 0;
2930 int expected_delimiter
= '\0';
2932 /* Scan thru the replacement list, ignoring comments and quoted
2933 strings, picking up on the macro calls. It does a linear search
2934 thru the arg list on every potential symbol. Profiling might say
2935 that something smarter should happen. */
2940 /* Find the beginning of the trailing whitespace. */
2941 /* Find end of leading whitespace. */
2944 while (p
< limit
&& is_space (limit
[-1])) limit
--;
2945 while (p
< limit
&& is_space (*p
)) p
++;
2947 /* Allocate space for the text in the macro definition.
2948 Leading and trailing whitespace chars need 2 bytes each.
2949 Each other input char may or may not need 1 byte,
2950 so this is an upper bound.
2951 The extra 2 are for invented trailing newline-marker and final null. */
2952 maxsize
= (sizeof (DEFINITION
)
2953 + 2 * (end
- limit
) + 2 * (p
- buf
)
2955 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
2957 defn
->nargs
= nargs
;
2958 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
2963 /* Convert leading whitespace to Newline-markers. */
2964 while (p
< limit
&& is_space (*p
)) {
2969 /* Process the main body of the definition. */
2971 int skipped_arg
= 0;
2972 register U_CHAR c
= *p
++;
2976 /* In -traditional mode, recognize arguments inside strings and
2977 and character constants, and ignore special properties of #.
2978 Arguments inside strings are considered "stringified", but no
2979 extra quote marks are supplied. */
2983 if (expected_delimiter
!= '\0') {
2984 if (c
== expected_delimiter
)
2985 expected_delimiter
= '\0';
2987 expected_delimiter
= c
;
2991 /* Backslash quotes delimiters and itself, but not macro args. */
2992 if (expected_delimiter
!= 0 && p
< limit
2993 && (*p
== expected_delimiter
|| *p
== '\\')) {
3000 if (expected_delimiter
!= '\0') /* No comments inside strings. */
3003 /* If we find a comment that wasn't removed by handle_directive,
3004 this must be -traditional. So replace the comment with
3008 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
3014 if (is_idchar (c
) && nargs
> 0) {
3015 U_CHAR
*id_beg
= p
- 1;
3019 while (p
!= limit
&& is_idchar (*p
)) p
++;
3020 id_len
= p
- id_beg
;
3022 if (is_idstart (c
)) {
3023 register struct arglist
*arg
;
3025 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
3026 struct reflist
*tpat
;
3028 if (arg
->name
[0] == c
3029 && arg
->length
== id_len
3030 && strncmp ((const char *)arg
->name
,
3031 (const char *)id_beg
, id_len
) == 0) {
3032 /* make a pat node for this arg and append it to the end of
3034 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
3036 tpat
->raw_before
= concat
== id_beg
;
3037 tpat
->raw_after
= 0;
3038 tpat
->stringify
= expected_delimiter
!= '\0';
3041 defn
->pattern
= tpat
;
3043 endpat
->next
= tpat
;
3046 tpat
->argno
= arg
->argno
;
3047 tpat
->nchars
= exp_p
- lastp
;
3049 register U_CHAR
*p1
= p
;
3050 SKIP_WHITE_SPACE (p1
);
3051 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
3052 tpat
->raw_after
= 1;
3054 lastp
= exp_p
; /* place to start copying from next time */
3061 /* If this was not a macro arg, copy it into the expansion. */
3062 if (! skipped_arg
) {
3063 register U_CHAR
*lim1
= p
;
3067 if (stringify
== id_beg
)
3068 error ("# operator should be followed by a macro argument name");
3074 /* Convert trailing whitespace to Newline-markers. */
3075 while (limit
< end
&& is_space (*limit
)) {
3077 *exp_p
++ = *limit
++;
3082 defn
->length
= exp_p
- defn
->expansion
;
3084 /* Crash now if we overrun the allocated size. */
3085 if (defn
->length
+ 1 > maxsize
)
3092 * interpret #line command. Remembers previously seen fnames
3093 * in its very own hash table.
3095 #define FNAME_HASHSIZE 37
3097 do_line (buf
, limit
, op
)
3098 U_CHAR
*buf
, *limit
;
3101 register U_CHAR
*bp
;
3102 FILE_BUF
*ip
= &instack
[indepth
];
3105 enum file_change_code file_change
= same_file
;
3107 /* Expand any macros. */
3108 tem
= expand_to_temp_buffer (buf
, limit
, 0);
3110 /* Point to macroexpanded line, which is null-terminated now. */
3112 SKIP_WHITE_SPACE (bp
);
3114 if (!ISDIGIT (*bp
)) {
3115 error ("invalid format #line command");
3119 /* The Newline at the end of this line remains to be processed.
3120 To put the next line at the specified line number,
3121 we must store a line number now that is one less. */
3122 new_lineno
= atoi ((const char *)bp
);
3124 /* skip over the line number. */
3125 while (ISDIGIT (*bp
))
3128 SKIP_WHITE_SPACE (bp
);
3131 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3132 HASHNODE
*hp
, **hash_bucket
;
3138 while (*bp
&& *bp
!= '\"')
3141 error ("invalid format #line command");
3145 fname_length
= bp
- fname
;
3148 SKIP_WHITE_SPACE (bp
);
3151 file_change
= enter_file
;
3152 else if (*bp
== '2')
3153 file_change
= leave_file
;
3155 error ("invalid format #line command");
3160 SKIP_WHITE_SPACE (bp
);
3162 error ("invalid format #line command");
3168 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3169 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3170 if (hp
->length
== fname_length
&&
3171 strncmp (hp
->value
.cpval
, (const char *)fname
, fname_length
) == 0) {
3172 ip
->fname
= hp
->value
.cpval
;
3177 /* Didn't find it; cons up a new one. */
3178 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3179 hp
->next
= *hash_bucket
;
3182 hp
->length
= fname_length
;
3183 ip
->fname
= hp
->value
.cpval
= q
= ((char *) hp
) + sizeof (HASHNODE
);
3184 memcpy (q
, fname
, fname_length
);
3187 error ("invalid format #line command");
3191 ip
->lineno
= new_lineno
;
3192 output_line_command (ip
, op
, 0, file_change
);
3193 ip
->bufp
++; /* Skip the new line. */
3194 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3198 * remove all definitions of symbol from symbol table.
3199 * according to un*x /lib/cpp, it is not an error to undef
3200 * something that has no definitions, so it isn't one here either.
3203 do_undef (buf
, limit
, op
)
3205 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3206 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3210 SKIP_WHITE_SPACE (buf
);
3212 if (! strncmp ((const char *)buf
, "defined", 7) && ! is_idchar (buf
[7]))
3213 warning ("undefining `defined'");
3215 while ((hp
= lookup (buf
, -1, -1)) != NULL
) {
3216 if (hp
->type
!= T_MACRO
)
3217 warning ("undefining `%s'", hp
->name
);
3222 /* Read the tokens of the answer into the macro pool. Only commit the
3223 memory if we intend it as permanent storage, i.e. the #assert case.
3224 Returns 0 on success. */
3227 parse_answer (buf
, limit
, answerp
, type
)
3228 const unsigned char *buf
, *limit
;
3229 struct answer
**answerp
;
3232 const unsigned char *start
;
3234 /* Skip leading whitespace. */
3235 if (buf
< limit
&& *buf
== ' ')
3238 /* Parentheses are optional here. */
3239 if (buf
== limit
&& type
== T_UNASSERT
)
3242 if (buf
== limit
|| *buf
++ != '(')
3247 error ("missing '(' after predicate");
3251 /* Drop whitespace at start. */
3252 while (buf
< limit
&& *buf
== ' ')
3256 while (buf
< limit
&& *buf
!= ')')
3261 error ("missing ')' to complete answer");
3267 error ("predicate's answer is empty");
3271 if ((type
== T_ASSERT
|| type
== T_UNASSERT
) && buf
+ 1 != limit
)
3273 error ("extra text at end of directive");
3277 /* Lose trailing whitespace. */
3281 *answerp
= (struct answer
*) xmalloc (sizeof (struct answer
));
3282 (*answerp
)->answer
= start
;
3283 (*answerp
)->len
= buf
- start
;
3288 /* Parses an assertion, returning a pointer to the hash node of the
3289 predicate, or 0 on error. If an answer was supplied, it is placed
3290 in ANSWERP, otherwise it is set to 0. */
3292 parse_assertion (buf
, limit
, answerp
, type
)
3293 const unsigned char *buf
, *limit
;
3294 struct answer
**answerp
;
3297 HASHNODE
*result
= 0;
3298 const unsigned char *climit
;
3299 unsigned char *bp
, *symname
= canonicalize_text (buf
, limit
, &climit
);
3303 if (bp
< climit
&& is_idstart (*bp
))
3307 while (bp
< climit
&& is_idchar (*bp
));
3314 if (symname
== climit
)
3315 error ("assertion without predicate");
3317 error ("predicate must be an identifier");
3319 /* Unfortunately, because of the way we handle #if, we don't avoid
3320 macro expansion in answers. This is not easy to fix. */
3321 else if (parse_answer (bp
, climit
, answerp
, type
) == 0)
3323 unsigned char *sym
= alloca (len
+ 1);
3326 /* Prefix '#' to get it out of macro namespace. */
3328 memcpy (sym
+ 1, symname
, len
);
3330 hashcode
= hashf (sym
, len
+ 1, HASHSIZE
);
3331 result
= lookup (sym
, len
+ 1, hashcode
);
3333 result
= install (sym
, len
+ 1, T_UNUSED
, hashcode
);
3339 /* Test an assertion within a preprocessor conditional. Returns zero
3340 on error or failure, one on success. */
3342 test_assertion (pbuf
)
3343 unsigned char **pbuf
; /* NUL-terminated. */
3345 unsigned char *buf
= *pbuf
;
3346 unsigned char *limit
= buf
+ strlen ((char *) buf
);
3347 struct answer
*answer
;
3351 node
= parse_assertion (buf
, limit
, &answer
, T_IF
);
3354 result
= (node
->type
== T_ASSERT
&&
3355 (answer
== 0 || *find_answer (node
, answer
) != 0));
3357 /* Yuk. We update pbuf to point after the assertion test.
3358 First, move past the identifier. */
3359 if (is_space (*buf
))
3361 while (is_idchar (*buf
))
3363 /* If we have an answer, we need to move past the parentheses. */
3365 while (*buf
++ != ')')
3373 /* Handle a #error directive. */
3375 do_error (buf
, limit
, op
)
3378 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3380 error ("#error%.*s", (int) (limit
- buf
), buf
);
3383 /* Handle a #warning directive. */
3385 do_warning (buf
, limit
, op
)
3388 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3390 warning ("#warning%.*s", (int) (limit
- buf
), buf
);
3393 /* Handle a #assert directive. */
3395 do_assert (buf
, limit
, op
)
3398 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3400 struct answer
*new_answer
;
3403 node
= parse_assertion (buf
, limit
, &new_answer
, T_ASSERT
);
3406 /* Place the new answer in the answer list. First check there
3407 is not a duplicate. */
3408 new_answer
->next
= 0;
3409 if (node
->type
== T_ASSERT
)
3411 if (*find_answer (node
, new_answer
))
3414 warning ("\"%s\" re-asserted", node
->name
+ 1);
3417 new_answer
->next
= node
->value
.answers
;
3419 node
->type
= T_ASSERT
;
3420 node
->value
.answers
= new_answer
;
3424 /* Function body to be provided later. */
3426 do_unassert (buf
, limit
, op
)
3429 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3432 struct answer
*answer
;
3434 node
= parse_assertion (buf
, limit
, &answer
, T_UNASSERT
);
3435 /* It isn't an error to #unassert something that isn't asserted. */
3438 if (node
->type
== T_ASSERT
)
3442 struct answer
**p
= find_answer (node
, answer
), *temp
;
3444 /* Remove the answer from the list. */
3449 /* Did we free the last answer? */
3450 if (node
->value
.answers
== 0)
3451 delete_macro (node
);
3454 delete_macro (node
);
3461 /* Returns a pointer to the pointer to the answer in the answer chain,
3462 or a pointer to NULL if the answer is not in the chain. */
3463 static struct answer
**
3464 find_answer (node
, candidate
)
3466 const struct answer
*candidate
;
3468 struct answer
**result
;
3470 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
3472 struct answer
*answer
= *result
;
3474 if (answer
->len
== candidate
->len
3475 && !memcmp (answer
->answer
, candidate
->answer
, answer
->len
))
3482 /* Return a malloced buffer with leading and trailing whitespace
3483 removed, and all instances of internal whitespace reduced to a
3485 static unsigned char *
3486 canonicalize_text (buf
, limit
, climit
)
3487 const unsigned char *buf
, *limit
, **climit
;
3489 unsigned int len
= limit
- buf
;
3490 unsigned char *result
= (unsigned char *) xmalloc (len
), *dest
;
3492 for (dest
= result
; buf
< limit
;)
3494 if (! is_space (*buf
))
3498 while (++buf
< limit
&& is_space (*buf
))
3500 if (dest
!= result
&& buf
!= limit
)
3510 * handle #if command by
3511 * 1) inserting special `defined' keyword into the hash table
3512 * that gets turned into 0 or 1 by special_symbol (thus,
3513 * if the luser has a symbol called `defined' already, it won't
3514 * work inside the #if command)
3515 * 2) rescan the input into a temporary output buffer
3516 * 3) pass the output buffer to the yacc parser and collect a value
3517 * 4) clean up the mess left from steps 1 and 2.
3518 * 5) call conditional_skip to skip til the next #endif (etc.),
3519 * or not, depending on the value from step 3.
3522 do_if (buf
, limit
, op
)
3523 U_CHAR
*buf
, *limit
;
3524 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3527 FILE_BUF
*ip
= &instack
[indepth
];
3529 value
= eval_if_expression (buf
, limit
- buf
);
3530 conditional_skip (ip
, value
== 0, T_IF
);
3534 * handle a #elif directive by not changing if_stack either.
3535 * see the comment above do_else.
3538 do_elif (buf
, limit
, op
)
3539 U_CHAR
*buf
, *limit
;
3543 FILE_BUF
*ip
= &instack
[indepth
];
3545 if (if_stack
== instack
[indepth
].if_stack
) {
3546 error ("#elif not within a conditional");
3549 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3550 error ("#elif after #else");
3551 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3552 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
3553 strcmp (if_stack
->fname
, ip
->fname
) != 0)
3554 fprintf (stderr
, ", file %s", if_stack
->fname
);
3555 fprintf (stderr
, ")\n");
3557 if_stack
->type
= T_ELIF
;
3560 if (if_stack
->if_succeeded
)
3561 skip_if_group (ip
, 0);
3563 value
= eval_if_expression (buf
, limit
- buf
);
3565 skip_if_group (ip
, 0);
3567 ++if_stack
->if_succeeded
; /* continue processing input */
3568 output_line_command (ip
, op
, 1, same_file
);
3574 * evaluate a #if expression in BUF, of length LENGTH,
3575 * then parse the result as a C expression and return the value as an int.
3578 eval_if_expression (buf
, length
)
3583 HASHNODE
*save_defined
;
3586 save_defined
= install (U
"defined", -1, T_SPEC_DEFINED
, -1);
3587 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0);
3588 delete_macro (save_defined
); /* clean up special symbol */
3590 value
= parse_c_expression ((const char *)temp_obuf
.buf
);
3592 free (temp_obuf
.buf
);
3598 * routine to handle ifdef/ifndef. Try to look up the symbol,
3599 * then do or don't skip to the #endif/#else/#elif depending
3600 * on what directive is actually being processed.
3603 do_xifdef (buf
, limit
, type
)
3604 U_CHAR
*buf
, *limit
;
3605 enum node_type type
;
3608 FILE_BUF
*ip
= &instack
[indepth
];
3611 /* Discard leading and trailing whitespace. */
3612 SKIP_WHITE_SPACE (buf
);
3613 while (limit
!= buf
&& is_nvspace (limit
[-1])) limit
--;
3615 /* Find the end of the identifier at the beginning. */
3616 for (end
= buf
; is_idchar (*end
); end
++);
3619 skip
= (type
== T_IFDEF
);
3621 skip
= (lookup (buf
, end
-buf
, -1) == NULL
) ^ (type
== T_IFNDEF
);
3623 conditional_skip (ip
, skip
, T_IF
);
3627 do_ifdef (buf
, limit
, op
)
3628 U_CHAR
*buf
, *limit
;
3629 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3631 do_xifdef (buf
, limit
, T_IFDEF
);
3635 do_ifndef (buf
, limit
, op
)
3636 U_CHAR
*buf
, *limit
;
3637 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3639 do_xifdef (buf
, limit
, T_IFNDEF
);
3643 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3646 conditional_skip (ip
, skip
, type
)
3649 enum node_type type
;
3651 IF_STACK_FRAME
*temp
;
3653 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3654 temp
->fname
= ip
->fname
;
3655 temp
->lineno
= ip
->lineno
;
3656 temp
->next
= if_stack
;
3659 if_stack
->type
= type
;
3662 skip_if_group (ip
, 0);
3665 ++if_stack
->if_succeeded
;
3666 output_line_command (ip
, &outbuf
, 1, same_file
);
3671 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3672 * leaves input ptr at the sharp sign found.
3673 * If ANY is nonzero, return at next directive of any sort.
3676 skip_if_group (ip
, any
)
3680 register U_CHAR
*bp
= ip
->bufp
, *cp
;
3681 register U_CHAR
*endb
= ip
->buf
+ ip
->length
;
3682 struct directive
*kt
;
3683 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
3684 U_CHAR
*beg_of_line
= bp
;
3688 case '/': /* possible comment */
3689 if (*bp
== '\\' && bp
[1] == '\n')
3693 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
);
3698 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
3701 /* Char after backslash loses its special meaning. */
3704 ++ip
->lineno
; /* But do update the line-count. */
3715 /* # keyword: a # must be first nonblank char on the line */
3716 if (beg_of_line
== 0)
3718 /* Scan from start of line, skipping whitespace, comments
3719 and backslash-newlines, and see if we reach this #.
3720 If not, this # is not special. */
3723 if (is_nvspace (*bp
))
3725 else if (*bp
== '\\' && bp
[1] == '\n')
3727 else if (*bp
== '/' && bp
[1] == '*') {
3729 while (!(*bp
== '*' && bp
[1] == '/')) {
3738 if (bp
!= ip
->bufp
) {
3739 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
3743 bp
= ip
->bufp
+ 1; /* Point after '#'. */
3745 /* Skip whitespace and \-newline. */
3747 if (is_nvspace (*bp
))
3749 else if (*bp
== '\\' && bp
[1] == '\n')
3751 else if (*bp
== '/' && bp
[1] == '*') {
3753 while (!(*bp
== '*' && bp
[1] == '/'))
3762 /* Now find end of directive name.
3763 If we encounter a backslash-newline, exchange it with any following
3764 symbol-constituents so that we end up with a contiguous name. */
3767 if (is_idchar (*bp
))
3770 if (*bp
== '\\' && bp
[1] == '\n')
3771 name_newline_fix (bp
);
3772 if (is_idchar (*bp
))
3778 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
3779 IF_STACK_FRAME
*temp
;
3780 if (strncmp ((const char *)cp
, kt
->name
, kt
->length
) == 0
3781 && !is_idchar (cp
[kt
->length
])) {
3783 /* If we are asked to return on next directive,
3792 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3793 temp
->next
= if_stack
;
3795 temp
->lineno
= ip
->lineno
;
3796 temp
->fname
= ip
->fname
;
3797 temp
->type
= kt
->type
;
3802 if (if_stack
== instack
[indepth
].if_stack
) {
3803 error ("#%s not within a conditional", kt
->name
);
3806 else if (if_stack
== save_if_stack
)
3807 return; /* found what we came for */
3809 if (kt
->type
!= T_ENDIF
) {
3810 if (if_stack
->type
== T_ELSE
)
3811 error ("#else or #elif after #else");
3812 if_stack
->type
= kt
->type
;
3817 if_stack
= if_stack
->next
;
3822 /* Anything else is ignored. */
3831 /* after this returns, rescan will exit because ip->bufp
3832 now points to the end of the buffer.
3833 rescan is responsible for the error message also. */
3837 * handle a #else directive. Do this by just continuing processing
3838 * without changing if_stack ; this is so that the error message
3839 * for missing #endif's etc. will point to the original #if. It
3840 * is possible that something different would be better.
3843 do_else (buf
, limit
, op
)
3844 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3845 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3848 FILE_BUF
*ip
= &instack
[indepth
];
3850 if (if_stack
== instack
[indepth
].if_stack
) {
3851 error ("#else not within a conditional");
3854 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3855 error ("#else after #else");
3856 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3857 if (strcmp (if_stack
->fname
, ip
->fname
) != 0)
3858 fprintf (stderr
, ", file %s", if_stack
->fname
);
3859 fprintf (stderr
, ")\n");
3861 if_stack
->type
= T_ELSE
;
3864 if (if_stack
->if_succeeded
)
3865 skip_if_group (ip
, 0);
3867 ++if_stack
->if_succeeded
; /* continue processing input */
3868 output_line_command (ip
, op
, 1, same_file
);
3873 * unstack after #endif command
3876 do_endif (buf
, limit
, op
)
3877 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3878 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3881 if (if_stack
== instack
[indepth
].if_stack
)
3882 error ("unbalanced #endif");
3884 IF_STACK_FRAME
*temp
= if_stack
;
3885 if_stack
= if_stack
->next
;
3887 output_line_command (&instack
[indepth
], op
, 1, same_file
);
3892 * Skip a comment, assuming the input ptr immediately follows the
3893 * initial slash-star. Bump line counter as necessary.
3894 * (The canonical line counter is &ip->lineno).
3895 * Don't use this routine (or the next one) if bumping the line
3896 * counter is not sufficient to deal with newlines in the string.
3899 skip_to_end_of_comment (ip
, line_counter
)
3900 register FILE_BUF
*ip
;
3901 int *line_counter
; /* place to remember newlines, or NULL */
3903 register U_CHAR
*limit
= ip
->buf
+ ip
->length
;
3904 register U_CHAR
*bp
= ip
->bufp
;
3905 FILE_BUF
*op
= &outbuf
; /* JF */
3906 int output
= put_out_comments
&& !line_counter
;
3908 /* JF this line_counter stuff is a crock to make sure the
3909 comment is only put out once, no matter how many times
3910 the comment is skipped. It almost works */
3915 while (bp
< limit
) {
3920 if (warn_comments
&& bp
< limit
&& *bp
== '*')
3921 warning("`/*' within comment");
3924 if (line_counter
!= NULL
)
3930 if (*bp
== '\\' && bp
[1] == '\n')
3946 * Skip over a quoted string. BP points to the opening quote.
3947 * Returns a pointer after the closing quote. Don't go past LIMIT.
3948 * START_LINE is the line number of the starting point (but it need
3949 * not be valid if the starting point is inside a macro expansion).
3951 * The input stack state is not changed.
3953 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3954 * for each newline passed.
3956 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3957 * if we pass a backslash-newline.
3959 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3962 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
3963 register const U_CHAR
*bp
;
3964 register const U_CHAR
*limit
;
3966 int *count_newlines
;
3967 int *backslash_newlines_p
;
3970 register U_CHAR c
, match
;
3975 error_with_line (line_for_error (start_line
),
3976 "unterminated string or character constant");
3983 while (*bp
== '\\' && bp
[1] == '\n') {
3984 if (backslash_newlines_p
)
3985 *backslash_newlines_p
= 1;
3990 if (*bp
== '\n' && count_newlines
) {
3991 if (backslash_newlines_p
)
3992 *backslash_newlines_p
= 1;
3996 } else if (c
== '\n') {
3997 /* Unterminated strings and character constants are 'legal'. */
3998 bp
--; /* Don't consume the newline. */
4002 } else if (c
== match
)
4005 return (U_CHAR
*) bp
;
4009 * write out a #line command, for instance, after an #include file.
4010 * If CONDITIONAL is nonzero, we can omit the #line if it would
4011 * appear to be a no-op, and we can output a few newlines instead
4012 * if we want to increase the line number by a small amount.
4013 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
4017 output_line_command (ip
, op
, conditional
, file_change
)
4020 enum file_change_code file_change
;
4023 char line_cmd_buf
[500];
4025 if (no_line_commands
4026 || ip
->fname
== NULL
4028 op
->lineno
= ip
->lineno
;
4033 if (ip
->lineno
== op
->lineno
)
4036 /* If the inherited line number is a little too small,
4037 output some newlines instead of a #line command. */
4038 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
4039 check_expand (op
, 10);
4040 while (ip
->lineno
> op
->lineno
) {
4048 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->fname
);
4049 if (file_change
!= same_file
)
4050 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
4051 if (system_include_depth
> 0)
4052 strcat (line_cmd_buf
, " 3");
4053 len
= strlen (line_cmd_buf
);
4054 line_cmd_buf
[len
++] = '\n';
4055 check_expand (op
, len
+ 1);
4056 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
4058 memcpy (op
->bufp
, line_cmd_buf
, len
);
4060 op
->lineno
= ip
->lineno
;
4064 /* Expand a macro call.
4065 HP points to the symbol that is the macro being called.
4066 Put the result of expansion onto the input stack
4067 so that subsequent input by our caller will use it.
4069 If macro wants arguments, caller has already verified that
4070 an argument list follows; arguments come from the input stack. */
4073 macroexpand (hp
, op
)
4078 DEFINITION
*defn
= hp
->value
.defn
;
4079 register U_CHAR
*xbuf
;
4081 int start_line
= instack
[indepth
].lineno
;
4083 CHECK_DEPTH (return;);
4085 /* it might not actually be a macro. */
4086 if (hp
->type
!= T_MACRO
) {
4087 special_symbol (hp
, op
);
4091 nargs
= defn
->nargs
;
4095 struct argdata
*args
;
4096 const char *parse_error
= 0;
4098 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
4100 for (i
= 0; i
< nargs
; i
++) {
4101 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
4102 args
[i
].raw_length
= args
[i
].expand_length
4103 = args
[i
].stringified_length
= 0;
4104 args
[i
].free1
= args
[i
].free2
= 0;
4107 /* Parse all the macro args that are supplied. I counts them.
4108 The first NARGS args are stored in ARGS.
4109 The rest are discarded. */
4112 /* Discard the open-parenthesis or comma before the next arg. */
4113 ++instack
[indepth
].bufp
;
4115 = macarg ((i
< nargs
|| (nargs
== 0 && i
== 0)) ? &args
[i
] : 0);
4118 error_with_line (line_for_error (start_line
), "%s", parse_error
);
4122 } while (*instack
[indepth
].bufp
!= ')');
4124 /* If we got one arg but it was just whitespace, call that 0 args. */
4126 register const U_CHAR
*bp
= args
[0].raw
;
4127 register const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
4128 while (bp
!= lim
&& is_space (*bp
)) bp
++;
4133 if (nargs
== 0 && i
> 0)
4134 error ("arguments given to macro `%s'", hp
->name
);
4135 else if (i
< nargs
) {
4136 /* traditional C allows foo() if foo wants one argument. */
4137 if (nargs
== 1 && i
== 0)
4140 error ("no args to macro `%s'", hp
->name
);
4142 error ("only 1 arg to macro `%s'", hp
->name
);
4144 error ("only %d args to macro `%s'", i
, hp
->name
);
4145 } else if (i
> nargs
)
4146 error ("too many (%d) args to macro `%s'", i
, hp
->name
);
4148 /* Swallow the closeparen. */
4149 ++instack
[indepth
].bufp
;
4151 /* If macro wants zero args, we parsed the arglist for checking only.
4152 Read directly from the macro definition. */
4154 xbuf
= defn
->expansion
;
4155 xbuf_len
= defn
->length
;
4157 register U_CHAR
*exp
= defn
->expansion
;
4158 register int offset
; /* offset in expansion,
4159 copied a piece at a time */
4160 register int totlen
; /* total amount of exp buffer filled so far */
4162 register struct reflist
*ap
;
4164 /* Macro really takes args. Compute the expansion of this call. */
4166 /* Compute length in characters of the macro's expansion. */
4167 xbuf_len
= defn
->length
;
4168 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4170 xbuf_len
+= args
[ap
->argno
].stringified_length
;
4172 xbuf_len
+= args
[ap
->argno
].raw_length
;
4175 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
4177 /* Generate in XBUF the complete expansion
4178 with arguments substituted in.
4179 TOTLEN is the total size generated so far.
4180 OFFSET is the index in the definition
4181 of where we are copying from. */
4182 offset
= totlen
= 0;
4183 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4184 register struct argdata
*arg
= &args
[ap
->argno
];
4186 for (i
= 0; i
< ap
->nchars
; i
++)
4187 xbuf
[totlen
++] = exp
[offset
++];
4189 if (ap
->stringify
!= 0) {
4190 int arglen
= arg
->raw_length
;
4196 && (c
= arg
->raw
[i
], is_space (c
)))
4199 && (c
= arg
->raw
[arglen
- 1], is_space (c
)))
4201 for (; i
< arglen
; i
++) {
4204 /* Special markers Newline Space
4205 generate nothing for a stringified argument. */
4206 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
4211 /* Internal sequences of whitespace are replaced by one space
4212 except within an string or char token. */
4214 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space (c
))) {
4216 /* Note that Newline Space does occur within whitespace
4217 sequences; consider it part of the sequence. */
4218 if (c
== '\n' && is_space (arg
->raw
[i
+1]))
4220 else if (c
!= '\n' && is_space (c
))
4237 } else if (c
== '\"' || c
== '\'')
4241 /* Escape these chars */
4242 if (c
== '\"' || (in_string
&& c
== '\\'))
4243 xbuf
[totlen
++] = '\\';
4247 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
4252 const U_CHAR
*p1
= arg
->raw
;
4253 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
4255 if (ap
->raw_before
) {
4256 while (p1
!= l1
&& is_space (*p1
)) p1
++;
4257 while (p1
!= l1
&& is_idchar (*p1
))
4258 xbuf
[totlen
++] = *p1
++;
4259 /* Delete any no-reexpansion marker that follows
4260 an identifier at the beginning of the argument
4261 if the argument is concatenated with what precedes it. */
4262 if (p1
[0] == '\n' && p1
[1] == '-')
4265 if (ap
->raw_after
) {
4266 /* Arg is concatenated after: delete trailing whitespace,
4267 whitespace markers, and no-reexpansion markers. */
4269 if (is_space (l1
[-1])) l1
--;
4270 else if (l1
[-1] == '-') {
4271 const U_CHAR
*p2
= l1
- 1;
4272 /* If a `-' is preceded by an odd number of newlines then it
4273 and the last newline are a no-reexpansion marker. */
4274 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
4275 if ((l1
- 1 - p2
) & 1) {
4283 memmove (xbuf
+ totlen
, p1
, l1
- p1
);
4287 if (totlen
> xbuf_len
)
4291 /* if there is anything left of the definition
4292 after handling the arg list, copy that in too. */
4294 for (i
= offset
; i
< defn
->length
; i
++)
4295 xbuf
[totlen
++] = exp
[i
];
4300 for (i
= 0; i
< nargs
; i
++) {
4301 if (args
[i
].free1
!= 0)
4302 free (args
[i
].free1
);
4303 if (args
[i
].free2
!= 0)
4304 free (args
[i
].free2
);
4308 xbuf
= defn
->expansion
;
4309 xbuf_len
= defn
->length
;
4312 /* Now put the expansion on the input stack
4313 so our caller will commence reading from it. */
4315 register FILE_BUF
*ip2
;
4317 ip2
= &instack
[++indepth
];
4322 ip2
->length
= xbuf_len
;
4324 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
4326 ip2
->if_stack
= if_stack
;
4331 * Parse a macro argument and store the info on it into *ARGPTR.
4332 * Return nonzero to indicate a syntax error.
4337 register struct argdata
*argptr
;
4339 FILE_BUF
*ip
= &instack
[indepth
];
4344 /* Try to parse as much of the argument as exists at this
4345 input stack level. */
4346 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
4347 &paren
, &newlines
, &comments
);
4349 /* If we find the end of the argument at this level,
4350 set up *ARGPTR to point at it in the input stack. */
4351 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
4352 && bp
!= ip
->buf
+ ip
->length
) {
4354 argptr
->raw
= ip
->bufp
;
4355 argptr
->raw_length
= bp
- ip
->bufp
;
4359 /* This input stack level ends before the macro argument does.
4360 We must pop levels and keep parsing.
4361 Therefore, we must allocate a temporary buffer and copy
4362 the macro argument into it. */
4363 int bufsize
= bp
- ip
->bufp
;
4364 int extra
= newlines
;
4365 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
4366 int final_start
= 0;
4368 memcpy (buffer
, ip
->bufp
, bufsize
);
4370 ip
->lineno
+= newlines
;
4372 while (bp
== ip
->buf
+ ip
->length
) {
4373 if (instack
[indepth
].macro
== 0) {
4375 return "unterminated macro call";
4377 ip
->macro
->type
= T_MACRO
;
4379 free (ip
->free_ptr
);
4380 ip
= &instack
[--indepth
];
4383 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
4384 &newlines
, &comments
);
4385 final_start
= bufsize
;
4386 bufsize
+= bp
- ip
->bufp
;
4388 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
4389 memcpy (buffer
+ bufsize
- (bp
- ip
->bufp
), ip
->bufp
, bp
- ip
->bufp
);
4391 ip
->lineno
+= newlines
;
4394 /* Now, if arg is actually wanted, record its raw form,
4395 discarding comments and duplicating newlines in whatever
4396 part of it did not come from a macro expansion.
4397 EXTRA space has been preallocated for duplicating the newlines.
4398 FINAL_START is the index of the start of that part. */
4400 argptr
->raw
= buffer
;
4401 argptr
->raw_length
= bufsize
;
4402 argptr
->free1
= buffer
;
4403 argptr
->newlines
= newlines
;
4404 argptr
->comments
= comments
;
4405 if ((newlines
|| comments
) && ip
->fname
!= 0)
4408 discard_comments (argptr
->raw
+ final_start
,
4409 argptr
->raw_length
- final_start
,
4411 argptr
->raw
[argptr
->raw_length
] = 0;
4412 if (argptr
->raw_length
> bufsize
+ extra
)
4417 /* If we are not discarding this argument,
4418 macroexpand it and compute its length as stringified.
4419 All this info goes into *ARGPTR. */
4423 register const U_CHAR
*buf
, *lim
;
4424 register int totlen
;
4426 obuf
= expand_to_temp_buffer (argptr
->raw
,
4427 argptr
->raw
+ argptr
->raw_length
,
4430 argptr
->expanded
= obuf
.buf
;
4431 argptr
->expand_length
= obuf
.length
;
4432 argptr
->free2
= obuf
.buf
;
4435 lim
= buf
+ argptr
->raw_length
;
4438 while (buf
!= lim
) {
4439 register U_CHAR c
= *buf
++;
4441 /* Internal sequences of whitespace are replaced by one space
4442 in most cases, but not always. So count all the whitespace
4443 in case we need to keep it all. */
4444 if (c
== '\"' || c
== '\\') /* escape these chars */
4446 else if (!ISPRINT (c
))
4449 argptr
->stringified_length
= totlen
;
4454 /* Scan text from START (inclusive) up to LIMIT (exclusive),
4455 counting parens in *DEPTHPTR,
4456 and return if reach LIMIT
4457 or before a `)' that would make *DEPTHPTR negative
4458 or before a comma when *DEPTHPTR is zero.
4459 Single and double quotes are matched and termination
4460 is inhibited within them. Comments also inhibit it.
4461 Value returned is pointer to stopping place.
4463 Increment *NEWLINES each time a newline is passed.
4464 Set *COMMENTS to 1 if a comment is seen. */
4467 macarg1 (start
, limit
, depthptr
, newlines
, comments
)
4469 register const U_CHAR
*limit
;
4470 int *depthptr
, *newlines
, *comments
;
4472 register U_CHAR
*bp
= start
;
4474 while (bp
< limit
) {
4480 if (--(*depthptr
) < 0)
4484 /* Traditionally, backslash makes following char not special. */
4488 /* But count source lines anyway. */
4497 if (bp
[1] == '\\' && bp
[2] == '\n')
4498 newline_fix (bp
+ 1);
4499 if (bp
[1] != '*' || bp
+ 1 >= limit
)
4503 while (bp
+ 1 < limit
) {
4505 && bp
[1] == '\\' && bp
[2] == '\n')
4506 newline_fix (bp
+ 1);
4507 if (bp
[0] == '*' && bp
[1] == '/')
4509 if (*bp
== '\n') ++*newlines
;
4518 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
4523 while (*bp
== '\\' && bp
[1] == '\n') {
4526 } else if (*bp
== '\n') {
4535 if ((*depthptr
) == 0)
4545 /* Discard comments and duplicate newlines
4546 in the string of length LENGTH at START,
4547 except inside of string constants.
4548 The string is copied into itself with its beginning staying fixed.
4550 NEWLINES is the number of newlines that must be duplicated.
4551 We assume that that much extra space is available past the end
4555 discard_comments (start
, length
, newlines
)
4560 register U_CHAR
*ibp
;
4561 register U_CHAR
*obp
;
4562 register const U_CHAR
*limit
;
4565 /* If we have newlines to duplicate, copy everything
4566 that many characters up. Then, in the second part,
4567 we will have room to insert the newlines
4569 NEWLINES may actually be too large, because it counts
4570 newlines in string constants, and we don't duplicate those.
4571 But that does no harm. */
4573 ibp
= start
+ length
;
4574 obp
= ibp
+ newlines
;
4576 while (limit
!= ibp
)
4580 ibp
= start
+ newlines
;
4581 limit
= start
+ length
+ newlines
;
4584 while (ibp
< limit
) {
4585 *obp
++ = c
= *ibp
++;
4588 /* Duplicate the newline. */
4600 if (*ibp
== '\\' && ibp
[1] == '\n')
4602 /* Delete any comment. */
4603 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
4607 while (ibp
+ 1 < limit
) {
4609 && ibp
[1] == '\\' && ibp
[2] == '\n')
4610 newline_fix (ibp
+ 1);
4611 if (ibp
[0] == '*' && ibp
[1] == '/')
4620 /* Notice and skip strings, so that we don't
4621 think that comments start inside them,
4622 and so we don't duplicate newlines in them. */
4625 while (ibp
< limit
) {
4626 *obp
++ = c
= *ibp
++;
4629 if (c
== '\n' && quotec
== '\'')
4631 if (c
== '\\' && ibp
< limit
) {
4632 while (*ibp
== '\\' && ibp
[1] == '\n')
4646 /* Core error handling routine. */
4648 v_message (mtype
, line
, msgid
, ap
)
4654 const char *fname
= 0;
4657 if (mtype
== WARNING
&& inhibit_warnings
)
4660 for (i
= indepth
; i
>= 0; i
--)
4661 if (instack
[i
].fname
!= NULL
) {
4663 line
= instack
[i
].lineno
;
4664 fname
= instack
[i
].fname
;
4669 fprintf (stderr
, "%s:%d: ", fname
, line
);
4671 fprintf (stderr
, "%s: ", progname
);
4673 if (mtype
== WARNING
)
4674 fputs ("warning: ", stderr
);
4676 vfprintf (stderr
, msgid
, ap
);
4677 putc ('\n', stderr
);
4684 * error - print error message and increment count of errors.
4687 error
VPARAMS ((const char *msgid
, ...))
4690 VA_FIXEDARG (ap
, const char *, msgid
);
4692 v_message (ERROR
, 0, msgid
, ap
);
4697 error_with_line
VPARAMS ((int line
, const char *msgid
, ...))
4700 VA_FIXEDARG (ap
, int, line
);
4701 VA_FIXEDARG (ap
, const char *, msgid
);
4703 v_message (ERROR
, line
, msgid
, ap
);
4707 /* Error including a message from `errno'. */
4709 error_from_errno (name
)
4712 error ("%s: %s", name
, strerror (errno
));
4715 /* Print error message but don't count it. */
4717 warning
VPARAMS ((const char *msgid
, ...))
4720 VA_FIXEDARG (ap
, const char *, msgid
);
4722 v_message (WARNING
, 0, msgid
, ap
);
4727 fatal
VPARAMS ((const char *msgid
, ...))
4730 VA_FIXEDARG (ap
, const char *, msgid
);
4732 v_message (FATAL
, 0, msgid
, ap
);
4734 exit (FATAL_EXIT_CODE
);
4737 /* More 'friendly' abort that prints the location at which we died. */
4739 fancy_abort (line
, func
)
4743 fatal ("Internal error in %s, at tradcpp.c:%d\n\
4744 Please submit a full bug report.\n\
4745 See %s for instructions.", func
, line
, GCCBUGURL
);
4749 perror_with_name (name
)
4752 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, strerror (errno
));
4757 pfatal_with_name (name
)
4760 perror_with_name (name
);
4761 exit (FATAL_EXIT_CODE
);
4764 /* Return the line at which an error occurred.
4765 The error is not necessarily associated with the current spot
4766 in the input stack, so LINE says where. LINE will have been
4767 copied from ip->lineno for the current input level.
4768 If the current level is for a file, we return LINE.
4769 But if the current level is not for a file, LINE is meaningless.
4770 In that case, we return the lineno of the innermost file. */
4772 line_for_error (line
)
4778 for (i
= indepth
; i
>= 0; ) {
4779 if (instack
[i
].fname
!= 0)
4784 line1
= instack
[i
].lineno
;
4790 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4792 * As things stand, nothing is ever placed in the output buffer to be
4793 * removed again except when it's KNOWN to be part of an identifier,
4794 * so flushing and moving down everything left, instead of expanding,
4799 grow_outbuf (obuf
, needed
)
4800 register FILE_BUF
*obuf
;
4801 register int needed
;
4806 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
4809 /* Make it at least twice as big as it is now. */
4811 /* Make it have at least 150% of the free space we will need. */
4812 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
4813 if (minsize
> obuf
->length
)
4814 obuf
->length
= minsize
;
4816 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
4817 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
4821 /* Symbol table for macro names and special symbols */
4824 * install a name in the main hash table, even if it is already there.
4825 * name stops with first non alphanumeric, except leading '#'.
4826 * caller must check against redefinition if that is desired.
4827 * delete_macro () removes things installed by install () in fifo order.
4828 * this is important because of the `defined' special symbol used
4829 * in #if, and also if pushdef/popdef directives are ever implemented.
4831 * If LEN is >= 0, it is the length of the name.
4832 * Otherwise, compute the length by scanning the entire name.
4834 * If HASH is >= 0, it is the precomputed hash code.
4835 * Otherwise, compute the hash code.
4837 * caller must set the value, if any is desired.
4840 install (name
, len
, type
, hash
)
4843 enum node_type type
;
4845 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4847 register HASHNODE
*hp
;
4848 register int bucket
;
4849 register const U_CHAR
*p
;
4854 while (is_idchar (*p
))
4860 hash
= hashf (name
, len
, HASHSIZE
);
4862 hp
= (HASHNODE
*) xmalloc (sizeof (HASHNODE
) + len
+ 1);
4864 hp
->bucket_hdr
= &hashtab
[bucket
];
4865 hp
->next
= hashtab
[bucket
];
4866 hashtab
[bucket
] = hp
;
4868 if (hp
->next
!= NULL
)
4869 hp
->next
->prev
= hp
;
4872 hp
->name
= q
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
4873 memcpy (q
, name
, len
);
4879 * find the most recent hash node for name name (ending with first
4880 * non-identifier char) installed by install
4882 * If LEN is >= 0, it is the length of the name.
4883 * Otherwise, compute the length by scanning the entire name.
4885 * If HASH is >= 0, it is the precomputed hash code.
4886 * Otherwise, compute the hash code.
4889 lookup (name
, len
, hash
)
4894 register const U_CHAR
*bp
;
4895 register HASHNODE
*bucket
;
4898 for (bp
= name
; is_idchar (*bp
); bp
++) ;
4903 hash
= hashf (name
, len
, HASHSIZE
);
4905 bucket
= hashtab
[hash
];
4907 if (bucket
->length
== len
4908 && strncmp ((const char *)bucket
->name
, (const char *)name
, len
) == 0)
4910 bucket
= bucket
->next
;
4916 * Delete a hash node. Some weirdness to free junk from macros.
4917 * More such weirdness will have to be added if you define more hash
4918 * types that need it.
4921 /* Note that the DEFINITION of a macro is removed from the hash table
4922 but its storage is not freed. This would be a storage leak
4923 except that it is not reasonable to keep undefining and redefining
4924 large numbers of macros many times.
4925 In any case, this is necessary, because a macro can be #undef'd
4926 in the middle of reading the arguments to a call to it.
4927 If #undef freed the DEFINITION, that would crash. */
4933 if (hp
->prev
!= NULL
)
4934 hp
->prev
->next
= hp
->next
;
4935 if (hp
->next
!= NULL
)
4936 hp
->next
->prev
= hp
->prev
;
4938 /* make sure that the bucket chain header that
4939 the deleted guy was on points to the right thing afterwards. */
4940 if (hp
== *hp
->bucket_hdr
)
4941 *hp
->bucket_hdr
= hp
->next
;
4947 * return hash function on name. must be compatible with the one
4948 * computed a step at a time, elsewhere
4951 hashf (name
, len
, hashsize
)
4952 register const U_CHAR
*name
;
4959 r
= HASHSTEP (r
, *name
++);
4961 return MAKE_POS (r
) % hashsize
;
4964 /* Dump all macro definitions as #defines to stdout. */
4971 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
4972 register HASHNODE
*hp
;
4974 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
4975 if (hp
->type
== T_MACRO
) {
4976 register DEFINITION
*defn
= hp
->value
.defn
;
4982 /* Print the definition of the macro HP. */
4984 printf ("#define %s", hp
->name
);
4985 if (defn
->nargs
>= 0) {
4989 for (i
= 0; i
< defn
->nargs
; i
++) {
4990 dump_arg_n (defn
, i
);
4991 if (i
+ 1 < defn
->nargs
)
5001 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
5002 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
);
5003 if (ap
->nchars
!= 0)
5005 offset
+= ap
->nchars
;
5008 if (ap
->raw_before
&& !concat
)
5011 dump_arg_n (defn
, ap
->argno
);
5012 if (ap
->raw_after
) {
5017 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
);
5024 /* Output to stdout a substring of a macro definition.
5025 BASE is the beginning of the definition.
5026 Output characters START thru LENGTH.
5027 Discard newlines outside of strings, thus
5028 converting funny-space markers to ordinary spaces. */
5030 dump_defn_1 (base
, start
, length
)
5035 const U_CHAR
*p
= base
+ start
;
5036 const U_CHAR
*limit
= base
+ start
+ length
;
5041 else if (*p
== '\"' || *p
=='\'') {
5042 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
5043 fwrite (p
, p1
- p
, 1, stdout
);
5050 /* Print the name of argument number ARGNUM of macro definition DEFN.
5051 Recall that DEFN->argnames contains all the arg names
5052 concatenated in reverse order with comma-space in between. */
5054 dump_arg_n (defn
, argnum
)
5058 register const U_CHAR
*p
= defn
->argnames
;
5059 while (argnum
+ 1 < defn
->nargs
) {
5060 p
= (const U_CHAR
*) strchr ((const char *)p
, ' ') + 1;
5064 while (*p
&& *p
!= ',') {
5070 /* Initialize the built-in macros. */
5071 #define DSC(x) U x, sizeof x - 1
5072 #define install_spec(name, type) \
5073 install(DSC(name), type, -1);
5074 #define install_value(name, val) \
5075 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
5077 initialize_builtins ()
5081 install_spec ("__BASE_FILE__", T_BASE_FILE
);
5082 install_spec ("__DATE__", T_DATE
);
5083 install_spec ("__FILE__", T_FILE
);
5084 install_spec ("__TIME__", T_TIME
);
5085 install_spec ("__VERSION__", T_VERSION
);
5086 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL
);
5087 install_spec ("__LINE__", T_SPECLINE
);
5089 #ifndef NO_BUILTIN_SIZE_TYPE
5090 install_value ("__SIZE_TYPE__", SIZE_TYPE
);
5092 #ifndef NO_BUILTIN_PTRDIFF_TYPE
5093 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
);
5095 #ifndef NO_BUILTIN_WCHAR_TYPE
5096 install_value ("__WCHAR_TYPE__", WCHAR_TYPE
);
5098 #ifndef NO_BUILTIN_WINT_TYPE
5099 install_value ("__WINT_TYPE__", WINT_TYPE
);
5101 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX
);
5102 install_value ("__USER_LABEL_PREFIX__", user_label_prefix
);
5106 #undef install_value
5108 /* Common handler of command line directives -U, -D and -A. */
5110 run_directive (str
, len
, type
)
5113 enum node_type type
;
5115 struct directive
*kt
;
5116 FILE_BUF
*ip
= &instack
[++indepth
];
5117 ip
->fname
= "*command line*";
5119 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
5124 ip
->if_stack
= if_stack
;
5126 for (kt
= directive_table
; kt
->type
!= type
; kt
++)
5129 (*kt
->func
) ((U_CHAR
*) str
, (U_CHAR
*) str
+ len
, NULL
);
5133 /* Handle the -D option. If STR is just an identifier, define it with
5134 * value 1. If STR has anything after the identifier, then it should
5135 * be identifier-space-definition. */
5137 make_definition (str
)
5143 /* Copy the entire option so we can modify it.
5144 Change the first "=" in the string to a space. If there is none,
5145 tack " 1" on the end. */
5147 /* Length including the null. */
5148 count
= strlen (str
);
5149 buf
= (char *) alloca (count
+ 2);
5150 memcpy (buf
, str
, count
);
5152 p
= strchr (str
, '=');
5161 run_directive (buf
, count
, T_DEFINE
);
5164 /* Handle the -U option. */
5169 run_directive (str
, strlen (str
), T_UNDEF
);
5172 /* Handles the #assert (-A) and #unassert (-A-) command line options. */
5174 make_assertion (str
)
5177 enum node_type type
= T_ASSERT
;
5187 count
= strlen (str
);
5188 p
= strchr (str
, '=');
5191 /* Copy the entire option so we can modify it. Change the first
5192 "=" in the string to a '(', and tack a ')' on the end. */
5193 char *buf
= (char *) alloca (count
+ 1);
5195 memcpy (buf
, str
, count
);
5201 run_directive (str
, count
, type
);
5204 /* Get the file-mode and data size of the file open on FD
5205 and store them in *MODE_POINTER and *SIZE_POINTER. */
5208 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
5215 if (fstat (fd
, &sbuf
) < 0) return -1;
5216 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
5217 if (size_pointer
) *size_pointer
= sbuf
.st_size
;