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 const int length
; /* Length of name */
314 void (*const func
) PARAMS ((U_CHAR
*, U_CHAR
*, FILE_BUF
*));
315 /* Function to handle directive */
316 const char *const name
; /* Name of directive */
317 const 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 static const 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;
521 /* Get rid of any avoidable limit on stack size. */
525 /* Set the stack limit huge so that alloca (particularly stringtab
526 * in dbxread.c) does not fail. */
527 getrlimit (RLIMIT_STACK
, &rlim
);
528 rlim
.rlim_cur
= rlim
.rlim_max
;
529 setrlimit (RLIMIT_STACK
, &rlim
);
531 #endif /* RLIMIT_STACK defined */
538 no_line_commands
= 0;
542 max_include_len
= cpp_GCC_INCLUDE_DIR_len
+ 7; /* ??? */
546 /* It's simplest to just create this struct whether or not it will
550 /* Process switches and find input file name. */
552 for (i
= 1; i
< argc
; i
++) {
553 if (argv
[i
][0] != '-') {
554 if (out_fname
!= NULL
)
555 fatal ("usage: %s [switches] input output", argv
[0]);
556 else if (in_fname
!= NULL
)
566 break; /* Ignore for compatibility with ISO/extended cpp. */
569 if (!strcmp (argv
[i
], "-lang-c++")
570 || !strcmp (argv
[i
], "-lang-objc++"))
571 fatal ("-traditional is not supported in C++");
572 else if (!strcmp (argv
[i
], "-lang-c89"))
573 fatal ("-traditional and -ansi are mutually exclusive");
574 else if (!strcmp (argv
[i
], "-lang-objc"))
575 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__OBJC__";
576 else if (!strcmp (argv
[i
], "-lang-asm"))
577 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "__ASSEMBLER__";
578 else if (!strcmp (argv
[i
], "-lang-fortran"))
579 pend
[i
].type
= PD_DEFINE
, pend
[i
].arg
= "_LANGUAGE_FORTRAN";
580 /* All other possibilities ignored. */
584 if (!strcmp (argv
[i
], "-include"))
587 fatal ("filename missing after -i option");
589 pend
[i
].type
= PD_FILE
, pend
[i
].arg
= argv
[i
+ 1], i
++;
591 else if (!strcmp (argv
[i
], "-iprefix"))
592 i
++; /* Ignore for compatibility */
593 else if (!strcmp (argv
[i
], "-isystem")
594 || !strcmp (argv
[i
], "-iwithprefix")
595 || !strcmp (argv
[i
], "-iwithprefixbefore")
596 || !strcmp (argv
[i
], "-idirafter"))
597 goto add_include
; /* best we can do */
602 if (out_fname
!= NULL
)
603 fatal ("output filename specified twice");
605 fatal ("filename missing after -o option");
606 out_fname
= argv
[++i
];
607 if (!strcmp (out_fname
, "-"))
612 inhibit_warnings
= 1;
616 if (!strcmp (argv
[i
], "-Wcomments"))
618 else if (!strcmp (argv
[i
], "-Wcomment"))
620 else if (!strcmp (argv
[i
], "-Wall")) {
626 if (!strcmp (argv
[i
], "-fleading-underscore"))
627 user_label_prefix
= "_";
628 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
629 user_label_prefix
= "";
636 /* -MD and -MMD for tradcpp are deprecated and undocumented
637 (use -M or -MM with -MF instead), and probably should be
638 removed with the next major GCC version. For the moment
639 we allow these for the benefit of Automake 1.4, which
640 uses these when dependency tracking is enabled. Automake
641 1.5 will fix this. */
642 if (!strncmp (argv
[i
], "-MD", 3)) {
645 } else if (!strncmp (argv
[i
], "-MMD", 4)) {
648 } else if (!strcmp (argv
[i
], "-M")) {
650 } else if (!strcmp (argv
[i
], "-MM")) {
652 } else if (!strcmp (argv
[i
], "-MG")) {
653 deps_missing_files
= 1;
654 } else if (!strcmp (argv
[i
], "-MF")) {
656 } else if (!strcmp (argv
[i
], "-MP")) {
657 print_deps_phony_targets
= 1;
658 } else if (!strcmp (argv
[i
], "-MQ") || !strcmp (argv
[i
], "-MT")) {
659 /* Add a target. -MQ quotes for Make. */
660 const char *tgt
= argv
[i
] + 3;
661 int quoted
= argv
[i
][2] == 'Q';
663 if (*tgt
== '\0' && i
+ 1 == argc
)
664 fatal ("target missing after %s option", argv
[i
]);
670 deps_add_target (deps
, tgt
, quoted
);
677 else if (i
+ 1 == argc
)
678 fatal ("filename missing after %s option", argv
[i
]);
680 deps_file
= argv
[++i
];
691 fprintf (stderr
, "GNU traditional CPP version %s\n", version_string
);
702 else if (i
+ 1 == argc
)
703 fatal ("macro name missing after -%c option", c
);
708 pend
[i
].type
= PD_DEFINE
;
710 pend
[i
].type
= PD_UNDEF
;
712 pend
[i
].type
= PD_ASSERTION
;
718 put_out_comments
= 1;
722 if (!strcmp (argv
[i
], "-pedantic"))
723 fatal ("-pedantic and -traditional are mutually exclusive");
727 if (!strcmp (argv
[i
], "-trigraphs"))
728 fatal ("-trigraphs and -traditional are mutually exclusive");
732 no_line_commands
= 1;
735 case 'I': /* Add directory to path for includes. */
738 struct file_name_list
*dirtmp
;
740 if (! ignore_srcdir
&& !strcmp (argv
[i
] + 2, "-"))
743 dirtmp
= (struct file_name_list
*)
744 xmalloc (sizeof (struct file_name_list
));
745 dirtmp
->next
= 0; /* New one goes on the end */
749 last_include
->next
= dirtmp
;
750 last_include
= dirtmp
; /* Tail follows the last one */
751 if (argv
[i
][1] == 'I' && argv
[i
][2] != 0)
752 dirtmp
->fname
= argv
[i
] + 2;
753 else if (i
+ 1 == argc
)
754 fatal ("directory name missing after -I option");
756 dirtmp
->fname
= argv
[++i
];
757 if (strlen (dirtmp
->fname
) > max_include_len
)
758 max_include_len
= strlen (dirtmp
->fname
);
759 if (ignore_srcdir
&& first_bracket_include
== 0)
760 first_bracket_include
= dirtmp
;
766 /* -nostdinc causes no default include directories.
767 You must specify all include-file directories with -I. */
768 no_standard_includes
= 1;
771 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
772 if (in_fname
== NULL
) {
775 } else if (out_fname
== NULL
) {
778 } /* else fall through into error */
781 fatal ("invalid option `%s'", argv
[i
]);
786 init_dependency_output ();
788 /* After checking the environment variables, check if -M or -MM has
789 not been specified, but other -M options have. */
791 && (deps_missing_files
|| deps_file
|| print_deps_phony_targets
))
792 fatal ("you must additionally specify either -M or -MM");
794 if (user_label_prefix
== 0)
795 user_label_prefix
= USER_LABEL_PREFIX
;
799 /* Set the default target (if there is none already), and
800 the dependency on the main file. */
801 deps_add_default_target (deps
, in_fname
);
803 deps_add_dep (deps
, in_fname
);
806 /* Install __LINE__, etc. Must follow option processing. */
807 initialize_builtins ();
809 /* Do defines specified with -D and undefines specified with -U. */
810 for (i
= 1; i
< argc
; i
++)
811 if (pend
[i
].type
== PD_DEFINE
)
812 make_definition (pend
[i
].arg
);
813 else if (pend
[i
].type
== PD_UNDEF
)
814 make_undef (pend
[i
].arg
);
815 else if (pend
[i
].type
== PD_ASSERTION
)
816 make_assertion (pend
[i
].arg
);
818 /* Unless -fnostdinc,
819 tack on the standard include file dirs to the specified list */
820 if (!no_standard_includes
) {
821 const struct default_include
*di
;
822 struct file_name_list
*old_last_include
= last_include
;
823 struct file_name_list
*dirtmp
;
824 for (di
= cpp_include_defaults
; di
->fname
; di
++) {
827 dirtmp
= (struct file_name_list
*)
828 xmalloc (sizeof (struct file_name_list
));
829 dirtmp
->next
= 0; /* New one goes on the end */
833 last_include
->next
= dirtmp
;
834 last_include
= dirtmp
; /* Tail follows the last one */
835 dirtmp
->fname
= di
->fname
;
836 if (strlen (dirtmp
->fname
) > max_include_len
)
837 max_include_len
= strlen (dirtmp
->fname
);
840 if (ignore_srcdir
&& first_bracket_include
== 0)
841 first_bracket_include
= old_last_include
->next
;
844 /* Initialize output buffer */
846 outbuf
.buf
= (U_CHAR
*) xmalloc (OUTBUF_SIZE
);
847 outbuf
.bufp
= outbuf
.buf
;
848 outbuf
.length
= OUTBUF_SIZE
;
850 /* Scan the -i files before the main input.
851 Much like #including them, but with no_output set
852 so that only their macro definitions matter. */
856 for (i
= 1; i
< argc
; i
++)
857 if (pend
[i
].type
== PD_FILE
)
859 int fd
= open (pend
[i
].arg
, O_RDONLY
, 0666);
862 perror_with_name (pend
[i
].arg
);
863 return FATAL_EXIT_CODE
;
866 /* For -M, add this file to the dependencies. */
868 deps_add_dep (deps
, pend
[i
].arg
);
870 finclude (fd
, pend
[i
].arg
, 0, &outbuf
);
875 /* Pending directives no longer needed. */
878 /* Create an input stack level for the main input file
879 and copy the entire contents of the file into it. */
881 fp
= &instack
[++indepth
];
883 /* JF check for stdin */
884 if (in_fname
== NULL
|| *in_fname
== 0) {
887 } else if ((f
= open (in_fname
, O_RDONLY
, 0666)) < 0)
890 if (file_size_and_mode (f
, &st_mode
, &st_size
))
892 fp
->fname
= in_fname
;
894 /* JF all this is mine about reading pipes and ttys */
895 if (!S_ISREG (st_mode
)) {
896 /* Read input from a file that is not a normal disk file.
897 We cannot preallocate a buffer with the correct size,
898 so we must read in the file a piece at the time and make it bigger. */
906 fp
->buf
= (U_CHAR
*) xmalloc (bsize
+ 2);
909 cnt
= read (f
, bufp
, bsize
- size
);
910 if (cnt
< 0) goto sys_error
; /* error! */
911 if (cnt
== 0) break; /* End of file */
914 if (bsize
== size
) { /* Buffer is full! */
916 fp
->buf
= (U_CHAR
*) xrealloc (fp
->buf
, bsize
+ 2);
917 bufp
= fp
->buf
+ size
; /* May have moved */
922 /* Read a file whose size we can determine in advance.
923 For the sake of VMS, st_size is just an upper bound. */
926 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
928 while (st_size
> 0) {
929 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
939 fp
->if_stack
= if_stack
;
941 /* Make sure data ends with a newline. And put a null after it. */
943 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
944 fp
->buf
[fp
->length
++] = '\n';
945 fp
->buf
[fp
->length
] = '\0';
947 /* Now that we know the input file is valid, open the output. */
949 if (!out_fname
|| !strcmp (out_fname
, ""))
950 out_fname
= "stdout";
951 else if (! freopen (out_fname
, "w", stdout
))
952 pfatal_with_name (out_fname
);
954 output_line_command (fp
, &outbuf
, 0, same_file
);
956 /* Scan the input, processing macros and directives. */
960 /* Now we have processed the entire input
961 Write whichever kind of output has been requested. */
966 else if (! inhibit_output
)
967 if (write (fileno (stdout
), outbuf
.buf
, outbuf
.bufp
- outbuf
.buf
) < 0)
968 fatal ("I/O error on output");
970 /* Don't write the deps file if preprocessing has failed. */
971 if (print_deps
&& errors
== 0)
974 /* Destruct the deps object. */
978 fatal ("I/O error on output");
981 exit (FATAL_EXIT_CODE
);
982 exit (SUCCESS_EXIT_CODE
);
985 pfatal_with_name (in_fname
);
988 /* Set up dependency-file output. */
990 init_dependency_output ()
992 char *spec
, *s
, *output_file
;
994 /* Either of two environment variables can specify output of deps.
995 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
996 where OUTPUT_FILE is the file to write deps info to
997 and DEPS_TARGET is the target to mention in the deps. */
1001 spec
= getenv ("DEPENDENCIES_OUTPUT");
1006 spec
= getenv ("SUNPRO_DEPENDENCIES");
1013 /* Find the space before the DEPS_TARGET, if there is one. */
1014 s
= strchr (spec
, ' ');
1017 /* Let the caller perform MAKE quoting. */
1018 deps_add_target (deps
, s
+ 1, 0);
1019 output_file
= (char *) xmalloc (s
- spec
+ 1);
1020 memcpy (output_file
, spec
, s
- spec
);
1021 output_file
[s
- spec
] = 0;
1026 /* Command line overrides environment variables. */
1028 deps_file
= output_file
;
1032 /* If dependencies go to standard output, or -MG is used, we should
1033 suppress output. The user may be requesting other stuff to
1034 stdout, with -dM, -v etc. We let them shoot themselves in the
1036 if (deps_file
== 0 || deps_missing_files
)
1040 /* Use mkdeps.c to output dependency information. */
1044 /* Stream on which to print the dependency information. */
1045 FILE *deps_stream
= 0;
1046 const char *const deps_mode
= deps_append
? "a" : "w";
1049 deps_stream
= stdout
;
1052 deps_stream
= fopen (deps_file
, deps_mode
);
1053 if (deps_stream
== 0)
1055 error_from_errno (deps_file
);
1060 deps_write (deps
, deps_stream
, 72);
1062 if (print_deps_phony_targets
)
1063 deps_phony_targets (deps
, deps_stream
);
1065 /* Don't close stdout. */
1068 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
1069 fatal ("I/O error on output");
1073 /* Move all backslash-newline pairs out of embarrassing places.
1074 Exchange all such pairs following BP
1075 with any potentially-embarrasing characters that follow them.
1076 Potentially-embarrassing characters are / and *
1077 (because a backslash-newline inside a comment delimiter
1078 would cause it not to be recognized). */
1086 /* First count the backslash-newline pairs here. */
1088 while (*p
++ == '\\' && *p
++ == '\n')
1093 /* Exit if what follows the backslash-newlines is not embarrassing. */
1095 if (count
== 0 || (*p
!= '/' && *p
!= '*'))
1098 /* Copy all potentially embarrassing characters
1099 that follow the backslash-newline pairs
1100 down to where the pairs originally started. */
1102 while (*p
== '*' || *p
== '/')
1105 /* Now write the same number of pairs after the embarrassing chars. */
1106 while (count
-- > 0) {
1112 /* Like newline_fix but for use within a directive-name.
1113 Move any backslash-newlines up past any following symbol constituents. */
1115 name_newline_fix (bp
)
1121 /* First count the backslash-newline pairs here. */
1123 while (*p
++ == '\\' && *p
++ == '\n')
1128 /* What follows the backslash-newlines is not embarrassing. */
1130 if (count
== 0 || !is_idchar (*p
))
1133 /* Copy all potentially embarrassing characters
1134 that follow the backslash-newline pairs
1135 down to where the pairs originally started. */
1137 while (is_idchar (*p
))
1140 /* Now write the same number of pairs after the embarrassing chars. */
1141 while (count
-- > 0) {
1148 * The main loop of the program.
1150 * Read characters from the input stack, transferring them to the
1153 * Macros are expanded and push levels on the input stack.
1154 * At the end of such a level it is popped off and we keep reading.
1155 * At the end of any other kind of level, we return.
1156 * #-directives are handled, except within macros.
1158 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1159 * and insert them when appropriate. This is set while scanning macro
1160 * arguments before substitution. It is zero when scanning for final output.
1161 * There are three types of Newline markers:
1162 * * Newline - follows a macro name that was not expanded
1163 * because it appeared inside an expansion of the same macro.
1164 * This marker prevents future expansion of that identifier.
1165 * When the input is rescanned into the final output, these are deleted.
1166 * These are also deleted by ## concatenation.
1167 * * Newline Space (or Newline and any other whitespace character)
1168 * stands for a place that tokens must be separated or whitespace
1169 * is otherwise desirable, but where the ANSI standard specifies there
1170 * is no whitespace. This marker turns into a Space (or whichever other
1171 * whitespace char appears in the marker) in the final output,
1172 * but it turns into nothing in an argument that is stringified with #.
1173 * Such stringified arguments are the only place where the ANSI standard
1174 * specifies with precision that whitespace may not appear.
1176 * During this function, IP->bufp is kept cached in IBP for speed of access.
1177 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1178 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1179 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1180 * explicitly, and before RECACHE, since RECACHE uses OBP.
1184 rescan (op
, output_marks
)
1188 /* Character being scanned in main loop. */
1191 /* Length of pending accumulated identifier. */
1192 int ident_length
= 0;
1194 /* Hash code of pending accumulated identifier. */
1197 /* Current input level (&instack[indepth]). */
1200 /* Pointer for scanning input. */
1203 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1206 /* Pointer for storing output. */
1209 /* REDO_CHAR is nonzero if we are processing an identifier
1210 after backing up over the terminating character.
1211 Sometimes we process an identifier without backing up over
1212 the terminating character, if the terminating character
1213 is not special. Backing up is done so that the terminating character
1214 will be dispatched on again once the identifier is dealt with. */
1217 /* 1 if within an identifier inside of which a concatenation
1218 marker (Newline -) has been seen. */
1219 int concatenated
= 0;
1221 /* While scanning a comment or a string constant,
1222 this records the line it started on, for error messages. */
1225 /* Record position of last `real' newline. */
1226 U_CHAR
*beg_of_line
;
1228 /* This has to be a global bacause of RECACHE. */
1229 U_CHAR
*obufp_before_macroname
= NULL
;
1231 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1234 do { ip->macro->type = T_MACRO; \
1235 if (ip->free_ptr) free (ip->free_ptr); \
1236 --indepth; } while (0)
1238 /* Reload `rescan's local variables that describe the current
1239 level of the input stack. */
1242 do { ip = &instack[indepth]; \
1244 limit = ip->buf + ip->length; \
1246 check_expand (op, limit - ibp); \
1248 obufp_before_macroname += op->bufp - obp; \
1249 obp = op->bufp; } while (0)
1251 if (no_output
&& instack
[indepth
].fname
!= 0)
1252 skip_if_group (&instack
[indepth
], 1);
1258 /* Our caller must always put a null after the end of
1259 the input at each input stack level. */
1272 /* Always merge lines ending with backslash-newline,
1273 even in middle of identifier. */
1276 --obp
; /* remove backslash from obuf */
1279 /* Otherwise, backslash suppresses specialness of following char,
1280 so copy it here to prevent the switch from seeing it.
1281 But first get any pending identifier processed. */
1282 if (ident_length
> 0)
1288 /* If this is expanding a macro definition, don't recognize
1289 preprocessor directives. */
1295 /* # keyword: a # must be the first char on the line */
1296 if (beg_of_line
== 0)
1298 if (beg_of_line
+ 1 != ibp
)
1301 /* This # can start a directive. */
1303 --obp
; /* Don't copy the '#' */
1307 if (! handle_directive (ip
, op
)) {
1311 /* Not a known directive: treat it as ordinary text.
1312 IP, OP, IBP, etc. have not been changed. */
1313 if (no_output
&& instack
[indepth
].fname
) {
1314 /* If not generating expanded output,
1315 what we do with ordinary text is skip it.
1316 Discard everything until next # directive. */
1317 skip_if_group (&instack
[indepth
], 1);
1322 ++obp
; /* Copy the '#' after all */
1328 /* A # directive has been successfully processed. */
1329 /* If not generating expanded output, ignore everything until
1330 next # directive. */
1331 if (no_output
&& instack
[indepth
].fname
)
1332 skip_if_group (&instack
[indepth
], 1);
1338 case '\"': /* skip quoted string */
1340 /* A single quoted string is treated like a double -- some
1341 programs (e.g., troff) are perverse this way */
1346 start_line
= ip
->lineno
;
1348 /* Skip ahead to a matching quote. */
1352 if (ip
->macro
!= 0) {
1353 /* try harder: this string crosses a macro expansion boundary */
1365 /* Traditionally, end of line ends a string constant with no error.
1366 So exit the loop and record the new line. */
1374 /* Backslash newline is replaced by nothing at all,
1375 but keep the line counts correct. */
1380 /* ANSI stupidly requires that in \\ the second \
1381 is *not* prevented from combining with a newline. */
1382 while (*ibp
== '\\' && ibp
[1] == '\n') {
1401 if (*ibp
== '\\' && ibp
[1] == '\n')
1403 /* Don't look for comments inside a macro definition. */
1406 /* A comment constitutes white space, so it can terminate an identifier.
1407 Process the identifier, if any. */
1414 /* We have a comment. Skip it, optionally copying it to output. */
1416 start_line
= ip
->lineno
;
1418 ++ibp
; /* Skip the star. */
1420 /* In K+R C, a comment is equivalent to nothing. Note that we
1421 already output the slash; we might not want it. */
1422 if (! put_out_comments
)
1428 U_CHAR
*before_bp
= ibp
;
1430 while (ibp
< limit
) {
1433 if (warn_comments
&& ibp
< limit
&& *ibp
== '*')
1434 warning("`/*' within comment");
1437 if (*ibp
== '\\' && ibp
[1] == '\n')
1439 if (ibp
>= limit
|| *ibp
== '/')
1444 /* Copy the newline into the output buffer, in order to
1445 avoid the pain of a #line every time a multiline comment
1447 if (!put_out_comments
)
1455 error_with_line (line_for_error (start_line
),
1456 "unterminated comment");
1459 if (put_out_comments
) {
1460 memcpy (obp
, before_bp
, ibp
- before_bp
);
1461 obp
+= ibp
- before_bp
;
1467 case '0': case '1': case '2': case '3': case '4':
1468 case '5': case '6': case '7': case '8': case '9':
1469 /* If digit is not part of identifier, it starts a number,
1470 which means that following letters are not an identifier.
1471 "0x5" does not refer to an identifier "x5".
1472 So copy all alphanumerics that follow without accumulating
1473 as an identifier. Periods also, for sake of "3.e7". */
1475 if (ident_length
== 0) {
1476 while (ibp
< limit
) {
1477 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1482 if (! ISIDNUM (c
) && c
!= '.') {
1487 /* A sign can be part of a preprocessing number
1488 if it follows an e. */
1489 if (c
== 'e' || c
== 'E') {
1490 while (ibp
< limit
&& ibp
[0] == '\\' && ibp
[1] == '\n') {
1494 if (ibp
< limit
&& (*ibp
== '+' || *ibp
== '-')) {
1496 /* Traditional C does not let the token go past the sign. */
1506 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1507 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1508 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1509 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1511 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1512 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1513 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1514 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1517 /* Compute step of hash function, to avoid a proc call on every token */
1518 hash
= HASHSTEP (hash
, c
);
1522 /* If reprocessing a macro expansion, newline is a special marker. */
1523 if (ip
->macro
!= 0) {
1524 /* Newline White is a "funny space" to separate tokens that are
1525 supposed to be separate but without space between.
1526 Here White means any horizontal whitespace character.
1527 Newline - marks a recursive macro use that is not
1528 supposed to be expandable. */
1531 /* Newline - inhibits expansion of preceding token.
1532 If expanding a macro arg, we keep the newline -.
1533 In final output, it is deleted. */
1534 if (! concatenated
) {
1539 if (!output_marks
) {
1542 /* If expanding a macro arg, keep the newline -. */
1545 } else if (is_space (*ibp
)) {
1546 /* Newline Space does not prevent expansion of preceding token
1547 so expand the preceding token and then come back. */
1548 if (ident_length
> 0)
1551 /* If generating final output, newline space makes a space. */
1552 if (!output_marks
) {
1554 /* And Newline Newline makes a newline, so count it. */
1555 if (obp
[-1] == '\n')
1558 /* If expanding a macro arg, keep the newline space.
1559 If the arg gets stringified, newline space makes nothing. */
1562 } else abort (); /* Newline followed by something random? */
1566 /* If there is a pending identifier, handle it and come back here. */
1567 if (ident_length
> 0)
1572 /* Update the line counts and output a #line if necessary. */
1575 if (ip
->lineno
!= op
->lineno
) {
1577 output_line_command (ip
, op
, 1, same_file
);
1578 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
1583 /* Come here either after (1) a null character that is part of the input
1584 or (2) at the end of the input, because there is a null there. */
1587 /* Our input really contains a null character. */
1590 /* At end of a macro-expansion level, pop it and read next level. */
1591 if (ip
->macro
!= 0) {
1594 /* If we have an identifier that ends here, process it now, so
1595 we get the right error for recursion. */
1596 if (ident_length
&& ! is_idchar (*instack
[indepth
- 1].bufp
)) {
1605 /* If we don't have a pending identifier,
1606 return at end of input. */
1607 if (ident_length
== 0) {
1615 /* If we do have a pending identifier, just consider this null
1616 a special character and arrange to dispatch on it again.
1617 The second time, IDENT_LENGTH will be zero so we will return. */
1623 /* Handle the case of a character such as /, ', " or null
1624 seen following an identifier. Back over it so that
1625 after the identifier is processed the special char
1626 will be dispatched on again. */
1636 if (ident_length
> 0) {
1639 /* We have just seen an identifier end. If it's a macro, expand it.
1641 IDENT_LENGTH is the length of the identifier
1642 and HASH is its hash code.
1644 The identifier has already been copied to the output,
1645 so if it is a macro we must remove it.
1647 If REDO_CHAR is 0, the char that terminated the identifier
1648 has been skipped in the output and the input.
1649 OBP-IDENT_LENGTH-1 points to the identifier.
1650 If the identifier is a macro, we must back over the terminator.
1652 If REDO_CHAR is 1, the terminating char has already been
1653 backed over. OBP-IDENT_LENGTH points to the identifier. */
1655 for (hp
= hashtab
[MAKE_POS (hash
) % HASHSIZE
]; hp
!= NULL
;
1658 if (hp
->length
== ident_length
) {
1659 /* obufp_before_macroname is used only in this block,
1660 but it has to be global because of RECACHE. */
1661 int op_lineno_before_macroname
;
1662 int i
= ident_length
;
1663 U_CHAR
*p
= hp
->name
;
1664 U_CHAR
*q
= obp
- i
;
1669 do { /* All this to avoid a strncmp () */
1674 /* We found a use of a macro name.
1675 see if the context shows it is a macro call. */
1677 /* Back up over terminating character if not already done. */
1683 obufp_before_macroname
= obp
- ident_length
;
1684 op_lineno_before_macroname
= op
->lineno
;
1686 /* If macro wants an arglist, verify that a '(' follows.
1687 first skip all whitespace, copying it to the output
1688 after the macro name. Then, if there is no '(',
1689 decide this is not a macro call and leave things that way. */
1690 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
1693 /* Scan forward over whitespace, copying it to the output. */
1694 if (ibp
== limit
&& ip
->macro
!= 0) {
1698 /* A comment: copy it unchanged or discard it. */
1699 else if (*ibp
== '/' && ibp
+1 != limit
&& ibp
[1] == '*') {
1700 if (put_out_comments
) {
1705 while (ibp
+ 1 != limit
1706 && !(ibp
[0] == '*' && ibp
[1] == '/')) {
1707 /* We need not worry about newline-marks,
1708 since they are never found in comments. */
1710 /* Newline in a file. Count it. */
1714 if (put_out_comments
)
1720 if (put_out_comments
) {
1725 else if (is_space (*ibp
)) {
1727 if (ibp
[-1] == '\n') {
1728 if (ip
->macro
== 0) {
1729 /* Newline in a file. Count it. */
1732 } else if (!output_marks
) {
1733 /* A newline mark, and we don't want marks
1734 in the output. If it is newline-hyphen,
1735 discard it entirely. Otherwise, it is
1736 newline-whitechar, so keep the whitechar. */
1746 /* A newline mark; copy both chars to the output. */
1757 /* This is now known to be a macro call.
1758 Discard the macro name from the output,
1759 along with any following whitespace just copied. */
1760 obp
= obufp_before_macroname
;
1761 op
->lineno
= op_lineno_before_macroname
;
1763 /* Expand the macro, reading arguments as needed,
1764 and push the expansion on the input stack. */
1767 macroexpand (hp
, op
);
1769 /* Reexamine input stack, since macroexpand has pushed
1770 a new level on it. */
1777 } /* End hash-table-search loop */
1778 ident_length
= hash
= 0; /* Stop collecting identifier */
1781 } /* End if (ident_length > 0) */
1783 } /* End per-char loop */
1785 /* Come here to return -- but first give an error message
1786 if there was an unterminated successful conditional. */
1788 if (if_stack
!= ip
->if_stack
) {
1790 switch (if_stack
->type
) {
1809 error_with_line (line_for_error (if_stack
->lineno
),
1810 "unterminated #%s conditional", str
);
1812 if_stack
= ip
->if_stack
;
1816 * Rescan a string into a temporary buffer and return the result
1817 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1819 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1820 * and insert such markers when appropriate. See `rescan' for details.
1821 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1822 * before substitution; it is 0 for other uses.
1825 expand_to_temp_buffer (buf
, limit
, output_marks
)
1826 const U_CHAR
*buf
, *limit
;
1831 int length
= limit
- buf
;
1833 int odepth
= indepth
;
1838 /* Set up the input on the input stack. */
1840 buf1
= (U_CHAR
*) alloca (length
+ 1);
1842 const U_CHAR
*p1
= buf
;
1850 /* Set up to receive the output. */
1852 obuf
.length
= length
* 2 + 100; /* Usually enough. Why be stingy? */
1853 obuf
.bufp
= obuf
.buf
= (U_CHAR
*) xmalloc (obuf
.length
);
1858 CHECK_DEPTH ({return obuf
;});
1862 ip
= &instack
[indepth
];
1866 ip
->length
= length
;
1867 ip
->buf
= ip
->bufp
= buf1
;
1868 ip
->if_stack
= if_stack
;
1870 ip
->lineno
= obuf
.lineno
= 1;
1872 /* Scan the input, create the output. */
1874 rescan (&obuf
, output_marks
);
1876 /* Pop input stack to original state. */
1879 if (indepth
!= odepth
)
1882 /* Record the output. */
1883 obuf
.length
= obuf
.bufp
- obuf
.buf
;
1889 * Process a # directive. Expects IP->bufp to point to the '#', as in
1890 * `#define foo bar'. Passes to the command handler
1891 * (do_define, do_include, etc.): the addresses of the 1st and
1892 * last chars of the command (starting immediately after the #
1893 * keyword), plus op and the keyword table pointer. If the command
1894 * contains comments it is copied into a temporary buffer sans comments
1895 * and the temporary buffer is passed to the command handler instead.
1896 * Likewise for backslash-newlines.
1898 * Returns nonzero if this was a known # directive.
1899 * Otherwise, returns zero, without advancing the input pointer.
1903 handle_directive (ip
, op
)
1907 const struct directive
*kt
;
1911 /* Nonzero means we must copy the entire command
1912 to get rid of comments or backslash-newlines. */
1913 int copy_command
= 0;
1915 U_CHAR
*ident
, *after_ident
;
1918 /* Skip whitespace and \-newline. */
1920 if (is_nvspace (*bp
))
1922 else if (*bp
== '/' && (newline_fix (bp
+ 1), bp
[1]) == '*') {
1924 skip_to_end_of_comment (ip
, &ip
->lineno
);
1926 } else if (*bp
== '\\' && bp
[1] == '\n') {
1927 bp
+= 2; ip
->lineno
++;
1931 /* Now find end of directive name.
1932 If we encounter a backslash-newline, exchange it with any following
1933 symbol-constituents so that we end up with a contiguous name. */
1937 if (is_idchar (*cp
))
1940 if (*cp
== '\\' && cp
[1] == '\n')
1941 name_newline_fix (cp
);
1942 if (is_idchar (*cp
))
1947 ident_length
= cp
- bp
;
1951 /* A line of just `#' becomes blank. */
1953 if (ident_length
== 0 && *after_ident
== '\n') {
1954 ip
->bufp
= after_ident
;
1959 * Decode the keyword and call the appropriate expansion
1960 * routine, after moving the input pointer up to the next line.
1962 for (kt
= directive_table
; kt
->length
> 0; kt
++) {
1963 if (kt
->length
== ident_length
1964 && !strncmp (kt
->name
, (const char *)ident
, ident_length
)) {
1966 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
1967 int unterminated
= 0;
1969 /* Nonzero means do not delete comments within the directive.
1970 #define needs this to detect traditional token paste. */
1971 int keep_comments
= kt
->type
== T_DEFINE
;
1973 /* Find the end of this command (first newline not backslashed
1974 and not in a string or comment).
1975 Set COPY_COMMAND if the command must be copied
1976 (it contains a backslash-newline or a comment). */
1978 buf
= bp
= after_ident
;
1979 while (bp
< limit
) {
1994 bp
= skip_quoted_string (bp
- 1, limit
, ip
->lineno
, &ip
->lineno
, ©_command
, &unterminated
);
1996 /* Traditional preprocessing permits unterminated strings. */
2002 /* <...> is special for #include. */
2004 if (kt
->type
!= T_INCLUDE
)
2006 while (*bp
&& *bp
!= '>') bp
++;
2010 if (*bp
== '\\' && bp
[1] == '\n')
2013 U_CHAR
*obp
= bp
- 1;
2015 skip_to_end_of_comment (ip
, &ip
->lineno
);
2017 /* No need to copy the command because of a comment at the end;
2018 just don't include the comment in the directive. */
2019 if (bp
== limit
|| *bp
== '\n') {
2023 /* Don't remove the comments if this is #define. */
2024 if (! keep_comments
)
2030 --bp
; /* Point to the newline */
2038 resume_p
= ip
->bufp
;
2039 /* BP is the end of the directive.
2040 RESUME_P is the next interesting data after the directive.
2041 A comment may come between. */
2045 /* Need to copy entire command into temp buffer before dispatching */
2047 cp
= (U_CHAR
*) alloca (bp
- buf
+ 5); /* room for cmd plus
2051 /* Copy to the new buffer, deleting comments
2052 and backslash-newlines (and whitespace surrounding the latter). */
2062 /* <...> is special for #include. */
2064 if (kt
->type
!= T_INCLUDE
)
2066 while (xp
< bp
&& c
!= '>') {
2068 if (c
== '\\' && xp
< bp
&& *xp
== '\n')
2079 if (cp
!= buf
&& is_space (cp
[-1])) {
2080 while (cp
!= buf
&& is_space(cp
[-1])) cp
--;
2082 SKIP_WHITE_SPACE (xp
);
2083 } else if (is_nvspace (*xp
)) {
2085 SKIP_WHITE_SPACE (xp
);
2096 = skip_quoted_string (xp
- 1, limit
, ip
->lineno
, 0, 0, 0);
2105 skip_to_end_of_comment (ip
, 0);
2107 while (xp
!= ip
->bufp
)
2109 /* Delete the slash. */
2117 /* Null-terminate the copy. */
2124 ip
->bufp
= resume_p
;
2126 /* Call the appropriate command handler. buf now points to
2127 either the appropriate place in the input buffer, or to
2128 the temp buffer if it was necessary to make one. cp
2129 points to the first char after the contents of the (possibly
2130 copied) command, in either case. */
2131 (*kt
->func
) (buf
, cp
, op
);
2132 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
2141 static const char *const
2142 monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2143 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2146 * expand things like __FILE__. Place the expansion into the output
2147 * buffer *without* rescanning.
2150 special_symbol (hp
, op
)
2158 FILE_BUF
*ip
= NULL
;
2159 static struct tm
*timebuf
= NULL
;
2161 int paren
= 0; /* For special `defined' keyword */
2163 for (i
= indepth
; i
>= 0; i
--)
2164 if (instack
[i
].fname
!= NULL
) {
2169 fatal ("not in any file?!");
2176 if (hp
->type
== T_FILE
)
2179 string
= instack
[0].fname
;
2183 char *tmp
= (char *) alloca (3 + strlen (string
));
2184 sprintf (tmp
, "\"%s\"", string
);
2193 case T_INCLUDE_LEVEL
:
2195 char *tmp
= (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2197 for (i
= indepth
; i
>= 0; i
--)
2198 if (instack
[i
].fname
!= NULL
)
2201 sprintf (tmp
, "%d", true_indepth
- 1);
2208 char *tmp
= (char *) alloca (3 + strlen (version_string
));
2209 sprintf (tmp
, "\"%s\"", version_string
);
2215 buf
= hp
->value
.cpval
;
2220 char *tmp
= (char *) alloca (10);
2221 sprintf (tmp
, "%d", ip
->lineno
);
2229 char *tmp
= (char *) alloca (20);
2231 if (timebuf
== NULL
) {
2233 timebuf
= localtime (&t
);
2235 if (hp
->type
== T_DATE
)
2236 sprintf (tmp
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2237 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2239 sprintf (tmp
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2245 case T_SPEC_DEFINED
:
2246 buf
= " 0 "; /* Assume symbol is not defined */
2247 ip
= &instack
[indepth
];
2248 SKIP_WHITE_SPACE (ip
->bufp
);
2249 if (*ip
->bufp
== '(') {
2251 ip
->bufp
++; /* Skip over the paren */
2252 SKIP_WHITE_SPACE (ip
->bufp
);
2255 if (!is_idstart (*ip
->bufp
))
2258 HASHNODE
*hp
= lookup (ip
->bufp
, -1, -1);
2260 if (hp
&& hp
->type
!= T_UNUSED
&& hp
->type
!= T_SPEC_DEFINED
)
2263 while (is_idchar (*ip
->bufp
))
2265 SKIP_WHITE_SPACE (ip
->bufp
);
2267 if (*ip
->bufp
!= ')')
2275 error ("`defined' must be followed by ident or (ident)");
2279 error ("cccp error: invalid special hash type"); /* time for gdb */
2283 check_expand (op
, len
);
2284 memcpy (op
->bufp
, buf
, len
);
2289 /* Routines to handle #directives */
2292 * Process include file by reading it in and calling rescan.
2293 * Expects to see "fname" or <fname> on the input.
2296 do_include (buf
, limit
, op
)
2297 U_CHAR
*buf
, *limit
;
2300 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2302 struct file_name_list
*stackp
= include
; /* Chain of dirs to search */
2303 struct file_name_list dsp
[1]; /* First in chain, if #include "..." */
2306 int retried
= 0; /* Have already tried macro
2307 expanding the include line*/
2308 FILE_BUF trybuf
; /* It got expanded into here */
2309 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2314 SKIP_WHITE_SPACE (fbeg
);
2315 /* Discard trailing whitespace so we can easily see
2316 if we have parsed all the significant chars we were given. */
2317 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2322 while (fend
!= limit
&& *fend
!= '\"')
2324 if (*fend
== '\"' && fend
+ 1 == limit
) {
2327 /* We have "filename". Figure out directory this source
2328 file is coming from and put it on the front of the list. */
2330 /* If -I- was specified, don't search current dir, only spec'd ones. */
2331 if (ignore_srcdir
) break;
2333 for (fp
= &instack
[indepth
]; fp
>= instack
; fp
--)
2336 const char *ep
, *nam
;
2338 if ((nam
= fp
->fname
) != NULL
) {
2339 /* Found a named file. Figure out dir of the file,
2340 and put it in front of the search list. */
2341 dsp
[0].next
= stackp
;
2343 ep
= strrchr (nam
, '/');
2347 f
= (char *) alloca (n
+ 1);
2348 strncpy (f
, nam
, n
);
2351 if (n
> max_include_len
) max_include_len
= n
;
2353 dsp
[0].fname
= 0; /* Current directory */
2364 while (fend
!= limit
&& *fend
!= '>') fend
++;
2365 if (*fend
== '>' && fend
+ 1 == limit
) {
2366 system_header_p
= 1;
2367 /* If -I-, start with the first -I dir after the -I-. */
2368 if (first_bracket_include
)
2369 stackp
= first_bracket_include
;
2377 error ("#include expects \"fname\" or <fname>");
2380 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2381 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2382 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2383 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2391 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2395 do_include_next (buf
, limit
, op
)
2396 U_CHAR
*buf
, *limit
;
2399 U_CHAR
*fbeg
, *fend
; /* Beginning and end of fname */
2401 struct file_name_list
*stackp
; /* Chain of dirs to search */
2404 int retried
= 0; /* Have already tried macro
2405 expanding the include line*/
2406 FILE_BUF trybuf
; /* It got expanded into here */
2407 int system_header_p
= 0; /* 0 for "...", 1 for <...> */
2409 /* Treat as plain #include if we don't know where to start
2411 stackp
= instack
[indepth
].next_header_dir
;
2414 do_include (buf
, limit
, op
);
2421 SKIP_WHITE_SPACE (fbeg
);
2422 /* Discard trailing whitespace so we can easily see
2423 if we have parsed all the significant chars we were given. */
2424 while (limit
!= fbeg
&& is_nvspace (limit
[-1])) limit
--;
2429 while (fend
!= limit
&& *fend
!= '\"')
2431 if (*fend
== '\"' && fend
+ 1 == limit
)
2437 while (fend
!= limit
&& *fend
!= '>') fend
++;
2438 if (*fend
== '>' && fend
+ 1 == limit
) {
2439 system_header_p
= 1;
2447 error ("#include expects \"fname\" or <fname>");
2450 trybuf
= expand_to_temp_buffer (buf
, limit
, 0);
2451 buf
= (U_CHAR
*) alloca (trybuf
.bufp
- trybuf
.buf
+ 1);
2452 memcpy (buf
, trybuf
.buf
, trybuf
.bufp
- trybuf
.buf
);
2453 limit
= buf
+ (trybuf
.bufp
- trybuf
.buf
);
2461 process_include (stackp
, fbeg
, flen
, system_header_p
, op
);
2465 process_include (stackp
, fbeg
, flen
, system_header_p
, op
)
2466 struct file_name_list
*stackp
;
2469 int system_header_p
;
2473 int f
= -1; /* file number */
2475 fname
= (char *) alloca (max_include_len
+ flen
+ 2);
2476 /* + 2 above for slash and terminating null. */
2478 /* If specified file name is absolute, just open it. */
2480 if (IS_ABSOLUTE_PATHNAME (fbeg
)) {
2481 strncpy (fname
, (const char *)fbeg
, flen
);
2483 f
= open (fname
, O_RDONLY
, 0666);
2485 /* Search directory path, trying to open the file.
2486 Copy each filename tried into FNAME. */
2488 for (; stackp
; stackp
= stackp
->next
) {
2489 if (stackp
->fname
) {
2490 strcpy (fname
, stackp
->fname
);
2491 strcat (fname
, "/");
2492 fname
[strlen (fname
) + flen
] = 0;
2496 strncat (fname
, (const char *)fbeg
, flen
);
2497 if ((f
= open (fname
, O_RDONLY
, 0666)) >= 0)
2503 strncpy (fname
, (const char *)fbeg
, flen
);
2505 if (deps_missing_files
2506 && print_deps
> (system_header_p
|| (system_include_depth
> 0))) {
2508 /* If requested as a system header, assume it belongs in
2509 the first system header directory. */
2510 if (first_bracket_include
)
2511 stackp
= first_bracket_include
;
2515 if (!system_header_p
|| IS_ABSOLUTE_PATHNAME (fbeg
) || !stackp
->fname
)
2516 deps_add_dep (deps
, fname
);
2519 int len
= strlen(stackp
->fname
);
2521 p
= (char *) alloca (len
+ flen
+ 2);
2522 memcpy (p
, stackp
->fname
, len
);
2524 memcpy (p
+ len
, fbeg
, flen
);
2527 deps_add_dep (deps
, p
);
2529 } else if (print_deps
2530 && print_deps
<= (system_header_p
2531 || (system_include_depth
> 0)))
2532 warning ("no include path in which to find %.*s", flen
, fbeg
);
2534 error_from_errno (fname
);
2538 /* Check to see if this include file is a once-only include file.
2541 struct file_name_list
* ptr
;
2543 for (ptr
= dont_repeat_files
; ptr
; ptr
= ptr
->next
) {
2544 if (!strcmp (ptr
->fname
, fname
)) {
2546 return; /* This file was once'd. */
2550 for (ptr
= all_include_files
; ptr
; ptr
= ptr
->next
) {
2551 if (!strcmp (ptr
->fname
, fname
))
2552 break; /* This file was included before. */
2556 /* This is the first time for this file. */
2557 /* Add it to list of files included. */
2559 ptr
= (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
2560 ptr
->next
= all_include_files
;
2561 all_include_files
= ptr
;
2562 ptr
->fname
= xstrdup (fname
);
2564 /* For -M, add this file to the dependencies. */
2565 if (print_deps
> (system_header_p
|| (system_include_depth
> 0)))
2566 deps_add_dep (deps
, fname
);
2569 if (system_header_p
)
2570 system_include_depth
++;
2572 /* Actually process the file. */
2573 finclude (f
, fname
, stackp
->next
, op
);
2575 if (system_header_p
)
2576 system_include_depth
--;
2582 /* Process the contents of include file FNAME, already open on descriptor F,
2583 with output to OP. */
2586 finclude (f
, fname
, nhd
, op
)
2589 struct file_name_list
*nhd
;
2595 FILE_BUF
*fp
; /* For input stack frame */
2597 CHECK_DEPTH (return;);
2599 if (file_size_and_mode (f
, &st_mode
, &st_size
))
2602 fp
= &instack
[indepth
+ 1];
2603 memset (fp
, 0, sizeof (FILE_BUF
));
2607 fp
->if_stack
= if_stack
;
2608 fp
->next_header_dir
= nhd
;
2610 if (S_ISREG (st_mode
)) {
2611 fp
->buf
= (U_CHAR
*) xmalloc (st_size
+ 2);
2614 /* Read the file contents, knowing that st_size is an upper bound
2615 on the number of bytes we can read. */
2616 while (st_size
> 0) {
2617 i
= read (f
, fp
->buf
+ fp
->length
, st_size
);
2627 /* Cannot count its file size before reading. */
2634 basep
= (U_CHAR
*) xmalloc (bsize
+ 2);
2638 i
= read (f
, bufp
, bsize
- st_size
);
2640 goto nope
; /* error! */
2642 break; /* End of file */
2645 if (bsize
== st_size
) { /* Buffer is full! */
2647 basep
= (U_CHAR
*) xrealloc (basep
, bsize
+ 2);
2648 bufp
= basep
+ st_size
; /* May have moved */
2653 fp
->length
= st_size
;
2657 /* Make sure data ends with a newline. And put a null after it. */
2659 if (fp
->length
> 0 && fp
->buf
[fp
->length
-1] != '\n')
2660 fp
->buf
[fp
->length
++] = '\n';
2661 fp
->buf
[fp
->length
] = '\0';
2664 output_line_command (fp
, op
, 0, enter_file
);
2667 instack
[indepth
].lineno
++;
2668 instack
[indepth
].bufp
++; /* Skip the new line. */
2669 output_line_command (&instack
[indepth
], op
, 0, leave_file
);
2674 perror_with_name (fname
);
2679 /* Process a #define command.
2680 BUF points to the contents of the #define command, as a continguous string.
2681 LIMIT points to the first character past the end of the definition.
2682 KEYWORD is the keyword-table entry for #define. */
2685 do_define (buf
, limit
, op
)
2686 U_CHAR
*buf
, *limit
;
2687 FILE_BUF
*op ATTRIBUTE_UNUSED
;
2689 U_CHAR
*bp
; /* temp ptr into input buffer */
2690 U_CHAR
*symname
; /* remember where symbol name starts */
2691 int sym_length
; /* and how long it is */
2694 int arglengths
= 0; /* Accumulate lengths of arg names
2695 plus number of args. */
2700 while (is_nvspace (*bp
))
2703 symname
= bp
; /* remember where it starts */
2704 while (is_idchar (*bp
) && bp
< limit
) {
2707 sym_length
= bp
- symname
;
2708 if (sym_length
== 0)
2710 error ("invalid macro name");
2713 else if (!is_idstart (*symname
)) {
2714 U_CHAR
*msg
; /* what pain... */
2715 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
2716 memcpy (msg
, symname
, sym_length
);
2717 msg
[sym_length
] = 0;
2718 error ("invalid macro name `%s'", msg
);
2721 if (! strncmp ((const char *)symname
, "defined", 7) && sym_length
== 7)
2723 error ("\"defined\" cannot be used as a macro name");
2728 /* lossage will occur if identifiers or control keywords are broken
2729 across lines using backslash. This is not the right place to take
2733 struct arglist
*arg_ptrs
= NULL
;
2736 bp
++; /* skip '(' */
2737 SKIP_WHITE_SPACE (bp
);
2739 /* Loop over macro argument names. */
2740 while (*bp
!= ')') {
2741 struct arglist
*temp
;
2743 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
2745 temp
->next
= arg_ptrs
;
2746 temp
->argno
= argno
++;
2749 if (!is_idstart (*bp
))
2750 warning ("parameter name starts with a digit in #define");
2752 /* Find the end of the arg name. */
2753 while (is_idchar (*bp
)) {
2756 temp
->length
= bp
- temp
->name
;
2757 arglengths
+= temp
->length
+ 2;
2758 SKIP_WHITE_SPACE (bp
);
2759 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
2760 error ("badly punctuated parameter list in #define");
2765 SKIP_WHITE_SPACE (bp
);
2768 error ("unterminated parameter list in #define");
2773 ++bp
; /* skip paren */
2774 while (is_nvspace (*bp
) && bp
< limit
) /* and leading whitespace */
2776 /* now everything from bp before limit is the definition. */
2777 defn
= collect_expansion (bp
, limit
, argno
, arg_ptrs
);
2779 /* Now set defn->argnames to the result of concatenating
2780 the argument names in reverse order
2781 with comma-space between them. */
2783 struct arglist
*temp
;
2785 U_CHAR
*tmp
= (U_CHAR
*) xmalloc (arglengths
+ 1);
2787 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
2788 memcpy (&tmp
[i
], temp
->name
, temp
->length
);
2790 if (temp
->next
!= 0) {
2796 defn
->argnames
= tmp
;
2800 /* simple expansion or empty definition; skip leading whitespace */
2801 while (is_nvspace (*bp
) && bp
< limit
)
2803 /* now everything from bp before limit is the definition. */
2804 defn
= collect_expansion (bp
, limit
, -1, 0);
2805 defn
->argnames
= (const U_CHAR
*) "";
2808 hashcode
= hashf (symname
, sym_length
, HASHSIZE
);
2812 if ((hp
= lookup (symname
, sym_length
, hashcode
)) == NULL
)
2813 hp
= install (symname
, sym_length
, T_MACRO
, hashcode
);
2815 if (hp
->type
!= T_MACRO
|| compare_defs (defn
, hp
->value
.defn
))
2816 warning ("\"%.*s\" redefined", sym_length
, symname
);
2818 /* Replace the old definition. */
2822 hp
->value
.defn
= defn
;
2827 * return zero if two DEFINITIONs are isomorphic
2830 compare_defs (d1
, d2
)
2831 DEFINITION
*d1
, *d2
;
2833 struct reflist
*a1
, *a2
;
2834 U_CHAR
*p1
= d1
->expansion
;
2835 U_CHAR
*p2
= d2
->expansion
;
2838 if (d1
->nargs
!= d2
->nargs
)
2840 if (strcmp ((const char *)d1
->argnames
, (const char *)d2
->argnames
))
2842 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
2843 a1
= a1
->next
, a2
= a2
->next
) {
2844 if (!((a1
->nchars
== a2
->nchars
2845 && ! strncmp ((const char *)p1
, (const char *)p2
, a1
->nchars
))
2846 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
2847 || a1
->argno
!= a2
->argno
2848 || a1
->stringify
!= a2
->stringify
2849 || a1
->raw_before
!= a2
->raw_before
2850 || a1
->raw_after
!= a2
->raw_after
)
2858 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
2859 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
2864 /* Return 1 if two parts of two macro definitions are effectively different.
2865 One of the parts starts at BEG1 and has LEN1 chars;
2866 the other has LEN2 chars at BEG2.
2867 Any sequence of whitespace matches any other sequence of whitespace.
2868 FIRST means these parts are the first of a macro definition;
2869 so ignore leading whitespace entirely.
2870 LAST means these parts are the last of a macro definition;
2871 so ignore trailing whitespace entirely. */
2873 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
2875 const U_CHAR
*beg1
, *beg2
;
2879 const U_CHAR
*end1
= beg1
+ len1
;
2880 const U_CHAR
*end2
= beg2
+ len2
;
2882 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2883 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2886 while (beg1
!= end1
&& is_space (end1
[-1])) end1
--;
2887 while (beg2
!= end2
&& is_space (end2
[-1])) end2
--;
2889 while (beg1
!= end1
&& beg2
!= end2
) {
2890 if (is_space (*beg1
) && is_space (*beg2
)) {
2891 while (beg1
!= end1
&& is_space (*beg1
)) beg1
++;
2892 while (beg2
!= end2
&& is_space (*beg2
)) beg2
++;
2893 } else if (*beg1
== *beg2
) {
2897 return (beg1
!= end1
) || (beg2
!= end2
);
2900 /* Read a replacement list for a macro with parameters.
2901 Build the DEFINITION structure.
2902 Reads characters of text starting at BUF until LIMIT.
2903 ARGLIST specifies the formal parameters to look for
2904 in the text of the definition; NARGS is the number of args
2905 in that list, or -1 for a macro name that wants no argument list.
2906 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2907 and NAMELEN is its length in characters.
2909 Note that comments and backslash-newlines have already been deleted
2910 from the argument. */
2912 /* Leading and trailing Space, Tab, etc. are converted to markers
2913 Newline Space, Newline Tab, etc.
2914 Newline Space makes a space in the final output
2915 but is discarded if stringified. (Newline Tab is similar but
2916 makes a Tab instead.)
2918 If there is no trailing whitespace, a Newline Space is added at the end
2919 to prevent concatenation that would be contrary to the standard. */
2922 collect_expansion (buf
, end
, nargs
, arglist
)
2925 struct arglist
*arglist
;
2928 U_CHAR
*p
, *limit
, *lastp
, *exp_p
;
2929 struct reflist
*endpat
= NULL
;
2930 /* Pointer to first nonspace after last ## seen. */
2932 /* Pointer to first nonspace after last single-# seen. */
2933 U_CHAR
*stringify
= 0;
2935 int expected_delimiter
= '\0';
2937 /* Scan thru the replacement list, ignoring comments and quoted
2938 strings, picking up on the macro calls. It does a linear search
2939 thru the arg list on every potential symbol. Profiling might say
2940 that something smarter should happen. */
2945 /* Find the beginning of the trailing whitespace. */
2946 /* Find end of leading whitespace. */
2949 while (p
< limit
&& is_space (limit
[-1])) limit
--;
2950 while (p
< limit
&& is_space (*p
)) p
++;
2952 /* Allocate space for the text in the macro definition.
2953 Leading and trailing whitespace chars need 2 bytes each.
2954 Each other input char may or may not need 1 byte,
2955 so this is an upper bound.
2956 The extra 2 are for invented trailing newline-marker and final null. */
2957 maxsize
= (sizeof (DEFINITION
)
2958 + 2 * (end
- limit
) + 2 * (p
- buf
)
2960 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
2962 defn
->nargs
= nargs
;
2963 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
2968 /* Convert leading whitespace to Newline-markers. */
2969 while (p
< limit
&& is_space (*p
)) {
2974 /* Process the main body of the definition. */
2976 int skipped_arg
= 0;
2981 /* In -traditional mode, recognize arguments inside strings and
2982 and character constants, and ignore special properties of #.
2983 Arguments inside strings are considered "stringified", but no
2984 extra quote marks are supplied. */
2988 if (expected_delimiter
!= '\0') {
2989 if (c
== expected_delimiter
)
2990 expected_delimiter
= '\0';
2992 expected_delimiter
= c
;
2996 /* Backslash quotes delimiters and itself, but not macro args. */
2997 if (expected_delimiter
!= 0 && p
< limit
2998 && (*p
== expected_delimiter
|| *p
== '\\')) {
3005 if (expected_delimiter
!= '\0') /* No comments inside strings. */
3008 /* If we find a comment that wasn't removed by handle_directive,
3009 this must be -traditional. So replace the comment with
3013 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
3019 if (is_idchar (c
) && nargs
> 0) {
3020 U_CHAR
*id_beg
= p
- 1;
3024 while (p
!= limit
&& is_idchar (*p
)) p
++;
3025 id_len
= p
- id_beg
;
3027 if (is_idstart (c
)) {
3028 struct arglist
*arg
;
3030 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
3031 struct reflist
*tpat
;
3033 if (arg
->name
[0] == c
3034 && arg
->length
== id_len
3035 && strncmp ((const char *)arg
->name
,
3036 (const char *)id_beg
, id_len
) == 0) {
3037 /* make a pat node for this arg and append it to the end of
3039 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
3041 tpat
->raw_before
= concat
== id_beg
;
3042 tpat
->raw_after
= 0;
3043 tpat
->stringify
= expected_delimiter
!= '\0';
3046 defn
->pattern
= tpat
;
3048 endpat
->next
= tpat
;
3051 tpat
->argno
= arg
->argno
;
3052 tpat
->nchars
= exp_p
- lastp
;
3055 SKIP_WHITE_SPACE (p1
);
3056 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
3057 tpat
->raw_after
= 1;
3059 lastp
= exp_p
; /* place to start copying from next time */
3066 /* If this was not a macro arg, copy it into the expansion. */
3067 if (! skipped_arg
) {
3072 if (stringify
== id_beg
)
3073 error ("# operator should be followed by a macro argument name");
3079 /* Convert trailing whitespace to Newline-markers. */
3080 while (limit
< end
&& is_space (*limit
)) {
3082 *exp_p
++ = *limit
++;
3087 defn
->length
= exp_p
- defn
->expansion
;
3089 /* Crash now if we overrun the allocated size. */
3090 if (defn
->length
+ 1 > maxsize
)
3097 * interpret #line command. Remembers previously seen fnames
3098 * in its very own hash table.
3100 #define FNAME_HASHSIZE 37
3102 do_line (buf
, limit
, op
)
3103 U_CHAR
*buf
, *limit
;
3107 FILE_BUF
*ip
= &instack
[indepth
];
3110 enum file_change_code file_change
= same_file
;
3112 /* Expand any macros. */
3113 tem
= expand_to_temp_buffer (buf
, limit
, 0);
3115 /* Point to macroexpanded line, which is null-terminated now. */
3117 SKIP_WHITE_SPACE (bp
);
3119 if (!ISDIGIT (*bp
)) {
3120 error ("invalid format #line command");
3124 /* The Newline at the end of this line remains to be processed.
3125 To put the next line at the specified line number,
3126 we must store a line number now that is one less. */
3127 new_lineno
= atoi ((const char *)bp
);
3129 /* skip over the line number. */
3130 while (ISDIGIT (*bp
))
3133 SKIP_WHITE_SPACE (bp
);
3136 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3137 HASHNODE
*hp
, **hash_bucket
;
3143 while (*bp
&& *bp
!= '\"')
3146 error ("invalid format #line command");
3150 fname_length
= bp
- fname
;
3153 SKIP_WHITE_SPACE (bp
);
3156 file_change
= enter_file
;
3157 else if (*bp
== '2')
3158 file_change
= leave_file
;
3160 error ("invalid format #line command");
3165 SKIP_WHITE_SPACE (bp
);
3167 error ("invalid format #line command");
3173 &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3174 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3175 if (hp
->length
== fname_length
&&
3176 strncmp (hp
->value
.cpval
, (const char *)fname
, fname_length
) == 0) {
3177 ip
->fname
= hp
->value
.cpval
;
3182 /* Didn't find it; cons up a new one. */
3183 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3184 hp
->next
= *hash_bucket
;
3187 hp
->length
= fname_length
;
3188 ip
->fname
= hp
->value
.cpval
= q
= ((char *) hp
) + sizeof (HASHNODE
);
3189 memcpy (q
, fname
, fname_length
);
3192 error ("invalid format #line command");
3196 ip
->lineno
= new_lineno
;
3197 output_line_command (ip
, op
, 0, file_change
);
3198 ip
->bufp
++; /* Skip the new line. */
3199 check_expand (op
, ip
->length
- (ip
->bufp
- ip
->buf
));
3203 * remove all definitions of symbol from symbol table.
3204 * according to un*x /lib/cpp, it is not an error to undef
3205 * something that has no definitions, so it isn't one here either.
3208 do_undef (buf
, limit
, op
)
3210 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3211 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3215 SKIP_WHITE_SPACE (buf
);
3217 if (! strncmp ((const char *)buf
, "defined", 7) && ! is_idchar (buf
[7]))
3218 warning ("undefining `defined'");
3220 while ((hp
= lookup (buf
, -1, -1)) != NULL
) {
3221 if (hp
->type
!= T_MACRO
)
3222 warning ("undefining `%s'", hp
->name
);
3227 /* Read the tokens of the answer into the macro pool. Only commit the
3228 memory if we intend it as permanent storage, i.e. the #assert case.
3229 Returns 0 on success. */
3232 parse_answer (buf
, limit
, answerp
, type
)
3233 const unsigned char *buf
, *limit
;
3234 struct answer
**answerp
;
3237 const unsigned char *start
;
3239 /* Skip leading whitespace. */
3240 if (buf
< limit
&& *buf
== ' ')
3243 /* Parentheses are optional here. */
3244 if (buf
== limit
&& type
== T_UNASSERT
)
3247 if (buf
== limit
|| *buf
++ != '(')
3252 error ("missing '(' after predicate");
3256 /* Drop whitespace at start. */
3257 while (buf
< limit
&& *buf
== ' ')
3261 while (buf
< limit
&& *buf
!= ')')
3266 error ("missing ')' to complete answer");
3272 error ("predicate's answer is empty");
3276 if ((type
== T_ASSERT
|| type
== T_UNASSERT
) && buf
+ 1 != limit
)
3278 error ("extra text at end of directive");
3282 /* Lose trailing whitespace. */
3286 *answerp
= (struct answer
*) xmalloc (sizeof (struct answer
));
3287 (*answerp
)->answer
= start
;
3288 (*answerp
)->len
= buf
- start
;
3293 /* Parses an assertion, returning a pointer to the hash node of the
3294 predicate, or 0 on error. If an answer was supplied, it is placed
3295 in ANSWERP, otherwise it is set to 0. */
3297 parse_assertion (buf
, limit
, answerp
, type
)
3298 const unsigned char *buf
, *limit
;
3299 struct answer
**answerp
;
3302 HASHNODE
*result
= 0;
3303 const unsigned char *climit
;
3304 unsigned char *bp
, *symname
= canonicalize_text (buf
, limit
, &climit
);
3308 if (bp
< climit
&& is_idstart (*bp
))
3312 while (bp
< climit
&& is_idchar (*bp
));
3319 if (symname
== climit
)
3320 error ("assertion without predicate");
3322 error ("predicate must be an identifier");
3324 /* Unfortunately, because of the way we handle #if, we don't avoid
3325 macro expansion in answers. This is not easy to fix. */
3326 else if (parse_answer (bp
, climit
, answerp
, type
) == 0)
3328 unsigned char *sym
= alloca (len
+ 1);
3331 /* Prefix '#' to get it out of macro namespace. */
3333 memcpy (sym
+ 1, symname
, len
);
3335 hashcode
= hashf (sym
, len
+ 1, HASHSIZE
);
3336 result
= lookup (sym
, len
+ 1, hashcode
);
3338 result
= install (sym
, len
+ 1, T_UNUSED
, hashcode
);
3344 /* Test an assertion within a preprocessor conditional. Returns zero
3345 on error or failure, one on success. */
3347 test_assertion (pbuf
)
3348 unsigned char **pbuf
; /* NUL-terminated. */
3350 unsigned char *buf
= *pbuf
;
3351 unsigned char *limit
= buf
+ strlen ((char *) buf
);
3352 struct answer
*answer
;
3356 node
= parse_assertion (buf
, limit
, &answer
, T_IF
);
3359 result
= (node
->type
== T_ASSERT
&&
3360 (answer
== 0 || *find_answer (node
, answer
) != 0));
3362 /* Yuk. We update pbuf to point after the assertion test.
3363 First, move past the identifier. */
3364 if (is_space (*buf
))
3366 while (is_idchar (*buf
))
3368 /* If we have an answer, we need to move past the parentheses. */
3370 while (*buf
++ != ')')
3378 /* Handle a #error directive. */
3380 do_error (buf
, limit
, op
)
3383 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3385 error ("#error%.*s", (int) (limit
- buf
), buf
);
3388 /* Handle a #warning directive. */
3390 do_warning (buf
, limit
, op
)
3393 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3395 warning ("#warning%.*s", (int) (limit
- buf
), buf
);
3398 /* Handle a #assert directive. */
3400 do_assert (buf
, limit
, op
)
3403 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3405 struct answer
*new_answer
;
3408 node
= parse_assertion (buf
, limit
, &new_answer
, T_ASSERT
);
3411 /* Place the new answer in the answer list. First check there
3412 is not a duplicate. */
3413 new_answer
->next
= 0;
3414 if (node
->type
== T_ASSERT
)
3416 if (*find_answer (node
, new_answer
))
3419 warning ("\"%s\" re-asserted", node
->name
+ 1);
3422 new_answer
->next
= node
->value
.answers
;
3424 node
->type
= T_ASSERT
;
3425 node
->value
.answers
= new_answer
;
3429 /* Function body to be provided later. */
3431 do_unassert (buf
, limit
, op
)
3434 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3437 struct answer
*answer
;
3439 node
= parse_assertion (buf
, limit
, &answer
, T_UNASSERT
);
3440 /* It isn't an error to #unassert something that isn't asserted. */
3443 if (node
->type
== T_ASSERT
)
3447 struct answer
**p
= find_answer (node
, answer
), *temp
;
3449 /* Remove the answer from the list. */
3454 /* Did we free the last answer? */
3455 if (node
->value
.answers
== 0)
3456 delete_macro (node
);
3459 delete_macro (node
);
3466 /* Returns a pointer to the pointer to the answer in the answer chain,
3467 or a pointer to NULL if the answer is not in the chain. */
3468 static struct answer
**
3469 find_answer (node
, candidate
)
3471 const struct answer
*candidate
;
3473 struct answer
**result
;
3475 for (result
= &node
->value
.answers
; *result
; result
= &(*result
)->next
)
3477 struct answer
*answer
= *result
;
3479 if (answer
->len
== candidate
->len
3480 && !memcmp (answer
->answer
, candidate
->answer
, answer
->len
))
3487 /* Return a malloced buffer with leading and trailing whitespace
3488 removed, and all instances of internal whitespace reduced to a
3490 static unsigned char *
3491 canonicalize_text (buf
, limit
, climit
)
3492 const unsigned char *buf
, *limit
, **climit
;
3494 unsigned int len
= limit
- buf
;
3495 unsigned char *result
= (unsigned char *) xmalloc (len
), *dest
;
3497 for (dest
= result
; buf
< limit
;)
3499 if (! is_space (*buf
))
3503 while (++buf
< limit
&& is_space (*buf
))
3505 if (dest
!= result
&& buf
!= limit
)
3515 * handle #if command by
3516 * 1) inserting special `defined' keyword into the hash table
3517 * that gets turned into 0 or 1 by special_symbol (thus,
3518 * if the luser has a symbol called `defined' already, it won't
3519 * work inside the #if command)
3520 * 2) rescan the input into a temporary output buffer
3521 * 3) pass the output buffer to the yacc parser and collect a value
3522 * 4) clean up the mess left from steps 1 and 2.
3523 * 5) call conditional_skip to skip til the next #endif (etc.),
3524 * or not, depending on the value from step 3.
3527 do_if (buf
, limit
, op
)
3528 U_CHAR
*buf
, *limit
;
3529 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3532 FILE_BUF
*ip
= &instack
[indepth
];
3534 value
= eval_if_expression (buf
, limit
- buf
);
3535 conditional_skip (ip
, value
== 0, T_IF
);
3539 * handle a #elif directive by not changing if_stack either.
3540 * see the comment above do_else.
3543 do_elif (buf
, limit
, op
)
3544 U_CHAR
*buf
, *limit
;
3548 FILE_BUF
*ip
= &instack
[indepth
];
3550 if (if_stack
== instack
[indepth
].if_stack
) {
3551 error ("#elif not within a conditional");
3554 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3555 error ("#elif after #else");
3556 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3557 if (if_stack
->fname
!= NULL
&& ip
->fname
!= NULL
&&
3558 strcmp (if_stack
->fname
, ip
->fname
) != 0)
3559 fprintf (stderr
, ", file %s", if_stack
->fname
);
3560 fprintf (stderr
, ")\n");
3562 if_stack
->type
= T_ELIF
;
3565 if (if_stack
->if_succeeded
)
3566 skip_if_group (ip
, 0);
3568 value
= eval_if_expression (buf
, limit
- buf
);
3570 skip_if_group (ip
, 0);
3572 ++if_stack
->if_succeeded
; /* continue processing input */
3573 output_line_command (ip
, op
, 1, same_file
);
3579 * evaluate a #if expression in BUF, of length LENGTH,
3580 * then parse the result as a C expression and return the value as an int.
3583 eval_if_expression (buf
, length
)
3588 HASHNODE
*save_defined
;
3591 save_defined
= install (U
"defined", -1, T_SPEC_DEFINED
, -1);
3592 temp_obuf
= expand_to_temp_buffer (buf
, buf
+ length
, 0);
3593 delete_macro (save_defined
); /* clean up special symbol */
3595 value
= parse_c_expression ((const char *)temp_obuf
.buf
);
3597 free (temp_obuf
.buf
);
3603 * routine to handle ifdef/ifndef. Try to look up the symbol,
3604 * then do or don't skip to the #endif/#else/#elif depending
3605 * on what directive is actually being processed.
3608 do_xifdef (buf
, limit
, type
)
3609 U_CHAR
*buf
, *limit
;
3610 enum node_type type
;
3613 FILE_BUF
*ip
= &instack
[indepth
];
3616 /* Discard leading and trailing whitespace. */
3617 SKIP_WHITE_SPACE (buf
);
3618 while (limit
!= buf
&& is_nvspace (limit
[-1])) limit
--;
3620 /* Find the end of the identifier at the beginning. */
3621 for (end
= buf
; is_idchar (*end
); end
++);
3624 skip
= (type
== T_IFDEF
);
3626 skip
= (lookup (buf
, end
-buf
, -1) == NULL
) ^ (type
== T_IFNDEF
);
3628 conditional_skip (ip
, skip
, T_IF
);
3632 do_ifdef (buf
, limit
, op
)
3633 U_CHAR
*buf
, *limit
;
3634 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3636 do_xifdef (buf
, limit
, T_IFDEF
);
3640 do_ifndef (buf
, limit
, op
)
3641 U_CHAR
*buf
, *limit
;
3642 FILE_BUF
*op ATTRIBUTE_UNUSED
;
3644 do_xifdef (buf
, limit
, T_IFNDEF
);
3648 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3651 conditional_skip (ip
, skip
, type
)
3654 enum node_type type
;
3656 IF_STACK_FRAME
*temp
;
3658 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3659 temp
->fname
= ip
->fname
;
3660 temp
->lineno
= ip
->lineno
;
3661 temp
->next
= if_stack
;
3664 if_stack
->type
= type
;
3667 skip_if_group (ip
, 0);
3670 ++if_stack
->if_succeeded
;
3671 output_line_command (ip
, &outbuf
, 1, same_file
);
3676 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3677 * leaves input ptr at the sharp sign found.
3678 * If ANY is nonzero, return at next directive of any sort.
3681 skip_if_group (ip
, any
)
3685 U_CHAR
*bp
= ip
->bufp
, *cp
;
3686 U_CHAR
*endb
= ip
->buf
+ ip
->length
;
3687 const struct directive
*kt
;
3688 IF_STACK_FRAME
*save_if_stack
= if_stack
; /* don't pop past here */
3689 U_CHAR
*beg_of_line
= bp
;
3693 case '/': /* possible comment */
3694 if (*bp
== '\\' && bp
[1] == '\n')
3698 bp
= skip_to_end_of_comment (ip
, &ip
->lineno
);
3703 bp
= skip_quoted_string (bp
- 1, endb
, ip
->lineno
, &ip
->lineno
, 0, 0);
3706 /* Char after backslash loses its special meaning. */
3709 ++ip
->lineno
; /* But do update the line-count. */
3720 /* # keyword: a # must be first nonblank char on the line */
3721 if (beg_of_line
== 0)
3723 /* Scan from start of line, skipping whitespace, comments
3724 and backslash-newlines, and see if we reach this #.
3725 If not, this # is not special. */
3728 if (is_nvspace (*bp
))
3730 else if (*bp
== '\\' && bp
[1] == '\n')
3732 else if (*bp
== '/' && bp
[1] == '*') {
3734 while (!(*bp
== '*' && bp
[1] == '/')) {
3743 if (bp
!= ip
->bufp
) {
3744 bp
= ip
->bufp
+ 1; /* Reset bp to after the #. */
3748 bp
= ip
->bufp
+ 1; /* Point after '#'. */
3750 /* Skip whitespace and \-newline. */
3752 if (is_nvspace (*bp
))
3754 else if (*bp
== '\\' && bp
[1] == '\n')
3756 else if (*bp
== '/' && bp
[1] == '*') {
3758 while (!(*bp
== '*' && bp
[1] == '/'))
3767 /* Now find end of directive name.
3768 If we encounter a backslash-newline, exchange it with any following
3769 symbol-constituents so that we end up with a contiguous name. */
3772 if (is_idchar (*bp
))
3775 if (*bp
== '\\' && bp
[1] == '\n')
3776 name_newline_fix (bp
);
3777 if (is_idchar (*bp
))
3783 for (kt
= directive_table
; kt
->length
>= 0; kt
++) {
3784 IF_STACK_FRAME
*temp
;
3785 if (strncmp ((const char *)cp
, kt
->name
, kt
->length
) == 0
3786 && !is_idchar (cp
[kt
->length
])) {
3788 /* If we are asked to return on next directive,
3797 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3798 temp
->next
= if_stack
;
3800 temp
->lineno
= ip
->lineno
;
3801 temp
->fname
= ip
->fname
;
3802 temp
->type
= kt
->type
;
3807 if (if_stack
== instack
[indepth
].if_stack
) {
3808 error ("#%s not within a conditional", kt
->name
);
3811 else if (if_stack
== save_if_stack
)
3812 return; /* found what we came for */
3814 if (kt
->type
!= T_ENDIF
) {
3815 if (if_stack
->type
== T_ELSE
)
3816 error ("#else or #elif after #else");
3817 if_stack
->type
= kt
->type
;
3822 if_stack
= if_stack
->next
;
3827 /* Anything else is ignored. */
3836 /* after this returns, rescan will exit because ip->bufp
3837 now points to the end of the buffer.
3838 rescan is responsible for the error message also. */
3842 * handle a #else directive. Do this by just continuing processing
3843 * without changing if_stack ; this is so that the error message
3844 * for missing #endif's etc. will point to the original #if. It
3845 * is possible that something different would be better.
3848 do_else (buf
, limit
, op
)
3849 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3850 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3853 FILE_BUF
*ip
= &instack
[indepth
];
3855 if (if_stack
== instack
[indepth
].if_stack
) {
3856 error ("#else not within a conditional");
3859 if (if_stack
->type
!= T_IF
&& if_stack
->type
!= T_ELIF
) {
3860 error ("#else after #else");
3861 fprintf (stderr
, " (matches line %d", if_stack
->lineno
);
3862 if (strcmp (if_stack
->fname
, ip
->fname
) != 0)
3863 fprintf (stderr
, ", file %s", if_stack
->fname
);
3864 fprintf (stderr
, ")\n");
3866 if_stack
->type
= T_ELSE
;
3869 if (if_stack
->if_succeeded
)
3870 skip_if_group (ip
, 0);
3872 ++if_stack
->if_succeeded
; /* continue processing input */
3873 output_line_command (ip
, op
, 1, same_file
);
3878 * unstack after #endif command
3881 do_endif (buf
, limit
, op
)
3882 U_CHAR
*buf ATTRIBUTE_UNUSED
;
3883 U_CHAR
*limit ATTRIBUTE_UNUSED
;
3886 if (if_stack
== instack
[indepth
].if_stack
)
3887 error ("unbalanced #endif");
3889 IF_STACK_FRAME
*temp
= if_stack
;
3890 if_stack
= if_stack
->next
;
3892 output_line_command (&instack
[indepth
], op
, 1, same_file
);
3897 * Skip a comment, assuming the input ptr immediately follows the
3898 * initial slash-star. Bump line counter as necessary.
3899 * (The canonical line counter is &ip->lineno).
3900 * Don't use this routine (or the next one) if bumping the line
3901 * counter is not sufficient to deal with newlines in the string.
3904 skip_to_end_of_comment (ip
, line_counter
)
3906 int *line_counter
; /* place to remember newlines, or NULL */
3908 U_CHAR
*limit
= ip
->buf
+ ip
->length
;
3909 U_CHAR
*bp
= ip
->bufp
;
3910 FILE_BUF
*op
= &outbuf
; /* JF */
3911 int output
= put_out_comments
&& !line_counter
;
3913 /* JF this line_counter stuff is a crock to make sure the
3914 comment is only put out once, no matter how many times
3915 the comment is skipped. It almost works */
3920 while (bp
< limit
) {
3925 if (warn_comments
&& bp
< limit
&& *bp
== '*')
3926 warning("`/*' within comment");
3929 if (line_counter
!= NULL
)
3935 if (*bp
== '\\' && bp
[1] == '\n')
3951 * Skip over a quoted string. BP points to the opening quote.
3952 * Returns a pointer after the closing quote. Don't go past LIMIT.
3953 * START_LINE is the line number of the starting point (but it need
3954 * not be valid if the starting point is inside a macro expansion).
3956 * The input stack state is not changed.
3958 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3959 * for each newline passed.
3961 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3962 * if we pass a backslash-newline.
3964 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3967 skip_quoted_string (bp
, limit
, start_line
, count_newlines
, backslash_newlines_p
, eofp
)
3969 const U_CHAR
*limit
;
3971 int *count_newlines
;
3972 int *backslash_newlines_p
;
3980 error_with_line (line_for_error (start_line
),
3981 "unterminated string or character constant");
3988 while (*bp
== '\\' && bp
[1] == '\n') {
3989 if (backslash_newlines_p
)
3990 *backslash_newlines_p
= 1;
3995 if (*bp
== '\n' && count_newlines
) {
3996 if (backslash_newlines_p
)
3997 *backslash_newlines_p
= 1;
4001 } else if (c
== '\n') {
4002 /* Unterminated strings and character constants are 'legal'. */
4003 bp
--; /* Don't consume the newline. */
4007 } else if (c
== match
)
4010 return (U_CHAR
*) bp
;
4014 * write out a #line command, for instance, after an #include file.
4015 * If CONDITIONAL is nonzero, we can omit the #line if it would
4016 * appear to be a no-op, and we can output a few newlines instead
4017 * if we want to increase the line number by a small amount.
4018 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
4022 output_line_command (ip
, op
, conditional
, file_change
)
4025 enum file_change_code file_change
;
4028 char line_cmd_buf
[500];
4030 if (no_line_commands
4031 || ip
->fname
== NULL
4033 op
->lineno
= ip
->lineno
;
4038 if (ip
->lineno
== op
->lineno
)
4041 /* If the inherited line number is a little too small,
4042 output some newlines instead of a #line command. */
4043 if (ip
->lineno
> op
->lineno
&& ip
->lineno
< op
->lineno
+ 8) {
4044 check_expand (op
, 10);
4045 while (ip
->lineno
> op
->lineno
) {
4053 sprintf (line_cmd_buf
, "# %d \"%s\"", ip
->lineno
, ip
->fname
);
4054 if (file_change
!= same_file
)
4055 strcat (line_cmd_buf
, file_change
== enter_file
? " 1" : " 2");
4056 if (system_include_depth
> 0)
4057 strcat (line_cmd_buf
, " 3");
4058 len
= strlen (line_cmd_buf
);
4059 line_cmd_buf
[len
++] = '\n';
4060 check_expand (op
, len
+ 1);
4061 if (op
->bufp
> op
->buf
&& op
->bufp
[-1] != '\n')
4063 memcpy (op
->bufp
, line_cmd_buf
, len
);
4065 op
->lineno
= ip
->lineno
;
4069 /* Expand a macro call.
4070 HP points to the symbol that is the macro being called.
4071 Put the result of expansion onto the input stack
4072 so that subsequent input by our caller will use it.
4074 If macro wants arguments, caller has already verified that
4075 an argument list follows; arguments come from the input stack. */
4078 macroexpand (hp
, op
)
4083 DEFINITION
*defn
= hp
->value
.defn
;
4086 int start_line
= instack
[indepth
].lineno
;
4088 CHECK_DEPTH (return;);
4090 /* it might not actually be a macro. */
4091 if (hp
->type
!= T_MACRO
) {
4092 special_symbol (hp
, op
);
4096 nargs
= defn
->nargs
;
4100 struct argdata
*args
;
4101 const char *parse_error
= 0;
4103 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
4105 for (i
= 0; i
< nargs
; i
++) {
4106 args
[i
].raw
= args
[i
].expanded
= (U_CHAR
*) "";
4107 args
[i
].raw_length
= args
[i
].expand_length
4108 = args
[i
].stringified_length
= 0;
4109 args
[i
].free1
= args
[i
].free2
= 0;
4112 /* Parse all the macro args that are supplied. I counts them.
4113 The first NARGS args are stored in ARGS.
4114 The rest are discarded. */
4117 /* Discard the open-parenthesis or comma before the next arg. */
4118 ++instack
[indepth
].bufp
;
4120 = macarg ((i
< nargs
|| (nargs
== 0 && i
== 0)) ? &args
[i
] : 0);
4123 error_with_line (line_for_error (start_line
), "%s", parse_error
);
4127 } while (*instack
[indepth
].bufp
!= ')');
4129 /* If we got one arg but it was just whitespace, call that 0 args. */
4131 const U_CHAR
*bp
= args
[0].raw
;
4132 const U_CHAR
*lim
= bp
+ args
[0].raw_length
;
4133 while (bp
!= lim
&& is_space (*bp
)) bp
++;
4138 if (nargs
== 0 && i
> 0)
4139 error ("arguments given to macro `%s'", hp
->name
);
4140 else if (i
< nargs
) {
4141 /* traditional C allows foo() if foo wants one argument. */
4142 if (nargs
== 1 && i
== 0)
4145 error ("no args to macro `%s'", hp
->name
);
4147 error ("only 1 arg to macro `%s'", hp
->name
);
4149 error ("only %d args to macro `%s'", i
, hp
->name
);
4150 } else if (i
> nargs
)
4151 error ("too many (%d) args to macro `%s'", i
, hp
->name
);
4153 /* Swallow the closeparen. */
4154 ++instack
[indepth
].bufp
;
4156 /* If macro wants zero args, we parsed the arglist for checking only.
4157 Read directly from the macro definition. */
4159 xbuf
= defn
->expansion
;
4160 xbuf_len
= defn
->length
;
4162 U_CHAR
*exp
= defn
->expansion
;
4163 int offset
; /* offset in expansion,
4164 copied a piece at a time */
4165 int totlen
; /* total amount of exp buffer filled so far */
4169 /* Macro really takes args. Compute the expansion of this call. */
4171 /* Compute length in characters of the macro's expansion. */
4172 xbuf_len
= defn
->length
;
4173 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4175 xbuf_len
+= args
[ap
->argno
].stringified_length
;
4177 xbuf_len
+= args
[ap
->argno
].raw_length
;
4180 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
4182 /* Generate in XBUF the complete expansion
4183 with arguments substituted in.
4184 TOTLEN is the total size generated so far.
4185 OFFSET is the index in the definition
4186 of where we are copying from. */
4187 offset
= totlen
= 0;
4188 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
4189 struct argdata
*arg
= &args
[ap
->argno
];
4191 for (i
= 0; i
< ap
->nchars
; i
++)
4192 xbuf
[totlen
++] = exp
[offset
++];
4194 if (ap
->stringify
!= 0) {
4195 int arglen
= arg
->raw_length
;
4201 && (c
= arg
->raw
[i
], is_space (c
)))
4204 && (c
= arg
->raw
[arglen
- 1], is_space (c
)))
4206 for (; i
< arglen
; i
++) {
4209 /* Special markers Newline Space
4210 generate nothing for a stringified argument. */
4211 if (c
== '\n' && arg
->raw
[i
+1] != '\n') {
4216 /* Internal sequences of whitespace are replaced by one space
4217 except within an string or char token. */
4219 && (c
== '\n' ? arg
->raw
[i
+1] == '\n' : is_space (c
))) {
4221 /* Note that Newline Space does occur within whitespace
4222 sequences; consider it part of the sequence. */
4223 if (c
== '\n' && is_space (arg
->raw
[i
+1]))
4225 else if (c
!= '\n' && is_space (c
))
4242 } else if (c
== '\"' || c
== '\'')
4246 /* Escape these chars */
4247 if (c
== '\"' || (in_string
&& c
== '\\'))
4248 xbuf
[totlen
++] = '\\';
4252 sprintf ((char *) &xbuf
[totlen
], "\\%03o", (unsigned int) c
);
4257 const U_CHAR
*p1
= arg
->raw
;
4258 const U_CHAR
*l1
= p1
+ arg
->raw_length
;
4260 if (ap
->raw_before
) {
4261 while (p1
!= l1
&& is_space (*p1
)) p1
++;
4262 while (p1
!= l1
&& is_idchar (*p1
))
4263 xbuf
[totlen
++] = *p1
++;
4264 /* Delete any no-reexpansion marker that follows
4265 an identifier at the beginning of the argument
4266 if the argument is concatenated with what precedes it. */
4267 if (p1
[0] == '\n' && p1
[1] == '-')
4270 if (ap
->raw_after
) {
4271 /* Arg is concatenated after: delete trailing whitespace,
4272 whitespace markers, and no-reexpansion markers. */
4274 if (is_space (l1
[-1])) l1
--;
4275 else if (l1
[-1] == '-') {
4276 const U_CHAR
*p2
= l1
- 1;
4277 /* If a `-' is preceded by an odd number of newlines then it
4278 and the last newline are a no-reexpansion marker. */
4279 while (p2
!= p1
&& p2
[-1] == '\n') p2
--;
4280 if ((l1
- 1 - p2
) & 1) {
4288 memmove (xbuf
+ totlen
, p1
, l1
- p1
);
4292 if (totlen
> xbuf_len
)
4296 /* if there is anything left of the definition
4297 after handling the arg list, copy that in too. */
4299 for (i
= offset
; i
< defn
->length
; i
++)
4300 xbuf
[totlen
++] = exp
[i
];
4305 for (i
= 0; i
< nargs
; i
++) {
4306 if (args
[i
].free1
!= 0)
4307 free (args
[i
].free1
);
4308 if (args
[i
].free2
!= 0)
4309 free (args
[i
].free2
);
4313 xbuf
= defn
->expansion
;
4314 xbuf_len
= defn
->length
;
4317 /* Now put the expansion on the input stack
4318 so our caller will commence reading from it. */
4322 ip2
= &instack
[++indepth
];
4327 ip2
->length
= xbuf_len
;
4329 ip2
->free_ptr
= (nargs
> 0) ? xbuf
: 0;
4331 ip2
->if_stack
= if_stack
;
4336 * Parse a macro argument and store the info on it into *ARGPTR.
4337 * Return nonzero to indicate a syntax error.
4342 struct argdata
*argptr
;
4344 FILE_BUF
*ip
= &instack
[indepth
];
4349 /* Try to parse as much of the argument as exists at this
4350 input stack level. */
4351 U_CHAR
*bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
,
4352 &paren
, &newlines
, &comments
);
4354 /* If we find the end of the argument at this level,
4355 set up *ARGPTR to point at it in the input stack. */
4356 if (!(ip
->fname
!= 0 && (newlines
!= 0 || comments
!= 0))
4357 && bp
!= ip
->buf
+ ip
->length
) {
4359 argptr
->raw
= ip
->bufp
;
4360 argptr
->raw_length
= bp
- ip
->bufp
;
4364 /* This input stack level ends before the macro argument does.
4365 We must pop levels and keep parsing.
4366 Therefore, we must allocate a temporary buffer and copy
4367 the macro argument into it. */
4368 int bufsize
= bp
- ip
->bufp
;
4369 int extra
= newlines
;
4370 U_CHAR
*buffer
= (U_CHAR
*) xmalloc (bufsize
+ extra
+ 1);
4371 int final_start
= 0;
4373 memcpy (buffer
, ip
->bufp
, bufsize
);
4375 ip
->lineno
+= newlines
;
4377 while (bp
== ip
->buf
+ ip
->length
) {
4378 if (instack
[indepth
].macro
== 0) {
4380 return "unterminated macro call";
4382 ip
->macro
->type
= T_MACRO
;
4384 free (ip
->free_ptr
);
4385 ip
= &instack
[--indepth
];
4388 bp
= macarg1 (ip
->bufp
, ip
->buf
+ ip
->length
, &paren
,
4389 &newlines
, &comments
);
4390 final_start
= bufsize
;
4391 bufsize
+= bp
- ip
->bufp
;
4393 buffer
= (U_CHAR
*) xrealloc (buffer
, bufsize
+ extra
+ 1);
4394 memcpy (buffer
+ bufsize
- (bp
- ip
->bufp
), ip
->bufp
, bp
- ip
->bufp
);
4396 ip
->lineno
+= newlines
;
4399 /* Now, if arg is actually wanted, record its raw form,
4400 discarding comments and duplicating newlines in whatever
4401 part of it did not come from a macro expansion.
4402 EXTRA space has been preallocated for duplicating the newlines.
4403 FINAL_START is the index of the start of that part. */
4405 argptr
->raw
= buffer
;
4406 argptr
->raw_length
= bufsize
;
4407 argptr
->free1
= buffer
;
4408 argptr
->newlines
= newlines
;
4409 argptr
->comments
= comments
;
4410 if ((newlines
|| comments
) && ip
->fname
!= 0)
4413 discard_comments (argptr
->raw
+ final_start
,
4414 argptr
->raw_length
- final_start
,
4416 argptr
->raw
[argptr
->raw_length
] = 0;
4417 if (argptr
->raw_length
> bufsize
+ extra
)
4422 /* If we are not discarding this argument,
4423 macroexpand it and compute its length as stringified.
4424 All this info goes into *ARGPTR. */
4428 const U_CHAR
*buf
, *lim
;
4431 obuf
= expand_to_temp_buffer (argptr
->raw
,
4432 argptr
->raw
+ argptr
->raw_length
,
4435 argptr
->expanded
= obuf
.buf
;
4436 argptr
->expand_length
= obuf
.length
;
4437 argptr
->free2
= obuf
.buf
;
4440 lim
= buf
+ argptr
->raw_length
;
4443 while (buf
!= lim
) {
4446 /* Internal sequences of whitespace are replaced by one space
4447 in most cases, but not always. So count all the whitespace
4448 in case we need to keep it all. */
4449 if (c
== '\"' || c
== '\\') /* escape these chars */
4451 else if (!ISPRINT (c
))
4454 argptr
->stringified_length
= totlen
;
4459 /* Scan text from START (inclusive) up to LIMIT (exclusive),
4460 counting parens in *DEPTHPTR,
4461 and return if reach LIMIT
4462 or before a `)' that would make *DEPTHPTR negative
4463 or before a comma when *DEPTHPTR is zero.
4464 Single and double quotes are matched and termination
4465 is inhibited within them. Comments also inhibit it.
4466 Value returned is pointer to stopping place.
4468 Increment *NEWLINES each time a newline is passed.
4469 Set *COMMENTS to 1 if a comment is seen. */
4472 macarg1 (start
, limit
, depthptr
, newlines
, comments
)
4474 const U_CHAR
*limit
;
4475 int *depthptr
, *newlines
, *comments
;
4479 while (bp
< limit
) {
4485 if (--(*depthptr
) < 0)
4489 /* Traditionally, backslash makes following char not special. */
4493 /* But count source lines anyway. */
4502 if (bp
[1] == '\\' && bp
[2] == '\n')
4503 newline_fix (bp
+ 1);
4504 if (bp
[1] != '*' || bp
+ 1 >= limit
)
4508 while (bp
+ 1 < limit
) {
4510 && bp
[1] == '\\' && bp
[2] == '\n')
4511 newline_fix (bp
+ 1);
4512 if (bp
[0] == '*' && bp
[1] == '/')
4514 if (*bp
== '\n') ++*newlines
;
4523 for (quotec
= *bp
++; bp
+ 1 < limit
&& *bp
!= quotec
; bp
++) {
4528 while (*bp
== '\\' && bp
[1] == '\n') {
4531 } else if (*bp
== '\n') {
4540 if ((*depthptr
) == 0)
4550 /* Discard comments and duplicate newlines
4551 in the string of length LENGTH at START,
4552 except inside of string constants.
4553 The string is copied into itself with its beginning staying fixed.
4555 NEWLINES is the number of newlines that must be duplicated.
4556 We assume that that much extra space is available past the end
4560 discard_comments (start
, length
, newlines
)
4567 const U_CHAR
*limit
;
4570 /* If we have newlines to duplicate, copy everything
4571 that many characters up. Then, in the second part,
4572 we will have room to insert the newlines
4574 NEWLINES may actually be too large, because it counts
4575 newlines in string constants, and we don't duplicate those.
4576 But that does no harm. */
4578 ibp
= start
+ length
;
4579 obp
= ibp
+ newlines
;
4581 while (limit
!= ibp
)
4585 ibp
= start
+ newlines
;
4586 limit
= start
+ length
+ newlines
;
4589 while (ibp
< limit
) {
4590 *obp
++ = c
= *ibp
++;
4593 /* Duplicate the newline. */
4605 if (*ibp
== '\\' && ibp
[1] == '\n')
4607 /* Delete any comment. */
4608 if (ibp
[0] != '*' || ibp
+ 1 >= limit
)
4612 while (ibp
+ 1 < limit
) {
4614 && ibp
[1] == '\\' && ibp
[2] == '\n')
4615 newline_fix (ibp
+ 1);
4616 if (ibp
[0] == '*' && ibp
[1] == '/')
4625 /* Notice and skip strings, so that we don't
4626 think that comments start inside them,
4627 and so we don't duplicate newlines in them. */
4630 while (ibp
< limit
) {
4631 *obp
++ = c
= *ibp
++;
4634 if (c
== '\n' && quotec
== '\'')
4636 if (c
== '\\' && ibp
< limit
) {
4637 while (*ibp
== '\\' && ibp
[1] == '\n')
4651 /* Core error handling routine. */
4653 v_message (mtype
, line
, msgid
, ap
)
4659 const char *fname
= 0;
4662 if (mtype
== MT_WARNING
&& inhibit_warnings
)
4665 for (i
= indepth
; i
>= 0; i
--)
4666 if (instack
[i
].fname
!= NULL
) {
4668 line
= instack
[i
].lineno
;
4669 fname
= instack
[i
].fname
;
4674 fprintf (stderr
, "%s:%d: ", fname
, line
);
4676 fprintf (stderr
, "%s: ", progname
);
4678 if (mtype
== MT_WARNING
)
4679 fputs (_("warning: "), stderr
);
4681 vfprintf (stderr
, _(msgid
), ap
);
4682 putc ('\n', stderr
);
4684 if (mtype
== MT_ERROR
)
4689 * error - print error message and increment count of errors.
4692 error
VPARAMS ((const char *msgid
, ...))
4695 VA_FIXEDARG (ap
, const char *, msgid
);
4697 v_message (MT_ERROR
, 0, msgid
, ap
);
4702 error_with_line
VPARAMS ((int line
, const char *msgid
, ...))
4705 VA_FIXEDARG (ap
, int, line
);
4706 VA_FIXEDARG (ap
, const char *, msgid
);
4708 v_message (MT_ERROR
, line
, msgid
, ap
);
4712 /* Error including a message from `errno'. */
4714 error_from_errno (name
)
4717 error ("%s: %s", name
, strerror (errno
));
4720 /* Print error message but don't count it. */
4722 warning
VPARAMS ((const char *msgid
, ...))
4725 VA_FIXEDARG (ap
, const char *, msgid
);
4727 v_message (MT_WARNING
, 0, msgid
, ap
);
4732 fatal
VPARAMS ((const char *msgid
, ...))
4735 VA_FIXEDARG (ap
, const char *, msgid
);
4737 v_message (MT_FATAL
, 0, msgid
, ap
);
4739 exit (FATAL_EXIT_CODE
);
4742 /* More 'friendly' abort that prints the location at which we died. */
4744 fancy_abort (line
, func
)
4748 fatal ("internal error in %s, at tradcpp.c:%d\n\
4749 Please submit a full bug report.\n\
4750 See %s for instructions.", func
, line
, GCCBUGURL
);
4754 perror_with_name (name
)
4757 fprintf (stderr
, "%s: %s: %s\n", progname
, name
, strerror (errno
));
4762 pfatal_with_name (name
)
4765 perror_with_name (name
);
4766 exit (FATAL_EXIT_CODE
);
4769 /* Return the line at which an error occurred.
4770 The error is not necessarily associated with the current spot
4771 in the input stack, so LINE says where. LINE will have been
4772 copied from ip->lineno for the current input level.
4773 If the current level is for a file, we return LINE.
4774 But if the current level is not for a file, LINE is meaningless.
4775 In that case, we return the lineno of the innermost file. */
4777 line_for_error (line
)
4783 for (i
= indepth
; i
>= 0; ) {
4784 if (instack
[i
].fname
!= 0)
4789 line1
= instack
[i
].lineno
;
4795 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4797 * As things stand, nothing is ever placed in the output buffer to be
4798 * removed again except when it's KNOWN to be part of an identifier,
4799 * so flushing and moving down everything left, instead of expanding,
4804 grow_outbuf (obuf
, needed
)
4811 if (obuf
->length
- (obuf
->bufp
- obuf
->buf
) > needed
)
4814 /* Make it at least twice as big as it is now. */
4816 /* Make it have at least 150% of the free space we will need. */
4817 minsize
= (3 * needed
) / 2 + (obuf
->bufp
- obuf
->buf
);
4818 if (minsize
> obuf
->length
)
4819 obuf
->length
= minsize
;
4821 p
= (U_CHAR
*) xrealloc (obuf
->buf
, obuf
->length
);
4822 obuf
->bufp
= p
+ (obuf
->bufp
- obuf
->buf
);
4826 /* Symbol table for macro names and special symbols */
4829 * install a name in the main hash table, even if it is already there.
4830 * name stops with first non alphanumeric, except leading '#'.
4831 * caller must check against redefinition if that is desired.
4832 * delete_macro () removes things installed by install () in fifo order.
4833 * this is important because of the `defined' special symbol used
4834 * in #if, and also if pushdef/popdef directives are ever implemented.
4836 * If LEN is >= 0, it is the length of the name.
4837 * Otherwise, compute the length by scanning the entire name.
4839 * If HASH is >= 0, it is the precomputed hash code.
4840 * Otherwise, compute the hash code.
4842 * caller must set the value, if any is desired.
4845 install (name
, len
, type
, hash
)
4848 enum node_type type
;
4850 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4859 while (is_idchar (*p
))
4865 hash
= hashf (name
, len
, HASHSIZE
);
4867 hp
= (HASHNODE
*) xmalloc (sizeof (HASHNODE
) + len
+ 1);
4869 hp
->bucket_hdr
= &hashtab
[bucket
];
4870 hp
->next
= hashtab
[bucket
];
4871 hashtab
[bucket
] = hp
;
4873 if (hp
->next
!= NULL
)
4874 hp
->next
->prev
= hp
;
4877 hp
->name
= q
= ((U_CHAR
*) hp
) + sizeof (HASHNODE
);
4878 memcpy (q
, name
, len
);
4884 * find the most recent hash node for name name (ending with first
4885 * non-identifier char) installed by install
4887 * If LEN is >= 0, it is the length of the name.
4888 * Otherwise, compute the length by scanning the entire name.
4890 * If HASH is >= 0, it is the precomputed hash code.
4891 * Otherwise, compute the hash code.
4894 lookup (name
, len
, hash
)
4903 for (bp
= name
; is_idchar (*bp
); bp
++) ;
4908 hash
= hashf (name
, len
, HASHSIZE
);
4910 bucket
= hashtab
[hash
];
4912 if (bucket
->length
== len
4913 && strncmp ((const char *)bucket
->name
, (const char *)name
, len
) == 0)
4915 bucket
= bucket
->next
;
4921 * Delete a hash node. Some weirdness to free junk from macros.
4922 * More such weirdness will have to be added if you define more hash
4923 * types that need it.
4926 /* Note that the DEFINITION of a macro is removed from the hash table
4927 but its storage is not freed. This would be a storage leak
4928 except that it is not reasonable to keep undefining and redefining
4929 large numbers of macros many times.
4930 In any case, this is necessary, because a macro can be #undef'd
4931 in the middle of reading the arguments to a call to it.
4932 If #undef freed the DEFINITION, that would crash. */
4938 if (hp
->prev
!= NULL
)
4939 hp
->prev
->next
= hp
->next
;
4940 if (hp
->next
!= NULL
)
4941 hp
->next
->prev
= hp
->prev
;
4943 /* make sure that the bucket chain header that
4944 the deleted guy was on points to the right thing afterwards. */
4945 if (hp
== *hp
->bucket_hdr
)
4946 *hp
->bucket_hdr
= hp
->next
;
4952 * return hash function on name. must be compatible with the one
4953 * computed a step at a time, elsewhere
4956 hashf (name
, len
, hashsize
)
4964 r
= HASHSTEP (r
, *name
++);
4966 return MAKE_POS (r
) % hashsize
;
4969 /* Dump all macro definitions as #defines to stdout. */
4976 for (bucket
= 0; bucket
< HASHSIZE
; bucket
++) {
4979 for (hp
= hashtab
[bucket
]; hp
; hp
= hp
->next
) {
4980 if (hp
->type
== T_MACRO
) {
4981 DEFINITION
*defn
= hp
->value
.defn
;
4987 /* Print the definition of the macro HP. */
4989 printf ("#define %s", hp
->name
);
4990 if (defn
->nargs
>= 0) {
4994 for (i
= 0; i
< defn
->nargs
; i
++) {
4995 dump_arg_n (defn
, i
);
4996 if (i
+ 1 < defn
->nargs
)
5006 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
) {
5007 dump_defn_1 (defn
->expansion
, offset
, ap
->nchars
);
5008 if (ap
->nchars
!= 0)
5010 offset
+= ap
->nchars
;
5013 if (ap
->raw_before
&& !concat
)
5016 dump_arg_n (defn
, ap
->argno
);
5017 if (ap
->raw_after
) {
5022 dump_defn_1 (defn
->expansion
, offset
, defn
->length
- offset
);
5029 /* Output to stdout a substring of a macro definition.
5030 BASE is the beginning of the definition.
5031 Output characters START thru LENGTH.
5032 Discard newlines outside of strings, thus
5033 converting funny-space markers to ordinary spaces. */
5035 dump_defn_1 (base
, start
, length
)
5040 const U_CHAR
*p
= base
+ start
;
5041 const U_CHAR
*limit
= base
+ start
+ length
;
5046 else if (*p
== '\"' || *p
=='\'') {
5047 const U_CHAR
*p1
= skip_quoted_string (p
, limit
, 0, 0, 0, 0);
5048 fwrite (p
, p1
- p
, 1, stdout
);
5055 /* Print the name of argument number ARGNUM of macro definition DEFN.
5056 Recall that DEFN->argnames contains all the arg names
5057 concatenated in reverse order with comma-space in between. */
5059 dump_arg_n (defn
, argnum
)
5063 const U_CHAR
*p
= defn
->argnames
;
5064 while (argnum
+ 1 < defn
->nargs
) {
5065 p
= (const U_CHAR
*) strchr ((const char *)p
, ' ') + 1;
5069 while (*p
&& *p
!= ',') {
5075 /* Initialize the built-in macros. */
5076 #define DSC(x) U x, sizeof x - 1
5077 #define install_spec(name, type) \
5078 install(DSC(name), type, -1);
5079 #define install_value(name, val) \
5080 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
5082 initialize_builtins ()
5086 install_spec ("__BASE_FILE__", T_BASE_FILE
);
5087 install_spec ("__DATE__", T_DATE
);
5088 install_spec ("__FILE__", T_FILE
);
5089 install_spec ("__TIME__", T_TIME
);
5090 install_spec ("__VERSION__", T_VERSION
);
5091 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL
);
5092 install_spec ("__LINE__", T_SPECLINE
);
5094 #ifndef NO_BUILTIN_SIZE_TYPE
5095 install_value ("__SIZE_TYPE__", SIZE_TYPE
);
5097 #ifndef NO_BUILTIN_PTRDIFF_TYPE
5098 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE
);
5100 #ifndef NO_BUILTIN_WCHAR_TYPE
5101 install_value ("__WCHAR_TYPE__", WCHAR_TYPE
);
5103 #ifndef NO_BUILTIN_WINT_TYPE
5104 install_value ("__WINT_TYPE__", WINT_TYPE
);
5106 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX
);
5107 install_value ("__USER_LABEL_PREFIX__", user_label_prefix
);
5111 #undef install_value
5113 /* Common handler of command line directives -U, -D and -A. */
5115 run_directive (str
, len
, type
)
5118 enum node_type type
;
5120 const struct directive
*kt
;
5121 FILE_BUF
*ip
= &instack
[++indepth
];
5122 ip
->fname
= "*command line*";
5124 ip
->buf
= ip
->bufp
= (U_CHAR
*) str
;
5129 ip
->if_stack
= if_stack
;
5131 for (kt
= directive_table
; kt
->type
!= type
; kt
++)
5134 (*kt
->func
) ((U_CHAR
*) str
, (U_CHAR
*) str
+ len
, NULL
);
5138 /* Handle the -D option. If STR is just an identifier, define it with
5139 * value 1. If STR has anything after the identifier, then it should
5140 * be identifier-space-definition. */
5142 make_definition (str
)
5148 /* Copy the entire option so we can modify it.
5149 Change the first "=" in the string to a space. If there is none,
5150 tack " 1" on the end. */
5152 /* Length including the null. */
5153 count
= strlen (str
);
5154 buf
= (char *) alloca (count
+ 2);
5155 memcpy (buf
, str
, count
);
5157 p
= strchr (str
, '=');
5166 run_directive (buf
, count
, T_DEFINE
);
5169 /* Handle the -U option. */
5174 run_directive (str
, strlen (str
), T_UNDEF
);
5177 /* Handles the #assert (-A) and #unassert (-A-) command line options. */
5179 make_assertion (str
)
5182 enum node_type type
= T_ASSERT
;
5192 count
= strlen (str
);
5193 p
= strchr (str
, '=');
5196 /* Copy the entire option so we can modify it. Change the first
5197 "=" in the string to a '(', and tack a ')' on the end. */
5198 char *buf
= (char *) alloca (count
+ 1);
5200 memcpy (buf
, str
, count
);
5206 run_directive (str
, count
, type
);
5209 /* Get the file-mode and data size of the file open on FD
5210 and store them in *MODE_POINTER and *SIZE_POINTER. */
5213 file_size_and_mode (fd
, mode_pointer
, size_pointer
)
5220 if (fstat (fd
, &sbuf
) < 0) return -1;
5221 if (mode_pointer
) *mode_pointer
= sbuf
.st_mode
;
5222 if (size_pointer
) *size_pointer
= sbuf
.st_size
;