1 /* C Compatible Compiler Preprocessor (CCCP)
2 Copyright (C) 1986, 1987, 1989, 2000, 2001 Free Software Foundation, Inc.
3 Written by Paul Rubin, June 1986
4 Adapted to ANSI C, Richard Stallman, Jan 1987
5 Dusted off, polished, and adapted for use as traditional
6 preprocessor only, Zack Weinberg, Jul 2000
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "cppdefault.h"
30 typedef unsigned char U_CHAR
;
32 /* Name under which this program was invoked. */
34 static const char *progname
;
36 /* Current maximum length of directory names in the search path
37 for include files. (Altered as we get more of them.) */
39 size_t max_include_len
;
41 /* Nonzero means copy comments into the output file. */
43 int put_out_comments
= 0;
45 /* mkdeps.h opaque structure that encapsulates dependency information. */
48 /* Nonzero means print the names of included files rather than
49 the preprocessed output. 1 means just the #include "...",
50 2 means #include <...> as well. */
54 /* Nonzero means print dummy targets for each header file. */
56 int print_deps_phony_targets
= 0;
58 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
62 /* File name which deps are being written to. This is 0 if deps are
63 being written to stdout. */
65 const char *deps_file
= 0;
67 /* Nonzero if missing .h files in -M output are assumed to be
68 generated files and not errors. */
70 int deps_missing_files
= 0;
72 /* Nonzero means don't output line number information. */
76 /* Nonzero means inhibit output of the preprocessed text
77 and instead output the definitions of all user-defined macros
78 in a form suitable for use as input to cccp. */
82 /* Nonzero means don't print warning messages. -w. */
84 int inhibit_warnings
= 0;
86 /* Non-0 means don't output the preprocessed program. */
87 int inhibit_output
= 0;
89 /* Nonzero means warn if slash-star appears in a comment. */
93 /* Nonzero causes output not to be done,
94 but directives such as #define that have side effects
99 /* Value of __USER_LABEL_PREFIX__. Target-dependent, also controlled
100 by -f(no-)leading-underscore. */
101 static const char *user_label_prefix
;
103 /* I/O buffer structure.
104 The `fname' field is nonzero for source files and #include files
105 and for the dummy text used for -D and -U.
106 It is zero for rescanning results of macro expansion
107 and for expanding macro arguments. */
108 #define INPUT_STACK_MAX 200
109 struct file_name_list
;
116 /* Macro that this level is the expansion of.
117 Included so that we can reenable the macro
118 at the end of this level. */
119 struct hashnode
*macro
;
120 /* Value of if_stack at start of this file.
121 Used to prohibit unmatched #endif (etc) in an include file. */
122 struct if_stack
*if_stack
;
123 /* Object to be freed at end of input at this level. */
125 /* Position to start scanning for #include_next in this file. */
126 struct file_name_list
*next_header_dir
;
127 } instack
[INPUT_STACK_MAX
];
129 typedef struct file_buf FILE_BUF
;
131 /* Current nesting level of input sources.
132 `instack[indepth]' is the level currently being read. */
134 #define CHECK_DEPTH(code) \
135 if (indepth >= (INPUT_STACK_MAX - 1)) \
137 error_with_line (line_for_error (instack[indepth].lineno), \
138 "macro or #include recursion too deep"); \
142 /* Current depth in #include directives that use <...>. */
143 int system_include_depth
= 0;
145 /* The output buffer. Its LENGTH field is the amount of room allocated
146 for the buffer, not the number of chars actually present. To get
147 that, subtract outbuf.buf from outbuf.bufp. */
149 #define OUTBUF_SIZE 10 /* initial size of output buffer */
152 /* Grow output buffer OBUF points at
153 so it can hold at least NEEDED more chars. */
155 #define check_expand(OBUF, NEEDED) do { \
156 if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
157 grow_outbuf ((OBUF), (NEEDED)); \
160 struct file_name_list
162 struct file_name_list
*next
;
166 struct file_name_list
*include
= 0; /* First dir to search */
167 /* First dir to search for <file> */
168 struct file_name_list
*first_bracket_include
= 0;
169 struct file_name_list
*last_include
= 0; /* Last in chain */
171 /* List of included files that contained #once. */
172 struct file_name_list
*dont_repeat_files
= 0;
174 /* List of other included files. */
175 struct file_name_list
*all_include_files
= 0;
177 /* Structure allocated for every #define. For a simple replacement
180 nargs = -1, the `pattern' list is null, and the expansion is just
181 the replacement text. Nargs = 0 means a functionlike macro with no args,
183 #define getchar() getc (stdin) .
184 When there are args, the expansion is the replacement text with the
185 args squashed out, and the reflist is a list describing how to
186 build the output from the input: e.g., "3 chars, then the 1st arg,
187 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
188 The chars here come from the expansion. Whatever is left of the
189 expansion after the last arg-occurrence is copied after that arg.
190 Note that the reflist can be arbitrarily long---
191 its length depends on the number of times the arguments appear in
192 the replacement text, not how many args there are. Example:
193 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
195 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
196 where (x, y) means (nchars, argno). */
198 typedef struct definition DEFINITION
;
201 int length
; /* length of expansion string */
204 struct reflist
*next
;
205 char stringify
; /* nonzero if this arg was preceded by a
207 char raw_before
; /* Nonzero if a ## operator before arg. */
208 char raw_after
; /* Nonzero if a ## operator after arg. */
209 int nchars
; /* Number of literal chars to copy before
210 this arg occurrence. */
211 int argno
; /* Number of arg to substitute (origin-0) */
213 /* Names of macro args, concatenated in reverse order
214 with comma-space between them.
215 The only use of this is that we warn on redefinition
216 if this differs between the old and new definitions. */
217 const U_CHAR
*argnames
;
220 /* Chained list of answers to an assertion. */
224 const unsigned char *answer
;
228 /* different kinds of things that can appear in the value field
229 of a hash node. Actually, this may be useless now. */
233 struct answer
*answers
;
236 /* The structure of a node in the hash table. The hash table
237 has entries for all tokens defined by #define commands (type T_MACRO),
238 plus some special tokens like __LINE__ (these each have their own
239 type, and the appropriate code is run when that type of node is seen.
240 It does not contain control words like "#define", which are recognized
241 by a separate piece of code. */
243 /* different flavors of hash nodes --- also used in keyword table */
245 T_DEFINE
= 1, /* `#define' */
246 T_INCLUDE
, /* `#include' */
247 T_INCLUDE_NEXT
,/* `#include_next' */
248 T_IFDEF
, /* `#ifdef' */
249 T_IFNDEF
, /* `#ifndef' */
251 T_ELSE
, /* `#else' */
252 T_ELIF
, /* `#elif' */
253 T_UNDEF
, /* `#undef' */
254 T_LINE
, /* `#line' */
255 T_ENDIF
, /* `#endif' */
256 T_ERROR
, /* `#error' */
257 T_WARNING
, /* `#warning' */
258 T_ASSERT
, /* `#assert' */
259 T_UNASSERT
, /* `#unassert' */
260 T_SPECLINE
, /* special symbol `__LINE__' */
261 T_DATE
, /* `__DATE__' */
262 T_FILE
, /* `__FILE__' */
263 T_BASE_FILE
, /* `__BASE_FILE__' */
264 T_INCLUDE_LEVEL
, /* `__INCLUDE_LEVEL__' */
265 T_VERSION
, /* `__VERSION__' */
266 T_TIME
, /* `__TIME__' */
267 T_CONST
, /* Constant value, used by `__STDC__' */
268 T_MACRO
, /* macro defined by `#define' */
269 T_SPEC_DEFINED
, /* special `defined' macro for use in #if statements */
270 T_UNUSED
/* Used for something not defined. */
274 struct hashnode
*next
; /* double links for easy deletion */
275 struct hashnode
*prev
;
276 struct hashnode
**bucket_hdr
; /* also, a back pointer to this node's hash
277 chain is kept, in case the node is the head
278 of the chain and gets deleted. */
279 enum node_type type
; /* type of special token */
280 int length
; /* length of token, for quick comparison */
281 U_CHAR
*name
; /* the actual name */
282 union hashval value
; /* pointer to expansion, or whatever */
285 typedef struct hashnode HASHNODE
;
287 static HASHNODE
*parse_assertion
PARAMS ((const unsigned char *,
288 const unsigned char *,
289 struct answer
**, int));
290 static struct answer
**find_answer
PARAMS ((HASHNODE
*,
291 const struct answer
*));
292 static int parse_answer
PARAMS ((const unsigned char *, const unsigned char *,
293 struct answer
**, int));
294 static unsigned char *canonicalize_text
PARAMS ((const unsigned char *,
295 const unsigned char *,
296 const unsigned char **));
298 /* Some definitions for the hash table. The hash function MUST be
299 computed as shown in hashf () below. That is because the rescan
300 loop computes the hash value `on the fly' for most tokens,
301 in order to avoid the overhead of a lot of procedure calls to
302 the hashf () function. Hashf () only exists for the sake of
303 politeness, for use when speed isn't so important. */
305 #define HASHSIZE 1403
306 HASHNODE
*hashtab
[HASHSIZE
];
307 #define HASHSTEP(old, c) ((old << 2) + c)
308 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
310 /* `struct directive' defines one #-directive, including how to handle it. */
313 int length
; /* Length of name */
314 void (*func
) PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
315 /* Function to handle directive */
316 const char *name
; /* Name of directive */
317 enum node_type type
; /* Code which describes which directive. */
320 /* Last arg to output_line_command. */
321 enum file_change_code
{same_file
, enter_file
, leave_file
};
323 /* This structure represents one parsed argument in a macro call.
324 `raw' points to the argument text as written (`raw_length' is its length).
325 `expanded' points to the argument's macro-expansion
326 (its length is `expand_length').
327 `stringified_length' is the length the argument would have
329 `free1' and `free2', if nonzero, point to blocks to be freed
330 when the macro argument data is no longer needed. */
333 U_CHAR
*raw
, *expanded
;
334 int raw_length
, expand_length
;
335 int stringified_length
;
336 U_CHAR
*free1
, *free2
;
341 /* The arglist structure is built by do_define to tell
342 collect_definition where the argument names begin. That
343 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
344 would contain pointers to the strings x, y, and z.
345 Collect_definition would then build a DEFINITION node,
346 with reflist nodes pointing to the places x, y, and z had
347 appeared. So the arglist is just convenience data passed
348 between these two routines. It is not kept around after
349 the current #define has been processed and entered into the
353 struct arglist
*next
;
359 /* Function prototypes. */
361 static void do_define
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
362 static void do_error
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
363 static void do_warning
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
364 static void do_line
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
365 static void do_include
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
366 static void do_include_next
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
367 static void do_undef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
368 static void do_if
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
369 static void do_ifdef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
370 static void do_ifndef
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
371 static void do_else
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
372 static void do_elif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
373 static void do_endif
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
374 static void do_assert
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
375 static void do_unassert
PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
376 static void do_xifdef
PARAMS ((U_CHAR
*, U_CHAR
*, enum node_type
));
378 static struct hashnode
*install
PARAMS ((const U_CHAR
*, int, enum node_type
, int));
379 static int hashf
PARAMS ((const U_CHAR
*, int, int));
380 static int compare_defs
PARAMS ((DEFINITION
*, DEFINITION
*));
381 static int comp_def_part
PARAMS ((int, const U_CHAR
*, int,
382 const U_CHAR
*, int, int));
383 static void delete_macro
PARAMS ((HASHNODE
*));
385 /* First arg to v_message. */
386 enum msgtype
{ MT_WARNING
= 0, MT_ERROR
, MT_FATAL
};
387 static void v_message
PARAMS ((enum msgtype mtype
, int line
,
388 const char *msgid
, va_list ap
))
389 ATTRIBUTE_PRINTF (3, 0);
391 static int line_for_error
PARAMS ((int));
393 /* We know perfectly well which file this is, so we don't need to
396 #if (GCC_VERSION >= 2007)
397 #define abort() fancy_abort(__LINE__, __FUNCTION__)
399 #define abort() fancy_abort(__LINE__, 0);
402 static void macroexpand
PARAMS ((HASHNODE
*, FILE_BUF
*));
403 static void special_symbol
PARAMS ((HASHNODE
*, FILE_BUF
*));
404 static void dump_all_macros
PARAMS ((void));
405 static void dump_defn_1
PARAMS ((const U_CHAR
*, int, int));
406 static void dump_arg_n
PARAMS ((DEFINITION
*, int));
407 static void conditional_skip
PARAMS ((FILE_BUF
*, int, enum node_type
));
408 static void skip_if_group
PARAMS ((FILE_BUF
*, int));
409 static void output_line_command
PARAMS ((FILE_BUF
*, FILE_BUF
*,
410 int, enum file_change_code
));
412 static int eval_if_expression
PARAMS ((const U_CHAR
*, int));
414 static void output_deps
PARAMS ((void));
415 static void initialize_builtins
PARAMS ((void));
416 static void run_directive
PARAMS ((const char *, size_t,
418 static void make_definition
PARAMS ((const char *));
419 static void make_undef
PARAMS ((const char *));
420 static void make_assertion
PARAMS ((const char *));
422 static void grow_outbuf
PARAMS ((FILE_BUF
*, int));
423 static int handle_directive
PARAMS ((FILE_BUF
*, FILE_BUF
*));
424 static void process_include
PARAMS ((struct file_name_list
*,
425 const U_CHAR
*, int, int, FILE_BUF
*));
426 static void finclude
PARAMS ((int, const char *,
427 struct file_name_list
*, FILE_BUF
*));
428 static void init_dependency_output
PARAMS ((void));
429 static void rescan
PARAMS ((FILE_BUF
*, int));
430 static void newline_fix
PARAMS ((U_CHAR
*));
431 static void name_newline_fix
PARAMS ((U_CHAR
*));
432 static U_CHAR
*macarg1
PARAMS ((U_CHAR
*, const U_CHAR
*, int *,
434 static const char *macarg
PARAMS ((struct argdata
*));
435 static int discard_comments
PARAMS ((U_CHAR
*, int, int));
436 static int file_size_and_mode
PARAMS ((int, int *, long *));
438 static U_CHAR
*skip_to_end_of_comment
PARAMS ((FILE_BUF
*, int *));
439 static U_CHAR
*skip_quoted_string
PARAMS ((const U_CHAR
*, const U_CHAR
*,
440 int, int *, int *, int *));
442 int main
PARAMS ((int, char **));
444 /* Convenience. Write U"string" to get an unsigned string constant. */
445 #define U (const unsigned char *)
447 /* Here is the actual list of #-directives, most-often-used first. */
449 struct directive directive_table
[] = {
450 { 6, do_define
, "define", T_DEFINE
},
451 { 7, do_include
, "include", T_INCLUDE
},
452 { 5, do_endif
, "endif", T_ENDIF
},
453 { 5, do_ifdef
, "ifdef", T_IFDEF
},
454 { 2, do_if
, "if", T_IF
, },
455 { 4, do_else
, "else", T_ELSE
},
456 { 6, do_ifndef
, "ifndef", T_IFNDEF
},
457 { 5, do_undef
, "undef", T_UNDEF
},
458 { 4, do_line
, "line", T_LINE
},
459 { 4, do_elif
, "elif", T_ELIF
},
460 { 5, do_error
, "error", T_ERROR
},
461 { 7, do_warning
, "warning", T_WARNING
},
462 { 12, do_include_next
, "include_next", T_INCLUDE_NEXT
},
463 { 6, do_assert
, "assert", T_ASSERT
},
464 { 8, do_unassert
,"unassert",T_UNASSERT
},
465 { -1, 0, "", T_UNUSED
},
468 #define SKIP_WHITE_SPACE(p) do { while (is_nvspace(*p)) p++; } while (0)
469 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space(*p)) p++; } while (0)
471 int errors
= 0; /* Error counter for exit code */
473 static FILE_BUF expand_to_temp_buffer
PARAMS ((const U_CHAR
*, const U_CHAR
*, int));
474 static DEFINITION
*collect_expansion
PARAMS ((U_CHAR
*, U_CHAR
*, int,
477 /* Stack of conditionals currently in progress
478 (including both successful and failing conditionals). */
481 struct if_stack
*next
; /* for chaining to the next stack frame */
482 const char *fname
; /* copied from input when frame is made */
483 int lineno
; /* similarly */
484 int if_succeeded
; /* true if a leg of this if-group
485 has been passed through rescan */
486 enum node_type type
; /* type of last directive seen in this group */
488 typedef struct if_stack IF_STACK_FRAME
;
489 IF_STACK_FRAME
*if_stack
= NULL
;
491 /* Nonzero means -I- has been seen,
492 so don't look for #include "foo" the source-file directory. */
495 /* Pending directives. */
496 enum pending_dir_t
{PD_NONE
= 0, PD_DEFINE
, PD_UNDEF
, PD_ASSERTION
, PD_FILE
};
498 typedef struct pending_dir pending_dir
;
502 enum pending_dir_t type
;
512 const char *in_fname
, *out_fname
;
515 pending_dir
*pend
= (pending_dir
*) xcalloc (argc
, sizeof (pending_dir
));
516 int no_standard_includes
= 0;
519 /* Get rid of any avoidable limit on stack size. */
523 /* Set the stack limit huge so that alloca (particularly stringtab
524 * in dbxread.c) does not fail. */
525 getrlimit (RLIMIT_STACK
, &rlim
);
526 rlim
.rlim_cur
= rlim
.rlim_max
;
527 setrlimit (RLIMIT_STACK
, &rlim
);
529 #endif /* RLIMIT_STACK defined */
536 no_line_commands
= 0;
540 max_include_len
= cpp_GCC_INCLUDE_DIR_len
+ 7; /* ??? */
544 /* It's simplest to just create this struct whether or not it will
548 /* Process switches and find input file name. */
550 for (i
= 1; i
< argc
; i
++) {
551 if (argv
[i
][0] != '-') {
552 if (out_fname
!= NULL
)
553 fatal ("usage: %s [switches] input output", argv
[0]);
554 else if (in_fname
!= NULL
)
564 break; /* Ignore for compatibility with ISO/extended cpp. */
567 if (!strcmp (argv
[i
], "-lang-c++")
568 || !strcmp (argv
[i
], "-lang-objc++"))
569 fatal ("-traditional is not supported in C++");
570 else if (!strcmp (argv
[i
], "-lang-c89"))
571 fatal ("-traditional and -ansi are mutually exclusive");
572 else if (!strcmp (argv
[i
], "-lang-objc"))
573 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__OBJC__";
574 else if (!strcmp (argv
[i
], "-lang-asm"))
575 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__ASSEMBLER__";
576 else if (!strcmp (argv
[i
], "-lang-fortran"))
577 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "_LANGUAGE_FORTRAN";
578 /* All other possibilities ignored. */
582 if (!strcmp (argv
[i
], "-include"))
585 fatal ("filename missing after -i option");
587 pend
[i
].type
= PD_FILE
, pend
[i
].arg
= argv
[i
+ 1], i
++;
589 else if (!strcmp (argv
[i
], "-iprefix"))
590 i
++; /* Ignore for compatibility */
591 else if (!strcmp (argv
[i
], "-isystem")
592 || !strcmp (argv
[i
], "-iwithprefix")
593 || !strcmp (argv
[i
], "-iwithprefixbefore")
594 || !strcmp (argv
[i
], "-idirafter"))
595 goto add_include
; /* best we can do */
600 if (out_fname
!= NULL
)
601 fatal ("output filename specified twice");
603 fatal ("filename missing after -o option");
604 out_fname
= argv
[++i
];
605 if (!strcmp (out_fname
, "-"))
610 inhibit_warnings
= 1;
614 if (!strcmp (argv
[i
], "-Wcomments"))
616 else if (!strcmp (argv
[i
], "-Wcomment"))
618 else if (!strcmp (argv
[i
], "-Wall")) {
624 if (!strcmp (argv
[i
], "-fleading-underscore"))
625 user_label_prefix
= "_";
626 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
627 user_label_prefix
= "";
634 /* -MD and -MMD for tradcpp are deprecated and undocumented
635 (use -M or -MM with -MF instead), and probably should be
636 removed with the next major GCC version. For the moment
637 we allow these for the benefit of Automake 1.4, which
638 uses these when dependency tracking is enabled. Automake
639 1.5 will fix this. */
640 if (!strncmp (argv
[i
], "-MD", 3)) {
643 } else if (!strncmp (argv
[i
], "-MMD", 4)) {
646 } else if (!strcmp (argv
[i
], "-M")) {
648 } else if (!strcmp (argv
[i
], "-MM")) {
650 } else if (!strcmp (argv
[i
], "-MG")) {
651 deps_missing_files
= 1;
652 } else if (!strcmp (argv
[i
], "-MF")) {
654 } else if (!strcmp (argv
[i
], "-MP")) {
655 print_deps_phony_targets
= 1;
656 } else if (!strcmp (argv
[i
], "-MQ") || !strcmp (argv
[i
], "-MT")) {
657 /* Add a target. -MQ quotes for Make. */
658 const char *tgt
= argv
[i
] + 3;
659 int quoted
= argv
[i
][2] == 'Q';
661 if (*tgt
== '\0' && i
+ 1 == argc
)
662 fatal ("target missing after %s option", argv
[i
]);
668 deps_add_target (deps
, tgt
, quoted
);
675 else if (i
+ 1 == argc
)
676 fatal ("filename missing after %s option", argv
[i
]);
678 deps_file
= argv
[++i
];
689 fprintf (stderr
, "GNU traditional CPP version %s\n", version_string
);
700 else if (i
+ 1 == argc
)
701 fatal ("macro name missing after -%c option", c
);
706 pend
[i
].type
= PD_DEFINE
;
708 pend
[i
].type
= PD_UNDEF
;
710 pend
[i
].type
= PD_ASSERTION
;
716 put_out_comments
= 1;
720 if (!strcmp (argv
[i
], "-pedantic"))
721 fatal ("-pedantic and -traditional are mutually exclusive");
725 if (!strcmp (argv
[i
], "-trigraphs"))
726 fatal ("-trigraphs and -traditional are mutually exclusive");
730 no_line_commands
= 1;
733 case 'I': /* Add directory to path for includes. */
736 struct file_name_list
*dirtmp
;
738 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
741 dirtmp
= (struct file_name_list
*)
742 xmalloc (sizeof (struct file_name_list
));
743 dirtmp
->next
= 0; /* New one goes on the end */
747 last_include
->next
= dirtmp
;
748 last_include
= dirtmp
; /* Tail follows the last one */
749 if (argv
[i
][1] == 'I' && argv
[i
][2] != 0)
750 dirtmp
->fname
= argv
[i
] + 2;
751 else if (i
+ 1 == argc
)
752 fatal ("directory name missing after -I option");
754 dirtmp
->fname
= argv
[++i
];
755 if (strlen (dirtmp
->fname
) > max_include_len
)
756 max_include_len
= strlen (dirtmp
->fname
);
757 if (ignore_srcdir
&& first_bracket_include
== 0)
758 first_bracket_include
= dirtmp
;
764 /* -nostdinc causes no default include directories.
765 You must specify all include-file directories with -I. */
766 no_standard_includes
= 1;
769 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
770 if (in_fname
== NULL
) {
773 } else if (out_fname
== NULL
) {
776 } /* else fall through into error */
779 fatal ("invalid option `%s'", argv
[i
]);
784 init_dependency_output ();
786 /* After checking the environment variables, check if -M or -MM has
787 not been specified, but other -M options have. */
789 && (deps_missing_files
|| deps_file
|| print_deps_phony_targets
))
790 fatal ("you must additionally specify either -M or -MM");
792 if (user_label_prefix
== 0)
793 user_label_prefix
= USER_LABEL_PREFIX
;
797 /* Set the default target (if there is none already), and
798 the dependency on the main file. */
799 deps_add_default_target (deps
, in_fname
);
801 deps_add_dep (deps
, in_fname
);
804 /* Install __LINE__, etc. Must follow option processing. */
805 initialize_builtins ();
807 /* Do defines specified with -D and undefines specified with -U. */
808 for (i
= 1; i
< argc
; i
++)
809 if (pend
[i
].type
== PD_DEFINE
)
810 make_definition (pend
[i
].arg
);
811 else if (pend
[i
].type
== PD_UNDEF
)
812 make_undef (pend
[i
].arg
);
813 else if (pend
[i
].type
== PD_ASSERTION
)
814 make_assertion (pend
[i
].arg
);
816 /* Unless -fnostdinc,
817 tack on the standard include file dirs to the specified list */
818 if (!no_standard_includes
) {
819 const struct default_include
*di
;
820 struct file_name_list
*old_last_include
= last_include
;
821 struct file_name_list
*dirtmp
;
822 for (di
= cpp_include_defaults
; di
->fname
; di
++) {
825 dirtmp
= (struct file_name_list
*)
826 xmalloc (sizeof (struct file_name_list
));
827 dirtmp
->next
= 0; /* New one goes on the end */
831 last_include
->next
= dirtmp
;
832 last_include
= dirtmp
; /* Tail follows the last one */
833 dirtmp
->fname
= di
->fname
;
834 if (strlen (dirtmp
->fname
) > max_include_len
)
835 max_include_len
= strlen (dirtmp
->fname
);
838 if (ignore_srcdir
&& first_bracket_include
== 0)
839 first_bracket_include
= old_last_include
->next
;
842 /* Initialize output buffer */
844 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
845 outbuf
.bufp
= outbuf
.buf
;
846 outbuf
.length
= OUTBUF_SIZE
;
848 /* Scan the -i files before the main input.
849 Much like #including them, but with no_output set
850 so that only their macro definitions matter. */
854 for (i
= 1; i
< argc
; i
++)
855 if (pend
[i
].type
== PD_FILE
)
857 int fd
= open (pend
[i
].arg
, O_RDONLY
, 0666);
860 perror_with_name (pend
[i
].arg
);
861 return FATAL_EXIT_CODE
;
864 /* For -M, add this file to the dependencies. */
866 deps_add_dep (deps
, pend
[i
].arg
);
868 finclude (fd
, pend
[i
].arg
, 0, &outbuf
);
873 /* Pending directives no longer needed. */
876 /* Create an input stack level for the main input file
877 and copy the entire contents of the file into it. */
879 fp
= &instack
[++indepth
];
881 /* JF check for stdin */
882 if (in_fname
== NULL
|| *in_fname
== 0) {
885 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
888 if (file_size_and_mode (f
, &st_mode
, &st_size
))
890 fp
->fname
= in_fname
;
892 /* JF all this is mine about reading pipes and ttys */
893 if (!S_ISREG (st_mode
)) {
894 /* Read input from a file that is not a normal disk file.
895 We cannot preallocate a buffer with the correct size,
896 so we must read in the file a piece at the time and make it bigger. */
904 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
907 cnt
= read (f
, bufp
, bsize
- size
);
908 if (cnt
< 0) goto sys_error
; /* error! */
909 if (cnt
== 0) break; /* End of file */
912 if (bsize
== size
) { /* Buffer is full! */
914 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
915 bufp
= fp
->buf
+ size
; /* May have moved */
920 /* Read a file whose size we can determine in advance.
921 For the sake of VMS, st_size is just an upper bound. */
924 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
926 while (st_size
> 0) {
927 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
937 fp
->if_stack
= if_stack
;
939 /* Make sure data ends with a newline. And put a null after it. */
941 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
942 fp
->buf
[fp
->length
++] = '\n';
943 fp
->buf
[fp
->length
] = '\0';
945 /* Now that we know the input file is valid, open the output. */
947 if (!out_fname
|| !strcmp (out_fname
, ""))
948 out_fname
= "stdout";
949 else if (! freopen (out_fname
, "w", stdout
))
950 pfatal_with_name (out_fname
);
952 output_line_command (fp
, &outbuf
, 0, same_file
);
954 /* Scan the input, processing macros and directives. */
958 /* Now we have processed the entire input
959 Write whichever kind of output has been requested. */
964 else if (! inhibit_output
)
965 if (write (fileno (stdout
), outbuf
.buf
, outbuf
.bufp
- outbuf
.buf
) < 0)
966 fatal ("I/O error on output");
968 /* Don't write the deps file if preprocessing has failed. */
969 if (print_deps
&& errors
== 0)
972 /* Destruct the deps object. */
976 fatal ("I/O error on output");
979 exit (FATAL_EXIT_CODE
);
980 exit (SUCCESS_EXIT_CODE
);
983 pfatal_with_name (in_fname
);
986 /* Set up dependency-file output. */
988 init_dependency_output ()
990 char *spec
, *s
, *output_file
;
992 /* Either of two environment variables can specify output of deps.
993 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
994 where OUTPUT_FILE is the file to write deps info to
995 and DEPS_TARGET is the target to mention in the deps. */
999 spec
= getenv ("DEPENDENCIES_OUTPUT");
1004 spec
= getenv ("SUNPRO_DEPENDENCIES");
1011 /* Find the space before the DEPS_TARGET, if there is one. */
1012 s
= strchr (spec
, ' ');
1015 /* Let the caller perform MAKE quoting. */
1016 deps_add_target (deps
, s
+ 1, 0);
1017 output_file
= (char *) xmalloc (s
- spec
+ 1);
1018 memcpy (output_file
, spec
, s
- spec
);
1019 output_file
[s
- spec
] = 0;
1024 /* Command line overrides environment variables. */
1026 deps_file
= output_file
;
1030 /* If dependencies go to standard output, or -MG is used, we should
1031 suppress output. The user may be requesting other stuff to
1032 stdout, with -dM, -v etc. We let them shoot themselves in the
1034 if (deps_file
== 0 || deps_missing_files
)
1038 /* Use mkdeps.c to output dependency information. */
1042 /* Stream on which to print the dependency information. */
1043 FILE *deps_stream
= 0;
1044 const char *const deps_mode
= deps_append
? "a" : "w";
1047 deps_stream
= stdout
;
1050 deps_stream
= fopen (deps_file
, deps_mode
);
1051 if (deps_stream
== 0)
1053 error_from_errno (deps_file
);
1058 deps_write (deps
, deps_stream
, 72);
1060 if (print_deps_phony_targets
)
1061 deps_phony_targets (deps
, deps_stream
);
1063 /* Don't close stdout. */
1066 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
1067 fatal ("I/O error on output");
1071 /* Move all backslash-newline pairs out of embarrassing places.
1072 Exchange all such pairs following BP
1073 with any potentially-embarrasing characters that follow them.
1074 Potentially-embarrassing characters are / and *
1075 (because a backslash-newline inside a comment delimiter
1076 would cause it not to be recognized). */
1084 /* First count the backslash-newline pairs here. */
1086 while (*p
++ == '\\' && *p
++ == '\n')
1091 /* Exit if what follows the backslash-newlines is not embarrassing. */
1093 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
1096 /* Copy all potentially embarrassing characters
1097 that follow the backslash-newline pairs
1098 down to where the pairs originally started. */
1100 while (*p
== '*' || *p
== '/')
1103 /* Now write the same number of pairs after the embarrassing chars. */
1104 while (count
-- > 0) {
1110 /* Like newline_fix but for use within a directive-name.
1111 Move any backslash-newlines up past any following symbol constituents. */
1113 name_newline_fix (bp
)
1119 /* First count the backslash-newline pairs here. */
1121 while (*p
++ == '\\' && *p
++ == '\n')
1126 /* What follows the backslash-newlines is not embarrassing. */
1128 if (count
== 0 || !is_idchar (*p
))
1131 /* Copy all potentially embarrassing characters
1132 that follow the backslash-newline pairs
1133 down to where the pairs originally started. */
1135 while (is_idchar (*p
))
1138 /* Now write the same number of pairs after the embarrassing chars. */
1139 while (count
-- > 0) {
1146 * The main loop of the program.
1148 * Read characters from the input stack, transferring them to the
1151 * Macros are expanded and push levels on the input stack.
1152 * At the end of such a level it is popped off and we keep reading.
1153 * At the end of any other kind of level, we return.
1154 * #-directives are handled, except within macros.
1156 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1157 * and insert them when appropriate. This is set while scanning macro
1158 * arguments before substitution. It is zero when scanning for final output.
1159 * There are three types of Newline markers:
1160 * * Newline - follows a macro name that was not expanded
1161 * because it appeared inside an expansion of the same macro.
1162 * This marker prevents future expansion of that identifier.
1163 * When the input is rescanned into the final output, these are deleted.
1164 * These are also deleted by ## concatenation.
1165 * * Newline Space (or Newline and any other whitespace character)
1166 * stands for a place that tokens must be separated or whitespace
1167 * is otherwise desirable, but where the ANSI standard specifies there
1168 * is no whitespace. This marker turns into a Space (or whichever other
1169 * whitespace char appears in the marker) in the final output,
1170 * but it turns into nothing in an argument that is stringified with #.
1171 * Such stringified arguments are the only place where the ANSI standard
1172 * specifies with precision that whitespace may not appear.
1174 * During this function, IP->bufp is kept cached in IBP for speed of access.
1175 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1176 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1177 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1178 * explicitly, and before RECACHE, since RECACHE uses OBP.
1182 rescan (op
, output_marks
)
1186 /* Character being scanned in main loop. */
1189 /* Length of pending accumulated identifier. */
1190 int ident_length
= 0;
1192 /* Hash code of pending accumulated identifier. */
1195 /* Current input level (&instack[indepth]). */
1198 /* Pointer for scanning input. */
1201 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1204 /* Pointer for storing output. */
1207 /* REDO_CHAR is nonzero if we are processing an identifier
1208 after backing up over the terminating character.
1209 Sometimes we process an identifier without backing up over
1210 the terminating character, if the terminating character
1211 is not special. Backing up is done so that the terminating character
1212 will be dispatched on again once the identifier is dealt with. */
1215 /* 1 if within an identifier inside of which a concatenation
1216 marker (Newline -) has been seen. */
1217 int concatenated
= 0;
1219 /* While scanning a comment or a string constant,
1220 this records the line it started on, for error messages. */
1223 /* Record position of last `real' newline. */
1224 U_CHAR
*beg_of_line
;
1226 /* This has to be a global bacause of RECACHE. */
1227 U_CHAR
*obufp_before_macroname
= NULL
;
1229 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1232 do { ip->macro->type = T_MACRO; \
1233 if (ip->free_ptr) free (ip->free_ptr); \
1234 --indepth; } while (0)
1236 /* Reload `rescan's local variables that describe the current
1237 level of the input stack. */
1240 do { ip = &instack[indepth]; \
1242 limit = ip->buf + ip->length; \
1244 check_expand (op, limit - ibp); \
1246 obufp_before_macroname += op->bufp - obp; \
1247 obp = op->bufp; } while (0)
1249 if (no_output
&& instack
[indepth
].fname
!= 0)
1250 skip_if_group (&instack
[indepth
], 1);
1256 /* Our caller must always put a null after the end of
1257 the input at each input stack level. */
1270 /* Always merge lines ending with backslash-newline,
1271 even in middle of identifier. */
1274 --obp
; /* remove backslash from obuf */
1277 /* Otherwise, backslash suppresses specialness of following char,
1278 so copy it here to prevent the switch from seeing it.
1279 But first get any pending identifier processed. */
1280 if (ident_length
> 0)
1286 /* If this is expanding a macro definition, don't recognize
1287 preprocessor directives. */
1293 /* # keyword: a # must be the first char on the line */
1294 if (beg_of_line
== 0)
1296 if (beg_of_line
+ 1 != ibp
)
1299 /* This # can start a directive. */
1301 --obp
; /* Don't copy the '#' */
1305 if (! handle_directive (ip
, op
)) {
1309 /* Not a known directive: treat it as ordinary text.
1310 IP, OP, IBP, etc. have not been changed. */
1311 if (no_output
&& instack
[indepth
].fname
) {
1312 /* If not generating expanded output,
1313 what we do with ordinary text is skip it.
1314 Discard everything until next # directive. */
1315 skip_if_group (&instack
[indepth
], 1);
1320 ++obp
; /* Copy the '#' after all */
1326 /* A # directive has been successfully processed. */
1327 /* If not generating expanded output, ignore everything until
1328 next # directive. */
1329 if (no_output
&& instack
[indepth
].fname
)
1330 skip_if_group (&instack
[indepth
], 1);
1336 case '\"': /* skip quoted string */
1338 /* A single quoted string is treated like a double -- some
1339 programs (e.g., troff) are perverse this way */
1344 start_line
= ip
->lineno
;
1346 /* Skip ahead to a matching quote. */
1350 if (ip
->macro
!= 0) {
1351 /* try harder: this string crosses a macro expansion boundary */
1363 /* Traditionally, end of line ends a string constant with no error.
1364 So exit the loop and record the new line. */
1372 /* Backslash newline is replaced by nothing at all,
1373 but keep the line counts correct. */
1378 /* ANSI stupidly requires that in \\ the second \
1379 is *not* prevented from combining with a newline. */
1380 while (*ibp
== '\\' && ibp
[1] == '\n') {
1399 if (*ibp
== '\\' && ibp
[1] == '\n')
1401 /* Don't look for comments inside a macro definition. */
1404 /* A comment constitutes white space, so it can terminate an identifier.
1405 Process the identifier, if any. */
1412 /* We have a comment. Skip it, optionally copying it to output. */
1414 start_line
= ip
->lineno
;
1416 ++ibp
; /* Skip the star. */
1418 /* In K+R C, a comment is equivalent to nothing. Note that we
1419 already output the slash; we might not want it. */
1420 if (! put_out_comments
)
1426 U_CHAR
*before_bp
= ibp
;
1428 while (ibp
< limit
) {
1431 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
1432 warning("`/*' within comment");
1435 if (*ibp
== '\\' && ibp
[1] == '\n')
1437 if (ibp
>= limit
|| *ibp
== '/')
1442 /* Copy the newline into the output buffer, in order to
1443 avoid the pain of a #line every time a multiline comment
1445 if (!put_out_comments
)
1453 error_with_line (line_for_error (start_line
),
1454 "unterminated comment");
1457 if (put_out_comments
) {
1458 memcpy (obp
, before_bp
, ibp
- before_bp
);
1459 obp
+= ibp
- before_bp
;
1465 case '0': case '1': case '2': case '3': case '4':
1466 case '5': case '6': case '7': case '8': case '9':
1467 /* If digit is not part of identifier, it starts a number,
1468 which means that following letters are not an identifier.
1469 "0x5" does not refer to an identifier "x5".
1470 So copy all alphanumerics that follow without accumulating
1471 as an identifier. Periods also, for sake of "3.e7". */
1473 if (ident_length
== 0) {
1474 while (ibp
< limit
) {
1475 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1480 if (! ISIDNUM (c
) && c
!= '.') {
1485 /* A sign can be part of a preprocessing number
1486 if it follows an e. */
1487 if (c
== 'e' || c
== 'E') {
1488 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1492 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
1494 /* Traditional C does not let the token go past the sign. */
1504 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1505 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1506 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1507 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1509 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1510 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1511 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1512 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1515 /* Compute step of hash function, to avoid a proc call on every token */
1516 hash
= HASHSTEP (hash
, c
);
1520 /* If reprocessing a macro expansion, newline is a special marker. */
1521 if (ip
->macro
!= 0) {
1522 /* Newline White is a "funny space" to separate tokens that are
1523 supposed to be separate but without space between.
1524 Here White means any horizontal whitespace character.
1525 Newline - marks a recursive macro use that is not
1526 supposed to be expandable. */
1529 /* Newline - inhibits expansion of preceding token.
1530 If expanding a macro arg, we keep the newline -.
1531 In final output, it is deleted. */
1532 if (! concatenated
) {
1537 if (!output_marks
) {
1540 /* If expanding a macro arg, keep the newline -. */
1543 } else if (is_space (*ibp
)) {
1544 /* Newline Space does not prevent expansion of preceding token
1545 so expand the preceding token and then come back. */
1546 if (ident_length
> 0)
1549 /* If generating final output, newline space makes a space. */
1550 if (!output_marks
) {
1552 /* And Newline Newline makes a newline, so count it. */
1553 if (obp
[-1] == '\n')
1556 /* If expanding a macro arg, keep the newline space.
1557 If the arg gets stringified, newline space makes nothing. */
1560 } else abort (); /* Newline followed by something random? */
1564 /* If there is a pending identifier, handle it and come back here. */
1565 if (ident_length
> 0)
1570 /* Update the line counts and output a #line if necessary. */
1573 if (ip
->lineno
!= op
->lineno
) {
1575 output_line_command (ip
, op
, 1, same_file
);
1576 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
1581 /* Come here either after (1) a null character that is part of the input
1582 or (2) at the end of the input, because there is a null there. */
1585 /* Our input really contains a null character. */
1588 /* At end of a macro-expansion level, pop it and read next level. */
1589 if (ip
->macro
!= 0) {
1592 /* If we have an identifier that ends here, process it now, so
1593 we get the right error for recursion. */
1594 if (ident_length
&& ! is_idchar (*instack
[indepth
- 1].bufp
)) {
1603 /* If we don't have a pending identifier,
1604 return at end of input. */
1605 if (ident_length
== 0) {
1613 /* If we do have a pending identifier, just consider this null
1614 a special character and arrange to dispatch on it again.
1615 The second time, IDENT_LENGTH will be zero so we will return. */
1621 /* Handle the case of a character such as /, ', " or null
1622 seen following an identifier. Back over it so that
1623 after the identifier is processed the special char
1624 will be dispatched on again. */
1634 if (ident_length
> 0) {
1637 /* We have just seen an identifier end. If it's a macro, expand it.
1639 IDENT_LENGTH is the length of the identifier
1640 and HASH is its hash code.
1642 The identifier has already been copied to the output,
1643 so if it is a macro we must remove it.
1645 If REDO_CHAR is 0, the char that terminated the identifier
1646 has been skipped in the output and the input.
1647 OBP-IDENT_LENGTH-1 points to the identifier.
1648 If the identifier is a macro, we must back over the terminator.
1650 If REDO_CHAR is 1, the terminating char has already been
1651 backed over. OBP-IDENT_LENGTH points to the identifier. */
1653 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
1656 if (hp
->length
== ident_length
) {
1657 /* obufp_before_macroname is used only in this block,
1658 but it has to be global because of RECACHE. */
1659 int op_lineno_before_macroname
;
1660 int i
= ident_length
;
1661 U_CHAR
*p
= hp
->name
;
1662 U_CHAR
*q
= obp
- i
;
1667 do { /* All this to avoid a strncmp () */
1672 /* We found a use of a macro name.
1673 see if the context shows it is a macro call. */
1675 /* Back up over terminating character if not already done. */
1681 obufp_before_macroname
= obp
- ident_length
;
1682 op_lineno_before_macroname
= op
->lineno
;
1684 /* If macro wants an arglist, verify that a '(' follows.
1685 first skip all whitespace, copying it to the output
1686 after the macro name. Then, if there is no '(',
1687 decide this is not a macro call and leave things that way. */
1688 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
1691 /* Scan forward over whitespace, copying it to the output. */
1692 if (ibp
== limit
&& ip
->macro
!= 0) {
1696 /* A comment: copy it unchanged or discard it. */
1697 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
1698 if (put_out_comments
) {
1703 while (ibp
+ 1 != limit
1704 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
1705 /* We need not worry about newline-marks,
1706 since they are never found in comments. */
1708 /* Newline in a file. Count it. */
1712 if (put_out_comments
)
1718 if (put_out_comments
) {
1723 else if (is_space (*ibp
)) {
1725 if (ibp
[-1] == '\n') {
1726 if (ip
->macro
== 0) {
1727 /* Newline in a file. Count it. */
1730 } else if (!output_marks
) {
1731 /* A newline mark, and we don't want marks
1732 in the output. If it is newline-hyphen,
1733 discard it entirely. Otherwise, it is
1734 newline-whitechar, so keep the whitechar. */
1744 /* A newline mark; copy both chars to the output. */
1755 /* This is now known to be a macro call.
1756 Discard the macro name from the output,
1757 along with any following whitespace just copied. */
1758 obp
= obufp_before_macroname
;
1759 op
->lineno
= op_lineno_before_macroname
;
1761 /* Expand the macro, reading arguments as needed,
1762 and push the expansion on the input stack. */
1765 macroexpand (hp
, op
);
1767 /* Reexamine input stack, since macroexpand has pushed
1768 a new level on it. */
1775 } /* End hash-table-search loop */
1776 ident_length
= hash
= 0; /* Stop collecting identifier */
1779 } /* End if (ident_length > 0) */
1781 } /* End per-char loop */
1783 /* Come here to return -- but first give an error message
1784 if there was an unterminated successful conditional. */
1786 if (if_stack
!= ip
->if_stack
) {
1788 switch (if_stack
->type
) {
1807 error_with_line (line_for_error (if_stack
->lineno
),
1808 "unterminated #%s conditional", str
);
1810 if_stack
= ip
->if_stack
;
1814 * Rescan a string into a temporary buffer and return the result
1815 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1817 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1818 * and insert such markers when appropriate. See `rescan' for details.
1819 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1820 * before substitution; it is 0 for other uses.
1823 expand_to_temp_buffer (buf
, limit
, output_marks
)
1824 const U_CHAR
*buf
, *limit
;
1829 int length
= limit
- buf
;
1831 int odepth
= indepth
;
1836 /* Set up the input on the input stack. */
1838 buf1
= (U_CHAR
*) alloca (length
+ 1);
1840 const U_CHAR
*p1
= buf
;
1848 /* Set up to receive the output. */
1850 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
1851 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
1856 CHECK_DEPTH ({return obuf
;});
1860 ip
= &instack
[indepth
];
1864 ip
->length
= length
;
1865 ip
->buf
= ip
->bufp
= buf1
;
1866 ip
->if_stack
= if_stack
;
1868 ip
->lineno
= obuf
.lineno
= 1;
1870 /* Scan the input, create the output. */
1872 rescan (&obuf
, output_marks
);
1874 /* Pop input stack to original state. */
1877 if (indepth
!= odepth
)
1880 /* Record the output. */
1881 obuf
.length
= obuf
.bufp
- obuf
.buf
;
1887 * Process a # directive. Expects IP->bufp to point to the '#', as in
1888 * `#define foo bar'. Passes to the command handler
1889 * (do_define, do_include, etc.): the addresses of the 1st and
1890 * last chars of the command (starting immediately after the #
1891 * keyword), plus op and the keyword table pointer. If the command
1892 * contains comments it is copied into a temporary buffer sans comments
1893 * and the temporary buffer is passed to the command handler instead.
1894 * Likewise for backslash-newlines.
1896 * Returns nonzero if this was a known # directive.
1897 * Otherwise, returns zero, without advancing the input pointer.
1901 handle_directive (ip
, op
)
1905 struct directive
*kt
;
1909 /* Nonzero means we must copy the entire command
1910 to get rid of comments or backslash-newlines. */
1911 int copy_command
= 0;
1913 U_CHAR
*ident
, *after_ident
;
1916 /* Skip whitespace and \-newline. */
1918 if (is_nvspace (*bp
))
1920 else if (*bp
== '/' && (newline_fix (bp
+ 1), bp
[1]) == '*') {
1922 skip_to_end_of_comment (ip
, &ip
->lineno
);
1924 } else if (*bp
== '\\' && bp
[1] == '\n') {
1925 bp
+= 2; ip
->lineno
++;
1929 /* Now find end of directive name.
1930 If we encounter a backslash-newline, exchange it with any following
1931 symbol-constituents so that we end up with a contiguous name. */
1935 if (is_idchar (*cp
))
1938 if (*cp
== '\\' && cp
[1] == '\n')
1939 name_newline_fix (cp
);
1940 if (is_idchar (*cp
))
1945 ident_length
= cp
- bp
;
1949 /* A line of just `#' becomes blank. */
1951 if (ident_length
== 0 && *after_ident
== '\n') {
1952 ip
->bufp
= after_ident
;
1957 * Decode the keyword and call the appropriate expansion
1958 * routine, after moving the input pointer up to the next line.
1960 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
1961 if (kt
->length
== ident_length
1962 && !strncmp (kt
->name
, (const char *)ident
, ident_length
)) {
1964 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
1965 int unterminated
= 0;
1967 /* Nonzero means do not delete comments within the directive.
1968 #define needs this to detect traditional token paste. */
1969 int keep_comments
= kt
->type
== T_DEFINE
;
1971 /* Find the end of this command (first newline not backslashed
1972 and not in a string or comment).
1973 Set COPY_COMMAND if the command must be copied
1974 (it contains a backslash-newline or a comment). */
1976 buf
= bp
= after_ident
;
1977 while (bp
< limit
) {
1992 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
1994 /* Traditional preprocessing permits unterminated strings. */
2000 /* <...> is special for #include. */
2002 if (kt
->type
!= T_INCLUDE
)
2004 while (*bp
&& *bp
!= '>') bp
++;
2008 if (*bp
== '\\' && bp
[1] == '\n')
2011 U_CHAR
*obp
= bp
- 1;
2013 skip_to_end_of_comment (ip
, &ip
->lineno
);
2015 /* No need to copy the command because of a comment at the end;
2016 just don't include the comment in the directive. */
2017 if (bp
== limit
|| *bp
== '\n') {
2021 /* Don't remove the comments if this is #define. */
2022 if (! keep_comments
)
2028 --bp
; /* Point to the newline */
2036 resume_p
= ip
->bufp
;
2037 /* BP is the end of the directive.
2038 RESUME_P is the next interesting data after the directive.
2039 A comment may come between. */
2043 /* Need to copy entire command into temp buffer before dispatching */
2045 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
2049 /* Copy to the new buffer, deleting comments
2050 and backslash-newlines (and whitespace surrounding the latter). */
2060 /* <...> is special for #include. */
2062 if (kt
->type
!= T_INCLUDE
)
2064 while (xp
< bp
&& c
!= '>') {
2066 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
2077 if (cp
!= buf
&& is_space (cp
[-1])) {
2078 while (cp
!= buf
&& is_space(cp
[-1])) cp
--;
2080 SKIP_WHITE_SPACE (xp
);
2081 } else if (is_nvspace (*xp
)) {
2083 SKIP_WHITE_SPACE (xp
);
2094 = skip_quoted_string (xp
- 1, limit
, ip
->lineno
, 0, 0, 0);
2103 skip_to_end_of_comment (ip
, 0);
2105 while (xp
!= ip
->bufp
)
2107 /* Delete the slash. */
2115 /* Null-terminate the copy. */
2122 ip
->bufp
= resume_p
;
2124 /* Call the appropriate command handler. buf now points to
2125 either the appropriate place in the input buffer, or to
2126 the temp buffer if it was necessary to make one. cp
2127 points to the first char after the contents of the (possibly
2128 copied) command, in either case. */
2129 (*kt
->func
) (buf
, cp
, op
);
2130 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2139 static const char *const
2140 monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2141 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2144 * expand things like __FILE__. Place the expansion into the output
2145 * buffer *without* rescanning.
2148 special_symbol (hp
, op
)
2156 FILE_BUF
*ip
= NULL
;
2157 static struct tm
*timebuf
= NULL
;
2159 int paren
= 0; /* For special `defined' keyword */
2161 for (i
= indepth
; i
>= 0; i
--)
2162 if (instack
[i
].fname
!= NULL
) {
2167 fatal ("not in any file?!");
2174 if (hp
->type
== T_FILE
)
2177 string
= instack
[0].fname
;
2181 char *tmp
= (char *) alloca (3 + strlen (string
));
2182 sprintf (tmp
, "\"%s\"", string
);
2191 case T_INCLUDE_LEVEL
:
2193 char *tmp
= (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2195 for (i
= indepth
; i
>= 0; i
--)
2196 if (instack
[i
].fname
!= NULL
)
2199 sprintf (tmp
, "%d", true_indepth
- 1);
2206 char *tmp
= (char *) alloca (3 + strlen (version_string
));
2207 sprintf (tmp
, "\"%s\"", version_string
);
2213 buf
= hp
->value
.cpval
;
2218 char *tmp
= (char *) alloca (10);
2219 sprintf (tmp
, "%d", ip
->lineno
);
2227 char *tmp
= (char *) alloca (20);
2229 if (timebuf
== NULL
) {
2231 timebuf
= localtime (&t
);
2233 if (hp
->type
== T_DATE
)
2234 sprintf (tmp
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2235 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2237 sprintf (tmp
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2243 case T_SPEC_DEFINED
:
2244 buf
= " 0 "; /* Assume symbol is not defined */
2245 ip
= &instack
[indepth
];
2246 SKIP_WHITE_SPACE (ip
->bufp
);
2247 if (*ip
->bufp
== '(') {
2249 ip
->bufp
++; /* Skip over the paren */
2250 SKIP_WHITE_SPACE (ip
->bufp
);
2253 if (!is_idstart (*ip
->bufp
))
2256 HASHNODE
*hp
= lookup (ip
->bufp
, -1, -1);
2258 if (hp
&& hp
->type
!= T_UNUSED
&& hp
->type
!= T_SPEC_DEFINED
)
2261 while (is_idchar (*ip
->bufp
))
2263 SKIP_WHITE_SPACE (ip
->bufp
);
2265 if (*ip
->bufp
!= ')')
2273 error ("`defined' must be followed by ident or (ident)");
2277 error ("cccp error: invalid special hash type"); /* time for gdb */
2281 check_expand (op
, len
);
2282 memcpy (op
->bufp
, buf
, len
);
2287 /* Routines to handle #directives */
2290 * Process include file by reading it in and calling rescan.
2291 * Expects to see "fname" or <fname> on the input.
2294 do_include (buf
, limit
, op
)
2295 U_CHAR
*buf
, *limit
;
2298 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2300 struct file_name_list
*stackp
= include
; /* Chain of dirs to search */
2301 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
2304 int retried
= 0; /* Have already tried macro
2305 expanding the include line*/
2306 FILE_BUF trybuf
; /* It got expanded into here */
2307 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2312 SKIP_WHITE_SPACE (fbeg
);
2313 /* Discard trailing whitespace so we can easily see
2314 if we have parsed all the significant chars we were given. */
2315 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2320 while (fend
!= limit
&& *fend
!= '\"')
2322 if (*fend
== '\"' && fend
+ 1 == limit
) {
2325 /* We have "filename". Figure out directory this source
2326 file is coming from and put it on the front of the list. */
2328 /* If -I- was specified, don't search current dir, only spec'd ones. */
2329 if (ignore_srcdir
) break;
2331 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
2334 const char *ep
, *nam
;
2336 if ((nam
= fp
->fname
) != NULL
) {
2337 /* Found a named file. Figure out dir of the file,
2338 and put it in front of the search list. */
2339 dsp
[0].next
= stackp
;
2341 ep
= strrchr (nam
, '/');
2345 f
= (char *) alloca (n
+ 1);
2346 strncpy (f
, nam
, n
);
2349 if (n
> max_include_len
) max_include_len
= n
;
2351 dsp
[0].fname
= 0; /* Current directory */
2362 while (fend
!= limit
&& *fend
!= '>') fend
++;
2363 if (*fend
== '>' && fend
+ 1 == limit
) {
2364 system_header_p
= 1;
2365 /* If -I-, start with the first -I dir after the -I-. */
2366 if (first_bracket_include
)
2367 stackp
= first_bracket_include
;
2375 error ("#include expects \"fname\" or <fname>");
2378 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2379 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2380 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2381 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2389 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2393 do_include_next (buf
, limit
, op
)
2394 U_CHAR
*buf
, *limit
;
2397 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2399 struct file_name_list
*stackp
; /* Chain of dirs to search */
2402 int retried
= 0; /* Have already tried macro
2403 expanding the include line*/
2404 FILE_BUF trybuf
; /* It got expanded into here */
2405 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2407 /* Treat as plain #include if we don't know where to start
2409 stackp
= instack
[indepth
].next_header_dir
;
2412 do_include (buf
, limit
, op
);
2419 SKIP_WHITE_SPACE (fbeg
);
2420 /* Discard trailing whitespace so we can easily see
2421 if we have parsed all the significant chars we were given. */
2422 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2427 while (fend
!= limit
&& *fend
!= '\"')
2429 if (*fend
== '\"' && fend
+ 1 == limit
)
2435 while (fend
!= limit
&& *fend
!= '>') fend
++;
2436 if (*fend
== '>' && fend
+ 1 == limit
) {
2437 system_header_p
= 1;
2445 error ("#include expects \"fname\" or <fname>");
2448 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2449 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2450 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2451 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2459 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2463 process_include (stackp
, fbeg
, flen
, system_header_p
, op
)
2464 struct file_name_list
*stackp
;
2467 int system_header_p
;
2471 int f
= -1; /* file number */
2473 fname
= (char *) alloca (max_include_len
+ flen
+ 2);
2474 /* + 2 above for slash and terminating null. */
2476 /* If specified file name is absolute, just open it. */
2478 if (IS_ABSOLUTE_PATHNAME (fbeg
)) {
2479 strncpy (fname
, (const char *)fbeg
, flen
);
2481 f
= open (fname
, O_RDONLY
, 0666);
2483 /* Search directory path, trying to open the file.
2484 Copy each filename tried into FNAME. */
2486 for (; stackp
; stackp
= stackp
->next
) {
2487 if (stackp
->fname
) {
2488 strcpy (fname
, stackp
->fname
);
2489 strcat (fname
, "/");
2490 fname
[strlen (fname
) + flen
] = 0;
2494 strncat (fname
, (const char *)fbeg
, flen
);
2495 if ((f
= open (fname
, O_RDONLY
, 0666)) >= 0)
2501 strncpy (fname
, (const char *)fbeg
, flen
);
2503 if (deps_missing_files
2504 && print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2506 /* If requested as a system header, assume it belongs in
2507 the first system header directory. */
2508 if (first_bracket_include
)
2509 stackp
= first_bracket_include
;
2513 if (!system_header_p
|| IS_ABSOLUTE_PATHNAME (fbeg
) || !stackp
->fname
)
2514 deps_add_dep (deps
, fname
);
2517 int len
= strlen(stackp
->fname
);
2519 p
= (char *) alloca (len
+ flen
+ 2);
2520 memcpy (p
, stackp
->fname
, len
);
2522 memcpy (p
+ len
, fbeg
, flen
);
2525 deps_add_dep (deps
, p
);
2527 } else if (print_deps
2528 && print_deps
<= (system_header_p
2529 || (system_include_depth
> 0)))
2530 warning ("no include path in which to find %.*s", flen
, fbeg
);
2532 error_from_errno (fname
);
2536 /* Check to see if this include file is a once-only include file.
2539 struct file_name_list
* ptr
;
2541 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
2542 if (!strcmp (ptr
->fname
, fname
)) {
2544 return; /* This file was once'd. */
2548 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
2549 if (!strcmp (ptr
->fname
, fname
))
2550 break; /* This file was included before. */
2554 /* This is the first time for this file. */
2555 /* Add it to list of files included. */
2557 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
2558 ptr
->next
= all_include_files
;
2559 all_include_files
= ptr
;
2560 ptr
->fname
= xstrdup (fname
);
2562 /* For -M, add this file to the dependencies. */
2563 if (print_deps
> (system_header_p
|| (system_include_depth
> 0)))
2564 deps_add_dep (deps
, fname
);
2567 if (system_header_p
)
2568 system_include_depth
++;
2570 /* Actually process the file. */
2571 finclude (f
, fname
, stackp
->next
, op
);
2573 if (system_header_p
)
2574 system_include_depth
--;
2580 /* Process the contents of include file FNAME, already open on descriptor F,
2581 with output to OP. */
2584 finclude (f
, fname
, nhd
, op
)
2587 struct file_name_list
*nhd
;
2593 FILE_BUF
*fp
; /* For input stack frame */
2595 CHECK_DEPTH (return;);
2597 if (file_size_and_mode (f
, &st_mode
, &st_size
))
2600 fp
= &instack
[indepth
+ 1];
2601 memset (fp
, 0, sizeof (FILE_BUF
));
2605 fp
->if_stack
= if_stack
;
2606 fp
->next_header_dir
= nhd
;
2608 if (S_ISREG (st_mode
)) {
2609 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
2612 /* Read the file contents, knowing that st_size is an upper bound
2613 on the number of bytes we can read. */
2614 while (st_size
> 0) {
2615 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
2625 /* Cannot count its file size before reading. */
2632 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
2636 i
= read (f
, bufp
, bsize
- st_size
);
2638 goto nope
; /* error! */
2640 break; /* End of file */
2643 if (bsize
== st_size
) { /* Buffer is full! */
2645 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
2646 bufp
= basep
+ st_size
; /* May have moved */
2651 fp
->length
= st_size
;
2655 /* Make sure data ends with a newline. And put a null after it. */
2657 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
2658 fp
->buf
[fp
->length
++] = '\n';
2659 fp
->buf
[fp
->length
] = '\0';
2662 output_line_command (fp
, op
, 0, enter_file
);
2665 instack
[indepth
].lineno
++;
2666 instack
[indepth
].bufp
++; /* Skip the new line. */
2667 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
2672 perror_with_name (fname
);
2677 /* Process a #define command.
2678 BUF points to the contents of the #define command, as a continguous string.
2679 LIMIT points to the first character past the end of the definition.
2680 KEYWORD is the keyword-table entry for #define. */
2683 do_define (buf
, limit
, op
)
2684 U_CHAR
*buf
, *limit
;
2685 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2687 U_CHAR
*bp
; /* temp ptr into input buffer */
2688 U_CHAR
*symname
; /* remember where symbol name starts */
2689 int sym_length
; /* and how long it is */
2692 int arglengths
= 0; /* Accumulate lengths of arg names
2693 plus number of args. */
2698 while (is_nvspace (*bp
))
2701 symname
= bp
; /* remember where it starts */
2702 while (is_idchar (*bp
) && bp
< limit
) {
2705 sym_length
= bp
- symname
;
2706 if (sym_length
== 0)
2708 error ("invalid macro name");
2711 else if (!is_idstart (*symname
)) {
2712 U_CHAR
*msg
; /* what pain... */
2713 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
2714 memcpy (msg
, symname
, sym_length
);
2715 msg
[sym_length
] = 0;
2716 error ("invalid macro name `%s'", msg
);
2719 if (! strncmp ((const char *)symname
, "defined", 7) && sym_length
== 7)
2721 error ("\"defined\" cannot be used as a macro name");
2726 /* lossage will occur if identifiers or control keywords are broken
2727 across lines using backslash. This is not the right place to take
2731 struct arglist
*arg_ptrs
= NULL
;
2734 bp
++; /* skip '(' */
2735 SKIP_WHITE_SPACE (bp
);
2737 /* Loop over macro argument names. */
2738 while (*bp
!= ')') {
2739 struct arglist
*temp
;
2741 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
2743 temp
->next
= arg_ptrs
;
2744 temp
->argno
= argno
++;
2747 if (!is_idstart (*bp
))
2748 warning ("parameter name starts with a digit in #define");
2750 /* Find the end of the arg name. */
2751 while (is_idchar (*bp
)) {
2754 temp
->length
= bp
- temp
->name
;
2755 arglengths
+= temp
->length
+ 2;
2756 SKIP_WHITE_SPACE (bp
);
2757 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
2758 error ("badly punctuated parameter list in #define");
2763 SKIP_WHITE_SPACE (bp
);
2766 error ("unterminated parameter list in #define");
2771 ++bp
; /* skip paren */
2772 while (is_nvspace (*bp
) && bp
< limit
) /* and leading whitespace */
2774 /* now everything from bp before limit is the definition. */
2775 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
2777 /* Now set defn->argnames to the result of concatenating
2778 the argument names in reverse order
2779 with comma-space between them. */
2781 struct arglist
*temp
;
2783 U_CHAR
*tmp
= (U_CHAR
*) xmalloc (arglengths
+ 1);
2785 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
2786 memcpy (&tmp
[i
], temp
->name
, temp
->length
);
2788 if (temp
->next
!= 0) {
2794 defn
->argnames
= tmp
;
2798 /* simple expansion or empty definition; skip leading whitespace */
2799 while (is_nvspace (*bp
) && bp
< limit
)
2801 /* now everything from bp before limit is the definition. */
2802 defn
= collect_expansion (bp
, limit
, -1, 0);
2803 defn
->argnames
= (const U_CHAR
*) "";
2806 hashcode
= hashf (symname
, sym_length
, HASHSIZE
);
2810 if ((hp
= lookup (symname
, sym_length
, hashcode
)) == NULL
)
2811 hp
= install (symname
, sym_length
, T_MACRO
, hashcode
);
2813 if (hp
->type
!= T_MACRO
|| compare_defs (defn
, hp
->value
.defn
))
2814 warning ("\"%.*s\" redefined", sym_length
, symname
);
2816 /* Replace the old definition. */
2820 hp
->value
.defn
= defn
;
2825 * return zero if two DEFINITIONs are isomorphic
2828 compare_defs (d1
, d2
)
2829 DEFINITION
*d1
, *d2
;
2831 struct reflist
*a1
, *a2
;
2832 U_CHAR
*p1
= d1
->expansion
;
2833 U_CHAR
*p2
= d2
->expansion
;
2836 if (d1
->nargs
!= d2
->nargs
)
2838 if (strcmp ((const char *)d1
->argnames
, (const char *)d2
->argnames
))
2840 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
2841 a1
= a1
->next
, a2
= a2
->next
) {
2842 if (!((a1
->nchars
== a2
->nchars
2843 && ! strncmp ((const char *)p1
, (const char *)p2
, a1
->nchars
))
2844 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
2845 || a1
->argno
!= a2
->argno
2846 || a1
->stringify
!= a2
->stringify
2847 || a1
->raw_before
!= a2
->raw_before
2848 || a1
->raw_after
!= a2
->raw_after
)
2856 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
2857 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
2862 /* Return 1 if two parts of two macro definitions are effectively different.
2863 One of the parts starts at BEG1 and has LEN1 chars;
2864 the other has LEN2 chars at BEG2.
2865 Any sequence of whitespace matches any other sequence of whitespace.
2866 FIRST means these parts are the first of a macro definition;
2867 so ignore leading whitespace entirely.
2868 LAST means these parts are the last of a macro definition;
2869 so ignore trailing whitespace entirely. */
2871 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
2873 const U_CHAR
*beg1
, *beg2
;
2877 const U_CHAR
*end1
= beg1
+ len1
;
2878 const U_CHAR
*end2
= beg2
+ len2
;
2880 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2881 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2884 while (beg1
!= end1
&& is_space (end1
[-1])) end1
--;
2885 while (beg2
!= end2
&& is_space (end2
[-1])) end2
--;
2887 while (beg1
!= end1
&& beg2
!= end2
) {
2888 if (is_space (*beg1
) && is_space (*beg2
)) {
2889 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2890 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2891 } else if (*beg1
== *beg2
) {
2895 return (beg1
!= end1
) || (beg2
!= end2
);
2898 /* Read a replacement list for a macro with parameters.
2899 Build the DEFINITION structure.
2900 Reads characters of text starting at BUF until LIMIT.
2901 ARGLIST specifies the formal parameters to look for
2902 in the text of the definition; NARGS is the number of args
2903 in that list, or -1 for a macro name that wants no argument list.
2904 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2905 and NAMELEN is its length in characters.
2907 Note that comments and backslash-newlines have already been deleted
2908 from the argument. */
2910 /* Leading and trailing Space, Tab, etc. are converted to markers
2911 Newline Space, Newline Tab, etc.
2912 Newline Space makes a space in the final output
2913 but is discarded if stringified. (Newline Tab is similar but
2914 makes a Tab instead.)
2916 If there is no trailing whitespace, a Newline Space is added at the end
2917 to prevent concatenation that would be contrary to the standard. */
2920 collect_expansion (buf
, end
, nargs
, arglist
)
2923 struct arglist
*arglist
;
2926 U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
2927 struct reflist
*endpat
= NULL
;
2928 /* Pointer to first nonspace after last ## seen. */
2930 /* Pointer to first nonspace after last single-# seen. */
2931 U_CHAR
*stringify
= 0;
2933 int expected_delimiter
= '\0';
2935 /* Scan thru the replacement list, ignoring comments and quoted
2936 strings, picking up on the macro calls. It does a linear search
2937 thru the arg list on every potential symbol. Profiling might say
2938 that something smarter should happen. */
2943 /* Find the beginning of the trailing whitespace. */
2944 /* Find end of leading whitespace. */
2947 while (p
< limit
&& is_space (limit
[-1])) limit
--;
2948 while (p
< limit
&& is_space (*p
)) p
++;
2950 /* Allocate space for the text in the macro definition.
2951 Leading and trailing whitespace chars need 2 bytes each.
2952 Each other input char may or may not need 1 byte,
2953 so this is an upper bound.
2954 The extra 2 are for invented trailing newline-marker and final null. */
2955 maxsize
= (sizeof (DEFINITION
)
2956 + 2 * (end
- limit
) + 2 * (p
- buf
)
2958 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
2960 defn
->nargs
= nargs
;
2961 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
2966 /* Convert leading whitespace to Newline-markers. */
2967 while (p
< limit
&& is_space (*p
)) {
2972 /* Process the main body of the definition. */
2974 int skipped_arg
= 0;
2979 /* In -traditional mode, recognize arguments inside strings and
2980 and character constants, and ignore special properties of #.
2981 Arguments inside strings are considered "stringified", but no
2982 extra quote marks are supplied. */
2986 if (expected_delimiter
!= '\0') {
2987 if (c
== expected_delimiter
)
2988 expected_delimiter
= '\0';
2990 expected_delimiter
= c
;
2994 /* Backslash quotes delimiters and itself, but not macro args. */
2995 if (expected_delimiter
!= 0 && p
< limit
2996 && (*p
== expected_delimiter
|| *p
== '\\')) {
3003 if (expected_delimiter
!= '\0') /* No comments inside strings. */
3006 /* If we find a comment that wasn't removed by handle_directive,
3007 this must be -traditional. So replace the comment with
3011 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
3017 if (is_idchar (c
) && nargs
> 0) {
3018 U_CHAR
*id_beg
= p
- 1;
3022 while (p
!= limit
&& is_idchar (*p
)) p
++;
3023 id_len
= p
- id_beg
;
3025 if (is_idstart (c
)) {
3026 struct arglist
*arg
;
3028 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
3029 struct reflist
*tpat
;
3031 if (arg
->name
[0] == c
3032 && arg
->length
== id_len
3033 && strncmp ((const char *)arg
->name
,
3034 (const char *)id_beg
, id_len
) == 0) {
3035 /* make a pat node for this arg and append it to the end of
3037 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
3039 tpat
->raw_before
= concat
== id_beg
;
3040 tpat
->raw_after
= 0;
3041 tpat
->stringify
= expected_delimiter
!= '\0';
3044 defn
->pattern
= tpat
;
3046 endpat
->next
= tpat
;
3049 tpat
->argno
= arg
->argno
;
3050 tpat
->nchars
= exp_p
- lastp
;
3053 SKIP_WHITE_SPACE (p1
);
3054 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
3055 tpat
->raw_after
= 1;
3057 lastp
= exp_p
; /* place to start copying from next time */
3064 /* If this was not a macro arg, copy it into the expansion. */
3065 if (! skipped_arg
) {
3070 if (stringify
== id_beg
)
3071 error ("# operator should be followed by a macro argument name");
3077 /* Convert trailing whitespace to Newline-markers. */
3078 while (limit
< end
&& is_space (*limit
)) {
3080 *exp_p
++ = *limit
++;
3085 defn
->length
= exp_p
- defn
->expansion
;
3087 /* Crash now if we overrun the allocated size. */
3088 if (defn
->length
+ 1 > maxsize
)
3095 * interpret #line command. Remembers previously seen fnames
3096 * in its very own hash table.
3098 #define FNAME_HASHSIZE 37
3100 do_line (buf
, limit
, op
)
3101 U_CHAR
*buf
, *limit
;
3105 FILE_BUF
*ip
= &instack
[indepth
];
3108 enum file_change_code file_change
= same_file
;
3110 /* Expand any macros. */
3111 tem
= expand_to_temp_buffer (buf
, limit
, 0);
3113 /* Point to macroexpanded line, which is null-terminated now. */
3115 SKIP_WHITE_SPACE (bp
);
3117 if (!ISDIGIT (*bp
)) {
3118 error ("invalid format #line command");
3122 /* The Newline at the end of this line remains to be processed.
3123 To put the next line at the specified line number,
3124 we must store a line number now that is one less. */
3125 new_lineno
= atoi ((const char *)bp
);
3127 /* skip over the line number. */
3128 while (ISDIGIT (*bp
))
3131 SKIP_WHITE_SPACE (bp
);
3134 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3135 HASHNODE
*hp
, **hash_bucket
;
3141 while (*bp
&& *bp
!= '\"')
3144 error ("invalid format #line command");
3148 fname_length
= bp
- fname
;
3151 SKIP_WHITE_SPACE (bp
);
3154 file_change
= enter_file
;
3155 else if (*bp
== '2')
3156 file_change
= leave_file
;
3158 error ("invalid format #line command");
3163 SKIP_WHITE_SPACE (bp
);
3165 error ("invalid format #line command");
3171 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3172 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3173 if (hp
->length
== fname_length
&&
3174 strncmp (hp
->value
.cpval
, (const char *)fname
, fname_length
) == 0) {
3175 ip
->fname
= hp
->value
.cpval
;
3180 /* Didn't find it; cons up a new one. */
3181 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3182 hp
->next
= *hash_bucket
;
3185 hp
->length
= fname_length
;
3186 ip
->fname
= hp
->value
.cpval
= q
= ((char *) hp
) + sizeof (HASHNODE
);
3187 memcpy (q
, fname
, fname_length
);
3190 error ("invalid format #line command");
3194 ip
->lineno
= new_lineno
;
3195 output_line_command (ip
, op
, 0, file_change
);
3196 ip
->bufp
++; /* Skip the new line. */
3197 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3201 * remove all definitions of symbol from symbol table.
3202 * according to un*x /lib/cpp, it is not an error to undef
3203 * something that has no definitions, so it isn't one here either.
3206 do_undef (buf
, limit
, op
)
3208 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3209 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3213 SKIP_WHITE_SPACE (buf
);
3215 if (! strncmp ((const char *)buf
, "defined", 7) && ! is_idchar (buf
[7]))
3216 warning ("undefining `defined'");
3218 while ((hp
= lookup (buf
, -1, -1)) != NULL
) {
3219 if (hp
->type
!= T_MACRO
)
3220 warning ("undefining `%s'", hp
->name
);
3225 /* Read the tokens of the answer into the macro pool. Only commit the
3226 memory if we intend it as permanent storage, i.e. the #assert case.
3227 Returns 0 on success. */
3230 parse_answer (buf
, limit
, answerp
, type
)
3231 const unsigned char *buf
, *limit
;
3232 struct answer
**answerp
;
3235 const unsigned char *start
;
3237 /* Skip leading whitespace. */
3238 if (buf
< limit
&& *buf
== ' ')
3241 /* Parentheses are optional here. */
3242 if (buf
== limit
&& type
== T_UNASSERT
)
3245 if (buf
== limit
|| *buf
++ != '(')
3250 error ("missing '(' after predicate");
3254 /* Drop whitespace at start. */
3255 while (buf
< limit
&& *buf
== ' ')
3259 while (buf
< limit
&& *buf
!= ')')
3264 error ("missing ')' to complete answer");
3270 error ("predicate's answer is empty");
3274 if ((type
== T_ASSERT
|| type
== T_UNASSERT
) && buf
+ 1 != limit
)
3276 error ("extra text at end of directive");
3280 /* Lose trailing whitespace. */
3284 *answerp
= (struct answer
*) xmalloc (sizeof (struct answer
));
3285 (*answerp
)->answer
= start
;
3286 (*answerp
)->len
= buf
- start
;
3291 /* Parses an assertion, returning a pointer to the hash node of the
3292 predicate, or 0 on error. If an answer was supplied, it is placed
3293 in ANSWERP, otherwise it is set to 0. */
3295 parse_assertion (buf
, limit
, answerp
, type
)
3296 const unsigned char *buf
, *limit
;
3297 struct answer
**answerp
;
3300 HASHNODE
*result
= 0;
3301 const unsigned char *climit
;
3302 unsigned char *bp
, *symname
= canonicalize_text (buf
, limit
, &climit
);
3306 if (bp
< climit
&& is_idstart (*bp
))
3310 while (bp
< climit
&& is_idchar (*bp
));
3317 if (symname
== climit
)
3318 error ("assertion without predicate");
3320 error ("predicate must be an identifier");
3322 /* Unfortunately, because of the way we handle #if, we don't avoid
3323 macro expansion in answers. This is not easy to fix. */
3324 else if (parse_answer (bp
, climit
, answerp
, type
) == 0)
3326 unsigned char *sym
= alloca (len
+ 1);
3329 /* Prefix '#' to get it out of macro namespace. */
3331 memcpy (sym
+ 1, symname
, len
);
3333 hashcode
= hashf (sym
, len
+ 1, HASHSIZE
);
3334 result
= lookup (sym
, len
+ 1, hashcode
);
3336 result
= install (sym
, len
+ 1, T_UNUSED
, hashcode
);
3342 /* Test an assertion within a preprocessor conditional. Returns zero
3343 on error or failure, one on success. */
3345 test_assertion (pbuf
)
3346 unsigned char **pbuf
; /* NUL-terminated. */
3348 unsigned char *buf
= *pbuf
;
3349 unsigned char *limit
= buf
+ strlen ((char *) buf
);
3350 struct answer
*answer
;
3354 node
= parse_assertion (buf
, limit
, &answer
, T_IF
);
3357 result
= (node
->type
== T_ASSERT
&&
3358 (answer
== 0 || *find_answer (node
, answer
) != 0));
3360 /* Yuk. We update pbuf to point after the assertion test.
3361 First, move past the identifier. */
3362 if (is_space (*buf
))
3364 while (is_idchar (*buf
))
3366 /* If we have an answer, we need to move past the parentheses. */
3368 while (*buf
++ != ')')
3376 /* Handle a #error directive. */
3378 do_error (buf
, limit
, op
)
3381 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3383 error ("#error%.*s", (int) (limit
- buf
), buf
);
3386 /* Handle a #warning directive. */
3388 do_warning (buf
, limit
, op
)
3391 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3393 warning ("#warning%.*s", (int) (limit
- buf
), buf
);
3396 /* Handle a #assert directive. */
3398 do_assert (buf
, limit
, op
)
3401 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3403 struct answer
*new_answer
;
3406 node
= parse_assertion (buf
, limit
, &new_answer
, T_ASSERT
);
3409 /* Place the new answer in the answer list. First check there
3410 is not a duplicate. */
3411 new_answer
->next
= 0;
3412 if (node
->type
== T_ASSERT
)
3414 if (*find_answer (node
, new_answer
))
3417 warning ("\"%s\" re-asserted", node
->name
+ 1);
3420 new_answer
->next
= node
->value
.answers
;
3422 node
->type
= T_ASSERT
;
3423 node
->value
.answers
= new_answer
;
3427 /* Function body to be provided later. */
3429 do_unassert (buf
, limit
, op
)
3432 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3435 struct answer
*answer
;
3437 node
= parse_assertion (buf
, limit
, &answer
, T_UNASSERT
);
3438 /* It isn't an error to #unassert something that isn't asserted. */
3441 if (node
->type
== T_ASSERT
)
3445 struct answer
**p
= find_answer (node
, answer
), *temp
;
3447 /* Remove the answer from the list. */
3452 /* Did we free the last answer? */
3453 if (node
->value
.answers
== 0)
3454 delete_macro (node
);
3457 delete_macro (node
);
3464 /* Returns a pointer to the pointer to the answer in the answer chain,
3465 or a pointer to NULL if the answer is not in the chain. */
3466 static struct answer
**
3467 find_answer (node
, candidate
)
3469 const struct answer
*candidate
;
3471 struct answer
**result
;
3473 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
3475 struct answer
*answer
= *result
;
3477 if (answer
->len
== candidate
->len
3478 && !memcmp (answer
->answer
, candidate
->answer
, answer
->len
))
3485 /* Return a malloced buffer with leading and trailing whitespace
3486 removed, and all instances of internal whitespace reduced to a
3488 static unsigned char *
3489 canonicalize_text (buf
, limit
, climit
)
3490 const unsigned char *buf
, *limit
, **climit
;
3492 unsigned int len
= limit
- buf
;
3493 unsigned char *result
= (unsigned char *) xmalloc (len
), *dest
;
3495 for (dest
= result
; buf
< limit
;)
3497 if (! is_space (*buf
))
3501 while (++buf
< limit
&& is_space (*buf
))
3503 if (dest
!= result
&& buf
!= limit
)
3513 * handle #if command by
3514 * 1) inserting special `defined' keyword into the hash table
3515 * that gets turned into 0 or 1 by special_symbol (thus,
3516 * if the luser has a symbol called `defined' already, it won't
3517 * work inside the #if command)
3518 * 2) rescan the input into a temporary output buffer
3519 * 3) pass the output buffer to the yacc parser and collect a value
3520 * 4) clean up the mess left from steps 1 and 2.
3521 * 5) call conditional_skip to skip til the next #endif (etc.),
3522 * or not, depending on the value from step 3.
3525 do_if (buf
, limit
, op
)
3526 U_CHAR
*buf
, *limit
;
3527 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3530 FILE_BUF
*ip
= &instack
[indepth
];
3532 value
= eval_if_expression (buf
, limit
- buf
);
3533 conditional_skip (ip
, value
== 0, T_IF
);
3537 * handle a #elif directive by not changing if_stack either.
3538 * see the comment above do_else.
3541 do_elif (buf
, limit
, op
)
3542 U_CHAR
*buf
, *limit
;
3546 FILE_BUF
*ip
= &instack
[indepth
];
3548 if (if_stack
== instack
[indepth
].if_stack
) {
3549 error ("#elif not within a conditional");
3552 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3553 error ("#elif after #else");
3554 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3555 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
3556 strcmp (if_stack
->fname
, ip
->fname
) != 0)
3557 fprintf (stderr
, ", file %s", if_stack
->fname
);
3558 fprintf (stderr
, ")\n");
3560 if_stack
->type
= T_ELIF
;
3563 if (if_stack
->if_succeeded
)
3564 skip_if_group (ip
, 0);
3566 value
= eval_if_expression (buf
, limit
- buf
);
3568 skip_if_group (ip
, 0);
3570 ++if_stack
->if_succeeded
; /* continue processing input */
3571 output_line_command (ip
, op
, 1, same_file
);
3577 * evaluate a #if expression in BUF, of length LENGTH,
3578 * then parse the result as a C expression and return the value as an int.
3581 eval_if_expression (buf
, length
)
3586 HASHNODE
*save_defined
;
3589 save_defined
= install (U
"defined", -1, T_SPEC_DEFINED
, -1);
3590 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0);
3591 delete_macro (save_defined
); /* clean up special symbol */
3593 value
= parse_c_expression ((const char *)temp_obuf
.buf
);
3595 free (temp_obuf
.buf
);
3601 * routine to handle ifdef/ifndef. Try to look up the symbol,
3602 * then do or don't skip to the #endif/#else/#elif depending
3603 * on what directive is actually being processed.
3606 do_xifdef (buf
, limit
, type
)
3607 U_CHAR
*buf
, *limit
;
3608 enum node_type type
;
3611 FILE_BUF
*ip
= &instack
[indepth
];
3614 /* Discard leading and trailing whitespace. */
3615 SKIP_WHITE_SPACE (buf
);
3616 while (limit
!= buf
&& is_nvspace (limit
[-1])) limit
--;
3618 /* Find the end of the identifier at the beginning. */
3619 for (end
= buf
; is_idchar (*end
); end
++);
3622 skip
= (type
== T_IFDEF
);
3624 skip
= (lookup (buf
, end
-buf
, -1) == NULL
) ^ (type
== T_IFNDEF
);
3626 conditional_skip (ip
, skip
, T_IF
);
3630 do_ifdef (buf
, limit
, op
)
3631 U_CHAR
*buf
, *limit
;
3632 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3634 do_xifdef (buf
, limit
, T_IFDEF
);
3638 do_ifndef (buf
, limit
, op
)
3639 U_CHAR
*buf
, *limit
;
3640 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3642 do_xifdef (buf
, limit
, T_IFNDEF
);
3646 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3649 conditional_skip (ip
, skip
, type
)
3652 enum node_type type
;
3654 IF_STACK_FRAME
*temp
;
3656 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3657 temp
->fname
= ip
->fname
;
3658 temp
->lineno
= ip
->lineno
;
3659 temp
->next
= if_stack
;
3662 if_stack
->type
= type
;
3665 skip_if_group (ip
, 0);
3668 ++if_stack
->if_succeeded
;
3669 output_line_command (ip
, &outbuf
, 1, same_file
);
3674 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3675 * leaves input ptr at the sharp sign found.
3676 * If ANY is nonzero, return at next directive of any sort.
3679 skip_if_group (ip
, any
)
3683 U_CHAR
*bp
= ip
->bufp
, *cp
;
3684 U_CHAR
*endb
= ip
->buf
+ ip
->length
;
3685 struct directive
*kt
;
3686 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
3687 U_CHAR
*beg_of_line
= bp
;
3691 case '/': /* possible comment */
3692 if (*bp
== '\\' && bp
[1] == '\n')
3696 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
);
3701 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
3704 /* Char after backslash loses its special meaning. */
3707 ++ip
->lineno
; /* But do update the line-count. */
3718 /* # keyword: a # must be first nonblank char on the line */
3719 if (beg_of_line
== 0)
3721 /* Scan from start of line, skipping whitespace, comments
3722 and backslash-newlines, and see if we reach this #.
3723 If not, this # is not special. */
3726 if (is_nvspace (*bp
))
3728 else if (*bp
== '\\' && bp
[1] == '\n')
3730 else if (*bp
== '/' && bp
[1] == '*') {
3732 while (!(*bp
== '*' && bp
[1] == '/')) {
3741 if (bp
!= ip
->bufp
) {
3742 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
3746 bp
= ip
->bufp
+ 1; /* Point after '#'. */
3748 /* Skip whitespace and \-newline. */
3750 if (is_nvspace (*bp
))
3752 else if (*bp
== '\\' && bp
[1] == '\n')
3754 else if (*bp
== '/' && bp
[1] == '*') {
3756 while (!(*bp
== '*' && bp
[1] == '/'))
3765 /* Now find end of directive name.
3766 If we encounter a backslash-newline, exchange it with any following
3767 symbol-constituents so that we end up with a contiguous name. */
3770 if (is_idchar (*bp
))
3773 if (*bp
== '\\' && bp
[1] == '\n')
3774 name_newline_fix (bp
);
3775 if (is_idchar (*bp
))
3781 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
3782 IF_STACK_FRAME
*temp
;
3783 if (strncmp ((const char *)cp
, kt
->name
, kt
->length
) == 0
3784 && !is_idchar (cp
[kt
->length
])) {
3786 /* If we are asked to return on next directive,
3795 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3796 temp
->next
= if_stack
;
3798 temp
->lineno
= ip
->lineno
;
3799 temp
->fname
= ip
->fname
;
3800 temp
->type
= kt
->type
;
3805 if (if_stack
== instack
[indepth
].if_stack
) {
3806 error ("#%s not within a conditional", kt
->name
);
3809 else if (if_stack
== save_if_stack
)
3810 return; /* found what we came for */
3812 if (kt
->type
!= T_ENDIF
) {
3813 if (if_stack
->type
== T_ELSE
)
3814 error ("#else or #elif after #else");
3815 if_stack
->type
= kt
->type
;
3820 if_stack
= if_stack
->next
;
3825 /* Anything else is ignored. */
3834 /* after this returns, rescan will exit because ip->bufp
3835 now points to the end of the buffer.
3836 rescan is responsible for the error message also. */
3840 * handle a #else directive. Do this by just continuing processing
3841 * without changing if_stack ; this is so that the error message
3842 * for missing #endif's etc. will point to the original #if. It
3843 * is possible that something different would be better.
3846 do_else (buf
, limit
, op
)
3847 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3848 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3851 FILE_BUF
*ip
= &instack
[indepth
];
3853 if (if_stack
== instack
[indepth
].if_stack
) {
3854 error ("#else not within a conditional");
3857 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3858 error ("#else after #else");
3859 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3860 if (strcmp (if_stack
->fname
, ip
->fname
) != 0)
3861 fprintf (stderr
, ", file %s", if_stack
->fname
);
3862 fprintf (stderr
, ")\n");
3864 if_stack
->type
= T_ELSE
;
3867 if (if_stack
->if_succeeded
)
3868 skip_if_group (ip
, 0);
3870 ++if_stack
->if_succeeded
; /* continue processing input */
3871 output_line_command (ip
, op
, 1, same_file
);
3876 * unstack after #endif command
3879 do_endif (buf
, limit
, op
)
3880 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3881 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3884 if (if_stack
== instack
[indepth
].if_stack
)
3885 error ("unbalanced #endif");
3887 IF_STACK_FRAME
*temp
= if_stack
;
3888 if_stack
= if_stack
->next
;
3890 output_line_command (&instack
[indepth
], op
, 1, same_file
);
3895 * Skip a comment, assuming the input ptr immediately follows the
3896 * initial slash-star. Bump line counter as necessary.
3897 * (The canonical line counter is &ip->lineno).
3898 * Don't use this routine (or the next one) if bumping the line
3899 * counter is not sufficient to deal with newlines in the string.
3902 skip_to_end_of_comment (ip
, line_counter
)
3904 int *line_counter
; /* place to remember newlines, or NULL */
3906 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
3907 U_CHAR
*bp
= ip
->bufp
;
3908 FILE_BUF
*op
= &outbuf
; /* JF */
3909 int output
= put_out_comments
&& !line_counter
;
3911 /* JF this line_counter stuff is a crock to make sure the
3912 comment is only put out once, no matter how many times
3913 the comment is skipped. It almost works */
3918 while (bp
< limit
) {
3923 if (warn_comments
&& bp
< limit
&& *bp
== '*')
3924 warning("`/*' within comment");
3927 if (line_counter
!= NULL
)
3933 if (*bp
== '\\' && bp
[1] == '\n')
3949 * Skip over a quoted string. BP points to the opening quote.
3950 * Returns a pointer after the closing quote. Don't go past LIMIT.
3951 * START_LINE is the line number of the starting point (but it need
3952 * not be valid if the starting point is inside a macro expansion).
3954 * The input stack state is not changed.
3956 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3957 * for each newline passed.
3959 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3960 * if we pass a backslash-newline.
3962 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3965 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
3967 const U_CHAR
*limit
;
3969 int *count_newlines
;
3970 int *backslash_newlines_p
;
3978 error_with_line (line_for_error (start_line
),
3979 "unterminated string or character constant");
3986 while (*bp
== '\\' && bp
[1] == '\n') {
3987 if (backslash_newlines_p
)
3988 *backslash_newlines_p
= 1;
3993 if (*bp
== '\n' && count_newlines
) {
3994 if (backslash_newlines_p
)
3995 *backslash_newlines_p
= 1;
3999 } else if (c
== '\n') {
4000 /* Unterminated strings and character constants are 'legal'. */
4001 bp
--; /* Don't consume the newline. */
4005 } else if (c
== match
)
4008 return (U_CHAR
*) bp
;
4012 * write out a #line command, for instance, after an #include file.
4013 * If CONDITIONAL is nonzero, we can omit the #line if it would
4014 * appear to be a no-op, and we can output a few newlines instead
4015 * if we want to increase the line number by a small amount.
4016 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
4020 output_line_command (ip
, op
, conditional
, file_change
)
4023 enum file_change_code file_change
;
4026 char line_cmd_buf
[500];
4028 if (no_line_commands
4029 || ip
->fname
== NULL
4031 op
->lineno
= ip
->lineno
;
4036 if (ip
->lineno
== op
->lineno
)
4039 /* If the inherited line number is a little too small,
4040 output some newlines instead of a #line command. */
4041 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
4042 check_expand (op
, 10);
4043 while (ip
->lineno
> op
->lineno
) {
4051 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->fname
);
4052 if (file_change
!= same_file
)
4053 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
4054 if (system_include_depth
> 0)
4055 strcat (line_cmd_buf
, " 3");
4056 len
= strlen (line_cmd_buf
);
4057 line_cmd_buf
[len
++] = '\n';
4058 check_expand (op
, len
+ 1);
4059 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
4061 memcpy (op
->bufp
, line_cmd_buf
, len
);
4063 op
->lineno
= ip
->lineno
;
4067 /* Expand a macro call.
4068 HP points to the symbol that is the macro being called.
4069 Put the result of expansion onto the input stack
4070 so that subsequent input by our caller will use it.
4072 If macro wants arguments, caller has already verified that
4073 an argument list follows; arguments come from the input stack. */
4076 macroexpand (hp
, op
)
4081 DEFINITION
*defn
= hp
->value
.defn
;
4084 int start_line
= instack
[indepth
].lineno
;
4086 CHECK_DEPTH (return;);
4088 /* it might not actually be a macro. */
4089 if (hp
->type
!= T_MACRO
) {
4090 special_symbol (hp
, op
);
4094 nargs
= defn
->nargs
;
4098 struct argdata
*args
;
4099 const char *parse_error
= 0;
4101 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
4103 for (i
= 0; i
< nargs
; i
++) {
4104 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
4105 args
[i
].raw_length
= args
[i
].expand_length
4106 = args
[i
].stringified_length
= 0;
4107 args
[i
].free1
= args
[i
].free2
= 0;
4110 /* Parse all the macro args that are supplied. I counts them.
4111 The first NARGS args are stored in ARGS.
4112 The rest are discarded. */
4115 /* Discard the open-parenthesis or comma before the next arg. */
4116 ++instack
[indepth
].bufp
;
4118 = macarg ((i
< nargs
|| (nargs
== 0 && i
== 0)) ? &args
[i
] : 0);
4121 error_with_line (line_for_error (start_line
), "%s", parse_error
);
4125 } while (*instack
[indepth
].bufp
!= ')');
4127 /* If we got one arg but it was just whitespace, call that 0 args. */
4129 const U_CHAR
*bp
= args
[0].raw
;
4130 const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
4131 while (bp
!= lim
&& is_space (*bp
)) bp
++;
4136 if (nargs
== 0 && i
> 0)
4137 error ("arguments given to macro `%s'", hp
->name
);
4138 else if (i
< nargs
) {
4139 /* traditional C allows foo() if foo wants one argument. */
4140 if (nargs
== 1 && i
== 0)
4143 error ("no args to macro `%s'", hp
->name
);
4145 error ("only 1 arg to macro `%s'", hp
->name
);
4147 error ("only %d args to macro `%s'", i
, hp
->name
);
4148 } else if (i
> nargs
)
4149 error ("too many (%d) args to macro `%s'", i
, hp
->name
);
4151 /* Swallow the closeparen. */
4152 ++instack
[indepth
].bufp
;
4154 /* If macro wants zero args, we parsed the arglist for checking only.
4155 Read directly from the macro definition. */
4157 xbuf
= defn
->expansion
;
4158 xbuf_len
= defn
->length
;
4160 U_CHAR
*exp
= defn
->expansion
;
4161 int offset
; /* offset in expansion,
4162 copied a piece at a time */
4163 int totlen
; /* total amount of exp buffer filled so far */
4167 /* Macro really takes args. Compute the expansion of this call. */
4169 /* Compute length in characters of the macro's expansion. */
4170 xbuf_len
= defn
->length
;
4171 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4173 xbuf_len
+= args
[ap
->argno
].stringified_length
;
4175 xbuf_len
+= args
[ap
->argno
].raw_length
;
4178 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
4180 /* Generate in XBUF the complete expansion
4181 with arguments substituted in.
4182 TOTLEN is the total size generated so far.
4183 OFFSET is the index in the definition
4184 of where we are copying from. */
4185 offset
= totlen
= 0;
4186 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4187 struct argdata
*arg
= &args
[ap
->argno
];
4189 for (i
= 0; i
< ap
->nchars
; i
++)
4190 xbuf
[totlen
++] = exp
[offset
++];
4192 if (ap
->stringify
!= 0) {
4193 int arglen
= arg
->raw_length
;
4199 && (c
= arg
->raw
[i
], is_space (c
)))
4202 && (c
= arg
->raw
[arglen
- 1], is_space (c
)))
4204 for (; i
< arglen
; i
++) {
4207 /* Special markers Newline Space
4208 generate nothing for a stringified argument. */
4209 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
4214 /* Internal sequences of whitespace are replaced by one space
4215 except within an string or char token. */
4217 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space (c
))) {
4219 /* Note that Newline Space does occur within whitespace
4220 sequences; consider it part of the sequence. */
4221 if (c
== '\n' && is_space (arg
->raw
[i
+1]))
4223 else if (c
!= '\n' && is_space (c
))
4240 } else if (c
== '\"' || c
== '\'')
4244 /* Escape these chars */
4245 if (c
== '\"' || (in_string
&& c
== '\\'))
4246 xbuf
[totlen
++] = '\\';
4250 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
4255 const U_CHAR
*p1
= arg
->raw
;
4256 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
4258 if (ap
->raw_before
) {
4259 while (p1
!= l1
&& is_space (*p1
)) p1
++;
4260 while (p1
!= l1
&& is_idchar (*p1
))
4261 xbuf
[totlen
++] = *p1
++;
4262 /* Delete any no-reexpansion marker that follows
4263 an identifier at the beginning of the argument
4264 if the argument is concatenated with what precedes it. */
4265 if (p1
[0] == '\n' && p1
[1] == '-')
4268 if (ap
->raw_after
) {
4269 /* Arg is concatenated after: delete trailing whitespace,
4270 whitespace markers, and no-reexpansion markers. */
4272 if (is_space (l1
[-1])) l1
--;
4273 else if (l1
[-1] == '-') {
4274 const U_CHAR
*p2
= l1
- 1;
4275 /* If a `-' is preceded by an odd number of newlines then it
4276 and the last newline are a no-reexpansion marker. */
4277 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
4278 if ((l1
- 1 - p2
) & 1) {
4286 memmove (xbuf
+ totlen
, p1
, l1
- p1
);
4290 if (totlen
> xbuf_len
)
4294 /* if there is anything left of the definition
4295 after handling the arg list, copy that in too. */
4297 for (i
= offset
; i
< defn
->length
; i
++)
4298 xbuf
[totlen
++] = exp
[i
];
4303 for (i
= 0; i
< nargs
; i
++) {
4304 if (args
[i
].free1
!= 0)
4305 free (args
[i
].free1
);
4306 if (args
[i
].free2
!= 0)
4307 free (args
[i
].free2
);
4311 xbuf
= defn
->expansion
;
4312 xbuf_len
= defn
->length
;
4315 /* Now put the expansion on the input stack
4316 so our caller will commence reading from it. */
4320 ip2
= &instack
[++indepth
];
4325 ip2
->length
= xbuf_len
;
4327 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
4329 ip2
->if_stack
= if_stack
;
4334 * Parse a macro argument and store the info on it into *ARGPTR.
4335 * Return nonzero to indicate a syntax error.
4340 struct argdata
*argptr
;
4342 FILE_BUF
*ip
= &instack
[indepth
];
4347 /* Try to parse as much of the argument as exists at this
4348 input stack level. */
4349 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
4350 &paren
, &newlines
, &comments
);
4352 /* If we find the end of the argument at this level,
4353 set up *ARGPTR to point at it in the input stack. */
4354 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
4355 && bp
!= ip
->buf
+ ip
->length
) {
4357 argptr
->raw
= ip
->bufp
;
4358 argptr
->raw_length
= bp
- ip
->bufp
;
4362 /* This input stack level ends before the macro argument does.
4363 We must pop levels and keep parsing.
4364 Therefore, we must allocate a temporary buffer and copy
4365 the macro argument into it. */
4366 int bufsize
= bp
- ip
->bufp
;
4367 int extra
= newlines
;
4368 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
4369 int final_start
= 0;
4371 memcpy (buffer
, ip
->bufp
, bufsize
);
4373 ip
->lineno
+= newlines
;
4375 while (bp
== ip
->buf
+ ip
->length
) {
4376 if (instack
[indepth
].macro
== 0) {
4378 return "unterminated macro call";
4380 ip
->macro
->type
= T_MACRO
;
4382 free (ip
->free_ptr
);
4383 ip
= &instack
[--indepth
];
4386 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
4387 &newlines
, &comments
);
4388 final_start
= bufsize
;
4389 bufsize
+= bp
- ip
->bufp
;
4391 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
4392 memcpy (buffer
+ bufsize
- (bp
- ip
->bufp
), ip
->bufp
, bp
- ip
->bufp
);
4394 ip
->lineno
+= newlines
;
4397 /* Now, if arg is actually wanted, record its raw form,
4398 discarding comments and duplicating newlines in whatever
4399 part of it did not come from a macro expansion.
4400 EXTRA space has been preallocated for duplicating the newlines.
4401 FINAL_START is the index of the start of that part. */
4403 argptr
->raw
= buffer
;
4404 argptr
->raw_length
= bufsize
;
4405 argptr
->free1
= buffer
;
4406 argptr
->newlines
= newlines
;
4407 argptr
->comments
= comments
;
4408 if ((newlines
|| comments
) && ip
->fname
!= 0)
4411 discard_comments (argptr
->raw
+ final_start
,
4412 argptr
->raw_length
- final_start
,
4414 argptr
->raw
[argptr
->raw_length
] = 0;
4415 if (argptr
->raw_length
> bufsize
+ extra
)
4420 /* If we are not discarding this argument,
4421 macroexpand it and compute its length as stringified.
4422 All this info goes into *ARGPTR. */
4426 const U_CHAR
*buf
, *lim
;
4429 obuf
= expand_to_temp_buffer (argptr
->raw
,
4430 argptr
->raw
+ argptr
->raw_length
,
4433 argptr
->expanded
= obuf
.buf
;
4434 argptr
->expand_length
= obuf
.length
;
4435 argptr
->free2
= obuf
.buf
;
4438 lim
= buf
+ argptr
->raw_length
;
4441 while (buf
!= lim
) {
4444 /* Internal sequences of whitespace are replaced by one space
4445 in most cases, but not always. So count all the whitespace
4446 in case we need to keep it all. */
4447 if (c
== '\"' || c
== '\\') /* escape these chars */
4449 else if (!ISPRINT (c
))
4452 argptr
->stringified_length
= totlen
;
4457 /* Scan text from START (inclusive) up to LIMIT (exclusive),
4458 counting parens in *DEPTHPTR,
4459 and return if reach LIMIT
4460 or before a `)' that would make *DEPTHPTR negative
4461 or before a comma when *DEPTHPTR is zero.
4462 Single and double quotes are matched and termination
4463 is inhibited within them. Comments also inhibit it.
4464 Value returned is pointer to stopping place.
4466 Increment *NEWLINES each time a newline is passed.
4467 Set *COMMENTS to 1 if a comment is seen. */
4470 macarg1 (start
, limit
, depthptr
, newlines
, comments
)
4472 const U_CHAR
*limit
;
4473 int *depthptr
, *newlines
, *comments
;
4477 while (bp
< limit
) {
4483 if (--(*depthptr
) < 0)
4487 /* Traditionally, backslash makes following char not special. */
4491 /* But count source lines anyway. */
4500 if (bp
[1] == '\\' && bp
[2] == '\n')
4501 newline_fix (bp
+ 1);
4502 if (bp
[1] != '*' || bp
+ 1 >= limit
)
4506 while (bp
+ 1 < limit
) {
4508 && bp
[1] == '\\' && bp
[2] == '\n')
4509 newline_fix (bp
+ 1);
4510 if (bp
[0] == '*' && bp
[1] == '/')
4512 if (*bp
== '\n') ++*newlines
;
4521 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
4526 while (*bp
== '\\' && bp
[1] == '\n') {
4529 } else if (*bp
== '\n') {
4538 if ((*depthptr
) == 0)
4548 /* Discard comments and duplicate newlines
4549 in the string of length LENGTH at START,
4550 except inside of string constants.
4551 The string is copied into itself with its beginning staying fixed.
4553 NEWLINES is the number of newlines that must be duplicated.
4554 We assume that that much extra space is available past the end
4558 discard_comments (start
, length
, newlines
)
4565 const U_CHAR
*limit
;
4568 /* If we have newlines to duplicate, copy everything
4569 that many characters up. Then, in the second part,
4570 we will have room to insert the newlines
4572 NEWLINES may actually be too large, because it counts
4573 newlines in string constants, and we don't duplicate those.
4574 But that does no harm. */
4576 ibp
= start
+ length
;
4577 obp
= ibp
+ newlines
;
4579 while (limit
!= ibp
)
4583 ibp
= start
+ newlines
;
4584 limit
= start
+ length
+ newlines
;
4587 while (ibp
< limit
) {
4588 *obp
++ = c
= *ibp
++;
4591 /* Duplicate the newline. */
4603 if (*ibp
== '\\' && ibp
[1] == '\n')
4605 /* Delete any comment. */
4606 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
4610 while (ibp
+ 1 < limit
) {
4612 && ibp
[1] == '\\' && ibp
[2] == '\n')
4613 newline_fix (ibp
+ 1);
4614 if (ibp
[0] == '*' && ibp
[1] == '/')
4623 /* Notice and skip strings, so that we don't
4624 think that comments start inside them,
4625 and so we don't duplicate newlines in them. */
4628 while (ibp
< limit
) {
4629 *obp
++ = c
= *ibp
++;
4632 if (c
== '\n' && quotec
== '\'')
4634 if (c
== '\\' && ibp
< limit
) {
4635 while (*ibp
== '\\' && ibp
[1] == '\n')
4649 /* Core error handling routine. */
4651 v_message (mtype
, line
, msgid
, ap
)
4657 const char *fname
= 0;
4660 if (mtype
== MT_WARNING
&& inhibit_warnings
)
4663 for (i
= indepth
; i
>= 0; i
--)
4664 if (instack
[i
].fname
!= NULL
) {
4666 line
= instack
[i
].lineno
;
4667 fname
= instack
[i
].fname
;
4672 fprintf (stderr
, "%s:%d: ", fname
, line
);
4674 fprintf (stderr
, "%s: ", progname
);
4676 if (mtype
== MT_WARNING
)
4677 fputs (_("warning: "), stderr
);
4679 vfprintf (stderr
, _(msgid
), ap
);
4680 putc ('\n', stderr
);
4682 if (mtype
== MT_ERROR
)
4687 * error - print error message and increment count of errors.
4690 error
VPARAMS ((const char *msgid
, ...))
4693 VA_FIXEDARG (ap
, const char *, msgid
);
4695 v_message (MT_ERROR
, 0, msgid
, ap
);
4700 error_with_line
VPARAMS ((int line
, const char *msgid
, ...))
4703 VA_FIXEDARG (ap
, int, line
);
4704 VA_FIXEDARG (ap
, const char *, msgid
);
4706 v_message (MT_ERROR
, line
, msgid
, ap
);
4710 /* Error including a message from `errno'. */
4712 error_from_errno (name
)
4715 error ("%s: %s", name
, strerror (errno
));
4718 /* Print error message but don't count it. */
4720 warning
VPARAMS ((const char *msgid
, ...))
4723 VA_FIXEDARG (ap
, const char *, msgid
);
4725 v_message (MT_WARNING
, 0, msgid
, ap
);
4730 fatal
VPARAMS ((const char *msgid
, ...))
4733 VA_FIXEDARG (ap
, const char *, msgid
);
4735 v_message (MT_FATAL
, 0, msgid
, ap
);
4737 exit (FATAL_EXIT_CODE
);
4740 /* More 'friendly' abort that prints the location at which we died. */
4742 fancy_abort (line
, func
)
4746 fatal ("internal error in %s, at tradcpp.c:%d\n\
4747 Please submit a full bug report.\n\
4748 See %s for instructions.", func
, line
, GCCBUGURL
);
4752 perror_with_name (name
)
4755 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, strerror (errno
));
4760 pfatal_with_name (name
)
4763 perror_with_name (name
);
4764 exit (FATAL_EXIT_CODE
);
4767 /* Return the line at which an error occurred.
4768 The error is not necessarily associated with the current spot
4769 in the input stack, so LINE says where. LINE will have been
4770 copied from ip->lineno for the current input level.
4771 If the current level is for a file, we return LINE.
4772 But if the current level is not for a file, LINE is meaningless.
4773 In that case, we return the lineno of the innermost file. */
4775 line_for_error (line
)
4781 for (i
= indepth
; i
>= 0; ) {
4782 if (instack
[i
].fname
!= 0)
4787 line1
= instack
[i
].lineno
;
4793 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4795 * As things stand, nothing is ever placed in the output buffer to be
4796 * removed again except when it's KNOWN to be part of an identifier,
4797 * so flushing and moving down everything left, instead of expanding,
4802 grow_outbuf (obuf
, needed
)
4809 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
4812 /* Make it at least twice as big as it is now. */
4814 /* Make it have at least 150% of the free space we will need. */
4815 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
4816 if (minsize
> obuf
->length
)
4817 obuf
->length
= minsize
;
4819 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
4820 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
4824 /* Symbol table for macro names and special symbols */
4827 * install a name in the main hash table, even if it is already there.
4828 * name stops with first non alphanumeric, except leading '#'.
4829 * caller must check against redefinition if that is desired.
4830 * delete_macro () removes things installed by install () in fifo order.
4831 * this is important because of the `defined' special symbol used
4832 * in #if, and also if pushdef/popdef directives are ever implemented.
4834 * If LEN is >= 0, it is the length of the name.
4835 * Otherwise, compute the length by scanning the entire name.
4837 * If HASH is >= 0, it is the precomputed hash code.
4838 * Otherwise, compute the hash code.
4840 * caller must set the value, if any is desired.
4843 install (name
, len
, type
, hash
)
4846 enum node_type type
;
4848 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4857 while (is_idchar (*p
))
4863 hash
= hashf (name
, len
, HASHSIZE
);
4865 hp
= (HASHNODE
*) xmalloc (sizeof (HASHNODE
) + len
+ 1);
4867 hp
->bucket_hdr
= &hashtab
[bucket
];
4868 hp
->next
= hashtab
[bucket
];
4869 hashtab
[bucket
] = hp
;
4871 if (hp
->next
!= NULL
)
4872 hp
->next
->prev
= hp
;
4875 hp
->name
= q
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
4876 memcpy (q
, name
, len
);
4882 * find the most recent hash node for name name (ending with first
4883 * non-identifier char) installed by install
4885 * If LEN is >= 0, it is the length of the name.
4886 * Otherwise, compute the length by scanning the entire name.
4888 * If HASH is >= 0, it is the precomputed hash code.
4889 * Otherwise, compute the hash code.
4892 lookup (name
, len
, hash
)
4901 for (bp
= name
; is_idchar (*bp
); bp
++) ;
4906 hash
= hashf (name
, len
, HASHSIZE
);
4908 bucket
= hashtab
[hash
];
4910 if (bucket
->length
== len
4911 && strncmp ((const char *)bucket
->name
, (const char *)name
, len
) == 0)
4913 bucket
= bucket
->next
;
4919 * Delete a hash node. Some weirdness to free junk from macros.
4920 * More such weirdness will have to be added if you define more hash
4921 * types that need it.
4924 /* Note that the DEFINITION of a macro is removed from the hash table
4925 but its storage is not freed. This would be a storage leak
4926 except that it is not reasonable to keep undefining and redefining
4927 large numbers of macros many times.
4928 In any case, this is necessary, because a macro can be #undef'd
4929 in the middle of reading the arguments to a call to it.
4930 If #undef freed the DEFINITION, that would crash. */
4936 if (hp
->prev
!= NULL
)
4937 hp
->prev
->next
= hp
->next
;
4938 if (hp
->next
!= NULL
)
4939 hp
->next
->prev
= hp
->prev
;
4941 /* make sure that the bucket chain header that
4942 the deleted guy was on points to the right thing afterwards. */
4943 if (hp
== *hp
->bucket_hdr
)
4944 *hp
->bucket_hdr
= hp
->next
;
4950 * return hash function on name. must be compatible with the one
4951 * computed a step at a time, elsewhere
4954 hashf (name
, len
, hashsize
)
4962 r
= HASHSTEP (r
, *name
++);
4964 return MAKE_POS (r
) % hashsize
;
4967 /* Dump all macro definitions as #defines to stdout. */
4974 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
4977 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
4978 if (hp
->type
== T_MACRO
) {
4979 DEFINITION
*defn
= hp
->value
.defn
;
4985 /* Print the definition of the macro HP. */
4987 printf ("#define %s", hp
->name
);
4988 if (defn
->nargs
>= 0) {
4992 for (i
= 0; i
< defn
->nargs
; i
++) {
4993 dump_arg_n (defn
, i
);
4994 if (i
+ 1 < defn
->nargs
)
5004 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
5005 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
);
5006 if (ap
->nchars
!= 0)
5008 offset
+= ap
->nchars
;
5011 if (ap
->raw_before
&& !concat
)
5014 dump_arg_n (defn
, ap
->argno
);
5015 if (ap
->raw_after
) {
5020 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
);
5027 /* Output to stdout a substring of a macro definition.
5028 BASE is the beginning of the definition.
5029 Output characters START thru LENGTH.
5030 Discard newlines outside of strings, thus
5031 converting funny-space markers to ordinary spaces. */
5033 dump_defn_1 (base
, start
, length
)
5038 const U_CHAR
*p
= base
+ start
;
5039 const U_CHAR
*limit
= base
+ start
+ length
;
5044 else if (*p
== '\"' || *p
=='\'') {
5045 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
5046 fwrite (p
, p1
- p
, 1, stdout
);
5053 /* Print the name of argument number ARGNUM of macro definition DEFN.
5054 Recall that DEFN->argnames contains all the arg names
5055 concatenated in reverse order with comma-space in between. */
5057 dump_arg_n (defn
, argnum
)
5061 const U_CHAR
*p
= defn
->argnames
;
5062 while (argnum
+ 1 < defn
->nargs
) {
5063 p
= (const U_CHAR
*) strchr ((const char *)p
, ' ') + 1;
5067 while (*p
&& *p
!= ',') {
5073 /* Initialize the built-in macros. */
5074 #define DSC(x) U x, sizeof x - 1
5075 #define install_spec(name, type) \
5076 install(DSC(name), type, -1);
5077 #define install_value(name, val) \
5078 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
5080 initialize_builtins ()
5084 install_spec ("__BASE_FILE__", T_BASE_FILE
);
5085 install_spec ("__DATE__", T_DATE
);
5086 install_spec ("__FILE__", T_FILE
);
5087 install_spec ("__TIME__", T_TIME
);
5088 install_spec ("__VERSION__", T_VERSION
);
5089 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL
);
5090 install_spec ("__LINE__", T_SPECLINE
);
5092 #ifndef NO_BUILTIN_SIZE_TYPE
5093 install_value ("__SIZE_TYPE__", SIZE_TYPE
);
5095 #ifndef NO_BUILTIN_PTRDIFF_TYPE
5096 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
);
5098 #ifndef NO_BUILTIN_WCHAR_TYPE
5099 install_value ("__WCHAR_TYPE__", WCHAR_TYPE
);
5101 #ifndef NO_BUILTIN_WINT_TYPE
5102 install_value ("__WINT_TYPE__", WINT_TYPE
);
5104 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX
);
5105 install_value ("__USER_LABEL_PREFIX__", user_label_prefix
);
5109 #undef install_value
5111 /* Common handler of command line directives -U, -D and -A. */
5113 run_directive (str
, len
, type
)
5116 enum node_type type
;
5118 struct directive
*kt
;
5119 FILE_BUF
*ip
= &instack
[++indepth
];
5120 ip
->fname
= "*command line*";
5122 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
5127 ip
->if_stack
= if_stack
;
5129 for (kt
= directive_table
; kt
->type
!= type
; kt
++)
5132 (*kt
->func
) ((U_CHAR
*) str
, (U_CHAR
*) str
+ len
, NULL
);
5136 /* Handle the -D option. If STR is just an identifier, define it with
5137 * value 1. If STR has anything after the identifier, then it should
5138 * be identifier-space-definition. */
5140 make_definition (str
)
5146 /* Copy the entire option so we can modify it.
5147 Change the first "=" in the string to a space. If there is none,
5148 tack " 1" on the end. */
5150 /* Length including the null. */
5151 count
= strlen (str
);
5152 buf
= (char *) alloca (count
+ 2);
5153 memcpy (buf
, str
, count
);
5155 p
= strchr (str
, '=');
5164 run_directive (buf
, count
, T_DEFINE
);
5167 /* Handle the -U option. */
5172 run_directive (str
, strlen (str
), T_UNDEF
);
5175 /* Handles the #assert (-A) and #unassert (-A-) command line options. */
5177 make_assertion (str
)
5180 enum node_type type
= T_ASSERT
;
5190 count
= strlen (str
);
5191 p
= strchr (str
, '=');
5194 /* Copy the entire option so we can modify it. Change the first
5195 "=" in the string to a '(', and tack a ')' on the end. */
5196 char *buf
= (char *) alloca (count
+ 1);
5198 memcpy (buf
, str
, count
);
5204 run_directive (str
, count
, type
);
5207 /* Get the file-mode and data size of the file open on FD
5208 and store them in *MODE_POINTER and *SIZE_POINTER. */
5211 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
5218 if (fstat (fd
, &sbuf
) < 0) return -1;
5219 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
5220 if (size_pointer
) *size_pointer
= sbuf
.st_size
;