Initial revision
[official-gcc.git] / gcc / cpplib.c
blob50ef8caae34fda34167e0bddafa4ab7d544650b9
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 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 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
46 #ifdef __STDC__
47 #define PTR_INT_TYPE ptrdiff_t
48 #else
49 #define PTR_INT_TYPE long
50 #endif
51 #endif /* 0 */
53 #include "cpplib.h"
54 #include "cpphash.h"
56 #ifndef STDC_VALUE
57 #define STDC_VALUE 1
58 #endif
60 /* By default, colon separates directories in a path. */
61 #ifndef PATH_SEPARATOR
62 #define PATH_SEPARATOR ':'
63 #endif
65 #include <ctype.h>
66 #include <stdio.h>
67 #include <signal.h>
68 #ifdef __STDC__
69 #include <stdlib.h>
70 #endif
72 #ifndef VMS
73 #ifndef USG
74 #include <sys/time.h> /* for __DATE__ and __TIME__ */
75 #include <sys/resource.h>
76 #else
77 #include <sys/times.h>
78 #include <time.h>
79 #include <fcntl.h>
80 #endif /* USG */
81 #endif /* not VMS */
83 /* This defines "errno" properly for VMS, and gives us EACCES. */
84 #include <errno.h>
86 extern char *index ();
87 extern char *rindex ();
89 #ifndef O_RDONLY
90 #define O_RDONLY 0
91 #endif
93 #undef MIN
94 #undef MAX
95 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
96 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
98 /* Find the largest host integer type and set its size and type. */
100 #ifndef HOST_BITS_PER_WIDE_INT
102 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
103 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
104 #define HOST_WIDE_INT long
105 #else
106 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
107 #define HOST_WIDE_INT int
108 #endif
110 #endif
112 #ifndef S_ISREG
113 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
114 #endif
116 #ifndef S_ISDIR
117 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
118 #endif
120 /* Define a generic NULL if one hasn't already been defined. */
122 #ifndef NULL
123 #define NULL 0
124 #endif
126 #ifndef GENERIC_PTR
127 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
128 #define GENERIC_PTR void *
129 #else
130 #define GENERIC_PTR char *
131 #endif
132 #endif
134 #ifndef NULL_PTR
135 #define NULL_PTR ((GENERIC_PTR) 0)
136 #endif
138 #ifndef INCLUDE_LEN_FUDGE
139 #define INCLUDE_LEN_FUDGE 0
140 #endif
142 /* Symbols to predefine. */
144 #ifdef CPP_PREDEFINES
145 static char *predefs = CPP_PREDEFINES;
146 #else
147 static char *predefs = "";
148 #endif
150 /* We let tm.h override the types used here, to handle trivial differences
151 such as the choice of unsigned int or long unsigned int for size_t.
152 When machines start needing nontrivial differences in the size type,
153 it would be best to do something here to figure out automatically
154 from other information what type to use. */
156 /* The string value for __SIZE_TYPE__. */
158 #ifndef SIZE_TYPE
159 #define SIZE_TYPE "long unsigned int"
160 #endif
162 /* The string value for __PTRDIFF_TYPE__. */
164 #ifndef PTRDIFF_TYPE
165 #define PTRDIFF_TYPE "long int"
166 #endif
168 /* The string value for __WCHAR_TYPE__. */
170 #ifndef WCHAR_TYPE
171 #define WCHAR_TYPE "int"
172 #endif
173 #define CPP_WCHAR_TYPE(PFILE) \
174 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
176 /* The string value for __USER_LABEL_PREFIX__ */
178 #ifndef USER_LABEL_PREFIX
179 #define USER_LABEL_PREFIX ""
180 #endif
182 /* The string value for __REGISTER_PREFIX__ */
184 #ifndef REGISTER_PREFIX
185 #define REGISTER_PREFIX ""
186 #endif
188 /* In the definition of a #assert name, this structure forms
189 a list of the individual values asserted.
190 Each value is itself a list of "tokens".
191 These are strings that are compared by name. */
193 struct tokenlist_list {
194 struct tokenlist_list *next;
195 struct arglist *tokens;
198 struct assertion_hashnode {
199 struct assertion_hashnode *next; /* double links for easy deletion */
200 struct assertion_hashnode *prev;
201 /* also, a back pointer to this node's hash
202 chain is kept, in case the node is the head
203 of the chain and gets deleted. */
204 struct assertion_hashnode **bucket_hdr;
205 int length; /* length of token, for quick comparison */
206 U_CHAR *name; /* the actual name */
207 /* List of token-sequences. */
208 struct tokenlist_list *value;
211 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
212 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
214 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
215 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
216 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
217 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
218 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
219 (Note that it is false while we're expanding marco *arguments*.) */
220 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
222 /* Move all backslash-newline pairs out of embarrassing places.
223 Exchange all such pairs following BP
224 with any potentially-embarrassing characters that follow them.
225 Potentially-embarrassing characters are / and *
226 (because a backslash-newline inside a comment delimiter
227 would cause it not to be recognized). */
229 #define NEWLINE_FIX \
230 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
232 /* Same, but assume we've already read the potential '\\' into C. */
233 #define NEWLINE_FIX1(C) do { \
234 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
235 } while(0)
237 struct cpp_pending {
238 struct cpp_pending *next;
239 char *cmd;
240 char *arg;
243 /* Forward declarations. */
245 extern char *xmalloc ();
247 static void add_import ();
248 static void append_include_chain ();
249 static void make_undef ();
250 static void make_assertion ();
251 static void path_include ();
252 static void initialize_builtins ();
253 static void initialize_char_syntax ();
254 static void dump_arg_n ();
255 static void dump_defn_1 ();
256 extern void delete_macro ();
257 static void trigraph_pcp ();
258 static int finclude ();
259 static void validate_else ();
260 static int comp_def_part ();
261 #ifdef abort
262 extern void fancy_abort ();
263 #endif
264 static void pipe_closed ();
265 static void print_containing_files ();
266 static int lookup_import ();
267 static int redundant_include_p ();
268 static is_system_include ();
269 static struct file_name_map *read_name_map ();
270 static char *read_filename_string ();
271 static int open_include_file ();
272 static int check_preconditions ();
273 static void pcfinclude ();
274 static void pcstring_used ();
275 static int check_macro_name ();
276 static int compare_defs ();
277 static int compare_token_lists ();
278 static HOST_WIDE_INT eval_if_expression ();
279 static int change_newlines ();
280 extern int hashf ();
281 static int file_size_and_mode ();
282 static struct arglist *read_token_list ();
283 static void free_token_list ();
284 static int safe_read ();
285 static void push_macro_expansion PARAMS ((cpp_reader *,
286 U_CHAR *, int, HASHNODE *));
287 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
288 extern char *xrealloc ();
289 static char *xcalloc ();
290 static char *savestring ();
292 static void conditional_skip ();
293 static void skip_if_group ();
295 /* Last arg to output_line_command. */
296 enum file_change_code {same_file, enter_file, leave_file};
298 /* External declarations. */
300 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
302 extern char *getenv ();
303 extern FILE *fdopen ();
304 extern char *version_string;
305 extern struct tm *localtime ();
307 /* These functions are declared to return int instead of void since they
308 are going to be placed in a table and some old compilers have trouble with
309 pointers to functions returning void. */
311 static int do_define ();
312 static int do_line ();
313 static int do_include ();
314 static int do_undef ();
315 static int do_error ();
316 static int do_pragma ();
317 static int do_ident ();
318 static int do_if ();
319 static int do_xifdef ();
320 static int do_else ();
321 static int do_elif ();
322 static int do_endif ();
323 static int do_sccs ();
324 static int do_once ();
325 static int do_assert ();
326 static int do_unassert ();
327 static int do_warning ();
329 struct file_name_list
331 struct file_name_list *next;
332 char *fname;
333 /* If the following is nonzero, it is a macro name.
334 Don't include the file again if that macro is defined. */
335 U_CHAR *control_macro;
336 /* If the following is nonzero, it is a C-language system include
337 directory. */
338 int c_system_include_path;
339 /* Mapping of file names for this directory. */
340 struct file_name_map *name_map;
341 /* Non-zero if name_map is valid. */
342 int got_name_map;
345 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
346 via the same directory as the file that #included it. */
347 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
349 /* #include "file" looks in source file dir, then stack. */
350 /* #include <file> just looks in the stack. */
351 /* -I directories are added to the end, then the defaults are added. */
352 /* The */
353 static struct default_include {
354 char *fname; /* The name of the directory. */
355 int cplusplus; /* Only look here if we're compiling C++. */
356 int cxx_aware; /* Includes in this directory don't need to
357 be wrapped in extern "C" when compiling
358 C++. */
359 } include_defaults_array[]
360 #ifdef INCLUDE_DEFAULTS
361 = INCLUDE_DEFAULTS;
362 #else
364 /* Pick up GNU C++ specific include files. */
365 { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
366 { OLD_GPLUSPLUS_INCLUDE_DIR, 1, 1 },
367 #ifdef CROSS_COMPILE
368 /* This is the dir for fixincludes. Put it just before
369 the files that we fix. */
370 { GCC_INCLUDE_DIR, 0, 0 },
371 /* For cross-compilation, this dir name is generated
372 automatically in Makefile.in. */
373 { CROSS_INCLUDE_DIR, 0, 0 },
374 #ifdef TOOL_INCLUDE_DIR
375 /* This is another place that the target system's headers might be. */
376 { TOOL_INCLUDE_DIR, 0, 1 },
377 #endif
378 #else /* not CROSS_COMPILE */
379 #ifdef LOCAL_INCLUDE_DIR
380 /* This should be /usr/local/include and should come before
381 the fixincludes-fixed header files. */
382 { LOCAL_INCLUDE_DIR, 0, 1 },
383 #endif
384 #ifdef TOOL_INCLUDE_DIR
385 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
386 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
387 { TOOL_INCLUDE_DIR, 0, 1 },
388 #endif
389 /* This is the dir for fixincludes. Put it just before
390 the files that we fix. */
391 { GCC_INCLUDE_DIR, 0, 0 },
392 /* Some systems have an extra dir of include files. */
393 #ifdef SYSTEM_INCLUDE_DIR
394 { SYSTEM_INCLUDE_DIR, 0, 0 },
395 #endif
396 { STANDARD_INCLUDE_DIR, 0, 0 },
397 #endif /* not CROSS_COMPILE */
398 { 0, 0, 0 }
400 #endif /* no INCLUDE_DEFAULTS */
402 /* `struct directive' defines one #-directive, including how to handle it. */
404 struct directive {
405 int length; /* Length of name */
406 int (*func)(); /* Function to handle directive */
407 char *name; /* Name of directive */
408 enum node_type type; /* Code which describes which directive. */
409 char command_reads_line; /* One if rest of line is read by func. */
410 char traditional_comments; /* Nonzero: keep comments if -traditional. */
411 char pass_thru; /* Copy preprocessed directive to output file.*/
414 /* Here is the actual list of #-directives, most-often-used first.
415 The initialize_builtins function assumes #define is the very first. */
417 static struct directive directive_table[] = {
418 { 6, do_define, "define", T_DEFINE, 0, 1},
419 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
420 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
421 { 7, do_include, "include", T_INCLUDE, 1},
422 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
423 { 6, do_include, "import", T_IMPORT, 1},
424 { 5, do_endif, "endif", T_ENDIF, 1},
425 { 4, do_else, "else", T_ELSE, 1},
426 { 2, do_if, "if", T_IF, 1},
427 { 4, do_elif, "elif", T_ELIF, 1},
428 { 5, do_undef, "undef", T_UNDEF},
429 { 5, do_error, "error", T_ERROR},
430 { 7, do_warning, "warning", T_WARNING},
431 { 6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
432 { 4, do_line, "line", T_LINE, 1},
433 { 5, do_ident, "ident", T_IDENT, 1, 0, 1},
434 #ifdef SCCS_DIRECTIVE
435 { 4, do_sccs, "sccs", T_SCCS},
436 #endif
437 { 6, do_assert, "assert", T_ASSERT, 1},
438 { 8, do_unassert, "unassert", T_UNASSERT, 1},
439 { -1, 0, "", T_UNUSED},
442 /* table to tell if char can be part of a C identifier. */
443 U_CHAR is_idchar[256];
444 /* table to tell if char can be first char of a c identifier. */
445 U_CHAR is_idstart[256];
446 /* table to tell if c is horizontal space. */
447 U_CHAR is_hor_space[256];
448 /* table to tell if c is horizontal or vertical space. */
449 static U_CHAR is_space[256];
451 /* Initialize syntactic classifications of characters. */
453 static void
454 initialize_char_syntax (opts)
455 struct cpp_options *opts;
457 register int i;
460 * Set up is_idchar and is_idstart tables. These should be
461 * faster than saying (is_alpha (c) || c == '_'), etc.
462 * Set up these things before calling any routines tthat
463 * refer to them.
465 for (i = 'a'; i <= 'z'; i++) {
466 is_idchar[i - 'a' + 'A'] = 1;
467 is_idchar[i] = 1;
468 is_idstart[i - 'a' + 'A'] = 1;
469 is_idstart[i] = 1;
471 for (i = '0'; i <= '9'; i++)
472 is_idchar[i] = 1;
473 is_idchar['_'] = 1;
474 is_idstart['_'] = 1;
475 is_idchar['$'] = opts->dollars_in_ident;
476 is_idstart['$'] = opts->dollars_in_ident;
478 /* horizontal space table */
479 is_hor_space[' '] = 1;
480 is_hor_space['\t'] = 1;
481 is_hor_space['\v'] = 1;
482 is_hor_space['\f'] = 1;
483 is_hor_space['\r'] = 1;
485 is_space[' '] = 1;
486 is_space['\t'] = 1;
487 is_space['\v'] = 1;
488 is_space['\f'] = 1;
489 is_space['\n'] = 1;
490 is_space['\r'] = 1;
494 /* Place into PFILE a quoted string representing the string SRC.
495 Caller must reserve enough space in pfile->token_buffer. */
497 static void
498 quote_string (pfile, src)
499 cpp_reader *pfile;
500 char *src;
502 U_CHAR c;
504 CPP_PUTC_Q (pfile, '\"');
505 for (;;)
506 switch ((c = *src++))
508 default:
509 if (isprint (c))
510 CPP_PUTC_Q (pfile, c);
511 else
513 sprintf (CPP_PWRITTEN (pfile), "\\%03o", c);
514 CPP_ADJUST_WRITTEN (pfile, 4);
516 break;
518 case '\"':
519 case '\\':
520 CPP_PUTC_Q (pfile, '\\');
521 CPP_PUTC_Q (pfile, c);
522 break;
524 case '\0':
525 CPP_PUTC_Q (pfile, '\"');
526 CPP_NUL_TERMINATE_Q (pfile);
527 return;
531 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
533 void
534 cpp_grow_buffer (pfile, n)
535 cpp_reader *pfile;
536 long n;
538 long old_written = CPP_WRITTEN (pfile);
539 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
540 pfile->token_buffer = (U_CHAR *)
541 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
542 CPP_SET_WRITTEN (pfile, old_written);
547 * process a given definition string, for initialization
548 * If STR is just an identifier, define it with value 1.
549 * If STR has anything after the identifier, then it should
550 * be identifier=definition.
553 void
554 cpp_define (pfile, str)
555 cpp_reader *pfile;
556 U_CHAR *str;
558 U_CHAR *buf, *p;
560 buf = str;
561 p = str;
562 if (!is_idstart[*p])
564 cpp_error (pfile, "malformed option `-D %s'", str);
565 return;
567 while (is_idchar[*++p])
569 if (*p == 0)
571 buf = (U_CHAR *) alloca (p - buf + 4);
572 strcpy ((char *)buf, str);
573 strcat ((char *)buf, " 1");
575 else if (*p != '=')
577 cpp_error (pfile, "malformed option `-D %s'", str);
578 return;
580 else
582 U_CHAR *q;
583 /* Copy the entire option so we can modify it. */
584 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
585 strncpy (buf, str, p - str);
586 /* Change the = to a space. */
587 buf[p - str] = ' ';
588 /* Scan for any backslash-newline and remove it. */
589 p++;
590 q = &buf[p - str];
591 while (*p)
593 if (*p == '\\' && p[1] == '\n')
594 p += 2;
595 else
596 *q++ = *p++;
598 *q = 0;
601 do_define (pfile, NULL, buf, buf + strlen (buf));
604 /* Process the string STR as if it appeared as the body of a #assert.
605 OPTION is the option name for which STR was the argument. */
607 static void
608 make_assertion (pfile, option, str)
609 cpp_reader *pfile;
610 char *option;
611 U_CHAR *str;
613 struct directive *kt;
614 U_CHAR *buf, *p, *q;
616 /* Copy the entire option so we can modify it. */
617 buf = (U_CHAR *) alloca (strlen (str) + 1);
618 strcpy ((char *) buf, str);
619 /* Scan for any backslash-newline and remove it. */
620 p = q = buf;
621 while (*p) {
622 #if 0
623 if (*p == '\\' && p[1] == '\n')
624 p += 2;
625 else
626 #endif
627 *q++ = *p++;
629 *q = 0;
631 p = buf;
632 if (!is_idstart[*p]) {
633 cpp_error (pfile, "malformed option `%s %s'", option, str);
634 return;
636 while (is_idchar[*++p])
638 while (*p == ' ' || *p == '\t') p++;
639 if (! (*p == 0 || *p == '(')) {
640 cpp_error (pfile, "malformed option `%s %s'", option, str);
641 return;
644 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
646 do_assert (pfile, NULL, NULL, NULL);
647 cpp_pop_buffer (pfile);
651 /* Append a chain of `struct file_name_list's
652 to the end of the main include chain.
653 FIRST is the beginning of the chain to append, and LAST is the end. */
655 static void
656 append_include_chain (pfile, first, last)
657 cpp_reader *pfile;
658 struct file_name_list *first, *last;
660 struct cpp_options *opts = CPP_OPTIONS (pfile);
661 struct file_name_list *dir;
663 if (!first || !last)
664 return;
666 if (opts->include == 0)
667 opts->include = first;
668 else
669 opts->last_include->next = first;
671 if (opts->first_bracket_include == 0)
672 opts->first_bracket_include = first;
674 for (dir = first; ; dir = dir->next) {
675 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
676 if (len > pfile->max_include_len)
677 pfile->max_include_len = len;
678 if (dir == last)
679 break;
682 last->next = NULL;
683 opts->last_include = last;
686 /* Add output to `deps_buffer' for the -M switch.
687 STRING points to the text to be output.
688 SPACER is ':' for targets, ' ' for dependencies, zero for text
689 to be inserted literally. */
691 static void
692 deps_output (pfile, string, spacer)
693 cpp_reader *pfile;
694 char *string;
695 int spacer;
697 int size = strlen (string);
699 if (size == 0)
700 return;
702 #ifndef MAX_OUTPUT_COLUMNS
703 #define MAX_OUTPUT_COLUMNS 72
704 #endif
705 if (spacer
706 && pfile->deps_column > 0
707 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
709 deps_output (pfile, " \\\n ", 0);
710 pfile->deps_column = 0;
713 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
715 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
716 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
717 pfile->deps_allocated_size);
719 if (spacer == ' ' && pfile->deps_column > 0)
720 pfile->deps_buffer[pfile->deps_size++] = ' ';
721 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
722 pfile->deps_size += size;
723 pfile->deps_column += size;
724 if (spacer == ':')
725 pfile->deps_buffer[pfile->deps_size++] = ':';
726 pfile->deps_buffer[pfile->deps_size] = 0;
729 /* Given a colon-separated list of file names PATH,
730 add all the names to the search path for include files. */
732 static void
733 path_include (pfile, path)
734 cpp_reader *pfile;
735 char *path;
737 char *p;
739 p = path;
741 if (*p)
742 while (1) {
743 char *q = p;
744 char *name;
745 struct file_name_list *dirtmp;
747 /* Find the end of this name. */
748 while (*q != 0 && *q != PATH_SEPARATOR) q++;
749 if (p == q) {
750 /* An empty name in the path stands for the current directory. */
751 name = (char *) xmalloc (2);
752 name[0] = '.';
753 name[1] = 0;
754 } else {
755 /* Otherwise use the directory that is named. */
756 name = (char *) xmalloc (q - p + 1);
757 bcopy (p, name, q - p);
758 name[q - p] = 0;
761 dirtmp = (struct file_name_list *)
762 xmalloc (sizeof (struct file_name_list));
763 dirtmp->next = 0; /* New one goes on the end */
764 dirtmp->control_macro = 0;
765 dirtmp->c_system_include_path = 0;
766 dirtmp->fname = name;
767 dirtmp->got_name_map = 0;
768 append_include_chain (pfile, dirtmp, dirtmp);
770 /* Advance past this name. */
771 p = q;
772 if (*p == 0)
773 break;
774 /* Skip the colon. */
775 p++;
779 void
780 cpp_options_init (opts)
781 cpp_options *opts;
783 bzero ((char *) opts, sizeof *opts);
784 opts->in_fname = NULL;
785 opts->out_fname = NULL;
787 /* Initialize is_idchar to allow $. */
788 opts->dollars_in_ident = 1;
789 initialize_char_syntax (opts);
791 opts->no_line_commands = 0;
792 opts->no_trigraphs = 1;
793 opts->put_out_comments = 0;
794 opts->print_include_names = 0;
795 opts->dump_macros = dump_none;
796 opts->no_output = 0;
797 opts->cplusplus = 0;
798 opts->cplusplus_comments = 0;
800 opts->verbose = 0;
801 opts->objc = 0;
802 opts->lang_asm = 0;
803 opts->for_lint = 0;
804 opts->chill = 0;
805 opts->pedantic_errors = 0;
806 opts->inhibit_warnings = 0;
807 opts->warn_comments = 0;
808 opts->warn_import = 1;
809 opts->warnings_are_errors = 0;
812 enum cpp_token
813 null_underflow (pfile)
814 cpp_reader *pfile;
816 return CPP_EOF;
820 null_cleanup (pbuf, pfile)
821 cpp_buffer *pbuf;
822 cpp_reader *pfile;
824 return 0;
828 macro_cleanup (pbuf, pfile)
829 cpp_buffer *pbuf;
830 cpp_reader *pfile;
832 HASHNODE *macro = (HASHNODE *) pbuf->data;
833 if (macro->type == T_DISABLED)
834 macro->type = T_MACRO;
835 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
836 free (pbuf->buf);
837 return 0;
841 file_cleanup (pbuf, pfile)
842 cpp_buffer *pbuf;
843 cpp_reader *pfile;
845 if (pbuf->buf)
847 free (pbuf->buf);
848 pbuf->buf = 0;
850 return 0;
853 /* Assuming we have read '/'.
854 If this is the start of a comment (followed by '*' or '/'),
855 skip to the end of the comment, and return ' '.
856 Return EOF if we reached the end of file before the end of the comment.
857 If not the start of a comment, return '/'. */
859 static int
860 skip_comment (pfile, linep)
861 cpp_reader *pfile;
862 long *linep;
864 int c = 0;
865 while (PEEKC() == '\\' && PEEKN(1) == '\n')
867 if (linep)
868 (*linep)++;
869 FORWARD(2);
871 if (PEEKC() == '*')
873 FORWARD(1);
874 for (;;)
876 int prev_c = c;
877 c = GETC ();
878 if (c == EOF)
879 return EOF;
880 while (c == '\\' && PEEKC() == '\n')
882 if (linep)
883 (*linep)++;
884 FORWARD(1), c = GETC();
886 if (prev_c == '*' && c == '/')
887 return ' ';
888 if (c == '\n' && linep)
889 (*linep)++;
892 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
894 FORWARD(1);
895 for (;;)
897 c = GETC ();
898 if (c == EOF)
899 return ' '; /* Allow // to be terminated by EOF. */
900 while (c == '\\' && PEEKC() == '\n')
902 FORWARD(1);
903 c = GETC();
904 if (linep)
905 (*linep)++;
907 if (c == '\n')
909 /* Don't consider final '\n' to be part of comment. */
910 FORWARD(-1);
911 return ' ';
915 else
916 return '/';
919 /* Skip whitespace \-newline and comments. Does not macro-expand. */
921 void
922 cpp_skip_hspace (pfile)
923 cpp_reader *pfile;
925 while (1)
927 int c = PEEKC();
928 if (c == EOF)
929 return; /* FIXME */
930 if (is_hor_space[c])
932 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
933 cpp_pedwarn (pfile, "%s in preprocessing directive",
934 c == '\f' ? "formfeed" : "vertical tab");
935 FORWARD(1);
937 else if (c == '/')
939 FORWARD (1);
940 c = skip_comment (pfile, NULL);
941 if (c == '/')
942 FORWARD(-1);
943 if (c == EOF || c == '/')
944 return;
946 else if (c == '\\' && PEEKN(1) == '\n') {
947 FORWARD(2);
949 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
950 && is_hor_space[PEEKN(1)])
951 FORWARD(2);
952 else return;
956 /* Read the rest of the current line.
957 The line is appended to PFILE's output buffer. */
959 static void
960 copy_rest_of_line (pfile)
961 cpp_reader *pfile;
963 struct cpp_options *opts = CPP_OPTIONS (pfile);
964 for (;;)
966 int c = GETC();
967 int nextc;
968 switch (c)
970 case EOF:
971 goto end_directive;
972 case '\\':
973 if (PEEKC() == '\n')
975 FORWARD (1);
976 continue;
978 case '\'':
979 case '\"':
980 goto scan_directive_token;
981 break;
982 case '/':
983 nextc = PEEKC();
984 if (nextc == '*' || (opts->cplusplus_comments && nextc == '*'))
985 goto scan_directive_token;
986 break;
987 case '\f':
988 case '\v':
989 if (CPP_PEDANTIC (pfile))
990 cpp_pedwarn (pfile, "%s in preprocessing directive",
991 c == '\f' ? "formfeed" : "vertical tab");
992 break;
994 case '\n':
995 FORWARD(-1);
996 goto end_directive;
997 scan_directive_token:
998 FORWARD(-1);
999 cpp_get_token (pfile);
1000 continue;
1002 CPP_PUTC (pfile, c);
1004 end_directive: ;
1005 CPP_NUL_TERMINATE (pfile);
1008 void
1009 skip_rest_of_line (pfile)
1010 cpp_reader *pfile;
1012 long old = CPP_WRITTEN (pfile);
1013 copy_rest_of_line (pfile);
1014 CPP_SET_WRITTEN (pfile, old);
1017 /* Handle a possible # directive.
1018 '#' has already been read. */
1021 handle_directive (pfile)
1022 cpp_reader *pfile;
1023 { int c;
1024 register struct directive *kt;
1025 int ident_length;
1026 long after_ident;
1027 U_CHAR *ident, *line_end;
1028 long old_written = CPP_WRITTEN (pfile);
1030 cpp_skip_hspace (pfile);
1032 c = PEEKC ();
1033 if (c >= '0' && c <= '9')
1035 /* Handle # followed by a line number. */
1036 if (CPP_PEDANTIC (pfile))
1037 cpp_pedwarn (pfile, "`#' followed by integer");
1038 do_line (pfile, NULL);
1039 goto done_a_directive;
1042 /* Now find the directive name. */
1043 CPP_PUTC (pfile, '#');
1044 parse_name (pfile, GETC());
1045 ident = pfile->token_buffer + old_written + 1;
1046 ident_length = CPP_PWRITTEN (pfile) - ident;
1047 if (ident_length == 0 && PEEKC() == '\n')
1049 /* A line of just `#' becomes blank. */
1050 goto done_a_directive;
1053 #if 0
1054 if (ident_length == 0 || !is_idstart[*ident]) {
1055 U_CHAR *p = ident;
1056 while (is_idchar[*p]) {
1057 if (*p < '0' || *p > '9')
1058 break;
1059 p++;
1061 /* Avoid error for `###' and similar cases unless -pedantic. */
1062 if (p == ident) {
1063 while (*p == '#' || is_hor_space[*p]) p++;
1064 if (*p == '\n') {
1065 if (pedantic && !lang_asm)
1066 cpp_warning (pfile, "invalid preprocessor directive");
1067 return 0;
1071 if (!lang_asm)
1072 cpp_error (pfile, "invalid preprocessor directive name");
1074 return 0;
1076 #endif
1078 * Decode the keyword and call the appropriate expansion
1079 * routine, after moving the input pointer up to the next line.
1081 for (kt = directive_table; ; kt++) {
1082 if (kt->length <= 0)
1083 goto not_a_directive;
1084 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1085 break;
1088 if (kt->command_reads_line)
1089 after_ident = 0;
1090 else
1092 /* Nonzero means do not delete comments within the directive.
1093 #define needs this when -traditional. */
1094 int comments = CPP_TRADITIONAL (pfile) && kt->traditional_comments;
1095 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1096 CPP_OPTIONS (pfile)->put_out_comments = comments;
1097 after_ident = CPP_WRITTEN (pfile);
1098 copy_rest_of_line (pfile);
1099 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1102 /* For #pragma and #define, we may want to pass through the directive.
1103 Other directives may create output, but we don't want the directive
1104 itself out, so we pop it now. For example #include may write a #line
1105 command (see comment in do_include), and conditionals may emit
1106 #failed ... #endfailed stuff. But note that popping the buffer
1107 means the parameters to kt->func may point after pfile->limit
1108 so these parameters are invalid as soon as something gets appended
1109 to the token_buffer. */
1111 line_end = CPP_PWRITTEN (pfile);
1112 if (!kt->pass_thru && kt->type != T_DEFINE)
1113 CPP_SET_WRITTEN (pfile, old_written);
1115 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1116 if (kt->pass_thru
1117 || (kt->type == T_DEFINE
1118 && CPP_OPTIONS (pfile)->dump_macros == dump_definitions))
1120 /* Just leave the entire #define in the output stack. */
1122 else if (kt->type == T_DEFINE
1123 && CPP_OPTIONS (pfile)->dump_macros == dump_names)
1125 U_CHAR *p = pfile->token_buffer + old_written + 7; /* Skip "#define". */
1126 SKIP_WHITE_SPACE (p);
1127 while (is_idchar[*p]) p++;
1128 pfile->limit = p;
1129 CPP_PUTC (pfile, '\n');
1131 else if (kt->type == T_DEFINE)
1132 CPP_SET_WRITTEN (pfile, old_written);
1133 done_a_directive:
1134 return 1;
1136 not_a_directive:
1137 return 0;
1140 /* Pass a directive through to the output file.
1141 BUF points to the contents of the directive, as a contiguous string.
1142 LIMIT points to the first character past the end of the directive.
1143 KEYWORD is the keyword-table entry for the directive. */
1145 static void
1146 pass_thru_directive (buf, limit, pfile, keyword)
1147 U_CHAR *buf, *limit;
1148 cpp_reader *pfile;
1149 struct directive *keyword;
1151 register unsigned keyword_length = keyword->length;
1153 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1154 CPP_PUTC_Q (pfile, '#');
1155 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1156 if (limit != buf && buf[0] != ' ')
1157 CPP_PUTC_Q (pfile, ' ');
1158 CPP_PUTS_Q (pfile, buf, limit - buf);
1159 #if 0
1160 CPP_PUTS_Q (pfile, '\n');
1161 /* Count the line we have just made in the output,
1162 to get in sync properly. */
1163 pfile->lineno++;
1164 #endif
1167 /* The arglist structure is built by do_define to tell
1168 collect_definition where the argument names begin. That
1169 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1170 would contain pointers to the strings x, y, and z.
1171 Collect_definition would then build a DEFINITION node,
1172 with reflist nodes pointing to the places x, y, and z had
1173 appeared. So the arglist is just convenience data passed
1174 between these two routines. It is not kept around after
1175 the current #define has been processed and entered into the
1176 hash table. */
1178 struct arglist {
1179 struct arglist *next;
1180 U_CHAR *name;
1181 int length;
1182 int argno;
1183 char rest_args;
1186 /* Read a replacement list for a macro with parameters.
1187 Build the DEFINITION structure.
1188 Reads characters of text starting at BUF until END.
1189 ARGLIST specifies the formal parameters to look for
1190 in the text of the definition; NARGS is the number of args
1191 in that list, or -1 for a macro name that wants no argument list.
1192 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1193 and NAMELEN is its length in characters.
1195 Note that comments, backslash-newlines, and leading white space
1196 have already been deleted from the argument. */
1198 static DEFINITION *
1199 collect_expansion (pfile, buf, limit, nargs, arglist)
1200 cpp_reader *pfile;
1201 U_CHAR *buf, *limit;
1202 int nargs;
1203 struct arglist *arglist;
1205 DEFINITION *defn;
1206 register U_CHAR *p, *lastp, *exp_p;
1207 struct reflist *endpat = NULL;
1208 /* Pointer to first nonspace after last ## seen. */
1209 U_CHAR *concat = 0;
1210 /* Pointer to first nonspace after last single-# seen. */
1211 U_CHAR *stringify = 0;
1212 int maxsize;
1213 int expected_delimiter = '\0';
1215 /* Scan thru the replacement list, ignoring comments and quoted
1216 strings, picking up on the macro calls. It does a linear search
1217 thru the arg list on every potential symbol. Profiling might say
1218 that something smarter should happen. */
1220 if (limit < buf)
1221 abort ();
1223 /* Find the beginning of the trailing whitespace. */
1224 p = buf;
1225 while (p < limit && is_space[limit[-1]]) limit--;
1227 /* Allocate space for the text in the macro definition.
1228 Leading and trailing whitespace chars need 2 bytes each.
1229 Each other input char may or may not need 1 byte,
1230 so this is an upper bound. The extra 5 are for invented
1231 leading and trailing newline-marker and final null. */
1232 maxsize = (sizeof (DEFINITION)
1233 + (limit - p) + 5);
1234 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1235 while (p < limit)
1236 if (*p++ == '@')
1237 maxsize++;
1238 defn = (DEFINITION *) xcalloc (1, maxsize);
1240 defn->nargs = nargs;
1241 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1242 lastp = exp_p;
1244 p = buf;
1246 /* Add one initial space escape-marker to prevent accidental
1247 token-pasting (often removed by macroexpand). */
1248 *exp_p++ = '@';
1249 *exp_p++ = ' ';
1251 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1252 cpp_error (pfile, "`##' at start of macro definition");
1253 p += 2;
1256 /* Process the main body of the definition. */
1257 while (p < limit) {
1258 int skipped_arg = 0;
1259 register U_CHAR c = *p++;
1261 *exp_p++ = c;
1263 if (!CPP_TRADITIONAL (pfile)) {
1264 switch (c) {
1265 case '\'':
1266 case '\"':
1267 if (expected_delimiter != '\0') {
1268 if (c == expected_delimiter)
1269 expected_delimiter = '\0';
1270 } else
1271 expected_delimiter = c;
1272 break;
1274 case '\\':
1275 if (p < limit && expected_delimiter) {
1276 /* In a string, backslash goes through
1277 and makes next char ordinary. */
1278 *exp_p++ = *p++;
1280 break;
1282 case '@':
1283 /* An '@' in a string or character constant stands for itself,
1284 and does not need to be escaped. */
1285 if (!expected_delimiter)
1286 *exp_p++ = c;
1287 break;
1289 case '#':
1290 /* # is ordinary inside a string. */
1291 if (expected_delimiter)
1292 break;
1293 if (p < limit && *p == '#') {
1294 /* ##: concatenate preceding and following tokens. */
1295 /* Take out the first #, discard preceding whitespace. */
1296 exp_p--;
1297 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1298 --exp_p;
1299 /* Skip the second #. */
1300 p++;
1301 /* Discard following whitespace. */
1302 SKIP_WHITE_SPACE (p);
1303 concat = p;
1304 if (p == limit)
1305 cpp_error (pfile, "`##' at end of macro definition");
1306 } else if (nargs >= 0) {
1307 /* Single #: stringify following argument ref.
1308 Don't leave the # in the expansion. */
1309 exp_p--;
1310 SKIP_WHITE_SPACE (p);
1311 if (p == limit || ! is_idstart[*p]
1312 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1313 cpp_error (pfile,
1314 "`#' operator is not followed by a macro argument name");
1315 else
1316 stringify = p;
1318 break;
1320 } else {
1321 /* In -traditional mode, recognize arguments inside strings and
1322 and character constants, and ignore special properties of #.
1323 Arguments inside strings are considered "stringified", but no
1324 extra quote marks are supplied. */
1325 switch (c) {
1326 case '\'':
1327 case '\"':
1328 if (expected_delimiter != '\0') {
1329 if (c == expected_delimiter)
1330 expected_delimiter = '\0';
1331 } else
1332 expected_delimiter = c;
1333 break;
1335 case '\\':
1336 /* Backslash quotes delimiters and itself, but not macro args. */
1337 if (expected_delimiter != 0 && p < limit
1338 && (*p == expected_delimiter || *p == '\\')) {
1339 *exp_p++ = *p++;
1340 continue;
1342 break;
1344 case '/':
1345 if (expected_delimiter != '\0') /* No comments inside strings. */
1346 break;
1347 if (*p == '*') {
1348 /* If we find a comment that wasn't removed by handle_directive,
1349 this must be -traditional. So replace the comment with
1350 nothing at all. */
1351 exp_p--;
1352 p += 1;
1353 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1354 p++;
1355 #if 0
1356 /* Mark this as a concatenation-point, as if it had been ##. */
1357 concat = p;
1358 #endif
1360 break;
1364 /* Handle the start of a symbol. */
1365 if (is_idchar[c] && nargs > 0) {
1366 U_CHAR *id_beg = p - 1;
1367 int id_len;
1369 --exp_p;
1370 while (p != limit && is_idchar[*p]) p++;
1371 id_len = p - id_beg;
1373 if (is_idstart[c]
1374 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1375 register struct arglist *arg;
1377 for (arg = arglist; arg != NULL; arg = arg->next) {
1378 struct reflist *tpat;
1380 if (arg->name[0] == c
1381 && arg->length == id_len
1382 && strncmp (arg->name, id_beg, id_len) == 0) {
1383 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1384 if (CPP_TRADITIONAL (pfile)) {
1385 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1386 id_len, arg->name);
1387 } else {
1388 cpp_warning (pfile,
1389 "macro arg `%.*s' would be stringified with -traditional.",
1390 id_len, arg->name);
1393 /* If ANSI, don't actually substitute inside a string. */
1394 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1395 break;
1396 /* make a pat node for this arg and append it to the end of
1397 the pat list */
1398 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1399 tpat->next = NULL;
1400 tpat->raw_before = concat == id_beg;
1401 tpat->raw_after = 0;
1402 tpat->rest_args = arg->rest_args;
1403 tpat->stringify = (CPP_TRADITIONAL (pfile)
1404 ? expected_delimiter != '\0'
1405 : stringify == id_beg);
1407 if (endpat == NULL)
1408 defn->pattern = tpat;
1409 else
1410 endpat->next = tpat;
1411 endpat = tpat;
1413 tpat->argno = arg->argno;
1414 tpat->nchars = exp_p - lastp;
1416 register U_CHAR *p1 = p;
1417 SKIP_WHITE_SPACE (p1);
1418 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1419 tpat->raw_after = 1;
1421 lastp = exp_p; /* place to start copying from next time */
1422 skipped_arg = 1;
1423 break;
1428 /* If this was not a macro arg, copy it into the expansion. */
1429 if (! skipped_arg) {
1430 register U_CHAR *lim1 = p;
1431 p = id_beg;
1432 while (p != lim1)
1433 *exp_p++ = *p++;
1434 if (stringify == id_beg)
1435 cpp_error (pfile,
1436 "`#' operator should be followed by a macro argument name");
1441 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1443 /* If ANSI, put in a "@ " marker to prevent token pasting.
1444 But not if "inside a string" (which in ANSI mode
1445 happens only for -D option). */
1446 *exp_p++ = '@';
1447 *exp_p++ = ' ';
1450 *exp_p = '\0';
1452 defn->length = exp_p - defn->expansion;
1454 /* Crash now if we overrun the allocated size. */
1455 if (defn->length + 1 > maxsize)
1456 abort ();
1458 #if 0
1459 /* This isn't worth the time it takes. */
1460 /* give back excess storage */
1461 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1462 #endif
1464 return defn;
1468 * special extension string that can be added to the last macro argument to
1469 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1470 * #define wow(a, b...) process (b, a, b)
1471 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1472 * { wow (one, two); } -> { process (two, one, two); }
1473 * if this "rest_arg" is used with the concat token '##' and if it is not
1474 * supplied then the token attached to with ## will not be outputted. Ex:
1475 * #define wow (a, b...) process (b ## , a, ## b)
1476 * { wow (1, 2); } -> { process (2, 1, 2); }
1477 * { wow (one); } -> { process (one); {
1479 static char rest_extension[] = "...";
1480 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1482 /* Create a DEFINITION node from a #define directive. Arguments are
1483 as for do_define. */
1485 static MACRODEF
1486 create_definition (buf, limit, pfile, predefinition)
1487 U_CHAR *buf, *limit;
1488 cpp_reader *pfile;
1489 int predefinition;
1491 U_CHAR *bp; /* temp ptr into input buffer */
1492 U_CHAR *symname; /* remember where symbol name starts */
1493 int sym_length; /* and how long it is */
1494 int rest_args = 0;
1495 long line, col;
1496 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1497 DEFINITION *defn;
1498 int arglengths = 0; /* Accumulate lengths of arg names
1499 plus number of args. */
1500 MACRODEF mdef;
1501 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1503 bp = buf;
1505 while (is_hor_space[*bp])
1506 bp++;
1508 symname = bp; /* remember where it starts */
1510 sym_length = check_macro_name (pfile, bp, "macro");
1511 bp += sym_length;
1513 /* Lossage will occur if identifiers or control keywords are broken
1514 across lines using backslash. This is not the right place to take
1515 care of that. */
1517 if (*bp == '(') {
1518 struct arglist *arg_ptrs = NULL;
1519 int argno = 0;
1521 bp++; /* skip '(' */
1522 SKIP_WHITE_SPACE (bp);
1524 /* Loop over macro argument names. */
1525 while (*bp != ')') {
1526 struct arglist *temp;
1528 temp = (struct arglist *) alloca (sizeof (struct arglist));
1529 temp->name = bp;
1530 temp->next = arg_ptrs;
1531 temp->argno = argno++;
1532 temp->rest_args = 0;
1533 arg_ptrs = temp;
1535 if (rest_args)
1536 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1538 if (!is_idstart[*bp])
1539 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1541 /* Find the end of the arg name. */
1542 while (is_idchar[*bp]) {
1543 bp++;
1544 /* do we have a "special" rest-args extension here? */
1545 if (limit - bp > REST_EXTENSION_LENGTH
1546 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1547 rest_args = 1;
1548 temp->rest_args = 1;
1549 break;
1552 temp->length = bp - temp->name;
1553 if (rest_args == 1)
1554 bp += REST_EXTENSION_LENGTH;
1555 arglengths += temp->length + 2;
1556 SKIP_WHITE_SPACE (bp);
1557 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1558 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1559 goto nope;
1561 if (*bp == ',') {
1562 bp++;
1563 SKIP_WHITE_SPACE (bp);
1565 if (bp >= limit) {
1566 cpp_error (pfile, "unterminated parameter list in `#define'");
1567 goto nope;
1570 struct arglist *otemp;
1572 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1573 if (temp->length == otemp->length
1574 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1575 U_CHAR *name;
1577 name = (U_CHAR *) alloca (temp->length + 1);
1578 (void) strncpy (name, temp->name, temp->length);
1579 name[temp->length] = '\0';
1580 cpp_error (pfile,
1581 "duplicate argument name `%s' in `#define'", name);
1582 goto nope;
1587 ++bp; /* skip paren */
1588 SKIP_WHITE_SPACE (bp);
1589 /* now everything from bp before limit is the definition. */
1590 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1591 defn->rest_args = rest_args;
1593 /* Now set defn->args.argnames to the result of concatenating
1594 the argument names in reverse order
1595 with comma-space between them. */
1596 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1598 struct arglist *temp;
1599 int i = 0;
1600 for (temp = arg_ptrs; temp; temp = temp->next) {
1601 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1602 i += temp->length;
1603 if (temp->next != 0) {
1604 defn->args.argnames[i++] = ',';
1605 defn->args.argnames[i++] = ' ';
1608 defn->args.argnames[i] = 0;
1610 } else {
1611 /* Simple expansion or empty definition. */
1613 if (bp < limit)
1615 if (is_hor_space[*bp]) {
1616 bp++;
1617 SKIP_WHITE_SPACE (bp);
1618 } else {
1619 switch (*bp) {
1620 case '!': case '"': case '#': case '%': case '&': case '\'':
1621 case ')': case '*': case '+': case ',': case '-': case '.':
1622 case '/': case ':': case ';': case '<': case '=': case '>':
1623 case '?': case '[': case '\\': case ']': case '^': case '{':
1624 case '|': case '}': case '~':
1625 cpp_warning (pfile, "missing white space after `#define %.*s'",
1626 sym_length, symname);
1627 break;
1629 default:
1630 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1631 sym_length, symname);
1632 break;
1636 /* now everything from bp before limit is the definition. */
1637 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1638 defn->args.argnames = (U_CHAR *) "";
1641 defn->line = line;
1642 defn->file = file;
1644 /* OP is null if this is a predefinition */
1645 defn->predefined = predefinition;
1646 mdef.defn = defn;
1647 mdef.symnam = symname;
1648 mdef.symlen = sym_length;
1650 return mdef;
1652 nope:
1653 mdef.defn = 0;
1654 return mdef;
1657 /* Check a purported macro name SYMNAME, and yield its length.
1658 USAGE is the kind of name this is intended for. */
1660 static int
1661 check_macro_name (pfile, symname, usage)
1662 cpp_reader *pfile;
1663 U_CHAR *symname;
1664 char *usage;
1666 U_CHAR *p;
1667 int sym_length;
1669 for (p = symname; is_idchar[*p]; p++)
1671 sym_length = p - symname;
1672 if (sym_length == 0
1673 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1674 cpp_error (pfile, "invalid %s name", usage);
1675 else if (!is_idstart[*symname]) {
1676 U_CHAR *msg; /* what pain... */
1677 msg = (U_CHAR *) alloca (sym_length + 1);
1678 bcopy (symname, msg, sym_length);
1679 msg[sym_length] = 0;
1680 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1681 } else {
1682 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1683 cpp_error (pfile, "invalid %s name `defined'", usage);
1685 return sym_length;
1688 /* Return zero if two DEFINITIONs are isomorphic. */
1690 static int
1691 compare_defs (d1, d2)
1692 DEFINITION *d1, *d2;
1694 register struct reflist *a1, *a2;
1695 register U_CHAR *p1 = d1->expansion;
1696 register U_CHAR *p2 = d2->expansion;
1697 int first = 1;
1699 if (d1->nargs != d2->nargs)
1700 return 1;
1701 if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1702 return 1;
1703 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1704 a1 = a1->next, a2 = a2->next) {
1705 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1706 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1707 || a1->argno != a2->argno
1708 || a1->stringify != a2->stringify
1709 || a1->raw_before != a2->raw_before
1710 || a1->raw_after != a2->raw_after)
1711 return 1;
1712 first = 0;
1713 p1 += a1->nchars;
1714 p2 += a2->nchars;
1716 if (a1 != a2)
1717 return 1;
1718 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1719 p2, d2->length - (p2 - d2->expansion), 1))
1720 return 1;
1721 return 0;
1724 /* Return 1 if two parts of two macro definitions are effectively different.
1725 One of the parts starts at BEG1 and has LEN1 chars;
1726 the other has LEN2 chars at BEG2.
1727 Any sequence of whitespace matches any other sequence of whitespace.
1728 FIRST means these parts are the first of a macro definition;
1729 so ignore leading whitespace entirely.
1730 LAST means these parts are the last of a macro definition;
1731 so ignore trailing whitespace entirely. */
1733 static int
1734 comp_def_part (first, beg1, len1, beg2, len2, last)
1735 int first;
1736 U_CHAR *beg1, *beg2;
1737 int len1, len2;
1738 int last;
1740 register U_CHAR *end1 = beg1 + len1;
1741 register U_CHAR *end2 = beg2 + len2;
1742 if (first) {
1743 while (beg1 != end1 && is_space[*beg1]) beg1++;
1744 while (beg2 != end2 && is_space[*beg2]) beg2++;
1746 if (last) {
1747 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1748 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1750 while (beg1 != end1 && beg2 != end2) {
1751 if (is_space[*beg1] && is_space[*beg2]) {
1752 while (beg1 != end1 && is_space[*beg1]) beg1++;
1753 while (beg2 != end2 && is_space[*beg2]) beg2++;
1754 } else if (*beg1 == *beg2) {
1755 beg1++; beg2++;
1756 } else break;
1758 return (beg1 != end1) || (beg2 != end2);
1761 /* Process a #define command.
1762 BUF points to the contents of the #define command, as a contiguous string.
1763 LIMIT points to the first character past the end of the definition.
1764 KEYWORD is the keyword-table entry for #define,
1765 or NULL for a "predefined" macro. */
1767 static int
1768 do_define (pfile, keyword, buf, limit)
1769 cpp_reader *pfile;
1770 struct directive *keyword;
1771 U_CHAR *buf, *limit;
1773 int hashcode;
1774 MACRODEF mdef;
1775 HASHNODE *hp;
1777 #if 0
1778 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1779 if (pcp_outfile && keyword)
1780 pass_thru_directive (buf, limit, pfile, keyword);
1781 #endif
1783 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1784 if (mdef.defn == 0)
1785 goto nope;
1787 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1789 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1791 int ok = 0;
1792 /* Redefining a precompiled key is ok. */
1793 if (hp->type == T_PCSTRING)
1794 ok = 1;
1795 /* Redefining a macro is ok if the definitions are the same. */
1796 else if (hp->type == T_MACRO)
1797 ok = ! compare_defs (mdef.defn, hp->value.defn);
1798 /* Redefining a constant is ok with -D. */
1799 else if (hp->type == T_CONST)
1800 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1801 /* Print the warning if it's not ok. */
1802 if (!ok)
1804 U_CHAR *msg; /* what pain... */
1806 /* If we are passing through #define and #undef directives, do
1807 that for this re-definition now. */
1808 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1809 pass_thru_directive (buf, limit, pfile, keyword);
1811 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1812 *msg = '`';
1813 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1814 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1815 cpp_pedwarn (pfile, msg);
1816 if (hp->type == T_MACRO)
1817 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1818 "this is the location of the previous definition");
1820 /* Replace the old definition. */
1821 hp->type = T_MACRO;
1822 hp->value.defn = mdef.defn;
1824 else
1826 /* If we are passing through #define and #undef directives, do
1827 that for this new definition now. */
1828 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1829 pass_thru_directive (buf, limit, pfile, keyword);
1830 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1831 (char *) mdef.defn, hashcode);
1834 return 0;
1836 nope:
1838 return 1;
1841 /* This structure represents one parsed argument in a macro call.
1842 `raw' points to the argument text as written (`raw_length' is its length).
1843 `expanded' points to the argument's macro-expansion
1844 (its length is `expand_length').
1845 `stringified_length' is the length the argument would have
1846 if stringified.
1847 `use_count' is the number of times this macro arg is substituted
1848 into the macro. If the actual use count exceeds 10,
1849 the value stored is 10. */
1851 /* raw and expanded are relative to ARG_BASE */
1852 #define ARG_BASE ((pfile)->token_buffer)
1854 struct argdata {
1855 /* Strings relative to pfile->token_buffer */
1856 long raw, expanded, stringified;
1857 int raw_length, expand_length;
1858 int stringified_length;
1859 char newlines;
1860 char use_count;
1863 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1864 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1865 as the new input buffer.
1866 Return the new buffer, or NULL on failure. */
1868 cpp_buffer *
1869 cpp_push_buffer (pfile, buffer, length)
1870 cpp_reader *pfile;
1871 U_CHAR *buffer;
1872 long length;
1874 register cpp_buffer *buf = CPP_BUFFER (pfile);
1875 if (buf == pfile->buffer_stack)
1877 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1878 buf->fname);
1879 return NULL;
1881 buf--;
1882 bzero ((char *) buf, sizeof (cpp_buffer));
1883 CPP_BUFFER (pfile) = buf;
1884 buf->if_stack = pfile->if_stack;
1885 buf->cleanup = null_cleanup;
1886 buf->underflow = null_underflow;
1887 buf->buf = buf->cur = buffer;
1888 buf->alimit = buf->rlimit = buffer + length;
1890 return buf;
1893 cpp_buffer *
1894 cpp_pop_buffer (pfile)
1895 cpp_reader *pfile;
1897 cpp_buffer *buf = CPP_BUFFER (pfile);
1898 (*buf->cleanup) (buf, pfile);
1899 return ++CPP_BUFFER (pfile);
1902 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1903 Pop the buffer when done. */
1905 void
1906 cpp_scan_buffer (pfile)
1907 cpp_reader *pfile;
1909 cpp_buffer *buffer = CPP_BUFFER (pfile);
1910 for (;;)
1912 enum cpp_token token = cpp_get_token (pfile);
1913 if (token == CPP_EOF) /* Should not happen ... */
1914 break;
1915 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1917 cpp_pop_buffer (pfile);
1918 break;
1924 * Rescan a string (which may have escape marks) into pfile's buffer.
1925 * Place the result in pfile->token_buffer.
1927 * The input is copied before it is scanned, so it is safe to pass
1928 * it something from the token_buffer that will get overwritten
1929 * (because it follows CPP_WRITTEN). This is used by do_include.
1932 static void
1933 cpp_expand_to_buffer (pfile, buf, length)
1934 cpp_reader *pfile;
1935 U_CHAR *buf;
1936 int length;
1938 register cpp_buffer *ip;
1939 cpp_buffer obuf;
1940 U_CHAR *limit = buf + length;
1941 U_CHAR *buf1;
1942 #if 0
1943 int odepth = indepth;
1944 #endif
1946 if (length < 0)
1947 abort ();
1949 /* Set up the input on the input stack. */
1951 buf1 = (U_CHAR *) alloca (length + 1);
1953 register U_CHAR *p1 = buf;
1954 register U_CHAR *p2 = buf1;
1956 while (p1 != limit)
1957 *p2++ = *p1++;
1959 buf1[length] = 0;
1961 ip = cpp_push_buffer (pfile, buf1, length);
1962 if (ip == NULL)
1963 return;
1964 ip->has_escapes = 1;
1965 #if 0
1966 ip->lineno = obuf.lineno = 1;
1967 #endif
1969 /* Scan the input, create the output. */
1970 cpp_scan_buffer (pfile);
1972 #if 0
1973 if (indepth != odepth)
1974 abort ();
1975 #endif
1977 CPP_NUL_TERMINATE (pfile);
1981 static void
1982 adjust_position (buf, limit, linep, colp)
1983 U_CHAR *buf;
1984 U_CHAR *limit;
1985 long *linep;
1986 long *colp;
1988 while (buf < limit)
1990 U_CHAR ch = *buf++;
1991 if (ch == '\n')
1992 (*linep)++, (*colp) = 1;
1993 else
1994 (*colp)++;
1998 /* Move line_base forward, updating lineno and colno. */
2000 static void
2001 update_position (pbuf)
2002 register cpp_buffer *pbuf;
2004 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2005 unsigned char *new_pos = pbuf->cur;
2006 register struct parse_marker *mark;
2007 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2009 if (pbuf->buf + mark->position < new_pos)
2010 new_pos = pbuf->buf + mark->position;
2012 pbuf->line_base += new_pos - old_pos;
2013 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2016 void
2017 cpp_buf_line_and_col (pbuf, linep, colp)
2018 register cpp_buffer *pbuf;
2019 long *linep, *colp;
2021 long dummy;
2022 if (colp == NULL)
2023 colp = &dummy;
2024 if (pbuf)
2026 *linep = pbuf->lineno;
2027 *colp = pbuf->colno;
2028 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2030 else
2032 *linep = 0;
2033 *colp = 0;
2037 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2039 cpp_buffer *
2040 cpp_file_buffer (pfile)
2041 cpp_reader *pfile;
2043 cpp_buffer *ip = CPP_BUFFER (pfile);
2045 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2046 if (ip->fname != NULL)
2047 return ip;
2048 return NULL;
2051 static long
2052 count_newlines (buf, limit)
2053 register U_CHAR *buf;
2054 register U_CHAR *limit;
2056 register long count = 0;
2057 while (buf < limit)
2059 U_CHAR ch = *buf++;
2060 if (ch == '\n')
2061 count++;
2063 return count;
2067 * write out a #line command, for instance, after an #include file.
2068 * If CONDITIONAL is nonzero, we can omit the #line if it would
2069 * appear to be a no-op, and we can output a few newlines instead
2070 * if we want to increase the line number by a small amount.
2071 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2074 static void
2075 output_line_command (pfile, conditional, file_change)
2076 cpp_reader *pfile;
2077 int conditional;
2078 enum file_change_code file_change;
2080 int len;
2081 char *line_cmd_buf, *line_end;
2082 long line, col;
2083 cpp_buffer *ip = CPP_BUFFER (pfile);
2085 if (ip->fname == NULL)
2086 return;
2088 update_position (ip);
2090 if (CPP_OPTIONS (pfile)->no_line_commands
2091 || CPP_OPTIONS (pfile)->no_output)
2092 return;
2094 line = CPP_BUFFER (pfile)->lineno;
2095 col = CPP_BUFFER (pfile)->colno;
2096 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2098 if (CPP_OPTIONS (pfile)->no_line_commands)
2099 return;
2101 if (conditional) {
2102 if (line == pfile->lineno)
2103 return;
2105 /* If the inherited line number is a little too small,
2106 output some newlines instead of a #line command. */
2107 if (line > pfile->lineno && line < pfile->lineno + 8) {
2108 CPP_RESERVE (pfile, 20);
2109 while (line > pfile->lineno) {
2110 CPP_PUTC_Q (pfile, '\n');
2111 pfile->lineno++;
2113 return;
2117 #if 0
2118 /* Don't output a line number of 0 if we can help it. */
2119 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2120 && *ip->bufp == '\n') {
2121 ip->lineno++;
2122 ip->bufp++;
2124 #endif
2126 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2128 #ifdef OUTPUT_LINE_COMMANDS
2129 static char sharp_line[] = "#line ";
2130 #else
2131 static char sharp_line[] = "# ";
2132 #endif
2133 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2136 sprintf (CPP_PWRITTEN (pfile), "%d ", line);
2137 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2139 quote_string (pfile, ip->nominal_fname);
2140 if (file_change != same_file) {
2141 CPP_PUTC_Q (pfile, ' ');
2142 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2144 /* Tell cc1 if following text comes from a system header file. */
2145 if (ip->system_header_p) {
2146 CPP_PUTC_Q (pfile, ' ');
2147 CPP_PUTC_Q (pfile, '3');
2149 #ifndef NO_IMPLICIT_EXTERN_C
2150 /* Tell cc1plus if following text should be treated as C. */
2151 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2152 CPP_PUTC_Q (pfile, ' ');
2153 CPP_PUTC_Q (pfile, '4');
2155 #endif
2156 CPP_PUTC_Q (pfile, '\n');
2157 pfile->lineno = line;
2161 * Parse a macro argument and append the info on PFILE's token_buffer.
2162 * REST_ARGS means to absorb the rest of the args.
2163 * Return nonzero to indicate a syntax error.
2166 static enum cpp_token
2167 macarg (pfile, rest_args)
2168 cpp_reader *pfile;
2169 int rest_args;
2171 int paren = 0;
2172 enum cpp_token token;
2173 long arg_start = CPP_WRITTEN (pfile);
2174 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2175 CPP_OPTIONS (pfile)->put_out_comments = 0;
2177 /* Try to parse as much of the argument as exists at this
2178 input stack level. */
2179 pfile->no_macro_expand++;
2180 for (;;)
2182 token = cpp_get_token (pfile);
2183 switch (token)
2185 case CPP_EOF:
2186 goto done;
2187 case CPP_POP:
2188 /* If we've hit end of file, it's an error (reported by caller).
2189 Ditto if it's the end of cpp_expand_to_buffer text.
2190 If we've hit end of macro, just continue. */
2191 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2192 goto done;
2193 break;
2194 case CPP_LPAREN:
2195 paren++;
2196 break;
2197 case CPP_RPAREN:
2198 if (--paren < 0)
2199 goto found;
2200 break;
2201 case CPP_COMMA:
2202 /* if we've returned to lowest level and
2203 we aren't absorbing all args */
2204 if (paren == 0 && rest_args == 0)
2205 goto found;
2206 break;
2207 found:
2208 /* Remove ',' or ')' from argument buffer. */
2209 CPP_ADJUST_WRITTEN (pfile, -1);
2210 goto done;
2211 default: ;
2215 done:
2216 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2217 pfile->no_macro_expand--;
2219 return token;
2222 /* Turn newlines to spaces in the string of length LENGTH at START,
2223 except inside of string constants.
2224 The string is copied into itself with its beginning staying fixed. */
2226 static int
2227 change_newlines (start, length)
2228 U_CHAR *start;
2229 int length;
2231 register U_CHAR *ibp;
2232 register U_CHAR *obp;
2233 register U_CHAR *limit;
2234 register int c;
2236 ibp = start;
2237 limit = start + length;
2238 obp = start;
2240 while (ibp < limit) {
2241 *obp++ = c = *ibp++;
2242 switch (c) {
2244 case '\'':
2245 case '\"':
2246 /* Notice and skip strings, so that we don't delete newlines in them. */
2248 int quotec = c;
2249 while (ibp < limit) {
2250 *obp++ = c = *ibp++;
2251 if (c == quotec)
2252 break;
2253 if (c == '\n' && quotec == '\'')
2254 break;
2257 break;
2261 return obp - start;
2265 static struct tm *
2266 timestamp (pfile)
2267 cpp_reader *pfile;
2269 if (!pfile->timebuf) {
2270 time_t t = time ((time_t *) 0);
2271 pfile->timebuf = localtime (&t);
2273 return pfile->timebuf;
2276 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2277 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2281 * expand things like __FILE__. Place the expansion into the output
2282 * buffer *without* rescanning.
2285 static void
2286 special_symbol (hp, pfile)
2287 HASHNODE *hp;
2288 cpp_reader *pfile;
2290 char *buf;
2291 int i, len;
2292 int true_indepth;
2293 cpp_buffer *ip = NULL;
2294 struct tm *timebuf;
2296 int paren = 0; /* For special `defined' keyword */
2298 #if 0
2299 if (pcp_outfile && pcp_inside_if
2300 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2301 cpp_error (pfile,
2302 "Predefined macro `%s' used inside `#if' during precompilation",
2303 hp->name);
2304 #endif
2306 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2308 if (ip == CPP_NULL_BUFFER (pfile))
2310 cpp_error (pfile, "cccp error: not in any file?!");
2311 return; /* the show must go on */
2313 if (ip->fname != NULL)
2314 break;
2317 switch (hp->type)
2319 case T_FILE:
2320 case T_BASE_FILE:
2322 char *string;
2323 if (hp->type == T_BASE_FILE)
2325 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2326 ip = CPP_PREV_BUFFER (ip);
2328 string = ip->nominal_fname;
2330 if (!string)
2331 string = "";
2332 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2333 quote_string (pfile, string);
2334 return;
2337 case T_INCLUDE_LEVEL:
2338 true_indepth = 0;
2339 ip = CPP_BUFFER (pfile);
2340 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2341 if (ip->fname != NULL)
2342 true_indepth++;
2344 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2345 sprintf (buf, "%d", true_indepth - 1);
2346 break;
2348 case T_VERSION:
2349 buf = (char *) alloca (3 + strlen (version_string));
2350 sprintf (buf, "\"%s\"", version_string);
2351 break;
2353 #ifndef NO_BUILTIN_SIZE_TYPE
2354 case T_SIZE_TYPE:
2355 buf = SIZE_TYPE;
2356 break;
2357 #endif
2359 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2360 case T_PTRDIFF_TYPE:
2361 buf = PTRDIFF_TYPE;
2362 break;
2363 #endif
2365 case T_WCHAR_TYPE:
2366 buf = CPP_WCHAR_TYPE (pfile);
2367 break;
2369 case T_USER_LABEL_PREFIX_TYPE:
2370 buf = USER_LABEL_PREFIX;
2371 break;
2373 case T_REGISTER_PREFIX_TYPE:
2374 buf = REGISTER_PREFIX;
2375 break;
2377 case T_CONST:
2378 buf = (char *) alloca (4 * sizeof (int));
2379 sprintf (buf, "%d", hp->value.ival);
2380 #if 0
2381 if (pcp_inside_if && pcp_outfile)
2382 /* Output a precondition for this macro use */
2383 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2384 #endif
2385 break;
2387 case T_SPECLINE:
2389 long line = ip->lineno;
2390 long col = ip->colno;
2391 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2393 buf = (char *) alloca (10);
2394 sprintf (buf, "%d", line);
2396 break;
2398 case T_DATE:
2399 case T_TIME:
2400 buf = (char *) alloca (20);
2401 timebuf = timestamp (pfile);
2402 if (hp->type == T_DATE)
2403 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2404 timebuf->tm_mday, timebuf->tm_year + 1900);
2405 else
2406 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2407 timebuf->tm_sec);
2408 break;
2410 case T_SPEC_DEFINED:
2411 buf = " 0 "; /* Assume symbol is not defined */
2412 ip = CPP_BUFFER (pfile);
2413 SKIP_WHITE_SPACE (ip->cur);
2414 if (*ip->cur == '(')
2416 paren++;
2417 ip->cur++; /* Skip over the paren */
2418 SKIP_WHITE_SPACE (ip->cur);
2421 if (!is_idstart[*ip->cur])
2422 goto oops;
2423 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2424 goto oops;
2425 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2427 #if 0
2428 if (pcp_outfile && pcp_inside_if
2429 && (hp->type == T_CONST
2430 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2431 /* Output a precondition for this macro use. */
2432 fprintf (pcp_outfile, "#define %s\n", hp->name);
2433 #endif
2434 buf = " 1 ";
2436 #if 0
2437 else
2438 if (pcp_outfile && pcp_inside_if)
2440 /* Output a precondition for this macro use */
2441 U_CHAR *cp = ip->bufp;
2442 fprintf (pcp_outfile, "#undef ");
2443 while (is_idchar[*cp]) /* Ick! */
2444 fputc (*cp++, pcp_outfile);
2445 putc ('\n', pcp_outfile);
2447 #endif
2448 while (is_idchar[*ip->cur])
2449 ++ip->cur;
2450 SKIP_WHITE_SPACE (ip->cur);
2451 if (paren)
2453 if (*ip->cur != ')')
2454 goto oops;
2455 ++ip->cur;
2457 break;
2459 oops:
2461 cpp_error (pfile, "`defined' without an identifier");
2462 break;
2464 default:
2465 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2466 abort ();
2468 len = strlen (buf);
2469 CPP_RESERVE (pfile, len + 1);
2470 CPP_PUTS_Q (pfile, buf, len);
2471 CPP_NUL_TERMINATE_Q (pfile);
2473 return;
2476 /* Write out a #define command for the special named MACRO_NAME
2477 to PFILE's token_buffer. */
2479 static void
2480 dump_special_to_buffer (pfile, macro_name)
2481 cpp_reader *pfile;
2482 char *macro_name;
2484 static char define_directive[] = "#define ";
2485 int macro_name_length = strlen (macro_name);
2486 output_line_command (pfile, 0, same_file);
2487 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2488 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2489 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2490 CPP_PUTC_Q (pfile, ' ');
2491 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2492 CPP_PUTC (pfile, '\n');
2495 /* Initialize the built-in macros. */
2497 static void
2498 initialize_builtins (pfile)
2499 cpp_reader *pfile;
2501 install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
2502 install ("__DATE__", -1, T_DATE, 0, 0, -1);
2503 install ("__FILE__", -1, T_FILE, 0, 0, -1);
2504 install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2505 install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2506 install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
2507 #ifndef NO_BUILTIN_SIZE_TYPE
2508 install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2509 #endif
2510 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2511 install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2512 #endif
2513 install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2514 install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2515 install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2516 install ("__TIME__", -1, T_TIME, 0, 0, -1);
2517 if (!CPP_TRADITIONAL (pfile))
2518 install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2519 if (CPP_OPTIONS (pfile)->objc)
2520 install ("__OBJC__", -1, T_CONST, 1, 0, -1);
2521 /* This is supplied using a -D by the compiler driver
2522 so that it is present only when truly compiling with GNU C. */
2523 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2525 if (CPP_OPTIONS (pfile)->debug_output)
2527 dump_special_to_buffer (pfile, "__BASE_FILE__");
2528 dump_special_to_buffer (pfile, "__VERSION__");
2529 #ifndef NO_BUILTIN_SIZE_TYPE
2530 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2531 #endif
2532 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2533 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2534 #endif
2535 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2536 dump_special_to_buffer (pfile, "__DATE__");
2537 dump_special_to_buffer (pfile, "__TIME__");
2538 if (!CPP_TRADITIONAL (pfile))
2539 dump_special_to_buffer (pfile, "__STDC__");
2540 if (CPP_OPTIONS (pfile)->objc)
2541 dump_special_to_buffer (pfile, "__OBJC__");
2545 /* Return 1 iff a token ending in C1 followed directly by a token C2
2546 could cause mis-tokenization. */
2548 static int
2549 unsafe_chars (c1, c2)
2550 int c1, c2;
2552 switch (c1)
2554 case '+': case '-':
2555 if (c2 == c1 || c2 == '=')
2556 return 1;
2557 goto letter;
2558 case '.':
2559 case '0': case '1': case '2': case '3': case '4':
2560 case '5': case '6': case '7': case '8': case '9':
2561 case 'e': case 'E': case 'p': case 'P':
2562 if (c2 == '-' || c2 == '+')
2563 return 1; /* could extend a pre-processing number */
2564 goto letter;
2565 case 'L':
2566 if (c2 == '\'' || c2 == '\"')
2567 return 1; /* Could turn into L"xxx" or L'xxx'. */
2568 goto letter;
2569 letter:
2570 case '_':
2571 case 'a': case 'b': case 'c': case 'd': case 'f':
2572 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2573 case 'm': case 'n': case 'o': case 'q': case 'r':
2574 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2575 case 'y': case 'z':
2576 case 'A': case 'B': case 'C': case 'D': case 'F':
2577 case 'G': case 'H': case 'I': case 'J': case 'K':
2578 case 'M': case 'N': case 'O': case 'Q': case 'R':
2579 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2580 case 'Y': case 'Z':
2581 /* We're in the middle of either a name or a pre-processing number. */
2582 return (is_idchar[c2] || c2 == '.');
2583 case '<': case '>': case '!': case '%': case '#': case ':':
2584 case '^': case '&': case '|': case '*': case '/': case '=':
2585 return (c2 == c1 || c2 == '=');
2587 return 0;
2590 /* Expand a macro call.
2591 HP points to the symbol that is the macro being called.
2592 Put the result of expansion onto the input stack
2593 so that subsequent input by our caller will use it.
2595 If macro wants arguments, caller has already verified that
2596 an argument list follows; arguments come from the input stack. */
2598 static void
2599 macroexpand (pfile, hp)
2600 cpp_reader *pfile;
2601 HASHNODE *hp;
2603 int nargs;
2604 DEFINITION *defn = hp->value.defn;
2605 register U_CHAR *xbuf;
2606 long start_line, start_column;
2607 int xbuf_len;
2608 struct argdata *args;
2609 long old_written = CPP_WRITTEN (pfile);
2610 #if 0
2611 int start_line = instack[indepth].lineno;
2612 #endif
2613 int rest_args, rest_zero;
2614 register int i;
2616 #if 0
2617 CHECK_DEPTH (return;);
2618 #endif
2620 #if 0
2621 /* This macro is being used inside a #if, which means it must be */
2622 /* recorded as a precondition. */
2623 if (pcp_inside_if && pcp_outfile && defn->predefined)
2624 dump_single_macro (hp, pcp_outfile);
2625 #endif
2627 pfile->output_escapes++;
2628 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2630 nargs = defn->nargs;
2632 if (nargs >= 0)
2634 enum cpp_token token;
2636 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2638 for (i = 0; i < nargs; i++)
2640 args[i].raw = args[i].expanded = 0;
2641 args[i].raw_length = 0;
2642 args[i].expand_length = args[i].stringified_length = -1;
2643 args[i].use_count = 0;
2646 /* Parse all the macro args that are supplied. I counts them.
2647 The first NARGS args are stored in ARGS.
2648 The rest are discarded. If rest_args is set then we assume
2649 macarg absorbed the rest of the args. */
2650 i = 0;
2651 rest_args = 0;
2652 rest_args = 0;
2653 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2656 if (rest_args)
2657 continue;
2658 if (i < nargs || (nargs == 0 && i == 0))
2660 /* if we are working on last arg which absorbs rest of args... */
2661 if (i == nargs - 1 && defn->rest_args)
2662 rest_args = 1;
2663 args[i].raw = CPP_WRITTEN (pfile);
2664 token = macarg (pfile, rest_args);
2665 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2666 args[i].newlines = 0; /* FIXME */
2668 else
2669 token = macarg (pfile, 0);
2670 if (token == CPP_EOF || token == CPP_POP)
2672 cpp_error_with_line (pfile, start_line, start_column,
2673 "unterminated macro call");
2674 return;
2676 i++;
2677 } while (token == CPP_COMMA);
2679 /* If we got one arg but it was just whitespace, call that 0 args. */
2680 if (i == 1)
2682 register U_CHAR *bp = ARG_BASE + args[0].raw;
2683 register U_CHAR *lim = bp + args[0].raw_length;
2684 /* cpp.texi says for foo ( ) we provide one argument.
2685 However, if foo wants just 0 arguments, treat this as 0. */
2686 if (nargs == 0)
2687 while (bp != lim && is_space[*bp]) bp++;
2688 if (bp == lim)
2689 i = 0;
2692 /* Don't output an error message if we have already output one for
2693 a parse error above. */
2694 rest_zero = 0;
2695 if (nargs == 0 && i > 0)
2697 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2699 else if (i < nargs)
2701 /* traditional C allows foo() if foo wants one argument. */
2702 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2704 /* the rest args token is allowed to absorb 0 tokens */
2705 else if (i == nargs - 1 && defn->rest_args)
2706 rest_zero = 1;
2707 else if (i == 0)
2708 cpp_error (pfile, "macro `%s' used without args", hp->name);
2709 else if (i == 1)
2710 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2711 else
2712 cpp_error (pfile, "macro `%s' used with only %d args",
2713 hp->name, i);
2715 else if (i > nargs)
2717 cpp_error (pfile,
2718 "macro `%s' used with too many (%d) args", hp->name, i);
2722 /* If macro wants zero args, we parsed the arglist for checking only.
2723 Read directly from the macro definition. */
2724 if (nargs <= 0)
2726 xbuf = defn->expansion;
2727 xbuf_len = defn->length;
2729 else
2731 register U_CHAR *exp = defn->expansion;
2732 register int offset; /* offset in expansion,
2733 copied a piece at a time */
2734 register int totlen; /* total amount of exp buffer filled so far */
2736 register struct reflist *ap, *last_ap;
2738 /* Macro really takes args. Compute the expansion of this call. */
2740 /* Compute length in characters of the macro's expansion.
2741 Also count number of times each arg is used. */
2742 xbuf_len = defn->length;
2743 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2745 if (ap->stringify)
2747 register struct argdata *arg = &args[ap->argno];
2748 /* Stringify it it hasn't already been */
2749 if (arg->stringified_length < 0)
2751 int arglen = arg->raw_length;
2752 int escaped = 0;
2753 int in_string = 0;
2754 int c;
2755 /* Initially need_space is -1. Otherwise, 1 means the
2756 previous character was a space, but we suppressed it;
2757 0 means the previous character was a non-space. */
2758 int need_space = -1;
2759 i = 0;
2760 arg->stringified = CPP_WRITTEN (pfile);
2761 if (!CPP_TRADITIONAL (pfile))
2762 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2763 for (; i < arglen; i++)
2765 c = (ARG_BASE + arg->raw)[i];
2767 if (! in_string)
2769 /* Internal sequences of whitespace are replaced by
2770 one space except within an string or char token.*/
2771 if (is_space[c])
2773 if (CPP_WRITTEN (pfile) > arg->stringified
2774 && (CPP_PWRITTEN (pfile))[-1] == '@')
2776 /* "@ " escape markers are removed */
2777 CPP_ADJUST_WRITTEN (pfile, -1);
2778 continue;
2780 if (need_space == 0)
2781 need_space = 1;
2782 continue;
2784 else if (need_space > 0)
2785 CPP_PUTC (pfile, ' ');
2786 need_space = 0;
2789 if (escaped)
2790 escaped = 0;
2791 else
2793 if (c == '\\')
2794 escaped = 1;
2795 if (in_string)
2797 if (c == in_string)
2798 in_string = 0;
2800 else if (c == '\"' || c == '\'')
2801 in_string = c;
2804 /* Escape these chars */
2805 if (c == '\"' || (in_string && c == '\\'))
2806 CPP_PUTC (pfile, '\\');
2807 if (isprint (c))
2808 CPP_PUTC (pfile, c);
2809 else
2811 CPP_RESERVE (pfile, 4);
2812 sprintf (CPP_PWRITTEN (pfile), "\\%03o",
2813 (unsigned int) c);
2814 CPP_ADJUST_WRITTEN (pfile, 4);
2817 if (!CPP_TRADITIONAL (pfile))
2818 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2819 arg->stringified_length
2820 = CPP_WRITTEN (pfile) - arg->stringified;
2822 xbuf_len += args[ap->argno].stringified_length;
2824 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2825 /* Add 4 for two newline-space markers to prevent
2826 token concatenation. */
2827 xbuf_len += args[ap->argno].raw_length + 4;
2828 else
2830 /* We have an ordinary (expanded) occurrence of the arg.
2831 So compute its expansion, if we have not already. */
2832 if (args[ap->argno].expand_length < 0)
2834 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2835 cpp_expand_to_buffer (pfile,
2836 ARG_BASE + args[ap->argno].raw,
2837 args[ap->argno].raw_length);
2839 args[ap->argno].expand_length
2840 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2843 /* Add 4 for two newline-space markers to prevent
2844 token concatenation. */
2845 xbuf_len += args[ap->argno].expand_length + 4;
2847 if (args[ap->argno].use_count < 10)
2848 args[ap->argno].use_count++;
2851 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2853 /* Generate in XBUF the complete expansion
2854 with arguments substituted in.
2855 TOTLEN is the total size generated so far.
2856 OFFSET is the index in the definition
2857 of where we are copying from. */
2858 offset = totlen = 0;
2859 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2860 last_ap = ap, ap = ap->next)
2862 register struct argdata *arg = &args[ap->argno];
2863 int count_before = totlen;
2865 /* Add chars to XBUF. */
2866 for (i = 0; i < ap->nchars; i++, offset++)
2867 xbuf[totlen++] = exp[offset];
2869 /* If followed by an empty rest arg with concatenation,
2870 delete the last run of nonwhite chars. */
2871 if (rest_zero && totlen > count_before
2872 && ((ap->rest_args && ap->raw_before)
2873 || (last_ap != NULL && last_ap->rest_args
2874 && last_ap->raw_after)))
2876 /* Delete final whitespace. */
2877 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2878 totlen--;
2880 /* Delete the nonwhites before them. */
2881 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2882 totlen--;
2885 if (ap->stringify != 0)
2887 bcopy (ARG_BASE + arg->stringified,
2888 xbuf + totlen, arg->stringified_length);
2889 totlen += arg->stringified_length;
2891 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2893 U_CHAR *p1 = ARG_BASE + arg->raw;
2894 U_CHAR *l1 = p1 + arg->raw_length;
2895 if (ap->raw_before)
2897 while (p1 != l1 && is_space[*p1]) p1++;
2898 while (p1 != l1 && is_idchar[*p1])
2899 xbuf[totlen++] = *p1++;
2900 /* Delete any no-reexpansion marker that follows
2901 an identifier at the beginning of the argument
2902 if the argument is concatenated with what precedes it. */
2903 if (p1[0] == '@' && p1[1] == '-')
2904 p1 += 2;
2906 if (ap->raw_after)
2908 /* Arg is concatenated after: delete trailing whitespace,
2909 whitespace markers, and no-reexpansion markers. */
2910 while (p1 != l1)
2912 if (is_space[l1[-1]]) l1--;
2913 else if (l1[-1] == '-')
2915 U_CHAR *p2 = l1 - 1;
2916 /* If a `-' is preceded by an odd number of newlines then it
2917 and the last newline are a no-reexpansion marker. */
2918 while (p2 != p1 && p2[-1] == '\n') p2--;
2919 if ((l1 - 1 - p2) & 1) {
2920 l1 -= 2;
2922 else break;
2924 else break;
2928 bcopy (p1, xbuf + totlen, l1 - p1);
2929 totlen += l1 - p1;
2931 else
2933 U_CHAR *expanded = ARG_BASE + arg->expanded;
2934 if (!ap->raw_before && totlen > 0 && arg->expand_length
2935 && !CPP_TRADITIONAL(pfile)
2936 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2938 xbuf[totlen++] = '@';
2939 xbuf[totlen++] = ' ';
2942 bcopy (expanded, xbuf + totlen, arg->expand_length);
2943 totlen += arg->expand_length;
2945 if (!ap->raw_after && totlen > 0 && offset < defn->length
2946 && !CPP_TRADITIONAL(pfile)
2947 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2949 xbuf[totlen++] = '@';
2950 xbuf[totlen++] = ' ';
2953 /* If a macro argument with newlines is used multiple times,
2954 then only expand the newlines once. This avoids creating
2955 output lines which don't correspond to any input line,
2956 which confuses gdb and gcov. */
2957 if (arg->use_count > 1 && arg->newlines > 0)
2959 /* Don't bother doing change_newlines for subsequent
2960 uses of arg. */
2961 arg->use_count = 1;
2962 arg->expand_length
2963 = change_newlines (expanded, arg->expand_length);
2967 if (totlen > xbuf_len)
2968 abort ();
2971 /* if there is anything left of the definition
2972 after handling the arg list, copy that in too. */
2974 for (i = offset; i < defn->length; i++)
2976 /* if we've reached the end of the macro */
2977 if (exp[i] == ')')
2978 rest_zero = 0;
2979 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2980 && last_ap->raw_after))
2981 xbuf[totlen++] = exp[i];
2984 xbuf[totlen] = 0;
2985 xbuf_len = totlen;
2989 pfile->output_escapes--;
2991 /* Now put the expansion on the input stack
2992 so our caller will commence reading from it. */
2993 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2994 CPP_BUFFER (pfile)->has_escapes = 1;
2996 /* Pop the space we've used in the token_buffer for argument expansion. */
2997 CPP_SET_WRITTEN (pfile, old_written);
2999 /* Recursive macro use sometimes works traditionally.
3000 #define foo(x,y) bar (x (y,0), y)
3001 foo (foo, baz) */
3003 if (!CPP_TRADITIONAL (pfile))
3004 hp->type = T_DISABLED;
3007 static void
3008 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
3009 cpp_reader *pfile;
3010 register U_CHAR *xbuf;
3011 int xbuf_len;
3012 HASHNODE *hp;
3014 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3015 if (mbuf == NULL)
3016 return;
3017 mbuf->cleanup = macro_cleanup;
3018 mbuf->data = hp;
3020 /* The first chars of the expansion should be a "@ " added by
3021 collect_expansion. This is to prevent accidental token-pasting
3022 between the text preceding the macro invocation, and the macro
3023 expansion text.
3025 We would like to avoid adding unneeded spaces (for the sake of
3026 tools that use cpp, such as imake). In some common cases we can
3027 tell that it is safe to omit the space.
3029 The character before the macro invocation cannot have been an
3030 idchar (or else it would have been pasted with the idchars of
3031 the macro name). Therefore, if the first non-space character
3032 of the expansion is an idchar, we do not need the extra space
3033 to prevent token pasting.
3035 Also, we don't need the extra space if the first char is '(',
3036 or some other (less common) characters. */
3038 if (xbuf[0] == '@' && xbuf[1] == ' '
3039 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3040 || xbuf[2] == '\"'))
3041 mbuf->cur += 2;
3044 /* Like cpp_get_token, except that it does not read past end-of-line.
3045 Also, horizontal space is skipped, and macros are popped. */
3047 static enum cpp_token
3048 get_directive_token (pfile)
3049 cpp_reader *pfile;
3051 for (;;)
3053 long old_written = CPP_WRITTEN (pfile);
3054 enum cpp_token token;
3055 cpp_skip_hspace (pfile);
3056 if (PEEKC () == '\n')
3057 return CPP_VSPACE;
3058 token = cpp_get_token (pfile);
3059 switch (token)
3061 case CPP_POP:
3062 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3063 return token;
3064 /* ... else fall though ... */
3065 case CPP_HSPACE: case CPP_COMMENT:
3066 CPP_SET_WRITTEN (pfile, old_written);
3067 break;
3068 default:
3069 return token;
3074 /* Handle #include and #import.
3075 This function expects to see "fname" or <fname> on the input.
3077 The input is normally in part of the output_buffer following
3078 CPP_WRITTEN, and will get overwritten by output_line_command.
3079 I.e. in input file specification has been popped by handle_directive.
3080 This is safe. */
3082 static int
3083 do_include (pfile, keyword, unused1, unused2)
3084 cpp_reader *pfile;
3085 struct directive *keyword;
3086 U_CHAR *unused1, *unused2;
3088 int importing = (keyword->type == T_IMPORT);
3089 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3090 char *fname; /* Dynamically allocated fname buffer */
3091 char *pcftry;
3092 char *pcfname;
3093 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3094 enum cpp_token token;
3096 /* Chain of dirs to search */
3097 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3098 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3099 struct file_name_list *searchptr = 0;
3100 long old_written = CPP_WRITTEN (pfile);
3102 int flen;
3104 int f; /* file number */
3106 int retried = 0; /* Have already tried macro
3107 expanding the include line */
3108 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3109 int pcf = -1;
3110 char *pcfbuf;
3111 char *pcfbuflimit;
3112 int pcfnum;
3113 f= -1; /* JF we iz paranoid! */
3115 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3117 if (importing)
3118 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
3119 if (skip_dirs)
3120 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
3123 if (importing && CPP_OPTIONS (pfile)->warn_import
3124 && !CPP_OPTIONS (pfile)->inhibit_warnings
3125 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3127 pfile->import_warning = 1;
3128 cpp_warning (pfile, "using `#import' is not recommended");
3129 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3130 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3131 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3132 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3133 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3134 fprintf (stderr, " ... <real contents of file> ...\n");
3135 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3136 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3137 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3138 fprintf (stderr, "when it is equipped with such a conditional.\n");
3141 pfile->parsing_include_directive++;
3142 token = get_directive_token (pfile);
3143 pfile->parsing_include_directive--;
3145 if (token == CPP_STRING)
3147 /* FIXME - check no trailing garbage */
3148 fbeg = pfile->token_buffer + old_written + 1;
3149 fend = CPP_PWRITTEN (pfile) - 1;
3150 if (fbeg[-1] == '<')
3152 angle_brackets = 1;
3153 /* If -I-, start with the first -I dir after the -I-. */
3154 if (CPP_OPTIONS (pfile)->first_bracket_include)
3155 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3157 /* If -I- was specified, don't search current dir, only spec'd ones. */
3158 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3160 cpp_buffer *fp = CPP_BUFFER (pfile);
3161 /* We have "filename". Figure out directory this source
3162 file is coming from and put it on the front of the list. */
3164 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3166 int n;
3167 char *ep,*nam;
3169 if ((nam = fp->nominal_fname) != NULL)
3171 /* Found a named file. Figure out dir of the file,
3172 and put it in front of the search list. */
3173 dsp[0].next = search_start;
3174 search_start = dsp;
3175 #ifndef VMS
3176 ep = rindex (nam, '/');
3177 #else /* VMS */
3178 ep = rindex (nam, ']');
3179 if (ep == NULL) ep = rindex (nam, '>');
3180 if (ep == NULL) ep = rindex (nam, ':');
3181 if (ep != NULL) ep++;
3182 #endif /* VMS */
3183 if (ep != NULL)
3185 n = ep - nam;
3186 dsp[0].fname = (char *) alloca (n + 1);
3187 strncpy (dsp[0].fname, nam, n);
3188 dsp[0].fname[n] = '\0';
3189 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3190 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3192 else
3194 dsp[0].fname = 0; /* Current directory */
3196 dsp[0].got_name_map = 0;
3197 break;
3202 #ifdef VMS
3203 else if (token == CPP_NAME)
3206 * Support '#include xyz' like VAX-C to allow for easy use of all the
3207 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3208 * code from case '<' is repeated here) and generates a warning.
3210 cpp_warning (pfile,
3211 "VAX-C-style include specification found, use '#include <filename.h>' !");
3212 angle_brackets = 1;
3213 /* If -I-, start with the first -I dir after the -I-. */
3214 if (CPP_OPTIONS (pfile)->first_bracket_include)
3215 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3216 fbeg = pfile->token_buffer + old_written;
3217 fend = CPP_PWRITTEN (pfile);
3219 #endif
3220 else
3222 cpp_error (pfile,
3223 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3224 CPP_SET_WRITTEN (pfile, old_written);
3225 skip_rest_of_line (pfile);
3226 return 0;
3229 *fend = 0;
3231 token = get_directive_token (pfile);
3232 if (token != CPP_VSPACE)
3234 cpp_error (pfile, "junk at end of `#include'");
3235 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3236 token = get_directive_token (pfile);
3239 /* For #include_next, skip in the search path
3240 past the dir in which the containing file was found. */
3241 if (skip_dirs)
3243 cpp_buffer *fp = CPP_BUFFER (pfile);
3244 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3245 if (fp->fname != NULL)
3247 /* fp->dir is null if the containing file was specified with
3248 an absolute file name. In that case, don't skip anything. */
3249 if (fp->dir == SELF_DIR_DUMMY)
3250 search_start = CPP_OPTIONS (pfile)->include;
3251 else if (fp->dir)
3252 search_start = fp->dir->next;
3253 break;
3257 CPP_SET_WRITTEN (pfile, old_written);
3259 flen = fend - fbeg;
3261 if (flen == 0)
3263 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3264 return 0;
3267 /* Allocate this permanently, because it gets stored in the definitions
3268 of macros. */
3269 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3270 /* + 2 above for slash and terminating null. */
3271 /* + 2 added for '.h' on VMS (to support '#include filename') */
3273 /* If specified file name is absolute, just open it. */
3275 if (*fbeg == '/') {
3276 strncpy (fname, fbeg, flen);
3277 fname[flen] = 0;
3278 if (redundant_include_p (pfile, fname))
3279 return 0;
3280 if (importing)
3281 f = lookup_import (pfile, fname, NULL_PTR);
3282 else
3283 f = open_include_file (pfile, fname, NULL_PTR);
3284 if (f == -2)
3285 return 0; /* Already included this file */
3286 } else {
3287 /* Search directory path, trying to open the file.
3288 Copy each filename tried into FNAME. */
3290 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3291 if (searchptr->fname) {
3292 /* The empty string in a search path is ignored.
3293 This makes it possible to turn off entirely
3294 a standard piece of the list. */
3295 if (searchptr->fname[0] == 0)
3296 continue;
3297 strcpy (fname, searchptr->fname);
3298 strcat (fname, "/");
3299 fname[strlen (fname) + flen] = 0;
3300 } else {
3301 fname[0] = 0;
3303 strncat (fname, fbeg, flen);
3304 #ifdef VMS
3305 /* Change this 1/2 Unix 1/2 VMS file specification into a
3306 full VMS file specification */
3307 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3308 /* Fix up the filename */
3309 hack_vms_include_specification (fname);
3310 } else {
3311 /* This is a normal VMS filespec, so use it unchanged. */
3312 strncpy (fname, fbeg, flen);
3313 fname[flen] = 0;
3314 /* if it's '#include filename', add the missing .h */
3315 if (index(fname,'.')==NULL) {
3316 strcat (fname, ".h");
3319 #endif /* VMS */
3320 /* ??? There are currently 3 separate mechanisms for avoiding processing
3321 of redundant include files: #import, #pragma once, and
3322 redundant_include_p. It would be nice if they were unified. */
3323 if (redundant_include_p (pfile, fname))
3324 return 0;
3325 if (importing)
3326 f = lookup_import (pfile, fname, searchptr);
3327 else
3328 f = open_include_file (pfile, fname, searchptr);
3329 if (f == -2)
3330 return 0; /* Already included this file */
3331 #ifdef EACCES
3332 else if (f == -1 && errno == EACCES)
3333 cpp_warning (pfile, "Header file %s exists, but is not readable",
3334 fname);
3335 #endif
3336 if (f >= 0)
3337 break;
3341 if (f < 0)
3343 /* A file that was not found. */
3344 strncpy (fname, fbeg, flen);
3345 fname[flen] = 0;
3346 /* If generating dependencies and -MG was specified, we assume missing
3347 files are leaf files, living in the same directory as the source file
3348 or other similar place; these missing files may be generated from
3349 other files and may not exist yet (eg: y.tab.h). */
3351 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3352 && CPP_PRINT_DEPS (pfile)
3353 > (angle_brackets || (pfile->system_include_depth > 0)))
3355 /* If it was requested as a system header file,
3356 then assume it belongs in the first place to look for such. */
3357 if (angle_brackets)
3359 for (searchptr = search_start; searchptr;
3360 searchptr = searchptr->next)
3362 if (searchptr->fname)
3364 char *p;
3366 if (searchptr->fname[0] == 0)
3367 continue;
3368 p = (char *) alloca (strlen (searchptr->fname)
3369 + strlen (fname) + 2);
3370 strcpy (p, searchptr->fname);
3371 strcat (p, "/");
3372 strcat (p, fname);
3373 deps_output (pfile, p, ' ');
3374 break;
3378 else
3380 /* Otherwise, omit the directory, as if the file existed
3381 in the directory with the source. */
3382 deps_output (pfile, fname, ' ');
3385 /* If -M was specified, and this header file won't be added to the
3386 dependency list, then don't count this as an error, because we can
3387 still produce correct output. Otherwise, we can't produce correct
3388 output, because there may be dependencies we need inside the missing
3389 file, and we don't know what directory this missing file exists in.*/
3390 else if (CPP_PRINT_DEPS (pfile)
3391 && (CPP_PRINT_DEPS (pfile)
3392 <= (angle_brackets || (pfile->system_include_depth > 0))))
3393 cpp_warning (pfile, "No include path in which to find %s", fname);
3394 else if (search_start)
3395 cpp_error_from_errno (pfile, fname);
3396 else
3397 cpp_error (pfile, "No include path in which to find %s", fname);
3399 else {
3400 /* Check to see if this include file is a once-only include file.
3401 If so, give up. */
3403 struct file_name_list *ptr;
3405 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3406 if (!strcmp (ptr->fname, fname)) {
3407 close (f);
3408 return 0; /* This file was once'd. */
3412 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3413 if (!strcmp (ptr->fname, fname))
3414 break; /* This file was included before. */
3417 if (ptr == 0) {
3418 /* This is the first time for this file. */
3419 /* Add it to list of files included. */
3421 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3422 ptr->control_macro = 0;
3423 ptr->c_system_include_path = 0;
3424 ptr->next = pfile->all_include_files;
3425 pfile->all_include_files = ptr;
3426 ptr->fname = savestring (fname);
3427 ptr->got_name_map = 0;
3429 /* For -M, add this file to the dependencies. */
3430 if (CPP_PRINT_DEPS (pfile)
3431 > (angle_brackets || (pfile->system_include_depth > 0)))
3432 deps_output (pfile, fname, ' ');
3435 /* Handle -H option. */
3436 if (CPP_OPTIONS(pfile)->print_include_names)
3438 cpp_buffer *buf = CPP_BUFFER (pfile);
3439 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3440 putc ('.', stderr);
3441 fprintf (stderr, "%s\n", fname);
3444 if (angle_brackets)
3445 pfile->system_include_depth++;
3447 /* Actually process the file. */
3449 /* Record file on "seen" list for #import. */
3450 add_import (pfile, f, fname);
3452 pcftry = (char *) alloca (strlen (fname) + 30);
3453 pcfbuf = 0;
3454 pcfnum = 0;
3456 #if 0
3457 if (!no_precomp)
3459 struct stat stat_f;
3461 fstat (f, &stat_f);
3463 do {
3464 sprintf (pcftry, "%s%d", fname, pcfnum++);
3466 pcf = open (pcftry, O_RDONLY, 0666);
3467 if (pcf != -1)
3469 struct stat s;
3471 fstat (pcf, &s);
3472 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3473 sizeof (s.st_ino))
3474 || stat_f.st_dev != s.st_dev)
3476 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3477 /* Don't need it any more. */
3478 close (pcf);
3480 else
3482 /* Don't need it at all. */
3483 close (pcf);
3484 break;
3487 } while (pcf != -1 && !pcfbuf);
3489 #endif
3491 /* Actually process the file */
3492 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3493 return 0;
3494 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3495 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3497 output_line_command (pfile, 0, enter_file);
3498 pfile->only_seen_white = 2;
3501 if (angle_brackets)
3502 pfile->system_include_depth--;
3504 return 0;
3507 /* Return nonzero if there is no need to include file NAME
3508 because it has already been included and it contains a conditional
3509 to make a repeated include do nothing. */
3511 static int
3512 redundant_include_p (pfile, name)
3513 cpp_reader *pfile;
3514 char *name;
3516 struct file_name_list *l = pfile->all_include_files;
3517 for (; l; l = l->next)
3518 if (! strcmp (name, l->fname)
3519 && l->control_macro
3520 && cpp_lookup (pfile, l->control_macro, -1, -1))
3521 return 1;
3522 return 0;
3525 /* Return nonzero if the given FILENAME is an absolute pathname which
3526 designates a file within one of the known "system" include file
3527 directories. We assume here that if the given FILENAME looks like
3528 it is the name of a file which resides either directly in a "system"
3529 include file directory, or within any subdirectory thereof, then the
3530 given file must be a "system" include file. This function tells us
3531 if we should suppress pedantic errors/warnings for the given FILENAME.
3533 The value is 2 if the file is a C-language system header file
3534 for which C++ should (on most systems) assume `extern "C"'. */
3536 static int
3537 is_system_include (pfile, filename)
3538 cpp_reader *pfile;
3539 register char *filename;
3541 struct file_name_list *searchptr;
3543 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3544 searchptr = searchptr->next)
3545 if (searchptr->fname) {
3546 register char *sys_dir = searchptr->fname;
3547 register unsigned length = strlen (sys_dir);
3549 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3551 if (searchptr->c_system_include_path)
3552 return 2;
3553 else
3554 return 1;
3557 return 0;
3562 * Install a name in the assertion hash table.
3564 * If LEN is >= 0, it is the length of the name.
3565 * Otherwise, compute the length by scanning the entire name.
3567 * If HASH is >= 0, it is the precomputed hash code.
3568 * Otherwise, compute the hash code.
3571 static ASSERTION_HASHNODE *
3572 assertion_install (pfile, name, len, hash)
3573 cpp_reader *pfile;
3574 U_CHAR *name;
3575 int len;
3576 int hash;
3578 register ASSERTION_HASHNODE *hp;
3579 register int i, bucket;
3580 register U_CHAR *p, *q;
3582 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3583 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3584 bucket = hash;
3585 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3586 hp->next = pfile->assertion_hashtab[bucket];
3587 pfile->assertion_hashtab[bucket] = hp;
3588 hp->prev = NULL;
3589 if (hp->next != NULL)
3590 hp->next->prev = hp;
3591 hp->length = len;
3592 hp->value = 0;
3593 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3594 p = hp->name;
3595 q = name;
3596 for (i = 0; i < len; i++)
3597 *p++ = *q++;
3598 hp->name[len] = 0;
3599 return hp;
3602 * find the most recent hash node for name name (ending with first
3603 * non-identifier char) installed by install
3605 * If LEN is >= 0, it is the length of the name.
3606 * Otherwise, compute the length by scanning the entire name.
3608 * If HASH is >= 0, it is the precomputed hash code.
3609 * Otherwise, compute the hash code.
3612 static ASSERTION_HASHNODE *
3613 assertion_lookup (pfile, name, len, hash)
3614 cpp_reader *pfile;
3615 U_CHAR *name;
3616 int len;
3617 int hash;
3619 register ASSERTION_HASHNODE *bucket;
3621 bucket = pfile->assertion_hashtab[hash];
3622 while (bucket) {
3623 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3624 return bucket;
3625 bucket = bucket->next;
3627 return NULL;
3630 static void
3631 delete_assertion (hp)
3632 ASSERTION_HASHNODE *hp;
3634 struct tokenlist_list *tail;
3635 if (hp->prev != NULL)
3636 hp->prev->next = hp->next;
3637 if (hp->next != NULL)
3638 hp->next->prev = hp->prev;
3640 for (tail = hp->value; tail; )
3642 struct tokenlist_list *next = tail->next;
3643 free_token_list (tail->tokens);
3644 free (tail);
3645 tail = next;
3648 /* Make sure that the bucket chain header that
3649 the deleted guy was on points to the right thing afterwards. */
3650 if (hp == *hp->bucket_hdr)
3651 *hp->bucket_hdr = hp->next;
3653 free (hp);
3656 /* Convert a character string literal into a nul-terminated string.
3657 The input string is [IN ... LIMIT).
3658 The result is placed in RESULT. RESULT can be the same as IN.
3659 The value returned in the end of the string written to RESULT,
3660 or NULL on error. */
3662 static U_CHAR *
3663 convert_string (pfile, result, in, limit, handle_escapes)
3664 cpp_reader *pfile;
3665 register U_CHAR *result, *in, *limit;
3666 int handle_escapes;
3668 U_CHAR c;
3669 c = *in++;
3670 if (c != '\"')
3671 return NULL;
3672 while (in < limit)
3674 U_CHAR c = *in++;
3675 switch (c)
3677 case '\0':
3678 return NULL;
3679 case '\"':
3680 limit = in;
3681 break;
3682 case '\\':
3683 if (handle_escapes)
3685 char *bpc = (char *) in;
3686 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3687 in = (U_CHAR *) bpc;
3688 if (i >= 0)
3689 *result++ = (U_CHAR)c;
3690 break;
3692 /* else fall through */
3693 default:
3694 *result++ = c;
3697 *result = 0;
3698 return result;
3702 * interpret #line command. Remembers previously seen fnames
3703 * in its very own hash table.
3705 #define FNAME_HASHSIZE 37
3707 static int
3708 do_line (pfile, keyword)
3709 cpp_reader *pfile;
3710 struct directive *keyword;
3712 cpp_buffer *ip = CPP_BUFFER (pfile);
3713 int new_lineno;
3714 long old_written = CPP_WRITTEN (pfile);
3715 enum file_change_code file_change = same_file;
3716 enum cpp_token token;
3717 int i;
3719 token = get_directive_token (pfile);
3721 if (token != CPP_NUMBER
3722 || !isdigit(pfile->token_buffer[old_written]))
3724 cpp_error (pfile, "invalid format `#line' command");
3725 goto bad_line_directive;
3728 /* The Newline at the end of this line remains to be processed.
3729 To put the next line at the specified line number,
3730 we must store a line number now that is one less. */
3731 new_lineno = atoi (pfile->token_buffer + old_written) - 1;
3732 CPP_SET_WRITTEN (pfile, old_written);
3734 /* NEW_LINENO is one less than the actual line number here. */
3735 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3736 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3738 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3739 if (PEEKC() && !is_space[PEEKC()]) {
3740 cpp_error (pfile, "invalid format `#line' command");
3741 goto bad_line_directive;
3743 #endif
3745 token = get_directive_token (pfile);
3747 if (token == CPP_STRING) {
3748 U_CHAR *fname = pfile->token_buffer + old_written;
3749 U_CHAR *end_name;
3750 static HASHNODE *fname_table[FNAME_HASHSIZE];
3751 HASHNODE *hp, **hash_bucket;
3752 U_CHAR *p;
3753 long num_start;
3754 int fname_length;
3756 /* Turn the file name, which is a character string literal,
3757 into a null-terminated string. Do this in place. */
3758 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3759 if (end_name == NULL)
3761 cpp_error (pfile, "invalid format `#line' command");
3762 goto bad_line_directive;
3765 fname_length = end_name - fname;
3767 num_start = CPP_WRITTEN (pfile);
3768 token = get_directive_token (pfile);
3769 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3770 p = pfile->token_buffer + num_start;
3771 if (CPP_PEDANTIC (pfile))
3772 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3774 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3776 cpp_error (pfile, "invalid format `#line' command");
3777 goto bad_line_directive;
3779 if (*p == '1')
3780 file_change = enter_file;
3781 else if (*p == 2)
3782 file_change = leave_file;
3783 else if (*p == 3)
3784 ip->system_header_p = 1;
3785 else /* if (*p == 4) */
3786 ip->system_header_p = 2;
3788 CPP_SET_WRITTEN (pfile, num_start);
3789 token = get_directive_token (pfile);
3790 p = pfile->token_buffer + num_start;
3791 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3792 ip->system_header_p = *p == 3 ? 1 : 2;
3793 token = get_directive_token (pfile);
3795 if (token != CPP_VSPACE) {
3796 cpp_error (pfile, "invalid format `#line' command");
3797 goto bad_line_directive;
3801 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3802 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3803 if (hp->length == fname_length
3804 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3805 ip->nominal_fname = hp->value.cpval;
3806 break;
3808 if (hp == 0) {
3809 /* Didn't find it; cons up a new one. */
3810 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3811 hp->next = *hash_bucket;
3812 *hash_bucket = hp;
3814 hp->length = fname_length;
3815 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3816 bcopy (fname, hp->value.cpval, fname_length);
3819 else if (token != CPP_VSPACE && token != CPP_EOF) {
3820 cpp_error (pfile, "invalid format `#line' command");
3821 goto bad_line_directive;
3824 ip->lineno = new_lineno;
3825 bad_line_directive:
3826 skip_rest_of_line (pfile);
3827 CPP_SET_WRITTEN (pfile, old_written);
3828 output_line_command (pfile, 0, file_change);
3829 return 0;
3833 * remove the definition of a symbol from the symbol table.
3834 * according to un*x /lib/cpp, it is not an error to undef
3835 * something that has no definitions, so it isn't one here either.
3838 static int
3839 do_undef (pfile, keyword, buf, limit)
3840 cpp_reader *pfile;
3841 struct directive *keyword;
3842 U_CHAR *buf, *limit;
3844 int sym_length;
3845 HASHNODE *hp;
3846 U_CHAR *orig_buf = buf;
3848 #if 0
3849 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3850 if (pcp_outfile && keyword)
3851 pass_thru_directive (buf, limit, pfile, keyword);
3852 #endif
3854 SKIP_WHITE_SPACE (buf);
3855 sym_length = check_macro_name (pfile, buf, "macro");
3857 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3859 /* If we are generating additional info for debugging (with -g) we
3860 need to pass through all effective #undef commands. */
3861 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3862 pass_thru_directive (orig_buf, limit, pfile, keyword);
3863 if (hp->type != T_MACRO)
3864 cpp_warning (pfile, "undefining `%s'", hp->name);
3865 delete_macro (hp);
3868 if (CPP_PEDANTIC (pfile)) {
3869 buf += sym_length;
3870 SKIP_WHITE_SPACE (buf);
3871 if (buf != limit)
3872 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3874 return 0;
3878 * Report an error detected by the program we are processing.
3879 * Use the text of the line in the error message.
3880 * (We use error because it prints the filename & line#.)
3883 static int
3884 do_error (pfile, keyword, buf, limit)
3885 cpp_reader *pfile;
3886 struct directive *keyword;
3887 U_CHAR *buf, *limit;
3889 int length = limit - buf;
3890 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3891 bcopy (buf, copy, length);
3892 copy[length] = 0;
3893 SKIP_WHITE_SPACE (copy);
3894 cpp_error (pfile, "#error %s", copy);
3895 return 0;
3899 * Report a warning detected by the program we are processing.
3900 * Use the text of the line in the warning message, then continue.
3901 * (We use error because it prints the filename & line#.)
3904 static int
3905 do_warning (pfile, keyword, buf, limit)
3906 cpp_reader *pfile;
3907 struct directive *keyword;
3908 U_CHAR *buf, *limit;
3910 int length = limit - buf;
3911 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3912 bcopy (buf, copy, length);
3913 copy[length] = 0;
3914 SKIP_WHITE_SPACE (copy);
3915 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3916 if -pedantic-errors is given, #warning should cause an error. */
3917 cpp_pedwarn (pfile, "#warning %s", copy);
3918 return 0;
3921 /* Remember the name of the current file being read from so that we can
3922 avoid ever including it again. */
3924 static int
3925 do_once (pfile)
3926 cpp_reader *pfile;
3928 cpp_buffer *ip = NULL;
3929 struct file_name_list *new;
3931 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3933 if (ip == CPP_NULL_BUFFER (pfile))
3934 return 0;
3935 if (ip->fname != NULL)
3936 break;
3940 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3941 new->next = pfile->dont_repeat_files;
3942 pfile->dont_repeat_files = new;
3943 new->fname = savestring (ip->fname);
3944 new->control_macro = 0;
3945 new->got_name_map = 0;
3946 new->c_system_include_path = 0;
3948 return 0;
3951 /* #ident has already been copied to the output file, so just ignore it. */
3953 static int
3954 do_ident (pfile, keyword, buf, limit)
3955 cpp_reader *pfile;
3956 struct directive *keyword;
3957 U_CHAR *buf, *limit;
3959 /* long old_written = CPP_WRITTEN (pfile);*/
3960 int len;
3962 /* Allow #ident in system headers, since that's not user's fault. */
3963 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3964 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3966 /* Leave rest of line to be read by later calls to cpp_get_token. */
3968 return 0;
3971 /* #pragma and its argument line have already been copied to the output file.
3972 Just check for some recognized pragmas that need validation here. */
3974 static int
3975 do_pragma (pfile, keyword, buf, limit)
3976 cpp_reader *pfile;
3977 struct directive *keyword;
3978 U_CHAR *buf, *limit;
3980 while (*buf == ' ' || *buf == '\t')
3981 buf++;
3982 if (!strncmp (buf, "once", 4)) {
3983 /* Allow #pragma once in system headers, since that's not the user's
3984 fault. */
3985 if (!CPP_BUFFER (pfile)->system_header_p)
3986 cpp_warning (pfile, "`#pragma once' is obsolete");
3987 do_once (pfile);
3990 if (!strncmp (buf, "implementation", 14)) {
3991 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3992 been included yet. */
3993 struct file_name_list *ptr;
3994 U_CHAR *p = buf + 14, *fname, *inc_fname;
3995 int fname_len;
3996 SKIP_WHITE_SPACE (p);
3997 if (*p == '\n' || *p != '\"')
3998 return 0;
4000 fname = p + 1;
4001 p = (U_CHAR *) index (fname, '\"');
4002 fname_len = p != NULL ? p - fname : strlen (fname);
4004 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4005 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4006 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4007 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4008 cpp_warning (pfile,
4009 "`#pragma implementation' for `%s' appears after file is included",
4010 fname);
4014 return 0;
4017 #if 0
4018 /* This was a fun hack, but #pragma seems to start to be useful.
4019 By failing to recognize it, we pass it through unchanged to cc1. */
4022 * the behavior of the #pragma directive is implementation defined.
4023 * this implementation defines it as follows.
4026 static int
4027 do_pragma ()
4029 close (0);
4030 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4031 goto nope;
4032 close (1);
4033 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4034 goto nope;
4035 execl ("/usr/games/hack", "#pragma", 0);
4036 execl ("/usr/games/rogue", "#pragma", 0);
4037 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4038 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4039 nope:
4040 fatal ("You are in a maze of twisty compiler features, all different");
4042 #endif
4044 /* Just ignore #sccs, on systems where we define it at all. */
4046 static int
4047 do_sccs (pfile, keyword, buf, limit)
4048 cpp_reader *pfile;
4049 struct directive *keyword;
4050 U_CHAR *buf, *limit;
4052 if (CPP_PEDANTIC (pfile))
4053 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4054 return 0;
4058 * handle #if command by
4059 * 1) inserting special `defined' keyword into the hash table
4060 * that gets turned into 0 or 1 by special_symbol (thus,
4061 * if the luser has a symbol called `defined' already, it won't
4062 * work inside the #if command)
4063 * 2) rescan the input into a temporary output buffer
4064 * 3) pass the output buffer to the yacc parser and collect a value
4065 * 4) clean up the mess left from steps 1 and 2.
4066 * 5) call conditional_skip to skip til the next #endif (etc.),
4067 * or not, depending on the value from step 3.
4070 static int
4071 do_if (pfile, keyword, buf, limit)
4072 cpp_reader *pfile;
4073 struct directive *keyword;
4074 U_CHAR *buf, *limit;
4076 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4077 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4078 return 0;
4082 * handle a #elif directive by not changing if_stack either.
4083 * see the comment above do_else.
4086 static int
4087 do_elif (pfile, keyword, buf, limit)
4088 cpp_reader *pfile;
4089 struct directive *keyword;
4090 U_CHAR *buf, *limit;
4092 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4093 cpp_error (pfile, "`#elif' not within a conditional");
4094 return 0;
4095 } else {
4096 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4097 cpp_error (pfile, "`#elif' after `#else'");
4098 #if 0
4099 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4100 #endif
4101 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4102 && strcmp (pfile->if_stack->fname,
4103 CPP_BUFFER (pfile)->nominal_fname) != 0)
4104 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4105 fprintf (stderr, ")\n");
4107 pfile->if_stack->type = T_ELIF;
4110 if (pfile->if_stack->if_succeeded)
4111 skip_if_group (pfile, 0);
4112 else {
4113 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4114 if (value == 0)
4115 skip_if_group (pfile, 0);
4116 else {
4117 ++pfile->if_stack->if_succeeded; /* continue processing input */
4118 output_line_command (pfile, 1, same_file);
4121 return 0;
4125 * evaluate a #if expression in BUF, of length LENGTH,
4126 * then parse the result as a C expression and return the value as an int.
4129 static HOST_WIDE_INT
4130 eval_if_expression (pfile, buf, length)
4131 cpp_reader *pfile;
4132 U_CHAR *buf;
4133 int length;
4135 HASHNODE *save_defined;
4136 HOST_WIDE_INT value;
4137 long old_written = CPP_WRITTEN (pfile);
4139 save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4140 pfile->pcp_inside_if = 1;
4142 value = cpp_parse_expr (pfile);
4143 pfile->pcp_inside_if = 0;
4144 delete_macro (save_defined); /* clean up special symbol */
4146 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4148 return value;
4152 * routine to handle ifdef/ifndef. Try to look up the symbol,
4153 * then do or don't skip to the #endif/#else/#elif depending
4154 * on what directive is actually being processed.
4157 static int
4158 do_xifdef (pfile, keyword, unused1, unused2)
4159 cpp_reader *pfile;
4160 struct directive *keyword;
4161 U_CHAR *unused1, *unused2;
4163 int skip;
4164 cpp_buffer *ip = CPP_BUFFER (pfile);
4165 U_CHAR *ident;
4166 int ident_length;
4167 enum cpp_token token;
4168 int start_of_file = 0;
4169 U_CHAR *control_macro = 0;
4170 int old_written = CPP_WRITTEN (pfile);
4172 /* Detect a #ifndef at start of file (not counting comments). */
4173 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4174 start_of_file = pfile->only_seen_white == 2;
4176 pfile->no_macro_expand++;
4177 token = get_directive_token (pfile);
4178 pfile->no_macro_expand--;
4180 ident = pfile->token_buffer + old_written;
4181 ident_length = CPP_WRITTEN (pfile) - old_written;
4182 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4184 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4186 skip = (keyword->type == T_IFDEF);
4187 if (! CPP_TRADITIONAL (pfile))
4188 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4190 else if (token == CPP_NAME)
4192 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4193 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4194 if (start_of_file && !skip)
4196 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4197 bcopy (ident, control_macro, ident_length + 1);
4200 else
4202 skip = (keyword->type == T_IFDEF);
4203 if (! CPP_TRADITIONAL (pfile))
4204 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4207 if (!CPP_TRADITIONAL (pfile))
4208 { int c;
4209 cpp_skip_hspace (pfile);
4210 c = PEEKC ();
4211 if (c != EOF && c != '\n')
4212 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4214 skip_rest_of_line (pfile);
4216 #if 0
4217 if (pcp_outfile) {
4218 /* Output a precondition for this macro. */
4219 if (hp && hp->value.defn->predefined)
4220 fprintf (pcp_outfile, "#define %s\n", hp->name);
4221 else {
4222 U_CHAR *cp = buf;
4223 fprintf (pcp_outfile, "#undef ");
4224 while (is_idchar[*cp]) /* Ick! */
4225 fputc (*cp++, pcp_outfile);
4226 putc ('\n', pcp_outfile);
4228 #endif
4230 conditional_skip (pfile, skip, T_IF, control_macro);
4231 return 0;
4234 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4235 If this is a #ifndef starting at the beginning of a file,
4236 CONTROL_MACRO is the macro name tested by the #ifndef.
4237 Otherwise, CONTROL_MACRO is 0. */
4239 static void
4240 conditional_skip (pfile, skip, type, control_macro)
4241 cpp_reader *pfile;
4242 int skip;
4243 enum node_type type;
4244 U_CHAR *control_macro;
4246 IF_STACK_FRAME *temp;
4248 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4249 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4250 #if 0
4251 temp->lineno = CPP_BUFFER (pfile)->lineno;
4252 #endif
4253 temp->next = pfile->if_stack;
4254 temp->control_macro = control_macro;
4255 pfile->if_stack = temp;
4257 pfile->if_stack->type = type;
4259 if (skip != 0) {
4260 skip_if_group (pfile, 0);
4261 return;
4262 } else {
4263 ++pfile->if_stack->if_succeeded;
4264 output_line_command (pfile, 1, same_file);
4269 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4270 * leaves input ptr at the sharp sign found.
4271 * If ANY is nonzero, return at next directive of any sort.
4274 static void
4275 skip_if_group (pfile, any)
4276 cpp_reader *pfile;
4277 int any;
4279 int c;
4280 int at_beg_of_line = 1;
4281 struct directive *kt;
4282 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4283 #if 0
4284 U_CHAR *beg_of_line = bp;
4285 #endif
4286 register int ident_length;
4287 U_CHAR *ident, *after_ident;
4288 struct parse_marker line_start_mark;
4290 parse_set_mark (&line_start_mark, pfile);
4292 if (CPP_OPTIONS (pfile)->output_conditionals) {
4293 static char failed[] = "#failed\n";
4294 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4295 pfile->lineno++;
4296 output_line_command (pfile, 1, same_file);
4299 beg_of_line:
4300 if (CPP_OPTIONS (pfile)->output_conditionals)
4302 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4303 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4304 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4306 parse_move_mark (&line_start_mark, pfile);
4307 if (!CPP_TRADITIONAL (pfile))
4308 cpp_skip_hspace (pfile);
4309 c = GETC();
4310 if (c == '#')
4312 int old_written = CPP_WRITTEN (pfile);
4313 cpp_skip_hspace (pfile);
4315 parse_name (pfile, GETC());
4316 ident_length = CPP_WRITTEN (pfile) - old_written;
4317 ident = pfile->token_buffer + old_written;
4318 pfile->limit = ident;
4319 #if 0
4320 if (ident_length == 0)
4321 goto not_a_directive;
4323 /* Handle # followed by a line number. */
4325 /* Avoid error for `###' and similar cases unless -pedantic. */
4326 #endif
4328 for (kt = directive_table; kt->length >= 0; kt++)
4330 IF_STACK_FRAME *temp;
4331 if (ident_length == kt->length
4332 && strncmp (ident, kt->name, kt->length) == 0)
4334 /* If we are asked to return on next directive, do so now. */
4335 if (any)
4336 goto done;
4338 switch (kt->type)
4340 case T_IF:
4341 case T_IFDEF:
4342 case T_IFNDEF:
4343 temp
4344 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4345 temp->next = pfile->if_stack;
4346 pfile->if_stack = temp;
4347 #if 0
4348 temp->lineno = CPP_BUFFER(pfile)->lineno;
4349 #endif
4350 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4351 temp->type = kt->type;
4352 break;
4353 case T_ELSE:
4354 case T_ENDIF:
4355 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4356 validate_else (pfile,
4357 kt->type == T_ELSE ? "#else" : "#endif");
4358 case T_ELIF:
4359 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4361 cpp_error (pfile,
4362 "`#%s' not within a conditional", kt->name);
4363 break;
4365 else if (pfile->if_stack == save_if_stack)
4366 goto done; /* found what we came for */
4368 if (kt->type != T_ENDIF)
4370 if (pfile->if_stack->type == T_ELSE)
4371 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4372 pfile->if_stack->type = kt->type;
4373 break;
4376 temp = pfile->if_stack;
4377 pfile->if_stack = temp->next;
4378 free (temp);
4379 break;
4380 default: ;
4382 break;
4384 /* Don't let erroneous code go by. */
4385 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4386 && CPP_PEDANTIC (pfile))
4387 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4389 c = GETC ();
4391 /* We're in the middle of a line. Skip the rest of it. */
4392 for (;;) {
4393 switch (c)
4395 long old;
4396 case EOF:
4397 goto done;
4398 case '/': /* possible comment */
4399 c = skip_comment (pfile, NULL);
4400 if (c == EOF)
4401 goto done;
4402 break;
4403 case '\"':
4404 case '\'':
4405 FORWARD(-1);
4406 old = CPP_WRITTEN (pfile);
4407 cpp_get_token (pfile);
4408 CPP_SET_WRITTEN (pfile, old);
4409 break;
4410 case '\\':
4411 /* Char after backslash loses its special meaning. */
4412 if (PEEKC() == '\n')
4413 FORWARD (1);
4414 break;
4415 case '\n':
4416 goto beg_of_line;
4417 break;
4419 c = GETC ();
4421 done:
4422 if (CPP_OPTIONS (pfile)->output_conditionals) {
4423 static char end_failed[] = "#endfailed\n";
4424 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4425 pfile->lineno++;
4427 pfile->only_seen_white = 1;
4428 parse_goto_mark (&line_start_mark, pfile);
4429 parse_clear_mark (&line_start_mark);
4433 * handle a #else directive. Do this by just continuing processing
4434 * without changing if_stack ; this is so that the error message
4435 * for missing #endif's etc. will point to the original #if. It
4436 * is possible that something different would be better.
4439 static int
4440 do_else (pfile, keyword, buf, limit)
4441 cpp_reader *pfile;
4442 struct directive *keyword;
4443 U_CHAR *buf, *limit;
4445 cpp_buffer *ip = CPP_BUFFER (pfile);
4447 if (CPP_PEDANTIC (pfile))
4448 validate_else (pfile, "#else");
4449 skip_rest_of_line (pfile);
4451 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4452 cpp_error (pfile, "`#else' not within a conditional");
4453 return 0;
4454 } else {
4455 /* #ifndef can't have its special treatment for containing the whole file
4456 if it has a #else clause. */
4457 pfile->if_stack->control_macro = 0;
4459 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4460 cpp_error (pfile, "`#else' after `#else'");
4461 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4462 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4463 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4464 fprintf (stderr, ")\n");
4466 pfile->if_stack->type = T_ELSE;
4469 if (pfile->if_stack->if_succeeded)
4470 skip_if_group (pfile, 0);
4471 else {
4472 ++pfile->if_stack->if_succeeded; /* continue processing input */
4473 output_line_command (pfile, 1, same_file);
4475 return 0;
4479 * unstack after #endif command
4482 static int
4483 do_endif (pfile, keyword, buf, limit)
4484 cpp_reader *pfile;
4485 struct directive *keyword;
4486 U_CHAR *buf, *limit;
4488 if (CPP_PEDANTIC (pfile))
4489 validate_else (pfile, "#endif");
4490 skip_rest_of_line (pfile);
4492 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4493 cpp_error (pfile, "unbalanced `#endif'");
4494 else
4496 IF_STACK_FRAME *temp = pfile->if_stack;
4497 pfile->if_stack = temp->next;
4498 if (temp->control_macro != 0)
4500 /* This #endif matched a #ifndef at the start of the file.
4501 See if it is at the end of the file. */
4502 struct parse_marker start_mark;
4503 int c;
4505 parse_set_mark (&start_mark, pfile);
4507 for (;;)
4509 cpp_skip_hspace (pfile);
4510 c = GETC ();
4511 if (c != '\n')
4512 break;
4514 parse_goto_mark (&start_mark, pfile);
4515 parse_clear_mark (&start_mark);
4517 if (c == EOF)
4519 /* If we get here, this #endif ends a #ifndef
4520 that contains all of the file (aside from whitespace).
4521 Arrange not to include the file again
4522 if the macro that was tested is defined.
4524 Do not do this for the top-level file in a -include or any
4525 file in a -imacros. */
4526 #if 0
4527 FIXME!
4528 if (indepth != 0
4529 && ! (indepth == 1 && pfile->no_record_file)
4530 && ! (pfile->no_record_file && no_output))
4531 #endif
4533 struct file_name_list *ifile = pfile->all_include_files;
4535 for ( ; ifile != NULL; ifile = ifile->next)
4537 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4539 ifile->control_macro = temp->control_macro;
4540 break;
4546 free (temp);
4547 output_line_command (pfile, 1, same_file);
4549 return 0;
4552 /* When an #else or #endif is found while skipping failed conditional,
4553 if -pedantic was specified, this is called to warn about text after
4554 the command name. P points to the first char after the command name. */
4556 static void
4557 validate_else (pfile, directive)
4558 cpp_reader *pfile;
4559 char *directive;
4561 int c;
4562 cpp_skip_hspace (pfile);
4563 c = PEEKC ();
4564 if (c != EOF && c != '\n')
4565 cpp_pedwarn (pfile,
4566 "text following `%s' violates ANSI standard", directive);
4569 /* Get the next token, and add it to the text in pfile->token_buffer.
4570 Return the kind of token we got. */
4572 enum cpp_token
4573 cpp_get_token (pfile)
4574 cpp_reader *pfile;
4576 register int c, c2, c3;
4577 long old_written;
4578 long start_line, start_column;
4579 enum cpp_token token;
4580 struct cpp_options *opts = CPP_OPTIONS (pfile);
4581 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4582 get_next:
4583 c = GETC();
4584 if (c == EOF)
4586 handle_eof:
4587 if (CPP_BUFFER (pfile)->seen_eof)
4589 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4590 goto get_next;
4591 else
4592 return CPP_EOF;
4594 else
4596 cpp_buffer *next_buf
4597 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4598 CPP_BUFFER (pfile)->seen_eof = 1;
4599 if (CPP_BUFFER (pfile)->nominal_fname
4600 && next_buf != CPP_NULL_BUFFER (pfile))
4602 /* We're about to return from an #include file.
4603 Emit #line information now (as part of the CPP_POP) result.
4604 But the #line refers to the file we will pop to. */
4605 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4606 CPP_BUFFER (pfile) = next_buf;
4607 pfile->input_stack_listing_current = 0;
4608 output_line_command (pfile, 0, leave_file);
4609 CPP_BUFFER (pfile) = cur_buffer;
4611 return CPP_POP;
4614 else
4616 switch (c)
4618 long newlines;
4619 struct parse_marker start_mark;
4620 case '/':
4621 if (PEEKC () == '=')
4622 goto op2;
4623 if (opts->put_out_comments)
4624 parse_set_mark (&start_mark, pfile);
4625 newlines = 0;
4626 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4627 &start_line, &start_column);
4628 c = skip_comment (pfile, &newlines);
4629 if (opts->put_out_comments && (c == '/' || c == EOF))
4630 parse_clear_mark (&start_mark);
4631 if (c == '/')
4632 goto randomchar;
4633 if (c == EOF)
4635 cpp_error_with_line (pfile, start_line, start_column,
4636 "unterminated comment");
4637 goto handle_eof;
4639 c = '/'; /* Initial letter of comment. */
4640 return_comment:
4641 /* Comments are equivalent to spaces.
4642 For -traditional, a comment is equivalent to nothing. */
4643 if (opts->put_out_comments)
4645 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4646 long dummy;
4647 U_CHAR *start = pbuf->buf + start_mark.position;
4648 int len = pbuf->cur - start;
4649 CPP_RESERVE(pfile, 1 + len);
4650 CPP_PUTC_Q (pfile, c);
4651 CPP_PUTS_Q (pfile, start, len);
4652 pfile->lineno += newlines;
4653 parse_clear_mark (&start_mark);
4654 return CPP_COMMENT;
4656 else if (CPP_TRADITIONAL (pfile))
4658 return CPP_COMMENT;
4660 else
4662 #if 0
4663 /* This may not work if cpp_get_token is called recursively,
4664 since many places look for horizontal space. */
4665 if (newlines)
4667 /* Copy the newlines into the output buffer, in order to
4668 avoid the pain of a #line every time a multiline comment
4669 is seen. */
4670 CPP_RESERVE(pfile, newlines);
4671 while (--newlines >= 0)
4673 CPP_PUTC_Q (pfile, '\n');
4674 pfile->lineno++;
4676 return CPP_VSPACE;
4678 #endif
4679 CPP_RESERVE(pfile, 1);
4680 CPP_PUTC_Q (pfile, ' ');
4681 return CPP_HSPACE;
4683 #if 0
4684 if (opts->for_lint) {
4685 U_CHAR *argbp;
4686 int cmdlen, arglen;
4687 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4689 if (lintcmd != NULL) {
4690 /* I believe it is always safe to emit this newline: */
4691 obp[-1] = '\n';
4692 bcopy ("#pragma lint ", (char *) obp, 13);
4693 obp += 13;
4694 bcopy (lintcmd, (char *) obp, cmdlen);
4695 obp += cmdlen;
4697 if (arglen != 0) {
4698 *(obp++) = ' ';
4699 bcopy (argbp, (char *) obp, arglen);
4700 obp += arglen;
4703 /* OK, now bring us back to the state we were in before we entered
4704 this branch. We need #line b/c the newline for the pragma
4705 could fuck things up. */
4706 output_line_command (pfile, 0, same_file);
4707 *(obp++) = ' '; /* just in case, if comments are copied thru */
4708 *(obp++) = '/';
4711 #endif
4713 case '#':
4714 #if 0
4715 /* If this is expanding a macro definition, don't recognize
4716 preprocessor directives. */
4717 if (ip->macro != 0)
4718 goto randomchar;
4719 /* If this is expand_into_temp_buffer, recognize them
4720 only after an actual newline at this level,
4721 not at the beginning of the input level. */
4722 if (ip->fname == 0 && beg_of_line == ip->buf)
4723 goto randomchar;
4724 if (ident_length)
4725 goto specialchar;
4726 #endif
4728 if (!pfile->only_seen_white)
4729 goto randomchar;
4730 if (handle_directive (pfile))
4731 return CPP_DIRECTIVE;
4732 pfile->only_seen_white = 0;
4733 return CPP_OTHER;
4735 case '\"':
4736 case '\'':
4737 /* A single quoted string is treated like a double -- some
4738 programs (e.g., troff) are perverse this way */
4739 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4740 &start_line, &start_column);
4741 old_written = CPP_WRITTEN (pfile);
4742 string:
4743 CPP_PUTC (pfile, c);
4744 while (1)
4746 int cc = GETC();
4747 if (cc == EOF)
4749 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4751 /* try harder: this string crosses a macro expansion
4752 boundary. This can happen naturally if -traditional.
4753 Otherwise, only -D can make a macro with an unmatched
4754 quote. */
4755 cpp_buffer *next_buf
4756 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4757 (*CPP_BUFFER (pfile)->cleanup)
4758 (CPP_BUFFER (pfile), pfile);
4759 CPP_BUFFER (pfile) = next_buf;
4760 continue;
4762 if (!CPP_TRADITIONAL (pfile))
4764 cpp_error_with_line (pfile, start_line, start_column,
4765 "unterminated string or character constant");
4766 if (pfile->multiline_string_line != start_line
4767 && pfile->multiline_string_line != 0)
4768 cpp_error_with_line (pfile,
4769 pfile->multiline_string_line, -1,
4770 "possible real start of unterminated constant");
4771 pfile->multiline_string_line = 0;
4773 break;
4775 CPP_PUTC (pfile, cc);
4776 switch (cc)
4778 case '\n':
4779 /* Traditionally, end of line ends a string constant with
4780 no error. So exit the loop and record the new line. */
4781 if (CPP_TRADITIONAL (pfile))
4782 goto while2end;
4783 if (c == '\'')
4785 cpp_error_with_line (pfile, start_line, start_column,
4786 "unterminated character constant");
4787 goto while2end;
4789 if (CPP_PEDANTIC (pfile)
4790 && pfile->multiline_string_line == 0)
4792 cpp_pedwarn_with_line (pfile, start_line, start_column,
4793 "string constant runs past end of line");
4795 if (pfile->multiline_string_line == 0)
4796 pfile->multiline_string_line = start_line;
4797 break;
4799 case '\\':
4800 cc = GETC();
4801 if (cc == '\n')
4803 /* Backslash newline is replaced by nothing at all. */
4804 CPP_ADJUST_WRITTEN (pfile, -1);
4805 pfile->lineno++;
4807 else
4809 /* ANSI stupidly requires that in \\ the second \
4810 is *not* prevented from combining with a newline. */
4811 NEWLINE_FIX1(cc);
4812 if (cc != EOF)
4813 CPP_PUTC (pfile, cc);
4815 break;
4817 case '\"':
4818 case '\'':
4819 if (cc == c)
4820 goto while2end;
4821 break;
4824 while2end:
4825 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4826 CPP_PWRITTEN (pfile));
4827 pfile->only_seen_white = 0;
4828 return c == '\'' ? CPP_CHAR : CPP_STRING;
4830 case '$':
4831 if (!opts->dollars_in_ident)
4832 goto randomchar;
4833 goto letter;
4835 case ':':
4836 if (opts->cplusplus && PEEKC () == ':')
4837 goto op2;
4838 goto randomchar;
4840 case '&':
4841 case '+':
4842 case '|':
4843 NEWLINE_FIX;
4844 c2 = PEEKC ();
4845 if (c2 == c || c2 == '=')
4846 goto op2;
4847 goto randomchar;
4849 case '*':
4850 case '!':
4851 case '%':
4852 case '=':
4853 case '^':
4854 NEWLINE_FIX;
4855 if (PEEKC () == '=')
4856 goto op2;
4857 goto randomchar;
4859 case '-':
4860 NEWLINE_FIX;
4861 c2 = PEEKC ();
4862 if (c2 == '-' && opts->chill)
4864 /* Chill style comment */
4865 if (opts->put_out_comments)
4866 parse_set_mark (&start_mark, pfile);
4867 FORWARD(1); /* Skip second '-'. */
4868 for (;;)
4870 c = GETC ();
4871 if (c == EOF)
4872 break;
4873 if (c == '\n')
4875 /* Don't consider final '\n' to be part of comment. */
4876 FORWARD(-1);
4877 break;
4880 c = '-';
4881 goto return_comment;
4883 if (c2 == '-' || c2 == '=' || c2 == '>')
4884 goto op2;
4885 goto randomchar;
4887 case '<':
4888 if (pfile->parsing_include_directive)
4890 for (;;)
4892 CPP_PUTC (pfile, c);
4893 if (c == '>')
4894 break;
4895 c = GETC ();
4896 NEWLINE_FIX1 (c);
4897 if (c == '\n' || c == EOF)
4899 cpp_error (pfile,
4900 "missing '>' in `#include <FILENAME>'");
4901 break;
4904 return CPP_STRING;
4906 /* else fall through */
4907 case '>':
4908 NEWLINE_FIX;
4909 c2 = PEEKC ();
4910 if (c2 == '=')
4911 goto op2;
4912 if (c2 != c)
4913 goto randomchar;
4914 FORWARD(1);
4915 CPP_RESERVE (pfile, 4);
4916 CPP_PUTC (pfile, c);
4917 CPP_PUTC (pfile, c2);
4918 NEWLINE_FIX;
4919 c3 = PEEKC ();
4920 if (c3 == '=')
4921 CPP_PUTC_Q (pfile, GETC ());
4922 CPP_NUL_TERMINATE_Q (pfile);
4923 pfile->only_seen_white = 0;
4924 return CPP_OTHER;
4926 case '@':
4927 if (CPP_BUFFER (pfile)->has_escapes)
4929 c = GETC ();
4930 if (c == '-')
4932 if (pfile->output_escapes)
4933 CPP_PUTS (pfile, "@-", 2);
4934 parse_name (pfile, GETC ());
4935 return CPP_NAME;
4937 else if (is_space [c])
4939 CPP_RESERVE (pfile, 2);
4940 if (pfile->output_escapes)
4941 CPP_PUTC_Q (pfile, '@');
4942 CPP_PUTC_Q (pfile, c);
4943 return CPP_HSPACE;
4946 if (pfile->output_escapes)
4948 CPP_PUTS (pfile, "@@", 2);
4949 return CPP_OTHER;
4951 goto randomchar;
4953 case '.':
4954 NEWLINE_FIX;
4955 c2 = PEEKC ();
4956 if (isdigit(c2))
4958 CPP_RESERVE(pfile, 2);
4959 CPP_PUTC_Q (pfile, '.');
4960 c = GETC ();
4961 goto number;
4963 /* FIXME - misses the case "..\\\n." */
4964 if (c2 == '.' && PEEKN(1) == '.')
4966 CPP_RESERVE(pfile, 4);
4967 CPP_PUTC_Q (pfile, '.');
4968 CPP_PUTC_Q (pfile, '.');
4969 CPP_PUTC_Q (pfile, '.');
4970 FORWARD (2);
4971 CPP_NUL_TERMINATE_Q (pfile);
4972 pfile->only_seen_white = 0;
4973 return CPP_3DOTS;
4975 goto randomchar;
4977 op2:
4978 token = CPP_OTHER;
4979 pfile->only_seen_white = 0;
4980 op2any:
4981 CPP_RESERVE(pfile, 3);
4982 CPP_PUTC_Q (pfile, c);
4983 CPP_PUTC_Q (pfile, GETC ());
4984 CPP_NUL_TERMINATE_Q (pfile);
4985 return token;
4987 case 'L':
4988 NEWLINE_FIX;
4989 c2 = PEEKC ();
4990 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4992 CPP_PUTC (pfile, c);
4993 c = GETC ();
4994 goto string;
4996 goto letter;
4998 case '0': case '1': case '2': case '3': case '4':
4999 case '5': case '6': case '7': case '8': case '9':
5000 number:
5001 c2 = '.';
5002 for (;;)
5004 CPP_RESERVE (pfile, 2);
5005 CPP_PUTC_Q (pfile, c);
5006 NEWLINE_FIX;
5007 c = PEEKC ();
5008 if (c == EOF)
5009 break;
5010 if (!is_idchar[c] && c != '.'
5011 && ((c2 != 'e' && c2 != 'E'
5012 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
5013 || (c != '+' && c != '-')))
5014 break;
5015 FORWARD(1);
5016 c2= c;
5018 CPP_NUL_TERMINATE_Q (pfile);
5019 pfile->only_seen_white = 0;
5020 return CPP_NUMBER;
5021 case 'b': case 'c': case 'd': case 'h': case 'o':
5022 case 'B': case 'C': case 'D': case 'H': case 'O':
5023 if (opts->chill && PEEKC () == '\'')
5025 pfile->only_seen_white = 0;
5026 CPP_RESERVE (pfile, 2);
5027 CPP_PUTC_Q (pfile, c);
5028 CPP_PUTC_Q (pfile, '\'');
5029 FORWARD(1);
5030 for (;;)
5032 c = GETC();
5033 if (c == EOF)
5034 goto chill_number_eof;
5035 if (!is_idchar[c])
5037 if (c == '\\' && PEEKC() == '\n')
5039 FORWARD(2);
5040 continue;
5042 break;
5044 CPP_PUTC (pfile, c);
5046 if (c == '\'')
5048 CPP_RESERVE (pfile, 2);
5049 CPP_PUTC_Q (pfile, c);
5050 CPP_NUL_TERMINATE_Q (pfile);
5051 return CPP_STRING;
5053 else
5055 FORWARD(-1);
5056 chill_number_eof:
5057 CPP_NUL_TERMINATE (pfile);
5058 return CPP_NUMBER;
5061 else
5062 goto letter;
5063 case '_':
5064 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5065 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5066 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5067 case 'x': case 'y': case 'z':
5068 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5069 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5070 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5071 case 'Y': case 'Z':
5072 letter:
5074 HASHNODE *hp;
5075 unsigned char *ident;
5076 int before_name_written = CPP_WRITTEN (pfile);
5077 int ident_len;
5078 parse_name (pfile, c);
5079 pfile->only_seen_white = 0;
5080 if (pfile->no_macro_expand)
5081 return CPP_NAME;
5082 ident = pfile->token_buffer + before_name_written;
5083 ident_len = CPP_PWRITTEN (pfile) - ident;
5084 hp = cpp_lookup (pfile, ident, ident_len, -1);
5085 if (!hp)
5086 return CPP_NAME;
5087 if (hp->type == T_DISABLED)
5089 if (pfile->output_escapes)
5090 { /* Return "@-IDENT", followed by '\0'. */
5091 int i;
5092 CPP_RESERVE (pfile, 3);
5093 ident = pfile->token_buffer + before_name_written;
5094 CPP_ADJUST_WRITTEN (pfile, 2);
5095 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5096 ident[0] = '@';
5097 ident[1] = '-';
5099 return CPP_NAME;
5102 /* If macro wants an arglist, verify that a '(' follows.
5103 first skip all whitespace, copying it to the output
5104 after the macro name. Then, if there is no '(',
5105 decide this is not a macro call and leave things that way. */
5106 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5108 struct parse_marker macro_mark;
5109 int is_macro_call;
5110 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5112 cpp_buffer *next_buf;
5113 cpp_skip_hspace (pfile);
5114 if (PEEKC () != EOF)
5115 break;
5116 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5117 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5118 CPP_BUFFER (pfile) = next_buf;
5120 parse_set_mark (&macro_mark, pfile);
5121 for (;;)
5123 cpp_skip_hspace (pfile);
5124 c = PEEKC ();
5125 is_macro_call = c == '(';
5126 if (c != '\n')
5127 break;
5128 FORWARD (1);
5130 if (!is_macro_call)
5131 parse_goto_mark (&macro_mark, pfile);
5132 parse_clear_mark (&macro_mark);
5133 if (!is_macro_call)
5134 return CPP_NAME;
5136 /* This is now known to be a macro call. */
5138 /* it might not actually be a macro. */
5139 if (hp->type != T_MACRO) {
5140 int xbuf_len; U_CHAR *xbuf;
5141 CPP_SET_WRITTEN (pfile, before_name_written);
5142 special_symbol (hp, pfile);
5143 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5144 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5145 CPP_SET_WRITTEN (pfile, before_name_written);
5146 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5147 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5149 else
5151 /* Expand the macro, reading arguments as needed,
5152 and push the expansion on the input stack. */
5153 macroexpand (pfile, hp);
5154 CPP_SET_WRITTEN (pfile, before_name_written);
5157 /* An extra "@ " is added to the end of a macro expansion
5158 to prevent accidental token pasting. We prefer to avoid
5159 unneeded extra spaces (for the sake of cpp-using tools like
5160 imake). Here we remove the space if it is safe to do so. */
5161 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5162 && pfile->buffer->rlimit[-2] == '@'
5163 && pfile->buffer->rlimit[-1] == ' ')
5165 int c1 = pfile->buffer->rlimit[-3];
5166 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5167 if (c2 == EOF || ! unsafe_chars (c1, c2))
5168 pfile->buffer->rlimit -= 2;
5171 goto get_next;
5173 case ' ': case '\t': case '\v': case '\r':
5174 for (;;)
5176 CPP_PUTC (pfile, c);
5177 c = PEEKC ();
5178 if (c == EOF || !is_hor_space[c])
5179 break;
5180 FORWARD(1);
5182 return CPP_HSPACE;
5184 case '\\':
5185 c2 = PEEKC ();
5186 if (c2 != '\n')
5187 goto randomchar;
5188 token = CPP_HSPACE;
5189 goto op2any;
5191 case '\n':
5192 CPP_PUTC (pfile, c);
5193 if (pfile->only_seen_white == 0)
5194 pfile->only_seen_white = 1;
5195 pfile->lineno++;
5196 output_line_command (pfile, 1, same_file);
5197 return CPP_VSPACE;
5199 case '(': token = CPP_LPAREN; goto char1;
5200 case ')': token = CPP_RPAREN; goto char1;
5201 case '{': token = CPP_LBRACE; goto char1;
5202 case '}': token = CPP_RBRACE; goto char1;
5203 case ',': token = CPP_COMMA; goto char1;
5204 case ';': token = CPP_SEMICOLON; goto char1;
5206 randomchar:
5207 default:
5208 token = CPP_OTHER;
5209 char1:
5210 pfile->only_seen_white = 0;
5211 CPP_PUTC (pfile, c);
5212 return token;
5217 /* Like cpp_get_token, but skip spaces and comments. */
5219 enum cpp_token
5220 cpp_get_non_space_token (pfile)
5221 cpp_reader *pfile;
5223 int old_written = CPP_WRITTEN (pfile);
5224 for (;;)
5226 enum cpp_token token = cpp_get_token (pfile);
5227 if (token != CPP_COMMENT && token != CPP_POP
5228 && token != CPP_HSPACE && token != CPP_VSPACE)
5229 return token;
5230 CPP_SET_WRITTEN (pfile, old_written);
5234 /* Parse an identifier starting with C. */
5237 parse_name (pfile, c)
5238 cpp_reader *pfile; int c;
5240 for (;;)
5242 if (! is_idchar[c])
5244 if (c == '\\' && PEEKC() == '\n')
5246 FORWARD(2);
5247 continue;
5249 FORWARD (-1);
5250 break;
5253 if (c == '$' && CPP_PEDANTIC (pfile))
5254 cpp_pedwarn ("`$' in identifier");
5256 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5257 CPP_PUTC_Q (pfile, c);
5258 c = GETC();
5259 if (c == EOF)
5260 break;
5262 CPP_NUL_TERMINATE_Q (pfile);
5263 return 1;
5267 /* Maintain and search list of included files, for #import. */
5269 /* Hash a file name for import_hash_table. */
5271 static int
5272 import_hash (f)
5273 char *f;
5275 int val = 0;
5277 while (*f) val += *f++;
5278 return (val%IMPORT_HASH_SIZE);
5281 /* Search for file FILENAME in import_hash_table.
5282 Return -2 if found, either a matching name or a matching inode.
5283 Otherwise, open the file and return a file descriptor if successful
5284 or -1 if unsuccessful. */
5286 static int
5287 lookup_import (pfile, filename, searchptr)
5288 cpp_reader *pfile;
5289 char *filename;
5290 struct file_name_list *searchptr;
5292 struct import_file *i;
5293 int h;
5294 int hashval;
5295 struct stat sb;
5296 int fd;
5298 hashval = import_hash (filename);
5300 /* Attempt to find file in list of already included files */
5301 i = pfile->import_hash_table[hashval];
5303 while (i) {
5304 if (!strcmp (filename, i->name))
5305 return -2; /* return found */
5306 i = i->next;
5308 /* Open it and try a match on inode/dev */
5309 fd = open_include_file (pfile, filename, searchptr);
5310 if (fd < 0)
5311 return fd;
5312 fstat (fd, &sb);
5313 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5314 i = pfile->import_hash_table[h];
5315 while (i) {
5316 /* Compare the inode and the device.
5317 Supposedly on some systems the inode is not a scalar. */
5318 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5319 && i->dev == sb.st_dev) {
5320 close (fd);
5321 return -2; /* return found */
5323 i = i->next;
5326 return fd; /* Not found, return open file */
5329 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5331 static void
5332 add_import (pfile, fd, fname)
5333 cpp_reader *pfile;
5334 int fd;
5335 char *fname;
5337 struct import_file *i;
5338 int hashval;
5339 struct stat sb;
5341 hashval = import_hash (fname);
5342 fstat (fd, &sb);
5343 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5344 i->name = (char *)xmalloc (strlen (fname)+1);
5345 strcpy (i->name, fname);
5346 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5347 i->dev = sb.st_dev;
5348 i->next = pfile->import_hash_table[hashval];
5349 pfile->import_hash_table[hashval] = i;
5352 /* The file_name_map structure holds a mapping of file names for a
5353 particular directory. This mapping is read from the file named
5354 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5355 map filenames on a file system with severe filename restrictions,
5356 such as DOS. The format of the file name map file is just a series
5357 of lines with two tokens on each line. The first token is the name
5358 to map, and the second token is the actual name to use. */
5360 struct file_name_map
5362 struct file_name_map *map_next;
5363 char *map_from;
5364 char *map_to;
5367 #define FILE_NAME_MAP_FILE "header.gcc"
5369 /* Read a space delimited string of unlimited length from a stdio
5370 file. */
5372 static char *
5373 read_filename_string (ch, f)
5374 int ch;
5375 FILE *f;
5377 char *alloc, *set;
5378 int len;
5380 len = 20;
5381 set = alloc = xmalloc (len + 1);
5382 if (! is_space[ch])
5384 *set++ = ch;
5385 while ((ch = getc (f)) != EOF && ! is_space[ch])
5387 if (set - alloc == len)
5389 len *= 2;
5390 alloc = xrealloc (alloc, len + 1);
5391 set = alloc + len / 2;
5393 *set++ = ch;
5396 *set = '\0';
5397 ungetc (ch, f);
5398 return alloc;
5401 /* This structure holds a linked list of file name maps, one per directory. */
5403 struct file_name_map_list
5405 struct file_name_map_list *map_list_next;
5406 char *map_list_name;
5407 struct file_name_map *map_list_map;
5410 /* Read the file name map file for DIRNAME. */
5412 static struct file_name_map *
5413 read_name_map (pfile, dirname)
5414 cpp_reader *pfile;
5415 char *dirname;
5417 register struct file_name_map_list *map_list_ptr;
5418 char *name;
5419 FILE *f;
5421 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5422 map_list_ptr = map_list_ptr->map_list_next)
5423 if (! strcmp (map_list_ptr->map_list_name, dirname))
5424 return map_list_ptr->map_list_map;
5426 map_list_ptr = ((struct file_name_map_list *)
5427 xmalloc (sizeof (struct file_name_map_list)));
5428 map_list_ptr->map_list_name = savestring (dirname);
5429 map_list_ptr->map_list_map = NULL;
5431 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5432 strcpy (name, dirname);
5433 if (*dirname)
5434 strcat (name, "/");
5435 strcat (name, FILE_NAME_MAP_FILE);
5436 f = fopen (name, "r");
5437 if (!f)
5438 map_list_ptr->map_list_map = NULL;
5439 else
5441 int ch;
5442 int dirlen = strlen (dirname);
5444 while ((ch = getc (f)) != EOF)
5446 char *from, *to;
5447 struct file_name_map *ptr;
5449 if (is_space[ch])
5450 continue;
5451 from = read_filename_string (ch, f);
5452 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5454 to = read_filename_string (ch, f);
5456 ptr = ((struct file_name_map *)
5457 xmalloc (sizeof (struct file_name_map)));
5458 ptr->map_from = from;
5460 /* Make the real filename absolute. */
5461 if (*to == '/')
5462 ptr->map_to = to;
5463 else
5465 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5466 strcpy (ptr->map_to, dirname);
5467 ptr->map_to[dirlen] = '/';
5468 strcpy (ptr->map_to + dirlen + 1, to);
5469 free (to);
5472 ptr->map_next = map_list_ptr->map_list_map;
5473 map_list_ptr->map_list_map = ptr;
5475 while ((ch = getc (f)) != '\n')
5476 if (ch == EOF)
5477 break;
5479 fclose (f);
5482 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5483 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5485 return map_list_ptr->map_list_map;
5488 /* Try to open include file FILENAME. SEARCHPTR is the directory
5489 being tried from the include file search path. This function maps
5490 filenames on file systems based on information read by
5491 read_name_map. */
5493 static int
5494 open_include_file (pfile, filename, searchptr)
5495 cpp_reader *pfile;
5496 char *filename;
5497 struct file_name_list *searchptr;
5499 register struct file_name_map *map;
5500 register char *from;
5501 char *p, *dir;
5503 if (searchptr && ! searchptr->got_name_map)
5505 searchptr->name_map = read_name_map (pfile,
5506 searchptr->fname
5507 ? searchptr->fname : ".");
5508 searchptr->got_name_map = 1;
5511 /* First check the mapping for the directory we are using. */
5512 if (searchptr && searchptr->name_map)
5514 from = filename;
5515 if (searchptr->fname)
5516 from += strlen (searchptr->fname) + 1;
5517 for (map = searchptr->name_map; map; map = map->map_next)
5519 if (! strcmp (map->map_from, from))
5521 /* Found a match. */
5522 return open (map->map_to, O_RDONLY, 0666);
5527 /* Try to find a mapping file for the particular directory we are
5528 looking in. Thus #include <sys/types.h> will look up sys/types.h
5529 in /usr/include/header.gcc and look up types.h in
5530 /usr/include/sys/header.gcc. */
5531 p = rindex (filename, '/');
5532 if (! p)
5533 p = filename;
5534 if (searchptr
5535 && searchptr->fname
5536 && strlen (searchptr->fname) == p - filename
5537 && ! strncmp (searchptr->fname, filename, p - filename))
5539 /* FILENAME is in SEARCHPTR, which we've already checked. */
5540 return open (filename, O_RDONLY, 0666);
5543 if (p == filename)
5545 dir = ".";
5546 from = filename;
5548 else
5550 dir = (char *) alloca (p - filename + 1);
5551 bcopy (filename, dir, p - filename);
5552 dir[p - filename] = '\0';
5553 from = p + 1;
5555 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5556 if (! strcmp (map->map_from, from))
5557 return open (map->map_to, O_RDONLY, 0666);
5559 return open (filename, O_RDONLY, 0666);
5562 /* Process the contents of include file FNAME, already open on descriptor F,
5563 with output to OP.
5564 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5565 "system" include directories (as decided by the `is_system_include'
5566 function above).
5567 DIRPTR is the link in the dir path through which this file was found,
5568 or 0 if the file name was absolute or via the current directory.
5569 Return 1 on success, 0 on failure.
5571 The caller is responsible for the cpp_push_buffer. */
5573 static int
5574 finclude (pfile, f, fname, system_header_p, dirptr)
5575 cpp_reader *pfile;
5576 int f;
5577 char *fname;
5578 int system_header_p;
5579 struct file_name_list *dirptr;
5581 int st_mode;
5582 long st_size;
5583 long i;
5584 int length;
5585 cpp_buffer *fp; /* For input stack frame */
5586 int missing_newline = 0;
5588 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5590 cpp_perror_with_name (pfile, fname);
5591 close (f);
5592 cpp_pop_buffer (pfile);
5593 return 0;
5596 fp = CPP_BUFFER (pfile);
5597 fp->nominal_fname = fp->fname = fname;
5598 #if 0
5599 fp->length = 0;
5600 #endif
5601 fp->dir = dirptr;
5602 fp->system_header_p = system_header_p;
5603 fp->lineno = 1;
5604 fp->colno = 1;
5605 fp->cleanup = file_cleanup;
5607 if (S_ISREG (st_mode)) {
5608 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5609 fp->alimit = fp->buf + st_size + 2;
5610 fp->cur = fp->buf;
5612 /* Read the file contents, knowing that st_size is an upper bound
5613 on the number of bytes we can read. */
5614 length = safe_read (f, fp->buf, st_size);
5615 fp->rlimit = fp->buf + length;
5616 if (length < 0) goto nope;
5618 else if (S_ISDIR (st_mode)) {
5619 cpp_error (pfile, "directory `%s' specified in #include", fname);
5620 close (f);
5621 return 0;
5622 } else {
5623 /* Cannot count its file size before reading.
5624 First read the entire file into heap and
5625 copy them into buffer on stack. */
5627 int bsize = 2000;
5629 st_size = 0;
5630 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5632 for (;;) {
5633 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5634 if (i < 0)
5635 goto nope; /* error! */
5636 st_size += i;
5637 if (st_size != bsize)
5638 break; /* End of file */
5639 bsize *= 2;
5640 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5642 fp->cur = fp->buf;
5643 length = st_size;
5646 if ((length > 0 && fp->buf[length - 1] != '\n')
5647 /* Backslash-newline at end is not good enough. */
5648 || (length > 1 && fp->buf[length - 2] == '\\')) {
5649 fp->buf[length++] = '\n';
5650 #if 0
5651 missing_newline = 1;
5652 #endif
5654 fp->buf[length] = '\0';
5655 fp->rlimit = fp->buf + length;
5657 /* Close descriptor now, so nesting does not use lots of descriptors. */
5658 close (f);
5660 /* Must do this before calling trigraph_pcp, so that the correct file name
5661 will be printed in warning messages. */
5663 pfile->input_stack_listing_current = 0;
5665 #if 0
5666 if (!no_trigraphs)
5667 trigraph_pcp (fp);
5668 #endif
5670 #if 0
5671 rescan (op, 0);
5673 if (missing_newline)
5674 fp->lineno--;
5676 if (CPP_PEDANTIC (pfile) && missing_newline)
5677 pedwarn ("file does not end in newline");
5679 indepth--;
5680 input_file_stack_tick++;
5681 free (fp->buf);
5682 #endif
5683 return 1;
5685 nope:
5687 cpp_perror_with_name (pfile, fname);
5688 close (f);
5689 free (fp->buf);
5690 return 1;
5693 /* This is called after options have been processed.
5694 * Check options for consistency, and setup for processing input
5695 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5696 * Return 1 on succes, 0 on failure.
5700 cpp_start_read (pfile, fname)
5701 cpp_reader *pfile;
5702 char *fname;
5704 struct cpp_options *opts = CPP_OPTIONS (pfile);
5705 struct cpp_pending *pend;
5706 char *p;
5707 int f;
5708 cpp_buffer *fp;
5710 /* The code looks at the defaults through this pointer, rather than through
5711 the constant structure above. This pointer gets changed if an environment
5712 variable specifies other defaults. */
5713 struct default_include *include_defaults = include_defaults_array;
5715 /* Add dirs from CPATH after dirs from -I. */
5716 /* There seems to be confusion about what CPATH should do,
5717 so for the moment it is not documented. */
5718 /* Some people say that CPATH should replace the standard include dirs,
5719 but that seems pointless: it comes before them, so it overrides them
5720 anyway. */
5721 p = (char *) getenv ("CPATH");
5722 if (p != 0 && ! opts->no_standard_includes)
5723 path_include (pfile, p);
5725 /* Now that dollars_in_ident is known, initialize is_idchar. */
5726 initialize_char_syntax (opts);
5728 /* Do partial setup of input buffer for the sake of generating
5729 early #line directives (when -g is in effect). */
5730 fp = cpp_push_buffer (pfile, NULL, 0);
5731 if (!fp)
5732 return 0;
5733 if (opts->in_fname == NULL)
5734 opts->in_fname = "";
5735 fp->nominal_fname = fp->fname = opts->in_fname;
5736 fp->lineno = 0;
5738 /* Install __LINE__, etc. Must follow initialize_char_syntax
5739 and option processing. */
5740 initialize_builtins (pfile);
5742 /* Do standard #defines and assertions
5743 that identify system and machine type. */
5745 if (!opts->inhibit_predefs) {
5746 char *p = (char *) alloca (strlen (predefs) + 1);
5747 strcpy (p, predefs);
5748 while (*p) {
5749 char *q;
5750 while (*p == ' ' || *p == '\t')
5751 p++;
5752 /* Handle -D options. */
5753 if (p[0] == '-' && p[1] == 'D') {
5754 q = &p[2];
5755 while (*p && *p != ' ' && *p != '\t')
5756 p++;
5757 if (*p != 0)
5758 *p++= 0;
5759 if (opts->debug_output)
5760 output_line_command (pfile, 0, same_file);
5761 cpp_define (pfile, q);
5762 while (*p == ' ' || *p == '\t')
5763 p++;
5764 } else if (p[0] == '-' && p[1] == 'A') {
5765 /* Handle -A options (assertions). */
5766 char *assertion;
5767 char *past_name;
5768 char *value;
5769 char *past_value;
5770 char *termination;
5771 int save_char;
5773 assertion = &p[2];
5774 past_name = assertion;
5775 /* Locate end of name. */
5776 while (*past_name && *past_name != ' '
5777 && *past_name != '\t' && *past_name != '(')
5778 past_name++;
5779 /* Locate `(' at start of value. */
5780 value = past_name;
5781 while (*value && (*value == ' ' || *value == '\t'))
5782 value++;
5783 if (*value++ != '(')
5784 abort ();
5785 while (*value && (*value == ' ' || *value == '\t'))
5786 value++;
5787 past_value = value;
5788 /* Locate end of value. */
5789 while (*past_value && *past_value != ' '
5790 && *past_value != '\t' && *past_value != ')')
5791 past_value++;
5792 termination = past_value;
5793 while (*termination && (*termination == ' ' || *termination == '\t'))
5794 termination++;
5795 if (*termination++ != ')')
5796 abort ();
5797 if (*termination && *termination != ' ' && *termination != '\t')
5798 abort ();
5799 /* Temporarily null-terminate the value. */
5800 save_char = *termination;
5801 *termination = '\0';
5802 /* Install the assertion. */
5803 make_assertion (pfile, "-A", assertion);
5804 *termination = (char) save_char;
5805 p = termination;
5806 while (*p == ' ' || *p == '\t')
5807 p++;
5808 } else {
5809 abort ();
5814 /* Now handle the command line options. */
5816 /* Do -U's, -D's and -A's in the order they were seen. */
5817 /* First reverse the list. */
5818 opts->pending = nreverse_pending (opts->pending);
5820 for (pend = opts->pending; pend; pend = pend->next)
5822 if (pend->cmd != NULL && pend->cmd[0] == '-')
5824 switch (pend->cmd[1])
5826 case 'U':
5827 if (opts->debug_output)
5828 output_line_command (pfile, 0, same_file);
5829 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5830 break;
5831 case 'D':
5832 if (opts->debug_output)
5833 output_line_command (pfile, 0, same_file);
5834 cpp_define (pfile, pend->arg);
5835 break;
5836 case 'A':
5837 make_assertion (pfile, "-A", pend->arg);
5838 break;
5843 opts->done_initializing = 1;
5845 { /* Read the appropriate environment variable and if it exists
5846 replace include_defaults with the listed path. */
5847 char *epath = 0;
5848 switch ((opts->objc << 1) + opts->cplusplus)
5850 case 0:
5851 epath = getenv ("C_INCLUDE_PATH");
5852 break;
5853 case 1:
5854 epath = getenv ("CPLUS_INCLUDE_PATH");
5855 break;
5856 case 2:
5857 epath = getenv ("OBJC_INCLUDE_PATH");
5858 break;
5859 case 3:
5860 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5861 break;
5863 /* If the environment var for this language is set,
5864 add to the default list of include directories. */
5865 if (epath) {
5866 char *nstore = (char *) alloca (strlen (epath) + 2);
5867 int num_dirs;
5868 char *startp, *endp;
5870 for (num_dirs = 1, startp = epath; *startp; startp++)
5871 if (*startp == PATH_SEPARATOR)
5872 num_dirs++;
5873 include_defaults
5874 = (struct default_include *) xmalloc ((num_dirs
5875 * sizeof (struct default_include))
5876 + sizeof (include_defaults_array));
5877 startp = endp = epath;
5878 num_dirs = 0;
5879 while (1) {
5880 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5881 if ((*endp == PATH_SEPARATOR)
5882 || *endp == 0) {
5883 strncpy (nstore, startp, endp-startp);
5884 if (endp == startp)
5885 strcpy (nstore, ".");
5886 else
5887 nstore[endp-startp] = '\0';
5889 include_defaults[num_dirs].fname = savestring (nstore);
5890 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5891 include_defaults[num_dirs].cxx_aware = 1;
5892 num_dirs++;
5893 if (*endp == '\0')
5894 break;
5895 endp = startp = endp + 1;
5896 } else
5897 endp++;
5899 /* Put the usual defaults back in at the end. */
5900 bcopy ((char *) include_defaults_array,
5901 (char *) &include_defaults[num_dirs],
5902 sizeof (include_defaults_array));
5906 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5907 opts->first_system_include = opts->before_system;
5909 /* Unless -fnostdinc,
5910 tack on the standard include file dirs to the specified list */
5911 if (!opts->no_standard_includes) {
5912 struct default_include *p = include_defaults;
5913 char *specd_prefix = opts->include_prefix;
5914 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5915 int default_len = 0;
5916 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5917 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5918 default_len = strlen (default_prefix) - 7;
5919 default_prefix[default_len] = 0;
5921 /* Search "translated" versions of GNU directories.
5922 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5923 if (specd_prefix != 0 && default_len != 0)
5924 for (p = include_defaults; p->fname; p++) {
5925 /* Some standard dirs are only for C++. */
5926 if (!p->cplusplus
5927 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5928 /* Does this dir start with the prefix? */
5929 if (!strncmp (p->fname, default_prefix, default_len)) {
5930 /* Yes; change prefix and add to search list. */
5931 struct file_name_list *new
5932 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5933 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5934 char *str = (char *) xmalloc (this_len + 1);
5935 strcpy (str, specd_prefix);
5936 strcat (str, p->fname + default_len);
5937 new->fname = str;
5938 new->control_macro = 0;
5939 new->c_system_include_path = !p->cxx_aware;
5940 new->got_name_map = 0;
5941 append_include_chain (pfile, new, new);
5942 if (opts->first_system_include == 0)
5943 opts->first_system_include = new;
5947 /* Search ordinary names for GNU include directories. */
5948 for (p = include_defaults; p->fname; p++) {
5949 /* Some standard dirs are only for C++. */
5950 if (!p->cplusplus
5951 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5952 struct file_name_list *new
5953 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5954 new->control_macro = 0;
5955 new->c_system_include_path = !p->cxx_aware;
5956 new->fname = p->fname;
5957 new->got_name_map = 0;
5958 append_include_chain (pfile, new, new);
5959 if (opts->first_system_include == 0)
5960 opts->first_system_include = new;
5965 /* Tack the after_include chain at the end of the include chain. */
5966 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5967 if (opts->first_system_include == 0)
5968 opts->first_system_include = opts->after_include;
5970 /* With -v, print the list of dirs to search. */
5971 if (opts->verbose) {
5972 struct file_name_list *p;
5973 fprintf (stderr, "#include \"...\" search starts here:\n");
5974 for (p = opts->include; p; p = p->next) {
5975 if (p == opts->first_bracket_include)
5976 fprintf (stderr, "#include <...> search starts here:\n");
5977 fprintf (stderr, " %s\n", p->fname);
5979 fprintf (stderr, "End of search list.\n");
5982 /* Scan the -imacros files before the main input.
5983 Much like #including them, but with no_output set
5984 so that only their macro definitions matter. */
5986 opts->no_output++; pfile->no_record_file++;
5987 for (pend = opts->pending; pend; pend = pend->next)
5989 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
5991 int fd = open (pend->arg, O_RDONLY, 0666);
5992 if (fd < 0)
5994 cpp_perror_with_name (pfile, pend->arg);
5995 return 0;
5997 if (!cpp_push_buffer (pfile, NULL, 0))
5998 return 0;
5999 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6000 cpp_scan_buffer (pfile);
6003 opts->no_output--; pfile->no_record_file--;
6005 /* Copy the entire contents of the main input file into
6006 the stacked input buffer previously allocated for it. */
6007 if (fname == NULL || *fname == 0) {
6008 fname = "";
6009 f = 0;
6010 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6011 cpp_pfatal_with_name (pfile, fname);
6013 /* -MG doesn't select the form of output and must be specified with one of
6014 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6015 inhibit compilation. */
6016 if (opts->print_deps_missing_files
6017 && (opts->print_deps == 0 || !opts->no_output))
6019 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
6020 return 0;
6023 /* Either of two environment variables can specify output of deps.
6024 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6025 where OUTPUT_FILE is the file to write deps info to
6026 and DEPS_TARGET is the target to mention in the deps. */
6028 if (opts->print_deps == 0
6029 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6030 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6031 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6032 char *s;
6033 char *output_file;
6035 if (spec == 0)
6037 spec = getenv ("SUNPRO_DEPENDENCIES");
6038 opts->print_deps = 2;
6040 else
6041 opts->print_deps = 1;
6043 s = spec;
6044 /* Find the space before the DEPS_TARGET, if there is one. */
6045 /* This should use index. (mrs) */
6046 while (*s != 0 && *s != ' ') s++;
6047 if (*s != 0)
6049 opts->deps_target = s + 1;
6050 output_file = (char *) xmalloc (s - spec + 1);
6051 bcopy (spec, output_file, s - spec);
6052 output_file[s - spec] = 0;
6054 else
6056 opts->deps_target = 0;
6057 output_file = spec;
6060 opts->deps_file = output_file;
6061 opts->print_deps_append = 1;
6064 /* For -M, print the expected object file name
6065 as the target of this Make-rule. */
6066 if (opts->print_deps)
6068 pfile->deps_allocated_size = 200;
6069 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6070 pfile->deps_buffer[0] = 0;
6071 pfile->deps_size = 0;
6072 pfile->deps_column = 0;
6074 if (opts->deps_target)
6075 deps_output (pfile, opts->deps_target, ':');
6076 else if (*opts->in_fname == 0)
6077 deps_output (pfile, "-", ':');
6078 else
6080 char *p, *q;
6081 int len;
6083 /* Discard all directory prefixes from filename. */
6084 if ((q = rindex (opts->in_fname, '/')) != NULL
6085 #ifdef DIR_SEPARATOR
6086 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6087 #endif
6089 ++q;
6090 else
6091 q = opts->in_fname;
6093 /* Copy remainder to mungable area. */
6094 p = (char *) alloca (strlen(q) + 8);
6095 strcpy (p, q);
6097 /* Output P, but remove known suffixes. */
6098 len = strlen (p);
6099 q = p + len;
6100 if (len >= 2
6101 && p[len - 2] == '.'
6102 && index("cCsSm", p[len - 1]))
6103 q = p + (len - 2);
6104 else if (len >= 3
6105 && p[len - 3] == '.'
6106 && p[len - 2] == 'c'
6107 && p[len - 1] == 'c')
6108 q = p + (len - 3);
6109 else if (len >= 4
6110 && p[len - 4] == '.'
6111 && p[len - 3] == 'c'
6112 && p[len - 2] == 'x'
6113 && p[len - 1] == 'x')
6114 q = p + (len - 4);
6115 else if (len >= 4
6116 && p[len - 4] == '.'
6117 && p[len - 3] == 'c'
6118 && p[len - 2] == 'p'
6119 && p[len - 1] == 'p')
6120 q = p + (len - 4);
6122 /* Supply our own suffix. */
6123 #ifndef VMS
6124 strcpy (q, ".o");
6125 #else
6126 strcpy (q, ".obj");
6127 #endif
6129 deps_output (pfile, p, ':');
6130 deps_output (pfile, opts->in_fname, ' ');
6134 #if 0
6135 /* Make sure data ends with a newline. And put a null after it. */
6137 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6138 /* Backslash-newline at end is not good enough. */
6139 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6140 fp->buf[fp->length++] = '\n';
6141 missing_newline = 1;
6143 fp->buf[fp->length] = '\0';
6145 /* Unless inhibited, convert trigraphs in the input. */
6147 if (!no_trigraphs)
6148 trigraph_pcp (fp);
6149 #endif
6151 /* Scan the -include files before the main input.
6152 We push these in reverse order, so that the first one is handled first. */
6154 pfile->no_record_file++;
6155 opts->pending = nreverse_pending (opts->pending);
6156 for (pend = opts->pending; pend; pend = pend->next)
6158 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6160 int fd = open (pend->arg, O_RDONLY, 0666);
6161 if (fd < 0)
6163 cpp_perror_with_name (pfile, pend->arg);
6164 return 0;
6166 if (!cpp_push_buffer (pfile, NULL, 0))
6167 return 0;
6168 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6171 pfile->no_record_file--;
6173 /* Free the pending list. */
6174 for (pend = opts->pending; pend; )
6176 struct cpp_pending *next = pend->next;
6177 free (pend);
6178 pend = next;
6180 opts->pending = NULL;
6182 #if 0
6183 /* Scan the input, processing macros and directives. */
6185 rescan (&outbuf, 0);
6187 if (missing_newline)
6188 fp->lineno--;
6190 if (CPP_PEDANTIC (pfile) && missing_newline)
6191 pedwarn ("file does not end in newline");
6193 #endif
6194 if (finclude (pfile, f, fname, 0, NULL_PTR))
6195 output_line_command (pfile, 0, same_file);
6196 return 1;
6199 void
6200 cpp_reader_init (pfile)
6201 cpp_reader *pfile;
6203 bzero ((char *) pfile, sizeof (cpp_reader));
6204 pfile->get_token = cpp_get_token;
6206 pfile->token_buffer_size = 200;
6207 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
6208 CPP_SET_WRITTEN (pfile, 0);
6210 pfile->system_include_depth = 0;
6211 pfile->dont_repeat_files = 0;
6212 pfile->all_include_files = 0;
6213 pfile->max_include_len = 0;
6214 pfile->timebuf = NULL;
6215 pfile->only_seen_white = 1;
6216 pfile->buffer = CPP_NULL_BUFFER(pfile);
6219 static struct cpp_pending *
6220 nreverse_pending (list)
6221 struct cpp_pending *list;
6224 register struct cpp_pending *prev = 0, *next, *pend;
6225 for (pend = list; pend; pend = next)
6227 next = pend->next;
6228 pend->next = prev;
6229 prev = pend;
6231 return prev;
6234 static void
6235 push_pending (pfile, cmd, arg)
6236 cpp_reader *pfile;
6237 char *cmd;
6238 char *arg;
6240 struct cpp_pending *pend
6241 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
6242 pend->cmd = cmd;
6243 pend->arg = arg;
6244 pend->next = CPP_OPTIONS (pfile)->pending;
6245 CPP_OPTIONS (pfile)->pending = pend;
6248 /* Handle command-line options in (argc, argv).
6249 Can be called multiple times, to handle multiple sets of options.
6250 Returns if an unrecognized option is seen.
6251 Returns number of handled arguments. */
6254 cpp_handle_options (pfile, argc, argv)
6255 cpp_reader *pfile;
6256 int argc;
6257 char **argv;
6259 int i;
6260 struct cpp_options *opts = CPP_OPTIONS (pfile);
6261 for (i = 0; i < argc; i++) {
6262 if (argv[i][0] != '-') {
6263 if (opts->out_fname != NULL)
6265 cpp_fatal (pfile, "Usage: %s [switches] input output", argv[0]);
6266 return argc;
6268 else if (opts->in_fname != NULL)
6269 opts->out_fname = argv[i];
6270 else
6271 opts->in_fname = argv[i];
6272 } else {
6273 switch (argv[i][1]) {
6275 missing_filename:
6276 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
6277 return argc;
6278 missing_dirname:
6279 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
6280 return argc;
6282 case 'i':
6283 if (!strcmp (argv[i], "-include")
6284 || !strcmp (argv[i], "-imacros")) {
6285 if (i + 1 == argc)
6286 goto missing_filename;
6287 else
6288 push_pending (pfile, argv[i], argv[i+1]), i++;
6290 if (!strcmp (argv[i], "-iprefix")) {
6291 if (i + 1 == argc)
6292 goto missing_filename;
6293 else
6294 opts->include_prefix = argv[++i];
6296 if (!strcmp (argv[i], "-ifoutput")) {
6297 opts->output_conditionals = 1;
6299 if (!strcmp (argv[i], "-isystem")) {
6300 struct file_name_list *dirtmp;
6302 if (i + 1 == argc)
6303 goto missing_filename;
6305 dirtmp = (struct file_name_list *)
6306 xmalloc (sizeof (struct file_name_list));
6307 dirtmp->next = 0;
6308 dirtmp->control_macro = 0;
6309 dirtmp->c_system_include_path = 1;
6310 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6311 strcpy (dirtmp->fname, argv[++i]);
6312 dirtmp->got_name_map = 0;
6314 if (opts->before_system == 0)
6315 opts->before_system = dirtmp;
6316 else
6317 opts->last_before_system->next = dirtmp;
6318 opts->last_before_system = dirtmp; /* Tail follows the last one */
6320 /* Add directory to end of path for includes,
6321 with the default prefix at the front of its name. */
6322 if (!strcmp (argv[i], "-iwithprefix")) {
6323 struct file_name_list *dirtmp;
6324 char *prefix;
6326 if (opts->include_prefix != 0)
6327 prefix = opts->include_prefix;
6328 else {
6329 prefix = savestring (GCC_INCLUDE_DIR);
6330 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6331 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6332 prefix[strlen (prefix) - 7] = 0;
6335 dirtmp = (struct file_name_list *)
6336 xmalloc (sizeof (struct file_name_list));
6337 dirtmp->next = 0; /* New one goes on the end */
6338 dirtmp->control_macro = 0;
6339 dirtmp->c_system_include_path = 0;
6340 if (i + 1 == argc)
6341 goto missing_dirname;
6343 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6344 + strlen (prefix) + 1);
6345 strcpy (dirtmp->fname, prefix);
6346 strcat (dirtmp->fname, argv[++i]);
6347 dirtmp->got_name_map = 0;
6349 if (opts->after_include == 0)
6350 opts->after_include = dirtmp;
6351 else
6352 opts->last_after_include->next = dirtmp;
6353 opts->last_after_include = dirtmp; /* Tail follows the last one */
6355 /* Add directory to main path for includes,
6356 with the default prefix at the front of its name. */
6357 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6358 struct file_name_list *dirtmp;
6359 char *prefix;
6361 if (opts->include_prefix != 0)
6362 prefix = opts->include_prefix;
6363 else {
6364 prefix = savestring (GCC_INCLUDE_DIR);
6365 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6366 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6367 prefix[strlen (prefix) - 7] = 0;
6370 dirtmp = (struct file_name_list *)
6371 xmalloc (sizeof (struct file_name_list));
6372 dirtmp->next = 0; /* New one goes on the end */
6373 dirtmp->control_macro = 0;
6374 dirtmp->c_system_include_path = 0;
6375 if (i + 1 == argc)
6376 goto missing_dirname;
6378 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6379 + strlen (prefix) + 1);
6380 strcpy (dirtmp->fname, prefix);
6381 strcat (dirtmp->fname, argv[++i]);
6382 dirtmp->got_name_map = 0;
6384 append_include_chain (pfile, dirtmp, dirtmp);
6386 /* Add directory to end of path for includes. */
6387 if (!strcmp (argv[i], "-idirafter")) {
6388 struct file_name_list *dirtmp;
6390 dirtmp = (struct file_name_list *)
6391 xmalloc (sizeof (struct file_name_list));
6392 dirtmp->next = 0; /* New one goes on the end */
6393 dirtmp->control_macro = 0;
6394 dirtmp->c_system_include_path = 0;
6395 if (i + 1 == argc)
6396 goto missing_dirname;
6397 else
6398 dirtmp->fname = argv[++i];
6399 dirtmp->got_name_map = 0;
6401 if (opts->after_include == 0)
6402 opts->after_include = dirtmp;
6403 else
6404 opts->last_after_include->next = dirtmp;
6405 opts->last_after_include = dirtmp; /* Tail follows the last one */
6407 break;
6409 case 'o':
6410 if (opts->out_fname != NULL)
6412 cpp_fatal (pfile, "Output filename specified twice");
6413 return argc;
6415 if (i + 1 == argc)
6416 goto missing_filename;
6417 opts->out_fname = argv[++i];
6418 if (!strcmp (opts->out_fname, "-"))
6419 opts->out_fname = "";
6420 break;
6422 case 'p':
6423 if (!strcmp (argv[i], "-pedantic"))
6424 CPP_PEDANTIC (pfile) = 1;
6425 else if (!strcmp (argv[i], "-pedantic-errors")) {
6426 CPP_PEDANTIC (pfile) = 1;
6427 opts->pedantic_errors = 1;
6429 #if 0
6430 else if (!strcmp (argv[i], "-pcp")) {
6431 char *pcp_fname = argv[++i];
6432 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6433 ? fopen (pcp_fname, "w")
6434 : fdopen (dup (fileno (stdout)), "w"));
6435 if (pcp_outfile == 0)
6436 cpp_pfatal_with_name (pfile, pcp_fname);
6437 no_precomp = 1;
6439 #endif
6440 break;
6442 case 't':
6443 if (!strcmp (argv[i], "-traditional")) {
6444 opts->traditional = 1;
6445 } else if (!strcmp (argv[i], "-trigraphs")) {
6446 if (!opts->chill)
6447 opts->no_trigraphs = 0;
6449 break;
6451 case 'l':
6452 if (! strcmp (argv[i], "-lang-c"))
6453 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6454 opts->objc = 0;
6455 if (! strcmp (argv[i], "-lang-c89"))
6456 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
6457 opts->objc = 0;
6458 if (! strcmp (argv[i], "-lang-c++"))
6459 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6460 opts->objc = 0;
6461 if (! strcmp (argv[i], "-lang-objc"))
6462 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6463 opts->objc = 1;
6464 if (! strcmp (argv[i], "-lang-objc++"))
6465 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6466 opts->objc = 1;
6467 if (! strcmp (argv[i], "-lang-asm"))
6468 opts->lang_asm = 1;
6469 if (! strcmp (argv[i], "-lint"))
6470 opts->for_lint = 1;
6471 if (! strcmp (argv[i], "-lang-chill"))
6472 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6473 opts->traditional = 1, opts->no_trigraphs = 1;
6474 break;
6476 case '+':
6477 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6478 break;
6480 case 'w':
6481 opts->inhibit_warnings = 1;
6482 break;
6484 case 'W':
6485 if (!strcmp (argv[i], "-Wtrigraphs"))
6486 opts->warn_trigraphs = 1;
6487 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6488 opts->warn_trigraphs = 0;
6489 else if (!strcmp (argv[i], "-Wcomment"))
6490 opts->warn_comments = 1;
6491 else if (!strcmp (argv[i], "-Wno-comment"))
6492 opts->warn_comments = 0;
6493 else if (!strcmp (argv[i], "-Wcomments"))
6494 opts->warn_comments = 1;
6495 else if (!strcmp (argv[i], "-Wno-comments"))
6496 opts->warn_comments = 0;
6497 else if (!strcmp (argv[i], "-Wtraditional"))
6498 opts->warn_stringify = 1;
6499 else if (!strcmp (argv[i], "-Wno-traditional"))
6500 opts->warn_stringify = 0;
6501 else if (!strcmp (argv[i], "-Wundef"))
6502 opts->warn_undef = 1;
6503 else if (!strcmp (argv[i], "-Wno-undef"))
6504 opts->warn_undef = 0;
6505 else if (!strcmp (argv[i], "-Wimport"))
6506 opts->warn_import = 1;
6507 else if (!strcmp (argv[i], "-Wno-import"))
6508 opts->warn_import = 0;
6509 else if (!strcmp (argv[i], "-Werror"))
6510 opts->warnings_are_errors = 1;
6511 else if (!strcmp (argv[i], "-Wno-error"))
6512 opts->warnings_are_errors = 0;
6513 else if (!strcmp (argv[i], "-Wall"))
6515 opts->warn_trigraphs = 1;
6516 opts->warn_comments = 1;
6518 break;
6520 case 'M':
6521 /* The style of the choices here is a bit mixed.
6522 The chosen scheme is a hybrid of keeping all options in one string
6523 and specifying each option in a separate argument:
6524 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6525 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6526 -M[M][G][D file]. This is awkward to handle in specs, and is not
6527 as extensible. */
6528 /* ??? -MG must be specified in addition to one of -M or -MM.
6529 This can be relaxed in the future without breaking anything.
6530 The converse isn't true. */
6532 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6533 if (!strcmp (argv[i], "-MG"))
6535 opts->print_deps_missing_files = 1;
6536 break;
6538 if (!strcmp (argv[i], "-M"))
6539 opts->print_deps = 2;
6540 else if (!strcmp (argv[i], "-MM"))
6541 opts->print_deps = 1;
6542 else if (!strcmp (argv[i], "-MD"))
6543 opts->print_deps = 2;
6544 else if (!strcmp (argv[i], "-MMD"))
6545 opts->print_deps = 1;
6546 /* For -MD and -MMD options, write deps on file named by next arg. */
6547 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6549 if (i+1 == argc)
6550 goto missing_filename;
6551 opts->deps_file = argv[++i];
6553 else
6555 /* For -M and -MM, write deps on standard output
6556 and suppress the usual output. */
6557 opts->no_output = 1;
6559 break;
6561 case 'd':
6563 char *p = argv[i] + 2;
6564 char c;
6565 while ((c = *p++) != 0) {
6566 /* Arg to -d specifies what parts of macros to dump */
6567 switch (c) {
6568 case 'M':
6569 opts->dump_macros = dump_only;
6570 opts->no_output = 1;
6571 break;
6572 case 'N':
6573 opts->dump_macros = dump_names;
6574 break;
6575 case 'D':
6576 opts->dump_macros = dump_definitions;
6577 break;
6581 break;
6583 case 'g':
6584 if (argv[i][2] == '3')
6585 opts->debug_output = 1;
6586 break;
6588 case 'v':
6589 fprintf (stderr, "GNU CPP version %s", version_string);
6590 #ifdef TARGET_VERSION
6591 TARGET_VERSION;
6592 #endif
6593 fprintf (stderr, "\n");
6594 opts->verbose = 1;
6595 break;
6597 case 'H':
6598 opts->print_include_names = 1;
6599 break;
6601 case 'D':
6602 if (argv[i][2] != 0)
6603 push_pending (pfile, "-D", argv[i] + 2);
6604 else if (i + 1 == argc)
6606 cpp_fatal (pfile, "Macro name missing after -D option");
6607 return argc;
6609 else
6610 i++, push_pending (pfile, "-D", argv[i]);
6611 break;
6613 case 'A':
6615 char *p;
6617 if (argv[i][2] != 0)
6618 p = argv[i] + 2;
6619 else if (i + 1 == argc)
6621 cpp_fatal (pfile, "Assertion missing after -A option");
6622 return argc;
6624 else
6625 p = argv[++i];
6627 if (!strcmp (p, "-")) {
6628 struct cpp_pending **ptr;
6629 /* -A- eliminates all predefined macros and assertions.
6630 Let's include also any that were specified earlier
6631 on the command line. That way we can get rid of any
6632 that were passed automatically in from GCC. */
6633 int j;
6634 opts->inhibit_predefs = 1;
6635 for (ptr = &opts->pending; *ptr != NULL; )
6637 struct cpp_pending *pend = *ptr;
6638 if (pend->cmd && pend->cmd[0] == '-'
6639 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6641 *ptr = pend->next;
6642 free (pend);
6644 else
6645 ptr = &pend->next;
6647 } else {
6648 push_pending (pfile, "-A", p);
6651 break;
6653 case 'U': /* JF #undef something */
6654 if (argv[i][2] != 0)
6655 push_pending (pfile, "-U", argv[i] + 2);
6656 else if (i + 1 == argc)
6658 cpp_fatal (pfile, "Macro name missing after -U option", NULL);
6659 return argc;
6661 else
6662 push_pending (pfile, "-U", argv[i+1]), i++;
6663 break;
6665 case 'C':
6666 opts->put_out_comments = 1;
6667 break;
6669 case 'E': /* -E comes from cc -E; ignore it. */
6670 break;
6672 case 'P':
6673 opts->no_line_commands = 1;
6674 break;
6676 case '$': /* Don't include $ in identifiers. */
6677 opts->dollars_in_ident = 0;
6678 break;
6680 case 'I': /* Add directory to path for includes. */
6682 struct file_name_list *dirtmp;
6684 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6685 && !strcmp (argv[i] + 2, "-")) {
6686 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6687 /* Don't use any preceding -I directories for #include <...>. */
6688 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6690 else {
6691 dirtmp = (struct file_name_list *)
6692 xmalloc (sizeof (struct file_name_list));
6693 dirtmp->next = 0; /* New one goes on the end */
6694 dirtmp->control_macro = 0;
6695 dirtmp->c_system_include_path = 0;
6696 if (argv[i][2] != 0)
6697 dirtmp->fname = argv[i] + 2;
6698 else if (i + 1 == argc)
6699 goto missing_dirname;
6700 else
6701 dirtmp->fname = argv[++i];
6702 dirtmp->got_name_map = 0;
6703 append_include_chain (pfile, dirtmp, dirtmp);
6706 break;
6708 case 'n':
6709 if (!strcmp (argv[i], "-nostdinc"))
6710 /* -nostdinc causes no default include directories.
6711 You must specify all include-file directories with -I. */
6712 opts->no_standard_includes = 1;
6713 else if (!strcmp (argv[i], "-nostdinc++"))
6714 /* -nostdinc++ causes no default C++-specific include directories. */
6715 opts->no_standard_cplusplus_includes = 1;
6716 #if 0
6717 else if (!strcmp (argv[i], "-noprecomp"))
6718 no_precomp = 1;
6719 #endif
6720 break;
6722 case 'u':
6723 /* Sun compiler passes undocumented switch "-undef".
6724 Let's assume it means to inhibit the predefined symbols. */
6725 opts->inhibit_predefs = 1;
6726 break;
6728 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6729 if (opts->in_fname == NULL) {
6730 opts->in_fname = "";
6731 break;
6732 } else if (opts->out_fname == NULL) {
6733 opts->out_fname = "";
6734 break;
6735 } /* else fall through into error */
6737 default:
6738 return i;
6742 return i;
6745 void
6746 cpp_finish (pfile)
6747 cpp_reader *pfile;
6749 struct cpp_options *opts = CPP_OPTIONS (pfile);
6751 if (opts->print_deps)
6753 /* Stream on which to print the dependency information. */
6754 FILE *deps_stream;
6756 /* Don't actually write the deps file if compilation has failed. */
6757 if (pfile->errors == 0)
6759 char *deps_mode = opts->print_deps_append ? "a" : "w";
6760 if (opts->deps_file == 0)
6761 deps_stream = stdout;
6762 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6763 cpp_pfatal_with_name (pfile, opts->deps_file);
6764 fputs (pfile->deps_buffer, deps_stream);
6765 putc ('\n', deps_stream);
6766 if (opts->deps_file)
6768 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6769 cpp_fatal (pfile, "I/O error on output");
6775 /* Free resources used by PFILE.
6776 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6778 void
6779 cpp_cleanup (pfile)
6780 cpp_reader *pfile;
6782 int i;
6783 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6784 cpp_pop_buffer (pfile);
6786 if (pfile->token_buffer)
6788 free (pfile->token_buffer);
6789 pfile->token_buffer = NULL;
6792 if (pfile->deps_buffer)
6794 free (pfile->deps_buffer);
6795 pfile->deps_buffer = NULL;
6796 pfile->deps_allocated_size = 0;
6799 while (pfile->if_stack)
6801 IF_STACK_FRAME *temp = pfile->if_stack;
6802 pfile->if_stack = temp->next;
6803 free (temp);
6806 while (pfile->dont_repeat_files)
6808 struct file_name_list *temp = pfile->dont_repeat_files;
6809 pfile->dont_repeat_files = temp->next;
6810 free (temp->fname);
6811 free (temp);
6814 while (pfile->all_include_files)
6816 struct file_name_list *temp = pfile->all_include_files;
6817 pfile->all_include_files = temp->next;
6818 free (temp->fname);
6819 free (temp);
6822 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6824 register struct import_file *imp = pfile->import_hash_table[i];
6825 while (imp)
6827 struct import_file *next = imp->next;
6828 free (imp->name);
6829 free (imp);
6830 imp = next;
6832 pfile->import_hash_table[i] = 0;
6835 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6837 while (pfile->assertion_hashtab[i])
6838 delete_assertion (pfile->assertion_hashtab[i]);
6841 cpp_hash_cleanup (pfile);
6844 static int
6845 do_assert (pfile, keyword, buf, limit)
6846 cpp_reader *pfile;
6847 struct directive *keyword;
6848 U_CHAR *buf, *limit;
6850 long symstart; /* remember where symbol name starts */
6851 int c;
6852 int sym_length; /* and how long it is */
6853 struct arglist *tokens = NULL;
6855 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6856 && !CPP_BUFFER (pfile)->system_header_p)
6857 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6859 cpp_skip_hspace (pfile);
6860 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6861 parse_name (pfile, GETC());
6862 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6863 "assertion");
6865 cpp_skip_hspace (pfile);
6866 if (PEEKC() != '(') {
6867 cpp_error (pfile, "missing token-sequence in `#assert'");
6868 goto error;
6872 int error_flag = 0;
6873 tokens = read_token_list (pfile, &error_flag);
6874 if (error_flag)
6875 goto error;
6876 if (tokens == 0) {
6877 cpp_error (pfile, "empty token-sequence in `#assert'");
6878 goto error;
6880 cpp_skip_hspace (pfile);
6881 c = PEEKC ();
6882 if (c != EOF && c != '\n')
6883 cpp_pedwarn (pfile, "junk at end of `#assert'");
6884 skip_rest_of_line (pfile);
6887 /* If this name isn't already an assertion name, make it one.
6888 Error if it was already in use in some other way. */
6891 ASSERTION_HASHNODE *hp;
6892 U_CHAR *symname = pfile->token_buffer + symstart;
6893 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6894 struct tokenlist_list *value
6895 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6897 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6898 if (hp == NULL) {
6899 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6900 cpp_error (pfile, "`defined' redefined as assertion");
6901 hp = assertion_install (pfile, symname, sym_length, hashcode);
6904 /* Add the spec'd token-sequence to the list of such. */
6905 value->tokens = tokens;
6906 value->next = hp->value;
6907 hp->value = value;
6909 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6910 return 0;
6911 error:
6912 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6913 skip_rest_of_line (pfile);
6914 return 1;
6917 static int
6918 do_unassert (pfile, keyword, buf, limit)
6919 cpp_reader *pfile;
6920 struct directive *keyword;
6921 U_CHAR *buf, *limit;
6923 long symstart; /* remember where symbol name starts */
6924 int sym_length; /* and how long it is */
6925 int c;
6927 struct arglist *tokens = NULL;
6928 int tokens_specified = 0;
6930 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6931 && !CPP_BUFFER (pfile)->system_header_p)
6932 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6934 cpp_skip_hspace (pfile);
6936 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6937 parse_name (pfile, GETC());
6938 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6939 "assertion");
6941 cpp_skip_hspace (pfile);
6942 if (PEEKC() == '(') {
6943 int error_flag = 0;
6945 tokens = read_token_list (pfile, &error_flag);
6946 if (error_flag)
6947 goto error;
6948 if (tokens == 0) {
6949 cpp_error (pfile, "empty token list in `#unassert'");
6950 goto error;
6953 tokens_specified = 1;
6956 cpp_skip_hspace (pfile);
6957 c = PEEKC ();
6958 if (c != EOF && c != '\n')
6959 cpp_error (pfile, "junk at end of `#unassert'");
6960 skip_rest_of_line (pfile);
6963 ASSERTION_HASHNODE *hp;
6964 U_CHAR *symname = pfile->token_buffer + symstart;
6965 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6966 struct tokenlist_list *tail, *prev;
6968 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6969 if (hp == NULL)
6970 return 1;
6972 /* If no token list was specified, then eliminate this assertion
6973 entirely. */
6974 if (! tokens_specified)
6975 delete_assertion (hp);
6976 else {
6977 /* If a list of tokens was given, then delete any matching list. */
6979 tail = hp->value;
6980 prev = 0;
6981 while (tail) {
6982 struct tokenlist_list *next = tail->next;
6983 if (compare_token_lists (tail->tokens, tokens)) {
6984 if (prev)
6985 prev->next = next;
6986 else
6987 hp->value = tail->next;
6988 free_token_list (tail->tokens);
6989 free (tail);
6990 } else {
6991 prev = tail;
6993 tail = next;
6998 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6999 return 0;
7000 error:
7001 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7002 skip_rest_of_line (pfile);
7003 return 1;
7006 /* Test whether there is an assertion named NAME
7007 and optionally whether it has an asserted token list TOKENS.
7008 NAME is not null terminated; its length is SYM_LENGTH.
7009 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7012 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
7013 cpp_reader *pfile;
7014 U_CHAR *name;
7015 int sym_length;
7016 int tokens_specified;
7017 struct arglist *tokens;
7019 ASSERTION_HASHNODE *hp;
7020 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7022 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7023 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7025 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7026 if (hp == NULL)
7027 /* It is not an assertion; just return false. */
7028 return 0;
7030 /* If no token list was specified, then value is 1. */
7031 if (! tokens_specified)
7032 return 1;
7035 struct tokenlist_list *tail;
7037 tail = hp->value;
7039 /* If a list of tokens was given,
7040 then succeed if the assertion records a matching list. */
7042 while (tail) {
7043 if (compare_token_lists (tail->tokens, tokens))
7044 return 1;
7045 tail = tail->next;
7048 /* Fail if the assertion has no matching list. */
7049 return 0;
7053 /* Compare two lists of tokens for equality including order of tokens. */
7055 static int
7056 compare_token_lists (l1, l2)
7057 struct arglist *l1, *l2;
7059 while (l1 && l2) {
7060 if (l1->length != l2->length)
7061 return 0;
7062 if (strncmp (l1->name, l2->name, l1->length))
7063 return 0;
7064 l1 = l1->next;
7065 l2 = l2->next;
7068 /* Succeed if both lists end at the same time. */
7069 return l1 == l2;
7072 struct arglist *
7073 reverse_token_list (tokens)
7074 struct arglist *tokens;
7076 register struct arglist *prev = 0, *this, *next;
7077 for (this = tokens; this; this = next)
7079 next = this->next;
7080 this->next = prev;
7081 prev = this;
7083 return prev;
7086 /* Read a space-separated list of tokens ending in a close parenthesis.
7087 Return a list of strings, in the order they were written.
7088 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7090 static struct arglist *
7091 read_token_list (pfile, error_flag)
7092 cpp_reader *pfile;
7093 int *error_flag;
7095 struct arglist *token_ptrs = 0;
7096 int depth = 1;
7097 int length;
7099 *error_flag = 0;
7100 FORWARD (1); /* Skip '(' */
7102 /* Loop over the assertion value tokens. */
7103 while (depth > 0)
7105 struct arglist *temp;
7106 long name_written = CPP_WRITTEN (pfile);
7107 int eofp = 0; int c;
7109 cpp_skip_hspace (pfile);
7111 c = GETC ();
7113 /* Find the end of the token. */
7114 if (c == '(')
7116 CPP_PUTC (pfile, c);
7117 depth++;
7119 else if (c == ')')
7121 depth--;
7122 if (depth == 0)
7123 break;
7124 CPP_PUTC (pfile, c);
7126 else if (c == '"' || c == '\'')
7128 FORWARD(-1);
7129 cpp_get_token (pfile);
7131 else if (c == '\n')
7132 break;
7133 else
7135 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7136 && c != '"' && c != '\'')
7138 CPP_PUTC (pfile, c);
7139 c = GETC();
7141 if (c != EOF) FORWARD(-1);
7144 length = CPP_WRITTEN (pfile) - name_written;
7145 temp = (struct arglist *)
7146 xmalloc (sizeof (struct arglist) + length + 1);
7147 temp->name = (U_CHAR *) (temp + 1);
7148 bcopy ((char *) (pfile->token_buffer + name_written),
7149 (char *) temp->name, length);
7150 temp->name[length] = 0;
7151 temp->next = token_ptrs;
7152 token_ptrs = temp;
7153 temp->length = length;
7155 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7157 if (c == EOF || c == '\n')
7158 { /* FIXME */
7159 cpp_error (pfile,
7160 "unterminated token sequence following `#' operator");
7161 return 0;
7165 /* We accumulated the names in reverse order.
7166 Now reverse them to get the proper order. */
7167 return reverse_token_list (token_ptrs);
7170 static void
7171 free_token_list (tokens)
7172 struct arglist *tokens;
7174 while (tokens) {
7175 struct arglist *next = tokens->next;
7176 free (tokens->name);
7177 free (tokens);
7178 tokens = next;
7182 /* Get the file-mode and data size of the file open on FD
7183 and store them in *MODE_POINTER and *SIZE_POINTER. */
7185 static int
7186 file_size_and_mode (fd, mode_pointer, size_pointer)
7187 int fd;
7188 int *mode_pointer;
7189 long int *size_pointer;
7191 struct stat sbuf;
7193 if (fstat (fd, &sbuf) < 0) return (-1);
7194 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7195 if (size_pointer) *size_pointer = sbuf.st_size;
7196 return 0;
7199 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7200 retrying if necessary. If MAX_READ_LEN is defined, read at most
7201 that bytes at a time. Return a negative value if an error occurs,
7202 otherwise return the actual number of bytes read,
7203 which must be LEN unless end-of-file was reached. */
7205 static int
7206 safe_read (desc, ptr, len)
7207 int desc;
7208 char *ptr;
7209 int len;
7211 int left, rcount, nchars;
7213 left = len;
7214 while (left > 0) {
7215 rcount = left;
7216 #ifdef MAX_READ_LEN
7217 if (rcount > MAX_READ_LEN)
7218 rcount = MAX_READ_LEN;
7219 #endif
7220 nchars = read (desc, ptr, rcount);
7221 if (nchars < 0)
7223 #ifdef EINTR
7224 if (errno == EINTR)
7225 continue;
7226 #endif
7227 return nchars;
7229 if (nchars == 0)
7230 break;
7231 ptr += nchars;
7232 left -= nchars;
7234 return len - left;
7237 static char *
7238 xcalloc (number, size)
7239 unsigned number, size;
7241 register unsigned total = number * size;
7242 register char *ptr = (char *) xmalloc (total);
7243 bzero (ptr, total);
7244 return ptr;
7247 static char *
7248 savestring (input)
7249 char *input;
7251 unsigned size = strlen (input);
7252 char *output = xmalloc (size + 1);
7253 strcpy (output, input);
7254 return output;
7257 /* Initialize PMARK to remember the current position of PFILE. */
7259 void
7260 parse_set_mark (pmark, pfile)
7261 struct parse_marker *pmark;
7262 cpp_reader *pfile;
7264 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7265 pmark->next = pbuf->marks;
7266 pbuf->marks = pmark;
7267 pmark->buf = pbuf;
7268 pmark->position = pbuf->cur - pbuf->buf;
7271 /* Cleanup PMARK - we no longer need it. */
7273 void
7274 parse_clear_mark (pmark)
7275 struct parse_marker *pmark;
7277 struct parse_marker **pp = &pmark->buf->marks;
7278 for (; ; pp = &(*pp)->next) {
7279 if (*pp == NULL) abort ();
7280 if (*pp == pmark) break;
7282 *pp = pmark->next;
7285 /* Backup the current position of PFILE to that saved in PMARK. */
7287 void
7288 parse_goto_mark (pmark, pfile)
7289 struct parse_marker *pmark;
7290 cpp_reader *pfile;
7292 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7293 if (pbuf != pmark->buf)
7294 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
7295 pbuf->cur = pbuf->buf + pmark->position;
7298 /* Reset PMARK to point to the current position of PFILE. (Same
7299 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7301 void
7302 parse_move_mark (pmark, pfile)
7303 struct parse_marker *pmark;
7304 cpp_reader *pfile;
7306 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7307 if (pbuf != pmark->buf)
7308 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
7309 pmark->position = pbuf->cur - pbuf->buf;
7313 cpp_read_check_assertion (pfile)
7314 cpp_reader *pfile;
7316 int name_start = CPP_WRITTEN (pfile);
7317 int name_length, name_written;
7318 int result;
7319 FORWARD (1); /* Skip '#' */
7320 cpp_skip_hspace (pfile);
7321 parse_name (pfile, GETC ());
7322 name_written = CPP_WRITTEN (pfile);
7323 name_length = name_written - name_start;
7324 cpp_skip_hspace (pfile);
7325 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7327 int error_flag;
7328 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7329 result = check_assertion (pfile,
7330 pfile->token_buffer + name_start, name_length,
7331 1, token_ptrs);
7333 else
7334 result = check_assertion (pfile,
7335 pfile->token_buffer + name_start, name_length,
7336 0, NULL_PTR);
7337 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7338 return result;
7341 void
7342 cpp_print_file_and_line (pfile)
7343 cpp_reader *pfile;
7345 cpp_buffer *ip = cpp_file_buffer (pfile);
7347 if (ip != NULL)
7349 long line, col;
7350 cpp_buf_line_and_col (ip, &line, &col);
7351 cpp_file_line_for_message (pfile, ip->nominal_fname,
7352 line, pfile->show_column ? col : -1);
7356 void
7357 cpp_error (pfile, msg, arg1, arg2, arg3)
7358 cpp_reader *pfile;
7359 char *msg;
7360 char *arg1, *arg2, *arg3;
7362 cpp_print_containing_files (pfile);
7363 cpp_print_file_and_line (pfile);
7364 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7367 /* Print error message but don't count it. */
7369 void
7370 cpp_warning (pfile, msg, arg1, arg2, arg3)
7371 cpp_reader *pfile;
7372 char *msg;
7373 char *arg1, *arg2, *arg3;
7375 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7376 return;
7378 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7379 pfile->errors++;
7381 cpp_print_containing_files (pfile);
7382 cpp_print_file_and_line (pfile);
7383 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7386 /* Print an error message and maybe count it. */
7388 void
7389 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7390 cpp_reader *pfile;
7391 char *msg;
7392 char *arg1, *arg2, *arg3;
7394 if (CPP_OPTIONS (pfile)->pedantic_errors)
7395 cpp_error (pfile, msg, arg1, arg2, arg3);
7396 else
7397 cpp_warning (pfile, msg, arg1, arg2, arg3);
7400 void
7401 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7402 cpp_reader *pfile;
7403 int line, column;
7404 char *msg;
7405 char *arg1, *arg2, *arg3;
7407 int i;
7408 cpp_buffer *ip = cpp_file_buffer (pfile);
7410 cpp_print_containing_files (pfile);
7412 if (ip != NULL)
7413 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7415 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7418 static void
7419 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7420 cpp_reader *pfile;
7421 int line, column;
7422 char *msg;
7423 char *arg1, *arg2, *arg3;
7425 int i;
7426 cpp_buffer *ip;
7428 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7429 return;
7431 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7432 pfile->errors++;
7434 cpp_print_containing_files (pfile);
7436 ip = cpp_file_buffer (pfile);
7438 if (ip != NULL)
7439 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7441 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7444 void
7445 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7446 cpp_reader *pfile;
7447 int line;
7448 char *msg;
7449 char *arg1, *arg2, *arg3;
7451 if (CPP_OPTIONS (pfile)->pedantic_errors)
7452 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7453 else
7454 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7457 /* Report a warning (or an error if pedantic_errors)
7458 giving specified file name and line number, not current. */
7460 void
7461 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7462 cpp_reader *pfile;
7463 char *file;
7464 int line;
7465 char *msg;
7466 char *arg1, *arg2, *arg3;
7468 if (!CPP_OPTIONS (pfile)->pedantic_errors
7469 && CPP_OPTIONS (pfile)->inhibit_warnings)
7470 return;
7471 if (file != NULL)
7472 cpp_file_line_for_message (pfile, file, line, -1);
7473 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7474 msg, arg1, arg2, arg3);
7477 /* This defines "errno" properly for VMS, and gives us EACCES. */
7478 #include <errno.h>
7479 #ifndef errno
7480 extern int errno;
7481 #endif
7483 #ifndef VMS
7484 #ifndef HAVE_STRERROR
7485 extern int sys_nerr;
7486 #if defined(bsd4_4)
7487 extern const char *const sys_errlist[];
7488 #else
7489 extern char *sys_errlist[];
7490 #endif
7491 #else /* HAVE_STRERROR */
7492 char *strerror ();
7493 #endif
7494 #else /* VMS */
7495 char *strerror (int,...);
7496 #endif
7498 /* my_strerror - return the descriptive text associated with an
7499 `errno' code. */
7501 char *
7502 my_strerror (errnum)
7503 int errnum;
7505 char *result;
7507 #ifndef VMS
7508 #ifndef HAVE_STRERROR
7509 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7510 #else
7511 result = strerror (errnum);
7512 #endif
7513 #else /* VMS */
7514 /* VAXCRTL's strerror() takes an optional second argument, which only
7515 matters when the first argument is EVMSERR. However, it's simplest
7516 just to pass it unconditionally. `vaxc$errno' is declared in
7517 <errno.h>, and maintained by the library in parallel with `errno'.
7518 We assume that caller's `errnum' either matches the last setting of
7519 `errno' by the library or else does not have the value `EVMSERR'. */
7521 result = strerror (errnum, vaxc$errno);
7522 #endif
7524 if (!result)
7525 result = "undocumented I/O error";
7527 return result;
7530 /* Error including a message from `errno'. */
7532 void
7533 cpp_error_from_errno (pfile, name)
7534 cpp_reader *pfile;
7535 char *name;
7537 int i;
7538 cpp_buffer *ip = cpp_file_buffer (pfile);
7540 cpp_print_containing_files (pfile);
7542 if (ip != NULL)
7543 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7545 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7548 void
7549 cpp_perror_with_name (pfile, name)
7550 cpp_reader *pfile;
7551 char *name;
7553 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7556 /* TODO:
7557 * No pre-compiled header file support.
7559 * Possibly different enum token codes for each C/C++ token.
7561 * Should clean up remaining directives to that do_XXX functions
7562 * only take two arguments and all have command_reads_line.
7564 * Find and cleanup remaining uses of static variables,
7566 * Support for trigraphs.
7568 * Support -dM flag (dump_all_macros).
7570 * Support for_lint flag.