* except.c (expand_start_catch_block): We only need the rethrow
[official-gcc.git] / gcc / cpplib.c
blobe3feae9dce5c1969fe36a8578ae5e9adce072d9a
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-6, 1997 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding! */
25 #ifdef EMACS
26 #define NO_SHORTNAMES
27 #include "../src/config.h"
28 #ifdef open
29 #undef open
30 #undef read
31 #undef write
32 #endif /* open */
33 #endif /* EMACS */
35 /* The macro EMACS is defined when cpp is distributed as part of Emacs,
36 for the sake of machines with limited C compilers. */
37 #ifndef EMACS
38 #include "config.h"
39 #endif /* not EMACS */
41 #ifndef STANDARD_INCLUDE_DIR
42 #define STANDARD_INCLUDE_DIR "/usr/include"
43 #endif
45 #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE. */
46 #ifdef __STDC__
47 #define PTR_INT_TYPE ptrdiff_t
48 #else
49 #define PTR_INT_TYPE long
50 #endif
51 #endif /* 0 */
53 #include "cpplib.h"
54 #include "cpphash.h"
56 #ifndef STDC_VALUE
57 #define STDC_VALUE 1
58 #endif
60 /* By default, colon separates directories in a path. */
61 #ifndef PATH_SEPARATOR
62 #define PATH_SEPARATOR ':'
63 #endif
65 #include <ctype.h>
66 #include <stdio.h>
67 #include <signal.h>
68 #ifdef __STDC__
69 #include <stdlib.h>
70 #endif
72 #ifndef VMS
73 #ifndef USG
74 #include <sys/time.h> /* for __DATE__ and __TIME__ */
75 #include <sys/resource.h>
76 #else
77 #include <sys/times.h>
78 #include <time.h>
79 #include <fcntl.h>
80 #endif /* USG */
81 #endif /* not VMS */
83 #if HAVE_LIMITS_H
84 # include <limits.h>
85 #endif
87 /* This defines "errno" properly for VMS, and gives us EACCES. */
88 #include <errno.h>
90 extern char *index ();
91 extern char *rindex ();
92 extern char *update_path ();
94 #ifndef O_RDONLY
95 #define O_RDONLY 0
96 #endif
98 #undef MIN
99 #undef MAX
100 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
101 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
103 /* Find the largest host integer type and set its size and type.
104 Watch out: on some crazy hosts `long' is shorter than `int'. */
106 #ifndef HOST_WIDE_INT
107 # if HAVE_INTTYPES_H
108 # include <inttypes.h>
109 # define HOST_WIDE_INT intmax_t
110 # else
111 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
112 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
113 # define HOST_WIDE_INT int
114 # else
115 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
116 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
117 # define HOST_WIDE_INT long
118 # else
119 # define HOST_WIDE_INT long long
120 # endif
121 # endif
122 # endif
123 #endif
125 #ifndef S_ISREG
126 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
127 #endif
129 #ifndef S_ISDIR
130 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
131 #endif
133 /* Define a generic NULL if one hasn't already been defined. */
135 #ifndef NULL
136 #define NULL 0
137 #endif
139 #ifndef GENERIC_PTR
140 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
141 #define GENERIC_PTR void *
142 #else
143 #define GENERIC_PTR char *
144 #endif
145 #endif
147 #ifndef NULL_PTR
148 #define NULL_PTR ((GENERIC_PTR) 0)
149 #endif
151 #ifndef INCLUDE_LEN_FUDGE
152 #define INCLUDE_LEN_FUDGE 0
153 #endif
155 /* Symbols to predefine. */
157 #ifdef CPP_PREDEFINES
158 static char *predefs = CPP_PREDEFINES;
159 #else
160 static char *predefs = "";
161 #endif
163 /* We let tm.h override the types used here, to handle trivial differences
164 such as the choice of unsigned int or long unsigned int for size_t.
165 When machines start needing nontrivial differences in the size type,
166 it would be best to do something here to figure out automatically
167 from other information what type to use. */
169 /* The string value for __SIZE_TYPE__. */
171 #ifndef SIZE_TYPE
172 #define SIZE_TYPE "long unsigned int"
173 #endif
175 /* The string value for __PTRDIFF_TYPE__. */
177 #ifndef PTRDIFF_TYPE
178 #define PTRDIFF_TYPE "long int"
179 #endif
181 /* The string value for __WCHAR_TYPE__. */
183 #ifndef WCHAR_TYPE
184 #define WCHAR_TYPE "int"
185 #endif
186 #define CPP_WCHAR_TYPE(PFILE) \
187 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
189 /* The string value for __USER_LABEL_PREFIX__ */
191 #ifndef USER_LABEL_PREFIX
192 #define USER_LABEL_PREFIX ""
193 #endif
195 /* The string value for __REGISTER_PREFIX__ */
197 #ifndef REGISTER_PREFIX
198 #define REGISTER_PREFIX ""
199 #endif
201 /* In the definition of a #assert name, this structure forms
202 a list of the individual values asserted.
203 Each value is itself a list of "tokens".
204 These are strings that are compared by name. */
206 struct tokenlist_list {
207 struct tokenlist_list *next;
208 struct arglist *tokens;
211 struct assertion_hashnode {
212 struct assertion_hashnode *next; /* double links for easy deletion */
213 struct assertion_hashnode *prev;
214 /* also, a back pointer to this node's hash
215 chain is kept, in case the node is the head
216 of the chain and gets deleted. */
217 struct assertion_hashnode **bucket_hdr;
218 int length; /* length of token, for quick comparison */
219 U_CHAR *name; /* the actual name */
220 /* List of token-sequences. */
221 struct tokenlist_list *value;
224 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
225 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
227 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
228 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
229 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
230 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
231 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
232 (Note that it is false while we're expanding marco *arguments*.) */
233 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
235 /* Move all backslash-newline pairs out of embarrassing places.
236 Exchange all such pairs following BP
237 with any potentially-embarrassing characters that follow them.
238 Potentially-embarrassing characters are / and *
239 (because a backslash-newline inside a comment delimiter
240 would cause it not to be recognized). */
242 #define NEWLINE_FIX \
243 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
245 /* Same, but assume we've already read the potential '\\' into C. */
246 #define NEWLINE_FIX1(C) do { \
247 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
248 } while(0)
250 struct cpp_pending {
251 struct cpp_pending *next;
252 char *cmd;
253 char *arg;
256 /* Forward declarations. */
258 char *xmalloc ();
259 void cpp_fatal ();
260 void cpp_file_line_for_message PARAMS ((cpp_reader *, char *, int, int));
261 void cpp_hash_cleanup PARAMS ((cpp_reader *));
262 void cpp_message ();
263 void cpp_print_containing_files PARAMS ((cpp_reader *));
265 static void add_import ();
266 static void append_include_chain ();
267 static void make_assertion ();
268 static void path_include ();
269 static void initialize_builtins ();
270 static void initialize_char_syntax ();
271 extern void delete_macro ();
272 #if 0
273 static void trigraph_pcp ();
274 #endif
275 static int finclude ();
276 static void validate_else ();
277 static int comp_def_part ();
278 #ifdef abort
279 extern void fancy_abort ();
280 #endif
281 static int lookup_import ();
282 static int redundant_include_p ();
283 static is_system_include ();
284 static struct file_name_map *read_name_map ();
285 static char *read_filename_string ();
286 static int open_include_file ();
287 static int check_macro_name ();
288 static int compare_defs ();
289 static int compare_token_lists ();
290 static HOST_WIDE_INT eval_if_expression ();
291 static int change_newlines ();
292 extern int hashf ();
293 static int file_size_and_mode ();
294 static struct arglist *read_token_list ();
295 static void free_token_list ();
296 static int safe_read ();
297 static void push_macro_expansion PARAMS ((cpp_reader *,
298 U_CHAR *, int, HASHNODE *));
299 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
300 extern char *xrealloc ();
301 static char *xcalloc ();
302 static char *savestring ();
304 static void conditional_skip ();
305 static void skip_if_group ();
307 /* Last arg to output_line_command. */
308 enum file_change_code {same_file, enter_file, leave_file};
310 /* External declarations. */
312 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
314 extern char *getenv ();
315 extern FILE *fdopen ();
316 extern char *version_string;
317 extern struct tm *localtime ();
319 /* These functions are declared to return int instead of void since they
320 are going to be placed in a table and some old compilers have trouble with
321 pointers to functions returning void. */
323 static int do_define ();
324 static int do_line ();
325 static int do_include ();
326 static int do_undef ();
327 static int do_error ();
328 static int do_pragma ();
329 static int do_ident ();
330 static int do_if ();
331 static int do_xifdef ();
332 static int do_else ();
333 static int do_elif ();
334 static int do_endif ();
335 static int do_sccs ();
336 static int do_once ();
337 static int do_assert ();
338 static int do_unassert ();
339 static int do_warning ();
341 struct file_name_list
343 struct file_name_list *next;
344 char *fname;
345 /* If the following is nonzero, it is a macro name.
346 Don't include the file again if that macro is defined. */
347 U_CHAR *control_macro;
348 /* If the following is nonzero, it is a C-language system include
349 directory. */
350 int c_system_include_path;
351 /* Mapping of file names for this directory. */
352 struct file_name_map *name_map;
353 /* Non-zero if name_map is valid. */
354 int got_name_map;
357 /* If a buffer's dir field is SELF_DIR_DUMMY, it means the file was found
358 via the same directory as the file that #included it. */
359 #define SELF_DIR_DUMMY ((struct file_name_list *) (~0))
361 /* #include "file" looks in source file dir, then stack. */
362 /* #include <file> just looks in the stack. */
363 /* -I directories are added to the end, then the defaults are added. */
364 /* The */
365 static struct default_include {
366 char *fname; /* The name of the directory. */
367 char *component; /* The component containing the directory */
368 int cplusplus; /* Only look here if we're compiling C++. */
369 int cxx_aware; /* Includes in this directory don't need to
370 be wrapped in extern "C" when compiling
371 C++. */
372 } include_defaults_array[]
373 #ifdef INCLUDE_DEFAULTS
374 = INCLUDE_DEFAULTS;
375 #else
377 /* Pick up GNU C++ specific include files. */
378 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
379 { OLD_GPLUSPLUS_INCLUDE_DIR, 0, 1, 1 },
380 #ifdef CROSS_COMPILE
381 /* This is the dir for fixincludes. Put it just before
382 the files that we fix. */
383 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
384 /* For cross-compilation, this dir name is generated
385 automatically in Makefile.in. */
386 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
387 #ifdef TOOL_INCLUDE_DIR
388 /* This is another place that the target system's headers might be. */
389 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
390 #endif
391 #else /* not CROSS_COMPILE */
392 #ifdef LOCAL_INCLUDE_DIR
393 /* This should be /usr/local/include and should come before
394 the fixincludes-fixed header files. */
395 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
396 #endif
397 #ifdef TOOL_INCLUDE_DIR
398 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
399 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
400 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
401 #endif
402 /* This is the dir for fixincludes. Put it just before
403 the files that we fix. */
404 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
405 /* Some systems have an extra dir of include files. */
406 #ifdef SYSTEM_INCLUDE_DIR
407 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
408 #endif
409 #ifndef STANDARD_INCLUDE_COMPONENT
410 #define STANDARD_INCLUDE_COMPONENT 0
411 #endif
412 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
413 #endif /* not CROSS_COMPILE */
414 { 0, 0, 0, 0 }
416 #endif /* no INCLUDE_DEFAULTS */
418 /* `struct directive' defines one #-directive, including how to handle it. */
420 struct directive {
421 int length; /* Length of name */
422 int (*func)(); /* Function to handle directive */
423 char *name; /* Name of directive */
424 enum node_type type; /* Code which describes which directive. */
425 char command_reads_line; /* One if rest of line is read by func. */
428 #define IS_INCLUDE_DIRECTIVE_TYPE(t) (T_INCLUDE <= (t) && (t) <= T_IMPORT)
430 /* Here is the actual list of #-directives, most-often-used first.
431 The initialize_builtins function assumes #define is the very first. */
433 static struct directive directive_table[] = {
434 { 6, do_define, "define", T_DEFINE},
435 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
436 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
437 { 7, do_include, "include", T_INCLUDE, 1},
438 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
439 { 6, do_include, "import", T_IMPORT, 1},
440 { 5, do_endif, "endif", T_ENDIF, 1},
441 { 4, do_else, "else", T_ELSE, 1},
442 { 2, do_if, "if", T_IF, 1},
443 { 4, do_elif, "elif", T_ELIF, 1},
444 { 5, do_undef, "undef", T_UNDEF},
445 { 5, do_error, "error", T_ERROR},
446 { 7, do_warning, "warning", T_WARNING},
447 { 6, do_pragma, "pragma", T_PRAGMA},
448 { 4, do_line, "line", T_LINE, 1},
449 { 5, do_ident, "ident", T_IDENT, 1},
450 #ifdef SCCS_DIRECTIVE
451 { 4, do_sccs, "sccs", T_SCCS},
452 #endif
453 { 6, do_assert, "assert", T_ASSERT, 1},
454 { 8, do_unassert, "unassert", T_UNASSERT, 1},
455 { -1, 0, "", T_UNUSED},
458 /* table to tell if char can be part of a C identifier. */
459 U_CHAR is_idchar[256];
460 /* table to tell if char can be first char of a c identifier. */
461 U_CHAR is_idstart[256];
462 /* table to tell if c is horizontal space. */
463 U_CHAR is_hor_space[256];
464 /* table to tell if c is horizontal or vertical space. */
465 static U_CHAR is_space[256];
467 /* Initialize syntactic classifications of characters. */
469 static void
470 initialize_char_syntax (opts)
471 struct cpp_options *opts;
473 register int i;
476 * Set up is_idchar and is_idstart tables. These should be
477 * faster than saying (is_alpha (c) || c == '_'), etc.
478 * Set up these things before calling any routines tthat
479 * refer to them.
481 for (i = 'a'; i <= 'z'; i++) {
482 is_idchar[i - 'a' + 'A'] = 1;
483 is_idchar[i] = 1;
484 is_idstart[i - 'a' + 'A'] = 1;
485 is_idstart[i] = 1;
487 for (i = '0'; i <= '9'; i++)
488 is_idchar[i] = 1;
489 is_idchar['_'] = 1;
490 is_idstart['_'] = 1;
491 is_idchar['$'] = opts->dollars_in_ident;
492 is_idstart['$'] = opts->dollars_in_ident;
494 /* horizontal space table */
495 is_hor_space[' '] = 1;
496 is_hor_space['\t'] = 1;
497 is_hor_space['\v'] = 1;
498 is_hor_space['\f'] = 1;
499 is_hor_space['\r'] = 1;
501 is_space[' '] = 1;
502 is_space['\t'] = 1;
503 is_space['\v'] = 1;
504 is_space['\f'] = 1;
505 is_space['\n'] = 1;
506 is_space['\r'] = 1;
510 /* Place into PFILE a quoted string representing the string SRC.
511 Caller must reserve enough space in pfile->token_buffer. */
513 static void
514 quote_string (pfile, src)
515 cpp_reader *pfile;
516 char *src;
518 U_CHAR c;
520 CPP_PUTC_Q (pfile, '\"');
521 for (;;)
522 switch ((c = *src++))
524 default:
525 if (isprint (c))
526 CPP_PUTC_Q (pfile, c);
527 else
529 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
530 CPP_ADJUST_WRITTEN (pfile, 4);
532 break;
534 case '\"':
535 case '\\':
536 CPP_PUTC_Q (pfile, '\\');
537 CPP_PUTC_Q (pfile, c);
538 break;
540 case '\0':
541 CPP_PUTC_Q (pfile, '\"');
542 CPP_NUL_TERMINATE_Q (pfile);
543 return;
547 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
549 void
550 cpp_grow_buffer (pfile, n)
551 cpp_reader *pfile;
552 long n;
554 long old_written = CPP_WRITTEN (pfile);
555 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
556 pfile->token_buffer = (U_CHAR *)
557 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
558 CPP_SET_WRITTEN (pfile, old_written);
563 * process a given definition string, for initialization
564 * If STR is just an identifier, define it with value 1.
565 * If STR has anything after the identifier, then it should
566 * be identifier=definition.
569 void
570 cpp_define (pfile, str)
571 cpp_reader *pfile;
572 U_CHAR *str;
574 U_CHAR *buf, *p;
576 buf = str;
577 p = str;
578 if (!is_idstart[*p])
580 cpp_error (pfile, "malformed option `-D %s'", str);
581 return;
583 while (is_idchar[*++p])
585 if (*p == 0)
587 buf = (U_CHAR *) alloca (p - buf + 4);
588 strcpy ((char *)buf, str);
589 strcat ((char *)buf, " 1");
591 else if (*p != '=')
593 cpp_error (pfile, "malformed option `-D %s'", str);
594 return;
596 else
598 U_CHAR *q;
599 /* Copy the entire option so we can modify it. */
600 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
601 strncpy (buf, str, p - str);
602 /* Change the = to a space. */
603 buf[p - str] = ' ';
604 /* Scan for any backslash-newline and remove it. */
605 p++;
606 q = &buf[p - str];
607 while (*p)
609 if (*p == '\\' && p[1] == '\n')
610 p += 2;
611 else
612 *q++ = *p++;
614 *q = 0;
617 do_define (pfile, NULL, buf, buf + strlen (buf));
620 /* Process the string STR as if it appeared as the body of a #assert.
621 OPTION is the option name for which STR was the argument. */
623 static void
624 make_assertion (pfile, option, str)
625 cpp_reader *pfile;
626 char *option;
627 U_CHAR *str;
629 struct directive *kt;
630 U_CHAR *buf, *p, *q;
632 /* Copy the entire option so we can modify it. */
633 buf = (U_CHAR *) alloca (strlen (str) + 1);
634 strcpy ((char *) buf, str);
635 /* Scan for any backslash-newline and remove it. */
636 p = q = buf;
637 while (*p) {
638 #if 0
639 if (*p == '\\' && p[1] == '\n')
640 p += 2;
641 else
642 #endif
643 *q++ = *p++;
645 *q = 0;
647 p = buf;
648 if (!is_idstart[*p]) {
649 cpp_error (pfile, "malformed option `%s %s'", option, str);
650 return;
652 while (is_idchar[*++p])
654 while (*p == ' ' || *p == '\t') p++;
655 if (! (*p == 0 || *p == '(')) {
656 cpp_error (pfile, "malformed option `%s %s'", option, str);
657 return;
660 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
662 do_assert (pfile, NULL, NULL, NULL);
663 cpp_pop_buffer (pfile);
667 /* Append a chain of `struct file_name_list's
668 to the end of the main include chain.
669 FIRST is the beginning of the chain to append, and LAST is the end. */
671 static void
672 append_include_chain (pfile, first, last)
673 cpp_reader *pfile;
674 struct file_name_list *first, *last;
676 struct cpp_options *opts = CPP_OPTIONS (pfile);
677 struct file_name_list *dir;
679 if (!first || !last)
680 return;
682 if (opts->include == 0)
683 opts->include = first;
684 else
685 opts->last_include->next = first;
687 if (opts->first_bracket_include == 0)
688 opts->first_bracket_include = first;
690 for (dir = first; ; dir = dir->next) {
691 int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
692 if (len > pfile->max_include_len)
693 pfile->max_include_len = len;
694 if (dir == last)
695 break;
698 last->next = NULL;
699 opts->last_include = last;
702 /* Add output to `deps_buffer' for the -M switch.
703 STRING points to the text to be output.
704 SPACER is ':' for targets, ' ' for dependencies, zero for text
705 to be inserted literally. */
707 static void
708 deps_output (pfile, string, spacer)
709 cpp_reader *pfile;
710 char *string;
711 int spacer;
713 int size = strlen (string);
715 if (size == 0)
716 return;
718 #ifndef MAX_OUTPUT_COLUMNS
719 #define MAX_OUTPUT_COLUMNS 72
720 #endif
721 if (spacer
722 && pfile->deps_column > 0
723 && (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
725 deps_output (pfile, " \\\n ", 0);
726 pfile->deps_column = 0;
729 if (pfile->deps_size + size + 8 > pfile->deps_allocated_size)
731 pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
732 pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
733 pfile->deps_allocated_size);
735 if (spacer == ' ' && pfile->deps_column > 0)
736 pfile->deps_buffer[pfile->deps_size++] = ' ';
737 bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
738 pfile->deps_size += size;
739 pfile->deps_column += size;
740 if (spacer == ':')
741 pfile->deps_buffer[pfile->deps_size++] = ':';
742 pfile->deps_buffer[pfile->deps_size] = 0;
745 /* Given a colon-separated list of file names PATH,
746 add all the names to the search path for include files. */
748 static void
749 path_include (pfile, path)
750 cpp_reader *pfile;
751 char *path;
753 char *p;
755 p = path;
757 if (*p)
758 while (1) {
759 char *q = p;
760 char *name;
761 struct file_name_list *dirtmp;
763 /* Find the end of this name. */
764 while (*q != 0 && *q != PATH_SEPARATOR) q++;
765 if (p == q) {
766 /* An empty name in the path stands for the current directory. */
767 name = (char *) xmalloc (2);
768 name[0] = '.';
769 name[1] = 0;
770 } else {
771 /* Otherwise use the directory that is named. */
772 name = (char *) xmalloc (q - p + 1);
773 bcopy (p, name, q - p);
774 name[q - p] = 0;
777 dirtmp = (struct file_name_list *)
778 xmalloc (sizeof (struct file_name_list));
779 dirtmp->next = 0; /* New one goes on the end */
780 dirtmp->control_macro = 0;
781 dirtmp->c_system_include_path = 0;
782 dirtmp->fname = name;
783 dirtmp->got_name_map = 0;
784 append_include_chain (pfile, dirtmp, dirtmp);
786 /* Advance past this name. */
787 p = q;
788 if (*p == 0)
789 break;
790 /* Skip the colon. */
791 p++;
795 void
796 cpp_options_init (opts)
797 cpp_options *opts;
799 bzero ((char *) opts, sizeof *opts);
800 opts->in_fname = NULL;
801 opts->out_fname = NULL;
803 /* Initialize is_idchar to allow $. */
804 opts->dollars_in_ident = 1;
805 initialize_char_syntax (opts);
807 opts->no_line_commands = 0;
808 opts->no_trigraphs = 1;
809 opts->put_out_comments = 0;
810 opts->print_include_names = 0;
811 opts->dump_macros = dump_none;
812 opts->no_output = 0;
813 opts->cplusplus = 0;
814 opts->cplusplus_comments = 0;
816 opts->verbose = 0;
817 opts->objc = 0;
818 opts->lang_asm = 0;
819 opts->for_lint = 0;
820 opts->chill = 0;
821 opts->pedantic_errors = 0;
822 opts->inhibit_warnings = 0;
823 opts->warn_comments = 0;
824 opts->warn_import = 1;
825 opts->warnings_are_errors = 0;
828 enum cpp_token
829 null_underflow (pfile)
830 cpp_reader *pfile;
832 return CPP_EOF;
836 null_cleanup (pbuf, pfile)
837 cpp_buffer *pbuf;
838 cpp_reader *pfile;
840 return 0;
844 macro_cleanup (pbuf, pfile)
845 cpp_buffer *pbuf;
846 cpp_reader *pfile;
848 HASHNODE *macro = (HASHNODE *) pbuf->data;
849 if (macro->type == T_DISABLED)
850 macro->type = T_MACRO;
851 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
852 free (pbuf->buf);
853 return 0;
857 file_cleanup (pbuf, pfile)
858 cpp_buffer *pbuf;
859 cpp_reader *pfile;
861 if (pbuf->buf)
863 free (pbuf->buf);
864 pbuf->buf = 0;
866 return 0;
869 /* Assuming we have read '/'.
870 If this is the start of a comment (followed by '*' or '/'),
871 skip to the end of the comment, and return ' '.
872 Return EOF if we reached the end of file before the end of the comment.
873 If not the start of a comment, return '/'. */
875 static int
876 skip_comment (pfile, linep)
877 cpp_reader *pfile;
878 long *linep;
880 int c = 0;
881 while (PEEKC() == '\\' && PEEKN(1) == '\n')
883 if (linep)
884 (*linep)++;
885 FORWARD(2);
887 if (PEEKC() == '*')
889 FORWARD(1);
890 for (;;)
892 int prev_c = c;
893 c = GETC ();
894 if (c == EOF)
895 return EOF;
896 while (c == '\\' && PEEKC() == '\n')
898 if (linep)
899 (*linep)++;
900 FORWARD(1), c = GETC();
902 if (prev_c == '*' && c == '/')
903 return ' ';
904 if (c == '\n' && linep)
905 (*linep)++;
908 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
910 FORWARD(1);
911 for (;;)
913 c = GETC ();
914 if (c == EOF)
915 return ' '; /* Allow // to be terminated by EOF. */
916 while (c == '\\' && PEEKC() == '\n')
918 FORWARD(1);
919 c = GETC();
920 if (linep)
921 (*linep)++;
923 if (c == '\n')
925 /* Don't consider final '\n' to be part of comment. */
926 FORWARD(-1);
927 return ' ';
931 else
932 return '/';
935 /* Skip whitespace \-newline and comments. Does not macro-expand. */
937 void
938 cpp_skip_hspace (pfile)
939 cpp_reader *pfile;
941 while (1)
943 int c = PEEKC();
944 if (c == EOF)
945 return; /* FIXME */
946 if (is_hor_space[c])
948 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
949 cpp_pedwarn (pfile, "%s in preprocessing directive",
950 c == '\f' ? "formfeed" : "vertical tab");
951 FORWARD(1);
953 else if (c == '/')
955 FORWARD (1);
956 c = skip_comment (pfile, NULL);
957 if (c == '/')
958 FORWARD(-1);
959 if (c == EOF || c == '/')
960 return;
962 else if (c == '\\' && PEEKN(1) == '\n') {
963 FORWARD(2);
965 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
966 && is_hor_space[PEEKN(1)])
967 FORWARD(2);
968 else return;
972 /* Read the rest of the current line.
973 The line is appended to PFILE's output buffer. */
975 static void
976 copy_rest_of_line (pfile)
977 cpp_reader *pfile;
979 struct cpp_options *opts = CPP_OPTIONS (pfile);
980 for (;;)
982 int c = GETC();
983 int nextc;
984 switch (c)
986 case EOF:
987 goto end_directive;
988 case '\\':
989 if (PEEKC() == '\n')
991 FORWARD (1);
992 continue;
994 case '\'':
995 case '\"':
996 goto scan_directive_token;
997 break;
998 case '/':
999 nextc = PEEKC();
1000 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
1001 goto scan_directive_token;
1002 break;
1003 case '\f':
1004 case '\v':
1005 if (CPP_PEDANTIC (pfile))
1006 cpp_pedwarn (pfile, "%s in preprocessing directive",
1007 c == '\f' ? "formfeed" : "vertical tab");
1008 break;
1010 case '\n':
1011 FORWARD(-1);
1012 goto end_directive;
1013 scan_directive_token:
1014 FORWARD(-1);
1015 cpp_get_token (pfile);
1016 continue;
1018 CPP_PUTC (pfile, c);
1020 end_directive: ;
1021 CPP_NUL_TERMINATE (pfile);
1024 void
1025 skip_rest_of_line (pfile)
1026 cpp_reader *pfile;
1028 long old = CPP_WRITTEN (pfile);
1029 copy_rest_of_line (pfile);
1030 CPP_SET_WRITTEN (pfile, old);
1033 /* Handle a possible # directive.
1034 '#' has already been read. */
1037 handle_directive (pfile)
1038 cpp_reader *pfile;
1039 { int c;
1040 register struct directive *kt;
1041 int ident_length;
1042 long after_ident;
1043 U_CHAR *ident, *line_end;
1044 long old_written = CPP_WRITTEN (pfile);
1046 cpp_skip_hspace (pfile);
1048 c = PEEKC ();
1049 if (c >= '0' && c <= '9')
1051 /* Handle # followed by a line number. */
1052 if (CPP_PEDANTIC (pfile))
1053 cpp_pedwarn (pfile, "`#' followed by integer");
1054 do_line (pfile, NULL);
1055 goto done_a_directive;
1058 /* Now find the directive name. */
1059 CPP_PUTC (pfile, '#');
1060 parse_name (pfile, GETC());
1061 ident = pfile->token_buffer + old_written + 1;
1062 ident_length = CPP_PWRITTEN (pfile) - ident;
1063 if (ident_length == 0 && PEEKC() == '\n')
1065 /* A line of just `#' becomes blank. */
1066 goto done_a_directive;
1069 #if 0
1070 if (ident_length == 0 || !is_idstart[*ident]) {
1071 U_CHAR *p = ident;
1072 while (is_idchar[*p]) {
1073 if (*p < '0' || *p > '9')
1074 break;
1075 p++;
1077 /* Avoid error for `###' and similar cases unless -pedantic. */
1078 if (p == ident) {
1079 while (*p == '#' || is_hor_space[*p]) p++;
1080 if (*p == '\n') {
1081 if (pedantic && !lang_asm)
1082 cpp_warning (pfile, "invalid preprocessor directive");
1083 return 0;
1087 if (!lang_asm)
1088 cpp_error (pfile, "invalid preprocessor directive name");
1090 return 0;
1092 #endif
1094 * Decode the keyword and call the appropriate expansion
1095 * routine, after moving the input pointer up to the next line.
1097 for (kt = directive_table; ; kt++) {
1098 if (kt->length <= 0)
1099 goto not_a_directive;
1100 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
1101 break;
1104 if (kt->command_reads_line)
1105 after_ident = 0;
1106 else
1108 /* Nonzero means do not delete comments within the directive.
1109 #define needs this when -traditional. */
1110 int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE;
1111 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1112 CPP_OPTIONS (pfile)->put_out_comments = comments;
1113 after_ident = CPP_WRITTEN (pfile);
1114 copy_rest_of_line (pfile);
1115 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1118 /* We may want to pass through #define, #pragma, and #include.
1119 Other directives may create output, but we don't want the directive
1120 itself out, so we pop it now. For example conditionals may emit
1121 #failed ... #endfailed stuff. But note that popping the buffer
1122 means the parameters to kt->func may point after pfile->limit
1123 so these parameters are invalid as soon as something gets appended
1124 to the token_buffer. */
1126 line_end = CPP_PWRITTEN (pfile);
1127 if (! (kt->type == T_DEFINE
1128 || kt->type == T_PRAGMA
1129 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
1130 && CPP_OPTIONS (pfile)->dump_includes)))
1131 CPP_SET_WRITTEN (pfile, old_written);
1133 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
1135 if (kt->type == T_DEFINE)
1137 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
1139 /* Skip "#define". */
1140 U_CHAR *p = pfile->token_buffer + old_written + 7;
1142 SKIP_WHITE_SPACE (p);
1143 while (is_idchar[*p]) p++;
1144 pfile->limit = p;
1145 CPP_PUTC (pfile, '\n');
1147 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
1148 CPP_SET_WRITTEN (pfile, old_written);
1151 done_a_directive:
1152 return 1;
1154 not_a_directive:
1155 return 0;
1158 /* Pass a directive through to the output file.
1159 BUF points to the contents of the directive, as a contiguous string.
1160 LIMIT points to the first character past the end of the directive.
1161 KEYWORD is the keyword-table entry for the directive. */
1163 static void
1164 pass_thru_directive (buf, limit, pfile, keyword)
1165 U_CHAR *buf, *limit;
1166 cpp_reader *pfile;
1167 struct directive *keyword;
1169 register unsigned keyword_length = keyword->length;
1171 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
1172 CPP_PUTC_Q (pfile, '#');
1173 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
1174 if (limit != buf && buf[0] != ' ')
1175 CPP_PUTC_Q (pfile, ' ');
1176 CPP_PUTS_Q (pfile, buf, limit - buf);
1177 #if 0
1178 CPP_PUTS_Q (pfile, '\n');
1179 /* Count the line we have just made in the output,
1180 to get in sync properly. */
1181 pfile->lineno++;
1182 #endif
1185 /* The arglist structure is built by do_define to tell
1186 collect_definition where the argument names begin. That
1187 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1188 would contain pointers to the strings x, y, and z.
1189 Collect_definition would then build a DEFINITION node,
1190 with reflist nodes pointing to the places x, y, and z had
1191 appeared. So the arglist is just convenience data passed
1192 between these two routines. It is not kept around after
1193 the current #define has been processed and entered into the
1194 hash table. */
1196 struct arglist {
1197 struct arglist *next;
1198 U_CHAR *name;
1199 int length;
1200 int argno;
1201 char rest_args;
1204 /* Read a replacement list for a macro with parameters.
1205 Build the DEFINITION structure.
1206 Reads characters of text starting at BUF until END.
1207 ARGLIST specifies the formal parameters to look for
1208 in the text of the definition; NARGS is the number of args
1209 in that list, or -1 for a macro name that wants no argument list.
1210 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1211 and NAMELEN is its length in characters.
1213 Note that comments, backslash-newlines, and leading white space
1214 have already been deleted from the argument. */
1216 static DEFINITION *
1217 collect_expansion (pfile, buf, limit, nargs, arglist)
1218 cpp_reader *pfile;
1219 U_CHAR *buf, *limit;
1220 int nargs;
1221 struct arglist *arglist;
1223 DEFINITION *defn;
1224 register U_CHAR *p, *lastp, *exp_p;
1225 struct reflist *endpat = NULL;
1226 /* Pointer to first nonspace after last ## seen. */
1227 U_CHAR *concat = 0;
1228 /* Pointer to first nonspace after last single-# seen. */
1229 U_CHAR *stringify = 0;
1230 int maxsize;
1231 int expected_delimiter = '\0';
1233 /* Scan thru the replacement list, ignoring comments and quoted
1234 strings, picking up on the macro calls. It does a linear search
1235 thru the arg list on every potential symbol. Profiling might say
1236 that something smarter should happen. */
1238 if (limit < buf)
1239 abort ();
1241 /* Find the beginning of the trailing whitespace. */
1242 p = buf;
1243 while (p < limit && is_space[limit[-1]]) limit--;
1245 /* Allocate space for the text in the macro definition.
1246 Leading and trailing whitespace chars need 2 bytes each.
1247 Each other input char may or may not need 1 byte,
1248 so this is an upper bound. The extra 5 are for invented
1249 leading and trailing newline-marker and final null. */
1250 maxsize = (sizeof (DEFINITION)
1251 + (limit - p) + 5);
1252 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1253 while (p < limit)
1254 if (*p++ == '@')
1255 maxsize++;
1256 defn = (DEFINITION *) xcalloc (1, maxsize);
1258 defn->nargs = nargs;
1259 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1260 lastp = exp_p;
1262 p = buf;
1264 /* Add one initial space escape-marker to prevent accidental
1265 token-pasting (often removed by macroexpand). */
1266 *exp_p++ = '@';
1267 *exp_p++ = ' ';
1269 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1270 cpp_error (pfile, "`##' at start of macro definition");
1271 p += 2;
1274 /* Process the main body of the definition. */
1275 while (p < limit) {
1276 int skipped_arg = 0;
1277 register U_CHAR c = *p++;
1279 *exp_p++ = c;
1281 if (!CPP_TRADITIONAL (pfile)) {
1282 switch (c) {
1283 case '\'':
1284 case '\"':
1285 if (expected_delimiter != '\0') {
1286 if (c == expected_delimiter)
1287 expected_delimiter = '\0';
1288 } else
1289 expected_delimiter = c;
1290 break;
1292 case '\\':
1293 if (p < limit && expected_delimiter) {
1294 /* In a string, backslash goes through
1295 and makes next char ordinary. */
1296 *exp_p++ = *p++;
1298 break;
1300 case '@':
1301 /* An '@' in a string or character constant stands for itself,
1302 and does not need to be escaped. */
1303 if (!expected_delimiter)
1304 *exp_p++ = c;
1305 break;
1307 case '#':
1308 /* # is ordinary inside a string. */
1309 if (expected_delimiter)
1310 break;
1311 if (p < limit && *p == '#') {
1312 /* ##: concatenate preceding and following tokens. */
1313 /* Take out the first #, discard preceding whitespace. */
1314 exp_p--;
1315 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1316 --exp_p;
1317 /* Skip the second #. */
1318 p++;
1319 /* Discard following whitespace. */
1320 SKIP_WHITE_SPACE (p);
1321 concat = p;
1322 if (p == limit)
1323 cpp_error (pfile, "`##' at end of macro definition");
1324 } else if (nargs >= 0) {
1325 /* Single #: stringify following argument ref.
1326 Don't leave the # in the expansion. */
1327 exp_p--;
1328 SKIP_WHITE_SPACE (p);
1329 if (p == limit || ! is_idstart[*p]
1330 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1331 cpp_error (pfile,
1332 "`#' operator is not followed by a macro argument name");
1333 else
1334 stringify = p;
1336 break;
1338 } else {
1339 /* In -traditional mode, recognize arguments inside strings and
1340 and character constants, and ignore special properties of #.
1341 Arguments inside strings are considered "stringified", but no
1342 extra quote marks are supplied. */
1343 switch (c) {
1344 case '\'':
1345 case '\"':
1346 if (expected_delimiter != '\0') {
1347 if (c == expected_delimiter)
1348 expected_delimiter = '\0';
1349 } else
1350 expected_delimiter = c;
1351 break;
1353 case '\\':
1354 /* Backslash quotes delimiters and itself, but not macro args. */
1355 if (expected_delimiter != 0 && p < limit
1356 && (*p == expected_delimiter || *p == '\\')) {
1357 *exp_p++ = *p++;
1358 continue;
1360 break;
1362 case '/':
1363 if (expected_delimiter != '\0') /* No comments inside strings. */
1364 break;
1365 if (*p == '*') {
1366 /* If we find a comment that wasn't removed by handle_directive,
1367 this must be -traditional. So replace the comment with
1368 nothing at all. */
1369 exp_p--;
1370 p += 1;
1371 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1372 p++;
1373 #if 0
1374 /* Mark this as a concatenation-point, as if it had been ##. */
1375 concat = p;
1376 #endif
1378 break;
1382 /* Handle the start of a symbol. */
1383 if (is_idchar[c] && nargs > 0) {
1384 U_CHAR *id_beg = p - 1;
1385 int id_len;
1387 --exp_p;
1388 while (p != limit && is_idchar[*p]) p++;
1389 id_len = p - id_beg;
1391 if (is_idstart[c]
1392 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1393 register struct arglist *arg;
1395 for (arg = arglist; arg != NULL; arg = arg->next) {
1396 struct reflist *tpat;
1398 if (arg->name[0] == c
1399 && arg->length == id_len
1400 && strncmp (arg->name, id_beg, id_len) == 0) {
1401 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1402 if (CPP_TRADITIONAL (pfile)) {
1403 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1404 id_len, arg->name);
1405 } else {
1406 cpp_warning (pfile,
1407 "macro arg `%.*s' would be stringified with -traditional.",
1408 id_len, arg->name);
1411 /* If ANSI, don't actually substitute inside a string. */
1412 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1413 break;
1414 /* make a pat node for this arg and append it to the end of
1415 the pat list */
1416 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1417 tpat->next = NULL;
1418 tpat->raw_before = concat == id_beg;
1419 tpat->raw_after = 0;
1420 tpat->rest_args = arg->rest_args;
1421 tpat->stringify = (CPP_TRADITIONAL (pfile)
1422 ? expected_delimiter != '\0'
1423 : stringify == id_beg);
1425 if (endpat == NULL)
1426 defn->pattern = tpat;
1427 else
1428 endpat->next = tpat;
1429 endpat = tpat;
1431 tpat->argno = arg->argno;
1432 tpat->nchars = exp_p - lastp;
1434 register U_CHAR *p1 = p;
1435 SKIP_WHITE_SPACE (p1);
1436 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1437 tpat->raw_after = 1;
1439 lastp = exp_p; /* place to start copying from next time */
1440 skipped_arg = 1;
1441 break;
1446 /* If this was not a macro arg, copy it into the expansion. */
1447 if (! skipped_arg) {
1448 register U_CHAR *lim1 = p;
1449 p = id_beg;
1450 while (p != lim1)
1451 *exp_p++ = *p++;
1452 if (stringify == id_beg)
1453 cpp_error (pfile,
1454 "`#' operator should be followed by a macro argument name");
1459 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1461 /* If ANSI, put in a "@ " marker to prevent token pasting.
1462 But not if "inside a string" (which in ANSI mode
1463 happens only for -D option). */
1464 *exp_p++ = '@';
1465 *exp_p++ = ' ';
1468 *exp_p = '\0';
1470 defn->length = exp_p - defn->expansion;
1472 /* Crash now if we overrun the allocated size. */
1473 if (defn->length + 1 > maxsize)
1474 abort ();
1476 #if 0
1477 /* This isn't worth the time it takes. */
1478 /* give back excess storage */
1479 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1480 #endif
1482 return defn;
1486 * special extension string that can be added to the last macro argument to
1487 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1488 * #define wow(a, b...) process (b, a, b)
1489 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1490 * { wow (one, two); } -> { process (two, one, two); }
1491 * if this "rest_arg" is used with the concat token '##' and if it is not
1492 * supplied then the token attached to with ## will not be outputted. Ex:
1493 * #define wow (a, b...) process (b ## , a, ## b)
1494 * { wow (1, 2); } -> { process (2, 1, 2); }
1495 * { wow (one); } -> { process (one); {
1497 static char rest_extension[] = "...";
1498 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1500 /* Create a DEFINITION node from a #define directive. Arguments are
1501 as for do_define. */
1503 static MACRODEF
1504 create_definition (buf, limit, pfile, predefinition)
1505 U_CHAR *buf, *limit;
1506 cpp_reader *pfile;
1507 int predefinition;
1509 U_CHAR *bp; /* temp ptr into input buffer */
1510 U_CHAR *symname; /* remember where symbol name starts */
1511 int sym_length; /* and how long it is */
1512 int rest_args = 0;
1513 long line, col;
1514 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1515 DEFINITION *defn;
1516 int arglengths = 0; /* Accumulate lengths of arg names
1517 plus number of args. */
1518 MACRODEF mdef;
1519 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1521 bp = buf;
1523 while (is_hor_space[*bp])
1524 bp++;
1526 symname = bp; /* remember where it starts */
1528 sym_length = check_macro_name (pfile, bp, "macro");
1529 bp += sym_length;
1531 /* Lossage will occur if identifiers or control keywords are broken
1532 across lines using backslash. This is not the right place to take
1533 care of that. */
1535 if (*bp == '(') {
1536 struct arglist *arg_ptrs = NULL;
1537 int argno = 0;
1539 bp++; /* skip '(' */
1540 SKIP_WHITE_SPACE (bp);
1542 /* Loop over macro argument names. */
1543 while (*bp != ')') {
1544 struct arglist *temp;
1546 temp = (struct arglist *) alloca (sizeof (struct arglist));
1547 temp->name = bp;
1548 temp->next = arg_ptrs;
1549 temp->argno = argno++;
1550 temp->rest_args = 0;
1551 arg_ptrs = temp;
1553 if (rest_args)
1554 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1556 if (!is_idstart[*bp])
1557 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1559 /* Find the end of the arg name. */
1560 while (is_idchar[*bp]) {
1561 bp++;
1562 /* do we have a "special" rest-args extension here? */
1563 if (limit - bp > REST_EXTENSION_LENGTH
1564 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1565 rest_args = 1;
1566 temp->rest_args = 1;
1567 break;
1570 temp->length = bp - temp->name;
1571 if (rest_args == 1)
1572 bp += REST_EXTENSION_LENGTH;
1573 arglengths += temp->length + 2;
1574 SKIP_WHITE_SPACE (bp);
1575 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1576 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1577 goto nope;
1579 if (*bp == ',') {
1580 bp++;
1581 SKIP_WHITE_SPACE (bp);
1583 if (bp >= limit) {
1584 cpp_error (pfile, "unterminated parameter list in `#define'");
1585 goto nope;
1588 struct arglist *otemp;
1590 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1591 if (temp->length == otemp->length
1592 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1593 U_CHAR *name;
1595 name = (U_CHAR *) alloca (temp->length + 1);
1596 (void) strncpy (name, temp->name, temp->length);
1597 name[temp->length] = '\0';
1598 cpp_error (pfile,
1599 "duplicate argument name `%s' in `#define'", name);
1600 goto nope;
1605 ++bp; /* skip paren */
1606 SKIP_WHITE_SPACE (bp);
1607 /* now everything from bp before limit is the definition. */
1608 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1609 defn->rest_args = rest_args;
1611 /* Now set defn->args.argnames to the result of concatenating
1612 the argument names in reverse order
1613 with comma-space between them. */
1614 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1616 struct arglist *temp;
1617 int i = 0;
1618 for (temp = arg_ptrs; temp; temp = temp->next) {
1619 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1620 i += temp->length;
1621 if (temp->next != 0) {
1622 defn->args.argnames[i++] = ',';
1623 defn->args.argnames[i++] = ' ';
1626 defn->args.argnames[i] = 0;
1628 } else {
1629 /* Simple expansion or empty definition. */
1631 if (bp < limit)
1633 if (is_hor_space[*bp]) {
1634 bp++;
1635 SKIP_WHITE_SPACE (bp);
1636 } else {
1637 switch (*bp) {
1638 case '!': case '"': case '#': case '%': case '&': case '\'':
1639 case ')': case '*': case '+': case ',': case '-': case '.':
1640 case '/': case ':': case ';': case '<': case '=': case '>':
1641 case '?': case '[': case '\\': case ']': case '^': case '{':
1642 case '|': case '}': case '~':
1643 cpp_warning (pfile, "missing white space after `#define %.*s'",
1644 sym_length, symname);
1645 break;
1647 default:
1648 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1649 sym_length, symname);
1650 break;
1654 /* now everything from bp before limit is the definition. */
1655 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1656 defn->args.argnames = (U_CHAR *) "";
1659 defn->line = line;
1660 defn->file = file;
1662 /* OP is null if this is a predefinition */
1663 defn->predefined = predefinition;
1664 mdef.defn = defn;
1665 mdef.symnam = symname;
1666 mdef.symlen = sym_length;
1668 return mdef;
1670 nope:
1671 mdef.defn = 0;
1672 return mdef;
1675 /* Check a purported macro name SYMNAME, and yield its length.
1676 USAGE is the kind of name this is intended for. */
1678 static int
1679 check_macro_name (pfile, symname, usage)
1680 cpp_reader *pfile;
1681 U_CHAR *symname;
1682 char *usage;
1684 U_CHAR *p;
1685 int sym_length;
1687 for (p = symname; is_idchar[*p]; p++)
1689 sym_length = p - symname;
1690 if (sym_length == 0
1691 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1692 cpp_error (pfile, "invalid %s name", usage);
1693 else if (!is_idstart[*symname]) {
1694 U_CHAR *msg; /* what pain... */
1695 msg = (U_CHAR *) alloca (sym_length + 1);
1696 bcopy (symname, msg, sym_length);
1697 msg[sym_length] = 0;
1698 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1699 } else {
1700 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1701 cpp_error (pfile, "invalid %s name `defined'", usage);
1703 return sym_length;
1706 /* Return zero if two DEFINITIONs are isomorphic. */
1708 static int
1709 compare_defs (pfile, d1, d2)
1710 cpp_reader *pfile;
1711 DEFINITION *d1, *d2;
1713 register struct reflist *a1, *a2;
1714 register U_CHAR *p1 = d1->expansion;
1715 register U_CHAR *p2 = d2->expansion;
1716 int first = 1;
1718 if (d1->nargs != d2->nargs)
1719 return 1;
1720 if (CPP_PEDANTIC (pfile)
1721 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1722 return 1;
1723 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1724 a1 = a1->next, a2 = a2->next) {
1725 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1726 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1727 || a1->argno != a2->argno
1728 || a1->stringify != a2->stringify
1729 || a1->raw_before != a2->raw_before
1730 || a1->raw_after != a2->raw_after)
1731 return 1;
1732 first = 0;
1733 p1 += a1->nchars;
1734 p2 += a2->nchars;
1736 if (a1 != a2)
1737 return 1;
1738 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1739 p2, d2->length - (p2 - d2->expansion), 1))
1740 return 1;
1741 return 0;
1744 /* Return 1 if two parts of two macro definitions are effectively different.
1745 One of the parts starts at BEG1 and has LEN1 chars;
1746 the other has LEN2 chars at BEG2.
1747 Any sequence of whitespace matches any other sequence of whitespace.
1748 FIRST means these parts are the first of a macro definition;
1749 so ignore leading whitespace entirely.
1750 LAST means these parts are the last of a macro definition;
1751 so ignore trailing whitespace entirely. */
1753 static int
1754 comp_def_part (first, beg1, len1, beg2, len2, last)
1755 int first;
1756 U_CHAR *beg1, *beg2;
1757 int len1, len2;
1758 int last;
1760 register U_CHAR *end1 = beg1 + len1;
1761 register U_CHAR *end2 = beg2 + len2;
1762 if (first) {
1763 while (beg1 != end1 && is_space[*beg1]) beg1++;
1764 while (beg2 != end2 && is_space[*beg2]) beg2++;
1766 if (last) {
1767 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1768 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1770 while (beg1 != end1 && beg2 != end2) {
1771 if (is_space[*beg1] && is_space[*beg2]) {
1772 while (beg1 != end1 && is_space[*beg1]) beg1++;
1773 while (beg2 != end2 && is_space[*beg2]) beg2++;
1774 } else if (*beg1 == *beg2) {
1775 beg1++; beg2++;
1776 } else break;
1778 return (beg1 != end1) || (beg2 != end2);
1781 /* Process a #define command.
1782 BUF points to the contents of the #define command, as a contiguous string.
1783 LIMIT points to the first character past the end of the definition.
1784 KEYWORD is the keyword-table entry for #define,
1785 or NULL for a "predefined" macro. */
1787 static int
1788 do_define (pfile, keyword, buf, limit)
1789 cpp_reader *pfile;
1790 struct directive *keyword;
1791 U_CHAR *buf, *limit;
1793 int hashcode;
1794 MACRODEF mdef;
1795 HASHNODE *hp;
1797 #if 0
1798 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1799 if (pcp_outfile && keyword)
1800 pass_thru_directive (buf, limit, pfile, keyword);
1801 #endif
1803 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1804 if (mdef.defn == 0)
1805 goto nope;
1807 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1809 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1811 int ok = 0;
1812 /* Redefining a precompiled key is ok. */
1813 if (hp->type == T_PCSTRING)
1814 ok = 1;
1815 /* Redefining a macro is ok if the definitions are the same. */
1816 else if (hp->type == T_MACRO)
1817 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1818 /* Redefining a constant is ok with -D. */
1819 else if (hp->type == T_CONST)
1820 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1821 /* Print the warning if it's not ok. */
1822 if (!ok)
1824 U_CHAR *msg; /* what pain... */
1826 /* If we are passing through #define and #undef directives, do
1827 that for this re-definition now. */
1828 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1829 pass_thru_directive (buf, limit, pfile, keyword);
1831 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1832 *msg = '`';
1833 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1834 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1835 cpp_pedwarn (pfile, msg);
1836 if (hp->type == T_MACRO)
1837 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1838 "this is the location of the previous definition");
1840 /* Replace the old definition. */
1841 hp->type = T_MACRO;
1842 hp->value.defn = mdef.defn;
1844 else
1846 /* If we are passing through #define and #undef directives, do
1847 that for this new definition now. */
1848 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1849 pass_thru_directive (buf, limit, pfile, keyword);
1850 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1851 (char *) mdef.defn, hashcode);
1854 return 0;
1856 nope:
1858 return 1;
1861 /* This structure represents one parsed argument in a macro call.
1862 `raw' points to the argument text as written (`raw_length' is its length).
1863 `expanded' points to the argument's macro-expansion
1864 (its length is `expand_length').
1865 `stringified_length' is the length the argument would have
1866 if stringified.
1867 `use_count' is the number of times this macro arg is substituted
1868 into the macro. If the actual use count exceeds 10,
1869 the value stored is 10. */
1871 /* raw and expanded are relative to ARG_BASE */
1872 #define ARG_BASE ((pfile)->token_buffer)
1874 struct argdata {
1875 /* Strings relative to pfile->token_buffer */
1876 long raw, expanded, stringified;
1877 int raw_length, expand_length;
1878 int stringified_length;
1879 char newlines;
1880 char use_count;
1883 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1884 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1885 as the new input buffer.
1886 Return the new buffer, or NULL on failure. */
1888 cpp_buffer *
1889 cpp_push_buffer (pfile, buffer, length)
1890 cpp_reader *pfile;
1891 U_CHAR *buffer;
1892 long length;
1894 register cpp_buffer *buf = CPP_BUFFER (pfile);
1895 if (buf == pfile->buffer_stack)
1897 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1898 buf->fname);
1899 return NULL;
1901 buf--;
1902 bzero ((char *) buf, sizeof (cpp_buffer));
1903 CPP_BUFFER (pfile) = buf;
1904 buf->if_stack = pfile->if_stack;
1905 buf->cleanup = null_cleanup;
1906 buf->underflow = null_underflow;
1907 buf->buf = buf->cur = buffer;
1908 buf->alimit = buf->rlimit = buffer + length;
1910 return buf;
1913 cpp_buffer *
1914 cpp_pop_buffer (pfile)
1915 cpp_reader *pfile;
1917 cpp_buffer *buf = CPP_BUFFER (pfile);
1918 (*buf->cleanup) (buf, pfile);
1919 return ++CPP_BUFFER (pfile);
1922 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1923 Pop the buffer when done. */
1925 void
1926 cpp_scan_buffer (pfile)
1927 cpp_reader *pfile;
1929 cpp_buffer *buffer = CPP_BUFFER (pfile);
1930 for (;;)
1932 enum cpp_token token = cpp_get_token (pfile);
1933 if (token == CPP_EOF) /* Should not happen ... */
1934 break;
1935 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1937 cpp_pop_buffer (pfile);
1938 break;
1944 * Rescan a string (which may have escape marks) into pfile's buffer.
1945 * Place the result in pfile->token_buffer.
1947 * The input is copied before it is scanned, so it is safe to pass
1948 * it something from the token_buffer that will get overwritten
1949 * (because it follows CPP_WRITTEN). This is used by do_include.
1952 static void
1953 cpp_expand_to_buffer (pfile, buf, length)
1954 cpp_reader *pfile;
1955 U_CHAR *buf;
1956 int length;
1958 register cpp_buffer *ip;
1959 cpp_buffer obuf;
1960 U_CHAR *limit = buf + length;
1961 U_CHAR *buf1;
1962 #if 0
1963 int odepth = indepth;
1964 #endif
1966 if (length < 0)
1967 abort ();
1969 /* Set up the input on the input stack. */
1971 buf1 = (U_CHAR *) alloca (length + 1);
1973 register U_CHAR *p1 = buf;
1974 register U_CHAR *p2 = buf1;
1976 while (p1 != limit)
1977 *p2++ = *p1++;
1979 buf1[length] = 0;
1981 ip = cpp_push_buffer (pfile, buf1, length);
1982 if (ip == NULL)
1983 return;
1984 ip->has_escapes = 1;
1985 #if 0
1986 ip->lineno = obuf.lineno = 1;
1987 #endif
1989 /* Scan the input, create the output. */
1990 cpp_scan_buffer (pfile);
1992 #if 0
1993 if (indepth != odepth)
1994 abort ();
1995 #endif
1997 CPP_NUL_TERMINATE (pfile);
2001 static void
2002 adjust_position (buf, limit, linep, colp)
2003 U_CHAR *buf;
2004 U_CHAR *limit;
2005 long *linep;
2006 long *colp;
2008 while (buf < limit)
2010 U_CHAR ch = *buf++;
2011 if (ch == '\n')
2012 (*linep)++, (*colp) = 1;
2013 else
2014 (*colp)++;
2018 /* Move line_base forward, updating lineno and colno. */
2020 static void
2021 update_position (pbuf)
2022 register cpp_buffer *pbuf;
2024 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
2025 unsigned char *new_pos = pbuf->cur;
2026 register struct parse_marker *mark;
2027 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
2029 if (pbuf->buf + mark->position < new_pos)
2030 new_pos = pbuf->buf + mark->position;
2032 pbuf->line_base += new_pos - old_pos;
2033 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
2036 void
2037 cpp_buf_line_and_col (pbuf, linep, colp)
2038 register cpp_buffer *pbuf;
2039 long *linep, *colp;
2041 long dummy;
2042 if (colp == NULL)
2043 colp = &dummy;
2044 if (pbuf)
2046 *linep = pbuf->lineno;
2047 *colp = pbuf->colno;
2048 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
2050 else
2052 *linep = 0;
2053 *colp = 0;
2057 /* Return the cpp_buffer that corresponds to a file (not a macro). */
2059 cpp_buffer *
2060 cpp_file_buffer (pfile)
2061 cpp_reader *pfile;
2063 cpp_buffer *ip = CPP_BUFFER (pfile);
2065 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2066 if (ip->fname != NULL)
2067 return ip;
2068 return NULL;
2071 static long
2072 count_newlines (buf, limit)
2073 register U_CHAR *buf;
2074 register U_CHAR *limit;
2076 register long count = 0;
2077 while (buf < limit)
2079 U_CHAR ch = *buf++;
2080 if (ch == '\n')
2081 count++;
2083 return count;
2087 * write out a #line command, for instance, after an #include file.
2088 * If CONDITIONAL is nonzero, we can omit the #line if it would
2089 * appear to be a no-op, and we can output a few newlines instead
2090 * if we want to increase the line number by a small amount.
2091 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
2094 static void
2095 output_line_command (pfile, conditional, file_change)
2096 cpp_reader *pfile;
2097 int conditional;
2098 enum file_change_code file_change;
2100 int len;
2101 char *line_cmd_buf, *line_end;
2102 long line, col;
2103 cpp_buffer *ip = CPP_BUFFER (pfile);
2105 if (ip->fname == NULL)
2106 return;
2108 update_position (ip);
2110 if (CPP_OPTIONS (pfile)->no_line_commands
2111 || CPP_OPTIONS (pfile)->no_output)
2112 return;
2114 line = CPP_BUFFER (pfile)->lineno;
2115 col = CPP_BUFFER (pfile)->colno;
2116 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2118 if (CPP_OPTIONS (pfile)->no_line_commands)
2119 return;
2121 if (conditional) {
2122 if (line == pfile->lineno)
2123 return;
2125 /* If the inherited line number is a little too small,
2126 output some newlines instead of a #line command. */
2127 if (line > pfile->lineno && line < pfile->lineno + 8) {
2128 CPP_RESERVE (pfile, 20);
2129 while (line > pfile->lineno) {
2130 CPP_PUTC_Q (pfile, '\n');
2131 pfile->lineno++;
2133 return;
2137 #if 0
2138 /* Don't output a line number of 0 if we can help it. */
2139 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
2140 && *ip->bufp == '\n') {
2141 ip->lineno++;
2142 ip->bufp++;
2144 #endif
2146 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
2148 #ifdef OUTPUT_LINE_COMMANDS
2149 static char sharp_line[] = "#line ";
2150 #else
2151 static char sharp_line[] = "# ";
2152 #endif
2153 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
2156 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
2157 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
2159 quote_string (pfile, ip->nominal_fname);
2160 if (file_change != same_file) {
2161 CPP_PUTC_Q (pfile, ' ');
2162 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
2164 /* Tell cc1 if following text comes from a system header file. */
2165 if (ip->system_header_p) {
2166 CPP_PUTC_Q (pfile, ' ');
2167 CPP_PUTC_Q (pfile, '3');
2169 #ifndef NO_IMPLICIT_EXTERN_C
2170 /* Tell cc1plus if following text should be treated as C. */
2171 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
2172 CPP_PUTC_Q (pfile, ' ');
2173 CPP_PUTC_Q (pfile, '4');
2175 #endif
2176 CPP_PUTC_Q (pfile, '\n');
2177 pfile->lineno = line;
2181 * Parse a macro argument and append the info on PFILE's token_buffer.
2182 * REST_ARGS means to absorb the rest of the args.
2183 * Return nonzero to indicate a syntax error.
2186 static enum cpp_token
2187 macarg (pfile, rest_args)
2188 cpp_reader *pfile;
2189 int rest_args;
2191 int paren = 0;
2192 enum cpp_token token;
2193 long arg_start = CPP_WRITTEN (pfile);
2194 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2195 CPP_OPTIONS (pfile)->put_out_comments = 0;
2197 /* Try to parse as much of the argument as exists at this
2198 input stack level. */
2199 pfile->no_macro_expand++;
2200 for (;;)
2202 token = cpp_get_token (pfile);
2203 switch (token)
2205 case CPP_EOF:
2206 goto done;
2207 case CPP_POP:
2208 /* If we've hit end of file, it's an error (reported by caller).
2209 Ditto if it's the end of cpp_expand_to_buffer text.
2210 If we've hit end of macro, just continue. */
2211 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2212 goto done;
2213 break;
2214 case CPP_LPAREN:
2215 paren++;
2216 break;
2217 case CPP_RPAREN:
2218 if (--paren < 0)
2219 goto found;
2220 break;
2221 case CPP_COMMA:
2222 /* if we've returned to lowest level and
2223 we aren't absorbing all args */
2224 if (paren == 0 && rest_args == 0)
2225 goto found;
2226 break;
2227 found:
2228 /* Remove ',' or ')' from argument buffer. */
2229 CPP_ADJUST_WRITTEN (pfile, -1);
2230 goto done;
2231 default: ;
2235 done:
2236 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2237 pfile->no_macro_expand--;
2239 return token;
2242 /* Turn newlines to spaces in the string of length LENGTH at START,
2243 except inside of string constants.
2244 The string is copied into itself with its beginning staying fixed. */
2246 static int
2247 change_newlines (start, length)
2248 U_CHAR *start;
2249 int length;
2251 register U_CHAR *ibp;
2252 register U_CHAR *obp;
2253 register U_CHAR *limit;
2254 register int c;
2256 ibp = start;
2257 limit = start + length;
2258 obp = start;
2260 while (ibp < limit) {
2261 *obp++ = c = *ibp++;
2262 switch (c) {
2264 case '\'':
2265 case '\"':
2266 /* Notice and skip strings, so that we don't delete newlines in them. */
2268 int quotec = c;
2269 while (ibp < limit) {
2270 *obp++ = c = *ibp++;
2271 if (c == quotec)
2272 break;
2273 if (c == '\n' && quotec == '\'')
2274 break;
2277 break;
2281 return obp - start;
2285 static struct tm *
2286 timestamp (pfile)
2287 cpp_reader *pfile;
2289 if (!pfile->timebuf) {
2290 time_t t = time ((time_t *) 0);
2291 pfile->timebuf = localtime (&t);
2293 return pfile->timebuf;
2296 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2297 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2301 * expand things like __FILE__. Place the expansion into the output
2302 * buffer *without* rescanning.
2305 static void
2306 special_symbol (hp, pfile)
2307 HASHNODE *hp;
2308 cpp_reader *pfile;
2310 char *buf;
2311 int i, len;
2312 int true_indepth;
2313 cpp_buffer *ip = NULL;
2314 struct tm *timebuf;
2316 int paren = 0; /* For special `defined' keyword */
2318 #if 0
2319 if (pcp_outfile && pcp_inside_if
2320 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2321 cpp_error (pfile,
2322 "Predefined macro `%s' used inside `#if' during precompilation",
2323 hp->name);
2324 #endif
2326 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2328 if (ip == CPP_NULL_BUFFER (pfile))
2330 cpp_error (pfile, "cccp error: not in any file?!");
2331 return; /* the show must go on */
2333 if (ip->fname != NULL)
2334 break;
2337 switch (hp->type)
2339 case T_FILE:
2340 case T_BASE_FILE:
2342 char *string;
2343 if (hp->type == T_BASE_FILE)
2345 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2346 ip = CPP_PREV_BUFFER (ip);
2348 string = ip->nominal_fname;
2350 if (!string)
2351 string = "";
2352 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2353 quote_string (pfile, string);
2354 return;
2357 case T_INCLUDE_LEVEL:
2358 true_indepth = 0;
2359 ip = CPP_BUFFER (pfile);
2360 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2361 if (ip->fname != NULL)
2362 true_indepth++;
2364 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2365 sprintf (buf, "%d", true_indepth - 1);
2366 break;
2368 case T_VERSION:
2369 buf = (char *) alloca (3 + strlen (version_string));
2370 sprintf (buf, "\"%s\"", version_string);
2371 break;
2373 #ifndef NO_BUILTIN_SIZE_TYPE
2374 case T_SIZE_TYPE:
2375 buf = SIZE_TYPE;
2376 break;
2377 #endif
2379 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2380 case T_PTRDIFF_TYPE:
2381 buf = PTRDIFF_TYPE;
2382 break;
2383 #endif
2385 case T_WCHAR_TYPE:
2386 buf = CPP_WCHAR_TYPE (pfile);
2387 break;
2389 case T_USER_LABEL_PREFIX_TYPE:
2390 buf = USER_LABEL_PREFIX;
2391 break;
2393 case T_REGISTER_PREFIX_TYPE:
2394 buf = REGISTER_PREFIX;
2395 break;
2397 case T_CONST:
2398 buf = (char *) alloca (4 * sizeof (int));
2399 sprintf (buf, "%d", hp->value.ival);
2400 #ifdef STDC_0_IN_SYSTEM_HEADERS
2401 if (ip->system_header_p
2402 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2403 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2404 strcpy (buf, "0");
2405 #endif
2406 #if 0
2407 if (pcp_inside_if && pcp_outfile)
2408 /* Output a precondition for this macro use */
2409 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2410 #endif
2411 break;
2413 case T_SPECLINE:
2415 long line = ip->lineno;
2416 long col = ip->colno;
2417 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2419 buf = (char *) alloca (10);
2420 sprintf (buf, "%ld", line);
2422 break;
2424 case T_DATE:
2425 case T_TIME:
2426 buf = (char *) alloca (20);
2427 timebuf = timestamp (pfile);
2428 if (hp->type == T_DATE)
2429 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2430 timebuf->tm_mday, timebuf->tm_year + 1900);
2431 else
2432 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2433 timebuf->tm_sec);
2434 break;
2436 case T_SPEC_DEFINED:
2437 buf = " 0 "; /* Assume symbol is not defined */
2438 ip = CPP_BUFFER (pfile);
2439 SKIP_WHITE_SPACE (ip->cur);
2440 if (*ip->cur == '(')
2442 paren++;
2443 ip->cur++; /* Skip over the paren */
2444 SKIP_WHITE_SPACE (ip->cur);
2447 if (!is_idstart[*ip->cur])
2448 goto oops;
2449 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2450 goto oops;
2451 if (hp = cpp_lookup (pfile, ip->cur, -1, -1))
2453 #if 0
2454 if (pcp_outfile && pcp_inside_if
2455 && (hp->type == T_CONST
2456 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2457 /* Output a precondition for this macro use. */
2458 fprintf (pcp_outfile, "#define %s\n", hp->name);
2459 #endif
2460 buf = " 1 ";
2462 #if 0
2463 else
2464 if (pcp_outfile && pcp_inside_if)
2466 /* Output a precondition for this macro use */
2467 U_CHAR *cp = ip->bufp;
2468 fprintf (pcp_outfile, "#undef ");
2469 while (is_idchar[*cp]) /* Ick! */
2470 fputc (*cp++, pcp_outfile);
2471 putc ('\n', pcp_outfile);
2473 #endif
2474 while (is_idchar[*ip->cur])
2475 ++ip->cur;
2476 SKIP_WHITE_SPACE (ip->cur);
2477 if (paren)
2479 if (*ip->cur != ')')
2480 goto oops;
2481 ++ip->cur;
2483 break;
2485 oops:
2487 cpp_error (pfile, "`defined' without an identifier");
2488 break;
2490 default:
2491 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2492 abort ();
2494 len = strlen (buf);
2495 CPP_RESERVE (pfile, len + 1);
2496 CPP_PUTS_Q (pfile, buf, len);
2497 CPP_NUL_TERMINATE_Q (pfile);
2499 return;
2502 /* Write out a #define command for the special named MACRO_NAME
2503 to PFILE's token_buffer. */
2505 static void
2506 dump_special_to_buffer (pfile, macro_name)
2507 cpp_reader *pfile;
2508 char *macro_name;
2510 static char define_directive[] = "#define ";
2511 int macro_name_length = strlen (macro_name);
2512 output_line_command (pfile, 0, same_file);
2513 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2514 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2515 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2516 CPP_PUTC_Q (pfile, ' ');
2517 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2518 CPP_PUTC (pfile, '\n');
2521 /* Initialize the built-in macros. */
2523 static void
2524 initialize_builtins (pfile)
2525 cpp_reader *pfile;
2527 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2528 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2529 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2530 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2531 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2532 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2533 #ifndef NO_BUILTIN_SIZE_TYPE
2534 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2535 #endif
2536 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2537 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2538 #endif
2539 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2540 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2541 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2542 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2543 if (!CPP_TRADITIONAL (pfile))
2544 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2545 if (CPP_OPTIONS (pfile)->objc)
2546 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2547 /* This is supplied using a -D by the compiler driver
2548 so that it is present only when truly compiling with GNU C. */
2549 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2551 if (CPP_OPTIONS (pfile)->debug_output)
2553 dump_special_to_buffer (pfile, "__BASE_FILE__");
2554 dump_special_to_buffer (pfile, "__VERSION__");
2555 #ifndef NO_BUILTIN_SIZE_TYPE
2556 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2557 #endif
2558 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2559 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2560 #endif
2561 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2562 dump_special_to_buffer (pfile, "__DATE__");
2563 dump_special_to_buffer (pfile, "__TIME__");
2564 if (!CPP_TRADITIONAL (pfile))
2565 dump_special_to_buffer (pfile, "__STDC__");
2566 if (CPP_OPTIONS (pfile)->objc)
2567 dump_special_to_buffer (pfile, "__OBJC__");
2571 /* Return 1 iff a token ending in C1 followed directly by a token C2
2572 could cause mis-tokenization. */
2574 static int
2575 unsafe_chars (c1, c2)
2576 int c1, c2;
2578 switch (c1)
2580 case '+': case '-':
2581 if (c2 == c1 || c2 == '=')
2582 return 1;
2583 goto letter;
2584 case '.':
2585 case '0': case '1': case '2': case '3': case '4':
2586 case '5': case '6': case '7': case '8': case '9':
2587 case 'e': case 'E': case 'p': case 'P':
2588 if (c2 == '-' || c2 == '+')
2589 return 1; /* could extend a pre-processing number */
2590 goto letter;
2591 case 'L':
2592 if (c2 == '\'' || c2 == '\"')
2593 return 1; /* Could turn into L"xxx" or L'xxx'. */
2594 goto letter;
2595 letter:
2596 case '_':
2597 case 'a': case 'b': case 'c': case 'd': case 'f':
2598 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2599 case 'm': case 'n': case 'o': case 'q': case 'r':
2600 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2601 case 'y': case 'z':
2602 case 'A': case 'B': case 'C': case 'D': case 'F':
2603 case 'G': case 'H': case 'I': case 'J': case 'K':
2604 case 'M': case 'N': case 'O': case 'Q': case 'R':
2605 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2606 case 'Y': case 'Z':
2607 /* We're in the middle of either a name or a pre-processing number. */
2608 return (is_idchar[c2] || c2 == '.');
2609 case '<': case '>': case '!': case '%': case '#': case ':':
2610 case '^': case '&': case '|': case '*': case '/': case '=':
2611 return (c2 == c1 || c2 == '=');
2613 return 0;
2616 /* Expand a macro call.
2617 HP points to the symbol that is the macro being called.
2618 Put the result of expansion onto the input stack
2619 so that subsequent input by our caller will use it.
2621 If macro wants arguments, caller has already verified that
2622 an argument list follows; arguments come from the input stack. */
2624 static void
2625 macroexpand (pfile, hp)
2626 cpp_reader *pfile;
2627 HASHNODE *hp;
2629 int nargs;
2630 DEFINITION *defn = hp->value.defn;
2631 register U_CHAR *xbuf;
2632 long start_line, start_column;
2633 int xbuf_len;
2634 struct argdata *args;
2635 long old_written = CPP_WRITTEN (pfile);
2636 #if 0
2637 int start_line = instack[indepth].lineno;
2638 #endif
2639 int rest_args, rest_zero;
2640 register int i;
2642 #if 0
2643 CHECK_DEPTH (return;);
2644 #endif
2646 #if 0
2647 /* This macro is being used inside a #if, which means it must be */
2648 /* recorded as a precondition. */
2649 if (pcp_inside_if && pcp_outfile && defn->predefined)
2650 dump_single_macro (hp, pcp_outfile);
2651 #endif
2653 pfile->output_escapes++;
2654 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2656 nargs = defn->nargs;
2658 if (nargs >= 0)
2660 enum cpp_token token;
2662 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2664 for (i = 0; i < nargs; i++)
2666 args[i].raw = args[i].expanded = 0;
2667 args[i].raw_length = 0;
2668 args[i].expand_length = args[i].stringified_length = -1;
2669 args[i].use_count = 0;
2672 /* Parse all the macro args that are supplied. I counts them.
2673 The first NARGS args are stored in ARGS.
2674 The rest are discarded. If rest_args is set then we assume
2675 macarg absorbed the rest of the args. */
2676 i = 0;
2677 rest_args = 0;
2678 rest_args = 0;
2679 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2682 if (rest_args)
2683 continue;
2684 if (i < nargs || (nargs == 0 && i == 0))
2686 /* if we are working on last arg which absorbs rest of args... */
2687 if (i == nargs - 1 && defn->rest_args)
2688 rest_args = 1;
2689 args[i].raw = CPP_WRITTEN (pfile);
2690 token = macarg (pfile, rest_args);
2691 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2692 args[i].newlines = 0; /* FIXME */
2694 else
2695 token = macarg (pfile, 0);
2696 if (token == CPP_EOF || token == CPP_POP)
2698 cpp_error_with_line (pfile, start_line, start_column,
2699 "unterminated macro call");
2700 return;
2702 i++;
2703 } while (token == CPP_COMMA);
2705 /* If we got one arg but it was just whitespace, call that 0 args. */
2706 if (i == 1)
2708 register U_CHAR *bp = ARG_BASE + args[0].raw;
2709 register U_CHAR *lim = bp + args[0].raw_length;
2710 /* cpp.texi says for foo ( ) we provide one argument.
2711 However, if foo wants just 0 arguments, treat this as 0. */
2712 if (nargs == 0)
2713 while (bp != lim && is_space[*bp]) bp++;
2714 if (bp == lim)
2715 i = 0;
2718 /* Don't output an error message if we have already output one for
2719 a parse error above. */
2720 rest_zero = 0;
2721 if (nargs == 0 && i > 0)
2723 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2725 else if (i < nargs)
2727 /* traditional C allows foo() if foo wants one argument. */
2728 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2730 /* the rest args token is allowed to absorb 0 tokens */
2731 else if (i == nargs - 1 && defn->rest_args)
2732 rest_zero = 1;
2733 else if (i == 0)
2734 cpp_error (pfile, "macro `%s' used without args", hp->name);
2735 else if (i == 1)
2736 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2737 else
2738 cpp_error (pfile, "macro `%s' used with only %d args",
2739 hp->name, i);
2741 else if (i > nargs)
2743 cpp_error (pfile,
2744 "macro `%s' used with too many (%d) args", hp->name, i);
2748 /* If macro wants zero args, we parsed the arglist for checking only.
2749 Read directly from the macro definition. */
2750 if (nargs <= 0)
2752 xbuf = defn->expansion;
2753 xbuf_len = defn->length;
2755 else
2757 register U_CHAR *exp = defn->expansion;
2758 register int offset; /* offset in expansion,
2759 copied a piece at a time */
2760 register int totlen; /* total amount of exp buffer filled so far */
2762 register struct reflist *ap, *last_ap;
2764 /* Macro really takes args. Compute the expansion of this call. */
2766 /* Compute length in characters of the macro's expansion.
2767 Also count number of times each arg is used. */
2768 xbuf_len = defn->length;
2769 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2771 if (ap->stringify)
2773 register struct argdata *arg = &args[ap->argno];
2774 /* Stringify it it hasn't already been */
2775 if (arg->stringified_length < 0)
2777 int arglen = arg->raw_length;
2778 int escaped = 0;
2779 int in_string = 0;
2780 int c;
2781 /* Initially need_space is -1. Otherwise, 1 means the
2782 previous character was a space, but we suppressed it;
2783 0 means the previous character was a non-space. */
2784 int need_space = -1;
2785 i = 0;
2786 arg->stringified = CPP_WRITTEN (pfile);
2787 if (!CPP_TRADITIONAL (pfile))
2788 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2789 for (; i < arglen; i++)
2791 c = (ARG_BASE + arg->raw)[i];
2793 if (! in_string)
2795 /* Internal sequences of whitespace are replaced by
2796 one space except within an string or char token.*/
2797 if (is_space[c])
2799 if (CPP_WRITTEN (pfile) > arg->stringified
2800 && (CPP_PWRITTEN (pfile))[-1] == '@')
2802 /* "@ " escape markers are removed */
2803 CPP_ADJUST_WRITTEN (pfile, -1);
2804 continue;
2806 if (need_space == 0)
2807 need_space = 1;
2808 continue;
2810 else if (need_space > 0)
2811 CPP_PUTC (pfile, ' ');
2812 need_space = 0;
2815 if (escaped)
2816 escaped = 0;
2817 else
2819 if (c == '\\')
2820 escaped = 1;
2821 if (in_string)
2823 if (c == in_string)
2824 in_string = 0;
2826 else if (c == '\"' || c == '\'')
2827 in_string = c;
2830 /* Escape these chars */
2831 if (c == '\"' || (in_string && c == '\\'))
2832 CPP_PUTC (pfile, '\\');
2833 if (isprint (c))
2834 CPP_PUTC (pfile, c);
2835 else
2837 CPP_RESERVE (pfile, 4);
2838 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2839 (unsigned int) c);
2840 CPP_ADJUST_WRITTEN (pfile, 4);
2843 if (!CPP_TRADITIONAL (pfile))
2844 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2845 arg->stringified_length
2846 = CPP_WRITTEN (pfile) - arg->stringified;
2848 xbuf_len += args[ap->argno].stringified_length;
2850 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2851 /* Add 4 for two newline-space markers to prevent
2852 token concatenation. */
2853 xbuf_len += args[ap->argno].raw_length + 4;
2854 else
2856 /* We have an ordinary (expanded) occurrence of the arg.
2857 So compute its expansion, if we have not already. */
2858 if (args[ap->argno].expand_length < 0)
2860 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2861 cpp_expand_to_buffer (pfile,
2862 ARG_BASE + args[ap->argno].raw,
2863 args[ap->argno].raw_length);
2865 args[ap->argno].expand_length
2866 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2869 /* Add 4 for two newline-space markers to prevent
2870 token concatenation. */
2871 xbuf_len += args[ap->argno].expand_length + 4;
2873 if (args[ap->argno].use_count < 10)
2874 args[ap->argno].use_count++;
2877 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2879 /* Generate in XBUF the complete expansion
2880 with arguments substituted in.
2881 TOTLEN is the total size generated so far.
2882 OFFSET is the index in the definition
2883 of where we are copying from. */
2884 offset = totlen = 0;
2885 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2886 last_ap = ap, ap = ap->next)
2888 register struct argdata *arg = &args[ap->argno];
2889 int count_before = totlen;
2891 /* Add chars to XBUF. */
2892 for (i = 0; i < ap->nchars; i++, offset++)
2893 xbuf[totlen++] = exp[offset];
2895 /* If followed by an empty rest arg with concatenation,
2896 delete the last run of nonwhite chars. */
2897 if (rest_zero && totlen > count_before
2898 && ((ap->rest_args && ap->raw_before)
2899 || (last_ap != NULL && last_ap->rest_args
2900 && last_ap->raw_after)))
2902 /* Delete final whitespace. */
2903 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2904 totlen--;
2906 /* Delete the nonwhites before them. */
2907 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2908 totlen--;
2911 if (ap->stringify != 0)
2913 bcopy (ARG_BASE + arg->stringified,
2914 xbuf + totlen, arg->stringified_length);
2915 totlen += arg->stringified_length;
2917 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2919 U_CHAR *p1 = ARG_BASE + arg->raw;
2920 U_CHAR *l1 = p1 + arg->raw_length;
2921 if (ap->raw_before)
2923 while (p1 != l1 && is_space[*p1]) p1++;
2924 while (p1 != l1 && is_idchar[*p1])
2925 xbuf[totlen++] = *p1++;
2926 /* Delete any no-reexpansion marker that follows
2927 an identifier at the beginning of the argument
2928 if the argument is concatenated with what precedes it. */
2929 if (p1[0] == '@' && p1[1] == '-')
2930 p1 += 2;
2932 if (ap->raw_after)
2934 /* Arg is concatenated after: delete trailing whitespace,
2935 whitespace markers, and no-reexpansion markers. */
2936 while (p1 != l1)
2938 if (is_space[l1[-1]]) l1--;
2939 else if (l1[-1] == '-')
2941 U_CHAR *p2 = l1 - 1;
2942 /* If a `-' is preceded by an odd number of newlines then it
2943 and the last newline are a no-reexpansion marker. */
2944 while (p2 != p1 && p2[-1] == '\n') p2--;
2945 if ((l1 - 1 - p2) & 1) {
2946 l1 -= 2;
2948 else break;
2950 else break;
2954 bcopy (p1, xbuf + totlen, l1 - p1);
2955 totlen += l1 - p1;
2957 else
2959 U_CHAR *expanded = ARG_BASE + arg->expanded;
2960 if (!ap->raw_before && totlen > 0 && arg->expand_length
2961 && !CPP_TRADITIONAL(pfile)
2962 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2964 xbuf[totlen++] = '@';
2965 xbuf[totlen++] = ' ';
2968 bcopy (expanded, xbuf + totlen, arg->expand_length);
2969 totlen += arg->expand_length;
2971 if (!ap->raw_after && totlen > 0 && offset < defn->length
2972 && !CPP_TRADITIONAL(pfile)
2973 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2975 xbuf[totlen++] = '@';
2976 xbuf[totlen++] = ' ';
2979 /* If a macro argument with newlines is used multiple times,
2980 then only expand the newlines once. This avoids creating
2981 output lines which don't correspond to any input line,
2982 which confuses gdb and gcov. */
2983 if (arg->use_count > 1 && arg->newlines > 0)
2985 /* Don't bother doing change_newlines for subsequent
2986 uses of arg. */
2987 arg->use_count = 1;
2988 arg->expand_length
2989 = change_newlines (expanded, arg->expand_length);
2993 if (totlen > xbuf_len)
2994 abort ();
2997 /* if there is anything left of the definition
2998 after handling the arg list, copy that in too. */
3000 for (i = offset; i < defn->length; i++)
3002 /* if we've reached the end of the macro */
3003 if (exp[i] == ')')
3004 rest_zero = 0;
3005 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
3006 && last_ap->raw_after))
3007 xbuf[totlen++] = exp[i];
3010 xbuf[totlen] = 0;
3011 xbuf_len = totlen;
3015 pfile->output_escapes--;
3017 /* Now put the expansion on the input stack
3018 so our caller will commence reading from it. */
3019 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
3020 CPP_BUFFER (pfile)->has_escapes = 1;
3022 /* Pop the space we've used in the token_buffer for argument expansion. */
3023 CPP_SET_WRITTEN (pfile, old_written);
3025 /* Recursive macro use sometimes works traditionally.
3026 #define foo(x,y) bar (x (y,0), y)
3027 foo (foo, baz) */
3029 if (!CPP_TRADITIONAL (pfile))
3030 hp->type = T_DISABLED;
3033 static void
3034 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
3035 cpp_reader *pfile;
3036 register U_CHAR *xbuf;
3037 int xbuf_len;
3038 HASHNODE *hp;
3040 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
3041 if (mbuf == NULL)
3042 return;
3043 mbuf->cleanup = macro_cleanup;
3044 mbuf->data = hp;
3046 /* The first chars of the expansion should be a "@ " added by
3047 collect_expansion. This is to prevent accidental token-pasting
3048 between the text preceding the macro invocation, and the macro
3049 expansion text.
3051 We would like to avoid adding unneeded spaces (for the sake of
3052 tools that use cpp, such as imake). In some common cases we can
3053 tell that it is safe to omit the space.
3055 The character before the macro invocation cannot have been an
3056 idchar (or else it would have been pasted with the idchars of
3057 the macro name). Therefore, if the first non-space character
3058 of the expansion is an idchar, we do not need the extra space
3059 to prevent token pasting.
3061 Also, we don't need the extra space if the first char is '(',
3062 or some other (less common) characters. */
3064 if (xbuf[0] == '@' && xbuf[1] == ' '
3065 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
3066 || xbuf[2] == '\"'))
3067 mbuf->cur += 2;
3070 /* Like cpp_get_token, except that it does not read past end-of-line.
3071 Also, horizontal space is skipped, and macros are popped. */
3073 static enum cpp_token
3074 get_directive_token (pfile)
3075 cpp_reader *pfile;
3077 for (;;)
3079 long old_written = CPP_WRITTEN (pfile);
3080 enum cpp_token token;
3081 cpp_skip_hspace (pfile);
3082 if (PEEKC () == '\n')
3083 return CPP_VSPACE;
3084 token = cpp_get_token (pfile);
3085 switch (token)
3087 case CPP_POP:
3088 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
3089 return token;
3090 /* ... else fall though ... */
3091 case CPP_HSPACE: case CPP_COMMENT:
3092 CPP_SET_WRITTEN (pfile, old_written);
3093 break;
3094 default:
3095 return token;
3100 /* Handle #include and #import.
3101 This function expects to see "fname" or <fname> on the input.
3103 The input is normally in part of the output_buffer following
3104 CPP_WRITTEN, and will get overwritten by output_line_command.
3105 I.e. in input file specification has been popped by handle_directive.
3106 This is safe. */
3108 static int
3109 do_include (pfile, keyword, unused1, unused2)
3110 cpp_reader *pfile;
3111 struct directive *keyword;
3112 U_CHAR *unused1, *unused2;
3114 int importing = (keyword->type == T_IMPORT);
3115 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
3116 char *fname; /* Dynamically allocated fname buffer */
3117 char *pcftry;
3118 char *pcfname;
3119 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
3120 enum cpp_token token;
3122 /* Chain of dirs to search */
3123 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
3124 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
3125 struct file_name_list *searchptr = 0;
3126 long old_written = CPP_WRITTEN (pfile);
3128 int flen;
3130 int f; /* file number */
3132 int retried = 0; /* Have already tried macro
3133 expanding the include line */
3134 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
3135 int pcf = -1;
3136 char *pcfbuf;
3137 char *pcfbuflimit;
3138 int pcfnum;
3139 f= -1; /* JF we iz paranoid! */
3141 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3143 if (importing)
3144 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
3145 if (skip_dirs)
3146 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
3149 if (importing && CPP_OPTIONS (pfile)->warn_import
3150 && !CPP_OPTIONS (pfile)->inhibit_warnings
3151 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
3153 pfile->import_warning = 1;
3154 cpp_warning (pfile, "using `#import' is not recommended");
3155 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
3156 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
3157 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
3158 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
3159 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
3160 fprintf (stderr, " ... <real contents of file> ...\n");
3161 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
3162 fprintf (stderr, "Then users can use `#include' any number of times.\n");
3163 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
3164 fprintf (stderr, "when it is equipped with such a conditional.\n");
3167 pfile->parsing_include_directive++;
3168 token = get_directive_token (pfile);
3169 pfile->parsing_include_directive--;
3171 if (token == CPP_STRING)
3173 /* FIXME - check no trailing garbage */
3174 fbeg = pfile->token_buffer + old_written + 1;
3175 fend = CPP_PWRITTEN (pfile) - 1;
3176 if (fbeg[-1] == '<')
3178 angle_brackets = 1;
3179 /* If -I-, start with the first -I dir after the -I-. */
3180 if (CPP_OPTIONS (pfile)->first_bracket_include)
3181 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3183 /* If -I- was specified, don't search current dir, only spec'd ones. */
3184 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3186 cpp_buffer *fp = CPP_BUFFER (pfile);
3187 /* We have "filename". Figure out directory this source
3188 file is coming from and put it on the front of the list. */
3190 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3192 int n;
3193 char *ep,*nam;
3195 if ((nam = fp->nominal_fname) != NULL)
3197 /* Found a named file. Figure out dir of the file,
3198 and put it in front of the search list. */
3199 dsp[0].next = search_start;
3200 search_start = dsp;
3201 #ifndef VMS
3202 ep = rindex (nam, '/');
3203 #else /* VMS */
3204 ep = rindex (nam, ']');
3205 if (ep == NULL) ep = rindex (nam, '>');
3206 if (ep == NULL) ep = rindex (nam, ':');
3207 if (ep != NULL) ep++;
3208 #endif /* VMS */
3209 if (ep != NULL)
3211 n = ep - nam;
3212 dsp[0].fname = (char *) alloca (n + 1);
3213 strncpy (dsp[0].fname, nam, n);
3214 dsp[0].fname[n] = '\0';
3215 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3216 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3218 else
3220 dsp[0].fname = 0; /* Current directory */
3222 dsp[0].got_name_map = 0;
3223 break;
3228 #ifdef VMS
3229 else if (token == CPP_NAME)
3232 * Support '#include xyz' like VAX-C to allow for easy use of all the
3233 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3234 * code from case '<' is repeated here) and generates a warning.
3236 cpp_warning (pfile,
3237 "VAX-C-style include specification found, use '#include <filename.h>' !");
3238 angle_brackets = 1;
3239 /* If -I-, start with the first -I dir after the -I-. */
3240 if (CPP_OPTIONS (pfile)->first_bracket_include)
3241 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3242 fbeg = pfile->token_buffer + old_written;
3243 fend = CPP_PWRITTEN (pfile);
3245 #endif
3246 else
3248 cpp_error (pfile,
3249 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3250 CPP_SET_WRITTEN (pfile, old_written);
3251 skip_rest_of_line (pfile);
3252 return 0;
3255 *fend = 0;
3257 token = get_directive_token (pfile);
3258 if (token != CPP_VSPACE)
3260 cpp_error (pfile, "junk at end of `#include'");
3261 while (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
3262 token = get_directive_token (pfile);
3265 /* For #include_next, skip in the search path
3266 past the dir in which the containing file was found. */
3267 if (skip_dirs)
3269 cpp_buffer *fp = CPP_BUFFER (pfile);
3270 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3271 if (fp->fname != NULL)
3273 /* fp->dir is null if the containing file was specified with
3274 an absolute file name. In that case, don't skip anything. */
3275 if (fp->dir == SELF_DIR_DUMMY)
3276 search_start = CPP_OPTIONS (pfile)->include;
3277 else if (fp->dir)
3278 search_start = fp->dir->next;
3279 break;
3283 CPP_SET_WRITTEN (pfile, old_written);
3285 flen = fend - fbeg;
3287 if (flen == 0)
3289 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3290 return 0;
3293 /* Allocate this permanently, because it gets stored in the definitions
3294 of macros. */
3295 fname = (char *) xmalloc (pfile->max_include_len + flen + 4);
3296 /* + 2 above for slash and terminating null. */
3297 /* + 2 added for '.h' on VMS (to support '#include filename') */
3299 /* If specified file name is absolute, just open it. */
3301 if (*fbeg == '/') {
3302 strncpy (fname, fbeg, flen);
3303 fname[flen] = 0;
3304 if (redundant_include_p (pfile, fname))
3305 return 0;
3306 if (importing)
3307 f = lookup_import (pfile, fname, NULL_PTR);
3308 else
3309 f = open_include_file (pfile, fname, NULL_PTR);
3310 if (f == -2)
3311 return 0; /* Already included this file */
3312 } else {
3313 /* Search directory path, trying to open the file.
3314 Copy each filename tried into FNAME. */
3316 for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
3317 if (searchptr->fname) {
3318 /* The empty string in a search path is ignored.
3319 This makes it possible to turn off entirely
3320 a standard piece of the list. */
3321 if (searchptr->fname[0] == 0)
3322 continue;
3323 strcpy (fname, searchptr->fname);
3324 strcat (fname, "/");
3325 fname[strlen (fname) + flen] = 0;
3326 } else {
3327 fname[0] = 0;
3329 strncat (fname, fbeg, flen);
3330 #ifdef VMS
3331 /* Change this 1/2 Unix 1/2 VMS file specification into a
3332 full VMS file specification */
3333 if (searchptr->fname && (searchptr->fname[0] != 0)) {
3334 /* Fix up the filename */
3335 hack_vms_include_specification (fname);
3336 } else {
3337 /* This is a normal VMS filespec, so use it unchanged. */
3338 strncpy (fname, fbeg, flen);
3339 fname[flen] = 0;
3340 /* if it's '#include filename', add the missing .h */
3341 if (index(fname,'.')==NULL) {
3342 strcat (fname, ".h");
3345 #endif /* VMS */
3346 /* ??? There are currently 3 separate mechanisms for avoiding processing
3347 of redundant include files: #import, #pragma once, and
3348 redundant_include_p. It would be nice if they were unified. */
3349 if (redundant_include_p (pfile, fname))
3350 return 0;
3351 if (importing)
3352 f = lookup_import (pfile, fname, searchptr);
3353 else
3354 f = open_include_file (pfile, fname, searchptr);
3355 if (f == -2)
3356 return 0; /* Already included this file */
3357 #ifdef EACCES
3358 else if (f == -1 && errno == EACCES)
3359 cpp_warning (pfile, "Header file %s exists, but is not readable",
3360 fname);
3361 #endif
3362 if (f >= 0)
3363 break;
3367 if (f < 0)
3369 /* A file that was not found. */
3370 strncpy (fname, fbeg, flen);
3371 fname[flen] = 0;
3372 /* If generating dependencies and -MG was specified, we assume missing
3373 files are leaf files, living in the same directory as the source file
3374 or other similar place; these missing files may be generated from
3375 other files and may not exist yet (eg: y.tab.h). */
3377 if (CPP_OPTIONS(pfile)->print_deps_missing_files
3378 && CPP_PRINT_DEPS (pfile)
3379 > (angle_brackets || (pfile->system_include_depth > 0)))
3381 /* If it was requested as a system header file,
3382 then assume it belongs in the first place to look for such. */
3383 if (angle_brackets)
3385 for (searchptr = search_start; searchptr;
3386 searchptr = searchptr->next)
3388 if (searchptr->fname)
3390 char *p;
3392 if (searchptr->fname[0] == 0)
3393 continue;
3394 p = (char *) alloca (strlen (searchptr->fname)
3395 + strlen (fname) + 2);
3396 strcpy (p, searchptr->fname);
3397 strcat (p, "/");
3398 strcat (p, fname);
3399 deps_output (pfile, p, ' ');
3400 break;
3404 else
3406 /* Otherwise, omit the directory, as if the file existed
3407 in the directory with the source. */
3408 deps_output (pfile, fname, ' ');
3411 /* If -M was specified, and this header file won't be added to the
3412 dependency list, then don't count this as an error, because we can
3413 still produce correct output. Otherwise, we can't produce correct
3414 output, because there may be dependencies we need inside the missing
3415 file, and we don't know what directory this missing file exists in.*/
3416 else if (CPP_PRINT_DEPS (pfile)
3417 && (CPP_PRINT_DEPS (pfile)
3418 <= (angle_brackets || (pfile->system_include_depth > 0))))
3419 cpp_warning (pfile, "No include path in which to find %s", fname);
3420 else if (search_start)
3421 cpp_error_from_errno (pfile, fname);
3422 else
3423 cpp_error (pfile, "No include path in which to find %s", fname);
3425 else {
3426 /* Check to see if this include file is a once-only include file.
3427 If so, give up. */
3429 struct file_name_list *ptr;
3431 for (ptr = pfile->dont_repeat_files; ptr; ptr = ptr->next) {
3432 if (!strcmp (ptr->fname, fname)) {
3433 close (f);
3434 return 0; /* This file was once'd. */
3438 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3439 if (!strcmp (ptr->fname, fname))
3440 break; /* This file was included before. */
3443 if (ptr == 0) {
3444 /* This is the first time for this file. */
3445 /* Add it to list of files included. */
3447 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3448 ptr->control_macro = 0;
3449 ptr->c_system_include_path = 0;
3450 ptr->next = pfile->all_include_files;
3451 pfile->all_include_files = ptr;
3452 ptr->fname = savestring (fname);
3453 ptr->got_name_map = 0;
3455 /* For -M, add this file to the dependencies. */
3456 if (CPP_PRINT_DEPS (pfile)
3457 > (angle_brackets || (pfile->system_include_depth > 0)))
3458 deps_output (pfile, fname, ' ');
3461 /* Handle -H option. */
3462 if (CPP_OPTIONS(pfile)->print_include_names)
3464 cpp_buffer *buf = CPP_BUFFER (pfile);
3465 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3466 putc ('.', stderr);
3467 fprintf (stderr, "%s\n", fname);
3470 if (angle_brackets)
3471 pfile->system_include_depth++;
3473 /* Actually process the file. */
3475 /* Record file on "seen" list for #import. */
3476 add_import (pfile, f, fname);
3478 pcftry = (char *) alloca (strlen (fname) + 30);
3479 pcfbuf = 0;
3480 pcfnum = 0;
3482 #if 0
3483 if (!no_precomp)
3485 struct stat stat_f;
3487 fstat (f, &stat_f);
3489 do {
3490 sprintf (pcftry, "%s%d", fname, pcfnum++);
3492 pcf = open (pcftry, O_RDONLY, 0666);
3493 if (pcf != -1)
3495 struct stat s;
3497 fstat (pcf, &s);
3498 if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
3499 sizeof (s.st_ino))
3500 || stat_f.st_dev != s.st_dev)
3502 pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
3503 /* Don't need it any more. */
3504 close (pcf);
3506 else
3508 /* Don't need it at all. */
3509 close (pcf);
3510 break;
3513 } while (pcf != -1 && !pcfbuf);
3515 #endif
3517 /* Actually process the file */
3518 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3519 return 0;
3520 if (finclude (pfile, f, fname, is_system_include (pfile, fname),
3521 searchptr != dsp ? searchptr : SELF_DIR_DUMMY))
3523 output_line_command (pfile, 0, enter_file);
3524 pfile->only_seen_white = 2;
3527 if (angle_brackets)
3528 pfile->system_include_depth--;
3530 return 0;
3533 /* Return nonzero if there is no need to include file NAME
3534 because it has already been included and it contains a conditional
3535 to make a repeated include do nothing. */
3537 static int
3538 redundant_include_p (pfile, name)
3539 cpp_reader *pfile;
3540 char *name;
3542 struct file_name_list *l = pfile->all_include_files;
3543 for (; l; l = l->next)
3544 if (! strcmp (name, l->fname)
3545 && l->control_macro
3546 && cpp_lookup (pfile, l->control_macro, -1, -1))
3547 return 1;
3548 return 0;
3551 /* Return nonzero if the given FILENAME is an absolute pathname which
3552 designates a file within one of the known "system" include file
3553 directories. We assume here that if the given FILENAME looks like
3554 it is the name of a file which resides either directly in a "system"
3555 include file directory, or within any subdirectory thereof, then the
3556 given file must be a "system" include file. This function tells us
3557 if we should suppress pedantic errors/warnings for the given FILENAME.
3559 The value is 2 if the file is a C-language system header file
3560 for which C++ should (on most systems) assume `extern "C"'. */
3562 static int
3563 is_system_include (pfile, filename)
3564 cpp_reader *pfile;
3565 register char *filename;
3567 struct file_name_list *searchptr;
3569 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3570 searchptr = searchptr->next)
3571 if (searchptr->fname) {
3572 register char *sys_dir = searchptr->fname;
3573 register unsigned length = strlen (sys_dir);
3575 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3577 if (searchptr->c_system_include_path)
3578 return 2;
3579 else
3580 return 1;
3583 return 0;
3588 * Install a name in the assertion hash table.
3590 * If LEN is >= 0, it is the length of the name.
3591 * Otherwise, compute the length by scanning the entire name.
3593 * If HASH is >= 0, it is the precomputed hash code.
3594 * Otherwise, compute the hash code.
3597 static ASSERTION_HASHNODE *
3598 assertion_install (pfile, name, len, hash)
3599 cpp_reader *pfile;
3600 U_CHAR *name;
3601 int len;
3602 int hash;
3604 register ASSERTION_HASHNODE *hp;
3605 register int i, bucket;
3606 register U_CHAR *p, *q;
3608 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3609 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3610 bucket = hash;
3611 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3612 hp->next = pfile->assertion_hashtab[bucket];
3613 pfile->assertion_hashtab[bucket] = hp;
3614 hp->prev = NULL;
3615 if (hp->next != NULL)
3616 hp->next->prev = hp;
3617 hp->length = len;
3618 hp->value = 0;
3619 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3620 p = hp->name;
3621 q = name;
3622 for (i = 0; i < len; i++)
3623 *p++ = *q++;
3624 hp->name[len] = 0;
3625 return hp;
3628 * find the most recent hash node for name name (ending with first
3629 * non-identifier char) installed by install
3631 * If LEN is >= 0, it is the length of the name.
3632 * Otherwise, compute the length by scanning the entire name.
3634 * If HASH is >= 0, it is the precomputed hash code.
3635 * Otherwise, compute the hash code.
3638 static ASSERTION_HASHNODE *
3639 assertion_lookup (pfile, name, len, hash)
3640 cpp_reader *pfile;
3641 U_CHAR *name;
3642 int len;
3643 int hash;
3645 register ASSERTION_HASHNODE *bucket;
3647 bucket = pfile->assertion_hashtab[hash];
3648 while (bucket) {
3649 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3650 return bucket;
3651 bucket = bucket->next;
3653 return NULL;
3656 static void
3657 delete_assertion (hp)
3658 ASSERTION_HASHNODE *hp;
3660 struct tokenlist_list *tail;
3661 if (hp->prev != NULL)
3662 hp->prev->next = hp->next;
3663 if (hp->next != NULL)
3664 hp->next->prev = hp->prev;
3666 for (tail = hp->value; tail; )
3668 struct tokenlist_list *next = tail->next;
3669 free_token_list (tail->tokens);
3670 free (tail);
3671 tail = next;
3674 /* Make sure that the bucket chain header that
3675 the deleted guy was on points to the right thing afterwards. */
3676 if (hp == *hp->bucket_hdr)
3677 *hp->bucket_hdr = hp->next;
3679 free (hp);
3682 /* Convert a character string literal into a nul-terminated string.
3683 The input string is [IN ... LIMIT).
3684 The result is placed in RESULT. RESULT can be the same as IN.
3685 The value returned in the end of the string written to RESULT,
3686 or NULL on error. */
3688 static U_CHAR *
3689 convert_string (pfile, result, in, limit, handle_escapes)
3690 cpp_reader *pfile;
3691 register U_CHAR *result, *in, *limit;
3692 int handle_escapes;
3694 U_CHAR c;
3695 c = *in++;
3696 if (c != '\"')
3697 return NULL;
3698 while (in < limit)
3700 U_CHAR c = *in++;
3701 switch (c)
3703 case '\0':
3704 return NULL;
3705 case '\"':
3706 limit = in;
3707 break;
3708 case '\\':
3709 if (handle_escapes)
3711 char *bpc = (char *) in;
3712 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3713 in = (U_CHAR *) bpc;
3714 if (i >= 0)
3715 *result++ = (U_CHAR)c;
3716 break;
3718 /* else fall through */
3719 default:
3720 *result++ = c;
3723 *result = 0;
3724 return result;
3728 * interpret #line command. Remembers previously seen fnames
3729 * in its very own hash table.
3731 #define FNAME_HASHSIZE 37
3733 static int
3734 do_line (pfile, keyword)
3735 cpp_reader *pfile;
3736 struct directive *keyword;
3738 cpp_buffer *ip = CPP_BUFFER (pfile);
3739 int new_lineno;
3740 long old_written = CPP_WRITTEN (pfile);
3741 enum file_change_code file_change = same_file;
3742 enum cpp_token token;
3743 int i;
3745 token = get_directive_token (pfile);
3747 if (token != CPP_NUMBER
3748 || !isdigit(pfile->token_buffer[old_written]))
3750 cpp_error (pfile, "invalid format `#line' command");
3751 goto bad_line_directive;
3754 /* The Newline at the end of this line remains to be processed.
3755 To put the next line at the specified line number,
3756 we must store a line number now that is one less. */
3757 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3758 CPP_SET_WRITTEN (pfile, old_written);
3760 /* NEW_LINENO is one less than the actual line number here. */
3761 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3762 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3764 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3765 if (PEEKC() && !is_space[PEEKC()]) {
3766 cpp_error (pfile, "invalid format `#line' command");
3767 goto bad_line_directive;
3769 #endif
3771 token = get_directive_token (pfile);
3773 if (token == CPP_STRING) {
3774 U_CHAR *fname = pfile->token_buffer + old_written;
3775 U_CHAR *end_name;
3776 static HASHNODE *fname_table[FNAME_HASHSIZE];
3777 HASHNODE *hp, **hash_bucket;
3778 U_CHAR *p;
3779 long num_start;
3780 int fname_length;
3782 /* Turn the file name, which is a character string literal,
3783 into a null-terminated string. Do this in place. */
3784 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3785 if (end_name == NULL)
3787 cpp_error (pfile, "invalid format `#line' command");
3788 goto bad_line_directive;
3791 fname_length = end_name - fname;
3793 num_start = CPP_WRITTEN (pfile);
3794 token = get_directive_token (pfile);
3795 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3796 p = pfile->token_buffer + num_start;
3797 if (CPP_PEDANTIC (pfile))
3798 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3800 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3802 cpp_error (pfile, "invalid format `#line' command");
3803 goto bad_line_directive;
3805 if (*p == '1')
3806 file_change = enter_file;
3807 else if (*p == 2)
3808 file_change = leave_file;
3809 else if (*p == 3)
3810 ip->system_header_p = 1;
3811 else /* if (*p == 4) */
3812 ip->system_header_p = 2;
3814 CPP_SET_WRITTEN (pfile, num_start);
3815 token = get_directive_token (pfile);
3816 p = pfile->token_buffer + num_start;
3817 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3818 ip->system_header_p = *p == 3 ? 1 : 2;
3819 token = get_directive_token (pfile);
3821 if (token != CPP_VSPACE) {
3822 cpp_error (pfile, "invalid format `#line' command");
3823 goto bad_line_directive;
3827 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3828 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3829 if (hp->length == fname_length
3830 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3831 ip->nominal_fname = hp->value.cpval;
3832 break;
3834 if (hp == 0) {
3835 /* Didn't find it; cons up a new one. */
3836 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3837 hp->next = *hash_bucket;
3838 *hash_bucket = hp;
3840 hp->length = fname_length;
3841 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3842 bcopy (fname, hp->value.cpval, fname_length);
3845 else if (token != CPP_VSPACE && token != CPP_EOF) {
3846 cpp_error (pfile, "invalid format `#line' command");
3847 goto bad_line_directive;
3850 ip->lineno = new_lineno;
3851 bad_line_directive:
3852 skip_rest_of_line (pfile);
3853 CPP_SET_WRITTEN (pfile, old_written);
3854 output_line_command (pfile, 0, file_change);
3855 return 0;
3859 * remove the definition of a symbol from the symbol table.
3860 * according to un*x /lib/cpp, it is not an error to undef
3861 * something that has no definitions, so it isn't one here either.
3864 static int
3865 do_undef (pfile, keyword, buf, limit)
3866 cpp_reader *pfile;
3867 struct directive *keyword;
3868 U_CHAR *buf, *limit;
3870 int sym_length;
3871 HASHNODE *hp;
3872 U_CHAR *orig_buf = buf;
3874 #if 0
3875 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3876 if (pcp_outfile && keyword)
3877 pass_thru_directive (buf, limit, pfile, keyword);
3878 #endif
3880 SKIP_WHITE_SPACE (buf);
3881 sym_length = check_macro_name (pfile, buf, "macro");
3883 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3885 /* If we are generating additional info for debugging (with -g) we
3886 need to pass through all effective #undef commands. */
3887 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3888 pass_thru_directive (orig_buf, limit, pfile, keyword);
3889 if (hp->type != T_MACRO)
3890 cpp_warning (pfile, "undefining `%s'", hp->name);
3891 delete_macro (hp);
3894 if (CPP_PEDANTIC (pfile)) {
3895 buf += sym_length;
3896 SKIP_WHITE_SPACE (buf);
3897 if (buf != limit)
3898 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3900 return 0;
3904 * Report an error detected by the program we are processing.
3905 * Use the text of the line in the error message.
3906 * (We use error because it prints the filename & line#.)
3909 static int
3910 do_error (pfile, keyword, buf, limit)
3911 cpp_reader *pfile;
3912 struct directive *keyword;
3913 U_CHAR *buf, *limit;
3915 int length = limit - buf;
3916 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3917 bcopy (buf, copy, length);
3918 copy[length] = 0;
3919 SKIP_WHITE_SPACE (copy);
3920 cpp_error (pfile, "#error %s", copy);
3921 return 0;
3925 * Report a warning detected by the program we are processing.
3926 * Use the text of the line in the warning message, then continue.
3927 * (We use error because it prints the filename & line#.)
3930 static int
3931 do_warning (pfile, keyword, buf, limit)
3932 cpp_reader *pfile;
3933 struct directive *keyword;
3934 U_CHAR *buf, *limit;
3936 int length = limit - buf;
3937 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3938 bcopy (buf, copy, length);
3939 copy[length] = 0;
3940 SKIP_WHITE_SPACE (copy);
3941 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3942 if -pedantic-errors is given, #warning should cause an error. */
3943 cpp_pedwarn (pfile, "#warning %s", copy);
3944 return 0;
3947 /* Remember the name of the current file being read from so that we can
3948 avoid ever including it again. */
3950 static int
3951 do_once (pfile)
3952 cpp_reader *pfile;
3954 cpp_buffer *ip = NULL;
3955 struct file_name_list *new;
3957 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3959 if (ip == CPP_NULL_BUFFER (pfile))
3960 return 0;
3961 if (ip->fname != NULL)
3962 break;
3966 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3967 new->next = pfile->dont_repeat_files;
3968 pfile->dont_repeat_files = new;
3969 new->fname = savestring (ip->fname);
3970 new->control_macro = 0;
3971 new->got_name_map = 0;
3972 new->c_system_include_path = 0;
3974 return 0;
3977 /* Report program identification. */
3979 static int
3980 do_ident (pfile, keyword, buf, limit)
3981 cpp_reader *pfile;
3982 struct directive *keyword;
3983 U_CHAR *buf, *limit;
3985 /* long old_written = CPP_WRITTEN (pfile);*/
3986 int len;
3988 /* Allow #ident in system headers, since that's not user's fault. */
3989 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3990 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3992 /* Leave rest of line to be read by later calls to cpp_get_token. */
3994 return 0;
3997 /* #pragma and its argument line have already been copied to the output file.
3998 Just check for some recognized pragmas that need validation here. */
4000 static int
4001 do_pragma (pfile, keyword, buf, limit)
4002 cpp_reader *pfile;
4003 struct directive *keyword;
4004 U_CHAR *buf, *limit;
4006 while (*buf == ' ' || *buf == '\t')
4007 buf++;
4008 if (!strncmp (buf, "once", 4)) {
4009 /* Allow #pragma once in system headers, since that's not the user's
4010 fault. */
4011 if (!CPP_BUFFER (pfile)->system_header_p)
4012 cpp_warning (pfile, "`#pragma once' is obsolete");
4013 do_once (pfile);
4016 if (!strncmp (buf, "implementation", 14)) {
4017 /* Be quiet about `#pragma implementation' for a file only if it hasn't
4018 been included yet. */
4019 struct file_name_list *ptr;
4020 U_CHAR *p = buf + 14, *fname, *inc_fname;
4021 int fname_len;
4022 SKIP_WHITE_SPACE (p);
4023 if (*p == '\n' || *p != '\"')
4024 return 0;
4026 fname = p + 1;
4027 p = (U_CHAR *) index (fname, '\"');
4028 fname_len = p != NULL ? p - fname : strlen (fname);
4030 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
4031 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
4032 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
4033 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
4034 cpp_warning (pfile,
4035 "`#pragma implementation' for `%s' appears after file is included",
4036 fname);
4040 return 0;
4043 #if 0
4044 /* This was a fun hack, but #pragma seems to start to be useful.
4045 By failing to recognize it, we pass it through unchanged to cc1. */
4048 * the behavior of the #pragma directive is implementation defined.
4049 * this implementation defines it as follows.
4052 static int
4053 do_pragma ()
4055 close (0);
4056 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
4057 goto nope;
4058 close (1);
4059 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
4060 goto nope;
4061 execl ("/usr/games/hack", "#pragma", 0);
4062 execl ("/usr/games/rogue", "#pragma", 0);
4063 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
4064 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
4065 nope:
4066 fatal ("You are in a maze of twisty compiler features, all different");
4068 #endif
4070 /* Just ignore #sccs, on systems where we define it at all. */
4072 static int
4073 do_sccs (pfile, keyword, buf, limit)
4074 cpp_reader *pfile;
4075 struct directive *keyword;
4076 U_CHAR *buf, *limit;
4078 if (CPP_PEDANTIC (pfile))
4079 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
4080 return 0;
4084 * handle #if command by
4085 * 1) inserting special `defined' keyword into the hash table
4086 * that gets turned into 0 or 1 by special_symbol (thus,
4087 * if the luser has a symbol called `defined' already, it won't
4088 * work inside the #if command)
4089 * 2) rescan the input into a temporary output buffer
4090 * 3) pass the output buffer to the yacc parser and collect a value
4091 * 4) clean up the mess left from steps 1 and 2.
4092 * 5) call conditional_skip to skip til the next #endif (etc.),
4093 * or not, depending on the value from step 3.
4096 static int
4097 do_if (pfile, keyword, buf, limit)
4098 cpp_reader *pfile;
4099 struct directive *keyword;
4100 U_CHAR *buf, *limit;
4102 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4103 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
4104 return 0;
4108 * handle a #elif directive by not changing if_stack either.
4109 * see the comment above do_else.
4112 static int
4113 do_elif (pfile, keyword, buf, limit)
4114 cpp_reader *pfile;
4115 struct directive *keyword;
4116 U_CHAR *buf, *limit;
4118 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4119 cpp_error (pfile, "`#elif' not within a conditional");
4120 return 0;
4121 } else {
4122 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4123 cpp_error (pfile, "`#elif' after `#else'");
4124 #if 0
4125 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4126 #endif
4127 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
4128 && strcmp (pfile->if_stack->fname,
4129 CPP_BUFFER (pfile)->nominal_fname) != 0)
4130 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4131 fprintf (stderr, ")\n");
4133 pfile->if_stack->type = T_ELIF;
4136 if (pfile->if_stack->if_succeeded)
4137 skip_if_group (pfile, 0);
4138 else {
4139 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
4140 if (value == 0)
4141 skip_if_group (pfile, 0);
4142 else {
4143 ++pfile->if_stack->if_succeeded; /* continue processing input */
4144 output_line_command (pfile, 1, same_file);
4147 return 0;
4151 * evaluate a #if expression in BUF, of length LENGTH,
4152 * then parse the result as a C expression and return the value as an int.
4155 static HOST_WIDE_INT
4156 eval_if_expression (pfile, buf, length)
4157 cpp_reader *pfile;
4158 U_CHAR *buf;
4159 int length;
4161 HASHNODE *save_defined;
4162 HOST_WIDE_INT value;
4163 long old_written = CPP_WRITTEN (pfile);
4165 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
4166 pfile->pcp_inside_if = 1;
4168 value = cpp_parse_expr (pfile);
4169 pfile->pcp_inside_if = 0;
4170 delete_macro (save_defined); /* clean up special symbol */
4172 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4174 return value;
4178 * routine to handle ifdef/ifndef. Try to look up the symbol,
4179 * then do or don't skip to the #endif/#else/#elif depending
4180 * on what directive is actually being processed.
4183 static int
4184 do_xifdef (pfile, keyword, unused1, unused2)
4185 cpp_reader *pfile;
4186 struct directive *keyword;
4187 U_CHAR *unused1, *unused2;
4189 int skip;
4190 cpp_buffer *ip = CPP_BUFFER (pfile);
4191 U_CHAR *ident;
4192 int ident_length;
4193 enum cpp_token token;
4194 int start_of_file = 0;
4195 U_CHAR *control_macro = 0;
4196 int old_written = CPP_WRITTEN (pfile);
4198 /* Detect a #ifndef at start of file (not counting comments). */
4199 if (ip->fname != 0 && keyword->type == T_IFNDEF)
4200 start_of_file = pfile->only_seen_white == 2;
4202 pfile->no_macro_expand++;
4203 token = get_directive_token (pfile);
4204 pfile->no_macro_expand--;
4206 ident = pfile->token_buffer + old_written;
4207 ident_length = CPP_WRITTEN (pfile) - old_written;
4208 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
4210 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
4212 skip = (keyword->type == T_IFDEF);
4213 if (! CPP_TRADITIONAL (pfile))
4214 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
4216 else if (token == CPP_NAME)
4218 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
4219 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
4220 if (start_of_file && !skip)
4222 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
4223 bcopy (ident, control_macro, ident_length + 1);
4226 else
4228 skip = (keyword->type == T_IFDEF);
4229 if (! CPP_TRADITIONAL (pfile))
4230 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
4233 if (!CPP_TRADITIONAL (pfile))
4234 { int c;
4235 cpp_skip_hspace (pfile);
4236 c = PEEKC ();
4237 if (c != EOF && c != '\n')
4238 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
4240 skip_rest_of_line (pfile);
4242 #if 0
4243 if (pcp_outfile) {
4244 /* Output a precondition for this macro. */
4245 if (hp && hp->value.defn->predefined)
4246 fprintf (pcp_outfile, "#define %s\n", hp->name);
4247 else {
4248 U_CHAR *cp = buf;
4249 fprintf (pcp_outfile, "#undef ");
4250 while (is_idchar[*cp]) /* Ick! */
4251 fputc (*cp++, pcp_outfile);
4252 putc ('\n', pcp_outfile);
4254 #endif
4256 conditional_skip (pfile, skip, T_IF, control_macro);
4257 return 0;
4260 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
4261 If this is a #ifndef starting at the beginning of a file,
4262 CONTROL_MACRO is the macro name tested by the #ifndef.
4263 Otherwise, CONTROL_MACRO is 0. */
4265 static void
4266 conditional_skip (pfile, skip, type, control_macro)
4267 cpp_reader *pfile;
4268 int skip;
4269 enum node_type type;
4270 U_CHAR *control_macro;
4272 IF_STACK_FRAME *temp;
4274 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4275 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
4276 #if 0
4277 temp->lineno = CPP_BUFFER (pfile)->lineno;
4278 #endif
4279 temp->next = pfile->if_stack;
4280 temp->control_macro = control_macro;
4281 pfile->if_stack = temp;
4283 pfile->if_stack->type = type;
4285 if (skip != 0) {
4286 skip_if_group (pfile, 0);
4287 return;
4288 } else {
4289 ++pfile->if_stack->if_succeeded;
4290 output_line_command (pfile, 1, same_file);
4295 * skip to #endif, #else, or #elif. adjust line numbers, etc.
4296 * leaves input ptr at the sharp sign found.
4297 * If ANY is nonzero, return at next directive of any sort.
4300 static void
4301 skip_if_group (pfile, any)
4302 cpp_reader *pfile;
4303 int any;
4305 int c;
4306 int at_beg_of_line = 1;
4307 struct directive *kt;
4308 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
4309 #if 0
4310 U_CHAR *beg_of_line = bp;
4311 #endif
4312 register int ident_length;
4313 U_CHAR *ident, *after_ident;
4314 struct parse_marker line_start_mark;
4316 parse_set_mark (&line_start_mark, pfile);
4318 if (CPP_OPTIONS (pfile)->output_conditionals) {
4319 static char failed[] = "#failed\n";
4320 CPP_PUTS (pfile, failed, sizeof(failed)-1);
4321 pfile->lineno++;
4322 output_line_command (pfile, 1, same_file);
4325 beg_of_line:
4326 if (CPP_OPTIONS (pfile)->output_conditionals)
4328 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4329 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4330 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4332 parse_move_mark (&line_start_mark, pfile);
4333 if (!CPP_TRADITIONAL (pfile))
4334 cpp_skip_hspace (pfile);
4335 c = GETC();
4336 if (c == '#')
4338 int old_written = CPP_WRITTEN (pfile);
4339 cpp_skip_hspace (pfile);
4341 parse_name (pfile, GETC());
4342 ident_length = CPP_WRITTEN (pfile) - old_written;
4343 ident = pfile->token_buffer + old_written;
4344 pfile->limit = ident;
4345 #if 0
4346 if (ident_length == 0)
4347 goto not_a_directive;
4349 /* Handle # followed by a line number. */
4351 /* Avoid error for `###' and similar cases unless -pedantic. */
4352 #endif
4354 for (kt = directive_table; kt->length >= 0; kt++)
4356 IF_STACK_FRAME *temp;
4357 if (ident_length == kt->length
4358 && strncmp (ident, kt->name, kt->length) == 0)
4360 /* If we are asked to return on next directive, do so now. */
4361 if (any)
4362 goto done;
4364 switch (kt->type)
4366 case T_IF:
4367 case T_IFDEF:
4368 case T_IFNDEF:
4369 temp
4370 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4371 temp->next = pfile->if_stack;
4372 pfile->if_stack = temp;
4373 #if 0
4374 temp->lineno = CPP_BUFFER(pfile)->lineno;
4375 #endif
4376 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4377 temp->type = kt->type;
4378 break;
4379 case T_ELSE:
4380 case T_ENDIF:
4381 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4382 validate_else (pfile,
4383 kt->type == T_ELSE ? "#else" : "#endif");
4384 case T_ELIF:
4385 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4387 cpp_error (pfile,
4388 "`#%s' not within a conditional", kt->name);
4389 break;
4391 else if (pfile->if_stack == save_if_stack)
4392 goto done; /* found what we came for */
4394 if (kt->type != T_ENDIF)
4396 if (pfile->if_stack->type == T_ELSE)
4397 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4398 pfile->if_stack->type = kt->type;
4399 break;
4402 temp = pfile->if_stack;
4403 pfile->if_stack = temp->next;
4404 free (temp);
4405 break;
4406 default: ;
4408 break;
4410 /* Don't let erroneous code go by. */
4411 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4412 && CPP_PEDANTIC (pfile))
4413 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4415 c = GETC ();
4417 /* We're in the middle of a line. Skip the rest of it. */
4418 for (;;) {
4419 switch (c)
4421 long old;
4422 case EOF:
4423 goto done;
4424 case '/': /* possible comment */
4425 c = skip_comment (pfile, NULL);
4426 if (c == EOF)
4427 goto done;
4428 break;
4429 case '\"':
4430 case '\'':
4431 FORWARD(-1);
4432 old = CPP_WRITTEN (pfile);
4433 cpp_get_token (pfile);
4434 CPP_SET_WRITTEN (pfile, old);
4435 break;
4436 case '\\':
4437 /* Char after backslash loses its special meaning. */
4438 if (PEEKC() == '\n')
4439 FORWARD (1);
4440 break;
4441 case '\n':
4442 goto beg_of_line;
4443 break;
4445 c = GETC ();
4447 done:
4448 if (CPP_OPTIONS (pfile)->output_conditionals) {
4449 static char end_failed[] = "#endfailed\n";
4450 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4451 pfile->lineno++;
4453 pfile->only_seen_white = 1;
4454 parse_goto_mark (&line_start_mark, pfile);
4455 parse_clear_mark (&line_start_mark);
4459 * handle a #else directive. Do this by just continuing processing
4460 * without changing if_stack ; this is so that the error message
4461 * for missing #endif's etc. will point to the original #if. It
4462 * is possible that something different would be better.
4465 static int
4466 do_else (pfile, keyword, buf, limit)
4467 cpp_reader *pfile;
4468 struct directive *keyword;
4469 U_CHAR *buf, *limit;
4471 cpp_buffer *ip = CPP_BUFFER (pfile);
4473 if (CPP_PEDANTIC (pfile))
4474 validate_else (pfile, "#else");
4475 skip_rest_of_line (pfile);
4477 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4478 cpp_error (pfile, "`#else' not within a conditional");
4479 return 0;
4480 } else {
4481 /* #ifndef can't have its special treatment for containing the whole file
4482 if it has a #else clause. */
4483 pfile->if_stack->control_macro = 0;
4485 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4486 cpp_error (pfile, "`#else' after `#else'");
4487 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4488 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4489 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4490 fprintf (stderr, ")\n");
4492 pfile->if_stack->type = T_ELSE;
4495 if (pfile->if_stack->if_succeeded)
4496 skip_if_group (pfile, 0);
4497 else {
4498 ++pfile->if_stack->if_succeeded; /* continue processing input */
4499 output_line_command (pfile, 1, same_file);
4501 return 0;
4505 * unstack after #endif command
4508 static int
4509 do_endif (pfile, keyword, buf, limit)
4510 cpp_reader *pfile;
4511 struct directive *keyword;
4512 U_CHAR *buf, *limit;
4514 if (CPP_PEDANTIC (pfile))
4515 validate_else (pfile, "#endif");
4516 skip_rest_of_line (pfile);
4518 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4519 cpp_error (pfile, "unbalanced `#endif'");
4520 else
4522 IF_STACK_FRAME *temp = pfile->if_stack;
4523 pfile->if_stack = temp->next;
4524 if (temp->control_macro != 0)
4526 /* This #endif matched a #ifndef at the start of the file.
4527 See if it is at the end of the file. */
4528 struct parse_marker start_mark;
4529 int c;
4531 parse_set_mark (&start_mark, pfile);
4533 for (;;)
4535 cpp_skip_hspace (pfile);
4536 c = GETC ();
4537 if (c != '\n')
4538 break;
4540 parse_goto_mark (&start_mark, pfile);
4541 parse_clear_mark (&start_mark);
4543 if (c == EOF)
4545 /* If we get here, this #endif ends a #ifndef
4546 that contains all of the file (aside from whitespace).
4547 Arrange not to include the file again
4548 if the macro that was tested is defined.
4550 Do not do this for the top-level file in a -include or any
4551 file in a -imacros. */
4552 #if 0
4553 FIXME!
4554 if (indepth != 0
4555 && ! (indepth == 1 && pfile->no_record_file)
4556 && ! (pfile->no_record_file && no_output))
4557 #endif
4559 struct file_name_list *ifile = pfile->all_include_files;
4561 for ( ; ifile != NULL; ifile = ifile->next)
4563 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4565 ifile->control_macro = temp->control_macro;
4566 break;
4572 free (temp);
4573 output_line_command (pfile, 1, same_file);
4575 return 0;
4578 /* When an #else or #endif is found while skipping failed conditional,
4579 if -pedantic was specified, this is called to warn about text after
4580 the command name. P points to the first char after the command name. */
4582 static void
4583 validate_else (pfile, directive)
4584 cpp_reader *pfile;
4585 char *directive;
4587 int c;
4588 cpp_skip_hspace (pfile);
4589 c = PEEKC ();
4590 if (c != EOF && c != '\n')
4591 cpp_pedwarn (pfile,
4592 "text following `%s' violates ANSI standard", directive);
4595 /* Get the next token, and add it to the text in pfile->token_buffer.
4596 Return the kind of token we got. */
4598 enum cpp_token
4599 cpp_get_token (pfile)
4600 cpp_reader *pfile;
4602 register int c, c2, c3;
4603 long old_written;
4604 long start_line, start_column;
4605 enum cpp_token token;
4606 struct cpp_options *opts = CPP_OPTIONS (pfile);
4607 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4608 get_next:
4609 c = GETC();
4610 if (c == EOF)
4612 handle_eof:
4613 if (CPP_BUFFER (pfile)->seen_eof)
4615 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4616 goto get_next;
4617 else
4618 return CPP_EOF;
4620 else
4622 cpp_buffer *next_buf
4623 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4624 CPP_BUFFER (pfile)->seen_eof = 1;
4625 if (CPP_BUFFER (pfile)->nominal_fname
4626 && next_buf != CPP_NULL_BUFFER (pfile))
4628 /* We're about to return from an #include file.
4629 Emit #line information now (as part of the CPP_POP) result.
4630 But the #line refers to the file we will pop to. */
4631 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4632 CPP_BUFFER (pfile) = next_buf;
4633 pfile->input_stack_listing_current = 0;
4634 output_line_command (pfile, 0, leave_file);
4635 CPP_BUFFER (pfile) = cur_buffer;
4637 return CPP_POP;
4640 else
4642 switch (c)
4644 long newlines;
4645 struct parse_marker start_mark;
4646 case '/':
4647 if (PEEKC () == '=')
4648 goto op2;
4649 if (opts->put_out_comments)
4650 parse_set_mark (&start_mark, pfile);
4651 newlines = 0;
4652 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4653 &start_line, &start_column);
4654 c = skip_comment (pfile, &newlines);
4655 if (opts->put_out_comments && (c == '/' || c == EOF))
4656 parse_clear_mark (&start_mark);
4657 if (c == '/')
4658 goto randomchar;
4659 if (c == EOF)
4661 cpp_error_with_line (pfile, start_line, start_column,
4662 "unterminated comment");
4663 goto handle_eof;
4665 c = '/'; /* Initial letter of comment. */
4666 return_comment:
4667 /* Comments are equivalent to spaces.
4668 For -traditional, a comment is equivalent to nothing. */
4669 if (opts->put_out_comments)
4671 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4672 long dummy;
4673 U_CHAR *start = pbuf->buf + start_mark.position;
4674 int len = pbuf->cur - start;
4675 CPP_RESERVE(pfile, 1 + len);
4676 CPP_PUTC_Q (pfile, c);
4677 CPP_PUTS_Q (pfile, start, len);
4678 pfile->lineno += newlines;
4679 parse_clear_mark (&start_mark);
4680 return CPP_COMMENT;
4682 else if (CPP_TRADITIONAL (pfile))
4684 return CPP_COMMENT;
4686 else
4688 #if 0
4689 /* This may not work if cpp_get_token is called recursively,
4690 since many places look for horizontal space. */
4691 if (newlines)
4693 /* Copy the newlines into the output buffer, in order to
4694 avoid the pain of a #line every time a multiline comment
4695 is seen. */
4696 CPP_RESERVE(pfile, newlines);
4697 while (--newlines >= 0)
4699 CPP_PUTC_Q (pfile, '\n');
4700 pfile->lineno++;
4702 return CPP_VSPACE;
4704 #endif
4705 CPP_RESERVE(pfile, 1);
4706 CPP_PUTC_Q (pfile, ' ');
4707 return CPP_HSPACE;
4709 #if 0
4710 if (opts->for_lint) {
4711 U_CHAR *argbp;
4712 int cmdlen, arglen;
4713 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4715 if (lintcmd != NULL) {
4716 /* I believe it is always safe to emit this newline: */
4717 obp[-1] = '\n';
4718 bcopy ("#pragma lint ", (char *) obp, 13);
4719 obp += 13;
4720 bcopy (lintcmd, (char *) obp, cmdlen);
4721 obp += cmdlen;
4723 if (arglen != 0) {
4724 *(obp++) = ' ';
4725 bcopy (argbp, (char *) obp, arglen);
4726 obp += arglen;
4729 /* OK, now bring us back to the state we were in before we entered
4730 this branch. We need #line b/c the newline for the pragma
4731 could fuck things up. */
4732 output_line_command (pfile, 0, same_file);
4733 *(obp++) = ' '; /* just in case, if comments are copied thru */
4734 *(obp++) = '/';
4737 #endif
4739 case '#':
4740 #if 0
4741 /* If this is expanding a macro definition, don't recognize
4742 preprocessor directives. */
4743 if (ip->macro != 0)
4744 goto randomchar;
4745 /* If this is expand_into_temp_buffer, recognize them
4746 only after an actual newline at this level,
4747 not at the beginning of the input level. */
4748 if (ip->fname == 0 && beg_of_line == ip->buf)
4749 goto randomchar;
4750 if (ident_length)
4751 goto specialchar;
4752 #endif
4754 if (!pfile->only_seen_white)
4755 goto randomchar;
4756 if (handle_directive (pfile))
4757 return CPP_DIRECTIVE;
4758 pfile->only_seen_white = 0;
4759 return CPP_OTHER;
4761 case '\"':
4762 case '\'':
4763 /* A single quoted string is treated like a double -- some
4764 programs (e.g., troff) are perverse this way */
4765 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4766 &start_line, &start_column);
4767 old_written = CPP_WRITTEN (pfile);
4768 string:
4769 CPP_PUTC (pfile, c);
4770 while (1)
4772 int cc = GETC();
4773 if (cc == EOF)
4775 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4777 /* try harder: this string crosses a macro expansion
4778 boundary. This can happen naturally if -traditional.
4779 Otherwise, only -D can make a macro with an unmatched
4780 quote. */
4781 cpp_buffer *next_buf
4782 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4783 (*CPP_BUFFER (pfile)->cleanup)
4784 (CPP_BUFFER (pfile), pfile);
4785 CPP_BUFFER (pfile) = next_buf;
4786 continue;
4788 if (!CPP_TRADITIONAL (pfile))
4790 cpp_error_with_line (pfile, start_line, start_column,
4791 "unterminated string or character constant");
4792 if (pfile->multiline_string_line != start_line
4793 && pfile->multiline_string_line != 0)
4794 cpp_error_with_line (pfile,
4795 pfile->multiline_string_line, -1,
4796 "possible real start of unterminated constant");
4797 pfile->multiline_string_line = 0;
4799 break;
4801 CPP_PUTC (pfile, cc);
4802 switch (cc)
4804 case '\n':
4805 /* Traditionally, end of line ends a string constant with
4806 no error. So exit the loop and record the new line. */
4807 if (CPP_TRADITIONAL (pfile))
4808 goto while2end;
4809 if (c == '\'')
4811 cpp_error_with_line (pfile, start_line, start_column,
4812 "unterminated character constant");
4813 goto while2end;
4815 if (CPP_PEDANTIC (pfile)
4816 && pfile->multiline_string_line == 0)
4818 cpp_pedwarn_with_line (pfile, start_line, start_column,
4819 "string constant runs past end of line");
4821 if (pfile->multiline_string_line == 0)
4822 pfile->multiline_string_line = start_line;
4823 break;
4825 case '\\':
4826 cc = GETC();
4827 if (cc == '\n')
4829 /* Backslash newline is replaced by nothing at all. */
4830 CPP_ADJUST_WRITTEN (pfile, -1);
4831 pfile->lineno++;
4833 else
4835 /* ANSI stupidly requires that in \\ the second \
4836 is *not* prevented from combining with a newline. */
4837 NEWLINE_FIX1(cc);
4838 if (cc != EOF)
4839 CPP_PUTC (pfile, cc);
4841 break;
4843 case '\"':
4844 case '\'':
4845 if (cc == c)
4846 goto while2end;
4847 break;
4850 while2end:
4851 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4852 CPP_PWRITTEN (pfile));
4853 pfile->only_seen_white = 0;
4854 return c == '\'' ? CPP_CHAR : CPP_STRING;
4856 case '$':
4857 if (!opts->dollars_in_ident)
4858 goto randomchar;
4859 goto letter;
4861 case ':':
4862 if (opts->cplusplus && PEEKC () == ':')
4863 goto op2;
4864 goto randomchar;
4866 case '&':
4867 case '+':
4868 case '|':
4869 NEWLINE_FIX;
4870 c2 = PEEKC ();
4871 if (c2 == c || c2 == '=')
4872 goto op2;
4873 goto randomchar;
4875 case '*':
4876 case '!':
4877 case '%':
4878 case '=':
4879 case '^':
4880 NEWLINE_FIX;
4881 if (PEEKC () == '=')
4882 goto op2;
4883 goto randomchar;
4885 case '-':
4886 NEWLINE_FIX;
4887 c2 = PEEKC ();
4888 if (c2 == '-' && opts->chill)
4890 /* Chill style comment */
4891 if (opts->put_out_comments)
4892 parse_set_mark (&start_mark, pfile);
4893 FORWARD(1); /* Skip second '-'. */
4894 for (;;)
4896 c = GETC ();
4897 if (c == EOF)
4898 break;
4899 if (c == '\n')
4901 /* Don't consider final '\n' to be part of comment. */
4902 FORWARD(-1);
4903 break;
4906 c = '-';
4907 goto return_comment;
4909 if (c2 == '-' || c2 == '=' || c2 == '>')
4910 goto op2;
4911 goto randomchar;
4913 case '<':
4914 if (pfile->parsing_include_directive)
4916 for (;;)
4918 CPP_PUTC (pfile, c);
4919 if (c == '>')
4920 break;
4921 c = GETC ();
4922 NEWLINE_FIX1 (c);
4923 if (c == '\n' || c == EOF)
4925 cpp_error (pfile,
4926 "missing '>' in `#include <FILENAME>'");
4927 break;
4930 return CPP_STRING;
4932 /* else fall through */
4933 case '>':
4934 NEWLINE_FIX;
4935 c2 = PEEKC ();
4936 if (c2 == '=')
4937 goto op2;
4938 if (c2 != c)
4939 goto randomchar;
4940 FORWARD(1);
4941 CPP_RESERVE (pfile, 4);
4942 CPP_PUTC (pfile, c);
4943 CPP_PUTC (pfile, c2);
4944 NEWLINE_FIX;
4945 c3 = PEEKC ();
4946 if (c3 == '=')
4947 CPP_PUTC_Q (pfile, GETC ());
4948 CPP_NUL_TERMINATE_Q (pfile);
4949 pfile->only_seen_white = 0;
4950 return CPP_OTHER;
4952 case '@':
4953 if (CPP_BUFFER (pfile)->has_escapes)
4955 c = GETC ();
4956 if (c == '-')
4958 if (pfile->output_escapes)
4959 CPP_PUTS (pfile, "@-", 2);
4960 parse_name (pfile, GETC ());
4961 return CPP_NAME;
4963 else if (is_space [c])
4965 CPP_RESERVE (pfile, 2);
4966 if (pfile->output_escapes)
4967 CPP_PUTC_Q (pfile, '@');
4968 CPP_PUTC_Q (pfile, c);
4969 return CPP_HSPACE;
4972 if (pfile->output_escapes)
4974 CPP_PUTS (pfile, "@@", 2);
4975 return CPP_OTHER;
4977 goto randomchar;
4979 case '.':
4980 NEWLINE_FIX;
4981 c2 = PEEKC ();
4982 if (isdigit(c2))
4984 CPP_RESERVE(pfile, 2);
4985 CPP_PUTC_Q (pfile, '.');
4986 c = GETC ();
4987 goto number;
4989 /* FIXME - misses the case "..\\\n." */
4990 if (c2 == '.' && PEEKN(1) == '.')
4992 CPP_RESERVE(pfile, 4);
4993 CPP_PUTC_Q (pfile, '.');
4994 CPP_PUTC_Q (pfile, '.');
4995 CPP_PUTC_Q (pfile, '.');
4996 FORWARD (2);
4997 CPP_NUL_TERMINATE_Q (pfile);
4998 pfile->only_seen_white = 0;
4999 return CPP_3DOTS;
5001 goto randomchar;
5003 op2:
5004 token = CPP_OTHER;
5005 pfile->only_seen_white = 0;
5006 op2any:
5007 CPP_RESERVE(pfile, 3);
5008 CPP_PUTC_Q (pfile, c);
5009 CPP_PUTC_Q (pfile, GETC ());
5010 CPP_NUL_TERMINATE_Q (pfile);
5011 return token;
5013 case 'L':
5014 NEWLINE_FIX;
5015 c2 = PEEKC ();
5016 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
5018 CPP_PUTC (pfile, c);
5019 c = GETC ();
5020 goto string;
5022 goto letter;
5024 case '0': case '1': case '2': case '3': case '4':
5025 case '5': case '6': case '7': case '8': case '9':
5026 number:
5027 c2 = '.';
5028 for (;;)
5030 CPP_RESERVE (pfile, 2);
5031 CPP_PUTC_Q (pfile, c);
5032 NEWLINE_FIX;
5033 c = PEEKC ();
5034 if (c == EOF)
5035 break;
5036 if (!is_idchar[c] && c != '.'
5037 && ((c2 != 'e' && c2 != 'E'
5038 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
5039 || (c != '+' && c != '-')))
5040 break;
5041 FORWARD(1);
5042 c2= c;
5044 CPP_NUL_TERMINATE_Q (pfile);
5045 pfile->only_seen_white = 0;
5046 return CPP_NUMBER;
5047 case 'b': case 'c': case 'd': case 'h': case 'o':
5048 case 'B': case 'C': case 'D': case 'H': case 'O':
5049 if (opts->chill && PEEKC () == '\'')
5051 pfile->only_seen_white = 0;
5052 CPP_RESERVE (pfile, 2);
5053 CPP_PUTC_Q (pfile, c);
5054 CPP_PUTC_Q (pfile, '\'');
5055 FORWARD(1);
5056 for (;;)
5058 c = GETC();
5059 if (c == EOF)
5060 goto chill_number_eof;
5061 if (!is_idchar[c])
5063 if (c == '\\' && PEEKC() == '\n')
5065 FORWARD(2);
5066 continue;
5068 break;
5070 CPP_PUTC (pfile, c);
5072 if (c == '\'')
5074 CPP_RESERVE (pfile, 2);
5075 CPP_PUTC_Q (pfile, c);
5076 CPP_NUL_TERMINATE_Q (pfile);
5077 return CPP_STRING;
5079 else
5081 FORWARD(-1);
5082 chill_number_eof:
5083 CPP_NUL_TERMINATE (pfile);
5084 return CPP_NUMBER;
5087 else
5088 goto letter;
5089 case '_':
5090 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
5091 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
5092 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
5093 case 'x': case 'y': case 'z':
5094 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
5095 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
5096 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
5097 case 'Y': case 'Z':
5098 letter:
5100 HASHNODE *hp;
5101 unsigned char *ident;
5102 int before_name_written = CPP_WRITTEN (pfile);
5103 int ident_len;
5104 parse_name (pfile, c);
5105 pfile->only_seen_white = 0;
5106 if (pfile->no_macro_expand)
5107 return CPP_NAME;
5108 ident = pfile->token_buffer + before_name_written;
5109 ident_len = CPP_PWRITTEN (pfile) - ident;
5110 hp = cpp_lookup (pfile, ident, ident_len, -1);
5111 if (!hp)
5112 return CPP_NAME;
5113 if (hp->type == T_DISABLED)
5115 if (pfile->output_escapes)
5116 { /* Return "@-IDENT", followed by '\0'. */
5117 int i;
5118 CPP_RESERVE (pfile, 3);
5119 ident = pfile->token_buffer + before_name_written;
5120 CPP_ADJUST_WRITTEN (pfile, 2);
5121 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
5122 ident[0] = '@';
5123 ident[1] = '-';
5125 return CPP_NAME;
5128 /* If macro wants an arglist, verify that a '(' follows.
5129 first skip all whitespace, copying it to the output
5130 after the macro name. Then, if there is no '(',
5131 decide this is not a macro call and leave things that way. */
5132 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
5134 struct parse_marker macro_mark;
5135 int is_macro_call;
5136 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
5138 cpp_buffer *next_buf;
5139 cpp_skip_hspace (pfile);
5140 if (PEEKC () != EOF)
5141 break;
5142 next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
5143 (*CPP_BUFFER (pfile)->cleanup) (CPP_BUFFER (pfile), pfile);
5144 CPP_BUFFER (pfile) = next_buf;
5146 parse_set_mark (&macro_mark, pfile);
5147 for (;;)
5149 cpp_skip_hspace (pfile);
5150 c = PEEKC ();
5151 is_macro_call = c == '(';
5152 if (c != '\n')
5153 break;
5154 FORWARD (1);
5156 if (!is_macro_call)
5157 parse_goto_mark (&macro_mark, pfile);
5158 parse_clear_mark (&macro_mark);
5159 if (!is_macro_call)
5160 return CPP_NAME;
5162 /* This is now known to be a macro call. */
5164 /* it might not actually be a macro. */
5165 if (hp->type != T_MACRO) {
5166 int xbuf_len; U_CHAR *xbuf;
5167 CPP_SET_WRITTEN (pfile, before_name_written);
5168 special_symbol (hp, pfile);
5169 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
5170 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
5171 CPP_SET_WRITTEN (pfile, before_name_written);
5172 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
5173 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
5175 else
5177 /* Expand the macro, reading arguments as needed,
5178 and push the expansion on the input stack. */
5179 macroexpand (pfile, hp);
5180 CPP_SET_WRITTEN (pfile, before_name_written);
5183 /* An extra "@ " is added to the end of a macro expansion
5184 to prevent accidental token pasting. We prefer to avoid
5185 unneeded extra spaces (for the sake of cpp-using tools like
5186 imake). Here we remove the space if it is safe to do so. */
5187 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
5188 && pfile->buffer->rlimit[-2] == '@'
5189 && pfile->buffer->rlimit[-1] == ' ')
5191 int c1 = pfile->buffer->rlimit[-3];
5192 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
5193 if (c2 == EOF || ! unsafe_chars (c1, c2))
5194 pfile->buffer->rlimit -= 2;
5197 goto get_next;
5199 case ' ': case '\t': case '\v': case '\r':
5200 for (;;)
5202 CPP_PUTC (pfile, c);
5203 c = PEEKC ();
5204 if (c == EOF || !is_hor_space[c])
5205 break;
5206 FORWARD(1);
5208 return CPP_HSPACE;
5210 case '\\':
5211 c2 = PEEKC ();
5212 if (c2 != '\n')
5213 goto randomchar;
5214 token = CPP_HSPACE;
5215 goto op2any;
5217 case '\n':
5218 CPP_PUTC (pfile, c);
5219 if (pfile->only_seen_white == 0)
5220 pfile->only_seen_white = 1;
5221 pfile->lineno++;
5222 output_line_command (pfile, 1, same_file);
5223 return CPP_VSPACE;
5225 case '(': token = CPP_LPAREN; goto char1;
5226 case ')': token = CPP_RPAREN; goto char1;
5227 case '{': token = CPP_LBRACE; goto char1;
5228 case '}': token = CPP_RBRACE; goto char1;
5229 case ',': token = CPP_COMMA; goto char1;
5230 case ';': token = CPP_SEMICOLON; goto char1;
5232 randomchar:
5233 default:
5234 token = CPP_OTHER;
5235 char1:
5236 pfile->only_seen_white = 0;
5237 CPP_PUTC (pfile, c);
5238 return token;
5243 /* Like cpp_get_token, but skip spaces and comments. */
5245 enum cpp_token
5246 cpp_get_non_space_token (pfile)
5247 cpp_reader *pfile;
5249 int old_written = CPP_WRITTEN (pfile);
5250 for (;;)
5252 enum cpp_token token = cpp_get_token (pfile);
5253 if (token != CPP_COMMENT && token != CPP_POP
5254 && token != CPP_HSPACE && token != CPP_VSPACE)
5255 return token;
5256 CPP_SET_WRITTEN (pfile, old_written);
5260 /* Parse an identifier starting with C. */
5263 parse_name (pfile, c)
5264 cpp_reader *pfile; int c;
5266 for (;;)
5268 if (! is_idchar[c])
5270 if (c == '\\' && PEEKC() == '\n')
5272 FORWARD(2);
5273 continue;
5275 FORWARD (-1);
5276 break;
5279 if (c == '$' && CPP_PEDANTIC (pfile))
5280 cpp_pedwarn ("`$' in identifier");
5282 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
5283 CPP_PUTC_Q (pfile, c);
5284 c = GETC();
5285 if (c == EOF)
5286 break;
5288 CPP_NUL_TERMINATE_Q (pfile);
5289 return 1;
5293 /* Maintain and search list of included files, for #import. */
5295 /* Hash a file name for import_hash_table. */
5297 static int
5298 import_hash (f)
5299 char *f;
5301 int val = 0;
5303 while (*f) val += *f++;
5304 return (val%IMPORT_HASH_SIZE);
5307 /* Search for file FILENAME in import_hash_table.
5308 Return -2 if found, either a matching name or a matching inode.
5309 Otherwise, open the file and return a file descriptor if successful
5310 or -1 if unsuccessful. */
5312 static int
5313 lookup_import (pfile, filename, searchptr)
5314 cpp_reader *pfile;
5315 char *filename;
5316 struct file_name_list *searchptr;
5318 struct import_file *i;
5319 int h;
5320 int hashval;
5321 struct stat sb;
5322 int fd;
5324 hashval = import_hash (filename);
5326 /* Attempt to find file in list of already included files */
5327 i = pfile->import_hash_table[hashval];
5329 while (i) {
5330 if (!strcmp (filename, i->name))
5331 return -2; /* return found */
5332 i = i->next;
5334 /* Open it and try a match on inode/dev */
5335 fd = open_include_file (pfile, filename, searchptr);
5336 if (fd < 0)
5337 return fd;
5338 fstat (fd, &sb);
5339 for (h = 0; h < IMPORT_HASH_SIZE; h++) {
5340 i = pfile->import_hash_table[h];
5341 while (i) {
5342 /* Compare the inode and the device.
5343 Supposedly on some systems the inode is not a scalar. */
5344 if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
5345 && i->dev == sb.st_dev) {
5346 close (fd);
5347 return -2; /* return found */
5349 i = i->next;
5352 return fd; /* Not found, return open file */
5355 /* Add the file FNAME, open on descriptor FD, to import_hash_table. */
5357 static void
5358 add_import (pfile, fd, fname)
5359 cpp_reader *pfile;
5360 int fd;
5361 char *fname;
5363 struct import_file *i;
5364 int hashval;
5365 struct stat sb;
5367 hashval = import_hash (fname);
5368 fstat (fd, &sb);
5369 i = (struct import_file *)xmalloc (sizeof (struct import_file));
5370 i->name = (char *)xmalloc (strlen (fname)+1);
5371 strcpy (i->name, fname);
5372 bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
5373 i->dev = sb.st_dev;
5374 i->next = pfile->import_hash_table[hashval];
5375 pfile->import_hash_table[hashval] = i;
5378 /* The file_name_map structure holds a mapping of file names for a
5379 particular directory. This mapping is read from the file named
5380 FILE_NAME_MAP_FILE in that directory. Such a file can be used to
5381 map filenames on a file system with severe filename restrictions,
5382 such as DOS. The format of the file name map file is just a series
5383 of lines with two tokens on each line. The first token is the name
5384 to map, and the second token is the actual name to use. */
5386 struct file_name_map
5388 struct file_name_map *map_next;
5389 char *map_from;
5390 char *map_to;
5393 #define FILE_NAME_MAP_FILE "header.gcc"
5395 /* Read a space delimited string of unlimited length from a stdio
5396 file. */
5398 static char *
5399 read_filename_string (ch, f)
5400 int ch;
5401 FILE *f;
5403 char *alloc, *set;
5404 int len;
5406 len = 20;
5407 set = alloc = xmalloc (len + 1);
5408 if (! is_space[ch])
5410 *set++ = ch;
5411 while ((ch = getc (f)) != EOF && ! is_space[ch])
5413 if (set - alloc == len)
5415 len *= 2;
5416 alloc = xrealloc (alloc, len + 1);
5417 set = alloc + len / 2;
5419 *set++ = ch;
5422 *set = '\0';
5423 ungetc (ch, f);
5424 return alloc;
5427 /* This structure holds a linked list of file name maps, one per directory. */
5429 struct file_name_map_list
5431 struct file_name_map_list *map_list_next;
5432 char *map_list_name;
5433 struct file_name_map *map_list_map;
5436 /* Read the file name map file for DIRNAME. */
5438 static struct file_name_map *
5439 read_name_map (pfile, dirname)
5440 cpp_reader *pfile;
5441 char *dirname;
5443 register struct file_name_map_list *map_list_ptr;
5444 char *name;
5445 FILE *f;
5447 for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
5448 map_list_ptr = map_list_ptr->map_list_next)
5449 if (! strcmp (map_list_ptr->map_list_name, dirname))
5450 return map_list_ptr->map_list_map;
5452 map_list_ptr = ((struct file_name_map_list *)
5453 xmalloc (sizeof (struct file_name_map_list)));
5454 map_list_ptr->map_list_name = savestring (dirname);
5455 map_list_ptr->map_list_map = NULL;
5457 name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
5458 strcpy (name, dirname);
5459 if (*dirname)
5460 strcat (name, "/");
5461 strcat (name, FILE_NAME_MAP_FILE);
5462 f = fopen (name, "r");
5463 if (!f)
5464 map_list_ptr->map_list_map = NULL;
5465 else
5467 int ch;
5468 int dirlen = strlen (dirname);
5470 while ((ch = getc (f)) != EOF)
5472 char *from, *to;
5473 struct file_name_map *ptr;
5475 if (is_space[ch])
5476 continue;
5477 from = read_filename_string (ch, f);
5478 while ((ch = getc (f)) != EOF && is_hor_space[ch])
5480 to = read_filename_string (ch, f);
5482 ptr = ((struct file_name_map *)
5483 xmalloc (sizeof (struct file_name_map)));
5484 ptr->map_from = from;
5486 /* Make the real filename absolute. */
5487 if (*to == '/')
5488 ptr->map_to = to;
5489 else
5491 ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
5492 strcpy (ptr->map_to, dirname);
5493 ptr->map_to[dirlen] = '/';
5494 strcpy (ptr->map_to + dirlen + 1, to);
5495 free (to);
5498 ptr->map_next = map_list_ptr->map_list_map;
5499 map_list_ptr->map_list_map = ptr;
5501 while ((ch = getc (f)) != '\n')
5502 if (ch == EOF)
5503 break;
5505 fclose (f);
5508 map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
5509 CPP_OPTIONS (pfile)->map_list = map_list_ptr;
5511 return map_list_ptr->map_list_map;
5514 /* Try to open include file FILENAME. SEARCHPTR is the directory
5515 being tried from the include file search path. This function maps
5516 filenames on file systems based on information read by
5517 read_name_map. */
5519 static int
5520 open_include_file (pfile, filename, searchptr)
5521 cpp_reader *pfile;
5522 char *filename;
5523 struct file_name_list *searchptr;
5525 register struct file_name_map *map;
5526 register char *from;
5527 char *p, *dir;
5529 if (searchptr && ! searchptr->got_name_map)
5531 searchptr->name_map = read_name_map (pfile,
5532 searchptr->fname
5533 ? searchptr->fname : ".");
5534 searchptr->got_name_map = 1;
5537 /* First check the mapping for the directory we are using. */
5538 if (searchptr && searchptr->name_map)
5540 from = filename;
5541 if (searchptr->fname)
5542 from += strlen (searchptr->fname) + 1;
5543 for (map = searchptr->name_map; map; map = map->map_next)
5545 if (! strcmp (map->map_from, from))
5547 /* Found a match. */
5548 return open (map->map_to, O_RDONLY, 0666);
5553 /* Try to find a mapping file for the particular directory we are
5554 looking in. Thus #include <sys/types.h> will look up sys/types.h
5555 in /usr/include/header.gcc and look up types.h in
5556 /usr/include/sys/header.gcc. */
5557 p = rindex (filename, '/');
5558 if (! p)
5559 p = filename;
5560 if (searchptr
5561 && searchptr->fname
5562 && strlen (searchptr->fname) == p - filename
5563 && ! strncmp (searchptr->fname, filename, p - filename))
5565 /* FILENAME is in SEARCHPTR, which we've already checked. */
5566 return open (filename, O_RDONLY, 0666);
5569 if (p == filename)
5571 dir = ".";
5572 from = filename;
5574 else
5576 dir = (char *) alloca (p - filename + 1);
5577 bcopy (filename, dir, p - filename);
5578 dir[p - filename] = '\0';
5579 from = p + 1;
5581 for (map = read_name_map (pfile, dir); map; map = map->map_next)
5582 if (! strcmp (map->map_from, from))
5583 return open (map->map_to, O_RDONLY, 0666);
5585 return open (filename, O_RDONLY, 0666);
5588 /* Process the contents of include file FNAME, already open on descriptor F,
5589 with output to OP.
5590 SYSTEM_HEADER_P is 1 if this file resides in any one of the known
5591 "system" include directories (as decided by the `is_system_include'
5592 function above).
5593 DIRPTR is the link in the dir path through which this file was found,
5594 or 0 if the file name was absolute or via the current directory.
5595 Return 1 on success, 0 on failure.
5597 The caller is responsible for the cpp_push_buffer. */
5599 static int
5600 finclude (pfile, f, fname, system_header_p, dirptr)
5601 cpp_reader *pfile;
5602 int f;
5603 char *fname;
5604 int system_header_p;
5605 struct file_name_list *dirptr;
5607 int st_mode;
5608 long st_size;
5609 long i;
5610 int length;
5611 cpp_buffer *fp; /* For input stack frame */
5612 int missing_newline = 0;
5614 if (file_size_and_mode (f, &st_mode, &st_size) < 0)
5616 cpp_perror_with_name (pfile, fname);
5617 close (f);
5618 cpp_pop_buffer (pfile);
5619 return 0;
5622 fp = CPP_BUFFER (pfile);
5623 fp->nominal_fname = fp->fname = fname;
5624 #if 0
5625 fp->length = 0;
5626 #endif
5627 fp->dir = dirptr;
5628 fp->system_header_p = system_header_p;
5629 fp->lineno = 1;
5630 fp->colno = 1;
5631 fp->cleanup = file_cleanup;
5633 if (S_ISREG (st_mode)) {
5634 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
5635 fp->alimit = fp->buf + st_size + 2;
5636 fp->cur = fp->buf;
5638 /* Read the file contents, knowing that st_size is an upper bound
5639 on the number of bytes we can read. */
5640 length = safe_read (f, fp->buf, st_size);
5641 fp->rlimit = fp->buf + length;
5642 if (length < 0) goto nope;
5644 else if (S_ISDIR (st_mode)) {
5645 cpp_error (pfile, "directory `%s' specified in #include", fname);
5646 close (f);
5647 return 0;
5648 } else {
5649 /* Cannot count its file size before reading.
5650 First read the entire file into heap and
5651 copy them into buffer on stack. */
5653 int bsize = 2000;
5655 st_size = 0;
5656 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
5658 for (;;) {
5659 i = safe_read (f, fp->buf + st_size, bsize - st_size);
5660 if (i < 0)
5661 goto nope; /* error! */
5662 st_size += i;
5663 if (st_size != bsize)
5664 break; /* End of file */
5665 bsize *= 2;
5666 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
5668 fp->cur = fp->buf;
5669 length = st_size;
5672 if ((length > 0 && fp->buf[length - 1] != '\n')
5673 /* Backslash-newline at end is not good enough. */
5674 || (length > 1 && fp->buf[length - 2] == '\\')) {
5675 fp->buf[length++] = '\n';
5676 #if 0
5677 missing_newline = 1;
5678 #endif
5680 fp->buf[length] = '\0';
5681 fp->rlimit = fp->buf + length;
5683 /* Close descriptor now, so nesting does not use lots of descriptors. */
5684 close (f);
5686 /* Must do this before calling trigraph_pcp, so that the correct file name
5687 will be printed in warning messages. */
5689 pfile->input_stack_listing_current = 0;
5691 #if 0
5692 if (!no_trigraphs)
5693 trigraph_pcp (fp);
5694 #endif
5696 #if 0
5697 rescan (op, 0);
5699 if (missing_newline)
5700 fp->lineno--;
5702 if (CPP_PEDANTIC (pfile) && missing_newline)
5703 pedwarn ("file does not end in newline");
5705 indepth--;
5706 input_file_stack_tick++;
5707 free (fp->buf);
5708 #endif
5709 return 1;
5711 nope:
5713 cpp_perror_with_name (pfile, fname);
5714 close (f);
5715 free (fp->buf);
5716 return 1;
5719 /* This is called after options have been processed.
5720 * Check options for consistency, and setup for processing input
5721 * from the file named FNAME. (Use standard input if FNAME==NULL.)
5722 * Return 1 on succes, 0 on failure.
5726 cpp_start_read (pfile, fname)
5727 cpp_reader *pfile;
5728 char *fname;
5730 struct cpp_options *opts = CPP_OPTIONS (pfile);
5731 struct cpp_pending *pend;
5732 char *p;
5733 int f;
5734 cpp_buffer *fp;
5736 /* The code looks at the defaults through this pointer, rather than through
5737 the constant structure above. This pointer gets changed if an environment
5738 variable specifies other defaults. */
5739 struct default_include *include_defaults = include_defaults_array;
5741 /* Add dirs from CPATH after dirs from -I. */
5742 /* There seems to be confusion about what CPATH should do,
5743 so for the moment it is not documented. */
5744 /* Some people say that CPATH should replace the standard include dirs,
5745 but that seems pointless: it comes before them, so it overrides them
5746 anyway. */
5747 p = (char *) getenv ("CPATH");
5748 if (p != 0 && ! opts->no_standard_includes)
5749 path_include (pfile, p);
5751 /* Now that dollars_in_ident is known, initialize is_idchar. */
5752 initialize_char_syntax (opts);
5754 /* Do partial setup of input buffer for the sake of generating
5755 early #line directives (when -g is in effect). */
5756 fp = cpp_push_buffer (pfile, NULL, 0);
5757 if (!fp)
5758 return 0;
5759 if (opts->in_fname == NULL)
5760 opts->in_fname = "";
5761 fp->nominal_fname = fp->fname = opts->in_fname;
5762 fp->lineno = 0;
5764 /* Install __LINE__, etc. Must follow initialize_char_syntax
5765 and option processing. */
5766 initialize_builtins (pfile);
5768 /* Do standard #defines and assertions
5769 that identify system and machine type. */
5771 if (!opts->inhibit_predefs) {
5772 char *p = (char *) alloca (strlen (predefs) + 1);
5773 strcpy (p, predefs);
5774 while (*p) {
5775 char *q;
5776 while (*p == ' ' || *p == '\t')
5777 p++;
5778 /* Handle -D options. */
5779 if (p[0] == '-' && p[1] == 'D') {
5780 q = &p[2];
5781 while (*p && *p != ' ' && *p != '\t')
5782 p++;
5783 if (*p != 0)
5784 *p++= 0;
5785 if (opts->debug_output)
5786 output_line_command (pfile, 0, same_file);
5787 cpp_define (pfile, q);
5788 while (*p == ' ' || *p == '\t')
5789 p++;
5790 } else if (p[0] == '-' && p[1] == 'A') {
5791 /* Handle -A options (assertions). */
5792 char *assertion;
5793 char *past_name;
5794 char *value;
5795 char *past_value;
5796 char *termination;
5797 int save_char;
5799 assertion = &p[2];
5800 past_name = assertion;
5801 /* Locate end of name. */
5802 while (*past_name && *past_name != ' '
5803 && *past_name != '\t' && *past_name != '(')
5804 past_name++;
5805 /* Locate `(' at start of value. */
5806 value = past_name;
5807 while (*value && (*value == ' ' || *value == '\t'))
5808 value++;
5809 if (*value++ != '(')
5810 abort ();
5811 while (*value && (*value == ' ' || *value == '\t'))
5812 value++;
5813 past_value = value;
5814 /* Locate end of value. */
5815 while (*past_value && *past_value != ' '
5816 && *past_value != '\t' && *past_value != ')')
5817 past_value++;
5818 termination = past_value;
5819 while (*termination && (*termination == ' ' || *termination == '\t'))
5820 termination++;
5821 if (*termination++ != ')')
5822 abort ();
5823 if (*termination && *termination != ' ' && *termination != '\t')
5824 abort ();
5825 /* Temporarily null-terminate the value. */
5826 save_char = *termination;
5827 *termination = '\0';
5828 /* Install the assertion. */
5829 make_assertion (pfile, "-A", assertion);
5830 *termination = (char) save_char;
5831 p = termination;
5832 while (*p == ' ' || *p == '\t')
5833 p++;
5834 } else {
5835 abort ();
5840 /* Now handle the command line options. */
5842 /* Do -U's, -D's and -A's in the order they were seen. */
5843 /* First reverse the list. */
5844 opts->pending = nreverse_pending (opts->pending);
5846 for (pend = opts->pending; pend; pend = pend->next)
5848 if (pend->cmd != NULL && pend->cmd[0] == '-')
5850 switch (pend->cmd[1])
5852 case 'U':
5853 if (opts->debug_output)
5854 output_line_command (pfile, 0, same_file);
5855 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5856 break;
5857 case 'D':
5858 if (opts->debug_output)
5859 output_line_command (pfile, 0, same_file);
5860 cpp_define (pfile, pend->arg);
5861 break;
5862 case 'A':
5863 make_assertion (pfile, "-A", pend->arg);
5864 break;
5869 opts->done_initializing = 1;
5871 { /* Read the appropriate environment variable and if it exists
5872 replace include_defaults with the listed path. */
5873 char *epath = 0;
5874 switch ((opts->objc << 1) + opts->cplusplus)
5876 case 0:
5877 epath = getenv ("C_INCLUDE_PATH");
5878 break;
5879 case 1:
5880 epath = getenv ("CPLUS_INCLUDE_PATH");
5881 break;
5882 case 2:
5883 epath = getenv ("OBJC_INCLUDE_PATH");
5884 break;
5885 case 3:
5886 epath = getenv ("OBJCPLUS_INCLUDE_PATH");
5887 break;
5889 /* If the environment var for this language is set,
5890 add to the default list of include directories. */
5891 if (epath) {
5892 char *nstore = (char *) alloca (strlen (epath) + 2);
5893 int num_dirs;
5894 char *startp, *endp;
5896 for (num_dirs = 1, startp = epath; *startp; startp++)
5897 if (*startp == PATH_SEPARATOR)
5898 num_dirs++;
5899 include_defaults
5900 = (struct default_include *) xmalloc ((num_dirs
5901 * sizeof (struct default_include))
5902 + sizeof (include_defaults_array));
5903 startp = endp = epath;
5904 num_dirs = 0;
5905 while (1) {
5906 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5907 if ((*endp == PATH_SEPARATOR)
5908 || *endp == 0) {
5909 strncpy (nstore, startp, endp-startp);
5910 if (endp == startp)
5911 strcpy (nstore, ".");
5912 else
5913 nstore[endp-startp] = '\0';
5915 include_defaults[num_dirs].fname = savestring (nstore);
5916 include_defaults[num_dirs].component = 0;
5917 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5918 include_defaults[num_dirs].cxx_aware = 1;
5919 num_dirs++;
5920 if (*endp == '\0')
5921 break;
5922 endp = startp = endp + 1;
5923 } else
5924 endp++;
5926 /* Put the usual defaults back in at the end. */
5927 bcopy ((char *) include_defaults_array,
5928 (char *) &include_defaults[num_dirs],
5929 sizeof (include_defaults_array));
5933 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5934 opts->first_system_include = opts->before_system;
5936 /* Unless -fnostdinc,
5937 tack on the standard include file dirs to the specified list */
5938 if (!opts->no_standard_includes) {
5939 struct default_include *p = include_defaults;
5940 char *specd_prefix = opts->include_prefix;
5941 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5942 int default_len = 0;
5943 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5944 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5945 default_len = strlen (default_prefix) - 7;
5946 default_prefix[default_len] = 0;
5948 /* Search "translated" versions of GNU directories.
5949 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5950 if (specd_prefix != 0 && default_len != 0)
5951 for (p = include_defaults; p->fname; p++) {
5952 /* Some standard dirs are only for C++. */
5953 if (!p->cplusplus
5954 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5955 /* Does this dir start with the prefix? */
5956 if (!strncmp (p->fname, default_prefix, default_len)) {
5957 /* Yes; change prefix and add to search list. */
5958 struct file_name_list *new
5959 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5960 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5961 char *str = (char *) xmalloc (this_len + 1);
5962 strcpy (str, specd_prefix);
5963 strcat (str, p->fname + default_len);
5964 new->fname = str;
5965 new->control_macro = 0;
5966 new->c_system_include_path = !p->cxx_aware;
5967 new->got_name_map = 0;
5968 append_include_chain (pfile, new, new);
5969 if (opts->first_system_include == 0)
5970 opts->first_system_include = new;
5974 /* Search ordinary names for GNU include directories. */
5975 for (p = include_defaults; p->fname; p++) {
5976 /* Some standard dirs are only for C++. */
5977 if (!p->cplusplus
5978 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5979 struct file_name_list *new
5980 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5981 new->control_macro = 0;
5982 new->c_system_include_path = !p->cxx_aware;
5983 new->fname = update_path (p->fname, p->component);
5984 new->got_name_map = 0;
5985 append_include_chain (pfile, new, new);
5986 if (opts->first_system_include == 0)
5987 opts->first_system_include = new;
5992 /* Tack the after_include chain at the end of the include chain. */
5993 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5994 if (opts->first_system_include == 0)
5995 opts->first_system_include = opts->after_include;
5997 /* With -v, print the list of dirs to search. */
5998 if (opts->verbose) {
5999 struct file_name_list *p;
6000 fprintf (stderr, "#include \"...\" search starts here:\n");
6001 for (p = opts->include; p; p = p->next) {
6002 if (p == opts->first_bracket_include)
6003 fprintf (stderr, "#include <...> search starts here:\n");
6004 fprintf (stderr, " %s\n", p->fname);
6006 fprintf (stderr, "End of search list.\n");
6009 /* Scan the -imacros files before the main input.
6010 Much like #including them, but with no_output set
6011 so that only their macro definitions matter. */
6013 opts->no_output++; pfile->no_record_file++;
6014 for (pend = opts->pending; pend; pend = pend->next)
6016 if (pend->cmd != NULL && strcmp (pend->cmd, "-imacros") == 0)
6018 int fd = open (pend->arg, O_RDONLY, 0666);
6019 if (fd < 0)
6021 cpp_perror_with_name (pfile, pend->arg);
6022 return 0;
6024 if (!cpp_push_buffer (pfile, NULL, 0))
6025 return 0;
6026 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6027 cpp_scan_buffer (pfile);
6030 opts->no_output--; pfile->no_record_file--;
6032 /* Copy the entire contents of the main input file into
6033 the stacked input buffer previously allocated for it. */
6034 if (fname == NULL || *fname == 0) {
6035 fname = "";
6036 f = 0;
6037 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
6038 cpp_pfatal_with_name (pfile, fname);
6040 /* -MG doesn't select the form of output and must be specified with one of
6041 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
6042 inhibit compilation. */
6043 if (opts->print_deps_missing_files
6044 && (opts->print_deps == 0 || !opts->no_output))
6046 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
6047 return 0;
6050 /* Either of two environment variables can specify output of deps.
6051 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
6052 where OUTPUT_FILE is the file to write deps info to
6053 and DEPS_TARGET is the target to mention in the deps. */
6055 if (opts->print_deps == 0
6056 && (getenv ("SUNPRO_DEPENDENCIES") != 0
6057 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
6058 char *spec = getenv ("DEPENDENCIES_OUTPUT");
6059 char *s;
6060 char *output_file;
6062 if (spec == 0)
6064 spec = getenv ("SUNPRO_DEPENDENCIES");
6065 opts->print_deps = 2;
6067 else
6068 opts->print_deps = 1;
6070 s = spec;
6071 /* Find the space before the DEPS_TARGET, if there is one. */
6072 /* This should use index. (mrs) */
6073 while (*s != 0 && *s != ' ') s++;
6074 if (*s != 0)
6076 opts->deps_target = s + 1;
6077 output_file = (char *) xmalloc (s - spec + 1);
6078 bcopy (spec, output_file, s - spec);
6079 output_file[s - spec] = 0;
6081 else
6083 opts->deps_target = 0;
6084 output_file = spec;
6087 opts->deps_file = output_file;
6088 opts->print_deps_append = 1;
6091 /* For -M, print the expected object file name
6092 as the target of this Make-rule. */
6093 if (opts->print_deps)
6095 pfile->deps_allocated_size = 200;
6096 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
6097 pfile->deps_buffer[0] = 0;
6098 pfile->deps_size = 0;
6099 pfile->deps_column = 0;
6101 if (opts->deps_target)
6102 deps_output (pfile, opts->deps_target, ':');
6103 else if (*opts->in_fname == 0)
6104 deps_output (pfile, "-", ':');
6105 else
6107 char *p, *q, *r;
6108 int len, x;
6109 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
6110 ".cc", ".cxx", ".cpp", ".cp",
6111 ".c++", 0
6114 /* Discard all directory prefixes from filename. */
6115 if ((q = rindex (opts->in_fname, '/')) != NULL
6116 #ifdef DIR_SEPARATOR
6117 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
6118 #endif
6120 ++q;
6121 else
6122 q = opts->in_fname;
6124 /* Copy remainder to mungable area. */
6125 p = (char *) alloca (strlen(q) + 8);
6126 strcpy (p, q);
6128 /* Output P, but remove known suffixes. */
6129 len = strlen (p);
6130 q = p + len;
6131 /* Point to the filename suffix. */
6132 r = rindex (p, '.');
6133 /* Compare against the known suffixes. */
6134 x = 0;
6135 while (known_suffixes[x] != 0)
6137 if (strncmp (known_suffixes[x], r, q - r) == 0)
6139 /* Make q point to the bit we're going to overwrite
6140 with an object suffix. */
6141 q = r;
6142 break;
6144 x++;
6147 /* Supply our own suffix. */
6148 #ifndef VMS
6149 strcpy (q, ".o");
6150 #else
6151 strcpy (q, ".obj");
6152 #endif
6154 deps_output (pfile, p, ':');
6155 deps_output (pfile, opts->in_fname, ' ');
6159 #if 0
6160 /* Make sure data ends with a newline. And put a null after it. */
6162 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
6163 /* Backslash-newline at end is not good enough. */
6164 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
6165 fp->buf[fp->length++] = '\n';
6166 missing_newline = 1;
6168 fp->buf[fp->length] = '\0';
6170 /* Unless inhibited, convert trigraphs in the input. */
6172 if (!no_trigraphs)
6173 trigraph_pcp (fp);
6174 #endif
6176 /* Scan the -include files before the main input.
6177 We push these in reverse order, so that the first one is handled first. */
6179 pfile->no_record_file++;
6180 opts->pending = nreverse_pending (opts->pending);
6181 for (pend = opts->pending; pend; pend = pend->next)
6183 if (pend->cmd != NULL && strcmp (pend->cmd, "-include") == 0)
6185 int fd = open (pend->arg, O_RDONLY, 0666);
6186 if (fd < 0)
6188 cpp_perror_with_name (pfile, pend->arg);
6189 return 0;
6191 if (!cpp_push_buffer (pfile, NULL, 0))
6192 return 0;
6193 finclude (pfile, fd, pend->arg, 0, NULL_PTR);
6196 pfile->no_record_file--;
6198 /* Free the pending list. */
6199 for (pend = opts->pending; pend; )
6201 struct cpp_pending *next = pend->next;
6202 free (pend);
6203 pend = next;
6205 opts->pending = NULL;
6207 #if 0
6208 /* Scan the input, processing macros and directives. */
6210 rescan (&outbuf, 0);
6212 if (missing_newline)
6213 fp->lineno--;
6215 if (CPP_PEDANTIC (pfile) && missing_newline)
6216 pedwarn ("file does not end in newline");
6218 #endif
6219 if (finclude (pfile, f, fname, 0, NULL_PTR))
6220 output_line_command (pfile, 0, same_file);
6221 return 1;
6224 void
6225 cpp_reader_init (pfile)
6226 cpp_reader *pfile;
6228 bzero ((char *) pfile, sizeof (cpp_reader));
6229 pfile->get_token = cpp_get_token;
6231 pfile->token_buffer_size = 200;
6232 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
6233 CPP_SET_WRITTEN (pfile, 0);
6235 pfile->system_include_depth = 0;
6236 pfile->dont_repeat_files = 0;
6237 pfile->all_include_files = 0;
6238 pfile->max_include_len = 0;
6239 pfile->timebuf = NULL;
6240 pfile->only_seen_white = 1;
6241 pfile->buffer = CPP_NULL_BUFFER(pfile);
6244 static struct cpp_pending *
6245 nreverse_pending (list)
6246 struct cpp_pending *list;
6249 register struct cpp_pending *prev = 0, *next, *pend;
6250 for (pend = list; pend; pend = next)
6252 next = pend->next;
6253 pend->next = prev;
6254 prev = pend;
6256 return prev;
6259 static void
6260 push_pending (pfile, cmd, arg)
6261 cpp_reader *pfile;
6262 char *cmd;
6263 char *arg;
6265 struct cpp_pending *pend
6266 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
6267 pend->cmd = cmd;
6268 pend->arg = arg;
6269 pend->next = CPP_OPTIONS (pfile)->pending;
6270 CPP_OPTIONS (pfile)->pending = pend;
6273 /* Handle command-line options in (argc, argv).
6274 Can be called multiple times, to handle multiple sets of options.
6275 Returns if an unrecognized option is seen.
6276 Returns number of handled arguments. */
6279 cpp_handle_options (pfile, argc, argv)
6280 cpp_reader *pfile;
6281 int argc;
6282 char **argv;
6284 int i;
6285 struct cpp_options *opts = CPP_OPTIONS (pfile);
6286 for (i = 0; i < argc; i++) {
6287 if (argv[i][0] != '-') {
6288 if (opts->out_fname != NULL)
6290 cpp_fatal (pfile, "Usage: %s [switches] input output", argv[0]);
6291 return argc;
6293 else if (opts->in_fname != NULL)
6294 opts->out_fname = argv[i];
6295 else
6296 opts->in_fname = argv[i];
6297 } else {
6298 switch (argv[i][1]) {
6300 missing_filename:
6301 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
6302 return argc;
6303 missing_dirname:
6304 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
6305 return argc;
6307 case 'i':
6308 if (!strcmp (argv[i], "-include")
6309 || !strcmp (argv[i], "-imacros")) {
6310 if (i + 1 == argc)
6311 goto missing_filename;
6312 else
6313 push_pending (pfile, argv[i], argv[i+1]), i++;
6315 if (!strcmp (argv[i], "-iprefix")) {
6316 if (i + 1 == argc)
6317 goto missing_filename;
6318 else
6319 opts->include_prefix = argv[++i];
6321 if (!strcmp (argv[i], "-ifoutput")) {
6322 opts->output_conditionals = 1;
6324 if (!strcmp (argv[i], "-isystem")) {
6325 struct file_name_list *dirtmp;
6327 if (i + 1 == argc)
6328 goto missing_filename;
6330 dirtmp = (struct file_name_list *)
6331 xmalloc (sizeof (struct file_name_list));
6332 dirtmp->next = 0;
6333 dirtmp->control_macro = 0;
6334 dirtmp->c_system_include_path = 1;
6335 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
6336 strcpy (dirtmp->fname, argv[++i]);
6337 dirtmp->got_name_map = 0;
6339 if (opts->before_system == 0)
6340 opts->before_system = dirtmp;
6341 else
6342 opts->last_before_system->next = dirtmp;
6343 opts->last_before_system = dirtmp; /* Tail follows the last one */
6345 /* Add directory to end of path for includes,
6346 with the default prefix at the front of its name. */
6347 if (!strcmp (argv[i], "-iwithprefix")) {
6348 struct file_name_list *dirtmp;
6349 char *prefix;
6351 if (opts->include_prefix != 0)
6352 prefix = opts->include_prefix;
6353 else {
6354 prefix = savestring (GCC_INCLUDE_DIR);
6355 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6356 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6357 prefix[strlen (prefix) - 7] = 0;
6360 dirtmp = (struct file_name_list *)
6361 xmalloc (sizeof (struct file_name_list));
6362 dirtmp->next = 0; /* New one goes on the end */
6363 dirtmp->control_macro = 0;
6364 dirtmp->c_system_include_path = 0;
6365 if (i + 1 == argc)
6366 goto missing_dirname;
6368 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6369 + strlen (prefix) + 1);
6370 strcpy (dirtmp->fname, prefix);
6371 strcat (dirtmp->fname, argv[++i]);
6372 dirtmp->got_name_map = 0;
6374 if (opts->after_include == 0)
6375 opts->after_include = dirtmp;
6376 else
6377 opts->last_after_include->next = dirtmp;
6378 opts->last_after_include = dirtmp; /* Tail follows the last one */
6380 /* Add directory to main path for includes,
6381 with the default prefix at the front of its name. */
6382 if (!strcmp (argv[i], "-iwithprefixbefore")) {
6383 struct file_name_list *dirtmp;
6384 char *prefix;
6386 if (opts->include_prefix != 0)
6387 prefix = opts->include_prefix;
6388 else {
6389 prefix = savestring (GCC_INCLUDE_DIR);
6390 /* Remove the `include' from /usr/local/lib/gcc.../include. */
6391 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
6392 prefix[strlen (prefix) - 7] = 0;
6395 dirtmp = (struct file_name_list *)
6396 xmalloc (sizeof (struct file_name_list));
6397 dirtmp->next = 0; /* New one goes on the end */
6398 dirtmp->control_macro = 0;
6399 dirtmp->c_system_include_path = 0;
6400 if (i + 1 == argc)
6401 goto missing_dirname;
6403 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
6404 + strlen (prefix) + 1);
6405 strcpy (dirtmp->fname, prefix);
6406 strcat (dirtmp->fname, argv[++i]);
6407 dirtmp->got_name_map = 0;
6409 append_include_chain (pfile, dirtmp, dirtmp);
6411 /* Add directory to end of path for includes. */
6412 if (!strcmp (argv[i], "-idirafter")) {
6413 struct file_name_list *dirtmp;
6415 dirtmp = (struct file_name_list *)
6416 xmalloc (sizeof (struct file_name_list));
6417 dirtmp->next = 0; /* New one goes on the end */
6418 dirtmp->control_macro = 0;
6419 dirtmp->c_system_include_path = 0;
6420 if (i + 1 == argc)
6421 goto missing_dirname;
6422 else
6423 dirtmp->fname = argv[++i];
6424 dirtmp->got_name_map = 0;
6426 if (opts->after_include == 0)
6427 opts->after_include = dirtmp;
6428 else
6429 opts->last_after_include->next = dirtmp;
6430 opts->last_after_include = dirtmp; /* Tail follows the last one */
6432 break;
6434 case 'o':
6435 if (opts->out_fname != NULL)
6437 cpp_fatal (pfile, "Output filename specified twice");
6438 return argc;
6440 if (i + 1 == argc)
6441 goto missing_filename;
6442 opts->out_fname = argv[++i];
6443 if (!strcmp (opts->out_fname, "-"))
6444 opts->out_fname = "";
6445 break;
6447 case 'p':
6448 if (!strcmp (argv[i], "-pedantic"))
6449 CPP_PEDANTIC (pfile) = 1;
6450 else if (!strcmp (argv[i], "-pedantic-errors")) {
6451 CPP_PEDANTIC (pfile) = 1;
6452 opts->pedantic_errors = 1;
6454 #if 0
6455 else if (!strcmp (argv[i], "-pcp")) {
6456 char *pcp_fname = argv[++i];
6457 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
6458 ? fopen (pcp_fname, "w")
6459 : fdopen (dup (fileno (stdout)), "w"));
6460 if (pcp_outfile == 0)
6461 cpp_pfatal_with_name (pfile, pcp_fname);
6462 no_precomp = 1;
6464 #endif
6465 break;
6467 case 't':
6468 if (!strcmp (argv[i], "-traditional")) {
6469 opts->traditional = 1;
6470 } else if (!strcmp (argv[i], "-trigraphs")) {
6471 if (!opts->chill)
6472 opts->no_trigraphs = 0;
6474 break;
6476 case 'l':
6477 if (! strcmp (argv[i], "-lang-c"))
6478 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6479 opts->objc = 0;
6480 if (! strcmp (argv[i], "-lang-c89"))
6481 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
6482 opts->objc = 0;
6483 if (! strcmp (argv[i], "-lang-c++"))
6484 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6485 opts->objc = 0;
6486 if (! strcmp (argv[i], "-lang-objc"))
6487 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
6488 opts->objc = 1;
6489 if (! strcmp (argv[i], "-lang-objc++"))
6490 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
6491 opts->objc = 1;
6492 if (! strcmp (argv[i], "-lang-asm"))
6493 opts->lang_asm = 1;
6494 if (! strcmp (argv[i], "-lint"))
6495 opts->for_lint = 1;
6496 if (! strcmp (argv[i], "-lang-chill"))
6497 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
6498 opts->traditional = 1, opts->no_trigraphs = 1;
6499 break;
6501 case '+':
6502 opts->cplusplus = 1, opts->cplusplus_comments = 1;
6503 break;
6505 case 'w':
6506 opts->inhibit_warnings = 1;
6507 break;
6509 case 'W':
6510 if (!strcmp (argv[i], "-Wtrigraphs"))
6511 opts->warn_trigraphs = 1;
6512 else if (!strcmp (argv[i], "-Wno-trigraphs"))
6513 opts->warn_trigraphs = 0;
6514 else if (!strcmp (argv[i], "-Wcomment"))
6515 opts->warn_comments = 1;
6516 else if (!strcmp (argv[i], "-Wno-comment"))
6517 opts->warn_comments = 0;
6518 else if (!strcmp (argv[i], "-Wcomments"))
6519 opts->warn_comments = 1;
6520 else if (!strcmp (argv[i], "-Wno-comments"))
6521 opts->warn_comments = 0;
6522 else if (!strcmp (argv[i], "-Wtraditional"))
6523 opts->warn_stringify = 1;
6524 else if (!strcmp (argv[i], "-Wno-traditional"))
6525 opts->warn_stringify = 0;
6526 else if (!strcmp (argv[i], "-Wundef"))
6527 opts->warn_undef = 1;
6528 else if (!strcmp (argv[i], "-Wno-undef"))
6529 opts->warn_undef = 0;
6530 else if (!strcmp (argv[i], "-Wimport"))
6531 opts->warn_import = 1;
6532 else if (!strcmp (argv[i], "-Wno-import"))
6533 opts->warn_import = 0;
6534 else if (!strcmp (argv[i], "-Werror"))
6535 opts->warnings_are_errors = 1;
6536 else if (!strcmp (argv[i], "-Wno-error"))
6537 opts->warnings_are_errors = 0;
6538 else if (!strcmp (argv[i], "-Wall"))
6540 opts->warn_trigraphs = 1;
6541 opts->warn_comments = 1;
6543 break;
6545 case 'M':
6546 /* The style of the choices here is a bit mixed.
6547 The chosen scheme is a hybrid of keeping all options in one string
6548 and specifying each option in a separate argument:
6549 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
6550 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
6551 -M[M][G][D file]. This is awkward to handle in specs, and is not
6552 as extensible. */
6553 /* ??? -MG must be specified in addition to one of -M or -MM.
6554 This can be relaxed in the future without breaking anything.
6555 The converse isn't true. */
6557 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
6558 if (!strcmp (argv[i], "-MG"))
6560 opts->print_deps_missing_files = 1;
6561 break;
6563 if (!strcmp (argv[i], "-M"))
6564 opts->print_deps = 2;
6565 else if (!strcmp (argv[i], "-MM"))
6566 opts->print_deps = 1;
6567 else if (!strcmp (argv[i], "-MD"))
6568 opts->print_deps = 2;
6569 else if (!strcmp (argv[i], "-MMD"))
6570 opts->print_deps = 1;
6571 /* For -MD and -MMD options, write deps on file named by next arg. */
6572 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
6574 if (i+1 == argc)
6575 goto missing_filename;
6576 opts->deps_file = argv[++i];
6578 else
6580 /* For -M and -MM, write deps on standard output
6581 and suppress the usual output. */
6582 opts->no_output = 1;
6584 break;
6586 case 'd':
6588 char *p = argv[i] + 2;
6589 char c;
6590 while ((c = *p++) != 0) {
6591 /* Arg to -d specifies what parts of macros to dump */
6592 switch (c) {
6593 case 'M':
6594 opts->dump_macros = dump_only;
6595 opts->no_output = 1;
6596 break;
6597 case 'N':
6598 opts->dump_macros = dump_names;
6599 break;
6600 case 'D':
6601 opts->dump_macros = dump_definitions;
6602 break;
6603 case 'I':
6604 opts->dump_includes = 1;
6605 break;
6609 break;
6611 case 'g':
6612 if (argv[i][2] == '3')
6613 opts->debug_output = 1;
6614 break;
6616 case 'v':
6617 fprintf (stderr, "GNU CPP version %s", version_string);
6618 #ifdef TARGET_VERSION
6619 TARGET_VERSION;
6620 #endif
6621 fprintf (stderr, "\n");
6622 opts->verbose = 1;
6623 break;
6625 case 'H':
6626 opts->print_include_names = 1;
6627 break;
6629 case 'D':
6630 if (argv[i][2] != 0)
6631 push_pending (pfile, "-D", argv[i] + 2);
6632 else if (i + 1 == argc)
6634 cpp_fatal (pfile, "Macro name missing after -D option");
6635 return argc;
6637 else
6638 i++, push_pending (pfile, "-D", argv[i]);
6639 break;
6641 case 'A':
6643 char *p;
6645 if (argv[i][2] != 0)
6646 p = argv[i] + 2;
6647 else if (i + 1 == argc)
6649 cpp_fatal (pfile, "Assertion missing after -A option");
6650 return argc;
6652 else
6653 p = argv[++i];
6655 if (!strcmp (p, "-")) {
6656 struct cpp_pending **ptr;
6657 /* -A- eliminates all predefined macros and assertions.
6658 Let's include also any that were specified earlier
6659 on the command line. That way we can get rid of any
6660 that were passed automatically in from GCC. */
6661 int j;
6662 opts->inhibit_predefs = 1;
6663 for (ptr = &opts->pending; *ptr != NULL; )
6665 struct cpp_pending *pend = *ptr;
6666 if (pend->cmd && pend->cmd[0] == '-'
6667 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6669 *ptr = pend->next;
6670 free (pend);
6672 else
6673 ptr = &pend->next;
6675 } else {
6676 push_pending (pfile, "-A", p);
6679 break;
6681 case 'U': /* JF #undef something */
6682 if (argv[i][2] != 0)
6683 push_pending (pfile, "-U", argv[i] + 2);
6684 else if (i + 1 == argc)
6686 cpp_fatal (pfile, "Macro name missing after -U option", NULL);
6687 return argc;
6689 else
6690 push_pending (pfile, "-U", argv[i+1]), i++;
6691 break;
6693 case 'C':
6694 opts->put_out_comments = 1;
6695 break;
6697 case 'E': /* -E comes from cc -E; ignore it. */
6698 break;
6700 case 'P':
6701 opts->no_line_commands = 1;
6702 break;
6704 case '$': /* Don't include $ in identifiers. */
6705 opts->dollars_in_ident = 0;
6706 break;
6708 case 'I': /* Add directory to path for includes. */
6710 struct file_name_list *dirtmp;
6712 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6713 && !strcmp (argv[i] + 2, "-")) {
6714 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6715 /* Don't use any preceding -I directories for #include <...>. */
6716 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6718 else {
6719 dirtmp = (struct file_name_list *)
6720 xmalloc (sizeof (struct file_name_list));
6721 dirtmp->next = 0; /* New one goes on the end */
6722 dirtmp->control_macro = 0;
6723 dirtmp->c_system_include_path = 0;
6724 if (argv[i][2] != 0)
6725 dirtmp->fname = argv[i] + 2;
6726 else if (i + 1 == argc)
6727 goto missing_dirname;
6728 else
6729 dirtmp->fname = argv[++i];
6730 dirtmp->got_name_map = 0;
6731 append_include_chain (pfile, dirtmp, dirtmp);
6734 break;
6736 case 'n':
6737 if (!strcmp (argv[i], "-nostdinc"))
6738 /* -nostdinc causes no default include directories.
6739 You must specify all include-file directories with -I. */
6740 opts->no_standard_includes = 1;
6741 else if (!strcmp (argv[i], "-nostdinc++"))
6742 /* -nostdinc++ causes no default C++-specific include directories. */
6743 opts->no_standard_cplusplus_includes = 1;
6744 #if 0
6745 else if (!strcmp (argv[i], "-noprecomp"))
6746 no_precomp = 1;
6747 #endif
6748 break;
6750 case 'u':
6751 /* Sun compiler passes undocumented switch "-undef".
6752 Let's assume it means to inhibit the predefined symbols. */
6753 opts->inhibit_predefs = 1;
6754 break;
6756 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6757 if (opts->in_fname == NULL) {
6758 opts->in_fname = "";
6759 break;
6760 } else if (opts->out_fname == NULL) {
6761 opts->out_fname = "";
6762 break;
6763 } /* else fall through into error */
6765 default:
6766 return i;
6770 return i;
6773 void
6774 cpp_finish (pfile)
6775 cpp_reader *pfile;
6777 struct cpp_options *opts = CPP_OPTIONS (pfile);
6779 if (opts->print_deps)
6781 /* Stream on which to print the dependency information. */
6782 FILE *deps_stream;
6784 /* Don't actually write the deps file if compilation has failed. */
6785 if (pfile->errors == 0)
6787 char *deps_mode = opts->print_deps_append ? "a" : "w";
6788 if (opts->deps_file == 0)
6789 deps_stream = stdout;
6790 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6791 cpp_pfatal_with_name (pfile, opts->deps_file);
6792 fputs (pfile->deps_buffer, deps_stream);
6793 putc ('\n', deps_stream);
6794 if (opts->deps_file)
6796 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6797 cpp_fatal (pfile, "I/O error on output");
6803 /* Free resources used by PFILE.
6804 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6806 void
6807 cpp_cleanup (pfile)
6808 cpp_reader *pfile;
6810 int i;
6811 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6812 cpp_pop_buffer (pfile);
6814 if (pfile->token_buffer)
6816 free (pfile->token_buffer);
6817 pfile->token_buffer = NULL;
6820 if (pfile->deps_buffer)
6822 free (pfile->deps_buffer);
6823 pfile->deps_buffer = NULL;
6824 pfile->deps_allocated_size = 0;
6827 while (pfile->if_stack)
6829 IF_STACK_FRAME *temp = pfile->if_stack;
6830 pfile->if_stack = temp->next;
6831 free (temp);
6834 while (pfile->dont_repeat_files)
6836 struct file_name_list *temp = pfile->dont_repeat_files;
6837 pfile->dont_repeat_files = temp->next;
6838 free (temp->fname);
6839 free (temp);
6842 while (pfile->all_include_files)
6844 struct file_name_list *temp = pfile->all_include_files;
6845 pfile->all_include_files = temp->next;
6846 free (temp->fname);
6847 free (temp);
6850 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6852 register struct import_file *imp = pfile->import_hash_table[i];
6853 while (imp)
6855 struct import_file *next = imp->next;
6856 free (imp->name);
6857 free (imp);
6858 imp = next;
6860 pfile->import_hash_table[i] = 0;
6863 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6865 while (pfile->assertion_hashtab[i])
6866 delete_assertion (pfile->assertion_hashtab[i]);
6869 cpp_hash_cleanup (pfile);
6872 static int
6873 do_assert (pfile, keyword, buf, limit)
6874 cpp_reader *pfile;
6875 struct directive *keyword;
6876 U_CHAR *buf, *limit;
6878 long symstart; /* remember where symbol name starts */
6879 int c;
6880 int sym_length; /* and how long it is */
6881 struct arglist *tokens = NULL;
6883 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6884 && !CPP_BUFFER (pfile)->system_header_p)
6885 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6887 cpp_skip_hspace (pfile);
6888 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6889 parse_name (pfile, GETC());
6890 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6891 "assertion");
6893 cpp_skip_hspace (pfile);
6894 if (PEEKC() != '(') {
6895 cpp_error (pfile, "missing token-sequence in `#assert'");
6896 goto error;
6900 int error_flag = 0;
6901 tokens = read_token_list (pfile, &error_flag);
6902 if (error_flag)
6903 goto error;
6904 if (tokens == 0) {
6905 cpp_error (pfile, "empty token-sequence in `#assert'");
6906 goto error;
6908 cpp_skip_hspace (pfile);
6909 c = PEEKC ();
6910 if (c != EOF && c != '\n')
6911 cpp_pedwarn (pfile, "junk at end of `#assert'");
6912 skip_rest_of_line (pfile);
6915 /* If this name isn't already an assertion name, make it one.
6916 Error if it was already in use in some other way. */
6919 ASSERTION_HASHNODE *hp;
6920 U_CHAR *symname = pfile->token_buffer + symstart;
6921 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6922 struct tokenlist_list *value
6923 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6925 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6926 if (hp == NULL) {
6927 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6928 cpp_error (pfile, "`defined' redefined as assertion");
6929 hp = assertion_install (pfile, symname, sym_length, hashcode);
6932 /* Add the spec'd token-sequence to the list of such. */
6933 value->tokens = tokens;
6934 value->next = hp->value;
6935 hp->value = value;
6937 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6938 return 0;
6939 error:
6940 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6941 skip_rest_of_line (pfile);
6942 return 1;
6945 static int
6946 do_unassert (pfile, keyword, buf, limit)
6947 cpp_reader *pfile;
6948 struct directive *keyword;
6949 U_CHAR *buf, *limit;
6951 long symstart; /* remember where symbol name starts */
6952 int sym_length; /* and how long it is */
6953 int c;
6955 struct arglist *tokens = NULL;
6956 int tokens_specified = 0;
6958 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6959 && !CPP_BUFFER (pfile)->system_header_p)
6960 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6962 cpp_skip_hspace (pfile);
6964 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6965 parse_name (pfile, GETC());
6966 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6967 "assertion");
6969 cpp_skip_hspace (pfile);
6970 if (PEEKC() == '(') {
6971 int error_flag = 0;
6973 tokens = read_token_list (pfile, &error_flag);
6974 if (error_flag)
6975 goto error;
6976 if (tokens == 0) {
6977 cpp_error (pfile, "empty token list in `#unassert'");
6978 goto error;
6981 tokens_specified = 1;
6984 cpp_skip_hspace (pfile);
6985 c = PEEKC ();
6986 if (c != EOF && c != '\n')
6987 cpp_error (pfile, "junk at end of `#unassert'");
6988 skip_rest_of_line (pfile);
6991 ASSERTION_HASHNODE *hp;
6992 U_CHAR *symname = pfile->token_buffer + symstart;
6993 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6994 struct tokenlist_list *tail, *prev;
6996 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6997 if (hp == NULL)
6998 return 1;
7000 /* If no token list was specified, then eliminate this assertion
7001 entirely. */
7002 if (! tokens_specified)
7003 delete_assertion (hp);
7004 else {
7005 /* If a list of tokens was given, then delete any matching list. */
7007 tail = hp->value;
7008 prev = 0;
7009 while (tail) {
7010 struct tokenlist_list *next = tail->next;
7011 if (compare_token_lists (tail->tokens, tokens)) {
7012 if (prev)
7013 prev->next = next;
7014 else
7015 hp->value = tail->next;
7016 free_token_list (tail->tokens);
7017 free (tail);
7018 } else {
7019 prev = tail;
7021 tail = next;
7026 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7027 return 0;
7028 error:
7029 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
7030 skip_rest_of_line (pfile);
7031 return 1;
7034 /* Test whether there is an assertion named NAME
7035 and optionally whether it has an asserted token list TOKENS.
7036 NAME is not null terminated; its length is SYM_LENGTH.
7037 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
7040 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
7041 cpp_reader *pfile;
7042 U_CHAR *name;
7043 int sym_length;
7044 int tokens_specified;
7045 struct arglist *tokens;
7047 ASSERTION_HASHNODE *hp;
7048 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
7050 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
7051 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
7053 hp = assertion_lookup (pfile, name, sym_length, hashcode);
7054 if (hp == NULL)
7055 /* It is not an assertion; just return false. */
7056 return 0;
7058 /* If no token list was specified, then value is 1. */
7059 if (! tokens_specified)
7060 return 1;
7063 struct tokenlist_list *tail;
7065 tail = hp->value;
7067 /* If a list of tokens was given,
7068 then succeed if the assertion records a matching list. */
7070 while (tail) {
7071 if (compare_token_lists (tail->tokens, tokens))
7072 return 1;
7073 tail = tail->next;
7076 /* Fail if the assertion has no matching list. */
7077 return 0;
7081 /* Compare two lists of tokens for equality including order of tokens. */
7083 static int
7084 compare_token_lists (l1, l2)
7085 struct arglist *l1, *l2;
7087 while (l1 && l2) {
7088 if (l1->length != l2->length)
7089 return 0;
7090 if (strncmp (l1->name, l2->name, l1->length))
7091 return 0;
7092 l1 = l1->next;
7093 l2 = l2->next;
7096 /* Succeed if both lists end at the same time. */
7097 return l1 == l2;
7100 struct arglist *
7101 reverse_token_list (tokens)
7102 struct arglist *tokens;
7104 register struct arglist *prev = 0, *this, *next;
7105 for (this = tokens; this; this = next)
7107 next = this->next;
7108 this->next = prev;
7109 prev = this;
7111 return prev;
7114 /* Read a space-separated list of tokens ending in a close parenthesis.
7115 Return a list of strings, in the order they were written.
7116 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
7118 static struct arglist *
7119 read_token_list (pfile, error_flag)
7120 cpp_reader *pfile;
7121 int *error_flag;
7123 struct arglist *token_ptrs = 0;
7124 int depth = 1;
7125 int length;
7127 *error_flag = 0;
7128 FORWARD (1); /* Skip '(' */
7130 /* Loop over the assertion value tokens. */
7131 while (depth > 0)
7133 struct arglist *temp;
7134 long name_written = CPP_WRITTEN (pfile);
7135 int eofp = 0; int c;
7137 cpp_skip_hspace (pfile);
7139 c = GETC ();
7141 /* Find the end of the token. */
7142 if (c == '(')
7144 CPP_PUTC (pfile, c);
7145 depth++;
7147 else if (c == ')')
7149 depth--;
7150 if (depth == 0)
7151 break;
7152 CPP_PUTC (pfile, c);
7154 else if (c == '"' || c == '\'')
7156 FORWARD(-1);
7157 cpp_get_token (pfile);
7159 else if (c == '\n')
7160 break;
7161 else
7163 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
7164 && c != '"' && c != '\'')
7166 CPP_PUTC (pfile, c);
7167 c = GETC();
7169 if (c != EOF) FORWARD(-1);
7172 length = CPP_WRITTEN (pfile) - name_written;
7173 temp = (struct arglist *)
7174 xmalloc (sizeof (struct arglist) + length + 1);
7175 temp->name = (U_CHAR *) (temp + 1);
7176 bcopy ((char *) (pfile->token_buffer + name_written),
7177 (char *) temp->name, length);
7178 temp->name[length] = 0;
7179 temp->next = token_ptrs;
7180 token_ptrs = temp;
7181 temp->length = length;
7183 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
7185 if (c == EOF || c == '\n')
7186 { /* FIXME */
7187 cpp_error (pfile,
7188 "unterminated token sequence following `#' operator");
7189 return 0;
7193 /* We accumulated the names in reverse order.
7194 Now reverse them to get the proper order. */
7195 return reverse_token_list (token_ptrs);
7198 static void
7199 free_token_list (tokens)
7200 struct arglist *tokens;
7202 while (tokens) {
7203 struct arglist *next = tokens->next;
7204 free (tokens->name);
7205 free (tokens);
7206 tokens = next;
7210 /* Get the file-mode and data size of the file open on FD
7211 and store them in *MODE_POINTER and *SIZE_POINTER. */
7213 static int
7214 file_size_and_mode (fd, mode_pointer, size_pointer)
7215 int fd;
7216 int *mode_pointer;
7217 long int *size_pointer;
7219 struct stat sbuf;
7221 if (fstat (fd, &sbuf) < 0) return (-1);
7222 if (mode_pointer) *mode_pointer = sbuf.st_mode;
7223 if (size_pointer) *size_pointer = sbuf.st_size;
7224 return 0;
7227 /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
7228 retrying if necessary. If MAX_READ_LEN is defined, read at most
7229 that bytes at a time. Return a negative value if an error occurs,
7230 otherwise return the actual number of bytes read,
7231 which must be LEN unless end-of-file was reached. */
7233 static int
7234 safe_read (desc, ptr, len)
7235 int desc;
7236 char *ptr;
7237 int len;
7239 int left, rcount, nchars;
7241 left = len;
7242 while (left > 0) {
7243 rcount = left;
7244 #ifdef MAX_READ_LEN
7245 if (rcount > MAX_READ_LEN)
7246 rcount = MAX_READ_LEN;
7247 #endif
7248 nchars = read (desc, ptr, rcount);
7249 if (nchars < 0)
7251 #ifdef EINTR
7252 if (errno == EINTR)
7253 continue;
7254 #endif
7255 return nchars;
7257 if (nchars == 0)
7258 break;
7259 ptr += nchars;
7260 left -= nchars;
7262 return len - left;
7265 static char *
7266 xcalloc (number, size)
7267 unsigned number, size;
7269 register unsigned total = number * size;
7270 register char *ptr = (char *) xmalloc (total);
7271 bzero (ptr, total);
7272 return ptr;
7275 static char *
7276 savestring (input)
7277 char *input;
7279 unsigned size = strlen (input);
7280 char *output = xmalloc (size + 1);
7281 strcpy (output, input);
7282 return output;
7285 /* Initialize PMARK to remember the current position of PFILE. */
7287 void
7288 parse_set_mark (pmark, pfile)
7289 struct parse_marker *pmark;
7290 cpp_reader *pfile;
7292 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7293 pmark->next = pbuf->marks;
7294 pbuf->marks = pmark;
7295 pmark->buf = pbuf;
7296 pmark->position = pbuf->cur - pbuf->buf;
7299 /* Cleanup PMARK - we no longer need it. */
7301 void
7302 parse_clear_mark (pmark)
7303 struct parse_marker *pmark;
7305 struct parse_marker **pp = &pmark->buf->marks;
7306 for (; ; pp = &(*pp)->next) {
7307 if (*pp == NULL) abort ();
7308 if (*pp == pmark) break;
7310 *pp = pmark->next;
7313 /* Backup the current position of PFILE to that saved in PMARK. */
7315 void
7316 parse_goto_mark (pmark, pfile)
7317 struct parse_marker *pmark;
7318 cpp_reader *pfile;
7320 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7321 if (pbuf != pmark->buf)
7322 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
7323 pbuf->cur = pbuf->buf + pmark->position;
7326 /* Reset PMARK to point to the current position of PFILE. (Same
7327 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
7329 void
7330 parse_move_mark (pmark, pfile)
7331 struct parse_marker *pmark;
7332 cpp_reader *pfile;
7334 cpp_buffer *pbuf = CPP_BUFFER (pfile);
7335 if (pbuf != pmark->buf)
7336 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
7337 pmark->position = pbuf->cur - pbuf->buf;
7341 cpp_read_check_assertion (pfile)
7342 cpp_reader *pfile;
7344 int name_start = CPP_WRITTEN (pfile);
7345 int name_length, name_written;
7346 int result;
7347 FORWARD (1); /* Skip '#' */
7348 cpp_skip_hspace (pfile);
7349 parse_name (pfile, GETC ());
7350 name_written = CPP_WRITTEN (pfile);
7351 name_length = name_written - name_start;
7352 cpp_skip_hspace (pfile);
7353 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
7355 int error_flag;
7356 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
7357 result = check_assertion (pfile,
7358 pfile->token_buffer + name_start, name_length,
7359 1, token_ptrs);
7361 else
7362 result = check_assertion (pfile,
7363 pfile->token_buffer + name_start, name_length,
7364 0, NULL_PTR);
7365 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
7366 return result;
7369 void
7370 cpp_print_file_and_line (pfile)
7371 cpp_reader *pfile;
7373 cpp_buffer *ip = cpp_file_buffer (pfile);
7375 if (ip != NULL)
7377 long line, col;
7378 cpp_buf_line_and_col (ip, &line, &col);
7379 cpp_file_line_for_message (pfile, ip->nominal_fname,
7380 line, pfile->show_column ? col : -1);
7384 void
7385 cpp_error (pfile, msg, arg1, arg2, arg3)
7386 cpp_reader *pfile;
7387 char *msg;
7388 char *arg1, *arg2, *arg3;
7390 cpp_print_containing_files (pfile);
7391 cpp_print_file_and_line (pfile);
7392 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7395 /* Print error message but don't count it. */
7397 void
7398 cpp_warning (pfile, msg, arg1, arg2, arg3)
7399 cpp_reader *pfile;
7400 char *msg;
7401 char *arg1, *arg2, *arg3;
7403 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7404 return;
7406 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7407 pfile->errors++;
7409 cpp_print_containing_files (pfile);
7410 cpp_print_file_and_line (pfile);
7411 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7414 /* Print an error message and maybe count it. */
7416 void
7417 cpp_pedwarn (pfile, msg, arg1, arg2, arg3)
7418 cpp_reader *pfile;
7419 char *msg;
7420 char *arg1, *arg2, *arg3;
7422 if (CPP_OPTIONS (pfile)->pedantic_errors)
7423 cpp_error (pfile, msg, arg1, arg2, arg3);
7424 else
7425 cpp_warning (pfile, msg, arg1, arg2, arg3);
7428 void
7429 cpp_error_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7430 cpp_reader *pfile;
7431 int line, column;
7432 char *msg;
7433 char *arg1, *arg2, *arg3;
7435 int i;
7436 cpp_buffer *ip = cpp_file_buffer (pfile);
7438 cpp_print_containing_files (pfile);
7440 if (ip != NULL)
7441 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7443 cpp_message (pfile, 1, msg, arg1, arg2, arg3);
7446 static void
7447 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7448 cpp_reader *pfile;
7449 int line, column;
7450 char *msg;
7451 char *arg1, *arg2, *arg3;
7453 int i;
7454 cpp_buffer *ip;
7456 if (CPP_OPTIONS (pfile)->inhibit_warnings)
7457 return;
7459 if (CPP_OPTIONS (pfile)->warnings_are_errors)
7460 pfile->errors++;
7462 cpp_print_containing_files (pfile);
7464 ip = cpp_file_buffer (pfile);
7466 if (ip != NULL)
7467 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
7469 cpp_message (pfile, 0, msg, arg1, arg2, arg3);
7472 void
7473 cpp_pedwarn_with_line (pfile, line, column, msg, arg1, arg2, arg3)
7474 cpp_reader *pfile;
7475 int line;
7476 char *msg;
7477 char *arg1, *arg2, *arg3;
7479 if (CPP_OPTIONS (pfile)->pedantic_errors)
7480 cpp_error_with_line (pfile, column, line, msg, arg1, arg2, arg3);
7481 else
7482 cpp_warning_with_line (pfile, line, column, msg, arg1, arg2, arg3);
7485 /* Report a warning (or an error if pedantic_errors)
7486 giving specified file name and line number, not current. */
7488 void
7489 cpp_pedwarn_with_file_and_line (pfile, file, line, msg, arg1, arg2, arg3)
7490 cpp_reader *pfile;
7491 char *file;
7492 int line;
7493 char *msg;
7494 char *arg1, *arg2, *arg3;
7496 if (!CPP_OPTIONS (pfile)->pedantic_errors
7497 && CPP_OPTIONS (pfile)->inhibit_warnings)
7498 return;
7499 if (file != NULL)
7500 cpp_file_line_for_message (pfile, file, line, -1);
7501 cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors,
7502 msg, arg1, arg2, arg3);
7505 /* This defines "errno" properly for VMS, and gives us EACCES. */
7506 #include <errno.h>
7507 #ifndef errno
7508 extern int errno;
7509 #endif
7511 #ifndef VMS
7512 #ifndef HAVE_STRERROR
7513 extern int sys_nerr;
7514 extern char *sys_errlist[];
7515 #else /* HAVE_STRERROR */
7516 char *strerror ();
7517 #endif
7518 #else /* VMS */
7519 char *strerror (int,...);
7520 #endif
7522 /* my_strerror - return the descriptive text associated with an
7523 `errno' code. */
7525 char *
7526 my_strerror (errnum)
7527 int errnum;
7529 char *result;
7531 #ifndef VMS
7532 #ifndef HAVE_STRERROR
7533 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
7534 #else
7535 result = strerror (errnum);
7536 #endif
7537 #else /* VMS */
7538 /* VAXCRTL's strerror() takes an optional second argument, which only
7539 matters when the first argument is EVMSERR. However, it's simplest
7540 just to pass it unconditionally. `vaxc$errno' is declared in
7541 <errno.h>, and maintained by the library in parallel with `errno'.
7542 We assume that caller's `errnum' either matches the last setting of
7543 `errno' by the library or else does not have the value `EVMSERR'. */
7545 result = strerror (errnum, vaxc$errno);
7546 #endif
7548 if (!result)
7549 result = "undocumented I/O error";
7551 return result;
7554 /* Error including a message from `errno'. */
7556 void
7557 cpp_error_from_errno (pfile, name)
7558 cpp_reader *pfile;
7559 char *name;
7561 int i;
7562 cpp_buffer *ip = cpp_file_buffer (pfile);
7564 cpp_print_containing_files (pfile);
7566 if (ip != NULL)
7567 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7569 cpp_message (pfile, 1, "%s: %s", name, my_strerror (errno));
7572 void
7573 cpp_perror_with_name (pfile, name)
7574 cpp_reader *pfile;
7575 char *name;
7577 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7580 /* TODO:
7581 * No pre-compiled header file support.
7583 * Possibly different enum token codes for each C/C++ token.
7585 * Should clean up remaining directives to that do_XXX functions
7586 * only take two arguments and all have command_reads_line.
7588 * Find and cleanup remaining uses of static variables,
7590 * Support for trigraphs.
7592 * Support -dM flag (dump_all_macros).
7594 * Support for_lint flag.