Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / cpplib.c
blob9c730860b7e93754212016bd3bf7354b205d871b
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-97, 1998 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
24 #ifndef STDC_VALUE
25 #define STDC_VALUE 1
26 #endif
28 #include <signal.h>
30 #ifdef HAVE_SYS_RESOURCE_H
31 # include <sys/resource.h>
32 #endif
34 #include "cpplib.h"
35 #include "cpphash.h"
36 #include "gansidecl.h"
37 #include "intl.h"
39 extern char *update_path ();
41 #undef MIN
42 #undef MAX
43 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
44 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
46 /* Find the largest host integer type and set its size and type.
47 Watch out: on some crazy hosts `long' is shorter than `int'. */
49 #ifndef HOST_WIDE_INT
50 # if HAVE_INTTYPES_H
51 # include <inttypes.h>
52 # define HOST_WIDE_INT intmax_t
53 # else
54 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
55 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
56 # define HOST_WIDE_INT int
57 # else
58 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
59 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
60 # define HOST_WIDE_INT long
61 # else
62 # define HOST_WIDE_INT long long
63 # endif
64 # endif
65 # endif
66 #endif
68 /* By default, colon separates directories in a path. */
69 #ifndef PATH_SEPARATOR
70 #define PATH_SEPARATOR ':'
71 #endif
73 #ifndef STANDARD_INCLUDE_DIR
74 #define STANDARD_INCLUDE_DIR "/usr/include"
75 #endif
76 #ifndef INCLUDE_LEN_FUDGE
77 #define INCLUDE_LEN_FUDGE 0
78 #endif
80 /* Symbols to predefine. */
82 #ifdef CPP_PREDEFINES
83 static char *predefs = CPP_PREDEFINES;
84 #else
85 static char *predefs = "";
86 #endif
88 /* We let tm.h override the types used here, to handle trivial differences
89 such as the choice of unsigned int or long unsigned int for size_t.
90 When machines start needing nontrivial differences in the size type,
91 it would be best to do something here to figure out automatically
92 from other information what type to use. */
94 /* The string value for __SIZE_TYPE__. */
96 #ifndef SIZE_TYPE
97 #define SIZE_TYPE "long unsigned int"
98 #endif
100 /* The string value for __PTRDIFF_TYPE__. */
102 #ifndef PTRDIFF_TYPE
103 #define PTRDIFF_TYPE "long int"
104 #endif
106 /* The string value for __WCHAR_TYPE__. */
108 #ifndef WCHAR_TYPE
109 #define WCHAR_TYPE "int"
110 #endif
111 #define CPP_WCHAR_TYPE(PFILE) \
112 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
114 /* The string value for __USER_LABEL_PREFIX__ */
116 #ifndef USER_LABEL_PREFIX
117 #define USER_LABEL_PREFIX ""
118 #endif
120 /* The string value for __REGISTER_PREFIX__ */
122 #ifndef REGISTER_PREFIX
123 #define REGISTER_PREFIX ""
124 #endif
126 /* In the definition of a #assert name, this structure forms
127 a list of the individual values asserted.
128 Each value is itself a list of "tokens".
129 These are strings that are compared by name. */
131 struct tokenlist_list {
132 struct tokenlist_list *next;
133 struct arglist *tokens;
136 struct assertion_hashnode {
137 struct assertion_hashnode *next; /* double links for easy deletion */
138 struct assertion_hashnode *prev;
139 /* also, a back pointer to this node's hash
140 chain is kept, in case the node is the head
141 of the chain and gets deleted. */
142 struct assertion_hashnode **bucket_hdr;
143 int length; /* length of token, for quick comparison */
144 U_CHAR *name; /* the actual name */
145 /* List of token-sequences. */
146 struct tokenlist_list *value;
149 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
150 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
152 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
153 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
154 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
155 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
156 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
157 (Note that it is false while we're expanding marco *arguments*.) */
158 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
160 /* Move all backslash-newline pairs out of embarrassing places.
161 Exchange all such pairs following BP
162 with any potentially-embarrassing characters that follow them.
163 Potentially-embarrassing characters are / and *
164 (because a backslash-newline inside a comment delimiter
165 would cause it not to be recognized). */
167 #define NEWLINE_FIX \
168 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
170 /* Same, but assume we've already read the potential '\\' into C. */
171 #define NEWLINE_FIX1(C) do { \
172 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
173 } while(0)
175 struct cpp_pending {
176 struct cpp_pending *next;
177 char *cmd;
178 char *arg;
181 /* Forward declarations. */
183 char *xmalloc ();
184 void cpp_fatal ();
185 void cpp_file_line_for_message PARAMS ((cpp_reader *, char *, int, int));
186 void cpp_hash_cleanup PARAMS ((cpp_reader *));
187 void cpp_message ();
188 void cpp_print_containing_files PARAMS ((cpp_reader *));
190 static void add_import ();
191 static void append_include_chain ();
192 static void make_assertion ();
193 static void path_include ();
194 static void initialize_builtins ();
195 static void initialize_char_syntax ();
196 extern void delete_macro ();
197 #if 0
198 static void trigraph_pcp ();
199 #endif
200 static int finclude ();
201 static void validate_else ();
202 static int comp_def_part ();
203 #ifdef abort
204 extern void fancy_abort ();
205 #endif
206 static int lookup_import ();
207 static int redundant_include_p ();
208 static is_system_include ();
209 static struct file_name_map *read_name_map ();
210 static char *read_filename_string ();
211 static int open_include_file ();
212 static int check_macro_name ();
213 static int compare_defs ();
214 static int compare_token_lists ();
215 static HOST_WIDE_INT eval_if_expression ();
216 static int change_newlines ();
217 extern int hashf ();
218 static struct arglist *read_token_list ();
219 static void free_token_list ();
220 static int safe_read ();
221 static void push_macro_expansion PARAMS ((cpp_reader *,
222 U_CHAR *, int, HASHNODE *));
223 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
224 extern char *xrealloc ();
225 static char *xcalloc ();
226 static char *savestring ();
228 static void conditional_skip ();
229 static void skip_if_group ();
231 /* Last arg to output_line_command. */
232 enum file_change_code {same_file, enter_file, leave_file};
234 /* External declarations. */
236 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
238 extern FILE *fdopen ();
239 extern char *version_string;
240 extern struct tm *localtime ();
242 /* These functions are declared to return int instead of void since they
243 are going to be placed in a table and some old compilers have trouble with
244 pointers to functions returning void. */
246 static int do_define ();
247 static int do_line ();
248 static int do_include ();
249 static int do_undef ();
250 static int do_error ();
251 static int do_pragma ();
252 static int do_ident ();
253 static int do_if ();
254 static int do_xifdef ();
255 static int do_else ();
256 static int do_elif ();
257 static int do_endif ();
258 static int do_sccs ();
259 static int do_once ();
260 static int do_assert ();
261 static int do_unassert ();
262 static int do_warning ();
264 struct file_name_list
266 struct file_name_list *next;
267 char *fname;
268 /* If the following is nonzero, it is a macro name.
269 Don't include the file again if that macro is defined. */
270 U_CHAR *control_macro;
271 /* If the following is nonzero, it is a C-language system include
272 directory. */
273 int c_system_include_path;
274 /* Mapping of file names for this directory. */
275 struct file_name_map *name_map;
276 /* Non-zero if name_map is valid. */
277 int got_name_map;
280 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
281 via the same directory as the file that #included it. */
282 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
284 /* #include "file" looks in source file dir, then stack. */
285 /* #include <file> just looks in the stack. */
286 /* -I directories are added to the end, then the defaults are added. */
287 /* The */
288 static struct default_include {
289 char *fname; /* The name of the directory. */
290 char *component; /* The component containing the directory */
291 int cplusplus; /* Only look here if we're compiling C++. */
292 int cxx_aware; /* Includes in this directory don't need to
293 be wrapped in extern "C" when compiling
294 C++. */
295 } include_defaults_array[]
296 #ifdef INCLUDE_DEFAULTS
297 = INCLUDE_DEFAULTS;
298 #else
300 /* Pick up GNU C++ specific include files. */
301 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
302 { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 },
303 #ifdef CROSS_COMPILE
304 /* This is the dir for fixincludes. Put it just before
305 the files that we fix. */
306 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
307 /* For cross-compilation, this dir name is generated
308 automatically in Makefile.in. */
309 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
310 #ifdef TOOL_INCLUDE_DIR
311 /* This is another place that the target system's headers might be. */
312 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
313 #endif
314 #else /* not CROSS_COMPILE */
315 #ifdef LOCAL_INCLUDE_DIR
316 /* This should be /usr/local/include and should come before
317 the fixincludes-fixed header files. */
318 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
319 #endif
320 #ifdef TOOL_INCLUDE_DIR
321 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
322 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
323 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
324 #endif
325 /* This is the dir for fixincludes. Put it just before
326 the files that we fix. */
327 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
328 /* Some systems have an extra dir of include files. */
329 #ifdef SYSTEM_INCLUDE_DIR
330 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
331 #endif
332 #ifndef STANDARD_INCLUDE_COMPONENT
333 #define STANDARD_INCLUDE_COMPONENT 0
334 #endif
335 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
336 #endif /* not CROSS_COMPILE */
337 { 0, 0, 0, 0 }
339 #endif /* no INCLUDE_DEFAULTS */
341 /* `struct directive' defines one #-directive, including how to handle it. */
343 struct directive {
344 int length; /* Length of name */
345 int (*func)(); /* Function to handle directive */
346 char *name; /* Name of directive */
347 enum node_type type; /* Code which describes which directive. */
348 char command_reads_line; /* One if rest of line is read by func. */
351 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
352 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
354 /* Here is the actual list of #-directives, most-often-used first.
355 The initialize_builtins function assumes #define is the very first. */
357 static struct directive directive_table[] = {
358 { 6, do_define, "define", T_DEFINE},
359 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
360 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
361 { 7, do_include, "include", T_INCLUDE, 1},
362 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
363 { 6, do_include, "import", T_IMPORT, 1},
364 { 5, do_endif, "endif", T_ENDIF, 1},
365 { 4, do_else, "else", T_ELSE, 1},
366 { 2, do_if, "if", T_IF, 1},
367 { 4, do_elif, "elif", T_ELIF, 1},
368 { 5, do_undef, "undef", T_UNDEF},
369 { 5, do_error, "error", T_ERROR},
370 { 7, do_warning, "warning", T_WARNING},
371 { 6, do_pragma, "pragma", T_PRAGMA},
372 { 4, do_line, "line", T_LINE, 1},
373 { 5, do_ident, "ident", T_IDENT, 1},
374 #ifdef SCCS_DIRECTIVE
375 { 4, do_sccs, "sccs", T_SCCS},
376 #endif
377 { 6, do_assert, "assert", T_ASSERT, 1},
378 { 8, do_unassert, "unassert", T_UNASSERT, 1},
379 { -1, 0, "", T_UNUSED},
382 /* table to tell if char can be part of a C identifier. */
383 U_CHAR is_idchar[256];
384 /* table to tell if char can be first char of a c identifier. */
385 U_CHAR is_idstart[256];
386 /* table to tell if c is horizontal space. */
387 U_CHAR is_hor_space[256];
388 /* table to tell if c is horizontal or vertical space. */
389 static U_CHAR is_space[256];
391 /* Initialize syntactic classifications of characters. */
393 static void
394 initialize_char_syntax (opts)
395 struct cpp_options *opts;
397 register int i;
400 * Set up is_idchar and is_idstart tables. These should be
401 * faster than saying (is_alpha (c) || c == '_'), etc.
402 * Set up these things before calling any routines tthat
403 * refer to them.
405 for (i = 'a'; i <= 'z'; i++) {
406 is_idchar[i - 'a' + 'A'] = 1;
407 is_idchar[i] = 1;
408 is_idstart[i - 'a' + 'A'] = 1;
409 is_idstart[i] = 1;
411 for (i = '0'; i <= '9'; i++)
412 is_idchar[i] = 1;
413 is_idchar['_'] = 1;
414 is_idstart['_'] = 1;
415 is_idchar['$'] = opts->dollars_in_ident;
416 is_idstart['$'] = opts->dollars_in_ident;
418 /* horizontal space table */
419 is_hor_space[' '] = 1;
420 is_hor_space['\t'] = 1;
421 is_hor_space['\v'] = 1;
422 is_hor_space['\f'] = 1;
423 is_hor_space['\r'] = 1;
425 is_space[' '] = 1;
426 is_space['\t'] = 1;
427 is_space['\v'] = 1;
428 is_space['\f'] = 1;
429 is_space['\n'] = 1;
430 is_space['\r'] = 1;
434 /* Place into PFILE a quoted string representing the string SRC.
435 Caller must reserve enough space in pfile->token_buffer. */
437 static void
438 quote_string (pfile, src)
439 cpp_reader *pfile;
440 char *src;
442 U_CHAR c;
444 CPP_PUTC_Q (pfile, '\"');
445 for (;;)
446 switch ((c = *src++))
448 default:
449 if (isprint (c))
450 CPP_PUTC_Q (pfile, c);
451 else
453 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
454 CPP_ADJUST_WRITTEN (pfile, 4);
456 break;
458 case '\"':
459 case '\\':
460 CPP_PUTC_Q (pfile, '\\');
461 CPP_PUTC_Q (pfile, c);
462 break;
464 case '\0':
465 CPP_PUTC_Q (pfile, '\"');
466 CPP_NUL_TERMINATE_Q (pfile);
467 return;
471 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
473 void
474 cpp_grow_buffer (pfile, n)
475 cpp_reader *pfile;
476 long n;
478 long old_written = CPP_WRITTEN (pfile);
479 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
480 pfile->token_buffer = (U_CHAR *)
481 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
482 CPP_SET_WRITTEN (pfile, old_written);
487 * process a given definition string, for initialization
488 * If STR is just an identifier, define it with value 1.
489 * If STR has anything after the identifier, then it should
490 * be identifier=definition.
493 void
494 cpp_define (pfile, str)
495 cpp_reader *pfile;
496 U_CHAR *str;
498 U_CHAR *buf, *p;
500 buf = str;
501 p = str;
502 if (!is_idstart[*p])
504 cpp_error (pfile, "malformed option `-D %s'", str);
505 return;
507 while (is_idchar[*++p])
509 if (*p == 0)
511 buf = (U_CHAR *) alloca (p - buf + 4);
512 strcpy ((char *)buf, str);
513 strcat ((char *)buf, " 1");
515 else if (*p != '=')
517 cpp_error (pfile, "malformed option `-D %s'", str);
518 return;
520 else
522 U_CHAR *q;
523 /* Copy the entire option so we can modify it. */
524 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
525 strncpy (buf, str, p - str);
526 /* Change the = to a space. */
527 buf[p - str] = ' ';
528 /* Scan for any backslash-newline and remove it. */
529 p++;
530 q = &buf[p - str];
531 while (*p)
533 if (*p == '\\' && p[1] == '\n')
534 p += 2;
535 else
536 *q++ = *p++;
538 *q = 0;
541 do_define (pfile, NULL, buf, buf + strlen (buf));
544 /* Process the string STR as if it appeared as the body of a #assert.
545 OPTION is the option name for which STR was the argument. */
547 static void
548 make_assertion (pfile, option, str)
549 cpp_reader *pfile;
550 char *option;
551 U_CHAR *str;
553 struct directive *kt;
554 U_CHAR *buf, *p, *q;
556 /* Copy the entire option so we can modify it. */
557 buf = (U_CHAR *) alloca (strlen (str) + 1);
558 strcpy ((char *) buf, str);
559 /* Scan for any backslash-newline and remove it. */
560 p = q = buf;
561 while (*p) {
562 #if 0
563 if (*p == '\\' && p[1] == '\n')
564 p += 2;
565 else
566 #endif
567 *q++ = *p++;
569 *q = 0;
571 p = buf;
572 if (!is_idstart[*p]) {
573 cpp_error (pfile, "malformed option `%s %s'", option, str);
574 return;
576 while (is_idchar[*++p])
578 while (*p == ' ' || *p == '\t') p++;
579 if (! (*p == 0 || *p == '(')) {
580 cpp_error (pfile, "malformed option `%s %s'", option, str);
581 return;
584 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
586 do_assert (pfile, NULL, NULL, NULL);
587 cpp_pop_buffer (pfile);
591 /* Append a chain of `struct file_name_list's
592 to the end of the main include chain.
593 FIRST is the beginning of the chain to append, and LAST is the end. */
595 static void
596 append_include_chain (pfile, first, last)
597 cpp_reader *pfile;
598 struct file_name_list *first, *last;
600 struct cpp_options *opts = CPP_OPTIONS (pfile);
601 struct file_name_list *dir;
603 if (!first || !last)
604 return;
606 if (opts->include == 0)
607 opts->include = first;
608 else
609 opts->last_include->next = first;
611 if (opts->first_bracket_include == 0)
612 opts->first_bracket_include = first;
614 for (dir = first; ; dir = dir->next) {
615 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
616 if (len > pfile->max_include_len)
617 pfile->max_include_len = len;
618 if (dir == last)
619 break;
622 last->next = NULL;
623 opts->last_include = last;
626 /* Add output to `deps_buffer' for the -M switch.
627 STRING points to the text to be output.
628 SPACER is ':' for targets, ' ' for dependencies, zero for text
629 to be inserted literally. */
631 static void
632 deps_output (pfile, string, spacer)
633 cpp_reader *pfile;
634 char *string;
635 int spacer;
637 int size = strlen (string);
639 if (size == 0)
640 return;
642 #ifndef MAX_OUTPUT_COLUMNS
643 #define MAX_OUTPUT_COLUMNS 72
644 #endif
645 if (spacer
646 && pfile->deps_column > 0
647 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
649 deps_output (pfile, " \\\n ", 0);
650 pfile->deps_column = 0;
653 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
655 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
656 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
657 pfile->deps_allocated_size);
659 if (spacer == ' ' && pfile->deps_column > 0)
660 pfile->deps_buffer[pfile->deps_size++] = ' ';
661 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
662 pfile->deps_size += size;
663 pfile->deps_column += size;
664 if (spacer == ':')
665 pfile->deps_buffer[pfile->deps_size++] = ':';
666 pfile->deps_buffer[pfile->deps_size] = 0;
669 /* Given a colon-separated list of file names PATH,
670 add all the names to the search path for include files. */
672 static void
673 path_include (pfile, path)
674 cpp_reader *pfile;
675 char *path;
677 char *p;
679 p = path;
681 if (*p)
682 while (1) {
683 char *q = p;
684 char *name;
685 struct file_name_list *dirtmp;
687 /* Find the end of this name. */
688 while (*q != 0 && *q != PATH_SEPARATOR) q++;
689 if (p == q) {
690 /* An empty name in the path stands for the current directory. */
691 name = (char *) xmalloc (2);
692 name[0] = '.';
693 name[1] = 0;
694 } else {
695 /* Otherwise use the directory that is named. */
696 name = (char *) xmalloc (q - p + 1);
697 bcopy (p, name, q - p);
698 name[q - p] = 0;
701 dirtmp = (struct file_name_list *)
702 xmalloc (sizeof (struct file_name_list));
703 dirtmp->next = 0; /* New one goes on the end */
704 dirtmp->control_macro = 0;
705 dirtmp->c_system_include_path = 0;
706 dirtmp->fname = name;
707 dirtmp->got_name_map = 0;
708 append_include_chain (pfile, dirtmp, dirtmp);
710 /* Advance past this name. */
711 p = q;
712 if (*p == 0)
713 break;
714 /* Skip the colon. */
715 p++;
719 void
720 cpp_options_init (opts)
721 cpp_options *opts;
723 bzero ((char *) opts, sizeof *opts);
724 opts->in_fname = NULL;
725 opts->out_fname = NULL;
727 /* Initialize is_idchar to allow $. */
728 opts->dollars_in_ident = 1;
729 initialize_char_syntax (opts);
731 opts->no_line_commands = 0;
732 opts->no_trigraphs = 1;
733 opts->put_out_comments = 0;
734 opts->print_include_names = 0;
735 opts->dump_macros = dump_none;
736 opts->no_output = 0;
737 opts->remap = 0;
738 opts->cplusplus = 0;
739 opts->cplusplus_comments = 0;
741 opts->verbose = 0;
742 opts->objc = 0;
743 opts->lang_asm = 0;
744 opts->for_lint = 0;
745 opts->chill = 0;
746 opts->pedantic_errors = 0;
747 opts->inhibit_warnings = 0;
748 opts->warn_comments = 0;
749 opts->warn_import = 1;
750 opts->warnings_are_errors = 0;
753 enum cpp_token
754 null_underflow (pfile)
755 cpp_reader *pfile;
757 return CPP_EOF;
761 null_cleanup (pbuf, pfile)
762 cpp_buffer *pbuf;
763 cpp_reader *pfile;
765 return 0;
769 macro_cleanup (pbuf, pfile)
770 cpp_buffer *pbuf;
771 cpp_reader *pfile;
773 HASHNODE *macro = (HASHNODE *) pbuf->data;
774 if (macro->type == T_DISABLED)
775 macro->type = T_MACRO;
776 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
777 free (pbuf->buf);
778 return 0;
782 file_cleanup (pbuf, pfile)
783 cpp_buffer *pbuf;
784 cpp_reader *pfile;
786 if (pbuf->buf)
788 free (pbuf->buf);
789 pbuf->buf = 0;
791 return 0;
794 /* Assuming we have read '/'.
795 If this is the start of a comment (followed by '*' or '/'),
796 skip to the end of the comment, and return ' '.
797 Return EOF if we reached the end of file before the end of the comment.
798 If not the start of a comment, return '/'. */
800 static int
801 skip_comment (pfile, linep)
802 cpp_reader *pfile;
803 long *linep;
805 int c = 0;
806 while (PEEKC() == '\\' && PEEKN(1) == '\n')
808 if (linep)
809 (*linep)++;
810 FORWARD(2);
812 if (PEEKC() == '*')
814 FORWARD(1);
815 for (;;)
817 int prev_c = c;
818 c = GETC ();
819 if (c == EOF)
820 return EOF;
821 while (c == '\\' && PEEKC() == '\n')
823 if (linep)
824 (*linep)++;
825 FORWARD(1), c = GETC();
827 if (prev_c == '*' && c == '/')
828 return ' ';
829 if (c == '\n' && linep)
830 (*linep)++;
833 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
835 FORWARD(1);
836 for (;;)
838 c = GETC ();
839 if (c == EOF)
840 return ' '; /* Allow // to be terminated by EOF. */
841 while (c == '\\' && PEEKC() == '\n')
843 FORWARD(1);
844 c = GETC();
845 if (linep)
846 (*linep)++;
848 if (c == '\n')
850 /* Don't consider final '\n' to be part of comment. */
851 FORWARD(-1);
852 return ' ';
856 else
857 return '/';
860 /* Skip whitespace \-newline and comments. Does not macro-expand. */
862 void
863 cpp_skip_hspace (pfile)
864 cpp_reader *pfile;
866 while (1)
868 int c = PEEKC();
869 if (c == EOF)
870 return; /* FIXME */
871 if (is_hor_space[c])
873 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
874 cpp_pedwarn (pfile, "%s in preprocessing directive",
875 c == '\f' ? "formfeed" : "vertical tab");
876 FORWARD(1);
878 else if (c == '/')
880 FORWARD (1);
881 c = skip_comment (pfile, NULL);
882 if (c == '/')
883 FORWARD(-1);
884 if (c == EOF || c == '/')
885 return;
887 else if (c == '\\' && PEEKN(1) == '\n') {
888 FORWARD(2);
890 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
891 && is_hor_space[PEEKN(1)])
892 FORWARD(2);
893 else return;
897 /* Read the rest of the current line.
898 The line is appended to PFILE's output buffer. */
900 static void
901 copy_rest_of_line (pfile)
902 cpp_reader *pfile;
904 struct cpp_options *opts = CPP_OPTIONS (pfile);
905 for (;;)
907 int c = GETC();
908 int nextc;
909 switch (c)
911 case EOF:
912 goto end_directive;
913 case '\\':
914 if (PEEKC() == '\n')
916 FORWARD (1);
917 continue;
919 case '\'':
920 case '\"':
921 goto scan_directive_token;
922 break;
923 case '/':
924 nextc = PEEKC();
925 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
926 goto scan_directive_token;
927 break;
928 case '\f':
929 case '\v':
930 if (CPP_PEDANTIC (pfile))
931 cpp_pedwarn (pfile, "%s in preprocessing directive",
932 c == '\f' ? "formfeed" : "vertical tab");
933 break;
935 case '\n':
936 FORWARD(-1);
937 goto end_directive;
938 scan_directive_token:
939 FORWARD(-1);
940 cpp_get_token (pfile);
941 continue;
943 CPP_PUTC (pfile, c);
945 end_directive: ;
946 CPP_NUL_TERMINATE (pfile);
949 void
950 skip_rest_of_line (pfile)
951 cpp_reader *pfile;
953 long old = CPP_WRITTEN (pfile);
954 copy_rest_of_line (pfile);
955 CPP_SET_WRITTEN (pfile, old);
958 /* Handle a possible # directive.
959 '#' has already been read. */
962 handle_directive (pfile)
963 cpp_reader *pfile;
964 { int c;
965 register struct directive *kt;
966 int ident_length;
967 long after_ident;
968 U_CHAR *ident, *line_end;
969 long old_written = CPP_WRITTEN (pfile);
971 cpp_skip_hspace (pfile);
973 c = PEEKC ();
974 if (c >= '0' && c <= '9')
976 /* Handle # followed by a line number. */
977 if (CPP_PEDANTIC (pfile))
978 cpp_pedwarn (pfile, "`#' followed by integer");
979 do_line (pfile, NULL);
980 goto done_a_directive;
983 /* Now find the directive name. */
984 CPP_PUTC (pfile, '#');
985 parse_name (pfile, GETC());
986 ident = pfile->token_buffer + old_written + 1;
987 ident_length = CPP_PWRITTEN (pfile) - ident;
988 if (ident_length == 0 && PEEKC() == '\n')
990 /* A line of just `#' becomes blank. */
991 goto done_a_directive;
994 #if 0
995 if (ident_length == 0 || !is_idstart[*ident]) {
996 U_CHAR *p = ident;
997 while (is_idchar[*p]) {
998 if (*p < '0' || *p > '9')
999 break;
1000 p++;
1002 /* Avoid error for `###' and similar cases unless -pedantic. */
1003 if (p == ident) {
1004 while (*p == '#' || is_hor_space[*p]) p++;
1005 if (*p == '\n') {
1006 if (pedantic && !lang_asm)
1007 cpp_warning (pfile, "invalid preprocessor directive");
1008 return 0;
1012 if (!lang_asm)
1013 cpp_error (pfile, "invalid preprocessor directive name");
1015 return 0;
1017 #endif
1019 * Decode the keyword and call the appropriate expansion
1020 * routine, after moving the input pointer up to the next line.
1022 for (kt = directive_table; ; kt++) {
1023 if (kt->length <= 0)
1024 goto not_a_directive;
1025 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1026 break;
1029 if (kt->command_reads_line)
1030 after_ident = 0;
1031 else
1033 /* Nonzero means do not delete comments within the directive.
1034 #define needs this when -traditional. */
1035 int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE;
1036 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1037 CPP_OPTIONS (pfile)->put_out_comments = comments;
1038 after_ident = CPP_WRITTEN (pfile);
1039 copy_rest_of_line (pfile);
1040 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1043 /* We may want to pass through #define, #undef, #pragma, and #include.
1044 Other directives may create output, but we don't want the directive
1045 itself out, so we pop it now. For example conditionals may emit
1046 #failed ... #endfailed stuff. But note that popping the buffer
1047 means the parameters to kt->func may point after pfile->limit
1048 so these parameters are invalid as soon as something gets appended
1049 to the token_buffer. */
1051 line_end = CPP_PWRITTEN (pfile);
1052 if (! (kt->type == T_DEFINE
1053 || (kt->type == T_UNDEF
1054 && CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
1055 || kt->type == T_PRAGMA
1056 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
1057 && CPP_OPTIONS (pfile)->dump_includes)))
1058 CPP_SET_WRITTEN (pfile, old_written);
1060 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1062 if (kt->type == T_DEFINE)
1064 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
1066 /* Skip "#define". */
1067 U_CHAR *p = pfile->token_buffer + old_written + 7;
1069 SKIP_WHITE_SPACE (p);
1070 while (is_idchar[*p]) p++;
1071 pfile->limit = p;
1072 CPP_PUTC (pfile, '\n');
1074 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
1075 CPP_SET_WRITTEN (pfile, old_written);
1078 done_a_directive:
1079 return 1;
1081 not_a_directive:
1082 return 0;
1085 /* Pass a directive through to the output file.
1086 BUF points to the contents of the directive, as a contiguous string.
1087 LIMIT points to the first character past the end of the directive.
1088 KEYWORD is the keyword-table entry for the directive. */
1090 static void
1091 pass_thru_directive (buf, limit, pfile, keyword)
1092 U_CHAR *buf, *limit;
1093 cpp_reader *pfile;
1094 struct directive *keyword;
1096 register unsigned keyword_length = keyword->length;
1098 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1099 CPP_PUTC_Q (pfile, '#');
1100 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1101 if (limit != buf && buf[0] != ' ')
1102 CPP_PUTC_Q (pfile, ' ');
1103 CPP_PUTS_Q (pfile, buf, limit - buf);
1104 #if 0
1105 CPP_PUTS_Q (pfile, '\n');
1106 /* Count the line we have just made in the output,
1107 to get in sync properly. */
1108 pfile->lineno++;
1109 #endif
1112 /* The arglist structure is built by do_define to tell
1113 collect_definition where the argument names begin. That
1114 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1115 would contain pointers to the strings x, y, and z.
1116 Collect_definition would then build a DEFINITION node,
1117 with reflist nodes pointing to the places x, y, and z had
1118 appeared. So the arglist is just convenience data passed
1119 between these two routines. It is not kept around after
1120 the current #define has been processed and entered into the
1121 hash table. */
1123 struct arglist {
1124 struct arglist *next;
1125 U_CHAR *name;
1126 int length;
1127 int argno;
1128 char rest_args;
1131 /* Read a replacement list for a macro with parameters.
1132 Build the DEFINITION structure.
1133 Reads characters of text starting at BUF until END.
1134 ARGLIST specifies the formal parameters to look for
1135 in the text of the definition; NARGS is the number of args
1136 in that list, or -1 for a macro name that wants no argument list.
1137 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1138 and NAMELEN is its length in characters.
1140 Note that comments, backslash-newlines, and leading white space
1141 have already been deleted from the argument. */
1143 static DEFINITION *
1144 collect_expansion (pfile, buf, limit, nargs, arglist)
1145 cpp_reader *pfile;
1146 U_CHAR *buf, *limit;
1147 int nargs;
1148 struct arglist *arglist;
1150 DEFINITION *defn;
1151 register U_CHAR *p, *lastp, *exp_p;
1152 struct reflist *endpat = NULL;
1153 /* Pointer to first nonspace after last ## seen. */
1154 U_CHAR *concat = 0;
1155 /* Pointer to first nonspace after last single-# seen. */
1156 U_CHAR *stringify = 0;
1157 int maxsize;
1158 int expected_delimiter = '\0';
1160 /* Scan thru the replacement list, ignoring comments and quoted
1161 strings, picking up on the macro calls. It does a linear search
1162 thru the arg list on every potential symbol. Profiling might say
1163 that something smarter should happen. */
1165 if (limit < buf)
1166 abort ();
1168 /* Find the beginning of the trailing whitespace. */
1169 p = buf;
1170 while (p < limit && is_space[limit[-1]]) limit--;
1172 /* Allocate space for the text in the macro definition.
1173 Leading and trailing whitespace chars need 2 bytes each.
1174 Each other input char may or may not need 1 byte,
1175 so this is an upper bound. The extra 5 are for invented
1176 leading and trailing newline-marker and final null. */
1177 maxsize = (sizeof (DEFINITION)
1178 + (limit - p) + 5);
1179 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1180 while (p < limit)
1181 if (*p++ == '@')
1182 maxsize++;
1183 defn = (DEFINITION *) xcalloc (1, maxsize);
1185 defn->nargs = nargs;
1186 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1187 lastp = exp_p;
1189 p = buf;
1191 /* Add one initial space escape-marker to prevent accidental
1192 token-pasting (often removed by macroexpand). */
1193 *exp_p++ = '@';
1194 *exp_p++ = ' ';
1196 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1197 cpp_error (pfile, "`##' at start of macro definition");
1198 p += 2;
1201 /* Process the main body of the definition. */
1202 while (p < limit) {
1203 int skipped_arg = 0;
1204 register U_CHAR c = *p++;
1206 *exp_p++ = c;
1208 if (!CPP_TRADITIONAL (pfile)) {
1209 switch (c) {
1210 case '\'':
1211 case '\"':
1212 if (expected_delimiter != '\0') {
1213 if (c == expected_delimiter)
1214 expected_delimiter = '\0';
1215 } else
1216 expected_delimiter = c;
1217 break;
1219 case '\\':
1220 if (p < limit && expected_delimiter) {
1221 /* In a string, backslash goes through
1222 and makes next char ordinary. */
1223 *exp_p++ = *p++;
1225 break;
1227 case '@':
1228 /* An '@' in a string or character constant stands for itself,
1229 and does not need to be escaped. */
1230 if (!expected_delimiter)
1231 *exp_p++ = c;
1232 break;
1234 case '#':
1235 /* # is ordinary inside a string. */
1236 if (expected_delimiter)
1237 break;
1238 if (p < limit && *p == '#') {
1239 /* ##: concatenate preceding and following tokens. */
1240 /* Take out the first #, discard preceding whitespace. */
1241 exp_p--;
1242 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1243 --exp_p;
1244 /* Skip the second #. */
1245 p++;
1246 /* Discard following whitespace. */
1247 SKIP_WHITE_SPACE (p);
1248 concat = p;
1249 if (p == limit)
1250 cpp_error (pfile, "`##' at end of macro definition");
1251 } else if (nargs >= 0) {
1252 /* Single #: stringify following argument ref.
1253 Don't leave the # in the expansion. */
1254 exp_p--;
1255 SKIP_WHITE_SPACE (p);
1256 if (p == limit || ! is_idstart[*p]
1257 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1258 cpp_error (pfile,
1259 "`#' operator is not followed by a macro argument name");
1260 else
1261 stringify = p;
1263 break;
1265 } else {
1266 /* In -traditional mode, recognize arguments inside strings and
1267 and character constants, and ignore special properties of #.
1268 Arguments inside strings are considered "stringified", but no
1269 extra quote marks are supplied. */
1270 switch (c) {
1271 case '\'':
1272 case '\"':
1273 if (expected_delimiter != '\0') {
1274 if (c == expected_delimiter)
1275 expected_delimiter = '\0';
1276 } else
1277 expected_delimiter = c;
1278 break;
1280 case '\\':
1281 /* Backslash quotes delimiters and itself, but not macro args. */
1282 if (expected_delimiter != 0 && p < limit
1283 && (*p == expected_delimiter || *p == '\\')) {
1284 *exp_p++ = *p++;
1285 continue;
1287 break;
1289 case '/':
1290 if (expected_delimiter != '\0') /* No comments inside strings. */
1291 break;
1292 if (*p == '*') {
1293 /* If we find a comment that wasn't removed by handle_directive,
1294 this must be -traditional. So replace the comment with
1295 nothing at all. */
1296 exp_p--;
1297 p += 1;
1298 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1299 p++;
1300 #if 0
1301 /* Mark this as a concatenation-point, as if it had been ##. */
1302 concat = p;
1303 #endif
1305 break;
1309 /* Handle the start of a symbol. */
1310 if (is_idchar[c] && nargs > 0) {
1311 U_CHAR *id_beg = p - 1;
1312 int id_len;
1314 --exp_p;
1315 while (p != limit && is_idchar[*p]) p++;
1316 id_len = p - id_beg;
1318 if (is_idstart[c]
1319 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1320 register struct arglist *arg;
1322 for (arg = arglist; arg != NULL; arg = arg->next) {
1323 struct reflist *tpat;
1325 if (arg->name[0] == c
1326 && arg->length == id_len
1327 && strncmp (arg->name, id_beg, id_len) == 0) {
1328 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1329 if (CPP_TRADITIONAL (pfile)) {
1330 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1331 id_len, arg->name);
1332 } else {
1333 cpp_warning (pfile,
1334 "macro arg `%.*s' would be stringified with -traditional.",
1335 id_len, arg->name);
1338 /* If ANSI, don't actually substitute inside a string. */
1339 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1340 break;
1341 /* make a pat node for this arg and append it to the end of
1342 the pat list */
1343 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1344 tpat->next = NULL;
1345 tpat->raw_before = concat == id_beg;
1346 tpat->raw_after = 0;
1347 tpat->rest_args = arg->rest_args;
1348 tpat->stringify = (CPP_TRADITIONAL (pfile)
1349 ? expected_delimiter != '\0'
1350 : stringify == id_beg);
1352 if (endpat == NULL)
1353 defn->pattern = tpat;
1354 else
1355 endpat->next = tpat;
1356 endpat = tpat;
1358 tpat->argno = arg->argno;
1359 tpat->nchars = exp_p - lastp;
1361 register U_CHAR *p1 = p;
1362 SKIP_WHITE_SPACE (p1);
1363 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1364 tpat->raw_after = 1;
1366 lastp = exp_p; /* place to start copying from next time */
1367 skipped_arg = 1;
1368 break;
1373 /* If this was not a macro arg, copy it into the expansion. */
1374 if (! skipped_arg) {
1375 register U_CHAR *lim1 = p;
1376 p = id_beg;
1377 while (p != lim1)
1378 *exp_p++ = *p++;
1379 if (stringify == id_beg)
1380 cpp_error (pfile,
1381 "`#' operator should be followed by a macro argument name");
1386 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1388 /* If ANSI, put in a "@ " marker to prevent token pasting.
1389 But not if "inside a string" (which in ANSI mode
1390 happens only for -D option). */
1391 *exp_p++ = '@';
1392 *exp_p++ = ' ';
1395 *exp_p = '\0';
1397 defn->length = exp_p - defn->expansion;
1399 /* Crash now if we overrun the allocated size. */
1400 if (defn->length + 1 > maxsize)
1401 abort ();
1403 #if 0
1404 /* This isn't worth the time it takes. */
1405 /* give back excess storage */
1406 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1407 #endif
1409 return defn;
1413 * special extension string that can be added to the last macro argument to
1414 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1415 * #define wow(a, b...) process (b, a, b)
1416 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1417 * { wow (one, two); } -> { process (two, one, two); }
1418 * if this "rest_arg" is used with the concat token '##' and if it is not
1419 * supplied then the token attached to with ## will not be outputted. Ex:
1420 * #define wow (a, b...) process (b ## , a, ## b)
1421 * { wow (1, 2); } -> { process (2, 1, 2); }
1422 * { wow (one); } -> { process (one); {
1424 static char rest_extension[] = "...";
1425 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1427 /* Create a DEFINITION node from a #define directive. Arguments are
1428 as for do_define. */
1430 static MACRODEF
1431 create_definition (buf, limit, pfile, predefinition)
1432 U_CHAR *buf, *limit;
1433 cpp_reader *pfile;
1434 int predefinition;
1436 U_CHAR *bp; /* temp ptr into input buffer */
1437 U_CHAR *symname; /* remember where symbol name starts */
1438 int sym_length; /* and how long it is */
1439 int rest_args = 0;
1440 long line, col;
1441 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1442 DEFINITION *defn;
1443 int arglengths = 0; /* Accumulate lengths of arg names
1444 plus number of args. */
1445 MACRODEF mdef;
1446 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1448 bp = buf;
1450 while (is_hor_space[*bp])
1451 bp++;
1453 symname = bp; /* remember where it starts */
1455 sym_length = check_macro_name (pfile, bp, 0);
1456 bp += sym_length;
1458 /* Lossage will occur if identifiers or control keywords are broken
1459 across lines using backslash. This is not the right place to take
1460 care of that. */
1462 if (*bp == '(') {
1463 struct arglist *arg_ptrs = NULL;
1464 int argno = 0;
1466 bp++; /* skip '(' */
1467 SKIP_WHITE_SPACE (bp);
1469 /* Loop over macro argument names. */
1470 while (*bp != ')') {
1471 struct arglist *temp;
1473 temp = (struct arglist *) alloca (sizeof (struct arglist));
1474 temp->name = bp;
1475 temp->next = arg_ptrs;
1476 temp->argno = argno++;
1477 temp->rest_args = 0;
1478 arg_ptrs = temp;
1480 if (rest_args)
1481 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1483 if (!is_idstart[*bp])
1484 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1486 /* Find the end of the arg name. */
1487 while (is_idchar[*bp]) {
1488 bp++;
1489 /* do we have a "special" rest-args extension here? */
1490 if (limit - bp > REST_EXTENSION_LENGTH
1491 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1492 rest_args = 1;
1493 temp->rest_args = 1;
1494 break;
1497 temp->length = bp - temp->name;
1498 if (rest_args == 1)
1499 bp += REST_EXTENSION_LENGTH;
1500 arglengths += temp->length + 2;
1501 SKIP_WHITE_SPACE (bp);
1502 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1503 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1504 goto nope;
1506 if (*bp == ',') {
1507 bp++;
1508 SKIP_WHITE_SPACE (bp);
1510 if (bp >= limit) {
1511 cpp_error (pfile, "unterminated parameter list in `#define'");
1512 goto nope;
1515 struct arglist *otemp;
1517 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1518 if (temp->length == otemp->length
1519 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1520 U_CHAR *name;
1522 name = (U_CHAR *) alloca (temp->length + 1);
1523 (void) strncpy (name, temp->name, temp->length);
1524 name[temp->length] = '\0';
1525 cpp_error (pfile,
1526 "duplicate argument name `%s' in `#define'", name);
1527 goto nope;
1532 ++bp; /* skip paren */
1533 SKIP_WHITE_SPACE (bp);
1534 /* now everything from bp before limit is the definition. */
1535 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1536 defn->rest_args = rest_args;
1538 /* Now set defn->args.argnames to the result of concatenating
1539 the argument names in reverse order
1540 with comma-space between them. */
1541 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1543 struct arglist *temp;
1544 int i = 0;
1545 for (temp = arg_ptrs; temp; temp = temp->next) {
1546 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1547 i += temp->length;
1548 if (temp->next != 0) {
1549 defn->args.argnames[i++] = ',';
1550 defn->args.argnames[i++] = ' ';
1553 defn->args.argnames[i] = 0;
1555 } else {
1556 /* Simple expansion or empty definition. */
1558 if (bp < limit)
1560 if (is_hor_space[*bp]) {
1561 bp++;
1562 SKIP_WHITE_SPACE (bp);
1563 } else {
1564 switch (*bp) {
1565 case '!': case '"': case '#': case '%': case '&': case '\'':
1566 case ')': case '*': case '+': case ',': case '-': case '.':
1567 case '/': case ':': case ';': case '<': case '=': case '>':
1568 case '?': case '[': case '\\': case ']': case '^': case '{':
1569 case '|': case '}': case '~':
1570 cpp_warning (pfile, "missing white space after `#define %.*s'",
1571 sym_length, symname);
1572 break;
1574 default:
1575 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1576 sym_length, symname);
1577 break;
1581 /* now everything from bp before limit is the definition. */
1582 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1583 defn->args.argnames = (U_CHAR *) "";
1586 defn->line = line;
1587 defn->file = file;
1589 /* OP is null if this is a predefinition */
1590 defn->predefined = predefinition;
1591 mdef.defn = defn;
1592 mdef.symnam = symname;
1593 mdef.symlen = sym_length;
1595 return mdef;
1597 nope:
1598 mdef.defn = 0;
1599 return mdef;
1602 /* Check a purported macro name SYMNAME, and yield its length.
1603 ASSERTION is nonzero if this is really for an assertion name. */
1605 static int
1606 check_macro_name (pfile, symname, assertion)
1607 cpp_reader *pfile;
1608 U_CHAR *symname;
1609 int assertion;
1611 U_CHAR *p;
1612 int sym_length;
1614 for (p = symname; is_idchar[*p]; p++)
1616 sym_length = p - symname;
1617 if (sym_length == 0
1618 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1619 cpp_error (pfile,
1620 assertion ? "invalid assertion name" : "invalid macro name");
1621 else if (!is_idstart[*symname]
1622 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
1623 U_CHAR *msg; /* what pain... */
1624 msg = (U_CHAR *) alloca (sym_length + 1);
1625 bcopy (symname, msg, sym_length);
1626 msg[sym_length] = 0;
1627 cpp_error (pfile,
1628 (assertion
1629 ? "invalid assertion name `%s'"
1630 : "invalid macro name `%s'"),
1631 msg);
1633 return sym_length;
1636 /* Return zero if two DEFINITIONs are isomorphic. */
1638 static int
1639 compare_defs (pfile, d1, d2)
1640 cpp_reader *pfile;
1641 DEFINITION *d1, *d2;
1643 register struct reflist *a1, *a2;
1644 register U_CHAR *p1 = d1->expansion;
1645 register U_CHAR *p2 = d2->expansion;
1646 int first = 1;
1648 if (d1->nargs != d2->nargs)
1649 return 1;
1650 if (CPP_PEDANTIC (pfile)
1651 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1652 return 1;
1653 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1654 a1 = a1->next, a2 = a2->next) {
1655 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1656 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1657 || a1->argno != a2->argno
1658 || a1->stringify != a2->stringify
1659 || a1->raw_before != a2->raw_before
1660 || a1->raw_after != a2->raw_after)
1661 return 1;
1662 first = 0;
1663 p1 += a1->nchars;
1664 p2 += a2->nchars;
1666 if (a1 != a2)
1667 return 1;
1668 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1669 p2, d2->length - (p2 - d2->expansion), 1))
1670 return 1;
1671 return 0;
1674 /* Return 1 if two parts of two macro definitions are effectively different.
1675 One of the parts starts at BEG1 and has LEN1 chars;
1676 the other has LEN2 chars at BEG2.
1677 Any sequence of whitespace matches any other sequence of whitespace.
1678 FIRST means these parts are the first of a macro definition;
1679 so ignore leading whitespace entirely.
1680 LAST means these parts are the last of a macro definition;
1681 so ignore trailing whitespace entirely. */
1683 static int
1684 comp_def_part (first, beg1, len1, beg2, len2, last)
1685 int first;
1686 U_CHAR *beg1, *beg2;
1687 int len1, len2;
1688 int last;
1690 register U_CHAR *end1 = beg1 + len1;
1691 register U_CHAR *end2 = beg2 + len2;
1692 if (first) {
1693 while (beg1 != end1 && is_space[*beg1]) beg1++;
1694 while (beg2 != end2 && is_space[*beg2]) beg2++;
1696 if (last) {
1697 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1698 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1700 while (beg1 != end1 && beg2 != end2) {
1701 if (is_space[*beg1] && is_space[*beg2]) {
1702 while (beg1 != end1 && is_space[*beg1]) beg1++;
1703 while (beg2 != end2 && is_space[*beg2]) beg2++;
1704 } else if (*beg1 == *beg2) {
1705 beg1++; beg2++;
1706 } else break;
1708 return (beg1 != end1) || (beg2 != end2);
1711 /* Process a #define command.
1712 BUF points to the contents of the #define command, as a contiguous string.
1713 LIMIT points to the first character past the end of the definition.
1714 KEYWORD is the keyword-table entry for #define,
1715 or NULL for a "predefined" macro. */
1717 static int
1718 do_define (pfile, keyword, buf, limit)
1719 cpp_reader *pfile;
1720 struct directive *keyword;
1721 U_CHAR *buf, *limit;
1723 int hashcode;
1724 MACRODEF mdef;
1725 HASHNODE *hp;
1727 #if 0
1728 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1729 if (pcp_outfile && keyword)
1730 pass_thru_directive (buf, limit, pfile, keyword);
1731 #endif
1733 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1734 if (mdef.defn == 0)
1735 goto nope;
1737 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1739 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1741 int ok = 0;
1742 /* Redefining a precompiled key is ok. */
1743 if (hp->type == T_PCSTRING)
1744 ok = 1;
1745 /* Redefining a macro is ok if the definitions are the same. */
1746 else if (hp->type == T_MACRO)
1747 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1748 /* Redefining a constant is ok with -D. */
1749 else if (hp->type == T_CONST)
1750 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1751 /* Print the warning if it's not ok. */
1752 if (!ok)
1754 U_CHAR *msg; /* what pain... */
1756 /* If we are passing through #define and #undef directives, do
1757 that for this re-definition now. */
1758 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1759 pass_thru_directive (buf, limit, pfile, keyword);
1761 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
1762 if (hp->type == T_MACRO)
1763 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1764 "this is the location of the previous definition");
1766 /* Replace the old definition. */
1767 hp->type = T_MACRO;
1768 hp->value.defn = mdef.defn;
1770 else
1772 /* If we are passing through #define and #undef directives, do
1773 that for this new definition now. */
1774 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1775 pass_thru_directive (buf, limit, pfile, keyword);
1776 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1777 (char *) mdef.defn, hashcode);
1780 return 0;
1782 nope:
1784 return 1;
1787 /* This structure represents one parsed argument in a macro call.
1788 `raw' points to the argument text as written (`raw_length' is its length).
1789 `expanded' points to the argument's macro-expansion
1790 (its length is `expand_length').
1791 `stringified_length' is the length the argument would have
1792 if stringified.
1793 `use_count' is the number of times this macro arg is substituted
1794 into the macro. If the actual use count exceeds 10,
1795 the value stored is 10. */
1797 /* raw and expanded are relative to ARG_BASE */
1798 #define ARG_BASE ((pfile)->token_buffer)
1800 struct argdata {
1801 /* Strings relative to pfile->token_buffer */
1802 long raw, expanded, stringified;
1803 int raw_length, expand_length;
1804 int stringified_length;
1805 char newlines;
1806 char use_count;
1809 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1810 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1811 as the new input buffer.
1812 Return the new buffer, or NULL on failure. */
1814 cpp_buffer *
1815 cpp_push_buffer (pfile, buffer, length)
1816 cpp_reader *pfile;
1817 U_CHAR *buffer;
1818 long length;
1820 register cpp_buffer *buf = CPP_BUFFER (pfile);
1821 if (buf == pfile->buffer_stack)
1823 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1824 buf->fname);
1825 return NULL;
1827 buf--;
1828 bzero ((char *) buf, sizeof (cpp_buffer));
1829 CPP_BUFFER (pfile) = buf;
1830 buf->if_stack = pfile->if_stack;
1831 buf->cleanup = null_cleanup;
1832 buf->underflow = null_underflow;
1833 buf->buf = buf->cur = buffer;
1834 buf->alimit = buf->rlimit = buffer + length;
1836 return buf;
1839 cpp_buffer *
1840 cpp_pop_buffer (pfile)
1841 cpp_reader *pfile;
1843 cpp_buffer *buf = CPP_BUFFER (pfile);
1844 (*buf->cleanup) (buf, pfile);
1845 return ++CPP_BUFFER (pfile);
1848 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1849 Pop the buffer when done. */
1851 void
1852 cpp_scan_buffer (pfile)
1853 cpp_reader *pfile;
1855 cpp_buffer *buffer = CPP_BUFFER (pfile);
1856 for (;;)
1858 enum cpp_token token = cpp_get_token (pfile);
1859 if (token == CPP_EOF) /* Should not happen ... */
1860 break;
1861 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1863 cpp_pop_buffer (pfile);
1864 break;
1870 * Rescan a string (which may have escape marks) into pfile's buffer.
1871 * Place the result in pfile->token_buffer.
1873 * The input is copied before it is scanned, so it is safe to pass
1874 * it something from the token_buffer that will get overwritten
1875 * (because it follows CPP_WRITTEN). This is used by do_include.
1878 static void
1879 cpp_expand_to_buffer (pfile, buf, length)
1880 cpp_reader *pfile;
1881 U_CHAR *buf;
1882 int length;
1884 register cpp_buffer *ip;
1885 cpp_buffer obuf;
1886 U_CHAR *limit = buf + length;
1887 U_CHAR *buf1;
1888 #if 0
1889 int odepth = indepth;
1890 #endif
1892 if (length < 0)
1893 abort ();
1895 /* Set up the input on the input stack. */
1897 buf1 = (U_CHAR *) alloca (length + 1);
1899 register U_CHAR *p1 = buf;
1900 register U_CHAR *p2 = buf1;
1902 while (p1 != limit)
1903 *p2++ = *p1++;
1905 buf1[length] = 0;
1907 ip = cpp_push_buffer (pfile, buf1, length);
1908 if (ip == NULL)
1909 return;
1910 ip->has_escapes = 1;
1911 #if 0
1912 ip->lineno = obuf.lineno = 1;
1913 #endif
1915 /* Scan the input, create the output. */
1916 cpp_scan_buffer (pfile);
1918 #if 0
1919 if (indepth != odepth)
1920 abort ();
1921 #endif
1923 CPP_NUL_TERMINATE (pfile);
1927 static void
1928 adjust_position (buf, limit, linep, colp)
1929 U_CHAR *buf;
1930 U_CHAR *limit;
1931 long *linep;
1932 long *colp;
1934 while (buf < limit)
1936 U_CHAR ch = *buf++;
1937 if (ch == '\n')
1938 (*linep)++, (*colp) = 1;
1939 else
1940 (*colp)++;
1944 /* Move line_base forward, updating lineno and colno. */
1946 static void
1947 update_position (pbuf)
1948 register cpp_buffer *pbuf;
1950 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
1951 unsigned char *new_pos = pbuf->cur;
1952 register struct parse_marker *mark;
1953 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
1955 if (pbuf->buf + mark->position < new_pos)
1956 new_pos = pbuf->buf + mark->position;
1958 pbuf->line_base += new_pos - old_pos;
1959 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1962 void
1963 cpp_buf_line_and_col (pbuf, linep, colp)
1964 register cpp_buffer *pbuf;
1965 long *linep, *colp;
1967 long dummy;
1968 if (colp == NULL)
1969 colp = &dummy;
1970 if (pbuf)
1972 *linep = pbuf->lineno;
1973 *colp = pbuf->colno;
1974 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
1976 else
1978 *linep = 0;
1979 *colp = 0;
1983 /* Return the cpp_buffer that corresponds to a file (not a macro). */
1985 cpp_buffer *
1986 cpp_file_buffer (pfile)
1987 cpp_reader *pfile;
1989 cpp_buffer *ip = CPP_BUFFER (pfile);
1991 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
1992 if (ip->fname != NULL)
1993 return ip;
1994 return NULL;
1997 static long
1998 count_newlines (buf, limit)
1999 register U_CHAR *buf;
2000 register U_CHAR *limit;
2002 register long count = 0;
2003 while (buf < limit)
2005 U_CHAR ch = *buf++;
2006 if (ch == '\n')
2007 count++;
2009 return count;
2013 * write out a #line command, for instance, after an #include file.
2014 * If CONDITIONAL is nonzero, we can omit the #line if it would
2015 * appear to be a no-op, and we can output a few newlines instead
2016 * if we want to increase the line number by a small amount.
2017 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2020 static void
2021 output_line_command (pfile, conditional, file_change)
2022 cpp_reader *pfile;
2023 int conditional;
2024 enum file_change_code file_change;
2026 int len;
2027 char *line_cmd_buf, *line_end;
2028 long line, col;
2029 cpp_buffer *ip = CPP_BUFFER (pfile);
2031 if (ip->fname == NULL)
2032 return;
2034 update_position (ip);
2036 if (CPP_OPTIONS (pfile)->no_line_commands
2037 || CPP_OPTIONS (pfile)->no_output)
2038 return;
2040 line = CPP_BUFFER (pfile)->lineno;
2041 col = CPP_BUFFER (pfile)->colno;
2042 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2044 if (CPP_OPTIONS (pfile)->no_line_commands)
2045 return;
2047 if (conditional) {
2048 if (line == pfile->lineno)
2049 return;
2051 /* If the inherited line number is a little too small,
2052 output some newlines instead of a #line command. */
2053 if (line > pfile->lineno && line < pfile->lineno + 8) {
2054 CPP_RESERVE (pfile, 20);
2055 while (line > pfile->lineno) {
2056 CPP_PUTC_Q (pfile, '\n');
2057 pfile->lineno++;
2059 return;
2063 #if 0
2064 /* Don't output a line number of 0 if we can help it. */
2065 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2066 && *ip->bufp == '\n') {
2067 ip->lineno++;
2068 ip->bufp++;
2070 #endif
2072 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2074 #ifdef OUTPUT_LINE_COMMANDS
2075 static char sharp_line[] = "#line ";
2076 #else
2077 static char sharp_line[] = "# ";
2078 #endif
2079 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2082 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
2083 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2085 quote_string (pfile, ip->nominal_fname);
2086 if (file_change != same_file) {
2087 CPP_PUTC_Q (pfile, ' ');
2088 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2090 /* Tell cc1 if following text comes from a system header file. */
2091 if (ip->system_header_p) {
2092 CPP_PUTC_Q (pfile, ' ');
2093 CPP_PUTC_Q (pfile, '3');
2095 #ifndef NO_IMPLICIT_EXTERN_C
2096 /* Tell cc1plus if following text should be treated as C. */
2097 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2098 CPP_PUTC_Q (pfile, ' ');
2099 CPP_PUTC_Q (pfile, '4');
2101 #endif
2102 CPP_PUTC_Q (pfile, '\n');
2103 pfile->lineno = line;
2107 * Parse a macro argument and append the info on PFILE's token_buffer.
2108 * REST_ARGS means to absorb the rest of the args.
2109 * Return nonzero to indicate a syntax error.
2112 static enum cpp_token
2113 macarg (pfile, rest_args)
2114 cpp_reader *pfile;
2115 int rest_args;
2117 int paren = 0;
2118 enum cpp_token token;
2119 long arg_start = CPP_WRITTEN (pfile);
2120 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2121 CPP_OPTIONS (pfile)->put_out_comments = 0;
2123 /* Try to parse as much of the argument as exists at this
2124 input stack level. */
2125 pfile->no_macro_expand++;
2126 for (;;)
2128 token = cpp_get_token (pfile);
2129 switch (token)
2131 case CPP_EOF:
2132 goto done;
2133 case CPP_POP:
2134 /* If we've hit end of file, it's an error (reported by caller).
2135 Ditto if it's the end of cpp_expand_to_buffer text.
2136 If we've hit end of macro, just continue. */
2137 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2138 goto done;
2139 break;
2140 case CPP_LPAREN:
2141 paren++;
2142 break;
2143 case CPP_RPAREN:
2144 if (--paren < 0)
2145 goto found;
2146 break;
2147 case CPP_COMMA:
2148 /* if we've returned to lowest level and
2149 we aren't absorbing all args */
2150 if (paren == 0 && rest_args == 0)
2151 goto found;
2152 break;
2153 found:
2154 /* Remove ',' or ')' from argument buffer. */
2155 CPP_ADJUST_WRITTEN (pfile, -1);
2156 goto done;
2157 default: ;
2161 done:
2162 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2163 pfile->no_macro_expand--;
2165 return token;
2168 /* Turn newlines to spaces in the string of length LENGTH at START,
2169 except inside of string constants.
2170 The string is copied into itself with its beginning staying fixed. */
2172 static int
2173 change_newlines (start, length)
2174 U_CHAR *start;
2175 int length;
2177 register U_CHAR *ibp;
2178 register U_CHAR *obp;
2179 register U_CHAR *limit;
2180 register int c;
2182 ibp = start;
2183 limit = start + length;
2184 obp = start;
2186 while (ibp < limit) {
2187 *obp++ = c = *ibp++;
2188 switch (c) {
2190 case '\'':
2191 case '\"':
2192 /* Notice and skip strings, so that we don't delete newlines in them. */
2194 int quotec = c;
2195 while (ibp < limit) {
2196 *obp++ = c = *ibp++;
2197 if (c == quotec)
2198 break;
2199 if (c == '\n' && quotec == '\'')
2200 break;
2203 break;
2207 return obp - start;
2211 static struct tm *
2212 timestamp (pfile)
2213 cpp_reader *pfile;
2215 if (!pfile->timebuf) {
2216 time_t t = time ((time_t *) 0);
2217 pfile->timebuf = localtime (&t);
2219 return pfile->timebuf;
2222 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2223 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2227 * expand things like __FILE__. Place the expansion into the output
2228 * buffer *without* rescanning.
2231 static void
2232 special_symbol (hp, pfile)
2233 HASHNODE *hp;
2234 cpp_reader *pfile;
2236 char *buf;
2237 int i, len;
2238 int true_indepth;
2239 cpp_buffer *ip = NULL;
2240 struct tm *timebuf;
2242 int paren = 0; /* For special `defined' keyword */
2244 #if 0
2245 if (pcp_outfile && pcp_inside_if
2246 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2247 cpp_error (pfile,
2248 "Predefined macro `%s' used inside `#if' during precompilation",
2249 hp->name);
2250 #endif
2252 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2254 if (ip == CPP_NULL_BUFFER (pfile))
2256 cpp_error (pfile, "cccp error: not in any file?!");
2257 return; /* the show must go on */
2259 if (ip->fname != NULL)
2260 break;
2263 switch (hp->type)
2265 case T_FILE:
2266 case T_BASE_FILE:
2268 char *string;
2269 if (hp->type == T_BASE_FILE)
2271 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2272 ip = CPP_PREV_BUFFER (ip);
2274 string = ip->nominal_fname;
2276 if (!string)
2277 string = "";
2278 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2279 quote_string (pfile, string);
2280 return;
2283 case T_INCLUDE_LEVEL:
2284 true_indepth = 0;
2285 ip = CPP_BUFFER (pfile);
2286 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2287 if (ip->fname != NULL)
2288 true_indepth++;
2290 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2291 sprintf (buf, "%d", true_indepth - 1);
2292 break;
2294 case T_VERSION:
2295 buf = (char *) alloca (3 + strlen (version_string));
2296 sprintf (buf, "\"%s\"", version_string);
2297 break;
2299 #ifndef NO_BUILTIN_SIZE_TYPE
2300 case T_SIZE_TYPE:
2301 buf = SIZE_TYPE;
2302 break;
2303 #endif
2305 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2306 case T_PTRDIFF_TYPE:
2307 buf = PTRDIFF_TYPE;
2308 break;
2309 #endif
2311 case T_WCHAR_TYPE:
2312 buf = CPP_WCHAR_TYPE (pfile);
2313 break;
2315 case T_USER_LABEL_PREFIX_TYPE:
2316 buf = USER_LABEL_PREFIX;
2317 break;
2319 case T_REGISTER_PREFIX_TYPE:
2320 buf = REGISTER_PREFIX;
2321 break;
2323 case T_CONST:
2324 buf = (char *) alloca (4 * sizeof (int));
2325 sprintf (buf, "%d", hp->value.ival);
2326 #ifdef STDC_0_IN_SYSTEM_HEADERS
2327 if (ip->system_header_p
2328 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2329 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2330 strcpy (buf, "0");
2331 #endif
2332 #if 0
2333 if (pcp_inside_if && pcp_outfile)
2334 /* Output a precondition for this macro use */
2335 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2336 #endif
2337 break;
2339 case T_SPECLINE:
2341 long line = ip->lineno;
2342 long col = ip->colno;
2343 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2345 buf = (char *) alloca (10);
2346 sprintf (buf, "%ld", line);
2348 break;
2350 case T_DATE:
2351 case T_TIME:
2352 buf = (char *) alloca (20);
2353 timebuf = timestamp (pfile);
2354 if (hp->type == T_DATE)
2355 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2356 timebuf->tm_mday, timebuf->tm_year + 1900);
2357 else
2358 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2359 timebuf->tm_sec);
2360 break;
2362 case T_SPEC_DEFINED:
2363 buf = " 0 "; /* Assume symbol is not defined */
2364 ip = CPP_BUFFER (pfile);
2365 SKIP_WHITE_SPACE (ip->cur);
2366 if (*ip->cur == '(')
2368 paren++;
2369 ip->cur++; /* Skip over the paren */
2370 SKIP_WHITE_SPACE (ip->cur);
2373 if (!is_idstart[*ip->cur])
2374 goto oops;
2375 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2376 goto oops;
2377 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2379 #if 0
2380 if (pcp_outfile && pcp_inside_if
2381 && (hp->type == T_CONST
2382 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2383 /* Output a precondition for this macro use. */
2384 fprintf (pcp_outfile, "#define %s\n", hp->name);
2385 #endif
2386 buf = " 1 ";
2388 #if 0
2389 else
2390 if (pcp_outfile && pcp_inside_if)
2392 /* Output a precondition for this macro use */
2393 U_CHAR *cp = ip->bufp;
2394 fprintf (pcp_outfile, "#undef ");
2395 while (is_idchar[*cp]) /* Ick! */
2396 fputc (*cp++, pcp_outfile);
2397 putc ('\n', pcp_outfile);
2399 #endif
2400 while (is_idchar[*ip->cur])
2401 ++ip->cur;
2402 SKIP_WHITE_SPACE (ip->cur);
2403 if (paren)
2405 if (*ip->cur != ')')
2406 goto oops;
2407 ++ip->cur;
2409 break;
2411 oops:
2413 cpp_error (pfile, "`defined' without an identifier");
2414 break;
2416 default:
2417 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2418 abort ();
2420 len = strlen (buf);
2421 CPP_RESERVE (pfile, len + 1);
2422 CPP_PUTS_Q (pfile, buf, len);
2423 CPP_NUL_TERMINATE_Q (pfile);
2425 return;
2428 /* Write out a #define command for the special named MACRO_NAME
2429 to PFILE's token_buffer. */
2431 static void
2432 dump_special_to_buffer (pfile, macro_name)
2433 cpp_reader *pfile;
2434 char *macro_name;
2436 static char define_directive[] = "#define ";
2437 int macro_name_length = strlen (macro_name);
2438 output_line_command (pfile, 0, same_file);
2439 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2440 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2441 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2442 CPP_PUTC_Q (pfile, ' ');
2443 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2444 CPP_PUTC (pfile, '\n');
2447 /* Initialize the built-in macros. */
2449 static void
2450 initialize_builtins (pfile)
2451 cpp_reader *pfile;
2453 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2454 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2455 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2456 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2457 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2458 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2459 #ifndef NO_BUILTIN_SIZE_TYPE
2460 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2461 #endif
2462 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2463 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2464 #endif
2465 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2466 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2467 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2468 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2469 if (!CPP_TRADITIONAL (pfile))
2470 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2471 if (CPP_OPTIONS (pfile)->objc)
2472 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2473 /* This is supplied using a -D by the compiler driver
2474 so that it is present only when truly compiling with GNU C. */
2475 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2477 if (CPP_OPTIONS (pfile)->debug_output)
2479 dump_special_to_buffer (pfile, "__BASE_FILE__");
2480 dump_special_to_buffer (pfile, "__VERSION__");
2481 #ifndef NO_BUILTIN_SIZE_TYPE
2482 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2483 #endif
2484 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2485 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2486 #endif
2487 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2488 dump_special_to_buffer (pfile, "__DATE__");
2489 dump_special_to_buffer (pfile, "__TIME__");
2490 if (!CPP_TRADITIONAL (pfile))
2491 dump_special_to_buffer (pfile, "__STDC__");
2492 if (CPP_OPTIONS (pfile)->objc)
2493 dump_special_to_buffer (pfile, "__OBJC__");
2497 /* Return 1 iff a token ending in C1 followed directly by a token C2
2498 could cause mis-tokenization. */
2500 static int
2501 unsafe_chars (c1, c2)
2502 int c1, c2;
2504 switch (c1)
2506 case '+': case '-':
2507 if (c2 == c1 || c2 == '=')
2508 return 1;
2509 goto letter;
2510 case '.':
2511 case '0': case '1': case '2': case '3': case '4':
2512 case '5': case '6': case '7': case '8': case '9':
2513 case 'e': case 'E': case 'p': case 'P':
2514 if (c2 == '-' || c2 == '+')
2515 return 1; /* could extend a pre-processing number */
2516 goto letter;
2517 case 'L':
2518 if (c2 == '\'' || c2 == '\"')
2519 return 1; /* Could turn into L"xxx" or L'xxx'. */
2520 goto letter;
2521 letter:
2522 case '_':
2523 case 'a': case 'b': case 'c': case 'd': case 'f':
2524 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2525 case 'm': case 'n': case 'o': case 'q': case 'r':
2526 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2527 case 'y': case 'z':
2528 case 'A': case 'B': case 'C': case 'D': case 'F':
2529 case 'G': case 'H': case 'I': case 'J': case 'K':
2530 case 'M': case 'N': case 'O': case 'Q': case 'R':
2531 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2532 case 'Y': case 'Z':
2533 /* We're in the middle of either a name or a pre-processing number. */
2534 return (is_idchar[c2] || c2 == '.');
2535 case '<': case '>': case '!': case '%': case '#': case ':':
2536 case '^': case '&': case '|': case '*': case '/': case '=':
2537 return (c2 == c1 || c2 == '=');
2539 return 0;
2542 /* Expand a macro call.
2543 HP points to the symbol that is the macro being called.
2544 Put the result of expansion onto the input stack
2545 so that subsequent input by our caller will use it.
2547 If macro wants arguments, caller has already verified that
2548 an argument list follows; arguments come from the input stack. */
2550 static void
2551 macroexpand (pfile, hp)
2552 cpp_reader *pfile;
2553 HASHNODE *hp;
2555 int nargs;
2556 DEFINITION *defn = hp->value.defn;
2557 register U_CHAR *xbuf;
2558 long start_line, start_column;
2559 int xbuf_len;
2560 struct argdata *args;
2561 long old_written = CPP_WRITTEN (pfile);
2562 #if 0
2563 int start_line = instack[indepth].lineno;
2564 #endif
2565 int rest_args, rest_zero;
2566 register int i;
2568 #if 0
2569 CHECK_DEPTH (return;);
2570 #endif
2572 #if 0
2573 /* This macro is being used inside a #if, which means it must be */
2574 /* recorded as a precondition. */
2575 if (pcp_inside_if && pcp_outfile && defn->predefined)
2576 dump_single_macro (hp, pcp_outfile);
2577 #endif
2579 pfile->output_escapes++;
2580 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2582 nargs = defn->nargs;
2584 if (nargs >= 0)
2586 enum cpp_token token;
2588 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2590 for (i = 0; i < nargs; i++)
2592 args[i].raw = args[i].expanded = 0;
2593 args[i].raw_length = 0;
2594 args[i].expand_length = args[i].stringified_length = -1;
2595 args[i].use_count = 0;
2598 /* Parse all the macro args that are supplied. I counts them.
2599 The first NARGS args are stored in ARGS.
2600 The rest are discarded. If rest_args is set then we assume
2601 macarg absorbed the rest of the args. */
2602 i = 0;
2603 rest_args = 0;
2604 rest_args = 0;
2605 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2608 if (rest_args)
2609 continue;
2610 if (i < nargs || (nargs == 0 && i == 0))
2612 /* if we are working on last arg which absorbs rest of args... */
2613 if (i == nargs - 1 && defn->rest_args)
2614 rest_args = 1;
2615 args[i].raw = CPP_WRITTEN (pfile);
2616 token = macarg (pfile, rest_args);
2617 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2618 args[i].newlines = 0; /* FIXME */
2620 else
2621 token = macarg (pfile, 0);
2622 if (token == CPP_EOF || token == CPP_POP)
2624 cpp_error_with_line (pfile, start_line, start_column,
2625 "unterminated macro call");
2626 return;
2628 i++;
2629 } while (token == CPP_COMMA);
2631 /* If we got one arg but it was just whitespace, call that 0 args. */
2632 if (i == 1)
2634 register U_CHAR *bp = ARG_BASE + args[0].raw;
2635 register U_CHAR *lim = bp + args[0].raw_length;
2636 /* cpp.texi says for foo ( ) we provide one argument.
2637 However, if foo wants just 0 arguments, treat this as 0. */
2638 if (nargs == 0)
2639 while (bp != lim && is_space[*bp]) bp++;
2640 if (bp == lim)
2641 i = 0;
2644 /* Don't output an error message if we have already output one for
2645 a parse error above. */
2646 rest_zero = 0;
2647 if (nargs == 0 && i > 0)
2649 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2651 else if (i < nargs)
2653 /* traditional C allows foo() if foo wants one argument. */
2654 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2656 /* the rest args token is allowed to absorb 0 tokens */
2657 else if (i == nargs - 1 && defn->rest_args)
2658 rest_zero = 1;
2659 else if (i == 0)
2660 cpp_error (pfile, "macro `%s' used without args", hp->name);
2661 else if (i == 1)
2662 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2663 else
2664 cpp_error (pfile, "macro `%s' used with only %d args",
2665 hp->name, i);
2667 else if (i > nargs)
2669 cpp_error (pfile,
2670 "macro `%s' used with too many (%d) args", hp->name, i);
2674 /* If macro wants zero args, we parsed the arglist for checking only.
2675 Read directly from the macro definition. */
2676 if (nargs <= 0)
2678 xbuf = defn->expansion;
2679 xbuf_len = defn->length;
2681 else
2683 register U_CHAR *exp = defn->expansion;
2684 register int offset; /* offset in expansion,
2685 copied a piece at a time */
2686 register int totlen; /* total amount of exp buffer filled so far */
2688 register struct reflist *ap, *last_ap;
2690 /* Macro really takes args. Compute the expansion of this call. */
2692 /* Compute length in characters of the macro's expansion.
2693 Also count number of times each arg is used. */
2694 xbuf_len = defn->length;
2695 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2697 if (ap->stringify)
2699 register struct argdata *arg = &args[ap->argno];
2700 /* Stringify it it hasn't already been */
2701 if (arg->stringified_length < 0)
2703 int arglen = arg->raw_length;
2704 int escaped = 0;
2705 int in_string = 0;
2706 int c;
2707 /* Initially need_space is -1. Otherwise, 1 means the
2708 previous character was a space, but we suppressed it;
2709 0 means the previous character was a non-space. */
2710 int need_space = -1;
2711 i = 0;
2712 arg->stringified = CPP_WRITTEN (pfile);
2713 if (!CPP_TRADITIONAL (pfile))
2714 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2715 for (; i < arglen; i++)
2717 c = (ARG_BASE + arg->raw)[i];
2719 if (! in_string)
2721 /* Internal sequences of whitespace are replaced by
2722 one space except within an string or char token.*/
2723 if (is_space[c])
2725 if (CPP_WRITTEN (pfile) > arg->stringified
2726 && (CPP_PWRITTEN (pfile))[-1] == '@')
2728 /* "@ " escape markers are removed */
2729 CPP_ADJUST_WRITTEN (pfile, -1);
2730 continue;
2732 if (need_space == 0)
2733 need_space = 1;
2734 continue;
2736 else if (need_space > 0)
2737 CPP_PUTC (pfile, ' ');
2738 need_space = 0;
2741 if (escaped)
2742 escaped = 0;
2743 else
2745 if (c == '\\')
2746 escaped = 1;
2747 if (in_string)
2749 if (c == in_string)
2750 in_string = 0;
2752 else if (c == '\"' || c == '\'')
2753 in_string = c;
2756 /* Escape these chars */
2757 if (c == '\"' || (in_string && c == '\\'))
2758 CPP_PUTC (pfile, '\\');
2759 if (isprint (c))
2760 CPP_PUTC (pfile, c);
2761 else
2763 CPP_RESERVE (pfile, 4);
2764 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2765 (unsigned int) c);
2766 CPP_ADJUST_WRITTEN (pfile, 4);
2769 if (!CPP_TRADITIONAL (pfile))
2770 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2771 arg->stringified_length
2772 = CPP_WRITTEN (pfile) - arg->stringified;
2774 xbuf_len += args[ap->argno].stringified_length;
2776 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2777 /* Add 4 for two newline-space markers to prevent
2778 token concatenation. */
2779 xbuf_len += args[ap->argno].raw_length + 4;
2780 else
2782 /* We have an ordinary (expanded) occurrence of the arg.
2783 So compute its expansion, if we have not already. */
2784 if (args[ap->argno].expand_length < 0)
2786 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2787 cpp_expand_to_buffer (pfile,
2788 ARG_BASE + args[ap->argno].raw,
2789 args[ap->argno].raw_length);
2791 args[ap->argno].expand_length
2792 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2795 /* Add 4 for two newline-space markers to prevent
2796 token concatenation. */
2797 xbuf_len += args[ap->argno].expand_length + 4;
2799 if (args[ap->argno].use_count < 10)
2800 args[ap->argno].use_count++;
2803 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2805 /* Generate in XBUF the complete expansion
2806 with arguments substituted in.
2807 TOTLEN is the total size generated so far.
2808 OFFSET is the index in the definition
2809 of where we are copying from. */
2810 offset = totlen = 0;
2811 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2812 last_ap = ap, ap = ap->next)
2814 register struct argdata *arg = &args[ap->argno];
2815 int count_before = totlen;
2817 /* Add chars to XBUF. */
2818 for (i = 0; i < ap->nchars; i++, offset++)
2819 xbuf[totlen++] = exp[offset];
2821 /* If followed by an empty rest arg with concatenation,
2822 delete the last run of nonwhite chars. */
2823 if (rest_zero && totlen > count_before
2824 && ((ap->rest_args && ap->raw_before)
2825 || (last_ap != NULL && last_ap->rest_args
2826 && last_ap->raw_after)))
2828 /* Delete final whitespace. */
2829 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2830 totlen--;
2832 /* Delete the nonwhites before them. */
2833 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2834 totlen--;
2837 if (ap->stringify != 0)
2839 bcopy (ARG_BASE + arg->stringified,
2840 xbuf + totlen, arg->stringified_length);
2841 totlen += arg->stringified_length;
2843 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2845 U_CHAR *p1 = ARG_BASE + arg->raw;
2846 U_CHAR *l1 = p1 + arg->raw_length;
2847 if (ap->raw_before)
2849 while (p1 != l1 && is_space[*p1]) p1++;
2850 while (p1 != l1 && is_idchar[*p1])
2851 xbuf[totlen++] = *p1++;
2852 /* Delete any no-reexpansion marker that follows
2853 an identifier at the beginning of the argument
2854 if the argument is concatenated with what precedes it. */
2855 if (p1[0] == '@' && p1[1] == '-')
2856 p1 += 2;
2858 if (ap->raw_after)
2860 /* Arg is concatenated after: delete trailing whitespace,
2861 whitespace markers, and no-reexpansion markers. */
2862 while (p1 != l1)
2864 if (is_space[l1[-1]]) l1--;
2865 else if (l1[-1] == '-')
2867 U_CHAR *p2 = l1 - 1;
2868 /* If a `-' is preceded by an odd number of newlines then it
2869 and the last newline are a no-reexpansion marker. */
2870 while (p2 != p1 && p2[-1] == '\n') p2--;
2871 if ((l1 - 1 - p2) & 1) {
2872 l1 -= 2;
2874 else break;
2876 else break;
2880 bcopy (p1, xbuf + totlen, l1 - p1);
2881 totlen += l1 - p1;
2883 else
2885 U_CHAR *expanded = ARG_BASE + arg->expanded;
2886 if (!ap->raw_before && totlen > 0 && arg->expand_length
2887 && !CPP_TRADITIONAL(pfile)
2888 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2890 xbuf[totlen++] = '@';
2891 xbuf[totlen++] = ' ';
2894 bcopy (expanded, xbuf + totlen, arg->expand_length);
2895 totlen += arg->expand_length;
2897 if (!ap->raw_after && totlen > 0 && offset < defn->length
2898 && !CPP_TRADITIONAL(pfile)
2899 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2901 xbuf[totlen++] = '@';
2902 xbuf[totlen++] = ' ';
2905 /* If a macro argument with newlines is used multiple times,
2906 then only expand the newlines once. This avoids creating
2907 output lines which don't correspond to any input line,
2908 which confuses gdb and gcov. */
2909 if (arg->use_count > 1 && arg->newlines > 0)
2911 /* Don't bother doing change_newlines for subsequent
2912 uses of arg. */
2913 arg->use_count = 1;
2914 arg->expand_length
2915 = change_newlines (expanded, arg->expand_length);
2919 if (totlen > xbuf_len)
2920 abort ();
2923 /* if there is anything left of the definition
2924 after handling the arg list, copy that in too. */
2926 for (i = offset; i < defn->length; i++)
2928 /* if we've reached the end of the macro */
2929 if (exp[i] == ')')
2930 rest_zero = 0;
2931 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2932 && last_ap->raw_after))
2933 xbuf[totlen++] = exp[i];
2936 xbuf[totlen] = 0;
2937 xbuf_len = totlen;
2941 pfile->output_escapes--;
2943 /* Now put the expansion on the input stack
2944 so our caller will commence reading from it. */
2945 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2946 CPP_BUFFER (pfile)->has_escapes = 1;
2948 /* Pop the space we've used in the token_buffer for argument expansion. */
2949 CPP_SET_WRITTEN (pfile, old_written);
2951 /* Recursive macro use sometimes works traditionally.
2952 #define foo(x,y) bar (x (y,0), y)
2953 foo (foo, baz) */
2955 if (!CPP_TRADITIONAL (pfile))
2956 hp->type = T_DISABLED;
2959 static void
2960 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2961 cpp_reader *pfile;
2962 register U_CHAR *xbuf;
2963 int xbuf_len;
2964 HASHNODE *hp;
2966 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2967 if (mbuf == NULL)
2968 return;
2969 mbuf->cleanup = macro_cleanup;
2970 mbuf->data = hp;
2972 /* The first chars of the expansion should be a "@ " added by
2973 collect_expansion. This is to prevent accidental token-pasting
2974 between the text preceding the macro invocation, and the macro
2975 expansion text.
2977 We would like to avoid adding unneeded spaces (for the sake of
2978 tools that use cpp, such as imake). In some common cases we can
2979 tell that it is safe to omit the space.
2981 The character before the macro invocation cannot have been an
2982 idchar (or else it would have been pasted with the idchars of
2983 the macro name). Therefore, if the first non-space character
2984 of the expansion is an idchar, we do not need the extra space
2985 to prevent token pasting.
2987 Also, we don't need the extra space if the first char is '(',
2988 or some other (less common) characters. */
2990 if (xbuf[0] == '@' && xbuf[1] == ' '
2991 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
2992 || xbuf[2] == '\"'))
2993 mbuf->cur += 2;
2996 /* Like cpp_get_token, except that it does not read past end-of-line.
2997 Also, horizontal space is skipped, and macros are popped. */
2999 static enum cpp_token
3000 get_directive_token (pfile)
3001 cpp_reader *pfile;
3003 for (;;)
3005 long old_written = CPP_WRITTEN (pfile);
3006 enum cpp_token token;
3007 cpp_skip_hspace (pfile);
3008 if (PEEKC () == '\n')
3009 return CPP_VSPACE;
3010 token = cpp_get_token (pfile);
3011 switch (token)
3013 case CPP_POP:
3014 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3015 return token;
3016 /* ... else fall though ... */
3017 case CPP_HSPACE: case CPP_COMMENT:
3018 CPP_SET_WRITTEN (pfile, old_written);
3019 break;
3020 default:
3021 return token;
3026 /* Handle #include and #import.
3027 This function expects to see "fname" or <fname> on the input.
3029 The input is normally in part of the output_buffer following
3030 CPP_WRITTEN, and will get overwritten by output_line_command.
3031 I.e. in input file specification has been popped by handle_directive.
3032 This is safe. */
3034 static int
3035 do_include (pfile, keyword, unused1, unused2)
3036 cpp_reader *pfile;
3037 struct directive *keyword;
3038 U_CHAR *unused1, *unused2;
3040 int importing = (keyword->type == T_IMPORT);
3041 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3042 char *fname; /* Dynamically allocated fname buffer */
3043 char *pcftry;
3044 char *pcfname;
3045 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3046 enum cpp_token token;
3048 /* Chain of dirs to search */
3049 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3050 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3051 struct file_name_list *searchptr = 0;
3052 long old_written = CPP_WRITTEN (pfile);
3054 int flen;
3056 int f; /* file number */
3058 int retried = 0; /* Have already tried macro
3059 expanding the include line */
3060 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3061 int pcf = -1;
3062 char *pcfbuf;
3063 char *pcfbuflimit;
3064 int pcfnum;
3065 f= -1; /* JF we iz paranoid! */
3067 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3069 if (importing)
3070 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
3071 if (skip_dirs)
3072 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
3075 if (importing && CPP_OPTIONS (pfile)->warn_import
3076 && !CPP_OPTIONS (pfile)->inhibit_warnings
3077 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3079 pfile->import_warning = 1;
3080 cpp_warning (pfile, "using `#import' is not recommended");
3081 cpp_notice ("The fact that a certain header file need not be processed more than once\n\
3082 should be indicated in the header file, not where it is used.\n\
3083 The best way to do this is with a conditional of this form:\n\
3085 #ifndef _FOO_H_INCLUDED\n\
3086 #define _FOO_H_INCLUDED\n\
3087 ... <real contents of file> ...\n\
3088 #endif /* Not _FOO_H_INCLUDED */\n\
3090 Then users can use `#include' any number of times.\n\
3091 GNU C automatically avoids processing the file more than once\n\
3092 when it is equipped with such a conditional.\n");
3095 pfile->parsing_include_directive++;
3096 token = get_directive_token (pfile);
3097 pfile->parsing_include_directive--;
3099 if (token == CPP_STRING)
3101 /* FIXME - check no trailing garbage */
3102 fbeg = pfile->token_buffer + old_written + 1;
3103 fend = CPP_PWRITTEN (pfile) - 1;
3104 if (fbeg[-1] == '<')
3106 angle_brackets = 1;
3107 /* If -I-, start with the first -I dir after the -I-. */
3108 if (CPP_OPTIONS (pfile)->first_bracket_include)
3109 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3111 /* If -I- was specified, don't search current dir, only spec'd ones. */
3112 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3114 cpp_buffer *fp = CPP_BUFFER (pfile);
3115 /* We have "filename". Figure out directory this source
3116 file is coming from and put it on the front of the list. */
3118 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3120 int n;
3121 char *ep,*nam;
3123 if ((nam = fp->nominal_fname) != NULL)
3125 /* Found a named file. Figure out dir of the file,
3126 and put it in front of the search list. */
3127 dsp[0].next = search_start;
3128 search_start = dsp;
3129 #ifndef VMS
3130 ep = rindex (nam, '/');
3131 #else /* VMS */
3132 ep = rindex (nam, ']');
3133 if (ep == NULL) ep = rindex (nam, '>');
3134 if (ep == NULL) ep = rindex (nam, ':');
3135 if (ep != NULL) ep++;
3136 #endif /* VMS */
3137 if (ep != NULL)
3139 n = ep - nam;
3140 dsp[0].fname = (char *) alloca (n + 1);
3141 strncpy (dsp[0].fname, nam, n);
3142 dsp[0].fname[n] = '\0';
3143 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3144 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3146 else
3148 dsp[0].fname = 0; /* Current directory */
3150 dsp[0].got_name_map = 0;
3151 break;
3156 #ifdef VMS
3157 else if (token == CPP_NAME)
3160 * Support '#include xyz' like VAX-C to allow for easy use of all the
3161 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3162 * code from case '<' is repeated here) and generates a warning.
3164 cpp_warning (pfile,
3165 "VAX-C-style include specification found, use '#include <filename.h>' !");
3166 angle_brackets = 1;
3167 /* If -I-, start with the first -I dir after the -I-. */
3168 if (CPP_OPTIONS (pfile)->first_bracket_include)
3169 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3170 fbeg = pfile->token_buffer + old_written;
3171 fend = CPP_PWRITTEN (pfile);
3173 #endif
3174 else
3176 cpp_error (pfile,
3177 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3178 CPP_SET_WRITTEN (pfile, old_written);
3179 skip_rest_of_line (pfile);
3180 return 0;
3183 *fend = 0;
3185 token = get_directive_token (pfile);
3186 if (token != CPP_VSPACE)
3188 cpp_error (pfile, "junk at end of `#include'");
3189 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3190 token = get_directive_token (pfile);
3193 /* For #include_next, skip in the search path
3194 past the dir in which the containing file was found. */
3195 if (skip_dirs)
3197 cpp_buffer *fp = CPP_BUFFER (pfile);
3198 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3199 if (fp->fname != NULL)
3201 /* fp->dir is null if the containing file was specified with
3202 an absolute file name. In that case, don't skip anything. */
3203 if (fp->dir == SELF_DIR_DUMMY)
3204 search_start = CPP_OPTIONS (pfile)->include;
3205 else if (fp->dir)
3206 search_start = fp->dir->next;
3207 break;
3211 CPP_SET_WRITTEN (pfile, old_written);
3213 flen = fend - fbeg;
3215 if (flen == 0)
3217 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3218 return 0;
3221 /* Allocate this permanently, because it gets stored in the definitions
3222 of macros. */
3223 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3224 /* + 2 above for slash and terminating null. */
3225 /* + 2 added for '.h' on VMS (to support '#include filename') */
3227 /* If specified file name is absolute, just open it. */
3229 if (*fbeg == '/') {
3230 strncpy (fname, fbeg, flen);
3231 fname[flen] = 0;
3232 if (redundant_include_p (pfile, fname))
3233 return 0;
3234 if (importing)
3235 f = lookup_import (pfile, fname, NULL_PTR);
3236 else
3237 f = open_include_file (pfile, fname, NULL_PTR);
3238 if (f == -2)
3239 return 0; /* Already included this file */
3240 } else {
3241 /* Search directory path, trying to open the file.
3242 Copy each filename tried into FNAME. */
3244 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3245 if (searchptr->fname) {
3246 /* The empty string in a search path is ignored.
3247 This makes it possible to turn off entirely
3248 a standard piece of the list. */
3249 if (searchptr->fname[0] == 0)
3250 continue;
3251 strcpy (fname, searchptr->fname);
3252 strcat (fname, "/");
3253 fname[strlen (fname) + flen] = 0;
3254 } else {
3255 fname[0] = 0;
3257 strncat (fname, fbeg, flen);
3258 #ifdef VMS
3259 /* Change this 1/2 Unix 1/2 VMS file specification into a
3260 full VMS file specification */
3261 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3262 /* Fix up the filename */
3263 hack_vms_include_specification (fname);
3264 } else {
3265 /* This is a normal VMS filespec, so use it unchanged. */
3266 strncpy (fname, fbeg, flen);
3267 fname[flen] = 0;
3268 /* if it's '#include filename', add the missing .h */
3269 if (index(fname,'.')==NULL) {
3270 strcat (fname, ".h");
3273 #endif /* VMS */
3274 /* ??? There are currently 3 separate mechanisms for avoiding processing
3275 of redundant include files: #import, #pragma once, and
3276 redundant_include_p. It would be nice if they were unified. */
3277 if (redundant_include_p (pfile, fname))
3278 return 0;
3279 if (importing)
3280 f = lookup_import (pfile, fname, searchptr);
3281 else
3282 f = open_include_file (pfile, fname, searchptr);
3283 if (f == -2)
3284 return 0; /* Already included this file */
3285 #ifdef EACCES
3286 else if (f == -1 && errno == EACCES)
3287 cpp_warning (pfile, "Header file %s exists, but is not readable",
3288 fname);
3289 #endif
3290 if (f >= 0)
3291 break;
3295 if (f < 0)
3297 /* A file that was not found. */
3298 strncpy (fname, fbeg, flen);
3299 fname[flen] = 0;
3300 /* If generating dependencies and -MG was specified, we assume missing
3301 files are leaf files, living in the same directory as the source file
3302 or other similar place; these missing files may be generated from
3303 other files and may not exist yet (eg: y.tab.h). */
3305 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3306 && CPP_PRINT_DEPS (pfile)
3307 > (angle_brackets || (pfile->system_include_depth > 0)))
3309 /* If it was requested as a system header file,
3310 then assume it belongs in the first place to look for such. */
3311 if (angle_brackets)
3313 for (searchptr = search_start; searchptr;
3314 searchptr = searchptr->next)
3316 if (searchptr->fname)
3318 char *p;
3320 if (searchptr->fname[0] == 0)
3321 continue;
3322 p = (char *) alloca (strlen (searchptr->fname)
3323 + strlen (fname) + 2);
3324 strcpy (p, searchptr->fname);
3325 strcat (p, "/");
3326 strcat (p, fname);
3327 deps_output (pfile, p, ' ');
3328 break;
3332 else
3334 /* Otherwise, omit the directory, as if the file existed
3335 in the directory with the source. */
3336 deps_output (pfile, fname, ' ');
3339 /* If -M was specified, and this header file won't be added to the
3340 dependency list, then don't count this as an error, because we can
3341 still produce correct output. Otherwise, we can't produce correct
3342 output, because there may be dependencies we need inside the missing
3343 file, and we don't know what directory this missing file exists in.*/
3344 else if (CPP_PRINT_DEPS (pfile)
3345 && (CPP_PRINT_DEPS (pfile)
3346 <= (angle_brackets || (pfile->system_include_depth > 0))))
3347 cpp_warning (pfile, "No include path in which to find %s", fname);
3348 else if (search_start)
3349 cpp_error_from_errno (pfile, fname);
3350 else
3351 cpp_error (pfile, "No include path in which to find %s", fname);
3353 else {
3354 /* Check to see if this include file is a once-only include file.
3355 If so, give up. */
3357 struct file_name_list *ptr;
3359 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3360 if (!strcmp (ptr->fname, fname)) {
3361 close (f);
3362 return 0; /* This file was once'd. */
3366 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3367 if (!strcmp (ptr->fname, fname))
3368 break; /* This file was included before. */
3371 if (ptr == 0) {
3372 /* This is the first time for this file. */
3373 /* Add it to list of files included. */
3375 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3376 ptr->control_macro = 0;
3377 ptr->c_system_include_path = 0;
3378 ptr->next = pfile->all_include_files;
3379 pfile->all_include_files = ptr;
3380 ptr->fname = savestring (fname);
3381 ptr->got_name_map = 0;
3383 /* For -M, add this file to the dependencies. */
3384 if (CPP_PRINT_DEPS (pfile)
3385 > (angle_brackets || (pfile->system_include_depth > 0)))
3386 deps_output (pfile, fname, ' ');
3389 /* Handle -H option. */
3390 if (CPP_OPTIONS(pfile)->print_include_names)
3392 cpp_buffer *buf = CPP_BUFFER (pfile);
3393 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3394 putc ('.', stderr);
3395 fprintf (stderr, "%s\n", fname);
3398 if (angle_brackets)
3399 pfile->system_include_depth++;
3401 /* Actually process the file. */
3403 /* Record file on "seen" list for #import. */
3404 add_import (pfile, f, fname);
3406 pcftry = (char *) alloca (strlen (fname) + 30);
3407 pcfbuf = 0;
3408 pcfnum = 0;
3410 #if 0
3411 if (!no_precomp)
3413 struct stat stat_f;
3415 fstat (f, &stat_f);
3417 do {
3418 sprintf (pcftry, "%s%d", fname, pcfnum++);
3420 pcf = open (pcftry, O_RDONLY, 0666);
3421 if (pcf != -1)
3423 struct stat s;
3425 fstat (pcf, &s);
3426 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3427 sizeof (s.st_ino))
3428 || stat_f.st_dev != s.st_dev)
3430 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3431 /* Don't need it any more. */
3432 close (pcf);
3434 else
3436 /* Don't need it at all. */
3437 close (pcf);
3438 break;
3441 } while (pcf != -1 && !pcfbuf);
3443 #endif
3445 /* Actually process the file */
3446 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3447 return 0;
3448 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3449 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3451 output_line_command (pfile, 0, enter_file);
3452 pfile->only_seen_white = 2;
3455 if (angle_brackets)
3456 pfile->system_include_depth--;
3458 return 0;
3461 /* Return nonzero if there is no need to include file NAME
3462 because it has already been included and it contains a conditional
3463 to make a repeated include do nothing. */
3465 static int
3466 redundant_include_p (pfile, name)
3467 cpp_reader *pfile;
3468 char *name;
3470 struct file_name_list *l = pfile->all_include_files;
3471 for (; l; l = l->next)
3472 if (! strcmp (name, l->fname)
3473 && l->control_macro
3474 && cpp_lookup (pfile, l->control_macro, -1, -1))
3475 return 1;
3476 return 0;
3479 /* Return nonzero if the given FILENAME is an absolute pathname which
3480 designates a file within one of the known "system" include file
3481 directories. We assume here that if the given FILENAME looks like
3482 it is the name of a file which resides either directly in a "system"
3483 include file directory, or within any subdirectory thereof, then the
3484 given file must be a "system" include file. This function tells us
3485 if we should suppress pedantic errors/warnings for the given FILENAME.
3487 The value is 2 if the file is a C-language system header file
3488 for which C++ should (on most systems) assume `extern "C"'. */
3490 static int
3491 is_system_include (pfile, filename)
3492 cpp_reader *pfile;
3493 register char *filename;
3495 struct file_name_list *searchptr;
3497 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3498 searchptr = searchptr->next)
3499 if (searchptr->fname) {
3500 register char *sys_dir = searchptr->fname;
3501 register unsigned length = strlen (sys_dir);
3503 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3505 if (searchptr->c_system_include_path)
3506 return 2;
3507 else
3508 return 1;
3511 return 0;
3516 * Install a name in the assertion hash table.
3518 * If LEN is >= 0, it is the length of the name.
3519 * Otherwise, compute the length by scanning the entire name.
3521 * If HASH is >= 0, it is the precomputed hash code.
3522 * Otherwise, compute the hash code.
3525 static ASSERTION_HASHNODE *
3526 assertion_install (pfile, name, len, hash)
3527 cpp_reader *pfile;
3528 U_CHAR *name;
3529 int len;
3530 int hash;
3532 register ASSERTION_HASHNODE *hp;
3533 register int i, bucket;
3534 register U_CHAR *p, *q;
3536 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3537 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3538 bucket = hash;
3539 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3540 hp->next = pfile->assertion_hashtab[bucket];
3541 pfile->assertion_hashtab[bucket] = hp;
3542 hp->prev = NULL;
3543 if (hp->next != NULL)
3544 hp->next->prev = hp;
3545 hp->length = len;
3546 hp->value = 0;
3547 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3548 p = hp->name;
3549 q = name;
3550 for (i = 0; i < len; i++)
3551 *p++ = *q++;
3552 hp->name[len] = 0;
3553 return hp;
3556 * find the most recent hash node for name name (ending with first
3557 * non-identifier char) installed by install
3559 * If LEN is >= 0, it is the length of the name.
3560 * Otherwise, compute the length by scanning the entire name.
3562 * If HASH is >= 0, it is the precomputed hash code.
3563 * Otherwise, compute the hash code.
3566 static ASSERTION_HASHNODE *
3567 assertion_lookup (pfile, name, len, hash)
3568 cpp_reader *pfile;
3569 U_CHAR *name;
3570 int len;
3571 int hash;
3573 register ASSERTION_HASHNODE *bucket;
3575 bucket = pfile->assertion_hashtab[hash];
3576 while (bucket) {
3577 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3578 return bucket;
3579 bucket = bucket->next;
3581 return NULL;
3584 static void
3585 delete_assertion (hp)
3586 ASSERTION_HASHNODE *hp;
3588 struct tokenlist_list *tail;
3589 if (hp->prev != NULL)
3590 hp->prev->next = hp->next;
3591 if (hp->next != NULL)
3592 hp->next->prev = hp->prev;
3594 for (tail = hp->value; tail; )
3596 struct tokenlist_list *next = tail->next;
3597 free_token_list (tail->tokens);
3598 free (tail);
3599 tail = next;
3602 /* Make sure that the bucket chain header that
3603 the deleted guy was on points to the right thing afterwards. */
3604 if (hp == *hp->bucket_hdr)
3605 *hp->bucket_hdr = hp->next;
3607 free (hp);
3610 /* Convert a character string literal into a nul-terminated string.
3611 The input string is [IN ... LIMIT).
3612 The result is placed in RESULT. RESULT can be the same as IN.
3613 The value returned in the end of the string written to RESULT,
3614 or NULL on error. */
3616 static U_CHAR *
3617 convert_string (pfile, result, in, limit, handle_escapes)
3618 cpp_reader *pfile;
3619 register U_CHAR *result, *in, *limit;
3620 int handle_escapes;
3622 U_CHAR c;
3623 c = *in++;
3624 if (c != '\"')
3625 return NULL;
3626 while (in < limit)
3628 U_CHAR c = *in++;
3629 switch (c)
3631 case '\0':
3632 return NULL;
3633 case '\"':
3634 limit = in;
3635 break;
3636 case '\\':
3637 if (handle_escapes)
3639 char *bpc = (char *) in;
3640 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3641 in = (U_CHAR *) bpc;
3642 if (i >= 0)
3643 *result++ = (U_CHAR)c;
3644 break;
3646 /* else fall through */
3647 default:
3648 *result++ = c;
3651 *result = 0;
3652 return result;
3656 * interpret #line command. Remembers previously seen fnames
3657 * in its very own hash table.
3659 #define FNAME_HASHSIZE 37
3661 static int
3662 do_line (pfile, keyword)
3663 cpp_reader *pfile;
3664 struct directive *keyword;
3666 cpp_buffer *ip = CPP_BUFFER (pfile);
3667 int new_lineno;
3668 long old_written = CPP_WRITTEN (pfile);
3669 enum file_change_code file_change = same_file;
3670 enum cpp_token token;
3671 int i;
3673 token = get_directive_token (pfile);
3675 if (token != CPP_NUMBER
3676 || !isdigit(pfile->token_buffer[old_written]))
3678 cpp_error (pfile, "invalid format `#line' command");
3679 goto bad_line_directive;
3682 /* The Newline at the end of this line remains to be processed.
3683 To put the next line at the specified line number,
3684 we must store a line number now that is one less. */
3685 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3686 CPP_SET_WRITTEN (pfile, old_written);
3688 /* NEW_LINENO is one less than the actual line number here. */
3689 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3690 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3692 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3693 if (PEEKC() && !is_space[PEEKC()]) {
3694 cpp_error (pfile, "invalid format `#line' command");
3695 goto bad_line_directive;
3697 #endif
3699 token = get_directive_token (pfile);
3701 if (token == CPP_STRING) {
3702 U_CHAR *fname = pfile->token_buffer + old_written;
3703 U_CHAR *end_name;
3704 static HASHNODE *fname_table[FNAME_HASHSIZE];
3705 HASHNODE *hp, **hash_bucket;
3706 U_CHAR *p;
3707 long num_start;
3708 int fname_length;
3710 /* Turn the file name, which is a character string literal,
3711 into a null-terminated string. Do this in place. */
3712 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3713 if (end_name == NULL)
3715 cpp_error (pfile, "invalid format `#line' command");
3716 goto bad_line_directive;
3719 fname_length = end_name - fname;
3721 num_start = CPP_WRITTEN (pfile);
3722 token = get_directive_token (pfile);
3723 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3724 p = pfile->token_buffer + num_start;
3725 if (CPP_PEDANTIC (pfile))
3726 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3728 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3730 cpp_error (pfile, "invalid format `#line' command");
3731 goto bad_line_directive;
3733 if (*p == '1')
3734 file_change = enter_file;
3735 else if (*p == 2)
3736 file_change = leave_file;
3737 else if (*p == 3)
3738 ip->system_header_p = 1;
3739 else /* if (*p == 4) */
3740 ip->system_header_p = 2;
3742 CPP_SET_WRITTEN (pfile, num_start);
3743 token = get_directive_token (pfile);
3744 p = pfile->token_buffer + num_start;
3745 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3746 ip->system_header_p = *p == 3 ? 1 : 2;
3747 token = get_directive_token (pfile);
3749 if (token != CPP_VSPACE) {
3750 cpp_error (pfile, "invalid format `#line' command");
3751 goto bad_line_directive;
3755 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3756 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3757 if (hp->length == fname_length
3758 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3759 ip->nominal_fname = hp->value.cpval;
3760 break;
3762 if (hp == 0) {
3763 /* Didn't find it; cons up a new one. */
3764 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3765 hp->next = *hash_bucket;
3766 *hash_bucket = hp;
3768 hp->length = fname_length;
3769 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3770 bcopy (fname, hp->value.cpval, fname_length);
3773 else if (token != CPP_VSPACE && token != CPP_EOF) {
3774 cpp_error (pfile, "invalid format `#line' command");
3775 goto bad_line_directive;
3778 ip->lineno = new_lineno;
3779 bad_line_directive:
3780 skip_rest_of_line (pfile);
3781 CPP_SET_WRITTEN (pfile, old_written);
3782 output_line_command (pfile, 0, file_change);
3783 return 0;
3787 * remove the definition of a symbol from the symbol table.
3788 * according to un*x /lib/cpp, it is not an error to undef
3789 * something that has no definitions, so it isn't one here either.
3792 static int
3793 do_undef (pfile, keyword, buf, limit)
3794 cpp_reader *pfile;
3795 struct directive *keyword;
3796 U_CHAR *buf, *limit;
3798 int sym_length;
3799 HASHNODE *hp;
3800 U_CHAR *orig_buf = buf;
3802 #if 0
3803 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3804 if (pcp_outfile && keyword)
3805 pass_thru_directive (buf, limit, pfile, keyword);
3806 #endif
3808 SKIP_WHITE_SPACE (buf);
3809 sym_length = check_macro_name (pfile, buf, 0);
3811 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3813 /* If we are generating additional info for debugging (with -g) we
3814 need to pass through all effective #undef commands. */
3815 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3816 pass_thru_directive (orig_buf, limit, pfile, keyword);
3817 if (hp->type != T_MACRO)
3818 cpp_warning (pfile, "undefining `%s'", hp->name);
3819 delete_macro (hp);
3822 if (CPP_PEDANTIC (pfile)) {
3823 buf += sym_length;
3824 SKIP_WHITE_SPACE (buf);
3825 if (buf != limit)
3826 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3828 return 0;
3832 * Report an error detected by the program we are processing.
3833 * Use the text of the line in the error message.
3834 * (We use error because it prints the filename & line#.)
3837 static int
3838 do_error (pfile, keyword, buf, limit)
3839 cpp_reader *pfile;
3840 struct directive *keyword;
3841 U_CHAR *buf, *limit;
3843 int length = limit - buf;
3844 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3845 bcopy (buf, copy, length);
3846 copy[length] = 0;
3847 SKIP_WHITE_SPACE (copy);
3848 cpp_error (pfile, "#error %s", copy);
3849 return 0;
3853 * Report a warning detected by the program we are processing.
3854 * Use the text of the line in the warning message, then continue.
3855 * (We use error because it prints the filename & line#.)
3858 static int
3859 do_warning (pfile, keyword, buf, limit)
3860 cpp_reader *pfile;
3861 struct directive *keyword;
3862 U_CHAR *buf, *limit;
3864 int length = limit - buf;
3865 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3866 bcopy (buf, copy, length);
3867 copy[length] = 0;
3868 SKIP_WHITE_SPACE (copy);
3870 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3871 cpp_pedwarn ("ANSI C does not allow `#warning'");
3873 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3874 if -pedantic-errors is given, #warning should cause an error. */
3875 cpp_pedwarn (pfile, "#warning %s", copy);
3876 return 0;
3879 /* Remember the name of the current file being read from so that we can
3880 avoid ever including it again. */
3882 static int
3883 do_once (pfile)
3884 cpp_reader *pfile;
3886 cpp_buffer *ip = NULL;
3887 struct file_name_list *new;
3889 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3891 if (ip == CPP_NULL_BUFFER (pfile))
3892 return 0;
3893 if (ip->fname != NULL)
3894 break;
3898 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3899 new->next = pfile->dont_repeat_files;
3900 pfile->dont_repeat_files = new;
3901 new->fname = savestring (ip->fname);
3902 new->control_macro = 0;
3903 new->got_name_map = 0;
3904 new->c_system_include_path = 0;
3906 return 0;
3909 /* Report program identification. */
3911 static int
3912 do_ident (pfile, keyword, buf, limit)
3913 cpp_reader *pfile;
3914 struct directive *keyword;
3915 U_CHAR *buf, *limit;
3917 /* long old_written = CPP_WRITTEN (pfile);*/
3918 int len;
3920 /* Allow #ident in system headers, since that's not user's fault. */
3921 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3922 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3924 /* Leave rest of line to be read by later calls to cpp_get_token. */
3926 return 0;
3929 /* #pragma and its argument line have already been copied to the output file.
3930 Just check for some recognized pragmas that need validation here. */
3932 static int
3933 do_pragma (pfile, keyword, buf, limit)
3934 cpp_reader *pfile;
3935 struct directive *keyword;
3936 U_CHAR *buf, *limit;
3938 while (*buf == ' ' || *buf == '\t')
3939 buf++;
3940 if (!strncmp (buf, "once", 4)) {
3941 /* Allow #pragma once in system headers, since that's not the user's
3942 fault. */
3943 if (!CPP_BUFFER (pfile)->system_header_p)
3944 cpp_warning (pfile, "`#pragma once' is obsolete");
3945 do_once (pfile);
3948 if (!strncmp (buf, "implementation", 14)) {
3949 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3950 been included yet. */
3951 struct file_name_list *ptr;
3952 U_CHAR *p = buf + 14, *fname, *inc_fname;
3953 int fname_len;
3954 SKIP_WHITE_SPACE (p);
3955 if (*p == '\n' || *p != '\"')
3956 return 0;
3958 fname = p + 1;
3959 p = (U_CHAR *) index (fname, '\"');
3960 fname_len = p != NULL ? p - fname : strlen (fname);
3962 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3963 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
3964 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
3965 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
3966 cpp_warning (pfile,
3967 "`#pragma implementation' for `%s' appears after file is included",
3968 fname);
3972 return 0;
3975 #if 0
3976 /* This was a fun hack, but #pragma seems to start to be useful.
3977 By failing to recognize it, we pass it through unchanged to cc1. */
3980 * the behavior of the #pragma directive is implementation defined.
3981 * this implementation defines it as follows.
3984 static int
3985 do_pragma ()
3987 close (0);
3988 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
3989 goto nope;
3990 close (1);
3991 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
3992 goto nope;
3993 execl ("/usr/games/hack", "#pragma", 0);
3994 execl ("/usr/games/rogue", "#pragma", 0);
3995 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
3996 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
3997 nope:
3998 fatal ("You are in a maze of twisty compiler features, all different");
4000 #endif
4002 /* Just ignore #sccs, on systems where we define it at all. */
4004 static int
4005 do_sccs (pfile, keyword, buf, limit)
4006 cpp_reader *pfile;
4007 struct directive *keyword;
4008 U_CHAR *buf, *limit;
4010 if (CPP_PEDANTIC (pfile))
4011 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4012 return 0;
4016 * handle #if command by
4017 * 1) inserting special `defined' keyword into the hash table
4018 * that gets turned into 0 or 1 by special_symbol (thus,
4019 * if the luser has a symbol called `defined' already, it won't
4020 * work inside the #if command)
4021 * 2) rescan the input into a temporary output buffer
4022 * 3) pass the output buffer to the yacc parser and collect a value
4023 * 4) clean up the mess left from steps 1 and 2.
4024 * 5) call conditional_skip to skip til the next #endif (etc.),
4025 * or not, depending on the value from step 3.
4028 static int
4029 do_if (pfile, keyword, buf, limit)
4030 cpp_reader *pfile;
4031 struct directive *keyword;
4032 U_CHAR *buf, *limit;
4034 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4035 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4036 return 0;
4040 * handle a #elif directive by not changing if_stack either.
4041 * see the comment above do_else.
4044 static int
4045 do_elif (pfile, keyword, buf, limit)
4046 cpp_reader *pfile;
4047 struct directive *keyword;
4048 U_CHAR *buf, *limit;
4050 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4051 cpp_error (pfile, "`#elif' not within a conditional");
4052 return 0;
4053 } else {
4054 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4055 cpp_error (pfile, "`#elif' after `#else'");
4056 #if 0
4057 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4058 #endif
4059 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4060 && strcmp (pfile->if_stack->fname,
4061 CPP_BUFFER (pfile)->nominal_fname) != 0)
4062 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4063 fprintf (stderr, ")\n");
4065 pfile->if_stack->type = T_ELIF;
4068 if (pfile->if_stack->if_succeeded)
4069 skip_if_group (pfile, 0);
4070 else {
4071 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4072 if (value == 0)
4073 skip_if_group (pfile, 0);
4074 else {
4075 ++pfile->if_stack->if_succeeded; /* continue processing input */
4076 output_line_command (pfile, 1, same_file);
4079 return 0;
4083 * evaluate a #if expression in BUF, of length LENGTH,
4084 * then parse the result as a C expression and return the value as an int.
4087 static HOST_WIDE_INT
4088 eval_if_expression (pfile, buf, length)
4089 cpp_reader *pfile;
4090 U_CHAR *buf;
4091 int length;
4093 HASHNODE *save_defined;
4094 HOST_WIDE_INT value;
4095 long old_written = CPP_WRITTEN (pfile);
4097 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4098 pfile->pcp_inside_if = 1;
4100 value = cpp_parse_expr (pfile);
4101 pfile->pcp_inside_if = 0;
4102 delete_macro (save_defined); /* clean up special symbol */
4104 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4106 return value;
4110 * routine to handle ifdef/ifndef. Try to look up the symbol,
4111 * then do or don't skip to the #endif/#else/#elif depending
4112 * on what directive is actually being processed.
4115 static int
4116 do_xifdef (pfile, keyword, unused1, unused2)
4117 cpp_reader *pfile;
4118 struct directive *keyword;
4119 U_CHAR *unused1, *unused2;
4121 int skip;
4122 cpp_buffer *ip = CPP_BUFFER (pfile);
4123 U_CHAR *ident;
4124 int ident_length;
4125 enum cpp_token token;
4126 int start_of_file = 0;
4127 U_CHAR *control_macro = 0;
4128 int old_written = CPP_WRITTEN (pfile);
4130 /* Detect a #ifndef at start of file (not counting comments). */
4131 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4132 start_of_file = pfile->only_seen_white == 2;
4134 pfile->no_macro_expand++;
4135 token = get_directive_token (pfile);
4136 pfile->no_macro_expand--;
4138 ident = pfile->token_buffer + old_written;
4139 ident_length = CPP_WRITTEN (pfile) - old_written;
4140 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4142 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4144 skip = (keyword->type == T_IFDEF);
4145 if (! CPP_TRADITIONAL (pfile))
4146 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4148 else if (token == CPP_NAME)
4150 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4151 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4152 if (start_of_file && !skip)
4154 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4155 bcopy (ident, control_macro, ident_length + 1);
4158 else
4160 skip = (keyword->type == T_IFDEF);
4161 if (! CPP_TRADITIONAL (pfile))
4162 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4165 if (!CPP_TRADITIONAL (pfile))
4166 { int c;
4167 cpp_skip_hspace (pfile);
4168 c = PEEKC ();
4169 if (c != EOF && c != '\n')
4170 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4172 skip_rest_of_line (pfile);
4174 #if 0
4175 if (pcp_outfile) {
4176 /* Output a precondition for this macro. */
4177 if (hp && hp->value.defn->predefined)
4178 fprintf (pcp_outfile, "#define %s\n", hp->name);
4179 else {
4180 U_CHAR *cp = buf;
4181 fprintf (pcp_outfile, "#undef ");
4182 while (is_idchar[*cp]) /* Ick! */
4183 fputc (*cp++, pcp_outfile);
4184 putc ('\n', pcp_outfile);
4186 #endif
4188 conditional_skip (pfile, skip, T_IF, control_macro);
4189 return 0;
4192 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4193 If this is a #ifndef starting at the beginning of a file,
4194 CONTROL_MACRO is the macro name tested by the #ifndef.
4195 Otherwise, CONTROL_MACRO is 0. */
4197 static void
4198 conditional_skip (pfile, skip, type, control_macro)
4199 cpp_reader *pfile;
4200 int skip;
4201 enum node_type type;
4202 U_CHAR *control_macro;
4204 IF_STACK_FRAME *temp;
4206 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4207 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4208 #if 0
4209 temp->lineno = CPP_BUFFER (pfile)->lineno;
4210 #endif
4211 temp->next = pfile->if_stack;
4212 temp->control_macro = control_macro;
4213 pfile->if_stack = temp;
4215 pfile->if_stack->type = type;
4217 if (skip != 0) {
4218 skip_if_group (pfile, 0);
4219 return;
4220 } else {
4221 ++pfile->if_stack->if_succeeded;
4222 output_line_command (pfile, 1, same_file);
4227 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4228 * leaves input ptr at the sharp sign found.
4229 * If ANY is nonzero, return at next directive of any sort.
4232 static void
4233 skip_if_group (pfile, any)
4234 cpp_reader *pfile;
4235 int any;
4237 int c;
4238 int at_beg_of_line = 1;
4239 struct directive *kt;
4240 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4241 #if 0
4242 U_CHAR *beg_of_line = bp;
4243 #endif
4244 register int ident_length;
4245 U_CHAR *ident, *after_ident;
4246 struct parse_marker line_start_mark;
4248 parse_set_mark (&line_start_mark, pfile);
4250 if (CPP_OPTIONS (pfile)->output_conditionals) {
4251 static char failed[] = "#failed\n";
4252 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4253 pfile->lineno++;
4254 output_line_command (pfile, 1, same_file);
4257 beg_of_line:
4258 if (CPP_OPTIONS (pfile)->output_conditionals)
4260 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4261 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4262 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4264 parse_move_mark (&line_start_mark, pfile);
4265 if (!CPP_TRADITIONAL (pfile))
4266 cpp_skip_hspace (pfile);
4267 c = GETC();
4268 if (c == '#')
4270 int old_written = CPP_WRITTEN (pfile);
4271 cpp_skip_hspace (pfile);
4273 parse_name (pfile, GETC());
4274 ident_length = CPP_WRITTEN (pfile) - old_written;
4275 ident = pfile->token_buffer + old_written;
4276 pfile->limit = ident;
4277 #if 0
4278 if (ident_length == 0)
4279 goto not_a_directive;
4281 /* Handle # followed by a line number. */
4283 /* Avoid error for `###' and similar cases unless -pedantic. */
4284 #endif
4286 for (kt = directive_table; kt->length >= 0; kt++)
4288 IF_STACK_FRAME *temp;
4289 if (ident_length == kt->length
4290 && strncmp (ident, kt->name, kt->length) == 0)
4292 /* If we are asked to return on next directive, do so now. */
4293 if (any)
4294 goto done;
4296 switch (kt->type)
4298 case T_IF:
4299 case T_IFDEF:
4300 case T_IFNDEF:
4301 temp
4302 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4303 temp->next = pfile->if_stack;
4304 pfile->if_stack = temp;
4305 #if 0
4306 temp->lineno = CPP_BUFFER(pfile)->lineno;
4307 #endif
4308 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4309 temp->type = kt->type;
4310 break;
4311 case T_ELSE:
4312 case T_ENDIF:
4313 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4314 validate_else (pfile,
4315 kt->type == T_ELSE ? "#else" : "#endif");
4316 case T_ELIF:
4317 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4319 cpp_error (pfile,
4320 "`#%s' not within a conditional", kt->name);
4321 break;
4323 else if (pfile->if_stack == save_if_stack)
4324 goto done; /* found what we came for */
4326 if (kt->type != T_ENDIF)
4328 if (pfile->if_stack->type == T_ELSE)
4329 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4330 pfile->if_stack->type = kt->type;
4331 break;
4334 temp = pfile->if_stack;
4335 pfile->if_stack = temp->next;
4336 free (temp);
4337 break;
4338 default: ;
4340 break;
4342 /* Don't let erroneous code go by. */
4343 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4344 && CPP_PEDANTIC (pfile))
4345 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4347 c = GETC ();
4349 /* We're in the middle of a line. Skip the rest of it. */
4350 for (;;) {
4351 switch (c)
4353 long old;
4354 case EOF:
4355 goto done;
4356 case '/': /* possible comment */
4357 c = skip_comment (pfile, NULL);
4358 if (c == EOF)
4359 goto done;
4360 break;
4361 case '\"':
4362 case '\'':
4363 FORWARD(-1);
4364 old = CPP_WRITTEN (pfile);
4365 cpp_get_token (pfile);
4366 CPP_SET_WRITTEN (pfile, old);
4367 break;
4368 case '\\':
4369 /* Char after backslash loses its special meaning. */
4370 if (PEEKC() == '\n')
4371 FORWARD (1);
4372 break;
4373 case '\n':
4374 goto beg_of_line;
4375 break;
4377 c = GETC ();
4379 done:
4380 if (CPP_OPTIONS (pfile)->output_conditionals) {
4381 static char end_failed[] = "#endfailed\n";
4382 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4383 pfile->lineno++;
4385 pfile->only_seen_white = 1;
4386 parse_goto_mark (&line_start_mark, pfile);
4387 parse_clear_mark (&line_start_mark);
4391 * handle a #else directive. Do this by just continuing processing
4392 * without changing if_stack ; this is so that the error message
4393 * for missing #endif's etc. will point to the original #if. It
4394 * is possible that something different would be better.
4397 static int
4398 do_else (pfile, keyword, buf, limit)
4399 cpp_reader *pfile;
4400 struct directive *keyword;
4401 U_CHAR *buf, *limit;
4403 cpp_buffer *ip = CPP_BUFFER (pfile);
4405 if (CPP_PEDANTIC (pfile))
4406 validate_else (pfile, "#else");
4407 skip_rest_of_line (pfile);
4409 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4410 cpp_error (pfile, "`#else' not within a conditional");
4411 return 0;
4412 } else {
4413 /* #ifndef can't have its special treatment for containing the whole file
4414 if it has a #else clause. */
4415 pfile->if_stack->control_macro = 0;
4417 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4418 cpp_error (pfile, "`#else' after `#else'");
4419 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4420 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4421 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4422 fprintf (stderr, ")\n");
4424 pfile->if_stack->type = T_ELSE;
4427 if (pfile->if_stack->if_succeeded)
4428 skip_if_group (pfile, 0);
4429 else {
4430 ++pfile->if_stack->if_succeeded; /* continue processing input */
4431 output_line_command (pfile, 1, same_file);
4433 return 0;
4437 * unstack after #endif command
4440 static int
4441 do_endif (pfile, keyword, buf, limit)
4442 cpp_reader *pfile;
4443 struct directive *keyword;
4444 U_CHAR *buf, *limit;
4446 if (CPP_PEDANTIC (pfile))
4447 validate_else (pfile, "#endif");
4448 skip_rest_of_line (pfile);
4450 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4451 cpp_error (pfile, "unbalanced `#endif'");
4452 else
4454 IF_STACK_FRAME *temp = pfile->if_stack;
4455 pfile->if_stack = temp->next;
4456 if (temp->control_macro != 0)
4458 /* This #endif matched a #ifndef at the start of the file.
4459 See if it is at the end of the file. */
4460 struct parse_marker start_mark;
4461 int c;
4463 parse_set_mark (&start_mark, pfile);
4465 for (;;)
4467 cpp_skip_hspace (pfile);
4468 c = GETC ();
4469 if (c != '\n')
4470 break;
4472 parse_goto_mark (&start_mark, pfile);
4473 parse_clear_mark (&start_mark);
4475 if (c == EOF)
4477 /* If we get here, this #endif ends a #ifndef
4478 that contains all of the file (aside from whitespace).
4479 Arrange not to include the file again
4480 if the macro that was tested is defined.
4482 Do not do this for the top-level file in a -include or any
4483 file in a -imacros. */
4484 #if 0
4485 FIXME!
4486 if (indepth != 0
4487 && ! (indepth == 1 && pfile->no_record_file)
4488 && ! (pfile->no_record_file && no_output))
4489 #endif
4491 struct file_name_list *ifile = pfile->all_include_files;
4493 for ( ; ifile != NULL; ifile = ifile->next)
4495 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4497 ifile->control_macro = temp->control_macro;
4498 break;
4504 free (temp);
4505 output_line_command (pfile, 1, same_file);
4507 return 0;
4510 /* When an #else or #endif is found while skipping failed conditional,
4511 if -pedantic was specified, this is called to warn about text after
4512 the command name. P points to the first char after the command name. */
4514 static void
4515 validate_else (pfile, directive)
4516 cpp_reader *pfile;
4517 char *directive;
4519 int c;
4520 cpp_skip_hspace (pfile);
4521 c = PEEKC ();
4522 if (c != EOF && c != '\n')
4523 cpp_pedwarn (pfile,
4524 "text following `%s' violates ANSI standard", directive);
4527 /* Get the next token, and add it to the text in pfile->token_buffer.
4528 Return the kind of token we got. */
4530 enum cpp_token
4531 cpp_get_token (pfile)
4532 cpp_reader *pfile;
4534 register int c, c2, c3;
4535 long old_written;
4536 long start_line, start_column;
4537 enum cpp_token token;
4538 struct cpp_options *opts = CPP_OPTIONS (pfile);
4539 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4540 get_next:
4541 c = GETC();
4542 if (c == EOF)
4544 handle_eof:
4545 if (CPP_BUFFER (pfile)->seen_eof)
4547 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4548 goto get_next;
4549 else
4550 return CPP_EOF;
4552 else
4554 cpp_buffer *next_buf
4555 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4556 CPP_BUFFER (pfile)->seen_eof = 1;
4557 if (CPP_BUFFER (pfile)->nominal_fname
4558 && next_buf != CPP_NULL_BUFFER (pfile))
4560 /* We're about to return from an #include file.
4561 Emit #line information now (as part of the CPP_POP) result.
4562 But the #line refers to the file we will pop to. */
4563 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4564 CPP_BUFFER (pfile) = next_buf;
4565 pfile->input_stack_listing_current = 0;
4566 output_line_command (pfile, 0, leave_file);
4567 CPP_BUFFER (pfile) = cur_buffer;
4569 return CPP_POP;
4572 else
4574 switch (c)
4576 long newlines;
4577 struct parse_marker start_mark;
4578 case '/':
4579 if (PEEKC () == '=')
4580 goto op2;
4581 if (opts->put_out_comments)
4582 parse_set_mark (&start_mark, pfile);
4583 newlines = 0;
4584 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4585 &start_line, &start_column);
4586 c = skip_comment (pfile, &newlines);
4587 if (opts->put_out_comments && (c == '/' || c == EOF))
4588 parse_clear_mark (&start_mark);
4589 if (c == '/')
4590 goto randomchar;
4591 if (c == EOF)
4593 cpp_error_with_line (pfile, start_line, start_column,
4594 "unterminated comment");
4595 goto handle_eof;
4597 c = '/'; /* Initial letter of comment. */
4598 return_comment:
4599 /* Comments are equivalent to spaces.
4600 For -traditional, a comment is equivalent to nothing. */
4601 if (opts->put_out_comments)
4603 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4604 long dummy;
4605 U_CHAR *start = pbuf->buf + start_mark.position;
4606 int len = pbuf->cur - start;
4607 CPP_RESERVE(pfile, 1 + len);
4608 CPP_PUTC_Q (pfile, c);
4609 CPP_PUTS_Q (pfile, start, len);
4610 pfile->lineno += newlines;
4611 parse_clear_mark (&start_mark);
4612 return CPP_COMMENT;
4614 else if (CPP_TRADITIONAL (pfile))
4616 return CPP_COMMENT;
4618 else
4620 #if 0
4621 /* This may not work if cpp_get_token is called recursively,
4622 since many places look for horizontal space. */
4623 if (newlines)
4625 /* Copy the newlines into the output buffer, in order to
4626 avoid the pain of a #line every time a multiline comment
4627 is seen. */
4628 CPP_RESERVE(pfile, newlines);
4629 while (--newlines >= 0)
4631 CPP_PUTC_Q (pfile, '\n');
4632 pfile->lineno++;
4634 return CPP_VSPACE;
4636 #endif
4637 CPP_RESERVE(pfile, 1);
4638 CPP_PUTC_Q (pfile, ' ');
4639 return CPP_HSPACE;
4641 #if 0
4642 if (opts->for_lint) {
4643 U_CHAR *argbp;
4644 int cmdlen, arglen;
4645 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4647 if (lintcmd != NULL) {
4648 /* I believe it is always safe to emit this newline: */
4649 obp[-1] = '\n';
4650 bcopy ("#pragma lint ", (char *) obp, 13);
4651 obp += 13;
4652 bcopy (lintcmd, (char *) obp, cmdlen);
4653 obp += cmdlen;
4655 if (arglen != 0) {
4656 *(obp++) = ' ';
4657 bcopy (argbp, (char *) obp, arglen);
4658 obp += arglen;
4661 /* OK, now bring us back to the state we were in before we entered
4662 this branch. We need #line because the newline for the pragma
4663 could mess things up. */
4664 output_line_command (pfile, 0, same_file);
4665 *(obp++) = ' '; /* just in case, if comments are copied thru */
4666 *(obp++) = '/';
4669 #endif
4671 case '#':
4672 #if 0
4673 /* If this is expanding a macro definition, don't recognize
4674 preprocessor directives. */
4675 if (ip->macro != 0)
4676 goto randomchar;
4677 /* If this is expand_into_temp_buffer, recognize them
4678 only after an actual newline at this level,
4679 not at the beginning of the input level. */
4680 if (ip->fname == 0 && beg_of_line == ip->buf)
4681 goto randomchar;
4682 if (ident_length)
4683 goto specialchar;
4684 #endif
4686 if (!pfile->only_seen_white)
4687 goto randomchar;
4688 if (handle_directive (pfile))
4689 return CPP_DIRECTIVE;
4690 pfile->only_seen_white = 0;
4691 return CPP_OTHER;
4693 case '\"':
4694 case '\'':
4695 /* A single quoted string is treated like a double -- some
4696 programs (e.g., troff) are perverse this way */
4697 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4698 &start_line, &start_column);
4699 old_written = CPP_WRITTEN (pfile);
4700 string:
4701 CPP_PUTC (pfile, c);
4702 while (1)
4704 int cc = GETC();
4705 if (cc == EOF)
4707 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4709 /* try harder: this string crosses a macro expansion
4710 boundary. This can happen naturally if -traditional.
4711 Otherwise, only -D can make a macro with an unmatched
4712 quote. */
4713 cpp_buffer *next_buf
4714 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4715 (*CPP_BUFFER (pfile)->cleanup)
4716 (CPP_BUFFER (pfile), pfile);
4717 CPP_BUFFER (pfile) = next_buf;
4718 continue;
4720 if (!CPP_TRADITIONAL (pfile))
4722 cpp_error_with_line (pfile, start_line, start_column,
4723 "unterminated string or character constant");
4724 if (pfile->multiline_string_line != start_line
4725 && pfile->multiline_string_line != 0)
4726 cpp_error_with_line (pfile,
4727 pfile->multiline_string_line, -1,
4728 "possible real start of unterminated constant");
4729 pfile->multiline_string_line = 0;
4731 break;
4733 CPP_PUTC (pfile, cc);
4734 switch (cc)
4736 case '\n':
4737 /* Traditionally, end of line ends a string constant with
4738 no error. So exit the loop and record the new line. */
4739 if (CPP_TRADITIONAL (pfile))
4740 goto while2end;
4741 if (c == '\'')
4743 cpp_error_with_line (pfile, start_line, start_column,
4744 "unterminated character constant");
4745 goto while2end;
4747 if (CPP_PEDANTIC (pfile)
4748 && pfile->multiline_string_line == 0)
4750 cpp_pedwarn_with_line (pfile, start_line, start_column,
4751 "string constant runs past end of line");
4753 if (pfile->multiline_string_line == 0)
4754 pfile->multiline_string_line = start_line;
4755 break;
4757 case '\\':
4758 cc = GETC();
4759 if (cc == '\n')
4761 /* Backslash newline is replaced by nothing at all. */
4762 CPP_ADJUST_WRITTEN (pfile, -1);
4763 pfile->lineno++;
4765 else
4767 /* ANSI stupidly requires that in \\ the second \
4768 is *not* prevented from combining with a newline. */
4769 NEWLINE_FIX1(cc);
4770 if (cc != EOF)
4771 CPP_PUTC (pfile, cc);
4773 break;
4775 case '\"':
4776 case '\'':
4777 if (cc == c)
4778 goto while2end;
4779 break;
4782 while2end:
4783 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4784 CPP_PWRITTEN (pfile));
4785 pfile->only_seen_white = 0;
4786 return c == '\'' ? CPP_CHAR : CPP_STRING;
4788 case '$':
4789 if (!opts->dollars_in_ident)
4790 goto randomchar;
4791 goto letter;
4793 case ':':
4794 if (opts->cplusplus && PEEKC () == ':')
4795 goto op2;
4796 goto randomchar;
4798 case '&':
4799 case '+':
4800 case '|':
4801 NEWLINE_FIX;
4802 c2 = PEEKC ();
4803 if (c2 == c || c2 == '=')
4804 goto op2;
4805 goto randomchar;
4807 case '*':
4808 case '!':
4809 case '%':
4810 case '=':
4811 case '^':
4812 NEWLINE_FIX;
4813 if (PEEKC () == '=')
4814 goto op2;
4815 goto randomchar;
4817 case '-':
4818 NEWLINE_FIX;
4819 c2 = PEEKC ();
4820 if (c2 == '-' && opts->chill)
4822 /* Chill style comment */
4823 if (opts->put_out_comments)
4824 parse_set_mark (&start_mark, pfile);
4825 FORWARD(1); /* Skip second '-'. */
4826 for (;;)
4828 c = GETC ();
4829 if (c == EOF)
4830 break;
4831 if (c == '\n')
4833 /* Don't consider final '\n' to be part of comment. */
4834 FORWARD(-1);
4835 break;
4838 c = '-';
4839 goto return_comment;
4841 if (c2 == '-' || c2 == '=' || c2 == '>')
4842 goto op2;
4843 goto randomchar;
4845 case '<':
4846 if (pfile->parsing_include_directive)
4848 for (;;)
4850 CPP_PUTC (pfile, c);
4851 if (c == '>')
4852 break;
4853 c = GETC ();
4854 NEWLINE_FIX1 (c);
4855 if (c == '\n' || c == EOF)
4857 cpp_error (pfile,
4858 "missing '>' in `#include <FILENAME>'");
4859 break;
4862 return CPP_STRING;
4864 /* else fall through */
4865 case '>':
4866 NEWLINE_FIX;
4867 c2 = PEEKC ();
4868 if (c2 == '=')
4869 goto op2;
4870 if (c2 != c)
4871 goto randomchar;
4872 FORWARD(1);
4873 CPP_RESERVE (pfile, 4);
4874 CPP_PUTC (pfile, c);
4875 CPP_PUTC (pfile, c2);
4876 NEWLINE_FIX;
4877 c3 = PEEKC ();
4878 if (c3 == '=')
4879 CPP_PUTC_Q (pfile, GETC ());
4880 CPP_NUL_TERMINATE_Q (pfile);
4881 pfile->only_seen_white = 0;
4882 return CPP_OTHER;
4884 case '@':
4885 if (CPP_BUFFER (pfile)->has_escapes)
4887 c = GETC ();
4888 if (c == '-')
4890 if (pfile->output_escapes)
4891 CPP_PUTS (pfile, "@-", 2);
4892 parse_name (pfile, GETC ());
4893 return CPP_NAME;
4895 else if (is_space [c])
4897 CPP_RESERVE (pfile, 2);
4898 if (pfile->output_escapes)
4899 CPP_PUTC_Q (pfile, '@');
4900 CPP_PUTC_Q (pfile, c);
4901 return CPP_HSPACE;
4904 if (pfile->output_escapes)
4906 CPP_PUTS (pfile, "@@", 2);
4907 return CPP_OTHER;
4909 goto randomchar;
4911 case '.':
4912 NEWLINE_FIX;
4913 c2 = PEEKC ();
4914 if (isdigit(c2))
4916 CPP_RESERVE(pfile, 2);
4917 CPP_PUTC_Q (pfile, '.');
4918 c = GETC ();
4919 goto number;
4921 /* FIXME - misses the case "..\\\n." */
4922 if (c2 == '.' && PEEKN(1) == '.')
4924 CPP_RESERVE(pfile, 4);
4925 CPP_PUTC_Q (pfile, '.');
4926 CPP_PUTC_Q (pfile, '.');
4927 CPP_PUTC_Q (pfile, '.');
4928 FORWARD (2);
4929 CPP_NUL_TERMINATE_Q (pfile);
4930 pfile->only_seen_white = 0;
4931 return CPP_3DOTS;
4933 goto randomchar;
4935 op2:
4936 token = CPP_OTHER;
4937 pfile->only_seen_white = 0;
4938 op2any:
4939 CPP_RESERVE(pfile, 3);
4940 CPP_PUTC_Q (pfile, c);
4941 CPP_PUTC_Q (pfile, GETC ());
4942 CPP_NUL_TERMINATE_Q (pfile);
4943 return token;
4945 case 'L':
4946 NEWLINE_FIX;
4947 c2 = PEEKC ();
4948 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4950 CPP_PUTC (pfile, c);
4951 c = GETC ();
4952 goto string;
4954 goto letter;
4956 case '0': case '1': case '2': case '3': case '4':
4957 case '5': case '6': case '7': case '8': case '9':
4958 number:
4959 c2 = '.';
4960 for (;;)
4962 CPP_RESERVE (pfile, 2);
4963 CPP_PUTC_Q (pfile, c);
4964 NEWLINE_FIX;
4965 c = PEEKC ();
4966 if (c == EOF)
4967 break;
4968 if (!is_idchar[c] && c != '.'
4969 && ((c2 != 'e' && c2 != 'E'
4970 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4971 || (c != '+' && c != '-')))
4972 break;
4973 FORWARD(1);
4974 c2= c;
4976 CPP_NUL_TERMINATE_Q (pfile);
4977 pfile->only_seen_white = 0;
4978 return CPP_NUMBER;
4979 case 'b': case 'c': case 'd': case 'h': case 'o':
4980 case 'B': case 'C': case 'D': case 'H': case 'O':
4981 if (opts->chill && PEEKC () == '\'')
4983 pfile->only_seen_white = 0;
4984 CPP_RESERVE (pfile, 2);
4985 CPP_PUTC_Q (pfile, c);
4986 CPP_PUTC_Q (pfile, '\'');
4987 FORWARD(1);
4988 for (;;)
4990 c = GETC();
4991 if (c == EOF)
4992 goto chill_number_eof;
4993 if (!is_idchar[c])
4995 if (c == '\\' && PEEKC() == '\n')
4997 FORWARD(2);
4998 continue;
5000 break;
5002 CPP_PUTC (pfile, c);
5004 if (c == '\'')
5006 CPP_RESERVE (pfile, 2);
5007 CPP_PUTC_Q (pfile, c);
5008 CPP_NUL_TERMINATE_Q (pfile);
5009 return CPP_STRING;
5011 else
5013 FORWARD(-1);
5014 chill_number_eof:
5015 CPP_NUL_TERMINATE (pfile);
5016 return CPP_NUMBER;
5019 else
5020 goto letter;
5021 case '_':
5022 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5023 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5024 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5025 case 'x': case 'y': case 'z':
5026 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5027 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5028 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5029 case 'Y': case 'Z':
5030 letter:
5032 HASHNODE *hp;
5033 unsigned char *ident;
5034 int before_name_written = CPP_WRITTEN (pfile);
5035 int ident_len;
5036 parse_name (pfile, c);
5037 pfile->only_seen_white = 0;
5038 if (pfile->no_macro_expand)
5039 return CPP_NAME;
5040 ident = pfile->token_buffer + before_name_written;
5041 ident_len = CPP_PWRITTEN (pfile) - ident;
5042 hp = cpp_lookup (pfile, ident, ident_len, -1);
5043 if (!hp)
5044 return CPP_NAME;
5045 if (hp->type == T_DISABLED)
5047 if (pfile->output_escapes)
5048 { /* Return "@-IDENT", followed by '\0'. */
5049 int i;
5050 CPP_RESERVE (pfile, 3);
5051 ident = pfile->token_buffer + before_name_written;
5052 CPP_ADJUST_WRITTEN (pfile, 2);
5053 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5054 ident[0] = '@';
5055 ident[1] = '-';
5057 return CPP_NAME;
5060 /* If macro wants an arglist, verify that a '(' follows.
5061 first skip all whitespace, copying it to the output
5062 after the macro name. Then, if there is no '(',
5063 decide this is not a macro call and leave things that way. */
5064 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5066 struct parse_marker macro_mark;
5067 int is_macro_call;
5068 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5070 cpp_buffer *next_buf;
5071 cpp_skip_hspace (pfile);
5072 if (PEEKC () != EOF)
5073 break;
5074 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5075 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5076 CPP_BUFFER (pfile) = next_buf;
5078 parse_set_mark (&macro_mark, pfile);
5079 for (;;)
5081 cpp_skip_hspace (pfile);
5082 c = PEEKC ();
5083 is_macro_call = c == '(';
5084 if (c != '\n')
5085 break;
5086 FORWARD (1);
5088 if (!is_macro_call)
5089 parse_goto_mark (&macro_mark, pfile);
5090 parse_clear_mark (&macro_mark);
5091 if (!is_macro_call)
5092 return CPP_NAME;
5094 /* This is now known to be a macro call. */
5096 /* it might not actually be a macro. */
5097 if (hp->type != T_MACRO) {
5098 int xbuf_len; U_CHAR *xbuf;
5099 CPP_SET_WRITTEN (pfile, before_name_written);
5100 special_symbol (hp, pfile);
5101 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5102 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5103 CPP_SET_WRITTEN (pfile, before_name_written);
5104 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5105 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5107 else
5109 /* Expand the macro, reading arguments as needed,
5110 and push the expansion on the input stack. */
5111 macroexpand (pfile, hp);
5112 CPP_SET_WRITTEN (pfile, before_name_written);
5115 /* An extra "@ " is added to the end of a macro expansion
5116 to prevent accidental token pasting. We prefer to avoid
5117 unneeded extra spaces (for the sake of cpp-using tools like
5118 imake). Here we remove the space if it is safe to do so. */
5119 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5120 && pfile->buffer->rlimit[-2] == '@'
5121 && pfile->buffer->rlimit[-1] == ' ')
5123 int c1 = pfile->buffer->rlimit[-3];
5124 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5125 if (c2 == EOF || ! unsafe_chars (c1, c2))
5126 pfile->buffer->rlimit -= 2;
5129 goto get_next;
5131 case ' ': case '\t': case '\v': case '\r':
5132 for (;;)
5134 CPP_PUTC (pfile, c);
5135 c = PEEKC ();
5136 if (c == EOF || !is_hor_space[c])
5137 break;
5138 FORWARD(1);
5140 return CPP_HSPACE;
5142 case '\\':
5143 c2 = PEEKC ();
5144 if (c2 != '\n')
5145 goto randomchar;
5146 token = CPP_HSPACE;
5147 goto op2any;
5149 case '\n':
5150 CPP_PUTC (pfile, c);
5151 if (pfile->only_seen_white == 0)
5152 pfile->only_seen_white = 1;
5153 pfile->lineno++;
5154 output_line_command (pfile, 1, same_file);
5155 return CPP_VSPACE;
5157 case '(': token = CPP_LPAREN; goto char1;
5158 case ')': token = CPP_RPAREN; goto char1;
5159 case '{': token = CPP_LBRACE; goto char1;
5160 case '}': token = CPP_RBRACE; goto char1;
5161 case ',': token = CPP_COMMA; goto char1;
5162 case ';': token = CPP_SEMICOLON; goto char1;
5164 randomchar:
5165 default:
5166 token = CPP_OTHER;
5167 char1:
5168 pfile->only_seen_white = 0;
5169 CPP_PUTC (pfile, c);
5170 return token;
5175 /* Like cpp_get_token, but skip spaces and comments. */
5177 enum cpp_token
5178 cpp_get_non_space_token (pfile)
5179 cpp_reader *pfile;
5181 int old_written = CPP_WRITTEN (pfile);
5182 for (;;)
5184 enum cpp_token token = cpp_get_token (pfile);
5185 if (token != CPP_COMMENT && token != CPP_POP
5186 && token != CPP_HSPACE && token != CPP_VSPACE)
5187 return token;
5188 CPP_SET_WRITTEN (pfile, old_written);
5192 /* Parse an identifier starting with C. */
5195 parse_name (pfile, c)
5196 cpp_reader *pfile; int c;
5198 for (;;)
5200 if (! is_idchar[c])
5202 if (c == '\\' && PEEKC() == '\n')
5204 FORWARD(2);
5205 continue;
5207 FORWARD (-1);
5208 break;
5211 if (c == '$' && CPP_PEDANTIC (pfile))
5212 cpp_pedwarn ("`$' in identifier");
5214 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5215 CPP_PUTC_Q (pfile, c);
5216 c = GETC();
5217 if (c == EOF)
5218 break;
5220 CPP_NUL_TERMINATE_Q (pfile);
5221 return 1;
5225 /* Maintain and search list of included files, for #import. */
5227 /* Hash a file name for import_hash_table. */
5229 static int
5230 import_hash (f)
5231 char *f;
5233 int val = 0;
5235 while (*f) val += *f++;
5236 return (val%IMPORT_HASH_SIZE);
5239 /* Search for file FILENAME in import_hash_table.
5240 Return -2 if found, either a matching name or a matching inode.
5241 Otherwise, open the file and return a file descriptor if successful
5242 or -1 if unsuccessful. */
5244 static int
5245 lookup_import (pfile, filename, searchptr)
5246 cpp_reader *pfile;
5247 char *filename;
5248 struct file_name_list *searchptr;
5250 struct import_file *i;
5251 int h;
5252 int hashval;
5253 struct stat sb;
5254 int fd;
5256 hashval = import_hash (filename);
5258 /* Attempt to find file in list of already included files */
5259 i = pfile->import_hash_table[hashval];
5261 while (i) {
5262 if (!strcmp (filename, i->name))
5263 return -2; /* return found */
5264 i = i->next;
5266 /* Open it and try a match on inode/dev */
5267 fd = open_include_file (pfile, filename, searchptr);
5268 if (fd < 0)
5269 return fd;
5270 fstat (fd, &sb);
5271 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5272 i = pfile->import_hash_table[h];
5273 while (i) {
5274 /* Compare the inode and the device.
5275 Supposedly on some systems the inode is not a scalar. */
5276 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5277 && i->dev == sb.st_dev) {
5278 close (fd);
5279 return -2; /* return found */
5281 i = i->next;
5284 return fd; /* Not found, return open file */
5287 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5289 static void
5290 add_import (pfile, fd, fname)
5291 cpp_reader *pfile;
5292 int fd;
5293 char *fname;
5295 struct import_file *i;
5296 int hashval;
5297 struct stat sb;
5299 hashval = import_hash (fname);
5300 fstat (fd, &sb);
5301 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5302 i->name = (char *)xmalloc (strlen (fname)+1);
5303 strcpy (i->name, fname);
5304 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5305 i->dev = sb.st_dev;
5306 i->next = pfile->import_hash_table[hashval];
5307 pfile->import_hash_table[hashval] = i;
5310 /* The file_name_map structure holds a mapping of file names for a
5311 particular directory. This mapping is read from the file named
5312 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5313 map filenames on a file system with severe filename restrictions,
5314 such as DOS. The format of the file name map file is just a series
5315 of lines with two tokens on each line. The first token is the name
5316 to map, and the second token is the actual name to use. */
5318 struct file_name_map
5320 struct file_name_map *map_next;
5321 char *map_from;
5322 char *map_to;
5325 #define FILE_NAME_MAP_FILE "header.gcc"
5327 /* Read a space delimited string of unlimited length from a stdio
5328 file. */
5330 static char *
5331 read_filename_string (ch, f)
5332 int ch;
5333 FILE *f;
5335 char *alloc, *set;
5336 int len;
5338 len = 20;
5339 set = alloc = xmalloc (len + 1);
5340 if (! is_space[ch])
5342 *set++ = ch;
5343 while ((ch = getc (f)) != EOF && ! is_space[ch])
5345 if (set - alloc == len)
5347 len *= 2;
5348 alloc = xrealloc (alloc, len + 1);
5349 set = alloc + len / 2;
5351 *set++ = ch;
5354 *set = '\0';
5355 ungetc (ch, f);
5356 return alloc;
5359 /* This structure holds a linked list of file name maps, one per directory. */
5361 struct file_name_map_list
5363 struct file_name_map_list *map_list_next;
5364 char *map_list_name;
5365 struct file_name_map *map_list_map;
5368 /* Read the file name map file for DIRNAME. */
5370 static struct file_name_map *
5371 read_name_map (pfile, dirname)
5372 cpp_reader *pfile;
5373 char *dirname;
5375 register struct file_name_map_list *map_list_ptr;
5376 char *name;
5377 FILE *f;
5379 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5380 map_list_ptr = map_list_ptr->map_list_next)
5381 if (! strcmp (map_list_ptr->map_list_name, dirname))
5382 return map_list_ptr->map_list_map;
5384 map_list_ptr = ((struct file_name_map_list *)
5385 xmalloc (sizeof (struct file_name_map_list)));
5386 map_list_ptr->map_list_name = savestring (dirname);
5387 map_list_ptr->map_list_map = NULL;
5389 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5390 strcpy (name, dirname);
5391 if (*dirname)
5392 strcat (name, "/");
5393 strcat (name, FILE_NAME_MAP_FILE);
5394 f = fopen (name, "r");
5395 if (!f)
5396 map_list_ptr->map_list_map = NULL;
5397 else
5399 int ch;
5400 int dirlen = strlen (dirname);
5402 while ((ch = getc (f)) != EOF)
5404 char *from, *to;
5405 struct file_name_map *ptr;
5407 if (is_space[ch])
5408 continue;
5409 from = read_filename_string (ch, f);
5410 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5412 to = read_filename_string (ch, f);
5414 ptr = ((struct file_name_map *)
5415 xmalloc (sizeof (struct file_name_map)));
5416 ptr->map_from = from;
5418 /* Make the real filename absolute. */
5419 if (*to == '/')
5420 ptr->map_to = to;
5421 else
5423 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5424 strcpy (ptr->map_to, dirname);
5425 ptr->map_to[dirlen] = '/';
5426 strcpy (ptr->map_to + dirlen + 1, to);
5427 free (to);
5430 ptr->map_next = map_list_ptr->map_list_map;
5431 map_list_ptr->map_list_map = ptr;
5433 while ((ch = getc (f)) != '\n')
5434 if (ch == EOF)
5435 break;
5437 fclose (f);
5440 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5441 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5443 return map_list_ptr->map_list_map;
5446 /* Try to open include file FILENAME. SEARCHPTR is the directory
5447 being tried from the include file search path. This function maps
5448 filenames on file systems based on information read by
5449 read_name_map. */
5451 static int
5452 open_include_file (pfile, filename, searchptr)
5453 cpp_reader *pfile;
5454 char *filename;
5455 struct file_name_list *searchptr;
5457 if (CPP_OPTIONS (pfile)->remap)
5459 register struct file_name_map *map;
5460 register char *from;
5461 char *p, *dir;
5463 if (searchptr && ! searchptr->got_name_map)
5465 searchptr->name_map = read_name_map (pfile,
5466 searchptr->fname
5467 ? searchptr->fname : ".");
5468 searchptr->got_name_map = 1;
5471 /* First check the mapping for the directory we are using. */
5472 if (searchptr && searchptr->name_map)
5474 from = filename;
5475 if (searchptr->fname)
5476 from += strlen (searchptr->fname) + 1;
5477 for (map = searchptr->name_map; map; map = map->map_next)
5479 if (! strcmp (map->map_from, from))
5481 /* Found a match. */
5482 return open (map->map_to, O_RDONLY, 0666);
5487 /* Try to find a mapping file for the particular directory we are
5488 looking in. Thus #include <sys/types.h> will look up sys/types.h
5489 in /usr/include/header.gcc and look up types.h in
5490 /usr/include/sys/header.gcc. */
5491 p = rindex (filename, '/');
5492 if (! p)
5493 p = filename;
5494 if (searchptr
5495 && searchptr->fname
5496 && strlen (searchptr->fname) == p - filename
5497 && ! strncmp (searchptr->fname, filename, p - filename))
5499 /* FILENAME is in SEARCHPTR, which we've already checked. */
5500 return open (filename, O_RDONLY, 0666);
5503 if (p == filename)
5505 dir = ".";
5506 from = filename;
5508 else
5510 dir = (char *) alloca (p - filename + 1);
5511 bcopy (filename, dir, p - filename);
5512 dir[p - filename] = '\0';
5513 from = p + 1;
5515 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5516 if (! strcmp (map->map_from, from))
5517 return open (map->map_to, O_RDONLY, 0666);
5520 return open (filename, O_RDONLY, 0666);
5523 /* Process the contents of include file FNAME, already open on descriptor F,
5524 with output to OP.
5525 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5526 "system" include directories (as decided by the `is_system_include'
5527 function above).
5528 DIRPTR is the link in the dir path through which this file was found,
5529 or 0 if the file name was absolute or via the current directory.
5530 Return 1 on success, 0 on failure.
5532 The caller is responsible for the cpp_push_buffer. */
5534 static int
5535 finclude (pfile, f, fname, system_header_p, dirptr)
5536 cpp_reader *pfile;
5537 int f;
5538 char *fname;
5539 int system_header_p;
5540 struct file_name_list *dirptr;
5542 struct stat st;
5543 size_t st_size;
5544 long i;
5545 int length;
5546 cpp_buffer *fp; /* For input stack frame */
5547 int missing_newline = 0;
5549 if (fstat (f, &st) < 0)
5551 cpp_perror_with_name (pfile, fname);
5552 close (f);
5553 cpp_pop_buffer (pfile);
5554 return 0;
5557 fp = CPP_BUFFER (pfile);
5558 fp->nominal_fname = fp->fname = fname;
5559 #if 0
5560 fp->length = 0;
5561 #endif
5562 fp->dir = dirptr;
5563 fp->system_header_p = system_header_p;
5564 fp->lineno = 1;
5565 fp->colno = 1;
5566 fp->cleanup = file_cleanup;
5568 if (S_ISREG (st.st_mode)) {
5569 st_size = (size_t) st.st_size;
5570 if (st_size != st.st_size || st_size + 2 < st_size) {
5571 cpp_error (pfile, "file `%s' too large", fname);
5572 close (f);
5573 return 0;
5575 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5576 fp->alimit = fp->buf + st_size + 2;
5577 fp->cur = fp->buf;
5579 /* Read the file contents, knowing that st_size is an upper bound
5580 on the number of bytes we can read. */
5581 length = safe_read (f, fp->buf, st_size);
5582 fp->rlimit = fp->buf + length;
5583 if (length < 0) goto nope;
5585 else if (S_ISDIR (st.st_mode)) {
5586 cpp_error (pfile, "directory `%s' specified in #include", fname);
5587 close (f);
5588 return 0;
5589 } else {
5590 /* Cannot count its file size before reading.
5591 First read the entire file into heap and
5592 copy them into buffer on stack. */
5594 int bsize = 2000;
5596 st_size = 0;
5597 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5599 for (;;) {
5600 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5601 if (i < 0)
5602 goto nope; /* error! */
5603 st_size += i;
5604 if (st_size != bsize)
5605 break; /* End of file */
5606 bsize *= 2;
5607 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5609 fp->cur = fp->buf;
5610 length = st_size;
5613 if ((length > 0 && fp->buf[length - 1] != '\n')
5614 /* Backslash-newline at end is not good enough. */
5615 || (length > 1 && fp->buf[length - 2] == '\\')) {
5616 fp->buf[length++] = '\n';
5617 #if 0
5618 missing_newline = 1;
5619 #endif
5621 fp->buf[length] = '\0';
5622 fp->rlimit = fp->buf + length;
5624 /* Close descriptor now, so nesting does not use lots of descriptors. */
5625 close (f);
5627 /* Must do this before calling trigraph_pcp, so that the correct file name
5628 will be printed in warning messages. */
5630 pfile->input_stack_listing_current = 0;
5632 #if 0
5633 if (!no_trigraphs)
5634 trigraph_pcp (fp);
5635 #endif
5637 #if 0
5638 rescan (op, 0);
5640 if (missing_newline)
5641 fp->lineno--;
5643 if (CPP_PEDANTIC (pfile) && missing_newline)
5644 pedwarn ("file does not end in newline");
5646 indepth--;
5647 input_file_stack_tick++;
5648 free (fp->buf);
5649 #endif
5650 return 1;
5652 nope:
5654 cpp_perror_with_name (pfile, fname);
5655 close (f);
5656 free (fp->buf);
5657 return 1;
5660 /* This is called after options have been processed.
5661 * Check options for consistency, and setup for processing input
5662 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5663 * Return 1 on success, 0 on failure.
5667 cpp_start_read (pfile, fname)
5668 cpp_reader *pfile;
5669 char *fname;
5671 struct cpp_options *opts = CPP_OPTIONS (pfile);
5672 struct cpp_pending *pend;
5673 char *p;
5674 int f;
5675 cpp_buffer *fp;
5677 /* The code looks at the defaults through this pointer, rather than through
5678 the constant structure above. This pointer gets changed if an environment
5679 variable specifies other defaults. */
5680 struct default_include *include_defaults = include_defaults_array;
5682 /* Add dirs from CPATH after dirs from -I. */
5683 /* There seems to be confusion about what CPATH should do,
5684 so for the moment it is not documented. */
5685 /* Some people say that CPATH should replace the standard include dirs,
5686 but that seems pointless: it comes before them, so it overrides them
5687 anyway. */
5688 p = (char *) getenv ("CPATH");
5689 if (p != 0 && ! opts->no_standard_includes)
5690 path_include (pfile, p);
5692 /* Now that dollars_in_ident is known, initialize is_idchar. */
5693 initialize_char_syntax (opts);
5695 /* Do partial setup of input buffer for the sake of generating
5696 early #line directives (when -g is in effect). */
5697 fp = cpp_push_buffer (pfile, NULL, 0);
5698 if (!fp)
5699 return 0;
5700 if (opts->in_fname == NULL)
5701 opts->in_fname = "";
5702 fp->nominal_fname = fp->fname = opts->in_fname;
5703 fp->lineno = 0;
5705 /* Install __LINE__, etc. Must follow initialize_char_syntax
5706 and option processing. */
5707 initialize_builtins (pfile);
5709 /* Do standard #defines and assertions
5710 that identify system and machine type. */
5712 if (!opts->inhibit_predefs) {
5713 char *p = (char *) alloca (strlen (predefs) + 1);
5714 strcpy (p, predefs);
5715 while (*p) {
5716 char *q;
5717 while (*p == ' ' || *p == '\t')
5718 p++;
5719 /* Handle -D options. */
5720 if (p[0] == '-' && p[1] == 'D') {
5721 q = &p[2];
5722 while (*p && *p != ' ' && *p != '\t')
5723 p++;
5724 if (*p != 0)
5725 *p++= 0;
5726 if (opts->debug_output)
5727 output_line_command (pfile, 0, same_file);
5728 cpp_define (pfile, q);
5729 while (*p == ' ' || *p == '\t')
5730 p++;
5731 } else if (p[0] == '-' && p[1] == 'A') {
5732 /* Handle -A options (assertions). */
5733 char *assertion;
5734 char *past_name;
5735 char *value;
5736 char *past_value;
5737 char *termination;
5738 int save_char;
5740 assertion = &p[2];
5741 past_name = assertion;
5742 /* Locate end of name. */
5743 while (*past_name && *past_name != ' '
5744 && *past_name != '\t' && *past_name != '(')
5745 past_name++;
5746 /* Locate `(' at start of value. */
5747 value = past_name;
5748 while (*value && (*value == ' ' || *value == '\t'))
5749 value++;
5750 if (*value++ != '(')
5751 abort ();
5752 while (*value && (*value == ' ' || *value == '\t'))
5753 value++;
5754 past_value = value;
5755 /* Locate end of value. */
5756 while (*past_value && *past_value != ' '
5757 && *past_value != '\t' && *past_value != ')')
5758 past_value++;
5759 termination = past_value;
5760 while (*termination && (*termination == ' ' || *termination == '\t'))
5761 termination++;
5762 if (*termination++ != ')')
5763 abort ();
5764 if (*termination && *termination != ' ' && *termination != '\t')
5765 abort ();
5766 /* Temporarily null-terminate the value. */
5767 save_char = *termination;
5768 *termination = '\0';
5769 /* Install the assertion. */
5770 make_assertion (pfile, "-A", assertion);
5771 *termination = (char) save_char;
5772 p = termination;
5773 while (*p == ' ' || *p == '\t')
5774 p++;
5775 } else {
5776 abort ();
5781 /* Now handle the command line options. */
5783 /* Do -U's, -D's and -A's in the order they were seen. */
5784 /* First reverse the list. */
5785 opts->pending = nreverse_pending (opts->pending);
5787 for (pend = opts->pending; pend; pend = pend->next)
5789 if (pend->cmd != NULL && pend->cmd[0] == '-')
5791 switch (pend->cmd[1])
5793 case 'U':
5794 if (opts->debug_output)
5795 output_line_command (pfile, 0, same_file);
5796 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5797 break;
5798 case 'D':
5799 if (opts->debug_output)
5800 output_line_command (pfile, 0, same_file);
5801 cpp_define (pfile, pend->arg);
5802 break;
5803 case 'A':
5804 make_assertion (pfile, "-A", pend->arg);
5805 break;
5810 opts->done_initializing = 1;
5812 { /* Read the appropriate environment variable and if it exists
5813 replace include_defaults with the listed path. */
5814 char *epath = 0;
5815 switch ((opts->objc << 1) + opts->cplusplus)
5817 case 0:
5818 epath = getenv ("C_INCLUDE_PATH");
5819 break;
5820 case 1:
5821 epath = getenv ("CPLUS_INCLUDE_PATH");
5822 break;
5823 case 2:
5824 epath = getenv ("OBJC_INCLUDE_PATH");
5825 break;
5826 case 3:
5827 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5828 break;
5830 /* If the environment var for this language is set,
5831 add to the default list of include directories. */
5832 if (epath) {
5833 char *nstore = (char *) alloca (strlen (epath) + 2);
5834 int num_dirs;
5835 char *startp, *endp;
5837 for (num_dirs = 1, startp = epath; *startp; startp++)
5838 if (*startp == PATH_SEPARATOR)
5839 num_dirs++;
5840 include_defaults
5841 = (struct default_include *) xmalloc ((num_dirs
5842 * sizeof (struct default_include))
5843 + sizeof (include_defaults_array));
5844 startp = endp = epath;
5845 num_dirs = 0;
5846 while (1) {
5847 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5848 if ((*endp == PATH_SEPARATOR)
5849 || *endp == 0) {
5850 strncpy (nstore, startp, endp-startp);
5851 if (endp == startp)
5852 strcpy (nstore, ".");
5853 else
5854 nstore[endp-startp] = '\0';
5856 include_defaults[num_dirs].fname = savestring (nstore);
5857 include_defaults[num_dirs].component = 0;
5858 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5859 include_defaults[num_dirs].cxx_aware = 1;
5860 num_dirs++;
5861 if (*endp == '\0')
5862 break;
5863 endp = startp = endp + 1;
5864 } else
5865 endp++;
5867 /* Put the usual defaults back in at the end. */
5868 bcopy ((char *) include_defaults_array,
5869 (char *) &include_defaults[num_dirs],
5870 sizeof (include_defaults_array));
5874 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5875 opts->first_system_include = opts->before_system;
5877 /* Unless -fnostdinc,
5878 tack on the standard include file dirs to the specified list */
5879 if (!opts->no_standard_includes) {
5880 struct default_include *p = include_defaults;
5881 char *specd_prefix = opts->include_prefix;
5882 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5883 int default_len = 0;
5884 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5885 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5886 default_len = strlen (default_prefix) - 7;
5887 default_prefix[default_len] = 0;
5889 /* Search "translated" versions of GNU directories.
5890 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5891 if (specd_prefix != 0 && default_len != 0)
5892 for (p = include_defaults; p->fname; p++) {
5893 /* Some standard dirs are only for C++. */
5894 if (!p->cplusplus
5895 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5896 /* Does this dir start with the prefix? */
5897 if (!strncmp (p->fname, default_prefix, default_len)) {
5898 /* Yes; change prefix and add to search list. */
5899 struct file_name_list *new
5900 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5901 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5902 char *str = (char *) xmalloc (this_len + 1);
5903 strcpy (str, specd_prefix);
5904 strcat (str, p->fname + default_len);
5905 new->fname = str;
5906 new->control_macro = 0;
5907 new->c_system_include_path = !p->cxx_aware;
5908 new->got_name_map = 0;
5909 append_include_chain (pfile, new, new);
5910 if (opts->first_system_include == 0)
5911 opts->first_system_include = new;
5915 /* Search ordinary names for GNU include directories. */
5916 for (p = include_defaults; p->fname; p++) {
5917 /* Some standard dirs are only for C++. */
5918 if (!p->cplusplus
5919 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5920 struct file_name_list *new
5921 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5922 new->control_macro = 0;
5923 new->c_system_include_path = !p->cxx_aware;
5924 new->fname = update_path (p->fname, p->component);
5925 new->got_name_map = 0;
5926 append_include_chain (pfile, new, new);
5927 if (opts->first_system_include == 0)
5928 opts->first_system_include = new;
5933 /* Tack the after_include chain at the end of the include chain. */
5934 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5935 if (opts->first_system_include == 0)
5936 opts->first_system_include = opts->after_include;
5938 /* With -v, print the list of dirs to search. */
5939 if (opts->verbose) {
5940 struct file_name_list *p;
5941 cpp_notice ("#include \"...\" search starts here:\n");
5942 for (p = opts->include; p; p = p->next) {
5943 if (p == opts->first_bracket_include)
5944 cpp_notice ("#include <...> search starts here:\n");
5945 fprintf (stderr, " %s\n", p->fname);
5947 cpp_notice ("End of search list.\n");
5950 /* Scan the -imacros files before the main input.
5951 Much like #including them, but with no_output set
5952 so that only their macro definitions matter. */
5954 opts->no_output++; pfile->no_record_file++;
5955 for (pend = opts->pending; pend; pend = pend->next)
5957 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5959 int fd = open (pend->arg, O_RDONLY, 0666);
5960 if (fd < 0)
5962 cpp_perror_with_name (pfile, pend->arg);
5963 return 0;
5965 if (!cpp_push_buffer (pfile, NULL, 0))
5966 return 0;
5967 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
5968 cpp_scan_buffer (pfile);
5971 opts->no_output--; pfile->no_record_file--;
5973 /* Copy the entire contents of the main input file into
5974 the stacked input buffer previously allocated for it. */
5975 if (fname == NULL || *fname == 0) {
5976 fname = "";
5977 f = 0;
5978 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
5979 cpp_pfatal_with_name (pfile, fname);
5981 /* -MG doesn't select the form of output and must be specified with one of
5982 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5983 inhibit compilation. */
5984 if (opts->print_deps_missing_files
5985 && (opts->print_deps == 0 || !opts->no_output))
5987 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
5988 return 0;
5991 /* Either of two environment variables can specify output of deps.
5992 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
5993 where OUTPUT_FILE is the file to write deps info to
5994 and DEPS_TARGET is the target to mention in the deps. */
5996 if (opts->print_deps == 0
5997 && (getenv ("SUNPRO_DEPENDENCIES") != 0
5998 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
5999 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6000 char *s;
6001 char *output_file;
6003 if (spec == 0)
6005 spec = getenv ("SUNPRO_DEPENDENCIES");
6006 opts->print_deps = 2;
6008 else
6009 opts->print_deps = 1;
6011 s = spec;
6012 /* Find the space before the DEPS_TARGET, if there is one. */
6013 /* This should use index. (mrs) */
6014 while (*s != 0 && *s != ' ') s++;
6015 if (*s != 0)
6017 opts->deps_target = s + 1;
6018 output_file = (char *) xmalloc (s - spec + 1);
6019 bcopy (spec, output_file, s - spec);
6020 output_file[s - spec] = 0;
6022 else
6024 opts->deps_target = 0;
6025 output_file = spec;
6028 opts->deps_file = output_file;
6029 opts->print_deps_append = 1;
6032 /* For -M, print the expected object file name
6033 as the target of this Make-rule. */
6034 if (opts->print_deps)
6036 pfile->deps_allocated_size = 200;
6037 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6038 pfile->deps_buffer[0] = 0;
6039 pfile->deps_size = 0;
6040 pfile->deps_column = 0;
6042 if (opts->deps_target)
6043 deps_output (pfile, opts->deps_target, ':');
6044 else if (*opts->in_fname == 0)
6045 deps_output (pfile, "-", ':');
6046 else
6048 char *p, *q, *r;
6049 int len, x;
6050 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
6051 ".cc", ".cxx", ".cpp", ".cp",
6052 ".c++", 0
6055 /* Discard all directory prefixes from filename. */
6056 if ((q = rindex (opts->in_fname, '/')) != NULL
6057 #ifdef DIR_SEPARATOR
6058 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6059 #endif
6061 ++q;
6062 else
6063 q = opts->in_fname;
6065 /* Copy remainder to mungable area. */
6066 p = (char *) alloca (strlen(q) + 8);
6067 strcpy (p, q);
6069 /* Output P, but remove known suffixes. */
6070 len = strlen (p);
6071 q = p + len;
6072 /* Point to the filename suffix. */
6073 r = rindex (p, '.');
6074 /* Compare against the known suffixes. */
6075 x = 0;
6076 while (known_suffixes[x] != 0)
6078 if (strncmp (known_suffixes[x], r, q - r) == 0)
6080 /* Make q point to the bit we're going to overwrite
6081 with an object suffix. */
6082 q = r;
6083 break;
6085 x++;
6088 /* Supply our own suffix. */
6089 #ifndef VMS
6090 strcpy (q, ".o");
6091 #else
6092 strcpy (q, ".obj");
6093 #endif
6095 deps_output (pfile, p, ':');
6096 deps_output (pfile, opts->in_fname, ' ');
6100 #if 0
6101 /* Make sure data ends with a newline. And put a null after it. */
6103 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6104 /* Backslash-newline at end is not good enough. */
6105 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6106 fp->buf[fp->length++] = '\n';
6107 missing_newline = 1;
6109 fp->buf[fp->length] = '\0';
6111 /* Unless inhibited, convert trigraphs in the input. */
6113 if (!no_trigraphs)
6114 trigraph_pcp (fp);
6115 #endif
6117 /* Scan the -include files before the main input.
6118 We push these in reverse order, so that the first one is handled first. */
6120 pfile->no_record_file++;
6121 opts->pending = nreverse_pending (opts->pending);
6122 for (pend = opts->pending; pend; pend = pend->next)
6124 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6126 int fd = open (pend->arg, O_RDONLY, 0666);
6127 if (fd < 0)
6129 cpp_perror_with_name (pfile, pend->arg);
6130 return 0;
6132 if (!cpp_push_buffer (pfile, NULL, 0))
6133 return 0;
6134 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6137 pfile->no_record_file--;
6139 /* Free the pending list. */
6140 for (pend = opts->pending; pend; )
6142 struct cpp_pending *next = pend->next;
6143 free (pend);
6144 pend = next;
6146 opts->pending = NULL;
6148 #if 0
6149 /* Scan the input, processing macros and directives. */
6151 rescan (&outbuf, 0);
6153 if (missing_newline)
6154 fp->lineno--;
6156 if (CPP_PEDANTIC (pfile) && missing_newline)
6157 pedwarn ("file does not end in newline");
6159 #endif
6160 if (finclude (pfile, f, fname, 0, NULL_PTR))
6161 output_line_command (pfile, 0, same_file);
6162 return 1;
6165 void
6166 cpp_reader_init (pfile)
6167 cpp_reader *pfile;
6169 bzero ((char *) pfile, sizeof (cpp_reader));
6170 pfile->get_token = cpp_get_token;
6172 pfile->token_buffer_size = 200;
6173 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
6174 CPP_SET_WRITTEN (pfile, 0);
6176 pfile->system_include_depth = 0;
6177 pfile->dont_repeat_files = 0;
6178 pfile->all_include_files = 0;
6179 pfile->max_include_len = 0;
6180 pfile->timebuf = NULL;
6181 pfile->only_seen_white = 1;
6182 pfile->buffer = CPP_NULL_BUFFER(pfile);
6185 static struct cpp_pending *
6186 nreverse_pending (list)
6187 struct cpp_pending *list;
6190 register struct cpp_pending *prev = 0, *next, *pend;
6191 for (pend = list; pend; pend = next)
6193 next = pend->next;
6194 pend->next = prev;
6195 prev = pend;
6197 return prev;
6200 static void
6201 push_pending (pfile, cmd, arg)
6202 cpp_reader *pfile;
6203 char *cmd;
6204 char *arg;
6206 struct cpp_pending *pend
6207 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
6208 pend->cmd = cmd;
6209 pend->arg = arg;
6210 pend->next = CPP_OPTIONS (pfile)->pending;
6211 CPP_OPTIONS (pfile)->pending = pend;
6214 /* Handle command-line options in (argc, argv).
6215 Can be called multiple times, to handle multiple sets of options.
6216 Returns if an unrecognized option is seen.
6217 Returns number of handled arguments. */
6220 cpp_handle_options (pfile, argc, argv)
6221 cpp_reader *pfile;
6222 int argc;
6223 char **argv;
6225 int i;
6226 struct cpp_options *opts = CPP_OPTIONS (pfile);
6227 for (i = 0; i < argc; i++) {
6228 if (argv[i][0] != '-') {
6229 if (opts->out_fname != NULL)
6231 cpp_fatal (pfile, "Usage: %s [switches] input output", argv[0]);
6232 return argc;
6234 else if (opts->in_fname != NULL)
6235 opts->out_fname = argv[i];
6236 else
6237 opts->in_fname = argv[i];
6238 } else {
6239 switch (argv[i][1]) {
6241 missing_filename:
6242 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
6243 return argc;
6244 missing_dirname:
6245 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
6246 return argc;
6248 case 'i':
6249 if (!strcmp (argv[i], "-include")
6250 || !strcmp (argv[i], "-imacros")) {
6251 if (i + 1 == argc)
6252 goto missing_filename;
6253 else
6254 push_pending (pfile, argv[i], argv[i+1]), i++;
6256 if (!strcmp (argv[i], "-iprefix")) {
6257 if (i + 1 == argc)
6258 goto missing_filename;
6259 else
6260 opts->include_prefix = argv[++i];
6262 if (!strcmp (argv[i], "-ifoutput")) {
6263 opts->output_conditionals = 1;
6265 if (!strcmp (argv[i], "-isystem")) {
6266 struct file_name_list *dirtmp;
6268 if (i + 1 == argc)
6269 goto missing_filename;
6271 dirtmp = (struct file_name_list *)
6272 xmalloc (sizeof (struct file_name_list));
6273 dirtmp->next = 0;
6274 dirtmp->control_macro = 0;
6275 dirtmp->c_system_include_path = 1;
6276 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6277 strcpy (dirtmp->fname, argv[++i]);
6278 dirtmp->got_name_map = 0;
6280 if (opts->before_system == 0)
6281 opts->before_system = dirtmp;
6282 else
6283 opts->last_before_system->next = dirtmp;
6284 opts->last_before_system = dirtmp; /* Tail follows the last one */
6286 /* Add directory to end of path for includes,
6287 with the default prefix at the front of its name. */
6288 if (!strcmp (argv[i], "-iwithprefix")) {
6289 struct file_name_list *dirtmp;
6290 char *prefix;
6292 if (opts->include_prefix != 0)
6293 prefix = opts->include_prefix;
6294 else {
6295 prefix = savestring (GCC_INCLUDE_DIR);
6296 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6297 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6298 prefix[strlen (prefix) - 7] = 0;
6301 dirtmp = (struct file_name_list *)
6302 xmalloc (sizeof (struct file_name_list));
6303 dirtmp->next = 0; /* New one goes on the end */
6304 dirtmp->control_macro = 0;
6305 dirtmp->c_system_include_path = 0;
6306 if (i + 1 == argc)
6307 goto missing_dirname;
6309 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6310 + strlen (prefix) + 1);
6311 strcpy (dirtmp->fname, prefix);
6312 strcat (dirtmp->fname, argv[++i]);
6313 dirtmp->got_name_map = 0;
6315 if (opts->after_include == 0)
6316 opts->after_include = dirtmp;
6317 else
6318 opts->last_after_include->next = dirtmp;
6319 opts->last_after_include = dirtmp; /* Tail follows the last one */
6321 /* Add directory to main path for includes,
6322 with the default prefix at the front of its name. */
6323 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6324 struct file_name_list *dirtmp;
6325 char *prefix;
6327 if (opts->include_prefix != 0)
6328 prefix = opts->include_prefix;
6329 else {
6330 prefix = savestring (GCC_INCLUDE_DIR);
6331 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6332 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6333 prefix[strlen (prefix) - 7] = 0;
6336 dirtmp = (struct file_name_list *)
6337 xmalloc (sizeof (struct file_name_list));
6338 dirtmp->next = 0; /* New one goes on the end */
6339 dirtmp->control_macro = 0;
6340 dirtmp->c_system_include_path = 0;
6341 if (i + 1 == argc)
6342 goto missing_dirname;
6344 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6345 + strlen (prefix) + 1);
6346 strcpy (dirtmp->fname, prefix);
6347 strcat (dirtmp->fname, argv[++i]);
6348 dirtmp->got_name_map = 0;
6350 append_include_chain (pfile, dirtmp, dirtmp);
6352 /* Add directory to end of path for includes. */
6353 if (!strcmp (argv[i], "-idirafter")) {
6354 struct file_name_list *dirtmp;
6356 dirtmp = (struct file_name_list *)
6357 xmalloc (sizeof (struct file_name_list));
6358 dirtmp->next = 0; /* New one goes on the end */
6359 dirtmp->control_macro = 0;
6360 dirtmp->c_system_include_path = 0;
6361 if (i + 1 == argc)
6362 goto missing_dirname;
6363 else
6364 dirtmp->fname = argv[++i];
6365 dirtmp->got_name_map = 0;
6367 if (opts->after_include == 0)
6368 opts->after_include = dirtmp;
6369 else
6370 opts->last_after_include->next = dirtmp;
6371 opts->last_after_include = dirtmp; /* Tail follows the last one */
6373 break;
6375 case 'o':
6376 if (opts->out_fname != NULL)
6378 cpp_fatal (pfile, "Output filename specified twice");
6379 return argc;
6381 if (i + 1 == argc)
6382 goto missing_filename;
6383 opts->out_fname = argv[++i];
6384 if (!strcmp (opts->out_fname, "-"))
6385 opts->out_fname = "";
6386 break;
6388 case 'p':
6389 if (!strcmp (argv[i], "-pedantic"))
6390 CPP_PEDANTIC (pfile) = 1;
6391 else if (!strcmp (argv[i], "-pedantic-errors")) {
6392 CPP_PEDANTIC (pfile) = 1;
6393 opts->pedantic_errors = 1;
6395 #if 0
6396 else if (!strcmp (argv[i], "-pcp")) {
6397 char *pcp_fname = argv[++i];
6398 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6399 ? fopen (pcp_fname, "w")
6400 : fdopen (dup (fileno (stdout)), "w"));
6401 if (pcp_outfile == 0)
6402 cpp_pfatal_with_name (pfile, pcp_fname);
6403 no_precomp = 1;
6405 #endif
6406 break;
6408 case 't':
6409 if (!strcmp (argv[i], "-traditional")) {
6410 opts->traditional = 1;
6411 } else if (!strcmp (argv[i], "-trigraphs")) {
6412 if (!opts->chill)
6413 opts->no_trigraphs = 0;
6415 break;
6417 case 'l':
6418 if (! strcmp (argv[i], "-lang-c"))
6419 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6420 opts->objc = 0;
6421 if (! strcmp (argv[i], "-lang-c89"))
6422 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
6423 opts->objc = 0;
6424 if (! strcmp (argv[i], "-lang-c++"))
6425 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6426 opts->objc = 0;
6427 if (! strcmp (argv[i], "-lang-objc"))
6428 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6429 opts->objc = 1;
6430 if (! strcmp (argv[i], "-lang-objc++"))
6431 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6432 opts->objc = 1;
6433 if (! strcmp (argv[i], "-lang-asm"))
6434 opts->lang_asm = 1;
6435 if (! strcmp (argv[i], "-lint"))
6436 opts->for_lint = 1;
6437 if (! strcmp (argv[i], "-lang-chill"))
6438 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6439 opts->traditional = 1, opts->no_trigraphs = 1;
6440 break;
6442 case '+':
6443 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6444 break;
6446 case 'w':
6447 opts->inhibit_warnings = 1;
6448 break;
6450 case 'W':
6451 if (!strcmp (argv[i], "-Wtrigraphs"))
6452 opts->warn_trigraphs = 1;
6453 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6454 opts->warn_trigraphs = 0;
6455 else if (!strcmp (argv[i], "-Wcomment"))
6456 opts->warn_comments = 1;
6457 else if (!strcmp (argv[i], "-Wno-comment"))
6458 opts->warn_comments = 0;
6459 else if (!strcmp (argv[i], "-Wcomments"))
6460 opts->warn_comments = 1;
6461 else if (!strcmp (argv[i], "-Wno-comments"))
6462 opts->warn_comments = 0;
6463 else if (!strcmp (argv[i], "-Wtraditional"))
6464 opts->warn_stringify = 1;
6465 else if (!strcmp (argv[i], "-Wno-traditional"))
6466 opts->warn_stringify = 0;
6467 else if (!strcmp (argv[i], "-Wundef"))
6468 opts->warn_undef = 1;
6469 else if (!strcmp (argv[i], "-Wno-undef"))
6470 opts->warn_undef = 0;
6471 else if (!strcmp (argv[i], "-Wimport"))
6472 opts->warn_import = 1;
6473 else if (!strcmp (argv[i], "-Wno-import"))
6474 opts->warn_import = 0;
6475 else if (!strcmp (argv[i], "-Werror"))
6476 opts->warnings_are_errors = 1;
6477 else if (!strcmp (argv[i], "-Wno-error"))
6478 opts->warnings_are_errors = 0;
6479 else if (!strcmp (argv[i], "-Wall"))
6481 opts->warn_trigraphs = 1;
6482 opts->warn_comments = 1;
6484 break;
6486 case 'M':
6487 /* The style of the choices here is a bit mixed.
6488 The chosen scheme is a hybrid of keeping all options in one string
6489 and specifying each option in a separate argument:
6490 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6491 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6492 -M[M][G][D file]. This is awkward to handle in specs, and is not
6493 as extensible. */
6494 /* ??? -MG must be specified in addition to one of -M or -MM.
6495 This can be relaxed in the future without breaking anything.
6496 The converse isn't true. */
6498 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6499 if (!strcmp (argv[i], "-MG"))
6501 opts->print_deps_missing_files = 1;
6502 break;
6504 if (!strcmp (argv[i], "-M"))
6505 opts->print_deps = 2;
6506 else if (!strcmp (argv[i], "-MM"))
6507 opts->print_deps = 1;
6508 else if (!strcmp (argv[i], "-MD"))
6509 opts->print_deps = 2;
6510 else if (!strcmp (argv[i], "-MMD"))
6511 opts->print_deps = 1;
6512 /* For -MD and -MMD options, write deps on file named by next arg. */
6513 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6515 if (i+1 == argc)
6516 goto missing_filename;
6517 opts->deps_file = argv[++i];
6519 else
6521 /* For -M and -MM, write deps on standard output
6522 and suppress the usual output. */
6523 opts->no_output = 1;
6525 break;
6527 case 'd':
6529 char *p = argv[i] + 2;
6530 char c;
6531 while ((c = *p++) != 0) {
6532 /* Arg to -d specifies what parts of macros to dump */
6533 switch (c) {
6534 case 'M':
6535 opts->dump_macros = dump_only;
6536 opts->no_output = 1;
6537 break;
6538 case 'N':
6539 opts->dump_macros = dump_names;
6540 break;
6541 case 'D':
6542 opts->dump_macros = dump_definitions;
6543 break;
6544 case 'I':
6545 opts->dump_includes = 1;
6546 break;
6550 break;
6552 case 'g':
6553 if (argv[i][2] == '3')
6554 opts->debug_output = 1;
6555 break;
6557 case 'v':
6558 cpp_notice ("GNU CPP version %s", version_string);
6559 #ifdef TARGET_VERSION
6560 TARGET_VERSION;
6561 #endif
6562 fprintf (stderr, "\n");
6563 opts->verbose = 1;
6564 break;
6566 case 'H':
6567 opts->print_include_names = 1;
6568 break;
6570 case 'D':
6571 if (argv[i][2] != 0)
6572 push_pending (pfile, "-D", argv[i] + 2);
6573 else if (i + 1 == argc)
6575 cpp_fatal (pfile, "Macro name missing after -D option");
6576 return argc;
6578 else
6579 i++, push_pending (pfile, "-D", argv[i]);
6580 break;
6582 case 'A':
6584 char *p;
6586 if (argv[i][2] != 0)
6587 p = argv[i] + 2;
6588 else if (i + 1 == argc)
6590 cpp_fatal (pfile, "Assertion missing after -A option");
6591 return argc;
6593 else
6594 p = argv[++i];
6596 if (!strcmp (p, "-")) {
6597 struct cpp_pending **ptr;
6598 /* -A- eliminates all predefined macros and assertions.
6599 Let's include also any that were specified earlier
6600 on the command line. That way we can get rid of any
6601 that were passed automatically in from GCC. */
6602 int j;
6603 opts->inhibit_predefs = 1;
6604 for (ptr = &opts->pending; *ptr != NULL; )
6606 struct cpp_pending *pend = *ptr;
6607 if (pend->cmd && pend->cmd[0] == '-'
6608 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6610 *ptr = pend->next;
6611 free (pend);
6613 else
6614 ptr = &pend->next;
6616 } else {
6617 push_pending (pfile, "-A", p);
6620 break;
6622 case 'U': /* JF #undef something */
6623 if (argv[i][2] != 0)
6624 push_pending (pfile, "-U", argv[i] + 2);
6625 else if (i + 1 == argc)
6627 cpp_fatal (pfile, "Macro name missing after -U option");
6628 return argc;
6630 else
6631 push_pending (pfile, "-U", argv[i+1]), i++;
6632 break;
6634 case 'C':
6635 opts->put_out_comments = 1;
6636 break;
6638 case 'E': /* -E comes from cc -E; ignore it. */
6639 break;
6641 case 'P':
6642 opts->no_line_commands = 1;
6643 break;
6645 case '$': /* Don't include $ in identifiers. */
6646 opts->dollars_in_ident = 0;
6647 break;
6649 case 'I': /* Add directory to path for includes. */
6651 struct file_name_list *dirtmp;
6653 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6654 && !strcmp (argv[i] + 2, "-")) {
6655 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6656 /* Don't use any preceding -I directories for #include <...>. */
6657 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6659 else {
6660 dirtmp = (struct file_name_list *)
6661 xmalloc (sizeof (struct file_name_list));
6662 dirtmp->next = 0; /* New one goes on the end */
6663 dirtmp->control_macro = 0;
6664 dirtmp->c_system_include_path = 0;
6665 if (argv[i][2] != 0)
6666 dirtmp->fname = argv[i] + 2;
6667 else if (i + 1 == argc)
6668 goto missing_dirname;
6669 else
6670 dirtmp->fname = argv[++i];
6671 dirtmp->got_name_map = 0;
6672 append_include_chain (pfile, dirtmp, dirtmp);
6675 break;
6677 case 'n':
6678 if (!strcmp (argv[i], "-nostdinc"))
6679 /* -nostdinc causes no default include directories.
6680 You must specify all include-file directories with -I. */
6681 opts->no_standard_includes = 1;
6682 else if (!strcmp (argv[i], "-nostdinc++"))
6683 /* -nostdinc++ causes no default C++-specific include directories. */
6684 opts->no_standard_cplusplus_includes = 1;
6685 #if 0
6686 else if (!strcmp (argv[i], "-noprecomp"))
6687 no_precomp = 1;
6688 #endif
6689 break;
6691 case 'r':
6692 if (!strcmp (argv[i], "-remap"))
6693 opts->remap = 1;
6694 break;
6696 case 'u':
6697 /* Sun compiler passes undocumented switch "-undef".
6698 Let's assume it means to inhibit the predefined symbols. */
6699 opts->inhibit_predefs = 1;
6700 break;
6702 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6703 if (opts->in_fname == NULL) {
6704 opts->in_fname = "";
6705 break;
6706 } else if (opts->out_fname == NULL) {
6707 opts->out_fname = "";
6708 break;
6709 } /* else fall through into error */
6711 default:
6712 return i;
6716 return i;
6719 void
6720 cpp_finish (pfile)
6721 cpp_reader *pfile;
6723 struct cpp_options *opts = CPP_OPTIONS (pfile);
6725 if (opts->print_deps)
6727 /* Stream on which to print the dependency information. */
6728 FILE *deps_stream;
6730 /* Don't actually write the deps file if compilation has failed. */
6731 if (pfile->errors == 0)
6733 char *deps_mode = opts->print_deps_append ? "a" : "w";
6734 if (opts->deps_file == 0)
6735 deps_stream = stdout;
6736 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6737 cpp_pfatal_with_name (pfile, opts->deps_file);
6738 fputs (pfile->deps_buffer, deps_stream);
6739 putc ('\n', deps_stream);
6740 if (opts->deps_file)
6742 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6743 cpp_fatal (pfile, "I/O error on output");
6749 /* Free resources used by PFILE.
6750 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6752 void
6753 cpp_cleanup (pfile)
6754 cpp_reader *pfile;
6756 int i;
6757 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6758 cpp_pop_buffer (pfile);
6760 if (pfile->token_buffer)
6762 free (pfile->token_buffer);
6763 pfile->token_buffer = NULL;
6766 if (pfile->deps_buffer)
6768 free (pfile->deps_buffer);
6769 pfile->deps_buffer = NULL;
6770 pfile->deps_allocated_size = 0;
6773 while (pfile->if_stack)
6775 IF_STACK_FRAME *temp = pfile->if_stack;
6776 pfile->if_stack = temp->next;
6777 free (temp);
6780 while (pfile->dont_repeat_files)
6782 struct file_name_list *temp = pfile->dont_repeat_files;
6783 pfile->dont_repeat_files = temp->next;
6784 free (temp->fname);
6785 free (temp);
6788 while (pfile->all_include_files)
6790 struct file_name_list *temp = pfile->all_include_files;
6791 pfile->all_include_files = temp->next;
6792 free (temp->fname);
6793 free (temp);
6796 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6798 register struct import_file *imp = pfile->import_hash_table[i];
6799 while (imp)
6801 struct import_file *next = imp->next;
6802 free (imp->name);
6803 free (imp);
6804 imp = next;
6806 pfile->import_hash_table[i] = 0;
6809 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6811 while (pfile->assertion_hashtab[i])
6812 delete_assertion (pfile->assertion_hashtab[i]);
6815 cpp_hash_cleanup (pfile);
6818 static int
6819 do_assert (pfile, keyword, buf, limit)
6820 cpp_reader *pfile;
6821 struct directive *keyword;
6822 U_CHAR *buf, *limit;
6824 long symstart; /* remember where symbol name starts */
6825 int c;
6826 int sym_length; /* and how long it is */
6827 struct arglist *tokens = NULL;
6829 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6830 && !CPP_BUFFER (pfile)->system_header_p)
6831 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6833 cpp_skip_hspace (pfile);
6834 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6835 parse_name (pfile, GETC());
6836 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart, 1);
6838 cpp_skip_hspace (pfile);
6839 if (PEEKC() != '(') {
6840 cpp_error (pfile, "missing token-sequence in `#assert'");
6841 goto error;
6845 int error_flag = 0;
6846 tokens = read_token_list (pfile, &error_flag);
6847 if (error_flag)
6848 goto error;
6849 if (tokens == 0) {
6850 cpp_error (pfile, "empty token-sequence in `#assert'");
6851 goto error;
6853 cpp_skip_hspace (pfile);
6854 c = PEEKC ();
6855 if (c != EOF && c != '\n')
6856 cpp_pedwarn (pfile, "junk at end of `#assert'");
6857 skip_rest_of_line (pfile);
6860 /* If this name isn't already an assertion name, make it one.
6861 Error if it was already in use in some other way. */
6864 ASSERTION_HASHNODE *hp;
6865 U_CHAR *symname = pfile->token_buffer + symstart;
6866 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6867 struct tokenlist_list *value
6868 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6870 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6871 if (hp == NULL) {
6872 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6873 cpp_error (pfile, "`defined' redefined as assertion");
6874 hp = assertion_install (pfile, symname, sym_length, hashcode);
6877 /* Add the spec'd token-sequence to the list of such. */
6878 value->tokens = tokens;
6879 value->next = hp->value;
6880 hp->value = value;
6882 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6883 return 0;
6884 error:
6885 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6886 skip_rest_of_line (pfile);
6887 return 1;
6890 static int
6891 do_unassert (pfile, keyword, buf, limit)
6892 cpp_reader *pfile;
6893 struct directive *keyword;
6894 U_CHAR *buf, *limit;
6896 long symstart; /* remember where symbol name starts */
6897 int sym_length; /* and how long it is */
6898 int c;
6900 struct arglist *tokens = NULL;
6901 int tokens_specified = 0;
6903 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6904 && !CPP_BUFFER (pfile)->system_header_p)
6905 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6907 cpp_skip_hspace (pfile);
6909 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6910 parse_name (pfile, GETC());
6911 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart, 1);
6913 cpp_skip_hspace (pfile);
6914 if (PEEKC() == '(') {
6915 int error_flag = 0;
6917 tokens = read_token_list (pfile, &error_flag);
6918 if (error_flag)
6919 goto error;
6920 if (tokens == 0) {
6921 cpp_error (pfile, "empty token list in `#unassert'");
6922 goto error;
6925 tokens_specified = 1;
6928 cpp_skip_hspace (pfile);
6929 c = PEEKC ();
6930 if (c != EOF && c != '\n')
6931 cpp_error (pfile, "junk at end of `#unassert'");
6932 skip_rest_of_line (pfile);
6935 ASSERTION_HASHNODE *hp;
6936 U_CHAR *symname = pfile->token_buffer + symstart;
6937 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6938 struct tokenlist_list *tail, *prev;
6940 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6941 if (hp == NULL)
6942 return 1;
6944 /* If no token list was specified, then eliminate this assertion
6945 entirely. */
6946 if (! tokens_specified)
6947 delete_assertion (hp);
6948 else {
6949 /* If a list of tokens was given, then delete any matching list. */
6951 tail = hp->value;
6952 prev = 0;
6953 while (tail) {
6954 struct tokenlist_list *next = tail->next;
6955 if (compare_token_lists (tail->tokens, tokens)) {
6956 if (prev)
6957 prev->next = next;
6958 else
6959 hp->value = tail->next;
6960 free_token_list (tail->tokens);
6961 free (tail);
6962 } else {
6963 prev = tail;
6965 tail = next;
6970 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6971 return 0;
6972 error:
6973 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6974 skip_rest_of_line (pfile);
6975 return 1;
6978 /* Test whether there is an assertion named NAME
6979 and optionally whether it has an asserted token list TOKENS.
6980 NAME is not null terminated; its length is SYM_LENGTH.
6981 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6984 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
6985 cpp_reader *pfile;
6986 U_CHAR *name;
6987 int sym_length;
6988 int tokens_specified;
6989 struct arglist *tokens;
6991 ASSERTION_HASHNODE *hp;
6992 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6994 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
6995 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
6997 hp = assertion_lookup (pfile, name, sym_length, hashcode);
6998 if (hp == NULL)
6999 /* It is not an assertion; just return false. */
7000 return 0;
7002 /* If no token list was specified, then value is 1. */
7003 if (! tokens_specified)
7004 return 1;
7007 struct tokenlist_list *tail;
7009 tail = hp->value;
7011 /* If a list of tokens was given,
7012 then succeed if the assertion records a matching list. */
7014 while (tail) {
7015 if (compare_token_lists (tail->tokens, tokens))
7016 return 1;
7017 tail = tail->next;
7020 /* Fail if the assertion has no matching list. */
7021 return 0;
7025 /* Compare two lists of tokens for equality including order of tokens. */
7027 static int
7028 compare_token_lists (l1, l2)
7029 struct arglist *l1, *l2;
7031 while (l1 && l2) {
7032 if (l1->length != l2->length)
7033 return 0;
7034 if (strncmp (l1->name, l2->name, l1->length))
7035 return 0;
7036 l1 = l1->next;
7037 l2 = l2->next;
7040 /* Succeed if both lists end at the same time. */
7041 return l1 == l2;
7044 struct arglist *
7045 reverse_token_list (tokens)
7046 struct arglist *tokens;
7048 register struct arglist *prev = 0, *this, *next;
7049 for (this = tokens; this; this = next)
7051 next = this->next;
7052 this->next = prev;
7053 prev = this;
7055 return prev;
7058 /* Read a space-separated list of tokens ending in a close parenthesis.
7059 Return a list of strings, in the order they were written.
7060 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7062 static struct arglist *
7063 read_token_list (pfile, error_flag)
7064 cpp_reader *pfile;
7065 int *error_flag;
7067 struct arglist *token_ptrs = 0;
7068 int depth = 1;
7069 int length;
7071 *error_flag = 0;
7072 FORWARD (1); /* Skip '(' */
7074 /* Loop over the assertion value tokens. */
7075 while (depth > 0)
7077 struct arglist *temp;
7078 long name_written = CPP_WRITTEN (pfile);
7079 int eofp = 0; int c;
7081 cpp_skip_hspace (pfile);
7083 c = GETC ();
7085 /* Find the end of the token. */
7086 if (c == '(')
7088 CPP_PUTC (pfile, c);
7089 depth++;
7091 else if (c == ')')
7093 depth--;
7094 if (depth == 0)
7095 break;
7096 CPP_PUTC (pfile, c);
7098 else if (c == '"' || c == '\'')
7100 FORWARD(-1);
7101 cpp_get_token (pfile);
7103 else if (c == '\n')
7104 break;
7105 else
7107 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7108 && c != '"' && c != '\'')
7110 CPP_PUTC (pfile, c);
7111 c = GETC();
7113 if (c != EOF) FORWARD(-1);
7116 length = CPP_WRITTEN (pfile) - name_written;
7117 temp = (struct arglist *)
7118 xmalloc (sizeof (struct arglist) + length + 1);
7119 temp->name = (U_CHAR *) (temp + 1);
7120 bcopy ((char *) (pfile->token_buffer + name_written),
7121 (char *) temp->name, length);
7122 temp->name[length] = 0;
7123 temp->next = token_ptrs;
7124 token_ptrs = temp;
7125 temp->length = length;
7127 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7129 if (c == EOF || c == '\n')
7130 { /* FIXME */
7131 cpp_error (pfile,
7132 "unterminated token sequence following `#' operator");
7133 return 0;
7137 /* We accumulated the names in reverse order.
7138 Now reverse them to get the proper order. */
7139 return reverse_token_list (token_ptrs);
7142 static void
7143 free_token_list (tokens)
7144 struct arglist *tokens;
7146 while (tokens) {
7147 struct arglist *next = tokens->next;
7148 free (tokens->name);
7149 free (tokens);
7150 tokens = next;
7154 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7155 retrying if necessary. If MAX_READ_LEN is defined, read at most
7156 that bytes at a time. Return a negative value if an error occurs,
7157 otherwise return the actual number of bytes read,
7158 which must be LEN unless end-of-file was reached. */
7160 static int
7161 safe_read (desc, ptr, len)
7162 int desc;
7163 char *ptr;
7164 int len;
7166 int left, rcount, nchars;
7168 left = len;
7169 while (left > 0) {
7170 rcount = left;
7171 #ifdef MAX_READ_LEN
7172 if (rcount > MAX_READ_LEN)
7173 rcount = MAX_READ_LEN;
7174 #endif
7175 nchars = read (desc, ptr, rcount);
7176 if (nchars < 0)
7178 #ifdef EINTR
7179 if (errno == EINTR)
7180 continue;
7181 #endif
7182 return nchars;
7184 if (nchars == 0)
7185 break;
7186 ptr += nchars;
7187 left -= nchars;
7189 return len - left;
7192 static char *
7193 xcalloc (number, size)
7194 unsigned number, size;
7196 register unsigned total = number * size;
7197 register char *ptr = (char *) xmalloc (total);
7198 bzero (ptr, total);
7199 return ptr;
7202 static char *
7203 savestring (input)
7204 char *input;
7206 unsigned size = strlen (input);
7207 char *output = xmalloc (size + 1);
7208 strcpy (output, input);
7209 return output;
7212 /* Initialize PMARK to remember the current position of PFILE. */
7214 void
7215 parse_set_mark (pmark, pfile)
7216 struct parse_marker *pmark;
7217 cpp_reader *pfile;
7219 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7220 pmark->next = pbuf->marks;
7221 pbuf->marks = pmark;
7222 pmark->buf = pbuf;
7223 pmark->position = pbuf->cur - pbuf->buf;
7226 /* Cleanup PMARK - we no longer need it. */
7228 void
7229 parse_clear_mark (pmark)
7230 struct parse_marker *pmark;
7232 struct parse_marker **pp = &pmark->buf->marks;
7233 for (; ; pp = &(*pp)->next) {
7234 if (*pp == NULL) abort ();
7235 if (*pp == pmark) break;
7237 *pp = pmark->next;
7240 /* Backup the current position of PFILE to that saved in PMARK. */
7242 void
7243 parse_goto_mark (pmark, pfile)
7244 struct parse_marker *pmark;
7245 cpp_reader *pfile;
7247 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7248 if (pbuf != pmark->buf)
7249 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
7250 pbuf->cur = pbuf->buf + pmark->position;
7253 /* Reset PMARK to point to the current position of PFILE. (Same
7254 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7256 void
7257 parse_move_mark (pmark, pfile)
7258 struct parse_marker *pmark;
7259 cpp_reader *pfile;
7261 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7262 if (pbuf != pmark->buf)
7263 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
7264 pmark->position = pbuf->cur - pbuf->buf;
7268 cpp_read_check_assertion (pfile)
7269 cpp_reader *pfile;
7271 int name_start = CPP_WRITTEN (pfile);
7272 int name_length, name_written;
7273 int result;
7274 FORWARD (1); /* Skip '#' */
7275 cpp_skip_hspace (pfile);
7276 parse_name (pfile, GETC ());
7277 name_written = CPP_WRITTEN (pfile);
7278 name_length = name_written - name_start;
7279 cpp_skip_hspace (pfile);
7280 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7282 int error_flag;
7283 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7284 result = check_assertion (pfile,
7285 pfile->token_buffer + name_start, name_length,
7286 1, token_ptrs);
7288 else
7289 result = check_assertion (pfile,
7290 pfile->token_buffer + name_start, name_length,
7291 0, NULL_PTR);
7292 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7293 return result;
7296 void
7297 cpp_print_file_and_line (pfile)
7298 cpp_reader *pfile;
7300 cpp_buffer *ip = cpp_file_buffer (pfile);
7302 if (ip != NULL)
7304 long line, col;
7305 cpp_buf_line_and_col (ip, &line, &col);
7306 cpp_file_line_for_message (pfile, ip->nominal_fname,
7307 line, pfile->show_column ? col : -1);
7311 void
7312 cpp_error (pfile, msgid, arg1, arg2, arg3)
7313 cpp_reader *pfile;
7314 char *msgid;
7315 char *arg1, *arg2, *arg3;
7317 cpp_print_containing_files (pfile);
7318 cpp_print_file_and_line (pfile);
7319 cpp_message (pfile, 1, _(msgid), arg1, arg2, arg3);
7322 /* Print error message but don't count it. */
7324 void
7325 cpp_warning (pfile, msgid, arg1, arg2, arg3)
7326 cpp_reader *pfile;
7327 char *msgid;
7328 char *arg1, *arg2, *arg3;
7330 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7331 return;
7333 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7334 pfile->errors++;
7336 cpp_print_containing_files (pfile);
7337 cpp_print_file_and_line (pfile);
7338 cpp_message (pfile, 0, _(msgid), arg1, arg2, arg3);
7341 /* Print an error message. */
7343 void
7344 cpp_notice (msgid, arg1, arg2, arg3)
7345 char *msgid;
7346 char *arg1, *arg2, *arg3;
7348 cpp_message ((cpp_reader *) 0, -1, _(msgid), arg1, arg2, arg3);
7351 /* Print an error message and maybe count it. */
7353 void
7354 cpp_pedwarn (pfile, msgid, arg1, arg2, arg3)
7355 cpp_reader *pfile;
7356 char *msgid;
7357 char *arg1, *arg2, *arg3;
7359 if (CPP_OPTIONS (pfile)->pedantic_errors)
7360 cpp_error (pfile, msgid, arg1, arg2, arg3);
7361 else
7362 cpp_warning (pfile, msgid, arg1, arg2, arg3);
7365 void
7366 cpp_error_with_line (pfile, line, column, msgid, arg1, arg2, arg3)
7367 cpp_reader *pfile;
7368 int line, column;
7369 char *msgid;
7370 char *arg1, *arg2, *arg3;
7372 int i;
7373 cpp_buffer *ip = cpp_file_buffer (pfile);
7375 cpp_print_containing_files (pfile);
7377 if (ip != NULL)
7378 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7380 cpp_message (pfile, 1, _(msgid), arg1, arg2, arg3);
7383 static void
7384 cpp_warning_with_line (pfile, line, column, msgid, arg1, arg2, arg3)
7385 cpp_reader *pfile;
7386 int line, column;
7387 char *msgid;
7388 char *arg1, *arg2, *arg3;
7390 int i;
7391 cpp_buffer *ip;
7393 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7394 return;
7396 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7397 pfile->errors++;
7399 cpp_print_containing_files (pfile);
7401 ip = cpp_file_buffer (pfile);
7403 if (ip != NULL)
7404 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7406 cpp_message (pfile, 0, _(msgid), arg1, arg2, arg3);
7409 void
7410 cpp_pedwarn_with_line (pfile, line, column, msgid, arg1, arg2, arg3)
7411 cpp_reader *pfile;
7412 int line;
7413 char *msgid;
7414 char *arg1, *arg2, *arg3;
7416 if (CPP_OPTIONS (pfile)->pedantic_errors)
7417 cpp_error_with_line (pfile, column, line, msgid, arg1, arg2, arg3);
7418 else
7419 cpp_warning_with_line (pfile, line, column, msgid, arg1, arg2, arg3);
7422 /* Report a warning (or an error if pedantic_errors)
7423 giving specified file name and line number, not current. */
7425 void
7426 cpp_pedwarn_with_file_and_line (pfile, file, line, msgid, arg1, arg2, arg3)
7427 cpp_reader *pfile;
7428 char *file;
7429 int line;
7430 char *msgid;
7431 char *arg1, *arg2, *arg3;
7433 if (!CPP_OPTIONS (pfile)->pedantic_errors
7434 && CPP_OPTIONS (pfile)->inhibit_warnings)
7435 return;
7436 if (file != NULL)
7437 cpp_file_line_for_message (pfile, file, line, -1);
7438 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7439 _(msgid), arg1, arg2, arg3);
7442 /* This defines "errno" properly for VMS, and gives us EACCES. */
7443 #include <errno.h>
7444 #ifndef errno
7445 extern int errno;
7446 #endif
7448 #ifndef VMS
7449 #ifndef HAVE_STRERROR
7450 extern int sys_nerr;
7451 extern char *sys_errlist[];
7452 #else /* HAVE_STRERROR */
7453 char *strerror ();
7454 #endif
7455 #else /* VMS */
7456 char *strerror (int,...);
7457 #endif
7459 /* my_strerror - return the descriptive text associated with an
7460 `errno' code. */
7462 char *
7463 my_strerror (errnum)
7464 int errnum;
7466 char *result;
7468 #ifndef VMS
7469 #ifndef HAVE_STRERROR
7470 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7471 #else
7472 result = strerror (errnum);
7473 #endif
7474 #else /* VMS */
7475 /* VAXCRTL's strerror() takes an optional second argument, which only
7476 matters when the first argument is EVMSERR. However, it's simplest
7477 just to pass it unconditionally. `vaxc$errno' is declared in
7478 <errno.h>, and maintained by the library in parallel with `errno'.
7479 We assume that caller's `errnum' either matches the last setting of
7480 `errno' by the library or else does not have the value `EVMSERR'. */
7482 result = strerror (errnum, vaxc$errno);
7483 #endif
7485 if (!result)
7486 result = "errno = ?";
7488 return result;
7491 /* Error including a message from `errno'. */
7493 void
7494 cpp_error_from_errno (pfile, name)
7495 cpp_reader *pfile;
7496 char *name;
7498 int e = errno;
7499 int i;
7500 cpp_buffer *ip = cpp_file_buffer (pfile);
7502 cpp_print_containing_files (pfile);
7504 if (ip != NULL)
7505 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7507 cpp_message (pfile, 1, "%s: %s", name, my_strerror (e));
7510 void
7511 cpp_perror_with_name (pfile, name)
7512 cpp_reader *pfile;
7513 char *name;
7515 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7518 /* TODO:
7519 * No pre-compiled header file support.
7521 * Possibly different enum token codes for each C/C++ token.
7523 * Should clean up remaining directives to that do_XXX functions
7524 * only take two arguments and all have command_reads_line.
7526 * Find and cleanup remaining uses of static variables,
7528 * Support for trigraphs.
7530 * Support -dM flag (dump_all_macros).
7532 * Support for_lint flag.