* decl.c (grokdeclarator): Remove const and volatile from type after
[official-gcc.git] / gcc / cpplib.c
blobaff4054fda714efdaac99278e58c1f0eef792b3d
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-7, 1998 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "config.h"
23 #ifndef STDC_VALUE
24 #define STDC_VALUE 1
25 #endif
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <signal.h>
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
35 #ifdef TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 # else
42 # include <time.h>
43 #endif
44 #endif
46 #ifdef HAVE_SYS_TIMES_H
47 #include <sys/times.h>
48 #endif
50 #ifdef HAVE_SYS_RESOURCE_H
51 # include <sys/resource.h>
52 #endif
54 #ifdef HAVE_FCNTL_H
55 # include <fcntl.h>
56 #endif
58 #if HAVE_LIMITS_H
59 # include <limits.h>
60 #endif
62 #ifdef HAVE_UNISTD_H
63 # include <unistd.h>
64 #endif
66 #ifdef HAVE_STRING_H
67 # include <string.h>
68 # else
69 # ifdef HAVE_STRINGS_H
70 # include <strings.h>
71 #endif
72 #endif
74 /* This defines "errno" properly for VMS, and gives us EACCES. */
75 #include <errno.h>
77 #include "cpplib.h"
78 #include "cpphash.h"
79 #include "gansidecl.h"
81 #ifdef NEED_DECLARATION_INDEX
82 extern char *index ();
83 #endif
85 #ifdef NEED_DECLARATION_RINDEX
86 extern char *rindex ();
87 #endif
89 #ifdef NEED_DECLARATION_GETENV
90 extern char *getenv ();
91 #endif
93 extern char *update_path ();
95 #ifndef O_RDONLY
96 #define O_RDONLY 0
97 #endif
99 #undef MIN
100 #undef MAX
101 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
102 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
104 /* Find the largest host integer type and set its size and type.
105 Watch out: on some crazy hosts `long' is shorter than `int'. */
107 #ifndef HOST_WIDE_INT
108 # if HAVE_INTTYPES_H
109 # include <inttypes.h>
110 # define HOST_WIDE_INT intmax_t
111 # else
112 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
113 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
114 # define HOST_WIDE_INT int
115 # else
116 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
117 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
118 # define HOST_WIDE_INT long
119 # else
120 # define HOST_WIDE_INT long long
121 # endif
122 # endif
123 # endif
124 #endif
126 #ifndef S_ISREG
127 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
128 #endif
130 #ifndef S_ISDIR
131 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
132 #endif
134 /* By default, colon separates directories in a path. */
135 #ifndef PATH_SEPARATOR
136 #define PATH_SEPARATOR ':'
137 #endif
139 #ifndef STANDARD_INCLUDE_DIR
140 #define STANDARD_INCLUDE_DIR "/usr/include"
141 #endif
142 #ifndef INCLUDE_LEN_FUDGE
143 #define INCLUDE_LEN_FUDGE 0
144 #endif
146 /* Symbols to predefine. */
148 #ifdef CPP_PREDEFINES
149 static char *predefs = CPP_PREDEFINES;
150 #else
151 static char *predefs = "";
152 #endif
154 /* We let tm.h override the types used here, to handle trivial differences
155 such as the choice of unsigned int or long unsigned int for size_t.
156 When machines start needing nontrivial differences in the size type,
157 it would be best to do something here to figure out automatically
158 from other information what type to use. */
160 /* The string value for __SIZE_TYPE__. */
162 #ifndef SIZE_TYPE
163 #define SIZE_TYPE "long unsigned int"
164 #endif
166 /* The string value for __PTRDIFF_TYPE__. */
168 #ifndef PTRDIFF_TYPE
169 #define PTRDIFF_TYPE "long int"
170 #endif
172 /* The string value for __WCHAR_TYPE__. */
174 #ifndef WCHAR_TYPE
175 #define WCHAR_TYPE "int"
176 #endif
177 #define CPP_WCHAR_TYPE(PFILE) \
178 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
180 /* The string value for __USER_LABEL_PREFIX__ */
182 #ifndef USER_LABEL_PREFIX
183 #define USER_LABEL_PREFIX ""
184 #endif
186 /* The string value for __REGISTER_PREFIX__ */
188 #ifndef REGISTER_PREFIX
189 #define REGISTER_PREFIX ""
190 #endif
192 /* In the definition of a #assert name, this structure forms
193 a list of the individual values asserted.
194 Each value is itself a list of "tokens".
195 These are strings that are compared by name. */
197 struct tokenlist_list {
198 struct tokenlist_list *next;
199 struct arglist *tokens;
202 struct assertion_hashnode {
203 struct assertion_hashnode *next; /* double links for easy deletion */
204 struct assertion_hashnode *prev;
205 /* also, a back pointer to this node's hash
206 chain is kept, in case the node is the head
207 of the chain and gets deleted. */
208 struct assertion_hashnode **bucket_hdr;
209 int length; /* length of token, for quick comparison */
210 U_CHAR *name; /* the actual name */
211 /* List of token-sequences. */
212 struct tokenlist_list *value;
215 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
216 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
218 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
219 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
220 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
221 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
222 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
223 (Note that it is false while we're expanding marco *arguments*.) */
224 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
226 /* Move all backslash-newline pairs out of embarrassing places.
227 Exchange all such pairs following BP
228 with any potentially-embarrassing characters that follow them.
229 Potentially-embarrassing characters are / and *
230 (because a backslash-newline inside a comment delimiter
231 would cause it not to be recognized). */
233 #define NEWLINE_FIX \
234 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
236 /* Same, but assume we've already read the potential '\\' into C. */
237 #define NEWLINE_FIX1(C) do { \
238 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
239 } while(0)
241 struct cpp_pending {
242 struct cpp_pending *next;
243 char *cmd;
244 char *arg;
247 /* Forward declarations. */
249 char *xmalloc ();
250 void cpp_fatal ();
251 void cpp_file_line_for_message PARAMS ((cpp_reader *, char *, int, int));
252 void cpp_hash_cleanup PARAMS ((cpp_reader *));
253 void cpp_message ();
254 void cpp_print_containing_files PARAMS ((cpp_reader *));
256 static void add_import ();
257 static void append_include_chain ();
258 static void make_assertion ();
259 static void path_include ();
260 static void initialize_builtins ();
261 static void initialize_char_syntax ();
262 extern void delete_macro ();
263 #if 0
264 static void trigraph_pcp ();
265 #endif
266 static int finclude ();
267 static void validate_else ();
268 static int comp_def_part ();
269 #ifdef abort
270 extern void fancy_abort ();
271 #endif
272 static int lookup_import ();
273 static int redundant_include_p ();
274 static int is_system_include ();
275 static struct file_name_map *read_name_map ();
276 static char *read_filename_string ();
277 static int open_include_file ();
278 static int check_macro_name ();
279 static int compare_defs ();
280 static int compare_token_lists ();
281 static HOST_WIDE_INT eval_if_expression ();
282 static int change_newlines ();
283 extern int hashf ();
284 static struct arglist *read_token_list ();
285 static void free_token_list ();
286 static int safe_read ();
287 static void push_macro_expansion PARAMS ((cpp_reader *,
288 U_CHAR *, int, HASHNODE *));
289 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
290 extern char *xrealloc ();
291 static char *xcalloc ();
292 static char *savestring ();
294 static void conditional_skip ();
295 static void skip_if_group ();
297 /* Last arg to output_line_command. */
298 enum file_change_code {same_file, enter_file, leave_file};
300 /* External declarations. */
302 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
304 extern FILE *fdopen ();
305 extern char *version_string;
306 extern struct tm *localtime ();
308 /* These functions are declared to return int instead of void since they
309 are going to be placed in a table and some old compilers have trouble with
310 pointers to functions returning void. */
312 static int do_define ();
313 static int do_line ();
314 static int do_include ();
315 static int do_undef ();
316 static int do_error ();
317 static int do_pragma ();
318 static int do_ident ();
319 static int do_if ();
320 static int do_xifdef ();
321 static int do_else ();
322 static int do_elif ();
323 static int do_endif ();
324 static int do_sccs ();
325 static int do_once ();
326 static int do_assert ();
327 static int do_unassert ();
328 static int do_warning ();
330 struct file_name_list
332 struct file_name_list *next;
333 char *fname;
334 /* If the following is nonzero, it is a macro name.
335 Don't include the file again if that macro is defined. */
336 U_CHAR *control_macro;
337 /* If the following is nonzero, it is a C-language system include
338 directory. */
339 int c_system_include_path;
340 /* Mapping of file names for this directory. */
341 struct file_name_map *name_map;
342 /* Non-zero if name_map is valid. */
343 int got_name_map;
346 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
347 via the same directory as the file that #included it. */
348 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
350 /* #include "file" looks in source file dir, then stack. */
351 /* #include <file> just looks in the stack. */
352 /* -I directories are added to the end, then the defaults are added. */
353 /* The */
354 static struct default_include {
355 char *fname; /* The name of the directory. */
356 char *component; /* The component containing the directory */
357 int cplusplus; /* Only look here if we're compiling C++. */
358 int cxx_aware; /* Includes in this directory don't need to
359 be wrapped in extern "C" when compiling
360 C++. */
361 } include_defaults_array[]
362 #ifdef INCLUDE_DEFAULTS
363 = INCLUDE_DEFAULTS;
364 #else
366 /* Pick up GNU C++ specific include files. */
367 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
368 { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 },
369 #ifdef CROSS_COMPILE
370 /* This is the dir for fixincludes. Put it just before
371 the files that we fix. */
372 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
373 /* For cross-compilation, this dir name is generated
374 automatically in Makefile.in. */
375 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
376 #ifdef TOOL_INCLUDE_DIR
377 /* This is another place that the target system's headers might be. */
378 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
379 #endif
380 #else /* not CROSS_COMPILE */
381 #ifdef LOCAL_INCLUDE_DIR
382 /* This should be /usr/local/include and should come before
383 the fixincludes-fixed header files. */
384 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
385 #endif
386 #ifdef TOOL_INCLUDE_DIR
387 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
388 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
389 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
390 #endif
391 /* This is the dir for fixincludes. Put it just before
392 the files that we fix. */
393 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
394 /* Some systems have an extra dir of include files. */
395 #ifdef SYSTEM_INCLUDE_DIR
396 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
397 #endif
398 #ifndef STANDARD_INCLUDE_COMPONENT
399 #define STANDARD_INCLUDE_COMPONENT 0
400 #endif
401 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
402 #endif /* not CROSS_COMPILE */
403 { 0, 0, 0, 0 }
405 #endif /* no INCLUDE_DEFAULTS */
407 /* `struct directive' defines one #-directive, including how to handle it. */
409 struct directive {
410 int length; /* Length of name */
411 int (*func)(); /* Function to handle directive */
412 char *name; /* Name of directive */
413 enum node_type type; /* Code which describes which directive. */
414 char command_reads_line; /* One if rest of line is read by func. */
417 #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT)
419 /* Here is the actual list of #-directives, most-often-used first.
420 The initialize_builtins function assumes #define is the very first. */
422 static struct directive directive_table[] = {
423 { 6, do_define, "define", T_DEFINE},
424 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
425 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
426 { 7, do_include, "include", T_INCLUDE, 1},
427 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
428 { 6, do_include, "import", T_IMPORT, 1},
429 { 5, do_endif, "endif", T_ENDIF, 1},
430 { 4, do_else, "else", T_ELSE, 1},
431 { 2, do_if, "if", T_IF, 1},
432 { 4, do_elif, "elif", T_ELIF, 1},
433 { 5, do_undef, "undef", T_UNDEF},
434 { 5, do_error, "error", T_ERROR},
435 { 7, do_warning, "warning", T_WARNING},
436 { 6, do_pragma, "pragma", T_PRAGMA},
437 { 4, do_line, "line", T_LINE, 1},
438 { 5, do_ident, "ident", T_IDENT, 1},
439 #ifdef SCCS_DIRECTIVE
440 { 4, do_sccs, "sccs", T_SCCS},
441 #endif
442 { 6, do_assert, "assert", T_ASSERT, 1},
443 { 8, do_unassert, "unassert", T_UNASSERT, 1},
444 { -1, 0, "", T_UNUSED},
447 /* table to tell if char can be part of a C identifier. */
448 U_CHAR is_idchar[256];
449 /* table to tell if char can be first char of a c identifier. */
450 U_CHAR is_idstart[256];
451 /* table to tell if c is horizontal space. */
452 U_CHAR is_hor_space[256];
453 /* table to tell if c is horizontal or vertical space. */
454 static U_CHAR is_space[256];
456 /* Initialize syntactic classifications of characters. */
458 static void
459 initialize_char_syntax (opts)
460 struct cpp_options *opts;
462 register int i;
465 * Set up is_idchar and is_idstart tables. These should be
466 * faster than saying (is_alpha (c) || c == '_'), etc.
467 * Set up these things before calling any routines tthat
468 * refer to them.
470 for (i = 'a'; i <= 'z'; i++) {
471 is_idchar[i - 'a' + 'A'] = 1;
472 is_idchar[i] = 1;
473 is_idstart[i - 'a' + 'A'] = 1;
474 is_idstart[i] = 1;
476 for (i = '0'; i <= '9'; i++)
477 is_idchar[i] = 1;
478 is_idchar['_'] = 1;
479 is_idstart['_'] = 1;
480 is_idchar['$'] = opts->dollars_in_ident;
481 is_idstart['$'] = opts->dollars_in_ident;
483 /* horizontal space table */
484 is_hor_space[' '] = 1;
485 is_hor_space['\t'] = 1;
486 is_hor_space['\v'] = 1;
487 is_hor_space['\f'] = 1;
488 is_hor_space['\r'] = 1;
490 is_space[' '] = 1;
491 is_space['\t'] = 1;
492 is_space['\v'] = 1;
493 is_space['\f'] = 1;
494 is_space['\n'] = 1;
495 is_space['\r'] = 1;
499 /* Place into PFILE a quoted string representing the string SRC.
500 Caller must reserve enough space in pfile->token_buffer. */
502 static void
503 quote_string (pfile, src)
504 cpp_reader *pfile;
505 char *src;
507 U_CHAR c;
509 CPP_PUTC_Q (pfile, '\"');
510 for (;;)
511 switch ((c = *src++))
513 default:
514 if (isprint (c))
515 CPP_PUTC_Q (pfile, c);
516 else
518 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
519 CPP_ADJUST_WRITTEN (pfile, 4);
521 break;
523 case '\"':
524 case '\\':
525 CPP_PUTC_Q (pfile, '\\');
526 CPP_PUTC_Q (pfile, c);
527 break;
529 case '\0':
530 CPP_PUTC_Q (pfile, '\"');
531 CPP_NUL_TERMINATE_Q (pfile);
532 return;
536 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
538 void
539 cpp_grow_buffer (pfile, n)
540 cpp_reader *pfile;
541 long n;
543 long old_written = CPP_WRITTEN (pfile);
544 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
545 pfile->token_buffer = (U_CHAR *)
546 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
547 CPP_SET_WRITTEN (pfile, old_written);
552 * process a given definition string, for initialization
553 * If STR is just an identifier, define it with value 1.
554 * If STR has anything after the identifier, then it should
555 * be identifier=definition.
558 void
559 cpp_define (pfile, str)
560 cpp_reader *pfile;
561 U_CHAR *str;
563 U_CHAR *buf, *p;
565 buf = str;
566 p = str;
567 if (!is_idstart[*p])
569 cpp_error (pfile, "malformed option `-D %s'", str);
570 return;
572 while (is_idchar[*++p])
574 if (*p == 0)
576 buf = (U_CHAR *) alloca (p - buf + 4);
577 strcpy ((char *)buf, str);
578 strcat ((char *)buf, " 1");
580 else if (*p != '=')
582 cpp_error (pfile, "malformed option `-D %s'", str);
583 return;
585 else
587 U_CHAR *q;
588 /* Copy the entire option so we can modify it. */
589 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
590 strncpy (buf, str, p - str);
591 /* Change the = to a space. */
592 buf[p - str] = ' ';
593 /* Scan for any backslash-newline and remove it. */
594 p++;
595 q = &buf[p - str];
596 while (*p)
598 if (*p == '\\' && p[1] == '\n')
599 p += 2;
600 else
601 *q++ = *p++;
603 *q = 0;
606 do_define (pfile, NULL, buf, buf + strlen (buf));
609 /* Process the string STR as if it appeared as the body of a #assert.
610 OPTION is the option name for which STR was the argument. */
612 static void
613 make_assertion (pfile, option, str)
614 cpp_reader *pfile;
615 char *option;
616 U_CHAR *str;
618 U_CHAR *buf, *p, *q;
620 /* Copy the entire option so we can modify it. */
621 buf = (U_CHAR *) alloca (strlen (str) + 1);
622 strcpy ((char *) buf, str);
623 /* Scan for any backslash-newline and remove it. */
624 p = q = buf;
625 while (*p) {
626 #if 0
627 if (*p == '\\' && p[1] == '\n')
628 p += 2;
629 else
630 #endif
631 *q++ = *p++;
633 *q = 0;
635 p = buf;
636 if (!is_idstart[*p]) {
637 cpp_error (pfile, "malformed option `%s %s'", option, str);
638 return;
640 while (is_idchar[*++p])
642 while (*p == ' ' || *p == '\t') p++;
643 if (! (*p == 0 || *p == '(')) {
644 cpp_error (pfile, "malformed option `%s %s'", option, str);
645 return;
648 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
650 do_assert (pfile, NULL, NULL, NULL);
651 cpp_pop_buffer (pfile);
655 /* Append a chain of `struct file_name_list's
656 to the end of the main include chain.
657 FIRST is the beginning of the chain to append, and LAST is the end. */
659 static void
660 append_include_chain (pfile, first, last)
661 cpp_reader *pfile;
662 struct file_name_list *first, *last;
664 struct cpp_options *opts = CPP_OPTIONS (pfile);
665 struct file_name_list *dir;
667 if (!first || !last)
668 return;
670 if (opts->include == 0)
671 opts->include = first;
672 else
673 opts->last_include->next = first;
675 if (opts->first_bracket_include == 0)
676 opts->first_bracket_include = first;
678 for (dir = first; ; dir = dir->next) {
679 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
680 if (len > pfile->max_include_len)
681 pfile->max_include_len = len;
682 if (dir == last)
683 break;
686 last->next = NULL;
687 opts->last_include = last;
690 /* Add output to `deps_buffer' for the -M switch.
691 STRING points to the text to be output.
692 SPACER is ':' for targets, ' ' for dependencies, zero for text
693 to be inserted literally. */
695 static void
696 deps_output (pfile, string, spacer)
697 cpp_reader *pfile;
698 char *string;
699 int spacer;
701 int size = strlen (string);
703 if (size == 0)
704 return;
706 #ifndef MAX_OUTPUT_COLUMNS
707 #define MAX_OUTPUT_COLUMNS 72
708 #endif
709 if (spacer
710 && pfile->deps_column > 0
711 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
713 deps_output (pfile, " \\\n ", 0);
714 pfile->deps_column = 0;
717 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
719 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
720 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
721 pfile->deps_allocated_size);
723 if (spacer == ' ' && pfile->deps_column > 0)
724 pfile->deps_buffer[pfile->deps_size++] = ' ';
725 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
726 pfile->deps_size += size;
727 pfile->deps_column += size;
728 if (spacer == ':')
729 pfile->deps_buffer[pfile->deps_size++] = ':';
730 pfile->deps_buffer[pfile->deps_size] = 0;
733 /* Given a colon-separated list of file names PATH,
734 add all the names to the search path for include files. */
736 static void
737 path_include (pfile, path)
738 cpp_reader *pfile;
739 char *path;
741 char *p;
743 p = path;
745 if (*p)
746 while (1) {
747 char *q = p;
748 char *name;
749 struct file_name_list *dirtmp;
751 /* Find the end of this name. */
752 while (*q != 0 && *q != PATH_SEPARATOR) q++;
753 if (p == q) {
754 /* An empty name in the path stands for the current directory. */
755 name = (char *) xmalloc (2);
756 name[0] = '.';
757 name[1] = 0;
758 } else {
759 /* Otherwise use the directory that is named. */
760 name = (char *) xmalloc (q - p + 1);
761 bcopy (p, name, q - p);
762 name[q - p] = 0;
765 dirtmp = (struct file_name_list *)
766 xmalloc (sizeof (struct file_name_list));
767 dirtmp->next = 0; /* New one goes on the end */
768 dirtmp->control_macro = 0;
769 dirtmp->c_system_include_path = 0;
770 dirtmp->fname = name;
771 dirtmp->got_name_map = 0;
772 append_include_chain (pfile, dirtmp, dirtmp);
774 /* Advance past this name. */
775 p = q;
776 if (*p == 0)
777 break;
778 /* Skip the colon. */
779 p++;
783 void
784 cpp_options_init (opts)
785 cpp_options *opts;
787 bzero ((char *) opts, sizeof *opts);
788 opts->in_fname = NULL;
789 opts->out_fname = NULL;
791 /* Initialize is_idchar to allow $. */
792 opts->dollars_in_ident = 1;
793 initialize_char_syntax (opts);
795 opts->no_line_commands = 0;
796 opts->no_trigraphs = 1;
797 opts->put_out_comments = 0;
798 opts->print_include_names = 0;
799 opts->dump_macros = dump_none;
800 opts->no_output = 0;
801 opts->remap = 0;
802 opts->cplusplus = 0;
803 opts->cplusplus_comments = 0;
805 opts->verbose = 0;
806 opts->objc = 0;
807 opts->lang_asm = 0;
808 opts->for_lint = 0;
809 opts->chill = 0;
810 opts->pedantic_errors = 0;
811 opts->inhibit_warnings = 0;
812 opts->warn_comments = 0;
813 opts->warn_import = 1;
814 opts->warnings_are_errors = 0;
817 enum cpp_token
818 null_underflow (pfile)
819 cpp_reader *pfile;
821 return CPP_EOF;
825 null_cleanup (pbuf, pfile)
826 cpp_buffer *pbuf;
827 cpp_reader *pfile;
829 return 0;
833 macro_cleanup (pbuf, pfile)
834 cpp_buffer *pbuf;
835 cpp_reader *pfile;
837 HASHNODE *macro = (HASHNODE *) pbuf->data;
838 if (macro->type == T_DISABLED)
839 macro->type = T_MACRO;
840 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
841 free (pbuf->buf);
842 return 0;
846 file_cleanup (pbuf, pfile)
847 cpp_buffer *pbuf;
848 cpp_reader *pfile;
850 if (pbuf->buf)
852 free (pbuf->buf);
853 pbuf->buf = 0;
855 return 0;
858 /* Assuming we have read '/'.
859 If this is the start of a comment (followed by '*' or '/'),
860 skip to the end of the comment, and return ' '.
861 Return EOF if we reached the end of file before the end of the comment.
862 If not the start of a comment, return '/'. */
864 static int
865 skip_comment (pfile, linep)
866 cpp_reader *pfile;
867 long *linep;
869 int c = 0;
870 while (PEEKC() == '\\' && PEEKN(1) == '\n')
872 if (linep)
873 (*linep)++;
874 FORWARD(2);
876 if (PEEKC() == '*')
878 FORWARD(1);
879 for (;;)
881 int prev_c = c;
882 c = GETC ();
883 if (c == EOF)
884 return EOF;
885 while (c == '\\' && PEEKC() == '\n')
887 if (linep)
888 (*linep)++;
889 FORWARD(1), c = GETC();
891 if (prev_c == '*' && c == '/')
892 return ' ';
893 if (c == '\n' && linep)
894 (*linep)++;
897 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
899 FORWARD(1);
900 for (;;)
902 c = GETC ();
903 if (c == EOF)
904 return ' '; /* Allow // to be terminated by EOF. */
905 while (c == '\\' && PEEKC() == '\n')
907 FORWARD(1);
908 c = GETC();
909 if (linep)
910 (*linep)++;
912 if (c == '\n')
914 /* Don't consider final '\n' to be part of comment. */
915 FORWARD(-1);
916 return ' ';
920 else
921 return '/';
924 /* Skip whitespace \-newline and comments. Does not macro-expand. */
926 void
927 cpp_skip_hspace (pfile)
928 cpp_reader *pfile;
930 while (1)
932 int c = PEEKC();
933 if (c == EOF)
934 return; /* FIXME */
935 if (is_hor_space[c])
937 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
938 cpp_pedwarn (pfile, "%s in preprocessing directive",
939 c == '\f' ? "formfeed" : "vertical tab");
940 FORWARD(1);
942 else if (c == '/')
944 FORWARD (1);
945 c = skip_comment (pfile, NULL);
946 if (c == '/')
947 FORWARD(-1);
948 if (c == EOF || c == '/')
949 return;
951 else if (c == '\\' && PEEKN(1) == '\n') {
952 FORWARD(2);
954 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
955 && is_hor_space[PEEKN(1)])
956 FORWARD(2);
957 else return;
961 /* Read the rest of the current line.
962 The line is appended to PFILE's output buffer. */
964 static void
965 copy_rest_of_line (pfile)
966 cpp_reader *pfile;
968 struct cpp_options *opts = CPP_OPTIONS (pfile);
969 for (;;)
971 int c = GETC();
972 int nextc;
973 switch (c)
975 case EOF:
976 goto end_directive;
977 case '\\':
978 if (PEEKC() == '\n')
980 FORWARD (1);
981 continue;
983 case '\'':
984 case '\"':
985 goto scan_directive_token;
986 break;
987 case '/':
988 nextc = PEEKC();
989 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
990 goto scan_directive_token;
991 break;
992 case '\f':
993 case '\v':
994 if (CPP_PEDANTIC (pfile))
995 cpp_pedwarn (pfile, "%s in preprocessing directive",
996 c == '\f' ? "formfeed" : "vertical tab");
997 break;
999 case '\n':
1000 FORWARD(-1);
1001 goto end_directive;
1002 scan_directive_token:
1003 FORWARD(-1);
1004 cpp_get_token (pfile);
1005 continue;
1007 CPP_PUTC (pfile, c);
1009 end_directive: ;
1010 CPP_NUL_TERMINATE (pfile);
1013 void
1014 skip_rest_of_line (pfile)
1015 cpp_reader *pfile;
1017 long old = CPP_WRITTEN (pfile);
1018 copy_rest_of_line (pfile);
1019 CPP_SET_WRITTEN (pfile, old);
1022 /* Handle a possible # directive.
1023 '#' has already been read. */
1026 handle_directive (pfile)
1027 cpp_reader *pfile;
1028 { int c;
1029 register struct directive *kt;
1030 int ident_length;
1031 long after_ident;
1032 U_CHAR *ident, *line_end;
1033 long old_written = CPP_WRITTEN (pfile);
1035 cpp_skip_hspace (pfile);
1037 c = PEEKC ();
1038 if (c >= '0' && c <= '9')
1040 /* Handle # followed by a line number. */
1041 if (CPP_PEDANTIC (pfile))
1042 cpp_pedwarn (pfile, "`#' followed by integer");
1043 do_line (pfile, NULL);
1044 goto done_a_directive;
1047 /* Now find the directive name. */
1048 CPP_PUTC (pfile, '#');
1049 parse_name (pfile, GETC());
1050 ident = pfile->token_buffer + old_written + 1;
1051 ident_length = CPP_PWRITTEN (pfile) - ident;
1052 if (ident_length == 0 && PEEKC() == '\n')
1054 /* A line of just `#' becomes blank. */
1055 goto done_a_directive;
1058 #if 0
1059 if (ident_length == 0 || !is_idstart[*ident]) {
1060 U_CHAR *p = ident;
1061 while (is_idchar[*p]) {
1062 if (*p < '0' || *p > '9')
1063 break;
1064 p++;
1066 /* Avoid error for `###' and similar cases unless -pedantic. */
1067 if (p == ident) {
1068 while (*p == '#' || is_hor_space[*p]) p++;
1069 if (*p == '\n') {
1070 if (pedantic && !lang_asm)
1071 cpp_warning (pfile, "invalid preprocessor directive");
1072 return 0;
1076 if (!lang_asm)
1077 cpp_error (pfile, "invalid preprocessor directive name");
1079 return 0;
1081 #endif
1083 * Decode the keyword and call the appropriate expansion
1084 * routine, after moving the input pointer up to the next line.
1086 for (kt = directive_table; ; kt++) {
1087 if (kt->length <= 0)
1088 goto not_a_directive;
1089 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1090 break;
1093 if (kt->command_reads_line)
1094 after_ident = 0;
1095 else
1097 /* Nonzero means do not delete comments within the directive.
1098 #define needs this when -traditional. */
1099 int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE;
1100 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1101 CPP_OPTIONS (pfile)->put_out_comments = comments;
1102 after_ident = CPP_WRITTEN (pfile);
1103 copy_rest_of_line (pfile);
1104 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1107 /* We may want to pass through #define, #pragma, and #include.
1108 Other directives may create output, but we don't want the directive
1109 itself out, so we pop it now. For example conditionals may emit
1110 #failed ... #endfailed stuff. But note that popping the buffer
1111 means the parameters to kt->func may point after pfile->limit
1112 so these parameters are invalid as soon as something gets appended
1113 to the token_buffer. */
1115 line_end = CPP_PWRITTEN (pfile);
1116 if (! (kt->type == T_DEFINE
1117 || kt->type == T_PRAGMA
1118 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
1119 && CPP_OPTIONS (pfile)->dump_includes)))
1120 CPP_SET_WRITTEN (pfile, old_written);
1122 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1124 if (kt->type == T_DEFINE)
1126 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
1128 /* Skip "#define". */
1129 U_CHAR *p = pfile->token_buffer + old_written + 7;
1131 SKIP_WHITE_SPACE (p);
1132 while (is_idchar[*p]) p++;
1133 pfile->limit = p;
1134 CPP_PUTC (pfile, '\n');
1136 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
1137 CPP_SET_WRITTEN (pfile, old_written);
1140 done_a_directive:
1141 return 1;
1143 not_a_directive:
1144 return 0;
1147 /* Pass a directive through to the output file.
1148 BUF points to the contents of the directive, as a contiguous string.
1149 LIMIT points to the first character past the end of the directive.
1150 KEYWORD is the keyword-table entry for the directive. */
1152 static void
1153 pass_thru_directive (buf, limit, pfile, keyword)
1154 U_CHAR *buf, *limit;
1155 cpp_reader *pfile;
1156 struct directive *keyword;
1158 register unsigned keyword_length = keyword->length;
1160 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1161 CPP_PUTC_Q (pfile, '#');
1162 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1163 if (limit != buf && buf[0] != ' ')
1164 CPP_PUTC_Q (pfile, ' ');
1165 CPP_PUTS_Q (pfile, buf, limit - buf);
1166 #if 0
1167 CPP_PUTS_Q (pfile, '\n');
1168 /* Count the line we have just made in the output,
1169 to get in sync properly. */
1170 pfile->lineno++;
1171 #endif
1174 /* The arglist structure is built by do_define to tell
1175 collect_definition where the argument names begin. That
1176 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1177 would contain pointers to the strings x, y, and z.
1178 Collect_definition would then build a DEFINITION node,
1179 with reflist nodes pointing to the places x, y, and z had
1180 appeared. So the arglist is just convenience data passed
1181 between these two routines. It is not kept around after
1182 the current #define has been processed and entered into the
1183 hash table. */
1185 struct arglist {
1186 struct arglist *next;
1187 U_CHAR *name;
1188 int length;
1189 int argno;
1190 char rest_args;
1193 /* Read a replacement list for a macro with parameters.
1194 Build the DEFINITION structure.
1195 Reads characters of text starting at BUF until END.
1196 ARGLIST specifies the formal parameters to look for
1197 in the text of the definition; NARGS is the number of args
1198 in that list, or -1 for a macro name that wants no argument list.
1199 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1200 and NAMELEN is its length in characters.
1202 Note that comments, backslash-newlines, and leading white space
1203 have already been deleted from the argument. */
1205 static DEFINITION *
1206 collect_expansion (pfile, buf, limit, nargs, arglist)
1207 cpp_reader *pfile;
1208 U_CHAR *buf, *limit;
1209 int nargs;
1210 struct arglist *arglist;
1212 DEFINITION *defn;
1213 register U_CHAR *p, *lastp, *exp_p;
1214 struct reflist *endpat = NULL;
1215 /* Pointer to first nonspace after last ## seen. */
1216 U_CHAR *concat = 0;
1217 /* Pointer to first nonspace after last single-# seen. */
1218 U_CHAR *stringify = 0;
1219 int maxsize;
1220 int expected_delimiter = '\0';
1222 /* Scan thru the replacement list, ignoring comments and quoted
1223 strings, picking up on the macro calls. It does a linear search
1224 thru the arg list on every potential symbol. Profiling might say
1225 that something smarter should happen. */
1227 if (limit < buf)
1228 abort ();
1230 /* Find the beginning of the trailing whitespace. */
1231 p = buf;
1232 while (p < limit && is_space[limit[-1]]) limit--;
1234 /* Allocate space for the text in the macro definition.
1235 Leading and trailing whitespace chars need 2 bytes each.
1236 Each other input char may or may not need 1 byte,
1237 so this is an upper bound. The extra 5 are for invented
1238 leading and trailing newline-marker and final null. */
1239 maxsize = (sizeof (DEFINITION)
1240 + (limit - p) + 5);
1241 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1242 while (p < limit)
1243 if (*p++ == '@')
1244 maxsize++;
1245 defn = (DEFINITION *) xcalloc (1, maxsize);
1247 defn->nargs = nargs;
1248 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1249 lastp = exp_p;
1251 p = buf;
1253 /* Add one initial space escape-marker to prevent accidental
1254 token-pasting (often removed by macroexpand). */
1255 *exp_p++ = '@';
1256 *exp_p++ = ' ';
1258 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1259 cpp_error (pfile, "`##' at start of macro definition");
1260 p += 2;
1263 /* Process the main body of the definition. */
1264 while (p < limit) {
1265 int skipped_arg = 0;
1266 register U_CHAR c = *p++;
1268 *exp_p++ = c;
1270 if (!CPP_TRADITIONAL (pfile)) {
1271 switch (c) {
1272 case '\'':
1273 case '\"':
1274 if (expected_delimiter != '\0') {
1275 if (c == expected_delimiter)
1276 expected_delimiter = '\0';
1277 } else
1278 expected_delimiter = c;
1279 break;
1281 case '\\':
1282 if (p < limit && expected_delimiter) {
1283 /* In a string, backslash goes through
1284 and makes next char ordinary. */
1285 *exp_p++ = *p++;
1287 break;
1289 case '@':
1290 /* An '@' in a string or character constant stands for itself,
1291 and does not need to be escaped. */
1292 if (!expected_delimiter)
1293 *exp_p++ = c;
1294 break;
1296 case '#':
1297 /* # is ordinary inside a string. */
1298 if (expected_delimiter)
1299 break;
1300 if (p < limit && *p == '#') {
1301 /* ##: concatenate preceding and following tokens. */
1302 /* Take out the first #, discard preceding whitespace. */
1303 exp_p--;
1304 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1305 --exp_p;
1306 /* Skip the second #. */
1307 p++;
1308 /* Discard following whitespace. */
1309 SKIP_WHITE_SPACE (p);
1310 concat = p;
1311 if (p == limit)
1312 cpp_error (pfile, "`##' at end of macro definition");
1313 } else if (nargs >= 0) {
1314 /* Single #: stringify following argument ref.
1315 Don't leave the # in the expansion. */
1316 exp_p--;
1317 SKIP_WHITE_SPACE (p);
1318 if (p == limit || ! is_idstart[*p]
1319 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1320 cpp_error (pfile,
1321 "`#' operator is not followed by a macro argument name");
1322 else
1323 stringify = p;
1325 break;
1327 } else {
1328 /* In -traditional mode, recognize arguments inside strings and
1329 and character constants, and ignore special properties of #.
1330 Arguments inside strings are considered "stringified", but no
1331 extra quote marks are supplied. */
1332 switch (c) {
1333 case '\'':
1334 case '\"':
1335 if (expected_delimiter != '\0') {
1336 if (c == expected_delimiter)
1337 expected_delimiter = '\0';
1338 } else
1339 expected_delimiter = c;
1340 break;
1342 case '\\':
1343 /* Backslash quotes delimiters and itself, but not macro args. */
1344 if (expected_delimiter != 0 && p < limit
1345 && (*p == expected_delimiter || *p == '\\')) {
1346 *exp_p++ = *p++;
1347 continue;
1349 break;
1351 case '/':
1352 if (expected_delimiter != '\0') /* No comments inside strings. */
1353 break;
1354 if (*p == '*') {
1355 /* If we find a comment that wasn't removed by handle_directive,
1356 this must be -traditional. So replace the comment with
1357 nothing at all. */
1358 exp_p--;
1359 p += 1;
1360 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1361 p++;
1362 #if 0
1363 /* Mark this as a concatenation-point, as if it had been ##. */
1364 concat = p;
1365 #endif
1367 break;
1371 /* Handle the start of a symbol. */
1372 if (is_idchar[c] && nargs > 0) {
1373 U_CHAR *id_beg = p - 1;
1374 int id_len;
1376 --exp_p;
1377 while (p != limit && is_idchar[*p]) p++;
1378 id_len = p - id_beg;
1380 if (is_idstart[c]
1381 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1382 register struct arglist *arg;
1384 for (arg = arglist; arg != NULL; arg = arg->next) {
1385 struct reflist *tpat;
1387 if (arg->name[0] == c
1388 && arg->length == id_len
1389 && strncmp (arg->name, id_beg, id_len) == 0) {
1390 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1391 if (CPP_TRADITIONAL (pfile)) {
1392 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1393 id_len, arg->name);
1394 } else {
1395 cpp_warning (pfile,
1396 "macro arg `%.*s' would be stringified with -traditional.",
1397 id_len, arg->name);
1400 /* If ANSI, don't actually substitute inside a string. */
1401 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1402 break;
1403 /* make a pat node for this arg and append it to the end of
1404 the pat list */
1405 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1406 tpat->next = NULL;
1407 tpat->raw_before = concat == id_beg;
1408 tpat->raw_after = 0;
1409 tpat->rest_args = arg->rest_args;
1410 tpat->stringify = (CPP_TRADITIONAL (pfile)
1411 ? expected_delimiter != '\0'
1412 : stringify == id_beg);
1414 if (endpat == NULL)
1415 defn->pattern = tpat;
1416 else
1417 endpat->next = tpat;
1418 endpat = tpat;
1420 tpat->argno = arg->argno;
1421 tpat->nchars = exp_p - lastp;
1423 register U_CHAR *p1 = p;
1424 SKIP_WHITE_SPACE (p1);
1425 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1426 tpat->raw_after = 1;
1428 lastp = exp_p; /* place to start copying from next time */
1429 skipped_arg = 1;
1430 break;
1435 /* If this was not a macro arg, copy it into the expansion. */
1436 if (! skipped_arg) {
1437 register U_CHAR *lim1 = p;
1438 p = id_beg;
1439 while (p != lim1)
1440 *exp_p++ = *p++;
1441 if (stringify == id_beg)
1442 cpp_error (pfile,
1443 "`#' operator should be followed by a macro argument name");
1448 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1450 /* If ANSI, put in a "@ " marker to prevent token pasting.
1451 But not if "inside a string" (which in ANSI mode
1452 happens only for -D option). */
1453 *exp_p++ = '@';
1454 *exp_p++ = ' ';
1457 *exp_p = '\0';
1459 defn->length = exp_p - defn->expansion;
1461 /* Crash now if we overrun the allocated size. */
1462 if (defn->length + 1 > maxsize)
1463 abort ();
1465 #if 0
1466 /* This isn't worth the time it takes. */
1467 /* give back excess storage */
1468 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1469 #endif
1471 return defn;
1475 * special extension string that can be added to the last macro argument to
1476 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1477 * #define wow(a, b...) process (b, a, b)
1478 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1479 * { wow (one, two); } -> { process (two, one, two); }
1480 * if this "rest_arg" is used with the concat token '##' and if it is not
1481 * supplied then the token attached to with ## will not be outputted. Ex:
1482 * #define wow (a, b...) process (b ## , a, ## b)
1483 * { wow (1, 2); } -> { process (2, 1, 2); }
1484 * { wow (one); } -> { process (one); {
1486 static char rest_extension[] = "...";
1487 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1489 /* Create a DEFINITION node from a #define directive. Arguments are
1490 as for do_define. */
1492 static MACRODEF
1493 create_definition (buf, limit, pfile, predefinition)
1494 U_CHAR *buf, *limit;
1495 cpp_reader *pfile;
1496 int predefinition;
1498 U_CHAR *bp; /* temp ptr into input buffer */
1499 U_CHAR *symname; /* remember where symbol name starts */
1500 int sym_length; /* and how long it is */
1501 int rest_args = 0;
1502 long line, col;
1503 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1504 DEFINITION *defn;
1505 int arglengths = 0; /* Accumulate lengths of arg names
1506 plus number of args. */
1507 MACRODEF mdef;
1508 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1510 bp = buf;
1512 while (is_hor_space[*bp])
1513 bp++;
1515 symname = bp; /* remember where it starts */
1517 sym_length = check_macro_name (pfile, bp, "macro");
1518 bp += sym_length;
1520 /* Lossage will occur if identifiers or control keywords are broken
1521 across lines using backslash. This is not the right place to take
1522 care of that. */
1524 if (*bp == '(') {
1525 struct arglist *arg_ptrs = NULL;
1526 int argno = 0;
1528 bp++; /* skip '(' */
1529 SKIP_WHITE_SPACE (bp);
1531 /* Loop over macro argument names. */
1532 while (*bp != ')') {
1533 struct arglist *temp;
1535 temp = (struct arglist *) alloca (sizeof (struct arglist));
1536 temp->name = bp;
1537 temp->next = arg_ptrs;
1538 temp->argno = argno++;
1539 temp->rest_args = 0;
1540 arg_ptrs = temp;
1542 if (rest_args)
1543 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1545 if (!is_idstart[*bp])
1546 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1548 /* Find the end of the arg name. */
1549 while (is_idchar[*bp]) {
1550 bp++;
1551 /* do we have a "special" rest-args extension here? */
1552 if (limit - bp > REST_EXTENSION_LENGTH
1553 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1554 rest_args = 1;
1555 temp->rest_args = 1;
1556 break;
1559 temp->length = bp - temp->name;
1560 if (rest_args == 1)
1561 bp += REST_EXTENSION_LENGTH;
1562 arglengths += temp->length + 2;
1563 SKIP_WHITE_SPACE (bp);
1564 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1565 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1566 goto nope;
1568 if (*bp == ',') {
1569 bp++;
1570 SKIP_WHITE_SPACE (bp);
1572 if (bp >= limit) {
1573 cpp_error (pfile, "unterminated parameter list in `#define'");
1574 goto nope;
1577 struct arglist *otemp;
1579 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1580 if (temp->length == otemp->length
1581 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1582 U_CHAR *name;
1584 name = (U_CHAR *) alloca (temp->length + 1);
1585 (void) strncpy (name, temp->name, temp->length);
1586 name[temp->length] = '\0';
1587 cpp_error (pfile,
1588 "duplicate argument name `%s' in `#define'", name);
1589 goto nope;
1594 ++bp; /* skip paren */
1595 SKIP_WHITE_SPACE (bp);
1596 /* now everything from bp before limit is the definition. */
1597 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1598 defn->rest_args = rest_args;
1600 /* Now set defn->args.argnames to the result of concatenating
1601 the argument names in reverse order
1602 with comma-space between them. */
1603 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1605 struct arglist *temp;
1606 int i = 0;
1607 for (temp = arg_ptrs; temp; temp = temp->next) {
1608 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1609 i += temp->length;
1610 if (temp->next != 0) {
1611 defn->args.argnames[i++] = ',';
1612 defn->args.argnames[i++] = ' ';
1615 defn->args.argnames[i] = 0;
1617 } else {
1618 /* Simple expansion or empty definition. */
1620 if (bp < limit)
1622 if (is_hor_space[*bp]) {
1623 bp++;
1624 SKIP_WHITE_SPACE (bp);
1625 } else {
1626 switch (*bp) {
1627 case '!': case '"': case '#': case '%': case '&': case '\'':
1628 case ')': case '*': case '+': case ',': case '-': case '.':
1629 case '/': case ':': case ';': case '<': case '=': case '>':
1630 case '?': case '[': case '\\': case ']': case '^': case '{':
1631 case '|': case '}': case '~':
1632 cpp_warning (pfile, "missing white space after `#define %.*s'",
1633 sym_length, symname);
1634 break;
1636 default:
1637 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1638 sym_length, symname);
1639 break;
1643 /* now everything from bp before limit is the definition. */
1644 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1645 defn->args.argnames = (U_CHAR *) "";
1648 defn->line = line;
1649 defn->file = file;
1651 /* OP is null if this is a predefinition */
1652 defn->predefined = predefinition;
1653 mdef.defn = defn;
1654 mdef.symnam = symname;
1655 mdef.symlen = sym_length;
1657 return mdef;
1659 nope:
1660 mdef.defn = 0;
1661 return mdef;
1664 /* Check a purported macro name SYMNAME, and yield its length.
1665 USAGE is the kind of name this is intended for. */
1667 static int
1668 check_macro_name (pfile, symname, usage)
1669 cpp_reader *pfile;
1670 U_CHAR *symname;
1671 char *usage;
1673 U_CHAR *p;
1674 int sym_length;
1676 for (p = symname; is_idchar[*p]; p++)
1678 sym_length = p - symname;
1679 if (sym_length == 0
1680 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1681 cpp_error (pfile, "invalid %s name", usage);
1682 else if (!is_idstart[*symname]) {
1683 U_CHAR *msg; /* what pain... */
1684 msg = (U_CHAR *) alloca (sym_length + 1);
1685 bcopy (symname, msg, sym_length);
1686 msg[sym_length] = 0;
1687 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1688 } else {
1689 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1690 cpp_error (pfile, "invalid %s name `defined'", usage);
1692 return sym_length;
1695 /* Return zero if two DEFINITIONs are isomorphic. */
1697 static int
1698 compare_defs (pfile, d1, d2)
1699 cpp_reader *pfile;
1700 DEFINITION *d1, *d2;
1702 register struct reflist *a1, *a2;
1703 register U_CHAR *p1 = d1->expansion;
1704 register U_CHAR *p2 = d2->expansion;
1705 int first = 1;
1707 if (d1->nargs != d2->nargs)
1708 return 1;
1709 if (CPP_PEDANTIC (pfile)
1710 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1711 return 1;
1712 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1713 a1 = a1->next, a2 = a2->next) {
1714 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1715 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1716 || a1->argno != a2->argno
1717 || a1->stringify != a2->stringify
1718 || a1->raw_before != a2->raw_before
1719 || a1->raw_after != a2->raw_after)
1720 return 1;
1721 first = 0;
1722 p1 += a1->nchars;
1723 p2 += a2->nchars;
1725 if (a1 != a2)
1726 return 1;
1727 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1728 p2, d2->length - (p2 - d2->expansion), 1))
1729 return 1;
1730 return 0;
1733 /* Return 1 if two parts of two macro definitions are effectively different.
1734 One of the parts starts at BEG1 and has LEN1 chars;
1735 the other has LEN2 chars at BEG2.
1736 Any sequence of whitespace matches any other sequence of whitespace.
1737 FIRST means these parts are the first of a macro definition;
1738 so ignore leading whitespace entirely.
1739 LAST means these parts are the last of a macro definition;
1740 so ignore trailing whitespace entirely. */
1742 static int
1743 comp_def_part (first, beg1, len1, beg2, len2, last)
1744 int first;
1745 U_CHAR *beg1, *beg2;
1746 int len1, len2;
1747 int last;
1749 register U_CHAR *end1 = beg1 + len1;
1750 register U_CHAR *end2 = beg2 + len2;
1751 if (first) {
1752 while (beg1 != end1 && is_space[*beg1]) beg1++;
1753 while (beg2 != end2 && is_space[*beg2]) beg2++;
1755 if (last) {
1756 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1757 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1759 while (beg1 != end1 && beg2 != end2) {
1760 if (is_space[*beg1] && is_space[*beg2]) {
1761 while (beg1 != end1 && is_space[*beg1]) beg1++;
1762 while (beg2 != end2 && is_space[*beg2]) beg2++;
1763 } else if (*beg1 == *beg2) {
1764 beg1++; beg2++;
1765 } else break;
1767 return (beg1 != end1) || (beg2 != end2);
1770 /* Process a #define command.
1771 BUF points to the contents of the #define command, as a contiguous string.
1772 LIMIT points to the first character past the end of the definition.
1773 KEYWORD is the keyword-table entry for #define,
1774 or NULL for a "predefined" macro. */
1776 static int
1777 do_define (pfile, keyword, buf, limit)
1778 cpp_reader *pfile;
1779 struct directive *keyword;
1780 U_CHAR *buf, *limit;
1782 int hashcode;
1783 MACRODEF mdef;
1784 HASHNODE *hp;
1786 #if 0
1787 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1788 if (pcp_outfile && keyword)
1789 pass_thru_directive (buf, limit, pfile, keyword);
1790 #endif
1792 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1793 if (mdef.defn == 0)
1794 goto nope;
1796 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1798 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1800 int ok = 0;
1801 /* Redefining a precompiled key is ok. */
1802 if (hp->type == T_PCSTRING)
1803 ok = 1;
1804 /* Redefining a macro is ok if the definitions are the same. */
1805 else if (hp->type == T_MACRO)
1806 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1807 /* Redefining a constant is ok with -D. */
1808 else if (hp->type == T_CONST)
1809 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1810 /* Print the warning if it's not ok. */
1811 if (!ok)
1813 U_CHAR *msg; /* what pain... */
1815 /* If we are passing through #define and #undef directives, do
1816 that for this re-definition now. */
1817 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1818 pass_thru_directive (buf, limit, pfile, keyword);
1820 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1821 *msg = '`';
1822 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1823 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1824 cpp_pedwarn (pfile, msg);
1825 if (hp->type == T_MACRO)
1826 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1827 "this is the location of the previous definition");
1829 /* Replace the old definition. */
1830 hp->type = T_MACRO;
1831 hp->value.defn = mdef.defn;
1833 else
1835 /* If we are passing through #define and #undef directives, do
1836 that for this new definition now. */
1837 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1838 pass_thru_directive (buf, limit, pfile, keyword);
1839 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1840 (char *) mdef.defn, hashcode);
1843 return 0;
1845 nope:
1847 return 1;
1850 /* This structure represents one parsed argument in a macro call.
1851 `raw' points to the argument text as written (`raw_length' is its length).
1852 `expanded' points to the argument's macro-expansion
1853 (its length is `expand_length').
1854 `stringified_length' is the length the argument would have
1855 if stringified.
1856 `use_count' is the number of times this macro arg is substituted
1857 into the macro. If the actual use count exceeds 10,
1858 the value stored is 10. */
1860 /* raw and expanded are relative to ARG_BASE */
1861 #define ARG_BASE ((pfile)->token_buffer)
1863 struct argdata {
1864 /* Strings relative to pfile->token_buffer */
1865 long raw, expanded, stringified;
1866 int raw_length, expand_length;
1867 int stringified_length;
1868 char newlines;
1869 char use_count;
1872 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1873 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1874 as the new input buffer.
1875 Return the new buffer, or NULL on failure. */
1877 cpp_buffer *
1878 cpp_push_buffer (pfile, buffer, length)
1879 cpp_reader *pfile;
1880 U_CHAR *buffer;
1881 long length;
1883 register cpp_buffer *buf = CPP_BUFFER (pfile);
1884 if (buf == pfile->buffer_stack)
1886 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1887 buf->fname);
1888 return NULL;
1890 buf--;
1891 bzero ((char *) buf, sizeof (cpp_buffer));
1892 CPP_BUFFER (pfile) = buf;
1893 buf->if_stack = pfile->if_stack;
1894 buf->cleanup = null_cleanup;
1895 buf->underflow = null_underflow;
1896 buf->buf = buf->cur = buffer;
1897 buf->alimit = buf->rlimit = buffer + length;
1899 return buf;
1902 cpp_buffer *
1903 cpp_pop_buffer (pfile)
1904 cpp_reader *pfile;
1906 cpp_buffer *buf = CPP_BUFFER (pfile);
1907 (*buf->cleanup) (buf, pfile);
1908 return ++CPP_BUFFER (pfile);
1911 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1912 Pop the buffer when done. */
1914 void
1915 cpp_scan_buffer (pfile)
1916 cpp_reader *pfile;
1918 cpp_buffer *buffer = CPP_BUFFER (pfile);
1919 for (;;)
1921 enum cpp_token token = cpp_get_token (pfile);
1922 if (token == CPP_EOF) /* Should not happen ... */
1923 break;
1924 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1926 cpp_pop_buffer (pfile);
1927 break;
1933 * Rescan a string (which may have escape marks) into pfile's buffer.
1934 * Place the result in pfile->token_buffer.
1936 * The input is copied before it is scanned, so it is safe to pass
1937 * it something from the token_buffer that will get overwritten
1938 * (because it follows CPP_WRITTEN). This is used by do_include.
1941 static void
1942 cpp_expand_to_buffer (pfile, buf, length)
1943 cpp_reader *pfile;
1944 U_CHAR *buf;
1945 int length;
1947 register cpp_buffer *ip;
1948 #if 0
1949 cpp_buffer obuf;
1950 #endif
1951 U_CHAR *limit = buf + length;
1952 U_CHAR *buf1;
1953 #if 0
1954 int odepth = indepth;
1955 #endif
1957 if (length < 0)
1958 abort ();
1960 /* Set up the input on the input stack. */
1962 buf1 = (U_CHAR *) alloca (length + 1);
1964 register U_CHAR *p1 = buf;
1965 register U_CHAR *p2 = buf1;
1967 while (p1 != limit)
1968 *p2++ = *p1++;
1970 buf1[length] = 0;
1972 ip = cpp_push_buffer (pfile, buf1, length);
1973 if (ip == NULL)
1974 return;
1975 ip->has_escapes = 1;
1976 #if 0
1977 ip->lineno = obuf.lineno = 1;
1978 #endif
1980 /* Scan the input, create the output. */
1981 cpp_scan_buffer (pfile);
1983 #if 0
1984 if (indepth != odepth)
1985 abort ();
1986 #endif
1988 CPP_NUL_TERMINATE (pfile);
1992 static void
1993 adjust_position (buf, limit, linep, colp)
1994 U_CHAR *buf;
1995 U_CHAR *limit;
1996 long *linep;
1997 long *colp;
1999 while (buf < limit)
2001 U_CHAR ch = *buf++;
2002 if (ch == '\n')
2003 (*linep)++, (*colp) = 1;
2004 else
2005 (*colp)++;
2009 /* Move line_base forward, updating lineno and colno. */
2011 static void
2012 update_position (pbuf)
2013 register cpp_buffer *pbuf;
2015 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2016 unsigned char *new_pos = pbuf->cur;
2017 register struct parse_marker *mark;
2018 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2020 if (pbuf->buf + mark->position < new_pos)
2021 new_pos = pbuf->buf + mark->position;
2023 pbuf->line_base += new_pos - old_pos;
2024 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2027 void
2028 cpp_buf_line_and_col (pbuf, linep, colp)
2029 register cpp_buffer *pbuf;
2030 long *linep, *colp;
2032 long dummy;
2033 if (colp == NULL)
2034 colp = &dummy;
2035 if (pbuf)
2037 *linep = pbuf->lineno;
2038 *colp = pbuf->colno;
2039 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2041 else
2043 *linep = 0;
2044 *colp = 0;
2048 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2050 cpp_buffer *
2051 cpp_file_buffer (pfile)
2052 cpp_reader *pfile;
2054 cpp_buffer *ip = CPP_BUFFER (pfile);
2056 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2057 if (ip->fname != NULL)
2058 return ip;
2059 return NULL;
2062 static long
2063 count_newlines (buf, limit)
2064 register U_CHAR *buf;
2065 register U_CHAR *limit;
2067 register long count = 0;
2068 while (buf < limit)
2070 U_CHAR ch = *buf++;
2071 if (ch == '\n')
2072 count++;
2074 return count;
2078 * write out a #line command, for instance, after an #include file.
2079 * If CONDITIONAL is nonzero, we can omit the #line if it would
2080 * appear to be a no-op, and we can output a few newlines instead
2081 * if we want to increase the line number by a small amount.
2082 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2085 static void
2086 output_line_command (pfile, conditional, file_change)
2087 cpp_reader *pfile;
2088 int conditional;
2089 enum file_change_code file_change;
2091 long line, col;
2092 cpp_buffer *ip = CPP_BUFFER (pfile);
2094 if (ip->fname == NULL)
2095 return;
2097 update_position (ip);
2099 if (CPP_OPTIONS (pfile)->no_line_commands
2100 || CPP_OPTIONS (pfile)->no_output)
2101 return;
2103 line = CPP_BUFFER (pfile)->lineno;
2104 col = CPP_BUFFER (pfile)->colno;
2105 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2107 if (CPP_OPTIONS (pfile)->no_line_commands)
2108 return;
2110 if (conditional) {
2111 if (line == pfile->lineno)
2112 return;
2114 /* If the inherited line number is a little too small,
2115 output some newlines instead of a #line command. */
2116 if (line > pfile->lineno && line < pfile->lineno + 8) {
2117 CPP_RESERVE (pfile, 20);
2118 while (line > pfile->lineno) {
2119 CPP_PUTC_Q (pfile, '\n');
2120 pfile->lineno++;
2122 return;
2126 #if 0
2127 /* Don't output a line number of 0 if we can help it. */
2128 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2129 && *ip->bufp == '\n') {
2130 ip->lineno++;
2131 ip->bufp++;
2133 #endif
2135 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2137 #ifdef OUTPUT_LINE_COMMANDS
2138 static char sharp_line[] = "#line ";
2139 #else
2140 static char sharp_line[] = "# ";
2141 #endif
2142 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2145 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
2146 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2148 quote_string (pfile, ip->nominal_fname);
2149 if (file_change != same_file) {
2150 CPP_PUTC_Q (pfile, ' ');
2151 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2153 /* Tell cc1 if following text comes from a system header file. */
2154 if (ip->system_header_p) {
2155 CPP_PUTC_Q (pfile, ' ');
2156 CPP_PUTC_Q (pfile, '3');
2158 #ifndef NO_IMPLICIT_EXTERN_C
2159 /* Tell cc1plus if following text should be treated as C. */
2160 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2161 CPP_PUTC_Q (pfile, ' ');
2162 CPP_PUTC_Q (pfile, '4');
2164 #endif
2165 CPP_PUTC_Q (pfile, '\n');
2166 pfile->lineno = line;
2170 * Parse a macro argument and append the info on PFILE's token_buffer.
2171 * REST_ARGS means to absorb the rest of the args.
2172 * Return nonzero to indicate a syntax error.
2175 static enum cpp_token
2176 macarg (pfile, rest_args)
2177 cpp_reader *pfile;
2178 int rest_args;
2180 int paren = 0;
2181 enum cpp_token token;
2182 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2183 CPP_OPTIONS (pfile)->put_out_comments = 0;
2185 /* Try to parse as much of the argument as exists at this
2186 input stack level. */
2187 pfile->no_macro_expand++;
2188 for (;;)
2190 token = cpp_get_token (pfile);
2191 switch (token)
2193 case CPP_EOF:
2194 goto done;
2195 case CPP_POP:
2196 /* If we've hit end of file, it's an error (reported by caller).
2197 Ditto if it's the end of cpp_expand_to_buffer text.
2198 If we've hit end of macro, just continue. */
2199 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2200 goto done;
2201 break;
2202 case CPP_LPAREN:
2203 paren++;
2204 break;
2205 case CPP_RPAREN:
2206 if (--paren < 0)
2207 goto found;
2208 break;
2209 case CPP_COMMA:
2210 /* if we've returned to lowest level and
2211 we aren't absorbing all args */
2212 if (paren == 0 && rest_args == 0)
2213 goto found;
2214 break;
2215 found:
2216 /* Remove ',' or ')' from argument buffer. */
2217 CPP_ADJUST_WRITTEN (pfile, -1);
2218 goto done;
2219 default: ;
2223 done:
2224 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2225 pfile->no_macro_expand--;
2227 return token;
2230 /* Turn newlines to spaces in the string of length LENGTH at START,
2231 except inside of string constants.
2232 The string is copied into itself with its beginning staying fixed. */
2234 static int
2235 change_newlines (start, length)
2236 U_CHAR *start;
2237 int length;
2239 register U_CHAR *ibp;
2240 register U_CHAR *obp;
2241 register U_CHAR *limit;
2242 register int c;
2244 ibp = start;
2245 limit = start + length;
2246 obp = start;
2248 while (ibp < limit) {
2249 *obp++ = c = *ibp++;
2250 switch (c) {
2252 case '\'':
2253 case '\"':
2254 /* Notice and skip strings, so that we don't delete newlines in them. */
2256 int quotec = c;
2257 while (ibp < limit) {
2258 *obp++ = c = *ibp++;
2259 if (c == quotec)
2260 break;
2261 if (c == '\n' && quotec == '\'')
2262 break;
2265 break;
2269 return obp - start;
2273 static struct tm *
2274 timestamp (pfile)
2275 cpp_reader *pfile;
2277 if (!pfile->timebuf) {
2278 time_t t = time ((time_t *) 0);
2279 pfile->timebuf = localtime (&t);
2281 return pfile->timebuf;
2284 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2285 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2289 * expand things like __FILE__. Place the expansion into the output
2290 * buffer *without* rescanning.
2293 static void
2294 special_symbol (hp, pfile)
2295 HASHNODE *hp;
2296 cpp_reader *pfile;
2298 char *buf;
2299 int len;
2300 int true_indepth;
2301 cpp_buffer *ip = NULL;
2302 struct tm *timebuf;
2304 int paren = 0; /* For special `defined' keyword */
2306 #if 0
2307 if (pcp_outfile && pcp_inside_if
2308 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2309 cpp_error (pfile,
2310 "Predefined macro `%s' used inside `#if' during precompilation",
2311 hp->name);
2312 #endif
2314 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2316 if (ip == CPP_NULL_BUFFER (pfile))
2318 cpp_error (pfile, "cccp error: not in any file?!");
2319 return; /* the show must go on */
2321 if (ip->fname != NULL)
2322 break;
2325 switch (hp->type)
2327 case T_FILE:
2328 case T_BASE_FILE:
2330 char *string;
2331 if (hp->type == T_BASE_FILE)
2333 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2334 ip = CPP_PREV_BUFFER (ip);
2336 string = ip->nominal_fname;
2338 if (!string)
2339 string = "";
2340 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2341 quote_string (pfile, string);
2342 return;
2345 case T_INCLUDE_LEVEL:
2346 true_indepth = 0;
2347 ip = CPP_BUFFER (pfile);
2348 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2349 if (ip->fname != NULL)
2350 true_indepth++;
2352 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2353 sprintf (buf, "%d", true_indepth - 1);
2354 break;
2356 case T_VERSION:
2357 buf = (char *) alloca (3 + strlen (version_string));
2358 sprintf (buf, "\"%s\"", version_string);
2359 break;
2361 #ifndef NO_BUILTIN_SIZE_TYPE
2362 case T_SIZE_TYPE:
2363 buf = SIZE_TYPE;
2364 break;
2365 #endif
2367 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2368 case T_PTRDIFF_TYPE:
2369 buf = PTRDIFF_TYPE;
2370 break;
2371 #endif
2373 case T_WCHAR_TYPE:
2374 buf = CPP_WCHAR_TYPE (pfile);
2375 break;
2377 case T_USER_LABEL_PREFIX_TYPE:
2378 buf = USER_LABEL_PREFIX;
2379 break;
2381 case T_REGISTER_PREFIX_TYPE:
2382 buf = REGISTER_PREFIX;
2383 break;
2385 case T_CONST:
2386 buf = (char *) alloca (4 * sizeof (int));
2387 sprintf (buf, "%d", hp->value.ival);
2388 #ifdef STDC_0_IN_SYSTEM_HEADERS
2389 if (ip->system_header_p
2390 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2391 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2392 strcpy (buf, "0");
2393 #endif
2394 #if 0
2395 if (pcp_inside_if && pcp_outfile)
2396 /* Output a precondition for this macro use */
2397 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2398 #endif
2399 break;
2401 case T_SPECLINE:
2403 long line = ip->lineno;
2404 long col = ip->colno;
2405 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2407 buf = (char *) alloca (10);
2408 sprintf (buf, "%ld", line);
2410 break;
2412 case T_DATE:
2413 case T_TIME:
2414 buf = (char *) alloca (20);
2415 timebuf = timestamp (pfile);
2416 if (hp->type == T_DATE)
2417 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2418 timebuf->tm_mday, timebuf->tm_year + 1900);
2419 else
2420 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2421 timebuf->tm_sec);
2422 break;
2424 case T_SPEC_DEFINED:
2425 buf = " 0 "; /* Assume symbol is not defined */
2426 ip = CPP_BUFFER (pfile);
2427 SKIP_WHITE_SPACE (ip->cur);
2428 if (*ip->cur == '(')
2430 paren++;
2431 ip->cur++; /* Skip over the paren */
2432 SKIP_WHITE_SPACE (ip->cur);
2435 if (!is_idstart[*ip->cur])
2436 goto oops;
2437 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2438 goto oops;
2439 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2441 #if 0
2442 if (pcp_outfile && pcp_inside_if
2443 && (hp->type == T_CONST
2444 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2445 /* Output a precondition for this macro use. */
2446 fprintf (pcp_outfile, "#define %s\n", hp->name);
2447 #endif
2448 buf = " 1 ";
2450 #if 0
2451 else
2452 if (pcp_outfile && pcp_inside_if)
2454 /* Output a precondition for this macro use */
2455 U_CHAR *cp = ip->bufp;
2456 fprintf (pcp_outfile, "#undef ");
2457 while (is_idchar[*cp]) /* Ick! */
2458 fputc (*cp++, pcp_outfile);
2459 putc ('\n', pcp_outfile);
2461 #endif
2462 while (is_idchar[*ip->cur])
2463 ++ip->cur;
2464 SKIP_WHITE_SPACE (ip->cur);
2465 if (paren)
2467 if (*ip->cur != ')')
2468 goto oops;
2469 ++ip->cur;
2471 break;
2473 oops:
2475 cpp_error (pfile, "`defined' without an identifier");
2476 break;
2478 default:
2479 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2480 abort ();
2482 len = strlen (buf);
2483 CPP_RESERVE (pfile, len + 1);
2484 CPP_PUTS_Q (pfile, buf, len);
2485 CPP_NUL_TERMINATE_Q (pfile);
2487 return;
2490 /* Write out a #define command for the special named MACRO_NAME
2491 to PFILE's token_buffer. */
2493 static void
2494 dump_special_to_buffer (pfile, macro_name)
2495 cpp_reader *pfile;
2496 char *macro_name;
2498 static char define_directive[] = "#define ";
2499 int macro_name_length = strlen (macro_name);
2500 output_line_command (pfile, 0, same_file);
2501 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2502 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2503 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2504 CPP_PUTC_Q (pfile, ' ');
2505 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2506 CPP_PUTC (pfile, '\n');
2509 /* Initialize the built-in macros. */
2511 static void
2512 initialize_builtins (pfile)
2513 cpp_reader *pfile;
2515 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2516 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2517 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2518 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2519 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2520 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2521 #ifndef NO_BUILTIN_SIZE_TYPE
2522 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2523 #endif
2524 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2525 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2526 #endif
2527 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2528 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2529 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2530 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2531 if (!CPP_TRADITIONAL (pfile))
2532 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2533 if (CPP_OPTIONS (pfile)->objc)
2534 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2535 /* This is supplied using a -D by the compiler driver
2536 so that it is present only when truly compiling with GNU C. */
2537 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2539 if (CPP_OPTIONS (pfile)->debug_output)
2541 dump_special_to_buffer (pfile, "__BASE_FILE__");
2542 dump_special_to_buffer (pfile, "__VERSION__");
2543 #ifndef NO_BUILTIN_SIZE_TYPE
2544 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2545 #endif
2546 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2547 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2548 #endif
2549 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2550 dump_special_to_buffer (pfile, "__DATE__");
2551 dump_special_to_buffer (pfile, "__TIME__");
2552 if (!CPP_TRADITIONAL (pfile))
2553 dump_special_to_buffer (pfile, "__STDC__");
2554 if (CPP_OPTIONS (pfile)->objc)
2555 dump_special_to_buffer (pfile, "__OBJC__");
2559 /* Return 1 iff a token ending in C1 followed directly by a token C2
2560 could cause mis-tokenization. */
2562 static int
2563 unsafe_chars (c1, c2)
2564 int c1, c2;
2566 switch (c1)
2568 case '+': case '-':
2569 if (c2 == c1 || c2 == '=')
2570 return 1;
2571 goto letter;
2572 case '.':
2573 case '0': case '1': case '2': case '3': case '4':
2574 case '5': case '6': case '7': case '8': case '9':
2575 case 'e': case 'E': case 'p': case 'P':
2576 if (c2 == '-' || c2 == '+')
2577 return 1; /* could extend a pre-processing number */
2578 goto letter;
2579 case 'L':
2580 if (c2 == '\'' || c2 == '\"')
2581 return 1; /* Could turn into L"xxx" or L'xxx'. */
2582 goto letter;
2583 letter:
2584 case '_':
2585 case 'a': case 'b': case 'c': case 'd': case 'f':
2586 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2587 case 'm': case 'n': case 'o': case 'q': case 'r':
2588 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2589 case 'y': case 'z':
2590 case 'A': case 'B': case 'C': case 'D': case 'F':
2591 case 'G': case 'H': case 'I': case 'J': case 'K':
2592 case 'M': case 'N': case 'O': case 'Q': case 'R':
2593 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2594 case 'Y': case 'Z':
2595 /* We're in the middle of either a name or a pre-processing number. */
2596 return (is_idchar[c2] || c2 == '.');
2597 case '<': case '>': case '!': case '%': case '#': case ':':
2598 case '^': case '&': case '|': case '*': case '/': case '=':
2599 return (c2 == c1 || c2 == '=');
2601 return 0;
2604 /* Expand a macro call.
2605 HP points to the symbol that is the macro being called.
2606 Put the result of expansion onto the input stack
2607 so that subsequent input by our caller will use it.
2609 If macro wants arguments, caller has already verified that
2610 an argument list follows; arguments come from the input stack. */
2612 static void
2613 macroexpand (pfile, hp)
2614 cpp_reader *pfile;
2615 HASHNODE *hp;
2617 int nargs;
2618 DEFINITION *defn = hp->value.defn;
2619 register U_CHAR *xbuf;
2620 long start_line, start_column;
2621 int xbuf_len;
2622 struct argdata *args;
2623 long old_written = CPP_WRITTEN (pfile);
2624 #if 0
2625 int start_line = instack[indepth].lineno;
2626 #endif
2627 int rest_args, rest_zero;
2628 register int i;
2630 #if 0
2631 CHECK_DEPTH (return;);
2632 #endif
2634 #if 0
2635 /* This macro is being used inside a #if, which means it must be */
2636 /* recorded as a precondition. */
2637 if (pcp_inside_if && pcp_outfile && defn->predefined)
2638 dump_single_macro (hp, pcp_outfile);
2639 #endif
2641 pfile->output_escapes++;
2642 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2644 nargs = defn->nargs;
2646 if (nargs >= 0)
2648 enum cpp_token token;
2650 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2652 for (i = 0; i < nargs; i++)
2654 args[i].raw = args[i].expanded = 0;
2655 args[i].raw_length = 0;
2656 args[i].expand_length = args[i].stringified_length = -1;
2657 args[i].use_count = 0;
2660 /* Parse all the macro args that are supplied. I counts them.
2661 The first NARGS args are stored in ARGS.
2662 The rest are discarded. If rest_args is set then we assume
2663 macarg absorbed the rest of the args. */
2664 i = 0;
2665 rest_args = 0;
2666 rest_args = 0;
2667 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2670 if (rest_args)
2671 continue;
2672 if (i < nargs || (nargs == 0 && i == 0))
2674 /* if we are working on last arg which absorbs rest of args... */
2675 if (i == nargs - 1 && defn->rest_args)
2676 rest_args = 1;
2677 args[i].raw = CPP_WRITTEN (pfile);
2678 token = macarg (pfile, rest_args);
2679 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2680 args[i].newlines = 0; /* FIXME */
2682 else
2683 token = macarg (pfile, 0);
2684 if (token == CPP_EOF || token == CPP_POP)
2686 cpp_error_with_line (pfile, start_line, start_column,
2687 "unterminated macro call");
2688 return;
2690 i++;
2691 } while (token == CPP_COMMA);
2693 /* If we got one arg but it was just whitespace, call that 0 args. */
2694 if (i == 1)
2696 register U_CHAR *bp = ARG_BASE + args[0].raw;
2697 register U_CHAR *lim = bp + args[0].raw_length;
2698 /* cpp.texi says for foo ( ) we provide one argument.
2699 However, if foo wants just 0 arguments, treat this as 0. */
2700 if (nargs == 0)
2701 while (bp != lim && is_space[*bp]) bp++;
2702 if (bp == lim)
2703 i = 0;
2706 /* Don't output an error message if we have already output one for
2707 a parse error above. */
2708 rest_zero = 0;
2709 if (nargs == 0 && i > 0)
2711 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2713 else if (i < nargs)
2715 /* traditional C allows foo() if foo wants one argument. */
2716 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2718 /* the rest args token is allowed to absorb 0 tokens */
2719 else if (i == nargs - 1 && defn->rest_args)
2720 rest_zero = 1;
2721 else if (i == 0)
2722 cpp_error (pfile, "macro `%s' used without args", hp->name);
2723 else if (i == 1)
2724 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2725 else
2726 cpp_error (pfile, "macro `%s' used with only %d args",
2727 hp->name, i);
2729 else if (i > nargs)
2731 cpp_error (pfile,
2732 "macro `%s' used with too many (%d) args", hp->name, i);
2736 /* If macro wants zero args, we parsed the arglist for checking only.
2737 Read directly from the macro definition. */
2738 if (nargs <= 0)
2740 xbuf = defn->expansion;
2741 xbuf_len = defn->length;
2743 else
2745 register U_CHAR *exp = defn->expansion;
2746 register int offset; /* offset in expansion,
2747 copied a piece at a time */
2748 register int totlen; /* total amount of exp buffer filled so far */
2750 register struct reflist *ap, *last_ap;
2752 /* Macro really takes args. Compute the expansion of this call. */
2754 /* Compute length in characters of the macro's expansion.
2755 Also count number of times each arg is used. */
2756 xbuf_len = defn->length;
2757 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2759 if (ap->stringify)
2761 register struct argdata *arg = &args[ap->argno];
2762 /* Stringify it it hasn't already been */
2763 if (arg->stringified_length < 0)
2765 int arglen = arg->raw_length;
2766 int escaped = 0;
2767 int in_string = 0;
2768 int c;
2769 /* Initially need_space is -1. Otherwise, 1 means the
2770 previous character was a space, but we suppressed it;
2771 0 means the previous character was a non-space. */
2772 int need_space = -1;
2773 i = 0;
2774 arg->stringified = CPP_WRITTEN (pfile);
2775 if (!CPP_TRADITIONAL (pfile))
2776 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2777 for (; i < arglen; i++)
2779 c = (ARG_BASE + arg->raw)[i];
2781 if (! in_string)
2783 /* Internal sequences of whitespace are replaced by
2784 one space except within an string or char token.*/
2785 if (is_space[c])
2787 if (CPP_WRITTEN (pfile) > arg->stringified
2788 && (CPP_PWRITTEN (pfile))[-1] == '@')
2790 /* "@ " escape markers are removed */
2791 CPP_ADJUST_WRITTEN (pfile, -1);
2792 continue;
2794 if (need_space == 0)
2795 need_space = 1;
2796 continue;
2798 else if (need_space > 0)
2799 CPP_PUTC (pfile, ' ');
2800 need_space = 0;
2803 if (escaped)
2804 escaped = 0;
2805 else
2807 if (c == '\\')
2808 escaped = 1;
2809 if (in_string)
2811 if (c == in_string)
2812 in_string = 0;
2814 else if (c == '\"' || c == '\'')
2815 in_string = c;
2818 /* Escape these chars */
2819 if (c == '\"' || (in_string && c == '\\'))
2820 CPP_PUTC (pfile, '\\');
2821 if (isprint (c))
2822 CPP_PUTC (pfile, c);
2823 else
2825 CPP_RESERVE (pfile, 4);
2826 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2827 (unsigned int) c);
2828 CPP_ADJUST_WRITTEN (pfile, 4);
2831 if (!CPP_TRADITIONAL (pfile))
2832 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2833 arg->stringified_length
2834 = CPP_WRITTEN (pfile) - arg->stringified;
2836 xbuf_len += args[ap->argno].stringified_length;
2838 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2839 /* Add 4 for two newline-space markers to prevent
2840 token concatenation. */
2841 xbuf_len += args[ap->argno].raw_length + 4;
2842 else
2844 /* We have an ordinary (expanded) occurrence of the arg.
2845 So compute its expansion, if we have not already. */
2846 if (args[ap->argno].expand_length < 0)
2848 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2849 cpp_expand_to_buffer (pfile,
2850 ARG_BASE + args[ap->argno].raw,
2851 args[ap->argno].raw_length);
2853 args[ap->argno].expand_length
2854 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2857 /* Add 4 for two newline-space markers to prevent
2858 token concatenation. */
2859 xbuf_len += args[ap->argno].expand_length + 4;
2861 if (args[ap->argno].use_count < 10)
2862 args[ap->argno].use_count++;
2865 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2867 /* Generate in XBUF the complete expansion
2868 with arguments substituted in.
2869 TOTLEN is the total size generated so far.
2870 OFFSET is the index in the definition
2871 of where we are copying from. */
2872 offset = totlen = 0;
2873 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2874 last_ap = ap, ap = ap->next)
2876 register struct argdata *arg = &args[ap->argno];
2877 int count_before = totlen;
2879 /* Add chars to XBUF. */
2880 for (i = 0; i < ap->nchars; i++, offset++)
2881 xbuf[totlen++] = exp[offset];
2883 /* If followed by an empty rest arg with concatenation,
2884 delete the last run of nonwhite chars. */
2885 if (rest_zero && totlen > count_before
2886 && ((ap->rest_args && ap->raw_before)
2887 || (last_ap != NULL && last_ap->rest_args
2888 && last_ap->raw_after)))
2890 /* Delete final whitespace. */
2891 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2892 totlen--;
2894 /* Delete the nonwhites before them. */
2895 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2896 totlen--;
2899 if (ap->stringify != 0)
2901 bcopy (ARG_BASE + arg->stringified,
2902 xbuf + totlen, arg->stringified_length);
2903 totlen += arg->stringified_length;
2905 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2907 U_CHAR *p1 = ARG_BASE + arg->raw;
2908 U_CHAR *l1 = p1 + arg->raw_length;
2909 if (ap->raw_before)
2911 while (p1 != l1 && is_space[*p1]) p1++;
2912 while (p1 != l1 && is_idchar[*p1])
2913 xbuf[totlen++] = *p1++;
2914 /* Delete any no-reexpansion marker that follows
2915 an identifier at the beginning of the argument
2916 if the argument is concatenated with what precedes it. */
2917 if (p1[0] == '@' && p1[1] == '-')
2918 p1 += 2;
2920 if (ap->raw_after)
2922 /* Arg is concatenated after: delete trailing whitespace,
2923 whitespace markers, and no-reexpansion markers. */
2924 while (p1 != l1)
2926 if (is_space[l1[-1]]) l1--;
2927 else if (l1[-1] == '-')
2929 U_CHAR *p2 = l1 - 1;
2930 /* If a `-' is preceded by an odd number of newlines then it
2931 and the last newline are a no-reexpansion marker. */
2932 while (p2 != p1 && p2[-1] == '\n') p2--;
2933 if ((l1 - 1 - p2) & 1) {
2934 l1 -= 2;
2936 else break;
2938 else break;
2942 bcopy (p1, xbuf + totlen, l1 - p1);
2943 totlen += l1 - p1;
2945 else
2947 U_CHAR *expanded = ARG_BASE + arg->expanded;
2948 if (!ap->raw_before && totlen > 0 && arg->expand_length
2949 && !CPP_TRADITIONAL(pfile)
2950 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2952 xbuf[totlen++] = '@';
2953 xbuf[totlen++] = ' ';
2956 bcopy (expanded, xbuf + totlen, arg->expand_length);
2957 totlen += arg->expand_length;
2959 if (!ap->raw_after && totlen > 0 && offset < defn->length
2960 && !CPP_TRADITIONAL(pfile)
2961 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2963 xbuf[totlen++] = '@';
2964 xbuf[totlen++] = ' ';
2967 /* If a macro argument with newlines is used multiple times,
2968 then only expand the newlines once. This avoids creating
2969 output lines which don't correspond to any input line,
2970 which confuses gdb and gcov. */
2971 if (arg->use_count > 1 && arg->newlines > 0)
2973 /* Don't bother doing change_newlines for subsequent
2974 uses of arg. */
2975 arg->use_count = 1;
2976 arg->expand_length
2977 = change_newlines (expanded, arg->expand_length);
2981 if (totlen > xbuf_len)
2982 abort ();
2985 /* if there is anything left of the definition
2986 after handling the arg list, copy that in too. */
2988 for (i = offset; i < defn->length; i++)
2990 /* if we've reached the end of the macro */
2991 if (exp[i] == ')')
2992 rest_zero = 0;
2993 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2994 && last_ap->raw_after))
2995 xbuf[totlen++] = exp[i];
2998 xbuf[totlen] = 0;
2999 xbuf_len = totlen;
3003 pfile->output_escapes--;
3005 /* Now put the expansion on the input stack
3006 so our caller will commence reading from it. */
3007 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3008 CPP_BUFFER (pfile)->has_escapes = 1;
3010 /* Pop the space we've used in the token_buffer for argument expansion. */
3011 CPP_SET_WRITTEN (pfile, old_written);
3013 /* Recursive macro use sometimes works traditionally.
3014 #define foo(x,y) bar (x (y,0), y)
3015 foo (foo, baz) */
3017 if (!CPP_TRADITIONAL (pfile))
3018 hp->type = T_DISABLED;
3021 static void
3022 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
3023 cpp_reader *pfile;
3024 register U_CHAR *xbuf;
3025 int xbuf_len;
3026 HASHNODE *hp;
3028 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3029 if (mbuf == NULL)
3030 return;
3031 mbuf->cleanup = macro_cleanup;
3032 mbuf->data = hp;
3034 /* The first chars of the expansion should be a "@ " added by
3035 collect_expansion. This is to prevent accidental token-pasting
3036 between the text preceding the macro invocation, and the macro
3037 expansion text.
3039 We would like to avoid adding unneeded spaces (for the sake of
3040 tools that use cpp, such as imake). In some common cases we can
3041 tell that it is safe to omit the space.
3043 The character before the macro invocation cannot have been an
3044 idchar (or else it would have been pasted with the idchars of
3045 the macro name). Therefore, if the first non-space character
3046 of the expansion is an idchar, we do not need the extra space
3047 to prevent token pasting.
3049 Also, we don't need the extra space if the first char is '(',
3050 or some other (less common) characters. */
3052 if (xbuf[0] == '@' && xbuf[1] == ' '
3053 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3054 || xbuf[2] == '\"'))
3055 mbuf->cur += 2;
3058 /* Like cpp_get_token, except that it does not read past end-of-line.
3059 Also, horizontal space is skipped, and macros are popped. */
3061 static enum cpp_token
3062 get_directive_token (pfile)
3063 cpp_reader *pfile;
3065 for (;;)
3067 long old_written = CPP_WRITTEN (pfile);
3068 enum cpp_token token;
3069 cpp_skip_hspace (pfile);
3070 if (PEEKC () == '\n')
3071 return CPP_VSPACE;
3072 token = cpp_get_token (pfile);
3073 switch (token)
3075 case CPP_POP:
3076 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3077 return token;
3078 /* ... else fall though ... */
3079 case CPP_HSPACE: case CPP_COMMENT:
3080 CPP_SET_WRITTEN (pfile, old_written);
3081 break;
3082 default:
3083 return token;
3088 /* Handle #include and #import.
3089 This function expects to see "fname" or <fname> on the input.
3091 The input is normally in part of the output_buffer following
3092 CPP_WRITTEN, and will get overwritten by output_line_command.
3093 I.e. in input file specification has been popped by handle_directive.
3094 This is safe. */
3096 static int
3097 do_include (pfile, keyword, unused1, unused2)
3098 cpp_reader *pfile;
3099 struct directive *keyword;
3100 U_CHAR *unused1, *unused2;
3102 int importing = (keyword->type == T_IMPORT);
3103 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3104 char *fname; /* Dynamically allocated fname buffer */
3105 char *pcftry;
3106 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3107 enum cpp_token token;
3109 /* Chain of dirs to search */
3110 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3111 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3112 struct file_name_list *searchptr = 0;
3113 long old_written = CPP_WRITTEN (pfile);
3115 int flen;
3117 int f; /* file number */
3119 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3120 char *pcfbuf;
3121 #if 0
3122 int pcf = -1;
3123 char *pcfbuflimit;
3124 #endif
3125 int pcfnum;
3126 f= -1; /* JF we iz paranoid! */
3128 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3130 if (importing)
3131 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
3132 if (skip_dirs)
3133 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
3136 if (importing && CPP_OPTIONS (pfile)->warn_import
3137 && !CPP_OPTIONS (pfile)->inhibit_warnings
3138 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3140 pfile->import_warning = 1;
3141 cpp_warning (pfile, "using `#import' is not recommended");
3142 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3143 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3144 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3145 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3146 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3147 fprintf (stderr, " ... <real contents of file> ...\n");
3148 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3149 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3150 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3151 fprintf (stderr, "when it is equipped with such a conditional.\n");
3154 pfile->parsing_include_directive++;
3155 token = get_directive_token (pfile);
3156 pfile->parsing_include_directive--;
3158 if (token == CPP_STRING)
3160 /* FIXME - check no trailing garbage */
3161 fbeg = pfile->token_buffer + old_written + 1;
3162 fend = CPP_PWRITTEN (pfile) - 1;
3163 if (fbeg[-1] == '<')
3165 angle_brackets = 1;
3166 /* If -I-, start with the first -I dir after the -I-. */
3167 if (CPP_OPTIONS (pfile)->first_bracket_include)
3168 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3170 /* If -I- was specified, don't search current dir, only spec'd ones. */
3171 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3173 cpp_buffer *fp = CPP_BUFFER (pfile);
3174 /* We have "filename". Figure out directory this source
3175 file is coming from and put it on the front of the list. */
3177 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3179 int n;
3180 char *ep,*nam;
3182 if ((nam = fp->nominal_fname) != NULL)
3184 /* Found a named file. Figure out dir of the file,
3185 and put it in front of the search list. */
3186 dsp[0].next = search_start;
3187 search_start = dsp;
3188 #ifndef VMS
3189 ep = rindex (nam, '/');
3190 #else /* VMS */
3191 ep = rindex (nam, ']');
3192 if (ep == NULL) ep = rindex (nam, '>');
3193 if (ep == NULL) ep = rindex (nam, ':');
3194 if (ep != NULL) ep++;
3195 #endif /* VMS */
3196 if (ep != NULL)
3198 n = ep - nam;
3199 dsp[0].fname = (char *) alloca (n + 1);
3200 strncpy (dsp[0].fname, nam, n);
3201 dsp[0].fname[n] = '\0';
3202 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3203 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3205 else
3207 dsp[0].fname = 0; /* Current directory */
3209 dsp[0].got_name_map = 0;
3210 break;
3215 #ifdef VMS
3216 else if (token == CPP_NAME)
3219 * Support '#include xyz' like VAX-C to allow for easy use of all the
3220 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3221 * code from case '<' is repeated here) and generates a warning.
3223 cpp_warning (pfile,
3224 "VAX-C-style include specification found, use '#include <filename.h>' !");
3225 angle_brackets = 1;
3226 /* If -I-, start with the first -I dir after the -I-. */
3227 if (CPP_OPTIONS (pfile)->first_bracket_include)
3228 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3229 fbeg = pfile->token_buffer + old_written;
3230 fend = CPP_PWRITTEN (pfile);
3232 #endif
3233 else
3235 cpp_error (pfile,
3236 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3237 CPP_SET_WRITTEN (pfile, old_written);
3238 skip_rest_of_line (pfile);
3239 return 0;
3242 *fend = 0;
3244 token = get_directive_token (pfile);
3245 if (token != CPP_VSPACE)
3247 cpp_error (pfile, "junk at end of `#include'");
3248 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3249 token = get_directive_token (pfile);
3252 /* For #include_next, skip in the search path
3253 past the dir in which the containing file was found. */
3254 if (skip_dirs)
3256 cpp_buffer *fp = CPP_BUFFER (pfile);
3257 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3258 if (fp->fname != NULL)
3260 /* fp->dir is null if the containing file was specified with
3261 an absolute file name. In that case, don't skip anything. */
3262 if (fp->dir == SELF_DIR_DUMMY)
3263 search_start = CPP_OPTIONS (pfile)->include;
3264 else if (fp->dir)
3265 search_start = fp->dir->next;
3266 break;
3270 CPP_SET_WRITTEN (pfile, old_written);
3272 flen = fend - fbeg;
3274 if (flen == 0)
3276 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3277 return 0;
3280 /* Allocate this permanently, because it gets stored in the definitions
3281 of macros. */
3282 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3283 /* + 2 above for slash and terminating null. */
3284 /* + 2 added for '.h' on VMS (to support '#include filename') */
3286 /* If specified file name is absolute, just open it. */
3288 if (*fbeg == '/') {
3289 strncpy (fname, fbeg, flen);
3290 fname[flen] = 0;
3291 if (redundant_include_p (pfile, fname))
3292 return 0;
3293 if (importing)
3294 f = lookup_import (pfile, fname, NULL_PTR);
3295 else
3296 f = open_include_file (pfile, fname, NULL_PTR);
3297 if (f == -2)
3298 return 0; /* Already included this file */
3299 } else {
3300 /* Search directory path, trying to open the file.
3301 Copy each filename tried into FNAME. */
3303 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3304 if (searchptr->fname) {
3305 /* The empty string in a search path is ignored.
3306 This makes it possible to turn off entirely
3307 a standard piece of the list. */
3308 if (searchptr->fname[0] == 0)
3309 continue;
3310 strcpy (fname, searchptr->fname);
3311 strcat (fname, "/");
3312 fname[strlen (fname) + flen] = 0;
3313 } else {
3314 fname[0] = 0;
3316 strncat (fname, fbeg, flen);
3317 #ifdef VMS
3318 /* Change this 1/2 Unix 1/2 VMS file specification into a
3319 full VMS file specification */
3320 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3321 /* Fix up the filename */
3322 hack_vms_include_specification (fname);
3323 } else {
3324 /* This is a normal VMS filespec, so use it unchanged. */
3325 strncpy (fname, fbeg, flen);
3326 fname[flen] = 0;
3327 /* if it's '#include filename', add the missing .h */
3328 if (index(fname,'.')==NULL) {
3329 strcat (fname, ".h");
3332 #endif /* VMS */
3333 /* ??? There are currently 3 separate mechanisms for avoiding processing
3334 of redundant include files: #import, #pragma once, and
3335 redundant_include_p. It would be nice if they were unified. */
3336 if (redundant_include_p (pfile, fname))
3337 return 0;
3338 if (importing)
3339 f = lookup_import (pfile, fname, searchptr);
3340 else
3341 f = open_include_file (pfile, fname, searchptr);
3342 if (f == -2)
3343 return 0; /* Already included this file */
3344 #ifdef EACCES
3345 else if (f == -1 && errno == EACCES)
3346 cpp_warning (pfile, "Header file %s exists, but is not readable",
3347 fname);
3348 #endif
3349 if (f >= 0)
3350 break;
3354 if (f < 0)
3356 /* A file that was not found. */
3357 strncpy (fname, fbeg, flen);
3358 fname[flen] = 0;
3359 /* If generating dependencies and -MG was specified, we assume missing
3360 files are leaf files, living in the same directory as the source file
3361 or other similar place; these missing files may be generated from
3362 other files and may not exist yet (eg: y.tab.h). */
3364 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3365 && CPP_PRINT_DEPS (pfile)
3366 > (angle_brackets || (pfile->system_include_depth > 0)))
3368 /* If it was requested as a system header file,
3369 then assume it belongs in the first place to look for such. */
3370 if (angle_brackets)
3372 for (searchptr = search_start; searchptr;
3373 searchptr = searchptr->next)
3375 if (searchptr->fname)
3377 char *p;
3379 if (searchptr->fname[0] == 0)
3380 continue;
3381 p = (char *) alloca (strlen (searchptr->fname)
3382 + strlen (fname) + 2);
3383 strcpy (p, searchptr->fname);
3384 strcat (p, "/");
3385 strcat (p, fname);
3386 deps_output (pfile, p, ' ');
3387 break;
3391 else
3393 /* Otherwise, omit the directory, as if the file existed
3394 in the directory with the source. */
3395 deps_output (pfile, fname, ' ');
3398 /* If -M was specified, and this header file won't be added to the
3399 dependency list, then don't count this as an error, because we can
3400 still produce correct output. Otherwise, we can't produce correct
3401 output, because there may be dependencies we need inside the missing
3402 file, and we don't know what directory this missing file exists in.*/
3403 else if (CPP_PRINT_DEPS (pfile)
3404 && (CPP_PRINT_DEPS (pfile)
3405 <= (angle_brackets || (pfile->system_include_depth > 0))))
3406 cpp_warning (pfile, "No include path in which to find %s", fname);
3407 else if (search_start)
3408 cpp_error_from_errno (pfile, fname);
3409 else
3410 cpp_error (pfile, "No include path in which to find %s", fname);
3412 else {
3413 /* Check to see if this include file is a once-only include file.
3414 If so, give up. */
3416 struct file_name_list *ptr;
3418 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3419 if (!strcmp (ptr->fname, fname)) {
3420 close (f);
3421 return 0; /* This file was once'd. */
3425 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3426 if (!strcmp (ptr->fname, fname))
3427 break; /* This file was included before. */
3430 if (ptr == 0) {
3431 /* This is the first time for this file. */
3432 /* Add it to list of files included. */
3434 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3435 ptr->control_macro = 0;
3436 ptr->c_system_include_path = 0;
3437 ptr->next = pfile->all_include_files;
3438 pfile->all_include_files = ptr;
3439 ptr->fname = savestring (fname);
3440 ptr->got_name_map = 0;
3442 /* For -M, add this file to the dependencies. */
3443 if (CPP_PRINT_DEPS (pfile)
3444 > (angle_brackets || (pfile->system_include_depth > 0)))
3445 deps_output (pfile, fname, ' ');
3448 /* Handle -H option. */
3449 if (CPP_OPTIONS(pfile)->print_include_names)
3451 cpp_buffer *buf = CPP_BUFFER (pfile);
3452 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3453 putc ('.', stderr);
3454 fprintf (stderr, "%s\n", fname);
3457 if (angle_brackets)
3458 pfile->system_include_depth++;
3460 /* Actually process the file. */
3462 /* Record file on "seen" list for #import. */
3463 add_import (pfile, f, fname);
3465 pcftry = (char *) alloca (strlen (fname) + 30);
3466 pcfbuf = 0;
3467 pcfnum = 0;
3469 #if 0
3470 if (!no_precomp)
3472 struct stat stat_f;
3474 fstat (f, &stat_f);
3476 do {
3477 sprintf (pcftry, "%s%d", fname, pcfnum++);
3479 pcf = open (pcftry, O_RDONLY, 0666);
3480 if (pcf != -1)
3482 struct stat s;
3484 fstat (pcf, &s);
3485 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3486 sizeof (s.st_ino))
3487 || stat_f.st_dev != s.st_dev)
3489 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3490 /* Don't need it any more. */
3491 close (pcf);
3493 else
3495 /* Don't need it at all. */
3496 close (pcf);
3497 break;
3500 } while (pcf != -1 && !pcfbuf);
3502 #endif
3504 /* Actually process the file */
3505 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3506 return 0;
3507 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3508 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3510 output_line_command (pfile, 0, enter_file);
3511 pfile->only_seen_white = 2;
3514 if (angle_brackets)
3515 pfile->system_include_depth--;
3517 return 0;
3520 /* Return nonzero if there is no need to include file NAME
3521 because it has already been included and it contains a conditional
3522 to make a repeated include do nothing. */
3524 static int
3525 redundant_include_p (pfile, name)
3526 cpp_reader *pfile;
3527 char *name;
3529 struct file_name_list *l = pfile->all_include_files;
3530 for (; l; l = l->next)
3531 if (! strcmp (name, l->fname)
3532 && l->control_macro
3533 && cpp_lookup (pfile, l->control_macro, -1, -1))
3534 return 1;
3535 return 0;
3538 /* Return nonzero if the given FILENAME is an absolute pathname which
3539 designates a file within one of the known "system" include file
3540 directories. We assume here that if the given FILENAME looks like
3541 it is the name of a file which resides either directly in a "system"
3542 include file directory, or within any subdirectory thereof, then the
3543 given file must be a "system" include file. This function tells us
3544 if we should suppress pedantic errors/warnings for the given FILENAME.
3546 The value is 2 if the file is a C-language system header file
3547 for which C++ should (on most systems) assume `extern "C"'. */
3549 static int
3550 is_system_include (pfile, filename)
3551 cpp_reader *pfile;
3552 register char *filename;
3554 struct file_name_list *searchptr;
3556 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3557 searchptr = searchptr->next)
3558 if (searchptr->fname) {
3559 register char *sys_dir = searchptr->fname;
3560 register unsigned length = strlen (sys_dir);
3562 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3564 if (searchptr->c_system_include_path)
3565 return 2;
3566 else
3567 return 1;
3570 return 0;
3575 * Install a name in the assertion hash table.
3577 * If LEN is >= 0, it is the length of the name.
3578 * Otherwise, compute the length by scanning the entire name.
3580 * If HASH is >= 0, it is the precomputed hash code.
3581 * Otherwise, compute the hash code.
3584 static ASSERTION_HASHNODE *
3585 assertion_install (pfile, name, len, hash)
3586 cpp_reader *pfile;
3587 U_CHAR *name;
3588 int len;
3589 int hash;
3591 register ASSERTION_HASHNODE *hp;
3592 register int i, bucket;
3593 register U_CHAR *p, *q;
3595 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3596 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3597 bucket = hash;
3598 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3599 hp->next = pfile->assertion_hashtab[bucket];
3600 pfile->assertion_hashtab[bucket] = hp;
3601 hp->prev = NULL;
3602 if (hp->next != NULL)
3603 hp->next->prev = hp;
3604 hp->length = len;
3605 hp->value = 0;
3606 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3607 p = hp->name;
3608 q = name;
3609 for (i = 0; i < len; i++)
3610 *p++ = *q++;
3611 hp->name[len] = 0;
3612 return hp;
3615 * find the most recent hash node for name name (ending with first
3616 * non-identifier char) installed by install
3618 * If LEN is >= 0, it is the length of the name.
3619 * Otherwise, compute the length by scanning the entire name.
3621 * If HASH is >= 0, it is the precomputed hash code.
3622 * Otherwise, compute the hash code.
3625 static ASSERTION_HASHNODE *
3626 assertion_lookup (pfile, name, len, hash)
3627 cpp_reader *pfile;
3628 U_CHAR *name;
3629 int len;
3630 int hash;
3632 register ASSERTION_HASHNODE *bucket;
3634 bucket = pfile->assertion_hashtab[hash];
3635 while (bucket) {
3636 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3637 return bucket;
3638 bucket = bucket->next;
3640 return NULL;
3643 static void
3644 delete_assertion (hp)
3645 ASSERTION_HASHNODE *hp;
3647 struct tokenlist_list *tail;
3648 if (hp->prev != NULL)
3649 hp->prev->next = hp->next;
3650 if (hp->next != NULL)
3651 hp->next->prev = hp->prev;
3653 for (tail = hp->value; tail; )
3655 struct tokenlist_list *next = tail->next;
3656 free_token_list (tail->tokens);
3657 free (tail);
3658 tail = next;
3661 /* Make sure that the bucket chain header that
3662 the deleted guy was on points to the right thing afterwards. */
3663 if (hp == *hp->bucket_hdr)
3664 *hp->bucket_hdr = hp->next;
3666 free (hp);
3669 /* Convert a character string literal into a nul-terminated string.
3670 The input string is [IN ... LIMIT).
3671 The result is placed in RESULT. RESULT can be the same as IN.
3672 The value returned in the end of the string written to RESULT,
3673 or NULL on error. */
3675 static U_CHAR *
3676 convert_string (pfile, result, in, limit, handle_escapes)
3677 cpp_reader *pfile;
3678 register U_CHAR *result, *in, *limit;
3679 int handle_escapes;
3681 U_CHAR c;
3682 c = *in++;
3683 if (c != '\"')
3684 return NULL;
3685 while (in < limit)
3687 U_CHAR c = *in++;
3688 switch (c)
3690 case '\0':
3691 return NULL;
3692 case '\"':
3693 limit = in;
3694 break;
3695 case '\\':
3696 if (handle_escapes)
3698 char *bpc = (char *) in;
3699 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3700 in = (U_CHAR *) bpc;
3701 if (i >= 0)
3702 *result++ = (U_CHAR)c;
3703 break;
3705 /* else fall through */
3706 default:
3707 *result++ = c;
3710 *result = 0;
3711 return result;
3715 * interpret #line command. Remembers previously seen fnames
3716 * in its very own hash table.
3718 #define FNAME_HASHSIZE 37
3720 static int
3721 do_line (pfile, keyword)
3722 cpp_reader *pfile;
3723 struct directive *keyword;
3725 cpp_buffer *ip = CPP_BUFFER (pfile);
3726 int new_lineno;
3727 long old_written = CPP_WRITTEN (pfile);
3728 enum file_change_code file_change = same_file;
3729 enum cpp_token token;
3731 token = get_directive_token (pfile);
3733 if (token != CPP_NUMBER
3734 || !isdigit(pfile->token_buffer[old_written]))
3736 cpp_error (pfile, "invalid format `#line' command");
3737 goto bad_line_directive;
3740 /* The Newline at the end of this line remains to be processed.
3741 To put the next line at the specified line number,
3742 we must store a line number now that is one less. */
3743 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3744 CPP_SET_WRITTEN (pfile, old_written);
3746 /* NEW_LINENO is one less than the actual line number here. */
3747 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3748 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3750 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3751 if (PEEKC() && !is_space[PEEKC()]) {
3752 cpp_error (pfile, "invalid format `#line' command");
3753 goto bad_line_directive;
3755 #endif
3757 token = get_directive_token (pfile);
3759 if (token == CPP_STRING) {
3760 U_CHAR *fname = pfile->token_buffer + old_written;
3761 U_CHAR *end_name;
3762 static HASHNODE *fname_table[FNAME_HASHSIZE];
3763 HASHNODE *hp, **hash_bucket;
3764 U_CHAR *p;
3765 long num_start;
3766 int fname_length;
3768 /* Turn the file name, which is a character string literal,
3769 into a null-terminated string. Do this in place. */
3770 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3771 if (end_name == NULL)
3773 cpp_error (pfile, "invalid format `#line' command");
3774 goto bad_line_directive;
3777 fname_length = end_name - fname;
3779 num_start = CPP_WRITTEN (pfile);
3780 token = get_directive_token (pfile);
3781 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3782 p = pfile->token_buffer + num_start;
3783 if (CPP_PEDANTIC (pfile))
3784 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3786 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3788 cpp_error (pfile, "invalid format `#line' command");
3789 goto bad_line_directive;
3791 if (*p == '1')
3792 file_change = enter_file;
3793 else if (*p == 2)
3794 file_change = leave_file;
3795 else if (*p == 3)
3796 ip->system_header_p = 1;
3797 else /* if (*p == 4) */
3798 ip->system_header_p = 2;
3800 CPP_SET_WRITTEN (pfile, num_start);
3801 token = get_directive_token (pfile);
3802 p = pfile->token_buffer + num_start;
3803 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3804 ip->system_header_p = *p == 3 ? 1 : 2;
3805 token = get_directive_token (pfile);
3807 if (token != CPP_VSPACE) {
3808 cpp_error (pfile, "invalid format `#line' command");
3809 goto bad_line_directive;
3813 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3814 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3815 if (hp->length == fname_length
3816 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3817 ip->nominal_fname = hp->value.cpval;
3818 break;
3820 if (hp == 0) {
3821 /* Didn't find it; cons up a new one. */
3822 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3823 hp->next = *hash_bucket;
3824 *hash_bucket = hp;
3826 hp->length = fname_length;
3827 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3828 bcopy (fname, hp->value.cpval, fname_length);
3831 else if (token != CPP_VSPACE && token != CPP_EOF) {
3832 cpp_error (pfile, "invalid format `#line' command");
3833 goto bad_line_directive;
3836 ip->lineno = new_lineno;
3837 bad_line_directive:
3838 skip_rest_of_line (pfile);
3839 CPP_SET_WRITTEN (pfile, old_written);
3840 output_line_command (pfile, 0, file_change);
3841 return 0;
3845 * remove the definition of a symbol from the symbol table.
3846 * according to un*x /lib/cpp, it is not an error to undef
3847 * something that has no definitions, so it isn't one here either.
3850 static int
3851 do_undef (pfile, keyword, buf, limit)
3852 cpp_reader *pfile;
3853 struct directive *keyword;
3854 U_CHAR *buf, *limit;
3856 int sym_length;
3857 HASHNODE *hp;
3858 U_CHAR *orig_buf = buf;
3860 #if 0
3861 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3862 if (pcp_outfile && keyword)
3863 pass_thru_directive (buf, limit, pfile, keyword);
3864 #endif
3866 SKIP_WHITE_SPACE (buf);
3867 sym_length = check_macro_name (pfile, buf, "macro");
3869 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3871 /* If we are generating additional info for debugging (with -g) we
3872 need to pass through all effective #undef commands. */
3873 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3874 pass_thru_directive (orig_buf, limit, pfile, keyword);
3875 if (hp->type != T_MACRO)
3876 cpp_warning (pfile, "undefining `%s'", hp->name);
3877 delete_macro (hp);
3880 if (CPP_PEDANTIC (pfile)) {
3881 buf += sym_length;
3882 SKIP_WHITE_SPACE (buf);
3883 if (buf != limit)
3884 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3886 return 0;
3890 * Report an error detected by the program we are processing.
3891 * Use the text of the line in the error message.
3892 * (We use error because it prints the filename & line#.)
3895 static int
3896 do_error (pfile, keyword, buf, limit)
3897 cpp_reader *pfile;
3898 struct directive *keyword;
3899 U_CHAR *buf, *limit;
3901 int length = limit - buf;
3902 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3903 bcopy (buf, copy, length);
3904 copy[length] = 0;
3905 SKIP_WHITE_SPACE (copy);
3906 cpp_error (pfile, "#error %s", copy);
3907 return 0;
3911 * Report a warning detected by the program we are processing.
3912 * Use the text of the line in the warning message, then continue.
3913 * (We use error because it prints the filename & line#.)
3916 static int
3917 do_warning (pfile, keyword, buf, limit)
3918 cpp_reader *pfile;
3919 struct directive *keyword;
3920 U_CHAR *buf, *limit;
3922 int length = limit - buf;
3923 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3924 bcopy (buf, copy, length);
3925 copy[length] = 0;
3926 SKIP_WHITE_SPACE (copy);
3927 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3928 if -pedantic-errors is given, #warning should cause an error. */
3929 cpp_pedwarn (pfile, "#warning %s", copy);
3930 return 0;
3933 /* Remember the name of the current file being read from so that we can
3934 avoid ever including it again. */
3936 static int
3937 do_once (pfile)
3938 cpp_reader *pfile;
3940 cpp_buffer *ip = NULL;
3941 struct file_name_list *new;
3943 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3945 if (ip == CPP_NULL_BUFFER (pfile))
3946 return 0;
3947 if (ip->fname != NULL)
3948 break;
3952 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3953 new->next = pfile->dont_repeat_files;
3954 pfile->dont_repeat_files = new;
3955 new->fname = savestring (ip->fname);
3956 new->control_macro = 0;
3957 new->got_name_map = 0;
3958 new->c_system_include_path = 0;
3960 return 0;
3963 /* Report program identification. */
3965 static int
3966 do_ident (pfile, keyword, buf, limit)
3967 cpp_reader *pfile;
3968 struct directive *keyword;
3969 U_CHAR *buf, *limit;
3971 /* long old_written = CPP_WRITTEN (pfile);*/
3973 /* Allow #ident in system headers, since that's not user's fault. */
3974 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3975 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3977 /* Leave rest of line to be read by later calls to cpp_get_token. */
3979 return 0;
3982 /* #pragma and its argument line have already been copied to the output file.
3983 Just check for some recognized pragmas that need validation here. */
3985 static int
3986 do_pragma (pfile, keyword, buf, limit)
3987 cpp_reader *pfile;
3988 struct directive *keyword;
3989 U_CHAR *buf, *limit;
3991 while (*buf == ' ' || *buf == '\t')
3992 buf++;
3993 if (!strncmp (buf, "once", 4)) {
3994 /* Allow #pragma once in system headers, since that's not the user's
3995 fault. */
3996 if (!CPP_BUFFER (pfile)->system_header_p)
3997 cpp_warning (pfile, "`#pragma once' is obsolete");
3998 do_once (pfile);
4001 if (!strncmp (buf, "implementation", 14)) {
4002 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4003 been included yet. */
4004 struct file_name_list *ptr;
4005 U_CHAR *p = buf + 14, *fname, *inc_fname;
4006 int fname_len;
4007 SKIP_WHITE_SPACE (p);
4008 if (*p == '\n' || *p != '\"')
4009 return 0;
4011 fname = p + 1;
4012 p = (U_CHAR *) index (fname, '\"');
4013 fname_len = p != NULL ? p - fname : strlen (fname);
4015 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4016 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4017 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4018 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4019 cpp_warning (pfile,
4020 "`#pragma implementation' for `%s' appears after file is included",
4021 fname);
4025 return 0;
4028 #if 0
4029 /* This was a fun hack, but #pragma seems to start to be useful.
4030 By failing to recognize it, we pass it through unchanged to cc1. */
4033 * the behavior of the #pragma directive is implementation defined.
4034 * this implementation defines it as follows.
4037 static int
4038 do_pragma ()
4040 close (0);
4041 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4042 goto nope;
4043 close (1);
4044 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4045 goto nope;
4046 execl ("/usr/games/hack", "#pragma", 0);
4047 execl ("/usr/games/rogue", "#pragma", 0);
4048 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4049 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4050 nope:
4051 fatal ("You are in a maze of twisty compiler features, all different");
4053 #endif
4055 /* Just ignore #sccs, on systems where we define it at all. */
4057 static int
4058 do_sccs (pfile, keyword, buf, limit)
4059 cpp_reader *pfile;
4060 struct directive *keyword;
4061 U_CHAR *buf, *limit;
4063 if (CPP_PEDANTIC (pfile))
4064 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4065 return 0;
4069 * handle #if command by
4070 * 1) inserting special `defined' keyword into the hash table
4071 * that gets turned into 0 or 1 by special_symbol (thus,
4072 * if the luser has a symbol called `defined' already, it won't
4073 * work inside the #if command)
4074 * 2) rescan the input into a temporary output buffer
4075 * 3) pass the output buffer to the yacc parser and collect a value
4076 * 4) clean up the mess left from steps 1 and 2.
4077 * 5) call conditional_skip to skip til the next #endif (etc.),
4078 * or not, depending on the value from step 3.
4081 static int
4082 do_if (pfile, keyword, buf, limit)
4083 cpp_reader *pfile;
4084 struct directive *keyword;
4085 U_CHAR *buf, *limit;
4087 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4088 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4089 return 0;
4093 * handle a #elif directive by not changing if_stack either.
4094 * see the comment above do_else.
4097 static int
4098 do_elif (pfile, keyword, buf, limit)
4099 cpp_reader *pfile;
4100 struct directive *keyword;
4101 U_CHAR *buf, *limit;
4103 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4104 cpp_error (pfile, "`#elif' not within a conditional");
4105 return 0;
4106 } else {
4107 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4108 cpp_error (pfile, "`#elif' after `#else'");
4109 #if 0
4110 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4111 #endif
4112 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4113 && strcmp (pfile->if_stack->fname,
4114 CPP_BUFFER (pfile)->nominal_fname) != 0)
4115 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4116 fprintf (stderr, ")\n");
4118 pfile->if_stack->type = T_ELIF;
4121 if (pfile->if_stack->if_succeeded)
4122 skip_if_group (pfile, 0);
4123 else {
4124 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4125 if (value == 0)
4126 skip_if_group (pfile, 0);
4127 else {
4128 ++pfile->if_stack->if_succeeded; /* continue processing input */
4129 output_line_command (pfile, 1, same_file);
4132 return 0;
4136 * evaluate a #if expression in BUF, of length LENGTH,
4137 * then parse the result as a C expression and return the value as an int.
4140 static HOST_WIDE_INT
4141 eval_if_expression (pfile, buf, length)
4142 cpp_reader *pfile;
4143 U_CHAR *buf;
4144 int length;
4146 HASHNODE *save_defined;
4147 HOST_WIDE_INT value;
4148 long old_written = CPP_WRITTEN (pfile);
4150 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4151 pfile->pcp_inside_if = 1;
4153 value = cpp_parse_expr (pfile);
4154 pfile->pcp_inside_if = 0;
4155 delete_macro (save_defined); /* clean up special symbol */
4157 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4159 return value;
4163 * routine to handle ifdef/ifndef. Try to look up the symbol,
4164 * then do or don't skip to the #endif/#else/#elif depending
4165 * on what directive is actually being processed.
4168 static int
4169 do_xifdef (pfile, keyword, unused1, unused2)
4170 cpp_reader *pfile;
4171 struct directive *keyword;
4172 U_CHAR *unused1, *unused2;
4174 int skip;
4175 cpp_buffer *ip = CPP_BUFFER (pfile);
4176 U_CHAR *ident;
4177 int ident_length;
4178 enum cpp_token token;
4179 int start_of_file = 0;
4180 U_CHAR *control_macro = 0;
4181 int old_written = CPP_WRITTEN (pfile);
4183 /* Detect a #ifndef at start of file (not counting comments). */
4184 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4185 start_of_file = pfile->only_seen_white == 2;
4187 pfile->no_macro_expand++;
4188 token = get_directive_token (pfile);
4189 pfile->no_macro_expand--;
4191 ident = pfile->token_buffer + old_written;
4192 ident_length = CPP_WRITTEN (pfile) - old_written;
4193 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4195 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4197 skip = (keyword->type == T_IFDEF);
4198 if (! CPP_TRADITIONAL (pfile))
4199 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4201 else if (token == CPP_NAME)
4203 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4204 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4205 if (start_of_file && !skip)
4207 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4208 bcopy (ident, control_macro, ident_length + 1);
4211 else
4213 skip = (keyword->type == T_IFDEF);
4214 if (! CPP_TRADITIONAL (pfile))
4215 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4218 if (!CPP_TRADITIONAL (pfile))
4219 { int c;
4220 cpp_skip_hspace (pfile);
4221 c = PEEKC ();
4222 if (c != EOF && c != '\n')
4223 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4225 skip_rest_of_line (pfile);
4227 #if 0
4228 if (pcp_outfile) {
4229 /* Output a precondition for this macro. */
4230 if (hp && hp->value.defn->predefined)
4231 fprintf (pcp_outfile, "#define %s\n", hp->name);
4232 else {
4233 U_CHAR *cp = buf;
4234 fprintf (pcp_outfile, "#undef ");
4235 while (is_idchar[*cp]) /* Ick! */
4236 fputc (*cp++, pcp_outfile);
4237 putc ('\n', pcp_outfile);
4239 #endif
4241 conditional_skip (pfile, skip, T_IF, control_macro);
4242 return 0;
4245 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4246 If this is a #ifndef starting at the beginning of a file,
4247 CONTROL_MACRO is the macro name tested by the #ifndef.
4248 Otherwise, CONTROL_MACRO is 0. */
4250 static void
4251 conditional_skip (pfile, skip, type, control_macro)
4252 cpp_reader *pfile;
4253 int skip;
4254 enum node_type type;
4255 U_CHAR *control_macro;
4257 IF_STACK_FRAME *temp;
4259 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4260 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4261 #if 0
4262 temp->lineno = CPP_BUFFER (pfile)->lineno;
4263 #endif
4264 temp->next = pfile->if_stack;
4265 temp->control_macro = control_macro;
4266 pfile->if_stack = temp;
4268 pfile->if_stack->type = type;
4270 if (skip != 0) {
4271 skip_if_group (pfile, 0);
4272 return;
4273 } else {
4274 ++pfile->if_stack->if_succeeded;
4275 output_line_command (pfile, 1, same_file);
4280 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4281 * leaves input ptr at the sharp sign found.
4282 * If ANY is nonzero, return at next directive of any sort.
4285 static void
4286 skip_if_group (pfile, any)
4287 cpp_reader *pfile;
4288 int any;
4290 int c;
4291 struct directive *kt;
4292 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4293 #if 0
4294 U_CHAR *beg_of_line = bp;
4295 #endif
4296 register int ident_length;
4297 U_CHAR *ident;
4298 struct parse_marker line_start_mark;
4300 parse_set_mark (&line_start_mark, pfile);
4302 if (CPP_OPTIONS (pfile)->output_conditionals) {
4303 static char failed[] = "#failed\n";
4304 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4305 pfile->lineno++;
4306 output_line_command (pfile, 1, same_file);
4309 beg_of_line:
4310 if (CPP_OPTIONS (pfile)->output_conditionals)
4312 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4313 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4314 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4316 parse_move_mark (&line_start_mark, pfile);
4317 if (!CPP_TRADITIONAL (pfile))
4318 cpp_skip_hspace (pfile);
4319 c = GETC();
4320 if (c == '#')
4322 int old_written = CPP_WRITTEN (pfile);
4323 cpp_skip_hspace (pfile);
4325 parse_name (pfile, GETC());
4326 ident_length = CPP_WRITTEN (pfile) - old_written;
4327 ident = pfile->token_buffer + old_written;
4328 pfile->limit = ident;
4329 #if 0
4330 if (ident_length == 0)
4331 goto not_a_directive;
4333 /* Handle # followed by a line number. */
4335 /* Avoid error for `###' and similar cases unless -pedantic. */
4336 #endif
4338 for (kt = directive_table; kt->length >= 0; kt++)
4340 IF_STACK_FRAME *temp;
4341 if (ident_length == kt->length
4342 && strncmp (ident, kt->name, kt->length) == 0)
4344 /* If we are asked to return on next directive, do so now. */
4345 if (any)
4346 goto done;
4348 switch (kt->type)
4350 case T_IF:
4351 case T_IFDEF:
4352 case T_IFNDEF:
4353 temp
4354 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4355 temp->next = pfile->if_stack;
4356 pfile->if_stack = temp;
4357 #if 0
4358 temp->lineno = CPP_BUFFER(pfile)->lineno;
4359 #endif
4360 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4361 temp->type = kt->type;
4362 break;
4363 case T_ELSE:
4364 case T_ENDIF:
4365 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4366 validate_else (pfile,
4367 kt->type == T_ELSE ? "#else" : "#endif");
4368 case T_ELIF:
4369 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4371 cpp_error (pfile,
4372 "`#%s' not within a conditional", kt->name);
4373 break;
4375 else if (pfile->if_stack == save_if_stack)
4376 goto done; /* found what we came for */
4378 if (kt->type != T_ENDIF)
4380 if (pfile->if_stack->type == T_ELSE)
4381 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4382 pfile->if_stack->type = kt->type;
4383 break;
4386 temp = pfile->if_stack;
4387 pfile->if_stack = temp->next;
4388 free (temp);
4389 break;
4390 default: ;
4392 break;
4394 /* Don't let erroneous code go by. */
4395 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4396 && CPP_PEDANTIC (pfile))
4397 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4399 c = GETC ();
4401 /* We're in the middle of a line. Skip the rest of it. */
4402 for (;;) {
4403 switch (c)
4405 long old;
4406 case EOF:
4407 goto done;
4408 case '/': /* possible comment */
4409 c = skip_comment (pfile, NULL);
4410 if (c == EOF)
4411 goto done;
4412 break;
4413 case '\"':
4414 case '\'':
4415 FORWARD(-1);
4416 old = CPP_WRITTEN (pfile);
4417 cpp_get_token (pfile);
4418 CPP_SET_WRITTEN (pfile, old);
4419 break;
4420 case '\\':
4421 /* Char after backslash loses its special meaning. */
4422 if (PEEKC() == '\n')
4423 FORWARD (1);
4424 break;
4425 case '\n':
4426 goto beg_of_line;
4427 break;
4429 c = GETC ();
4431 done:
4432 if (CPP_OPTIONS (pfile)->output_conditionals) {
4433 static char end_failed[] = "#endfailed\n";
4434 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4435 pfile->lineno++;
4437 pfile->only_seen_white = 1;
4438 parse_goto_mark (&line_start_mark, pfile);
4439 parse_clear_mark (&line_start_mark);
4443 * handle a #else directive. Do this by just continuing processing
4444 * without changing if_stack ; this is so that the error message
4445 * for missing #endif's etc. will point to the original #if. It
4446 * is possible that something different would be better.
4449 static int
4450 do_else (pfile, keyword, buf, limit)
4451 cpp_reader *pfile;
4452 struct directive *keyword;
4453 U_CHAR *buf, *limit;
4455 cpp_buffer *ip = CPP_BUFFER (pfile);
4457 if (CPP_PEDANTIC (pfile))
4458 validate_else (pfile, "#else");
4459 skip_rest_of_line (pfile);
4461 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4462 cpp_error (pfile, "`#else' not within a conditional");
4463 return 0;
4464 } else {
4465 /* #ifndef can't have its special treatment for containing the whole file
4466 if it has a #else clause. */
4467 pfile->if_stack->control_macro = 0;
4469 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4470 cpp_error (pfile, "`#else' after `#else'");
4471 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4472 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4473 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4474 fprintf (stderr, ")\n");
4476 pfile->if_stack->type = T_ELSE;
4479 if (pfile->if_stack->if_succeeded)
4480 skip_if_group (pfile, 0);
4481 else {
4482 ++pfile->if_stack->if_succeeded; /* continue processing input */
4483 output_line_command (pfile, 1, same_file);
4485 return 0;
4489 * unstack after #endif command
4492 static int
4493 do_endif (pfile, keyword, buf, limit)
4494 cpp_reader *pfile;
4495 struct directive *keyword;
4496 U_CHAR *buf, *limit;
4498 if (CPP_PEDANTIC (pfile))
4499 validate_else (pfile, "#endif");
4500 skip_rest_of_line (pfile);
4502 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4503 cpp_error (pfile, "unbalanced `#endif'");
4504 else
4506 IF_STACK_FRAME *temp = pfile->if_stack;
4507 pfile->if_stack = temp->next;
4508 if (temp->control_macro != 0)
4510 /* This #endif matched a #ifndef at the start of the file.
4511 See if it is at the end of the file. */
4512 struct parse_marker start_mark;
4513 int c;
4515 parse_set_mark (&start_mark, pfile);
4517 for (;;)
4519 cpp_skip_hspace (pfile);
4520 c = GETC ();
4521 if (c != '\n')
4522 break;
4524 parse_goto_mark (&start_mark, pfile);
4525 parse_clear_mark (&start_mark);
4527 if (c == EOF)
4529 /* If we get here, this #endif ends a #ifndef
4530 that contains all of the file (aside from whitespace).
4531 Arrange not to include the file again
4532 if the macro that was tested is defined.
4534 Do not do this for the top-level file in a -include or any
4535 file in a -imacros. */
4536 #if 0
4537 FIXME!
4538 if (indepth != 0
4539 && ! (indepth == 1 && pfile->no_record_file)
4540 && ! (pfile->no_record_file && no_output))
4541 #endif
4543 struct file_name_list *ifile = pfile->all_include_files;
4545 for ( ; ifile != NULL; ifile = ifile->next)
4547 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4549 ifile->control_macro = temp->control_macro;
4550 break;
4556 free (temp);
4557 output_line_command (pfile, 1, same_file);
4559 return 0;
4562 /* When an #else or #endif is found while skipping failed conditional,
4563 if -pedantic was specified, this is called to warn about text after
4564 the command name. P points to the first char after the command name. */
4566 static void
4567 validate_else (pfile, directive)
4568 cpp_reader *pfile;
4569 char *directive;
4571 int c;
4572 cpp_skip_hspace (pfile);
4573 c = PEEKC ();
4574 if (c != EOF && c != '\n')
4575 cpp_pedwarn (pfile,
4576 "text following `%s' violates ANSI standard", directive);
4579 /* Get the next token, and add it to the text in pfile->token_buffer.
4580 Return the kind of token we got. */
4582 enum cpp_token
4583 cpp_get_token (pfile)
4584 cpp_reader *pfile;
4586 register int c, c2, c3;
4587 long old_written;
4588 long start_line, start_column;
4589 enum cpp_token token;
4590 struct cpp_options *opts = CPP_OPTIONS (pfile);
4591 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4592 get_next:
4593 c = GETC();
4594 if (c == EOF)
4596 handle_eof:
4597 if (CPP_BUFFER (pfile)->seen_eof)
4599 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4600 goto get_next;
4601 else
4602 return CPP_EOF;
4604 else
4606 cpp_buffer *next_buf
4607 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4608 CPP_BUFFER (pfile)->seen_eof = 1;
4609 if (CPP_BUFFER (pfile)->nominal_fname
4610 && next_buf != CPP_NULL_BUFFER (pfile))
4612 /* We're about to return from an #include file.
4613 Emit #line information now (as part of the CPP_POP) result.
4614 But the #line refers to the file we will pop to. */
4615 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4616 CPP_BUFFER (pfile) = next_buf;
4617 pfile->input_stack_listing_current = 0;
4618 output_line_command (pfile, 0, leave_file);
4619 CPP_BUFFER (pfile) = cur_buffer;
4621 return CPP_POP;
4624 else
4626 switch (c)
4628 long newlines;
4629 struct parse_marker start_mark;
4630 case '/':
4631 if (PEEKC () == '=')
4632 goto op2;
4633 if (opts->put_out_comments)
4634 parse_set_mark (&start_mark, pfile);
4635 newlines = 0;
4636 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4637 &start_line, &start_column);
4638 c = skip_comment (pfile, &newlines);
4639 if (opts->put_out_comments && (c == '/' || c == EOF))
4640 parse_clear_mark (&start_mark);
4641 if (c == '/')
4642 goto randomchar;
4643 if (c == EOF)
4645 cpp_error_with_line (pfile, start_line, start_column,
4646 "unterminated comment");
4647 goto handle_eof;
4649 c = '/'; /* Initial letter of comment. */
4650 return_comment:
4651 /* Comments are equivalent to spaces.
4652 For -traditional, a comment is equivalent to nothing. */
4653 if (opts->put_out_comments)
4655 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4656 U_CHAR *start = pbuf->buf + start_mark.position;
4657 int len = pbuf->cur - start;
4658 CPP_RESERVE(pfile, 1 + len);
4659 CPP_PUTC_Q (pfile, c);
4660 CPP_PUTS_Q (pfile, start, len);
4661 pfile->lineno += newlines;
4662 parse_clear_mark (&start_mark);
4663 return CPP_COMMENT;
4665 else if (CPP_TRADITIONAL (pfile))
4667 return CPP_COMMENT;
4669 else
4671 #if 0
4672 /* This may not work if cpp_get_token is called recursively,
4673 since many places look for horizontal space. */
4674 if (newlines)
4676 /* Copy the newlines into the output buffer, in order to
4677 avoid the pain of a #line every time a multiline comment
4678 is seen. */
4679 CPP_RESERVE(pfile, newlines);
4680 while (--newlines >= 0)
4682 CPP_PUTC_Q (pfile, '\n');
4683 pfile->lineno++;
4685 return CPP_VSPACE;
4687 #endif
4688 CPP_RESERVE(pfile, 1);
4689 CPP_PUTC_Q (pfile, ' ');
4690 return CPP_HSPACE;
4692 #if 0
4693 if (opts->for_lint) {
4694 U_CHAR *argbp;
4695 int cmdlen, arglen;
4696 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4698 if (lintcmd != NULL) {
4699 /* I believe it is always safe to emit this newline: */
4700 obp[-1] = '\n';
4701 bcopy ("#pragma lint ", (char *) obp, 13);
4702 obp += 13;
4703 bcopy (lintcmd, (char *) obp, cmdlen);
4704 obp += cmdlen;
4706 if (arglen != 0) {
4707 *(obp++) = ' ';
4708 bcopy (argbp, (char *) obp, arglen);
4709 obp += arglen;
4712 /* OK, now bring us back to the state we were in before we entered
4713 this branch. We need #line because the newline for the pragma
4714 could mess things up. */
4715 output_line_command (pfile, 0, same_file);
4716 *(obp++) = ' '; /* just in case, if comments are copied thru */
4717 *(obp++) = '/';
4720 #endif
4722 case '#':
4723 #if 0
4724 /* If this is expanding a macro definition, don't recognize
4725 preprocessor directives. */
4726 if (ip->macro != 0)
4727 goto randomchar;
4728 /* If this is expand_into_temp_buffer, recognize them
4729 only after an actual newline at this level,
4730 not at the beginning of the input level. */
4731 if (ip->fname == 0 && beg_of_line == ip->buf)
4732 goto randomchar;
4733 if (ident_length)
4734 goto specialchar;
4735 #endif
4737 if (!pfile->only_seen_white)
4738 goto randomchar;
4739 if (handle_directive (pfile))
4740 return CPP_DIRECTIVE;
4741 pfile->only_seen_white = 0;
4742 return CPP_OTHER;
4744 case '\"':
4745 case '\'':
4746 /* A single quoted string is treated like a double -- some
4747 programs (e.g., troff) are perverse this way */
4748 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4749 &start_line, &start_column);
4750 old_written = CPP_WRITTEN (pfile);
4751 string:
4752 CPP_PUTC (pfile, c);
4753 while (1)
4755 int cc = GETC();
4756 if (cc == EOF)
4758 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4760 /* try harder: this string crosses a macro expansion
4761 boundary. This can happen naturally if -traditional.
4762 Otherwise, only -D can make a macro with an unmatched
4763 quote. */
4764 cpp_buffer *next_buf
4765 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4766 (*CPP_BUFFER (pfile)->cleanup)
4767 (CPP_BUFFER (pfile), pfile);
4768 CPP_BUFFER (pfile) = next_buf;
4769 continue;
4771 if (!CPP_TRADITIONAL (pfile))
4773 cpp_error_with_line (pfile, start_line, start_column,
4774 "unterminated string or character constant");
4775 if (pfile->multiline_string_line != start_line
4776 && pfile->multiline_string_line != 0)
4777 cpp_error_with_line (pfile,
4778 pfile->multiline_string_line, -1,
4779 "possible real start of unterminated constant");
4780 pfile->multiline_string_line = 0;
4782 break;
4784 CPP_PUTC (pfile, cc);
4785 switch (cc)
4787 case '\n':
4788 /* Traditionally, end of line ends a string constant with
4789 no error. So exit the loop and record the new line. */
4790 if (CPP_TRADITIONAL (pfile))
4791 goto while2end;
4792 if (c == '\'')
4794 cpp_error_with_line (pfile, start_line, start_column,
4795 "unterminated character constant");
4796 goto while2end;
4798 if (CPP_PEDANTIC (pfile)
4799 && pfile->multiline_string_line == 0)
4801 cpp_pedwarn_with_line (pfile, start_line, start_column,
4802 "string constant runs past end of line");
4804 if (pfile->multiline_string_line == 0)
4805 pfile->multiline_string_line = start_line;
4806 break;
4808 case '\\':
4809 cc = GETC();
4810 if (cc == '\n')
4812 /* Backslash newline is replaced by nothing at all. */
4813 CPP_ADJUST_WRITTEN (pfile, -1);
4814 pfile->lineno++;
4816 else
4818 /* ANSI stupidly requires that in \\ the second \
4819 is *not* prevented from combining with a newline. */
4820 NEWLINE_FIX1(cc);
4821 if (cc != EOF)
4822 CPP_PUTC (pfile, cc);
4824 break;
4826 case '\"':
4827 case '\'':
4828 if (cc == c)
4829 goto while2end;
4830 break;
4833 while2end:
4834 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4835 CPP_PWRITTEN (pfile));
4836 pfile->only_seen_white = 0;
4837 return c == '\'' ? CPP_CHAR : CPP_STRING;
4839 case '$':
4840 if (!opts->dollars_in_ident)
4841 goto randomchar;
4842 goto letter;
4844 case ':':
4845 if (opts->cplusplus && PEEKC () == ':')
4846 goto op2;
4847 goto randomchar;
4849 case '&':
4850 case '+':
4851 case '|':
4852 NEWLINE_FIX;
4853 c2 = PEEKC ();
4854 if (c2 == c || c2 == '=')
4855 goto op2;
4856 goto randomchar;
4858 case '*':
4859 case '!':
4860 case '%':
4861 case '=':
4862 case '^':
4863 NEWLINE_FIX;
4864 if (PEEKC () == '=')
4865 goto op2;
4866 goto randomchar;
4868 case '-':
4869 NEWLINE_FIX;
4870 c2 = PEEKC ();
4871 if (c2 == '-' && opts->chill)
4873 /* Chill style comment */
4874 if (opts->put_out_comments)
4875 parse_set_mark (&start_mark, pfile);
4876 FORWARD(1); /* Skip second '-'. */
4877 for (;;)
4879 c = GETC ();
4880 if (c == EOF)
4881 break;
4882 if (c == '\n')
4884 /* Don't consider final '\n' to be part of comment. */
4885 FORWARD(-1);
4886 break;
4889 c = '-';
4890 goto return_comment;
4892 if (c2 == '-' || c2 == '=' || c2 == '>')
4893 goto op2;
4894 goto randomchar;
4896 case '<':
4897 if (pfile->parsing_include_directive)
4899 for (;;)
4901 CPP_PUTC (pfile, c);
4902 if (c == '>')
4903 break;
4904 c = GETC ();
4905 NEWLINE_FIX1 (c);
4906 if (c == '\n' || c == EOF)
4908 cpp_error (pfile,
4909 "missing '>' in `#include <FILENAME>'");
4910 break;
4913 return CPP_STRING;
4915 /* else fall through */
4916 case '>':
4917 NEWLINE_FIX;
4918 c2 = PEEKC ();
4919 if (c2 == '=')
4920 goto op2;
4921 if (c2 != c)
4922 goto randomchar;
4923 FORWARD(1);
4924 CPP_RESERVE (pfile, 4);
4925 CPP_PUTC (pfile, c);
4926 CPP_PUTC (pfile, c2);
4927 NEWLINE_FIX;
4928 c3 = PEEKC ();
4929 if (c3 == '=')
4930 CPP_PUTC_Q (pfile, GETC ());
4931 CPP_NUL_TERMINATE_Q (pfile);
4932 pfile->only_seen_white = 0;
4933 return CPP_OTHER;
4935 case '@':
4936 if (CPP_BUFFER (pfile)->has_escapes)
4938 c = GETC ();
4939 if (c == '-')
4941 if (pfile->output_escapes)
4942 CPP_PUTS (pfile, "@-", 2);
4943 parse_name (pfile, GETC ());
4944 return CPP_NAME;
4946 else if (is_space [c])
4948 CPP_RESERVE (pfile, 2);
4949 if (pfile->output_escapes)
4950 CPP_PUTC_Q (pfile, '@');
4951 CPP_PUTC_Q (pfile, c);
4952 return CPP_HSPACE;
4955 if (pfile->output_escapes)
4957 CPP_PUTS (pfile, "@@", 2);
4958 return CPP_OTHER;
4960 goto randomchar;
4962 case '.':
4963 NEWLINE_FIX;
4964 c2 = PEEKC ();
4965 if (isdigit(c2))
4967 CPP_RESERVE(pfile, 2);
4968 CPP_PUTC_Q (pfile, '.');
4969 c = GETC ();
4970 goto number;
4972 /* FIXME - misses the case "..\\\n." */
4973 if (c2 == '.' && PEEKN(1) == '.')
4975 CPP_RESERVE(pfile, 4);
4976 CPP_PUTC_Q (pfile, '.');
4977 CPP_PUTC_Q (pfile, '.');
4978 CPP_PUTC_Q (pfile, '.');
4979 FORWARD (2);
4980 CPP_NUL_TERMINATE_Q (pfile);
4981 pfile->only_seen_white = 0;
4982 return CPP_3DOTS;
4984 goto randomchar;
4986 op2:
4987 token = CPP_OTHER;
4988 pfile->only_seen_white = 0;
4989 op2any:
4990 CPP_RESERVE(pfile, 3);
4991 CPP_PUTC_Q (pfile, c);
4992 CPP_PUTC_Q (pfile, GETC ());
4993 CPP_NUL_TERMINATE_Q (pfile);
4994 return token;
4996 case 'L':
4997 NEWLINE_FIX;
4998 c2 = PEEKC ();
4999 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5001 CPP_PUTC (pfile, c);
5002 c = GETC ();
5003 goto string;
5005 goto letter;
5007 case '0': case '1': case '2': case '3': case '4':
5008 case '5': case '6': case '7': case '8': case '9':
5009 number:
5010 c2 = '.';
5011 for (;;)
5013 CPP_RESERVE (pfile, 2);
5014 CPP_PUTC_Q (pfile, c);
5015 NEWLINE_FIX;
5016 c = PEEKC ();
5017 if (c == EOF)
5018 break;
5019 if (!is_idchar[c] && c != '.'
5020 && ((c2 != 'e' && c2 != 'E'
5021 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
5022 || (c != '+' && c != '-')))
5023 break;
5024 FORWARD(1);
5025 c2= c;
5027 CPP_NUL_TERMINATE_Q (pfile);
5028 pfile->only_seen_white = 0;
5029 return CPP_NUMBER;
5030 case 'b': case 'c': case 'd': case 'h': case 'o':
5031 case 'B': case 'C': case 'D': case 'H': case 'O':
5032 if (opts->chill && PEEKC () == '\'')
5034 pfile->only_seen_white = 0;
5035 CPP_RESERVE (pfile, 2);
5036 CPP_PUTC_Q (pfile, c);
5037 CPP_PUTC_Q (pfile, '\'');
5038 FORWARD(1);
5039 for (;;)
5041 c = GETC();
5042 if (c == EOF)
5043 goto chill_number_eof;
5044 if (!is_idchar[c])
5046 if (c == '\\' && PEEKC() == '\n')
5048 FORWARD(2);
5049 continue;
5051 break;
5053 CPP_PUTC (pfile, c);
5055 if (c == '\'')
5057 CPP_RESERVE (pfile, 2);
5058 CPP_PUTC_Q (pfile, c);
5059 CPP_NUL_TERMINATE_Q (pfile);
5060 return CPP_STRING;
5062 else
5064 FORWARD(-1);
5065 chill_number_eof:
5066 CPP_NUL_TERMINATE (pfile);
5067 return CPP_NUMBER;
5070 else
5071 goto letter;
5072 case '_':
5073 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5074 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5075 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5076 case 'x': case 'y': case 'z':
5077 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5078 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5079 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5080 case 'Y': case 'Z':
5081 letter:
5083 HASHNODE *hp;
5084 unsigned char *ident;
5085 int before_name_written = CPP_WRITTEN (pfile);
5086 int ident_len;
5087 parse_name (pfile, c);
5088 pfile->only_seen_white = 0;
5089 if (pfile->no_macro_expand)
5090 return CPP_NAME;
5091 ident = pfile->token_buffer + before_name_written;
5092 ident_len = CPP_PWRITTEN (pfile) - ident;
5093 hp = cpp_lookup (pfile, ident, ident_len, -1);
5094 if (!hp)
5095 return CPP_NAME;
5096 if (hp->type == T_DISABLED)
5098 if (pfile->output_escapes)
5099 { /* Return "@-IDENT", followed by '\0'. */
5100 int i;
5101 CPP_RESERVE (pfile, 3);
5102 ident = pfile->token_buffer + before_name_written;
5103 CPP_ADJUST_WRITTEN (pfile, 2);
5104 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5105 ident[0] = '@';
5106 ident[1] = '-';
5108 return CPP_NAME;
5111 /* If macro wants an arglist, verify that a '(' follows.
5112 first skip all whitespace, copying it to the output
5113 after the macro name. Then, if there is no '(',
5114 decide this is not a macro call and leave things that way. */
5115 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5117 struct parse_marker macro_mark;
5118 int is_macro_call;
5119 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5121 cpp_buffer *next_buf;
5122 cpp_skip_hspace (pfile);
5123 if (PEEKC () != EOF)
5124 break;
5125 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5126 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5127 CPP_BUFFER (pfile) = next_buf;
5129 parse_set_mark (&macro_mark, pfile);
5130 for (;;)
5132 cpp_skip_hspace (pfile);
5133 c = PEEKC ();
5134 is_macro_call = c == '(';
5135 if (c != '\n')
5136 break;
5137 FORWARD (1);
5139 if (!is_macro_call)
5140 parse_goto_mark (&macro_mark, pfile);
5141 parse_clear_mark (&macro_mark);
5142 if (!is_macro_call)
5143 return CPP_NAME;
5145 /* This is now known to be a macro call. */
5147 /* it might not actually be a macro. */
5148 if (hp->type != T_MACRO) {
5149 int xbuf_len; U_CHAR *xbuf;
5150 CPP_SET_WRITTEN (pfile, before_name_written);
5151 special_symbol (hp, pfile);
5152 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5153 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5154 CPP_SET_WRITTEN (pfile, before_name_written);
5155 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5156 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5158 else
5160 /* Expand the macro, reading arguments as needed,
5161 and push the expansion on the input stack. */
5162 macroexpand (pfile, hp);
5163 CPP_SET_WRITTEN (pfile, before_name_written);
5166 /* An extra "@ " is added to the end of a macro expansion
5167 to prevent accidental token pasting. We prefer to avoid
5168 unneeded extra spaces (for the sake of cpp-using tools like
5169 imake). Here we remove the space if it is safe to do so. */
5170 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5171 && pfile->buffer->rlimit[-2] == '@'
5172 && pfile->buffer->rlimit[-1] == ' ')
5174 int c1 = pfile->buffer->rlimit[-3];
5175 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5176 if (c2 == EOF || ! unsafe_chars (c1, c2))
5177 pfile->buffer->rlimit -= 2;
5180 goto get_next;
5182 case ' ': case '\t': case '\v': case '\r':
5183 for (;;)
5185 CPP_PUTC (pfile, c);
5186 c = PEEKC ();
5187 if (c == EOF || !is_hor_space[c])
5188 break;
5189 FORWARD(1);
5191 return CPP_HSPACE;
5193 case '\\':
5194 c2 = PEEKC ();
5195 if (c2 != '\n')
5196 goto randomchar;
5197 token = CPP_HSPACE;
5198 goto op2any;
5200 case '\n':
5201 CPP_PUTC (pfile, c);
5202 if (pfile->only_seen_white == 0)
5203 pfile->only_seen_white = 1;
5204 pfile->lineno++;
5205 output_line_command (pfile, 1, same_file);
5206 return CPP_VSPACE;
5208 case '(': token = CPP_LPAREN; goto char1;
5209 case ')': token = CPP_RPAREN; goto char1;
5210 case '{': token = CPP_LBRACE; goto char1;
5211 case '}': token = CPP_RBRACE; goto char1;
5212 case ',': token = CPP_COMMA; goto char1;
5213 case ';': token = CPP_SEMICOLON; goto char1;
5215 randomchar:
5216 default:
5217 token = CPP_OTHER;
5218 char1:
5219 pfile->only_seen_white = 0;
5220 CPP_PUTC (pfile, c);
5221 return token;
5226 /* Like cpp_get_token, but skip spaces and comments. */
5228 enum cpp_token
5229 cpp_get_non_space_token (pfile)
5230 cpp_reader *pfile;
5232 int old_written = CPP_WRITTEN (pfile);
5233 for (;;)
5235 enum cpp_token token = cpp_get_token (pfile);
5236 if (token != CPP_COMMENT && token != CPP_POP
5237 && token != CPP_HSPACE && token != CPP_VSPACE)
5238 return token;
5239 CPP_SET_WRITTEN (pfile, old_written);
5243 /* Parse an identifier starting with C. */
5246 parse_name (pfile, c)
5247 cpp_reader *pfile; int c;
5249 for (;;)
5251 if (! is_idchar[c])
5253 if (c == '\\' && PEEKC() == '\n')
5255 FORWARD(2);
5256 continue;
5258 FORWARD (-1);
5259 break;
5262 if (c == '$' && CPP_PEDANTIC (pfile))
5263 cpp_pedwarn ("`$' in identifier");
5265 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5266 CPP_PUTC_Q (pfile, c);
5267 c = GETC();
5268 if (c == EOF)
5269 break;
5271 CPP_NUL_TERMINATE_Q (pfile);
5272 return 1;
5276 /* Maintain and search list of included files, for #import. */
5278 /* Hash a file name for import_hash_table. */
5280 static int
5281 import_hash (f)
5282 char *f;
5284 int val = 0;
5286 while (*f) val += *f++;
5287 return (val%IMPORT_HASH_SIZE);
5290 /* Search for file FILENAME in import_hash_table.
5291 Return -2 if found, either a matching name or a matching inode.
5292 Otherwise, open the file and return a file descriptor if successful
5293 or -1 if unsuccessful. */
5295 static int
5296 lookup_import (pfile, filename, searchptr)
5297 cpp_reader *pfile;
5298 char *filename;
5299 struct file_name_list *searchptr;
5301 struct import_file *i;
5302 int h;
5303 int hashval;
5304 struct stat sb;
5305 int fd;
5307 hashval = import_hash (filename);
5309 /* Attempt to find file in list of already included files */
5310 i = pfile->import_hash_table[hashval];
5312 while (i) {
5313 if (!strcmp (filename, i->name))
5314 return -2; /* return found */
5315 i = i->next;
5317 /* Open it and try a match on inode/dev */
5318 fd = open_include_file (pfile, filename, searchptr);
5319 if (fd < 0)
5320 return fd;
5321 fstat (fd, &sb);
5322 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5323 i = pfile->import_hash_table[h];
5324 while (i) {
5325 /* Compare the inode and the device.
5326 Supposedly on some systems the inode is not a scalar. */
5327 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5328 && i->dev == sb.st_dev) {
5329 close (fd);
5330 return -2; /* return found */
5332 i = i->next;
5335 return fd; /* Not found, return open file */
5338 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5340 static void
5341 add_import (pfile, fd, fname)
5342 cpp_reader *pfile;
5343 int fd;
5344 char *fname;
5346 struct import_file *i;
5347 int hashval;
5348 struct stat sb;
5350 hashval = import_hash (fname);
5351 fstat (fd, &sb);
5352 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5353 i->name = (char *)xmalloc (strlen (fname)+1);
5354 strcpy (i->name, fname);
5355 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5356 i->dev = sb.st_dev;
5357 i->next = pfile->import_hash_table[hashval];
5358 pfile->import_hash_table[hashval] = i;
5361 /* The file_name_map structure holds a mapping of file names for a
5362 particular directory. This mapping is read from the file named
5363 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5364 map filenames on a file system with severe filename restrictions,
5365 such as DOS. The format of the file name map file is just a series
5366 of lines with two tokens on each line. The first token is the name
5367 to map, and the second token is the actual name to use. */
5369 struct file_name_map
5371 struct file_name_map *map_next;
5372 char *map_from;
5373 char *map_to;
5376 #define FILE_NAME_MAP_FILE "header.gcc"
5378 /* Read a space delimited string of unlimited length from a stdio
5379 file. */
5381 static char *
5382 read_filename_string (ch, f)
5383 int ch;
5384 FILE *f;
5386 char *alloc, *set;
5387 int len;
5389 len = 20;
5390 set = alloc = xmalloc (len + 1);
5391 if (! is_space[ch])
5393 *set++ = ch;
5394 while ((ch = getc (f)) != EOF && ! is_space[ch])
5396 if (set - alloc == len)
5398 len *= 2;
5399 alloc = xrealloc (alloc, len + 1);
5400 set = alloc + len / 2;
5402 *set++ = ch;
5405 *set = '\0';
5406 ungetc (ch, f);
5407 return alloc;
5410 /* This structure holds a linked list of file name maps, one per directory. */
5412 struct file_name_map_list
5414 struct file_name_map_list *map_list_next;
5415 char *map_list_name;
5416 struct file_name_map *map_list_map;
5419 /* Read the file name map file for DIRNAME. */
5421 static struct file_name_map *
5422 read_name_map (pfile, dirname)
5423 cpp_reader *pfile;
5424 char *dirname;
5426 register struct file_name_map_list *map_list_ptr;
5427 char *name;
5428 FILE *f;
5430 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5431 map_list_ptr = map_list_ptr->map_list_next)
5432 if (! strcmp (map_list_ptr->map_list_name, dirname))
5433 return map_list_ptr->map_list_map;
5435 map_list_ptr = ((struct file_name_map_list *)
5436 xmalloc (sizeof (struct file_name_map_list)));
5437 map_list_ptr->map_list_name = savestring (dirname);
5438 map_list_ptr->map_list_map = NULL;
5440 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5441 strcpy (name, dirname);
5442 if (*dirname)
5443 strcat (name, "/");
5444 strcat (name, FILE_NAME_MAP_FILE);
5445 f = fopen (name, "r");
5446 if (!f)
5447 map_list_ptr->map_list_map = NULL;
5448 else
5450 int ch;
5451 int dirlen = strlen (dirname);
5453 while ((ch = getc (f)) != EOF)
5455 char *from, *to;
5456 struct file_name_map *ptr;
5458 if (is_space[ch])
5459 continue;
5460 from = read_filename_string (ch, f);
5461 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5463 to = read_filename_string (ch, f);
5465 ptr = ((struct file_name_map *)
5466 xmalloc (sizeof (struct file_name_map)));
5467 ptr->map_from = from;
5469 /* Make the real filename absolute. */
5470 if (*to == '/')
5471 ptr->map_to = to;
5472 else
5474 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5475 strcpy (ptr->map_to, dirname);
5476 ptr->map_to[dirlen] = '/';
5477 strcpy (ptr->map_to + dirlen + 1, to);
5478 free (to);
5481 ptr->map_next = map_list_ptr->map_list_map;
5482 map_list_ptr->map_list_map = ptr;
5484 while ((ch = getc (f)) != '\n')
5485 if (ch == EOF)
5486 break;
5488 fclose (f);
5491 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5492 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5494 return map_list_ptr->map_list_map;
5497 /* Try to open include file FILENAME. SEARCHPTR is the directory
5498 being tried from the include file search path. This function maps
5499 filenames on file systems based on information read by
5500 read_name_map. */
5502 static int
5503 open_include_file (pfile, filename, searchptr)
5504 cpp_reader *pfile;
5505 char *filename;
5506 struct file_name_list *searchptr;
5508 if (CPP_OPTIONS (pfile)->remap)
5510 register struct file_name_map *map;
5511 register char *from;
5512 char *p, *dir;
5514 if (searchptr && ! searchptr->got_name_map)
5516 searchptr->name_map = read_name_map (pfile,
5517 searchptr->fname
5518 ? searchptr->fname : ".");
5519 searchptr->got_name_map = 1;
5522 /* First check the mapping for the directory we are using. */
5523 if (searchptr && searchptr->name_map)
5525 from = filename;
5526 if (searchptr->fname)
5527 from += strlen (searchptr->fname) + 1;
5528 for (map = searchptr->name_map; map; map = map->map_next)
5530 if (! strcmp (map->map_from, from))
5532 /* Found a match. */
5533 return open (map->map_to, O_RDONLY, 0666);
5538 /* Try to find a mapping file for the particular directory we are
5539 looking in. Thus #include <sys/types.h> will look up sys/types.h
5540 in /usr/include/header.gcc and look up types.h in
5541 /usr/include/sys/header.gcc. */
5542 p = rindex (filename, '/');
5543 if (! p)
5544 p = filename;
5545 if (searchptr
5546 && searchptr->fname
5547 && strlen (searchptr->fname) == p - filename
5548 && ! strncmp (searchptr->fname, filename, p - filename))
5550 /* FILENAME is in SEARCHPTR, which we've already checked. */
5551 return open (filename, O_RDONLY, 0666);
5554 if (p == filename)
5556 dir = ".";
5557 from = filename;
5559 else
5561 dir = (char *) alloca (p - filename + 1);
5562 bcopy (filename, dir, p - filename);
5563 dir[p - filename] = '\0';
5564 from = p + 1;
5566 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5567 if (! strcmp (map->map_from, from))
5568 return open (map->map_to, O_RDONLY, 0666);
5571 return open (filename, O_RDONLY, 0666);
5574 /* Process the contents of include file FNAME, already open on descriptor F,
5575 with output to OP.
5576 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5577 "system" include directories (as decided by the `is_system_include'
5578 function above).
5579 DIRPTR is the link in the dir path through which this file was found,
5580 or 0 if the file name was absolute or via the current directory.
5581 Return 1 on success, 0 on failure.
5583 The caller is responsible for the cpp_push_buffer. */
5585 static int
5586 finclude (pfile, f, fname, system_header_p, dirptr)
5587 cpp_reader *pfile;
5588 int f;
5589 char *fname;
5590 int system_header_p;
5591 struct file_name_list *dirptr;
5593 struct stat st;
5594 size_t st_size;
5595 long i;
5596 int length;
5597 cpp_buffer *fp; /* For input stack frame */
5598 #if 0
5599 int missing_newline = 0;
5600 #endif
5602 if (fstat (f, &st) < 0)
5604 cpp_perror_with_name (pfile, fname);
5605 close (f);
5606 cpp_pop_buffer (pfile);
5607 return 0;
5610 fp = CPP_BUFFER (pfile);
5611 fp->nominal_fname = fp->fname = fname;
5612 #if 0
5613 fp->length = 0;
5614 #endif
5615 fp->dir = dirptr;
5616 fp->system_header_p = system_header_p;
5617 fp->lineno = 1;
5618 fp->colno = 1;
5619 fp->cleanup = file_cleanup;
5621 if (S_ISREG (st.st_mode)) {
5622 st_size = (size_t) st.st_size;
5623 if (st_size != st.st_size || st_size + 2 < st_size) {
5624 cpp_error (pfile, "file `%s' too large", fname);
5625 close (f);
5626 return 0;
5628 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5629 fp->alimit = fp->buf + st_size + 2;
5630 fp->cur = fp->buf;
5632 /* Read the file contents, knowing that st_size is an upper bound
5633 on the number of bytes we can read. */
5634 length = safe_read (f, fp->buf, st_size);
5635 fp->rlimit = fp->buf + length;
5636 if (length < 0) goto nope;
5638 else if (S_ISDIR (st.st_mode)) {
5639 cpp_error (pfile, "directory `%s' specified in #include", fname);
5640 close (f);
5641 return 0;
5642 } else {
5643 /* Cannot count its file size before reading.
5644 First read the entire file into heap and
5645 copy them into buffer on stack. */
5647 int bsize = 2000;
5649 st_size = 0;
5650 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5652 for (;;) {
5653 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5654 if (i < 0)
5655 goto nope; /* error! */
5656 st_size += i;
5657 if (st_size != bsize)
5658 break; /* End of file */
5659 bsize *= 2;
5660 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5662 fp->cur = fp->buf;
5663 length = st_size;
5666 if ((length > 0 && fp->buf[length - 1] != '\n')
5667 /* Backslash-newline at end is not good enough. */
5668 || (length > 1 && fp->buf[length - 2] == '\\')) {
5669 fp->buf[length++] = '\n';
5670 #if 0
5671 missing_newline = 1;
5672 #endif
5674 fp->buf[length] = '\0';
5675 fp->rlimit = fp->buf + length;
5677 /* Close descriptor now, so nesting does not use lots of descriptors. */
5678 close (f);
5680 /* Must do this before calling trigraph_pcp, so that the correct file name
5681 will be printed in warning messages. */
5683 pfile->input_stack_listing_current = 0;
5685 #if 0
5686 if (!no_trigraphs)
5687 trigraph_pcp (fp);
5688 #endif
5690 #if 0
5691 rescan (op, 0);
5693 if (missing_newline)
5694 fp->lineno--;
5696 if (CPP_PEDANTIC (pfile) && missing_newline)
5697 pedwarn ("file does not end in newline");
5699 indepth--;
5700 input_file_stack_tick++;
5701 free (fp->buf);
5702 #endif
5703 return 1;
5705 nope:
5707 cpp_perror_with_name (pfile, fname);
5708 close (f);
5709 free (fp->buf);
5710 return 1;
5713 /* This is called after options have been processed.
5714 * Check options for consistency, and setup for processing input
5715 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5716 * Return 1 on success, 0 on failure.
5720 cpp_start_read (pfile, fname)
5721 cpp_reader *pfile;
5722 char *fname;
5724 struct cpp_options *opts = CPP_OPTIONS (pfile);
5725 struct cpp_pending *pend;
5726 char *p;
5727 int f;
5728 cpp_buffer *fp;
5730 /* The code looks at the defaults through this pointer, rather than through
5731 the constant structure above. This pointer gets changed if an environment
5732 variable specifies other defaults. */
5733 struct default_include *include_defaults = include_defaults_array;
5735 /* Add dirs from CPATH after dirs from -I. */
5736 /* There seems to be confusion about what CPATH should do,
5737 so for the moment it is not documented. */
5738 /* Some people say that CPATH should replace the standard include dirs,
5739 but that seems pointless: it comes before them, so it overrides them
5740 anyway. */
5741 p = (char *) getenv ("CPATH");
5742 if (p != 0 && ! opts->no_standard_includes)
5743 path_include (pfile, p);
5745 /* Now that dollars_in_ident is known, initialize is_idchar. */
5746 initialize_char_syntax (opts);
5748 /* Do partial setup of input buffer for the sake of generating
5749 early #line directives (when -g is in effect). */
5750 fp = cpp_push_buffer (pfile, NULL, 0);
5751 if (!fp)
5752 return 0;
5753 if (opts->in_fname == NULL)
5754 opts->in_fname = "";
5755 fp->nominal_fname = fp->fname = opts->in_fname;
5756 fp->lineno = 0;
5758 /* Install __LINE__, etc. Must follow initialize_char_syntax
5759 and option processing. */
5760 initialize_builtins (pfile);
5762 /* Do standard #defines and assertions
5763 that identify system and machine type. */
5765 if (!opts->inhibit_predefs) {
5766 char *p = (char *) alloca (strlen (predefs) + 1);
5767 strcpy (p, predefs);
5768 while (*p) {
5769 char *q;
5770 while (*p == ' ' || *p == '\t')
5771 p++;
5772 /* Handle -D options. */
5773 if (p[0] == '-' && p[1] == 'D') {
5774 q = &p[2];
5775 while (*p && *p != ' ' && *p != '\t')
5776 p++;
5777 if (*p != 0)
5778 *p++= 0;
5779 if (opts->debug_output)
5780 output_line_command (pfile, 0, same_file);
5781 cpp_define (pfile, q);
5782 while (*p == ' ' || *p == '\t')
5783 p++;
5784 } else if (p[0] == '-' && p[1] == 'A') {
5785 /* Handle -A options (assertions). */
5786 char *assertion;
5787 char *past_name;
5788 char *value;
5789 char *past_value;
5790 char *termination;
5791 int save_char;
5793 assertion = &p[2];
5794 past_name = assertion;
5795 /* Locate end of name. */
5796 while (*past_name && *past_name != ' '
5797 && *past_name != '\t' && *past_name != '(')
5798 past_name++;
5799 /* Locate `(' at start of value. */
5800 value = past_name;
5801 while (*value && (*value == ' ' || *value == '\t'))
5802 value++;
5803 if (*value++ != '(')
5804 abort ();
5805 while (*value && (*value == ' ' || *value == '\t'))
5806 value++;
5807 past_value = value;
5808 /* Locate end of value. */
5809 while (*past_value && *past_value != ' '
5810 && *past_value != '\t' && *past_value != ')')
5811 past_value++;
5812 termination = past_value;
5813 while (*termination && (*termination == ' ' || *termination == '\t'))
5814 termination++;
5815 if (*termination++ != ')')
5816 abort ();
5817 if (*termination && *termination != ' ' && *termination != '\t')
5818 abort ();
5819 /* Temporarily null-terminate the value. */
5820 save_char = *termination;
5821 *termination = '\0';
5822 /* Install the assertion. */
5823 make_assertion (pfile, "-A", assertion);
5824 *termination = (char) save_char;
5825 p = termination;
5826 while (*p == ' ' || *p == '\t')
5827 p++;
5828 } else {
5829 abort ();
5834 /* Now handle the command line options. */
5836 /* Do -U's, -D's and -A's in the order they were seen. */
5837 /* First reverse the list. */
5838 opts->pending = nreverse_pending (opts->pending);
5840 for (pend = opts->pending; pend; pend = pend->next)
5842 if (pend->cmd != NULL && pend->cmd[0] == '-')
5844 switch (pend->cmd[1])
5846 case 'U':
5847 if (opts->debug_output)
5848 output_line_command (pfile, 0, same_file);
5849 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5850 break;
5851 case 'D':
5852 if (opts->debug_output)
5853 output_line_command (pfile, 0, same_file);
5854 cpp_define (pfile, pend->arg);
5855 break;
5856 case 'A':
5857 make_assertion (pfile, "-A", pend->arg);
5858 break;
5863 opts->done_initializing = 1;
5865 { /* Read the appropriate environment variable and if it exists
5866 replace include_defaults with the listed path. */
5867 char *epath = 0;
5868 switch ((opts->objc << 1) + opts->cplusplus)
5870 case 0:
5871 epath = getenv ("C_INCLUDE_PATH");
5872 break;
5873 case 1:
5874 epath = getenv ("CPLUS_INCLUDE_PATH");
5875 break;
5876 case 2:
5877 epath = getenv ("OBJC_INCLUDE_PATH");
5878 break;
5879 case 3:
5880 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5881 break;
5883 /* If the environment var for this language is set,
5884 add to the default list of include directories. */
5885 if (epath) {
5886 char *nstore = (char *) alloca (strlen (epath) + 2);
5887 int num_dirs;
5888 char *startp, *endp;
5890 for (num_dirs = 1, startp = epath; *startp; startp++)
5891 if (*startp == PATH_SEPARATOR)
5892 num_dirs++;
5893 include_defaults
5894 = (struct default_include *) xmalloc ((num_dirs
5895 * sizeof (struct default_include))
5896 + sizeof (include_defaults_array));
5897 startp = endp = epath;
5898 num_dirs = 0;
5899 while (1) {
5900 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5901 if ((*endp == PATH_SEPARATOR)
5902 || *endp == 0) {
5903 strncpy (nstore, startp, endp-startp);
5904 if (endp == startp)
5905 strcpy (nstore, ".");
5906 else
5907 nstore[endp-startp] = '\0';
5909 include_defaults[num_dirs].fname = savestring (nstore);
5910 include_defaults[num_dirs].component = 0;
5911 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5912 include_defaults[num_dirs].cxx_aware = 1;
5913 num_dirs++;
5914 if (*endp == '\0')
5915 break;
5916 endp = startp = endp + 1;
5917 } else
5918 endp++;
5920 /* Put the usual defaults back in at the end. */
5921 bcopy ((char *) include_defaults_array,
5922 (char *) &include_defaults[num_dirs],
5923 sizeof (include_defaults_array));
5927 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5928 opts->first_system_include = opts->before_system;
5930 /* Unless -fnostdinc,
5931 tack on the standard include file dirs to the specified list */
5932 if (!opts->no_standard_includes) {
5933 struct default_include *p = include_defaults;
5934 char *specd_prefix = opts->include_prefix;
5935 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5936 int default_len = 0;
5937 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5938 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5939 default_len = strlen (default_prefix) - 7;
5940 default_prefix[default_len] = 0;
5942 /* Search "translated" versions of GNU directories.
5943 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5944 if (specd_prefix != 0 && default_len != 0)
5945 for (p = include_defaults; p->fname; p++) {
5946 /* Some standard dirs are only for C++. */
5947 if (!p->cplusplus
5948 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5949 /* Does this dir start with the prefix? */
5950 if (!strncmp (p->fname, default_prefix, default_len)) {
5951 /* Yes; change prefix and add to search list. */
5952 struct file_name_list *new
5953 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5954 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5955 char *str = (char *) xmalloc (this_len + 1);
5956 strcpy (str, specd_prefix);
5957 strcat (str, p->fname + default_len);
5958 new->fname = str;
5959 new->control_macro = 0;
5960 new->c_system_include_path = !p->cxx_aware;
5961 new->got_name_map = 0;
5962 append_include_chain (pfile, new, new);
5963 if (opts->first_system_include == 0)
5964 opts->first_system_include = new;
5968 /* Search ordinary names for GNU include directories. */
5969 for (p = include_defaults; p->fname; p++) {
5970 /* Some standard dirs are only for C++. */
5971 if (!p->cplusplus
5972 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5973 struct file_name_list *new
5974 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5975 new->control_macro = 0;
5976 new->c_system_include_path = !p->cxx_aware;
5977 new->fname = update_path (p->fname, p->component);
5978 new->got_name_map = 0;
5979 append_include_chain (pfile, new, new);
5980 if (opts->first_system_include == 0)
5981 opts->first_system_include = new;
5986 /* Tack the after_include chain at the end of the include chain. */
5987 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5988 if (opts->first_system_include == 0)
5989 opts->first_system_include = opts->after_include;
5991 /* With -v, print the list of dirs to search. */
5992 if (opts->verbose) {
5993 struct file_name_list *p;
5994 fprintf (stderr, "#include \"...\" search starts here:\n");
5995 for (p = opts->include; p; p = p->next) {
5996 if (p == opts->first_bracket_include)
5997 fprintf (stderr, "#include <...> search starts here:\n");
5998 fprintf (stderr, " %s\n", p->fname);
6000 fprintf (stderr, "End of search list.\n");
6003 /* Scan the -imacros files before the main input.
6004 Much like #including them, but with no_output set
6005 so that only their macro definitions matter. */
6007 opts->no_output++; pfile->no_record_file++;
6008 for (pend = opts->pending; pend; pend = pend->next)
6010 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6012 int fd = open (pend->arg, O_RDONLY, 0666);
6013 if (fd < 0)
6015 cpp_perror_with_name (pfile, pend->arg);
6016 return 0;
6018 if (!cpp_push_buffer (pfile, NULL, 0))
6019 return 0;
6020 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6021 cpp_scan_buffer (pfile);
6024 opts->no_output--; pfile->no_record_file--;
6026 /* Copy the entire contents of the main input file into
6027 the stacked input buffer previously allocated for it. */
6028 if (fname == NULL || *fname == 0) {
6029 fname = "";
6030 f = 0;
6031 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6032 cpp_pfatal_with_name (pfile, fname);
6034 /* -MG doesn't select the form of output and must be specified with one of
6035 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6036 inhibit compilation. */
6037 if (opts->print_deps_missing_files
6038 && (opts->print_deps == 0 || !opts->no_output))
6040 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
6041 return 0;
6044 /* Either of two environment variables can specify output of deps.
6045 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6046 where OUTPUT_FILE is the file to write deps info to
6047 and DEPS_TARGET is the target to mention in the deps. */
6049 if (opts->print_deps == 0
6050 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6051 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6052 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6053 char *s;
6054 char *output_file;
6056 if (spec == 0)
6058 spec = getenv ("SUNPRO_DEPENDENCIES");
6059 opts->print_deps = 2;
6061 else
6062 opts->print_deps = 1;
6064 s = spec;
6065 /* Find the space before the DEPS_TARGET, if there is one. */
6066 /* This should use index. (mrs) */
6067 while (*s != 0 && *s != ' ') s++;
6068 if (*s != 0)
6070 opts->deps_target = s + 1;
6071 output_file = (char *) xmalloc (s - spec + 1);
6072 bcopy (spec, output_file, s - spec);
6073 output_file[s - spec] = 0;
6075 else
6077 opts->deps_target = 0;
6078 output_file = spec;
6081 opts->deps_file = output_file;
6082 opts->print_deps_append = 1;
6085 /* For -M, print the expected object file name
6086 as the target of this Make-rule. */
6087 if (opts->print_deps)
6089 pfile->deps_allocated_size = 200;
6090 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6091 pfile->deps_buffer[0] = 0;
6092 pfile->deps_size = 0;
6093 pfile->deps_column = 0;
6095 if (opts->deps_target)
6096 deps_output (pfile, opts->deps_target, ':');
6097 else if (*opts->in_fname == 0)
6098 deps_output (pfile, "-", ':');
6099 else
6101 char *p, *q, *r;
6102 int len, x;
6103 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
6104 ".cc", ".cxx", ".cpp", ".cp",
6105 ".c++", 0
6108 /* Discard all directory prefixes from filename. */
6109 if ((q = rindex (opts->in_fname, '/')) != NULL
6110 #ifdef DIR_SEPARATOR
6111 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6112 #endif
6114 ++q;
6115 else
6116 q = opts->in_fname;
6118 /* Copy remainder to mungable area. */
6119 p = (char *) alloca (strlen(q) + 8);
6120 strcpy (p, q);
6122 /* Output P, but remove known suffixes. */
6123 len = strlen (p);
6124 q = p + len;
6125 /* Point to the filename suffix. */
6126 r = rindex (p, '.');
6127 /* Compare against the known suffixes. */
6128 x = 0;
6129 while (known_suffixes[x] != 0)
6131 if (strncmp (known_suffixes[x], r, q - r) == 0)
6133 /* Make q point to the bit we're going to overwrite
6134 with an object suffix. */
6135 q = r;
6136 break;
6138 x++;
6141 /* Supply our own suffix. */
6142 #ifndef VMS
6143 strcpy (q, ".o");
6144 #else
6145 strcpy (q, ".obj");
6146 #endif
6148 deps_output (pfile, p, ':');
6149 deps_output (pfile, opts->in_fname, ' ');
6153 #if 0
6154 /* Make sure data ends with a newline. And put a null after it. */
6156 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6157 /* Backslash-newline at end is not good enough. */
6158 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6159 fp->buf[fp->length++] = '\n';
6160 missing_newline = 1;
6162 fp->buf[fp->length] = '\0';
6164 /* Unless inhibited, convert trigraphs in the input. */
6166 if (!no_trigraphs)
6167 trigraph_pcp (fp);
6168 #endif
6170 /* Scan the -include files before the main input.
6171 We push these in reverse order, so that the first one is handled first. */
6173 pfile->no_record_file++;
6174 opts->pending = nreverse_pending (opts->pending);
6175 for (pend = opts->pending; pend; pend = pend->next)
6177 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6179 int fd = open (pend->arg, O_RDONLY, 0666);
6180 if (fd < 0)
6182 cpp_perror_with_name (pfile, pend->arg);
6183 return 0;
6185 if (!cpp_push_buffer (pfile, NULL, 0))
6186 return 0;
6187 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6190 pfile->no_record_file--;
6192 /* Free the pending list. */
6193 for (pend = opts->pending; pend; )
6195 struct cpp_pending *next = pend->next;
6196 free (pend);
6197 pend = next;
6199 opts->pending = NULL;
6201 #if 0
6202 /* Scan the input, processing macros and directives. */
6204 rescan (&outbuf, 0);
6206 if (missing_newline)
6207 fp->lineno--;
6209 if (CPP_PEDANTIC (pfile) && missing_newline)
6210 pedwarn ("file does not end in newline");
6212 #endif
6213 if (finclude (pfile, f, fname, 0, NULL_PTR))
6214 output_line_command (pfile, 0, same_file);
6215 return 1;
6218 void
6219 cpp_reader_init (pfile)
6220 cpp_reader *pfile;
6222 bzero ((char *) pfile, sizeof (cpp_reader));
6223 pfile->get_token = cpp_get_token;
6225 pfile->token_buffer_size = 200;
6226 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
6227 CPP_SET_WRITTEN (pfile, 0);
6229 pfile->system_include_depth = 0;
6230 pfile->dont_repeat_files = 0;
6231 pfile->all_include_files = 0;
6232 pfile->max_include_len = 0;
6233 pfile->timebuf = NULL;
6234 pfile->only_seen_white = 1;
6235 pfile->buffer = CPP_NULL_BUFFER(pfile);
6238 static struct cpp_pending *
6239 nreverse_pending (list)
6240 struct cpp_pending *list;
6243 register struct cpp_pending *prev = 0, *next, *pend;
6244 for (pend = list; pend; pend = next)
6246 next = pend->next;
6247 pend->next = prev;
6248 prev = pend;
6250 return prev;
6253 static void
6254 push_pending (pfile, cmd, arg)
6255 cpp_reader *pfile;
6256 char *cmd;
6257 char *arg;
6259 struct cpp_pending *pend
6260 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
6261 pend->cmd = cmd;
6262 pend->arg = arg;
6263 pend->next = CPP_OPTIONS (pfile)->pending;
6264 CPP_OPTIONS (pfile)->pending = pend;
6267 /* Handle command-line options in (argc, argv).
6268 Can be called multiple times, to handle multiple sets of options.
6269 Returns if an unrecognized option is seen.
6270 Returns number of handled arguments. */
6273 cpp_handle_options (pfile, argc, argv)
6274 cpp_reader *pfile;
6275 int argc;
6276 char **argv;
6278 int i;
6279 struct cpp_options *opts = CPP_OPTIONS (pfile);
6280 for (i = 0; i < argc; i++) {
6281 if (argv[i][0] != '-') {
6282 if (opts->out_fname != NULL)
6284 cpp_fatal (pfile, "Usage: %s [switches] input output", argv[0]);
6285 return argc;
6287 else if (opts->in_fname != NULL)
6288 opts->out_fname = argv[i];
6289 else
6290 opts->in_fname = argv[i];
6291 } else {
6292 switch (argv[i][1]) {
6294 missing_filename:
6295 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
6296 return argc;
6297 missing_dirname:
6298 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
6299 return argc;
6301 case 'i':
6302 if (!strcmp (argv[i], "-include")
6303 || !strcmp (argv[i], "-imacros")) {
6304 if (i + 1 == argc)
6305 goto missing_filename;
6306 else
6307 push_pending (pfile, argv[i], argv[i+1]), i++;
6309 if (!strcmp (argv[i], "-iprefix")) {
6310 if (i + 1 == argc)
6311 goto missing_filename;
6312 else
6313 opts->include_prefix = argv[++i];
6315 if (!strcmp (argv[i], "-ifoutput")) {
6316 opts->output_conditionals = 1;
6318 if (!strcmp (argv[i], "-isystem")) {
6319 struct file_name_list *dirtmp;
6321 if (i + 1 == argc)
6322 goto missing_filename;
6324 dirtmp = (struct file_name_list *)
6325 xmalloc (sizeof (struct file_name_list));
6326 dirtmp->next = 0;
6327 dirtmp->control_macro = 0;
6328 dirtmp->c_system_include_path = 1;
6329 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6330 strcpy (dirtmp->fname, argv[++i]);
6331 dirtmp->got_name_map = 0;
6333 if (opts->before_system == 0)
6334 opts->before_system = dirtmp;
6335 else
6336 opts->last_before_system->next = dirtmp;
6337 opts->last_before_system = dirtmp; /* Tail follows the last one */
6339 /* Add directory to end of path for includes,
6340 with the default prefix at the front of its name. */
6341 if (!strcmp (argv[i], "-iwithprefix")) {
6342 struct file_name_list *dirtmp;
6343 char *prefix;
6345 if (opts->include_prefix != 0)
6346 prefix = opts->include_prefix;
6347 else {
6348 prefix = savestring (GCC_INCLUDE_DIR);
6349 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6350 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6351 prefix[strlen (prefix) - 7] = 0;
6354 dirtmp = (struct file_name_list *)
6355 xmalloc (sizeof (struct file_name_list));
6356 dirtmp->next = 0; /* New one goes on the end */
6357 dirtmp->control_macro = 0;
6358 dirtmp->c_system_include_path = 0;
6359 if (i + 1 == argc)
6360 goto missing_dirname;
6362 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6363 + strlen (prefix) + 1);
6364 strcpy (dirtmp->fname, prefix);
6365 strcat (dirtmp->fname, argv[++i]);
6366 dirtmp->got_name_map = 0;
6368 if (opts->after_include == 0)
6369 opts->after_include = dirtmp;
6370 else
6371 opts->last_after_include->next = dirtmp;
6372 opts->last_after_include = dirtmp; /* Tail follows the last one */
6374 /* Add directory to main path for includes,
6375 with the default prefix at the front of its name. */
6376 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6377 struct file_name_list *dirtmp;
6378 char *prefix;
6380 if (opts->include_prefix != 0)
6381 prefix = opts->include_prefix;
6382 else {
6383 prefix = savestring (GCC_INCLUDE_DIR);
6384 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6385 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6386 prefix[strlen (prefix) - 7] = 0;
6389 dirtmp = (struct file_name_list *)
6390 xmalloc (sizeof (struct file_name_list));
6391 dirtmp->next = 0; /* New one goes on the end */
6392 dirtmp->control_macro = 0;
6393 dirtmp->c_system_include_path = 0;
6394 if (i + 1 == argc)
6395 goto missing_dirname;
6397 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6398 + strlen (prefix) + 1);
6399 strcpy (dirtmp->fname, prefix);
6400 strcat (dirtmp->fname, argv[++i]);
6401 dirtmp->got_name_map = 0;
6403 append_include_chain (pfile, dirtmp, dirtmp);
6405 /* Add directory to end of path for includes. */
6406 if (!strcmp (argv[i], "-idirafter")) {
6407 struct file_name_list *dirtmp;
6409 dirtmp = (struct file_name_list *)
6410 xmalloc (sizeof (struct file_name_list));
6411 dirtmp->next = 0; /* New one goes on the end */
6412 dirtmp->control_macro = 0;
6413 dirtmp->c_system_include_path = 0;
6414 if (i + 1 == argc)
6415 goto missing_dirname;
6416 else
6417 dirtmp->fname = argv[++i];
6418 dirtmp->got_name_map = 0;
6420 if (opts->after_include == 0)
6421 opts->after_include = dirtmp;
6422 else
6423 opts->last_after_include->next = dirtmp;
6424 opts->last_after_include = dirtmp; /* Tail follows the last one */
6426 break;
6428 case 'o':
6429 if (opts->out_fname != NULL)
6431 cpp_fatal (pfile, "Output filename specified twice");
6432 return argc;
6434 if (i + 1 == argc)
6435 goto missing_filename;
6436 opts->out_fname = argv[++i];
6437 if (!strcmp (opts->out_fname, "-"))
6438 opts->out_fname = "";
6439 break;
6441 case 'p':
6442 if (!strcmp (argv[i], "-pedantic"))
6443 CPP_PEDANTIC (pfile) = 1;
6444 else if (!strcmp (argv[i], "-pedantic-errors")) {
6445 CPP_PEDANTIC (pfile) = 1;
6446 opts->pedantic_errors = 1;
6448 #if 0
6449 else if (!strcmp (argv[i], "-pcp")) {
6450 char *pcp_fname = argv[++i];
6451 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6452 ? fopen (pcp_fname, "w")
6453 : fdopen (dup (fileno (stdout)), "w"));
6454 if (pcp_outfile == 0)
6455 cpp_pfatal_with_name (pfile, pcp_fname);
6456 no_precomp = 1;
6458 #endif
6459 break;
6461 case 't':
6462 if (!strcmp (argv[i], "-traditional")) {
6463 opts->traditional = 1;
6464 } else if (!strcmp (argv[i], "-trigraphs")) {
6465 if (!opts->chill)
6466 opts->no_trigraphs = 0;
6468 break;
6470 case 'l':
6471 if (! strcmp (argv[i], "-lang-c"))
6472 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6473 opts->objc = 0;
6474 if (! strcmp (argv[i], "-lang-c89"))
6475 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
6476 opts->objc = 0;
6477 if (! strcmp (argv[i], "-lang-c++"))
6478 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6479 opts->objc = 0;
6480 if (! strcmp (argv[i], "-lang-objc"))
6481 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6482 opts->objc = 1;
6483 if (! strcmp (argv[i], "-lang-objc++"))
6484 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6485 opts->objc = 1;
6486 if (! strcmp (argv[i], "-lang-asm"))
6487 opts->lang_asm = 1;
6488 if (! strcmp (argv[i], "-lint"))
6489 opts->for_lint = 1;
6490 if (! strcmp (argv[i], "-lang-chill"))
6491 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6492 opts->traditional = 1, opts->no_trigraphs = 1;
6493 break;
6495 case '+':
6496 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6497 break;
6499 case 'w':
6500 opts->inhibit_warnings = 1;
6501 break;
6503 case 'W':
6504 if (!strcmp (argv[i], "-Wtrigraphs"))
6505 opts->warn_trigraphs = 1;
6506 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6507 opts->warn_trigraphs = 0;
6508 else if (!strcmp (argv[i], "-Wcomment"))
6509 opts->warn_comments = 1;
6510 else if (!strcmp (argv[i], "-Wno-comment"))
6511 opts->warn_comments = 0;
6512 else if (!strcmp (argv[i], "-Wcomments"))
6513 opts->warn_comments = 1;
6514 else if (!strcmp (argv[i], "-Wno-comments"))
6515 opts->warn_comments = 0;
6516 else if (!strcmp (argv[i], "-Wtraditional"))
6517 opts->warn_stringify = 1;
6518 else if (!strcmp (argv[i], "-Wno-traditional"))
6519 opts->warn_stringify = 0;
6520 else if (!strcmp (argv[i], "-Wundef"))
6521 opts->warn_undef = 1;
6522 else if (!strcmp (argv[i], "-Wno-undef"))
6523 opts->warn_undef = 0;
6524 else if (!strcmp (argv[i], "-Wimport"))
6525 opts->warn_import = 1;
6526 else if (!strcmp (argv[i], "-Wno-import"))
6527 opts->warn_import = 0;
6528 else if (!strcmp (argv[i], "-Werror"))
6529 opts->warnings_are_errors = 1;
6530 else if (!strcmp (argv[i], "-Wno-error"))
6531 opts->warnings_are_errors = 0;
6532 else if (!strcmp (argv[i], "-Wall"))
6534 opts->warn_trigraphs = 1;
6535 opts->warn_comments = 1;
6537 break;
6539 case 'M':
6540 /* The style of the choices here is a bit mixed.
6541 The chosen scheme is a hybrid of keeping all options in one string
6542 and specifying each option in a separate argument:
6543 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6544 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6545 -M[M][G][D file]. This is awkward to handle in specs, and is not
6546 as extensible. */
6547 /* ??? -MG must be specified in addition to one of -M or -MM.
6548 This can be relaxed in the future without breaking anything.
6549 The converse isn't true. */
6551 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6552 if (!strcmp (argv[i], "-MG"))
6554 opts->print_deps_missing_files = 1;
6555 break;
6557 if (!strcmp (argv[i], "-M"))
6558 opts->print_deps = 2;
6559 else if (!strcmp (argv[i], "-MM"))
6560 opts->print_deps = 1;
6561 else if (!strcmp (argv[i], "-MD"))
6562 opts->print_deps = 2;
6563 else if (!strcmp (argv[i], "-MMD"))
6564 opts->print_deps = 1;
6565 /* For -MD and -MMD options, write deps on file named by next arg. */
6566 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6568 if (i+1 == argc)
6569 goto missing_filename;
6570 opts->deps_file = argv[++i];
6572 else
6574 /* For -M and -MM, write deps on standard output
6575 and suppress the usual output. */
6576 opts->no_output = 1;
6578 break;
6580 case 'd':
6582 char *p = argv[i] + 2;
6583 char c;
6584 while ((c = *p++) != 0) {
6585 /* Arg to -d specifies what parts of macros to dump */
6586 switch (c) {
6587 case 'M':
6588 opts->dump_macros = dump_only;
6589 opts->no_output = 1;
6590 break;
6591 case 'N':
6592 opts->dump_macros = dump_names;
6593 break;
6594 case 'D':
6595 opts->dump_macros = dump_definitions;
6596 break;
6597 case 'I':
6598 opts->dump_includes = 1;
6599 break;
6603 break;
6605 case 'g':
6606 if (argv[i][2] == '3')
6607 opts->debug_output = 1;
6608 break;
6610 case 'v':
6611 fprintf (stderr, "GNU CPP version %s", version_string);
6612 #ifdef TARGET_VERSION
6613 TARGET_VERSION;
6614 #endif
6615 fprintf (stderr, "\n");
6616 opts->verbose = 1;
6617 break;
6619 case 'H':
6620 opts->print_include_names = 1;
6621 break;
6623 case 'D':
6624 if (argv[i][2] != 0)
6625 push_pending (pfile, "-D", argv[i] + 2);
6626 else if (i + 1 == argc)
6628 cpp_fatal (pfile, "Macro name missing after -D option");
6629 return argc;
6631 else
6632 i++, push_pending (pfile, "-D", argv[i]);
6633 break;
6635 case 'A':
6637 char *p;
6639 if (argv[i][2] != 0)
6640 p = argv[i] + 2;
6641 else if (i + 1 == argc)
6643 cpp_fatal (pfile, "Assertion missing after -A option");
6644 return argc;
6646 else
6647 p = argv[++i];
6649 if (!strcmp (p, "-")) {
6650 struct cpp_pending **ptr;
6651 /* -A- eliminates all predefined macros and assertions.
6652 Let's include also any that were specified earlier
6653 on the command line. That way we can get rid of any
6654 that were passed automatically in from GCC. */
6655 opts->inhibit_predefs = 1;
6656 for (ptr = &opts->pending; *ptr != NULL; )
6658 struct cpp_pending *pend = *ptr;
6659 if (pend->cmd && pend->cmd[0] == '-'
6660 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6662 *ptr = pend->next;
6663 free (pend);
6665 else
6666 ptr = &pend->next;
6668 } else {
6669 push_pending (pfile, "-A", p);
6672 break;
6674 case 'U': /* JF #undef something */
6675 if (argv[i][2] != 0)
6676 push_pending (pfile, "-U", argv[i] + 2);
6677 else if (i + 1 == argc)
6679 cpp_fatal (pfile, "Macro name missing after -U option", NULL);
6680 return argc;
6682 else
6683 push_pending (pfile, "-U", argv[i+1]), i++;
6684 break;
6686 case 'C':
6687 opts->put_out_comments = 1;
6688 break;
6690 case 'E': /* -E comes from cc -E; ignore it. */
6691 break;
6693 case 'P':
6694 opts->no_line_commands = 1;
6695 break;
6697 case '$': /* Don't include $ in identifiers. */
6698 opts->dollars_in_ident = 0;
6699 break;
6701 case 'I': /* Add directory to path for includes. */
6703 struct file_name_list *dirtmp;
6705 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6706 && !strcmp (argv[i] + 2, "-")) {
6707 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6708 /* Don't use any preceding -I directories for #include <...>. */
6709 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6711 else {
6712 dirtmp = (struct file_name_list *)
6713 xmalloc (sizeof (struct file_name_list));
6714 dirtmp->next = 0; /* New one goes on the end */
6715 dirtmp->control_macro = 0;
6716 dirtmp->c_system_include_path = 0;
6717 if (argv[i][2] != 0)
6718 dirtmp->fname = argv[i] + 2;
6719 else if (i + 1 == argc)
6720 goto missing_dirname;
6721 else
6722 dirtmp->fname = argv[++i];
6723 dirtmp->got_name_map = 0;
6724 append_include_chain (pfile, dirtmp, dirtmp);
6727 break;
6729 case 'n':
6730 if (!strcmp (argv[i], "-nostdinc"))
6731 /* -nostdinc causes no default include directories.
6732 You must specify all include-file directories with -I. */
6733 opts->no_standard_includes = 1;
6734 else if (!strcmp (argv[i], "-nostdinc++"))
6735 /* -nostdinc++ causes no default C++-specific include directories. */
6736 opts->no_standard_cplusplus_includes = 1;
6737 #if 0
6738 else if (!strcmp (argv[i], "-noprecomp"))
6739 no_precomp = 1;
6740 #endif
6741 break;
6743 case 'r':
6744 if (!strcmp (argv[i], "-remap"))
6745 opts->remap = 1;
6746 break;
6748 case 'u':
6749 /* Sun compiler passes undocumented switch "-undef".
6750 Let's assume it means to inhibit the predefined symbols. */
6751 opts->inhibit_predefs = 1;
6752 break;
6754 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6755 if (opts->in_fname == NULL) {
6756 opts->in_fname = "";
6757 break;
6758 } else if (opts->out_fname == NULL) {
6759 opts->out_fname = "";
6760 break;
6761 } /* else fall through into error */
6763 default:
6764 return i;
6768 return i;
6771 void
6772 cpp_finish (pfile)
6773 cpp_reader *pfile;
6775 struct cpp_options *opts = CPP_OPTIONS (pfile);
6777 if (opts->print_deps)
6779 /* Stream on which to print the dependency information. */
6780 FILE *deps_stream;
6782 /* Don't actually write the deps file if compilation has failed. */
6783 if (pfile->errors == 0)
6785 char *deps_mode = opts->print_deps_append ? "a" : "w";
6786 if (opts->deps_file == 0)
6787 deps_stream = stdout;
6788 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6789 cpp_pfatal_with_name (pfile, opts->deps_file);
6790 fputs (pfile->deps_buffer, deps_stream);
6791 putc ('\n', deps_stream);
6792 if (opts->deps_file)
6794 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6795 cpp_fatal (pfile, "I/O error on output");
6801 /* Free resources used by PFILE.
6802 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6804 void
6805 cpp_cleanup (pfile)
6806 cpp_reader *pfile;
6808 int i;
6809 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6810 cpp_pop_buffer (pfile);
6812 if (pfile->token_buffer)
6814 free (pfile->token_buffer);
6815 pfile->token_buffer = NULL;
6818 if (pfile->deps_buffer)
6820 free (pfile->deps_buffer);
6821 pfile->deps_buffer = NULL;
6822 pfile->deps_allocated_size = 0;
6825 while (pfile->if_stack)
6827 IF_STACK_FRAME *temp = pfile->if_stack;
6828 pfile->if_stack = temp->next;
6829 free (temp);
6832 while (pfile->dont_repeat_files)
6834 struct file_name_list *temp = pfile->dont_repeat_files;
6835 pfile->dont_repeat_files = temp->next;
6836 free (temp->fname);
6837 free (temp);
6840 while (pfile->all_include_files)
6842 struct file_name_list *temp = pfile->all_include_files;
6843 pfile->all_include_files = temp->next;
6844 free (temp->fname);
6845 free (temp);
6848 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6850 register struct import_file *imp = pfile->import_hash_table[i];
6851 while (imp)
6853 struct import_file *next = imp->next;
6854 free (imp->name);
6855 free (imp);
6856 imp = next;
6858 pfile->import_hash_table[i] = 0;
6861 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6863 while (pfile->assertion_hashtab[i])
6864 delete_assertion (pfile->assertion_hashtab[i]);
6867 cpp_hash_cleanup (pfile);
6870 static int
6871 do_assert (pfile, keyword, buf, limit)
6872 cpp_reader *pfile;
6873 struct directive *keyword;
6874 U_CHAR *buf, *limit;
6876 long symstart; /* remember where symbol name starts */
6877 int c;
6878 int sym_length; /* and how long it is */
6879 struct arglist *tokens = NULL;
6881 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6882 && !CPP_BUFFER (pfile)->system_header_p)
6883 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6885 cpp_skip_hspace (pfile);
6886 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6887 parse_name (pfile, GETC());
6888 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6889 "assertion");
6891 cpp_skip_hspace (pfile);
6892 if (PEEKC() != '(') {
6893 cpp_error (pfile, "missing token-sequence in `#assert'");
6894 goto error;
6898 int error_flag = 0;
6899 tokens = read_token_list (pfile, &error_flag);
6900 if (error_flag)
6901 goto error;
6902 if (tokens == 0) {
6903 cpp_error (pfile, "empty token-sequence in `#assert'");
6904 goto error;
6906 cpp_skip_hspace (pfile);
6907 c = PEEKC ();
6908 if (c != EOF && c != '\n')
6909 cpp_pedwarn (pfile, "junk at end of `#assert'");
6910 skip_rest_of_line (pfile);
6913 /* If this name isn't already an assertion name, make it one.
6914 Error if it was already in use in some other way. */
6917 ASSERTION_HASHNODE *hp;
6918 U_CHAR *symname = pfile->token_buffer + symstart;
6919 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6920 struct tokenlist_list *value
6921 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6923 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6924 if (hp == NULL) {
6925 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6926 cpp_error (pfile, "`defined' redefined as assertion");
6927 hp = assertion_install (pfile, symname, sym_length, hashcode);
6930 /* Add the spec'd token-sequence to the list of such. */
6931 value->tokens = tokens;
6932 value->next = hp->value;
6933 hp->value = value;
6935 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6936 return 0;
6937 error:
6938 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6939 skip_rest_of_line (pfile);
6940 return 1;
6943 static int
6944 do_unassert (pfile, keyword, buf, limit)
6945 cpp_reader *pfile;
6946 struct directive *keyword;
6947 U_CHAR *buf, *limit;
6949 long symstart; /* remember where symbol name starts */
6950 int sym_length; /* and how long it is */
6951 int c;
6953 struct arglist *tokens = NULL;
6954 int tokens_specified = 0;
6956 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6957 && !CPP_BUFFER (pfile)->system_header_p)
6958 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6960 cpp_skip_hspace (pfile);
6962 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6963 parse_name (pfile, GETC());
6964 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6965 "assertion");
6967 cpp_skip_hspace (pfile);
6968 if (PEEKC() == '(') {
6969 int error_flag = 0;
6971 tokens = read_token_list (pfile, &error_flag);
6972 if (error_flag)
6973 goto error;
6974 if (tokens == 0) {
6975 cpp_error (pfile, "empty token list in `#unassert'");
6976 goto error;
6979 tokens_specified = 1;
6982 cpp_skip_hspace (pfile);
6983 c = PEEKC ();
6984 if (c != EOF && c != '\n')
6985 cpp_error (pfile, "junk at end of `#unassert'");
6986 skip_rest_of_line (pfile);
6989 ASSERTION_HASHNODE *hp;
6990 U_CHAR *symname = pfile->token_buffer + symstart;
6991 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6992 struct tokenlist_list *tail, *prev;
6994 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6995 if (hp == NULL)
6996 return 1;
6998 /* If no token list was specified, then eliminate this assertion
6999 entirely. */
7000 if (! tokens_specified)
7001 delete_assertion (hp);
7002 else {
7003 /* If a list of tokens was given, then delete any matching list. */
7005 tail = hp->value;
7006 prev = 0;
7007 while (tail) {
7008 struct tokenlist_list *next = tail->next;
7009 if (compare_token_lists (tail->tokens, tokens)) {
7010 if (prev)
7011 prev->next = next;
7012 else
7013 hp->value = tail->next;
7014 free_token_list (tail->tokens);
7015 free (tail);
7016 } else {
7017 prev = tail;
7019 tail = next;
7024 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7025 return 0;
7026 error:
7027 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7028 skip_rest_of_line (pfile);
7029 return 1;
7032 /* Test whether there is an assertion named NAME
7033 and optionally whether it has an asserted token list TOKENS.
7034 NAME is not null terminated; its length is SYM_LENGTH.
7035 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7038 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
7039 cpp_reader *pfile;
7040 U_CHAR *name;
7041 int sym_length;
7042 int tokens_specified;
7043 struct arglist *tokens;
7045 ASSERTION_HASHNODE *hp;
7046 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7048 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7049 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7051 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7052 if (hp == NULL)
7053 /* It is not an assertion; just return false. */
7054 return 0;
7056 /* If no token list was specified, then value is 1. */
7057 if (! tokens_specified)
7058 return 1;
7061 struct tokenlist_list *tail;
7063 tail = hp->value;
7065 /* If a list of tokens was given,
7066 then succeed if the assertion records a matching list. */
7068 while (tail) {
7069 if (compare_token_lists (tail->tokens, tokens))
7070 return 1;
7071 tail = tail->next;
7074 /* Fail if the assertion has no matching list. */
7075 return 0;
7079 /* Compare two lists of tokens for equality including order of tokens. */
7081 static int
7082 compare_token_lists (l1, l2)
7083 struct arglist *l1, *l2;
7085 while (l1 && l2) {
7086 if (l1->length != l2->length)
7087 return 0;
7088 if (strncmp (l1->name, l2->name, l1->length))
7089 return 0;
7090 l1 = l1->next;
7091 l2 = l2->next;
7094 /* Succeed if both lists end at the same time. */
7095 return l1 == l2;
7098 struct arglist *
7099 reverse_token_list (tokens)
7100 struct arglist *tokens;
7102 register struct arglist *prev = 0, *this, *next;
7103 for (this = tokens; this; this = next)
7105 next = this->next;
7106 this->next = prev;
7107 prev = this;
7109 return prev;
7112 /* Read a space-separated list of tokens ending in a close parenthesis.
7113 Return a list of strings, in the order they were written.
7114 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7116 static struct arglist *
7117 read_token_list (pfile, error_flag)
7118 cpp_reader *pfile;
7119 int *error_flag;
7121 struct arglist *token_ptrs = 0;
7122 int depth = 1;
7123 int length;
7125 *error_flag = 0;
7126 FORWARD (1); /* Skip '(' */
7128 /* Loop over the assertion value tokens. */
7129 while (depth > 0)
7131 struct arglist *temp;
7132 long name_written = CPP_WRITTEN (pfile);
7133 int c;
7135 cpp_skip_hspace (pfile);
7137 c = GETC ();
7139 /* Find the end of the token. */
7140 if (c == '(')
7142 CPP_PUTC (pfile, c);
7143 depth++;
7145 else if (c == ')')
7147 depth--;
7148 if (depth == 0)
7149 break;
7150 CPP_PUTC (pfile, c);
7152 else if (c == '"' || c == '\'')
7154 FORWARD(-1);
7155 cpp_get_token (pfile);
7157 else if (c == '\n')
7158 break;
7159 else
7161 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7162 && c != '"' && c != '\'')
7164 CPP_PUTC (pfile, c);
7165 c = GETC();
7167 if (c != EOF) FORWARD(-1);
7170 length = CPP_WRITTEN (pfile) - name_written;
7171 temp = (struct arglist *)
7172 xmalloc (sizeof (struct arglist) + length + 1);
7173 temp->name = (U_CHAR *) (temp + 1);
7174 bcopy ((char *) (pfile->token_buffer + name_written),
7175 (char *) temp->name, length);
7176 temp->name[length] = 0;
7177 temp->next = token_ptrs;
7178 token_ptrs = temp;
7179 temp->length = length;
7181 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7183 if (c == EOF || c == '\n')
7184 { /* FIXME */
7185 cpp_error (pfile,
7186 "unterminated token sequence following `#' operator");
7187 return 0;
7191 /* We accumulated the names in reverse order.
7192 Now reverse them to get the proper order. */
7193 return reverse_token_list (token_ptrs);
7196 static void
7197 free_token_list (tokens)
7198 struct arglist *tokens;
7200 while (tokens) {
7201 struct arglist *next = tokens->next;
7202 free (tokens->name);
7203 free (tokens);
7204 tokens = next;
7208 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7209 retrying if necessary. If MAX_READ_LEN is defined, read at most
7210 that bytes at a time. Return a negative value if an error occurs,
7211 otherwise return the actual number of bytes read,
7212 which must be LEN unless end-of-file was reached. */
7214 static int
7215 safe_read (desc, ptr, len)
7216 int desc;
7217 char *ptr;
7218 int len;
7220 int left, rcount, nchars;
7222 left = len;
7223 while (left > 0) {
7224 rcount = left;
7225 #ifdef MAX_READ_LEN
7226 if (rcount > MAX_READ_LEN)
7227 rcount = MAX_READ_LEN;
7228 #endif
7229 nchars = read (desc, ptr, rcount);
7230 if (nchars < 0)
7232 #ifdef EINTR
7233 if (errno == EINTR)
7234 continue;
7235 #endif
7236 return nchars;
7238 if (nchars == 0)
7239 break;
7240 ptr += nchars;
7241 left -= nchars;
7243 return len - left;
7246 static char *
7247 xcalloc (number, size)
7248 unsigned number, size;
7250 register unsigned total = number * size;
7251 register char *ptr = (char *) xmalloc (total);
7252 bzero (ptr, total);
7253 return ptr;
7256 static char *
7257 savestring (input)
7258 char *input;
7260 unsigned size = strlen (input);
7261 char *output = xmalloc (size + 1);
7262 strcpy (output, input);
7263 return output;
7266 /* Initialize PMARK to remember the current position of PFILE. */
7268 void
7269 parse_set_mark (pmark, pfile)
7270 struct parse_marker *pmark;
7271 cpp_reader *pfile;
7273 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7274 pmark->next = pbuf->marks;
7275 pbuf->marks = pmark;
7276 pmark->buf = pbuf;
7277 pmark->position = pbuf->cur - pbuf->buf;
7280 /* Cleanup PMARK - we no longer need it. */
7282 void
7283 parse_clear_mark (pmark)
7284 struct parse_marker *pmark;
7286 struct parse_marker **pp = &pmark->buf->marks;
7287 for (; ; pp = &(*pp)->next) {
7288 if (*pp == NULL) abort ();
7289 if (*pp == pmark) break;
7291 *pp = pmark->next;
7294 /* Backup the current position of PFILE to that saved in PMARK. */
7296 void
7297 parse_goto_mark (pmark, pfile)
7298 struct parse_marker *pmark;
7299 cpp_reader *pfile;
7301 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7302 if (pbuf != pmark->buf)
7303 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
7304 pbuf->cur = pbuf->buf + pmark->position;
7307 /* Reset PMARK to point to the current position of PFILE. (Same
7308 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7310 void
7311 parse_move_mark (pmark, pfile)
7312 struct parse_marker *pmark;
7313 cpp_reader *pfile;
7315 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7316 if (pbuf != pmark->buf)
7317 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
7318 pmark->position = pbuf->cur - pbuf->buf;
7322 cpp_read_check_assertion (pfile)
7323 cpp_reader *pfile;
7325 int name_start = CPP_WRITTEN (pfile);
7326 int name_length, name_written;
7327 int result;
7328 FORWARD (1); /* Skip '#' */
7329 cpp_skip_hspace (pfile);
7330 parse_name (pfile, GETC ());
7331 name_written = CPP_WRITTEN (pfile);
7332 name_length = name_written - name_start;
7333 cpp_skip_hspace (pfile);
7334 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7336 int error_flag;
7337 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7338 result = check_assertion (pfile,
7339 pfile->token_buffer + name_start, name_length,
7340 1, token_ptrs);
7342 else
7343 result = check_assertion (pfile,
7344 pfile->token_buffer + name_start, name_length,
7345 0, NULL_PTR);
7346 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7347 return result;
7350 void
7351 cpp_print_file_and_line (pfile)
7352 cpp_reader *pfile;
7354 cpp_buffer *ip = cpp_file_buffer (pfile);
7356 if (ip != NULL)
7358 long line, col;
7359 cpp_buf_line_and_col (ip, &line, &col);
7360 cpp_file_line_for_message (pfile, ip->nominal_fname,
7361 line, pfile->show_column ? col : -1);
7365 void
7366 cpp_error (pfile, msg, arg1, arg2, arg3)
7367 cpp_reader *pfile;
7368 char *msg;
7369 char *arg1, *arg2, *arg3;
7371 cpp_print_containing_files (pfile);
7372 cpp_print_file_and_line (pfile);
7373 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7376 /* Print error message but don't count it. */
7378 void
7379 cpp_warning (pfile, msg, arg1, arg2, arg3)
7380 cpp_reader *pfile;
7381 char *msg;
7382 char *arg1, *arg2, *arg3;
7384 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7385 return;
7387 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7388 pfile->errors++;
7390 cpp_print_containing_files (pfile);
7391 cpp_print_file_and_line (pfile);
7392 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7395 /* Print an error message and maybe count it. */
7397 void
7398 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7399 cpp_reader *pfile;
7400 char *msg;
7401 char *arg1, *arg2, *arg3;
7403 if (CPP_OPTIONS (pfile)->pedantic_errors)
7404 cpp_error (pfile, msg, arg1, arg2, arg3);
7405 else
7406 cpp_warning (pfile, msg, arg1, arg2, arg3);
7409 void
7410 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7411 cpp_reader *pfile;
7412 int line, column;
7413 char *msg;
7414 char *arg1, *arg2, *arg3;
7416 cpp_buffer *ip = cpp_file_buffer (pfile);
7418 cpp_print_containing_files (pfile);
7420 if (ip != NULL)
7421 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7423 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7426 static void
7427 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7428 cpp_reader *pfile;
7429 int line, column;
7430 char *msg;
7431 char *arg1, *arg2, *arg3;
7433 cpp_buffer *ip;
7435 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7436 return;
7438 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7439 pfile->errors++;
7441 cpp_print_containing_files (pfile);
7443 ip = cpp_file_buffer (pfile);
7445 if (ip != NULL)
7446 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7448 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7451 void
7452 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7453 cpp_reader *pfile;
7454 int line, column;
7455 char *msg;
7456 char *arg1, *arg2, *arg3;
7458 if (CPP_OPTIONS (pfile)->pedantic_errors)
7459 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7460 else
7461 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7464 /* Report a warning (or an error if pedantic_errors)
7465 giving specified file name and line number, not current. */
7467 void
7468 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7469 cpp_reader *pfile;
7470 char *file;
7471 int line;
7472 char *msg;
7473 char *arg1, *arg2, *arg3;
7475 if (!CPP_OPTIONS (pfile)->pedantic_errors
7476 && CPP_OPTIONS (pfile)->inhibit_warnings)
7477 return;
7478 if (file != NULL)
7479 cpp_file_line_for_message (pfile, file, line, -1);
7480 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7481 msg, arg1, arg2, arg3);
7484 /* This defines "errno" properly for VMS, and gives us EACCES. */
7485 #include <errno.h>
7486 #ifndef errno
7487 extern int errno;
7488 #endif
7490 #ifndef VMS
7491 #ifndef HAVE_STRERROR
7492 extern int sys_nerr;
7493 extern char *sys_errlist[];
7494 #else /* HAVE_STRERROR */
7495 char *strerror ();
7496 #endif
7497 #else /* VMS */
7498 char *strerror (int,...);
7499 #endif
7501 /* my_strerror - return the descriptive text associated with an
7502 `errno' code. */
7504 char *
7505 my_strerror (errnum)
7506 int errnum;
7508 char *result;
7510 #ifndef VMS
7511 #ifndef HAVE_STRERROR
7512 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7513 #else
7514 result = strerror (errnum);
7515 #endif
7516 #else /* VMS */
7517 /* VAXCRTL's strerror() takes an optional second argument, which only
7518 matters when the first argument is EVMSERR. However, it's simplest
7519 just to pass it unconditionally. `vaxc$errno' is declared in
7520 <errno.h>, and maintained by the library in parallel with `errno'.
7521 We assume that caller's `errnum' either matches the last setting of
7522 `errno' by the library or else does not have the value `EVMSERR'. */
7524 result = strerror (errnum, vaxc$errno);
7525 #endif
7527 if (!result)
7528 result = "undocumented I/O error";
7530 return result;
7533 /* Error including a message from `errno'. */
7535 void
7536 cpp_error_from_errno (pfile, name)
7537 cpp_reader *pfile;
7538 char *name;
7540 cpp_buffer *ip = cpp_file_buffer (pfile);
7542 cpp_print_containing_files (pfile);
7544 if (ip != NULL)
7545 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7547 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7550 void
7551 cpp_perror_with_name (pfile, name)
7552 cpp_reader *pfile;
7553 char *name;
7555 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7558 /* TODO:
7559 * No pre-compiled header file support.
7561 * Possibly different enum token codes for each C/C++ token.
7563 * Should clean up remaining directives to that do_XXX functions
7564 * only take two arguments and all have command_reads_line.
7566 * Find and cleanup remaining uses of static variables,
7568 * Support for trigraphs.
7570 * Support -dM flag (dump_all_macros).
7572 * Support for_lint flag.