(ASM_SPEC): Change {% to %{.
[official-gcc.git] / gcc / cpplib.c
blob6719c82adb278cc783d34bd4f64f9457ffc8965a
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-5, 1996 Free Software Foundation, Inc.
3 Written by Per Bothner, 1994-95.
4 Based on CCCP program by 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 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 #ifdef EMACS
26 #define NO_SHORTNAMES
27 #include "../src/config.h"
28 #ifdef open
29 #undef open
30 #undef read
31 #undef write
32 #endif /* open */
33 #endif /* EMACS */
35 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
36 for the sake of machines with limited C compilers. */
37 #ifndef EMACS
38 #include "config.h"
39 #endif /* not EMACS */
41 #ifndef STANDARD_INCLUDE_DIR
42 #define STANDARD_INCLUDE_DIR "/usr/include"
43 #endif
45 #ifndef LOCAL_INCLUDE_DIR
46 #define LOCAL_INCLUDE_DIR "/usr/local/include"
47 #endif
49 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
50 #ifdef __STDC__
51 #define PTR_INT_TYPE ptrdiff_t
52 #else
53 #define PTR_INT_TYPE long
54 #endif
55 #endif /* 0 */
57 #include "cpplib.h"
58 #include "cpphash.h"
60 #ifndef STDC_VALUE
61 #define STDC_VALUE 1
62 #endif
64 /* By default, colon separates directories in a path. */
65 #ifndef PATH_SEPARATOR
66 #define PATH_SEPARATOR ':'
67 #endif
69 #include <ctype.h>
70 #include <stdio.h>
71 #include <signal.h>
72 #ifdef __STDC__
73 #include <stdlib.h>
74 #endif
76 #ifndef VMS
77 #ifndef USG
78 #include <sys/time.h> /* for __DATE__ and __TIME__ */
79 #include <sys/resource.h>
80 #else
81 #include <sys/times.h>
82 #include <time.h>
83 #include <fcntl.h>
84 #endif /* USG */
85 #endif /* not VMS */
87 /* This defines "errno" properly for VMS, and gives us EACCES. */
88 #include <errno.h>
90 extern char *index ();
91 extern char *rindex ();
93 #ifndef O_RDONLY
94 #define O_RDONLY 0
95 #endif
97 #undef MIN
98 #undef MAX
99 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
100 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
102 /* Find the largest host integer type and set its size and type. */
104 #ifndef HOST_BITS_PER_WIDE_INT
106 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
107 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
108 #define HOST_WIDE_INT long
109 #else
110 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
111 #define HOST_WIDE_INT int
112 #endif
114 #endif
116 #ifndef S_ISREG
117 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
118 #endif
120 #ifndef S_ISDIR
121 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
122 #endif
124 /* Define a generic NULL if one hasn't already been defined. */
126 #ifndef NULL
127 #define NULL 0
128 #endif
130 #ifndef GENERIC_PTR
131 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
132 #define GENERIC_PTR void *
133 #else
134 #define GENERIC_PTR char *
135 #endif
136 #endif
138 #ifndef NULL_PTR
139 #define NULL_PTR ((GENERIC_PTR) 0)
140 #endif
142 #ifndef INCLUDE_LEN_FUDGE
143 #define INCLUDE_LEN_FUDGE 0
144 #endif
146 /* Symbols to predefine. */
148 #ifdef CPP_PREDEFINES
149 static char *predefs = CPP_PREDEFINES;
150 #else
151 static char *predefs = "";
152 #endif
154 /* We let tm.h override the types used here, to handle trivial differences
155 such as the choice of unsigned int or long unsigned int for size_t.
156 When machines start needing nontrivial differences in the size type,
157 it would be best to do something here to figure out automatically
158 from other information what type to use. */
160 /* The string value for __SIZE_TYPE__. */
162 #ifndef SIZE_TYPE
163 #define SIZE_TYPE "long unsigned int"
164 #endif
166 /* The string value for __PTRDIFF_TYPE__. */
168 #ifndef PTRDIFF_TYPE
169 #define PTRDIFF_TYPE "long int"
170 #endif
172 /* The string value for __WCHAR_TYPE__. */
174 #ifndef WCHAR_TYPE
175 #define WCHAR_TYPE "int"
176 #endif
177 #define CPP_WCHAR_TYPE(PFILE) \
178 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
180 /* The string value for __USER_LABEL_PREFIX__ */
182 #ifndef USER_LABEL_PREFIX
183 #define USER_LABEL_PREFIX ""
184 #endif
186 /* The string value for __REGISTER_PREFIX__ */
188 #ifndef REGISTER_PREFIX
189 #define REGISTER_PREFIX ""
190 #endif
192 /* In the definition of a #assert name, this structure forms
193 a list of the individual values asserted.
194 Each value is itself a list of "tokens".
195 These are strings that are compared by name. */
197 struct tokenlist_list {
198 struct tokenlist_list *next;
199 struct arglist *tokens;
202 struct assertion_hashnode {
203 struct assertion_hashnode *next; /* double links for easy deletion */
204 struct assertion_hashnode *prev;
205 /* also, a back pointer to this node's hash
206 chain is kept, in case the node is the head
207 of the chain and gets deleted. */
208 struct assertion_hashnode **bucket_hdr;
209 int length; /* length of token, for quick comparison */
210 U_CHAR *name; /* the actual name */
211 /* List of token-sequences. */
212 struct tokenlist_list *value;
215 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
216 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
218 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
219 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
220 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
221 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
222 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
223 (Note that it is false while we're expanding marco *arguments*.) */
224 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
226 /* Move all backslash-newline pairs out of embarrassing places.
227 Exchange all such pairs following BP
228 with any potentially-embarrassing characters that follow them.
229 Potentially-embarrassing characters are / and *
230 (because a backslash-newline inside a comment delimiter
231 would cause it not to be recognized). */
233 #define NEWLINE_FIX \
234 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
236 /* Same, but assume we've already read the potential '\\' into C. */
237 #define NEWLINE_FIX1(C) do { \
238 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
239 } while(0)
241 struct cpp_pending {
242 struct cpp_pending *next;
243 char *cmd;
244 char *arg;
247 /* Forward declarations. */
249 extern char *xmalloc ();
251 static void add_import ();
252 static void append_include_chain ();
253 static void make_undef ();
254 static void make_assertion ();
255 static void path_include ();
256 static void initialize_builtins ();
257 static void initialize_char_syntax ();
258 static void dump_arg_n ();
259 static void dump_defn_1 ();
260 extern void delete_macro ();
261 static void trigraph_pcp ();
262 static int finclude ();
263 static void validate_else ();
264 static int comp_def_part ();
265 #ifdef abort
266 extern void fancy_abort ();
267 #endif
268 static void pipe_closed ();
269 static void print_containing_files ();
270 static int lookup_import ();
271 static int redundant_include_p ();
272 static is_system_include ();
273 static struct file_name_map *read_name_map ();
274 static char *read_filename_string ();
275 static int open_include_file ();
276 static int check_preconditions ();
277 static void pcfinclude ();
278 static void pcstring_used ();
279 static int check_macro_name ();
280 static int compare_defs ();
281 static int compare_token_lists ();
282 static HOST_WIDE_INT eval_if_expression ();
283 static int change_newlines ();
284 extern int hashf ();
285 static int file_size_and_mode ();
286 static struct arglist *read_token_list ();
287 static void free_token_list ();
288 static int safe_read ();
289 static void push_macro_expansion PARAMS ((cpp_reader *,
290 U_CHAR *, int, HASHNODE *));
291 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
292 extern char *xrealloc ();
293 static char *xcalloc ();
294 static char *savestring ();
296 static void conditional_skip ();
297 static void skip_if_group ();
299 /* Last arg to output_line_command. */
300 enum file_change_code {same_file, enter_file, leave_file};
302 /* External declarations. */
304 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
306 extern char *getenv ();
307 extern FILE *fdopen ();
308 extern char *version_string;
309 extern struct tm *localtime ();
311 /* These functions are declared to return int instead of void since they
312 are going to be placed in a table and some old compilers have trouble with
313 pointers to functions returning void. */
315 static int do_define ();
316 static int do_line ();
317 static int do_include ();
318 static int do_undef ();
319 static int do_error ();
320 static int do_pragma ();
321 static int do_ident ();
322 static int do_if ();
323 static int do_xifdef ();
324 static int do_else ();
325 static int do_elif ();
326 static int do_endif ();
327 static int do_sccs ();
328 static int do_once ();
329 static int do_assert ();
330 static int do_unassert ();
331 static int do_warning ();
333 struct file_name_list
335 struct file_name_list *next;
336 char *fname;
337 /* If the following is nonzero, it is a macro name.
338 Don't include the file again if that macro is defined. */
339 U_CHAR *control_macro;
340 /* If the following is nonzero, it is a C-language system include
341 directory. */
342 int c_system_include_path;
343 /* Mapping of file names for this directory. */
344 struct file_name_map *name_map;
345 /* Non-zero if name_map is valid. */
346 int got_name_map;
349 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
350 via the same directory as the file that #included it. */
351 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
353 /* #include "file" looks in source file dir, then stack. */
354 /* #include <file> just looks in the stack. */
355 /* -I directories are added to the end, then the defaults are added. */
356 /* The */
357 static struct default_include {
358 char *fname; /* The name of the directory. */
359 int cplusplus; /* Only look here if we're compiling C++. */
360 int cxx_aware; /* Includes in this directory don't need to
361 be wrapped in extern "C" when compiling
362 C++. */
363 } include_defaults_array[]
364 #ifdef INCLUDE_DEFAULTS
365 = INCLUDE_DEFAULTS;
366 #else
368 /* Pick up GNU C++ specific include files. */
369 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
370 { OLD_GPLUSPLUS_INCLUDE_DIR, 1, 1 },
371 #ifdef CROSS_COMPILE
372 /* This is the dir for fixincludes. Put it just before
373 the files that we fix. */
374 { GCC_INCLUDE_DIR, 0, 0 },
375 /* For cross-compilation, this dir name is generated
376 automatically in Makefile.in. */
377 { CROSS_INCLUDE_DIR, 0, 0 },
378 /* This is another place that the target system's headers might be. */
379 { TOOL_INCLUDE_DIR, 0, 1 },
380 #else /* not CROSS_COMPILE */
381 /* This should be /usr/local/include and should come before
382 the fixincludes-fixed header files. */
383 { LOCAL_INCLUDE_DIR, 0, 1 },
384 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
385 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
386 { TOOL_INCLUDE_DIR, 0, 1 },
387 /* This is the dir for fixincludes. Put it just before
388 the files that we fix. */
389 { GCC_INCLUDE_DIR, 0, 0 },
390 /* Some systems have an extra dir of include files. */
391 #ifdef SYSTEM_INCLUDE_DIR
392 { SYSTEM_INCLUDE_DIR, 0, 0 },
393 #endif
394 { STANDARD_INCLUDE_DIR, 0, 0 },
395 #endif /* not CROSS_COMPILE */
396 { 0, 0, 0 }
398 #endif /* no INCLUDE_DEFAULTS */
400 /* `struct directive' defines one #-directive, including how to handle it. */
402 struct directive {
403 int length; /* Length of name */
404 int (*func)(); /* Function to handle directive */
405 char *name; /* Name of directive */
406 enum node_type type; /* Code which describes which directive. */
407 char command_reads_line; /* One if rest of line is read by func. */
408 char traditional_comments; /* Nonzero: keep comments if -traditional. */
409 char pass_thru; /* Copy preprocessed directive to output file.*/
412 /* Here is the actual list of #-directives, most-often-used first.
413 The initialize_builtins function assumes #define is the very first. */
415 static struct directive directive_table[] = {
416 { 6, do_define, "define", T_DEFINE, 0, 1},
417 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
418 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
419 { 7, do_include, "include", T_INCLUDE, 1},
420 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
421 { 6, do_include, "import", T_IMPORT, 1},
422 { 5, do_endif, "endif", T_ENDIF, 1},
423 { 4, do_else, "else", T_ELSE, 1},
424 { 2, do_if, "if", T_IF, 1},
425 { 4, do_elif, "elif", T_ELIF, 1},
426 { 5, do_undef, "undef", T_UNDEF},
427 { 5, do_error, "error", T_ERROR},
428 { 7, do_warning, "warning", T_WARNING},
429 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
430 { 4, do_line, "line", T_LINE, 1},
431 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
432 #ifdef SCCS_DIRECTIVE
433 { 4, do_sccs, "sccs", T_SCCS},
434 #endif
435 { 6, do_assert, "assert", T_ASSERT, 1},
436 { 8, do_unassert, "unassert", T_UNASSERT, 1},
437 { -1, 0, "", T_UNUSED},
440 /* table to tell if char can be part of a C identifier. */
441 U_CHAR is_idchar[256];
442 /* table to tell if char can be first char of a c identifier. */
443 U_CHAR is_idstart[256];
444 /* table to tell if c is horizontal space. */
445 U_CHAR is_hor_space[256];
446 /* table to tell if c is horizontal or vertical space. */
447 static U_CHAR is_space[256];
449 /* Initialize syntactic classifications of characters. */
451 static void
452 initialize_char_syntax (opts)
453 struct cpp_options *opts;
455 register int i;
458 * Set up is_idchar and is_idstart tables. These should be
459 * faster than saying (is_alpha (c) || c == '_'), etc.
460 * Set up these things before calling any routines tthat
461 * refer to them.
463 for (i = 'a'; i <= 'z'; i++) {
464 is_idchar[i - 'a' + 'A'] = 1;
465 is_idchar[i] = 1;
466 is_idstart[i - 'a' + 'A'] = 1;
467 is_idstart[i] = 1;
469 for (i = '0'; i <= '9'; i++)
470 is_idchar[i] = 1;
471 is_idchar['_'] = 1;
472 is_idstart['_'] = 1;
473 is_idchar['$'] = opts->dollars_in_ident;
474 is_idstart['$'] = opts->dollars_in_ident;
476 /* horizontal space table */
477 is_hor_space[' '] = 1;
478 is_hor_space['\t'] = 1;
479 is_hor_space['\v'] = 1;
480 is_hor_space['\f'] = 1;
481 is_hor_space['\r'] = 1;
483 is_space[' '] = 1;
484 is_space['\t'] = 1;
485 is_space['\v'] = 1;
486 is_space['\f'] = 1;
487 is_space['\n'] = 1;
488 is_space['\r'] = 1;
492 /* Place into PFILE a quoted string representing the string SRC.
493 Caller must reserve enough space in pfile->token_buffer. */
495 static void
496 quote_string (pfile, src)
497 cpp_reader *pfile;
498 char *src;
500 U_CHAR c;
502 CPP_PUTC_Q (pfile, '\"');
503 for (;;)
504 switch ((c = *src++))
506 default:
507 if (isprint (c))
508 CPP_PUTC_Q (pfile, c);
509 else
511 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
512 CPP_ADJUST_WRITTEN (pfile, 4);
514 break;
516 case '\"':
517 case '\\':
518 CPP_PUTC_Q (pfile, '\\');
519 CPP_PUTC_Q (pfile, c);
520 break;
522 case '\0':
523 CPP_PUTC_Q (pfile, '\"');
524 CPP_NUL_TERMINATE_Q (pfile);
525 return;
529 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
531 void
532 cpp_grow_buffer (pfile, n)
533 cpp_reader *pfile;
534 long n;
536 long old_written = CPP_WRITTEN (pfile);
537 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
538 pfile->token_buffer = (U_CHAR *)
539 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
540 CPP_SET_WRITTEN (pfile, old_written);
545 * process a given definition string, for initialization
546 * If STR is just an identifier, define it with value 1.
547 * If STR has anything after the identifier, then it should
548 * be identifier=definition.
551 void
552 cpp_define (pfile, str)
553 cpp_reader *pfile;
554 U_CHAR *str;
556 U_CHAR *buf, *p;
558 buf = str;
559 p = str;
560 if (!is_idstart[*p])
562 cpp_error (pfile, "malformed option `-D %s'", str);
563 return;
565 while (is_idchar[*++p])
567 if (*p == 0)
569 buf = (U_CHAR *) alloca (p - buf + 4);
570 strcpy ((char *)buf, str);
571 strcat ((char *)buf, " 1");
573 else if (*p != '=')
575 cpp_error (pfile, "malformed option `-D %s'", str);
576 return;
578 else
580 U_CHAR *q;
581 /* Copy the entire option so we can modify it. */
582 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
583 strncpy (buf, str, p - str);
584 /* Change the = to a space. */
585 buf[p - str] = ' ';
586 /* Scan for any backslash-newline and remove it. */
587 p++;
588 q = &buf[p - str];
589 while (*p)
591 if (*p == '\\' && p[1] == '\n')
592 p += 2;
593 else
594 *q++ = *p++;
596 *q = 0;
599 do_define (pfile, NULL, buf, buf + strlen (buf));
602 /* Process the string STR as if it appeared as the body of a #assert.
603 OPTION is the option name for which STR was the argument. */
605 static void
606 make_assertion (pfile, option, str)
607 cpp_reader *pfile;
608 char *option;
609 U_CHAR *str;
611 struct directive *kt;
612 U_CHAR *buf, *p, *q;
614 /* Copy the entire option so we can modify it. */
615 buf = (U_CHAR *) alloca (strlen (str) + 1);
616 strcpy ((char *) buf, str);
617 /* Scan for any backslash-newline and remove it. */
618 p = q = buf;
619 while (*p) {
620 #if 0
621 if (*p == '\\' && p[1] == '\n')
622 p += 2;
623 else
624 #endif
625 *q++ = *p++;
627 *q = 0;
629 p = buf;
630 if (!is_idstart[*p]) {
631 cpp_error (pfile, "malformed option `%s %s'", option, str);
632 return;
634 while (is_idchar[*++p])
636 while (*p == ' ' || *p == '\t') p++;
637 if (! (*p == 0 || *p == '(')) {
638 cpp_error (pfile, "malformed option `%s %s'", option, str);
639 return;
642 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
644 do_assert (pfile, NULL, NULL, NULL);
645 cpp_pop_buffer (pfile);
649 /* Append a chain of `struct file_name_list's
650 to the end of the main include chain.
651 FIRST is the beginning of the chain to append, and LAST is the end. */
653 static void
654 append_include_chain (pfile, first, last)
655 cpp_reader *pfile;
656 struct file_name_list *first, *last;
658 struct cpp_options *opts = CPP_OPTIONS (pfile);
659 struct file_name_list *dir;
661 if (!first || !last)
662 return;
664 if (opts->include == 0)
665 opts->include = first;
666 else
667 opts->last_include->next = first;
669 if (opts->first_bracket_include == 0)
670 opts->first_bracket_include = first;
672 for (dir = first; ; dir = dir->next) {
673 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
674 if (len > pfile->max_include_len)
675 pfile->max_include_len = len;
676 if (dir == last)
677 break;
680 last->next = NULL;
681 opts->last_include = last;
684 /* Add output to `deps_buffer' for the -M switch.
685 STRING points to the text to be output.
686 SPACER is ':' for targets, ' ' for dependencies, zero for text
687 to be inserted literally. */
689 static void
690 deps_output (pfile, string, spacer)
691 cpp_reader *pfile;
692 char *string;
693 int spacer;
695 int size = strlen (string);
697 if (size == 0)
698 return;
700 #ifndef MAX_OUTPUT_COLUMNS
701 #define MAX_OUTPUT_COLUMNS 72
702 #endif
703 if (spacer
704 && pfile->deps_column > 0
705 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
707 deps_output (pfile, " \\\n ", 0);
708 pfile->deps_column = 0;
711 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
713 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
714 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
715 pfile->deps_allocated_size);
717 if (spacer == ' ' && pfile->deps_column > 0)
718 pfile->deps_buffer[pfile->deps_size++] = ' ';
719 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
720 pfile->deps_size += size;
721 pfile->deps_column += size;
722 if (spacer == ':')
723 pfile->deps_buffer[pfile->deps_size++] = ':';
724 pfile->deps_buffer[pfile->deps_size] = 0;
727 /* Given a colon-separated list of file names PATH,
728 add all the names to the search path for include files. */
730 static void
731 path_include (pfile, path)
732 cpp_reader *pfile;
733 char *path;
735 char *p;
737 p = path;
739 if (*p)
740 while (1) {
741 char *q = p;
742 char *name;
743 struct file_name_list *dirtmp;
745 /* Find the end of this name. */
746 while (*q != 0 && *q != PATH_SEPARATOR) q++;
747 if (p == q) {
748 /* An empty name in the path stands for the current directory. */
749 name = (char *) xmalloc (2);
750 name[0] = '.';
751 name[1] = 0;
752 } else {
753 /* Otherwise use the directory that is named. */
754 name = (char *) xmalloc (q - p + 1);
755 bcopy (p, name, q - p);
756 name[q - p] = 0;
759 dirtmp = (struct file_name_list *)
760 xmalloc (sizeof (struct file_name_list));
761 dirtmp->next = 0; /* New one goes on the end */
762 dirtmp->control_macro = 0;
763 dirtmp->c_system_include_path = 0;
764 dirtmp->fname = name;
765 dirtmp->got_name_map = 0;
766 append_include_chain (pfile, dirtmp, dirtmp);
768 /* Advance past this name. */
769 p = q;
770 if (*p == 0)
771 break;
772 /* Skip the colon. */
773 p++;
777 void
778 cpp_options_init (opts)
779 cpp_options *opts;
781 bzero ((char *) opts, sizeof *opts);
782 opts->in_fname = NULL;
783 opts->out_fname = NULL;
785 /* Initialize is_idchar to allow $. */
786 opts->dollars_in_ident = 1;
787 initialize_char_syntax (opts);
788 opts->dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
790 opts->no_line_commands = 0;
791 opts->no_trigraphs = 1;
792 opts->put_out_comments = 0;
793 opts->print_include_names = 0;
794 opts->dump_macros = dump_none;
795 opts->no_output = 0;
796 opts->cplusplus = 0;
797 opts->cplusplus_comments = 0;
799 opts->verbose = 0;
800 opts->objc = 0;
801 opts->lang_asm = 0;
802 opts->for_lint = 0;
803 opts->chill = 0;
804 opts->pedantic_errors = 0;
805 opts->inhibit_warnings = 0;
806 opts->warn_comments = 0;
807 opts->warn_import = 1;
808 opts->warnings_are_errors = 0;
811 enum cpp_token
812 null_underflow (pfile)
813 cpp_reader *pfile;
815 return CPP_EOF;
819 null_cleanup (pbuf, pfile)
820 cpp_buffer *pbuf;
821 cpp_reader *pfile;
823 return 0;
827 macro_cleanup (pbuf, pfile)
828 cpp_buffer *pbuf;
829 cpp_reader *pfile;
831 HASHNODE *macro = (HASHNODE *) pbuf->data;
832 if (macro->type == T_DISABLED)
833 macro->type = T_MACRO;
834 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
835 free (pbuf->buf);
836 return 0;
840 file_cleanup (pbuf, pfile)
841 cpp_buffer *pbuf;
842 cpp_reader *pfile;
844 if (pbuf->buf)
846 free (pbuf->buf);
847 pbuf->buf = 0;
849 return 0;
852 /* Assuming we have read '/'.
853 If this is the start of a comment (followed by '*' or '/'),
854 skip to the end of the comment, and return ' '.
855 Return EOF if we reached the end of file before the end of the comment.
856 If not the start of a comment, return '/'. */
858 static int
859 skip_comment (pfile, linep)
860 cpp_reader *pfile;
861 long *linep;
863 int c = 0;
864 while (PEEKC() == '\\' && PEEKN(1) == '\n')
866 if (linep)
867 (*linep)++;
868 FORWARD(2);
870 if (PEEKC() == '*')
872 FORWARD(1);
873 for (;;)
875 int prev_c = c;
876 c = GETC ();
877 if (c == EOF)
878 return EOF;
879 while (c == '\\' && PEEKC() == '\n')
881 if (linep)
882 (*linep)++;
883 FORWARD(1), c = GETC();
885 if (prev_c == '*' && c == '/')
886 return ' ';
887 if (c == '\n' && linep)
888 (*linep)++;
891 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
893 FORWARD(1);
894 for (;;)
896 c = GETC ();
897 if (c == EOF)
898 return ' '; /* Allow // to be terminated by EOF. */
899 while (c == '\\' && PEEKC() == '\n')
901 FORWARD(1);
902 c = GETC();
903 if (linep)
904 (*linep)++;
906 if (c == '\n')
908 /* Don't consider final '\n' to be part of comment. */
909 FORWARD(-1);
910 return ' ';
914 else
915 return '/';
918 /* Skip whitespace \-newline and comments. Does not macro-expand. */
920 void
921 cpp_skip_hspace (pfile)
922 cpp_reader *pfile;
924 while (1)
926 int c = PEEKC();
927 if (c == EOF)
928 return; /* FIXME */
929 if (is_hor_space[c])
931 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
932 cpp_pedwarn (pfile, "%s in preprocessing directive",
933 c == '\f' ? "formfeed" : "vertical tab");
934 FORWARD(1);
936 else if (c == '/')
938 FORWARD (1);
939 c = skip_comment (pfile, NULL);
940 if (c == '/')
941 FORWARD(-1);
942 if (c == EOF || c == '/')
943 return;
945 else if (c == '\\' && PEEKN(1) == '\n') {
946 FORWARD(2);
948 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
949 && is_hor_space[PEEKN(1)])
950 FORWARD(2);
951 else return;
955 /* Read the rest of the current line.
956 The line is appended to PFILE's output buffer. */
958 static void
959 copy_rest_of_line (pfile)
960 cpp_reader *pfile;
962 struct cpp_options *opts = CPP_OPTIONS (pfile);
963 for (;;)
965 int c = GETC();
966 int nextc;
967 switch (c)
969 case EOF:
970 goto end_directive;
971 case '\\':
972 if (PEEKC() == '\n')
974 FORWARD (1);
975 continue;
977 case '\'':
978 case '\"':
979 goto scan_directive_token;
980 break;
981 case '/':
982 nextc = PEEKC();
983 if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
984 goto scan_directive_token;
985 break;
986 case '\f':
987 case '\v':
988 if (CPP_PEDANTIC (pfile))
989 cpp_pedwarn (pfile, "%s in preprocessing directive",
990 c == '\f' ? "formfeed" : "vertical tab");
991 break;
993 case '\n':
994 FORWARD(-1);
995 goto end_directive;
996 scan_directive_token:
997 FORWARD(-1);
998 cpp_get_token (pfile);
999 continue;
1001 CPP_PUTC (pfile, c);
1003 end_directive: ;
1004 CPP_NUL_TERMINATE (pfile);
1007 void
1008 skip_rest_of_line (pfile)
1009 cpp_reader *pfile;
1011 long old = CPP_WRITTEN (pfile);
1012 copy_rest_of_line (pfile);
1013 CPP_SET_WRITTEN (pfile, old);
1016 /* Handle a possible # directive.
1017 '#' has already been read. */
1020 handle_directive (pfile)
1021 cpp_reader *pfile;
1022 { int c;
1023 register struct directive *kt;
1024 int ident_length;
1025 long after_ident;
1026 U_CHAR *ident, *line_end;
1027 long old_written = CPP_WRITTEN (pfile);
1029 cpp_skip_hspace (pfile);
1031 c = PEEKC ();
1032 if (c >= '0' && c <= '9')
1034 /* Handle # followed by a line number. */
1035 if (CPP_PEDANTIC (pfile))
1036 cpp_pedwarn (pfile, "`#' followed by integer");
1037 do_line (pfile, NULL);
1038 goto done_a_directive;
1041 /* Now find the directive name. */
1042 CPP_PUTC (pfile, '#');
1043 parse_name (pfile, GETC());
1044 ident = pfile->token_buffer + old_written + 1;
1045 ident_length = CPP_PWRITTEN (pfile) - ident;
1046 if (ident_length == 0 && PEEKC() == '\n')
1048 /* A line of just `#' becomes blank. */
1049 goto done_a_directive;
1052 #if 0
1053 if (ident_length == 0 || !is_idstart[*ident]) {
1054 U_CHAR *p = ident;
1055 while (is_idchar[*p]) {
1056 if (*p < '0' || *p > '9')
1057 break;
1058 p++;
1060 /* Avoid error for `###' and similar cases unless -pedantic. */
1061 if (p == ident) {
1062 while (*p == '#' || is_hor_space[*p]) p++;
1063 if (*p == '\n') {
1064 if (pedantic && !lang_asm)
1065 cpp_warning (pfile, "invalid preprocessor directive");
1066 return 0;
1070 if (!lang_asm)
1071 cpp_error (pfile, "invalid preprocessor directive name");
1073 return 0;
1075 #endif
1077 * Decode the keyword and call the appropriate expansion
1078 * routine, after moving the input pointer up to the next line.
1080 for (kt = directive_table; ; kt++) {
1081 if (kt->length <= 0)
1082 goto not_a_directive;
1083 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1084 break;
1087 if (kt->command_reads_line)
1088 after_ident = 0;
1089 else
1091 /* Nonzero means do not delete comments within the directive.
1092 #define needs this when -traditional. */
1093 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1094 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1095 CPP_OPTIONS (pfile)->put_out_comments = comments;
1096 after_ident = CPP_WRITTEN (pfile);
1097 copy_rest_of_line (pfile);
1098 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1101 /* For #pragma and #define, we may want to pass through the directive.
1102 Other directives may create output, but we don't want the directive
1103 itself out, so we pop it now. For example #include may write a #line
1104 command (see comment in do_include), and conditionals may emit
1105 #failed ... #endfailed stuff. But note that popping the buffer
1106 means the parameters to kt->func may point after pfile->limit
1107 so these parameters are invalid as soon as something gets appended
1108 to the token_buffer. */
1110 line_end = CPP_PWRITTEN (pfile);
1111 if (!kt->pass_thru && kt->type != T_DEFINE)
1112 CPP_SET_WRITTEN (pfile, old_written);
1114 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1115 if (kt->pass_thru
1116 || (kt->type == T_DEFINE
1117 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1119 /* Just leave the entire #define in the output stack. */
1121 else if (kt->type == T_DEFINE
1122 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1124 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1125 SKIP_WHITE_SPACE (p);
1126 while (is_idchar[*p]) p++;
1127 pfile->limit = p;
1128 CPP_PUTC (pfile, '\n');
1130 else if (kt->type == T_DEFINE)
1131 CPP_SET_WRITTEN (pfile, old_written);
1132 done_a_directive:
1133 return 1;
1135 not_a_directive:
1136 return 0;
1139 /* Pass a directive through to the output file.
1140 BUF points to the contents of the directive, as a contiguous string.
1141 LIMIT points to the first character past the end of the directive.
1142 KEYWORD is the keyword-table entry for the directive. */
1144 static void
1145 pass_thru_directive (buf, limit, pfile, keyword)
1146 U_CHAR *buf, *limit;
1147 cpp_reader *pfile;
1148 struct directive *keyword;
1150 register unsigned keyword_length = keyword->length;
1152 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1153 CPP_PUTC_Q (pfile, '#');
1154 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1155 if (limit != buf && buf[0] != ' ')
1156 CPP_PUTC_Q (pfile, ' ');
1157 CPP_PUTS_Q (pfile, buf, limit - buf);
1158 #if 0
1159 CPP_PUTS_Q (pfile, '\n');
1160 /* Count the line we have just made in the output,
1161 to get in sync properly. */
1162 pfile->lineno++;
1163 #endif
1166 /* The arglist structure is built by do_define to tell
1167 collect_definition where the argument names begin. That
1168 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1169 would contain pointers to the strings x, y, and z.
1170 Collect_definition would then build a DEFINITION node,
1171 with reflist nodes pointing to the places x, y, and z had
1172 appeared. So the arglist is just convenience data passed
1173 between these two routines. It is not kept around after
1174 the current #define has been processed and entered into the
1175 hash table. */
1177 struct arglist {
1178 struct arglist *next;
1179 U_CHAR *name;
1180 int length;
1181 int argno;
1182 char rest_args;
1185 /* Read a replacement list for a macro with parameters.
1186 Build the DEFINITION structure.
1187 Reads characters of text starting at BUF until END.
1188 ARGLIST specifies the formal parameters to look for
1189 in the text of the definition; NARGS is the number of args
1190 in that list, or -1 for a macro name that wants no argument list.
1191 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1192 and NAMELEN is its length in characters.
1194 Note that comments, backslash-newlines, and leading white space
1195 have already been deleted from the argument. */
1197 static DEFINITION *
1198 collect_expansion (pfile, buf, limit, nargs, arglist)
1199 cpp_reader *pfile;
1200 U_CHAR *buf, *limit;
1201 int nargs;
1202 struct arglist *arglist;
1204 DEFINITION *defn;
1205 register U_CHAR *p, *lastp, *exp_p;
1206 struct reflist *endpat = NULL;
1207 /* Pointer to first nonspace after last ## seen. */
1208 U_CHAR *concat = 0;
1209 /* Pointer to first nonspace after last single-# seen. */
1210 U_CHAR *stringify = 0;
1211 int maxsize;
1212 int expected_delimiter = '\0';
1214 /* Scan thru the replacement list, ignoring comments and quoted
1215 strings, picking up on the macro calls. It does a linear search
1216 thru the arg list on every potential symbol. Profiling might say
1217 that something smarter should happen. */
1219 if (limit < buf)
1220 abort ();
1222 /* Find the beginning of the trailing whitespace. */
1223 p = buf;
1224 while (p < limit && is_space[limit[-1]]) limit--;
1226 /* Allocate space for the text in the macro definition.
1227 Leading and trailing whitespace chars need 2 bytes each.
1228 Each other input char may or may not need 1 byte,
1229 so this is an upper bound. The extra 5 are for invented
1230 leading and trailing newline-marker and final null. */
1231 maxsize = (sizeof (DEFINITION)
1232 + (limit - p) + 5);
1233 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1234 while (p < limit)
1235 if (*p++ == '@')
1236 maxsize++;
1237 defn = (DEFINITION *) xcalloc (1, maxsize);
1239 defn->nargs = nargs;
1240 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1241 lastp = exp_p;
1243 p = buf;
1245 /* Add one initial space escape-marker to prevent accidental
1246 token-pasting (often removed by macroexpand). */
1247 *exp_p++ = '@';
1248 *exp_p++ = ' ';
1250 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1251 cpp_error (pfile, "`##' at start of macro definition");
1252 p += 2;
1255 /* Process the main body of the definition. */
1256 while (p < limit) {
1257 int skipped_arg = 0;
1258 register U_CHAR c = *p++;
1260 *exp_p++ = c;
1262 if (!CPP_TRADITIONAL (pfile)) {
1263 switch (c) {
1264 case '\'':
1265 case '\"':
1266 if (expected_delimiter != '\0') {
1267 if (c == expected_delimiter)
1268 expected_delimiter = '\0';
1269 } else
1270 expected_delimiter = c;
1271 break;
1273 case '\\':
1274 if (p < limit && expected_delimiter) {
1275 /* In a string, backslash goes through
1276 and makes next char ordinary. */
1277 *exp_p++ = *p++;
1279 break;
1281 case '@':
1282 /* An '@' in a string or character constant stands for itself,
1283 and does not need to be escaped. */
1284 if (!expected_delimiter)
1285 *exp_p++ = c;
1286 break;
1288 case '#':
1289 /* # is ordinary inside a string. */
1290 if (expected_delimiter)
1291 break;
1292 if (p < limit && *p == '#') {
1293 /* ##: concatenate preceding and following tokens. */
1294 /* Take out the first #, discard preceding whitespace. */
1295 exp_p--;
1296 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1297 --exp_p;
1298 /* Skip the second #. */
1299 p++;
1300 /* Discard following whitespace. */
1301 SKIP_WHITE_SPACE (p);
1302 concat = p;
1303 if (p == limit)
1304 cpp_error (pfile, "`##' at end of macro definition");
1305 } else if (nargs >= 0) {
1306 /* Single #: stringify following argument ref.
1307 Don't leave the # in the expansion. */
1308 exp_p--;
1309 SKIP_WHITE_SPACE (p);
1310 if (p == limit || ! is_idstart[*p])
1311 cpp_error (pfile,
1312 "`#' operator is not followed by a macro argument name");
1313 else
1314 stringify = p;
1316 break;
1318 } else {
1319 /* In -traditional mode, recognize arguments inside strings and
1320 and character constants, and ignore special properties of #.
1321 Arguments inside strings are considered "stringified", but no
1322 extra quote marks are supplied. */
1323 switch (c) {
1324 case '\'':
1325 case '\"':
1326 if (expected_delimiter != '\0') {
1327 if (c == expected_delimiter)
1328 expected_delimiter = '\0';
1329 } else
1330 expected_delimiter = c;
1331 break;
1333 case '\\':
1334 /* Backslash quotes delimiters and itself, but not macro args. */
1335 if (expected_delimiter != 0 && p < limit
1336 && (*p == expected_delimiter || *p == '\\')) {
1337 *exp_p++ = *p++;
1338 continue;
1340 break;
1342 case '/':
1343 if (expected_delimiter != '\0') /* No comments inside strings. */
1344 break;
1345 if (*p == '*') {
1346 /* If we find a comment that wasn't removed by handle_directive,
1347 this must be -traditional. So replace the comment with
1348 nothing at all. */
1349 exp_p--;
1350 p += 1;
1351 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1352 p++;
1353 #if 0
1354 /* Mark this as a concatenation-point, as if it had been ##. */
1355 concat = p;
1356 #endif
1358 break;
1362 /* Handle the start of a symbol. */
1363 if (is_idchar[c] && nargs > 0) {
1364 U_CHAR *id_beg = p - 1;
1365 int id_len;
1367 --exp_p;
1368 while (p != limit && is_idchar[*p]) p++;
1369 id_len = p - id_beg;
1371 if (is_idstart[c]) {
1372 register struct arglist *arg;
1374 for (arg = arglist; arg != NULL; arg = arg->next) {
1375 struct reflist *tpat;
1377 if (arg->name[0] == c
1378 && arg->length == id_len
1379 && strncmp (arg->name, id_beg, id_len) == 0) {
1380 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1381 if (CPP_TRADITIONAL (pfile)) {
1382 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1383 id_len, arg->name);
1384 } else {
1385 cpp_warning (pfile,
1386 "macro arg `%.*s' would be stringified with -traditional.",
1387 id_len, arg->name);
1390 /* If ANSI, don't actually substitute inside a string. */
1391 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1392 break;
1393 /* make a pat node for this arg and append it to the end of
1394 the pat list */
1395 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1396 tpat->next = NULL;
1397 tpat->raw_before = concat == id_beg;
1398 tpat->raw_after = 0;
1399 tpat->rest_args = arg->rest_args;
1400 tpat->stringify = (CPP_TRADITIONAL (pfile)
1401 ? expected_delimiter != '\0'
1402 : stringify == id_beg);
1404 if (endpat == NULL)
1405 defn->pattern = tpat;
1406 else
1407 endpat->next = tpat;
1408 endpat = tpat;
1410 tpat->argno = arg->argno;
1411 tpat->nchars = exp_p - lastp;
1413 register U_CHAR *p1 = p;
1414 SKIP_WHITE_SPACE (p1);
1415 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1416 tpat->raw_after = 1;
1418 lastp = exp_p; /* place to start copying from next time */
1419 skipped_arg = 1;
1420 break;
1425 /* If this was not a macro arg, copy it into the expansion. */
1426 if (! skipped_arg) {
1427 register U_CHAR *lim1 = p;
1428 p = id_beg;
1429 while (p != lim1)
1430 *exp_p++ = *p++;
1431 if (stringify == id_beg)
1432 cpp_error (pfile,
1433 "`#' operator should be followed by a macro argument name");
1438 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1440 /* If ANSI, put in a "@ " marker to prevent token pasting.
1441 But not if "inside a string" (which in ANSI mode
1442 happens only for -D option). */
1443 *exp_p++ = '@';
1444 *exp_p++ = ' ';
1447 *exp_p = '\0';
1449 defn->length = exp_p - defn->expansion;
1451 /* Crash now if we overrun the allocated size. */
1452 if (defn->length + 1 > maxsize)
1453 abort ();
1455 #if 0
1456 /* This isn't worth the time it takes. */
1457 /* give back excess storage */
1458 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1459 #endif
1461 return defn;
1465 * special extension string that can be added to the last macro argument to
1466 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1467 * #define wow(a, b...) process (b, a, b)
1468 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1469 * { wow (one, two); } -> { process (two, one, two); }
1470 * if this "rest_arg" is used with the concat token '##' and if it is not
1471 * supplied then the token attached to with ## will not be outputted. Ex:
1472 * #define wow (a, b...) process (b ## , a, ## b)
1473 * { wow (1, 2); } -> { process (2, 1, 2); }
1474 * { wow (one); } -> { process (one); {
1476 static char rest_extension[] = "...";
1477 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1479 /* Create a DEFINITION node from a #define directive. Arguments are
1480 as for do_define. */
1482 static MACRODEF
1483 create_definition (buf, limit, pfile, predefinition)
1484 U_CHAR *buf, *limit;
1485 cpp_reader *pfile;
1486 int predefinition;
1488 U_CHAR *bp; /* temp ptr into input buffer */
1489 U_CHAR *symname; /* remember where symbol name starts */
1490 int sym_length; /* and how long it is */
1491 int rest_args = 0;
1492 long line, col;
1493 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1494 DEFINITION *defn;
1495 int arglengths = 0; /* Accumulate lengths of arg names
1496 plus number of args. */
1497 MACRODEF mdef;
1498 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1500 bp = buf;
1502 while (is_hor_space[*bp])
1503 bp++;
1505 symname = bp; /* remember where it starts */
1507 sym_length = check_macro_name (pfile, bp, "macro");
1508 bp += sym_length;
1510 /* Lossage will occur if identifiers or control keywords are broken
1511 across lines using backslash. This is not the right place to take
1512 care of that. */
1514 if (*bp == '(') {
1515 struct arglist *arg_ptrs = NULL;
1516 int argno = 0;
1518 bp++; /* skip '(' */
1519 SKIP_WHITE_SPACE (bp);
1521 /* Loop over macro argument names. */
1522 while (*bp != ')') {
1523 struct arglist *temp;
1525 temp = (struct arglist *) alloca (sizeof (struct arglist));
1526 temp->name = bp;
1527 temp->next = arg_ptrs;
1528 temp->argno = argno++;
1529 temp->rest_args = 0;
1530 arg_ptrs = temp;
1532 if (rest_args)
1533 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1535 if (!is_idstart[*bp])
1536 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1538 /* Find the end of the arg name. */
1539 while (is_idchar[*bp]) {
1540 bp++;
1541 /* do we have a "special" rest-args extension here? */
1542 if (limit - bp > REST_EXTENSION_LENGTH &&
1543 strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1544 rest_args = 1;
1545 temp->rest_args = 1;
1546 break;
1549 temp->length = bp - temp->name;
1550 if (rest_args == 1)
1551 bp += REST_EXTENSION_LENGTH;
1552 arglengths += temp->length + 2;
1553 SKIP_WHITE_SPACE (bp);
1554 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1555 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1556 goto nope;
1558 if (*bp == ',') {
1559 bp++;
1560 SKIP_WHITE_SPACE (bp);
1562 if (bp >= limit) {
1563 cpp_error (pfile, "unterminated parameter list in `#define'");
1564 goto nope;
1567 struct arglist *otemp;
1569 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1570 if (temp->length == otemp->length &&
1571 strncmp (temp->name, otemp->name, temp->length) == 0) {
1572 U_CHAR *name;
1574 name = (U_CHAR *) alloca (temp->length + 1);
1575 (void) strncpy (name, temp->name, temp->length);
1576 name[temp->length] = '\0';
1577 cpp_error (pfile,
1578 "duplicate argument name `%s' in `#define'", name);
1579 goto nope;
1584 ++bp; /* skip paren */
1585 SKIP_WHITE_SPACE (bp);
1586 /* now everything from bp before limit is the definition. */
1587 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1588 defn->rest_args = rest_args;
1590 /* Now set defn->args.argnames to the result of concatenating
1591 the argument names in reverse order
1592 with comma-space between them. */
1593 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1595 struct arglist *temp;
1596 int i = 0;
1597 for (temp = arg_ptrs; temp; temp = temp->next) {
1598 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1599 i += temp->length;
1600 if (temp->next != 0) {
1601 defn->args.argnames[i++] = ',';
1602 defn->args.argnames[i++] = ' ';
1605 defn->args.argnames[i] = 0;
1607 } else {
1608 /* Simple expansion or empty definition. */
1610 if (bp < limit)
1612 if (is_hor_space[*bp]) {
1613 bp++;
1614 SKIP_WHITE_SPACE (bp);
1615 } else {
1616 switch (*bp) {
1617 case '!': case '"': case '#': case '%': case '&': case '\'':
1618 case ')': case '*': case '+': case ',': case '-': case '.':
1619 case '/': case ':': case ';': case '<': case '=': case '>':
1620 case '?': case '[': case '\\': case ']': case '^': case '{':
1621 case '|': case '}': case '~':
1622 cpp_warning (pfile, "missing white space after `#define %.*s'",
1623 sym_length, symname);
1624 break;
1626 default:
1627 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1628 sym_length, symname);
1629 break;
1633 /* now everything from bp before limit is the definition. */
1634 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1635 defn->args.argnames = (U_CHAR *) "";
1638 defn->line = line;
1639 defn->file = file;
1641 /* OP is null if this is a predefinition */
1642 defn->predefined = predefinition;
1643 mdef.defn = defn;
1644 mdef.symnam = symname;
1645 mdef.symlen = sym_length;
1647 return mdef;
1649 nope:
1650 mdef.defn = 0;
1651 return mdef;
1654 /* Check a purported macro name SYMNAME, and yield its length.
1655 USAGE is the kind of name this is intended for. */
1657 static int
1658 check_macro_name (pfile, symname, usage)
1659 cpp_reader *pfile;
1660 U_CHAR *symname;
1661 char *usage;
1663 U_CHAR *p;
1664 int sym_length;
1666 for (p = symname; is_idchar[*p]; p++)
1668 sym_length = p - symname;
1669 if (sym_length == 0)
1670 cpp_error (pfile, "invalid %s name", usage);
1671 else if (!is_idstart[*symname]) {
1672 U_CHAR *msg; /* what pain... */
1673 msg = (U_CHAR *) alloca (sym_length + 1);
1674 bcopy (symname, msg, sym_length);
1675 msg[sym_length] = 0;
1676 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1677 } else {
1678 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1679 cpp_error (pfile, "invalid %s name `defined'", usage);
1681 return sym_length;
1684 /* Return zero if two DEFINITIONs are isomorphic. */
1686 static int
1687 compare_defs (d1, d2)
1688 DEFINITION *d1, *d2;
1690 register struct reflist *a1, *a2;
1691 register U_CHAR *p1 = d1->expansion;
1692 register U_CHAR *p2 = d2->expansion;
1693 int first = 1;
1695 if (d1->nargs != d2->nargs)
1696 return 1;
1697 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1698 return 1;
1699 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1700 a1 = a1->next, a2 = a2->next) {
1701 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1702 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1703 || a1->argno != a2->argno
1704 || a1->stringify != a2->stringify
1705 || a1->raw_before != a2->raw_before
1706 || a1->raw_after != a2->raw_after)
1707 return 1;
1708 first = 0;
1709 p1 += a1->nchars;
1710 p2 += a2->nchars;
1712 if (a1 != a2)
1713 return 1;
1714 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1715 p2, d2->length - (p2 - d2->expansion), 1))
1716 return 1;
1717 return 0;
1720 /* Return 1 if two parts of two macro definitions are effectively different.
1721 One of the parts starts at BEG1 and has LEN1 chars;
1722 the other has LEN2 chars at BEG2.
1723 Any sequence of whitespace matches any other sequence of whitespace.
1724 FIRST means these parts are the first of a macro definition;
1725 so ignore leading whitespace entirely.
1726 LAST means these parts are the last of a macro definition;
1727 so ignore trailing whitespace entirely. */
1729 static int
1730 comp_def_part (first, beg1, len1, beg2, len2, last)
1731 int first;
1732 U_CHAR *beg1, *beg2;
1733 int len1, len2;
1734 int last;
1736 register U_CHAR *end1 = beg1 + len1;
1737 register U_CHAR *end2 = beg2 + len2;
1738 if (first) {
1739 while (beg1 != end1 && is_space[*beg1]) beg1++;
1740 while (beg2 != end2 && is_space[*beg2]) beg2++;
1742 if (last) {
1743 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1744 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1746 while (beg1 != end1 && beg2 != end2) {
1747 if (is_space[*beg1] && is_space[*beg2]) {
1748 while (beg1 != end1 && is_space[*beg1]) beg1++;
1749 while (beg2 != end2 && is_space[*beg2]) beg2++;
1750 } else if (*beg1 == *beg2) {
1751 beg1++; beg2++;
1752 } else break;
1754 return (beg1 != end1) || (beg2 != end2);
1757 /* Process a #define command.
1758 BUF points to the contents of the #define command, as a contiguous string.
1759 LIMIT points to the first character past the end of the definition.
1760 KEYWORD is the keyword-table entry for #define,
1761 or NULL for a "predefined" macro. */
1763 static int
1764 do_define (pfile, keyword, buf, limit)
1765 cpp_reader *pfile;
1766 struct directive *keyword;
1767 U_CHAR *buf, *limit;
1769 int hashcode;
1770 MACRODEF mdef;
1771 HASHNODE *hp;
1773 #if 0
1774 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1775 if (pcp_outfile && keyword)
1776 pass_thru_directive (buf, limit, pfile, keyword);
1777 #endif
1779 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1780 if (mdef.defn == 0)
1781 goto nope;
1783 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1785 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1787 int ok = 0;
1788 /* Redefining a precompiled key is ok. */
1789 if (hp->type == T_PCSTRING)
1790 ok = 1;
1791 /* Redefining a macro is ok if the definitions are the same. */
1792 else if (hp->type == T_MACRO)
1793 ok = ! compare_defs (mdef.defn, hp->value.defn);
1794 /* Redefining a constant is ok with -D. */
1795 else if (hp->type == T_CONST)
1796 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1797 /* Print the warning if it's not ok. */
1798 if (!ok)
1800 U_CHAR *msg; /* what pain... */
1802 /* If we are passing through #define and #undef directives, do
1803 that for this re-definition now. */
1804 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1805 pass_thru_directive (buf, limit, pfile, keyword);
1807 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1808 *msg = '`';
1809 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1810 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1811 cpp_pedwarn (pfile, msg);
1812 if (hp->type == T_MACRO)
1813 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1814 "this is the location of the previous definition");
1816 /* Replace the old definition. */
1817 hp->type = T_MACRO;
1818 hp->value.defn = mdef.defn;
1820 else
1822 /* If we are passing through #define and #undef directives, do
1823 that for this new definition now. */
1824 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1825 pass_thru_directive (buf, limit, pfile, keyword);
1826 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1827 (char *) mdef.defn, hashcode);
1830 return 0;
1832 nope:
1834 return 1;
1837 /* This structure represents one parsed argument in a macro call.
1838 `raw' points to the argument text as written (`raw_length' is its length).
1839 `expanded' points to the argument's macro-expansion
1840 (its length is `expand_length').
1841 `stringified_length' is the length the argument would have
1842 if stringified.
1843 `use_count' is the number of times this macro arg is substituted
1844 into the macro. If the actual use count exceeds 10,
1845 the value stored is 10. */
1847 /* raw and expanded are relative to ARG_BASE */
1848 #define ARG_BASE ((pfile)->token_buffer)
1850 struct argdata {
1851 /* Strings relative to pfile->token_buffer */
1852 long raw, expanded, stringified;
1853 int raw_length, expand_length;
1854 int stringified_length;
1855 char newlines;
1856 char use_count;
1859 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1860 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1861 as the new input buffer.
1862 Return the new buffer, or NULL on failure. */
1864 cpp_buffer *
1865 cpp_push_buffer (pfile, buffer, length)
1866 cpp_reader *pfile;
1867 U_CHAR *buffer;
1868 long length;
1870 register cpp_buffer *buf = CPP_BUFFER (pfile);
1871 if (buf == pfile->buffer_stack)
1873 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1874 buf->fname);
1875 return NULL;
1877 buf--;
1878 bzero ((char *) buf, sizeof (cpp_buffer));
1879 CPP_BUFFER (pfile) = buf;
1880 buf->if_stack = pfile->if_stack;
1881 buf->cleanup = null_cleanup;
1882 buf->underflow = null_underflow;
1883 buf->buf = buf->cur = buffer;
1884 buf->alimit = buf->rlimit = buffer + length;
1886 return buf;
1889 cpp_buffer *
1890 cpp_pop_buffer (pfile)
1891 cpp_reader *pfile;
1893 cpp_buffer *buf = CPP_BUFFER (pfile);
1894 (*buf->cleanup) (buf, pfile);
1895 return ++CPP_BUFFER (pfile);
1898 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1899 Pop the buffer when done. */
1901 void
1902 cpp_scan_buffer (pfile)
1903 cpp_reader *pfile;
1905 cpp_buffer *buffer = CPP_BUFFER (pfile);
1906 for (;;)
1908 enum cpp_token token = cpp_get_token (pfile);
1909 if (token == CPP_EOF) /* Should not happen ... */
1910 break;
1911 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1913 cpp_pop_buffer (pfile);
1914 break;
1920 * Rescan a string (which may have escape marks) into pfile's buffer.
1921 * Place the result in pfile->token_buffer.
1923 * The input is copied before it is scanned, so it is safe to pass
1924 * it something from the token_buffer that will get overwritten
1925 * (because it follows CPP_WRITTEN). This is used by do_include.
1928 static void
1929 cpp_expand_to_buffer (pfile, buf, length)
1930 cpp_reader *pfile;
1931 U_CHAR *buf;
1932 int length;
1934 register cpp_buffer *ip;
1935 cpp_buffer obuf;
1936 U_CHAR *limit = buf + length;
1937 U_CHAR *buf1;
1938 #if 0
1939 int odepth = indepth;
1940 #endif
1942 if (length < 0)
1943 abort ();
1945 /* Set up the input on the input stack. */
1947 buf1 = (U_CHAR *) alloca (length + 1);
1949 register U_CHAR *p1 = buf;
1950 register U_CHAR *p2 = buf1;
1952 while (p1 != limit)
1953 *p2++ = *p1++;
1955 buf1[length] = 0;
1957 ip = cpp_push_buffer (pfile, buf1, length);
1958 if (ip == NULL)
1959 return;
1960 ip->has_escapes = 1;
1961 #if 0
1962 ip->lineno = obuf.lineno = 1;
1963 #endif
1965 /* Scan the input, create the output. */
1966 cpp_scan_buffer (pfile);
1968 #if 0
1969 if (indepth != odepth)
1970 abort ();
1971 #endif
1973 CPP_NUL_TERMINATE (pfile);
1977 static void
1978 adjust_position (buf, limit, linep, colp)
1979 U_CHAR *buf;
1980 U_CHAR *limit;
1981 long *linep;
1982 long *colp;
1984 while (buf < limit)
1986 U_CHAR ch = *buf++;
1987 if (ch == '\n')
1988 (*linep)++, (*colp) = 1;
1989 else
1990 (*colp)++;
1994 /* Move line_base forward, updating lineno and colno. */
1996 static void
1997 update_position (pbuf)
1998 register cpp_buffer *pbuf;
2000 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2001 unsigned char *new_pos = pbuf->cur;
2002 register struct parse_marker *mark;
2003 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2005 if (pbuf->buf + mark->position < new_pos)
2006 new_pos = pbuf->buf + mark->position;
2008 pbuf->line_base += new_pos - old_pos;
2009 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2012 void
2013 cpp_buf_line_and_col (pbuf, linep, colp)
2014 register cpp_buffer *pbuf;
2015 long *linep, *colp;
2017 long dummy;
2018 if (colp == NULL)
2019 colp = &dummy;
2020 if (pbuf)
2022 *linep = pbuf->lineno;
2023 *colp = pbuf->colno;
2024 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2026 else
2028 *linep = 0;
2029 *colp = 0;
2033 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2035 cpp_buffer *
2036 cpp_file_buffer (pfile)
2037 cpp_reader *pfile;
2039 cpp_buffer *ip = CPP_BUFFER (pfile);
2041 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2042 if (ip->fname != NULL)
2043 return ip;
2044 return NULL;
2047 static long
2048 count_newlines (buf, limit)
2049 register U_CHAR *buf;
2050 register U_CHAR *limit;
2052 register long count = 0;
2053 while (buf < limit)
2055 U_CHAR ch = *buf++;
2056 if (ch == '\n')
2057 count++;
2059 return count;
2063 * write out a #line command, for instance, after an #include file.
2064 * If CONDITIONAL is nonzero, we can omit the #line if it would
2065 * appear to be a no-op, and we can output a few newlines instead
2066 * if we want to increase the line number by a small amount.
2067 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2070 static void
2071 output_line_command (pfile, conditional, file_change)
2072 cpp_reader *pfile;
2073 int conditional;
2074 enum file_change_code file_change;
2076 int len;
2077 char *line_cmd_buf, *line_end;
2078 long line, col;
2079 cpp_buffer *ip = CPP_BUFFER (pfile);
2081 if (ip->fname == NULL)
2082 return;
2084 update_position (ip);
2086 if (CPP_OPTIONS (pfile)->no_line_commands
2087 || CPP_OPTIONS (pfile)->no_output)
2088 return;
2090 line = CPP_BUFFER (pfile)->lineno;
2091 col = CPP_BUFFER (pfile)->colno;
2092 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2094 if (CPP_OPTIONS (pfile)->no_line_commands)
2095 return;
2097 if (conditional) {
2098 if (line == pfile->lineno)
2099 return;
2101 /* If the inherited line number is a little too small,
2102 output some newlines instead of a #line command. */
2103 if (line > pfile->lineno && line < pfile->lineno + 8) {
2104 CPP_RESERVE (pfile, 20);
2105 while (line > pfile->lineno) {
2106 CPP_PUTC_Q (pfile, '\n');
2107 pfile->lineno++;
2109 return;
2113 #if 0
2114 /* Don't output a line number of 0 if we can help it. */
2115 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2116 && *ip->bufp == '\n') {
2117 ip->lineno++;
2118 ip->bufp++;
2120 #endif
2122 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2124 #ifdef OUTPUT_LINE_COMMANDS
2125 static char sharp_line[] = "#line ";
2126 #else
2127 static char sharp_line[] = "# ";
2128 #endif
2129 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2132 sprintf (CPP_PWRITTEN (pfile), "%d ", line);
2133 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2135 quote_string (pfile, ip->nominal_fname);
2136 if (file_change != same_file) {
2137 CPP_PUTC_Q (pfile, ' ');
2138 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2140 /* Tell cc1 if following text comes from a system header file. */
2141 if (ip->system_header_p) {
2142 CPP_PUTC_Q (pfile, ' ');
2143 CPP_PUTC_Q (pfile, '3');
2145 #ifndef NO_IMPLICIT_EXTERN_C
2146 /* Tell cc1plus if following text should be treated as C. */
2147 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2148 CPP_PUTC_Q (pfile, ' ');
2149 CPP_PUTC_Q (pfile, '4');
2151 #endif
2152 CPP_PUTC_Q (pfile, '\n');
2153 pfile->lineno = line;
2157 * Parse a macro argument and append the info on PFILE's token_buffer.
2158 * REST_ARGS means to absorb the rest of the args.
2159 * Return nonzero to indicate a syntax error.
2162 static enum cpp_token
2163 macarg (pfile, rest_args)
2164 cpp_reader *pfile;
2165 int rest_args;
2167 int paren = 0;
2168 enum cpp_token token;
2169 long arg_start = CPP_WRITTEN (pfile);
2170 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2171 CPP_OPTIONS (pfile)->put_out_comments = 0;
2173 /* Try to parse as much of the argument as exists at this
2174 input stack level. */
2175 pfile->no_macro_expand++;
2176 for (;;)
2178 token = cpp_get_token (pfile);
2179 switch (token)
2181 case CPP_EOF:
2182 goto done;
2183 case CPP_POP:
2184 /* If we've hit end of file, it's an error (reported by caller).
2185 Ditto if it's the end of cpp_expand_to_buffer text.
2186 If we've hit end of macro, just continue. */
2187 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2188 goto done;
2189 break;
2190 case CPP_LPAREN:
2191 paren++;
2192 break;
2193 case CPP_RPAREN:
2194 if (--paren < 0)
2195 goto found;
2196 break;
2197 case CPP_COMMA:
2198 /* if we've returned to lowest level and
2199 we aren't absorbing all args */
2200 if (paren == 0 && rest_args == 0)
2201 goto found;
2202 break;
2203 found:
2204 /* Remove ',' or ')' from argument buffer. */
2205 CPP_ADJUST_WRITTEN (pfile, -1);
2206 goto done;
2207 default: ;
2211 done:
2212 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2213 pfile->no_macro_expand--;
2215 return token;
2218 /* Turn newlines to spaces in the string of length LENGTH at START,
2219 except inside of string constants.
2220 The string is copied into itself with its beginning staying fixed. */
2222 static int
2223 change_newlines (start, length)
2224 U_CHAR *start;
2225 int length;
2227 register U_CHAR *ibp;
2228 register U_CHAR *obp;
2229 register U_CHAR *limit;
2230 register int c;
2232 ibp = start;
2233 limit = start + length;
2234 obp = start;
2236 while (ibp < limit) {
2237 *obp++ = c = *ibp++;
2238 switch (c) {
2240 case '\'':
2241 case '\"':
2242 /* Notice and skip strings, so that we don't delete newlines in them. */
2244 int quotec = c;
2245 while (ibp < limit) {
2246 *obp++ = c = *ibp++;
2247 if (c == quotec)
2248 break;
2249 if (c == '\n' && quotec == '\'')
2250 break;
2253 break;
2257 return obp - start;
2261 static struct tm *
2262 timestamp (pfile)
2263 cpp_reader *pfile;
2265 if (!pfile->timebuf) {
2266 time_t t = time ((time_t *) 0);
2267 pfile->timebuf = localtime (&t);
2269 return pfile->timebuf;
2272 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2273 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2277 * expand things like __FILE__. Place the expansion into the output
2278 * buffer *without* rescanning.
2281 static void
2282 special_symbol (hp, pfile)
2283 HASHNODE *hp;
2284 cpp_reader *pfile;
2286 char *buf;
2287 int i, len;
2288 int true_indepth;
2289 cpp_buffer *ip = NULL;
2290 struct tm *timebuf;
2292 int paren = 0; /* For special `defined' keyword */
2294 #if 0
2295 if (pcp_outfile && pcp_inside_if
2296 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2297 cpp_error (pfile,
2298 "Predefined macro `%s' used inside `#if' during precompilation",
2299 hp->name);
2300 #endif
2302 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2304 if (ip == CPP_NULL_BUFFER (pfile))
2306 cpp_error (pfile, "cccp error: not in any file?!");
2307 return; /* the show must go on */
2309 if (ip->fname != NULL)
2310 break;
2313 switch (hp->type)
2315 case T_FILE:
2316 case T_BASE_FILE:
2318 char *string;
2319 if (hp->type == T_BASE_FILE)
2321 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2322 ip = CPP_PREV_BUFFER (ip);
2324 string = ip->nominal_fname;
2326 if (!string)
2327 string = "";
2328 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2329 quote_string (pfile, string);
2330 return;
2333 case T_INCLUDE_LEVEL:
2334 true_indepth = 0;
2335 ip = CPP_BUFFER (pfile);
2336 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2337 if (ip->fname != NULL)
2338 true_indepth++;
2340 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2341 sprintf (buf, "%d", true_indepth - 1);
2342 break;
2344 case T_VERSION:
2345 buf = (char *) alloca (3 + strlen (version_string));
2346 sprintf (buf, "\"%s\"", version_string);
2347 break;
2349 #ifndef NO_BUILTIN_SIZE_TYPE
2350 case T_SIZE_TYPE:
2351 buf = SIZE_TYPE;
2352 break;
2353 #endif
2355 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2356 case T_PTRDIFF_TYPE:
2357 buf = PTRDIFF_TYPE;
2358 break;
2359 #endif
2361 case T_WCHAR_TYPE:
2362 buf = CPP_WCHAR_TYPE (pfile);
2363 break;
2365 case T_USER_LABEL_PREFIX_TYPE:
2366 buf = USER_LABEL_PREFIX;
2367 break;
2369 case T_REGISTER_PREFIX_TYPE:
2370 buf = REGISTER_PREFIX;
2371 break;
2373 case T_CONST:
2374 buf = (char *) alloca (4 * sizeof (int));
2375 sprintf (buf, "%d", hp->value.ival);
2376 #if 0
2377 if (pcp_inside_if && pcp_outfile)
2378 /* Output a precondition for this macro use */
2379 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2380 #endif
2381 break;
2383 case T_SPECLINE:
2385 long line = ip->lineno;
2386 long col = ip->colno;
2387 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2389 buf = (char *) alloca (10);
2390 sprintf (buf, "%d", line);
2392 break;
2394 case T_DATE:
2395 case T_TIME:
2396 buf = (char *) alloca (20);
2397 timebuf = timestamp (pfile);
2398 if (hp->type == T_DATE)
2399 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2400 timebuf->tm_mday, timebuf->tm_year + 1900);
2401 else
2402 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2403 timebuf->tm_sec);
2404 break;
2406 case T_SPEC_DEFINED:
2407 buf = " 0 "; /* Assume symbol is not defined */
2408 ip = CPP_BUFFER (pfile);
2409 SKIP_WHITE_SPACE (ip->cur);
2410 if (*ip->cur == '(')
2412 paren++;
2413 ip->cur++; /* Skip over the paren */
2414 SKIP_WHITE_SPACE (ip->cur);
2417 if (!is_idstart[*ip->cur])
2418 goto oops;
2419 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2421 #if 0
2422 if (pcp_outfile && pcp_inside_if
2423 && (hp->type == T_CONST
2424 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2425 /* Output a precondition for this macro use. */
2426 fprintf (pcp_outfile, "#define %s\n", hp->name);
2427 #endif
2428 buf = " 1 ";
2430 #if 0
2431 else
2432 if (pcp_outfile && pcp_inside_if)
2434 /* Output a precondition for this macro use */
2435 U_CHAR *cp = ip->bufp;
2436 fprintf (pcp_outfile, "#undef ");
2437 while (is_idchar[*cp]) /* Ick! */
2438 fputc (*cp++, pcp_outfile);
2439 putc ('\n', pcp_outfile);
2441 #endif
2442 while (is_idchar[*ip->cur])
2443 ++ip->cur;
2444 SKIP_WHITE_SPACE (ip->cur);
2445 if (paren)
2447 if (*ip->cur != ')')
2448 goto oops;
2449 ++ip->cur;
2451 break;
2453 oops:
2455 cpp_error (pfile, "`defined' without an identifier");
2456 break;
2458 default:
2459 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2460 abort ();
2462 len = strlen (buf);
2463 CPP_RESERVE (pfile, len + 1);
2464 CPP_PUTS_Q (pfile, buf, len);
2465 CPP_NUL_TERMINATE_Q (pfile);
2467 return;
2470 /* Write out a #define command for the special named MACRO_NAME
2471 to PFILE's token_buffer. */
2473 static void
2474 dump_special_to_buffer (pfile, macro_name)
2475 cpp_reader *pfile;
2476 char *macro_name;
2478 static char define_directive[] = "#define ";
2479 int macro_name_length = strlen (macro_name);
2480 output_line_command (pfile, 0, same_file);
2481 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2482 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2483 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2484 CPP_PUTC_Q (pfile, ' ');
2485 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2486 CPP_PUTC (pfile, '\n');
2489 /* Initialize the built-in macros. */
2491 static void
2492 initialize_builtins (pfile)
2493 cpp_reader *pfile;
2495 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2496 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2497 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2498 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2499 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2500 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2501 #ifndef NO_BUILTIN_SIZE_TYPE
2502 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2503 #endif
2504 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2505 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2506 #endif
2507 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2508 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2509 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2510 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2511 if (!CPP_TRADITIONAL (pfile))
2512 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2513 if (CPP_OPTIONS (pfile)->objc)
2514 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2515 /* This is supplied using a -D by the compiler driver
2516 so that it is present only when truly compiling with GNU C. */
2517 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2519 if (CPP_OPTIONS (pfile)->debug_output)
2521 dump_special_to_buffer (pfile, "__BASE_FILE__");
2522 dump_special_to_buffer (pfile, "__VERSION__");
2523 #ifndef NO_BUILTIN_SIZE_TYPE
2524 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2525 #endif
2526 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2527 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2528 #endif
2529 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2530 dump_special_to_buffer (pfile, "__DATE__");
2531 dump_special_to_buffer (pfile, "__TIME__");
2532 if (!CPP_TRADITIONAL (pfile))
2533 dump_special_to_buffer (pfile, "__STDC__");
2534 if (CPP_OPTIONS (pfile)->objc)
2535 dump_special_to_buffer (pfile, "__OBJC__");
2539 /* Return 1 iff a token ending in C1 followed directly by a token C2
2540 could cause mis-tokenization. */
2542 static int
2543 unsafe_chars (c1, c2)
2544 int c1, c2;
2546 switch (c1)
2548 case '+': case '-':
2549 if (c2 == c1 || c2 == '=')
2550 return 1;
2551 goto letter;
2552 case '.':
2553 case '0': case '1': case '2': case '3': case '4':
2554 case '5': case '6': case '7': case '8': case '9':
2555 case 'e': case 'E':
2556 if (c2 == '-' || c2 == '+')
2557 return 1; /* could extend a pre-processing number */
2558 goto letter;
2559 case 'L':
2560 if (c2 == '\'' || c2 == '\"')
2561 return 1; /* Could turn into L"xxx" or L'xxx'. */
2562 goto letter;
2563 letter:
2564 case '_':
2565 case 'a': case 'b': case 'c': case 'd': case 'f':
2566 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2567 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2568 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2569 case 'y': case 'z':
2570 case 'A': case 'B': case 'C': case 'D': case 'F':
2571 case 'G': case 'H': case 'I': case 'J': case 'K':
2572 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2573 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2574 case 'Y': case 'Z':
2575 /* We're in the middle of either a name or a pre-processing number. */
2576 return (is_idchar[c2] || c2 == '.');
2577 case '<': case '>': case '!': case '%': case '#': case ':':
2578 case '^': case '&': case '|': case '*': case '/': case '=':
2579 return (c2 == c1 || c2 == '=');
2581 return 0;
2584 /* Expand a macro call.
2585 HP points to the symbol that is the macro being called.
2586 Put the result of expansion onto the input stack
2587 so that subsequent input by our caller will use it.
2589 If macro wants arguments, caller has already verified that
2590 an argument list follows; arguments come from the input stack. */
2592 static void
2593 macroexpand (pfile, hp)
2594 cpp_reader *pfile;
2595 HASHNODE *hp;
2597 int nargs;
2598 DEFINITION *defn = hp->value.defn;
2599 register U_CHAR *xbuf;
2600 long start_line, start_column;
2601 int xbuf_len;
2602 struct argdata *args;
2603 long old_written = CPP_WRITTEN (pfile);
2604 #if 0
2605 int start_line = instack[indepth].lineno;
2606 #endif
2607 int rest_args, rest_zero;
2608 register int i;
2610 #if 0
2611 CHECK_DEPTH (return;);
2612 #endif
2614 #if 0
2615 /* This macro is being used inside a #if, which means it must be */
2616 /* recorded as a precondition. */
2617 if (pcp_inside_if && pcp_outfile && defn->predefined)
2618 dump_single_macro (hp, pcp_outfile);
2619 #endif
2621 pfile->output_escapes++;
2622 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2624 nargs = defn->nargs;
2626 if (nargs >= 0)
2628 enum cpp_token token;
2630 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2632 for (i = 0; i < nargs; i++)
2634 args[i].raw = args[i].expanded = 0;
2635 args[i].raw_length = 0;
2636 args[i].expand_length = args[i].stringified_length = -1;
2637 args[i].use_count = 0;
2640 /* Parse all the macro args that are supplied. I counts them.
2641 The first NARGS args are stored in ARGS.
2642 The rest are discarded. If rest_args is set then we assume
2643 macarg absorbed the rest of the args. */
2644 i = 0;
2645 rest_args = 0;
2646 rest_args = 0;
2647 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2650 if (rest_args)
2651 continue;
2652 if (i < nargs || (nargs == 0 && i == 0))
2654 /* if we are working on last arg which absorbs rest of args... */
2655 if (i == nargs - 1 && defn->rest_args)
2656 rest_args = 1;
2657 args[i].raw = CPP_WRITTEN (pfile);
2658 token = macarg (pfile, rest_args);
2659 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2660 args[i].newlines = 0; /* FIXME */
2662 else
2663 token = macarg (pfile, 0);
2664 if (token == CPP_EOF || token == CPP_POP)
2666 cpp_error_with_line (pfile, start_line, start_column,
2667 "unterminated macro call");
2668 return;
2670 i++;
2671 } while (token == CPP_COMMA);
2673 /* If we got one arg but it was just whitespace, call that 0 args. */
2674 if (i == 1)
2676 register U_CHAR *bp = ARG_BASE + args[0].raw;
2677 register U_CHAR *lim = bp + args[0].raw_length;
2678 /* cpp.texi says for foo ( ) we provide one argument.
2679 However, if foo wants just 0 arguments, treat this as 0. */
2680 if (nargs == 0)
2681 while (bp != lim && is_space[*bp]) bp++;
2682 if (bp == lim)
2683 i = 0;
2686 /* Don't output an error message if we have already output one for
2687 a parse error above. */
2688 rest_zero = 0;
2689 if (nargs == 0 && i > 0)
2691 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2693 else if (i < nargs)
2695 /* traditional C allows foo() if foo wants one argument. */
2696 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2698 /* the rest args token is allowed to absorb 0 tokens */
2699 else if (i == nargs - 1 && defn->rest_args)
2700 rest_zero = 1;
2701 else if (i == 0)
2702 cpp_error (pfile, "macro `%s' used without args", hp->name);
2703 else if (i == 1)
2704 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2705 else
2706 cpp_error (pfile, "macro `%s' used with only %d args",
2707 hp->name, i);
2709 else if (i > nargs)
2711 cpp_error (pfile,
2712 "macro `%s' used with too many (%d) args", hp->name, i);
2716 /* If macro wants zero args, we parsed the arglist for checking only.
2717 Read directly from the macro definition. */
2718 if (nargs <= 0)
2720 xbuf = defn->expansion;
2721 xbuf_len = defn->length;
2723 else
2725 register U_CHAR *exp = defn->expansion;
2726 register int offset; /* offset in expansion,
2727 copied a piece at a time */
2728 register int totlen; /* total amount of exp buffer filled so far */
2730 register struct reflist *ap, *last_ap;
2732 /* Macro really takes args. Compute the expansion of this call. */
2734 /* Compute length in characters of the macro's expansion.
2735 Also count number of times each arg is used. */
2736 xbuf_len = defn->length;
2737 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2739 if (ap->stringify)
2741 register struct argdata *arg = &args[ap->argno];
2742 /* Stringify it it hasn't already been */
2743 if (arg->stringified_length < 0)
2745 int arglen = arg->raw_length;
2746 int escaped = 0;
2747 int in_string = 0;
2748 int c;
2749 /* Initially need_space is -1. Otherwise, 1 means the
2750 previous character was a space, but we suppressed it;
2751 0 means the previous character was a non-space. */
2752 int need_space = -1;
2753 i = 0;
2754 arg->stringified = CPP_WRITTEN (pfile);
2755 if (!CPP_TRADITIONAL (pfile))
2756 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2757 for (; i < arglen; i++)
2759 c = (ARG_BASE + arg->raw)[i];
2761 if (! in_string)
2763 /* Internal sequences of whitespace are replaced by
2764 one space except within an string or char token.*/
2765 if (is_space[c])
2767 if (CPP_WRITTEN (pfile) > arg->stringified
2768 && (CPP_PWRITTEN (pfile))[-1] == '@')
2770 /* "@ " escape markers are removed */
2771 CPP_ADJUST_WRITTEN (pfile, -1);
2772 continue;
2774 if (need_space == 0)
2775 need_space = 1;
2776 continue;
2778 else if (need_space > 0)
2779 CPP_PUTC (pfile, ' ');
2780 need_space = 0;
2783 if (escaped)
2784 escaped = 0;
2785 else
2787 if (c == '\\')
2788 escaped = 1;
2789 if (in_string)
2791 if (c == in_string)
2792 in_string = 0;
2794 else if (c == '\"' || c == '\'')
2795 in_string = c;
2798 /* Escape these chars */
2799 if (c == '\"' || (in_string && c == '\\'))
2800 CPP_PUTC (pfile, '\\');
2801 if (isprint (c))
2802 CPP_PUTC (pfile, c);
2803 else
2805 CPP_RESERVE (pfile, 4);
2806 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2807 (unsigned int) c);
2808 CPP_ADJUST_WRITTEN (pfile, 4);
2811 if (!CPP_TRADITIONAL (pfile))
2812 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2813 arg->stringified_length
2814 = CPP_WRITTEN (pfile) - arg->stringified;
2816 xbuf_len += args[ap->argno].stringified_length;
2818 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2819 /* Add 4 for two newline-space markers to prevent
2820 token concatenation. */
2821 xbuf_len += args[ap->argno].raw_length + 4;
2822 else
2824 /* We have an ordinary (expanded) occurrence of the arg.
2825 So compute its expansion, if we have not already. */
2826 if (args[ap->argno].expand_length < 0)
2828 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2829 cpp_expand_to_buffer (pfile,
2830 ARG_BASE + args[ap->argno].raw,
2831 args[ap->argno].raw_length);
2833 args[ap->argno].expand_length
2834 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2837 /* Add 4 for two newline-space markers to prevent
2838 token concatenation. */
2839 xbuf_len += args[ap->argno].expand_length + 4;
2841 if (args[ap->argno].use_count < 10)
2842 args[ap->argno].use_count++;
2845 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2847 /* Generate in XBUF the complete expansion
2848 with arguments substituted in.
2849 TOTLEN is the total size generated so far.
2850 OFFSET is the index in the definition
2851 of where we are copying from. */
2852 offset = totlen = 0;
2853 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2854 last_ap = ap, ap = ap->next)
2856 register struct argdata *arg = &args[ap->argno];
2857 int count_before = totlen;
2859 /* Add chars to XBUF. */
2860 for (i = 0; i < ap->nchars; i++, offset++)
2861 xbuf[totlen++] = exp[offset];
2863 /* If followed by an empty rest arg with concatenation,
2864 delete the last run of nonwhite chars. */
2865 if (rest_zero && totlen > count_before
2866 && ((ap->rest_args && ap->raw_before)
2867 || (last_ap != NULL && last_ap->rest_args
2868 && last_ap->raw_after)))
2870 /* Delete final whitespace. */
2871 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2872 totlen--;
2874 /* Delete the nonwhites before them. */
2875 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2876 totlen--;
2879 if (ap->stringify != 0)
2881 bcopy (ARG_BASE + arg->stringified,
2882 xbuf + totlen, arg->stringified_length);
2883 totlen += arg->stringified_length;
2885 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2887 U_CHAR *p1 = ARG_BASE + arg->raw;
2888 U_CHAR *l1 = p1 + arg->raw_length;
2889 if (ap->raw_before)
2891 while (p1 != l1 && is_space[*p1]) p1++;
2892 while (p1 != l1 && is_idchar[*p1])
2893 xbuf[totlen++] = *p1++;
2895 if (ap->raw_after)
2897 /* Arg is concatenated after: delete trailing whitespace,
2898 whitespace markers, and no-reexpansion markers. */
2899 while (p1 != l1)
2901 if (is_space[l1[-1]]) l1--;
2902 else if (l1[-1] == '-')
2904 U_CHAR *p2 = l1 - 1;
2905 /* If a `-' is preceded by an odd number of newlines then it
2906 and the last newline are a no-reexpansion marker. */
2907 while (p2 != p1 && p2[-1] == '\n') p2--;
2908 if ((l1 - 1 - p2) & 1) {
2909 l1 -= 2;
2911 else break;
2913 else break;
2917 bcopy (p1, xbuf + totlen, l1 - p1);
2918 totlen += l1 - p1;
2920 else
2922 U_CHAR *expanded = ARG_BASE + arg->expanded;
2923 if (!ap->raw_before && totlen > 0 && arg->expand_length
2924 && !CPP_TRADITIONAL(pfile)
2925 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2927 xbuf[totlen++] = '@';
2928 xbuf[totlen++] = ' ';
2931 bcopy (expanded, xbuf + totlen, arg->expand_length);
2932 totlen += arg->expand_length;
2934 if (!ap->raw_after && totlen > 0 && offset < defn->length
2935 && !CPP_TRADITIONAL(pfile)
2936 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2938 xbuf[totlen++] = '@';
2939 xbuf[totlen++] = ' ';
2942 /* If a macro argument with newlines is used multiple times,
2943 then only expand the newlines once. This avoids creating
2944 output lines which don't correspond to any input line,
2945 which confuses gdb and gcov. */
2946 if (arg->use_count > 1 && arg->newlines > 0)
2948 /* Don't bother doing change_newlines for subsequent
2949 uses of arg. */
2950 arg->use_count = 1;
2951 arg->expand_length
2952 = change_newlines (expanded, arg->expand_length);
2956 if (totlen > xbuf_len)
2957 abort ();
2960 /* if there is anything left of the definition
2961 after handling the arg list, copy that in too. */
2963 for (i = offset; i < defn->length; i++)
2965 /* if we've reached the end of the macro */
2966 if (exp[i] == ')')
2967 rest_zero = 0;
2968 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2969 && last_ap->raw_after))
2970 xbuf[totlen++] = exp[i];
2973 xbuf[totlen] = 0;
2974 xbuf_len = totlen;
2978 pfile->output_escapes--;
2980 /* Now put the expansion on the input stack
2981 so our caller will commence reading from it. */
2982 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2983 CPP_BUFFER (pfile)->has_escapes = 1;
2985 /* Pop the space we've used in the token_buffer for argument expansion. */
2986 CPP_SET_WRITTEN (pfile, old_written);
2988 /* Recursive macro use sometimes works traditionally.
2989 #define foo(x,y) bar (x (y,0), y)
2990 foo (foo, baz) */
2992 if (!CPP_TRADITIONAL (pfile))
2993 hp->type = T_DISABLED;
2996 static void
2997 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2998 cpp_reader *pfile;
2999 register U_CHAR *xbuf;
3000 int xbuf_len;
3001 HASHNODE *hp;
3003 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3004 if (mbuf == NULL)
3005 return;
3006 mbuf->cleanup = macro_cleanup;
3007 mbuf->data = hp;
3009 /* The first chars of the expansion should be a "@ " added by
3010 collect_expansion. This is to prevent accidental token-pasting
3011 between the text preceding the macro invocation, and the macro
3012 expansion text.
3014 We would like to avoid adding unneeded spaces (for the sake of
3015 tools that use cpp, such as imake). In some common cases we can
3016 tell that it is safe to omit the space.
3018 The character before the macro invocation cannot have been an
3019 idchar (or else it would have been pasted with the idchars of
3020 the macro name). Therefore, if the first non-space character
3021 of the expansion is an idchar, we do not need the extra space
3022 to prevent token pasting.
3024 Also, we don't need the extra space if the first char is '(',
3025 or some other (less common) characters. */
3027 if (xbuf[0] == '@' && xbuf[1] == ' '
3028 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3029 || xbuf[2] == '\"'))
3030 mbuf->cur += 2;
3033 /* Like cpp_get_token, except that it does not read past end-of-line.
3034 Also, horizontal space is skipped, and macros are popped. */
3036 static enum cpp_token
3037 get_directive_token (pfile)
3038 cpp_reader *pfile;
3040 for (;;)
3042 long old_written = CPP_WRITTEN (pfile);
3043 enum cpp_token token;
3044 cpp_skip_hspace (pfile);
3045 if (PEEKC () == '\n')
3046 return CPP_VSPACE;
3047 token = cpp_get_token (pfile);
3048 switch (token)
3050 case CPP_POP:
3051 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3052 return token;
3053 /* ... else fall though ... */
3054 case CPP_HSPACE: case CPP_COMMENT:
3055 CPP_SET_WRITTEN (pfile, old_written);
3056 break;
3057 default:
3058 return token;
3063 /* Handle #include and #import.
3064 This function expects to see "fname" or <fname> on the input.
3066 The input is normally in part of the output_buffer following
3067 CPP_WRITTEN, and will get overwritten by output_line_command.
3068 I.e. in input file specification has been popped by handle_directive.
3069 This is safe. */
3071 static int
3072 do_include (pfile, keyword, unused1, unused2)
3073 cpp_reader *pfile;
3074 struct directive *keyword;
3075 U_CHAR *unused1, *unused2;
3077 int importing = (keyword->type == T_IMPORT);
3078 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3079 char *fname; /* Dynamically allocated fname buffer */
3080 char *pcftry;
3081 char *pcfname;
3082 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3083 enum cpp_token token;
3085 /* Chain of dirs to search */
3086 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3087 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3088 struct file_name_list *searchptr = 0;
3089 long old_written = CPP_WRITTEN (pfile);
3091 int flen;
3093 int f; /* file number */
3095 int retried = 0; /* Have already tried macro
3096 expanding the include line */
3097 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3098 int pcf = -1;
3099 char *pcfbuf;
3100 char *pcfbuflimit;
3101 int pcfnum;
3102 f= -1; /* JF we iz paranoid! */
3104 if (importing && CPP_OPTIONS (pfile)->warn_import
3105 && !CPP_OPTIONS (pfile)->inhibit_warnings
3106 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3108 pfile->import_warning = 1;
3109 cpp_warning (pfile, "using `#import' is not recommended");
3110 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3111 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3112 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3113 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3114 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3115 fprintf (stderr, " ... <real contents of file> ...\n");
3116 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3117 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3118 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3119 fprintf (stderr, "when it is equipped with such a conditional.\n");
3122 pfile->parsing_include_directive++;
3123 token = get_directive_token (pfile);
3124 pfile->parsing_include_directive--;
3126 if (token == CPP_STRING)
3128 /* FIXME - check no trailing garbage */
3129 fbeg = pfile->token_buffer + old_written + 1;
3130 fend = CPP_PWRITTEN (pfile) - 1;
3131 if (fbeg[-1] == '<')
3133 angle_brackets = 1;
3134 /* If -I-, start with the first -I dir after the -I-. */
3135 if (CPP_OPTIONS (pfile)->first_bracket_include)
3136 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3138 /* If -I- was specified, don't search current dir, only spec'd ones. */
3139 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3141 cpp_buffer *fp = CPP_BUFFER (pfile);
3142 /* We have "filename". Figure out directory this source
3143 file is coming from and put it on the front of the list. */
3145 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3147 int n;
3148 char *ep,*nam;
3150 if ((nam = fp->nominal_fname) != NULL)
3152 /* Found a named file. Figure out dir of the file,
3153 and put it in front of the search list. */
3154 dsp[0].next = search_start;
3155 search_start = dsp;
3156 #ifndef VMS
3157 ep = rindex (nam, '/');
3158 #else /* VMS */
3159 ep = rindex (nam, ']');
3160 if (ep == NULL) ep = rindex (nam, '>');
3161 if (ep == NULL) ep = rindex (nam, ':');
3162 if (ep != NULL) ep++;
3163 #endif /* VMS */
3164 if (ep != NULL)
3166 n = ep - nam;
3167 dsp[0].fname = (char *) alloca (n + 1);
3168 strncpy (dsp[0].fname, nam, n);
3169 dsp[0].fname[n] = '\0';
3170 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3171 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3173 else
3175 dsp[0].fname = 0; /* Current directory */
3177 dsp[0].got_name_map = 0;
3178 break;
3183 #ifdef VMS
3184 else if (token == CPP_NAME)
3187 * Support '#include xyz' like VAX-C to allow for easy use of all the
3188 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3189 * code from case '<' is repeated here) and generates a warning.
3191 cpp_warning (pfile,
3192 "VAX-C-style include specification found, use '#include <filename.h>' !");
3193 angle_brackets = 1;
3194 /* If -I-, start with the first -I dir after the -I-. */
3195 if (CPP_OPTIONS (pfile)->first_bracket_include)
3196 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3197 fbeg = pfile->token_buffer + old_written;
3198 fend = CPP_PWRITTEN (pfile);
3200 #endif
3201 else
3203 cpp_error (pfile,
3204 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3205 CPP_SET_WRITTEN (pfile, old_written);
3206 skip_rest_of_line (pfile);
3207 return 0;
3210 *fend = 0;
3212 token = get_directive_token (pfile);
3213 if (token != CPP_VSPACE)
3215 cpp_error (pfile, "junk at end of `#include'");
3216 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3217 token = get_directive_token (pfile);
3220 /* For #include_next, skip in the search path
3221 past the dir in which the containing file was found. */
3222 if (skip_dirs)
3224 cpp_buffer *fp = CPP_BUFFER (pfile);
3225 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3226 if (fp->fname != NULL)
3228 /* fp->dir is null if the containing file was specified with
3229 an absolute file name. In that case, don't skip anything. */
3230 if (fp->dir == SELF_DIR_DUMMY)
3231 search_start = CPP_OPTIONS (pfile)->include;
3232 else if (fp->dir)
3233 search_start = fp->dir->next;
3234 break;
3238 CPP_SET_WRITTEN (pfile, old_written);
3240 flen = fend - fbeg;
3242 if (flen == 0)
3244 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3245 return 0;
3248 /* Allocate this permanently, because it gets stored in the definitions
3249 of macros. */
3250 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3251 /* + 2 above for slash and terminating null. */
3252 /* + 2 added for '.h' on VMS (to support '#include filename') */
3254 /* If specified file name is absolute, just open it. */
3256 if (*fbeg == '/') {
3257 strncpy (fname, fbeg, flen);
3258 fname[flen] = 0;
3259 if (redundant_include_p (pfile, fname))
3260 return 0;
3261 if (importing)
3262 f = lookup_import (pfile, fname, NULL_PTR);
3263 else
3264 f = open_include_file (pfile, fname, NULL_PTR);
3265 if (f == -2)
3266 return 0; /* Already included this file */
3267 } else {
3268 /* Search directory path, trying to open the file.
3269 Copy each filename tried into FNAME. */
3271 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3272 if (searchptr->fname) {
3273 /* The empty string in a search path is ignored.
3274 This makes it possible to turn off entirely
3275 a standard piece of the list. */
3276 if (searchptr->fname[0] == 0)
3277 continue;
3278 strcpy (fname, searchptr->fname);
3279 strcat (fname, "/");
3280 fname[strlen (fname) + flen] = 0;
3281 } else {
3282 fname[0] = 0;
3284 strncat (fname, fbeg, flen);
3285 #ifdef VMS
3286 /* Change this 1/2 Unix 1/2 VMS file specification into a
3287 full VMS file specification */
3288 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3289 /* Fix up the filename */
3290 hack_vms_include_specification (fname);
3291 } else {
3292 /* This is a normal VMS filespec, so use it unchanged. */
3293 strncpy (fname, fbeg, flen);
3294 fname[flen] = 0;
3295 /* if it's '#include filename', add the missing .h */
3296 if (index(fname,'.')==NULL) {
3297 strcat (fname, ".h");
3300 #endif /* VMS */
3301 /* ??? There are currently 3 separate mechanisms for avoiding processing
3302 of redundant include files: #import, #pragma once, and
3303 redundant_include_p. It would be nice if they were unified. */
3304 if (redundant_include_p (pfile, fname))
3305 return 0;
3306 if (importing)
3307 f = lookup_import (pfile, fname, searchptr);
3308 else
3309 f = open_include_file (pfile, fname, searchptr);
3310 if (f == -2)
3311 return 0; /* Already included this file */
3312 #ifdef EACCES
3313 else if (f == -1 && errno == EACCES)
3314 cpp_warning (pfile, "Header file %s exists, but is not readable",
3315 fname);
3316 #endif
3317 if (f >= 0)
3318 break;
3322 if (f < 0)
3324 /* A file that was not found. */
3325 strncpy (fname, fbeg, flen);
3326 fname[flen] = 0;
3327 /* If generating dependencies and -MG was specified, we assume missing
3328 files are leaf files, living in the same directory as the source file
3329 or other similar place; these missing files may be generated from
3330 other files and may not exist yet (eg: y.tab.h). */
3332 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3333 && CPP_PRINT_DEPS (pfile)
3334 > (angle_brackets || (pfile->system_include_depth > 0)))
3336 /* If it was requested as a system header file,
3337 then assume it belongs in the first place to look for such. */
3338 if (angle_brackets)
3340 for (searchptr = search_start; searchptr;
3341 searchptr = searchptr->next)
3343 if (searchptr->fname)
3345 char *p;
3347 if (searchptr->fname[0] == 0)
3348 continue;
3349 p = (char *) alloca (strlen (searchptr->fname)
3350 + strlen (fname) + 2);
3351 strcpy (p, searchptr->fname);
3352 strcat (p, "/");
3353 strcat (p, fname);
3354 deps_output (pfile, p, ' ');
3355 break;
3359 else
3361 /* Otherwise, omit the directory, as if the file existed
3362 in the directory with the source. */
3363 deps_output (pfile, fname, ' ');
3366 /* If -M was specified, and this header file won't be added to the
3367 dependency list, then don't count this as an error, because we can
3368 still produce correct output. Otherwise, we can't produce correct
3369 output, because there may be dependencies we need inside the missing
3370 file, and we don't know what directory this missing file exists in.*/
3371 else if (CPP_PRINT_DEPS (pfile)
3372 && (CPP_PRINT_DEPS (pfile)
3373 <= (angle_brackets || (pfile->system_include_depth > 0))))
3374 cpp_warning (pfile, "No include path in which to find %s", fname);
3375 else if (search_start)
3376 cpp_error_from_errno (pfile, fname);
3377 else
3378 cpp_error (pfile, "No include path in which to find %s", fname);
3380 else {
3381 /* Check to see if this include file is a once-only include file.
3382 If so, give up. */
3384 struct file_name_list *ptr;
3386 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3387 if (!strcmp (ptr->fname, fname)) {
3388 close (f);
3389 return 0; /* This file was once'd. */
3393 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3394 if (!strcmp (ptr->fname, fname))
3395 break; /* This file was included before. */
3398 if (ptr == 0) {
3399 /* This is the first time for this file. */
3400 /* Add it to list of files included. */
3402 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3403 ptr->control_macro = 0;
3404 ptr->c_system_include_path = 0;
3405 ptr->next = pfile->all_include_files;
3406 pfile->all_include_files = ptr;
3407 ptr->fname = savestring (fname);
3408 ptr->got_name_map = 0;
3410 /* For -M, add this file to the dependencies. */
3411 if (CPP_PRINT_DEPS (pfile)
3412 > (angle_brackets || (pfile->system_include_depth > 0)))
3413 deps_output (pfile, fname, ' ');
3416 /* Handle -H option. */
3417 if (CPP_OPTIONS(pfile)->print_include_names)
3419 cpp_buffer *buf = CPP_BUFFER (pfile);
3420 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3421 putc ('.', stderr);
3422 fprintf (stderr, "%s\n", fname);
3425 if (angle_brackets)
3426 pfile->system_include_depth++;
3428 /* Actually process the file. */
3430 /* Record file on "seen" list for #import. */
3431 add_import (pfile, f, fname);
3433 pcftry = (char *) alloca (strlen (fname) + 30);
3434 pcfbuf = 0;
3435 pcfnum = 0;
3437 #if 0
3438 if (!no_precomp)
3440 struct stat stat_f;
3442 fstat (f, &stat_f);
3444 do {
3445 sprintf (pcftry, "%s%d", fname, pcfnum++);
3447 pcf = open (pcftry, O_RDONLY, 0666);
3448 if (pcf != -1)
3450 struct stat s;
3452 fstat (pcf, &s);
3453 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3454 sizeof (s.st_ino))
3455 || stat_f.st_dev != s.st_dev)
3457 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3458 /* Don't need it any more. */
3459 close (pcf);
3461 else
3463 /* Don't need it at all. */
3464 close (pcf);
3465 break;
3468 } while (pcf != -1 && !pcfbuf);
3470 #endif
3472 /* Actually process the file */
3473 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3474 return 0;
3475 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3476 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3478 output_line_command (pfile, 0, enter_file);
3479 pfile->only_seen_white = 2;
3482 if (angle_brackets)
3483 pfile->system_include_depth--;
3485 return 0;
3488 /* Return nonzero if there is no need to include file NAME
3489 because it has already been included and it contains a conditional
3490 to make a repeated include do nothing. */
3492 static int
3493 redundant_include_p (pfile, name)
3494 cpp_reader *pfile;
3495 char *name;
3497 struct file_name_list *l = pfile->all_include_files;
3498 for (; l; l = l->next)
3499 if (! strcmp (name, l->fname)
3500 && l->control_macro
3501 && cpp_lookup (pfile, l->control_macro, -1, -1))
3502 return 1;
3503 return 0;
3506 /* Return nonzero if the given FILENAME is an absolute pathname which
3507 designates a file within one of the known "system" include file
3508 directories. We assume here that if the given FILENAME looks like
3509 it is the name of a file which resides either directly in a "system"
3510 include file directory, or within any subdirectory thereof, then the
3511 given file must be a "system" include file. This function tells us
3512 if we should suppress pedantic errors/warnings for the given FILENAME.
3514 The value is 2 if the file is a C-language system header file
3515 for which C++ should (on most systems) assume `extern "C"'. */
3517 static int
3518 is_system_include (pfile, filename)
3519 cpp_reader *pfile;
3520 register char *filename;
3522 struct file_name_list *searchptr;
3524 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3525 searchptr = searchptr->next)
3526 if (searchptr->fname) {
3527 register char *sys_dir = searchptr->fname;
3528 register unsigned length = strlen (sys_dir);
3530 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3532 if (searchptr->c_system_include_path)
3533 return 2;
3534 else
3535 return 1;
3538 return 0;
3543 * Install a name in the assertion hash table.
3545 * If LEN is >= 0, it is the length of the name.
3546 * Otherwise, compute the length by scanning the entire name.
3548 * If HASH is >= 0, it is the precomputed hash code.
3549 * Otherwise, compute the hash code.
3552 static ASSERTION_HASHNODE *
3553 assertion_install (pfile, name, len, hash)
3554 cpp_reader *pfile;
3555 U_CHAR *name;
3556 int len;
3557 int hash;
3559 register ASSERTION_HASHNODE *hp;
3560 register int i, bucket;
3561 register U_CHAR *p, *q;
3563 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3564 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3565 bucket = hash;
3566 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3567 hp->next = pfile->assertion_hashtab[bucket];
3568 pfile->assertion_hashtab[bucket] = hp;
3569 hp->prev = NULL;
3570 if (hp->next != NULL)
3571 hp->next->prev = hp;
3572 hp->length = len;
3573 hp->value = 0;
3574 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3575 p = hp->name;
3576 q = name;
3577 for (i = 0; i < len; i++)
3578 *p++ = *q++;
3579 hp->name[len] = 0;
3580 return hp;
3583 * find the most recent hash node for name name (ending with first
3584 * non-identifier char) installed by install
3586 * If LEN is >= 0, it is the length of the name.
3587 * Otherwise, compute the length by scanning the entire name.
3589 * If HASH is >= 0, it is the precomputed hash code.
3590 * Otherwise, compute the hash code.
3593 static ASSERTION_HASHNODE *
3594 assertion_lookup (pfile, name, len, hash)
3595 cpp_reader *pfile;
3596 U_CHAR *name;
3597 int len;
3598 int hash;
3600 register ASSERTION_HASHNODE *bucket;
3602 bucket = pfile->assertion_hashtab[hash];
3603 while (bucket) {
3604 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3605 return bucket;
3606 bucket = bucket->next;
3608 return NULL;
3611 static void
3612 delete_assertion (hp)
3613 ASSERTION_HASHNODE *hp;
3615 struct tokenlist_list *tail;
3616 if (hp->prev != NULL)
3617 hp->prev->next = hp->next;
3618 if (hp->next != NULL)
3619 hp->next->prev = hp->prev;
3621 for (tail = hp->value; tail; )
3623 struct tokenlist_list *next = tail->next;
3624 free_token_list (tail->tokens);
3625 free (tail);
3626 tail = next;
3629 /* Make sure that the bucket chain header that
3630 the deleted guy was on points to the right thing afterwards. */
3631 if (hp == *hp->bucket_hdr)
3632 *hp->bucket_hdr = hp->next;
3634 free (hp);
3637 /* Convert a character string literal into a nul-terminated string.
3638 The input string is [IN ... LIMIT).
3639 The result is placed in RESULT. RESULT can be the same as IN.
3640 The value returned in the end of the string written to RESULT,
3641 or NULL on error. */
3643 static U_CHAR *
3644 convert_string (pfile, result, in, limit, handle_escapes)
3645 cpp_reader *pfile;
3646 register U_CHAR *result, *in, *limit;
3647 int handle_escapes;
3649 U_CHAR c;
3650 c = *in++;
3651 if (c != '\"')
3652 return NULL;
3653 while (in < limit)
3655 U_CHAR c = *in++;
3656 switch (c)
3658 case '\0':
3659 return NULL;
3660 case '\"':
3661 limit = in;
3662 break;
3663 case '\\':
3664 if (handle_escapes)
3666 char *bpc = (char *) in;
3667 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3668 in = (U_CHAR *) bpc;
3669 if (i >= 0)
3670 *result++ = (U_CHAR)c;
3671 break;
3673 /* else fall through */
3674 default:
3675 *result++ = c;
3678 *result = 0;
3679 return result;
3683 * interpret #line command. Remembers previously seen fnames
3684 * in its very own hash table.
3686 #define FNAME_HASHSIZE 37
3688 static int
3689 do_line (pfile, keyword)
3690 cpp_reader *pfile;
3691 struct directive *keyword;
3693 cpp_buffer *ip = CPP_BUFFER (pfile);
3694 int new_lineno;
3695 long old_written = CPP_WRITTEN (pfile);
3696 enum file_change_code file_change = same_file;
3697 enum cpp_token token;
3698 int i;
3700 token = get_directive_token (pfile);
3702 if (token != CPP_NUMBER
3703 || !isdigit(pfile->token_buffer[old_written]))
3705 cpp_error (pfile, "invalid format `#line' command");
3706 goto bad_line_directive;
3709 /* The Newline at the end of this line remains to be processed.
3710 To put the next line at the specified line number,
3711 we must store a line number now that is one less. */
3712 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3713 CPP_SET_WRITTEN (pfile, old_written);
3715 /* NEW_LINENO is one less than the actual line number here. */
3716 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3717 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3719 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3720 if (PEEKC() && !is_space[PEEKC()]) {
3721 cpp_error (pfile, "invalid format `#line' command");
3722 goto bad_line_directive;
3724 #endif
3726 token = get_directive_token (pfile);
3728 if (token == CPP_STRING) {
3729 U_CHAR *fname = pfile->token_buffer + old_written;
3730 U_CHAR *end_name;
3731 static HASHNODE *fname_table[FNAME_HASHSIZE];
3732 HASHNODE *hp, **hash_bucket;
3733 U_CHAR *p;
3734 long num_start;
3735 int fname_length;
3737 /* Turn the file name, which is a character string literal,
3738 into a null-terminated string. Do this in place. */
3739 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3740 if (end_name == NULL)
3742 cpp_error (pfile, "invalid format `#line' command");
3743 goto bad_line_directive;
3746 fname_length = end_name - fname;
3748 num_start = CPP_WRITTEN (pfile);
3749 token = get_directive_token (pfile);
3750 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3751 p = pfile->token_buffer + num_start;
3752 if (CPP_PEDANTIC (pfile))
3753 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3755 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3757 cpp_error (pfile, "invalid format `#line' command");
3758 goto bad_line_directive;
3760 if (*p == '1')
3761 file_change = enter_file;
3762 else if (*p == 2)
3763 file_change = leave_file;
3764 else if (*p == 3)
3765 ip->system_header_p = 1;
3766 else /* if (*p == 4) */
3767 ip->system_header_p = 2;
3769 CPP_SET_WRITTEN (pfile, num_start);
3770 token = get_directive_token (pfile);
3771 p = pfile->token_buffer + num_start;
3772 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3773 ip->system_header_p = *p == 3 ? 1 : 2;
3774 token = get_directive_token (pfile);
3776 if (token != CPP_VSPACE) {
3777 cpp_error (pfile, "invalid format `#line' command");
3778 goto bad_line_directive;
3782 hash_bucket =
3783 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3784 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3785 if (hp->length == fname_length &&
3786 strncmp (hp->value.cpval, fname, fname_length) == 0) {
3787 ip->nominal_fname = hp->value.cpval;
3788 break;
3790 if (hp == 0) {
3791 /* Didn't find it; cons up a new one. */
3792 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3793 hp->next = *hash_bucket;
3794 *hash_bucket = hp;
3796 hp->length = fname_length;
3797 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3798 bcopy (fname, hp->value.cpval, fname_length);
3801 else if (token != CPP_VSPACE && token != CPP_EOF) {
3802 cpp_error (pfile, "invalid format `#line' command");
3803 goto bad_line_directive;
3806 ip->lineno = new_lineno;
3807 bad_line_directive:
3808 skip_rest_of_line (pfile);
3809 CPP_SET_WRITTEN (pfile, old_written);
3810 output_line_command (pfile, 0, file_change);
3811 return 0;
3815 * remove the definition of a symbol from the symbol table.
3816 * according to un*x /lib/cpp, it is not an error to undef
3817 * something that has no definitions, so it isn't one here either.
3820 static int
3821 do_undef (pfile, keyword, buf, limit)
3822 cpp_reader *pfile;
3823 struct directive *keyword;
3824 U_CHAR *buf, *limit;
3826 int sym_length;
3827 HASHNODE *hp;
3828 U_CHAR *orig_buf = buf;
3830 #if 0
3831 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3832 if (pcp_outfile && keyword)
3833 pass_thru_directive (buf, limit, pfile, keyword);
3834 #endif
3836 SKIP_WHITE_SPACE (buf);
3837 sym_length = check_macro_name (pfile, buf, "macro");
3839 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3841 /* If we are generating additional info for debugging (with -g) we
3842 need to pass through all effective #undef commands. */
3843 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3844 pass_thru_directive (orig_buf, limit, pfile, keyword);
3845 if (hp->type != T_MACRO)
3846 cpp_warning (pfile, "undefining `%s'", hp->name);
3847 delete_macro (hp);
3850 if (CPP_PEDANTIC (pfile)) {
3851 buf += sym_length;
3852 SKIP_WHITE_SPACE (buf);
3853 if (buf != limit)
3854 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3856 return 0;
3860 * Report an error detected by the program we are processing.
3861 * Use the text of the line in the error message.
3862 * (We use error because it prints the filename & line#.)
3865 static int
3866 do_error (pfile, keyword, buf, limit)
3867 cpp_reader *pfile;
3868 struct directive *keyword;
3869 U_CHAR *buf, *limit;
3871 int length = limit - buf;
3872 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3873 bcopy (buf, copy, length);
3874 copy[length] = 0;
3875 SKIP_WHITE_SPACE (copy);
3876 cpp_error (pfile, "#error %s", copy);
3877 return 0;
3881 * Report a warning detected by the program we are processing.
3882 * Use the text of the line in the warning message, then continue.
3883 * (We use error because it prints the filename & line#.)
3886 static int
3887 do_warning (pfile, keyword, buf, limit)
3888 cpp_reader *pfile;
3889 struct directive *keyword;
3890 U_CHAR *buf, *limit;
3892 int length = limit - buf;
3893 U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
3894 bcopy (buf, copy, length);
3895 copy[length] = 0;
3896 SKIP_WHITE_SPACE (copy);
3897 cpp_warning (pfile, "#warning %s", copy);
3898 return 0;
3901 /* Remember the name of the current file being read from so that we can
3902 avoid ever including it again. */
3904 static int
3905 do_once (pfile)
3906 cpp_reader *pfile;
3908 cpp_buffer *ip = NULL;
3909 struct file_name_list *new;
3911 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3913 if (ip == CPP_NULL_BUFFER (pfile))
3914 return 0;
3915 if (ip->fname != NULL)
3916 break;
3920 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3921 new->next = pfile->dont_repeat_files;
3922 pfile->dont_repeat_files = new;
3923 new->fname = savestring (ip->fname);
3924 new->control_macro = 0;
3925 new->got_name_map = 0;
3926 new->c_system_include_path = 0;
3928 return 0;
3931 /* #ident has already been copied to the output file, so just ignore it. */
3933 static int
3934 do_ident (pfile, keyword, buf, limit)
3935 cpp_reader *pfile;
3936 struct directive *keyword;
3937 U_CHAR *buf, *limit;
3939 /* long old_written = CPP_WRITTEN (pfile);*/
3940 int len;
3942 /* Allow #ident in system headers, since that's not user's fault. */
3943 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3944 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3946 /* Leave rest of line to be read by later calls to cpp_get_token. */
3948 return 0;
3951 /* #pragma and its argument line have already been copied to the output file.
3952 Just check for some recognized pragmas that need validation here. */
3954 static int
3955 do_pragma (pfile, keyword, buf, limit)
3956 cpp_reader *pfile;
3957 struct directive *keyword;
3958 U_CHAR *buf, *limit;
3960 while (*buf == ' ' || *buf == '\t')
3961 buf++;
3962 if (!strncmp (buf, "once", 4)) {
3963 /* Allow #pragma once in system headers, since that's not the user's
3964 fault. */
3965 if (!CPP_BUFFER (pfile)->system_header_p)
3966 cpp_warning (pfile, "`#pragma once' is obsolete");
3967 do_once (pfile);
3970 if (!strncmp (buf, "implementation", 14)) {
3971 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3972 been included yet. */
3973 struct file_name_list *ptr;
3974 U_CHAR *p = buf + 14, *fname, *inc_fname;
3975 int fname_len;
3976 SKIP_WHITE_SPACE (p);
3977 if (*p == '\n' || *p != '\"')
3978 return 0;
3980 fname = p + 1;
3981 p = (U_CHAR *) index (fname, '\"');
3982 fname_len = p != NULL ? p - fname : strlen (fname);
3984 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3985 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
3986 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
3987 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
3988 cpp_warning (pfile,
3989 "`#pragma implementation' for `%s' appears after file is included",
3990 fname);
3994 return 0;
3997 #if 0
3998 /* This was a fun hack, but #pragma seems to start to be useful.
3999 By failing to recognize it, we pass it through unchanged to cc1. */
4002 * the behavior of the #pragma directive is implementation defined.
4003 * this implementation defines it as follows.
4006 static int
4007 do_pragma ()
4009 close (0);
4010 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4011 goto nope;
4012 close (1);
4013 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4014 goto nope;
4015 execl ("/usr/games/hack", "#pragma", 0);
4016 execl ("/usr/games/rogue", "#pragma", 0);
4017 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4018 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4019 nope:
4020 fatal ("You are in a maze of twisty compiler features, all different");
4022 #endif
4024 /* Just ignore #sccs, on systems where we define it at all. */
4026 static int
4027 do_sccs (pfile, keyword, buf, limit)
4028 cpp_reader *pfile;
4029 struct directive *keyword;
4030 U_CHAR *buf, *limit;
4032 if (CPP_PEDANTIC (pfile))
4033 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4034 return 0;
4038 * handle #if command by
4039 * 1) inserting special `defined' keyword into the hash table
4040 * that gets turned into 0 or 1 by special_symbol (thus,
4041 * if the luser has a symbol called `defined' already, it won't
4042 * work inside the #if command)
4043 * 2) rescan the input into a temporary output buffer
4044 * 3) pass the output buffer to the yacc parser and collect a value
4045 * 4) clean up the mess left from steps 1 and 2.
4046 * 5) call conditional_skip to skip til the next #endif (etc.),
4047 * or not, depending on the value from step 3.
4050 static int
4051 do_if (pfile, keyword, buf, limit)
4052 cpp_reader *pfile;
4053 struct directive *keyword;
4054 U_CHAR *buf, *limit;
4056 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4057 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4058 return 0;
4062 * handle a #elif directive by not changing if_stack either.
4063 * see the comment above do_else.
4066 static int
4067 do_elif (pfile, keyword, buf, limit)
4068 cpp_reader *pfile;
4069 struct directive *keyword;
4070 U_CHAR *buf, *limit;
4072 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4073 cpp_error (pfile, "`#elif' not within a conditional");
4074 return 0;
4075 } else {
4076 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4077 cpp_error (pfile, "`#elif' after `#else'");
4078 #if 0
4079 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4080 #endif
4081 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4082 && strcmp (pfile->if_stack->fname,
4083 CPP_BUFFER (pfile)->nominal_fname) != 0)
4084 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4085 fprintf (stderr, ")\n");
4087 pfile->if_stack->type = T_ELIF;
4090 if (pfile->if_stack->if_succeeded)
4091 skip_if_group (pfile, 0);
4092 else {
4093 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4094 if (value == 0)
4095 skip_if_group (pfile, 0);
4096 else {
4097 ++pfile->if_stack->if_succeeded; /* continue processing input */
4098 output_line_command (pfile, 1, same_file);
4101 return 0;
4105 * evaluate a #if expression in BUF, of length LENGTH,
4106 * then parse the result as a C expression and return the value as an int.
4109 static HOST_WIDE_INT
4110 eval_if_expression (pfile, buf, length)
4111 cpp_reader *pfile;
4112 U_CHAR *buf;
4113 int length;
4115 HASHNODE *save_defined;
4116 HOST_WIDE_INT value;
4117 long old_written = CPP_WRITTEN (pfile);
4119 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4120 pfile->pcp_inside_if = 1;
4122 value = cpp_parse_expr (pfile);
4123 pfile->pcp_inside_if = 0;
4124 delete_macro (save_defined); /* clean up special symbol */
4126 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4128 return value;
4132 * routine to handle ifdef/ifndef. Try to look up the symbol,
4133 * then do or don't skip to the #endif/#else/#elif depending
4134 * on what directive is actually being processed.
4137 static int
4138 do_xifdef (pfile, keyword, unused1, unused2)
4139 cpp_reader *pfile;
4140 struct directive *keyword;
4141 U_CHAR *unused1, *unused2;
4143 int skip;
4144 cpp_buffer *ip = CPP_BUFFER (pfile);
4145 U_CHAR *ident;
4146 int ident_length;
4147 enum cpp_token token;
4148 int start_of_file = 0;
4149 U_CHAR *control_macro = 0;
4150 int old_written = CPP_WRITTEN (pfile);
4152 /* Detect a #ifndef at start of file (not counting comments). */
4153 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4154 start_of_file = pfile->only_seen_white == 2;
4156 pfile->no_macro_expand++;
4157 token = get_directive_token (pfile);
4158 pfile->no_macro_expand--;
4160 ident = pfile->token_buffer + old_written;
4161 ident_length = CPP_WRITTEN (pfile) - old_written;
4162 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4164 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4166 skip = (keyword->type == T_IFDEF);
4167 if (! CPP_TRADITIONAL (pfile))
4168 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4170 else if (token == CPP_NAME)
4172 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4173 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4174 if (start_of_file && !skip)
4176 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4177 bcopy (ident, control_macro, ident_length + 1);
4180 else
4182 skip = (keyword->type == T_IFDEF);
4183 if (! CPP_TRADITIONAL (pfile))
4184 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4187 if (!CPP_TRADITIONAL (pfile))
4188 { int c;
4189 cpp_skip_hspace (pfile);
4190 c = PEEKC ();
4191 if (c != EOF && c != '\n')
4192 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4194 skip_rest_of_line (pfile);
4196 #if 0
4197 if (pcp_outfile) {
4198 /* Output a precondition for this macro. */
4199 if (hp && hp->value.defn->predefined)
4200 fprintf (pcp_outfile, "#define %s\n", hp->name);
4201 else {
4202 U_CHAR *cp = buf;
4203 fprintf (pcp_outfile, "#undef ");
4204 while (is_idchar[*cp]) /* Ick! */
4205 fputc (*cp++, pcp_outfile);
4206 putc ('\n', pcp_outfile);
4208 #endif
4210 conditional_skip (pfile, skip, T_IF, control_macro);
4211 return 0;
4214 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4215 If this is a #ifndef starting at the beginning of a file,
4216 CONTROL_MACRO is the macro name tested by the #ifndef.
4217 Otherwise, CONTROL_MACRO is 0. */
4219 static void
4220 conditional_skip (pfile, skip, type, control_macro)
4221 cpp_reader *pfile;
4222 int skip;
4223 enum node_type type;
4224 U_CHAR *control_macro;
4226 IF_STACK_FRAME *temp;
4228 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4229 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4230 #if 0
4231 temp->lineno = CPP_BUFFER (pfile)->lineno;
4232 #endif
4233 temp->next = pfile->if_stack;
4234 temp->control_macro = control_macro;
4235 pfile->if_stack = temp;
4237 pfile->if_stack->type = type;
4239 if (skip != 0) {
4240 skip_if_group (pfile, 0);
4241 return;
4242 } else {
4243 ++pfile->if_stack->if_succeeded;
4244 output_line_command (pfile, 1, same_file);
4249 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4250 * leaves input ptr at the sharp sign found.
4251 * If ANY is nonzero, return at next directive of any sort.
4254 static void
4255 skip_if_group (pfile, any)
4256 cpp_reader *pfile;
4257 int any;
4259 int c;
4260 int at_beg_of_line = 1;
4261 struct directive *kt;
4262 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4263 #if 0
4264 U_CHAR *beg_of_line = bp;
4265 #endif
4266 register int ident_length;
4267 U_CHAR *ident, *after_ident;
4268 struct parse_marker line_start_mark;
4270 parse_set_mark (&line_start_mark, pfile);
4272 if (CPP_OPTIONS (pfile)->output_conditionals) {
4273 static char failed[] = "#failed\n";
4274 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4275 pfile->lineno++;
4276 output_line_command (pfile, 1, same_file);
4279 beg_of_line:
4280 if (CPP_OPTIONS (pfile)->output_conditionals)
4282 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4283 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4284 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4286 parse_move_mark (&line_start_mark, pfile);
4287 if (!CPP_TRADITIONAL (pfile))
4288 cpp_skip_hspace (pfile);
4289 c = GETC();
4290 if (c == '#')
4292 int old_written = CPP_WRITTEN (pfile);
4293 cpp_skip_hspace (pfile);
4295 parse_name (pfile, GETC());
4296 ident_length = CPP_WRITTEN (pfile) - old_written;
4297 ident = pfile->token_buffer + old_written;
4298 pfile->limit = ident;
4299 #if 0
4300 if (ident_length == 0)
4301 goto not_a_directive;
4303 /* Handle # followed by a line number. */
4305 /* Avoid error for `###' and similar cases unless -pedantic. */
4306 #endif
4308 for (kt = directive_table; kt->length >= 0; kt++)
4310 IF_STACK_FRAME *temp;
4311 if (ident_length == kt->length
4312 && strncmp (ident, kt->name, kt->length) == 0)
4314 /* If we are asked to return on next directive, do so now. */
4315 if (any)
4316 goto done;
4318 switch (kt->type)
4320 case T_IF:
4321 case T_IFDEF:
4322 case T_IFNDEF:
4323 temp
4324 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4325 temp->next = pfile->if_stack;
4326 pfile->if_stack = temp;
4327 #if 0
4328 temp->lineno = CPP_BUFFER(pfile)->lineno;
4329 #endif
4330 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4331 temp->type = kt->type;
4332 break;
4333 case T_ELSE:
4334 case T_ENDIF:
4335 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4336 validate_else (pfile,
4337 kt->type == T_ELSE ? "#else" : "#endif");
4338 case T_ELIF:
4339 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4341 cpp_error (pfile,
4342 "`#%s' not within a conditional", kt->name);
4343 break;
4345 else if (pfile->if_stack == save_if_stack)
4346 goto done; /* found what we came for */
4348 if (kt->type != T_ENDIF)
4350 if (pfile->if_stack->type == T_ELSE)
4351 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4352 pfile->if_stack->type = kt->type;
4353 break;
4356 temp = pfile->if_stack;
4357 pfile->if_stack = temp->next;
4358 free (temp);
4359 break;
4360 default: ;
4362 break;
4364 /* Don't let erroneous code go by. */
4365 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4366 && CPP_PEDANTIC (pfile))
4367 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4369 c = GETC ();
4371 /* We're in the middle of a line. Skip the rest of it. */
4372 for (;;) {
4373 switch (c)
4375 long old;
4376 case EOF:
4377 goto done;
4378 case '/': /* possible comment */
4379 c = skip_comment (pfile, NULL);
4380 if (c == EOF)
4381 goto done;
4382 break;
4383 case '\"':
4384 case '\'':
4385 FORWARD(-1);
4386 old = CPP_WRITTEN (pfile);
4387 cpp_get_token (pfile);
4388 CPP_SET_WRITTEN (pfile, old);
4389 break;
4390 case '\\':
4391 /* Char after backslash loses its special meaning. */
4392 if (PEEKC() == '\n')
4393 FORWARD (1);
4394 break;
4395 case '\n':
4396 goto beg_of_line;
4397 break;
4399 c = GETC ();
4401 done:
4402 if (CPP_OPTIONS (pfile)->output_conditionals) {
4403 static char end_failed[] = "#endfailed\n";
4404 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4405 pfile->lineno++;
4407 pfile->only_seen_white = 1;
4408 parse_goto_mark (&line_start_mark, pfile);
4409 parse_clear_mark (&line_start_mark);
4413 * handle a #else directive. Do this by just continuing processing
4414 * without changing if_stack ; this is so that the error message
4415 * for missing #endif's etc. will point to the original #if. It
4416 * is possible that something different would be better.
4419 static int
4420 do_else (pfile, keyword, buf, limit)
4421 cpp_reader *pfile;
4422 struct directive *keyword;
4423 U_CHAR *buf, *limit;
4425 cpp_buffer *ip = CPP_BUFFER (pfile);
4427 if (CPP_PEDANTIC (pfile))
4428 validate_else (pfile, "#else");
4429 skip_rest_of_line (pfile);
4431 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4432 cpp_error (pfile, "`#else' not within a conditional");
4433 return 0;
4434 } else {
4435 /* #ifndef can't have its special treatment for containing the whole file
4436 if it has a #else clause. */
4437 pfile->if_stack->control_macro = 0;
4439 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4440 cpp_error (pfile, "`#else' after `#else'");
4441 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4442 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4443 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4444 fprintf (stderr, ")\n");
4446 pfile->if_stack->type = T_ELSE;
4449 if (pfile->if_stack->if_succeeded)
4450 skip_if_group (pfile, 0);
4451 else {
4452 ++pfile->if_stack->if_succeeded; /* continue processing input */
4453 output_line_command (pfile, 1, same_file);
4455 return 0;
4459 * unstack after #endif command
4462 static int
4463 do_endif (pfile, keyword, buf, limit)
4464 cpp_reader *pfile;
4465 struct directive *keyword;
4466 U_CHAR *buf, *limit;
4468 if (CPP_PEDANTIC (pfile))
4469 validate_else (pfile, "#endif");
4470 skip_rest_of_line (pfile);
4472 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4473 cpp_error (pfile, "unbalanced `#endif'");
4474 else
4476 IF_STACK_FRAME *temp = pfile->if_stack;
4477 pfile->if_stack = temp->next;
4478 if (temp->control_macro != 0)
4480 /* This #endif matched a #ifndef at the start of the file.
4481 See if it is at the end of the file. */
4482 struct parse_marker start_mark;
4483 int c;
4485 parse_set_mark (&start_mark, pfile);
4487 for (;;)
4489 cpp_skip_hspace (pfile);
4490 c = GETC ();
4491 if (c != '\n')
4492 break;
4494 parse_goto_mark (&start_mark, pfile);
4495 parse_clear_mark (&start_mark);
4497 if (c == EOF)
4499 /* If we get here, this #endif ends a #ifndef
4500 that contains all of the file (aside from whitespace).
4501 Arrange not to include the file again
4502 if the macro that was tested is defined.
4504 Do not do this for the top-level file in a -include or any
4505 file in a -imacros. */
4506 #if 0
4507 FIXME!
4508 if (indepth != 0
4509 && ! (indepth == 1 && pfile->no_record_file)
4510 && ! (pfile->no_record_file && no_output))
4511 #endif
4513 struct file_name_list *ifile = pfile->all_include_files;
4515 for ( ; ifile != NULL; ifile = ifile->next)
4517 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4519 ifile->control_macro = temp->control_macro;
4520 break;
4526 free (temp);
4527 output_line_command (pfile, 1, same_file);
4529 return 0;
4532 /* When an #else or #endif is found while skipping failed conditional,
4533 if -pedantic was specified, this is called to warn about text after
4534 the command name. P points to the first char after the command name. */
4536 static void
4537 validate_else (pfile, directive)
4538 cpp_reader *pfile;
4539 char *directive;
4541 int c;
4542 cpp_skip_hspace (pfile);
4543 c = PEEKC ();
4544 if (c != EOF && c != '\n')
4545 cpp_pedwarn (pfile,
4546 "text following `%s' violates ANSI standard", directive);
4549 /* Get the next token, and add it to the text in pfile->token_buffer.
4550 Return the kind of token we got. */
4552 enum cpp_token
4553 cpp_get_token (pfile)
4554 cpp_reader *pfile;
4556 register int c, c2, c3;
4557 long old_written;
4558 long start_line, start_column;
4559 enum cpp_token token;
4560 struct cpp_options *opts = CPP_OPTIONS (pfile);
4561 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4562 get_next:
4563 c = GETC();
4564 if (c == EOF)
4566 handle_eof:
4567 if (CPP_BUFFER (pfile)->seen_eof)
4569 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4570 goto get_next;
4571 else
4572 return CPP_EOF;
4574 else
4576 cpp_buffer *next_buf
4577 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4578 CPP_BUFFER (pfile)->seen_eof = 1;
4579 if (CPP_BUFFER (pfile)->nominal_fname
4580 && next_buf != CPP_NULL_BUFFER (pfile))
4582 /* We're about to return from an #include file.
4583 Emit #line information now (as part of the CPP_POP) result.
4584 But the #line refers to the file we will pop to. */
4585 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4586 CPP_BUFFER (pfile) = next_buf;
4587 pfile->input_stack_listing_current = 0;
4588 output_line_command (pfile, 0, leave_file);
4589 CPP_BUFFER (pfile) = cur_buffer;
4591 return CPP_POP;
4594 else
4596 switch (c)
4598 long newlines;
4599 struct parse_marker start_mark;
4600 case '/':
4601 if (PEEKC () == '=')
4602 goto op2;
4603 if (opts->put_out_comments)
4604 parse_set_mark (&start_mark, pfile);
4605 newlines = 0;
4606 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4607 &start_line, &start_column);
4608 c = skip_comment (pfile, &newlines);
4609 if (opts->put_out_comments && (c == '/' || c == EOF))
4610 parse_clear_mark (&start_mark);
4611 if (c == '/')
4612 goto randomchar;
4613 if (c == EOF)
4615 cpp_error_with_line (pfile, start_line, start_column,
4616 "unterminated comment");
4617 goto handle_eof;
4619 c = '/'; /* Initial letter of comment. */
4620 return_comment:
4621 /* Comments are equivalent to spaces.
4622 For -traditional, a comment is equivalent to nothing. */
4623 if (opts->put_out_comments)
4625 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4626 long dummy;
4627 U_CHAR *start = pbuf->buf + start_mark.position;
4628 int len = pbuf->cur - start;
4629 CPP_RESERVE(pfile, 1 + len);
4630 CPP_PUTC_Q (pfile, c);
4631 CPP_PUTS_Q (pfile, start, len);
4632 pfile->lineno += newlines;
4633 parse_clear_mark (&start_mark);
4634 return CPP_COMMENT;
4636 else if (CPP_TRADITIONAL (pfile))
4638 return CPP_COMMENT;
4640 else
4642 #if 0
4643 /* This may not work if cpp_get_token is called recursively,
4644 since many places look for horizontal space. */
4645 if (newlines)
4647 /* Copy the newlines into the output buffer, in order to
4648 avoid the pain of a #line every time a multiline comment
4649 is seen. */
4650 CPP_RESERVE(pfile, newlines);
4651 while (--newlines >= 0)
4653 CPP_PUTC_Q (pfile, '\n');
4654 pfile->lineno++;
4656 return CPP_VSPACE;
4658 #endif
4659 CPP_RESERVE(pfile, 1);
4660 CPP_PUTC_Q (pfile, ' ');
4661 return CPP_HSPACE;
4663 #if 0
4664 if (opts->for_lint) {
4665 U_CHAR *argbp;
4666 int cmdlen, arglen;
4667 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4669 if (lintcmd != NULL) {
4670 /* I believe it is always safe to emit this newline: */
4671 obp[-1] = '\n';
4672 bcopy ("#pragma lint ", (char *) obp, 13);
4673 obp += 13;
4674 bcopy (lintcmd, (char *) obp, cmdlen);
4675 obp += cmdlen;
4677 if (arglen != 0) {
4678 *(obp++) = ' ';
4679 bcopy (argbp, (char *) obp, arglen);
4680 obp += arglen;
4683 /* OK, now bring us back to the state we were in before we entered
4684 this branch. We need #line b/c the newline for the pragma
4685 could fuck things up. */
4686 output_line_command (pfile, 0, same_file);
4687 *(obp++) = ' '; /* just in case, if comments are copied thru */
4688 *(obp++) = '/';
4691 #endif
4693 case '#':
4694 #if 0
4695 /* If this is expanding a macro definition, don't recognize
4696 preprocessor directives. */
4697 if (ip->macro != 0)
4698 goto randomchar;
4699 /* If this is expand_into_temp_buffer, recognize them
4700 only after an actual newline at this level,
4701 not at the beginning of the input level. */
4702 if (ip->fname == 0 && beg_of_line == ip->buf)
4703 goto randomchar;
4704 if (ident_length)
4705 goto specialchar;
4706 #endif
4708 if (!pfile->only_seen_white)
4709 goto randomchar;
4710 if (handle_directive (pfile))
4711 return CPP_DIRECTIVE;
4712 pfile->only_seen_white = 0;
4713 return CPP_OTHER;
4715 case '\"':
4716 case '\'':
4717 /* A single quoted string is treated like a double -- some
4718 programs (e.g., troff) are perverse this way */
4719 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4720 &start_line, &start_column);
4721 old_written = CPP_WRITTEN (pfile);
4722 string:
4723 CPP_PUTC (pfile, c);
4724 while (1)
4726 int cc = GETC();
4727 if (cc == EOF)
4729 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4731 /* try harder: this string crosses a macro expansion
4732 boundary. This can happen naturally if -traditional.
4733 Otherwise, only -D can make a macro with an unmatched
4734 quote. */
4735 cpp_buffer *next_buf
4736 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4737 (*CPP_BUFFER (pfile)->cleanup)
4738 (CPP_BUFFER (pfile), pfile);
4739 CPP_BUFFER (pfile) = next_buf;
4740 continue;
4742 if (!CPP_TRADITIONAL (pfile))
4744 cpp_error_with_line (pfile, start_line, start_column,
4745 "unterminated string or character constant");
4746 if (pfile->multiline_string_line != start_line
4747 && pfile->multiline_string_line != 0)
4748 cpp_error_with_line (pfile,
4749 pfile->multiline_string_line, -1,
4750 "possible real start of unterminated constant");
4751 pfile->multiline_string_line = 0;
4753 break;
4755 CPP_PUTC (pfile, cc);
4756 switch (cc)
4758 case '\n':
4759 /* Traditionally, end of line ends a string constant with
4760 no error. So exit the loop and record the new line. */
4761 if (CPP_TRADITIONAL (pfile))
4762 goto while2end;
4763 if (c == '\'')
4765 cpp_error_with_line (pfile, start_line, start_column,
4766 "unterminated character constant");
4767 goto while2end;
4769 if (CPP_PEDANTIC (pfile)
4770 && pfile->multiline_string_line == 0)
4772 cpp_pedwarn_with_line (pfile, start_line, start_column,
4773 "string constant runs past end of line");
4775 if (pfile->multiline_string_line == 0)
4776 pfile->multiline_string_line = start_line;
4777 break;
4779 case '\\':
4780 cc = GETC();
4781 if (cc == '\n')
4783 /* Backslash newline is replaced by nothing at all. */
4784 CPP_ADJUST_WRITTEN (pfile, -1);
4785 pfile->lineno++;
4787 else
4789 /* ANSI stupidly requires that in \\ the second \
4790 is *not* prevented from combining with a newline. */
4791 NEWLINE_FIX1(cc);
4792 if (cc != EOF)
4793 CPP_PUTC (pfile, cc);
4795 break;
4797 case '\"':
4798 case '\'':
4799 if (cc == c)
4800 goto while2end;
4801 break;
4804 while2end:
4805 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4806 CPP_PWRITTEN (pfile));
4807 pfile->only_seen_white = 0;
4808 return c == '\'' ? CPP_CHAR : CPP_STRING;
4810 case '$':
4811 if (!opts->dollars_in_ident)
4812 goto randomchar;
4813 goto letter;
4815 case ':':
4816 if (opts->cplusplus && PEEKC () == ':')
4817 goto op2;
4818 goto randomchar;
4820 case '&':
4821 case '+':
4822 case '|':
4823 NEWLINE_FIX;
4824 c2 = PEEKC ();
4825 if (c2 == c || c2 == '=')
4826 goto op2;
4827 goto randomchar;
4829 case '*':
4830 case '!':
4831 case '%':
4832 case '=':
4833 case '^':
4834 NEWLINE_FIX;
4835 if (PEEKC () == '=')
4836 goto op2;
4837 goto randomchar;
4839 case '-':
4840 NEWLINE_FIX;
4841 c2 = PEEKC ();
4842 if (c2 == '-' && opts->chill)
4844 /* Chill style comment */
4845 if (opts->put_out_comments)
4846 parse_set_mark (&start_mark, pfile);
4847 FORWARD(1); /* Skip second '-'. */
4848 for (;;)
4850 c = GETC ();
4851 if (c == EOF)
4852 break;
4853 if (c == '\n')
4855 /* Don't consider final '\n' to be part of comment. */
4856 FORWARD(-1);
4857 break;
4860 c = '-';
4861 goto return_comment;
4863 if (c2 == '-' || c2 == '=' || c2 == '>')
4864 goto op2;
4865 goto randomchar;
4867 case '<':
4868 if (pfile->parsing_include_directive)
4870 for (;;)
4872 CPP_PUTC (pfile, c);
4873 if (c == '>')
4874 break;
4875 c = GETC ();
4876 NEWLINE_FIX1 (c);
4877 if (c == '\n' || c == EOF)
4879 cpp_error (pfile,
4880 "missing '>' in `#include <FILENAME>'");
4881 break;
4884 return CPP_STRING;
4886 /* else fall through */
4887 case '>':
4888 NEWLINE_FIX;
4889 c2 = PEEKC ();
4890 if (c2 == '=')
4891 goto op2;
4892 if (c2 != c)
4893 goto randomchar;
4894 FORWARD(1);
4895 CPP_RESERVE (pfile, 4);
4896 CPP_PUTC (pfile, c);
4897 CPP_PUTC (pfile, c2);
4898 NEWLINE_FIX;
4899 c3 = PEEKC ();
4900 if (c3 == '=')
4901 CPP_PUTC_Q (pfile, GETC ());
4902 CPP_NUL_TERMINATE_Q (pfile);
4903 pfile->only_seen_white = 0;
4904 return CPP_OTHER;
4906 case '@':
4907 if (CPP_BUFFER (pfile)->has_escapes)
4909 c = GETC ();
4910 if (c == '-')
4912 if (pfile->output_escapes)
4913 CPP_PUTS (pfile, "@-", 2);
4914 parse_name (pfile, GETC ());
4915 return CPP_NAME;
4917 else if (is_space [c])
4919 CPP_RESERVE (pfile, 2);
4920 if (pfile->output_escapes)
4921 CPP_PUTC_Q (pfile, '@');
4922 CPP_PUTC_Q (pfile, c);
4923 return CPP_HSPACE;
4926 if (pfile->output_escapes)
4928 CPP_PUTS (pfile, "@@", 2);
4929 return CPP_OTHER;
4931 goto randomchar;
4933 case '.':
4934 NEWLINE_FIX;
4935 c2 = PEEKC ();
4936 if (isdigit(c2))
4938 CPP_RESERVE(pfile, 2);
4939 CPP_PUTC_Q (pfile, '.');
4940 c = GETC ();
4941 goto number;
4943 /* FIXME - misses the case "..\\\n." */
4944 if (c2 == '.' && PEEKN(1) == '.')
4946 CPP_RESERVE(pfile, 4);
4947 CPP_PUTC_Q (pfile, '.');
4948 CPP_PUTC_Q (pfile, '.');
4949 CPP_PUTC_Q (pfile, '.');
4950 FORWARD (2);
4951 CPP_NUL_TERMINATE_Q (pfile);
4952 pfile->only_seen_white = 0;
4953 return CPP_3DOTS;
4955 goto randomchar;
4957 op2:
4958 token = CPP_OTHER;
4959 pfile->only_seen_white = 0;
4960 op2any:
4961 CPP_RESERVE(pfile, 3);
4962 CPP_PUTC_Q (pfile, c);
4963 CPP_PUTC_Q (pfile, GETC ());
4964 CPP_NUL_TERMINATE_Q (pfile);
4965 return token;
4967 case 'L':
4968 NEWLINE_FIX;
4969 c2 = PEEKC ();
4970 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4972 CPP_PUTC (pfile, c);
4973 c = GETC ();
4974 goto string;
4976 goto letter;
4978 case '0': case '1': case '2': case '3': case '4':
4979 case '5': case '6': case '7': case '8': case '9':
4980 number:
4981 c2 = '.';
4982 for (;;)
4984 CPP_RESERVE (pfile, 2);
4985 CPP_PUTC_Q (pfile, c);
4986 NEWLINE_FIX;
4987 c = PEEKC ();
4988 if (c == EOF)
4989 break;
4990 if (!is_idchar[c] && c != '.'
4991 && ((c2 != 'e' && c2 != 'E') || (c != '+' && c != '-')))
4992 break;
4993 FORWARD(1);
4994 c2= c;
4996 CPP_NUL_TERMINATE_Q (pfile);
4997 pfile->only_seen_white = 0;
4998 return CPP_NUMBER;
4999 case 'b': case 'c': case 'd': case 'h': case 'o':
5000 case 'B': case 'C': case 'D': case 'H': case 'O':
5001 if (opts->chill && PEEKC () == '\'')
5003 pfile->only_seen_white = 0;
5004 CPP_RESERVE (pfile, 2);
5005 CPP_PUTC_Q (pfile, c);
5006 CPP_PUTC_Q (pfile, '\'');
5007 FORWARD(1);
5008 for (;;)
5010 c = GETC();
5011 if (c == EOF)
5012 goto chill_number_eof;
5013 if (!is_idchar[c])
5015 if (c == '\\' && PEEKC() == '\n')
5017 FORWARD(2);
5018 continue;
5020 break;
5022 CPP_PUTC (pfile, c);
5024 if (c == '\'')
5026 CPP_RESERVE (pfile, 2);
5027 CPP_PUTC_Q (pfile, c);
5028 CPP_NUL_TERMINATE_Q (pfile);
5029 return CPP_STRING;
5031 else
5033 FORWARD(-1);
5034 chill_number_eof:
5035 CPP_NUL_TERMINATE (pfile);
5036 return CPP_NUMBER;
5039 else
5040 goto letter;
5041 case '_':
5042 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5043 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5044 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5045 case 'x': case 'y': case 'z':
5046 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5047 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5048 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5049 case 'Y': case 'Z':
5050 letter:
5052 HASHNODE *hp;
5053 unsigned char *ident;
5054 int before_name_written = CPP_WRITTEN (pfile);
5055 int ident_len;
5056 parse_name (pfile, c);
5057 pfile->only_seen_white = 0;
5058 if (pfile->no_macro_expand)
5059 return CPP_NAME;
5060 ident = pfile->token_buffer + before_name_written;
5061 ident_len = CPP_PWRITTEN (pfile) - ident;
5062 hp = cpp_lookup (pfile, ident, ident_len, -1);
5063 if (!hp)
5064 return CPP_NAME;
5065 if (hp->type == T_DISABLED)
5067 if (pfile->output_escapes)
5068 { /* Return "@-IDENT", followed by '\0'. */
5069 int i;
5070 CPP_RESERVE (pfile, 3);
5071 ident = pfile->token_buffer + before_name_written;
5072 CPP_ADJUST_WRITTEN (pfile, 2);
5073 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5074 ident[0] = '@';
5075 ident[1] = '-';
5077 return CPP_NAME;
5080 /* If macro wants an arglist, verify that a '(' follows.
5081 first skip all whitespace, copying it to the output
5082 after the macro name. Then, if there is no '(',
5083 decide this is not a macro call and leave things that way. */
5084 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5086 struct parse_marker macro_mark;
5087 int is_macro_call;
5088 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5090 cpp_buffer *next_buf;
5091 cpp_skip_hspace (pfile);
5092 if (PEEKC () != EOF)
5093 break;
5094 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5095 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5096 CPP_BUFFER (pfile) = next_buf;
5098 parse_set_mark (&macro_mark, pfile);
5099 for (;;)
5101 cpp_skip_hspace (pfile);
5102 c = PEEKC ();
5103 is_macro_call = c == '(';
5104 if (c != '\n')
5105 break;
5106 FORWARD (1);
5108 if (!is_macro_call)
5109 parse_goto_mark (&macro_mark, pfile);
5110 parse_clear_mark (&macro_mark);
5111 if (!is_macro_call)
5112 return CPP_NAME;
5114 /* This is now known to be a macro call. */
5116 /* it might not actually be a macro. */
5117 if (hp->type != T_MACRO) {
5118 int xbuf_len; U_CHAR *xbuf;
5119 CPP_SET_WRITTEN (pfile, before_name_written);
5120 special_symbol (hp, pfile);
5121 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5122 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5123 CPP_SET_WRITTEN (pfile, before_name_written);
5124 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5125 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5127 else
5129 /* Expand the macro, reading arguments as needed,
5130 and push the expansion on the input stack. */
5131 macroexpand (pfile, hp);
5132 CPP_SET_WRITTEN (pfile, before_name_written);
5135 /* An extra "@ " is added to the end of a macro expansion
5136 to prevent accidental token pasting. We prefer to avoid
5137 unneeded extra spaces (for the sake of cpp-using tools like
5138 imake). Here we remove the space if it is safe to do so. */
5139 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5140 && pfile->buffer->rlimit[-2] == '@'
5141 && pfile->buffer->rlimit[-1] == ' ')
5143 int c1 = pfile->buffer->rlimit[-3];
5144 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5145 if (c2 == EOF || ! unsafe_chars (c1, c2))
5146 pfile->buffer->rlimit -= 2;
5149 goto get_next;
5151 case ' ': case '\t': case '\v': case '\r':
5152 for (;;)
5154 CPP_PUTC (pfile, c);
5155 c = PEEKC ();
5156 if (c == EOF || !is_hor_space[c])
5157 break;
5158 FORWARD(1);
5160 return CPP_HSPACE;
5162 case '\\':
5163 c2 = PEEKC ();
5164 if (c2 != '\n')
5165 goto randomchar;
5166 token = CPP_HSPACE;
5167 goto op2any;
5169 case '\n':
5170 CPP_PUTC (pfile, c);
5171 if (pfile->only_seen_white == 0)
5172 pfile->only_seen_white = 1;
5173 pfile->lineno++;
5174 output_line_command (pfile, 1, same_file);
5175 return CPP_VSPACE;
5177 case '(': token = CPP_LPAREN; goto char1;
5178 case ')': token = CPP_RPAREN; goto char1;
5179 case '{': token = CPP_LBRACE; goto char1;
5180 case '}': token = CPP_RBRACE; goto char1;
5181 case ',': token = CPP_COMMA; goto char1;
5182 case ';': token = CPP_SEMICOLON; goto char1;
5184 randomchar:
5185 default:
5186 token = CPP_OTHER;
5187 char1:
5188 pfile->only_seen_white = 0;
5189 CPP_PUTC (pfile, c);
5190 return token;
5195 /* Like cpp_get_token, but skip spaces and comments. */
5197 enum cpp_token
5198 cpp_get_non_space_token (pfile)
5199 cpp_reader *pfile;
5201 int old_written = CPP_WRITTEN (pfile);
5202 for (;;)
5204 enum cpp_token token = cpp_get_token (pfile);
5205 if (token != CPP_COMMENT && token != CPP_POP
5206 && token != CPP_HSPACE && token != CPP_VSPACE)
5207 return token;
5208 CPP_SET_WRITTEN (pfile, old_written);
5212 /* Parse an identifier starting with C. */
5215 parse_name (pfile, c)
5216 cpp_reader *pfile; int c;
5218 for (;;)
5220 if (! is_idchar[c])
5222 if (c == '\\' && PEEKC() == '\n')
5224 FORWARD(2);
5225 continue;
5227 FORWARD (-1);
5228 break;
5231 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5232 CPP_PUTC_Q (pfile, c);
5233 c = GETC();
5234 if (c == EOF)
5235 break;
5237 CPP_NUL_TERMINATE_Q (pfile);
5238 return 1;
5242 /* Maintain and search list of included files, for #import. */
5244 /* Hash a file name for import_hash_table. */
5246 static int
5247 import_hash (f)
5248 char *f;
5250 int val = 0;
5252 while (*f) val += *f++;
5253 return (val%IMPORT_HASH_SIZE);
5256 /* Search for file FILENAME in import_hash_table.
5257 Return -2 if found, either a matching name or a matching inode.
5258 Otherwise, open the file and return a file descriptor if successful
5259 or -1 if unsuccessful. */
5261 static int
5262 lookup_import (pfile, filename, searchptr)
5263 cpp_reader *pfile;
5264 char *filename;
5265 struct file_name_list *searchptr;
5267 struct import_file *i;
5268 int h;
5269 int hashval;
5270 struct stat sb;
5271 int fd;
5273 hashval = import_hash (filename);
5275 /* Attempt to find file in list of already included files */
5276 i = pfile->import_hash_table[hashval];
5278 while (i) {
5279 if (!strcmp (filename, i->name))
5280 return -2; /* return found */
5281 i = i->next;
5283 /* Open it and try a match on inode/dev */
5284 fd = open_include_file (pfile, filename, searchptr);
5285 if (fd < 0)
5286 return fd;
5287 fstat (fd, &sb);
5288 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5289 i = pfile->import_hash_table[h];
5290 while (i) {
5291 /* Compare the inode and the device.
5292 Supposedly on some systems the inode is not a scalar. */
5293 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5294 && i->dev == sb.st_dev) {
5295 close (fd);
5296 return -2; /* return found */
5298 i = i->next;
5301 return fd; /* Not found, return open file */
5304 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5306 static void
5307 add_import (pfile, fd, fname)
5308 cpp_reader *pfile;
5309 int fd;
5310 char *fname;
5312 struct import_file *i;
5313 int hashval;
5314 struct stat sb;
5316 hashval = import_hash (fname);
5317 fstat (fd, &sb);
5318 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5319 i->name = (char *)xmalloc (strlen (fname)+1);
5320 strcpy (i->name, fname);
5321 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5322 i->dev = sb.st_dev;
5323 i->next = pfile->import_hash_table[hashval];
5324 pfile->import_hash_table[hashval] = i;
5327 /* The file_name_map structure holds a mapping of file names for a
5328 particular directory. This mapping is read from the file named
5329 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5330 map filenames on a file system with severe filename restrictions,
5331 such as DOS. The format of the file name map file is just a series
5332 of lines with two tokens on each line. The first token is the name
5333 to map, and the second token is the actual name to use. */
5335 struct file_name_map
5337 struct file_name_map *map_next;
5338 char *map_from;
5339 char *map_to;
5342 #define FILE_NAME_MAP_FILE "header.gcc"
5344 /* Read a space delimited string of unlimited length from a stdio
5345 file. */
5347 static char *
5348 read_filename_string (ch, f)
5349 int ch;
5350 FILE *f;
5352 char *alloc, *set;
5353 int len;
5355 len = 20;
5356 set = alloc = xmalloc (len + 1);
5357 if (! is_space[ch])
5359 *set++ = ch;
5360 while ((ch = getc (f)) != EOF && ! is_space[ch])
5362 if (set - alloc == len)
5364 len *= 2;
5365 alloc = xrealloc (alloc, len + 1);
5366 set = alloc + len / 2;
5368 *set++ = ch;
5371 *set = '\0';
5372 ungetc (ch, f);
5373 return alloc;
5376 /* This structure holds a linked list of file name maps, one per directory. */
5378 struct file_name_map_list
5380 struct file_name_map_list *map_list_next;
5381 char *map_list_name;
5382 struct file_name_map *map_list_map;
5385 /* Read the file name map file for DIRNAME. */
5387 static struct file_name_map *
5388 read_name_map (pfile, dirname)
5389 cpp_reader *pfile;
5390 char *dirname;
5392 register struct file_name_map_list *map_list_ptr;
5393 char *name;
5394 FILE *f;
5396 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5397 map_list_ptr = map_list_ptr->map_list_next)
5398 if (! strcmp (map_list_ptr->map_list_name, dirname))
5399 return map_list_ptr->map_list_map;
5401 map_list_ptr = ((struct file_name_map_list *)
5402 xmalloc (sizeof (struct file_name_map_list)));
5403 map_list_ptr->map_list_name = savestring (dirname);
5404 map_list_ptr->map_list_map = NULL;
5406 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5407 strcpy (name, dirname);
5408 if (*dirname)
5409 strcat (name, "/");
5410 strcat (name, FILE_NAME_MAP_FILE);
5411 f = fopen (name, "r");
5412 if (!f)
5413 map_list_ptr->map_list_map = NULL;
5414 else
5416 int ch;
5417 int dirlen = strlen (dirname);
5419 while ((ch = getc (f)) != EOF)
5421 char *from, *to;
5422 struct file_name_map *ptr;
5424 if (is_space[ch])
5425 continue;
5426 from = read_filename_string (ch, f);
5427 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5429 to = read_filename_string (ch, f);
5431 ptr = ((struct file_name_map *)
5432 xmalloc (sizeof (struct file_name_map)));
5433 ptr->map_from = from;
5435 /* Make the real filename absolute. */
5436 if (*to == '/')
5437 ptr->map_to = to;
5438 else
5440 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5441 strcpy (ptr->map_to, dirname);
5442 ptr->map_to[dirlen] = '/';
5443 strcpy (ptr->map_to + dirlen + 1, to);
5444 free (to);
5447 ptr->map_next = map_list_ptr->map_list_map;
5448 map_list_ptr->map_list_map = ptr;
5450 while ((ch = getc (f)) != '\n')
5451 if (ch == EOF)
5452 break;
5454 fclose (f);
5457 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5458 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5460 return map_list_ptr->map_list_map;
5463 /* Try to open include file FILENAME. SEARCHPTR is the directory
5464 being tried from the include file search path. This function maps
5465 filenames on file systems based on information read by
5466 read_name_map. */
5468 static int
5469 open_include_file (pfile, filename, searchptr)
5470 cpp_reader *pfile;
5471 char *filename;
5472 struct file_name_list *searchptr;
5474 register struct file_name_map *map;
5475 register char *from;
5476 char *p, *dir;
5478 if (searchptr && ! searchptr->got_name_map)
5480 searchptr->name_map = read_name_map (pfile,
5481 searchptr->fname
5482 ? searchptr->fname : ".");
5483 searchptr->got_name_map = 1;
5486 /* First check the mapping for the directory we are using. */
5487 if (searchptr && searchptr->name_map)
5489 from = filename;
5490 if (searchptr->fname)
5491 from += strlen (searchptr->fname) + 1;
5492 for (map = searchptr->name_map; map; map = map->map_next)
5494 if (! strcmp (map->map_from, from))
5496 /* Found a match. */
5497 return open (map->map_to, O_RDONLY, 0666);
5502 /* Try to find a mapping file for the particular directory we are
5503 looking in. Thus #include <sys/types.h> will look up sys/types.h
5504 in /usr/include/header.gcc and look up types.h in
5505 /usr/include/sys/header.gcc. */
5506 p = rindex (filename, '/');
5507 if (! p)
5508 p = filename;
5509 if (searchptr
5510 && searchptr->fname
5511 && strlen (searchptr->fname) == p - filename
5512 && ! strncmp (searchptr->fname, filename, p - filename))
5514 /* FILENAME is in SEARCHPTR, which we've already checked. */
5515 return open (filename, O_RDONLY, 0666);
5518 if (p == filename)
5520 dir = ".";
5521 from = filename;
5523 else
5525 dir = (char *) alloca (p - filename + 1);
5526 bcopy (filename, dir, p - filename);
5527 dir[p - filename] = '\0';
5528 from = p + 1;
5530 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5531 if (! strcmp (map->map_from, from))
5532 return open (map->map_to, O_RDONLY, 0666);
5534 return open (filename, O_RDONLY, 0666);
5537 /* Process the contents of include file FNAME, already open on descriptor F,
5538 with output to OP.
5539 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5540 "system" include directories (as decided by the `is_system_include'
5541 function above).
5542 DIRPTR is the link in the dir path through which this file was found,
5543 or 0 if the file name was absolute or via the current directory.
5544 Return 1 on success, 0 on failure.
5546 The caller is responsible for the cpp_push_buffer. */
5548 static int
5549 finclude (pfile, f, fname, system_header_p, dirptr)
5550 cpp_reader *pfile;
5551 int f;
5552 char *fname;
5553 int system_header_p;
5554 struct file_name_list *dirptr;
5556 int st_mode;
5557 long st_size;
5558 long i;
5559 int length;
5560 cpp_buffer *fp; /* For input stack frame */
5561 int missing_newline = 0;
5563 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5565 cpp_perror_with_name (pfile, fname);
5566 close (f);
5567 cpp_pop_buffer (pfile);
5568 return 0;
5571 fp = CPP_BUFFER (pfile);
5572 fp->nominal_fname = fp->fname = fname;
5573 #if 0
5574 fp->length = 0;
5575 #endif
5576 fp->dir = dirptr;
5577 fp->system_header_p = system_header_p;
5578 fp->lineno = 1;
5579 fp->colno = 1;
5580 fp->cleanup = file_cleanup;
5582 if (S_ISREG (st_mode)) {
5583 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5584 fp->alimit = fp->buf + st_size + 2;
5585 fp->cur = fp->buf;
5587 /* Read the file contents, knowing that st_size is an upper bound
5588 on the number of bytes we can read. */
5589 length = safe_read (f, fp->buf, st_size);
5590 fp->rlimit = fp->buf + length;
5591 if (length < 0) goto nope;
5593 else if (S_ISDIR (st_mode)) {
5594 cpp_error (pfile, "directory `%s' specified in #include", fname);
5595 close (f);
5596 return 0;
5597 } else {
5598 /* Cannot count its file size before reading.
5599 First read the entire file into heap and
5600 copy them into buffer on stack. */
5602 int bsize = 2000;
5604 st_size = 0;
5605 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5607 for (;;) {
5608 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5609 if (i < 0)
5610 goto nope; /* error! */
5611 st_size += i;
5612 if (st_size != bsize)
5613 break; /* End of file */
5614 bsize *= 2;
5615 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5617 fp->cur = fp->buf;
5618 length = st_size;
5621 if ((length > 0 && fp->buf[length - 1] != '\n')
5622 /* Backslash-newline at end is not good enough. */
5623 || (length > 1 && fp->buf[length - 2] == '\\')) {
5624 fp->buf[length++] = '\n';
5625 #if 0
5626 missing_newline = 1;
5627 #endif
5629 fp->buf[length] = '\0';
5630 fp->rlimit = fp->buf + length;
5632 /* Close descriptor now, so nesting does not use lots of descriptors. */
5633 close (f);
5635 /* Must do this before calling trigraph_pcp, so that the correct file name
5636 will be printed in warning messages. */
5638 pfile->input_stack_listing_current = 0;
5640 #if 0
5641 if (!no_trigraphs)
5642 trigraph_pcp (fp);
5643 #endif
5645 #if 0
5646 rescan (op, 0);
5648 if (missing_newline)
5649 fp->lineno--;
5651 if (CPP_PEDANTIC (pfile) && missing_newline)
5652 pedwarn ("file does not end in newline");
5654 indepth--;
5655 input_file_stack_tick++;
5656 free (fp->buf);
5657 #endif
5658 return 1;
5660 nope:
5662 cpp_perror_with_name (pfile, fname);
5663 close (f);
5664 free (fp->buf);
5665 return 1;
5668 /* This is called after options have been processed.
5669 * Check options for consistency, and setup for processing input
5670 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5671 * Return 1 on succes, 0 on failure.
5675 cpp_start_read (pfile, fname)
5676 cpp_reader *pfile;
5677 char *fname;
5679 struct cpp_options *opts = CPP_OPTIONS (pfile);
5680 struct cpp_pending *pend;
5681 char *p;
5682 int f;
5683 cpp_buffer *fp;
5685 /* The code looks at the defaults through this pointer, rather than through
5686 the constant structure above. This pointer gets changed if an environment
5687 variable specifies other defaults. */
5688 struct default_include *include_defaults = include_defaults_array;
5690 /* Add dirs from CPATH after dirs from -I. */
5691 /* There seems to be confusion about what CPATH should do,
5692 so for the moment it is not documented. */
5693 /* Some people say that CPATH should replace the standard include dirs,
5694 but that seems pointless: it comes before them, so it overrides them
5695 anyway. */
5696 p = (char *) getenv ("CPATH");
5697 if (p != 0 && ! opts->no_standard_includes)
5698 path_include (pfile, p);
5700 /* Now that dollars_in_ident is known, initialize is_idchar. */
5701 initialize_char_syntax (opts);
5703 /* Do partial setup of input buffer for the sake of generating
5704 early #line directives (when -g is in effect). */
5705 fp = cpp_push_buffer (pfile, NULL, 0);
5706 if (!fp)
5707 return 0;
5708 if (opts->in_fname == NULL)
5709 opts->in_fname = "";
5710 fp->nominal_fname = fp->fname = opts->in_fname;
5711 fp->lineno = 0;
5713 /* Install __LINE__, etc. Must follow initialize_char_syntax
5714 and option processing. */
5715 initialize_builtins (pfile);
5717 /* Do standard #defines and assertions
5718 that identify system and machine type. */
5720 if (!opts->inhibit_predefs) {
5721 char *p = (char *) alloca (strlen (predefs) + 1);
5722 strcpy (p, predefs);
5723 while (*p) {
5724 char *q;
5725 while (*p == ' ' || *p == '\t')
5726 p++;
5727 /* Handle -D options. */
5728 if (p[0] == '-' && p[1] == 'D') {
5729 q = &p[2];
5730 while (*p && *p != ' ' && *p != '\t')
5731 p++;
5732 if (*p != 0)
5733 *p++= 0;
5734 if (opts->debug_output)
5735 output_line_command (pfile, 0, same_file);
5736 cpp_define (pfile, q);
5737 while (*p == ' ' || *p == '\t')
5738 p++;
5739 } else if (p[0] == '-' && p[1] == 'A') {
5740 /* Handle -A options (assertions). */
5741 char *assertion;
5742 char *past_name;
5743 char *value;
5744 char *past_value;
5745 char *termination;
5746 int save_char;
5748 assertion = &p[2];
5749 past_name = assertion;
5750 /* Locate end of name. */
5751 while (*past_name && *past_name != ' '
5752 && *past_name != '\t' && *past_name != '(')
5753 past_name++;
5754 /* Locate `(' at start of value. */
5755 value = past_name;
5756 while (*value && (*value == ' ' || *value == '\t'))
5757 value++;
5758 if (*value++ != '(')
5759 abort ();
5760 while (*value && (*value == ' ' || *value == '\t'))
5761 value++;
5762 past_value = value;
5763 /* Locate end of value. */
5764 while (*past_value && *past_value != ' '
5765 && *past_value != '\t' && *past_value != ')')
5766 past_value++;
5767 termination = past_value;
5768 while (*termination && (*termination == ' ' || *termination == '\t'))
5769 termination++;
5770 if (*termination++ != ')')
5771 abort ();
5772 if (*termination && *termination != ' ' && *termination != '\t')
5773 abort ();
5774 /* Temporarily null-terminate the value. */
5775 save_char = *termination;
5776 *termination = '\0';
5777 /* Install the assertion. */
5778 make_assertion (pfile, "-A", assertion);
5779 *termination = (char) save_char;
5780 p = termination;
5781 while (*p == ' ' || *p == '\t')
5782 p++;
5783 } else {
5784 abort ();
5789 /* Now handle the command line options. */
5791 /* Do -U's, -D's and -A's in the order they were seen. */
5792 /* First reverse the list. */
5793 opts->pending = nreverse_pending (opts->pending);
5795 for (pend = opts->pending; pend; pend = pend->next)
5797 if (pend->cmd != NULL && pend->cmd[0] == '-')
5799 switch (pend->cmd[1])
5801 case 'U':
5802 if (opts->debug_output)
5803 output_line_command (pfile, 0, same_file);
5804 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5805 break;
5806 case 'D':
5807 if (opts->debug_output)
5808 output_line_command (pfile, 0, same_file);
5809 cpp_define (pfile, pend->arg);
5810 break;
5811 case 'A':
5812 make_assertion (pfile, "-A", pend->arg);
5813 break;
5818 opts->done_initializing = 1;
5820 { /* Read the appropriate environment variable and if it exists
5821 replace include_defaults with the listed path. */
5822 char *epath = 0;
5823 switch ((opts->objc << 1) + opts->cplusplus)
5825 case 0:
5826 epath = getenv ("C_INCLUDE_PATH");
5827 break;
5828 case 1:
5829 epath = getenv ("CPLUS_INCLUDE_PATH");
5830 break;
5831 case 2:
5832 epath = getenv ("OBJC_INCLUDE_PATH");
5833 break;
5834 case 3:
5835 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5836 break;
5838 /* If the environment var for this language is set,
5839 add to the default list of include directories. */
5840 if (epath) {
5841 char *nstore = (char *) alloca (strlen (epath) + 2);
5842 int num_dirs;
5843 char *startp, *endp;
5845 for (num_dirs = 1, startp = epath; *startp; startp++)
5846 if (*startp == PATH_SEPARATOR)
5847 num_dirs++;
5848 include_defaults
5849 = (struct default_include *) xmalloc ((num_dirs
5850 * sizeof (struct default_include))
5851 + sizeof (include_defaults_array));
5852 startp = endp = epath;
5853 num_dirs = 0;
5854 while (1) {
5855 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5856 if ((*endp == PATH_SEPARATOR)
5857 || *endp == 0) {
5858 strncpy (nstore, startp, endp-startp);
5859 if (endp == startp)
5860 strcpy (nstore, ".");
5861 else
5862 nstore[endp-startp] = '\0';
5864 include_defaults[num_dirs].fname = savestring (nstore);
5865 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5866 include_defaults[num_dirs].cxx_aware = 1;
5867 num_dirs++;
5868 if (*endp == '\0')
5869 break;
5870 endp = startp = endp + 1;
5871 } else
5872 endp++;
5874 /* Put the usual defaults back in at the end. */
5875 bcopy ((char *) include_defaults_array,
5876 (char *) &include_defaults[num_dirs],
5877 sizeof (include_defaults_array));
5881 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5882 opts->first_system_include = opts->before_system;
5884 /* Unless -fnostdinc,
5885 tack on the standard include file dirs to the specified list */
5886 if (!opts->no_standard_includes) {
5887 struct default_include *p = include_defaults;
5888 char *specd_prefix = opts->include_prefix;
5889 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5890 int default_len = 0;
5891 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5892 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5893 default_len = strlen (default_prefix) - 7;
5894 default_prefix[default_len] = 0;
5896 /* Search "translated" versions of GNU directories.
5897 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5898 if (specd_prefix != 0 && default_len != 0)
5899 for (p = include_defaults; p->fname; p++) {
5900 /* Some standard dirs are only for C++. */
5901 if (!p->cplusplus
5902 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5903 /* Does this dir start with the prefix? */
5904 if (!strncmp (p->fname, default_prefix, default_len)) {
5905 /* Yes; change prefix and add to search list. */
5906 struct file_name_list *new
5907 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5908 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5909 char *str = (char *) xmalloc (this_len + 1);
5910 strcpy (str, specd_prefix);
5911 strcat (str, p->fname + default_len);
5912 new->fname = str;
5913 new->control_macro = 0;
5914 new->c_system_include_path = !p->cxx_aware;
5915 new->got_name_map = 0;
5916 append_include_chain (pfile, new, new);
5917 if (opts->first_system_include == 0)
5918 opts->first_system_include = new;
5922 /* Search ordinary names for GNU include directories. */
5923 for (p = include_defaults; p->fname; p++) {
5924 /* Some standard dirs are only for C++. */
5925 if (!p->cplusplus
5926 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5927 struct file_name_list *new
5928 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5929 new->control_macro = 0;
5930 new->c_system_include_path = !p->cxx_aware;
5931 new->fname = p->fname;
5932 new->got_name_map = 0;
5933 append_include_chain (pfile, new, new);
5934 if (opts->first_system_include == 0)
5935 opts->first_system_include = new;
5940 /* Tack the after_include chain at the end of the include chain. */
5941 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5942 if (opts->first_system_include == 0)
5943 opts->first_system_include = opts->after_include;
5945 /* With -v, print the list of dirs to search. */
5946 if (opts->verbose) {
5947 struct file_name_list *p;
5948 fprintf (stderr, "#include \"...\" search starts here:\n");
5949 for (p = opts->include; p; p = p->next) {
5950 if (p == opts->first_bracket_include)
5951 fprintf (stderr, "#include <...> search starts here:\n");
5952 fprintf (stderr, " %s\n", p->fname);
5954 fprintf (stderr, "End of search list.\n");
5957 /* Scan the -imacros files before the main input.
5958 Much like #including them, but with no_output set
5959 so that only their macro definitions matter. */
5961 opts->no_output++; pfile->no_record_file++;
5962 for (pend = opts->pending; pend; pend = pend->next)
5964 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5966 int fd = open (pend->arg, O_RDONLY, 0666);
5967 if (fd < 0)
5969 cpp_perror_with_name (pfile, pend->arg);
5970 return 0;
5972 if (!cpp_push_buffer (pfile, NULL, 0))
5973 return 0;
5974 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
5975 cpp_scan_buffer (pfile);
5978 opts->no_output--; pfile->no_record_file--;
5980 /* Copy the entire contents of the main input file into
5981 the stacked input buffer previously allocated for it. */
5982 if (fname == NULL || *fname == 0) {
5983 fname = "";
5984 f = 0;
5985 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
5986 cpp_pfatal_with_name (pfile, fname);
5988 /* -MG doesn't select the form of output and must be specified with one of
5989 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5990 inhibit compilation. */
5991 if (opts->print_deps_missing_files
5992 && (opts->print_deps == 0 || !opts->no_output))
5994 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
5995 return 0;
5998 /* Either of two environment variables can specify output of deps.
5999 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6000 where OUTPUT_FILE is the file to write deps info to
6001 and DEPS_TARGET is the target to mention in the deps. */
6003 if (opts->print_deps == 0
6004 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6005 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6006 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6007 char *s;
6008 char *output_file;
6010 if (spec == 0)
6012 spec = getenv ("SUNPRO_DEPENDENCIES");
6013 opts->print_deps = 2;
6015 else
6016 opts->print_deps = 1;
6018 s = spec;
6019 /* Find the space before the DEPS_TARGET, if there is one. */
6020 /* This should use index. (mrs) */
6021 while (*s != 0 && *s != ' ') s++;
6022 if (*s != 0)
6024 opts->deps_target = s + 1;
6025 output_file = (char *) xmalloc (s - spec + 1);
6026 bcopy (spec, output_file, s - spec);
6027 output_file[s - spec] = 0;
6029 else
6031 opts->deps_target = 0;
6032 output_file = spec;
6035 opts->deps_file = output_file;
6036 opts->print_deps_append = 1;
6039 /* For -M, print the expected object file name
6040 as the target of this Make-rule. */
6041 if (opts->print_deps)
6043 pfile->deps_allocated_size = 200;
6044 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6045 pfile->deps_buffer[0] = 0;
6046 pfile->deps_size = 0;
6047 pfile->deps_column = 0;
6049 if (opts->deps_target)
6050 deps_output (pfile, opts->deps_target, ':');
6051 else if (*opts->in_fname == 0)
6052 deps_output (pfile, "-", ':');
6053 else
6055 char *p, *q;
6056 int len;
6058 /* Discard all directory prefixes from filename. */
6059 if ((q = rindex (opts->in_fname, '/')) != NULL
6060 #ifdef DIR_SEPARATOR
6061 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6062 #endif
6064 ++q;
6065 else
6066 q = opts->in_fname;
6068 /* Copy remainder to mungable area. */
6069 p = (char *) alloca (strlen(q) + 8);
6070 strcpy (p, q);
6072 /* Output P, but remove known suffixes. */
6073 len = strlen (p);
6074 q = p + len;
6075 if (len >= 2
6076 && p[len - 2] == '.'
6077 && index("cCsSm", p[len - 1]))
6078 q = p + (len - 2);
6079 else if (len >= 3
6080 && p[len - 3] == '.'
6081 && p[len - 2] == 'c'
6082 && p[len - 1] == 'c')
6083 q = p + (len - 3);
6084 else if (len >= 4
6085 && p[len - 4] == '.'
6086 && p[len - 3] == 'c'
6087 && p[len - 2] == 'x'
6088 && p[len - 1] == 'x')
6089 q = p + (len - 4);
6090 else if (len >= 4
6091 && p[len - 4] == '.'
6092 && p[len - 3] == 'c'
6093 && p[len - 2] == 'p'
6094 && p[len - 1] == 'p')
6095 q = p + (len - 4);
6097 /* Supply our own suffix. */
6098 #ifndef VMS
6099 strcpy (q, ".o");
6100 #else
6101 strcpy (q, ".obj");
6102 #endif
6104 deps_output (pfile, p, ':');
6105 deps_output (pfile, opts->in_fname, ' ');
6109 #if 0
6110 /* Make sure data ends with a newline. And put a null after it. */
6112 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6113 /* Backslash-newline at end is not good enough. */
6114 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6115 fp->buf[fp->length++] = '\n';
6116 missing_newline = 1;
6118 fp->buf[fp->length] = '\0';
6120 /* Unless inhibited, convert trigraphs in the input. */
6122 if (!no_trigraphs)
6123 trigraph_pcp (fp);
6124 #endif
6126 /* Scan the -include files before the main input.
6127 We push these in reverse order, so that the first one is handled first. */
6129 pfile->no_record_file++;
6130 opts->pending = nreverse_pending (opts->pending);
6131 for (pend = opts->pending; pend; pend = pend->next)
6133 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6135 int fd = open (pend->arg, O_RDONLY, 0666);
6136 if (fd < 0)
6138 cpp_perror_with_name (pfile, pend->arg);
6139 return 0;
6141 if (!cpp_push_buffer (pfile, NULL, 0))
6142 return 0;
6143 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6146 pfile->no_record_file--;
6148 /* Free the pending list. */
6149 for (pend = opts->pending; pend; )
6151 struct cpp_pending *next = pend->next;
6152 free (pend);
6153 pend = next;
6155 opts->pending = NULL;
6157 #if 0
6158 /* Scan the input, processing macros and directives. */
6160 rescan (&outbuf, 0);
6162 if (missing_newline)
6163 fp->lineno--;
6165 if (CPP_PEDANTIC (pfile) && missing_newline)
6166 pedwarn ("file does not end in newline");
6168 #endif
6169 if (finclude (pfile, f, fname, 0, NULL_PTR))
6170 output_line_command (pfile, 0, same_file);
6171 return 1;
6174 void
6175 cpp_reader_init (pfile)
6176 cpp_reader *pfile;
6178 bzero ((char *) pfile, sizeof (cpp_reader));
6179 pfile->get_token = cpp_get_token;
6181 pfile->token_buffer_size = 200;
6182 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
6183 CPP_SET_WRITTEN (pfile, 0);
6185 pfile->system_include_depth = 0;
6186 pfile->dont_repeat_files = 0;
6187 pfile->all_include_files = 0;
6188 pfile->max_include_len = 0;
6189 pfile->timebuf = NULL;
6190 pfile->only_seen_white = 1;
6191 pfile->buffer = CPP_NULL_BUFFER(pfile);
6194 static struct cpp_pending *
6195 nreverse_pending (list)
6196 struct cpp_pending *list;
6199 register struct cpp_pending *prev = 0, *next, *pend;
6200 for (pend = list; pend; pend = next)
6202 next = pend->next;
6203 pend->next = prev;
6204 prev = pend;
6206 return prev;
6209 static void
6210 push_pending (pfile, cmd, arg)
6211 cpp_reader *pfile;
6212 char *cmd;
6213 char *arg;
6215 struct cpp_pending *pend
6216 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
6217 pend->cmd = cmd;
6218 pend->arg = arg;
6219 pend->next = CPP_OPTIONS (pfile)->pending;
6220 CPP_OPTIONS (pfile)->pending = pend;
6223 /* Handle command-line options in (argc, argv).
6224 Can be called multiple times, to handle multiple sets of options.
6225 Returns if an unrecognized option is seen.
6226 Returns number of handled arguments. */
6229 cpp_handle_options (pfile, argc, argv)
6230 cpp_reader *pfile;
6231 int argc;
6232 char **argv;
6234 int i;
6235 struct cpp_options *opts = CPP_OPTIONS (pfile);
6236 for (i = 0; i < argc; i++) {
6237 if (argv[i][0] != '-') {
6238 if (opts->out_fname != NULL)
6240 cpp_fatal (pfile, "Usage: %s [switches] input output", argv[0]);
6241 return argc;
6243 else if (opts->in_fname != NULL)
6244 opts->out_fname = argv[i];
6245 else
6246 opts->in_fname = argv[i];
6247 } else {
6248 switch (argv[i][1]) {
6250 missing_filename:
6251 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
6252 return argc;
6253 missing_dirname:
6254 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
6255 return argc;
6257 case 'i':
6258 if (!strcmp (argv[i], "-include")
6259 || !strcmp (argv[i], "-imacros")) {
6260 if (i + 1 == argc)
6261 goto missing_filename;
6262 else
6263 push_pending (pfile, argv[i], argv[i+1]), i++;
6265 if (!strcmp (argv[i], "-iprefix")) {
6266 if (i + 1 == argc)
6267 goto missing_filename;
6268 else
6269 opts->include_prefix = argv[++i];
6271 if (!strcmp (argv[i], "-ifoutput")) {
6272 opts->output_conditionals = 1;
6274 if (!strcmp (argv[i], "-isystem")) {
6275 struct file_name_list *dirtmp;
6277 if (i + 1 == argc)
6278 goto missing_filename;
6280 dirtmp = (struct file_name_list *)
6281 xmalloc (sizeof (struct file_name_list));
6282 dirtmp->next = 0;
6283 dirtmp->control_macro = 0;
6284 dirtmp->c_system_include_path = 1;
6285 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6286 strcpy (dirtmp->fname, argv[++i]);
6287 dirtmp->got_name_map = 0;
6289 if (opts->before_system == 0)
6290 opts->before_system = dirtmp;
6291 else
6292 opts->last_before_system->next = dirtmp;
6293 opts->last_before_system = dirtmp; /* Tail follows the last one */
6295 /* Add directory to end of path for includes,
6296 with the default prefix at the front of its name. */
6297 if (!strcmp (argv[i], "-iwithprefix")) {
6298 struct file_name_list *dirtmp;
6299 char *prefix;
6301 if (opts->include_prefix != 0)
6302 prefix = opts->include_prefix;
6303 else {
6304 prefix = savestring (GCC_INCLUDE_DIR);
6305 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6306 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6307 prefix[strlen (prefix) - 7] = 0;
6310 dirtmp = (struct file_name_list *)
6311 xmalloc (sizeof (struct file_name_list));
6312 dirtmp->next = 0; /* New one goes on the end */
6313 dirtmp->control_macro = 0;
6314 dirtmp->c_system_include_path = 0;
6315 if (i + 1 == argc)
6316 goto missing_dirname;
6318 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6319 + strlen (prefix) + 1);
6320 strcpy (dirtmp->fname, prefix);
6321 strcat (dirtmp->fname, argv[++i]);
6322 dirtmp->got_name_map = 0;
6324 if (opts->after_include == 0)
6325 opts->after_include = dirtmp;
6326 else
6327 opts->last_after_include->next = dirtmp;
6328 opts->last_after_include = dirtmp; /* Tail follows the last one */
6330 /* Add directory to main path for includes,
6331 with the default prefix at the front of its name. */
6332 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6333 struct file_name_list *dirtmp;
6334 char *prefix;
6336 if (opts->include_prefix != 0)
6337 prefix = opts->include_prefix;
6338 else {
6339 prefix = savestring (GCC_INCLUDE_DIR);
6340 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6341 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6342 prefix[strlen (prefix) - 7] = 0;
6345 dirtmp = (struct file_name_list *)
6346 xmalloc (sizeof (struct file_name_list));
6347 dirtmp->next = 0; /* New one goes on the end */
6348 dirtmp->control_macro = 0;
6349 dirtmp->c_system_include_path = 0;
6350 if (i + 1 == argc)
6351 goto missing_dirname;
6353 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6354 + strlen (prefix) + 1);
6355 strcpy (dirtmp->fname, prefix);
6356 strcat (dirtmp->fname, argv[++i]);
6357 dirtmp->got_name_map = 0;
6359 append_include_chain (pfile, dirtmp, dirtmp);
6361 /* Add directory to end of path for includes. */
6362 if (!strcmp (argv[i], "-idirafter")) {
6363 struct file_name_list *dirtmp;
6365 dirtmp = (struct file_name_list *)
6366 xmalloc (sizeof (struct file_name_list));
6367 dirtmp->next = 0; /* New one goes on the end */
6368 dirtmp->control_macro = 0;
6369 dirtmp->c_system_include_path = 0;
6370 if (i + 1 == argc)
6371 goto missing_dirname;
6372 else
6373 dirtmp->fname = argv[++i];
6374 dirtmp->got_name_map = 0;
6376 if (opts->after_include == 0)
6377 opts->after_include = dirtmp;
6378 else
6379 opts->last_after_include->next = dirtmp;
6380 opts->last_after_include = dirtmp; /* Tail follows the last one */
6382 break;
6384 case 'o':
6385 if (opts->out_fname != NULL)
6387 cpp_fatal (pfile, "Output filename specified twice");
6388 return argc;
6390 if (i + 1 == argc)
6391 goto missing_filename;
6392 opts->out_fname = argv[++i];
6393 if (!strcmp (opts->out_fname, "-"))
6394 opts->out_fname = "";
6395 break;
6397 case 'p':
6398 if (!strcmp (argv[i], "-pedantic"))
6399 CPP_PEDANTIC (pfile) = 1;
6400 else if (!strcmp (argv[i], "-pedantic-errors")) {
6401 CPP_PEDANTIC (pfile) = 1;
6402 opts->pedantic_errors = 1;
6404 #if 0
6405 else if (!strcmp (argv[i], "-pcp")) {
6406 char *pcp_fname = argv[++i];
6407 pcp_outfile =
6408 ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6409 ? fopen (pcp_fname, "w")
6410 : fdopen (dup (fileno (stdout)), "w"));
6411 if (pcp_outfile == 0)
6412 cpp_pfatal_with_name (pfile, pcp_fname);
6413 no_precomp = 1;
6415 #endif
6416 break;
6418 case 't':
6419 if (!strcmp (argv[i], "-traditional")) {
6420 opts->traditional = 1;
6421 if (opts->dollars_in_ident > 0)
6422 opts->dollars_in_ident = 1;
6423 } else if (!strcmp (argv[i], "-trigraphs")) {
6424 if (!opts->chill)
6425 opts->no_trigraphs = 0;
6427 break;
6429 case 'l':
6430 if (! strcmp (argv[i], "-lang-c"))
6431 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->objc = 0;
6432 if (! strcmp (argv[i], "-lang-c++"))
6433 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->objc = 0;
6434 if (! strcmp (argv[i], "-lang-c-c++-comments"))
6435 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->objc = 0;
6436 if (! strcmp (argv[i], "-lang-objc"))
6437 opts->objc = 1, opts->cplusplus = 0, opts->cplusplus_comments = 1;
6438 if (! strcmp (argv[i], "-lang-objc++"))
6439 opts->objc = 1, opts->cplusplus = 1, opts->cplusplus_comments = 1;
6440 if (! strcmp (argv[i], "-lang-asm"))
6441 opts->lang_asm = 1;
6442 if (! strcmp (argv[i], "-lint"))
6443 opts->for_lint = 1;
6444 if (! strcmp (argv[i], "-lang-chill"))
6445 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6446 opts->traditional = 1, opts->no_trigraphs = 1;
6447 break;
6449 case '+':
6450 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6451 break;
6453 case 'w':
6454 opts->inhibit_warnings = 1;
6455 break;
6457 case 'W':
6458 if (!strcmp (argv[i], "-Wtrigraphs"))
6459 opts->warn_trigraphs = 1;
6460 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6461 opts->warn_trigraphs = 0;
6462 else if (!strcmp (argv[i], "-Wcomment"))
6463 opts->warn_comments = 1;
6464 else if (!strcmp (argv[i], "-Wno-comment"))
6465 opts->warn_comments = 0;
6466 else if (!strcmp (argv[i], "-Wcomments"))
6467 opts->warn_comments = 1;
6468 else if (!strcmp (argv[i], "-Wno-comments"))
6469 opts->warn_comments = 0;
6470 else if (!strcmp (argv[i], "-Wtraditional"))
6471 opts->warn_stringify = 1;
6472 else if (!strcmp (argv[i], "-Wno-traditional"))
6473 opts->warn_stringify = 0;
6474 else if (!strcmp (argv[i], "-Wimport"))
6475 opts->warn_import = 1;
6476 else if (!strcmp (argv[i], "-Wno-import"))
6477 opts->warn_import = 0;
6478 else if (!strcmp (argv[i], "-Werror"))
6479 opts->warnings_are_errors = 1;
6480 else if (!strcmp (argv[i], "-Wno-error"))
6481 opts->warnings_are_errors = 0;
6482 else if (!strcmp (argv[i], "-Wall"))
6484 opts->warn_trigraphs = 1;
6485 opts->warn_comments = 1;
6487 break;
6489 case 'M':
6490 /* The style of the choices here is a bit mixed.
6491 The chosen scheme is a hybrid of keeping all options in one string
6492 and specifying each option in a separate argument:
6493 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6494 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6495 -M[M][G][D file]. This is awkward to handle in specs, and is not
6496 as extensible. */
6497 /* ??? -MG must be specified in addition to one of -M or -MM.
6498 This can be relaxed in the future without breaking anything.
6499 The converse isn't true. */
6501 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6502 if (!strcmp (argv[i], "-MG"))
6504 opts->print_deps_missing_files = 1;
6505 break;
6507 if (!strcmp (argv[i], "-M"))
6508 opts->print_deps = 2;
6509 else if (!strcmp (argv[i], "-MM"))
6510 opts->print_deps = 1;
6511 else if (!strcmp (argv[i], "-MD"))
6512 opts->print_deps = 2;
6513 else if (!strcmp (argv[i], "-MMD"))
6514 opts->print_deps = 1;
6515 /* For -MD and -MMD options, write deps on file named by next arg. */
6516 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6518 if (i+1 == argc)
6519 goto missing_filename;
6520 opts->deps_file = argv[++i];
6522 else
6524 /* For -M and -MM, write deps on standard output
6525 and suppress the usual output. */
6526 opts->no_output = 1;
6528 break;
6530 case 'd':
6532 char *p = argv[i] + 2;
6533 char c;
6534 while ((c = *p++) != 0) {
6535 /* Arg to -d specifies what parts of macros to dump */
6536 switch (c) {
6537 case 'M':
6538 opts->dump_macros = dump_only;
6539 opts->no_output = 1;
6540 break;
6541 case 'N':
6542 opts->dump_macros = dump_names;
6543 break;
6544 case 'D':
6545 opts->dump_macros = dump_definitions;
6546 break;
6550 break;
6552 case 'g':
6553 if (argv[i][2] == '3')
6554 opts->debug_output = 1;
6555 break;
6557 case 'v':
6558 fprintf (stderr, "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", NULL);
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 'u':
6692 /* Sun compiler passes undocumented switch "-undef".
6693 Let's assume it means to inhibit the predefined symbols. */
6694 opts->inhibit_predefs = 1;
6695 break;
6697 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6698 if (opts->in_fname == NULL) {
6699 opts->in_fname = "";
6700 break;
6701 } else if (opts->out_fname == NULL) {
6702 opts->out_fname = "";
6703 break;
6704 } /* else fall through into error */
6706 default:
6707 return i;
6711 return i;
6714 void
6715 cpp_finish (pfile)
6716 cpp_reader *pfile;
6718 struct cpp_options *opts = CPP_OPTIONS (pfile);
6720 if (opts->print_deps)
6722 /* Stream on which to print the dependency information. */
6723 FILE *deps_stream;
6725 /* Don't actually write the deps file if compilation has failed. */
6726 if (pfile->errors == 0)
6728 char *deps_mode = opts->print_deps_append ? "a" : "w";
6729 if (opts->deps_file == 0)
6730 deps_stream = stdout;
6731 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6732 cpp_pfatal_with_name (pfile, opts->deps_file);
6733 fputs (pfile->deps_buffer, deps_stream);
6734 putc ('\n', deps_stream);
6735 if (opts->deps_file)
6737 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6738 cpp_fatal (pfile, "I/O error on output");
6744 /* Free resources used by PFILE.
6745 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6747 void
6748 cpp_cleanup (pfile)
6749 cpp_reader *pfile;
6751 int i;
6752 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6753 cpp_pop_buffer (pfile);
6755 if (pfile->token_buffer)
6757 free (pfile->token_buffer);
6758 pfile->token_buffer = NULL;
6761 if (pfile->deps_buffer)
6763 free (pfile->deps_buffer);
6764 pfile->deps_buffer = NULL;
6765 pfile->deps_allocated_size = 0;
6768 while (pfile->if_stack)
6770 IF_STACK_FRAME *temp = pfile->if_stack;
6771 pfile->if_stack = temp->next;
6772 free (temp);
6775 while (pfile->dont_repeat_files)
6777 struct file_name_list *temp = pfile->dont_repeat_files;
6778 pfile->dont_repeat_files = temp->next;
6779 free (temp->fname);
6780 free (temp);
6783 while (pfile->all_include_files)
6785 struct file_name_list *temp = pfile->all_include_files;
6786 pfile->all_include_files = temp->next;
6787 free (temp->fname);
6788 free (temp);
6791 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6793 register struct import_file *imp = pfile->import_hash_table[i];
6794 while (imp)
6796 struct import_file *next = imp->next;
6797 free (imp->name);
6798 free (imp);
6799 imp = next;
6801 pfile->import_hash_table[i] = 0;
6804 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6806 while (pfile->assertion_hashtab[i])
6807 delete_assertion (pfile->assertion_hashtab[i]);
6810 cpp_hash_cleanup (pfile);
6813 static int
6814 do_assert (pfile, keyword, buf, limit)
6815 cpp_reader *pfile;
6816 struct directive *keyword;
6817 U_CHAR *buf, *limit;
6819 long symstart; /* remember where symbol name starts */
6820 int c;
6821 int sym_length; /* and how long it is */
6822 struct arglist *tokens = NULL;
6824 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6825 && !CPP_BUFFER (pfile)->system_header_p)
6826 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6828 cpp_skip_hspace (pfile);
6829 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6830 parse_name (pfile, GETC());
6831 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6832 "assertion");
6834 cpp_skip_hspace (pfile);
6835 if (PEEKC() != '(') {
6836 cpp_error (pfile, "missing token-sequence in `#assert'");
6837 goto error;
6841 int error_flag = 0;
6842 tokens = read_token_list (pfile, &error_flag);
6843 if (error_flag)
6844 goto error;
6845 if (tokens == 0) {
6846 cpp_error (pfile, "empty token-sequence in `#assert'");
6847 goto error;
6849 cpp_skip_hspace (pfile);
6850 c = PEEKC ();
6851 if (c != EOF && c != '\n')
6852 cpp_pedwarn (pfile, "junk at end of `#assert'");
6853 skip_rest_of_line (pfile);
6856 /* If this name isn't already an assertion name, make it one.
6857 Error if it was already in use in some other way. */
6860 ASSERTION_HASHNODE *hp;
6861 U_CHAR *symname = pfile->token_buffer + symstart;
6862 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6863 struct tokenlist_list *value
6864 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6866 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6867 if (hp == NULL) {
6868 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6869 cpp_error (pfile, "`defined' redefined as assertion");
6870 hp = assertion_install (pfile, symname, sym_length, hashcode);
6873 /* Add the spec'd token-sequence to the list of such. */
6874 value->tokens = tokens;
6875 value->next = hp->value;
6876 hp->value = value;
6878 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6879 return 0;
6880 error:
6881 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6882 skip_rest_of_line (pfile);
6883 return 1;
6886 static int
6887 do_unassert (pfile, keyword, buf, limit)
6888 cpp_reader *pfile;
6889 struct directive *keyword;
6890 U_CHAR *buf, *limit;
6892 long symstart; /* remember where symbol name starts */
6893 int sym_length; /* and how long it is */
6894 int c;
6896 struct arglist *tokens = NULL;
6897 int tokens_specified = 0;
6899 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6900 && !CPP_BUFFER (pfile)->system_header_p)
6901 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6903 cpp_skip_hspace (pfile);
6905 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6906 parse_name (pfile, GETC());
6907 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6908 "assertion");
6910 cpp_skip_hspace (pfile);
6911 if (PEEKC() == '(') {
6912 int error_flag = 0;
6914 tokens = read_token_list (pfile, &error_flag);
6915 if (error_flag)
6916 goto error;
6917 if (tokens == 0) {
6918 cpp_error (pfile, "empty token list in `#unassert'");
6919 goto error;
6922 tokens_specified = 1;
6925 cpp_skip_hspace (pfile);
6926 c = PEEKC ();
6927 if (c != EOF && c != '\n')
6928 cpp_error (pfile, "junk at end of `#unassert'");
6929 skip_rest_of_line (pfile);
6932 ASSERTION_HASHNODE *hp;
6933 U_CHAR *symname = pfile->token_buffer + symstart;
6934 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6935 struct tokenlist_list *tail, *prev;
6937 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6938 if (hp == NULL)
6939 return 1;
6941 /* If no token list was specified, then eliminate this assertion
6942 entirely. */
6943 if (! tokens_specified)
6944 delete_assertion (hp);
6945 else {
6946 /* If a list of tokens was given, then delete any matching list. */
6948 tail = hp->value;
6949 prev = 0;
6950 while (tail) {
6951 struct tokenlist_list *next = tail->next;
6952 if (compare_token_lists (tail->tokens, tokens)) {
6953 if (prev)
6954 prev->next = next;
6955 else
6956 hp->value = tail->next;
6957 free_token_list (tail->tokens);
6958 free (tail);
6959 } else {
6960 prev = tail;
6962 tail = next;
6967 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6968 return 0;
6969 error:
6970 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6971 skip_rest_of_line (pfile);
6972 return 1;
6975 /* Test whether there is an assertion named NAME
6976 and optionally whether it has an asserted token list TOKENS.
6977 NAME is not null terminated; its length is SYM_LENGTH.
6978 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6981 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
6982 cpp_reader *pfile;
6983 U_CHAR *name;
6984 int sym_length;
6985 int tokens_specified;
6986 struct arglist *tokens;
6988 ASSERTION_HASHNODE *hp;
6989 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6991 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
6992 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
6994 hp = assertion_lookup (pfile, name, sym_length, hashcode);
6995 if (hp == NULL)
6996 /* It is not an assertion; just return false. */
6997 return 0;
6999 /* If no token list was specified, then value is 1. */
7000 if (! tokens_specified)
7001 return 1;
7004 struct tokenlist_list *tail;
7006 tail = hp->value;
7008 /* If a list of tokens was given,
7009 then succeed if the assertion records a matching list. */
7011 while (tail) {
7012 if (compare_token_lists (tail->tokens, tokens))
7013 return 1;
7014 tail = tail->next;
7017 /* Fail if the assertion has no matching list. */
7018 return 0;
7022 /* Compare two lists of tokens for equality including order of tokens. */
7024 static int
7025 compare_token_lists (l1, l2)
7026 struct arglist *l1, *l2;
7028 while (l1 && l2) {
7029 if (l1->length != l2->length)
7030 return 0;
7031 if (strncmp (l1->name, l2->name, l1->length))
7032 return 0;
7033 l1 = l1->next;
7034 l2 = l2->next;
7037 /* Succeed if both lists end at the same time. */
7038 return l1 == l2;
7041 struct arglist *
7042 reverse_token_list (tokens)
7043 struct arglist *tokens;
7045 register struct arglist *prev = 0, *this, *next;
7046 for (this = tokens; this; this = next)
7048 next = this->next;
7049 this->next = prev;
7050 prev = this;
7052 return prev;
7055 /* Read a space-separated list of tokens ending in a close parenthesis.
7056 Return a list of strings, in the order they were written.
7057 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7059 static struct arglist *
7060 read_token_list (pfile, error_flag)
7061 cpp_reader *pfile;
7062 int *error_flag;
7064 struct arglist *token_ptrs = 0;
7065 int depth = 1;
7066 int length;
7068 *error_flag = 0;
7069 FORWARD (1); /* Skip '(' */
7071 /* Loop over the assertion value tokens. */
7072 while (depth > 0)
7074 struct arglist *temp;
7075 long name_written = CPP_WRITTEN (pfile);
7076 int eofp = 0; int c;
7078 cpp_skip_hspace (pfile);
7080 c = GETC ();
7082 /* Find the end of the token. */
7083 if (c == '(')
7085 CPP_PUTC (pfile, c);
7086 depth++;
7088 else if (c == ')')
7090 depth--;
7091 if (depth == 0)
7092 break;
7093 CPP_PUTC (pfile, c);
7095 else if (c == '"' || c == '\'')
7097 FORWARD(-1);
7098 cpp_get_token (pfile);
7100 else if (c == '\n')
7101 break;
7102 else
7104 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7105 && c != '"' && c != '\'')
7107 CPP_PUTC (pfile, c);
7108 c = GETC();
7110 if (c != EOF) FORWARD(-1);
7113 length = CPP_WRITTEN (pfile) - name_written;
7114 temp = (struct arglist *)
7115 xmalloc (sizeof (struct arglist) + length + 1);
7116 temp->name = (U_CHAR *) (temp + 1);
7117 bcopy ((char *) (pfile->token_buffer + name_written),
7118 (char *) temp->name, length);
7119 temp->name[length] = 0;
7120 temp->next = token_ptrs;
7121 token_ptrs = temp;
7122 temp->length = length;
7124 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7126 if (c == EOF || c == '\n')
7127 { /* FIXME */
7128 cpp_error (pfile,
7129 "unterminated token sequence following `#' operator");
7130 return 0;
7134 /* We accumulated the names in reverse order.
7135 Now reverse them to get the proper order. */
7136 return reverse_token_list (token_ptrs);
7139 static void
7140 free_token_list (tokens)
7141 struct arglist *tokens;
7143 while (tokens) {
7144 struct arglist *next = tokens->next;
7145 free (tokens->name);
7146 free (tokens);
7147 tokens = next;
7151 /* Get the file-mode and data size of the file open on FD
7152 and store them in *MODE_POINTER and *SIZE_POINTER. */
7154 static int
7155 file_size_and_mode (fd, mode_pointer, size_pointer)
7156 int fd;
7157 int *mode_pointer;
7158 long int *size_pointer;
7160 struct stat sbuf;
7162 if (fstat (fd, &sbuf) < 0) return (-1);
7163 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7164 if (size_pointer) *size_pointer = sbuf.st_size;
7165 return 0;
7168 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7169 retrying if necessary. Return a negative value if an error occurs,
7170 otherwise return the actual number of bytes read,
7171 which must be LEN unless end-of-file was reached. */
7173 static int
7174 safe_read (desc, ptr, len)
7175 int desc;
7176 char *ptr;
7177 int len;
7179 int left = len;
7180 while (left > 0) {
7181 int nchars = read (desc, ptr, left);
7182 if (nchars < 0)
7184 #ifdef EINTR
7185 if (errno == EINTR)
7186 continue;
7187 #endif
7188 return nchars;
7190 if (nchars == 0)
7191 break;
7192 ptr += nchars;
7193 left -= nchars;
7195 return len - left;
7198 static char *
7199 xcalloc (number, size)
7200 unsigned number, size;
7202 register unsigned total = number * size;
7203 register char *ptr = (char *) xmalloc (total);
7204 bzero (ptr, total);
7205 return ptr;
7208 static char *
7209 savestring (input)
7210 char *input;
7212 unsigned size = strlen (input);
7213 char *output = xmalloc (size + 1);
7214 strcpy (output, input);
7215 return output;
7218 /* Initialize PMARK to remember the current position of PFILE. */
7220 void
7221 parse_set_mark (pmark, pfile)
7222 struct parse_marker *pmark;
7223 cpp_reader *pfile;
7225 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7226 pmark->next = pbuf->marks;
7227 pbuf->marks = pmark;
7228 pmark->buf = pbuf;
7229 pmark->position = pbuf->cur - pbuf->buf;
7232 /* Cleanup PMARK - we no longer need it. */
7234 void
7235 parse_clear_mark (pmark)
7236 struct parse_marker *pmark;
7238 struct parse_marker **pp = &pmark->buf->marks;
7239 for (; ; pp = &(*pp)->next) {
7240 if (*pp == NULL) abort ();
7241 if (*pp == pmark) break;
7243 *pp = pmark->next;
7246 /* Backup the current position of PFILE to that saved in PMARK. */
7248 void
7249 parse_goto_mark (pmark, pfile)
7250 struct parse_marker *pmark;
7251 cpp_reader *pfile;
7253 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7254 if (pbuf != pmark->buf)
7255 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
7256 pbuf->cur = pbuf->buf + pmark->position;
7259 /* Reset PMARK to point to the current position of PFILE. (Same
7260 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7262 void
7263 parse_move_mark (pmark, pfile)
7264 struct parse_marker *pmark;
7265 cpp_reader *pfile;
7267 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7268 if (pbuf != pmark->buf)
7269 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
7270 pmark->position = pbuf->cur - pbuf->buf;
7274 cpp_read_check_assertion (pfile)
7275 cpp_reader *pfile;
7277 int name_start = CPP_WRITTEN (pfile);
7278 int name_length, name_written;
7279 int result;
7280 FORWARD (1); /* Skip '#' */
7281 cpp_skip_hspace (pfile);
7282 parse_name (pfile, GETC ());
7283 name_written = CPP_WRITTEN (pfile);
7284 name_length = name_written - name_start;
7285 cpp_skip_hspace (pfile);
7286 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7288 int error_flag;
7289 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7290 result = check_assertion (pfile,
7291 pfile->token_buffer + name_start, name_length,
7292 1, token_ptrs);
7294 else
7295 result = check_assertion (pfile,
7296 pfile->token_buffer + name_start, name_length,
7297 0, NULL_PTR);
7298 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7299 return result;
7302 void
7303 cpp_print_file_and_line (pfile)
7304 cpp_reader *pfile;
7306 cpp_buffer *ip = cpp_file_buffer (pfile);
7308 if (ip != NULL)
7310 long line, col;
7311 cpp_buf_line_and_col (ip, &line, &col);
7312 cpp_file_line_for_message (pfile, ip->nominal_fname,
7313 line, pfile->show_column ? col : -1);
7317 void
7318 cpp_error (pfile, msg, arg1, arg2, arg3)
7319 cpp_reader *pfile;
7320 char *msg;
7321 char *arg1, *arg2, *arg3;
7323 cpp_print_containing_files (pfile);
7324 cpp_print_file_and_line (pfile);
7325 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7328 /* Print error message but don't count it. */
7330 void
7331 cpp_warning (pfile, msg, arg1, arg2, arg3)
7332 cpp_reader *pfile;
7333 char *msg;
7334 char *arg1, *arg2, *arg3;
7336 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7337 return;
7339 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7340 pfile->errors++;
7342 cpp_print_containing_files (pfile);
7343 cpp_print_file_and_line (pfile);
7344 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7347 /* Print an error message and maybe count it. */
7349 void
7350 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7351 cpp_reader *pfile;
7352 char *msg;
7353 char *arg1, *arg2, *arg3;
7355 if (CPP_OPTIONS (pfile)->pedantic_errors)
7356 cpp_error (pfile, msg, arg1, arg2, arg3);
7357 else
7358 cpp_warning (pfile, msg, arg1, arg2, arg3);
7361 void
7362 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7363 cpp_reader *pfile;
7364 int line, column;
7365 char *msg;
7366 char *arg1, *arg2, *arg3;
7368 int i;
7369 cpp_buffer *ip = cpp_file_buffer (pfile);
7371 cpp_print_containing_files (pfile);
7373 if (ip != NULL)
7374 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7376 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7379 static void
7380 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7381 cpp_reader *pfile;
7382 int line, column;
7383 char *msg;
7384 char *arg1, *arg2, *arg3;
7386 int i;
7387 cpp_buffer *ip;
7389 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7390 return;
7392 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7393 pfile->errors++;
7395 cpp_print_containing_files (pfile);
7397 ip = cpp_file_buffer (pfile);
7399 if (ip != NULL)
7400 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7402 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7405 void
7406 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7407 cpp_reader *pfile;
7408 int line;
7409 char *msg;
7410 char *arg1, *arg2, *arg3;
7412 if (CPP_OPTIONS (pfile)->pedantic_errors)
7413 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7414 else
7415 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7418 /* Report a warning (or an error if pedantic_errors)
7419 giving specified file name and line number, not current. */
7421 void
7422 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7423 cpp_reader *pfile;
7424 char *file;
7425 int line;
7426 char *msg;
7427 char *arg1, *arg2, *arg3;
7429 if (!CPP_OPTIONS (pfile)->pedantic_errors
7430 && CPP_OPTIONS (pfile)->inhibit_warnings)
7431 return;
7432 if (file != NULL)
7433 cpp_file_line_for_message (pfile, file, line, -1);
7434 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7435 msg, arg1, arg2, arg3);
7438 /* This defines "errno" properly for VMS, and gives us EACCES. */
7439 #include <errno.h>
7440 #ifndef errno
7441 extern int errno;
7442 #endif
7444 #ifndef VMS
7445 #ifndef HAVE_STRERROR
7446 extern int sys_nerr;
7447 #if defined(bsd4_4)
7448 extern const char *const sys_errlist[];
7449 #else
7450 extern char *sys_errlist[];
7451 #endif
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 = "undocumented I/O error";
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 i;
7499 cpp_buffer *ip = cpp_file_buffer (pfile);
7501 cpp_print_containing_files (pfile);
7503 if (ip != NULL)
7504 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7506 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7509 void
7510 cpp_perror_with_name (pfile, name)
7511 cpp_reader *pfile;
7512 char *name;
7514 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7517 /* TODO:
7518 * No pre-compiled header file support.
7520 * Possibly different enum token codes for each C/C++ token.
7522 * Should clean up remaining directives to that do_XXX functions
7523 * only take two arguments and all have command_reads_line.
7525 * Find and cleanup remaining uses of static variables,
7527 * Support for trigraphs.
7529 * Support -dM flag (dump_all_macros).
7531 * Support for_lint flag.